blob: ce58ca6122cc95bde1fbe5446c5435bc85356231 [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)
scroggo@google.com15543602012-08-02 18:49:49 +000074 : fNumFlatsToKeep(numFlatsToKeep) {
scroggo@google.com664fab12012-08-14 19:22:05 +000075 SkASSERT((isCrossProcess && fset != NULL) || (!isCrossProcess && NULL == fset));
76 if (isCrossProcess) {
77 this->setNamedFactorySet(fset);
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000078 this->setWriteBufferFlags(SkWriteBuffer::kCrossProcess_Flag);
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;
112};
113
114void FlattenableHeap::unalloc(void* ptr) {
115 int indexToRemove = fPointers.rfind(ptr);
116 if (indexToRemove >= 0) {
117 sk_free(ptr);
118 fPointers.remove(indexToRemove);
119 }
120}
121
122void* FlattenableHeap::allocThrow(size_t bytes) {
123 void* ptr = sk_malloc_throw(bytes);
124 *fPointers.append() = ptr;
125 return ptr;
126}
127
128const SkFlatData* FlattenableHeap::flatToReplace() const {
129 // First, determine whether we should replace one.
130 if (fPointers.count() > fNumFlatsToKeep) {
131 // Look through the flattenable heap.
132 // TODO: Return the LRU flat.
133 for (int i = 0; i < fPointers.count(); i++) {
134 SkFlatData* potential = (SkFlatData*)fPointers[i];
135 // Make sure that it is not one that must be kept.
136 bool mustKeep = false;
137 for (int j = 0; j < fFlatsThatMustBeKept.count(); j++) {
138 if (potential->index() == fFlatsThatMustBeKept[j]) {
139 mustKeep = true;
140 break;
141 }
142 }
143 if (!mustKeep) {
144 return potential;
145 }
146 }
147 }
148 return NULL;
149}
150
151///////////////////////////////////////////////////////////////////////////////
152
commit-bot@chromium.org07adb632014-01-02 22:20:49 +0000153struct SkFlattenableTraits {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000154 static void flatten(SkWriteBuffer& buffer, const SkFlattenable& flattenable) {
commit-bot@chromium.org07adb632014-01-02 22:20:49 +0000155 buffer.writeFlattenable(&flattenable);
scroggo@google.com4dffc592012-07-17 16:49:40 +0000156 }
commit-bot@chromium.org07adb632014-01-02 22:20:49 +0000157 // No need to define unflatten if we never call it.
scroggo@google.com4dffc592012-07-17 16:49:40 +0000158};
commit-bot@chromium.org07adb632014-01-02 22:20:49 +0000159typedef SkFlatDictionary<SkFlattenable, SkFlattenableTraits> FlatDictionary;
scroggo@google.com4dffc592012-07-17 16:49:40 +0000160
161///////////////////////////////////////////////////////////////////////////////
162
scroggo@google.com59c3ab62013-11-12 14:32:38 +0000163/**
164 * If SkBitmaps are to be flattened to send to the reader, this class is
165 * provided to the SkBitmapHeap to tell the SkGPipeCanvas to do so.
166 */
167class BitmapShuttle : public SkBitmapHeap::ExternalStorage {
168public:
169 BitmapShuttle(SkGPipeCanvas*);
170
171 ~BitmapShuttle();
172
173 virtual bool insert(const SkBitmap& bitmap, int32_t slot) SK_OVERRIDE;
174
175 /**
176 * Remove the SkGPipeCanvas used for insertion. After this, calls to
177 * insert will crash.
178 */
179 void removeCanvas();
180
181private:
182 SkGPipeCanvas* fCanvas;
183};
184
185///////////////////////////////////////////////////////////////////////////////
186
reed@google.combb6992a2011-04-26 17:41:56 +0000187class SkGPipeCanvas : public SkCanvas {
188public:
junov@chromium.orga8db8fe2012-08-15 19:49:22 +0000189 SkGPipeCanvas(SkGPipeController*, SkWriter32*, uint32_t flags,
190 uint32_t width, uint32_t height);
reed@google.combb6992a2011-04-26 17:41:56 +0000191 virtual ~SkGPipeCanvas();
192
scroggo@google.com59c3ab62013-11-12 14:32:38 +0000193 /**
194 * Called when nothing else is to be written to the stream. Any repeated
195 * calls are ignored.
196 *
197 * @param notifyReaders Whether to send a message to the reader(s) that
198 * the writer is through sending commands. Should generally be true,
199 * unless there is an error which prevents further messages from
200 * being sent.
201 */
202 void finish(bool notifyReaders) {
203 if (fDone) {
204 return;
reed@google.combb6992a2011-04-26 17:41:56 +0000205 }
scroggo@google.com59c3ab62013-11-12 14:32:38 +0000206 if (notifyReaders && this->needOpBytes()) {
207 this->writeOp(kDone_DrawOp);
208 this->doNotify();
209 }
210 if (shouldFlattenBitmaps(fFlags)) {
211 // The following circular references exist:
212 // fFlattenableHeap -> fWriteBuffer -> fBitmapStorage -> fExternalStorage -> fCanvas
213 // fBitmapHeap -> fExternalStorage -> fCanvas
214 // fFlattenableHeap -> fBitmapStorage -> fExternalStorage -> fCanvas
215
216 // Break them all by destroying the final link to this SkGPipeCanvas.
217 fBitmapShuttle->removeCanvas();
218 }
219 fDone = true;
reed@google.combb6992a2011-04-26 17:41:56 +0000220 }
221
junov@chromium.org77eec242012-07-18 17:54:45 +0000222 void flushRecording(bool detachCurrentBlock);
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000223 size_t freeMemoryIfPossible(size_t bytesToFree);
junov@chromium.org77eec242012-07-18 17:54:45 +0000224
scroggo@google.com15011ee2012-07-26 20:03:32 +0000225 size_t storageAllocatedForRecording() {
scroggo@google.comd5d158b2012-08-14 20:38:28 +0000226 return (NULL == fBitmapHeap) ? 0 : fBitmapHeap->bytesAllocated();
scroggo@google.com15011ee2012-07-26 20:03:32 +0000227 }
228
reed@google.combb6992a2011-04-26 17:41:56 +0000229 // overrides from SkCanvas
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000230 virtual int save(SaveFlags) SK_OVERRIDE;
231 virtual int saveLayer(const SkRect* bounds, const SkPaint*,
232 SaveFlags) SK_OVERRIDE;
233 virtual void restore() SK_OVERRIDE;
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000234 virtual bool isDrawingToLayer() const SK_OVERRIDE;
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000235 virtual bool translate(SkScalar dx, SkScalar dy) SK_OVERRIDE;
236 virtual bool scale(SkScalar sx, SkScalar sy) SK_OVERRIDE;
237 virtual bool rotate(SkScalar degrees) SK_OVERRIDE;
238 virtual bool skew(SkScalar sx, SkScalar sy) SK_OVERRIDE;
239 virtual bool concat(const SkMatrix& matrix) SK_OVERRIDE;
240 virtual void setMatrix(const SkMatrix& matrix) SK_OVERRIDE;
reed@google.com4ed0fb72012-12-12 20:48:18 +0000241 virtual bool clipRect(const SkRect&, SkRegion::Op op, bool doAntiAlias = false) SK_OVERRIDE;
242 virtual bool clipRRect(const SkRRect&, SkRegion::Op op, bool doAntiAlias = false) SK_OVERRIDE;
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000243 virtual bool clipPath(const SkPath& path, SkRegion::Op op,
244 bool doAntiAlias = false) SK_OVERRIDE;
245 virtual bool clipRegion(const SkRegion& region, SkRegion::Op op) SK_OVERRIDE;
246 virtual void clear(SkColor) SK_OVERRIDE;
247 virtual void drawPaint(const SkPaint& paint) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000248 virtual void drawPoints(PointMode, size_t count, const SkPoint pts[],
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000249 const SkPaint&) SK_OVERRIDE;
reed@google.com4ed0fb72012-12-12 20:48:18 +0000250 virtual void drawOval(const SkRect&, const SkPaint&) SK_OVERRIDE;
bsalomon@google.com7ce564c2013-10-22 16:54:15 +0000251 virtual void drawRect(const SkRect& rect, const SkPaint&) SK_OVERRIDE;
reed@google.com4ed0fb72012-12-12 20:48:18 +0000252 virtual void drawRRect(const SkRRect&, const SkPaint&) SK_OVERRIDE;
bsalomon@google.com7ce564c2013-10-22 16:54:15 +0000253 virtual void drawPath(const SkPath& path, const SkPaint&) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000254 virtual void drawBitmap(const SkBitmap&, SkScalar left, SkScalar top,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000255 const SkPaint*) SK_OVERRIDE;
reed@google.com71121732012-09-18 15:14:33 +0000256 virtual void drawBitmapRectToRect(const SkBitmap&, const SkRect* src,
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000257 const SkRect& dst, const SkPaint* paint,
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +0000258 DrawBitmapRectFlags flags) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000259 virtual void drawBitmapMatrix(const SkBitmap&, const SkMatrix&,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000260 const SkPaint*) SK_OVERRIDE;
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000261 virtual void drawBitmapNine(const SkBitmap& bitmap, const SkIRect& center,
262 const SkRect& dst, const SkPaint* paint = NULL) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000263 virtual void drawSprite(const SkBitmap&, int left, int top,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000264 const SkPaint*) SK_OVERRIDE;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000265 virtual void drawText(const void* text, size_t byteLength, SkScalar x,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000266 SkScalar y, const SkPaint&) SK_OVERRIDE;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000267 virtual void drawPosText(const void* text, size_t byteLength,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000268 const SkPoint pos[], const SkPaint&) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000269 virtual void drawPosTextH(const void* text, size_t byteLength,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000270 const SkScalar xpos[], SkScalar constY,
271 const SkPaint&) SK_OVERRIDE;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000272 virtual void drawTextOnPath(const void* text, size_t byteLength,
273 const SkPath& path, const SkMatrix* matrix,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000274 const SkPaint&) SK_OVERRIDE;
275 virtual void drawPicture(SkPicture& picture) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000276 virtual void drawVertices(VertexMode, int vertexCount,
277 const SkPoint vertices[], const SkPoint texs[],
278 const SkColor colors[], SkXfermode*,
279 const uint16_t indices[], int indexCount,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000280 const SkPaint&) SK_OVERRIDE;
281 virtual void drawData(const void*, size_t) SK_OVERRIDE;
robertphillips@google.com0a4805e2013-05-29 13:24:23 +0000282 virtual void beginCommentGroup(const char* description) SK_OVERRIDE;
283 virtual void addComment(const char* kywd, const char* value) SK_OVERRIDE;
284 virtual void endCommentGroup() SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000285
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000286 /**
287 * Flatten an SkBitmap to send to the reader, where it will be referenced
288 * according to slot.
289 */
290 bool shuttleBitmap(const SkBitmap&, int32_t slot);
reed@google.combb6992a2011-04-26 17:41:56 +0000291private:
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000292 enum {
293 kNoSaveLayer = -1,
294 };
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000295 SkNamedFactorySet* fFactorySet;
296 int fFirstSaveLayerStackLevel;
scroggo@google.comd9d29672012-08-14 17:21:34 +0000297 SkBitmapHeap* fBitmapHeap;
reed@google.comacd471f2011-05-03 21:26:46 +0000298 SkGPipeController* fController;
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000299 SkWriter32& fWriter;
300 size_t fBlockSize; // amount allocated for writer
301 size_t fBytesNotified;
302 bool fDone;
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000303 const uint32_t fFlags;
reed@google.combb6992a2011-04-26 17:41:56 +0000304
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000305 SkRefCntSet fTypefaceSet;
reed@google.comf5842f72011-05-04 18:30:04 +0000306
307 uint32_t getTypefaceID(SkTypeface*);
308
reed@google.comacd471f2011-05-03 21:26:46 +0000309 inline void writeOp(DrawOps op, unsigned flags, unsigned data) {
reed@google.combb6992a2011-04-26 17:41:56 +0000310 fWriter.write32(DrawOp_packOpFlagData(op, flags, data));
311 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000312
reed@google.comacd471f2011-05-03 21:26:46 +0000313 inline void writeOp(DrawOps op) {
reed@google.combb6992a2011-04-26 17:41:56 +0000314 fWriter.write32(DrawOp_packOpFlagData(op, 0, 0));
315 }
reed@google.comacd471f2011-05-03 21:26:46 +0000316
317 bool needOpBytes(size_t size = 0);
318
319 inline void doNotify() {
320 if (!fDone) {
reed@google.com44699382013-10-31 17:28:30 +0000321 size_t bytes = fWriter.bytesWritten() - fBytesNotified;
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000322 if (bytes > 0) {
323 fController->notifyWritten(bytes);
324 fBytesNotified += bytes;
325 }
reed@google.comacd471f2011-05-03 21:26:46 +0000326 }
327 }
reed@google.comb55d1182011-05-11 00:42:04 +0000328
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000329 // Should be called after any calls to an SkFlatDictionary::findAndReplace
330 // if a new SkFlatData was added when in cross process mode
331 void flattenFactoryNames();
332
scroggo@google.com59c3ab62013-11-12 14:32:38 +0000333 FlattenableHeap fFlattenableHeap;
334 FlatDictionary fFlatDictionary;
335 SkAutoTUnref<BitmapShuttle> fBitmapShuttle;
336 int fCurrFlatIndex[kCount_PaintFlats];
337
reed@google.comb55d1182011-05-11 00:42:04 +0000338 int flattenToIndex(SkFlattenable* obj, PaintFlats);
339
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000340 // Common code used by drawBitmap*. Behaves differently depending on the
341 // type of SkBitmapHeap being used, which is determined by the flags used.
342 bool commonDrawBitmap(const SkBitmap& bm, DrawOps op, unsigned flags,
343 size_t opBytesNeeded, const SkPaint* paint);
scroggo@google.com58be6822012-07-30 14:40:01 +0000344
reed@google.com31891582011-05-12 03:03:56 +0000345 SkPaint fPaint;
346 void writePaint(const SkPaint&);
reed@google.combb6992a2011-04-26 17:41:56 +0000347
reed@google.comacd471f2011-05-03 21:26:46 +0000348 class AutoPipeNotify {
349 public:
350 AutoPipeNotify(SkGPipeCanvas* canvas) : fCanvas(canvas) {}
351 ~AutoPipeNotify() { fCanvas->doNotify(); }
352 private:
353 SkGPipeCanvas* fCanvas;
354 };
355 friend class AutoPipeNotify;
356
reed@google.combb6992a2011-04-26 17:41:56 +0000357 typedef SkCanvas INHERITED;
358};
359
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000360void SkGPipeCanvas::flattenFactoryNames() {
361 const char* name;
362 while ((name = fFactorySet->getNextAddedFactoryName()) != NULL) {
363 size_t len = strlen(name);
364 if (this->needOpBytes(len)) {
365 this->writeOp(kDef_Factory_DrawOp);
366 fWriter.writeString(name, len);
367 }
368 }
369}
370
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000371bool SkGPipeCanvas::shuttleBitmap(const SkBitmap& bm, int32_t slot) {
scroggo@google.com565254b2012-06-28 15:41:32 +0000372 SkASSERT(shouldFlattenBitmaps(fFlags));
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000373 SkWriteBuffer buffer;
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000374 buffer.setNamedFactoryRecorder(fFactorySet);
scroggo@google.com74b7ffd2013-04-30 02:32:41 +0000375 buffer.writeBitmap(bm);
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000376 this->flattenFactoryNames();
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000377 uint32_t size = buffer.bytesWritten();
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000378 if (this->needOpBytes(size)) {
379 this->writeOp(kDef_Bitmap_DrawOp, 0, slot);
380 void* dst = static_cast<void*>(fWriter.reserve(size));
381 buffer.writeToMemory(dst);
382 return true;
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000383 }
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000384 return false;
scroggo@google.comd3ba5cc2012-07-09 16:05:53 +0000385}
386
reed@google.comb55d1182011-05-11 00:42:04 +0000387// return 0 for NULL (or unflattenable obj), or index-base-1
scroggo@google.comd3ba5cc2012-07-09 16:05:53 +0000388// return ~(index-base-1) if an old flattenable was replaced
reed@google.comb55d1182011-05-11 00:42:04 +0000389int SkGPipeCanvas::flattenToIndex(SkFlattenable* obj, PaintFlats paintflat) {
scroggo@google.comd5d158b2012-08-14 20:38:28 +0000390 SkASSERT(!fDone && fBitmapHeap != NULL);
reed@google.comb55d1182011-05-11 00:42:04 +0000391 if (NULL == obj) {
392 return 0;
393 }
reed@google.comb55d1182011-05-11 00:42:04 +0000394
scroggo@google.comd9d29672012-08-14 17:21:34 +0000395 fBitmapHeap->deferAddingOwners();
scroggo@google.com4dffc592012-07-17 16:49:40 +0000396 bool added, replaced;
scroggo@google.com664fab12012-08-14 19:22:05 +0000397 const SkFlatData* flat = fFlatDictionary.findAndReplace(*obj, fFlattenableHeap.flatToReplace(),
398 &added, &replaced);
scroggo@google.comd9d29672012-08-14 17:21:34 +0000399 fBitmapHeap->endAddingOwnersDeferral(added);
scroggo@google.com4dffc592012-07-17 16:49:40 +0000400 int index = flat->index();
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000401 if (added) {
402 if (isCrossProcess(fFlags)) {
403 this->flattenFactoryNames();
404 }
405 size_t flatSize = flat->flatSize();
406 if (this->needOpBytes(flatSize)) {
407 this->writeOp(kDef_Flattenable_DrawOp, paintflat, index);
408 fWriter.write(flat->data(), flatSize);
409 }
scroggo@google.com4dffc592012-07-17 16:49:40 +0000410 }
411 if (replaced) {
reed@google.comb55d1182011-05-11 00:42:04 +0000412 index = ~index;
reed@google.comb55d1182011-05-11 00:42:04 +0000413 }
scroggo@google.com4dffc592012-07-17 16:49:40 +0000414 return index;
reed@google.comb55d1182011-05-11 00:42:04 +0000415}
416
reed@google.combb6992a2011-04-26 17:41:56 +0000417///////////////////////////////////////////////////////////////////////////////
418
reed@google.comacd471f2011-05-03 21:26:46 +0000419#define MIN_BLOCK_SIZE (16 * 1024)
scroggo@google.com4dffc592012-07-17 16:49:40 +0000420#define BITMAPS_TO_KEEP 5
421#define FLATTENABLES_TO_KEEP 10
reed@google.combb6992a2011-04-26 17:41:56 +0000422
reed@google.comacd471f2011-05-03 21:26:46 +0000423SkGPipeCanvas::SkGPipeCanvas(SkGPipeController* controller,
rmistry@google.comd6176b02012-08-23 18:14:13 +0000424 SkWriter32* writer, uint32_t flags,
junov@chromium.orga8db8fe2012-08-15 19:49:22 +0000425 uint32_t width, uint32_t height)
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000426: fFactorySet(isCrossProcess(flags) ? SkNEW(SkNamedFactorySet) : NULL)
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000427, fWriter(*writer)
428, fFlags(flags)
scroggo@google.com664fab12012-08-14 19:22:05 +0000429, fFlattenableHeap(FLATTENABLES_TO_KEEP, fFactorySet, isCrossProcess(flags))
scroggo@google.com15543602012-08-02 18:49:49 +0000430, fFlatDictionary(&fFlattenableHeap) {
reed@google.comacd471f2011-05-03 21:26:46 +0000431 fController = controller;
reed@google.combb6992a2011-04-26 17:41:56 +0000432 fDone = false;
reed@google.comacd471f2011-05-03 21:26:46 +0000433 fBlockSize = 0; // need first block from controller
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000434 fBytesNotified = 0;
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000435 fFirstSaveLayerStackLevel = kNoSaveLayer;
reed@google.comb55d1182011-05-11 00:42:04 +0000436 sk_bzero(fCurrFlatIndex, sizeof(fCurrFlatIndex));
reed@google.comacd471f2011-05-03 21:26:46 +0000437
reed@google.combb6793b2011-05-05 15:18:15 +0000438 // we need a device to limit our clip
yangsu@google.com06b4da12011-06-17 15:04:40 +0000439 // We don't allocate pixels for the bitmap
reed@google.combb6793b2011-05-05 15:18:15 +0000440 SkBitmap bitmap;
junov@chromium.orga8db8fe2012-08-15 19:49:22 +0000441 bitmap.setConfig(SkBitmap::kARGB_8888_Config, width, height);
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000442 SkBaseDevice* device = SkNEW_ARGS(SkBitmapDevice, (bitmap));
reed@google.combb6793b2011-05-05 15:18:15 +0000443 this->setDevice(device)->unref();
scroggo@google.com10dccde2012-08-08 20:43:22 +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();
481 size_t blockSize = SkMax32(MIN_BLOCK_SIZE, needed);
482 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
reed@google.combb6992a2011-04-26 17:41:56 +0000516int SkGPipeCanvas::save(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 }
reed@google.combb6992a2011-04-26 17:41:56 +0000521 return this->INHERITED::save(flags);
522}
523
524int SkGPipeCanvas::saveLayer(const SkRect* bounds, const SkPaint* paint,
525 SaveFlags saveFlags) {
reed@google.comacd471f2011-05-03 21:26:46 +0000526 NOTIFY_SETUP(this);
527 size_t size = 0;
reed@google.combb6992a2011-04-26 17:41:56 +0000528 unsigned opFlags = 0;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000529
reed@google.combb6992a2011-04-26 17:41:56 +0000530 if (bounds) {
531 opFlags |= kSaveLayer_HasBounds_DrawOpFlag;
reed@google.comacd471f2011-05-03 21:26:46 +0000532 size += sizeof(SkRect);
reed@google.combb6992a2011-04-26 17:41:56 +0000533 }
534 if (paint) {
535 opFlags |= kSaveLayer_HasPaint_DrawOpFlag;
reed@google.com31891582011-05-12 03:03:56 +0000536 this->writePaint(*paint);
reed@google.combb6992a2011-04-26 17:41:56 +0000537 }
538
reed@google.comacd471f2011-05-03 21:26:46 +0000539 if (this->needOpBytes(size)) {
540 this->writeOp(kSaveLayer_DrawOp, opFlags, saveFlags);
541 if (bounds) {
542 fWriter.writeRect(*bounds);
543 }
reed@google.combb6992a2011-04-26 17:41:56 +0000544 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000545
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000546 if (kNoSaveLayer == fFirstSaveLayerStackLevel){
547 fFirstSaveLayerStackLevel = this->getSaveCount();
548 }
reed@google.combb6992a2011-04-26 17:41:56 +0000549 // we just pass on the save, so we don't create a layer
550 return this->INHERITED::save(saveFlags);
551}
552
553void SkGPipeCanvas::restore() {
reed@google.comacd471f2011-05-03 21:26:46 +0000554 NOTIFY_SETUP(this);
555 if (this->needOpBytes()) {
556 this->writeOp(kRestore_DrawOp);
557 }
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000558
reed@google.combb6992a2011-04-26 17:41:56 +0000559 this->INHERITED::restore();
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000560
561 if (this->getSaveCount() == fFirstSaveLayerStackLevel){
562 fFirstSaveLayerStackLevel = kNoSaveLayer;
563 }
564}
565
566bool SkGPipeCanvas::isDrawingToLayer() const {
567 return kNoSaveLayer != fFirstSaveLayerStackLevel;
reed@google.combb6992a2011-04-26 17:41:56 +0000568}
569
570bool SkGPipeCanvas::translate(SkScalar dx, SkScalar dy) {
571 if (dx || dy) {
reed@google.comacd471f2011-05-03 21:26:46 +0000572 NOTIFY_SETUP(this);
573 if (this->needOpBytes(2 * sizeof(SkScalar))) {
574 this->writeOp(kTranslate_DrawOp);
575 fWriter.writeScalar(dx);
576 fWriter.writeScalar(dy);
577 }
reed@google.combb6992a2011-04-26 17:41:56 +0000578 }
579 return this->INHERITED::translate(dx, dy);
580}
581
582bool SkGPipeCanvas::scale(SkScalar sx, SkScalar sy) {
583 if (sx || sy) {
reed@google.comacd471f2011-05-03 21:26:46 +0000584 NOTIFY_SETUP(this);
585 if (this->needOpBytes(2 * sizeof(SkScalar))) {
586 this->writeOp(kScale_DrawOp);
587 fWriter.writeScalar(sx);
588 fWriter.writeScalar(sy);
589 }
reed@google.combb6992a2011-04-26 17:41:56 +0000590 }
591 return this->INHERITED::scale(sx, sy);
592}
593
594bool SkGPipeCanvas::rotate(SkScalar degrees) {
595 if (degrees) {
reed@google.comacd471f2011-05-03 21:26:46 +0000596 NOTIFY_SETUP(this);
597 if (this->needOpBytes(sizeof(SkScalar))) {
598 this->writeOp(kRotate_DrawOp);
599 fWriter.writeScalar(degrees);
600 }
reed@google.combb6992a2011-04-26 17:41:56 +0000601 }
602 return this->INHERITED::rotate(degrees);
603}
604
605bool SkGPipeCanvas::skew(SkScalar sx, SkScalar sy) {
606 if (sx || sy) {
reed@google.comacd471f2011-05-03 21:26:46 +0000607 NOTIFY_SETUP(this);
608 if (this->needOpBytes(2 * sizeof(SkScalar))) {
609 this->writeOp(kSkew_DrawOp);
610 fWriter.writeScalar(sx);
611 fWriter.writeScalar(sy);
612 }
reed@google.combb6992a2011-04-26 17:41:56 +0000613 }
614 return this->INHERITED::skew(sx, sy);
615}
616
617bool SkGPipeCanvas::concat(const SkMatrix& matrix) {
618 if (!matrix.isIdentity()) {
reed@google.comacd471f2011-05-03 21:26:46 +0000619 NOTIFY_SETUP(this);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000620 if (this->needOpBytes(matrix.writeToMemory(NULL))) {
reed@google.comacd471f2011-05-03 21:26:46 +0000621 this->writeOp(kConcat_DrawOp);
djsollen@google.com2b2ede32012-04-12 13:24:04 +0000622 fWriter.writeMatrix(matrix);
reed@google.comacd471f2011-05-03 21:26:46 +0000623 }
reed@google.combb6992a2011-04-26 17:41:56 +0000624 }
625 return this->INHERITED::concat(matrix);
626}
627
628void SkGPipeCanvas::setMatrix(const SkMatrix& matrix) {
reed@google.comacd471f2011-05-03 21:26:46 +0000629 NOTIFY_SETUP(this);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000630 if (this->needOpBytes(matrix.writeToMemory(NULL))) {
reed@google.comacd471f2011-05-03 21:26:46 +0000631 this->writeOp(kSetMatrix_DrawOp);
djsollen@google.com2b2ede32012-04-12 13:24:04 +0000632 fWriter.writeMatrix(matrix);
reed@google.comacd471f2011-05-03 21:26:46 +0000633 }
reed@google.combb6992a2011-04-26 17:41:56 +0000634 this->INHERITED::setMatrix(matrix);
635}
636
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000637bool SkGPipeCanvas::clipRect(const SkRect& rect, SkRegion::Op rgnOp,
638 bool doAntiAlias) {
reed@google.comacd471f2011-05-03 21:26:46 +0000639 NOTIFY_SETUP(this);
scroggo@google.com460a23e2012-08-16 17:56:49 +0000640 if (this->needOpBytes(sizeof(SkRect))) {
641 unsigned flags = doAntiAlias & kClip_HasAntiAlias_DrawOpFlag;
642 this->writeOp(kClipRect_DrawOp, flags, rgnOp);
reed@google.comacd471f2011-05-03 21:26:46 +0000643 fWriter.writeRect(rect);
644 }
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000645 return this->INHERITED::clipRect(rect, rgnOp, doAntiAlias);
reed@google.combb6992a2011-04-26 17:41:56 +0000646}
647
reed@google.com4ed0fb72012-12-12 20:48:18 +0000648bool SkGPipeCanvas::clipRRect(const SkRRect& rrect, SkRegion::Op rgnOp,
649 bool doAntiAlias) {
650 NOTIFY_SETUP(this);
651 if (this->needOpBytes(kSizeOfFlatRRect)) {
652 unsigned flags = doAntiAlias & kClip_HasAntiAlias_DrawOpFlag;
653 this->writeOp(kClipRRect_DrawOp, flags, rgnOp);
654 fWriter.writeRRect(rrect);
655 }
656 return this->INHERITED::clipRRect(rrect, rgnOp, doAntiAlias);
657}
658
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000659bool SkGPipeCanvas::clipPath(const SkPath& path, SkRegion::Op rgnOp,
660 bool doAntiAlias) {
reed@google.comacd471f2011-05-03 21:26:46 +0000661 NOTIFY_SETUP(this);
scroggo@google.com460a23e2012-08-16 17:56:49 +0000662 if (this->needOpBytes(path.writeToMemory(NULL))) {
663 unsigned flags = doAntiAlias & kClip_HasAntiAlias_DrawOpFlag;
664 this->writeOp(kClipPath_DrawOp, flags, rgnOp);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000665 fWriter.writePath(path);
reed@google.comacd471f2011-05-03 21:26:46 +0000666 }
reed@google.combb6992a2011-04-26 17:41:56 +0000667 // we just pass on the bounds of the path
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000668 return this->INHERITED::clipRect(path.getBounds(), rgnOp, doAntiAlias);
reed@google.combb6992a2011-04-26 17:41:56 +0000669}
670
671bool SkGPipeCanvas::clipRegion(const SkRegion& region, SkRegion::Op rgnOp) {
reed@google.comacd471f2011-05-03 21:26:46 +0000672 NOTIFY_SETUP(this);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000673 if (this->needOpBytes(region.writeToMemory(NULL))) {
reed@google.comacd471f2011-05-03 21:26:46 +0000674 this->writeOp(kClipRegion_DrawOp, 0, rgnOp);
djsollen@google.com2b2ede32012-04-12 13:24:04 +0000675 fWriter.writeRegion(region);
reed@google.comacd471f2011-05-03 21:26:46 +0000676 }
reed@google.combb6992a2011-04-26 17:41:56 +0000677 return this->INHERITED::clipRegion(region, rgnOp);
678}
679
680///////////////////////////////////////////////////////////////////////////////
681
682void SkGPipeCanvas::clear(SkColor color) {
reed@google.comacd471f2011-05-03 21:26:46 +0000683 NOTIFY_SETUP(this);
reed@google.combb6992a2011-04-26 17:41:56 +0000684 unsigned flags = 0;
685 if (color) {
686 flags |= kClear_HasColor_DrawOpFlag;
687 }
reed@google.comacd471f2011-05-03 21:26:46 +0000688 if (this->needOpBytes(sizeof(SkColor))) {
689 this->writeOp(kDrawClear_DrawOp, flags, 0);
690 if (color) {
691 fWriter.write32(color);
692 }
reed@google.combb6992a2011-04-26 17:41:56 +0000693 }
694}
695
696void SkGPipeCanvas::drawPaint(const SkPaint& paint) {
reed@google.comacd471f2011-05-03 21:26:46 +0000697 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000698 this->writePaint(paint);
reed@google.comacd471f2011-05-03 21:26:46 +0000699 if (this->needOpBytes()) {
reed@google.com31891582011-05-12 03:03:56 +0000700 this->writeOp(kDrawPaint_DrawOp);
reed@google.comacd471f2011-05-03 21:26:46 +0000701 }
reed@google.combb6992a2011-04-26 17:41:56 +0000702}
703
704void SkGPipeCanvas::drawPoints(PointMode mode, size_t count,
robertphillips@google.com8b169312013-10-15 17:47:36 +0000705 const SkPoint pts[], const SkPaint& paint) {
reed@google.combb6992a2011-04-26 17:41:56 +0000706 if (count) {
reed@google.comacd471f2011-05-03 21:26:46 +0000707 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000708 this->writePaint(paint);
reed@google.comacd471f2011-05-03 21:26:46 +0000709 if (this->needOpBytes(4 + count * sizeof(SkPoint))) {
reed@google.com31891582011-05-12 03:03:56 +0000710 this->writeOp(kDrawPoints_DrawOp, mode, 0);
reed@google.comacd471f2011-05-03 21:26:46 +0000711 fWriter.write32(count);
712 fWriter.write(pts, count * sizeof(SkPoint));
713 }
reed@google.combb6992a2011-04-26 17:41:56 +0000714 }
715}
716
reed@google.com4ed0fb72012-12-12 20:48:18 +0000717void SkGPipeCanvas::drawOval(const SkRect& rect, const SkPaint& paint) {
718 NOTIFY_SETUP(this);
719 this->writePaint(paint);
720 if (this->needOpBytes(sizeof(SkRect))) {
721 this->writeOp(kDrawOval_DrawOp);
722 fWriter.writeRect(rect);
723 }
724}
725
bsalomon@google.com7ce564c2013-10-22 16:54:15 +0000726void SkGPipeCanvas::drawRect(const SkRect& rect, const SkPaint& paint) {
reed@google.comacd471f2011-05-03 21:26:46 +0000727 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000728 this->writePaint(paint);
reed@google.comacd471f2011-05-03 21:26:46 +0000729 if (this->needOpBytes(sizeof(SkRect))) {
reed@google.com31891582011-05-12 03:03:56 +0000730 this->writeOp(kDrawRect_DrawOp);
reed@google.comacd471f2011-05-03 21:26:46 +0000731 fWriter.writeRect(rect);
732 }
reed@google.combb6992a2011-04-26 17:41:56 +0000733}
734
reed@google.com4ed0fb72012-12-12 20:48:18 +0000735void SkGPipeCanvas::drawRRect(const SkRRect& rrect, const SkPaint& paint) {
736 NOTIFY_SETUP(this);
737 this->writePaint(paint);
738 if (this->needOpBytes(kSizeOfFlatRRect)) {
739 this->writeOp(kDrawRRect_DrawOp);
740 fWriter.writeRRect(rrect);
741 }
742}
743
bsalomon@google.com7ce564c2013-10-22 16:54:15 +0000744void SkGPipeCanvas::drawPath(const SkPath& path, const SkPaint& paint) {
reed@google.comacd471f2011-05-03 21:26:46 +0000745 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000746 this->writePaint(paint);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000747 if (this->needOpBytes(path.writeToMemory(NULL))) {
reed@google.com31891582011-05-12 03:03:56 +0000748 this->writeOp(kDrawPath_DrawOp);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000749 fWriter.writePath(path);
reed@google.comacd471f2011-05-03 21:26:46 +0000750 }
reed@google.combb6992a2011-04-26 17:41:56 +0000751}
752
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000753bool SkGPipeCanvas::commonDrawBitmap(const SkBitmap& bm, DrawOps op,
754 unsigned flags,
755 size_t opBytesNeeded,
756 const SkPaint* paint) {
scroggo@google.com58be6822012-07-30 14:40:01 +0000757 if (paint != NULL) {
scroggo@google.com460a23e2012-08-16 17:56:49 +0000758 flags |= kDrawBitmap_HasPaint_DrawOpFlag;
scroggo@google.com58be6822012-07-30 14:40:01 +0000759 this->writePaint(*paint);
760 }
scroggo@google.com10dccde2012-08-08 20:43:22 +0000761 if (this->needOpBytes(opBytesNeeded)) {
scroggo@google.comd5d158b2012-08-14 20:38:28 +0000762 SkASSERT(fBitmapHeap != NULL);
763 int32_t bitmapIndex = fBitmapHeap->insert(bm);
764 if (SkBitmapHeap::INVALID_SLOT == bitmapIndex) {
765 return false;
766 }
scroggo@google.com10dccde2012-08-08 20:43:22 +0000767 this->writeOp(op, flags, bitmapIndex);
scroggo@google.com58be6822012-07-30 14:40:01 +0000768 return true;
769 }
770 return false;
771}
772
773void SkGPipeCanvas::drawBitmap(const SkBitmap& bm, SkScalar left, SkScalar top,
774 const SkPaint* paint) {
775 NOTIFY_SETUP(this);
776 size_t opBytesNeeded = sizeof(SkScalar) * 2;
777
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000778 if (this->commonDrawBitmap(bm, kDrawBitmap_DrawOp, 0, opBytesNeeded, paint)) {
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000779 fWriter.writeScalar(left);
780 fWriter.writeScalar(top);
781 }
reed@google.combb6992a2011-04-26 17:41:56 +0000782}
783
reed@google.com71121732012-09-18 15:14:33 +0000784void SkGPipeCanvas::drawBitmapRectToRect(const SkBitmap& bm, const SkRect* src,
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +0000785 const SkRect& dst, const SkPaint* paint,
786 DrawBitmapRectFlags dbmrFlags) {
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000787 NOTIFY_SETUP(this);
scroggo@google.com58be6822012-07-30 14:40:01 +0000788 size_t opBytesNeeded = sizeof(SkRect);
789 bool hasSrc = src != NULL;
790 unsigned flags;
791 if (hasSrc) {
scroggo@google.com460a23e2012-08-16 17:56:49 +0000792 flags = kDrawBitmap_HasSrcRect_DrawOpFlag;
scroggo@google.com58be6822012-07-30 14:40:01 +0000793 opBytesNeeded += sizeof(int32_t) * 4;
794 } else {
795 flags = 0;
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000796 }
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +0000797 if (dbmrFlags & kBleed_DrawBitmapRectFlag) {
798 flags |= kDrawBitmap_Bleed_DrawOpFlag;
799 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000800
reed@google.com71121732012-09-18 15:14:33 +0000801 if (this->commonDrawBitmap(bm, kDrawBitmapRectToRect_DrawOp, flags, opBytesNeeded, paint)) {
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000802 if (hasSrc) {
reed@google.com71121732012-09-18 15:14:33 +0000803 fWriter.writeRect(*src);
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000804 }
805 fWriter.writeRect(dst);
806 }
reed@google.combb6992a2011-04-26 17:41:56 +0000807}
808
scroggo@google.com58be6822012-07-30 14:40:01 +0000809void SkGPipeCanvas::drawBitmapMatrix(const SkBitmap& bm, const SkMatrix& matrix,
810 const SkPaint* paint) {
811 NOTIFY_SETUP(this);
812 size_t opBytesNeeded = matrix.writeToMemory(NULL);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000813
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000814 if (this->commonDrawBitmap(bm, kDrawBitmapMatrix_DrawOp, 0, opBytesNeeded, paint)) {
scroggo@google.com58be6822012-07-30 14:40:01 +0000815 fWriter.writeMatrix(matrix);
816 }
reed@google.combb6992a2011-04-26 17:41:56 +0000817}
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000818
819void SkGPipeCanvas::drawBitmapNine(const SkBitmap& bm, const SkIRect& center,
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000820 const SkRect& dst, const SkPaint* paint) {
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000821 NOTIFY_SETUP(this);
scroggo@google.com58be6822012-07-30 14:40:01 +0000822 size_t opBytesNeeded = sizeof(int32_t) * 4 + sizeof(SkRect);
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000823
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000824 if (this->commonDrawBitmap(bm, kDrawBitmapNine_DrawOp, 0, opBytesNeeded, paint)) {
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000825 fWriter.write32(center.fLeft);
826 fWriter.write32(center.fTop);
827 fWriter.write32(center.fRight);
828 fWriter.write32(center.fBottom);
829 fWriter.writeRect(dst);
830 }
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000831}
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000832
833void SkGPipeCanvas::drawSprite(const SkBitmap& bm, int left, int top,
834 const SkPaint* paint) {
835 NOTIFY_SETUP(this);
scroggo@google.com58be6822012-07-30 14:40:01 +0000836 size_t opBytesNeeded = sizeof(int32_t) * 2;
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000837
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000838 if (this->commonDrawBitmap(bm, kDrawSprite_DrawOp, 0, opBytesNeeded, paint)) {
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000839 fWriter.write32(left);
840 fWriter.write32(top);
841 }
reed@google.combb6992a2011-04-26 17:41:56 +0000842}
843
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000844void SkGPipeCanvas::drawText(const void* text, size_t byteLength, SkScalar x,
reed@google.combb6992a2011-04-26 17:41:56 +0000845 SkScalar y, const SkPaint& paint) {
846 if (byteLength) {
reed@google.comacd471f2011-05-03 21:26:46 +0000847 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000848 this->writePaint(paint);
reed@google.comacd471f2011-05-03 21:26:46 +0000849 if (this->needOpBytes(4 + SkAlign4(byteLength) + 2 * sizeof(SkScalar))) {
reed@google.com31891582011-05-12 03:03:56 +0000850 this->writeOp(kDrawText_DrawOp);
reed@google.comacd471f2011-05-03 21:26:46 +0000851 fWriter.write32(byteLength);
852 fWriter.writePad(text, byteLength);
853 fWriter.writeScalar(x);
854 fWriter.writeScalar(y);
855 }
reed@google.combb6992a2011-04-26 17:41:56 +0000856 }
857}
858
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000859void SkGPipeCanvas::drawPosText(const void* text, size_t byteLength,
reed@google.combb6992a2011-04-26 17:41:56 +0000860 const SkPoint pos[], const SkPaint& paint) {
861 if (byteLength) {
reed@google.comacd471f2011-05-03 21:26:46 +0000862 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000863 this->writePaint(paint);
reed@google.combb6992a2011-04-26 17:41:56 +0000864 int count = paint.textToGlyphs(text, byteLength, NULL);
reed@google.comacd471f2011-05-03 21:26:46 +0000865 if (this->needOpBytes(4 + SkAlign4(byteLength) + 4 + count * sizeof(SkPoint))) {
reed@google.com31891582011-05-12 03:03:56 +0000866 this->writeOp(kDrawPosText_DrawOp);
reed@google.comacd471f2011-05-03 21:26:46 +0000867 fWriter.write32(byteLength);
868 fWriter.writePad(text, byteLength);
869 fWriter.write32(count);
870 fWriter.write(pos, count * sizeof(SkPoint));
871 }
reed@google.combb6992a2011-04-26 17:41:56 +0000872 }
873}
874
875void SkGPipeCanvas::drawPosTextH(const void* text, size_t byteLength,
876 const SkScalar xpos[], SkScalar constY,
877 const SkPaint& paint) {
878 if (byteLength) {
reed@google.comacd471f2011-05-03 21:26:46 +0000879 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000880 this->writePaint(paint);
reed@google.combb6992a2011-04-26 17:41:56 +0000881 int count = paint.textToGlyphs(text, byteLength, NULL);
reed@google.comacd471f2011-05-03 21:26:46 +0000882 if (this->needOpBytes(4 + SkAlign4(byteLength) + 4 + count * sizeof(SkScalar) + 4)) {
reed@google.com31891582011-05-12 03:03:56 +0000883 this->writeOp(kDrawPosTextH_DrawOp);
reed@google.comacd471f2011-05-03 21:26:46 +0000884 fWriter.write32(byteLength);
885 fWriter.writePad(text, byteLength);
886 fWriter.write32(count);
887 fWriter.write(xpos, count * sizeof(SkScalar));
888 fWriter.writeScalar(constY);
889 }
reed@google.combb6992a2011-04-26 17:41:56 +0000890 }
891}
892
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000893void SkGPipeCanvas::drawTextOnPath(const void* text, size_t byteLength,
894 const SkPath& path, const SkMatrix* matrix,
reed@google.combb6992a2011-04-26 17:41:56 +0000895 const SkPaint& paint) {
896 if (byteLength) {
reed@google.comacd471f2011-05-03 21:26:46 +0000897 NOTIFY_SETUP(this);
reed@google.combb6992a2011-04-26 17:41:56 +0000898 unsigned flags = 0;
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000899 size_t size = 4 + SkAlign4(byteLength) + path.writeToMemory(NULL);
reed@google.combb6992a2011-04-26 17:41:56 +0000900 if (matrix) {
901 flags |= kDrawTextOnPath_HasMatrix_DrawOpFlag;
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000902 size += matrix->writeToMemory(NULL);
reed@google.combb6992a2011-04-26 17:41:56 +0000903 }
reed@google.com31891582011-05-12 03:03:56 +0000904 this->writePaint(paint);
reed@google.comacd471f2011-05-03 21:26:46 +0000905 if (this->needOpBytes(size)) {
reed@google.com31891582011-05-12 03:03:56 +0000906 this->writeOp(kDrawTextOnPath_DrawOp, flags, 0);
reed@google.combb6992a2011-04-26 17:41:56 +0000907
reed@google.comacd471f2011-05-03 21:26:46 +0000908 fWriter.write32(byteLength);
909 fWriter.writePad(text, byteLength);
reed@google.combb6992a2011-04-26 17:41:56 +0000910
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000911 fWriter.writePath(path);
reed@google.comacd471f2011-05-03 21:26:46 +0000912 if (matrix) {
djsollen@google.com2b2ede32012-04-12 13:24:04 +0000913 fWriter.writeMatrix(*matrix);
reed@google.comacd471f2011-05-03 21:26:46 +0000914 }
reed@google.combb6992a2011-04-26 17:41:56 +0000915 }
916 }
917}
918
919void SkGPipeCanvas::drawPicture(SkPicture& picture) {
reed@google.com0faac1e2011-05-11 05:58:58 +0000920 // we want to playback the picture into individual draw calls
921 this->INHERITED::drawPicture(picture);
reed@google.combb6992a2011-04-26 17:41:56 +0000922}
923
reed@google.com85e143c2013-12-30 15:51:25 +0000924void SkGPipeCanvas::drawVertices(VertexMode vmode, int vertexCount,
reed@google.combb6992a2011-04-26 17:41:56 +0000925 const SkPoint vertices[], const SkPoint texs[],
reed@google.com85e143c2013-12-30 15:51:25 +0000926 const SkColor colors[], SkXfermode* xfer,
reed@google.combb6992a2011-04-26 17:41:56 +0000927 const uint16_t indices[], int indexCount,
928 const SkPaint& paint) {
929 if (0 == vertexCount) {
930 return;
931 }
932
reed@google.comacd471f2011-05-03 21:26:46 +0000933 NOTIFY_SETUP(this);
934 size_t size = 4 + vertexCount * sizeof(SkPoint);
reed@google.com31891582011-05-12 03:03:56 +0000935 this->writePaint(paint);
reed@google.combb6992a2011-04-26 17:41:56 +0000936 unsigned flags = 0;
937 if (texs) {
938 flags |= kDrawVertices_HasTexs_DrawOpFlag;
reed@google.comacd471f2011-05-03 21:26:46 +0000939 size += vertexCount * sizeof(SkPoint);
reed@google.combb6992a2011-04-26 17:41:56 +0000940 }
941 if (colors) {
942 flags |= kDrawVertices_HasColors_DrawOpFlag;
reed@google.comacd471f2011-05-03 21:26:46 +0000943 size += vertexCount * sizeof(SkColor);
reed@google.combb6992a2011-04-26 17:41:56 +0000944 }
945 if (indices && indexCount > 0) {
946 flags |= kDrawVertices_HasIndices_DrawOpFlag;
reed@google.comacd471f2011-05-03 21:26:46 +0000947 size += 4 + SkAlign4(indexCount * sizeof(uint16_t));
reed@google.combb6992a2011-04-26 17:41:56 +0000948 }
reed@google.com85e143c2013-12-30 15:51:25 +0000949 if (xfer && !SkXfermode::IsMode(xfer, SkXfermode::kModulate_Mode)) {
950 flags |= kDrawVertices_HasXfermode_DrawOpFlag;
951 size += sizeof(int32_t); // mode enum
952 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000953
reed@google.comacd471f2011-05-03 21:26:46 +0000954 if (this->needOpBytes(size)) {
reed@google.com31891582011-05-12 03:03:56 +0000955 this->writeOp(kDrawVertices_DrawOp, flags, 0);
reed@google.com85e143c2013-12-30 15:51:25 +0000956 fWriter.write32(vmode);
reed@google.comacd471f2011-05-03 21:26:46 +0000957 fWriter.write32(vertexCount);
958 fWriter.write(vertices, vertexCount * sizeof(SkPoint));
959 if (texs) {
960 fWriter.write(texs, vertexCount * sizeof(SkPoint));
961 }
962 if (colors) {
963 fWriter.write(colors, vertexCount * sizeof(SkColor));
964 }
reed@google.com85e143c2013-12-30 15:51:25 +0000965 if (flags & kDrawVertices_HasXfermode_DrawOpFlag) {
966 SkXfermode::Mode mode = SkXfermode::kModulate_Mode;
967 (void)xfer->asMode(&mode);
968 fWriter.write32(mode);
969 }
reed@google.comacd471f2011-05-03 21:26:46 +0000970 if (indices && indexCount > 0) {
971 fWriter.write32(indexCount);
972 fWriter.writePad(indices, indexCount * sizeof(uint16_t));
973 }
reed@google.combb6992a2011-04-26 17:41:56 +0000974 }
975}
976
reed@google.comacd471f2011-05-03 21:26:46 +0000977void SkGPipeCanvas::drawData(const void* ptr, size_t size) {
978 if (size && ptr) {
979 NOTIFY_SETUP(this);
reed@google.combb6992a2011-04-26 17:41:56 +0000980 unsigned data = 0;
981 if (size < (1 << DRAWOPS_DATA_BITS)) {
982 data = (unsigned)size;
983 }
reed@google.comacd471f2011-05-03 21:26:46 +0000984 if (this->needOpBytes(4 + SkAlign4(size))) {
985 this->writeOp(kDrawData_DrawOp, 0, data);
986 if (0 == data) {
987 fWriter.write32(size);
988 }
reed@google.combb6793b2011-05-05 15:18:15 +0000989 fWriter.writePad(ptr, size);
reed@google.combb6992a2011-04-26 17:41:56 +0000990 }
991 }
992}
993
robertphillips@google.com0a4805e2013-05-29 13:24:23 +0000994void SkGPipeCanvas::beginCommentGroup(const char* description) {
995 // ignore for now
996}
997
998void SkGPipeCanvas::addComment(const char* kywd, const char* value) {
999 // ignore for now
1000}
1001
1002void SkGPipeCanvas::endCommentGroup() {
1003 // ignore for now
1004}
1005
junov@chromium.org77eec242012-07-18 17:54:45 +00001006void SkGPipeCanvas::flushRecording(bool detachCurrentBlock) {
1007 doNotify();
1008 if (detachCurrentBlock) {
1009 // force a new block to be requested for the next recorded command
rmistry@google.comd6176b02012-08-23 18:14:13 +00001010 fBlockSize = 0;
junov@chromium.org77eec242012-07-18 17:54:45 +00001011 }
1012}
1013
junov@chromium.org2e14ba82012-08-07 14:26:57 +00001014size_t SkGPipeCanvas::freeMemoryIfPossible(size_t bytesToFree) {
scroggo@google.comd5d158b2012-08-14 20:38:28 +00001015 return (NULL == fBitmapHeap) ? 0 : fBitmapHeap->freeMemoryIfPossible(bytesToFree);
junov@chromium.org2e14ba82012-08-07 14:26:57 +00001016}
1017
reed@google.combb6992a2011-04-26 17:41:56 +00001018///////////////////////////////////////////////////////////////////////////////
1019
1020template <typename T> uint32_t castToU32(T value) {
1021 union {
1022 T fSrc;
1023 uint32_t fDst;
1024 } data;
1025 data.fSrc = value;
1026 return data.fDst;
1027}
1028
reed@google.com31891582011-05-12 03:03:56 +00001029void SkGPipeCanvas::writePaint(const SkPaint& paint) {
scroggo@google.comd5d158b2012-08-14 20:38:28 +00001030 if (fDone) {
1031 return;
1032 }
reed@google.com31891582011-05-12 03:03:56 +00001033 SkPaint& base = fPaint;
reed@google.combb6992a2011-04-26 17:41:56 +00001034 uint32_t storage[32];
1035 uint32_t* ptr = storage;
reed@google.combb6992a2011-04-26 17:41:56 +00001036
1037 if (base.getFlags() != paint.getFlags()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001038 *ptr++ = PaintOp_packOpData(kFlags_PaintOp, paint.getFlags());
reed@google.comf5842f72011-05-04 18:30:04 +00001039 base.setFlags(paint.getFlags());
reed@google.combb6992a2011-04-26 17:41:56 +00001040 }
1041 if (base.getColor() != paint.getColor()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001042 *ptr++ = PaintOp_packOp(kColor_PaintOp);
1043 *ptr++ = paint.getColor();
reed@google.comf5842f72011-05-04 18:30:04 +00001044 base.setColor(paint.getColor());
reed@google.combb6992a2011-04-26 17:41:56 +00001045 }
1046 if (base.getStyle() != paint.getStyle()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001047 *ptr++ = PaintOp_packOpData(kStyle_PaintOp, paint.getStyle());
reed@google.comf5842f72011-05-04 18:30:04 +00001048 base.setStyle(paint.getStyle());
reed@google.combb6992a2011-04-26 17:41:56 +00001049 }
1050 if (base.getStrokeJoin() != paint.getStrokeJoin()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001051 *ptr++ = PaintOp_packOpData(kJoin_PaintOp, paint.getStrokeJoin());
reed@google.comf5842f72011-05-04 18:30:04 +00001052 base.setStrokeJoin(paint.getStrokeJoin());
reed@google.combb6992a2011-04-26 17:41:56 +00001053 }
1054 if (base.getStrokeCap() != paint.getStrokeCap()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001055 *ptr++ = PaintOp_packOpData(kCap_PaintOp, paint.getStrokeCap());
reed@google.comf5842f72011-05-04 18:30:04 +00001056 base.setStrokeCap(paint.getStrokeCap());
reed@google.combb6992a2011-04-26 17:41:56 +00001057 }
1058 if (base.getStrokeWidth() != paint.getStrokeWidth()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001059 *ptr++ = PaintOp_packOp(kWidth_PaintOp);
1060 *ptr++ = castToU32(paint.getStrokeWidth());
reed@google.comf5842f72011-05-04 18:30:04 +00001061 base.setStrokeWidth(paint.getStrokeWidth());
reed@google.combb6992a2011-04-26 17:41:56 +00001062 }
1063 if (base.getStrokeMiter() != paint.getStrokeMiter()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001064 *ptr++ = PaintOp_packOp(kMiter_PaintOp);
1065 *ptr++ = castToU32(paint.getStrokeMiter());
reed@google.comf5842f72011-05-04 18:30:04 +00001066 base.setStrokeMiter(paint.getStrokeMiter());
reed@google.combb6992a2011-04-26 17:41:56 +00001067 }
1068 if (base.getTextEncoding() != paint.getTextEncoding()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001069 *ptr++ = PaintOp_packOpData(kEncoding_PaintOp, paint.getTextEncoding());
reed@google.comf5842f72011-05-04 18:30:04 +00001070 base.setTextEncoding(paint.getTextEncoding());
reed@google.combb6992a2011-04-26 17:41:56 +00001071 }
1072 if (base.getHinting() != paint.getHinting()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001073 *ptr++ = PaintOp_packOpData(kHinting_PaintOp, paint.getHinting());
reed@google.comf5842f72011-05-04 18:30:04 +00001074 base.setHinting(paint.getHinting());
reed@google.combb6992a2011-04-26 17:41:56 +00001075 }
1076 if (base.getTextAlign() != paint.getTextAlign()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001077 *ptr++ = PaintOp_packOpData(kAlign_PaintOp, paint.getTextAlign());
reed@google.comf5842f72011-05-04 18:30:04 +00001078 base.setTextAlign(paint.getTextAlign());
reed@google.combb6992a2011-04-26 17:41:56 +00001079 }
1080 if (base.getTextSize() != paint.getTextSize()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001081 *ptr++ = PaintOp_packOp(kTextSize_PaintOp);
1082 *ptr++ = castToU32(paint.getTextSize());
reed@google.comf5842f72011-05-04 18:30:04 +00001083 base.setTextSize(paint.getTextSize());
reed@google.combb6992a2011-04-26 17:41:56 +00001084 }
1085 if (base.getTextScaleX() != paint.getTextScaleX()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001086 *ptr++ = PaintOp_packOp(kTextScaleX_PaintOp);
1087 *ptr++ = castToU32(paint.getTextScaleX());
reed@google.comf5842f72011-05-04 18:30:04 +00001088 base.setTextScaleX(paint.getTextScaleX());
reed@google.combb6992a2011-04-26 17:41:56 +00001089 }
1090 if (base.getTextSkewX() != paint.getTextSkewX()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001091 *ptr++ = PaintOp_packOp(kTextSkewX_PaintOp);
1092 *ptr++ = castToU32(paint.getTextSkewX());
reed@google.comf5842f72011-05-04 18:30:04 +00001093 base.setTextSkewX(paint.getTextSkewX());
1094 }
1095
1096 if (!SkTypeface::Equal(base.getTypeface(), paint.getTypeface())) {
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +00001097 if (isCrossProcess(fFlags)) {
scroggo@google.com3cb969f2012-07-27 20:39:19 +00001098 uint32_t id = this->getTypefaceID(paint.getTypeface());
1099 *ptr++ = PaintOp_packOpData(kTypeface_PaintOp, id);
1100 } else if (this->needOpBytes(sizeof(void*))) {
1101 // Add to the set for ref counting.
1102 fTypefaceSet.add(paint.getTypeface());
1103 // It is safe to write the typeface to the stream before the rest
1104 // of the paint unless we ever send a kReset_PaintOp, which we
1105 // currently never do.
1106 this->writeOp(kSetTypeface_DrawOp);
1107 fWriter.writePtr(paint.getTypeface());
1108 }
reed@google.comf5842f72011-05-04 18:30:04 +00001109 base.setTypeface(paint.getTypeface());
reed@google.combb6992a2011-04-26 17:41:56 +00001110 }
reed@google.combb6992a2011-04-26 17:41:56 +00001111
scroggo@google.com4dffc592012-07-17 16:49:40 +00001112 // This is a new paint, so all old flats can be safely purged, if necessary.
1113 fFlattenableHeap.markAllFlatsSafeToDelete();
reed@google.comb55d1182011-05-11 00:42:04 +00001114 for (int i = 0; i < kCount_PaintFlats; i++) {
1115 int index = this->flattenToIndex(get_paintflat(paint, i), (PaintFlats)i);
scroggo@google.comd3ba5cc2012-07-09 16:05:53 +00001116 bool replaced = index < 0;
1117 if (replaced) {
1118 index = ~index;
1119 }
scroggo@google.com4dffc592012-07-17 16:49:40 +00001120 // Store the index of any flat that needs to be kept. 0 means no flat.
1121 if (index > 0) {
1122 fFlattenableHeap.markFlatForKeeping(index);
1123 }
1124 SkASSERT(index >= 0 && index <= fFlatDictionary.count());
scroggo@google.comd3ba5cc2012-07-09 16:05:53 +00001125 if (index != fCurrFlatIndex[i] || replaced) {
reed@google.comb55d1182011-05-11 00:42:04 +00001126 *ptr++ = PaintOp_packOpFlagData(kFlatIndex_PaintOp, i, index);
1127 fCurrFlatIndex[i] = index;
1128 }
1129 }
1130
reed@google.comacd471f2011-05-03 21:26:46 +00001131 size_t size = (char*)ptr - (char*)storage;
1132 if (size && this->needOpBytes(size)) {
reed@google.comb55d1182011-05-11 00:42:04 +00001133 this->writeOp(kPaintOp_DrawOp, 0, size);
reed@google.comb55d1182011-05-11 00:42:04 +00001134 fWriter.write(storage, size);
reed@google.combb6992a2011-04-26 17:41:56 +00001135 for (size_t i = 0; i < size/4; i++) {
reed@google.comb55d1182011-05-11 00:42:04 +00001136// SkDebugf("[%d] %08X\n", i, storage[i]);
reed@google.combb6992a2011-04-26 17:41:56 +00001137 }
1138 }
reed@google.com0cd2ac62013-10-14 20:02:44 +00001139
1140 //
1141 // Do these after we've written kPaintOp_DrawOp
skia.committer@gmail.comfbc58a32013-10-15 07:02:27 +00001142
reed@google.com0cd2ac62013-10-14 20:02:44 +00001143 if (base.getAnnotation() != paint.getAnnotation()) {
1144 if (NULL == paint.getAnnotation()) {
commit-bot@chromium.org89ff3dd2013-10-29 20:29:38 +00001145 if (this->needOpBytes()) {
1146 this->writeOp(kSetAnnotation_DrawOp, 0, 0);
1147 }
reed@google.com0cd2ac62013-10-14 20:02:44 +00001148 } else {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +00001149 SkWriteBuffer buffer;
reed@google.com0cd2ac62013-10-14 20:02:44 +00001150 paint.getAnnotation()->writeToBuffer(buffer);
commit-bot@chromium.org89ff3dd2013-10-29 20:29:38 +00001151 const size_t size = buffer.bytesWritten();
1152 if (this->needOpBytes(size)) {
1153 this->writeOp(kSetAnnotation_DrawOp, 0, size);
1154 buffer.writeToMemory(fWriter.reserve(size));
1155 }
reed@google.com0cd2ac62013-10-14 20:02:44 +00001156 }
commit-bot@chromium.org40258a52013-10-29 19:23:26 +00001157 base.setAnnotation(paint.getAnnotation());
reed@google.com0cd2ac62013-10-14 20:02:44 +00001158 }
reed@google.combb6992a2011-04-26 17:41:56 +00001159}
1160
1161///////////////////////////////////////////////////////////////////////////////
1162
1163#include "SkGPipe.h"
1164
scroggo@google.com3cb969f2012-07-27 20:39:19 +00001165SkGPipeController::~SkGPipeController() {
1166 SkSafeUnref(fCanvas);
1167}
1168
1169void SkGPipeController::setCanvas(SkGPipeCanvas* canvas) {
1170 SkRefCnt_SafeAssign(fCanvas, canvas);
1171}
1172
1173///////////////////////////////////////////////////////////////////////////////
1174
1175SkGPipeWriter::SkGPipeWriter()
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +00001176: fWriter(0) {
reed@google.combb6992a2011-04-26 17:41:56 +00001177 fCanvas = NULL;
1178}
1179
1180SkGPipeWriter::~SkGPipeWriter() {
reed@google.comacd471f2011-05-03 21:26:46 +00001181 this->endRecording();
reed@google.combb6992a2011-04-26 17:41:56 +00001182}
1183
junov@chromium.orga8db8fe2012-08-15 19:49:22 +00001184SkCanvas* SkGPipeWriter::startRecording(SkGPipeController* controller, uint32_t flags,
1185 uint32_t width, uint32_t height) {
reed@google.combb6992a2011-04-26 17:41:56 +00001186 if (NULL == fCanvas) {
reed@google.comacd471f2011-05-03 21:26:46 +00001187 fWriter.reset(NULL, 0);
junov@chromium.orga8db8fe2012-08-15 19:49:22 +00001188 fCanvas = SkNEW_ARGS(SkGPipeCanvas, (controller, &fWriter, flags, width, height));
reed@google.combb6992a2011-04-26 17:41:56 +00001189 }
scroggo@google.com3cb969f2012-07-27 20:39:19 +00001190 controller->setCanvas(fCanvas);
reed@google.combb6992a2011-04-26 17:41:56 +00001191 return fCanvas;
1192}
1193
1194void SkGPipeWriter::endRecording() {
1195 if (fCanvas) {
scroggo@google.com59c3ab62013-11-12 14:32:38 +00001196 fCanvas->finish(true);
reed@google.combb6992a2011-04-26 17:41:56 +00001197 fCanvas->unref();
1198 fCanvas = NULL;
1199 }
1200}
1201
junov@chromium.org2e14ba82012-08-07 14:26:57 +00001202void SkGPipeWriter::flushRecording(bool detachCurrentBlock) {
1203 if (fCanvas) {
1204 fCanvas->flushRecording(detachCurrentBlock);
1205 }
junov@chromium.org77eec242012-07-18 17:54:45 +00001206}
1207
junov@chromium.org2e14ba82012-08-07 14:26:57 +00001208size_t SkGPipeWriter::freeMemoryIfPossible(size_t bytesToFree) {
1209 if (fCanvas) {
1210 return fCanvas->freeMemoryIfPossible(bytesToFree);
1211 }
1212 return 0;
1213}
1214
1215size_t SkGPipeWriter::storageAllocatedForRecording() const {
scroggo@google.com15011ee2012-07-26 20:03:32 +00001216 return NULL == fCanvas ? 0 : fCanvas->storageAllocatedForRecording();
1217}
1218
scroggo@google.com3e26bd02012-08-14 15:20:01 +00001219///////////////////////////////////////////////////////////////////////////////
1220
1221BitmapShuttle::BitmapShuttle(SkGPipeCanvas* canvas) {
1222 SkASSERT(canvas != NULL);
1223 fCanvas = canvas;
1224 fCanvas->ref();
1225}
1226
1227BitmapShuttle::~BitmapShuttle() {
scroggo@google.com59c3ab62013-11-12 14:32:38 +00001228 this->removeCanvas();
scroggo@google.com3e26bd02012-08-14 15:20:01 +00001229}
1230
1231bool BitmapShuttle::insert(const SkBitmap& bitmap, int32_t slot) {
scroggo@google.com59c3ab62013-11-12 14:32:38 +00001232 SkASSERT(fCanvas != NULL);
scroggo@google.com3e26bd02012-08-14 15:20:01 +00001233 return fCanvas->shuttleBitmap(bitmap, slot);
1234}
scroggo@google.com59c3ab62013-11-12 14:32:38 +00001235
1236void BitmapShuttle::removeCanvas() {
1237 if (NULL == fCanvas) {
1238 return;
1239 }
1240 fCanvas->unref();
1241 fCanvas = NULL;
1242}