blob: c68f3ec33851b5eff586596030de543da680e86c [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,
scroggo@google.comd9d29672012-08-14 17:21:34 +000077 kShareBitmapHeap_DrawOp,
reed@google.combb6992a2011-04-26 17:41:56 +000078 kDone_DrawOp,
79};
80
81/**
82 * DrawOp packs into a 32bit int as follows
83 *
84 * DrawOp:8 - Flags:4 - Data:20
85 *
86 * Flags and Data are called out separately, so we can reuse Data between
87 * different Ops that might have different Flags. e.g. Data might be a Paint
88 * index for both drawRect (no flags) and saveLayer (does have flags).
89 *
90 * All Ops that take a SkPaint use their Data field to store the index to
91 * the paint (previously defined with kPaintOp_DrawOp).
92 */
93
94#define DRAWOPS_OP_BITS 8
95#define DRAWOPS_FLAG_BITS 4
96#define DRAWOPS_DATA_BITS 20
97
98#define DRAWOPS_OP_MASK ((1 << DRAWOPS_OP_BITS) - 1)
99#define DRAWOPS_FLAG_MASK ((1 << DRAWOPS_FLAG_BITS) - 1)
100#define DRAWOPS_DATA_MASK ((1 << DRAWOPS_DATA_BITS) - 1)
101
caryclark@google.com920901d2012-06-06 12:04:11 +0000102static inline unsigned DrawOp_unpackOp(uint32_t op32) {
reed@google.combb6992a2011-04-26 17:41:56 +0000103 return (op32 >> (DRAWOPS_FLAG_BITS + DRAWOPS_DATA_BITS));
104}
105
caryclark@google.com920901d2012-06-06 12:04:11 +0000106static inline unsigned DrawOp_unpackFlags(uint32_t op32) {
reed@google.combb6992a2011-04-26 17:41:56 +0000107 return (op32 >> DRAWOPS_DATA_BITS) & DRAWOPS_FLAG_MASK;
108}
109
caryclark@google.com920901d2012-06-06 12:04:11 +0000110static inline unsigned DrawOp_unpackData(uint32_t op32) {
reed@google.combb6992a2011-04-26 17:41:56 +0000111 return op32 & DRAWOPS_DATA_MASK;
112}
113
caryclark@google.com920901d2012-06-06 12:04:11 +0000114static inline uint32_t DrawOp_packOpFlagData(DrawOps op, unsigned flags, unsigned data) {
reed@google.combb6992a2011-04-26 17:41:56 +0000115 SkASSERT(0 == (op & ~DRAWOPS_OP_MASK));
116 SkASSERT(0 == (flags & ~DRAWOPS_FLAG_MASK));
117 SkASSERT(0 == (data & ~DRAWOPS_DATA_MASK));
118
reed@google.com67908f22011-06-27 14:47:50 +0000119 return (op << (DRAWOPS_FLAG_BITS + DRAWOPS_DATA_BITS)) |
reed@google.combb6992a2011-04-26 17:41:56 +0000120 (flags << DRAWOPS_DATA_BITS) |
121 data;
122}
123
124/** DrawOp specific flag bits
125 */
126
127enum {
128 kSaveLayer_HasBounds_DrawOpFlag = 1 << 0,
129 kSaveLayer_HasPaint_DrawOpFlag = 1 << 1,
130};
131enum {
132 kClear_HasColor_DrawOpFlag = 1 << 0
133};
134enum {
135 kDrawTextOnPath_HasMatrix_DrawOpFlag = 1 << 0
136};
137enum {
138 kDrawVertices_HasTexs_DrawOpFlag = 1 << 0,
139 kDrawVertices_HasColors_DrawOpFlag = 1 << 1,
140 kDrawVertices_HasIndices_DrawOpFlag = 1 << 2,
141};
scroggo@google.com58be6822012-07-30 14:40:01 +0000142enum {
143 kDrawBitmap_HasPaint_DrawOpsFlag = 1 << 0,
144 // Specific to drawBitmapRect, but needs to be different from HasPaint,
145 // which is used for all drawBitmap calls, so include it here.
146 kDrawBitmap_HasSrcRect_DrawOpsFlag = 1 << 1,
147};
reed@google.combb6992a2011-04-26 17:41:56 +0000148
149///////////////////////////////////////////////////////////////////////////////
150
scroggo@google.com284bf502012-07-17 16:10:34 +0000151class BitmapInfo : SkNoncopyable {
152public:
153 BitmapInfo(SkBitmap* bitmap, uint32_t genID, int toBeDrawnCount)
154 : fBitmap(bitmap)
155 , fGenID(genID)
scroggo@google.com15011ee2012-07-26 20:03:32 +0000156 , fBytesAllocated(0)
scroggo@google.com284bf502012-07-17 16:10:34 +0000157 , fMoreRecentlyUsed(NULL)
158 , fLessRecentlyUsed(NULL)
159 , fToBeDrawnCount(toBeDrawnCount)
160 {}
161
162 ~BitmapInfo() {
163 SkASSERT(0 == fToBeDrawnCount);
164 SkDELETE(fBitmap);
165 }
166
167 void addDraws(int drawsToAdd) {
168 if (0 == fToBeDrawnCount) {
169 // The readers will only ever decrement the count, so once the
170 // count is zero, the writer will be the only one modifying it,
171 // so it does not need to be an atomic operation.
172 fToBeDrawnCount = drawsToAdd;
173 } else {
174 sk_atomic_add(&fToBeDrawnCount, drawsToAdd);
175 }
176 }
177
178 void decDraws() {
179 sk_atomic_dec(&fToBeDrawnCount);
180 }
181
182 int drawCount() const {
183 return fToBeDrawnCount;
184 }
185
186 SkBitmap* fBitmap;
187 // Store the generation ID of the original bitmap, since copying does
188 // not copy this field, so fBitmap's generation ID will not be useful
189 // for comparing.
scroggo@google.com15011ee2012-07-26 20:03:32 +0000190 // FIXME: Is it reasonable to make copying a bitmap/pixelref copy the
191 // generation ID?
scroggo@google.com284bf502012-07-17 16:10:34 +0000192 uint32_t fGenID;
scroggo@google.com15011ee2012-07-26 20:03:32 +0000193 // Keep track of the bytes allocated for this bitmap. When replacing the
194 // bitmap or removing this BitmapInfo we know how much memory has been
195 // reclaimed.
196 size_t fBytesAllocated;
scroggo@google.com284bf502012-07-17 16:10:34 +0000197 // TODO: Generalize the LRU caching mechanism
198 BitmapInfo* fMoreRecentlyUsed;
199 BitmapInfo* fLessRecentlyUsed;
200private:
201 int fToBeDrawnCount;
202};
203
scroggo@google.com565254b2012-06-28 15:41:32 +0000204static inline bool shouldFlattenBitmaps(uint32_t flags) {
scroggo@google.com3cb969f2012-07-27 20:39:19 +0000205 return SkToBool(flags & SkGPipeWriter::kCrossProcess_Flag
206 && !(flags & SkGPipeWriter::kSharedAddressSpace_Flag));
scroggo@google.com565254b2012-06-28 15:41:32 +0000207}
208
209///////////////////////////////////////////////////////////////////////////////
210
reed@google.combb6992a2011-04-26 17:41:56 +0000211enum PaintOps {
212 kReset_PaintOp, // no arg
213
214 kFlags_PaintOp, // arg inline
215 kColor_PaintOp, // arg 32
216 kStyle_PaintOp, // arg inline
217 kJoin_PaintOp, // arg inline
218 kCap_PaintOp, // arg inline
219 kWidth_PaintOp, // arg scalar
220 kMiter_PaintOp,// arg scalar
221
222 kEncoding_PaintOp, // arg inline - text
223 kHinting_PaintOp, // arg inline - text
224 kAlign_PaintOp, // arg inline - text
225 kTextSize_PaintOp, // arg scalar - text
226 kTextScaleX_PaintOp,// arg scalar - text
227 kTextSkewX_PaintOp, // arg scalar - text
reed@google.comf5842f72011-05-04 18:30:04 +0000228 kTypeface_PaintOp, // arg inline (index) - text
229
reed@google.comb55d1182011-05-11 00:42:04 +0000230 kFlatIndex_PaintOp, // flags=paintflat, data=index
reed@google.combb6992a2011-04-26 17:41:56 +0000231};
232
233#define PAINTOPS_OP_BITS 8
234#define PAINTOPS_FLAG_BITS 4
235#define PAINTOPS_DATA_BITS 20
236
237#define PAINTOPS_OP_MASK ((1 << PAINTOPS_OP_BITS) - 1)
238#define PAINTOPS_FLAG_MASK ((1 << PAINTOPS_FLAG_BITS) - 1)
239#define PAINTOPS_DATA_MASK ((1 << PAINTOPS_DATA_BITS) - 1)
240
caryclark@google.com920901d2012-06-06 12:04:11 +0000241static inline unsigned PaintOp_unpackOp(uint32_t op32) {
reed@google.combb6992a2011-04-26 17:41:56 +0000242 return (op32 >> (PAINTOPS_FLAG_BITS + PAINTOPS_DATA_BITS));
243}
244
caryclark@google.com920901d2012-06-06 12:04:11 +0000245static inline unsigned PaintOp_unpackFlags(uint32_t op32) {
reed@google.combb6992a2011-04-26 17:41:56 +0000246 return (op32 >> PAINTOPS_DATA_BITS) & PAINTOPS_FLAG_MASK;
247}
248
caryclark@google.com920901d2012-06-06 12:04:11 +0000249static inline unsigned PaintOp_unpackData(uint32_t op32) {
reed@google.combb6992a2011-04-26 17:41:56 +0000250 return op32 & PAINTOPS_DATA_MASK;
251}
252
caryclark@google.com920901d2012-06-06 12:04:11 +0000253static inline uint32_t PaintOp_packOp(PaintOps op) {
reed@google.combb6992a2011-04-26 17:41:56 +0000254 SkASSERT(0 == (op & ~PAINTOPS_OP_MASK));
255
reed@google.com67908f22011-06-27 14:47:50 +0000256 return op << (PAINTOPS_FLAG_BITS + PAINTOPS_DATA_BITS);
reed@google.combb6992a2011-04-26 17:41:56 +0000257}
258
caryclark@google.com920901d2012-06-06 12:04:11 +0000259static inline uint32_t PaintOp_packOpData(PaintOps op, unsigned data) {
reed@google.combb6992a2011-04-26 17:41:56 +0000260 SkASSERT(0 == (op & ~PAINTOPS_OP_MASK));
261 SkASSERT(0 == (data & ~PAINTOPS_DATA_MASK));
262
reed@google.com67908f22011-06-27 14:47:50 +0000263 return (op << (PAINTOPS_FLAG_BITS + PAINTOPS_DATA_BITS)) | data;
reed@google.combb6992a2011-04-26 17:41:56 +0000264}
265
caryclark@google.com920901d2012-06-06 12:04:11 +0000266static inline uint32_t PaintOp_packOpFlagData(PaintOps op, unsigned flags, unsigned data) {
reed@google.combb6992a2011-04-26 17:41:56 +0000267 SkASSERT(0 == (op & ~PAINTOPS_OP_MASK));
268 SkASSERT(0 == (flags & ~PAINTOPS_FLAG_MASK));
269 SkASSERT(0 == (data & ~PAINTOPS_DATA_MASK));
270
reed@google.com67908f22011-06-27 14:47:50 +0000271 return (op << (PAINTOPS_FLAG_BITS + PAINTOPS_DATA_BITS)) |
reed@google.combb6992a2011-04-26 17:41:56 +0000272 (flags << PAINTOPS_DATA_BITS) |
273 data;
274}
275
reed@google.combb6992a2011-04-26 17:41:56 +0000276#endif