blob: 43240803256ca534e33fe31c8c1169eadeeee19c [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
scroggo@google.com59c3ab62013-11-12 14:32:38 +0000170/**
171 * If SkBitmaps are to be flattened to send to the reader, this class is
172 * provided to the SkBitmapHeap to tell the SkGPipeCanvas to do so.
173 */
174class BitmapShuttle : public SkBitmapHeap::ExternalStorage {
175public:
176 BitmapShuttle(SkGPipeCanvas*);
177
178 ~BitmapShuttle();
179
180 virtual bool insert(const SkBitmap& bitmap, int32_t slot) SK_OVERRIDE;
181
182 /**
183 * Remove the SkGPipeCanvas used for insertion. After this, calls to
184 * insert will crash.
185 */
186 void removeCanvas();
187
188private:
189 SkGPipeCanvas* fCanvas;
190};
191
192///////////////////////////////////////////////////////////////////////////////
193
reed@google.combb6992a2011-04-26 17:41:56 +0000194class SkGPipeCanvas : public SkCanvas {
195public:
junov@chromium.orga8db8fe2012-08-15 19:49:22 +0000196 SkGPipeCanvas(SkGPipeController*, SkWriter32*, uint32_t flags,
197 uint32_t width, uint32_t height);
reed@google.combb6992a2011-04-26 17:41:56 +0000198 virtual ~SkGPipeCanvas();
199
scroggo@google.com59c3ab62013-11-12 14:32:38 +0000200 /**
201 * Called when nothing else is to be written to the stream. Any repeated
202 * calls are ignored.
203 *
204 * @param notifyReaders Whether to send a message to the reader(s) that
205 * the writer is through sending commands. Should generally be true,
206 * unless there is an error which prevents further messages from
207 * being sent.
208 */
209 void finish(bool notifyReaders) {
210 if (fDone) {
211 return;
reed@google.combb6992a2011-04-26 17:41:56 +0000212 }
scroggo@google.com59c3ab62013-11-12 14:32:38 +0000213 if (notifyReaders && this->needOpBytes()) {
214 this->writeOp(kDone_DrawOp);
215 this->doNotify();
216 }
217 if (shouldFlattenBitmaps(fFlags)) {
218 // The following circular references exist:
219 // fFlattenableHeap -> fWriteBuffer -> fBitmapStorage -> fExternalStorage -> fCanvas
220 // fBitmapHeap -> fExternalStorage -> fCanvas
221 // fFlattenableHeap -> fBitmapStorage -> fExternalStorage -> fCanvas
222
223 // Break them all by destroying the final link to this SkGPipeCanvas.
224 fBitmapShuttle->removeCanvas();
225 }
226 fDone = true;
reed@google.combb6992a2011-04-26 17:41:56 +0000227 }
228
junov@chromium.org77eec242012-07-18 17:54:45 +0000229 void flushRecording(bool detachCurrentBlock);
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000230 size_t freeMemoryIfPossible(size_t bytesToFree);
junov@chromium.org77eec242012-07-18 17:54:45 +0000231
scroggo@google.com15011ee2012-07-26 20:03:32 +0000232 size_t storageAllocatedForRecording() {
scroggo@google.comd5d158b2012-08-14 20:38:28 +0000233 return (NULL == fBitmapHeap) ? 0 : fBitmapHeap->bytesAllocated();
scroggo@google.com15011ee2012-07-26 20:03:32 +0000234 }
235
reed@google.combb6992a2011-04-26 17:41:56 +0000236 // overrides from SkCanvas
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000237 virtual int save(SaveFlags) SK_OVERRIDE;
238 virtual int saveLayer(const SkRect* bounds, const SkPaint*,
239 SaveFlags) SK_OVERRIDE;
240 virtual void restore() SK_OVERRIDE;
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000241 virtual bool isDrawingToLayer() const SK_OVERRIDE;
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000242 virtual bool translate(SkScalar dx, SkScalar dy) SK_OVERRIDE;
243 virtual bool scale(SkScalar sx, SkScalar sy) SK_OVERRIDE;
244 virtual bool rotate(SkScalar degrees) SK_OVERRIDE;
245 virtual bool skew(SkScalar sx, SkScalar sy) SK_OVERRIDE;
246 virtual bool concat(const SkMatrix& matrix) SK_OVERRIDE;
247 virtual void setMatrix(const SkMatrix& matrix) SK_OVERRIDE;
reed@google.com4ed0fb72012-12-12 20:48:18 +0000248 virtual bool clipRect(const SkRect&, SkRegion::Op op, bool doAntiAlias = false) SK_OVERRIDE;
249 virtual bool clipRRect(const SkRRect&, SkRegion::Op op, bool doAntiAlias = false) SK_OVERRIDE;
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000250 virtual bool clipPath(const SkPath& path, SkRegion::Op op,
251 bool doAntiAlias = false) SK_OVERRIDE;
252 virtual bool clipRegion(const SkRegion& region, SkRegion::Op op) SK_OVERRIDE;
253 virtual void clear(SkColor) SK_OVERRIDE;
254 virtual void drawPaint(const SkPaint& paint) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000255 virtual void drawPoints(PointMode, size_t count, const SkPoint pts[],
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000256 const SkPaint&) SK_OVERRIDE;
reed@google.com4ed0fb72012-12-12 20:48:18 +0000257 virtual void drawOval(const SkRect&, const SkPaint&) SK_OVERRIDE;
bsalomon@google.com7ce564c2013-10-22 16:54:15 +0000258 virtual void drawRect(const SkRect& rect, const SkPaint&) SK_OVERRIDE;
reed@google.com4ed0fb72012-12-12 20:48:18 +0000259 virtual void drawRRect(const SkRRect&, const SkPaint&) SK_OVERRIDE;
bsalomon@google.com7ce564c2013-10-22 16:54:15 +0000260 virtual void drawPath(const SkPath& path, const SkPaint&) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000261 virtual void drawBitmap(const SkBitmap&, SkScalar left, SkScalar top,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000262 const SkPaint*) SK_OVERRIDE;
reed@google.com71121732012-09-18 15:14:33 +0000263 virtual void drawBitmapRectToRect(const SkBitmap&, const SkRect* src,
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000264 const SkRect& dst, const SkPaint* paint,
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +0000265 DrawBitmapRectFlags flags) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000266 virtual void drawBitmapMatrix(const SkBitmap&, const SkMatrix&,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000267 const SkPaint*) SK_OVERRIDE;
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000268 virtual void drawBitmapNine(const SkBitmap& bitmap, const SkIRect& center,
269 const SkRect& dst, const SkPaint* paint = NULL) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000270 virtual void drawSprite(const SkBitmap&, int left, int top,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000271 const SkPaint*) SK_OVERRIDE;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000272 virtual void drawText(const void* text, size_t byteLength, SkScalar x,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000273 SkScalar y, const SkPaint&) SK_OVERRIDE;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000274 virtual void drawPosText(const void* text, size_t byteLength,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000275 const SkPoint pos[], const SkPaint&) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000276 virtual void drawPosTextH(const void* text, size_t byteLength,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000277 const SkScalar xpos[], SkScalar constY,
278 const SkPaint&) SK_OVERRIDE;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000279 virtual void drawTextOnPath(const void* text, size_t byteLength,
280 const SkPath& path, const SkMatrix* matrix,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000281 const SkPaint&) SK_OVERRIDE;
282 virtual void drawPicture(SkPicture& picture) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000283 virtual void drawVertices(VertexMode, int vertexCount,
284 const SkPoint vertices[], const SkPoint texs[],
285 const SkColor colors[], SkXfermode*,
286 const uint16_t indices[], int indexCount,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000287 const SkPaint&) SK_OVERRIDE;
288 virtual void drawData(const void*, size_t) SK_OVERRIDE;
robertphillips@google.com0a4805e2013-05-29 13:24:23 +0000289 virtual void beginCommentGroup(const char* description) SK_OVERRIDE;
290 virtual void addComment(const char* kywd, const char* value) SK_OVERRIDE;
291 virtual void endCommentGroup() SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000292
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000293 /**
294 * Flatten an SkBitmap to send to the reader, where it will be referenced
295 * according to slot.
296 */
297 bool shuttleBitmap(const SkBitmap&, int32_t slot);
reed@google.combb6992a2011-04-26 17:41:56 +0000298private:
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000299 enum {
300 kNoSaveLayer = -1,
301 };
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000302 SkNamedFactorySet* fFactorySet;
303 int fFirstSaveLayerStackLevel;
scroggo@google.comd9d29672012-08-14 17:21:34 +0000304 SkBitmapHeap* fBitmapHeap;
reed@google.comacd471f2011-05-03 21:26:46 +0000305 SkGPipeController* fController;
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000306 SkWriter32& fWriter;
307 size_t fBlockSize; // amount allocated for writer
308 size_t fBytesNotified;
309 bool fDone;
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000310 const uint32_t fFlags;
reed@google.combb6992a2011-04-26 17:41:56 +0000311
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000312 SkRefCntSet fTypefaceSet;
reed@google.comf5842f72011-05-04 18:30:04 +0000313
314 uint32_t getTypefaceID(SkTypeface*);
315
reed@google.comacd471f2011-05-03 21:26:46 +0000316 inline void writeOp(DrawOps op, unsigned flags, unsigned data) {
reed@google.combb6992a2011-04-26 17:41:56 +0000317 fWriter.write32(DrawOp_packOpFlagData(op, flags, data));
318 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000319
reed@google.comacd471f2011-05-03 21:26:46 +0000320 inline void writeOp(DrawOps op) {
reed@google.combb6992a2011-04-26 17:41:56 +0000321 fWriter.write32(DrawOp_packOpFlagData(op, 0, 0));
322 }
reed@google.comacd471f2011-05-03 21:26:46 +0000323
324 bool needOpBytes(size_t size = 0);
325
326 inline void doNotify() {
327 if (!fDone) {
reed@google.com44699382013-10-31 17:28:30 +0000328 size_t bytes = fWriter.bytesWritten() - fBytesNotified;
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000329 if (bytes > 0) {
330 fController->notifyWritten(bytes);
331 fBytesNotified += bytes;
332 }
reed@google.comacd471f2011-05-03 21:26:46 +0000333 }
334 }
reed@google.comb55d1182011-05-11 00:42:04 +0000335
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000336 // Should be called after any calls to an SkFlatDictionary::findAndReplace
337 // if a new SkFlatData was added when in cross process mode
338 void flattenFactoryNames();
339
scroggo@google.com59c3ab62013-11-12 14:32:38 +0000340 FlattenableHeap fFlattenableHeap;
341 FlatDictionary fFlatDictionary;
342 SkAutoTUnref<BitmapShuttle> fBitmapShuttle;
343 int fCurrFlatIndex[kCount_PaintFlats];
344
reed@google.comb55d1182011-05-11 00:42:04 +0000345 int flattenToIndex(SkFlattenable* obj, PaintFlats);
346
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000347 // Common code used by drawBitmap*. Behaves differently depending on the
348 // type of SkBitmapHeap being used, which is determined by the flags used.
349 bool commonDrawBitmap(const SkBitmap& bm, DrawOps op, unsigned flags,
350 size_t opBytesNeeded, const SkPaint* paint);
scroggo@google.com58be6822012-07-30 14:40:01 +0000351
reed@google.com31891582011-05-12 03:03:56 +0000352 SkPaint fPaint;
353 void writePaint(const SkPaint&);
reed@google.combb6992a2011-04-26 17:41:56 +0000354
reed@google.comacd471f2011-05-03 21:26:46 +0000355 class AutoPipeNotify {
356 public:
357 AutoPipeNotify(SkGPipeCanvas* canvas) : fCanvas(canvas) {}
358 ~AutoPipeNotify() { fCanvas->doNotify(); }
359 private:
360 SkGPipeCanvas* fCanvas;
361 };
362 friend class AutoPipeNotify;
363
reed@google.combb6992a2011-04-26 17:41:56 +0000364 typedef SkCanvas INHERITED;
365};
366
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000367void SkGPipeCanvas::flattenFactoryNames() {
368 const char* name;
369 while ((name = fFactorySet->getNextAddedFactoryName()) != NULL) {
370 size_t len = strlen(name);
371 if (this->needOpBytes(len)) {
372 this->writeOp(kDef_Factory_DrawOp);
373 fWriter.writeString(name, len);
374 }
375 }
376}
377
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000378bool SkGPipeCanvas::shuttleBitmap(const SkBitmap& bm, int32_t slot) {
scroggo@google.com565254b2012-06-28 15:41:32 +0000379 SkASSERT(shouldFlattenBitmaps(fFlags));
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000380 SkOrderedWriteBuffer buffer(1024);
381 buffer.setNamedFactoryRecorder(fFactorySet);
scroggo@google.com74b7ffd2013-04-30 02:32:41 +0000382 buffer.writeBitmap(bm);
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000383 this->flattenFactoryNames();
384 uint32_t size = buffer.size();
385 if (this->needOpBytes(size)) {
386 this->writeOp(kDef_Bitmap_DrawOp, 0, slot);
387 void* dst = static_cast<void*>(fWriter.reserve(size));
388 buffer.writeToMemory(dst);
389 return true;
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000390 }
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000391 return false;
scroggo@google.comd3ba5cc2012-07-09 16:05:53 +0000392}
393
reed@google.comb55d1182011-05-11 00:42:04 +0000394// return 0 for NULL (or unflattenable obj), or index-base-1
scroggo@google.comd3ba5cc2012-07-09 16:05:53 +0000395// return ~(index-base-1) if an old flattenable was replaced
reed@google.comb55d1182011-05-11 00:42:04 +0000396int SkGPipeCanvas::flattenToIndex(SkFlattenable* obj, PaintFlats paintflat) {
scroggo@google.comd5d158b2012-08-14 20:38:28 +0000397 SkASSERT(!fDone && fBitmapHeap != NULL);
reed@google.comb55d1182011-05-11 00:42:04 +0000398 if (NULL == obj) {
399 return 0;
400 }
reed@google.comb55d1182011-05-11 00:42:04 +0000401
scroggo@google.comd9d29672012-08-14 17:21:34 +0000402 fBitmapHeap->deferAddingOwners();
scroggo@google.com4dffc592012-07-17 16:49:40 +0000403 bool added, replaced;
scroggo@google.com664fab12012-08-14 19:22:05 +0000404 const SkFlatData* flat = fFlatDictionary.findAndReplace(*obj, fFlattenableHeap.flatToReplace(),
405 &added, &replaced);
scroggo@google.comd9d29672012-08-14 17:21:34 +0000406 fBitmapHeap->endAddingOwnersDeferral(added);
scroggo@google.com4dffc592012-07-17 16:49:40 +0000407 int index = flat->index();
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000408 if (added) {
409 if (isCrossProcess(fFlags)) {
410 this->flattenFactoryNames();
411 }
412 size_t flatSize = flat->flatSize();
413 if (this->needOpBytes(flatSize)) {
414 this->writeOp(kDef_Flattenable_DrawOp, paintflat, index);
415 fWriter.write(flat->data(), flatSize);
416 }
scroggo@google.com4dffc592012-07-17 16:49:40 +0000417 }
418 if (replaced) {
reed@google.comb55d1182011-05-11 00:42:04 +0000419 index = ~index;
reed@google.comb55d1182011-05-11 00:42:04 +0000420 }
scroggo@google.com4dffc592012-07-17 16:49:40 +0000421 return index;
reed@google.comb55d1182011-05-11 00:42:04 +0000422}
423
reed@google.combb6992a2011-04-26 17:41:56 +0000424///////////////////////////////////////////////////////////////////////////////
425
reed@google.comacd471f2011-05-03 21:26:46 +0000426#define MIN_BLOCK_SIZE (16 * 1024)
scroggo@google.com4dffc592012-07-17 16:49:40 +0000427#define BITMAPS_TO_KEEP 5
428#define FLATTENABLES_TO_KEEP 10
reed@google.combb6992a2011-04-26 17:41:56 +0000429
reed@google.comacd471f2011-05-03 21:26:46 +0000430SkGPipeCanvas::SkGPipeCanvas(SkGPipeController* controller,
rmistry@google.comd6176b02012-08-23 18:14:13 +0000431 SkWriter32* writer, uint32_t flags,
junov@chromium.orga8db8fe2012-08-15 19:49:22 +0000432 uint32_t width, uint32_t height)
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000433: fFactorySet(isCrossProcess(flags) ? SkNEW(SkNamedFactorySet) : NULL)
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000434, fWriter(*writer)
435, fFlags(flags)
scroggo@google.com664fab12012-08-14 19:22:05 +0000436, fFlattenableHeap(FLATTENABLES_TO_KEEP, fFactorySet, isCrossProcess(flags))
scroggo@google.com15543602012-08-02 18:49:49 +0000437, fFlatDictionary(&fFlattenableHeap) {
reed@google.comacd471f2011-05-03 21:26:46 +0000438 fController = controller;
reed@google.combb6992a2011-04-26 17:41:56 +0000439 fDone = false;
reed@google.comacd471f2011-05-03 21:26:46 +0000440 fBlockSize = 0; // need first block from controller
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000441 fBytesNotified = 0;
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000442 fFirstSaveLayerStackLevel = kNoSaveLayer;
reed@google.comb55d1182011-05-11 00:42:04 +0000443 sk_bzero(fCurrFlatIndex, sizeof(fCurrFlatIndex));
reed@google.comacd471f2011-05-03 21:26:46 +0000444
reed@google.combb6793b2011-05-05 15:18:15 +0000445 // we need a device to limit our clip
yangsu@google.com06b4da12011-06-17 15:04:40 +0000446 // We don't allocate pixels for the bitmap
reed@google.combb6793b2011-05-05 15:18:15 +0000447 SkBitmap bitmap;
junov@chromium.orga8db8fe2012-08-15 19:49:22 +0000448 bitmap.setConfig(SkBitmap::kARGB_8888_Config, width, height);
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000449 SkBaseDevice* device = SkNEW_ARGS(SkBitmapDevice, (bitmap));
reed@google.combb6793b2011-05-05 15:18:15 +0000450 this->setDevice(device)->unref();
scroggo@google.com10dccde2012-08-08 20:43:22 +0000451
scroggo@google.com565254b2012-06-28 15:41:32 +0000452 // Tell the reader the appropriate flags to use.
453 if (this->needOpBytes()) {
454 this->writeOp(kReportFlags_DrawOp, fFlags, 0);
455 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000456
scroggo@google.com10dccde2012-08-08 20:43:22 +0000457 if (shouldFlattenBitmaps(flags)) {
scroggo@google.com59c3ab62013-11-12 14:32:38 +0000458 fBitmapShuttle.reset(SkNEW_ARGS(BitmapShuttle, (this)));
459 fBitmapHeap = SkNEW_ARGS(SkBitmapHeap, (fBitmapShuttle.get(), BITMAPS_TO_KEEP));
scroggo@google.com10dccde2012-08-08 20:43:22 +0000460 } else {
scroggo@google.comd9d29672012-08-14 17:21:34 +0000461 fBitmapHeap = SkNEW_ARGS(SkBitmapHeap,
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000462 (BITMAPS_TO_KEEP, controller->numberOfReaders()));
scroggo@google.com10dccde2012-08-08 20:43:22 +0000463 if (this->needOpBytes(sizeof(void*))) {
scroggo@google.comd9d29672012-08-14 17:21:34 +0000464 this->writeOp(kShareBitmapHeap_DrawOp);
465 fWriter.writePtr(static_cast<void*>(fBitmapHeap));
scroggo@google.com10dccde2012-08-08 20:43:22 +0000466 }
467 }
scroggo@google.comd9d29672012-08-14 17:21:34 +0000468 fFlattenableHeap.setBitmapStorage(fBitmapHeap);
scroggo@google.com10dccde2012-08-08 20:43:22 +0000469 this->doNotify();
reed@google.combb6992a2011-04-26 17:41:56 +0000470}
471
472SkGPipeCanvas::~SkGPipeCanvas() {
scroggo@google.com59c3ab62013-11-12 14:32:38 +0000473 this->finish(true);
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000474 SkSafeUnref(fFactorySet);
scroggo@google.comd9d29672012-08-14 17:21:34 +0000475 SkSafeUnref(fBitmapHeap);
reed@google.combb6992a2011-04-26 17:41:56 +0000476}
477
reed@google.comacd471f2011-05-03 21:26:46 +0000478bool SkGPipeCanvas::needOpBytes(size_t needed) {
479 if (fDone) {
480 return false;
481 }
482
483 needed += 4; // size of DrawOp atom
reed@google.com44699382013-10-31 17:28:30 +0000484 if (fWriter.bytesWritten() + needed > fBlockSize) {
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000485 // Before we wipe out any data that has already been written, read it
486 // out.
487 this->doNotify();
488 size_t blockSize = SkMax32(MIN_BLOCK_SIZE, needed);
489 void* block = fController->requestBlock(blockSize, &fBlockSize);
reed@google.comacd471f2011-05-03 21:26:46 +0000490 if (NULL == block) {
scroggo@google.com59c3ab62013-11-12 14:32:38 +0000491 // Do not notify the readers, which would call this function again.
492 this->finish(false);
reed@google.comacd471f2011-05-03 21:26:46 +0000493 return false;
494 }
scroggo@google.com460a23e2012-08-16 17:56:49 +0000495 SkASSERT(SkIsAlign4(fBlockSize));
reed@google.comacd471f2011-05-03 21:26:46 +0000496 fWriter.reset(block, fBlockSize);
497 fBytesNotified = 0;
498 }
499 return true;
500}
501
reed@google.comf5842f72011-05-04 18:30:04 +0000502uint32_t SkGPipeCanvas::getTypefaceID(SkTypeface* face) {
503 uint32_t id = 0; // 0 means default/null typeface
504 if (face) {
505 id = fTypefaceSet.find(face);
506 if (0 == id) {
507 id = fTypefaceSet.add(face);
508 size_t size = writeTypeface(NULL, face);
509 if (this->needOpBytes(size)) {
reed@google.combb6793b2011-05-05 15:18:15 +0000510 this->writeOp(kDef_Typeface_DrawOp);
reed@google.comf5842f72011-05-04 18:30:04 +0000511 writeTypeface(&fWriter, face);
512 }
513 }
514 }
515 return id;
516}
517
reed@google.combb6992a2011-04-26 17:41:56 +0000518///////////////////////////////////////////////////////////////////////////////
519
reed@google.comacd471f2011-05-03 21:26:46 +0000520#define NOTIFY_SETUP(canvas) \
521 AutoPipeNotify apn(canvas)
522
reed@google.combb6992a2011-04-26 17:41:56 +0000523int SkGPipeCanvas::save(SaveFlags flags) {
reed@google.comacd471f2011-05-03 21:26:46 +0000524 NOTIFY_SETUP(this);
525 if (this->needOpBytes()) {
526 this->writeOp(kSave_DrawOp, 0, flags);
527 }
reed@google.combb6992a2011-04-26 17:41:56 +0000528 return this->INHERITED::save(flags);
529}
530
531int SkGPipeCanvas::saveLayer(const SkRect* bounds, const SkPaint* paint,
532 SaveFlags saveFlags) {
reed@google.comacd471f2011-05-03 21:26:46 +0000533 NOTIFY_SETUP(this);
534 size_t size = 0;
reed@google.combb6992a2011-04-26 17:41:56 +0000535 unsigned opFlags = 0;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000536
reed@google.combb6992a2011-04-26 17:41:56 +0000537 if (bounds) {
538 opFlags |= kSaveLayer_HasBounds_DrawOpFlag;
reed@google.comacd471f2011-05-03 21:26:46 +0000539 size += sizeof(SkRect);
reed@google.combb6992a2011-04-26 17:41:56 +0000540 }
541 if (paint) {
542 opFlags |= kSaveLayer_HasPaint_DrawOpFlag;
reed@google.com31891582011-05-12 03:03:56 +0000543 this->writePaint(*paint);
reed@google.combb6992a2011-04-26 17:41:56 +0000544 }
545
reed@google.comacd471f2011-05-03 21:26:46 +0000546 if (this->needOpBytes(size)) {
547 this->writeOp(kSaveLayer_DrawOp, opFlags, saveFlags);
548 if (bounds) {
549 fWriter.writeRect(*bounds);
550 }
reed@google.combb6992a2011-04-26 17:41:56 +0000551 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000552
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000553 if (kNoSaveLayer == fFirstSaveLayerStackLevel){
554 fFirstSaveLayerStackLevel = this->getSaveCount();
555 }
reed@google.combb6992a2011-04-26 17:41:56 +0000556 // we just pass on the save, so we don't create a layer
557 return this->INHERITED::save(saveFlags);
558}
559
560void SkGPipeCanvas::restore() {
reed@google.comacd471f2011-05-03 21:26:46 +0000561 NOTIFY_SETUP(this);
562 if (this->needOpBytes()) {
563 this->writeOp(kRestore_DrawOp);
564 }
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000565
reed@google.combb6992a2011-04-26 17:41:56 +0000566 this->INHERITED::restore();
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000567
568 if (this->getSaveCount() == fFirstSaveLayerStackLevel){
569 fFirstSaveLayerStackLevel = kNoSaveLayer;
570 }
571}
572
573bool SkGPipeCanvas::isDrawingToLayer() const {
574 return kNoSaveLayer != fFirstSaveLayerStackLevel;
reed@google.combb6992a2011-04-26 17:41:56 +0000575}
576
577bool SkGPipeCanvas::translate(SkScalar dx, SkScalar dy) {
578 if (dx || dy) {
reed@google.comacd471f2011-05-03 21:26:46 +0000579 NOTIFY_SETUP(this);
580 if (this->needOpBytes(2 * sizeof(SkScalar))) {
581 this->writeOp(kTranslate_DrawOp);
582 fWriter.writeScalar(dx);
583 fWriter.writeScalar(dy);
584 }
reed@google.combb6992a2011-04-26 17:41:56 +0000585 }
586 return this->INHERITED::translate(dx, dy);
587}
588
589bool SkGPipeCanvas::scale(SkScalar sx, SkScalar sy) {
590 if (sx || sy) {
reed@google.comacd471f2011-05-03 21:26:46 +0000591 NOTIFY_SETUP(this);
592 if (this->needOpBytes(2 * sizeof(SkScalar))) {
593 this->writeOp(kScale_DrawOp);
594 fWriter.writeScalar(sx);
595 fWriter.writeScalar(sy);
596 }
reed@google.combb6992a2011-04-26 17:41:56 +0000597 }
598 return this->INHERITED::scale(sx, sy);
599}
600
601bool SkGPipeCanvas::rotate(SkScalar degrees) {
602 if (degrees) {
reed@google.comacd471f2011-05-03 21:26:46 +0000603 NOTIFY_SETUP(this);
604 if (this->needOpBytes(sizeof(SkScalar))) {
605 this->writeOp(kRotate_DrawOp);
606 fWriter.writeScalar(degrees);
607 }
reed@google.combb6992a2011-04-26 17:41:56 +0000608 }
609 return this->INHERITED::rotate(degrees);
610}
611
612bool SkGPipeCanvas::skew(SkScalar sx, SkScalar sy) {
613 if (sx || sy) {
reed@google.comacd471f2011-05-03 21:26:46 +0000614 NOTIFY_SETUP(this);
615 if (this->needOpBytes(2 * sizeof(SkScalar))) {
616 this->writeOp(kSkew_DrawOp);
617 fWriter.writeScalar(sx);
618 fWriter.writeScalar(sy);
619 }
reed@google.combb6992a2011-04-26 17:41:56 +0000620 }
621 return this->INHERITED::skew(sx, sy);
622}
623
624bool SkGPipeCanvas::concat(const SkMatrix& matrix) {
625 if (!matrix.isIdentity()) {
reed@google.comacd471f2011-05-03 21:26:46 +0000626 NOTIFY_SETUP(this);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000627 if (this->needOpBytes(matrix.writeToMemory(NULL))) {
reed@google.comacd471f2011-05-03 21:26:46 +0000628 this->writeOp(kConcat_DrawOp);
djsollen@google.com2b2ede32012-04-12 13:24:04 +0000629 fWriter.writeMatrix(matrix);
reed@google.comacd471f2011-05-03 21:26:46 +0000630 }
reed@google.combb6992a2011-04-26 17:41:56 +0000631 }
632 return this->INHERITED::concat(matrix);
633}
634
635void SkGPipeCanvas::setMatrix(const SkMatrix& matrix) {
reed@google.comacd471f2011-05-03 21:26:46 +0000636 NOTIFY_SETUP(this);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000637 if (this->needOpBytes(matrix.writeToMemory(NULL))) {
reed@google.comacd471f2011-05-03 21:26:46 +0000638 this->writeOp(kSetMatrix_DrawOp);
djsollen@google.com2b2ede32012-04-12 13:24:04 +0000639 fWriter.writeMatrix(matrix);
reed@google.comacd471f2011-05-03 21:26:46 +0000640 }
reed@google.combb6992a2011-04-26 17:41:56 +0000641 this->INHERITED::setMatrix(matrix);
642}
643
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000644bool SkGPipeCanvas::clipRect(const SkRect& rect, SkRegion::Op rgnOp,
645 bool doAntiAlias) {
reed@google.comacd471f2011-05-03 21:26:46 +0000646 NOTIFY_SETUP(this);
scroggo@google.com460a23e2012-08-16 17:56:49 +0000647 if (this->needOpBytes(sizeof(SkRect))) {
648 unsigned flags = doAntiAlias & kClip_HasAntiAlias_DrawOpFlag;
649 this->writeOp(kClipRect_DrawOp, flags, rgnOp);
reed@google.comacd471f2011-05-03 21:26:46 +0000650 fWriter.writeRect(rect);
651 }
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000652 return this->INHERITED::clipRect(rect, rgnOp, doAntiAlias);
reed@google.combb6992a2011-04-26 17:41:56 +0000653}
654
reed@google.com4ed0fb72012-12-12 20:48:18 +0000655bool SkGPipeCanvas::clipRRect(const SkRRect& rrect, SkRegion::Op rgnOp,
656 bool doAntiAlias) {
657 NOTIFY_SETUP(this);
658 if (this->needOpBytes(kSizeOfFlatRRect)) {
659 unsigned flags = doAntiAlias & kClip_HasAntiAlias_DrawOpFlag;
660 this->writeOp(kClipRRect_DrawOp, flags, rgnOp);
661 fWriter.writeRRect(rrect);
662 }
663 return this->INHERITED::clipRRect(rrect, rgnOp, doAntiAlias);
664}
665
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000666bool SkGPipeCanvas::clipPath(const SkPath& path, SkRegion::Op rgnOp,
667 bool doAntiAlias) {
reed@google.comacd471f2011-05-03 21:26:46 +0000668 NOTIFY_SETUP(this);
scroggo@google.com460a23e2012-08-16 17:56:49 +0000669 if (this->needOpBytes(path.writeToMemory(NULL))) {
670 unsigned flags = doAntiAlias & kClip_HasAntiAlias_DrawOpFlag;
671 this->writeOp(kClipPath_DrawOp, flags, rgnOp);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000672 fWriter.writePath(path);
reed@google.comacd471f2011-05-03 21:26:46 +0000673 }
reed@google.combb6992a2011-04-26 17:41:56 +0000674 // we just pass on the bounds of the path
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000675 return this->INHERITED::clipRect(path.getBounds(), rgnOp, doAntiAlias);
reed@google.combb6992a2011-04-26 17:41:56 +0000676}
677
678bool SkGPipeCanvas::clipRegion(const SkRegion& region, SkRegion::Op rgnOp) {
reed@google.comacd471f2011-05-03 21:26:46 +0000679 NOTIFY_SETUP(this);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000680 if (this->needOpBytes(region.writeToMemory(NULL))) {
reed@google.comacd471f2011-05-03 21:26:46 +0000681 this->writeOp(kClipRegion_DrawOp, 0, rgnOp);
djsollen@google.com2b2ede32012-04-12 13:24:04 +0000682 fWriter.writeRegion(region);
reed@google.comacd471f2011-05-03 21:26:46 +0000683 }
reed@google.combb6992a2011-04-26 17:41:56 +0000684 return this->INHERITED::clipRegion(region, rgnOp);
685}
686
687///////////////////////////////////////////////////////////////////////////////
688
689void SkGPipeCanvas::clear(SkColor color) {
reed@google.comacd471f2011-05-03 21:26:46 +0000690 NOTIFY_SETUP(this);
reed@google.combb6992a2011-04-26 17:41:56 +0000691 unsigned flags = 0;
692 if (color) {
693 flags |= kClear_HasColor_DrawOpFlag;
694 }
reed@google.comacd471f2011-05-03 21:26:46 +0000695 if (this->needOpBytes(sizeof(SkColor))) {
696 this->writeOp(kDrawClear_DrawOp, flags, 0);
697 if (color) {
698 fWriter.write32(color);
699 }
reed@google.combb6992a2011-04-26 17:41:56 +0000700 }
701}
702
703void SkGPipeCanvas::drawPaint(const SkPaint& paint) {
reed@google.comacd471f2011-05-03 21:26:46 +0000704 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000705 this->writePaint(paint);
reed@google.comacd471f2011-05-03 21:26:46 +0000706 if (this->needOpBytes()) {
reed@google.com31891582011-05-12 03:03:56 +0000707 this->writeOp(kDrawPaint_DrawOp);
reed@google.comacd471f2011-05-03 21:26:46 +0000708 }
reed@google.combb6992a2011-04-26 17:41:56 +0000709}
710
711void SkGPipeCanvas::drawPoints(PointMode mode, size_t count,
robertphillips@google.com8b169312013-10-15 17:47:36 +0000712 const SkPoint pts[], const SkPaint& paint) {
reed@google.combb6992a2011-04-26 17:41:56 +0000713 if (count) {
reed@google.comacd471f2011-05-03 21:26:46 +0000714 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000715 this->writePaint(paint);
reed@google.comacd471f2011-05-03 21:26:46 +0000716 if (this->needOpBytes(4 + count * sizeof(SkPoint))) {
reed@google.com31891582011-05-12 03:03:56 +0000717 this->writeOp(kDrawPoints_DrawOp, mode, 0);
reed@google.comacd471f2011-05-03 21:26:46 +0000718 fWriter.write32(count);
719 fWriter.write(pts, count * sizeof(SkPoint));
720 }
reed@google.combb6992a2011-04-26 17:41:56 +0000721 }
722}
723
reed@google.com4ed0fb72012-12-12 20:48:18 +0000724void SkGPipeCanvas::drawOval(const SkRect& rect, const SkPaint& paint) {
725 NOTIFY_SETUP(this);
726 this->writePaint(paint);
727 if (this->needOpBytes(sizeof(SkRect))) {
728 this->writeOp(kDrawOval_DrawOp);
729 fWriter.writeRect(rect);
730 }
731}
732
bsalomon@google.com7ce564c2013-10-22 16:54:15 +0000733void SkGPipeCanvas::drawRect(const SkRect& rect, 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);
reed@google.comacd471f2011-05-03 21:26:46 +0000736 if (this->needOpBytes(sizeof(SkRect))) {
reed@google.com31891582011-05-12 03:03:56 +0000737 this->writeOp(kDrawRect_DrawOp);
reed@google.comacd471f2011-05-03 21:26:46 +0000738 fWriter.writeRect(rect);
739 }
reed@google.combb6992a2011-04-26 17:41:56 +0000740}
741
reed@google.com4ed0fb72012-12-12 20:48:18 +0000742void SkGPipeCanvas::drawRRect(const SkRRect& rrect, const SkPaint& paint) {
743 NOTIFY_SETUP(this);
744 this->writePaint(paint);
745 if (this->needOpBytes(kSizeOfFlatRRect)) {
746 this->writeOp(kDrawRRect_DrawOp);
747 fWriter.writeRRect(rrect);
748 }
749}
750
bsalomon@google.com7ce564c2013-10-22 16:54:15 +0000751void SkGPipeCanvas::drawPath(const SkPath& path, const SkPaint& paint) {
reed@google.comacd471f2011-05-03 21:26:46 +0000752 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000753 this->writePaint(paint);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000754 if (this->needOpBytes(path.writeToMemory(NULL))) {
reed@google.com31891582011-05-12 03:03:56 +0000755 this->writeOp(kDrawPath_DrawOp);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000756 fWriter.writePath(path);
reed@google.comacd471f2011-05-03 21:26:46 +0000757 }
reed@google.combb6992a2011-04-26 17:41:56 +0000758}
759
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000760bool SkGPipeCanvas::commonDrawBitmap(const SkBitmap& bm, DrawOps op,
761 unsigned flags,
762 size_t opBytesNeeded,
763 const SkPaint* paint) {
scroggo@google.com58be6822012-07-30 14:40:01 +0000764 if (paint != NULL) {
scroggo@google.com460a23e2012-08-16 17:56:49 +0000765 flags |= kDrawBitmap_HasPaint_DrawOpFlag;
scroggo@google.com58be6822012-07-30 14:40:01 +0000766 this->writePaint(*paint);
767 }
scroggo@google.com10dccde2012-08-08 20:43:22 +0000768 if (this->needOpBytes(opBytesNeeded)) {
scroggo@google.comd5d158b2012-08-14 20:38:28 +0000769 SkASSERT(fBitmapHeap != NULL);
770 int32_t bitmapIndex = fBitmapHeap->insert(bm);
771 if (SkBitmapHeap::INVALID_SLOT == bitmapIndex) {
772 return false;
773 }
scroggo@google.com10dccde2012-08-08 20:43:22 +0000774 this->writeOp(op, flags, bitmapIndex);
scroggo@google.com58be6822012-07-30 14:40:01 +0000775 return true;
776 }
777 return false;
778}
779
780void SkGPipeCanvas::drawBitmap(const SkBitmap& bm, SkScalar left, SkScalar top,
781 const SkPaint* paint) {
782 NOTIFY_SETUP(this);
783 size_t opBytesNeeded = sizeof(SkScalar) * 2;
784
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000785 if (this->commonDrawBitmap(bm, kDrawBitmap_DrawOp, 0, opBytesNeeded, paint)) {
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000786 fWriter.writeScalar(left);
787 fWriter.writeScalar(top);
788 }
reed@google.combb6992a2011-04-26 17:41:56 +0000789}
790
reed@google.com71121732012-09-18 15:14:33 +0000791void SkGPipeCanvas::drawBitmapRectToRect(const SkBitmap& bm, const SkRect* src,
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +0000792 const SkRect& dst, const SkPaint* paint,
793 DrawBitmapRectFlags dbmrFlags) {
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000794 NOTIFY_SETUP(this);
scroggo@google.com58be6822012-07-30 14:40:01 +0000795 size_t opBytesNeeded = sizeof(SkRect);
796 bool hasSrc = src != NULL;
797 unsigned flags;
798 if (hasSrc) {
scroggo@google.com460a23e2012-08-16 17:56:49 +0000799 flags = kDrawBitmap_HasSrcRect_DrawOpFlag;
scroggo@google.com58be6822012-07-30 14:40:01 +0000800 opBytesNeeded += sizeof(int32_t) * 4;
801 } else {
802 flags = 0;
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000803 }
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +0000804 if (dbmrFlags & kBleed_DrawBitmapRectFlag) {
805 flags |= kDrawBitmap_Bleed_DrawOpFlag;
806 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000807
reed@google.com71121732012-09-18 15:14:33 +0000808 if (this->commonDrawBitmap(bm, kDrawBitmapRectToRect_DrawOp, flags, opBytesNeeded, paint)) {
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000809 if (hasSrc) {
reed@google.com71121732012-09-18 15:14:33 +0000810 fWriter.writeRect(*src);
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000811 }
812 fWriter.writeRect(dst);
813 }
reed@google.combb6992a2011-04-26 17:41:56 +0000814}
815
scroggo@google.com58be6822012-07-30 14:40:01 +0000816void SkGPipeCanvas::drawBitmapMatrix(const SkBitmap& bm, const SkMatrix& matrix,
817 const SkPaint* paint) {
818 NOTIFY_SETUP(this);
819 size_t opBytesNeeded = matrix.writeToMemory(NULL);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000820
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000821 if (this->commonDrawBitmap(bm, kDrawBitmapMatrix_DrawOp, 0, opBytesNeeded, paint)) {
scroggo@google.com58be6822012-07-30 14:40:01 +0000822 fWriter.writeMatrix(matrix);
823 }
reed@google.combb6992a2011-04-26 17:41:56 +0000824}
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000825
826void SkGPipeCanvas::drawBitmapNine(const SkBitmap& bm, const SkIRect& center,
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000827 const SkRect& dst, const SkPaint* paint) {
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000828 NOTIFY_SETUP(this);
scroggo@google.com58be6822012-07-30 14:40:01 +0000829 size_t opBytesNeeded = sizeof(int32_t) * 4 + sizeof(SkRect);
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000830
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000831 if (this->commonDrawBitmap(bm, kDrawBitmapNine_DrawOp, 0, opBytesNeeded, paint)) {
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000832 fWriter.write32(center.fLeft);
833 fWriter.write32(center.fTop);
834 fWriter.write32(center.fRight);
835 fWriter.write32(center.fBottom);
836 fWriter.writeRect(dst);
837 }
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000838}
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000839
840void SkGPipeCanvas::drawSprite(const SkBitmap& bm, int left, int top,
841 const SkPaint* paint) {
842 NOTIFY_SETUP(this);
scroggo@google.com58be6822012-07-30 14:40:01 +0000843 size_t opBytesNeeded = sizeof(int32_t) * 2;
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000844
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000845 if (this->commonDrawBitmap(bm, kDrawSprite_DrawOp, 0, opBytesNeeded, paint)) {
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000846 fWriter.write32(left);
847 fWriter.write32(top);
848 }
reed@google.combb6992a2011-04-26 17:41:56 +0000849}
850
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000851void SkGPipeCanvas::drawText(const void* text, size_t byteLength, SkScalar x,
reed@google.combb6992a2011-04-26 17:41:56 +0000852 SkScalar y, 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.comacd471f2011-05-03 21:26:46 +0000856 if (this->needOpBytes(4 + SkAlign4(byteLength) + 2 * sizeof(SkScalar))) {
reed@google.com31891582011-05-12 03:03:56 +0000857 this->writeOp(kDrawText_DrawOp);
reed@google.comacd471f2011-05-03 21:26:46 +0000858 fWriter.write32(byteLength);
859 fWriter.writePad(text, byteLength);
860 fWriter.writeScalar(x);
861 fWriter.writeScalar(y);
862 }
reed@google.combb6992a2011-04-26 17:41:56 +0000863 }
864}
865
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000866void SkGPipeCanvas::drawPosText(const void* text, size_t byteLength,
reed@google.combb6992a2011-04-26 17:41:56 +0000867 const SkPoint pos[], const SkPaint& paint) {
868 if (byteLength) {
reed@google.comacd471f2011-05-03 21:26:46 +0000869 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000870 this->writePaint(paint);
reed@google.combb6992a2011-04-26 17:41:56 +0000871 int count = paint.textToGlyphs(text, byteLength, NULL);
reed@google.comacd471f2011-05-03 21:26:46 +0000872 if (this->needOpBytes(4 + SkAlign4(byteLength) + 4 + count * sizeof(SkPoint))) {
reed@google.com31891582011-05-12 03:03:56 +0000873 this->writeOp(kDrawPosText_DrawOp);
reed@google.comacd471f2011-05-03 21:26:46 +0000874 fWriter.write32(byteLength);
875 fWriter.writePad(text, byteLength);
876 fWriter.write32(count);
877 fWriter.write(pos, count * sizeof(SkPoint));
878 }
reed@google.combb6992a2011-04-26 17:41:56 +0000879 }
880}
881
882void SkGPipeCanvas::drawPosTextH(const void* text, size_t byteLength,
883 const SkScalar xpos[], SkScalar constY,
884 const SkPaint& paint) {
885 if (byteLength) {
reed@google.comacd471f2011-05-03 21:26:46 +0000886 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000887 this->writePaint(paint);
reed@google.combb6992a2011-04-26 17:41:56 +0000888 int count = paint.textToGlyphs(text, byteLength, NULL);
reed@google.comacd471f2011-05-03 21:26:46 +0000889 if (this->needOpBytes(4 + SkAlign4(byteLength) + 4 + count * sizeof(SkScalar) + 4)) {
reed@google.com31891582011-05-12 03:03:56 +0000890 this->writeOp(kDrawPosTextH_DrawOp);
reed@google.comacd471f2011-05-03 21:26:46 +0000891 fWriter.write32(byteLength);
892 fWriter.writePad(text, byteLength);
893 fWriter.write32(count);
894 fWriter.write(xpos, count * sizeof(SkScalar));
895 fWriter.writeScalar(constY);
896 }
reed@google.combb6992a2011-04-26 17:41:56 +0000897 }
898}
899
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000900void SkGPipeCanvas::drawTextOnPath(const void* text, size_t byteLength,
901 const SkPath& path, const SkMatrix* matrix,
reed@google.combb6992a2011-04-26 17:41:56 +0000902 const SkPaint& paint) {
903 if (byteLength) {
reed@google.comacd471f2011-05-03 21:26:46 +0000904 NOTIFY_SETUP(this);
reed@google.combb6992a2011-04-26 17:41:56 +0000905 unsigned flags = 0;
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000906 size_t size = 4 + SkAlign4(byteLength) + path.writeToMemory(NULL);
reed@google.combb6992a2011-04-26 17:41:56 +0000907 if (matrix) {
908 flags |= kDrawTextOnPath_HasMatrix_DrawOpFlag;
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000909 size += matrix->writeToMemory(NULL);
reed@google.combb6992a2011-04-26 17:41:56 +0000910 }
reed@google.com31891582011-05-12 03:03:56 +0000911 this->writePaint(paint);
reed@google.comacd471f2011-05-03 21:26:46 +0000912 if (this->needOpBytes(size)) {
reed@google.com31891582011-05-12 03:03:56 +0000913 this->writeOp(kDrawTextOnPath_DrawOp, flags, 0);
reed@google.combb6992a2011-04-26 17:41:56 +0000914
reed@google.comacd471f2011-05-03 21:26:46 +0000915 fWriter.write32(byteLength);
916 fWriter.writePad(text, byteLength);
reed@google.combb6992a2011-04-26 17:41:56 +0000917
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000918 fWriter.writePath(path);
reed@google.comacd471f2011-05-03 21:26:46 +0000919 if (matrix) {
djsollen@google.com2b2ede32012-04-12 13:24:04 +0000920 fWriter.writeMatrix(*matrix);
reed@google.comacd471f2011-05-03 21:26:46 +0000921 }
reed@google.combb6992a2011-04-26 17:41:56 +0000922 }
923 }
924}
925
926void SkGPipeCanvas::drawPicture(SkPicture& picture) {
reed@google.com0faac1e2011-05-11 05:58:58 +0000927 // we want to playback the picture into individual draw calls
928 this->INHERITED::drawPicture(picture);
reed@google.combb6992a2011-04-26 17:41:56 +0000929}
930
reed@google.combb6992a2011-04-26 17:41:56 +0000931void SkGPipeCanvas::drawVertices(VertexMode mode, int vertexCount,
932 const SkPoint vertices[], const SkPoint texs[],
933 const SkColor colors[], SkXfermode*,
934 const uint16_t indices[], int indexCount,
935 const SkPaint& paint) {
936 if (0 == vertexCount) {
937 return;
938 }
939
reed@google.comacd471f2011-05-03 21:26:46 +0000940 NOTIFY_SETUP(this);
941 size_t size = 4 + vertexCount * sizeof(SkPoint);
reed@google.com31891582011-05-12 03:03:56 +0000942 this->writePaint(paint);
reed@google.combb6992a2011-04-26 17:41:56 +0000943 unsigned flags = 0;
944 if (texs) {
945 flags |= kDrawVertices_HasTexs_DrawOpFlag;
reed@google.comacd471f2011-05-03 21:26:46 +0000946 size += vertexCount * sizeof(SkPoint);
reed@google.combb6992a2011-04-26 17:41:56 +0000947 }
948 if (colors) {
949 flags |= kDrawVertices_HasColors_DrawOpFlag;
reed@google.comacd471f2011-05-03 21:26:46 +0000950 size += vertexCount * sizeof(SkColor);
reed@google.combb6992a2011-04-26 17:41:56 +0000951 }
952 if (indices && indexCount > 0) {
953 flags |= kDrawVertices_HasIndices_DrawOpFlag;
reed@google.comacd471f2011-05-03 21:26:46 +0000954 size += 4 + SkAlign4(indexCount * sizeof(uint16_t));
reed@google.combb6992a2011-04-26 17:41:56 +0000955 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000956
reed@google.comacd471f2011-05-03 21:26:46 +0000957 if (this->needOpBytes(size)) {
reed@google.com31891582011-05-12 03:03:56 +0000958 this->writeOp(kDrawVertices_DrawOp, flags, 0);
reed@google.comacd471f2011-05-03 21:26:46 +0000959 fWriter.write32(mode);
960 fWriter.write32(vertexCount);
961 fWriter.write(vertices, vertexCount * sizeof(SkPoint));
962 if (texs) {
963 fWriter.write(texs, vertexCount * sizeof(SkPoint));
964 }
965 if (colors) {
966 fWriter.write(colors, vertexCount * sizeof(SkColor));
967 }
reed@google.combb6992a2011-04-26 17:41:56 +0000968
reed@google.comacd471f2011-05-03 21:26:46 +0000969 // TODO: flatten xfermode
reed@google.combb6992a2011-04-26 17:41:56 +0000970
reed@google.comacd471f2011-05-03 21:26:46 +0000971 if (indices && indexCount > 0) {
972 fWriter.write32(indexCount);
973 fWriter.writePad(indices, indexCount * sizeof(uint16_t));
974 }
reed@google.combb6992a2011-04-26 17:41:56 +0000975 }
976}
977
reed@google.comacd471f2011-05-03 21:26:46 +0000978void SkGPipeCanvas::drawData(const void* ptr, size_t size) {
979 if (size && ptr) {
980 NOTIFY_SETUP(this);
reed@google.combb6992a2011-04-26 17:41:56 +0000981 unsigned data = 0;
982 if (size < (1 << DRAWOPS_DATA_BITS)) {
983 data = (unsigned)size;
984 }
reed@google.comacd471f2011-05-03 21:26:46 +0000985 if (this->needOpBytes(4 + SkAlign4(size))) {
986 this->writeOp(kDrawData_DrawOp, 0, data);
987 if (0 == data) {
988 fWriter.write32(size);
989 }
reed@google.combb6793b2011-05-05 15:18:15 +0000990 fWriter.writePad(ptr, size);
reed@google.combb6992a2011-04-26 17:41:56 +0000991 }
992 }
993}
994
robertphillips@google.com0a4805e2013-05-29 13:24:23 +0000995void SkGPipeCanvas::beginCommentGroup(const char* description) {
996 // ignore for now
997}
998
999void SkGPipeCanvas::addComment(const char* kywd, const char* value) {
1000 // ignore for now
1001}
1002
1003void SkGPipeCanvas::endCommentGroup() {
1004 // ignore for now
1005}
1006
junov@chromium.org77eec242012-07-18 17:54:45 +00001007void SkGPipeCanvas::flushRecording(bool detachCurrentBlock) {
1008 doNotify();
1009 if (detachCurrentBlock) {
1010 // force a new block to be requested for the next recorded command
rmistry@google.comd6176b02012-08-23 18:14:13 +00001011 fBlockSize = 0;
junov@chromium.org77eec242012-07-18 17:54:45 +00001012 }
1013}
1014
junov@chromium.org2e14ba82012-08-07 14:26:57 +00001015size_t SkGPipeCanvas::freeMemoryIfPossible(size_t bytesToFree) {
scroggo@google.comd5d158b2012-08-14 20:38:28 +00001016 return (NULL == fBitmapHeap) ? 0 : fBitmapHeap->freeMemoryIfPossible(bytesToFree);
junov@chromium.org2e14ba82012-08-07 14:26:57 +00001017}
1018
reed@google.combb6992a2011-04-26 17:41:56 +00001019///////////////////////////////////////////////////////////////////////////////
1020
1021template <typename T> uint32_t castToU32(T value) {
1022 union {
1023 T fSrc;
1024 uint32_t fDst;
1025 } data;
1026 data.fSrc = value;
1027 return data.fDst;
1028}
1029
reed@google.com31891582011-05-12 03:03:56 +00001030void SkGPipeCanvas::writePaint(const SkPaint& paint) {
scroggo@google.comd5d158b2012-08-14 20:38:28 +00001031 if (fDone) {
1032 return;
1033 }
reed@google.com31891582011-05-12 03:03:56 +00001034 SkPaint& base = fPaint;
reed@google.combb6992a2011-04-26 17:41:56 +00001035 uint32_t storage[32];
1036 uint32_t* ptr = storage;
reed@google.combb6992a2011-04-26 17:41:56 +00001037
1038 if (base.getFlags() != paint.getFlags()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001039 *ptr++ = PaintOp_packOpData(kFlags_PaintOp, paint.getFlags());
reed@google.comf5842f72011-05-04 18:30:04 +00001040 base.setFlags(paint.getFlags());
reed@google.combb6992a2011-04-26 17:41:56 +00001041 }
1042 if (base.getColor() != paint.getColor()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001043 *ptr++ = PaintOp_packOp(kColor_PaintOp);
1044 *ptr++ = paint.getColor();
reed@google.comf5842f72011-05-04 18:30:04 +00001045 base.setColor(paint.getColor());
reed@google.combb6992a2011-04-26 17:41:56 +00001046 }
1047 if (base.getStyle() != paint.getStyle()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001048 *ptr++ = PaintOp_packOpData(kStyle_PaintOp, paint.getStyle());
reed@google.comf5842f72011-05-04 18:30:04 +00001049 base.setStyle(paint.getStyle());
reed@google.combb6992a2011-04-26 17:41:56 +00001050 }
1051 if (base.getStrokeJoin() != paint.getStrokeJoin()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001052 *ptr++ = PaintOp_packOpData(kJoin_PaintOp, paint.getStrokeJoin());
reed@google.comf5842f72011-05-04 18:30:04 +00001053 base.setStrokeJoin(paint.getStrokeJoin());
reed@google.combb6992a2011-04-26 17:41:56 +00001054 }
1055 if (base.getStrokeCap() != paint.getStrokeCap()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001056 *ptr++ = PaintOp_packOpData(kCap_PaintOp, paint.getStrokeCap());
reed@google.comf5842f72011-05-04 18:30:04 +00001057 base.setStrokeCap(paint.getStrokeCap());
reed@google.combb6992a2011-04-26 17:41:56 +00001058 }
1059 if (base.getStrokeWidth() != paint.getStrokeWidth()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001060 *ptr++ = PaintOp_packOp(kWidth_PaintOp);
1061 *ptr++ = castToU32(paint.getStrokeWidth());
reed@google.comf5842f72011-05-04 18:30:04 +00001062 base.setStrokeWidth(paint.getStrokeWidth());
reed@google.combb6992a2011-04-26 17:41:56 +00001063 }
1064 if (base.getStrokeMiter() != paint.getStrokeMiter()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001065 *ptr++ = PaintOp_packOp(kMiter_PaintOp);
1066 *ptr++ = castToU32(paint.getStrokeMiter());
reed@google.comf5842f72011-05-04 18:30:04 +00001067 base.setStrokeMiter(paint.getStrokeMiter());
reed@google.combb6992a2011-04-26 17:41:56 +00001068 }
1069 if (base.getTextEncoding() != paint.getTextEncoding()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001070 *ptr++ = PaintOp_packOpData(kEncoding_PaintOp, paint.getTextEncoding());
reed@google.comf5842f72011-05-04 18:30:04 +00001071 base.setTextEncoding(paint.getTextEncoding());
reed@google.combb6992a2011-04-26 17:41:56 +00001072 }
1073 if (base.getHinting() != paint.getHinting()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001074 *ptr++ = PaintOp_packOpData(kHinting_PaintOp, paint.getHinting());
reed@google.comf5842f72011-05-04 18:30:04 +00001075 base.setHinting(paint.getHinting());
reed@google.combb6992a2011-04-26 17:41:56 +00001076 }
1077 if (base.getTextAlign() != paint.getTextAlign()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001078 *ptr++ = PaintOp_packOpData(kAlign_PaintOp, paint.getTextAlign());
reed@google.comf5842f72011-05-04 18:30:04 +00001079 base.setTextAlign(paint.getTextAlign());
reed@google.combb6992a2011-04-26 17:41:56 +00001080 }
1081 if (base.getTextSize() != paint.getTextSize()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001082 *ptr++ = PaintOp_packOp(kTextSize_PaintOp);
1083 *ptr++ = castToU32(paint.getTextSize());
reed@google.comf5842f72011-05-04 18:30:04 +00001084 base.setTextSize(paint.getTextSize());
reed@google.combb6992a2011-04-26 17:41:56 +00001085 }
1086 if (base.getTextScaleX() != paint.getTextScaleX()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001087 *ptr++ = PaintOp_packOp(kTextScaleX_PaintOp);
1088 *ptr++ = castToU32(paint.getTextScaleX());
reed@google.comf5842f72011-05-04 18:30:04 +00001089 base.setTextScaleX(paint.getTextScaleX());
reed@google.combb6992a2011-04-26 17:41:56 +00001090 }
1091 if (base.getTextSkewX() != paint.getTextSkewX()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001092 *ptr++ = PaintOp_packOp(kTextSkewX_PaintOp);
1093 *ptr++ = castToU32(paint.getTextSkewX());
reed@google.comf5842f72011-05-04 18:30:04 +00001094 base.setTextSkewX(paint.getTextSkewX());
1095 }
1096
1097 if (!SkTypeface::Equal(base.getTypeface(), paint.getTypeface())) {
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +00001098 if (isCrossProcess(fFlags)) {
scroggo@google.com3cb969f2012-07-27 20:39:19 +00001099 uint32_t id = this->getTypefaceID(paint.getTypeface());
1100 *ptr++ = PaintOp_packOpData(kTypeface_PaintOp, id);
1101 } else if (this->needOpBytes(sizeof(void*))) {
1102 // Add to the set for ref counting.
1103 fTypefaceSet.add(paint.getTypeface());
1104 // It is safe to write the typeface to the stream before the rest
1105 // of the paint unless we ever send a kReset_PaintOp, which we
1106 // currently never do.
1107 this->writeOp(kSetTypeface_DrawOp);
1108 fWriter.writePtr(paint.getTypeface());
1109 }
reed@google.comf5842f72011-05-04 18:30:04 +00001110 base.setTypeface(paint.getTypeface());
reed@google.combb6992a2011-04-26 17:41:56 +00001111 }
reed@google.combb6992a2011-04-26 17:41:56 +00001112
scroggo@google.com4dffc592012-07-17 16:49:40 +00001113 // This is a new paint, so all old flats can be safely purged, if necessary.
1114 fFlattenableHeap.markAllFlatsSafeToDelete();
reed@google.comb55d1182011-05-11 00:42:04 +00001115 for (int i = 0; i < kCount_PaintFlats; i++) {
1116 int index = this->flattenToIndex(get_paintflat(paint, i), (PaintFlats)i);
scroggo@google.comd3ba5cc2012-07-09 16:05:53 +00001117 bool replaced = index < 0;
1118 if (replaced) {
1119 index = ~index;
1120 }
scroggo@google.com4dffc592012-07-17 16:49:40 +00001121 // Store the index of any flat that needs to be kept. 0 means no flat.
1122 if (index > 0) {
1123 fFlattenableHeap.markFlatForKeeping(index);
1124 }
1125 SkASSERT(index >= 0 && index <= fFlatDictionary.count());
scroggo@google.comd3ba5cc2012-07-09 16:05:53 +00001126 if (index != fCurrFlatIndex[i] || replaced) {
reed@google.comb55d1182011-05-11 00:42:04 +00001127 *ptr++ = PaintOp_packOpFlagData(kFlatIndex_PaintOp, i, index);
1128 fCurrFlatIndex[i] = index;
1129 }
1130 }
1131
reed@google.comacd471f2011-05-03 21:26:46 +00001132 size_t size = (char*)ptr - (char*)storage;
1133 if (size && this->needOpBytes(size)) {
reed@google.comb55d1182011-05-11 00:42:04 +00001134 this->writeOp(kPaintOp_DrawOp, 0, size);
reed@google.comb55d1182011-05-11 00:42:04 +00001135 fWriter.write(storage, size);
reed@google.combb6992a2011-04-26 17:41:56 +00001136 for (size_t i = 0; i < size/4; i++) {
reed@google.comb55d1182011-05-11 00:42:04 +00001137// SkDebugf("[%d] %08X\n", i, storage[i]);
reed@google.combb6992a2011-04-26 17:41:56 +00001138 }
1139 }
reed@google.com0cd2ac62013-10-14 20:02:44 +00001140
1141 //
1142 // Do these after we've written kPaintOp_DrawOp
skia.committer@gmail.comfbc58a32013-10-15 07:02:27 +00001143
reed@google.com0cd2ac62013-10-14 20:02:44 +00001144 if (base.getAnnotation() != paint.getAnnotation()) {
1145 if (NULL == paint.getAnnotation()) {
commit-bot@chromium.org89ff3dd2013-10-29 20:29:38 +00001146 if (this->needOpBytes()) {
1147 this->writeOp(kSetAnnotation_DrawOp, 0, 0);
1148 }
reed@google.com0cd2ac62013-10-14 20:02:44 +00001149 } else {
1150 SkOrderedWriteBuffer buffer(1024);
1151 paint.getAnnotation()->writeToBuffer(buffer);
commit-bot@chromium.org89ff3dd2013-10-29 20:29:38 +00001152 const size_t size = buffer.bytesWritten();
1153 if (this->needOpBytes(size)) {
1154 this->writeOp(kSetAnnotation_DrawOp, 0, size);
1155 buffer.writeToMemory(fWriter.reserve(size));
1156 }
reed@google.com0cd2ac62013-10-14 20:02:44 +00001157 }
commit-bot@chromium.org40258a52013-10-29 19:23:26 +00001158 base.setAnnotation(paint.getAnnotation());
reed@google.com0cd2ac62013-10-14 20:02:44 +00001159 }
reed@google.combb6992a2011-04-26 17:41:56 +00001160}
1161
1162///////////////////////////////////////////////////////////////////////////////
1163
1164#include "SkGPipe.h"
1165
scroggo@google.com3cb969f2012-07-27 20:39:19 +00001166SkGPipeController::~SkGPipeController() {
1167 SkSafeUnref(fCanvas);
1168}
1169
1170void SkGPipeController::setCanvas(SkGPipeCanvas* canvas) {
1171 SkRefCnt_SafeAssign(fCanvas, canvas);
1172}
1173
1174///////////////////////////////////////////////////////////////////////////////
1175
1176SkGPipeWriter::SkGPipeWriter()
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +00001177: fWriter(0) {
reed@google.combb6992a2011-04-26 17:41:56 +00001178 fCanvas = NULL;
1179}
1180
1181SkGPipeWriter::~SkGPipeWriter() {
reed@google.comacd471f2011-05-03 21:26:46 +00001182 this->endRecording();
reed@google.combb6992a2011-04-26 17:41:56 +00001183}
1184
junov@chromium.orga8db8fe2012-08-15 19:49:22 +00001185SkCanvas* SkGPipeWriter::startRecording(SkGPipeController* controller, uint32_t flags,
1186 uint32_t width, uint32_t height) {
reed@google.combb6992a2011-04-26 17:41:56 +00001187 if (NULL == fCanvas) {
reed@google.comacd471f2011-05-03 21:26:46 +00001188 fWriter.reset(NULL, 0);
junov@chromium.orga8db8fe2012-08-15 19:49:22 +00001189 fCanvas = SkNEW_ARGS(SkGPipeCanvas, (controller, &fWriter, flags, width, height));
reed@google.combb6992a2011-04-26 17:41:56 +00001190 }
scroggo@google.com3cb969f2012-07-27 20:39:19 +00001191 controller->setCanvas(fCanvas);
reed@google.combb6992a2011-04-26 17:41:56 +00001192 return fCanvas;
1193}
1194
1195void SkGPipeWriter::endRecording() {
1196 if (fCanvas) {
scroggo@google.com59c3ab62013-11-12 14:32:38 +00001197 fCanvas->finish(true);
reed@google.combb6992a2011-04-26 17:41:56 +00001198 fCanvas->unref();
1199 fCanvas = NULL;
1200 }
1201}
1202
junov@chromium.org2e14ba82012-08-07 14:26:57 +00001203void SkGPipeWriter::flushRecording(bool detachCurrentBlock) {
1204 if (fCanvas) {
1205 fCanvas->flushRecording(detachCurrentBlock);
1206 }
junov@chromium.org77eec242012-07-18 17:54:45 +00001207}
1208
junov@chromium.org2e14ba82012-08-07 14:26:57 +00001209size_t SkGPipeWriter::freeMemoryIfPossible(size_t bytesToFree) {
1210 if (fCanvas) {
1211 return fCanvas->freeMemoryIfPossible(bytesToFree);
1212 }
1213 return 0;
1214}
1215
1216size_t SkGPipeWriter::storageAllocatedForRecording() const {
scroggo@google.com15011ee2012-07-26 20:03:32 +00001217 return NULL == fCanvas ? 0 : fCanvas->storageAllocatedForRecording();
1218}
1219
scroggo@google.com3e26bd02012-08-14 15:20:01 +00001220///////////////////////////////////////////////////////////////////////////////
1221
1222BitmapShuttle::BitmapShuttle(SkGPipeCanvas* canvas) {
1223 SkASSERT(canvas != NULL);
1224 fCanvas = canvas;
1225 fCanvas->ref();
1226}
1227
1228BitmapShuttle::~BitmapShuttle() {
scroggo@google.com59c3ab62013-11-12 14:32:38 +00001229 this->removeCanvas();
scroggo@google.com3e26bd02012-08-14 15:20:01 +00001230}
1231
1232bool BitmapShuttle::insert(const SkBitmap& bitmap, int32_t slot) {
scroggo@google.com59c3ab62013-11-12 14:32:38 +00001233 SkASSERT(fCanvas != NULL);
scroggo@google.com3e26bd02012-08-14 15:20:01 +00001234 return fCanvas->shuttleBitmap(bitmap, slot);
1235}
scroggo@google.com59c3ab62013-11-12 14:32:38 +00001236
1237void BitmapShuttle::removeCanvas() {
1238 if (NULL == fCanvas) {
1239 return;
1240 }
1241 fCanvas->unref();
1242 fCanvas = NULL;
1243}