blob: aa108e602eb152c9ac5d1c330306b14bbf302ac7 [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"
fmalita228a6f22014-08-28 13:59:42 -070029#include "SkTextBlob.h"
reed@google.comf5842f72011-05-04 18:30:04 +000030#include "SkTypeface.h"
reed@google.combb6793b2011-05-05 15:18:15 +000031#include "SkXfermode.h"
32
commit-bot@chromium.orgc0b7e102013-10-23 17:06:21 +000033static SkFlattenable::Type paintflat_to_flattype(PaintFlats pf) {
reed@google.com35348222013-10-16 13:05:06 +000034 static const uint8_t gEffectTypesInPaintFlatsOrder[] = {
commit-bot@chromium.orgc0b7e102013-10-23 17:06:21 +000035 SkFlattenable::kSkColorFilter_Type,
36 SkFlattenable::kSkDrawLooper_Type,
37 SkFlattenable::kSkImageFilter_Type,
38 SkFlattenable::kSkMaskFilter_Type,
39 SkFlattenable::kSkPathEffect_Type,
40 SkFlattenable::kSkRasterizer_Type,
41 SkFlattenable::kSkShader_Type,
42 SkFlattenable::kSkXfermode_Type,
reed@google.com35348222013-10-16 13:05:06 +000043 };
skia.committer@gmail.com4677acc2013-10-17 07:02:33 +000044
reed@google.com35348222013-10-16 13:05:06 +000045 SkASSERT((size_t)pf < SK_ARRAY_COUNT(gEffectTypesInPaintFlatsOrder));
commit-bot@chromium.orgc0b7e102013-10-23 17:06:21 +000046 return (SkFlattenable::Type)gEffectTypesInPaintFlatsOrder[pf];
reed@google.com35348222013-10-16 13:05:06 +000047}
48
reed@google.comb55d1182011-05-11 00:42:04 +000049static void set_paintflat(SkPaint* paint, SkFlattenable* obj, unsigned paintFlat) {
50 SkASSERT(paintFlat < kCount_PaintFlats);
51 switch (paintFlat) {
52 case kColorFilter_PaintFlat:
53 paint->setColorFilter((SkColorFilter*)obj);
54 break;
reed@google.com0faac1e2011-05-11 05:58:58 +000055 case kDrawLooper_PaintFlat:
56 paint->setLooper((SkDrawLooper*)obj);
57 break;
reed@google.comb55d1182011-05-11 00:42:04 +000058 case kMaskFilter_PaintFlat:
59 paint->setMaskFilter((SkMaskFilter*)obj);
60 break;
61 case kPathEffect_PaintFlat:
62 paint->setPathEffect((SkPathEffect*)obj);
63 break;
64 case kRasterizer_PaintFlat:
65 paint->setRasterizer((SkRasterizer*)obj);
66 break;
67 case kShader_PaintFlat:
68 paint->setShader((SkShader*)obj);
69 break;
scroggo@google.com16d1d0b2012-05-02 19:09:40 +000070 case kImageFilter_PaintFlat:
71 paint->setImageFilter((SkImageFilter*)obj);
72 break;
reed@google.comb55d1182011-05-11 00:42:04 +000073 case kXfermode_PaintFlat:
74 paint->setXfermode((SkXfermode*)obj);
75 break;
76 default:
tomhudson@google.com0c00f212011-12-28 14:59:50 +000077 SkDEBUGFAIL("never gets here");
reed@google.comb55d1182011-05-11 00:42:04 +000078 }
79}
80
reed@google.combb6793b2011-05-05 15:18:15 +000081template <typename T> class SkRefCntTDArray : public SkTDArray<T> {
82public:
83 ~SkRefCntTDArray() { this->unrefAll(); }
84};
reed@google.combb6992a2011-04-26 17:41:56 +000085
scroggo@google.com7ca24432012-08-14 15:48:43 +000086class SkGPipeState : public SkBitmapHeapReader {
reed@google.combb6992a2011-04-26 17:41:56 +000087public:
88 SkGPipeState();
89 ~SkGPipeState();
reed@google.combb6793b2011-05-05 15:18:15 +000090
junov@chromium.orgfb103892012-09-20 19:35:43 +000091 void setSilent(bool silent) {
92 fSilent = silent;
93 }
94
95 bool shouldDraw() {
96 return !fSilent;
97 }
98
scroggo@google.com565254b2012-06-28 15:41:32 +000099 void setFlags(unsigned flags) {
100 if (fFlags != flags) {
101 fFlags = flags;
102 this->updateReader();
103 }
104 }
105
106 unsigned getFlags() const {
107 return fFlags;
108 }
109
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000110 void setReader(SkReadBuffer* reader) {
reed@google.comdde09562011-05-23 12:21:05 +0000111 fReader = reader;
scroggo@google.com565254b2012-06-28 15:41:32 +0000112 this->updateReader();
reed@google.comdde09562011-05-23 12:21:05 +0000113 }
reed@google.combb6793b2011-05-05 15:18:15 +0000114
reed@google.com31891582011-05-12 03:03:56 +0000115 const SkPaint& paint() const { return fPaint; }
116 SkPaint* editPaint() { return &fPaint; }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000117
reed@google.comb55d1182011-05-11 00:42:04 +0000118 SkFlattenable* getFlat(unsigned index) const {
119 if (0 == index) {
120 return NULL;
121 }
122 return fFlatArray[index - 1];
123 }
124
reed@google.com67908f22011-06-27 14:47:50 +0000125 void defFlattenable(PaintFlats pf, int index) {
scroggo@google.comd3ba5cc2012-07-09 16:05:53 +0000126 index--;
commit-bot@chromium.orgc0b7e102013-10-23 17:06:21 +0000127 SkFlattenable* obj = fReader->readFlattenable(paintflat_to_flattype(pf));
scroggo@google.comd3ba5cc2012-07-09 16:05:53 +0000128 if (fFlatArray.count() == index) {
129 *fFlatArray.append() = obj;
130 } else {
131 SkSafeUnref(fFlatArray[index]);
132 fFlatArray[index] = obj;
133 }
reed@google.comb55d1182011-05-11 00:42:04 +0000134 }
135
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000136 void defFactory(const char* name) {
137 SkFlattenable::Factory factory = SkFlattenable::NameToFactory(name);
138 if (factory) {
139 SkASSERT(fFactoryArray.find(factory) < 0);
140 *fFactoryArray.append() = factory;
141 }
142 }
143
scroggo@google.com74b7ffd2013-04-30 02:32:41 +0000144 /**
145 * Add a bitmap to the array of bitmaps, or replace an existing one.
146 * This is only used when in cross process mode without a shared heap.
147 */
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000148 void addBitmap(int index) {
scroggo@google.com74b7ffd2013-04-30 02:32:41 +0000149 SkASSERT(shouldFlattenBitmaps(fFlags));
scroggo@google.com4dffc592012-07-17 16:49:40 +0000150 SkBitmap* bm;
151 if(fBitmaps.count() == index) {
152 bm = SkNEW(SkBitmap);
153 *fBitmaps.append() = bm;
154 } else {
155 bm = fBitmaps[index];
156 }
scroggo@google.com74b7ffd2013-04-30 02:32:41 +0000157 fReader->readBitmap(bm);
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000158 }
159
scroggo@google.com7ca24432012-08-14 15:48:43 +0000160 /**
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000161 * Override of SkBitmapHeapReader, so that SkReadBuffer can use
scroggo@google.com74b7ffd2013-04-30 02:32:41 +0000162 * these SkBitmaps for bitmap shaders. Used only in cross process mode
163 * without a shared heap.
scroggo@google.com7ca24432012-08-14 15:48:43 +0000164 */
mtklein72c9faa2015-01-09 10:06:39 -0800165 SkBitmap* getBitmap(int32_t index) const SK_OVERRIDE {
scroggo@google.com74b7ffd2013-04-30 02:32:41 +0000166 SkASSERT(shouldFlattenBitmaps(fFlags));
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000167 return fBitmaps[index];
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000168 }
169
scroggo@google.com7ca24432012-08-14 15:48:43 +0000170 /**
171 * Needed to be a non-abstract subclass of SkBitmapHeapReader.
172 */
mtklein72c9faa2015-01-09 10:06:39 -0800173 void releaseRef(int32_t) SK_OVERRIDE {}
scroggo@google.com7ca24432012-08-14 15:48:43 +0000174
scroggo@google.com10dccde2012-08-08 20:43:22 +0000175 void setSharedHeap(SkBitmapHeap* heap) {
176 SkASSERT(!shouldFlattenBitmaps(fFlags) || NULL == heap);
177 SkRefCnt_SafeAssign(fSharedHeap, heap);
scroggo@google.com7ca24432012-08-14 15:48:43 +0000178 this->updateReader();
scroggo@google.com10dccde2012-08-08 20:43:22 +0000179 }
180
scroggo@google.com74b7ffd2013-04-30 02:32:41 +0000181 /**
182 * Access the shared heap. Only used in the case when bitmaps are not
183 * flattened.
184 */
scroggo@google.com10dccde2012-08-08 20:43:22 +0000185 SkBitmapHeap* getSharedHeap() const {
scroggo@google.com74b7ffd2013-04-30 02:32:41 +0000186 SkASSERT(!shouldFlattenBitmaps(fFlags));
scroggo@google.com10dccde2012-08-08 20:43:22 +0000187 return fSharedHeap;
188 }
189
reed@google.combb6793b2011-05-05 15:18:15 +0000190 void addTypeface() {
djsollen@google.comc73dd5c2012-08-07 15:54:32 +0000191 size_t size = fReader->read32();
reed@google.combb6793b2011-05-05 15:18:15 +0000192 const void* data = fReader->skip(SkAlign4(size));
reed@google.comf5842f72011-05-04 18:30:04 +0000193 SkMemoryStream stream(data, size, false);
reed@google.comf5842f72011-05-04 18:30:04 +0000194 *fTypefaces.append() = SkTypeface::Deserialize(&stream);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000195 }
scroggo@google.com10dccde2012-08-08 20:43:22 +0000196
fmalitaacb882c2014-09-16 17:58:34 -0700197 SkTypeface* getTypeface(unsigned id) const {
198 return id ? fTypefaces[id - 1] : NULL;
reed@google.combb6793b2011-05-05 15:18:15 +0000199 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000200
reed@google.comb55d1182011-05-11 00:42:04 +0000201private:
scroggo@google.com565254b2012-06-28 15:41:32 +0000202 void updateReader() {
203 if (NULL == fReader) {
204 return;
205 }
206 bool crossProcess = SkToBool(fFlags & SkGPipeWriter::kCrossProcess_Flag);
207 fReader->setFlags(SkSetClearMask(fReader->getFlags(), crossProcess,
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000208 SkReadBuffer::kCrossProcess_Flag));
scroggo@google.com565254b2012-06-28 15:41:32 +0000209 if (crossProcess) {
210 fReader->setFactoryArray(&fFactoryArray);
211 } else {
212 fReader->setFactoryArray(NULL);
213 }
scroggo@google.com7ca24432012-08-14 15:48:43 +0000214
215 if (shouldFlattenBitmaps(fFlags)) {
216 fReader->setBitmapStorage(this);
217 } else {
218 fReader->setBitmapStorage(fSharedHeap);
219 }
scroggo@google.com565254b2012-06-28 15:41:32 +0000220 }
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000221 SkReadBuffer* fReader;
reed@google.com31891582011-05-12 03:03:56 +0000222 SkPaint fPaint;
223 SkTDArray<SkFlattenable*> fFlatArray;
224 SkTDArray<SkTypeface*> fTypefaces;
reed@google.comdde09562011-05-23 12:21:05 +0000225 SkTDArray<SkFlattenable::Factory> fFactoryArray;
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000226 SkTDArray<SkBitmap*> fBitmaps;
junov@chromium.orgfb103892012-09-20 19:35:43 +0000227 bool fSilent;
scroggo@google.com10dccde2012-08-08 20:43:22 +0000228 // Only used when sharing bitmaps with the writer.
229 SkBitmapHeap* fSharedHeap;
scroggo@google.com565254b2012-06-28 15:41:32 +0000230 unsigned fFlags;
reed@google.combb6992a2011-04-26 17:41:56 +0000231};
232
233///////////////////////////////////////////////////////////////////////////////
234
commit-bot@chromium.orgb45bd1f2014-04-24 18:17:30 +0000235template <typename T> const T* skip(SkReader32* reader, size_t count = 1) {
reed@google.combb6992a2011-04-26 17:41:56 +0000236 size_t size = sizeof(T) * count;
237 SkASSERT(SkAlign4(size) == size);
238 return reinterpret_cast<const T*>(reader->skip(size));
239}
240
commit-bot@chromium.orgb45bd1f2014-04-24 18:17:30 +0000241template <typename T> const T* skipAlign(SkReader32* reader, size_t count = 1) {
reed@google.combb6992a2011-04-26 17:41:56 +0000242 size_t size = SkAlign4(sizeof(T) * count);
243 return reinterpret_cast<const T*>(reader->skip(size));
244}
245
reed@google.combb6992a2011-04-26 17:41:56 +0000246///////////////////////////////////////////////////////////////////////////////
247///////////////////////////////////////////////////////////////////////////////
248
249static void clipPath_rp(SkCanvas* canvas, SkReader32* reader, uint32_t op32,
250 SkGPipeState* state) {
251 SkPath path;
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000252 reader->readPath(&path);
scroggo@google.com460a23e2012-08-16 17:56:49 +0000253 bool doAA = SkToBool(DrawOp_unpackFlags(op32) & kClip_HasAntiAlias_DrawOpFlag);
254 canvas->clipPath(path, (SkRegion::Op)DrawOp_unpackData(op32), doAA);
reed@google.combb6992a2011-04-26 17:41:56 +0000255}
256
257static void clipRegion_rp(SkCanvas* canvas, SkReader32* reader, uint32_t op32,
258 SkGPipeState* state) {
259 SkRegion rgn;
djsollen@google.com2b2ede32012-04-12 13:24:04 +0000260 reader->readRegion(&rgn);
reed@google.combb6992a2011-04-26 17:41:56 +0000261 canvas->clipRegion(rgn, (SkRegion::Op)DrawOp_unpackData(op32));
262}
263
264static void clipRect_rp(SkCanvas* canvas, SkReader32* reader, uint32_t op32,
265 SkGPipeState* state) {
scroggo@google.com691703c2012-06-05 14:58:40 +0000266 const SkRect* rect = skip<SkRect>(reader);
scroggo@google.com460a23e2012-08-16 17:56:49 +0000267 bool doAA = SkToBool(DrawOp_unpackFlags(op32) & kClip_HasAntiAlias_DrawOpFlag);
scroggo@google.com691703c2012-06-05 14:58:40 +0000268 canvas->clipRect(*rect, (SkRegion::Op)DrawOp_unpackData(op32), doAA);
reed@google.combb6992a2011-04-26 17:41:56 +0000269}
270
reed@google.com4ed0fb72012-12-12 20:48:18 +0000271static void clipRRect_rp(SkCanvas* canvas, SkReader32* reader, uint32_t op32,
272 SkGPipeState* state) {
273 SkRRect rrect;
274 reader->readRRect(&rrect);
275 bool doAA = SkToBool(DrawOp_unpackFlags(op32) & kClip_HasAntiAlias_DrawOpFlag);
276 canvas->clipRRect(rrect, (SkRegion::Op)DrawOp_unpackData(op32), doAA);
277}
278
reed@google.combb6992a2011-04-26 17:41:56 +0000279///////////////////////////////////////////////////////////////////////////////
280
281static void setMatrix_rp(SkCanvas* canvas, SkReader32* reader, uint32_t op32,
282 SkGPipeState* state) {
283 SkMatrix matrix;
djsollen@google.com2b2ede32012-04-12 13:24:04 +0000284 reader->readMatrix(&matrix);
reed@google.combb6992a2011-04-26 17:41:56 +0000285 canvas->setMatrix(matrix);
286}
287
288static void concat_rp(SkCanvas* canvas, SkReader32* reader, uint32_t op32,
289 SkGPipeState* state) {
290 SkMatrix matrix;
djsollen@google.com2b2ede32012-04-12 13:24:04 +0000291 reader->readMatrix(&matrix);
reed@google.combb6992a2011-04-26 17:41:56 +0000292 canvas->concat(matrix);
293}
294
295static void scale_rp(SkCanvas* canvas, SkReader32* reader, uint32_t op32,
296 SkGPipeState* state) {
297 const SkScalar* param = skip<SkScalar>(reader, 2);
298 canvas->scale(param[0], param[1]);
299}
300
301static void skew_rp(SkCanvas* canvas, SkReader32* reader, uint32_t op32,
302 SkGPipeState* state) {
303 const SkScalar* param = skip<SkScalar>(reader, 2);
304 canvas->skew(param[0], param[1]);
305}
306
307static void rotate_rp(SkCanvas* canvas, SkReader32* reader, uint32_t op32,
308 SkGPipeState* state) {
309 canvas->rotate(reader->readScalar());
310}
311
312static void translate_rp(SkCanvas* canvas, SkReader32* reader, uint32_t op32,
313 SkGPipeState* state) {
314 const SkScalar* param = skip<SkScalar>(reader, 2);
315 canvas->translate(param[0], param[1]);
316}
317
318///////////////////////////////////////////////////////////////////////////////
319
320static void save_rp(SkCanvas* canvas, SkReader32* reader, uint32_t op32,
321 SkGPipeState* state) {
Florin Malita5f6102d2014-06-30 10:13:28 -0400322 canvas->save();
reed@google.combb6992a2011-04-26 17:41:56 +0000323}
324
325static void saveLayer_rp(SkCanvas* canvas, SkReader32* reader, uint32_t op32,
326 SkGPipeState* state) {
reed@google.comacd471f2011-05-03 21:26:46 +0000327 unsigned flags = DrawOp_unpackFlags(op32);
328 SkCanvas::SaveFlags saveFlags = (SkCanvas::SaveFlags)DrawOp_unpackData(op32);
reed@google.combb6992a2011-04-26 17:41:56 +0000329
330 const SkRect* bounds = NULL;
331 if (flags & kSaveLayer_HasBounds_DrawOpFlag) {
332 bounds = skip<SkRect>(reader);
333 }
334 const SkPaint* paint = NULL;
335 if (flags & kSaveLayer_HasPaint_DrawOpFlag) {
reed@google.com31891582011-05-12 03:03:56 +0000336 paint = &state->paint();
reed@google.combb6992a2011-04-26 17:41:56 +0000337 }
reed@google.comacd471f2011-05-03 21:26:46 +0000338 canvas->saveLayer(bounds, paint, saveFlags);
reed@google.combb6992a2011-04-26 17:41:56 +0000339}
340
341static void restore_rp(SkCanvas* canvas, SkReader32* reader, uint32_t op32,
342 SkGPipeState* state) {
343 canvas->restore();
344}
345
346///////////////////////////////////////////////////////////////////////////////
347
348static void drawClear_rp(SkCanvas* canvas, SkReader32* reader, uint32_t op32,
349 SkGPipeState* state) {
350 SkColor color = 0;
351 if (DrawOp_unpackFlags(op32) & kClear_HasColor_DrawOpFlag) {
352 color = reader->readU32();
353 }
354 canvas->clear(color);
355}
356
357static void drawPaint_rp(SkCanvas* canvas, SkReader32* reader, uint32_t op32,
358 SkGPipeState* state) {
junov@chromium.orgfb103892012-09-20 19:35:43 +0000359 if (state->shouldDraw()) {
360 canvas->drawPaint(state->paint());
361 }
reed@google.combb6992a2011-04-26 17:41:56 +0000362}
363
364static void drawPoints_rp(SkCanvas* canvas, SkReader32* reader, uint32_t op32,
365 SkGPipeState* state) {
366 SkCanvas::PointMode mode = (SkCanvas::PointMode)DrawOp_unpackFlags(op32);
367 size_t count = reader->readU32();
368 const SkPoint* pts = skip<SkPoint>(reader, count);
junov@chromium.orgfb103892012-09-20 19:35:43 +0000369 if (state->shouldDraw()) {
370 canvas->drawPoints(mode, count, pts, state->paint());
371 }
reed@google.combb6992a2011-04-26 17:41:56 +0000372}
373
reed@google.com4ed0fb72012-12-12 20:48:18 +0000374static void drawOval_rp(SkCanvas* canvas, SkReader32* reader, uint32_t op32,
375 SkGPipeState* state) {
376 const SkRect* rect = skip<SkRect>(reader);
377 if (state->shouldDraw()) {
378 canvas->drawOval(*rect, state->paint());
379 }
380}
381
reed@google.combb6992a2011-04-26 17:41:56 +0000382static void drawRect_rp(SkCanvas* canvas, SkReader32* reader, uint32_t op32,
383 SkGPipeState* state) {
junov@chromium.orgfb103892012-09-20 19:35:43 +0000384 const SkRect* rect = skip<SkRect>(reader);
385 if (state->shouldDraw()) {
386 canvas->drawRect(*rect, state->paint());
387 }
reed@google.combb6992a2011-04-26 17:41:56 +0000388}
389
reed@google.com4ed0fb72012-12-12 20:48:18 +0000390static void drawRRect_rp(SkCanvas* canvas, SkReader32* reader, uint32_t op32,
391 SkGPipeState* state) {
392 SkRRect rrect;
393 reader->readRRect(&rrect);
394 if (state->shouldDraw()) {
395 canvas->drawRRect(rrect, state->paint());
396 }
397}
398
commit-bot@chromium.orged9806f2014-02-21 02:32:36 +0000399static void drawDRRect_rp(SkCanvas* canvas, SkReader32* reader, uint32_t op32,
400 SkGPipeState* state) {
401 SkRRect outer, inner;
402 reader->readRRect(&outer);
403 reader->readRRect(&inner);
404 if (state->shouldDraw()) {
405 canvas->drawDRRect(outer, inner, state->paint());
406 }
407}
408
dandov963137b2014-08-07 07:49:53 -0700409static void drawPatch_rp(SkCanvas* canvas, SkReader32* reader, uint32_t op32,
410 SkGPipeState* state) {
piotaixr2bf8d092014-11-04 13:21:41 -0800411
dandovb3c9d1c2014-08-12 08:34:29 -0700412 unsigned flags = DrawOp_unpackFlags(op32);
piotaixr2bf8d092014-11-04 13:21:41 -0800413
dandovb3c9d1c2014-08-12 08:34:29 -0700414 const SkPoint* cubics = skip<SkPoint>(reader, SkPatchUtils::kNumCtrlPts);
piotaixr2bf8d092014-11-04 13:21:41 -0800415
dandovb3c9d1c2014-08-12 08:34:29 -0700416 const SkColor* colors = NULL;
417 if (flags & kDrawVertices_HasColors_DrawOpFlag) {
418 colors = skip<SkColor>(reader, SkPatchUtils::kNumCorners);
419 }
420 const SkPoint* texCoords = NULL;
421 if (flags & kDrawVertices_HasTexs_DrawOpFlag) {
422 texCoords = skip<SkPoint>(reader, SkPatchUtils::kNumCorners);
423 }
424 SkAutoTUnref<SkXfermode> xfer;
425 if (flags & kDrawVertices_HasXfermode_DrawOpFlag) {
426 int mode = reader->readInt();
427 if (mode < 0 || mode > SkXfermode::kLastMode) {
428 mode = SkXfermode::kModulate_Mode;
429 }
430 xfer.reset(SkXfermode::Create((SkXfermode::Mode)mode));
431 }
dandov963137b2014-08-07 07:49:53 -0700432 if (state->shouldDraw()) {
dandovb3c9d1c2014-08-12 08:34:29 -0700433 canvas->drawPatch(cubics, colors, texCoords, xfer, state->paint());
dandov963137b2014-08-07 07:49:53 -0700434 }
435}
436
reed@google.combb6992a2011-04-26 17:41:56 +0000437static void drawPath_rp(SkCanvas* canvas, SkReader32* reader, uint32_t op32,
438 SkGPipeState* state) {
439 SkPath path;
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000440 reader->readPath(&path);
junov@chromium.orgfb103892012-09-20 19:35:43 +0000441 if (state->shouldDraw()) {
442 canvas->drawPath(path, state->paint());
443 }
reed@google.combb6992a2011-04-26 17:41:56 +0000444}
445
446static void drawVertices_rp(SkCanvas* canvas, SkReader32* reader, uint32_t op32,
447 SkGPipeState* state) {
448 unsigned flags = DrawOp_unpackFlags(op32);
449
reed@google.com85e143c2013-12-30 15:51:25 +0000450 SkCanvas::VertexMode vmode = (SkCanvas::VertexMode)reader->readU32();
reed@google.combb6992a2011-04-26 17:41:56 +0000451 int vertexCount = reader->readU32();
452 const SkPoint* verts = skip<SkPoint>(reader, vertexCount);
453
454 const SkPoint* texs = NULL;
455 if (flags & kDrawVertices_HasTexs_DrawOpFlag) {
456 texs = skip<SkPoint>(reader, vertexCount);
457 }
458
459 const SkColor* colors = NULL;
460 if (flags & kDrawVertices_HasColors_DrawOpFlag) {
461 colors = skip<SkColor>(reader, vertexCount);
462 }
463
reed@google.com85e143c2013-12-30 15:51:25 +0000464 SkAutoTUnref<SkXfermode> xfer;
465 if (flags & kDrawVertices_HasXfermode_DrawOpFlag) {
466 SkXfermode::Mode mode = (SkXfermode::Mode)reader->readU32();
467 xfer.reset(SkXfermode::Create(mode));
468 }
reed@google.combb6992a2011-04-26 17:41:56 +0000469
470 int indexCount = 0;
471 const uint16_t* indices = NULL;
472 if (flags & kDrawVertices_HasIndices_DrawOpFlag) {
473 indexCount = reader->readU32();
474 indices = skipAlign<uint16_t>(reader, indexCount);
475 }
junov@chromium.orgfb103892012-09-20 19:35:43 +0000476 if (state->shouldDraw()) {
reed@google.com85e143c2013-12-30 15:51:25 +0000477 canvas->drawVertices(vmode, vertexCount, verts, texs, colors, xfer,
junov@chromium.orgfb103892012-09-20 19:35:43 +0000478 indices, indexCount, state->paint());
479 }
reed@google.combb6992a2011-04-26 17:41:56 +0000480}
481
482///////////////////////////////////////////////////////////////////////////////
483
484static void drawText_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 const SkScalar* xy = skip<SkScalar>(reader, 2);
junov@chromium.orgfb103892012-09-20 19:35:43 +0000489 if (state->shouldDraw()) {
490 canvas->drawText(text, len, xy[0], xy[1], state->paint());
491 }
reed@google.combb6992a2011-04-26 17:41:56 +0000492}
493
494static void drawPosText_rp(SkCanvas* canvas, SkReader32* reader, uint32_t op32,
495 SkGPipeState* state) {
496 size_t len = reader->readU32();
497 const void* text = reader->skip(SkAlign4(len));
498 size_t posCount = reader->readU32(); // compute by our writer
499 const SkPoint* pos = skip<SkPoint>(reader, posCount);
junov@chromium.orgfb103892012-09-20 19:35:43 +0000500 if (state->shouldDraw()) {
501 canvas->drawPosText(text, len, pos, state->paint());
502 }
reed@google.combb6992a2011-04-26 17:41:56 +0000503}
504
505static void drawPosTextH_rp(SkCanvas* canvas, SkReader32* reader, uint32_t op32,
506 SkGPipeState* state) {
507 size_t len = reader->readU32();
508 const void* text = reader->skip(SkAlign4(len));
509 size_t posCount = reader->readU32(); // compute by our writer
510 const SkScalar* xpos = skip<SkScalar>(reader, posCount);
511 SkScalar constY = reader->readScalar();
junov@chromium.orgfb103892012-09-20 19:35:43 +0000512 if (state->shouldDraw()) {
513 canvas->drawPosTextH(text, len, xpos, constY, state->paint());
514 }
reed@google.combb6992a2011-04-26 17:41:56 +0000515}
516
517static void drawTextOnPath_rp(SkCanvas* canvas, SkReader32* reader, uint32_t op32,
518 SkGPipeState* state) {
519 size_t len = reader->readU32();
520 const void* text = reader->skip(SkAlign4(len));
521
522 SkPath path;
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000523 reader->readPath(&path);
reed@google.combb6992a2011-04-26 17:41:56 +0000524
525 SkMatrix matrixStorage;
526 const SkMatrix* matrix = NULL;
527 if (DrawOp_unpackFlags(op32) & kDrawTextOnPath_HasMatrix_DrawOpFlag) {
djsollen@google.com2b2ede32012-04-12 13:24:04 +0000528 reader->readMatrix(&matrixStorage);
reed@google.combb6992a2011-04-26 17:41:56 +0000529 matrix = &matrixStorage;
530 }
junov@chromium.orgfb103892012-09-20 19:35:43 +0000531 if (state->shouldDraw()) {
532 canvas->drawTextOnPath(text, len, path, matrix, state->paint());
533 }
reed@google.combb6992a2011-04-26 17:41:56 +0000534}
535
536///////////////////////////////////////////////////////////////////////////////
537
scroggo@google.com284bf502012-07-17 16:10:34 +0000538class BitmapHolder : SkNoncopyable {
539public:
540 BitmapHolder(SkReader32* reader, uint32_t op32, SkGPipeState* state);
541 ~BitmapHolder() {
scroggo@google.com10dccde2012-08-08 20:43:22 +0000542 if (fHeapEntry != NULL) {
543 fHeapEntry->releaseRef();
scroggo@google.com284bf502012-07-17 16:10:34 +0000544 }
scroggo@google.com565254b2012-06-28 15:41:32 +0000545 }
scroggo@google.com284bf502012-07-17 16:10:34 +0000546 const SkBitmap* getBitmap() {
547 return fBitmap;
548 }
549private:
scroggo@google.com10dccde2012-08-08 20:43:22 +0000550 SkBitmapHeapEntry* fHeapEntry;
551 const SkBitmap* fBitmap;
scroggo@google.com58b4ead2012-08-31 16:15:22 +0000552 SkBitmap fBitmapStorage;
scroggo@google.com284bf502012-07-17 16:10:34 +0000553};
554
555BitmapHolder::BitmapHolder(SkReader32* reader, uint32_t op32,
556 SkGPipeState* state) {
scroggo@google.com58b4ead2012-08-31 16:15:22 +0000557 const unsigned flags = state->getFlags();
558 const unsigned index = DrawOp_unpackData(op32);
559 if (shouldFlattenBitmaps(flags)) {
scroggo@google.com10dccde2012-08-08 20:43:22 +0000560 fHeapEntry = NULL;
561 fBitmap = state->getBitmap(index);
scroggo@google.com284bf502012-07-17 16:10:34 +0000562 } else {
scroggo@google.com58b4ead2012-08-31 16:15:22 +0000563 SkBitmapHeapEntry* entry = state->getSharedHeap()->getEntry(index);
564 if (SkToBool(flags & SkGPipeWriter::kSimultaneousReaders_Flag)) {
565 // Make a shallow copy for thread safety. Each thread will point to the same SkPixelRef,
566 // which is thread safe.
567 fBitmapStorage = *entry->getBitmap();
568 fBitmap = &fBitmapStorage;
569 // Release the ref on the bitmap now, since we made our own copy.
570 entry->releaseRef();
571 fHeapEntry = NULL;
572 } else {
573 SkASSERT(!shouldFlattenBitmaps(flags));
574 SkASSERT(!SkToBool(flags & SkGPipeWriter::kSimultaneousReaders_Flag));
575 fHeapEntry = entry;
576 fBitmap = fHeapEntry->getBitmap();
577 }
scroggo@google.com284bf502012-07-17 16:10:34 +0000578 }
scroggo@google.com565254b2012-06-28 15:41:32 +0000579}
580
reed@google.combb6992a2011-04-26 17:41:56 +0000581static void drawBitmap_rp(SkCanvas* canvas, SkReader32* reader, uint32_t op32,
582 SkGPipeState* state) {
scroggo@google.com284bf502012-07-17 16:10:34 +0000583 BitmapHolder holder(reader, op32, state);
scroggo@google.com460a23e2012-08-16 17:56:49 +0000584 bool hasPaint = SkToBool(DrawOp_unpackFlags(op32) & kDrawBitmap_HasPaint_DrawOpFlag);
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000585 SkScalar left = reader->readScalar();
586 SkScalar top = reader->readScalar();
junov@chromium.orgfb103892012-09-20 19:35:43 +0000587 const SkBitmap* bitmap = holder.getBitmap();
588 if (state->shouldDraw()) {
589 canvas->drawBitmap(*bitmap, left, top, hasPaint ? &state->paint() : NULL);
590 }
reed@google.combb6992a2011-04-26 17:41:56 +0000591}
592
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000593static void drawBitmapNine_rp(SkCanvas* canvas, SkReader32* reader,
594 uint32_t op32, SkGPipeState* state) {
scroggo@google.com284bf502012-07-17 16:10:34 +0000595 BitmapHolder holder(reader, op32, state);
scroggo@google.com460a23e2012-08-16 17:56:49 +0000596 bool hasPaint = SkToBool(DrawOp_unpackFlags(op32) & kDrawBitmap_HasPaint_DrawOpFlag);
scroggo@google.com691703c2012-06-05 14:58:40 +0000597 const SkIRect* center = skip<SkIRect>(reader);
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000598 const SkRect* dst = skip<SkRect>(reader);
junov@chromium.orgfb103892012-09-20 19:35:43 +0000599 const SkBitmap* bitmap = holder.getBitmap();
600 if (state->shouldDraw()) {
601 canvas->drawBitmapNine(*bitmap, *center, *dst,
602 hasPaint ? &state->paint() : NULL);
603 }
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000604}
605
606static void drawBitmapRect_rp(SkCanvas* canvas, SkReader32* reader,
607 uint32_t op32, SkGPipeState* state) {
scroggo@google.com284bf502012-07-17 16:10:34 +0000608 BitmapHolder holder(reader, op32, state);
scroggo@google.com58be6822012-07-30 14:40:01 +0000609 unsigned flags = DrawOp_unpackFlags(op32);
scroggo@google.com460a23e2012-08-16 17:56:49 +0000610 bool hasPaint = SkToBool(flags & kDrawBitmap_HasPaint_DrawOpFlag);
611 bool hasSrc = SkToBool(flags & kDrawBitmap_HasSrcRect_DrawOpFlag);
reed@google.com71121732012-09-18 15:14:33 +0000612 const SkRect* src;
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000613 if (hasSrc) {
reed@google.com71121732012-09-18 15:14:33 +0000614 src = skip<SkRect>(reader);
scroggo@google.com691703c2012-06-05 14:58:40 +0000615 } else {
616 src = NULL;
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000617 }
robertphillips@google.com31acc112013-08-20 12:13:48 +0000618 SkCanvas::DrawBitmapRectFlags dbmrFlags = SkCanvas::kNone_DrawBitmapRectFlag;
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +0000619 if (flags & kDrawBitmap_Bleed_DrawOpFlag) {
620 dbmrFlags = (SkCanvas::DrawBitmapRectFlags)(dbmrFlags|SkCanvas::kBleed_DrawBitmapRectFlag);
621 }
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000622 const SkRect* dst = skip<SkRect>(reader);
junov@chromium.orgfb103892012-09-20 19:35:43 +0000623 const SkBitmap* bitmap = holder.getBitmap();
624 if (state->shouldDraw()) {
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000625 canvas->drawBitmapRectToRect(*bitmap, src, *dst,
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +0000626 hasPaint ? &state->paint() : NULL, dbmrFlags);
junov@chromium.orgfb103892012-09-20 19:35:43 +0000627 }
reed@google.combb6992a2011-04-26 17:41:56 +0000628}
629
630static void drawSprite_rp(SkCanvas* canvas, SkReader32* reader, uint32_t op32,
631 SkGPipeState* state) {
scroggo@google.com284bf502012-07-17 16:10:34 +0000632 BitmapHolder holder(reader, op32, state);
scroggo@google.com460a23e2012-08-16 17:56:49 +0000633 bool hasPaint = SkToBool(DrawOp_unpackFlags(op32) & kDrawBitmap_HasPaint_DrawOpFlag);
scroggo@google.com691703c2012-06-05 14:58:40 +0000634 const SkIPoint* point = skip<SkIPoint>(reader);
junov@chromium.orgfb103892012-09-20 19:35:43 +0000635 const SkBitmap* bitmap = holder.getBitmap();
636 if (state->shouldDraw()) {
637 canvas->drawSprite(*bitmap, point->fX, point->fY, hasPaint ? &state->paint() : NULL);
638 }
reed@google.combb6992a2011-04-26 17:41:56 +0000639}
640
641///////////////////////////////////////////////////////////////////////////////
642
reed@google.combb6992a2011-04-26 17:41:56 +0000643static void drawPicture_rp(SkCanvas* canvas, SkReader32* reader, uint32_t op32,
644 SkGPipeState* state) {
645 UNIMPLEMENTED
646}
647
fmalitab7425172014-08-26 07:56:44 -0700648static void drawTextBlob_rp(SkCanvas* canvas, SkReader32* reader, uint32_t op32,
649 SkGPipeState* state) {
fmalita228a6f22014-08-28 13:59:42 -0700650 SkScalar x = reader->readScalar();
651 SkScalar y = reader->readScalar();
652
fmalitaacb882c2014-09-16 17:58:34 -0700653 int typefaceCount = reader->readU32();
654 SkAutoSTMalloc<16, SkTypeface*> typefaceArray(typefaceCount);
655 if (state->getFlags() & SkGPipeWriter::kCrossProcess_Flag) {
656 for (int i = 0; i < typefaceCount; ++i) {
657 typefaceArray[i] = state->getTypeface(reader->readU32());
658 }
659 } else {
660 reader->read(typefaceArray.get(), typefaceCount * sizeof(SkTypeface*));
661 }
662
fmalita228a6f22014-08-28 13:59:42 -0700663 size_t blobSize = reader->readU32();
664 const void* data = reader->skip(SkAlign4(blobSize));
665
666 if (state->shouldDraw()) {
667 SkReadBuffer blobBuffer(data, blobSize);
fmalitaacb882c2014-09-16 17:58:34 -0700668 blobBuffer.setTypefaceArray(typefaceArray.get(), typefaceCount);
fmalita228a6f22014-08-28 13:59:42 -0700669 SkAutoTUnref<const SkTextBlob> blob(SkTextBlob::CreateFromBuffer(blobBuffer));
670 SkASSERT(blob.get());
671
672 canvas->drawTextBlob(blob, x, y, state->paint());
673 }
fmalitab7425172014-08-26 07:56:44 -0700674}
reed@google.combb6992a2011-04-26 17:41:56 +0000675///////////////////////////////////////////////////////////////////////////////
676
reed@google.combb6992a2011-04-26 17:41:56 +0000677static void paintOp_rp(SkCanvas*, SkReader32* reader, uint32_t op32,
678 SkGPipeState* state) {
reed@google.comb55d1182011-05-11 00:42:04 +0000679 size_t offset = reader->offset();
680 size_t stop = offset + PaintOp_unpackData(op32);
reed@google.com31891582011-05-12 03:03:56 +0000681 SkPaint* p = state->editPaint();
reed@google.combb6992a2011-04-26 17:41:56 +0000682
683 do {
684 uint32_t p32 = reader->readU32();
685 unsigned op = PaintOp_unpackOp(p32);
686 unsigned data = PaintOp_unpackData(p32);
reed@google.combb6992a2011-04-26 17:41:56 +0000687
reed@google.comb55d1182011-05-11 00:42:04 +0000688// SkDebugf(" read %08X op=%d flags=%d data=%d\n", p32, op, done, data);
reed@google.combb6992a2011-04-26 17:41:56 +0000689
690 switch (op) {
691 case kReset_PaintOp: p->reset(); break;
692 case kFlags_PaintOp: p->setFlags(data); break;
693 case kColor_PaintOp: p->setColor(reader->readU32()); break;
commit-bot@chromium.org85faf502014-04-16 12:58:02 +0000694 case kFilterLevel_PaintOp: p->setFilterLevel((SkPaint::FilterLevel)data); break;
reed@google.combb6992a2011-04-26 17:41:56 +0000695 case kStyle_PaintOp: p->setStyle((SkPaint::Style)data); break;
696 case kJoin_PaintOp: p->setStrokeJoin((SkPaint::Join)data); break;
697 case kCap_PaintOp: p->setStrokeCap((SkPaint::Cap)data); break;
698 case kWidth_PaintOp: p->setStrokeWidth(reader->readScalar()); break;
699 case kMiter_PaintOp: p->setStrokeMiter(reader->readScalar()); break;
700 case kEncoding_PaintOp:
701 p->setTextEncoding((SkPaint::TextEncoding)data);
702 break;
703 case kHinting_PaintOp: p->setHinting((SkPaint::Hinting)data); break;
704 case kAlign_PaintOp: p->setTextAlign((SkPaint::Align)data); break;
705 case kTextSize_PaintOp: p->setTextSize(reader->readScalar()); break;
706 case kTextScaleX_PaintOp: p->setTextScaleX(reader->readScalar()); break;
707 case kTextSkewX_PaintOp: p->setTextSkewX(reader->readScalar()); break;
reed@google.combb6793b2011-05-05 15:18:15 +0000708
reed@google.comb55d1182011-05-11 00:42:04 +0000709 case kFlatIndex_PaintOp: {
710 PaintFlats pf = (PaintFlats)PaintOp_unpackFlags(p32);
711 unsigned index = data;
712 set_paintflat(p, state->getFlat(index), pf);
713 break;
714 }
715
scroggo@google.com3cb969f2012-07-27 20:39:19 +0000716 case kTypeface_PaintOp:
717 SkASSERT(SkToBool(state->getFlags() &
718 SkGPipeWriter::kCrossProcess_Flag));
fmalitaacb882c2014-09-16 17:58:34 -0700719 p->setTypeface(state->getTypeface(data));
720 break;
tomhudson@google.com0c00f212011-12-28 14:59:50 +0000721 default: SkDEBUGFAIL("bad paintop"); return;
reed@google.combb6992a2011-04-26 17:41:56 +0000722 }
reed@google.comb55d1182011-05-11 00:42:04 +0000723 SkASSERT(reader->offset() <= stop);
reed@google.com31891582011-05-12 03:03:56 +0000724 } while (reader->offset() < stop);
reed@google.combb6992a2011-04-26 17:41:56 +0000725}
726
scroggo@google.com3cb969f2012-07-27 20:39:19 +0000727static void typeface_rp(SkCanvas*, SkReader32* reader, uint32_t,
728 SkGPipeState* state) {
729 SkASSERT(!SkToBool(state->getFlags() & SkGPipeWriter::kCrossProcess_Flag));
730 SkPaint* p = state->editPaint();
731 p->setTypeface(static_cast<SkTypeface*>(reader->readPtr()));
732}
733
reed@google.com0cd2ac62013-10-14 20:02:44 +0000734static void annotation_rp(SkCanvas*, SkReader32* reader, uint32_t op32,
735 SkGPipeState* state) {
736 SkPaint* p = state->editPaint();
skia.committer@gmail.comfbc58a32013-10-15 07:02:27 +0000737
commit-bot@chromium.org89ff3dd2013-10-29 20:29:38 +0000738 const size_t size = DrawOp_unpackData(op32);
739 if (size > 0) {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000740 SkReadBuffer buffer(reader->skip(size), size);
commit-bot@chromium.orgd9579842014-02-27 11:47:36 +0000741 p->setAnnotation(SkAnnotation::Create(buffer))->unref();
commit-bot@chromium.org89ff3dd2013-10-29 20:29:38 +0000742 SkASSERT(buffer.offset() == size);
reed@google.com0cd2ac62013-10-14 20:02:44 +0000743 } else {
744 p->setAnnotation(NULL);
745 }
746}
747
reed@google.combb6793b2011-05-05 15:18:15 +0000748///////////////////////////////////////////////////////////////////////////////
749
reed@google.com0faac1e2011-05-11 05:58:58 +0000750static void def_Typeface_rp(SkCanvas*, SkReader32*, uint32_t, SkGPipeState* state) {
751 state->addTypeface();
752}
753
reed@google.comb55d1182011-05-11 00:42:04 +0000754static void def_PaintFlat_rp(SkCanvas*, SkReader32*, uint32_t op32,
755 SkGPipeState* state) {
756 PaintFlats pf = (PaintFlats)DrawOp_unpackFlags(op32);
757 unsigned index = DrawOp_unpackData(op32);
758 state->defFlattenable(pf, index);
759}
760
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000761static void def_Bitmap_rp(SkCanvas*, SkReader32*, uint32_t op32,
762 SkGPipeState* state) {
763 unsigned index = DrawOp_unpackData(op32);
764 state->addBitmap(index);
765}
766
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000767static void def_Factory_rp(SkCanvas*, SkReader32* reader, uint32_t,
768 SkGPipeState* state) {
769 state->defFactory(reader->readString());
770}
771
reed@google.combb6992a2011-04-26 17:41:56 +0000772///////////////////////////////////////////////////////////////////////////////
773
reed@google.comacd471f2011-05-03 21:26:46 +0000774static void skip_rp(SkCanvas*, SkReader32* reader, uint32_t op32, SkGPipeState*) {
775 size_t bytes = DrawOp_unpackData(op32);
776 (void)reader->skip(bytes);
777}
778
scroggo@google.com10dccde2012-08-08 20:43:22 +0000779static void reportFlags_rp(SkCanvas*, SkReader32*, uint32_t op32,
scroggo@google.com565254b2012-06-28 15:41:32 +0000780 SkGPipeState* state) {
781 unsigned flags = DrawOp_unpackFlags(op32);
782 state->setFlags(flags);
783}
784
scroggo@google.comd9d29672012-08-14 17:21:34 +0000785static void shareBitmapHeap_rp(SkCanvas*, SkReader32* reader, uint32_t,
scroggo@google.com10dccde2012-08-08 20:43:22 +0000786 SkGPipeState* state) {
787 state->setSharedHeap(static_cast<SkBitmapHeap*>(reader->readPtr()));
788}
789
reed@google.combb6992a2011-04-26 17:41:56 +0000790static void done_rp(SkCanvas*, SkReader32*, uint32_t, SkGPipeState*) {}
791
792typedef void (*ReadProc)(SkCanvas*, SkReader32*, uint32_t op32, SkGPipeState*);
793
794static const ReadProc gReadTable[] = {
reed@google.comacd471f2011-05-03 21:26:46 +0000795 skip_rp,
reed@google.combb6992a2011-04-26 17:41:56 +0000796 clipPath_rp,
797 clipRegion_rp,
798 clipRect_rp,
reed@google.com4ed0fb72012-12-12 20:48:18 +0000799 clipRRect_rp,
reed@google.combb6992a2011-04-26 17:41:56 +0000800 concat_rp,
801 drawBitmap_rp,
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000802 drawBitmapNine_rp,
reed@google.combb6992a2011-04-26 17:41:56 +0000803 drawBitmapRect_rp,
804 drawClear_rp,
commit-bot@chromium.orged9806f2014-02-21 02:32:36 +0000805 drawDRRect_rp,
reed@google.com4ed0fb72012-12-12 20:48:18 +0000806 drawOval_rp,
reed@google.combb6992a2011-04-26 17:41:56 +0000807 drawPaint_rp,
dandov963137b2014-08-07 07:49:53 -0700808 drawPatch_rp,
reed@google.combb6992a2011-04-26 17:41:56 +0000809 drawPath_rp,
810 drawPicture_rp,
811 drawPoints_rp,
812 drawPosText_rp,
813 drawPosTextH_rp,
814 drawRect_rp,
reed@google.com4ed0fb72012-12-12 20:48:18 +0000815 drawRRect_rp,
reed@google.combb6992a2011-04-26 17:41:56 +0000816 drawSprite_rp,
817 drawText_rp,
fmalitab7425172014-08-26 07:56:44 -0700818 drawTextBlob_rp,
reed@google.combb6992a2011-04-26 17:41:56 +0000819 drawTextOnPath_rp,
820 drawVertices_rp,
821 restore_rp,
822 rotate_rp,
823 save_rp,
824 saveLayer_rp,
825 scale_rp,
826 setMatrix_rp,
827 skew_rp,
828 translate_rp,
reed@google.com0faac1e2011-05-11 05:58:58 +0000829
reed@google.combb6992a2011-04-26 17:41:56 +0000830 paintOp_rp,
scroggo@google.com3cb969f2012-07-27 20:39:19 +0000831 typeface_rp,
reed@google.com0cd2ac62013-10-14 20:02:44 +0000832 annotation_rp,
833
reed@google.combb6793b2011-05-05 15:18:15 +0000834 def_Typeface_rp,
reed@google.com0faac1e2011-05-11 05:58:58 +0000835 def_PaintFlat_rp,
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000836 def_Bitmap_rp,
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000837 def_Factory_rp,
reed@google.com0faac1e2011-05-11 05:58:58 +0000838
scroggo@google.com10dccde2012-08-08 20:43:22 +0000839 reportFlags_rp,
scroggo@google.comd9d29672012-08-14 17:21:34 +0000840 shareBitmapHeap_rp,
reed@google.combb6992a2011-04-26 17:41:56 +0000841 done_rp
842};
843
844///////////////////////////////////////////////////////////////////////////////
845
tomhudson@google.com8afae612012-08-14 15:03:35 +0000846SkGPipeState::SkGPipeState()
847 : fReader(0)
junov@chromium.orgfb103892012-09-20 19:35:43 +0000848 , fSilent(false)
tomhudson@google.com8afae612012-08-14 15:03:35 +0000849 , fSharedHeap(NULL)
850 , fFlags(0) {
851
852}
reed@google.combb6992a2011-04-26 17:41:56 +0000853
reed@google.comb55d1182011-05-11 00:42:04 +0000854SkGPipeState::~SkGPipeState() {
reed@google.com6bac9472011-06-21 19:24:00 +0000855 fTypefaces.safeUnrefAll();
856 fFlatArray.safeUnrefAll();
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000857 fBitmaps.deleteAll();
scroggo@google.com10dccde2012-08-08 20:43:22 +0000858 SkSafeUnref(fSharedHeap);
reed@google.combb6992a2011-04-26 17:41:56 +0000859}
860
861///////////////////////////////////////////////////////////////////////////////
862
863#include "SkGPipe.h"
864
scroggo@google.com72c96722012-06-06 21:07:10 +0000865SkGPipeReader::SkGPipeReader() {
866 fCanvas = NULL;
reed@google.combb6992a2011-04-26 17:41:56 +0000867 fState = NULL;
scroggo@google.com74b7ffd2013-04-30 02:32:41 +0000868 fProc = NULL;
reed@google.combb6992a2011-04-26 17:41:56 +0000869}
870
scroggo@google.com72c96722012-06-06 21:07:10 +0000871SkGPipeReader::SkGPipeReader(SkCanvas* target) {
872 fCanvas = NULL;
873 this->setCanvas(target);
874 fState = NULL;
scroggo@google.com74b7ffd2013-04-30 02:32:41 +0000875 fProc = NULL;
scroggo@google.com72c96722012-06-06 21:07:10 +0000876}
877
878void SkGPipeReader::setCanvas(SkCanvas *target) {
879 SkRefCnt_SafeAssign(fCanvas, target);
880}
881
reed@google.combb6992a2011-04-26 17:41:56 +0000882SkGPipeReader::~SkGPipeReader() {
883 SkSafeUnref(fCanvas);
884 delete fState;
885}
886
reed@google.com80b09de2011-05-23 13:54:24 +0000887SkGPipeReader::Status SkGPipeReader::playback(const void* data, size_t length,
junov@chromium.orgfb103892012-09-20 19:35:43 +0000888 uint32_t playbackFlags, size_t* bytesRead) {
reed@google.combb6992a2011-04-26 17:41:56 +0000889 if (NULL == fCanvas) {
890 return kError_Status;
891 }
892
893 if (NULL == fState) {
894 fState = new SkGPipeState;
895 }
896
junov@chromium.orgfb103892012-09-20 19:35:43 +0000897 fState->setSilent(playbackFlags & kSilent_PlaybackFlag);
898
reed@google.com0faac1e2011-05-11 05:58:58 +0000899 SkASSERT(SK_ARRAY_COUNT(gReadTable) == (kDone_DrawOp + 1));
900
reed@google.combb6992a2011-04-26 17:41:56 +0000901 const ReadProc* table = gReadTable;
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000902 SkReadBuffer reader(data, length);
scroggo@google.com74b7ffd2013-04-30 02:32:41 +0000903 reader.setBitmapDecoder(fProc);
reed@google.combb6992a2011-04-26 17:41:56 +0000904 SkCanvas* canvas = fCanvas;
reed@google.com80b09de2011-05-23 13:54:24 +0000905 Status status = kEOF_Status;
reed@google.combb6793b2011-05-05 15:18:15 +0000906
907 fState->setReader(&reader);
reed@google.combb6992a2011-04-26 17:41:56 +0000908 while (!reader.eof()) {
djsollen@google.comc73dd5c2012-08-07 15:54:32 +0000909 uint32_t op32 = reader.readUInt();
reed@google.combb6992a2011-04-26 17:41:56 +0000910 unsigned op = DrawOp_unpackOp(op32);
caryclark@google.com920901d2012-06-06 12:04:11 +0000911 // SkDEBUGCODE(DrawOps drawOp = (DrawOps)op;)
rmistry@google.comd6176b02012-08-23 18:14:13 +0000912
reed@google.combb6992a2011-04-26 17:41:56 +0000913 if (op >= SK_ARRAY_COUNT(gReadTable)) {
914 SkDebugf("---- bad op during GPipeState::playback\n");
reed@google.com80b09de2011-05-23 13:54:24 +0000915 status = kError_Status;
916 break;
reed@google.combb6992a2011-04-26 17:41:56 +0000917 }
918 if (kDone_DrawOp == op) {
reed@google.com80b09de2011-05-23 13:54:24 +0000919 status = kDone_Status;
920 break;
reed@google.combb6992a2011-04-26 17:41:56 +0000921 }
djsollen@google.com2b2ede32012-04-12 13:24:04 +0000922 table[op](canvas, reader.getReader32(), op32, fState);
junov@chromium.orgfb103892012-09-20 19:35:43 +0000923 if ((playbackFlags & kReadAtom_PlaybackFlag) &&
yangsu@google.com1bce0a52011-06-16 21:08:19 +0000924 (table[op] != paintOp_rp &&
925 table[op] != def_Typeface_rp &&
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000926 table[op] != def_PaintFlat_rp &&
927 table[op] != def_Bitmap_rp
yangsu@google.com1bce0a52011-06-16 21:08:19 +0000928 )) {
929 status = kReadAtom_Status;
930 break;
931 }
reed@google.combb6992a2011-04-26 17:41:56 +0000932 }
reed@google.com80b09de2011-05-23 13:54:24 +0000933
934 if (bytesRead) {
reed@google.comac45f002011-05-24 13:50:21 +0000935 *bytesRead = reader.offset();
reed@google.com80b09de2011-05-23 13:54:24 +0000936 }
937 return status;
reed@google.combb6992a2011-04-26 17:41:56 +0000938}