blob: 57d4a0fdc345ee57263420a44fb08ae19e7d6783 [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"
robertphillips@google.com1f2f3382013-08-29 11:54:56 +000010#include "SkBitmapDevice.h"
scroggo@google.com10dccde2012-08-08 20:43:22 +000011#include "SkBitmapHeap.h"
reed@google.combb6992a2011-04-26 17:41:56 +000012#include "SkCanvas.h"
scroggo@google.com10dccde2012-08-08 20:43:22 +000013#include "SkColorFilter.h"
reed@google.com8a85d0c2011-06-24 19:12:12 +000014#include "SkData.h"
scroggo@google.com10dccde2012-08-08 20:43:22 +000015#include "SkDrawLooper.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();
51 }
tomhudson@google.com0c00f212011-12-28 14:59:50 +000052 SkDEBUGFAIL("never gets here");
reed@google.comb55d1182011-05-11 00:42:04 +000053 return NULL;
54}
reed@google.combb6992a2011-04-26 17:41:56 +000055
reed@google.comf5842f72011-05-04 18:30:04 +000056static size_t writeTypeface(SkWriter32* writer, SkTypeface* typeface) {
57 SkASSERT(typeface);
58 SkDynamicMemoryWStream stream;
59 typeface->serialize(&stream);
60 size_t size = stream.getOffset();
61 if (writer) {
62 writer->write32(size);
reed@google.com8a85d0c2011-06-24 19:12:12 +000063 SkAutoDataUnref data(stream.copyToData());
robertphillips@google.com59f46b82012-07-10 17:30:58 +000064 writer->writePad(data->data(), size);
reed@google.comf5842f72011-05-04 18:30:04 +000065 }
scroggo@google.com5af9b202012-06-04 17:17:36 +000066 return 4 + SkAlign4(size);
reed@google.comf5842f72011-05-04 18:30:04 +000067}
68
reed@google.combb6992a2011-04-26 17:41:56 +000069///////////////////////////////////////////////////////////////////////////////
70
scroggo@google.com4dffc592012-07-17 16:49:40 +000071class FlattenableHeap : public SkFlatController {
72public:
scroggo@google.com664fab12012-08-14 19:22:05 +000073 FlattenableHeap(int numFlatsToKeep, SkNamedFactorySet* fset, bool isCrossProcess)
scroggo@google.com15543602012-08-02 18:49:49 +000074 : fNumFlatsToKeep(numFlatsToKeep) {
scroggo@google.com664fab12012-08-14 19:22:05 +000075 SkASSERT((isCrossProcess && fset != NULL) || (!isCrossProcess && NULL == fset));
76 if (isCrossProcess) {
77 this->setNamedFactorySet(fset);
78 this->setWriteBufferFlags(SkFlattenableWriteBuffer::kCrossProcess_Flag);
79 }
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +000080 }
scroggo@google.com4dffc592012-07-17 16:49:40 +000081
82 ~FlattenableHeap() {
83 fPointers.freeAll();
84 }
85
86 virtual void* allocThrow(size_t bytes) SK_OVERRIDE;
87
88 virtual void unalloc(void* ptr) SK_OVERRIDE;
89
scroggo@google.com7ca24432012-08-14 15:48:43 +000090 void setBitmapStorage(SkBitmapHeap* heap) {
91 this->setBitmapHeap(heap);
92 }
rmistry@google.comd6176b02012-08-23 18:14:13 +000093
scroggo@google.com4dffc592012-07-17 16:49:40 +000094 const SkFlatData* flatToReplace() const;
95
96 // Mark an SkFlatData as one that should not be returned by flatToReplace.
97 // Takes the result of SkFlatData::index() as its parameter.
98 void markFlatForKeeping(int index) {
99 *fFlatsThatMustBeKept.append() = index;
100 }
101
102 void markAllFlatsSafeToDelete() {
103 fFlatsThatMustBeKept.reset();
104 }
105
106private:
107 // Keep track of the indices (i.e. the result of SkFlatData::index()) of
108 // flats that must be kept, since they are on the current paint.
109 SkTDArray<int> fFlatsThatMustBeKept;
110 SkTDArray<void*> fPointers;
111 const int fNumFlatsToKeep;
112};
113
114void FlattenableHeap::unalloc(void* ptr) {
115 int indexToRemove = fPointers.rfind(ptr);
116 if (indexToRemove >= 0) {
117 sk_free(ptr);
118 fPointers.remove(indexToRemove);
119 }
120}
121
122void* FlattenableHeap::allocThrow(size_t bytes) {
123 void* ptr = sk_malloc_throw(bytes);
124 *fPointers.append() = ptr;
125 return ptr;
126}
127
128const SkFlatData* FlattenableHeap::flatToReplace() const {
129 // First, determine whether we should replace one.
130 if (fPointers.count() > fNumFlatsToKeep) {
131 // Look through the flattenable heap.
132 // TODO: Return the LRU flat.
133 for (int i = 0; i < fPointers.count(); i++) {
134 SkFlatData* potential = (SkFlatData*)fPointers[i];
135 // Make sure that it is not one that must be kept.
136 bool mustKeep = false;
137 for (int j = 0; j < fFlatsThatMustBeKept.count(); j++) {
138 if (potential->index() == fFlatsThatMustBeKept[j]) {
139 mustKeep = true;
140 break;
141 }
142 }
143 if (!mustKeep) {
144 return potential;
145 }
146 }
147 }
148 return NULL;
149}
150
151///////////////////////////////////////////////////////////////////////////////
152
153class FlatDictionary : public SkFlatDictionary<SkFlattenable> {
154public:
scroggo@google.com15543602012-08-02 18:49:49 +0000155 FlatDictionary(FlattenableHeap* heap)
156 : SkFlatDictionary<SkFlattenable>(heap) {
scroggo@google.com4dffc592012-07-17 16:49:40 +0000157 fFlattenProc = &flattenFlattenableProc;
158 // No need to define fUnflattenProc since the writer will never
159 // unflatten the data.
160 }
161 static void flattenFlattenableProc(SkOrderedWriteBuffer& buffer,
162 const void* obj) {
163 buffer.writeFlattenable((SkFlattenable*)obj);
164 }
165
166};
167
168///////////////////////////////////////////////////////////////////////////////
169
reed@google.combb6992a2011-04-26 17:41:56 +0000170class SkGPipeCanvas : public SkCanvas {
171public:
junov@chromium.orga8db8fe2012-08-15 19:49:22 +0000172 SkGPipeCanvas(SkGPipeController*, SkWriter32*, uint32_t flags,
173 uint32_t width, uint32_t height);
reed@google.combb6992a2011-04-26 17:41:56 +0000174 virtual ~SkGPipeCanvas();
175
176 void finish() {
177 if (!fDone) {
reed@google.comdbccc882011-07-08 18:53:39 +0000178 if (this->needOpBytes()) {
179 this->writeOp(kDone_DrawOp);
180 this->doNotify();
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000181 if (shouldFlattenBitmaps(fFlags)) {
scroggo@google.comd9d29672012-08-14 17:21:34 +0000182 // In this case, a BitmapShuttle is reffed by the SkBitmapHeap
183 // and refs this canvas. Unref the SkBitmapHeap to end the
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000184 // circular reference. When shouldFlattenBitmaps is false,
scroggo@google.comd9d29672012-08-14 17:21:34 +0000185 // there is no circular reference, so the SkBitmapHeap can be
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000186 // safely unreffed in the destructor.
scroggo@google.comd9d29672012-08-14 17:21:34 +0000187 fBitmapHeap->unref();
scroggo@google.com7ca24432012-08-14 15:48:43 +0000188 // This eliminates a similar circular reference (Canvas owns
scroggo@google.comd9d29672012-08-14 17:21:34 +0000189 // the FlattenableHeap which holds a ref to the SkBitmapHeap).
scroggo@google.com7ca24432012-08-14 15:48:43 +0000190 fFlattenableHeap.setBitmapStorage(NULL);
scroggo@google.comd9d29672012-08-14 17:21:34 +0000191 fBitmapHeap = NULL;
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000192 }
reed@google.comdbccc882011-07-08 18:53:39 +0000193 }
reed@google.combb6992a2011-04-26 17:41:56 +0000194 fDone = true;
195 }
196 }
197
junov@chromium.org77eec242012-07-18 17:54:45 +0000198 void flushRecording(bool detachCurrentBlock);
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000199 size_t freeMemoryIfPossible(size_t bytesToFree);
junov@chromium.org77eec242012-07-18 17:54:45 +0000200
scroggo@google.com15011ee2012-07-26 20:03:32 +0000201 size_t storageAllocatedForRecording() {
scroggo@google.comd5d158b2012-08-14 20:38:28 +0000202 return (NULL == fBitmapHeap) ? 0 : fBitmapHeap->bytesAllocated();
scroggo@google.com15011ee2012-07-26 20:03:32 +0000203 }
204
reed@google.combb6992a2011-04-26 17:41:56 +0000205 // overrides from SkCanvas
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000206 virtual int save(SaveFlags) SK_OVERRIDE;
207 virtual int saveLayer(const SkRect* bounds, const SkPaint*,
208 SaveFlags) SK_OVERRIDE;
209 virtual void restore() SK_OVERRIDE;
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000210 virtual bool isDrawingToLayer() const SK_OVERRIDE;
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000211 virtual bool translate(SkScalar dx, SkScalar dy) SK_OVERRIDE;
212 virtual bool scale(SkScalar sx, SkScalar sy) SK_OVERRIDE;
213 virtual bool rotate(SkScalar degrees) SK_OVERRIDE;
214 virtual bool skew(SkScalar sx, SkScalar sy) SK_OVERRIDE;
215 virtual bool concat(const SkMatrix& matrix) SK_OVERRIDE;
216 virtual void setMatrix(const SkMatrix& matrix) SK_OVERRIDE;
reed@google.com4ed0fb72012-12-12 20:48:18 +0000217 virtual bool clipRect(const SkRect&, SkRegion::Op op, bool doAntiAlias = false) SK_OVERRIDE;
218 virtual bool clipRRect(const SkRRect&, SkRegion::Op op, bool doAntiAlias = false) SK_OVERRIDE;
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000219 virtual bool clipPath(const SkPath& path, SkRegion::Op op,
220 bool doAntiAlias = false) SK_OVERRIDE;
221 virtual bool clipRegion(const SkRegion& region, SkRegion::Op op) SK_OVERRIDE;
222 virtual void clear(SkColor) SK_OVERRIDE;
223 virtual void drawPaint(const SkPaint& paint) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000224 virtual void drawPoints(PointMode, size_t count, const SkPoint pts[],
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000225 const SkPaint&) SK_OVERRIDE;
reed@google.com4ed0fb72012-12-12 20:48:18 +0000226 virtual void drawOval(const SkRect&, const SkPaint&) SK_OVERRIDE;
bsalomon@google.com7ce564c2013-10-22 16:54:15 +0000227 virtual void drawRect(const SkRect& rect, const SkPaint&) SK_OVERRIDE;
reed@google.com4ed0fb72012-12-12 20:48:18 +0000228 virtual void drawRRect(const SkRRect&, const SkPaint&) SK_OVERRIDE;
bsalomon@google.com7ce564c2013-10-22 16:54:15 +0000229 virtual void drawPath(const SkPath& path, const SkPaint&) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000230 virtual void drawBitmap(const SkBitmap&, SkScalar left, SkScalar top,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000231 const SkPaint*) SK_OVERRIDE;
reed@google.com71121732012-09-18 15:14:33 +0000232 virtual void drawBitmapRectToRect(const SkBitmap&, const SkRect* src,
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000233 const SkRect& dst, const SkPaint* paint,
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +0000234 DrawBitmapRectFlags flags) 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;
robertphillips@google.com0a4805e2013-05-29 13:24:23 +0000258 virtual void beginCommentGroup(const char* description) SK_OVERRIDE;
259 virtual void addComment(const char* kywd, const char* value) SK_OVERRIDE;
260 virtual void endCommentGroup() SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000261
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000262 /**
263 * Flatten an SkBitmap to send to the reader, where it will be referenced
264 * according to slot.
265 */
266 bool shuttleBitmap(const SkBitmap&, int32_t slot);
reed@google.combb6992a2011-04-26 17:41:56 +0000267private:
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000268 enum {
269 kNoSaveLayer = -1,
270 };
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000271 SkNamedFactorySet* fFactorySet;
272 int fFirstSaveLayerStackLevel;
scroggo@google.comd9d29672012-08-14 17:21:34 +0000273 SkBitmapHeap* fBitmapHeap;
reed@google.comacd471f2011-05-03 21:26:46 +0000274 SkGPipeController* fController;
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000275 SkWriter32& fWriter;
276 size_t fBlockSize; // amount allocated for writer
277 size_t fBytesNotified;
278 bool fDone;
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000279 const uint32_t fFlags;
reed@google.combb6992a2011-04-26 17:41:56 +0000280
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000281 SkRefCntSet fTypefaceSet;
reed@google.comf5842f72011-05-04 18:30:04 +0000282
283 uint32_t getTypefaceID(SkTypeface*);
284
reed@google.comacd471f2011-05-03 21:26:46 +0000285 inline void writeOp(DrawOps op, unsigned flags, unsigned data) {
reed@google.combb6992a2011-04-26 17:41:56 +0000286 fWriter.write32(DrawOp_packOpFlagData(op, flags, data));
287 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000288
reed@google.comacd471f2011-05-03 21:26:46 +0000289 inline void writeOp(DrawOps op) {
reed@google.combb6992a2011-04-26 17:41:56 +0000290 fWriter.write32(DrawOp_packOpFlagData(op, 0, 0));
291 }
reed@google.comacd471f2011-05-03 21:26:46 +0000292
293 bool needOpBytes(size_t size = 0);
294
295 inline void doNotify() {
296 if (!fDone) {
297 size_t bytes = fWriter.size() - fBytesNotified;
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000298 if (bytes > 0) {
299 fController->notifyWritten(bytes);
300 fBytesNotified += bytes;
301 }
reed@google.comacd471f2011-05-03 21:26:46 +0000302 }
303 }
reed@google.comb55d1182011-05-11 00:42:04 +0000304
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000305 // Should be called after any calls to an SkFlatDictionary::findAndReplace
306 // if a new SkFlatData was added when in cross process mode
307 void flattenFactoryNames();
308
scroggo@google.com4dffc592012-07-17 16:49:40 +0000309 FlattenableHeap fFlattenableHeap;
310 FlatDictionary fFlatDictionary;
reed@google.comb55d1182011-05-11 00:42:04 +0000311 int fCurrFlatIndex[kCount_PaintFlats];
312 int flattenToIndex(SkFlattenable* obj, PaintFlats);
313
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000314 // Common code used by drawBitmap*. Behaves differently depending on the
315 // type of SkBitmapHeap being used, which is determined by the flags used.
316 bool commonDrawBitmap(const SkBitmap& bm, DrawOps op, unsigned flags,
317 size_t opBytesNeeded, const SkPaint* paint);
scroggo@google.com58be6822012-07-30 14:40:01 +0000318
reed@google.com31891582011-05-12 03:03:56 +0000319 SkPaint fPaint;
320 void writePaint(const SkPaint&);
reed@google.combb6992a2011-04-26 17:41:56 +0000321
reed@google.comacd471f2011-05-03 21:26:46 +0000322 class AutoPipeNotify {
323 public:
324 AutoPipeNotify(SkGPipeCanvas* canvas) : fCanvas(canvas) {}
325 ~AutoPipeNotify() { fCanvas->doNotify(); }
326 private:
327 SkGPipeCanvas* fCanvas;
328 };
329 friend class AutoPipeNotify;
330
reed@google.combb6992a2011-04-26 17:41:56 +0000331 typedef SkCanvas INHERITED;
332};
333
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000334void SkGPipeCanvas::flattenFactoryNames() {
335 const char* name;
336 while ((name = fFactorySet->getNextAddedFactoryName()) != NULL) {
337 size_t len = strlen(name);
338 if (this->needOpBytes(len)) {
339 this->writeOp(kDef_Factory_DrawOp);
340 fWriter.writeString(name, len);
341 }
342 }
343}
344
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000345bool SkGPipeCanvas::shuttleBitmap(const SkBitmap& bm, int32_t slot) {
scroggo@google.com565254b2012-06-28 15:41:32 +0000346 SkASSERT(shouldFlattenBitmaps(fFlags));
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000347 SkOrderedWriteBuffer buffer(1024);
348 buffer.setNamedFactoryRecorder(fFactorySet);
scroggo@google.com74b7ffd2013-04-30 02:32:41 +0000349 buffer.writeBitmap(bm);
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000350 this->flattenFactoryNames();
351 uint32_t size = buffer.size();
352 if (this->needOpBytes(size)) {
353 this->writeOp(kDef_Bitmap_DrawOp, 0, slot);
354 void* dst = static_cast<void*>(fWriter.reserve(size));
355 buffer.writeToMemory(dst);
356 return true;
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000357 }
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000358 return false;
scroggo@google.comd3ba5cc2012-07-09 16:05:53 +0000359}
360
reed@google.comb55d1182011-05-11 00:42:04 +0000361// return 0 for NULL (or unflattenable obj), or index-base-1
scroggo@google.comd3ba5cc2012-07-09 16:05:53 +0000362// return ~(index-base-1) if an old flattenable was replaced
reed@google.comb55d1182011-05-11 00:42:04 +0000363int SkGPipeCanvas::flattenToIndex(SkFlattenable* obj, PaintFlats paintflat) {
scroggo@google.comd5d158b2012-08-14 20:38:28 +0000364 SkASSERT(!fDone && fBitmapHeap != NULL);
reed@google.comb55d1182011-05-11 00:42:04 +0000365 if (NULL == obj) {
366 return 0;
367 }
reed@google.comb55d1182011-05-11 00:42:04 +0000368
scroggo@google.comd9d29672012-08-14 17:21:34 +0000369 fBitmapHeap->deferAddingOwners();
scroggo@google.com4dffc592012-07-17 16:49:40 +0000370 bool added, replaced;
scroggo@google.com664fab12012-08-14 19:22:05 +0000371 const SkFlatData* flat = fFlatDictionary.findAndReplace(*obj, fFlattenableHeap.flatToReplace(),
372 &added, &replaced);
scroggo@google.comd9d29672012-08-14 17:21:34 +0000373 fBitmapHeap->endAddingOwnersDeferral(added);
scroggo@google.com4dffc592012-07-17 16:49:40 +0000374 int index = flat->index();
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000375 if (added) {
376 if (isCrossProcess(fFlags)) {
377 this->flattenFactoryNames();
378 }
379 size_t flatSize = flat->flatSize();
380 if (this->needOpBytes(flatSize)) {
381 this->writeOp(kDef_Flattenable_DrawOp, paintflat, index);
382 fWriter.write(flat->data(), flatSize);
383 }
scroggo@google.com4dffc592012-07-17 16:49:40 +0000384 }
385 if (replaced) {
reed@google.comb55d1182011-05-11 00:42:04 +0000386 index = ~index;
reed@google.comb55d1182011-05-11 00:42:04 +0000387 }
scroggo@google.com4dffc592012-07-17 16:49:40 +0000388 return index;
reed@google.comb55d1182011-05-11 00:42:04 +0000389}
390
reed@google.combb6992a2011-04-26 17:41:56 +0000391///////////////////////////////////////////////////////////////////////////////
392
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000393/**
394 * If SkBitmaps are to be flattened to send to the reader, this class is
395 * provided to the SkBitmapHeap to tell the SkGPipeCanvas to do so.
396 */
397class BitmapShuttle : public SkBitmapHeap::ExternalStorage {
398public:
399 BitmapShuttle(SkGPipeCanvas*);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000400
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000401 ~BitmapShuttle();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000402
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000403 virtual bool insert(const SkBitmap& bitmap, int32_t slot) SK_OVERRIDE;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000404
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000405private:
406 SkGPipeCanvas* fCanvas;
407};
408
409///////////////////////////////////////////////////////////////////////////////
410
reed@google.comacd471f2011-05-03 21:26:46 +0000411#define MIN_BLOCK_SIZE (16 * 1024)
scroggo@google.com4dffc592012-07-17 16:49:40 +0000412#define BITMAPS_TO_KEEP 5
413#define FLATTENABLES_TO_KEEP 10
reed@google.combb6992a2011-04-26 17:41:56 +0000414
reed@google.comacd471f2011-05-03 21:26:46 +0000415SkGPipeCanvas::SkGPipeCanvas(SkGPipeController* controller,
rmistry@google.comd6176b02012-08-23 18:14:13 +0000416 SkWriter32* writer, uint32_t flags,
junov@chromium.orga8db8fe2012-08-15 19:49:22 +0000417 uint32_t width, uint32_t height)
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000418: fFactorySet(isCrossProcess(flags) ? SkNEW(SkNamedFactorySet) : NULL)
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000419, fWriter(*writer)
420, fFlags(flags)
scroggo@google.com664fab12012-08-14 19:22:05 +0000421, fFlattenableHeap(FLATTENABLES_TO_KEEP, fFactorySet, isCrossProcess(flags))
scroggo@google.com15543602012-08-02 18:49:49 +0000422, fFlatDictionary(&fFlattenableHeap) {
reed@google.comacd471f2011-05-03 21:26:46 +0000423 fController = controller;
reed@google.combb6992a2011-04-26 17:41:56 +0000424 fDone = false;
reed@google.comacd471f2011-05-03 21:26:46 +0000425 fBlockSize = 0; // need first block from controller
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000426 fBytesNotified = 0;
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000427 fFirstSaveLayerStackLevel = kNoSaveLayer;
reed@google.comb55d1182011-05-11 00:42:04 +0000428 sk_bzero(fCurrFlatIndex, sizeof(fCurrFlatIndex));
reed@google.comacd471f2011-05-03 21:26:46 +0000429
reed@google.combb6793b2011-05-05 15:18:15 +0000430 // we need a device to limit our clip
yangsu@google.com06b4da12011-06-17 15:04:40 +0000431 // We don't allocate pixels for the bitmap
reed@google.combb6793b2011-05-05 15:18:15 +0000432 SkBitmap bitmap;
junov@chromium.orga8db8fe2012-08-15 19:49:22 +0000433 bitmap.setConfig(SkBitmap::kARGB_8888_Config, width, height);
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000434 SkBaseDevice* device = SkNEW_ARGS(SkBitmapDevice, (bitmap));
reed@google.combb6793b2011-05-05 15:18:15 +0000435 this->setDevice(device)->unref();
scroggo@google.com10dccde2012-08-08 20:43:22 +0000436
scroggo@google.com565254b2012-06-28 15:41:32 +0000437 // Tell the reader the appropriate flags to use.
438 if (this->needOpBytes()) {
439 this->writeOp(kReportFlags_DrawOp, fFlags, 0);
440 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000441
scroggo@google.com10dccde2012-08-08 20:43:22 +0000442 if (shouldFlattenBitmaps(flags)) {
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000443 BitmapShuttle* shuttle = SkNEW_ARGS(BitmapShuttle, (this));
scroggo@google.comd9d29672012-08-14 17:21:34 +0000444 fBitmapHeap = SkNEW_ARGS(SkBitmapHeap, (shuttle, BITMAPS_TO_KEEP));
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000445 shuttle->unref();
scroggo@google.com10dccde2012-08-08 20:43:22 +0000446 } else {
scroggo@google.comd9d29672012-08-14 17:21:34 +0000447 fBitmapHeap = SkNEW_ARGS(SkBitmapHeap,
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000448 (BITMAPS_TO_KEEP, controller->numberOfReaders()));
scroggo@google.com10dccde2012-08-08 20:43:22 +0000449 if (this->needOpBytes(sizeof(void*))) {
scroggo@google.comd9d29672012-08-14 17:21:34 +0000450 this->writeOp(kShareBitmapHeap_DrawOp);
451 fWriter.writePtr(static_cast<void*>(fBitmapHeap));
scroggo@google.com10dccde2012-08-08 20:43:22 +0000452 }
453 }
scroggo@google.comd9d29672012-08-14 17:21:34 +0000454 fFlattenableHeap.setBitmapStorage(fBitmapHeap);
scroggo@google.com10dccde2012-08-08 20:43:22 +0000455 this->doNotify();
reed@google.combb6992a2011-04-26 17:41:56 +0000456}
457
458SkGPipeCanvas::~SkGPipeCanvas() {
459 this->finish();
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000460 SkSafeUnref(fFactorySet);
scroggo@google.comd9d29672012-08-14 17:21:34 +0000461 SkSafeUnref(fBitmapHeap);
reed@google.combb6992a2011-04-26 17:41:56 +0000462}
463
reed@google.comacd471f2011-05-03 21:26:46 +0000464bool SkGPipeCanvas::needOpBytes(size_t needed) {
465 if (fDone) {
466 return false;
467 }
468
469 needed += 4; // size of DrawOp atom
470 if (fWriter.size() + needed > fBlockSize) {
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000471 // Before we wipe out any data that has already been written, read it
472 // out.
473 this->doNotify();
474 size_t blockSize = SkMax32(MIN_BLOCK_SIZE, needed);
475 void* block = fController->requestBlock(blockSize, &fBlockSize);
reed@google.comacd471f2011-05-03 21:26:46 +0000476 if (NULL == block) {
477 fDone = true;
478 return false;
479 }
scroggo@google.com460a23e2012-08-16 17:56:49 +0000480 SkASSERT(SkIsAlign4(fBlockSize));
reed@google.comacd471f2011-05-03 21:26:46 +0000481 fWriter.reset(block, fBlockSize);
482 fBytesNotified = 0;
483 }
484 return true;
485}
486
reed@google.comf5842f72011-05-04 18:30:04 +0000487uint32_t SkGPipeCanvas::getTypefaceID(SkTypeface* face) {
488 uint32_t id = 0; // 0 means default/null typeface
489 if (face) {
490 id = fTypefaceSet.find(face);
491 if (0 == id) {
492 id = fTypefaceSet.add(face);
493 size_t size = writeTypeface(NULL, face);
494 if (this->needOpBytes(size)) {
reed@google.combb6793b2011-05-05 15:18:15 +0000495 this->writeOp(kDef_Typeface_DrawOp);
reed@google.comf5842f72011-05-04 18:30:04 +0000496 writeTypeface(&fWriter, face);
497 }
498 }
499 }
500 return id;
501}
502
reed@google.combb6992a2011-04-26 17:41:56 +0000503///////////////////////////////////////////////////////////////////////////////
504
reed@google.comacd471f2011-05-03 21:26:46 +0000505#define NOTIFY_SETUP(canvas) \
506 AutoPipeNotify apn(canvas)
507
reed@google.combb6992a2011-04-26 17:41:56 +0000508int SkGPipeCanvas::save(SaveFlags flags) {
reed@google.comacd471f2011-05-03 21:26:46 +0000509 NOTIFY_SETUP(this);
510 if (this->needOpBytes()) {
511 this->writeOp(kSave_DrawOp, 0, flags);
512 }
reed@google.combb6992a2011-04-26 17:41:56 +0000513 return this->INHERITED::save(flags);
514}
515
516int SkGPipeCanvas::saveLayer(const SkRect* bounds, const SkPaint* paint,
517 SaveFlags saveFlags) {
reed@google.comacd471f2011-05-03 21:26:46 +0000518 NOTIFY_SETUP(this);
519 size_t size = 0;
reed@google.combb6992a2011-04-26 17:41:56 +0000520 unsigned opFlags = 0;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000521
reed@google.combb6992a2011-04-26 17:41:56 +0000522 if (bounds) {
523 opFlags |= kSaveLayer_HasBounds_DrawOpFlag;
reed@google.comacd471f2011-05-03 21:26:46 +0000524 size += sizeof(SkRect);
reed@google.combb6992a2011-04-26 17:41:56 +0000525 }
526 if (paint) {
527 opFlags |= kSaveLayer_HasPaint_DrawOpFlag;
reed@google.com31891582011-05-12 03:03:56 +0000528 this->writePaint(*paint);
reed@google.combb6992a2011-04-26 17:41:56 +0000529 }
530
reed@google.comacd471f2011-05-03 21:26:46 +0000531 if (this->needOpBytes(size)) {
532 this->writeOp(kSaveLayer_DrawOp, opFlags, saveFlags);
533 if (bounds) {
534 fWriter.writeRect(*bounds);
535 }
reed@google.combb6992a2011-04-26 17:41:56 +0000536 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000537
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000538 if (kNoSaveLayer == fFirstSaveLayerStackLevel){
539 fFirstSaveLayerStackLevel = this->getSaveCount();
540 }
reed@google.combb6992a2011-04-26 17:41:56 +0000541 // we just pass on the save, so we don't create a layer
542 return this->INHERITED::save(saveFlags);
543}
544
545void SkGPipeCanvas::restore() {
reed@google.comacd471f2011-05-03 21:26:46 +0000546 NOTIFY_SETUP(this);
547 if (this->needOpBytes()) {
548 this->writeOp(kRestore_DrawOp);
549 }
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000550
reed@google.combb6992a2011-04-26 17:41:56 +0000551 this->INHERITED::restore();
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000552
553 if (this->getSaveCount() == fFirstSaveLayerStackLevel){
554 fFirstSaveLayerStackLevel = kNoSaveLayer;
555 }
556}
557
558bool SkGPipeCanvas::isDrawingToLayer() const {
559 return kNoSaveLayer != fFirstSaveLayerStackLevel;
reed@google.combb6992a2011-04-26 17:41:56 +0000560}
561
562bool SkGPipeCanvas::translate(SkScalar dx, SkScalar dy) {
563 if (dx || dy) {
reed@google.comacd471f2011-05-03 21:26:46 +0000564 NOTIFY_SETUP(this);
565 if (this->needOpBytes(2 * sizeof(SkScalar))) {
566 this->writeOp(kTranslate_DrawOp);
567 fWriter.writeScalar(dx);
568 fWriter.writeScalar(dy);
569 }
reed@google.combb6992a2011-04-26 17:41:56 +0000570 }
571 return this->INHERITED::translate(dx, dy);
572}
573
574bool SkGPipeCanvas::scale(SkScalar sx, SkScalar sy) {
575 if (sx || sy) {
reed@google.comacd471f2011-05-03 21:26:46 +0000576 NOTIFY_SETUP(this);
577 if (this->needOpBytes(2 * sizeof(SkScalar))) {
578 this->writeOp(kScale_DrawOp);
579 fWriter.writeScalar(sx);
580 fWriter.writeScalar(sy);
581 }
reed@google.combb6992a2011-04-26 17:41:56 +0000582 }
583 return this->INHERITED::scale(sx, sy);
584}
585
586bool SkGPipeCanvas::rotate(SkScalar degrees) {
587 if (degrees) {
reed@google.comacd471f2011-05-03 21:26:46 +0000588 NOTIFY_SETUP(this);
589 if (this->needOpBytes(sizeof(SkScalar))) {
590 this->writeOp(kRotate_DrawOp);
591 fWriter.writeScalar(degrees);
592 }
reed@google.combb6992a2011-04-26 17:41:56 +0000593 }
594 return this->INHERITED::rotate(degrees);
595}
596
597bool SkGPipeCanvas::skew(SkScalar sx, SkScalar sy) {
598 if (sx || sy) {
reed@google.comacd471f2011-05-03 21:26:46 +0000599 NOTIFY_SETUP(this);
600 if (this->needOpBytes(2 * sizeof(SkScalar))) {
601 this->writeOp(kSkew_DrawOp);
602 fWriter.writeScalar(sx);
603 fWriter.writeScalar(sy);
604 }
reed@google.combb6992a2011-04-26 17:41:56 +0000605 }
606 return this->INHERITED::skew(sx, sy);
607}
608
609bool SkGPipeCanvas::concat(const SkMatrix& matrix) {
610 if (!matrix.isIdentity()) {
reed@google.comacd471f2011-05-03 21:26:46 +0000611 NOTIFY_SETUP(this);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000612 if (this->needOpBytes(matrix.writeToMemory(NULL))) {
reed@google.comacd471f2011-05-03 21:26:46 +0000613 this->writeOp(kConcat_DrawOp);
djsollen@google.com2b2ede32012-04-12 13:24:04 +0000614 fWriter.writeMatrix(matrix);
reed@google.comacd471f2011-05-03 21:26:46 +0000615 }
reed@google.combb6992a2011-04-26 17:41:56 +0000616 }
617 return this->INHERITED::concat(matrix);
618}
619
620void SkGPipeCanvas::setMatrix(const SkMatrix& matrix) {
reed@google.comacd471f2011-05-03 21:26:46 +0000621 NOTIFY_SETUP(this);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000622 if (this->needOpBytes(matrix.writeToMemory(NULL))) {
reed@google.comacd471f2011-05-03 21:26:46 +0000623 this->writeOp(kSetMatrix_DrawOp);
djsollen@google.com2b2ede32012-04-12 13:24:04 +0000624 fWriter.writeMatrix(matrix);
reed@google.comacd471f2011-05-03 21:26:46 +0000625 }
reed@google.combb6992a2011-04-26 17:41:56 +0000626 this->INHERITED::setMatrix(matrix);
627}
628
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000629bool SkGPipeCanvas::clipRect(const SkRect& rect, SkRegion::Op rgnOp,
630 bool doAntiAlias) {
reed@google.comacd471f2011-05-03 21:26:46 +0000631 NOTIFY_SETUP(this);
scroggo@google.com460a23e2012-08-16 17:56:49 +0000632 if (this->needOpBytes(sizeof(SkRect))) {
633 unsigned flags = doAntiAlias & kClip_HasAntiAlias_DrawOpFlag;
634 this->writeOp(kClipRect_DrawOp, flags, rgnOp);
reed@google.comacd471f2011-05-03 21:26:46 +0000635 fWriter.writeRect(rect);
636 }
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000637 return this->INHERITED::clipRect(rect, rgnOp, doAntiAlias);
reed@google.combb6992a2011-04-26 17:41:56 +0000638}
639
reed@google.com4ed0fb72012-12-12 20:48:18 +0000640bool SkGPipeCanvas::clipRRect(const SkRRect& rrect, SkRegion::Op rgnOp,
641 bool doAntiAlias) {
642 NOTIFY_SETUP(this);
643 if (this->needOpBytes(kSizeOfFlatRRect)) {
644 unsigned flags = doAntiAlias & kClip_HasAntiAlias_DrawOpFlag;
645 this->writeOp(kClipRRect_DrawOp, flags, rgnOp);
646 fWriter.writeRRect(rrect);
647 }
648 return this->INHERITED::clipRRect(rrect, rgnOp, doAntiAlias);
649}
650
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000651bool SkGPipeCanvas::clipPath(const SkPath& path, SkRegion::Op rgnOp,
652 bool doAntiAlias) {
reed@google.comacd471f2011-05-03 21:26:46 +0000653 NOTIFY_SETUP(this);
scroggo@google.com460a23e2012-08-16 17:56:49 +0000654 if (this->needOpBytes(path.writeToMemory(NULL))) {
655 unsigned flags = doAntiAlias & kClip_HasAntiAlias_DrawOpFlag;
656 this->writeOp(kClipPath_DrawOp, flags, rgnOp);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000657 fWriter.writePath(path);
reed@google.comacd471f2011-05-03 21:26:46 +0000658 }
reed@google.combb6992a2011-04-26 17:41:56 +0000659 // we just pass on the bounds of the path
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000660 return this->INHERITED::clipRect(path.getBounds(), rgnOp, doAntiAlias);
reed@google.combb6992a2011-04-26 17:41:56 +0000661}
662
663bool SkGPipeCanvas::clipRegion(const SkRegion& region, SkRegion::Op rgnOp) {
reed@google.comacd471f2011-05-03 21:26:46 +0000664 NOTIFY_SETUP(this);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000665 if (this->needOpBytes(region.writeToMemory(NULL))) {
reed@google.comacd471f2011-05-03 21:26:46 +0000666 this->writeOp(kClipRegion_DrawOp, 0, rgnOp);
djsollen@google.com2b2ede32012-04-12 13:24:04 +0000667 fWriter.writeRegion(region);
reed@google.comacd471f2011-05-03 21:26:46 +0000668 }
reed@google.combb6992a2011-04-26 17:41:56 +0000669 return this->INHERITED::clipRegion(region, rgnOp);
670}
671
672///////////////////////////////////////////////////////////////////////////////
673
674void SkGPipeCanvas::clear(SkColor color) {
reed@google.comacd471f2011-05-03 21:26:46 +0000675 NOTIFY_SETUP(this);
reed@google.combb6992a2011-04-26 17:41:56 +0000676 unsigned flags = 0;
677 if (color) {
678 flags |= kClear_HasColor_DrawOpFlag;
679 }
reed@google.comacd471f2011-05-03 21:26:46 +0000680 if (this->needOpBytes(sizeof(SkColor))) {
681 this->writeOp(kDrawClear_DrawOp, flags, 0);
682 if (color) {
683 fWriter.write32(color);
684 }
reed@google.combb6992a2011-04-26 17:41:56 +0000685 }
686}
687
688void SkGPipeCanvas::drawPaint(const SkPaint& paint) {
reed@google.comacd471f2011-05-03 21:26:46 +0000689 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000690 this->writePaint(paint);
reed@google.comacd471f2011-05-03 21:26:46 +0000691 if (this->needOpBytes()) {
reed@google.com31891582011-05-12 03:03:56 +0000692 this->writeOp(kDrawPaint_DrawOp);
reed@google.comacd471f2011-05-03 21:26:46 +0000693 }
reed@google.combb6992a2011-04-26 17:41:56 +0000694}
695
696void SkGPipeCanvas::drawPoints(PointMode mode, size_t count,
robertphillips@google.com8b169312013-10-15 17:47:36 +0000697 const SkPoint pts[], const SkPaint& paint) {
reed@google.combb6992a2011-04-26 17:41:56 +0000698 if (count) {
reed@google.comacd471f2011-05-03 21:26:46 +0000699 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000700 this->writePaint(paint);
reed@google.comacd471f2011-05-03 21:26:46 +0000701 if (this->needOpBytes(4 + count * sizeof(SkPoint))) {
reed@google.com31891582011-05-12 03:03:56 +0000702 this->writeOp(kDrawPoints_DrawOp, mode, 0);
reed@google.comacd471f2011-05-03 21:26:46 +0000703 fWriter.write32(count);
704 fWriter.write(pts, count * sizeof(SkPoint));
705 }
reed@google.combb6992a2011-04-26 17:41:56 +0000706 }
707}
708
reed@google.com4ed0fb72012-12-12 20:48:18 +0000709void SkGPipeCanvas::drawOval(const SkRect& rect, const SkPaint& paint) {
710 NOTIFY_SETUP(this);
711 this->writePaint(paint);
712 if (this->needOpBytes(sizeof(SkRect))) {
713 this->writeOp(kDrawOval_DrawOp);
714 fWriter.writeRect(rect);
715 }
716}
717
bsalomon@google.com7ce564c2013-10-22 16:54:15 +0000718void SkGPipeCanvas::drawRect(const SkRect& rect, const SkPaint& paint) {
reed@google.comacd471f2011-05-03 21:26:46 +0000719 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000720 this->writePaint(paint);
reed@google.comacd471f2011-05-03 21:26:46 +0000721 if (this->needOpBytes(sizeof(SkRect))) {
reed@google.com31891582011-05-12 03:03:56 +0000722 this->writeOp(kDrawRect_DrawOp);
reed@google.comacd471f2011-05-03 21:26:46 +0000723 fWriter.writeRect(rect);
724 }
reed@google.combb6992a2011-04-26 17:41:56 +0000725}
726
reed@google.com4ed0fb72012-12-12 20:48:18 +0000727void SkGPipeCanvas::drawRRect(const SkRRect& rrect, const SkPaint& paint) {
728 NOTIFY_SETUP(this);
729 this->writePaint(paint);
730 if (this->needOpBytes(kSizeOfFlatRRect)) {
731 this->writeOp(kDrawRRect_DrawOp);
732 fWriter.writeRRect(rrect);
733 }
734}
735
bsalomon@google.com7ce564c2013-10-22 16:54:15 +0000736void SkGPipeCanvas::drawPath(const SkPath& path, const SkPaint& paint) {
reed@google.comacd471f2011-05-03 21:26:46 +0000737 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000738 this->writePaint(paint);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000739 if (this->needOpBytes(path.writeToMemory(NULL))) {
reed@google.com31891582011-05-12 03:03:56 +0000740 this->writeOp(kDrawPath_DrawOp);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000741 fWriter.writePath(path);
reed@google.comacd471f2011-05-03 21:26:46 +0000742 }
reed@google.combb6992a2011-04-26 17:41:56 +0000743}
744
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000745bool SkGPipeCanvas::commonDrawBitmap(const SkBitmap& bm, DrawOps op,
746 unsigned flags,
747 size_t opBytesNeeded,
748 const SkPaint* paint) {
scroggo@google.com58be6822012-07-30 14:40:01 +0000749 if (paint != NULL) {
scroggo@google.com460a23e2012-08-16 17:56:49 +0000750 flags |= kDrawBitmap_HasPaint_DrawOpFlag;
scroggo@google.com58be6822012-07-30 14:40:01 +0000751 this->writePaint(*paint);
752 }
scroggo@google.com10dccde2012-08-08 20:43:22 +0000753 if (this->needOpBytes(opBytesNeeded)) {
scroggo@google.comd5d158b2012-08-14 20:38:28 +0000754 SkASSERT(fBitmapHeap != NULL);
755 int32_t bitmapIndex = fBitmapHeap->insert(bm);
756 if (SkBitmapHeap::INVALID_SLOT == bitmapIndex) {
757 return false;
758 }
scroggo@google.com10dccde2012-08-08 20:43:22 +0000759 this->writeOp(op, flags, bitmapIndex);
scroggo@google.com58be6822012-07-30 14:40:01 +0000760 return true;
761 }
762 return false;
763}
764
765void SkGPipeCanvas::drawBitmap(const SkBitmap& bm, SkScalar left, SkScalar top,
766 const SkPaint* paint) {
767 NOTIFY_SETUP(this);
768 size_t opBytesNeeded = sizeof(SkScalar) * 2;
769
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000770 if (this->commonDrawBitmap(bm, kDrawBitmap_DrawOp, 0, opBytesNeeded, paint)) {
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000771 fWriter.writeScalar(left);
772 fWriter.writeScalar(top);
773 }
reed@google.combb6992a2011-04-26 17:41:56 +0000774}
775
reed@google.com71121732012-09-18 15:14:33 +0000776void SkGPipeCanvas::drawBitmapRectToRect(const SkBitmap& bm, const SkRect* src,
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +0000777 const SkRect& dst, const SkPaint* paint,
778 DrawBitmapRectFlags dbmrFlags) {
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000779 NOTIFY_SETUP(this);
scroggo@google.com58be6822012-07-30 14:40:01 +0000780 size_t opBytesNeeded = sizeof(SkRect);
781 bool hasSrc = src != NULL;
782 unsigned flags;
783 if (hasSrc) {
scroggo@google.com460a23e2012-08-16 17:56:49 +0000784 flags = kDrawBitmap_HasSrcRect_DrawOpFlag;
scroggo@google.com58be6822012-07-30 14:40:01 +0000785 opBytesNeeded += sizeof(int32_t) * 4;
786 } else {
787 flags = 0;
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000788 }
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +0000789 if (dbmrFlags & kBleed_DrawBitmapRectFlag) {
790 flags |= kDrawBitmap_Bleed_DrawOpFlag;
791 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000792
reed@google.com71121732012-09-18 15:14:33 +0000793 if (this->commonDrawBitmap(bm, kDrawBitmapRectToRect_DrawOp, flags, opBytesNeeded, paint)) {
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000794 if (hasSrc) {
reed@google.com71121732012-09-18 15:14:33 +0000795 fWriter.writeRect(*src);
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000796 }
797 fWriter.writeRect(dst);
798 }
reed@google.combb6992a2011-04-26 17:41:56 +0000799}
800
scroggo@google.com58be6822012-07-30 14:40:01 +0000801void SkGPipeCanvas::drawBitmapMatrix(const SkBitmap& bm, const SkMatrix& matrix,
802 const SkPaint* paint) {
803 NOTIFY_SETUP(this);
804 size_t opBytesNeeded = matrix.writeToMemory(NULL);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000805
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000806 if (this->commonDrawBitmap(bm, kDrawBitmapMatrix_DrawOp, 0, opBytesNeeded, paint)) {
scroggo@google.com58be6822012-07-30 14:40:01 +0000807 fWriter.writeMatrix(matrix);
808 }
reed@google.combb6992a2011-04-26 17:41:56 +0000809}
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000810
811void SkGPipeCanvas::drawBitmapNine(const SkBitmap& bm, const SkIRect& center,
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000812 const SkRect& dst, const SkPaint* paint) {
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000813 NOTIFY_SETUP(this);
scroggo@google.com58be6822012-07-30 14:40:01 +0000814 size_t opBytesNeeded = sizeof(int32_t) * 4 + sizeof(SkRect);
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000815
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000816 if (this->commonDrawBitmap(bm, kDrawBitmapNine_DrawOp, 0, opBytesNeeded, paint)) {
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000817 fWriter.write32(center.fLeft);
818 fWriter.write32(center.fTop);
819 fWriter.write32(center.fRight);
820 fWriter.write32(center.fBottom);
821 fWriter.writeRect(dst);
822 }
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000823}
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000824
825void SkGPipeCanvas::drawSprite(const SkBitmap& bm, int left, int top,
826 const SkPaint* paint) {
827 NOTIFY_SETUP(this);
scroggo@google.com58be6822012-07-30 14:40:01 +0000828 size_t opBytesNeeded = sizeof(int32_t) * 2;
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000829
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000830 if (this->commonDrawBitmap(bm, kDrawSprite_DrawOp, 0, opBytesNeeded, paint)) {
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000831 fWriter.write32(left);
832 fWriter.write32(top);
833 }
reed@google.combb6992a2011-04-26 17:41:56 +0000834}
835
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000836void SkGPipeCanvas::drawText(const void* text, size_t byteLength, SkScalar x,
reed@google.combb6992a2011-04-26 17:41:56 +0000837 SkScalar y, const SkPaint& paint) {
838 if (byteLength) {
reed@google.comacd471f2011-05-03 21:26:46 +0000839 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000840 this->writePaint(paint);
reed@google.comacd471f2011-05-03 21:26:46 +0000841 if (this->needOpBytes(4 + SkAlign4(byteLength) + 2 * sizeof(SkScalar))) {
reed@google.com31891582011-05-12 03:03:56 +0000842 this->writeOp(kDrawText_DrawOp);
reed@google.comacd471f2011-05-03 21:26:46 +0000843 fWriter.write32(byteLength);
844 fWriter.writePad(text, byteLength);
845 fWriter.writeScalar(x);
846 fWriter.writeScalar(y);
847 }
reed@google.combb6992a2011-04-26 17:41:56 +0000848 }
849}
850
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000851void SkGPipeCanvas::drawPosText(const void* text, size_t byteLength,
reed@google.combb6992a2011-04-26 17:41:56 +0000852 const SkPoint pos[], const SkPaint& paint) {
853 if (byteLength) {
reed@google.comacd471f2011-05-03 21:26:46 +0000854 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000855 this->writePaint(paint);
reed@google.combb6992a2011-04-26 17:41:56 +0000856 int count = paint.textToGlyphs(text, byteLength, NULL);
reed@google.comacd471f2011-05-03 21:26:46 +0000857 if (this->needOpBytes(4 + SkAlign4(byteLength) + 4 + count * sizeof(SkPoint))) {
reed@google.com31891582011-05-12 03:03:56 +0000858 this->writeOp(kDrawPosText_DrawOp);
reed@google.comacd471f2011-05-03 21:26:46 +0000859 fWriter.write32(byteLength);
860 fWriter.writePad(text, byteLength);
861 fWriter.write32(count);
862 fWriter.write(pos, count * sizeof(SkPoint));
863 }
reed@google.combb6992a2011-04-26 17:41:56 +0000864 }
865}
866
867void SkGPipeCanvas::drawPosTextH(const void* text, size_t byteLength,
868 const SkScalar xpos[], SkScalar constY,
869 const SkPaint& paint) {
870 if (byteLength) {
reed@google.comacd471f2011-05-03 21:26:46 +0000871 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000872 this->writePaint(paint);
reed@google.combb6992a2011-04-26 17:41:56 +0000873 int count = paint.textToGlyphs(text, byteLength, NULL);
reed@google.comacd471f2011-05-03 21:26:46 +0000874 if (this->needOpBytes(4 + SkAlign4(byteLength) + 4 + count * sizeof(SkScalar) + 4)) {
reed@google.com31891582011-05-12 03:03:56 +0000875 this->writeOp(kDrawPosTextH_DrawOp);
reed@google.comacd471f2011-05-03 21:26:46 +0000876 fWriter.write32(byteLength);
877 fWriter.writePad(text, byteLength);
878 fWriter.write32(count);
879 fWriter.write(xpos, count * sizeof(SkScalar));
880 fWriter.writeScalar(constY);
881 }
reed@google.combb6992a2011-04-26 17:41:56 +0000882 }
883}
884
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000885void SkGPipeCanvas::drawTextOnPath(const void* text, size_t byteLength,
886 const SkPath& path, const SkMatrix* matrix,
reed@google.combb6992a2011-04-26 17:41:56 +0000887 const SkPaint& paint) {
888 if (byteLength) {
reed@google.comacd471f2011-05-03 21:26:46 +0000889 NOTIFY_SETUP(this);
reed@google.combb6992a2011-04-26 17:41:56 +0000890 unsigned flags = 0;
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000891 size_t size = 4 + SkAlign4(byteLength) + path.writeToMemory(NULL);
reed@google.combb6992a2011-04-26 17:41:56 +0000892 if (matrix) {
893 flags |= kDrawTextOnPath_HasMatrix_DrawOpFlag;
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000894 size += matrix->writeToMemory(NULL);
reed@google.combb6992a2011-04-26 17:41:56 +0000895 }
reed@google.com31891582011-05-12 03:03:56 +0000896 this->writePaint(paint);
reed@google.comacd471f2011-05-03 21:26:46 +0000897 if (this->needOpBytes(size)) {
reed@google.com31891582011-05-12 03:03:56 +0000898 this->writeOp(kDrawTextOnPath_DrawOp, flags, 0);
reed@google.combb6992a2011-04-26 17:41:56 +0000899
reed@google.comacd471f2011-05-03 21:26:46 +0000900 fWriter.write32(byteLength);
901 fWriter.writePad(text, byteLength);
reed@google.combb6992a2011-04-26 17:41:56 +0000902
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000903 fWriter.writePath(path);
reed@google.comacd471f2011-05-03 21:26:46 +0000904 if (matrix) {
djsollen@google.com2b2ede32012-04-12 13:24:04 +0000905 fWriter.writeMatrix(*matrix);
reed@google.comacd471f2011-05-03 21:26:46 +0000906 }
reed@google.combb6992a2011-04-26 17:41:56 +0000907 }
908 }
909}
910
911void SkGPipeCanvas::drawPicture(SkPicture& picture) {
reed@google.com0faac1e2011-05-11 05:58:58 +0000912 // we want to playback the picture into individual draw calls
913 this->INHERITED::drawPicture(picture);
reed@google.combb6992a2011-04-26 17:41:56 +0000914}
915
reed@google.combb6992a2011-04-26 17:41:56 +0000916void SkGPipeCanvas::drawVertices(VertexMode mode, int vertexCount,
917 const SkPoint vertices[], const SkPoint texs[],
918 const SkColor colors[], SkXfermode*,
919 const uint16_t indices[], int indexCount,
920 const SkPaint& paint) {
921 if (0 == vertexCount) {
922 return;
923 }
924
reed@google.comacd471f2011-05-03 21:26:46 +0000925 NOTIFY_SETUP(this);
926 size_t size = 4 + vertexCount * sizeof(SkPoint);
reed@google.com31891582011-05-12 03:03:56 +0000927 this->writePaint(paint);
reed@google.combb6992a2011-04-26 17:41:56 +0000928 unsigned flags = 0;
929 if (texs) {
930 flags |= kDrawVertices_HasTexs_DrawOpFlag;
reed@google.comacd471f2011-05-03 21:26:46 +0000931 size += vertexCount * sizeof(SkPoint);
reed@google.combb6992a2011-04-26 17:41:56 +0000932 }
933 if (colors) {
934 flags |= kDrawVertices_HasColors_DrawOpFlag;
reed@google.comacd471f2011-05-03 21:26:46 +0000935 size += vertexCount * sizeof(SkColor);
reed@google.combb6992a2011-04-26 17:41:56 +0000936 }
937 if (indices && indexCount > 0) {
938 flags |= kDrawVertices_HasIndices_DrawOpFlag;
reed@google.comacd471f2011-05-03 21:26:46 +0000939 size += 4 + SkAlign4(indexCount * sizeof(uint16_t));
reed@google.combb6992a2011-04-26 17:41:56 +0000940 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000941
reed@google.comacd471f2011-05-03 21:26:46 +0000942 if (this->needOpBytes(size)) {
reed@google.com31891582011-05-12 03:03:56 +0000943 this->writeOp(kDrawVertices_DrawOp, flags, 0);
reed@google.comacd471f2011-05-03 21:26:46 +0000944 fWriter.write32(mode);
945 fWriter.write32(vertexCount);
946 fWriter.write(vertices, vertexCount * sizeof(SkPoint));
947 if (texs) {
948 fWriter.write(texs, vertexCount * sizeof(SkPoint));
949 }
950 if (colors) {
951 fWriter.write(colors, vertexCount * sizeof(SkColor));
952 }
reed@google.combb6992a2011-04-26 17:41:56 +0000953
reed@google.comacd471f2011-05-03 21:26:46 +0000954 // TODO: flatten xfermode
reed@google.combb6992a2011-04-26 17:41:56 +0000955
reed@google.comacd471f2011-05-03 21:26:46 +0000956 if (indices && indexCount > 0) {
957 fWriter.write32(indexCount);
958 fWriter.writePad(indices, indexCount * sizeof(uint16_t));
959 }
reed@google.combb6992a2011-04-26 17:41:56 +0000960 }
961}
962
reed@google.comacd471f2011-05-03 21:26:46 +0000963void SkGPipeCanvas::drawData(const void* ptr, size_t size) {
964 if (size && ptr) {
965 NOTIFY_SETUP(this);
reed@google.combb6992a2011-04-26 17:41:56 +0000966 unsigned data = 0;
967 if (size < (1 << DRAWOPS_DATA_BITS)) {
968 data = (unsigned)size;
969 }
reed@google.comacd471f2011-05-03 21:26:46 +0000970 if (this->needOpBytes(4 + SkAlign4(size))) {
971 this->writeOp(kDrawData_DrawOp, 0, data);
972 if (0 == data) {
973 fWriter.write32(size);
974 }
reed@google.combb6793b2011-05-05 15:18:15 +0000975 fWriter.writePad(ptr, size);
reed@google.combb6992a2011-04-26 17:41:56 +0000976 }
977 }
978}
979
robertphillips@google.com0a4805e2013-05-29 13:24:23 +0000980void SkGPipeCanvas::beginCommentGroup(const char* description) {
981 // ignore for now
982}
983
984void SkGPipeCanvas::addComment(const char* kywd, const char* value) {
985 // ignore for now
986}
987
988void SkGPipeCanvas::endCommentGroup() {
989 // ignore for now
990}
991
junov@chromium.org77eec242012-07-18 17:54:45 +0000992void SkGPipeCanvas::flushRecording(bool detachCurrentBlock) {
993 doNotify();
994 if (detachCurrentBlock) {
995 // force a new block to be requested for the next recorded command
rmistry@google.comd6176b02012-08-23 18:14:13 +0000996 fBlockSize = 0;
junov@chromium.org77eec242012-07-18 17:54:45 +0000997 }
998}
999
junov@chromium.org2e14ba82012-08-07 14:26:57 +00001000size_t SkGPipeCanvas::freeMemoryIfPossible(size_t bytesToFree) {
scroggo@google.comd5d158b2012-08-14 20:38:28 +00001001 return (NULL == fBitmapHeap) ? 0 : fBitmapHeap->freeMemoryIfPossible(bytesToFree);
junov@chromium.org2e14ba82012-08-07 14:26:57 +00001002}
1003
reed@google.combb6992a2011-04-26 17:41:56 +00001004///////////////////////////////////////////////////////////////////////////////
1005
1006template <typename T> uint32_t castToU32(T value) {
1007 union {
1008 T fSrc;
1009 uint32_t fDst;
1010 } data;
1011 data.fSrc = value;
1012 return data.fDst;
1013}
1014
reed@google.com31891582011-05-12 03:03:56 +00001015void SkGPipeCanvas::writePaint(const SkPaint& paint) {
scroggo@google.comd5d158b2012-08-14 20:38:28 +00001016 if (fDone) {
1017 return;
1018 }
reed@google.com31891582011-05-12 03:03:56 +00001019 SkPaint& base = fPaint;
reed@google.combb6992a2011-04-26 17:41:56 +00001020 uint32_t storage[32];
1021 uint32_t* ptr = storage;
reed@google.combb6992a2011-04-26 17:41:56 +00001022
1023 if (base.getFlags() != paint.getFlags()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001024 *ptr++ = PaintOp_packOpData(kFlags_PaintOp, paint.getFlags());
reed@google.comf5842f72011-05-04 18:30:04 +00001025 base.setFlags(paint.getFlags());
reed@google.combb6992a2011-04-26 17:41:56 +00001026 }
1027 if (base.getColor() != paint.getColor()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001028 *ptr++ = PaintOp_packOp(kColor_PaintOp);
1029 *ptr++ = paint.getColor();
reed@google.comf5842f72011-05-04 18:30:04 +00001030 base.setColor(paint.getColor());
reed@google.combb6992a2011-04-26 17:41:56 +00001031 }
1032 if (base.getStyle() != paint.getStyle()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001033 *ptr++ = PaintOp_packOpData(kStyle_PaintOp, paint.getStyle());
reed@google.comf5842f72011-05-04 18:30:04 +00001034 base.setStyle(paint.getStyle());
reed@google.combb6992a2011-04-26 17:41:56 +00001035 }
1036 if (base.getStrokeJoin() != paint.getStrokeJoin()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001037 *ptr++ = PaintOp_packOpData(kJoin_PaintOp, paint.getStrokeJoin());
reed@google.comf5842f72011-05-04 18:30:04 +00001038 base.setStrokeJoin(paint.getStrokeJoin());
reed@google.combb6992a2011-04-26 17:41:56 +00001039 }
1040 if (base.getStrokeCap() != paint.getStrokeCap()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001041 *ptr++ = PaintOp_packOpData(kCap_PaintOp, paint.getStrokeCap());
reed@google.comf5842f72011-05-04 18:30:04 +00001042 base.setStrokeCap(paint.getStrokeCap());
reed@google.combb6992a2011-04-26 17:41:56 +00001043 }
1044 if (base.getStrokeWidth() != paint.getStrokeWidth()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001045 *ptr++ = PaintOp_packOp(kWidth_PaintOp);
1046 *ptr++ = castToU32(paint.getStrokeWidth());
reed@google.comf5842f72011-05-04 18:30:04 +00001047 base.setStrokeWidth(paint.getStrokeWidth());
reed@google.combb6992a2011-04-26 17:41:56 +00001048 }
1049 if (base.getStrokeMiter() != paint.getStrokeMiter()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001050 *ptr++ = PaintOp_packOp(kMiter_PaintOp);
1051 *ptr++ = castToU32(paint.getStrokeMiter());
reed@google.comf5842f72011-05-04 18:30:04 +00001052 base.setStrokeMiter(paint.getStrokeMiter());
reed@google.combb6992a2011-04-26 17:41:56 +00001053 }
1054 if (base.getTextEncoding() != paint.getTextEncoding()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001055 *ptr++ = PaintOp_packOpData(kEncoding_PaintOp, paint.getTextEncoding());
reed@google.comf5842f72011-05-04 18:30:04 +00001056 base.setTextEncoding(paint.getTextEncoding());
reed@google.combb6992a2011-04-26 17:41:56 +00001057 }
1058 if (base.getHinting() != paint.getHinting()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001059 *ptr++ = PaintOp_packOpData(kHinting_PaintOp, paint.getHinting());
reed@google.comf5842f72011-05-04 18:30:04 +00001060 base.setHinting(paint.getHinting());
reed@google.combb6992a2011-04-26 17:41:56 +00001061 }
1062 if (base.getTextAlign() != paint.getTextAlign()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001063 *ptr++ = PaintOp_packOpData(kAlign_PaintOp, paint.getTextAlign());
reed@google.comf5842f72011-05-04 18:30:04 +00001064 base.setTextAlign(paint.getTextAlign());
reed@google.combb6992a2011-04-26 17:41:56 +00001065 }
1066 if (base.getTextSize() != paint.getTextSize()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001067 *ptr++ = PaintOp_packOp(kTextSize_PaintOp);
1068 *ptr++ = castToU32(paint.getTextSize());
reed@google.comf5842f72011-05-04 18:30:04 +00001069 base.setTextSize(paint.getTextSize());
reed@google.combb6992a2011-04-26 17:41:56 +00001070 }
1071 if (base.getTextScaleX() != paint.getTextScaleX()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001072 *ptr++ = PaintOp_packOp(kTextScaleX_PaintOp);
1073 *ptr++ = castToU32(paint.getTextScaleX());
reed@google.comf5842f72011-05-04 18:30:04 +00001074 base.setTextScaleX(paint.getTextScaleX());
reed@google.combb6992a2011-04-26 17:41:56 +00001075 }
1076 if (base.getTextSkewX() != paint.getTextSkewX()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001077 *ptr++ = PaintOp_packOp(kTextSkewX_PaintOp);
1078 *ptr++ = castToU32(paint.getTextSkewX());
reed@google.comf5842f72011-05-04 18:30:04 +00001079 base.setTextSkewX(paint.getTextSkewX());
1080 }
1081
1082 if (!SkTypeface::Equal(base.getTypeface(), paint.getTypeface())) {
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +00001083 if (isCrossProcess(fFlags)) {
scroggo@google.com3cb969f2012-07-27 20:39:19 +00001084 uint32_t id = this->getTypefaceID(paint.getTypeface());
1085 *ptr++ = PaintOp_packOpData(kTypeface_PaintOp, id);
1086 } else if (this->needOpBytes(sizeof(void*))) {
1087 // Add to the set for ref counting.
1088 fTypefaceSet.add(paint.getTypeface());
1089 // It is safe to write the typeface to the stream before the rest
1090 // of the paint unless we ever send a kReset_PaintOp, which we
1091 // currently never do.
1092 this->writeOp(kSetTypeface_DrawOp);
1093 fWriter.writePtr(paint.getTypeface());
1094 }
reed@google.comf5842f72011-05-04 18:30:04 +00001095 base.setTypeface(paint.getTypeface());
reed@google.combb6992a2011-04-26 17:41:56 +00001096 }
reed@google.combb6992a2011-04-26 17:41:56 +00001097
scroggo@google.com4dffc592012-07-17 16:49:40 +00001098 // This is a new paint, so all old flats can be safely purged, if necessary.
1099 fFlattenableHeap.markAllFlatsSafeToDelete();
reed@google.comb55d1182011-05-11 00:42:04 +00001100 for (int i = 0; i < kCount_PaintFlats; i++) {
1101 int index = this->flattenToIndex(get_paintflat(paint, i), (PaintFlats)i);
scroggo@google.comd3ba5cc2012-07-09 16:05:53 +00001102 bool replaced = index < 0;
1103 if (replaced) {
1104 index = ~index;
1105 }
scroggo@google.com4dffc592012-07-17 16:49:40 +00001106 // Store the index of any flat that needs to be kept. 0 means no flat.
1107 if (index > 0) {
1108 fFlattenableHeap.markFlatForKeeping(index);
1109 }
1110 SkASSERT(index >= 0 && index <= fFlatDictionary.count());
scroggo@google.comd3ba5cc2012-07-09 16:05:53 +00001111 if (index != fCurrFlatIndex[i] || replaced) {
reed@google.comb55d1182011-05-11 00:42:04 +00001112 *ptr++ = PaintOp_packOpFlagData(kFlatIndex_PaintOp, i, index);
1113 fCurrFlatIndex[i] = index;
1114 }
1115 }
1116
reed@google.comacd471f2011-05-03 21:26:46 +00001117 size_t size = (char*)ptr - (char*)storage;
1118 if (size && this->needOpBytes(size)) {
reed@google.comb55d1182011-05-11 00:42:04 +00001119 this->writeOp(kPaintOp_DrawOp, 0, size);
reed@google.comb55d1182011-05-11 00:42:04 +00001120 fWriter.write(storage, size);
reed@google.combb6992a2011-04-26 17:41:56 +00001121 for (size_t i = 0; i < size/4; i++) {
reed@google.comb55d1182011-05-11 00:42:04 +00001122// SkDebugf("[%d] %08X\n", i, storage[i]);
reed@google.combb6992a2011-04-26 17:41:56 +00001123 }
1124 }
reed@google.com0cd2ac62013-10-14 20:02:44 +00001125
1126 //
1127 // Do these after we've written kPaintOp_DrawOp
skia.committer@gmail.comfbc58a32013-10-15 07:02:27 +00001128
reed@google.com0cd2ac62013-10-14 20:02:44 +00001129 if (base.getAnnotation() != paint.getAnnotation()) {
1130 if (NULL == paint.getAnnotation()) {
1131 this->writeOp(kSetAnnotation_DrawOp, 0, 0);
1132 } else {
1133 SkOrderedWriteBuffer buffer(1024);
1134 paint.getAnnotation()->writeToBuffer(buffer);
1135 size = buffer.bytesWritten();
skia.committer@gmail.comfbc58a32013-10-15 07:02:27 +00001136
reed@google.com0cd2ac62013-10-14 20:02:44 +00001137 SkAutoMalloc storage(size);
1138 buffer.writeToMemory(storage.get());
skia.committer@gmail.comfbc58a32013-10-15 07:02:27 +00001139
reed@google.com0cd2ac62013-10-14 20:02:44 +00001140 this->writeOp(kSetAnnotation_DrawOp, 0, 1);
1141 fWriter.write32(size);
1142 fWriter.write(storage.get(), size);
1143 }
1144 }
reed@google.combb6992a2011-04-26 17:41:56 +00001145}
1146
1147///////////////////////////////////////////////////////////////////////////////
1148
1149#include "SkGPipe.h"
1150
scroggo@google.com3cb969f2012-07-27 20:39:19 +00001151SkGPipeController::~SkGPipeController() {
1152 SkSafeUnref(fCanvas);
1153}
1154
1155void SkGPipeController::setCanvas(SkGPipeCanvas* canvas) {
1156 SkRefCnt_SafeAssign(fCanvas, canvas);
1157}
1158
1159///////////////////////////////////////////////////////////////////////////////
1160
1161SkGPipeWriter::SkGPipeWriter()
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +00001162: fWriter(0) {
reed@google.combb6992a2011-04-26 17:41:56 +00001163 fCanvas = NULL;
1164}
1165
1166SkGPipeWriter::~SkGPipeWriter() {
reed@google.comacd471f2011-05-03 21:26:46 +00001167 this->endRecording();
reed@google.combb6992a2011-04-26 17:41:56 +00001168}
1169
junov@chromium.orga8db8fe2012-08-15 19:49:22 +00001170SkCanvas* SkGPipeWriter::startRecording(SkGPipeController* controller, uint32_t flags,
1171 uint32_t width, uint32_t height) {
reed@google.combb6992a2011-04-26 17:41:56 +00001172 if (NULL == fCanvas) {
reed@google.comacd471f2011-05-03 21:26:46 +00001173 fWriter.reset(NULL, 0);
junov@chromium.orga8db8fe2012-08-15 19:49:22 +00001174 fCanvas = SkNEW_ARGS(SkGPipeCanvas, (controller, &fWriter, flags, width, height));
reed@google.combb6992a2011-04-26 17:41:56 +00001175 }
scroggo@google.com3cb969f2012-07-27 20:39:19 +00001176 controller->setCanvas(fCanvas);
reed@google.combb6992a2011-04-26 17:41:56 +00001177 return fCanvas;
1178}
1179
1180void SkGPipeWriter::endRecording() {
1181 if (fCanvas) {
1182 fCanvas->finish();
1183 fCanvas->unref();
1184 fCanvas = NULL;
1185 }
1186}
1187
junov@chromium.org2e14ba82012-08-07 14:26:57 +00001188void SkGPipeWriter::flushRecording(bool detachCurrentBlock) {
1189 if (fCanvas) {
1190 fCanvas->flushRecording(detachCurrentBlock);
1191 }
junov@chromium.org77eec242012-07-18 17:54:45 +00001192}
1193
junov@chromium.org2e14ba82012-08-07 14:26:57 +00001194size_t SkGPipeWriter::freeMemoryIfPossible(size_t bytesToFree) {
1195 if (fCanvas) {
1196 return fCanvas->freeMemoryIfPossible(bytesToFree);
1197 }
1198 return 0;
1199}
1200
1201size_t SkGPipeWriter::storageAllocatedForRecording() const {
scroggo@google.com15011ee2012-07-26 20:03:32 +00001202 return NULL == fCanvas ? 0 : fCanvas->storageAllocatedForRecording();
1203}
1204
scroggo@google.com3e26bd02012-08-14 15:20:01 +00001205///////////////////////////////////////////////////////////////////////////////
1206
1207BitmapShuttle::BitmapShuttle(SkGPipeCanvas* canvas) {
1208 SkASSERT(canvas != NULL);
1209 fCanvas = canvas;
1210 fCanvas->ref();
1211}
1212
1213BitmapShuttle::~BitmapShuttle() {
1214 fCanvas->unref();
1215}
1216
1217bool BitmapShuttle::insert(const SkBitmap& bitmap, int32_t slot) {
1218 return fCanvas->shuttleBitmap(bitmap, slot);
1219}