blob: a6ffae1a379b2e4ef8281bfd694655f58870851a [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)
scroggo@google.com15011ee2012-07-26 20:03:32 +0000164 , fNumberOfReaders(numOfReaders)
165 , fBytesAllocated(0) {}
scroggo@google.com4dffc592012-07-17 16:49:40 +0000166 ~SharedHeap() {
scroggo@google.com284bf502012-07-17 16:10:34 +0000167 BitmapInfo* iter = fMostRecentlyUsed;
168 while (iter != NULL) {
scroggo@google.com15011ee2012-07-26 20:03:32 +0000169 SkDEBUGCODE(fBytesAllocated -= (iter->fBytesAllocated + sizeof(BitmapInfo)));
scroggo@google.com284bf502012-07-17 16:10:34 +0000170 BitmapInfo* next = iter->fLessRecentlyUsed;
171 SkDELETE(iter);
172 fBitmapCount--;
173 iter = next;
scroggo@google.com8ae3c7f2012-06-13 17:47:52 +0000174 }
scroggo@google.com284bf502012-07-17 16:10:34 +0000175 SkASSERT(0 == fBitmapCount);
scroggo@google.com15011ee2012-07-26 20:03:32 +0000176 SkASSERT(0 == fBytesAllocated);
scroggo@google.com8ae3c7f2012-06-13 17:47:52 +0000177 }
178
179 /*
scroggo@google.com15011ee2012-07-26 20:03:32 +0000180 * Get the approximate number of bytes allocated.
181 *
182 * Not exact. Some SkBitmaps may share SkPixelRefs, in which case only one
183 * SkBitmap will take the size of the SkPixelRef into account (the first
184 * one). It is possible that the one which accounts for the SkPixelRef has
185 * been removed, in which case we will no longer be counting those bytes.
186 */
187 size_t bytesAllocated() { return fBytesAllocated; }
188
189 /*
scroggo@google.com8ae3c7f2012-06-13 17:47:52 +0000190 * Add a copy of a bitmap to the heap.
191 * @param bm The SkBitmap to be copied and placed in the heap.
scroggo@google.com284bf502012-07-17 16:10:34 +0000192 * @return void* Pointer to the BitmapInfo stored in the heap, which
193 * contains a copy of the SkBitmap. If NULL,
scroggo@google.com8ae3c7f2012-06-13 17:47:52 +0000194 * the bitmap could not be copied.
195 */
scroggo@google.com284bf502012-07-17 16:10:34 +0000196 const void* addBitmap(const SkBitmap& orig) {
scroggo@google.com4f1f6bf2012-07-02 13:35:09 +0000197 const uint32_t genID = orig.getGenerationID();
198 SkPixelRef* sharedPixelRef = NULL;
scroggo@google.com284bf502012-07-17 16:10:34 +0000199 // When looking to see if we've previously used this bitmap, start at
200 // the end, assuming that the caller is more likely to reuse a recent
201 // one.
202 BitmapInfo* iter = fMostRecentlyUsed;
203 while (iter != NULL) {
204 if (genID == iter->fGenID) {
205 SkBitmap* storedBitmap = iter->fBitmap;
scroggo@google.com15011ee2012-07-26 20:03:32 +0000206 // TODO: Perhaps we can share code with
207 // SkPictureRecord::PixelRefDictionaryEntry/
208 // BitmapIndexCacheEntry so we can do a binary search for a
209 // matching bitmap
scroggo@google.com6ea165d2012-07-03 14:52:08 +0000210 if (orig.pixelRefOffset() != storedBitmap->pixelRefOffset()
211 || orig.width() != storedBitmap->width()
212 || orig.height() != storedBitmap->height()) {
scroggo@google.com4f1f6bf2012-07-02 13:35:09 +0000213 // In this case, the bitmaps share a pixelRef, but have
scroggo@google.com6ea165d2012-07-03 14:52:08 +0000214 // different offsets or sizes. Keep track of the other
215 // bitmap so that instead of making another copy of the
216 // pixelRef we can use the copy we already made.
217 sharedPixelRef = storedBitmap->pixelRef();
scroggo@google.com4f1f6bf2012-07-02 13:35:09 +0000218 break;
219 }
scroggo@google.com284bf502012-07-17 16:10:34 +0000220 iter->addDraws(fNumberOfReaders);
221 this->setMostRecentlyUsed(iter);
222 return iter;
scroggo@google.com8ae3c7f2012-06-13 17:47:52 +0000223 }
scroggo@google.com284bf502012-07-17 16:10:34 +0000224 iter = iter->fLessRecentlyUsed;
scroggo@google.com8ae3c7f2012-06-13 17:47:52 +0000225 }
scroggo@google.com284bf502012-07-17 16:10:34 +0000226 SkAutoRef ar((SkRefCnt*)sharedPixelRef);
227 BitmapInfo* replace = this->bitmapToReplace(orig);
scroggo@google.com565254b2012-06-28 15:41:32 +0000228 SkBitmap* copy;
229 // If the bitmap is mutable, we still need to do a deep copy, since the
230 // caller may modify it afterwards. That said, if the bitmap is mutable,
231 // but has no pixelRef, the copy constructor actually does a deep copy.
scroggo@google.com4f1f6bf2012-07-02 13:35:09 +0000232 if (fCanDoShallowCopies && (orig.isImmutable() || !orig.pixelRef())) {
scroggo@google.com284bf502012-07-17 16:10:34 +0000233 if (NULL == replace) {
234 copy = SkNEW_ARGS(SkBitmap, (orig));
235 } else {
236 *replace->fBitmap = orig;
237 }
scroggo@google.com565254b2012-06-28 15:41:32 +0000238 } else {
scroggo@google.com4f1f6bf2012-07-02 13:35:09 +0000239 if (sharedPixelRef != NULL) {
scroggo@google.com284bf502012-07-17 16:10:34 +0000240 if (NULL == replace) {
241 // Do a shallow copy of the bitmap to get the width, height, etc
242 copy = SkNEW_ARGS(SkBitmap, (orig));
243 // Replace the pixelRef with the copy that was already made, and
244 // use the appropriate offset.
245 copy->setPixelRef(sharedPixelRef, orig.pixelRefOffset());
246 } else {
247 *replace->fBitmap = orig;
248 replace->fBitmap->setPixelRef(sharedPixelRef, orig.pixelRefOffset());
249 }
scroggo@google.com4f1f6bf2012-07-02 13:35:09 +0000250 } else {
scroggo@google.com284bf502012-07-17 16:10:34 +0000251 if (NULL == replace) {
252 copy = SkNEW(SkBitmap);
253 if (!orig.copyTo(copy, orig.getConfig())) {
254 delete copy;
255 return NULL;
256 }
257 } else {
258 if (!orig.copyTo(replace->fBitmap, orig.getConfig())) {
259 return NULL;
260 }
scroggo@google.com4f1f6bf2012-07-02 13:35:09 +0000261 }
scroggo@google.com565254b2012-06-28 15:41:32 +0000262 }
scroggo@google.com8ae3c7f2012-06-13 17:47:52 +0000263 }
scroggo@google.com284bf502012-07-17 16:10:34 +0000264 BitmapInfo* info;
265 if (NULL == replace) {
scroggo@google.com15011ee2012-07-26 20:03:32 +0000266 fBytesAllocated += sizeof(BitmapInfo);
scroggo@google.com284bf502012-07-17 16:10:34 +0000267 info = SkNEW_ARGS(BitmapInfo, (copy, genID, fNumberOfReaders));
268 fBitmapCount++;
269 } else {
scroggo@google.com15011ee2012-07-26 20:03:32 +0000270 fBytesAllocated -= replace->fBytesAllocated;
scroggo@google.com284bf502012-07-17 16:10:34 +0000271 replace->fGenID = genID;
272 replace->addDraws(fNumberOfReaders);
273 info = replace;
274 }
scroggo@google.com15011ee2012-07-26 20:03:32 +0000275 // Always include the size of the SkBitmap struct.
276 info->fBytesAllocated = sizeof(SkBitmap);
277 // If the SkBitmap does not share an SkPixelRef with an SkBitmap already
278 // in the SharedHeap, also include the size of its pixels.
279 if (NULL == sharedPixelRef) {
280 info->fBytesAllocated += orig.getSize();
281 }
282 fBytesAllocated += info->fBytesAllocated;
scroggo@google.com284bf502012-07-17 16:10:34 +0000283 this->setMostRecentlyUsed(info);
284 return info;
scroggo@google.com8ae3c7f2012-06-13 17:47:52 +0000285 }
286private:
scroggo@google.com284bf502012-07-17 16:10:34 +0000287 void setMostRecentlyUsed(BitmapInfo* info);
288 BitmapInfo* bitmapToReplace(const SkBitmap& bm) const;
289
290 int fBitmapCount;
291 BitmapInfo* fLeastRecentlyUsed;
292 BitmapInfo* fMostRecentlyUsed;
293 const bool fCanDoShallowCopies;
294 const int fNumberOfReaders;
scroggo@google.com15011ee2012-07-26 20:03:32 +0000295 size_t fBytesAllocated;
scroggo@google.com8ae3c7f2012-06-13 17:47:52 +0000296};
297
scroggo@google.com284bf502012-07-17 16:10:34 +0000298// We just "used" info. Update our LRU accordingly
scroggo@google.com4dffc592012-07-17 16:49:40 +0000299void SharedHeap::setMostRecentlyUsed(BitmapInfo* info) {
scroggo@google.com284bf502012-07-17 16:10:34 +0000300 SkASSERT(info != NULL);
301 if (info == fMostRecentlyUsed) {
302 return;
303 }
304 // Remove info from its prior place, and make sure to cover the hole.
305 if (fLeastRecentlyUsed == info) {
306 SkASSERT(info->fMoreRecentlyUsed != NULL);
307 fLeastRecentlyUsed = info->fMoreRecentlyUsed;
308 }
309 if (info->fMoreRecentlyUsed != NULL) {
310 SkASSERT(fMostRecentlyUsed != info);
311 info->fMoreRecentlyUsed->fLessRecentlyUsed = info->fLessRecentlyUsed;
312 }
313 if (info->fLessRecentlyUsed != NULL) {
314 SkASSERT(fLeastRecentlyUsed != info);
315 info->fLessRecentlyUsed->fMoreRecentlyUsed = info->fMoreRecentlyUsed;
316 }
317 info->fMoreRecentlyUsed = NULL;
318 // Set up the head and tail pointers properly.
319 if (fMostRecentlyUsed != NULL) {
320 SkASSERT(NULL == fMostRecentlyUsed->fMoreRecentlyUsed);
321 fMostRecentlyUsed->fMoreRecentlyUsed = info;
322 info->fLessRecentlyUsed = fMostRecentlyUsed;
323 }
324 fMostRecentlyUsed = info;
325 if (NULL == fLeastRecentlyUsed) {
326 fLeastRecentlyUsed = info;
327 }
328}
329
330/**
331 * Given a new bitmap to be added to the cache, return an existing one that
332 * should be removed to make room, or NULL if there is already room.
333 */
scroggo@google.com4dffc592012-07-17 16:49:40 +0000334BitmapInfo* SharedHeap::bitmapToReplace(const SkBitmap& bm) const {
scroggo@google.com284bf502012-07-17 16:10:34 +0000335 // Arbitrarily set a limit of 5. We should test to find the best tradeoff
336 // between time and space. A lower limit means that we use less space, but
337 // it also means that we may have to insert the same bitmap into the heap
338 // multiple times (depending on the input), potentially taking more time.
339 // On the other hand, a lower limit also means searching through our stored
340 // bitmaps takes less time.
341 if (fBitmapCount > 5) {
342 BitmapInfo* iter = fLeastRecentlyUsed;
343 while (iter != NULL) {
344 if (iter->drawCount() > 0) {
345 // If the least recently used bitmap has not been drawn by some
346 // reader, then a more recently used one will not have been
347 // drawn yet either.
348 return NULL;
349 }
350 if (bm.pixelRef() != NULL
351 && bm.pixelRef() == iter->fBitmap->pixelRef()) {
352 // Do not replace a bitmap with a new one using the same
353 // pixel ref. Instead look for a different one that will
354 // potentially free up more space.
355 iter = iter->fMoreRecentlyUsed;
356 } else {
357 return iter;
358 }
359 }
360 }
361 return NULL;
362}
363
scroggo@google.com8ae3c7f2012-06-13 17:47:52 +0000364///////////////////////////////////////////////////////////////////////////////
365
reed@google.combb6992a2011-04-26 17:41:56 +0000366class SkGPipeCanvas : public SkCanvas {
367public:
scroggo@google.com565254b2012-06-28 15:41:32 +0000368 SkGPipeCanvas(SkGPipeController*, SkWriter32*, SkFactorySet*, uint32_t flags);
reed@google.combb6992a2011-04-26 17:41:56 +0000369 virtual ~SkGPipeCanvas();
370
371 void finish() {
372 if (!fDone) {
reed@google.comdbccc882011-07-08 18:53:39 +0000373 if (this->needOpBytes()) {
374 this->writeOp(kDone_DrawOp);
375 this->doNotify();
376 }
reed@google.combb6992a2011-04-26 17:41:56 +0000377 fDone = true;
378 }
379 }
380
junov@chromium.org77eec242012-07-18 17:54:45 +0000381 void flushRecording(bool detachCurrentBlock);
382
scroggo@google.com15011ee2012-07-26 20:03:32 +0000383 size_t storageAllocatedForRecording() {
384 return fSharedHeap.bytesAllocated();
385 }
386
reed@google.combb6992a2011-04-26 17:41:56 +0000387 // overrides from SkCanvas
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000388 virtual int save(SaveFlags) SK_OVERRIDE;
389 virtual int saveLayer(const SkRect* bounds, const SkPaint*,
390 SaveFlags) SK_OVERRIDE;
391 virtual void restore() SK_OVERRIDE;
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000392 virtual bool isDrawingToLayer() const SK_OVERRIDE;
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000393 virtual bool translate(SkScalar dx, SkScalar dy) SK_OVERRIDE;
394 virtual bool scale(SkScalar sx, SkScalar sy) SK_OVERRIDE;
395 virtual bool rotate(SkScalar degrees) SK_OVERRIDE;
396 virtual bool skew(SkScalar sx, SkScalar sy) SK_OVERRIDE;
397 virtual bool concat(const SkMatrix& matrix) SK_OVERRIDE;
398 virtual void setMatrix(const SkMatrix& matrix) SK_OVERRIDE;
399 virtual bool clipRect(const SkRect& rect, SkRegion::Op op,
400 bool doAntiAlias = false) SK_OVERRIDE;
401 virtual bool clipPath(const SkPath& path, SkRegion::Op op,
402 bool doAntiAlias = false) SK_OVERRIDE;
403 virtual bool clipRegion(const SkRegion& region, SkRegion::Op op) SK_OVERRIDE;
404 virtual void clear(SkColor) SK_OVERRIDE;
405 virtual void drawPaint(const SkPaint& paint) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000406 virtual void drawPoints(PointMode, size_t count, const SkPoint pts[],
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000407 const SkPaint&) SK_OVERRIDE;
408 virtual void drawRect(const SkRect& rect, const SkPaint&) SK_OVERRIDE;
409 virtual void drawPath(const SkPath& path, const SkPaint&) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000410 virtual void drawBitmap(const SkBitmap&, SkScalar left, SkScalar top,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000411 const SkPaint*) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000412 virtual void drawBitmapRect(const SkBitmap&, const SkIRect* src,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000413 const SkRect& dst, const SkPaint*) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000414 virtual void drawBitmapMatrix(const SkBitmap&, const SkMatrix&,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000415 const SkPaint*) SK_OVERRIDE;
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000416 virtual void drawBitmapNine(const SkBitmap& bitmap, const SkIRect& center,
417 const SkRect& dst, const SkPaint* paint = NULL) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000418 virtual void drawSprite(const SkBitmap&, int left, int top,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000419 const SkPaint*) SK_OVERRIDE;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000420 virtual void drawText(const void* text, size_t byteLength, SkScalar x,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000421 SkScalar y, const SkPaint&) SK_OVERRIDE;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000422 virtual void drawPosText(const void* text, size_t byteLength,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000423 const SkPoint pos[], const SkPaint&) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000424 virtual void drawPosTextH(const void* text, size_t byteLength,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000425 const SkScalar xpos[], SkScalar constY,
426 const SkPaint&) SK_OVERRIDE;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000427 virtual void drawTextOnPath(const void* text, size_t byteLength,
428 const SkPath& path, const SkMatrix* matrix,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000429 const SkPaint&) SK_OVERRIDE;
430 virtual void drawPicture(SkPicture& picture) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000431 virtual void drawVertices(VertexMode, int vertexCount,
432 const SkPoint vertices[], const SkPoint texs[],
433 const SkColor colors[], SkXfermode*,
434 const uint16_t indices[], int indexCount,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000435 const SkPaint&) SK_OVERRIDE;
436 virtual void drawData(const void*, size_t) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000437
438private:
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000439 enum {
440 kNoSaveLayer = -1,
441 };
442 int fFirstSaveLayerStackLevel;
443 SharedHeap fSharedHeap;
reed@google.comacd471f2011-05-03 21:26:46 +0000444 SkGPipeController* fController;
reed@google.combb6992a2011-04-26 17:41:56 +0000445 SkWriter32& fWriter;
reed@google.comacd471f2011-05-03 21:26:46 +0000446 size_t fBlockSize; // amount allocated for writer
447 size_t fBytesNotified;
reed@google.combb6992a2011-04-26 17:41:56 +0000448 bool fDone;
scroggo@google.com565254b2012-06-28 15:41:32 +0000449 uint32_t fFlags;
reed@google.combb6992a2011-04-26 17:41:56 +0000450
reed@google.comf5842f72011-05-04 18:30:04 +0000451 SkRefCntSet fTypefaceSet;
452
453 uint32_t getTypefaceID(SkTypeface*);
454
reed@google.comacd471f2011-05-03 21:26:46 +0000455 inline void writeOp(DrawOps op, unsigned flags, unsigned data) {
reed@google.combb6992a2011-04-26 17:41:56 +0000456 fWriter.write32(DrawOp_packOpFlagData(op, flags, data));
457 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000458
reed@google.comacd471f2011-05-03 21:26:46 +0000459 inline void writeOp(DrawOps op) {
reed@google.combb6992a2011-04-26 17:41:56 +0000460 fWriter.write32(DrawOp_packOpFlagData(op, 0, 0));
461 }
reed@google.comacd471f2011-05-03 21:26:46 +0000462
463 bool needOpBytes(size_t size = 0);
464
465 inline void doNotify() {
466 if (!fDone) {
467 size_t bytes = fWriter.size() - fBytesNotified;
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000468 if (bytes > 0) {
469 fController->notifyWritten(bytes);
470 fBytesNotified += bytes;
471 }
reed@google.comacd471f2011-05-03 21:26:46 +0000472 }
473 }
reed@google.comb55d1182011-05-11 00:42:04 +0000474
scroggo@google.com4dffc592012-07-17 16:49:40 +0000475 // These are only used when in cross process, but with no shared address
476 // space, so bitmaps are flattened.
477 FlattenableHeap fBitmapHeap;
478 SkBitmapDictionary fBitmapDictionary;
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000479 int flattenToIndex(const SkBitmap&);
480
scroggo@google.com4dffc592012-07-17 16:49:40 +0000481 FlattenableHeap fFlattenableHeap;
482 FlatDictionary fFlatDictionary;
reed@google.comb55d1182011-05-11 00:42:04 +0000483 int fCurrFlatIndex[kCount_PaintFlats];
484 int flattenToIndex(SkFlattenable* obj, PaintFlats);
485
reed@google.com31891582011-05-12 03:03:56 +0000486 SkPaint fPaint;
487 void writePaint(const SkPaint&);
reed@google.combb6992a2011-04-26 17:41:56 +0000488
reed@google.comacd471f2011-05-03 21:26:46 +0000489 class AutoPipeNotify {
490 public:
491 AutoPipeNotify(SkGPipeCanvas* canvas) : fCanvas(canvas) {}
492 ~AutoPipeNotify() { fCanvas->doNotify(); }
493 private:
494 SkGPipeCanvas* fCanvas;
495 };
496 friend class AutoPipeNotify;
497
reed@google.combb6992a2011-04-26 17:41:56 +0000498 typedef SkCanvas INHERITED;
499};
500
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000501int SkGPipeCanvas::flattenToIndex(const SkBitmap & bitmap) {
scroggo@google.com565254b2012-06-28 15:41:32 +0000502 SkASSERT(shouldFlattenBitmaps(fFlags));
scroggo@google.com4dffc592012-07-17 16:49:40 +0000503 uint32_t flags = SkFlattenableWriteBuffer::kInlineFactoryNames_Flag
504 | SkFlattenableWriteBuffer::kCrossProcess_Flag;
505 bool added, replaced;
506 const SkFlatData* flat = fBitmapDictionary.findAndReplace(
507 bitmap, flags, fBitmapHeap.flatToReplace(), &added, &replaced);
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000508
scroggo@google.com4dffc592012-07-17 16:49:40 +0000509 int index = flat->index();
510 if (added && this->needOpBytes(flat->flatSize())) {
511 this->writeOp(kDef_Bitmap_DrawOp, 0, index);
512 fWriter.write(flat->data(), flat->flatSize());
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000513 }
scroggo@google.com4dffc592012-07-17 16:49:40 +0000514 return index;
scroggo@google.comd3ba5cc2012-07-09 16:05:53 +0000515}
516
reed@google.comb55d1182011-05-11 00:42:04 +0000517// return 0 for NULL (or unflattenable obj), or index-base-1
scroggo@google.comd3ba5cc2012-07-09 16:05:53 +0000518// return ~(index-base-1) if an old flattenable was replaced
reed@google.comb55d1182011-05-11 00:42:04 +0000519int SkGPipeCanvas::flattenToIndex(SkFlattenable* obj, PaintFlats paintflat) {
520 if (NULL == obj) {
521 return 0;
522 }
reed@google.comb55d1182011-05-11 00:42:04 +0000523
scroggo@google.com4dffc592012-07-17 16:49:40 +0000524 uint32_t writeBufferFlags;
scroggo@google.com565254b2012-06-28 15:41:32 +0000525 if (fFlags & SkGPipeWriter::kCrossProcess_Flag) {
scroggo@google.com4dffc592012-07-17 16:49:40 +0000526 writeBufferFlags = (SkFlattenableWriteBuffer::kInlineFactoryNames_Flag
527 | SkFlattenableWriteBuffer::kCrossProcess_Flag);
scroggo@google.com565254b2012-06-28 15:41:32 +0000528 } else {
529 // Needed for bitmap shaders.
scroggo@google.com4dffc592012-07-17 16:49:40 +0000530 writeBufferFlags = SkFlattenableWriteBuffer::kForceFlattenBitmapPixels_Flag;
scroggo@google.com565254b2012-06-28 15:41:32 +0000531 }
reed@google.comdde09562011-05-23 12:21:05 +0000532
scroggo@google.com4dffc592012-07-17 16:49:40 +0000533 bool added, replaced;
534 const SkFlatData* flat = fFlatDictionary.findAndReplace(
535 *obj, writeBufferFlags, fFlattenableHeap.flatToReplace(), &added, &replaced);
536 int index = flat->index();
537 if (added && this->needOpBytes(flat->flatSize())) {
538 this->writeOp(kDef_Flattenable_DrawOp, paintflat, index);
539 fWriter.write(flat->data(), flat->flatSize());
540 }
541 if (replaced) {
reed@google.comb55d1182011-05-11 00:42:04 +0000542 index = ~index;
reed@google.comb55d1182011-05-11 00:42:04 +0000543 }
scroggo@google.com4dffc592012-07-17 16:49:40 +0000544 return index;
reed@google.comb55d1182011-05-11 00:42:04 +0000545}
546
reed@google.combb6992a2011-04-26 17:41:56 +0000547///////////////////////////////////////////////////////////////////////////////
548
reed@google.comacd471f2011-05-03 21:26:46 +0000549#define MIN_BLOCK_SIZE (16 * 1024)
scroggo@google.com4dffc592012-07-17 16:49:40 +0000550#define BITMAPS_TO_KEEP 5
551#define FLATTENABLES_TO_KEEP 10
reed@google.combb6992a2011-04-26 17:41:56 +0000552
reed@google.comacd471f2011-05-03 21:26:46 +0000553SkGPipeCanvas::SkGPipeCanvas(SkGPipeController* controller,
scroggo@google.com565254b2012-06-28 15:41:32 +0000554 SkWriter32* writer, SkFactorySet* fset, uint32_t flags)
scroggo@google.com4dffc592012-07-17 16:49:40 +0000555: fSharedHeap(!(flags & SkGPipeWriter::kCrossProcess_Flag), controller->numberOfReaders())
556, fWriter(*writer), fFlags(flags)
557, fBitmapHeap(BITMAPS_TO_KEEP), fBitmapDictionary(&fBitmapHeap, NULL, NULL, fset)
558, fFlattenableHeap(FLATTENABLES_TO_KEEP), fFlatDictionary(&fFlattenableHeap, fset) {
reed@google.comacd471f2011-05-03 21:26:46 +0000559 fController = controller;
reed@google.combb6992a2011-04-26 17:41:56 +0000560 fDone = false;
reed@google.comacd471f2011-05-03 21:26:46 +0000561 fBlockSize = 0; // need first block from controller
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000562 fBytesNotified = 0;
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000563 fFirstSaveLayerStackLevel = kNoSaveLayer;
reed@google.comb55d1182011-05-11 00:42:04 +0000564 sk_bzero(fCurrFlatIndex, sizeof(fCurrFlatIndex));
reed@google.comacd471f2011-05-03 21:26:46 +0000565
reed@google.combb6793b2011-05-05 15:18:15 +0000566 // we need a device to limit our clip
567 // should the caller give us the bounds?
yangsu@google.com06b4da162011-06-17 15:04:40 +0000568 // We don't allocate pixels for the bitmap
reed@google.combb6793b2011-05-05 15:18:15 +0000569 SkBitmap bitmap;
570 bitmap.setConfig(SkBitmap::kARGB_8888_Config, 32767, 32767);
yangsu@google.com06b4da162011-06-17 15:04:40 +0000571 SkDevice* device = SkNEW_ARGS(SkDevice, (bitmap));
reed@google.combb6793b2011-05-05 15:18:15 +0000572 this->setDevice(device)->unref();
scroggo@google.com565254b2012-06-28 15:41:32 +0000573 // Tell the reader the appropriate flags to use.
574 if (this->needOpBytes()) {
575 this->writeOp(kReportFlags_DrawOp, fFlags, 0);
576 }
reed@google.combb6992a2011-04-26 17:41:56 +0000577}
578
579SkGPipeCanvas::~SkGPipeCanvas() {
580 this->finish();
reed@google.combb6992a2011-04-26 17:41:56 +0000581}
582
reed@google.comacd471f2011-05-03 21:26:46 +0000583bool SkGPipeCanvas::needOpBytes(size_t needed) {
584 if (fDone) {
585 return false;
586 }
587
588 needed += 4; // size of DrawOp atom
589 if (fWriter.size() + needed > fBlockSize) {
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000590 // Before we wipe out any data that has already been written, read it
591 // out.
592 this->doNotify();
593 size_t blockSize = SkMax32(MIN_BLOCK_SIZE, needed);
594 void* block = fController->requestBlock(blockSize, &fBlockSize);
reed@google.comacd471f2011-05-03 21:26:46 +0000595 if (NULL == block) {
596 fDone = true;
597 return false;
598 }
599 fWriter.reset(block, fBlockSize);
600 fBytesNotified = 0;
601 }
602 return true;
603}
604
reed@google.comf5842f72011-05-04 18:30:04 +0000605uint32_t SkGPipeCanvas::getTypefaceID(SkTypeface* face) {
606 uint32_t id = 0; // 0 means default/null typeface
607 if (face) {
608 id = fTypefaceSet.find(face);
609 if (0 == id) {
610 id = fTypefaceSet.add(face);
611 size_t size = writeTypeface(NULL, face);
612 if (this->needOpBytes(size)) {
reed@google.combb6793b2011-05-05 15:18:15 +0000613 this->writeOp(kDef_Typeface_DrawOp);
reed@google.comf5842f72011-05-04 18:30:04 +0000614 writeTypeface(&fWriter, face);
615 }
616 }
617 }
618 return id;
619}
620
reed@google.combb6992a2011-04-26 17:41:56 +0000621///////////////////////////////////////////////////////////////////////////////
622
reed@google.comacd471f2011-05-03 21:26:46 +0000623#define NOTIFY_SETUP(canvas) \
624 AutoPipeNotify apn(canvas)
625
reed@google.combb6992a2011-04-26 17:41:56 +0000626int SkGPipeCanvas::save(SaveFlags flags) {
reed@google.comacd471f2011-05-03 21:26:46 +0000627 NOTIFY_SETUP(this);
628 if (this->needOpBytes()) {
629 this->writeOp(kSave_DrawOp, 0, flags);
630 }
reed@google.combb6992a2011-04-26 17:41:56 +0000631 return this->INHERITED::save(flags);
632}
633
634int SkGPipeCanvas::saveLayer(const SkRect* bounds, const SkPaint* paint,
635 SaveFlags saveFlags) {
reed@google.comacd471f2011-05-03 21:26:46 +0000636 NOTIFY_SETUP(this);
637 size_t size = 0;
reed@google.combb6992a2011-04-26 17:41:56 +0000638 unsigned opFlags = 0;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000639
reed@google.combb6992a2011-04-26 17:41:56 +0000640 if (bounds) {
641 opFlags |= kSaveLayer_HasBounds_DrawOpFlag;
reed@google.comacd471f2011-05-03 21:26:46 +0000642 size += sizeof(SkRect);
reed@google.combb6992a2011-04-26 17:41:56 +0000643 }
644 if (paint) {
645 opFlags |= kSaveLayer_HasPaint_DrawOpFlag;
reed@google.com31891582011-05-12 03:03:56 +0000646 this->writePaint(*paint);
reed@google.combb6992a2011-04-26 17:41:56 +0000647 }
648
reed@google.comacd471f2011-05-03 21:26:46 +0000649 if (this->needOpBytes(size)) {
650 this->writeOp(kSaveLayer_DrawOp, opFlags, saveFlags);
651 if (bounds) {
652 fWriter.writeRect(*bounds);
653 }
reed@google.combb6992a2011-04-26 17:41:56 +0000654 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000655
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000656 if (kNoSaveLayer == fFirstSaveLayerStackLevel){
657 fFirstSaveLayerStackLevel = this->getSaveCount();
658 }
reed@google.combb6992a2011-04-26 17:41:56 +0000659 // we just pass on the save, so we don't create a layer
660 return this->INHERITED::save(saveFlags);
661}
662
663void SkGPipeCanvas::restore() {
reed@google.comacd471f2011-05-03 21:26:46 +0000664 NOTIFY_SETUP(this);
665 if (this->needOpBytes()) {
666 this->writeOp(kRestore_DrawOp);
667 }
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000668
reed@google.combb6992a2011-04-26 17:41:56 +0000669 this->INHERITED::restore();
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000670
671 if (this->getSaveCount() == fFirstSaveLayerStackLevel){
672 fFirstSaveLayerStackLevel = kNoSaveLayer;
673 }
674}
675
676bool SkGPipeCanvas::isDrawingToLayer() const {
677 return kNoSaveLayer != fFirstSaveLayerStackLevel;
reed@google.combb6992a2011-04-26 17:41:56 +0000678}
679
680bool SkGPipeCanvas::translate(SkScalar dx, SkScalar dy) {
681 if (dx || dy) {
reed@google.comacd471f2011-05-03 21:26:46 +0000682 NOTIFY_SETUP(this);
683 if (this->needOpBytes(2 * sizeof(SkScalar))) {
684 this->writeOp(kTranslate_DrawOp);
685 fWriter.writeScalar(dx);
686 fWriter.writeScalar(dy);
687 }
reed@google.combb6992a2011-04-26 17:41:56 +0000688 }
689 return this->INHERITED::translate(dx, dy);
690}
691
692bool SkGPipeCanvas::scale(SkScalar sx, SkScalar sy) {
693 if (sx || sy) {
reed@google.comacd471f2011-05-03 21:26:46 +0000694 NOTIFY_SETUP(this);
695 if (this->needOpBytes(2 * sizeof(SkScalar))) {
696 this->writeOp(kScale_DrawOp);
697 fWriter.writeScalar(sx);
698 fWriter.writeScalar(sy);
699 }
reed@google.combb6992a2011-04-26 17:41:56 +0000700 }
701 return this->INHERITED::scale(sx, sy);
702}
703
704bool SkGPipeCanvas::rotate(SkScalar degrees) {
705 if (degrees) {
reed@google.comacd471f2011-05-03 21:26:46 +0000706 NOTIFY_SETUP(this);
707 if (this->needOpBytes(sizeof(SkScalar))) {
708 this->writeOp(kRotate_DrawOp);
709 fWriter.writeScalar(degrees);
710 }
reed@google.combb6992a2011-04-26 17:41:56 +0000711 }
712 return this->INHERITED::rotate(degrees);
713}
714
715bool SkGPipeCanvas::skew(SkScalar sx, SkScalar sy) {
716 if (sx || sy) {
reed@google.comacd471f2011-05-03 21:26:46 +0000717 NOTIFY_SETUP(this);
718 if (this->needOpBytes(2 * sizeof(SkScalar))) {
719 this->writeOp(kSkew_DrawOp);
720 fWriter.writeScalar(sx);
721 fWriter.writeScalar(sy);
722 }
reed@google.combb6992a2011-04-26 17:41:56 +0000723 }
724 return this->INHERITED::skew(sx, sy);
725}
726
727bool SkGPipeCanvas::concat(const SkMatrix& matrix) {
728 if (!matrix.isIdentity()) {
reed@google.comacd471f2011-05-03 21:26:46 +0000729 NOTIFY_SETUP(this);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000730 if (this->needOpBytes(matrix.writeToMemory(NULL))) {
reed@google.comacd471f2011-05-03 21:26:46 +0000731 this->writeOp(kConcat_DrawOp);
djsollen@google.com2b2ede32012-04-12 13:24:04 +0000732 fWriter.writeMatrix(matrix);
reed@google.comacd471f2011-05-03 21:26:46 +0000733 }
reed@google.combb6992a2011-04-26 17:41:56 +0000734 }
735 return this->INHERITED::concat(matrix);
736}
737
738void SkGPipeCanvas::setMatrix(const SkMatrix& matrix) {
reed@google.comacd471f2011-05-03 21:26:46 +0000739 NOTIFY_SETUP(this);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000740 if (this->needOpBytes(matrix.writeToMemory(NULL))) {
reed@google.comacd471f2011-05-03 21:26:46 +0000741 this->writeOp(kSetMatrix_DrawOp);
djsollen@google.com2b2ede32012-04-12 13:24:04 +0000742 fWriter.writeMatrix(matrix);
reed@google.comacd471f2011-05-03 21:26:46 +0000743 }
reed@google.combb6992a2011-04-26 17:41:56 +0000744 this->INHERITED::setMatrix(matrix);
745}
746
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000747bool SkGPipeCanvas::clipRect(const SkRect& rect, SkRegion::Op rgnOp,
748 bool doAntiAlias) {
reed@google.comacd471f2011-05-03 21:26:46 +0000749 NOTIFY_SETUP(this);
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000750 if (this->needOpBytes(sizeof(SkRect)) + sizeof(bool)) {
reed@google.comacd471f2011-05-03 21:26:46 +0000751 this->writeOp(kClipRect_DrawOp, 0, rgnOp);
752 fWriter.writeRect(rect);
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000753 fWriter.writeBool(doAntiAlias);
reed@google.comacd471f2011-05-03 21:26:46 +0000754 }
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000755 return this->INHERITED::clipRect(rect, rgnOp, doAntiAlias);
reed@google.combb6992a2011-04-26 17:41:56 +0000756}
757
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000758bool SkGPipeCanvas::clipPath(const SkPath& path, SkRegion::Op rgnOp,
759 bool doAntiAlias) {
reed@google.comacd471f2011-05-03 21:26:46 +0000760 NOTIFY_SETUP(this);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000761 if (this->needOpBytes(path.writeToMemory(NULL)) + sizeof(bool)) {
reed@google.comacd471f2011-05-03 21:26:46 +0000762 this->writeOp(kClipPath_DrawOp, 0, rgnOp);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000763 fWriter.writePath(path);
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000764 fWriter.writeBool(doAntiAlias);
reed@google.comacd471f2011-05-03 21:26:46 +0000765 }
reed@google.combb6992a2011-04-26 17:41:56 +0000766 // we just pass on the bounds of the path
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000767 return this->INHERITED::clipRect(path.getBounds(), rgnOp, doAntiAlias);
reed@google.combb6992a2011-04-26 17:41:56 +0000768}
769
770bool SkGPipeCanvas::clipRegion(const SkRegion& region, SkRegion::Op rgnOp) {
reed@google.comacd471f2011-05-03 21:26:46 +0000771 NOTIFY_SETUP(this);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000772 if (this->needOpBytes(region.writeToMemory(NULL))) {
reed@google.comacd471f2011-05-03 21:26:46 +0000773 this->writeOp(kClipRegion_DrawOp, 0, rgnOp);
djsollen@google.com2b2ede32012-04-12 13:24:04 +0000774 fWriter.writeRegion(region);
reed@google.comacd471f2011-05-03 21:26:46 +0000775 }
reed@google.combb6992a2011-04-26 17:41:56 +0000776 return this->INHERITED::clipRegion(region, rgnOp);
777}
778
779///////////////////////////////////////////////////////////////////////////////
780
781void SkGPipeCanvas::clear(SkColor color) {
reed@google.comacd471f2011-05-03 21:26:46 +0000782 NOTIFY_SETUP(this);
reed@google.combb6992a2011-04-26 17:41:56 +0000783 unsigned flags = 0;
784 if (color) {
785 flags |= kClear_HasColor_DrawOpFlag;
786 }
reed@google.comacd471f2011-05-03 21:26:46 +0000787 if (this->needOpBytes(sizeof(SkColor))) {
788 this->writeOp(kDrawClear_DrawOp, flags, 0);
789 if (color) {
790 fWriter.write32(color);
791 }
reed@google.combb6992a2011-04-26 17:41:56 +0000792 }
793}
794
795void SkGPipeCanvas::drawPaint(const SkPaint& paint) {
reed@google.comacd471f2011-05-03 21:26:46 +0000796 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000797 this->writePaint(paint);
reed@google.comacd471f2011-05-03 21:26:46 +0000798 if (this->needOpBytes()) {
reed@google.com31891582011-05-12 03:03:56 +0000799 this->writeOp(kDrawPaint_DrawOp);
reed@google.comacd471f2011-05-03 21:26:46 +0000800 }
reed@google.combb6992a2011-04-26 17:41:56 +0000801}
802
803void SkGPipeCanvas::drawPoints(PointMode mode, size_t count,
804 const SkPoint pts[], const SkPaint& paint) {
805 if (count) {
reed@google.comacd471f2011-05-03 21:26:46 +0000806 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000807 this->writePaint(paint);
reed@google.comacd471f2011-05-03 21:26:46 +0000808 if (this->needOpBytes(4 + count * sizeof(SkPoint))) {
reed@google.com31891582011-05-12 03:03:56 +0000809 this->writeOp(kDrawPoints_DrawOp, mode, 0);
reed@google.comacd471f2011-05-03 21:26:46 +0000810 fWriter.write32(count);
811 fWriter.write(pts, count * sizeof(SkPoint));
812 }
reed@google.combb6992a2011-04-26 17:41:56 +0000813 }
814}
815
816void SkGPipeCanvas::drawRect(const SkRect& rect, const SkPaint& paint) {
reed@google.comacd471f2011-05-03 21:26:46 +0000817 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000818 this->writePaint(paint);
reed@google.comacd471f2011-05-03 21:26:46 +0000819 if (this->needOpBytes(sizeof(SkRect))) {
reed@google.com31891582011-05-12 03:03:56 +0000820 this->writeOp(kDrawRect_DrawOp);
reed@google.comacd471f2011-05-03 21:26:46 +0000821 fWriter.writeRect(rect);
822 }
reed@google.combb6992a2011-04-26 17:41:56 +0000823}
824
825void SkGPipeCanvas::drawPath(const SkPath& path, const SkPaint& paint) {
reed@google.comacd471f2011-05-03 21:26:46 +0000826 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000827 this->writePaint(paint);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000828 if (this->needOpBytes(path.writeToMemory(NULL))) {
reed@google.com31891582011-05-12 03:03:56 +0000829 this->writeOp(kDrawPath_DrawOp);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000830 fWriter.writePath(path);
reed@google.comacd471f2011-05-03 21:26:46 +0000831 }
reed@google.combb6992a2011-04-26 17:41:56 +0000832}
833
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000834void SkGPipeCanvas::drawBitmap(const SkBitmap& bm, SkScalar left, SkScalar top,
835 const SkPaint* paint) {
scroggo@google.com565254b2012-06-28 15:41:32 +0000836 bool flatten = shouldFlattenBitmaps(fFlags);
837 const void* ptr = 0;
838 int bitmapIndex = 0;
839 if (flatten) {
840 bitmapIndex = this->flattenToIndex(bm);
841 } else {
scroggo@google.com4dffc592012-07-17 16:49:40 +0000842 ptr = fSharedHeap.addBitmap(bm);
scroggo@google.com565254b2012-06-28 15:41:32 +0000843 if (NULL == ptr) {
844 return;
845 }
scroggo@google.com8ae3c7f2012-06-13 17:47:52 +0000846 }
847
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000848 NOTIFY_SETUP(this);
849 if (paint) {
850 this->writePaint(*paint);
851 }
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000852
scroggo@google.com565254b2012-06-28 15:41:32 +0000853 size_t opBytesNeeded = sizeof(SkScalar) * 2 + sizeof(bool);
854 if (!flatten) {
855 opBytesNeeded += sizeof(void*);
856 }
857 if (this->needOpBytes(opBytesNeeded)) {
858 this->writeOp(kDrawBitmap_DrawOp, 0, bitmapIndex);
859 if (!flatten) {
860 fWriter.writePtr(const_cast<void*>(ptr));
861 }
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000862 fWriter.writeBool(paint != NULL);
863 fWriter.writeScalar(left);
864 fWriter.writeScalar(top);
865 }
reed@google.combb6992a2011-04-26 17:41:56 +0000866}
867
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000868void SkGPipeCanvas::drawBitmapRect(const SkBitmap& bm, const SkIRect* src,
869 const SkRect& dst, const SkPaint* paint) {
scroggo@google.com565254b2012-06-28 15:41:32 +0000870 bool flatten = shouldFlattenBitmaps(fFlags);
871 const void* ptr = 0;
872 int bitmapIndex = 0;
873 if (flatten) {
874 bitmapIndex = this->flattenToIndex(bm);
875 } else {
scroggo@google.com4dffc592012-07-17 16:49:40 +0000876 ptr = fSharedHeap.addBitmap(bm);
scroggo@google.com565254b2012-06-28 15:41:32 +0000877 if (NULL == ptr) {
878 return;
879 }
scroggo@google.com8ae3c7f2012-06-13 17:47:52 +0000880 }
881
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000882 NOTIFY_SETUP(this);
883 if (paint) {
884 this->writePaint(*paint);
885 }
886
scroggo@google.com565254b2012-06-28 15:41:32 +0000887 size_t opBytesNeeded = sizeof(SkRect) + sizeof(bool) * 2;
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000888 bool hasSrc = src != NULL;
889 if (hasSrc) {
890 opBytesNeeded += sizeof(int32_t) * 4;
891 }
scroggo@google.com565254b2012-06-28 15:41:32 +0000892 if (!flatten) {
893 opBytesNeeded += sizeof(void*);
894 }
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000895 if (this->needOpBytes(opBytesNeeded)) {
scroggo@google.com565254b2012-06-28 15:41:32 +0000896 this->writeOp(kDrawBitmapRect_DrawOp, 0, bitmapIndex);
897 if (!flatten) {
898 fWriter.writePtr(const_cast<void*>(ptr));
899 }
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000900 fWriter.writeBool(paint != NULL);
901 fWriter.writeBool(hasSrc);
902 if (hasSrc) {
903 fWriter.write32(src->fLeft);
904 fWriter.write32(src->fTop);
905 fWriter.write32(src->fRight);
906 fWriter.write32(src->fBottom);
907 }
908 fWriter.writeRect(dst);
909 }
reed@google.combb6992a2011-04-26 17:41:56 +0000910}
911
912void SkGPipeCanvas::drawBitmapMatrix(const SkBitmap&, const SkMatrix&,
913 const SkPaint*) {
914 UNIMPLEMENTED
915}
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000916
917void SkGPipeCanvas::drawBitmapNine(const SkBitmap& bm, const SkIRect& center,
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000918 const SkRect& dst, const SkPaint* paint) {
scroggo@google.com565254b2012-06-28 15:41:32 +0000919 bool flatten = shouldFlattenBitmaps(fFlags);
920 const void* ptr = 0;
921 int bitmapIndex = 0;
922 if (flatten) {
923 bitmapIndex = this->flattenToIndex(bm);
924 } else {
scroggo@google.com4dffc592012-07-17 16:49:40 +0000925 ptr = fSharedHeap.addBitmap(bm);
scroggo@google.com565254b2012-06-28 15:41:32 +0000926 if (NULL == ptr) {
927 return;
928 }
scroggo@google.com8ae3c7f2012-06-13 17:47:52 +0000929 }
930
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000931 NOTIFY_SETUP(this);
932 if (paint) {
933 this->writePaint(*paint);
934 }
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000935
scroggo@google.com565254b2012-06-28 15:41:32 +0000936 size_t opBytesNeeded = sizeof(int32_t) * 4 + sizeof(bool) + sizeof(SkRect);
937 if (!flatten) {
938 opBytesNeeded += sizeof(void*);
939 }
940 if (this->needOpBytes(opBytesNeeded)) {
941 this->writeOp(kDrawBitmapNine_DrawOp, 0, bitmapIndex);
942 if (!flatten) {
943 fWriter.writePtr(const_cast<void*>(ptr));
944 }
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000945 fWriter.writeBool(paint != NULL);
946 fWriter.write32(center.fLeft);
947 fWriter.write32(center.fTop);
948 fWriter.write32(center.fRight);
949 fWriter.write32(center.fBottom);
950 fWriter.writeRect(dst);
951 }
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000952}
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000953
954void SkGPipeCanvas::drawSprite(const SkBitmap& bm, int left, int top,
955 const SkPaint* paint) {
scroggo@google.com565254b2012-06-28 15:41:32 +0000956 bool flatten = shouldFlattenBitmaps(fFlags);
957 const void* ptr = 0;
958 int bitmapIndex = 0;
959 if (flatten) {
960 bitmapIndex = this->flattenToIndex(bm);
961 } else {
scroggo@google.com4dffc592012-07-17 16:49:40 +0000962 ptr = fSharedHeap.addBitmap(bm);
scroggo@google.com565254b2012-06-28 15:41:32 +0000963 if (NULL == ptr) {
964 return;
965 }
scroggo@google.com8ae3c7f2012-06-13 17:47:52 +0000966 }
967
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000968 NOTIFY_SETUP(this);
969 if (paint) {
970 this->writePaint(*paint);
971 }
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000972
scroggo@google.com565254b2012-06-28 15:41:32 +0000973 size_t opBytesNeeded = sizeof(int32_t) * 2 + sizeof(bool);
974 if (!flatten) {
975 opBytesNeeded += sizeof(void*);
976 }
977 if (this->needOpBytes(opBytesNeeded)) {
978 this->writeOp(kDrawSprite_DrawOp, 0, bitmapIndex);
979 if (!flatten) {
980 fWriter.writePtr(const_cast<void*>(ptr));
981 }
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000982 fWriter.writeBool(paint != NULL);
983 fWriter.write32(left);
984 fWriter.write32(top);
985 }
reed@google.combb6992a2011-04-26 17:41:56 +0000986}
987
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000988void SkGPipeCanvas::drawText(const void* text, size_t byteLength, SkScalar x,
reed@google.combb6992a2011-04-26 17:41:56 +0000989 SkScalar y, const SkPaint& paint) {
990 if (byteLength) {
reed@google.comacd471f2011-05-03 21:26:46 +0000991 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000992 this->writePaint(paint);
reed@google.comacd471f2011-05-03 21:26:46 +0000993 if (this->needOpBytes(4 + SkAlign4(byteLength) + 2 * sizeof(SkScalar))) {
reed@google.com31891582011-05-12 03:03:56 +0000994 this->writeOp(kDrawText_DrawOp);
reed@google.comacd471f2011-05-03 21:26:46 +0000995 fWriter.write32(byteLength);
996 fWriter.writePad(text, byteLength);
997 fWriter.writeScalar(x);
998 fWriter.writeScalar(y);
999 }
reed@google.combb6992a2011-04-26 17:41:56 +00001000 }
1001}
1002
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001003void SkGPipeCanvas::drawPosText(const void* text, size_t byteLength,
reed@google.combb6992a2011-04-26 17:41:56 +00001004 const SkPoint pos[], const SkPaint& paint) {
1005 if (byteLength) {
reed@google.comacd471f2011-05-03 21:26:46 +00001006 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +00001007 this->writePaint(paint);
reed@google.combb6992a2011-04-26 17:41:56 +00001008 int count = paint.textToGlyphs(text, byteLength, NULL);
reed@google.comacd471f2011-05-03 21:26:46 +00001009 if (this->needOpBytes(4 + SkAlign4(byteLength) + 4 + count * sizeof(SkPoint))) {
reed@google.com31891582011-05-12 03:03:56 +00001010 this->writeOp(kDrawPosText_DrawOp);
reed@google.comacd471f2011-05-03 21:26:46 +00001011 fWriter.write32(byteLength);
1012 fWriter.writePad(text, byteLength);
1013 fWriter.write32(count);
1014 fWriter.write(pos, count * sizeof(SkPoint));
1015 }
reed@google.combb6992a2011-04-26 17:41:56 +00001016 }
1017}
1018
1019void SkGPipeCanvas::drawPosTextH(const void* text, size_t byteLength,
1020 const SkScalar xpos[], SkScalar constY,
1021 const SkPaint& paint) {
1022 if (byteLength) {
reed@google.comacd471f2011-05-03 21:26:46 +00001023 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +00001024 this->writePaint(paint);
reed@google.combb6992a2011-04-26 17:41:56 +00001025 int count = paint.textToGlyphs(text, byteLength, NULL);
reed@google.comacd471f2011-05-03 21:26:46 +00001026 if (this->needOpBytes(4 + SkAlign4(byteLength) + 4 + count * sizeof(SkScalar) + 4)) {
reed@google.com31891582011-05-12 03:03:56 +00001027 this->writeOp(kDrawPosTextH_DrawOp);
reed@google.comacd471f2011-05-03 21:26:46 +00001028 fWriter.write32(byteLength);
1029 fWriter.writePad(text, byteLength);
1030 fWriter.write32(count);
1031 fWriter.write(xpos, count * sizeof(SkScalar));
1032 fWriter.writeScalar(constY);
1033 }
reed@google.combb6992a2011-04-26 17:41:56 +00001034 }
1035}
1036
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001037void SkGPipeCanvas::drawTextOnPath(const void* text, size_t byteLength,
1038 const SkPath& path, const SkMatrix* matrix,
reed@google.combb6992a2011-04-26 17:41:56 +00001039 const SkPaint& paint) {
1040 if (byteLength) {
reed@google.comacd471f2011-05-03 21:26:46 +00001041 NOTIFY_SETUP(this);
reed@google.combb6992a2011-04-26 17:41:56 +00001042 unsigned flags = 0;
djsollen@google.com94e75ee2012-06-08 18:30:46 +00001043 size_t size = 4 + SkAlign4(byteLength) + path.writeToMemory(NULL);
reed@google.combb6992a2011-04-26 17:41:56 +00001044 if (matrix) {
1045 flags |= kDrawTextOnPath_HasMatrix_DrawOpFlag;
djsollen@google.com94e75ee2012-06-08 18:30:46 +00001046 size += matrix->writeToMemory(NULL);
reed@google.combb6992a2011-04-26 17:41:56 +00001047 }
reed@google.com31891582011-05-12 03:03:56 +00001048 this->writePaint(paint);
reed@google.comacd471f2011-05-03 21:26:46 +00001049 if (this->needOpBytes(size)) {
reed@google.com31891582011-05-12 03:03:56 +00001050 this->writeOp(kDrawTextOnPath_DrawOp, flags, 0);
reed@google.combb6992a2011-04-26 17:41:56 +00001051
reed@google.comacd471f2011-05-03 21:26:46 +00001052 fWriter.write32(byteLength);
1053 fWriter.writePad(text, byteLength);
reed@google.combb6992a2011-04-26 17:41:56 +00001054
djsollen@google.com94e75ee2012-06-08 18:30:46 +00001055 fWriter.writePath(path);
reed@google.comacd471f2011-05-03 21:26:46 +00001056 if (matrix) {
djsollen@google.com2b2ede32012-04-12 13:24:04 +00001057 fWriter.writeMatrix(*matrix);
reed@google.comacd471f2011-05-03 21:26:46 +00001058 }
reed@google.combb6992a2011-04-26 17:41:56 +00001059 }
1060 }
1061}
1062
1063void SkGPipeCanvas::drawPicture(SkPicture& picture) {
reed@google.com0faac1e2011-05-11 05:58:58 +00001064 // we want to playback the picture into individual draw calls
1065 this->INHERITED::drawPicture(picture);
reed@google.combb6992a2011-04-26 17:41:56 +00001066}
1067
reed@google.combb6992a2011-04-26 17:41:56 +00001068void SkGPipeCanvas::drawVertices(VertexMode mode, int vertexCount,
1069 const SkPoint vertices[], const SkPoint texs[],
1070 const SkColor colors[], SkXfermode*,
1071 const uint16_t indices[], int indexCount,
1072 const SkPaint& paint) {
1073 if (0 == vertexCount) {
1074 return;
1075 }
1076
reed@google.comacd471f2011-05-03 21:26:46 +00001077 NOTIFY_SETUP(this);
1078 size_t size = 4 + vertexCount * sizeof(SkPoint);
reed@google.com31891582011-05-12 03:03:56 +00001079 this->writePaint(paint);
reed@google.combb6992a2011-04-26 17:41:56 +00001080 unsigned flags = 0;
1081 if (texs) {
1082 flags |= kDrawVertices_HasTexs_DrawOpFlag;
reed@google.comacd471f2011-05-03 21:26:46 +00001083 size += vertexCount * sizeof(SkPoint);
reed@google.combb6992a2011-04-26 17:41:56 +00001084 }
1085 if (colors) {
1086 flags |= kDrawVertices_HasColors_DrawOpFlag;
reed@google.comacd471f2011-05-03 21:26:46 +00001087 size += vertexCount * sizeof(SkColor);
reed@google.combb6992a2011-04-26 17:41:56 +00001088 }
1089 if (indices && indexCount > 0) {
1090 flags |= kDrawVertices_HasIndices_DrawOpFlag;
reed@google.comacd471f2011-05-03 21:26:46 +00001091 size += 4 + SkAlign4(indexCount * sizeof(uint16_t));
reed@google.combb6992a2011-04-26 17:41:56 +00001092 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001093
reed@google.comacd471f2011-05-03 21:26:46 +00001094 if (this->needOpBytes(size)) {
reed@google.com31891582011-05-12 03:03:56 +00001095 this->writeOp(kDrawVertices_DrawOp, flags, 0);
reed@google.comacd471f2011-05-03 21:26:46 +00001096 fWriter.write32(mode);
1097 fWriter.write32(vertexCount);
1098 fWriter.write(vertices, vertexCount * sizeof(SkPoint));
1099 if (texs) {
1100 fWriter.write(texs, vertexCount * sizeof(SkPoint));
1101 }
1102 if (colors) {
1103 fWriter.write(colors, vertexCount * sizeof(SkColor));
1104 }
reed@google.combb6992a2011-04-26 17:41:56 +00001105
reed@google.comacd471f2011-05-03 21:26:46 +00001106 // TODO: flatten xfermode
reed@google.combb6992a2011-04-26 17:41:56 +00001107
reed@google.comacd471f2011-05-03 21:26:46 +00001108 if (indices && indexCount > 0) {
1109 fWriter.write32(indexCount);
1110 fWriter.writePad(indices, indexCount * sizeof(uint16_t));
1111 }
reed@google.combb6992a2011-04-26 17:41:56 +00001112 }
1113}
1114
reed@google.comacd471f2011-05-03 21:26:46 +00001115void SkGPipeCanvas::drawData(const void* ptr, size_t size) {
1116 if (size && ptr) {
1117 NOTIFY_SETUP(this);
reed@google.combb6992a2011-04-26 17:41:56 +00001118 unsigned data = 0;
1119 if (size < (1 << DRAWOPS_DATA_BITS)) {
1120 data = (unsigned)size;
1121 }
reed@google.comacd471f2011-05-03 21:26:46 +00001122 if (this->needOpBytes(4 + SkAlign4(size))) {
1123 this->writeOp(kDrawData_DrawOp, 0, data);
1124 if (0 == data) {
1125 fWriter.write32(size);
1126 }
reed@google.combb6793b2011-05-05 15:18:15 +00001127 fWriter.writePad(ptr, size);
reed@google.combb6992a2011-04-26 17:41:56 +00001128 }
1129 }
1130}
1131
junov@chromium.org77eec242012-07-18 17:54:45 +00001132void SkGPipeCanvas::flushRecording(bool detachCurrentBlock) {
1133 doNotify();
1134 if (detachCurrentBlock) {
1135 // force a new block to be requested for the next recorded command
1136 fBlockSize = 0;
1137 }
1138}
1139
reed@google.combb6992a2011-04-26 17:41:56 +00001140///////////////////////////////////////////////////////////////////////////////
1141
1142template <typename T> uint32_t castToU32(T value) {
1143 union {
1144 T fSrc;
1145 uint32_t fDst;
1146 } data;
1147 data.fSrc = value;
1148 return data.fDst;
1149}
1150
reed@google.com31891582011-05-12 03:03:56 +00001151void SkGPipeCanvas::writePaint(const SkPaint& paint) {
1152 SkPaint& base = fPaint;
reed@google.combb6992a2011-04-26 17:41:56 +00001153 uint32_t storage[32];
1154 uint32_t* ptr = storage;
reed@google.combb6992a2011-04-26 17:41:56 +00001155
1156 if (base.getFlags() != paint.getFlags()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001157 *ptr++ = PaintOp_packOpData(kFlags_PaintOp, paint.getFlags());
reed@google.comf5842f72011-05-04 18:30:04 +00001158 base.setFlags(paint.getFlags());
reed@google.combb6992a2011-04-26 17:41:56 +00001159 }
1160 if (base.getColor() != paint.getColor()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001161 *ptr++ = PaintOp_packOp(kColor_PaintOp);
1162 *ptr++ = paint.getColor();
reed@google.comf5842f72011-05-04 18:30:04 +00001163 base.setColor(paint.getColor());
reed@google.combb6992a2011-04-26 17:41:56 +00001164 }
1165 if (base.getStyle() != paint.getStyle()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001166 *ptr++ = PaintOp_packOpData(kStyle_PaintOp, paint.getStyle());
reed@google.comf5842f72011-05-04 18:30:04 +00001167 base.setStyle(paint.getStyle());
reed@google.combb6992a2011-04-26 17:41:56 +00001168 }
1169 if (base.getStrokeJoin() != paint.getStrokeJoin()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001170 *ptr++ = PaintOp_packOpData(kJoin_PaintOp, paint.getStrokeJoin());
reed@google.comf5842f72011-05-04 18:30:04 +00001171 base.setStrokeJoin(paint.getStrokeJoin());
reed@google.combb6992a2011-04-26 17:41:56 +00001172 }
1173 if (base.getStrokeCap() != paint.getStrokeCap()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001174 *ptr++ = PaintOp_packOpData(kCap_PaintOp, paint.getStrokeCap());
reed@google.comf5842f72011-05-04 18:30:04 +00001175 base.setStrokeCap(paint.getStrokeCap());
reed@google.combb6992a2011-04-26 17:41:56 +00001176 }
1177 if (base.getStrokeWidth() != paint.getStrokeWidth()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001178 *ptr++ = PaintOp_packOp(kWidth_PaintOp);
1179 *ptr++ = castToU32(paint.getStrokeWidth());
reed@google.comf5842f72011-05-04 18:30:04 +00001180 base.setStrokeWidth(paint.getStrokeWidth());
reed@google.combb6992a2011-04-26 17:41:56 +00001181 }
1182 if (base.getStrokeMiter() != paint.getStrokeMiter()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001183 *ptr++ = PaintOp_packOp(kMiter_PaintOp);
1184 *ptr++ = castToU32(paint.getStrokeMiter());
reed@google.comf5842f72011-05-04 18:30:04 +00001185 base.setStrokeMiter(paint.getStrokeMiter());
reed@google.combb6992a2011-04-26 17:41:56 +00001186 }
1187 if (base.getTextEncoding() != paint.getTextEncoding()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001188 *ptr++ = PaintOp_packOpData(kEncoding_PaintOp, paint.getTextEncoding());
reed@google.comf5842f72011-05-04 18:30:04 +00001189 base.setTextEncoding(paint.getTextEncoding());
reed@google.combb6992a2011-04-26 17:41:56 +00001190 }
1191 if (base.getHinting() != paint.getHinting()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001192 *ptr++ = PaintOp_packOpData(kHinting_PaintOp, paint.getHinting());
reed@google.comf5842f72011-05-04 18:30:04 +00001193 base.setHinting(paint.getHinting());
reed@google.combb6992a2011-04-26 17:41:56 +00001194 }
1195 if (base.getTextAlign() != paint.getTextAlign()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001196 *ptr++ = PaintOp_packOpData(kAlign_PaintOp, paint.getTextAlign());
reed@google.comf5842f72011-05-04 18:30:04 +00001197 base.setTextAlign(paint.getTextAlign());
reed@google.combb6992a2011-04-26 17:41:56 +00001198 }
1199 if (base.getTextSize() != paint.getTextSize()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001200 *ptr++ = PaintOp_packOp(kTextSize_PaintOp);
1201 *ptr++ = castToU32(paint.getTextSize());
reed@google.comf5842f72011-05-04 18:30:04 +00001202 base.setTextSize(paint.getTextSize());
reed@google.combb6992a2011-04-26 17:41:56 +00001203 }
1204 if (base.getTextScaleX() != paint.getTextScaleX()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001205 *ptr++ = PaintOp_packOp(kTextScaleX_PaintOp);
1206 *ptr++ = castToU32(paint.getTextScaleX());
reed@google.comf5842f72011-05-04 18:30:04 +00001207 base.setTextScaleX(paint.getTextScaleX());
reed@google.combb6992a2011-04-26 17:41:56 +00001208 }
1209 if (base.getTextSkewX() != paint.getTextSkewX()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001210 *ptr++ = PaintOp_packOp(kTextSkewX_PaintOp);
1211 *ptr++ = castToU32(paint.getTextSkewX());
reed@google.comf5842f72011-05-04 18:30:04 +00001212 base.setTextSkewX(paint.getTextSkewX());
1213 }
1214
1215 if (!SkTypeface::Equal(base.getTypeface(), paint.getTypeface())) {
1216 uint32_t id = this->getTypefaceID(paint.getTypeface());
reed@google.comf5842f72011-05-04 18:30:04 +00001217 *ptr++ = PaintOp_packOpData(kTypeface_PaintOp, id);
1218 base.setTypeface(paint.getTypeface());
reed@google.combb6992a2011-04-26 17:41:56 +00001219 }
reed@google.combb6992a2011-04-26 17:41:56 +00001220
scroggo@google.com4dffc592012-07-17 16:49:40 +00001221 // This is a new paint, so all old flats can be safely purged, if necessary.
1222 fFlattenableHeap.markAllFlatsSafeToDelete();
reed@google.comb55d1182011-05-11 00:42:04 +00001223 for (int i = 0; i < kCount_PaintFlats; i++) {
1224 int index = this->flattenToIndex(get_paintflat(paint, i), (PaintFlats)i);
scroggo@google.comd3ba5cc2012-07-09 16:05:53 +00001225 bool replaced = index < 0;
1226 if (replaced) {
1227 index = ~index;
1228 }
scroggo@google.com4dffc592012-07-17 16:49:40 +00001229 // Store the index of any flat that needs to be kept. 0 means no flat.
1230 if (index > 0) {
1231 fFlattenableHeap.markFlatForKeeping(index);
1232 }
1233 SkASSERT(index >= 0 && index <= fFlatDictionary.count());
scroggo@google.comd3ba5cc2012-07-09 16:05:53 +00001234 if (index != fCurrFlatIndex[i] || replaced) {
reed@google.comb55d1182011-05-11 00:42:04 +00001235 *ptr++ = PaintOp_packOpFlagData(kFlatIndex_PaintOp, i, index);
1236 fCurrFlatIndex[i] = index;
1237 }
1238 }
1239
reed@google.comacd471f2011-05-03 21:26:46 +00001240 size_t size = (char*)ptr - (char*)storage;
1241 if (size && this->needOpBytes(size)) {
reed@google.comb55d1182011-05-11 00:42:04 +00001242 this->writeOp(kPaintOp_DrawOp, 0, size);
reed@google.comb55d1182011-05-11 00:42:04 +00001243 fWriter.write(storage, size);
reed@google.combb6992a2011-04-26 17:41:56 +00001244 for (size_t i = 0; i < size/4; i++) {
reed@google.comb55d1182011-05-11 00:42:04 +00001245// SkDebugf("[%d] %08X\n", i, storage[i]);
reed@google.combb6992a2011-04-26 17:41:56 +00001246 }
1247 }
reed@google.combb6992a2011-04-26 17:41:56 +00001248}
1249
1250///////////////////////////////////////////////////////////////////////////////
1251
1252#include "SkGPipe.h"
1253
reed@google.comacd471f2011-05-03 21:26:46 +00001254SkGPipeWriter::SkGPipeWriter() : fWriter(0) {
reed@google.combb6992a2011-04-26 17:41:56 +00001255 fCanvas = NULL;
1256}
1257
1258SkGPipeWriter::~SkGPipeWriter() {
reed@google.comacd471f2011-05-03 21:26:46 +00001259 this->endRecording();
reed@google.combb6992a2011-04-26 17:41:56 +00001260 SkSafeUnref(fCanvas);
1261}
1262
scroggo@google.com565254b2012-06-28 15:41:32 +00001263SkCanvas* SkGPipeWriter::startRecording(SkGPipeController* controller, uint32_t flags) {
reed@google.combb6992a2011-04-26 17:41:56 +00001264 if (NULL == fCanvas) {
reed@google.comacd471f2011-05-03 21:26:46 +00001265 fWriter.reset(NULL, 0);
reed@google.comdde09562011-05-23 12:21:05 +00001266 fFactorySet.reset();
1267 fCanvas = SkNEW_ARGS(SkGPipeCanvas, (controller, &fWriter,
1268 (flags & kCrossProcess_Flag) ?
scroggo@google.com565254b2012-06-28 15:41:32 +00001269 &fFactorySet : NULL, flags));
reed@google.combb6992a2011-04-26 17:41:56 +00001270 }
1271 return fCanvas;
1272}
1273
1274void SkGPipeWriter::endRecording() {
1275 if (fCanvas) {
1276 fCanvas->finish();
1277 fCanvas->unref();
1278 fCanvas = NULL;
1279 }
1280}
1281
junov@chromium.org77eec242012-07-18 17:54:45 +00001282void SkGPipeWriter::flushRecording(bool detachCurrentBlock){
1283 fCanvas->flushRecording(detachCurrentBlock);
1284}
1285
scroggo@google.com15011ee2012-07-26 20:03:32 +00001286size_t SkGPipeWriter::storageAllocatedForRecording() {
1287 return NULL == fCanvas ? 0 : fCanvas->storageAllocatedForRecording();
1288}
1289