blob: 72b7f377a4c3e7c8a83382c46d58728eb031c91f [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,
reed@google.combb6793b2011-05-05 15:18:15 +000068
reed@google.combb6793b2011-05-05 15:18:15 +000069 kDef_Typeface_DrawOp,
reed@google.com0faac1e2011-05-11 05:58:58 +000070 kDef_Flattenable_DrawOp,
scroggo@google.com16d1d0b2012-05-02 19:09:40 +000071 kDef_Bitmap_DrawOp,
reed@google.combb6992a2011-04-26 17:41:56 +000072
73 // these are signals to playback, not drawing verbs
scroggo@google.com565254b2012-06-28 15:41:32 +000074 kReportFlags_DrawOp,
reed@google.combb6992a2011-04-26 17:41:56 +000075 kDone_DrawOp,
76};
77
78/**
79 * DrawOp packs into a 32bit int as follows
80 *
81 * DrawOp:8 - Flags:4 - Data:20
82 *
83 * Flags and Data are called out separately, so we can reuse Data between
84 * different Ops that might have different Flags. e.g. Data might be a Paint
85 * index for both drawRect (no flags) and saveLayer (does have flags).
86 *
87 * All Ops that take a SkPaint use their Data field to store the index to
88 * the paint (previously defined with kPaintOp_DrawOp).
89 */
90
91#define DRAWOPS_OP_BITS 8
92#define DRAWOPS_FLAG_BITS 4
93#define DRAWOPS_DATA_BITS 20
94
95#define DRAWOPS_OP_MASK ((1 << DRAWOPS_OP_BITS) - 1)
96#define DRAWOPS_FLAG_MASK ((1 << DRAWOPS_FLAG_BITS) - 1)
97#define DRAWOPS_DATA_MASK ((1 << DRAWOPS_DATA_BITS) - 1)
98
caryclark@google.com920901d2012-06-06 12:04:11 +000099static inline unsigned DrawOp_unpackOp(uint32_t op32) {
reed@google.combb6992a2011-04-26 17:41:56 +0000100 return (op32 >> (DRAWOPS_FLAG_BITS + DRAWOPS_DATA_BITS));
101}
102
caryclark@google.com920901d2012-06-06 12:04:11 +0000103static inline unsigned DrawOp_unpackFlags(uint32_t op32) {
reed@google.combb6992a2011-04-26 17:41:56 +0000104 return (op32 >> DRAWOPS_DATA_BITS) & DRAWOPS_FLAG_MASK;
105}
106
caryclark@google.com920901d2012-06-06 12:04:11 +0000107static inline unsigned DrawOp_unpackData(uint32_t op32) {
reed@google.combb6992a2011-04-26 17:41:56 +0000108 return op32 & DRAWOPS_DATA_MASK;
109}
110
caryclark@google.com920901d2012-06-06 12:04:11 +0000111static inline uint32_t DrawOp_packOpFlagData(DrawOps op, unsigned flags, unsigned data) {
reed@google.combb6992a2011-04-26 17:41:56 +0000112 SkASSERT(0 == (op & ~DRAWOPS_OP_MASK));
113 SkASSERT(0 == (flags & ~DRAWOPS_FLAG_MASK));
114 SkASSERT(0 == (data & ~DRAWOPS_DATA_MASK));
115
reed@google.com67908f22011-06-27 14:47:50 +0000116 return (op << (DRAWOPS_FLAG_BITS + DRAWOPS_DATA_BITS)) |
reed@google.combb6992a2011-04-26 17:41:56 +0000117 (flags << DRAWOPS_DATA_BITS) |
118 data;
119}
120
121/** DrawOp specific flag bits
122 */
123
124enum {
125 kSaveLayer_HasBounds_DrawOpFlag = 1 << 0,
126 kSaveLayer_HasPaint_DrawOpFlag = 1 << 1,
127};
128enum {
129 kClear_HasColor_DrawOpFlag = 1 << 0
130};
131enum {
132 kDrawTextOnPath_HasMatrix_DrawOpFlag = 1 << 0
133};
134enum {
135 kDrawVertices_HasTexs_DrawOpFlag = 1 << 0,
136 kDrawVertices_HasColors_DrawOpFlag = 1 << 1,
137 kDrawVertices_HasIndices_DrawOpFlag = 1 << 2,
138};
139
140///////////////////////////////////////////////////////////////////////////////
141
scroggo@google.com284bf502012-07-17 16:10:34 +0000142class BitmapInfo : SkNoncopyable {
143public:
144 BitmapInfo(SkBitmap* bitmap, uint32_t genID, int toBeDrawnCount)
145 : fBitmap(bitmap)
146 , fGenID(genID)
scroggo@google.com15011ee2012-07-26 20:03:32 +0000147 , fBytesAllocated(0)
scroggo@google.com284bf502012-07-17 16:10:34 +0000148 , fMoreRecentlyUsed(NULL)
149 , fLessRecentlyUsed(NULL)
150 , fToBeDrawnCount(toBeDrawnCount)
151 {}
152
153 ~BitmapInfo() {
154 SkASSERT(0 == fToBeDrawnCount);
155 SkDELETE(fBitmap);
156 }
157
158 void addDraws(int drawsToAdd) {
159 if (0 == fToBeDrawnCount) {
160 // The readers will only ever decrement the count, so once the
161 // count is zero, the writer will be the only one modifying it,
162 // so it does not need to be an atomic operation.
163 fToBeDrawnCount = drawsToAdd;
164 } else {
165 sk_atomic_add(&fToBeDrawnCount, drawsToAdd);
166 }
167 }
168
169 void decDraws() {
170 sk_atomic_dec(&fToBeDrawnCount);
171 }
172
173 int drawCount() const {
174 return fToBeDrawnCount;
175 }
176
177 SkBitmap* fBitmap;
178 // Store the generation ID of the original bitmap, since copying does
179 // not copy this field, so fBitmap's generation ID will not be useful
180 // for comparing.
scroggo@google.com15011ee2012-07-26 20:03:32 +0000181 // FIXME: Is it reasonable to make copying a bitmap/pixelref copy the
182 // generation ID?
scroggo@google.com284bf502012-07-17 16:10:34 +0000183 uint32_t fGenID;
scroggo@google.com15011ee2012-07-26 20:03:32 +0000184 // Keep track of the bytes allocated for this bitmap. When replacing the
185 // bitmap or removing this BitmapInfo we know how much memory has been
186 // reclaimed.
187 size_t fBytesAllocated;
scroggo@google.com284bf502012-07-17 16:10:34 +0000188 // TODO: Generalize the LRU caching mechanism
189 BitmapInfo* fMoreRecentlyUsed;
190 BitmapInfo* fLessRecentlyUsed;
191private:
192 int fToBeDrawnCount;
193};
194
scroggo@google.com565254b2012-06-28 15:41:32 +0000195static inline bool shouldFlattenBitmaps(uint32_t flags) {
196 return flags & SkGPipeWriter::kCrossProcess_Flag
scroggo@google.com15011ee2012-07-26 20:03:32 +0000197 && !(flags & SkGPipeWriter::kSharedAddressSpace_Flag);
scroggo@google.com565254b2012-06-28 15:41:32 +0000198}
199
200///////////////////////////////////////////////////////////////////////////////
201
reed@google.combb6992a2011-04-26 17:41:56 +0000202enum PaintOps {
203 kReset_PaintOp, // no arg
204
205 kFlags_PaintOp, // arg inline
206 kColor_PaintOp, // arg 32
207 kStyle_PaintOp, // arg inline
208 kJoin_PaintOp, // arg inline
209 kCap_PaintOp, // arg inline
210 kWidth_PaintOp, // arg scalar
211 kMiter_PaintOp,// arg scalar
212
213 kEncoding_PaintOp, // arg inline - text
214 kHinting_PaintOp, // arg inline - text
215 kAlign_PaintOp, // arg inline - text
216 kTextSize_PaintOp, // arg scalar - text
217 kTextScaleX_PaintOp,// arg scalar - text
218 kTextSkewX_PaintOp, // arg scalar - text
reed@google.comf5842f72011-05-04 18:30:04 +0000219 kTypeface_PaintOp, // arg inline (index) - text
220
reed@google.comb55d1182011-05-11 00:42:04 +0000221 kFlatIndex_PaintOp, // flags=paintflat, data=index
reed@google.combb6992a2011-04-26 17:41:56 +0000222};
223
224#define PAINTOPS_OP_BITS 8
225#define PAINTOPS_FLAG_BITS 4
226#define PAINTOPS_DATA_BITS 20
227
228#define PAINTOPS_OP_MASK ((1 << PAINTOPS_OP_BITS) - 1)
229#define PAINTOPS_FLAG_MASK ((1 << PAINTOPS_FLAG_BITS) - 1)
230#define PAINTOPS_DATA_MASK ((1 << PAINTOPS_DATA_BITS) - 1)
231
caryclark@google.com920901d2012-06-06 12:04:11 +0000232static inline unsigned PaintOp_unpackOp(uint32_t op32) {
reed@google.combb6992a2011-04-26 17:41:56 +0000233 return (op32 >> (PAINTOPS_FLAG_BITS + PAINTOPS_DATA_BITS));
234}
235
caryclark@google.com920901d2012-06-06 12:04:11 +0000236static inline unsigned PaintOp_unpackFlags(uint32_t op32) {
reed@google.combb6992a2011-04-26 17:41:56 +0000237 return (op32 >> PAINTOPS_DATA_BITS) & PAINTOPS_FLAG_MASK;
238}
239
caryclark@google.com920901d2012-06-06 12:04:11 +0000240static inline unsigned PaintOp_unpackData(uint32_t op32) {
reed@google.combb6992a2011-04-26 17:41:56 +0000241 return op32 & PAINTOPS_DATA_MASK;
242}
243
caryclark@google.com920901d2012-06-06 12:04:11 +0000244static inline uint32_t PaintOp_packOp(PaintOps op) {
reed@google.combb6992a2011-04-26 17:41:56 +0000245 SkASSERT(0 == (op & ~PAINTOPS_OP_MASK));
246
reed@google.com67908f22011-06-27 14:47:50 +0000247 return op << (PAINTOPS_FLAG_BITS + PAINTOPS_DATA_BITS);
reed@google.combb6992a2011-04-26 17:41:56 +0000248}
249
caryclark@google.com920901d2012-06-06 12:04:11 +0000250static inline uint32_t PaintOp_packOpData(PaintOps op, unsigned data) {
reed@google.combb6992a2011-04-26 17:41:56 +0000251 SkASSERT(0 == (op & ~PAINTOPS_OP_MASK));
252 SkASSERT(0 == (data & ~PAINTOPS_DATA_MASK));
253
reed@google.com67908f22011-06-27 14:47:50 +0000254 return (op << (PAINTOPS_FLAG_BITS + PAINTOPS_DATA_BITS)) | data;
reed@google.combb6992a2011-04-26 17:41:56 +0000255}
256
caryclark@google.com920901d2012-06-06 12:04:11 +0000257static inline uint32_t PaintOp_packOpFlagData(PaintOps op, unsigned flags, unsigned data) {
reed@google.combb6992a2011-04-26 17:41:56 +0000258 SkASSERT(0 == (op & ~PAINTOPS_OP_MASK));
259 SkASSERT(0 == (flags & ~PAINTOPS_FLAG_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)) |
reed@google.combb6992a2011-04-26 17:41:56 +0000263 (flags << PAINTOPS_DATA_BITS) |
264 data;
265}
266
reed@google.combb6992a2011-04-26 17:41:56 +0000267#endif