blob: b97b98d08a4aadeedf7b05a9e339031bb006e6c2 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@google.combb6992a2011-04-26 17:41:56 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
reed@google.combb6992a2011-04-26 17:41:56 +00007 */
8
9
epoger@google.comec3ed6a2011-07-28 14:26:00 +000010
reed@google.combb6992a2011-04-26 17:41:56 +000011#include "SkCanvas.h"
reed@google.com8a85d0c2011-06-24 19:12:12 +000012#include "SkData.h"
reed@google.combb6793b2011-05-05 15:18:15 +000013#include "SkDevice.h"
reed@google.combb6992a2011-04-26 17:41:56 +000014#include "SkPaint.h"
reed@google.com75a09722012-05-10 12:56:16 +000015#include "SkPathEffect.h"
reed@google.comacd471f2011-05-03 21:26:46 +000016#include "SkGPipe.h"
reed@google.combb6992a2011-04-26 17:41:56 +000017#include "SkGPipePriv.h"
scroggo@google.com16d1d0b2012-05-02 19:09:40 +000018#include "SkImageFilter.h"
reed@google.comf5842f72011-05-04 18:30:04 +000019#include "SkStream.h"
reed@google.comb55d1182011-05-11 00:42:04 +000020#include "SkTSearch.h"
reed@google.comf5842f72011-05-04 18:30:04 +000021#include "SkTypeface.h"
reed@google.combb6992a2011-04-26 17:41:56 +000022#include "SkWriter32.h"
reed@google.comb55d1182011-05-11 00:42:04 +000023#include "SkColorFilter.h"
reed@google.com0faac1e2011-05-11 05:58:58 +000024#include "SkDrawLooper.h"
reed@google.comb55d1182011-05-11 00:42:04 +000025#include "SkMaskFilter.h"
26#include "SkRasterizer.h"
27#include "SkShader.h"
djsollen@google.com2b2ede32012-04-12 13:24:04 +000028#include "SkOrderedWriteBuffer.h"
reed@google.comb55d1182011-05-11 00:42:04 +000029
30static SkFlattenable* get_paintflat(const SkPaint& paint, unsigned paintFlat) {
31 SkASSERT(paintFlat < kCount_PaintFlats);
32 switch (paintFlat) {
33 case kColorFilter_PaintFlat: return paint.getColorFilter();
reed@google.com0faac1e2011-05-11 05:58:58 +000034 case kDrawLooper_PaintFlat: return paint.getLooper();
reed@google.comb55d1182011-05-11 00:42:04 +000035 case kMaskFilter_PaintFlat: return paint.getMaskFilter();
36 case kPathEffect_PaintFlat: return paint.getPathEffect();
37 case kRasterizer_PaintFlat: return paint.getRasterizer();
38 case kShader_PaintFlat: return paint.getShader();
scroggo@google.com16d1d0b2012-05-02 19:09:40 +000039 case kImageFilter_PaintFlat: return paint.getImageFilter();
reed@google.comb55d1182011-05-11 00:42:04 +000040 case kXfermode_PaintFlat: return paint.getXfermode();
41 }
tomhudson@google.com0c00f212011-12-28 14:59:50 +000042 SkDEBUGFAIL("never gets here");
reed@google.comb55d1182011-05-11 00:42:04 +000043 return NULL;
44}
reed@google.combb6992a2011-04-26 17:41:56 +000045
reed@google.comf5842f72011-05-04 18:30:04 +000046static size_t writeTypeface(SkWriter32* writer, SkTypeface* typeface) {
47 SkASSERT(typeface);
48 SkDynamicMemoryWStream stream;
49 typeface->serialize(&stream);
50 size_t size = stream.getOffset();
51 if (writer) {
52 writer->write32(size);
reed@google.com8a85d0c2011-06-24 19:12:12 +000053 SkAutoDataUnref data(stream.copyToData());
scroggo@google.com5af9b202012-06-04 17:17:36 +000054 writer->writePad(data.data(), size);
reed@google.comf5842f72011-05-04 18:30:04 +000055 }
scroggo@google.com5af9b202012-06-04 17:17:36 +000056 return 4 + SkAlign4(size);
reed@google.comf5842f72011-05-04 18:30:04 +000057}
58
reed@google.combb6992a2011-04-26 17:41:56 +000059///////////////////////////////////////////////////////////////////////////////
60
scroggo@google.com8ae3c7f2012-06-13 17:47:52 +000061/*
62 * Shared heap for storing large things that can be shared, for a stream
63 * used by multiple readers.
64 * TODO: Make the allocations all come from cross process safe address space
65 * TODO: Store paths (others?)
66 * TODO: Allow reclaiming of memory. Will require us to know when all readers
67 * have used the object.
68 */
69class Heap {
70public:
71 Heap() {}
72 ~Heap() {
73 for (int i = 0; i < fBitmaps.count(); i++) {
74 delete fBitmaps[i].fBitmap;
75 }
76 }
77
78 /*
79 * Add a copy of a bitmap to the heap.
80 * @param bm The SkBitmap to be copied and placed in the heap.
81 * @return void* Pointer to the heap's copy of the bitmap. If NULL,
82 * the bitmap could not be copied.
83 */
84 const SkBitmap* addBitmap(const SkBitmap& bm) {
85 const uint32_t genID = bm.getGenerationID();
86 for (int i = fBitmaps.count() - 1; i >= 0; i--) {
87 if (genID == fBitmaps[i].fGenID) {
88 return fBitmaps[i].fBitmap;
89 }
90 }
91 // TODO: Use a flag to determine whether we need the bitmap to be
92 // in shared cross process address space. If not, we can do a shallow
93 // copy.
94 SkBitmap* copy = new SkBitmap();
95 if (bm.copyTo(copy, bm.getConfig())) {
96 BitmapInfo* info = fBitmaps.append();
97 info->fBitmap = copy;
98 info->fGenID = genID;
99 return copy;
100 }
101 delete copy;
102 return NULL;
103 }
104private:
105 struct BitmapInfo {
106 SkBitmap* fBitmap;
107 // Store the generation ID of the original bitmap, since copying does
108 // not copy this field, so fBitmap's generation ID will not be useful
109 // for comparing.
110 uint32_t fGenID;
111 };
112 SkTDArray<BitmapInfo>fBitmaps;
113};
114
115///////////////////////////////////////////////////////////////////////////////
116
reed@google.combb6992a2011-04-26 17:41:56 +0000117class SkGPipeCanvas : public SkCanvas {
118public:
reed@google.comdde09562011-05-23 12:21:05 +0000119 SkGPipeCanvas(SkGPipeController*, SkWriter32*, SkFactorySet*);
reed@google.combb6992a2011-04-26 17:41:56 +0000120 virtual ~SkGPipeCanvas();
121
122 void finish() {
123 if (!fDone) {
reed@google.comdbccc882011-07-08 18:53:39 +0000124 if (this->needOpBytes()) {
125 this->writeOp(kDone_DrawOp);
126 this->doNotify();
127 }
reed@google.combb6992a2011-04-26 17:41:56 +0000128 fDone = true;
129 }
130 }
131
132 // overrides from SkCanvas
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000133 virtual int save(SaveFlags) SK_OVERRIDE;
134 virtual int saveLayer(const SkRect* bounds, const SkPaint*,
135 SaveFlags) SK_OVERRIDE;
136 virtual void restore() SK_OVERRIDE;
137 virtual bool translate(SkScalar dx, SkScalar dy) SK_OVERRIDE;
138 virtual bool scale(SkScalar sx, SkScalar sy) SK_OVERRIDE;
139 virtual bool rotate(SkScalar degrees) SK_OVERRIDE;
140 virtual bool skew(SkScalar sx, SkScalar sy) SK_OVERRIDE;
141 virtual bool concat(const SkMatrix& matrix) SK_OVERRIDE;
142 virtual void setMatrix(const SkMatrix& matrix) SK_OVERRIDE;
143 virtual bool clipRect(const SkRect& rect, SkRegion::Op op,
144 bool doAntiAlias = false) SK_OVERRIDE;
145 virtual bool clipPath(const SkPath& path, SkRegion::Op op,
146 bool doAntiAlias = false) SK_OVERRIDE;
147 virtual bool clipRegion(const SkRegion& region, SkRegion::Op op) SK_OVERRIDE;
148 virtual void clear(SkColor) SK_OVERRIDE;
149 virtual void drawPaint(const SkPaint& paint) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000150 virtual void drawPoints(PointMode, size_t count, const SkPoint pts[],
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000151 const SkPaint&) SK_OVERRIDE;
152 virtual void drawRect(const SkRect& rect, const SkPaint&) SK_OVERRIDE;
153 virtual void drawPath(const SkPath& path, const SkPaint&) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000154 virtual void drawBitmap(const SkBitmap&, SkScalar left, SkScalar top,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000155 const SkPaint*) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000156 virtual void drawBitmapRect(const SkBitmap&, const SkIRect* src,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000157 const SkRect& dst, const SkPaint*) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000158 virtual void drawBitmapMatrix(const SkBitmap&, const SkMatrix&,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000159 const SkPaint*) SK_OVERRIDE;
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000160 virtual void drawBitmapNine(const SkBitmap& bitmap, const SkIRect& center,
161 const SkRect& dst, const SkPaint* paint = NULL) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000162 virtual void drawSprite(const SkBitmap&, int left, int top,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000163 const SkPaint*) SK_OVERRIDE;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000164 virtual void drawText(const void* text, size_t byteLength, SkScalar x,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000165 SkScalar y, const SkPaint&) SK_OVERRIDE;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000166 virtual void drawPosText(const void* text, size_t byteLength,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000167 const SkPoint pos[], const SkPaint&) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000168 virtual void drawPosTextH(const void* text, size_t byteLength,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000169 const SkScalar xpos[], SkScalar constY,
170 const SkPaint&) SK_OVERRIDE;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000171 virtual void drawTextOnPath(const void* text, size_t byteLength,
172 const SkPath& path, const SkMatrix* matrix,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000173 const SkPaint&) SK_OVERRIDE;
174 virtual void drawPicture(SkPicture& picture) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000175 virtual void drawVertices(VertexMode, int vertexCount,
176 const SkPoint vertices[], const SkPoint texs[],
177 const SkColor colors[], SkXfermode*,
178 const uint16_t indices[], int indexCount,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000179 const SkPaint&) SK_OVERRIDE;
180 virtual void drawData(const void*, size_t) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000181
182private:
scroggo@google.com8ae3c7f2012-06-13 17:47:52 +0000183 Heap fHeap;
reed@google.comdde09562011-05-23 12:21:05 +0000184 SkFactorySet* fFactorySet; // optional, only used if cross-process
reed@google.comacd471f2011-05-03 21:26:46 +0000185 SkGPipeController* fController;
reed@google.combb6992a2011-04-26 17:41:56 +0000186 SkWriter32& fWriter;
reed@google.comacd471f2011-05-03 21:26:46 +0000187 size_t fBlockSize; // amount allocated for writer
188 size_t fBytesNotified;
reed@google.combb6992a2011-04-26 17:41:56 +0000189 bool fDone;
190
reed@google.comf5842f72011-05-04 18:30:04 +0000191 SkRefCntSet fTypefaceSet;
192
193 uint32_t getTypefaceID(SkTypeface*);
194
reed@google.comacd471f2011-05-03 21:26:46 +0000195 inline void writeOp(DrawOps op, unsigned flags, unsigned data) {
reed@google.combb6992a2011-04-26 17:41:56 +0000196 fWriter.write32(DrawOp_packOpFlagData(op, flags, data));
197 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000198
reed@google.comacd471f2011-05-03 21:26:46 +0000199 inline void writeOp(DrawOps op) {
reed@google.combb6992a2011-04-26 17:41:56 +0000200 fWriter.write32(DrawOp_packOpFlagData(op, 0, 0));
201 }
reed@google.comacd471f2011-05-03 21:26:46 +0000202
203 bool needOpBytes(size_t size = 0);
204
205 inline void doNotify() {
206 if (!fDone) {
207 size_t bytes = fWriter.size() - fBytesNotified;
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000208 if (bytes > 0) {
209 fController->notifyWritten(bytes);
210 fBytesNotified += bytes;
211 }
reed@google.comacd471f2011-05-03 21:26:46 +0000212 }
213 }
reed@google.comb55d1182011-05-11 00:42:04 +0000214
215 struct FlatData {
216 uint32_t fIndex; // always > 0
217 uint32_t fSize;
218
219 void* data() { return (char*)this + sizeof(*this); }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000220
reed@google.comb55d1182011-05-11 00:42:04 +0000221 static int Compare(const FlatData* a, const FlatData* b) {
222 return memcmp(&a->fSize, &b->fSize, a->fSize + sizeof(a->fSize));
223 }
224 };
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000225
226 SkTDArray<FlatData*> fBitmapArray;
227 int flattenToIndex(const SkBitmap&);
228
reed@google.comb55d1182011-05-11 00:42:04 +0000229 SkTDArray<FlatData*> fFlatArray;
230 int fCurrFlatIndex[kCount_PaintFlats];
231 int flattenToIndex(SkFlattenable* obj, PaintFlats);
232
reed@google.com31891582011-05-12 03:03:56 +0000233 SkPaint fPaint;
234 void writePaint(const SkPaint&);
reed@google.combb6992a2011-04-26 17:41:56 +0000235
reed@google.comacd471f2011-05-03 21:26:46 +0000236 class AutoPipeNotify {
237 public:
238 AutoPipeNotify(SkGPipeCanvas* canvas) : fCanvas(canvas) {}
239 ~AutoPipeNotify() { fCanvas->doNotify(); }
240 private:
241 SkGPipeCanvas* fCanvas;
242 };
243 friend class AutoPipeNotify;
244
reed@google.combb6992a2011-04-26 17:41:56 +0000245 typedef SkCanvas INHERITED;
246};
247
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000248int SkGPipeCanvas::flattenToIndex(const SkBitmap & bitmap) {
249 SkOrderedWriteBuffer tmpWriter(1024);
250 // FIXME: Rather than forcing CrossProcess, we should create an SkRefCntSet
251 // so that we can store a pointer to a bitmap's pixels during flattening.
252 tmpWriter.setFlags(SkFlattenableWriteBuffer::kCrossProcess_Flag);
253 bitmap.flatten(tmpWriter);
254
255 size_t len = tmpWriter.size();
256 size_t allocSize = len + sizeof(FlatData);
257
258 SkAutoSMalloc<1024> storage(allocSize);
259 FlatData* flat = (FlatData*)storage.get();
260 flat->fSize = len;
261 tmpWriter.flatten(flat->data());
262
263 int index = SkTSearch<FlatData>((const FlatData**)fBitmapArray.begin(),
264 fBitmapArray.count(), flat, sizeof(flat),
265 &FlatData::Compare);
266 if (index < 0) {
267 index = ~index;
268 FlatData* copy = (FlatData*)sk_malloc_throw(allocSize);
269 memcpy(copy, flat, allocSize);
270 // For bitmaps, we can use zero based indices, since we will never ask
271 // for a NULL bitmap (unlike with paint flattenables).
272 copy->fIndex = fBitmapArray.count();
273 *fBitmapArray.insert(index) = copy;
274 if (this->needOpBytes(len + sizeof(uint32_t))) {
275 this->writeOp(kDef_Bitmap_DrawOp, 0, copy->fIndex);
276 fWriter.write32(len);
277 fWriter.write(copy->data(), len);
278 }
279 }
280 return fBitmapArray[index]->fIndex;
281}
282
reed@google.comb55d1182011-05-11 00:42:04 +0000283// return 0 for NULL (or unflattenable obj), or index-base-1
284int SkGPipeCanvas::flattenToIndex(SkFlattenable* obj, PaintFlats paintflat) {
285 if (NULL == obj) {
286 return 0;
287 }
reed@google.comb55d1182011-05-11 00:42:04 +0000288
djsollen@google.com2b2ede32012-04-12 13:24:04 +0000289 SkOrderedWriteBuffer tmpWriter(1024);
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000290
291 // Needs to be cross process so a bitmap shader will be preserved
292 // FIXME: Rather than forcing CrossProcess, we should create an SkRefCntSet
293 // so that we can store a pointer to a bitmap's pixels during flattening.
294 tmpWriter.setFlags((SkFlattenableWriteBuffer::Flags)
295 (SkFlattenableWriteBuffer::kInlineFactoryNames_Flag
296 | SkFlattenableWriteBuffer::kCrossProcess_Flag));
reed@google.comdde09562011-05-23 12:21:05 +0000297 tmpWriter.setFactoryRecorder(fFactorySet);
298
reed@google.comb55d1182011-05-11 00:42:04 +0000299 tmpWriter.writeFlattenable(obj);
300 size_t len = tmpWriter.size();
301 size_t allocSize = len + sizeof(FlatData);
302
303 SkAutoSMalloc<1024> storage(allocSize);
304 FlatData* flat = (FlatData*)storage.get();
305 flat->fSize = len;
306 tmpWriter.flatten(flat->data());
307
308 int index = SkTSearch<FlatData>((const FlatData**)fFlatArray.begin(),
309 fFlatArray.count(), flat, sizeof(flat),
310 &FlatData::Compare);
311 if (index < 0) {
312 index = ~index;
313 FlatData* copy = (FlatData*)sk_malloc_throw(allocSize);
314 memcpy(copy, flat, allocSize);
315 *fFlatArray.insert(index) = copy;
316 // call this after the insert, so that count() will have been grown
317 copy->fIndex = fFlatArray.count();
318// SkDebugf("--- add flattenable[%d] size=%d index=%d\n", paintflat, len, copy->fIndex);
319
320 if (this->needOpBytes(len)) {
reed@google.com0faac1e2011-05-11 05:58:58 +0000321 this->writeOp(kDef_Flattenable_DrawOp, paintflat, copy->fIndex);
reed@google.comb55d1182011-05-11 00:42:04 +0000322 fWriter.write(copy->data(), len);
323 }
324 }
325 return fFlatArray[index]->fIndex;
326}
327
reed@google.combb6992a2011-04-26 17:41:56 +0000328///////////////////////////////////////////////////////////////////////////////
329
reed@google.comacd471f2011-05-03 21:26:46 +0000330#define MIN_BLOCK_SIZE (16 * 1024)
reed@google.combb6992a2011-04-26 17:41:56 +0000331
reed@google.comacd471f2011-05-03 21:26:46 +0000332SkGPipeCanvas::SkGPipeCanvas(SkGPipeController* controller,
reed@google.comdde09562011-05-23 12:21:05 +0000333 SkWriter32* writer, SkFactorySet* fset)
reed@google.com67908f22011-06-27 14:47:50 +0000334 : fWriter(*writer) {
335 fFactorySet = fset;
reed@google.comacd471f2011-05-03 21:26:46 +0000336 fController = controller;
reed@google.combb6992a2011-04-26 17:41:56 +0000337 fDone = false;
reed@google.comacd471f2011-05-03 21:26:46 +0000338 fBlockSize = 0; // need first block from controller
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000339 fBytesNotified = 0;
reed@google.comb55d1182011-05-11 00:42:04 +0000340 sk_bzero(fCurrFlatIndex, sizeof(fCurrFlatIndex));
reed@google.comacd471f2011-05-03 21:26:46 +0000341
reed@google.combb6793b2011-05-05 15:18:15 +0000342 // we need a device to limit our clip
343 // should the caller give us the bounds?
yangsu@google.com06b4da162011-06-17 15:04:40 +0000344 // We don't allocate pixels for the bitmap
reed@google.combb6793b2011-05-05 15:18:15 +0000345 SkBitmap bitmap;
346 bitmap.setConfig(SkBitmap::kARGB_8888_Config, 32767, 32767);
yangsu@google.com06b4da162011-06-17 15:04:40 +0000347 SkDevice* device = SkNEW_ARGS(SkDevice, (bitmap));
reed@google.combb6793b2011-05-05 15:18:15 +0000348 this->setDevice(device)->unref();
reed@google.combb6992a2011-04-26 17:41:56 +0000349}
350
351SkGPipeCanvas::~SkGPipeCanvas() {
352 this->finish();
353
reed@google.comb55d1182011-05-11 00:42:04 +0000354 fFlatArray.freeAll();
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000355 fBitmapArray.freeAll();
reed@google.combb6992a2011-04-26 17:41:56 +0000356}
357
reed@google.comacd471f2011-05-03 21:26:46 +0000358bool SkGPipeCanvas::needOpBytes(size_t needed) {
359 if (fDone) {
360 return false;
361 }
362
363 needed += 4; // size of DrawOp atom
364 if (fWriter.size() + needed > fBlockSize) {
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000365 // Before we wipe out any data that has already been written, read it
366 // out.
367 this->doNotify();
368 size_t blockSize = SkMax32(MIN_BLOCK_SIZE, needed);
369 void* block = fController->requestBlock(blockSize, &fBlockSize);
reed@google.comacd471f2011-05-03 21:26:46 +0000370 if (NULL == block) {
371 fDone = true;
372 return false;
373 }
374 fWriter.reset(block, fBlockSize);
375 fBytesNotified = 0;
376 }
377 return true;
378}
379
reed@google.comf5842f72011-05-04 18:30:04 +0000380uint32_t SkGPipeCanvas::getTypefaceID(SkTypeface* face) {
381 uint32_t id = 0; // 0 means default/null typeface
382 if (face) {
383 id = fTypefaceSet.find(face);
384 if (0 == id) {
385 id = fTypefaceSet.add(face);
386 size_t size = writeTypeface(NULL, face);
387 if (this->needOpBytes(size)) {
reed@google.combb6793b2011-05-05 15:18:15 +0000388 this->writeOp(kDef_Typeface_DrawOp);
reed@google.comf5842f72011-05-04 18:30:04 +0000389 writeTypeface(&fWriter, face);
390 }
391 }
392 }
393 return id;
394}
395
reed@google.combb6992a2011-04-26 17:41:56 +0000396///////////////////////////////////////////////////////////////////////////////
397
reed@google.comacd471f2011-05-03 21:26:46 +0000398#define NOTIFY_SETUP(canvas) \
399 AutoPipeNotify apn(canvas)
400
reed@google.combb6992a2011-04-26 17:41:56 +0000401int SkGPipeCanvas::save(SaveFlags flags) {
reed@google.comacd471f2011-05-03 21:26:46 +0000402 NOTIFY_SETUP(this);
403 if (this->needOpBytes()) {
404 this->writeOp(kSave_DrawOp, 0, flags);
405 }
reed@google.combb6992a2011-04-26 17:41:56 +0000406 return this->INHERITED::save(flags);
407}
408
409int SkGPipeCanvas::saveLayer(const SkRect* bounds, const SkPaint* paint,
410 SaveFlags saveFlags) {
reed@google.comacd471f2011-05-03 21:26:46 +0000411 NOTIFY_SETUP(this);
412 size_t size = 0;
reed@google.combb6992a2011-04-26 17:41:56 +0000413 unsigned opFlags = 0;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000414
reed@google.combb6992a2011-04-26 17:41:56 +0000415 if (bounds) {
416 opFlags |= kSaveLayer_HasBounds_DrawOpFlag;
reed@google.comacd471f2011-05-03 21:26:46 +0000417 size += sizeof(SkRect);
reed@google.combb6992a2011-04-26 17:41:56 +0000418 }
419 if (paint) {
420 opFlags |= kSaveLayer_HasPaint_DrawOpFlag;
reed@google.com31891582011-05-12 03:03:56 +0000421 this->writePaint(*paint);
reed@google.combb6992a2011-04-26 17:41:56 +0000422 }
423
reed@google.comacd471f2011-05-03 21:26:46 +0000424 if (this->needOpBytes(size)) {
425 this->writeOp(kSaveLayer_DrawOp, opFlags, saveFlags);
426 if (bounds) {
427 fWriter.writeRect(*bounds);
428 }
reed@google.combb6992a2011-04-26 17:41:56 +0000429 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000430
reed@google.combb6992a2011-04-26 17:41:56 +0000431 // we just pass on the save, so we don't create a layer
432 return this->INHERITED::save(saveFlags);
433}
434
435void SkGPipeCanvas::restore() {
reed@google.comacd471f2011-05-03 21:26:46 +0000436 NOTIFY_SETUP(this);
437 if (this->needOpBytes()) {
438 this->writeOp(kRestore_DrawOp);
439 }
reed@google.combb6992a2011-04-26 17:41:56 +0000440 this->INHERITED::restore();
441}
442
443bool SkGPipeCanvas::translate(SkScalar dx, SkScalar dy) {
444 if (dx || dy) {
reed@google.comacd471f2011-05-03 21:26:46 +0000445 NOTIFY_SETUP(this);
446 if (this->needOpBytes(2 * sizeof(SkScalar))) {
447 this->writeOp(kTranslate_DrawOp);
448 fWriter.writeScalar(dx);
449 fWriter.writeScalar(dy);
450 }
reed@google.combb6992a2011-04-26 17:41:56 +0000451 }
452 return this->INHERITED::translate(dx, dy);
453}
454
455bool SkGPipeCanvas::scale(SkScalar sx, SkScalar sy) {
456 if (sx || sy) {
reed@google.comacd471f2011-05-03 21:26:46 +0000457 NOTIFY_SETUP(this);
458 if (this->needOpBytes(2 * sizeof(SkScalar))) {
459 this->writeOp(kScale_DrawOp);
460 fWriter.writeScalar(sx);
461 fWriter.writeScalar(sy);
462 }
reed@google.combb6992a2011-04-26 17:41:56 +0000463 }
464 return this->INHERITED::scale(sx, sy);
465}
466
467bool SkGPipeCanvas::rotate(SkScalar degrees) {
468 if (degrees) {
reed@google.comacd471f2011-05-03 21:26:46 +0000469 NOTIFY_SETUP(this);
470 if (this->needOpBytes(sizeof(SkScalar))) {
471 this->writeOp(kRotate_DrawOp);
472 fWriter.writeScalar(degrees);
473 }
reed@google.combb6992a2011-04-26 17:41:56 +0000474 }
475 return this->INHERITED::rotate(degrees);
476}
477
478bool SkGPipeCanvas::skew(SkScalar sx, SkScalar sy) {
479 if (sx || sy) {
reed@google.comacd471f2011-05-03 21:26:46 +0000480 NOTIFY_SETUP(this);
481 if (this->needOpBytes(2 * sizeof(SkScalar))) {
482 this->writeOp(kSkew_DrawOp);
483 fWriter.writeScalar(sx);
484 fWriter.writeScalar(sy);
485 }
reed@google.combb6992a2011-04-26 17:41:56 +0000486 }
487 return this->INHERITED::skew(sx, sy);
488}
489
490bool SkGPipeCanvas::concat(const SkMatrix& matrix) {
491 if (!matrix.isIdentity()) {
reed@google.comacd471f2011-05-03 21:26:46 +0000492 NOTIFY_SETUP(this);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000493 if (this->needOpBytes(matrix.writeToMemory(NULL))) {
reed@google.comacd471f2011-05-03 21:26:46 +0000494 this->writeOp(kConcat_DrawOp);
djsollen@google.com2b2ede32012-04-12 13:24:04 +0000495 fWriter.writeMatrix(matrix);
reed@google.comacd471f2011-05-03 21:26:46 +0000496 }
reed@google.combb6992a2011-04-26 17:41:56 +0000497 }
498 return this->INHERITED::concat(matrix);
499}
500
501void SkGPipeCanvas::setMatrix(const SkMatrix& matrix) {
reed@google.comacd471f2011-05-03 21:26:46 +0000502 NOTIFY_SETUP(this);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000503 if (this->needOpBytes(matrix.writeToMemory(NULL))) {
reed@google.comacd471f2011-05-03 21:26:46 +0000504 this->writeOp(kSetMatrix_DrawOp);
djsollen@google.com2b2ede32012-04-12 13:24:04 +0000505 fWriter.writeMatrix(matrix);
reed@google.comacd471f2011-05-03 21:26:46 +0000506 }
reed@google.combb6992a2011-04-26 17:41:56 +0000507 this->INHERITED::setMatrix(matrix);
508}
509
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000510bool SkGPipeCanvas::clipRect(const SkRect& rect, SkRegion::Op rgnOp,
511 bool doAntiAlias) {
reed@google.comacd471f2011-05-03 21:26:46 +0000512 NOTIFY_SETUP(this);
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000513 if (this->needOpBytes(sizeof(SkRect)) + sizeof(bool)) {
reed@google.comacd471f2011-05-03 21:26:46 +0000514 this->writeOp(kClipRect_DrawOp, 0, rgnOp);
515 fWriter.writeRect(rect);
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000516 fWriter.writeBool(doAntiAlias);
reed@google.comacd471f2011-05-03 21:26:46 +0000517 }
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000518 return this->INHERITED::clipRect(rect, rgnOp, doAntiAlias);
reed@google.combb6992a2011-04-26 17:41:56 +0000519}
520
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000521bool SkGPipeCanvas::clipPath(const SkPath& path, SkRegion::Op rgnOp,
522 bool doAntiAlias) {
reed@google.comacd471f2011-05-03 21:26:46 +0000523 NOTIFY_SETUP(this);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000524 if (this->needOpBytes(path.writeToMemory(NULL)) + sizeof(bool)) {
reed@google.comacd471f2011-05-03 21:26:46 +0000525 this->writeOp(kClipPath_DrawOp, 0, rgnOp);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000526 fWriter.writePath(path);
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000527 fWriter.writeBool(doAntiAlias);
reed@google.comacd471f2011-05-03 21:26:46 +0000528 }
reed@google.combb6992a2011-04-26 17:41:56 +0000529 // we just pass on the bounds of the path
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000530 return this->INHERITED::clipRect(path.getBounds(), rgnOp, doAntiAlias);
reed@google.combb6992a2011-04-26 17:41:56 +0000531}
532
533bool SkGPipeCanvas::clipRegion(const SkRegion& region, SkRegion::Op rgnOp) {
reed@google.comacd471f2011-05-03 21:26:46 +0000534 NOTIFY_SETUP(this);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000535 if (this->needOpBytes(region.writeToMemory(NULL))) {
reed@google.comacd471f2011-05-03 21:26:46 +0000536 this->writeOp(kClipRegion_DrawOp, 0, rgnOp);
djsollen@google.com2b2ede32012-04-12 13:24:04 +0000537 fWriter.writeRegion(region);
reed@google.comacd471f2011-05-03 21:26:46 +0000538 }
reed@google.combb6992a2011-04-26 17:41:56 +0000539 return this->INHERITED::clipRegion(region, rgnOp);
540}
541
542///////////////////////////////////////////////////////////////////////////////
543
544void SkGPipeCanvas::clear(SkColor color) {
reed@google.comacd471f2011-05-03 21:26:46 +0000545 NOTIFY_SETUP(this);
reed@google.combb6992a2011-04-26 17:41:56 +0000546 unsigned flags = 0;
547 if (color) {
548 flags |= kClear_HasColor_DrawOpFlag;
549 }
reed@google.comacd471f2011-05-03 21:26:46 +0000550 if (this->needOpBytes(sizeof(SkColor))) {
551 this->writeOp(kDrawClear_DrawOp, flags, 0);
552 if (color) {
553 fWriter.write32(color);
554 }
reed@google.combb6992a2011-04-26 17:41:56 +0000555 }
556}
557
558void SkGPipeCanvas::drawPaint(const SkPaint& paint) {
reed@google.comacd471f2011-05-03 21:26:46 +0000559 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000560 this->writePaint(paint);
reed@google.comacd471f2011-05-03 21:26:46 +0000561 if (this->needOpBytes()) {
reed@google.com31891582011-05-12 03:03:56 +0000562 this->writeOp(kDrawPaint_DrawOp);
reed@google.comacd471f2011-05-03 21:26:46 +0000563 }
reed@google.combb6992a2011-04-26 17:41:56 +0000564}
565
566void SkGPipeCanvas::drawPoints(PointMode mode, size_t count,
567 const SkPoint pts[], const SkPaint& paint) {
568 if (count) {
reed@google.comacd471f2011-05-03 21:26:46 +0000569 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000570 this->writePaint(paint);
reed@google.comacd471f2011-05-03 21:26:46 +0000571 if (this->needOpBytes(4 + count * sizeof(SkPoint))) {
reed@google.com31891582011-05-12 03:03:56 +0000572 this->writeOp(kDrawPoints_DrawOp, mode, 0);
reed@google.comacd471f2011-05-03 21:26:46 +0000573 fWriter.write32(count);
574 fWriter.write(pts, count * sizeof(SkPoint));
575 }
reed@google.combb6992a2011-04-26 17:41:56 +0000576 }
577}
578
579void SkGPipeCanvas::drawRect(const SkRect& rect, const SkPaint& paint) {
reed@google.comacd471f2011-05-03 21:26:46 +0000580 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000581 this->writePaint(paint);
reed@google.comacd471f2011-05-03 21:26:46 +0000582 if (this->needOpBytes(sizeof(SkRect))) {
reed@google.com31891582011-05-12 03:03:56 +0000583 this->writeOp(kDrawRect_DrawOp);
reed@google.comacd471f2011-05-03 21:26:46 +0000584 fWriter.writeRect(rect);
585 }
reed@google.combb6992a2011-04-26 17:41:56 +0000586}
587
588void SkGPipeCanvas::drawPath(const SkPath& path, const SkPaint& paint) {
reed@google.comacd471f2011-05-03 21:26:46 +0000589 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000590 this->writePaint(paint);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000591 if (this->needOpBytes(path.writeToMemory(NULL))) {
reed@google.com31891582011-05-12 03:03:56 +0000592 this->writeOp(kDrawPath_DrawOp);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000593 fWriter.writePath(path);
reed@google.comacd471f2011-05-03 21:26:46 +0000594 }
reed@google.combb6992a2011-04-26 17:41:56 +0000595}
596
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000597void SkGPipeCanvas::drawBitmap(const SkBitmap& bm, SkScalar left, SkScalar top,
598 const SkPaint* paint) {
scroggo@google.com8ae3c7f2012-06-13 17:47:52 +0000599 const void* ptr(fHeap.addBitmap(bm));
600 if (NULL == ptr) {
601 return;
602 }
603
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000604 NOTIFY_SETUP(this);
605 if (paint) {
606 this->writePaint(*paint);
607 }
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000608
scroggo@google.com8ae3c7f2012-06-13 17:47:52 +0000609 if (this->needOpBytes(sizeof(SkScalar) * 2 + sizeof(bool) + sizeof(void*))) {
610 this->writeOp(kDrawBitmap_DrawOp, 0, 0);
611 fWriter.writePtr(const_cast<void*>(ptr));
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000612 fWriter.writeBool(paint != NULL);
613 fWriter.writeScalar(left);
614 fWriter.writeScalar(top);
615 }
reed@google.combb6992a2011-04-26 17:41:56 +0000616}
617
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000618void SkGPipeCanvas::drawBitmapRect(const SkBitmap& bm, const SkIRect* src,
619 const SkRect& dst, const SkPaint* paint) {
scroggo@google.com8ae3c7f2012-06-13 17:47:52 +0000620 const void* ptr(fHeap.addBitmap(bm));
621 if (NULL == ptr) {
622 return;
623 }
624
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000625 NOTIFY_SETUP(this);
626 if (paint) {
627 this->writePaint(*paint);
628 }
629
scroggo@google.com8ae3c7f2012-06-13 17:47:52 +0000630 size_t opBytesNeeded = sizeof(SkRect) + sizeof(bool) * 2 + sizeof(void*);
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000631 bool hasSrc = src != NULL;
632 if (hasSrc) {
633 opBytesNeeded += sizeof(int32_t) * 4;
634 }
635 if (this->needOpBytes(opBytesNeeded)) {
scroggo@google.com8ae3c7f2012-06-13 17:47:52 +0000636 this->writeOp(kDrawBitmapRect_DrawOp, 0, 0);
637 fWriter.writePtr(const_cast<void*>(ptr));
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000638 fWriter.writeBool(paint != NULL);
639 fWriter.writeBool(hasSrc);
640 if (hasSrc) {
641 fWriter.write32(src->fLeft);
642 fWriter.write32(src->fTop);
643 fWriter.write32(src->fRight);
644 fWriter.write32(src->fBottom);
645 }
646 fWriter.writeRect(dst);
647 }
reed@google.combb6992a2011-04-26 17:41:56 +0000648}
649
650void SkGPipeCanvas::drawBitmapMatrix(const SkBitmap&, const SkMatrix&,
651 const SkPaint*) {
652 UNIMPLEMENTED
653}
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000654
655void SkGPipeCanvas::drawBitmapNine(const SkBitmap& bm, const SkIRect& center,
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000656 const SkRect& dst, const SkPaint* paint) {
scroggo@google.com8ae3c7f2012-06-13 17:47:52 +0000657 const void* ptr(fHeap.addBitmap(bm));
658 if (NULL == ptr) {
659 return;
660 }
661
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000662 NOTIFY_SETUP(this);
663 if (paint) {
664 this->writePaint(*paint);
665 }
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000666
667 if (this->needOpBytes(sizeof(int32_t) * 4 + sizeof(bool)
scroggo@google.com8ae3c7f2012-06-13 17:47:52 +0000668 + sizeof(SkRect) + sizeof(void*))) {
669 this->writeOp(kDrawBitmapNine_DrawOp, 0, 0);
670 fWriter.writePtr(const_cast<void*>(ptr));
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000671 fWriter.writeBool(paint != NULL);
672 fWriter.write32(center.fLeft);
673 fWriter.write32(center.fTop);
674 fWriter.write32(center.fRight);
675 fWriter.write32(center.fBottom);
676 fWriter.writeRect(dst);
677 }
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000678}
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000679
680void SkGPipeCanvas::drawSprite(const SkBitmap& bm, int left, int top,
681 const SkPaint* paint) {
scroggo@google.com8ae3c7f2012-06-13 17:47:52 +0000682 const void* ptr(fHeap.addBitmap(bm));
683 if (NULL == ptr) {
684 return;
685 }
686
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000687 NOTIFY_SETUP(this);
688 if (paint) {
689 this->writePaint(*paint);
690 }
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000691
scroggo@google.com8ae3c7f2012-06-13 17:47:52 +0000692 if (this->needOpBytes(sizeof(int32_t) * 2 + sizeof(bool) + sizeof(void*))) {
693 this->writeOp(kDrawSprite_DrawOp, 0, 0);
694 fWriter.writePtr(const_cast<void*>(ptr));
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000695 fWriter.writeBool(paint != NULL);
696 fWriter.write32(left);
697 fWriter.write32(top);
698 }
reed@google.combb6992a2011-04-26 17:41:56 +0000699}
700
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000701void SkGPipeCanvas::drawText(const void* text, size_t byteLength, SkScalar x,
reed@google.combb6992a2011-04-26 17:41:56 +0000702 SkScalar y, const SkPaint& paint) {
703 if (byteLength) {
reed@google.comacd471f2011-05-03 21:26:46 +0000704 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000705 this->writePaint(paint);
reed@google.comacd471f2011-05-03 21:26:46 +0000706 if (this->needOpBytes(4 + SkAlign4(byteLength) + 2 * sizeof(SkScalar))) {
reed@google.com31891582011-05-12 03:03:56 +0000707 this->writeOp(kDrawText_DrawOp);
reed@google.comacd471f2011-05-03 21:26:46 +0000708 fWriter.write32(byteLength);
709 fWriter.writePad(text, byteLength);
710 fWriter.writeScalar(x);
711 fWriter.writeScalar(y);
712 }
reed@google.combb6992a2011-04-26 17:41:56 +0000713 }
714}
715
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000716void SkGPipeCanvas::drawPosText(const void* text, size_t byteLength,
reed@google.combb6992a2011-04-26 17:41:56 +0000717 const SkPoint pos[], const SkPaint& paint) {
718 if (byteLength) {
reed@google.comacd471f2011-05-03 21:26:46 +0000719 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000720 this->writePaint(paint);
reed@google.combb6992a2011-04-26 17:41:56 +0000721 int count = paint.textToGlyphs(text, byteLength, NULL);
reed@google.comacd471f2011-05-03 21:26:46 +0000722 if (this->needOpBytes(4 + SkAlign4(byteLength) + 4 + count * sizeof(SkPoint))) {
reed@google.com31891582011-05-12 03:03:56 +0000723 this->writeOp(kDrawPosText_DrawOp);
reed@google.comacd471f2011-05-03 21:26:46 +0000724 fWriter.write32(byteLength);
725 fWriter.writePad(text, byteLength);
726 fWriter.write32(count);
727 fWriter.write(pos, count * sizeof(SkPoint));
728 }
reed@google.combb6992a2011-04-26 17:41:56 +0000729 }
730}
731
732void SkGPipeCanvas::drawPosTextH(const void* text, size_t byteLength,
733 const SkScalar xpos[], SkScalar constY,
734 const SkPaint& paint) {
735 if (byteLength) {
reed@google.comacd471f2011-05-03 21:26:46 +0000736 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000737 this->writePaint(paint);
reed@google.combb6992a2011-04-26 17:41:56 +0000738 int count = paint.textToGlyphs(text, byteLength, NULL);
reed@google.comacd471f2011-05-03 21:26:46 +0000739 if (this->needOpBytes(4 + SkAlign4(byteLength) + 4 + count * sizeof(SkScalar) + 4)) {
reed@google.com31891582011-05-12 03:03:56 +0000740 this->writeOp(kDrawPosTextH_DrawOp);
reed@google.comacd471f2011-05-03 21:26:46 +0000741 fWriter.write32(byteLength);
742 fWriter.writePad(text, byteLength);
743 fWriter.write32(count);
744 fWriter.write(xpos, count * sizeof(SkScalar));
745 fWriter.writeScalar(constY);
746 }
reed@google.combb6992a2011-04-26 17:41:56 +0000747 }
748}
749
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000750void SkGPipeCanvas::drawTextOnPath(const void* text, size_t byteLength,
751 const SkPath& path, const SkMatrix* matrix,
reed@google.combb6992a2011-04-26 17:41:56 +0000752 const SkPaint& paint) {
753 if (byteLength) {
reed@google.comacd471f2011-05-03 21:26:46 +0000754 NOTIFY_SETUP(this);
reed@google.combb6992a2011-04-26 17:41:56 +0000755 unsigned flags = 0;
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000756 size_t size = 4 + SkAlign4(byteLength) + path.writeToMemory(NULL);
reed@google.combb6992a2011-04-26 17:41:56 +0000757 if (matrix) {
758 flags |= kDrawTextOnPath_HasMatrix_DrawOpFlag;
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000759 size += matrix->writeToMemory(NULL);
reed@google.combb6992a2011-04-26 17:41:56 +0000760 }
reed@google.com31891582011-05-12 03:03:56 +0000761 this->writePaint(paint);
reed@google.comacd471f2011-05-03 21:26:46 +0000762 if (this->needOpBytes(size)) {
reed@google.com31891582011-05-12 03:03:56 +0000763 this->writeOp(kDrawTextOnPath_DrawOp, flags, 0);
reed@google.combb6992a2011-04-26 17:41:56 +0000764
reed@google.comacd471f2011-05-03 21:26:46 +0000765 fWriter.write32(byteLength);
766 fWriter.writePad(text, byteLength);
reed@google.combb6992a2011-04-26 17:41:56 +0000767
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000768 fWriter.writePath(path);
reed@google.comacd471f2011-05-03 21:26:46 +0000769 if (matrix) {
djsollen@google.com2b2ede32012-04-12 13:24:04 +0000770 fWriter.writeMatrix(*matrix);
reed@google.comacd471f2011-05-03 21:26:46 +0000771 }
reed@google.combb6992a2011-04-26 17:41:56 +0000772 }
773 }
774}
775
776void SkGPipeCanvas::drawPicture(SkPicture& picture) {
reed@google.com0faac1e2011-05-11 05:58:58 +0000777 // we want to playback the picture into individual draw calls
778 this->INHERITED::drawPicture(picture);
reed@google.combb6992a2011-04-26 17:41:56 +0000779}
780
reed@google.combb6992a2011-04-26 17:41:56 +0000781void SkGPipeCanvas::drawVertices(VertexMode mode, int vertexCount,
782 const SkPoint vertices[], const SkPoint texs[],
783 const SkColor colors[], SkXfermode*,
784 const uint16_t indices[], int indexCount,
785 const SkPaint& paint) {
786 if (0 == vertexCount) {
787 return;
788 }
789
reed@google.comacd471f2011-05-03 21:26:46 +0000790 NOTIFY_SETUP(this);
791 size_t size = 4 + vertexCount * sizeof(SkPoint);
reed@google.com31891582011-05-12 03:03:56 +0000792 this->writePaint(paint);
reed@google.combb6992a2011-04-26 17:41:56 +0000793 unsigned flags = 0;
794 if (texs) {
795 flags |= kDrawVertices_HasTexs_DrawOpFlag;
reed@google.comacd471f2011-05-03 21:26:46 +0000796 size += vertexCount * sizeof(SkPoint);
reed@google.combb6992a2011-04-26 17:41:56 +0000797 }
798 if (colors) {
799 flags |= kDrawVertices_HasColors_DrawOpFlag;
reed@google.comacd471f2011-05-03 21:26:46 +0000800 size += vertexCount * sizeof(SkColor);
reed@google.combb6992a2011-04-26 17:41:56 +0000801 }
802 if (indices && indexCount > 0) {
803 flags |= kDrawVertices_HasIndices_DrawOpFlag;
reed@google.comacd471f2011-05-03 21:26:46 +0000804 size += 4 + SkAlign4(indexCount * sizeof(uint16_t));
reed@google.combb6992a2011-04-26 17:41:56 +0000805 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000806
reed@google.comacd471f2011-05-03 21:26:46 +0000807 if (this->needOpBytes(size)) {
reed@google.com31891582011-05-12 03:03:56 +0000808 this->writeOp(kDrawVertices_DrawOp, flags, 0);
reed@google.comacd471f2011-05-03 21:26:46 +0000809 fWriter.write32(mode);
810 fWriter.write32(vertexCount);
811 fWriter.write(vertices, vertexCount * sizeof(SkPoint));
812 if (texs) {
813 fWriter.write(texs, vertexCount * sizeof(SkPoint));
814 }
815 if (colors) {
816 fWriter.write(colors, vertexCount * sizeof(SkColor));
817 }
reed@google.combb6992a2011-04-26 17:41:56 +0000818
reed@google.comacd471f2011-05-03 21:26:46 +0000819 // TODO: flatten xfermode
reed@google.combb6992a2011-04-26 17:41:56 +0000820
reed@google.comacd471f2011-05-03 21:26:46 +0000821 if (indices && indexCount > 0) {
822 fWriter.write32(indexCount);
823 fWriter.writePad(indices, indexCount * sizeof(uint16_t));
824 }
reed@google.combb6992a2011-04-26 17:41:56 +0000825 }
826}
827
reed@google.comacd471f2011-05-03 21:26:46 +0000828void SkGPipeCanvas::drawData(const void* ptr, size_t size) {
829 if (size && ptr) {
830 NOTIFY_SETUP(this);
reed@google.combb6992a2011-04-26 17:41:56 +0000831 unsigned data = 0;
832 if (size < (1 << DRAWOPS_DATA_BITS)) {
833 data = (unsigned)size;
834 }
reed@google.comacd471f2011-05-03 21:26:46 +0000835 if (this->needOpBytes(4 + SkAlign4(size))) {
836 this->writeOp(kDrawData_DrawOp, 0, data);
837 if (0 == data) {
838 fWriter.write32(size);
839 }
reed@google.combb6793b2011-05-05 15:18:15 +0000840 fWriter.writePad(ptr, size);
reed@google.combb6992a2011-04-26 17:41:56 +0000841 }
842 }
843}
844
845///////////////////////////////////////////////////////////////////////////////
846
847template <typename T> uint32_t castToU32(T value) {
848 union {
849 T fSrc;
850 uint32_t fDst;
851 } data;
852 data.fSrc = value;
853 return data.fDst;
854}
855
reed@google.com31891582011-05-12 03:03:56 +0000856void SkGPipeCanvas::writePaint(const SkPaint& paint) {
857 SkPaint& base = fPaint;
reed@google.combb6992a2011-04-26 17:41:56 +0000858 uint32_t storage[32];
859 uint32_t* ptr = storage;
reed@google.combb6992a2011-04-26 17:41:56 +0000860
861 if (base.getFlags() != paint.getFlags()) {
reed@google.combb6992a2011-04-26 17:41:56 +0000862 *ptr++ = PaintOp_packOpData(kFlags_PaintOp, paint.getFlags());
reed@google.comf5842f72011-05-04 18:30:04 +0000863 base.setFlags(paint.getFlags());
reed@google.combb6992a2011-04-26 17:41:56 +0000864 }
865 if (base.getColor() != paint.getColor()) {
reed@google.combb6992a2011-04-26 17:41:56 +0000866 *ptr++ = PaintOp_packOp(kColor_PaintOp);
867 *ptr++ = paint.getColor();
reed@google.comf5842f72011-05-04 18:30:04 +0000868 base.setColor(paint.getColor());
reed@google.combb6992a2011-04-26 17:41:56 +0000869 }
870 if (base.getStyle() != paint.getStyle()) {
reed@google.combb6992a2011-04-26 17:41:56 +0000871 *ptr++ = PaintOp_packOpData(kStyle_PaintOp, paint.getStyle());
reed@google.comf5842f72011-05-04 18:30:04 +0000872 base.setStyle(paint.getStyle());
reed@google.combb6992a2011-04-26 17:41:56 +0000873 }
874 if (base.getStrokeJoin() != paint.getStrokeJoin()) {
reed@google.combb6992a2011-04-26 17:41:56 +0000875 *ptr++ = PaintOp_packOpData(kJoin_PaintOp, paint.getStrokeJoin());
reed@google.comf5842f72011-05-04 18:30:04 +0000876 base.setStrokeJoin(paint.getStrokeJoin());
reed@google.combb6992a2011-04-26 17:41:56 +0000877 }
878 if (base.getStrokeCap() != paint.getStrokeCap()) {
reed@google.combb6992a2011-04-26 17:41:56 +0000879 *ptr++ = PaintOp_packOpData(kCap_PaintOp, paint.getStrokeCap());
reed@google.comf5842f72011-05-04 18:30:04 +0000880 base.setStrokeCap(paint.getStrokeCap());
reed@google.combb6992a2011-04-26 17:41:56 +0000881 }
882 if (base.getStrokeWidth() != paint.getStrokeWidth()) {
reed@google.combb6992a2011-04-26 17:41:56 +0000883 *ptr++ = PaintOp_packOp(kWidth_PaintOp);
884 *ptr++ = castToU32(paint.getStrokeWidth());
reed@google.comf5842f72011-05-04 18:30:04 +0000885 base.setStrokeWidth(paint.getStrokeWidth());
reed@google.combb6992a2011-04-26 17:41:56 +0000886 }
887 if (base.getStrokeMiter() != paint.getStrokeMiter()) {
reed@google.combb6992a2011-04-26 17:41:56 +0000888 *ptr++ = PaintOp_packOp(kMiter_PaintOp);
889 *ptr++ = castToU32(paint.getStrokeMiter());
reed@google.comf5842f72011-05-04 18:30:04 +0000890 base.setStrokeMiter(paint.getStrokeMiter());
reed@google.combb6992a2011-04-26 17:41:56 +0000891 }
892 if (base.getTextEncoding() != paint.getTextEncoding()) {
reed@google.combb6992a2011-04-26 17:41:56 +0000893 *ptr++ = PaintOp_packOpData(kEncoding_PaintOp, paint.getTextEncoding());
reed@google.comf5842f72011-05-04 18:30:04 +0000894 base.setTextEncoding(paint.getTextEncoding());
reed@google.combb6992a2011-04-26 17:41:56 +0000895 }
896 if (base.getHinting() != paint.getHinting()) {
reed@google.combb6992a2011-04-26 17:41:56 +0000897 *ptr++ = PaintOp_packOpData(kHinting_PaintOp, paint.getHinting());
reed@google.comf5842f72011-05-04 18:30:04 +0000898 base.setHinting(paint.getHinting());
reed@google.combb6992a2011-04-26 17:41:56 +0000899 }
900 if (base.getTextAlign() != paint.getTextAlign()) {
reed@google.combb6992a2011-04-26 17:41:56 +0000901 *ptr++ = PaintOp_packOpData(kAlign_PaintOp, paint.getTextAlign());
reed@google.comf5842f72011-05-04 18:30:04 +0000902 base.setTextAlign(paint.getTextAlign());
reed@google.combb6992a2011-04-26 17:41:56 +0000903 }
904 if (base.getTextSize() != paint.getTextSize()) {
reed@google.combb6992a2011-04-26 17:41:56 +0000905 *ptr++ = PaintOp_packOp(kTextSize_PaintOp);
906 *ptr++ = castToU32(paint.getTextSize());
reed@google.comf5842f72011-05-04 18:30:04 +0000907 base.setTextSize(paint.getTextSize());
reed@google.combb6992a2011-04-26 17:41:56 +0000908 }
909 if (base.getTextScaleX() != paint.getTextScaleX()) {
reed@google.combb6992a2011-04-26 17:41:56 +0000910 *ptr++ = PaintOp_packOp(kTextScaleX_PaintOp);
911 *ptr++ = castToU32(paint.getTextScaleX());
reed@google.comf5842f72011-05-04 18:30:04 +0000912 base.setTextScaleX(paint.getTextScaleX());
reed@google.combb6992a2011-04-26 17:41:56 +0000913 }
914 if (base.getTextSkewX() != paint.getTextSkewX()) {
reed@google.combb6992a2011-04-26 17:41:56 +0000915 *ptr++ = PaintOp_packOp(kTextSkewX_PaintOp);
916 *ptr++ = castToU32(paint.getTextSkewX());
reed@google.comf5842f72011-05-04 18:30:04 +0000917 base.setTextSkewX(paint.getTextSkewX());
918 }
919
920 if (!SkTypeface::Equal(base.getTypeface(), paint.getTypeface())) {
921 uint32_t id = this->getTypefaceID(paint.getTypeface());
reed@google.comf5842f72011-05-04 18:30:04 +0000922 *ptr++ = PaintOp_packOpData(kTypeface_PaintOp, id);
923 base.setTypeface(paint.getTypeface());
reed@google.combb6992a2011-04-26 17:41:56 +0000924 }
reed@google.combb6992a2011-04-26 17:41:56 +0000925
reed@google.comb55d1182011-05-11 00:42:04 +0000926 for (int i = 0; i < kCount_PaintFlats; i++) {
927 int index = this->flattenToIndex(get_paintflat(paint, i), (PaintFlats)i);
928 SkASSERT(index >= 0 && index <= fFlatArray.count());
929 if (index != fCurrFlatIndex[i]) {
reed@google.comb55d1182011-05-11 00:42:04 +0000930 *ptr++ = PaintOp_packOpFlagData(kFlatIndex_PaintOp, i, index);
931 fCurrFlatIndex[i] = index;
932 }
933 }
934
reed@google.comacd471f2011-05-03 21:26:46 +0000935 size_t size = (char*)ptr - (char*)storage;
936 if (size && this->needOpBytes(size)) {
reed@google.comb55d1182011-05-11 00:42:04 +0000937 this->writeOp(kPaintOp_DrawOp, 0, size);
reed@google.comb55d1182011-05-11 00:42:04 +0000938 fWriter.write(storage, size);
reed@google.combb6992a2011-04-26 17:41:56 +0000939 for (size_t i = 0; i < size/4; i++) {
reed@google.comb55d1182011-05-11 00:42:04 +0000940// SkDebugf("[%d] %08X\n", i, storage[i]);
reed@google.combb6992a2011-04-26 17:41:56 +0000941 }
942 }
reed@google.combb6992a2011-04-26 17:41:56 +0000943}
944
945///////////////////////////////////////////////////////////////////////////////
946
947#include "SkGPipe.h"
948
reed@google.comacd471f2011-05-03 21:26:46 +0000949SkGPipeWriter::SkGPipeWriter() : fWriter(0) {
reed@google.combb6992a2011-04-26 17:41:56 +0000950 fCanvas = NULL;
951}
952
953SkGPipeWriter::~SkGPipeWriter() {
reed@google.comacd471f2011-05-03 21:26:46 +0000954 this->endRecording();
reed@google.combb6992a2011-04-26 17:41:56 +0000955 SkSafeUnref(fCanvas);
956}
957
reed@google.comdde09562011-05-23 12:21:05 +0000958SkCanvas* SkGPipeWriter::startRecording(SkGPipeController* controller,
959 uint32_t flags) {
reed@google.combb6992a2011-04-26 17:41:56 +0000960 if (NULL == fCanvas) {
reed@google.comacd471f2011-05-03 21:26:46 +0000961 fWriter.reset(NULL, 0);
reed@google.comdde09562011-05-23 12:21:05 +0000962 fFactorySet.reset();
963 fCanvas = SkNEW_ARGS(SkGPipeCanvas, (controller, &fWriter,
964 (flags & kCrossProcess_Flag) ?
965 &fFactorySet : NULL));
reed@google.combb6992a2011-04-26 17:41:56 +0000966 }
967 return fCanvas;
968}
969
970void SkGPipeWriter::endRecording() {
971 if (fCanvas) {
972 fCanvas->finish();
973 fCanvas->unref();
974 fCanvas = NULL;
975 }
976}
977