blob: b5d2522348e0c422123da72af134328876b595b5 [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"
dandovb3c9d1c2014-08-12 08:34:29 -070022#include "SkPatchUtils.h"
scroggo@google.com10dccde2012-08-08 20:43:22 +000023#include "SkPathEffect.h"
24#include "SkPictureFlat.h"
25#include "SkRasterizer.h"
reed@google.com4ed0fb72012-12-12 20:48:18 +000026#include "SkRRect.h"
scroggo@google.com10dccde2012-08-08 20:43:22 +000027#include "SkShader.h"
reed@google.comf5842f72011-05-04 18:30:04 +000028#include "SkStream.h"
fmalita228a6f22014-08-28 13:59:42 -070029#include "SkTextBlob.h"
reed@google.comb55d1182011-05-11 00:42:04 +000030#include "SkTSearch.h"
reed@google.comf5842f72011-05-04 18:30:04 +000031#include "SkTypeface.h"
reed@google.combb6992a2011-04-26 17:41:56 +000032#include "SkWriter32.h"
reed@google.comb55d1182011-05-11 00:42:04 +000033
reed@google.com4ed0fb72012-12-12 20:48:18 +000034enum {
35 kSizeOfFlatRRect = sizeof(SkRect) + 4 * sizeof(SkVector)
36};
37
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +000038static bool isCrossProcess(uint32_t flags) {
39 return SkToBool(flags & SkGPipeWriter::kCrossProcess_Flag);
40}
41
reed@google.comb55d1182011-05-11 00:42:04 +000042static SkFlattenable* get_paintflat(const SkPaint& paint, unsigned paintFlat) {
43 SkASSERT(paintFlat < kCount_PaintFlats);
44 switch (paintFlat) {
45 case kColorFilter_PaintFlat: return paint.getColorFilter();
reed@google.com0faac1e2011-05-11 05:58:58 +000046 case kDrawLooper_PaintFlat: return paint.getLooper();
reed@google.comb55d1182011-05-11 00:42:04 +000047 case kMaskFilter_PaintFlat: return paint.getMaskFilter();
48 case kPathEffect_PaintFlat: return paint.getPathEffect();
49 case kRasterizer_PaintFlat: return paint.getRasterizer();
50 case kShader_PaintFlat: return paint.getShader();
scroggo@google.com16d1d0b2012-05-02 19:09:40 +000051 case kImageFilter_PaintFlat: return paint.getImageFilter();
reed@google.comb55d1182011-05-11 00:42:04 +000052 case kXfermode_PaintFlat: return paint.getXfermode();
53 }
tomhudson@google.com0c00f212011-12-28 14:59:50 +000054 SkDEBUGFAIL("never gets here");
reed@google.comb55d1182011-05-11 00:42:04 +000055 return NULL;
56}
reed@google.combb6992a2011-04-26 17:41:56 +000057
reed@google.comf5842f72011-05-04 18:30:04 +000058static size_t writeTypeface(SkWriter32* writer, SkTypeface* typeface) {
59 SkASSERT(typeface);
60 SkDynamicMemoryWStream stream;
61 typeface->serialize(&stream);
62 size_t size = stream.getOffset();
63 if (writer) {
commit-bot@chromium.orgb45bd1f2014-04-24 18:17:30 +000064 writer->write32(SkToU32(size));
reed@google.com8a85d0c2011-06-24 19:12:12 +000065 SkAutoDataUnref data(stream.copyToData());
robertphillips@google.com59f46b82012-07-10 17:30:58 +000066 writer->writePad(data->data(), size);
reed@google.comf5842f72011-05-04 18:30:04 +000067 }
scroggo@google.com5af9b202012-06-04 17:17:36 +000068 return 4 + SkAlign4(size);
reed@google.comf5842f72011-05-04 18:30:04 +000069}
70
reed@google.combb6992a2011-04-26 17:41:56 +000071///////////////////////////////////////////////////////////////////////////////
72
scroggo@google.com4dffc592012-07-17 16:49:40 +000073class FlattenableHeap : public SkFlatController {
74public:
scroggo@google.com664fab12012-08-14 19:22:05 +000075 FlattenableHeap(int numFlatsToKeep, SkNamedFactorySet* fset, bool isCrossProcess)
commit-bot@chromium.orga2bd2d12014-01-30 22:16:32 +000076 : INHERITED(isCrossProcess ? SkWriteBuffer::kCrossProcess_Flag : 0)
77 , fNumFlatsToKeep(numFlatsToKeep) {
scroggo@google.com664fab12012-08-14 19:22:05 +000078 SkASSERT((isCrossProcess && fset != NULL) || (!isCrossProcess && NULL == fset));
79 if (isCrossProcess) {
80 this->setNamedFactorySet(fset);
scroggo@google.com664fab12012-08-14 19:22:05 +000081 }
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +000082 }
scroggo@google.com4dffc592012-07-17 16:49:40 +000083
84 ~FlattenableHeap() {
85 fPointers.freeAll();
86 }
87
88 virtual void* allocThrow(size_t bytes) SK_OVERRIDE;
89
90 virtual void unalloc(void* ptr) SK_OVERRIDE;
91
scroggo@google.com7ca24432012-08-14 15:48:43 +000092 void setBitmapStorage(SkBitmapHeap* heap) {
93 this->setBitmapHeap(heap);
94 }
rmistry@google.comd6176b02012-08-23 18:14:13 +000095
scroggo@google.com4dffc592012-07-17 16:49:40 +000096 const SkFlatData* flatToReplace() const;
97
98 // Mark an SkFlatData as one that should not be returned by flatToReplace.
99 // Takes the result of SkFlatData::index() as its parameter.
100 void markFlatForKeeping(int index) {
101 *fFlatsThatMustBeKept.append() = index;
102 }
103
104 void markAllFlatsSafeToDelete() {
105 fFlatsThatMustBeKept.reset();
106 }
107
108private:
109 // Keep track of the indices (i.e. the result of SkFlatData::index()) of
110 // flats that must be kept, since they are on the current paint.
111 SkTDArray<int> fFlatsThatMustBeKept;
112 SkTDArray<void*> fPointers;
113 const int fNumFlatsToKeep;
commit-bot@chromium.orga2bd2d12014-01-30 22:16:32 +0000114
115 typedef SkFlatController INHERITED;
scroggo@google.com4dffc592012-07-17 16:49:40 +0000116};
117
118void FlattenableHeap::unalloc(void* ptr) {
119 int indexToRemove = fPointers.rfind(ptr);
120 if (indexToRemove >= 0) {
121 sk_free(ptr);
122 fPointers.remove(indexToRemove);
123 }
124}
125
126void* FlattenableHeap::allocThrow(size_t bytes) {
127 void* ptr = sk_malloc_throw(bytes);
128 *fPointers.append() = ptr;
129 return ptr;
130}
131
132const SkFlatData* FlattenableHeap::flatToReplace() const {
133 // First, determine whether we should replace one.
134 if (fPointers.count() > fNumFlatsToKeep) {
135 // Look through the flattenable heap.
136 // TODO: Return the LRU flat.
137 for (int i = 0; i < fPointers.count(); i++) {
138 SkFlatData* potential = (SkFlatData*)fPointers[i];
139 // Make sure that it is not one that must be kept.
140 bool mustKeep = false;
141 for (int j = 0; j < fFlatsThatMustBeKept.count(); j++) {
142 if (potential->index() == fFlatsThatMustBeKept[j]) {
143 mustKeep = true;
144 break;
145 }
146 }
147 if (!mustKeep) {
148 return potential;
149 }
150 }
151 }
152 return NULL;
153}
154
155///////////////////////////////////////////////////////////////////////////////
156
commit-bot@chromium.org07adb632014-01-02 22:20:49 +0000157struct SkFlattenableTraits {
commit-bot@chromium.org186c0cc2014-02-18 16:15:05 +0000158 static void Flatten(SkWriteBuffer& buffer, const SkFlattenable& flattenable) {
commit-bot@chromium.org07adb632014-01-02 22:20:49 +0000159 buffer.writeFlattenable(&flattenable);
scroggo@google.com4dffc592012-07-17 16:49:40 +0000160 }
commit-bot@chromium.org07adb632014-01-02 22:20:49 +0000161 // No need to define unflatten if we never call it.
scroggo@google.com4dffc592012-07-17 16:49:40 +0000162};
commit-bot@chromium.org07adb632014-01-02 22:20:49 +0000163typedef SkFlatDictionary<SkFlattenable, SkFlattenableTraits> FlatDictionary;
scroggo@google.com4dffc592012-07-17 16:49:40 +0000164
165///////////////////////////////////////////////////////////////////////////////
166
scroggo@google.com59c3ab62013-11-12 14:32:38 +0000167/**
168 * If SkBitmaps are to be flattened to send to the reader, this class is
169 * provided to the SkBitmapHeap to tell the SkGPipeCanvas to do so.
170 */
171class BitmapShuttle : public SkBitmapHeap::ExternalStorage {
172public:
173 BitmapShuttle(SkGPipeCanvas*);
174
175 ~BitmapShuttle();
176
177 virtual bool insert(const SkBitmap& bitmap, int32_t slot) SK_OVERRIDE;
178
179 /**
180 * Remove the SkGPipeCanvas used for insertion. After this, calls to
181 * insert will crash.
182 */
183 void removeCanvas();
184
185private:
186 SkGPipeCanvas* fCanvas;
187};
188
189///////////////////////////////////////////////////////////////////////////////
190
reed@google.combb6992a2011-04-26 17:41:56 +0000191class SkGPipeCanvas : public SkCanvas {
192public:
junov@chromium.orga8db8fe2012-08-15 19:49:22 +0000193 SkGPipeCanvas(SkGPipeController*, SkWriter32*, uint32_t flags,
194 uint32_t width, uint32_t height);
reed@google.combb6992a2011-04-26 17:41:56 +0000195 virtual ~SkGPipeCanvas();
196
scroggo@google.com59c3ab62013-11-12 14:32:38 +0000197 /**
198 * Called when nothing else is to be written to the stream. Any repeated
199 * calls are ignored.
200 *
201 * @param notifyReaders Whether to send a message to the reader(s) that
202 * the writer is through sending commands. Should generally be true,
203 * unless there is an error which prevents further messages from
204 * being sent.
205 */
206 void finish(bool notifyReaders) {
207 if (fDone) {
208 return;
reed@google.combb6992a2011-04-26 17:41:56 +0000209 }
scroggo@google.com59c3ab62013-11-12 14:32:38 +0000210 if (notifyReaders && this->needOpBytes()) {
211 this->writeOp(kDone_DrawOp);
212 this->doNotify();
213 }
214 if (shouldFlattenBitmaps(fFlags)) {
215 // The following circular references exist:
216 // fFlattenableHeap -> fWriteBuffer -> fBitmapStorage -> fExternalStorage -> fCanvas
217 // fBitmapHeap -> fExternalStorage -> fCanvas
218 // fFlattenableHeap -> fBitmapStorage -> fExternalStorage -> fCanvas
219
220 // Break them all by destroying the final link to this SkGPipeCanvas.
221 fBitmapShuttle->removeCanvas();
222 }
223 fDone = true;
reed@google.combb6992a2011-04-26 17:41:56 +0000224 }
225
junov@chromium.org77eec242012-07-18 17:54:45 +0000226 void flushRecording(bool detachCurrentBlock);
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000227 size_t freeMemoryIfPossible(size_t bytesToFree);
junov@chromium.org77eec242012-07-18 17:54:45 +0000228
scroggo@google.com15011ee2012-07-26 20:03:32 +0000229 size_t storageAllocatedForRecording() {
scroggo@google.comd5d158b2012-08-14 20:38:28 +0000230 return (NULL == fBitmapHeap) ? 0 : fBitmapHeap->bytesAllocated();
scroggo@google.com15011ee2012-07-26 20:03:32 +0000231 }
232
reed@google.combb6992a2011-04-26 17:41:56 +0000233 // overrides from SkCanvas
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000234 virtual bool isDrawingToLayer() const SK_OVERRIDE;
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000235 virtual void clear(SkColor) SK_OVERRIDE;
236 virtual void drawPaint(const SkPaint& paint) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000237 virtual void drawPoints(PointMode, size_t count, const SkPoint pts[],
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000238 const SkPaint&) SK_OVERRIDE;
reed@google.com4ed0fb72012-12-12 20:48:18 +0000239 virtual void drawOval(const SkRect&, const SkPaint&) SK_OVERRIDE;
bsalomon@google.com7ce564c2013-10-22 16:54:15 +0000240 virtual void drawRect(const SkRect& rect, const SkPaint&) SK_OVERRIDE;
reed@google.com4ed0fb72012-12-12 20:48:18 +0000241 virtual void drawRRect(const SkRRect&, const SkPaint&) SK_OVERRIDE;
bsalomon@google.com7ce564c2013-10-22 16:54:15 +0000242 virtual void drawPath(const SkPath& path, const SkPaint&) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000243 virtual void drawBitmap(const SkBitmap&, SkScalar left, SkScalar top,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000244 const SkPaint*) SK_OVERRIDE;
reed@google.com71121732012-09-18 15:14:33 +0000245 virtual void drawBitmapRectToRect(const SkBitmap&, const SkRect* src,
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000246 const SkRect& dst, const SkPaint* paint,
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +0000247 DrawBitmapRectFlags flags) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000248 virtual void drawBitmapMatrix(const SkBitmap&, const SkMatrix&,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000249 const SkPaint*) SK_OVERRIDE;
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000250 virtual void drawBitmapNine(const SkBitmap& bitmap, const SkIRect& center,
251 const SkRect& dst, const SkPaint* paint = NULL) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000252 virtual void drawSprite(const SkBitmap&, int left, int top,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000253 const SkPaint*) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000254 virtual void drawVertices(VertexMode, int vertexCount,
255 const SkPoint vertices[], const SkPoint texs[],
256 const SkColor colors[], SkXfermode*,
257 const uint16_t indices[], int indexCount,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000258 const SkPaint&) SK_OVERRIDE;
259 virtual void drawData(const void*, size_t) SK_OVERRIDE;
robertphillips@google.com0a4805e2013-05-29 13:24:23 +0000260 virtual void beginCommentGroup(const char* description) SK_OVERRIDE;
261 virtual void addComment(const char* kywd, const char* value) SK_OVERRIDE;
262 virtual void endCommentGroup() SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000263
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000264 /**
265 * Flatten an SkBitmap to send to the reader, where it will be referenced
266 * according to slot.
267 */
268 bool shuttleBitmap(const SkBitmap&, int32_t slot);
commit-bot@chromium.orged9806f2014-02-21 02:32:36 +0000269
270protected:
Florin Malita5f6102d2014-06-30 10:13:28 -0400271 virtual void willSave() SK_OVERRIDE;
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000272 virtual SaveLayerStrategy willSaveLayer(const SkRect*, const SkPaint*, SaveFlags) SK_OVERRIDE;
273 virtual void willRestore() SK_OVERRIDE;
274
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +0000275 virtual void didConcat(const SkMatrix&) SK_OVERRIDE;
276 virtual void didSetMatrix(const SkMatrix&) SK_OVERRIDE;
277
commit-bot@chromium.orged9806f2014-02-21 02:32:36 +0000278 virtual void onDrawDRRect(const SkRRect&, const SkRRect&, const SkPaint&) SK_OVERRIDE;
reed@google.come0d9ce82014-04-23 04:00:17 +0000279 virtual void onDrawText(const void* text, size_t byteLength, SkScalar x, SkScalar y,
280 const SkPaint&) SK_OVERRIDE;
281 virtual void onDrawPosText(const void* text, size_t byteLength, const SkPoint pos[],
282 const SkPaint&) SK_OVERRIDE;
283 virtual void onDrawPosTextH(const void* text, size_t byteLength, const SkScalar xpos[],
284 SkScalar constY, const SkPaint&) SK_OVERRIDE;
285 virtual void onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& path,
286 const SkMatrix* matrix, const SkPaint&) SK_OVERRIDE;
fmalitab7425172014-08-26 07:56:44 -0700287 virtual void onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
288 const SkPaint& paint) SK_OVERRIDE;
dandovb3c9d1c2014-08-12 08:34:29 -0700289 virtual void onDrawPatch(const SkPoint cubics[12], const SkColor colors[4],
290 const SkPoint texCoords[4], SkXfermode* xmode,
291 const SkPaint& paint) SK_OVERRIDE;
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
reedd5fa1a42014-08-09 11:08:05 -0700297 virtual void onDrawPicture(const SkPicture*, const SkMatrix*, const SkPaint*) SK_OVERRIDE;
robertphillips9b14f262014-06-04 05:40:44 -0700298
reed@google.combb6992a2011-04-26 17:41:56 +0000299private:
commit-bot@chromium.orgd9ea09e2014-03-25 17:32:26 +0000300 void recordTranslate(const SkMatrix&);
301 void recordScale(const SkMatrix&);
302 void recordConcat(const SkMatrix&);
303
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000304 enum {
305 kNoSaveLayer = -1,
306 };
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000307 SkNamedFactorySet* fFactorySet;
308 int fFirstSaveLayerStackLevel;
scroggo@google.comd9d29672012-08-14 17:21:34 +0000309 SkBitmapHeap* fBitmapHeap;
reed@google.comacd471f2011-05-03 21:26:46 +0000310 SkGPipeController* fController;
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000311 SkWriter32& fWriter;
312 size_t fBlockSize; // amount allocated for writer
313 size_t fBytesNotified;
314 bool fDone;
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000315 const uint32_t fFlags;
reed@google.combb6992a2011-04-26 17:41:56 +0000316
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000317 SkRefCntSet fTypefaceSet;
reed@google.comf5842f72011-05-04 18:30:04 +0000318
319 uint32_t getTypefaceID(SkTypeface*);
320
reed@google.comacd471f2011-05-03 21:26:46 +0000321 inline void writeOp(DrawOps op, unsigned flags, unsigned data) {
reed@google.combb6992a2011-04-26 17:41:56 +0000322 fWriter.write32(DrawOp_packOpFlagData(op, flags, data));
323 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000324
reed@google.comacd471f2011-05-03 21:26:46 +0000325 inline void writeOp(DrawOps op) {
reed@google.combb6992a2011-04-26 17:41:56 +0000326 fWriter.write32(DrawOp_packOpFlagData(op, 0, 0));
327 }
reed@google.comacd471f2011-05-03 21:26:46 +0000328
329 bool needOpBytes(size_t size = 0);
330
331 inline void doNotify() {
332 if (!fDone) {
reed@google.com44699382013-10-31 17:28:30 +0000333 size_t bytes = fWriter.bytesWritten() - fBytesNotified;
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000334 if (bytes > 0) {
335 fController->notifyWritten(bytes);
336 fBytesNotified += bytes;
337 }
reed@google.comacd471f2011-05-03 21:26:46 +0000338 }
339 }
reed@google.comb55d1182011-05-11 00:42:04 +0000340
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000341 // Should be called after any calls to an SkFlatDictionary::findAndReplace
342 // if a new SkFlatData was added when in cross process mode
343 void flattenFactoryNames();
344
scroggo@google.com59c3ab62013-11-12 14:32:38 +0000345 FlattenableHeap fFlattenableHeap;
346 FlatDictionary fFlatDictionary;
347 SkAutoTUnref<BitmapShuttle> fBitmapShuttle;
348 int fCurrFlatIndex[kCount_PaintFlats];
349
reed@google.comb55d1182011-05-11 00:42:04 +0000350 int flattenToIndex(SkFlattenable* obj, PaintFlats);
351
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000352 // Common code used by drawBitmap*. Behaves differently depending on the
353 // type of SkBitmapHeap being used, which is determined by the flags used.
354 bool commonDrawBitmap(const SkBitmap& bm, DrawOps op, unsigned flags,
355 size_t opBytesNeeded, const SkPaint* paint);
scroggo@google.com58be6822012-07-30 14:40:01 +0000356
reed@google.com31891582011-05-12 03:03:56 +0000357 SkPaint fPaint;
358 void writePaint(const SkPaint&);
reed@google.combb6992a2011-04-26 17:41:56 +0000359
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000360 class AutoPipeNotify {
361 public:
362 AutoPipeNotify(SkGPipeCanvas* canvas) : fCanvas(canvas) {}
363 ~AutoPipeNotify() { fCanvas->doNotify(); }
364 private:
365 SkGPipeCanvas* fCanvas;
366 };
367 friend class AutoPipeNotify;
368
reed@google.combb6992a2011-04-26 17:41:56 +0000369 typedef SkCanvas INHERITED;
370};
371
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000372void SkGPipeCanvas::flattenFactoryNames() {
373 const char* name;
374 while ((name = fFactorySet->getNextAddedFactoryName()) != NULL) {
375 size_t len = strlen(name);
commit-bot@chromium.orgd0490172014-05-14 15:12:08 +0000376 if (this->needOpBytes(SkWriter32::WriteStringSize(name, len))) {
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000377 this->writeOp(kDef_Factory_DrawOp);
378 fWriter.writeString(name, len);
379 }
380 }
381}
382
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000383bool SkGPipeCanvas::shuttleBitmap(const SkBitmap& bm, int32_t slot) {
scroggo@google.com565254b2012-06-28 15:41:32 +0000384 SkASSERT(shouldFlattenBitmaps(fFlags));
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000385 SkWriteBuffer buffer;
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000386 buffer.setNamedFactoryRecorder(fFactorySet);
scroggo@google.com74b7ffd2013-04-30 02:32:41 +0000387 buffer.writeBitmap(bm);
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000388 this->flattenFactoryNames();
commit-bot@chromium.orgb45bd1f2014-04-24 18:17:30 +0000389 size_t size = buffer.bytesWritten();
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000390 if (this->needOpBytes(size)) {
391 this->writeOp(kDef_Bitmap_DrawOp, 0, slot);
392 void* dst = static_cast<void*>(fWriter.reserve(size));
393 buffer.writeToMemory(dst);
394 return true;
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000395 }
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000396 return false;
scroggo@google.comd3ba5cc2012-07-09 16:05:53 +0000397}
398
reed@google.comb55d1182011-05-11 00:42:04 +0000399// return 0 for NULL (or unflattenable obj), or index-base-1
scroggo@google.comd3ba5cc2012-07-09 16:05:53 +0000400// return ~(index-base-1) if an old flattenable was replaced
reed@google.comb55d1182011-05-11 00:42:04 +0000401int SkGPipeCanvas::flattenToIndex(SkFlattenable* obj, PaintFlats paintflat) {
scroggo@google.comd5d158b2012-08-14 20:38:28 +0000402 SkASSERT(!fDone && fBitmapHeap != NULL);
reed@google.comb55d1182011-05-11 00:42:04 +0000403 if (NULL == obj) {
404 return 0;
405 }
reed@google.comb55d1182011-05-11 00:42:04 +0000406
scroggo@google.comd9d29672012-08-14 17:21:34 +0000407 fBitmapHeap->deferAddingOwners();
scroggo@google.com4dffc592012-07-17 16:49:40 +0000408 bool added, replaced;
scroggo@google.com664fab12012-08-14 19:22:05 +0000409 const SkFlatData* flat = fFlatDictionary.findAndReplace(*obj, fFlattenableHeap.flatToReplace(),
410 &added, &replaced);
scroggo@google.comd9d29672012-08-14 17:21:34 +0000411 fBitmapHeap->endAddingOwnersDeferral(added);
scroggo@google.com4dffc592012-07-17 16:49:40 +0000412 int index = flat->index();
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000413 if (added) {
414 if (isCrossProcess(fFlags)) {
415 this->flattenFactoryNames();
416 }
417 size_t flatSize = flat->flatSize();
418 if (this->needOpBytes(flatSize)) {
419 this->writeOp(kDef_Flattenable_DrawOp, paintflat, index);
420 fWriter.write(flat->data(), flatSize);
421 }
scroggo@google.com4dffc592012-07-17 16:49:40 +0000422 }
423 if (replaced) {
reed@google.comb55d1182011-05-11 00:42:04 +0000424 index = ~index;
reed@google.comb55d1182011-05-11 00:42:04 +0000425 }
scroggo@google.com4dffc592012-07-17 16:49:40 +0000426 return index;
reed@google.comb55d1182011-05-11 00:42:04 +0000427}
428
reed@google.combb6992a2011-04-26 17:41:56 +0000429///////////////////////////////////////////////////////////////////////////////
430
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000431#define MIN_BLOCK_SIZE (16 * 1024)
scroggo@google.com4dffc592012-07-17 16:49:40 +0000432#define BITMAPS_TO_KEEP 5
433#define FLATTENABLES_TO_KEEP 10
reed@google.combb6992a2011-04-26 17:41:56 +0000434
reed@google.comacd471f2011-05-03 21:26:46 +0000435SkGPipeCanvas::SkGPipeCanvas(SkGPipeController* controller,
rmistry@google.comd6176b02012-08-23 18:14:13 +0000436 SkWriter32* writer, uint32_t flags,
junov@chromium.orga8db8fe2012-08-15 19:49:22 +0000437 uint32_t width, uint32_t height)
commit-bot@chromium.org403f8d72014-02-17 15:24:26 +0000438 : SkCanvas(width, height)
439 , fFactorySet(isCrossProcess(flags) ? SkNEW(SkNamedFactorySet) : NULL)
440 , fWriter(*writer)
441 , fFlags(flags)
442 , fFlattenableHeap(FLATTENABLES_TO_KEEP, fFactorySet, isCrossProcess(flags))
443 , fFlatDictionary(&fFlattenableHeap)
444{
reed@google.comacd471f2011-05-03 21:26:46 +0000445 fController = controller;
reed@google.combb6992a2011-04-26 17:41:56 +0000446 fDone = false;
reed@google.comacd471f2011-05-03 21:26:46 +0000447 fBlockSize = 0; // need first block from controller
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000448 fBytesNotified = 0;
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000449 fFirstSaveLayerStackLevel = kNoSaveLayer;
reed@google.comb55d1182011-05-11 00:42:04 +0000450 sk_bzero(fCurrFlatIndex, sizeof(fCurrFlatIndex));
reed@google.comacd471f2011-05-03 21:26:46 +0000451
scroggo@google.com565254b2012-06-28 15:41:32 +0000452 // Tell the reader the appropriate flags to use.
453 if (this->needOpBytes()) {
454 this->writeOp(kReportFlags_DrawOp, fFlags, 0);
455 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000456
scroggo@google.com10dccde2012-08-08 20:43:22 +0000457 if (shouldFlattenBitmaps(flags)) {
scroggo@google.com59c3ab62013-11-12 14:32:38 +0000458 fBitmapShuttle.reset(SkNEW_ARGS(BitmapShuttle, (this)));
459 fBitmapHeap = SkNEW_ARGS(SkBitmapHeap, (fBitmapShuttle.get(), BITMAPS_TO_KEEP));
scroggo@google.com10dccde2012-08-08 20:43:22 +0000460 } else {
scroggo@google.comd9d29672012-08-14 17:21:34 +0000461 fBitmapHeap = SkNEW_ARGS(SkBitmapHeap,
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000462 (BITMAPS_TO_KEEP, controller->numberOfReaders()));
scroggo@google.com10dccde2012-08-08 20:43:22 +0000463 if (this->needOpBytes(sizeof(void*))) {
scroggo@google.comd9d29672012-08-14 17:21:34 +0000464 this->writeOp(kShareBitmapHeap_DrawOp);
465 fWriter.writePtr(static_cast<void*>(fBitmapHeap));
scroggo@google.com10dccde2012-08-08 20:43:22 +0000466 }
467 }
scroggo@google.comd9d29672012-08-14 17:21:34 +0000468 fFlattenableHeap.setBitmapStorage(fBitmapHeap);
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000469 this->doNotify();
reed@google.combb6992a2011-04-26 17:41:56 +0000470}
471
472SkGPipeCanvas::~SkGPipeCanvas() {
scroggo@google.com59c3ab62013-11-12 14:32:38 +0000473 this->finish(true);
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000474 SkSafeUnref(fFactorySet);
scroggo@google.comd9d29672012-08-14 17:21:34 +0000475 SkSafeUnref(fBitmapHeap);
reed@google.combb6992a2011-04-26 17:41:56 +0000476}
477
reed@google.comacd471f2011-05-03 21:26:46 +0000478bool SkGPipeCanvas::needOpBytes(size_t needed) {
479 if (fDone) {
480 return false;
481 }
482
483 needed += 4; // size of DrawOp atom
commit-bot@chromium.orgd0490172014-05-14 15:12:08 +0000484 needed = SkAlign4(needed);
reed@google.com44699382013-10-31 17:28:30 +0000485 if (fWriter.bytesWritten() + needed > fBlockSize) {
mtklein6d88e6c2014-07-30 09:17:54 -0700486 // Before we wipe out any data that has already been written, read it out.
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000487 this->doNotify();
mtklein6d88e6c2014-07-30 09:17:54 -0700488
489 // If we're going to allocate a new block, allocate enough to make it worthwhile.
490 needed = SkTMax<size_t>(MIN_BLOCK_SIZE, needed);
491
commit-bot@chromium.orgd0490172014-05-14 15:12:08 +0000492 void* block = fController->requestBlock(needed, &fBlockSize);
reed@google.comacd471f2011-05-03 21:26:46 +0000493 if (NULL == block) {
scroggo@google.com59c3ab62013-11-12 14:32:38 +0000494 // Do not notify the readers, which would call this function again.
495 this->finish(false);
reed@google.comacd471f2011-05-03 21:26:46 +0000496 return false;
497 }
scroggo@google.com460a23e2012-08-16 17:56:49 +0000498 SkASSERT(SkIsAlign4(fBlockSize));
reed@google.comacd471f2011-05-03 21:26:46 +0000499 fWriter.reset(block, fBlockSize);
500 fBytesNotified = 0;
501 }
502 return true;
503}
504
reed@google.comf5842f72011-05-04 18:30:04 +0000505uint32_t SkGPipeCanvas::getTypefaceID(SkTypeface* face) {
506 uint32_t id = 0; // 0 means default/null typeface
507 if (face) {
508 id = fTypefaceSet.find(face);
509 if (0 == id) {
510 id = fTypefaceSet.add(face);
511 size_t size = writeTypeface(NULL, face);
512 if (this->needOpBytes(size)) {
reed@google.combb6793b2011-05-05 15:18:15 +0000513 this->writeOp(kDef_Typeface_DrawOp);
reed@google.comf5842f72011-05-04 18:30:04 +0000514 writeTypeface(&fWriter, face);
515 }
516 }
517 }
518 return id;
519}
520
reed@google.combb6992a2011-04-26 17:41:56 +0000521///////////////////////////////////////////////////////////////////////////////
522
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000523#define NOTIFY_SETUP(canvas) \
524 AutoPipeNotify apn(canvas)
525
Florin Malita5f6102d2014-06-30 10:13:28 -0400526void SkGPipeCanvas::willSave() {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000527 NOTIFY_SETUP(this);
reed@google.comacd471f2011-05-03 21:26:46 +0000528 if (this->needOpBytes()) {
Florin Malita5f6102d2014-06-30 10:13:28 -0400529 this->writeOp(kSave_DrawOp);
reed@google.comacd471f2011-05-03 21:26:46 +0000530 }
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000531
Florin Malita5f6102d2014-06-30 10:13:28 -0400532 this->INHERITED::willSave();
reed@google.combb6992a2011-04-26 17:41:56 +0000533}
534
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000535SkCanvas::SaveLayerStrategy SkGPipeCanvas::willSaveLayer(const SkRect* bounds, const SkPaint* paint,
536 SaveFlags saveFlags) {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000537 NOTIFY_SETUP(this);
reed@google.comacd471f2011-05-03 21:26:46 +0000538 size_t size = 0;
reed@google.combb6992a2011-04-26 17:41:56 +0000539 unsigned opFlags = 0;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000540
reed@google.combb6992a2011-04-26 17:41:56 +0000541 if (bounds) {
542 opFlags |= kSaveLayer_HasBounds_DrawOpFlag;
reed@google.comacd471f2011-05-03 21:26:46 +0000543 size += sizeof(SkRect);
reed@google.combb6992a2011-04-26 17:41:56 +0000544 }
545 if (paint) {
546 opFlags |= kSaveLayer_HasPaint_DrawOpFlag;
reed@google.com31891582011-05-12 03:03:56 +0000547 this->writePaint(*paint);
reed@google.combb6992a2011-04-26 17:41:56 +0000548 }
549
reed@google.comacd471f2011-05-03 21:26:46 +0000550 if (this->needOpBytes(size)) {
551 this->writeOp(kSaveLayer_DrawOp, opFlags, saveFlags);
552 if (bounds) {
553 fWriter.writeRect(*bounds);
554 }
reed@google.combb6992a2011-04-26 17:41:56 +0000555 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000556
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000557 if (kNoSaveLayer == fFirstSaveLayerStackLevel){
558 fFirstSaveLayerStackLevel = this->getSaveCount();
559 }
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000560
561 this->INHERITED::willSaveLayer(bounds, paint, saveFlags);
562 // we don't create a layer
563 return kNoLayer_SaveLayerStrategy;
reed@google.combb6992a2011-04-26 17:41:56 +0000564}
565
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000566void SkGPipeCanvas::willRestore() {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000567 NOTIFY_SETUP(this);
reed@google.comacd471f2011-05-03 21:26:46 +0000568 if (this->needOpBytes()) {
569 this->writeOp(kRestore_DrawOp);
570 }
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000571
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000572 if (this->getSaveCount() - 1 == fFirstSaveLayerStackLevel){
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000573 fFirstSaveLayerStackLevel = kNoSaveLayer;
574 }
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000575
576 this->INHERITED::willRestore();
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000577}
578
579bool SkGPipeCanvas::isDrawingToLayer() const {
580 return kNoSaveLayer != fFirstSaveLayerStackLevel;
reed@google.combb6992a2011-04-26 17:41:56 +0000581}
582
commit-bot@chromium.orgd9ea09e2014-03-25 17:32:26 +0000583void SkGPipeCanvas::recordTranslate(const SkMatrix& m) {
584 if (this->needOpBytes(2 * sizeof(SkScalar))) {
585 this->writeOp(kTranslate_DrawOp);
586 fWriter.writeScalar(m.getTranslateX());
587 fWriter.writeScalar(m.getTranslateY());
reed@google.combb6992a2011-04-26 17:41:56 +0000588 }
reed@google.combb6992a2011-04-26 17:41:56 +0000589}
590
commit-bot@chromium.orgd9ea09e2014-03-25 17:32:26 +0000591void SkGPipeCanvas::recordScale(const SkMatrix& m) {
592 if (this->needOpBytes(2 * sizeof(SkScalar))) {
593 this->writeOp(kScale_DrawOp);
594 fWriter.writeScalar(m.getScaleX());
595 fWriter.writeScalar(m.getScaleY());
reed@google.combb6992a2011-04-26 17:41:56 +0000596 }
reed@google.combb6992a2011-04-26 17:41:56 +0000597}
598
commit-bot@chromium.orgd9ea09e2014-03-25 17:32:26 +0000599void SkGPipeCanvas::recordConcat(const SkMatrix& m) {
600 if (this->needOpBytes(m.writeToMemory(NULL))) {
601 this->writeOp(kConcat_DrawOp);
602 fWriter.writeMatrix(m);
reed@google.combb6992a2011-04-26 17:41:56 +0000603 }
reed@google.combb6992a2011-04-26 17:41:56 +0000604}
605
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +0000606void SkGPipeCanvas::didConcat(const SkMatrix& matrix) {
reed@google.combb6992a2011-04-26 17:41:56 +0000607 if (!matrix.isIdentity()) {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000608 NOTIFY_SETUP(this);
commit-bot@chromium.orgd9ea09e2014-03-25 17:32:26 +0000609 switch (matrix.getType()) {
610 case SkMatrix::kTranslate_Mask:
611 this->recordTranslate(matrix);
612 break;
613 case SkMatrix::kScale_Mask:
614 this->recordScale(matrix);
615 break;
616 default:
617 this->recordConcat(matrix);
618 break;
reed@google.comacd471f2011-05-03 21:26:46 +0000619 }
reed@google.combb6992a2011-04-26 17:41:56 +0000620 }
commit-bot@chromium.orgd9ea09e2014-03-25 17:32:26 +0000621
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +0000622 this->INHERITED::didConcat(matrix);
reed@google.combb6992a2011-04-26 17:41:56 +0000623}
624
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +0000625void SkGPipeCanvas::didSetMatrix(const SkMatrix& matrix) {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000626 NOTIFY_SETUP(this);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000627 if (this->needOpBytes(matrix.writeToMemory(NULL))) {
reed@google.comacd471f2011-05-03 21:26:46 +0000628 this->writeOp(kSetMatrix_DrawOp);
djsollen@google.com2b2ede32012-04-12 13:24:04 +0000629 fWriter.writeMatrix(matrix);
reed@google.comacd471f2011-05-03 21:26:46 +0000630 }
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +0000631 this->INHERITED::didSetMatrix(matrix);
reed@google.combb6992a2011-04-26 17:41:56 +0000632}
633
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000634void SkGPipeCanvas::onClipRect(const SkRect& rect, SkRegion::Op rgnOp,
635 ClipEdgeStyle edgeStyle) {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000636 NOTIFY_SETUP(this);
scroggo@google.com460a23e2012-08-16 17:56:49 +0000637 if (this->needOpBytes(sizeof(SkRect))) {
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000638 unsigned flags = 0;
639 if (kSoft_ClipEdgeStyle == edgeStyle) {
640 flags = kClip_HasAntiAlias_DrawOpFlag;
641 }
scroggo@google.com460a23e2012-08-16 17:56:49 +0000642 this->writeOp(kClipRect_DrawOp, flags, rgnOp);
reed@google.comacd471f2011-05-03 21:26:46 +0000643 fWriter.writeRect(rect);
644 }
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000645 this->INHERITED::onClipRect(rect, rgnOp, edgeStyle);
reed@google.combb6992a2011-04-26 17:41:56 +0000646}
647
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000648void SkGPipeCanvas::onClipRRect(const SkRRect& rrect, SkRegion::Op rgnOp,
649 ClipEdgeStyle edgeStyle) {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000650 NOTIFY_SETUP(this);
reed@google.com4ed0fb72012-12-12 20:48:18 +0000651 if (this->needOpBytes(kSizeOfFlatRRect)) {
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000652 unsigned flags = 0;
653 if (kSoft_ClipEdgeStyle == edgeStyle) {
654 flags = kClip_HasAntiAlias_DrawOpFlag;
655 }
reed@google.com4ed0fb72012-12-12 20:48:18 +0000656 this->writeOp(kClipRRect_DrawOp, flags, rgnOp);
657 fWriter.writeRRect(rrect);
658 }
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000659 this->INHERITED::onClipRRect(rrect, rgnOp, edgeStyle);
reed@google.com4ed0fb72012-12-12 20:48:18 +0000660}
661
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000662void SkGPipeCanvas::onClipPath(const SkPath& path, SkRegion::Op rgnOp,
663 ClipEdgeStyle edgeStyle) {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000664 NOTIFY_SETUP(this);
scroggo@google.com460a23e2012-08-16 17:56:49 +0000665 if (this->needOpBytes(path.writeToMemory(NULL))) {
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000666 unsigned flags = 0;
667 if (kSoft_ClipEdgeStyle == edgeStyle) {
668 flags = kClip_HasAntiAlias_DrawOpFlag;
669 }
scroggo@google.com460a23e2012-08-16 17:56:49 +0000670 this->writeOp(kClipPath_DrawOp, flags, rgnOp);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000671 fWriter.writePath(path);
reed@google.comacd471f2011-05-03 21:26:46 +0000672 }
reed@google.combb6992a2011-04-26 17:41:56 +0000673 // we just pass on the bounds of the path
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000674 this->INHERITED::onClipRect(path.getBounds(), rgnOp, edgeStyle);
reed@google.combb6992a2011-04-26 17:41:56 +0000675}
676
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000677void SkGPipeCanvas::onClipRegion(const SkRegion& region, SkRegion::Op rgnOp) {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000678 NOTIFY_SETUP(this);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000679 if (this->needOpBytes(region.writeToMemory(NULL))) {
reed@google.comacd471f2011-05-03 21:26:46 +0000680 this->writeOp(kClipRegion_DrawOp, 0, rgnOp);
djsollen@google.com2b2ede32012-04-12 13:24:04 +0000681 fWriter.writeRegion(region);
reed@google.comacd471f2011-05-03 21:26:46 +0000682 }
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000683 this->INHERITED::onClipRegion(region, rgnOp);
reed@google.combb6992a2011-04-26 17:41:56 +0000684}
685
686///////////////////////////////////////////////////////////////////////////////
687
688void SkGPipeCanvas::clear(SkColor color) {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000689 NOTIFY_SETUP(this);
reed@google.combb6992a2011-04-26 17:41:56 +0000690 unsigned flags = 0;
691 if (color) {
692 flags |= kClear_HasColor_DrawOpFlag;
693 }
reed@google.comacd471f2011-05-03 21:26:46 +0000694 if (this->needOpBytes(sizeof(SkColor))) {
695 this->writeOp(kDrawClear_DrawOp, flags, 0);
696 if (color) {
697 fWriter.write32(color);
698 }
reed@google.combb6992a2011-04-26 17:41:56 +0000699 }
700}
701
702void SkGPipeCanvas::drawPaint(const SkPaint& paint) {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000703 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000704 this->writePaint(paint);
reed@google.comacd471f2011-05-03 21:26:46 +0000705 if (this->needOpBytes()) {
reed@google.com31891582011-05-12 03:03:56 +0000706 this->writeOp(kDrawPaint_DrawOp);
reed@google.comacd471f2011-05-03 21:26:46 +0000707 }
reed@google.combb6992a2011-04-26 17:41:56 +0000708}
709
710void SkGPipeCanvas::drawPoints(PointMode mode, size_t count,
robertphillips@google.com8b169312013-10-15 17:47:36 +0000711 const SkPoint pts[], const SkPaint& paint) {
reed@google.combb6992a2011-04-26 17:41:56 +0000712 if (count) {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000713 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000714 this->writePaint(paint);
reed@google.comacd471f2011-05-03 21:26:46 +0000715 if (this->needOpBytes(4 + count * sizeof(SkPoint))) {
reed@google.com31891582011-05-12 03:03:56 +0000716 this->writeOp(kDrawPoints_DrawOp, mode, 0);
commit-bot@chromium.orgb45bd1f2014-04-24 18:17:30 +0000717 fWriter.write32(SkToU32(count));
reed@google.comacd471f2011-05-03 21:26:46 +0000718 fWriter.write(pts, count * sizeof(SkPoint));
719 }
reed@google.combb6992a2011-04-26 17:41:56 +0000720 }
721}
722
reed@google.com4ed0fb72012-12-12 20:48:18 +0000723void SkGPipeCanvas::drawOval(const SkRect& rect, const SkPaint& paint) {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000724 NOTIFY_SETUP(this);
reed@google.com4ed0fb72012-12-12 20:48:18 +0000725 this->writePaint(paint);
726 if (this->needOpBytes(sizeof(SkRect))) {
727 this->writeOp(kDrawOval_DrawOp);
728 fWriter.writeRect(rect);
729 }
730}
731
bsalomon@google.com7ce564c2013-10-22 16:54:15 +0000732void SkGPipeCanvas::drawRect(const SkRect& rect, const SkPaint& paint) {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000733 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000734 this->writePaint(paint);
reed@google.comacd471f2011-05-03 21:26:46 +0000735 if (this->needOpBytes(sizeof(SkRect))) {
reed@google.com31891582011-05-12 03:03:56 +0000736 this->writeOp(kDrawRect_DrawOp);
reed@google.comacd471f2011-05-03 21:26:46 +0000737 fWriter.writeRect(rect);
738 }
reed@google.combb6992a2011-04-26 17:41:56 +0000739}
740
reed@google.com4ed0fb72012-12-12 20:48:18 +0000741void SkGPipeCanvas::drawRRect(const SkRRect& rrect, const SkPaint& paint) {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000742 NOTIFY_SETUP(this);
reed@google.com4ed0fb72012-12-12 20:48:18 +0000743 this->writePaint(paint);
744 if (this->needOpBytes(kSizeOfFlatRRect)) {
745 this->writeOp(kDrawRRect_DrawOp);
746 fWriter.writeRRect(rrect);
747 }
748}
749
commit-bot@chromium.orged9806f2014-02-21 02:32:36 +0000750void SkGPipeCanvas::onDrawDRRect(const SkRRect& outer, const SkRRect& inner,
751 const SkPaint& paint) {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000752 NOTIFY_SETUP(this);
commit-bot@chromium.orged9806f2014-02-21 02:32:36 +0000753 this->writePaint(paint);
754 if (this->needOpBytes(kSizeOfFlatRRect * 2)) {
755 this->writeOp(kDrawDRRect_DrawOp);
756 fWriter.writeRRect(outer);
757 fWriter.writeRRect(inner);
758 }
759}
760
bsalomon@google.com7ce564c2013-10-22 16:54:15 +0000761void SkGPipeCanvas::drawPath(const SkPath& path, const SkPaint& paint) {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000762 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000763 this->writePaint(paint);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000764 if (this->needOpBytes(path.writeToMemory(NULL))) {
reed@google.com31891582011-05-12 03:03:56 +0000765 this->writeOp(kDrawPath_DrawOp);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000766 fWriter.writePath(path);
reed@google.comacd471f2011-05-03 21:26:46 +0000767 }
reed@google.combb6992a2011-04-26 17:41:56 +0000768}
769
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000770bool SkGPipeCanvas::commonDrawBitmap(const SkBitmap& bm, DrawOps op,
771 unsigned flags,
772 size_t opBytesNeeded,
773 const SkPaint* paint) {
commit-bot@chromium.org44803fb2014-05-14 15:38:25 +0000774 if (fDone) {
775 return false;
776 }
777
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 }
commit-bot@chromium.org44803fb2014-05-14 15:38:25 +0000782 // This needs to run first so its calls to needOpBytes() and its writes
783 // don't interlace with the needOpBytes() and write below.
784 SkASSERT(fBitmapHeap != NULL);
785 int32_t bitmapIndex = fBitmapHeap->insert(bm);
786 if (SkBitmapHeap::INVALID_SLOT == bitmapIndex) {
787 return false;
788 }
789
scroggo@google.com10dccde2012-08-08 20:43:22 +0000790 if (this->needOpBytes(opBytesNeeded)) {
791 this->writeOp(op, flags, bitmapIndex);
scroggo@google.com58be6822012-07-30 14:40:01 +0000792 return true;
793 }
794 return false;
795}
796
797void SkGPipeCanvas::drawBitmap(const SkBitmap& bm, SkScalar left, SkScalar top,
798 const SkPaint* paint) {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000799 NOTIFY_SETUP(this);
scroggo@google.com58be6822012-07-30 14:40:01 +0000800 size_t opBytesNeeded = sizeof(SkScalar) * 2;
801
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000802 if (this->commonDrawBitmap(bm, kDrawBitmap_DrawOp, 0, opBytesNeeded, paint)) {
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000803 fWriter.writeScalar(left);
804 fWriter.writeScalar(top);
805 }
reed@google.combb6992a2011-04-26 17:41:56 +0000806}
807
reed@google.com71121732012-09-18 15:14:33 +0000808void SkGPipeCanvas::drawBitmapRectToRect(const SkBitmap& bm, const SkRect* src,
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +0000809 const SkRect& dst, const SkPaint* paint,
810 DrawBitmapRectFlags dbmrFlags) {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000811 NOTIFY_SETUP(this);
scroggo@google.com58be6822012-07-30 14:40:01 +0000812 size_t opBytesNeeded = sizeof(SkRect);
813 bool hasSrc = src != NULL;
814 unsigned flags;
815 if (hasSrc) {
scroggo@google.com460a23e2012-08-16 17:56:49 +0000816 flags = kDrawBitmap_HasSrcRect_DrawOpFlag;
scroggo@google.com58be6822012-07-30 14:40:01 +0000817 opBytesNeeded += sizeof(int32_t) * 4;
818 } else {
819 flags = 0;
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000820 }
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +0000821 if (dbmrFlags & kBleed_DrawBitmapRectFlag) {
822 flags |= kDrawBitmap_Bleed_DrawOpFlag;
823 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000824
reed@google.com71121732012-09-18 15:14:33 +0000825 if (this->commonDrawBitmap(bm, kDrawBitmapRectToRect_DrawOp, flags, opBytesNeeded, paint)) {
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000826 if (hasSrc) {
reed@google.com71121732012-09-18 15:14:33 +0000827 fWriter.writeRect(*src);
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000828 }
829 fWriter.writeRect(dst);
830 }
reed@google.combb6992a2011-04-26 17:41:56 +0000831}
832
scroggo@google.com58be6822012-07-30 14:40:01 +0000833void SkGPipeCanvas::drawBitmapMatrix(const SkBitmap& bm, const SkMatrix& matrix,
834 const SkPaint* paint) {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000835 NOTIFY_SETUP(this);
scroggo@google.com58be6822012-07-30 14:40:01 +0000836 size_t opBytesNeeded = matrix.writeToMemory(NULL);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000837
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000838 if (this->commonDrawBitmap(bm, kDrawBitmapMatrix_DrawOp, 0, opBytesNeeded, paint)) {
scroggo@google.com58be6822012-07-30 14:40:01 +0000839 fWriter.writeMatrix(matrix);
840 }
reed@google.combb6992a2011-04-26 17:41:56 +0000841}
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000842
843void SkGPipeCanvas::drawBitmapNine(const SkBitmap& bm, const SkIRect& center,
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000844 const SkRect& dst, const SkPaint* paint) {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000845 NOTIFY_SETUP(this);
scroggo@google.com58be6822012-07-30 14:40:01 +0000846 size_t opBytesNeeded = sizeof(int32_t) * 4 + sizeof(SkRect);
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000847
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000848 if (this->commonDrawBitmap(bm, kDrawBitmapNine_DrawOp, 0, opBytesNeeded, paint)) {
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000849 fWriter.write32(center.fLeft);
850 fWriter.write32(center.fTop);
851 fWriter.write32(center.fRight);
852 fWriter.write32(center.fBottom);
853 fWriter.writeRect(dst);
854 }
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000855}
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000856
857void SkGPipeCanvas::drawSprite(const SkBitmap& bm, int left, int top,
858 const SkPaint* paint) {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000859 NOTIFY_SETUP(this);
scroggo@google.com58be6822012-07-30 14:40:01 +0000860 size_t opBytesNeeded = sizeof(int32_t) * 2;
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000861
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000862 if (this->commonDrawBitmap(bm, kDrawSprite_DrawOp, 0, opBytesNeeded, paint)) {
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000863 fWriter.write32(left);
864 fWriter.write32(top);
865 }
reed@google.combb6992a2011-04-26 17:41:56 +0000866}
867
reed@google.come0d9ce82014-04-23 04:00:17 +0000868void SkGPipeCanvas::onDrawText(const void* text, size_t byteLength, SkScalar x, SkScalar y,
869 const SkPaint& paint) {
reed@google.combb6992a2011-04-26 17:41:56 +0000870 if (byteLength) {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000871 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000872 this->writePaint(paint);
reed@google.comacd471f2011-05-03 21:26:46 +0000873 if (this->needOpBytes(4 + SkAlign4(byteLength) + 2 * sizeof(SkScalar))) {
reed@google.com31891582011-05-12 03:03:56 +0000874 this->writeOp(kDrawText_DrawOp);
commit-bot@chromium.orgb45bd1f2014-04-24 18:17:30 +0000875 fWriter.write32(SkToU32(byteLength));
reed@google.comacd471f2011-05-03 21:26:46 +0000876 fWriter.writePad(text, byteLength);
877 fWriter.writeScalar(x);
878 fWriter.writeScalar(y);
879 }
reed@google.combb6992a2011-04-26 17:41:56 +0000880 }
881}
882
reed@google.come0d9ce82014-04-23 04:00:17 +0000883void SkGPipeCanvas::onDrawPosText(const void* text, size_t byteLength, const SkPoint pos[],
884 const SkPaint& paint) {
reed@google.combb6992a2011-04-26 17:41:56 +0000885 if (byteLength) {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000886 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000887 this->writePaint(paint);
reed@google.combb6992a2011-04-26 17:41:56 +0000888 int count = paint.textToGlyphs(text, byteLength, NULL);
reed@google.comacd471f2011-05-03 21:26:46 +0000889 if (this->needOpBytes(4 + SkAlign4(byteLength) + 4 + count * sizeof(SkPoint))) {
reed@google.com31891582011-05-12 03:03:56 +0000890 this->writeOp(kDrawPosText_DrawOp);
commit-bot@chromium.orgb45bd1f2014-04-24 18:17:30 +0000891 fWriter.write32(SkToU32(byteLength));
reed@google.comacd471f2011-05-03 21:26:46 +0000892 fWriter.writePad(text, byteLength);
893 fWriter.write32(count);
894 fWriter.write(pos, count * sizeof(SkPoint));
895 }
reed@google.combb6992a2011-04-26 17:41:56 +0000896 }
897}
898
reed@google.come0d9ce82014-04-23 04:00:17 +0000899void SkGPipeCanvas::onDrawPosTextH(const void* text, size_t byteLength, const SkScalar xpos[],
900 SkScalar constY, const SkPaint& paint) {
reed@google.combb6992a2011-04-26 17:41:56 +0000901 if (byteLength) {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000902 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000903 this->writePaint(paint);
reed@google.combb6992a2011-04-26 17:41:56 +0000904 int count = paint.textToGlyphs(text, byteLength, NULL);
reed@google.comacd471f2011-05-03 21:26:46 +0000905 if (this->needOpBytes(4 + SkAlign4(byteLength) + 4 + count * sizeof(SkScalar) + 4)) {
reed@google.com31891582011-05-12 03:03:56 +0000906 this->writeOp(kDrawPosTextH_DrawOp);
commit-bot@chromium.orgb45bd1f2014-04-24 18:17:30 +0000907 fWriter.write32(SkToU32(byteLength));
reed@google.comacd471f2011-05-03 21:26:46 +0000908 fWriter.writePad(text, byteLength);
909 fWriter.write32(count);
910 fWriter.write(xpos, count * sizeof(SkScalar));
911 fWriter.writeScalar(constY);
912 }
reed@google.combb6992a2011-04-26 17:41:56 +0000913 }
914}
915
reed@google.come0d9ce82014-04-23 04:00:17 +0000916void SkGPipeCanvas::onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& path,
917 const SkMatrix* matrix, const SkPaint& paint) {
reed@google.combb6992a2011-04-26 17:41:56 +0000918 if (byteLength) {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000919 NOTIFY_SETUP(this);
reed@google.combb6992a2011-04-26 17:41:56 +0000920 unsigned flags = 0;
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000921 size_t size = 4 + SkAlign4(byteLength) + path.writeToMemory(NULL);
reed@google.combb6992a2011-04-26 17:41:56 +0000922 if (matrix) {
923 flags |= kDrawTextOnPath_HasMatrix_DrawOpFlag;
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000924 size += matrix->writeToMemory(NULL);
reed@google.combb6992a2011-04-26 17:41:56 +0000925 }
reed@google.com31891582011-05-12 03:03:56 +0000926 this->writePaint(paint);
reed@google.comacd471f2011-05-03 21:26:46 +0000927 if (this->needOpBytes(size)) {
reed@google.com31891582011-05-12 03:03:56 +0000928 this->writeOp(kDrawTextOnPath_DrawOp, flags, 0);
reed@google.combb6992a2011-04-26 17:41:56 +0000929
commit-bot@chromium.orgb45bd1f2014-04-24 18:17:30 +0000930 fWriter.write32(SkToU32(byteLength));
reed@google.comacd471f2011-05-03 21:26:46 +0000931 fWriter.writePad(text, byteLength);
reed@google.combb6992a2011-04-26 17:41:56 +0000932
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000933 fWriter.writePath(path);
reed@google.comacd471f2011-05-03 21:26:46 +0000934 if (matrix) {
djsollen@google.com2b2ede32012-04-12 13:24:04 +0000935 fWriter.writeMatrix(*matrix);
reed@google.comacd471f2011-05-03 21:26:46 +0000936 }
reed@google.combb6992a2011-04-26 17:41:56 +0000937 }
938 }
939}
940
fmalitab7425172014-08-26 07:56:44 -0700941void SkGPipeCanvas::onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
942 const SkPaint& paint) {
fmalita228a6f22014-08-28 13:59:42 -0700943 NOTIFY_SETUP(this);
944 this->writePaint(paint);
945
946 // FIXME: this is inefficient but avoids duplicating the blob serialization logic.
947 SkWriteBuffer blobBuffer;
948 blob->flatten(blobBuffer);
949
950 size_t size = sizeof(uint32_t) + 2 * sizeof(SkScalar) + blobBuffer.bytesWritten();
951 if (this->needOpBytes(size)) {
952 this->writeOp(kDrawTextBlob_DrawOp);
953 fWriter.writeScalar(x);
954 fWriter.writeScalar(y);
955 fWriter.write32(SkToU32(blobBuffer.bytesWritten()));
956 uint32_t* pad = fWriter.reservePad(blobBuffer.bytesWritten());
957 blobBuffer.writeToMemory(pad);
958 }
fmalitab7425172014-08-26 07:56:44 -0700959}
960
reedd5fa1a42014-08-09 11:08:05 -0700961void SkGPipeCanvas::onDrawPicture(const SkPicture* picture, const SkMatrix* matrix,
962 const SkPaint* paint) {
reed@google.com0faac1e2011-05-11 05:58:58 +0000963 // we want to playback the picture into individual draw calls
reedd5fa1a42014-08-09 11:08:05 -0700964 //
965 // todo: do we always have to unroll? If the pipe is not cross-process, seems like
966 // we could just ref the picture and move on...? <reed, scroggo>
967 //
968 this->INHERITED::onDrawPicture(picture, matrix, paint);
reed@google.combb6992a2011-04-26 17:41:56 +0000969}
970
reed@google.com85e143c2013-12-30 15:51:25 +0000971void SkGPipeCanvas::drawVertices(VertexMode vmode, int vertexCount,
reed@google.combb6992a2011-04-26 17:41:56 +0000972 const SkPoint vertices[], const SkPoint texs[],
reed@google.com85e143c2013-12-30 15:51:25 +0000973 const SkColor colors[], SkXfermode* xfer,
reed@google.combb6992a2011-04-26 17:41:56 +0000974 const uint16_t indices[], int indexCount,
975 const SkPaint& paint) {
976 if (0 == vertexCount) {
977 return;
978 }
979
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000980 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000981 this->writePaint(paint);
commit-bot@chromium.orgd0490172014-05-14 15:12:08 +0000982
983 unsigned flags = 0; // packs with the op, so needs no extra space
984
985 size_t size = 0;
986 size += 4; // vmode
987 size += 4; // vertex count
988 size += vertexCount * sizeof(SkPoint); // vertices
989
reed@google.combb6992a2011-04-26 17:41:56 +0000990 if (texs) {
991 flags |= kDrawVertices_HasTexs_DrawOpFlag;
reed@google.comacd471f2011-05-03 21:26:46 +0000992 size += vertexCount * sizeof(SkPoint);
reed@google.combb6992a2011-04-26 17:41:56 +0000993 }
994 if (colors) {
995 flags |= kDrawVertices_HasColors_DrawOpFlag;
reed@google.comacd471f2011-05-03 21:26:46 +0000996 size += vertexCount * sizeof(SkColor);
reed@google.combb6992a2011-04-26 17:41:56 +0000997 }
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000998 if (xfer && !SkXfermode::IsMode(xfer, SkXfermode::kModulate_Mode)) {
999 flags |= kDrawVertices_HasXfermode_DrawOpFlag;
commit-bot@chromium.orgd0490172014-05-14 15:12:08 +00001000 size += sizeof(int32_t); // SkXfermode::Mode
1001 }
1002 if (indices && indexCount > 0) {
1003 flags |= kDrawVertices_HasIndices_DrawOpFlag;
1004 size += 4; // index count
1005 size += SkAlign4(indexCount * sizeof(uint16_t)); // indices
reed@google.com85e143c2013-12-30 15:51:25 +00001006 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001007
reed@google.comacd471f2011-05-03 21:26:46 +00001008 if (this->needOpBytes(size)) {
reed@google.com31891582011-05-12 03:03:56 +00001009 this->writeOp(kDrawVertices_DrawOp, flags, 0);
reed@google.com85e143c2013-12-30 15:51:25 +00001010 fWriter.write32(vmode);
reed@google.comacd471f2011-05-03 21:26:46 +00001011 fWriter.write32(vertexCount);
1012 fWriter.write(vertices, vertexCount * sizeof(SkPoint));
commit-bot@chromium.orgd0490172014-05-14 15:12:08 +00001013 if (flags & kDrawVertices_HasTexs_DrawOpFlag) {
reed@google.comacd471f2011-05-03 21:26:46 +00001014 fWriter.write(texs, vertexCount * sizeof(SkPoint));
1015 }
commit-bot@chromium.orgd0490172014-05-14 15:12:08 +00001016 if (flags & kDrawVertices_HasColors_DrawOpFlag) {
reed@google.comacd471f2011-05-03 21:26:46 +00001017 fWriter.write(colors, vertexCount * sizeof(SkColor));
1018 }
reed@google.com85e143c2013-12-30 15:51:25 +00001019 if (flags & kDrawVertices_HasXfermode_DrawOpFlag) {
1020 SkXfermode::Mode mode = SkXfermode::kModulate_Mode;
commit-bot@chromium.orgd0490172014-05-14 15:12:08 +00001021 SkAssertResult(xfer->asMode(&mode));
reed@google.com85e143c2013-12-30 15:51:25 +00001022 fWriter.write32(mode);
1023 }
commit-bot@chromium.orgd0490172014-05-14 15:12:08 +00001024 if (flags & kDrawVertices_HasIndices_DrawOpFlag) {
reed@google.comacd471f2011-05-03 21:26:46 +00001025 fWriter.write32(indexCount);
1026 fWriter.writePad(indices, indexCount * sizeof(uint16_t));
1027 }
reed@google.combb6992a2011-04-26 17:41:56 +00001028 }
1029}
1030
dandovb3c9d1c2014-08-12 08:34:29 -07001031void SkGPipeCanvas::onDrawPatch(const SkPoint cubics[12], const SkColor colors[4],
1032 const SkPoint texCoords[4], SkXfermode* xmode,
1033 const SkPaint& paint) {
dandov963137b2014-08-07 07:49:53 -07001034 NOTIFY_SETUP(this);
dandovb3c9d1c2014-08-12 08:34:29 -07001035
1036 size_t size = SkPatchUtils::kNumCtrlPts * sizeof(SkPoint);
1037 unsigned flags = 0;
1038 if (NULL != colors) {
1039 flags |= kDrawVertices_HasColors_DrawOpFlag;
1040 size += SkPatchUtils::kNumCorners * sizeof(SkColor);
1041 }
1042 if (NULL != texCoords) {
1043 flags |= kDrawVertices_HasTexs_DrawOpFlag;
1044 size += SkPatchUtils::kNumCorners * sizeof(SkPoint);
1045 }
1046 if (NULL != xmode) {
1047 SkXfermode::Mode mode;
1048 if (xmode->asMode(&mode) && SkXfermode::kModulate_Mode != mode) {
1049 flags |= kDrawVertices_HasXfermode_DrawOpFlag;
1050 size += sizeof(int32_t);
1051 }
1052 }
1053
dandov963137b2014-08-07 07:49:53 -07001054 this->writePaint(paint);
dandovb3c9d1c2014-08-12 08:34:29 -07001055 if (this->needOpBytes(size)) {
1056 this->writeOp(kDrawPatch_DrawOp, flags, 0);
1057
1058 fWriter.write(cubics, SkPatchUtils::kNumCtrlPts * sizeof(SkPoint));
1059
1060 if (NULL != colors) {
1061 fWriter.write(colors, SkPatchUtils::kNumCorners * sizeof(SkColor));
1062 }
1063
1064 if (NULL != texCoords) {
1065 fWriter.write(texCoords, SkPatchUtils::kNumCorners * sizeof(SkPoint));
1066 }
1067
1068 if (flags & kDrawVertices_HasXfermode_DrawOpFlag) {
1069 SkXfermode::Mode mode = SkXfermode::kModulate_Mode;
1070 SkAssertResult(xmode->asMode(&mode));
1071 fWriter.write32(mode);
1072 }
dandov963137b2014-08-07 07:49:53 -07001073 }
1074}
1075
reed@google.comacd471f2011-05-03 21:26:46 +00001076void SkGPipeCanvas::drawData(const void* ptr, size_t size) {
1077 if (size && ptr) {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +00001078 NOTIFY_SETUP(this);
reed@google.combb6992a2011-04-26 17:41:56 +00001079 unsigned data = 0;
1080 if (size < (1 << DRAWOPS_DATA_BITS)) {
1081 data = (unsigned)size;
1082 }
reed@google.comacd471f2011-05-03 21:26:46 +00001083 if (this->needOpBytes(4 + SkAlign4(size))) {
1084 this->writeOp(kDrawData_DrawOp, 0, data);
1085 if (0 == data) {
commit-bot@chromium.orgb45bd1f2014-04-24 18:17:30 +00001086 fWriter.write32(SkToU32(size));
reed@google.comacd471f2011-05-03 21:26:46 +00001087 }
reed@google.combb6793b2011-05-05 15:18:15 +00001088 fWriter.writePad(ptr, size);
reed@google.combb6992a2011-04-26 17:41:56 +00001089 }
1090 }
1091}
1092
robertphillips@google.com0a4805e2013-05-29 13:24:23 +00001093void SkGPipeCanvas::beginCommentGroup(const char* description) {
1094 // ignore for now
1095}
1096
1097void SkGPipeCanvas::addComment(const char* kywd, const char* value) {
1098 // ignore for now
1099}
1100
1101void SkGPipeCanvas::endCommentGroup() {
1102 // ignore for now
1103}
1104
junov@chromium.org77eec242012-07-18 17:54:45 +00001105void SkGPipeCanvas::flushRecording(bool detachCurrentBlock) {
commit-bot@chromium.orgd0490172014-05-14 15:12:08 +00001106 this->doNotify();
junov@chromium.org77eec242012-07-18 17:54:45 +00001107 if (detachCurrentBlock) {
1108 // force a new block to be requested for the next recorded command
rmistry@google.comd6176b02012-08-23 18:14:13 +00001109 fBlockSize = 0;
junov@chromium.org77eec242012-07-18 17:54:45 +00001110 }
1111}
1112
junov@chromium.org2e14ba82012-08-07 14:26:57 +00001113size_t SkGPipeCanvas::freeMemoryIfPossible(size_t bytesToFree) {
scroggo@google.comd5d158b2012-08-14 20:38:28 +00001114 return (NULL == fBitmapHeap) ? 0 : fBitmapHeap->freeMemoryIfPossible(bytesToFree);
junov@chromium.org2e14ba82012-08-07 14:26:57 +00001115}
1116
reed@google.combb6992a2011-04-26 17:41:56 +00001117///////////////////////////////////////////////////////////////////////////////
1118
1119template <typename T> uint32_t castToU32(T value) {
1120 union {
1121 T fSrc;
1122 uint32_t fDst;
1123 } data;
1124 data.fSrc = value;
1125 return data.fDst;
1126}
1127
reed@google.com31891582011-05-12 03:03:56 +00001128void SkGPipeCanvas::writePaint(const SkPaint& paint) {
scroggo@google.comd5d158b2012-08-14 20:38:28 +00001129 if (fDone) {
1130 return;
1131 }
reed@google.com31891582011-05-12 03:03:56 +00001132 SkPaint& base = fPaint;
reed@google.combb6992a2011-04-26 17:41:56 +00001133 uint32_t storage[32];
1134 uint32_t* ptr = storage;
reed@google.combb6992a2011-04-26 17:41:56 +00001135
1136 if (base.getFlags() != paint.getFlags()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001137 *ptr++ = PaintOp_packOpData(kFlags_PaintOp, paint.getFlags());
reed@google.comf5842f72011-05-04 18:30:04 +00001138 base.setFlags(paint.getFlags());
reed@google.combb6992a2011-04-26 17:41:56 +00001139 }
1140 if (base.getColor() != paint.getColor()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001141 *ptr++ = PaintOp_packOp(kColor_PaintOp);
1142 *ptr++ = paint.getColor();
reed@google.comf5842f72011-05-04 18:30:04 +00001143 base.setColor(paint.getColor());
reed@google.combb6992a2011-04-26 17:41:56 +00001144 }
commit-bot@chromium.org85faf502014-04-16 12:58:02 +00001145 if (base.getFilterLevel() != paint.getFilterLevel()) {
1146 *ptr++ = PaintOp_packOpData(kFilterLevel_PaintOp, paint.getFilterLevel());
1147 base.setFilterLevel(paint.getFilterLevel());
1148 }
reed@google.combb6992a2011-04-26 17:41:56 +00001149 if (base.getStyle() != paint.getStyle()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001150 *ptr++ = PaintOp_packOpData(kStyle_PaintOp, paint.getStyle());
reed@google.comf5842f72011-05-04 18:30:04 +00001151 base.setStyle(paint.getStyle());
reed@google.combb6992a2011-04-26 17:41:56 +00001152 }
1153 if (base.getStrokeJoin() != paint.getStrokeJoin()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001154 *ptr++ = PaintOp_packOpData(kJoin_PaintOp, paint.getStrokeJoin());
reed@google.comf5842f72011-05-04 18:30:04 +00001155 base.setStrokeJoin(paint.getStrokeJoin());
reed@google.combb6992a2011-04-26 17:41:56 +00001156 }
1157 if (base.getStrokeCap() != paint.getStrokeCap()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001158 *ptr++ = PaintOp_packOpData(kCap_PaintOp, paint.getStrokeCap());
reed@google.comf5842f72011-05-04 18:30:04 +00001159 base.setStrokeCap(paint.getStrokeCap());
reed@google.combb6992a2011-04-26 17:41:56 +00001160 }
1161 if (base.getStrokeWidth() != paint.getStrokeWidth()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001162 *ptr++ = PaintOp_packOp(kWidth_PaintOp);
1163 *ptr++ = castToU32(paint.getStrokeWidth());
reed@google.comf5842f72011-05-04 18:30:04 +00001164 base.setStrokeWidth(paint.getStrokeWidth());
reed@google.combb6992a2011-04-26 17:41:56 +00001165 }
1166 if (base.getStrokeMiter() != paint.getStrokeMiter()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001167 *ptr++ = PaintOp_packOp(kMiter_PaintOp);
1168 *ptr++ = castToU32(paint.getStrokeMiter());
reed@google.comf5842f72011-05-04 18:30:04 +00001169 base.setStrokeMiter(paint.getStrokeMiter());
reed@google.combb6992a2011-04-26 17:41:56 +00001170 }
1171 if (base.getTextEncoding() != paint.getTextEncoding()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001172 *ptr++ = PaintOp_packOpData(kEncoding_PaintOp, paint.getTextEncoding());
reed@google.comf5842f72011-05-04 18:30:04 +00001173 base.setTextEncoding(paint.getTextEncoding());
reed@google.combb6992a2011-04-26 17:41:56 +00001174 }
1175 if (base.getHinting() != paint.getHinting()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001176 *ptr++ = PaintOp_packOpData(kHinting_PaintOp, paint.getHinting());
reed@google.comf5842f72011-05-04 18:30:04 +00001177 base.setHinting(paint.getHinting());
reed@google.combb6992a2011-04-26 17:41:56 +00001178 }
1179 if (base.getTextAlign() != paint.getTextAlign()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001180 *ptr++ = PaintOp_packOpData(kAlign_PaintOp, paint.getTextAlign());
reed@google.comf5842f72011-05-04 18:30:04 +00001181 base.setTextAlign(paint.getTextAlign());
reed@google.combb6992a2011-04-26 17:41:56 +00001182 }
1183 if (base.getTextSize() != paint.getTextSize()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001184 *ptr++ = PaintOp_packOp(kTextSize_PaintOp);
1185 *ptr++ = castToU32(paint.getTextSize());
reed@google.comf5842f72011-05-04 18:30:04 +00001186 base.setTextSize(paint.getTextSize());
reed@google.combb6992a2011-04-26 17:41:56 +00001187 }
1188 if (base.getTextScaleX() != paint.getTextScaleX()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001189 *ptr++ = PaintOp_packOp(kTextScaleX_PaintOp);
1190 *ptr++ = castToU32(paint.getTextScaleX());
reed@google.comf5842f72011-05-04 18:30:04 +00001191 base.setTextScaleX(paint.getTextScaleX());
reed@google.combb6992a2011-04-26 17:41:56 +00001192 }
1193 if (base.getTextSkewX() != paint.getTextSkewX()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001194 *ptr++ = PaintOp_packOp(kTextSkewX_PaintOp);
1195 *ptr++ = castToU32(paint.getTextSkewX());
reed@google.comf5842f72011-05-04 18:30:04 +00001196 base.setTextSkewX(paint.getTextSkewX());
1197 }
1198
1199 if (!SkTypeface::Equal(base.getTypeface(), paint.getTypeface())) {
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +00001200 if (isCrossProcess(fFlags)) {
scroggo@google.com3cb969f2012-07-27 20:39:19 +00001201 uint32_t id = this->getTypefaceID(paint.getTypeface());
1202 *ptr++ = PaintOp_packOpData(kTypeface_PaintOp, id);
1203 } else if (this->needOpBytes(sizeof(void*))) {
1204 // Add to the set for ref counting.
1205 fTypefaceSet.add(paint.getTypeface());
1206 // It is safe to write the typeface to the stream before the rest
1207 // of the paint unless we ever send a kReset_PaintOp, which we
1208 // currently never do.
1209 this->writeOp(kSetTypeface_DrawOp);
1210 fWriter.writePtr(paint.getTypeface());
1211 }
reed@google.comf5842f72011-05-04 18:30:04 +00001212 base.setTypeface(paint.getTypeface());
reed@google.combb6992a2011-04-26 17:41:56 +00001213 }
reed@google.combb6992a2011-04-26 17:41:56 +00001214
scroggo@google.com4dffc592012-07-17 16:49:40 +00001215 // This is a new paint, so all old flats can be safely purged, if necessary.
1216 fFlattenableHeap.markAllFlatsSafeToDelete();
reed@google.comb55d1182011-05-11 00:42:04 +00001217 for (int i = 0; i < kCount_PaintFlats; i++) {
1218 int index = this->flattenToIndex(get_paintflat(paint, i), (PaintFlats)i);
scroggo@google.comd3ba5cc2012-07-09 16:05:53 +00001219 bool replaced = index < 0;
1220 if (replaced) {
1221 index = ~index;
1222 }
scroggo@google.com4dffc592012-07-17 16:49:40 +00001223 // Store the index of any flat that needs to be kept. 0 means no flat.
1224 if (index > 0) {
1225 fFlattenableHeap.markFlatForKeeping(index);
1226 }
1227 SkASSERT(index >= 0 && index <= fFlatDictionary.count());
scroggo@google.comd3ba5cc2012-07-09 16:05:53 +00001228 if (index != fCurrFlatIndex[i] || replaced) {
reed@google.comb55d1182011-05-11 00:42:04 +00001229 *ptr++ = PaintOp_packOpFlagData(kFlatIndex_PaintOp, i, index);
1230 fCurrFlatIndex[i] = index;
1231 }
1232 }
1233
reed@google.comacd471f2011-05-03 21:26:46 +00001234 size_t size = (char*)ptr - (char*)storage;
1235 if (size && this->needOpBytes(size)) {
commit-bot@chromium.orgb45bd1f2014-04-24 18:17:30 +00001236 this->writeOp(kPaintOp_DrawOp, 0, SkToU32(size));
reed@google.comb55d1182011-05-11 00:42:04 +00001237 fWriter.write(storage, size);
reed@google.combb6992a2011-04-26 17:41:56 +00001238 for (size_t i = 0; i < size/4; i++) {
reed@google.comb55d1182011-05-11 00:42:04 +00001239// SkDebugf("[%d] %08X\n", i, storage[i]);
reed@google.combb6992a2011-04-26 17:41:56 +00001240 }
1241 }
reed@google.com0cd2ac62013-10-14 20:02:44 +00001242
1243 //
1244 // Do these after we've written kPaintOp_DrawOp
skia.committer@gmail.comfbc58a32013-10-15 07:02:27 +00001245
reed@google.com0cd2ac62013-10-14 20:02:44 +00001246 if (base.getAnnotation() != paint.getAnnotation()) {
1247 if (NULL == paint.getAnnotation()) {
commit-bot@chromium.org89ff3dd2013-10-29 20:29:38 +00001248 if (this->needOpBytes()) {
1249 this->writeOp(kSetAnnotation_DrawOp, 0, 0);
1250 }
reed@google.com0cd2ac62013-10-14 20:02:44 +00001251 } else {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +00001252 SkWriteBuffer buffer;
reed@google.com0cd2ac62013-10-14 20:02:44 +00001253 paint.getAnnotation()->writeToBuffer(buffer);
commit-bot@chromium.org89ff3dd2013-10-29 20:29:38 +00001254 const size_t size = buffer.bytesWritten();
1255 if (this->needOpBytes(size)) {
commit-bot@chromium.orgb45bd1f2014-04-24 18:17:30 +00001256 this->writeOp(kSetAnnotation_DrawOp, 0, SkToU32(size));
commit-bot@chromium.org89ff3dd2013-10-29 20:29:38 +00001257 buffer.writeToMemory(fWriter.reserve(size));
1258 }
reed@google.com0cd2ac62013-10-14 20:02:44 +00001259 }
commit-bot@chromium.org40258a52013-10-29 19:23:26 +00001260 base.setAnnotation(paint.getAnnotation());
reed@google.com0cd2ac62013-10-14 20:02:44 +00001261 }
reed@google.combb6992a2011-04-26 17:41:56 +00001262}
1263
1264///////////////////////////////////////////////////////////////////////////////
1265
1266#include "SkGPipe.h"
1267
scroggo@google.com3cb969f2012-07-27 20:39:19 +00001268SkGPipeController::~SkGPipeController() {
1269 SkSafeUnref(fCanvas);
1270}
1271
1272void SkGPipeController::setCanvas(SkGPipeCanvas* canvas) {
1273 SkRefCnt_SafeAssign(fCanvas, canvas);
1274}
1275
1276///////////////////////////////////////////////////////////////////////////////
1277
1278SkGPipeWriter::SkGPipeWriter()
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +00001279: fWriter(0) {
reed@google.combb6992a2011-04-26 17:41:56 +00001280 fCanvas = NULL;
1281}
1282
1283SkGPipeWriter::~SkGPipeWriter() {
reed@google.comacd471f2011-05-03 21:26:46 +00001284 this->endRecording();
reed@google.combb6992a2011-04-26 17:41:56 +00001285}
1286
junov@chromium.orga8db8fe2012-08-15 19:49:22 +00001287SkCanvas* SkGPipeWriter::startRecording(SkGPipeController* controller, uint32_t flags,
1288 uint32_t width, uint32_t height) {
reed@google.combb6992a2011-04-26 17:41:56 +00001289 if (NULL == fCanvas) {
reed@google.comacd471f2011-05-03 21:26:46 +00001290 fWriter.reset(NULL, 0);
junov@chromium.orga8db8fe2012-08-15 19:49:22 +00001291 fCanvas = SkNEW_ARGS(SkGPipeCanvas, (controller, &fWriter, flags, width, height));
reed@google.combb6992a2011-04-26 17:41:56 +00001292 }
scroggo@google.com3cb969f2012-07-27 20:39:19 +00001293 controller->setCanvas(fCanvas);
reed@google.combb6992a2011-04-26 17:41:56 +00001294 return fCanvas;
1295}
1296
1297void SkGPipeWriter::endRecording() {
1298 if (fCanvas) {
scroggo@google.com59c3ab62013-11-12 14:32:38 +00001299 fCanvas->finish(true);
reed@google.combb6992a2011-04-26 17:41:56 +00001300 fCanvas->unref();
1301 fCanvas = NULL;
1302 }
1303}
1304
junov@chromium.org2e14ba82012-08-07 14:26:57 +00001305void SkGPipeWriter::flushRecording(bool detachCurrentBlock) {
1306 if (fCanvas) {
1307 fCanvas->flushRecording(detachCurrentBlock);
1308 }
junov@chromium.org77eec242012-07-18 17:54:45 +00001309}
1310
junov@chromium.org2e14ba82012-08-07 14:26:57 +00001311size_t SkGPipeWriter::freeMemoryIfPossible(size_t bytesToFree) {
1312 if (fCanvas) {
1313 return fCanvas->freeMemoryIfPossible(bytesToFree);
1314 }
1315 return 0;
1316}
1317
1318size_t SkGPipeWriter::storageAllocatedForRecording() const {
scroggo@google.com15011ee2012-07-26 20:03:32 +00001319 return NULL == fCanvas ? 0 : fCanvas->storageAllocatedForRecording();
1320}
1321
scroggo@google.com3e26bd02012-08-14 15:20:01 +00001322///////////////////////////////////////////////////////////////////////////////
1323
1324BitmapShuttle::BitmapShuttle(SkGPipeCanvas* canvas) {
1325 SkASSERT(canvas != NULL);
1326 fCanvas = canvas;
1327 fCanvas->ref();
1328}
1329
1330BitmapShuttle::~BitmapShuttle() {
scroggo@google.com59c3ab62013-11-12 14:32:38 +00001331 this->removeCanvas();
scroggo@google.com3e26bd02012-08-14 15:20:01 +00001332}
1333
1334bool BitmapShuttle::insert(const SkBitmap& bitmap, int32_t slot) {
scroggo@google.com59c3ab62013-11-12 14:32:38 +00001335 SkASSERT(fCanvas != NULL);
scroggo@google.com3e26bd02012-08-14 15:20:01 +00001336 return fCanvas->shuttleBitmap(bitmap, slot);
1337}
scroggo@google.com59c3ab62013-11-12 14:32:38 +00001338
1339void BitmapShuttle::removeCanvas() {
1340 if (NULL == fCanvas) {
1341 return;
1342 }
1343 fCanvas->unref();
1344 fCanvas = NULL;
1345}