blob: 43209f644b9e64743d02f02e1e24800fe3959e55 [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"
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000020#include "SkWriteBuffer.h"
scroggo@google.com10dccde2012-08-08 20:43:22 +000021#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)
commit-bot@chromium.orga2bd2d12014-01-30 22:16:32 +000074 : INHERITED(isCrossProcess ? SkWriteBuffer::kCrossProcess_Flag : 0)
75 , fNumFlatsToKeep(numFlatsToKeep) {
scroggo@google.com664fab12012-08-14 19:22:05 +000076 SkASSERT((isCrossProcess && fset != NULL) || (!isCrossProcess && NULL == fset));
77 if (isCrossProcess) {
78 this->setNamedFactorySet(fset);
scroggo@google.com664fab12012-08-14 19:22:05 +000079 }
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;
commit-bot@chromium.orga2bd2d12014-01-30 22:16:32 +0000112
113 typedef SkFlatController INHERITED;
scroggo@google.com4dffc592012-07-17 16:49:40 +0000114};
115
116void FlattenableHeap::unalloc(void* ptr) {
117 int indexToRemove = fPointers.rfind(ptr);
118 if (indexToRemove >= 0) {
119 sk_free(ptr);
120 fPointers.remove(indexToRemove);
121 }
122}
123
124void* FlattenableHeap::allocThrow(size_t bytes) {
125 void* ptr = sk_malloc_throw(bytes);
126 *fPointers.append() = ptr;
127 return ptr;
128}
129
130const SkFlatData* FlattenableHeap::flatToReplace() const {
131 // First, determine whether we should replace one.
132 if (fPointers.count() > fNumFlatsToKeep) {
133 // Look through the flattenable heap.
134 // TODO: Return the LRU flat.
135 for (int i = 0; i < fPointers.count(); i++) {
136 SkFlatData* potential = (SkFlatData*)fPointers[i];
137 // Make sure that it is not one that must be kept.
138 bool mustKeep = false;
139 for (int j = 0; j < fFlatsThatMustBeKept.count(); j++) {
140 if (potential->index() == fFlatsThatMustBeKept[j]) {
141 mustKeep = true;
142 break;
143 }
144 }
145 if (!mustKeep) {
146 return potential;
147 }
148 }
149 }
150 return NULL;
151}
152
153///////////////////////////////////////////////////////////////////////////////
154
commit-bot@chromium.org07adb632014-01-02 22:20:49 +0000155struct SkFlattenableTraits {
commit-bot@chromium.org186c0cc2014-02-18 16:15:05 +0000156 static void Flatten(SkWriteBuffer& buffer, const SkFlattenable& flattenable) {
commit-bot@chromium.org07adb632014-01-02 22:20:49 +0000157 buffer.writeFlattenable(&flattenable);
scroggo@google.com4dffc592012-07-17 16:49:40 +0000158 }
commit-bot@chromium.org07adb632014-01-02 22:20:49 +0000159 // No need to define unflatten if we never call it.
scroggo@google.com4dffc592012-07-17 16:49:40 +0000160};
commit-bot@chromium.org07adb632014-01-02 22:20:49 +0000161typedef SkFlatDictionary<SkFlattenable, SkFlattenableTraits> FlatDictionary;
scroggo@google.com4dffc592012-07-17 16:49:40 +0000162
163///////////////////////////////////////////////////////////////////////////////
164
scroggo@google.com59c3ab62013-11-12 14:32:38 +0000165/**
166 * If SkBitmaps are to be flattened to send to the reader, this class is
167 * provided to the SkBitmapHeap to tell the SkGPipeCanvas to do so.
168 */
169class BitmapShuttle : public SkBitmapHeap::ExternalStorage {
170public:
171 BitmapShuttle(SkGPipeCanvas*);
172
173 ~BitmapShuttle();
174
175 virtual bool insert(const SkBitmap& bitmap, int32_t slot) SK_OVERRIDE;
176
177 /**
178 * Remove the SkGPipeCanvas used for insertion. After this, calls to
179 * insert will crash.
180 */
181 void removeCanvas();
182
183private:
184 SkGPipeCanvas* fCanvas;
185};
186
187///////////////////////////////////////////////////////////////////////////////
188
reed@google.combb6992a2011-04-26 17:41:56 +0000189class SkGPipeCanvas : public SkCanvas {
190public:
junov@chromium.orga8db8fe2012-08-15 19:49:22 +0000191 SkGPipeCanvas(SkGPipeController*, SkWriter32*, uint32_t flags,
192 uint32_t width, uint32_t height);
reed@google.combb6992a2011-04-26 17:41:56 +0000193 virtual ~SkGPipeCanvas();
194
scroggo@google.com59c3ab62013-11-12 14:32:38 +0000195 /**
196 * Called when nothing else is to be written to the stream. Any repeated
197 * calls are ignored.
198 *
199 * @param notifyReaders Whether to send a message to the reader(s) that
200 * the writer is through sending commands. Should generally be true,
201 * unless there is an error which prevents further messages from
202 * being sent.
203 */
204 void finish(bool notifyReaders) {
205 if (fDone) {
206 return;
reed@google.combb6992a2011-04-26 17:41:56 +0000207 }
scroggo@google.com59c3ab62013-11-12 14:32:38 +0000208 if (notifyReaders && this->needOpBytes()) {
209 this->writeOp(kDone_DrawOp);
210 this->doNotify();
211 }
212 if (shouldFlattenBitmaps(fFlags)) {
213 // The following circular references exist:
214 // fFlattenableHeap -> fWriteBuffer -> fBitmapStorage -> fExternalStorage -> fCanvas
215 // fBitmapHeap -> fExternalStorage -> fCanvas
216 // fFlattenableHeap -> fBitmapStorage -> fExternalStorage -> fCanvas
217
218 // Break them all by destroying the final link to this SkGPipeCanvas.
219 fBitmapShuttle->removeCanvas();
220 }
221 fDone = true;
reed@google.combb6992a2011-04-26 17:41:56 +0000222 }
223
junov@chromium.org77eec242012-07-18 17:54:45 +0000224 void flushRecording(bool detachCurrentBlock);
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000225 size_t freeMemoryIfPossible(size_t bytesToFree);
junov@chromium.org77eec242012-07-18 17:54:45 +0000226
scroggo@google.com15011ee2012-07-26 20:03:32 +0000227 size_t storageAllocatedForRecording() {
scroggo@google.comd5d158b2012-08-14 20:38:28 +0000228 return (NULL == fBitmapHeap) ? 0 : fBitmapHeap->bytesAllocated();
scroggo@google.com15011ee2012-07-26 20:03:32 +0000229 }
230
reed@google.combb6992a2011-04-26 17:41:56 +0000231 // overrides from SkCanvas
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000232 virtual int save(SaveFlags) SK_OVERRIDE;
233 virtual int saveLayer(const SkRect* bounds, const SkPaint*,
234 SaveFlags) SK_OVERRIDE;
235 virtual void restore() SK_OVERRIDE;
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000236 virtual bool isDrawingToLayer() const SK_OVERRIDE;
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000237 virtual bool translate(SkScalar dx, SkScalar dy) SK_OVERRIDE;
238 virtual bool scale(SkScalar sx, SkScalar sy) SK_OVERRIDE;
239 virtual bool rotate(SkScalar degrees) SK_OVERRIDE;
240 virtual bool skew(SkScalar sx, SkScalar sy) SK_OVERRIDE;
241 virtual bool concat(const SkMatrix& matrix) SK_OVERRIDE;
242 virtual void setMatrix(const SkMatrix& matrix) SK_OVERRIDE;
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000243 virtual void clear(SkColor) SK_OVERRIDE;
244 virtual void drawPaint(const SkPaint& paint) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000245 virtual void drawPoints(PointMode, size_t count, const SkPoint pts[],
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000246 const SkPaint&) SK_OVERRIDE;
reed@google.com4ed0fb72012-12-12 20:48:18 +0000247 virtual void drawOval(const SkRect&, const SkPaint&) SK_OVERRIDE;
bsalomon@google.com7ce564c2013-10-22 16:54:15 +0000248 virtual void drawRect(const SkRect& rect, const SkPaint&) SK_OVERRIDE;
reed@google.com4ed0fb72012-12-12 20:48:18 +0000249 virtual void drawRRect(const SkRRect&, const SkPaint&) SK_OVERRIDE;
bsalomon@google.com7ce564c2013-10-22 16:54:15 +0000250 virtual void drawPath(const SkPath& path, const SkPaint&) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000251 virtual void drawBitmap(const SkBitmap&, SkScalar left, SkScalar top,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000252 const SkPaint*) SK_OVERRIDE;
reed@google.com71121732012-09-18 15:14:33 +0000253 virtual void drawBitmapRectToRect(const SkBitmap&, const SkRect* src,
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000254 const SkRect& dst, const SkPaint* paint,
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +0000255 DrawBitmapRectFlags flags) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000256 virtual void drawBitmapMatrix(const SkBitmap&, const SkMatrix&,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000257 const SkPaint*) SK_OVERRIDE;
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000258 virtual void drawBitmapNine(const SkBitmap& bitmap, const SkIRect& center,
259 const SkRect& dst, const SkPaint* paint = NULL) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000260 virtual void drawSprite(const SkBitmap&, int left, int top,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000261 const SkPaint*) SK_OVERRIDE;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000262 virtual void drawText(const void* text, size_t byteLength, SkScalar x,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000263 SkScalar y, const SkPaint&) SK_OVERRIDE;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000264 virtual void drawPosText(const void* text, size_t byteLength,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000265 const SkPoint pos[], const SkPaint&) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000266 virtual void drawPosTextH(const void* text, size_t byteLength,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000267 const SkScalar xpos[], SkScalar constY,
268 const SkPaint&) SK_OVERRIDE;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000269 virtual void drawTextOnPath(const void* text, size_t byteLength,
270 const SkPath& path, const SkMatrix* matrix,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000271 const SkPaint&) SK_OVERRIDE;
272 virtual void drawPicture(SkPicture& picture) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000273 virtual void drawVertices(VertexMode, int vertexCount,
274 const SkPoint vertices[], const SkPoint texs[],
275 const SkColor colors[], SkXfermode*,
276 const uint16_t indices[], int indexCount,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000277 const SkPaint&) SK_OVERRIDE;
278 virtual void drawData(const void*, size_t) SK_OVERRIDE;
robertphillips@google.com0a4805e2013-05-29 13:24:23 +0000279 virtual void beginCommentGroup(const char* description) SK_OVERRIDE;
280 virtual void addComment(const char* kywd, const char* value) SK_OVERRIDE;
281 virtual void endCommentGroup() SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000282
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000283 /**
284 * Flatten an SkBitmap to send to the reader, where it will be referenced
285 * according to slot.
286 */
287 bool shuttleBitmap(const SkBitmap&, int32_t slot);
commit-bot@chromium.orged9806f2014-02-21 02:32:36 +0000288
289protected:
290 virtual void onDrawDRRect(const SkRRect&, const SkRRect&, const SkPaint&) SK_OVERRIDE;
291
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000292 virtual void onClipRect(const SkRect&, SkRegion::Op, ClipEdgeStyle) SK_OVERRIDE;
293 virtual void onClipRRect(const SkRRect&, SkRegion::Op, ClipEdgeStyle) SK_OVERRIDE;
294 virtual void onClipPath(const SkPath&, SkRegion::Op, ClipEdgeStyle) SK_OVERRIDE;
295 virtual void onClipRegion(const SkRegion&, SkRegion::Op) SK_OVERRIDE;
296
reed@google.combb6992a2011-04-26 17:41:56 +0000297private:
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000298 enum {
299 kNoSaveLayer = -1,
300 };
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000301 SkNamedFactorySet* fFactorySet;
302 int fFirstSaveLayerStackLevel;
scroggo@google.comd9d29672012-08-14 17:21:34 +0000303 SkBitmapHeap* fBitmapHeap;
reed@google.comacd471f2011-05-03 21:26:46 +0000304 SkGPipeController* fController;
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000305 SkWriter32& fWriter;
306 size_t fBlockSize; // amount allocated for writer
307 size_t fBytesNotified;
308 bool fDone;
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000309 const uint32_t fFlags;
reed@google.combb6992a2011-04-26 17:41:56 +0000310
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000311 SkRefCntSet fTypefaceSet;
reed@google.comf5842f72011-05-04 18:30:04 +0000312
313 uint32_t getTypefaceID(SkTypeface*);
314
reed@google.comacd471f2011-05-03 21:26:46 +0000315 inline void writeOp(DrawOps op, unsigned flags, unsigned data) {
reed@google.combb6992a2011-04-26 17:41:56 +0000316 fWriter.write32(DrawOp_packOpFlagData(op, flags, data));
317 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000318
reed@google.comacd471f2011-05-03 21:26:46 +0000319 inline void writeOp(DrawOps op) {
reed@google.combb6992a2011-04-26 17:41:56 +0000320 fWriter.write32(DrawOp_packOpFlagData(op, 0, 0));
321 }
reed@google.comacd471f2011-05-03 21:26:46 +0000322
323 bool needOpBytes(size_t size = 0);
324
325 inline void doNotify() {
326 if (!fDone) {
reed@google.com44699382013-10-31 17:28:30 +0000327 size_t bytes = fWriter.bytesWritten() - fBytesNotified;
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000328 if (bytes > 0) {
329 fController->notifyWritten(bytes);
330 fBytesNotified += bytes;
331 }
reed@google.comacd471f2011-05-03 21:26:46 +0000332 }
333 }
reed@google.comb55d1182011-05-11 00:42:04 +0000334
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000335 // Should be called after any calls to an SkFlatDictionary::findAndReplace
336 // if a new SkFlatData was added when in cross process mode
337 void flattenFactoryNames();
338
scroggo@google.com59c3ab62013-11-12 14:32:38 +0000339 FlattenableHeap fFlattenableHeap;
340 FlatDictionary fFlatDictionary;
341 SkAutoTUnref<BitmapShuttle> fBitmapShuttle;
342 int fCurrFlatIndex[kCount_PaintFlats];
343
reed@google.comb55d1182011-05-11 00:42:04 +0000344 int flattenToIndex(SkFlattenable* obj, PaintFlats);
345
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000346 // Common code used by drawBitmap*. Behaves differently depending on the
347 // type of SkBitmapHeap being used, which is determined by the flags used.
348 bool commonDrawBitmap(const SkBitmap& bm, DrawOps op, unsigned flags,
349 size_t opBytesNeeded, const SkPaint* paint);
scroggo@google.com58be6822012-07-30 14:40:01 +0000350
reed@google.com31891582011-05-12 03:03:56 +0000351 SkPaint fPaint;
352 void writePaint(const SkPaint&);
reed@google.combb6992a2011-04-26 17:41:56 +0000353
reed@google.comacd471f2011-05-03 21:26:46 +0000354 class AutoPipeNotify {
355 public:
356 AutoPipeNotify(SkGPipeCanvas* canvas) : fCanvas(canvas) {}
357 ~AutoPipeNotify() { fCanvas->doNotify(); }
358 private:
359 SkGPipeCanvas* fCanvas;
360 };
361 friend class AutoPipeNotify;
362
reed@google.combb6992a2011-04-26 17:41:56 +0000363 typedef SkCanvas INHERITED;
364};
365
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000366void SkGPipeCanvas::flattenFactoryNames() {
367 const char* name;
368 while ((name = fFactorySet->getNextAddedFactoryName()) != NULL) {
369 size_t len = strlen(name);
370 if (this->needOpBytes(len)) {
371 this->writeOp(kDef_Factory_DrawOp);
372 fWriter.writeString(name, len);
373 }
374 }
375}
376
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000377bool SkGPipeCanvas::shuttleBitmap(const SkBitmap& bm, int32_t slot) {
scroggo@google.com565254b2012-06-28 15:41:32 +0000378 SkASSERT(shouldFlattenBitmaps(fFlags));
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000379 SkWriteBuffer buffer;
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000380 buffer.setNamedFactoryRecorder(fFactorySet);
scroggo@google.com74b7ffd2013-04-30 02:32:41 +0000381 buffer.writeBitmap(bm);
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000382 this->flattenFactoryNames();
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000383 uint32_t size = buffer.bytesWritten();
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000384 if (this->needOpBytes(size)) {
385 this->writeOp(kDef_Bitmap_DrawOp, 0, slot);
386 void* dst = static_cast<void*>(fWriter.reserve(size));
387 buffer.writeToMemory(dst);
388 return true;
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000389 }
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000390 return false;
scroggo@google.comd3ba5cc2012-07-09 16:05:53 +0000391}
392
reed@google.comb55d1182011-05-11 00:42:04 +0000393// return 0 for NULL (or unflattenable obj), or index-base-1
scroggo@google.comd3ba5cc2012-07-09 16:05:53 +0000394// return ~(index-base-1) if an old flattenable was replaced
reed@google.comb55d1182011-05-11 00:42:04 +0000395int SkGPipeCanvas::flattenToIndex(SkFlattenable* obj, PaintFlats paintflat) {
scroggo@google.comd5d158b2012-08-14 20:38:28 +0000396 SkASSERT(!fDone && fBitmapHeap != NULL);
reed@google.comb55d1182011-05-11 00:42:04 +0000397 if (NULL == obj) {
398 return 0;
399 }
reed@google.comb55d1182011-05-11 00:42:04 +0000400
scroggo@google.comd9d29672012-08-14 17:21:34 +0000401 fBitmapHeap->deferAddingOwners();
scroggo@google.com4dffc592012-07-17 16:49:40 +0000402 bool added, replaced;
scroggo@google.com664fab12012-08-14 19:22:05 +0000403 const SkFlatData* flat = fFlatDictionary.findAndReplace(*obj, fFlattenableHeap.flatToReplace(),
404 &added, &replaced);
scroggo@google.comd9d29672012-08-14 17:21:34 +0000405 fBitmapHeap->endAddingOwnersDeferral(added);
scroggo@google.com4dffc592012-07-17 16:49:40 +0000406 int index = flat->index();
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000407 if (added) {
408 if (isCrossProcess(fFlags)) {
409 this->flattenFactoryNames();
410 }
411 size_t flatSize = flat->flatSize();
412 if (this->needOpBytes(flatSize)) {
413 this->writeOp(kDef_Flattenable_DrawOp, paintflat, index);
414 fWriter.write(flat->data(), flatSize);
415 }
scroggo@google.com4dffc592012-07-17 16:49:40 +0000416 }
417 if (replaced) {
reed@google.comb55d1182011-05-11 00:42:04 +0000418 index = ~index;
reed@google.comb55d1182011-05-11 00:42:04 +0000419 }
scroggo@google.com4dffc592012-07-17 16:49:40 +0000420 return index;
reed@google.comb55d1182011-05-11 00:42:04 +0000421}
422
reed@google.combb6992a2011-04-26 17:41:56 +0000423///////////////////////////////////////////////////////////////////////////////
424
reed@google.comacd471f2011-05-03 21:26:46 +0000425#define MIN_BLOCK_SIZE (16 * 1024)
scroggo@google.com4dffc592012-07-17 16:49:40 +0000426#define BITMAPS_TO_KEEP 5
427#define FLATTENABLES_TO_KEEP 10
reed@google.combb6992a2011-04-26 17:41:56 +0000428
reed@google.comacd471f2011-05-03 21:26:46 +0000429SkGPipeCanvas::SkGPipeCanvas(SkGPipeController* controller,
rmistry@google.comd6176b02012-08-23 18:14:13 +0000430 SkWriter32* writer, uint32_t flags,
junov@chromium.orga8db8fe2012-08-15 19:49:22 +0000431 uint32_t width, uint32_t height)
commit-bot@chromium.org403f8d72014-02-17 15:24:26 +0000432 : SkCanvas(width, height)
433 , fFactorySet(isCrossProcess(flags) ? SkNEW(SkNamedFactorySet) : NULL)
434 , fWriter(*writer)
435 , fFlags(flags)
436 , fFlattenableHeap(FLATTENABLES_TO_KEEP, fFactorySet, isCrossProcess(flags))
437 , fFlatDictionary(&fFlattenableHeap)
438{
reed@google.comacd471f2011-05-03 21:26:46 +0000439 fController = controller;
reed@google.combb6992a2011-04-26 17:41:56 +0000440 fDone = false;
reed@google.comacd471f2011-05-03 21:26:46 +0000441 fBlockSize = 0; // need first block from controller
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000442 fBytesNotified = 0;
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000443 fFirstSaveLayerStackLevel = kNoSaveLayer;
reed@google.comb55d1182011-05-11 00:42:04 +0000444 sk_bzero(fCurrFlatIndex, sizeof(fCurrFlatIndex));
reed@google.comacd471f2011-05-03 21:26:46 +0000445
scroggo@google.com565254b2012-06-28 15:41:32 +0000446 // Tell the reader the appropriate flags to use.
447 if (this->needOpBytes()) {
448 this->writeOp(kReportFlags_DrawOp, fFlags, 0);
449 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000450
scroggo@google.com10dccde2012-08-08 20:43:22 +0000451 if (shouldFlattenBitmaps(flags)) {
scroggo@google.com59c3ab62013-11-12 14:32:38 +0000452 fBitmapShuttle.reset(SkNEW_ARGS(BitmapShuttle, (this)));
453 fBitmapHeap = SkNEW_ARGS(SkBitmapHeap, (fBitmapShuttle.get(), BITMAPS_TO_KEEP));
scroggo@google.com10dccde2012-08-08 20:43:22 +0000454 } else {
scroggo@google.comd9d29672012-08-14 17:21:34 +0000455 fBitmapHeap = SkNEW_ARGS(SkBitmapHeap,
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000456 (BITMAPS_TO_KEEP, controller->numberOfReaders()));
scroggo@google.com10dccde2012-08-08 20:43:22 +0000457 if (this->needOpBytes(sizeof(void*))) {
scroggo@google.comd9d29672012-08-14 17:21:34 +0000458 this->writeOp(kShareBitmapHeap_DrawOp);
459 fWriter.writePtr(static_cast<void*>(fBitmapHeap));
scroggo@google.com10dccde2012-08-08 20:43:22 +0000460 }
461 }
scroggo@google.comd9d29672012-08-14 17:21:34 +0000462 fFlattenableHeap.setBitmapStorage(fBitmapHeap);
scroggo@google.com10dccde2012-08-08 20:43:22 +0000463 this->doNotify();
reed@google.combb6992a2011-04-26 17:41:56 +0000464}
465
466SkGPipeCanvas::~SkGPipeCanvas() {
scroggo@google.com59c3ab62013-11-12 14:32:38 +0000467 this->finish(true);
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000468 SkSafeUnref(fFactorySet);
scroggo@google.comd9d29672012-08-14 17:21:34 +0000469 SkSafeUnref(fBitmapHeap);
reed@google.combb6992a2011-04-26 17:41:56 +0000470}
471
reed@google.comacd471f2011-05-03 21:26:46 +0000472bool SkGPipeCanvas::needOpBytes(size_t needed) {
473 if (fDone) {
474 return false;
475 }
476
477 needed += 4; // size of DrawOp atom
reed@google.com44699382013-10-31 17:28:30 +0000478 if (fWriter.bytesWritten() + needed > fBlockSize) {
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000479 // Before we wipe out any data that has already been written, read it
480 // out.
481 this->doNotify();
482 size_t blockSize = SkMax32(MIN_BLOCK_SIZE, needed);
483 void* block = fController->requestBlock(blockSize, &fBlockSize);
reed@google.comacd471f2011-05-03 21:26:46 +0000484 if (NULL == block) {
scroggo@google.com59c3ab62013-11-12 14:32:38 +0000485 // Do not notify the readers, which would call this function again.
486 this->finish(false);
reed@google.comacd471f2011-05-03 21:26:46 +0000487 return false;
488 }
scroggo@google.com460a23e2012-08-16 17:56:49 +0000489 SkASSERT(SkIsAlign4(fBlockSize));
reed@google.comacd471f2011-05-03 21:26:46 +0000490 fWriter.reset(block, fBlockSize);
491 fBytesNotified = 0;
492 }
493 return true;
494}
495
reed@google.comf5842f72011-05-04 18:30:04 +0000496uint32_t SkGPipeCanvas::getTypefaceID(SkTypeface* face) {
497 uint32_t id = 0; // 0 means default/null typeface
498 if (face) {
499 id = fTypefaceSet.find(face);
500 if (0 == id) {
501 id = fTypefaceSet.add(face);
502 size_t size = writeTypeface(NULL, face);
503 if (this->needOpBytes(size)) {
reed@google.combb6793b2011-05-05 15:18:15 +0000504 this->writeOp(kDef_Typeface_DrawOp);
reed@google.comf5842f72011-05-04 18:30:04 +0000505 writeTypeface(&fWriter, face);
506 }
507 }
508 }
509 return id;
510}
511
reed@google.combb6992a2011-04-26 17:41:56 +0000512///////////////////////////////////////////////////////////////////////////////
513
reed@google.comacd471f2011-05-03 21:26:46 +0000514#define NOTIFY_SETUP(canvas) \
515 AutoPipeNotify apn(canvas)
516
reed@google.combb6992a2011-04-26 17:41:56 +0000517int SkGPipeCanvas::save(SaveFlags flags) {
reed@google.comacd471f2011-05-03 21:26:46 +0000518 NOTIFY_SETUP(this);
519 if (this->needOpBytes()) {
520 this->writeOp(kSave_DrawOp, 0, flags);
521 }
reed@google.combb6992a2011-04-26 17:41:56 +0000522 return this->INHERITED::save(flags);
523}
524
525int SkGPipeCanvas::saveLayer(const SkRect* bounds, const SkPaint* paint,
526 SaveFlags saveFlags) {
reed@google.comacd471f2011-05-03 21:26:46 +0000527 NOTIFY_SETUP(this);
528 size_t size = 0;
reed@google.combb6992a2011-04-26 17:41:56 +0000529 unsigned opFlags = 0;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000530
reed@google.combb6992a2011-04-26 17:41:56 +0000531 if (bounds) {
532 opFlags |= kSaveLayer_HasBounds_DrawOpFlag;
reed@google.comacd471f2011-05-03 21:26:46 +0000533 size += sizeof(SkRect);
reed@google.combb6992a2011-04-26 17:41:56 +0000534 }
535 if (paint) {
536 opFlags |= kSaveLayer_HasPaint_DrawOpFlag;
reed@google.com31891582011-05-12 03:03:56 +0000537 this->writePaint(*paint);
reed@google.combb6992a2011-04-26 17:41:56 +0000538 }
539
reed@google.comacd471f2011-05-03 21:26:46 +0000540 if (this->needOpBytes(size)) {
541 this->writeOp(kSaveLayer_DrawOp, opFlags, saveFlags);
542 if (bounds) {
543 fWriter.writeRect(*bounds);
544 }
reed@google.combb6992a2011-04-26 17:41:56 +0000545 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000546
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000547 if (kNoSaveLayer == fFirstSaveLayerStackLevel){
548 fFirstSaveLayerStackLevel = this->getSaveCount();
549 }
reed@google.combb6992a2011-04-26 17:41:56 +0000550 // we just pass on the save, so we don't create a layer
551 return this->INHERITED::save(saveFlags);
552}
553
554void SkGPipeCanvas::restore() {
reed@google.comacd471f2011-05-03 21:26:46 +0000555 NOTIFY_SETUP(this);
556 if (this->needOpBytes()) {
557 this->writeOp(kRestore_DrawOp);
558 }
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000559
reed@google.combb6992a2011-04-26 17:41:56 +0000560 this->INHERITED::restore();
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000561
562 if (this->getSaveCount() == fFirstSaveLayerStackLevel){
563 fFirstSaveLayerStackLevel = kNoSaveLayer;
564 }
565}
566
567bool SkGPipeCanvas::isDrawingToLayer() const {
568 return kNoSaveLayer != fFirstSaveLayerStackLevel;
reed@google.combb6992a2011-04-26 17:41:56 +0000569}
570
571bool SkGPipeCanvas::translate(SkScalar dx, SkScalar dy) {
572 if (dx || dy) {
reed@google.comacd471f2011-05-03 21:26:46 +0000573 NOTIFY_SETUP(this);
574 if (this->needOpBytes(2 * sizeof(SkScalar))) {
575 this->writeOp(kTranslate_DrawOp);
576 fWriter.writeScalar(dx);
577 fWriter.writeScalar(dy);
578 }
reed@google.combb6992a2011-04-26 17:41:56 +0000579 }
580 return this->INHERITED::translate(dx, dy);
581}
582
583bool SkGPipeCanvas::scale(SkScalar sx, SkScalar sy) {
584 if (sx || sy) {
reed@google.comacd471f2011-05-03 21:26:46 +0000585 NOTIFY_SETUP(this);
586 if (this->needOpBytes(2 * sizeof(SkScalar))) {
587 this->writeOp(kScale_DrawOp);
588 fWriter.writeScalar(sx);
589 fWriter.writeScalar(sy);
590 }
reed@google.combb6992a2011-04-26 17:41:56 +0000591 }
592 return this->INHERITED::scale(sx, sy);
593}
594
595bool SkGPipeCanvas::rotate(SkScalar degrees) {
596 if (degrees) {
reed@google.comacd471f2011-05-03 21:26:46 +0000597 NOTIFY_SETUP(this);
598 if (this->needOpBytes(sizeof(SkScalar))) {
599 this->writeOp(kRotate_DrawOp);
600 fWriter.writeScalar(degrees);
601 }
reed@google.combb6992a2011-04-26 17:41:56 +0000602 }
603 return this->INHERITED::rotate(degrees);
604}
605
606bool SkGPipeCanvas::skew(SkScalar sx, SkScalar sy) {
607 if (sx || sy) {
reed@google.comacd471f2011-05-03 21:26:46 +0000608 NOTIFY_SETUP(this);
609 if (this->needOpBytes(2 * sizeof(SkScalar))) {
610 this->writeOp(kSkew_DrawOp);
611 fWriter.writeScalar(sx);
612 fWriter.writeScalar(sy);
613 }
reed@google.combb6992a2011-04-26 17:41:56 +0000614 }
615 return this->INHERITED::skew(sx, sy);
616}
617
618bool SkGPipeCanvas::concat(const SkMatrix& matrix) {
619 if (!matrix.isIdentity()) {
reed@google.comacd471f2011-05-03 21:26:46 +0000620 NOTIFY_SETUP(this);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000621 if (this->needOpBytes(matrix.writeToMemory(NULL))) {
reed@google.comacd471f2011-05-03 21:26:46 +0000622 this->writeOp(kConcat_DrawOp);
djsollen@google.com2b2ede32012-04-12 13:24:04 +0000623 fWriter.writeMatrix(matrix);
reed@google.comacd471f2011-05-03 21:26:46 +0000624 }
reed@google.combb6992a2011-04-26 17:41:56 +0000625 }
626 return this->INHERITED::concat(matrix);
627}
628
629void SkGPipeCanvas::setMatrix(const SkMatrix& matrix) {
reed@google.comacd471f2011-05-03 21:26:46 +0000630 NOTIFY_SETUP(this);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000631 if (this->needOpBytes(matrix.writeToMemory(NULL))) {
reed@google.comacd471f2011-05-03 21:26:46 +0000632 this->writeOp(kSetMatrix_DrawOp);
djsollen@google.com2b2ede32012-04-12 13:24:04 +0000633 fWriter.writeMatrix(matrix);
reed@google.comacd471f2011-05-03 21:26:46 +0000634 }
reed@google.combb6992a2011-04-26 17:41:56 +0000635 this->INHERITED::setMatrix(matrix);
636}
637
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000638void SkGPipeCanvas::onClipRect(const SkRect& rect, SkRegion::Op rgnOp,
639 ClipEdgeStyle edgeStyle) {
reed@google.comacd471f2011-05-03 21:26:46 +0000640 NOTIFY_SETUP(this);
scroggo@google.com460a23e2012-08-16 17:56:49 +0000641 if (this->needOpBytes(sizeof(SkRect))) {
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000642 unsigned flags = 0;
643 if (kSoft_ClipEdgeStyle == edgeStyle) {
644 flags = kClip_HasAntiAlias_DrawOpFlag;
645 }
scroggo@google.com460a23e2012-08-16 17:56:49 +0000646 this->writeOp(kClipRect_DrawOp, flags, rgnOp);
reed@google.comacd471f2011-05-03 21:26:46 +0000647 fWriter.writeRect(rect);
648 }
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000649 this->INHERITED::onClipRect(rect, rgnOp, edgeStyle);
reed@google.combb6992a2011-04-26 17:41:56 +0000650}
651
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000652void SkGPipeCanvas::onClipRRect(const SkRRect& rrect, SkRegion::Op rgnOp,
653 ClipEdgeStyle edgeStyle) {
reed@google.com4ed0fb72012-12-12 20:48:18 +0000654 NOTIFY_SETUP(this);
655 if (this->needOpBytes(kSizeOfFlatRRect)) {
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000656 unsigned flags = 0;
657 if (kSoft_ClipEdgeStyle == edgeStyle) {
658 flags = kClip_HasAntiAlias_DrawOpFlag;
659 }
reed@google.com4ed0fb72012-12-12 20:48:18 +0000660 this->writeOp(kClipRRect_DrawOp, flags, rgnOp);
661 fWriter.writeRRect(rrect);
662 }
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000663 this->INHERITED::onClipRRect(rrect, rgnOp, edgeStyle);
reed@google.com4ed0fb72012-12-12 20:48:18 +0000664}
665
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000666void SkGPipeCanvas::onClipPath(const SkPath& path, SkRegion::Op rgnOp,
667 ClipEdgeStyle edgeStyle) {
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))) {
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000670 unsigned flags = 0;
671 if (kSoft_ClipEdgeStyle == edgeStyle) {
672 flags = kClip_HasAntiAlias_DrawOpFlag;
673 }
scroggo@google.com460a23e2012-08-16 17:56:49 +0000674 this->writeOp(kClipPath_DrawOp, flags, rgnOp);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000675 fWriter.writePath(path);
reed@google.comacd471f2011-05-03 21:26:46 +0000676 }
reed@google.combb6992a2011-04-26 17:41:56 +0000677 // we just pass on the bounds of the path
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000678 this->INHERITED::onClipRect(path.getBounds(), rgnOp, edgeStyle);
reed@google.combb6992a2011-04-26 17:41:56 +0000679}
680
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000681void SkGPipeCanvas::onClipRegion(const SkRegion& region, SkRegion::Op rgnOp) {
reed@google.comacd471f2011-05-03 21:26:46 +0000682 NOTIFY_SETUP(this);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000683 if (this->needOpBytes(region.writeToMemory(NULL))) {
reed@google.comacd471f2011-05-03 21:26:46 +0000684 this->writeOp(kClipRegion_DrawOp, 0, rgnOp);
djsollen@google.com2b2ede32012-04-12 13:24:04 +0000685 fWriter.writeRegion(region);
reed@google.comacd471f2011-05-03 21:26:46 +0000686 }
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000687 this->INHERITED::onClipRegion(region, rgnOp);
reed@google.combb6992a2011-04-26 17:41:56 +0000688}
689
690///////////////////////////////////////////////////////////////////////////////
691
692void SkGPipeCanvas::clear(SkColor color) {
reed@google.comacd471f2011-05-03 21:26:46 +0000693 NOTIFY_SETUP(this);
reed@google.combb6992a2011-04-26 17:41:56 +0000694 unsigned flags = 0;
695 if (color) {
696 flags |= kClear_HasColor_DrawOpFlag;
697 }
reed@google.comacd471f2011-05-03 21:26:46 +0000698 if (this->needOpBytes(sizeof(SkColor))) {
699 this->writeOp(kDrawClear_DrawOp, flags, 0);
700 if (color) {
701 fWriter.write32(color);
702 }
reed@google.combb6992a2011-04-26 17:41:56 +0000703 }
704}
705
706void SkGPipeCanvas::drawPaint(const SkPaint& paint) {
reed@google.comacd471f2011-05-03 21:26:46 +0000707 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000708 this->writePaint(paint);
reed@google.comacd471f2011-05-03 21:26:46 +0000709 if (this->needOpBytes()) {
reed@google.com31891582011-05-12 03:03:56 +0000710 this->writeOp(kDrawPaint_DrawOp);
reed@google.comacd471f2011-05-03 21:26:46 +0000711 }
reed@google.combb6992a2011-04-26 17:41:56 +0000712}
713
714void SkGPipeCanvas::drawPoints(PointMode mode, size_t count,
robertphillips@google.com8b169312013-10-15 17:47:36 +0000715 const SkPoint pts[], const SkPaint& paint) {
reed@google.combb6992a2011-04-26 17:41:56 +0000716 if (count) {
reed@google.comacd471f2011-05-03 21:26:46 +0000717 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000718 this->writePaint(paint);
reed@google.comacd471f2011-05-03 21:26:46 +0000719 if (this->needOpBytes(4 + count * sizeof(SkPoint))) {
reed@google.com31891582011-05-12 03:03:56 +0000720 this->writeOp(kDrawPoints_DrawOp, mode, 0);
reed@google.comacd471f2011-05-03 21:26:46 +0000721 fWriter.write32(count);
722 fWriter.write(pts, count * sizeof(SkPoint));
723 }
reed@google.combb6992a2011-04-26 17:41:56 +0000724 }
725}
726
reed@google.com4ed0fb72012-12-12 20:48:18 +0000727void SkGPipeCanvas::drawOval(const SkRect& rect, const SkPaint& paint) {
728 NOTIFY_SETUP(this);
729 this->writePaint(paint);
730 if (this->needOpBytes(sizeof(SkRect))) {
731 this->writeOp(kDrawOval_DrawOp);
732 fWriter.writeRect(rect);
733 }
734}
735
bsalomon@google.com7ce564c2013-10-22 16:54:15 +0000736void SkGPipeCanvas::drawRect(const SkRect& rect, const SkPaint& paint) {
reed@google.comacd471f2011-05-03 21:26:46 +0000737 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000738 this->writePaint(paint);
reed@google.comacd471f2011-05-03 21:26:46 +0000739 if (this->needOpBytes(sizeof(SkRect))) {
reed@google.com31891582011-05-12 03:03:56 +0000740 this->writeOp(kDrawRect_DrawOp);
reed@google.comacd471f2011-05-03 21:26:46 +0000741 fWriter.writeRect(rect);
742 }
reed@google.combb6992a2011-04-26 17:41:56 +0000743}
744
reed@google.com4ed0fb72012-12-12 20:48:18 +0000745void SkGPipeCanvas::drawRRect(const SkRRect& rrect, const SkPaint& paint) {
746 NOTIFY_SETUP(this);
747 this->writePaint(paint);
748 if (this->needOpBytes(kSizeOfFlatRRect)) {
749 this->writeOp(kDrawRRect_DrawOp);
750 fWriter.writeRRect(rrect);
751 }
752}
753
commit-bot@chromium.orged9806f2014-02-21 02:32:36 +0000754void SkGPipeCanvas::onDrawDRRect(const SkRRect& outer, const SkRRect& inner,
755 const SkPaint& paint) {
756 NOTIFY_SETUP(this);
757 this->writePaint(paint);
758 if (this->needOpBytes(kSizeOfFlatRRect * 2)) {
759 this->writeOp(kDrawDRRect_DrawOp);
760 fWriter.writeRRect(outer);
761 fWriter.writeRRect(inner);
762 }
763}
764
bsalomon@google.com7ce564c2013-10-22 16:54:15 +0000765void SkGPipeCanvas::drawPath(const SkPath& path, const SkPaint& paint) {
reed@google.comacd471f2011-05-03 21:26:46 +0000766 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000767 this->writePaint(paint);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000768 if (this->needOpBytes(path.writeToMemory(NULL))) {
reed@google.com31891582011-05-12 03:03:56 +0000769 this->writeOp(kDrawPath_DrawOp);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000770 fWriter.writePath(path);
reed@google.comacd471f2011-05-03 21:26:46 +0000771 }
reed@google.combb6992a2011-04-26 17:41:56 +0000772}
773
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000774bool SkGPipeCanvas::commonDrawBitmap(const SkBitmap& bm, DrawOps op,
775 unsigned flags,
776 size_t opBytesNeeded,
777 const SkPaint* paint) {
scroggo@google.com58be6822012-07-30 14:40:01 +0000778 if (paint != NULL) {
scroggo@google.com460a23e2012-08-16 17:56:49 +0000779 flags |= kDrawBitmap_HasPaint_DrawOpFlag;
scroggo@google.com58be6822012-07-30 14:40:01 +0000780 this->writePaint(*paint);
781 }
scroggo@google.com10dccde2012-08-08 20:43:22 +0000782 if (this->needOpBytes(opBytesNeeded)) {
scroggo@google.comd5d158b2012-08-14 20:38:28 +0000783 SkASSERT(fBitmapHeap != NULL);
784 int32_t bitmapIndex = fBitmapHeap->insert(bm);
785 if (SkBitmapHeap::INVALID_SLOT == bitmapIndex) {
786 return false;
787 }
scroggo@google.com10dccde2012-08-08 20:43:22 +0000788 this->writeOp(op, flags, bitmapIndex);
scroggo@google.com58be6822012-07-30 14:40:01 +0000789 return true;
790 }
791 return false;
792}
793
794void SkGPipeCanvas::drawBitmap(const SkBitmap& bm, SkScalar left, SkScalar top,
795 const SkPaint* paint) {
796 NOTIFY_SETUP(this);
797 size_t opBytesNeeded = sizeof(SkScalar) * 2;
798
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000799 if (this->commonDrawBitmap(bm, kDrawBitmap_DrawOp, 0, opBytesNeeded, paint)) {
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000800 fWriter.writeScalar(left);
801 fWriter.writeScalar(top);
802 }
reed@google.combb6992a2011-04-26 17:41:56 +0000803}
804
reed@google.com71121732012-09-18 15:14:33 +0000805void SkGPipeCanvas::drawBitmapRectToRect(const SkBitmap& bm, const SkRect* src,
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +0000806 const SkRect& dst, const SkPaint* paint,
807 DrawBitmapRectFlags dbmrFlags) {
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000808 NOTIFY_SETUP(this);
scroggo@google.com58be6822012-07-30 14:40:01 +0000809 size_t opBytesNeeded = sizeof(SkRect);
810 bool hasSrc = src != NULL;
811 unsigned flags;
812 if (hasSrc) {
scroggo@google.com460a23e2012-08-16 17:56:49 +0000813 flags = kDrawBitmap_HasSrcRect_DrawOpFlag;
scroggo@google.com58be6822012-07-30 14:40:01 +0000814 opBytesNeeded += sizeof(int32_t) * 4;
815 } else {
816 flags = 0;
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000817 }
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +0000818 if (dbmrFlags & kBleed_DrawBitmapRectFlag) {
819 flags |= kDrawBitmap_Bleed_DrawOpFlag;
820 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000821
reed@google.com71121732012-09-18 15:14:33 +0000822 if (this->commonDrawBitmap(bm, kDrawBitmapRectToRect_DrawOp, flags, opBytesNeeded, paint)) {
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000823 if (hasSrc) {
reed@google.com71121732012-09-18 15:14:33 +0000824 fWriter.writeRect(*src);
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000825 }
826 fWriter.writeRect(dst);
827 }
reed@google.combb6992a2011-04-26 17:41:56 +0000828}
829
scroggo@google.com58be6822012-07-30 14:40:01 +0000830void SkGPipeCanvas::drawBitmapMatrix(const SkBitmap& bm, const SkMatrix& matrix,
831 const SkPaint* paint) {
832 NOTIFY_SETUP(this);
833 size_t opBytesNeeded = matrix.writeToMemory(NULL);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000834
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000835 if (this->commonDrawBitmap(bm, kDrawBitmapMatrix_DrawOp, 0, opBytesNeeded, paint)) {
scroggo@google.com58be6822012-07-30 14:40:01 +0000836 fWriter.writeMatrix(matrix);
837 }
reed@google.combb6992a2011-04-26 17:41:56 +0000838}
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000839
840void SkGPipeCanvas::drawBitmapNine(const SkBitmap& bm, const SkIRect& center,
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000841 const SkRect& dst, const SkPaint* paint) {
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000842 NOTIFY_SETUP(this);
scroggo@google.com58be6822012-07-30 14:40:01 +0000843 size_t opBytesNeeded = sizeof(int32_t) * 4 + sizeof(SkRect);
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000844
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000845 if (this->commonDrawBitmap(bm, kDrawBitmapNine_DrawOp, 0, opBytesNeeded, paint)) {
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000846 fWriter.write32(center.fLeft);
847 fWriter.write32(center.fTop);
848 fWriter.write32(center.fRight);
849 fWriter.write32(center.fBottom);
850 fWriter.writeRect(dst);
851 }
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000852}
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000853
854void SkGPipeCanvas::drawSprite(const SkBitmap& bm, int left, int top,
855 const SkPaint* paint) {
856 NOTIFY_SETUP(this);
scroggo@google.com58be6822012-07-30 14:40:01 +0000857 size_t opBytesNeeded = sizeof(int32_t) * 2;
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000858
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000859 if (this->commonDrawBitmap(bm, kDrawSprite_DrawOp, 0, opBytesNeeded, paint)) {
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000860 fWriter.write32(left);
861 fWriter.write32(top);
862 }
reed@google.combb6992a2011-04-26 17:41:56 +0000863}
864
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000865void SkGPipeCanvas::drawText(const void* text, size_t byteLength, SkScalar x,
reed@google.combb6992a2011-04-26 17:41:56 +0000866 SkScalar y, const SkPaint& paint) {
867 if (byteLength) {
reed@google.comacd471f2011-05-03 21:26:46 +0000868 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000869 this->writePaint(paint);
reed@google.comacd471f2011-05-03 21:26:46 +0000870 if (this->needOpBytes(4 + SkAlign4(byteLength) + 2 * sizeof(SkScalar))) {
reed@google.com31891582011-05-12 03:03:56 +0000871 this->writeOp(kDrawText_DrawOp);
reed@google.comacd471f2011-05-03 21:26:46 +0000872 fWriter.write32(byteLength);
873 fWriter.writePad(text, byteLength);
874 fWriter.writeScalar(x);
875 fWriter.writeScalar(y);
876 }
reed@google.combb6992a2011-04-26 17:41:56 +0000877 }
878}
879
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000880void SkGPipeCanvas::drawPosText(const void* text, size_t byteLength,
reed@google.combb6992a2011-04-26 17:41:56 +0000881 const SkPoint pos[], const SkPaint& paint) {
882 if (byteLength) {
reed@google.comacd471f2011-05-03 21:26:46 +0000883 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000884 this->writePaint(paint);
reed@google.combb6992a2011-04-26 17:41:56 +0000885 int count = paint.textToGlyphs(text, byteLength, NULL);
reed@google.comacd471f2011-05-03 21:26:46 +0000886 if (this->needOpBytes(4 + SkAlign4(byteLength) + 4 + count * sizeof(SkPoint))) {
reed@google.com31891582011-05-12 03:03:56 +0000887 this->writeOp(kDrawPosText_DrawOp);
reed@google.comacd471f2011-05-03 21:26:46 +0000888 fWriter.write32(byteLength);
889 fWriter.writePad(text, byteLength);
890 fWriter.write32(count);
891 fWriter.write(pos, count * sizeof(SkPoint));
892 }
reed@google.combb6992a2011-04-26 17:41:56 +0000893 }
894}
895
896void SkGPipeCanvas::drawPosTextH(const void* text, size_t byteLength,
897 const SkScalar xpos[], SkScalar constY,
898 const SkPaint& paint) {
899 if (byteLength) {
reed@google.comacd471f2011-05-03 21:26:46 +0000900 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000901 this->writePaint(paint);
reed@google.combb6992a2011-04-26 17:41:56 +0000902 int count = paint.textToGlyphs(text, byteLength, NULL);
reed@google.comacd471f2011-05-03 21:26:46 +0000903 if (this->needOpBytes(4 + SkAlign4(byteLength) + 4 + count * sizeof(SkScalar) + 4)) {
reed@google.com31891582011-05-12 03:03:56 +0000904 this->writeOp(kDrawPosTextH_DrawOp);
reed@google.comacd471f2011-05-03 21:26:46 +0000905 fWriter.write32(byteLength);
906 fWriter.writePad(text, byteLength);
907 fWriter.write32(count);
908 fWriter.write(xpos, count * sizeof(SkScalar));
909 fWriter.writeScalar(constY);
910 }
reed@google.combb6992a2011-04-26 17:41:56 +0000911 }
912}
913
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000914void SkGPipeCanvas::drawTextOnPath(const void* text, size_t byteLength,
915 const SkPath& path, const SkMatrix* matrix,
reed@google.combb6992a2011-04-26 17:41:56 +0000916 const SkPaint& paint) {
917 if (byteLength) {
reed@google.comacd471f2011-05-03 21:26:46 +0000918 NOTIFY_SETUP(this);
reed@google.combb6992a2011-04-26 17:41:56 +0000919 unsigned flags = 0;
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000920 size_t size = 4 + SkAlign4(byteLength) + path.writeToMemory(NULL);
reed@google.combb6992a2011-04-26 17:41:56 +0000921 if (matrix) {
922 flags |= kDrawTextOnPath_HasMatrix_DrawOpFlag;
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000923 size += matrix->writeToMemory(NULL);
reed@google.combb6992a2011-04-26 17:41:56 +0000924 }
reed@google.com31891582011-05-12 03:03:56 +0000925 this->writePaint(paint);
reed@google.comacd471f2011-05-03 21:26:46 +0000926 if (this->needOpBytes(size)) {
reed@google.com31891582011-05-12 03:03:56 +0000927 this->writeOp(kDrawTextOnPath_DrawOp, flags, 0);
reed@google.combb6992a2011-04-26 17:41:56 +0000928
reed@google.comacd471f2011-05-03 21:26:46 +0000929 fWriter.write32(byteLength);
930 fWriter.writePad(text, byteLength);
reed@google.combb6992a2011-04-26 17:41:56 +0000931
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000932 fWriter.writePath(path);
reed@google.comacd471f2011-05-03 21:26:46 +0000933 if (matrix) {
djsollen@google.com2b2ede32012-04-12 13:24:04 +0000934 fWriter.writeMatrix(*matrix);
reed@google.comacd471f2011-05-03 21:26:46 +0000935 }
reed@google.combb6992a2011-04-26 17:41:56 +0000936 }
937 }
938}
939
940void SkGPipeCanvas::drawPicture(SkPicture& picture) {
reed@google.com0faac1e2011-05-11 05:58:58 +0000941 // we want to playback the picture into individual draw calls
942 this->INHERITED::drawPicture(picture);
reed@google.combb6992a2011-04-26 17:41:56 +0000943}
944
reed@google.com85e143c2013-12-30 15:51:25 +0000945void SkGPipeCanvas::drawVertices(VertexMode vmode, int vertexCount,
reed@google.combb6992a2011-04-26 17:41:56 +0000946 const SkPoint vertices[], const SkPoint texs[],
reed@google.com85e143c2013-12-30 15:51:25 +0000947 const SkColor colors[], SkXfermode* xfer,
reed@google.combb6992a2011-04-26 17:41:56 +0000948 const uint16_t indices[], int indexCount,
949 const SkPaint& paint) {
950 if (0 == vertexCount) {
951 return;
952 }
953
reed@google.comacd471f2011-05-03 21:26:46 +0000954 NOTIFY_SETUP(this);
955 size_t size = 4 + vertexCount * sizeof(SkPoint);
reed@google.com31891582011-05-12 03:03:56 +0000956 this->writePaint(paint);
reed@google.combb6992a2011-04-26 17:41:56 +0000957 unsigned flags = 0;
958 if (texs) {
959 flags |= kDrawVertices_HasTexs_DrawOpFlag;
reed@google.comacd471f2011-05-03 21:26:46 +0000960 size += vertexCount * sizeof(SkPoint);
reed@google.combb6992a2011-04-26 17:41:56 +0000961 }
962 if (colors) {
963 flags |= kDrawVertices_HasColors_DrawOpFlag;
reed@google.comacd471f2011-05-03 21:26:46 +0000964 size += vertexCount * sizeof(SkColor);
reed@google.combb6992a2011-04-26 17:41:56 +0000965 }
966 if (indices && indexCount > 0) {
967 flags |= kDrawVertices_HasIndices_DrawOpFlag;
reed@google.comacd471f2011-05-03 21:26:46 +0000968 size += 4 + SkAlign4(indexCount * sizeof(uint16_t));
reed@google.combb6992a2011-04-26 17:41:56 +0000969 }
reed@google.com85e143c2013-12-30 15:51:25 +0000970 if (xfer && !SkXfermode::IsMode(xfer, SkXfermode::kModulate_Mode)) {
971 flags |= kDrawVertices_HasXfermode_DrawOpFlag;
972 size += sizeof(int32_t); // mode enum
973 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000974
reed@google.comacd471f2011-05-03 21:26:46 +0000975 if (this->needOpBytes(size)) {
reed@google.com31891582011-05-12 03:03:56 +0000976 this->writeOp(kDrawVertices_DrawOp, flags, 0);
reed@google.com85e143c2013-12-30 15:51:25 +0000977 fWriter.write32(vmode);
reed@google.comacd471f2011-05-03 21:26:46 +0000978 fWriter.write32(vertexCount);
979 fWriter.write(vertices, vertexCount * sizeof(SkPoint));
980 if (texs) {
981 fWriter.write(texs, vertexCount * sizeof(SkPoint));
982 }
983 if (colors) {
984 fWriter.write(colors, vertexCount * sizeof(SkColor));
985 }
reed@google.com85e143c2013-12-30 15:51:25 +0000986 if (flags & kDrawVertices_HasXfermode_DrawOpFlag) {
987 SkXfermode::Mode mode = SkXfermode::kModulate_Mode;
988 (void)xfer->asMode(&mode);
989 fWriter.write32(mode);
990 }
reed@google.comacd471f2011-05-03 21:26:46 +0000991 if (indices && indexCount > 0) {
992 fWriter.write32(indexCount);
993 fWriter.writePad(indices, indexCount * sizeof(uint16_t));
994 }
reed@google.combb6992a2011-04-26 17:41:56 +0000995 }
996}
997
reed@google.comacd471f2011-05-03 21:26:46 +0000998void SkGPipeCanvas::drawData(const void* ptr, size_t size) {
999 if (size && ptr) {
1000 NOTIFY_SETUP(this);
reed@google.combb6992a2011-04-26 17:41:56 +00001001 unsigned data = 0;
1002 if (size < (1 << DRAWOPS_DATA_BITS)) {
1003 data = (unsigned)size;
1004 }
reed@google.comacd471f2011-05-03 21:26:46 +00001005 if (this->needOpBytes(4 + SkAlign4(size))) {
1006 this->writeOp(kDrawData_DrawOp, 0, data);
1007 if (0 == data) {
1008 fWriter.write32(size);
1009 }
reed@google.combb6793b2011-05-05 15:18:15 +00001010 fWriter.writePad(ptr, size);
reed@google.combb6992a2011-04-26 17:41:56 +00001011 }
1012 }
1013}
1014
robertphillips@google.com0a4805e2013-05-29 13:24:23 +00001015void SkGPipeCanvas::beginCommentGroup(const char* description) {
1016 // ignore for now
1017}
1018
1019void SkGPipeCanvas::addComment(const char* kywd, const char* value) {
1020 // ignore for now
1021}
1022
1023void SkGPipeCanvas::endCommentGroup() {
1024 // ignore for now
1025}
1026
junov@chromium.org77eec242012-07-18 17:54:45 +00001027void SkGPipeCanvas::flushRecording(bool detachCurrentBlock) {
1028 doNotify();
1029 if (detachCurrentBlock) {
1030 // force a new block to be requested for the next recorded command
rmistry@google.comd6176b02012-08-23 18:14:13 +00001031 fBlockSize = 0;
junov@chromium.org77eec242012-07-18 17:54:45 +00001032 }
1033}
1034
junov@chromium.org2e14ba82012-08-07 14:26:57 +00001035size_t SkGPipeCanvas::freeMemoryIfPossible(size_t bytesToFree) {
scroggo@google.comd5d158b2012-08-14 20:38:28 +00001036 return (NULL == fBitmapHeap) ? 0 : fBitmapHeap->freeMemoryIfPossible(bytesToFree);
junov@chromium.org2e14ba82012-08-07 14:26:57 +00001037}
1038
reed@google.combb6992a2011-04-26 17:41:56 +00001039///////////////////////////////////////////////////////////////////////////////
1040
1041template <typename T> uint32_t castToU32(T value) {
1042 union {
1043 T fSrc;
1044 uint32_t fDst;
1045 } data;
1046 data.fSrc = value;
1047 return data.fDst;
1048}
1049
reed@google.com31891582011-05-12 03:03:56 +00001050void SkGPipeCanvas::writePaint(const SkPaint& paint) {
scroggo@google.comd5d158b2012-08-14 20:38:28 +00001051 if (fDone) {
1052 return;
1053 }
reed@google.com31891582011-05-12 03:03:56 +00001054 SkPaint& base = fPaint;
reed@google.combb6992a2011-04-26 17:41:56 +00001055 uint32_t storage[32];
1056 uint32_t* ptr = storage;
reed@google.combb6992a2011-04-26 17:41:56 +00001057
1058 if (base.getFlags() != paint.getFlags()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001059 *ptr++ = PaintOp_packOpData(kFlags_PaintOp, paint.getFlags());
reed@google.comf5842f72011-05-04 18:30:04 +00001060 base.setFlags(paint.getFlags());
reed@google.combb6992a2011-04-26 17:41:56 +00001061 }
1062 if (base.getColor() != paint.getColor()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001063 *ptr++ = PaintOp_packOp(kColor_PaintOp);
1064 *ptr++ = paint.getColor();
reed@google.comf5842f72011-05-04 18:30:04 +00001065 base.setColor(paint.getColor());
reed@google.combb6992a2011-04-26 17:41:56 +00001066 }
1067 if (base.getStyle() != paint.getStyle()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001068 *ptr++ = PaintOp_packOpData(kStyle_PaintOp, paint.getStyle());
reed@google.comf5842f72011-05-04 18:30:04 +00001069 base.setStyle(paint.getStyle());
reed@google.combb6992a2011-04-26 17:41:56 +00001070 }
1071 if (base.getStrokeJoin() != paint.getStrokeJoin()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001072 *ptr++ = PaintOp_packOpData(kJoin_PaintOp, paint.getStrokeJoin());
reed@google.comf5842f72011-05-04 18:30:04 +00001073 base.setStrokeJoin(paint.getStrokeJoin());
reed@google.combb6992a2011-04-26 17:41:56 +00001074 }
1075 if (base.getStrokeCap() != paint.getStrokeCap()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001076 *ptr++ = PaintOp_packOpData(kCap_PaintOp, paint.getStrokeCap());
reed@google.comf5842f72011-05-04 18:30:04 +00001077 base.setStrokeCap(paint.getStrokeCap());
reed@google.combb6992a2011-04-26 17:41:56 +00001078 }
1079 if (base.getStrokeWidth() != paint.getStrokeWidth()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001080 *ptr++ = PaintOp_packOp(kWidth_PaintOp);
1081 *ptr++ = castToU32(paint.getStrokeWidth());
reed@google.comf5842f72011-05-04 18:30:04 +00001082 base.setStrokeWidth(paint.getStrokeWidth());
reed@google.combb6992a2011-04-26 17:41:56 +00001083 }
1084 if (base.getStrokeMiter() != paint.getStrokeMiter()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001085 *ptr++ = PaintOp_packOp(kMiter_PaintOp);
1086 *ptr++ = castToU32(paint.getStrokeMiter());
reed@google.comf5842f72011-05-04 18:30:04 +00001087 base.setStrokeMiter(paint.getStrokeMiter());
reed@google.combb6992a2011-04-26 17:41:56 +00001088 }
1089 if (base.getTextEncoding() != paint.getTextEncoding()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001090 *ptr++ = PaintOp_packOpData(kEncoding_PaintOp, paint.getTextEncoding());
reed@google.comf5842f72011-05-04 18:30:04 +00001091 base.setTextEncoding(paint.getTextEncoding());
reed@google.combb6992a2011-04-26 17:41:56 +00001092 }
1093 if (base.getHinting() != paint.getHinting()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001094 *ptr++ = PaintOp_packOpData(kHinting_PaintOp, paint.getHinting());
reed@google.comf5842f72011-05-04 18:30:04 +00001095 base.setHinting(paint.getHinting());
reed@google.combb6992a2011-04-26 17:41:56 +00001096 }
1097 if (base.getTextAlign() != paint.getTextAlign()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001098 *ptr++ = PaintOp_packOpData(kAlign_PaintOp, paint.getTextAlign());
reed@google.comf5842f72011-05-04 18:30:04 +00001099 base.setTextAlign(paint.getTextAlign());
reed@google.combb6992a2011-04-26 17:41:56 +00001100 }
1101 if (base.getTextSize() != paint.getTextSize()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001102 *ptr++ = PaintOp_packOp(kTextSize_PaintOp);
1103 *ptr++ = castToU32(paint.getTextSize());
reed@google.comf5842f72011-05-04 18:30:04 +00001104 base.setTextSize(paint.getTextSize());
reed@google.combb6992a2011-04-26 17:41:56 +00001105 }
1106 if (base.getTextScaleX() != paint.getTextScaleX()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001107 *ptr++ = PaintOp_packOp(kTextScaleX_PaintOp);
1108 *ptr++ = castToU32(paint.getTextScaleX());
reed@google.comf5842f72011-05-04 18:30:04 +00001109 base.setTextScaleX(paint.getTextScaleX());
reed@google.combb6992a2011-04-26 17:41:56 +00001110 }
1111 if (base.getTextSkewX() != paint.getTextSkewX()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001112 *ptr++ = PaintOp_packOp(kTextSkewX_PaintOp);
1113 *ptr++ = castToU32(paint.getTextSkewX());
reed@google.comf5842f72011-05-04 18:30:04 +00001114 base.setTextSkewX(paint.getTextSkewX());
1115 }
1116
1117 if (!SkTypeface::Equal(base.getTypeface(), paint.getTypeface())) {
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +00001118 if (isCrossProcess(fFlags)) {
scroggo@google.com3cb969f2012-07-27 20:39:19 +00001119 uint32_t id = this->getTypefaceID(paint.getTypeface());
1120 *ptr++ = PaintOp_packOpData(kTypeface_PaintOp, id);
1121 } else if (this->needOpBytes(sizeof(void*))) {
1122 // Add to the set for ref counting.
1123 fTypefaceSet.add(paint.getTypeface());
1124 // It is safe to write the typeface to the stream before the rest
1125 // of the paint unless we ever send a kReset_PaintOp, which we
1126 // currently never do.
1127 this->writeOp(kSetTypeface_DrawOp);
1128 fWriter.writePtr(paint.getTypeface());
1129 }
reed@google.comf5842f72011-05-04 18:30:04 +00001130 base.setTypeface(paint.getTypeface());
reed@google.combb6992a2011-04-26 17:41:56 +00001131 }
reed@google.combb6992a2011-04-26 17:41:56 +00001132
scroggo@google.com4dffc592012-07-17 16:49:40 +00001133 // This is a new paint, so all old flats can be safely purged, if necessary.
1134 fFlattenableHeap.markAllFlatsSafeToDelete();
reed@google.comb55d1182011-05-11 00:42:04 +00001135 for (int i = 0; i < kCount_PaintFlats; i++) {
1136 int index = this->flattenToIndex(get_paintflat(paint, i), (PaintFlats)i);
scroggo@google.comd3ba5cc2012-07-09 16:05:53 +00001137 bool replaced = index < 0;
1138 if (replaced) {
1139 index = ~index;
1140 }
scroggo@google.com4dffc592012-07-17 16:49:40 +00001141 // Store the index of any flat that needs to be kept. 0 means no flat.
1142 if (index > 0) {
1143 fFlattenableHeap.markFlatForKeeping(index);
1144 }
1145 SkASSERT(index >= 0 && index <= fFlatDictionary.count());
scroggo@google.comd3ba5cc2012-07-09 16:05:53 +00001146 if (index != fCurrFlatIndex[i] || replaced) {
reed@google.comb55d1182011-05-11 00:42:04 +00001147 *ptr++ = PaintOp_packOpFlagData(kFlatIndex_PaintOp, i, index);
1148 fCurrFlatIndex[i] = index;
1149 }
1150 }
1151
reed@google.comacd471f2011-05-03 21:26:46 +00001152 size_t size = (char*)ptr - (char*)storage;
1153 if (size && this->needOpBytes(size)) {
reed@google.comb55d1182011-05-11 00:42:04 +00001154 this->writeOp(kPaintOp_DrawOp, 0, size);
reed@google.comb55d1182011-05-11 00:42:04 +00001155 fWriter.write(storage, size);
reed@google.combb6992a2011-04-26 17:41:56 +00001156 for (size_t i = 0; i < size/4; i++) {
reed@google.comb55d1182011-05-11 00:42:04 +00001157// SkDebugf("[%d] %08X\n", i, storage[i]);
reed@google.combb6992a2011-04-26 17:41:56 +00001158 }
1159 }
reed@google.com0cd2ac62013-10-14 20:02:44 +00001160
1161 //
1162 // Do these after we've written kPaintOp_DrawOp
skia.committer@gmail.comfbc58a32013-10-15 07:02:27 +00001163
reed@google.com0cd2ac62013-10-14 20:02:44 +00001164 if (base.getAnnotation() != paint.getAnnotation()) {
1165 if (NULL == paint.getAnnotation()) {
commit-bot@chromium.org89ff3dd2013-10-29 20:29:38 +00001166 if (this->needOpBytes()) {
1167 this->writeOp(kSetAnnotation_DrawOp, 0, 0);
1168 }
reed@google.com0cd2ac62013-10-14 20:02:44 +00001169 } else {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +00001170 SkWriteBuffer buffer;
reed@google.com0cd2ac62013-10-14 20:02:44 +00001171 paint.getAnnotation()->writeToBuffer(buffer);
commit-bot@chromium.org89ff3dd2013-10-29 20:29:38 +00001172 const size_t size = buffer.bytesWritten();
1173 if (this->needOpBytes(size)) {
1174 this->writeOp(kSetAnnotation_DrawOp, 0, size);
1175 buffer.writeToMemory(fWriter.reserve(size));
1176 }
reed@google.com0cd2ac62013-10-14 20:02:44 +00001177 }
commit-bot@chromium.org40258a52013-10-29 19:23:26 +00001178 base.setAnnotation(paint.getAnnotation());
reed@google.com0cd2ac62013-10-14 20:02:44 +00001179 }
reed@google.combb6992a2011-04-26 17:41:56 +00001180}
1181
1182///////////////////////////////////////////////////////////////////////////////
1183
1184#include "SkGPipe.h"
1185
scroggo@google.com3cb969f2012-07-27 20:39:19 +00001186SkGPipeController::~SkGPipeController() {
1187 SkSafeUnref(fCanvas);
1188}
1189
1190void SkGPipeController::setCanvas(SkGPipeCanvas* canvas) {
1191 SkRefCnt_SafeAssign(fCanvas, canvas);
1192}
1193
1194///////////////////////////////////////////////////////////////////////////////
1195
1196SkGPipeWriter::SkGPipeWriter()
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +00001197: fWriter(0) {
reed@google.combb6992a2011-04-26 17:41:56 +00001198 fCanvas = NULL;
1199}
1200
1201SkGPipeWriter::~SkGPipeWriter() {
reed@google.comacd471f2011-05-03 21:26:46 +00001202 this->endRecording();
reed@google.combb6992a2011-04-26 17:41:56 +00001203}
1204
junov@chromium.orga8db8fe2012-08-15 19:49:22 +00001205SkCanvas* SkGPipeWriter::startRecording(SkGPipeController* controller, uint32_t flags,
1206 uint32_t width, uint32_t height) {
reed@google.combb6992a2011-04-26 17:41:56 +00001207 if (NULL == fCanvas) {
reed@google.comacd471f2011-05-03 21:26:46 +00001208 fWriter.reset(NULL, 0);
junov@chromium.orga8db8fe2012-08-15 19:49:22 +00001209 fCanvas = SkNEW_ARGS(SkGPipeCanvas, (controller, &fWriter, flags, width, height));
reed@google.combb6992a2011-04-26 17:41:56 +00001210 }
scroggo@google.com3cb969f2012-07-27 20:39:19 +00001211 controller->setCanvas(fCanvas);
reed@google.combb6992a2011-04-26 17:41:56 +00001212 return fCanvas;
1213}
1214
1215void SkGPipeWriter::endRecording() {
1216 if (fCanvas) {
scroggo@google.com59c3ab62013-11-12 14:32:38 +00001217 fCanvas->finish(true);
reed@google.combb6992a2011-04-26 17:41:56 +00001218 fCanvas->unref();
1219 fCanvas = NULL;
1220 }
1221}
1222
junov@chromium.org2e14ba82012-08-07 14:26:57 +00001223void SkGPipeWriter::flushRecording(bool detachCurrentBlock) {
1224 if (fCanvas) {
1225 fCanvas->flushRecording(detachCurrentBlock);
1226 }
junov@chromium.org77eec242012-07-18 17:54:45 +00001227}
1228
junov@chromium.org2e14ba82012-08-07 14:26:57 +00001229size_t SkGPipeWriter::freeMemoryIfPossible(size_t bytesToFree) {
1230 if (fCanvas) {
1231 return fCanvas->freeMemoryIfPossible(bytesToFree);
1232 }
1233 return 0;
1234}
1235
1236size_t SkGPipeWriter::storageAllocatedForRecording() const {
scroggo@google.com15011ee2012-07-26 20:03:32 +00001237 return NULL == fCanvas ? 0 : fCanvas->storageAllocatedForRecording();
1238}
1239
scroggo@google.com3e26bd02012-08-14 15:20:01 +00001240///////////////////////////////////////////////////////////////////////////////
1241
1242BitmapShuttle::BitmapShuttle(SkGPipeCanvas* canvas) {
1243 SkASSERT(canvas != NULL);
1244 fCanvas = canvas;
1245 fCanvas->ref();
1246}
1247
1248BitmapShuttle::~BitmapShuttle() {
scroggo@google.com59c3ab62013-11-12 14:32:38 +00001249 this->removeCanvas();
scroggo@google.com3e26bd02012-08-14 15:20:01 +00001250}
1251
1252bool BitmapShuttle::insert(const SkBitmap& bitmap, int32_t slot) {
scroggo@google.com59c3ab62013-11-12 14:32:38 +00001253 SkASSERT(fCanvas != NULL);
scroggo@google.com3e26bd02012-08-14 15:20:01 +00001254 return fCanvas->shuttleBitmap(bitmap, slot);
1255}
scroggo@google.com59c3ab62013-11-12 14:32:38 +00001256
1257void BitmapShuttle::removeCanvas() {
1258 if (NULL == fCanvas) {
1259 return;
1260 }
1261 fCanvas->unref();
1262 fCanvas = NULL;
1263}