blob: 1eb060429c3feb0d04bff0c901099631ad3236e8 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@google.combb6992a2011-04-26 17:41:56 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * 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.
reed@google.combb6992a2011-04-26 17:41:56 +00007 */
8
9
epoger@google.comec3ed6a2011-07-28 14:26:00 +000010
reed@google.combb6992a2011-04-26 17:41:56 +000011#ifndef SkGPipePriv_DEFINED
12#define SkGPipePriv_DEFINED
13
14#include "SkTypes.h"
15
16#define UNIMPLEMENTED
17
reed@google.comb55d1182011-05-11 00:42:04 +000018// these must be contiguous, 0...N-1
19enum PaintFlats {
20 kColorFilter_PaintFlat,
reed@google.com0faac1e2011-05-11 05:58:58 +000021 kDrawLooper_PaintFlat,
reed@google.comb55d1182011-05-11 00:42:04 +000022 kMaskFilter_PaintFlat,
23 kPathEffect_PaintFlat,
24 kRasterizer_PaintFlat,
25 kShader_PaintFlat,
scroggo@google.com16d1d0b2012-05-02 19:09:40 +000026 kImageFilter_PaintFlat,
reed@google.comb55d1182011-05-11 00:42:04 +000027 kXfermode_PaintFlat,
28
29 kLast_PaintFlat = kXfermode_PaintFlat
30};
31#define kCount_PaintFlats (kLast_PaintFlat + 1)
32
reed@google.combb6992a2011-04-26 17:41:56 +000033enum DrawOps {
reed@google.comacd471f2011-05-03 21:26:46 +000034 kSkip_DrawOp, // skip an addition N bytes (N == data)
35
reed@google.combb6992a2011-04-26 17:41:56 +000036 // these match Canvas apis
37 kClipPath_DrawOp,
38 kClipRegion_DrawOp,
39 kClipRect_DrawOp,
40 kConcat_DrawOp,
41 kDrawBitmap_DrawOp,
42 kDrawBitmapMatrix_DrawOp,
scroggo@google.com16d1d0b2012-05-02 19:09:40 +000043 kDrawBitmapNine_DrawOp,
reed@google.combb6992a2011-04-26 17:41:56 +000044 kDrawBitmapRect_DrawOp,
45 kDrawClear_DrawOp,
46 kDrawData_DrawOp,
47 kDrawPaint_DrawOp,
48 kDrawPath_DrawOp,
49 kDrawPicture_DrawOp,
50 kDrawPoints_DrawOp,
51 kDrawPosText_DrawOp,
52 kDrawPosTextH_DrawOp,
53 kDrawRect_DrawOp,
reed@google.combb6992a2011-04-26 17:41:56 +000054 kDrawSprite_DrawOp,
55 kDrawText_DrawOp,
56 kDrawTextOnPath_DrawOp,
57 kDrawVertices_DrawOp,
58 kRestore_DrawOp,
59 kRotate_DrawOp,
60 kSave_DrawOp,
61 kSaveLayer_DrawOp,
62 kScale_DrawOp,
63 kSetMatrix_DrawOp,
64 kSkew_DrawOp,
65 kTranslate_DrawOp,
66
reed@google.combb6992a2011-04-26 17:41:56 +000067 kPaintOp_DrawOp,
scroggo@google.com3cb969f2012-07-27 20:39:19 +000068 kSetTypeface_DrawOp,
reed@google.combb6793b2011-05-05 15:18:15 +000069
reed@google.combb6793b2011-05-05 15:18:15 +000070 kDef_Typeface_DrawOp,
reed@google.com0faac1e2011-05-11 05:58:58 +000071 kDef_Flattenable_DrawOp,
scroggo@google.com16d1d0b2012-05-02 19:09:40 +000072 kDef_Bitmap_DrawOp,
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +000073 kDef_Factory_DrawOp,
reed@google.combb6992a2011-04-26 17:41:56 +000074
75 // these are signals to playback, not drawing verbs
scroggo@google.com565254b2012-06-28 15:41:32 +000076 kReportFlags_DrawOp,
reed@google.combb6992a2011-04-26 17:41:56 +000077 kDone_DrawOp,
78};
79
80/**
81 * DrawOp packs into a 32bit int as follows
82 *
83 * DrawOp:8 - Flags:4 - Data:20
84 *
85 * Flags and Data are called out separately, so we can reuse Data between
86 * different Ops that might have different Flags. e.g. Data might be a Paint
87 * index for both drawRect (no flags) and saveLayer (does have flags).
88 *
89 * All Ops that take a SkPaint use their Data field to store the index to
90 * the paint (previously defined with kPaintOp_DrawOp).
91 */
92
93#define DRAWOPS_OP_BITS 8
94#define DRAWOPS_FLAG_BITS 4
95#define DRAWOPS_DATA_BITS 20
96
97#define DRAWOPS_OP_MASK ((1 << DRAWOPS_OP_BITS) - 1)
98#define DRAWOPS_FLAG_MASK ((1 << DRAWOPS_FLAG_BITS) - 1)
99#define DRAWOPS_DATA_MASK ((1 << DRAWOPS_DATA_BITS) - 1)
100
caryclark@google.com920901d2012-06-06 12:04:11 +0000101static inline unsigned DrawOp_unpackOp(uint32_t op32) {
reed@google.combb6992a2011-04-26 17:41:56 +0000102 return (op32 >> (DRAWOPS_FLAG_BITS + DRAWOPS_DATA_BITS));
103}
104
caryclark@google.com920901d2012-06-06 12:04:11 +0000105static inline unsigned DrawOp_unpackFlags(uint32_t op32) {
reed@google.combb6992a2011-04-26 17:41:56 +0000106 return (op32 >> DRAWOPS_DATA_BITS) & DRAWOPS_FLAG_MASK;
107}
108
caryclark@google.com920901d2012-06-06 12:04:11 +0000109static inline unsigned DrawOp_unpackData(uint32_t op32) {
reed@google.combb6992a2011-04-26 17:41:56 +0000110 return op32 & DRAWOPS_DATA_MASK;
111}
112
caryclark@google.com920901d2012-06-06 12:04:11 +0000113static inline uint32_t DrawOp_packOpFlagData(DrawOps op, unsigned flags, unsigned data) {
reed@google.combb6992a2011-04-26 17:41:56 +0000114 SkASSERT(0 == (op & ~DRAWOPS_OP_MASK));
115 SkASSERT(0 == (flags & ~DRAWOPS_FLAG_MASK));
116 SkASSERT(0 == (data & ~DRAWOPS_DATA_MASK));
117
reed@google.com67908f22011-06-27 14:47:50 +0000118 return (op << (DRAWOPS_FLAG_BITS + DRAWOPS_DATA_BITS)) |
reed@google.combb6992a2011-04-26 17:41:56 +0000119 (flags << DRAWOPS_DATA_BITS) |
120 data;
121}
122
123/** DrawOp specific flag bits
124 */
125
126enum {
127 kSaveLayer_HasBounds_DrawOpFlag = 1 << 0,
128 kSaveLayer_HasPaint_DrawOpFlag = 1 << 1,
129};
130enum {
131 kClear_HasColor_DrawOpFlag = 1 << 0
132};
133enum {
134 kDrawTextOnPath_HasMatrix_DrawOpFlag = 1 << 0
135};
136enum {
137 kDrawVertices_HasTexs_DrawOpFlag = 1 << 0,
138 kDrawVertices_HasColors_DrawOpFlag = 1 << 1,
139 kDrawVertices_HasIndices_DrawOpFlag = 1 << 2,
140};
scroggo@google.com58be6822012-07-30 14:40:01 +0000141enum {
142 kDrawBitmap_HasPaint_DrawOpsFlag = 1 << 0,
143 // Specific to drawBitmapRect, but needs to be different from HasPaint,
144 // which is used for all drawBitmap calls, so include it here.
145 kDrawBitmap_HasSrcRect_DrawOpsFlag = 1 << 1,
146};
reed@google.combb6992a2011-04-26 17:41:56 +0000147
148///////////////////////////////////////////////////////////////////////////////
149
scroggo@google.com284bf502012-07-17 16:10:34 +0000150class BitmapInfo : SkNoncopyable {
151public:
152 BitmapInfo(SkBitmap* bitmap, uint32_t genID, int toBeDrawnCount)
153 : fBitmap(bitmap)
154 , fGenID(genID)
scroggo@google.com15011ee2012-07-26 20:03:32 +0000155 , fBytesAllocated(0)
scroggo@google.com284bf502012-07-17 16:10:34 +0000156 , fMoreRecentlyUsed(NULL)
157 , fLessRecentlyUsed(NULL)
158 , fToBeDrawnCount(toBeDrawnCount)
159 {}
160
161 ~BitmapInfo() {
162 SkASSERT(0 == fToBeDrawnCount);
163 SkDELETE(fBitmap);
164 }
165
166 void addDraws(int drawsToAdd) {
167 if (0 == fToBeDrawnCount) {
168 // The readers will only ever decrement the count, so once the
169 // count is zero, the writer will be the only one modifying it,
170 // so it does not need to be an atomic operation.
171 fToBeDrawnCount = drawsToAdd;
172 } else {
173 sk_atomic_add(&fToBeDrawnCount, drawsToAdd);
174 }
175 }
176
177 void decDraws() {
178 sk_atomic_dec(&fToBeDrawnCount);
179 }
180
181 int drawCount() const {
182 return fToBeDrawnCount;
183 }
184
185 SkBitmap* fBitmap;
186 // Store the generation ID of the original bitmap, since copying does
187 // not copy this field, so fBitmap's generation ID will not be useful
188 // for comparing.
scroggo@google.com15011ee2012-07-26 20:03:32 +0000189 // FIXME: Is it reasonable to make copying a bitmap/pixelref copy the
190 // generation ID?
scroggo@google.com284bf502012-07-17 16:10:34 +0000191 uint32_t fGenID;
scroggo@google.com15011ee2012-07-26 20:03:32 +0000192 // Keep track of the bytes allocated for this bitmap. When replacing the
193 // bitmap or removing this BitmapInfo we know how much memory has been
194 // reclaimed.
195 size_t fBytesAllocated;
scroggo@google.com284bf502012-07-17 16:10:34 +0000196 // TODO: Generalize the LRU caching mechanism
197 BitmapInfo* fMoreRecentlyUsed;
198 BitmapInfo* fLessRecentlyUsed;
199private:
200 int fToBeDrawnCount;
201};
202
scroggo@google.com565254b2012-06-28 15:41:32 +0000203static inline bool shouldFlattenBitmaps(uint32_t flags) {
scroggo@google.com3cb969f2012-07-27 20:39:19 +0000204 return SkToBool(flags & SkGPipeWriter::kCrossProcess_Flag
205 && !(flags & SkGPipeWriter::kSharedAddressSpace_Flag));
scroggo@google.com565254b2012-06-28 15:41:32 +0000206}
207
208///////////////////////////////////////////////////////////////////////////////
209
reed@google.combb6992a2011-04-26 17:41:56 +0000210enum PaintOps {
211 kReset_PaintOp, // no arg
212
213 kFlags_PaintOp, // arg inline
214 kColor_PaintOp, // arg 32
215 kStyle_PaintOp, // arg inline
216 kJoin_PaintOp, // arg inline
217 kCap_PaintOp, // arg inline
218 kWidth_PaintOp, // arg scalar
219 kMiter_PaintOp,// arg scalar
220
221 kEncoding_PaintOp, // arg inline - text
222 kHinting_PaintOp, // arg inline - text
223 kAlign_PaintOp, // arg inline - text
224 kTextSize_PaintOp, // arg scalar - text
225 kTextScaleX_PaintOp,// arg scalar - text
226 kTextSkewX_PaintOp, // arg scalar - text
reed@google.comf5842f72011-05-04 18:30:04 +0000227 kTypeface_PaintOp, // arg inline (index) - text
228
reed@google.comb55d1182011-05-11 00:42:04 +0000229 kFlatIndex_PaintOp, // flags=paintflat, data=index
reed@google.combb6992a2011-04-26 17:41:56 +0000230};
231
232#define PAINTOPS_OP_BITS 8
233#define PAINTOPS_FLAG_BITS 4
234#define PAINTOPS_DATA_BITS 20
235
236#define PAINTOPS_OP_MASK ((1 << PAINTOPS_OP_BITS) - 1)
237#define PAINTOPS_FLAG_MASK ((1 << PAINTOPS_FLAG_BITS) - 1)
238#define PAINTOPS_DATA_MASK ((1 << PAINTOPS_DATA_BITS) - 1)
239
caryclark@google.com920901d2012-06-06 12:04:11 +0000240static inline unsigned PaintOp_unpackOp(uint32_t op32) {
reed@google.combb6992a2011-04-26 17:41:56 +0000241 return (op32 >> (PAINTOPS_FLAG_BITS + PAINTOPS_DATA_BITS));
242}
243
caryclark@google.com920901d2012-06-06 12:04:11 +0000244static inline unsigned PaintOp_unpackFlags(uint32_t op32) {
reed@google.combb6992a2011-04-26 17:41:56 +0000245 return (op32 >> PAINTOPS_DATA_BITS) & PAINTOPS_FLAG_MASK;
246}
247
caryclark@google.com920901d2012-06-06 12:04:11 +0000248static inline unsigned PaintOp_unpackData(uint32_t op32) {
reed@google.combb6992a2011-04-26 17:41:56 +0000249 return op32 & PAINTOPS_DATA_MASK;
250}
251
caryclark@google.com920901d2012-06-06 12:04:11 +0000252static inline uint32_t PaintOp_packOp(PaintOps op) {
reed@google.combb6992a2011-04-26 17:41:56 +0000253 SkASSERT(0 == (op & ~PAINTOPS_OP_MASK));
254
reed@google.com67908f22011-06-27 14:47:50 +0000255 return op << (PAINTOPS_FLAG_BITS + PAINTOPS_DATA_BITS);
reed@google.combb6992a2011-04-26 17:41:56 +0000256}
257
caryclark@google.com920901d2012-06-06 12:04:11 +0000258static inline uint32_t PaintOp_packOpData(PaintOps op, unsigned data) {
reed@google.combb6992a2011-04-26 17:41:56 +0000259 SkASSERT(0 == (op & ~PAINTOPS_OP_MASK));
260 SkASSERT(0 == (data & ~PAINTOPS_DATA_MASK));
261
reed@google.com67908f22011-06-27 14:47:50 +0000262 return (op << (PAINTOPS_FLAG_BITS + PAINTOPS_DATA_BITS)) | data;
reed@google.combb6992a2011-04-26 17:41:56 +0000263}
264
caryclark@google.com920901d2012-06-06 12:04:11 +0000265static inline uint32_t PaintOp_packOpFlagData(PaintOps op, unsigned flags, unsigned data) {
reed@google.combb6992a2011-04-26 17:41:56 +0000266 SkASSERT(0 == (op & ~PAINTOPS_OP_MASK));
267 SkASSERT(0 == (flags & ~PAINTOPS_FLAG_MASK));
268 SkASSERT(0 == (data & ~PAINTOPS_DATA_MASK));
269
reed@google.com67908f22011-06-27 14:47:50 +0000270 return (op << (PAINTOPS_FLAG_BITS + PAINTOPS_DATA_BITS)) |
reed@google.combb6992a2011-04-26 17:41:56 +0000271 (flags << PAINTOPS_DATA_BITS) |
272 data;
273}
274
reed@google.combb6992a2011-04-26 17:41:56 +0000275#endif