blob: 9efc0b58bb43eeef7b8fcbfe8607500efbc57728 [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
scroggo@google.com58be6822012-07-30 14:40:01 +0000486 // Common code used by drawBitmap* when flattening.
487 bool commonDrawBitmapFlatten(const SkBitmap& bm, DrawOps op, unsigned flags,
488 size_t opBytesNeeded, const SkPaint* paint);
489 // Common code used by drawBitmap* when storing in the heap.
490 bool commonDrawBitmapHeap(const SkBitmap& bm, DrawOps op, unsigned flags,
491 size_t opBytesNeeded, const SkPaint* paint);
492 // Convenience type for function pointer
493 typedef bool (SkGPipeCanvas::*BitmapCommonFunction)(const SkBitmap&,
494 DrawOps, unsigned,
495 size_t, const SkPaint*);
496
reed@google.com31891582011-05-12 03:03:56 +0000497 SkPaint fPaint;
498 void writePaint(const SkPaint&);
reed@google.combb6992a2011-04-26 17:41:56 +0000499
reed@google.comacd471f2011-05-03 21:26:46 +0000500 class AutoPipeNotify {
501 public:
502 AutoPipeNotify(SkGPipeCanvas* canvas) : fCanvas(canvas) {}
503 ~AutoPipeNotify() { fCanvas->doNotify(); }
504 private:
505 SkGPipeCanvas* fCanvas;
506 };
507 friend class AutoPipeNotify;
508
reed@google.combb6992a2011-04-26 17:41:56 +0000509 typedef SkCanvas INHERITED;
510};
511
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000512int SkGPipeCanvas::flattenToIndex(const SkBitmap & bitmap) {
scroggo@google.com565254b2012-06-28 15:41:32 +0000513 SkASSERT(shouldFlattenBitmaps(fFlags));
scroggo@google.com4dffc592012-07-17 16:49:40 +0000514 uint32_t flags = SkFlattenableWriteBuffer::kInlineFactoryNames_Flag
515 | SkFlattenableWriteBuffer::kCrossProcess_Flag;
516 bool added, replaced;
517 const SkFlatData* flat = fBitmapDictionary.findAndReplace(
518 bitmap, flags, fBitmapHeap.flatToReplace(), &added, &replaced);
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000519
scroggo@google.com4dffc592012-07-17 16:49:40 +0000520 int index = flat->index();
521 if (added && this->needOpBytes(flat->flatSize())) {
522 this->writeOp(kDef_Bitmap_DrawOp, 0, index);
523 fWriter.write(flat->data(), flat->flatSize());
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000524 }
scroggo@google.com4dffc592012-07-17 16:49:40 +0000525 return index;
scroggo@google.comd3ba5cc2012-07-09 16:05:53 +0000526}
527
reed@google.comb55d1182011-05-11 00:42:04 +0000528// return 0 for NULL (or unflattenable obj), or index-base-1
scroggo@google.comd3ba5cc2012-07-09 16:05:53 +0000529// return ~(index-base-1) if an old flattenable was replaced
reed@google.comb55d1182011-05-11 00:42:04 +0000530int SkGPipeCanvas::flattenToIndex(SkFlattenable* obj, PaintFlats paintflat) {
531 if (NULL == obj) {
532 return 0;
533 }
reed@google.comb55d1182011-05-11 00:42:04 +0000534
scroggo@google.com4dffc592012-07-17 16:49:40 +0000535 uint32_t writeBufferFlags;
scroggo@google.com3cb969f2012-07-27 20:39:19 +0000536 if (SkToBool(fFlags & SkGPipeWriter::kCrossProcess_Flag)) {
scroggo@google.com4dffc592012-07-17 16:49:40 +0000537 writeBufferFlags = (SkFlattenableWriteBuffer::kInlineFactoryNames_Flag
538 | SkFlattenableWriteBuffer::kCrossProcess_Flag);
scroggo@google.com565254b2012-06-28 15:41:32 +0000539 } else {
540 // Needed for bitmap shaders.
scroggo@google.com4dffc592012-07-17 16:49:40 +0000541 writeBufferFlags = SkFlattenableWriteBuffer::kForceFlattenBitmapPixels_Flag;
scroggo@google.com565254b2012-06-28 15:41:32 +0000542 }
reed@google.comdde09562011-05-23 12:21:05 +0000543
scroggo@google.com4dffc592012-07-17 16:49:40 +0000544 bool added, replaced;
545 const SkFlatData* flat = fFlatDictionary.findAndReplace(
546 *obj, writeBufferFlags, fFlattenableHeap.flatToReplace(), &added, &replaced);
547 int index = flat->index();
548 if (added && this->needOpBytes(flat->flatSize())) {
549 this->writeOp(kDef_Flattenable_DrawOp, paintflat, index);
550 fWriter.write(flat->data(), flat->flatSize());
551 }
552 if (replaced) {
reed@google.comb55d1182011-05-11 00:42:04 +0000553 index = ~index;
reed@google.comb55d1182011-05-11 00:42:04 +0000554 }
scroggo@google.com4dffc592012-07-17 16:49:40 +0000555 return index;
reed@google.comb55d1182011-05-11 00:42:04 +0000556}
557
reed@google.combb6992a2011-04-26 17:41:56 +0000558///////////////////////////////////////////////////////////////////////////////
559
reed@google.comacd471f2011-05-03 21:26:46 +0000560#define MIN_BLOCK_SIZE (16 * 1024)
scroggo@google.com4dffc592012-07-17 16:49:40 +0000561#define BITMAPS_TO_KEEP 5
562#define FLATTENABLES_TO_KEEP 10
reed@google.combb6992a2011-04-26 17:41:56 +0000563
reed@google.comacd471f2011-05-03 21:26:46 +0000564SkGPipeCanvas::SkGPipeCanvas(SkGPipeController* controller,
scroggo@google.com565254b2012-06-28 15:41:32 +0000565 SkWriter32* writer, SkFactorySet* fset, uint32_t flags)
scroggo@google.com4dffc592012-07-17 16:49:40 +0000566: fSharedHeap(!(flags & SkGPipeWriter::kCrossProcess_Flag), controller->numberOfReaders())
567, fWriter(*writer), fFlags(flags)
568, fBitmapHeap(BITMAPS_TO_KEEP), fBitmapDictionary(&fBitmapHeap, NULL, NULL, fset)
569, fFlattenableHeap(FLATTENABLES_TO_KEEP), fFlatDictionary(&fFlattenableHeap, fset) {
reed@google.comacd471f2011-05-03 21:26:46 +0000570 fController = controller;
reed@google.combb6992a2011-04-26 17:41:56 +0000571 fDone = false;
reed@google.comacd471f2011-05-03 21:26:46 +0000572 fBlockSize = 0; // need first block from controller
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000573 fBytesNotified = 0;
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000574 fFirstSaveLayerStackLevel = kNoSaveLayer;
reed@google.comb55d1182011-05-11 00:42:04 +0000575 sk_bzero(fCurrFlatIndex, sizeof(fCurrFlatIndex));
reed@google.comacd471f2011-05-03 21:26:46 +0000576
reed@google.combb6793b2011-05-05 15:18:15 +0000577 // we need a device to limit our clip
578 // should the caller give us the bounds?
yangsu@google.com06b4da162011-06-17 15:04:40 +0000579 // We don't allocate pixels for the bitmap
reed@google.combb6793b2011-05-05 15:18:15 +0000580 SkBitmap bitmap;
581 bitmap.setConfig(SkBitmap::kARGB_8888_Config, 32767, 32767);
yangsu@google.com06b4da162011-06-17 15:04:40 +0000582 SkDevice* device = SkNEW_ARGS(SkDevice, (bitmap));
reed@google.combb6793b2011-05-05 15:18:15 +0000583 this->setDevice(device)->unref();
scroggo@google.com565254b2012-06-28 15:41:32 +0000584 // Tell the reader the appropriate flags to use.
585 if (this->needOpBytes()) {
586 this->writeOp(kReportFlags_DrawOp, fFlags, 0);
587 }
reed@google.combb6992a2011-04-26 17:41:56 +0000588}
589
590SkGPipeCanvas::~SkGPipeCanvas() {
591 this->finish();
reed@google.combb6992a2011-04-26 17:41:56 +0000592}
593
reed@google.comacd471f2011-05-03 21:26:46 +0000594bool SkGPipeCanvas::needOpBytes(size_t needed) {
595 if (fDone) {
596 return false;
597 }
598
599 needed += 4; // size of DrawOp atom
600 if (fWriter.size() + needed > fBlockSize) {
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000601 // Before we wipe out any data that has already been written, read it
602 // out.
603 this->doNotify();
604 size_t blockSize = SkMax32(MIN_BLOCK_SIZE, needed);
605 void* block = fController->requestBlock(blockSize, &fBlockSize);
reed@google.comacd471f2011-05-03 21:26:46 +0000606 if (NULL == block) {
607 fDone = true;
608 return false;
609 }
610 fWriter.reset(block, fBlockSize);
611 fBytesNotified = 0;
612 }
613 return true;
614}
615
reed@google.comf5842f72011-05-04 18:30:04 +0000616uint32_t SkGPipeCanvas::getTypefaceID(SkTypeface* face) {
617 uint32_t id = 0; // 0 means default/null typeface
618 if (face) {
619 id = fTypefaceSet.find(face);
620 if (0 == id) {
621 id = fTypefaceSet.add(face);
622 size_t size = writeTypeface(NULL, face);
623 if (this->needOpBytes(size)) {
reed@google.combb6793b2011-05-05 15:18:15 +0000624 this->writeOp(kDef_Typeface_DrawOp);
reed@google.comf5842f72011-05-04 18:30:04 +0000625 writeTypeface(&fWriter, face);
626 }
627 }
628 }
629 return id;
630}
631
reed@google.combb6992a2011-04-26 17:41:56 +0000632///////////////////////////////////////////////////////////////////////////////
633
reed@google.comacd471f2011-05-03 21:26:46 +0000634#define NOTIFY_SETUP(canvas) \
635 AutoPipeNotify apn(canvas)
636
reed@google.combb6992a2011-04-26 17:41:56 +0000637int SkGPipeCanvas::save(SaveFlags flags) {
reed@google.comacd471f2011-05-03 21:26:46 +0000638 NOTIFY_SETUP(this);
639 if (this->needOpBytes()) {
640 this->writeOp(kSave_DrawOp, 0, flags);
641 }
reed@google.combb6992a2011-04-26 17:41:56 +0000642 return this->INHERITED::save(flags);
643}
644
645int SkGPipeCanvas::saveLayer(const SkRect* bounds, const SkPaint* paint,
646 SaveFlags saveFlags) {
reed@google.comacd471f2011-05-03 21:26:46 +0000647 NOTIFY_SETUP(this);
648 size_t size = 0;
reed@google.combb6992a2011-04-26 17:41:56 +0000649 unsigned opFlags = 0;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000650
reed@google.combb6992a2011-04-26 17:41:56 +0000651 if (bounds) {
652 opFlags |= kSaveLayer_HasBounds_DrawOpFlag;
reed@google.comacd471f2011-05-03 21:26:46 +0000653 size += sizeof(SkRect);
reed@google.combb6992a2011-04-26 17:41:56 +0000654 }
655 if (paint) {
656 opFlags |= kSaveLayer_HasPaint_DrawOpFlag;
reed@google.com31891582011-05-12 03:03:56 +0000657 this->writePaint(*paint);
reed@google.combb6992a2011-04-26 17:41:56 +0000658 }
659
reed@google.comacd471f2011-05-03 21:26:46 +0000660 if (this->needOpBytes(size)) {
661 this->writeOp(kSaveLayer_DrawOp, opFlags, saveFlags);
662 if (bounds) {
663 fWriter.writeRect(*bounds);
664 }
reed@google.combb6992a2011-04-26 17:41:56 +0000665 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000666
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000667 if (kNoSaveLayer == fFirstSaveLayerStackLevel){
668 fFirstSaveLayerStackLevel = this->getSaveCount();
669 }
reed@google.combb6992a2011-04-26 17:41:56 +0000670 // we just pass on the save, so we don't create a layer
671 return this->INHERITED::save(saveFlags);
672}
673
674void SkGPipeCanvas::restore() {
reed@google.comacd471f2011-05-03 21:26:46 +0000675 NOTIFY_SETUP(this);
676 if (this->needOpBytes()) {
677 this->writeOp(kRestore_DrawOp);
678 }
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000679
reed@google.combb6992a2011-04-26 17:41:56 +0000680 this->INHERITED::restore();
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000681
682 if (this->getSaveCount() == fFirstSaveLayerStackLevel){
683 fFirstSaveLayerStackLevel = kNoSaveLayer;
684 }
685}
686
687bool SkGPipeCanvas::isDrawingToLayer() const {
688 return kNoSaveLayer != fFirstSaveLayerStackLevel;
reed@google.combb6992a2011-04-26 17:41:56 +0000689}
690
691bool SkGPipeCanvas::translate(SkScalar dx, SkScalar dy) {
692 if (dx || dy) {
reed@google.comacd471f2011-05-03 21:26:46 +0000693 NOTIFY_SETUP(this);
694 if (this->needOpBytes(2 * sizeof(SkScalar))) {
695 this->writeOp(kTranslate_DrawOp);
696 fWriter.writeScalar(dx);
697 fWriter.writeScalar(dy);
698 }
reed@google.combb6992a2011-04-26 17:41:56 +0000699 }
700 return this->INHERITED::translate(dx, dy);
701}
702
703bool SkGPipeCanvas::scale(SkScalar sx, SkScalar sy) {
704 if (sx || sy) {
reed@google.comacd471f2011-05-03 21:26:46 +0000705 NOTIFY_SETUP(this);
706 if (this->needOpBytes(2 * sizeof(SkScalar))) {
707 this->writeOp(kScale_DrawOp);
708 fWriter.writeScalar(sx);
709 fWriter.writeScalar(sy);
710 }
reed@google.combb6992a2011-04-26 17:41:56 +0000711 }
712 return this->INHERITED::scale(sx, sy);
713}
714
715bool SkGPipeCanvas::rotate(SkScalar degrees) {
716 if (degrees) {
reed@google.comacd471f2011-05-03 21:26:46 +0000717 NOTIFY_SETUP(this);
718 if (this->needOpBytes(sizeof(SkScalar))) {
719 this->writeOp(kRotate_DrawOp);
720 fWriter.writeScalar(degrees);
721 }
reed@google.combb6992a2011-04-26 17:41:56 +0000722 }
723 return this->INHERITED::rotate(degrees);
724}
725
726bool SkGPipeCanvas::skew(SkScalar sx, SkScalar sy) {
727 if (sx || sy) {
reed@google.comacd471f2011-05-03 21:26:46 +0000728 NOTIFY_SETUP(this);
729 if (this->needOpBytes(2 * sizeof(SkScalar))) {
730 this->writeOp(kSkew_DrawOp);
731 fWriter.writeScalar(sx);
732 fWriter.writeScalar(sy);
733 }
reed@google.combb6992a2011-04-26 17:41:56 +0000734 }
735 return this->INHERITED::skew(sx, sy);
736}
737
738bool SkGPipeCanvas::concat(const SkMatrix& matrix) {
739 if (!matrix.isIdentity()) {
reed@google.comacd471f2011-05-03 21:26:46 +0000740 NOTIFY_SETUP(this);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000741 if (this->needOpBytes(matrix.writeToMemory(NULL))) {
reed@google.comacd471f2011-05-03 21:26:46 +0000742 this->writeOp(kConcat_DrawOp);
djsollen@google.com2b2ede32012-04-12 13:24:04 +0000743 fWriter.writeMatrix(matrix);
reed@google.comacd471f2011-05-03 21:26:46 +0000744 }
reed@google.combb6992a2011-04-26 17:41:56 +0000745 }
746 return this->INHERITED::concat(matrix);
747}
748
749void SkGPipeCanvas::setMatrix(const SkMatrix& matrix) {
reed@google.comacd471f2011-05-03 21:26:46 +0000750 NOTIFY_SETUP(this);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000751 if (this->needOpBytes(matrix.writeToMemory(NULL))) {
reed@google.comacd471f2011-05-03 21:26:46 +0000752 this->writeOp(kSetMatrix_DrawOp);
djsollen@google.com2b2ede32012-04-12 13:24:04 +0000753 fWriter.writeMatrix(matrix);
reed@google.comacd471f2011-05-03 21:26:46 +0000754 }
reed@google.combb6992a2011-04-26 17:41:56 +0000755 this->INHERITED::setMatrix(matrix);
756}
757
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000758bool SkGPipeCanvas::clipRect(const SkRect& rect, SkRegion::Op rgnOp,
759 bool doAntiAlias) {
reed@google.comacd471f2011-05-03 21:26:46 +0000760 NOTIFY_SETUP(this);
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000761 if (this->needOpBytes(sizeof(SkRect)) + sizeof(bool)) {
reed@google.comacd471f2011-05-03 21:26:46 +0000762 this->writeOp(kClipRect_DrawOp, 0, rgnOp);
763 fWriter.writeRect(rect);
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000764 fWriter.writeBool(doAntiAlias);
reed@google.comacd471f2011-05-03 21:26:46 +0000765 }
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000766 return this->INHERITED::clipRect(rect, rgnOp, doAntiAlias);
reed@google.combb6992a2011-04-26 17:41:56 +0000767}
768
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000769bool SkGPipeCanvas::clipPath(const SkPath& path, SkRegion::Op rgnOp,
770 bool doAntiAlias) {
reed@google.comacd471f2011-05-03 21:26:46 +0000771 NOTIFY_SETUP(this);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000772 if (this->needOpBytes(path.writeToMemory(NULL)) + sizeof(bool)) {
reed@google.comacd471f2011-05-03 21:26:46 +0000773 this->writeOp(kClipPath_DrawOp, 0, rgnOp);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000774 fWriter.writePath(path);
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000775 fWriter.writeBool(doAntiAlias);
reed@google.comacd471f2011-05-03 21:26:46 +0000776 }
reed@google.combb6992a2011-04-26 17:41:56 +0000777 // we just pass on the bounds of the path
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000778 return this->INHERITED::clipRect(path.getBounds(), rgnOp, doAntiAlias);
reed@google.combb6992a2011-04-26 17:41:56 +0000779}
780
781bool SkGPipeCanvas::clipRegion(const SkRegion& region, SkRegion::Op rgnOp) {
reed@google.comacd471f2011-05-03 21:26:46 +0000782 NOTIFY_SETUP(this);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000783 if (this->needOpBytes(region.writeToMemory(NULL))) {
reed@google.comacd471f2011-05-03 21:26:46 +0000784 this->writeOp(kClipRegion_DrawOp, 0, rgnOp);
djsollen@google.com2b2ede32012-04-12 13:24:04 +0000785 fWriter.writeRegion(region);
reed@google.comacd471f2011-05-03 21:26:46 +0000786 }
reed@google.combb6992a2011-04-26 17:41:56 +0000787 return this->INHERITED::clipRegion(region, rgnOp);
788}
789
790///////////////////////////////////////////////////////////////////////////////
791
792void SkGPipeCanvas::clear(SkColor color) {
reed@google.comacd471f2011-05-03 21:26:46 +0000793 NOTIFY_SETUP(this);
reed@google.combb6992a2011-04-26 17:41:56 +0000794 unsigned flags = 0;
795 if (color) {
796 flags |= kClear_HasColor_DrawOpFlag;
797 }
reed@google.comacd471f2011-05-03 21:26:46 +0000798 if (this->needOpBytes(sizeof(SkColor))) {
799 this->writeOp(kDrawClear_DrawOp, flags, 0);
800 if (color) {
801 fWriter.write32(color);
802 }
reed@google.combb6992a2011-04-26 17:41:56 +0000803 }
804}
805
806void SkGPipeCanvas::drawPaint(const SkPaint& paint) {
reed@google.comacd471f2011-05-03 21:26:46 +0000807 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000808 this->writePaint(paint);
reed@google.comacd471f2011-05-03 21:26:46 +0000809 if (this->needOpBytes()) {
reed@google.com31891582011-05-12 03:03:56 +0000810 this->writeOp(kDrawPaint_DrawOp);
reed@google.comacd471f2011-05-03 21:26:46 +0000811 }
reed@google.combb6992a2011-04-26 17:41:56 +0000812}
813
814void SkGPipeCanvas::drawPoints(PointMode mode, size_t count,
815 const SkPoint pts[], const SkPaint& paint) {
816 if (count) {
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(4 + count * sizeof(SkPoint))) {
reed@google.com31891582011-05-12 03:03:56 +0000820 this->writeOp(kDrawPoints_DrawOp, mode, 0);
reed@google.comacd471f2011-05-03 21:26:46 +0000821 fWriter.write32(count);
822 fWriter.write(pts, count * sizeof(SkPoint));
823 }
reed@google.combb6992a2011-04-26 17:41:56 +0000824 }
825}
826
827void SkGPipeCanvas::drawRect(const SkRect& rect, const SkPaint& paint) {
reed@google.comacd471f2011-05-03 21:26:46 +0000828 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000829 this->writePaint(paint);
reed@google.comacd471f2011-05-03 21:26:46 +0000830 if (this->needOpBytes(sizeof(SkRect))) {
reed@google.com31891582011-05-12 03:03:56 +0000831 this->writeOp(kDrawRect_DrawOp);
reed@google.comacd471f2011-05-03 21:26:46 +0000832 fWriter.writeRect(rect);
833 }
reed@google.combb6992a2011-04-26 17:41:56 +0000834}
835
836void SkGPipeCanvas::drawPath(const SkPath& path, const SkPaint& paint) {
reed@google.comacd471f2011-05-03 21:26:46 +0000837 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000838 this->writePaint(paint);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000839 if (this->needOpBytes(path.writeToMemory(NULL))) {
reed@google.com31891582011-05-12 03:03:56 +0000840 this->writeOp(kDrawPath_DrawOp);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000841 fWriter.writePath(path);
reed@google.comacd471f2011-05-03 21:26:46 +0000842 }
reed@google.combb6992a2011-04-26 17:41:56 +0000843}
844
scroggo@google.com58be6822012-07-30 14:40:01 +0000845bool SkGPipeCanvas::commonDrawBitmapFlatten(const SkBitmap& bm, DrawOps op,
846 unsigned flags,
847 size_t opBytesNeeded,
848 const SkPaint* paint) {
849 if (paint != NULL) {
850 flags |= kDrawBitmap_HasPaint_DrawOpsFlag;
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000851 this->writePaint(*paint);
852 }
scroggo@google.com58be6822012-07-30 14:40:01 +0000853 int bitmapIndex = this->flattenToIndex(bm);
scroggo@google.com565254b2012-06-28 15:41:32 +0000854 if (this->needOpBytes(opBytesNeeded)) {
scroggo@google.com58be6822012-07-30 14:40:01 +0000855 this->writeOp(op, flags, bitmapIndex);
856 return true;
857 }
858 return false;
859}
860
861bool SkGPipeCanvas::commonDrawBitmapHeap(const SkBitmap& bm, DrawOps op,
862 unsigned flags,
863 size_t opBytesNeeded,
864 const SkPaint* paint) {
865 const void* ptr = fSharedHeap.addBitmap(bm);
866 if (NULL == ptr) {
867 return false;
868 }
869 if (paint != NULL) {
870 flags |= kDrawBitmap_HasPaint_DrawOpsFlag;
871 this->writePaint(*paint);
872 }
873 if (this->needOpBytes(opBytesNeeded + sizeof(void*))) {
874 this->writeOp(op, flags, 0);
875 fWriter.writePtr(const_cast<void*>(ptr));
876 return true;
877 }
878 return false;
879}
880
881void SkGPipeCanvas::drawBitmap(const SkBitmap& bm, SkScalar left, SkScalar top,
882 const SkPaint* paint) {
883 NOTIFY_SETUP(this);
884 size_t opBytesNeeded = sizeof(SkScalar) * 2;
885
886 BitmapCommonFunction bitmapCommon = shouldFlattenBitmaps(fFlags) ?
887 &SkGPipeCanvas::commonDrawBitmapFlatten :
888 &SkGPipeCanvas::commonDrawBitmapHeap;
889
890 if ((*this.*bitmapCommon)(bm, kDrawBitmap_DrawOp, 0, opBytesNeeded, paint)) {
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000891 fWriter.writeScalar(left);
892 fWriter.writeScalar(top);
893 }
reed@google.combb6992a2011-04-26 17:41:56 +0000894}
895
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000896void SkGPipeCanvas::drawBitmapRect(const SkBitmap& bm, const SkIRect* src,
scroggo@google.com58be6822012-07-30 14:40:01 +0000897 const SkRect& dst, const SkPaint* paint) {
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000898 NOTIFY_SETUP(this);
scroggo@google.com58be6822012-07-30 14:40:01 +0000899 size_t opBytesNeeded = sizeof(SkRect);
900 bool hasSrc = src != NULL;
901 unsigned flags;
902 if (hasSrc) {
903 flags = kDrawBitmap_HasSrcRect_DrawOpsFlag;
904 opBytesNeeded += sizeof(int32_t) * 4;
905 } else {
906 flags = 0;
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000907 }
908
scroggo@google.com58be6822012-07-30 14:40:01 +0000909 BitmapCommonFunction bitmapCommon = shouldFlattenBitmaps(fFlags) ?
910 &SkGPipeCanvas::commonDrawBitmapFlatten :
911 &SkGPipeCanvas::commonDrawBitmapHeap;
912
913 if ((*this.*bitmapCommon)(bm, kDrawBitmapRect_DrawOp, flags, opBytesNeeded, paint)) {
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000914 if (hasSrc) {
915 fWriter.write32(src->fLeft);
916 fWriter.write32(src->fTop);
917 fWriter.write32(src->fRight);
918 fWriter.write32(src->fBottom);
919 }
920 fWriter.writeRect(dst);
921 }
reed@google.combb6992a2011-04-26 17:41:56 +0000922}
923
scroggo@google.com58be6822012-07-30 14:40:01 +0000924void SkGPipeCanvas::drawBitmapMatrix(const SkBitmap& bm, const SkMatrix& matrix,
925 const SkPaint* paint) {
926 NOTIFY_SETUP(this);
927 size_t opBytesNeeded = matrix.writeToMemory(NULL);
928
929 BitmapCommonFunction bitmapCommon = shouldFlattenBitmaps(fFlags) ?
930 &SkGPipeCanvas::commonDrawBitmapFlatten :
931 &SkGPipeCanvas::commonDrawBitmapHeap;
932
933 if ((*this.*bitmapCommon)(bm, kDrawBitmapMatrix_DrawOp, 0, opBytesNeeded, paint)) {
934 fWriter.writeMatrix(matrix);
935 }
reed@google.combb6992a2011-04-26 17:41:56 +0000936}
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000937
938void SkGPipeCanvas::drawBitmapNine(const SkBitmap& bm, const SkIRect& center,
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000939 const SkRect& dst, const SkPaint* paint) {
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000940 NOTIFY_SETUP(this);
scroggo@google.com58be6822012-07-30 14:40:01 +0000941 size_t opBytesNeeded = sizeof(int32_t) * 4 + sizeof(SkRect);
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000942
scroggo@google.com58be6822012-07-30 14:40:01 +0000943 BitmapCommonFunction bitmapCommon = shouldFlattenBitmaps(fFlags) ?
944 &SkGPipeCanvas::commonDrawBitmapFlatten :
945 &SkGPipeCanvas::commonDrawBitmapHeap;
946
947 if ((*this.*bitmapCommon)(bm, kDrawBitmapNine_DrawOp, 0, opBytesNeeded, paint)) {
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000948 fWriter.write32(center.fLeft);
949 fWriter.write32(center.fTop);
950 fWriter.write32(center.fRight);
951 fWriter.write32(center.fBottom);
952 fWriter.writeRect(dst);
953 }
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000954}
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000955
956void SkGPipeCanvas::drawSprite(const SkBitmap& bm, int left, int top,
957 const SkPaint* paint) {
958 NOTIFY_SETUP(this);
scroggo@google.com58be6822012-07-30 14:40:01 +0000959 size_t opBytesNeeded = sizeof(int32_t) * 2;
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000960
scroggo@google.com58be6822012-07-30 14:40:01 +0000961 BitmapCommonFunction bitmapCommon = shouldFlattenBitmaps(fFlags) ?
962 &SkGPipeCanvas::commonDrawBitmapFlatten :
963 &SkGPipeCanvas::commonDrawBitmapHeap;
964
965 if ((*this.*bitmapCommon)(bm, kDrawSprite_DrawOp, 0, opBytesNeeded, paint)) {
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000966 fWriter.write32(left);
967 fWriter.write32(top);
968 }
reed@google.combb6992a2011-04-26 17:41:56 +0000969}
970
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000971void SkGPipeCanvas::drawText(const void* text, size_t byteLength, SkScalar x,
reed@google.combb6992a2011-04-26 17:41:56 +0000972 SkScalar y, const SkPaint& paint) {
973 if (byteLength) {
reed@google.comacd471f2011-05-03 21:26:46 +0000974 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000975 this->writePaint(paint);
reed@google.comacd471f2011-05-03 21:26:46 +0000976 if (this->needOpBytes(4 + SkAlign4(byteLength) + 2 * sizeof(SkScalar))) {
reed@google.com31891582011-05-12 03:03:56 +0000977 this->writeOp(kDrawText_DrawOp);
reed@google.comacd471f2011-05-03 21:26:46 +0000978 fWriter.write32(byteLength);
979 fWriter.writePad(text, byteLength);
980 fWriter.writeScalar(x);
981 fWriter.writeScalar(y);
982 }
reed@google.combb6992a2011-04-26 17:41:56 +0000983 }
984}
985
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000986void SkGPipeCanvas::drawPosText(const void* text, size_t byteLength,
reed@google.combb6992a2011-04-26 17:41:56 +0000987 const SkPoint pos[], const SkPaint& paint) {
988 if (byteLength) {
reed@google.comacd471f2011-05-03 21:26:46 +0000989 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000990 this->writePaint(paint);
reed@google.combb6992a2011-04-26 17:41:56 +0000991 int count = paint.textToGlyphs(text, byteLength, NULL);
reed@google.comacd471f2011-05-03 21:26:46 +0000992 if (this->needOpBytes(4 + SkAlign4(byteLength) + 4 + count * sizeof(SkPoint))) {
reed@google.com31891582011-05-12 03:03:56 +0000993 this->writeOp(kDrawPosText_DrawOp);
reed@google.comacd471f2011-05-03 21:26:46 +0000994 fWriter.write32(byteLength);
995 fWriter.writePad(text, byteLength);
996 fWriter.write32(count);
997 fWriter.write(pos, count * sizeof(SkPoint));
998 }
reed@google.combb6992a2011-04-26 17:41:56 +0000999 }
1000}
1001
1002void SkGPipeCanvas::drawPosTextH(const void* text, size_t byteLength,
1003 const SkScalar xpos[], SkScalar constY,
1004 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(SkScalar) + 4)) {
reed@google.com31891582011-05-12 03:03:56 +00001010 this->writeOp(kDrawPosTextH_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(xpos, count * sizeof(SkScalar));
1015 fWriter.writeScalar(constY);
1016 }
reed@google.combb6992a2011-04-26 17:41:56 +00001017 }
1018}
1019
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001020void SkGPipeCanvas::drawTextOnPath(const void* text, size_t byteLength,
1021 const SkPath& path, const SkMatrix* matrix,
reed@google.combb6992a2011-04-26 17:41:56 +00001022 const SkPaint& paint) {
1023 if (byteLength) {
reed@google.comacd471f2011-05-03 21:26:46 +00001024 NOTIFY_SETUP(this);
reed@google.combb6992a2011-04-26 17:41:56 +00001025 unsigned flags = 0;
djsollen@google.com94e75ee2012-06-08 18:30:46 +00001026 size_t size = 4 + SkAlign4(byteLength) + path.writeToMemory(NULL);
reed@google.combb6992a2011-04-26 17:41:56 +00001027 if (matrix) {
1028 flags |= kDrawTextOnPath_HasMatrix_DrawOpFlag;
djsollen@google.com94e75ee2012-06-08 18:30:46 +00001029 size += matrix->writeToMemory(NULL);
reed@google.combb6992a2011-04-26 17:41:56 +00001030 }
reed@google.com31891582011-05-12 03:03:56 +00001031 this->writePaint(paint);
reed@google.comacd471f2011-05-03 21:26:46 +00001032 if (this->needOpBytes(size)) {
reed@google.com31891582011-05-12 03:03:56 +00001033 this->writeOp(kDrawTextOnPath_DrawOp, flags, 0);
reed@google.combb6992a2011-04-26 17:41:56 +00001034
reed@google.comacd471f2011-05-03 21:26:46 +00001035 fWriter.write32(byteLength);
1036 fWriter.writePad(text, byteLength);
reed@google.combb6992a2011-04-26 17:41:56 +00001037
djsollen@google.com94e75ee2012-06-08 18:30:46 +00001038 fWriter.writePath(path);
reed@google.comacd471f2011-05-03 21:26:46 +00001039 if (matrix) {
djsollen@google.com2b2ede32012-04-12 13:24:04 +00001040 fWriter.writeMatrix(*matrix);
reed@google.comacd471f2011-05-03 21:26:46 +00001041 }
reed@google.combb6992a2011-04-26 17:41:56 +00001042 }
1043 }
1044}
1045
1046void SkGPipeCanvas::drawPicture(SkPicture& picture) {
reed@google.com0faac1e2011-05-11 05:58:58 +00001047 // we want to playback the picture into individual draw calls
1048 this->INHERITED::drawPicture(picture);
reed@google.combb6992a2011-04-26 17:41:56 +00001049}
1050
reed@google.combb6992a2011-04-26 17:41:56 +00001051void SkGPipeCanvas::drawVertices(VertexMode mode, int vertexCount,
1052 const SkPoint vertices[], const SkPoint texs[],
1053 const SkColor colors[], SkXfermode*,
1054 const uint16_t indices[], int indexCount,
1055 const SkPaint& paint) {
1056 if (0 == vertexCount) {
1057 return;
1058 }
1059
reed@google.comacd471f2011-05-03 21:26:46 +00001060 NOTIFY_SETUP(this);
1061 size_t size = 4 + vertexCount * sizeof(SkPoint);
reed@google.com31891582011-05-12 03:03:56 +00001062 this->writePaint(paint);
reed@google.combb6992a2011-04-26 17:41:56 +00001063 unsigned flags = 0;
1064 if (texs) {
1065 flags |= kDrawVertices_HasTexs_DrawOpFlag;
reed@google.comacd471f2011-05-03 21:26:46 +00001066 size += vertexCount * sizeof(SkPoint);
reed@google.combb6992a2011-04-26 17:41:56 +00001067 }
1068 if (colors) {
1069 flags |= kDrawVertices_HasColors_DrawOpFlag;
reed@google.comacd471f2011-05-03 21:26:46 +00001070 size += vertexCount * sizeof(SkColor);
reed@google.combb6992a2011-04-26 17:41:56 +00001071 }
1072 if (indices && indexCount > 0) {
1073 flags |= kDrawVertices_HasIndices_DrawOpFlag;
reed@google.comacd471f2011-05-03 21:26:46 +00001074 size += 4 + SkAlign4(indexCount * sizeof(uint16_t));
reed@google.combb6992a2011-04-26 17:41:56 +00001075 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001076
reed@google.comacd471f2011-05-03 21:26:46 +00001077 if (this->needOpBytes(size)) {
reed@google.com31891582011-05-12 03:03:56 +00001078 this->writeOp(kDrawVertices_DrawOp, flags, 0);
reed@google.comacd471f2011-05-03 21:26:46 +00001079 fWriter.write32(mode);
1080 fWriter.write32(vertexCount);
1081 fWriter.write(vertices, vertexCount * sizeof(SkPoint));
1082 if (texs) {
1083 fWriter.write(texs, vertexCount * sizeof(SkPoint));
1084 }
1085 if (colors) {
1086 fWriter.write(colors, vertexCount * sizeof(SkColor));
1087 }
reed@google.combb6992a2011-04-26 17:41:56 +00001088
reed@google.comacd471f2011-05-03 21:26:46 +00001089 // TODO: flatten xfermode
reed@google.combb6992a2011-04-26 17:41:56 +00001090
reed@google.comacd471f2011-05-03 21:26:46 +00001091 if (indices && indexCount > 0) {
1092 fWriter.write32(indexCount);
1093 fWriter.writePad(indices, indexCount * sizeof(uint16_t));
1094 }
reed@google.combb6992a2011-04-26 17:41:56 +00001095 }
1096}
1097
reed@google.comacd471f2011-05-03 21:26:46 +00001098void SkGPipeCanvas::drawData(const void* ptr, size_t size) {
1099 if (size && ptr) {
1100 NOTIFY_SETUP(this);
reed@google.combb6992a2011-04-26 17:41:56 +00001101 unsigned data = 0;
1102 if (size < (1 << DRAWOPS_DATA_BITS)) {
1103 data = (unsigned)size;
1104 }
reed@google.comacd471f2011-05-03 21:26:46 +00001105 if (this->needOpBytes(4 + SkAlign4(size))) {
1106 this->writeOp(kDrawData_DrawOp, 0, data);
1107 if (0 == data) {
1108 fWriter.write32(size);
1109 }
reed@google.combb6793b2011-05-05 15:18:15 +00001110 fWriter.writePad(ptr, size);
reed@google.combb6992a2011-04-26 17:41:56 +00001111 }
1112 }
1113}
1114
junov@chromium.org77eec242012-07-18 17:54:45 +00001115void SkGPipeCanvas::flushRecording(bool detachCurrentBlock) {
1116 doNotify();
1117 if (detachCurrentBlock) {
1118 // force a new block to be requested for the next recorded command
1119 fBlockSize = 0;
1120 }
1121}
1122
reed@google.combb6992a2011-04-26 17:41:56 +00001123///////////////////////////////////////////////////////////////////////////////
1124
1125template <typename T> uint32_t castToU32(T value) {
1126 union {
1127 T fSrc;
1128 uint32_t fDst;
1129 } data;
1130 data.fSrc = value;
1131 return data.fDst;
1132}
1133
reed@google.com31891582011-05-12 03:03:56 +00001134void SkGPipeCanvas::writePaint(const SkPaint& paint) {
1135 SkPaint& base = fPaint;
reed@google.combb6992a2011-04-26 17:41:56 +00001136 uint32_t storage[32];
1137 uint32_t* ptr = storage;
reed@google.combb6992a2011-04-26 17:41:56 +00001138
1139 if (base.getFlags() != paint.getFlags()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001140 *ptr++ = PaintOp_packOpData(kFlags_PaintOp, paint.getFlags());
reed@google.comf5842f72011-05-04 18:30:04 +00001141 base.setFlags(paint.getFlags());
reed@google.combb6992a2011-04-26 17:41:56 +00001142 }
1143 if (base.getColor() != paint.getColor()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001144 *ptr++ = PaintOp_packOp(kColor_PaintOp);
1145 *ptr++ = paint.getColor();
reed@google.comf5842f72011-05-04 18:30:04 +00001146 base.setColor(paint.getColor());
reed@google.combb6992a2011-04-26 17:41:56 +00001147 }
1148 if (base.getStyle() != paint.getStyle()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001149 *ptr++ = PaintOp_packOpData(kStyle_PaintOp, paint.getStyle());
reed@google.comf5842f72011-05-04 18:30:04 +00001150 base.setStyle(paint.getStyle());
reed@google.combb6992a2011-04-26 17:41:56 +00001151 }
1152 if (base.getStrokeJoin() != paint.getStrokeJoin()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001153 *ptr++ = PaintOp_packOpData(kJoin_PaintOp, paint.getStrokeJoin());
reed@google.comf5842f72011-05-04 18:30:04 +00001154 base.setStrokeJoin(paint.getStrokeJoin());
reed@google.combb6992a2011-04-26 17:41:56 +00001155 }
1156 if (base.getStrokeCap() != paint.getStrokeCap()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001157 *ptr++ = PaintOp_packOpData(kCap_PaintOp, paint.getStrokeCap());
reed@google.comf5842f72011-05-04 18:30:04 +00001158 base.setStrokeCap(paint.getStrokeCap());
reed@google.combb6992a2011-04-26 17:41:56 +00001159 }
1160 if (base.getStrokeWidth() != paint.getStrokeWidth()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001161 *ptr++ = PaintOp_packOp(kWidth_PaintOp);
1162 *ptr++ = castToU32(paint.getStrokeWidth());
reed@google.comf5842f72011-05-04 18:30:04 +00001163 base.setStrokeWidth(paint.getStrokeWidth());
reed@google.combb6992a2011-04-26 17:41:56 +00001164 }
1165 if (base.getStrokeMiter() != paint.getStrokeMiter()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001166 *ptr++ = PaintOp_packOp(kMiter_PaintOp);
1167 *ptr++ = castToU32(paint.getStrokeMiter());
reed@google.comf5842f72011-05-04 18:30:04 +00001168 base.setStrokeMiter(paint.getStrokeMiter());
reed@google.combb6992a2011-04-26 17:41:56 +00001169 }
1170 if (base.getTextEncoding() != paint.getTextEncoding()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001171 *ptr++ = PaintOp_packOpData(kEncoding_PaintOp, paint.getTextEncoding());
reed@google.comf5842f72011-05-04 18:30:04 +00001172 base.setTextEncoding(paint.getTextEncoding());
reed@google.combb6992a2011-04-26 17:41:56 +00001173 }
1174 if (base.getHinting() != paint.getHinting()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001175 *ptr++ = PaintOp_packOpData(kHinting_PaintOp, paint.getHinting());
reed@google.comf5842f72011-05-04 18:30:04 +00001176 base.setHinting(paint.getHinting());
reed@google.combb6992a2011-04-26 17:41:56 +00001177 }
1178 if (base.getTextAlign() != paint.getTextAlign()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001179 *ptr++ = PaintOp_packOpData(kAlign_PaintOp, paint.getTextAlign());
reed@google.comf5842f72011-05-04 18:30:04 +00001180 base.setTextAlign(paint.getTextAlign());
reed@google.combb6992a2011-04-26 17:41:56 +00001181 }
1182 if (base.getTextSize() != paint.getTextSize()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001183 *ptr++ = PaintOp_packOp(kTextSize_PaintOp);
1184 *ptr++ = castToU32(paint.getTextSize());
reed@google.comf5842f72011-05-04 18:30:04 +00001185 base.setTextSize(paint.getTextSize());
reed@google.combb6992a2011-04-26 17:41:56 +00001186 }
1187 if (base.getTextScaleX() != paint.getTextScaleX()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001188 *ptr++ = PaintOp_packOp(kTextScaleX_PaintOp);
1189 *ptr++ = castToU32(paint.getTextScaleX());
reed@google.comf5842f72011-05-04 18:30:04 +00001190 base.setTextScaleX(paint.getTextScaleX());
reed@google.combb6992a2011-04-26 17:41:56 +00001191 }
1192 if (base.getTextSkewX() != paint.getTextSkewX()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001193 *ptr++ = PaintOp_packOp(kTextSkewX_PaintOp);
1194 *ptr++ = castToU32(paint.getTextSkewX());
reed@google.comf5842f72011-05-04 18:30:04 +00001195 base.setTextSkewX(paint.getTextSkewX());
1196 }
1197
1198 if (!SkTypeface::Equal(base.getTypeface(), paint.getTypeface())) {
scroggo@google.com3cb969f2012-07-27 20:39:19 +00001199 if (SkToBool(fFlags & SkGPipeWriter::kCrossProcess_Flag)) {
1200 uint32_t id = this->getTypefaceID(paint.getTypeface());
1201 *ptr++ = PaintOp_packOpData(kTypeface_PaintOp, id);
1202 } else if (this->needOpBytes(sizeof(void*))) {
1203 // Add to the set for ref counting.
1204 fTypefaceSet.add(paint.getTypeface());
1205 // It is safe to write the typeface to the stream before the rest
1206 // of the paint unless we ever send a kReset_PaintOp, which we
1207 // currently never do.
1208 this->writeOp(kSetTypeface_DrawOp);
1209 fWriter.writePtr(paint.getTypeface());
1210 }
reed@google.comf5842f72011-05-04 18:30:04 +00001211 base.setTypeface(paint.getTypeface());
reed@google.combb6992a2011-04-26 17:41:56 +00001212 }
reed@google.combb6992a2011-04-26 17:41:56 +00001213
scroggo@google.com4dffc592012-07-17 16:49:40 +00001214 // This is a new paint, so all old flats can be safely purged, if necessary.
1215 fFlattenableHeap.markAllFlatsSafeToDelete();
reed@google.comb55d1182011-05-11 00:42:04 +00001216 for (int i = 0; i < kCount_PaintFlats; i++) {
1217 int index = this->flattenToIndex(get_paintflat(paint, i), (PaintFlats)i);
scroggo@google.comd3ba5cc2012-07-09 16:05:53 +00001218 bool replaced = index < 0;
1219 if (replaced) {
1220 index = ~index;
1221 }
scroggo@google.com4dffc592012-07-17 16:49:40 +00001222 // Store the index of any flat that needs to be kept. 0 means no flat.
1223 if (index > 0) {
1224 fFlattenableHeap.markFlatForKeeping(index);
1225 }
1226 SkASSERT(index >= 0 && index <= fFlatDictionary.count());
scroggo@google.comd3ba5cc2012-07-09 16:05:53 +00001227 if (index != fCurrFlatIndex[i] || replaced) {
reed@google.comb55d1182011-05-11 00:42:04 +00001228 *ptr++ = PaintOp_packOpFlagData(kFlatIndex_PaintOp, i, index);
1229 fCurrFlatIndex[i] = index;
1230 }
1231 }
1232
reed@google.comacd471f2011-05-03 21:26:46 +00001233 size_t size = (char*)ptr - (char*)storage;
1234 if (size && this->needOpBytes(size)) {
reed@google.comb55d1182011-05-11 00:42:04 +00001235 this->writeOp(kPaintOp_DrawOp, 0, size);
reed@google.comb55d1182011-05-11 00:42:04 +00001236 fWriter.write(storage, size);
reed@google.combb6992a2011-04-26 17:41:56 +00001237 for (size_t i = 0; i < size/4; i++) {
reed@google.comb55d1182011-05-11 00:42:04 +00001238// SkDebugf("[%d] %08X\n", i, storage[i]);
reed@google.combb6992a2011-04-26 17:41:56 +00001239 }
1240 }
reed@google.combb6992a2011-04-26 17:41:56 +00001241}
1242
1243///////////////////////////////////////////////////////////////////////////////
1244
1245#include "SkGPipe.h"
1246
scroggo@google.com3cb969f2012-07-27 20:39:19 +00001247SkGPipeController::~SkGPipeController() {
1248 SkSafeUnref(fCanvas);
1249}
1250
1251void SkGPipeController::setCanvas(SkGPipeCanvas* canvas) {
1252 SkRefCnt_SafeAssign(fCanvas, canvas);
1253}
1254
1255///////////////////////////////////////////////////////////////////////////////
1256
1257SkGPipeWriter::SkGPipeWriter()
1258: fFactorySet(SkNEW(SkFactorySet))
1259, fWriter(0) {
reed@google.combb6992a2011-04-26 17:41:56 +00001260 fCanvas = NULL;
1261}
1262
1263SkGPipeWriter::~SkGPipeWriter() {
reed@google.comacd471f2011-05-03 21:26:46 +00001264 this->endRecording();
scroggo@google.com3cb969f2012-07-27 20:39:19 +00001265 fFactorySet->unref();
reed@google.combb6992a2011-04-26 17:41:56 +00001266}
1267
scroggo@google.com565254b2012-06-28 15:41:32 +00001268SkCanvas* SkGPipeWriter::startRecording(SkGPipeController* controller, uint32_t flags) {
reed@google.combb6992a2011-04-26 17:41:56 +00001269 if (NULL == fCanvas) {
reed@google.comacd471f2011-05-03 21:26:46 +00001270 fWriter.reset(NULL, 0);
scroggo@google.com3cb969f2012-07-27 20:39:19 +00001271 fFactorySet->reset();
reed@google.comdde09562011-05-23 12:21:05 +00001272 fCanvas = SkNEW_ARGS(SkGPipeCanvas, (controller, &fWriter,
1273 (flags & kCrossProcess_Flag) ?
scroggo@google.com3cb969f2012-07-27 20:39:19 +00001274 fFactorySet : NULL, flags));
reed@google.combb6992a2011-04-26 17:41:56 +00001275 }
scroggo@google.com3cb969f2012-07-27 20:39:19 +00001276 controller->setCanvas(fCanvas);
reed@google.combb6992a2011-04-26 17:41:56 +00001277 return fCanvas;
1278}
1279
1280void SkGPipeWriter::endRecording() {
1281 if (fCanvas) {
1282 fCanvas->finish();
1283 fCanvas->unref();
1284 fCanvas = NULL;
1285 }
1286}
1287
junov@chromium.org77eec242012-07-18 17:54:45 +00001288void SkGPipeWriter::flushRecording(bool detachCurrentBlock){
1289 fCanvas->flushRecording(detachCurrentBlock);
1290}
1291
scroggo@google.com15011ee2012-07-26 20:03:32 +00001292size_t SkGPipeWriter::storageAllocatedForRecording() {
1293 return NULL == fCanvas ? 0 : fCanvas->storageAllocatedForRecording();
1294}
1295