blob: 4f408fd17921d7ebf4bf23352c1628811998f5bc [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
scroggo@google.com10dccde2012-08-08 20:43:22 +00009#include "SkBitmapHeap.h"
reed@google.combb6992a2011-04-26 17:41:56 +000010#include "SkCanvas.h"
scroggo@google.com10dccde2012-08-08 20:43:22 +000011#include "SkColorFilter.h"
reed@google.com8a85d0c2011-06-24 19:12:12 +000012#include "SkData.h"
scroggo@google.com10dccde2012-08-08 20:43:22 +000013#include "SkDrawLooper.h"
reed@google.combb6793b2011-05-05 15:18:15 +000014#include "SkDevice.h"
reed@google.comacd471f2011-05-03 21:26:46 +000015#include "SkGPipe.h"
reed@google.combb6992a2011-04-26 17:41:56 +000016#include "SkGPipePriv.h"
scroggo@google.com16d1d0b2012-05-02 19:09:40 +000017#include "SkImageFilter.h"
scroggo@google.com10dccde2012-08-08 20:43:22 +000018#include "SkMaskFilter.h"
19#include "SkOrderedWriteBuffer.h"
20#include "SkPaint.h"
21#include "SkPathEffect.h"
22#include "SkPictureFlat.h"
23#include "SkRasterizer.h"
24#include "SkShader.h"
reed@google.comf5842f72011-05-04 18:30:04 +000025#include "SkStream.h"
reed@google.comb55d1182011-05-11 00:42:04 +000026#include "SkTSearch.h"
reed@google.comf5842f72011-05-04 18:30:04 +000027#include "SkTypeface.h"
reed@google.combb6992a2011-04-26 17:41:56 +000028#include "SkWriter32.h"
reed@google.comb55d1182011-05-11 00:42:04 +000029
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +000030static bool isCrossProcess(uint32_t flags) {
31 return SkToBool(flags & SkGPipeWriter::kCrossProcess_Flag);
32}
33
reed@google.comb55d1182011-05-11 00:42:04 +000034static SkFlattenable* get_paintflat(const SkPaint& paint, unsigned paintFlat) {
35 SkASSERT(paintFlat < kCount_PaintFlats);
36 switch (paintFlat) {
37 case kColorFilter_PaintFlat: return paint.getColorFilter();
reed@google.com0faac1e2011-05-11 05:58:58 +000038 case kDrawLooper_PaintFlat: return paint.getLooper();
reed@google.comb55d1182011-05-11 00:42:04 +000039 case kMaskFilter_PaintFlat: return paint.getMaskFilter();
40 case kPathEffect_PaintFlat: return paint.getPathEffect();
41 case kRasterizer_PaintFlat: return paint.getRasterizer();
42 case kShader_PaintFlat: return paint.getShader();
scroggo@google.com16d1d0b2012-05-02 19:09:40 +000043 case kImageFilter_PaintFlat: return paint.getImageFilter();
reed@google.comb55d1182011-05-11 00:42:04 +000044 case kXfermode_PaintFlat: return paint.getXfermode();
45 }
tomhudson@google.com0c00f212011-12-28 14:59:50 +000046 SkDEBUGFAIL("never gets here");
reed@google.comb55d1182011-05-11 00:42:04 +000047 return NULL;
48}
reed@google.combb6992a2011-04-26 17:41:56 +000049
reed@google.comf5842f72011-05-04 18:30:04 +000050static size_t writeTypeface(SkWriter32* writer, SkTypeface* typeface) {
51 SkASSERT(typeface);
52 SkDynamicMemoryWStream stream;
53 typeface->serialize(&stream);
54 size_t size = stream.getOffset();
55 if (writer) {
56 writer->write32(size);
reed@google.com8a85d0c2011-06-24 19:12:12 +000057 SkAutoDataUnref data(stream.copyToData());
robertphillips@google.com59f46b82012-07-10 17:30:58 +000058 writer->writePad(data->data(), size);
reed@google.comf5842f72011-05-04 18:30:04 +000059 }
scroggo@google.com5af9b202012-06-04 17:17:36 +000060 return 4 + SkAlign4(size);
reed@google.comf5842f72011-05-04 18:30:04 +000061}
62
reed@google.combb6992a2011-04-26 17:41:56 +000063///////////////////////////////////////////////////////////////////////////////
64
scroggo@google.com4dffc592012-07-17 16:49:40 +000065class FlattenableHeap : public SkFlatController {
66public:
scroggo@google.com664fab12012-08-14 19:22:05 +000067 FlattenableHeap(int numFlatsToKeep, SkNamedFactorySet* fset, bool isCrossProcess)
scroggo@google.com15543602012-08-02 18:49:49 +000068 : fNumFlatsToKeep(numFlatsToKeep) {
scroggo@google.com664fab12012-08-14 19:22:05 +000069 SkASSERT((isCrossProcess && fset != NULL) || (!isCrossProcess && NULL == fset));
70 if (isCrossProcess) {
71 this->setNamedFactorySet(fset);
72 this->setWriteBufferFlags(SkFlattenableWriteBuffer::kCrossProcess_Flag);
73 }
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +000074 }
scroggo@google.com4dffc592012-07-17 16:49:40 +000075
76 ~FlattenableHeap() {
77 fPointers.freeAll();
78 }
79
80 virtual void* allocThrow(size_t bytes) SK_OVERRIDE;
81
82 virtual void unalloc(void* ptr) SK_OVERRIDE;
83
scroggo@google.com7ca24432012-08-14 15:48:43 +000084 void setBitmapStorage(SkBitmapHeap* heap) {
85 this->setBitmapHeap(heap);
86 }
87
scroggo@google.com4dffc592012-07-17 16:49:40 +000088 const SkFlatData* flatToReplace() const;
89
90 // Mark an SkFlatData as one that should not be returned by flatToReplace.
91 // Takes the result of SkFlatData::index() as its parameter.
92 void markFlatForKeeping(int index) {
93 *fFlatsThatMustBeKept.append() = index;
94 }
95
96 void markAllFlatsSafeToDelete() {
97 fFlatsThatMustBeKept.reset();
98 }
99
100private:
101 // Keep track of the indices (i.e. the result of SkFlatData::index()) of
102 // flats that must be kept, since they are on the current paint.
103 SkTDArray<int> fFlatsThatMustBeKept;
104 SkTDArray<void*> fPointers;
105 const int fNumFlatsToKeep;
106};
107
108void FlattenableHeap::unalloc(void* ptr) {
109 int indexToRemove = fPointers.rfind(ptr);
110 if (indexToRemove >= 0) {
111 sk_free(ptr);
112 fPointers.remove(indexToRemove);
113 }
114}
115
116void* FlattenableHeap::allocThrow(size_t bytes) {
117 void* ptr = sk_malloc_throw(bytes);
118 *fPointers.append() = ptr;
119 return ptr;
120}
121
122const SkFlatData* FlattenableHeap::flatToReplace() const {
123 // First, determine whether we should replace one.
124 if (fPointers.count() > fNumFlatsToKeep) {
125 // Look through the flattenable heap.
126 // TODO: Return the LRU flat.
127 for (int i = 0; i < fPointers.count(); i++) {
128 SkFlatData* potential = (SkFlatData*)fPointers[i];
129 // Make sure that it is not one that must be kept.
130 bool mustKeep = false;
131 for (int j = 0; j < fFlatsThatMustBeKept.count(); j++) {
132 if (potential->index() == fFlatsThatMustBeKept[j]) {
133 mustKeep = true;
134 break;
135 }
136 }
137 if (!mustKeep) {
138 return potential;
139 }
140 }
141 }
142 return NULL;
143}
144
145///////////////////////////////////////////////////////////////////////////////
146
147class FlatDictionary : public SkFlatDictionary<SkFlattenable> {
148public:
scroggo@google.com15543602012-08-02 18:49:49 +0000149 FlatDictionary(FlattenableHeap* heap)
150 : SkFlatDictionary<SkFlattenable>(heap) {
scroggo@google.com4dffc592012-07-17 16:49:40 +0000151 fFlattenProc = &flattenFlattenableProc;
152 // No need to define fUnflattenProc since the writer will never
153 // unflatten the data.
154 }
155 static void flattenFlattenableProc(SkOrderedWriteBuffer& buffer,
156 const void* obj) {
157 buffer.writeFlattenable((SkFlattenable*)obj);
158 }
159
160};
161
162///////////////////////////////////////////////////////////////////////////////
163
reed@google.combb6992a2011-04-26 17:41:56 +0000164class SkGPipeCanvas : public SkCanvas {
165public:
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000166 SkGPipeCanvas(SkGPipeController*, SkWriter32*, uint32_t flags);
reed@google.combb6992a2011-04-26 17:41:56 +0000167 virtual ~SkGPipeCanvas();
168
169 void finish() {
170 if (!fDone) {
reed@google.comdbccc882011-07-08 18:53:39 +0000171 if (this->needOpBytes()) {
172 this->writeOp(kDone_DrawOp);
173 this->doNotify();
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000174 if (shouldFlattenBitmaps(fFlags)) {
scroggo@google.comd9d29672012-08-14 17:21:34 +0000175 // In this case, a BitmapShuttle is reffed by the SkBitmapHeap
176 // and refs this canvas. Unref the SkBitmapHeap to end the
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000177 // circular reference. When shouldFlattenBitmaps is false,
scroggo@google.comd9d29672012-08-14 17:21:34 +0000178 // there is no circular reference, so the SkBitmapHeap can be
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000179 // safely unreffed in the destructor.
scroggo@google.comd9d29672012-08-14 17:21:34 +0000180 fBitmapHeap->unref();
scroggo@google.com7ca24432012-08-14 15:48:43 +0000181 // This eliminates a similar circular reference (Canvas owns
scroggo@google.comd9d29672012-08-14 17:21:34 +0000182 // the FlattenableHeap which holds a ref to the SkBitmapHeap).
scroggo@google.com7ca24432012-08-14 15:48:43 +0000183 fFlattenableHeap.setBitmapStorage(NULL);
scroggo@google.comd9d29672012-08-14 17:21:34 +0000184 fBitmapHeap = NULL;
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000185 }
reed@google.comdbccc882011-07-08 18:53:39 +0000186 }
reed@google.combb6992a2011-04-26 17:41:56 +0000187 fDone = true;
188 }
189 }
190
junov@chromium.org77eec242012-07-18 17:54:45 +0000191 void flushRecording(bool detachCurrentBlock);
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000192 size_t freeMemoryIfPossible(size_t bytesToFree);
junov@chromium.org77eec242012-07-18 17:54:45 +0000193
scroggo@google.com15011ee2012-07-26 20:03:32 +0000194 size_t storageAllocatedForRecording() {
scroggo@google.comd5d158b2012-08-14 20:38:28 +0000195 return (NULL == fBitmapHeap) ? 0 : fBitmapHeap->bytesAllocated();
scroggo@google.com15011ee2012-07-26 20:03:32 +0000196 }
197
reed@google.combb6992a2011-04-26 17:41:56 +0000198 // overrides from SkCanvas
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000199 virtual int save(SaveFlags) SK_OVERRIDE;
200 virtual int saveLayer(const SkRect* bounds, const SkPaint*,
201 SaveFlags) SK_OVERRIDE;
202 virtual void restore() SK_OVERRIDE;
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000203 virtual bool isDrawingToLayer() const SK_OVERRIDE;
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000204 virtual bool translate(SkScalar dx, SkScalar dy) SK_OVERRIDE;
205 virtual bool scale(SkScalar sx, SkScalar sy) SK_OVERRIDE;
206 virtual bool rotate(SkScalar degrees) SK_OVERRIDE;
207 virtual bool skew(SkScalar sx, SkScalar sy) SK_OVERRIDE;
208 virtual bool concat(const SkMatrix& matrix) SK_OVERRIDE;
209 virtual void setMatrix(const SkMatrix& matrix) SK_OVERRIDE;
210 virtual bool clipRect(const SkRect& rect, SkRegion::Op op,
211 bool doAntiAlias = false) SK_OVERRIDE;
212 virtual bool clipPath(const SkPath& path, SkRegion::Op op,
213 bool doAntiAlias = false) SK_OVERRIDE;
214 virtual bool clipRegion(const SkRegion& region, SkRegion::Op op) SK_OVERRIDE;
215 virtual void clear(SkColor) SK_OVERRIDE;
216 virtual void drawPaint(const SkPaint& paint) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000217 virtual void drawPoints(PointMode, size_t count, const SkPoint pts[],
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000218 const SkPaint&) SK_OVERRIDE;
219 virtual void drawRect(const SkRect& rect, const SkPaint&) SK_OVERRIDE;
220 virtual void drawPath(const SkPath& path, const SkPaint&) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000221 virtual void drawBitmap(const SkBitmap&, SkScalar left, SkScalar top,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000222 const SkPaint*) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000223 virtual void drawBitmapRect(const SkBitmap&, const SkIRect* src,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000224 const SkRect& dst, const SkPaint*) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000225 virtual void drawBitmapMatrix(const SkBitmap&, const SkMatrix&,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000226 const SkPaint*) SK_OVERRIDE;
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000227 virtual void drawBitmapNine(const SkBitmap& bitmap, const SkIRect& center,
228 const SkRect& dst, const SkPaint* paint = NULL) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000229 virtual void drawSprite(const SkBitmap&, int left, int top,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000230 const SkPaint*) SK_OVERRIDE;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000231 virtual void drawText(const void* text, size_t byteLength, SkScalar x,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000232 SkScalar y, const SkPaint&) SK_OVERRIDE;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000233 virtual void drawPosText(const void* text, size_t byteLength,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000234 const SkPoint pos[], const SkPaint&) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000235 virtual void drawPosTextH(const void* text, size_t byteLength,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000236 const SkScalar xpos[], SkScalar constY,
237 const SkPaint&) SK_OVERRIDE;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000238 virtual void drawTextOnPath(const void* text, size_t byteLength,
239 const SkPath& path, const SkMatrix* matrix,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000240 const SkPaint&) SK_OVERRIDE;
241 virtual void drawPicture(SkPicture& picture) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000242 virtual void drawVertices(VertexMode, int vertexCount,
243 const SkPoint vertices[], const SkPoint texs[],
244 const SkColor colors[], SkXfermode*,
245 const uint16_t indices[], int indexCount,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000246 const SkPaint&) SK_OVERRIDE;
247 virtual void drawData(const void*, size_t) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000248
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000249 /**
250 * Flatten an SkBitmap to send to the reader, where it will be referenced
251 * according to slot.
252 */
253 bool shuttleBitmap(const SkBitmap&, int32_t slot);
reed@google.combb6992a2011-04-26 17:41:56 +0000254private:
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000255 enum {
256 kNoSaveLayer = -1,
257 };
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000258 SkNamedFactorySet* fFactorySet;
259 int fFirstSaveLayerStackLevel;
scroggo@google.comd9d29672012-08-14 17:21:34 +0000260 SkBitmapHeap* fBitmapHeap;
reed@google.comacd471f2011-05-03 21:26:46 +0000261 SkGPipeController* fController;
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000262 SkWriter32& fWriter;
263 size_t fBlockSize; // amount allocated for writer
264 size_t fBytesNotified;
265 bool fDone;
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000266 const uint32_t fFlags;
reed@google.combb6992a2011-04-26 17:41:56 +0000267
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000268 SkRefCntSet fTypefaceSet;
reed@google.comf5842f72011-05-04 18:30:04 +0000269
270 uint32_t getTypefaceID(SkTypeface*);
271
reed@google.comacd471f2011-05-03 21:26:46 +0000272 inline void writeOp(DrawOps op, unsigned flags, unsigned data) {
reed@google.combb6992a2011-04-26 17:41:56 +0000273 fWriter.write32(DrawOp_packOpFlagData(op, flags, data));
274 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000275
reed@google.comacd471f2011-05-03 21:26:46 +0000276 inline void writeOp(DrawOps op) {
reed@google.combb6992a2011-04-26 17:41:56 +0000277 fWriter.write32(DrawOp_packOpFlagData(op, 0, 0));
278 }
reed@google.comacd471f2011-05-03 21:26:46 +0000279
280 bool needOpBytes(size_t size = 0);
281
282 inline void doNotify() {
283 if (!fDone) {
284 size_t bytes = fWriter.size() - fBytesNotified;
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000285 if (bytes > 0) {
286 fController->notifyWritten(bytes);
287 fBytesNotified += bytes;
288 }
reed@google.comacd471f2011-05-03 21:26:46 +0000289 }
290 }
reed@google.comb55d1182011-05-11 00:42:04 +0000291
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000292 // Should be called after any calls to an SkFlatDictionary::findAndReplace
293 // if a new SkFlatData was added when in cross process mode
294 void flattenFactoryNames();
295
scroggo@google.com4dffc592012-07-17 16:49:40 +0000296 FlattenableHeap fFlattenableHeap;
297 FlatDictionary fFlatDictionary;
reed@google.comb55d1182011-05-11 00:42:04 +0000298 int fCurrFlatIndex[kCount_PaintFlats];
299 int flattenToIndex(SkFlattenable* obj, PaintFlats);
300
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000301 // Common code used by drawBitmap*. Behaves differently depending on the
302 // type of SkBitmapHeap being used, which is determined by the flags used.
303 bool commonDrawBitmap(const SkBitmap& bm, DrawOps op, unsigned flags,
304 size_t opBytesNeeded, const SkPaint* paint);
scroggo@google.com58be6822012-07-30 14:40:01 +0000305
reed@google.com31891582011-05-12 03:03:56 +0000306 SkPaint fPaint;
307 void writePaint(const SkPaint&);
reed@google.combb6992a2011-04-26 17:41:56 +0000308
reed@google.comacd471f2011-05-03 21:26:46 +0000309 class AutoPipeNotify {
310 public:
311 AutoPipeNotify(SkGPipeCanvas* canvas) : fCanvas(canvas) {}
312 ~AutoPipeNotify() { fCanvas->doNotify(); }
313 private:
314 SkGPipeCanvas* fCanvas;
315 };
316 friend class AutoPipeNotify;
317
reed@google.combb6992a2011-04-26 17:41:56 +0000318 typedef SkCanvas INHERITED;
319};
320
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000321void SkGPipeCanvas::flattenFactoryNames() {
322 const char* name;
323 while ((name = fFactorySet->getNextAddedFactoryName()) != NULL) {
324 size_t len = strlen(name);
325 if (this->needOpBytes(len)) {
326 this->writeOp(kDef_Factory_DrawOp);
327 fWriter.writeString(name, len);
328 }
329 }
330}
331
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000332bool SkGPipeCanvas::shuttleBitmap(const SkBitmap& bm, int32_t slot) {
scroggo@google.com565254b2012-06-28 15:41:32 +0000333 SkASSERT(shouldFlattenBitmaps(fFlags));
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000334 SkOrderedWriteBuffer buffer(1024);
335 buffer.setNamedFactoryRecorder(fFactorySet);
336 bm.flatten(buffer);
337 this->flattenFactoryNames();
338 uint32_t size = buffer.size();
339 if (this->needOpBytes(size)) {
340 this->writeOp(kDef_Bitmap_DrawOp, 0, slot);
341 void* dst = static_cast<void*>(fWriter.reserve(size));
342 buffer.writeToMemory(dst);
343 return true;
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000344 }
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000345 return false;
scroggo@google.comd3ba5cc2012-07-09 16:05:53 +0000346}
347
reed@google.comb55d1182011-05-11 00:42:04 +0000348// return 0 for NULL (or unflattenable obj), or index-base-1
scroggo@google.comd3ba5cc2012-07-09 16:05:53 +0000349// return ~(index-base-1) if an old flattenable was replaced
reed@google.comb55d1182011-05-11 00:42:04 +0000350int SkGPipeCanvas::flattenToIndex(SkFlattenable* obj, PaintFlats paintflat) {
scroggo@google.comd5d158b2012-08-14 20:38:28 +0000351 SkASSERT(!fDone && fBitmapHeap != NULL);
reed@google.comb55d1182011-05-11 00:42:04 +0000352 if (NULL == obj) {
353 return 0;
354 }
reed@google.comb55d1182011-05-11 00:42:04 +0000355
scroggo@google.comd9d29672012-08-14 17:21:34 +0000356 fBitmapHeap->deferAddingOwners();
scroggo@google.com4dffc592012-07-17 16:49:40 +0000357 bool added, replaced;
scroggo@google.com664fab12012-08-14 19:22:05 +0000358 const SkFlatData* flat = fFlatDictionary.findAndReplace(*obj, fFlattenableHeap.flatToReplace(),
359 &added, &replaced);
scroggo@google.comd9d29672012-08-14 17:21:34 +0000360 fBitmapHeap->endAddingOwnersDeferral(added);
scroggo@google.com4dffc592012-07-17 16:49:40 +0000361 int index = flat->index();
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000362 if (added) {
363 if (isCrossProcess(fFlags)) {
364 this->flattenFactoryNames();
365 }
366 size_t flatSize = flat->flatSize();
367 if (this->needOpBytes(flatSize)) {
368 this->writeOp(kDef_Flattenable_DrawOp, paintflat, index);
369 fWriter.write(flat->data(), flatSize);
370 }
scroggo@google.com4dffc592012-07-17 16:49:40 +0000371 }
372 if (replaced) {
reed@google.comb55d1182011-05-11 00:42:04 +0000373 index = ~index;
reed@google.comb55d1182011-05-11 00:42:04 +0000374 }
scroggo@google.com4dffc592012-07-17 16:49:40 +0000375 return index;
reed@google.comb55d1182011-05-11 00:42:04 +0000376}
377
reed@google.combb6992a2011-04-26 17:41:56 +0000378///////////////////////////////////////////////////////////////////////////////
379
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000380/**
381 * If SkBitmaps are to be flattened to send to the reader, this class is
382 * provided to the SkBitmapHeap to tell the SkGPipeCanvas to do so.
383 */
384class BitmapShuttle : public SkBitmapHeap::ExternalStorage {
385public:
386 BitmapShuttle(SkGPipeCanvas*);
387
388 ~BitmapShuttle();
389
390 virtual bool insert(const SkBitmap& bitmap, int32_t slot) SK_OVERRIDE;
391
392private:
393 SkGPipeCanvas* fCanvas;
394};
395
396///////////////////////////////////////////////////////////////////////////////
397
reed@google.comacd471f2011-05-03 21:26:46 +0000398#define MIN_BLOCK_SIZE (16 * 1024)
scroggo@google.com4dffc592012-07-17 16:49:40 +0000399#define BITMAPS_TO_KEEP 5
400#define FLATTENABLES_TO_KEEP 10
reed@google.combb6992a2011-04-26 17:41:56 +0000401
reed@google.comacd471f2011-05-03 21:26:46 +0000402SkGPipeCanvas::SkGPipeCanvas(SkGPipeController* controller,
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000403 SkWriter32* writer, uint32_t flags)
404: fFactorySet(isCrossProcess(flags) ? SkNEW(SkNamedFactorySet) : NULL)
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000405, fWriter(*writer)
406, fFlags(flags)
scroggo@google.com664fab12012-08-14 19:22:05 +0000407, fFlattenableHeap(FLATTENABLES_TO_KEEP, fFactorySet, isCrossProcess(flags))
scroggo@google.com15543602012-08-02 18:49:49 +0000408, fFlatDictionary(&fFlattenableHeap) {
reed@google.comacd471f2011-05-03 21:26:46 +0000409 fController = controller;
reed@google.combb6992a2011-04-26 17:41:56 +0000410 fDone = false;
reed@google.comacd471f2011-05-03 21:26:46 +0000411 fBlockSize = 0; // need first block from controller
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000412 fBytesNotified = 0;
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000413 fFirstSaveLayerStackLevel = kNoSaveLayer;
reed@google.comb55d1182011-05-11 00:42:04 +0000414 sk_bzero(fCurrFlatIndex, sizeof(fCurrFlatIndex));
reed@google.comacd471f2011-05-03 21:26:46 +0000415
reed@google.combb6793b2011-05-05 15:18:15 +0000416 // we need a device to limit our clip
417 // should the caller give us the bounds?
yangsu@google.com06b4da162011-06-17 15:04:40 +0000418 // We don't allocate pixels for the bitmap
reed@google.combb6793b2011-05-05 15:18:15 +0000419 SkBitmap bitmap;
420 bitmap.setConfig(SkBitmap::kARGB_8888_Config, 32767, 32767);
yangsu@google.com06b4da162011-06-17 15:04:40 +0000421 SkDevice* device = SkNEW_ARGS(SkDevice, (bitmap));
reed@google.combb6793b2011-05-05 15:18:15 +0000422 this->setDevice(device)->unref();
scroggo@google.com10dccde2012-08-08 20:43:22 +0000423
scroggo@google.com565254b2012-06-28 15:41:32 +0000424 // Tell the reader the appropriate flags to use.
425 if (this->needOpBytes()) {
426 this->writeOp(kReportFlags_DrawOp, fFlags, 0);
427 }
scroggo@google.com10dccde2012-08-08 20:43:22 +0000428
429 if (shouldFlattenBitmaps(flags)) {
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000430 BitmapShuttle* shuttle = SkNEW_ARGS(BitmapShuttle, (this));
scroggo@google.comd9d29672012-08-14 17:21:34 +0000431 fBitmapHeap = SkNEW_ARGS(SkBitmapHeap, (shuttle, BITMAPS_TO_KEEP));
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000432 shuttle->unref();
scroggo@google.com10dccde2012-08-08 20:43:22 +0000433 } else {
scroggo@google.comd9d29672012-08-14 17:21:34 +0000434 fBitmapHeap = SkNEW_ARGS(SkBitmapHeap,
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000435 (BITMAPS_TO_KEEP, controller->numberOfReaders()));
scroggo@google.com10dccde2012-08-08 20:43:22 +0000436 if (this->needOpBytes(sizeof(void*))) {
scroggo@google.comd9d29672012-08-14 17:21:34 +0000437 this->writeOp(kShareBitmapHeap_DrawOp);
438 fWriter.writePtr(static_cast<void*>(fBitmapHeap));
scroggo@google.com10dccde2012-08-08 20:43:22 +0000439 }
440 }
scroggo@google.comd9d29672012-08-14 17:21:34 +0000441 fFlattenableHeap.setBitmapStorage(fBitmapHeap);
scroggo@google.com10dccde2012-08-08 20:43:22 +0000442 this->doNotify();
reed@google.combb6992a2011-04-26 17:41:56 +0000443}
444
445SkGPipeCanvas::~SkGPipeCanvas() {
446 this->finish();
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000447 SkSafeUnref(fFactorySet);
scroggo@google.comd9d29672012-08-14 17:21:34 +0000448 SkSafeUnref(fBitmapHeap);
reed@google.combb6992a2011-04-26 17:41:56 +0000449}
450
reed@google.comacd471f2011-05-03 21:26:46 +0000451bool SkGPipeCanvas::needOpBytes(size_t needed) {
452 if (fDone) {
453 return false;
454 }
455
456 needed += 4; // size of DrawOp atom
457 if (fWriter.size() + needed > fBlockSize) {
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000458 // Before we wipe out any data that has already been written, read it
459 // out.
460 this->doNotify();
461 size_t blockSize = SkMax32(MIN_BLOCK_SIZE, needed);
462 void* block = fController->requestBlock(blockSize, &fBlockSize);
reed@google.comacd471f2011-05-03 21:26:46 +0000463 if (NULL == block) {
464 fDone = true;
465 return false;
466 }
467 fWriter.reset(block, fBlockSize);
468 fBytesNotified = 0;
469 }
470 return true;
471}
472
reed@google.comf5842f72011-05-04 18:30:04 +0000473uint32_t SkGPipeCanvas::getTypefaceID(SkTypeface* face) {
474 uint32_t id = 0; // 0 means default/null typeface
475 if (face) {
476 id = fTypefaceSet.find(face);
477 if (0 == id) {
478 id = fTypefaceSet.add(face);
479 size_t size = writeTypeface(NULL, face);
480 if (this->needOpBytes(size)) {
reed@google.combb6793b2011-05-05 15:18:15 +0000481 this->writeOp(kDef_Typeface_DrawOp);
reed@google.comf5842f72011-05-04 18:30:04 +0000482 writeTypeface(&fWriter, face);
483 }
484 }
485 }
486 return id;
487}
488
reed@google.combb6992a2011-04-26 17:41:56 +0000489///////////////////////////////////////////////////////////////////////////////
490
reed@google.comacd471f2011-05-03 21:26:46 +0000491#define NOTIFY_SETUP(canvas) \
492 AutoPipeNotify apn(canvas)
493
reed@google.combb6992a2011-04-26 17:41:56 +0000494int SkGPipeCanvas::save(SaveFlags flags) {
reed@google.comacd471f2011-05-03 21:26:46 +0000495 NOTIFY_SETUP(this);
496 if (this->needOpBytes()) {
497 this->writeOp(kSave_DrawOp, 0, flags);
498 }
reed@google.combb6992a2011-04-26 17:41:56 +0000499 return this->INHERITED::save(flags);
500}
501
502int SkGPipeCanvas::saveLayer(const SkRect* bounds, const SkPaint* paint,
503 SaveFlags saveFlags) {
reed@google.comacd471f2011-05-03 21:26:46 +0000504 NOTIFY_SETUP(this);
505 size_t size = 0;
reed@google.combb6992a2011-04-26 17:41:56 +0000506 unsigned opFlags = 0;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000507
reed@google.combb6992a2011-04-26 17:41:56 +0000508 if (bounds) {
509 opFlags |= kSaveLayer_HasBounds_DrawOpFlag;
reed@google.comacd471f2011-05-03 21:26:46 +0000510 size += sizeof(SkRect);
reed@google.combb6992a2011-04-26 17:41:56 +0000511 }
512 if (paint) {
513 opFlags |= kSaveLayer_HasPaint_DrawOpFlag;
reed@google.com31891582011-05-12 03:03:56 +0000514 this->writePaint(*paint);
reed@google.combb6992a2011-04-26 17:41:56 +0000515 }
516
reed@google.comacd471f2011-05-03 21:26:46 +0000517 if (this->needOpBytes(size)) {
518 this->writeOp(kSaveLayer_DrawOp, opFlags, saveFlags);
519 if (bounds) {
520 fWriter.writeRect(*bounds);
521 }
reed@google.combb6992a2011-04-26 17:41:56 +0000522 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000523
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000524 if (kNoSaveLayer == fFirstSaveLayerStackLevel){
525 fFirstSaveLayerStackLevel = this->getSaveCount();
526 }
reed@google.combb6992a2011-04-26 17:41:56 +0000527 // we just pass on the save, so we don't create a layer
528 return this->INHERITED::save(saveFlags);
529}
530
531void SkGPipeCanvas::restore() {
reed@google.comacd471f2011-05-03 21:26:46 +0000532 NOTIFY_SETUP(this);
533 if (this->needOpBytes()) {
534 this->writeOp(kRestore_DrawOp);
535 }
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000536
reed@google.combb6992a2011-04-26 17:41:56 +0000537 this->INHERITED::restore();
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000538
539 if (this->getSaveCount() == fFirstSaveLayerStackLevel){
540 fFirstSaveLayerStackLevel = kNoSaveLayer;
541 }
542}
543
544bool SkGPipeCanvas::isDrawingToLayer() const {
545 return kNoSaveLayer != fFirstSaveLayerStackLevel;
reed@google.combb6992a2011-04-26 17:41:56 +0000546}
547
548bool SkGPipeCanvas::translate(SkScalar dx, SkScalar dy) {
549 if (dx || dy) {
reed@google.comacd471f2011-05-03 21:26:46 +0000550 NOTIFY_SETUP(this);
551 if (this->needOpBytes(2 * sizeof(SkScalar))) {
552 this->writeOp(kTranslate_DrawOp);
553 fWriter.writeScalar(dx);
554 fWriter.writeScalar(dy);
555 }
reed@google.combb6992a2011-04-26 17:41:56 +0000556 }
557 return this->INHERITED::translate(dx, dy);
558}
559
560bool SkGPipeCanvas::scale(SkScalar sx, SkScalar sy) {
561 if (sx || sy) {
reed@google.comacd471f2011-05-03 21:26:46 +0000562 NOTIFY_SETUP(this);
563 if (this->needOpBytes(2 * sizeof(SkScalar))) {
564 this->writeOp(kScale_DrawOp);
565 fWriter.writeScalar(sx);
566 fWriter.writeScalar(sy);
567 }
reed@google.combb6992a2011-04-26 17:41:56 +0000568 }
569 return this->INHERITED::scale(sx, sy);
570}
571
572bool SkGPipeCanvas::rotate(SkScalar degrees) {
573 if (degrees) {
reed@google.comacd471f2011-05-03 21:26:46 +0000574 NOTIFY_SETUP(this);
575 if (this->needOpBytes(sizeof(SkScalar))) {
576 this->writeOp(kRotate_DrawOp);
577 fWriter.writeScalar(degrees);
578 }
reed@google.combb6992a2011-04-26 17:41:56 +0000579 }
580 return this->INHERITED::rotate(degrees);
581}
582
583bool SkGPipeCanvas::skew(SkScalar sx, SkScalar sy) {
584 if (sx || sy) {
reed@google.comacd471f2011-05-03 21:26:46 +0000585 NOTIFY_SETUP(this);
586 if (this->needOpBytes(2 * sizeof(SkScalar))) {
587 this->writeOp(kSkew_DrawOp);
588 fWriter.writeScalar(sx);
589 fWriter.writeScalar(sy);
590 }
reed@google.combb6992a2011-04-26 17:41:56 +0000591 }
592 return this->INHERITED::skew(sx, sy);
593}
594
595bool SkGPipeCanvas::concat(const SkMatrix& matrix) {
596 if (!matrix.isIdentity()) {
reed@google.comacd471f2011-05-03 21:26:46 +0000597 NOTIFY_SETUP(this);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000598 if (this->needOpBytes(matrix.writeToMemory(NULL))) {
reed@google.comacd471f2011-05-03 21:26:46 +0000599 this->writeOp(kConcat_DrawOp);
djsollen@google.com2b2ede32012-04-12 13:24:04 +0000600 fWriter.writeMatrix(matrix);
reed@google.comacd471f2011-05-03 21:26:46 +0000601 }
reed@google.combb6992a2011-04-26 17:41:56 +0000602 }
603 return this->INHERITED::concat(matrix);
604}
605
606void SkGPipeCanvas::setMatrix(const SkMatrix& matrix) {
reed@google.comacd471f2011-05-03 21:26:46 +0000607 NOTIFY_SETUP(this);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000608 if (this->needOpBytes(matrix.writeToMemory(NULL))) {
reed@google.comacd471f2011-05-03 21:26:46 +0000609 this->writeOp(kSetMatrix_DrawOp);
djsollen@google.com2b2ede32012-04-12 13:24:04 +0000610 fWriter.writeMatrix(matrix);
reed@google.comacd471f2011-05-03 21:26:46 +0000611 }
reed@google.combb6992a2011-04-26 17:41:56 +0000612 this->INHERITED::setMatrix(matrix);
613}
614
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000615bool SkGPipeCanvas::clipRect(const SkRect& rect, SkRegion::Op rgnOp,
616 bool doAntiAlias) {
reed@google.comacd471f2011-05-03 21:26:46 +0000617 NOTIFY_SETUP(this);
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000618 if (this->needOpBytes(sizeof(SkRect)) + sizeof(bool)) {
reed@google.comacd471f2011-05-03 21:26:46 +0000619 this->writeOp(kClipRect_DrawOp, 0, rgnOp);
620 fWriter.writeRect(rect);
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000621 fWriter.writeBool(doAntiAlias);
reed@google.comacd471f2011-05-03 21:26:46 +0000622 }
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000623 return this->INHERITED::clipRect(rect, rgnOp, doAntiAlias);
reed@google.combb6992a2011-04-26 17:41:56 +0000624}
625
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000626bool SkGPipeCanvas::clipPath(const SkPath& path, SkRegion::Op rgnOp,
627 bool doAntiAlias) {
reed@google.comacd471f2011-05-03 21:26:46 +0000628 NOTIFY_SETUP(this);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000629 if (this->needOpBytes(path.writeToMemory(NULL)) + sizeof(bool)) {
reed@google.comacd471f2011-05-03 21:26:46 +0000630 this->writeOp(kClipPath_DrawOp, 0, rgnOp);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000631 fWriter.writePath(path);
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000632 fWriter.writeBool(doAntiAlias);
reed@google.comacd471f2011-05-03 21:26:46 +0000633 }
reed@google.combb6992a2011-04-26 17:41:56 +0000634 // we just pass on the bounds of the path
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000635 return this->INHERITED::clipRect(path.getBounds(), rgnOp, doAntiAlias);
reed@google.combb6992a2011-04-26 17:41:56 +0000636}
637
638bool SkGPipeCanvas::clipRegion(const SkRegion& region, SkRegion::Op rgnOp) {
reed@google.comacd471f2011-05-03 21:26:46 +0000639 NOTIFY_SETUP(this);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000640 if (this->needOpBytes(region.writeToMemory(NULL))) {
reed@google.comacd471f2011-05-03 21:26:46 +0000641 this->writeOp(kClipRegion_DrawOp, 0, rgnOp);
djsollen@google.com2b2ede32012-04-12 13:24:04 +0000642 fWriter.writeRegion(region);
reed@google.comacd471f2011-05-03 21:26:46 +0000643 }
reed@google.combb6992a2011-04-26 17:41:56 +0000644 return this->INHERITED::clipRegion(region, rgnOp);
645}
646
647///////////////////////////////////////////////////////////////////////////////
648
649void SkGPipeCanvas::clear(SkColor color) {
reed@google.comacd471f2011-05-03 21:26:46 +0000650 NOTIFY_SETUP(this);
reed@google.combb6992a2011-04-26 17:41:56 +0000651 unsigned flags = 0;
652 if (color) {
653 flags |= kClear_HasColor_DrawOpFlag;
654 }
reed@google.comacd471f2011-05-03 21:26:46 +0000655 if (this->needOpBytes(sizeof(SkColor))) {
656 this->writeOp(kDrawClear_DrawOp, flags, 0);
657 if (color) {
658 fWriter.write32(color);
659 }
reed@google.combb6992a2011-04-26 17:41:56 +0000660 }
661}
662
663void SkGPipeCanvas::drawPaint(const SkPaint& paint) {
reed@google.comacd471f2011-05-03 21:26:46 +0000664 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000665 this->writePaint(paint);
reed@google.comacd471f2011-05-03 21:26:46 +0000666 if (this->needOpBytes()) {
reed@google.com31891582011-05-12 03:03:56 +0000667 this->writeOp(kDrawPaint_DrawOp);
reed@google.comacd471f2011-05-03 21:26:46 +0000668 }
reed@google.combb6992a2011-04-26 17:41:56 +0000669}
670
671void SkGPipeCanvas::drawPoints(PointMode mode, size_t count,
672 const SkPoint pts[], const SkPaint& paint) {
673 if (count) {
reed@google.comacd471f2011-05-03 21:26:46 +0000674 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000675 this->writePaint(paint);
reed@google.comacd471f2011-05-03 21:26:46 +0000676 if (this->needOpBytes(4 + count * sizeof(SkPoint))) {
reed@google.com31891582011-05-12 03:03:56 +0000677 this->writeOp(kDrawPoints_DrawOp, mode, 0);
reed@google.comacd471f2011-05-03 21:26:46 +0000678 fWriter.write32(count);
679 fWriter.write(pts, count * sizeof(SkPoint));
680 }
reed@google.combb6992a2011-04-26 17:41:56 +0000681 }
682}
683
684void SkGPipeCanvas::drawRect(const SkRect& rect, const SkPaint& paint) {
reed@google.comacd471f2011-05-03 21:26:46 +0000685 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000686 this->writePaint(paint);
reed@google.comacd471f2011-05-03 21:26:46 +0000687 if (this->needOpBytes(sizeof(SkRect))) {
reed@google.com31891582011-05-12 03:03:56 +0000688 this->writeOp(kDrawRect_DrawOp);
reed@google.comacd471f2011-05-03 21:26:46 +0000689 fWriter.writeRect(rect);
690 }
reed@google.combb6992a2011-04-26 17:41:56 +0000691}
692
693void SkGPipeCanvas::drawPath(const SkPath& path, const SkPaint& paint) {
reed@google.comacd471f2011-05-03 21:26:46 +0000694 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000695 this->writePaint(paint);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000696 if (this->needOpBytes(path.writeToMemory(NULL))) {
reed@google.com31891582011-05-12 03:03:56 +0000697 this->writeOp(kDrawPath_DrawOp);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000698 fWriter.writePath(path);
reed@google.comacd471f2011-05-03 21:26:46 +0000699 }
reed@google.combb6992a2011-04-26 17:41:56 +0000700}
701
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000702bool SkGPipeCanvas::commonDrawBitmap(const SkBitmap& bm, DrawOps op,
703 unsigned flags,
704 size_t opBytesNeeded,
705 const SkPaint* paint) {
scroggo@google.com58be6822012-07-30 14:40:01 +0000706 if (paint != NULL) {
707 flags |= kDrawBitmap_HasPaint_DrawOpsFlag;
708 this->writePaint(*paint);
709 }
scroggo@google.com10dccde2012-08-08 20:43:22 +0000710 if (this->needOpBytes(opBytesNeeded)) {
scroggo@google.comd5d158b2012-08-14 20:38:28 +0000711 SkASSERT(fBitmapHeap != NULL);
712 int32_t bitmapIndex = fBitmapHeap->insert(bm);
713 if (SkBitmapHeap::INVALID_SLOT == bitmapIndex) {
714 return false;
715 }
scroggo@google.com10dccde2012-08-08 20:43:22 +0000716 this->writeOp(op, flags, bitmapIndex);
scroggo@google.com58be6822012-07-30 14:40:01 +0000717 return true;
718 }
719 return false;
720}
721
722void SkGPipeCanvas::drawBitmap(const SkBitmap& bm, SkScalar left, SkScalar top,
723 const SkPaint* paint) {
724 NOTIFY_SETUP(this);
725 size_t opBytesNeeded = sizeof(SkScalar) * 2;
726
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000727 if (this->commonDrawBitmap(bm, kDrawBitmap_DrawOp, 0, opBytesNeeded, paint)) {
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000728 fWriter.writeScalar(left);
729 fWriter.writeScalar(top);
730 }
reed@google.combb6992a2011-04-26 17:41:56 +0000731}
732
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000733void SkGPipeCanvas::drawBitmapRect(const SkBitmap& bm, const SkIRect* src,
scroggo@google.com58be6822012-07-30 14:40:01 +0000734 const SkRect& dst, const SkPaint* paint) {
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000735 NOTIFY_SETUP(this);
scroggo@google.com58be6822012-07-30 14:40:01 +0000736 size_t opBytesNeeded = sizeof(SkRect);
737 bool hasSrc = src != NULL;
738 unsigned flags;
739 if (hasSrc) {
740 flags = kDrawBitmap_HasSrcRect_DrawOpsFlag;
741 opBytesNeeded += sizeof(int32_t) * 4;
742 } else {
743 flags = 0;
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000744 }
scroggo@google.com58be6822012-07-30 14:40:01 +0000745
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000746 if (this->commonDrawBitmap(bm, kDrawBitmapRect_DrawOp, flags, opBytesNeeded, paint)) {
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000747 if (hasSrc) {
748 fWriter.write32(src->fLeft);
749 fWriter.write32(src->fTop);
750 fWriter.write32(src->fRight);
751 fWriter.write32(src->fBottom);
752 }
753 fWriter.writeRect(dst);
754 }
reed@google.combb6992a2011-04-26 17:41:56 +0000755}
756
scroggo@google.com58be6822012-07-30 14:40:01 +0000757void SkGPipeCanvas::drawBitmapMatrix(const SkBitmap& bm, const SkMatrix& matrix,
758 const SkPaint* paint) {
759 NOTIFY_SETUP(this);
760 size_t opBytesNeeded = matrix.writeToMemory(NULL);
761
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000762 if (this->commonDrawBitmap(bm, kDrawBitmapMatrix_DrawOp, 0, opBytesNeeded, paint)) {
scroggo@google.com58be6822012-07-30 14:40:01 +0000763 fWriter.writeMatrix(matrix);
764 }
reed@google.combb6992a2011-04-26 17:41:56 +0000765}
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000766
767void SkGPipeCanvas::drawBitmapNine(const SkBitmap& bm, const SkIRect& center,
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000768 const SkRect& dst, const SkPaint* paint) {
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000769 NOTIFY_SETUP(this);
scroggo@google.com58be6822012-07-30 14:40:01 +0000770 size_t opBytesNeeded = sizeof(int32_t) * 4 + sizeof(SkRect);
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000771
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000772 if (this->commonDrawBitmap(bm, kDrawBitmapNine_DrawOp, 0, opBytesNeeded, paint)) {
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000773 fWriter.write32(center.fLeft);
774 fWriter.write32(center.fTop);
775 fWriter.write32(center.fRight);
776 fWriter.write32(center.fBottom);
777 fWriter.writeRect(dst);
778 }
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000779}
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000780
781void SkGPipeCanvas::drawSprite(const SkBitmap& bm, int left, int top,
782 const SkPaint* paint) {
783 NOTIFY_SETUP(this);
scroggo@google.com58be6822012-07-30 14:40:01 +0000784 size_t opBytesNeeded = sizeof(int32_t) * 2;
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000785
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000786 if (this->commonDrawBitmap(bm, kDrawSprite_DrawOp, 0, opBytesNeeded, paint)) {
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000787 fWriter.write32(left);
788 fWriter.write32(top);
789 }
reed@google.combb6992a2011-04-26 17:41:56 +0000790}
791
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000792void SkGPipeCanvas::drawText(const void* text, size_t byteLength, SkScalar x,
reed@google.combb6992a2011-04-26 17:41:56 +0000793 SkScalar y, const SkPaint& paint) {
794 if (byteLength) {
reed@google.comacd471f2011-05-03 21:26:46 +0000795 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000796 this->writePaint(paint);
reed@google.comacd471f2011-05-03 21:26:46 +0000797 if (this->needOpBytes(4 + SkAlign4(byteLength) + 2 * sizeof(SkScalar))) {
reed@google.com31891582011-05-12 03:03:56 +0000798 this->writeOp(kDrawText_DrawOp);
reed@google.comacd471f2011-05-03 21:26:46 +0000799 fWriter.write32(byteLength);
800 fWriter.writePad(text, byteLength);
801 fWriter.writeScalar(x);
802 fWriter.writeScalar(y);
803 }
reed@google.combb6992a2011-04-26 17:41:56 +0000804 }
805}
806
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000807void SkGPipeCanvas::drawPosText(const void* text, size_t byteLength,
reed@google.combb6992a2011-04-26 17:41:56 +0000808 const SkPoint pos[], const SkPaint& paint) {
809 if (byteLength) {
reed@google.comacd471f2011-05-03 21:26:46 +0000810 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000811 this->writePaint(paint);
reed@google.combb6992a2011-04-26 17:41:56 +0000812 int count = paint.textToGlyphs(text, byteLength, NULL);
reed@google.comacd471f2011-05-03 21:26:46 +0000813 if (this->needOpBytes(4 + SkAlign4(byteLength) + 4 + count * sizeof(SkPoint))) {
reed@google.com31891582011-05-12 03:03:56 +0000814 this->writeOp(kDrawPosText_DrawOp);
reed@google.comacd471f2011-05-03 21:26:46 +0000815 fWriter.write32(byteLength);
816 fWriter.writePad(text, byteLength);
817 fWriter.write32(count);
818 fWriter.write(pos, count * sizeof(SkPoint));
819 }
reed@google.combb6992a2011-04-26 17:41:56 +0000820 }
821}
822
823void SkGPipeCanvas::drawPosTextH(const void* text, size_t byteLength,
824 const SkScalar xpos[], SkScalar constY,
825 const SkPaint& paint) {
826 if (byteLength) {
reed@google.comacd471f2011-05-03 21:26:46 +0000827 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000828 this->writePaint(paint);
reed@google.combb6992a2011-04-26 17:41:56 +0000829 int count = paint.textToGlyphs(text, byteLength, NULL);
reed@google.comacd471f2011-05-03 21:26:46 +0000830 if (this->needOpBytes(4 + SkAlign4(byteLength) + 4 + count * sizeof(SkScalar) + 4)) {
reed@google.com31891582011-05-12 03:03:56 +0000831 this->writeOp(kDrawPosTextH_DrawOp);
reed@google.comacd471f2011-05-03 21:26:46 +0000832 fWriter.write32(byteLength);
833 fWriter.writePad(text, byteLength);
834 fWriter.write32(count);
835 fWriter.write(xpos, count * sizeof(SkScalar));
836 fWriter.writeScalar(constY);
837 }
reed@google.combb6992a2011-04-26 17:41:56 +0000838 }
839}
840
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000841void SkGPipeCanvas::drawTextOnPath(const void* text, size_t byteLength,
842 const SkPath& path, const SkMatrix* matrix,
reed@google.combb6992a2011-04-26 17:41:56 +0000843 const SkPaint& paint) {
844 if (byteLength) {
reed@google.comacd471f2011-05-03 21:26:46 +0000845 NOTIFY_SETUP(this);
reed@google.combb6992a2011-04-26 17:41:56 +0000846 unsigned flags = 0;
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000847 size_t size = 4 + SkAlign4(byteLength) + path.writeToMemory(NULL);
reed@google.combb6992a2011-04-26 17:41:56 +0000848 if (matrix) {
849 flags |= kDrawTextOnPath_HasMatrix_DrawOpFlag;
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000850 size += matrix->writeToMemory(NULL);
reed@google.combb6992a2011-04-26 17:41:56 +0000851 }
reed@google.com31891582011-05-12 03:03:56 +0000852 this->writePaint(paint);
reed@google.comacd471f2011-05-03 21:26:46 +0000853 if (this->needOpBytes(size)) {
reed@google.com31891582011-05-12 03:03:56 +0000854 this->writeOp(kDrawTextOnPath_DrawOp, flags, 0);
reed@google.combb6992a2011-04-26 17:41:56 +0000855
reed@google.comacd471f2011-05-03 21:26:46 +0000856 fWriter.write32(byteLength);
857 fWriter.writePad(text, byteLength);
reed@google.combb6992a2011-04-26 17:41:56 +0000858
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000859 fWriter.writePath(path);
reed@google.comacd471f2011-05-03 21:26:46 +0000860 if (matrix) {
djsollen@google.com2b2ede32012-04-12 13:24:04 +0000861 fWriter.writeMatrix(*matrix);
reed@google.comacd471f2011-05-03 21:26:46 +0000862 }
reed@google.combb6992a2011-04-26 17:41:56 +0000863 }
864 }
865}
866
867void SkGPipeCanvas::drawPicture(SkPicture& picture) {
reed@google.com0faac1e2011-05-11 05:58:58 +0000868 // we want to playback the picture into individual draw calls
869 this->INHERITED::drawPicture(picture);
reed@google.combb6992a2011-04-26 17:41:56 +0000870}
871
reed@google.combb6992a2011-04-26 17:41:56 +0000872void SkGPipeCanvas::drawVertices(VertexMode mode, int vertexCount,
873 const SkPoint vertices[], const SkPoint texs[],
874 const SkColor colors[], SkXfermode*,
875 const uint16_t indices[], int indexCount,
876 const SkPaint& paint) {
877 if (0 == vertexCount) {
878 return;
879 }
880
reed@google.comacd471f2011-05-03 21:26:46 +0000881 NOTIFY_SETUP(this);
882 size_t size = 4 + vertexCount * sizeof(SkPoint);
reed@google.com31891582011-05-12 03:03:56 +0000883 this->writePaint(paint);
reed@google.combb6992a2011-04-26 17:41:56 +0000884 unsigned flags = 0;
885 if (texs) {
886 flags |= kDrawVertices_HasTexs_DrawOpFlag;
reed@google.comacd471f2011-05-03 21:26:46 +0000887 size += vertexCount * sizeof(SkPoint);
reed@google.combb6992a2011-04-26 17:41:56 +0000888 }
889 if (colors) {
890 flags |= kDrawVertices_HasColors_DrawOpFlag;
reed@google.comacd471f2011-05-03 21:26:46 +0000891 size += vertexCount * sizeof(SkColor);
reed@google.combb6992a2011-04-26 17:41:56 +0000892 }
893 if (indices && indexCount > 0) {
894 flags |= kDrawVertices_HasIndices_DrawOpFlag;
reed@google.comacd471f2011-05-03 21:26:46 +0000895 size += 4 + SkAlign4(indexCount * sizeof(uint16_t));
reed@google.combb6992a2011-04-26 17:41:56 +0000896 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000897
reed@google.comacd471f2011-05-03 21:26:46 +0000898 if (this->needOpBytes(size)) {
reed@google.com31891582011-05-12 03:03:56 +0000899 this->writeOp(kDrawVertices_DrawOp, flags, 0);
reed@google.comacd471f2011-05-03 21:26:46 +0000900 fWriter.write32(mode);
901 fWriter.write32(vertexCount);
902 fWriter.write(vertices, vertexCount * sizeof(SkPoint));
903 if (texs) {
904 fWriter.write(texs, vertexCount * sizeof(SkPoint));
905 }
906 if (colors) {
907 fWriter.write(colors, vertexCount * sizeof(SkColor));
908 }
reed@google.combb6992a2011-04-26 17:41:56 +0000909
reed@google.comacd471f2011-05-03 21:26:46 +0000910 // TODO: flatten xfermode
reed@google.combb6992a2011-04-26 17:41:56 +0000911
reed@google.comacd471f2011-05-03 21:26:46 +0000912 if (indices && indexCount > 0) {
913 fWriter.write32(indexCount);
914 fWriter.writePad(indices, indexCount * sizeof(uint16_t));
915 }
reed@google.combb6992a2011-04-26 17:41:56 +0000916 }
917}
918
reed@google.comacd471f2011-05-03 21:26:46 +0000919void SkGPipeCanvas::drawData(const void* ptr, size_t size) {
920 if (size && ptr) {
921 NOTIFY_SETUP(this);
reed@google.combb6992a2011-04-26 17:41:56 +0000922 unsigned data = 0;
923 if (size < (1 << DRAWOPS_DATA_BITS)) {
924 data = (unsigned)size;
925 }
reed@google.comacd471f2011-05-03 21:26:46 +0000926 if (this->needOpBytes(4 + SkAlign4(size))) {
927 this->writeOp(kDrawData_DrawOp, 0, data);
928 if (0 == data) {
929 fWriter.write32(size);
930 }
reed@google.combb6793b2011-05-05 15:18:15 +0000931 fWriter.writePad(ptr, size);
reed@google.combb6992a2011-04-26 17:41:56 +0000932 }
933 }
934}
935
junov@chromium.org77eec242012-07-18 17:54:45 +0000936void SkGPipeCanvas::flushRecording(bool detachCurrentBlock) {
937 doNotify();
938 if (detachCurrentBlock) {
939 // force a new block to be requested for the next recorded command
940 fBlockSize = 0;
941 }
942}
943
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000944size_t SkGPipeCanvas::freeMemoryIfPossible(size_t bytesToFree) {
scroggo@google.comd5d158b2012-08-14 20:38:28 +0000945 return (NULL == fBitmapHeap) ? 0 : fBitmapHeap->freeMemoryIfPossible(bytesToFree);
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000946}
947
reed@google.combb6992a2011-04-26 17:41:56 +0000948///////////////////////////////////////////////////////////////////////////////
949
950template <typename T> uint32_t castToU32(T value) {
951 union {
952 T fSrc;
953 uint32_t fDst;
954 } data;
955 data.fSrc = value;
956 return data.fDst;
957}
958
reed@google.com31891582011-05-12 03:03:56 +0000959void SkGPipeCanvas::writePaint(const SkPaint& paint) {
scroggo@google.comd5d158b2012-08-14 20:38:28 +0000960 if (fDone) {
961 return;
962 }
reed@google.com31891582011-05-12 03:03:56 +0000963 SkPaint& base = fPaint;
reed@google.combb6992a2011-04-26 17:41:56 +0000964 uint32_t storage[32];
965 uint32_t* ptr = storage;
reed@google.combb6992a2011-04-26 17:41:56 +0000966
967 if (base.getFlags() != paint.getFlags()) {
reed@google.combb6992a2011-04-26 17:41:56 +0000968 *ptr++ = PaintOp_packOpData(kFlags_PaintOp, paint.getFlags());
reed@google.comf5842f72011-05-04 18:30:04 +0000969 base.setFlags(paint.getFlags());
reed@google.combb6992a2011-04-26 17:41:56 +0000970 }
971 if (base.getColor() != paint.getColor()) {
reed@google.combb6992a2011-04-26 17:41:56 +0000972 *ptr++ = PaintOp_packOp(kColor_PaintOp);
973 *ptr++ = paint.getColor();
reed@google.comf5842f72011-05-04 18:30:04 +0000974 base.setColor(paint.getColor());
reed@google.combb6992a2011-04-26 17:41:56 +0000975 }
976 if (base.getStyle() != paint.getStyle()) {
reed@google.combb6992a2011-04-26 17:41:56 +0000977 *ptr++ = PaintOp_packOpData(kStyle_PaintOp, paint.getStyle());
reed@google.comf5842f72011-05-04 18:30:04 +0000978 base.setStyle(paint.getStyle());
reed@google.combb6992a2011-04-26 17:41:56 +0000979 }
980 if (base.getStrokeJoin() != paint.getStrokeJoin()) {
reed@google.combb6992a2011-04-26 17:41:56 +0000981 *ptr++ = PaintOp_packOpData(kJoin_PaintOp, paint.getStrokeJoin());
reed@google.comf5842f72011-05-04 18:30:04 +0000982 base.setStrokeJoin(paint.getStrokeJoin());
reed@google.combb6992a2011-04-26 17:41:56 +0000983 }
984 if (base.getStrokeCap() != paint.getStrokeCap()) {
reed@google.combb6992a2011-04-26 17:41:56 +0000985 *ptr++ = PaintOp_packOpData(kCap_PaintOp, paint.getStrokeCap());
reed@google.comf5842f72011-05-04 18:30:04 +0000986 base.setStrokeCap(paint.getStrokeCap());
reed@google.combb6992a2011-04-26 17:41:56 +0000987 }
988 if (base.getStrokeWidth() != paint.getStrokeWidth()) {
reed@google.combb6992a2011-04-26 17:41:56 +0000989 *ptr++ = PaintOp_packOp(kWidth_PaintOp);
990 *ptr++ = castToU32(paint.getStrokeWidth());
reed@google.comf5842f72011-05-04 18:30:04 +0000991 base.setStrokeWidth(paint.getStrokeWidth());
reed@google.combb6992a2011-04-26 17:41:56 +0000992 }
993 if (base.getStrokeMiter() != paint.getStrokeMiter()) {
reed@google.combb6992a2011-04-26 17:41:56 +0000994 *ptr++ = PaintOp_packOp(kMiter_PaintOp);
995 *ptr++ = castToU32(paint.getStrokeMiter());
reed@google.comf5842f72011-05-04 18:30:04 +0000996 base.setStrokeMiter(paint.getStrokeMiter());
reed@google.combb6992a2011-04-26 17:41:56 +0000997 }
998 if (base.getTextEncoding() != paint.getTextEncoding()) {
reed@google.combb6992a2011-04-26 17:41:56 +0000999 *ptr++ = PaintOp_packOpData(kEncoding_PaintOp, paint.getTextEncoding());
reed@google.comf5842f72011-05-04 18:30:04 +00001000 base.setTextEncoding(paint.getTextEncoding());
reed@google.combb6992a2011-04-26 17:41:56 +00001001 }
1002 if (base.getHinting() != paint.getHinting()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001003 *ptr++ = PaintOp_packOpData(kHinting_PaintOp, paint.getHinting());
reed@google.comf5842f72011-05-04 18:30:04 +00001004 base.setHinting(paint.getHinting());
reed@google.combb6992a2011-04-26 17:41:56 +00001005 }
1006 if (base.getTextAlign() != paint.getTextAlign()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001007 *ptr++ = PaintOp_packOpData(kAlign_PaintOp, paint.getTextAlign());
reed@google.comf5842f72011-05-04 18:30:04 +00001008 base.setTextAlign(paint.getTextAlign());
reed@google.combb6992a2011-04-26 17:41:56 +00001009 }
1010 if (base.getTextSize() != paint.getTextSize()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001011 *ptr++ = PaintOp_packOp(kTextSize_PaintOp);
1012 *ptr++ = castToU32(paint.getTextSize());
reed@google.comf5842f72011-05-04 18:30:04 +00001013 base.setTextSize(paint.getTextSize());
reed@google.combb6992a2011-04-26 17:41:56 +00001014 }
1015 if (base.getTextScaleX() != paint.getTextScaleX()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001016 *ptr++ = PaintOp_packOp(kTextScaleX_PaintOp);
1017 *ptr++ = castToU32(paint.getTextScaleX());
reed@google.comf5842f72011-05-04 18:30:04 +00001018 base.setTextScaleX(paint.getTextScaleX());
reed@google.combb6992a2011-04-26 17:41:56 +00001019 }
1020 if (base.getTextSkewX() != paint.getTextSkewX()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001021 *ptr++ = PaintOp_packOp(kTextSkewX_PaintOp);
1022 *ptr++ = castToU32(paint.getTextSkewX());
reed@google.comf5842f72011-05-04 18:30:04 +00001023 base.setTextSkewX(paint.getTextSkewX());
1024 }
1025
1026 if (!SkTypeface::Equal(base.getTypeface(), paint.getTypeface())) {
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +00001027 if (isCrossProcess(fFlags)) {
scroggo@google.com3cb969f2012-07-27 20:39:19 +00001028 uint32_t id = this->getTypefaceID(paint.getTypeface());
1029 *ptr++ = PaintOp_packOpData(kTypeface_PaintOp, id);
1030 } else if (this->needOpBytes(sizeof(void*))) {
1031 // Add to the set for ref counting.
1032 fTypefaceSet.add(paint.getTypeface());
1033 // It is safe to write the typeface to the stream before the rest
1034 // of the paint unless we ever send a kReset_PaintOp, which we
1035 // currently never do.
1036 this->writeOp(kSetTypeface_DrawOp);
1037 fWriter.writePtr(paint.getTypeface());
1038 }
reed@google.comf5842f72011-05-04 18:30:04 +00001039 base.setTypeface(paint.getTypeface());
reed@google.combb6992a2011-04-26 17:41:56 +00001040 }
reed@google.combb6992a2011-04-26 17:41:56 +00001041
scroggo@google.com4dffc592012-07-17 16:49:40 +00001042 // This is a new paint, so all old flats can be safely purged, if necessary.
1043 fFlattenableHeap.markAllFlatsSafeToDelete();
reed@google.comb55d1182011-05-11 00:42:04 +00001044 for (int i = 0; i < kCount_PaintFlats; i++) {
1045 int index = this->flattenToIndex(get_paintflat(paint, i), (PaintFlats)i);
scroggo@google.comd3ba5cc2012-07-09 16:05:53 +00001046 bool replaced = index < 0;
1047 if (replaced) {
1048 index = ~index;
1049 }
scroggo@google.com4dffc592012-07-17 16:49:40 +00001050 // Store the index of any flat that needs to be kept. 0 means no flat.
1051 if (index > 0) {
1052 fFlattenableHeap.markFlatForKeeping(index);
1053 }
1054 SkASSERT(index >= 0 && index <= fFlatDictionary.count());
scroggo@google.comd3ba5cc2012-07-09 16:05:53 +00001055 if (index != fCurrFlatIndex[i] || replaced) {
reed@google.comb55d1182011-05-11 00:42:04 +00001056 *ptr++ = PaintOp_packOpFlagData(kFlatIndex_PaintOp, i, index);
1057 fCurrFlatIndex[i] = index;
1058 }
1059 }
1060
reed@google.comacd471f2011-05-03 21:26:46 +00001061 size_t size = (char*)ptr - (char*)storage;
1062 if (size && this->needOpBytes(size)) {
reed@google.comb55d1182011-05-11 00:42:04 +00001063 this->writeOp(kPaintOp_DrawOp, 0, size);
reed@google.comb55d1182011-05-11 00:42:04 +00001064 fWriter.write(storage, size);
reed@google.combb6992a2011-04-26 17:41:56 +00001065 for (size_t i = 0; i < size/4; i++) {
reed@google.comb55d1182011-05-11 00:42:04 +00001066// SkDebugf("[%d] %08X\n", i, storage[i]);
reed@google.combb6992a2011-04-26 17:41:56 +00001067 }
1068 }
reed@google.combb6992a2011-04-26 17:41:56 +00001069}
1070
1071///////////////////////////////////////////////////////////////////////////////
1072
1073#include "SkGPipe.h"
1074
scroggo@google.com3cb969f2012-07-27 20:39:19 +00001075SkGPipeController::~SkGPipeController() {
1076 SkSafeUnref(fCanvas);
1077}
1078
1079void SkGPipeController::setCanvas(SkGPipeCanvas* canvas) {
1080 SkRefCnt_SafeAssign(fCanvas, canvas);
1081}
1082
1083///////////////////////////////////////////////////////////////////////////////
1084
1085SkGPipeWriter::SkGPipeWriter()
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +00001086: fWriter(0) {
reed@google.combb6992a2011-04-26 17:41:56 +00001087 fCanvas = NULL;
1088}
1089
1090SkGPipeWriter::~SkGPipeWriter() {
reed@google.comacd471f2011-05-03 21:26:46 +00001091 this->endRecording();
reed@google.combb6992a2011-04-26 17:41:56 +00001092}
1093
scroggo@google.com565254b2012-06-28 15:41:32 +00001094SkCanvas* SkGPipeWriter::startRecording(SkGPipeController* controller, uint32_t flags) {
reed@google.combb6992a2011-04-26 17:41:56 +00001095 if (NULL == fCanvas) {
reed@google.comacd471f2011-05-03 21:26:46 +00001096 fWriter.reset(NULL, 0);
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +00001097 fCanvas = SkNEW_ARGS(SkGPipeCanvas, (controller, &fWriter, flags));
reed@google.combb6992a2011-04-26 17:41:56 +00001098 }
scroggo@google.com3cb969f2012-07-27 20:39:19 +00001099 controller->setCanvas(fCanvas);
reed@google.combb6992a2011-04-26 17:41:56 +00001100 return fCanvas;
1101}
1102
1103void SkGPipeWriter::endRecording() {
1104 if (fCanvas) {
1105 fCanvas->finish();
1106 fCanvas->unref();
1107 fCanvas = NULL;
1108 }
1109}
1110
junov@chromium.org2e14ba82012-08-07 14:26:57 +00001111void SkGPipeWriter::flushRecording(bool detachCurrentBlock) {
1112 if (fCanvas) {
1113 fCanvas->flushRecording(detachCurrentBlock);
1114 }
junov@chromium.org77eec242012-07-18 17:54:45 +00001115}
1116
junov@chromium.org2e14ba82012-08-07 14:26:57 +00001117size_t SkGPipeWriter::freeMemoryIfPossible(size_t bytesToFree) {
1118 if (fCanvas) {
1119 return fCanvas->freeMemoryIfPossible(bytesToFree);
1120 }
1121 return 0;
1122}
1123
1124size_t SkGPipeWriter::storageAllocatedForRecording() const {
scroggo@google.com15011ee2012-07-26 20:03:32 +00001125 return NULL == fCanvas ? 0 : fCanvas->storageAllocatedForRecording();
1126}
1127
scroggo@google.com3e26bd02012-08-14 15:20:01 +00001128///////////////////////////////////////////////////////////////////////////////
1129
1130BitmapShuttle::BitmapShuttle(SkGPipeCanvas* canvas) {
1131 SkASSERT(canvas != NULL);
1132 fCanvas = canvas;
1133 fCanvas->ref();
1134}
1135
1136BitmapShuttle::~BitmapShuttle() {
1137 fCanvas->unref();
1138}
1139
1140bool BitmapShuttle::insert(const SkBitmap& bitmap, int32_t slot) {
1141 return fCanvas->shuttleBitmap(bitmap, slot);
1142}