SkGPipePriv.h revision f1ab723033a186dc53434104a636c2dfac5fc863
1
2/*
3 * 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.
7 */
8
9
10
11#ifndef SkGPipePriv_DEFINED
12#define SkGPipePriv_DEFINED
13
14#include "SkTypes.h"
15
16#define UNIMPLEMENTED
17
18// these must be contiguous, 0...N-1
19enum PaintFlats {
20    kColorFilter_PaintFlat,
21    kDrawLooper_PaintFlat,
22    kMaskFilter_PaintFlat,
23    kPathEffect_PaintFlat,
24    kRasterizer_PaintFlat,
25    kShader_PaintFlat,
26    kImageFilter_PaintFlat,
27    kXfermode_PaintFlat,
28
29    kLast_PaintFlat = kXfermode_PaintFlat
30};
31#define kCount_PaintFlats   (kLast_PaintFlat + 1)
32
33enum DrawOps {
34    kSkip_DrawOp,   // skip an addition N bytes (N == data)
35
36    // these match Canvas apis
37    kClipPath_DrawOp,
38    kClipRegion_DrawOp,
39    kClipRect_DrawOp,
40    kConcat_DrawOp,
41    kDrawBitmap_DrawOp,
42    kDrawBitmapMatrix_DrawOp,
43    kDrawBitmapNine_DrawOp,
44    kDrawBitmapRectToRect_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,
54    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
67    kPaintOp_DrawOp,
68    kSetTypeface_DrawOp,
69
70    kDef_Typeface_DrawOp,
71    kDef_Flattenable_DrawOp,
72    kDef_Bitmap_DrawOp,
73    kDef_Factory_DrawOp,
74
75    // these are signals to playback, not drawing verbs
76    kReportFlags_DrawOp,
77    kShareBitmapHeap_DrawOp,
78    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
102static inline unsigned DrawOp_unpackOp(uint32_t op32) {
103    return (op32 >> (DRAWOPS_FLAG_BITS + DRAWOPS_DATA_BITS));
104}
105
106static inline unsigned DrawOp_unpackFlags(uint32_t op32) {
107    return (op32 >> DRAWOPS_DATA_BITS) & DRAWOPS_FLAG_MASK;
108}
109
110static inline unsigned DrawOp_unpackData(uint32_t op32) {
111    return op32 & DRAWOPS_DATA_MASK;
112}
113
114static inline uint32_t DrawOp_packOpFlagData(DrawOps op, unsigned flags, unsigned data) {
115    SkASSERT(0 == (op & ~DRAWOPS_OP_MASK));
116    SkASSERT(0 == (flags & ~DRAWOPS_FLAG_MASK));
117    SkASSERT(0 == (data & ~DRAWOPS_DATA_MASK));
118
119    return (op << (DRAWOPS_FLAG_BITS + DRAWOPS_DATA_BITS)) |
120           (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};
142enum {
143    kDrawBitmap_HasPaint_DrawOpFlag   = 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_DrawOpFlag = 1 << 1,
147};
148enum {
149    kClip_HasAntiAlias_DrawOpFlag = 1 << 0,
150};
151///////////////////////////////////////////////////////////////////////////////
152
153class BitmapInfo : SkNoncopyable {
154public:
155    BitmapInfo(SkBitmap* bitmap, uint32_t genID, int toBeDrawnCount)
156        : fBitmap(bitmap)
157        , fGenID(genID)
158        , fBytesAllocated(0)
159        , fMoreRecentlyUsed(NULL)
160        , fLessRecentlyUsed(NULL)
161        , fToBeDrawnCount(toBeDrawnCount)
162    {}
163
164    ~BitmapInfo() {
165        SkASSERT(0 == fToBeDrawnCount);
166        SkDELETE(fBitmap);
167    }
168
169    void addDraws(int drawsToAdd) {
170        if (0 == fToBeDrawnCount) {
171            // The readers will only ever decrement the count, so once the
172            // count is zero, the writer will be the only one modifying it,
173            // so it does not need to be an atomic operation.
174            fToBeDrawnCount = drawsToAdd;
175        } else {
176            sk_atomic_add(&fToBeDrawnCount, drawsToAdd);
177        }
178    }
179
180    void decDraws() {
181        sk_atomic_dec(&fToBeDrawnCount);
182    }
183
184    int drawCount() const {
185        return fToBeDrawnCount;
186    }
187
188    SkBitmap* fBitmap;
189    // Store the generation ID of the original bitmap, since copying does
190    // not copy this field, so fBitmap's generation ID will not be useful
191    // for comparing.
192    // FIXME: Is it reasonable to make copying a bitmap/pixelref copy the
193    // generation ID?
194    uint32_t fGenID;
195    // Keep track of the bytes allocated for this bitmap. When replacing the
196    // bitmap or removing this BitmapInfo we know how much memory has been
197    // reclaimed.
198    size_t fBytesAllocated;
199    // TODO: Generalize the LRU caching mechanism
200    BitmapInfo* fMoreRecentlyUsed;
201    BitmapInfo* fLessRecentlyUsed;
202private:
203    int      fToBeDrawnCount;
204};
205
206static inline bool shouldFlattenBitmaps(uint32_t flags) {
207    return SkToBool(flags & SkGPipeWriter::kCrossProcess_Flag
208            && !(flags & SkGPipeWriter::kSharedAddressSpace_Flag));
209}
210
211///////////////////////////////////////////////////////////////////////////////
212
213enum PaintOps {
214    kReset_PaintOp,     // no arg
215
216    kFlags_PaintOp,     // arg inline
217    kColor_PaintOp,     // arg 32
218    kStyle_PaintOp,     // arg inline
219    kJoin_PaintOp,      // arg inline
220    kCap_PaintOp,       // arg inline
221    kWidth_PaintOp,     // arg scalar
222    kMiter_PaintOp,// arg scalar
223
224    kEncoding_PaintOp,  // arg inline - text
225    kHinting_PaintOp,   // arg inline - text
226    kAlign_PaintOp,     // arg inline - text
227    kTextSize_PaintOp,  // arg scalar - text
228    kTextScaleX_PaintOp,// arg scalar - text
229    kTextSkewX_PaintOp, // arg scalar - text
230    kTypeface_PaintOp,  // arg inline (index) - text
231
232    kFlatIndex_PaintOp, // flags=paintflat, data=index
233};
234
235#define PAINTOPS_OP_BITS     8
236#define PAINTOPS_FLAG_BITS   4
237#define PAINTOPS_DATA_BITS   20
238
239#define PAINTOPS_OP_MASK     ((1 << PAINTOPS_OP_BITS) - 1)
240#define PAINTOPS_FLAG_MASK   ((1 << PAINTOPS_FLAG_BITS) - 1)
241#define PAINTOPS_DATA_MASK   ((1 << PAINTOPS_DATA_BITS) - 1)
242
243static inline unsigned PaintOp_unpackOp(uint32_t op32) {
244    return (op32 >> (PAINTOPS_FLAG_BITS + PAINTOPS_DATA_BITS));
245}
246
247static inline unsigned PaintOp_unpackFlags(uint32_t op32) {
248    return (op32 >> PAINTOPS_DATA_BITS) & PAINTOPS_FLAG_MASK;
249}
250
251static inline unsigned PaintOp_unpackData(uint32_t op32) {
252    return op32 & PAINTOPS_DATA_MASK;
253}
254
255static inline uint32_t PaintOp_packOp(PaintOps op) {
256    SkASSERT(0 == (op & ~PAINTOPS_OP_MASK));
257
258    return op << (PAINTOPS_FLAG_BITS + PAINTOPS_DATA_BITS);
259}
260
261static inline uint32_t PaintOp_packOpData(PaintOps op, unsigned data) {
262    SkASSERT(0 == (op & ~PAINTOPS_OP_MASK));
263    SkASSERT(0 == (data & ~PAINTOPS_DATA_MASK));
264
265    return (op << (PAINTOPS_FLAG_BITS + PAINTOPS_DATA_BITS)) | data;
266}
267
268static inline uint32_t PaintOp_packOpFlagData(PaintOps op, unsigned flags, unsigned data) {
269    SkASSERT(0 == (op & ~PAINTOPS_OP_MASK));
270    SkASSERT(0 == (flags & ~PAINTOPS_FLAG_MASK));
271    SkASSERT(0 == (data & ~PAINTOPS_DATA_MASK));
272
273    return (op << (PAINTOPS_FLAG_BITS + PAINTOPS_DATA_BITS)) |
274    (flags << PAINTOPS_DATA_BITS) |
275    data;
276}
277
278#endif
279