blob: 34020dfe7f82984d04b4ffea35637af59efbc990 [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,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000234 const SkRect& dst, const SkPaint*) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000235 virtual void drawBitmapMatrix(const SkBitmap&, const SkMatrix&,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000236 const SkPaint*) SK_OVERRIDE;
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000237 virtual void drawBitmapNine(const SkBitmap& bitmap, const SkIRect& center,
238 const SkRect& dst, const SkPaint* paint = NULL) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000239 virtual void drawSprite(const SkBitmap&, int left, int top,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000240 const SkPaint*) SK_OVERRIDE;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000241 virtual void drawText(const void* text, size_t byteLength, SkScalar x,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000242 SkScalar y, const SkPaint&) SK_OVERRIDE;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000243 virtual void drawPosText(const void* text, size_t byteLength,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000244 const SkPoint pos[], const SkPaint&) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000245 virtual void drawPosTextH(const void* text, size_t byteLength,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000246 const SkScalar xpos[], SkScalar constY,
247 const SkPaint&) SK_OVERRIDE;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000248 virtual void drawTextOnPath(const void* text, size_t byteLength,
249 const SkPath& path, const SkMatrix* matrix,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000250 const SkPaint&) SK_OVERRIDE;
251 virtual void drawPicture(SkPicture& picture) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000252 virtual void drawVertices(VertexMode, int vertexCount,
253 const SkPoint vertices[], const SkPoint texs[],
254 const SkColor colors[], SkXfermode*,
255 const uint16_t indices[], int indexCount,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000256 const SkPaint&) SK_OVERRIDE;
257 virtual void drawData(const void*, size_t) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000258
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000259 /**
260 * Flatten an SkBitmap to send to the reader, where it will be referenced
261 * according to slot.
262 */
263 bool shuttleBitmap(const SkBitmap&, int32_t slot);
reed@google.combb6992a2011-04-26 17:41:56 +0000264private:
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000265 enum {
266 kNoSaveLayer = -1,
267 };
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000268 SkNamedFactorySet* fFactorySet;
269 int fFirstSaveLayerStackLevel;
scroggo@google.comd9d29672012-08-14 17:21:34 +0000270 SkBitmapHeap* fBitmapHeap;
reed@google.comacd471f2011-05-03 21:26:46 +0000271 SkGPipeController* fController;
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000272 SkWriter32& fWriter;
273 size_t fBlockSize; // amount allocated for writer
274 size_t fBytesNotified;
275 bool fDone;
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000276 const uint32_t fFlags;
reed@google.combb6992a2011-04-26 17:41:56 +0000277
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000278 SkRefCntSet fTypefaceSet;
reed@google.comf5842f72011-05-04 18:30:04 +0000279
280 uint32_t getTypefaceID(SkTypeface*);
281
reed@google.comacd471f2011-05-03 21:26:46 +0000282 inline void writeOp(DrawOps op, unsigned flags, unsigned data) {
reed@google.combb6992a2011-04-26 17:41:56 +0000283 fWriter.write32(DrawOp_packOpFlagData(op, flags, data));
284 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000285
reed@google.comacd471f2011-05-03 21:26:46 +0000286 inline void writeOp(DrawOps op) {
reed@google.combb6992a2011-04-26 17:41:56 +0000287 fWriter.write32(DrawOp_packOpFlagData(op, 0, 0));
288 }
reed@google.comacd471f2011-05-03 21:26:46 +0000289
290 bool needOpBytes(size_t size = 0);
291
292 inline void doNotify() {
293 if (!fDone) {
294 size_t bytes = fWriter.size() - fBytesNotified;
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000295 if (bytes > 0) {
296 fController->notifyWritten(bytes);
297 fBytesNotified += bytes;
298 }
reed@google.comacd471f2011-05-03 21:26:46 +0000299 }
300 }
reed@google.comb55d1182011-05-11 00:42:04 +0000301
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000302 // Should be called after any calls to an SkFlatDictionary::findAndReplace
303 // if a new SkFlatData was added when in cross process mode
304 void flattenFactoryNames();
305
scroggo@google.com4dffc592012-07-17 16:49:40 +0000306 FlattenableHeap fFlattenableHeap;
307 FlatDictionary fFlatDictionary;
reed@google.comb55d1182011-05-11 00:42:04 +0000308 int fCurrFlatIndex[kCount_PaintFlats];
309 int flattenToIndex(SkFlattenable* obj, PaintFlats);
310
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000311 // Common code used by drawBitmap*. Behaves differently depending on the
312 // type of SkBitmapHeap being used, which is determined by the flags used.
313 bool commonDrawBitmap(const SkBitmap& bm, DrawOps op, unsigned flags,
314 size_t opBytesNeeded, const SkPaint* paint);
scroggo@google.com58be6822012-07-30 14:40:01 +0000315
reed@google.com31891582011-05-12 03:03:56 +0000316 SkPaint fPaint;
317 void writePaint(const SkPaint&);
reed@google.combb6992a2011-04-26 17:41:56 +0000318
reed@google.comacd471f2011-05-03 21:26:46 +0000319 class AutoPipeNotify {
320 public:
321 AutoPipeNotify(SkGPipeCanvas* canvas) : fCanvas(canvas) {}
322 ~AutoPipeNotify() { fCanvas->doNotify(); }
323 private:
324 SkGPipeCanvas* fCanvas;
325 };
326 friend class AutoPipeNotify;
327
reed@google.combb6992a2011-04-26 17:41:56 +0000328 typedef SkCanvas INHERITED;
329};
330
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000331void SkGPipeCanvas::flattenFactoryNames() {
332 const char* name;
333 while ((name = fFactorySet->getNextAddedFactoryName()) != NULL) {
334 size_t len = strlen(name);
335 if (this->needOpBytes(len)) {
336 this->writeOp(kDef_Factory_DrawOp);
337 fWriter.writeString(name, len);
338 }
339 }
340}
341
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000342bool SkGPipeCanvas::shuttleBitmap(const SkBitmap& bm, int32_t slot) {
scroggo@google.com565254b2012-06-28 15:41:32 +0000343 SkASSERT(shouldFlattenBitmaps(fFlags));
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000344 SkOrderedWriteBuffer buffer(1024);
345 buffer.setNamedFactoryRecorder(fFactorySet);
346 bm.flatten(buffer);
347 this->flattenFactoryNames();
348 uint32_t size = buffer.size();
349 if (this->needOpBytes(size)) {
350 this->writeOp(kDef_Bitmap_DrawOp, 0, slot);
351 void* dst = static_cast<void*>(fWriter.reserve(size));
352 buffer.writeToMemory(dst);
353 return true;
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000354 }
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000355 return false;
scroggo@google.comd3ba5cc2012-07-09 16:05:53 +0000356}
357
reed@google.comb55d1182011-05-11 00:42:04 +0000358// return 0 for NULL (or unflattenable obj), or index-base-1
scroggo@google.comd3ba5cc2012-07-09 16:05:53 +0000359// return ~(index-base-1) if an old flattenable was replaced
reed@google.comb55d1182011-05-11 00:42:04 +0000360int SkGPipeCanvas::flattenToIndex(SkFlattenable* obj, PaintFlats paintflat) {
scroggo@google.comd5d158b2012-08-14 20:38:28 +0000361 SkASSERT(!fDone && fBitmapHeap != NULL);
reed@google.comb55d1182011-05-11 00:42:04 +0000362 if (NULL == obj) {
363 return 0;
364 }
reed@google.comb55d1182011-05-11 00:42:04 +0000365
scroggo@google.comd9d29672012-08-14 17:21:34 +0000366 fBitmapHeap->deferAddingOwners();
scroggo@google.com4dffc592012-07-17 16:49:40 +0000367 bool added, replaced;
scroggo@google.com664fab12012-08-14 19:22:05 +0000368 const SkFlatData* flat = fFlatDictionary.findAndReplace(*obj, fFlattenableHeap.flatToReplace(),
369 &added, &replaced);
scroggo@google.comd9d29672012-08-14 17:21:34 +0000370 fBitmapHeap->endAddingOwnersDeferral(added);
scroggo@google.com4dffc592012-07-17 16:49:40 +0000371 int index = flat->index();
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000372 if (added) {
373 if (isCrossProcess(fFlags)) {
374 this->flattenFactoryNames();
375 }
376 size_t flatSize = flat->flatSize();
377 if (this->needOpBytes(flatSize)) {
378 this->writeOp(kDef_Flattenable_DrawOp, paintflat, index);
379 fWriter.write(flat->data(), flatSize);
380 }
scroggo@google.com4dffc592012-07-17 16:49:40 +0000381 }
382 if (replaced) {
reed@google.comb55d1182011-05-11 00:42:04 +0000383 index = ~index;
reed@google.comb55d1182011-05-11 00:42:04 +0000384 }
scroggo@google.com4dffc592012-07-17 16:49:40 +0000385 return index;
reed@google.comb55d1182011-05-11 00:42:04 +0000386}
387
reed@google.combb6992a2011-04-26 17:41:56 +0000388///////////////////////////////////////////////////////////////////////////////
389
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000390/**
391 * If SkBitmaps are to be flattened to send to the reader, this class is
392 * provided to the SkBitmapHeap to tell the SkGPipeCanvas to do so.
393 */
394class BitmapShuttle : public SkBitmapHeap::ExternalStorage {
395public:
396 BitmapShuttle(SkGPipeCanvas*);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000397
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000398 ~BitmapShuttle();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000399
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000400 virtual bool insert(const SkBitmap& bitmap, int32_t slot) SK_OVERRIDE;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000401
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000402private:
403 SkGPipeCanvas* fCanvas;
404};
405
406///////////////////////////////////////////////////////////////////////////////
407
reed@google.comacd471f2011-05-03 21:26:46 +0000408#define MIN_BLOCK_SIZE (16 * 1024)
scroggo@google.com4dffc592012-07-17 16:49:40 +0000409#define BITMAPS_TO_KEEP 5
410#define FLATTENABLES_TO_KEEP 10
reed@google.combb6992a2011-04-26 17:41:56 +0000411
reed@google.comacd471f2011-05-03 21:26:46 +0000412SkGPipeCanvas::SkGPipeCanvas(SkGPipeController* controller,
rmistry@google.comd6176b02012-08-23 18:14:13 +0000413 SkWriter32* writer, uint32_t flags,
junov@chromium.orga8db8fe2012-08-15 19:49:22 +0000414 uint32_t width, uint32_t height)
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000415: fFactorySet(isCrossProcess(flags) ? SkNEW(SkNamedFactorySet) : NULL)
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000416, fWriter(*writer)
417, fFlags(flags)
scroggo@google.com664fab12012-08-14 19:22:05 +0000418, fFlattenableHeap(FLATTENABLES_TO_KEEP, fFactorySet, isCrossProcess(flags))
scroggo@google.com15543602012-08-02 18:49:49 +0000419, fFlatDictionary(&fFlattenableHeap) {
reed@google.comacd471f2011-05-03 21:26:46 +0000420 fController = controller;
reed@google.combb6992a2011-04-26 17:41:56 +0000421 fDone = false;
reed@google.comacd471f2011-05-03 21:26:46 +0000422 fBlockSize = 0; // need first block from controller
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000423 fBytesNotified = 0;
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000424 fFirstSaveLayerStackLevel = kNoSaveLayer;
reed@google.comb55d1182011-05-11 00:42:04 +0000425 sk_bzero(fCurrFlatIndex, sizeof(fCurrFlatIndex));
reed@google.comacd471f2011-05-03 21:26:46 +0000426
reed@google.combb6793b2011-05-05 15:18:15 +0000427 // we need a device to limit our clip
yangsu@google.com06b4da162011-06-17 15:04:40 +0000428 // We don't allocate pixels for the bitmap
reed@google.combb6793b2011-05-05 15:18:15 +0000429 SkBitmap bitmap;
junov@chromium.orga8db8fe2012-08-15 19:49:22 +0000430 bitmap.setConfig(SkBitmap::kARGB_8888_Config, width, height);
yangsu@google.com06b4da162011-06-17 15:04:40 +0000431 SkDevice* device = SkNEW_ARGS(SkDevice, (bitmap));
reed@google.combb6793b2011-05-05 15:18:15 +0000432 this->setDevice(device)->unref();
scroggo@google.com10dccde2012-08-08 20:43:22 +0000433
scroggo@google.com565254b2012-06-28 15:41:32 +0000434 // Tell the reader the appropriate flags to use.
435 if (this->needOpBytes()) {
436 this->writeOp(kReportFlags_DrawOp, fFlags, 0);
437 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000438
scroggo@google.com10dccde2012-08-08 20:43:22 +0000439 if (shouldFlattenBitmaps(flags)) {
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000440 BitmapShuttle* shuttle = SkNEW_ARGS(BitmapShuttle, (this));
scroggo@google.comd9d29672012-08-14 17:21:34 +0000441 fBitmapHeap = SkNEW_ARGS(SkBitmapHeap, (shuttle, BITMAPS_TO_KEEP));
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000442 shuttle->unref();
scroggo@google.com10dccde2012-08-08 20:43:22 +0000443 } else {
scroggo@google.comd9d29672012-08-14 17:21:34 +0000444 fBitmapHeap = SkNEW_ARGS(SkBitmapHeap,
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000445 (BITMAPS_TO_KEEP, controller->numberOfReaders()));
scroggo@google.com10dccde2012-08-08 20:43:22 +0000446 if (this->needOpBytes(sizeof(void*))) {
scroggo@google.comd9d29672012-08-14 17:21:34 +0000447 this->writeOp(kShareBitmapHeap_DrawOp);
448 fWriter.writePtr(static_cast<void*>(fBitmapHeap));
scroggo@google.com10dccde2012-08-08 20:43:22 +0000449 }
450 }
scroggo@google.comd9d29672012-08-14 17:21:34 +0000451 fFlattenableHeap.setBitmapStorage(fBitmapHeap);
scroggo@google.com10dccde2012-08-08 20:43:22 +0000452 this->doNotify();
reed@google.combb6992a2011-04-26 17:41:56 +0000453}
454
455SkGPipeCanvas::~SkGPipeCanvas() {
456 this->finish();
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000457 SkSafeUnref(fFactorySet);
scroggo@google.comd9d29672012-08-14 17:21:34 +0000458 SkSafeUnref(fBitmapHeap);
reed@google.combb6992a2011-04-26 17:41:56 +0000459}
460
reed@google.comacd471f2011-05-03 21:26:46 +0000461bool SkGPipeCanvas::needOpBytes(size_t needed) {
462 if (fDone) {
463 return false;
464 }
465
466 needed += 4; // size of DrawOp atom
467 if (fWriter.size() + needed > fBlockSize) {
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000468 // Before we wipe out any data that has already been written, read it
469 // out.
470 this->doNotify();
471 size_t blockSize = SkMax32(MIN_BLOCK_SIZE, needed);
472 void* block = fController->requestBlock(blockSize, &fBlockSize);
reed@google.comacd471f2011-05-03 21:26:46 +0000473 if (NULL == block) {
474 fDone = true;
475 return false;
476 }
scroggo@google.com460a23e2012-08-16 17:56:49 +0000477 SkASSERT(SkIsAlign4(fBlockSize));
reed@google.comacd471f2011-05-03 21:26:46 +0000478 fWriter.reset(block, fBlockSize);
479 fBytesNotified = 0;
480 }
481 return true;
482}
483
reed@google.comf5842f72011-05-04 18:30:04 +0000484uint32_t SkGPipeCanvas::getTypefaceID(SkTypeface* face) {
485 uint32_t id = 0; // 0 means default/null typeface
486 if (face) {
487 id = fTypefaceSet.find(face);
488 if (0 == id) {
489 id = fTypefaceSet.add(face);
490 size_t size = writeTypeface(NULL, face);
491 if (this->needOpBytes(size)) {
reed@google.combb6793b2011-05-05 15:18:15 +0000492 this->writeOp(kDef_Typeface_DrawOp);
reed@google.comf5842f72011-05-04 18:30:04 +0000493 writeTypeface(&fWriter, face);
494 }
495 }
496 }
497 return id;
498}
499
reed@google.combb6992a2011-04-26 17:41:56 +0000500///////////////////////////////////////////////////////////////////////////////
501
reed@google.comacd471f2011-05-03 21:26:46 +0000502#define NOTIFY_SETUP(canvas) \
503 AutoPipeNotify apn(canvas)
504
reed@google.combb6992a2011-04-26 17:41:56 +0000505int SkGPipeCanvas::save(SaveFlags flags) {
reed@google.comacd471f2011-05-03 21:26:46 +0000506 NOTIFY_SETUP(this);
507 if (this->needOpBytes()) {
508 this->writeOp(kSave_DrawOp, 0, flags);
509 }
reed@google.combb6992a2011-04-26 17:41:56 +0000510 return this->INHERITED::save(flags);
511}
512
513int SkGPipeCanvas::saveLayer(const SkRect* bounds, const SkPaint* paint,
514 SaveFlags saveFlags) {
reed@google.comacd471f2011-05-03 21:26:46 +0000515 NOTIFY_SETUP(this);
516 size_t size = 0;
reed@google.combb6992a2011-04-26 17:41:56 +0000517 unsigned opFlags = 0;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000518
reed@google.combb6992a2011-04-26 17:41:56 +0000519 if (bounds) {
520 opFlags |= kSaveLayer_HasBounds_DrawOpFlag;
reed@google.comacd471f2011-05-03 21:26:46 +0000521 size += sizeof(SkRect);
reed@google.combb6992a2011-04-26 17:41:56 +0000522 }
523 if (paint) {
524 opFlags |= kSaveLayer_HasPaint_DrawOpFlag;
reed@google.com31891582011-05-12 03:03:56 +0000525 this->writePaint(*paint);
reed@google.combb6992a2011-04-26 17:41:56 +0000526 }
527
reed@google.comacd471f2011-05-03 21:26:46 +0000528 if (this->needOpBytes(size)) {
529 this->writeOp(kSaveLayer_DrawOp, opFlags, saveFlags);
530 if (bounds) {
531 fWriter.writeRect(*bounds);
532 }
reed@google.combb6992a2011-04-26 17:41:56 +0000533 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000534
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000535 if (kNoSaveLayer == fFirstSaveLayerStackLevel){
536 fFirstSaveLayerStackLevel = this->getSaveCount();
537 }
reed@google.combb6992a2011-04-26 17:41:56 +0000538 // we just pass on the save, so we don't create a layer
539 return this->INHERITED::save(saveFlags);
540}
541
542void SkGPipeCanvas::restore() {
reed@google.comacd471f2011-05-03 21:26:46 +0000543 NOTIFY_SETUP(this);
544 if (this->needOpBytes()) {
545 this->writeOp(kRestore_DrawOp);
546 }
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000547
reed@google.combb6992a2011-04-26 17:41:56 +0000548 this->INHERITED::restore();
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000549
550 if (this->getSaveCount() == fFirstSaveLayerStackLevel){
551 fFirstSaveLayerStackLevel = kNoSaveLayer;
552 }
553}
554
555bool SkGPipeCanvas::isDrawingToLayer() const {
556 return kNoSaveLayer != fFirstSaveLayerStackLevel;
reed@google.combb6992a2011-04-26 17:41:56 +0000557}
558
559bool SkGPipeCanvas::translate(SkScalar dx, SkScalar dy) {
560 if (dx || dy) {
reed@google.comacd471f2011-05-03 21:26:46 +0000561 NOTIFY_SETUP(this);
562 if (this->needOpBytes(2 * sizeof(SkScalar))) {
563 this->writeOp(kTranslate_DrawOp);
564 fWriter.writeScalar(dx);
565 fWriter.writeScalar(dy);
566 }
reed@google.combb6992a2011-04-26 17:41:56 +0000567 }
568 return this->INHERITED::translate(dx, dy);
569}
570
571bool SkGPipeCanvas::scale(SkScalar sx, SkScalar sy) {
572 if (sx || sy) {
reed@google.comacd471f2011-05-03 21:26:46 +0000573 NOTIFY_SETUP(this);
574 if (this->needOpBytes(2 * sizeof(SkScalar))) {
575 this->writeOp(kScale_DrawOp);
576 fWriter.writeScalar(sx);
577 fWriter.writeScalar(sy);
578 }
reed@google.combb6992a2011-04-26 17:41:56 +0000579 }
580 return this->INHERITED::scale(sx, sy);
581}
582
583bool SkGPipeCanvas::rotate(SkScalar degrees) {
584 if (degrees) {
reed@google.comacd471f2011-05-03 21:26:46 +0000585 NOTIFY_SETUP(this);
586 if (this->needOpBytes(sizeof(SkScalar))) {
587 this->writeOp(kRotate_DrawOp);
588 fWriter.writeScalar(degrees);
589 }
reed@google.combb6992a2011-04-26 17:41:56 +0000590 }
591 return this->INHERITED::rotate(degrees);
592}
593
594bool SkGPipeCanvas::skew(SkScalar sx, SkScalar sy) {
595 if (sx || sy) {
reed@google.comacd471f2011-05-03 21:26:46 +0000596 NOTIFY_SETUP(this);
597 if (this->needOpBytes(2 * sizeof(SkScalar))) {
598 this->writeOp(kSkew_DrawOp);
599 fWriter.writeScalar(sx);
600 fWriter.writeScalar(sy);
601 }
reed@google.combb6992a2011-04-26 17:41:56 +0000602 }
603 return this->INHERITED::skew(sx, sy);
604}
605
606bool SkGPipeCanvas::concat(const SkMatrix& matrix) {
607 if (!matrix.isIdentity()) {
reed@google.comacd471f2011-05-03 21:26:46 +0000608 NOTIFY_SETUP(this);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000609 if (this->needOpBytes(matrix.writeToMemory(NULL))) {
reed@google.comacd471f2011-05-03 21:26:46 +0000610 this->writeOp(kConcat_DrawOp);
djsollen@google.com2b2ede32012-04-12 13:24:04 +0000611 fWriter.writeMatrix(matrix);
reed@google.comacd471f2011-05-03 21:26:46 +0000612 }
reed@google.combb6992a2011-04-26 17:41:56 +0000613 }
614 return this->INHERITED::concat(matrix);
615}
616
617void SkGPipeCanvas::setMatrix(const SkMatrix& matrix) {
reed@google.comacd471f2011-05-03 21:26:46 +0000618 NOTIFY_SETUP(this);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000619 if (this->needOpBytes(matrix.writeToMemory(NULL))) {
reed@google.comacd471f2011-05-03 21:26:46 +0000620 this->writeOp(kSetMatrix_DrawOp);
djsollen@google.com2b2ede32012-04-12 13:24:04 +0000621 fWriter.writeMatrix(matrix);
reed@google.comacd471f2011-05-03 21:26:46 +0000622 }
reed@google.combb6992a2011-04-26 17:41:56 +0000623 this->INHERITED::setMatrix(matrix);
624}
625
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000626bool SkGPipeCanvas::clipRect(const SkRect& rect, SkRegion::Op rgnOp,
627 bool doAntiAlias) {
reed@google.comacd471f2011-05-03 21:26:46 +0000628 NOTIFY_SETUP(this);
scroggo@google.com460a23e2012-08-16 17:56:49 +0000629 if (this->needOpBytes(sizeof(SkRect))) {
630 unsigned flags = doAntiAlias & kClip_HasAntiAlias_DrawOpFlag;
631 this->writeOp(kClipRect_DrawOp, flags, rgnOp);
reed@google.comacd471f2011-05-03 21:26:46 +0000632 fWriter.writeRect(rect);
633 }
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000634 return this->INHERITED::clipRect(rect, rgnOp, doAntiAlias);
reed@google.combb6992a2011-04-26 17:41:56 +0000635}
636
reed@google.com4ed0fb72012-12-12 20:48:18 +0000637bool SkGPipeCanvas::clipRRect(const SkRRect& rrect, SkRegion::Op rgnOp,
638 bool doAntiAlias) {
639 NOTIFY_SETUP(this);
640 if (this->needOpBytes(kSizeOfFlatRRect)) {
641 unsigned flags = doAntiAlias & kClip_HasAntiAlias_DrawOpFlag;
642 this->writeOp(kClipRRect_DrawOp, flags, rgnOp);
643 fWriter.writeRRect(rrect);
644 }
645 return this->INHERITED::clipRRect(rrect, rgnOp, doAntiAlias);
646}
647
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000648bool SkGPipeCanvas::clipPath(const SkPath& path, SkRegion::Op rgnOp,
649 bool doAntiAlias) {
reed@google.comacd471f2011-05-03 21:26:46 +0000650 NOTIFY_SETUP(this);
scroggo@google.com460a23e2012-08-16 17:56:49 +0000651 if (this->needOpBytes(path.writeToMemory(NULL))) {
652 unsigned flags = doAntiAlias & kClip_HasAntiAlias_DrawOpFlag;
653 this->writeOp(kClipPath_DrawOp, flags, rgnOp);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000654 fWriter.writePath(path);
reed@google.comacd471f2011-05-03 21:26:46 +0000655 }
reed@google.combb6992a2011-04-26 17:41:56 +0000656 // we just pass on the bounds of the path
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000657 return this->INHERITED::clipRect(path.getBounds(), rgnOp, doAntiAlias);
reed@google.combb6992a2011-04-26 17:41:56 +0000658}
659
660bool SkGPipeCanvas::clipRegion(const SkRegion& region, SkRegion::Op rgnOp) {
reed@google.comacd471f2011-05-03 21:26:46 +0000661 NOTIFY_SETUP(this);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000662 if (this->needOpBytes(region.writeToMemory(NULL))) {
reed@google.comacd471f2011-05-03 21:26:46 +0000663 this->writeOp(kClipRegion_DrawOp, 0, rgnOp);
djsollen@google.com2b2ede32012-04-12 13:24:04 +0000664 fWriter.writeRegion(region);
reed@google.comacd471f2011-05-03 21:26:46 +0000665 }
reed@google.combb6992a2011-04-26 17:41:56 +0000666 return this->INHERITED::clipRegion(region, rgnOp);
667}
668
669///////////////////////////////////////////////////////////////////////////////
670
671void SkGPipeCanvas::clear(SkColor color) {
reed@google.comacd471f2011-05-03 21:26:46 +0000672 NOTIFY_SETUP(this);
reed@google.combb6992a2011-04-26 17:41:56 +0000673 unsigned flags = 0;
674 if (color) {
675 flags |= kClear_HasColor_DrawOpFlag;
676 }
reed@google.comacd471f2011-05-03 21:26:46 +0000677 if (this->needOpBytes(sizeof(SkColor))) {
678 this->writeOp(kDrawClear_DrawOp, flags, 0);
679 if (color) {
680 fWriter.write32(color);
681 }
reed@google.combb6992a2011-04-26 17:41:56 +0000682 }
683}
684
685void SkGPipeCanvas::drawPaint(const SkPaint& paint) {
reed@google.comacd471f2011-05-03 21:26:46 +0000686 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000687 this->writePaint(paint);
reed@google.comacd471f2011-05-03 21:26:46 +0000688 if (this->needOpBytes()) {
reed@google.com31891582011-05-12 03:03:56 +0000689 this->writeOp(kDrawPaint_DrawOp);
reed@google.comacd471f2011-05-03 21:26:46 +0000690 }
reed@google.combb6992a2011-04-26 17:41:56 +0000691}
692
693void SkGPipeCanvas::drawPoints(PointMode mode, size_t count,
694 const SkPoint pts[], const SkPaint& paint) {
695 if (count) {
reed@google.comacd471f2011-05-03 21:26:46 +0000696 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000697 this->writePaint(paint);
reed@google.comacd471f2011-05-03 21:26:46 +0000698 if (this->needOpBytes(4 + count * sizeof(SkPoint))) {
reed@google.com31891582011-05-12 03:03:56 +0000699 this->writeOp(kDrawPoints_DrawOp, mode, 0);
reed@google.comacd471f2011-05-03 21:26:46 +0000700 fWriter.write32(count);
701 fWriter.write(pts, count * sizeof(SkPoint));
702 }
reed@google.combb6992a2011-04-26 17:41:56 +0000703 }
704}
705
reed@google.com4ed0fb72012-12-12 20:48:18 +0000706void SkGPipeCanvas::drawOval(const SkRect& rect, const SkPaint& paint) {
707 NOTIFY_SETUP(this);
708 this->writePaint(paint);
709 if (this->needOpBytes(sizeof(SkRect))) {
710 this->writeOp(kDrawOval_DrawOp);
711 fWriter.writeRect(rect);
712 }
713}
714
reed@google.combb6992a2011-04-26 17:41:56 +0000715void SkGPipeCanvas::drawRect(const SkRect& rect, const SkPaint& paint) {
reed@google.comacd471f2011-05-03 21:26:46 +0000716 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000717 this->writePaint(paint);
reed@google.comacd471f2011-05-03 21:26:46 +0000718 if (this->needOpBytes(sizeof(SkRect))) {
reed@google.com31891582011-05-12 03:03:56 +0000719 this->writeOp(kDrawRect_DrawOp);
reed@google.comacd471f2011-05-03 21:26:46 +0000720 fWriter.writeRect(rect);
721 }
reed@google.combb6992a2011-04-26 17:41:56 +0000722}
723
reed@google.com4ed0fb72012-12-12 20:48:18 +0000724void SkGPipeCanvas::drawRRect(const SkRRect& rrect, const SkPaint& paint) {
725 NOTIFY_SETUP(this);
726 this->writePaint(paint);
727 if (this->needOpBytes(kSizeOfFlatRRect)) {
728 this->writeOp(kDrawRRect_DrawOp);
729 fWriter.writeRRect(rrect);
730 }
731}
732
reed@google.combb6992a2011-04-26 17:41:56 +0000733void SkGPipeCanvas::drawPath(const SkPath& path, const SkPaint& paint) {
reed@google.comacd471f2011-05-03 21:26:46 +0000734 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000735 this->writePaint(paint);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000736 if (this->needOpBytes(path.writeToMemory(NULL))) {
reed@google.com31891582011-05-12 03:03:56 +0000737 this->writeOp(kDrawPath_DrawOp);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000738 fWriter.writePath(path);
reed@google.comacd471f2011-05-03 21:26:46 +0000739 }
reed@google.combb6992a2011-04-26 17:41:56 +0000740}
741
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000742bool SkGPipeCanvas::commonDrawBitmap(const SkBitmap& bm, DrawOps op,
743 unsigned flags,
744 size_t opBytesNeeded,
745 const SkPaint* paint) {
scroggo@google.com58be6822012-07-30 14:40:01 +0000746 if (paint != NULL) {
scroggo@google.com460a23e2012-08-16 17:56:49 +0000747 flags |= kDrawBitmap_HasPaint_DrawOpFlag;
scroggo@google.com58be6822012-07-30 14:40:01 +0000748 this->writePaint(*paint);
749 }
scroggo@google.com10dccde2012-08-08 20:43:22 +0000750 if (this->needOpBytes(opBytesNeeded)) {
scroggo@google.comd5d158b2012-08-14 20:38:28 +0000751 SkASSERT(fBitmapHeap != NULL);
752 int32_t bitmapIndex = fBitmapHeap->insert(bm);
753 if (SkBitmapHeap::INVALID_SLOT == bitmapIndex) {
754 return false;
755 }
scroggo@google.com10dccde2012-08-08 20:43:22 +0000756 this->writeOp(op, flags, bitmapIndex);
scroggo@google.com58be6822012-07-30 14:40:01 +0000757 return true;
758 }
759 return false;
760}
761
762void SkGPipeCanvas::drawBitmap(const SkBitmap& bm, SkScalar left, SkScalar top,
763 const SkPaint* paint) {
764 NOTIFY_SETUP(this);
765 size_t opBytesNeeded = sizeof(SkScalar) * 2;
766
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000767 if (this->commonDrawBitmap(bm, kDrawBitmap_DrawOp, 0, opBytesNeeded, paint)) {
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000768 fWriter.writeScalar(left);
769 fWriter.writeScalar(top);
770 }
reed@google.combb6992a2011-04-26 17:41:56 +0000771}
772
reed@google.com71121732012-09-18 15:14:33 +0000773void SkGPipeCanvas::drawBitmapRectToRect(const SkBitmap& bm, const SkRect* src,
scroggo@google.com58be6822012-07-30 14:40:01 +0000774 const SkRect& dst, const SkPaint* paint) {
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000775 NOTIFY_SETUP(this);
scroggo@google.com58be6822012-07-30 14:40:01 +0000776 size_t opBytesNeeded = sizeof(SkRect);
777 bool hasSrc = src != NULL;
778 unsigned flags;
779 if (hasSrc) {
scroggo@google.com460a23e2012-08-16 17:56:49 +0000780 flags = kDrawBitmap_HasSrcRect_DrawOpFlag;
scroggo@google.com58be6822012-07-30 14:40:01 +0000781 opBytesNeeded += sizeof(int32_t) * 4;
782 } else {
783 flags = 0;
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000784 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000785
reed@google.com71121732012-09-18 15:14:33 +0000786 if (this->commonDrawBitmap(bm, kDrawBitmapRectToRect_DrawOp, flags, opBytesNeeded, paint)) {
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000787 if (hasSrc) {
reed@google.com71121732012-09-18 15:14:33 +0000788 fWriter.writeRect(*src);
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000789 }
790 fWriter.writeRect(dst);
791 }
reed@google.combb6992a2011-04-26 17:41:56 +0000792}
793
scroggo@google.com58be6822012-07-30 14:40:01 +0000794void SkGPipeCanvas::drawBitmapMatrix(const SkBitmap& bm, const SkMatrix& matrix,
795 const SkPaint* paint) {
796 NOTIFY_SETUP(this);
797 size_t opBytesNeeded = matrix.writeToMemory(NULL);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000798
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000799 if (this->commonDrawBitmap(bm, kDrawBitmapMatrix_DrawOp, 0, opBytesNeeded, paint)) {
scroggo@google.com58be6822012-07-30 14:40:01 +0000800 fWriter.writeMatrix(matrix);
801 }
reed@google.combb6992a2011-04-26 17:41:56 +0000802}
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000803
804void SkGPipeCanvas::drawBitmapNine(const SkBitmap& bm, const SkIRect& center,
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000805 const SkRect& dst, const SkPaint* paint) {
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000806 NOTIFY_SETUP(this);
scroggo@google.com58be6822012-07-30 14:40:01 +0000807 size_t opBytesNeeded = sizeof(int32_t) * 4 + sizeof(SkRect);
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000808
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000809 if (this->commonDrawBitmap(bm, kDrawBitmapNine_DrawOp, 0, opBytesNeeded, paint)) {
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000810 fWriter.write32(center.fLeft);
811 fWriter.write32(center.fTop);
812 fWriter.write32(center.fRight);
813 fWriter.write32(center.fBottom);
814 fWriter.writeRect(dst);
815 }
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000816}
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000817
818void SkGPipeCanvas::drawSprite(const SkBitmap& bm, int left, int top,
819 const SkPaint* paint) {
820 NOTIFY_SETUP(this);
scroggo@google.com58be6822012-07-30 14:40:01 +0000821 size_t opBytesNeeded = sizeof(int32_t) * 2;
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000822
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000823 if (this->commonDrawBitmap(bm, kDrawSprite_DrawOp, 0, opBytesNeeded, paint)) {
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000824 fWriter.write32(left);
825 fWriter.write32(top);
826 }
reed@google.combb6992a2011-04-26 17:41:56 +0000827}
828
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000829void SkGPipeCanvas::drawText(const void* text, size_t byteLength, SkScalar x,
reed@google.combb6992a2011-04-26 17:41:56 +0000830 SkScalar y, const SkPaint& paint) {
831 if (byteLength) {
reed@google.comacd471f2011-05-03 21:26:46 +0000832 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000833 this->writePaint(paint);
reed@google.comacd471f2011-05-03 21:26:46 +0000834 if (this->needOpBytes(4 + SkAlign4(byteLength) + 2 * sizeof(SkScalar))) {
reed@google.com31891582011-05-12 03:03:56 +0000835 this->writeOp(kDrawText_DrawOp);
reed@google.comacd471f2011-05-03 21:26:46 +0000836 fWriter.write32(byteLength);
837 fWriter.writePad(text, byteLength);
838 fWriter.writeScalar(x);
839 fWriter.writeScalar(y);
840 }
reed@google.combb6992a2011-04-26 17:41:56 +0000841 }
842}
843
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000844void SkGPipeCanvas::drawPosText(const void* text, size_t byteLength,
reed@google.combb6992a2011-04-26 17:41:56 +0000845 const SkPoint pos[], const SkPaint& paint) {
846 if (byteLength) {
reed@google.comacd471f2011-05-03 21:26:46 +0000847 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000848 this->writePaint(paint);
reed@google.combb6992a2011-04-26 17:41:56 +0000849 int count = paint.textToGlyphs(text, byteLength, NULL);
reed@google.comacd471f2011-05-03 21:26:46 +0000850 if (this->needOpBytes(4 + SkAlign4(byteLength) + 4 + count * sizeof(SkPoint))) {
reed@google.com31891582011-05-12 03:03:56 +0000851 this->writeOp(kDrawPosText_DrawOp);
reed@google.comacd471f2011-05-03 21:26:46 +0000852 fWriter.write32(byteLength);
853 fWriter.writePad(text, byteLength);
854 fWriter.write32(count);
855 fWriter.write(pos, count * sizeof(SkPoint));
856 }
reed@google.combb6992a2011-04-26 17:41:56 +0000857 }
858}
859
860void SkGPipeCanvas::drawPosTextH(const void* text, size_t byteLength,
861 const SkScalar xpos[], SkScalar constY,
862 const SkPaint& paint) {
863 if (byteLength) {
reed@google.comacd471f2011-05-03 21:26:46 +0000864 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000865 this->writePaint(paint);
reed@google.combb6992a2011-04-26 17:41:56 +0000866 int count = paint.textToGlyphs(text, byteLength, NULL);
reed@google.comacd471f2011-05-03 21:26:46 +0000867 if (this->needOpBytes(4 + SkAlign4(byteLength) + 4 + count * sizeof(SkScalar) + 4)) {
reed@google.com31891582011-05-12 03:03:56 +0000868 this->writeOp(kDrawPosTextH_DrawOp);
reed@google.comacd471f2011-05-03 21:26:46 +0000869 fWriter.write32(byteLength);
870 fWriter.writePad(text, byteLength);
871 fWriter.write32(count);
872 fWriter.write(xpos, count * sizeof(SkScalar));
873 fWriter.writeScalar(constY);
874 }
reed@google.combb6992a2011-04-26 17:41:56 +0000875 }
876}
877
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000878void SkGPipeCanvas::drawTextOnPath(const void* text, size_t byteLength,
879 const SkPath& path, const SkMatrix* matrix,
reed@google.combb6992a2011-04-26 17:41:56 +0000880 const SkPaint& paint) {
881 if (byteLength) {
reed@google.comacd471f2011-05-03 21:26:46 +0000882 NOTIFY_SETUP(this);
reed@google.combb6992a2011-04-26 17:41:56 +0000883 unsigned flags = 0;
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000884 size_t size = 4 + SkAlign4(byteLength) + path.writeToMemory(NULL);
reed@google.combb6992a2011-04-26 17:41:56 +0000885 if (matrix) {
886 flags |= kDrawTextOnPath_HasMatrix_DrawOpFlag;
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000887 size += matrix->writeToMemory(NULL);
reed@google.combb6992a2011-04-26 17:41:56 +0000888 }
reed@google.com31891582011-05-12 03:03:56 +0000889 this->writePaint(paint);
reed@google.comacd471f2011-05-03 21:26:46 +0000890 if (this->needOpBytes(size)) {
reed@google.com31891582011-05-12 03:03:56 +0000891 this->writeOp(kDrawTextOnPath_DrawOp, flags, 0);
reed@google.combb6992a2011-04-26 17:41:56 +0000892
reed@google.comacd471f2011-05-03 21:26:46 +0000893 fWriter.write32(byteLength);
894 fWriter.writePad(text, byteLength);
reed@google.combb6992a2011-04-26 17:41:56 +0000895
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000896 fWriter.writePath(path);
reed@google.comacd471f2011-05-03 21:26:46 +0000897 if (matrix) {
djsollen@google.com2b2ede32012-04-12 13:24:04 +0000898 fWriter.writeMatrix(*matrix);
reed@google.comacd471f2011-05-03 21:26:46 +0000899 }
reed@google.combb6992a2011-04-26 17:41:56 +0000900 }
901 }
902}
903
904void SkGPipeCanvas::drawPicture(SkPicture& picture) {
reed@google.com0faac1e2011-05-11 05:58:58 +0000905 // we want to playback the picture into individual draw calls
906 this->INHERITED::drawPicture(picture);
reed@google.combb6992a2011-04-26 17:41:56 +0000907}
908
reed@google.combb6992a2011-04-26 17:41:56 +0000909void SkGPipeCanvas::drawVertices(VertexMode mode, int vertexCount,
910 const SkPoint vertices[], const SkPoint texs[],
911 const SkColor colors[], SkXfermode*,
912 const uint16_t indices[], int indexCount,
913 const SkPaint& paint) {
914 if (0 == vertexCount) {
915 return;
916 }
917
reed@google.comacd471f2011-05-03 21:26:46 +0000918 NOTIFY_SETUP(this);
919 size_t size = 4 + vertexCount * sizeof(SkPoint);
reed@google.com31891582011-05-12 03:03:56 +0000920 this->writePaint(paint);
reed@google.combb6992a2011-04-26 17:41:56 +0000921 unsigned flags = 0;
922 if (texs) {
923 flags |= kDrawVertices_HasTexs_DrawOpFlag;
reed@google.comacd471f2011-05-03 21:26:46 +0000924 size += vertexCount * sizeof(SkPoint);
reed@google.combb6992a2011-04-26 17:41:56 +0000925 }
926 if (colors) {
927 flags |= kDrawVertices_HasColors_DrawOpFlag;
reed@google.comacd471f2011-05-03 21:26:46 +0000928 size += vertexCount * sizeof(SkColor);
reed@google.combb6992a2011-04-26 17:41:56 +0000929 }
930 if (indices && indexCount > 0) {
931 flags |= kDrawVertices_HasIndices_DrawOpFlag;
reed@google.comacd471f2011-05-03 21:26:46 +0000932 size += 4 + SkAlign4(indexCount * sizeof(uint16_t));
reed@google.combb6992a2011-04-26 17:41:56 +0000933 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000934
reed@google.comacd471f2011-05-03 21:26:46 +0000935 if (this->needOpBytes(size)) {
reed@google.com31891582011-05-12 03:03:56 +0000936 this->writeOp(kDrawVertices_DrawOp, flags, 0);
reed@google.comacd471f2011-05-03 21:26:46 +0000937 fWriter.write32(mode);
938 fWriter.write32(vertexCount);
939 fWriter.write(vertices, vertexCount * sizeof(SkPoint));
940 if (texs) {
941 fWriter.write(texs, vertexCount * sizeof(SkPoint));
942 }
943 if (colors) {
944 fWriter.write(colors, vertexCount * sizeof(SkColor));
945 }
reed@google.combb6992a2011-04-26 17:41:56 +0000946
reed@google.comacd471f2011-05-03 21:26:46 +0000947 // TODO: flatten xfermode
reed@google.combb6992a2011-04-26 17:41:56 +0000948
reed@google.comacd471f2011-05-03 21:26:46 +0000949 if (indices && indexCount > 0) {
950 fWriter.write32(indexCount);
951 fWriter.writePad(indices, indexCount * sizeof(uint16_t));
952 }
reed@google.combb6992a2011-04-26 17:41:56 +0000953 }
954}
955
reed@google.comacd471f2011-05-03 21:26:46 +0000956void SkGPipeCanvas::drawData(const void* ptr, size_t size) {
957 if (size && ptr) {
958 NOTIFY_SETUP(this);
reed@google.combb6992a2011-04-26 17:41:56 +0000959 unsigned data = 0;
960 if (size < (1 << DRAWOPS_DATA_BITS)) {
961 data = (unsigned)size;
962 }
reed@google.comacd471f2011-05-03 21:26:46 +0000963 if (this->needOpBytes(4 + SkAlign4(size))) {
964 this->writeOp(kDrawData_DrawOp, 0, data);
965 if (0 == data) {
966 fWriter.write32(size);
967 }
reed@google.combb6793b2011-05-05 15:18:15 +0000968 fWriter.writePad(ptr, size);
reed@google.combb6992a2011-04-26 17:41:56 +0000969 }
970 }
971}
972
junov@chromium.org77eec242012-07-18 17:54:45 +0000973void SkGPipeCanvas::flushRecording(bool detachCurrentBlock) {
974 doNotify();
975 if (detachCurrentBlock) {
976 // force a new block to be requested for the next recorded command
rmistry@google.comd6176b02012-08-23 18:14:13 +0000977 fBlockSize = 0;
junov@chromium.org77eec242012-07-18 17:54:45 +0000978 }
979}
980
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000981size_t SkGPipeCanvas::freeMemoryIfPossible(size_t bytesToFree) {
scroggo@google.comd5d158b2012-08-14 20:38:28 +0000982 return (NULL == fBitmapHeap) ? 0 : fBitmapHeap->freeMemoryIfPossible(bytesToFree);
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000983}
984
reed@google.combb6992a2011-04-26 17:41:56 +0000985///////////////////////////////////////////////////////////////////////////////
986
987template <typename T> uint32_t castToU32(T value) {
988 union {
989 T fSrc;
990 uint32_t fDst;
991 } data;
992 data.fSrc = value;
993 return data.fDst;
994}
995
reed@google.com31891582011-05-12 03:03:56 +0000996void SkGPipeCanvas::writePaint(const SkPaint& paint) {
scroggo@google.comd5d158b2012-08-14 20:38:28 +0000997 if (fDone) {
998 return;
999 }
reed@google.com31891582011-05-12 03:03:56 +00001000 SkPaint& base = fPaint;
reed@google.combb6992a2011-04-26 17:41:56 +00001001 uint32_t storage[32];
1002 uint32_t* ptr = storage;
reed@google.combb6992a2011-04-26 17:41:56 +00001003
1004 if (base.getFlags() != paint.getFlags()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001005 *ptr++ = PaintOp_packOpData(kFlags_PaintOp, paint.getFlags());
reed@google.comf5842f72011-05-04 18:30:04 +00001006 base.setFlags(paint.getFlags());
reed@google.combb6992a2011-04-26 17:41:56 +00001007 }
1008 if (base.getColor() != paint.getColor()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001009 *ptr++ = PaintOp_packOp(kColor_PaintOp);
1010 *ptr++ = paint.getColor();
reed@google.comf5842f72011-05-04 18:30:04 +00001011 base.setColor(paint.getColor());
reed@google.combb6992a2011-04-26 17:41:56 +00001012 }
1013 if (base.getStyle() != paint.getStyle()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001014 *ptr++ = PaintOp_packOpData(kStyle_PaintOp, paint.getStyle());
reed@google.comf5842f72011-05-04 18:30:04 +00001015 base.setStyle(paint.getStyle());
reed@google.combb6992a2011-04-26 17:41:56 +00001016 }
1017 if (base.getStrokeJoin() != paint.getStrokeJoin()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001018 *ptr++ = PaintOp_packOpData(kJoin_PaintOp, paint.getStrokeJoin());
reed@google.comf5842f72011-05-04 18:30:04 +00001019 base.setStrokeJoin(paint.getStrokeJoin());
reed@google.combb6992a2011-04-26 17:41:56 +00001020 }
1021 if (base.getStrokeCap() != paint.getStrokeCap()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001022 *ptr++ = PaintOp_packOpData(kCap_PaintOp, paint.getStrokeCap());
reed@google.comf5842f72011-05-04 18:30:04 +00001023 base.setStrokeCap(paint.getStrokeCap());
reed@google.combb6992a2011-04-26 17:41:56 +00001024 }
1025 if (base.getStrokeWidth() != paint.getStrokeWidth()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001026 *ptr++ = PaintOp_packOp(kWidth_PaintOp);
1027 *ptr++ = castToU32(paint.getStrokeWidth());
reed@google.comf5842f72011-05-04 18:30:04 +00001028 base.setStrokeWidth(paint.getStrokeWidth());
reed@google.combb6992a2011-04-26 17:41:56 +00001029 }
1030 if (base.getStrokeMiter() != paint.getStrokeMiter()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001031 *ptr++ = PaintOp_packOp(kMiter_PaintOp);
1032 *ptr++ = castToU32(paint.getStrokeMiter());
reed@google.comf5842f72011-05-04 18:30:04 +00001033 base.setStrokeMiter(paint.getStrokeMiter());
reed@google.combb6992a2011-04-26 17:41:56 +00001034 }
1035 if (base.getTextEncoding() != paint.getTextEncoding()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001036 *ptr++ = PaintOp_packOpData(kEncoding_PaintOp, paint.getTextEncoding());
reed@google.comf5842f72011-05-04 18:30:04 +00001037 base.setTextEncoding(paint.getTextEncoding());
reed@google.combb6992a2011-04-26 17:41:56 +00001038 }
1039 if (base.getHinting() != paint.getHinting()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001040 *ptr++ = PaintOp_packOpData(kHinting_PaintOp, paint.getHinting());
reed@google.comf5842f72011-05-04 18:30:04 +00001041 base.setHinting(paint.getHinting());
reed@google.combb6992a2011-04-26 17:41:56 +00001042 }
1043 if (base.getTextAlign() != paint.getTextAlign()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001044 *ptr++ = PaintOp_packOpData(kAlign_PaintOp, paint.getTextAlign());
reed@google.comf5842f72011-05-04 18:30:04 +00001045 base.setTextAlign(paint.getTextAlign());
reed@google.combb6992a2011-04-26 17:41:56 +00001046 }
1047 if (base.getTextSize() != paint.getTextSize()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001048 *ptr++ = PaintOp_packOp(kTextSize_PaintOp);
1049 *ptr++ = castToU32(paint.getTextSize());
reed@google.comf5842f72011-05-04 18:30:04 +00001050 base.setTextSize(paint.getTextSize());
reed@google.combb6992a2011-04-26 17:41:56 +00001051 }
1052 if (base.getTextScaleX() != paint.getTextScaleX()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001053 *ptr++ = PaintOp_packOp(kTextScaleX_PaintOp);
1054 *ptr++ = castToU32(paint.getTextScaleX());
reed@google.comf5842f72011-05-04 18:30:04 +00001055 base.setTextScaleX(paint.getTextScaleX());
reed@google.combb6992a2011-04-26 17:41:56 +00001056 }
1057 if (base.getTextSkewX() != paint.getTextSkewX()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001058 *ptr++ = PaintOp_packOp(kTextSkewX_PaintOp);
1059 *ptr++ = castToU32(paint.getTextSkewX());
reed@google.comf5842f72011-05-04 18:30:04 +00001060 base.setTextSkewX(paint.getTextSkewX());
1061 }
1062
1063 if (!SkTypeface::Equal(base.getTypeface(), paint.getTypeface())) {
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +00001064 if (isCrossProcess(fFlags)) {
scroggo@google.com3cb969f2012-07-27 20:39:19 +00001065 uint32_t id = this->getTypefaceID(paint.getTypeface());
1066 *ptr++ = PaintOp_packOpData(kTypeface_PaintOp, id);
1067 } else if (this->needOpBytes(sizeof(void*))) {
1068 // Add to the set for ref counting.
1069 fTypefaceSet.add(paint.getTypeface());
1070 // It is safe to write the typeface to the stream before the rest
1071 // of the paint unless we ever send a kReset_PaintOp, which we
1072 // currently never do.
1073 this->writeOp(kSetTypeface_DrawOp);
1074 fWriter.writePtr(paint.getTypeface());
1075 }
reed@google.comf5842f72011-05-04 18:30:04 +00001076 base.setTypeface(paint.getTypeface());
reed@google.combb6992a2011-04-26 17:41:56 +00001077 }
reed@google.combb6992a2011-04-26 17:41:56 +00001078
scroggo@google.com4dffc592012-07-17 16:49:40 +00001079 // This is a new paint, so all old flats can be safely purged, if necessary.
1080 fFlattenableHeap.markAllFlatsSafeToDelete();
reed@google.comb55d1182011-05-11 00:42:04 +00001081 for (int i = 0; i < kCount_PaintFlats; i++) {
1082 int index = this->flattenToIndex(get_paintflat(paint, i), (PaintFlats)i);
scroggo@google.comd3ba5cc2012-07-09 16:05:53 +00001083 bool replaced = index < 0;
1084 if (replaced) {
1085 index = ~index;
1086 }
scroggo@google.com4dffc592012-07-17 16:49:40 +00001087 // Store the index of any flat that needs to be kept. 0 means no flat.
1088 if (index > 0) {
1089 fFlattenableHeap.markFlatForKeeping(index);
1090 }
1091 SkASSERT(index >= 0 && index <= fFlatDictionary.count());
scroggo@google.comd3ba5cc2012-07-09 16:05:53 +00001092 if (index != fCurrFlatIndex[i] || replaced) {
reed@google.comb55d1182011-05-11 00:42:04 +00001093 *ptr++ = PaintOp_packOpFlagData(kFlatIndex_PaintOp, i, index);
1094 fCurrFlatIndex[i] = index;
1095 }
1096 }
1097
reed@google.comacd471f2011-05-03 21:26:46 +00001098 size_t size = (char*)ptr - (char*)storage;
1099 if (size && this->needOpBytes(size)) {
reed@google.comb55d1182011-05-11 00:42:04 +00001100 this->writeOp(kPaintOp_DrawOp, 0, size);
reed@google.comb55d1182011-05-11 00:42:04 +00001101 fWriter.write(storage, size);
reed@google.combb6992a2011-04-26 17:41:56 +00001102 for (size_t i = 0; i < size/4; i++) {
reed@google.comb55d1182011-05-11 00:42:04 +00001103// SkDebugf("[%d] %08X\n", i, storage[i]);
reed@google.combb6992a2011-04-26 17:41:56 +00001104 }
1105 }
reed@google.combb6992a2011-04-26 17:41:56 +00001106}
1107
1108///////////////////////////////////////////////////////////////////////////////
1109
1110#include "SkGPipe.h"
1111
scroggo@google.com3cb969f2012-07-27 20:39:19 +00001112SkGPipeController::~SkGPipeController() {
1113 SkSafeUnref(fCanvas);
1114}
1115
1116void SkGPipeController::setCanvas(SkGPipeCanvas* canvas) {
1117 SkRefCnt_SafeAssign(fCanvas, canvas);
1118}
1119
1120///////////////////////////////////////////////////////////////////////////////
1121
1122SkGPipeWriter::SkGPipeWriter()
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +00001123: fWriter(0) {
reed@google.combb6992a2011-04-26 17:41:56 +00001124 fCanvas = NULL;
1125}
1126
1127SkGPipeWriter::~SkGPipeWriter() {
reed@google.comacd471f2011-05-03 21:26:46 +00001128 this->endRecording();
reed@google.combb6992a2011-04-26 17:41:56 +00001129}
1130
junov@chromium.orga8db8fe2012-08-15 19:49:22 +00001131SkCanvas* SkGPipeWriter::startRecording(SkGPipeController* controller, uint32_t flags,
1132 uint32_t width, uint32_t height) {
reed@google.combb6992a2011-04-26 17:41:56 +00001133 if (NULL == fCanvas) {
reed@google.comacd471f2011-05-03 21:26:46 +00001134 fWriter.reset(NULL, 0);
junov@chromium.orga8db8fe2012-08-15 19:49:22 +00001135 fCanvas = SkNEW_ARGS(SkGPipeCanvas, (controller, &fWriter, flags, width, height));
reed@google.combb6992a2011-04-26 17:41:56 +00001136 }
scroggo@google.com3cb969f2012-07-27 20:39:19 +00001137 controller->setCanvas(fCanvas);
reed@google.combb6992a2011-04-26 17:41:56 +00001138 return fCanvas;
1139}
1140
1141void SkGPipeWriter::endRecording() {
1142 if (fCanvas) {
1143 fCanvas->finish();
1144 fCanvas->unref();
1145 fCanvas = NULL;
1146 }
1147}
1148
junov@chromium.org2e14ba82012-08-07 14:26:57 +00001149void SkGPipeWriter::flushRecording(bool detachCurrentBlock) {
1150 if (fCanvas) {
1151 fCanvas->flushRecording(detachCurrentBlock);
1152 }
junov@chromium.org77eec242012-07-18 17:54:45 +00001153}
1154
junov@chromium.org2e14ba82012-08-07 14:26:57 +00001155size_t SkGPipeWriter::freeMemoryIfPossible(size_t bytesToFree) {
1156 if (fCanvas) {
1157 return fCanvas->freeMemoryIfPossible(bytesToFree);
1158 }
1159 return 0;
1160}
1161
1162size_t SkGPipeWriter::storageAllocatedForRecording() const {
scroggo@google.com15011ee2012-07-26 20:03:32 +00001163 return NULL == fCanvas ? 0 : fCanvas->storageAllocatedForRecording();
1164}
1165
scroggo@google.com3e26bd02012-08-14 15:20:01 +00001166///////////////////////////////////////////////////////////////////////////////
1167
1168BitmapShuttle::BitmapShuttle(SkGPipeCanvas* canvas) {
1169 SkASSERT(canvas != NULL);
1170 fCanvas = canvas;
1171 fCanvas->ref();
1172}
1173
1174BitmapShuttle::~BitmapShuttle() {
1175 fCanvas->unref();
1176}
1177
1178bool BitmapShuttle::insert(const SkBitmap& bitmap, int32_t slot) {
1179 return fCanvas->shuttleBitmap(bitmap, slot);
1180}