blob: 14caecd850798694232d0458f9c4d46e59c823f3 [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
commit-bot@chromium.org2d91eff2014-05-07 15:27:10 +000032#if SK_DEBUG
33// When debugging, allocate snuggly.
34static const size_t kMinBlockSize = 0;
35#else
36// For peformance, make large allocations.
37static const size_t kMinBlockSize = 16 * 1024;
38#endif
39
reed@google.com4ed0fb72012-12-12 20:48:18 +000040enum {
41 kSizeOfFlatRRect = sizeof(SkRect) + 4 * sizeof(SkVector)
42};
43
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +000044static bool isCrossProcess(uint32_t flags) {
45 return SkToBool(flags & SkGPipeWriter::kCrossProcess_Flag);
46}
47
reed@google.comb55d1182011-05-11 00:42:04 +000048static SkFlattenable* get_paintflat(const SkPaint& paint, unsigned paintFlat) {
49 SkASSERT(paintFlat < kCount_PaintFlats);
50 switch (paintFlat) {
51 case kColorFilter_PaintFlat: return paint.getColorFilter();
reed@google.com0faac1e2011-05-11 05:58:58 +000052 case kDrawLooper_PaintFlat: return paint.getLooper();
reed@google.comb55d1182011-05-11 00:42:04 +000053 case kMaskFilter_PaintFlat: return paint.getMaskFilter();
54 case kPathEffect_PaintFlat: return paint.getPathEffect();
55 case kRasterizer_PaintFlat: return paint.getRasterizer();
56 case kShader_PaintFlat: return paint.getShader();
scroggo@google.com16d1d0b2012-05-02 19:09:40 +000057 case kImageFilter_PaintFlat: return paint.getImageFilter();
reed@google.comb55d1182011-05-11 00:42:04 +000058 case kXfermode_PaintFlat: return paint.getXfermode();
59 }
tomhudson@google.com0c00f212011-12-28 14:59:50 +000060 SkDEBUGFAIL("never gets here");
reed@google.comb55d1182011-05-11 00:42:04 +000061 return NULL;
62}
reed@google.combb6992a2011-04-26 17:41:56 +000063
reed@google.comf5842f72011-05-04 18:30:04 +000064static size_t writeTypeface(SkWriter32* writer, SkTypeface* typeface) {
65 SkASSERT(typeface);
66 SkDynamicMemoryWStream stream;
67 typeface->serialize(&stream);
68 size_t size = stream.getOffset();
69 if (writer) {
commit-bot@chromium.orgb45bd1f2014-04-24 18:17:30 +000070 writer->write32(SkToU32(size));
reed@google.com8a85d0c2011-06-24 19:12:12 +000071 SkAutoDataUnref data(stream.copyToData());
robertphillips@google.com59f46b82012-07-10 17:30:58 +000072 writer->writePad(data->data(), size);
reed@google.comf5842f72011-05-04 18:30:04 +000073 }
scroggo@google.com5af9b202012-06-04 17:17:36 +000074 return 4 + SkAlign4(size);
reed@google.comf5842f72011-05-04 18:30:04 +000075}
76
reed@google.combb6992a2011-04-26 17:41:56 +000077///////////////////////////////////////////////////////////////////////////////
78
scroggo@google.com4dffc592012-07-17 16:49:40 +000079class FlattenableHeap : public SkFlatController {
80public:
scroggo@google.com664fab12012-08-14 19:22:05 +000081 FlattenableHeap(int numFlatsToKeep, SkNamedFactorySet* fset, bool isCrossProcess)
commit-bot@chromium.orga2bd2d12014-01-30 22:16:32 +000082 : INHERITED(isCrossProcess ? SkWriteBuffer::kCrossProcess_Flag : 0)
83 , fNumFlatsToKeep(numFlatsToKeep) {
scroggo@google.com664fab12012-08-14 19:22:05 +000084 SkASSERT((isCrossProcess && fset != NULL) || (!isCrossProcess && NULL == fset));
85 if (isCrossProcess) {
86 this->setNamedFactorySet(fset);
scroggo@google.com664fab12012-08-14 19:22:05 +000087 }
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +000088 }
scroggo@google.com4dffc592012-07-17 16:49:40 +000089
90 ~FlattenableHeap() {
91 fPointers.freeAll();
92 }
93
94 virtual void* allocThrow(size_t bytes) SK_OVERRIDE;
95
96 virtual void unalloc(void* ptr) SK_OVERRIDE;
97
scroggo@google.com7ca24432012-08-14 15:48:43 +000098 void setBitmapStorage(SkBitmapHeap* heap) {
99 this->setBitmapHeap(heap);
100 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000101
scroggo@google.com4dffc592012-07-17 16:49:40 +0000102 const SkFlatData* flatToReplace() const;
103
104 // Mark an SkFlatData as one that should not be returned by flatToReplace.
105 // Takes the result of SkFlatData::index() as its parameter.
106 void markFlatForKeeping(int index) {
107 *fFlatsThatMustBeKept.append() = index;
108 }
109
110 void markAllFlatsSafeToDelete() {
111 fFlatsThatMustBeKept.reset();
112 }
113
114private:
115 // Keep track of the indices (i.e. the result of SkFlatData::index()) of
116 // flats that must be kept, since they are on the current paint.
117 SkTDArray<int> fFlatsThatMustBeKept;
118 SkTDArray<void*> fPointers;
119 const int fNumFlatsToKeep;
commit-bot@chromium.orga2bd2d12014-01-30 22:16:32 +0000120
121 typedef SkFlatController INHERITED;
scroggo@google.com4dffc592012-07-17 16:49:40 +0000122};
123
124void FlattenableHeap::unalloc(void* ptr) {
125 int indexToRemove = fPointers.rfind(ptr);
126 if (indexToRemove >= 0) {
127 sk_free(ptr);
128 fPointers.remove(indexToRemove);
129 }
130}
131
132void* FlattenableHeap::allocThrow(size_t bytes) {
133 void* ptr = sk_malloc_throw(bytes);
134 *fPointers.append() = ptr;
135 return ptr;
136}
137
138const SkFlatData* FlattenableHeap::flatToReplace() const {
139 // First, determine whether we should replace one.
140 if (fPointers.count() > fNumFlatsToKeep) {
141 // Look through the flattenable heap.
142 // TODO: Return the LRU flat.
143 for (int i = 0; i < fPointers.count(); i++) {
144 SkFlatData* potential = (SkFlatData*)fPointers[i];
145 // Make sure that it is not one that must be kept.
146 bool mustKeep = false;
147 for (int j = 0; j < fFlatsThatMustBeKept.count(); j++) {
148 if (potential->index() == fFlatsThatMustBeKept[j]) {
149 mustKeep = true;
150 break;
151 }
152 }
153 if (!mustKeep) {
154 return potential;
155 }
156 }
157 }
158 return NULL;
159}
160
161///////////////////////////////////////////////////////////////////////////////
162
commit-bot@chromium.org07adb632014-01-02 22:20:49 +0000163struct SkFlattenableTraits {
commit-bot@chromium.org186c0cc2014-02-18 16:15:05 +0000164 static void Flatten(SkWriteBuffer& buffer, const SkFlattenable& flattenable) {
commit-bot@chromium.org07adb632014-01-02 22:20:49 +0000165 buffer.writeFlattenable(&flattenable);
scroggo@google.com4dffc592012-07-17 16:49:40 +0000166 }
commit-bot@chromium.org07adb632014-01-02 22:20:49 +0000167 // No need to define unflatten if we never call it.
scroggo@google.com4dffc592012-07-17 16:49:40 +0000168};
commit-bot@chromium.org07adb632014-01-02 22:20:49 +0000169typedef SkFlatDictionary<SkFlattenable, SkFlattenableTraits> FlatDictionary;
scroggo@google.com4dffc592012-07-17 16:49:40 +0000170
171///////////////////////////////////////////////////////////////////////////////
172
scroggo@google.com59c3ab62013-11-12 14:32:38 +0000173/**
174 * If SkBitmaps are to be flattened to send to the reader, this class is
175 * provided to the SkBitmapHeap to tell the SkGPipeCanvas to do so.
176 */
177class BitmapShuttle : public SkBitmapHeap::ExternalStorage {
178public:
179 BitmapShuttle(SkGPipeCanvas*);
180
181 ~BitmapShuttle();
182
183 virtual bool insert(const SkBitmap& bitmap, int32_t slot) SK_OVERRIDE;
184
185 /**
186 * Remove the SkGPipeCanvas used for insertion. After this, calls to
187 * insert will crash.
188 */
189 void removeCanvas();
190
191private:
192 SkGPipeCanvas* fCanvas;
193};
194
195///////////////////////////////////////////////////////////////////////////////
196
reed@google.combb6992a2011-04-26 17:41:56 +0000197class SkGPipeCanvas : public SkCanvas {
198public:
junov@chromium.orga8db8fe2012-08-15 19:49:22 +0000199 SkGPipeCanvas(SkGPipeController*, SkWriter32*, uint32_t flags,
200 uint32_t width, uint32_t height);
reed@google.combb6992a2011-04-26 17:41:56 +0000201 virtual ~SkGPipeCanvas();
202
scroggo@google.com59c3ab62013-11-12 14:32:38 +0000203 /**
204 * Called when nothing else is to be written to the stream. Any repeated
205 * calls are ignored.
206 *
207 * @param notifyReaders Whether to send a message to the reader(s) that
208 * the writer is through sending commands. Should generally be true,
209 * unless there is an error which prevents further messages from
210 * being sent.
211 */
212 void finish(bool notifyReaders) {
213 if (fDone) {
214 return;
reed@google.combb6992a2011-04-26 17:41:56 +0000215 }
scroggo@google.com59c3ab62013-11-12 14:32:38 +0000216 if (notifyReaders && this->needOpBytes()) {
217 this->writeOp(kDone_DrawOp);
218 this->doNotify();
219 }
220 if (shouldFlattenBitmaps(fFlags)) {
221 // The following circular references exist:
222 // fFlattenableHeap -> fWriteBuffer -> fBitmapStorage -> fExternalStorage -> fCanvas
223 // fBitmapHeap -> fExternalStorage -> fCanvas
224 // fFlattenableHeap -> fBitmapStorage -> fExternalStorage -> fCanvas
225
226 // Break them all by destroying the final link to this SkGPipeCanvas.
227 fBitmapShuttle->removeCanvas();
228 }
229 fDone = true;
reed@google.combb6992a2011-04-26 17:41:56 +0000230 }
231
junov@chromium.org77eec242012-07-18 17:54:45 +0000232 void flushRecording(bool detachCurrentBlock);
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000233 size_t freeMemoryIfPossible(size_t bytesToFree);
junov@chromium.org77eec242012-07-18 17:54:45 +0000234
scroggo@google.com15011ee2012-07-26 20:03:32 +0000235 size_t storageAllocatedForRecording() {
scroggo@google.comd5d158b2012-08-14 20:38:28 +0000236 return (NULL == fBitmapHeap) ? 0 : fBitmapHeap->bytesAllocated();
scroggo@google.com15011ee2012-07-26 20:03:32 +0000237 }
238
reed@google.combb6992a2011-04-26 17:41:56 +0000239 // overrides from SkCanvas
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000240 virtual bool isDrawingToLayer() const SK_OVERRIDE;
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000241 virtual void clear(SkColor) SK_OVERRIDE;
242 virtual void drawPaint(const SkPaint& paint) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000243 virtual void drawPoints(PointMode, size_t count, const SkPoint pts[],
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000244 const SkPaint&) SK_OVERRIDE;
reed@google.com4ed0fb72012-12-12 20:48:18 +0000245 virtual void drawOval(const SkRect&, const SkPaint&) SK_OVERRIDE;
bsalomon@google.com7ce564c2013-10-22 16:54:15 +0000246 virtual void drawRect(const SkRect& rect, const SkPaint&) SK_OVERRIDE;
reed@google.com4ed0fb72012-12-12 20:48:18 +0000247 virtual void drawRRect(const SkRRect&, const SkPaint&) SK_OVERRIDE;
bsalomon@google.com7ce564c2013-10-22 16:54:15 +0000248 virtual void drawPath(const SkPath& path, const SkPaint&) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000249 virtual void drawBitmap(const SkBitmap&, SkScalar left, SkScalar top,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000250 const SkPaint*) SK_OVERRIDE;
reed@google.com71121732012-09-18 15:14:33 +0000251 virtual void drawBitmapRectToRect(const SkBitmap&, const SkRect* src,
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000252 const SkRect& dst, const SkPaint* paint,
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +0000253 DrawBitmapRectFlags flags) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000254 virtual void drawBitmapMatrix(const SkBitmap&, const SkMatrix&,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000255 const SkPaint*) SK_OVERRIDE;
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000256 virtual void drawBitmapNine(const SkBitmap& bitmap, const SkIRect& center,
257 const SkRect& dst, const SkPaint* paint = NULL) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000258 virtual void drawSprite(const SkBitmap&, int left, int top,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000259 const SkPaint*) SK_OVERRIDE;
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000260 virtual void drawPicture(SkPicture& picture) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000261 virtual void drawVertices(VertexMode, int vertexCount,
262 const SkPoint vertices[], const SkPoint texs[],
263 const SkColor colors[], SkXfermode*,
264 const uint16_t indices[], int indexCount,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000265 const SkPaint&) SK_OVERRIDE;
266 virtual void drawData(const void*, size_t) SK_OVERRIDE;
robertphillips@google.com0a4805e2013-05-29 13:24:23 +0000267 virtual void beginCommentGroup(const char* description) SK_OVERRIDE;
268 virtual void addComment(const char* kywd, const char* value) SK_OVERRIDE;
269 virtual void endCommentGroup() SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000270
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000271 /**
272 * Flatten an SkBitmap to send to the reader, where it will be referenced
273 * according to slot.
274 */
275 bool shuttleBitmap(const SkBitmap&, int32_t slot);
commit-bot@chromium.orged9806f2014-02-21 02:32:36 +0000276
277protected:
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000278 virtual void willSave(SaveFlags) SK_OVERRIDE;
279 virtual SaveLayerStrategy willSaveLayer(const SkRect*, const SkPaint*, SaveFlags) SK_OVERRIDE;
280 virtual void willRestore() SK_OVERRIDE;
281
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +0000282 virtual void didConcat(const SkMatrix&) SK_OVERRIDE;
283 virtual void didSetMatrix(const SkMatrix&) SK_OVERRIDE;
284
commit-bot@chromium.orged9806f2014-02-21 02:32:36 +0000285 virtual void onDrawDRRect(const SkRRect&, const SkRRect&, const SkPaint&) SK_OVERRIDE;
reed@google.come0d9ce82014-04-23 04:00:17 +0000286 virtual void onDrawText(const void* text, size_t byteLength, SkScalar x, SkScalar y,
287 const SkPaint&) SK_OVERRIDE;
288 virtual void onDrawPosText(const void* text, size_t byteLength, const SkPoint pos[],
289 const SkPaint&) SK_OVERRIDE;
290 virtual void onDrawPosTextH(const void* text, size_t byteLength, const SkScalar xpos[],
291 SkScalar constY, const SkPaint&) SK_OVERRIDE;
292 virtual void onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& path,
293 const SkMatrix* matrix, const SkPaint&) SK_OVERRIDE;
commit-bot@chromium.orged9806f2014-02-21 02:32:36 +0000294
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000295 virtual void onClipRect(const SkRect&, SkRegion::Op, ClipEdgeStyle) SK_OVERRIDE;
296 virtual void onClipRRect(const SkRRect&, SkRegion::Op, ClipEdgeStyle) SK_OVERRIDE;
297 virtual void onClipPath(const SkPath&, SkRegion::Op, ClipEdgeStyle) SK_OVERRIDE;
298 virtual void onClipRegion(const SkRegion&, SkRegion::Op) SK_OVERRIDE;
299
reed@google.combb6992a2011-04-26 17:41:56 +0000300private:
commit-bot@chromium.orgd9ea09e2014-03-25 17:32:26 +0000301 void recordTranslate(const SkMatrix&);
302 void recordScale(const SkMatrix&);
303 void recordConcat(const SkMatrix&);
304
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000305 enum {
306 kNoSaveLayer = -1,
307 };
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000308 SkNamedFactorySet* fFactorySet;
309 int fFirstSaveLayerStackLevel;
scroggo@google.comd9d29672012-08-14 17:21:34 +0000310 SkBitmapHeap* fBitmapHeap;
reed@google.comacd471f2011-05-03 21:26:46 +0000311 SkGPipeController* fController;
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000312 SkWriter32& fWriter;
313 size_t fBlockSize; // amount allocated for writer
314 size_t fBytesNotified;
315 bool fDone;
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000316 const uint32_t fFlags;
reed@google.combb6992a2011-04-26 17:41:56 +0000317
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000318 SkRefCntSet fTypefaceSet;
reed@google.comf5842f72011-05-04 18:30:04 +0000319
320 uint32_t getTypefaceID(SkTypeface*);
321
reed@google.comacd471f2011-05-03 21:26:46 +0000322 inline void writeOp(DrawOps op, unsigned flags, unsigned data) {
reed@google.combb6992a2011-04-26 17:41:56 +0000323 fWriter.write32(DrawOp_packOpFlagData(op, flags, data));
324 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000325
reed@google.comacd471f2011-05-03 21:26:46 +0000326 inline void writeOp(DrawOps op) {
reed@google.combb6992a2011-04-26 17:41:56 +0000327 fWriter.write32(DrawOp_packOpFlagData(op, 0, 0));
328 }
reed@google.comacd471f2011-05-03 21:26:46 +0000329
330 bool needOpBytes(size_t size = 0);
331
332 inline void doNotify() {
333 if (!fDone) {
reed@google.com44699382013-10-31 17:28:30 +0000334 size_t bytes = fWriter.bytesWritten() - fBytesNotified;
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000335 if (bytes > 0) {
336 fController->notifyWritten(bytes);
337 fBytesNotified += bytes;
338 }
reed@google.comacd471f2011-05-03 21:26:46 +0000339 }
340 }
reed@google.comb55d1182011-05-11 00:42:04 +0000341
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000342 // Should be called after any calls to an SkFlatDictionary::findAndReplace
343 // if a new SkFlatData was added when in cross process mode
344 void flattenFactoryNames();
345
scroggo@google.com59c3ab62013-11-12 14:32:38 +0000346 FlattenableHeap fFlattenableHeap;
347 FlatDictionary fFlatDictionary;
348 SkAutoTUnref<BitmapShuttle> fBitmapShuttle;
349 int fCurrFlatIndex[kCount_PaintFlats];
350
reed@google.comb55d1182011-05-11 00:42:04 +0000351 int flattenToIndex(SkFlattenable* obj, PaintFlats);
352
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000353 // Common code used by drawBitmap*. Behaves differently depending on the
354 // type of SkBitmapHeap being used, which is determined by the flags used.
355 bool commonDrawBitmap(const SkBitmap& bm, DrawOps op, unsigned flags,
356 size_t opBytesNeeded, const SkPaint* paint);
scroggo@google.com58be6822012-07-30 14:40:01 +0000357
reed@google.com31891582011-05-12 03:03:56 +0000358 SkPaint fPaint;
359 void writePaint(const SkPaint&);
reed@google.combb6992a2011-04-26 17:41:56 +0000360
361 typedef SkCanvas INHERITED;
362};
363
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000364void SkGPipeCanvas::flattenFactoryNames() {
365 const char* name;
366 while ((name = fFactorySet->getNextAddedFactoryName()) != NULL) {
367 size_t len = strlen(name);
commit-bot@chromium.org2d91eff2014-05-07 15:27:10 +0000368 if (this->needOpBytes(SkWriter32::WriteStringSize(name, len))) {
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000369 this->writeOp(kDef_Factory_DrawOp);
370 fWriter.writeString(name, len);
371 }
372 }
373}
374
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000375bool SkGPipeCanvas::shuttleBitmap(const SkBitmap& bm, int32_t slot) {
scroggo@google.com565254b2012-06-28 15:41:32 +0000376 SkASSERT(shouldFlattenBitmaps(fFlags));
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000377 SkWriteBuffer buffer;
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000378 buffer.setNamedFactoryRecorder(fFactorySet);
scroggo@google.com74b7ffd2013-04-30 02:32:41 +0000379 buffer.writeBitmap(bm);
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000380 this->flattenFactoryNames();
commit-bot@chromium.orgb45bd1f2014-04-24 18:17:30 +0000381 size_t size = buffer.bytesWritten();
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000382 if (this->needOpBytes(size)) {
383 this->writeOp(kDef_Bitmap_DrawOp, 0, slot);
384 void* dst = static_cast<void*>(fWriter.reserve(size));
385 buffer.writeToMemory(dst);
386 return true;
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000387 }
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000388 return false;
scroggo@google.comd3ba5cc2012-07-09 16:05:53 +0000389}
390
reed@google.comb55d1182011-05-11 00:42:04 +0000391// return 0 for NULL (or unflattenable obj), or index-base-1
scroggo@google.comd3ba5cc2012-07-09 16:05:53 +0000392// return ~(index-base-1) if an old flattenable was replaced
reed@google.comb55d1182011-05-11 00:42:04 +0000393int SkGPipeCanvas::flattenToIndex(SkFlattenable* obj, PaintFlats paintflat) {
scroggo@google.comd5d158b2012-08-14 20:38:28 +0000394 SkASSERT(!fDone && fBitmapHeap != NULL);
reed@google.comb55d1182011-05-11 00:42:04 +0000395 if (NULL == obj) {
396 return 0;
397 }
reed@google.comb55d1182011-05-11 00:42:04 +0000398
scroggo@google.comd9d29672012-08-14 17:21:34 +0000399 fBitmapHeap->deferAddingOwners();
scroggo@google.com4dffc592012-07-17 16:49:40 +0000400 bool added, replaced;
scroggo@google.com664fab12012-08-14 19:22:05 +0000401 const SkFlatData* flat = fFlatDictionary.findAndReplace(*obj, fFlattenableHeap.flatToReplace(),
402 &added, &replaced);
scroggo@google.comd9d29672012-08-14 17:21:34 +0000403 fBitmapHeap->endAddingOwnersDeferral(added);
scroggo@google.com4dffc592012-07-17 16:49:40 +0000404 int index = flat->index();
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000405 if (added) {
406 if (isCrossProcess(fFlags)) {
407 this->flattenFactoryNames();
408 }
409 size_t flatSize = flat->flatSize();
410 if (this->needOpBytes(flatSize)) {
411 this->writeOp(kDef_Flattenable_DrawOp, paintflat, index);
412 fWriter.write(flat->data(), flatSize);
413 }
scroggo@google.com4dffc592012-07-17 16:49:40 +0000414 }
415 if (replaced) {
reed@google.comb55d1182011-05-11 00:42:04 +0000416 index = ~index;
reed@google.comb55d1182011-05-11 00:42:04 +0000417 }
scroggo@google.com4dffc592012-07-17 16:49:40 +0000418 return index;
reed@google.comb55d1182011-05-11 00:42:04 +0000419}
420
reed@google.combb6992a2011-04-26 17:41:56 +0000421///////////////////////////////////////////////////////////////////////////////
422
scroggo@google.com4dffc592012-07-17 16:49:40 +0000423#define BITMAPS_TO_KEEP 5
424#define FLATTENABLES_TO_KEEP 10
reed@google.combb6992a2011-04-26 17:41:56 +0000425
reed@google.comacd471f2011-05-03 21:26:46 +0000426SkGPipeCanvas::SkGPipeCanvas(SkGPipeController* controller,
rmistry@google.comd6176b02012-08-23 18:14:13 +0000427 SkWriter32* writer, uint32_t flags,
junov@chromium.orga8db8fe2012-08-15 19:49:22 +0000428 uint32_t width, uint32_t height)
commit-bot@chromium.org403f8d72014-02-17 15:24:26 +0000429 : SkCanvas(width, height)
430 , fFactorySet(isCrossProcess(flags) ? SkNEW(SkNamedFactorySet) : NULL)
431 , fWriter(*writer)
432 , fFlags(flags)
433 , fFlattenableHeap(FLATTENABLES_TO_KEEP, fFactorySet, isCrossProcess(flags))
434 , fFlatDictionary(&fFlattenableHeap)
435{
reed@google.comacd471f2011-05-03 21:26:46 +0000436 fController = controller;
reed@google.combb6992a2011-04-26 17:41:56 +0000437 fDone = false;
reed@google.comacd471f2011-05-03 21:26:46 +0000438 fBlockSize = 0; // need first block from controller
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000439 fBytesNotified = 0;
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000440 fFirstSaveLayerStackLevel = kNoSaveLayer;
reed@google.comb55d1182011-05-11 00:42:04 +0000441 sk_bzero(fCurrFlatIndex, sizeof(fCurrFlatIndex));
reed@google.comacd471f2011-05-03 21:26:46 +0000442
scroggo@google.com565254b2012-06-28 15:41:32 +0000443 // Tell the reader the appropriate flags to use.
444 if (this->needOpBytes()) {
445 this->writeOp(kReportFlags_DrawOp, fFlags, 0);
446 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000447
scroggo@google.com10dccde2012-08-08 20:43:22 +0000448 if (shouldFlattenBitmaps(flags)) {
scroggo@google.com59c3ab62013-11-12 14:32:38 +0000449 fBitmapShuttle.reset(SkNEW_ARGS(BitmapShuttle, (this)));
450 fBitmapHeap = SkNEW_ARGS(SkBitmapHeap, (fBitmapShuttle.get(), BITMAPS_TO_KEEP));
scroggo@google.com10dccde2012-08-08 20:43:22 +0000451 } else {
scroggo@google.comd9d29672012-08-14 17:21:34 +0000452 fBitmapHeap = SkNEW_ARGS(SkBitmapHeap,
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000453 (BITMAPS_TO_KEEP, controller->numberOfReaders()));
scroggo@google.com10dccde2012-08-08 20:43:22 +0000454 if (this->needOpBytes(sizeof(void*))) {
scroggo@google.comd9d29672012-08-14 17:21:34 +0000455 this->writeOp(kShareBitmapHeap_DrawOp);
456 fWriter.writePtr(static_cast<void*>(fBitmapHeap));
scroggo@google.com10dccde2012-08-08 20:43:22 +0000457 }
458 }
scroggo@google.comd9d29672012-08-14 17:21:34 +0000459 fFlattenableHeap.setBitmapStorage(fBitmapHeap);
reed@google.combb6992a2011-04-26 17:41:56 +0000460}
461
462SkGPipeCanvas::~SkGPipeCanvas() {
scroggo@google.com59c3ab62013-11-12 14:32:38 +0000463 this->finish(true);
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000464 SkSafeUnref(fFactorySet);
scroggo@google.comd9d29672012-08-14 17:21:34 +0000465 SkSafeUnref(fBitmapHeap);
reed@google.combb6992a2011-04-26 17:41:56 +0000466}
467
reed@google.comacd471f2011-05-03 21:26:46 +0000468bool SkGPipeCanvas::needOpBytes(size_t needed) {
469 if (fDone) {
470 return false;
471 }
472
473 needed += 4; // size of DrawOp atom
commit-bot@chromium.org2d91eff2014-05-07 15:27:10 +0000474 needed = SkTMax(kMinBlockSize, needed);
475 needed = SkAlign4(needed);
reed@google.com44699382013-10-31 17:28:30 +0000476 if (fWriter.bytesWritten() + needed > fBlockSize) {
commit-bot@chromium.org2d91eff2014-05-07 15:27:10 +0000477 // We're making a new block. First push out the current block.
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000478 this->doNotify();
commit-bot@chromium.org2d91eff2014-05-07 15:27:10 +0000479
480 void* block = fController->requestBlock(needed, &fBlockSize);
reed@google.comacd471f2011-05-03 21:26:46 +0000481 if (NULL == block) {
scroggo@google.com59c3ab62013-11-12 14:32:38 +0000482 // Do not notify the readers, which would call this function again.
483 this->finish(false);
reed@google.comacd471f2011-05-03 21:26:46 +0000484 return false;
485 }
scroggo@google.com460a23e2012-08-16 17:56:49 +0000486 SkASSERT(SkIsAlign4(fBlockSize));
reed@google.comacd471f2011-05-03 21:26:46 +0000487 fWriter.reset(block, fBlockSize);
488 fBytesNotified = 0;
489 }
490 return true;
491}
492
reed@google.comf5842f72011-05-04 18:30:04 +0000493uint32_t SkGPipeCanvas::getTypefaceID(SkTypeface* face) {
494 uint32_t id = 0; // 0 means default/null typeface
495 if (face) {
496 id = fTypefaceSet.find(face);
497 if (0 == id) {
498 id = fTypefaceSet.add(face);
499 size_t size = writeTypeface(NULL, face);
500 if (this->needOpBytes(size)) {
reed@google.combb6793b2011-05-05 15:18:15 +0000501 this->writeOp(kDef_Typeface_DrawOp);
reed@google.comf5842f72011-05-04 18:30:04 +0000502 writeTypeface(&fWriter, face);
503 }
504 }
505 }
506 return id;
507}
508
reed@google.combb6992a2011-04-26 17:41:56 +0000509///////////////////////////////////////////////////////////////////////////////
510
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000511void SkGPipeCanvas::willSave(SaveFlags flags) {
reed@google.comacd471f2011-05-03 21:26:46 +0000512 if (this->needOpBytes()) {
513 this->writeOp(kSave_DrawOp, 0, flags);
514 }
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000515
516 this->INHERITED::willSave(flags);
reed@google.combb6992a2011-04-26 17:41:56 +0000517}
518
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000519SkCanvas::SaveLayerStrategy SkGPipeCanvas::willSaveLayer(const SkRect* bounds, const SkPaint* paint,
520 SaveFlags saveFlags) {
reed@google.comacd471f2011-05-03 21:26:46 +0000521 size_t size = 0;
reed@google.combb6992a2011-04-26 17:41:56 +0000522 unsigned opFlags = 0;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000523
reed@google.combb6992a2011-04-26 17:41:56 +0000524 if (bounds) {
525 opFlags |= kSaveLayer_HasBounds_DrawOpFlag;
reed@google.comacd471f2011-05-03 21:26:46 +0000526 size += sizeof(SkRect);
reed@google.combb6992a2011-04-26 17:41:56 +0000527 }
528 if (paint) {
529 opFlags |= kSaveLayer_HasPaint_DrawOpFlag;
reed@google.com31891582011-05-12 03:03:56 +0000530 this->writePaint(*paint);
reed@google.combb6992a2011-04-26 17:41:56 +0000531 }
532
reed@google.comacd471f2011-05-03 21:26:46 +0000533 if (this->needOpBytes(size)) {
534 this->writeOp(kSaveLayer_DrawOp, opFlags, saveFlags);
535 if (bounds) {
536 fWriter.writeRect(*bounds);
537 }
reed@google.combb6992a2011-04-26 17:41:56 +0000538 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000539
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000540 if (kNoSaveLayer == fFirstSaveLayerStackLevel){
541 fFirstSaveLayerStackLevel = this->getSaveCount();
542 }
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000543
544 this->INHERITED::willSaveLayer(bounds, paint, saveFlags);
545 // we don't create a layer
546 return kNoLayer_SaveLayerStrategy;
reed@google.combb6992a2011-04-26 17:41:56 +0000547}
548
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000549void SkGPipeCanvas::willRestore() {
reed@google.comacd471f2011-05-03 21:26:46 +0000550 if (this->needOpBytes()) {
551 this->writeOp(kRestore_DrawOp);
552 }
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000553
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000554 if (this->getSaveCount() - 1 == fFirstSaveLayerStackLevel){
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000555 fFirstSaveLayerStackLevel = kNoSaveLayer;
556 }
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000557
558 this->INHERITED::willRestore();
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000559}
560
561bool SkGPipeCanvas::isDrawingToLayer() const {
562 return kNoSaveLayer != fFirstSaveLayerStackLevel;
reed@google.combb6992a2011-04-26 17:41:56 +0000563}
564
commit-bot@chromium.orgd9ea09e2014-03-25 17:32:26 +0000565void SkGPipeCanvas::recordTranslate(const SkMatrix& m) {
566 if (this->needOpBytes(2 * sizeof(SkScalar))) {
567 this->writeOp(kTranslate_DrawOp);
568 fWriter.writeScalar(m.getTranslateX());
569 fWriter.writeScalar(m.getTranslateY());
reed@google.combb6992a2011-04-26 17:41:56 +0000570 }
reed@google.combb6992a2011-04-26 17:41:56 +0000571}
572
commit-bot@chromium.orgd9ea09e2014-03-25 17:32:26 +0000573void SkGPipeCanvas::recordScale(const SkMatrix& m) {
574 if (this->needOpBytes(2 * sizeof(SkScalar))) {
575 this->writeOp(kScale_DrawOp);
576 fWriter.writeScalar(m.getScaleX());
577 fWriter.writeScalar(m.getScaleY());
reed@google.combb6992a2011-04-26 17:41:56 +0000578 }
reed@google.combb6992a2011-04-26 17:41:56 +0000579}
580
commit-bot@chromium.orgd9ea09e2014-03-25 17:32:26 +0000581void SkGPipeCanvas::recordConcat(const SkMatrix& m) {
582 if (this->needOpBytes(m.writeToMemory(NULL))) {
583 this->writeOp(kConcat_DrawOp);
584 fWriter.writeMatrix(m);
reed@google.combb6992a2011-04-26 17:41:56 +0000585 }
reed@google.combb6992a2011-04-26 17:41:56 +0000586}
587
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +0000588void SkGPipeCanvas::didConcat(const SkMatrix& matrix) {
reed@google.combb6992a2011-04-26 17:41:56 +0000589 if (!matrix.isIdentity()) {
commit-bot@chromium.orgd9ea09e2014-03-25 17:32:26 +0000590 switch (matrix.getType()) {
591 case SkMatrix::kTranslate_Mask:
592 this->recordTranslate(matrix);
593 break;
594 case SkMatrix::kScale_Mask:
595 this->recordScale(matrix);
596 break;
597 default:
598 this->recordConcat(matrix);
599 break;
reed@google.comacd471f2011-05-03 21:26:46 +0000600 }
reed@google.combb6992a2011-04-26 17:41:56 +0000601 }
commit-bot@chromium.orgd9ea09e2014-03-25 17:32:26 +0000602
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +0000603 this->INHERITED::didConcat(matrix);
reed@google.combb6992a2011-04-26 17:41:56 +0000604}
605
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +0000606void SkGPipeCanvas::didSetMatrix(const SkMatrix& matrix) {
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000607 if (this->needOpBytes(matrix.writeToMemory(NULL))) {
reed@google.comacd471f2011-05-03 21:26:46 +0000608 this->writeOp(kSetMatrix_DrawOp);
djsollen@google.com2b2ede32012-04-12 13:24:04 +0000609 fWriter.writeMatrix(matrix);
reed@google.comacd471f2011-05-03 21:26:46 +0000610 }
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +0000611 this->INHERITED::didSetMatrix(matrix);
reed@google.combb6992a2011-04-26 17:41:56 +0000612}
613
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000614void SkGPipeCanvas::onClipRect(const SkRect& rect, SkRegion::Op rgnOp,
615 ClipEdgeStyle edgeStyle) {
scroggo@google.com460a23e2012-08-16 17:56:49 +0000616 if (this->needOpBytes(sizeof(SkRect))) {
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000617 unsigned flags = 0;
618 if (kSoft_ClipEdgeStyle == edgeStyle) {
619 flags = kClip_HasAntiAlias_DrawOpFlag;
620 }
scroggo@google.com460a23e2012-08-16 17:56:49 +0000621 this->writeOp(kClipRect_DrawOp, flags, rgnOp);
reed@google.comacd471f2011-05-03 21:26:46 +0000622 fWriter.writeRect(rect);
623 }
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000624 this->INHERITED::onClipRect(rect, rgnOp, edgeStyle);
reed@google.combb6992a2011-04-26 17:41:56 +0000625}
626
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000627void SkGPipeCanvas::onClipRRect(const SkRRect& rrect, SkRegion::Op rgnOp,
628 ClipEdgeStyle edgeStyle) {
reed@google.com4ed0fb72012-12-12 20:48:18 +0000629 if (this->needOpBytes(kSizeOfFlatRRect)) {
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000630 unsigned flags = 0;
631 if (kSoft_ClipEdgeStyle == edgeStyle) {
632 flags = kClip_HasAntiAlias_DrawOpFlag;
633 }
reed@google.com4ed0fb72012-12-12 20:48:18 +0000634 this->writeOp(kClipRRect_DrawOp, flags, rgnOp);
635 fWriter.writeRRect(rrect);
636 }
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000637 this->INHERITED::onClipRRect(rrect, rgnOp, edgeStyle);
reed@google.com4ed0fb72012-12-12 20:48:18 +0000638}
639
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000640void SkGPipeCanvas::onClipPath(const SkPath& path, SkRegion::Op rgnOp,
641 ClipEdgeStyle edgeStyle) {
scroggo@google.com460a23e2012-08-16 17:56:49 +0000642 if (this->needOpBytes(path.writeToMemory(NULL))) {
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000643 unsigned flags = 0;
644 if (kSoft_ClipEdgeStyle == edgeStyle) {
645 flags = kClip_HasAntiAlias_DrawOpFlag;
646 }
scroggo@google.com460a23e2012-08-16 17:56:49 +0000647 this->writeOp(kClipPath_DrawOp, flags, rgnOp);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000648 fWriter.writePath(path);
reed@google.comacd471f2011-05-03 21:26:46 +0000649 }
reed@google.combb6992a2011-04-26 17:41:56 +0000650 // we just pass on the bounds of the path
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000651 this->INHERITED::onClipRect(path.getBounds(), rgnOp, edgeStyle);
reed@google.combb6992a2011-04-26 17:41:56 +0000652}
653
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000654void SkGPipeCanvas::onClipRegion(const SkRegion& region, SkRegion::Op rgnOp) {
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000655 if (this->needOpBytes(region.writeToMemory(NULL))) {
reed@google.comacd471f2011-05-03 21:26:46 +0000656 this->writeOp(kClipRegion_DrawOp, 0, rgnOp);
djsollen@google.com2b2ede32012-04-12 13:24:04 +0000657 fWriter.writeRegion(region);
reed@google.comacd471f2011-05-03 21:26:46 +0000658 }
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000659 this->INHERITED::onClipRegion(region, rgnOp);
reed@google.combb6992a2011-04-26 17:41:56 +0000660}
661
662///////////////////////////////////////////////////////////////////////////////
663
664void SkGPipeCanvas::clear(SkColor color) {
665 unsigned flags = 0;
666 if (color) {
667 flags |= kClear_HasColor_DrawOpFlag;
668 }
reed@google.comacd471f2011-05-03 21:26:46 +0000669 if (this->needOpBytes(sizeof(SkColor))) {
670 this->writeOp(kDrawClear_DrawOp, flags, 0);
671 if (color) {
672 fWriter.write32(color);
673 }
reed@google.combb6992a2011-04-26 17:41:56 +0000674 }
675}
676
677void SkGPipeCanvas::drawPaint(const SkPaint& paint) {
reed@google.com31891582011-05-12 03:03:56 +0000678 this->writePaint(paint);
reed@google.comacd471f2011-05-03 21:26:46 +0000679 if (this->needOpBytes()) {
reed@google.com31891582011-05-12 03:03:56 +0000680 this->writeOp(kDrawPaint_DrawOp);
reed@google.comacd471f2011-05-03 21:26:46 +0000681 }
reed@google.combb6992a2011-04-26 17:41:56 +0000682}
683
684void SkGPipeCanvas::drawPoints(PointMode mode, size_t count,
robertphillips@google.com8b169312013-10-15 17:47:36 +0000685 const SkPoint pts[], const SkPaint& paint) {
reed@google.combb6992a2011-04-26 17:41:56 +0000686 if (count) {
reed@google.com31891582011-05-12 03:03:56 +0000687 this->writePaint(paint);
reed@google.comacd471f2011-05-03 21:26:46 +0000688 if (this->needOpBytes(4 + count * sizeof(SkPoint))) {
reed@google.com31891582011-05-12 03:03:56 +0000689 this->writeOp(kDrawPoints_DrawOp, mode, 0);
commit-bot@chromium.orgb45bd1f2014-04-24 18:17:30 +0000690 fWriter.write32(SkToU32(count));
reed@google.comacd471f2011-05-03 21:26:46 +0000691 fWriter.write(pts, count * sizeof(SkPoint));
692 }
reed@google.combb6992a2011-04-26 17:41:56 +0000693 }
694}
695
reed@google.com4ed0fb72012-12-12 20:48:18 +0000696void SkGPipeCanvas::drawOval(const SkRect& rect, const SkPaint& paint) {
reed@google.com4ed0fb72012-12-12 20:48:18 +0000697 this->writePaint(paint);
698 if (this->needOpBytes(sizeof(SkRect))) {
699 this->writeOp(kDrawOval_DrawOp);
700 fWriter.writeRect(rect);
701 }
702}
703
bsalomon@google.com7ce564c2013-10-22 16:54:15 +0000704void SkGPipeCanvas::drawRect(const SkRect& rect, const SkPaint& paint) {
reed@google.com31891582011-05-12 03:03:56 +0000705 this->writePaint(paint);
reed@google.comacd471f2011-05-03 21:26:46 +0000706 if (this->needOpBytes(sizeof(SkRect))) {
reed@google.com31891582011-05-12 03:03:56 +0000707 this->writeOp(kDrawRect_DrawOp);
reed@google.comacd471f2011-05-03 21:26:46 +0000708 fWriter.writeRect(rect);
709 }
reed@google.combb6992a2011-04-26 17:41:56 +0000710}
711
reed@google.com4ed0fb72012-12-12 20:48:18 +0000712void SkGPipeCanvas::drawRRect(const SkRRect& rrect, const SkPaint& paint) {
reed@google.com4ed0fb72012-12-12 20:48:18 +0000713 this->writePaint(paint);
714 if (this->needOpBytes(kSizeOfFlatRRect)) {
715 this->writeOp(kDrawRRect_DrawOp);
716 fWriter.writeRRect(rrect);
717 }
718}
719
commit-bot@chromium.orged9806f2014-02-21 02:32:36 +0000720void SkGPipeCanvas::onDrawDRRect(const SkRRect& outer, const SkRRect& inner,
721 const SkPaint& paint) {
commit-bot@chromium.orged9806f2014-02-21 02:32:36 +0000722 this->writePaint(paint);
723 if (this->needOpBytes(kSizeOfFlatRRect * 2)) {
724 this->writeOp(kDrawDRRect_DrawOp);
725 fWriter.writeRRect(outer);
726 fWriter.writeRRect(inner);
727 }
728}
729
bsalomon@google.com7ce564c2013-10-22 16:54:15 +0000730void SkGPipeCanvas::drawPath(const SkPath& path, const SkPaint& paint) {
reed@google.com31891582011-05-12 03:03:56 +0000731 this->writePaint(paint);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000732 if (this->needOpBytes(path.writeToMemory(NULL))) {
reed@google.com31891582011-05-12 03:03:56 +0000733 this->writeOp(kDrawPath_DrawOp);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000734 fWriter.writePath(path);
reed@google.comacd471f2011-05-03 21:26:46 +0000735 }
reed@google.combb6992a2011-04-26 17:41:56 +0000736}
737
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000738bool SkGPipeCanvas::commonDrawBitmap(const SkBitmap& bm, DrawOps op,
739 unsigned flags,
740 size_t opBytesNeeded,
741 const SkPaint* paint) {
commit-bot@chromium.org2d91eff2014-05-07 15:27:10 +0000742 if (fDone) {
743 return false;
744 }
745
scroggo@google.com58be6822012-07-30 14:40:01 +0000746 if (paint != NULL) {
scroggo@google.com460a23e2012-08-16 17:56:49 +0000747 flags |= kDrawBitmap_HasPaint_DrawOpFlag;
scroggo@google.com58be6822012-07-30 14:40:01 +0000748 this->writePaint(*paint);
749 }
commit-bot@chromium.org2d91eff2014-05-07 15:27:10 +0000750
751 // This needs to run first so its calls to needOpBytes() and writes
752 // don't interlace with the needOpBytes() and writes below.
753 SkASSERT(fBitmapHeap != NULL);
754 int32_t bitmapIndex = fBitmapHeap->insert(bm);
755 if (SkBitmapHeap::INVALID_SLOT == bitmapIndex) {
756 return false;
757 }
758
scroggo@google.com10dccde2012-08-08 20:43:22 +0000759 if (this->needOpBytes(opBytesNeeded)) {
760 this->writeOp(op, flags, bitmapIndex);
scroggo@google.com58be6822012-07-30 14:40:01 +0000761 return true;
762 }
763 return false;
764}
765
766void SkGPipeCanvas::drawBitmap(const SkBitmap& bm, SkScalar left, SkScalar top,
767 const SkPaint* paint) {
scroggo@google.com58be6822012-07-30 14:40:01 +0000768 size_t opBytesNeeded = sizeof(SkScalar) * 2;
769
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000770 if (this->commonDrawBitmap(bm, kDrawBitmap_DrawOp, 0, opBytesNeeded, paint)) {
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000771 fWriter.writeScalar(left);
772 fWriter.writeScalar(top);
773 }
reed@google.combb6992a2011-04-26 17:41:56 +0000774}
775
reed@google.com71121732012-09-18 15:14:33 +0000776void SkGPipeCanvas::drawBitmapRectToRect(const SkBitmap& bm, const SkRect* src,
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +0000777 const SkRect& dst, const SkPaint* paint,
778 DrawBitmapRectFlags dbmrFlags) {
scroggo@google.com58be6822012-07-30 14:40:01 +0000779 size_t opBytesNeeded = sizeof(SkRect);
780 bool hasSrc = src != NULL;
781 unsigned flags;
782 if (hasSrc) {
scroggo@google.com460a23e2012-08-16 17:56:49 +0000783 flags = kDrawBitmap_HasSrcRect_DrawOpFlag;
scroggo@google.com58be6822012-07-30 14:40:01 +0000784 opBytesNeeded += sizeof(int32_t) * 4;
785 } else {
786 flags = 0;
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000787 }
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +0000788 if (dbmrFlags & kBleed_DrawBitmapRectFlag) {
789 flags |= kDrawBitmap_Bleed_DrawOpFlag;
790 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000791
reed@google.com71121732012-09-18 15:14:33 +0000792 if (this->commonDrawBitmap(bm, kDrawBitmapRectToRect_DrawOp, flags, opBytesNeeded, paint)) {
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000793 if (hasSrc) {
reed@google.com71121732012-09-18 15:14:33 +0000794 fWriter.writeRect(*src);
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000795 }
796 fWriter.writeRect(dst);
797 }
reed@google.combb6992a2011-04-26 17:41:56 +0000798}
799
scroggo@google.com58be6822012-07-30 14:40:01 +0000800void SkGPipeCanvas::drawBitmapMatrix(const SkBitmap& bm, const SkMatrix& matrix,
801 const SkPaint* paint) {
scroggo@google.com58be6822012-07-30 14:40:01 +0000802 size_t opBytesNeeded = matrix.writeToMemory(NULL);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000803
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000804 if (this->commonDrawBitmap(bm, kDrawBitmapMatrix_DrawOp, 0, opBytesNeeded, paint)) {
scroggo@google.com58be6822012-07-30 14:40:01 +0000805 fWriter.writeMatrix(matrix);
806 }
reed@google.combb6992a2011-04-26 17:41:56 +0000807}
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000808
809void SkGPipeCanvas::drawBitmapNine(const SkBitmap& bm, const SkIRect& center,
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000810 const SkRect& dst, const SkPaint* paint) {
scroggo@google.com58be6822012-07-30 14:40:01 +0000811 size_t opBytesNeeded = sizeof(int32_t) * 4 + sizeof(SkRect);
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000812
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000813 if (this->commonDrawBitmap(bm, kDrawBitmapNine_DrawOp, 0, opBytesNeeded, paint)) {
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000814 fWriter.write32(center.fLeft);
815 fWriter.write32(center.fTop);
816 fWriter.write32(center.fRight);
817 fWriter.write32(center.fBottom);
818 fWriter.writeRect(dst);
819 }
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000820}
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000821
822void SkGPipeCanvas::drawSprite(const SkBitmap& bm, int left, int top,
823 const SkPaint* paint) {
scroggo@google.com58be6822012-07-30 14:40:01 +0000824 size_t opBytesNeeded = sizeof(int32_t) * 2;
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000825
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000826 if (this->commonDrawBitmap(bm, kDrawSprite_DrawOp, 0, opBytesNeeded, paint)) {
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000827 fWriter.write32(left);
828 fWriter.write32(top);
829 }
reed@google.combb6992a2011-04-26 17:41:56 +0000830}
831
reed@google.come0d9ce82014-04-23 04:00:17 +0000832void SkGPipeCanvas::onDrawText(const void* text, size_t byteLength, SkScalar x, SkScalar y,
833 const SkPaint& paint) {
reed@google.combb6992a2011-04-26 17:41:56 +0000834 if (byteLength) {
reed@google.com31891582011-05-12 03:03:56 +0000835 this->writePaint(paint);
reed@google.comacd471f2011-05-03 21:26:46 +0000836 if (this->needOpBytes(4 + SkAlign4(byteLength) + 2 * sizeof(SkScalar))) {
reed@google.com31891582011-05-12 03:03:56 +0000837 this->writeOp(kDrawText_DrawOp);
commit-bot@chromium.orgb45bd1f2014-04-24 18:17:30 +0000838 fWriter.write32(SkToU32(byteLength));
reed@google.comacd471f2011-05-03 21:26:46 +0000839 fWriter.writePad(text, byteLength);
840 fWriter.writeScalar(x);
841 fWriter.writeScalar(y);
842 }
reed@google.combb6992a2011-04-26 17:41:56 +0000843 }
844}
845
reed@google.come0d9ce82014-04-23 04:00:17 +0000846void SkGPipeCanvas::onDrawPosText(const void* text, size_t byteLength, const SkPoint pos[],
847 const SkPaint& paint) {
reed@google.combb6992a2011-04-26 17:41:56 +0000848 if (byteLength) {
reed@google.com31891582011-05-12 03:03:56 +0000849 this->writePaint(paint);
reed@google.combb6992a2011-04-26 17:41:56 +0000850 int count = paint.textToGlyphs(text, byteLength, NULL);
reed@google.comacd471f2011-05-03 21:26:46 +0000851 if (this->needOpBytes(4 + SkAlign4(byteLength) + 4 + count * sizeof(SkPoint))) {
reed@google.com31891582011-05-12 03:03:56 +0000852 this->writeOp(kDrawPosText_DrawOp);
commit-bot@chromium.orgb45bd1f2014-04-24 18:17:30 +0000853 fWriter.write32(SkToU32(byteLength));
reed@google.comacd471f2011-05-03 21:26:46 +0000854 fWriter.writePad(text, byteLength);
855 fWriter.write32(count);
856 fWriter.write(pos, count * sizeof(SkPoint));
857 }
reed@google.combb6992a2011-04-26 17:41:56 +0000858 }
859}
860
reed@google.come0d9ce82014-04-23 04:00:17 +0000861void SkGPipeCanvas::onDrawPosTextH(const void* text, size_t byteLength, const SkScalar xpos[],
862 SkScalar constY, const SkPaint& paint) {
reed@google.combb6992a2011-04-26 17:41:56 +0000863 if (byteLength) {
reed@google.com31891582011-05-12 03:03:56 +0000864 this->writePaint(paint);
reed@google.combb6992a2011-04-26 17:41:56 +0000865 int count = paint.textToGlyphs(text, byteLength, NULL);
reed@google.comacd471f2011-05-03 21:26:46 +0000866 if (this->needOpBytes(4 + SkAlign4(byteLength) + 4 + count * sizeof(SkScalar) + 4)) {
reed@google.com31891582011-05-12 03:03:56 +0000867 this->writeOp(kDrawPosTextH_DrawOp);
commit-bot@chromium.orgb45bd1f2014-04-24 18:17:30 +0000868 fWriter.write32(SkToU32(byteLength));
reed@google.comacd471f2011-05-03 21:26:46 +0000869 fWriter.writePad(text, byteLength);
870 fWriter.write32(count);
871 fWriter.write(xpos, count * sizeof(SkScalar));
872 fWriter.writeScalar(constY);
873 }
reed@google.combb6992a2011-04-26 17:41:56 +0000874 }
875}
876
reed@google.come0d9ce82014-04-23 04:00:17 +0000877void SkGPipeCanvas::onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& path,
878 const SkMatrix* matrix, const SkPaint& paint) {
reed@google.combb6992a2011-04-26 17:41:56 +0000879 if (byteLength) {
880 unsigned flags = 0;
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000881 size_t size = 4 + SkAlign4(byteLength) + path.writeToMemory(NULL);
reed@google.combb6992a2011-04-26 17:41:56 +0000882 if (matrix) {
883 flags |= kDrawTextOnPath_HasMatrix_DrawOpFlag;
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000884 size += matrix->writeToMemory(NULL);
reed@google.combb6992a2011-04-26 17:41:56 +0000885 }
reed@google.com31891582011-05-12 03:03:56 +0000886 this->writePaint(paint);
reed@google.comacd471f2011-05-03 21:26:46 +0000887 if (this->needOpBytes(size)) {
reed@google.com31891582011-05-12 03:03:56 +0000888 this->writeOp(kDrawTextOnPath_DrawOp, flags, 0);
reed@google.combb6992a2011-04-26 17:41:56 +0000889
commit-bot@chromium.orgb45bd1f2014-04-24 18:17:30 +0000890 fWriter.write32(SkToU32(byteLength));
reed@google.comacd471f2011-05-03 21:26:46 +0000891 fWriter.writePad(text, byteLength);
reed@google.combb6992a2011-04-26 17:41:56 +0000892
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000893 fWriter.writePath(path);
reed@google.comacd471f2011-05-03 21:26:46 +0000894 if (matrix) {
djsollen@google.com2b2ede32012-04-12 13:24:04 +0000895 fWriter.writeMatrix(*matrix);
reed@google.comacd471f2011-05-03 21:26:46 +0000896 }
reed@google.combb6992a2011-04-26 17:41:56 +0000897 }
898 }
899}
900
901void SkGPipeCanvas::drawPicture(SkPicture& picture) {
reed@google.com0faac1e2011-05-11 05:58:58 +0000902 // we want to playback the picture into individual draw calls
903 this->INHERITED::drawPicture(picture);
reed@google.combb6992a2011-04-26 17:41:56 +0000904}
905
reed@google.com85e143c2013-12-30 15:51:25 +0000906void SkGPipeCanvas::drawVertices(VertexMode vmode, int vertexCount,
reed@google.combb6992a2011-04-26 17:41:56 +0000907 const SkPoint vertices[], const SkPoint texs[],
reed@google.com85e143c2013-12-30 15:51:25 +0000908 const SkColor colors[], SkXfermode* xfer,
reed@google.combb6992a2011-04-26 17:41:56 +0000909 const uint16_t indices[], int indexCount,
910 const SkPaint& paint) {
911 if (0 == vertexCount) {
912 return;
913 }
914
reed@google.com31891582011-05-12 03:03:56 +0000915 this->writePaint(paint);
commit-bot@chromium.org2d91eff2014-05-07 15:27:10 +0000916
917 unsigned flags = 0; // flags pack with the op, so don't need extra space themselves
918
919 size_t size = 0;
920 size += 4; // vmode
921 size += 4; // vertexCount
922 size += vertexCount * sizeof(SkPoint); // vertices
reed@google.combb6992a2011-04-26 17:41:56 +0000923 if (texs) {
924 flags |= kDrawVertices_HasTexs_DrawOpFlag;
reed@google.comacd471f2011-05-03 21:26:46 +0000925 size += vertexCount * sizeof(SkPoint);
reed@google.combb6992a2011-04-26 17:41:56 +0000926 }
927 if (colors) {
928 flags |= kDrawVertices_HasColors_DrawOpFlag;
reed@google.comacd471f2011-05-03 21:26:46 +0000929 size += vertexCount * sizeof(SkColor);
reed@google.combb6992a2011-04-26 17:41:56 +0000930 }
reed@google.com85e143c2013-12-30 15:51:25 +0000931 if (xfer && !SkXfermode::IsMode(xfer, SkXfermode::kModulate_Mode)) {
932 flags |= kDrawVertices_HasXfermode_DrawOpFlag;
commit-bot@chromium.org2d91eff2014-05-07 15:27:10 +0000933 size += sizeof(int32_t); // SkXfermode::Mode
934 }
935 if (indices && indexCount > 0) {
936 flags |= kDrawVertices_HasIndices_DrawOpFlag;
937 size += 4; // indexCount
938 size += SkAlign4(indexCount * sizeof(uint16_t)); // indices
reed@google.com85e143c2013-12-30 15:51:25 +0000939 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000940
reed@google.comacd471f2011-05-03 21:26:46 +0000941 if (this->needOpBytes(size)) {
reed@google.com31891582011-05-12 03:03:56 +0000942 this->writeOp(kDrawVertices_DrawOp, flags, 0);
reed@google.com85e143c2013-12-30 15:51:25 +0000943 fWriter.write32(vmode);
reed@google.comacd471f2011-05-03 21:26:46 +0000944 fWriter.write32(vertexCount);
945 fWriter.write(vertices, vertexCount * sizeof(SkPoint));
commit-bot@chromium.org2d91eff2014-05-07 15:27:10 +0000946 if (flags & kDrawVertices_HasTexs_DrawOpFlag) {
reed@google.comacd471f2011-05-03 21:26:46 +0000947 fWriter.write(texs, vertexCount * sizeof(SkPoint));
948 }
commit-bot@chromium.org2d91eff2014-05-07 15:27:10 +0000949 if (flags & kDrawVertices_HasColors_DrawOpFlag) {
reed@google.comacd471f2011-05-03 21:26:46 +0000950 fWriter.write(colors, vertexCount * sizeof(SkColor));
951 }
reed@google.com85e143c2013-12-30 15:51:25 +0000952 if (flags & kDrawVertices_HasXfermode_DrawOpFlag) {
953 SkXfermode::Mode mode = SkXfermode::kModulate_Mode;
commit-bot@chromium.org2d91eff2014-05-07 15:27:10 +0000954 SkAssertResult(xfer->asMode(&mode));
reed@google.com85e143c2013-12-30 15:51:25 +0000955 fWriter.write32(mode);
956 }
commit-bot@chromium.org2d91eff2014-05-07 15:27:10 +0000957 if (flags & kDrawVertices_HasIndices_DrawOpFlag) {
reed@google.comacd471f2011-05-03 21:26:46 +0000958 fWriter.write32(indexCount);
959 fWriter.writePad(indices, indexCount * sizeof(uint16_t));
960 }
reed@google.combb6992a2011-04-26 17:41:56 +0000961 }
962}
963
reed@google.comacd471f2011-05-03 21:26:46 +0000964void SkGPipeCanvas::drawData(const void* ptr, size_t size) {
965 if (size && ptr) {
reed@google.combb6992a2011-04-26 17:41:56 +0000966 unsigned data = 0;
967 if (size < (1 << DRAWOPS_DATA_BITS)) {
968 data = (unsigned)size;
969 }
reed@google.comacd471f2011-05-03 21:26:46 +0000970 if (this->needOpBytes(4 + SkAlign4(size))) {
971 this->writeOp(kDrawData_DrawOp, 0, data);
972 if (0 == data) {
commit-bot@chromium.orgb45bd1f2014-04-24 18:17:30 +0000973 fWriter.write32(SkToU32(size));
reed@google.comacd471f2011-05-03 21:26:46 +0000974 }
reed@google.combb6793b2011-05-05 15:18:15 +0000975 fWriter.writePad(ptr, size);
reed@google.combb6992a2011-04-26 17:41:56 +0000976 }
977 }
978}
979
robertphillips@google.com0a4805e2013-05-29 13:24:23 +0000980void SkGPipeCanvas::beginCommentGroup(const char* description) {
981 // ignore for now
982}
983
984void SkGPipeCanvas::addComment(const char* kywd, const char* value) {
985 // ignore for now
986}
987
988void SkGPipeCanvas::endCommentGroup() {
989 // ignore for now
990}
991
junov@chromium.org77eec242012-07-18 17:54:45 +0000992void SkGPipeCanvas::flushRecording(bool detachCurrentBlock) {
commit-bot@chromium.org2d91eff2014-05-07 15:27:10 +0000993 this->doNotify();
junov@chromium.org77eec242012-07-18 17:54:45 +0000994 if (detachCurrentBlock) {
995 // force a new block to be requested for the next recorded command
rmistry@google.comd6176b02012-08-23 18:14:13 +0000996 fBlockSize = 0;
junov@chromium.org77eec242012-07-18 17:54:45 +0000997 }
998}
999
junov@chromium.org2e14ba82012-08-07 14:26:57 +00001000size_t SkGPipeCanvas::freeMemoryIfPossible(size_t bytesToFree) {
scroggo@google.comd5d158b2012-08-14 20:38:28 +00001001 return (NULL == fBitmapHeap) ? 0 : fBitmapHeap->freeMemoryIfPossible(bytesToFree);
junov@chromium.org2e14ba82012-08-07 14:26:57 +00001002}
1003
reed@google.combb6992a2011-04-26 17:41:56 +00001004///////////////////////////////////////////////////////////////////////////////
1005
1006template <typename T> uint32_t castToU32(T value) {
1007 union {
1008 T fSrc;
1009 uint32_t fDst;
1010 } data;
1011 data.fSrc = value;
1012 return data.fDst;
1013}
1014
reed@google.com31891582011-05-12 03:03:56 +00001015void SkGPipeCanvas::writePaint(const SkPaint& paint) {
scroggo@google.comd5d158b2012-08-14 20:38:28 +00001016 if (fDone) {
1017 return;
1018 }
reed@google.com31891582011-05-12 03:03:56 +00001019 SkPaint& base = fPaint;
reed@google.combb6992a2011-04-26 17:41:56 +00001020 uint32_t storage[32];
1021 uint32_t* ptr = storage;
reed@google.combb6992a2011-04-26 17:41:56 +00001022
1023 if (base.getFlags() != paint.getFlags()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001024 *ptr++ = PaintOp_packOpData(kFlags_PaintOp, paint.getFlags());
reed@google.comf5842f72011-05-04 18:30:04 +00001025 base.setFlags(paint.getFlags());
reed@google.combb6992a2011-04-26 17:41:56 +00001026 }
1027 if (base.getColor() != paint.getColor()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001028 *ptr++ = PaintOp_packOp(kColor_PaintOp);
1029 *ptr++ = paint.getColor();
reed@google.comf5842f72011-05-04 18:30:04 +00001030 base.setColor(paint.getColor());
reed@google.combb6992a2011-04-26 17:41:56 +00001031 }
commit-bot@chromium.org85faf502014-04-16 12:58:02 +00001032 if (base.getFilterLevel() != paint.getFilterLevel()) {
1033 *ptr++ = PaintOp_packOpData(kFilterLevel_PaintOp, paint.getFilterLevel());
1034 base.setFilterLevel(paint.getFilterLevel());
1035 }
reed@google.combb6992a2011-04-26 17:41:56 +00001036 if (base.getStyle() != paint.getStyle()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001037 *ptr++ = PaintOp_packOpData(kStyle_PaintOp, paint.getStyle());
reed@google.comf5842f72011-05-04 18:30:04 +00001038 base.setStyle(paint.getStyle());
reed@google.combb6992a2011-04-26 17:41:56 +00001039 }
1040 if (base.getStrokeJoin() != paint.getStrokeJoin()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001041 *ptr++ = PaintOp_packOpData(kJoin_PaintOp, paint.getStrokeJoin());
reed@google.comf5842f72011-05-04 18:30:04 +00001042 base.setStrokeJoin(paint.getStrokeJoin());
reed@google.combb6992a2011-04-26 17:41:56 +00001043 }
1044 if (base.getStrokeCap() != paint.getStrokeCap()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001045 *ptr++ = PaintOp_packOpData(kCap_PaintOp, paint.getStrokeCap());
reed@google.comf5842f72011-05-04 18:30:04 +00001046 base.setStrokeCap(paint.getStrokeCap());
reed@google.combb6992a2011-04-26 17:41:56 +00001047 }
1048 if (base.getStrokeWidth() != paint.getStrokeWidth()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001049 *ptr++ = PaintOp_packOp(kWidth_PaintOp);
1050 *ptr++ = castToU32(paint.getStrokeWidth());
reed@google.comf5842f72011-05-04 18:30:04 +00001051 base.setStrokeWidth(paint.getStrokeWidth());
reed@google.combb6992a2011-04-26 17:41:56 +00001052 }
1053 if (base.getStrokeMiter() != paint.getStrokeMiter()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001054 *ptr++ = PaintOp_packOp(kMiter_PaintOp);
1055 *ptr++ = castToU32(paint.getStrokeMiter());
reed@google.comf5842f72011-05-04 18:30:04 +00001056 base.setStrokeMiter(paint.getStrokeMiter());
reed@google.combb6992a2011-04-26 17:41:56 +00001057 }
1058 if (base.getTextEncoding() != paint.getTextEncoding()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001059 *ptr++ = PaintOp_packOpData(kEncoding_PaintOp, paint.getTextEncoding());
reed@google.comf5842f72011-05-04 18:30:04 +00001060 base.setTextEncoding(paint.getTextEncoding());
reed@google.combb6992a2011-04-26 17:41:56 +00001061 }
1062 if (base.getHinting() != paint.getHinting()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001063 *ptr++ = PaintOp_packOpData(kHinting_PaintOp, paint.getHinting());
reed@google.comf5842f72011-05-04 18:30:04 +00001064 base.setHinting(paint.getHinting());
reed@google.combb6992a2011-04-26 17:41:56 +00001065 }
1066 if (base.getTextAlign() != paint.getTextAlign()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001067 *ptr++ = PaintOp_packOpData(kAlign_PaintOp, paint.getTextAlign());
reed@google.comf5842f72011-05-04 18:30:04 +00001068 base.setTextAlign(paint.getTextAlign());
reed@google.combb6992a2011-04-26 17:41:56 +00001069 }
1070 if (base.getTextSize() != paint.getTextSize()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001071 *ptr++ = PaintOp_packOp(kTextSize_PaintOp);
1072 *ptr++ = castToU32(paint.getTextSize());
reed@google.comf5842f72011-05-04 18:30:04 +00001073 base.setTextSize(paint.getTextSize());
reed@google.combb6992a2011-04-26 17:41:56 +00001074 }
1075 if (base.getTextScaleX() != paint.getTextScaleX()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001076 *ptr++ = PaintOp_packOp(kTextScaleX_PaintOp);
1077 *ptr++ = castToU32(paint.getTextScaleX());
reed@google.comf5842f72011-05-04 18:30:04 +00001078 base.setTextScaleX(paint.getTextScaleX());
reed@google.combb6992a2011-04-26 17:41:56 +00001079 }
1080 if (base.getTextSkewX() != paint.getTextSkewX()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001081 *ptr++ = PaintOp_packOp(kTextSkewX_PaintOp);
1082 *ptr++ = castToU32(paint.getTextSkewX());
reed@google.comf5842f72011-05-04 18:30:04 +00001083 base.setTextSkewX(paint.getTextSkewX());
1084 }
1085
1086 if (!SkTypeface::Equal(base.getTypeface(), paint.getTypeface())) {
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +00001087 if (isCrossProcess(fFlags)) {
scroggo@google.com3cb969f2012-07-27 20:39:19 +00001088 uint32_t id = this->getTypefaceID(paint.getTypeface());
1089 *ptr++ = PaintOp_packOpData(kTypeface_PaintOp, id);
1090 } else if (this->needOpBytes(sizeof(void*))) {
1091 // Add to the set for ref counting.
1092 fTypefaceSet.add(paint.getTypeface());
1093 // It is safe to write the typeface to the stream before the rest
1094 // of the paint unless we ever send a kReset_PaintOp, which we
1095 // currently never do.
1096 this->writeOp(kSetTypeface_DrawOp);
1097 fWriter.writePtr(paint.getTypeface());
1098 }
reed@google.comf5842f72011-05-04 18:30:04 +00001099 base.setTypeface(paint.getTypeface());
reed@google.combb6992a2011-04-26 17:41:56 +00001100 }
reed@google.combb6992a2011-04-26 17:41:56 +00001101
scroggo@google.com4dffc592012-07-17 16:49:40 +00001102 // This is a new paint, so all old flats can be safely purged, if necessary.
1103 fFlattenableHeap.markAllFlatsSafeToDelete();
reed@google.comb55d1182011-05-11 00:42:04 +00001104 for (int i = 0; i < kCount_PaintFlats; i++) {
1105 int index = this->flattenToIndex(get_paintflat(paint, i), (PaintFlats)i);
scroggo@google.comd3ba5cc2012-07-09 16:05:53 +00001106 bool replaced = index < 0;
1107 if (replaced) {
1108 index = ~index;
1109 }
scroggo@google.com4dffc592012-07-17 16:49:40 +00001110 // Store the index of any flat that needs to be kept. 0 means no flat.
1111 if (index > 0) {
1112 fFlattenableHeap.markFlatForKeeping(index);
1113 }
1114 SkASSERT(index >= 0 && index <= fFlatDictionary.count());
scroggo@google.comd3ba5cc2012-07-09 16:05:53 +00001115 if (index != fCurrFlatIndex[i] || replaced) {
reed@google.comb55d1182011-05-11 00:42:04 +00001116 *ptr++ = PaintOp_packOpFlagData(kFlatIndex_PaintOp, i, index);
1117 fCurrFlatIndex[i] = index;
1118 }
1119 }
1120
reed@google.comacd471f2011-05-03 21:26:46 +00001121 size_t size = (char*)ptr - (char*)storage;
1122 if (size && this->needOpBytes(size)) {
commit-bot@chromium.orgb45bd1f2014-04-24 18:17:30 +00001123 this->writeOp(kPaintOp_DrawOp, 0, SkToU32(size));
reed@google.comb55d1182011-05-11 00:42:04 +00001124 fWriter.write(storage, size);
reed@google.combb6992a2011-04-26 17:41:56 +00001125 for (size_t i = 0; i < size/4; i++) {
reed@google.comb55d1182011-05-11 00:42:04 +00001126// SkDebugf("[%d] %08X\n", i, storage[i]);
reed@google.combb6992a2011-04-26 17:41:56 +00001127 }
1128 }
reed@google.com0cd2ac62013-10-14 20:02:44 +00001129
1130 //
1131 // Do these after we've written kPaintOp_DrawOp
skia.committer@gmail.comfbc58a32013-10-15 07:02:27 +00001132
reed@google.com0cd2ac62013-10-14 20:02:44 +00001133 if (base.getAnnotation() != paint.getAnnotation()) {
1134 if (NULL == paint.getAnnotation()) {
commit-bot@chromium.org89ff3dd2013-10-29 20:29:38 +00001135 if (this->needOpBytes()) {
1136 this->writeOp(kSetAnnotation_DrawOp, 0, 0);
1137 }
reed@google.com0cd2ac62013-10-14 20:02:44 +00001138 } else {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +00001139 SkWriteBuffer buffer;
reed@google.com0cd2ac62013-10-14 20:02:44 +00001140 paint.getAnnotation()->writeToBuffer(buffer);
commit-bot@chromium.org89ff3dd2013-10-29 20:29:38 +00001141 const size_t size = buffer.bytesWritten();
1142 if (this->needOpBytes(size)) {
commit-bot@chromium.orgb45bd1f2014-04-24 18:17:30 +00001143 this->writeOp(kSetAnnotation_DrawOp, 0, SkToU32(size));
commit-bot@chromium.org89ff3dd2013-10-29 20:29:38 +00001144 buffer.writeToMemory(fWriter.reserve(size));
1145 }
reed@google.com0cd2ac62013-10-14 20:02:44 +00001146 }
commit-bot@chromium.org40258a52013-10-29 19:23:26 +00001147 base.setAnnotation(paint.getAnnotation());
reed@google.com0cd2ac62013-10-14 20:02:44 +00001148 }
reed@google.combb6992a2011-04-26 17:41:56 +00001149}
1150
1151///////////////////////////////////////////////////////////////////////////////
1152
1153#include "SkGPipe.h"
1154
scroggo@google.com3cb969f2012-07-27 20:39:19 +00001155SkGPipeController::~SkGPipeController() {
1156 SkSafeUnref(fCanvas);
1157}
1158
1159void SkGPipeController::setCanvas(SkGPipeCanvas* canvas) {
1160 SkRefCnt_SafeAssign(fCanvas, canvas);
1161}
1162
1163///////////////////////////////////////////////////////////////////////////////
1164
1165SkGPipeWriter::SkGPipeWriter()
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +00001166: fWriter(0) {
reed@google.combb6992a2011-04-26 17:41:56 +00001167 fCanvas = NULL;
1168}
1169
1170SkGPipeWriter::~SkGPipeWriter() {
reed@google.comacd471f2011-05-03 21:26:46 +00001171 this->endRecording();
reed@google.combb6992a2011-04-26 17:41:56 +00001172}
1173
junov@chromium.orga8db8fe2012-08-15 19:49:22 +00001174SkCanvas* SkGPipeWriter::startRecording(SkGPipeController* controller, uint32_t flags,
1175 uint32_t width, uint32_t height) {
reed@google.combb6992a2011-04-26 17:41:56 +00001176 if (NULL == fCanvas) {
reed@google.comacd471f2011-05-03 21:26:46 +00001177 fWriter.reset(NULL, 0);
junov@chromium.orga8db8fe2012-08-15 19:49:22 +00001178 fCanvas = SkNEW_ARGS(SkGPipeCanvas, (controller, &fWriter, flags, width, height));
reed@google.combb6992a2011-04-26 17:41:56 +00001179 }
scroggo@google.com3cb969f2012-07-27 20:39:19 +00001180 controller->setCanvas(fCanvas);
reed@google.combb6992a2011-04-26 17:41:56 +00001181 return fCanvas;
1182}
1183
1184void SkGPipeWriter::endRecording() {
1185 if (fCanvas) {
scroggo@google.com59c3ab62013-11-12 14:32:38 +00001186 fCanvas->finish(true);
reed@google.combb6992a2011-04-26 17:41:56 +00001187 fCanvas->unref();
1188 fCanvas = NULL;
1189 }
1190}
1191
junov@chromium.org2e14ba82012-08-07 14:26:57 +00001192void SkGPipeWriter::flushRecording(bool detachCurrentBlock) {
1193 if (fCanvas) {
1194 fCanvas->flushRecording(detachCurrentBlock);
1195 }
junov@chromium.org77eec242012-07-18 17:54:45 +00001196}
1197
junov@chromium.org2e14ba82012-08-07 14:26:57 +00001198size_t SkGPipeWriter::freeMemoryIfPossible(size_t bytesToFree) {
1199 if (fCanvas) {
1200 return fCanvas->freeMemoryIfPossible(bytesToFree);
1201 }
1202 return 0;
1203}
1204
1205size_t SkGPipeWriter::storageAllocatedForRecording() const {
scroggo@google.com15011ee2012-07-26 20:03:32 +00001206 return NULL == fCanvas ? 0 : fCanvas->storageAllocatedForRecording();
1207}
1208
scroggo@google.com3e26bd02012-08-14 15:20:01 +00001209///////////////////////////////////////////////////////////////////////////////
1210
1211BitmapShuttle::BitmapShuttle(SkGPipeCanvas* canvas) {
1212 SkASSERT(canvas != NULL);
1213 fCanvas = canvas;
1214 fCanvas->ref();
1215}
1216
1217BitmapShuttle::~BitmapShuttle() {
scroggo@google.com59c3ab62013-11-12 14:32:38 +00001218 this->removeCanvas();
scroggo@google.com3e26bd02012-08-14 15:20:01 +00001219}
1220
1221bool BitmapShuttle::insert(const SkBitmap& bitmap, int32_t slot) {
scroggo@google.com59c3ab62013-11-12 14:32:38 +00001222 SkASSERT(fCanvas != NULL);
scroggo@google.com3e26bd02012-08-14 15:20:01 +00001223 return fCanvas->shuttleBitmap(bitmap, slot);
1224}
scroggo@google.com59c3ab62013-11-12 14:32:38 +00001225
1226void BitmapShuttle::removeCanvas() {
1227 if (NULL == fCanvas) {
1228 return;
1229 }
1230 fCanvas->unref();
1231 fCanvas = NULL;
1232}
commit-bot@chromium.org2d91eff2014-05-07 15:27:10 +00001233