1
2 /*
3 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
9
10
11 #ifndef SkGPipePriv_DEFINED
12 #define SkGPipePriv_DEFINED
13
14 #include "SkTypes.h"
15
16 #define UNIMPLEMENTED
17
18 // these must be contiguous, 0...N-1
19 enum PaintFlats {
20 kColorFilter_PaintFlat,
21 kDrawLooper_PaintFlat,
22 kImageFilter_PaintFlat,
23 kMaskFilter_PaintFlat,
24 kPathEffect_PaintFlat,
25 kRasterizer_PaintFlat,
26 kShader_PaintFlat,
27 kXfermode_PaintFlat,
28
29 kLast_PaintFlat = kXfermode_PaintFlat
30 };
31 #define kCount_PaintFlats (kLast_PaintFlat + 1)
32
33 enum DrawOps {
34 kSkip_DrawOp, // skip an addition N bytes (N == data)
35
36 // these match Canvas apis
37 kClipPath_DrawOp,
38 kClipRegion_DrawOp,
39 kClipRect_DrawOp,
40 kClipRRect_DrawOp,
41 kConcat_DrawOp,
42 kDrawBitmap_DrawOp,
43 kDrawBitmapNine_DrawOp,
44 kDrawBitmapRectToRect_DrawOp,
45 kDrawDRRect_DrawOp,
46 kDrawImage_DrawOp,
47 kDrawImageRect_DrawOp,
48 kDrawOval_DrawOp,
49 kDrawPaint_DrawOp,
50 kDrawPatch_DrawOp,
51 kDrawPath_DrawOp,
52 kDrawPicture_DrawOp,
53 kDrawPoints_DrawOp,
54 kDrawPosText_DrawOp,
55 kDrawPosTextH_DrawOp,
56 kDrawRect_DrawOp,
57 kDrawRRect_DrawOp,
58 kDrawSprite_DrawOp,
59 kDrawText_DrawOp,
60 kDrawTextBlob_DrawOp,
61 kDrawTextOnPath_DrawOp,
62 kDrawVertices_DrawOp,
63 kRestore_DrawOp,
64 kRotate_DrawOp,
65 kSave_DrawOp,
66 kSaveLayer_DrawOp,
67 kScale_DrawOp,
68 kSetMatrix_DrawOp,
69 kSkew_DrawOp,
70 kTranslate_DrawOp,
71
72 kPaintOp_DrawOp,
73 kSetTypeface_DrawOp,
74 kSetAnnotation_DrawOp,
75
76 kDef_Typeface_DrawOp,
77 kDef_Flattenable_DrawOp,
78 kDef_Bitmap_DrawOp,
79 kDef_Factory_DrawOp,
80
81 // these are signals to playback, not drawing verbs
82 kReportFlags_DrawOp,
83 kShareBitmapHeap_DrawOp,
84 kShareImageHeap_DrawOp,
85 kDone_DrawOp,
86 };
87
88 /**
89 * DrawOp packs into a 32bit int as follows
90 *
91 * DrawOp:8 - Flags:4 - Data:20
92 *
93 * Flags and Data are called out separately, so we can reuse Data between
94 * different Ops that might have different Flags. e.g. Data might be a Paint
95 * index for both drawRect (no flags) and saveLayer (does have flags).
96 *
97 * All Ops that take a SkPaint use their Data field to store the index to
98 * the paint (previously defined with kPaintOp_DrawOp).
99 */
100
101 #define DRAWOPS_OP_BITS 8
102 #define DRAWOPS_FLAG_BITS 4
103 #define DRAWOPS_DATA_BITS 20
104
105 #define DRAWOPS_OP_MASK ((1 << DRAWOPS_OP_BITS) - 1)
106 #define DRAWOPS_FLAG_MASK ((1 << DRAWOPS_FLAG_BITS) - 1)
107 #define DRAWOPS_DATA_MASK ((1 << DRAWOPS_DATA_BITS) - 1)
108
DrawOp_unpackOp(uint32_t op32)109 static inline unsigned DrawOp_unpackOp(uint32_t op32) {
110 return (op32 >> (DRAWOPS_FLAG_BITS + DRAWOPS_DATA_BITS));
111 }
112
DrawOp_unpackFlags(uint32_t op32)113 static inline unsigned DrawOp_unpackFlags(uint32_t op32) {
114 return (op32 >> DRAWOPS_DATA_BITS) & DRAWOPS_FLAG_MASK;
115 }
116
DrawOp_unpackData(uint32_t op32)117 static inline unsigned DrawOp_unpackData(uint32_t op32) {
118 return op32 & DRAWOPS_DATA_MASK;
119 }
120
DrawOp_packOpFlagData(DrawOps op,unsigned flags,unsigned data)121 static inline uint32_t DrawOp_packOpFlagData(DrawOps op, unsigned flags, unsigned data) {
122 SkASSERT(0 == (op & ~DRAWOPS_OP_MASK));
123 SkASSERT(0 == (flags & ~DRAWOPS_FLAG_MASK));
124 SkASSERT(0 == (data & ~DRAWOPS_DATA_MASK));
125
126 return (op << (DRAWOPS_FLAG_BITS + DRAWOPS_DATA_BITS)) |
127 (flags << DRAWOPS_DATA_BITS) |
128 data;
129 }
130
131 /** DrawOp specific flag bits
132 */
133
134 enum {
135 kSaveLayer_HasBounds_DrawOpFlag = 1 << 0,
136 kSaveLayer_HasPaint_DrawOpFlag = 1 << 1,
137 };
138 enum {
139 kDrawTextOnPath_HasMatrix_DrawOpFlag = 1 << 0
140 };
141 enum {
142 kDrawVertices_HasTexs_DrawOpFlag = 1 << 0,
143 kDrawVertices_HasColors_DrawOpFlag = 1 << 1,
144 kDrawVertices_HasIndices_DrawOpFlag = 1 << 2,
145 kDrawVertices_HasXfermode_DrawOpFlag = 1 << 3,
146 };
147 // These are shared between drawbitmap and drawimage
148 enum {
149 kDrawBitmap_HasPaint_DrawOpFlag = 1 << 0,
150 // Specific to drawBitmapRect, but needs to be different from HasPaint,
151 // which is used for all drawBitmap calls, so include it here.
152 kDrawBitmap_HasSrcRect_DrawOpFlag = 1 << 1,
153 // SkCanvas::DrawBitmapRectFlags::kBleed_DrawBitmapRectFlag is
154 // converted into and out of this flag to save space
155 kDrawBitmap_Bleed_DrawOpFlag = 1 << 2,
156 };
157 enum {
158 kClip_HasAntiAlias_DrawOpFlag = 1 << 0,
159 };
160 ///////////////////////////////////////////////////////////////////////////////
161
162 class BitmapInfo : SkNoncopyable {
163 public:
BitmapInfo(SkBitmap * bitmap,uint32_t genID,int toBeDrawnCount)164 BitmapInfo(SkBitmap* bitmap, uint32_t genID, int toBeDrawnCount)
165 : fBitmap(bitmap)
166 , fGenID(genID)
167 , fBytesAllocated(0)
168 , fMoreRecentlyUsed(NULL)
169 , fLessRecentlyUsed(NULL)
170 , fToBeDrawnCount(toBeDrawnCount)
171 {}
172
~BitmapInfo()173 ~BitmapInfo() {
174 SkASSERT(0 == fToBeDrawnCount);
175 SkDELETE(fBitmap);
176 }
177
addDraws(int drawsToAdd)178 void addDraws(int drawsToAdd) {
179 if (0 == fToBeDrawnCount) {
180 // The readers will only ever decrement the count, so once the
181 // count is zero, the writer will be the only one modifying it,
182 // so it does not need to be an atomic operation.
183 fToBeDrawnCount = drawsToAdd;
184 } else {
185 sk_atomic_add(&fToBeDrawnCount, drawsToAdd);
186 }
187 }
188
decDraws()189 void decDraws() {
190 sk_atomic_dec(&fToBeDrawnCount);
191 }
192
drawCount()193 int drawCount() const {
194 return fToBeDrawnCount;
195 }
196
197 SkBitmap* fBitmap;
198 // Store the generation ID of the original bitmap, since copying does
199 // not copy this field, so fBitmap's generation ID will not be useful
200 // for comparing.
201 // FIXME: Is it reasonable to make copying a bitmap/pixelref copy the
202 // generation ID?
203 uint32_t fGenID;
204 // Keep track of the bytes allocated for this bitmap. When replacing the
205 // bitmap or removing this BitmapInfo we know how much memory has been
206 // reclaimed.
207 size_t fBytesAllocated;
208 // TODO: Generalize the LRU caching mechanism
209 BitmapInfo* fMoreRecentlyUsed;
210 BitmapInfo* fLessRecentlyUsed;
211 private:
212 int fToBeDrawnCount;
213 };
214
shouldFlattenBitmaps(uint32_t flags)215 static inline bool shouldFlattenBitmaps(uint32_t flags) {
216 return SkToBool(flags & SkGPipeWriter::kCrossProcess_Flag
217 && !(flags & SkGPipeWriter::kSharedAddressSpace_Flag));
218 }
219
220 class SkImageHeap : public SkRefCnt {
221 public:
222 SkImageHeap();
223 virtual ~SkImageHeap();
224
225 // slot must be "valid" -- 0 is never valid
226 const SkImage* get(int32_t slot) const;
227 // returns 0 if not found, else returns slot
228 int32_t find(const SkImage*) const;
229 // returns non-zero value for where the image was stored
230 int32_t insert(const SkImage*);
231
232 private:
233 SkTDArray<const SkImage*> fArray;
234 };
235
236 ///////////////////////////////////////////////////////////////////////////////
237
238 enum PaintOps {
239 kReset_PaintOp, // no arg
240
241 kFlags_PaintOp, // arg inline
242 kColor_PaintOp, // arg 32
243 kFilterLevel_PaintOp, // arg inline
244 kStyle_PaintOp, // arg inline
245 kJoin_PaintOp, // arg inline
246 kCap_PaintOp, // arg inline
247 kWidth_PaintOp, // arg scalar
248 kMiter_PaintOp, // arg scalar
249
250 kEncoding_PaintOp, // arg inline - text
251 kHinting_PaintOp, // arg inline - text
252 kAlign_PaintOp, // arg inline - text
253 kTextSize_PaintOp, // arg scalar - text
254 kTextScaleX_PaintOp,// arg scalar - text
255 kTextSkewX_PaintOp, // arg scalar - text
256 kTypeface_PaintOp, // arg inline (index) - text
257
258 kFlatIndex_PaintOp, // flags=paintflat, data=index
259 };
260
261 #define PAINTOPS_OP_BITS 8
262 #define PAINTOPS_FLAG_BITS 4
263 #define PAINTOPS_DATA_BITS 20
264
265 #define PAINTOPS_OP_MASK ((1 << PAINTOPS_OP_BITS) - 1)
266 #define PAINTOPS_FLAG_MASK ((1 << PAINTOPS_FLAG_BITS) - 1)
267 #define PAINTOPS_DATA_MASK ((1 << PAINTOPS_DATA_BITS) - 1)
268
PaintOp_unpackOp(uint32_t op32)269 static inline unsigned PaintOp_unpackOp(uint32_t op32) {
270 return (op32 >> (PAINTOPS_FLAG_BITS + PAINTOPS_DATA_BITS));
271 }
272
PaintOp_unpackFlags(uint32_t op32)273 static inline unsigned PaintOp_unpackFlags(uint32_t op32) {
274 return (op32 >> PAINTOPS_DATA_BITS) & PAINTOPS_FLAG_MASK;
275 }
276
PaintOp_unpackData(uint32_t op32)277 static inline unsigned PaintOp_unpackData(uint32_t op32) {
278 return op32 & PAINTOPS_DATA_MASK;
279 }
280
PaintOp_packOp(PaintOps op)281 static inline uint32_t PaintOp_packOp(PaintOps op) {
282 SkASSERT(0 == (op & ~PAINTOPS_OP_MASK));
283
284 return op << (PAINTOPS_FLAG_BITS + PAINTOPS_DATA_BITS);
285 }
286
PaintOp_packOpData(PaintOps op,unsigned data)287 static inline uint32_t PaintOp_packOpData(PaintOps op, unsigned data) {
288 SkASSERT(0 == (op & ~PAINTOPS_OP_MASK));
289 SkASSERT(0 == (data & ~PAINTOPS_DATA_MASK));
290
291 return (op << (PAINTOPS_FLAG_BITS + PAINTOPS_DATA_BITS)) | data;
292 }
293
PaintOp_packOpFlagData(PaintOps op,unsigned flags,unsigned data)294 static inline uint32_t PaintOp_packOpFlagData(PaintOps op, unsigned flags, unsigned data) {
295 SkASSERT(0 == (op & ~PAINTOPS_OP_MASK));
296 SkASSERT(0 == (flags & ~PAINTOPS_FLAG_MASK));
297 SkASSERT(0 == (data & ~PAINTOPS_DATA_MASK));
298
299 return (op << (PAINTOPS_FLAG_BITS + PAINTOPS_DATA_BITS)) |
300 (flags << PAINTOPS_DATA_BITS) |
301 data;
302 }
303
304 #endif
305