blob: f646babda52b3916dce8ede5a095f912b53d8dcf [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
epoger@google.comb58772f2013-03-08 09:09:10 +00009#include "SkAnnotation.h"
scroggo@google.com10dccde2012-08-08 20:43:22 +000010#include "SkBitmapHeap.h"
reed@google.combb6992a2011-04-26 17:41:56 +000011#include "SkCanvas.h"
scroggo@google.com10dccde2012-08-08 20:43:22 +000012#include "SkColorFilter.h"
reed@google.com8a85d0c2011-06-24 19:12:12 +000013#include "SkData.h"
scroggo@google.com10dccde2012-08-08 20:43:22 +000014#include "SkDrawLooper.h"
reed@google.combb6793b2011-05-05 15:18:15 +000015#include "SkDevice.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"
scroggo@google.com10dccde2012-08-08 20:43:22 +000019#include "SkMaskFilter.h"
20#include "SkOrderedWriteBuffer.h"
21#include "SkPaint.h"
22#include "SkPathEffect.h"
23#include "SkPictureFlat.h"
24#include "SkRasterizer.h"
reed@google.com4ed0fb72012-12-12 20:48:18 +000025#include "SkRRect.h"
scroggo@google.com10dccde2012-08-08 20:43:22 +000026#include "SkShader.h"
reed@google.comf5842f72011-05-04 18:30:04 +000027#include "SkStream.h"
reed@google.comb55d1182011-05-11 00:42:04 +000028#include "SkTSearch.h"
reed@google.comf5842f72011-05-04 18:30:04 +000029#include "SkTypeface.h"
reed@google.combb6992a2011-04-26 17:41:56 +000030#include "SkWriter32.h"
reed@google.comb55d1182011-05-11 00:42:04 +000031
reed@google.com4ed0fb72012-12-12 20:48:18 +000032enum {
33 kSizeOfFlatRRect = sizeof(SkRect) + 4 * sizeof(SkVector)
34};
35
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +000036static bool isCrossProcess(uint32_t flags) {
37 return SkToBool(flags & SkGPipeWriter::kCrossProcess_Flag);
38}
39
reed@google.comb55d1182011-05-11 00:42:04 +000040static SkFlattenable* get_paintflat(const SkPaint& paint, unsigned paintFlat) {
41 SkASSERT(paintFlat < kCount_PaintFlats);
42 switch (paintFlat) {
43 case kColorFilter_PaintFlat: return paint.getColorFilter();
reed@google.com0faac1e2011-05-11 05:58:58 +000044 case kDrawLooper_PaintFlat: return paint.getLooper();
reed@google.comb55d1182011-05-11 00:42:04 +000045 case kMaskFilter_PaintFlat: return paint.getMaskFilter();
46 case kPathEffect_PaintFlat: return paint.getPathEffect();
47 case kRasterizer_PaintFlat: return paint.getRasterizer();
48 case kShader_PaintFlat: return paint.getShader();
scroggo@google.com16d1d0b2012-05-02 19:09:40 +000049 case kImageFilter_PaintFlat: return paint.getImageFilter();
reed@google.comb55d1182011-05-11 00:42:04 +000050 case kXfermode_PaintFlat: return paint.getXfermode();
epoger@google.comb58772f2013-03-08 09:09:10 +000051 case kAnnotation_PaintFlat: return paint.getAnnotation();
reed@google.comb55d1182011-05-11 00:42:04 +000052 }
tomhudson@google.com0c00f212011-12-28 14:59:50 +000053 SkDEBUGFAIL("never gets here");
reed@google.comb55d1182011-05-11 00:42:04 +000054 return NULL;
55}
reed@google.combb6992a2011-04-26 17:41:56 +000056
reed@google.comf5842f72011-05-04 18:30:04 +000057static size_t writeTypeface(SkWriter32* writer, SkTypeface* typeface) {
58 SkASSERT(typeface);
59 SkDynamicMemoryWStream stream;
60 typeface->serialize(&stream);
61 size_t size = stream.getOffset();
62 if (writer) {
63 writer->write32(size);
reed@google.com8a85d0c2011-06-24 19:12:12 +000064 SkAutoDataUnref data(stream.copyToData());
robertphillips@google.com59f46b82012-07-10 17:30:58 +000065 writer->writePad(data->data(), size);
reed@google.comf5842f72011-05-04 18:30:04 +000066 }
scroggo@google.com5af9b202012-06-04 17:17:36 +000067 return 4 + SkAlign4(size);
reed@google.comf5842f72011-05-04 18:30:04 +000068}
69
reed@google.combb6992a2011-04-26 17:41:56 +000070///////////////////////////////////////////////////////////////////////////////
71
scroggo@google.com4dffc592012-07-17 16:49:40 +000072class FlattenableHeap : public SkFlatController {
73public:
scroggo@google.com664fab12012-08-14 19:22:05 +000074 FlattenableHeap(int numFlatsToKeep, SkNamedFactorySet* fset, bool isCrossProcess)
scroggo@google.com15543602012-08-02 18:49:49 +000075 : fNumFlatsToKeep(numFlatsToKeep) {
scroggo@google.com664fab12012-08-14 19:22:05 +000076 SkASSERT((isCrossProcess && fset != NULL) || (!isCrossProcess && NULL == fset));
77 if (isCrossProcess) {
78 this->setNamedFactorySet(fset);
79 this->setWriteBufferFlags(SkFlattenableWriteBuffer::kCrossProcess_Flag);
80 }
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +000081 }
scroggo@google.com4dffc592012-07-17 16:49:40 +000082
83 ~FlattenableHeap() {
84 fPointers.freeAll();
85 }
86
87 virtual void* allocThrow(size_t bytes) SK_OVERRIDE;
88
89 virtual void unalloc(void* ptr) SK_OVERRIDE;
90
scroggo@google.com7ca24432012-08-14 15:48:43 +000091 void setBitmapStorage(SkBitmapHeap* heap) {
92 this->setBitmapHeap(heap);
93 }
rmistry@google.comd6176b02012-08-23 18:14:13 +000094
scroggo@google.com4dffc592012-07-17 16:49:40 +000095 const SkFlatData* flatToReplace() const;
96
97 // Mark an SkFlatData as one that should not be returned by flatToReplace.
98 // Takes the result of SkFlatData::index() as its parameter.
99 void markFlatForKeeping(int index) {
100 *fFlatsThatMustBeKept.append() = index;
101 }
102
103 void markAllFlatsSafeToDelete() {
104 fFlatsThatMustBeKept.reset();
105 }
106
107private:
108 // Keep track of the indices (i.e. the result of SkFlatData::index()) of
109 // flats that must be kept, since they are on the current paint.
110 SkTDArray<int> fFlatsThatMustBeKept;
111 SkTDArray<void*> fPointers;
112 const int fNumFlatsToKeep;
113};
114
115void FlattenableHeap::unalloc(void* ptr) {
116 int indexToRemove = fPointers.rfind(ptr);
117 if (indexToRemove >= 0) {
118 sk_free(ptr);
119 fPointers.remove(indexToRemove);
120 }
121}
122
123void* FlattenableHeap::allocThrow(size_t bytes) {
124 void* ptr = sk_malloc_throw(bytes);
125 *fPointers.append() = ptr;
126 return ptr;
127}
128
129const SkFlatData* FlattenableHeap::flatToReplace() const {
130 // First, determine whether we should replace one.
131 if (fPointers.count() > fNumFlatsToKeep) {
132 // Look through the flattenable heap.
133 // TODO: Return the LRU flat.
134 for (int i = 0; i < fPointers.count(); i++) {
135 SkFlatData* potential = (SkFlatData*)fPointers[i];
136 // Make sure that it is not one that must be kept.
137 bool mustKeep = false;
138 for (int j = 0; j < fFlatsThatMustBeKept.count(); j++) {
139 if (potential->index() == fFlatsThatMustBeKept[j]) {
140 mustKeep = true;
141 break;
142 }
143 }
144 if (!mustKeep) {
145 return potential;
146 }
147 }
148 }
149 return NULL;
150}
151
152///////////////////////////////////////////////////////////////////////////////
153
154class FlatDictionary : public SkFlatDictionary<SkFlattenable> {
155public:
scroggo@google.com15543602012-08-02 18:49:49 +0000156 FlatDictionary(FlattenableHeap* heap)
157 : SkFlatDictionary<SkFlattenable>(heap) {
scroggo@google.com4dffc592012-07-17 16:49:40 +0000158 fFlattenProc = &flattenFlattenableProc;
159 // No need to define fUnflattenProc since the writer will never
160 // unflatten the data.
161 }
162 static void flattenFlattenableProc(SkOrderedWriteBuffer& buffer,
163 const void* obj) {
164 buffer.writeFlattenable((SkFlattenable*)obj);
165 }
166
167};
168
169///////////////////////////////////////////////////////////////////////////////
170
reed@google.combb6992a2011-04-26 17:41:56 +0000171class SkGPipeCanvas : public SkCanvas {
172public:
junov@chromium.orga8db8fe2012-08-15 19:49:22 +0000173 SkGPipeCanvas(SkGPipeController*, SkWriter32*, uint32_t flags,
174 uint32_t width, uint32_t height);
reed@google.combb6992a2011-04-26 17:41:56 +0000175 virtual ~SkGPipeCanvas();
176
177 void finish() {
178 if (!fDone) {
reed@google.comdbccc882011-07-08 18:53:39 +0000179 if (this->needOpBytes()) {
180 this->writeOp(kDone_DrawOp);
181 this->doNotify();
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000182 if (shouldFlattenBitmaps(fFlags)) {
scroggo@google.comd9d29672012-08-14 17:21:34 +0000183 // In this case, a BitmapShuttle is reffed by the SkBitmapHeap
184 // and refs this canvas. Unref the SkBitmapHeap to end the
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000185 // circular reference. When shouldFlattenBitmaps is false,
scroggo@google.comd9d29672012-08-14 17:21:34 +0000186 // there is no circular reference, so the SkBitmapHeap can be
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000187 // safely unreffed in the destructor.
scroggo@google.comd9d29672012-08-14 17:21:34 +0000188 fBitmapHeap->unref();
scroggo@google.com7ca24432012-08-14 15:48:43 +0000189 // This eliminates a similar circular reference (Canvas owns
scroggo@google.comd9d29672012-08-14 17:21:34 +0000190 // the FlattenableHeap which holds a ref to the SkBitmapHeap).
scroggo@google.com7ca24432012-08-14 15:48:43 +0000191 fFlattenableHeap.setBitmapStorage(NULL);
scroggo@google.comd9d29672012-08-14 17:21:34 +0000192 fBitmapHeap = NULL;
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000193 }
reed@google.comdbccc882011-07-08 18:53:39 +0000194 }
reed@google.combb6992a2011-04-26 17:41:56 +0000195 fDone = true;
196 }
197 }
198
junov@chromium.org77eec242012-07-18 17:54:45 +0000199 void flushRecording(bool detachCurrentBlock);
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000200 size_t freeMemoryIfPossible(size_t bytesToFree);
junov@chromium.org77eec242012-07-18 17:54:45 +0000201
scroggo@google.com15011ee2012-07-26 20:03:32 +0000202 size_t storageAllocatedForRecording() {
scroggo@google.comd5d158b2012-08-14 20:38:28 +0000203 return (NULL == fBitmapHeap) ? 0 : fBitmapHeap->bytesAllocated();
scroggo@google.com15011ee2012-07-26 20:03:32 +0000204 }
205
reed@google.combb6992a2011-04-26 17:41:56 +0000206 // overrides from SkCanvas
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000207 virtual int save(SaveFlags) SK_OVERRIDE;
208 virtual int saveLayer(const SkRect* bounds, const SkPaint*,
209 SaveFlags) SK_OVERRIDE;
210 virtual void restore() SK_OVERRIDE;
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000211 virtual bool isDrawingToLayer() const SK_OVERRIDE;
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000212 virtual bool translate(SkScalar dx, SkScalar dy) SK_OVERRIDE;
213 virtual bool scale(SkScalar sx, SkScalar sy) SK_OVERRIDE;
214 virtual bool rotate(SkScalar degrees) SK_OVERRIDE;
215 virtual bool skew(SkScalar sx, SkScalar sy) SK_OVERRIDE;
216 virtual bool concat(const SkMatrix& matrix) SK_OVERRIDE;
217 virtual void setMatrix(const SkMatrix& matrix) SK_OVERRIDE;
reed@google.com4ed0fb72012-12-12 20:48:18 +0000218 virtual bool clipRect(const SkRect&, SkRegion::Op op, bool doAntiAlias = false) SK_OVERRIDE;
219 virtual bool clipRRect(const SkRRect&, SkRegion::Op op, bool doAntiAlias = false) SK_OVERRIDE;
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000220 virtual bool clipPath(const SkPath& path, SkRegion::Op op,
221 bool doAntiAlias = false) SK_OVERRIDE;
222 virtual bool clipRegion(const SkRegion& region, SkRegion::Op op) SK_OVERRIDE;
223 virtual void clear(SkColor) SK_OVERRIDE;
224 virtual void drawPaint(const SkPaint& paint) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000225 virtual void drawPoints(PointMode, size_t count, const SkPoint pts[],
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000226 const SkPaint&) SK_OVERRIDE;
reed@google.com4ed0fb72012-12-12 20:48:18 +0000227 virtual void drawOval(const SkRect&, const SkPaint&) SK_OVERRIDE;
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000228 virtual void drawRect(const SkRect& rect, const SkPaint&) SK_OVERRIDE;
reed@google.com4ed0fb72012-12-12 20:48:18 +0000229 virtual void drawRRect(const SkRRect&, const SkPaint&) SK_OVERRIDE;
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000230 virtual void drawPath(const SkPath& path, const SkPaint&) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000231 virtual void drawBitmap(const SkBitmap&, SkScalar left, SkScalar top,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000232 const SkPaint*) SK_OVERRIDE;
reed@google.com71121732012-09-18 15:14:33 +0000233 virtual void drawBitmapRectToRect(const SkBitmap&, const SkRect* src,
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000234 const SkRect& dst, const SkPaint* paint,
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +0000235 DrawBitmapRectFlags flags) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000236 virtual void drawBitmapMatrix(const SkBitmap&, const SkMatrix&,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000237 const SkPaint*) SK_OVERRIDE;
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000238 virtual void drawBitmapNine(const SkBitmap& bitmap, const SkIRect& center,
239 const SkRect& dst, const SkPaint* paint = NULL) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000240 virtual void drawSprite(const SkBitmap&, int left, int top,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000241 const SkPaint*) SK_OVERRIDE;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000242 virtual void drawText(const void* text, size_t byteLength, SkScalar x,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000243 SkScalar y, const SkPaint&) SK_OVERRIDE;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000244 virtual void drawPosText(const void* text, size_t byteLength,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000245 const SkPoint pos[], const SkPaint&) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000246 virtual void drawPosTextH(const void* text, size_t byteLength,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000247 const SkScalar xpos[], SkScalar constY,
248 const SkPaint&) SK_OVERRIDE;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000249 virtual void drawTextOnPath(const void* text, size_t byteLength,
250 const SkPath& path, const SkMatrix* matrix,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000251 const SkPaint&) SK_OVERRIDE;
252 virtual void drawPicture(SkPicture& picture) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000253 virtual void drawVertices(VertexMode, int vertexCount,
254 const SkPoint vertices[], const SkPoint texs[],
255 const SkColor colors[], SkXfermode*,
256 const uint16_t indices[], int indexCount,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000257 const SkPaint&) SK_OVERRIDE;
258 virtual void drawData(const void*, size_t) SK_OVERRIDE;
robertphillips@google.com0a4805e2013-05-29 13:24:23 +0000259 virtual void beginCommentGroup(const char* description) SK_OVERRIDE;
260 virtual void addComment(const char* kywd, const char* value) SK_OVERRIDE;
261 virtual void endCommentGroup() SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000262
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000263 /**
264 * Flatten an SkBitmap to send to the reader, where it will be referenced
265 * according to slot.
266 */
267 bool shuttleBitmap(const SkBitmap&, int32_t slot);
reed@google.combb6992a2011-04-26 17:41:56 +0000268private:
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000269 enum {
270 kNoSaveLayer = -1,
271 };
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000272 SkNamedFactorySet* fFactorySet;
273 int fFirstSaveLayerStackLevel;
scroggo@google.comd9d29672012-08-14 17:21:34 +0000274 SkBitmapHeap* fBitmapHeap;
reed@google.comacd471f2011-05-03 21:26:46 +0000275 SkGPipeController* fController;
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000276 SkWriter32& fWriter;
277 size_t fBlockSize; // amount allocated for writer
278 size_t fBytesNotified;
279 bool fDone;
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000280 const uint32_t fFlags;
reed@google.combb6992a2011-04-26 17:41:56 +0000281
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000282 SkRefCntSet fTypefaceSet;
reed@google.comf5842f72011-05-04 18:30:04 +0000283
284 uint32_t getTypefaceID(SkTypeface*);
285
reed@google.comacd471f2011-05-03 21:26:46 +0000286 inline void writeOp(DrawOps op, unsigned flags, unsigned data) {
reed@google.combb6992a2011-04-26 17:41:56 +0000287 fWriter.write32(DrawOp_packOpFlagData(op, flags, data));
288 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000289
reed@google.comacd471f2011-05-03 21:26:46 +0000290 inline void writeOp(DrawOps op) {
reed@google.combb6992a2011-04-26 17:41:56 +0000291 fWriter.write32(DrawOp_packOpFlagData(op, 0, 0));
292 }
reed@google.comacd471f2011-05-03 21:26:46 +0000293
294 bool needOpBytes(size_t size = 0);
295
296 inline void doNotify() {
297 if (!fDone) {
298 size_t bytes = fWriter.size() - fBytesNotified;
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000299 if (bytes > 0) {
300 fController->notifyWritten(bytes);
301 fBytesNotified += bytes;
302 }
reed@google.comacd471f2011-05-03 21:26:46 +0000303 }
304 }
reed@google.comb55d1182011-05-11 00:42:04 +0000305
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000306 // Should be called after any calls to an SkFlatDictionary::findAndReplace
307 // if a new SkFlatData was added when in cross process mode
308 void flattenFactoryNames();
309
scroggo@google.com4dffc592012-07-17 16:49:40 +0000310 FlattenableHeap fFlattenableHeap;
311 FlatDictionary fFlatDictionary;
reed@google.comb55d1182011-05-11 00:42:04 +0000312 int fCurrFlatIndex[kCount_PaintFlats];
313 int flattenToIndex(SkFlattenable* obj, PaintFlats);
314
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000315 // Common code used by drawBitmap*. Behaves differently depending on the
316 // type of SkBitmapHeap being used, which is determined by the flags used.
317 bool commonDrawBitmap(const SkBitmap& bm, DrawOps op, unsigned flags,
318 size_t opBytesNeeded, const SkPaint* paint);
scroggo@google.com58be6822012-07-30 14:40:01 +0000319
reed@google.com31891582011-05-12 03:03:56 +0000320 SkPaint fPaint;
321 void writePaint(const SkPaint&);
reed@google.combb6992a2011-04-26 17:41:56 +0000322
reed@google.comacd471f2011-05-03 21:26:46 +0000323 class AutoPipeNotify {
324 public:
325 AutoPipeNotify(SkGPipeCanvas* canvas) : fCanvas(canvas) {}
326 ~AutoPipeNotify() { fCanvas->doNotify(); }
327 private:
328 SkGPipeCanvas* fCanvas;
329 };
330 friend class AutoPipeNotify;
331
reed@google.combb6992a2011-04-26 17:41:56 +0000332 typedef SkCanvas INHERITED;
333};
334
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000335void SkGPipeCanvas::flattenFactoryNames() {
336 const char* name;
337 while ((name = fFactorySet->getNextAddedFactoryName()) != NULL) {
338 size_t len = strlen(name);
339 if (this->needOpBytes(len)) {
340 this->writeOp(kDef_Factory_DrawOp);
341 fWriter.writeString(name, len);
342 }
343 }
344}
345
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000346bool SkGPipeCanvas::shuttleBitmap(const SkBitmap& bm, int32_t slot) {
scroggo@google.com565254b2012-06-28 15:41:32 +0000347 SkASSERT(shouldFlattenBitmaps(fFlags));
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000348 SkOrderedWriteBuffer buffer(1024);
349 buffer.setNamedFactoryRecorder(fFactorySet);
scroggo@google.com74b7ffd2013-04-30 02:32:41 +0000350 buffer.writeBitmap(bm);
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000351 this->flattenFactoryNames();
352 uint32_t size = buffer.size();
353 if (this->needOpBytes(size)) {
354 this->writeOp(kDef_Bitmap_DrawOp, 0, slot);
355 void* dst = static_cast<void*>(fWriter.reserve(size));
356 buffer.writeToMemory(dst);
357 return true;
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000358 }
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000359 return false;
scroggo@google.comd3ba5cc2012-07-09 16:05:53 +0000360}
361
reed@google.comb55d1182011-05-11 00:42:04 +0000362// return 0 for NULL (or unflattenable obj), or index-base-1
scroggo@google.comd3ba5cc2012-07-09 16:05:53 +0000363// return ~(index-base-1) if an old flattenable was replaced
reed@google.comb55d1182011-05-11 00:42:04 +0000364int SkGPipeCanvas::flattenToIndex(SkFlattenable* obj, PaintFlats paintflat) {
scroggo@google.comd5d158b2012-08-14 20:38:28 +0000365 SkASSERT(!fDone && fBitmapHeap != NULL);
reed@google.comb55d1182011-05-11 00:42:04 +0000366 if (NULL == obj) {
367 return 0;
368 }
reed@google.comb55d1182011-05-11 00:42:04 +0000369
scroggo@google.comd9d29672012-08-14 17:21:34 +0000370 fBitmapHeap->deferAddingOwners();
scroggo@google.com4dffc592012-07-17 16:49:40 +0000371 bool added, replaced;
scroggo@google.com664fab12012-08-14 19:22:05 +0000372 const SkFlatData* flat = fFlatDictionary.findAndReplace(*obj, fFlattenableHeap.flatToReplace(),
373 &added, &replaced);
scroggo@google.comd9d29672012-08-14 17:21:34 +0000374 fBitmapHeap->endAddingOwnersDeferral(added);
scroggo@google.com4dffc592012-07-17 16:49:40 +0000375 int index = flat->index();
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000376 if (added) {
377 if (isCrossProcess(fFlags)) {
378 this->flattenFactoryNames();
379 }
380 size_t flatSize = flat->flatSize();
381 if (this->needOpBytes(flatSize)) {
382 this->writeOp(kDef_Flattenable_DrawOp, paintflat, index);
383 fWriter.write(flat->data(), flatSize);
384 }
scroggo@google.com4dffc592012-07-17 16:49:40 +0000385 }
386 if (replaced) {
reed@google.comb55d1182011-05-11 00:42:04 +0000387 index = ~index;
reed@google.comb55d1182011-05-11 00:42:04 +0000388 }
scroggo@google.com4dffc592012-07-17 16:49:40 +0000389 return index;
reed@google.comb55d1182011-05-11 00:42:04 +0000390}
391
reed@google.combb6992a2011-04-26 17:41:56 +0000392///////////////////////////////////////////////////////////////////////////////
393
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000394/**
395 * If SkBitmaps are to be flattened to send to the reader, this class is
396 * provided to the SkBitmapHeap to tell the SkGPipeCanvas to do so.
397 */
398class BitmapShuttle : public SkBitmapHeap::ExternalStorage {
399public:
400 BitmapShuttle(SkGPipeCanvas*);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000401
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000402 ~BitmapShuttle();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000403
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000404 virtual bool insert(const SkBitmap& bitmap, int32_t slot) SK_OVERRIDE;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000405
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000406private:
407 SkGPipeCanvas* fCanvas;
408};
409
410///////////////////////////////////////////////////////////////////////////////
411
reed@google.comacd471f2011-05-03 21:26:46 +0000412#define MIN_BLOCK_SIZE (16 * 1024)
scroggo@google.com4dffc592012-07-17 16:49:40 +0000413#define BITMAPS_TO_KEEP 5
414#define FLATTENABLES_TO_KEEP 10
reed@google.combb6992a2011-04-26 17:41:56 +0000415
reed@google.comacd471f2011-05-03 21:26:46 +0000416SkGPipeCanvas::SkGPipeCanvas(SkGPipeController* controller,
rmistry@google.comd6176b02012-08-23 18:14:13 +0000417 SkWriter32* writer, uint32_t flags,
junov@chromium.orga8db8fe2012-08-15 19:49:22 +0000418 uint32_t width, uint32_t height)
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000419: fFactorySet(isCrossProcess(flags) ? SkNEW(SkNamedFactorySet) : NULL)
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000420, fWriter(*writer)
421, fFlags(flags)
scroggo@google.com664fab12012-08-14 19:22:05 +0000422, fFlattenableHeap(FLATTENABLES_TO_KEEP, fFactorySet, isCrossProcess(flags))
scroggo@google.com15543602012-08-02 18:49:49 +0000423, fFlatDictionary(&fFlattenableHeap) {
reed@google.comacd471f2011-05-03 21:26:46 +0000424 fController = controller;
reed@google.combb6992a2011-04-26 17:41:56 +0000425 fDone = false;
reed@google.comacd471f2011-05-03 21:26:46 +0000426 fBlockSize = 0; // need first block from controller
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000427 fBytesNotified = 0;
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000428 fFirstSaveLayerStackLevel = kNoSaveLayer;
reed@google.comb55d1182011-05-11 00:42:04 +0000429 sk_bzero(fCurrFlatIndex, sizeof(fCurrFlatIndex));
reed@google.comacd471f2011-05-03 21:26:46 +0000430
reed@google.combb6793b2011-05-05 15:18:15 +0000431 // we need a device to limit our clip
yangsu@google.com06b4da12011-06-17 15:04:40 +0000432 // We don't allocate pixels for the bitmap
reed@google.combb6793b2011-05-05 15:18:15 +0000433 SkBitmap bitmap;
junov@chromium.orga8db8fe2012-08-15 19:49:22 +0000434 bitmap.setConfig(SkBitmap::kARGB_8888_Config, width, height);
robertphillips@google.com9b051a32013-08-20 20:06:40 +0000435 SkDevice* device = SkNEW_ARGS(SkDevice, (bitmap));
reed@google.combb6793b2011-05-05 15:18:15 +0000436 this->setDevice(device)->unref();
scroggo@google.com10dccde2012-08-08 20:43:22 +0000437
scroggo@google.com565254b2012-06-28 15:41:32 +0000438 // Tell the reader the appropriate flags to use.
439 if (this->needOpBytes()) {
440 this->writeOp(kReportFlags_DrawOp, fFlags, 0);
441 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000442
scroggo@google.com10dccde2012-08-08 20:43:22 +0000443 if (shouldFlattenBitmaps(flags)) {
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000444 BitmapShuttle* shuttle = SkNEW_ARGS(BitmapShuttle, (this));
scroggo@google.comd9d29672012-08-14 17:21:34 +0000445 fBitmapHeap = SkNEW_ARGS(SkBitmapHeap, (shuttle, BITMAPS_TO_KEEP));
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000446 shuttle->unref();
scroggo@google.com10dccde2012-08-08 20:43:22 +0000447 } else {
scroggo@google.comd9d29672012-08-14 17:21:34 +0000448 fBitmapHeap = SkNEW_ARGS(SkBitmapHeap,
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000449 (BITMAPS_TO_KEEP, controller->numberOfReaders()));
scroggo@google.com10dccde2012-08-08 20:43:22 +0000450 if (this->needOpBytes(sizeof(void*))) {
scroggo@google.comd9d29672012-08-14 17:21:34 +0000451 this->writeOp(kShareBitmapHeap_DrawOp);
452 fWriter.writePtr(static_cast<void*>(fBitmapHeap));
scroggo@google.com10dccde2012-08-08 20:43:22 +0000453 }
454 }
scroggo@google.comd9d29672012-08-14 17:21:34 +0000455 fFlattenableHeap.setBitmapStorage(fBitmapHeap);
scroggo@google.com10dccde2012-08-08 20:43:22 +0000456 this->doNotify();
reed@google.combb6992a2011-04-26 17:41:56 +0000457}
458
459SkGPipeCanvas::~SkGPipeCanvas() {
460 this->finish();
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000461 SkSafeUnref(fFactorySet);
scroggo@google.comd9d29672012-08-14 17:21:34 +0000462 SkSafeUnref(fBitmapHeap);
reed@google.combb6992a2011-04-26 17:41:56 +0000463}
464
reed@google.comacd471f2011-05-03 21:26:46 +0000465bool SkGPipeCanvas::needOpBytes(size_t needed) {
466 if (fDone) {
467 return false;
468 }
469
470 needed += 4; // size of DrawOp atom
471 if (fWriter.size() + needed > fBlockSize) {
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000472 // Before we wipe out any data that has already been written, read it
473 // out.
474 this->doNotify();
475 size_t blockSize = SkMax32(MIN_BLOCK_SIZE, needed);
476 void* block = fController->requestBlock(blockSize, &fBlockSize);
reed@google.comacd471f2011-05-03 21:26:46 +0000477 if (NULL == block) {
478 fDone = true;
479 return false;
480 }
scroggo@google.com460a23e2012-08-16 17:56:49 +0000481 SkASSERT(SkIsAlign4(fBlockSize));
reed@google.comacd471f2011-05-03 21:26:46 +0000482 fWriter.reset(block, fBlockSize);
483 fBytesNotified = 0;
484 }
485 return true;
486}
487
reed@google.comf5842f72011-05-04 18:30:04 +0000488uint32_t SkGPipeCanvas::getTypefaceID(SkTypeface* face) {
489 uint32_t id = 0; // 0 means default/null typeface
490 if (face) {
491 id = fTypefaceSet.find(face);
492 if (0 == id) {
493 id = fTypefaceSet.add(face);
494 size_t size = writeTypeface(NULL, face);
495 if (this->needOpBytes(size)) {
reed@google.combb6793b2011-05-05 15:18:15 +0000496 this->writeOp(kDef_Typeface_DrawOp);
reed@google.comf5842f72011-05-04 18:30:04 +0000497 writeTypeface(&fWriter, face);
498 }
499 }
500 }
501 return id;
502}
503
reed@google.combb6992a2011-04-26 17:41:56 +0000504///////////////////////////////////////////////////////////////////////////////
505
reed@google.comacd471f2011-05-03 21:26:46 +0000506#define NOTIFY_SETUP(canvas) \
507 AutoPipeNotify apn(canvas)
508
reed@google.combb6992a2011-04-26 17:41:56 +0000509int SkGPipeCanvas::save(SaveFlags flags) {
reed@google.comacd471f2011-05-03 21:26:46 +0000510 NOTIFY_SETUP(this);
511 if (this->needOpBytes()) {
512 this->writeOp(kSave_DrawOp, 0, flags);
513 }
reed@google.combb6992a2011-04-26 17:41:56 +0000514 return this->INHERITED::save(flags);
515}
516
517int SkGPipeCanvas::saveLayer(const SkRect* bounds, const SkPaint* paint,
518 SaveFlags saveFlags) {
reed@google.comacd471f2011-05-03 21:26:46 +0000519 NOTIFY_SETUP(this);
520 size_t size = 0;
reed@google.combb6992a2011-04-26 17:41:56 +0000521 unsigned opFlags = 0;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000522
reed@google.combb6992a2011-04-26 17:41:56 +0000523 if (bounds) {
524 opFlags |= kSaveLayer_HasBounds_DrawOpFlag;
reed@google.comacd471f2011-05-03 21:26:46 +0000525 size += sizeof(SkRect);
reed@google.combb6992a2011-04-26 17:41:56 +0000526 }
527 if (paint) {
528 opFlags |= kSaveLayer_HasPaint_DrawOpFlag;
reed@google.com31891582011-05-12 03:03:56 +0000529 this->writePaint(*paint);
reed@google.combb6992a2011-04-26 17:41:56 +0000530 }
531
reed@google.comacd471f2011-05-03 21:26:46 +0000532 if (this->needOpBytes(size)) {
533 this->writeOp(kSaveLayer_DrawOp, opFlags, saveFlags);
534 if (bounds) {
535 fWriter.writeRect(*bounds);
536 }
reed@google.combb6992a2011-04-26 17:41:56 +0000537 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000538
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000539 if (kNoSaveLayer == fFirstSaveLayerStackLevel){
540 fFirstSaveLayerStackLevel = this->getSaveCount();
541 }
reed@google.combb6992a2011-04-26 17:41:56 +0000542 // we just pass on the save, so we don't create a layer
543 return this->INHERITED::save(saveFlags);
544}
545
546void SkGPipeCanvas::restore() {
reed@google.comacd471f2011-05-03 21:26:46 +0000547 NOTIFY_SETUP(this);
548 if (this->needOpBytes()) {
549 this->writeOp(kRestore_DrawOp);
550 }
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000551
reed@google.combb6992a2011-04-26 17:41:56 +0000552 this->INHERITED::restore();
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000553
554 if (this->getSaveCount() == fFirstSaveLayerStackLevel){
555 fFirstSaveLayerStackLevel = kNoSaveLayer;
556 }
557}
558
559bool SkGPipeCanvas::isDrawingToLayer() const {
560 return kNoSaveLayer != fFirstSaveLayerStackLevel;
reed@google.combb6992a2011-04-26 17:41:56 +0000561}
562
563bool SkGPipeCanvas::translate(SkScalar dx, SkScalar dy) {
564 if (dx || dy) {
reed@google.comacd471f2011-05-03 21:26:46 +0000565 NOTIFY_SETUP(this);
566 if (this->needOpBytes(2 * sizeof(SkScalar))) {
567 this->writeOp(kTranslate_DrawOp);
568 fWriter.writeScalar(dx);
569 fWriter.writeScalar(dy);
570 }
reed@google.combb6992a2011-04-26 17:41:56 +0000571 }
572 return this->INHERITED::translate(dx, dy);
573}
574
575bool SkGPipeCanvas::scale(SkScalar sx, SkScalar sy) {
576 if (sx || sy) {
reed@google.comacd471f2011-05-03 21:26:46 +0000577 NOTIFY_SETUP(this);
578 if (this->needOpBytes(2 * sizeof(SkScalar))) {
579 this->writeOp(kScale_DrawOp);
580 fWriter.writeScalar(sx);
581 fWriter.writeScalar(sy);
582 }
reed@google.combb6992a2011-04-26 17:41:56 +0000583 }
584 return this->INHERITED::scale(sx, sy);
585}
586
587bool SkGPipeCanvas::rotate(SkScalar degrees) {
588 if (degrees) {
reed@google.comacd471f2011-05-03 21:26:46 +0000589 NOTIFY_SETUP(this);
590 if (this->needOpBytes(sizeof(SkScalar))) {
591 this->writeOp(kRotate_DrawOp);
592 fWriter.writeScalar(degrees);
593 }
reed@google.combb6992a2011-04-26 17:41:56 +0000594 }
595 return this->INHERITED::rotate(degrees);
596}
597
598bool SkGPipeCanvas::skew(SkScalar sx, SkScalar sy) {
599 if (sx || sy) {
reed@google.comacd471f2011-05-03 21:26:46 +0000600 NOTIFY_SETUP(this);
601 if (this->needOpBytes(2 * sizeof(SkScalar))) {
602 this->writeOp(kSkew_DrawOp);
603 fWriter.writeScalar(sx);
604 fWriter.writeScalar(sy);
605 }
reed@google.combb6992a2011-04-26 17:41:56 +0000606 }
607 return this->INHERITED::skew(sx, sy);
608}
609
610bool SkGPipeCanvas::concat(const SkMatrix& matrix) {
611 if (!matrix.isIdentity()) {
reed@google.comacd471f2011-05-03 21:26:46 +0000612 NOTIFY_SETUP(this);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000613 if (this->needOpBytes(matrix.writeToMemory(NULL))) {
reed@google.comacd471f2011-05-03 21:26:46 +0000614 this->writeOp(kConcat_DrawOp);
djsollen@google.com2b2ede32012-04-12 13:24:04 +0000615 fWriter.writeMatrix(matrix);
reed@google.comacd471f2011-05-03 21:26:46 +0000616 }
reed@google.combb6992a2011-04-26 17:41:56 +0000617 }
618 return this->INHERITED::concat(matrix);
619}
620
621void SkGPipeCanvas::setMatrix(const SkMatrix& matrix) {
reed@google.comacd471f2011-05-03 21:26:46 +0000622 NOTIFY_SETUP(this);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000623 if (this->needOpBytes(matrix.writeToMemory(NULL))) {
reed@google.comacd471f2011-05-03 21:26:46 +0000624 this->writeOp(kSetMatrix_DrawOp);
djsollen@google.com2b2ede32012-04-12 13:24:04 +0000625 fWriter.writeMatrix(matrix);
reed@google.comacd471f2011-05-03 21:26:46 +0000626 }
reed@google.combb6992a2011-04-26 17:41:56 +0000627 this->INHERITED::setMatrix(matrix);
628}
629
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000630bool SkGPipeCanvas::clipRect(const SkRect& rect, SkRegion::Op rgnOp,
631 bool doAntiAlias) {
reed@google.comacd471f2011-05-03 21:26:46 +0000632 NOTIFY_SETUP(this);
scroggo@google.com460a23e2012-08-16 17:56:49 +0000633 if (this->needOpBytes(sizeof(SkRect))) {
634 unsigned flags = doAntiAlias & kClip_HasAntiAlias_DrawOpFlag;
635 this->writeOp(kClipRect_DrawOp, flags, rgnOp);
reed@google.comacd471f2011-05-03 21:26:46 +0000636 fWriter.writeRect(rect);
637 }
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000638 return this->INHERITED::clipRect(rect, rgnOp, doAntiAlias);
reed@google.combb6992a2011-04-26 17:41:56 +0000639}
640
reed@google.com4ed0fb72012-12-12 20:48:18 +0000641bool SkGPipeCanvas::clipRRect(const SkRRect& rrect, SkRegion::Op rgnOp,
642 bool doAntiAlias) {
643 NOTIFY_SETUP(this);
644 if (this->needOpBytes(kSizeOfFlatRRect)) {
645 unsigned flags = doAntiAlias & kClip_HasAntiAlias_DrawOpFlag;
646 this->writeOp(kClipRRect_DrawOp, flags, rgnOp);
647 fWriter.writeRRect(rrect);
648 }
649 return this->INHERITED::clipRRect(rrect, rgnOp, doAntiAlias);
650}
651
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000652bool SkGPipeCanvas::clipPath(const SkPath& path, SkRegion::Op rgnOp,
653 bool doAntiAlias) {
reed@google.comacd471f2011-05-03 21:26:46 +0000654 NOTIFY_SETUP(this);
scroggo@google.com460a23e2012-08-16 17:56:49 +0000655 if (this->needOpBytes(path.writeToMemory(NULL))) {
656 unsigned flags = doAntiAlias & kClip_HasAntiAlias_DrawOpFlag;
657 this->writeOp(kClipPath_DrawOp, flags, rgnOp);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000658 fWriter.writePath(path);
reed@google.comacd471f2011-05-03 21:26:46 +0000659 }
reed@google.combb6992a2011-04-26 17:41:56 +0000660 // we just pass on the bounds of the path
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000661 return this->INHERITED::clipRect(path.getBounds(), rgnOp, doAntiAlias);
reed@google.combb6992a2011-04-26 17:41:56 +0000662}
663
664bool SkGPipeCanvas::clipRegion(const SkRegion& region, SkRegion::Op rgnOp) {
reed@google.comacd471f2011-05-03 21:26:46 +0000665 NOTIFY_SETUP(this);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000666 if (this->needOpBytes(region.writeToMemory(NULL))) {
reed@google.comacd471f2011-05-03 21:26:46 +0000667 this->writeOp(kClipRegion_DrawOp, 0, rgnOp);
djsollen@google.com2b2ede32012-04-12 13:24:04 +0000668 fWriter.writeRegion(region);
reed@google.comacd471f2011-05-03 21:26:46 +0000669 }
reed@google.combb6992a2011-04-26 17:41:56 +0000670 return this->INHERITED::clipRegion(region, rgnOp);
671}
672
673///////////////////////////////////////////////////////////////////////////////
674
675void SkGPipeCanvas::clear(SkColor color) {
reed@google.comacd471f2011-05-03 21:26:46 +0000676 NOTIFY_SETUP(this);
reed@google.combb6992a2011-04-26 17:41:56 +0000677 unsigned flags = 0;
678 if (color) {
679 flags |= kClear_HasColor_DrawOpFlag;
680 }
reed@google.comacd471f2011-05-03 21:26:46 +0000681 if (this->needOpBytes(sizeof(SkColor))) {
682 this->writeOp(kDrawClear_DrawOp, flags, 0);
683 if (color) {
684 fWriter.write32(color);
685 }
reed@google.combb6992a2011-04-26 17:41:56 +0000686 }
687}
688
689void SkGPipeCanvas::drawPaint(const SkPaint& paint) {
reed@google.comacd471f2011-05-03 21:26:46 +0000690 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000691 this->writePaint(paint);
reed@google.comacd471f2011-05-03 21:26:46 +0000692 if (this->needOpBytes()) {
reed@google.com31891582011-05-12 03:03:56 +0000693 this->writeOp(kDrawPaint_DrawOp);
reed@google.comacd471f2011-05-03 21:26:46 +0000694 }
reed@google.combb6992a2011-04-26 17:41:56 +0000695}
696
697void SkGPipeCanvas::drawPoints(PointMode mode, size_t count,
698 const SkPoint pts[], const SkPaint& paint) {
699 if (count) {
reed@google.comacd471f2011-05-03 21:26:46 +0000700 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000701 this->writePaint(paint);
reed@google.comacd471f2011-05-03 21:26:46 +0000702 if (this->needOpBytes(4 + count * sizeof(SkPoint))) {
reed@google.com31891582011-05-12 03:03:56 +0000703 this->writeOp(kDrawPoints_DrawOp, mode, 0);
reed@google.comacd471f2011-05-03 21:26:46 +0000704 fWriter.write32(count);
705 fWriter.write(pts, count * sizeof(SkPoint));
706 }
reed@google.combb6992a2011-04-26 17:41:56 +0000707 }
708}
709
reed@google.com4ed0fb72012-12-12 20:48:18 +0000710void SkGPipeCanvas::drawOval(const SkRect& rect, const SkPaint& paint) {
711 NOTIFY_SETUP(this);
712 this->writePaint(paint);
713 if (this->needOpBytes(sizeof(SkRect))) {
714 this->writeOp(kDrawOval_DrawOp);
715 fWriter.writeRect(rect);
716 }
717}
718
reed@google.combb6992a2011-04-26 17:41:56 +0000719void SkGPipeCanvas::drawRect(const SkRect& rect, const SkPaint& paint) {
reed@google.comacd471f2011-05-03 21:26:46 +0000720 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000721 this->writePaint(paint);
reed@google.comacd471f2011-05-03 21:26:46 +0000722 if (this->needOpBytes(sizeof(SkRect))) {
reed@google.com31891582011-05-12 03:03:56 +0000723 this->writeOp(kDrawRect_DrawOp);
reed@google.comacd471f2011-05-03 21:26:46 +0000724 fWriter.writeRect(rect);
725 }
reed@google.combb6992a2011-04-26 17:41:56 +0000726}
727
reed@google.com4ed0fb72012-12-12 20:48:18 +0000728void SkGPipeCanvas::drawRRect(const SkRRect& rrect, const SkPaint& paint) {
729 NOTIFY_SETUP(this);
730 this->writePaint(paint);
731 if (this->needOpBytes(kSizeOfFlatRRect)) {
732 this->writeOp(kDrawRRect_DrawOp);
733 fWriter.writeRRect(rrect);
734 }
735}
736
reed@google.combb6992a2011-04-26 17:41:56 +0000737void SkGPipeCanvas::drawPath(const SkPath& path, const SkPaint& paint) {
reed@google.comacd471f2011-05-03 21:26:46 +0000738 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000739 this->writePaint(paint);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000740 if (this->needOpBytes(path.writeToMemory(NULL))) {
reed@google.com31891582011-05-12 03:03:56 +0000741 this->writeOp(kDrawPath_DrawOp);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000742 fWriter.writePath(path);
reed@google.comacd471f2011-05-03 21:26:46 +0000743 }
reed@google.combb6992a2011-04-26 17:41:56 +0000744}
745
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000746bool SkGPipeCanvas::commonDrawBitmap(const SkBitmap& bm, DrawOps op,
747 unsigned flags,
748 size_t opBytesNeeded,
749 const SkPaint* paint) {
scroggo@google.com58be6822012-07-30 14:40:01 +0000750 if (paint != NULL) {
scroggo@google.com460a23e2012-08-16 17:56:49 +0000751 flags |= kDrawBitmap_HasPaint_DrawOpFlag;
scroggo@google.com58be6822012-07-30 14:40:01 +0000752 this->writePaint(*paint);
753 }
scroggo@google.com10dccde2012-08-08 20:43:22 +0000754 if (this->needOpBytes(opBytesNeeded)) {
scroggo@google.comd5d158b2012-08-14 20:38:28 +0000755 SkASSERT(fBitmapHeap != NULL);
756 int32_t bitmapIndex = fBitmapHeap->insert(bm);
757 if (SkBitmapHeap::INVALID_SLOT == bitmapIndex) {
758 return false;
759 }
scroggo@google.com10dccde2012-08-08 20:43:22 +0000760 this->writeOp(op, flags, bitmapIndex);
scroggo@google.com58be6822012-07-30 14:40:01 +0000761 return true;
762 }
763 return false;
764}
765
766void SkGPipeCanvas::drawBitmap(const SkBitmap& bm, SkScalar left, SkScalar top,
767 const SkPaint* paint) {
768 NOTIFY_SETUP(this);
769 size_t opBytesNeeded = sizeof(SkScalar) * 2;
770
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000771 if (this->commonDrawBitmap(bm, kDrawBitmap_DrawOp, 0, opBytesNeeded, paint)) {
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000772 fWriter.writeScalar(left);
773 fWriter.writeScalar(top);
774 }
reed@google.combb6992a2011-04-26 17:41:56 +0000775}
776
reed@google.com71121732012-09-18 15:14:33 +0000777void SkGPipeCanvas::drawBitmapRectToRect(const SkBitmap& bm, const SkRect* src,
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +0000778 const SkRect& dst, const SkPaint* paint,
779 DrawBitmapRectFlags dbmrFlags) {
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000780 NOTIFY_SETUP(this);
scroggo@google.com58be6822012-07-30 14:40:01 +0000781 size_t opBytesNeeded = sizeof(SkRect);
782 bool hasSrc = src != NULL;
783 unsigned flags;
784 if (hasSrc) {
scroggo@google.com460a23e2012-08-16 17:56:49 +0000785 flags = kDrawBitmap_HasSrcRect_DrawOpFlag;
scroggo@google.com58be6822012-07-30 14:40:01 +0000786 opBytesNeeded += sizeof(int32_t) * 4;
787 } else {
788 flags = 0;
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000789 }
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +0000790 if (dbmrFlags & kBleed_DrawBitmapRectFlag) {
791 flags |= kDrawBitmap_Bleed_DrawOpFlag;
792 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000793
reed@google.com71121732012-09-18 15:14:33 +0000794 if (this->commonDrawBitmap(bm, kDrawBitmapRectToRect_DrawOp, flags, opBytesNeeded, paint)) {
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000795 if (hasSrc) {
reed@google.com71121732012-09-18 15:14:33 +0000796 fWriter.writeRect(*src);
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000797 }
798 fWriter.writeRect(dst);
799 }
reed@google.combb6992a2011-04-26 17:41:56 +0000800}
801
scroggo@google.com58be6822012-07-30 14:40:01 +0000802void SkGPipeCanvas::drawBitmapMatrix(const SkBitmap& bm, const SkMatrix& matrix,
803 const SkPaint* paint) {
804 NOTIFY_SETUP(this);
805 size_t opBytesNeeded = matrix.writeToMemory(NULL);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000806
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000807 if (this->commonDrawBitmap(bm, kDrawBitmapMatrix_DrawOp, 0, opBytesNeeded, paint)) {
scroggo@google.com58be6822012-07-30 14:40:01 +0000808 fWriter.writeMatrix(matrix);
809 }
reed@google.combb6992a2011-04-26 17:41:56 +0000810}
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000811
812void SkGPipeCanvas::drawBitmapNine(const SkBitmap& bm, const SkIRect& center,
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000813 const SkRect& dst, const SkPaint* paint) {
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000814 NOTIFY_SETUP(this);
scroggo@google.com58be6822012-07-30 14:40:01 +0000815 size_t opBytesNeeded = sizeof(int32_t) * 4 + sizeof(SkRect);
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000816
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000817 if (this->commonDrawBitmap(bm, kDrawBitmapNine_DrawOp, 0, opBytesNeeded, paint)) {
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000818 fWriter.write32(center.fLeft);
819 fWriter.write32(center.fTop);
820 fWriter.write32(center.fRight);
821 fWriter.write32(center.fBottom);
822 fWriter.writeRect(dst);
823 }
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000824}
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000825
826void SkGPipeCanvas::drawSprite(const SkBitmap& bm, int left, int top,
827 const SkPaint* paint) {
828 NOTIFY_SETUP(this);
scroggo@google.com58be6822012-07-30 14:40:01 +0000829 size_t opBytesNeeded = sizeof(int32_t) * 2;
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000830
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000831 if (this->commonDrawBitmap(bm, kDrawSprite_DrawOp, 0, opBytesNeeded, paint)) {
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000832 fWriter.write32(left);
833 fWriter.write32(top);
834 }
reed@google.combb6992a2011-04-26 17:41:56 +0000835}
836
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000837void SkGPipeCanvas::drawText(const void* text, size_t byteLength, SkScalar x,
reed@google.combb6992a2011-04-26 17:41:56 +0000838 SkScalar y, const SkPaint& paint) {
839 if (byteLength) {
reed@google.comacd471f2011-05-03 21:26:46 +0000840 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000841 this->writePaint(paint);
reed@google.comacd471f2011-05-03 21:26:46 +0000842 if (this->needOpBytes(4 + SkAlign4(byteLength) + 2 * sizeof(SkScalar))) {
reed@google.com31891582011-05-12 03:03:56 +0000843 this->writeOp(kDrawText_DrawOp);
reed@google.comacd471f2011-05-03 21:26:46 +0000844 fWriter.write32(byteLength);
845 fWriter.writePad(text, byteLength);
846 fWriter.writeScalar(x);
847 fWriter.writeScalar(y);
848 }
reed@google.combb6992a2011-04-26 17:41:56 +0000849 }
850}
851
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000852void SkGPipeCanvas::drawPosText(const void* text, size_t byteLength,
reed@google.combb6992a2011-04-26 17:41:56 +0000853 const SkPoint pos[], const SkPaint& paint) {
854 if (byteLength) {
reed@google.comacd471f2011-05-03 21:26:46 +0000855 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000856 this->writePaint(paint);
reed@google.combb6992a2011-04-26 17:41:56 +0000857 int count = paint.textToGlyphs(text, byteLength, NULL);
reed@google.comacd471f2011-05-03 21:26:46 +0000858 if (this->needOpBytes(4 + SkAlign4(byteLength) + 4 + count * sizeof(SkPoint))) {
reed@google.com31891582011-05-12 03:03:56 +0000859 this->writeOp(kDrawPosText_DrawOp);
reed@google.comacd471f2011-05-03 21:26:46 +0000860 fWriter.write32(byteLength);
861 fWriter.writePad(text, byteLength);
862 fWriter.write32(count);
863 fWriter.write(pos, count * sizeof(SkPoint));
864 }
reed@google.combb6992a2011-04-26 17:41:56 +0000865 }
866}
867
868void SkGPipeCanvas::drawPosTextH(const void* text, size_t byteLength,
869 const SkScalar xpos[], SkScalar constY,
870 const SkPaint& paint) {
871 if (byteLength) {
reed@google.comacd471f2011-05-03 21:26:46 +0000872 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000873 this->writePaint(paint);
reed@google.combb6992a2011-04-26 17:41:56 +0000874 int count = paint.textToGlyphs(text, byteLength, NULL);
reed@google.comacd471f2011-05-03 21:26:46 +0000875 if (this->needOpBytes(4 + SkAlign4(byteLength) + 4 + count * sizeof(SkScalar) + 4)) {
reed@google.com31891582011-05-12 03:03:56 +0000876 this->writeOp(kDrawPosTextH_DrawOp);
reed@google.comacd471f2011-05-03 21:26:46 +0000877 fWriter.write32(byteLength);
878 fWriter.writePad(text, byteLength);
879 fWriter.write32(count);
880 fWriter.write(xpos, count * sizeof(SkScalar));
881 fWriter.writeScalar(constY);
882 }
reed@google.combb6992a2011-04-26 17:41:56 +0000883 }
884}
885
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000886void SkGPipeCanvas::drawTextOnPath(const void* text, size_t byteLength,
887 const SkPath& path, const SkMatrix* matrix,
reed@google.combb6992a2011-04-26 17:41:56 +0000888 const SkPaint& paint) {
889 if (byteLength) {
reed@google.comacd471f2011-05-03 21:26:46 +0000890 NOTIFY_SETUP(this);
reed@google.combb6992a2011-04-26 17:41:56 +0000891 unsigned flags = 0;
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000892 size_t size = 4 + SkAlign4(byteLength) + path.writeToMemory(NULL);
reed@google.combb6992a2011-04-26 17:41:56 +0000893 if (matrix) {
894 flags |= kDrawTextOnPath_HasMatrix_DrawOpFlag;
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000895 size += matrix->writeToMemory(NULL);
reed@google.combb6992a2011-04-26 17:41:56 +0000896 }
reed@google.com31891582011-05-12 03:03:56 +0000897 this->writePaint(paint);
reed@google.comacd471f2011-05-03 21:26:46 +0000898 if (this->needOpBytes(size)) {
reed@google.com31891582011-05-12 03:03:56 +0000899 this->writeOp(kDrawTextOnPath_DrawOp, flags, 0);
reed@google.combb6992a2011-04-26 17:41:56 +0000900
reed@google.comacd471f2011-05-03 21:26:46 +0000901 fWriter.write32(byteLength);
902 fWriter.writePad(text, byteLength);
reed@google.combb6992a2011-04-26 17:41:56 +0000903
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000904 fWriter.writePath(path);
reed@google.comacd471f2011-05-03 21:26:46 +0000905 if (matrix) {
djsollen@google.com2b2ede32012-04-12 13:24:04 +0000906 fWriter.writeMatrix(*matrix);
reed@google.comacd471f2011-05-03 21:26:46 +0000907 }
reed@google.combb6992a2011-04-26 17:41:56 +0000908 }
909 }
910}
911
912void SkGPipeCanvas::drawPicture(SkPicture& picture) {
reed@google.com0faac1e2011-05-11 05:58:58 +0000913 // we want to playback the picture into individual draw calls
914 this->INHERITED::drawPicture(picture);
reed@google.combb6992a2011-04-26 17:41:56 +0000915}
916
reed@google.combb6992a2011-04-26 17:41:56 +0000917void SkGPipeCanvas::drawVertices(VertexMode mode, int vertexCount,
918 const SkPoint vertices[], const SkPoint texs[],
919 const SkColor colors[], SkXfermode*,
920 const uint16_t indices[], int indexCount,
921 const SkPaint& paint) {
922 if (0 == vertexCount) {
923 return;
924 }
925
reed@google.comacd471f2011-05-03 21:26:46 +0000926 NOTIFY_SETUP(this);
927 size_t size = 4 + vertexCount * sizeof(SkPoint);
reed@google.com31891582011-05-12 03:03:56 +0000928 this->writePaint(paint);
reed@google.combb6992a2011-04-26 17:41:56 +0000929 unsigned flags = 0;
930 if (texs) {
931 flags |= kDrawVertices_HasTexs_DrawOpFlag;
reed@google.comacd471f2011-05-03 21:26:46 +0000932 size += vertexCount * sizeof(SkPoint);
reed@google.combb6992a2011-04-26 17:41:56 +0000933 }
934 if (colors) {
935 flags |= kDrawVertices_HasColors_DrawOpFlag;
reed@google.comacd471f2011-05-03 21:26:46 +0000936 size += vertexCount * sizeof(SkColor);
reed@google.combb6992a2011-04-26 17:41:56 +0000937 }
938 if (indices && indexCount > 0) {
939 flags |= kDrawVertices_HasIndices_DrawOpFlag;
reed@google.comacd471f2011-05-03 21:26:46 +0000940 size += 4 + SkAlign4(indexCount * sizeof(uint16_t));
reed@google.combb6992a2011-04-26 17:41:56 +0000941 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000942
reed@google.comacd471f2011-05-03 21:26:46 +0000943 if (this->needOpBytes(size)) {
reed@google.com31891582011-05-12 03:03:56 +0000944 this->writeOp(kDrawVertices_DrawOp, flags, 0);
reed@google.comacd471f2011-05-03 21:26:46 +0000945 fWriter.write32(mode);
946 fWriter.write32(vertexCount);
947 fWriter.write(vertices, vertexCount * sizeof(SkPoint));
948 if (texs) {
949 fWriter.write(texs, vertexCount * sizeof(SkPoint));
950 }
951 if (colors) {
952 fWriter.write(colors, vertexCount * sizeof(SkColor));
953 }
reed@google.combb6992a2011-04-26 17:41:56 +0000954
reed@google.comacd471f2011-05-03 21:26:46 +0000955 // TODO: flatten xfermode
reed@google.combb6992a2011-04-26 17:41:56 +0000956
reed@google.comacd471f2011-05-03 21:26:46 +0000957 if (indices && indexCount > 0) {
958 fWriter.write32(indexCount);
959 fWriter.writePad(indices, indexCount * sizeof(uint16_t));
960 }
reed@google.combb6992a2011-04-26 17:41:56 +0000961 }
962}
963
reed@google.comacd471f2011-05-03 21:26:46 +0000964void SkGPipeCanvas::drawData(const void* ptr, size_t size) {
965 if (size && ptr) {
966 NOTIFY_SETUP(this);
reed@google.combb6992a2011-04-26 17:41:56 +0000967 unsigned data = 0;
968 if (size < (1 << DRAWOPS_DATA_BITS)) {
969 data = (unsigned)size;
970 }
reed@google.comacd471f2011-05-03 21:26:46 +0000971 if (this->needOpBytes(4 + SkAlign4(size))) {
972 this->writeOp(kDrawData_DrawOp, 0, data);
973 if (0 == data) {
974 fWriter.write32(size);
975 }
reed@google.combb6793b2011-05-05 15:18:15 +0000976 fWriter.writePad(ptr, size);
reed@google.combb6992a2011-04-26 17:41:56 +0000977 }
978 }
979}
980
robertphillips@google.com0a4805e2013-05-29 13:24:23 +0000981void SkGPipeCanvas::beginCommentGroup(const char* description) {
982 // ignore for now
983}
984
985void SkGPipeCanvas::addComment(const char* kywd, const char* value) {
986 // ignore for now
987}
988
989void SkGPipeCanvas::endCommentGroup() {
990 // ignore for now
991}
992
junov@chromium.org77eec242012-07-18 17:54:45 +0000993void SkGPipeCanvas::flushRecording(bool detachCurrentBlock) {
994 doNotify();
995 if (detachCurrentBlock) {
996 // force a new block to be requested for the next recorded command
rmistry@google.comd6176b02012-08-23 18:14:13 +0000997 fBlockSize = 0;
junov@chromium.org77eec242012-07-18 17:54:45 +0000998 }
999}
1000
junov@chromium.org2e14ba82012-08-07 14:26:57 +00001001size_t SkGPipeCanvas::freeMemoryIfPossible(size_t bytesToFree) {
scroggo@google.comd5d158b2012-08-14 20:38:28 +00001002 return (NULL == fBitmapHeap) ? 0 : fBitmapHeap->freeMemoryIfPossible(bytesToFree);
junov@chromium.org2e14ba82012-08-07 14:26:57 +00001003}
1004
reed@google.combb6992a2011-04-26 17:41:56 +00001005///////////////////////////////////////////////////////////////////////////////
1006
1007template <typename T> uint32_t castToU32(T value) {
1008 union {
1009 T fSrc;
1010 uint32_t fDst;
1011 } data;
1012 data.fSrc = value;
1013 return data.fDst;
1014}
1015
reed@google.com31891582011-05-12 03:03:56 +00001016void SkGPipeCanvas::writePaint(const SkPaint& paint) {
scroggo@google.comd5d158b2012-08-14 20:38:28 +00001017 if (fDone) {
1018 return;
1019 }
reed@google.com31891582011-05-12 03:03:56 +00001020 SkPaint& base = fPaint;
reed@google.combb6992a2011-04-26 17:41:56 +00001021 uint32_t storage[32];
1022 uint32_t* ptr = storage;
reed@google.combb6992a2011-04-26 17:41:56 +00001023
1024 if (base.getFlags() != paint.getFlags()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001025 *ptr++ = PaintOp_packOpData(kFlags_PaintOp, paint.getFlags());
reed@google.comf5842f72011-05-04 18:30:04 +00001026 base.setFlags(paint.getFlags());
reed@google.combb6992a2011-04-26 17:41:56 +00001027 }
1028 if (base.getColor() != paint.getColor()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001029 *ptr++ = PaintOp_packOp(kColor_PaintOp);
1030 *ptr++ = paint.getColor();
reed@google.comf5842f72011-05-04 18:30:04 +00001031 base.setColor(paint.getColor());
reed@google.combb6992a2011-04-26 17:41:56 +00001032 }
1033 if (base.getStyle() != paint.getStyle()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001034 *ptr++ = PaintOp_packOpData(kStyle_PaintOp, paint.getStyle());
reed@google.comf5842f72011-05-04 18:30:04 +00001035 base.setStyle(paint.getStyle());
reed@google.combb6992a2011-04-26 17:41:56 +00001036 }
1037 if (base.getStrokeJoin() != paint.getStrokeJoin()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001038 *ptr++ = PaintOp_packOpData(kJoin_PaintOp, paint.getStrokeJoin());
reed@google.comf5842f72011-05-04 18:30:04 +00001039 base.setStrokeJoin(paint.getStrokeJoin());
reed@google.combb6992a2011-04-26 17:41:56 +00001040 }
1041 if (base.getStrokeCap() != paint.getStrokeCap()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001042 *ptr++ = PaintOp_packOpData(kCap_PaintOp, paint.getStrokeCap());
reed@google.comf5842f72011-05-04 18:30:04 +00001043 base.setStrokeCap(paint.getStrokeCap());
reed@google.combb6992a2011-04-26 17:41:56 +00001044 }
1045 if (base.getStrokeWidth() != paint.getStrokeWidth()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001046 *ptr++ = PaintOp_packOp(kWidth_PaintOp);
1047 *ptr++ = castToU32(paint.getStrokeWidth());
reed@google.comf5842f72011-05-04 18:30:04 +00001048 base.setStrokeWidth(paint.getStrokeWidth());
reed@google.combb6992a2011-04-26 17:41:56 +00001049 }
1050 if (base.getStrokeMiter() != paint.getStrokeMiter()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001051 *ptr++ = PaintOp_packOp(kMiter_PaintOp);
1052 *ptr++ = castToU32(paint.getStrokeMiter());
reed@google.comf5842f72011-05-04 18:30:04 +00001053 base.setStrokeMiter(paint.getStrokeMiter());
reed@google.combb6992a2011-04-26 17:41:56 +00001054 }
1055 if (base.getTextEncoding() != paint.getTextEncoding()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001056 *ptr++ = PaintOp_packOpData(kEncoding_PaintOp, paint.getTextEncoding());
reed@google.comf5842f72011-05-04 18:30:04 +00001057 base.setTextEncoding(paint.getTextEncoding());
reed@google.combb6992a2011-04-26 17:41:56 +00001058 }
1059 if (base.getHinting() != paint.getHinting()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001060 *ptr++ = PaintOp_packOpData(kHinting_PaintOp, paint.getHinting());
reed@google.comf5842f72011-05-04 18:30:04 +00001061 base.setHinting(paint.getHinting());
reed@google.combb6992a2011-04-26 17:41:56 +00001062 }
1063 if (base.getTextAlign() != paint.getTextAlign()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001064 *ptr++ = PaintOp_packOpData(kAlign_PaintOp, paint.getTextAlign());
reed@google.comf5842f72011-05-04 18:30:04 +00001065 base.setTextAlign(paint.getTextAlign());
reed@google.combb6992a2011-04-26 17:41:56 +00001066 }
1067 if (base.getTextSize() != paint.getTextSize()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001068 *ptr++ = PaintOp_packOp(kTextSize_PaintOp);
1069 *ptr++ = castToU32(paint.getTextSize());
reed@google.comf5842f72011-05-04 18:30:04 +00001070 base.setTextSize(paint.getTextSize());
reed@google.combb6992a2011-04-26 17:41:56 +00001071 }
1072 if (base.getTextScaleX() != paint.getTextScaleX()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001073 *ptr++ = PaintOp_packOp(kTextScaleX_PaintOp);
1074 *ptr++ = castToU32(paint.getTextScaleX());
reed@google.comf5842f72011-05-04 18:30:04 +00001075 base.setTextScaleX(paint.getTextScaleX());
reed@google.combb6992a2011-04-26 17:41:56 +00001076 }
1077 if (base.getTextSkewX() != paint.getTextSkewX()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001078 *ptr++ = PaintOp_packOp(kTextSkewX_PaintOp);
1079 *ptr++ = castToU32(paint.getTextSkewX());
reed@google.comf5842f72011-05-04 18:30:04 +00001080 base.setTextSkewX(paint.getTextSkewX());
1081 }
1082
1083 if (!SkTypeface::Equal(base.getTypeface(), paint.getTypeface())) {
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +00001084 if (isCrossProcess(fFlags)) {
scroggo@google.com3cb969f2012-07-27 20:39:19 +00001085 uint32_t id = this->getTypefaceID(paint.getTypeface());
1086 *ptr++ = PaintOp_packOpData(kTypeface_PaintOp, id);
1087 } else if (this->needOpBytes(sizeof(void*))) {
1088 // Add to the set for ref counting.
1089 fTypefaceSet.add(paint.getTypeface());
1090 // It is safe to write the typeface to the stream before the rest
1091 // of the paint unless we ever send a kReset_PaintOp, which we
1092 // currently never do.
1093 this->writeOp(kSetTypeface_DrawOp);
1094 fWriter.writePtr(paint.getTypeface());
1095 }
reed@google.comf5842f72011-05-04 18:30:04 +00001096 base.setTypeface(paint.getTypeface());
reed@google.combb6992a2011-04-26 17:41:56 +00001097 }
reed@google.combb6992a2011-04-26 17:41:56 +00001098
scroggo@google.com4dffc592012-07-17 16:49:40 +00001099 // This is a new paint, so all old flats can be safely purged, if necessary.
1100 fFlattenableHeap.markAllFlatsSafeToDelete();
reed@google.comb55d1182011-05-11 00:42:04 +00001101 for (int i = 0; i < kCount_PaintFlats; i++) {
1102 int index = this->flattenToIndex(get_paintflat(paint, i), (PaintFlats)i);
scroggo@google.comd3ba5cc2012-07-09 16:05:53 +00001103 bool replaced = index < 0;
1104 if (replaced) {
1105 index = ~index;
1106 }
scroggo@google.com4dffc592012-07-17 16:49:40 +00001107 // Store the index of any flat that needs to be kept. 0 means no flat.
1108 if (index > 0) {
1109 fFlattenableHeap.markFlatForKeeping(index);
1110 }
1111 SkASSERT(index >= 0 && index <= fFlatDictionary.count());
scroggo@google.comd3ba5cc2012-07-09 16:05:53 +00001112 if (index != fCurrFlatIndex[i] || replaced) {
reed@google.comb55d1182011-05-11 00:42:04 +00001113 *ptr++ = PaintOp_packOpFlagData(kFlatIndex_PaintOp, i, index);
1114 fCurrFlatIndex[i] = index;
1115 }
1116 }
1117
reed@google.comacd471f2011-05-03 21:26:46 +00001118 size_t size = (char*)ptr - (char*)storage;
1119 if (size && this->needOpBytes(size)) {
reed@google.comb55d1182011-05-11 00:42:04 +00001120 this->writeOp(kPaintOp_DrawOp, 0, size);
reed@google.comb55d1182011-05-11 00:42:04 +00001121 fWriter.write(storage, size);
reed@google.combb6992a2011-04-26 17:41:56 +00001122 for (size_t i = 0; i < size/4; i++) {
reed@google.comb55d1182011-05-11 00:42:04 +00001123// SkDebugf("[%d] %08X\n", i, storage[i]);
reed@google.combb6992a2011-04-26 17:41:56 +00001124 }
1125 }
reed@google.combb6992a2011-04-26 17:41:56 +00001126}
1127
1128///////////////////////////////////////////////////////////////////////////////
1129
1130#include "SkGPipe.h"
1131
scroggo@google.com3cb969f2012-07-27 20:39:19 +00001132SkGPipeController::~SkGPipeController() {
1133 SkSafeUnref(fCanvas);
1134}
1135
1136void SkGPipeController::setCanvas(SkGPipeCanvas* canvas) {
1137 SkRefCnt_SafeAssign(fCanvas, canvas);
1138}
1139
1140///////////////////////////////////////////////////////////////////////////////
1141
1142SkGPipeWriter::SkGPipeWriter()
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +00001143: fWriter(0) {
reed@google.combb6992a2011-04-26 17:41:56 +00001144 fCanvas = NULL;
1145}
1146
1147SkGPipeWriter::~SkGPipeWriter() {
reed@google.comacd471f2011-05-03 21:26:46 +00001148 this->endRecording();
reed@google.combb6992a2011-04-26 17:41:56 +00001149}
1150
junov@chromium.orga8db8fe2012-08-15 19:49:22 +00001151SkCanvas* SkGPipeWriter::startRecording(SkGPipeController* controller, uint32_t flags,
1152 uint32_t width, uint32_t height) {
reed@google.combb6992a2011-04-26 17:41:56 +00001153 if (NULL == fCanvas) {
reed@google.comacd471f2011-05-03 21:26:46 +00001154 fWriter.reset(NULL, 0);
junov@chromium.orga8db8fe2012-08-15 19:49:22 +00001155 fCanvas = SkNEW_ARGS(SkGPipeCanvas, (controller, &fWriter, flags, width, height));
reed@google.combb6992a2011-04-26 17:41:56 +00001156 }
scroggo@google.com3cb969f2012-07-27 20:39:19 +00001157 controller->setCanvas(fCanvas);
reed@google.combb6992a2011-04-26 17:41:56 +00001158 return fCanvas;
1159}
1160
1161void SkGPipeWriter::endRecording() {
1162 if (fCanvas) {
1163 fCanvas->finish();
1164 fCanvas->unref();
1165 fCanvas = NULL;
1166 }
1167}
1168
junov@chromium.org2e14ba82012-08-07 14:26:57 +00001169void SkGPipeWriter::flushRecording(bool detachCurrentBlock) {
1170 if (fCanvas) {
1171 fCanvas->flushRecording(detachCurrentBlock);
1172 }
junov@chromium.org77eec242012-07-18 17:54:45 +00001173}
1174
junov@chromium.org2e14ba82012-08-07 14:26:57 +00001175size_t SkGPipeWriter::freeMemoryIfPossible(size_t bytesToFree) {
1176 if (fCanvas) {
1177 return fCanvas->freeMemoryIfPossible(bytesToFree);
1178 }
1179 return 0;
1180}
1181
1182size_t SkGPipeWriter::storageAllocatedForRecording() const {
scroggo@google.com15011ee2012-07-26 20:03:32 +00001183 return NULL == fCanvas ? 0 : fCanvas->storageAllocatedForRecording();
1184}
1185
scroggo@google.com3e26bd02012-08-14 15:20:01 +00001186///////////////////////////////////////////////////////////////////////////////
1187
1188BitmapShuttle::BitmapShuttle(SkGPipeCanvas* canvas) {
1189 SkASSERT(canvas != NULL);
1190 fCanvas = canvas;
1191 fCanvas->ref();
1192}
1193
1194BitmapShuttle::~BitmapShuttle() {
1195 fCanvas->unref();
1196}
1197
1198bool BitmapShuttle::insert(const SkBitmap& bitmap, int32_t slot) {
1199 return fCanvas->shuttleBitmap(bitmap, slot);
1200}