blob: 32af81d9217a196d19cdfe1de7f8368039296dcc [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"
reed@google.comb55d1182011-05-11 00:42:04 +000029#include "SkTSearch.h"
reed@google.comf5842f72011-05-04 18:30:04 +000030#include "SkTypeface.h"
reed@google.combb6992a2011-04-26 17:41:56 +000031#include "SkWriter32.h"
reed@google.comb55d1182011-05-11 00:42:04 +000032
reed@google.com4ed0fb72012-12-12 20:48:18 +000033enum {
34 kSizeOfFlatRRect = sizeof(SkRect) + 4 * sizeof(SkVector)
35};
36
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +000037static bool isCrossProcess(uint32_t flags) {
38 return SkToBool(flags & SkGPipeWriter::kCrossProcess_Flag);
39}
40
reed@google.comb55d1182011-05-11 00:42:04 +000041static SkFlattenable* get_paintflat(const SkPaint& paint, unsigned paintFlat) {
42 SkASSERT(paintFlat < kCount_PaintFlats);
43 switch (paintFlat) {
44 case kColorFilter_PaintFlat: return paint.getColorFilter();
reed@google.com0faac1e2011-05-11 05:58:58 +000045 case kDrawLooper_PaintFlat: return paint.getLooper();
reed@google.comb55d1182011-05-11 00:42:04 +000046 case kMaskFilter_PaintFlat: return paint.getMaskFilter();
47 case kPathEffect_PaintFlat: return paint.getPathEffect();
48 case kRasterizer_PaintFlat: return paint.getRasterizer();
49 case kShader_PaintFlat: return paint.getShader();
scroggo@google.com16d1d0b2012-05-02 19:09:40 +000050 case kImageFilter_PaintFlat: return paint.getImageFilter();
reed@google.comb55d1182011-05-11 00:42:04 +000051 case kXfermode_PaintFlat: return paint.getXfermode();
52 }
tomhudson@google.com0c00f212011-12-28 14:59:50 +000053 SkDEBUGFAIL("never gets here");
reed@google.comb55d1182011-05-11 00:42:04 +000054 return NULL;
55}
reed@google.combb6992a2011-04-26 17:41:56 +000056
reed@google.comf5842f72011-05-04 18:30:04 +000057static size_t writeTypeface(SkWriter32* writer, SkTypeface* typeface) {
58 SkASSERT(typeface);
59 SkDynamicMemoryWStream stream;
60 typeface->serialize(&stream);
61 size_t size = stream.getOffset();
62 if (writer) {
commit-bot@chromium.orgb45bd1f2014-04-24 18:17:30 +000063 writer->write32(SkToU32(size));
reed@google.com8a85d0c2011-06-24 19:12:12 +000064 SkAutoDataUnref data(stream.copyToData());
robertphillips@google.com59f46b82012-07-10 17:30:58 +000065 writer->writePad(data->data(), size);
reed@google.comf5842f72011-05-04 18:30:04 +000066 }
scroggo@google.com5af9b202012-06-04 17:17:36 +000067 return 4 + SkAlign4(size);
reed@google.comf5842f72011-05-04 18:30:04 +000068}
69
reed@google.combb6992a2011-04-26 17:41:56 +000070///////////////////////////////////////////////////////////////////////////////
71
scroggo@google.com4dffc592012-07-17 16:49:40 +000072class FlattenableHeap : public SkFlatController {
73public:
scroggo@google.com664fab12012-08-14 19:22:05 +000074 FlattenableHeap(int numFlatsToKeep, SkNamedFactorySet* fset, bool isCrossProcess)
commit-bot@chromium.orga2bd2d12014-01-30 22:16:32 +000075 : INHERITED(isCrossProcess ? SkWriteBuffer::kCrossProcess_Flag : 0)
76 , fNumFlatsToKeep(numFlatsToKeep) {
scroggo@google.com664fab12012-08-14 19:22:05 +000077 SkASSERT((isCrossProcess && fset != NULL) || (!isCrossProcess && NULL == fset));
78 if (isCrossProcess) {
79 this->setNamedFactorySet(fset);
scroggo@google.com664fab12012-08-14 19:22:05 +000080 }
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +000081 }
scroggo@google.com4dffc592012-07-17 16:49:40 +000082
83 ~FlattenableHeap() {
84 fPointers.freeAll();
85 }
86
87 virtual void* allocThrow(size_t bytes) SK_OVERRIDE;
88
89 virtual void unalloc(void* ptr) SK_OVERRIDE;
90
scroggo@google.com7ca24432012-08-14 15:48:43 +000091 void setBitmapStorage(SkBitmapHeap* heap) {
92 this->setBitmapHeap(heap);
93 }
rmistry@google.comd6176b02012-08-23 18:14:13 +000094
scroggo@google.com4dffc592012-07-17 16:49:40 +000095 const SkFlatData* flatToReplace() const;
96
97 // Mark an SkFlatData as one that should not be returned by flatToReplace.
98 // Takes the result of SkFlatData::index() as its parameter.
99 void markFlatForKeeping(int index) {
100 *fFlatsThatMustBeKept.append() = index;
101 }
102
103 void markAllFlatsSafeToDelete() {
104 fFlatsThatMustBeKept.reset();
105 }
106
107private:
108 // Keep track of the indices (i.e. the result of SkFlatData::index()) of
109 // flats that must be kept, since they are on the current paint.
110 SkTDArray<int> fFlatsThatMustBeKept;
111 SkTDArray<void*> fPointers;
112 const int fNumFlatsToKeep;
commit-bot@chromium.orga2bd2d12014-01-30 22:16:32 +0000113
114 typedef SkFlatController INHERITED;
scroggo@google.com4dffc592012-07-17 16:49:40 +0000115};
116
117void FlattenableHeap::unalloc(void* ptr) {
118 int indexToRemove = fPointers.rfind(ptr);
119 if (indexToRemove >= 0) {
120 sk_free(ptr);
121 fPointers.remove(indexToRemove);
122 }
123}
124
125void* FlattenableHeap::allocThrow(size_t bytes) {
126 void* ptr = sk_malloc_throw(bytes);
127 *fPointers.append() = ptr;
128 return ptr;
129}
130
131const SkFlatData* FlattenableHeap::flatToReplace() const {
132 // First, determine whether we should replace one.
133 if (fPointers.count() > fNumFlatsToKeep) {
134 // Look through the flattenable heap.
135 // TODO: Return the LRU flat.
136 for (int i = 0; i < fPointers.count(); i++) {
137 SkFlatData* potential = (SkFlatData*)fPointers[i];
138 // Make sure that it is not one that must be kept.
139 bool mustKeep = false;
140 for (int j = 0; j < fFlatsThatMustBeKept.count(); j++) {
141 if (potential->index() == fFlatsThatMustBeKept[j]) {
142 mustKeep = true;
143 break;
144 }
145 }
146 if (!mustKeep) {
147 return potential;
148 }
149 }
150 }
151 return NULL;
152}
153
154///////////////////////////////////////////////////////////////////////////////
155
commit-bot@chromium.org07adb632014-01-02 22:20:49 +0000156struct SkFlattenableTraits {
commit-bot@chromium.org186c0cc2014-02-18 16:15:05 +0000157 static void Flatten(SkWriteBuffer& buffer, const SkFlattenable& flattenable) {
commit-bot@chromium.org07adb632014-01-02 22:20:49 +0000158 buffer.writeFlattenable(&flattenable);
scroggo@google.com4dffc592012-07-17 16:49:40 +0000159 }
commit-bot@chromium.org07adb632014-01-02 22:20:49 +0000160 // No need to define unflatten if we never call it.
scroggo@google.com4dffc592012-07-17 16:49:40 +0000161};
commit-bot@chromium.org07adb632014-01-02 22:20:49 +0000162typedef SkFlatDictionary<SkFlattenable, SkFlattenableTraits> FlatDictionary;
scroggo@google.com4dffc592012-07-17 16:49:40 +0000163
164///////////////////////////////////////////////////////////////////////////////
165
scroggo@google.com59c3ab62013-11-12 14:32:38 +0000166/**
167 * If SkBitmaps are to be flattened to send to the reader, this class is
168 * provided to the SkBitmapHeap to tell the SkGPipeCanvas to do so.
169 */
170class BitmapShuttle : public SkBitmapHeap::ExternalStorage {
171public:
172 BitmapShuttle(SkGPipeCanvas*);
173
174 ~BitmapShuttle();
175
176 virtual bool insert(const SkBitmap& bitmap, int32_t slot) SK_OVERRIDE;
177
178 /**
179 * Remove the SkGPipeCanvas used for insertion. After this, calls to
180 * insert will crash.
181 */
182 void removeCanvas();
183
184private:
185 SkGPipeCanvas* fCanvas;
186};
187
188///////////////////////////////////////////////////////////////////////////////
189
reed@google.combb6992a2011-04-26 17:41:56 +0000190class SkGPipeCanvas : public SkCanvas {
191public:
junov@chromium.orga8db8fe2012-08-15 19:49:22 +0000192 SkGPipeCanvas(SkGPipeController*, SkWriter32*, uint32_t flags,
193 uint32_t width, uint32_t height);
reed@google.combb6992a2011-04-26 17:41:56 +0000194 virtual ~SkGPipeCanvas();
195
scroggo@google.com59c3ab62013-11-12 14:32:38 +0000196 /**
197 * Called when nothing else is to be written to the stream. Any repeated
198 * calls are ignored.
199 *
200 * @param notifyReaders Whether to send a message to the reader(s) that
201 * the writer is through sending commands. Should generally be true,
202 * unless there is an error which prevents further messages from
203 * being sent.
204 */
205 void finish(bool notifyReaders) {
206 if (fDone) {
207 return;
reed@google.combb6992a2011-04-26 17:41:56 +0000208 }
scroggo@google.com59c3ab62013-11-12 14:32:38 +0000209 if (notifyReaders && this->needOpBytes()) {
210 this->writeOp(kDone_DrawOp);
211 this->doNotify();
212 }
213 if (shouldFlattenBitmaps(fFlags)) {
214 // The following circular references exist:
215 // fFlattenableHeap -> fWriteBuffer -> fBitmapStorage -> fExternalStorage -> fCanvas
216 // fBitmapHeap -> fExternalStorage -> fCanvas
217 // fFlattenableHeap -> fBitmapStorage -> fExternalStorage -> fCanvas
218
219 // Break them all by destroying the final link to this SkGPipeCanvas.
220 fBitmapShuttle->removeCanvas();
221 }
222 fDone = true;
reed@google.combb6992a2011-04-26 17:41:56 +0000223 }
224
junov@chromium.org77eec242012-07-18 17:54:45 +0000225 void flushRecording(bool detachCurrentBlock);
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000226 size_t freeMemoryIfPossible(size_t bytesToFree);
junov@chromium.org77eec242012-07-18 17:54:45 +0000227
scroggo@google.com15011ee2012-07-26 20:03:32 +0000228 size_t storageAllocatedForRecording() {
scroggo@google.comd5d158b2012-08-14 20:38:28 +0000229 return (NULL == fBitmapHeap) ? 0 : fBitmapHeap->bytesAllocated();
scroggo@google.com15011ee2012-07-26 20:03:32 +0000230 }
231
reed@google.combb6992a2011-04-26 17:41:56 +0000232 // overrides from SkCanvas
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000233 virtual bool isDrawingToLayer() const SK_OVERRIDE;
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000234 virtual void clear(SkColor) SK_OVERRIDE;
235 virtual void drawPaint(const SkPaint& paint) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000236 virtual void drawPoints(PointMode, size_t count, const SkPoint pts[],
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000237 const SkPaint&) SK_OVERRIDE;
reed@google.com4ed0fb72012-12-12 20:48:18 +0000238 virtual void drawOval(const SkRect&, const SkPaint&) SK_OVERRIDE;
bsalomon@google.com7ce564c2013-10-22 16:54:15 +0000239 virtual void drawRect(const SkRect& rect, const SkPaint&) SK_OVERRIDE;
reed@google.com4ed0fb72012-12-12 20:48:18 +0000240 virtual void drawRRect(const SkRRect&, const SkPaint&) SK_OVERRIDE;
bsalomon@google.com7ce564c2013-10-22 16:54:15 +0000241 virtual void drawPath(const SkPath& path, const SkPaint&) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000242 virtual void drawBitmap(const SkBitmap&, SkScalar left, SkScalar top,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000243 const SkPaint*) SK_OVERRIDE;
reed@google.com71121732012-09-18 15:14:33 +0000244 virtual void drawBitmapRectToRect(const SkBitmap&, const SkRect* src,
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000245 const SkRect& dst, const SkPaint* paint,
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +0000246 DrawBitmapRectFlags flags) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000247 virtual void drawBitmapMatrix(const SkBitmap&, const SkMatrix&,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000248 const SkPaint*) SK_OVERRIDE;
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000249 virtual void drawBitmapNine(const SkBitmap& bitmap, const SkIRect& center,
250 const SkRect& dst, const SkPaint* paint = NULL) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000251 virtual void drawSprite(const SkBitmap&, int left, int top,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000252 const SkPaint*) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000253 virtual void drawVertices(VertexMode, int vertexCount,
254 const SkPoint vertices[], const SkPoint texs[],
255 const SkColor colors[], SkXfermode*,
256 const uint16_t indices[], int indexCount,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000257 const SkPaint&) SK_OVERRIDE;
258 virtual void drawData(const void*, size_t) SK_OVERRIDE;
robertphillips@google.com0a4805e2013-05-29 13:24:23 +0000259 virtual void beginCommentGroup(const char* description) SK_OVERRIDE;
260 virtual void addComment(const char* kywd, const char* value) SK_OVERRIDE;
261 virtual void endCommentGroup() SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000262
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000263 /**
264 * Flatten an SkBitmap to send to the reader, where it will be referenced
265 * according to slot.
266 */
267 bool shuttleBitmap(const SkBitmap&, int32_t slot);
commit-bot@chromium.orged9806f2014-02-21 02:32:36 +0000268
269protected:
Florin Malita5f6102d2014-06-30 10:13:28 -0400270 virtual void willSave() SK_OVERRIDE;
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000271 virtual SaveLayerStrategy willSaveLayer(const SkRect*, const SkPaint*, SaveFlags) SK_OVERRIDE;
272 virtual void willRestore() SK_OVERRIDE;
273
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +0000274 virtual void didConcat(const SkMatrix&) SK_OVERRIDE;
275 virtual void didSetMatrix(const SkMatrix&) SK_OVERRIDE;
276
commit-bot@chromium.orged9806f2014-02-21 02:32:36 +0000277 virtual void onDrawDRRect(const SkRRect&, const SkRRect&, const SkPaint&) SK_OVERRIDE;
reed@google.come0d9ce82014-04-23 04:00:17 +0000278 virtual void onDrawText(const void* text, size_t byteLength, SkScalar x, SkScalar y,
279 const SkPaint&) SK_OVERRIDE;
280 virtual void onDrawPosText(const void* text, size_t byteLength, const SkPoint pos[],
281 const SkPaint&) SK_OVERRIDE;
282 virtual void onDrawPosTextH(const void* text, size_t byteLength, const SkScalar xpos[],
283 SkScalar constY, const SkPaint&) SK_OVERRIDE;
284 virtual void onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& path,
285 const SkMatrix* matrix, const SkPaint&) SK_OVERRIDE;
fmalitab7425172014-08-26 07:56:44 -0700286 virtual void onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
287 const SkPaint& paint) SK_OVERRIDE;
dandovb3c9d1c2014-08-12 08:34:29 -0700288 virtual void onDrawPatch(const SkPoint cubics[12], const SkColor colors[4],
289 const SkPoint texCoords[4], SkXfermode* xmode,
290 const SkPaint& paint) SK_OVERRIDE;
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000291 virtual void onClipRect(const SkRect&, SkRegion::Op, ClipEdgeStyle) SK_OVERRIDE;
292 virtual void onClipRRect(const SkRRect&, SkRegion::Op, ClipEdgeStyle) SK_OVERRIDE;
293 virtual void onClipPath(const SkPath&, SkRegion::Op, ClipEdgeStyle) SK_OVERRIDE;
294 virtual void onClipRegion(const SkRegion&, SkRegion::Op) SK_OVERRIDE;
295
reedd5fa1a42014-08-09 11:08:05 -0700296 virtual void onDrawPicture(const SkPicture*, const SkMatrix*, const SkPaint*) SK_OVERRIDE;
robertphillips9b14f262014-06-04 05:40:44 -0700297
reed@google.combb6992a2011-04-26 17:41:56 +0000298private:
commit-bot@chromium.orgd9ea09e2014-03-25 17:32:26 +0000299 void recordTranslate(const SkMatrix&);
300 void recordScale(const SkMatrix&);
301 void recordConcat(const SkMatrix&);
302
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000303 enum {
304 kNoSaveLayer = -1,
305 };
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000306 SkNamedFactorySet* fFactorySet;
307 int fFirstSaveLayerStackLevel;
scroggo@google.comd9d29672012-08-14 17:21:34 +0000308 SkBitmapHeap* fBitmapHeap;
reed@google.comacd471f2011-05-03 21:26:46 +0000309 SkGPipeController* fController;
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000310 SkWriter32& fWriter;
311 size_t fBlockSize; // amount allocated for writer
312 size_t fBytesNotified;
313 bool fDone;
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000314 const uint32_t fFlags;
reed@google.combb6992a2011-04-26 17:41:56 +0000315
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000316 SkRefCntSet fTypefaceSet;
reed@google.comf5842f72011-05-04 18:30:04 +0000317
318 uint32_t getTypefaceID(SkTypeface*);
319
reed@google.comacd471f2011-05-03 21:26:46 +0000320 inline void writeOp(DrawOps op, unsigned flags, unsigned data) {
reed@google.combb6992a2011-04-26 17:41:56 +0000321 fWriter.write32(DrawOp_packOpFlagData(op, flags, data));
322 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000323
reed@google.comacd471f2011-05-03 21:26:46 +0000324 inline void writeOp(DrawOps op) {
reed@google.combb6992a2011-04-26 17:41:56 +0000325 fWriter.write32(DrawOp_packOpFlagData(op, 0, 0));
326 }
reed@google.comacd471f2011-05-03 21:26:46 +0000327
328 bool needOpBytes(size_t size = 0);
329
330 inline void doNotify() {
331 if (!fDone) {
reed@google.com44699382013-10-31 17:28:30 +0000332 size_t bytes = fWriter.bytesWritten() - fBytesNotified;
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000333 if (bytes > 0) {
334 fController->notifyWritten(bytes);
335 fBytesNotified += bytes;
336 }
reed@google.comacd471f2011-05-03 21:26:46 +0000337 }
338 }
reed@google.comb55d1182011-05-11 00:42:04 +0000339
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000340 // Should be called after any calls to an SkFlatDictionary::findAndReplace
341 // if a new SkFlatData was added when in cross process mode
342 void flattenFactoryNames();
343
scroggo@google.com59c3ab62013-11-12 14:32:38 +0000344 FlattenableHeap fFlattenableHeap;
345 FlatDictionary fFlatDictionary;
346 SkAutoTUnref<BitmapShuttle> fBitmapShuttle;
347 int fCurrFlatIndex[kCount_PaintFlats];
348
reed@google.comb55d1182011-05-11 00:42:04 +0000349 int flattenToIndex(SkFlattenable* obj, PaintFlats);
350
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000351 // Common code used by drawBitmap*. Behaves differently depending on the
352 // type of SkBitmapHeap being used, which is determined by the flags used.
353 bool commonDrawBitmap(const SkBitmap& bm, DrawOps op, unsigned flags,
354 size_t opBytesNeeded, const SkPaint* paint);
scroggo@google.com58be6822012-07-30 14:40:01 +0000355
reed@google.com31891582011-05-12 03:03:56 +0000356 SkPaint fPaint;
357 void writePaint(const SkPaint&);
reed@google.combb6992a2011-04-26 17:41:56 +0000358
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000359 class AutoPipeNotify {
360 public:
361 AutoPipeNotify(SkGPipeCanvas* canvas) : fCanvas(canvas) {}
362 ~AutoPipeNotify() { fCanvas->doNotify(); }
363 private:
364 SkGPipeCanvas* fCanvas;
365 };
366 friend class AutoPipeNotify;
367
reed@google.combb6992a2011-04-26 17:41:56 +0000368 typedef SkCanvas INHERITED;
369};
370
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000371void SkGPipeCanvas::flattenFactoryNames() {
372 const char* name;
373 while ((name = fFactorySet->getNextAddedFactoryName()) != NULL) {
374 size_t len = strlen(name);
commit-bot@chromium.orgd0490172014-05-14 15:12:08 +0000375 if (this->needOpBytes(SkWriter32::WriteStringSize(name, len))) {
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000376 this->writeOp(kDef_Factory_DrawOp);
377 fWriter.writeString(name, len);
378 }
379 }
380}
381
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000382bool SkGPipeCanvas::shuttleBitmap(const SkBitmap& bm, int32_t slot) {
scroggo@google.com565254b2012-06-28 15:41:32 +0000383 SkASSERT(shouldFlattenBitmaps(fFlags));
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000384 SkWriteBuffer buffer;
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000385 buffer.setNamedFactoryRecorder(fFactorySet);
scroggo@google.com74b7ffd2013-04-30 02:32:41 +0000386 buffer.writeBitmap(bm);
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000387 this->flattenFactoryNames();
commit-bot@chromium.orgb45bd1f2014-04-24 18:17:30 +0000388 size_t size = buffer.bytesWritten();
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000389 if (this->needOpBytes(size)) {
390 this->writeOp(kDef_Bitmap_DrawOp, 0, slot);
391 void* dst = static_cast<void*>(fWriter.reserve(size));
392 buffer.writeToMemory(dst);
393 return true;
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000394 }
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000395 return false;
scroggo@google.comd3ba5cc2012-07-09 16:05:53 +0000396}
397
reed@google.comb55d1182011-05-11 00:42:04 +0000398// return 0 for NULL (or unflattenable obj), or index-base-1
scroggo@google.comd3ba5cc2012-07-09 16:05:53 +0000399// return ~(index-base-1) if an old flattenable was replaced
reed@google.comb55d1182011-05-11 00:42:04 +0000400int SkGPipeCanvas::flattenToIndex(SkFlattenable* obj, PaintFlats paintflat) {
scroggo@google.comd5d158b2012-08-14 20:38:28 +0000401 SkASSERT(!fDone && fBitmapHeap != NULL);
reed@google.comb55d1182011-05-11 00:42:04 +0000402 if (NULL == obj) {
403 return 0;
404 }
reed@google.comb55d1182011-05-11 00:42:04 +0000405
scroggo@google.comd9d29672012-08-14 17:21:34 +0000406 fBitmapHeap->deferAddingOwners();
scroggo@google.com4dffc592012-07-17 16:49:40 +0000407 bool added, replaced;
scroggo@google.com664fab12012-08-14 19:22:05 +0000408 const SkFlatData* flat = fFlatDictionary.findAndReplace(*obj, fFlattenableHeap.flatToReplace(),
409 &added, &replaced);
scroggo@google.comd9d29672012-08-14 17:21:34 +0000410 fBitmapHeap->endAddingOwnersDeferral(added);
scroggo@google.com4dffc592012-07-17 16:49:40 +0000411 int index = flat->index();
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000412 if (added) {
413 if (isCrossProcess(fFlags)) {
414 this->flattenFactoryNames();
415 }
416 size_t flatSize = flat->flatSize();
417 if (this->needOpBytes(flatSize)) {
418 this->writeOp(kDef_Flattenable_DrawOp, paintflat, index);
419 fWriter.write(flat->data(), flatSize);
420 }
scroggo@google.com4dffc592012-07-17 16:49:40 +0000421 }
422 if (replaced) {
reed@google.comb55d1182011-05-11 00:42:04 +0000423 index = ~index;
reed@google.comb55d1182011-05-11 00:42:04 +0000424 }
scroggo@google.com4dffc592012-07-17 16:49:40 +0000425 return index;
reed@google.comb55d1182011-05-11 00:42:04 +0000426}
427
reed@google.combb6992a2011-04-26 17:41:56 +0000428///////////////////////////////////////////////////////////////////////////////
429
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000430#define MIN_BLOCK_SIZE (16 * 1024)
scroggo@google.com4dffc592012-07-17 16:49:40 +0000431#define BITMAPS_TO_KEEP 5
432#define FLATTENABLES_TO_KEEP 10
reed@google.combb6992a2011-04-26 17:41:56 +0000433
reed@google.comacd471f2011-05-03 21:26:46 +0000434SkGPipeCanvas::SkGPipeCanvas(SkGPipeController* controller,
rmistry@google.comd6176b02012-08-23 18:14:13 +0000435 SkWriter32* writer, uint32_t flags,
junov@chromium.orga8db8fe2012-08-15 19:49:22 +0000436 uint32_t width, uint32_t height)
commit-bot@chromium.org403f8d72014-02-17 15:24:26 +0000437 : SkCanvas(width, height)
438 , fFactorySet(isCrossProcess(flags) ? SkNEW(SkNamedFactorySet) : NULL)
439 , fWriter(*writer)
440 , fFlags(flags)
441 , fFlattenableHeap(FLATTENABLES_TO_KEEP, fFactorySet, isCrossProcess(flags))
442 , fFlatDictionary(&fFlattenableHeap)
443{
reed@google.comacd471f2011-05-03 21:26:46 +0000444 fController = controller;
reed@google.combb6992a2011-04-26 17:41:56 +0000445 fDone = false;
reed@google.comacd471f2011-05-03 21:26:46 +0000446 fBlockSize = 0; // need first block from controller
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000447 fBytesNotified = 0;
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000448 fFirstSaveLayerStackLevel = kNoSaveLayer;
reed@google.comb55d1182011-05-11 00:42:04 +0000449 sk_bzero(fCurrFlatIndex, sizeof(fCurrFlatIndex));
reed@google.comacd471f2011-05-03 21:26:46 +0000450
scroggo@google.com565254b2012-06-28 15:41:32 +0000451 // Tell the reader the appropriate flags to use.
452 if (this->needOpBytes()) {
453 this->writeOp(kReportFlags_DrawOp, fFlags, 0);
454 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000455
scroggo@google.com10dccde2012-08-08 20:43:22 +0000456 if (shouldFlattenBitmaps(flags)) {
scroggo@google.com59c3ab62013-11-12 14:32:38 +0000457 fBitmapShuttle.reset(SkNEW_ARGS(BitmapShuttle, (this)));
458 fBitmapHeap = SkNEW_ARGS(SkBitmapHeap, (fBitmapShuttle.get(), BITMAPS_TO_KEEP));
scroggo@google.com10dccde2012-08-08 20:43:22 +0000459 } else {
scroggo@google.comd9d29672012-08-14 17:21:34 +0000460 fBitmapHeap = SkNEW_ARGS(SkBitmapHeap,
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000461 (BITMAPS_TO_KEEP, controller->numberOfReaders()));
scroggo@google.com10dccde2012-08-08 20:43:22 +0000462 if (this->needOpBytes(sizeof(void*))) {
scroggo@google.comd9d29672012-08-14 17:21:34 +0000463 this->writeOp(kShareBitmapHeap_DrawOp);
464 fWriter.writePtr(static_cast<void*>(fBitmapHeap));
scroggo@google.com10dccde2012-08-08 20:43:22 +0000465 }
466 }
scroggo@google.comd9d29672012-08-14 17:21:34 +0000467 fFlattenableHeap.setBitmapStorage(fBitmapHeap);
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000468 this->doNotify();
reed@google.combb6992a2011-04-26 17:41:56 +0000469}
470
471SkGPipeCanvas::~SkGPipeCanvas() {
scroggo@google.com59c3ab62013-11-12 14:32:38 +0000472 this->finish(true);
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000473 SkSafeUnref(fFactorySet);
scroggo@google.comd9d29672012-08-14 17:21:34 +0000474 SkSafeUnref(fBitmapHeap);
reed@google.combb6992a2011-04-26 17:41:56 +0000475}
476
reed@google.comacd471f2011-05-03 21:26:46 +0000477bool SkGPipeCanvas::needOpBytes(size_t needed) {
478 if (fDone) {
479 return false;
480 }
481
482 needed += 4; // size of DrawOp atom
commit-bot@chromium.orgd0490172014-05-14 15:12:08 +0000483 needed = SkAlign4(needed);
reed@google.com44699382013-10-31 17:28:30 +0000484 if (fWriter.bytesWritten() + needed > fBlockSize) {
mtklein6d88e6c2014-07-30 09:17:54 -0700485 // Before we wipe out any data that has already been written, read it out.
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000486 this->doNotify();
mtklein6d88e6c2014-07-30 09:17:54 -0700487
488 // If we're going to allocate a new block, allocate enough to make it worthwhile.
489 needed = SkTMax<size_t>(MIN_BLOCK_SIZE, needed);
490
commit-bot@chromium.orgd0490172014-05-14 15:12:08 +0000491 void* block = fController->requestBlock(needed, &fBlockSize);
reed@google.comacd471f2011-05-03 21:26:46 +0000492 if (NULL == block) {
scroggo@google.com59c3ab62013-11-12 14:32:38 +0000493 // Do not notify the readers, which would call this function again.
494 this->finish(false);
reed@google.comacd471f2011-05-03 21:26:46 +0000495 return false;
496 }
scroggo@google.com460a23e2012-08-16 17:56:49 +0000497 SkASSERT(SkIsAlign4(fBlockSize));
reed@google.comacd471f2011-05-03 21:26:46 +0000498 fWriter.reset(block, fBlockSize);
499 fBytesNotified = 0;
500 }
501 return true;
502}
503
reed@google.comf5842f72011-05-04 18:30:04 +0000504uint32_t SkGPipeCanvas::getTypefaceID(SkTypeface* face) {
505 uint32_t id = 0; // 0 means default/null typeface
506 if (face) {
507 id = fTypefaceSet.find(face);
508 if (0 == id) {
509 id = fTypefaceSet.add(face);
510 size_t size = writeTypeface(NULL, face);
511 if (this->needOpBytes(size)) {
reed@google.combb6793b2011-05-05 15:18:15 +0000512 this->writeOp(kDef_Typeface_DrawOp);
reed@google.comf5842f72011-05-04 18:30:04 +0000513 writeTypeface(&fWriter, face);
514 }
515 }
516 }
517 return id;
518}
519
reed@google.combb6992a2011-04-26 17:41:56 +0000520///////////////////////////////////////////////////////////////////////////////
521
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000522#define NOTIFY_SETUP(canvas) \
523 AutoPipeNotify apn(canvas)
524
Florin Malita5f6102d2014-06-30 10:13:28 -0400525void SkGPipeCanvas::willSave() {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000526 NOTIFY_SETUP(this);
reed@google.comacd471f2011-05-03 21:26:46 +0000527 if (this->needOpBytes()) {
Florin Malita5f6102d2014-06-30 10:13:28 -0400528 this->writeOp(kSave_DrawOp);
reed@google.comacd471f2011-05-03 21:26:46 +0000529 }
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000530
Florin Malita5f6102d2014-06-30 10:13:28 -0400531 this->INHERITED::willSave();
reed@google.combb6992a2011-04-26 17:41:56 +0000532}
533
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000534SkCanvas::SaveLayerStrategy SkGPipeCanvas::willSaveLayer(const SkRect* bounds, const SkPaint* paint,
535 SaveFlags saveFlags) {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000536 NOTIFY_SETUP(this);
reed@google.comacd471f2011-05-03 21:26:46 +0000537 size_t size = 0;
reed@google.combb6992a2011-04-26 17:41:56 +0000538 unsigned opFlags = 0;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000539
reed@google.combb6992a2011-04-26 17:41:56 +0000540 if (bounds) {
541 opFlags |= kSaveLayer_HasBounds_DrawOpFlag;
reed@google.comacd471f2011-05-03 21:26:46 +0000542 size += sizeof(SkRect);
reed@google.combb6992a2011-04-26 17:41:56 +0000543 }
544 if (paint) {
545 opFlags |= kSaveLayer_HasPaint_DrawOpFlag;
reed@google.com31891582011-05-12 03:03:56 +0000546 this->writePaint(*paint);
reed@google.combb6992a2011-04-26 17:41:56 +0000547 }
548
reed@google.comacd471f2011-05-03 21:26:46 +0000549 if (this->needOpBytes(size)) {
550 this->writeOp(kSaveLayer_DrawOp, opFlags, saveFlags);
551 if (bounds) {
552 fWriter.writeRect(*bounds);
553 }
reed@google.combb6992a2011-04-26 17:41:56 +0000554 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000555
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000556 if (kNoSaveLayer == fFirstSaveLayerStackLevel){
557 fFirstSaveLayerStackLevel = this->getSaveCount();
558 }
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000559
560 this->INHERITED::willSaveLayer(bounds, paint, saveFlags);
561 // we don't create a layer
562 return kNoLayer_SaveLayerStrategy;
reed@google.combb6992a2011-04-26 17:41:56 +0000563}
564
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000565void SkGPipeCanvas::willRestore() {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000566 NOTIFY_SETUP(this);
reed@google.comacd471f2011-05-03 21:26:46 +0000567 if (this->needOpBytes()) {
568 this->writeOp(kRestore_DrawOp);
569 }
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000570
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000571 if (this->getSaveCount() - 1 == fFirstSaveLayerStackLevel){
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000572 fFirstSaveLayerStackLevel = kNoSaveLayer;
573 }
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000574
575 this->INHERITED::willRestore();
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000576}
577
578bool SkGPipeCanvas::isDrawingToLayer() const {
579 return kNoSaveLayer != fFirstSaveLayerStackLevel;
reed@google.combb6992a2011-04-26 17:41:56 +0000580}
581
commit-bot@chromium.orgd9ea09e2014-03-25 17:32:26 +0000582void SkGPipeCanvas::recordTranslate(const SkMatrix& m) {
583 if (this->needOpBytes(2 * sizeof(SkScalar))) {
584 this->writeOp(kTranslate_DrawOp);
585 fWriter.writeScalar(m.getTranslateX());
586 fWriter.writeScalar(m.getTranslateY());
reed@google.combb6992a2011-04-26 17:41:56 +0000587 }
reed@google.combb6992a2011-04-26 17:41:56 +0000588}
589
commit-bot@chromium.orgd9ea09e2014-03-25 17:32:26 +0000590void SkGPipeCanvas::recordScale(const SkMatrix& m) {
591 if (this->needOpBytes(2 * sizeof(SkScalar))) {
592 this->writeOp(kScale_DrawOp);
593 fWriter.writeScalar(m.getScaleX());
594 fWriter.writeScalar(m.getScaleY());
reed@google.combb6992a2011-04-26 17:41:56 +0000595 }
reed@google.combb6992a2011-04-26 17:41:56 +0000596}
597
commit-bot@chromium.orgd9ea09e2014-03-25 17:32:26 +0000598void SkGPipeCanvas::recordConcat(const SkMatrix& m) {
599 if (this->needOpBytes(m.writeToMemory(NULL))) {
600 this->writeOp(kConcat_DrawOp);
601 fWriter.writeMatrix(m);
reed@google.combb6992a2011-04-26 17:41:56 +0000602 }
reed@google.combb6992a2011-04-26 17:41:56 +0000603}
604
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +0000605void SkGPipeCanvas::didConcat(const SkMatrix& matrix) {
reed@google.combb6992a2011-04-26 17:41:56 +0000606 if (!matrix.isIdentity()) {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000607 NOTIFY_SETUP(this);
commit-bot@chromium.orgd9ea09e2014-03-25 17:32:26 +0000608 switch (matrix.getType()) {
609 case SkMatrix::kTranslate_Mask:
610 this->recordTranslate(matrix);
611 break;
612 case SkMatrix::kScale_Mask:
613 this->recordScale(matrix);
614 break;
615 default:
616 this->recordConcat(matrix);
617 break;
reed@google.comacd471f2011-05-03 21:26:46 +0000618 }
reed@google.combb6992a2011-04-26 17:41:56 +0000619 }
commit-bot@chromium.orgd9ea09e2014-03-25 17:32:26 +0000620
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +0000621 this->INHERITED::didConcat(matrix);
reed@google.combb6992a2011-04-26 17:41:56 +0000622}
623
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +0000624void SkGPipeCanvas::didSetMatrix(const SkMatrix& matrix) {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000625 NOTIFY_SETUP(this);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000626 if (this->needOpBytes(matrix.writeToMemory(NULL))) {
reed@google.comacd471f2011-05-03 21:26:46 +0000627 this->writeOp(kSetMatrix_DrawOp);
djsollen@google.com2b2ede32012-04-12 13:24:04 +0000628 fWriter.writeMatrix(matrix);
reed@google.comacd471f2011-05-03 21:26:46 +0000629 }
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +0000630 this->INHERITED::didSetMatrix(matrix);
reed@google.combb6992a2011-04-26 17:41:56 +0000631}
632
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000633void SkGPipeCanvas::onClipRect(const SkRect& rect, SkRegion::Op rgnOp,
634 ClipEdgeStyle edgeStyle) {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000635 NOTIFY_SETUP(this);
scroggo@google.com460a23e2012-08-16 17:56:49 +0000636 if (this->needOpBytes(sizeof(SkRect))) {
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000637 unsigned flags = 0;
638 if (kSoft_ClipEdgeStyle == edgeStyle) {
639 flags = kClip_HasAntiAlias_DrawOpFlag;
640 }
scroggo@google.com460a23e2012-08-16 17:56:49 +0000641 this->writeOp(kClipRect_DrawOp, flags, rgnOp);
reed@google.comacd471f2011-05-03 21:26:46 +0000642 fWriter.writeRect(rect);
643 }
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000644 this->INHERITED::onClipRect(rect, rgnOp, edgeStyle);
reed@google.combb6992a2011-04-26 17:41:56 +0000645}
646
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000647void SkGPipeCanvas::onClipRRect(const SkRRect& rrect, SkRegion::Op rgnOp,
648 ClipEdgeStyle edgeStyle) {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000649 NOTIFY_SETUP(this);
reed@google.com4ed0fb72012-12-12 20:48:18 +0000650 if (this->needOpBytes(kSizeOfFlatRRect)) {
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000651 unsigned flags = 0;
652 if (kSoft_ClipEdgeStyle == edgeStyle) {
653 flags = kClip_HasAntiAlias_DrawOpFlag;
654 }
reed@google.com4ed0fb72012-12-12 20:48:18 +0000655 this->writeOp(kClipRRect_DrawOp, flags, rgnOp);
656 fWriter.writeRRect(rrect);
657 }
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000658 this->INHERITED::onClipRRect(rrect, rgnOp, edgeStyle);
reed@google.com4ed0fb72012-12-12 20:48:18 +0000659}
660
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000661void SkGPipeCanvas::onClipPath(const SkPath& path, SkRegion::Op rgnOp,
662 ClipEdgeStyle edgeStyle) {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000663 NOTIFY_SETUP(this);
scroggo@google.com460a23e2012-08-16 17:56:49 +0000664 if (this->needOpBytes(path.writeToMemory(NULL))) {
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000665 unsigned flags = 0;
666 if (kSoft_ClipEdgeStyle == edgeStyle) {
667 flags = kClip_HasAntiAlias_DrawOpFlag;
668 }
scroggo@google.com460a23e2012-08-16 17:56:49 +0000669 this->writeOp(kClipPath_DrawOp, flags, rgnOp);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000670 fWriter.writePath(path);
reed@google.comacd471f2011-05-03 21:26:46 +0000671 }
reed@google.combb6992a2011-04-26 17:41:56 +0000672 // we just pass on the bounds of the path
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000673 this->INHERITED::onClipRect(path.getBounds(), rgnOp, edgeStyle);
reed@google.combb6992a2011-04-26 17:41:56 +0000674}
675
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000676void SkGPipeCanvas::onClipRegion(const SkRegion& region, SkRegion::Op rgnOp) {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000677 NOTIFY_SETUP(this);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000678 if (this->needOpBytes(region.writeToMemory(NULL))) {
reed@google.comacd471f2011-05-03 21:26:46 +0000679 this->writeOp(kClipRegion_DrawOp, 0, rgnOp);
djsollen@google.com2b2ede32012-04-12 13:24:04 +0000680 fWriter.writeRegion(region);
reed@google.comacd471f2011-05-03 21:26:46 +0000681 }
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000682 this->INHERITED::onClipRegion(region, rgnOp);
reed@google.combb6992a2011-04-26 17:41:56 +0000683}
684
685///////////////////////////////////////////////////////////////////////////////
686
687void SkGPipeCanvas::clear(SkColor color) {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000688 NOTIFY_SETUP(this);
reed@google.combb6992a2011-04-26 17:41:56 +0000689 unsigned flags = 0;
690 if (color) {
691 flags |= kClear_HasColor_DrawOpFlag;
692 }
reed@google.comacd471f2011-05-03 21:26:46 +0000693 if (this->needOpBytes(sizeof(SkColor))) {
694 this->writeOp(kDrawClear_DrawOp, flags, 0);
695 if (color) {
696 fWriter.write32(color);
697 }
reed@google.combb6992a2011-04-26 17:41:56 +0000698 }
699}
700
701void SkGPipeCanvas::drawPaint(const SkPaint& paint) {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000702 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000703 this->writePaint(paint);
reed@google.comacd471f2011-05-03 21:26:46 +0000704 if (this->needOpBytes()) {
reed@google.com31891582011-05-12 03:03:56 +0000705 this->writeOp(kDrawPaint_DrawOp);
reed@google.comacd471f2011-05-03 21:26:46 +0000706 }
reed@google.combb6992a2011-04-26 17:41:56 +0000707}
708
709void SkGPipeCanvas::drawPoints(PointMode mode, size_t count,
robertphillips@google.com8b169312013-10-15 17:47:36 +0000710 const SkPoint pts[], const SkPaint& paint) {
reed@google.combb6992a2011-04-26 17:41:56 +0000711 if (count) {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000712 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000713 this->writePaint(paint);
reed@google.comacd471f2011-05-03 21:26:46 +0000714 if (this->needOpBytes(4 + count * sizeof(SkPoint))) {
reed@google.com31891582011-05-12 03:03:56 +0000715 this->writeOp(kDrawPoints_DrawOp, mode, 0);
commit-bot@chromium.orgb45bd1f2014-04-24 18:17:30 +0000716 fWriter.write32(SkToU32(count));
reed@google.comacd471f2011-05-03 21:26:46 +0000717 fWriter.write(pts, count * sizeof(SkPoint));
718 }
reed@google.combb6992a2011-04-26 17:41:56 +0000719 }
720}
721
reed@google.com4ed0fb72012-12-12 20:48:18 +0000722void SkGPipeCanvas::drawOval(const SkRect& rect, const SkPaint& paint) {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000723 NOTIFY_SETUP(this);
reed@google.com4ed0fb72012-12-12 20:48:18 +0000724 this->writePaint(paint);
725 if (this->needOpBytes(sizeof(SkRect))) {
726 this->writeOp(kDrawOval_DrawOp);
727 fWriter.writeRect(rect);
728 }
729}
730
bsalomon@google.com7ce564c2013-10-22 16:54:15 +0000731void SkGPipeCanvas::drawRect(const SkRect& rect, const SkPaint& paint) {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000732 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000733 this->writePaint(paint);
reed@google.comacd471f2011-05-03 21:26:46 +0000734 if (this->needOpBytes(sizeof(SkRect))) {
reed@google.com31891582011-05-12 03:03:56 +0000735 this->writeOp(kDrawRect_DrawOp);
reed@google.comacd471f2011-05-03 21:26:46 +0000736 fWriter.writeRect(rect);
737 }
reed@google.combb6992a2011-04-26 17:41:56 +0000738}
739
reed@google.com4ed0fb72012-12-12 20:48:18 +0000740void SkGPipeCanvas::drawRRect(const SkRRect& rrect, const SkPaint& paint) {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000741 NOTIFY_SETUP(this);
reed@google.com4ed0fb72012-12-12 20:48:18 +0000742 this->writePaint(paint);
743 if (this->needOpBytes(kSizeOfFlatRRect)) {
744 this->writeOp(kDrawRRect_DrawOp);
745 fWriter.writeRRect(rrect);
746 }
747}
748
commit-bot@chromium.orged9806f2014-02-21 02:32:36 +0000749void SkGPipeCanvas::onDrawDRRect(const SkRRect& outer, const SkRRect& inner,
750 const SkPaint& paint) {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000751 NOTIFY_SETUP(this);
commit-bot@chromium.orged9806f2014-02-21 02:32:36 +0000752 this->writePaint(paint);
753 if (this->needOpBytes(kSizeOfFlatRRect * 2)) {
754 this->writeOp(kDrawDRRect_DrawOp);
755 fWriter.writeRRect(outer);
756 fWriter.writeRRect(inner);
757 }
758}
759
bsalomon@google.com7ce564c2013-10-22 16:54:15 +0000760void SkGPipeCanvas::drawPath(const SkPath& path, const SkPaint& paint) {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000761 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000762 this->writePaint(paint);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000763 if (this->needOpBytes(path.writeToMemory(NULL))) {
reed@google.com31891582011-05-12 03:03:56 +0000764 this->writeOp(kDrawPath_DrawOp);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000765 fWriter.writePath(path);
reed@google.comacd471f2011-05-03 21:26:46 +0000766 }
reed@google.combb6992a2011-04-26 17:41:56 +0000767}
768
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000769bool SkGPipeCanvas::commonDrawBitmap(const SkBitmap& bm, DrawOps op,
770 unsigned flags,
771 size_t opBytesNeeded,
772 const SkPaint* paint) {
commit-bot@chromium.org44803fb2014-05-14 15:38:25 +0000773 if (fDone) {
774 return false;
775 }
776
scroggo@google.com58be6822012-07-30 14:40:01 +0000777 if (paint != NULL) {
scroggo@google.com460a23e2012-08-16 17:56:49 +0000778 flags |= kDrawBitmap_HasPaint_DrawOpFlag;
scroggo@google.com58be6822012-07-30 14:40:01 +0000779 this->writePaint(*paint);
780 }
commit-bot@chromium.org44803fb2014-05-14 15:38:25 +0000781 // This needs to run first so its calls to needOpBytes() and its writes
782 // don't interlace with the needOpBytes() and write below.
783 SkASSERT(fBitmapHeap != NULL);
784 int32_t bitmapIndex = fBitmapHeap->insert(bm);
785 if (SkBitmapHeap::INVALID_SLOT == bitmapIndex) {
786 return false;
787 }
788
scroggo@google.com10dccde2012-08-08 20:43:22 +0000789 if (this->needOpBytes(opBytesNeeded)) {
790 this->writeOp(op, flags, bitmapIndex);
scroggo@google.com58be6822012-07-30 14:40:01 +0000791 return true;
792 }
793 return false;
794}
795
796void SkGPipeCanvas::drawBitmap(const SkBitmap& bm, SkScalar left, SkScalar top,
797 const SkPaint* paint) {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000798 NOTIFY_SETUP(this);
scroggo@google.com58be6822012-07-30 14:40:01 +0000799 size_t opBytesNeeded = sizeof(SkScalar) * 2;
800
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000801 if (this->commonDrawBitmap(bm, kDrawBitmap_DrawOp, 0, opBytesNeeded, paint)) {
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000802 fWriter.writeScalar(left);
803 fWriter.writeScalar(top);
804 }
reed@google.combb6992a2011-04-26 17:41:56 +0000805}
806
reed@google.com71121732012-09-18 15:14:33 +0000807void SkGPipeCanvas::drawBitmapRectToRect(const SkBitmap& bm, const SkRect* src,
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +0000808 const SkRect& dst, const SkPaint* paint,
809 DrawBitmapRectFlags dbmrFlags) {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000810 NOTIFY_SETUP(this);
scroggo@google.com58be6822012-07-30 14:40:01 +0000811 size_t opBytesNeeded = sizeof(SkRect);
812 bool hasSrc = src != NULL;
813 unsigned flags;
814 if (hasSrc) {
scroggo@google.com460a23e2012-08-16 17:56:49 +0000815 flags = kDrawBitmap_HasSrcRect_DrawOpFlag;
scroggo@google.com58be6822012-07-30 14:40:01 +0000816 opBytesNeeded += sizeof(int32_t) * 4;
817 } else {
818 flags = 0;
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000819 }
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +0000820 if (dbmrFlags & kBleed_DrawBitmapRectFlag) {
821 flags |= kDrawBitmap_Bleed_DrawOpFlag;
822 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000823
reed@google.com71121732012-09-18 15:14:33 +0000824 if (this->commonDrawBitmap(bm, kDrawBitmapRectToRect_DrawOp, flags, opBytesNeeded, paint)) {
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000825 if (hasSrc) {
reed@google.com71121732012-09-18 15:14:33 +0000826 fWriter.writeRect(*src);
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000827 }
828 fWriter.writeRect(dst);
829 }
reed@google.combb6992a2011-04-26 17:41:56 +0000830}
831
scroggo@google.com58be6822012-07-30 14:40:01 +0000832void SkGPipeCanvas::drawBitmapMatrix(const SkBitmap& bm, const SkMatrix& matrix,
833 const SkPaint* paint) {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000834 NOTIFY_SETUP(this);
scroggo@google.com58be6822012-07-30 14:40:01 +0000835 size_t opBytesNeeded = matrix.writeToMemory(NULL);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000836
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000837 if (this->commonDrawBitmap(bm, kDrawBitmapMatrix_DrawOp, 0, opBytesNeeded, paint)) {
scroggo@google.com58be6822012-07-30 14:40:01 +0000838 fWriter.writeMatrix(matrix);
839 }
reed@google.combb6992a2011-04-26 17:41:56 +0000840}
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000841
842void SkGPipeCanvas::drawBitmapNine(const SkBitmap& bm, const SkIRect& center,
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000843 const SkRect& dst, const SkPaint* paint) {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000844 NOTIFY_SETUP(this);
scroggo@google.com58be6822012-07-30 14:40:01 +0000845 size_t opBytesNeeded = sizeof(int32_t) * 4 + sizeof(SkRect);
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000846
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000847 if (this->commonDrawBitmap(bm, kDrawBitmapNine_DrawOp, 0, opBytesNeeded, paint)) {
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000848 fWriter.write32(center.fLeft);
849 fWriter.write32(center.fTop);
850 fWriter.write32(center.fRight);
851 fWriter.write32(center.fBottom);
852 fWriter.writeRect(dst);
853 }
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000854}
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000855
856void SkGPipeCanvas::drawSprite(const SkBitmap& bm, int left, int top,
857 const SkPaint* paint) {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000858 NOTIFY_SETUP(this);
scroggo@google.com58be6822012-07-30 14:40:01 +0000859 size_t opBytesNeeded = sizeof(int32_t) * 2;
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000860
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000861 if (this->commonDrawBitmap(bm, kDrawSprite_DrawOp, 0, opBytesNeeded, paint)) {
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000862 fWriter.write32(left);
863 fWriter.write32(top);
864 }
reed@google.combb6992a2011-04-26 17:41:56 +0000865}
866
reed@google.come0d9ce82014-04-23 04:00:17 +0000867void SkGPipeCanvas::onDrawText(const void* text, size_t byteLength, SkScalar x, SkScalar y,
868 const SkPaint& paint) {
reed@google.combb6992a2011-04-26 17:41:56 +0000869 if (byteLength) {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000870 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000871 this->writePaint(paint);
reed@google.comacd471f2011-05-03 21:26:46 +0000872 if (this->needOpBytes(4 + SkAlign4(byteLength) + 2 * sizeof(SkScalar))) {
reed@google.com31891582011-05-12 03:03:56 +0000873 this->writeOp(kDrawText_DrawOp);
commit-bot@chromium.orgb45bd1f2014-04-24 18:17:30 +0000874 fWriter.write32(SkToU32(byteLength));
reed@google.comacd471f2011-05-03 21:26:46 +0000875 fWriter.writePad(text, byteLength);
876 fWriter.writeScalar(x);
877 fWriter.writeScalar(y);
878 }
reed@google.combb6992a2011-04-26 17:41:56 +0000879 }
880}
881
reed@google.come0d9ce82014-04-23 04:00:17 +0000882void SkGPipeCanvas::onDrawPosText(const void* text, size_t byteLength, const SkPoint pos[],
883 const SkPaint& paint) {
reed@google.combb6992a2011-04-26 17:41:56 +0000884 if (byteLength) {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000885 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000886 this->writePaint(paint);
reed@google.combb6992a2011-04-26 17:41:56 +0000887 int count = paint.textToGlyphs(text, byteLength, NULL);
reed@google.comacd471f2011-05-03 21:26:46 +0000888 if (this->needOpBytes(4 + SkAlign4(byteLength) + 4 + count * sizeof(SkPoint))) {
reed@google.com31891582011-05-12 03:03:56 +0000889 this->writeOp(kDrawPosText_DrawOp);
commit-bot@chromium.orgb45bd1f2014-04-24 18:17:30 +0000890 fWriter.write32(SkToU32(byteLength));
reed@google.comacd471f2011-05-03 21:26:46 +0000891 fWriter.writePad(text, byteLength);
892 fWriter.write32(count);
893 fWriter.write(pos, count * sizeof(SkPoint));
894 }
reed@google.combb6992a2011-04-26 17:41:56 +0000895 }
896}
897
reed@google.come0d9ce82014-04-23 04:00:17 +0000898void SkGPipeCanvas::onDrawPosTextH(const void* text, size_t byteLength, const SkScalar xpos[],
899 SkScalar constY, const SkPaint& paint) {
reed@google.combb6992a2011-04-26 17:41:56 +0000900 if (byteLength) {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000901 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000902 this->writePaint(paint);
reed@google.combb6992a2011-04-26 17:41:56 +0000903 int count = paint.textToGlyphs(text, byteLength, NULL);
reed@google.comacd471f2011-05-03 21:26:46 +0000904 if (this->needOpBytes(4 + SkAlign4(byteLength) + 4 + count * sizeof(SkScalar) + 4)) {
reed@google.com31891582011-05-12 03:03:56 +0000905 this->writeOp(kDrawPosTextH_DrawOp);
commit-bot@chromium.orgb45bd1f2014-04-24 18:17:30 +0000906 fWriter.write32(SkToU32(byteLength));
reed@google.comacd471f2011-05-03 21:26:46 +0000907 fWriter.writePad(text, byteLength);
908 fWriter.write32(count);
909 fWriter.write(xpos, count * sizeof(SkScalar));
910 fWriter.writeScalar(constY);
911 }
reed@google.combb6992a2011-04-26 17:41:56 +0000912 }
913}
914
reed@google.come0d9ce82014-04-23 04:00:17 +0000915void SkGPipeCanvas::onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& path,
916 const SkMatrix* matrix, const SkPaint& paint) {
reed@google.combb6992a2011-04-26 17:41:56 +0000917 if (byteLength) {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +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
commit-bot@chromium.orgb45bd1f2014-04-24 18:17:30 +0000929 fWriter.write32(SkToU32(byteLength));
reed@google.comacd471f2011-05-03 21:26:46 +0000930 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
fmalitab7425172014-08-26 07:56:44 -0700940void SkGPipeCanvas::onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
941 const SkPaint& paint) {
942 // FIXME: blob serialization only supports SkWriteBuffers
943 // -- convert to SkWriter32 to avoid unrolling?
944 this->INHERITED::onDrawTextBlob(blob, x, y, paint);
945}
946
reedd5fa1a42014-08-09 11:08:05 -0700947void SkGPipeCanvas::onDrawPicture(const SkPicture* picture, const SkMatrix* matrix,
948 const SkPaint* paint) {
reed@google.com0faac1e2011-05-11 05:58:58 +0000949 // we want to playback the picture into individual draw calls
reedd5fa1a42014-08-09 11:08:05 -0700950 //
951 // todo: do we always have to unroll? If the pipe is not cross-process, seems like
952 // we could just ref the picture and move on...? <reed, scroggo>
953 //
954 this->INHERITED::onDrawPicture(picture, matrix, paint);
reed@google.combb6992a2011-04-26 17:41:56 +0000955}
956
reed@google.com85e143c2013-12-30 15:51:25 +0000957void SkGPipeCanvas::drawVertices(VertexMode vmode, int vertexCount,
reed@google.combb6992a2011-04-26 17:41:56 +0000958 const SkPoint vertices[], const SkPoint texs[],
reed@google.com85e143c2013-12-30 15:51:25 +0000959 const SkColor colors[], SkXfermode* xfer,
reed@google.combb6992a2011-04-26 17:41:56 +0000960 const uint16_t indices[], int indexCount,
961 const SkPaint& paint) {
962 if (0 == vertexCount) {
963 return;
964 }
965
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000966 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000967 this->writePaint(paint);
commit-bot@chromium.orgd0490172014-05-14 15:12:08 +0000968
969 unsigned flags = 0; // packs with the op, so needs no extra space
970
971 size_t size = 0;
972 size += 4; // vmode
973 size += 4; // vertex count
974 size += vertexCount * sizeof(SkPoint); // vertices
975
reed@google.combb6992a2011-04-26 17:41:56 +0000976 if (texs) {
977 flags |= kDrawVertices_HasTexs_DrawOpFlag;
reed@google.comacd471f2011-05-03 21:26:46 +0000978 size += vertexCount * sizeof(SkPoint);
reed@google.combb6992a2011-04-26 17:41:56 +0000979 }
980 if (colors) {
981 flags |= kDrawVertices_HasColors_DrawOpFlag;
reed@google.comacd471f2011-05-03 21:26:46 +0000982 size += vertexCount * sizeof(SkColor);
reed@google.combb6992a2011-04-26 17:41:56 +0000983 }
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000984 if (xfer && !SkXfermode::IsMode(xfer, SkXfermode::kModulate_Mode)) {
985 flags |= kDrawVertices_HasXfermode_DrawOpFlag;
commit-bot@chromium.orgd0490172014-05-14 15:12:08 +0000986 size += sizeof(int32_t); // SkXfermode::Mode
987 }
988 if (indices && indexCount > 0) {
989 flags |= kDrawVertices_HasIndices_DrawOpFlag;
990 size += 4; // index count
991 size += SkAlign4(indexCount * sizeof(uint16_t)); // indices
reed@google.com85e143c2013-12-30 15:51:25 +0000992 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000993
reed@google.comacd471f2011-05-03 21:26:46 +0000994 if (this->needOpBytes(size)) {
reed@google.com31891582011-05-12 03:03:56 +0000995 this->writeOp(kDrawVertices_DrawOp, flags, 0);
reed@google.com85e143c2013-12-30 15:51:25 +0000996 fWriter.write32(vmode);
reed@google.comacd471f2011-05-03 21:26:46 +0000997 fWriter.write32(vertexCount);
998 fWriter.write(vertices, vertexCount * sizeof(SkPoint));
commit-bot@chromium.orgd0490172014-05-14 15:12:08 +0000999 if (flags & kDrawVertices_HasTexs_DrawOpFlag) {
reed@google.comacd471f2011-05-03 21:26:46 +00001000 fWriter.write(texs, vertexCount * sizeof(SkPoint));
1001 }
commit-bot@chromium.orgd0490172014-05-14 15:12:08 +00001002 if (flags & kDrawVertices_HasColors_DrawOpFlag) {
reed@google.comacd471f2011-05-03 21:26:46 +00001003 fWriter.write(colors, vertexCount * sizeof(SkColor));
1004 }
reed@google.com85e143c2013-12-30 15:51:25 +00001005 if (flags & kDrawVertices_HasXfermode_DrawOpFlag) {
1006 SkXfermode::Mode mode = SkXfermode::kModulate_Mode;
commit-bot@chromium.orgd0490172014-05-14 15:12:08 +00001007 SkAssertResult(xfer->asMode(&mode));
reed@google.com85e143c2013-12-30 15:51:25 +00001008 fWriter.write32(mode);
1009 }
commit-bot@chromium.orgd0490172014-05-14 15:12:08 +00001010 if (flags & kDrawVertices_HasIndices_DrawOpFlag) {
reed@google.comacd471f2011-05-03 21:26:46 +00001011 fWriter.write32(indexCount);
1012 fWriter.writePad(indices, indexCount * sizeof(uint16_t));
1013 }
reed@google.combb6992a2011-04-26 17:41:56 +00001014 }
1015}
1016
dandovb3c9d1c2014-08-12 08:34:29 -07001017void SkGPipeCanvas::onDrawPatch(const SkPoint cubics[12], const SkColor colors[4],
1018 const SkPoint texCoords[4], SkXfermode* xmode,
1019 const SkPaint& paint) {
dandov963137b2014-08-07 07:49:53 -07001020 NOTIFY_SETUP(this);
dandovb3c9d1c2014-08-12 08:34:29 -07001021
1022 size_t size = SkPatchUtils::kNumCtrlPts * sizeof(SkPoint);
1023 unsigned flags = 0;
1024 if (NULL != colors) {
1025 flags |= kDrawVertices_HasColors_DrawOpFlag;
1026 size += SkPatchUtils::kNumCorners * sizeof(SkColor);
1027 }
1028 if (NULL != texCoords) {
1029 flags |= kDrawVertices_HasTexs_DrawOpFlag;
1030 size += SkPatchUtils::kNumCorners * sizeof(SkPoint);
1031 }
1032 if (NULL != xmode) {
1033 SkXfermode::Mode mode;
1034 if (xmode->asMode(&mode) && SkXfermode::kModulate_Mode != mode) {
1035 flags |= kDrawVertices_HasXfermode_DrawOpFlag;
1036 size += sizeof(int32_t);
1037 }
1038 }
1039
dandov963137b2014-08-07 07:49:53 -07001040 this->writePaint(paint);
dandovb3c9d1c2014-08-12 08:34:29 -07001041 if (this->needOpBytes(size)) {
1042 this->writeOp(kDrawPatch_DrawOp, flags, 0);
1043
1044 fWriter.write(cubics, SkPatchUtils::kNumCtrlPts * sizeof(SkPoint));
1045
1046 if (NULL != colors) {
1047 fWriter.write(colors, SkPatchUtils::kNumCorners * sizeof(SkColor));
1048 }
1049
1050 if (NULL != texCoords) {
1051 fWriter.write(texCoords, SkPatchUtils::kNumCorners * sizeof(SkPoint));
1052 }
1053
1054 if (flags & kDrawVertices_HasXfermode_DrawOpFlag) {
1055 SkXfermode::Mode mode = SkXfermode::kModulate_Mode;
1056 SkAssertResult(xmode->asMode(&mode));
1057 fWriter.write32(mode);
1058 }
dandov963137b2014-08-07 07:49:53 -07001059 }
1060}
1061
reed@google.comacd471f2011-05-03 21:26:46 +00001062void SkGPipeCanvas::drawData(const void* ptr, size_t size) {
1063 if (size && ptr) {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +00001064 NOTIFY_SETUP(this);
reed@google.combb6992a2011-04-26 17:41:56 +00001065 unsigned data = 0;
1066 if (size < (1 << DRAWOPS_DATA_BITS)) {
1067 data = (unsigned)size;
1068 }
reed@google.comacd471f2011-05-03 21:26:46 +00001069 if (this->needOpBytes(4 + SkAlign4(size))) {
1070 this->writeOp(kDrawData_DrawOp, 0, data);
1071 if (0 == data) {
commit-bot@chromium.orgb45bd1f2014-04-24 18:17:30 +00001072 fWriter.write32(SkToU32(size));
reed@google.comacd471f2011-05-03 21:26:46 +00001073 }
reed@google.combb6793b2011-05-05 15:18:15 +00001074 fWriter.writePad(ptr, size);
reed@google.combb6992a2011-04-26 17:41:56 +00001075 }
1076 }
1077}
1078
robertphillips@google.com0a4805e2013-05-29 13:24:23 +00001079void SkGPipeCanvas::beginCommentGroup(const char* description) {
1080 // ignore for now
1081}
1082
1083void SkGPipeCanvas::addComment(const char* kywd, const char* value) {
1084 // ignore for now
1085}
1086
1087void SkGPipeCanvas::endCommentGroup() {
1088 // ignore for now
1089}
1090
junov@chromium.org77eec242012-07-18 17:54:45 +00001091void SkGPipeCanvas::flushRecording(bool detachCurrentBlock) {
commit-bot@chromium.orgd0490172014-05-14 15:12:08 +00001092 this->doNotify();
junov@chromium.org77eec242012-07-18 17:54:45 +00001093 if (detachCurrentBlock) {
1094 // force a new block to be requested for the next recorded command
rmistry@google.comd6176b02012-08-23 18:14:13 +00001095 fBlockSize = 0;
junov@chromium.org77eec242012-07-18 17:54:45 +00001096 }
1097}
1098
junov@chromium.org2e14ba82012-08-07 14:26:57 +00001099size_t SkGPipeCanvas::freeMemoryIfPossible(size_t bytesToFree) {
scroggo@google.comd5d158b2012-08-14 20:38:28 +00001100 return (NULL == fBitmapHeap) ? 0 : fBitmapHeap->freeMemoryIfPossible(bytesToFree);
junov@chromium.org2e14ba82012-08-07 14:26:57 +00001101}
1102
reed@google.combb6992a2011-04-26 17:41:56 +00001103///////////////////////////////////////////////////////////////////////////////
1104
1105template <typename T> uint32_t castToU32(T value) {
1106 union {
1107 T fSrc;
1108 uint32_t fDst;
1109 } data;
1110 data.fSrc = value;
1111 return data.fDst;
1112}
1113
reed@google.com31891582011-05-12 03:03:56 +00001114void SkGPipeCanvas::writePaint(const SkPaint& paint) {
scroggo@google.comd5d158b2012-08-14 20:38:28 +00001115 if (fDone) {
1116 return;
1117 }
reed@google.com31891582011-05-12 03:03:56 +00001118 SkPaint& base = fPaint;
reed@google.combb6992a2011-04-26 17:41:56 +00001119 uint32_t storage[32];
1120 uint32_t* ptr = storage;
reed@google.combb6992a2011-04-26 17:41:56 +00001121
1122 if (base.getFlags() != paint.getFlags()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001123 *ptr++ = PaintOp_packOpData(kFlags_PaintOp, paint.getFlags());
reed@google.comf5842f72011-05-04 18:30:04 +00001124 base.setFlags(paint.getFlags());
reed@google.combb6992a2011-04-26 17:41:56 +00001125 }
1126 if (base.getColor() != paint.getColor()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001127 *ptr++ = PaintOp_packOp(kColor_PaintOp);
1128 *ptr++ = paint.getColor();
reed@google.comf5842f72011-05-04 18:30:04 +00001129 base.setColor(paint.getColor());
reed@google.combb6992a2011-04-26 17:41:56 +00001130 }
commit-bot@chromium.org85faf502014-04-16 12:58:02 +00001131 if (base.getFilterLevel() != paint.getFilterLevel()) {
1132 *ptr++ = PaintOp_packOpData(kFilterLevel_PaintOp, paint.getFilterLevel());
1133 base.setFilterLevel(paint.getFilterLevel());
1134 }
reed@google.combb6992a2011-04-26 17:41:56 +00001135 if (base.getStyle() != paint.getStyle()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001136 *ptr++ = PaintOp_packOpData(kStyle_PaintOp, paint.getStyle());
reed@google.comf5842f72011-05-04 18:30:04 +00001137 base.setStyle(paint.getStyle());
reed@google.combb6992a2011-04-26 17:41:56 +00001138 }
1139 if (base.getStrokeJoin() != paint.getStrokeJoin()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001140 *ptr++ = PaintOp_packOpData(kJoin_PaintOp, paint.getStrokeJoin());
reed@google.comf5842f72011-05-04 18:30:04 +00001141 base.setStrokeJoin(paint.getStrokeJoin());
reed@google.combb6992a2011-04-26 17:41:56 +00001142 }
1143 if (base.getStrokeCap() != paint.getStrokeCap()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001144 *ptr++ = PaintOp_packOpData(kCap_PaintOp, paint.getStrokeCap());
reed@google.comf5842f72011-05-04 18:30:04 +00001145 base.setStrokeCap(paint.getStrokeCap());
reed@google.combb6992a2011-04-26 17:41:56 +00001146 }
1147 if (base.getStrokeWidth() != paint.getStrokeWidth()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001148 *ptr++ = PaintOp_packOp(kWidth_PaintOp);
1149 *ptr++ = castToU32(paint.getStrokeWidth());
reed@google.comf5842f72011-05-04 18:30:04 +00001150 base.setStrokeWidth(paint.getStrokeWidth());
reed@google.combb6992a2011-04-26 17:41:56 +00001151 }
1152 if (base.getStrokeMiter() != paint.getStrokeMiter()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001153 *ptr++ = PaintOp_packOp(kMiter_PaintOp);
1154 *ptr++ = castToU32(paint.getStrokeMiter());
reed@google.comf5842f72011-05-04 18:30:04 +00001155 base.setStrokeMiter(paint.getStrokeMiter());
reed@google.combb6992a2011-04-26 17:41:56 +00001156 }
1157 if (base.getTextEncoding() != paint.getTextEncoding()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001158 *ptr++ = PaintOp_packOpData(kEncoding_PaintOp, paint.getTextEncoding());
reed@google.comf5842f72011-05-04 18:30:04 +00001159 base.setTextEncoding(paint.getTextEncoding());
reed@google.combb6992a2011-04-26 17:41:56 +00001160 }
1161 if (base.getHinting() != paint.getHinting()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001162 *ptr++ = PaintOp_packOpData(kHinting_PaintOp, paint.getHinting());
reed@google.comf5842f72011-05-04 18:30:04 +00001163 base.setHinting(paint.getHinting());
reed@google.combb6992a2011-04-26 17:41:56 +00001164 }
1165 if (base.getTextAlign() != paint.getTextAlign()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001166 *ptr++ = PaintOp_packOpData(kAlign_PaintOp, paint.getTextAlign());
reed@google.comf5842f72011-05-04 18:30:04 +00001167 base.setTextAlign(paint.getTextAlign());
reed@google.combb6992a2011-04-26 17:41:56 +00001168 }
1169 if (base.getTextSize() != paint.getTextSize()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001170 *ptr++ = PaintOp_packOp(kTextSize_PaintOp);
1171 *ptr++ = castToU32(paint.getTextSize());
reed@google.comf5842f72011-05-04 18:30:04 +00001172 base.setTextSize(paint.getTextSize());
reed@google.combb6992a2011-04-26 17:41:56 +00001173 }
1174 if (base.getTextScaleX() != paint.getTextScaleX()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001175 *ptr++ = PaintOp_packOp(kTextScaleX_PaintOp);
1176 *ptr++ = castToU32(paint.getTextScaleX());
reed@google.comf5842f72011-05-04 18:30:04 +00001177 base.setTextScaleX(paint.getTextScaleX());
reed@google.combb6992a2011-04-26 17:41:56 +00001178 }
1179 if (base.getTextSkewX() != paint.getTextSkewX()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001180 *ptr++ = PaintOp_packOp(kTextSkewX_PaintOp);
1181 *ptr++ = castToU32(paint.getTextSkewX());
reed@google.comf5842f72011-05-04 18:30:04 +00001182 base.setTextSkewX(paint.getTextSkewX());
1183 }
1184
1185 if (!SkTypeface::Equal(base.getTypeface(), paint.getTypeface())) {
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +00001186 if (isCrossProcess(fFlags)) {
scroggo@google.com3cb969f2012-07-27 20:39:19 +00001187 uint32_t id = this->getTypefaceID(paint.getTypeface());
1188 *ptr++ = PaintOp_packOpData(kTypeface_PaintOp, id);
1189 } else if (this->needOpBytes(sizeof(void*))) {
1190 // Add to the set for ref counting.
1191 fTypefaceSet.add(paint.getTypeface());
1192 // It is safe to write the typeface to the stream before the rest
1193 // of the paint unless we ever send a kReset_PaintOp, which we
1194 // currently never do.
1195 this->writeOp(kSetTypeface_DrawOp);
1196 fWriter.writePtr(paint.getTypeface());
1197 }
reed@google.comf5842f72011-05-04 18:30:04 +00001198 base.setTypeface(paint.getTypeface());
reed@google.combb6992a2011-04-26 17:41:56 +00001199 }
reed@google.combb6992a2011-04-26 17:41:56 +00001200
scroggo@google.com4dffc592012-07-17 16:49:40 +00001201 // This is a new paint, so all old flats can be safely purged, if necessary.
1202 fFlattenableHeap.markAllFlatsSafeToDelete();
reed@google.comb55d1182011-05-11 00:42:04 +00001203 for (int i = 0; i < kCount_PaintFlats; i++) {
1204 int index = this->flattenToIndex(get_paintflat(paint, i), (PaintFlats)i);
scroggo@google.comd3ba5cc2012-07-09 16:05:53 +00001205 bool replaced = index < 0;
1206 if (replaced) {
1207 index = ~index;
1208 }
scroggo@google.com4dffc592012-07-17 16:49:40 +00001209 // Store the index of any flat that needs to be kept. 0 means no flat.
1210 if (index > 0) {
1211 fFlattenableHeap.markFlatForKeeping(index);
1212 }
1213 SkASSERT(index >= 0 && index <= fFlatDictionary.count());
scroggo@google.comd3ba5cc2012-07-09 16:05:53 +00001214 if (index != fCurrFlatIndex[i] || replaced) {
reed@google.comb55d1182011-05-11 00:42:04 +00001215 *ptr++ = PaintOp_packOpFlagData(kFlatIndex_PaintOp, i, index);
1216 fCurrFlatIndex[i] = index;
1217 }
1218 }
1219
reed@google.comacd471f2011-05-03 21:26:46 +00001220 size_t size = (char*)ptr - (char*)storage;
1221 if (size && this->needOpBytes(size)) {
commit-bot@chromium.orgb45bd1f2014-04-24 18:17:30 +00001222 this->writeOp(kPaintOp_DrawOp, 0, SkToU32(size));
reed@google.comb55d1182011-05-11 00:42:04 +00001223 fWriter.write(storage, size);
reed@google.combb6992a2011-04-26 17:41:56 +00001224 for (size_t i = 0; i < size/4; i++) {
reed@google.comb55d1182011-05-11 00:42:04 +00001225// SkDebugf("[%d] %08X\n", i, storage[i]);
reed@google.combb6992a2011-04-26 17:41:56 +00001226 }
1227 }
reed@google.com0cd2ac62013-10-14 20:02:44 +00001228
1229 //
1230 // Do these after we've written kPaintOp_DrawOp
skia.committer@gmail.comfbc58a32013-10-15 07:02:27 +00001231
reed@google.com0cd2ac62013-10-14 20:02:44 +00001232 if (base.getAnnotation() != paint.getAnnotation()) {
1233 if (NULL == paint.getAnnotation()) {
commit-bot@chromium.org89ff3dd2013-10-29 20:29:38 +00001234 if (this->needOpBytes()) {
1235 this->writeOp(kSetAnnotation_DrawOp, 0, 0);
1236 }
reed@google.com0cd2ac62013-10-14 20:02:44 +00001237 } else {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +00001238 SkWriteBuffer buffer;
reed@google.com0cd2ac62013-10-14 20:02:44 +00001239 paint.getAnnotation()->writeToBuffer(buffer);
commit-bot@chromium.org89ff3dd2013-10-29 20:29:38 +00001240 const size_t size = buffer.bytesWritten();
1241 if (this->needOpBytes(size)) {
commit-bot@chromium.orgb45bd1f2014-04-24 18:17:30 +00001242 this->writeOp(kSetAnnotation_DrawOp, 0, SkToU32(size));
commit-bot@chromium.org89ff3dd2013-10-29 20:29:38 +00001243 buffer.writeToMemory(fWriter.reserve(size));
1244 }
reed@google.com0cd2ac62013-10-14 20:02:44 +00001245 }
commit-bot@chromium.org40258a52013-10-29 19:23:26 +00001246 base.setAnnotation(paint.getAnnotation());
reed@google.com0cd2ac62013-10-14 20:02:44 +00001247 }
reed@google.combb6992a2011-04-26 17:41:56 +00001248}
1249
1250///////////////////////////////////////////////////////////////////////////////
1251
1252#include "SkGPipe.h"
1253
scroggo@google.com3cb969f2012-07-27 20:39:19 +00001254SkGPipeController::~SkGPipeController() {
1255 SkSafeUnref(fCanvas);
1256}
1257
1258void SkGPipeController::setCanvas(SkGPipeCanvas* canvas) {
1259 SkRefCnt_SafeAssign(fCanvas, canvas);
1260}
1261
1262///////////////////////////////////////////////////////////////////////////////
1263
1264SkGPipeWriter::SkGPipeWriter()
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +00001265: fWriter(0) {
reed@google.combb6992a2011-04-26 17:41:56 +00001266 fCanvas = NULL;
1267}
1268
1269SkGPipeWriter::~SkGPipeWriter() {
reed@google.comacd471f2011-05-03 21:26:46 +00001270 this->endRecording();
reed@google.combb6992a2011-04-26 17:41:56 +00001271}
1272
junov@chromium.orga8db8fe2012-08-15 19:49:22 +00001273SkCanvas* SkGPipeWriter::startRecording(SkGPipeController* controller, uint32_t flags,
1274 uint32_t width, uint32_t height) {
reed@google.combb6992a2011-04-26 17:41:56 +00001275 if (NULL == fCanvas) {
reed@google.comacd471f2011-05-03 21:26:46 +00001276 fWriter.reset(NULL, 0);
junov@chromium.orga8db8fe2012-08-15 19:49:22 +00001277 fCanvas = SkNEW_ARGS(SkGPipeCanvas, (controller, &fWriter, flags, width, height));
reed@google.combb6992a2011-04-26 17:41:56 +00001278 }
scroggo@google.com3cb969f2012-07-27 20:39:19 +00001279 controller->setCanvas(fCanvas);
reed@google.combb6992a2011-04-26 17:41:56 +00001280 return fCanvas;
1281}
1282
1283void SkGPipeWriter::endRecording() {
1284 if (fCanvas) {
scroggo@google.com59c3ab62013-11-12 14:32:38 +00001285 fCanvas->finish(true);
reed@google.combb6992a2011-04-26 17:41:56 +00001286 fCanvas->unref();
1287 fCanvas = NULL;
1288 }
1289}
1290
junov@chromium.org2e14ba82012-08-07 14:26:57 +00001291void SkGPipeWriter::flushRecording(bool detachCurrentBlock) {
1292 if (fCanvas) {
1293 fCanvas->flushRecording(detachCurrentBlock);
1294 }
junov@chromium.org77eec242012-07-18 17:54:45 +00001295}
1296
junov@chromium.org2e14ba82012-08-07 14:26:57 +00001297size_t SkGPipeWriter::freeMemoryIfPossible(size_t bytesToFree) {
1298 if (fCanvas) {
1299 return fCanvas->freeMemoryIfPossible(bytesToFree);
1300 }
1301 return 0;
1302}
1303
1304size_t SkGPipeWriter::storageAllocatedForRecording() const {
scroggo@google.com15011ee2012-07-26 20:03:32 +00001305 return NULL == fCanvas ? 0 : fCanvas->storageAllocatedForRecording();
1306}
1307
scroggo@google.com3e26bd02012-08-14 15:20:01 +00001308///////////////////////////////////////////////////////////////////////////////
1309
1310BitmapShuttle::BitmapShuttle(SkGPipeCanvas* canvas) {
1311 SkASSERT(canvas != NULL);
1312 fCanvas = canvas;
1313 fCanvas->ref();
1314}
1315
1316BitmapShuttle::~BitmapShuttle() {
scroggo@google.com59c3ab62013-11-12 14:32:38 +00001317 this->removeCanvas();
scroggo@google.com3e26bd02012-08-14 15:20:01 +00001318}
1319
1320bool BitmapShuttle::insert(const SkBitmap& bitmap, int32_t slot) {
scroggo@google.com59c3ab62013-11-12 14:32:38 +00001321 SkASSERT(fCanvas != NULL);
scroggo@google.com3e26bd02012-08-14 15:20:01 +00001322 return fCanvas->shuttleBitmap(bitmap, slot);
1323}
scroggo@google.com59c3ab62013-11-12 14:32:38 +00001324
1325void BitmapShuttle::removeCanvas() {
1326 if (NULL == fCanvas) {
1327 return;
1328 }
1329 fCanvas->unref();
1330 fCanvas = NULL;
1331}