blob: cfd1e7ceef5526c2332392b5173f14b71250a18e [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
scroggo@google.com10dccde2012-08-08 20:43:22 +00009#include "SkBitmapHeap.h"
reed@google.combb6992a2011-04-26 17:41:56 +000010#include "SkCanvas.h"
scroggo@google.com10dccde2012-08-08 20:43:22 +000011#include "SkColorFilter.h"
reed@google.com8a85d0c2011-06-24 19:12:12 +000012#include "SkData.h"
scroggo@google.com10dccde2012-08-08 20:43:22 +000013#include "SkDrawLooper.h"
reed@google.combb6793b2011-05-05 15:18:15 +000014#include "SkDevice.h"
reed@google.comacd471f2011-05-03 21:26:46 +000015#include "SkGPipe.h"
reed@google.combb6992a2011-04-26 17:41:56 +000016#include "SkGPipePriv.h"
scroggo@google.com16d1d0b2012-05-02 19:09:40 +000017#include "SkImageFilter.h"
scroggo@google.com10dccde2012-08-08 20:43:22 +000018#include "SkMaskFilter.h"
19#include "SkOrderedWriteBuffer.h"
20#include "SkPaint.h"
21#include "SkPathEffect.h"
22#include "SkPictureFlat.h"
23#include "SkRasterizer.h"
reed@google.com4ed0fb72012-12-12 20:48:18 +000024#include "SkRRect.h"
scroggo@google.com10dccde2012-08-08 20:43:22 +000025#include "SkShader.h"
reed@google.comf5842f72011-05-04 18:30:04 +000026#include "SkStream.h"
reed@google.comb55d1182011-05-11 00:42:04 +000027#include "SkTSearch.h"
reed@google.comf5842f72011-05-04 18:30:04 +000028#include "SkTypeface.h"
reed@google.combb6992a2011-04-26 17:41:56 +000029#include "SkWriter32.h"
reed@google.comb55d1182011-05-11 00:42:04 +000030
reed@google.com4ed0fb72012-12-12 20:48:18 +000031enum {
32 kSizeOfFlatRRect = sizeof(SkRect) + 4 * sizeof(SkVector)
33};
34
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +000035static bool isCrossProcess(uint32_t flags) {
36 return SkToBool(flags & SkGPipeWriter::kCrossProcess_Flag);
37}
38
reed@google.comb55d1182011-05-11 00:42:04 +000039static SkFlattenable* get_paintflat(const SkPaint& paint, unsigned paintFlat) {
40 SkASSERT(paintFlat < kCount_PaintFlats);
41 switch (paintFlat) {
42 case kColorFilter_PaintFlat: return paint.getColorFilter();
reed@google.com0faac1e2011-05-11 05:58:58 +000043 case kDrawLooper_PaintFlat: return paint.getLooper();
reed@google.comb55d1182011-05-11 00:42:04 +000044 case kMaskFilter_PaintFlat: return paint.getMaskFilter();
45 case kPathEffect_PaintFlat: return paint.getPathEffect();
46 case kRasterizer_PaintFlat: return paint.getRasterizer();
47 case kShader_PaintFlat: return paint.getShader();
scroggo@google.com16d1d0b2012-05-02 19:09:40 +000048 case kImageFilter_PaintFlat: return paint.getImageFilter();
reed@google.comb55d1182011-05-11 00:42:04 +000049 case kXfermode_PaintFlat: return paint.getXfermode();
50 }
tomhudson@google.com0c00f212011-12-28 14:59:50 +000051 SkDEBUGFAIL("never gets here");
reed@google.comb55d1182011-05-11 00:42:04 +000052 return NULL;
53}
reed@google.combb6992a2011-04-26 17:41:56 +000054
reed@google.comf5842f72011-05-04 18:30:04 +000055static size_t writeTypeface(SkWriter32* writer, SkTypeface* typeface) {
56 SkASSERT(typeface);
57 SkDynamicMemoryWStream stream;
58 typeface->serialize(&stream);
59 size_t size = stream.getOffset();
60 if (writer) {
61 writer->write32(size);
reed@google.com8a85d0c2011-06-24 19:12:12 +000062 SkAutoDataUnref data(stream.copyToData());
robertphillips@google.com59f46b82012-07-10 17:30:58 +000063 writer->writePad(data->data(), size);
reed@google.comf5842f72011-05-04 18:30:04 +000064 }
scroggo@google.com5af9b202012-06-04 17:17:36 +000065 return 4 + SkAlign4(size);
reed@google.comf5842f72011-05-04 18:30:04 +000066}
67
reed@google.combb6992a2011-04-26 17:41:56 +000068///////////////////////////////////////////////////////////////////////////////
69
scroggo@google.com4dffc592012-07-17 16:49:40 +000070class FlattenableHeap : public SkFlatController {
71public:
scroggo@google.com664fab12012-08-14 19:22:05 +000072 FlattenableHeap(int numFlatsToKeep, SkNamedFactorySet* fset, bool isCrossProcess)
scroggo@google.com15543602012-08-02 18:49:49 +000073 : fNumFlatsToKeep(numFlatsToKeep) {
scroggo@google.com664fab12012-08-14 19:22:05 +000074 SkASSERT((isCrossProcess && fset != NULL) || (!isCrossProcess && NULL == fset));
75 if (isCrossProcess) {
76 this->setNamedFactorySet(fset);
77 this->setWriteBufferFlags(SkFlattenableWriteBuffer::kCrossProcess_Flag);
78 }
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +000079 }
scroggo@google.com4dffc592012-07-17 16:49:40 +000080
81 ~FlattenableHeap() {
82 fPointers.freeAll();
83 }
84
85 virtual void* allocThrow(size_t bytes) SK_OVERRIDE;
86
87 virtual void unalloc(void* ptr) SK_OVERRIDE;
88
scroggo@google.com7ca24432012-08-14 15:48:43 +000089 void setBitmapStorage(SkBitmapHeap* heap) {
90 this->setBitmapHeap(heap);
91 }
rmistry@google.comd6176b02012-08-23 18:14:13 +000092
scroggo@google.com4dffc592012-07-17 16:49:40 +000093 const SkFlatData* flatToReplace() const;
94
95 // Mark an SkFlatData as one that should not be returned by flatToReplace.
96 // Takes the result of SkFlatData::index() as its parameter.
97 void markFlatForKeeping(int index) {
98 *fFlatsThatMustBeKept.append() = index;
99 }
100
101 void markAllFlatsSafeToDelete() {
102 fFlatsThatMustBeKept.reset();
103 }
104
105private:
106 // Keep track of the indices (i.e. the result of SkFlatData::index()) of
107 // flats that must be kept, since they are on the current paint.
108 SkTDArray<int> fFlatsThatMustBeKept;
109 SkTDArray<void*> fPointers;
110 const int fNumFlatsToKeep;
111};
112
113void FlattenableHeap::unalloc(void* ptr) {
114 int indexToRemove = fPointers.rfind(ptr);
115 if (indexToRemove >= 0) {
116 sk_free(ptr);
117 fPointers.remove(indexToRemove);
118 }
119}
120
121void* FlattenableHeap::allocThrow(size_t bytes) {
122 void* ptr = sk_malloc_throw(bytes);
123 *fPointers.append() = ptr;
124 return ptr;
125}
126
127const SkFlatData* FlattenableHeap::flatToReplace() const {
128 // First, determine whether we should replace one.
129 if (fPointers.count() > fNumFlatsToKeep) {
130 // Look through the flattenable heap.
131 // TODO: Return the LRU flat.
132 for (int i = 0; i < fPointers.count(); i++) {
133 SkFlatData* potential = (SkFlatData*)fPointers[i];
134 // Make sure that it is not one that must be kept.
135 bool mustKeep = false;
136 for (int j = 0; j < fFlatsThatMustBeKept.count(); j++) {
137 if (potential->index() == fFlatsThatMustBeKept[j]) {
138 mustKeep = true;
139 break;
140 }
141 }
142 if (!mustKeep) {
143 return potential;
144 }
145 }
146 }
147 return NULL;
148}
149
150///////////////////////////////////////////////////////////////////////////////
151
152class FlatDictionary : public SkFlatDictionary<SkFlattenable> {
153public:
scroggo@google.com15543602012-08-02 18:49:49 +0000154 FlatDictionary(FlattenableHeap* heap)
155 : SkFlatDictionary<SkFlattenable>(heap) {
scroggo@google.com4dffc592012-07-17 16:49:40 +0000156 fFlattenProc = &flattenFlattenableProc;
157 // No need to define fUnflattenProc since the writer will never
158 // unflatten the data.
159 }
160 static void flattenFlattenableProc(SkOrderedWriteBuffer& buffer,
161 const void* obj) {
162 buffer.writeFlattenable((SkFlattenable*)obj);
163 }
164
165};
166
167///////////////////////////////////////////////////////////////////////////////
168
reed@google.combb6992a2011-04-26 17:41:56 +0000169class SkGPipeCanvas : public SkCanvas {
170public:
junov@chromium.orga8db8fe2012-08-15 19:49:22 +0000171 SkGPipeCanvas(SkGPipeController*, SkWriter32*, uint32_t flags,
172 uint32_t width, uint32_t height);
reed@google.combb6992a2011-04-26 17:41:56 +0000173 virtual ~SkGPipeCanvas();
174
175 void finish() {
176 if (!fDone) {
reed@google.comdbccc882011-07-08 18:53:39 +0000177 if (this->needOpBytes()) {
178 this->writeOp(kDone_DrawOp);
179 this->doNotify();
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000180 if (shouldFlattenBitmaps(fFlags)) {
scroggo@google.comd9d29672012-08-14 17:21:34 +0000181 // In this case, a BitmapShuttle is reffed by the SkBitmapHeap
182 // and refs this canvas. Unref the SkBitmapHeap to end the
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000183 // circular reference. When shouldFlattenBitmaps is false,
scroggo@google.comd9d29672012-08-14 17:21:34 +0000184 // there is no circular reference, so the SkBitmapHeap can be
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000185 // safely unreffed in the destructor.
scroggo@google.comd9d29672012-08-14 17:21:34 +0000186 fBitmapHeap->unref();
scroggo@google.com7ca24432012-08-14 15:48:43 +0000187 // This eliminates a similar circular reference (Canvas owns
scroggo@google.comd9d29672012-08-14 17:21:34 +0000188 // the FlattenableHeap which holds a ref to the SkBitmapHeap).
scroggo@google.com7ca24432012-08-14 15:48:43 +0000189 fFlattenableHeap.setBitmapStorage(NULL);
scroggo@google.comd9d29672012-08-14 17:21:34 +0000190 fBitmapHeap = NULL;
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000191 }
reed@google.comdbccc882011-07-08 18:53:39 +0000192 }
reed@google.combb6992a2011-04-26 17:41:56 +0000193 fDone = true;
194 }
195 }
196
junov@chromium.org77eec242012-07-18 17:54:45 +0000197 void flushRecording(bool detachCurrentBlock);
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000198 size_t freeMemoryIfPossible(size_t bytesToFree);
junov@chromium.org77eec242012-07-18 17:54:45 +0000199
scroggo@google.com15011ee2012-07-26 20:03:32 +0000200 size_t storageAllocatedForRecording() {
scroggo@google.comd5d158b2012-08-14 20:38:28 +0000201 return (NULL == fBitmapHeap) ? 0 : fBitmapHeap->bytesAllocated();
scroggo@google.com15011ee2012-07-26 20:03:32 +0000202 }
203
reed@google.combb6992a2011-04-26 17:41:56 +0000204 // overrides from SkCanvas
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000205 virtual int save(SaveFlags) SK_OVERRIDE;
206 virtual int saveLayer(const SkRect* bounds, const SkPaint*,
207 SaveFlags) SK_OVERRIDE;
208 virtual void restore() SK_OVERRIDE;
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000209 virtual bool isDrawingToLayer() const SK_OVERRIDE;
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000210 virtual bool translate(SkScalar dx, SkScalar dy) SK_OVERRIDE;
211 virtual bool scale(SkScalar sx, SkScalar sy) SK_OVERRIDE;
212 virtual bool rotate(SkScalar degrees) SK_OVERRIDE;
213 virtual bool skew(SkScalar sx, SkScalar sy) SK_OVERRIDE;
214 virtual bool concat(const SkMatrix& matrix) SK_OVERRIDE;
215 virtual void setMatrix(const SkMatrix& matrix) SK_OVERRIDE;
reed@google.com4ed0fb72012-12-12 20:48:18 +0000216 virtual bool clipRect(const SkRect&, SkRegion::Op op, bool doAntiAlias = false) SK_OVERRIDE;
217 virtual bool clipRRect(const SkRRect&, SkRegion::Op op, bool doAntiAlias = false) SK_OVERRIDE;
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000218 virtual bool clipPath(const SkPath& path, SkRegion::Op op,
219 bool doAntiAlias = false) SK_OVERRIDE;
220 virtual bool clipRegion(const SkRegion& region, SkRegion::Op op) SK_OVERRIDE;
221 virtual void clear(SkColor) SK_OVERRIDE;
222 virtual void drawPaint(const SkPaint& paint) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000223 virtual void drawPoints(PointMode, size_t count, const SkPoint pts[],
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000224 const SkPaint&) SK_OVERRIDE;
reed@google.com4ed0fb72012-12-12 20:48:18 +0000225 virtual void drawOval(const SkRect&, const SkPaint&) SK_OVERRIDE;
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000226 virtual void drawRect(const SkRect& rect, const SkPaint&) SK_OVERRIDE;
reed@google.com4ed0fb72012-12-12 20:48:18 +0000227 virtual void drawRRect(const SkRRect&, const SkPaint&) SK_OVERRIDE;
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000228 virtual void drawPath(const SkPath& path, const SkPaint&) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000229 virtual void drawBitmap(const SkBitmap&, SkScalar left, SkScalar top,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000230 const SkPaint*) SK_OVERRIDE;
reed@google.com71121732012-09-18 15:14:33 +0000231 virtual void drawBitmapRectToRect(const SkBitmap&, const SkRect* src,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000232 const SkRect& dst, const SkPaint*) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000233 virtual void drawBitmapMatrix(const SkBitmap&, const SkMatrix&,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000234 const SkPaint*) SK_OVERRIDE;
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000235 virtual void drawBitmapNine(const SkBitmap& bitmap, const SkIRect& center,
236 const SkRect& dst, const SkPaint* paint = NULL) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000237 virtual void drawSprite(const SkBitmap&, int left, int top,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000238 const SkPaint*) SK_OVERRIDE;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000239 virtual void drawText(const void* text, size_t byteLength, SkScalar x,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000240 SkScalar y, const SkPaint&) SK_OVERRIDE;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000241 virtual void drawPosText(const void* text, size_t byteLength,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000242 const SkPoint pos[], const SkPaint&) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000243 virtual void drawPosTextH(const void* text, size_t byteLength,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000244 const SkScalar xpos[], SkScalar constY,
245 const SkPaint&) SK_OVERRIDE;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000246 virtual void drawTextOnPath(const void* text, size_t byteLength,
247 const SkPath& path, const SkMatrix* matrix,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000248 const SkPaint&) SK_OVERRIDE;
249 virtual void drawPicture(SkPicture& picture) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000250 virtual void drawVertices(VertexMode, int vertexCount,
251 const SkPoint vertices[], const SkPoint texs[],
252 const SkColor colors[], SkXfermode*,
253 const uint16_t indices[], int indexCount,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000254 const SkPaint&) SK_OVERRIDE;
255 virtual void drawData(const void*, size_t) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000256
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000257 /**
258 * Flatten an SkBitmap to send to the reader, where it will be referenced
259 * according to slot.
260 */
261 bool shuttleBitmap(const SkBitmap&, int32_t slot);
reed@google.combb6992a2011-04-26 17:41:56 +0000262private:
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000263 enum {
264 kNoSaveLayer = -1,
265 };
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000266 SkNamedFactorySet* fFactorySet;
267 int fFirstSaveLayerStackLevel;
scroggo@google.comd9d29672012-08-14 17:21:34 +0000268 SkBitmapHeap* fBitmapHeap;
reed@google.comacd471f2011-05-03 21:26:46 +0000269 SkGPipeController* fController;
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000270 SkWriter32& fWriter;
271 size_t fBlockSize; // amount allocated for writer
272 size_t fBytesNotified;
273 bool fDone;
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000274 const uint32_t fFlags;
reed@google.combb6992a2011-04-26 17:41:56 +0000275
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000276 SkRefCntSet fTypefaceSet;
reed@google.comf5842f72011-05-04 18:30:04 +0000277
278 uint32_t getTypefaceID(SkTypeface*);
279
reed@google.comacd471f2011-05-03 21:26:46 +0000280 inline void writeOp(DrawOps op, unsigned flags, unsigned data) {
reed@google.combb6992a2011-04-26 17:41:56 +0000281 fWriter.write32(DrawOp_packOpFlagData(op, flags, data));
282 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000283
reed@google.comacd471f2011-05-03 21:26:46 +0000284 inline void writeOp(DrawOps op) {
reed@google.combb6992a2011-04-26 17:41:56 +0000285 fWriter.write32(DrawOp_packOpFlagData(op, 0, 0));
286 }
reed@google.comacd471f2011-05-03 21:26:46 +0000287
288 bool needOpBytes(size_t size = 0);
289
290 inline void doNotify() {
291 if (!fDone) {
292 size_t bytes = fWriter.size() - fBytesNotified;
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000293 if (bytes > 0) {
294 fController->notifyWritten(bytes);
295 fBytesNotified += bytes;
296 }
reed@google.comacd471f2011-05-03 21:26:46 +0000297 }
298 }
reed@google.comb55d1182011-05-11 00:42:04 +0000299
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000300 // Should be called after any calls to an SkFlatDictionary::findAndReplace
301 // if a new SkFlatData was added when in cross process mode
302 void flattenFactoryNames();
303
scroggo@google.com4dffc592012-07-17 16:49:40 +0000304 FlattenableHeap fFlattenableHeap;
305 FlatDictionary fFlatDictionary;
reed@google.comb55d1182011-05-11 00:42:04 +0000306 int fCurrFlatIndex[kCount_PaintFlats];
307 int flattenToIndex(SkFlattenable* obj, PaintFlats);
308
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000309 // Common code used by drawBitmap*. Behaves differently depending on the
310 // type of SkBitmapHeap being used, which is determined by the flags used.
311 bool commonDrawBitmap(const SkBitmap& bm, DrawOps op, unsigned flags,
312 size_t opBytesNeeded, const SkPaint* paint);
scroggo@google.com58be6822012-07-30 14:40:01 +0000313
reed@google.com31891582011-05-12 03:03:56 +0000314 SkPaint fPaint;
315 void writePaint(const SkPaint&);
reed@google.combb6992a2011-04-26 17:41:56 +0000316
reed@google.comacd471f2011-05-03 21:26:46 +0000317 class AutoPipeNotify {
318 public:
319 AutoPipeNotify(SkGPipeCanvas* canvas) : fCanvas(canvas) {}
320 ~AutoPipeNotify() { fCanvas->doNotify(); }
321 private:
322 SkGPipeCanvas* fCanvas;
323 };
324 friend class AutoPipeNotify;
325
reed@google.combb6992a2011-04-26 17:41:56 +0000326 typedef SkCanvas INHERITED;
327};
328
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000329void SkGPipeCanvas::flattenFactoryNames() {
330 const char* name;
331 while ((name = fFactorySet->getNextAddedFactoryName()) != NULL) {
332 size_t len = strlen(name);
333 if (this->needOpBytes(len)) {
334 this->writeOp(kDef_Factory_DrawOp);
335 fWriter.writeString(name, len);
336 }
337 }
338}
339
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000340bool SkGPipeCanvas::shuttleBitmap(const SkBitmap& bm, int32_t slot) {
scroggo@google.com565254b2012-06-28 15:41:32 +0000341 SkASSERT(shouldFlattenBitmaps(fFlags));
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000342 SkOrderedWriteBuffer buffer(1024);
343 buffer.setNamedFactoryRecorder(fFactorySet);
344 bm.flatten(buffer);
345 this->flattenFactoryNames();
346 uint32_t size = buffer.size();
347 if (this->needOpBytes(size)) {
348 this->writeOp(kDef_Bitmap_DrawOp, 0, slot);
349 void* dst = static_cast<void*>(fWriter.reserve(size));
350 buffer.writeToMemory(dst);
351 return true;
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000352 }
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000353 return false;
scroggo@google.comd3ba5cc2012-07-09 16:05:53 +0000354}
355
reed@google.comb55d1182011-05-11 00:42:04 +0000356// return 0 for NULL (or unflattenable obj), or index-base-1
scroggo@google.comd3ba5cc2012-07-09 16:05:53 +0000357// return ~(index-base-1) if an old flattenable was replaced
reed@google.comb55d1182011-05-11 00:42:04 +0000358int SkGPipeCanvas::flattenToIndex(SkFlattenable* obj, PaintFlats paintflat) {
scroggo@google.comd5d158b2012-08-14 20:38:28 +0000359 SkASSERT(!fDone && fBitmapHeap != NULL);
reed@google.comb55d1182011-05-11 00:42:04 +0000360 if (NULL == obj) {
361 return 0;
362 }
reed@google.comb55d1182011-05-11 00:42:04 +0000363
scroggo@google.comd9d29672012-08-14 17:21:34 +0000364 fBitmapHeap->deferAddingOwners();
scroggo@google.com4dffc592012-07-17 16:49:40 +0000365 bool added, replaced;
scroggo@google.com664fab12012-08-14 19:22:05 +0000366 const SkFlatData* flat = fFlatDictionary.findAndReplace(*obj, fFlattenableHeap.flatToReplace(),
367 &added, &replaced);
scroggo@google.comd9d29672012-08-14 17:21:34 +0000368 fBitmapHeap->endAddingOwnersDeferral(added);
scroggo@google.com4dffc592012-07-17 16:49:40 +0000369 int index = flat->index();
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000370 if (added) {
371 if (isCrossProcess(fFlags)) {
372 this->flattenFactoryNames();
373 }
374 size_t flatSize = flat->flatSize();
375 if (this->needOpBytes(flatSize)) {
376 this->writeOp(kDef_Flattenable_DrawOp, paintflat, index);
377 fWriter.write(flat->data(), flatSize);
378 }
scroggo@google.com4dffc592012-07-17 16:49:40 +0000379 }
380 if (replaced) {
reed@google.comb55d1182011-05-11 00:42:04 +0000381 index = ~index;
reed@google.comb55d1182011-05-11 00:42:04 +0000382 }
scroggo@google.com4dffc592012-07-17 16:49:40 +0000383 return index;
reed@google.comb55d1182011-05-11 00:42:04 +0000384}
385
reed@google.combb6992a2011-04-26 17:41:56 +0000386///////////////////////////////////////////////////////////////////////////////
387
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000388/**
389 * If SkBitmaps are to be flattened to send to the reader, this class is
390 * provided to the SkBitmapHeap to tell the SkGPipeCanvas to do so.
391 */
392class BitmapShuttle : public SkBitmapHeap::ExternalStorage {
393public:
394 BitmapShuttle(SkGPipeCanvas*);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000395
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000396 ~BitmapShuttle();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000397
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000398 virtual bool insert(const SkBitmap& bitmap, int32_t slot) SK_OVERRIDE;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000399
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000400private:
401 SkGPipeCanvas* fCanvas;
402};
403
404///////////////////////////////////////////////////////////////////////////////
405
reed@google.comacd471f2011-05-03 21:26:46 +0000406#define MIN_BLOCK_SIZE (16 * 1024)
scroggo@google.com4dffc592012-07-17 16:49:40 +0000407#define BITMAPS_TO_KEEP 5
408#define FLATTENABLES_TO_KEEP 10
reed@google.combb6992a2011-04-26 17:41:56 +0000409
reed@google.comacd471f2011-05-03 21:26:46 +0000410SkGPipeCanvas::SkGPipeCanvas(SkGPipeController* controller,
rmistry@google.comd6176b02012-08-23 18:14:13 +0000411 SkWriter32* writer, uint32_t flags,
junov@chromium.orga8db8fe2012-08-15 19:49:22 +0000412 uint32_t width, uint32_t height)
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000413: fFactorySet(isCrossProcess(flags) ? SkNEW(SkNamedFactorySet) : NULL)
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000414, fWriter(*writer)
415, fFlags(flags)
scroggo@google.com664fab12012-08-14 19:22:05 +0000416, fFlattenableHeap(FLATTENABLES_TO_KEEP, fFactorySet, isCrossProcess(flags))
scroggo@google.com15543602012-08-02 18:49:49 +0000417, fFlatDictionary(&fFlattenableHeap) {
reed@google.comacd471f2011-05-03 21:26:46 +0000418 fController = controller;
reed@google.combb6992a2011-04-26 17:41:56 +0000419 fDone = false;
reed@google.comacd471f2011-05-03 21:26:46 +0000420 fBlockSize = 0; // need first block from controller
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000421 fBytesNotified = 0;
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000422 fFirstSaveLayerStackLevel = kNoSaveLayer;
reed@google.comb55d1182011-05-11 00:42:04 +0000423 sk_bzero(fCurrFlatIndex, sizeof(fCurrFlatIndex));
reed@google.comacd471f2011-05-03 21:26:46 +0000424
reed@google.combb6793b2011-05-05 15:18:15 +0000425 // we need a device to limit our clip
yangsu@google.com06b4da12011-06-17 15:04:40 +0000426 // We don't allocate pixels for the bitmap
reed@google.combb6793b2011-05-05 15:18:15 +0000427 SkBitmap bitmap;
junov@chromium.orga8db8fe2012-08-15 19:49:22 +0000428 bitmap.setConfig(SkBitmap::kARGB_8888_Config, width, height);
yangsu@google.com06b4da12011-06-17 15:04:40 +0000429 SkDevice* device = SkNEW_ARGS(SkDevice, (bitmap));
reed@google.combb6793b2011-05-05 15:18:15 +0000430 this->setDevice(device)->unref();
scroggo@google.com10dccde2012-08-08 20:43:22 +0000431
scroggo@google.com565254b2012-06-28 15:41:32 +0000432 // Tell the reader the appropriate flags to use.
433 if (this->needOpBytes()) {
434 this->writeOp(kReportFlags_DrawOp, fFlags, 0);
435 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000436
scroggo@google.com10dccde2012-08-08 20:43:22 +0000437 if (shouldFlattenBitmaps(flags)) {
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000438 BitmapShuttle* shuttle = SkNEW_ARGS(BitmapShuttle, (this));
scroggo@google.comd9d29672012-08-14 17:21:34 +0000439 fBitmapHeap = SkNEW_ARGS(SkBitmapHeap, (shuttle, BITMAPS_TO_KEEP));
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000440 shuttle->unref();
scroggo@google.com10dccde2012-08-08 20:43:22 +0000441 } else {
scroggo@google.comd9d29672012-08-14 17:21:34 +0000442 fBitmapHeap = SkNEW_ARGS(SkBitmapHeap,
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000443 (BITMAPS_TO_KEEP, controller->numberOfReaders()));
scroggo@google.com10dccde2012-08-08 20:43:22 +0000444 if (this->needOpBytes(sizeof(void*))) {
scroggo@google.comd9d29672012-08-14 17:21:34 +0000445 this->writeOp(kShareBitmapHeap_DrawOp);
446 fWriter.writePtr(static_cast<void*>(fBitmapHeap));
scroggo@google.com10dccde2012-08-08 20:43:22 +0000447 }
448 }
scroggo@google.comd9d29672012-08-14 17:21:34 +0000449 fFlattenableHeap.setBitmapStorage(fBitmapHeap);
scroggo@google.com10dccde2012-08-08 20:43:22 +0000450 this->doNotify();
reed@google.combb6992a2011-04-26 17:41:56 +0000451}
452
453SkGPipeCanvas::~SkGPipeCanvas() {
454 this->finish();
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000455 SkSafeUnref(fFactorySet);
scroggo@google.comd9d29672012-08-14 17:21:34 +0000456 SkSafeUnref(fBitmapHeap);
reed@google.combb6992a2011-04-26 17:41:56 +0000457}
458
reed@google.comacd471f2011-05-03 21:26:46 +0000459bool SkGPipeCanvas::needOpBytes(size_t needed) {
460 if (fDone) {
461 return false;
462 }
463
464 needed += 4; // size of DrawOp atom
465 if (fWriter.size() + needed > fBlockSize) {
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000466 // Before we wipe out any data that has already been written, read it
467 // out.
468 this->doNotify();
469 size_t blockSize = SkMax32(MIN_BLOCK_SIZE, needed);
470 void* block = fController->requestBlock(blockSize, &fBlockSize);
reed@google.comacd471f2011-05-03 21:26:46 +0000471 if (NULL == block) {
472 fDone = true;
473 return false;
474 }
scroggo@google.com460a23e2012-08-16 17:56:49 +0000475 SkASSERT(SkIsAlign4(fBlockSize));
reed@google.comacd471f2011-05-03 21:26:46 +0000476 fWriter.reset(block, fBlockSize);
477 fBytesNotified = 0;
478 }
479 return true;
480}
481
reed@google.comf5842f72011-05-04 18:30:04 +0000482uint32_t SkGPipeCanvas::getTypefaceID(SkTypeface* face) {
483 uint32_t id = 0; // 0 means default/null typeface
484 if (face) {
485 id = fTypefaceSet.find(face);
486 if (0 == id) {
487 id = fTypefaceSet.add(face);
488 size_t size = writeTypeface(NULL, face);
489 if (this->needOpBytes(size)) {
reed@google.combb6793b2011-05-05 15:18:15 +0000490 this->writeOp(kDef_Typeface_DrawOp);
reed@google.comf5842f72011-05-04 18:30:04 +0000491 writeTypeface(&fWriter, face);
492 }
493 }
494 }
495 return id;
496}
497
reed@google.combb6992a2011-04-26 17:41:56 +0000498///////////////////////////////////////////////////////////////////////////////
499
reed@google.comacd471f2011-05-03 21:26:46 +0000500#define NOTIFY_SETUP(canvas) \
501 AutoPipeNotify apn(canvas)
502
reed@google.combb6992a2011-04-26 17:41:56 +0000503int SkGPipeCanvas::save(SaveFlags flags) {
reed@google.comacd471f2011-05-03 21:26:46 +0000504 NOTIFY_SETUP(this);
505 if (this->needOpBytes()) {
506 this->writeOp(kSave_DrawOp, 0, flags);
507 }
reed@google.combb6992a2011-04-26 17:41:56 +0000508 return this->INHERITED::save(flags);
509}
510
511int SkGPipeCanvas::saveLayer(const SkRect* bounds, const SkPaint* paint,
512 SaveFlags saveFlags) {
reed@google.comacd471f2011-05-03 21:26:46 +0000513 NOTIFY_SETUP(this);
514 size_t size = 0;
reed@google.combb6992a2011-04-26 17:41:56 +0000515 unsigned opFlags = 0;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000516
reed@google.combb6992a2011-04-26 17:41:56 +0000517 if (bounds) {
518 opFlags |= kSaveLayer_HasBounds_DrawOpFlag;
reed@google.comacd471f2011-05-03 21:26:46 +0000519 size += sizeof(SkRect);
reed@google.combb6992a2011-04-26 17:41:56 +0000520 }
521 if (paint) {
522 opFlags |= kSaveLayer_HasPaint_DrawOpFlag;
reed@google.com31891582011-05-12 03:03:56 +0000523 this->writePaint(*paint);
reed@google.combb6992a2011-04-26 17:41:56 +0000524 }
525
reed@google.comacd471f2011-05-03 21:26:46 +0000526 if (this->needOpBytes(size)) {
527 this->writeOp(kSaveLayer_DrawOp, opFlags, saveFlags);
528 if (bounds) {
529 fWriter.writeRect(*bounds);
530 }
reed@google.combb6992a2011-04-26 17:41:56 +0000531 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000532
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000533 if (kNoSaveLayer == fFirstSaveLayerStackLevel){
534 fFirstSaveLayerStackLevel = this->getSaveCount();
535 }
reed@google.combb6992a2011-04-26 17:41:56 +0000536 // we just pass on the save, so we don't create a layer
537 return this->INHERITED::save(saveFlags);
538}
539
540void SkGPipeCanvas::restore() {
reed@google.comacd471f2011-05-03 21:26:46 +0000541 NOTIFY_SETUP(this);
542 if (this->needOpBytes()) {
543 this->writeOp(kRestore_DrawOp);
544 }
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000545
reed@google.combb6992a2011-04-26 17:41:56 +0000546 this->INHERITED::restore();
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000547
548 if (this->getSaveCount() == fFirstSaveLayerStackLevel){
549 fFirstSaveLayerStackLevel = kNoSaveLayer;
550 }
551}
552
553bool SkGPipeCanvas::isDrawingToLayer() const {
554 return kNoSaveLayer != fFirstSaveLayerStackLevel;
reed@google.combb6992a2011-04-26 17:41:56 +0000555}
556
557bool SkGPipeCanvas::translate(SkScalar dx, SkScalar dy) {
558 if (dx || dy) {
reed@google.comacd471f2011-05-03 21:26:46 +0000559 NOTIFY_SETUP(this);
560 if (this->needOpBytes(2 * sizeof(SkScalar))) {
561 this->writeOp(kTranslate_DrawOp);
562 fWriter.writeScalar(dx);
563 fWriter.writeScalar(dy);
564 }
reed@google.combb6992a2011-04-26 17:41:56 +0000565 }
566 return this->INHERITED::translate(dx, dy);
567}
568
569bool SkGPipeCanvas::scale(SkScalar sx, SkScalar sy) {
570 if (sx || sy) {
reed@google.comacd471f2011-05-03 21:26:46 +0000571 NOTIFY_SETUP(this);
572 if (this->needOpBytes(2 * sizeof(SkScalar))) {
573 this->writeOp(kScale_DrawOp);
574 fWriter.writeScalar(sx);
575 fWriter.writeScalar(sy);
576 }
reed@google.combb6992a2011-04-26 17:41:56 +0000577 }
578 return this->INHERITED::scale(sx, sy);
579}
580
581bool SkGPipeCanvas::rotate(SkScalar degrees) {
582 if (degrees) {
reed@google.comacd471f2011-05-03 21:26:46 +0000583 NOTIFY_SETUP(this);
584 if (this->needOpBytes(sizeof(SkScalar))) {
585 this->writeOp(kRotate_DrawOp);
586 fWriter.writeScalar(degrees);
587 }
reed@google.combb6992a2011-04-26 17:41:56 +0000588 }
589 return this->INHERITED::rotate(degrees);
590}
591
592bool SkGPipeCanvas::skew(SkScalar sx, SkScalar sy) {
593 if (sx || sy) {
reed@google.comacd471f2011-05-03 21:26:46 +0000594 NOTIFY_SETUP(this);
595 if (this->needOpBytes(2 * sizeof(SkScalar))) {
596 this->writeOp(kSkew_DrawOp);
597 fWriter.writeScalar(sx);
598 fWriter.writeScalar(sy);
599 }
reed@google.combb6992a2011-04-26 17:41:56 +0000600 }
601 return this->INHERITED::skew(sx, sy);
602}
603
604bool SkGPipeCanvas::concat(const SkMatrix& matrix) {
605 if (!matrix.isIdentity()) {
reed@google.comacd471f2011-05-03 21:26:46 +0000606 NOTIFY_SETUP(this);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000607 if (this->needOpBytes(matrix.writeToMemory(NULL))) {
reed@google.comacd471f2011-05-03 21:26:46 +0000608 this->writeOp(kConcat_DrawOp);
djsollen@google.com2b2ede32012-04-12 13:24:04 +0000609 fWriter.writeMatrix(matrix);
reed@google.comacd471f2011-05-03 21:26:46 +0000610 }
reed@google.combb6992a2011-04-26 17:41:56 +0000611 }
612 return this->INHERITED::concat(matrix);
613}
614
615void SkGPipeCanvas::setMatrix(const SkMatrix& matrix) {
reed@google.comacd471f2011-05-03 21:26:46 +0000616 NOTIFY_SETUP(this);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000617 if (this->needOpBytes(matrix.writeToMemory(NULL))) {
reed@google.comacd471f2011-05-03 21:26:46 +0000618 this->writeOp(kSetMatrix_DrawOp);
djsollen@google.com2b2ede32012-04-12 13:24:04 +0000619 fWriter.writeMatrix(matrix);
reed@google.comacd471f2011-05-03 21:26:46 +0000620 }
reed@google.combb6992a2011-04-26 17:41:56 +0000621 this->INHERITED::setMatrix(matrix);
622}
623
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000624bool SkGPipeCanvas::clipRect(const SkRect& rect, SkRegion::Op rgnOp,
625 bool doAntiAlias) {
reed@google.comacd471f2011-05-03 21:26:46 +0000626 NOTIFY_SETUP(this);
scroggo@google.com460a23e2012-08-16 17:56:49 +0000627 if (this->needOpBytes(sizeof(SkRect))) {
628 unsigned flags = doAntiAlias & kClip_HasAntiAlias_DrawOpFlag;
629 this->writeOp(kClipRect_DrawOp, flags, rgnOp);
reed@google.comacd471f2011-05-03 21:26:46 +0000630 fWriter.writeRect(rect);
631 }
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000632 return this->INHERITED::clipRect(rect, rgnOp, doAntiAlias);
reed@google.combb6992a2011-04-26 17:41:56 +0000633}
634
reed@google.com4ed0fb72012-12-12 20:48:18 +0000635bool SkGPipeCanvas::clipRRect(const SkRRect& rrect, SkRegion::Op rgnOp,
636 bool doAntiAlias) {
637 NOTIFY_SETUP(this);
638 if (this->needOpBytes(kSizeOfFlatRRect)) {
639 unsigned flags = doAntiAlias & kClip_HasAntiAlias_DrawOpFlag;
640 this->writeOp(kClipRRect_DrawOp, flags, rgnOp);
641 fWriter.writeRRect(rrect);
642 }
643 return this->INHERITED::clipRRect(rrect, rgnOp, doAntiAlias);
644}
645
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000646bool SkGPipeCanvas::clipPath(const SkPath& path, SkRegion::Op rgnOp,
647 bool doAntiAlias) {
reed@google.comacd471f2011-05-03 21:26:46 +0000648 NOTIFY_SETUP(this);
scroggo@google.com460a23e2012-08-16 17:56:49 +0000649 if (this->needOpBytes(path.writeToMemory(NULL))) {
650 unsigned flags = doAntiAlias & kClip_HasAntiAlias_DrawOpFlag;
651 this->writeOp(kClipPath_DrawOp, flags, rgnOp);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000652 fWriter.writePath(path);
reed@google.comacd471f2011-05-03 21:26:46 +0000653 }
reed@google.combb6992a2011-04-26 17:41:56 +0000654 // we just pass on the bounds of the path
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000655 return this->INHERITED::clipRect(path.getBounds(), rgnOp, doAntiAlias);
reed@google.combb6992a2011-04-26 17:41:56 +0000656}
657
658bool SkGPipeCanvas::clipRegion(const SkRegion& region, SkRegion::Op rgnOp) {
reed@google.comacd471f2011-05-03 21:26:46 +0000659 NOTIFY_SETUP(this);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000660 if (this->needOpBytes(region.writeToMemory(NULL))) {
reed@google.comacd471f2011-05-03 21:26:46 +0000661 this->writeOp(kClipRegion_DrawOp, 0, rgnOp);
djsollen@google.com2b2ede32012-04-12 13:24:04 +0000662 fWriter.writeRegion(region);
reed@google.comacd471f2011-05-03 21:26:46 +0000663 }
reed@google.combb6992a2011-04-26 17:41:56 +0000664 return this->INHERITED::clipRegion(region, rgnOp);
665}
666
667///////////////////////////////////////////////////////////////////////////////
668
669void SkGPipeCanvas::clear(SkColor color) {
reed@google.comacd471f2011-05-03 21:26:46 +0000670 NOTIFY_SETUP(this);
reed@google.combb6992a2011-04-26 17:41:56 +0000671 unsigned flags = 0;
672 if (color) {
673 flags |= kClear_HasColor_DrawOpFlag;
674 }
reed@google.comacd471f2011-05-03 21:26:46 +0000675 if (this->needOpBytes(sizeof(SkColor))) {
676 this->writeOp(kDrawClear_DrawOp, flags, 0);
677 if (color) {
678 fWriter.write32(color);
679 }
reed@google.combb6992a2011-04-26 17:41:56 +0000680 }
681}
682
683void SkGPipeCanvas::drawPaint(const SkPaint& paint) {
reed@google.comacd471f2011-05-03 21:26:46 +0000684 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000685 this->writePaint(paint);
reed@google.comacd471f2011-05-03 21:26:46 +0000686 if (this->needOpBytes()) {
reed@google.com31891582011-05-12 03:03:56 +0000687 this->writeOp(kDrawPaint_DrawOp);
reed@google.comacd471f2011-05-03 21:26:46 +0000688 }
reed@google.combb6992a2011-04-26 17:41:56 +0000689}
690
691void SkGPipeCanvas::drawPoints(PointMode mode, size_t count,
692 const SkPoint pts[], const SkPaint& paint) {
693 if (count) {
reed@google.comacd471f2011-05-03 21:26:46 +0000694 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000695 this->writePaint(paint);
reed@google.comacd471f2011-05-03 21:26:46 +0000696 if (this->needOpBytes(4 + count * sizeof(SkPoint))) {
reed@google.com31891582011-05-12 03:03:56 +0000697 this->writeOp(kDrawPoints_DrawOp, mode, 0);
reed@google.comacd471f2011-05-03 21:26:46 +0000698 fWriter.write32(count);
699 fWriter.write(pts, count * sizeof(SkPoint));
700 }
reed@google.combb6992a2011-04-26 17:41:56 +0000701 }
702}
703
reed@google.com4ed0fb72012-12-12 20:48:18 +0000704void SkGPipeCanvas::drawOval(const SkRect& rect, const SkPaint& paint) {
705 NOTIFY_SETUP(this);
706 this->writePaint(paint);
707 if (this->needOpBytes(sizeof(SkRect))) {
708 this->writeOp(kDrawOval_DrawOp);
709 fWriter.writeRect(rect);
710 }
711}
712
reed@google.combb6992a2011-04-26 17:41:56 +0000713void SkGPipeCanvas::drawRect(const SkRect& rect, const SkPaint& paint) {
reed@google.comacd471f2011-05-03 21:26:46 +0000714 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000715 this->writePaint(paint);
reed@google.comacd471f2011-05-03 21:26:46 +0000716 if (this->needOpBytes(sizeof(SkRect))) {
reed@google.com31891582011-05-12 03:03:56 +0000717 this->writeOp(kDrawRect_DrawOp);
reed@google.comacd471f2011-05-03 21:26:46 +0000718 fWriter.writeRect(rect);
719 }
reed@google.combb6992a2011-04-26 17:41:56 +0000720}
721
reed@google.com4ed0fb72012-12-12 20:48:18 +0000722void SkGPipeCanvas::drawRRect(const SkRRect& rrect, const SkPaint& paint) {
723 NOTIFY_SETUP(this);
724 this->writePaint(paint);
725 if (this->needOpBytes(kSizeOfFlatRRect)) {
726 this->writeOp(kDrawRRect_DrawOp);
727 fWriter.writeRRect(rrect);
728 }
729}
730
reed@google.combb6992a2011-04-26 17:41:56 +0000731void SkGPipeCanvas::drawPath(const SkPath& path, const SkPaint& paint) {
reed@google.comacd471f2011-05-03 21:26:46 +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) {
scroggo@google.com58be6822012-07-30 14:40:01 +0000744 if (paint != NULL) {
scroggo@google.com460a23e2012-08-16 17:56:49 +0000745 flags |= kDrawBitmap_HasPaint_DrawOpFlag;
scroggo@google.com58be6822012-07-30 14:40:01 +0000746 this->writePaint(*paint);
747 }
scroggo@google.com10dccde2012-08-08 20:43:22 +0000748 if (this->needOpBytes(opBytesNeeded)) {
scroggo@google.comd5d158b2012-08-14 20:38:28 +0000749 SkASSERT(fBitmapHeap != NULL);
750 int32_t bitmapIndex = fBitmapHeap->insert(bm);
751 if (SkBitmapHeap::INVALID_SLOT == bitmapIndex) {
752 return false;
753 }
scroggo@google.com10dccde2012-08-08 20:43:22 +0000754 this->writeOp(op, flags, bitmapIndex);
scroggo@google.com58be6822012-07-30 14:40:01 +0000755 return true;
756 }
757 return false;
758}
759
760void SkGPipeCanvas::drawBitmap(const SkBitmap& bm, SkScalar left, SkScalar top,
761 const SkPaint* paint) {
762 NOTIFY_SETUP(this);
763 size_t opBytesNeeded = sizeof(SkScalar) * 2;
764
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000765 if (this->commonDrawBitmap(bm, kDrawBitmap_DrawOp, 0, opBytesNeeded, paint)) {
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000766 fWriter.writeScalar(left);
767 fWriter.writeScalar(top);
768 }
reed@google.combb6992a2011-04-26 17:41:56 +0000769}
770
reed@google.com71121732012-09-18 15:14:33 +0000771void SkGPipeCanvas::drawBitmapRectToRect(const SkBitmap& bm, const SkRect* src,
scroggo@google.com58be6822012-07-30 14:40:01 +0000772 const SkRect& dst, const SkPaint* paint) {
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000773 NOTIFY_SETUP(this);
scroggo@google.com58be6822012-07-30 14:40:01 +0000774 size_t opBytesNeeded = sizeof(SkRect);
775 bool hasSrc = src != NULL;
776 unsigned flags;
777 if (hasSrc) {
scroggo@google.com460a23e2012-08-16 17:56:49 +0000778 flags = kDrawBitmap_HasSrcRect_DrawOpFlag;
scroggo@google.com58be6822012-07-30 14:40:01 +0000779 opBytesNeeded += sizeof(int32_t) * 4;
780 } else {
781 flags = 0;
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000782 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000783
reed@google.com71121732012-09-18 15:14:33 +0000784 if (this->commonDrawBitmap(bm, kDrawBitmapRectToRect_DrawOp, flags, opBytesNeeded, paint)) {
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000785 if (hasSrc) {
reed@google.com71121732012-09-18 15:14:33 +0000786 fWriter.writeRect(*src);
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000787 }
788 fWriter.writeRect(dst);
789 }
reed@google.combb6992a2011-04-26 17:41:56 +0000790}
791
scroggo@google.com58be6822012-07-30 14:40:01 +0000792void SkGPipeCanvas::drawBitmapMatrix(const SkBitmap& bm, const SkMatrix& matrix,
793 const SkPaint* paint) {
794 NOTIFY_SETUP(this);
795 size_t opBytesNeeded = matrix.writeToMemory(NULL);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000796
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000797 if (this->commonDrawBitmap(bm, kDrawBitmapMatrix_DrawOp, 0, opBytesNeeded, paint)) {
scroggo@google.com58be6822012-07-30 14:40:01 +0000798 fWriter.writeMatrix(matrix);
799 }
reed@google.combb6992a2011-04-26 17:41:56 +0000800}
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000801
802void SkGPipeCanvas::drawBitmapNine(const SkBitmap& bm, const SkIRect& center,
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000803 const SkRect& dst, const SkPaint* paint) {
scroggo@google.com16d1d0b2012-05-02 19:09:40 +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
816void SkGPipeCanvas::drawSprite(const SkBitmap& bm, int left, int top,
817 const SkPaint* paint) {
818 NOTIFY_SETUP(this);
scroggo@google.com58be6822012-07-30 14:40:01 +0000819 size_t opBytesNeeded = sizeof(int32_t) * 2;
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000820
scroggo@google.com3e26bd02012-08-14 15:20:01 +0000821 if (this->commonDrawBitmap(bm, kDrawSprite_DrawOp, 0, opBytesNeeded, paint)) {
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000822 fWriter.write32(left);
823 fWriter.write32(top);
824 }
reed@google.combb6992a2011-04-26 17:41:56 +0000825}
826
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000827void SkGPipeCanvas::drawText(const void* text, size_t byteLength, SkScalar x,
reed@google.combb6992a2011-04-26 17:41:56 +0000828 SkScalar y, const SkPaint& paint) {
829 if (byteLength) {
reed@google.comacd471f2011-05-03 21:26:46 +0000830 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000831 this->writePaint(paint);
reed@google.comacd471f2011-05-03 21:26:46 +0000832 if (this->needOpBytes(4 + SkAlign4(byteLength) + 2 * sizeof(SkScalar))) {
reed@google.com31891582011-05-12 03:03:56 +0000833 this->writeOp(kDrawText_DrawOp);
reed@google.comacd471f2011-05-03 21:26:46 +0000834 fWriter.write32(byteLength);
835 fWriter.writePad(text, byteLength);
836 fWriter.writeScalar(x);
837 fWriter.writeScalar(y);
838 }
reed@google.combb6992a2011-04-26 17:41:56 +0000839 }
840}
841
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000842void SkGPipeCanvas::drawPosText(const void* text, size_t byteLength,
reed@google.combb6992a2011-04-26 17:41:56 +0000843 const SkPoint pos[], const SkPaint& paint) {
844 if (byteLength) {
reed@google.comacd471f2011-05-03 21:26:46 +0000845 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000846 this->writePaint(paint);
reed@google.combb6992a2011-04-26 17:41:56 +0000847 int count = paint.textToGlyphs(text, byteLength, NULL);
reed@google.comacd471f2011-05-03 21:26:46 +0000848 if (this->needOpBytes(4 + SkAlign4(byteLength) + 4 + count * sizeof(SkPoint))) {
reed@google.com31891582011-05-12 03:03:56 +0000849 this->writeOp(kDrawPosText_DrawOp);
reed@google.comacd471f2011-05-03 21:26:46 +0000850 fWriter.write32(byteLength);
851 fWriter.writePad(text, byteLength);
852 fWriter.write32(count);
853 fWriter.write(pos, count * sizeof(SkPoint));
854 }
reed@google.combb6992a2011-04-26 17:41:56 +0000855 }
856}
857
858void SkGPipeCanvas::drawPosTextH(const void* text, size_t byteLength,
859 const SkScalar xpos[], SkScalar constY,
860 const SkPaint& paint) {
861 if (byteLength) {
reed@google.comacd471f2011-05-03 21:26:46 +0000862 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000863 this->writePaint(paint);
reed@google.combb6992a2011-04-26 17:41:56 +0000864 int count = paint.textToGlyphs(text, byteLength, NULL);
reed@google.comacd471f2011-05-03 21:26:46 +0000865 if (this->needOpBytes(4 + SkAlign4(byteLength) + 4 + count * sizeof(SkScalar) + 4)) {
reed@google.com31891582011-05-12 03:03:56 +0000866 this->writeOp(kDrawPosTextH_DrawOp);
reed@google.comacd471f2011-05-03 21:26:46 +0000867 fWriter.write32(byteLength);
868 fWriter.writePad(text, byteLength);
869 fWriter.write32(count);
870 fWriter.write(xpos, count * sizeof(SkScalar));
871 fWriter.writeScalar(constY);
872 }
reed@google.combb6992a2011-04-26 17:41:56 +0000873 }
874}
875
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000876void SkGPipeCanvas::drawTextOnPath(const void* text, size_t byteLength,
877 const SkPath& path, const SkMatrix* matrix,
reed@google.combb6992a2011-04-26 17:41:56 +0000878 const SkPaint& paint) {
879 if (byteLength) {
reed@google.comacd471f2011-05-03 21:26:46 +0000880 NOTIFY_SETUP(this);
reed@google.combb6992a2011-04-26 17:41:56 +0000881 unsigned flags = 0;
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000882 size_t size = 4 + SkAlign4(byteLength) + path.writeToMemory(NULL);
reed@google.combb6992a2011-04-26 17:41:56 +0000883 if (matrix) {
884 flags |= kDrawTextOnPath_HasMatrix_DrawOpFlag;
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000885 size += matrix->writeToMemory(NULL);
reed@google.combb6992a2011-04-26 17:41:56 +0000886 }
reed@google.com31891582011-05-12 03:03:56 +0000887 this->writePaint(paint);
reed@google.comacd471f2011-05-03 21:26:46 +0000888 if (this->needOpBytes(size)) {
reed@google.com31891582011-05-12 03:03:56 +0000889 this->writeOp(kDrawTextOnPath_DrawOp, flags, 0);
reed@google.combb6992a2011-04-26 17:41:56 +0000890
reed@google.comacd471f2011-05-03 21:26:46 +0000891 fWriter.write32(byteLength);
892 fWriter.writePad(text, byteLength);
reed@google.combb6992a2011-04-26 17:41:56 +0000893
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000894 fWriter.writePath(path);
reed@google.comacd471f2011-05-03 21:26:46 +0000895 if (matrix) {
djsollen@google.com2b2ede32012-04-12 13:24:04 +0000896 fWriter.writeMatrix(*matrix);
reed@google.comacd471f2011-05-03 21:26:46 +0000897 }
reed@google.combb6992a2011-04-26 17:41:56 +0000898 }
899 }
900}
901
902void SkGPipeCanvas::drawPicture(SkPicture& picture) {
reed@google.com0faac1e2011-05-11 05:58:58 +0000903 // we want to playback the picture into individual draw calls
904 this->INHERITED::drawPicture(picture);
reed@google.combb6992a2011-04-26 17:41:56 +0000905}
906
reed@google.combb6992a2011-04-26 17:41:56 +0000907void SkGPipeCanvas::drawVertices(VertexMode mode, int vertexCount,
908 const SkPoint vertices[], const SkPoint texs[],
909 const SkColor colors[], SkXfermode*,
910 const uint16_t indices[], int indexCount,
911 const SkPaint& paint) {
912 if (0 == vertexCount) {
913 return;
914 }
915
reed@google.comacd471f2011-05-03 21:26:46 +0000916 NOTIFY_SETUP(this);
917 size_t size = 4 + vertexCount * sizeof(SkPoint);
reed@google.com31891582011-05-12 03:03:56 +0000918 this->writePaint(paint);
reed@google.combb6992a2011-04-26 17:41:56 +0000919 unsigned flags = 0;
920 if (texs) {
921 flags |= kDrawVertices_HasTexs_DrawOpFlag;
reed@google.comacd471f2011-05-03 21:26:46 +0000922 size += vertexCount * sizeof(SkPoint);
reed@google.combb6992a2011-04-26 17:41:56 +0000923 }
924 if (colors) {
925 flags |= kDrawVertices_HasColors_DrawOpFlag;
reed@google.comacd471f2011-05-03 21:26:46 +0000926 size += vertexCount * sizeof(SkColor);
reed@google.combb6992a2011-04-26 17:41:56 +0000927 }
928 if (indices && indexCount > 0) {
929 flags |= kDrawVertices_HasIndices_DrawOpFlag;
reed@google.comacd471f2011-05-03 21:26:46 +0000930 size += 4 + SkAlign4(indexCount * sizeof(uint16_t));
reed@google.combb6992a2011-04-26 17:41:56 +0000931 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000932
reed@google.comacd471f2011-05-03 21:26:46 +0000933 if (this->needOpBytes(size)) {
reed@google.com31891582011-05-12 03:03:56 +0000934 this->writeOp(kDrawVertices_DrawOp, flags, 0);
reed@google.comacd471f2011-05-03 21:26:46 +0000935 fWriter.write32(mode);
936 fWriter.write32(vertexCount);
937 fWriter.write(vertices, vertexCount * sizeof(SkPoint));
938 if (texs) {
939 fWriter.write(texs, vertexCount * sizeof(SkPoint));
940 }
941 if (colors) {
942 fWriter.write(colors, vertexCount * sizeof(SkColor));
943 }
reed@google.combb6992a2011-04-26 17:41:56 +0000944
reed@google.comacd471f2011-05-03 21:26:46 +0000945 // TODO: flatten xfermode
reed@google.combb6992a2011-04-26 17:41:56 +0000946
reed@google.comacd471f2011-05-03 21:26:46 +0000947 if (indices && indexCount > 0) {
948 fWriter.write32(indexCount);
949 fWriter.writePad(indices, indexCount * sizeof(uint16_t));
950 }
reed@google.combb6992a2011-04-26 17:41:56 +0000951 }
952}
953
reed@google.comacd471f2011-05-03 21:26:46 +0000954void SkGPipeCanvas::drawData(const void* ptr, size_t size) {
955 if (size && ptr) {
956 NOTIFY_SETUP(this);
reed@google.combb6992a2011-04-26 17:41:56 +0000957 unsigned data = 0;
958 if (size < (1 << DRAWOPS_DATA_BITS)) {
959 data = (unsigned)size;
960 }
reed@google.comacd471f2011-05-03 21:26:46 +0000961 if (this->needOpBytes(4 + SkAlign4(size))) {
962 this->writeOp(kDrawData_DrawOp, 0, data);
963 if (0 == data) {
964 fWriter.write32(size);
965 }
reed@google.combb6793b2011-05-05 15:18:15 +0000966 fWriter.writePad(ptr, size);
reed@google.combb6992a2011-04-26 17:41:56 +0000967 }
968 }
969}
970
junov@chromium.org77eec242012-07-18 17:54:45 +0000971void SkGPipeCanvas::flushRecording(bool detachCurrentBlock) {
972 doNotify();
973 if (detachCurrentBlock) {
974 // force a new block to be requested for the next recorded command
rmistry@google.comd6176b02012-08-23 18:14:13 +0000975 fBlockSize = 0;
junov@chromium.org77eec242012-07-18 17:54:45 +0000976 }
977}
978
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000979size_t SkGPipeCanvas::freeMemoryIfPossible(size_t bytesToFree) {
scroggo@google.comd5d158b2012-08-14 20:38:28 +0000980 return (NULL == fBitmapHeap) ? 0 : fBitmapHeap->freeMemoryIfPossible(bytesToFree);
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000981}
982
reed@google.combb6992a2011-04-26 17:41:56 +0000983///////////////////////////////////////////////////////////////////////////////
984
985template <typename T> uint32_t castToU32(T value) {
986 union {
987 T fSrc;
988 uint32_t fDst;
989 } data;
990 data.fSrc = value;
991 return data.fDst;
992}
993
reed@google.com31891582011-05-12 03:03:56 +0000994void SkGPipeCanvas::writePaint(const SkPaint& paint) {
scroggo@google.comd5d158b2012-08-14 20:38:28 +0000995 if (fDone) {
996 return;
997 }
reed@google.com31891582011-05-12 03:03:56 +0000998 SkPaint& base = fPaint;
reed@google.combb6992a2011-04-26 17:41:56 +0000999 uint32_t storage[32];
1000 uint32_t* ptr = storage;
reed@google.combb6992a2011-04-26 17:41:56 +00001001
1002 if (base.getFlags() != paint.getFlags()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001003 *ptr++ = PaintOp_packOpData(kFlags_PaintOp, paint.getFlags());
reed@google.comf5842f72011-05-04 18:30:04 +00001004 base.setFlags(paint.getFlags());
reed@google.combb6992a2011-04-26 17:41:56 +00001005 }
1006 if (base.getColor() != paint.getColor()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001007 *ptr++ = PaintOp_packOp(kColor_PaintOp);
1008 *ptr++ = paint.getColor();
reed@google.comf5842f72011-05-04 18:30:04 +00001009 base.setColor(paint.getColor());
reed@google.combb6992a2011-04-26 17:41:56 +00001010 }
1011 if (base.getStyle() != paint.getStyle()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001012 *ptr++ = PaintOp_packOpData(kStyle_PaintOp, paint.getStyle());
reed@google.comf5842f72011-05-04 18:30:04 +00001013 base.setStyle(paint.getStyle());
reed@google.combb6992a2011-04-26 17:41:56 +00001014 }
1015 if (base.getStrokeJoin() != paint.getStrokeJoin()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001016 *ptr++ = PaintOp_packOpData(kJoin_PaintOp, paint.getStrokeJoin());
reed@google.comf5842f72011-05-04 18:30:04 +00001017 base.setStrokeJoin(paint.getStrokeJoin());
reed@google.combb6992a2011-04-26 17:41:56 +00001018 }
1019 if (base.getStrokeCap() != paint.getStrokeCap()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001020 *ptr++ = PaintOp_packOpData(kCap_PaintOp, paint.getStrokeCap());
reed@google.comf5842f72011-05-04 18:30:04 +00001021 base.setStrokeCap(paint.getStrokeCap());
reed@google.combb6992a2011-04-26 17:41:56 +00001022 }
1023 if (base.getStrokeWidth() != paint.getStrokeWidth()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001024 *ptr++ = PaintOp_packOp(kWidth_PaintOp);
1025 *ptr++ = castToU32(paint.getStrokeWidth());
reed@google.comf5842f72011-05-04 18:30:04 +00001026 base.setStrokeWidth(paint.getStrokeWidth());
reed@google.combb6992a2011-04-26 17:41:56 +00001027 }
1028 if (base.getStrokeMiter() != paint.getStrokeMiter()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001029 *ptr++ = PaintOp_packOp(kMiter_PaintOp);
1030 *ptr++ = castToU32(paint.getStrokeMiter());
reed@google.comf5842f72011-05-04 18:30:04 +00001031 base.setStrokeMiter(paint.getStrokeMiter());
reed@google.combb6992a2011-04-26 17:41:56 +00001032 }
1033 if (base.getTextEncoding() != paint.getTextEncoding()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001034 *ptr++ = PaintOp_packOpData(kEncoding_PaintOp, paint.getTextEncoding());
reed@google.comf5842f72011-05-04 18:30:04 +00001035 base.setTextEncoding(paint.getTextEncoding());
reed@google.combb6992a2011-04-26 17:41:56 +00001036 }
1037 if (base.getHinting() != paint.getHinting()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001038 *ptr++ = PaintOp_packOpData(kHinting_PaintOp, paint.getHinting());
reed@google.comf5842f72011-05-04 18:30:04 +00001039 base.setHinting(paint.getHinting());
reed@google.combb6992a2011-04-26 17:41:56 +00001040 }
1041 if (base.getTextAlign() != paint.getTextAlign()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001042 *ptr++ = PaintOp_packOpData(kAlign_PaintOp, paint.getTextAlign());
reed@google.comf5842f72011-05-04 18:30:04 +00001043 base.setTextAlign(paint.getTextAlign());
reed@google.combb6992a2011-04-26 17:41:56 +00001044 }
1045 if (base.getTextSize() != paint.getTextSize()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001046 *ptr++ = PaintOp_packOp(kTextSize_PaintOp);
1047 *ptr++ = castToU32(paint.getTextSize());
reed@google.comf5842f72011-05-04 18:30:04 +00001048 base.setTextSize(paint.getTextSize());
reed@google.combb6992a2011-04-26 17:41:56 +00001049 }
1050 if (base.getTextScaleX() != paint.getTextScaleX()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001051 *ptr++ = PaintOp_packOp(kTextScaleX_PaintOp);
1052 *ptr++ = castToU32(paint.getTextScaleX());
reed@google.comf5842f72011-05-04 18:30:04 +00001053 base.setTextScaleX(paint.getTextScaleX());
reed@google.combb6992a2011-04-26 17:41:56 +00001054 }
1055 if (base.getTextSkewX() != paint.getTextSkewX()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001056 *ptr++ = PaintOp_packOp(kTextSkewX_PaintOp);
1057 *ptr++ = castToU32(paint.getTextSkewX());
reed@google.comf5842f72011-05-04 18:30:04 +00001058 base.setTextSkewX(paint.getTextSkewX());
1059 }
1060
1061 if (!SkTypeface::Equal(base.getTypeface(), paint.getTypeface())) {
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +00001062 if (isCrossProcess(fFlags)) {
scroggo@google.com3cb969f2012-07-27 20:39:19 +00001063 uint32_t id = this->getTypefaceID(paint.getTypeface());
1064 *ptr++ = PaintOp_packOpData(kTypeface_PaintOp, id);
1065 } else if (this->needOpBytes(sizeof(void*))) {
1066 // Add to the set for ref counting.
1067 fTypefaceSet.add(paint.getTypeface());
1068 // It is safe to write the typeface to the stream before the rest
1069 // of the paint unless we ever send a kReset_PaintOp, which we
1070 // currently never do.
1071 this->writeOp(kSetTypeface_DrawOp);
1072 fWriter.writePtr(paint.getTypeface());
1073 }
reed@google.comf5842f72011-05-04 18:30:04 +00001074 base.setTypeface(paint.getTypeface());
reed@google.combb6992a2011-04-26 17:41:56 +00001075 }
reed@google.combb6992a2011-04-26 17:41:56 +00001076
scroggo@google.com4dffc592012-07-17 16:49:40 +00001077 // This is a new paint, so all old flats can be safely purged, if necessary.
1078 fFlattenableHeap.markAllFlatsSafeToDelete();
reed@google.comb55d1182011-05-11 00:42:04 +00001079 for (int i = 0; i < kCount_PaintFlats; i++) {
1080 int index = this->flattenToIndex(get_paintflat(paint, i), (PaintFlats)i);
scroggo@google.comd3ba5cc2012-07-09 16:05:53 +00001081 bool replaced = index < 0;
1082 if (replaced) {
1083 index = ~index;
1084 }
scroggo@google.com4dffc592012-07-17 16:49:40 +00001085 // Store the index of any flat that needs to be kept. 0 means no flat.
1086 if (index > 0) {
1087 fFlattenableHeap.markFlatForKeeping(index);
1088 }
1089 SkASSERT(index >= 0 && index <= fFlatDictionary.count());
scroggo@google.comd3ba5cc2012-07-09 16:05:53 +00001090 if (index != fCurrFlatIndex[i] || replaced) {
reed@google.comb55d1182011-05-11 00:42:04 +00001091 *ptr++ = PaintOp_packOpFlagData(kFlatIndex_PaintOp, i, index);
1092 fCurrFlatIndex[i] = index;
1093 }
1094 }
1095
reed@google.comacd471f2011-05-03 21:26:46 +00001096 size_t size = (char*)ptr - (char*)storage;
1097 if (size && this->needOpBytes(size)) {
reed@google.comb55d1182011-05-11 00:42:04 +00001098 this->writeOp(kPaintOp_DrawOp, 0, size);
reed@google.comb55d1182011-05-11 00:42:04 +00001099 fWriter.write(storage, size);
reed@google.combb6992a2011-04-26 17:41:56 +00001100 for (size_t i = 0; i < size/4; i++) {
reed@google.comb55d1182011-05-11 00:42:04 +00001101// SkDebugf("[%d] %08X\n", i, storage[i]);
reed@google.combb6992a2011-04-26 17:41:56 +00001102 }
1103 }
reed@google.combb6992a2011-04-26 17:41:56 +00001104}
1105
1106///////////////////////////////////////////////////////////////////////////////
1107
1108#include "SkGPipe.h"
1109
scroggo@google.com3cb969f2012-07-27 20:39:19 +00001110SkGPipeController::~SkGPipeController() {
1111 SkSafeUnref(fCanvas);
1112}
1113
1114void SkGPipeController::setCanvas(SkGPipeCanvas* canvas) {
1115 SkRefCnt_SafeAssign(fCanvas, canvas);
1116}
1117
1118///////////////////////////////////////////////////////////////////////////////
1119
1120SkGPipeWriter::SkGPipeWriter()
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +00001121: fWriter(0) {
reed@google.combb6992a2011-04-26 17:41:56 +00001122 fCanvas = NULL;
1123}
1124
1125SkGPipeWriter::~SkGPipeWriter() {
reed@google.comacd471f2011-05-03 21:26:46 +00001126 this->endRecording();
reed@google.combb6992a2011-04-26 17:41:56 +00001127}
1128
junov@chromium.orga8db8fe2012-08-15 19:49:22 +00001129SkCanvas* SkGPipeWriter::startRecording(SkGPipeController* controller, uint32_t flags,
1130 uint32_t width, uint32_t height) {
reed@google.combb6992a2011-04-26 17:41:56 +00001131 if (NULL == fCanvas) {
reed@google.comacd471f2011-05-03 21:26:46 +00001132 fWriter.reset(NULL, 0);
junov@chromium.orga8db8fe2012-08-15 19:49:22 +00001133 fCanvas = SkNEW_ARGS(SkGPipeCanvas, (controller, &fWriter, flags, width, height));
reed@google.combb6992a2011-04-26 17:41:56 +00001134 }
scroggo@google.com3cb969f2012-07-27 20:39:19 +00001135 controller->setCanvas(fCanvas);
reed@google.combb6992a2011-04-26 17:41:56 +00001136 return fCanvas;
1137}
1138
1139void SkGPipeWriter::endRecording() {
1140 if (fCanvas) {
1141 fCanvas->finish();
1142 fCanvas->unref();
1143 fCanvas = NULL;
1144 }
1145}
1146
junov@chromium.org2e14ba82012-08-07 14:26:57 +00001147void SkGPipeWriter::flushRecording(bool detachCurrentBlock) {
1148 if (fCanvas) {
1149 fCanvas->flushRecording(detachCurrentBlock);
1150 }
junov@chromium.org77eec242012-07-18 17:54:45 +00001151}
1152
junov@chromium.org2e14ba82012-08-07 14:26:57 +00001153size_t SkGPipeWriter::freeMemoryIfPossible(size_t bytesToFree) {
1154 if (fCanvas) {
1155 return fCanvas->freeMemoryIfPossible(bytesToFree);
1156 }
1157 return 0;
1158}
1159
1160size_t SkGPipeWriter::storageAllocatedForRecording() const {
scroggo@google.com15011ee2012-07-26 20:03:32 +00001161 return NULL == fCanvas ? 0 : fCanvas->storageAllocatedForRecording();
1162}
1163
scroggo@google.com3e26bd02012-08-14 15:20:01 +00001164///////////////////////////////////////////////////////////////////////////////
1165
1166BitmapShuttle::BitmapShuttle(SkGPipeCanvas* canvas) {
1167 SkASSERT(canvas != NULL);
1168 fCanvas = canvas;
1169 fCanvas->ref();
1170}
1171
1172BitmapShuttle::~BitmapShuttle() {
1173 fCanvas->unref();
1174}
1175
1176bool BitmapShuttle::insert(const SkBitmap& bitmap, int32_t slot) {
1177 return fCanvas->shuttleBitmap(bitmap, slot);
1178}