blob: 94a30a34e254d6dc6e3083ece796b14f102381d9 [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;
dandovb3c9d1c2014-08-12 08:34:29 -0700286 virtual void onDrawPatch(const SkPoint cubics[12], const SkColor colors[4],
287 const SkPoint texCoords[4], SkXfermode* xmode,
288 const SkPaint& paint) SK_OVERRIDE;
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000289 virtual void onClipRect(const SkRect&, SkRegion::Op, ClipEdgeStyle) SK_OVERRIDE;
290 virtual void onClipRRect(const SkRRect&, SkRegion::Op, ClipEdgeStyle) SK_OVERRIDE;
291 virtual void onClipPath(const SkPath&, SkRegion::Op, ClipEdgeStyle) SK_OVERRIDE;
292 virtual void onClipRegion(const SkRegion&, SkRegion::Op) SK_OVERRIDE;
293
reedd5fa1a42014-08-09 11:08:05 -0700294 virtual void onDrawPicture(const SkPicture*, const SkMatrix*, const SkPaint*) SK_OVERRIDE;
robertphillips9b14f262014-06-04 05:40:44 -0700295
reed@google.combb6992a2011-04-26 17:41:56 +0000296private:
commit-bot@chromium.orgd9ea09e2014-03-25 17:32:26 +0000297 void recordTranslate(const SkMatrix&);
298 void recordScale(const SkMatrix&);
299 void recordConcat(const SkMatrix&);
300
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000301 enum {
302 kNoSaveLayer = -1,
303 };
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000304 SkNamedFactorySet* fFactorySet;
305 int fFirstSaveLayerStackLevel;
scroggo@google.comd9d29672012-08-14 17:21:34 +0000306 SkBitmapHeap* fBitmapHeap;
reed@google.comacd471f2011-05-03 21:26:46 +0000307 SkGPipeController* fController;
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000308 SkWriter32& fWriter;
309 size_t fBlockSize; // amount allocated for writer
310 size_t fBytesNotified;
311 bool fDone;
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000312 const uint32_t fFlags;
reed@google.combb6992a2011-04-26 17:41:56 +0000313
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000314 SkRefCntSet fTypefaceSet;
reed@google.comf5842f72011-05-04 18:30:04 +0000315
316 uint32_t getTypefaceID(SkTypeface*);
317
reed@google.comacd471f2011-05-03 21:26:46 +0000318 inline void writeOp(DrawOps op, unsigned flags, unsigned data) {
reed@google.combb6992a2011-04-26 17:41:56 +0000319 fWriter.write32(DrawOp_packOpFlagData(op, flags, data));
320 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000321
reed@google.comacd471f2011-05-03 21:26:46 +0000322 inline void writeOp(DrawOps op) {
reed@google.combb6992a2011-04-26 17:41:56 +0000323 fWriter.write32(DrawOp_packOpFlagData(op, 0, 0));
324 }
reed@google.comacd471f2011-05-03 21:26:46 +0000325
326 bool needOpBytes(size_t size = 0);
327
328 inline void doNotify() {
329 if (!fDone) {
reed@google.com44699382013-10-31 17:28:30 +0000330 size_t bytes = fWriter.bytesWritten() - fBytesNotified;
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000331 if (bytes > 0) {
332 fController->notifyWritten(bytes);
333 fBytesNotified += bytes;
334 }
reed@google.comacd471f2011-05-03 21:26:46 +0000335 }
336 }
reed@google.comb55d1182011-05-11 00:42:04 +0000337
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000338 // Should be called after any calls to an SkFlatDictionary::findAndReplace
339 // if a new SkFlatData was added when in cross process mode
340 void flattenFactoryNames();
341
scroggo@google.com59c3ab62013-11-12 14:32:38 +0000342 FlattenableHeap fFlattenableHeap;
343 FlatDictionary fFlatDictionary;
344 SkAutoTUnref<BitmapShuttle> fBitmapShuttle;
345 int fCurrFlatIndex[kCount_PaintFlats];
346
reed@google.comb55d1182011-05-11 00:42:04 +0000347 int flattenToIndex(SkFlattenable* obj, PaintFlats);
348
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000349 // Common code used by drawBitmap*. Behaves differently depending on the
350 // type of SkBitmapHeap being used, which is determined by the flags used.
351 bool commonDrawBitmap(const SkBitmap& bm, DrawOps op, unsigned flags,
352 size_t opBytesNeeded, const SkPaint* paint);
scroggo@google.com58be6822012-07-30 14:40:01 +0000353
reed@google.com31891582011-05-12 03:03:56 +0000354 SkPaint fPaint;
355 void writePaint(const SkPaint&);
reed@google.combb6992a2011-04-26 17:41:56 +0000356
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000357 class AutoPipeNotify {
358 public:
359 AutoPipeNotify(SkGPipeCanvas* canvas) : fCanvas(canvas) {}
360 ~AutoPipeNotify() { fCanvas->doNotify(); }
361 private:
362 SkGPipeCanvas* fCanvas;
363 };
364 friend class AutoPipeNotify;
365
reed@google.combb6992a2011-04-26 17:41:56 +0000366 typedef SkCanvas INHERITED;
367};
368
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000369void SkGPipeCanvas::flattenFactoryNames() {
370 const char* name;
371 while ((name = fFactorySet->getNextAddedFactoryName()) != NULL) {
372 size_t len = strlen(name);
commit-bot@chromium.orgd0490172014-05-14 15:12:08 +0000373 if (this->needOpBytes(SkWriter32::WriteStringSize(name, len))) {
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000374 this->writeOp(kDef_Factory_DrawOp);
375 fWriter.writeString(name, len);
376 }
377 }
378}
379
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000380bool SkGPipeCanvas::shuttleBitmap(const SkBitmap& bm, int32_t slot) {
scroggo@google.com565254b2012-06-28 15:41:32 +0000381 SkASSERT(shouldFlattenBitmaps(fFlags));
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000382 SkWriteBuffer buffer;
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000383 buffer.setNamedFactoryRecorder(fFactorySet);
scroggo@google.com74b7ffd2013-04-30 02:32:41 +0000384 buffer.writeBitmap(bm);
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000385 this->flattenFactoryNames();
commit-bot@chromium.orgb45bd1f2014-04-24 18:17:30 +0000386 size_t size = buffer.bytesWritten();
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000387 if (this->needOpBytes(size)) {
388 this->writeOp(kDef_Bitmap_DrawOp, 0, slot);
389 void* dst = static_cast<void*>(fWriter.reserve(size));
390 buffer.writeToMemory(dst);
391 return true;
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000392 }
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000393 return false;
scroggo@google.comd3ba5cc2012-07-09 16:05:53 +0000394}
395
reed@google.comb55d1182011-05-11 00:42:04 +0000396// return 0 for NULL (or unflattenable obj), or index-base-1
scroggo@google.comd3ba5cc2012-07-09 16:05:53 +0000397// return ~(index-base-1) if an old flattenable was replaced
reed@google.comb55d1182011-05-11 00:42:04 +0000398int SkGPipeCanvas::flattenToIndex(SkFlattenable* obj, PaintFlats paintflat) {
scroggo@google.comd5d158b2012-08-14 20:38:28 +0000399 SkASSERT(!fDone && fBitmapHeap != NULL);
reed@google.comb55d1182011-05-11 00:42:04 +0000400 if (NULL == obj) {
401 return 0;
402 }
reed@google.comb55d1182011-05-11 00:42:04 +0000403
scroggo@google.comd9d29672012-08-14 17:21:34 +0000404 fBitmapHeap->deferAddingOwners();
scroggo@google.com4dffc592012-07-17 16:49:40 +0000405 bool added, replaced;
scroggo@google.com664fab12012-08-14 19:22:05 +0000406 const SkFlatData* flat = fFlatDictionary.findAndReplace(*obj, fFlattenableHeap.flatToReplace(),
407 &added, &replaced);
scroggo@google.comd9d29672012-08-14 17:21:34 +0000408 fBitmapHeap->endAddingOwnersDeferral(added);
scroggo@google.com4dffc592012-07-17 16:49:40 +0000409 int index = flat->index();
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000410 if (added) {
411 if (isCrossProcess(fFlags)) {
412 this->flattenFactoryNames();
413 }
414 size_t flatSize = flat->flatSize();
415 if (this->needOpBytes(flatSize)) {
416 this->writeOp(kDef_Flattenable_DrawOp, paintflat, index);
417 fWriter.write(flat->data(), flatSize);
418 }
scroggo@google.com4dffc592012-07-17 16:49:40 +0000419 }
420 if (replaced) {
reed@google.comb55d1182011-05-11 00:42:04 +0000421 index = ~index;
reed@google.comb55d1182011-05-11 00:42:04 +0000422 }
scroggo@google.com4dffc592012-07-17 16:49:40 +0000423 return index;
reed@google.comb55d1182011-05-11 00:42:04 +0000424}
425
reed@google.combb6992a2011-04-26 17:41:56 +0000426///////////////////////////////////////////////////////////////////////////////
427
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000428#define MIN_BLOCK_SIZE (16 * 1024)
scroggo@google.com4dffc592012-07-17 16:49:40 +0000429#define BITMAPS_TO_KEEP 5
430#define FLATTENABLES_TO_KEEP 10
reed@google.combb6992a2011-04-26 17:41:56 +0000431
reed@google.comacd471f2011-05-03 21:26:46 +0000432SkGPipeCanvas::SkGPipeCanvas(SkGPipeController* controller,
rmistry@google.comd6176b02012-08-23 18:14:13 +0000433 SkWriter32* writer, uint32_t flags,
junov@chromium.orga8db8fe2012-08-15 19:49:22 +0000434 uint32_t width, uint32_t height)
commit-bot@chromium.org403f8d72014-02-17 15:24:26 +0000435 : SkCanvas(width, height)
436 , fFactorySet(isCrossProcess(flags) ? SkNEW(SkNamedFactorySet) : NULL)
437 , fWriter(*writer)
438 , fFlags(flags)
439 , fFlattenableHeap(FLATTENABLES_TO_KEEP, fFactorySet, isCrossProcess(flags))
440 , fFlatDictionary(&fFlattenableHeap)
441{
reed@google.comacd471f2011-05-03 21:26:46 +0000442 fController = controller;
reed@google.combb6992a2011-04-26 17:41:56 +0000443 fDone = false;
reed@google.comacd471f2011-05-03 21:26:46 +0000444 fBlockSize = 0; // need first block from controller
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000445 fBytesNotified = 0;
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000446 fFirstSaveLayerStackLevel = kNoSaveLayer;
reed@google.comb55d1182011-05-11 00:42:04 +0000447 sk_bzero(fCurrFlatIndex, sizeof(fCurrFlatIndex));
reed@google.comacd471f2011-05-03 21:26:46 +0000448
scroggo@google.com565254b2012-06-28 15:41:32 +0000449 // Tell the reader the appropriate flags to use.
450 if (this->needOpBytes()) {
451 this->writeOp(kReportFlags_DrawOp, fFlags, 0);
452 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000453
scroggo@google.com10dccde2012-08-08 20:43:22 +0000454 if (shouldFlattenBitmaps(flags)) {
scroggo@google.com59c3ab62013-11-12 14:32:38 +0000455 fBitmapShuttle.reset(SkNEW_ARGS(BitmapShuttle, (this)));
456 fBitmapHeap = SkNEW_ARGS(SkBitmapHeap, (fBitmapShuttle.get(), BITMAPS_TO_KEEP));
scroggo@google.com10dccde2012-08-08 20:43:22 +0000457 } else {
scroggo@google.comd9d29672012-08-14 17:21:34 +0000458 fBitmapHeap = SkNEW_ARGS(SkBitmapHeap,
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000459 (BITMAPS_TO_KEEP, controller->numberOfReaders()));
scroggo@google.com10dccde2012-08-08 20:43:22 +0000460 if (this->needOpBytes(sizeof(void*))) {
scroggo@google.comd9d29672012-08-14 17:21:34 +0000461 this->writeOp(kShareBitmapHeap_DrawOp);
462 fWriter.writePtr(static_cast<void*>(fBitmapHeap));
scroggo@google.com10dccde2012-08-08 20:43:22 +0000463 }
464 }
scroggo@google.comd9d29672012-08-14 17:21:34 +0000465 fFlattenableHeap.setBitmapStorage(fBitmapHeap);
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000466 this->doNotify();
reed@google.combb6992a2011-04-26 17:41:56 +0000467}
468
469SkGPipeCanvas::~SkGPipeCanvas() {
scroggo@google.com59c3ab62013-11-12 14:32:38 +0000470 this->finish(true);
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000471 SkSafeUnref(fFactorySet);
scroggo@google.comd9d29672012-08-14 17:21:34 +0000472 SkSafeUnref(fBitmapHeap);
reed@google.combb6992a2011-04-26 17:41:56 +0000473}
474
reed@google.comacd471f2011-05-03 21:26:46 +0000475bool SkGPipeCanvas::needOpBytes(size_t needed) {
476 if (fDone) {
477 return false;
478 }
479
480 needed += 4; // size of DrawOp atom
commit-bot@chromium.orgd0490172014-05-14 15:12:08 +0000481 needed = SkAlign4(needed);
reed@google.com44699382013-10-31 17:28:30 +0000482 if (fWriter.bytesWritten() + needed > fBlockSize) {
mtklein6d88e6c2014-07-30 09:17:54 -0700483 // Before we wipe out any data that has already been written, read it out.
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000484 this->doNotify();
mtklein6d88e6c2014-07-30 09:17:54 -0700485
486 // If we're going to allocate a new block, allocate enough to make it worthwhile.
487 needed = SkTMax<size_t>(MIN_BLOCK_SIZE, needed);
488
commit-bot@chromium.orgd0490172014-05-14 15:12:08 +0000489 void* block = fController->requestBlock(needed, &fBlockSize);
reed@google.comacd471f2011-05-03 21:26:46 +0000490 if (NULL == block) {
scroggo@google.com59c3ab62013-11-12 14:32:38 +0000491 // Do not notify the readers, which would call this function again.
492 this->finish(false);
reed@google.comacd471f2011-05-03 21:26:46 +0000493 return false;
494 }
scroggo@google.com460a23e2012-08-16 17:56:49 +0000495 SkASSERT(SkIsAlign4(fBlockSize));
reed@google.comacd471f2011-05-03 21:26:46 +0000496 fWriter.reset(block, fBlockSize);
497 fBytesNotified = 0;
498 }
499 return true;
500}
501
reed@google.comf5842f72011-05-04 18:30:04 +0000502uint32_t SkGPipeCanvas::getTypefaceID(SkTypeface* face) {
503 uint32_t id = 0; // 0 means default/null typeface
504 if (face) {
505 id = fTypefaceSet.find(face);
506 if (0 == id) {
507 id = fTypefaceSet.add(face);
508 size_t size = writeTypeface(NULL, face);
509 if (this->needOpBytes(size)) {
reed@google.combb6793b2011-05-05 15:18:15 +0000510 this->writeOp(kDef_Typeface_DrawOp);
reed@google.comf5842f72011-05-04 18:30:04 +0000511 writeTypeface(&fWriter, face);
512 }
513 }
514 }
515 return id;
516}
517
reed@google.combb6992a2011-04-26 17:41:56 +0000518///////////////////////////////////////////////////////////////////////////////
519
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000520#define NOTIFY_SETUP(canvas) \
521 AutoPipeNotify apn(canvas)
522
Florin Malita5f6102d2014-06-30 10:13:28 -0400523void SkGPipeCanvas::willSave() {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000524 NOTIFY_SETUP(this);
reed@google.comacd471f2011-05-03 21:26:46 +0000525 if (this->needOpBytes()) {
Florin Malita5f6102d2014-06-30 10:13:28 -0400526 this->writeOp(kSave_DrawOp);
reed@google.comacd471f2011-05-03 21:26:46 +0000527 }
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000528
Florin Malita5f6102d2014-06-30 10:13:28 -0400529 this->INHERITED::willSave();
reed@google.combb6992a2011-04-26 17:41:56 +0000530}
531
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000532SkCanvas::SaveLayerStrategy SkGPipeCanvas::willSaveLayer(const SkRect* bounds, const SkPaint* paint,
533 SaveFlags saveFlags) {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000534 NOTIFY_SETUP(this);
reed@google.comacd471f2011-05-03 21:26:46 +0000535 size_t size = 0;
reed@google.combb6992a2011-04-26 17:41:56 +0000536 unsigned opFlags = 0;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000537
reed@google.combb6992a2011-04-26 17:41:56 +0000538 if (bounds) {
539 opFlags |= kSaveLayer_HasBounds_DrawOpFlag;
reed@google.comacd471f2011-05-03 21:26:46 +0000540 size += sizeof(SkRect);
reed@google.combb6992a2011-04-26 17:41:56 +0000541 }
542 if (paint) {
543 opFlags |= kSaveLayer_HasPaint_DrawOpFlag;
reed@google.com31891582011-05-12 03:03:56 +0000544 this->writePaint(*paint);
reed@google.combb6992a2011-04-26 17:41:56 +0000545 }
546
reed@google.comacd471f2011-05-03 21:26:46 +0000547 if (this->needOpBytes(size)) {
548 this->writeOp(kSaveLayer_DrawOp, opFlags, saveFlags);
549 if (bounds) {
550 fWriter.writeRect(*bounds);
551 }
reed@google.combb6992a2011-04-26 17:41:56 +0000552 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000553
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000554 if (kNoSaveLayer == fFirstSaveLayerStackLevel){
555 fFirstSaveLayerStackLevel = this->getSaveCount();
556 }
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000557
558 this->INHERITED::willSaveLayer(bounds, paint, saveFlags);
559 // we don't create a layer
560 return kNoLayer_SaveLayerStrategy;
reed@google.combb6992a2011-04-26 17:41:56 +0000561}
562
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000563void SkGPipeCanvas::willRestore() {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000564 NOTIFY_SETUP(this);
reed@google.comacd471f2011-05-03 21:26:46 +0000565 if (this->needOpBytes()) {
566 this->writeOp(kRestore_DrawOp);
567 }
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000568
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000569 if (this->getSaveCount() - 1 == fFirstSaveLayerStackLevel){
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000570 fFirstSaveLayerStackLevel = kNoSaveLayer;
571 }
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000572
573 this->INHERITED::willRestore();
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000574}
575
576bool SkGPipeCanvas::isDrawingToLayer() const {
577 return kNoSaveLayer != fFirstSaveLayerStackLevel;
reed@google.combb6992a2011-04-26 17:41:56 +0000578}
579
commit-bot@chromium.orgd9ea09e2014-03-25 17:32:26 +0000580void SkGPipeCanvas::recordTranslate(const SkMatrix& m) {
581 if (this->needOpBytes(2 * sizeof(SkScalar))) {
582 this->writeOp(kTranslate_DrawOp);
583 fWriter.writeScalar(m.getTranslateX());
584 fWriter.writeScalar(m.getTranslateY());
reed@google.combb6992a2011-04-26 17:41:56 +0000585 }
reed@google.combb6992a2011-04-26 17:41:56 +0000586}
587
commit-bot@chromium.orgd9ea09e2014-03-25 17:32:26 +0000588void SkGPipeCanvas::recordScale(const SkMatrix& m) {
589 if (this->needOpBytes(2 * sizeof(SkScalar))) {
590 this->writeOp(kScale_DrawOp);
591 fWriter.writeScalar(m.getScaleX());
592 fWriter.writeScalar(m.getScaleY());
reed@google.combb6992a2011-04-26 17:41:56 +0000593 }
reed@google.combb6992a2011-04-26 17:41:56 +0000594}
595
commit-bot@chromium.orgd9ea09e2014-03-25 17:32:26 +0000596void SkGPipeCanvas::recordConcat(const SkMatrix& m) {
597 if (this->needOpBytes(m.writeToMemory(NULL))) {
598 this->writeOp(kConcat_DrawOp);
599 fWriter.writeMatrix(m);
reed@google.combb6992a2011-04-26 17:41:56 +0000600 }
reed@google.combb6992a2011-04-26 17:41:56 +0000601}
602
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +0000603void SkGPipeCanvas::didConcat(const SkMatrix& matrix) {
reed@google.combb6992a2011-04-26 17:41:56 +0000604 if (!matrix.isIdentity()) {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000605 NOTIFY_SETUP(this);
commit-bot@chromium.orgd9ea09e2014-03-25 17:32:26 +0000606 switch (matrix.getType()) {
607 case SkMatrix::kTranslate_Mask:
608 this->recordTranslate(matrix);
609 break;
610 case SkMatrix::kScale_Mask:
611 this->recordScale(matrix);
612 break;
613 default:
614 this->recordConcat(matrix);
615 break;
reed@google.comacd471f2011-05-03 21:26:46 +0000616 }
reed@google.combb6992a2011-04-26 17:41:56 +0000617 }
commit-bot@chromium.orgd9ea09e2014-03-25 17:32:26 +0000618
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +0000619 this->INHERITED::didConcat(matrix);
reed@google.combb6992a2011-04-26 17:41:56 +0000620}
621
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +0000622void SkGPipeCanvas::didSetMatrix(const SkMatrix& matrix) {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000623 NOTIFY_SETUP(this);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000624 if (this->needOpBytes(matrix.writeToMemory(NULL))) {
reed@google.comacd471f2011-05-03 21:26:46 +0000625 this->writeOp(kSetMatrix_DrawOp);
djsollen@google.com2b2ede32012-04-12 13:24:04 +0000626 fWriter.writeMatrix(matrix);
reed@google.comacd471f2011-05-03 21:26:46 +0000627 }
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +0000628 this->INHERITED::didSetMatrix(matrix);
reed@google.combb6992a2011-04-26 17:41:56 +0000629}
630
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000631void SkGPipeCanvas::onClipRect(const SkRect& rect, SkRegion::Op rgnOp,
632 ClipEdgeStyle edgeStyle) {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000633 NOTIFY_SETUP(this);
scroggo@google.com460a23e2012-08-16 17:56:49 +0000634 if (this->needOpBytes(sizeof(SkRect))) {
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000635 unsigned flags = 0;
636 if (kSoft_ClipEdgeStyle == edgeStyle) {
637 flags = kClip_HasAntiAlias_DrawOpFlag;
638 }
scroggo@google.com460a23e2012-08-16 17:56:49 +0000639 this->writeOp(kClipRect_DrawOp, flags, rgnOp);
reed@google.comacd471f2011-05-03 21:26:46 +0000640 fWriter.writeRect(rect);
641 }
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000642 this->INHERITED::onClipRect(rect, rgnOp, edgeStyle);
reed@google.combb6992a2011-04-26 17:41:56 +0000643}
644
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000645void SkGPipeCanvas::onClipRRect(const SkRRect& rrect, SkRegion::Op rgnOp,
646 ClipEdgeStyle edgeStyle) {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000647 NOTIFY_SETUP(this);
reed@google.com4ed0fb72012-12-12 20:48:18 +0000648 if (this->needOpBytes(kSizeOfFlatRRect)) {
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000649 unsigned flags = 0;
650 if (kSoft_ClipEdgeStyle == edgeStyle) {
651 flags = kClip_HasAntiAlias_DrawOpFlag;
652 }
reed@google.com4ed0fb72012-12-12 20:48:18 +0000653 this->writeOp(kClipRRect_DrawOp, flags, rgnOp);
654 fWriter.writeRRect(rrect);
655 }
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000656 this->INHERITED::onClipRRect(rrect, rgnOp, edgeStyle);
reed@google.com4ed0fb72012-12-12 20:48:18 +0000657}
658
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000659void SkGPipeCanvas::onClipPath(const SkPath& path, SkRegion::Op rgnOp,
660 ClipEdgeStyle edgeStyle) {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000661 NOTIFY_SETUP(this);
scroggo@google.com460a23e2012-08-16 17:56:49 +0000662 if (this->needOpBytes(path.writeToMemory(NULL))) {
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000663 unsigned flags = 0;
664 if (kSoft_ClipEdgeStyle == edgeStyle) {
665 flags = kClip_HasAntiAlias_DrawOpFlag;
666 }
scroggo@google.com460a23e2012-08-16 17:56:49 +0000667 this->writeOp(kClipPath_DrawOp, flags, rgnOp);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000668 fWriter.writePath(path);
reed@google.comacd471f2011-05-03 21:26:46 +0000669 }
reed@google.combb6992a2011-04-26 17:41:56 +0000670 // we just pass on the bounds of the path
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000671 this->INHERITED::onClipRect(path.getBounds(), rgnOp, edgeStyle);
reed@google.combb6992a2011-04-26 17:41:56 +0000672}
673
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000674void SkGPipeCanvas::onClipRegion(const SkRegion& region, SkRegion::Op rgnOp) {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000675 NOTIFY_SETUP(this);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000676 if (this->needOpBytes(region.writeToMemory(NULL))) {
reed@google.comacd471f2011-05-03 21:26:46 +0000677 this->writeOp(kClipRegion_DrawOp, 0, rgnOp);
djsollen@google.com2b2ede32012-04-12 13:24:04 +0000678 fWriter.writeRegion(region);
reed@google.comacd471f2011-05-03 21:26:46 +0000679 }
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000680 this->INHERITED::onClipRegion(region, rgnOp);
reed@google.combb6992a2011-04-26 17:41:56 +0000681}
682
683///////////////////////////////////////////////////////////////////////////////
684
685void SkGPipeCanvas::clear(SkColor color) {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000686 NOTIFY_SETUP(this);
reed@google.combb6992a2011-04-26 17:41:56 +0000687 unsigned flags = 0;
688 if (color) {
689 flags |= kClear_HasColor_DrawOpFlag;
690 }
reed@google.comacd471f2011-05-03 21:26:46 +0000691 if (this->needOpBytes(sizeof(SkColor))) {
692 this->writeOp(kDrawClear_DrawOp, flags, 0);
693 if (color) {
694 fWriter.write32(color);
695 }
reed@google.combb6992a2011-04-26 17:41:56 +0000696 }
697}
698
699void SkGPipeCanvas::drawPaint(const SkPaint& paint) {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000700 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000701 this->writePaint(paint);
reed@google.comacd471f2011-05-03 21:26:46 +0000702 if (this->needOpBytes()) {
reed@google.com31891582011-05-12 03:03:56 +0000703 this->writeOp(kDrawPaint_DrawOp);
reed@google.comacd471f2011-05-03 21:26:46 +0000704 }
reed@google.combb6992a2011-04-26 17:41:56 +0000705}
706
707void SkGPipeCanvas::drawPoints(PointMode mode, size_t count,
robertphillips@google.com8b169312013-10-15 17:47:36 +0000708 const SkPoint pts[], const SkPaint& paint) {
reed@google.combb6992a2011-04-26 17:41:56 +0000709 if (count) {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000710 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000711 this->writePaint(paint);
reed@google.comacd471f2011-05-03 21:26:46 +0000712 if (this->needOpBytes(4 + count * sizeof(SkPoint))) {
reed@google.com31891582011-05-12 03:03:56 +0000713 this->writeOp(kDrawPoints_DrawOp, mode, 0);
commit-bot@chromium.orgb45bd1f2014-04-24 18:17:30 +0000714 fWriter.write32(SkToU32(count));
reed@google.comacd471f2011-05-03 21:26:46 +0000715 fWriter.write(pts, count * sizeof(SkPoint));
716 }
reed@google.combb6992a2011-04-26 17:41:56 +0000717 }
718}
719
reed@google.com4ed0fb72012-12-12 20:48:18 +0000720void SkGPipeCanvas::drawOval(const SkRect& rect, const SkPaint& paint) {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000721 NOTIFY_SETUP(this);
reed@google.com4ed0fb72012-12-12 20:48:18 +0000722 this->writePaint(paint);
723 if (this->needOpBytes(sizeof(SkRect))) {
724 this->writeOp(kDrawOval_DrawOp);
725 fWriter.writeRect(rect);
726 }
727}
728
bsalomon@google.com7ce564c2013-10-22 16:54:15 +0000729void SkGPipeCanvas::drawRect(const SkRect& rect, const SkPaint& paint) {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000730 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000731 this->writePaint(paint);
reed@google.comacd471f2011-05-03 21:26:46 +0000732 if (this->needOpBytes(sizeof(SkRect))) {
reed@google.com31891582011-05-12 03:03:56 +0000733 this->writeOp(kDrawRect_DrawOp);
reed@google.comacd471f2011-05-03 21:26:46 +0000734 fWriter.writeRect(rect);
735 }
reed@google.combb6992a2011-04-26 17:41:56 +0000736}
737
reed@google.com4ed0fb72012-12-12 20:48:18 +0000738void SkGPipeCanvas::drawRRect(const SkRRect& rrect, const SkPaint& paint) {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000739 NOTIFY_SETUP(this);
reed@google.com4ed0fb72012-12-12 20:48:18 +0000740 this->writePaint(paint);
741 if (this->needOpBytes(kSizeOfFlatRRect)) {
742 this->writeOp(kDrawRRect_DrawOp);
743 fWriter.writeRRect(rrect);
744 }
745}
746
commit-bot@chromium.orged9806f2014-02-21 02:32:36 +0000747void SkGPipeCanvas::onDrawDRRect(const SkRRect& outer, const SkRRect& inner,
748 const SkPaint& paint) {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000749 NOTIFY_SETUP(this);
commit-bot@chromium.orged9806f2014-02-21 02:32:36 +0000750 this->writePaint(paint);
751 if (this->needOpBytes(kSizeOfFlatRRect * 2)) {
752 this->writeOp(kDrawDRRect_DrawOp);
753 fWriter.writeRRect(outer);
754 fWriter.writeRRect(inner);
755 }
756}
757
bsalomon@google.com7ce564c2013-10-22 16:54:15 +0000758void SkGPipeCanvas::drawPath(const SkPath& path, const SkPaint& paint) {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000759 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000760 this->writePaint(paint);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000761 if (this->needOpBytes(path.writeToMemory(NULL))) {
reed@google.com31891582011-05-12 03:03:56 +0000762 this->writeOp(kDrawPath_DrawOp);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000763 fWriter.writePath(path);
reed@google.comacd471f2011-05-03 21:26:46 +0000764 }
reed@google.combb6992a2011-04-26 17:41:56 +0000765}
766
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000767bool SkGPipeCanvas::commonDrawBitmap(const SkBitmap& bm, DrawOps op,
768 unsigned flags,
769 size_t opBytesNeeded,
770 const SkPaint* paint) {
commit-bot@chromium.org44803fb2014-05-14 15:38:25 +0000771 if (fDone) {
772 return false;
773 }
774
scroggo@google.com58be6822012-07-30 14:40:01 +0000775 if (paint != NULL) {
scroggo@google.com460a23e2012-08-16 17:56:49 +0000776 flags |= kDrawBitmap_HasPaint_DrawOpFlag;
scroggo@google.com58be6822012-07-30 14:40:01 +0000777 this->writePaint(*paint);
778 }
commit-bot@chromium.org44803fb2014-05-14 15:38:25 +0000779 // This needs to run first so its calls to needOpBytes() and its writes
780 // don't interlace with the needOpBytes() and write below.
781 SkASSERT(fBitmapHeap != NULL);
782 int32_t bitmapIndex = fBitmapHeap->insert(bm);
783 if (SkBitmapHeap::INVALID_SLOT == bitmapIndex) {
784 return false;
785 }
786
scroggo@google.com10dccde2012-08-08 20:43:22 +0000787 if (this->needOpBytes(opBytesNeeded)) {
788 this->writeOp(op, flags, bitmapIndex);
scroggo@google.com58be6822012-07-30 14:40:01 +0000789 return true;
790 }
791 return false;
792}
793
794void SkGPipeCanvas::drawBitmap(const SkBitmap& bm, SkScalar left, SkScalar top,
795 const SkPaint* paint) {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000796 NOTIFY_SETUP(this);
scroggo@google.com58be6822012-07-30 14:40:01 +0000797 size_t opBytesNeeded = sizeof(SkScalar) * 2;
798
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000799 if (this->commonDrawBitmap(bm, kDrawBitmap_DrawOp, 0, opBytesNeeded, paint)) {
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000800 fWriter.writeScalar(left);
801 fWriter.writeScalar(top);
802 }
reed@google.combb6992a2011-04-26 17:41:56 +0000803}
804
reed@google.com71121732012-09-18 15:14:33 +0000805void SkGPipeCanvas::drawBitmapRectToRect(const SkBitmap& bm, const SkRect* src,
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +0000806 const SkRect& dst, const SkPaint* paint,
807 DrawBitmapRectFlags dbmrFlags) {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000808 NOTIFY_SETUP(this);
scroggo@google.com58be6822012-07-30 14:40:01 +0000809 size_t opBytesNeeded = sizeof(SkRect);
810 bool hasSrc = src != NULL;
811 unsigned flags;
812 if (hasSrc) {
scroggo@google.com460a23e2012-08-16 17:56:49 +0000813 flags = kDrawBitmap_HasSrcRect_DrawOpFlag;
scroggo@google.com58be6822012-07-30 14:40:01 +0000814 opBytesNeeded += sizeof(int32_t) * 4;
815 } else {
816 flags = 0;
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000817 }
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +0000818 if (dbmrFlags & kBleed_DrawBitmapRectFlag) {
819 flags |= kDrawBitmap_Bleed_DrawOpFlag;
820 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000821
reed@google.com71121732012-09-18 15:14:33 +0000822 if (this->commonDrawBitmap(bm, kDrawBitmapRectToRect_DrawOp, flags, opBytesNeeded, paint)) {
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000823 if (hasSrc) {
reed@google.com71121732012-09-18 15:14:33 +0000824 fWriter.writeRect(*src);
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000825 }
826 fWriter.writeRect(dst);
827 }
reed@google.combb6992a2011-04-26 17:41:56 +0000828}
829
scroggo@google.com58be6822012-07-30 14:40:01 +0000830void SkGPipeCanvas::drawBitmapMatrix(const SkBitmap& bm, const SkMatrix& matrix,
831 const SkPaint* paint) {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000832 NOTIFY_SETUP(this);
scroggo@google.com58be6822012-07-30 14:40:01 +0000833 size_t opBytesNeeded = matrix.writeToMemory(NULL);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000834
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000835 if (this->commonDrawBitmap(bm, kDrawBitmapMatrix_DrawOp, 0, opBytesNeeded, paint)) {
scroggo@google.com58be6822012-07-30 14:40:01 +0000836 fWriter.writeMatrix(matrix);
837 }
reed@google.combb6992a2011-04-26 17:41:56 +0000838}
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000839
840void SkGPipeCanvas::drawBitmapNine(const SkBitmap& bm, const SkIRect& center,
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000841 const SkRect& dst, const SkPaint* paint) {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000842 NOTIFY_SETUP(this);
scroggo@google.com58be6822012-07-30 14:40:01 +0000843 size_t opBytesNeeded = sizeof(int32_t) * 4 + sizeof(SkRect);
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000844
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000845 if (this->commonDrawBitmap(bm, kDrawBitmapNine_DrawOp, 0, opBytesNeeded, paint)) {
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000846 fWriter.write32(center.fLeft);
847 fWriter.write32(center.fTop);
848 fWriter.write32(center.fRight);
849 fWriter.write32(center.fBottom);
850 fWriter.writeRect(dst);
851 }
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000852}
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000853
854void SkGPipeCanvas::drawSprite(const SkBitmap& bm, int left, int top,
855 const SkPaint* paint) {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000856 NOTIFY_SETUP(this);
scroggo@google.com58be6822012-07-30 14:40:01 +0000857 size_t opBytesNeeded = sizeof(int32_t) * 2;
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000858
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000859 if (this->commonDrawBitmap(bm, kDrawSprite_DrawOp, 0, opBytesNeeded, paint)) {
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000860 fWriter.write32(left);
861 fWriter.write32(top);
862 }
reed@google.combb6992a2011-04-26 17:41:56 +0000863}
864
reed@google.come0d9ce82014-04-23 04:00:17 +0000865void SkGPipeCanvas::onDrawText(const void* text, size_t byteLength, SkScalar x, SkScalar y,
866 const SkPaint& paint) {
reed@google.combb6992a2011-04-26 17:41:56 +0000867 if (byteLength) {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000868 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000869 this->writePaint(paint);
reed@google.comacd471f2011-05-03 21:26:46 +0000870 if (this->needOpBytes(4 + SkAlign4(byteLength) + 2 * sizeof(SkScalar))) {
reed@google.com31891582011-05-12 03:03:56 +0000871 this->writeOp(kDrawText_DrawOp);
commit-bot@chromium.orgb45bd1f2014-04-24 18:17:30 +0000872 fWriter.write32(SkToU32(byteLength));
reed@google.comacd471f2011-05-03 21:26:46 +0000873 fWriter.writePad(text, byteLength);
874 fWriter.writeScalar(x);
875 fWriter.writeScalar(y);
876 }
reed@google.combb6992a2011-04-26 17:41:56 +0000877 }
878}
879
reed@google.come0d9ce82014-04-23 04:00:17 +0000880void SkGPipeCanvas::onDrawPosText(const void* text, size_t byteLength, const SkPoint pos[],
881 const SkPaint& paint) {
reed@google.combb6992a2011-04-26 17:41:56 +0000882 if (byteLength) {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000883 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000884 this->writePaint(paint);
reed@google.combb6992a2011-04-26 17:41:56 +0000885 int count = paint.textToGlyphs(text, byteLength, NULL);
reed@google.comacd471f2011-05-03 21:26:46 +0000886 if (this->needOpBytes(4 + SkAlign4(byteLength) + 4 + count * sizeof(SkPoint))) {
reed@google.com31891582011-05-12 03:03:56 +0000887 this->writeOp(kDrawPosText_DrawOp);
commit-bot@chromium.orgb45bd1f2014-04-24 18:17:30 +0000888 fWriter.write32(SkToU32(byteLength));
reed@google.comacd471f2011-05-03 21:26:46 +0000889 fWriter.writePad(text, byteLength);
890 fWriter.write32(count);
891 fWriter.write(pos, count * sizeof(SkPoint));
892 }
reed@google.combb6992a2011-04-26 17:41:56 +0000893 }
894}
895
reed@google.come0d9ce82014-04-23 04:00:17 +0000896void SkGPipeCanvas::onDrawPosTextH(const void* text, size_t byteLength, const SkScalar xpos[],
897 SkScalar constY, const SkPaint& paint) {
reed@google.combb6992a2011-04-26 17:41:56 +0000898 if (byteLength) {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000899 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000900 this->writePaint(paint);
reed@google.combb6992a2011-04-26 17:41:56 +0000901 int count = paint.textToGlyphs(text, byteLength, NULL);
reed@google.comacd471f2011-05-03 21:26:46 +0000902 if (this->needOpBytes(4 + SkAlign4(byteLength) + 4 + count * sizeof(SkScalar) + 4)) {
reed@google.com31891582011-05-12 03:03:56 +0000903 this->writeOp(kDrawPosTextH_DrawOp);
commit-bot@chromium.orgb45bd1f2014-04-24 18:17:30 +0000904 fWriter.write32(SkToU32(byteLength));
reed@google.comacd471f2011-05-03 21:26:46 +0000905 fWriter.writePad(text, byteLength);
906 fWriter.write32(count);
907 fWriter.write(xpos, count * sizeof(SkScalar));
908 fWriter.writeScalar(constY);
909 }
reed@google.combb6992a2011-04-26 17:41:56 +0000910 }
911}
912
reed@google.come0d9ce82014-04-23 04:00:17 +0000913void SkGPipeCanvas::onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& path,
914 const SkMatrix* matrix, const SkPaint& paint) {
reed@google.combb6992a2011-04-26 17:41:56 +0000915 if (byteLength) {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000916 NOTIFY_SETUP(this);
reed@google.combb6992a2011-04-26 17:41:56 +0000917 unsigned flags = 0;
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000918 size_t size = 4 + SkAlign4(byteLength) + path.writeToMemory(NULL);
reed@google.combb6992a2011-04-26 17:41:56 +0000919 if (matrix) {
920 flags |= kDrawTextOnPath_HasMatrix_DrawOpFlag;
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000921 size += matrix->writeToMemory(NULL);
reed@google.combb6992a2011-04-26 17:41:56 +0000922 }
reed@google.com31891582011-05-12 03:03:56 +0000923 this->writePaint(paint);
reed@google.comacd471f2011-05-03 21:26:46 +0000924 if (this->needOpBytes(size)) {
reed@google.com31891582011-05-12 03:03:56 +0000925 this->writeOp(kDrawTextOnPath_DrawOp, flags, 0);
reed@google.combb6992a2011-04-26 17:41:56 +0000926
commit-bot@chromium.orgb45bd1f2014-04-24 18:17:30 +0000927 fWriter.write32(SkToU32(byteLength));
reed@google.comacd471f2011-05-03 21:26:46 +0000928 fWriter.writePad(text, byteLength);
reed@google.combb6992a2011-04-26 17:41:56 +0000929
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000930 fWriter.writePath(path);
reed@google.comacd471f2011-05-03 21:26:46 +0000931 if (matrix) {
djsollen@google.com2b2ede32012-04-12 13:24:04 +0000932 fWriter.writeMatrix(*matrix);
reed@google.comacd471f2011-05-03 21:26:46 +0000933 }
reed@google.combb6992a2011-04-26 17:41:56 +0000934 }
935 }
936}
937
reedd5fa1a42014-08-09 11:08:05 -0700938void SkGPipeCanvas::onDrawPicture(const SkPicture* picture, const SkMatrix* matrix,
939 const SkPaint* paint) {
reed@google.com0faac1e2011-05-11 05:58:58 +0000940 // we want to playback the picture into individual draw calls
reedd5fa1a42014-08-09 11:08:05 -0700941 //
942 // todo: do we always have to unroll? If the pipe is not cross-process, seems like
943 // we could just ref the picture and move on...? <reed, scroggo>
944 //
945 this->INHERITED::onDrawPicture(picture, matrix, paint);
reed@google.combb6992a2011-04-26 17:41:56 +0000946}
947
reed@google.com85e143c2013-12-30 15:51:25 +0000948void SkGPipeCanvas::drawVertices(VertexMode vmode, int vertexCount,
reed@google.combb6992a2011-04-26 17:41:56 +0000949 const SkPoint vertices[], const SkPoint texs[],
reed@google.com85e143c2013-12-30 15:51:25 +0000950 const SkColor colors[], SkXfermode* xfer,
reed@google.combb6992a2011-04-26 17:41:56 +0000951 const uint16_t indices[], int indexCount,
952 const SkPaint& paint) {
953 if (0 == vertexCount) {
954 return;
955 }
956
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000957 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000958 this->writePaint(paint);
commit-bot@chromium.orgd0490172014-05-14 15:12:08 +0000959
960 unsigned flags = 0; // packs with the op, so needs no extra space
961
962 size_t size = 0;
963 size += 4; // vmode
964 size += 4; // vertex count
965 size += vertexCount * sizeof(SkPoint); // vertices
966
reed@google.combb6992a2011-04-26 17:41:56 +0000967 if (texs) {
968 flags |= kDrawVertices_HasTexs_DrawOpFlag;
reed@google.comacd471f2011-05-03 21:26:46 +0000969 size += vertexCount * sizeof(SkPoint);
reed@google.combb6992a2011-04-26 17:41:56 +0000970 }
971 if (colors) {
972 flags |= kDrawVertices_HasColors_DrawOpFlag;
reed@google.comacd471f2011-05-03 21:26:46 +0000973 size += vertexCount * sizeof(SkColor);
reed@google.combb6992a2011-04-26 17:41:56 +0000974 }
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000975 if (xfer && !SkXfermode::IsMode(xfer, SkXfermode::kModulate_Mode)) {
976 flags |= kDrawVertices_HasXfermode_DrawOpFlag;
commit-bot@chromium.orgd0490172014-05-14 15:12:08 +0000977 size += sizeof(int32_t); // SkXfermode::Mode
978 }
979 if (indices && indexCount > 0) {
980 flags |= kDrawVertices_HasIndices_DrawOpFlag;
981 size += 4; // index count
982 size += SkAlign4(indexCount * sizeof(uint16_t)); // indices
reed@google.com85e143c2013-12-30 15:51:25 +0000983 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000984
reed@google.comacd471f2011-05-03 21:26:46 +0000985 if (this->needOpBytes(size)) {
reed@google.com31891582011-05-12 03:03:56 +0000986 this->writeOp(kDrawVertices_DrawOp, flags, 0);
reed@google.com85e143c2013-12-30 15:51:25 +0000987 fWriter.write32(vmode);
reed@google.comacd471f2011-05-03 21:26:46 +0000988 fWriter.write32(vertexCount);
989 fWriter.write(vertices, vertexCount * sizeof(SkPoint));
commit-bot@chromium.orgd0490172014-05-14 15:12:08 +0000990 if (flags & kDrawVertices_HasTexs_DrawOpFlag) {
reed@google.comacd471f2011-05-03 21:26:46 +0000991 fWriter.write(texs, vertexCount * sizeof(SkPoint));
992 }
commit-bot@chromium.orgd0490172014-05-14 15:12:08 +0000993 if (flags & kDrawVertices_HasColors_DrawOpFlag) {
reed@google.comacd471f2011-05-03 21:26:46 +0000994 fWriter.write(colors, vertexCount * sizeof(SkColor));
995 }
reed@google.com85e143c2013-12-30 15:51:25 +0000996 if (flags & kDrawVertices_HasXfermode_DrawOpFlag) {
997 SkXfermode::Mode mode = SkXfermode::kModulate_Mode;
commit-bot@chromium.orgd0490172014-05-14 15:12:08 +0000998 SkAssertResult(xfer->asMode(&mode));
reed@google.com85e143c2013-12-30 15:51:25 +0000999 fWriter.write32(mode);
1000 }
commit-bot@chromium.orgd0490172014-05-14 15:12:08 +00001001 if (flags & kDrawVertices_HasIndices_DrawOpFlag) {
reed@google.comacd471f2011-05-03 21:26:46 +00001002 fWriter.write32(indexCount);
1003 fWriter.writePad(indices, indexCount * sizeof(uint16_t));
1004 }
reed@google.combb6992a2011-04-26 17:41:56 +00001005 }
1006}
1007
dandovb3c9d1c2014-08-12 08:34:29 -07001008void SkGPipeCanvas::onDrawPatch(const SkPoint cubics[12], const SkColor colors[4],
1009 const SkPoint texCoords[4], SkXfermode* xmode,
1010 const SkPaint& paint) {
dandov963137b2014-08-07 07:49:53 -07001011 NOTIFY_SETUP(this);
dandovb3c9d1c2014-08-12 08:34:29 -07001012
1013 size_t size = SkPatchUtils::kNumCtrlPts * sizeof(SkPoint);
1014 unsigned flags = 0;
1015 if (NULL != colors) {
1016 flags |= kDrawVertices_HasColors_DrawOpFlag;
1017 size += SkPatchUtils::kNumCorners * sizeof(SkColor);
1018 }
1019 if (NULL != texCoords) {
1020 flags |= kDrawVertices_HasTexs_DrawOpFlag;
1021 size += SkPatchUtils::kNumCorners * sizeof(SkPoint);
1022 }
1023 if (NULL != xmode) {
1024 SkXfermode::Mode mode;
1025 if (xmode->asMode(&mode) && SkXfermode::kModulate_Mode != mode) {
1026 flags |= kDrawVertices_HasXfermode_DrawOpFlag;
1027 size += sizeof(int32_t);
1028 }
1029 }
1030
dandov963137b2014-08-07 07:49:53 -07001031 this->writePaint(paint);
dandovb3c9d1c2014-08-12 08:34:29 -07001032 if (this->needOpBytes(size)) {
1033 this->writeOp(kDrawPatch_DrawOp, flags, 0);
1034
1035 fWriter.write(cubics, SkPatchUtils::kNumCtrlPts * sizeof(SkPoint));
1036
1037 if (NULL != colors) {
1038 fWriter.write(colors, SkPatchUtils::kNumCorners * sizeof(SkColor));
1039 }
1040
1041 if (NULL != texCoords) {
1042 fWriter.write(texCoords, SkPatchUtils::kNumCorners * sizeof(SkPoint));
1043 }
1044
1045 if (flags & kDrawVertices_HasXfermode_DrawOpFlag) {
1046 SkXfermode::Mode mode = SkXfermode::kModulate_Mode;
1047 SkAssertResult(xmode->asMode(&mode));
1048 fWriter.write32(mode);
1049 }
dandov963137b2014-08-07 07:49:53 -07001050 }
1051}
1052
reed@google.comacd471f2011-05-03 21:26:46 +00001053void SkGPipeCanvas::drawData(const void* ptr, size_t size) {
1054 if (size && ptr) {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +00001055 NOTIFY_SETUP(this);
reed@google.combb6992a2011-04-26 17:41:56 +00001056 unsigned data = 0;
1057 if (size < (1 << DRAWOPS_DATA_BITS)) {
1058 data = (unsigned)size;
1059 }
reed@google.comacd471f2011-05-03 21:26:46 +00001060 if (this->needOpBytes(4 + SkAlign4(size))) {
1061 this->writeOp(kDrawData_DrawOp, 0, data);
1062 if (0 == data) {
commit-bot@chromium.orgb45bd1f2014-04-24 18:17:30 +00001063 fWriter.write32(SkToU32(size));
reed@google.comacd471f2011-05-03 21:26:46 +00001064 }
reed@google.combb6793b2011-05-05 15:18:15 +00001065 fWriter.writePad(ptr, size);
reed@google.combb6992a2011-04-26 17:41:56 +00001066 }
1067 }
1068}
1069
robertphillips@google.com0a4805e2013-05-29 13:24:23 +00001070void SkGPipeCanvas::beginCommentGroup(const char* description) {
1071 // ignore for now
1072}
1073
1074void SkGPipeCanvas::addComment(const char* kywd, const char* value) {
1075 // ignore for now
1076}
1077
1078void SkGPipeCanvas::endCommentGroup() {
1079 // ignore for now
1080}
1081
junov@chromium.org77eec242012-07-18 17:54:45 +00001082void SkGPipeCanvas::flushRecording(bool detachCurrentBlock) {
commit-bot@chromium.orgd0490172014-05-14 15:12:08 +00001083 this->doNotify();
junov@chromium.org77eec242012-07-18 17:54:45 +00001084 if (detachCurrentBlock) {
1085 // force a new block to be requested for the next recorded command
rmistry@google.comd6176b02012-08-23 18:14:13 +00001086 fBlockSize = 0;
junov@chromium.org77eec242012-07-18 17:54:45 +00001087 }
1088}
1089
junov@chromium.org2e14ba82012-08-07 14:26:57 +00001090size_t SkGPipeCanvas::freeMemoryIfPossible(size_t bytesToFree) {
scroggo@google.comd5d158b2012-08-14 20:38:28 +00001091 return (NULL == fBitmapHeap) ? 0 : fBitmapHeap->freeMemoryIfPossible(bytesToFree);
junov@chromium.org2e14ba82012-08-07 14:26:57 +00001092}
1093
reed@google.combb6992a2011-04-26 17:41:56 +00001094///////////////////////////////////////////////////////////////////////////////
1095
1096template <typename T> uint32_t castToU32(T value) {
1097 union {
1098 T fSrc;
1099 uint32_t fDst;
1100 } data;
1101 data.fSrc = value;
1102 return data.fDst;
1103}
1104
reed@google.com31891582011-05-12 03:03:56 +00001105void SkGPipeCanvas::writePaint(const SkPaint& paint) {
scroggo@google.comd5d158b2012-08-14 20:38:28 +00001106 if (fDone) {
1107 return;
1108 }
reed@google.com31891582011-05-12 03:03:56 +00001109 SkPaint& base = fPaint;
reed@google.combb6992a2011-04-26 17:41:56 +00001110 uint32_t storage[32];
1111 uint32_t* ptr = storage;
reed@google.combb6992a2011-04-26 17:41:56 +00001112
1113 if (base.getFlags() != paint.getFlags()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001114 *ptr++ = PaintOp_packOpData(kFlags_PaintOp, paint.getFlags());
reed@google.comf5842f72011-05-04 18:30:04 +00001115 base.setFlags(paint.getFlags());
reed@google.combb6992a2011-04-26 17:41:56 +00001116 }
1117 if (base.getColor() != paint.getColor()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001118 *ptr++ = PaintOp_packOp(kColor_PaintOp);
1119 *ptr++ = paint.getColor();
reed@google.comf5842f72011-05-04 18:30:04 +00001120 base.setColor(paint.getColor());
reed@google.combb6992a2011-04-26 17:41:56 +00001121 }
commit-bot@chromium.org85faf502014-04-16 12:58:02 +00001122 if (base.getFilterLevel() != paint.getFilterLevel()) {
1123 *ptr++ = PaintOp_packOpData(kFilterLevel_PaintOp, paint.getFilterLevel());
1124 base.setFilterLevel(paint.getFilterLevel());
1125 }
reed@google.combb6992a2011-04-26 17:41:56 +00001126 if (base.getStyle() != paint.getStyle()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001127 *ptr++ = PaintOp_packOpData(kStyle_PaintOp, paint.getStyle());
reed@google.comf5842f72011-05-04 18:30:04 +00001128 base.setStyle(paint.getStyle());
reed@google.combb6992a2011-04-26 17:41:56 +00001129 }
1130 if (base.getStrokeJoin() != paint.getStrokeJoin()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001131 *ptr++ = PaintOp_packOpData(kJoin_PaintOp, paint.getStrokeJoin());
reed@google.comf5842f72011-05-04 18:30:04 +00001132 base.setStrokeJoin(paint.getStrokeJoin());
reed@google.combb6992a2011-04-26 17:41:56 +00001133 }
1134 if (base.getStrokeCap() != paint.getStrokeCap()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001135 *ptr++ = PaintOp_packOpData(kCap_PaintOp, paint.getStrokeCap());
reed@google.comf5842f72011-05-04 18:30:04 +00001136 base.setStrokeCap(paint.getStrokeCap());
reed@google.combb6992a2011-04-26 17:41:56 +00001137 }
1138 if (base.getStrokeWidth() != paint.getStrokeWidth()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001139 *ptr++ = PaintOp_packOp(kWidth_PaintOp);
1140 *ptr++ = castToU32(paint.getStrokeWidth());
reed@google.comf5842f72011-05-04 18:30:04 +00001141 base.setStrokeWidth(paint.getStrokeWidth());
reed@google.combb6992a2011-04-26 17:41:56 +00001142 }
1143 if (base.getStrokeMiter() != paint.getStrokeMiter()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001144 *ptr++ = PaintOp_packOp(kMiter_PaintOp);
1145 *ptr++ = castToU32(paint.getStrokeMiter());
reed@google.comf5842f72011-05-04 18:30:04 +00001146 base.setStrokeMiter(paint.getStrokeMiter());
reed@google.combb6992a2011-04-26 17:41:56 +00001147 }
1148 if (base.getTextEncoding() != paint.getTextEncoding()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001149 *ptr++ = PaintOp_packOpData(kEncoding_PaintOp, paint.getTextEncoding());
reed@google.comf5842f72011-05-04 18:30:04 +00001150 base.setTextEncoding(paint.getTextEncoding());
reed@google.combb6992a2011-04-26 17:41:56 +00001151 }
1152 if (base.getHinting() != paint.getHinting()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001153 *ptr++ = PaintOp_packOpData(kHinting_PaintOp, paint.getHinting());
reed@google.comf5842f72011-05-04 18:30:04 +00001154 base.setHinting(paint.getHinting());
reed@google.combb6992a2011-04-26 17:41:56 +00001155 }
1156 if (base.getTextAlign() != paint.getTextAlign()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001157 *ptr++ = PaintOp_packOpData(kAlign_PaintOp, paint.getTextAlign());
reed@google.comf5842f72011-05-04 18:30:04 +00001158 base.setTextAlign(paint.getTextAlign());
reed@google.combb6992a2011-04-26 17:41:56 +00001159 }
1160 if (base.getTextSize() != paint.getTextSize()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001161 *ptr++ = PaintOp_packOp(kTextSize_PaintOp);
1162 *ptr++ = castToU32(paint.getTextSize());
reed@google.comf5842f72011-05-04 18:30:04 +00001163 base.setTextSize(paint.getTextSize());
reed@google.combb6992a2011-04-26 17:41:56 +00001164 }
1165 if (base.getTextScaleX() != paint.getTextScaleX()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001166 *ptr++ = PaintOp_packOp(kTextScaleX_PaintOp);
1167 *ptr++ = castToU32(paint.getTextScaleX());
reed@google.comf5842f72011-05-04 18:30:04 +00001168 base.setTextScaleX(paint.getTextScaleX());
reed@google.combb6992a2011-04-26 17:41:56 +00001169 }
1170 if (base.getTextSkewX() != paint.getTextSkewX()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001171 *ptr++ = PaintOp_packOp(kTextSkewX_PaintOp);
1172 *ptr++ = castToU32(paint.getTextSkewX());
reed@google.comf5842f72011-05-04 18:30:04 +00001173 base.setTextSkewX(paint.getTextSkewX());
1174 }
1175
1176 if (!SkTypeface::Equal(base.getTypeface(), paint.getTypeface())) {
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +00001177 if (isCrossProcess(fFlags)) {
scroggo@google.com3cb969f2012-07-27 20:39:19 +00001178 uint32_t id = this->getTypefaceID(paint.getTypeface());
1179 *ptr++ = PaintOp_packOpData(kTypeface_PaintOp, id);
1180 } else if (this->needOpBytes(sizeof(void*))) {
1181 // Add to the set for ref counting.
1182 fTypefaceSet.add(paint.getTypeface());
1183 // It is safe to write the typeface to the stream before the rest
1184 // of the paint unless we ever send a kReset_PaintOp, which we
1185 // currently never do.
1186 this->writeOp(kSetTypeface_DrawOp);
1187 fWriter.writePtr(paint.getTypeface());
1188 }
reed@google.comf5842f72011-05-04 18:30:04 +00001189 base.setTypeface(paint.getTypeface());
reed@google.combb6992a2011-04-26 17:41:56 +00001190 }
reed@google.combb6992a2011-04-26 17:41:56 +00001191
scroggo@google.com4dffc592012-07-17 16:49:40 +00001192 // This is a new paint, so all old flats can be safely purged, if necessary.
1193 fFlattenableHeap.markAllFlatsSafeToDelete();
reed@google.comb55d1182011-05-11 00:42:04 +00001194 for (int i = 0; i < kCount_PaintFlats; i++) {
1195 int index = this->flattenToIndex(get_paintflat(paint, i), (PaintFlats)i);
scroggo@google.comd3ba5cc2012-07-09 16:05:53 +00001196 bool replaced = index < 0;
1197 if (replaced) {
1198 index = ~index;
1199 }
scroggo@google.com4dffc592012-07-17 16:49:40 +00001200 // Store the index of any flat that needs to be kept. 0 means no flat.
1201 if (index > 0) {
1202 fFlattenableHeap.markFlatForKeeping(index);
1203 }
1204 SkASSERT(index >= 0 && index <= fFlatDictionary.count());
scroggo@google.comd3ba5cc2012-07-09 16:05:53 +00001205 if (index != fCurrFlatIndex[i] || replaced) {
reed@google.comb55d1182011-05-11 00:42:04 +00001206 *ptr++ = PaintOp_packOpFlagData(kFlatIndex_PaintOp, i, index);
1207 fCurrFlatIndex[i] = index;
1208 }
1209 }
1210
reed@google.comacd471f2011-05-03 21:26:46 +00001211 size_t size = (char*)ptr - (char*)storage;
1212 if (size && this->needOpBytes(size)) {
commit-bot@chromium.orgb45bd1f2014-04-24 18:17:30 +00001213 this->writeOp(kPaintOp_DrawOp, 0, SkToU32(size));
reed@google.comb55d1182011-05-11 00:42:04 +00001214 fWriter.write(storage, size);
reed@google.combb6992a2011-04-26 17:41:56 +00001215 for (size_t i = 0; i < size/4; i++) {
reed@google.comb55d1182011-05-11 00:42:04 +00001216// SkDebugf("[%d] %08X\n", i, storage[i]);
reed@google.combb6992a2011-04-26 17:41:56 +00001217 }
1218 }
reed@google.com0cd2ac62013-10-14 20:02:44 +00001219
1220 //
1221 // Do these after we've written kPaintOp_DrawOp
skia.committer@gmail.comfbc58a32013-10-15 07:02:27 +00001222
reed@google.com0cd2ac62013-10-14 20:02:44 +00001223 if (base.getAnnotation() != paint.getAnnotation()) {
1224 if (NULL == paint.getAnnotation()) {
commit-bot@chromium.org89ff3dd2013-10-29 20:29:38 +00001225 if (this->needOpBytes()) {
1226 this->writeOp(kSetAnnotation_DrawOp, 0, 0);
1227 }
reed@google.com0cd2ac62013-10-14 20:02:44 +00001228 } else {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +00001229 SkWriteBuffer buffer;
reed@google.com0cd2ac62013-10-14 20:02:44 +00001230 paint.getAnnotation()->writeToBuffer(buffer);
commit-bot@chromium.org89ff3dd2013-10-29 20:29:38 +00001231 const size_t size = buffer.bytesWritten();
1232 if (this->needOpBytes(size)) {
commit-bot@chromium.orgb45bd1f2014-04-24 18:17:30 +00001233 this->writeOp(kSetAnnotation_DrawOp, 0, SkToU32(size));
commit-bot@chromium.org89ff3dd2013-10-29 20:29:38 +00001234 buffer.writeToMemory(fWriter.reserve(size));
1235 }
reed@google.com0cd2ac62013-10-14 20:02:44 +00001236 }
commit-bot@chromium.org40258a52013-10-29 19:23:26 +00001237 base.setAnnotation(paint.getAnnotation());
reed@google.com0cd2ac62013-10-14 20:02:44 +00001238 }
reed@google.combb6992a2011-04-26 17:41:56 +00001239}
1240
1241///////////////////////////////////////////////////////////////////////////////
1242
1243#include "SkGPipe.h"
1244
scroggo@google.com3cb969f2012-07-27 20:39:19 +00001245SkGPipeController::~SkGPipeController() {
1246 SkSafeUnref(fCanvas);
1247}
1248
1249void SkGPipeController::setCanvas(SkGPipeCanvas* canvas) {
1250 SkRefCnt_SafeAssign(fCanvas, canvas);
1251}
1252
1253///////////////////////////////////////////////////////////////////////////////
1254
1255SkGPipeWriter::SkGPipeWriter()
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +00001256: fWriter(0) {
reed@google.combb6992a2011-04-26 17:41:56 +00001257 fCanvas = NULL;
1258}
1259
1260SkGPipeWriter::~SkGPipeWriter() {
reed@google.comacd471f2011-05-03 21:26:46 +00001261 this->endRecording();
reed@google.combb6992a2011-04-26 17:41:56 +00001262}
1263
junov@chromium.orga8db8fe2012-08-15 19:49:22 +00001264SkCanvas* SkGPipeWriter::startRecording(SkGPipeController* controller, uint32_t flags,
1265 uint32_t width, uint32_t height) {
reed@google.combb6992a2011-04-26 17:41:56 +00001266 if (NULL == fCanvas) {
reed@google.comacd471f2011-05-03 21:26:46 +00001267 fWriter.reset(NULL, 0);
junov@chromium.orga8db8fe2012-08-15 19:49:22 +00001268 fCanvas = SkNEW_ARGS(SkGPipeCanvas, (controller, &fWriter, flags, width, height));
reed@google.combb6992a2011-04-26 17:41:56 +00001269 }
scroggo@google.com3cb969f2012-07-27 20:39:19 +00001270 controller->setCanvas(fCanvas);
reed@google.combb6992a2011-04-26 17:41:56 +00001271 return fCanvas;
1272}
1273
1274void SkGPipeWriter::endRecording() {
1275 if (fCanvas) {
scroggo@google.com59c3ab62013-11-12 14:32:38 +00001276 fCanvas->finish(true);
reed@google.combb6992a2011-04-26 17:41:56 +00001277 fCanvas->unref();
1278 fCanvas = NULL;
1279 }
1280}
1281
junov@chromium.org2e14ba82012-08-07 14:26:57 +00001282void SkGPipeWriter::flushRecording(bool detachCurrentBlock) {
1283 if (fCanvas) {
1284 fCanvas->flushRecording(detachCurrentBlock);
1285 }
junov@chromium.org77eec242012-07-18 17:54:45 +00001286}
1287
junov@chromium.org2e14ba82012-08-07 14:26:57 +00001288size_t SkGPipeWriter::freeMemoryIfPossible(size_t bytesToFree) {
1289 if (fCanvas) {
1290 return fCanvas->freeMemoryIfPossible(bytesToFree);
1291 }
1292 return 0;
1293}
1294
1295size_t SkGPipeWriter::storageAllocatedForRecording() const {
scroggo@google.com15011ee2012-07-26 20:03:32 +00001296 return NULL == fCanvas ? 0 : fCanvas->storageAllocatedForRecording();
1297}
1298
scroggo@google.com3e26bd02012-08-14 15:20:01 +00001299///////////////////////////////////////////////////////////////////////////////
1300
1301BitmapShuttle::BitmapShuttle(SkGPipeCanvas* canvas) {
1302 SkASSERT(canvas != NULL);
1303 fCanvas = canvas;
1304 fCanvas->ref();
1305}
1306
1307BitmapShuttle::~BitmapShuttle() {
scroggo@google.com59c3ab62013-11-12 14:32:38 +00001308 this->removeCanvas();
scroggo@google.com3e26bd02012-08-14 15:20:01 +00001309}
1310
1311bool BitmapShuttle::insert(const SkBitmap& bitmap, int32_t slot) {
scroggo@google.com59c3ab62013-11-12 14:32:38 +00001312 SkASSERT(fCanvas != NULL);
scroggo@google.com3e26bd02012-08-14 15:20:01 +00001313 return fCanvas->shuttleBitmap(bitmap, slot);
1314}
scroggo@google.com59c3ab62013-11-12 14:32:38 +00001315
1316void BitmapShuttle::removeCanvas() {
1317 if (NULL == fCanvas) {
1318 return;
1319 }
1320 fCanvas->unref();
1321 fCanvas = NULL;
1322}