blob: 82848f8a6ff1af1aa7b81b6147f791c658bcb4b3 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@google.combb6992a2011-04-26 17:41:56 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
reed@google.combb6992a2011-04-26 17:41:56 +00007 */
8
epoger@google.comb58772f2013-03-08 09:09:10 +00009#include "SkAnnotation.h"
robertphillips@google.com1f2f3382013-08-29 11:54:56 +000010#include "SkBitmapDevice.h"
scroggo@google.com10dccde2012-08-08 20:43:22 +000011#include "SkBitmapHeap.h"
reed@google.combb6992a2011-04-26 17:41:56 +000012#include "SkCanvas.h"
scroggo@google.com10dccde2012-08-08 20:43:22 +000013#include "SkColorFilter.h"
reed@google.com8a85d0c2011-06-24 19:12:12 +000014#include "SkData.h"
scroggo@google.com10dccde2012-08-08 20:43:22 +000015#include "SkDrawLooper.h"
reed@google.comacd471f2011-05-03 21:26:46 +000016#include "SkGPipe.h"
reed@google.combb6992a2011-04-26 17:41:56 +000017#include "SkGPipePriv.h"
scroggo@google.com16d1d0b2012-05-02 19:09:40 +000018#include "SkImageFilter.h"
scroggo@google.com10dccde2012-08-08 20:43:22 +000019#include "SkMaskFilter.h"
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000020#include "SkWriteBuffer.h"
scroggo@google.com10dccde2012-08-08 20:43:22 +000021#include "SkPaint.h"
22#include "SkPathEffect.h"
23#include "SkPictureFlat.h"
24#include "SkRasterizer.h"
reed@google.com4ed0fb72012-12-12 20:48:18 +000025#include "SkRRect.h"
scroggo@google.com10dccde2012-08-08 20:43:22 +000026#include "SkShader.h"
reed@google.comf5842f72011-05-04 18:30:04 +000027#include "SkStream.h"
reed@google.comb55d1182011-05-11 00:42:04 +000028#include "SkTSearch.h"
reed@google.comf5842f72011-05-04 18:30:04 +000029#include "SkTypeface.h"
reed@google.combb6992a2011-04-26 17:41:56 +000030#include "SkWriter32.h"
reed@google.comb55d1182011-05-11 00:42:04 +000031
reed@google.com4ed0fb72012-12-12 20:48:18 +000032enum {
33 kSizeOfFlatRRect = sizeof(SkRect) + 4 * sizeof(SkVector)
34};
35
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +000036static bool isCrossProcess(uint32_t flags) {
37 return SkToBool(flags & SkGPipeWriter::kCrossProcess_Flag);
38}
39
reed@google.comb55d1182011-05-11 00:42:04 +000040static SkFlattenable* get_paintflat(const SkPaint& paint, unsigned paintFlat) {
41 SkASSERT(paintFlat < kCount_PaintFlats);
42 switch (paintFlat) {
43 case kColorFilter_PaintFlat: return paint.getColorFilter();
reed@google.com0faac1e2011-05-11 05:58:58 +000044 case kDrawLooper_PaintFlat: return paint.getLooper();
reed@google.comb55d1182011-05-11 00:42:04 +000045 case kMaskFilter_PaintFlat: return paint.getMaskFilter();
46 case kPathEffect_PaintFlat: return paint.getPathEffect();
47 case kRasterizer_PaintFlat: return paint.getRasterizer();
48 case kShader_PaintFlat: return paint.getShader();
scroggo@google.com16d1d0b2012-05-02 19:09:40 +000049 case kImageFilter_PaintFlat: return paint.getImageFilter();
reed@google.comb55d1182011-05-11 00:42:04 +000050 case kXfermode_PaintFlat: return paint.getXfermode();
51 }
tomhudson@google.com0c00f212011-12-28 14:59:50 +000052 SkDEBUGFAIL("never gets here");
reed@google.comb55d1182011-05-11 00:42:04 +000053 return NULL;
54}
reed@google.combb6992a2011-04-26 17:41:56 +000055
reed@google.comf5842f72011-05-04 18:30:04 +000056static size_t writeTypeface(SkWriter32* writer, SkTypeface* typeface) {
57 SkASSERT(typeface);
58 SkDynamicMemoryWStream stream;
59 typeface->serialize(&stream);
60 size_t size = stream.getOffset();
61 if (writer) {
commit-bot@chromium.orgb45bd1f2014-04-24 18:17:30 +000062 writer->write32(SkToU32(size));
reed@google.com8a85d0c2011-06-24 19:12:12 +000063 SkAutoDataUnref data(stream.copyToData());
robertphillips@google.com59f46b82012-07-10 17:30:58 +000064 writer->writePad(data->data(), size);
reed@google.comf5842f72011-05-04 18:30:04 +000065 }
scroggo@google.com5af9b202012-06-04 17:17:36 +000066 return 4 + SkAlign4(size);
reed@google.comf5842f72011-05-04 18:30:04 +000067}
68
reed@google.combb6992a2011-04-26 17:41:56 +000069///////////////////////////////////////////////////////////////////////////////
70
scroggo@google.com4dffc592012-07-17 16:49:40 +000071class FlattenableHeap : public SkFlatController {
72public:
scroggo@google.com664fab12012-08-14 19:22:05 +000073 FlattenableHeap(int numFlatsToKeep, SkNamedFactorySet* fset, bool isCrossProcess)
commit-bot@chromium.orga2bd2d12014-01-30 22:16:32 +000074 : INHERITED(isCrossProcess ? SkWriteBuffer::kCrossProcess_Flag : 0)
75 , fNumFlatsToKeep(numFlatsToKeep) {
scroggo@google.com664fab12012-08-14 19:22:05 +000076 SkASSERT((isCrossProcess && fset != NULL) || (!isCrossProcess && NULL == fset));
77 if (isCrossProcess) {
78 this->setNamedFactorySet(fset);
scroggo@google.com664fab12012-08-14 19:22:05 +000079 }
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +000080 }
scroggo@google.com4dffc592012-07-17 16:49:40 +000081
82 ~FlattenableHeap() {
83 fPointers.freeAll();
84 }
85
86 virtual void* allocThrow(size_t bytes) SK_OVERRIDE;
87
88 virtual void unalloc(void* ptr) SK_OVERRIDE;
89
scroggo@google.com7ca24432012-08-14 15:48:43 +000090 void setBitmapStorage(SkBitmapHeap* heap) {
91 this->setBitmapHeap(heap);
92 }
rmistry@google.comd6176b02012-08-23 18:14:13 +000093
scroggo@google.com4dffc592012-07-17 16:49:40 +000094 const SkFlatData* flatToReplace() const;
95
96 // Mark an SkFlatData as one that should not be returned by flatToReplace.
97 // Takes the result of SkFlatData::index() as its parameter.
98 void markFlatForKeeping(int index) {
99 *fFlatsThatMustBeKept.append() = index;
100 }
101
102 void markAllFlatsSafeToDelete() {
103 fFlatsThatMustBeKept.reset();
104 }
105
106private:
107 // Keep track of the indices (i.e. the result of SkFlatData::index()) of
108 // flats that must be kept, since they are on the current paint.
109 SkTDArray<int> fFlatsThatMustBeKept;
110 SkTDArray<void*> fPointers;
111 const int fNumFlatsToKeep;
commit-bot@chromium.orga2bd2d12014-01-30 22:16:32 +0000112
113 typedef SkFlatController INHERITED;
scroggo@google.com4dffc592012-07-17 16:49:40 +0000114};
115
116void FlattenableHeap::unalloc(void* ptr) {
117 int indexToRemove = fPointers.rfind(ptr);
118 if (indexToRemove >= 0) {
119 sk_free(ptr);
120 fPointers.remove(indexToRemove);
121 }
122}
123
124void* FlattenableHeap::allocThrow(size_t bytes) {
125 void* ptr = sk_malloc_throw(bytes);
126 *fPointers.append() = ptr;
127 return ptr;
128}
129
130const SkFlatData* FlattenableHeap::flatToReplace() const {
131 // First, determine whether we should replace one.
132 if (fPointers.count() > fNumFlatsToKeep) {
133 // Look through the flattenable heap.
134 // TODO: Return the LRU flat.
135 for (int i = 0; i < fPointers.count(); i++) {
136 SkFlatData* potential = (SkFlatData*)fPointers[i];
137 // Make sure that it is not one that must be kept.
138 bool mustKeep = false;
139 for (int j = 0; j < fFlatsThatMustBeKept.count(); j++) {
140 if (potential->index() == fFlatsThatMustBeKept[j]) {
141 mustKeep = true;
142 break;
143 }
144 }
145 if (!mustKeep) {
146 return potential;
147 }
148 }
149 }
150 return NULL;
151}
152
153///////////////////////////////////////////////////////////////////////////////
154
commit-bot@chromium.org07adb632014-01-02 22:20:49 +0000155struct SkFlattenableTraits {
commit-bot@chromium.org186c0cc2014-02-18 16:15:05 +0000156 static void Flatten(SkWriteBuffer& buffer, const SkFlattenable& flattenable) {
commit-bot@chromium.org07adb632014-01-02 22:20:49 +0000157 buffer.writeFlattenable(&flattenable);
scroggo@google.com4dffc592012-07-17 16:49:40 +0000158 }
commit-bot@chromium.org07adb632014-01-02 22:20:49 +0000159 // No need to define unflatten if we never call it.
scroggo@google.com4dffc592012-07-17 16:49:40 +0000160};
commit-bot@chromium.org07adb632014-01-02 22:20:49 +0000161typedef SkFlatDictionary<SkFlattenable, SkFlattenableTraits> FlatDictionary;
scroggo@google.com4dffc592012-07-17 16:49:40 +0000162
163///////////////////////////////////////////////////////////////////////////////
164
scroggo@google.com59c3ab62013-11-12 14:32:38 +0000165/**
166 * If SkBitmaps are to be flattened to send to the reader, this class is
167 * provided to the SkBitmapHeap to tell the SkGPipeCanvas to do so.
168 */
169class BitmapShuttle : public SkBitmapHeap::ExternalStorage {
170public:
171 BitmapShuttle(SkGPipeCanvas*);
172
173 ~BitmapShuttle();
174
175 virtual bool insert(const SkBitmap& bitmap, int32_t slot) SK_OVERRIDE;
176
177 /**
178 * Remove the SkGPipeCanvas used for insertion. After this, calls to
179 * insert will crash.
180 */
181 void removeCanvas();
182
183private:
184 SkGPipeCanvas* fCanvas;
185};
186
187///////////////////////////////////////////////////////////////////////////////
188
reed@google.combb6992a2011-04-26 17:41:56 +0000189class SkGPipeCanvas : public SkCanvas {
190public:
junov@chromium.orga8db8fe2012-08-15 19:49:22 +0000191 SkGPipeCanvas(SkGPipeController*, SkWriter32*, uint32_t flags,
192 uint32_t width, uint32_t height);
reed@google.combb6992a2011-04-26 17:41:56 +0000193 virtual ~SkGPipeCanvas();
194
scroggo@google.com59c3ab62013-11-12 14:32:38 +0000195 /**
196 * Called when nothing else is to be written to the stream. Any repeated
197 * calls are ignored.
198 *
199 * @param notifyReaders Whether to send a message to the reader(s) that
200 * the writer is through sending commands. Should generally be true,
201 * unless there is an error which prevents further messages from
202 * being sent.
203 */
204 void finish(bool notifyReaders) {
205 if (fDone) {
206 return;
reed@google.combb6992a2011-04-26 17:41:56 +0000207 }
scroggo@google.com59c3ab62013-11-12 14:32:38 +0000208 if (notifyReaders && this->needOpBytes()) {
209 this->writeOp(kDone_DrawOp);
210 this->doNotify();
211 }
212 if (shouldFlattenBitmaps(fFlags)) {
213 // The following circular references exist:
214 // fFlattenableHeap -> fWriteBuffer -> fBitmapStorage -> fExternalStorage -> fCanvas
215 // fBitmapHeap -> fExternalStorage -> fCanvas
216 // fFlattenableHeap -> fBitmapStorage -> fExternalStorage -> fCanvas
217
218 // Break them all by destroying the final link to this SkGPipeCanvas.
219 fBitmapShuttle->removeCanvas();
220 }
221 fDone = true;
reed@google.combb6992a2011-04-26 17:41:56 +0000222 }
223
junov@chromium.org77eec242012-07-18 17:54:45 +0000224 void flushRecording(bool detachCurrentBlock);
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000225 size_t freeMemoryIfPossible(size_t bytesToFree);
junov@chromium.org77eec242012-07-18 17:54:45 +0000226
scroggo@google.com15011ee2012-07-26 20:03:32 +0000227 size_t storageAllocatedForRecording() {
scroggo@google.comd5d158b2012-08-14 20:38:28 +0000228 return (NULL == fBitmapHeap) ? 0 : fBitmapHeap->bytesAllocated();
scroggo@google.com15011ee2012-07-26 20:03:32 +0000229 }
230
reed@google.combb6992a2011-04-26 17:41:56 +0000231 // overrides from SkCanvas
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000232 virtual bool isDrawingToLayer() const SK_OVERRIDE;
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000233 virtual void clear(SkColor) SK_OVERRIDE;
234 virtual void drawPaint(const SkPaint& paint) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000235 virtual void drawPoints(PointMode, size_t count, const SkPoint pts[],
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000236 const SkPaint&) SK_OVERRIDE;
reed@google.com4ed0fb72012-12-12 20:48:18 +0000237 virtual void drawOval(const SkRect&, const SkPaint&) SK_OVERRIDE;
bsalomon@google.com7ce564c2013-10-22 16:54:15 +0000238 virtual void drawRect(const SkRect& rect, const SkPaint&) SK_OVERRIDE;
reed@google.com4ed0fb72012-12-12 20:48:18 +0000239 virtual void drawRRect(const SkRRect&, const SkPaint&) SK_OVERRIDE;
bsalomon@google.com7ce564c2013-10-22 16:54:15 +0000240 virtual void drawPath(const SkPath& path, const SkPaint&) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000241 virtual void drawBitmap(const SkBitmap&, SkScalar left, SkScalar top,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000242 const SkPaint*) SK_OVERRIDE;
reed@google.com71121732012-09-18 15:14:33 +0000243 virtual void drawBitmapRectToRect(const SkBitmap&, const SkRect* src,
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000244 const SkRect& dst, const SkPaint* paint,
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +0000245 DrawBitmapRectFlags flags) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000246 virtual void drawBitmapMatrix(const SkBitmap&, const SkMatrix&,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000247 const SkPaint*) SK_OVERRIDE;
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000248 virtual void drawBitmapNine(const SkBitmap& bitmap, const SkIRect& center,
249 const SkRect& dst, const SkPaint* paint = NULL) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000250 virtual void drawSprite(const SkBitmap&, int left, int top,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000251 const SkPaint*) SK_OVERRIDE;
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000252 virtual void drawPicture(SkPicture& picture) 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:
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000270 virtual void willSave(SaveFlags) SK_OVERRIDE;
271 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;
commit-bot@chromium.orged9806f2014-02-21 02:32:36 +0000286
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000287 virtual void onClipRect(const SkRect&, SkRegion::Op, ClipEdgeStyle) SK_OVERRIDE;
288 virtual void onClipRRect(const SkRRect&, SkRegion::Op, ClipEdgeStyle) SK_OVERRIDE;
289 virtual void onClipPath(const SkPath&, SkRegion::Op, ClipEdgeStyle) SK_OVERRIDE;
290 virtual void onClipRegion(const SkRegion&, SkRegion::Op) SK_OVERRIDE;
291
reed@google.combb6992a2011-04-26 17:41:56 +0000292private:
commit-bot@chromium.orgd9ea09e2014-03-25 17:32:26 +0000293 void recordTranslate(const SkMatrix&);
294 void recordScale(const SkMatrix&);
295 void recordConcat(const SkMatrix&);
296
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000297 enum {
298 kNoSaveLayer = -1,
299 };
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000300 SkNamedFactorySet* fFactorySet;
301 int fFirstSaveLayerStackLevel;
scroggo@google.comd9d29672012-08-14 17:21:34 +0000302 SkBitmapHeap* fBitmapHeap;
reed@google.comacd471f2011-05-03 21:26:46 +0000303 SkGPipeController* fController;
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000304 SkWriter32& fWriter;
305 size_t fBlockSize; // amount allocated for writer
306 size_t fBytesNotified;
307 bool fDone;
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000308 const uint32_t fFlags;
reed@google.combb6992a2011-04-26 17:41:56 +0000309
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000310 SkRefCntSet fTypefaceSet;
reed@google.comf5842f72011-05-04 18:30:04 +0000311
312 uint32_t getTypefaceID(SkTypeface*);
313
reed@google.comacd471f2011-05-03 21:26:46 +0000314 inline void writeOp(DrawOps op, unsigned flags, unsigned data) {
reed@google.combb6992a2011-04-26 17:41:56 +0000315 fWriter.write32(DrawOp_packOpFlagData(op, flags, data));
316 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000317
reed@google.comacd471f2011-05-03 21:26:46 +0000318 inline void writeOp(DrawOps op) {
reed@google.combb6992a2011-04-26 17:41:56 +0000319 fWriter.write32(DrawOp_packOpFlagData(op, 0, 0));
320 }
reed@google.comacd471f2011-05-03 21:26:46 +0000321
322 bool needOpBytes(size_t size = 0);
323
324 inline void doNotify() {
325 if (!fDone) {
reed@google.com44699382013-10-31 17:28:30 +0000326 size_t bytes = fWriter.bytesWritten() - fBytesNotified;
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000327 if (bytes > 0) {
328 fController->notifyWritten(bytes);
329 fBytesNotified += bytes;
330 }
reed@google.comacd471f2011-05-03 21:26:46 +0000331 }
332 }
reed@google.comb55d1182011-05-11 00:42:04 +0000333
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000334 // Should be called after any calls to an SkFlatDictionary::findAndReplace
335 // if a new SkFlatData was added when in cross process mode
336 void flattenFactoryNames();
337
scroggo@google.com59c3ab62013-11-12 14:32:38 +0000338 FlattenableHeap fFlattenableHeap;
339 FlatDictionary fFlatDictionary;
340 SkAutoTUnref<BitmapShuttle> fBitmapShuttle;
341 int fCurrFlatIndex[kCount_PaintFlats];
342
reed@google.comb55d1182011-05-11 00:42:04 +0000343 int flattenToIndex(SkFlattenable* obj, PaintFlats);
344
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000345 // Common code used by drawBitmap*. Behaves differently depending on the
346 // type of SkBitmapHeap being used, which is determined by the flags used.
347 bool commonDrawBitmap(const SkBitmap& bm, DrawOps op, unsigned flags,
348 size_t opBytesNeeded, const SkPaint* paint);
scroggo@google.com58be6822012-07-30 14:40:01 +0000349
reed@google.com31891582011-05-12 03:03:56 +0000350 SkPaint fPaint;
351 void writePaint(const SkPaint&);
reed@google.combb6992a2011-04-26 17:41:56 +0000352
reed@google.comacd471f2011-05-03 21:26:46 +0000353 class AutoPipeNotify {
354 public:
355 AutoPipeNotify(SkGPipeCanvas* canvas) : fCanvas(canvas) {}
356 ~AutoPipeNotify() { fCanvas->doNotify(); }
357 private:
358 SkGPipeCanvas* fCanvas;
359 };
360 friend class AutoPipeNotify;
361
reed@google.combb6992a2011-04-26 17:41:56 +0000362 typedef SkCanvas INHERITED;
363};
364
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000365void SkGPipeCanvas::flattenFactoryNames() {
366 const char* name;
367 while ((name = fFactorySet->getNextAddedFactoryName()) != NULL) {
368 size_t len = strlen(name);
369 if (this->needOpBytes(len)) {
370 this->writeOp(kDef_Factory_DrawOp);
371 fWriter.writeString(name, len);
372 }
373 }
374}
375
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000376bool SkGPipeCanvas::shuttleBitmap(const SkBitmap& bm, int32_t slot) {
scroggo@google.com565254b2012-06-28 15:41:32 +0000377 SkASSERT(shouldFlattenBitmaps(fFlags));
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000378 SkWriteBuffer buffer;
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000379 buffer.setNamedFactoryRecorder(fFactorySet);
scroggo@google.com74b7ffd2013-04-30 02:32:41 +0000380 buffer.writeBitmap(bm);
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000381 this->flattenFactoryNames();
commit-bot@chromium.orgb45bd1f2014-04-24 18:17:30 +0000382 size_t size = buffer.bytesWritten();
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000383 if (this->needOpBytes(size)) {
384 this->writeOp(kDef_Bitmap_DrawOp, 0, slot);
385 void* dst = static_cast<void*>(fWriter.reserve(size));
386 buffer.writeToMemory(dst);
387 return true;
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000388 }
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000389 return false;
scroggo@google.comd3ba5cc2012-07-09 16:05:53 +0000390}
391
reed@google.comb55d1182011-05-11 00:42:04 +0000392// return 0 for NULL (or unflattenable obj), or index-base-1
scroggo@google.comd3ba5cc2012-07-09 16:05:53 +0000393// return ~(index-base-1) if an old flattenable was replaced
reed@google.comb55d1182011-05-11 00:42:04 +0000394int SkGPipeCanvas::flattenToIndex(SkFlattenable* obj, PaintFlats paintflat) {
scroggo@google.comd5d158b2012-08-14 20:38:28 +0000395 SkASSERT(!fDone && fBitmapHeap != NULL);
reed@google.comb55d1182011-05-11 00:42:04 +0000396 if (NULL == obj) {
397 return 0;
398 }
reed@google.comb55d1182011-05-11 00:42:04 +0000399
scroggo@google.comd9d29672012-08-14 17:21:34 +0000400 fBitmapHeap->deferAddingOwners();
scroggo@google.com4dffc592012-07-17 16:49:40 +0000401 bool added, replaced;
scroggo@google.com664fab12012-08-14 19:22:05 +0000402 const SkFlatData* flat = fFlatDictionary.findAndReplace(*obj, fFlattenableHeap.flatToReplace(),
403 &added, &replaced);
scroggo@google.comd9d29672012-08-14 17:21:34 +0000404 fBitmapHeap->endAddingOwnersDeferral(added);
scroggo@google.com4dffc592012-07-17 16:49:40 +0000405 int index = flat->index();
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000406 if (added) {
407 if (isCrossProcess(fFlags)) {
408 this->flattenFactoryNames();
409 }
410 size_t flatSize = flat->flatSize();
411 if (this->needOpBytes(flatSize)) {
412 this->writeOp(kDef_Flattenable_DrawOp, paintflat, index);
413 fWriter.write(flat->data(), flatSize);
414 }
scroggo@google.com4dffc592012-07-17 16:49:40 +0000415 }
416 if (replaced) {
reed@google.comb55d1182011-05-11 00:42:04 +0000417 index = ~index;
reed@google.comb55d1182011-05-11 00:42:04 +0000418 }
scroggo@google.com4dffc592012-07-17 16:49:40 +0000419 return index;
reed@google.comb55d1182011-05-11 00:42:04 +0000420}
421
reed@google.combb6992a2011-04-26 17:41:56 +0000422///////////////////////////////////////////////////////////////////////////////
423
reed@google.comacd471f2011-05-03 21:26:46 +0000424#define MIN_BLOCK_SIZE (16 * 1024)
scroggo@google.com4dffc592012-07-17 16:49:40 +0000425#define BITMAPS_TO_KEEP 5
426#define FLATTENABLES_TO_KEEP 10
reed@google.combb6992a2011-04-26 17:41:56 +0000427
reed@google.comacd471f2011-05-03 21:26:46 +0000428SkGPipeCanvas::SkGPipeCanvas(SkGPipeController* controller,
rmistry@google.comd6176b02012-08-23 18:14:13 +0000429 SkWriter32* writer, uint32_t flags,
junov@chromium.orga8db8fe2012-08-15 19:49:22 +0000430 uint32_t width, uint32_t height)
commit-bot@chromium.org403f8d72014-02-17 15:24:26 +0000431 : SkCanvas(width, height)
432 , fFactorySet(isCrossProcess(flags) ? SkNEW(SkNamedFactorySet) : NULL)
433 , fWriter(*writer)
434 , fFlags(flags)
435 , fFlattenableHeap(FLATTENABLES_TO_KEEP, fFactorySet, isCrossProcess(flags))
436 , fFlatDictionary(&fFlattenableHeap)
437{
reed@google.comacd471f2011-05-03 21:26:46 +0000438 fController = controller;
reed@google.combb6992a2011-04-26 17:41:56 +0000439 fDone = false;
reed@google.comacd471f2011-05-03 21:26:46 +0000440 fBlockSize = 0; // need first block from controller
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000441 fBytesNotified = 0;
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000442 fFirstSaveLayerStackLevel = kNoSaveLayer;
reed@google.comb55d1182011-05-11 00:42:04 +0000443 sk_bzero(fCurrFlatIndex, sizeof(fCurrFlatIndex));
reed@google.comacd471f2011-05-03 21:26:46 +0000444
scroggo@google.com565254b2012-06-28 15:41:32 +0000445 // Tell the reader the appropriate flags to use.
446 if (this->needOpBytes()) {
447 this->writeOp(kReportFlags_DrawOp, fFlags, 0);
448 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000449
scroggo@google.com10dccde2012-08-08 20:43:22 +0000450 if (shouldFlattenBitmaps(flags)) {
scroggo@google.com59c3ab62013-11-12 14:32:38 +0000451 fBitmapShuttle.reset(SkNEW_ARGS(BitmapShuttle, (this)));
452 fBitmapHeap = SkNEW_ARGS(SkBitmapHeap, (fBitmapShuttle.get(), BITMAPS_TO_KEEP));
scroggo@google.com10dccde2012-08-08 20:43:22 +0000453 } else {
scroggo@google.comd9d29672012-08-14 17:21:34 +0000454 fBitmapHeap = SkNEW_ARGS(SkBitmapHeap,
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000455 (BITMAPS_TO_KEEP, controller->numberOfReaders()));
scroggo@google.com10dccde2012-08-08 20:43:22 +0000456 if (this->needOpBytes(sizeof(void*))) {
scroggo@google.comd9d29672012-08-14 17:21:34 +0000457 this->writeOp(kShareBitmapHeap_DrawOp);
458 fWriter.writePtr(static_cast<void*>(fBitmapHeap));
scroggo@google.com10dccde2012-08-08 20:43:22 +0000459 }
460 }
scroggo@google.comd9d29672012-08-14 17:21:34 +0000461 fFlattenableHeap.setBitmapStorage(fBitmapHeap);
scroggo@google.com10dccde2012-08-08 20:43:22 +0000462 this->doNotify();
reed@google.combb6992a2011-04-26 17:41:56 +0000463}
464
465SkGPipeCanvas::~SkGPipeCanvas() {
scroggo@google.com59c3ab62013-11-12 14:32:38 +0000466 this->finish(true);
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000467 SkSafeUnref(fFactorySet);
scroggo@google.comd9d29672012-08-14 17:21:34 +0000468 SkSafeUnref(fBitmapHeap);
reed@google.combb6992a2011-04-26 17:41:56 +0000469}
470
reed@google.comacd471f2011-05-03 21:26:46 +0000471bool SkGPipeCanvas::needOpBytes(size_t needed) {
472 if (fDone) {
473 return false;
474 }
475
476 needed += 4; // size of DrawOp atom
reed@google.com44699382013-10-31 17:28:30 +0000477 if (fWriter.bytesWritten() + needed > fBlockSize) {
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000478 // Before we wipe out any data that has already been written, read it
479 // out.
480 this->doNotify();
commit-bot@chromium.orgb45bd1f2014-04-24 18:17:30 +0000481 size_t blockSize = SkTMax<size_t>(MIN_BLOCK_SIZE, needed);
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000482 void* block = fController->requestBlock(blockSize, &fBlockSize);
reed@google.comacd471f2011-05-03 21:26:46 +0000483 if (NULL == block) {
scroggo@google.com59c3ab62013-11-12 14:32:38 +0000484 // Do not notify the readers, which would call this function again.
485 this->finish(false);
reed@google.comacd471f2011-05-03 21:26:46 +0000486 return false;
487 }
scroggo@google.com460a23e2012-08-16 17:56:49 +0000488 SkASSERT(SkIsAlign4(fBlockSize));
reed@google.comacd471f2011-05-03 21:26:46 +0000489 fWriter.reset(block, fBlockSize);
490 fBytesNotified = 0;
491 }
492 return true;
493}
494
reed@google.comf5842f72011-05-04 18:30:04 +0000495uint32_t SkGPipeCanvas::getTypefaceID(SkTypeface* face) {
496 uint32_t id = 0; // 0 means default/null typeface
497 if (face) {
498 id = fTypefaceSet.find(face);
499 if (0 == id) {
500 id = fTypefaceSet.add(face);
501 size_t size = writeTypeface(NULL, face);
502 if (this->needOpBytes(size)) {
reed@google.combb6793b2011-05-05 15:18:15 +0000503 this->writeOp(kDef_Typeface_DrawOp);
reed@google.comf5842f72011-05-04 18:30:04 +0000504 writeTypeface(&fWriter, face);
505 }
506 }
507 }
508 return id;
509}
510
reed@google.combb6992a2011-04-26 17:41:56 +0000511///////////////////////////////////////////////////////////////////////////////
512
reed@google.comacd471f2011-05-03 21:26:46 +0000513#define NOTIFY_SETUP(canvas) \
514 AutoPipeNotify apn(canvas)
515
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000516void SkGPipeCanvas::willSave(SaveFlags flags) {
reed@google.comacd471f2011-05-03 21:26:46 +0000517 NOTIFY_SETUP(this);
518 if (this->needOpBytes()) {
519 this->writeOp(kSave_DrawOp, 0, flags);
520 }
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000521
522 this->INHERITED::willSave(flags);
reed@google.combb6992a2011-04-26 17:41:56 +0000523}
524
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000525SkCanvas::SaveLayerStrategy SkGPipeCanvas::willSaveLayer(const SkRect* bounds, const SkPaint* paint,
526 SaveFlags saveFlags) {
reed@google.comacd471f2011-05-03 21:26:46 +0000527 NOTIFY_SETUP(this);
528 size_t size = 0;
reed@google.combb6992a2011-04-26 17:41:56 +0000529 unsigned opFlags = 0;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000530
reed@google.combb6992a2011-04-26 17:41:56 +0000531 if (bounds) {
532 opFlags |= kSaveLayer_HasBounds_DrawOpFlag;
reed@google.comacd471f2011-05-03 21:26:46 +0000533 size += sizeof(SkRect);
reed@google.combb6992a2011-04-26 17:41:56 +0000534 }
535 if (paint) {
536 opFlags |= kSaveLayer_HasPaint_DrawOpFlag;
reed@google.com31891582011-05-12 03:03:56 +0000537 this->writePaint(*paint);
reed@google.combb6992a2011-04-26 17:41:56 +0000538 }
539
reed@google.comacd471f2011-05-03 21:26:46 +0000540 if (this->needOpBytes(size)) {
541 this->writeOp(kSaveLayer_DrawOp, opFlags, saveFlags);
542 if (bounds) {
543 fWriter.writeRect(*bounds);
544 }
reed@google.combb6992a2011-04-26 17:41:56 +0000545 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000546
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000547 if (kNoSaveLayer == fFirstSaveLayerStackLevel){
548 fFirstSaveLayerStackLevel = this->getSaveCount();
549 }
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000550
551 this->INHERITED::willSaveLayer(bounds, paint, saveFlags);
552 // we don't create a layer
553 return kNoLayer_SaveLayerStrategy;
reed@google.combb6992a2011-04-26 17:41:56 +0000554}
555
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000556void SkGPipeCanvas::willRestore() {
reed@google.comacd471f2011-05-03 21:26:46 +0000557 NOTIFY_SETUP(this);
558 if (this->needOpBytes()) {
559 this->writeOp(kRestore_DrawOp);
560 }
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000561
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000562 if (this->getSaveCount() - 1 == fFirstSaveLayerStackLevel){
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000563 fFirstSaveLayerStackLevel = kNoSaveLayer;
564 }
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000565
566 this->INHERITED::willRestore();
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000567}
568
569bool SkGPipeCanvas::isDrawingToLayer() const {
570 return kNoSaveLayer != fFirstSaveLayerStackLevel;
reed@google.combb6992a2011-04-26 17:41:56 +0000571}
572
commit-bot@chromium.orgd9ea09e2014-03-25 17:32:26 +0000573void SkGPipeCanvas::recordTranslate(const SkMatrix& m) {
574 if (this->needOpBytes(2 * sizeof(SkScalar))) {
575 this->writeOp(kTranslate_DrawOp);
576 fWriter.writeScalar(m.getTranslateX());
577 fWriter.writeScalar(m.getTranslateY());
reed@google.combb6992a2011-04-26 17:41:56 +0000578 }
reed@google.combb6992a2011-04-26 17:41:56 +0000579}
580
commit-bot@chromium.orgd9ea09e2014-03-25 17:32:26 +0000581void SkGPipeCanvas::recordScale(const SkMatrix& m) {
582 if (this->needOpBytes(2 * sizeof(SkScalar))) {
583 this->writeOp(kScale_DrawOp);
584 fWriter.writeScalar(m.getScaleX());
585 fWriter.writeScalar(m.getScaleY());
reed@google.combb6992a2011-04-26 17:41:56 +0000586 }
reed@google.combb6992a2011-04-26 17:41:56 +0000587}
588
commit-bot@chromium.orgd9ea09e2014-03-25 17:32:26 +0000589void SkGPipeCanvas::recordConcat(const SkMatrix& m) {
590 if (this->needOpBytes(m.writeToMemory(NULL))) {
591 this->writeOp(kConcat_DrawOp);
592 fWriter.writeMatrix(m);
reed@google.combb6992a2011-04-26 17:41:56 +0000593 }
reed@google.combb6992a2011-04-26 17:41:56 +0000594}
595
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +0000596void SkGPipeCanvas::didConcat(const SkMatrix& matrix) {
reed@google.combb6992a2011-04-26 17:41:56 +0000597 if (!matrix.isIdentity()) {
reed@google.comacd471f2011-05-03 21:26:46 +0000598 NOTIFY_SETUP(this);
commit-bot@chromium.orgd9ea09e2014-03-25 17:32:26 +0000599 switch (matrix.getType()) {
600 case SkMatrix::kTranslate_Mask:
601 this->recordTranslate(matrix);
602 break;
603 case SkMatrix::kScale_Mask:
604 this->recordScale(matrix);
605 break;
606 default:
607 this->recordConcat(matrix);
608 break;
reed@google.comacd471f2011-05-03 21:26:46 +0000609 }
reed@google.combb6992a2011-04-26 17:41:56 +0000610 }
commit-bot@chromium.orgd9ea09e2014-03-25 17:32:26 +0000611
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +0000612 this->INHERITED::didConcat(matrix);
reed@google.combb6992a2011-04-26 17:41:56 +0000613}
614
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +0000615void SkGPipeCanvas::didSetMatrix(const SkMatrix& matrix) {
reed@google.comacd471f2011-05-03 21:26:46 +0000616 NOTIFY_SETUP(this);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000617 if (this->needOpBytes(matrix.writeToMemory(NULL))) {
reed@google.comacd471f2011-05-03 21:26:46 +0000618 this->writeOp(kSetMatrix_DrawOp);
djsollen@google.com2b2ede32012-04-12 13:24:04 +0000619 fWriter.writeMatrix(matrix);
reed@google.comacd471f2011-05-03 21:26:46 +0000620 }
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +0000621 this->INHERITED::didSetMatrix(matrix);
reed@google.combb6992a2011-04-26 17:41:56 +0000622}
623
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000624void SkGPipeCanvas::onClipRect(const SkRect& rect, SkRegion::Op rgnOp,
625 ClipEdgeStyle edgeStyle) {
reed@google.comacd471f2011-05-03 21:26:46 +0000626 NOTIFY_SETUP(this);
scroggo@google.com460a23e2012-08-16 17:56:49 +0000627 if (this->needOpBytes(sizeof(SkRect))) {
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000628 unsigned flags = 0;
629 if (kSoft_ClipEdgeStyle == edgeStyle) {
630 flags = kClip_HasAntiAlias_DrawOpFlag;
631 }
scroggo@google.com460a23e2012-08-16 17:56:49 +0000632 this->writeOp(kClipRect_DrawOp, flags, rgnOp);
reed@google.comacd471f2011-05-03 21:26:46 +0000633 fWriter.writeRect(rect);
634 }
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000635 this->INHERITED::onClipRect(rect, rgnOp, edgeStyle);
reed@google.combb6992a2011-04-26 17:41:56 +0000636}
637
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000638void SkGPipeCanvas::onClipRRect(const SkRRect& rrect, SkRegion::Op rgnOp,
639 ClipEdgeStyle edgeStyle) {
reed@google.com4ed0fb72012-12-12 20:48:18 +0000640 NOTIFY_SETUP(this);
641 if (this->needOpBytes(kSizeOfFlatRRect)) {
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000642 unsigned flags = 0;
643 if (kSoft_ClipEdgeStyle == edgeStyle) {
644 flags = kClip_HasAntiAlias_DrawOpFlag;
645 }
reed@google.com4ed0fb72012-12-12 20:48:18 +0000646 this->writeOp(kClipRRect_DrawOp, flags, rgnOp);
647 fWriter.writeRRect(rrect);
648 }
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000649 this->INHERITED::onClipRRect(rrect, rgnOp, edgeStyle);
reed@google.com4ed0fb72012-12-12 20:48:18 +0000650}
651
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000652void SkGPipeCanvas::onClipPath(const SkPath& path, SkRegion::Op rgnOp,
653 ClipEdgeStyle edgeStyle) {
reed@google.comacd471f2011-05-03 21:26:46 +0000654 NOTIFY_SETUP(this);
scroggo@google.com460a23e2012-08-16 17:56:49 +0000655 if (this->needOpBytes(path.writeToMemory(NULL))) {
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000656 unsigned flags = 0;
657 if (kSoft_ClipEdgeStyle == edgeStyle) {
658 flags = kClip_HasAntiAlias_DrawOpFlag;
659 }
scroggo@google.com460a23e2012-08-16 17:56:49 +0000660 this->writeOp(kClipPath_DrawOp, flags, rgnOp);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000661 fWriter.writePath(path);
reed@google.comacd471f2011-05-03 21:26:46 +0000662 }
reed@google.combb6992a2011-04-26 17:41:56 +0000663 // we just pass on the bounds of the path
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000664 this->INHERITED::onClipRect(path.getBounds(), rgnOp, edgeStyle);
reed@google.combb6992a2011-04-26 17:41:56 +0000665}
666
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000667void SkGPipeCanvas::onClipRegion(const SkRegion& region, SkRegion::Op rgnOp) {
reed@google.comacd471f2011-05-03 21:26:46 +0000668 NOTIFY_SETUP(this);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000669 if (this->needOpBytes(region.writeToMemory(NULL))) {
reed@google.comacd471f2011-05-03 21:26:46 +0000670 this->writeOp(kClipRegion_DrawOp, 0, rgnOp);
djsollen@google.com2b2ede32012-04-12 13:24:04 +0000671 fWriter.writeRegion(region);
reed@google.comacd471f2011-05-03 21:26:46 +0000672 }
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000673 this->INHERITED::onClipRegion(region, rgnOp);
reed@google.combb6992a2011-04-26 17:41:56 +0000674}
675
676///////////////////////////////////////////////////////////////////////////////
677
678void SkGPipeCanvas::clear(SkColor color) {
reed@google.comacd471f2011-05-03 21:26:46 +0000679 NOTIFY_SETUP(this);
reed@google.combb6992a2011-04-26 17:41:56 +0000680 unsigned flags = 0;
681 if (color) {
682 flags |= kClear_HasColor_DrawOpFlag;
683 }
reed@google.comacd471f2011-05-03 21:26:46 +0000684 if (this->needOpBytes(sizeof(SkColor))) {
685 this->writeOp(kDrawClear_DrawOp, flags, 0);
686 if (color) {
687 fWriter.write32(color);
688 }
reed@google.combb6992a2011-04-26 17:41:56 +0000689 }
690}
691
692void SkGPipeCanvas::drawPaint(const SkPaint& paint) {
reed@google.comacd471f2011-05-03 21:26:46 +0000693 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000694 this->writePaint(paint);
reed@google.comacd471f2011-05-03 21:26:46 +0000695 if (this->needOpBytes()) {
reed@google.com31891582011-05-12 03:03:56 +0000696 this->writeOp(kDrawPaint_DrawOp);
reed@google.comacd471f2011-05-03 21:26:46 +0000697 }
reed@google.combb6992a2011-04-26 17:41:56 +0000698}
699
700void SkGPipeCanvas::drawPoints(PointMode mode, size_t count,
robertphillips@google.com8b169312013-10-15 17:47:36 +0000701 const SkPoint pts[], const SkPaint& paint) {
reed@google.combb6992a2011-04-26 17:41:56 +0000702 if (count) {
reed@google.comacd471f2011-05-03 21:26:46 +0000703 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000704 this->writePaint(paint);
reed@google.comacd471f2011-05-03 21:26:46 +0000705 if (this->needOpBytes(4 + count * sizeof(SkPoint))) {
reed@google.com31891582011-05-12 03:03:56 +0000706 this->writeOp(kDrawPoints_DrawOp, mode, 0);
commit-bot@chromium.orgb45bd1f2014-04-24 18:17:30 +0000707 fWriter.write32(SkToU32(count));
reed@google.comacd471f2011-05-03 21:26:46 +0000708 fWriter.write(pts, count * sizeof(SkPoint));
709 }
reed@google.combb6992a2011-04-26 17:41:56 +0000710 }
711}
712
reed@google.com4ed0fb72012-12-12 20:48:18 +0000713void SkGPipeCanvas::drawOval(const SkRect& rect, const SkPaint& paint) {
714 NOTIFY_SETUP(this);
715 this->writePaint(paint);
716 if (this->needOpBytes(sizeof(SkRect))) {
717 this->writeOp(kDrawOval_DrawOp);
718 fWriter.writeRect(rect);
719 }
720}
721
bsalomon@google.com7ce564c2013-10-22 16:54:15 +0000722void SkGPipeCanvas::drawRect(const SkRect& rect, const SkPaint& paint) {
reed@google.comacd471f2011-05-03 21:26:46 +0000723 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000724 this->writePaint(paint);
reed@google.comacd471f2011-05-03 21:26:46 +0000725 if (this->needOpBytes(sizeof(SkRect))) {
reed@google.com31891582011-05-12 03:03:56 +0000726 this->writeOp(kDrawRect_DrawOp);
reed@google.comacd471f2011-05-03 21:26:46 +0000727 fWriter.writeRect(rect);
728 }
reed@google.combb6992a2011-04-26 17:41:56 +0000729}
730
reed@google.com4ed0fb72012-12-12 20:48:18 +0000731void SkGPipeCanvas::drawRRect(const SkRRect& rrect, const SkPaint& paint) {
732 NOTIFY_SETUP(this);
733 this->writePaint(paint);
734 if (this->needOpBytes(kSizeOfFlatRRect)) {
735 this->writeOp(kDrawRRect_DrawOp);
736 fWriter.writeRRect(rrect);
737 }
738}
739
commit-bot@chromium.orged9806f2014-02-21 02:32:36 +0000740void SkGPipeCanvas::onDrawDRRect(const SkRRect& outer, const SkRRect& inner,
741 const SkPaint& paint) {
742 NOTIFY_SETUP(this);
743 this->writePaint(paint);
744 if (this->needOpBytes(kSizeOfFlatRRect * 2)) {
745 this->writeOp(kDrawDRRect_DrawOp);
746 fWriter.writeRRect(outer);
747 fWriter.writeRRect(inner);
748 }
749}
750
bsalomon@google.com7ce564c2013-10-22 16:54:15 +0000751void SkGPipeCanvas::drawPath(const SkPath& path, const SkPaint& paint) {
reed@google.comacd471f2011-05-03 21:26:46 +0000752 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000753 this->writePaint(paint);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000754 if (this->needOpBytes(path.writeToMemory(NULL))) {
reed@google.com31891582011-05-12 03:03:56 +0000755 this->writeOp(kDrawPath_DrawOp);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000756 fWriter.writePath(path);
reed@google.comacd471f2011-05-03 21:26:46 +0000757 }
reed@google.combb6992a2011-04-26 17:41:56 +0000758}
759
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000760bool SkGPipeCanvas::commonDrawBitmap(const SkBitmap& bm, DrawOps op,
761 unsigned flags,
762 size_t opBytesNeeded,
763 const SkPaint* paint) {
scroggo@google.com58be6822012-07-30 14:40:01 +0000764 if (paint != NULL) {
scroggo@google.com460a23e2012-08-16 17:56:49 +0000765 flags |= kDrawBitmap_HasPaint_DrawOpFlag;
scroggo@google.com58be6822012-07-30 14:40:01 +0000766 this->writePaint(*paint);
767 }
scroggo@google.com10dccde2012-08-08 20:43:22 +0000768 if (this->needOpBytes(opBytesNeeded)) {
scroggo@google.comd5d158b2012-08-14 20:38:28 +0000769 SkASSERT(fBitmapHeap != NULL);
770 int32_t bitmapIndex = fBitmapHeap->insert(bm);
771 if (SkBitmapHeap::INVALID_SLOT == bitmapIndex) {
772 return false;
773 }
scroggo@google.com10dccde2012-08-08 20:43:22 +0000774 this->writeOp(op, flags, bitmapIndex);
scroggo@google.com58be6822012-07-30 14:40:01 +0000775 return true;
776 }
777 return false;
778}
779
780void SkGPipeCanvas::drawBitmap(const SkBitmap& bm, SkScalar left, SkScalar top,
781 const SkPaint* paint) {
782 NOTIFY_SETUP(this);
783 size_t opBytesNeeded = sizeof(SkScalar) * 2;
784
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000785 if (this->commonDrawBitmap(bm, kDrawBitmap_DrawOp, 0, opBytesNeeded, paint)) {
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000786 fWriter.writeScalar(left);
787 fWriter.writeScalar(top);
788 }
reed@google.combb6992a2011-04-26 17:41:56 +0000789}
790
reed@google.com71121732012-09-18 15:14:33 +0000791void SkGPipeCanvas::drawBitmapRectToRect(const SkBitmap& bm, const SkRect* src,
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +0000792 const SkRect& dst, const SkPaint* paint,
793 DrawBitmapRectFlags dbmrFlags) {
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000794 NOTIFY_SETUP(this);
scroggo@google.com58be6822012-07-30 14:40:01 +0000795 size_t opBytesNeeded = sizeof(SkRect);
796 bool hasSrc = src != NULL;
797 unsigned flags;
798 if (hasSrc) {
scroggo@google.com460a23e2012-08-16 17:56:49 +0000799 flags = kDrawBitmap_HasSrcRect_DrawOpFlag;
scroggo@google.com58be6822012-07-30 14:40:01 +0000800 opBytesNeeded += sizeof(int32_t) * 4;
801 } else {
802 flags = 0;
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000803 }
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +0000804 if (dbmrFlags & kBleed_DrawBitmapRectFlag) {
805 flags |= kDrawBitmap_Bleed_DrawOpFlag;
806 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000807
reed@google.com71121732012-09-18 15:14:33 +0000808 if (this->commonDrawBitmap(bm, kDrawBitmapRectToRect_DrawOp, flags, opBytesNeeded, paint)) {
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000809 if (hasSrc) {
reed@google.com71121732012-09-18 15:14:33 +0000810 fWriter.writeRect(*src);
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000811 }
812 fWriter.writeRect(dst);
813 }
reed@google.combb6992a2011-04-26 17:41:56 +0000814}
815
scroggo@google.com58be6822012-07-30 14:40:01 +0000816void SkGPipeCanvas::drawBitmapMatrix(const SkBitmap& bm, const SkMatrix& matrix,
817 const SkPaint* paint) {
818 NOTIFY_SETUP(this);
819 size_t opBytesNeeded = matrix.writeToMemory(NULL);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000820
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000821 if (this->commonDrawBitmap(bm, kDrawBitmapMatrix_DrawOp, 0, opBytesNeeded, paint)) {
scroggo@google.com58be6822012-07-30 14:40:01 +0000822 fWriter.writeMatrix(matrix);
823 }
reed@google.combb6992a2011-04-26 17:41:56 +0000824}
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000825
826void SkGPipeCanvas::drawBitmapNine(const SkBitmap& bm, const SkIRect& center,
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000827 const SkRect& dst, const SkPaint* paint) {
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000828 NOTIFY_SETUP(this);
scroggo@google.com58be6822012-07-30 14:40:01 +0000829 size_t opBytesNeeded = sizeof(int32_t) * 4 + sizeof(SkRect);
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000830
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000831 if (this->commonDrawBitmap(bm, kDrawBitmapNine_DrawOp, 0, opBytesNeeded, paint)) {
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000832 fWriter.write32(center.fLeft);
833 fWriter.write32(center.fTop);
834 fWriter.write32(center.fRight);
835 fWriter.write32(center.fBottom);
836 fWriter.writeRect(dst);
837 }
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000838}
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000839
840void SkGPipeCanvas::drawSprite(const SkBitmap& bm, int left, int top,
841 const SkPaint* paint) {
842 NOTIFY_SETUP(this);
scroggo@google.com58be6822012-07-30 14:40:01 +0000843 size_t opBytesNeeded = sizeof(int32_t) * 2;
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000844
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000845 if (this->commonDrawBitmap(bm, kDrawSprite_DrawOp, 0, opBytesNeeded, paint)) {
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000846 fWriter.write32(left);
847 fWriter.write32(top);
848 }
reed@google.combb6992a2011-04-26 17:41:56 +0000849}
850
reed@google.come0d9ce82014-04-23 04:00:17 +0000851void SkGPipeCanvas::onDrawText(const void* text, size_t byteLength, SkScalar x, SkScalar y,
852 const SkPaint& paint) {
reed@google.combb6992a2011-04-26 17:41:56 +0000853 if (byteLength) {
reed@google.comacd471f2011-05-03 21:26:46 +0000854 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000855 this->writePaint(paint);
reed@google.comacd471f2011-05-03 21:26:46 +0000856 if (this->needOpBytes(4 + SkAlign4(byteLength) + 2 * sizeof(SkScalar))) {
reed@google.com31891582011-05-12 03:03:56 +0000857 this->writeOp(kDrawText_DrawOp);
commit-bot@chromium.orgb45bd1f2014-04-24 18:17:30 +0000858 fWriter.write32(SkToU32(byteLength));
reed@google.comacd471f2011-05-03 21:26:46 +0000859 fWriter.writePad(text, byteLength);
860 fWriter.writeScalar(x);
861 fWriter.writeScalar(y);
862 }
reed@google.combb6992a2011-04-26 17:41:56 +0000863 }
864}
865
reed@google.come0d9ce82014-04-23 04:00:17 +0000866void SkGPipeCanvas::onDrawPosText(const void* text, size_t byteLength, const SkPoint pos[],
867 const SkPaint& paint) {
reed@google.combb6992a2011-04-26 17:41:56 +0000868 if (byteLength) {
reed@google.comacd471f2011-05-03 21:26:46 +0000869 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000870 this->writePaint(paint);
reed@google.combb6992a2011-04-26 17:41:56 +0000871 int count = paint.textToGlyphs(text, byteLength, NULL);
reed@google.comacd471f2011-05-03 21:26:46 +0000872 if (this->needOpBytes(4 + SkAlign4(byteLength) + 4 + count * sizeof(SkPoint))) {
reed@google.com31891582011-05-12 03:03:56 +0000873 this->writeOp(kDrawPosText_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.write32(count);
877 fWriter.write(pos, count * sizeof(SkPoint));
878 }
reed@google.combb6992a2011-04-26 17:41:56 +0000879 }
880}
881
reed@google.come0d9ce82014-04-23 04:00:17 +0000882void SkGPipeCanvas::onDrawPosTextH(const void* text, size_t byteLength, const SkScalar xpos[],
883 SkScalar constY, const SkPaint& paint) {
reed@google.combb6992a2011-04-26 17:41:56 +0000884 if (byteLength) {
reed@google.comacd471f2011-05-03 21:26:46 +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(SkScalar) + 4)) {
reed@google.com31891582011-05-12 03:03:56 +0000889 this->writeOp(kDrawPosTextH_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(xpos, count * sizeof(SkScalar));
894 fWriter.writeScalar(constY);
895 }
reed@google.combb6992a2011-04-26 17:41:56 +0000896 }
897}
898
reed@google.come0d9ce82014-04-23 04:00:17 +0000899void SkGPipeCanvas::onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& path,
900 const SkMatrix* matrix, const SkPaint& paint) {
reed@google.combb6992a2011-04-26 17:41:56 +0000901 if (byteLength) {
reed@google.comacd471f2011-05-03 21:26:46 +0000902 NOTIFY_SETUP(this);
reed@google.combb6992a2011-04-26 17:41:56 +0000903 unsigned flags = 0;
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000904 size_t size = 4 + SkAlign4(byteLength) + path.writeToMemory(NULL);
reed@google.combb6992a2011-04-26 17:41:56 +0000905 if (matrix) {
906 flags |= kDrawTextOnPath_HasMatrix_DrawOpFlag;
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000907 size += matrix->writeToMemory(NULL);
reed@google.combb6992a2011-04-26 17:41:56 +0000908 }
reed@google.com31891582011-05-12 03:03:56 +0000909 this->writePaint(paint);
reed@google.comacd471f2011-05-03 21:26:46 +0000910 if (this->needOpBytes(size)) {
reed@google.com31891582011-05-12 03:03:56 +0000911 this->writeOp(kDrawTextOnPath_DrawOp, flags, 0);
reed@google.combb6992a2011-04-26 17:41:56 +0000912
commit-bot@chromium.orgb45bd1f2014-04-24 18:17:30 +0000913 fWriter.write32(SkToU32(byteLength));
reed@google.comacd471f2011-05-03 21:26:46 +0000914 fWriter.writePad(text, byteLength);
reed@google.combb6992a2011-04-26 17:41:56 +0000915
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000916 fWriter.writePath(path);
reed@google.comacd471f2011-05-03 21:26:46 +0000917 if (matrix) {
djsollen@google.com2b2ede32012-04-12 13:24:04 +0000918 fWriter.writeMatrix(*matrix);
reed@google.comacd471f2011-05-03 21:26:46 +0000919 }
reed@google.combb6992a2011-04-26 17:41:56 +0000920 }
921 }
922}
923
924void SkGPipeCanvas::drawPicture(SkPicture& picture) {
reed@google.com0faac1e2011-05-11 05:58:58 +0000925 // we want to playback the picture into individual draw calls
926 this->INHERITED::drawPicture(picture);
reed@google.combb6992a2011-04-26 17:41:56 +0000927}
928
reed@google.com85e143c2013-12-30 15:51:25 +0000929void SkGPipeCanvas::drawVertices(VertexMode vmode, int vertexCount,
reed@google.combb6992a2011-04-26 17:41:56 +0000930 const SkPoint vertices[], const SkPoint texs[],
reed@google.com85e143c2013-12-30 15:51:25 +0000931 const SkColor colors[], SkXfermode* xfer,
reed@google.combb6992a2011-04-26 17:41:56 +0000932 const uint16_t indices[], int indexCount,
933 const SkPaint& paint) {
934 if (0 == vertexCount) {
935 return;
936 }
937
reed@google.comacd471f2011-05-03 21:26:46 +0000938 NOTIFY_SETUP(this);
939 size_t size = 4 + vertexCount * sizeof(SkPoint);
reed@google.com31891582011-05-12 03:03:56 +0000940 this->writePaint(paint);
reed@google.combb6992a2011-04-26 17:41:56 +0000941 unsigned flags = 0;
942 if (texs) {
943 flags |= kDrawVertices_HasTexs_DrawOpFlag;
reed@google.comacd471f2011-05-03 21:26:46 +0000944 size += vertexCount * sizeof(SkPoint);
reed@google.combb6992a2011-04-26 17:41:56 +0000945 }
946 if (colors) {
947 flags |= kDrawVertices_HasColors_DrawOpFlag;
reed@google.comacd471f2011-05-03 21:26:46 +0000948 size += vertexCount * sizeof(SkColor);
reed@google.combb6992a2011-04-26 17:41:56 +0000949 }
950 if (indices && indexCount > 0) {
951 flags |= kDrawVertices_HasIndices_DrawOpFlag;
reed@google.comacd471f2011-05-03 21:26:46 +0000952 size += 4 + SkAlign4(indexCount * sizeof(uint16_t));
reed@google.combb6992a2011-04-26 17:41:56 +0000953 }
reed@google.com85e143c2013-12-30 15:51:25 +0000954 if (xfer && !SkXfermode::IsMode(xfer, SkXfermode::kModulate_Mode)) {
955 flags |= kDrawVertices_HasXfermode_DrawOpFlag;
956 size += sizeof(int32_t); // mode enum
957 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000958
reed@google.comacd471f2011-05-03 21:26:46 +0000959 if (this->needOpBytes(size)) {
reed@google.com31891582011-05-12 03:03:56 +0000960 this->writeOp(kDrawVertices_DrawOp, flags, 0);
reed@google.com85e143c2013-12-30 15:51:25 +0000961 fWriter.write32(vmode);
reed@google.comacd471f2011-05-03 21:26:46 +0000962 fWriter.write32(vertexCount);
963 fWriter.write(vertices, vertexCount * sizeof(SkPoint));
964 if (texs) {
965 fWriter.write(texs, vertexCount * sizeof(SkPoint));
966 }
967 if (colors) {
968 fWriter.write(colors, vertexCount * sizeof(SkColor));
969 }
reed@google.com85e143c2013-12-30 15:51:25 +0000970 if (flags & kDrawVertices_HasXfermode_DrawOpFlag) {
971 SkXfermode::Mode mode = SkXfermode::kModulate_Mode;
972 (void)xfer->asMode(&mode);
973 fWriter.write32(mode);
974 }
reed@google.comacd471f2011-05-03 21:26:46 +0000975 if (indices && indexCount > 0) {
976 fWriter.write32(indexCount);
977 fWriter.writePad(indices, indexCount * sizeof(uint16_t));
978 }
reed@google.combb6992a2011-04-26 17:41:56 +0000979 }
980}
981
reed@google.comacd471f2011-05-03 21:26:46 +0000982void SkGPipeCanvas::drawData(const void* ptr, size_t size) {
983 if (size && ptr) {
984 NOTIFY_SETUP(this);
reed@google.combb6992a2011-04-26 17:41:56 +0000985 unsigned data = 0;
986 if (size < (1 << DRAWOPS_DATA_BITS)) {
987 data = (unsigned)size;
988 }
reed@google.comacd471f2011-05-03 21:26:46 +0000989 if (this->needOpBytes(4 + SkAlign4(size))) {
990 this->writeOp(kDrawData_DrawOp, 0, data);
991 if (0 == data) {
commit-bot@chromium.orgb45bd1f2014-04-24 18:17:30 +0000992 fWriter.write32(SkToU32(size));
reed@google.comacd471f2011-05-03 21:26:46 +0000993 }
reed@google.combb6793b2011-05-05 15:18:15 +0000994 fWriter.writePad(ptr, size);
reed@google.combb6992a2011-04-26 17:41:56 +0000995 }
996 }
997}
998
robertphillips@google.com0a4805e2013-05-29 13:24:23 +0000999void SkGPipeCanvas::beginCommentGroup(const char* description) {
1000 // ignore for now
1001}
1002
1003void SkGPipeCanvas::addComment(const char* kywd, const char* value) {
1004 // ignore for now
1005}
1006
1007void SkGPipeCanvas::endCommentGroup() {
1008 // ignore for now
1009}
1010
junov@chromium.org77eec242012-07-18 17:54:45 +00001011void SkGPipeCanvas::flushRecording(bool detachCurrentBlock) {
1012 doNotify();
1013 if (detachCurrentBlock) {
1014 // force a new block to be requested for the next recorded command
rmistry@google.comd6176b02012-08-23 18:14:13 +00001015 fBlockSize = 0;
junov@chromium.org77eec242012-07-18 17:54:45 +00001016 }
1017}
1018
junov@chromium.org2e14ba82012-08-07 14:26:57 +00001019size_t SkGPipeCanvas::freeMemoryIfPossible(size_t bytesToFree) {
scroggo@google.comd5d158b2012-08-14 20:38:28 +00001020 return (NULL == fBitmapHeap) ? 0 : fBitmapHeap->freeMemoryIfPossible(bytesToFree);
junov@chromium.org2e14ba82012-08-07 14:26:57 +00001021}
1022
reed@google.combb6992a2011-04-26 17:41:56 +00001023///////////////////////////////////////////////////////////////////////////////
1024
1025template <typename T> uint32_t castToU32(T value) {
1026 union {
1027 T fSrc;
1028 uint32_t fDst;
1029 } data;
1030 data.fSrc = value;
1031 return data.fDst;
1032}
1033
reed@google.com31891582011-05-12 03:03:56 +00001034void SkGPipeCanvas::writePaint(const SkPaint& paint) {
scroggo@google.comd5d158b2012-08-14 20:38:28 +00001035 if (fDone) {
1036 return;
1037 }
reed@google.com31891582011-05-12 03:03:56 +00001038 SkPaint& base = fPaint;
reed@google.combb6992a2011-04-26 17:41:56 +00001039 uint32_t storage[32];
1040 uint32_t* ptr = storage;
reed@google.combb6992a2011-04-26 17:41:56 +00001041
1042 if (base.getFlags() != paint.getFlags()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001043 *ptr++ = PaintOp_packOpData(kFlags_PaintOp, paint.getFlags());
reed@google.comf5842f72011-05-04 18:30:04 +00001044 base.setFlags(paint.getFlags());
reed@google.combb6992a2011-04-26 17:41:56 +00001045 }
1046 if (base.getColor() != paint.getColor()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001047 *ptr++ = PaintOp_packOp(kColor_PaintOp);
1048 *ptr++ = paint.getColor();
reed@google.comf5842f72011-05-04 18:30:04 +00001049 base.setColor(paint.getColor());
reed@google.combb6992a2011-04-26 17:41:56 +00001050 }
commit-bot@chromium.org85faf502014-04-16 12:58:02 +00001051 if (base.getFilterLevel() != paint.getFilterLevel()) {
1052 *ptr++ = PaintOp_packOpData(kFilterLevel_PaintOp, paint.getFilterLevel());
1053 base.setFilterLevel(paint.getFilterLevel());
1054 }
reed@google.combb6992a2011-04-26 17:41:56 +00001055 if (base.getStyle() != paint.getStyle()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001056 *ptr++ = PaintOp_packOpData(kStyle_PaintOp, paint.getStyle());
reed@google.comf5842f72011-05-04 18:30:04 +00001057 base.setStyle(paint.getStyle());
reed@google.combb6992a2011-04-26 17:41:56 +00001058 }
1059 if (base.getStrokeJoin() != paint.getStrokeJoin()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001060 *ptr++ = PaintOp_packOpData(kJoin_PaintOp, paint.getStrokeJoin());
reed@google.comf5842f72011-05-04 18:30:04 +00001061 base.setStrokeJoin(paint.getStrokeJoin());
reed@google.combb6992a2011-04-26 17:41:56 +00001062 }
1063 if (base.getStrokeCap() != paint.getStrokeCap()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001064 *ptr++ = PaintOp_packOpData(kCap_PaintOp, paint.getStrokeCap());
reed@google.comf5842f72011-05-04 18:30:04 +00001065 base.setStrokeCap(paint.getStrokeCap());
reed@google.combb6992a2011-04-26 17:41:56 +00001066 }
1067 if (base.getStrokeWidth() != paint.getStrokeWidth()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001068 *ptr++ = PaintOp_packOp(kWidth_PaintOp);
1069 *ptr++ = castToU32(paint.getStrokeWidth());
reed@google.comf5842f72011-05-04 18:30:04 +00001070 base.setStrokeWidth(paint.getStrokeWidth());
reed@google.combb6992a2011-04-26 17:41:56 +00001071 }
1072 if (base.getStrokeMiter() != paint.getStrokeMiter()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001073 *ptr++ = PaintOp_packOp(kMiter_PaintOp);
1074 *ptr++ = castToU32(paint.getStrokeMiter());
reed@google.comf5842f72011-05-04 18:30:04 +00001075 base.setStrokeMiter(paint.getStrokeMiter());
reed@google.combb6992a2011-04-26 17:41:56 +00001076 }
1077 if (base.getTextEncoding() != paint.getTextEncoding()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001078 *ptr++ = PaintOp_packOpData(kEncoding_PaintOp, paint.getTextEncoding());
reed@google.comf5842f72011-05-04 18:30:04 +00001079 base.setTextEncoding(paint.getTextEncoding());
reed@google.combb6992a2011-04-26 17:41:56 +00001080 }
1081 if (base.getHinting() != paint.getHinting()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001082 *ptr++ = PaintOp_packOpData(kHinting_PaintOp, paint.getHinting());
reed@google.comf5842f72011-05-04 18:30:04 +00001083 base.setHinting(paint.getHinting());
reed@google.combb6992a2011-04-26 17:41:56 +00001084 }
1085 if (base.getTextAlign() != paint.getTextAlign()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001086 *ptr++ = PaintOp_packOpData(kAlign_PaintOp, paint.getTextAlign());
reed@google.comf5842f72011-05-04 18:30:04 +00001087 base.setTextAlign(paint.getTextAlign());
reed@google.combb6992a2011-04-26 17:41:56 +00001088 }
1089 if (base.getTextSize() != paint.getTextSize()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001090 *ptr++ = PaintOp_packOp(kTextSize_PaintOp);
1091 *ptr++ = castToU32(paint.getTextSize());
reed@google.comf5842f72011-05-04 18:30:04 +00001092 base.setTextSize(paint.getTextSize());
reed@google.combb6992a2011-04-26 17:41:56 +00001093 }
1094 if (base.getTextScaleX() != paint.getTextScaleX()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001095 *ptr++ = PaintOp_packOp(kTextScaleX_PaintOp);
1096 *ptr++ = castToU32(paint.getTextScaleX());
reed@google.comf5842f72011-05-04 18:30:04 +00001097 base.setTextScaleX(paint.getTextScaleX());
reed@google.combb6992a2011-04-26 17:41:56 +00001098 }
1099 if (base.getTextSkewX() != paint.getTextSkewX()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001100 *ptr++ = PaintOp_packOp(kTextSkewX_PaintOp);
1101 *ptr++ = castToU32(paint.getTextSkewX());
reed@google.comf5842f72011-05-04 18:30:04 +00001102 base.setTextSkewX(paint.getTextSkewX());
1103 }
1104
1105 if (!SkTypeface::Equal(base.getTypeface(), paint.getTypeface())) {
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +00001106 if (isCrossProcess(fFlags)) {
scroggo@google.com3cb969f2012-07-27 20:39:19 +00001107 uint32_t id = this->getTypefaceID(paint.getTypeface());
1108 *ptr++ = PaintOp_packOpData(kTypeface_PaintOp, id);
1109 } else if (this->needOpBytes(sizeof(void*))) {
1110 // Add to the set for ref counting.
1111 fTypefaceSet.add(paint.getTypeface());
1112 // It is safe to write the typeface to the stream before the rest
1113 // of the paint unless we ever send a kReset_PaintOp, which we
1114 // currently never do.
1115 this->writeOp(kSetTypeface_DrawOp);
1116 fWriter.writePtr(paint.getTypeface());
1117 }
reed@google.comf5842f72011-05-04 18:30:04 +00001118 base.setTypeface(paint.getTypeface());
reed@google.combb6992a2011-04-26 17:41:56 +00001119 }
reed@google.combb6992a2011-04-26 17:41:56 +00001120
scroggo@google.com4dffc592012-07-17 16:49:40 +00001121 // This is a new paint, so all old flats can be safely purged, if necessary.
1122 fFlattenableHeap.markAllFlatsSafeToDelete();
reed@google.comb55d1182011-05-11 00:42:04 +00001123 for (int i = 0; i < kCount_PaintFlats; i++) {
1124 int index = this->flattenToIndex(get_paintflat(paint, i), (PaintFlats)i);
scroggo@google.comd3ba5cc2012-07-09 16:05:53 +00001125 bool replaced = index < 0;
1126 if (replaced) {
1127 index = ~index;
1128 }
scroggo@google.com4dffc592012-07-17 16:49:40 +00001129 // Store the index of any flat that needs to be kept. 0 means no flat.
1130 if (index > 0) {
1131 fFlattenableHeap.markFlatForKeeping(index);
1132 }
1133 SkASSERT(index >= 0 && index <= fFlatDictionary.count());
scroggo@google.comd3ba5cc2012-07-09 16:05:53 +00001134 if (index != fCurrFlatIndex[i] || replaced) {
reed@google.comb55d1182011-05-11 00:42:04 +00001135 *ptr++ = PaintOp_packOpFlagData(kFlatIndex_PaintOp, i, index);
1136 fCurrFlatIndex[i] = index;
1137 }
1138 }
1139
reed@google.comacd471f2011-05-03 21:26:46 +00001140 size_t size = (char*)ptr - (char*)storage;
1141 if (size && this->needOpBytes(size)) {
commit-bot@chromium.orgb45bd1f2014-04-24 18:17:30 +00001142 this->writeOp(kPaintOp_DrawOp, 0, SkToU32(size));
reed@google.comb55d1182011-05-11 00:42:04 +00001143 fWriter.write(storage, size);
reed@google.combb6992a2011-04-26 17:41:56 +00001144 for (size_t i = 0; i < size/4; i++) {
reed@google.comb55d1182011-05-11 00:42:04 +00001145// SkDebugf("[%d] %08X\n", i, storage[i]);
reed@google.combb6992a2011-04-26 17:41:56 +00001146 }
1147 }
reed@google.com0cd2ac62013-10-14 20:02:44 +00001148
1149 //
1150 // Do these after we've written kPaintOp_DrawOp
skia.committer@gmail.comfbc58a32013-10-15 07:02:27 +00001151
reed@google.com0cd2ac62013-10-14 20:02:44 +00001152 if (base.getAnnotation() != paint.getAnnotation()) {
1153 if (NULL == paint.getAnnotation()) {
commit-bot@chromium.org89ff3dd2013-10-29 20:29:38 +00001154 if (this->needOpBytes()) {
1155 this->writeOp(kSetAnnotation_DrawOp, 0, 0);
1156 }
reed@google.com0cd2ac62013-10-14 20:02:44 +00001157 } else {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +00001158 SkWriteBuffer buffer;
reed@google.com0cd2ac62013-10-14 20:02:44 +00001159 paint.getAnnotation()->writeToBuffer(buffer);
commit-bot@chromium.org89ff3dd2013-10-29 20:29:38 +00001160 const size_t size = buffer.bytesWritten();
1161 if (this->needOpBytes(size)) {
commit-bot@chromium.orgb45bd1f2014-04-24 18:17:30 +00001162 this->writeOp(kSetAnnotation_DrawOp, 0, SkToU32(size));
commit-bot@chromium.org89ff3dd2013-10-29 20:29:38 +00001163 buffer.writeToMemory(fWriter.reserve(size));
1164 }
reed@google.com0cd2ac62013-10-14 20:02:44 +00001165 }
commit-bot@chromium.org40258a52013-10-29 19:23:26 +00001166 base.setAnnotation(paint.getAnnotation());
reed@google.com0cd2ac62013-10-14 20:02:44 +00001167 }
reed@google.combb6992a2011-04-26 17:41:56 +00001168}
1169
1170///////////////////////////////////////////////////////////////////////////////
1171
1172#include "SkGPipe.h"
1173
scroggo@google.com3cb969f2012-07-27 20:39:19 +00001174SkGPipeController::~SkGPipeController() {
1175 SkSafeUnref(fCanvas);
1176}
1177
1178void SkGPipeController::setCanvas(SkGPipeCanvas* canvas) {
1179 SkRefCnt_SafeAssign(fCanvas, canvas);
1180}
1181
1182///////////////////////////////////////////////////////////////////////////////
1183
1184SkGPipeWriter::SkGPipeWriter()
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +00001185: fWriter(0) {
reed@google.combb6992a2011-04-26 17:41:56 +00001186 fCanvas = NULL;
1187}
1188
1189SkGPipeWriter::~SkGPipeWriter() {
reed@google.comacd471f2011-05-03 21:26:46 +00001190 this->endRecording();
reed@google.combb6992a2011-04-26 17:41:56 +00001191}
1192
junov@chromium.orga8db8fe2012-08-15 19:49:22 +00001193SkCanvas* SkGPipeWriter::startRecording(SkGPipeController* controller, uint32_t flags,
1194 uint32_t width, uint32_t height) {
reed@google.combb6992a2011-04-26 17:41:56 +00001195 if (NULL == fCanvas) {
reed@google.comacd471f2011-05-03 21:26:46 +00001196 fWriter.reset(NULL, 0);
junov@chromium.orga8db8fe2012-08-15 19:49:22 +00001197 fCanvas = SkNEW_ARGS(SkGPipeCanvas, (controller, &fWriter, flags, width, height));
reed@google.combb6992a2011-04-26 17:41:56 +00001198 }
scroggo@google.com3cb969f2012-07-27 20:39:19 +00001199 controller->setCanvas(fCanvas);
reed@google.combb6992a2011-04-26 17:41:56 +00001200 return fCanvas;
1201}
1202
1203void SkGPipeWriter::endRecording() {
1204 if (fCanvas) {
scroggo@google.com59c3ab62013-11-12 14:32:38 +00001205 fCanvas->finish(true);
reed@google.combb6992a2011-04-26 17:41:56 +00001206 fCanvas->unref();
1207 fCanvas = NULL;
1208 }
1209}
1210
junov@chromium.org2e14ba82012-08-07 14:26:57 +00001211void SkGPipeWriter::flushRecording(bool detachCurrentBlock) {
1212 if (fCanvas) {
1213 fCanvas->flushRecording(detachCurrentBlock);
1214 }
junov@chromium.org77eec242012-07-18 17:54:45 +00001215}
1216
junov@chromium.org2e14ba82012-08-07 14:26:57 +00001217size_t SkGPipeWriter::freeMemoryIfPossible(size_t bytesToFree) {
1218 if (fCanvas) {
1219 return fCanvas->freeMemoryIfPossible(bytesToFree);
1220 }
1221 return 0;
1222}
1223
1224size_t SkGPipeWriter::storageAllocatedForRecording() const {
scroggo@google.com15011ee2012-07-26 20:03:32 +00001225 return NULL == fCanvas ? 0 : fCanvas->storageAllocatedForRecording();
1226}
1227
scroggo@google.com3e26bd02012-08-14 15:20:01 +00001228///////////////////////////////////////////////////////////////////////////////
1229
1230BitmapShuttle::BitmapShuttle(SkGPipeCanvas* canvas) {
1231 SkASSERT(canvas != NULL);
1232 fCanvas = canvas;
1233 fCanvas->ref();
1234}
1235
1236BitmapShuttle::~BitmapShuttle() {
scroggo@google.com59c3ab62013-11-12 14:32:38 +00001237 this->removeCanvas();
scroggo@google.com3e26bd02012-08-14 15:20:01 +00001238}
1239
1240bool BitmapShuttle::insert(const SkBitmap& bitmap, int32_t slot) {
scroggo@google.com59c3ab62013-11-12 14:32:38 +00001241 SkASSERT(fCanvas != NULL);
scroggo@google.com3e26bd02012-08-14 15:20:01 +00001242 return fCanvas->shuttleBitmap(bitmap, slot);
1243}
scroggo@google.com59c3ab62013-11-12 14:32:38 +00001244
1245void BitmapShuttle::removeCanvas() {
1246 if (NULL == fCanvas) {
1247 return;
1248 }
1249 fCanvas->unref();
1250 fCanvas = NULL;
1251}