blob: 8d2a2af294b718c26fbace2db99cfa8113a11727 [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.com35348222013-10-16 13:05:06 +000022 kImageFilter_PaintFlat,
reed@google.comb55d1182011-05-11 00:42:04 +000023 kMaskFilter_PaintFlat,
24 kPathEffect_PaintFlat,
25 kRasterizer_PaintFlat,
26 kShader_PaintFlat,
27 kXfermode_PaintFlat,
28
reed@google.com0cd2ac62013-10-14 20:02:44 +000029 kLast_PaintFlat = kXfermode_PaintFlat
reed@google.comb55d1182011-05-11 00:42:04 +000030};
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,
commit-bot@chromium.orged9806f2014-02-21 02:32:36 +000048 kDrawDRRect_DrawOp,
piotaixr687732f2014-10-16 11:55:35 -070049 kDrawImage_DrawOp,
50 kDrawImageRect_DrawOp,
reed@google.com4ed0fb72012-12-12 20:48:18 +000051 kDrawOval_DrawOp,
reed@google.combb6992a2011-04-26 17:41:56 +000052 kDrawPaint_DrawOp,
dandov963137b2014-08-07 07:49:53 -070053 kDrawPatch_DrawOp,
reed@google.combb6992a2011-04-26 17:41:56 +000054 kDrawPath_DrawOp,
55 kDrawPicture_DrawOp,
56 kDrawPoints_DrawOp,
57 kDrawPosText_DrawOp,
58 kDrawPosTextH_DrawOp,
59 kDrawRect_DrawOp,
reed@google.com4ed0fb72012-12-12 20:48:18 +000060 kDrawRRect_DrawOp,
reed@google.combb6992a2011-04-26 17:41:56 +000061 kDrawSprite_DrawOp,
62 kDrawText_DrawOp,
fmalitab7425172014-08-26 07:56:44 -070063 kDrawTextBlob_DrawOp,
reed@google.combb6992a2011-04-26 17:41:56 +000064 kDrawTextOnPath_DrawOp,
65 kDrawVertices_DrawOp,
66 kRestore_DrawOp,
67 kRotate_DrawOp,
68 kSave_DrawOp,
69 kSaveLayer_DrawOp,
70 kScale_DrawOp,
71 kSetMatrix_DrawOp,
72 kSkew_DrawOp,
73 kTranslate_DrawOp,
74
reed@google.combb6992a2011-04-26 17:41:56 +000075 kPaintOp_DrawOp,
scroggo@google.com3cb969f2012-07-27 20:39:19 +000076 kSetTypeface_DrawOp,
reed@google.com0cd2ac62013-10-14 20:02:44 +000077 kSetAnnotation_DrawOp,
reed@google.combb6793b2011-05-05 15:18:15 +000078
reed@google.combb6793b2011-05-05 15:18:15 +000079 kDef_Typeface_DrawOp,
reed@google.com0faac1e2011-05-11 05:58:58 +000080 kDef_Flattenable_DrawOp,
scroggo@google.com16d1d0b2012-05-02 19:09:40 +000081 kDef_Bitmap_DrawOp,
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +000082 kDef_Factory_DrawOp,
reed@google.combb6992a2011-04-26 17:41:56 +000083
84 // these are signals to playback, not drawing verbs
scroggo@google.com565254b2012-06-28 15:41:32 +000085 kReportFlags_DrawOp,
scroggo@google.comd9d29672012-08-14 17:21:34 +000086 kShareBitmapHeap_DrawOp,
reed@google.combb6992a2011-04-26 17:41:56 +000087 kDone_DrawOp,
88};
89
90/**
91 * DrawOp packs into a 32bit int as follows
92 *
93 * DrawOp:8 - Flags:4 - Data:20
94 *
95 * Flags and Data are called out separately, so we can reuse Data between
96 * different Ops that might have different Flags. e.g. Data might be a Paint
97 * index for both drawRect (no flags) and saveLayer (does have flags).
98 *
99 * All Ops that take a SkPaint use their Data field to store the index to
100 * the paint (previously defined with kPaintOp_DrawOp).
101 */
102
103#define DRAWOPS_OP_BITS 8
104#define DRAWOPS_FLAG_BITS 4
105#define DRAWOPS_DATA_BITS 20
106
107#define DRAWOPS_OP_MASK ((1 << DRAWOPS_OP_BITS) - 1)
108#define DRAWOPS_FLAG_MASK ((1 << DRAWOPS_FLAG_BITS) - 1)
109#define DRAWOPS_DATA_MASK ((1 << DRAWOPS_DATA_BITS) - 1)
110
caryclark@google.com920901d2012-06-06 12:04:11 +0000111static inline unsigned DrawOp_unpackOp(uint32_t op32) {
reed@google.combb6992a2011-04-26 17:41:56 +0000112 return (op32 >> (DRAWOPS_FLAG_BITS + DRAWOPS_DATA_BITS));
113}
114
caryclark@google.com920901d2012-06-06 12:04:11 +0000115static inline unsigned DrawOp_unpackFlags(uint32_t op32) {
reed@google.combb6992a2011-04-26 17:41:56 +0000116 return (op32 >> DRAWOPS_DATA_BITS) & DRAWOPS_FLAG_MASK;
117}
118
caryclark@google.com920901d2012-06-06 12:04:11 +0000119static inline unsigned DrawOp_unpackData(uint32_t op32) {
reed@google.combb6992a2011-04-26 17:41:56 +0000120 return op32 & DRAWOPS_DATA_MASK;
121}
122
caryclark@google.com920901d2012-06-06 12:04:11 +0000123static inline uint32_t DrawOp_packOpFlagData(DrawOps op, unsigned flags, unsigned data) {
reed@google.combb6992a2011-04-26 17:41:56 +0000124 SkASSERT(0 == (op & ~DRAWOPS_OP_MASK));
125 SkASSERT(0 == (flags & ~DRAWOPS_FLAG_MASK));
126 SkASSERT(0 == (data & ~DRAWOPS_DATA_MASK));
127
reed@google.com67908f22011-06-27 14:47:50 +0000128 return (op << (DRAWOPS_FLAG_BITS + DRAWOPS_DATA_BITS)) |
reed@google.combb6992a2011-04-26 17:41:56 +0000129 (flags << DRAWOPS_DATA_BITS) |
130 data;
131}
132
133/** DrawOp specific flag bits
134 */
135
136enum {
137 kSaveLayer_HasBounds_DrawOpFlag = 1 << 0,
138 kSaveLayer_HasPaint_DrawOpFlag = 1 << 1,
139};
140enum {
141 kClear_HasColor_DrawOpFlag = 1 << 0
142};
143enum {
144 kDrawTextOnPath_HasMatrix_DrawOpFlag = 1 << 0
145};
146enum {
147 kDrawVertices_HasTexs_DrawOpFlag = 1 << 0,
148 kDrawVertices_HasColors_DrawOpFlag = 1 << 1,
149 kDrawVertices_HasIndices_DrawOpFlag = 1 << 2,
reed@google.com85e143c2013-12-30 15:51:25 +0000150 kDrawVertices_HasXfermode_DrawOpFlag = 1 << 3,
reed@google.combb6992a2011-04-26 17:41:56 +0000151};
scroggo@google.com58be6822012-07-30 14:40:01 +0000152enum {
scroggo@google.com460a23e2012-08-16 17:56:49 +0000153 kDrawBitmap_HasPaint_DrawOpFlag = 1 << 0,
scroggo@google.com58be6822012-07-30 14:40:01 +0000154 // Specific to drawBitmapRect, but needs to be different from HasPaint,
155 // which is used for all drawBitmap calls, so include it here.
scroggo@google.com460a23e2012-08-16 17:56:49 +0000156 kDrawBitmap_HasSrcRect_DrawOpFlag = 1 << 1,
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000157 // SkCanvas::DrawBitmapRectFlags::kBleed_DrawBitmapRectFlag is
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +0000158 // converted into and out of this flag to save space
159 kDrawBitmap_Bleed_DrawOpFlag = 1 << 2,
scroggo@google.com58be6822012-07-30 14:40:01 +0000160};
scroggo@google.com460a23e2012-08-16 17:56:49 +0000161enum {
piotaixr687732f2014-10-16 11:55:35 -0700162 kDrawImage_HasPaint_DrawOpFlag = 1 << 0,
163 kDrawImage_HasSrcRect_DrawOpFlag = 1 << 1,
164};
165enum {
scroggo@google.com460a23e2012-08-16 17:56:49 +0000166 kClip_HasAntiAlias_DrawOpFlag = 1 << 0,
167};
reed@google.combb6992a2011-04-26 17:41:56 +0000168///////////////////////////////////////////////////////////////////////////////
169
scroggo@google.com284bf502012-07-17 16:10:34 +0000170class BitmapInfo : SkNoncopyable {
171public:
172 BitmapInfo(SkBitmap* bitmap, uint32_t genID, int toBeDrawnCount)
173 : fBitmap(bitmap)
174 , fGenID(genID)
scroggo@google.com15011ee2012-07-26 20:03:32 +0000175 , fBytesAllocated(0)
scroggo@google.com284bf502012-07-17 16:10:34 +0000176 , fMoreRecentlyUsed(NULL)
177 , fLessRecentlyUsed(NULL)
178 , fToBeDrawnCount(toBeDrawnCount)
179 {}
180
181 ~BitmapInfo() {
182 SkASSERT(0 == fToBeDrawnCount);
183 SkDELETE(fBitmap);
184 }
185
186 void addDraws(int drawsToAdd) {
187 if (0 == fToBeDrawnCount) {
188 // The readers will only ever decrement the count, so once the
189 // count is zero, the writer will be the only one modifying it,
190 // so it does not need to be an atomic operation.
191 fToBeDrawnCount = drawsToAdd;
192 } else {
193 sk_atomic_add(&fToBeDrawnCount, drawsToAdd);
194 }
195 }
196
197 void decDraws() {
198 sk_atomic_dec(&fToBeDrawnCount);
199 }
200
201 int drawCount() const {
202 return fToBeDrawnCount;
203 }
204
205 SkBitmap* fBitmap;
206 // Store the generation ID of the original bitmap, since copying does
207 // not copy this field, so fBitmap's generation ID will not be useful
208 // for comparing.
scroggo@google.com15011ee2012-07-26 20:03:32 +0000209 // FIXME: Is it reasonable to make copying a bitmap/pixelref copy the
210 // generation ID?
scroggo@google.com284bf502012-07-17 16:10:34 +0000211 uint32_t fGenID;
scroggo@google.com15011ee2012-07-26 20:03:32 +0000212 // Keep track of the bytes allocated for this bitmap. When replacing the
213 // bitmap or removing this BitmapInfo we know how much memory has been
214 // reclaimed.
215 size_t fBytesAllocated;
scroggo@google.com284bf502012-07-17 16:10:34 +0000216 // TODO: Generalize the LRU caching mechanism
217 BitmapInfo* fMoreRecentlyUsed;
218 BitmapInfo* fLessRecentlyUsed;
219private:
220 int fToBeDrawnCount;
221};
222
scroggo@google.com565254b2012-06-28 15:41:32 +0000223static inline bool shouldFlattenBitmaps(uint32_t flags) {
scroggo@google.com3cb969f2012-07-27 20:39:19 +0000224 return SkToBool(flags & SkGPipeWriter::kCrossProcess_Flag
225 && !(flags & SkGPipeWriter::kSharedAddressSpace_Flag));
scroggo@google.com565254b2012-06-28 15:41:32 +0000226}
227
228///////////////////////////////////////////////////////////////////////////////
229
reed@google.combb6992a2011-04-26 17:41:56 +0000230enum PaintOps {
231 kReset_PaintOp, // no arg
rmistry@google.comd6176b02012-08-23 18:14:13 +0000232
reed@google.combb6992a2011-04-26 17:41:56 +0000233 kFlags_PaintOp, // arg inline
234 kColor_PaintOp, // arg 32
commit-bot@chromium.org85faf502014-04-16 12:58:02 +0000235 kFilterLevel_PaintOp, // arg inline
reed@google.combb6992a2011-04-26 17:41:56 +0000236 kStyle_PaintOp, // arg inline
237 kJoin_PaintOp, // arg inline
238 kCap_PaintOp, // arg inline
239 kWidth_PaintOp, // arg scalar
reed@google.com0cd2ac62013-10-14 20:02:44 +0000240 kMiter_PaintOp, // arg scalar
rmistry@google.comd6176b02012-08-23 18:14:13 +0000241
reed@google.combb6992a2011-04-26 17:41:56 +0000242 kEncoding_PaintOp, // arg inline - text
243 kHinting_PaintOp, // arg inline - text
244 kAlign_PaintOp, // arg inline - text
245 kTextSize_PaintOp, // arg scalar - text
246 kTextScaleX_PaintOp,// arg scalar - text
247 kTextSkewX_PaintOp, // arg scalar - text
reed@google.comf5842f72011-05-04 18:30:04 +0000248 kTypeface_PaintOp, // arg inline (index) - text
249
reed@google.comb55d1182011-05-11 00:42:04 +0000250 kFlatIndex_PaintOp, // flags=paintflat, data=index
reed@google.combb6992a2011-04-26 17:41:56 +0000251};
252
253#define PAINTOPS_OP_BITS 8
254#define PAINTOPS_FLAG_BITS 4
255#define PAINTOPS_DATA_BITS 20
256
257#define PAINTOPS_OP_MASK ((1 << PAINTOPS_OP_BITS) - 1)
258#define PAINTOPS_FLAG_MASK ((1 << PAINTOPS_FLAG_BITS) - 1)
259#define PAINTOPS_DATA_MASK ((1 << PAINTOPS_DATA_BITS) - 1)
260
caryclark@google.com920901d2012-06-06 12:04:11 +0000261static inline unsigned PaintOp_unpackOp(uint32_t op32) {
reed@google.combb6992a2011-04-26 17:41:56 +0000262 return (op32 >> (PAINTOPS_FLAG_BITS + PAINTOPS_DATA_BITS));
263}
264
caryclark@google.com920901d2012-06-06 12:04:11 +0000265static inline unsigned PaintOp_unpackFlags(uint32_t op32) {
reed@google.combb6992a2011-04-26 17:41:56 +0000266 return (op32 >> PAINTOPS_DATA_BITS) & PAINTOPS_FLAG_MASK;
267}
268
caryclark@google.com920901d2012-06-06 12:04:11 +0000269static inline unsigned PaintOp_unpackData(uint32_t op32) {
reed@google.combb6992a2011-04-26 17:41:56 +0000270 return op32 & PAINTOPS_DATA_MASK;
271}
272
caryclark@google.com920901d2012-06-06 12:04:11 +0000273static inline uint32_t PaintOp_packOp(PaintOps op) {
reed@google.combb6992a2011-04-26 17:41:56 +0000274 SkASSERT(0 == (op & ~PAINTOPS_OP_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}
278
caryclark@google.com920901d2012-06-06 12:04:11 +0000279static inline uint32_t PaintOp_packOpData(PaintOps op, unsigned data) {
reed@google.combb6992a2011-04-26 17:41:56 +0000280 SkASSERT(0 == (op & ~PAINTOPS_OP_MASK));
281 SkASSERT(0 == (data & ~PAINTOPS_DATA_MASK));
rmistry@google.comd6176b02012-08-23 18:14:13 +0000282
reed@google.com67908f22011-06-27 14:47:50 +0000283 return (op << (PAINTOPS_FLAG_BITS + PAINTOPS_DATA_BITS)) | data;
reed@google.combb6992a2011-04-26 17:41:56 +0000284}
285
caryclark@google.com920901d2012-06-06 12:04:11 +0000286static inline uint32_t PaintOp_packOpFlagData(PaintOps op, unsigned flags, unsigned data) {
reed@google.combb6992a2011-04-26 17:41:56 +0000287 SkASSERT(0 == (op & ~PAINTOPS_OP_MASK));
288 SkASSERT(0 == (flags & ~PAINTOPS_FLAG_MASK));
289 SkASSERT(0 == (data & ~PAINTOPS_DATA_MASK));
rmistry@google.comd6176b02012-08-23 18:14:13 +0000290
reed@google.com67908f22011-06-27 14:47:50 +0000291 return (op << (PAINTOPS_FLAG_BITS + PAINTOPS_DATA_BITS)) |
reed@google.combb6992a2011-04-26 17:41:56 +0000292 (flags << PAINTOPS_DATA_BITS) |
293 data;
294}
295
reed@google.combb6992a2011-04-26 17:41:56 +0000296#endif