blob: e48baf38122e92b0224720a108cd5486a0fd4781 [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
scroggo@google.com10dccde2012-08-08 20:43:22 +000010#include "SkBitmapHeap.h"
reed@google.combb6992a2011-04-26 17:41:56 +000011#include "SkCanvas.h"
12#include "SkPaint.h"
reed@google.comacd471f2011-05-03 21:26:46 +000013#include "SkGPipe.h"
reed@google.combb6992a2011-04-26 17:41:56 +000014#include "SkGPipePriv.h"
15#include "SkReader32.h"
reed@google.comf5842f72011-05-04 18:30:04 +000016#include "SkStream.h"
reed@google.combb6793b2011-05-05 15:18:15 +000017
reed@google.com0cd2ac62013-10-14 20:02:44 +000018#include "SkAnnotation.h"
reed@google.combb6793b2011-05-05 15:18:15 +000019#include "SkColorFilter.h"
20#include "SkDrawLooper.h"
reed@google.com35348222013-10-16 13:05:06 +000021#include "SkImageFilter.h"
reed@google.combb6793b2011-05-05 15:18:15 +000022#include "SkMaskFilter.h"
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000023#include "SkReadBuffer.h"
dandovb3c9d1c2014-08-12 08:34:29 -070024#include "SkPatchUtils.h"
reed@google.combb6793b2011-05-05 15:18:15 +000025#include "SkPathEffect.h"
26#include "SkRasterizer.h"
reed@google.com4ed0fb72012-12-12 20:48:18 +000027#include "SkRRect.h"
reed@google.combb6793b2011-05-05 15:18:15 +000028#include "SkShader.h"
reed@google.comf5842f72011-05-04 18:30:04 +000029#include "SkTypeface.h"
reed@google.combb6793b2011-05-05 15:18:15 +000030#include "SkXfermode.h"
31
commit-bot@chromium.orgc0b7e102013-10-23 17:06:21 +000032static SkFlattenable::Type paintflat_to_flattype(PaintFlats pf) {
reed@google.com35348222013-10-16 13:05:06 +000033 static const uint8_t gEffectTypesInPaintFlatsOrder[] = {
commit-bot@chromium.orgc0b7e102013-10-23 17:06:21 +000034 SkFlattenable::kSkColorFilter_Type,
35 SkFlattenable::kSkDrawLooper_Type,
36 SkFlattenable::kSkImageFilter_Type,
37 SkFlattenable::kSkMaskFilter_Type,
38 SkFlattenable::kSkPathEffect_Type,
39 SkFlattenable::kSkRasterizer_Type,
40 SkFlattenable::kSkShader_Type,
41 SkFlattenable::kSkXfermode_Type,
reed@google.com35348222013-10-16 13:05:06 +000042 };
skia.committer@gmail.com4677acc2013-10-17 07:02:33 +000043
reed@google.com35348222013-10-16 13:05:06 +000044 SkASSERT((size_t)pf < SK_ARRAY_COUNT(gEffectTypesInPaintFlatsOrder));
commit-bot@chromium.orgc0b7e102013-10-23 17:06:21 +000045 return (SkFlattenable::Type)gEffectTypesInPaintFlatsOrder[pf];
reed@google.com35348222013-10-16 13:05:06 +000046}
47
reed@google.comb55d1182011-05-11 00:42:04 +000048static void set_paintflat(SkPaint* paint, SkFlattenable* obj, unsigned paintFlat) {
49 SkASSERT(paintFlat < kCount_PaintFlats);
50 switch (paintFlat) {
51 case kColorFilter_PaintFlat:
52 paint->setColorFilter((SkColorFilter*)obj);
53 break;
reed@google.com0faac1e2011-05-11 05:58:58 +000054 case kDrawLooper_PaintFlat:
55 paint->setLooper((SkDrawLooper*)obj);
56 break;
reed@google.comb55d1182011-05-11 00:42:04 +000057 case kMaskFilter_PaintFlat:
58 paint->setMaskFilter((SkMaskFilter*)obj);
59 break;
60 case kPathEffect_PaintFlat:
61 paint->setPathEffect((SkPathEffect*)obj);
62 break;
63 case kRasterizer_PaintFlat:
64 paint->setRasterizer((SkRasterizer*)obj);
65 break;
66 case kShader_PaintFlat:
67 paint->setShader((SkShader*)obj);
68 break;
scroggo@google.com16d1d0b2012-05-02 19:09:40 +000069 case kImageFilter_PaintFlat:
70 paint->setImageFilter((SkImageFilter*)obj);
71 break;
reed@google.comb55d1182011-05-11 00:42:04 +000072 case kXfermode_PaintFlat:
73 paint->setXfermode((SkXfermode*)obj);
74 break;
75 default:
tomhudson@google.com0c00f212011-12-28 14:59:50 +000076 SkDEBUGFAIL("never gets here");
reed@google.comb55d1182011-05-11 00:42:04 +000077 }
78}
79
reed@google.combb6793b2011-05-05 15:18:15 +000080template <typename T> class SkRefCntTDArray : public SkTDArray<T> {
81public:
82 ~SkRefCntTDArray() { this->unrefAll(); }
83};
reed@google.combb6992a2011-04-26 17:41:56 +000084
scroggo@google.com7ca24432012-08-14 15:48:43 +000085class SkGPipeState : public SkBitmapHeapReader {
reed@google.combb6992a2011-04-26 17:41:56 +000086public:
87 SkGPipeState();
88 ~SkGPipeState();
reed@google.combb6793b2011-05-05 15:18:15 +000089
junov@chromium.orgfb103892012-09-20 19:35:43 +000090 void setSilent(bool silent) {
91 fSilent = silent;
92 }
93
94 bool shouldDraw() {
95 return !fSilent;
96 }
97
scroggo@google.com565254b2012-06-28 15:41:32 +000098 void setFlags(unsigned flags) {
99 if (fFlags != flags) {
100 fFlags = flags;
101 this->updateReader();
102 }
103 }
104
105 unsigned getFlags() const {
106 return fFlags;
107 }
108
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000109 void setReader(SkReadBuffer* reader) {
reed@google.comdde09562011-05-23 12:21:05 +0000110 fReader = reader;
scroggo@google.com565254b2012-06-28 15:41:32 +0000111 this->updateReader();
reed@google.comdde09562011-05-23 12:21:05 +0000112 }
reed@google.combb6793b2011-05-05 15:18:15 +0000113
reed@google.com31891582011-05-12 03:03:56 +0000114 const SkPaint& paint() const { return fPaint; }
115 SkPaint* editPaint() { return &fPaint; }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000116
reed@google.comb55d1182011-05-11 00:42:04 +0000117 SkFlattenable* getFlat(unsigned index) const {
118 if (0 == index) {
119 return NULL;
120 }
121 return fFlatArray[index - 1];
122 }
123
reed@google.com67908f22011-06-27 14:47:50 +0000124 void defFlattenable(PaintFlats pf, int index) {
scroggo@google.comd3ba5cc2012-07-09 16:05:53 +0000125 index--;
commit-bot@chromium.orgc0b7e102013-10-23 17:06:21 +0000126 SkFlattenable* obj = fReader->readFlattenable(paintflat_to_flattype(pf));
scroggo@google.comd3ba5cc2012-07-09 16:05:53 +0000127 if (fFlatArray.count() == index) {
128 *fFlatArray.append() = obj;
129 } else {
130 SkSafeUnref(fFlatArray[index]);
131 fFlatArray[index] = obj;
132 }
reed@google.comb55d1182011-05-11 00:42:04 +0000133 }
134
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000135 void defFactory(const char* name) {
136 SkFlattenable::Factory factory = SkFlattenable::NameToFactory(name);
137 if (factory) {
138 SkASSERT(fFactoryArray.find(factory) < 0);
139 *fFactoryArray.append() = factory;
140 }
141 }
142
scroggo@google.com74b7ffd2013-04-30 02:32:41 +0000143 /**
144 * Add a bitmap to the array of bitmaps, or replace an existing one.
145 * This is only used when in cross process mode without a shared heap.
146 */
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000147 void addBitmap(int index) {
scroggo@google.com74b7ffd2013-04-30 02:32:41 +0000148 SkASSERT(shouldFlattenBitmaps(fFlags));
scroggo@google.com4dffc592012-07-17 16:49:40 +0000149 SkBitmap* bm;
150 if(fBitmaps.count() == index) {
151 bm = SkNEW(SkBitmap);
152 *fBitmaps.append() = bm;
153 } else {
154 bm = fBitmaps[index];
155 }
scroggo@google.com74b7ffd2013-04-30 02:32:41 +0000156 fReader->readBitmap(bm);
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000157 }
158
scroggo@google.com7ca24432012-08-14 15:48:43 +0000159 /**
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000160 * Override of SkBitmapHeapReader, so that SkReadBuffer can use
scroggo@google.com74b7ffd2013-04-30 02:32:41 +0000161 * these SkBitmaps for bitmap shaders. Used only in cross process mode
162 * without a shared heap.
scroggo@google.com7ca24432012-08-14 15:48:43 +0000163 */
164 virtual SkBitmap* getBitmap(int32_t index) const SK_OVERRIDE {
scroggo@google.com74b7ffd2013-04-30 02:32:41 +0000165 SkASSERT(shouldFlattenBitmaps(fFlags));
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000166 return fBitmaps[index];
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000167 }
168
scroggo@google.com7ca24432012-08-14 15:48:43 +0000169 /**
170 * Needed to be a non-abstract subclass of SkBitmapHeapReader.
171 */
172 virtual void releaseRef(int32_t) SK_OVERRIDE {}
173
scroggo@google.com10dccde2012-08-08 20:43:22 +0000174 void setSharedHeap(SkBitmapHeap* heap) {
175 SkASSERT(!shouldFlattenBitmaps(fFlags) || NULL == heap);
176 SkRefCnt_SafeAssign(fSharedHeap, heap);
scroggo@google.com7ca24432012-08-14 15:48:43 +0000177 this->updateReader();
scroggo@google.com10dccde2012-08-08 20:43:22 +0000178 }
179
scroggo@google.com74b7ffd2013-04-30 02:32:41 +0000180 /**
181 * Access the shared heap. Only used in the case when bitmaps are not
182 * flattened.
183 */
scroggo@google.com10dccde2012-08-08 20:43:22 +0000184 SkBitmapHeap* getSharedHeap() const {
scroggo@google.com74b7ffd2013-04-30 02:32:41 +0000185 SkASSERT(!shouldFlattenBitmaps(fFlags));
scroggo@google.com10dccde2012-08-08 20:43:22 +0000186 return fSharedHeap;
187 }
188
reed@google.combb6793b2011-05-05 15:18:15 +0000189 void addTypeface() {
djsollen@google.comc73dd5c2012-08-07 15:54:32 +0000190 size_t size = fReader->read32();
reed@google.combb6793b2011-05-05 15:18:15 +0000191 const void* data = fReader->skip(SkAlign4(size));
reed@google.comf5842f72011-05-04 18:30:04 +0000192 SkMemoryStream stream(data, size, false);
reed@google.comf5842f72011-05-04 18:30:04 +0000193 *fTypefaces.append() = SkTypeface::Deserialize(&stream);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000194 }
scroggo@google.com10dccde2012-08-08 20:43:22 +0000195
reed@google.combb6793b2011-05-05 15:18:15 +0000196 void setTypeface(SkPaint* paint, unsigned id) {
197 paint->setTypeface(id ? fTypefaces[id - 1] : NULL);
198 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000199
reed@google.comb55d1182011-05-11 00:42:04 +0000200private:
scroggo@google.com565254b2012-06-28 15:41:32 +0000201 void updateReader() {
202 if (NULL == fReader) {
203 return;
204 }
205 bool crossProcess = SkToBool(fFlags & SkGPipeWriter::kCrossProcess_Flag);
206 fReader->setFlags(SkSetClearMask(fReader->getFlags(), crossProcess,
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000207 SkReadBuffer::kCrossProcess_Flag));
scroggo@google.com565254b2012-06-28 15:41:32 +0000208 if (crossProcess) {
209 fReader->setFactoryArray(&fFactoryArray);
210 } else {
211 fReader->setFactoryArray(NULL);
212 }
scroggo@google.com7ca24432012-08-14 15:48:43 +0000213
214 if (shouldFlattenBitmaps(fFlags)) {
215 fReader->setBitmapStorage(this);
216 } else {
217 fReader->setBitmapStorage(fSharedHeap);
218 }
scroggo@google.com565254b2012-06-28 15:41:32 +0000219 }
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000220 SkReadBuffer* fReader;
reed@google.com31891582011-05-12 03:03:56 +0000221 SkPaint fPaint;
222 SkTDArray<SkFlattenable*> fFlatArray;
223 SkTDArray<SkTypeface*> fTypefaces;
reed@google.comdde09562011-05-23 12:21:05 +0000224 SkTDArray<SkFlattenable::Factory> fFactoryArray;
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000225 SkTDArray<SkBitmap*> fBitmaps;
junov@chromium.orgfb103892012-09-20 19:35:43 +0000226 bool fSilent;
scroggo@google.com10dccde2012-08-08 20:43:22 +0000227 // Only used when sharing bitmaps with the writer.
228 SkBitmapHeap* fSharedHeap;
scroggo@google.com565254b2012-06-28 15:41:32 +0000229 unsigned fFlags;
reed@google.combb6992a2011-04-26 17:41:56 +0000230};
231
232///////////////////////////////////////////////////////////////////////////////
233
commit-bot@chromium.orgb45bd1f2014-04-24 18:17:30 +0000234template <typename T> const T* skip(SkReader32* reader, size_t count = 1) {
reed@google.combb6992a2011-04-26 17:41:56 +0000235 size_t size = sizeof(T) * count;
236 SkASSERT(SkAlign4(size) == size);
237 return reinterpret_cast<const T*>(reader->skip(size));
238}
239
commit-bot@chromium.orgb45bd1f2014-04-24 18:17:30 +0000240template <typename T> const T* skipAlign(SkReader32* reader, size_t count = 1) {
reed@google.combb6992a2011-04-26 17:41:56 +0000241 size_t size = SkAlign4(sizeof(T) * count);
242 return reinterpret_cast<const T*>(reader->skip(size));
243}
244
reed@google.combb6992a2011-04-26 17:41:56 +0000245///////////////////////////////////////////////////////////////////////////////
246///////////////////////////////////////////////////////////////////////////////
247
248static void clipPath_rp(SkCanvas* canvas, SkReader32* reader, uint32_t op32,
249 SkGPipeState* state) {
250 SkPath path;
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000251 reader->readPath(&path);
scroggo@google.com460a23e2012-08-16 17:56:49 +0000252 bool doAA = SkToBool(DrawOp_unpackFlags(op32) & kClip_HasAntiAlias_DrawOpFlag);
253 canvas->clipPath(path, (SkRegion::Op)DrawOp_unpackData(op32), doAA);
reed@google.combb6992a2011-04-26 17:41:56 +0000254}
255
256static void clipRegion_rp(SkCanvas* canvas, SkReader32* reader, uint32_t op32,
257 SkGPipeState* state) {
258 SkRegion rgn;
djsollen@google.com2b2ede32012-04-12 13:24:04 +0000259 reader->readRegion(&rgn);
reed@google.combb6992a2011-04-26 17:41:56 +0000260 canvas->clipRegion(rgn, (SkRegion::Op)DrawOp_unpackData(op32));
261}
262
263static void clipRect_rp(SkCanvas* canvas, SkReader32* reader, uint32_t op32,
264 SkGPipeState* state) {
scroggo@google.com691703c2012-06-05 14:58:40 +0000265 const SkRect* rect = skip<SkRect>(reader);
scroggo@google.com460a23e2012-08-16 17:56:49 +0000266 bool doAA = SkToBool(DrawOp_unpackFlags(op32) & kClip_HasAntiAlias_DrawOpFlag);
scroggo@google.com691703c2012-06-05 14:58:40 +0000267 canvas->clipRect(*rect, (SkRegion::Op)DrawOp_unpackData(op32), doAA);
reed@google.combb6992a2011-04-26 17:41:56 +0000268}
269
reed@google.com4ed0fb72012-12-12 20:48:18 +0000270static void clipRRect_rp(SkCanvas* canvas, SkReader32* reader, uint32_t op32,
271 SkGPipeState* state) {
272 SkRRect rrect;
273 reader->readRRect(&rrect);
274 bool doAA = SkToBool(DrawOp_unpackFlags(op32) & kClip_HasAntiAlias_DrawOpFlag);
275 canvas->clipRRect(rrect, (SkRegion::Op)DrawOp_unpackData(op32), doAA);
276}
277
reed@google.combb6992a2011-04-26 17:41:56 +0000278///////////////////////////////////////////////////////////////////////////////
279
280static void setMatrix_rp(SkCanvas* canvas, SkReader32* reader, uint32_t op32,
281 SkGPipeState* state) {
282 SkMatrix matrix;
djsollen@google.com2b2ede32012-04-12 13:24:04 +0000283 reader->readMatrix(&matrix);
reed@google.combb6992a2011-04-26 17:41:56 +0000284 canvas->setMatrix(matrix);
285}
286
287static void concat_rp(SkCanvas* canvas, SkReader32* reader, uint32_t op32,
288 SkGPipeState* state) {
289 SkMatrix matrix;
djsollen@google.com2b2ede32012-04-12 13:24:04 +0000290 reader->readMatrix(&matrix);
reed@google.combb6992a2011-04-26 17:41:56 +0000291 canvas->concat(matrix);
292}
293
294static void scale_rp(SkCanvas* canvas, SkReader32* reader, uint32_t op32,
295 SkGPipeState* state) {
296 const SkScalar* param = skip<SkScalar>(reader, 2);
297 canvas->scale(param[0], param[1]);
298}
299
300static void skew_rp(SkCanvas* canvas, SkReader32* reader, uint32_t op32,
301 SkGPipeState* state) {
302 const SkScalar* param = skip<SkScalar>(reader, 2);
303 canvas->skew(param[0], param[1]);
304}
305
306static void rotate_rp(SkCanvas* canvas, SkReader32* reader, uint32_t op32,
307 SkGPipeState* state) {
308 canvas->rotate(reader->readScalar());
309}
310
311static void translate_rp(SkCanvas* canvas, SkReader32* reader, uint32_t op32,
312 SkGPipeState* state) {
313 const SkScalar* param = skip<SkScalar>(reader, 2);
314 canvas->translate(param[0], param[1]);
315}
316
317///////////////////////////////////////////////////////////////////////////////
318
319static void save_rp(SkCanvas* canvas, SkReader32* reader, uint32_t op32,
320 SkGPipeState* state) {
Florin Malita5f6102d2014-06-30 10:13:28 -0400321 canvas->save();
reed@google.combb6992a2011-04-26 17:41:56 +0000322}
323
324static void saveLayer_rp(SkCanvas* canvas, SkReader32* reader, uint32_t op32,
325 SkGPipeState* state) {
reed@google.comacd471f2011-05-03 21:26:46 +0000326 unsigned flags = DrawOp_unpackFlags(op32);
327 SkCanvas::SaveFlags saveFlags = (SkCanvas::SaveFlags)DrawOp_unpackData(op32);
reed@google.combb6992a2011-04-26 17:41:56 +0000328
329 const SkRect* bounds = NULL;
330 if (flags & kSaveLayer_HasBounds_DrawOpFlag) {
331 bounds = skip<SkRect>(reader);
332 }
333 const SkPaint* paint = NULL;
334 if (flags & kSaveLayer_HasPaint_DrawOpFlag) {
reed@google.com31891582011-05-12 03:03:56 +0000335 paint = &state->paint();
reed@google.combb6992a2011-04-26 17:41:56 +0000336 }
reed@google.comacd471f2011-05-03 21:26:46 +0000337 canvas->saveLayer(bounds, paint, saveFlags);
reed@google.combb6992a2011-04-26 17:41:56 +0000338}
339
340static void restore_rp(SkCanvas* canvas, SkReader32* reader, uint32_t op32,
341 SkGPipeState* state) {
342 canvas->restore();
343}
344
345///////////////////////////////////////////////////////////////////////////////
346
347static void drawClear_rp(SkCanvas* canvas, SkReader32* reader, uint32_t op32,
348 SkGPipeState* state) {
349 SkColor color = 0;
350 if (DrawOp_unpackFlags(op32) & kClear_HasColor_DrawOpFlag) {
351 color = reader->readU32();
352 }
353 canvas->clear(color);
354}
355
356static void drawPaint_rp(SkCanvas* canvas, SkReader32* reader, uint32_t op32,
357 SkGPipeState* state) {
junov@chromium.orgfb103892012-09-20 19:35:43 +0000358 if (state->shouldDraw()) {
359 canvas->drawPaint(state->paint());
360 }
reed@google.combb6992a2011-04-26 17:41:56 +0000361}
362
363static void drawPoints_rp(SkCanvas* canvas, SkReader32* reader, uint32_t op32,
364 SkGPipeState* state) {
365 SkCanvas::PointMode mode = (SkCanvas::PointMode)DrawOp_unpackFlags(op32);
366 size_t count = reader->readU32();
367 const SkPoint* pts = skip<SkPoint>(reader, count);
junov@chromium.orgfb103892012-09-20 19:35:43 +0000368 if (state->shouldDraw()) {
369 canvas->drawPoints(mode, count, pts, state->paint());
370 }
reed@google.combb6992a2011-04-26 17:41:56 +0000371}
372
reed@google.com4ed0fb72012-12-12 20:48:18 +0000373static void drawOval_rp(SkCanvas* canvas, SkReader32* reader, uint32_t op32,
374 SkGPipeState* state) {
375 const SkRect* rect = skip<SkRect>(reader);
376 if (state->shouldDraw()) {
377 canvas->drawOval(*rect, state->paint());
378 }
379}
380
reed@google.combb6992a2011-04-26 17:41:56 +0000381static void drawRect_rp(SkCanvas* canvas, SkReader32* reader, uint32_t op32,
382 SkGPipeState* state) {
junov@chromium.orgfb103892012-09-20 19:35:43 +0000383 const SkRect* rect = skip<SkRect>(reader);
384 if (state->shouldDraw()) {
385 canvas->drawRect(*rect, state->paint());
386 }
reed@google.combb6992a2011-04-26 17:41:56 +0000387}
388
reed@google.com4ed0fb72012-12-12 20:48:18 +0000389static void drawRRect_rp(SkCanvas* canvas, SkReader32* reader, uint32_t op32,
390 SkGPipeState* state) {
391 SkRRect rrect;
392 reader->readRRect(&rrect);
393 if (state->shouldDraw()) {
394 canvas->drawRRect(rrect, state->paint());
395 }
396}
397
commit-bot@chromium.orged9806f2014-02-21 02:32:36 +0000398static void drawDRRect_rp(SkCanvas* canvas, SkReader32* reader, uint32_t op32,
399 SkGPipeState* state) {
400 SkRRect outer, inner;
401 reader->readRRect(&outer);
402 reader->readRRect(&inner);
403 if (state->shouldDraw()) {
404 canvas->drawDRRect(outer, inner, state->paint());
405 }
406}
407
dandov963137b2014-08-07 07:49:53 -0700408static void drawPatch_rp(SkCanvas* canvas, SkReader32* reader, uint32_t op32,
409 SkGPipeState* state) {
dandovb3c9d1c2014-08-12 08:34:29 -0700410
411 unsigned flags = DrawOp_unpackFlags(op32);
412
413 const SkPoint* cubics = skip<SkPoint>(reader, SkPatchUtils::kNumCtrlPts);
414
415 const SkColor* colors = NULL;
416 if (flags & kDrawVertices_HasColors_DrawOpFlag) {
417 colors = skip<SkColor>(reader, SkPatchUtils::kNumCorners);
418 }
419 const SkPoint* texCoords = NULL;
420 if (flags & kDrawVertices_HasTexs_DrawOpFlag) {
421 texCoords = skip<SkPoint>(reader, SkPatchUtils::kNumCorners);
422 }
423 SkAutoTUnref<SkXfermode> xfer;
424 if (flags & kDrawVertices_HasXfermode_DrawOpFlag) {
425 int mode = reader->readInt();
426 if (mode < 0 || mode > SkXfermode::kLastMode) {
427 mode = SkXfermode::kModulate_Mode;
428 }
429 xfer.reset(SkXfermode::Create((SkXfermode::Mode)mode));
430 }
dandov963137b2014-08-07 07:49:53 -0700431 if (state->shouldDraw()) {
dandovb3c9d1c2014-08-12 08:34:29 -0700432 canvas->drawPatch(cubics, colors, texCoords, xfer, state->paint());
dandov963137b2014-08-07 07:49:53 -0700433 }
434}
435
reed@google.combb6992a2011-04-26 17:41:56 +0000436static void drawPath_rp(SkCanvas* canvas, SkReader32* reader, uint32_t op32,
437 SkGPipeState* state) {
438 SkPath path;
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000439 reader->readPath(&path);
junov@chromium.orgfb103892012-09-20 19:35:43 +0000440 if (state->shouldDraw()) {
441 canvas->drawPath(path, state->paint());
442 }
reed@google.combb6992a2011-04-26 17:41:56 +0000443}
444
445static void drawVertices_rp(SkCanvas* canvas, SkReader32* reader, uint32_t op32,
446 SkGPipeState* state) {
447 unsigned flags = DrawOp_unpackFlags(op32);
448
reed@google.com85e143c2013-12-30 15:51:25 +0000449 SkCanvas::VertexMode vmode = (SkCanvas::VertexMode)reader->readU32();
reed@google.combb6992a2011-04-26 17:41:56 +0000450 int vertexCount = reader->readU32();
451 const SkPoint* verts = skip<SkPoint>(reader, vertexCount);
452
453 const SkPoint* texs = NULL;
454 if (flags & kDrawVertices_HasTexs_DrawOpFlag) {
455 texs = skip<SkPoint>(reader, vertexCount);
456 }
457
458 const SkColor* colors = NULL;
459 if (flags & kDrawVertices_HasColors_DrawOpFlag) {
460 colors = skip<SkColor>(reader, vertexCount);
461 }
462
reed@google.com85e143c2013-12-30 15:51:25 +0000463 SkAutoTUnref<SkXfermode> xfer;
464 if (flags & kDrawVertices_HasXfermode_DrawOpFlag) {
465 SkXfermode::Mode mode = (SkXfermode::Mode)reader->readU32();
466 xfer.reset(SkXfermode::Create(mode));
467 }
reed@google.combb6992a2011-04-26 17:41:56 +0000468
469 int indexCount = 0;
470 const uint16_t* indices = NULL;
471 if (flags & kDrawVertices_HasIndices_DrawOpFlag) {
472 indexCount = reader->readU32();
473 indices = skipAlign<uint16_t>(reader, indexCount);
474 }
junov@chromium.orgfb103892012-09-20 19:35:43 +0000475 if (state->shouldDraw()) {
reed@google.com85e143c2013-12-30 15:51:25 +0000476 canvas->drawVertices(vmode, vertexCount, verts, texs, colors, xfer,
junov@chromium.orgfb103892012-09-20 19:35:43 +0000477 indices, indexCount, state->paint());
478 }
reed@google.combb6992a2011-04-26 17:41:56 +0000479}
480
481///////////////////////////////////////////////////////////////////////////////
482
483static void drawText_rp(SkCanvas* canvas, SkReader32* reader, uint32_t op32,
484 SkGPipeState* state) {
485 size_t len = reader->readU32();
486 const void* text = reader->skip(SkAlign4(len));
487 const SkScalar* xy = skip<SkScalar>(reader, 2);
junov@chromium.orgfb103892012-09-20 19:35:43 +0000488 if (state->shouldDraw()) {
489 canvas->drawText(text, len, xy[0], xy[1], state->paint());
490 }
reed@google.combb6992a2011-04-26 17:41:56 +0000491}
492
493static void drawPosText_rp(SkCanvas* canvas, SkReader32* reader, uint32_t op32,
494 SkGPipeState* state) {
495 size_t len = reader->readU32();
496 const void* text = reader->skip(SkAlign4(len));
497 size_t posCount = reader->readU32(); // compute by our writer
498 const SkPoint* pos = skip<SkPoint>(reader, posCount);
junov@chromium.orgfb103892012-09-20 19:35:43 +0000499 if (state->shouldDraw()) {
500 canvas->drawPosText(text, len, pos, state->paint());
501 }
reed@google.combb6992a2011-04-26 17:41:56 +0000502}
503
504static void drawPosTextH_rp(SkCanvas* canvas, SkReader32* reader, uint32_t op32,
505 SkGPipeState* state) {
506 size_t len = reader->readU32();
507 const void* text = reader->skip(SkAlign4(len));
508 size_t posCount = reader->readU32(); // compute by our writer
509 const SkScalar* xpos = skip<SkScalar>(reader, posCount);
510 SkScalar constY = reader->readScalar();
junov@chromium.orgfb103892012-09-20 19:35:43 +0000511 if (state->shouldDraw()) {
512 canvas->drawPosTextH(text, len, xpos, constY, state->paint());
513 }
reed@google.combb6992a2011-04-26 17:41:56 +0000514}
515
516static void drawTextOnPath_rp(SkCanvas* canvas, SkReader32* reader, uint32_t op32,
517 SkGPipeState* state) {
518 size_t len = reader->readU32();
519 const void* text = reader->skip(SkAlign4(len));
520
521 SkPath path;
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000522 reader->readPath(&path);
reed@google.combb6992a2011-04-26 17:41:56 +0000523
524 SkMatrix matrixStorage;
525 const SkMatrix* matrix = NULL;
526 if (DrawOp_unpackFlags(op32) & kDrawTextOnPath_HasMatrix_DrawOpFlag) {
djsollen@google.com2b2ede32012-04-12 13:24:04 +0000527 reader->readMatrix(&matrixStorage);
reed@google.combb6992a2011-04-26 17:41:56 +0000528 matrix = &matrixStorage;
529 }
junov@chromium.orgfb103892012-09-20 19:35:43 +0000530 if (state->shouldDraw()) {
531 canvas->drawTextOnPath(text, len, path, matrix, state->paint());
532 }
reed@google.combb6992a2011-04-26 17:41:56 +0000533}
534
535///////////////////////////////////////////////////////////////////////////////
536
scroggo@google.com284bf502012-07-17 16:10:34 +0000537class BitmapHolder : SkNoncopyable {
538public:
539 BitmapHolder(SkReader32* reader, uint32_t op32, SkGPipeState* state);
540 ~BitmapHolder() {
scroggo@google.com10dccde2012-08-08 20:43:22 +0000541 if (fHeapEntry != NULL) {
542 fHeapEntry->releaseRef();
scroggo@google.com284bf502012-07-17 16:10:34 +0000543 }
scroggo@google.com565254b2012-06-28 15:41:32 +0000544 }
scroggo@google.com284bf502012-07-17 16:10:34 +0000545 const SkBitmap* getBitmap() {
546 return fBitmap;
547 }
548private:
scroggo@google.com10dccde2012-08-08 20:43:22 +0000549 SkBitmapHeapEntry* fHeapEntry;
550 const SkBitmap* fBitmap;
scroggo@google.com58b4ead2012-08-31 16:15:22 +0000551 SkBitmap fBitmapStorage;
scroggo@google.com284bf502012-07-17 16:10:34 +0000552};
553
554BitmapHolder::BitmapHolder(SkReader32* reader, uint32_t op32,
555 SkGPipeState* state) {
scroggo@google.com58b4ead2012-08-31 16:15:22 +0000556 const unsigned flags = state->getFlags();
557 const unsigned index = DrawOp_unpackData(op32);
558 if (shouldFlattenBitmaps(flags)) {
scroggo@google.com10dccde2012-08-08 20:43:22 +0000559 fHeapEntry = NULL;
560 fBitmap = state->getBitmap(index);
scroggo@google.com284bf502012-07-17 16:10:34 +0000561 } else {
scroggo@google.com58b4ead2012-08-31 16:15:22 +0000562 SkBitmapHeapEntry* entry = state->getSharedHeap()->getEntry(index);
563 if (SkToBool(flags & SkGPipeWriter::kSimultaneousReaders_Flag)) {
564 // Make a shallow copy for thread safety. Each thread will point to the same SkPixelRef,
565 // which is thread safe.
566 fBitmapStorage = *entry->getBitmap();
567 fBitmap = &fBitmapStorage;
568 // Release the ref on the bitmap now, since we made our own copy.
569 entry->releaseRef();
570 fHeapEntry = NULL;
571 } else {
572 SkASSERT(!shouldFlattenBitmaps(flags));
573 SkASSERT(!SkToBool(flags & SkGPipeWriter::kSimultaneousReaders_Flag));
574 fHeapEntry = entry;
575 fBitmap = fHeapEntry->getBitmap();
576 }
scroggo@google.com284bf502012-07-17 16:10:34 +0000577 }
scroggo@google.com565254b2012-06-28 15:41:32 +0000578}
579
reed@google.combb6992a2011-04-26 17:41:56 +0000580static void drawBitmap_rp(SkCanvas* canvas, SkReader32* reader, uint32_t op32,
581 SkGPipeState* state) {
scroggo@google.com284bf502012-07-17 16:10:34 +0000582 BitmapHolder holder(reader, op32, state);
scroggo@google.com460a23e2012-08-16 17:56:49 +0000583 bool hasPaint = SkToBool(DrawOp_unpackFlags(op32) & kDrawBitmap_HasPaint_DrawOpFlag);
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000584 SkScalar left = reader->readScalar();
585 SkScalar top = reader->readScalar();
junov@chromium.orgfb103892012-09-20 19:35:43 +0000586 const SkBitmap* bitmap = holder.getBitmap();
587 if (state->shouldDraw()) {
588 canvas->drawBitmap(*bitmap, left, top, hasPaint ? &state->paint() : NULL);
589 }
reed@google.combb6992a2011-04-26 17:41:56 +0000590}
591
592static void drawBitmapMatrix_rp(SkCanvas* canvas, SkReader32* reader, uint32_t op32,
593 SkGPipeState* state) {
scroggo@google.com58be6822012-07-30 14:40:01 +0000594 BitmapHolder holder(reader, op32, state);
scroggo@google.com460a23e2012-08-16 17:56:49 +0000595 bool hasPaint = SkToBool(DrawOp_unpackFlags(op32) & kDrawBitmap_HasPaint_DrawOpFlag);
scroggo@google.com58be6822012-07-30 14:40:01 +0000596 SkMatrix matrix;
597 reader->readMatrix(&matrix);
junov@chromium.orgfb103892012-09-20 19:35:43 +0000598 const SkBitmap* bitmap = holder.getBitmap();
599 if (state->shouldDraw()) {
600 canvas->drawBitmapMatrix(*bitmap, matrix,
601 hasPaint ? &state->paint() : NULL);
602 }
reed@google.combb6992a2011-04-26 17:41:56 +0000603}
604
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000605static void drawBitmapNine_rp(SkCanvas* canvas, SkReader32* reader,
606 uint32_t op32, SkGPipeState* state) {
scroggo@google.com284bf502012-07-17 16:10:34 +0000607 BitmapHolder holder(reader, op32, state);
scroggo@google.com460a23e2012-08-16 17:56:49 +0000608 bool hasPaint = SkToBool(DrawOp_unpackFlags(op32) & kDrawBitmap_HasPaint_DrawOpFlag);
scroggo@google.com691703c2012-06-05 14:58:40 +0000609 const SkIRect* center = skip<SkIRect>(reader);
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000610 const SkRect* dst = skip<SkRect>(reader);
junov@chromium.orgfb103892012-09-20 19:35:43 +0000611 const SkBitmap* bitmap = holder.getBitmap();
612 if (state->shouldDraw()) {
613 canvas->drawBitmapNine(*bitmap, *center, *dst,
614 hasPaint ? &state->paint() : NULL);
615 }
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000616}
617
618static void drawBitmapRect_rp(SkCanvas* canvas, SkReader32* reader,
619 uint32_t op32, SkGPipeState* state) {
scroggo@google.com284bf502012-07-17 16:10:34 +0000620 BitmapHolder holder(reader, op32, state);
scroggo@google.com58be6822012-07-30 14:40:01 +0000621 unsigned flags = DrawOp_unpackFlags(op32);
scroggo@google.com460a23e2012-08-16 17:56:49 +0000622 bool hasPaint = SkToBool(flags & kDrawBitmap_HasPaint_DrawOpFlag);
623 bool hasSrc = SkToBool(flags & kDrawBitmap_HasSrcRect_DrawOpFlag);
reed@google.com71121732012-09-18 15:14:33 +0000624 const SkRect* src;
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000625 if (hasSrc) {
reed@google.com71121732012-09-18 15:14:33 +0000626 src = skip<SkRect>(reader);
scroggo@google.com691703c2012-06-05 14:58:40 +0000627 } else {
628 src = NULL;
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000629 }
robertphillips@google.com31acc112013-08-20 12:13:48 +0000630 SkCanvas::DrawBitmapRectFlags dbmrFlags = SkCanvas::kNone_DrawBitmapRectFlag;
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +0000631 if (flags & kDrawBitmap_Bleed_DrawOpFlag) {
632 dbmrFlags = (SkCanvas::DrawBitmapRectFlags)(dbmrFlags|SkCanvas::kBleed_DrawBitmapRectFlag);
633 }
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000634 const SkRect* dst = skip<SkRect>(reader);
junov@chromium.orgfb103892012-09-20 19:35:43 +0000635 const SkBitmap* bitmap = holder.getBitmap();
636 if (state->shouldDraw()) {
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000637 canvas->drawBitmapRectToRect(*bitmap, src, *dst,
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +0000638 hasPaint ? &state->paint() : NULL, dbmrFlags);
junov@chromium.orgfb103892012-09-20 19:35:43 +0000639 }
reed@google.combb6992a2011-04-26 17:41:56 +0000640}
641
642static void drawSprite_rp(SkCanvas* canvas, SkReader32* reader, uint32_t op32,
643 SkGPipeState* state) {
scroggo@google.com284bf502012-07-17 16:10:34 +0000644 BitmapHolder holder(reader, op32, state);
scroggo@google.com460a23e2012-08-16 17:56:49 +0000645 bool hasPaint = SkToBool(DrawOp_unpackFlags(op32) & kDrawBitmap_HasPaint_DrawOpFlag);
scroggo@google.com691703c2012-06-05 14:58:40 +0000646 const SkIPoint* point = skip<SkIPoint>(reader);
junov@chromium.orgfb103892012-09-20 19:35:43 +0000647 const SkBitmap* bitmap = holder.getBitmap();
648 if (state->shouldDraw()) {
649 canvas->drawSprite(*bitmap, point->fX, point->fY, hasPaint ? &state->paint() : NULL);
650 }
reed@google.combb6992a2011-04-26 17:41:56 +0000651}
652
653///////////////////////////////////////////////////////////////////////////////
654
655static void drawData_rp(SkCanvas* canvas, SkReader32* reader, uint32_t op32,
656 SkGPipeState* state) {
657 // since we don't have a paint, we can use data for our (small) sizes
658 size_t size = DrawOp_unpackData(op32);
659 if (0 == size) {
660 size = reader->readU32();
661 }
662 const void* data = reader->skip(SkAlign4(size));
junov@chromium.orgfb103892012-09-20 19:35:43 +0000663 if (state->shouldDraw()) {
664 canvas->drawData(data, size);
665 }
reed@google.combb6992a2011-04-26 17:41:56 +0000666}
667
reed@google.combb6992a2011-04-26 17:41:56 +0000668static void drawPicture_rp(SkCanvas* canvas, SkReader32* reader, uint32_t op32,
669 SkGPipeState* state) {
670 UNIMPLEMENTED
671}
672
fmalitab7425172014-08-26 07:56:44 -0700673static void drawTextBlob_rp(SkCanvas* canvas, SkReader32* reader, uint32_t op32,
674 SkGPipeState* state) {
675 UNIMPLEMENTED
676}
reed@google.combb6992a2011-04-26 17:41:56 +0000677///////////////////////////////////////////////////////////////////////////////
678
reed@google.combb6992a2011-04-26 17:41:56 +0000679static void paintOp_rp(SkCanvas*, SkReader32* reader, uint32_t op32,
680 SkGPipeState* state) {
reed@google.comb55d1182011-05-11 00:42:04 +0000681 size_t offset = reader->offset();
682 size_t stop = offset + PaintOp_unpackData(op32);
reed@google.com31891582011-05-12 03:03:56 +0000683 SkPaint* p = state->editPaint();
reed@google.combb6992a2011-04-26 17:41:56 +0000684
685 do {
686 uint32_t p32 = reader->readU32();
687 unsigned op = PaintOp_unpackOp(p32);
688 unsigned data = PaintOp_unpackData(p32);
reed@google.combb6992a2011-04-26 17:41:56 +0000689
reed@google.comb55d1182011-05-11 00:42:04 +0000690// SkDebugf(" read %08X op=%d flags=%d data=%d\n", p32, op, done, data);
reed@google.combb6992a2011-04-26 17:41:56 +0000691
692 switch (op) {
693 case kReset_PaintOp: p->reset(); break;
694 case kFlags_PaintOp: p->setFlags(data); break;
695 case kColor_PaintOp: p->setColor(reader->readU32()); break;
commit-bot@chromium.org85faf502014-04-16 12:58:02 +0000696 case kFilterLevel_PaintOp: p->setFilterLevel((SkPaint::FilterLevel)data); break;
reed@google.combb6992a2011-04-26 17:41:56 +0000697 case kStyle_PaintOp: p->setStyle((SkPaint::Style)data); break;
698 case kJoin_PaintOp: p->setStrokeJoin((SkPaint::Join)data); break;
699 case kCap_PaintOp: p->setStrokeCap((SkPaint::Cap)data); break;
700 case kWidth_PaintOp: p->setStrokeWidth(reader->readScalar()); break;
701 case kMiter_PaintOp: p->setStrokeMiter(reader->readScalar()); break;
702 case kEncoding_PaintOp:
703 p->setTextEncoding((SkPaint::TextEncoding)data);
704 break;
705 case kHinting_PaintOp: p->setHinting((SkPaint::Hinting)data); break;
706 case kAlign_PaintOp: p->setTextAlign((SkPaint::Align)data); break;
707 case kTextSize_PaintOp: p->setTextSize(reader->readScalar()); break;
708 case kTextScaleX_PaintOp: p->setTextScaleX(reader->readScalar()); break;
709 case kTextSkewX_PaintOp: p->setTextSkewX(reader->readScalar()); break;
reed@google.combb6793b2011-05-05 15:18:15 +0000710
reed@google.comb55d1182011-05-11 00:42:04 +0000711 case kFlatIndex_PaintOp: {
712 PaintFlats pf = (PaintFlats)PaintOp_unpackFlags(p32);
713 unsigned index = data;
714 set_paintflat(p, state->getFlat(index), pf);
715 break;
716 }
717
scroggo@google.com3cb969f2012-07-27 20:39:19 +0000718 case kTypeface_PaintOp:
719 SkASSERT(SkToBool(state->getFlags() &
720 SkGPipeWriter::kCrossProcess_Flag));
721 state->setTypeface(p, data); break;
tomhudson@google.com0c00f212011-12-28 14:59:50 +0000722 default: SkDEBUGFAIL("bad paintop"); return;
reed@google.combb6992a2011-04-26 17:41:56 +0000723 }
reed@google.comb55d1182011-05-11 00:42:04 +0000724 SkASSERT(reader->offset() <= stop);
reed@google.com31891582011-05-12 03:03:56 +0000725 } while (reader->offset() < stop);
reed@google.combb6992a2011-04-26 17:41:56 +0000726}
727
scroggo@google.com3cb969f2012-07-27 20:39:19 +0000728static void typeface_rp(SkCanvas*, SkReader32* reader, uint32_t,
729 SkGPipeState* state) {
730 SkASSERT(!SkToBool(state->getFlags() & SkGPipeWriter::kCrossProcess_Flag));
731 SkPaint* p = state->editPaint();
732 p->setTypeface(static_cast<SkTypeface*>(reader->readPtr()));
733}
734
reed@google.com0cd2ac62013-10-14 20:02:44 +0000735static void annotation_rp(SkCanvas*, SkReader32* reader, uint32_t op32,
736 SkGPipeState* state) {
737 SkPaint* p = state->editPaint();
skia.committer@gmail.comfbc58a32013-10-15 07:02:27 +0000738
commit-bot@chromium.org89ff3dd2013-10-29 20:29:38 +0000739 const size_t size = DrawOp_unpackData(op32);
740 if (size > 0) {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000741 SkReadBuffer buffer(reader->skip(size), size);
commit-bot@chromium.orgd9579842014-02-27 11:47:36 +0000742 p->setAnnotation(SkAnnotation::Create(buffer))->unref();
commit-bot@chromium.org89ff3dd2013-10-29 20:29:38 +0000743 SkASSERT(buffer.offset() == size);
reed@google.com0cd2ac62013-10-14 20:02:44 +0000744 } else {
745 p->setAnnotation(NULL);
746 }
747}
748
reed@google.combb6793b2011-05-05 15:18:15 +0000749///////////////////////////////////////////////////////////////////////////////
750
reed@google.com0faac1e2011-05-11 05:58:58 +0000751static void def_Typeface_rp(SkCanvas*, SkReader32*, uint32_t, SkGPipeState* state) {
752 state->addTypeface();
753}
754
reed@google.comb55d1182011-05-11 00:42:04 +0000755static void def_PaintFlat_rp(SkCanvas*, SkReader32*, uint32_t op32,
756 SkGPipeState* state) {
757 PaintFlats pf = (PaintFlats)DrawOp_unpackFlags(op32);
758 unsigned index = DrawOp_unpackData(op32);
759 state->defFlattenable(pf, index);
760}
761
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000762static void def_Bitmap_rp(SkCanvas*, SkReader32*, uint32_t op32,
763 SkGPipeState* state) {
764 unsigned index = DrawOp_unpackData(op32);
765 state->addBitmap(index);
766}
767
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000768static void def_Factory_rp(SkCanvas*, SkReader32* reader, uint32_t,
769 SkGPipeState* state) {
770 state->defFactory(reader->readString());
771}
772
reed@google.combb6992a2011-04-26 17:41:56 +0000773///////////////////////////////////////////////////////////////////////////////
774
reed@google.comacd471f2011-05-03 21:26:46 +0000775static void skip_rp(SkCanvas*, SkReader32* reader, uint32_t op32, SkGPipeState*) {
776 size_t bytes = DrawOp_unpackData(op32);
777 (void)reader->skip(bytes);
778}
779
scroggo@google.com10dccde2012-08-08 20:43:22 +0000780static void reportFlags_rp(SkCanvas*, SkReader32*, uint32_t op32,
scroggo@google.com565254b2012-06-28 15:41:32 +0000781 SkGPipeState* state) {
782 unsigned flags = DrawOp_unpackFlags(op32);
783 state->setFlags(flags);
784}
785
scroggo@google.comd9d29672012-08-14 17:21:34 +0000786static void shareBitmapHeap_rp(SkCanvas*, SkReader32* reader, uint32_t,
scroggo@google.com10dccde2012-08-08 20:43:22 +0000787 SkGPipeState* state) {
788 state->setSharedHeap(static_cast<SkBitmapHeap*>(reader->readPtr()));
789}
790
reed@google.combb6992a2011-04-26 17:41:56 +0000791static void done_rp(SkCanvas*, SkReader32*, uint32_t, SkGPipeState*) {}
792
793typedef void (*ReadProc)(SkCanvas*, SkReader32*, uint32_t op32, SkGPipeState*);
794
795static const ReadProc gReadTable[] = {
reed@google.comacd471f2011-05-03 21:26:46 +0000796 skip_rp,
reed@google.combb6992a2011-04-26 17:41:56 +0000797 clipPath_rp,
798 clipRegion_rp,
799 clipRect_rp,
reed@google.com4ed0fb72012-12-12 20:48:18 +0000800 clipRRect_rp,
reed@google.combb6992a2011-04-26 17:41:56 +0000801 concat_rp,
802 drawBitmap_rp,
803 drawBitmapMatrix_rp,
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000804 drawBitmapNine_rp,
reed@google.combb6992a2011-04-26 17:41:56 +0000805 drawBitmapRect_rp,
806 drawClear_rp,
807 drawData_rp,
commit-bot@chromium.orged9806f2014-02-21 02:32:36 +0000808 drawDRRect_rp,
reed@google.com4ed0fb72012-12-12 20:48:18 +0000809 drawOval_rp,
reed@google.combb6992a2011-04-26 17:41:56 +0000810 drawPaint_rp,
dandov963137b2014-08-07 07:49:53 -0700811 drawPatch_rp,
reed@google.combb6992a2011-04-26 17:41:56 +0000812 drawPath_rp,
813 drawPicture_rp,
814 drawPoints_rp,
815 drawPosText_rp,
816 drawPosTextH_rp,
817 drawRect_rp,
reed@google.com4ed0fb72012-12-12 20:48:18 +0000818 drawRRect_rp,
reed@google.combb6992a2011-04-26 17:41:56 +0000819 drawSprite_rp,
820 drawText_rp,
fmalitab7425172014-08-26 07:56:44 -0700821 drawTextBlob_rp,
reed@google.combb6992a2011-04-26 17:41:56 +0000822 drawTextOnPath_rp,
823 drawVertices_rp,
824 restore_rp,
825 rotate_rp,
826 save_rp,
827 saveLayer_rp,
828 scale_rp,
829 setMatrix_rp,
830 skew_rp,
831 translate_rp,
reed@google.com0faac1e2011-05-11 05:58:58 +0000832
reed@google.combb6992a2011-04-26 17:41:56 +0000833 paintOp_rp,
scroggo@google.com3cb969f2012-07-27 20:39:19 +0000834 typeface_rp,
reed@google.com0cd2ac62013-10-14 20:02:44 +0000835 annotation_rp,
836
reed@google.combb6793b2011-05-05 15:18:15 +0000837 def_Typeface_rp,
reed@google.com0faac1e2011-05-11 05:58:58 +0000838 def_PaintFlat_rp,
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000839 def_Bitmap_rp,
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000840 def_Factory_rp,
reed@google.com0faac1e2011-05-11 05:58:58 +0000841
scroggo@google.com10dccde2012-08-08 20:43:22 +0000842 reportFlags_rp,
scroggo@google.comd9d29672012-08-14 17:21:34 +0000843 shareBitmapHeap_rp,
reed@google.combb6992a2011-04-26 17:41:56 +0000844 done_rp
845};
846
847///////////////////////////////////////////////////////////////////////////////
848
tomhudson@google.com8afae612012-08-14 15:03:35 +0000849SkGPipeState::SkGPipeState()
850 : fReader(0)
junov@chromium.orgfb103892012-09-20 19:35:43 +0000851 , fSilent(false)
tomhudson@google.com8afae612012-08-14 15:03:35 +0000852 , fSharedHeap(NULL)
853 , fFlags(0) {
854
855}
reed@google.combb6992a2011-04-26 17:41:56 +0000856
reed@google.comb55d1182011-05-11 00:42:04 +0000857SkGPipeState::~SkGPipeState() {
reed@google.com6bac9472011-06-21 19:24:00 +0000858 fTypefaces.safeUnrefAll();
859 fFlatArray.safeUnrefAll();
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000860 fBitmaps.deleteAll();
scroggo@google.com10dccde2012-08-08 20:43:22 +0000861 SkSafeUnref(fSharedHeap);
reed@google.combb6992a2011-04-26 17:41:56 +0000862}
863
864///////////////////////////////////////////////////////////////////////////////
865
866#include "SkGPipe.h"
867
scroggo@google.com72c96722012-06-06 21:07:10 +0000868SkGPipeReader::SkGPipeReader() {
869 fCanvas = NULL;
reed@google.combb6992a2011-04-26 17:41:56 +0000870 fState = NULL;
scroggo@google.com74b7ffd2013-04-30 02:32:41 +0000871 fProc = NULL;
reed@google.combb6992a2011-04-26 17:41:56 +0000872}
873
scroggo@google.com72c96722012-06-06 21:07:10 +0000874SkGPipeReader::SkGPipeReader(SkCanvas* target) {
875 fCanvas = NULL;
876 this->setCanvas(target);
877 fState = NULL;
scroggo@google.com74b7ffd2013-04-30 02:32:41 +0000878 fProc = NULL;
scroggo@google.com72c96722012-06-06 21:07:10 +0000879}
880
881void SkGPipeReader::setCanvas(SkCanvas *target) {
882 SkRefCnt_SafeAssign(fCanvas, target);
883}
884
reed@google.combb6992a2011-04-26 17:41:56 +0000885SkGPipeReader::~SkGPipeReader() {
886 SkSafeUnref(fCanvas);
887 delete fState;
888}
889
reed@google.com80b09de2011-05-23 13:54:24 +0000890SkGPipeReader::Status SkGPipeReader::playback(const void* data, size_t length,
junov@chromium.orgfb103892012-09-20 19:35:43 +0000891 uint32_t playbackFlags, size_t* bytesRead) {
reed@google.combb6992a2011-04-26 17:41:56 +0000892 if (NULL == fCanvas) {
893 return kError_Status;
894 }
895
896 if (NULL == fState) {
897 fState = new SkGPipeState;
898 }
899
junov@chromium.orgfb103892012-09-20 19:35:43 +0000900 fState->setSilent(playbackFlags & kSilent_PlaybackFlag);
901
reed@google.com0faac1e2011-05-11 05:58:58 +0000902 SkASSERT(SK_ARRAY_COUNT(gReadTable) == (kDone_DrawOp + 1));
903
reed@google.combb6992a2011-04-26 17:41:56 +0000904 const ReadProc* table = gReadTable;
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000905 SkReadBuffer reader(data, length);
scroggo@google.com74b7ffd2013-04-30 02:32:41 +0000906 reader.setBitmapDecoder(fProc);
reed@google.combb6992a2011-04-26 17:41:56 +0000907 SkCanvas* canvas = fCanvas;
reed@google.com80b09de2011-05-23 13:54:24 +0000908 Status status = kEOF_Status;
reed@google.combb6793b2011-05-05 15:18:15 +0000909
910 fState->setReader(&reader);
reed@google.combb6992a2011-04-26 17:41:56 +0000911 while (!reader.eof()) {
djsollen@google.comc73dd5c2012-08-07 15:54:32 +0000912 uint32_t op32 = reader.readUInt();
reed@google.combb6992a2011-04-26 17:41:56 +0000913 unsigned op = DrawOp_unpackOp(op32);
caryclark@google.com920901d2012-06-06 12:04:11 +0000914 // SkDEBUGCODE(DrawOps drawOp = (DrawOps)op;)
rmistry@google.comd6176b02012-08-23 18:14:13 +0000915
reed@google.combb6992a2011-04-26 17:41:56 +0000916 if (op >= SK_ARRAY_COUNT(gReadTable)) {
917 SkDebugf("---- bad op during GPipeState::playback\n");
reed@google.com80b09de2011-05-23 13:54:24 +0000918 status = kError_Status;
919 break;
reed@google.combb6992a2011-04-26 17:41:56 +0000920 }
921 if (kDone_DrawOp == op) {
reed@google.com80b09de2011-05-23 13:54:24 +0000922 status = kDone_Status;
923 break;
reed@google.combb6992a2011-04-26 17:41:56 +0000924 }
djsollen@google.com2b2ede32012-04-12 13:24:04 +0000925 table[op](canvas, reader.getReader32(), op32, fState);
junov@chromium.orgfb103892012-09-20 19:35:43 +0000926 if ((playbackFlags & kReadAtom_PlaybackFlag) &&
yangsu@google.com1bce0a52011-06-16 21:08:19 +0000927 (table[op] != paintOp_rp &&
928 table[op] != def_Typeface_rp &&
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000929 table[op] != def_PaintFlat_rp &&
930 table[op] != def_Bitmap_rp
yangsu@google.com1bce0a52011-06-16 21:08:19 +0000931 )) {
932 status = kReadAtom_Status;
933 break;
934 }
reed@google.combb6992a2011-04-26 17:41:56 +0000935 }
reed@google.com80b09de2011-05-23 13:54:24 +0000936
937 if (bytesRead) {
reed@google.comac45f002011-05-24 13:50:21 +0000938 *bytesRead = reader.offset();
reed@google.com80b09de2011-05-23 13:54:24 +0000939 }
940 return status;
reed@google.combb6992a2011-04-26 17:41:56 +0000941}