blob: fc40006e66fa54c611558b91a219c57a2114592e [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
9
epoger@google.comec3ed6a2011-07-28 14:26:00 +000010
reed@google.combb6992a2011-04-26 17:41:56 +000011#include "SkCanvas.h"
reed@google.com8a85d0c2011-06-24 19:12:12 +000012#include "SkData.h"
reed@google.combb6793b2011-05-05 15:18:15 +000013#include "SkDevice.h"
reed@google.combb6992a2011-04-26 17:41:56 +000014#include "SkPaint.h"
reed@google.com75a09722012-05-10 12:56:16 +000015#include "SkPathEffect.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"
reed@google.comf5842f72011-05-04 18:30:04 +000019#include "SkStream.h"
reed@google.comb55d1182011-05-11 00:42:04 +000020#include "SkTSearch.h"
reed@google.comf5842f72011-05-04 18:30:04 +000021#include "SkTypeface.h"
reed@google.combb6992a2011-04-26 17:41:56 +000022#include "SkWriter32.h"
reed@google.comb55d1182011-05-11 00:42:04 +000023#include "SkColorFilter.h"
reed@google.com0faac1e2011-05-11 05:58:58 +000024#include "SkDrawLooper.h"
reed@google.comb55d1182011-05-11 00:42:04 +000025#include "SkMaskFilter.h"
26#include "SkRasterizer.h"
27#include "SkShader.h"
djsollen@google.com2b2ede32012-04-12 13:24:04 +000028#include "SkOrderedWriteBuffer.h"
scroggo@google.com4dffc592012-07-17 16:49:40 +000029#include "SkPictureFlat.h"
reed@google.comb55d1182011-05-11 00:42:04 +000030
31static SkFlattenable* get_paintflat(const SkPaint& paint, unsigned paintFlat) {
32 SkASSERT(paintFlat < kCount_PaintFlats);
33 switch (paintFlat) {
34 case kColorFilter_PaintFlat: return paint.getColorFilter();
reed@google.com0faac1e2011-05-11 05:58:58 +000035 case kDrawLooper_PaintFlat: return paint.getLooper();
reed@google.comb55d1182011-05-11 00:42:04 +000036 case kMaskFilter_PaintFlat: return paint.getMaskFilter();
37 case kPathEffect_PaintFlat: return paint.getPathEffect();
38 case kRasterizer_PaintFlat: return paint.getRasterizer();
39 case kShader_PaintFlat: return paint.getShader();
scroggo@google.com16d1d0b2012-05-02 19:09:40 +000040 case kImageFilter_PaintFlat: return paint.getImageFilter();
reed@google.comb55d1182011-05-11 00:42:04 +000041 case kXfermode_PaintFlat: return paint.getXfermode();
42 }
tomhudson@google.com0c00f212011-12-28 14:59:50 +000043 SkDEBUGFAIL("never gets here");
reed@google.comb55d1182011-05-11 00:42:04 +000044 return NULL;
45}
reed@google.combb6992a2011-04-26 17:41:56 +000046
reed@google.comf5842f72011-05-04 18:30:04 +000047static size_t writeTypeface(SkWriter32* writer, SkTypeface* typeface) {
48 SkASSERT(typeface);
49 SkDynamicMemoryWStream stream;
50 typeface->serialize(&stream);
51 size_t size = stream.getOffset();
52 if (writer) {
53 writer->write32(size);
reed@google.com8a85d0c2011-06-24 19:12:12 +000054 SkAutoDataUnref data(stream.copyToData());
robertphillips@google.com59f46b82012-07-10 17:30:58 +000055 writer->writePad(data->data(), size);
reed@google.comf5842f72011-05-04 18:30:04 +000056 }
scroggo@google.com5af9b202012-06-04 17:17:36 +000057 return 4 + SkAlign4(size);
reed@google.comf5842f72011-05-04 18:30:04 +000058}
59
reed@google.combb6992a2011-04-26 17:41:56 +000060///////////////////////////////////////////////////////////////////////////////
61
scroggo@google.com4dffc592012-07-17 16:49:40 +000062class FlattenableHeap : public SkFlatController {
63public:
64 FlattenableHeap(int numFlatsToKeep) : fNumFlatsToKeep(numFlatsToKeep) {}
65
66 ~FlattenableHeap() {
67 fPointers.freeAll();
68 }
69
70 virtual void* allocThrow(size_t bytes) SK_OVERRIDE;
71
72 virtual void unalloc(void* ptr) SK_OVERRIDE;
73
74 const SkFlatData* flatToReplace() const;
75
76 // Mark an SkFlatData as one that should not be returned by flatToReplace.
77 // Takes the result of SkFlatData::index() as its parameter.
78 void markFlatForKeeping(int index) {
79 *fFlatsThatMustBeKept.append() = index;
80 }
81
82 void markAllFlatsSafeToDelete() {
83 fFlatsThatMustBeKept.reset();
84 }
85
86private:
87 // Keep track of the indices (i.e. the result of SkFlatData::index()) of
88 // flats that must be kept, since they are on the current paint.
89 SkTDArray<int> fFlatsThatMustBeKept;
90 SkTDArray<void*> fPointers;
91 const int fNumFlatsToKeep;
92};
93
94void FlattenableHeap::unalloc(void* ptr) {
95 int indexToRemove = fPointers.rfind(ptr);
96 if (indexToRemove >= 0) {
97 sk_free(ptr);
98 fPointers.remove(indexToRemove);
99 }
100}
101
102void* FlattenableHeap::allocThrow(size_t bytes) {
103 void* ptr = sk_malloc_throw(bytes);
104 *fPointers.append() = ptr;
105 return ptr;
106}
107
108const SkFlatData* FlattenableHeap::flatToReplace() const {
109 // First, determine whether we should replace one.
110 if (fPointers.count() > fNumFlatsToKeep) {
111 // Look through the flattenable heap.
112 // TODO: Return the LRU flat.
113 for (int i = 0; i < fPointers.count(); i++) {
114 SkFlatData* potential = (SkFlatData*)fPointers[i];
115 // Make sure that it is not one that must be kept.
116 bool mustKeep = false;
117 for (int j = 0; j < fFlatsThatMustBeKept.count(); j++) {
118 if (potential->index() == fFlatsThatMustBeKept[j]) {
119 mustKeep = true;
120 break;
121 }
122 }
123 if (!mustKeep) {
124 return potential;
125 }
126 }
127 }
128 return NULL;
129}
130
131///////////////////////////////////////////////////////////////////////////////
132
133class FlatDictionary : public SkFlatDictionary<SkFlattenable> {
134public:
135 FlatDictionary(FlattenableHeap* heap, SkFactorySet* factorySet)
136 : SkFlatDictionary<SkFlattenable>(heap, NULL, NULL, factorySet) {
137 fFlattenProc = &flattenFlattenableProc;
138 // No need to define fUnflattenProc since the writer will never
139 // unflatten the data.
140 }
141 static void flattenFlattenableProc(SkOrderedWriteBuffer& buffer,
142 const void* obj) {
143 buffer.writeFlattenable((SkFlattenable*)obj);
144 }
145
146};
147
148///////////////////////////////////////////////////////////////////////////////
149
scroggo@google.com8ae3c7f2012-06-13 17:47:52 +0000150/*
151 * Shared heap for storing large things that can be shared, for a stream
152 * used by multiple readers.
153 * TODO: Make the allocations all come from cross process safe address space
154 * TODO: Store paths (others?)
scroggo@google.com284bf502012-07-17 16:10:34 +0000155 * TODO: Generalize the LRU caching mechanism
scroggo@google.com8ae3c7f2012-06-13 17:47:52 +0000156 */
scroggo@google.com4dffc592012-07-17 16:49:40 +0000157class SharedHeap {
scroggo@google.com8ae3c7f2012-06-13 17:47:52 +0000158public:
scroggo@google.com4dffc592012-07-17 16:49:40 +0000159 SharedHeap(bool shallow, int numOfReaders)
scroggo@google.com284bf502012-07-17 16:10:34 +0000160 : fBitmapCount(0)
161 , fMostRecentlyUsed(NULL)
162 , fLeastRecentlyUsed(NULL)
163 , fCanDoShallowCopies(shallow)
164 , fNumberOfReaders(numOfReaders) {}
scroggo@google.com4dffc592012-07-17 16:49:40 +0000165 ~SharedHeap() {
scroggo@google.com284bf502012-07-17 16:10:34 +0000166 BitmapInfo* iter = fMostRecentlyUsed;
167 while (iter != NULL) {
168 BitmapInfo* next = iter->fLessRecentlyUsed;
169 SkDELETE(iter);
170 fBitmapCount--;
171 iter = next;
scroggo@google.com8ae3c7f2012-06-13 17:47:52 +0000172 }
scroggo@google.com284bf502012-07-17 16:10:34 +0000173 SkASSERT(0 == fBitmapCount);
scroggo@google.com8ae3c7f2012-06-13 17:47:52 +0000174 }
175
176 /*
177 * Add a copy of a bitmap to the heap.
178 * @param bm The SkBitmap to be copied and placed in the heap.
scroggo@google.com284bf502012-07-17 16:10:34 +0000179 * @return void* Pointer to the BitmapInfo stored in the heap, which
180 * contains a copy of the SkBitmap. If NULL,
scroggo@google.com8ae3c7f2012-06-13 17:47:52 +0000181 * the bitmap could not be copied.
182 */
scroggo@google.com284bf502012-07-17 16:10:34 +0000183 const void* addBitmap(const SkBitmap& orig) {
scroggo@google.com4f1f6bf2012-07-02 13:35:09 +0000184 const uint32_t genID = orig.getGenerationID();
185 SkPixelRef* sharedPixelRef = NULL;
scroggo@google.com284bf502012-07-17 16:10:34 +0000186 // When looking to see if we've previously used this bitmap, start at
187 // the end, assuming that the caller is more likely to reuse a recent
188 // one.
189 BitmapInfo* iter = fMostRecentlyUsed;
190 while (iter != NULL) {
191 if (genID == iter->fGenID) {
192 SkBitmap* storedBitmap = iter->fBitmap;
scroggo@google.com6ea165d2012-07-03 14:52:08 +0000193 if (orig.pixelRefOffset() != storedBitmap->pixelRefOffset()
194 || orig.width() != storedBitmap->width()
195 || orig.height() != storedBitmap->height()) {
scroggo@google.com4f1f6bf2012-07-02 13:35:09 +0000196 // In this case, the bitmaps share a pixelRef, but have
scroggo@google.com6ea165d2012-07-03 14:52:08 +0000197 // different offsets or sizes. Keep track of the other
198 // bitmap so that instead of making another copy of the
199 // pixelRef we can use the copy we already made.
200 sharedPixelRef = storedBitmap->pixelRef();
scroggo@google.com4f1f6bf2012-07-02 13:35:09 +0000201 break;
202 }
scroggo@google.com284bf502012-07-17 16:10:34 +0000203 iter->addDraws(fNumberOfReaders);
204 this->setMostRecentlyUsed(iter);
205 return iter;
scroggo@google.com8ae3c7f2012-06-13 17:47:52 +0000206 }
scroggo@google.com284bf502012-07-17 16:10:34 +0000207 iter = iter->fLessRecentlyUsed;
scroggo@google.com8ae3c7f2012-06-13 17:47:52 +0000208 }
scroggo@google.com284bf502012-07-17 16:10:34 +0000209 SkAutoRef ar((SkRefCnt*)sharedPixelRef);
210 BitmapInfo* replace = this->bitmapToReplace(orig);
scroggo@google.com565254b2012-06-28 15:41:32 +0000211 SkBitmap* copy;
212 // If the bitmap is mutable, we still need to do a deep copy, since the
213 // caller may modify it afterwards. That said, if the bitmap is mutable,
214 // but has no pixelRef, the copy constructor actually does a deep copy.
scroggo@google.com4f1f6bf2012-07-02 13:35:09 +0000215 if (fCanDoShallowCopies && (orig.isImmutable() || !orig.pixelRef())) {
scroggo@google.com284bf502012-07-17 16:10:34 +0000216 if (NULL == replace) {
217 copy = SkNEW_ARGS(SkBitmap, (orig));
218 } else {
219 *replace->fBitmap = orig;
220 }
scroggo@google.com565254b2012-06-28 15:41:32 +0000221 } else {
scroggo@google.com4f1f6bf2012-07-02 13:35:09 +0000222 if (sharedPixelRef != NULL) {
scroggo@google.com284bf502012-07-17 16:10:34 +0000223 if (NULL == replace) {
224 // Do a shallow copy of the bitmap to get the width, height, etc
225 copy = SkNEW_ARGS(SkBitmap, (orig));
226 // Replace the pixelRef with the copy that was already made, and
227 // use the appropriate offset.
228 copy->setPixelRef(sharedPixelRef, orig.pixelRefOffset());
229 } else {
230 *replace->fBitmap = orig;
231 replace->fBitmap->setPixelRef(sharedPixelRef, orig.pixelRefOffset());
232 }
scroggo@google.com4f1f6bf2012-07-02 13:35:09 +0000233 } else {
scroggo@google.com284bf502012-07-17 16:10:34 +0000234 if (NULL == replace) {
235 copy = SkNEW(SkBitmap);
236 if (!orig.copyTo(copy, orig.getConfig())) {
237 delete copy;
238 return NULL;
239 }
240 } else {
241 if (!orig.copyTo(replace->fBitmap, orig.getConfig())) {
242 return NULL;
243 }
scroggo@google.com4f1f6bf2012-07-02 13:35:09 +0000244 }
scroggo@google.com565254b2012-06-28 15:41:32 +0000245 }
scroggo@google.com8ae3c7f2012-06-13 17:47:52 +0000246 }
scroggo@google.com284bf502012-07-17 16:10:34 +0000247 BitmapInfo* info;
248 if (NULL == replace) {
249 info = SkNEW_ARGS(BitmapInfo, (copy, genID, fNumberOfReaders));
250 fBitmapCount++;
251 } else {
252 replace->fGenID = genID;
253 replace->addDraws(fNumberOfReaders);
254 info = replace;
255 }
256 this->setMostRecentlyUsed(info);
257 return info;
scroggo@google.com8ae3c7f2012-06-13 17:47:52 +0000258 }
259private:
scroggo@google.com284bf502012-07-17 16:10:34 +0000260 void setMostRecentlyUsed(BitmapInfo* info);
261 BitmapInfo* bitmapToReplace(const SkBitmap& bm) const;
262
263 int fBitmapCount;
264 BitmapInfo* fLeastRecentlyUsed;
265 BitmapInfo* fMostRecentlyUsed;
266 const bool fCanDoShallowCopies;
267 const int fNumberOfReaders;
scroggo@google.com8ae3c7f2012-06-13 17:47:52 +0000268};
269
scroggo@google.com284bf502012-07-17 16:10:34 +0000270// We just "used" info. Update our LRU accordingly
scroggo@google.com4dffc592012-07-17 16:49:40 +0000271void SharedHeap::setMostRecentlyUsed(BitmapInfo* info) {
scroggo@google.com284bf502012-07-17 16:10:34 +0000272 SkASSERT(info != NULL);
273 if (info == fMostRecentlyUsed) {
274 return;
275 }
276 // Remove info from its prior place, and make sure to cover the hole.
277 if (fLeastRecentlyUsed == info) {
278 SkASSERT(info->fMoreRecentlyUsed != NULL);
279 fLeastRecentlyUsed = info->fMoreRecentlyUsed;
280 }
281 if (info->fMoreRecentlyUsed != NULL) {
282 SkASSERT(fMostRecentlyUsed != info);
283 info->fMoreRecentlyUsed->fLessRecentlyUsed = info->fLessRecentlyUsed;
284 }
285 if (info->fLessRecentlyUsed != NULL) {
286 SkASSERT(fLeastRecentlyUsed != info);
287 info->fLessRecentlyUsed->fMoreRecentlyUsed = info->fMoreRecentlyUsed;
288 }
289 info->fMoreRecentlyUsed = NULL;
290 // Set up the head and tail pointers properly.
291 if (fMostRecentlyUsed != NULL) {
292 SkASSERT(NULL == fMostRecentlyUsed->fMoreRecentlyUsed);
293 fMostRecentlyUsed->fMoreRecentlyUsed = info;
294 info->fLessRecentlyUsed = fMostRecentlyUsed;
295 }
296 fMostRecentlyUsed = info;
297 if (NULL == fLeastRecentlyUsed) {
298 fLeastRecentlyUsed = info;
299 }
300}
301
302/**
303 * Given a new bitmap to be added to the cache, return an existing one that
304 * should be removed to make room, or NULL if there is already room.
305 */
scroggo@google.com4dffc592012-07-17 16:49:40 +0000306BitmapInfo* SharedHeap::bitmapToReplace(const SkBitmap& bm) const {
scroggo@google.com284bf502012-07-17 16:10:34 +0000307 // Arbitrarily set a limit of 5. We should test to find the best tradeoff
308 // between time and space. A lower limit means that we use less space, but
309 // it also means that we may have to insert the same bitmap into the heap
310 // multiple times (depending on the input), potentially taking more time.
311 // On the other hand, a lower limit also means searching through our stored
312 // bitmaps takes less time.
313 if (fBitmapCount > 5) {
314 BitmapInfo* iter = fLeastRecentlyUsed;
315 while (iter != NULL) {
316 if (iter->drawCount() > 0) {
317 // If the least recently used bitmap has not been drawn by some
318 // reader, then a more recently used one will not have been
319 // drawn yet either.
320 return NULL;
321 }
322 if (bm.pixelRef() != NULL
323 && bm.pixelRef() == iter->fBitmap->pixelRef()) {
324 // Do not replace a bitmap with a new one using the same
325 // pixel ref. Instead look for a different one that will
326 // potentially free up more space.
327 iter = iter->fMoreRecentlyUsed;
328 } else {
329 return iter;
330 }
331 }
332 }
333 return NULL;
334}
335
scroggo@google.com8ae3c7f2012-06-13 17:47:52 +0000336///////////////////////////////////////////////////////////////////////////////
337
reed@google.combb6992a2011-04-26 17:41:56 +0000338class SkGPipeCanvas : public SkCanvas {
339public:
scroggo@google.com565254b2012-06-28 15:41:32 +0000340 SkGPipeCanvas(SkGPipeController*, SkWriter32*, SkFactorySet*, uint32_t flags);
reed@google.combb6992a2011-04-26 17:41:56 +0000341 virtual ~SkGPipeCanvas();
342
343 void finish() {
344 if (!fDone) {
reed@google.comdbccc882011-07-08 18:53:39 +0000345 if (this->needOpBytes()) {
346 this->writeOp(kDone_DrawOp);
347 this->doNotify();
348 }
reed@google.combb6992a2011-04-26 17:41:56 +0000349 fDone = true;
350 }
351 }
352
junov@chromium.org77eec242012-07-18 17:54:45 +0000353 void flushRecording(bool detachCurrentBlock);
354
reed@google.combb6992a2011-04-26 17:41:56 +0000355 // overrides from SkCanvas
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000356 virtual int save(SaveFlags) SK_OVERRIDE;
357 virtual int saveLayer(const SkRect* bounds, const SkPaint*,
358 SaveFlags) SK_OVERRIDE;
359 virtual void restore() SK_OVERRIDE;
360 virtual bool translate(SkScalar dx, SkScalar dy) SK_OVERRIDE;
361 virtual bool scale(SkScalar sx, SkScalar sy) SK_OVERRIDE;
362 virtual bool rotate(SkScalar degrees) SK_OVERRIDE;
363 virtual bool skew(SkScalar sx, SkScalar sy) SK_OVERRIDE;
364 virtual bool concat(const SkMatrix& matrix) SK_OVERRIDE;
365 virtual void setMatrix(const SkMatrix& matrix) SK_OVERRIDE;
366 virtual bool clipRect(const SkRect& rect, SkRegion::Op op,
367 bool doAntiAlias = false) SK_OVERRIDE;
368 virtual bool clipPath(const SkPath& path, SkRegion::Op op,
369 bool doAntiAlias = false) SK_OVERRIDE;
370 virtual bool clipRegion(const SkRegion& region, SkRegion::Op op) SK_OVERRIDE;
371 virtual void clear(SkColor) SK_OVERRIDE;
372 virtual void drawPaint(const SkPaint& paint) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000373 virtual void drawPoints(PointMode, size_t count, const SkPoint pts[],
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000374 const SkPaint&) SK_OVERRIDE;
375 virtual void drawRect(const SkRect& rect, const SkPaint&) SK_OVERRIDE;
376 virtual void drawPath(const SkPath& path, const SkPaint&) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000377 virtual void drawBitmap(const SkBitmap&, SkScalar left, SkScalar top,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000378 const SkPaint*) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000379 virtual void drawBitmapRect(const SkBitmap&, const SkIRect* src,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000380 const SkRect& dst, const SkPaint*) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000381 virtual void drawBitmapMatrix(const SkBitmap&, const SkMatrix&,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000382 const SkPaint*) SK_OVERRIDE;
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000383 virtual void drawBitmapNine(const SkBitmap& bitmap, const SkIRect& center,
384 const SkRect& dst, const SkPaint* paint = NULL) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000385 virtual void drawSprite(const SkBitmap&, int left, int top,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000386 const SkPaint*) SK_OVERRIDE;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000387 virtual void drawText(const void* text, size_t byteLength, SkScalar x,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000388 SkScalar y, const SkPaint&) SK_OVERRIDE;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000389 virtual void drawPosText(const void* text, size_t byteLength,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000390 const SkPoint pos[], const SkPaint&) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000391 virtual void drawPosTextH(const void* text, size_t byteLength,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000392 const SkScalar xpos[], SkScalar constY,
393 const SkPaint&) SK_OVERRIDE;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000394 virtual void drawTextOnPath(const void* text, size_t byteLength,
395 const SkPath& path, const SkMatrix* matrix,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000396 const SkPaint&) SK_OVERRIDE;
397 virtual void drawPicture(SkPicture& picture) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000398 virtual void drawVertices(VertexMode, int vertexCount,
399 const SkPoint vertices[], const SkPoint texs[],
400 const SkColor colors[], SkXfermode*,
401 const uint16_t indices[], int indexCount,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000402 const SkPaint&) SK_OVERRIDE;
403 virtual void drawData(const void*, size_t) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000404
405private:
scroggo@google.com4dffc592012-07-17 16:49:40 +0000406 SharedHeap fSharedHeap;
reed@google.comacd471f2011-05-03 21:26:46 +0000407 SkGPipeController* fController;
reed@google.combb6992a2011-04-26 17:41:56 +0000408 SkWriter32& fWriter;
reed@google.comacd471f2011-05-03 21:26:46 +0000409 size_t fBlockSize; // amount allocated for writer
410 size_t fBytesNotified;
reed@google.combb6992a2011-04-26 17:41:56 +0000411 bool fDone;
scroggo@google.com565254b2012-06-28 15:41:32 +0000412 uint32_t fFlags;
reed@google.combb6992a2011-04-26 17:41:56 +0000413
reed@google.comf5842f72011-05-04 18:30:04 +0000414 SkRefCntSet fTypefaceSet;
415
416 uint32_t getTypefaceID(SkTypeface*);
417
reed@google.comacd471f2011-05-03 21:26:46 +0000418 inline void writeOp(DrawOps op, unsigned flags, unsigned data) {
reed@google.combb6992a2011-04-26 17:41:56 +0000419 fWriter.write32(DrawOp_packOpFlagData(op, flags, data));
420 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000421
reed@google.comacd471f2011-05-03 21:26:46 +0000422 inline void writeOp(DrawOps op) {
reed@google.combb6992a2011-04-26 17:41:56 +0000423 fWriter.write32(DrawOp_packOpFlagData(op, 0, 0));
424 }
reed@google.comacd471f2011-05-03 21:26:46 +0000425
426 bool needOpBytes(size_t size = 0);
427
428 inline void doNotify() {
429 if (!fDone) {
430 size_t bytes = fWriter.size() - fBytesNotified;
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000431 if (bytes > 0) {
432 fController->notifyWritten(bytes);
433 fBytesNotified += bytes;
434 }
reed@google.comacd471f2011-05-03 21:26:46 +0000435 }
436 }
reed@google.comb55d1182011-05-11 00:42:04 +0000437
scroggo@google.com4dffc592012-07-17 16:49:40 +0000438 // These are only used when in cross process, but with no shared address
439 // space, so bitmaps are flattened.
440 FlattenableHeap fBitmapHeap;
441 SkBitmapDictionary fBitmapDictionary;
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000442 int flattenToIndex(const SkBitmap&);
443
scroggo@google.com4dffc592012-07-17 16:49:40 +0000444 FlattenableHeap fFlattenableHeap;
445 FlatDictionary fFlatDictionary;
reed@google.comb55d1182011-05-11 00:42:04 +0000446 int fCurrFlatIndex[kCount_PaintFlats];
447 int flattenToIndex(SkFlattenable* obj, PaintFlats);
448
reed@google.com31891582011-05-12 03:03:56 +0000449 SkPaint fPaint;
450 void writePaint(const SkPaint&);
reed@google.combb6992a2011-04-26 17:41:56 +0000451
reed@google.comacd471f2011-05-03 21:26:46 +0000452 class AutoPipeNotify {
453 public:
454 AutoPipeNotify(SkGPipeCanvas* canvas) : fCanvas(canvas) {}
455 ~AutoPipeNotify() { fCanvas->doNotify(); }
456 private:
457 SkGPipeCanvas* fCanvas;
458 };
459 friend class AutoPipeNotify;
460
reed@google.combb6992a2011-04-26 17:41:56 +0000461 typedef SkCanvas INHERITED;
462};
463
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000464int SkGPipeCanvas::flattenToIndex(const SkBitmap & bitmap) {
scroggo@google.com565254b2012-06-28 15:41:32 +0000465 SkASSERT(shouldFlattenBitmaps(fFlags));
scroggo@google.com4dffc592012-07-17 16:49:40 +0000466 uint32_t flags = SkFlattenableWriteBuffer::kInlineFactoryNames_Flag
467 | SkFlattenableWriteBuffer::kCrossProcess_Flag;
468 bool added, replaced;
469 const SkFlatData* flat = fBitmapDictionary.findAndReplace(
470 bitmap, flags, fBitmapHeap.flatToReplace(), &added, &replaced);
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000471
scroggo@google.com4dffc592012-07-17 16:49:40 +0000472 int index = flat->index();
473 if (added && this->needOpBytes(flat->flatSize())) {
474 this->writeOp(kDef_Bitmap_DrawOp, 0, index);
475 fWriter.write(flat->data(), flat->flatSize());
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000476 }
scroggo@google.com4dffc592012-07-17 16:49:40 +0000477 return index;
scroggo@google.comd3ba5cc2012-07-09 16:05:53 +0000478}
479
reed@google.comb55d1182011-05-11 00:42:04 +0000480// return 0 for NULL (or unflattenable obj), or index-base-1
scroggo@google.comd3ba5cc2012-07-09 16:05:53 +0000481// return ~(index-base-1) if an old flattenable was replaced
reed@google.comb55d1182011-05-11 00:42:04 +0000482int SkGPipeCanvas::flattenToIndex(SkFlattenable* obj, PaintFlats paintflat) {
483 if (NULL == obj) {
484 return 0;
485 }
reed@google.comb55d1182011-05-11 00:42:04 +0000486
scroggo@google.com4dffc592012-07-17 16:49:40 +0000487 uint32_t writeBufferFlags;
scroggo@google.com565254b2012-06-28 15:41:32 +0000488 if (fFlags & SkGPipeWriter::kCrossProcess_Flag) {
scroggo@google.com4dffc592012-07-17 16:49:40 +0000489 writeBufferFlags = (SkFlattenableWriteBuffer::kInlineFactoryNames_Flag
490 | SkFlattenableWriteBuffer::kCrossProcess_Flag);
scroggo@google.com565254b2012-06-28 15:41:32 +0000491 } else {
492 // Needed for bitmap shaders.
scroggo@google.com4dffc592012-07-17 16:49:40 +0000493 writeBufferFlags = SkFlattenableWriteBuffer::kForceFlattenBitmapPixels_Flag;
scroggo@google.com565254b2012-06-28 15:41:32 +0000494 }
reed@google.comdde09562011-05-23 12:21:05 +0000495
scroggo@google.com4dffc592012-07-17 16:49:40 +0000496 bool added, replaced;
497 const SkFlatData* flat = fFlatDictionary.findAndReplace(
498 *obj, writeBufferFlags, fFlattenableHeap.flatToReplace(), &added, &replaced);
499 int index = flat->index();
500 if (added && this->needOpBytes(flat->flatSize())) {
501 this->writeOp(kDef_Flattenable_DrawOp, paintflat, index);
502 fWriter.write(flat->data(), flat->flatSize());
503 }
504 if (replaced) {
reed@google.comb55d1182011-05-11 00:42:04 +0000505 index = ~index;
reed@google.comb55d1182011-05-11 00:42:04 +0000506 }
scroggo@google.com4dffc592012-07-17 16:49:40 +0000507 return index;
reed@google.comb55d1182011-05-11 00:42:04 +0000508}
509
reed@google.combb6992a2011-04-26 17:41:56 +0000510///////////////////////////////////////////////////////////////////////////////
511
reed@google.comacd471f2011-05-03 21:26:46 +0000512#define MIN_BLOCK_SIZE (16 * 1024)
scroggo@google.com4dffc592012-07-17 16:49:40 +0000513#define BITMAPS_TO_KEEP 5
514#define FLATTENABLES_TO_KEEP 10
reed@google.combb6992a2011-04-26 17:41:56 +0000515
reed@google.comacd471f2011-05-03 21:26:46 +0000516SkGPipeCanvas::SkGPipeCanvas(SkGPipeController* controller,
scroggo@google.com565254b2012-06-28 15:41:32 +0000517 SkWriter32* writer, SkFactorySet* fset, uint32_t flags)
scroggo@google.com4dffc592012-07-17 16:49:40 +0000518: fSharedHeap(!(flags & SkGPipeWriter::kCrossProcess_Flag), controller->numberOfReaders())
519, fWriter(*writer), fFlags(flags)
520, fBitmapHeap(BITMAPS_TO_KEEP), fBitmapDictionary(&fBitmapHeap, NULL, NULL, fset)
521, fFlattenableHeap(FLATTENABLES_TO_KEEP), fFlatDictionary(&fFlattenableHeap, fset) {
reed@google.comacd471f2011-05-03 21:26:46 +0000522 fController = controller;
reed@google.combb6992a2011-04-26 17:41:56 +0000523 fDone = false;
reed@google.comacd471f2011-05-03 21:26:46 +0000524 fBlockSize = 0; // need first block from controller
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000525 fBytesNotified = 0;
reed@google.comb55d1182011-05-11 00:42:04 +0000526 sk_bzero(fCurrFlatIndex, sizeof(fCurrFlatIndex));
reed@google.comacd471f2011-05-03 21:26:46 +0000527
reed@google.combb6793b2011-05-05 15:18:15 +0000528 // we need a device to limit our clip
529 // should the caller give us the bounds?
yangsu@google.com06b4da162011-06-17 15:04:40 +0000530 // We don't allocate pixels for the bitmap
reed@google.combb6793b2011-05-05 15:18:15 +0000531 SkBitmap bitmap;
532 bitmap.setConfig(SkBitmap::kARGB_8888_Config, 32767, 32767);
yangsu@google.com06b4da162011-06-17 15:04:40 +0000533 SkDevice* device = SkNEW_ARGS(SkDevice, (bitmap));
reed@google.combb6793b2011-05-05 15:18:15 +0000534 this->setDevice(device)->unref();
scroggo@google.com565254b2012-06-28 15:41:32 +0000535 // Tell the reader the appropriate flags to use.
536 if (this->needOpBytes()) {
537 this->writeOp(kReportFlags_DrawOp, fFlags, 0);
538 }
reed@google.combb6992a2011-04-26 17:41:56 +0000539}
540
541SkGPipeCanvas::~SkGPipeCanvas() {
542 this->finish();
reed@google.combb6992a2011-04-26 17:41:56 +0000543}
544
reed@google.comacd471f2011-05-03 21:26:46 +0000545bool SkGPipeCanvas::needOpBytes(size_t needed) {
546 if (fDone) {
547 return false;
548 }
549
550 needed += 4; // size of DrawOp atom
551 if (fWriter.size() + needed > fBlockSize) {
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000552 // Before we wipe out any data that has already been written, read it
553 // out.
554 this->doNotify();
555 size_t blockSize = SkMax32(MIN_BLOCK_SIZE, needed);
556 void* block = fController->requestBlock(blockSize, &fBlockSize);
reed@google.comacd471f2011-05-03 21:26:46 +0000557 if (NULL == block) {
558 fDone = true;
559 return false;
560 }
561 fWriter.reset(block, fBlockSize);
562 fBytesNotified = 0;
563 }
564 return true;
565}
566
reed@google.comf5842f72011-05-04 18:30:04 +0000567uint32_t SkGPipeCanvas::getTypefaceID(SkTypeface* face) {
568 uint32_t id = 0; // 0 means default/null typeface
569 if (face) {
570 id = fTypefaceSet.find(face);
571 if (0 == id) {
572 id = fTypefaceSet.add(face);
573 size_t size = writeTypeface(NULL, face);
574 if (this->needOpBytes(size)) {
reed@google.combb6793b2011-05-05 15:18:15 +0000575 this->writeOp(kDef_Typeface_DrawOp);
reed@google.comf5842f72011-05-04 18:30:04 +0000576 writeTypeface(&fWriter, face);
577 }
578 }
579 }
580 return id;
581}
582
reed@google.combb6992a2011-04-26 17:41:56 +0000583///////////////////////////////////////////////////////////////////////////////
584
reed@google.comacd471f2011-05-03 21:26:46 +0000585#define NOTIFY_SETUP(canvas) \
586 AutoPipeNotify apn(canvas)
587
reed@google.combb6992a2011-04-26 17:41:56 +0000588int SkGPipeCanvas::save(SaveFlags flags) {
reed@google.comacd471f2011-05-03 21:26:46 +0000589 NOTIFY_SETUP(this);
590 if (this->needOpBytes()) {
591 this->writeOp(kSave_DrawOp, 0, flags);
592 }
reed@google.combb6992a2011-04-26 17:41:56 +0000593 return this->INHERITED::save(flags);
594}
595
596int SkGPipeCanvas::saveLayer(const SkRect* bounds, const SkPaint* paint,
597 SaveFlags saveFlags) {
reed@google.comacd471f2011-05-03 21:26:46 +0000598 NOTIFY_SETUP(this);
599 size_t size = 0;
reed@google.combb6992a2011-04-26 17:41:56 +0000600 unsigned opFlags = 0;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000601
reed@google.combb6992a2011-04-26 17:41:56 +0000602 if (bounds) {
603 opFlags |= kSaveLayer_HasBounds_DrawOpFlag;
reed@google.comacd471f2011-05-03 21:26:46 +0000604 size += sizeof(SkRect);
reed@google.combb6992a2011-04-26 17:41:56 +0000605 }
606 if (paint) {
607 opFlags |= kSaveLayer_HasPaint_DrawOpFlag;
reed@google.com31891582011-05-12 03:03:56 +0000608 this->writePaint(*paint);
reed@google.combb6992a2011-04-26 17:41:56 +0000609 }
610
reed@google.comacd471f2011-05-03 21:26:46 +0000611 if (this->needOpBytes(size)) {
612 this->writeOp(kSaveLayer_DrawOp, opFlags, saveFlags);
613 if (bounds) {
614 fWriter.writeRect(*bounds);
615 }
reed@google.combb6992a2011-04-26 17:41:56 +0000616 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000617
reed@google.combb6992a2011-04-26 17:41:56 +0000618 // we just pass on the save, so we don't create a layer
619 return this->INHERITED::save(saveFlags);
620}
621
622void SkGPipeCanvas::restore() {
reed@google.comacd471f2011-05-03 21:26:46 +0000623 NOTIFY_SETUP(this);
624 if (this->needOpBytes()) {
625 this->writeOp(kRestore_DrawOp);
626 }
reed@google.combb6992a2011-04-26 17:41:56 +0000627 this->INHERITED::restore();
628}
629
630bool SkGPipeCanvas::translate(SkScalar dx, SkScalar dy) {
631 if (dx || dy) {
reed@google.comacd471f2011-05-03 21:26:46 +0000632 NOTIFY_SETUP(this);
633 if (this->needOpBytes(2 * sizeof(SkScalar))) {
634 this->writeOp(kTranslate_DrawOp);
635 fWriter.writeScalar(dx);
636 fWriter.writeScalar(dy);
637 }
reed@google.combb6992a2011-04-26 17:41:56 +0000638 }
639 return this->INHERITED::translate(dx, dy);
640}
641
642bool SkGPipeCanvas::scale(SkScalar sx, SkScalar sy) {
643 if (sx || sy) {
reed@google.comacd471f2011-05-03 21:26:46 +0000644 NOTIFY_SETUP(this);
645 if (this->needOpBytes(2 * sizeof(SkScalar))) {
646 this->writeOp(kScale_DrawOp);
647 fWriter.writeScalar(sx);
648 fWriter.writeScalar(sy);
649 }
reed@google.combb6992a2011-04-26 17:41:56 +0000650 }
651 return this->INHERITED::scale(sx, sy);
652}
653
654bool SkGPipeCanvas::rotate(SkScalar degrees) {
655 if (degrees) {
reed@google.comacd471f2011-05-03 21:26:46 +0000656 NOTIFY_SETUP(this);
657 if (this->needOpBytes(sizeof(SkScalar))) {
658 this->writeOp(kRotate_DrawOp);
659 fWriter.writeScalar(degrees);
660 }
reed@google.combb6992a2011-04-26 17:41:56 +0000661 }
662 return this->INHERITED::rotate(degrees);
663}
664
665bool SkGPipeCanvas::skew(SkScalar sx, SkScalar sy) {
666 if (sx || sy) {
reed@google.comacd471f2011-05-03 21:26:46 +0000667 NOTIFY_SETUP(this);
668 if (this->needOpBytes(2 * sizeof(SkScalar))) {
669 this->writeOp(kSkew_DrawOp);
670 fWriter.writeScalar(sx);
671 fWriter.writeScalar(sy);
672 }
reed@google.combb6992a2011-04-26 17:41:56 +0000673 }
674 return this->INHERITED::skew(sx, sy);
675}
676
677bool SkGPipeCanvas::concat(const SkMatrix& matrix) {
678 if (!matrix.isIdentity()) {
reed@google.comacd471f2011-05-03 21:26:46 +0000679 NOTIFY_SETUP(this);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000680 if (this->needOpBytes(matrix.writeToMemory(NULL))) {
reed@google.comacd471f2011-05-03 21:26:46 +0000681 this->writeOp(kConcat_DrawOp);
djsollen@google.com2b2ede32012-04-12 13:24:04 +0000682 fWriter.writeMatrix(matrix);
reed@google.comacd471f2011-05-03 21:26:46 +0000683 }
reed@google.combb6992a2011-04-26 17:41:56 +0000684 }
685 return this->INHERITED::concat(matrix);
686}
687
688void SkGPipeCanvas::setMatrix(const SkMatrix& matrix) {
reed@google.comacd471f2011-05-03 21:26:46 +0000689 NOTIFY_SETUP(this);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000690 if (this->needOpBytes(matrix.writeToMemory(NULL))) {
reed@google.comacd471f2011-05-03 21:26:46 +0000691 this->writeOp(kSetMatrix_DrawOp);
djsollen@google.com2b2ede32012-04-12 13:24:04 +0000692 fWriter.writeMatrix(matrix);
reed@google.comacd471f2011-05-03 21:26:46 +0000693 }
reed@google.combb6992a2011-04-26 17:41:56 +0000694 this->INHERITED::setMatrix(matrix);
695}
696
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000697bool SkGPipeCanvas::clipRect(const SkRect& rect, SkRegion::Op rgnOp,
698 bool doAntiAlias) {
reed@google.comacd471f2011-05-03 21:26:46 +0000699 NOTIFY_SETUP(this);
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000700 if (this->needOpBytes(sizeof(SkRect)) + sizeof(bool)) {
reed@google.comacd471f2011-05-03 21:26:46 +0000701 this->writeOp(kClipRect_DrawOp, 0, rgnOp);
702 fWriter.writeRect(rect);
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000703 fWriter.writeBool(doAntiAlias);
reed@google.comacd471f2011-05-03 21:26:46 +0000704 }
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000705 return this->INHERITED::clipRect(rect, rgnOp, doAntiAlias);
reed@google.combb6992a2011-04-26 17:41:56 +0000706}
707
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000708bool SkGPipeCanvas::clipPath(const SkPath& path, SkRegion::Op rgnOp,
709 bool doAntiAlias) {
reed@google.comacd471f2011-05-03 21:26:46 +0000710 NOTIFY_SETUP(this);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000711 if (this->needOpBytes(path.writeToMemory(NULL)) + sizeof(bool)) {
reed@google.comacd471f2011-05-03 21:26:46 +0000712 this->writeOp(kClipPath_DrawOp, 0, rgnOp);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000713 fWriter.writePath(path);
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000714 fWriter.writeBool(doAntiAlias);
reed@google.comacd471f2011-05-03 21:26:46 +0000715 }
reed@google.combb6992a2011-04-26 17:41:56 +0000716 // we just pass on the bounds of the path
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000717 return this->INHERITED::clipRect(path.getBounds(), rgnOp, doAntiAlias);
reed@google.combb6992a2011-04-26 17:41:56 +0000718}
719
720bool SkGPipeCanvas::clipRegion(const SkRegion& region, SkRegion::Op rgnOp) {
reed@google.comacd471f2011-05-03 21:26:46 +0000721 NOTIFY_SETUP(this);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000722 if (this->needOpBytes(region.writeToMemory(NULL))) {
reed@google.comacd471f2011-05-03 21:26:46 +0000723 this->writeOp(kClipRegion_DrawOp, 0, rgnOp);
djsollen@google.com2b2ede32012-04-12 13:24:04 +0000724 fWriter.writeRegion(region);
reed@google.comacd471f2011-05-03 21:26:46 +0000725 }
reed@google.combb6992a2011-04-26 17:41:56 +0000726 return this->INHERITED::clipRegion(region, rgnOp);
727}
728
729///////////////////////////////////////////////////////////////////////////////
730
731void SkGPipeCanvas::clear(SkColor color) {
reed@google.comacd471f2011-05-03 21:26:46 +0000732 NOTIFY_SETUP(this);
reed@google.combb6992a2011-04-26 17:41:56 +0000733 unsigned flags = 0;
734 if (color) {
735 flags |= kClear_HasColor_DrawOpFlag;
736 }
reed@google.comacd471f2011-05-03 21:26:46 +0000737 if (this->needOpBytes(sizeof(SkColor))) {
738 this->writeOp(kDrawClear_DrawOp, flags, 0);
739 if (color) {
740 fWriter.write32(color);
741 }
reed@google.combb6992a2011-04-26 17:41:56 +0000742 }
743}
744
745void SkGPipeCanvas::drawPaint(const SkPaint& paint) {
reed@google.comacd471f2011-05-03 21:26:46 +0000746 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000747 this->writePaint(paint);
reed@google.comacd471f2011-05-03 21:26:46 +0000748 if (this->needOpBytes()) {
reed@google.com31891582011-05-12 03:03:56 +0000749 this->writeOp(kDrawPaint_DrawOp);
reed@google.comacd471f2011-05-03 21:26:46 +0000750 }
reed@google.combb6992a2011-04-26 17:41:56 +0000751}
752
753void SkGPipeCanvas::drawPoints(PointMode mode, size_t count,
754 const SkPoint pts[], const SkPaint& paint) {
755 if (count) {
reed@google.comacd471f2011-05-03 21:26:46 +0000756 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000757 this->writePaint(paint);
reed@google.comacd471f2011-05-03 21:26:46 +0000758 if (this->needOpBytes(4 + count * sizeof(SkPoint))) {
reed@google.com31891582011-05-12 03:03:56 +0000759 this->writeOp(kDrawPoints_DrawOp, mode, 0);
reed@google.comacd471f2011-05-03 21:26:46 +0000760 fWriter.write32(count);
761 fWriter.write(pts, count * sizeof(SkPoint));
762 }
reed@google.combb6992a2011-04-26 17:41:56 +0000763 }
764}
765
766void SkGPipeCanvas::drawRect(const SkRect& rect, const SkPaint& paint) {
reed@google.comacd471f2011-05-03 21:26:46 +0000767 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000768 this->writePaint(paint);
reed@google.comacd471f2011-05-03 21:26:46 +0000769 if (this->needOpBytes(sizeof(SkRect))) {
reed@google.com31891582011-05-12 03:03:56 +0000770 this->writeOp(kDrawRect_DrawOp);
reed@google.comacd471f2011-05-03 21:26:46 +0000771 fWriter.writeRect(rect);
772 }
reed@google.combb6992a2011-04-26 17:41:56 +0000773}
774
775void SkGPipeCanvas::drawPath(const SkPath& path, const SkPaint& paint) {
reed@google.comacd471f2011-05-03 21:26:46 +0000776 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000777 this->writePaint(paint);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000778 if (this->needOpBytes(path.writeToMemory(NULL))) {
reed@google.com31891582011-05-12 03:03:56 +0000779 this->writeOp(kDrawPath_DrawOp);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000780 fWriter.writePath(path);
reed@google.comacd471f2011-05-03 21:26:46 +0000781 }
reed@google.combb6992a2011-04-26 17:41:56 +0000782}
783
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000784void SkGPipeCanvas::drawBitmap(const SkBitmap& bm, SkScalar left, SkScalar top,
785 const SkPaint* paint) {
scroggo@google.com565254b2012-06-28 15:41:32 +0000786 bool flatten = shouldFlattenBitmaps(fFlags);
787 const void* ptr = 0;
788 int bitmapIndex = 0;
789 if (flatten) {
790 bitmapIndex = this->flattenToIndex(bm);
791 } else {
scroggo@google.com4dffc592012-07-17 16:49:40 +0000792 ptr = fSharedHeap.addBitmap(bm);
scroggo@google.com565254b2012-06-28 15:41:32 +0000793 if (NULL == ptr) {
794 return;
795 }
scroggo@google.com8ae3c7f2012-06-13 17:47:52 +0000796 }
797
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000798 NOTIFY_SETUP(this);
799 if (paint) {
800 this->writePaint(*paint);
801 }
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000802
scroggo@google.com565254b2012-06-28 15:41:32 +0000803 size_t opBytesNeeded = sizeof(SkScalar) * 2 + sizeof(bool);
804 if (!flatten) {
805 opBytesNeeded += sizeof(void*);
806 }
807 if (this->needOpBytes(opBytesNeeded)) {
808 this->writeOp(kDrawBitmap_DrawOp, 0, bitmapIndex);
809 if (!flatten) {
810 fWriter.writePtr(const_cast<void*>(ptr));
811 }
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000812 fWriter.writeBool(paint != NULL);
813 fWriter.writeScalar(left);
814 fWriter.writeScalar(top);
815 }
reed@google.combb6992a2011-04-26 17:41:56 +0000816}
817
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000818void SkGPipeCanvas::drawBitmapRect(const SkBitmap& bm, const SkIRect* src,
819 const SkRect& dst, const SkPaint* paint) {
scroggo@google.com565254b2012-06-28 15:41:32 +0000820 bool flatten = shouldFlattenBitmaps(fFlags);
821 const void* ptr = 0;
822 int bitmapIndex = 0;
823 if (flatten) {
824 bitmapIndex = this->flattenToIndex(bm);
825 } else {
scroggo@google.com4dffc592012-07-17 16:49:40 +0000826 ptr = fSharedHeap.addBitmap(bm);
scroggo@google.com565254b2012-06-28 15:41:32 +0000827 if (NULL == ptr) {
828 return;
829 }
scroggo@google.com8ae3c7f2012-06-13 17:47:52 +0000830 }
831
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000832 NOTIFY_SETUP(this);
833 if (paint) {
834 this->writePaint(*paint);
835 }
836
scroggo@google.com565254b2012-06-28 15:41:32 +0000837 size_t opBytesNeeded = sizeof(SkRect) + sizeof(bool) * 2;
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000838 bool hasSrc = src != NULL;
839 if (hasSrc) {
840 opBytesNeeded += sizeof(int32_t) * 4;
841 }
scroggo@google.com565254b2012-06-28 15:41:32 +0000842 if (!flatten) {
843 opBytesNeeded += sizeof(void*);
844 }
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000845 if (this->needOpBytes(opBytesNeeded)) {
scroggo@google.com565254b2012-06-28 15:41:32 +0000846 this->writeOp(kDrawBitmapRect_DrawOp, 0, bitmapIndex);
847 if (!flatten) {
848 fWriter.writePtr(const_cast<void*>(ptr));
849 }
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000850 fWriter.writeBool(paint != NULL);
851 fWriter.writeBool(hasSrc);
852 if (hasSrc) {
853 fWriter.write32(src->fLeft);
854 fWriter.write32(src->fTop);
855 fWriter.write32(src->fRight);
856 fWriter.write32(src->fBottom);
857 }
858 fWriter.writeRect(dst);
859 }
reed@google.combb6992a2011-04-26 17:41:56 +0000860}
861
862void SkGPipeCanvas::drawBitmapMatrix(const SkBitmap&, const SkMatrix&,
863 const SkPaint*) {
864 UNIMPLEMENTED
865}
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000866
867void SkGPipeCanvas::drawBitmapNine(const SkBitmap& bm, const SkIRect& center,
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000868 const SkRect& dst, const SkPaint* paint) {
scroggo@google.com565254b2012-06-28 15:41:32 +0000869 bool flatten = shouldFlattenBitmaps(fFlags);
870 const void* ptr = 0;
871 int bitmapIndex = 0;
872 if (flatten) {
873 bitmapIndex = this->flattenToIndex(bm);
874 } else {
scroggo@google.com4dffc592012-07-17 16:49:40 +0000875 ptr = fSharedHeap.addBitmap(bm);
scroggo@google.com565254b2012-06-28 15:41:32 +0000876 if (NULL == ptr) {
877 return;
878 }
scroggo@google.com8ae3c7f2012-06-13 17:47:52 +0000879 }
880
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000881 NOTIFY_SETUP(this);
882 if (paint) {
883 this->writePaint(*paint);
884 }
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000885
scroggo@google.com565254b2012-06-28 15:41:32 +0000886 size_t opBytesNeeded = sizeof(int32_t) * 4 + sizeof(bool) + sizeof(SkRect);
887 if (!flatten) {
888 opBytesNeeded += sizeof(void*);
889 }
890 if (this->needOpBytes(opBytesNeeded)) {
891 this->writeOp(kDrawBitmapNine_DrawOp, 0, bitmapIndex);
892 if (!flatten) {
893 fWriter.writePtr(const_cast<void*>(ptr));
894 }
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000895 fWriter.writeBool(paint != NULL);
896 fWriter.write32(center.fLeft);
897 fWriter.write32(center.fTop);
898 fWriter.write32(center.fRight);
899 fWriter.write32(center.fBottom);
900 fWriter.writeRect(dst);
901 }
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000902}
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000903
904void SkGPipeCanvas::drawSprite(const SkBitmap& bm, int left, int top,
905 const SkPaint* paint) {
scroggo@google.com565254b2012-06-28 15:41:32 +0000906 bool flatten = shouldFlattenBitmaps(fFlags);
907 const void* ptr = 0;
908 int bitmapIndex = 0;
909 if (flatten) {
910 bitmapIndex = this->flattenToIndex(bm);
911 } else {
scroggo@google.com4dffc592012-07-17 16:49:40 +0000912 ptr = fSharedHeap.addBitmap(bm);
scroggo@google.com565254b2012-06-28 15:41:32 +0000913 if (NULL == ptr) {
914 return;
915 }
scroggo@google.com8ae3c7f2012-06-13 17:47:52 +0000916 }
917
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000918 NOTIFY_SETUP(this);
919 if (paint) {
920 this->writePaint(*paint);
921 }
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000922
scroggo@google.com565254b2012-06-28 15:41:32 +0000923 size_t opBytesNeeded = sizeof(int32_t) * 2 + sizeof(bool);
924 if (!flatten) {
925 opBytesNeeded += sizeof(void*);
926 }
927 if (this->needOpBytes(opBytesNeeded)) {
928 this->writeOp(kDrawSprite_DrawOp, 0, bitmapIndex);
929 if (!flatten) {
930 fWriter.writePtr(const_cast<void*>(ptr));
931 }
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000932 fWriter.writeBool(paint != NULL);
933 fWriter.write32(left);
934 fWriter.write32(top);
935 }
reed@google.combb6992a2011-04-26 17:41:56 +0000936}
937
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000938void SkGPipeCanvas::drawText(const void* text, size_t byteLength, SkScalar x,
reed@google.combb6992a2011-04-26 17:41:56 +0000939 SkScalar y, const SkPaint& paint) {
940 if (byteLength) {
reed@google.comacd471f2011-05-03 21:26:46 +0000941 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000942 this->writePaint(paint);
reed@google.comacd471f2011-05-03 21:26:46 +0000943 if (this->needOpBytes(4 + SkAlign4(byteLength) + 2 * sizeof(SkScalar))) {
reed@google.com31891582011-05-12 03:03:56 +0000944 this->writeOp(kDrawText_DrawOp);
reed@google.comacd471f2011-05-03 21:26:46 +0000945 fWriter.write32(byteLength);
946 fWriter.writePad(text, byteLength);
947 fWriter.writeScalar(x);
948 fWriter.writeScalar(y);
949 }
reed@google.combb6992a2011-04-26 17:41:56 +0000950 }
951}
952
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000953void SkGPipeCanvas::drawPosText(const void* text, size_t byteLength,
reed@google.combb6992a2011-04-26 17:41:56 +0000954 const SkPoint pos[], const SkPaint& paint) {
955 if (byteLength) {
reed@google.comacd471f2011-05-03 21:26:46 +0000956 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000957 this->writePaint(paint);
reed@google.combb6992a2011-04-26 17:41:56 +0000958 int count = paint.textToGlyphs(text, byteLength, NULL);
reed@google.comacd471f2011-05-03 21:26:46 +0000959 if (this->needOpBytes(4 + SkAlign4(byteLength) + 4 + count * sizeof(SkPoint))) {
reed@google.com31891582011-05-12 03:03:56 +0000960 this->writeOp(kDrawPosText_DrawOp);
reed@google.comacd471f2011-05-03 21:26:46 +0000961 fWriter.write32(byteLength);
962 fWriter.writePad(text, byteLength);
963 fWriter.write32(count);
964 fWriter.write(pos, count * sizeof(SkPoint));
965 }
reed@google.combb6992a2011-04-26 17:41:56 +0000966 }
967}
968
969void SkGPipeCanvas::drawPosTextH(const void* text, size_t byteLength,
970 const SkScalar xpos[], SkScalar constY,
971 const SkPaint& paint) {
972 if (byteLength) {
reed@google.comacd471f2011-05-03 21:26:46 +0000973 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000974 this->writePaint(paint);
reed@google.combb6992a2011-04-26 17:41:56 +0000975 int count = paint.textToGlyphs(text, byteLength, NULL);
reed@google.comacd471f2011-05-03 21:26:46 +0000976 if (this->needOpBytes(4 + SkAlign4(byteLength) + 4 + count * sizeof(SkScalar) + 4)) {
reed@google.com31891582011-05-12 03:03:56 +0000977 this->writeOp(kDrawPosTextH_DrawOp);
reed@google.comacd471f2011-05-03 21:26:46 +0000978 fWriter.write32(byteLength);
979 fWriter.writePad(text, byteLength);
980 fWriter.write32(count);
981 fWriter.write(xpos, count * sizeof(SkScalar));
982 fWriter.writeScalar(constY);
983 }
reed@google.combb6992a2011-04-26 17:41:56 +0000984 }
985}
986
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000987void SkGPipeCanvas::drawTextOnPath(const void* text, size_t byteLength,
988 const SkPath& path, const SkMatrix* matrix,
reed@google.combb6992a2011-04-26 17:41:56 +0000989 const SkPaint& paint) {
990 if (byteLength) {
reed@google.comacd471f2011-05-03 21:26:46 +0000991 NOTIFY_SETUP(this);
reed@google.combb6992a2011-04-26 17:41:56 +0000992 unsigned flags = 0;
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000993 size_t size = 4 + SkAlign4(byteLength) + path.writeToMemory(NULL);
reed@google.combb6992a2011-04-26 17:41:56 +0000994 if (matrix) {
995 flags |= kDrawTextOnPath_HasMatrix_DrawOpFlag;
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000996 size += matrix->writeToMemory(NULL);
reed@google.combb6992a2011-04-26 17:41:56 +0000997 }
reed@google.com31891582011-05-12 03:03:56 +0000998 this->writePaint(paint);
reed@google.comacd471f2011-05-03 21:26:46 +0000999 if (this->needOpBytes(size)) {
reed@google.com31891582011-05-12 03:03:56 +00001000 this->writeOp(kDrawTextOnPath_DrawOp, flags, 0);
reed@google.combb6992a2011-04-26 17:41:56 +00001001
reed@google.comacd471f2011-05-03 21:26:46 +00001002 fWriter.write32(byteLength);
1003 fWriter.writePad(text, byteLength);
reed@google.combb6992a2011-04-26 17:41:56 +00001004
djsollen@google.com94e75ee2012-06-08 18:30:46 +00001005 fWriter.writePath(path);
reed@google.comacd471f2011-05-03 21:26:46 +00001006 if (matrix) {
djsollen@google.com2b2ede32012-04-12 13:24:04 +00001007 fWriter.writeMatrix(*matrix);
reed@google.comacd471f2011-05-03 21:26:46 +00001008 }
reed@google.combb6992a2011-04-26 17:41:56 +00001009 }
1010 }
1011}
1012
1013void SkGPipeCanvas::drawPicture(SkPicture& picture) {
reed@google.com0faac1e2011-05-11 05:58:58 +00001014 // we want to playback the picture into individual draw calls
1015 this->INHERITED::drawPicture(picture);
reed@google.combb6992a2011-04-26 17:41:56 +00001016}
1017
reed@google.combb6992a2011-04-26 17:41:56 +00001018void SkGPipeCanvas::drawVertices(VertexMode mode, int vertexCount,
1019 const SkPoint vertices[], const SkPoint texs[],
1020 const SkColor colors[], SkXfermode*,
1021 const uint16_t indices[], int indexCount,
1022 const SkPaint& paint) {
1023 if (0 == vertexCount) {
1024 return;
1025 }
1026
reed@google.comacd471f2011-05-03 21:26:46 +00001027 NOTIFY_SETUP(this);
1028 size_t size = 4 + vertexCount * sizeof(SkPoint);
reed@google.com31891582011-05-12 03:03:56 +00001029 this->writePaint(paint);
reed@google.combb6992a2011-04-26 17:41:56 +00001030 unsigned flags = 0;
1031 if (texs) {
1032 flags |= kDrawVertices_HasTexs_DrawOpFlag;
reed@google.comacd471f2011-05-03 21:26:46 +00001033 size += vertexCount * sizeof(SkPoint);
reed@google.combb6992a2011-04-26 17:41:56 +00001034 }
1035 if (colors) {
1036 flags |= kDrawVertices_HasColors_DrawOpFlag;
reed@google.comacd471f2011-05-03 21:26:46 +00001037 size += vertexCount * sizeof(SkColor);
reed@google.combb6992a2011-04-26 17:41:56 +00001038 }
1039 if (indices && indexCount > 0) {
1040 flags |= kDrawVertices_HasIndices_DrawOpFlag;
reed@google.comacd471f2011-05-03 21:26:46 +00001041 size += 4 + SkAlign4(indexCount * sizeof(uint16_t));
reed@google.combb6992a2011-04-26 17:41:56 +00001042 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001043
reed@google.comacd471f2011-05-03 21:26:46 +00001044 if (this->needOpBytes(size)) {
reed@google.com31891582011-05-12 03:03:56 +00001045 this->writeOp(kDrawVertices_DrawOp, flags, 0);
reed@google.comacd471f2011-05-03 21:26:46 +00001046 fWriter.write32(mode);
1047 fWriter.write32(vertexCount);
1048 fWriter.write(vertices, vertexCount * sizeof(SkPoint));
1049 if (texs) {
1050 fWriter.write(texs, vertexCount * sizeof(SkPoint));
1051 }
1052 if (colors) {
1053 fWriter.write(colors, vertexCount * sizeof(SkColor));
1054 }
reed@google.combb6992a2011-04-26 17:41:56 +00001055
reed@google.comacd471f2011-05-03 21:26:46 +00001056 // TODO: flatten xfermode
reed@google.combb6992a2011-04-26 17:41:56 +00001057
reed@google.comacd471f2011-05-03 21:26:46 +00001058 if (indices && indexCount > 0) {
1059 fWriter.write32(indexCount);
1060 fWriter.writePad(indices, indexCount * sizeof(uint16_t));
1061 }
reed@google.combb6992a2011-04-26 17:41:56 +00001062 }
1063}
1064
reed@google.comacd471f2011-05-03 21:26:46 +00001065void SkGPipeCanvas::drawData(const void* ptr, size_t size) {
1066 if (size && ptr) {
1067 NOTIFY_SETUP(this);
reed@google.combb6992a2011-04-26 17:41:56 +00001068 unsigned data = 0;
1069 if (size < (1 << DRAWOPS_DATA_BITS)) {
1070 data = (unsigned)size;
1071 }
reed@google.comacd471f2011-05-03 21:26:46 +00001072 if (this->needOpBytes(4 + SkAlign4(size))) {
1073 this->writeOp(kDrawData_DrawOp, 0, data);
1074 if (0 == data) {
1075 fWriter.write32(size);
1076 }
reed@google.combb6793b2011-05-05 15:18:15 +00001077 fWriter.writePad(ptr, size);
reed@google.combb6992a2011-04-26 17:41:56 +00001078 }
1079 }
1080}
1081
junov@chromium.org77eec242012-07-18 17:54:45 +00001082void SkGPipeCanvas::flushRecording(bool detachCurrentBlock) {
1083 doNotify();
1084 if (detachCurrentBlock) {
1085 // force a new block to be requested for the next recorded command
1086 fBlockSize = 0;
1087 }
1088}
1089
reed@google.combb6992a2011-04-26 17:41:56 +00001090///////////////////////////////////////////////////////////////////////////////
1091
1092template <typename T> uint32_t castToU32(T value) {
1093 union {
1094 T fSrc;
1095 uint32_t fDst;
1096 } data;
1097 data.fSrc = value;
1098 return data.fDst;
1099}
1100
reed@google.com31891582011-05-12 03:03:56 +00001101void SkGPipeCanvas::writePaint(const SkPaint& paint) {
1102 SkPaint& base = fPaint;
reed@google.combb6992a2011-04-26 17:41:56 +00001103 uint32_t storage[32];
1104 uint32_t* ptr = storage;
reed@google.combb6992a2011-04-26 17:41:56 +00001105
1106 if (base.getFlags() != paint.getFlags()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001107 *ptr++ = PaintOp_packOpData(kFlags_PaintOp, paint.getFlags());
reed@google.comf5842f72011-05-04 18:30:04 +00001108 base.setFlags(paint.getFlags());
reed@google.combb6992a2011-04-26 17:41:56 +00001109 }
1110 if (base.getColor() != paint.getColor()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001111 *ptr++ = PaintOp_packOp(kColor_PaintOp);
1112 *ptr++ = paint.getColor();
reed@google.comf5842f72011-05-04 18:30:04 +00001113 base.setColor(paint.getColor());
reed@google.combb6992a2011-04-26 17:41:56 +00001114 }
1115 if (base.getStyle() != paint.getStyle()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001116 *ptr++ = PaintOp_packOpData(kStyle_PaintOp, paint.getStyle());
reed@google.comf5842f72011-05-04 18:30:04 +00001117 base.setStyle(paint.getStyle());
reed@google.combb6992a2011-04-26 17:41:56 +00001118 }
1119 if (base.getStrokeJoin() != paint.getStrokeJoin()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001120 *ptr++ = PaintOp_packOpData(kJoin_PaintOp, paint.getStrokeJoin());
reed@google.comf5842f72011-05-04 18:30:04 +00001121 base.setStrokeJoin(paint.getStrokeJoin());
reed@google.combb6992a2011-04-26 17:41:56 +00001122 }
1123 if (base.getStrokeCap() != paint.getStrokeCap()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001124 *ptr++ = PaintOp_packOpData(kCap_PaintOp, paint.getStrokeCap());
reed@google.comf5842f72011-05-04 18:30:04 +00001125 base.setStrokeCap(paint.getStrokeCap());
reed@google.combb6992a2011-04-26 17:41:56 +00001126 }
1127 if (base.getStrokeWidth() != paint.getStrokeWidth()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001128 *ptr++ = PaintOp_packOp(kWidth_PaintOp);
1129 *ptr++ = castToU32(paint.getStrokeWidth());
reed@google.comf5842f72011-05-04 18:30:04 +00001130 base.setStrokeWidth(paint.getStrokeWidth());
reed@google.combb6992a2011-04-26 17:41:56 +00001131 }
1132 if (base.getStrokeMiter() != paint.getStrokeMiter()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001133 *ptr++ = PaintOp_packOp(kMiter_PaintOp);
1134 *ptr++ = castToU32(paint.getStrokeMiter());
reed@google.comf5842f72011-05-04 18:30:04 +00001135 base.setStrokeMiter(paint.getStrokeMiter());
reed@google.combb6992a2011-04-26 17:41:56 +00001136 }
1137 if (base.getTextEncoding() != paint.getTextEncoding()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001138 *ptr++ = PaintOp_packOpData(kEncoding_PaintOp, paint.getTextEncoding());
reed@google.comf5842f72011-05-04 18:30:04 +00001139 base.setTextEncoding(paint.getTextEncoding());
reed@google.combb6992a2011-04-26 17:41:56 +00001140 }
1141 if (base.getHinting() != paint.getHinting()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001142 *ptr++ = PaintOp_packOpData(kHinting_PaintOp, paint.getHinting());
reed@google.comf5842f72011-05-04 18:30:04 +00001143 base.setHinting(paint.getHinting());
reed@google.combb6992a2011-04-26 17:41:56 +00001144 }
1145 if (base.getTextAlign() != paint.getTextAlign()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001146 *ptr++ = PaintOp_packOpData(kAlign_PaintOp, paint.getTextAlign());
reed@google.comf5842f72011-05-04 18:30:04 +00001147 base.setTextAlign(paint.getTextAlign());
reed@google.combb6992a2011-04-26 17:41:56 +00001148 }
1149 if (base.getTextSize() != paint.getTextSize()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001150 *ptr++ = PaintOp_packOp(kTextSize_PaintOp);
1151 *ptr++ = castToU32(paint.getTextSize());
reed@google.comf5842f72011-05-04 18:30:04 +00001152 base.setTextSize(paint.getTextSize());
reed@google.combb6992a2011-04-26 17:41:56 +00001153 }
1154 if (base.getTextScaleX() != paint.getTextScaleX()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001155 *ptr++ = PaintOp_packOp(kTextScaleX_PaintOp);
1156 *ptr++ = castToU32(paint.getTextScaleX());
reed@google.comf5842f72011-05-04 18:30:04 +00001157 base.setTextScaleX(paint.getTextScaleX());
reed@google.combb6992a2011-04-26 17:41:56 +00001158 }
1159 if (base.getTextSkewX() != paint.getTextSkewX()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001160 *ptr++ = PaintOp_packOp(kTextSkewX_PaintOp);
1161 *ptr++ = castToU32(paint.getTextSkewX());
reed@google.comf5842f72011-05-04 18:30:04 +00001162 base.setTextSkewX(paint.getTextSkewX());
1163 }
1164
1165 if (!SkTypeface::Equal(base.getTypeface(), paint.getTypeface())) {
1166 uint32_t id = this->getTypefaceID(paint.getTypeface());
reed@google.comf5842f72011-05-04 18:30:04 +00001167 *ptr++ = PaintOp_packOpData(kTypeface_PaintOp, id);
1168 base.setTypeface(paint.getTypeface());
reed@google.combb6992a2011-04-26 17:41:56 +00001169 }
reed@google.combb6992a2011-04-26 17:41:56 +00001170
scroggo@google.com4dffc592012-07-17 16:49:40 +00001171 // This is a new paint, so all old flats can be safely purged, if necessary.
1172 fFlattenableHeap.markAllFlatsSafeToDelete();
reed@google.comb55d1182011-05-11 00:42:04 +00001173 for (int i = 0; i < kCount_PaintFlats; i++) {
1174 int index = this->flattenToIndex(get_paintflat(paint, i), (PaintFlats)i);
scroggo@google.comd3ba5cc2012-07-09 16:05:53 +00001175 bool replaced = index < 0;
1176 if (replaced) {
1177 index = ~index;
1178 }
scroggo@google.com4dffc592012-07-17 16:49:40 +00001179 // Store the index of any flat that needs to be kept. 0 means no flat.
1180 if (index > 0) {
1181 fFlattenableHeap.markFlatForKeeping(index);
1182 }
1183 SkASSERT(index >= 0 && index <= fFlatDictionary.count());
scroggo@google.comd3ba5cc2012-07-09 16:05:53 +00001184 if (index != fCurrFlatIndex[i] || replaced) {
reed@google.comb55d1182011-05-11 00:42:04 +00001185 *ptr++ = PaintOp_packOpFlagData(kFlatIndex_PaintOp, i, index);
1186 fCurrFlatIndex[i] = index;
1187 }
1188 }
1189
reed@google.comacd471f2011-05-03 21:26:46 +00001190 size_t size = (char*)ptr - (char*)storage;
1191 if (size && this->needOpBytes(size)) {
reed@google.comb55d1182011-05-11 00:42:04 +00001192 this->writeOp(kPaintOp_DrawOp, 0, size);
reed@google.comb55d1182011-05-11 00:42:04 +00001193 fWriter.write(storage, size);
reed@google.combb6992a2011-04-26 17:41:56 +00001194 for (size_t i = 0; i < size/4; i++) {
reed@google.comb55d1182011-05-11 00:42:04 +00001195// SkDebugf("[%d] %08X\n", i, storage[i]);
reed@google.combb6992a2011-04-26 17:41:56 +00001196 }
1197 }
reed@google.combb6992a2011-04-26 17:41:56 +00001198}
1199
1200///////////////////////////////////////////////////////////////////////////////
1201
1202#include "SkGPipe.h"
1203
reed@google.comacd471f2011-05-03 21:26:46 +00001204SkGPipeWriter::SkGPipeWriter() : fWriter(0) {
reed@google.combb6992a2011-04-26 17:41:56 +00001205 fCanvas = NULL;
1206}
1207
1208SkGPipeWriter::~SkGPipeWriter() {
reed@google.comacd471f2011-05-03 21:26:46 +00001209 this->endRecording();
reed@google.combb6992a2011-04-26 17:41:56 +00001210 SkSafeUnref(fCanvas);
1211}
1212
scroggo@google.com565254b2012-06-28 15:41:32 +00001213SkCanvas* SkGPipeWriter::startRecording(SkGPipeController* controller, uint32_t flags) {
reed@google.combb6992a2011-04-26 17:41:56 +00001214 if (NULL == fCanvas) {
reed@google.comacd471f2011-05-03 21:26:46 +00001215 fWriter.reset(NULL, 0);
reed@google.comdde09562011-05-23 12:21:05 +00001216 fFactorySet.reset();
1217 fCanvas = SkNEW_ARGS(SkGPipeCanvas, (controller, &fWriter,
1218 (flags & kCrossProcess_Flag) ?
scroggo@google.com565254b2012-06-28 15:41:32 +00001219 &fFactorySet : NULL, flags));
reed@google.combb6992a2011-04-26 17:41:56 +00001220 }
1221 return fCanvas;
1222}
1223
1224void SkGPipeWriter::endRecording() {
1225 if (fCanvas) {
1226 fCanvas->finish();
1227 fCanvas->unref();
1228 fCanvas = NULL;
1229 }
1230}
1231
junov@chromium.org77eec242012-07-18 17:54:45 +00001232void SkGPipeWriter::flushRecording(bool detachCurrentBlock){
1233 fCanvas->flushRecording(detachCurrentBlock);
1234}
1235