blob: 83e5c57d274b8dd92dafc0ac6bcc4ffbedf1e8a0 [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
mtklein36352bf2015-03-25 18:17:31 -070089 void* allocThrow(size_t bytes) override;
scroggo@google.com4dffc592012-07-17 16:49:40 +000090
mtklein36352bf2015-03-25 18:17:31 -070091 void unalloc(void* ptr) override;
scroggo@google.com4dffc592012-07-17 16:49:40 +000092
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
mtklein36352bf2015-03-25 18:17:31 -0700178 bool insert(const SkBitmap& bitmap, int32_t slot) override;
scroggo@google.com59c3ab62013-11-12 14:32:38 +0000179
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
mtklein36352bf2015-03-25 18:17:31 -0700234 void beginCommentGroup(const char* description) override;
235 void addComment(const char* kywd, const char* value) override;
236 void endCommentGroup() override;
reed@google.combb6992a2011-04-26 17:41:56 +0000237
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000238 /**
239 * Flatten an SkBitmap to send to the reader, where it will be referenced
240 * according to slot.
241 */
242 bool shuttleBitmap(const SkBitmap&, int32_t slot);
commit-bot@chromium.orged9806f2014-02-21 02:32:36 +0000243
244protected:
mtklein36352bf2015-03-25 18:17:31 -0700245 void willSave() override;
246 SaveLayerStrategy willSaveLayer(const SkRect*, const SkPaint*, SaveFlags) override;
247 void willRestore() override;
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000248
mtklein36352bf2015-03-25 18:17:31 -0700249 void didConcat(const SkMatrix&) override;
250 void didSetMatrix(const SkMatrix&) override;
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +0000251
mtklein36352bf2015-03-25 18:17:31 -0700252 void onDrawDRRect(const SkRRect&, const SkRRect&, const SkPaint&) override;
tfarinad96fd0c2015-04-24 13:12:44 -0700253 void onDrawText(const void* text, size_t byteLength, SkScalar x, SkScalar y,
254 const SkPaint&) override;
255 void onDrawPosText(const void* text, size_t byteLength, const SkPoint pos[],
256 const SkPaint&) override;
257 void onDrawPosTextH(const void* text, size_t byteLength, const SkScalar xpos[],
258 SkScalar constY, const SkPaint&) override;
259 void onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& path,
260 const SkMatrix* matrix, const SkPaint&) override;
261 void onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
262 const SkPaint& paint) override;
263 void onDrawPatch(const SkPoint cubics[12], const SkColor colors[4],
264 const SkPoint texCoords[4], SkXfermode* xmode,
265 const SkPaint& paint) override;
mtklein36352bf2015-03-25 18:17:31 -0700266 void onDrawPaint(const SkPaint&) override;
267 void onDrawPoints(PointMode, size_t count, const SkPoint pts[], const SkPaint&) override;
268 void onDrawRect(const SkRect&, const SkPaint&) override;
269 void onDrawOval(const SkRect&, const SkPaint&) override;
270 void onDrawRRect(const SkRRect&, const SkPaint&) override;
271 void onDrawPath(const SkPath&, const SkPaint&) override;
272 void onDrawBitmap(const SkBitmap&, SkScalar left, SkScalar top, const SkPaint*) override;
reed41af9662015-01-05 07:49:08 -0800273 void onDrawBitmapRect(const SkBitmap&, const SkRect* src, const SkRect& dst, const SkPaint*,
mtklein36352bf2015-03-25 18:17:31 -0700274 DrawBitmapRectFlags flags) override;
reed41af9662015-01-05 07:49:08 -0800275#if 0
276 // rely on decomposition into bitmap (for now)
mtklein36352bf2015-03-25 18:17:31 -0700277 void onDrawImage(const SkImage*, SkScalar left, SkScalar top, const SkPaint*) override;
reed41af9662015-01-05 07:49:08 -0800278 void onDrawImageRect(const SkImage*, const SkRect* src, const SkRect& dst,
mtklein36352bf2015-03-25 18:17:31 -0700279 const SkPaint*) override;
reed41af9662015-01-05 07:49:08 -0800280#endif
281 void onDrawBitmapNine(const SkBitmap&, const SkIRect& center, const SkRect& dst,
mtklein36352bf2015-03-25 18:17:31 -0700282 const SkPaint*) override;
283 void onDrawSprite(const SkBitmap&, int left, int top, const SkPaint*) override;
reed41af9662015-01-05 07:49:08 -0800284 void onDrawVertices(VertexMode vmode, int vertexCount,
285 const SkPoint vertices[], const SkPoint texs[],
286 const SkColor colors[], SkXfermode* xmode,
287 const uint16_t indices[], int indexCount,
mtklein36352bf2015-03-25 18:17:31 -0700288 const SkPaint&) override;
289 void onClipRect(const SkRect&, SkRegion::Op, ClipEdgeStyle) override;
290 void onClipRRect(const SkRRect&, SkRegion::Op, ClipEdgeStyle) override;
291 void onClipPath(const SkPath&, SkRegion::Op, ClipEdgeStyle) override;
292 void onClipRegion(const SkRegion&, SkRegion::Op) override;
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000293
mtklein36352bf2015-03-25 18:17:31 -0700294 void onDrawPicture(const SkPicture*, const SkMatrix*, const SkPaint*) override;
robertphillips9b14f262014-06-04 05:40:44 -0700295
reed@google.combb6992a2011-04-26 17:41:56 +0000296private:
commit-bot@chromium.orgd9ea09e2014-03-25 17:32:26 +0000297 void recordTranslate(const SkMatrix&);
298 void recordScale(const SkMatrix&);
299 void recordConcat(const SkMatrix&);
300
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000301 SkNamedFactorySet* fFactorySet;
scroggo@google.comd9d29672012-08-14 17:21:34 +0000302 SkBitmapHeap* fBitmapHeap;
reed@google.comacd471f2011-05-03 21:26:46 +0000303 SkGPipeController* fController;
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000304 SkWriter32& fWriter;
305 size_t fBlockSize; // amount allocated for writer
306 size_t fBytesNotified;
307 bool fDone;
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000308 const uint32_t fFlags;
reed@google.combb6992a2011-04-26 17:41:56 +0000309
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000310 SkRefCntSet fTypefaceSet;
reed@google.comf5842f72011-05-04 18:30:04 +0000311
312 uint32_t getTypefaceID(SkTypeface*);
313
reed@google.comacd471f2011-05-03 21:26:46 +0000314 inline void writeOp(DrawOps op, unsigned flags, unsigned data) {
reed@google.combb6992a2011-04-26 17:41:56 +0000315 fWriter.write32(DrawOp_packOpFlagData(op, flags, data));
316 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000317
reed@google.comacd471f2011-05-03 21:26:46 +0000318 inline void writeOp(DrawOps op) {
reed@google.combb6992a2011-04-26 17:41:56 +0000319 fWriter.write32(DrawOp_packOpFlagData(op, 0, 0));
320 }
reed@google.comacd471f2011-05-03 21:26:46 +0000321
322 bool needOpBytes(size_t size = 0);
323
324 inline void doNotify() {
325 if (!fDone) {
reed@google.com44699382013-10-31 17:28:30 +0000326 size_t bytes = fWriter.bytesWritten() - fBytesNotified;
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000327 if (bytes > 0) {
328 fController->notifyWritten(bytes);
329 fBytesNotified += bytes;
330 }
reed@google.comacd471f2011-05-03 21:26:46 +0000331 }
332 }
reed@google.comb55d1182011-05-11 00:42:04 +0000333
fmalitaacb882c2014-09-16 17:58:34 -0700334 typedef SkAutoSTMalloc<128, uint8_t> TypefaceBuffer;
335 size_t getInProcessTypefaces(const SkRefCntSet& typefaceSet, TypefaceBuffer*);
336 size_t getCrossProcessTypefaces(const SkRefCntSet& typefaceSet, TypefaceBuffer*);
337
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000338 // Should be called after any calls to an SkFlatDictionary::findAndReplace
339 // if a new SkFlatData was added when in cross process mode
340 void flattenFactoryNames();
341
scroggo@google.com59c3ab62013-11-12 14:32:38 +0000342 FlattenableHeap fFlattenableHeap;
343 FlatDictionary fFlatDictionary;
344 SkAutoTUnref<BitmapShuttle> fBitmapShuttle;
345 int fCurrFlatIndex[kCount_PaintFlats];
346
reed@google.comb55d1182011-05-11 00:42:04 +0000347 int flattenToIndex(SkFlattenable* obj, PaintFlats);
348
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000349 // Common code used by drawBitmap*. Behaves differently depending on the
350 // type of SkBitmapHeap being used, which is determined by the flags used.
351 bool commonDrawBitmap(const SkBitmap& bm, DrawOps op, unsigned flags,
352 size_t opBytesNeeded, const SkPaint* paint);
scroggo@google.com58be6822012-07-30 14:40:01 +0000353
reed@google.com31891582011-05-12 03:03:56 +0000354 SkPaint fPaint;
355 void writePaint(const SkPaint&);
reed@google.combb6992a2011-04-26 17:41:56 +0000356
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000357 class AutoPipeNotify {
358 public:
359 AutoPipeNotify(SkGPipeCanvas* canvas) : fCanvas(canvas) {}
360 ~AutoPipeNotify() { fCanvas->doNotify(); }
361 private:
362 SkGPipeCanvas* fCanvas;
363 };
364 friend class AutoPipeNotify;
365
reed@google.combb6992a2011-04-26 17:41:56 +0000366 typedef SkCanvas INHERITED;
367};
368
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000369void SkGPipeCanvas::flattenFactoryNames() {
370 const char* name;
371 while ((name = fFactorySet->getNextAddedFactoryName()) != NULL) {
372 size_t len = strlen(name);
commit-bot@chromium.orgd0490172014-05-14 15:12:08 +0000373 if (this->needOpBytes(SkWriter32::WriteStringSize(name, len))) {
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000374 this->writeOp(kDef_Factory_DrawOp);
375 fWriter.writeString(name, len);
376 }
377 }
378}
379
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000380bool SkGPipeCanvas::shuttleBitmap(const SkBitmap& bm, int32_t slot) {
scroggo@google.com565254b2012-06-28 15:41:32 +0000381 SkASSERT(shouldFlattenBitmaps(fFlags));
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000382 SkWriteBuffer buffer;
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000383 buffer.setNamedFactoryRecorder(fFactorySet);
scroggo@google.com74b7ffd2013-04-30 02:32:41 +0000384 buffer.writeBitmap(bm);
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000385 this->flattenFactoryNames();
commit-bot@chromium.orgb45bd1f2014-04-24 18:17:30 +0000386 size_t size = buffer.bytesWritten();
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000387 if (this->needOpBytes(size)) {
388 this->writeOp(kDef_Bitmap_DrawOp, 0, slot);
389 void* dst = static_cast<void*>(fWriter.reserve(size));
390 buffer.writeToMemory(dst);
391 return true;
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000392 }
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000393 return false;
scroggo@google.comd3ba5cc2012-07-09 16:05:53 +0000394}
395
reed@google.comb55d1182011-05-11 00:42:04 +0000396// return 0 for NULL (or unflattenable obj), or index-base-1
scroggo@google.comd3ba5cc2012-07-09 16:05:53 +0000397// return ~(index-base-1) if an old flattenable was replaced
reed@google.comb55d1182011-05-11 00:42:04 +0000398int SkGPipeCanvas::flattenToIndex(SkFlattenable* obj, PaintFlats paintflat) {
scroggo@google.comd5d158b2012-08-14 20:38:28 +0000399 SkASSERT(!fDone && fBitmapHeap != NULL);
reed@google.comb55d1182011-05-11 00:42:04 +0000400 if (NULL == obj) {
401 return 0;
402 }
reed@google.comb55d1182011-05-11 00:42:04 +0000403
scroggo@google.comd9d29672012-08-14 17:21:34 +0000404 fBitmapHeap->deferAddingOwners();
scroggo@google.com4dffc592012-07-17 16:49:40 +0000405 bool added, replaced;
scroggo@google.com664fab12012-08-14 19:22:05 +0000406 const SkFlatData* flat = fFlatDictionary.findAndReplace(*obj, fFlattenableHeap.flatToReplace(),
407 &added, &replaced);
scroggo@google.comd9d29672012-08-14 17:21:34 +0000408 fBitmapHeap->endAddingOwnersDeferral(added);
scroggo@google.com4dffc592012-07-17 16:49:40 +0000409 int index = flat->index();
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000410 if (added) {
fmalitaacb882c2014-09-16 17:58:34 -0700411 if (is_cross_process(fFlags)) {
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000412 this->flattenFactoryNames();
413 }
414 size_t flatSize = flat->flatSize();
415 if (this->needOpBytes(flatSize)) {
416 this->writeOp(kDef_Flattenable_DrawOp, paintflat, index);
417 fWriter.write(flat->data(), flatSize);
418 }
scroggo@google.com4dffc592012-07-17 16:49:40 +0000419 }
420 if (replaced) {
reed@google.comb55d1182011-05-11 00:42:04 +0000421 index = ~index;
reed@google.comb55d1182011-05-11 00:42:04 +0000422 }
scroggo@google.com4dffc592012-07-17 16:49:40 +0000423 return index;
reed@google.comb55d1182011-05-11 00:42:04 +0000424}
425
reed@google.combb6992a2011-04-26 17:41:56 +0000426///////////////////////////////////////////////////////////////////////////////
427
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000428#define MIN_BLOCK_SIZE (16 * 1024)
scroggo@google.com4dffc592012-07-17 16:49:40 +0000429#define BITMAPS_TO_KEEP 5
430#define FLATTENABLES_TO_KEEP 10
reed@google.combb6992a2011-04-26 17:41:56 +0000431
reed@google.comacd471f2011-05-03 21:26:46 +0000432SkGPipeCanvas::SkGPipeCanvas(SkGPipeController* controller,
rmistry@google.comd6176b02012-08-23 18:14:13 +0000433 SkWriter32* writer, uint32_t flags,
junov@chromium.orga8db8fe2012-08-15 19:49:22 +0000434 uint32_t width, uint32_t height)
commit-bot@chromium.org403f8d72014-02-17 15:24:26 +0000435 : SkCanvas(width, height)
fmalitaacb882c2014-09-16 17:58:34 -0700436 , fFactorySet(is_cross_process(flags) ? SkNEW(SkNamedFactorySet) : NULL)
commit-bot@chromium.org403f8d72014-02-17 15:24:26 +0000437 , fWriter(*writer)
438 , fFlags(flags)
fmalitaacb882c2014-09-16 17:58:34 -0700439 , fFlattenableHeap(FLATTENABLES_TO_KEEP, fFactorySet, is_cross_process(flags))
commit-bot@chromium.org403f8d72014-02-17 15:24:26 +0000440 , fFlatDictionary(&fFlattenableHeap)
441{
reed@google.comacd471f2011-05-03 21:26:46 +0000442 fController = controller;
reed@google.combb6992a2011-04-26 17:41:56 +0000443 fDone = false;
reed@google.comacd471f2011-05-03 21:26:46 +0000444 fBlockSize = 0; // need first block from controller
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000445 fBytesNotified = 0;
reed@google.comb55d1182011-05-11 00:42:04 +0000446 sk_bzero(fCurrFlatIndex, sizeof(fCurrFlatIndex));
reed@google.comacd471f2011-05-03 21:26:46 +0000447
scroggo@google.com565254b2012-06-28 15:41:32 +0000448 // Tell the reader the appropriate flags to use.
449 if (this->needOpBytes()) {
450 this->writeOp(kReportFlags_DrawOp, fFlags, 0);
451 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000452
scroggo@google.com10dccde2012-08-08 20:43:22 +0000453 if (shouldFlattenBitmaps(flags)) {
scroggo@google.com59c3ab62013-11-12 14:32:38 +0000454 fBitmapShuttle.reset(SkNEW_ARGS(BitmapShuttle, (this)));
455 fBitmapHeap = SkNEW_ARGS(SkBitmapHeap, (fBitmapShuttle.get(), BITMAPS_TO_KEEP));
scroggo@google.com10dccde2012-08-08 20:43:22 +0000456 } else {
scroggo@google.comd9d29672012-08-14 17:21:34 +0000457 fBitmapHeap = SkNEW_ARGS(SkBitmapHeap,
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000458 (BITMAPS_TO_KEEP, controller->numberOfReaders()));
scroggo@google.com10dccde2012-08-08 20:43:22 +0000459 if (this->needOpBytes(sizeof(void*))) {
scroggo@google.comd9d29672012-08-14 17:21:34 +0000460 this->writeOp(kShareBitmapHeap_DrawOp);
461 fWriter.writePtr(static_cast<void*>(fBitmapHeap));
scroggo@google.com10dccde2012-08-08 20:43:22 +0000462 }
463 }
scroggo@google.comd9d29672012-08-14 17:21:34 +0000464 fFlattenableHeap.setBitmapStorage(fBitmapHeap);
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000465 this->doNotify();
reed@google.combb6992a2011-04-26 17:41:56 +0000466}
467
468SkGPipeCanvas::~SkGPipeCanvas() {
scroggo@google.com59c3ab62013-11-12 14:32:38 +0000469 this->finish(true);
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000470 SkSafeUnref(fFactorySet);
scroggo@google.comd9d29672012-08-14 17:21:34 +0000471 SkSafeUnref(fBitmapHeap);
reed@google.combb6992a2011-04-26 17:41:56 +0000472}
473
reed@google.comacd471f2011-05-03 21:26:46 +0000474bool SkGPipeCanvas::needOpBytes(size_t needed) {
475 if (fDone) {
476 return false;
477 }
478
479 needed += 4; // size of DrawOp atom
commit-bot@chromium.orgd0490172014-05-14 15:12:08 +0000480 needed = SkAlign4(needed);
reed@google.com44699382013-10-31 17:28:30 +0000481 if (fWriter.bytesWritten() + needed > fBlockSize) {
mtklein6d88e6c2014-07-30 09:17:54 -0700482 // Before we wipe out any data that has already been written, read it out.
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000483 this->doNotify();
mtklein6d88e6c2014-07-30 09:17:54 -0700484
485 // If we're going to allocate a new block, allocate enough to make it worthwhile.
486 needed = SkTMax<size_t>(MIN_BLOCK_SIZE, needed);
487
commit-bot@chromium.orgd0490172014-05-14 15:12:08 +0000488 void* block = fController->requestBlock(needed, &fBlockSize);
reed@google.comacd471f2011-05-03 21:26:46 +0000489 if (NULL == block) {
scroggo@google.com59c3ab62013-11-12 14:32:38 +0000490 // Do not notify the readers, which would call this function again.
491 this->finish(false);
reed@google.comacd471f2011-05-03 21:26:46 +0000492 return false;
493 }
scroggo@google.com460a23e2012-08-16 17:56:49 +0000494 SkASSERT(SkIsAlign4(fBlockSize));
reed@google.comacd471f2011-05-03 21:26:46 +0000495 fWriter.reset(block, fBlockSize);
496 fBytesNotified = 0;
497 }
498 return true;
499}
500
reed@google.comf5842f72011-05-04 18:30:04 +0000501uint32_t SkGPipeCanvas::getTypefaceID(SkTypeface* face) {
502 uint32_t id = 0; // 0 means default/null typeface
503 if (face) {
504 id = fTypefaceSet.find(face);
505 if (0 == id) {
506 id = fTypefaceSet.add(face);
507 size_t size = writeTypeface(NULL, face);
508 if (this->needOpBytes(size)) {
reed@google.combb6793b2011-05-05 15:18:15 +0000509 this->writeOp(kDef_Typeface_DrawOp);
reed@google.comf5842f72011-05-04 18:30:04 +0000510 writeTypeface(&fWriter, face);
511 }
512 }
513 }
514 return id;
515}
516
reed@google.combb6992a2011-04-26 17:41:56 +0000517///////////////////////////////////////////////////////////////////////////////
518
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000519#define NOTIFY_SETUP(canvas) \
520 AutoPipeNotify apn(canvas)
521
Florin Malita5f6102d2014-06-30 10:13:28 -0400522void SkGPipeCanvas::willSave() {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000523 NOTIFY_SETUP(this);
reed@google.comacd471f2011-05-03 21:26:46 +0000524 if (this->needOpBytes()) {
Florin Malita5f6102d2014-06-30 10:13:28 -0400525 this->writeOp(kSave_DrawOp);
reed@google.comacd471f2011-05-03 21:26:46 +0000526 }
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000527
Florin Malita5f6102d2014-06-30 10:13:28 -0400528 this->INHERITED::willSave();
reed@google.combb6992a2011-04-26 17:41:56 +0000529}
530
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000531SkCanvas::SaveLayerStrategy SkGPipeCanvas::willSaveLayer(const SkRect* bounds, const SkPaint* paint,
532 SaveFlags saveFlags) {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000533 NOTIFY_SETUP(this);
reed@google.comacd471f2011-05-03 21:26:46 +0000534 size_t size = 0;
reed@google.combb6992a2011-04-26 17:41:56 +0000535 unsigned opFlags = 0;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000536
reed@google.combb6992a2011-04-26 17:41:56 +0000537 if (bounds) {
538 opFlags |= kSaveLayer_HasBounds_DrawOpFlag;
reed@google.comacd471f2011-05-03 21:26:46 +0000539 size += sizeof(SkRect);
reed@google.combb6992a2011-04-26 17:41:56 +0000540 }
541 if (paint) {
542 opFlags |= kSaveLayer_HasPaint_DrawOpFlag;
reed@google.com31891582011-05-12 03:03:56 +0000543 this->writePaint(*paint);
reed@google.combb6992a2011-04-26 17:41:56 +0000544 }
545
reed@google.comacd471f2011-05-03 21:26:46 +0000546 if (this->needOpBytes(size)) {
547 this->writeOp(kSaveLayer_DrawOp, opFlags, saveFlags);
548 if (bounds) {
549 fWriter.writeRect(*bounds);
550 }
reed@google.combb6992a2011-04-26 17:41:56 +0000551 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000552
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000553 this->INHERITED::willSaveLayer(bounds, paint, saveFlags);
554 // we don't create a layer
555 return kNoLayer_SaveLayerStrategy;
reed@google.combb6992a2011-04-26 17:41:56 +0000556}
557
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000558void SkGPipeCanvas::willRestore() {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000559 NOTIFY_SETUP(this);
reed@google.comacd471f2011-05-03 21:26:46 +0000560 if (this->needOpBytes()) {
561 this->writeOp(kRestore_DrawOp);
562 }
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000563
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000564 this->INHERITED::willRestore();
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000565}
566
commit-bot@chromium.orgd9ea09e2014-03-25 17:32:26 +0000567void SkGPipeCanvas::recordTranslate(const SkMatrix& m) {
568 if (this->needOpBytes(2 * sizeof(SkScalar))) {
569 this->writeOp(kTranslate_DrawOp);
570 fWriter.writeScalar(m.getTranslateX());
571 fWriter.writeScalar(m.getTranslateY());
reed@google.combb6992a2011-04-26 17:41:56 +0000572 }
reed@google.combb6992a2011-04-26 17:41:56 +0000573}
574
commit-bot@chromium.orgd9ea09e2014-03-25 17:32:26 +0000575void SkGPipeCanvas::recordScale(const SkMatrix& m) {
576 if (this->needOpBytes(2 * sizeof(SkScalar))) {
577 this->writeOp(kScale_DrawOp);
578 fWriter.writeScalar(m.getScaleX());
579 fWriter.writeScalar(m.getScaleY());
reed@google.combb6992a2011-04-26 17:41:56 +0000580 }
reed@google.combb6992a2011-04-26 17:41:56 +0000581}
582
commit-bot@chromium.orgd9ea09e2014-03-25 17:32:26 +0000583void SkGPipeCanvas::recordConcat(const SkMatrix& m) {
584 if (this->needOpBytes(m.writeToMemory(NULL))) {
585 this->writeOp(kConcat_DrawOp);
586 fWriter.writeMatrix(m);
reed@google.combb6992a2011-04-26 17:41:56 +0000587 }
reed@google.combb6992a2011-04-26 17:41:56 +0000588}
589
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +0000590void SkGPipeCanvas::didConcat(const SkMatrix& matrix) {
reed@google.combb6992a2011-04-26 17:41:56 +0000591 if (!matrix.isIdentity()) {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000592 NOTIFY_SETUP(this);
commit-bot@chromium.orgd9ea09e2014-03-25 17:32:26 +0000593 switch (matrix.getType()) {
594 case SkMatrix::kTranslate_Mask:
595 this->recordTranslate(matrix);
596 break;
597 case SkMatrix::kScale_Mask:
598 this->recordScale(matrix);
599 break;
600 default:
601 this->recordConcat(matrix);
602 break;
reed@google.comacd471f2011-05-03 21:26:46 +0000603 }
reed@google.combb6992a2011-04-26 17:41:56 +0000604 }
commit-bot@chromium.orgd9ea09e2014-03-25 17:32:26 +0000605
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +0000606 this->INHERITED::didConcat(matrix);
reed@google.combb6992a2011-04-26 17:41:56 +0000607}
608
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +0000609void SkGPipeCanvas::didSetMatrix(const SkMatrix& matrix) {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000610 NOTIFY_SETUP(this);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000611 if (this->needOpBytes(matrix.writeToMemory(NULL))) {
reed@google.comacd471f2011-05-03 21:26:46 +0000612 this->writeOp(kSetMatrix_DrawOp);
djsollen@google.com2b2ede32012-04-12 13:24:04 +0000613 fWriter.writeMatrix(matrix);
reed@google.comacd471f2011-05-03 21:26:46 +0000614 }
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +0000615 this->INHERITED::didSetMatrix(matrix);
reed@google.combb6992a2011-04-26 17:41:56 +0000616}
617
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000618void SkGPipeCanvas::onClipRect(const SkRect& rect, SkRegion::Op rgnOp,
619 ClipEdgeStyle edgeStyle) {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000620 NOTIFY_SETUP(this);
scroggo@google.com460a23e2012-08-16 17:56:49 +0000621 if (this->needOpBytes(sizeof(SkRect))) {
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000622 unsigned flags = 0;
623 if (kSoft_ClipEdgeStyle == edgeStyle) {
624 flags = kClip_HasAntiAlias_DrawOpFlag;
625 }
scroggo@google.com460a23e2012-08-16 17:56:49 +0000626 this->writeOp(kClipRect_DrawOp, flags, rgnOp);
reed@google.comacd471f2011-05-03 21:26:46 +0000627 fWriter.writeRect(rect);
628 }
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000629 this->INHERITED::onClipRect(rect, rgnOp, edgeStyle);
reed@google.combb6992a2011-04-26 17:41:56 +0000630}
631
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000632void SkGPipeCanvas::onClipRRect(const SkRRect& rrect, SkRegion::Op rgnOp,
633 ClipEdgeStyle edgeStyle) {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000634 NOTIFY_SETUP(this);
reed@google.com4ed0fb72012-12-12 20:48:18 +0000635 if (this->needOpBytes(kSizeOfFlatRRect)) {
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000636 unsigned flags = 0;
637 if (kSoft_ClipEdgeStyle == edgeStyle) {
638 flags = kClip_HasAntiAlias_DrawOpFlag;
639 }
reed@google.com4ed0fb72012-12-12 20:48:18 +0000640 this->writeOp(kClipRRect_DrawOp, flags, rgnOp);
641 fWriter.writeRRect(rrect);
642 }
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000643 this->INHERITED::onClipRRect(rrect, rgnOp, edgeStyle);
reed@google.com4ed0fb72012-12-12 20:48:18 +0000644}
645
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000646void SkGPipeCanvas::onClipPath(const SkPath& path, SkRegion::Op rgnOp,
647 ClipEdgeStyle edgeStyle) {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000648 NOTIFY_SETUP(this);
scroggo@google.com460a23e2012-08-16 17:56:49 +0000649 if (this->needOpBytes(path.writeToMemory(NULL))) {
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000650 unsigned flags = 0;
651 if (kSoft_ClipEdgeStyle == edgeStyle) {
652 flags = kClip_HasAntiAlias_DrawOpFlag;
653 }
scroggo@google.com460a23e2012-08-16 17:56:49 +0000654 this->writeOp(kClipPath_DrawOp, flags, rgnOp);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000655 fWriter.writePath(path);
reed@google.comacd471f2011-05-03 21:26:46 +0000656 }
reed@google.combb6992a2011-04-26 17:41:56 +0000657 // we just pass on the bounds of the path
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000658 this->INHERITED::onClipRect(path.getBounds(), rgnOp, edgeStyle);
reed@google.combb6992a2011-04-26 17:41:56 +0000659}
660
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000661void SkGPipeCanvas::onClipRegion(const SkRegion& region, SkRegion::Op rgnOp) {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000662 NOTIFY_SETUP(this);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000663 if (this->needOpBytes(region.writeToMemory(NULL))) {
reed@google.comacd471f2011-05-03 21:26:46 +0000664 this->writeOp(kClipRegion_DrawOp, 0, rgnOp);
djsollen@google.com2b2ede32012-04-12 13:24:04 +0000665 fWriter.writeRegion(region);
reed@google.comacd471f2011-05-03 21:26:46 +0000666 }
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000667 this->INHERITED::onClipRegion(region, rgnOp);
reed@google.combb6992a2011-04-26 17:41:56 +0000668}
669
670///////////////////////////////////////////////////////////////////////////////
671
reed41af9662015-01-05 07:49:08 -0800672void SkGPipeCanvas::onDrawPaint(const SkPaint& paint) {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000673 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000674 this->writePaint(paint);
reed@google.comacd471f2011-05-03 21:26:46 +0000675 if (this->needOpBytes()) {
reed@google.com31891582011-05-12 03:03:56 +0000676 this->writeOp(kDrawPaint_DrawOp);
reed@google.comacd471f2011-05-03 21:26:46 +0000677 }
reed@google.combb6992a2011-04-26 17:41:56 +0000678}
679
reed41af9662015-01-05 07:49:08 -0800680void SkGPipeCanvas::onDrawPoints(PointMode mode, size_t count,
681 const SkPoint pts[], const SkPaint& paint) {
reed@google.combb6992a2011-04-26 17:41:56 +0000682 if (count) {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000683 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000684 this->writePaint(paint);
reed@google.comacd471f2011-05-03 21:26:46 +0000685 if (this->needOpBytes(4 + count * sizeof(SkPoint))) {
reed@google.com31891582011-05-12 03:03:56 +0000686 this->writeOp(kDrawPoints_DrawOp, mode, 0);
commit-bot@chromium.orgb45bd1f2014-04-24 18:17:30 +0000687 fWriter.write32(SkToU32(count));
reed@google.comacd471f2011-05-03 21:26:46 +0000688 fWriter.write(pts, count * sizeof(SkPoint));
689 }
reed@google.combb6992a2011-04-26 17:41:56 +0000690 }
691}
692
reed41af9662015-01-05 07:49:08 -0800693void SkGPipeCanvas::onDrawOval(const SkRect& rect, const SkPaint& paint) {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000694 NOTIFY_SETUP(this);
reed@google.com4ed0fb72012-12-12 20:48:18 +0000695 this->writePaint(paint);
696 if (this->needOpBytes(sizeof(SkRect))) {
697 this->writeOp(kDrawOval_DrawOp);
698 fWriter.writeRect(rect);
699 }
700}
701
reed41af9662015-01-05 07:49:08 -0800702void SkGPipeCanvas::onDrawRect(const SkRect& rect, const SkPaint& paint) {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000703 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000704 this->writePaint(paint);
reed@google.comacd471f2011-05-03 21:26:46 +0000705 if (this->needOpBytes(sizeof(SkRect))) {
reed@google.com31891582011-05-12 03:03:56 +0000706 this->writeOp(kDrawRect_DrawOp);
reed@google.comacd471f2011-05-03 21:26:46 +0000707 fWriter.writeRect(rect);
708 }
reed@google.combb6992a2011-04-26 17:41:56 +0000709}
710
reed41af9662015-01-05 07:49:08 -0800711void SkGPipeCanvas::onDrawRRect(const SkRRect& rrect, const SkPaint& paint) {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000712 NOTIFY_SETUP(this);
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.orgf700fb22014-05-14 14:50:57 +0000722 NOTIFY_SETUP(this);
commit-bot@chromium.orged9806f2014-02-21 02:32:36 +0000723 this->writePaint(paint);
724 if (this->needOpBytes(kSizeOfFlatRRect * 2)) {
725 this->writeOp(kDrawDRRect_DrawOp);
726 fWriter.writeRRect(outer);
727 fWriter.writeRRect(inner);
728 }
729}
730
reed41af9662015-01-05 07:49:08 -0800731void SkGPipeCanvas::onDrawPath(const SkPath& path, const SkPaint& paint) {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000732 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000733 this->writePaint(paint);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000734 if (this->needOpBytes(path.writeToMemory(NULL))) {
reed@google.com31891582011-05-12 03:03:56 +0000735 this->writeOp(kDrawPath_DrawOp);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000736 fWriter.writePath(path);
reed@google.comacd471f2011-05-03 21:26:46 +0000737 }
reed@google.combb6992a2011-04-26 17:41:56 +0000738}
739
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000740bool SkGPipeCanvas::commonDrawBitmap(const SkBitmap& bm, DrawOps op,
741 unsigned flags,
742 size_t opBytesNeeded,
743 const SkPaint* paint) {
commit-bot@chromium.org44803fb2014-05-14 15:38:25 +0000744 if (fDone) {
745 return false;
746 }
747
scroggo@google.com58be6822012-07-30 14:40:01 +0000748 if (paint != NULL) {
scroggo@google.com460a23e2012-08-16 17:56:49 +0000749 flags |= kDrawBitmap_HasPaint_DrawOpFlag;
scroggo@google.com58be6822012-07-30 14:40:01 +0000750 this->writePaint(*paint);
751 }
commit-bot@chromium.org44803fb2014-05-14 15:38:25 +0000752 // This needs to run first so its calls to needOpBytes() and its writes
753 // don't interlace with the needOpBytes() and write below.
754 SkASSERT(fBitmapHeap != NULL);
755 int32_t bitmapIndex = fBitmapHeap->insert(bm);
756 if (SkBitmapHeap::INVALID_SLOT == bitmapIndex) {
757 return false;
758 }
759
scroggo@google.com10dccde2012-08-08 20:43:22 +0000760 if (this->needOpBytes(opBytesNeeded)) {
761 this->writeOp(op, flags, bitmapIndex);
scroggo@google.com58be6822012-07-30 14:40:01 +0000762 return true;
763 }
764 return false;
765}
766
reed41af9662015-01-05 07:49:08 -0800767void SkGPipeCanvas::onDrawBitmap(const SkBitmap& bm, SkScalar left, SkScalar top,
768 const SkPaint* paint) {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000769 NOTIFY_SETUP(this);
scroggo@google.com58be6822012-07-30 14:40:01 +0000770 size_t opBytesNeeded = sizeof(SkScalar) * 2;
771
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000772 if (this->commonDrawBitmap(bm, kDrawBitmap_DrawOp, 0, opBytesNeeded, paint)) {
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000773 fWriter.writeScalar(left);
774 fWriter.writeScalar(top);
775 }
reed@google.combb6992a2011-04-26 17:41:56 +0000776}
777
reed41af9662015-01-05 07:49:08 -0800778void SkGPipeCanvas::onDrawBitmapRect(const SkBitmap& bm, const SkRect* src, const SkRect& dst,
779 const SkPaint* paint, DrawBitmapRectFlags dbmrFlags) {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000780 NOTIFY_SETUP(this);
scroggo@google.com58be6822012-07-30 14:40:01 +0000781 size_t opBytesNeeded = sizeof(SkRect);
782 bool hasSrc = src != NULL;
783 unsigned flags;
784 if (hasSrc) {
scroggo@google.com460a23e2012-08-16 17:56:49 +0000785 flags = kDrawBitmap_HasSrcRect_DrawOpFlag;
scroggo@google.com58be6822012-07-30 14:40:01 +0000786 opBytesNeeded += sizeof(int32_t) * 4;
787 } else {
788 flags = 0;
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000789 }
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +0000790 if (dbmrFlags & kBleed_DrawBitmapRectFlag) {
791 flags |= kDrawBitmap_Bleed_DrawOpFlag;
792 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000793
reed@google.com71121732012-09-18 15:14:33 +0000794 if (this->commonDrawBitmap(bm, kDrawBitmapRectToRect_DrawOp, flags, opBytesNeeded, paint)) {
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000795 if (hasSrc) {
reed@google.com71121732012-09-18 15:14:33 +0000796 fWriter.writeRect(*src);
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000797 }
798 fWriter.writeRect(dst);
799 }
reed@google.combb6992a2011-04-26 17:41:56 +0000800}
801
reed41af9662015-01-05 07:49:08 -0800802void SkGPipeCanvas::onDrawBitmapNine(const SkBitmap& bm, const SkIRect& center,
803 const SkRect& dst, const SkPaint* paint) {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000804 NOTIFY_SETUP(this);
scroggo@google.com58be6822012-07-30 14:40:01 +0000805 size_t opBytesNeeded = sizeof(int32_t) * 4 + sizeof(SkRect);
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000806
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000807 if (this->commonDrawBitmap(bm, kDrawBitmapNine_DrawOp, 0, opBytesNeeded, paint)) {
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000808 fWriter.write32(center.fLeft);
809 fWriter.write32(center.fTop);
810 fWriter.write32(center.fRight);
811 fWriter.write32(center.fBottom);
812 fWriter.writeRect(dst);
813 }
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000814}
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000815
reed41af9662015-01-05 07:49:08 -0800816void SkGPipeCanvas::onDrawSprite(const SkBitmap& bm, int left, int top, const SkPaint* paint) {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000817 NOTIFY_SETUP(this);
scroggo@google.com58be6822012-07-30 14:40:01 +0000818 size_t opBytesNeeded = sizeof(int32_t) * 2;
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000819
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000820 if (this->commonDrawBitmap(bm, kDrawSprite_DrawOp, 0, opBytesNeeded, paint)) {
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000821 fWriter.write32(left);
822 fWriter.write32(top);
823 }
reed@google.combb6992a2011-04-26 17:41:56 +0000824}
825
reed@google.come0d9ce82014-04-23 04:00:17 +0000826void SkGPipeCanvas::onDrawText(const void* text, size_t byteLength, SkScalar x, SkScalar y,
827 const SkPaint& paint) {
reed@google.combb6992a2011-04-26 17:41:56 +0000828 if (byteLength) {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000829 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000830 this->writePaint(paint);
reed@google.comacd471f2011-05-03 21:26:46 +0000831 if (this->needOpBytes(4 + SkAlign4(byteLength) + 2 * sizeof(SkScalar))) {
reed@google.com31891582011-05-12 03:03:56 +0000832 this->writeOp(kDrawText_DrawOp);
commit-bot@chromium.orgb45bd1f2014-04-24 18:17:30 +0000833 fWriter.write32(SkToU32(byteLength));
reed@google.comacd471f2011-05-03 21:26:46 +0000834 fWriter.writePad(text, byteLength);
835 fWriter.writeScalar(x);
836 fWriter.writeScalar(y);
837 }
reed@google.combb6992a2011-04-26 17:41:56 +0000838 }
839}
840
reed@google.come0d9ce82014-04-23 04:00:17 +0000841void SkGPipeCanvas::onDrawPosText(const void* text, size_t byteLength, const SkPoint pos[],
842 const SkPaint& paint) {
reed@google.combb6992a2011-04-26 17:41:56 +0000843 if (byteLength) {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000844 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000845 this->writePaint(paint);
reed@google.combb6992a2011-04-26 17:41:56 +0000846 int count = paint.textToGlyphs(text, byteLength, NULL);
reed@google.comacd471f2011-05-03 21:26:46 +0000847 if (this->needOpBytes(4 + SkAlign4(byteLength) + 4 + count * sizeof(SkPoint))) {
reed@google.com31891582011-05-12 03:03:56 +0000848 this->writeOp(kDrawPosText_DrawOp);
commit-bot@chromium.orgb45bd1f2014-04-24 18:17:30 +0000849 fWriter.write32(SkToU32(byteLength));
reed@google.comacd471f2011-05-03 21:26:46 +0000850 fWriter.writePad(text, byteLength);
851 fWriter.write32(count);
852 fWriter.write(pos, count * sizeof(SkPoint));
853 }
reed@google.combb6992a2011-04-26 17:41:56 +0000854 }
855}
856
reed@google.come0d9ce82014-04-23 04:00:17 +0000857void SkGPipeCanvas::onDrawPosTextH(const void* text, size_t byteLength, const SkScalar xpos[],
858 SkScalar constY, const SkPaint& paint) {
reed@google.combb6992a2011-04-26 17:41:56 +0000859 if (byteLength) {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000860 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000861 this->writePaint(paint);
reed@google.combb6992a2011-04-26 17:41:56 +0000862 int count = paint.textToGlyphs(text, byteLength, NULL);
reed@google.comacd471f2011-05-03 21:26:46 +0000863 if (this->needOpBytes(4 + SkAlign4(byteLength) + 4 + count * sizeof(SkScalar) + 4)) {
reed@google.com31891582011-05-12 03:03:56 +0000864 this->writeOp(kDrawPosTextH_DrawOp);
commit-bot@chromium.orgb45bd1f2014-04-24 18:17:30 +0000865 fWriter.write32(SkToU32(byteLength));
reed@google.comacd471f2011-05-03 21:26:46 +0000866 fWriter.writePad(text, byteLength);
867 fWriter.write32(count);
868 fWriter.write(xpos, count * sizeof(SkScalar));
869 fWriter.writeScalar(constY);
870 }
reed@google.combb6992a2011-04-26 17:41:56 +0000871 }
872}
873
reed@google.come0d9ce82014-04-23 04:00:17 +0000874void SkGPipeCanvas::onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& path,
875 const SkMatrix* matrix, const SkPaint& paint) {
reed@google.combb6992a2011-04-26 17:41:56 +0000876 if (byteLength) {
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000877 NOTIFY_SETUP(this);
reed@google.combb6992a2011-04-26 17:41:56 +0000878 unsigned flags = 0;
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000879 size_t size = 4 + SkAlign4(byteLength) + path.writeToMemory(NULL);
reed@google.combb6992a2011-04-26 17:41:56 +0000880 if (matrix) {
881 flags |= kDrawTextOnPath_HasMatrix_DrawOpFlag;
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000882 size += matrix->writeToMemory(NULL);
reed@google.combb6992a2011-04-26 17:41:56 +0000883 }
reed@google.com31891582011-05-12 03:03:56 +0000884 this->writePaint(paint);
reed@google.comacd471f2011-05-03 21:26:46 +0000885 if (this->needOpBytes(size)) {
reed@google.com31891582011-05-12 03:03:56 +0000886 this->writeOp(kDrawTextOnPath_DrawOp, flags, 0);
reed@google.combb6992a2011-04-26 17:41:56 +0000887
commit-bot@chromium.orgb45bd1f2014-04-24 18:17:30 +0000888 fWriter.write32(SkToU32(byteLength));
reed@google.comacd471f2011-05-03 21:26:46 +0000889 fWriter.writePad(text, byteLength);
reed@google.combb6992a2011-04-26 17:41:56 +0000890
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000891 fWriter.writePath(path);
reed@google.comacd471f2011-05-03 21:26:46 +0000892 if (matrix) {
djsollen@google.com2b2ede32012-04-12 13:24:04 +0000893 fWriter.writeMatrix(*matrix);
reed@google.comacd471f2011-05-03 21:26:46 +0000894 }
reed@google.combb6992a2011-04-26 17:41:56 +0000895 }
896 }
897}
898
fmalitaacb882c2014-09-16 17:58:34 -0700899size_t SkGPipeCanvas::getInProcessTypefaces(const SkRefCntSet& typefaceSet,
900 TypefaceBuffer* buffer) {
901 // When in-process, we simply write out the typeface pointers.
902 size_t size = typefaceSet.count() * sizeof(SkTypeface*);
903 buffer->reset(size);
904 typefaceSet.copyToArray(reinterpret_cast<SkRefCnt**>(buffer->get()));
905
906 return size;
907}
908
909size_t SkGPipeCanvas::getCrossProcessTypefaces(const SkRefCntSet& typefaceSet,
910 TypefaceBuffer* buffer) {
911 // For cross-process we use typeface IDs.
912 size_t size = typefaceSet.count() * sizeof(uint32_t);
913 buffer->reset(size);
914
915 uint32_t* idBuffer = reinterpret_cast<uint32_t*>(buffer->get());
916 SkRefCntSet::Iter iter(typefaceSet);
917 int i = 0;
918
919 for (void* setPtr = iter.next(); setPtr; setPtr = iter.next()) {
920 idBuffer[i++] = this->getTypefaceID(reinterpret_cast<SkTypeface*>(setPtr));
921 }
922
923 SkASSERT(i == typefaceSet.count());
924
925 return size;
926}
927
fmalitab7425172014-08-26 07:56:44 -0700928void SkGPipeCanvas::onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
929 const SkPaint& paint) {
fmalita228a6f22014-08-28 13:59:42 -0700930 NOTIFY_SETUP(this);
931 this->writePaint(paint);
932
933 // FIXME: this is inefficient but avoids duplicating the blob serialization logic.
fmalitaacb882c2014-09-16 17:58:34 -0700934 SkRefCntSet typefaceSet;
fmalita228a6f22014-08-28 13:59:42 -0700935 SkWriteBuffer blobBuffer;
fmalitaacb882c2014-09-16 17:58:34 -0700936 blobBuffer.setTypefaceRecorder(&typefaceSet);
fmalita228a6f22014-08-28 13:59:42 -0700937 blob->flatten(blobBuffer);
938
fmalitaacb882c2014-09-16 17:58:34 -0700939 // Unlike most draw ops (which only use one paint/typeface), text blobs may reference
940 // an arbitrary number of typefaces. Since the one-paint-per-op model is not applicable,
941 // we need to serialize these explicitly.
942 TypefaceBuffer typefaceBuffer;
943 size_t typefaceSize = is_cross_process(fFlags)
944 ? this->getCrossProcessTypefaces(typefaceSet, &typefaceBuffer)
945 : this->getInProcessTypefaces(typefaceSet, &typefaceBuffer);
946
947 // blob byte count + typeface count + x + y + blob data + an index (cross-process)
948 // or pointer (in-process) for each typeface
949 size_t size = 2 * sizeof(uint32_t)
950 + 2 * sizeof(SkScalar)
951 + blobBuffer.bytesWritten()
952 + typefaceSize;
953
fmalita228a6f22014-08-28 13:59:42 -0700954 if (this->needOpBytes(size)) {
955 this->writeOp(kDrawTextBlob_DrawOp);
fmalitaacb882c2014-09-16 17:58:34 -0700956 SkDEBUGCODE(size_t initialOffset = fWriter.bytesWritten();)
957
fmalita228a6f22014-08-28 13:59:42 -0700958 fWriter.writeScalar(x);
959 fWriter.writeScalar(y);
fmalitaacb882c2014-09-16 17:58:34 -0700960
961 fWriter.write32(typefaceSet.count());
962 fWriter.write(typefaceBuffer.get(), typefaceSize);
963
fmalita228a6f22014-08-28 13:59:42 -0700964 fWriter.write32(SkToU32(blobBuffer.bytesWritten()));
965 uint32_t* pad = fWriter.reservePad(blobBuffer.bytesWritten());
966 blobBuffer.writeToMemory(pad);
fmalitaacb882c2014-09-16 17:58:34 -0700967
968 SkASSERT(initialOffset + size == fWriter.bytesWritten());
fmalita228a6f22014-08-28 13:59:42 -0700969 }
fmalitab7425172014-08-26 07:56:44 -0700970}
971
reedd5fa1a42014-08-09 11:08:05 -0700972void SkGPipeCanvas::onDrawPicture(const SkPicture* picture, const SkMatrix* matrix,
973 const SkPaint* paint) {
reed@google.com0faac1e2011-05-11 05:58:58 +0000974 // we want to playback the picture into individual draw calls
reedd5fa1a42014-08-09 11:08:05 -0700975 //
976 // todo: do we always have to unroll? If the pipe is not cross-process, seems like
977 // we could just ref the picture and move on...? <reed, scroggo>
978 //
979 this->INHERITED::onDrawPicture(picture, matrix, paint);
reed@google.combb6992a2011-04-26 17:41:56 +0000980}
981
reed41af9662015-01-05 07:49:08 -0800982void SkGPipeCanvas::onDrawVertices(VertexMode vmode, int vertexCount,
983 const SkPoint vertices[], const SkPoint texs[],
984 const SkColor colors[], SkXfermode* xfer,
985 const uint16_t indices[], int indexCount,
986 const SkPaint& paint) {
reed@google.combb6992a2011-04-26 17:41:56 +0000987 if (0 == vertexCount) {
988 return;
989 }
990
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +0000991 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000992 this->writePaint(paint);
commit-bot@chromium.orgd0490172014-05-14 15:12:08 +0000993
994 unsigned flags = 0; // packs with the op, so needs no extra space
995
996 size_t size = 0;
997 size += 4; // vmode
998 size += 4; // vertex count
999 size += vertexCount * sizeof(SkPoint); // vertices
1000
reed@google.combb6992a2011-04-26 17:41:56 +00001001 if (texs) {
1002 flags |= kDrawVertices_HasTexs_DrawOpFlag;
reed@google.comacd471f2011-05-03 21:26:46 +00001003 size += vertexCount * sizeof(SkPoint);
reed@google.combb6992a2011-04-26 17:41:56 +00001004 }
1005 if (colors) {
1006 flags |= kDrawVertices_HasColors_DrawOpFlag;
reed@google.comacd471f2011-05-03 21:26:46 +00001007 size += vertexCount * sizeof(SkColor);
reed@google.combb6992a2011-04-26 17:41:56 +00001008 }
commit-bot@chromium.orgf700fb22014-05-14 14:50:57 +00001009 if (xfer && !SkXfermode::IsMode(xfer, SkXfermode::kModulate_Mode)) {
1010 flags |= kDrawVertices_HasXfermode_DrawOpFlag;
commit-bot@chromium.orgd0490172014-05-14 15:12:08 +00001011 size += sizeof(int32_t); // SkXfermode::Mode
1012 }
1013 if (indices && indexCount > 0) {
1014 flags |= kDrawVertices_HasIndices_DrawOpFlag;
1015 size += 4; // index count
1016 size += SkAlign4(indexCount * sizeof(uint16_t)); // indices
reed@google.com85e143c2013-12-30 15:51:25 +00001017 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001018
reed@google.comacd471f2011-05-03 21:26:46 +00001019 if (this->needOpBytes(size)) {
reed@google.com31891582011-05-12 03:03:56 +00001020 this->writeOp(kDrawVertices_DrawOp, flags, 0);
reed@google.com85e143c2013-12-30 15:51:25 +00001021 fWriter.write32(vmode);
reed@google.comacd471f2011-05-03 21:26:46 +00001022 fWriter.write32(vertexCount);
1023 fWriter.write(vertices, vertexCount * sizeof(SkPoint));
commit-bot@chromium.orgd0490172014-05-14 15:12:08 +00001024 if (flags & kDrawVertices_HasTexs_DrawOpFlag) {
reed@google.comacd471f2011-05-03 21:26:46 +00001025 fWriter.write(texs, vertexCount * sizeof(SkPoint));
1026 }
commit-bot@chromium.orgd0490172014-05-14 15:12:08 +00001027 if (flags & kDrawVertices_HasColors_DrawOpFlag) {
reed@google.comacd471f2011-05-03 21:26:46 +00001028 fWriter.write(colors, vertexCount * sizeof(SkColor));
1029 }
reed@google.com85e143c2013-12-30 15:51:25 +00001030 if (flags & kDrawVertices_HasXfermode_DrawOpFlag) {
1031 SkXfermode::Mode mode = SkXfermode::kModulate_Mode;
commit-bot@chromium.orgd0490172014-05-14 15:12:08 +00001032 SkAssertResult(xfer->asMode(&mode));
reed@google.com85e143c2013-12-30 15:51:25 +00001033 fWriter.write32(mode);
1034 }
commit-bot@chromium.orgd0490172014-05-14 15:12:08 +00001035 if (flags & kDrawVertices_HasIndices_DrawOpFlag) {
reed@google.comacd471f2011-05-03 21:26:46 +00001036 fWriter.write32(indexCount);
1037 fWriter.writePad(indices, indexCount * sizeof(uint16_t));
1038 }
reed@google.combb6992a2011-04-26 17:41:56 +00001039 }
1040}
1041
dandovb3c9d1c2014-08-12 08:34:29 -07001042void SkGPipeCanvas::onDrawPatch(const SkPoint cubics[12], const SkColor colors[4],
1043 const SkPoint texCoords[4], SkXfermode* xmode,
1044 const SkPaint& paint) {
dandov963137b2014-08-07 07:49:53 -07001045 NOTIFY_SETUP(this);
piotaixr2bf8d092014-11-04 13:21:41 -08001046
dandovb3c9d1c2014-08-12 08:34:29 -07001047 size_t size = SkPatchUtils::kNumCtrlPts * sizeof(SkPoint);
1048 unsigned flags = 0;
bsalomon49f085d2014-09-05 13:34:00 -07001049 if (colors) {
dandovb3c9d1c2014-08-12 08:34:29 -07001050 flags |= kDrawVertices_HasColors_DrawOpFlag;
1051 size += SkPatchUtils::kNumCorners * sizeof(SkColor);
1052 }
bsalomon49f085d2014-09-05 13:34:00 -07001053 if (texCoords) {
dandovb3c9d1c2014-08-12 08:34:29 -07001054 flags |= kDrawVertices_HasTexs_DrawOpFlag;
1055 size += SkPatchUtils::kNumCorners * sizeof(SkPoint);
1056 }
bsalomon49f085d2014-09-05 13:34:00 -07001057 if (xmode) {
dandovb3c9d1c2014-08-12 08:34:29 -07001058 SkXfermode::Mode mode;
1059 if (xmode->asMode(&mode) && SkXfermode::kModulate_Mode != mode) {
1060 flags |= kDrawVertices_HasXfermode_DrawOpFlag;
1061 size += sizeof(int32_t);
1062 }
1063 }
piotaixr2bf8d092014-11-04 13:21:41 -08001064
dandov963137b2014-08-07 07:49:53 -07001065 this->writePaint(paint);
dandovb3c9d1c2014-08-12 08:34:29 -07001066 if (this->needOpBytes(size)) {
1067 this->writeOp(kDrawPatch_DrawOp, flags, 0);
piotaixr2bf8d092014-11-04 13:21:41 -08001068
dandovb3c9d1c2014-08-12 08:34:29 -07001069 fWriter.write(cubics, SkPatchUtils::kNumCtrlPts * sizeof(SkPoint));
piotaixr2bf8d092014-11-04 13:21:41 -08001070
bsalomon49f085d2014-09-05 13:34:00 -07001071 if (colors) {
dandovb3c9d1c2014-08-12 08:34:29 -07001072 fWriter.write(colors, SkPatchUtils::kNumCorners * sizeof(SkColor));
1073 }
piotaixr2bf8d092014-11-04 13:21:41 -08001074
bsalomon49f085d2014-09-05 13:34:00 -07001075 if (texCoords) {
dandovb3c9d1c2014-08-12 08:34:29 -07001076 fWriter.write(texCoords, SkPatchUtils::kNumCorners * sizeof(SkPoint));
1077 }
piotaixr2bf8d092014-11-04 13:21:41 -08001078
dandovb3c9d1c2014-08-12 08:34:29 -07001079 if (flags & kDrawVertices_HasXfermode_DrawOpFlag) {
1080 SkXfermode::Mode mode = SkXfermode::kModulate_Mode;
1081 SkAssertResult(xmode->asMode(&mode));
1082 fWriter.write32(mode);
1083 }
dandov963137b2014-08-07 07:49:53 -07001084 }
1085}
1086
robertphillips@google.com0a4805e2013-05-29 13:24:23 +00001087void SkGPipeCanvas::beginCommentGroup(const char* description) {
1088 // ignore for now
1089}
1090
1091void SkGPipeCanvas::addComment(const char* kywd, const char* value) {
1092 // ignore for now
1093}
1094
1095void SkGPipeCanvas::endCommentGroup() {
1096 // ignore for now
1097}
1098
junov@chromium.org77eec242012-07-18 17:54:45 +00001099void SkGPipeCanvas::flushRecording(bool detachCurrentBlock) {
commit-bot@chromium.orgd0490172014-05-14 15:12:08 +00001100 this->doNotify();
junov@chromium.org77eec242012-07-18 17:54:45 +00001101 if (detachCurrentBlock) {
1102 // force a new block to be requested for the next recorded command
rmistry@google.comd6176b02012-08-23 18:14:13 +00001103 fBlockSize = 0;
junov@chromium.org77eec242012-07-18 17:54:45 +00001104 }
1105}
1106
junov@chromium.org2e14ba82012-08-07 14:26:57 +00001107size_t SkGPipeCanvas::freeMemoryIfPossible(size_t bytesToFree) {
scroggo@google.comd5d158b2012-08-14 20:38:28 +00001108 return (NULL == fBitmapHeap) ? 0 : fBitmapHeap->freeMemoryIfPossible(bytesToFree);
junov@chromium.org2e14ba82012-08-07 14:26:57 +00001109}
1110
reed@google.combb6992a2011-04-26 17:41:56 +00001111///////////////////////////////////////////////////////////////////////////////
1112
1113template <typename T> uint32_t castToU32(T value) {
1114 union {
1115 T fSrc;
1116 uint32_t fDst;
1117 } data;
1118 data.fSrc = value;
1119 return data.fDst;
1120}
1121
reed@google.com31891582011-05-12 03:03:56 +00001122void SkGPipeCanvas::writePaint(const SkPaint& paint) {
scroggo@google.comd5d158b2012-08-14 20:38:28 +00001123 if (fDone) {
1124 return;
1125 }
reed@google.com31891582011-05-12 03:03:56 +00001126 SkPaint& base = fPaint;
reed@google.combb6992a2011-04-26 17:41:56 +00001127 uint32_t storage[32];
1128 uint32_t* ptr = storage;
reed@google.combb6992a2011-04-26 17:41:56 +00001129
1130 if (base.getFlags() != paint.getFlags()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001131 *ptr++ = PaintOp_packOpData(kFlags_PaintOp, paint.getFlags());
reed@google.comf5842f72011-05-04 18:30:04 +00001132 base.setFlags(paint.getFlags());
reed@google.combb6992a2011-04-26 17:41:56 +00001133 }
1134 if (base.getColor() != paint.getColor()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001135 *ptr++ = PaintOp_packOp(kColor_PaintOp);
1136 *ptr++ = paint.getColor();
reed@google.comf5842f72011-05-04 18:30:04 +00001137 base.setColor(paint.getColor());
reed@google.combb6992a2011-04-26 17:41:56 +00001138 }
reed93a12152015-03-16 10:08:34 -07001139 if (base.getFilterQuality() != paint.getFilterQuality()) {
1140 *ptr++ = PaintOp_packOpData(kFilterLevel_PaintOp, paint.getFilterQuality());
1141 base.setFilterQuality(paint.getFilterQuality());
commit-bot@chromium.org85faf502014-04-16 12:58:02 +00001142 }
reed@google.combb6992a2011-04-26 17:41:56 +00001143 if (base.getStyle() != paint.getStyle()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001144 *ptr++ = PaintOp_packOpData(kStyle_PaintOp, paint.getStyle());
reed@google.comf5842f72011-05-04 18:30:04 +00001145 base.setStyle(paint.getStyle());
reed@google.combb6992a2011-04-26 17:41:56 +00001146 }
1147 if (base.getStrokeJoin() != paint.getStrokeJoin()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001148 *ptr++ = PaintOp_packOpData(kJoin_PaintOp, paint.getStrokeJoin());
reed@google.comf5842f72011-05-04 18:30:04 +00001149 base.setStrokeJoin(paint.getStrokeJoin());
reed@google.combb6992a2011-04-26 17:41:56 +00001150 }
1151 if (base.getStrokeCap() != paint.getStrokeCap()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001152 *ptr++ = PaintOp_packOpData(kCap_PaintOp, paint.getStrokeCap());
reed@google.comf5842f72011-05-04 18:30:04 +00001153 base.setStrokeCap(paint.getStrokeCap());
reed@google.combb6992a2011-04-26 17:41:56 +00001154 }
1155 if (base.getStrokeWidth() != paint.getStrokeWidth()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001156 *ptr++ = PaintOp_packOp(kWidth_PaintOp);
1157 *ptr++ = castToU32(paint.getStrokeWidth());
reed@google.comf5842f72011-05-04 18:30:04 +00001158 base.setStrokeWidth(paint.getStrokeWidth());
reed@google.combb6992a2011-04-26 17:41:56 +00001159 }
1160 if (base.getStrokeMiter() != paint.getStrokeMiter()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001161 *ptr++ = PaintOp_packOp(kMiter_PaintOp);
1162 *ptr++ = castToU32(paint.getStrokeMiter());
reed@google.comf5842f72011-05-04 18:30:04 +00001163 base.setStrokeMiter(paint.getStrokeMiter());
reed@google.combb6992a2011-04-26 17:41:56 +00001164 }
1165 if (base.getTextEncoding() != paint.getTextEncoding()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001166 *ptr++ = PaintOp_packOpData(kEncoding_PaintOp, paint.getTextEncoding());
reed@google.comf5842f72011-05-04 18:30:04 +00001167 base.setTextEncoding(paint.getTextEncoding());
reed@google.combb6992a2011-04-26 17:41:56 +00001168 }
1169 if (base.getHinting() != paint.getHinting()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001170 *ptr++ = PaintOp_packOpData(kHinting_PaintOp, paint.getHinting());
reed@google.comf5842f72011-05-04 18:30:04 +00001171 base.setHinting(paint.getHinting());
reed@google.combb6992a2011-04-26 17:41:56 +00001172 }
1173 if (base.getTextAlign() != paint.getTextAlign()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001174 *ptr++ = PaintOp_packOpData(kAlign_PaintOp, paint.getTextAlign());
reed@google.comf5842f72011-05-04 18:30:04 +00001175 base.setTextAlign(paint.getTextAlign());
reed@google.combb6992a2011-04-26 17:41:56 +00001176 }
1177 if (base.getTextSize() != paint.getTextSize()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001178 *ptr++ = PaintOp_packOp(kTextSize_PaintOp);
1179 *ptr++ = castToU32(paint.getTextSize());
reed@google.comf5842f72011-05-04 18:30:04 +00001180 base.setTextSize(paint.getTextSize());
reed@google.combb6992a2011-04-26 17:41:56 +00001181 }
1182 if (base.getTextScaleX() != paint.getTextScaleX()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001183 *ptr++ = PaintOp_packOp(kTextScaleX_PaintOp);
1184 *ptr++ = castToU32(paint.getTextScaleX());
reed@google.comf5842f72011-05-04 18:30:04 +00001185 base.setTextScaleX(paint.getTextScaleX());
reed@google.combb6992a2011-04-26 17:41:56 +00001186 }
1187 if (base.getTextSkewX() != paint.getTextSkewX()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001188 *ptr++ = PaintOp_packOp(kTextSkewX_PaintOp);
1189 *ptr++ = castToU32(paint.getTextSkewX());
reed@google.comf5842f72011-05-04 18:30:04 +00001190 base.setTextSkewX(paint.getTextSkewX());
1191 }
1192
1193 if (!SkTypeface::Equal(base.getTypeface(), paint.getTypeface())) {
fmalitaacb882c2014-09-16 17:58:34 -07001194 if (is_cross_process(fFlags)) {
scroggo@google.com3cb969f2012-07-27 20:39:19 +00001195 uint32_t id = this->getTypefaceID(paint.getTypeface());
1196 *ptr++ = PaintOp_packOpData(kTypeface_PaintOp, id);
1197 } else if (this->needOpBytes(sizeof(void*))) {
1198 // Add to the set for ref counting.
1199 fTypefaceSet.add(paint.getTypeface());
1200 // It is safe to write the typeface to the stream before the rest
1201 // of the paint unless we ever send a kReset_PaintOp, which we
1202 // currently never do.
1203 this->writeOp(kSetTypeface_DrawOp);
1204 fWriter.writePtr(paint.getTypeface());
1205 }
reed@google.comf5842f72011-05-04 18:30:04 +00001206 base.setTypeface(paint.getTypeface());
reed@google.combb6992a2011-04-26 17:41:56 +00001207 }
reed@google.combb6992a2011-04-26 17:41:56 +00001208
scroggo@google.com4dffc592012-07-17 16:49:40 +00001209 // This is a new paint, so all old flats can be safely purged, if necessary.
1210 fFlattenableHeap.markAllFlatsSafeToDelete();
reed@google.comb55d1182011-05-11 00:42:04 +00001211 for (int i = 0; i < kCount_PaintFlats; i++) {
1212 int index = this->flattenToIndex(get_paintflat(paint, i), (PaintFlats)i);
scroggo@google.comd3ba5cc2012-07-09 16:05:53 +00001213 bool replaced = index < 0;
1214 if (replaced) {
1215 index = ~index;
1216 }
scroggo@google.com4dffc592012-07-17 16:49:40 +00001217 // Store the index of any flat that needs to be kept. 0 means no flat.
1218 if (index > 0) {
1219 fFlattenableHeap.markFlatForKeeping(index);
1220 }
1221 SkASSERT(index >= 0 && index <= fFlatDictionary.count());
scroggo@google.comd3ba5cc2012-07-09 16:05:53 +00001222 if (index != fCurrFlatIndex[i] || replaced) {
reed@google.comb55d1182011-05-11 00:42:04 +00001223 *ptr++ = PaintOp_packOpFlagData(kFlatIndex_PaintOp, i, index);
1224 fCurrFlatIndex[i] = index;
1225 }
1226 }
1227
reed@google.comacd471f2011-05-03 21:26:46 +00001228 size_t size = (char*)ptr - (char*)storage;
1229 if (size && this->needOpBytes(size)) {
commit-bot@chromium.orgb45bd1f2014-04-24 18:17:30 +00001230 this->writeOp(kPaintOp_DrawOp, 0, SkToU32(size));
reed@google.comb55d1182011-05-11 00:42:04 +00001231 fWriter.write(storage, size);
reed@google.combb6992a2011-04-26 17:41:56 +00001232 for (size_t i = 0; i < size/4; i++) {
reed@google.comb55d1182011-05-11 00:42:04 +00001233// SkDebugf("[%d] %08X\n", i, storage[i]);
reed@google.combb6992a2011-04-26 17:41:56 +00001234 }
1235 }
reed@google.com0cd2ac62013-10-14 20:02:44 +00001236
1237 //
1238 // Do these after we've written kPaintOp_DrawOp
skia.committer@gmail.comfbc58a32013-10-15 07:02:27 +00001239
reed@google.com0cd2ac62013-10-14 20:02:44 +00001240 if (base.getAnnotation() != paint.getAnnotation()) {
1241 if (NULL == paint.getAnnotation()) {
commit-bot@chromium.org89ff3dd2013-10-29 20:29:38 +00001242 if (this->needOpBytes()) {
1243 this->writeOp(kSetAnnotation_DrawOp, 0, 0);
1244 }
reed@google.com0cd2ac62013-10-14 20:02:44 +00001245 } else {
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +00001246 SkWriteBuffer buffer;
reed@google.com0cd2ac62013-10-14 20:02:44 +00001247 paint.getAnnotation()->writeToBuffer(buffer);
commit-bot@chromium.org89ff3dd2013-10-29 20:29:38 +00001248 const size_t size = buffer.bytesWritten();
1249 if (this->needOpBytes(size)) {
commit-bot@chromium.orgb45bd1f2014-04-24 18:17:30 +00001250 this->writeOp(kSetAnnotation_DrawOp, 0, SkToU32(size));
commit-bot@chromium.org89ff3dd2013-10-29 20:29:38 +00001251 buffer.writeToMemory(fWriter.reserve(size));
1252 }
reed@google.com0cd2ac62013-10-14 20:02:44 +00001253 }
commit-bot@chromium.org40258a52013-10-29 19:23:26 +00001254 base.setAnnotation(paint.getAnnotation());
reed@google.com0cd2ac62013-10-14 20:02:44 +00001255 }
reed@google.combb6992a2011-04-26 17:41:56 +00001256}
1257
1258///////////////////////////////////////////////////////////////////////////////
1259
1260#include "SkGPipe.h"
1261
scroggo@google.com3cb969f2012-07-27 20:39:19 +00001262SkGPipeController::~SkGPipeController() {
1263 SkSafeUnref(fCanvas);
1264}
1265
1266void SkGPipeController::setCanvas(SkGPipeCanvas* canvas) {
1267 SkRefCnt_SafeAssign(fCanvas, canvas);
1268}
1269
1270///////////////////////////////////////////////////////////////////////////////
1271
1272SkGPipeWriter::SkGPipeWriter()
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +00001273: fWriter(0) {
reed@google.combb6992a2011-04-26 17:41:56 +00001274 fCanvas = NULL;
1275}
1276
1277SkGPipeWriter::~SkGPipeWriter() {
reed@google.comacd471f2011-05-03 21:26:46 +00001278 this->endRecording();
reed@google.combb6992a2011-04-26 17:41:56 +00001279}
1280
junov@chromium.orga8db8fe2012-08-15 19:49:22 +00001281SkCanvas* SkGPipeWriter::startRecording(SkGPipeController* controller, uint32_t flags,
1282 uint32_t width, uint32_t height) {
reed@google.combb6992a2011-04-26 17:41:56 +00001283 if (NULL == fCanvas) {
reed@google.comacd471f2011-05-03 21:26:46 +00001284 fWriter.reset(NULL, 0);
junov@chromium.orga8db8fe2012-08-15 19:49:22 +00001285 fCanvas = SkNEW_ARGS(SkGPipeCanvas, (controller, &fWriter, flags, width, height));
reed@google.combb6992a2011-04-26 17:41:56 +00001286 }
scroggo@google.com3cb969f2012-07-27 20:39:19 +00001287 controller->setCanvas(fCanvas);
reed@google.combb6992a2011-04-26 17:41:56 +00001288 return fCanvas;
1289}
1290
1291void SkGPipeWriter::endRecording() {
1292 if (fCanvas) {
scroggo@google.com59c3ab62013-11-12 14:32:38 +00001293 fCanvas->finish(true);
reed@google.combb6992a2011-04-26 17:41:56 +00001294 fCanvas->unref();
1295 fCanvas = NULL;
1296 }
1297}
1298
junov@chromium.org2e14ba82012-08-07 14:26:57 +00001299void SkGPipeWriter::flushRecording(bool detachCurrentBlock) {
1300 if (fCanvas) {
1301 fCanvas->flushRecording(detachCurrentBlock);
1302 }
junov@chromium.org77eec242012-07-18 17:54:45 +00001303}
1304
junov@chromium.org2e14ba82012-08-07 14:26:57 +00001305size_t SkGPipeWriter::freeMemoryIfPossible(size_t bytesToFree) {
1306 if (fCanvas) {
1307 return fCanvas->freeMemoryIfPossible(bytesToFree);
1308 }
1309 return 0;
1310}
1311
1312size_t SkGPipeWriter::storageAllocatedForRecording() const {
scroggo@google.com15011ee2012-07-26 20:03:32 +00001313 return NULL == fCanvas ? 0 : fCanvas->storageAllocatedForRecording();
1314}
1315
scroggo@google.com3e26bd02012-08-14 15:20:01 +00001316///////////////////////////////////////////////////////////////////////////////
1317
1318BitmapShuttle::BitmapShuttle(SkGPipeCanvas* canvas) {
1319 SkASSERT(canvas != NULL);
1320 fCanvas = canvas;
1321 fCanvas->ref();
1322}
1323
1324BitmapShuttle::~BitmapShuttle() {
scroggo@google.com59c3ab62013-11-12 14:32:38 +00001325 this->removeCanvas();
scroggo@google.com3e26bd02012-08-14 15:20:01 +00001326}
1327
1328bool BitmapShuttle::insert(const SkBitmap& bitmap, int32_t slot) {
scroggo@google.com59c3ab62013-11-12 14:32:38 +00001329 SkASSERT(fCanvas != NULL);
scroggo@google.com3e26bd02012-08-14 15:20:01 +00001330 return fCanvas->shuttleBitmap(bitmap, slot);
1331}
scroggo@google.com59c3ab62013-11-12 14:32:38 +00001332
1333void BitmapShuttle::removeCanvas() {
1334 if (NULL == fCanvas) {
1335 return;
1336 }
1337 fCanvas->unref();
1338 fCanvas = NULL;
1339}