blob: 54e3bead69d81dfc54c7c3e3acdae92640f4830d [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@google.combb6992a2011-04-26 17:41:56 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
reed@google.combb6992a2011-04-26 17:41:56 +00007 */
8
epoger@google.comb58772f2013-03-08 09:09:10 +00009#include "SkAnnotation.h"
robertphillips@google.com1f2f3382013-08-29 11:54:56 +000010#include "SkBitmapDevice.h"
scroggo@google.com10dccde2012-08-08 20:43:22 +000011#include "SkBitmapHeap.h"
reed@google.combb6992a2011-04-26 17:41:56 +000012#include "SkCanvas.h"
scroggo@google.com10dccde2012-08-08 20:43:22 +000013#include "SkColorFilter.h"
reed@google.com8a85d0c2011-06-24 19:12:12 +000014#include "SkData.h"
scroggo@google.com10dccde2012-08-08 20:43:22 +000015#include "SkDrawLooper.h"
reed@google.comacd471f2011-05-03 21:26:46 +000016#include "SkGPipe.h"
reed@google.combb6992a2011-04-26 17:41:56 +000017#include "SkGPipePriv.h"
scroggo@google.com16d1d0b2012-05-02 19:09:40 +000018#include "SkImageFilter.h"
scroggo@google.com10dccde2012-08-08 20:43:22 +000019#include "SkMaskFilter.h"
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000020#include "SkWriteBuffer.h"
scroggo@google.com10dccde2012-08-08 20:43:22 +000021#include "SkPaint.h"
22#include "SkPathEffect.h"
23#include "SkPictureFlat.h"
24#include "SkRasterizer.h"
reed@google.com4ed0fb72012-12-12 20:48:18 +000025#include "SkRRect.h"
scroggo@google.com10dccde2012-08-08 20:43:22 +000026#include "SkShader.h"
reed@google.comf5842f72011-05-04 18:30:04 +000027#include "SkStream.h"
reed@google.comb55d1182011-05-11 00:42:04 +000028#include "SkTSearch.h"
reed@google.comf5842f72011-05-04 18:30:04 +000029#include "SkTypeface.h"
reed@google.combb6992a2011-04-26 17:41:56 +000030#include "SkWriter32.h"
reed@google.comb55d1182011-05-11 00:42:04 +000031
reed@google.com4ed0fb72012-12-12 20:48:18 +000032enum {
33 kSizeOfFlatRRect = sizeof(SkRect) + 4 * sizeof(SkVector)
34};
35
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +000036static bool isCrossProcess(uint32_t flags) {
37 return SkToBool(flags & SkGPipeWriter::kCrossProcess_Flag);
38}
39
reed@google.comb55d1182011-05-11 00:42:04 +000040static SkFlattenable* get_paintflat(const SkPaint& paint, unsigned paintFlat) {
41 SkASSERT(paintFlat < kCount_PaintFlats);
42 switch (paintFlat) {
43 case kColorFilter_PaintFlat: return paint.getColorFilter();
reed@google.com0faac1e2011-05-11 05:58:58 +000044 case kDrawLooper_PaintFlat: return paint.getLooper();
reed@google.comb55d1182011-05-11 00:42:04 +000045 case kMaskFilter_PaintFlat: return paint.getMaskFilter();
46 case kPathEffect_PaintFlat: return paint.getPathEffect();
47 case kRasterizer_PaintFlat: return paint.getRasterizer();
48 case kShader_PaintFlat: return paint.getShader();
scroggo@google.com16d1d0b2012-05-02 19:09:40 +000049 case kImageFilter_PaintFlat: return paint.getImageFilter();
reed@google.comb55d1182011-05-11 00:42:04 +000050 case kXfermode_PaintFlat: return paint.getXfermode();
51 }
tomhudson@google.com0c00f212011-12-28 14:59:50 +000052 SkDEBUGFAIL("never gets here");
reed@google.comb55d1182011-05-11 00:42:04 +000053 return NULL;
54}
reed@google.combb6992a2011-04-26 17:41:56 +000055
reed@google.comf5842f72011-05-04 18:30:04 +000056static size_t writeTypeface(SkWriter32* writer, SkTypeface* typeface) {
57 SkASSERT(typeface);
58 SkDynamicMemoryWStream stream;
59 typeface->serialize(&stream);
60 size_t size = stream.getOffset();
61 if (writer) {
62 writer->write32(size);
reed@google.com8a85d0c2011-06-24 19:12:12 +000063 SkAutoDataUnref data(stream.copyToData());
robertphillips@google.com59f46b82012-07-10 17:30:58 +000064 writer->writePad(data->data(), size);
reed@google.comf5842f72011-05-04 18:30:04 +000065 }
scroggo@google.com5af9b202012-06-04 17:17:36 +000066 return 4 + SkAlign4(size);
reed@google.comf5842f72011-05-04 18:30:04 +000067}
68
reed@google.combb6992a2011-04-26 17:41:56 +000069///////////////////////////////////////////////////////////////////////////////
70
scroggo@google.com4dffc592012-07-17 16:49:40 +000071class FlattenableHeap : public SkFlatController {
72public:
scroggo@google.com664fab12012-08-14 19:22:05 +000073 FlattenableHeap(int numFlatsToKeep, SkNamedFactorySet* fset, bool isCrossProcess)
commit-bot@chromium.orga2bd2d12014-01-30 22:16:32 +000074 : INHERITED(isCrossProcess ? SkWriteBuffer::kCrossProcess_Flag : 0)
75 , fNumFlatsToKeep(numFlatsToKeep) {
scroggo@google.com664fab12012-08-14 19:22:05 +000076 SkASSERT((isCrossProcess && fset != NULL) || (!isCrossProcess && NULL == fset));
77 if (isCrossProcess) {
78 this->setNamedFactorySet(fset);
scroggo@google.com664fab12012-08-14 19:22:05 +000079 }
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +000080 }
scroggo@google.com4dffc592012-07-17 16:49:40 +000081
82 ~FlattenableHeap() {
83 fPointers.freeAll();
84 }
85
86 virtual void* allocThrow(size_t bytes) SK_OVERRIDE;
87
88 virtual void unalloc(void* ptr) SK_OVERRIDE;
89
scroggo@google.com7ca24432012-08-14 15:48:43 +000090 void setBitmapStorage(SkBitmapHeap* heap) {
91 this->setBitmapHeap(heap);
92 }
rmistry@google.comd6176b02012-08-23 18:14:13 +000093
scroggo@google.com4dffc592012-07-17 16:49:40 +000094 const SkFlatData* flatToReplace() const;
95
96 // Mark an SkFlatData as one that should not be returned by flatToReplace.
97 // Takes the result of SkFlatData::index() as its parameter.
98 void markFlatForKeeping(int index) {
99 *fFlatsThatMustBeKept.append() = index;
100 }
101
102 void markAllFlatsSafeToDelete() {
103 fFlatsThatMustBeKept.reset();
104 }
105
106private:
107 // Keep track of the indices (i.e. the result of SkFlatData::index()) of
108 // flats that must be kept, since they are on the current paint.
109 SkTDArray<int> fFlatsThatMustBeKept;
110 SkTDArray<void*> fPointers;
111 const int fNumFlatsToKeep;
commit-bot@chromium.orga2bd2d12014-01-30 22:16:32 +0000112
113 typedef SkFlatController INHERITED;
scroggo@google.com4dffc592012-07-17 16:49:40 +0000114};
115
116void FlattenableHeap::unalloc(void* ptr) {
117 int indexToRemove = fPointers.rfind(ptr);
118 if (indexToRemove >= 0) {
119 sk_free(ptr);
120 fPointers.remove(indexToRemove);
121 }
122}
123
124void* FlattenableHeap::allocThrow(size_t bytes) {
125 void* ptr = sk_malloc_throw(bytes);
126 *fPointers.append() = ptr;
127 return ptr;
128}
129
130const SkFlatData* FlattenableHeap::flatToReplace() const {
131 // First, determine whether we should replace one.
132 if (fPointers.count() > fNumFlatsToKeep) {
133 // Look through the flattenable heap.
134 // TODO: Return the LRU flat.
135 for (int i = 0; i < fPointers.count(); i++) {
136 SkFlatData* potential = (SkFlatData*)fPointers[i];
137 // Make sure that it is not one that must be kept.
138 bool mustKeep = false;
139 for (int j = 0; j < fFlatsThatMustBeKept.count(); j++) {
140 if (potential->index() == fFlatsThatMustBeKept[j]) {
141 mustKeep = true;
142 break;
143 }
144 }
145 if (!mustKeep) {
146 return potential;
147 }
148 }
149 }
150 return NULL;
151}
152
153///////////////////////////////////////////////////////////////////////////////
154
commit-bot@chromium.org07adb632014-01-02 22:20:49 +0000155struct SkFlattenableTraits {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000156 static void flatten(SkWriteBuffer& buffer, const SkFlattenable& flattenable) {
commit-bot@chromium.org07adb632014-01-02 22:20:49 +0000157 buffer.writeFlattenable(&flattenable);
scroggo@google.com4dffc592012-07-17 16:49:40 +0000158 }
commit-bot@chromium.org07adb632014-01-02 22:20:49 +0000159 // No need to define unflatten if we never call it.
scroggo@google.com4dffc592012-07-17 16:49:40 +0000160};
commit-bot@chromium.org07adb632014-01-02 22:20:49 +0000161typedef SkFlatDictionary<SkFlattenable, SkFlattenableTraits> FlatDictionary;
scroggo@google.com4dffc592012-07-17 16:49:40 +0000162
163///////////////////////////////////////////////////////////////////////////////
164
scroggo@google.com59c3ab62013-11-12 14:32:38 +0000165/**
166 * If SkBitmaps are to be flattened to send to the reader, this class is
167 * provided to the SkBitmapHeap to tell the SkGPipeCanvas to do so.
168 */
169class BitmapShuttle : public SkBitmapHeap::ExternalStorage {
170public:
171 BitmapShuttle(SkGPipeCanvas*);
172
173 ~BitmapShuttle();
174
175 virtual bool insert(const SkBitmap& bitmap, int32_t slot) SK_OVERRIDE;
176
177 /**
178 * Remove the SkGPipeCanvas used for insertion. After this, calls to
179 * insert will crash.
180 */
181 void removeCanvas();
182
183private:
184 SkGPipeCanvas* fCanvas;
185};
186
187///////////////////////////////////////////////////////////////////////////////
188
reed@google.combb6992a2011-04-26 17:41:56 +0000189class SkGPipeCanvas : public SkCanvas {
190public:
junov@chromium.orga8db8fe2012-08-15 19:49:22 +0000191 SkGPipeCanvas(SkGPipeController*, SkWriter32*, uint32_t flags,
192 uint32_t width, uint32_t height);
reed@google.combb6992a2011-04-26 17:41:56 +0000193 virtual ~SkGPipeCanvas();
194
scroggo@google.com59c3ab62013-11-12 14:32:38 +0000195 /**
196 * Called when nothing else is to be written to the stream. Any repeated
197 * calls are ignored.
198 *
199 * @param notifyReaders Whether to send a message to the reader(s) that
200 * the writer is through sending commands. Should generally be true,
201 * unless there is an error which prevents further messages from
202 * being sent.
203 */
204 void finish(bool notifyReaders) {
205 if (fDone) {
206 return;
reed@google.combb6992a2011-04-26 17:41:56 +0000207 }
scroggo@google.com59c3ab62013-11-12 14:32:38 +0000208 if (notifyReaders && this->needOpBytes()) {
209 this->writeOp(kDone_DrawOp);
210 this->doNotify();
211 }
212 if (shouldFlattenBitmaps(fFlags)) {
213 // The following circular references exist:
214 // fFlattenableHeap -> fWriteBuffer -> fBitmapStorage -> fExternalStorage -> fCanvas
215 // fBitmapHeap -> fExternalStorage -> fCanvas
216 // fFlattenableHeap -> fBitmapStorage -> fExternalStorage -> fCanvas
217
218 // Break them all by destroying the final link to this SkGPipeCanvas.
219 fBitmapShuttle->removeCanvas();
220 }
221 fDone = true;
reed@google.combb6992a2011-04-26 17:41:56 +0000222 }
223
junov@chromium.org77eec242012-07-18 17:54:45 +0000224 void flushRecording(bool detachCurrentBlock);
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000225 size_t freeMemoryIfPossible(size_t bytesToFree);
junov@chromium.org77eec242012-07-18 17:54:45 +0000226
scroggo@google.com15011ee2012-07-26 20:03:32 +0000227 size_t storageAllocatedForRecording() {
scroggo@google.comd5d158b2012-08-14 20:38:28 +0000228 return (NULL == fBitmapHeap) ? 0 : fBitmapHeap->bytesAllocated();
scroggo@google.com15011ee2012-07-26 20:03:32 +0000229 }
230
reed@google.combb6992a2011-04-26 17:41:56 +0000231 // overrides from SkCanvas
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000232 virtual int save(SaveFlags) SK_OVERRIDE;
233 virtual int saveLayer(const SkRect* bounds, const SkPaint*,
234 SaveFlags) SK_OVERRIDE;
235 virtual void restore() SK_OVERRIDE;
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000236 virtual bool isDrawingToLayer() const SK_OVERRIDE;
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000237 virtual bool translate(SkScalar dx, SkScalar dy) SK_OVERRIDE;
238 virtual bool scale(SkScalar sx, SkScalar sy) SK_OVERRIDE;
239 virtual bool rotate(SkScalar degrees) SK_OVERRIDE;
240 virtual bool skew(SkScalar sx, SkScalar sy) SK_OVERRIDE;
241 virtual bool concat(const SkMatrix& matrix) SK_OVERRIDE;
242 virtual void setMatrix(const SkMatrix& matrix) SK_OVERRIDE;
reed@google.com4ed0fb72012-12-12 20:48:18 +0000243 virtual bool clipRect(const SkRect&, SkRegion::Op op, bool doAntiAlias = false) SK_OVERRIDE;
244 virtual bool clipRRect(const SkRRect&, SkRegion::Op op, bool doAntiAlias = false) SK_OVERRIDE;
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000245 virtual bool clipPath(const SkPath& path, SkRegion::Op op,
246 bool doAntiAlias = false) SK_OVERRIDE;
247 virtual bool clipRegion(const SkRegion& region, SkRegion::Op op) SK_OVERRIDE;
248 virtual void clear(SkColor) SK_OVERRIDE;
249 virtual void drawPaint(const SkPaint& paint) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000250 virtual void drawPoints(PointMode, size_t count, const SkPoint pts[],
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000251 const SkPaint&) SK_OVERRIDE;
reed@google.com4ed0fb72012-12-12 20:48:18 +0000252 virtual void drawOval(const SkRect&, const SkPaint&) SK_OVERRIDE;
bsalomon@google.com7ce564c2013-10-22 16:54:15 +0000253 virtual void drawRect(const SkRect& rect, const SkPaint&) SK_OVERRIDE;
reed@google.com4ed0fb72012-12-12 20:48:18 +0000254 virtual void drawRRect(const SkRRect&, const SkPaint&) SK_OVERRIDE;
bsalomon@google.com7ce564c2013-10-22 16:54:15 +0000255 virtual void drawPath(const SkPath& path, const SkPaint&) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000256 virtual void drawBitmap(const SkBitmap&, SkScalar left, SkScalar top,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000257 const SkPaint*) SK_OVERRIDE;
reed@google.com71121732012-09-18 15:14:33 +0000258 virtual void drawBitmapRectToRect(const SkBitmap&, const SkRect* src,
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000259 const SkRect& dst, const SkPaint* paint,
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +0000260 DrawBitmapRectFlags flags) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000261 virtual void drawBitmapMatrix(const SkBitmap&, const SkMatrix&,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000262 const SkPaint*) SK_OVERRIDE;
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000263 virtual void drawBitmapNine(const SkBitmap& bitmap, const SkIRect& center,
264 const SkRect& dst, const SkPaint* paint = NULL) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000265 virtual void drawSprite(const SkBitmap&, int left, int top,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000266 const SkPaint*) SK_OVERRIDE;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000267 virtual void drawText(const void* text, size_t byteLength, SkScalar x,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000268 SkScalar y, const SkPaint&) SK_OVERRIDE;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000269 virtual void drawPosText(const void* text, size_t byteLength,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000270 const SkPoint pos[], const SkPaint&) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000271 virtual void drawPosTextH(const void* text, size_t byteLength,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000272 const SkScalar xpos[], SkScalar constY,
273 const SkPaint&) SK_OVERRIDE;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000274 virtual void drawTextOnPath(const void* text, size_t byteLength,
275 const SkPath& path, const SkMatrix* matrix,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000276 const SkPaint&) SK_OVERRIDE;
277 virtual void drawPicture(SkPicture& picture) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000278 virtual void drawVertices(VertexMode, int vertexCount,
279 const SkPoint vertices[], const SkPoint texs[],
280 const SkColor colors[], SkXfermode*,
281 const uint16_t indices[], int indexCount,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000282 const SkPaint&) SK_OVERRIDE;
283 virtual void drawData(const void*, size_t) SK_OVERRIDE;
robertphillips@google.com0a4805e2013-05-29 13:24:23 +0000284 virtual void beginCommentGroup(const char* description) SK_OVERRIDE;
285 virtual void addComment(const char* kywd, const char* value) SK_OVERRIDE;
286 virtual void endCommentGroup() SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000287
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000288 /**
289 * Flatten an SkBitmap to send to the reader, where it will be referenced
290 * according to slot.
291 */
292 bool shuttleBitmap(const SkBitmap&, int32_t slot);
reed@google.combb6992a2011-04-26 17:41:56 +0000293private:
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000294 enum {
295 kNoSaveLayer = -1,
296 };
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000297 SkNamedFactorySet* fFactorySet;
298 int fFirstSaveLayerStackLevel;
scroggo@google.comd9d29672012-08-14 17:21:34 +0000299 SkBitmapHeap* fBitmapHeap;
reed@google.comacd471f2011-05-03 21:26:46 +0000300 SkGPipeController* fController;
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000301 SkWriter32& fWriter;
302 size_t fBlockSize; // amount allocated for writer
303 size_t fBytesNotified;
304 bool fDone;
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000305 const uint32_t fFlags;
reed@google.combb6992a2011-04-26 17:41:56 +0000306
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000307 SkRefCntSet fTypefaceSet;
reed@google.comf5842f72011-05-04 18:30:04 +0000308
309 uint32_t getTypefaceID(SkTypeface*);
310
reed@google.comacd471f2011-05-03 21:26:46 +0000311 inline void writeOp(DrawOps op, unsigned flags, unsigned data) {
reed@google.combb6992a2011-04-26 17:41:56 +0000312 fWriter.write32(DrawOp_packOpFlagData(op, flags, data));
313 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000314
reed@google.comacd471f2011-05-03 21:26:46 +0000315 inline void writeOp(DrawOps op) {
reed@google.combb6992a2011-04-26 17:41:56 +0000316 fWriter.write32(DrawOp_packOpFlagData(op, 0, 0));
317 }
reed@google.comacd471f2011-05-03 21:26:46 +0000318
319 bool needOpBytes(size_t size = 0);
320
321 inline void doNotify() {
322 if (!fDone) {
reed@google.com44699382013-10-31 17:28:30 +0000323 size_t bytes = fWriter.bytesWritten() - fBytesNotified;
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000324 if (bytes > 0) {
325 fController->notifyWritten(bytes);
326 fBytesNotified += bytes;
327 }
reed@google.comacd471f2011-05-03 21:26:46 +0000328 }
329 }
reed@google.comb55d1182011-05-11 00:42:04 +0000330
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000331 // Should be called after any calls to an SkFlatDictionary::findAndReplace
332 // if a new SkFlatData was added when in cross process mode
333 void flattenFactoryNames();
334
scroggo@google.com59c3ab62013-11-12 14:32:38 +0000335 FlattenableHeap fFlattenableHeap;
336 FlatDictionary fFlatDictionary;
337 SkAutoTUnref<BitmapShuttle> fBitmapShuttle;
338 int fCurrFlatIndex[kCount_PaintFlats];
339
reed@google.comb55d1182011-05-11 00:42:04 +0000340 int flattenToIndex(SkFlattenable* obj, PaintFlats);
341
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000342 // Common code used by drawBitmap*. Behaves differently depending on the
343 // type of SkBitmapHeap being used, which is determined by the flags used.
344 bool commonDrawBitmap(const SkBitmap& bm, DrawOps op, unsigned flags,
345 size_t opBytesNeeded, const SkPaint* paint);
scroggo@google.com58be6822012-07-30 14:40:01 +0000346
reed@google.com31891582011-05-12 03:03:56 +0000347 SkPaint fPaint;
348 void writePaint(const SkPaint&);
reed@google.combb6992a2011-04-26 17:41:56 +0000349
reed@google.comacd471f2011-05-03 21:26:46 +0000350 class AutoPipeNotify {
351 public:
352 AutoPipeNotify(SkGPipeCanvas* canvas) : fCanvas(canvas) {}
353 ~AutoPipeNotify() { fCanvas->doNotify(); }
354 private:
355 SkGPipeCanvas* fCanvas;
356 };
357 friend class AutoPipeNotify;
358
reed@google.combb6992a2011-04-26 17:41:56 +0000359 typedef SkCanvas INHERITED;
360};
361
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000362void SkGPipeCanvas::flattenFactoryNames() {
363 const char* name;
364 while ((name = fFactorySet->getNextAddedFactoryName()) != NULL) {
365 size_t len = strlen(name);
366 if (this->needOpBytes(len)) {
367 this->writeOp(kDef_Factory_DrawOp);
368 fWriter.writeString(name, len);
369 }
370 }
371}
372
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000373bool SkGPipeCanvas::shuttleBitmap(const SkBitmap& bm, int32_t slot) {
scroggo@google.com565254b2012-06-28 15:41:32 +0000374 SkASSERT(shouldFlattenBitmaps(fFlags));
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000375 SkWriteBuffer buffer;
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000376 buffer.setNamedFactoryRecorder(fFactorySet);
scroggo@google.com74b7ffd2013-04-30 02:32:41 +0000377 buffer.writeBitmap(bm);
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000378 this->flattenFactoryNames();
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000379 uint32_t size = buffer.bytesWritten();
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000380 if (this->needOpBytes(size)) {
381 this->writeOp(kDef_Bitmap_DrawOp, 0, slot);
382 void* dst = static_cast<void*>(fWriter.reserve(size));
383 buffer.writeToMemory(dst);
384 return true;
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000385 }
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000386 return false;
scroggo@google.comd3ba5cc2012-07-09 16:05:53 +0000387}
388
reed@google.comb55d1182011-05-11 00:42:04 +0000389// return 0 for NULL (or unflattenable obj), or index-base-1
scroggo@google.comd3ba5cc2012-07-09 16:05:53 +0000390// return ~(index-base-1) if an old flattenable was replaced
reed@google.comb55d1182011-05-11 00:42:04 +0000391int SkGPipeCanvas::flattenToIndex(SkFlattenable* obj, PaintFlats paintflat) {
scroggo@google.comd5d158b2012-08-14 20:38:28 +0000392 SkASSERT(!fDone && fBitmapHeap != NULL);
reed@google.comb55d1182011-05-11 00:42:04 +0000393 if (NULL == obj) {
394 return 0;
395 }
reed@google.comb55d1182011-05-11 00:42:04 +0000396
scroggo@google.comd9d29672012-08-14 17:21:34 +0000397 fBitmapHeap->deferAddingOwners();
scroggo@google.com4dffc592012-07-17 16:49:40 +0000398 bool added, replaced;
scroggo@google.com664fab12012-08-14 19:22:05 +0000399 const SkFlatData* flat = fFlatDictionary.findAndReplace(*obj, fFlattenableHeap.flatToReplace(),
400 &added, &replaced);
scroggo@google.comd9d29672012-08-14 17:21:34 +0000401 fBitmapHeap->endAddingOwnersDeferral(added);
scroggo@google.com4dffc592012-07-17 16:49:40 +0000402 int index = flat->index();
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000403 if (added) {
404 if (isCrossProcess(fFlags)) {
405 this->flattenFactoryNames();
406 }
407 size_t flatSize = flat->flatSize();
408 if (this->needOpBytes(flatSize)) {
409 this->writeOp(kDef_Flattenable_DrawOp, paintflat, index);
410 fWriter.write(flat->data(), flatSize);
411 }
scroggo@google.com4dffc592012-07-17 16:49:40 +0000412 }
413 if (replaced) {
reed@google.comb55d1182011-05-11 00:42:04 +0000414 index = ~index;
reed@google.comb55d1182011-05-11 00:42:04 +0000415 }
scroggo@google.com4dffc592012-07-17 16:49:40 +0000416 return index;
reed@google.comb55d1182011-05-11 00:42:04 +0000417}
418
reed@google.combb6992a2011-04-26 17:41:56 +0000419///////////////////////////////////////////////////////////////////////////////
420
reed@google.comacd471f2011-05-03 21:26:46 +0000421#define MIN_BLOCK_SIZE (16 * 1024)
scroggo@google.com4dffc592012-07-17 16:49:40 +0000422#define BITMAPS_TO_KEEP 5
423#define FLATTENABLES_TO_KEEP 10
reed@google.combb6992a2011-04-26 17:41:56 +0000424
reed@google.comacd471f2011-05-03 21:26:46 +0000425SkGPipeCanvas::SkGPipeCanvas(SkGPipeController* controller,
rmistry@google.comd6176b02012-08-23 18:14:13 +0000426 SkWriter32* writer, uint32_t flags,
junov@chromium.orga8db8fe2012-08-15 19:49:22 +0000427 uint32_t width, uint32_t height)
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000428: fFactorySet(isCrossProcess(flags) ? SkNEW(SkNamedFactorySet) : NULL)
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000429, fWriter(*writer)
430, fFlags(flags)
scroggo@google.com664fab12012-08-14 19:22:05 +0000431, fFlattenableHeap(FLATTENABLES_TO_KEEP, fFactorySet, isCrossProcess(flags))
scroggo@google.com15543602012-08-02 18:49:49 +0000432, fFlatDictionary(&fFlattenableHeap) {
reed@google.comacd471f2011-05-03 21:26:46 +0000433 fController = controller;
reed@google.combb6992a2011-04-26 17:41:56 +0000434 fDone = false;
reed@google.comacd471f2011-05-03 21:26:46 +0000435 fBlockSize = 0; // need first block from controller
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000436 fBytesNotified = 0;
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000437 fFirstSaveLayerStackLevel = kNoSaveLayer;
reed@google.comb55d1182011-05-11 00:42:04 +0000438 sk_bzero(fCurrFlatIndex, sizeof(fCurrFlatIndex));
reed@google.comacd471f2011-05-03 21:26:46 +0000439
reed@google.combb6793b2011-05-05 15:18:15 +0000440 // we need a device to limit our clip
yangsu@google.com06b4da12011-06-17 15:04:40 +0000441 // We don't allocate pixels for the bitmap
reed@google.combb6793b2011-05-05 15:18:15 +0000442 SkBitmap bitmap;
junov@chromium.orga8db8fe2012-08-15 19:49:22 +0000443 bitmap.setConfig(SkBitmap::kARGB_8888_Config, width, height);
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000444 SkBaseDevice* device = SkNEW_ARGS(SkBitmapDevice, (bitmap));
reed@google.combb6793b2011-05-05 15:18:15 +0000445 this->setDevice(device)->unref();
scroggo@google.com10dccde2012-08-08 20:43:22 +0000446
scroggo@google.com565254b2012-06-28 15:41:32 +0000447 // Tell the reader the appropriate flags to use.
448 if (this->needOpBytes()) {
449 this->writeOp(kReportFlags_DrawOp, fFlags, 0);
450 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000451
scroggo@google.com10dccde2012-08-08 20:43:22 +0000452 if (shouldFlattenBitmaps(flags)) {
scroggo@google.com59c3ab62013-11-12 14:32:38 +0000453 fBitmapShuttle.reset(SkNEW_ARGS(BitmapShuttle, (this)));
454 fBitmapHeap = SkNEW_ARGS(SkBitmapHeap, (fBitmapShuttle.get(), BITMAPS_TO_KEEP));
scroggo@google.com10dccde2012-08-08 20:43:22 +0000455 } else {
scroggo@google.comd9d29672012-08-14 17:21:34 +0000456 fBitmapHeap = SkNEW_ARGS(SkBitmapHeap,
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000457 (BITMAPS_TO_KEEP, controller->numberOfReaders()));
scroggo@google.com10dccde2012-08-08 20:43:22 +0000458 if (this->needOpBytes(sizeof(void*))) {
scroggo@google.comd9d29672012-08-14 17:21:34 +0000459 this->writeOp(kShareBitmapHeap_DrawOp);
460 fWriter.writePtr(static_cast<void*>(fBitmapHeap));
scroggo@google.com10dccde2012-08-08 20:43:22 +0000461 }
462 }
scroggo@google.comd9d29672012-08-14 17:21:34 +0000463 fFlattenableHeap.setBitmapStorage(fBitmapHeap);
scroggo@google.com10dccde2012-08-08 20:43:22 +0000464 this->doNotify();
reed@google.combb6992a2011-04-26 17:41:56 +0000465}
466
467SkGPipeCanvas::~SkGPipeCanvas() {
scroggo@google.com59c3ab62013-11-12 14:32:38 +0000468 this->finish(true);
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000469 SkSafeUnref(fFactorySet);
scroggo@google.comd9d29672012-08-14 17:21:34 +0000470 SkSafeUnref(fBitmapHeap);
reed@google.combb6992a2011-04-26 17:41:56 +0000471}
472
reed@google.comacd471f2011-05-03 21:26:46 +0000473bool SkGPipeCanvas::needOpBytes(size_t needed) {
474 if (fDone) {
475 return false;
476 }
477
478 needed += 4; // size of DrawOp atom
reed@google.com44699382013-10-31 17:28:30 +0000479 if (fWriter.bytesWritten() + needed > fBlockSize) {
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000480 // Before we wipe out any data that has already been written, read it
481 // out.
482 this->doNotify();
483 size_t blockSize = SkMax32(MIN_BLOCK_SIZE, needed);
484 void* block = fController->requestBlock(blockSize, &fBlockSize);
reed@google.comacd471f2011-05-03 21:26:46 +0000485 if (NULL == block) {
scroggo@google.com59c3ab62013-11-12 14:32:38 +0000486 // Do not notify the readers, which would call this function again.
487 this->finish(false);
reed@google.comacd471f2011-05-03 21:26:46 +0000488 return false;
489 }
scroggo@google.com460a23e2012-08-16 17:56:49 +0000490 SkASSERT(SkIsAlign4(fBlockSize));
reed@google.comacd471f2011-05-03 21:26:46 +0000491 fWriter.reset(block, fBlockSize);
492 fBytesNotified = 0;
493 }
494 return true;
495}
496
reed@google.comf5842f72011-05-04 18:30:04 +0000497uint32_t SkGPipeCanvas::getTypefaceID(SkTypeface* face) {
498 uint32_t id = 0; // 0 means default/null typeface
499 if (face) {
500 id = fTypefaceSet.find(face);
501 if (0 == id) {
502 id = fTypefaceSet.add(face);
503 size_t size = writeTypeface(NULL, face);
504 if (this->needOpBytes(size)) {
reed@google.combb6793b2011-05-05 15:18:15 +0000505 this->writeOp(kDef_Typeface_DrawOp);
reed@google.comf5842f72011-05-04 18:30:04 +0000506 writeTypeface(&fWriter, face);
507 }
508 }
509 }
510 return id;
511}
512
reed@google.combb6992a2011-04-26 17:41:56 +0000513///////////////////////////////////////////////////////////////////////////////
514
reed@google.comacd471f2011-05-03 21:26:46 +0000515#define NOTIFY_SETUP(canvas) \
516 AutoPipeNotify apn(canvas)
517
reed@google.combb6992a2011-04-26 17:41:56 +0000518int SkGPipeCanvas::save(SaveFlags flags) {
reed@google.comacd471f2011-05-03 21:26:46 +0000519 NOTIFY_SETUP(this);
520 if (this->needOpBytes()) {
521 this->writeOp(kSave_DrawOp, 0, flags);
522 }
reed@google.combb6992a2011-04-26 17:41:56 +0000523 return this->INHERITED::save(flags);
524}
525
526int SkGPipeCanvas::saveLayer(const SkRect* bounds, const SkPaint* paint,
527 SaveFlags saveFlags) {
reed@google.comacd471f2011-05-03 21:26:46 +0000528 NOTIFY_SETUP(this);
529 size_t size = 0;
reed@google.combb6992a2011-04-26 17:41:56 +0000530 unsigned opFlags = 0;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000531
reed@google.combb6992a2011-04-26 17:41:56 +0000532 if (bounds) {
533 opFlags |= kSaveLayer_HasBounds_DrawOpFlag;
reed@google.comacd471f2011-05-03 21:26:46 +0000534 size += sizeof(SkRect);
reed@google.combb6992a2011-04-26 17:41:56 +0000535 }
536 if (paint) {
537 opFlags |= kSaveLayer_HasPaint_DrawOpFlag;
reed@google.com31891582011-05-12 03:03:56 +0000538 this->writePaint(*paint);
reed@google.combb6992a2011-04-26 17:41:56 +0000539 }
540
reed@google.comacd471f2011-05-03 21:26:46 +0000541 if (this->needOpBytes(size)) {
542 this->writeOp(kSaveLayer_DrawOp, opFlags, saveFlags);
543 if (bounds) {
544 fWriter.writeRect(*bounds);
545 }
reed@google.combb6992a2011-04-26 17:41:56 +0000546 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000547
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000548 if (kNoSaveLayer == fFirstSaveLayerStackLevel){
549 fFirstSaveLayerStackLevel = this->getSaveCount();
550 }
reed@google.combb6992a2011-04-26 17:41:56 +0000551 // we just pass on the save, so we don't create a layer
552 return this->INHERITED::save(saveFlags);
553}
554
555void SkGPipeCanvas::restore() {
reed@google.comacd471f2011-05-03 21:26:46 +0000556 NOTIFY_SETUP(this);
557 if (this->needOpBytes()) {
558 this->writeOp(kRestore_DrawOp);
559 }
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000560
reed@google.combb6992a2011-04-26 17:41:56 +0000561 this->INHERITED::restore();
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000562
563 if (this->getSaveCount() == fFirstSaveLayerStackLevel){
564 fFirstSaveLayerStackLevel = kNoSaveLayer;
565 }
566}
567
568bool SkGPipeCanvas::isDrawingToLayer() const {
569 return kNoSaveLayer != fFirstSaveLayerStackLevel;
reed@google.combb6992a2011-04-26 17:41:56 +0000570}
571
572bool SkGPipeCanvas::translate(SkScalar dx, SkScalar dy) {
573 if (dx || dy) {
reed@google.comacd471f2011-05-03 21:26:46 +0000574 NOTIFY_SETUP(this);
575 if (this->needOpBytes(2 * sizeof(SkScalar))) {
576 this->writeOp(kTranslate_DrawOp);
577 fWriter.writeScalar(dx);
578 fWriter.writeScalar(dy);
579 }
reed@google.combb6992a2011-04-26 17:41:56 +0000580 }
581 return this->INHERITED::translate(dx, dy);
582}
583
584bool SkGPipeCanvas::scale(SkScalar sx, SkScalar sy) {
585 if (sx || sy) {
reed@google.comacd471f2011-05-03 21:26:46 +0000586 NOTIFY_SETUP(this);
587 if (this->needOpBytes(2 * sizeof(SkScalar))) {
588 this->writeOp(kScale_DrawOp);
589 fWriter.writeScalar(sx);
590 fWriter.writeScalar(sy);
591 }
reed@google.combb6992a2011-04-26 17:41:56 +0000592 }
593 return this->INHERITED::scale(sx, sy);
594}
595
596bool SkGPipeCanvas::rotate(SkScalar degrees) {
597 if (degrees) {
reed@google.comacd471f2011-05-03 21:26:46 +0000598 NOTIFY_SETUP(this);
599 if (this->needOpBytes(sizeof(SkScalar))) {
600 this->writeOp(kRotate_DrawOp);
601 fWriter.writeScalar(degrees);
602 }
reed@google.combb6992a2011-04-26 17:41:56 +0000603 }
604 return this->INHERITED::rotate(degrees);
605}
606
607bool SkGPipeCanvas::skew(SkScalar sx, SkScalar sy) {
608 if (sx || sy) {
reed@google.comacd471f2011-05-03 21:26:46 +0000609 NOTIFY_SETUP(this);
610 if (this->needOpBytes(2 * sizeof(SkScalar))) {
611 this->writeOp(kSkew_DrawOp);
612 fWriter.writeScalar(sx);
613 fWriter.writeScalar(sy);
614 }
reed@google.combb6992a2011-04-26 17:41:56 +0000615 }
616 return this->INHERITED::skew(sx, sy);
617}
618
619bool SkGPipeCanvas::concat(const SkMatrix& matrix) {
620 if (!matrix.isIdentity()) {
reed@google.comacd471f2011-05-03 21:26:46 +0000621 NOTIFY_SETUP(this);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000622 if (this->needOpBytes(matrix.writeToMemory(NULL))) {
reed@google.comacd471f2011-05-03 21:26:46 +0000623 this->writeOp(kConcat_DrawOp);
djsollen@google.com2b2ede32012-04-12 13:24:04 +0000624 fWriter.writeMatrix(matrix);
reed@google.comacd471f2011-05-03 21:26:46 +0000625 }
reed@google.combb6992a2011-04-26 17:41:56 +0000626 }
627 return this->INHERITED::concat(matrix);
628}
629
630void SkGPipeCanvas::setMatrix(const SkMatrix& matrix) {
reed@google.comacd471f2011-05-03 21:26:46 +0000631 NOTIFY_SETUP(this);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000632 if (this->needOpBytes(matrix.writeToMemory(NULL))) {
reed@google.comacd471f2011-05-03 21:26:46 +0000633 this->writeOp(kSetMatrix_DrawOp);
djsollen@google.com2b2ede32012-04-12 13:24:04 +0000634 fWriter.writeMatrix(matrix);
reed@google.comacd471f2011-05-03 21:26:46 +0000635 }
reed@google.combb6992a2011-04-26 17:41:56 +0000636 this->INHERITED::setMatrix(matrix);
637}
638
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000639bool SkGPipeCanvas::clipRect(const SkRect& rect, SkRegion::Op rgnOp,
640 bool doAntiAlias) {
reed@google.comacd471f2011-05-03 21:26:46 +0000641 NOTIFY_SETUP(this);
scroggo@google.com460a23e2012-08-16 17:56:49 +0000642 if (this->needOpBytes(sizeof(SkRect))) {
643 unsigned flags = doAntiAlias & kClip_HasAntiAlias_DrawOpFlag;
644 this->writeOp(kClipRect_DrawOp, flags, rgnOp);
reed@google.comacd471f2011-05-03 21:26:46 +0000645 fWriter.writeRect(rect);
646 }
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000647 return this->INHERITED::clipRect(rect, rgnOp, doAntiAlias);
reed@google.combb6992a2011-04-26 17:41:56 +0000648}
649
reed@google.com4ed0fb72012-12-12 20:48:18 +0000650bool SkGPipeCanvas::clipRRect(const SkRRect& rrect, SkRegion::Op rgnOp,
651 bool doAntiAlias) {
652 NOTIFY_SETUP(this);
653 if (this->needOpBytes(kSizeOfFlatRRect)) {
654 unsigned flags = doAntiAlias & kClip_HasAntiAlias_DrawOpFlag;
655 this->writeOp(kClipRRect_DrawOp, flags, rgnOp);
656 fWriter.writeRRect(rrect);
657 }
658 return this->INHERITED::clipRRect(rrect, rgnOp, doAntiAlias);
659}
660
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000661bool SkGPipeCanvas::clipPath(const SkPath& path, SkRegion::Op rgnOp,
662 bool doAntiAlias) {
reed@google.comacd471f2011-05-03 21:26:46 +0000663 NOTIFY_SETUP(this);
scroggo@google.com460a23e2012-08-16 17:56:49 +0000664 if (this->needOpBytes(path.writeToMemory(NULL))) {
665 unsigned flags = doAntiAlias & kClip_HasAntiAlias_DrawOpFlag;
666 this->writeOp(kClipPath_DrawOp, flags, rgnOp);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000667 fWriter.writePath(path);
reed@google.comacd471f2011-05-03 21:26:46 +0000668 }
reed@google.combb6992a2011-04-26 17:41:56 +0000669 // we just pass on the bounds of the path
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000670 return this->INHERITED::clipRect(path.getBounds(), rgnOp, doAntiAlias);
reed@google.combb6992a2011-04-26 17:41:56 +0000671}
672
673bool SkGPipeCanvas::clipRegion(const SkRegion& region, SkRegion::Op rgnOp) {
reed@google.comacd471f2011-05-03 21:26:46 +0000674 NOTIFY_SETUP(this);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000675 if (this->needOpBytes(region.writeToMemory(NULL))) {
reed@google.comacd471f2011-05-03 21:26:46 +0000676 this->writeOp(kClipRegion_DrawOp, 0, rgnOp);
djsollen@google.com2b2ede32012-04-12 13:24:04 +0000677 fWriter.writeRegion(region);
reed@google.comacd471f2011-05-03 21:26:46 +0000678 }
reed@google.combb6992a2011-04-26 17:41:56 +0000679 return this->INHERITED::clipRegion(region, rgnOp);
680}
681
682///////////////////////////////////////////////////////////////////////////////
683
684void SkGPipeCanvas::clear(SkColor color) {
reed@google.comacd471f2011-05-03 21:26:46 +0000685 NOTIFY_SETUP(this);
reed@google.combb6992a2011-04-26 17:41:56 +0000686 unsigned flags = 0;
687 if (color) {
688 flags |= kClear_HasColor_DrawOpFlag;
689 }
reed@google.comacd471f2011-05-03 21:26:46 +0000690 if (this->needOpBytes(sizeof(SkColor))) {
691 this->writeOp(kDrawClear_DrawOp, flags, 0);
692 if (color) {
693 fWriter.write32(color);
694 }
reed@google.combb6992a2011-04-26 17:41:56 +0000695 }
696}
697
698void SkGPipeCanvas::drawPaint(const SkPaint& paint) {
reed@google.comacd471f2011-05-03 21:26:46 +0000699 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000700 this->writePaint(paint);
reed@google.comacd471f2011-05-03 21:26:46 +0000701 if (this->needOpBytes()) {
reed@google.com31891582011-05-12 03:03:56 +0000702 this->writeOp(kDrawPaint_DrawOp);
reed@google.comacd471f2011-05-03 21:26:46 +0000703 }
reed@google.combb6992a2011-04-26 17:41:56 +0000704}
705
706void SkGPipeCanvas::drawPoints(PointMode mode, size_t count,
robertphillips@google.com8b169312013-10-15 17:47:36 +0000707 const SkPoint pts[], const SkPaint& paint) {
reed@google.combb6992a2011-04-26 17:41:56 +0000708 if (count) {
reed@google.comacd471f2011-05-03 21:26:46 +0000709 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000710 this->writePaint(paint);
reed@google.comacd471f2011-05-03 21:26:46 +0000711 if (this->needOpBytes(4 + count * sizeof(SkPoint))) {
reed@google.com31891582011-05-12 03:03:56 +0000712 this->writeOp(kDrawPoints_DrawOp, mode, 0);
reed@google.comacd471f2011-05-03 21:26:46 +0000713 fWriter.write32(count);
714 fWriter.write(pts, count * sizeof(SkPoint));
715 }
reed@google.combb6992a2011-04-26 17:41:56 +0000716 }
717}
718
reed@google.com4ed0fb72012-12-12 20:48:18 +0000719void SkGPipeCanvas::drawOval(const SkRect& rect, const SkPaint& paint) {
720 NOTIFY_SETUP(this);
721 this->writePaint(paint);
722 if (this->needOpBytes(sizeof(SkRect))) {
723 this->writeOp(kDrawOval_DrawOp);
724 fWriter.writeRect(rect);
725 }
726}
727
bsalomon@google.com7ce564c2013-10-22 16:54:15 +0000728void SkGPipeCanvas::drawRect(const SkRect& rect, const SkPaint& paint) {
reed@google.comacd471f2011-05-03 21:26:46 +0000729 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000730 this->writePaint(paint);
reed@google.comacd471f2011-05-03 21:26:46 +0000731 if (this->needOpBytes(sizeof(SkRect))) {
reed@google.com31891582011-05-12 03:03:56 +0000732 this->writeOp(kDrawRect_DrawOp);
reed@google.comacd471f2011-05-03 21:26:46 +0000733 fWriter.writeRect(rect);
734 }
reed@google.combb6992a2011-04-26 17:41:56 +0000735}
736
reed@google.com4ed0fb72012-12-12 20:48:18 +0000737void SkGPipeCanvas::drawRRect(const SkRRect& rrect, const SkPaint& paint) {
738 NOTIFY_SETUP(this);
739 this->writePaint(paint);
740 if (this->needOpBytes(kSizeOfFlatRRect)) {
741 this->writeOp(kDrawRRect_DrawOp);
742 fWriter.writeRRect(rrect);
743 }
744}
745
bsalomon@google.com7ce564c2013-10-22 16:54:15 +0000746void SkGPipeCanvas::drawPath(const SkPath& path, const SkPaint& paint) {
reed@google.comacd471f2011-05-03 21:26:46 +0000747 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000748 this->writePaint(paint);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000749 if (this->needOpBytes(path.writeToMemory(NULL))) {
reed@google.com31891582011-05-12 03:03:56 +0000750 this->writeOp(kDrawPath_DrawOp);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000751 fWriter.writePath(path);
reed@google.comacd471f2011-05-03 21:26:46 +0000752 }
reed@google.combb6992a2011-04-26 17:41:56 +0000753}
754
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000755bool SkGPipeCanvas::commonDrawBitmap(const SkBitmap& bm, DrawOps op,
756 unsigned flags,
757 size_t opBytesNeeded,
758 const SkPaint* paint) {
scroggo@google.com58be6822012-07-30 14:40:01 +0000759 if (paint != NULL) {
scroggo@google.com460a23e2012-08-16 17:56:49 +0000760 flags |= kDrawBitmap_HasPaint_DrawOpFlag;
scroggo@google.com58be6822012-07-30 14:40:01 +0000761 this->writePaint(*paint);
762 }
scroggo@google.com10dccde2012-08-08 20:43:22 +0000763 if (this->needOpBytes(opBytesNeeded)) {
scroggo@google.comd5d158b2012-08-14 20:38:28 +0000764 SkASSERT(fBitmapHeap != NULL);
765 int32_t bitmapIndex = fBitmapHeap->insert(bm);
766 if (SkBitmapHeap::INVALID_SLOT == bitmapIndex) {
767 return false;
768 }
scroggo@google.com10dccde2012-08-08 20:43:22 +0000769 this->writeOp(op, flags, bitmapIndex);
scroggo@google.com58be6822012-07-30 14:40:01 +0000770 return true;
771 }
772 return false;
773}
774
775void SkGPipeCanvas::drawBitmap(const SkBitmap& bm, SkScalar left, SkScalar top,
776 const SkPaint* paint) {
777 NOTIFY_SETUP(this);
778 size_t opBytesNeeded = sizeof(SkScalar) * 2;
779
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000780 if (this->commonDrawBitmap(bm, kDrawBitmap_DrawOp, 0, opBytesNeeded, paint)) {
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000781 fWriter.writeScalar(left);
782 fWriter.writeScalar(top);
783 }
reed@google.combb6992a2011-04-26 17:41:56 +0000784}
785
reed@google.com71121732012-09-18 15:14:33 +0000786void SkGPipeCanvas::drawBitmapRectToRect(const SkBitmap& bm, const SkRect* src,
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +0000787 const SkRect& dst, const SkPaint* paint,
788 DrawBitmapRectFlags dbmrFlags) {
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000789 NOTIFY_SETUP(this);
scroggo@google.com58be6822012-07-30 14:40:01 +0000790 size_t opBytesNeeded = sizeof(SkRect);
791 bool hasSrc = src != NULL;
792 unsigned flags;
793 if (hasSrc) {
scroggo@google.com460a23e2012-08-16 17:56:49 +0000794 flags = kDrawBitmap_HasSrcRect_DrawOpFlag;
scroggo@google.com58be6822012-07-30 14:40:01 +0000795 opBytesNeeded += sizeof(int32_t) * 4;
796 } else {
797 flags = 0;
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000798 }
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +0000799 if (dbmrFlags & kBleed_DrawBitmapRectFlag) {
800 flags |= kDrawBitmap_Bleed_DrawOpFlag;
801 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000802
reed@google.com71121732012-09-18 15:14:33 +0000803 if (this->commonDrawBitmap(bm, kDrawBitmapRectToRect_DrawOp, flags, opBytesNeeded, paint)) {
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000804 if (hasSrc) {
reed@google.com71121732012-09-18 15:14:33 +0000805 fWriter.writeRect(*src);
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000806 }
807 fWriter.writeRect(dst);
808 }
reed@google.combb6992a2011-04-26 17:41:56 +0000809}
810
scroggo@google.com58be6822012-07-30 14:40:01 +0000811void SkGPipeCanvas::drawBitmapMatrix(const SkBitmap& bm, const SkMatrix& matrix,
812 const SkPaint* paint) {
813 NOTIFY_SETUP(this);
814 size_t opBytesNeeded = matrix.writeToMemory(NULL);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000815
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000816 if (this->commonDrawBitmap(bm, kDrawBitmapMatrix_DrawOp, 0, opBytesNeeded, paint)) {
scroggo@google.com58be6822012-07-30 14:40:01 +0000817 fWriter.writeMatrix(matrix);
818 }
reed@google.combb6992a2011-04-26 17:41:56 +0000819}
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000820
821void SkGPipeCanvas::drawBitmapNine(const SkBitmap& bm, const SkIRect& center,
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000822 const SkRect& dst, const SkPaint* paint) {
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000823 NOTIFY_SETUP(this);
scroggo@google.com58be6822012-07-30 14:40:01 +0000824 size_t opBytesNeeded = sizeof(int32_t) * 4 + sizeof(SkRect);
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000825
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000826 if (this->commonDrawBitmap(bm, kDrawBitmapNine_DrawOp, 0, opBytesNeeded, paint)) {
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000827 fWriter.write32(center.fLeft);
828 fWriter.write32(center.fTop);
829 fWriter.write32(center.fRight);
830 fWriter.write32(center.fBottom);
831 fWriter.writeRect(dst);
832 }
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000833}
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000834
835void SkGPipeCanvas::drawSprite(const SkBitmap& bm, int left, int top,
836 const SkPaint* paint) {
837 NOTIFY_SETUP(this);
scroggo@google.com58be6822012-07-30 14:40:01 +0000838 size_t opBytesNeeded = sizeof(int32_t) * 2;
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000839
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000840 if (this->commonDrawBitmap(bm, kDrawSprite_DrawOp, 0, opBytesNeeded, paint)) {
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000841 fWriter.write32(left);
842 fWriter.write32(top);
843 }
reed@google.combb6992a2011-04-26 17:41:56 +0000844}
845
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000846void SkGPipeCanvas::drawText(const void* text, size_t byteLength, SkScalar x,
reed@google.combb6992a2011-04-26 17:41:56 +0000847 SkScalar y, const SkPaint& paint) {
848 if (byteLength) {
reed@google.comacd471f2011-05-03 21:26:46 +0000849 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000850 this->writePaint(paint);
reed@google.comacd471f2011-05-03 21:26:46 +0000851 if (this->needOpBytes(4 + SkAlign4(byteLength) + 2 * sizeof(SkScalar))) {
reed@google.com31891582011-05-12 03:03:56 +0000852 this->writeOp(kDrawText_DrawOp);
reed@google.comacd471f2011-05-03 21:26:46 +0000853 fWriter.write32(byteLength);
854 fWriter.writePad(text, byteLength);
855 fWriter.writeScalar(x);
856 fWriter.writeScalar(y);
857 }
reed@google.combb6992a2011-04-26 17:41:56 +0000858 }
859}
860
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000861void SkGPipeCanvas::drawPosText(const void* text, size_t byteLength,
reed@google.combb6992a2011-04-26 17:41:56 +0000862 const SkPoint pos[], const SkPaint& paint) {
863 if (byteLength) {
reed@google.comacd471f2011-05-03 21:26:46 +0000864 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000865 this->writePaint(paint);
reed@google.combb6992a2011-04-26 17:41:56 +0000866 int count = paint.textToGlyphs(text, byteLength, NULL);
reed@google.comacd471f2011-05-03 21:26:46 +0000867 if (this->needOpBytes(4 + SkAlign4(byteLength) + 4 + count * sizeof(SkPoint))) {
reed@google.com31891582011-05-12 03:03:56 +0000868 this->writeOp(kDrawPosText_DrawOp);
reed@google.comacd471f2011-05-03 21:26:46 +0000869 fWriter.write32(byteLength);
870 fWriter.writePad(text, byteLength);
871 fWriter.write32(count);
872 fWriter.write(pos, count * sizeof(SkPoint));
873 }
reed@google.combb6992a2011-04-26 17:41:56 +0000874 }
875}
876
877void SkGPipeCanvas::drawPosTextH(const void* text, size_t byteLength,
878 const SkScalar xpos[], SkScalar constY,
879 const SkPaint& paint) {
880 if (byteLength) {
reed@google.comacd471f2011-05-03 21:26:46 +0000881 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000882 this->writePaint(paint);
reed@google.combb6992a2011-04-26 17:41:56 +0000883 int count = paint.textToGlyphs(text, byteLength, NULL);
reed@google.comacd471f2011-05-03 21:26:46 +0000884 if (this->needOpBytes(4 + SkAlign4(byteLength) + 4 + count * sizeof(SkScalar) + 4)) {
reed@google.com31891582011-05-12 03:03:56 +0000885 this->writeOp(kDrawPosTextH_DrawOp);
reed@google.comacd471f2011-05-03 21:26:46 +0000886 fWriter.write32(byteLength);
887 fWriter.writePad(text, byteLength);
888 fWriter.write32(count);
889 fWriter.write(xpos, count * sizeof(SkScalar));
890 fWriter.writeScalar(constY);
891 }
reed@google.combb6992a2011-04-26 17:41:56 +0000892 }
893}
894
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000895void SkGPipeCanvas::drawTextOnPath(const void* text, size_t byteLength,
896 const SkPath& path, const SkMatrix* matrix,
reed@google.combb6992a2011-04-26 17:41:56 +0000897 const SkPaint& paint) {
898 if (byteLength) {
reed@google.comacd471f2011-05-03 21:26:46 +0000899 NOTIFY_SETUP(this);
reed@google.combb6992a2011-04-26 17:41:56 +0000900 unsigned flags = 0;
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000901 size_t size = 4 + SkAlign4(byteLength) + path.writeToMemory(NULL);
reed@google.combb6992a2011-04-26 17:41:56 +0000902 if (matrix) {
903 flags |= kDrawTextOnPath_HasMatrix_DrawOpFlag;
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000904 size += matrix->writeToMemory(NULL);
reed@google.combb6992a2011-04-26 17:41:56 +0000905 }
reed@google.com31891582011-05-12 03:03:56 +0000906 this->writePaint(paint);
reed@google.comacd471f2011-05-03 21:26:46 +0000907 if (this->needOpBytes(size)) {
reed@google.com31891582011-05-12 03:03:56 +0000908 this->writeOp(kDrawTextOnPath_DrawOp, flags, 0);
reed@google.combb6992a2011-04-26 17:41:56 +0000909
reed@google.comacd471f2011-05-03 21:26:46 +0000910 fWriter.write32(byteLength);
911 fWriter.writePad(text, byteLength);
reed@google.combb6992a2011-04-26 17:41:56 +0000912
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000913 fWriter.writePath(path);
reed@google.comacd471f2011-05-03 21:26:46 +0000914 if (matrix) {
djsollen@google.com2b2ede32012-04-12 13:24:04 +0000915 fWriter.writeMatrix(*matrix);
reed@google.comacd471f2011-05-03 21:26:46 +0000916 }
reed@google.combb6992a2011-04-26 17:41:56 +0000917 }
918 }
919}
920
921void SkGPipeCanvas::drawPicture(SkPicture& picture) {
reed@google.com0faac1e2011-05-11 05:58:58 +0000922 // we want to playback the picture into individual draw calls
923 this->INHERITED::drawPicture(picture);
reed@google.combb6992a2011-04-26 17:41:56 +0000924}
925
reed@google.com85e143c2013-12-30 15:51:25 +0000926void SkGPipeCanvas::drawVertices(VertexMode vmode, int vertexCount,
reed@google.combb6992a2011-04-26 17:41:56 +0000927 const SkPoint vertices[], const SkPoint texs[],
reed@google.com85e143c2013-12-30 15:51:25 +0000928 const SkColor colors[], SkXfermode* xfer,
reed@google.combb6992a2011-04-26 17:41:56 +0000929 const uint16_t indices[], int indexCount,
930 const SkPaint& paint) {
931 if (0 == vertexCount) {
932 return;
933 }
934
reed@google.comacd471f2011-05-03 21:26:46 +0000935 NOTIFY_SETUP(this);
936 size_t size = 4 + vertexCount * sizeof(SkPoint);
reed@google.com31891582011-05-12 03:03:56 +0000937 this->writePaint(paint);
reed@google.combb6992a2011-04-26 17:41:56 +0000938 unsigned flags = 0;
939 if (texs) {
940 flags |= kDrawVertices_HasTexs_DrawOpFlag;
reed@google.comacd471f2011-05-03 21:26:46 +0000941 size += vertexCount * sizeof(SkPoint);
reed@google.combb6992a2011-04-26 17:41:56 +0000942 }
943 if (colors) {
944 flags |= kDrawVertices_HasColors_DrawOpFlag;
reed@google.comacd471f2011-05-03 21:26:46 +0000945 size += vertexCount * sizeof(SkColor);
reed@google.combb6992a2011-04-26 17:41:56 +0000946 }
947 if (indices && indexCount > 0) {
948 flags |= kDrawVertices_HasIndices_DrawOpFlag;
reed@google.comacd471f2011-05-03 21:26:46 +0000949 size += 4 + SkAlign4(indexCount * sizeof(uint16_t));
reed@google.combb6992a2011-04-26 17:41:56 +0000950 }
reed@google.com85e143c2013-12-30 15:51:25 +0000951 if (xfer && !SkXfermode::IsMode(xfer, SkXfermode::kModulate_Mode)) {
952 flags |= kDrawVertices_HasXfermode_DrawOpFlag;
953 size += sizeof(int32_t); // mode enum
954 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000955
reed@google.comacd471f2011-05-03 21:26:46 +0000956 if (this->needOpBytes(size)) {
reed@google.com31891582011-05-12 03:03:56 +0000957 this->writeOp(kDrawVertices_DrawOp, flags, 0);
reed@google.com85e143c2013-12-30 15:51:25 +0000958 fWriter.write32(vmode);
reed@google.comacd471f2011-05-03 21:26:46 +0000959 fWriter.write32(vertexCount);
960 fWriter.write(vertices, vertexCount * sizeof(SkPoint));
961 if (texs) {
962 fWriter.write(texs, vertexCount * sizeof(SkPoint));
963 }
964 if (colors) {
965 fWriter.write(colors, vertexCount * sizeof(SkColor));
966 }
reed@google.com85e143c2013-12-30 15:51:25 +0000967 if (flags & kDrawVertices_HasXfermode_DrawOpFlag) {
968 SkXfermode::Mode mode = SkXfermode::kModulate_Mode;
969 (void)xfer->asMode(&mode);
970 fWriter.write32(mode);
971 }
reed@google.comacd471f2011-05-03 21:26:46 +0000972 if (indices && indexCount > 0) {
973 fWriter.write32(indexCount);
974 fWriter.writePad(indices, indexCount * sizeof(uint16_t));
975 }
reed@google.combb6992a2011-04-26 17:41:56 +0000976 }
977}
978
reed@google.comacd471f2011-05-03 21:26:46 +0000979void SkGPipeCanvas::drawData(const void* ptr, size_t size) {
980 if (size && ptr) {
981 NOTIFY_SETUP(this);
reed@google.combb6992a2011-04-26 17:41:56 +0000982 unsigned data = 0;
983 if (size < (1 << DRAWOPS_DATA_BITS)) {
984 data = (unsigned)size;
985 }
reed@google.comacd471f2011-05-03 21:26:46 +0000986 if (this->needOpBytes(4 + SkAlign4(size))) {
987 this->writeOp(kDrawData_DrawOp, 0, data);
988 if (0 == data) {
989 fWriter.write32(size);
990 }
reed@google.combb6793b2011-05-05 15:18:15 +0000991 fWriter.writePad(ptr, size);
reed@google.combb6992a2011-04-26 17:41:56 +0000992 }
993 }
994}
995
robertphillips@google.com0a4805e2013-05-29 13:24:23 +0000996void SkGPipeCanvas::beginCommentGroup(const char* description) {
997 // ignore for now
998}
999
1000void SkGPipeCanvas::addComment(const char* kywd, const char* value) {
1001 // ignore for now
1002}
1003
1004void SkGPipeCanvas::endCommentGroup() {
1005 // ignore for now
1006}
1007
junov@chromium.org77eec242012-07-18 17:54:45 +00001008void SkGPipeCanvas::flushRecording(bool detachCurrentBlock) {
1009 doNotify();
1010 if (detachCurrentBlock) {
1011 // force a new block to be requested for the next recorded command
rmistry@google.comd6176b02012-08-23 18:14:13 +00001012 fBlockSize = 0;
junov@chromium.org77eec242012-07-18 17:54:45 +00001013 }
1014}
1015
junov@chromium.org2e14ba82012-08-07 14:26:57 +00001016size_t SkGPipeCanvas::freeMemoryIfPossible(size_t bytesToFree) {
scroggo@google.comd5d158b2012-08-14 20:38:28 +00001017 return (NULL == fBitmapHeap) ? 0 : fBitmapHeap->freeMemoryIfPossible(bytesToFree);
junov@chromium.org2e14ba82012-08-07 14:26:57 +00001018}
1019
reed@google.combb6992a2011-04-26 17:41:56 +00001020///////////////////////////////////////////////////////////////////////////////
1021
1022template <typename T> uint32_t castToU32(T value) {
1023 union {
1024 T fSrc;
1025 uint32_t fDst;
1026 } data;
1027 data.fSrc = value;
1028 return data.fDst;
1029}
1030
reed@google.com31891582011-05-12 03:03:56 +00001031void SkGPipeCanvas::writePaint(const SkPaint& paint) {
scroggo@google.comd5d158b2012-08-14 20:38:28 +00001032 if (fDone) {
1033 return;
1034 }
reed@google.com31891582011-05-12 03:03:56 +00001035 SkPaint& base = fPaint;
reed@google.combb6992a2011-04-26 17:41:56 +00001036 uint32_t storage[32];
1037 uint32_t* ptr = storage;
reed@google.combb6992a2011-04-26 17:41:56 +00001038
1039 if (base.getFlags() != paint.getFlags()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001040 *ptr++ = PaintOp_packOpData(kFlags_PaintOp, paint.getFlags());
reed@google.comf5842f72011-05-04 18:30:04 +00001041 base.setFlags(paint.getFlags());
reed@google.combb6992a2011-04-26 17:41:56 +00001042 }
1043 if (base.getColor() != paint.getColor()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001044 *ptr++ = PaintOp_packOp(kColor_PaintOp);
1045 *ptr++ = paint.getColor();
reed@google.comf5842f72011-05-04 18:30:04 +00001046 base.setColor(paint.getColor());
reed@google.combb6992a2011-04-26 17:41:56 +00001047 }
1048 if (base.getStyle() != paint.getStyle()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001049 *ptr++ = PaintOp_packOpData(kStyle_PaintOp, paint.getStyle());
reed@google.comf5842f72011-05-04 18:30:04 +00001050 base.setStyle(paint.getStyle());
reed@google.combb6992a2011-04-26 17:41:56 +00001051 }
1052 if (base.getStrokeJoin() != paint.getStrokeJoin()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001053 *ptr++ = PaintOp_packOpData(kJoin_PaintOp, paint.getStrokeJoin());
reed@google.comf5842f72011-05-04 18:30:04 +00001054 base.setStrokeJoin(paint.getStrokeJoin());
reed@google.combb6992a2011-04-26 17:41:56 +00001055 }
1056 if (base.getStrokeCap() != paint.getStrokeCap()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001057 *ptr++ = PaintOp_packOpData(kCap_PaintOp, paint.getStrokeCap());
reed@google.comf5842f72011-05-04 18:30:04 +00001058 base.setStrokeCap(paint.getStrokeCap());
reed@google.combb6992a2011-04-26 17:41:56 +00001059 }
1060 if (base.getStrokeWidth() != paint.getStrokeWidth()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001061 *ptr++ = PaintOp_packOp(kWidth_PaintOp);
1062 *ptr++ = castToU32(paint.getStrokeWidth());
reed@google.comf5842f72011-05-04 18:30:04 +00001063 base.setStrokeWidth(paint.getStrokeWidth());
reed@google.combb6992a2011-04-26 17:41:56 +00001064 }
1065 if (base.getStrokeMiter() != paint.getStrokeMiter()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001066 *ptr++ = PaintOp_packOp(kMiter_PaintOp);
1067 *ptr++ = castToU32(paint.getStrokeMiter());
reed@google.comf5842f72011-05-04 18:30:04 +00001068 base.setStrokeMiter(paint.getStrokeMiter());
reed@google.combb6992a2011-04-26 17:41:56 +00001069 }
1070 if (base.getTextEncoding() != paint.getTextEncoding()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001071 *ptr++ = PaintOp_packOpData(kEncoding_PaintOp, paint.getTextEncoding());
reed@google.comf5842f72011-05-04 18:30:04 +00001072 base.setTextEncoding(paint.getTextEncoding());
reed@google.combb6992a2011-04-26 17:41:56 +00001073 }
1074 if (base.getHinting() != paint.getHinting()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001075 *ptr++ = PaintOp_packOpData(kHinting_PaintOp, paint.getHinting());
reed@google.comf5842f72011-05-04 18:30:04 +00001076 base.setHinting(paint.getHinting());
reed@google.combb6992a2011-04-26 17:41:56 +00001077 }
1078 if (base.getTextAlign() != paint.getTextAlign()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001079 *ptr++ = PaintOp_packOpData(kAlign_PaintOp, paint.getTextAlign());
reed@google.comf5842f72011-05-04 18:30:04 +00001080 base.setTextAlign(paint.getTextAlign());
reed@google.combb6992a2011-04-26 17:41:56 +00001081 }
1082 if (base.getTextSize() != paint.getTextSize()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001083 *ptr++ = PaintOp_packOp(kTextSize_PaintOp);
1084 *ptr++ = castToU32(paint.getTextSize());
reed@google.comf5842f72011-05-04 18:30:04 +00001085 base.setTextSize(paint.getTextSize());
reed@google.combb6992a2011-04-26 17:41:56 +00001086 }
1087 if (base.getTextScaleX() != paint.getTextScaleX()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001088 *ptr++ = PaintOp_packOp(kTextScaleX_PaintOp);
1089 *ptr++ = castToU32(paint.getTextScaleX());
reed@google.comf5842f72011-05-04 18:30:04 +00001090 base.setTextScaleX(paint.getTextScaleX());
reed@google.combb6992a2011-04-26 17:41:56 +00001091 }
1092 if (base.getTextSkewX() != paint.getTextSkewX()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001093 *ptr++ = PaintOp_packOp(kTextSkewX_PaintOp);
1094 *ptr++ = castToU32(paint.getTextSkewX());
reed@google.comf5842f72011-05-04 18:30:04 +00001095 base.setTextSkewX(paint.getTextSkewX());
1096 }
1097
1098 if (!SkTypeface::Equal(base.getTypeface(), paint.getTypeface())) {
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +00001099 if (isCrossProcess(fFlags)) {
scroggo@google.com3cb969f2012-07-27 20:39:19 +00001100 uint32_t id = this->getTypefaceID(paint.getTypeface());
1101 *ptr++ = PaintOp_packOpData(kTypeface_PaintOp, id);
1102 } else if (this->needOpBytes(sizeof(void*))) {
1103 // Add to the set for ref counting.
1104 fTypefaceSet.add(paint.getTypeface());
1105 // It is safe to write the typeface to the stream before the rest
1106 // of the paint unless we ever send a kReset_PaintOp, which we
1107 // currently never do.
1108 this->writeOp(kSetTypeface_DrawOp);
1109 fWriter.writePtr(paint.getTypeface());
1110 }
reed@google.comf5842f72011-05-04 18:30:04 +00001111 base.setTypeface(paint.getTypeface());
reed@google.combb6992a2011-04-26 17:41:56 +00001112 }
reed@google.combb6992a2011-04-26 17:41:56 +00001113
scroggo@google.com4dffc592012-07-17 16:49:40 +00001114 // This is a new paint, so all old flats can be safely purged, if necessary.
1115 fFlattenableHeap.markAllFlatsSafeToDelete();
reed@google.comb55d1182011-05-11 00:42:04 +00001116 for (int i = 0; i < kCount_PaintFlats; i++) {
1117 int index = this->flattenToIndex(get_paintflat(paint, i), (PaintFlats)i);
scroggo@google.comd3ba5cc2012-07-09 16:05:53 +00001118 bool replaced = index < 0;
1119 if (replaced) {
1120 index = ~index;
1121 }
scroggo@google.com4dffc592012-07-17 16:49:40 +00001122 // Store the index of any flat that needs to be kept. 0 means no flat.
1123 if (index > 0) {
1124 fFlattenableHeap.markFlatForKeeping(index);
1125 }
1126 SkASSERT(index >= 0 && index <= fFlatDictionary.count());
scroggo@google.comd3ba5cc2012-07-09 16:05:53 +00001127 if (index != fCurrFlatIndex[i] || replaced) {
reed@google.comb55d1182011-05-11 00:42:04 +00001128 *ptr++ = PaintOp_packOpFlagData(kFlatIndex_PaintOp, i, index);
1129 fCurrFlatIndex[i] = index;
1130 }
1131 }
1132
reed@google.comacd471f2011-05-03 21:26:46 +00001133 size_t size = (char*)ptr - (char*)storage;
1134 if (size && this->needOpBytes(size)) {
reed@google.comb55d1182011-05-11 00:42:04 +00001135 this->writeOp(kPaintOp_DrawOp, 0, size);
reed@google.comb55d1182011-05-11 00:42:04 +00001136 fWriter.write(storage, size);
reed@google.combb6992a2011-04-26 17:41:56 +00001137 for (size_t i = 0; i < size/4; i++) {
reed@google.comb55d1182011-05-11 00:42:04 +00001138// SkDebugf("[%d] %08X\n", i, storage[i]);
reed@google.combb6992a2011-04-26 17:41:56 +00001139 }
1140 }
reed@google.com0cd2ac62013-10-14 20:02:44 +00001141
1142 //
1143 // Do these after we've written kPaintOp_DrawOp
skia.committer@gmail.comfbc58a32013-10-15 07:02:27 +00001144
reed@google.com0cd2ac62013-10-14 20:02:44 +00001145 if (base.getAnnotation() != paint.getAnnotation()) {
1146 if (NULL == paint.getAnnotation()) {
commit-bot@chromium.org89ff3dd2013-10-29 20:29:38 +00001147 if (this->needOpBytes()) {
1148 this->writeOp(kSetAnnotation_DrawOp, 0, 0);
1149 }
reed@google.com0cd2ac62013-10-14 20:02:44 +00001150 } else {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +00001151 SkWriteBuffer buffer;
reed@google.com0cd2ac62013-10-14 20:02:44 +00001152 paint.getAnnotation()->writeToBuffer(buffer);
commit-bot@chromium.org89ff3dd2013-10-29 20:29:38 +00001153 const size_t size = buffer.bytesWritten();
1154 if (this->needOpBytes(size)) {
1155 this->writeOp(kSetAnnotation_DrawOp, 0, size);
1156 buffer.writeToMemory(fWriter.reserve(size));
1157 }
reed@google.com0cd2ac62013-10-14 20:02:44 +00001158 }
commit-bot@chromium.org40258a52013-10-29 19:23:26 +00001159 base.setAnnotation(paint.getAnnotation());
reed@google.com0cd2ac62013-10-14 20:02:44 +00001160 }
reed@google.combb6992a2011-04-26 17:41:56 +00001161}
1162
1163///////////////////////////////////////////////////////////////////////////////
1164
1165#include "SkGPipe.h"
1166
scroggo@google.com3cb969f2012-07-27 20:39:19 +00001167SkGPipeController::~SkGPipeController() {
1168 SkSafeUnref(fCanvas);
1169}
1170
1171void SkGPipeController::setCanvas(SkGPipeCanvas* canvas) {
1172 SkRefCnt_SafeAssign(fCanvas, canvas);
1173}
1174
1175///////////////////////////////////////////////////////////////////////////////
1176
1177SkGPipeWriter::SkGPipeWriter()
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +00001178: fWriter(0) {
reed@google.combb6992a2011-04-26 17:41:56 +00001179 fCanvas = NULL;
1180}
1181
1182SkGPipeWriter::~SkGPipeWriter() {
reed@google.comacd471f2011-05-03 21:26:46 +00001183 this->endRecording();
reed@google.combb6992a2011-04-26 17:41:56 +00001184}
1185
junov@chromium.orga8db8fe2012-08-15 19:49:22 +00001186SkCanvas* SkGPipeWriter::startRecording(SkGPipeController* controller, uint32_t flags,
1187 uint32_t width, uint32_t height) {
reed@google.combb6992a2011-04-26 17:41:56 +00001188 if (NULL == fCanvas) {
reed@google.comacd471f2011-05-03 21:26:46 +00001189 fWriter.reset(NULL, 0);
junov@chromium.orga8db8fe2012-08-15 19:49:22 +00001190 fCanvas = SkNEW_ARGS(SkGPipeCanvas, (controller, &fWriter, flags, width, height));
reed@google.combb6992a2011-04-26 17:41:56 +00001191 }
scroggo@google.com3cb969f2012-07-27 20:39:19 +00001192 controller->setCanvas(fCanvas);
reed@google.combb6992a2011-04-26 17:41:56 +00001193 return fCanvas;
1194}
1195
1196void SkGPipeWriter::endRecording() {
1197 if (fCanvas) {
scroggo@google.com59c3ab62013-11-12 14:32:38 +00001198 fCanvas->finish(true);
reed@google.combb6992a2011-04-26 17:41:56 +00001199 fCanvas->unref();
1200 fCanvas = NULL;
1201 }
1202}
1203
junov@chromium.org2e14ba82012-08-07 14:26:57 +00001204void SkGPipeWriter::flushRecording(bool detachCurrentBlock) {
1205 if (fCanvas) {
1206 fCanvas->flushRecording(detachCurrentBlock);
1207 }
junov@chromium.org77eec242012-07-18 17:54:45 +00001208}
1209
junov@chromium.org2e14ba82012-08-07 14:26:57 +00001210size_t SkGPipeWriter::freeMemoryIfPossible(size_t bytesToFree) {
1211 if (fCanvas) {
1212 return fCanvas->freeMemoryIfPossible(bytesToFree);
1213 }
1214 return 0;
1215}
1216
1217size_t SkGPipeWriter::storageAllocatedForRecording() const {
scroggo@google.com15011ee2012-07-26 20:03:32 +00001218 return NULL == fCanvas ? 0 : fCanvas->storageAllocatedForRecording();
1219}
1220
scroggo@google.com3e26bd02012-08-14 15:20:01 +00001221///////////////////////////////////////////////////////////////////////////////
1222
1223BitmapShuttle::BitmapShuttle(SkGPipeCanvas* canvas) {
1224 SkASSERT(canvas != NULL);
1225 fCanvas = canvas;
1226 fCanvas->ref();
1227}
1228
1229BitmapShuttle::~BitmapShuttle() {
scroggo@google.com59c3ab62013-11-12 14:32:38 +00001230 this->removeCanvas();
scroggo@google.com3e26bd02012-08-14 15:20:01 +00001231}
1232
1233bool BitmapShuttle::insert(const SkBitmap& bitmap, int32_t slot) {
scroggo@google.com59c3ab62013-11-12 14:32:38 +00001234 SkASSERT(fCanvas != NULL);
scroggo@google.com3e26bd02012-08-14 15:20:01 +00001235 return fCanvas->shuttleBitmap(bitmap, slot);
1236}
scroggo@google.com59c3ab62013-11-12 14:32:38 +00001237
1238void BitmapShuttle::removeCanvas() {
1239 if (NULL == fCanvas) {
1240 return;
1241 }
1242 fCanvas->unref();
1243 fCanvas = NULL;
1244}