blob: 35b0a9457088aefe3243233f48ddcabf3425f2b8 [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"
reed@google.combb6793b2011-05-05 15:18:15 +000024#include "SkPathEffect.h"
25#include "SkRasterizer.h"
reed@google.com4ed0fb72012-12-12 20:48:18 +000026#include "SkRRect.h"
reed@google.combb6793b2011-05-05 15:18:15 +000027#include "SkShader.h"
reed@google.comf5842f72011-05-04 18:30:04 +000028#include "SkTypeface.h"
reed@google.combb6793b2011-05-05 15:18:15 +000029#include "SkXfermode.h"
30
commit-bot@chromium.orgc0b7e102013-10-23 17:06:21 +000031static SkFlattenable::Type paintflat_to_flattype(PaintFlats pf) {
reed@google.com35348222013-10-16 13:05:06 +000032 static const uint8_t gEffectTypesInPaintFlatsOrder[] = {
commit-bot@chromium.orgc0b7e102013-10-23 17:06:21 +000033 SkFlattenable::kSkColorFilter_Type,
34 SkFlattenable::kSkDrawLooper_Type,
35 SkFlattenable::kSkImageFilter_Type,
36 SkFlattenable::kSkMaskFilter_Type,
37 SkFlattenable::kSkPathEffect_Type,
38 SkFlattenable::kSkRasterizer_Type,
39 SkFlattenable::kSkShader_Type,
40 SkFlattenable::kSkXfermode_Type,
reed@google.com35348222013-10-16 13:05:06 +000041 };
skia.committer@gmail.com4677acc2013-10-17 07:02:33 +000042
reed@google.com35348222013-10-16 13:05:06 +000043 SkASSERT((size_t)pf < SK_ARRAY_COUNT(gEffectTypesInPaintFlatsOrder));
commit-bot@chromium.orgc0b7e102013-10-23 17:06:21 +000044 return (SkFlattenable::Type)gEffectTypesInPaintFlatsOrder[pf];
reed@google.com35348222013-10-16 13:05:06 +000045}
46
reed@google.comb55d1182011-05-11 00:42:04 +000047static void set_paintflat(SkPaint* paint, SkFlattenable* obj, unsigned paintFlat) {
48 SkASSERT(paintFlat < kCount_PaintFlats);
49 switch (paintFlat) {
50 case kColorFilter_PaintFlat:
51 paint->setColorFilter((SkColorFilter*)obj);
52 break;
reed@google.com0faac1e2011-05-11 05:58:58 +000053 case kDrawLooper_PaintFlat:
54 paint->setLooper((SkDrawLooper*)obj);
55 break;
reed@google.comb55d1182011-05-11 00:42:04 +000056 case kMaskFilter_PaintFlat:
57 paint->setMaskFilter((SkMaskFilter*)obj);
58 break;
59 case kPathEffect_PaintFlat:
60 paint->setPathEffect((SkPathEffect*)obj);
61 break;
62 case kRasterizer_PaintFlat:
63 paint->setRasterizer((SkRasterizer*)obj);
64 break;
65 case kShader_PaintFlat:
66 paint->setShader((SkShader*)obj);
67 break;
scroggo@google.com16d1d0b2012-05-02 19:09:40 +000068 case kImageFilter_PaintFlat:
69 paint->setImageFilter((SkImageFilter*)obj);
70 break;
reed@google.comb55d1182011-05-11 00:42:04 +000071 case kXfermode_PaintFlat:
72 paint->setXfermode((SkXfermode*)obj);
73 break;
74 default:
tomhudson@google.com0c00f212011-12-28 14:59:50 +000075 SkDEBUGFAIL("never gets here");
reed@google.comb55d1182011-05-11 00:42:04 +000076 }
77}
78
reed@google.combb6793b2011-05-05 15:18:15 +000079template <typename T> class SkRefCntTDArray : public SkTDArray<T> {
80public:
81 ~SkRefCntTDArray() { this->unrefAll(); }
82};
reed@google.combb6992a2011-04-26 17:41:56 +000083
scroggo@google.com7ca24432012-08-14 15:48:43 +000084class SkGPipeState : public SkBitmapHeapReader {
reed@google.combb6992a2011-04-26 17:41:56 +000085public:
86 SkGPipeState();
87 ~SkGPipeState();
reed@google.combb6793b2011-05-05 15:18:15 +000088
junov@chromium.orgfb103892012-09-20 19:35:43 +000089 void setSilent(bool silent) {
90 fSilent = silent;
91 }
92
93 bool shouldDraw() {
94 return !fSilent;
95 }
96
scroggo@google.com565254b2012-06-28 15:41:32 +000097 void setFlags(unsigned flags) {
98 if (fFlags != flags) {
99 fFlags = flags;
100 this->updateReader();
101 }
102 }
103
104 unsigned getFlags() const {
105 return fFlags;
106 }
107
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000108 void setReader(SkReadBuffer* reader) {
reed@google.comdde09562011-05-23 12:21:05 +0000109 fReader = reader;
scroggo@google.com565254b2012-06-28 15:41:32 +0000110 this->updateReader();
reed@google.comdde09562011-05-23 12:21:05 +0000111 }
reed@google.combb6793b2011-05-05 15:18:15 +0000112
reed@google.com31891582011-05-12 03:03:56 +0000113 const SkPaint& paint() const { return fPaint; }
114 SkPaint* editPaint() { return &fPaint; }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000115
reed@google.comb55d1182011-05-11 00:42:04 +0000116 SkFlattenable* getFlat(unsigned index) const {
117 if (0 == index) {
118 return NULL;
119 }
120 return fFlatArray[index - 1];
121 }
122
reed@google.com67908f22011-06-27 14:47:50 +0000123 void defFlattenable(PaintFlats pf, int index) {
scroggo@google.comd3ba5cc2012-07-09 16:05:53 +0000124 index--;
commit-bot@chromium.orgc0b7e102013-10-23 17:06:21 +0000125 SkFlattenable* obj = fReader->readFlattenable(paintflat_to_flattype(pf));
scroggo@google.comd3ba5cc2012-07-09 16:05:53 +0000126 if (fFlatArray.count() == index) {
127 *fFlatArray.append() = obj;
128 } else {
129 SkSafeUnref(fFlatArray[index]);
130 fFlatArray[index] = obj;
131 }
reed@google.comb55d1182011-05-11 00:42:04 +0000132 }
133
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000134 void defFactory(const char* name) {
135 SkFlattenable::Factory factory = SkFlattenable::NameToFactory(name);
136 if (factory) {
137 SkASSERT(fFactoryArray.find(factory) < 0);
138 *fFactoryArray.append() = factory;
139 }
140 }
141
scroggo@google.com74b7ffd2013-04-30 02:32:41 +0000142 /**
143 * Add a bitmap to the array of bitmaps, or replace an existing one.
144 * This is only used when in cross process mode without a shared heap.
145 */
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000146 void addBitmap(int index) {
scroggo@google.com74b7ffd2013-04-30 02:32:41 +0000147 SkASSERT(shouldFlattenBitmaps(fFlags));
scroggo@google.com4dffc592012-07-17 16:49:40 +0000148 SkBitmap* bm;
149 if(fBitmaps.count() == index) {
150 bm = SkNEW(SkBitmap);
151 *fBitmaps.append() = bm;
152 } else {
153 bm = fBitmaps[index];
154 }
scroggo@google.com74b7ffd2013-04-30 02:32:41 +0000155 fReader->readBitmap(bm);
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000156 }
157
scroggo@google.com7ca24432012-08-14 15:48:43 +0000158 /**
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000159 * Override of SkBitmapHeapReader, so that SkReadBuffer can use
scroggo@google.com74b7ffd2013-04-30 02:32:41 +0000160 * these SkBitmaps for bitmap shaders. Used only in cross process mode
161 * without a shared heap.
scroggo@google.com7ca24432012-08-14 15:48:43 +0000162 */
163 virtual SkBitmap* getBitmap(int32_t index) const SK_OVERRIDE {
scroggo@google.com74b7ffd2013-04-30 02:32:41 +0000164 SkASSERT(shouldFlattenBitmaps(fFlags));
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000165 return fBitmaps[index];
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000166 }
167
scroggo@google.com7ca24432012-08-14 15:48:43 +0000168 /**
169 * Needed to be a non-abstract subclass of SkBitmapHeapReader.
170 */
171 virtual void releaseRef(int32_t) SK_OVERRIDE {}
172
scroggo@google.com10dccde2012-08-08 20:43:22 +0000173 void setSharedHeap(SkBitmapHeap* heap) {
174 SkASSERT(!shouldFlattenBitmaps(fFlags) || NULL == heap);
175 SkRefCnt_SafeAssign(fSharedHeap, heap);
scroggo@google.com7ca24432012-08-14 15:48:43 +0000176 this->updateReader();
scroggo@google.com10dccde2012-08-08 20:43:22 +0000177 }
178
scroggo@google.com74b7ffd2013-04-30 02:32:41 +0000179 /**
180 * Access the shared heap. Only used in the case when bitmaps are not
181 * flattened.
182 */
scroggo@google.com10dccde2012-08-08 20:43:22 +0000183 SkBitmapHeap* getSharedHeap() const {
scroggo@google.com74b7ffd2013-04-30 02:32:41 +0000184 SkASSERT(!shouldFlattenBitmaps(fFlags));
scroggo@google.com10dccde2012-08-08 20:43:22 +0000185 return fSharedHeap;
186 }
187
reed@google.combb6793b2011-05-05 15:18:15 +0000188 void addTypeface() {
djsollen@google.comc73dd5c2012-08-07 15:54:32 +0000189 size_t size = fReader->read32();
reed@google.combb6793b2011-05-05 15:18:15 +0000190 const void* data = fReader->skip(SkAlign4(size));
reed@google.comf5842f72011-05-04 18:30:04 +0000191 SkMemoryStream stream(data, size, false);
reed@google.comf5842f72011-05-04 18:30:04 +0000192 *fTypefaces.append() = SkTypeface::Deserialize(&stream);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000193 }
scroggo@google.com10dccde2012-08-08 20:43:22 +0000194
reed@google.combb6793b2011-05-05 15:18:15 +0000195 void setTypeface(SkPaint* paint, unsigned id) {
196 paint->setTypeface(id ? fTypefaces[id - 1] : NULL);
197 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000198
reed@google.comb55d1182011-05-11 00:42:04 +0000199private:
scroggo@google.com565254b2012-06-28 15:41:32 +0000200 void updateReader() {
201 if (NULL == fReader) {
202 return;
203 }
204 bool crossProcess = SkToBool(fFlags & SkGPipeWriter::kCrossProcess_Flag);
205 fReader->setFlags(SkSetClearMask(fReader->getFlags(), crossProcess,
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000206 SkReadBuffer::kCrossProcess_Flag));
scroggo@google.com565254b2012-06-28 15:41:32 +0000207 if (crossProcess) {
208 fReader->setFactoryArray(&fFactoryArray);
209 } else {
210 fReader->setFactoryArray(NULL);
211 }
scroggo@google.com7ca24432012-08-14 15:48:43 +0000212
213 if (shouldFlattenBitmaps(fFlags)) {
214 fReader->setBitmapStorage(this);
215 } else {
216 fReader->setBitmapStorage(fSharedHeap);
217 }
scroggo@google.com565254b2012-06-28 15:41:32 +0000218 }
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000219 SkReadBuffer* fReader;
reed@google.com31891582011-05-12 03:03:56 +0000220 SkPaint fPaint;
221 SkTDArray<SkFlattenable*> fFlatArray;
222 SkTDArray<SkTypeface*> fTypefaces;
reed@google.comdde09562011-05-23 12:21:05 +0000223 SkTDArray<SkFlattenable::Factory> fFactoryArray;
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000224 SkTDArray<SkBitmap*> fBitmaps;
junov@chromium.orgfb103892012-09-20 19:35:43 +0000225 bool fSilent;
scroggo@google.com10dccde2012-08-08 20:43:22 +0000226 // Only used when sharing bitmaps with the writer.
227 SkBitmapHeap* fSharedHeap;
scroggo@google.com565254b2012-06-28 15:41:32 +0000228 unsigned fFlags;
reed@google.combb6992a2011-04-26 17:41:56 +0000229};
230
231///////////////////////////////////////////////////////////////////////////////
232
commit-bot@chromium.orgb45bd1f2014-04-24 18:17:30 +0000233template <typename T> const T* skip(SkReader32* reader, size_t count = 1) {
reed@google.combb6992a2011-04-26 17:41:56 +0000234 size_t size = sizeof(T) * count;
235 SkASSERT(SkAlign4(size) == size);
236 return reinterpret_cast<const T*>(reader->skip(size));
237}
238
commit-bot@chromium.orgb45bd1f2014-04-24 18:17:30 +0000239template <typename T> const T* skipAlign(SkReader32* reader, size_t count = 1) {
reed@google.combb6992a2011-04-26 17:41:56 +0000240 size_t size = SkAlign4(sizeof(T) * count);
241 return reinterpret_cast<const T*>(reader->skip(size));
242}
243
reed@google.combb6992a2011-04-26 17:41:56 +0000244///////////////////////////////////////////////////////////////////////////////
245///////////////////////////////////////////////////////////////////////////////
246
247static void clipPath_rp(SkCanvas* canvas, SkReader32* reader, uint32_t op32,
248 SkGPipeState* state) {
249 SkPath path;
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000250 reader->readPath(&path);
scroggo@google.com460a23e2012-08-16 17:56:49 +0000251 bool doAA = SkToBool(DrawOp_unpackFlags(op32) & kClip_HasAntiAlias_DrawOpFlag);
252 canvas->clipPath(path, (SkRegion::Op)DrawOp_unpackData(op32), doAA);
reed@google.combb6992a2011-04-26 17:41:56 +0000253}
254
255static void clipRegion_rp(SkCanvas* canvas, SkReader32* reader, uint32_t op32,
256 SkGPipeState* state) {
257 SkRegion rgn;
djsollen@google.com2b2ede32012-04-12 13:24:04 +0000258 reader->readRegion(&rgn);
reed@google.combb6992a2011-04-26 17:41:56 +0000259 canvas->clipRegion(rgn, (SkRegion::Op)DrawOp_unpackData(op32));
260}
261
262static void clipRect_rp(SkCanvas* canvas, SkReader32* reader, uint32_t op32,
263 SkGPipeState* state) {
scroggo@google.com691703c2012-06-05 14:58:40 +0000264 const SkRect* rect = skip<SkRect>(reader);
scroggo@google.com460a23e2012-08-16 17:56:49 +0000265 bool doAA = SkToBool(DrawOp_unpackFlags(op32) & kClip_HasAntiAlias_DrawOpFlag);
scroggo@google.com691703c2012-06-05 14:58:40 +0000266 canvas->clipRect(*rect, (SkRegion::Op)DrawOp_unpackData(op32), doAA);
reed@google.combb6992a2011-04-26 17:41:56 +0000267}
268
reed@google.com4ed0fb72012-12-12 20:48:18 +0000269static void clipRRect_rp(SkCanvas* canvas, SkReader32* reader, uint32_t op32,
270 SkGPipeState* state) {
271 SkRRect rrect;
272 reader->readRRect(&rrect);
273 bool doAA = SkToBool(DrawOp_unpackFlags(op32) & kClip_HasAntiAlias_DrawOpFlag);
274 canvas->clipRRect(rrect, (SkRegion::Op)DrawOp_unpackData(op32), doAA);
275}
276
reed@google.combb6992a2011-04-26 17:41:56 +0000277///////////////////////////////////////////////////////////////////////////////
278
279static void setMatrix_rp(SkCanvas* canvas, SkReader32* reader, uint32_t op32,
280 SkGPipeState* state) {
281 SkMatrix matrix;
djsollen@google.com2b2ede32012-04-12 13:24:04 +0000282 reader->readMatrix(&matrix);
reed@google.combb6992a2011-04-26 17:41:56 +0000283 canvas->setMatrix(matrix);
284}
285
286static void concat_rp(SkCanvas* canvas, SkReader32* reader, uint32_t op32,
287 SkGPipeState* state) {
288 SkMatrix matrix;
djsollen@google.com2b2ede32012-04-12 13:24:04 +0000289 reader->readMatrix(&matrix);
reed@google.combb6992a2011-04-26 17:41:56 +0000290 canvas->concat(matrix);
291}
292
293static void scale_rp(SkCanvas* canvas, SkReader32* reader, uint32_t op32,
294 SkGPipeState* state) {
295 const SkScalar* param = skip<SkScalar>(reader, 2);
296 canvas->scale(param[0], param[1]);
297}
298
299static void skew_rp(SkCanvas* canvas, SkReader32* reader, uint32_t op32,
300 SkGPipeState* state) {
301 const SkScalar* param = skip<SkScalar>(reader, 2);
302 canvas->skew(param[0], param[1]);
303}
304
305static void rotate_rp(SkCanvas* canvas, SkReader32* reader, uint32_t op32,
306 SkGPipeState* state) {
307 canvas->rotate(reader->readScalar());
308}
309
310static void translate_rp(SkCanvas* canvas, SkReader32* reader, uint32_t op32,
311 SkGPipeState* state) {
312 const SkScalar* param = skip<SkScalar>(reader, 2);
313 canvas->translate(param[0], param[1]);
314}
315
316///////////////////////////////////////////////////////////////////////////////
317
318static void save_rp(SkCanvas* canvas, SkReader32* reader, uint32_t op32,
319 SkGPipeState* state) {
Florin Malita5f6102d2014-06-30 10:13:28 -0400320 canvas->save();
reed@google.combb6992a2011-04-26 17:41:56 +0000321}
322
323static void saveLayer_rp(SkCanvas* canvas, SkReader32* reader, uint32_t op32,
324 SkGPipeState* state) {
reed@google.comacd471f2011-05-03 21:26:46 +0000325 unsigned flags = DrawOp_unpackFlags(op32);
326 SkCanvas::SaveFlags saveFlags = (SkCanvas::SaveFlags)DrawOp_unpackData(op32);
reed@google.combb6992a2011-04-26 17:41:56 +0000327
328 const SkRect* bounds = NULL;
329 if (flags & kSaveLayer_HasBounds_DrawOpFlag) {
330 bounds = skip<SkRect>(reader);
331 }
332 const SkPaint* paint = NULL;
333 if (flags & kSaveLayer_HasPaint_DrawOpFlag) {
reed@google.com31891582011-05-12 03:03:56 +0000334 paint = &state->paint();
reed@google.combb6992a2011-04-26 17:41:56 +0000335 }
reed@google.comacd471f2011-05-03 21:26:46 +0000336 canvas->saveLayer(bounds, paint, saveFlags);
reed@google.combb6992a2011-04-26 17:41:56 +0000337}
338
339static void restore_rp(SkCanvas* canvas, SkReader32* reader, uint32_t op32,
340 SkGPipeState* state) {
341 canvas->restore();
342}
343
344///////////////////////////////////////////////////////////////////////////////
345
346static void drawClear_rp(SkCanvas* canvas, SkReader32* reader, uint32_t op32,
347 SkGPipeState* state) {
348 SkColor color = 0;
349 if (DrawOp_unpackFlags(op32) & kClear_HasColor_DrawOpFlag) {
350 color = reader->readU32();
351 }
352 canvas->clear(color);
353}
354
355static void drawPaint_rp(SkCanvas* canvas, SkReader32* reader, uint32_t op32,
356 SkGPipeState* state) {
junov@chromium.orgfb103892012-09-20 19:35:43 +0000357 if (state->shouldDraw()) {
358 canvas->drawPaint(state->paint());
359 }
reed@google.combb6992a2011-04-26 17:41:56 +0000360}
361
362static void drawPoints_rp(SkCanvas* canvas, SkReader32* reader, uint32_t op32,
363 SkGPipeState* state) {
364 SkCanvas::PointMode mode = (SkCanvas::PointMode)DrawOp_unpackFlags(op32);
365 size_t count = reader->readU32();
366 const SkPoint* pts = skip<SkPoint>(reader, count);
junov@chromium.orgfb103892012-09-20 19:35:43 +0000367 if (state->shouldDraw()) {
368 canvas->drawPoints(mode, count, pts, state->paint());
369 }
reed@google.combb6992a2011-04-26 17:41:56 +0000370}
371
reed@google.com4ed0fb72012-12-12 20:48:18 +0000372static void drawOval_rp(SkCanvas* canvas, SkReader32* reader, uint32_t op32,
373 SkGPipeState* state) {
374 const SkRect* rect = skip<SkRect>(reader);
375 if (state->shouldDraw()) {
376 canvas->drawOval(*rect, state->paint());
377 }
378}
379
reed@google.combb6992a2011-04-26 17:41:56 +0000380static void drawRect_rp(SkCanvas* canvas, SkReader32* reader, uint32_t op32,
381 SkGPipeState* state) {
junov@chromium.orgfb103892012-09-20 19:35:43 +0000382 const SkRect* rect = skip<SkRect>(reader);
383 if (state->shouldDraw()) {
384 canvas->drawRect(*rect, state->paint());
385 }
reed@google.combb6992a2011-04-26 17:41:56 +0000386}
387
reed@google.com4ed0fb72012-12-12 20:48:18 +0000388static void drawRRect_rp(SkCanvas* canvas, SkReader32* reader, uint32_t op32,
389 SkGPipeState* state) {
390 SkRRect rrect;
391 reader->readRRect(&rrect);
392 if (state->shouldDraw()) {
393 canvas->drawRRect(rrect, state->paint());
394 }
395}
396
commit-bot@chromium.orged9806f2014-02-21 02:32:36 +0000397static void drawDRRect_rp(SkCanvas* canvas, SkReader32* reader, uint32_t op32,
398 SkGPipeState* state) {
399 SkRRect outer, inner;
400 reader->readRRect(&outer);
401 reader->readRRect(&inner);
402 if (state->shouldDraw()) {
403 canvas->drawDRRect(outer, inner, state->paint());
404 }
405}
406
dandov963137b2014-08-07 07:49:53 -0700407static void drawPatch_rp(SkCanvas* canvas, SkReader32* reader, uint32_t op32,
408 SkGPipeState* state) {
409 SkPatch patch;
410 reader->readPatch(&patch);
411 if (state->shouldDraw()) {
412 canvas->drawPatch(patch, state->paint());
413 }
414}
415
reed@google.combb6992a2011-04-26 17:41:56 +0000416static void drawPath_rp(SkCanvas* canvas, SkReader32* reader, uint32_t op32,
417 SkGPipeState* state) {
418 SkPath path;
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000419 reader->readPath(&path);
junov@chromium.orgfb103892012-09-20 19:35:43 +0000420 if (state->shouldDraw()) {
421 canvas->drawPath(path, state->paint());
422 }
reed@google.combb6992a2011-04-26 17:41:56 +0000423}
424
425static void drawVertices_rp(SkCanvas* canvas, SkReader32* reader, uint32_t op32,
426 SkGPipeState* state) {
427 unsigned flags = DrawOp_unpackFlags(op32);
428
reed@google.com85e143c2013-12-30 15:51:25 +0000429 SkCanvas::VertexMode vmode = (SkCanvas::VertexMode)reader->readU32();
reed@google.combb6992a2011-04-26 17:41:56 +0000430 int vertexCount = reader->readU32();
431 const SkPoint* verts = skip<SkPoint>(reader, vertexCount);
432
433 const SkPoint* texs = NULL;
434 if (flags & kDrawVertices_HasTexs_DrawOpFlag) {
435 texs = skip<SkPoint>(reader, vertexCount);
436 }
437
438 const SkColor* colors = NULL;
439 if (flags & kDrawVertices_HasColors_DrawOpFlag) {
440 colors = skip<SkColor>(reader, vertexCount);
441 }
442
reed@google.com85e143c2013-12-30 15:51:25 +0000443 SkAutoTUnref<SkXfermode> xfer;
444 if (flags & kDrawVertices_HasXfermode_DrawOpFlag) {
445 SkXfermode::Mode mode = (SkXfermode::Mode)reader->readU32();
446 xfer.reset(SkXfermode::Create(mode));
447 }
reed@google.combb6992a2011-04-26 17:41:56 +0000448
449 int indexCount = 0;
450 const uint16_t* indices = NULL;
451 if (flags & kDrawVertices_HasIndices_DrawOpFlag) {
452 indexCount = reader->readU32();
453 indices = skipAlign<uint16_t>(reader, indexCount);
454 }
junov@chromium.orgfb103892012-09-20 19:35:43 +0000455 if (state->shouldDraw()) {
reed@google.com85e143c2013-12-30 15:51:25 +0000456 canvas->drawVertices(vmode, vertexCount, verts, texs, colors, xfer,
junov@chromium.orgfb103892012-09-20 19:35:43 +0000457 indices, indexCount, state->paint());
458 }
reed@google.combb6992a2011-04-26 17:41:56 +0000459}
460
461///////////////////////////////////////////////////////////////////////////////
462
463static void drawText_rp(SkCanvas* canvas, SkReader32* reader, uint32_t op32,
464 SkGPipeState* state) {
465 size_t len = reader->readU32();
466 const void* text = reader->skip(SkAlign4(len));
467 const SkScalar* xy = skip<SkScalar>(reader, 2);
junov@chromium.orgfb103892012-09-20 19:35:43 +0000468 if (state->shouldDraw()) {
469 canvas->drawText(text, len, xy[0], xy[1], state->paint());
470 }
reed@google.combb6992a2011-04-26 17:41:56 +0000471}
472
473static void drawPosText_rp(SkCanvas* canvas, SkReader32* reader, uint32_t op32,
474 SkGPipeState* state) {
475 size_t len = reader->readU32();
476 const void* text = reader->skip(SkAlign4(len));
477 size_t posCount = reader->readU32(); // compute by our writer
478 const SkPoint* pos = skip<SkPoint>(reader, posCount);
junov@chromium.orgfb103892012-09-20 19:35:43 +0000479 if (state->shouldDraw()) {
480 canvas->drawPosText(text, len, pos, state->paint());
481 }
reed@google.combb6992a2011-04-26 17:41:56 +0000482}
483
484static void drawPosTextH_rp(SkCanvas* canvas, SkReader32* reader, uint32_t op32,
485 SkGPipeState* state) {
486 size_t len = reader->readU32();
487 const void* text = reader->skip(SkAlign4(len));
488 size_t posCount = reader->readU32(); // compute by our writer
489 const SkScalar* xpos = skip<SkScalar>(reader, posCount);
490 SkScalar constY = reader->readScalar();
junov@chromium.orgfb103892012-09-20 19:35:43 +0000491 if (state->shouldDraw()) {
492 canvas->drawPosTextH(text, len, xpos, constY, state->paint());
493 }
reed@google.combb6992a2011-04-26 17:41:56 +0000494}
495
496static void drawTextOnPath_rp(SkCanvas* canvas, SkReader32* reader, uint32_t op32,
497 SkGPipeState* state) {
498 size_t len = reader->readU32();
499 const void* text = reader->skip(SkAlign4(len));
500
501 SkPath path;
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000502 reader->readPath(&path);
reed@google.combb6992a2011-04-26 17:41:56 +0000503
504 SkMatrix matrixStorage;
505 const SkMatrix* matrix = NULL;
506 if (DrawOp_unpackFlags(op32) & kDrawTextOnPath_HasMatrix_DrawOpFlag) {
djsollen@google.com2b2ede32012-04-12 13:24:04 +0000507 reader->readMatrix(&matrixStorage);
reed@google.combb6992a2011-04-26 17:41:56 +0000508 matrix = &matrixStorage;
509 }
junov@chromium.orgfb103892012-09-20 19:35:43 +0000510 if (state->shouldDraw()) {
511 canvas->drawTextOnPath(text, len, path, matrix, state->paint());
512 }
reed@google.combb6992a2011-04-26 17:41:56 +0000513}
514
515///////////////////////////////////////////////////////////////////////////////
516
scroggo@google.com284bf502012-07-17 16:10:34 +0000517class BitmapHolder : SkNoncopyable {
518public:
519 BitmapHolder(SkReader32* reader, uint32_t op32, SkGPipeState* state);
520 ~BitmapHolder() {
scroggo@google.com10dccde2012-08-08 20:43:22 +0000521 if (fHeapEntry != NULL) {
522 fHeapEntry->releaseRef();
scroggo@google.com284bf502012-07-17 16:10:34 +0000523 }
scroggo@google.com565254b2012-06-28 15:41:32 +0000524 }
scroggo@google.com284bf502012-07-17 16:10:34 +0000525 const SkBitmap* getBitmap() {
526 return fBitmap;
527 }
528private:
scroggo@google.com10dccde2012-08-08 20:43:22 +0000529 SkBitmapHeapEntry* fHeapEntry;
530 const SkBitmap* fBitmap;
scroggo@google.com58b4ead2012-08-31 16:15:22 +0000531 SkBitmap fBitmapStorage;
scroggo@google.com284bf502012-07-17 16:10:34 +0000532};
533
534BitmapHolder::BitmapHolder(SkReader32* reader, uint32_t op32,
535 SkGPipeState* state) {
scroggo@google.com58b4ead2012-08-31 16:15:22 +0000536 const unsigned flags = state->getFlags();
537 const unsigned index = DrawOp_unpackData(op32);
538 if (shouldFlattenBitmaps(flags)) {
scroggo@google.com10dccde2012-08-08 20:43:22 +0000539 fHeapEntry = NULL;
540 fBitmap = state->getBitmap(index);
scroggo@google.com284bf502012-07-17 16:10:34 +0000541 } else {
scroggo@google.com58b4ead2012-08-31 16:15:22 +0000542 SkBitmapHeapEntry* entry = state->getSharedHeap()->getEntry(index);
543 if (SkToBool(flags & SkGPipeWriter::kSimultaneousReaders_Flag)) {
544 // Make a shallow copy for thread safety. Each thread will point to the same SkPixelRef,
545 // which is thread safe.
546 fBitmapStorage = *entry->getBitmap();
547 fBitmap = &fBitmapStorage;
548 // Release the ref on the bitmap now, since we made our own copy.
549 entry->releaseRef();
550 fHeapEntry = NULL;
551 } else {
552 SkASSERT(!shouldFlattenBitmaps(flags));
553 SkASSERT(!SkToBool(flags & SkGPipeWriter::kSimultaneousReaders_Flag));
554 fHeapEntry = entry;
555 fBitmap = fHeapEntry->getBitmap();
556 }
scroggo@google.com284bf502012-07-17 16:10:34 +0000557 }
scroggo@google.com565254b2012-06-28 15:41:32 +0000558}
559
reed@google.combb6992a2011-04-26 17:41:56 +0000560static void drawBitmap_rp(SkCanvas* canvas, SkReader32* reader, uint32_t op32,
561 SkGPipeState* state) {
scroggo@google.com284bf502012-07-17 16:10:34 +0000562 BitmapHolder holder(reader, op32, state);
scroggo@google.com460a23e2012-08-16 17:56:49 +0000563 bool hasPaint = SkToBool(DrawOp_unpackFlags(op32) & kDrawBitmap_HasPaint_DrawOpFlag);
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000564 SkScalar left = reader->readScalar();
565 SkScalar top = reader->readScalar();
junov@chromium.orgfb103892012-09-20 19:35:43 +0000566 const SkBitmap* bitmap = holder.getBitmap();
567 if (state->shouldDraw()) {
568 canvas->drawBitmap(*bitmap, left, top, hasPaint ? &state->paint() : NULL);
569 }
reed@google.combb6992a2011-04-26 17:41:56 +0000570}
571
572static void drawBitmapMatrix_rp(SkCanvas* canvas, SkReader32* reader, uint32_t op32,
573 SkGPipeState* state) {
scroggo@google.com58be6822012-07-30 14:40:01 +0000574 BitmapHolder holder(reader, op32, state);
scroggo@google.com460a23e2012-08-16 17:56:49 +0000575 bool hasPaint = SkToBool(DrawOp_unpackFlags(op32) & kDrawBitmap_HasPaint_DrawOpFlag);
scroggo@google.com58be6822012-07-30 14:40:01 +0000576 SkMatrix matrix;
577 reader->readMatrix(&matrix);
junov@chromium.orgfb103892012-09-20 19:35:43 +0000578 const SkBitmap* bitmap = holder.getBitmap();
579 if (state->shouldDraw()) {
580 canvas->drawBitmapMatrix(*bitmap, matrix,
581 hasPaint ? &state->paint() : NULL);
582 }
reed@google.combb6992a2011-04-26 17:41:56 +0000583}
584
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000585static void drawBitmapNine_rp(SkCanvas* canvas, SkReader32* reader,
586 uint32_t op32, SkGPipeState* state) {
scroggo@google.com284bf502012-07-17 16:10:34 +0000587 BitmapHolder holder(reader, op32, state);
scroggo@google.com460a23e2012-08-16 17:56:49 +0000588 bool hasPaint = SkToBool(DrawOp_unpackFlags(op32) & kDrawBitmap_HasPaint_DrawOpFlag);
scroggo@google.com691703c2012-06-05 14:58:40 +0000589 const SkIRect* center = skip<SkIRect>(reader);
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000590 const SkRect* dst = skip<SkRect>(reader);
junov@chromium.orgfb103892012-09-20 19:35:43 +0000591 const SkBitmap* bitmap = holder.getBitmap();
592 if (state->shouldDraw()) {
593 canvas->drawBitmapNine(*bitmap, *center, *dst,
594 hasPaint ? &state->paint() : NULL);
595 }
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000596}
597
598static void drawBitmapRect_rp(SkCanvas* canvas, SkReader32* reader,
599 uint32_t op32, SkGPipeState* state) {
scroggo@google.com284bf502012-07-17 16:10:34 +0000600 BitmapHolder holder(reader, op32, state);
scroggo@google.com58be6822012-07-30 14:40:01 +0000601 unsigned flags = DrawOp_unpackFlags(op32);
scroggo@google.com460a23e2012-08-16 17:56:49 +0000602 bool hasPaint = SkToBool(flags & kDrawBitmap_HasPaint_DrawOpFlag);
603 bool hasSrc = SkToBool(flags & kDrawBitmap_HasSrcRect_DrawOpFlag);
reed@google.com71121732012-09-18 15:14:33 +0000604 const SkRect* src;
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000605 if (hasSrc) {
reed@google.com71121732012-09-18 15:14:33 +0000606 src = skip<SkRect>(reader);
scroggo@google.com691703c2012-06-05 14:58:40 +0000607 } else {
608 src = NULL;
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000609 }
robertphillips@google.com31acc112013-08-20 12:13:48 +0000610 SkCanvas::DrawBitmapRectFlags dbmrFlags = SkCanvas::kNone_DrawBitmapRectFlag;
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +0000611 if (flags & kDrawBitmap_Bleed_DrawOpFlag) {
612 dbmrFlags = (SkCanvas::DrawBitmapRectFlags)(dbmrFlags|SkCanvas::kBleed_DrawBitmapRectFlag);
613 }
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000614 const SkRect* dst = skip<SkRect>(reader);
junov@chromium.orgfb103892012-09-20 19:35:43 +0000615 const SkBitmap* bitmap = holder.getBitmap();
616 if (state->shouldDraw()) {
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000617 canvas->drawBitmapRectToRect(*bitmap, src, *dst,
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +0000618 hasPaint ? &state->paint() : NULL, dbmrFlags);
junov@chromium.orgfb103892012-09-20 19:35:43 +0000619 }
reed@google.combb6992a2011-04-26 17:41:56 +0000620}
621
622static void drawSprite_rp(SkCanvas* canvas, SkReader32* reader, uint32_t op32,
623 SkGPipeState* state) {
scroggo@google.com284bf502012-07-17 16:10:34 +0000624 BitmapHolder holder(reader, op32, state);
scroggo@google.com460a23e2012-08-16 17:56:49 +0000625 bool hasPaint = SkToBool(DrawOp_unpackFlags(op32) & kDrawBitmap_HasPaint_DrawOpFlag);
scroggo@google.com691703c2012-06-05 14:58:40 +0000626 const SkIPoint* point = skip<SkIPoint>(reader);
junov@chromium.orgfb103892012-09-20 19:35:43 +0000627 const SkBitmap* bitmap = holder.getBitmap();
628 if (state->shouldDraw()) {
629 canvas->drawSprite(*bitmap, point->fX, point->fY, hasPaint ? &state->paint() : NULL);
630 }
reed@google.combb6992a2011-04-26 17:41:56 +0000631}
632
633///////////////////////////////////////////////////////////////////////////////
634
635static void drawData_rp(SkCanvas* canvas, SkReader32* reader, uint32_t op32,
636 SkGPipeState* state) {
637 // since we don't have a paint, we can use data for our (small) sizes
638 size_t size = DrawOp_unpackData(op32);
639 if (0 == size) {
640 size = reader->readU32();
641 }
642 const void* data = reader->skip(SkAlign4(size));
junov@chromium.orgfb103892012-09-20 19:35:43 +0000643 if (state->shouldDraw()) {
644 canvas->drawData(data, size);
645 }
reed@google.combb6992a2011-04-26 17:41:56 +0000646}
647
reed@google.combb6992a2011-04-26 17:41:56 +0000648static void drawPicture_rp(SkCanvas* canvas, SkReader32* reader, uint32_t op32,
649 SkGPipeState* state) {
650 UNIMPLEMENTED
651}
652
653///////////////////////////////////////////////////////////////////////////////
654
reed@google.combb6992a2011-04-26 17:41:56 +0000655static void paintOp_rp(SkCanvas*, SkReader32* reader, uint32_t op32,
656 SkGPipeState* state) {
reed@google.comb55d1182011-05-11 00:42:04 +0000657 size_t offset = reader->offset();
658 size_t stop = offset + PaintOp_unpackData(op32);
reed@google.com31891582011-05-12 03:03:56 +0000659 SkPaint* p = state->editPaint();
reed@google.combb6992a2011-04-26 17:41:56 +0000660
661 do {
662 uint32_t p32 = reader->readU32();
663 unsigned op = PaintOp_unpackOp(p32);
664 unsigned data = PaintOp_unpackData(p32);
reed@google.combb6992a2011-04-26 17:41:56 +0000665
reed@google.comb55d1182011-05-11 00:42:04 +0000666// SkDebugf(" read %08X op=%d flags=%d data=%d\n", p32, op, done, data);
reed@google.combb6992a2011-04-26 17:41:56 +0000667
668 switch (op) {
669 case kReset_PaintOp: p->reset(); break;
670 case kFlags_PaintOp: p->setFlags(data); break;
671 case kColor_PaintOp: p->setColor(reader->readU32()); break;
commit-bot@chromium.org85faf502014-04-16 12:58:02 +0000672 case kFilterLevel_PaintOp: p->setFilterLevel((SkPaint::FilterLevel)data); break;
reed@google.combb6992a2011-04-26 17:41:56 +0000673 case kStyle_PaintOp: p->setStyle((SkPaint::Style)data); break;
674 case kJoin_PaintOp: p->setStrokeJoin((SkPaint::Join)data); break;
675 case kCap_PaintOp: p->setStrokeCap((SkPaint::Cap)data); break;
676 case kWidth_PaintOp: p->setStrokeWidth(reader->readScalar()); break;
677 case kMiter_PaintOp: p->setStrokeMiter(reader->readScalar()); break;
678 case kEncoding_PaintOp:
679 p->setTextEncoding((SkPaint::TextEncoding)data);
680 break;
681 case kHinting_PaintOp: p->setHinting((SkPaint::Hinting)data); break;
682 case kAlign_PaintOp: p->setTextAlign((SkPaint::Align)data); break;
683 case kTextSize_PaintOp: p->setTextSize(reader->readScalar()); break;
684 case kTextScaleX_PaintOp: p->setTextScaleX(reader->readScalar()); break;
685 case kTextSkewX_PaintOp: p->setTextSkewX(reader->readScalar()); break;
reed@google.combb6793b2011-05-05 15:18:15 +0000686
reed@google.comb55d1182011-05-11 00:42:04 +0000687 case kFlatIndex_PaintOp: {
688 PaintFlats pf = (PaintFlats)PaintOp_unpackFlags(p32);
689 unsigned index = data;
690 set_paintflat(p, state->getFlat(index), pf);
691 break;
692 }
693
scroggo@google.com3cb969f2012-07-27 20:39:19 +0000694 case kTypeface_PaintOp:
695 SkASSERT(SkToBool(state->getFlags() &
696 SkGPipeWriter::kCrossProcess_Flag));
697 state->setTypeface(p, data); break;
tomhudson@google.com0c00f212011-12-28 14:59:50 +0000698 default: SkDEBUGFAIL("bad paintop"); return;
reed@google.combb6992a2011-04-26 17:41:56 +0000699 }
reed@google.comb55d1182011-05-11 00:42:04 +0000700 SkASSERT(reader->offset() <= stop);
reed@google.com31891582011-05-12 03:03:56 +0000701 } while (reader->offset() < stop);
reed@google.combb6992a2011-04-26 17:41:56 +0000702}
703
scroggo@google.com3cb969f2012-07-27 20:39:19 +0000704static void typeface_rp(SkCanvas*, SkReader32* reader, uint32_t,
705 SkGPipeState* state) {
706 SkASSERT(!SkToBool(state->getFlags() & SkGPipeWriter::kCrossProcess_Flag));
707 SkPaint* p = state->editPaint();
708 p->setTypeface(static_cast<SkTypeface*>(reader->readPtr()));
709}
710
reed@google.com0cd2ac62013-10-14 20:02:44 +0000711static void annotation_rp(SkCanvas*, SkReader32* reader, uint32_t op32,
712 SkGPipeState* state) {
713 SkPaint* p = state->editPaint();
skia.committer@gmail.comfbc58a32013-10-15 07:02:27 +0000714
commit-bot@chromium.org89ff3dd2013-10-29 20:29:38 +0000715 const size_t size = DrawOp_unpackData(op32);
716 if (size > 0) {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000717 SkReadBuffer buffer(reader->skip(size), size);
commit-bot@chromium.orgd9579842014-02-27 11:47:36 +0000718 p->setAnnotation(SkAnnotation::Create(buffer))->unref();
commit-bot@chromium.org89ff3dd2013-10-29 20:29:38 +0000719 SkASSERT(buffer.offset() == size);
reed@google.com0cd2ac62013-10-14 20:02:44 +0000720 } else {
721 p->setAnnotation(NULL);
722 }
723}
724
reed@google.combb6793b2011-05-05 15:18:15 +0000725///////////////////////////////////////////////////////////////////////////////
726
reed@google.com0faac1e2011-05-11 05:58:58 +0000727static void def_Typeface_rp(SkCanvas*, SkReader32*, uint32_t, SkGPipeState* state) {
728 state->addTypeface();
729}
730
reed@google.comb55d1182011-05-11 00:42:04 +0000731static void def_PaintFlat_rp(SkCanvas*, SkReader32*, uint32_t op32,
732 SkGPipeState* state) {
733 PaintFlats pf = (PaintFlats)DrawOp_unpackFlags(op32);
734 unsigned index = DrawOp_unpackData(op32);
735 state->defFlattenable(pf, index);
736}
737
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000738static void def_Bitmap_rp(SkCanvas*, SkReader32*, uint32_t op32,
739 SkGPipeState* state) {
740 unsigned index = DrawOp_unpackData(op32);
741 state->addBitmap(index);
742}
743
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000744static void def_Factory_rp(SkCanvas*, SkReader32* reader, uint32_t,
745 SkGPipeState* state) {
746 state->defFactory(reader->readString());
747}
748
reed@google.combb6992a2011-04-26 17:41:56 +0000749///////////////////////////////////////////////////////////////////////////////
750
reed@google.comacd471f2011-05-03 21:26:46 +0000751static void skip_rp(SkCanvas*, SkReader32* reader, uint32_t op32, SkGPipeState*) {
752 size_t bytes = DrawOp_unpackData(op32);
753 (void)reader->skip(bytes);
754}
755
scroggo@google.com10dccde2012-08-08 20:43:22 +0000756static void reportFlags_rp(SkCanvas*, SkReader32*, uint32_t op32,
scroggo@google.com565254b2012-06-28 15:41:32 +0000757 SkGPipeState* state) {
758 unsigned flags = DrawOp_unpackFlags(op32);
759 state->setFlags(flags);
760}
761
scroggo@google.comd9d29672012-08-14 17:21:34 +0000762static void shareBitmapHeap_rp(SkCanvas*, SkReader32* reader, uint32_t,
scroggo@google.com10dccde2012-08-08 20:43:22 +0000763 SkGPipeState* state) {
764 state->setSharedHeap(static_cast<SkBitmapHeap*>(reader->readPtr()));
765}
766
reed@google.combb6992a2011-04-26 17:41:56 +0000767static void done_rp(SkCanvas*, SkReader32*, uint32_t, SkGPipeState*) {}
768
769typedef void (*ReadProc)(SkCanvas*, SkReader32*, uint32_t op32, SkGPipeState*);
770
771static const ReadProc gReadTable[] = {
reed@google.comacd471f2011-05-03 21:26:46 +0000772 skip_rp,
reed@google.combb6992a2011-04-26 17:41:56 +0000773 clipPath_rp,
774 clipRegion_rp,
775 clipRect_rp,
reed@google.com4ed0fb72012-12-12 20:48:18 +0000776 clipRRect_rp,
reed@google.combb6992a2011-04-26 17:41:56 +0000777 concat_rp,
778 drawBitmap_rp,
779 drawBitmapMatrix_rp,
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000780 drawBitmapNine_rp,
reed@google.combb6992a2011-04-26 17:41:56 +0000781 drawBitmapRect_rp,
782 drawClear_rp,
783 drawData_rp,
commit-bot@chromium.orged9806f2014-02-21 02:32:36 +0000784 drawDRRect_rp,
reed@google.com4ed0fb72012-12-12 20:48:18 +0000785 drawOval_rp,
reed@google.combb6992a2011-04-26 17:41:56 +0000786 drawPaint_rp,
dandov963137b2014-08-07 07:49:53 -0700787 drawPatch_rp,
reed@google.combb6992a2011-04-26 17:41:56 +0000788 drawPath_rp,
789 drawPicture_rp,
790 drawPoints_rp,
791 drawPosText_rp,
792 drawPosTextH_rp,
793 drawRect_rp,
reed@google.com4ed0fb72012-12-12 20:48:18 +0000794 drawRRect_rp,
reed@google.combb6992a2011-04-26 17:41:56 +0000795 drawSprite_rp,
796 drawText_rp,
797 drawTextOnPath_rp,
798 drawVertices_rp,
799 restore_rp,
800 rotate_rp,
801 save_rp,
802 saveLayer_rp,
803 scale_rp,
804 setMatrix_rp,
805 skew_rp,
806 translate_rp,
reed@google.com0faac1e2011-05-11 05:58:58 +0000807
reed@google.combb6992a2011-04-26 17:41:56 +0000808 paintOp_rp,
scroggo@google.com3cb969f2012-07-27 20:39:19 +0000809 typeface_rp,
reed@google.com0cd2ac62013-10-14 20:02:44 +0000810 annotation_rp,
811
reed@google.combb6793b2011-05-05 15:18:15 +0000812 def_Typeface_rp,
reed@google.com0faac1e2011-05-11 05:58:58 +0000813 def_PaintFlat_rp,
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000814 def_Bitmap_rp,
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000815 def_Factory_rp,
reed@google.com0faac1e2011-05-11 05:58:58 +0000816
scroggo@google.com10dccde2012-08-08 20:43:22 +0000817 reportFlags_rp,
scroggo@google.comd9d29672012-08-14 17:21:34 +0000818 shareBitmapHeap_rp,
reed@google.combb6992a2011-04-26 17:41:56 +0000819 done_rp
820};
821
822///////////////////////////////////////////////////////////////////////////////
823
tomhudson@google.com8afae612012-08-14 15:03:35 +0000824SkGPipeState::SkGPipeState()
825 : fReader(0)
junov@chromium.orgfb103892012-09-20 19:35:43 +0000826 , fSilent(false)
tomhudson@google.com8afae612012-08-14 15:03:35 +0000827 , fSharedHeap(NULL)
828 , fFlags(0) {
829
830}
reed@google.combb6992a2011-04-26 17:41:56 +0000831
reed@google.comb55d1182011-05-11 00:42:04 +0000832SkGPipeState::~SkGPipeState() {
reed@google.com6bac9472011-06-21 19:24:00 +0000833 fTypefaces.safeUnrefAll();
834 fFlatArray.safeUnrefAll();
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000835 fBitmaps.deleteAll();
scroggo@google.com10dccde2012-08-08 20:43:22 +0000836 SkSafeUnref(fSharedHeap);
reed@google.combb6992a2011-04-26 17:41:56 +0000837}
838
839///////////////////////////////////////////////////////////////////////////////
840
841#include "SkGPipe.h"
842
scroggo@google.com72c96722012-06-06 21:07:10 +0000843SkGPipeReader::SkGPipeReader() {
844 fCanvas = NULL;
reed@google.combb6992a2011-04-26 17:41:56 +0000845 fState = NULL;
scroggo@google.com74b7ffd2013-04-30 02:32:41 +0000846 fProc = NULL;
reed@google.combb6992a2011-04-26 17:41:56 +0000847}
848
scroggo@google.com72c96722012-06-06 21:07:10 +0000849SkGPipeReader::SkGPipeReader(SkCanvas* target) {
850 fCanvas = NULL;
851 this->setCanvas(target);
852 fState = NULL;
scroggo@google.com74b7ffd2013-04-30 02:32:41 +0000853 fProc = NULL;
scroggo@google.com72c96722012-06-06 21:07:10 +0000854}
855
856void SkGPipeReader::setCanvas(SkCanvas *target) {
857 SkRefCnt_SafeAssign(fCanvas, target);
858}
859
reed@google.combb6992a2011-04-26 17:41:56 +0000860SkGPipeReader::~SkGPipeReader() {
861 SkSafeUnref(fCanvas);
862 delete fState;
863}
864
reed@google.com80b09de2011-05-23 13:54:24 +0000865SkGPipeReader::Status SkGPipeReader::playback(const void* data, size_t length,
junov@chromium.orgfb103892012-09-20 19:35:43 +0000866 uint32_t playbackFlags, size_t* bytesRead) {
reed@google.combb6992a2011-04-26 17:41:56 +0000867 if (NULL == fCanvas) {
868 return kError_Status;
869 }
870
871 if (NULL == fState) {
872 fState = new SkGPipeState;
873 }
874
junov@chromium.orgfb103892012-09-20 19:35:43 +0000875 fState->setSilent(playbackFlags & kSilent_PlaybackFlag);
876
reed@google.com0faac1e2011-05-11 05:58:58 +0000877 SkASSERT(SK_ARRAY_COUNT(gReadTable) == (kDone_DrawOp + 1));
878
reed@google.combb6992a2011-04-26 17:41:56 +0000879 const ReadProc* table = gReadTable;
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000880 SkReadBuffer reader(data, length);
scroggo@google.com74b7ffd2013-04-30 02:32:41 +0000881 reader.setBitmapDecoder(fProc);
reed@google.combb6992a2011-04-26 17:41:56 +0000882 SkCanvas* canvas = fCanvas;
reed@google.com80b09de2011-05-23 13:54:24 +0000883 Status status = kEOF_Status;
reed@google.combb6793b2011-05-05 15:18:15 +0000884
885 fState->setReader(&reader);
reed@google.combb6992a2011-04-26 17:41:56 +0000886 while (!reader.eof()) {
djsollen@google.comc73dd5c2012-08-07 15:54:32 +0000887 uint32_t op32 = reader.readUInt();
reed@google.combb6992a2011-04-26 17:41:56 +0000888 unsigned op = DrawOp_unpackOp(op32);
caryclark@google.com920901d2012-06-06 12:04:11 +0000889 // SkDEBUGCODE(DrawOps drawOp = (DrawOps)op;)
rmistry@google.comd6176b02012-08-23 18:14:13 +0000890
reed@google.combb6992a2011-04-26 17:41:56 +0000891 if (op >= SK_ARRAY_COUNT(gReadTable)) {
892 SkDebugf("---- bad op during GPipeState::playback\n");
reed@google.com80b09de2011-05-23 13:54:24 +0000893 status = kError_Status;
894 break;
reed@google.combb6992a2011-04-26 17:41:56 +0000895 }
896 if (kDone_DrawOp == op) {
reed@google.com80b09de2011-05-23 13:54:24 +0000897 status = kDone_Status;
898 break;
reed@google.combb6992a2011-04-26 17:41:56 +0000899 }
djsollen@google.com2b2ede32012-04-12 13:24:04 +0000900 table[op](canvas, reader.getReader32(), op32, fState);
junov@chromium.orgfb103892012-09-20 19:35:43 +0000901 if ((playbackFlags & kReadAtom_PlaybackFlag) &&
yangsu@google.com1bce0a52011-06-16 21:08:19 +0000902 (table[op] != paintOp_rp &&
903 table[op] != def_Typeface_rp &&
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000904 table[op] != def_PaintFlat_rp &&
905 table[op] != def_Bitmap_rp
yangsu@google.com1bce0a52011-06-16 21:08:19 +0000906 )) {
907 status = kReadAtom_Status;
908 break;
909 }
reed@google.combb6992a2011-04-26 17:41:56 +0000910 }
reed@google.com80b09de2011-05-23 13:54:24 +0000911
912 if (bytesRead) {
reed@google.comac45f002011-05-24 13:50:21 +0000913 *bytesRead = reader.offset();
reed@google.com80b09de2011-05-23 13:54:24 +0000914 }
915 return status;
reed@google.combb6992a2011-04-26 17:41:56 +0000916}