blob: a1e7dc2872405f5e26ab630a537276ff7956b12b [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@google.combb6992a2011-04-26 17:41:56 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
reed@google.combb6992a2011-04-26 17:41:56 +00007 */
8
epoger@google.comb58772f2013-03-08 09:09:10 +00009#include "SkAnnotation.h"
robertphillips@google.com1f2f3382013-08-29 11:54:56 +000010#include "SkBitmapDevice.h"
scroggo@google.com10dccde2012-08-08 20:43:22 +000011#include "SkBitmapHeap.h"
reed@google.combb6992a2011-04-26 17:41:56 +000012#include "SkCanvas.h"
scroggo@google.com10dccde2012-08-08 20:43:22 +000013#include "SkColorFilter.h"
reed@google.com8a85d0c2011-06-24 19:12:12 +000014#include "SkData.h"
scroggo@google.com10dccde2012-08-08 20:43:22 +000015#include "SkDrawLooper.h"
reed@google.comacd471f2011-05-03 21:26:46 +000016#include "SkGPipe.h"
reed@google.combb6992a2011-04-26 17:41:56 +000017#include "SkGPipePriv.h"
scroggo@google.com16d1d0b2012-05-02 19:09:40 +000018#include "SkImageFilter.h"
scroggo@google.com10dccde2012-08-08 20:43:22 +000019#include "SkMaskFilter.h"
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000020#include "SkWriteBuffer.h"
scroggo@google.com10dccde2012-08-08 20:43:22 +000021#include "SkPaint.h"
dandovb3c9d1c2014-08-12 08:34:29 -070022#include "SkPatchUtils.h"
scroggo@google.com10dccde2012-08-08 20:43:22 +000023#include "SkPathEffect.h"
24#include "SkPictureFlat.h"
fmalitaacb882c2014-09-16 17:58:34 -070025#include "SkPtrRecorder.h"
scroggo@google.com10dccde2012-08-08 20:43:22 +000026#include "SkRasterizer.h"
reed@google.com4ed0fb72012-12-12 20:48:18 +000027#include "SkRRect.h"
scroggo@google.com10dccde2012-08-08 20:43:22 +000028#include "SkShader.h"
reed@google.comf5842f72011-05-04 18:30:04 +000029#include "SkStream.h"
fmalita228a6f22014-08-28 13:59:42 -070030#include "SkTextBlob.h"
reed@google.comb55d1182011-05-11 00:42:04 +000031#include "SkTSearch.h"
reed@google.comf5842f72011-05-04 18:30:04 +000032#include "SkTypeface.h"
reed@google.combb6992a2011-04-26 17:41:56 +000033#include "SkWriter32.h"
reed@google.comb55d1182011-05-11 00:42:04 +000034
reed@google.com4ed0fb72012-12-12 20:48:18 +000035enum {
36 kSizeOfFlatRRect = sizeof(SkRect) + 4 * sizeof(SkVector)
37};
38
fmalitaacb882c2014-09-16 17:58:34 -070039static bool is_cross_process(uint32_t flags) {
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +000040 return SkToBool(flags & SkGPipeWriter::kCrossProcess_Flag);
41}
42
reed@google.comb55d1182011-05-11 00:42:04 +000043static SkFlattenable* get_paintflat(const SkPaint& paint, unsigned paintFlat) {
44 SkASSERT(paintFlat < kCount_PaintFlats);
45 switch (paintFlat) {
46 case kColorFilter_PaintFlat: return paint.getColorFilter();
reed@google.com0faac1e2011-05-11 05:58:58 +000047 case kDrawLooper_PaintFlat: return paint.getLooper();
reed@google.comb55d1182011-05-11 00:42:04 +000048 case kMaskFilter_PaintFlat: return paint.getMaskFilter();
49 case kPathEffect_PaintFlat: return paint.getPathEffect();
50 case kRasterizer_PaintFlat: return paint.getRasterizer();
51 case kShader_PaintFlat: return paint.getShader();
scroggo@google.com16d1d0b2012-05-02 19:09:40 +000052 case kImageFilter_PaintFlat: return paint.getImageFilter();
reed@google.comb55d1182011-05-11 00:42:04 +000053 case kXfermode_PaintFlat: return paint.getXfermode();
54 }
tomhudson@google.com0c00f212011-12-28 14:59:50 +000055 SkDEBUGFAIL("never gets here");
reed@google.comb55d1182011-05-11 00:42:04 +000056 return NULL;
57}
reed@google.combb6992a2011-04-26 17:41:56 +000058
reed@google.comf5842f72011-05-04 18:30:04 +000059static size_t writeTypeface(SkWriter32* writer, SkTypeface* typeface) {
60 SkASSERT(typeface);
61 SkDynamicMemoryWStream stream;
62 typeface->serialize(&stream);
63 size_t size = stream.getOffset();
64 if (writer) {
commit-bot@chromium.orgb45bd1f2014-04-24 18:17:30 +000065 writer->write32(SkToU32(size));
reed@google.com8a85d0c2011-06-24 19:12:12 +000066 SkAutoDataUnref data(stream.copyToData());
robertphillips@google.com59f46b82012-07-10 17:30:58 +000067 writer->writePad(data->data(), size);
reed@google.comf5842f72011-05-04 18:30:04 +000068 }
scroggo@google.com5af9b202012-06-04 17:17:36 +000069 return 4 + SkAlign4(size);
reed@google.comf5842f72011-05-04 18:30:04 +000070}
71
reed@google.combb6992a2011-04-26 17:41:56 +000072///////////////////////////////////////////////////////////////////////////////
73
scroggo@google.com4dffc592012-07-17 16:49:40 +000074class FlattenableHeap : public SkFlatController {
75public:
scroggo@google.com664fab12012-08-14 19:22:05 +000076 FlattenableHeap(int numFlatsToKeep, SkNamedFactorySet* fset, bool isCrossProcess)
commit-bot@chromium.orga2bd2d12014-01-30 22:16:32 +000077 : INHERITED(isCrossProcess ? SkWriteBuffer::kCrossProcess_Flag : 0)
78 , fNumFlatsToKeep(numFlatsToKeep) {
scroggo@google.com664fab12012-08-14 19:22:05 +000079 SkASSERT((isCrossProcess && fset != NULL) || (!isCrossProcess && NULL == fset));
80 if (isCrossProcess) {
81 this->setNamedFactorySet(fset);
scroggo@google.com664fab12012-08-14 19:22:05 +000082 }
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +000083 }
scroggo@google.com4dffc592012-07-17 16:49:40 +000084
85 ~FlattenableHeap() {
86 fPointers.freeAll();
87 }
88
89 virtual void* allocThrow(size_t bytes) SK_OVERRIDE;
90
91 virtual void unalloc(void* ptr) SK_OVERRIDE;
92
scroggo@google.com7ca24432012-08-14 15:48:43 +000093 void setBitmapStorage(SkBitmapHeap* heap) {
94 this->setBitmapHeap(heap);
95 }
rmistry@google.comd6176b02012-08-23 18:14:13 +000096
scroggo@google.com4dffc592012-07-17 16:49:40 +000097 const SkFlatData* flatToReplace() const;
98
99 // Mark an SkFlatData as one that should not be returned by flatToReplace.
100 // Takes the result of SkFlatData::index() as its parameter.
101 void markFlatForKeeping(int index) {
102 *fFlatsThatMustBeKept.append() = index;
103 }
104
105 void markAllFlatsSafeToDelete() {
106 fFlatsThatMustBeKept.reset();
107 }
108
109private:
110 // Keep track of the indices (i.e. the result of SkFlatData::index()) of
111 // flats that must be kept, since they are on the current paint.
112 SkTDArray<int> fFlatsThatMustBeKept;
113 SkTDArray<void*> fPointers;
114 const int fNumFlatsToKeep;
commit-bot@chromium.orga2bd2d12014-01-30 22:16:32 +0000115
116 typedef SkFlatController INHERITED;
scroggo@google.com4dffc592012-07-17 16:49:40 +0000117};
118
119void FlattenableHeap::unalloc(void* ptr) {
120 int indexToRemove = fPointers.rfind(ptr);
121 if (indexToRemove >= 0) {
122 sk_free(ptr);
123 fPointers.remove(indexToRemove);
124 }
125}
126
127void* FlattenableHeap::allocThrow(size_t bytes) {
128 void* ptr = sk_malloc_throw(bytes);
129 *fPointers.append() = ptr;
130 return ptr;
131}
132
133const SkFlatData* FlattenableHeap::flatToReplace() const {
134 // First, determine whether we should replace one.
135 if (fPointers.count() > fNumFlatsToKeep) {
136 // Look through the flattenable heap.
137 // TODO: Return the LRU flat.
138 for (int i = 0; i < fPointers.count(); i++) {
139 SkFlatData* potential = (SkFlatData*)fPointers[i];
140 // Make sure that it is not one that must be kept.
141 bool mustKeep = false;
142 for (int j = 0; j < fFlatsThatMustBeKept.count(); j++) {
143 if (potential->index() == fFlatsThatMustBeKept[j]) {
144 mustKeep = true;
145 break;
146 }
147 }
148 if (!mustKeep) {
149 return potential;
150 }
151 }
152 }
153 return NULL;
154}
155
156///////////////////////////////////////////////////////////////////////////////
157
commit-bot@chromium.org07adb632014-01-02 22:20:49 +0000158struct SkFlattenableTraits {
commit-bot@chromium.org186c0cc2014-02-18 16:15:05 +0000159 static void Flatten(SkWriteBuffer& buffer, const SkFlattenable& flattenable) {
commit-bot@chromium.org07adb632014-01-02 22:20:49 +0000160 buffer.writeFlattenable(&flattenable);
scroggo@google.com4dffc592012-07-17 16:49:40 +0000161 }
commit-bot@chromium.org07adb632014-01-02 22:20:49 +0000162 // No need to define unflatten if we never call it.
scroggo@google.com4dffc592012-07-17 16:49:40 +0000163};
commit-bot@chromium.org07adb632014-01-02 22:20:49 +0000164typedef SkFlatDictionary<SkFlattenable, SkFlattenableTraits> FlatDictionary;
scroggo@google.com4dffc592012-07-17 16:49:40 +0000165
166///////////////////////////////////////////////////////////////////////////////
167
scroggo@google.com59c3ab62013-11-12 14:32:38 +0000168/**
169 * If SkBitmaps are to be flattened to send to the reader, this class is
170 * provided to the SkBitmapHeap to tell the SkGPipeCanvas to do so.
171 */
172class BitmapShuttle : public SkBitmapHeap::ExternalStorage {
173public:
174 BitmapShuttle(SkGPipeCanvas*);
175
176 ~BitmapShuttle();
177
178 virtual bool insert(const SkBitmap& bitmap, int32_t slot) SK_OVERRIDE;
179
180 /**
181 * Remove the SkGPipeCanvas used for insertion. After this, calls to
182 * insert will crash.
183 */
184 void removeCanvas();
185
186private:
187 SkGPipeCanvas* fCanvas;
188};
189
190///////////////////////////////////////////////////////////////////////////////
191
reed@google.combb6992a2011-04-26 17:41:56 +0000192class SkGPipeCanvas : public SkCanvas {
193public:
junov@chromium.orga8db8fe2012-08-15 19:49:22 +0000194 SkGPipeCanvas(SkGPipeController*, SkWriter32*, uint32_t flags,
195 uint32_t width, uint32_t height);
reed@google.combb6992a2011-04-26 17:41:56 +0000196 virtual ~SkGPipeCanvas();
197
scroggo@google.com59c3ab62013-11-12 14:32:38 +0000198 /**
199 * Called when nothing else is to be written to the stream. Any repeated
200 * calls are ignored.
201 *
202 * @param notifyReaders Whether to send a message to the reader(s) that
203 * the writer is through sending commands. Should generally be true,
204 * unless there is an error which prevents further messages from
205 * being sent.
206 */
207 void finish(bool notifyReaders) {
208 if (fDone) {
209 return;
reed@google.combb6992a2011-04-26 17:41:56 +0000210 }
scroggo@google.com59c3ab62013-11-12 14:32:38 +0000211 if (notifyReaders && this->needOpBytes()) {
212 this->writeOp(kDone_DrawOp);
213 this->doNotify();
214 }
215 if (shouldFlattenBitmaps(fFlags)) {
216 // The following circular references exist:
217 // fFlattenableHeap -> fWriteBuffer -> fBitmapStorage -> fExternalStorage -> fCanvas
218 // fBitmapHeap -> fExternalStorage -> fCanvas
219 // fFlattenableHeap -> fBitmapStorage -> fExternalStorage -> fCanvas
220
221 // Break them all by destroying the final link to this SkGPipeCanvas.
222 fBitmapShuttle->removeCanvas();
223 }
224 fDone = true;
reed@google.combb6992a2011-04-26 17:41:56 +0000225 }
226
junov@chromium.org77eec242012-07-18 17:54:45 +0000227 void flushRecording(bool detachCurrentBlock);
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000228 size_t freeMemoryIfPossible(size_t bytesToFree);
junov@chromium.org77eec242012-07-18 17:54:45 +0000229
scroggo@google.com15011ee2012-07-26 20:03:32 +0000230 size_t storageAllocatedForRecording() {
scroggo@google.comd5d158b2012-08-14 20:38:28 +0000231 return (NULL == fBitmapHeap) ? 0 : fBitmapHeap->bytesAllocated();
scroggo@google.com15011ee2012-07-26 20:03:32 +0000232 }
233
reed@google.combb6992a2011-04-26 17:41:56 +0000234 // overrides from SkCanvas
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000235 virtual bool isDrawingToLayer() const SK_OVERRIDE;
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000236 virtual void clear(SkColor) SK_OVERRIDE;
237 virtual void drawPaint(const SkPaint& paint) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000238 virtual void drawPoints(PointMode, size_t count, const SkPoint pts[],
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000239 const SkPaint&) SK_OVERRIDE;
reed@google.com4ed0fb72012-12-12 20:48:18 +0000240 virtual void drawOval(const SkRect&, const SkPaint&) SK_OVERRIDE;
bsalomon@google.com7ce564c2013-10-22 16:54:15 +0000241 virtual void drawRect(const SkRect& rect, const SkPaint&) SK_OVERRIDE;
reed@google.com4ed0fb72012-12-12 20:48:18 +0000242 virtual void drawRRect(const SkRRect&, const SkPaint&) SK_OVERRIDE;
bsalomon@google.com7ce564c2013-10-22 16:54:15 +0000243 virtual void drawPath(const SkPath& path, const SkPaint&) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000244 virtual void drawBitmap(const SkBitmap&, SkScalar left, SkScalar top,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000245 const SkPaint*) SK_OVERRIDE;
reed@google.com71121732012-09-18 15:14:33 +0000246 virtual void drawBitmapRectToRect(const SkBitmap&, const SkRect* src,
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000247 const SkRect& dst, const SkPaint* paint,
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +0000248 DrawBitmapRectFlags flags) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000249 virtual void drawBitmapMatrix(const SkBitmap&, const SkMatrix&,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000250 const SkPaint*) SK_OVERRIDE;
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000251 virtual void drawBitmapNine(const SkBitmap& bitmap, const SkIRect& center,
252 const SkRect& dst, const SkPaint* paint = NULL) SK_OVERRIDE;
piotaixr9bb75392014-10-22 11:02:32 -0700253 virtual void drawImage(const SkImage* image, SkScalar left, SkScalar top,
254 const SkPaint* paint) SK_OVERRIDE;
255 virtual void drawImageRect(const SkImage* image, const SkRect* src,
256 const SkRect& dst,
257 const SkPaint* paint) 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;
reed@google.combb6992a2011-04-26 17:41:56 +0000260 virtual void drawVertices(VertexMode, int vertexCount,
261 const SkPoint vertices[], const SkPoint texs[],
262 const SkColor colors[], SkXfermode*,
263 const uint16_t indices[], int indexCount,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000264 const SkPaint&) SK_OVERRIDE;
265 virtual void drawData(const void*, size_t) SK_OVERRIDE;
robertphillips@google.com0a4805e2013-05-29 13:24:23 +0000266 virtual void beginCommentGroup(const char* description) SK_OVERRIDE;
267 virtual void addComment(const char* kywd, const char* value) SK_OVERRIDE;
268 virtual void endCommentGroup() SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000269
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000270 /**
271 * Flatten an SkBitmap to send to the reader, where it will be referenced
272 * according to slot.
273 */
274 bool shuttleBitmap(const SkBitmap&, int32_t slot);
commit-bot@chromium.orged9806f2014-02-21 02:32:36 +0000275
276protected:
Florin Malita5f6102d2014-06-30 10:13:28 -0400277 virtual void willSave() SK_OVERRIDE;
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000278 virtual SaveLayerStrategy willSaveLayer(const SkRect*, const SkPaint*, SaveFlags) SK_OVERRIDE;
279 virtual void willRestore() SK_OVERRIDE;
280
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +0000281 virtual void didConcat(const SkMatrix&) SK_OVERRIDE;
282 virtual void didSetMatrix(const SkMatrix&) SK_OVERRIDE;
283
commit-bot@chromium.orged9806f2014-02-21 02:32:36 +0000284 virtual void onDrawDRRect(const SkRRect&, const SkRRect&, const SkPaint&) SK_OVERRIDE;
reed@google.come0d9ce82014-04-23 04:00:17 +0000285 virtual void onDrawText(const void* text, size_t byteLength, SkScalar x, SkScalar y,
286 const SkPaint&) SK_OVERRIDE;
287 virtual void onDrawPosText(const void* text, size_t byteLength, const SkPoint pos[],
288 const SkPaint&) SK_OVERRIDE;
289 virtual void onDrawPosTextH(const void* text, size_t byteLength, const SkScalar xpos[],
290 SkScalar constY, const SkPaint&) SK_OVERRIDE;
291 virtual void onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& path,
292 const SkMatrix* matrix, const SkPaint&) SK_OVERRIDE;
fmalitab7425172014-08-26 07:56:44 -0700293 virtual void onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
294 const SkPaint& paint) SK_OVERRIDE;
dandovb3c9d1c2014-08-12 08:34:29 -0700295 virtual void onDrawPatch(const SkPoint cubics[12], const SkColor colors[4],
296 const SkPoint texCoords[4], SkXfermode* xmode,
297 const SkPaint& paint) SK_OVERRIDE;
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000298 virtual void onClipRect(const SkRect&, SkRegion::Op, ClipEdgeStyle) SK_OVERRIDE;
299 virtual void onClipRRect(const SkRRect&, SkRegion::Op, ClipEdgeStyle) SK_OVERRIDE;
300 virtual void onClipPath(const SkPath&, SkRegion::Op, ClipEdgeStyle) SK_OVERRIDE;
301 virtual void onClipRegion(const SkRegion&, SkRegion::Op) SK_OVERRIDE;
302
reedd5fa1a42014-08-09 11:08:05 -0700303 virtual void onDrawPicture(const SkPicture*, const SkMatrix*, const SkPaint*) SK_OVERRIDE;
robertphillips9b14f262014-06-04 05:40:44 -0700304
reed@google.combb6992a2011-04-26 17:41:56 +0000305private:
commit-bot@chromium.orgd9ea09e2014-03-25 17:32:26 +0000306 void recordTranslate(const SkMatrix&);
307 void recordScale(const SkMatrix&);
308 void recordConcat(const SkMatrix&);
309
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000310 enum {
311 kNoSaveLayer = -1,
312 };
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000313 SkNamedFactorySet* fFactorySet;
314 int fFirstSaveLayerStackLevel;
scroggo@google.comd9d29672012-08-14 17:21:34 +0000315 SkBitmapHeap* fBitmapHeap;
reed@google.comacd471f2011-05-03 21:26:46 +0000316 SkGPipeController* fController;
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000317 SkWriter32& fWriter;
318 size_t fBlockSize; // amount allocated for writer
319 size_t fBytesNotified;
320 bool fDone;
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000321 const uint32_t fFlags;
reed@google.combb6992a2011-04-26 17:41:56 +0000322
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000323 SkRefCntSet fTypefaceSet;
reed@google.comf5842f72011-05-04 18:30:04 +0000324
325 uint32_t getTypefaceID(SkTypeface*);
326
reed@google.comacd471f2011-05-03 21:26:46 +0000327 inline void writeOp(DrawOps op, unsigned flags, unsigned data) {
reed@google.combb6992a2011-04-26 17:41:56 +0000328 fWriter.write32(DrawOp_packOpFlagData(op, flags, data));
329 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000330
reed@google.comacd471f2011-05-03 21:26:46 +0000331 inline void writeOp(DrawOps op) {
reed@google.combb6992a2011-04-26 17:41:56 +0000332 fWriter.write32(DrawOp_packOpFlagData(op, 0, 0));
333 }
reed@google.comacd471f2011-05-03 21:26:46 +0000334
335 bool needOpBytes(size_t size = 0);
336
337 inline void doNotify() {
338 if (!fDone) {
reed@google.com44699382013-10-31 17:28:30 +0000339 size_t bytes = fWriter.bytesWritten() - fBytesNotified;
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000340 if (bytes > 0) {
341 fController->notifyWritten(bytes);
342 fBytesNotified += bytes;
343 }
reed@google.comacd471f2011-05-03 21:26:46 +0000344 }
345 }
reed@google.comb55d1182011-05-11 00:42:04 +0000346
fmalitaacb882c2014-09-16 17:58:34 -0700347 typedef SkAutoSTMalloc<128, uint8_t> TypefaceBuffer;
348 size_t getInProcessTypefaces(const SkRefCntSet& typefaceSet, TypefaceBuffer*);
349 size_t getCrossProcessTypefaces(const SkRefCntSet& typefaceSet, TypefaceBuffer*);
350
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000351 // Should be called after any calls to an SkFlatDictionary::findAndReplace
352 // if a new SkFlatData was added when in cross process mode
353 void flattenFactoryNames();
354
scroggo@google.com59c3ab62013-11-12 14:32:38 +0000355 FlattenableHeap fFlattenableHeap;
356 FlatDictionary fFlatDictionary;
357 SkAutoTUnref<BitmapShuttle> fBitmapShuttle;
358 int fCurrFlatIndex[kCount_PaintFlats];
359
reed@google.comb55d1182011-05-11 00:42:04 +0000360 int flattenToIndex(SkFlattenable* obj, PaintFlats);
361
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000362 // Common code used by drawBitmap*. Behaves differently depending on the
363 // type of SkBitmapHeap being used, which is determined by the flags used.
364 bool commonDrawBitmap(const SkBitmap& bm, DrawOps op, unsigned flags,
365 size_t opBytesNeeded, const SkPaint* paint);
scroggo@google.com58be6822012-07-30 14:40:01 +0000366
piotaixr9bb75392014-10-22 11:02:32 -0700367 bool commonDrawImage(const SkImage* image, DrawOps op, unsigned flags,
368 size_t opBytesNeeded, const SkPaint* paint);
369
reed@google.com31891582011-05-12 03:03:56 +0000370 SkPaint fPaint;
371 void writePaint(const SkPaint&);
reed@google.combb6992a2011-04-26 17:41:56 +0000372
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000373 class AutoPipeNotify {
374 public:
375 AutoPipeNotify(SkGPipeCanvas* canvas) : fCanvas(canvas) {}
376 ~AutoPipeNotify() { fCanvas->doNotify(); }
377 private:
378 SkGPipeCanvas* fCanvas;
379 };
380 friend class AutoPipeNotify;
381
reed@google.combb6992a2011-04-26 17:41:56 +0000382 typedef SkCanvas INHERITED;
383};
384
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000385void SkGPipeCanvas::flattenFactoryNames() {
386 const char* name;
387 while ((name = fFactorySet->getNextAddedFactoryName()) != NULL) {
388 size_t len = strlen(name);
commit-bot@chromium.orgd0490172014-05-14 15:12:08 +0000389 if (this->needOpBytes(SkWriter32::WriteStringSize(name, len))) {
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000390 this->writeOp(kDef_Factory_DrawOp);
391 fWriter.writeString(name, len);
392 }
393 }
394}
395
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000396bool SkGPipeCanvas::shuttleBitmap(const SkBitmap& bm, int32_t slot) {
scroggo@google.com565254b2012-06-28 15:41:32 +0000397 SkASSERT(shouldFlattenBitmaps(fFlags));
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000398 SkWriteBuffer buffer;
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000399 buffer.setNamedFactoryRecorder(fFactorySet);
scroggo@google.com74b7ffd2013-04-30 02:32:41 +0000400 buffer.writeBitmap(bm);
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000401 this->flattenFactoryNames();
commit-bot@chromium.orgb45bd1f2014-04-24 18:17:30 +0000402 size_t size = buffer.bytesWritten();
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000403 if (this->needOpBytes(size)) {
404 this->writeOp(kDef_Bitmap_DrawOp, 0, slot);
405 void* dst = static_cast<void*>(fWriter.reserve(size));
406 buffer.writeToMemory(dst);
407 return true;
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000408 }
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000409 return false;
scroggo@google.comd3ba5cc2012-07-09 16:05:53 +0000410}
411
reed@google.comb55d1182011-05-11 00:42:04 +0000412// return 0 for NULL (or unflattenable obj), or index-base-1
scroggo@google.comd3ba5cc2012-07-09 16:05:53 +0000413// return ~(index-base-1) if an old flattenable was replaced
reed@google.comb55d1182011-05-11 00:42:04 +0000414int SkGPipeCanvas::flattenToIndex(SkFlattenable* obj, PaintFlats paintflat) {
scroggo@google.comd5d158b2012-08-14 20:38:28 +0000415 SkASSERT(!fDone && fBitmapHeap != NULL);
reed@google.comb55d1182011-05-11 00:42:04 +0000416 if (NULL == obj) {
417 return 0;
418 }
reed@google.comb55d1182011-05-11 00:42:04 +0000419
scroggo@google.comd9d29672012-08-14 17:21:34 +0000420 fBitmapHeap->deferAddingOwners();
scroggo@google.com4dffc592012-07-17 16:49:40 +0000421 bool added, replaced;
scroggo@google.com664fab12012-08-14 19:22:05 +0000422 const SkFlatData* flat = fFlatDictionary.findAndReplace(*obj, fFlattenableHeap.flatToReplace(),
423 &added, &replaced);
scroggo@google.comd9d29672012-08-14 17:21:34 +0000424 fBitmapHeap->endAddingOwnersDeferral(added);
scroggo@google.com4dffc592012-07-17 16:49:40 +0000425 int index = flat->index();
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000426 if (added) {
fmalitaacb882c2014-09-16 17:58:34 -0700427 if (is_cross_process(fFlags)) {
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000428 this->flattenFactoryNames();
429 }
430 size_t flatSize = flat->flatSize();
431 if (this->needOpBytes(flatSize)) {
432 this->writeOp(kDef_Flattenable_DrawOp, paintflat, index);
433 fWriter.write(flat->data(), flatSize);
434 }
scroggo@google.com4dffc592012-07-17 16:49:40 +0000435 }
436 if (replaced) {
reed@google.comb55d1182011-05-11 00:42:04 +0000437 index = ~index;
reed@google.comb55d1182011-05-11 00:42:04 +0000438 }
scroggo@google.com4dffc592012-07-17 16:49:40 +0000439 return index;
reed@google.comb55d1182011-05-11 00:42:04 +0000440}
441
reed@google.combb6992a2011-04-26 17:41:56 +0000442///////////////////////////////////////////////////////////////////////////////
443
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000444#define MIN_BLOCK_SIZE (16 * 1024)
scroggo@google.com4dffc592012-07-17 16:49:40 +0000445#define BITMAPS_TO_KEEP 5
446#define FLATTENABLES_TO_KEEP 10
reed@google.combb6992a2011-04-26 17:41:56 +0000447
reed@google.comacd471f2011-05-03 21:26:46 +0000448SkGPipeCanvas::SkGPipeCanvas(SkGPipeController* controller,
rmistry@google.comd6176b02012-08-23 18:14:13 +0000449 SkWriter32* writer, uint32_t flags,
junov@chromium.orga8db8fe2012-08-15 19:49:22 +0000450 uint32_t width, uint32_t height)
commit-bot@chromium.org403f8d72014-02-17 15:24:26 +0000451 : SkCanvas(width, height)
fmalitaacb882c2014-09-16 17:58:34 -0700452 , fFactorySet(is_cross_process(flags) ? SkNEW(SkNamedFactorySet) : NULL)
commit-bot@chromium.org403f8d72014-02-17 15:24:26 +0000453 , fWriter(*writer)
454 , fFlags(flags)
fmalitaacb882c2014-09-16 17:58:34 -0700455 , fFlattenableHeap(FLATTENABLES_TO_KEEP, fFactorySet, is_cross_process(flags))
commit-bot@chromium.org403f8d72014-02-17 15:24:26 +0000456 , fFlatDictionary(&fFlattenableHeap)
457{
reed@google.comacd471f2011-05-03 21:26:46 +0000458 fController = controller;
reed@google.combb6992a2011-04-26 17:41:56 +0000459 fDone = false;
reed@google.comacd471f2011-05-03 21:26:46 +0000460 fBlockSize = 0; // need first block from controller
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000461 fBytesNotified = 0;
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000462 fFirstSaveLayerStackLevel = kNoSaveLayer;
reed@google.comb55d1182011-05-11 00:42:04 +0000463 sk_bzero(fCurrFlatIndex, sizeof(fCurrFlatIndex));
reed@google.comacd471f2011-05-03 21:26:46 +0000464
scroggo@google.com565254b2012-06-28 15:41:32 +0000465 // Tell the reader the appropriate flags to use.
466 if (this->needOpBytes()) {
467 this->writeOp(kReportFlags_DrawOp, fFlags, 0);
468 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000469
scroggo@google.com10dccde2012-08-08 20:43:22 +0000470 if (shouldFlattenBitmaps(flags)) {
scroggo@google.com59c3ab62013-11-12 14:32:38 +0000471 fBitmapShuttle.reset(SkNEW_ARGS(BitmapShuttle, (this)));
472 fBitmapHeap = SkNEW_ARGS(SkBitmapHeap, (fBitmapShuttle.get(), BITMAPS_TO_KEEP));
scroggo@google.com10dccde2012-08-08 20:43:22 +0000473 } else {
scroggo@google.comd9d29672012-08-14 17:21:34 +0000474 fBitmapHeap = SkNEW_ARGS(SkBitmapHeap,
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000475 (BITMAPS_TO_KEEP, controller->numberOfReaders()));
scroggo@google.com10dccde2012-08-08 20:43:22 +0000476 if (this->needOpBytes(sizeof(void*))) {
scroggo@google.comd9d29672012-08-14 17:21:34 +0000477 this->writeOp(kShareBitmapHeap_DrawOp);
478 fWriter.writePtr(static_cast<void*>(fBitmapHeap));
scroggo@google.com10dccde2012-08-08 20:43:22 +0000479 }
480 }
scroggo@google.comd9d29672012-08-14 17:21:34 +0000481 fFlattenableHeap.setBitmapStorage(fBitmapHeap);
piotaixr9bb75392014-10-22 11:02:32 -0700482
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000483 this->doNotify();
reed@google.combb6992a2011-04-26 17:41:56 +0000484}
485
486SkGPipeCanvas::~SkGPipeCanvas() {
scroggo@google.com59c3ab62013-11-12 14:32:38 +0000487 this->finish(true);
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000488 SkSafeUnref(fFactorySet);
scroggo@google.comd9d29672012-08-14 17:21:34 +0000489 SkSafeUnref(fBitmapHeap);
reed@google.combb6992a2011-04-26 17:41:56 +0000490}
491
reed@google.comacd471f2011-05-03 21:26:46 +0000492bool SkGPipeCanvas::needOpBytes(size_t needed) {
493 if (fDone) {
494 return false;
495 }
496
497 needed += 4; // size of DrawOp atom
commit-bot@chromium.orgd0490172014-05-14 15:12:08 +0000498 needed = SkAlign4(needed);
reed@google.com44699382013-10-31 17:28:30 +0000499 if (fWriter.bytesWritten() + needed > fBlockSize) {
mtklein6d88e6c2014-07-30 09:17:54 -0700500 // Before we wipe out any data that has already been written, read it out.
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000501 this->doNotify();
mtklein6d88e6c2014-07-30 09:17:54 -0700502
503 // If we're going to allocate a new block, allocate enough to make it worthwhile.
504 needed = SkTMax<size_t>(MIN_BLOCK_SIZE, needed);
505
commit-bot@chromium.orgd0490172014-05-14 15:12:08 +0000506 void* block = fController->requestBlock(needed, &fBlockSize);
reed@google.comacd471f2011-05-03 21:26:46 +0000507 if (NULL == block) {
scroggo@google.com59c3ab62013-11-12 14:32:38 +0000508 // Do not notify the readers, which would call this function again.
509 this->finish(false);
reed@google.comacd471f2011-05-03 21:26:46 +0000510 return false;
511 }
scroggo@google.com460a23e2012-08-16 17:56:49 +0000512 SkASSERT(SkIsAlign4(fBlockSize));
reed@google.comacd471f2011-05-03 21:26:46 +0000513 fWriter.reset(block, fBlockSize);
514 fBytesNotified = 0;
515 }
516 return true;
517}
518
reed@google.comf5842f72011-05-04 18:30:04 +0000519uint32_t SkGPipeCanvas::getTypefaceID(SkTypeface* face) {
520 uint32_t id = 0; // 0 means default/null typeface
521 if (face) {
522 id = fTypefaceSet.find(face);
523 if (0 == id) {
524 id = fTypefaceSet.add(face);
525 size_t size = writeTypeface(NULL, face);
526 if (this->needOpBytes(size)) {
reed@google.combb6793b2011-05-05 15:18:15 +0000527 this->writeOp(kDef_Typeface_DrawOp);
reed@google.comf5842f72011-05-04 18:30:04 +0000528 writeTypeface(&fWriter, face);
529 }
530 }
531 }
532 return id;
533}
534
reed@google.combb6992a2011-04-26 17:41:56 +0000535///////////////////////////////////////////////////////////////////////////////
536
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000537#define NOTIFY_SETUP(canvas) \
538 AutoPipeNotify apn(canvas)
539
Florin Malita5f6102d2014-06-30 10:13:28 -0400540void SkGPipeCanvas::willSave() {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000541 NOTIFY_SETUP(this);
reed@google.comacd471f2011-05-03 21:26:46 +0000542 if (this->needOpBytes()) {
Florin Malita5f6102d2014-06-30 10:13:28 -0400543 this->writeOp(kSave_DrawOp);
reed@google.comacd471f2011-05-03 21:26:46 +0000544 }
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000545
Florin Malita5f6102d2014-06-30 10:13:28 -0400546 this->INHERITED::willSave();
reed@google.combb6992a2011-04-26 17:41:56 +0000547}
548
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000549SkCanvas::SaveLayerStrategy SkGPipeCanvas::willSaveLayer(const SkRect* bounds, const SkPaint* paint,
550 SaveFlags saveFlags) {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000551 NOTIFY_SETUP(this);
reed@google.comacd471f2011-05-03 21:26:46 +0000552 size_t size = 0;
reed@google.combb6992a2011-04-26 17:41:56 +0000553 unsigned opFlags = 0;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000554
reed@google.combb6992a2011-04-26 17:41:56 +0000555 if (bounds) {
556 opFlags |= kSaveLayer_HasBounds_DrawOpFlag;
reed@google.comacd471f2011-05-03 21:26:46 +0000557 size += sizeof(SkRect);
reed@google.combb6992a2011-04-26 17:41:56 +0000558 }
559 if (paint) {
560 opFlags |= kSaveLayer_HasPaint_DrawOpFlag;
reed@google.com31891582011-05-12 03:03:56 +0000561 this->writePaint(*paint);
reed@google.combb6992a2011-04-26 17:41:56 +0000562 }
563
reed@google.comacd471f2011-05-03 21:26:46 +0000564 if (this->needOpBytes(size)) {
565 this->writeOp(kSaveLayer_DrawOp, opFlags, saveFlags);
566 if (bounds) {
567 fWriter.writeRect(*bounds);
568 }
reed@google.combb6992a2011-04-26 17:41:56 +0000569 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000570
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000571 if (kNoSaveLayer == fFirstSaveLayerStackLevel){
572 fFirstSaveLayerStackLevel = this->getSaveCount();
573 }
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000574
575 this->INHERITED::willSaveLayer(bounds, paint, saveFlags);
576 // we don't create a layer
577 return kNoLayer_SaveLayerStrategy;
reed@google.combb6992a2011-04-26 17:41:56 +0000578}
579
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000580void SkGPipeCanvas::willRestore() {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000581 NOTIFY_SETUP(this);
reed@google.comacd471f2011-05-03 21:26:46 +0000582 if (this->needOpBytes()) {
583 this->writeOp(kRestore_DrawOp);
584 }
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000585
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000586 if (this->getSaveCount() - 1 == fFirstSaveLayerStackLevel){
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000587 fFirstSaveLayerStackLevel = kNoSaveLayer;
588 }
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000589
590 this->INHERITED::willRestore();
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000591}
592
593bool SkGPipeCanvas::isDrawingToLayer() const {
594 return kNoSaveLayer != fFirstSaveLayerStackLevel;
reed@google.combb6992a2011-04-26 17:41:56 +0000595}
596
commit-bot@chromium.orgd9ea09e2014-03-25 17:32:26 +0000597void SkGPipeCanvas::recordTranslate(const SkMatrix& m) {
598 if (this->needOpBytes(2 * sizeof(SkScalar))) {
599 this->writeOp(kTranslate_DrawOp);
600 fWriter.writeScalar(m.getTranslateX());
601 fWriter.writeScalar(m.getTranslateY());
reed@google.combb6992a2011-04-26 17:41:56 +0000602 }
reed@google.combb6992a2011-04-26 17:41:56 +0000603}
604
commit-bot@chromium.orgd9ea09e2014-03-25 17:32:26 +0000605void SkGPipeCanvas::recordScale(const SkMatrix& m) {
606 if (this->needOpBytes(2 * sizeof(SkScalar))) {
607 this->writeOp(kScale_DrawOp);
608 fWriter.writeScalar(m.getScaleX());
609 fWriter.writeScalar(m.getScaleY());
reed@google.combb6992a2011-04-26 17:41:56 +0000610 }
reed@google.combb6992a2011-04-26 17:41:56 +0000611}
612
commit-bot@chromium.orgd9ea09e2014-03-25 17:32:26 +0000613void SkGPipeCanvas::recordConcat(const SkMatrix& m) {
614 if (this->needOpBytes(m.writeToMemory(NULL))) {
615 this->writeOp(kConcat_DrawOp);
616 fWriter.writeMatrix(m);
reed@google.combb6992a2011-04-26 17:41:56 +0000617 }
reed@google.combb6992a2011-04-26 17:41:56 +0000618}
619
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +0000620void SkGPipeCanvas::didConcat(const SkMatrix& matrix) {
reed@google.combb6992a2011-04-26 17:41:56 +0000621 if (!matrix.isIdentity()) {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000622 NOTIFY_SETUP(this);
commit-bot@chromium.orgd9ea09e2014-03-25 17:32:26 +0000623 switch (matrix.getType()) {
624 case SkMatrix::kTranslate_Mask:
625 this->recordTranslate(matrix);
626 break;
627 case SkMatrix::kScale_Mask:
628 this->recordScale(matrix);
629 break;
630 default:
631 this->recordConcat(matrix);
632 break;
reed@google.comacd471f2011-05-03 21:26:46 +0000633 }
reed@google.combb6992a2011-04-26 17:41:56 +0000634 }
commit-bot@chromium.orgd9ea09e2014-03-25 17:32:26 +0000635
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +0000636 this->INHERITED::didConcat(matrix);
reed@google.combb6992a2011-04-26 17:41:56 +0000637}
638
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +0000639void SkGPipeCanvas::didSetMatrix(const SkMatrix& matrix) {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000640 NOTIFY_SETUP(this);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000641 if (this->needOpBytes(matrix.writeToMemory(NULL))) {
reed@google.comacd471f2011-05-03 21:26:46 +0000642 this->writeOp(kSetMatrix_DrawOp);
djsollen@google.com2b2ede32012-04-12 13:24:04 +0000643 fWriter.writeMatrix(matrix);
reed@google.comacd471f2011-05-03 21:26:46 +0000644 }
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +0000645 this->INHERITED::didSetMatrix(matrix);
reed@google.combb6992a2011-04-26 17:41:56 +0000646}
647
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000648void SkGPipeCanvas::onClipRect(const SkRect& rect, SkRegion::Op rgnOp,
649 ClipEdgeStyle edgeStyle) {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000650 NOTIFY_SETUP(this);
scroggo@google.com460a23e2012-08-16 17:56:49 +0000651 if (this->needOpBytes(sizeof(SkRect))) {
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000652 unsigned flags = 0;
653 if (kSoft_ClipEdgeStyle == edgeStyle) {
654 flags = kClip_HasAntiAlias_DrawOpFlag;
655 }
scroggo@google.com460a23e2012-08-16 17:56:49 +0000656 this->writeOp(kClipRect_DrawOp, flags, rgnOp);
reed@google.comacd471f2011-05-03 21:26:46 +0000657 fWriter.writeRect(rect);
658 }
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000659 this->INHERITED::onClipRect(rect, rgnOp, edgeStyle);
reed@google.combb6992a2011-04-26 17:41:56 +0000660}
661
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000662void SkGPipeCanvas::onClipRRect(const SkRRect& rrect, SkRegion::Op rgnOp,
663 ClipEdgeStyle edgeStyle) {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000664 NOTIFY_SETUP(this);
reed@google.com4ed0fb72012-12-12 20:48:18 +0000665 if (this->needOpBytes(kSizeOfFlatRRect)) {
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000666 unsigned flags = 0;
667 if (kSoft_ClipEdgeStyle == edgeStyle) {
668 flags = kClip_HasAntiAlias_DrawOpFlag;
669 }
reed@google.com4ed0fb72012-12-12 20:48:18 +0000670 this->writeOp(kClipRRect_DrawOp, flags, rgnOp);
671 fWriter.writeRRect(rrect);
672 }
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000673 this->INHERITED::onClipRRect(rrect, rgnOp, edgeStyle);
reed@google.com4ed0fb72012-12-12 20:48:18 +0000674}
675
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000676void SkGPipeCanvas::onClipPath(const SkPath& path, SkRegion::Op rgnOp,
677 ClipEdgeStyle edgeStyle) {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000678 NOTIFY_SETUP(this);
scroggo@google.com460a23e2012-08-16 17:56:49 +0000679 if (this->needOpBytes(path.writeToMemory(NULL))) {
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000680 unsigned flags = 0;
681 if (kSoft_ClipEdgeStyle == edgeStyle) {
682 flags = kClip_HasAntiAlias_DrawOpFlag;
683 }
scroggo@google.com460a23e2012-08-16 17:56:49 +0000684 this->writeOp(kClipPath_DrawOp, flags, rgnOp);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000685 fWriter.writePath(path);
reed@google.comacd471f2011-05-03 21:26:46 +0000686 }
reed@google.combb6992a2011-04-26 17:41:56 +0000687 // we just pass on the bounds of the path
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000688 this->INHERITED::onClipRect(path.getBounds(), rgnOp, edgeStyle);
reed@google.combb6992a2011-04-26 17:41:56 +0000689}
690
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000691void SkGPipeCanvas::onClipRegion(const SkRegion& region, SkRegion::Op rgnOp) {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000692 NOTIFY_SETUP(this);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000693 if (this->needOpBytes(region.writeToMemory(NULL))) {
reed@google.comacd471f2011-05-03 21:26:46 +0000694 this->writeOp(kClipRegion_DrawOp, 0, rgnOp);
djsollen@google.com2b2ede32012-04-12 13:24:04 +0000695 fWriter.writeRegion(region);
reed@google.comacd471f2011-05-03 21:26:46 +0000696 }
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000697 this->INHERITED::onClipRegion(region, rgnOp);
reed@google.combb6992a2011-04-26 17:41:56 +0000698}
699
700///////////////////////////////////////////////////////////////////////////////
701
702void SkGPipeCanvas::clear(SkColor color) {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000703 NOTIFY_SETUP(this);
reed@google.combb6992a2011-04-26 17:41:56 +0000704 unsigned flags = 0;
705 if (color) {
706 flags |= kClear_HasColor_DrawOpFlag;
707 }
reed@google.comacd471f2011-05-03 21:26:46 +0000708 if (this->needOpBytes(sizeof(SkColor))) {
709 this->writeOp(kDrawClear_DrawOp, flags, 0);
710 if (color) {
711 fWriter.write32(color);
712 }
reed@google.combb6992a2011-04-26 17:41:56 +0000713 }
714}
715
716void SkGPipeCanvas::drawPaint(const SkPaint& paint) {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000717 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000718 this->writePaint(paint);
reed@google.comacd471f2011-05-03 21:26:46 +0000719 if (this->needOpBytes()) {
reed@google.com31891582011-05-12 03:03:56 +0000720 this->writeOp(kDrawPaint_DrawOp);
reed@google.comacd471f2011-05-03 21:26:46 +0000721 }
reed@google.combb6992a2011-04-26 17:41:56 +0000722}
723
724void SkGPipeCanvas::drawPoints(PointMode mode, size_t count,
robertphillips@google.com8b169312013-10-15 17:47:36 +0000725 const SkPoint pts[], const SkPaint& paint) {
reed@google.combb6992a2011-04-26 17:41:56 +0000726 if (count) {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +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(4 + count * sizeof(SkPoint))) {
reed@google.com31891582011-05-12 03:03:56 +0000730 this->writeOp(kDrawPoints_DrawOp, mode, 0);
commit-bot@chromium.orgb45bd1f2014-04-24 18:17:30 +0000731 fWriter.write32(SkToU32(count));
reed@google.comacd471f2011-05-03 21:26:46 +0000732 fWriter.write(pts, count * sizeof(SkPoint));
733 }
reed@google.combb6992a2011-04-26 17:41:56 +0000734 }
735}
736
reed@google.com4ed0fb72012-12-12 20:48:18 +0000737void SkGPipeCanvas::drawOval(const SkRect& rect, const SkPaint& paint) {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000738 NOTIFY_SETUP(this);
reed@google.com4ed0fb72012-12-12 20:48:18 +0000739 this->writePaint(paint);
740 if (this->needOpBytes(sizeof(SkRect))) {
741 this->writeOp(kDrawOval_DrawOp);
742 fWriter.writeRect(rect);
743 }
744}
745
bsalomon@google.com7ce564c2013-10-22 16:54:15 +0000746void SkGPipeCanvas::drawRect(const SkRect& rect, const SkPaint& paint) {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000747 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000748 this->writePaint(paint);
reed@google.comacd471f2011-05-03 21:26:46 +0000749 if (this->needOpBytes(sizeof(SkRect))) {
reed@google.com31891582011-05-12 03:03:56 +0000750 this->writeOp(kDrawRect_DrawOp);
reed@google.comacd471f2011-05-03 21:26:46 +0000751 fWriter.writeRect(rect);
752 }
reed@google.combb6992a2011-04-26 17:41:56 +0000753}
754
reed@google.com4ed0fb72012-12-12 20:48:18 +0000755void SkGPipeCanvas::drawRRect(const SkRRect& rrect, const SkPaint& paint) {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000756 NOTIFY_SETUP(this);
reed@google.com4ed0fb72012-12-12 20:48:18 +0000757 this->writePaint(paint);
758 if (this->needOpBytes(kSizeOfFlatRRect)) {
759 this->writeOp(kDrawRRect_DrawOp);
760 fWriter.writeRRect(rrect);
761 }
762}
763
commit-bot@chromium.orged9806f2014-02-21 02:32:36 +0000764void SkGPipeCanvas::onDrawDRRect(const SkRRect& outer, const SkRRect& inner,
765 const SkPaint& paint) {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000766 NOTIFY_SETUP(this);
commit-bot@chromium.orged9806f2014-02-21 02:32:36 +0000767 this->writePaint(paint);
768 if (this->needOpBytes(kSizeOfFlatRRect * 2)) {
769 this->writeOp(kDrawDRRect_DrawOp);
770 fWriter.writeRRect(outer);
771 fWriter.writeRRect(inner);
772 }
773}
774
bsalomon@google.com7ce564c2013-10-22 16:54:15 +0000775void SkGPipeCanvas::drawPath(const SkPath& path, const SkPaint& paint) {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000776 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000777 this->writePaint(paint);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000778 if (this->needOpBytes(path.writeToMemory(NULL))) {
reed@google.com31891582011-05-12 03:03:56 +0000779 this->writeOp(kDrawPath_DrawOp);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000780 fWriter.writePath(path);
reed@google.comacd471f2011-05-03 21:26:46 +0000781 }
reed@google.combb6992a2011-04-26 17:41:56 +0000782}
783
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000784bool SkGPipeCanvas::commonDrawBitmap(const SkBitmap& bm, DrawOps op,
785 unsigned flags,
786 size_t opBytesNeeded,
787 const SkPaint* paint) {
commit-bot@chromium.org44803fb2014-05-14 15:38:25 +0000788 if (fDone) {
789 return false;
790 }
791
scroggo@google.com58be6822012-07-30 14:40:01 +0000792 if (paint != NULL) {
scroggo@google.com460a23e2012-08-16 17:56:49 +0000793 flags |= kDrawBitmap_HasPaint_DrawOpFlag;
scroggo@google.com58be6822012-07-30 14:40:01 +0000794 this->writePaint(*paint);
795 }
commit-bot@chromium.org44803fb2014-05-14 15:38:25 +0000796 // This needs to run first so its calls to needOpBytes() and its writes
797 // don't interlace with the needOpBytes() and write below.
798 SkASSERT(fBitmapHeap != NULL);
799 int32_t bitmapIndex = fBitmapHeap->insert(bm);
800 if (SkBitmapHeap::INVALID_SLOT == bitmapIndex) {
801 return false;
802 }
803
scroggo@google.com10dccde2012-08-08 20:43:22 +0000804 if (this->needOpBytes(opBytesNeeded)) {
805 this->writeOp(op, flags, bitmapIndex);
scroggo@google.com58be6822012-07-30 14:40:01 +0000806 return true;
807 }
808 return false;
809}
810
piotaixr9bb75392014-10-22 11:02:32 -0700811bool SkGPipeCanvas::commonDrawImage(const SkImage* image, DrawOps op,
812 unsigned flags,
813 size_t opBytesNeeded,
814 const SkPaint* paint) {
815 if (fDone) {
816 return false;
817 }
818
819 if (paint != NULL) {
820 flags |= kDrawBitmap_HasPaint_DrawOpFlag;
821 this->writePaint(*paint);
822 }
823
824 opBytesNeeded += sizeof (SkImage*);
825
826 if (this->needOpBytes(opBytesNeeded)) {
827 this->writeOp(op, flags, 0);
828
829 image->ref(); // The SkGPipeReader will have to call unref()
830 fWriter.writePtr(static_cast<void*>(const_cast<SkImage*>(image)));
831
832 return true;
833 }
834 return false;
835}
836
837void SkGPipeCanvas::drawImage(const SkImage* image, SkScalar left, SkScalar top,
838 const SkPaint* paint) {
839 if (is_cross_process(fFlags)){
840 // If the SkGPipe is cross-process, we will have to flatten the data in the SkImage, so
841 // fallback to the default implementation in SkCanvas (which calls SkImage::draw())
842 // https://code.google.com//p/skia/issues/detail?id=2985
843 this->INHERITED::drawImage(image, left, top, paint);
844 } else {
845 NOTIFY_SETUP(this);
846 size_t opBytesNeeded = sizeof(SkScalar) * 2;
847
848 if (this->commonDrawImage(image, kDrawImage_DrawOp, 0, opBytesNeeded, paint)) {
849 fWriter.writeScalar(left);
850 fWriter.writeScalar(top);
851 }
852 }
853}
854
855void SkGPipeCanvas::drawImageRect(const SkImage* image, const SkRect* src,
856 const SkRect& dst,
857 const SkPaint* paint) {
858 if (is_cross_process(fFlags)){
859 // If the SkGPipe is cross-process, we will have to flatten the data in the SkImage, so
860 // fallback to the default implementation in SkCanvas (which calls SkImage::drawRect())
861 // https://code.google.com//p/skia/issues/detail?id=2985
862 this->INHERITED::drawImageRect(image, src, dst, paint);
863 } else {
864 NOTIFY_SETUP(this);
865 size_t opBytesNeeded = sizeof (SkRect);
866 bool hasSrc = src != NULL;
867 unsigned flags;
868 if (hasSrc) {
869 flags = kDrawImage_HasSrcRect_DrawOpFlag;
870 opBytesNeeded += sizeof (SkRect);
871 } else {
872 flags = 0;
873 }
874
875 if (this->commonDrawImage(image, kDrawImageRect_DrawOp, flags, opBytesNeeded, paint)) {
876 if (hasSrc) {
877 fWriter.writeRect(*src);
878 }
879 fWriter.writeRect(dst);
880 }
881 }
882}
883
scroggo@google.com58be6822012-07-30 14:40:01 +0000884void SkGPipeCanvas::drawBitmap(const SkBitmap& bm, SkScalar left, SkScalar top,
885 const SkPaint* paint) {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000886 NOTIFY_SETUP(this);
scroggo@google.com58be6822012-07-30 14:40:01 +0000887 size_t opBytesNeeded = sizeof(SkScalar) * 2;
888
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000889 if (this->commonDrawBitmap(bm, kDrawBitmap_DrawOp, 0, opBytesNeeded, paint)) {
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000890 fWriter.writeScalar(left);
891 fWriter.writeScalar(top);
892 }
reed@google.combb6992a2011-04-26 17:41:56 +0000893}
894
reed@google.com71121732012-09-18 15:14:33 +0000895void SkGPipeCanvas::drawBitmapRectToRect(const SkBitmap& bm, const SkRect* src,
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +0000896 const SkRect& dst, const SkPaint* paint,
897 DrawBitmapRectFlags dbmrFlags) {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000898 NOTIFY_SETUP(this);
scroggo@google.com58be6822012-07-30 14:40:01 +0000899 size_t opBytesNeeded = sizeof(SkRect);
900 bool hasSrc = src != NULL;
901 unsigned flags;
902 if (hasSrc) {
scroggo@google.com460a23e2012-08-16 17:56:49 +0000903 flags = kDrawBitmap_HasSrcRect_DrawOpFlag;
scroggo@google.com58be6822012-07-30 14:40:01 +0000904 opBytesNeeded += sizeof(int32_t) * 4;
905 } else {
906 flags = 0;
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000907 }
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +0000908 if (dbmrFlags & kBleed_DrawBitmapRectFlag) {
909 flags |= kDrawBitmap_Bleed_DrawOpFlag;
910 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000911
reed@google.com71121732012-09-18 15:14:33 +0000912 if (this->commonDrawBitmap(bm, kDrawBitmapRectToRect_DrawOp, flags, opBytesNeeded, paint)) {
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000913 if (hasSrc) {
reed@google.com71121732012-09-18 15:14:33 +0000914 fWriter.writeRect(*src);
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000915 }
916 fWriter.writeRect(dst);
917 }
reed@google.combb6992a2011-04-26 17:41:56 +0000918}
919
scroggo@google.com58be6822012-07-30 14:40:01 +0000920void SkGPipeCanvas::drawBitmapMatrix(const SkBitmap& bm, const SkMatrix& matrix,
921 const SkPaint* paint) {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000922 NOTIFY_SETUP(this);
scroggo@google.com58be6822012-07-30 14:40:01 +0000923 size_t opBytesNeeded = matrix.writeToMemory(NULL);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000924
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000925 if (this->commonDrawBitmap(bm, kDrawBitmapMatrix_DrawOp, 0, opBytesNeeded, paint)) {
scroggo@google.com58be6822012-07-30 14:40:01 +0000926 fWriter.writeMatrix(matrix);
927 }
reed@google.combb6992a2011-04-26 17:41:56 +0000928}
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000929
930void SkGPipeCanvas::drawBitmapNine(const SkBitmap& bm, const SkIRect& center,
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000931 const SkRect& dst, const SkPaint* paint) {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000932 NOTIFY_SETUP(this);
scroggo@google.com58be6822012-07-30 14:40:01 +0000933 size_t opBytesNeeded = sizeof(int32_t) * 4 + sizeof(SkRect);
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000934
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000935 if (this->commonDrawBitmap(bm, kDrawBitmapNine_DrawOp, 0, opBytesNeeded, paint)) {
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000936 fWriter.write32(center.fLeft);
937 fWriter.write32(center.fTop);
938 fWriter.write32(center.fRight);
939 fWriter.write32(center.fBottom);
940 fWriter.writeRect(dst);
941 }
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000942}
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000943
944void SkGPipeCanvas::drawSprite(const SkBitmap& bm, int left, int top,
945 const SkPaint* paint) {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000946 NOTIFY_SETUP(this);
scroggo@google.com58be6822012-07-30 14:40:01 +0000947 size_t opBytesNeeded = sizeof(int32_t) * 2;
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000948
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000949 if (this->commonDrawBitmap(bm, kDrawSprite_DrawOp, 0, opBytesNeeded, paint)) {
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000950 fWriter.write32(left);
951 fWriter.write32(top);
952 }
reed@google.combb6992a2011-04-26 17:41:56 +0000953}
954
reed@google.come0d9ce82014-04-23 04:00:17 +0000955void SkGPipeCanvas::onDrawText(const void* text, size_t byteLength, SkScalar x, SkScalar y,
956 const SkPaint& paint) {
reed@google.combb6992a2011-04-26 17:41:56 +0000957 if (byteLength) {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000958 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000959 this->writePaint(paint);
reed@google.comacd471f2011-05-03 21:26:46 +0000960 if (this->needOpBytes(4 + SkAlign4(byteLength) + 2 * sizeof(SkScalar))) {
reed@google.com31891582011-05-12 03:03:56 +0000961 this->writeOp(kDrawText_DrawOp);
commit-bot@chromium.orgb45bd1f2014-04-24 18:17:30 +0000962 fWriter.write32(SkToU32(byteLength));
reed@google.comacd471f2011-05-03 21:26:46 +0000963 fWriter.writePad(text, byteLength);
964 fWriter.writeScalar(x);
965 fWriter.writeScalar(y);
966 }
reed@google.combb6992a2011-04-26 17:41:56 +0000967 }
968}
969
reed@google.come0d9ce82014-04-23 04:00:17 +0000970void SkGPipeCanvas::onDrawPosText(const void* text, size_t byteLength, const SkPoint pos[],
971 const SkPaint& paint) {
reed@google.combb6992a2011-04-26 17:41:56 +0000972 if (byteLength) {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000973 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000974 this->writePaint(paint);
reed@google.combb6992a2011-04-26 17:41:56 +0000975 int count = paint.textToGlyphs(text, byteLength, NULL);
reed@google.comacd471f2011-05-03 21:26:46 +0000976 if (this->needOpBytes(4 + SkAlign4(byteLength) + 4 + count * sizeof(SkPoint))) {
reed@google.com31891582011-05-12 03:03:56 +0000977 this->writeOp(kDrawPosText_DrawOp);
commit-bot@chromium.orgb45bd1f2014-04-24 18:17:30 +0000978 fWriter.write32(SkToU32(byteLength));
reed@google.comacd471f2011-05-03 21:26:46 +0000979 fWriter.writePad(text, byteLength);
980 fWriter.write32(count);
981 fWriter.write(pos, count * sizeof(SkPoint));
982 }
reed@google.combb6992a2011-04-26 17:41:56 +0000983 }
984}
985
reed@google.come0d9ce82014-04-23 04:00:17 +0000986void SkGPipeCanvas::onDrawPosTextH(const void* text, size_t byteLength, const SkScalar xpos[],
987 SkScalar constY, const SkPaint& paint) {
reed@google.combb6992a2011-04-26 17:41:56 +0000988 if (byteLength) {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000989 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000990 this->writePaint(paint);
reed@google.combb6992a2011-04-26 17:41:56 +0000991 int count = paint.textToGlyphs(text, byteLength, NULL);
reed@google.comacd471f2011-05-03 21:26:46 +0000992 if (this->needOpBytes(4 + SkAlign4(byteLength) + 4 + count * sizeof(SkScalar) + 4)) {
reed@google.com31891582011-05-12 03:03:56 +0000993 this->writeOp(kDrawPosTextH_DrawOp);
commit-bot@chromium.orgb45bd1f2014-04-24 18:17:30 +0000994 fWriter.write32(SkToU32(byteLength));
reed@google.comacd471f2011-05-03 21:26:46 +0000995 fWriter.writePad(text, byteLength);
996 fWriter.write32(count);
997 fWriter.write(xpos, count * sizeof(SkScalar));
998 fWriter.writeScalar(constY);
999 }
reed@google.combb6992a2011-04-26 17:41:56 +00001000 }
1001}
1002
reed@google.come0d9ce82014-04-23 04:00:17 +00001003void SkGPipeCanvas::onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& path,
1004 const SkMatrix* matrix, const SkPaint& paint) {
reed@google.combb6992a2011-04-26 17:41:56 +00001005 if (byteLength) {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +00001006 NOTIFY_SETUP(this);
reed@google.combb6992a2011-04-26 17:41:56 +00001007 unsigned flags = 0;
djsollen@google.com94e75ee2012-06-08 18:30:46 +00001008 size_t size = 4 + SkAlign4(byteLength) + path.writeToMemory(NULL);
reed@google.combb6992a2011-04-26 17:41:56 +00001009 if (matrix) {
1010 flags |= kDrawTextOnPath_HasMatrix_DrawOpFlag;
djsollen@google.com94e75ee2012-06-08 18:30:46 +00001011 size += matrix->writeToMemory(NULL);
reed@google.combb6992a2011-04-26 17:41:56 +00001012 }
reed@google.com31891582011-05-12 03:03:56 +00001013 this->writePaint(paint);
reed@google.comacd471f2011-05-03 21:26:46 +00001014 if (this->needOpBytes(size)) {
reed@google.com31891582011-05-12 03:03:56 +00001015 this->writeOp(kDrawTextOnPath_DrawOp, flags, 0);
reed@google.combb6992a2011-04-26 17:41:56 +00001016
commit-bot@chromium.orgb45bd1f2014-04-24 18:17:30 +00001017 fWriter.write32(SkToU32(byteLength));
reed@google.comacd471f2011-05-03 21:26:46 +00001018 fWriter.writePad(text, byteLength);
reed@google.combb6992a2011-04-26 17:41:56 +00001019
djsollen@google.com94e75ee2012-06-08 18:30:46 +00001020 fWriter.writePath(path);
reed@google.comacd471f2011-05-03 21:26:46 +00001021 if (matrix) {
djsollen@google.com2b2ede32012-04-12 13:24:04 +00001022 fWriter.writeMatrix(*matrix);
reed@google.comacd471f2011-05-03 21:26:46 +00001023 }
reed@google.combb6992a2011-04-26 17:41:56 +00001024 }
1025 }
1026}
1027
fmalitaacb882c2014-09-16 17:58:34 -07001028size_t SkGPipeCanvas::getInProcessTypefaces(const SkRefCntSet& typefaceSet,
1029 TypefaceBuffer* buffer) {
1030 // When in-process, we simply write out the typeface pointers.
1031 size_t size = typefaceSet.count() * sizeof(SkTypeface*);
1032 buffer->reset(size);
1033 typefaceSet.copyToArray(reinterpret_cast<SkRefCnt**>(buffer->get()));
1034
1035 return size;
1036}
1037
1038size_t SkGPipeCanvas::getCrossProcessTypefaces(const SkRefCntSet& typefaceSet,
1039 TypefaceBuffer* buffer) {
1040 // For cross-process we use typeface IDs.
1041 size_t size = typefaceSet.count() * sizeof(uint32_t);
1042 buffer->reset(size);
1043
1044 uint32_t* idBuffer = reinterpret_cast<uint32_t*>(buffer->get());
1045 SkRefCntSet::Iter iter(typefaceSet);
1046 int i = 0;
1047
1048 for (void* setPtr = iter.next(); setPtr; setPtr = iter.next()) {
1049 idBuffer[i++] = this->getTypefaceID(reinterpret_cast<SkTypeface*>(setPtr));
1050 }
1051
1052 SkASSERT(i == typefaceSet.count());
1053
1054 return size;
1055}
1056
fmalitab7425172014-08-26 07:56:44 -07001057void SkGPipeCanvas::onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
1058 const SkPaint& paint) {
fmalita228a6f22014-08-28 13:59:42 -07001059 NOTIFY_SETUP(this);
1060 this->writePaint(paint);
1061
1062 // FIXME: this is inefficient but avoids duplicating the blob serialization logic.
fmalitaacb882c2014-09-16 17:58:34 -07001063 SkRefCntSet typefaceSet;
fmalita228a6f22014-08-28 13:59:42 -07001064 SkWriteBuffer blobBuffer;
fmalitaacb882c2014-09-16 17:58:34 -07001065 blobBuffer.setTypefaceRecorder(&typefaceSet);
fmalita228a6f22014-08-28 13:59:42 -07001066 blob->flatten(blobBuffer);
1067
fmalitaacb882c2014-09-16 17:58:34 -07001068 // Unlike most draw ops (which only use one paint/typeface), text blobs may reference
1069 // an arbitrary number of typefaces. Since the one-paint-per-op model is not applicable,
1070 // we need to serialize these explicitly.
1071 TypefaceBuffer typefaceBuffer;
1072 size_t typefaceSize = is_cross_process(fFlags)
1073 ? this->getCrossProcessTypefaces(typefaceSet, &typefaceBuffer)
1074 : this->getInProcessTypefaces(typefaceSet, &typefaceBuffer);
1075
1076 // blob byte count + typeface count + x + y + blob data + an index (cross-process)
1077 // or pointer (in-process) for each typeface
1078 size_t size = 2 * sizeof(uint32_t)
1079 + 2 * sizeof(SkScalar)
1080 + blobBuffer.bytesWritten()
1081 + typefaceSize;
1082
fmalita228a6f22014-08-28 13:59:42 -07001083 if (this->needOpBytes(size)) {
1084 this->writeOp(kDrawTextBlob_DrawOp);
fmalitaacb882c2014-09-16 17:58:34 -07001085 SkDEBUGCODE(size_t initialOffset = fWriter.bytesWritten();)
1086
fmalita228a6f22014-08-28 13:59:42 -07001087 fWriter.writeScalar(x);
1088 fWriter.writeScalar(y);
fmalitaacb882c2014-09-16 17:58:34 -07001089
1090 fWriter.write32(typefaceSet.count());
1091 fWriter.write(typefaceBuffer.get(), typefaceSize);
1092
fmalita228a6f22014-08-28 13:59:42 -07001093 fWriter.write32(SkToU32(blobBuffer.bytesWritten()));
1094 uint32_t* pad = fWriter.reservePad(blobBuffer.bytesWritten());
1095 blobBuffer.writeToMemory(pad);
fmalitaacb882c2014-09-16 17:58:34 -07001096
1097 SkASSERT(initialOffset + size == fWriter.bytesWritten());
fmalita228a6f22014-08-28 13:59:42 -07001098 }
fmalitab7425172014-08-26 07:56:44 -07001099}
1100
reedd5fa1a42014-08-09 11:08:05 -07001101void SkGPipeCanvas::onDrawPicture(const SkPicture* picture, const SkMatrix* matrix,
1102 const SkPaint* paint) {
reed@google.com0faac1e2011-05-11 05:58:58 +00001103 // we want to playback the picture into individual draw calls
reedd5fa1a42014-08-09 11:08:05 -07001104 //
1105 // todo: do we always have to unroll? If the pipe is not cross-process, seems like
1106 // we could just ref the picture and move on...? <reed, scroggo>
1107 //
1108 this->INHERITED::onDrawPicture(picture, matrix, paint);
reed@google.combb6992a2011-04-26 17:41:56 +00001109}
1110
reed@google.com85e143c2013-12-30 15:51:25 +00001111void SkGPipeCanvas::drawVertices(VertexMode vmode, int vertexCount,
reed@google.combb6992a2011-04-26 17:41:56 +00001112 const SkPoint vertices[], const SkPoint texs[],
reed@google.com85e143c2013-12-30 15:51:25 +00001113 const SkColor colors[], SkXfermode* xfer,
reed@google.combb6992a2011-04-26 17:41:56 +00001114 const uint16_t indices[], int indexCount,
1115 const SkPaint& paint) {
1116 if (0 == vertexCount) {
1117 return;
1118 }
1119
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +00001120 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +00001121 this->writePaint(paint);
commit-bot@chromium.orgd0490172014-05-14 15:12:08 +00001122
1123 unsigned flags = 0; // packs with the op, so needs no extra space
1124
1125 size_t size = 0;
1126 size += 4; // vmode
1127 size += 4; // vertex count
1128 size += vertexCount * sizeof(SkPoint); // vertices
1129
reed@google.combb6992a2011-04-26 17:41:56 +00001130 if (texs) {
1131 flags |= kDrawVertices_HasTexs_DrawOpFlag;
reed@google.comacd471f2011-05-03 21:26:46 +00001132 size += vertexCount * sizeof(SkPoint);
reed@google.combb6992a2011-04-26 17:41:56 +00001133 }
1134 if (colors) {
1135 flags |= kDrawVertices_HasColors_DrawOpFlag;
reed@google.comacd471f2011-05-03 21:26:46 +00001136 size += vertexCount * sizeof(SkColor);
reed@google.combb6992a2011-04-26 17:41:56 +00001137 }
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +00001138 if (xfer && !SkXfermode::IsMode(xfer, SkXfermode::kModulate_Mode)) {
1139 flags |= kDrawVertices_HasXfermode_DrawOpFlag;
commit-bot@chromium.orgd0490172014-05-14 15:12:08 +00001140 size += sizeof(int32_t); // SkXfermode::Mode
1141 }
1142 if (indices && indexCount > 0) {
1143 flags |= kDrawVertices_HasIndices_DrawOpFlag;
1144 size += 4; // index count
1145 size += SkAlign4(indexCount * sizeof(uint16_t)); // indices
reed@google.com85e143c2013-12-30 15:51:25 +00001146 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001147
reed@google.comacd471f2011-05-03 21:26:46 +00001148 if (this->needOpBytes(size)) {
reed@google.com31891582011-05-12 03:03:56 +00001149 this->writeOp(kDrawVertices_DrawOp, flags, 0);
reed@google.com85e143c2013-12-30 15:51:25 +00001150 fWriter.write32(vmode);
reed@google.comacd471f2011-05-03 21:26:46 +00001151 fWriter.write32(vertexCount);
1152 fWriter.write(vertices, vertexCount * sizeof(SkPoint));
commit-bot@chromium.orgd0490172014-05-14 15:12:08 +00001153 if (flags & kDrawVertices_HasTexs_DrawOpFlag) {
reed@google.comacd471f2011-05-03 21:26:46 +00001154 fWriter.write(texs, vertexCount * sizeof(SkPoint));
1155 }
commit-bot@chromium.orgd0490172014-05-14 15:12:08 +00001156 if (flags & kDrawVertices_HasColors_DrawOpFlag) {
reed@google.comacd471f2011-05-03 21:26:46 +00001157 fWriter.write(colors, vertexCount * sizeof(SkColor));
1158 }
reed@google.com85e143c2013-12-30 15:51:25 +00001159 if (flags & kDrawVertices_HasXfermode_DrawOpFlag) {
1160 SkXfermode::Mode mode = SkXfermode::kModulate_Mode;
commit-bot@chromium.orgd0490172014-05-14 15:12:08 +00001161 SkAssertResult(xfer->asMode(&mode));
reed@google.com85e143c2013-12-30 15:51:25 +00001162 fWriter.write32(mode);
1163 }
commit-bot@chromium.orgd0490172014-05-14 15:12:08 +00001164 if (flags & kDrawVertices_HasIndices_DrawOpFlag) {
reed@google.comacd471f2011-05-03 21:26:46 +00001165 fWriter.write32(indexCount);
1166 fWriter.writePad(indices, indexCount * sizeof(uint16_t));
1167 }
reed@google.combb6992a2011-04-26 17:41:56 +00001168 }
1169}
1170
dandovb3c9d1c2014-08-12 08:34:29 -07001171void SkGPipeCanvas::onDrawPatch(const SkPoint cubics[12], const SkColor colors[4],
1172 const SkPoint texCoords[4], SkXfermode* xmode,
1173 const SkPaint& paint) {
dandov963137b2014-08-07 07:49:53 -07001174 NOTIFY_SETUP(this);
piotaixr9bb75392014-10-22 11:02:32 -07001175
dandovb3c9d1c2014-08-12 08:34:29 -07001176 size_t size = SkPatchUtils::kNumCtrlPts * sizeof(SkPoint);
1177 unsigned flags = 0;
bsalomon49f085d2014-09-05 13:34:00 -07001178 if (colors) {
dandovb3c9d1c2014-08-12 08:34:29 -07001179 flags |= kDrawVertices_HasColors_DrawOpFlag;
1180 size += SkPatchUtils::kNumCorners * sizeof(SkColor);
1181 }
bsalomon49f085d2014-09-05 13:34:00 -07001182 if (texCoords) {
dandovb3c9d1c2014-08-12 08:34:29 -07001183 flags |= kDrawVertices_HasTexs_DrawOpFlag;
1184 size += SkPatchUtils::kNumCorners * sizeof(SkPoint);
1185 }
bsalomon49f085d2014-09-05 13:34:00 -07001186 if (xmode) {
dandovb3c9d1c2014-08-12 08:34:29 -07001187 SkXfermode::Mode mode;
1188 if (xmode->asMode(&mode) && SkXfermode::kModulate_Mode != mode) {
1189 flags |= kDrawVertices_HasXfermode_DrawOpFlag;
1190 size += sizeof(int32_t);
1191 }
1192 }
piotaixr9bb75392014-10-22 11:02:32 -07001193
dandov963137b2014-08-07 07:49:53 -07001194 this->writePaint(paint);
dandovb3c9d1c2014-08-12 08:34:29 -07001195 if (this->needOpBytes(size)) {
1196 this->writeOp(kDrawPatch_DrawOp, flags, 0);
piotaixr9bb75392014-10-22 11:02:32 -07001197
dandovb3c9d1c2014-08-12 08:34:29 -07001198 fWriter.write(cubics, SkPatchUtils::kNumCtrlPts * sizeof(SkPoint));
piotaixr9bb75392014-10-22 11:02:32 -07001199
bsalomon49f085d2014-09-05 13:34:00 -07001200 if (colors) {
dandovb3c9d1c2014-08-12 08:34:29 -07001201 fWriter.write(colors, SkPatchUtils::kNumCorners * sizeof(SkColor));
1202 }
piotaixr9bb75392014-10-22 11:02:32 -07001203
bsalomon49f085d2014-09-05 13:34:00 -07001204 if (texCoords) {
dandovb3c9d1c2014-08-12 08:34:29 -07001205 fWriter.write(texCoords, SkPatchUtils::kNumCorners * sizeof(SkPoint));
1206 }
piotaixr9bb75392014-10-22 11:02:32 -07001207
dandovb3c9d1c2014-08-12 08:34:29 -07001208 if (flags & kDrawVertices_HasXfermode_DrawOpFlag) {
1209 SkXfermode::Mode mode = SkXfermode::kModulate_Mode;
1210 SkAssertResult(xmode->asMode(&mode));
1211 fWriter.write32(mode);
1212 }
dandov963137b2014-08-07 07:49:53 -07001213 }
1214}
1215
reed@google.comacd471f2011-05-03 21:26:46 +00001216void SkGPipeCanvas::drawData(const void* ptr, size_t size) {
1217 if (size && ptr) {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +00001218 NOTIFY_SETUP(this);
reed@google.combb6992a2011-04-26 17:41:56 +00001219 unsigned data = 0;
1220 if (size < (1 << DRAWOPS_DATA_BITS)) {
1221 data = (unsigned)size;
1222 }
reed@google.comacd471f2011-05-03 21:26:46 +00001223 if (this->needOpBytes(4 + SkAlign4(size))) {
1224 this->writeOp(kDrawData_DrawOp, 0, data);
1225 if (0 == data) {
commit-bot@chromium.orgb45bd1f2014-04-24 18:17:30 +00001226 fWriter.write32(SkToU32(size));
reed@google.comacd471f2011-05-03 21:26:46 +00001227 }
reed@google.combb6793b2011-05-05 15:18:15 +00001228 fWriter.writePad(ptr, size);
reed@google.combb6992a2011-04-26 17:41:56 +00001229 }
1230 }
1231}
1232
robertphillips@google.com0a4805e2013-05-29 13:24:23 +00001233void SkGPipeCanvas::beginCommentGroup(const char* description) {
1234 // ignore for now
1235}
1236
1237void SkGPipeCanvas::addComment(const char* kywd, const char* value) {
1238 // ignore for now
1239}
1240
1241void SkGPipeCanvas::endCommentGroup() {
1242 // ignore for now
1243}
1244
junov@chromium.org77eec242012-07-18 17:54:45 +00001245void SkGPipeCanvas::flushRecording(bool detachCurrentBlock) {
commit-bot@chromium.orgd0490172014-05-14 15:12:08 +00001246 this->doNotify();
junov@chromium.org77eec242012-07-18 17:54:45 +00001247 if (detachCurrentBlock) {
1248 // force a new block to be requested for the next recorded command
rmistry@google.comd6176b02012-08-23 18:14:13 +00001249 fBlockSize = 0;
junov@chromium.org77eec242012-07-18 17:54:45 +00001250 }
1251}
1252
junov@chromium.org2e14ba82012-08-07 14:26:57 +00001253size_t SkGPipeCanvas::freeMemoryIfPossible(size_t bytesToFree) {
scroggo@google.comd5d158b2012-08-14 20:38:28 +00001254 return (NULL == fBitmapHeap) ? 0 : fBitmapHeap->freeMemoryIfPossible(bytesToFree);
junov@chromium.org2e14ba82012-08-07 14:26:57 +00001255}
1256
reed@google.combb6992a2011-04-26 17:41:56 +00001257///////////////////////////////////////////////////////////////////////////////
1258
1259template <typename T> uint32_t castToU32(T value) {
1260 union {
1261 T fSrc;
1262 uint32_t fDst;
1263 } data;
1264 data.fSrc = value;
1265 return data.fDst;
1266}
1267
reed@google.com31891582011-05-12 03:03:56 +00001268void SkGPipeCanvas::writePaint(const SkPaint& paint) {
scroggo@google.comd5d158b2012-08-14 20:38:28 +00001269 if (fDone) {
1270 return;
1271 }
reed@google.com31891582011-05-12 03:03:56 +00001272 SkPaint& base = fPaint;
reed@google.combb6992a2011-04-26 17:41:56 +00001273 uint32_t storage[32];
1274 uint32_t* ptr = storage;
reed@google.combb6992a2011-04-26 17:41:56 +00001275
1276 if (base.getFlags() != paint.getFlags()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001277 *ptr++ = PaintOp_packOpData(kFlags_PaintOp, paint.getFlags());
reed@google.comf5842f72011-05-04 18:30:04 +00001278 base.setFlags(paint.getFlags());
reed@google.combb6992a2011-04-26 17:41:56 +00001279 }
1280 if (base.getColor() != paint.getColor()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001281 *ptr++ = PaintOp_packOp(kColor_PaintOp);
1282 *ptr++ = paint.getColor();
reed@google.comf5842f72011-05-04 18:30:04 +00001283 base.setColor(paint.getColor());
reed@google.combb6992a2011-04-26 17:41:56 +00001284 }
commit-bot@chromium.org85faf502014-04-16 12:58:02 +00001285 if (base.getFilterLevel() != paint.getFilterLevel()) {
1286 *ptr++ = PaintOp_packOpData(kFilterLevel_PaintOp, paint.getFilterLevel());
1287 base.setFilterLevel(paint.getFilterLevel());
1288 }
reed@google.combb6992a2011-04-26 17:41:56 +00001289 if (base.getStyle() != paint.getStyle()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001290 *ptr++ = PaintOp_packOpData(kStyle_PaintOp, paint.getStyle());
reed@google.comf5842f72011-05-04 18:30:04 +00001291 base.setStyle(paint.getStyle());
reed@google.combb6992a2011-04-26 17:41:56 +00001292 }
1293 if (base.getStrokeJoin() != paint.getStrokeJoin()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001294 *ptr++ = PaintOp_packOpData(kJoin_PaintOp, paint.getStrokeJoin());
reed@google.comf5842f72011-05-04 18:30:04 +00001295 base.setStrokeJoin(paint.getStrokeJoin());
reed@google.combb6992a2011-04-26 17:41:56 +00001296 }
1297 if (base.getStrokeCap() != paint.getStrokeCap()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001298 *ptr++ = PaintOp_packOpData(kCap_PaintOp, paint.getStrokeCap());
reed@google.comf5842f72011-05-04 18:30:04 +00001299 base.setStrokeCap(paint.getStrokeCap());
reed@google.combb6992a2011-04-26 17:41:56 +00001300 }
1301 if (base.getStrokeWidth() != paint.getStrokeWidth()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001302 *ptr++ = PaintOp_packOp(kWidth_PaintOp);
1303 *ptr++ = castToU32(paint.getStrokeWidth());
reed@google.comf5842f72011-05-04 18:30:04 +00001304 base.setStrokeWidth(paint.getStrokeWidth());
reed@google.combb6992a2011-04-26 17:41:56 +00001305 }
1306 if (base.getStrokeMiter() != paint.getStrokeMiter()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001307 *ptr++ = PaintOp_packOp(kMiter_PaintOp);
1308 *ptr++ = castToU32(paint.getStrokeMiter());
reed@google.comf5842f72011-05-04 18:30:04 +00001309 base.setStrokeMiter(paint.getStrokeMiter());
reed@google.combb6992a2011-04-26 17:41:56 +00001310 }
1311 if (base.getTextEncoding() != paint.getTextEncoding()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001312 *ptr++ = PaintOp_packOpData(kEncoding_PaintOp, paint.getTextEncoding());
reed@google.comf5842f72011-05-04 18:30:04 +00001313 base.setTextEncoding(paint.getTextEncoding());
reed@google.combb6992a2011-04-26 17:41:56 +00001314 }
1315 if (base.getHinting() != paint.getHinting()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001316 *ptr++ = PaintOp_packOpData(kHinting_PaintOp, paint.getHinting());
reed@google.comf5842f72011-05-04 18:30:04 +00001317 base.setHinting(paint.getHinting());
reed@google.combb6992a2011-04-26 17:41:56 +00001318 }
1319 if (base.getTextAlign() != paint.getTextAlign()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001320 *ptr++ = PaintOp_packOpData(kAlign_PaintOp, paint.getTextAlign());
reed@google.comf5842f72011-05-04 18:30:04 +00001321 base.setTextAlign(paint.getTextAlign());
reed@google.combb6992a2011-04-26 17:41:56 +00001322 }
1323 if (base.getTextSize() != paint.getTextSize()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001324 *ptr++ = PaintOp_packOp(kTextSize_PaintOp);
1325 *ptr++ = castToU32(paint.getTextSize());
reed@google.comf5842f72011-05-04 18:30:04 +00001326 base.setTextSize(paint.getTextSize());
reed@google.combb6992a2011-04-26 17:41:56 +00001327 }
1328 if (base.getTextScaleX() != paint.getTextScaleX()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001329 *ptr++ = PaintOp_packOp(kTextScaleX_PaintOp);
1330 *ptr++ = castToU32(paint.getTextScaleX());
reed@google.comf5842f72011-05-04 18:30:04 +00001331 base.setTextScaleX(paint.getTextScaleX());
reed@google.combb6992a2011-04-26 17:41:56 +00001332 }
1333 if (base.getTextSkewX() != paint.getTextSkewX()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001334 *ptr++ = PaintOp_packOp(kTextSkewX_PaintOp);
1335 *ptr++ = castToU32(paint.getTextSkewX());
reed@google.comf5842f72011-05-04 18:30:04 +00001336 base.setTextSkewX(paint.getTextSkewX());
1337 }
1338
1339 if (!SkTypeface::Equal(base.getTypeface(), paint.getTypeface())) {
fmalitaacb882c2014-09-16 17:58:34 -07001340 if (is_cross_process(fFlags)) {
scroggo@google.com3cb969f2012-07-27 20:39:19 +00001341 uint32_t id = this->getTypefaceID(paint.getTypeface());
1342 *ptr++ = PaintOp_packOpData(kTypeface_PaintOp, id);
1343 } else if (this->needOpBytes(sizeof(void*))) {
1344 // Add to the set for ref counting.
1345 fTypefaceSet.add(paint.getTypeface());
1346 // It is safe to write the typeface to the stream before the rest
1347 // of the paint unless we ever send a kReset_PaintOp, which we
1348 // currently never do.
1349 this->writeOp(kSetTypeface_DrawOp);
1350 fWriter.writePtr(paint.getTypeface());
1351 }
reed@google.comf5842f72011-05-04 18:30:04 +00001352 base.setTypeface(paint.getTypeface());
reed@google.combb6992a2011-04-26 17:41:56 +00001353 }
reed@google.combb6992a2011-04-26 17:41:56 +00001354
scroggo@google.com4dffc592012-07-17 16:49:40 +00001355 // This is a new paint, so all old flats can be safely purged, if necessary.
1356 fFlattenableHeap.markAllFlatsSafeToDelete();
reed@google.comb55d1182011-05-11 00:42:04 +00001357 for (int i = 0; i < kCount_PaintFlats; i++) {
1358 int index = this->flattenToIndex(get_paintflat(paint, i), (PaintFlats)i);
scroggo@google.comd3ba5cc2012-07-09 16:05:53 +00001359 bool replaced = index < 0;
1360 if (replaced) {
1361 index = ~index;
1362 }
scroggo@google.com4dffc592012-07-17 16:49:40 +00001363 // Store the index of any flat that needs to be kept. 0 means no flat.
1364 if (index > 0) {
1365 fFlattenableHeap.markFlatForKeeping(index);
1366 }
1367 SkASSERT(index >= 0 && index <= fFlatDictionary.count());
scroggo@google.comd3ba5cc2012-07-09 16:05:53 +00001368 if (index != fCurrFlatIndex[i] || replaced) {
reed@google.comb55d1182011-05-11 00:42:04 +00001369 *ptr++ = PaintOp_packOpFlagData(kFlatIndex_PaintOp, i, index);
1370 fCurrFlatIndex[i] = index;
1371 }
1372 }
1373
reed@google.comacd471f2011-05-03 21:26:46 +00001374 size_t size = (char*)ptr - (char*)storage;
1375 if (size && this->needOpBytes(size)) {
commit-bot@chromium.orgb45bd1f2014-04-24 18:17:30 +00001376 this->writeOp(kPaintOp_DrawOp, 0, SkToU32(size));
reed@google.comb55d1182011-05-11 00:42:04 +00001377 fWriter.write(storage, size);
reed@google.combb6992a2011-04-26 17:41:56 +00001378 for (size_t i = 0; i < size/4; i++) {
reed@google.comb55d1182011-05-11 00:42:04 +00001379// SkDebugf("[%d] %08X\n", i, storage[i]);
reed@google.combb6992a2011-04-26 17:41:56 +00001380 }
1381 }
reed@google.com0cd2ac62013-10-14 20:02:44 +00001382
1383 //
1384 // Do these after we've written kPaintOp_DrawOp
skia.committer@gmail.comfbc58a32013-10-15 07:02:27 +00001385
reed@google.com0cd2ac62013-10-14 20:02:44 +00001386 if (base.getAnnotation() != paint.getAnnotation()) {
1387 if (NULL == paint.getAnnotation()) {
commit-bot@chromium.org89ff3dd2013-10-29 20:29:38 +00001388 if (this->needOpBytes()) {
1389 this->writeOp(kSetAnnotation_DrawOp, 0, 0);
1390 }
reed@google.com0cd2ac62013-10-14 20:02:44 +00001391 } else {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +00001392 SkWriteBuffer buffer;
reed@google.com0cd2ac62013-10-14 20:02:44 +00001393 paint.getAnnotation()->writeToBuffer(buffer);
commit-bot@chromium.org89ff3dd2013-10-29 20:29:38 +00001394 const size_t size = buffer.bytesWritten();
1395 if (this->needOpBytes(size)) {
commit-bot@chromium.orgb45bd1f2014-04-24 18:17:30 +00001396 this->writeOp(kSetAnnotation_DrawOp, 0, SkToU32(size));
commit-bot@chromium.org89ff3dd2013-10-29 20:29:38 +00001397 buffer.writeToMemory(fWriter.reserve(size));
1398 }
reed@google.com0cd2ac62013-10-14 20:02:44 +00001399 }
commit-bot@chromium.org40258a52013-10-29 19:23:26 +00001400 base.setAnnotation(paint.getAnnotation());
reed@google.com0cd2ac62013-10-14 20:02:44 +00001401 }
reed@google.combb6992a2011-04-26 17:41:56 +00001402}
1403
1404///////////////////////////////////////////////////////////////////////////////
1405
1406#include "SkGPipe.h"
1407
scroggo@google.com3cb969f2012-07-27 20:39:19 +00001408SkGPipeController::~SkGPipeController() {
1409 SkSafeUnref(fCanvas);
1410}
1411
1412void SkGPipeController::setCanvas(SkGPipeCanvas* canvas) {
1413 SkRefCnt_SafeAssign(fCanvas, canvas);
1414}
1415
1416///////////////////////////////////////////////////////////////////////////////
1417
1418SkGPipeWriter::SkGPipeWriter()
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +00001419: fWriter(0) {
reed@google.combb6992a2011-04-26 17:41:56 +00001420 fCanvas = NULL;
1421}
1422
1423SkGPipeWriter::~SkGPipeWriter() {
reed@google.comacd471f2011-05-03 21:26:46 +00001424 this->endRecording();
reed@google.combb6992a2011-04-26 17:41:56 +00001425}
1426
junov@chromium.orga8db8fe2012-08-15 19:49:22 +00001427SkCanvas* SkGPipeWriter::startRecording(SkGPipeController* controller, uint32_t flags,
1428 uint32_t width, uint32_t height) {
reed@google.combb6992a2011-04-26 17:41:56 +00001429 if (NULL == fCanvas) {
reed@google.comacd471f2011-05-03 21:26:46 +00001430 fWriter.reset(NULL, 0);
junov@chromium.orga8db8fe2012-08-15 19:49:22 +00001431 fCanvas = SkNEW_ARGS(SkGPipeCanvas, (controller, &fWriter, flags, width, height));
reed@google.combb6992a2011-04-26 17:41:56 +00001432 }
scroggo@google.com3cb969f2012-07-27 20:39:19 +00001433 controller->setCanvas(fCanvas);
reed@google.combb6992a2011-04-26 17:41:56 +00001434 return fCanvas;
1435}
1436
1437void SkGPipeWriter::endRecording() {
1438 if (fCanvas) {
scroggo@google.com59c3ab62013-11-12 14:32:38 +00001439 fCanvas->finish(true);
reed@google.combb6992a2011-04-26 17:41:56 +00001440 fCanvas->unref();
1441 fCanvas = NULL;
1442 }
1443}
1444
junov@chromium.org2e14ba82012-08-07 14:26:57 +00001445void SkGPipeWriter::flushRecording(bool detachCurrentBlock) {
1446 if (fCanvas) {
1447 fCanvas->flushRecording(detachCurrentBlock);
1448 }
junov@chromium.org77eec242012-07-18 17:54:45 +00001449}
1450
junov@chromium.org2e14ba82012-08-07 14:26:57 +00001451size_t SkGPipeWriter::freeMemoryIfPossible(size_t bytesToFree) {
1452 if (fCanvas) {
1453 return fCanvas->freeMemoryIfPossible(bytesToFree);
1454 }
1455 return 0;
1456}
1457
1458size_t SkGPipeWriter::storageAllocatedForRecording() const {
scroggo@google.com15011ee2012-07-26 20:03:32 +00001459 return NULL == fCanvas ? 0 : fCanvas->storageAllocatedForRecording();
1460}
1461
scroggo@google.com3e26bd02012-08-14 15:20:01 +00001462///////////////////////////////////////////////////////////////////////////////
1463
1464BitmapShuttle::BitmapShuttle(SkGPipeCanvas* canvas) {
1465 SkASSERT(canvas != NULL);
1466 fCanvas = canvas;
1467 fCanvas->ref();
1468}
1469
1470BitmapShuttle::~BitmapShuttle() {
scroggo@google.com59c3ab62013-11-12 14:32:38 +00001471 this->removeCanvas();
scroggo@google.com3e26bd02012-08-14 15:20:01 +00001472}
1473
1474bool BitmapShuttle::insert(const SkBitmap& bitmap, int32_t slot) {
scroggo@google.com59c3ab62013-11-12 14:32:38 +00001475 SkASSERT(fCanvas != NULL);
scroggo@google.com3e26bd02012-08-14 15:20:01 +00001476 return fCanvas->shuttleBitmap(bitmap, slot);
1477}
scroggo@google.com59c3ab62013-11-12 14:32:38 +00001478
1479void BitmapShuttle::removeCanvas() {
1480 if (NULL == fCanvas) {
1481 return;
1482 }
1483 fCanvas->unref();
1484 fCanvas = NULL;
1485}