blob: f14cdacc9b21f03a3492bb505b3726e5a5499d94 [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;
reed@google.com4ed0fb72012-12-12 20:48:18 +0000227 virtual void drawRRect(const SkRRect&, const SkPaint&) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000228 virtual void drawBitmap(const SkBitmap&, SkScalar left, SkScalar top,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000229 const SkPaint*) SK_OVERRIDE;
reed@google.com71121732012-09-18 15:14:33 +0000230 virtual void drawBitmapRectToRect(const SkBitmap&, const SkRect* src,
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000231 const SkRect& dst, const SkPaint* paint,
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +0000232 DrawBitmapRectFlags flags) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000233 virtual void drawBitmapMatrix(const SkBitmap&, const SkMatrix&,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000234 const SkPaint*) SK_OVERRIDE;
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000235 virtual void drawBitmapNine(const SkBitmap& bitmap, const SkIRect& center,
236 const SkRect& dst, const SkPaint* paint = NULL) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000237 virtual void drawSprite(const SkBitmap&, int left, int top,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000238 const SkPaint*) SK_OVERRIDE;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000239 virtual void drawText(const void* text, size_t byteLength, SkScalar x,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000240 SkScalar y, const SkPaint&) SK_OVERRIDE;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000241 virtual void drawPosText(const void* text, size_t byteLength,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000242 const SkPoint pos[], const SkPaint&) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000243 virtual void drawPosTextH(const void* text, size_t byteLength,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000244 const SkScalar xpos[], SkScalar constY,
245 const SkPaint&) SK_OVERRIDE;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000246 virtual void drawTextOnPath(const void* text, size_t byteLength,
247 const SkPath& path, const SkMatrix* matrix,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000248 const SkPaint&) SK_OVERRIDE;
249 virtual void drawPicture(SkPicture& picture) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000250 virtual void drawVertices(VertexMode, int vertexCount,
251 const SkPoint vertices[], const SkPoint texs[],
252 const SkColor colors[], SkXfermode*,
253 const uint16_t indices[], int indexCount,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000254 const SkPaint&) SK_OVERRIDE;
255 virtual void drawData(const void*, size_t) SK_OVERRIDE;
robertphillips@google.com0a4805e2013-05-29 13:24:23 +0000256 virtual void beginCommentGroup(const char* description) SK_OVERRIDE;
257 virtual void addComment(const char* kywd, const char* value) SK_OVERRIDE;
258 virtual void endCommentGroup() SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000259
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000260 /**
261 * Flatten an SkBitmap to send to the reader, where it will be referenced
262 * according to slot.
263 */
264 bool shuttleBitmap(const SkBitmap&, int32_t slot);
bsalomon@google.comad254fe2013-10-22 13:19:12 +0000265
266protected:
267 virtual void onDrawRect(const SkRect& rect, const SkPaint&) SK_OVERRIDE;
268 virtual void onDrawPath(const SkPath& path, const SkPaint&) SK_OVERRIDE;
269
reed@google.combb6992a2011-04-26 17:41:56 +0000270private:
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000271 enum {
272 kNoSaveLayer = -1,
273 };
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000274 SkNamedFactorySet* fFactorySet;
275 int fFirstSaveLayerStackLevel;
scroggo@google.comd9d29672012-08-14 17:21:34 +0000276 SkBitmapHeap* fBitmapHeap;
reed@google.comacd471f2011-05-03 21:26:46 +0000277 SkGPipeController* fController;
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000278 SkWriter32& fWriter;
279 size_t fBlockSize; // amount allocated for writer
280 size_t fBytesNotified;
281 bool fDone;
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000282 const uint32_t fFlags;
reed@google.combb6992a2011-04-26 17:41:56 +0000283
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000284 SkRefCntSet fTypefaceSet;
reed@google.comf5842f72011-05-04 18:30:04 +0000285
286 uint32_t getTypefaceID(SkTypeface*);
287
reed@google.comacd471f2011-05-03 21:26:46 +0000288 inline void writeOp(DrawOps op, unsigned flags, unsigned data) {
reed@google.combb6992a2011-04-26 17:41:56 +0000289 fWriter.write32(DrawOp_packOpFlagData(op, flags, data));
290 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000291
reed@google.comacd471f2011-05-03 21:26:46 +0000292 inline void writeOp(DrawOps op) {
reed@google.combb6992a2011-04-26 17:41:56 +0000293 fWriter.write32(DrawOp_packOpFlagData(op, 0, 0));
294 }
reed@google.comacd471f2011-05-03 21:26:46 +0000295
296 bool needOpBytes(size_t size = 0);
297
298 inline void doNotify() {
299 if (!fDone) {
300 size_t bytes = fWriter.size() - fBytesNotified;
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000301 if (bytes > 0) {
302 fController->notifyWritten(bytes);
303 fBytesNotified += bytes;
304 }
reed@google.comacd471f2011-05-03 21:26:46 +0000305 }
306 }
reed@google.comb55d1182011-05-11 00:42:04 +0000307
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000308 // Should be called after any calls to an SkFlatDictionary::findAndReplace
309 // if a new SkFlatData was added when in cross process mode
310 void flattenFactoryNames();
311
scroggo@google.com4dffc592012-07-17 16:49:40 +0000312 FlattenableHeap fFlattenableHeap;
313 FlatDictionary fFlatDictionary;
reed@google.comb55d1182011-05-11 00:42:04 +0000314 int fCurrFlatIndex[kCount_PaintFlats];
315 int flattenToIndex(SkFlattenable* obj, PaintFlats);
316
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000317 // Common code used by drawBitmap*. Behaves differently depending on the
318 // type of SkBitmapHeap being used, which is determined by the flags used.
319 bool commonDrawBitmap(const SkBitmap& bm, DrawOps op, unsigned flags,
320 size_t opBytesNeeded, const SkPaint* paint);
scroggo@google.com58be6822012-07-30 14:40:01 +0000321
reed@google.com31891582011-05-12 03:03:56 +0000322 SkPaint fPaint;
323 void writePaint(const SkPaint&);
reed@google.combb6992a2011-04-26 17:41:56 +0000324
reed@google.comacd471f2011-05-03 21:26:46 +0000325 class AutoPipeNotify {
326 public:
327 AutoPipeNotify(SkGPipeCanvas* canvas) : fCanvas(canvas) {}
328 ~AutoPipeNotify() { fCanvas->doNotify(); }
329 private:
330 SkGPipeCanvas* fCanvas;
331 };
332 friend class AutoPipeNotify;
333
reed@google.combb6992a2011-04-26 17:41:56 +0000334 typedef SkCanvas INHERITED;
335};
336
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000337void SkGPipeCanvas::flattenFactoryNames() {
338 const char* name;
339 while ((name = fFactorySet->getNextAddedFactoryName()) != NULL) {
340 size_t len = strlen(name);
341 if (this->needOpBytes(len)) {
342 this->writeOp(kDef_Factory_DrawOp);
343 fWriter.writeString(name, len);
344 }
345 }
346}
347
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000348bool SkGPipeCanvas::shuttleBitmap(const SkBitmap& bm, int32_t slot) {
scroggo@google.com565254b2012-06-28 15:41:32 +0000349 SkASSERT(shouldFlattenBitmaps(fFlags));
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000350 SkOrderedWriteBuffer buffer(1024);
351 buffer.setNamedFactoryRecorder(fFactorySet);
scroggo@google.com74b7ffd2013-04-30 02:32:41 +0000352 buffer.writeBitmap(bm);
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000353 this->flattenFactoryNames();
354 uint32_t size = buffer.size();
355 if (this->needOpBytes(size)) {
356 this->writeOp(kDef_Bitmap_DrawOp, 0, slot);
357 void* dst = static_cast<void*>(fWriter.reserve(size));
358 buffer.writeToMemory(dst);
359 return true;
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000360 }
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000361 return false;
scroggo@google.comd3ba5cc2012-07-09 16:05:53 +0000362}
363
reed@google.comb55d1182011-05-11 00:42:04 +0000364// return 0 for NULL (or unflattenable obj), or index-base-1
scroggo@google.comd3ba5cc2012-07-09 16:05:53 +0000365// return ~(index-base-1) if an old flattenable was replaced
reed@google.comb55d1182011-05-11 00:42:04 +0000366int SkGPipeCanvas::flattenToIndex(SkFlattenable* obj, PaintFlats paintflat) {
scroggo@google.comd5d158b2012-08-14 20:38:28 +0000367 SkASSERT(!fDone && fBitmapHeap != NULL);
reed@google.comb55d1182011-05-11 00:42:04 +0000368 if (NULL == obj) {
369 return 0;
370 }
reed@google.comb55d1182011-05-11 00:42:04 +0000371
scroggo@google.comd9d29672012-08-14 17:21:34 +0000372 fBitmapHeap->deferAddingOwners();
scroggo@google.com4dffc592012-07-17 16:49:40 +0000373 bool added, replaced;
scroggo@google.com664fab12012-08-14 19:22:05 +0000374 const SkFlatData* flat = fFlatDictionary.findAndReplace(*obj, fFlattenableHeap.flatToReplace(),
375 &added, &replaced);
scroggo@google.comd9d29672012-08-14 17:21:34 +0000376 fBitmapHeap->endAddingOwnersDeferral(added);
scroggo@google.com4dffc592012-07-17 16:49:40 +0000377 int index = flat->index();
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000378 if (added) {
379 if (isCrossProcess(fFlags)) {
380 this->flattenFactoryNames();
381 }
382 size_t flatSize = flat->flatSize();
383 if (this->needOpBytes(flatSize)) {
384 this->writeOp(kDef_Flattenable_DrawOp, paintflat, index);
385 fWriter.write(flat->data(), flatSize);
386 }
scroggo@google.com4dffc592012-07-17 16:49:40 +0000387 }
388 if (replaced) {
reed@google.comb55d1182011-05-11 00:42:04 +0000389 index = ~index;
reed@google.comb55d1182011-05-11 00:42:04 +0000390 }
scroggo@google.com4dffc592012-07-17 16:49:40 +0000391 return index;
reed@google.comb55d1182011-05-11 00:42:04 +0000392}
393
reed@google.combb6992a2011-04-26 17:41:56 +0000394///////////////////////////////////////////////////////////////////////////////
395
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000396/**
397 * If SkBitmaps are to be flattened to send to the reader, this class is
398 * provided to the SkBitmapHeap to tell the SkGPipeCanvas to do so.
399 */
400class BitmapShuttle : public SkBitmapHeap::ExternalStorage {
401public:
402 BitmapShuttle(SkGPipeCanvas*);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000403
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000404 ~BitmapShuttle();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000405
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000406 virtual bool insert(const SkBitmap& bitmap, int32_t slot) SK_OVERRIDE;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000407
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000408private:
409 SkGPipeCanvas* fCanvas;
410};
411
412///////////////////////////////////////////////////////////////////////////////
413
reed@google.comacd471f2011-05-03 21:26:46 +0000414#define MIN_BLOCK_SIZE (16 * 1024)
scroggo@google.com4dffc592012-07-17 16:49:40 +0000415#define BITMAPS_TO_KEEP 5
416#define FLATTENABLES_TO_KEEP 10
reed@google.combb6992a2011-04-26 17:41:56 +0000417
reed@google.comacd471f2011-05-03 21:26:46 +0000418SkGPipeCanvas::SkGPipeCanvas(SkGPipeController* controller,
rmistry@google.comd6176b02012-08-23 18:14:13 +0000419 SkWriter32* writer, uint32_t flags,
junov@chromium.orga8db8fe2012-08-15 19:49:22 +0000420 uint32_t width, uint32_t height)
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000421: fFactorySet(isCrossProcess(flags) ? SkNEW(SkNamedFactorySet) : NULL)
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000422, fWriter(*writer)
423, fFlags(flags)
scroggo@google.com664fab12012-08-14 19:22:05 +0000424, fFlattenableHeap(FLATTENABLES_TO_KEEP, fFactorySet, isCrossProcess(flags))
scroggo@google.com15543602012-08-02 18:49:49 +0000425, fFlatDictionary(&fFlattenableHeap) {
reed@google.comacd471f2011-05-03 21:26:46 +0000426 fController = controller;
reed@google.combb6992a2011-04-26 17:41:56 +0000427 fDone = false;
reed@google.comacd471f2011-05-03 21:26:46 +0000428 fBlockSize = 0; // need first block from controller
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000429 fBytesNotified = 0;
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000430 fFirstSaveLayerStackLevel = kNoSaveLayer;
reed@google.comb55d1182011-05-11 00:42:04 +0000431 sk_bzero(fCurrFlatIndex, sizeof(fCurrFlatIndex));
reed@google.comacd471f2011-05-03 21:26:46 +0000432
reed@google.combb6793b2011-05-05 15:18:15 +0000433 // we need a device to limit our clip
yangsu@google.com06b4da12011-06-17 15:04:40 +0000434 // We don't allocate pixels for the bitmap
reed@google.combb6793b2011-05-05 15:18:15 +0000435 SkBitmap bitmap;
junov@chromium.orga8db8fe2012-08-15 19:49:22 +0000436 bitmap.setConfig(SkBitmap::kARGB_8888_Config, width, height);
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000437 SkBaseDevice* device = SkNEW_ARGS(SkBitmapDevice, (bitmap));
reed@google.combb6793b2011-05-05 15:18:15 +0000438 this->setDevice(device)->unref();
scroggo@google.com10dccde2012-08-08 20:43:22 +0000439
scroggo@google.com565254b2012-06-28 15:41:32 +0000440 // Tell the reader the appropriate flags to use.
441 if (this->needOpBytes()) {
442 this->writeOp(kReportFlags_DrawOp, fFlags, 0);
443 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000444
scroggo@google.com10dccde2012-08-08 20:43:22 +0000445 if (shouldFlattenBitmaps(flags)) {
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000446 BitmapShuttle* shuttle = SkNEW_ARGS(BitmapShuttle, (this));
scroggo@google.comd9d29672012-08-14 17:21:34 +0000447 fBitmapHeap = SkNEW_ARGS(SkBitmapHeap, (shuttle, BITMAPS_TO_KEEP));
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000448 shuttle->unref();
scroggo@google.com10dccde2012-08-08 20:43:22 +0000449 } else {
scroggo@google.comd9d29672012-08-14 17:21:34 +0000450 fBitmapHeap = SkNEW_ARGS(SkBitmapHeap,
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000451 (BITMAPS_TO_KEEP, controller->numberOfReaders()));
scroggo@google.com10dccde2012-08-08 20:43:22 +0000452 if (this->needOpBytes(sizeof(void*))) {
scroggo@google.comd9d29672012-08-14 17:21:34 +0000453 this->writeOp(kShareBitmapHeap_DrawOp);
454 fWriter.writePtr(static_cast<void*>(fBitmapHeap));
scroggo@google.com10dccde2012-08-08 20:43:22 +0000455 }
456 }
scroggo@google.comd9d29672012-08-14 17:21:34 +0000457 fFlattenableHeap.setBitmapStorage(fBitmapHeap);
scroggo@google.com10dccde2012-08-08 20:43:22 +0000458 this->doNotify();
reed@google.combb6992a2011-04-26 17:41:56 +0000459}
460
461SkGPipeCanvas::~SkGPipeCanvas() {
462 this->finish();
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000463 SkSafeUnref(fFactorySet);
scroggo@google.comd9d29672012-08-14 17:21:34 +0000464 SkSafeUnref(fBitmapHeap);
reed@google.combb6992a2011-04-26 17:41:56 +0000465}
466
reed@google.comacd471f2011-05-03 21:26:46 +0000467bool SkGPipeCanvas::needOpBytes(size_t needed) {
468 if (fDone) {
469 return false;
470 }
471
472 needed += 4; // size of DrawOp atom
473 if (fWriter.size() + needed > fBlockSize) {
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000474 // Before we wipe out any data that has already been written, read it
475 // out.
476 this->doNotify();
477 size_t blockSize = SkMax32(MIN_BLOCK_SIZE, needed);
478 void* block = fController->requestBlock(blockSize, &fBlockSize);
reed@google.comacd471f2011-05-03 21:26:46 +0000479 if (NULL == block) {
480 fDone = true;
481 return false;
482 }
scroggo@google.com460a23e2012-08-16 17:56:49 +0000483 SkASSERT(SkIsAlign4(fBlockSize));
reed@google.comacd471f2011-05-03 21:26:46 +0000484 fWriter.reset(block, fBlockSize);
485 fBytesNotified = 0;
486 }
487 return true;
488}
489
reed@google.comf5842f72011-05-04 18:30:04 +0000490uint32_t SkGPipeCanvas::getTypefaceID(SkTypeface* face) {
491 uint32_t id = 0; // 0 means default/null typeface
492 if (face) {
493 id = fTypefaceSet.find(face);
494 if (0 == id) {
495 id = fTypefaceSet.add(face);
496 size_t size = writeTypeface(NULL, face);
497 if (this->needOpBytes(size)) {
reed@google.combb6793b2011-05-05 15:18:15 +0000498 this->writeOp(kDef_Typeface_DrawOp);
reed@google.comf5842f72011-05-04 18:30:04 +0000499 writeTypeface(&fWriter, face);
500 }
501 }
502 }
503 return id;
504}
505
reed@google.combb6992a2011-04-26 17:41:56 +0000506///////////////////////////////////////////////////////////////////////////////
507
reed@google.comacd471f2011-05-03 21:26:46 +0000508#define NOTIFY_SETUP(canvas) \
509 AutoPipeNotify apn(canvas)
510
reed@google.combb6992a2011-04-26 17:41:56 +0000511int SkGPipeCanvas::save(SaveFlags flags) {
reed@google.comacd471f2011-05-03 21:26:46 +0000512 NOTIFY_SETUP(this);
513 if (this->needOpBytes()) {
514 this->writeOp(kSave_DrawOp, 0, flags);
515 }
reed@google.combb6992a2011-04-26 17:41:56 +0000516 return this->INHERITED::save(flags);
517}
518
519int SkGPipeCanvas::saveLayer(const SkRect* bounds, const SkPaint* paint,
520 SaveFlags saveFlags) {
reed@google.comacd471f2011-05-03 21:26:46 +0000521 NOTIFY_SETUP(this);
522 size_t size = 0;
reed@google.combb6992a2011-04-26 17:41:56 +0000523 unsigned opFlags = 0;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000524
reed@google.combb6992a2011-04-26 17:41:56 +0000525 if (bounds) {
526 opFlags |= kSaveLayer_HasBounds_DrawOpFlag;
reed@google.comacd471f2011-05-03 21:26:46 +0000527 size += sizeof(SkRect);
reed@google.combb6992a2011-04-26 17:41:56 +0000528 }
529 if (paint) {
530 opFlags |= kSaveLayer_HasPaint_DrawOpFlag;
reed@google.com31891582011-05-12 03:03:56 +0000531 this->writePaint(*paint);
reed@google.combb6992a2011-04-26 17:41:56 +0000532 }
533
reed@google.comacd471f2011-05-03 21:26:46 +0000534 if (this->needOpBytes(size)) {
535 this->writeOp(kSaveLayer_DrawOp, opFlags, saveFlags);
536 if (bounds) {
537 fWriter.writeRect(*bounds);
538 }
reed@google.combb6992a2011-04-26 17:41:56 +0000539 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000540
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000541 if (kNoSaveLayer == fFirstSaveLayerStackLevel){
542 fFirstSaveLayerStackLevel = this->getSaveCount();
543 }
reed@google.combb6992a2011-04-26 17:41:56 +0000544 // we just pass on the save, so we don't create a layer
545 return this->INHERITED::save(saveFlags);
546}
547
548void SkGPipeCanvas::restore() {
reed@google.comacd471f2011-05-03 21:26:46 +0000549 NOTIFY_SETUP(this);
550 if (this->needOpBytes()) {
551 this->writeOp(kRestore_DrawOp);
552 }
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000553
reed@google.combb6992a2011-04-26 17:41:56 +0000554 this->INHERITED::restore();
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000555
556 if (this->getSaveCount() == fFirstSaveLayerStackLevel){
557 fFirstSaveLayerStackLevel = kNoSaveLayer;
558 }
559}
560
561bool SkGPipeCanvas::isDrawingToLayer() const {
562 return kNoSaveLayer != fFirstSaveLayerStackLevel;
reed@google.combb6992a2011-04-26 17:41:56 +0000563}
564
565bool SkGPipeCanvas::translate(SkScalar dx, SkScalar dy) {
566 if (dx || dy) {
reed@google.comacd471f2011-05-03 21:26:46 +0000567 NOTIFY_SETUP(this);
568 if (this->needOpBytes(2 * sizeof(SkScalar))) {
569 this->writeOp(kTranslate_DrawOp);
570 fWriter.writeScalar(dx);
571 fWriter.writeScalar(dy);
572 }
reed@google.combb6992a2011-04-26 17:41:56 +0000573 }
574 return this->INHERITED::translate(dx, dy);
575}
576
577bool SkGPipeCanvas::scale(SkScalar sx, SkScalar sy) {
578 if (sx || sy) {
reed@google.comacd471f2011-05-03 21:26:46 +0000579 NOTIFY_SETUP(this);
580 if (this->needOpBytes(2 * sizeof(SkScalar))) {
581 this->writeOp(kScale_DrawOp);
582 fWriter.writeScalar(sx);
583 fWriter.writeScalar(sy);
584 }
reed@google.combb6992a2011-04-26 17:41:56 +0000585 }
586 return this->INHERITED::scale(sx, sy);
587}
588
589bool SkGPipeCanvas::rotate(SkScalar degrees) {
590 if (degrees) {
reed@google.comacd471f2011-05-03 21:26:46 +0000591 NOTIFY_SETUP(this);
592 if (this->needOpBytes(sizeof(SkScalar))) {
593 this->writeOp(kRotate_DrawOp);
594 fWriter.writeScalar(degrees);
595 }
reed@google.combb6992a2011-04-26 17:41:56 +0000596 }
597 return this->INHERITED::rotate(degrees);
598}
599
600bool SkGPipeCanvas::skew(SkScalar sx, SkScalar sy) {
601 if (sx || sy) {
reed@google.comacd471f2011-05-03 21:26:46 +0000602 NOTIFY_SETUP(this);
603 if (this->needOpBytes(2 * sizeof(SkScalar))) {
604 this->writeOp(kSkew_DrawOp);
605 fWriter.writeScalar(sx);
606 fWriter.writeScalar(sy);
607 }
reed@google.combb6992a2011-04-26 17:41:56 +0000608 }
609 return this->INHERITED::skew(sx, sy);
610}
611
612bool SkGPipeCanvas::concat(const SkMatrix& matrix) {
613 if (!matrix.isIdentity()) {
reed@google.comacd471f2011-05-03 21:26:46 +0000614 NOTIFY_SETUP(this);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000615 if (this->needOpBytes(matrix.writeToMemory(NULL))) {
reed@google.comacd471f2011-05-03 21:26:46 +0000616 this->writeOp(kConcat_DrawOp);
djsollen@google.com2b2ede32012-04-12 13:24:04 +0000617 fWriter.writeMatrix(matrix);
reed@google.comacd471f2011-05-03 21:26:46 +0000618 }
reed@google.combb6992a2011-04-26 17:41:56 +0000619 }
620 return this->INHERITED::concat(matrix);
621}
622
623void SkGPipeCanvas::setMatrix(const SkMatrix& matrix) {
reed@google.comacd471f2011-05-03 21:26:46 +0000624 NOTIFY_SETUP(this);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000625 if (this->needOpBytes(matrix.writeToMemory(NULL))) {
reed@google.comacd471f2011-05-03 21:26:46 +0000626 this->writeOp(kSetMatrix_DrawOp);
djsollen@google.com2b2ede32012-04-12 13:24:04 +0000627 fWriter.writeMatrix(matrix);
reed@google.comacd471f2011-05-03 21:26:46 +0000628 }
reed@google.combb6992a2011-04-26 17:41:56 +0000629 this->INHERITED::setMatrix(matrix);
630}
631
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000632bool SkGPipeCanvas::clipRect(const SkRect& rect, SkRegion::Op rgnOp,
633 bool doAntiAlias) {
reed@google.comacd471f2011-05-03 21:26:46 +0000634 NOTIFY_SETUP(this);
scroggo@google.com460a23e2012-08-16 17:56:49 +0000635 if (this->needOpBytes(sizeof(SkRect))) {
636 unsigned flags = doAntiAlias & kClip_HasAntiAlias_DrawOpFlag;
637 this->writeOp(kClipRect_DrawOp, flags, rgnOp);
reed@google.comacd471f2011-05-03 21:26:46 +0000638 fWriter.writeRect(rect);
639 }
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000640 return this->INHERITED::clipRect(rect, rgnOp, doAntiAlias);
reed@google.combb6992a2011-04-26 17:41:56 +0000641}
642
reed@google.com4ed0fb72012-12-12 20:48:18 +0000643bool SkGPipeCanvas::clipRRect(const SkRRect& rrect, SkRegion::Op rgnOp,
644 bool doAntiAlias) {
645 NOTIFY_SETUP(this);
646 if (this->needOpBytes(kSizeOfFlatRRect)) {
647 unsigned flags = doAntiAlias & kClip_HasAntiAlias_DrawOpFlag;
648 this->writeOp(kClipRRect_DrawOp, flags, rgnOp);
649 fWriter.writeRRect(rrect);
650 }
651 return this->INHERITED::clipRRect(rrect, rgnOp, doAntiAlias);
652}
653
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000654bool SkGPipeCanvas::clipPath(const SkPath& path, SkRegion::Op rgnOp,
655 bool doAntiAlias) {
reed@google.comacd471f2011-05-03 21:26:46 +0000656 NOTIFY_SETUP(this);
scroggo@google.com460a23e2012-08-16 17:56:49 +0000657 if (this->needOpBytes(path.writeToMemory(NULL))) {
658 unsigned flags = doAntiAlias & kClip_HasAntiAlias_DrawOpFlag;
659 this->writeOp(kClipPath_DrawOp, flags, rgnOp);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000660 fWriter.writePath(path);
reed@google.comacd471f2011-05-03 21:26:46 +0000661 }
reed@google.combb6992a2011-04-26 17:41:56 +0000662 // we just pass on the bounds of the path
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000663 return this->INHERITED::clipRect(path.getBounds(), rgnOp, doAntiAlias);
reed@google.combb6992a2011-04-26 17:41:56 +0000664}
665
666bool SkGPipeCanvas::clipRegion(const SkRegion& region, SkRegion::Op rgnOp) {
reed@google.comacd471f2011-05-03 21:26:46 +0000667 NOTIFY_SETUP(this);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000668 if (this->needOpBytes(region.writeToMemory(NULL))) {
reed@google.comacd471f2011-05-03 21:26:46 +0000669 this->writeOp(kClipRegion_DrawOp, 0, rgnOp);
djsollen@google.com2b2ede32012-04-12 13:24:04 +0000670 fWriter.writeRegion(region);
reed@google.comacd471f2011-05-03 21:26:46 +0000671 }
reed@google.combb6992a2011-04-26 17:41:56 +0000672 return this->INHERITED::clipRegion(region, rgnOp);
673}
674
675///////////////////////////////////////////////////////////////////////////////
676
677void SkGPipeCanvas::clear(SkColor color) {
reed@google.comacd471f2011-05-03 21:26:46 +0000678 NOTIFY_SETUP(this);
reed@google.combb6992a2011-04-26 17:41:56 +0000679 unsigned flags = 0;
680 if (color) {
681 flags |= kClear_HasColor_DrawOpFlag;
682 }
reed@google.comacd471f2011-05-03 21:26:46 +0000683 if (this->needOpBytes(sizeof(SkColor))) {
684 this->writeOp(kDrawClear_DrawOp, flags, 0);
685 if (color) {
686 fWriter.write32(color);
687 }
reed@google.combb6992a2011-04-26 17:41:56 +0000688 }
689}
690
691void SkGPipeCanvas::drawPaint(const SkPaint& paint) {
reed@google.comacd471f2011-05-03 21:26:46 +0000692 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000693 this->writePaint(paint);
reed@google.comacd471f2011-05-03 21:26:46 +0000694 if (this->needOpBytes()) {
reed@google.com31891582011-05-12 03:03:56 +0000695 this->writeOp(kDrawPaint_DrawOp);
reed@google.comacd471f2011-05-03 21:26:46 +0000696 }
reed@google.combb6992a2011-04-26 17:41:56 +0000697}
698
699void SkGPipeCanvas::drawPoints(PointMode mode, size_t count,
robertphillips@google.com8b169312013-10-15 17:47:36 +0000700 const SkPoint pts[], const SkPaint& paint) {
reed@google.combb6992a2011-04-26 17:41:56 +0000701 if (count) {
reed@google.comacd471f2011-05-03 21:26:46 +0000702 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000703 this->writePaint(paint);
reed@google.comacd471f2011-05-03 21:26:46 +0000704 if (this->needOpBytes(4 + count * sizeof(SkPoint))) {
reed@google.com31891582011-05-12 03:03:56 +0000705 this->writeOp(kDrawPoints_DrawOp, mode, 0);
reed@google.comacd471f2011-05-03 21:26:46 +0000706 fWriter.write32(count);
707 fWriter.write(pts, count * sizeof(SkPoint));
708 }
reed@google.combb6992a2011-04-26 17:41:56 +0000709 }
710}
711
reed@google.com4ed0fb72012-12-12 20:48:18 +0000712void SkGPipeCanvas::drawOval(const SkRect& rect, const SkPaint& paint) {
713 NOTIFY_SETUP(this);
714 this->writePaint(paint);
715 if (this->needOpBytes(sizeof(SkRect))) {
716 this->writeOp(kDrawOval_DrawOp);
717 fWriter.writeRect(rect);
718 }
719}
720
bsalomon@google.comad254fe2013-10-22 13:19:12 +0000721void SkGPipeCanvas::onDrawRect(const SkRect& rect, const SkPaint& paint) {
reed@google.comacd471f2011-05-03 21:26:46 +0000722 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000723 this->writePaint(paint);
reed@google.comacd471f2011-05-03 21:26:46 +0000724 if (this->needOpBytes(sizeof(SkRect))) {
reed@google.com31891582011-05-12 03:03:56 +0000725 this->writeOp(kDrawRect_DrawOp);
reed@google.comacd471f2011-05-03 21:26:46 +0000726 fWriter.writeRect(rect);
727 }
reed@google.combb6992a2011-04-26 17:41:56 +0000728}
729
reed@google.com4ed0fb72012-12-12 20:48:18 +0000730void SkGPipeCanvas::drawRRect(const SkRRect& rrect, const SkPaint& paint) {
731 NOTIFY_SETUP(this);
732 this->writePaint(paint);
733 if (this->needOpBytes(kSizeOfFlatRRect)) {
734 this->writeOp(kDrawRRect_DrawOp);
735 fWriter.writeRRect(rrect);
736 }
737}
738
bsalomon@google.comad254fe2013-10-22 13:19:12 +0000739void SkGPipeCanvas::onDrawPath(const SkPath& path, const SkPaint& paint) {
reed@google.comacd471f2011-05-03 21:26:46 +0000740 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000741 this->writePaint(paint);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000742 if (this->needOpBytes(path.writeToMemory(NULL))) {
reed@google.com31891582011-05-12 03:03:56 +0000743 this->writeOp(kDrawPath_DrawOp);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000744 fWriter.writePath(path);
reed@google.comacd471f2011-05-03 21:26:46 +0000745 }
reed@google.combb6992a2011-04-26 17:41:56 +0000746}
747
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000748bool SkGPipeCanvas::commonDrawBitmap(const SkBitmap& bm, DrawOps op,
749 unsigned flags,
750 size_t opBytesNeeded,
751 const SkPaint* paint) {
scroggo@google.com58be6822012-07-30 14:40:01 +0000752 if (paint != NULL) {
scroggo@google.com460a23e2012-08-16 17:56:49 +0000753 flags |= kDrawBitmap_HasPaint_DrawOpFlag;
scroggo@google.com58be6822012-07-30 14:40:01 +0000754 this->writePaint(*paint);
755 }
scroggo@google.com10dccde2012-08-08 20:43:22 +0000756 if (this->needOpBytes(opBytesNeeded)) {
scroggo@google.comd5d158b2012-08-14 20:38:28 +0000757 SkASSERT(fBitmapHeap != NULL);
758 int32_t bitmapIndex = fBitmapHeap->insert(bm);
759 if (SkBitmapHeap::INVALID_SLOT == bitmapIndex) {
760 return false;
761 }
scroggo@google.com10dccde2012-08-08 20:43:22 +0000762 this->writeOp(op, flags, bitmapIndex);
scroggo@google.com58be6822012-07-30 14:40:01 +0000763 return true;
764 }
765 return false;
766}
767
768void SkGPipeCanvas::drawBitmap(const SkBitmap& bm, SkScalar left, SkScalar top,
769 const SkPaint* paint) {
770 NOTIFY_SETUP(this);
771 size_t opBytesNeeded = sizeof(SkScalar) * 2;
772
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000773 if (this->commonDrawBitmap(bm, kDrawBitmap_DrawOp, 0, opBytesNeeded, paint)) {
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000774 fWriter.writeScalar(left);
775 fWriter.writeScalar(top);
776 }
reed@google.combb6992a2011-04-26 17:41:56 +0000777}
778
reed@google.com71121732012-09-18 15:14:33 +0000779void SkGPipeCanvas::drawBitmapRectToRect(const SkBitmap& bm, const SkRect* src,
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +0000780 const SkRect& dst, const SkPaint* paint,
781 DrawBitmapRectFlags dbmrFlags) {
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000782 NOTIFY_SETUP(this);
scroggo@google.com58be6822012-07-30 14:40:01 +0000783 size_t opBytesNeeded = sizeof(SkRect);
784 bool hasSrc = src != NULL;
785 unsigned flags;
786 if (hasSrc) {
scroggo@google.com460a23e2012-08-16 17:56:49 +0000787 flags = kDrawBitmap_HasSrcRect_DrawOpFlag;
scroggo@google.com58be6822012-07-30 14:40:01 +0000788 opBytesNeeded += sizeof(int32_t) * 4;
789 } else {
790 flags = 0;
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000791 }
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +0000792 if (dbmrFlags & kBleed_DrawBitmapRectFlag) {
793 flags |= kDrawBitmap_Bleed_DrawOpFlag;
794 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000795
reed@google.com71121732012-09-18 15:14:33 +0000796 if (this->commonDrawBitmap(bm, kDrawBitmapRectToRect_DrawOp, flags, opBytesNeeded, paint)) {
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000797 if (hasSrc) {
reed@google.com71121732012-09-18 15:14:33 +0000798 fWriter.writeRect(*src);
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000799 }
800 fWriter.writeRect(dst);
801 }
reed@google.combb6992a2011-04-26 17:41:56 +0000802}
803
scroggo@google.com58be6822012-07-30 14:40:01 +0000804void SkGPipeCanvas::drawBitmapMatrix(const SkBitmap& bm, const SkMatrix& matrix,
805 const SkPaint* paint) {
806 NOTIFY_SETUP(this);
807 size_t opBytesNeeded = matrix.writeToMemory(NULL);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000808
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000809 if (this->commonDrawBitmap(bm, kDrawBitmapMatrix_DrawOp, 0, opBytesNeeded, paint)) {
scroggo@google.com58be6822012-07-30 14:40:01 +0000810 fWriter.writeMatrix(matrix);
811 }
reed@google.combb6992a2011-04-26 17:41:56 +0000812}
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000813
814void SkGPipeCanvas::drawBitmapNine(const SkBitmap& bm, const SkIRect& center,
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000815 const SkRect& dst, const SkPaint* paint) {
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000816 NOTIFY_SETUP(this);
scroggo@google.com58be6822012-07-30 14:40:01 +0000817 size_t opBytesNeeded = sizeof(int32_t) * 4 + sizeof(SkRect);
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000818
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000819 if (this->commonDrawBitmap(bm, kDrawBitmapNine_DrawOp, 0, opBytesNeeded, paint)) {
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000820 fWriter.write32(center.fLeft);
821 fWriter.write32(center.fTop);
822 fWriter.write32(center.fRight);
823 fWriter.write32(center.fBottom);
824 fWriter.writeRect(dst);
825 }
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000826}
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000827
828void SkGPipeCanvas::drawSprite(const SkBitmap& bm, int left, int top,
829 const SkPaint* paint) {
830 NOTIFY_SETUP(this);
scroggo@google.com58be6822012-07-30 14:40:01 +0000831 size_t opBytesNeeded = sizeof(int32_t) * 2;
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000832
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000833 if (this->commonDrawBitmap(bm, kDrawSprite_DrawOp, 0, opBytesNeeded, paint)) {
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000834 fWriter.write32(left);
835 fWriter.write32(top);
836 }
reed@google.combb6992a2011-04-26 17:41:56 +0000837}
838
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000839void SkGPipeCanvas::drawText(const void* text, size_t byteLength, SkScalar x,
reed@google.combb6992a2011-04-26 17:41:56 +0000840 SkScalar y, const SkPaint& paint) {
841 if (byteLength) {
reed@google.comacd471f2011-05-03 21:26:46 +0000842 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000843 this->writePaint(paint);
reed@google.comacd471f2011-05-03 21:26:46 +0000844 if (this->needOpBytes(4 + SkAlign4(byteLength) + 2 * sizeof(SkScalar))) {
reed@google.com31891582011-05-12 03:03:56 +0000845 this->writeOp(kDrawText_DrawOp);
reed@google.comacd471f2011-05-03 21:26:46 +0000846 fWriter.write32(byteLength);
847 fWriter.writePad(text, byteLength);
848 fWriter.writeScalar(x);
849 fWriter.writeScalar(y);
850 }
reed@google.combb6992a2011-04-26 17:41:56 +0000851 }
852}
853
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000854void SkGPipeCanvas::drawPosText(const void* text, size_t byteLength,
reed@google.combb6992a2011-04-26 17:41:56 +0000855 const SkPoint pos[], const SkPaint& paint) {
856 if (byteLength) {
reed@google.comacd471f2011-05-03 21:26:46 +0000857 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000858 this->writePaint(paint);
reed@google.combb6992a2011-04-26 17:41:56 +0000859 int count = paint.textToGlyphs(text, byteLength, NULL);
reed@google.comacd471f2011-05-03 21:26:46 +0000860 if (this->needOpBytes(4 + SkAlign4(byteLength) + 4 + count * sizeof(SkPoint))) {
reed@google.com31891582011-05-12 03:03:56 +0000861 this->writeOp(kDrawPosText_DrawOp);
reed@google.comacd471f2011-05-03 21:26:46 +0000862 fWriter.write32(byteLength);
863 fWriter.writePad(text, byteLength);
864 fWriter.write32(count);
865 fWriter.write(pos, count * sizeof(SkPoint));
866 }
reed@google.combb6992a2011-04-26 17:41:56 +0000867 }
868}
869
870void SkGPipeCanvas::drawPosTextH(const void* text, size_t byteLength,
871 const SkScalar xpos[], SkScalar constY,
872 const SkPaint& paint) {
873 if (byteLength) {
reed@google.comacd471f2011-05-03 21:26:46 +0000874 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000875 this->writePaint(paint);
reed@google.combb6992a2011-04-26 17:41:56 +0000876 int count = paint.textToGlyphs(text, byteLength, NULL);
reed@google.comacd471f2011-05-03 21:26:46 +0000877 if (this->needOpBytes(4 + SkAlign4(byteLength) + 4 + count * sizeof(SkScalar) + 4)) {
reed@google.com31891582011-05-12 03:03:56 +0000878 this->writeOp(kDrawPosTextH_DrawOp);
reed@google.comacd471f2011-05-03 21:26:46 +0000879 fWriter.write32(byteLength);
880 fWriter.writePad(text, byteLength);
881 fWriter.write32(count);
882 fWriter.write(xpos, count * sizeof(SkScalar));
883 fWriter.writeScalar(constY);
884 }
reed@google.combb6992a2011-04-26 17:41:56 +0000885 }
886}
887
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000888void SkGPipeCanvas::drawTextOnPath(const void* text, size_t byteLength,
889 const SkPath& path, const SkMatrix* matrix,
reed@google.combb6992a2011-04-26 17:41:56 +0000890 const SkPaint& paint) {
891 if (byteLength) {
reed@google.comacd471f2011-05-03 21:26:46 +0000892 NOTIFY_SETUP(this);
reed@google.combb6992a2011-04-26 17:41:56 +0000893 unsigned flags = 0;
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000894 size_t size = 4 + SkAlign4(byteLength) + path.writeToMemory(NULL);
reed@google.combb6992a2011-04-26 17:41:56 +0000895 if (matrix) {
896 flags |= kDrawTextOnPath_HasMatrix_DrawOpFlag;
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000897 size += matrix->writeToMemory(NULL);
reed@google.combb6992a2011-04-26 17:41:56 +0000898 }
reed@google.com31891582011-05-12 03:03:56 +0000899 this->writePaint(paint);
reed@google.comacd471f2011-05-03 21:26:46 +0000900 if (this->needOpBytes(size)) {
reed@google.com31891582011-05-12 03:03:56 +0000901 this->writeOp(kDrawTextOnPath_DrawOp, flags, 0);
reed@google.combb6992a2011-04-26 17:41:56 +0000902
reed@google.comacd471f2011-05-03 21:26:46 +0000903 fWriter.write32(byteLength);
904 fWriter.writePad(text, byteLength);
reed@google.combb6992a2011-04-26 17:41:56 +0000905
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000906 fWriter.writePath(path);
reed@google.comacd471f2011-05-03 21:26:46 +0000907 if (matrix) {
djsollen@google.com2b2ede32012-04-12 13:24:04 +0000908 fWriter.writeMatrix(*matrix);
reed@google.comacd471f2011-05-03 21:26:46 +0000909 }
reed@google.combb6992a2011-04-26 17:41:56 +0000910 }
911 }
912}
913
914void SkGPipeCanvas::drawPicture(SkPicture& picture) {
reed@google.com0faac1e2011-05-11 05:58:58 +0000915 // we want to playback the picture into individual draw calls
916 this->INHERITED::drawPicture(picture);
reed@google.combb6992a2011-04-26 17:41:56 +0000917}
918
reed@google.combb6992a2011-04-26 17:41:56 +0000919void SkGPipeCanvas::drawVertices(VertexMode mode, int vertexCount,
920 const SkPoint vertices[], const SkPoint texs[],
921 const SkColor colors[], SkXfermode*,
922 const uint16_t indices[], int indexCount,
923 const SkPaint& paint) {
924 if (0 == vertexCount) {
925 return;
926 }
927
reed@google.comacd471f2011-05-03 21:26:46 +0000928 NOTIFY_SETUP(this);
929 size_t size = 4 + vertexCount * sizeof(SkPoint);
reed@google.com31891582011-05-12 03:03:56 +0000930 this->writePaint(paint);
reed@google.combb6992a2011-04-26 17:41:56 +0000931 unsigned flags = 0;
932 if (texs) {
933 flags |= kDrawVertices_HasTexs_DrawOpFlag;
reed@google.comacd471f2011-05-03 21:26:46 +0000934 size += vertexCount * sizeof(SkPoint);
reed@google.combb6992a2011-04-26 17:41:56 +0000935 }
936 if (colors) {
937 flags |= kDrawVertices_HasColors_DrawOpFlag;
reed@google.comacd471f2011-05-03 21:26:46 +0000938 size += vertexCount * sizeof(SkColor);
reed@google.combb6992a2011-04-26 17:41:56 +0000939 }
940 if (indices && indexCount > 0) {
941 flags |= kDrawVertices_HasIndices_DrawOpFlag;
reed@google.comacd471f2011-05-03 21:26:46 +0000942 size += 4 + SkAlign4(indexCount * sizeof(uint16_t));
reed@google.combb6992a2011-04-26 17:41:56 +0000943 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000944
reed@google.comacd471f2011-05-03 21:26:46 +0000945 if (this->needOpBytes(size)) {
reed@google.com31891582011-05-12 03:03:56 +0000946 this->writeOp(kDrawVertices_DrawOp, flags, 0);
reed@google.comacd471f2011-05-03 21:26:46 +0000947 fWriter.write32(mode);
948 fWriter.write32(vertexCount);
949 fWriter.write(vertices, vertexCount * sizeof(SkPoint));
950 if (texs) {
951 fWriter.write(texs, vertexCount * sizeof(SkPoint));
952 }
953 if (colors) {
954 fWriter.write(colors, vertexCount * sizeof(SkColor));
955 }
reed@google.combb6992a2011-04-26 17:41:56 +0000956
reed@google.comacd471f2011-05-03 21:26:46 +0000957 // TODO: flatten xfermode
reed@google.combb6992a2011-04-26 17:41:56 +0000958
reed@google.comacd471f2011-05-03 21:26:46 +0000959 if (indices && indexCount > 0) {
960 fWriter.write32(indexCount);
961 fWriter.writePad(indices, indexCount * sizeof(uint16_t));
962 }
reed@google.combb6992a2011-04-26 17:41:56 +0000963 }
964}
965
reed@google.comacd471f2011-05-03 21:26:46 +0000966void SkGPipeCanvas::drawData(const void* ptr, size_t size) {
967 if (size && ptr) {
968 NOTIFY_SETUP(this);
reed@google.combb6992a2011-04-26 17:41:56 +0000969 unsigned data = 0;
970 if (size < (1 << DRAWOPS_DATA_BITS)) {
971 data = (unsigned)size;
972 }
reed@google.comacd471f2011-05-03 21:26:46 +0000973 if (this->needOpBytes(4 + SkAlign4(size))) {
974 this->writeOp(kDrawData_DrawOp, 0, data);
975 if (0 == data) {
976 fWriter.write32(size);
977 }
reed@google.combb6793b2011-05-05 15:18:15 +0000978 fWriter.writePad(ptr, size);
reed@google.combb6992a2011-04-26 17:41:56 +0000979 }
980 }
981}
982
robertphillips@google.com0a4805e2013-05-29 13:24:23 +0000983void SkGPipeCanvas::beginCommentGroup(const char* description) {
984 // ignore for now
985}
986
987void SkGPipeCanvas::addComment(const char* kywd, const char* value) {
988 // ignore for now
989}
990
991void SkGPipeCanvas::endCommentGroup() {
992 // ignore for now
993}
994
junov@chromium.org77eec242012-07-18 17:54:45 +0000995void SkGPipeCanvas::flushRecording(bool detachCurrentBlock) {
996 doNotify();
997 if (detachCurrentBlock) {
998 // force a new block to be requested for the next recorded command
rmistry@google.comd6176b02012-08-23 18:14:13 +0000999 fBlockSize = 0;
junov@chromium.org77eec242012-07-18 17:54:45 +00001000 }
1001}
1002
junov@chromium.org2e14ba82012-08-07 14:26:57 +00001003size_t SkGPipeCanvas::freeMemoryIfPossible(size_t bytesToFree) {
scroggo@google.comd5d158b2012-08-14 20:38:28 +00001004 return (NULL == fBitmapHeap) ? 0 : fBitmapHeap->freeMemoryIfPossible(bytesToFree);
junov@chromium.org2e14ba82012-08-07 14:26:57 +00001005}
1006
reed@google.combb6992a2011-04-26 17:41:56 +00001007///////////////////////////////////////////////////////////////////////////////
1008
1009template <typename T> uint32_t castToU32(T value) {
1010 union {
1011 T fSrc;
1012 uint32_t fDst;
1013 } data;
1014 data.fSrc = value;
1015 return data.fDst;
1016}
1017
reed@google.com31891582011-05-12 03:03:56 +00001018void SkGPipeCanvas::writePaint(const SkPaint& paint) {
scroggo@google.comd5d158b2012-08-14 20:38:28 +00001019 if (fDone) {
1020 return;
1021 }
reed@google.com31891582011-05-12 03:03:56 +00001022 SkPaint& base = fPaint;
reed@google.combb6992a2011-04-26 17:41:56 +00001023 uint32_t storage[32];
1024 uint32_t* ptr = storage;
reed@google.combb6992a2011-04-26 17:41:56 +00001025
1026 if (base.getFlags() != paint.getFlags()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001027 *ptr++ = PaintOp_packOpData(kFlags_PaintOp, paint.getFlags());
reed@google.comf5842f72011-05-04 18:30:04 +00001028 base.setFlags(paint.getFlags());
reed@google.combb6992a2011-04-26 17:41:56 +00001029 }
1030 if (base.getColor() != paint.getColor()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001031 *ptr++ = PaintOp_packOp(kColor_PaintOp);
1032 *ptr++ = paint.getColor();
reed@google.comf5842f72011-05-04 18:30:04 +00001033 base.setColor(paint.getColor());
reed@google.combb6992a2011-04-26 17:41:56 +00001034 }
1035 if (base.getStyle() != paint.getStyle()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001036 *ptr++ = PaintOp_packOpData(kStyle_PaintOp, paint.getStyle());
reed@google.comf5842f72011-05-04 18:30:04 +00001037 base.setStyle(paint.getStyle());
reed@google.combb6992a2011-04-26 17:41:56 +00001038 }
1039 if (base.getStrokeJoin() != paint.getStrokeJoin()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001040 *ptr++ = PaintOp_packOpData(kJoin_PaintOp, paint.getStrokeJoin());
reed@google.comf5842f72011-05-04 18:30:04 +00001041 base.setStrokeJoin(paint.getStrokeJoin());
reed@google.combb6992a2011-04-26 17:41:56 +00001042 }
1043 if (base.getStrokeCap() != paint.getStrokeCap()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001044 *ptr++ = PaintOp_packOpData(kCap_PaintOp, paint.getStrokeCap());
reed@google.comf5842f72011-05-04 18:30:04 +00001045 base.setStrokeCap(paint.getStrokeCap());
reed@google.combb6992a2011-04-26 17:41:56 +00001046 }
1047 if (base.getStrokeWidth() != paint.getStrokeWidth()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001048 *ptr++ = PaintOp_packOp(kWidth_PaintOp);
1049 *ptr++ = castToU32(paint.getStrokeWidth());
reed@google.comf5842f72011-05-04 18:30:04 +00001050 base.setStrokeWidth(paint.getStrokeWidth());
reed@google.combb6992a2011-04-26 17:41:56 +00001051 }
1052 if (base.getStrokeMiter() != paint.getStrokeMiter()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001053 *ptr++ = PaintOp_packOp(kMiter_PaintOp);
1054 *ptr++ = castToU32(paint.getStrokeMiter());
reed@google.comf5842f72011-05-04 18:30:04 +00001055 base.setStrokeMiter(paint.getStrokeMiter());
reed@google.combb6992a2011-04-26 17:41:56 +00001056 }
1057 if (base.getTextEncoding() != paint.getTextEncoding()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001058 *ptr++ = PaintOp_packOpData(kEncoding_PaintOp, paint.getTextEncoding());
reed@google.comf5842f72011-05-04 18:30:04 +00001059 base.setTextEncoding(paint.getTextEncoding());
reed@google.combb6992a2011-04-26 17:41:56 +00001060 }
1061 if (base.getHinting() != paint.getHinting()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001062 *ptr++ = PaintOp_packOpData(kHinting_PaintOp, paint.getHinting());
reed@google.comf5842f72011-05-04 18:30:04 +00001063 base.setHinting(paint.getHinting());
reed@google.combb6992a2011-04-26 17:41:56 +00001064 }
1065 if (base.getTextAlign() != paint.getTextAlign()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001066 *ptr++ = PaintOp_packOpData(kAlign_PaintOp, paint.getTextAlign());
reed@google.comf5842f72011-05-04 18:30:04 +00001067 base.setTextAlign(paint.getTextAlign());
reed@google.combb6992a2011-04-26 17:41:56 +00001068 }
1069 if (base.getTextSize() != paint.getTextSize()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001070 *ptr++ = PaintOp_packOp(kTextSize_PaintOp);
1071 *ptr++ = castToU32(paint.getTextSize());
reed@google.comf5842f72011-05-04 18:30:04 +00001072 base.setTextSize(paint.getTextSize());
reed@google.combb6992a2011-04-26 17:41:56 +00001073 }
1074 if (base.getTextScaleX() != paint.getTextScaleX()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001075 *ptr++ = PaintOp_packOp(kTextScaleX_PaintOp);
1076 *ptr++ = castToU32(paint.getTextScaleX());
reed@google.comf5842f72011-05-04 18:30:04 +00001077 base.setTextScaleX(paint.getTextScaleX());
reed@google.combb6992a2011-04-26 17:41:56 +00001078 }
1079 if (base.getTextSkewX() != paint.getTextSkewX()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001080 *ptr++ = PaintOp_packOp(kTextSkewX_PaintOp);
1081 *ptr++ = castToU32(paint.getTextSkewX());
reed@google.comf5842f72011-05-04 18:30:04 +00001082 base.setTextSkewX(paint.getTextSkewX());
1083 }
1084
1085 if (!SkTypeface::Equal(base.getTypeface(), paint.getTypeface())) {
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +00001086 if (isCrossProcess(fFlags)) {
scroggo@google.com3cb969f2012-07-27 20:39:19 +00001087 uint32_t id = this->getTypefaceID(paint.getTypeface());
1088 *ptr++ = PaintOp_packOpData(kTypeface_PaintOp, id);
1089 } else if (this->needOpBytes(sizeof(void*))) {
1090 // Add to the set for ref counting.
1091 fTypefaceSet.add(paint.getTypeface());
1092 // It is safe to write the typeface to the stream before the rest
1093 // of the paint unless we ever send a kReset_PaintOp, which we
1094 // currently never do.
1095 this->writeOp(kSetTypeface_DrawOp);
1096 fWriter.writePtr(paint.getTypeface());
1097 }
reed@google.comf5842f72011-05-04 18:30:04 +00001098 base.setTypeface(paint.getTypeface());
reed@google.combb6992a2011-04-26 17:41:56 +00001099 }
reed@google.combb6992a2011-04-26 17:41:56 +00001100
scroggo@google.com4dffc592012-07-17 16:49:40 +00001101 // This is a new paint, so all old flats can be safely purged, if necessary.
1102 fFlattenableHeap.markAllFlatsSafeToDelete();
reed@google.comb55d1182011-05-11 00:42:04 +00001103 for (int i = 0; i < kCount_PaintFlats; i++) {
1104 int index = this->flattenToIndex(get_paintflat(paint, i), (PaintFlats)i);
scroggo@google.comd3ba5cc2012-07-09 16:05:53 +00001105 bool replaced = index < 0;
1106 if (replaced) {
1107 index = ~index;
1108 }
scroggo@google.com4dffc592012-07-17 16:49:40 +00001109 // Store the index of any flat that needs to be kept. 0 means no flat.
1110 if (index > 0) {
1111 fFlattenableHeap.markFlatForKeeping(index);
1112 }
1113 SkASSERT(index >= 0 && index <= fFlatDictionary.count());
scroggo@google.comd3ba5cc2012-07-09 16:05:53 +00001114 if (index != fCurrFlatIndex[i] || replaced) {
reed@google.comb55d1182011-05-11 00:42:04 +00001115 *ptr++ = PaintOp_packOpFlagData(kFlatIndex_PaintOp, i, index);
1116 fCurrFlatIndex[i] = index;
1117 }
1118 }
1119
reed@google.comacd471f2011-05-03 21:26:46 +00001120 size_t size = (char*)ptr - (char*)storage;
1121 if (size && this->needOpBytes(size)) {
reed@google.comb55d1182011-05-11 00:42:04 +00001122 this->writeOp(kPaintOp_DrawOp, 0, size);
reed@google.comb55d1182011-05-11 00:42:04 +00001123 fWriter.write(storage, size);
reed@google.combb6992a2011-04-26 17:41:56 +00001124 for (size_t i = 0; i < size/4; i++) {
reed@google.comb55d1182011-05-11 00:42:04 +00001125// SkDebugf("[%d] %08X\n", i, storage[i]);
reed@google.combb6992a2011-04-26 17:41:56 +00001126 }
1127 }
reed@google.com0cd2ac62013-10-14 20:02:44 +00001128
1129 //
1130 // Do these after we've written kPaintOp_DrawOp
skia.committer@gmail.comfbc58a32013-10-15 07:02:27 +00001131
reed@google.com0cd2ac62013-10-14 20:02:44 +00001132 if (base.getAnnotation() != paint.getAnnotation()) {
1133 if (NULL == paint.getAnnotation()) {
1134 this->writeOp(kSetAnnotation_DrawOp, 0, 0);
1135 } else {
1136 SkOrderedWriteBuffer buffer(1024);
1137 paint.getAnnotation()->writeToBuffer(buffer);
1138 size = buffer.bytesWritten();
skia.committer@gmail.comfbc58a32013-10-15 07:02:27 +00001139
reed@google.com0cd2ac62013-10-14 20:02:44 +00001140 SkAutoMalloc storage(size);
1141 buffer.writeToMemory(storage.get());
skia.committer@gmail.comfbc58a32013-10-15 07:02:27 +00001142
reed@google.com0cd2ac62013-10-14 20:02:44 +00001143 this->writeOp(kSetAnnotation_DrawOp, 0, 1);
1144 fWriter.write32(size);
1145 fWriter.write(storage.get(), size);
1146 }
1147 }
reed@google.combb6992a2011-04-26 17:41:56 +00001148}
1149
1150///////////////////////////////////////////////////////////////////////////////
1151
1152#include "SkGPipe.h"
1153
scroggo@google.com3cb969f2012-07-27 20:39:19 +00001154SkGPipeController::~SkGPipeController() {
1155 SkSafeUnref(fCanvas);
1156}
1157
1158void SkGPipeController::setCanvas(SkGPipeCanvas* canvas) {
1159 SkRefCnt_SafeAssign(fCanvas, canvas);
1160}
1161
1162///////////////////////////////////////////////////////////////////////////////
1163
1164SkGPipeWriter::SkGPipeWriter()
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +00001165: fWriter(0) {
reed@google.combb6992a2011-04-26 17:41:56 +00001166 fCanvas = NULL;
1167}
1168
1169SkGPipeWriter::~SkGPipeWriter() {
reed@google.comacd471f2011-05-03 21:26:46 +00001170 this->endRecording();
reed@google.combb6992a2011-04-26 17:41:56 +00001171}
1172
junov@chromium.orga8db8fe2012-08-15 19:49:22 +00001173SkCanvas* SkGPipeWriter::startRecording(SkGPipeController* controller, uint32_t flags,
1174 uint32_t width, uint32_t height) {
reed@google.combb6992a2011-04-26 17:41:56 +00001175 if (NULL == fCanvas) {
reed@google.comacd471f2011-05-03 21:26:46 +00001176 fWriter.reset(NULL, 0);
junov@chromium.orga8db8fe2012-08-15 19:49:22 +00001177 fCanvas = SkNEW_ARGS(SkGPipeCanvas, (controller, &fWriter, flags, width, height));
reed@google.combb6992a2011-04-26 17:41:56 +00001178 }
scroggo@google.com3cb969f2012-07-27 20:39:19 +00001179 controller->setCanvas(fCanvas);
reed@google.combb6992a2011-04-26 17:41:56 +00001180 return fCanvas;
1181}
1182
1183void SkGPipeWriter::endRecording() {
1184 if (fCanvas) {
1185 fCanvas->finish();
1186 fCanvas->unref();
1187 fCanvas = NULL;
1188 }
1189}
1190
junov@chromium.org2e14ba82012-08-07 14:26:57 +00001191void SkGPipeWriter::flushRecording(bool detachCurrentBlock) {
1192 if (fCanvas) {
1193 fCanvas->flushRecording(detachCurrentBlock);
1194 }
junov@chromium.org77eec242012-07-18 17:54:45 +00001195}
1196
junov@chromium.org2e14ba82012-08-07 14:26:57 +00001197size_t SkGPipeWriter::freeMemoryIfPossible(size_t bytesToFree) {
1198 if (fCanvas) {
1199 return fCanvas->freeMemoryIfPossible(bytesToFree);
1200 }
1201 return 0;
1202}
1203
1204size_t SkGPipeWriter::storageAllocatedForRecording() const {
scroggo@google.com15011ee2012-07-26 20:03:32 +00001205 return NULL == fCanvas ? 0 : fCanvas->storageAllocatedForRecording();
1206}
1207
scroggo@google.com3e26bd02012-08-14 15:20:01 +00001208///////////////////////////////////////////////////////////////////////////////
1209
1210BitmapShuttle::BitmapShuttle(SkGPipeCanvas* canvas) {
1211 SkASSERT(canvas != NULL);
1212 fCanvas = canvas;
1213 fCanvas->ref();
1214}
1215
1216BitmapShuttle::~BitmapShuttle() {
1217 fCanvas->unref();
1218}
1219
1220bool BitmapShuttle::insert(const SkBitmap& bitmap, int32_t slot) {
1221 return fCanvas->shuttleBitmap(bitmap, slot);
1222}