blob: b563652cb90256f52e79a2b12aa3c6ed7f4afbdd [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,
reed@google.com4ed0fb72012-12-12 20:48:18 +000040 kClipRRect_DrawOp,
reed@google.combb6992a2011-04-26 17:41:56 +000041 kConcat_DrawOp,
42 kDrawBitmap_DrawOp,
43 kDrawBitmapMatrix_DrawOp,
scroggo@google.com16d1d0b2012-05-02 19:09:40 +000044 kDrawBitmapNine_DrawOp,
reed@google.com71121732012-09-18 15:14:33 +000045 kDrawBitmapRectToRect_DrawOp,
reed@google.combb6992a2011-04-26 17:41:56 +000046 kDrawClear_DrawOp,
47 kDrawData_DrawOp,
reed@google.com4ed0fb72012-12-12 20:48:18 +000048 kDrawOval_DrawOp,
reed@google.combb6992a2011-04-26 17:41:56 +000049 kDrawPaint_DrawOp,
50 kDrawPath_DrawOp,
51 kDrawPicture_DrawOp,
52 kDrawPoints_DrawOp,
53 kDrawPosText_DrawOp,
54 kDrawPosTextH_DrawOp,
55 kDrawRect_DrawOp,
reed@google.com4ed0fb72012-12-12 20:48:18 +000056 kDrawRRect_DrawOp,
reed@google.combb6992a2011-04-26 17:41:56 +000057 kDrawSprite_DrawOp,
58 kDrawText_DrawOp,
59 kDrawTextOnPath_DrawOp,
60 kDrawVertices_DrawOp,
61 kRestore_DrawOp,
62 kRotate_DrawOp,
63 kSave_DrawOp,
64 kSaveLayer_DrawOp,
65 kScale_DrawOp,
66 kSetMatrix_DrawOp,
67 kSkew_DrawOp,
68 kTranslate_DrawOp,
69
reed@google.combb6992a2011-04-26 17:41:56 +000070 kPaintOp_DrawOp,
scroggo@google.com3cb969f2012-07-27 20:39:19 +000071 kSetTypeface_DrawOp,
reed@google.combb6793b2011-05-05 15:18:15 +000072
reed@google.combb6793b2011-05-05 15:18:15 +000073 kDef_Typeface_DrawOp,
reed@google.com0faac1e2011-05-11 05:58:58 +000074 kDef_Flattenable_DrawOp,
scroggo@google.com16d1d0b2012-05-02 19:09:40 +000075 kDef_Bitmap_DrawOp,
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +000076 kDef_Factory_DrawOp,
reed@google.combb6992a2011-04-26 17:41:56 +000077
78 // these are signals to playback, not drawing verbs
scroggo@google.com565254b2012-06-28 15:41:32 +000079 kReportFlags_DrawOp,
scroggo@google.comd9d29672012-08-14 17:21:34 +000080 kShareBitmapHeap_DrawOp,
reed@google.combb6992a2011-04-26 17:41:56 +000081 kDone_DrawOp,
82};
83
84/**
85 * DrawOp packs into a 32bit int as follows
86 *
87 * DrawOp:8 - Flags:4 - Data:20
88 *
89 * Flags and Data are called out separately, so we can reuse Data between
90 * different Ops that might have different Flags. e.g. Data might be a Paint
91 * index for both drawRect (no flags) and saveLayer (does have flags).
92 *
93 * All Ops that take a SkPaint use their Data field to store the index to
94 * the paint (previously defined with kPaintOp_DrawOp).
95 */
96
97#define DRAWOPS_OP_BITS 8
98#define DRAWOPS_FLAG_BITS 4
99#define DRAWOPS_DATA_BITS 20
100
101#define DRAWOPS_OP_MASK ((1 << DRAWOPS_OP_BITS) - 1)
102#define DRAWOPS_FLAG_MASK ((1 << DRAWOPS_FLAG_BITS) - 1)
103#define DRAWOPS_DATA_MASK ((1 << DRAWOPS_DATA_BITS) - 1)
104
caryclark@google.com920901d2012-06-06 12:04:11 +0000105static inline unsigned DrawOp_unpackOp(uint32_t op32) {
reed@google.combb6992a2011-04-26 17:41:56 +0000106 return (op32 >> (DRAWOPS_FLAG_BITS + DRAWOPS_DATA_BITS));
107}
108
caryclark@google.com920901d2012-06-06 12:04:11 +0000109static inline unsigned DrawOp_unpackFlags(uint32_t op32) {
reed@google.combb6992a2011-04-26 17:41:56 +0000110 return (op32 >> DRAWOPS_DATA_BITS) & DRAWOPS_FLAG_MASK;
111}
112
caryclark@google.com920901d2012-06-06 12:04:11 +0000113static inline unsigned DrawOp_unpackData(uint32_t op32) {
reed@google.combb6992a2011-04-26 17:41:56 +0000114 return op32 & DRAWOPS_DATA_MASK;
115}
116
caryclark@google.com920901d2012-06-06 12:04:11 +0000117static inline uint32_t DrawOp_packOpFlagData(DrawOps op, unsigned flags, unsigned data) {
reed@google.combb6992a2011-04-26 17:41:56 +0000118 SkASSERT(0 == (op & ~DRAWOPS_OP_MASK));
119 SkASSERT(0 == (flags & ~DRAWOPS_FLAG_MASK));
120 SkASSERT(0 == (data & ~DRAWOPS_DATA_MASK));
121
reed@google.com67908f22011-06-27 14:47:50 +0000122 return (op << (DRAWOPS_FLAG_BITS + DRAWOPS_DATA_BITS)) |
reed@google.combb6992a2011-04-26 17:41:56 +0000123 (flags << DRAWOPS_DATA_BITS) |
124 data;
125}
126
127/** DrawOp specific flag bits
128 */
129
130enum {
131 kSaveLayer_HasBounds_DrawOpFlag = 1 << 0,
132 kSaveLayer_HasPaint_DrawOpFlag = 1 << 1,
133};
134enum {
135 kClear_HasColor_DrawOpFlag = 1 << 0
136};
137enum {
138 kDrawTextOnPath_HasMatrix_DrawOpFlag = 1 << 0
139};
140enum {
141 kDrawVertices_HasTexs_DrawOpFlag = 1 << 0,
142 kDrawVertices_HasColors_DrawOpFlag = 1 << 1,
143 kDrawVertices_HasIndices_DrawOpFlag = 1 << 2,
144};
scroggo@google.com58be6822012-07-30 14:40:01 +0000145enum {
scroggo@google.com460a23e2012-08-16 17:56:49 +0000146 kDrawBitmap_HasPaint_DrawOpFlag = 1 << 0,
scroggo@google.com58be6822012-07-30 14:40:01 +0000147 // Specific to drawBitmapRect, but needs to be different from HasPaint,
148 // which is used for all drawBitmap calls, so include it here.
scroggo@google.com460a23e2012-08-16 17:56:49 +0000149 kDrawBitmap_HasSrcRect_DrawOpFlag = 1 << 1,
scroggo@google.com58be6822012-07-30 14:40:01 +0000150};
scroggo@google.com460a23e2012-08-16 17:56:49 +0000151enum {
152 kClip_HasAntiAlias_DrawOpFlag = 1 << 0,
153};
reed@google.combb6992a2011-04-26 17:41:56 +0000154///////////////////////////////////////////////////////////////////////////////
155
scroggo@google.com284bf502012-07-17 16:10:34 +0000156class BitmapInfo : SkNoncopyable {
157public:
158 BitmapInfo(SkBitmap* bitmap, uint32_t genID, int toBeDrawnCount)
159 : fBitmap(bitmap)
160 , fGenID(genID)
scroggo@google.com15011ee2012-07-26 20:03:32 +0000161 , fBytesAllocated(0)
scroggo@google.com284bf502012-07-17 16:10:34 +0000162 , fMoreRecentlyUsed(NULL)
163 , fLessRecentlyUsed(NULL)
164 , fToBeDrawnCount(toBeDrawnCount)
165 {}
166
167 ~BitmapInfo() {
168 SkASSERT(0 == fToBeDrawnCount);
169 SkDELETE(fBitmap);
170 }
171
172 void addDraws(int drawsToAdd) {
173 if (0 == fToBeDrawnCount) {
174 // The readers will only ever decrement the count, so once the
175 // count is zero, the writer will be the only one modifying it,
176 // so it does not need to be an atomic operation.
177 fToBeDrawnCount = drawsToAdd;
178 } else {
179 sk_atomic_add(&fToBeDrawnCount, drawsToAdd);
180 }
181 }
182
183 void decDraws() {
184 sk_atomic_dec(&fToBeDrawnCount);
185 }
186
187 int drawCount() const {
188 return fToBeDrawnCount;
189 }
190
191 SkBitmap* fBitmap;
192 // Store the generation ID of the original bitmap, since copying does
193 // not copy this field, so fBitmap's generation ID will not be useful
194 // for comparing.
scroggo@google.com15011ee2012-07-26 20:03:32 +0000195 // FIXME: Is it reasonable to make copying a bitmap/pixelref copy the
196 // generation ID?
scroggo@google.com284bf502012-07-17 16:10:34 +0000197 uint32_t fGenID;
scroggo@google.com15011ee2012-07-26 20:03:32 +0000198 // Keep track of the bytes allocated for this bitmap. When replacing the
199 // bitmap or removing this BitmapInfo we know how much memory has been
200 // reclaimed.
201 size_t fBytesAllocated;
scroggo@google.com284bf502012-07-17 16:10:34 +0000202 // TODO: Generalize the LRU caching mechanism
203 BitmapInfo* fMoreRecentlyUsed;
204 BitmapInfo* fLessRecentlyUsed;
205private:
206 int fToBeDrawnCount;
207};
208
scroggo@google.com565254b2012-06-28 15:41:32 +0000209static inline bool shouldFlattenBitmaps(uint32_t flags) {
scroggo@google.com3cb969f2012-07-27 20:39:19 +0000210 return SkToBool(flags & SkGPipeWriter::kCrossProcess_Flag
211 && !(flags & SkGPipeWriter::kSharedAddressSpace_Flag));
scroggo@google.com565254b2012-06-28 15:41:32 +0000212}
213
214///////////////////////////////////////////////////////////////////////////////
215
reed@google.combb6992a2011-04-26 17:41:56 +0000216enum PaintOps {
217 kReset_PaintOp, // no arg
rmistry@google.comd6176b02012-08-23 18:14:13 +0000218
reed@google.combb6992a2011-04-26 17:41:56 +0000219 kFlags_PaintOp, // arg inline
220 kColor_PaintOp, // arg 32
221 kStyle_PaintOp, // arg inline
222 kJoin_PaintOp, // arg inline
223 kCap_PaintOp, // arg inline
224 kWidth_PaintOp, // arg scalar
225 kMiter_PaintOp,// arg scalar
rmistry@google.comd6176b02012-08-23 18:14:13 +0000226
reed@google.combb6992a2011-04-26 17:41:56 +0000227 kEncoding_PaintOp, // arg inline - text
228 kHinting_PaintOp, // arg inline - text
229 kAlign_PaintOp, // arg inline - text
230 kTextSize_PaintOp, // arg scalar - text
231 kTextScaleX_PaintOp,// arg scalar - text
232 kTextSkewX_PaintOp, // arg scalar - text
reed@google.comf5842f72011-05-04 18:30:04 +0000233 kTypeface_PaintOp, // arg inline (index) - text
234
reed@google.comb55d1182011-05-11 00:42:04 +0000235 kFlatIndex_PaintOp, // flags=paintflat, data=index
reed@google.combb6992a2011-04-26 17:41:56 +0000236};
237
238#define PAINTOPS_OP_BITS 8
239#define PAINTOPS_FLAG_BITS 4
240#define PAINTOPS_DATA_BITS 20
241
242#define PAINTOPS_OP_MASK ((1 << PAINTOPS_OP_BITS) - 1)
243#define PAINTOPS_FLAG_MASK ((1 << PAINTOPS_FLAG_BITS) - 1)
244#define PAINTOPS_DATA_MASK ((1 << PAINTOPS_DATA_BITS) - 1)
245
caryclark@google.com920901d2012-06-06 12:04:11 +0000246static inline unsigned PaintOp_unpackOp(uint32_t op32) {
reed@google.combb6992a2011-04-26 17:41:56 +0000247 return (op32 >> (PAINTOPS_FLAG_BITS + PAINTOPS_DATA_BITS));
248}
249
caryclark@google.com920901d2012-06-06 12:04:11 +0000250static inline unsigned PaintOp_unpackFlags(uint32_t op32) {
reed@google.combb6992a2011-04-26 17:41:56 +0000251 return (op32 >> PAINTOPS_DATA_BITS) & PAINTOPS_FLAG_MASK;
252}
253
caryclark@google.com920901d2012-06-06 12:04:11 +0000254static inline unsigned PaintOp_unpackData(uint32_t op32) {
reed@google.combb6992a2011-04-26 17:41:56 +0000255 return op32 & PAINTOPS_DATA_MASK;
256}
257
caryclark@google.com920901d2012-06-06 12:04:11 +0000258static inline uint32_t PaintOp_packOp(PaintOps op) {
reed@google.combb6992a2011-04-26 17:41:56 +0000259 SkASSERT(0 == (op & ~PAINTOPS_OP_MASK));
rmistry@google.comd6176b02012-08-23 18:14:13 +0000260
reed@google.com67908f22011-06-27 14:47:50 +0000261 return op << (PAINTOPS_FLAG_BITS + PAINTOPS_DATA_BITS);
reed@google.combb6992a2011-04-26 17:41:56 +0000262}
263
caryclark@google.com920901d2012-06-06 12:04:11 +0000264static inline uint32_t PaintOp_packOpData(PaintOps op, unsigned data) {
reed@google.combb6992a2011-04-26 17:41:56 +0000265 SkASSERT(0 == (op & ~PAINTOPS_OP_MASK));
266 SkASSERT(0 == (data & ~PAINTOPS_DATA_MASK));
rmistry@google.comd6176b02012-08-23 18:14:13 +0000267
reed@google.com67908f22011-06-27 14:47:50 +0000268 return (op << (PAINTOPS_FLAG_BITS + PAINTOPS_DATA_BITS)) | data;
reed@google.combb6992a2011-04-26 17:41:56 +0000269}
270
caryclark@google.com920901d2012-06-06 12:04:11 +0000271static inline uint32_t PaintOp_packOpFlagData(PaintOps op, unsigned flags, unsigned data) {
reed@google.combb6992a2011-04-26 17:41:56 +0000272 SkASSERT(0 == (op & ~PAINTOPS_OP_MASK));
273 SkASSERT(0 == (flags & ~PAINTOPS_FLAG_MASK));
274 SkASSERT(0 == (data & ~PAINTOPS_DATA_MASK));
rmistry@google.comd6176b02012-08-23 18:14:13 +0000275
reed@google.com67908f22011-06-27 14:47:50 +0000276 return (op << (PAINTOPS_FLAG_BITS + PAINTOPS_DATA_BITS)) |
reed@google.combb6992a2011-04-26 17:41:56 +0000277 (flags << PAINTOPS_DATA_BITS) |
278 data;
279}
280
reed@google.combb6992a2011-04-26 17:41:56 +0000281#endif