blob: e020beae74120d71b5ec7f046ab352ef1beefa8a [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
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +000031static bool isCrossProcess(uint32_t flags) {
32 return SkToBool(flags & SkGPipeWriter::kCrossProcess_Flag);
33}
34
reed@google.comb55d1182011-05-11 00:42:04 +000035static SkFlattenable* get_paintflat(const SkPaint& paint, unsigned paintFlat) {
36 SkASSERT(paintFlat < kCount_PaintFlats);
37 switch (paintFlat) {
38 case kColorFilter_PaintFlat: return paint.getColorFilter();
reed@google.com0faac1e2011-05-11 05:58:58 +000039 case kDrawLooper_PaintFlat: return paint.getLooper();
reed@google.comb55d1182011-05-11 00:42:04 +000040 case kMaskFilter_PaintFlat: return paint.getMaskFilter();
41 case kPathEffect_PaintFlat: return paint.getPathEffect();
42 case kRasterizer_PaintFlat: return paint.getRasterizer();
43 case kShader_PaintFlat: return paint.getShader();
scroggo@google.com16d1d0b2012-05-02 19:09:40 +000044 case kImageFilter_PaintFlat: return paint.getImageFilter();
reed@google.comb55d1182011-05-11 00:42:04 +000045 case kXfermode_PaintFlat: return paint.getXfermode();
46 }
tomhudson@google.com0c00f212011-12-28 14:59:50 +000047 SkDEBUGFAIL("never gets here");
reed@google.comb55d1182011-05-11 00:42:04 +000048 return NULL;
49}
reed@google.combb6992a2011-04-26 17:41:56 +000050
reed@google.comf5842f72011-05-04 18:30:04 +000051static size_t writeTypeface(SkWriter32* writer, SkTypeface* typeface) {
52 SkASSERT(typeface);
53 SkDynamicMemoryWStream stream;
54 typeface->serialize(&stream);
55 size_t size = stream.getOffset();
56 if (writer) {
57 writer->write32(size);
reed@google.com8a85d0c2011-06-24 19:12:12 +000058 SkAutoDataUnref data(stream.copyToData());
robertphillips@google.com59f46b82012-07-10 17:30:58 +000059 writer->writePad(data->data(), size);
reed@google.comf5842f72011-05-04 18:30:04 +000060 }
scroggo@google.com5af9b202012-06-04 17:17:36 +000061 return 4 + SkAlign4(size);
reed@google.comf5842f72011-05-04 18:30:04 +000062}
63
reed@google.combb6992a2011-04-26 17:41:56 +000064///////////////////////////////////////////////////////////////////////////////
65
scroggo@google.com4dffc592012-07-17 16:49:40 +000066class FlattenableHeap : public SkFlatController {
67public:
scroggo@google.com15543602012-08-02 18:49:49 +000068 FlattenableHeap(int numFlatsToKeep, SkNamedFactorySet* fset)
69 : fNumFlatsToKeep(numFlatsToKeep) {
70 this->setNamedFactorySet(fset);
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +000071 }
scroggo@google.com4dffc592012-07-17 16:49:40 +000072
73 ~FlattenableHeap() {
74 fPointers.freeAll();
75 }
76
77 virtual void* allocThrow(size_t bytes) SK_OVERRIDE;
78
79 virtual void unalloc(void* ptr) SK_OVERRIDE;
80
81 const SkFlatData* flatToReplace() const;
82
83 // Mark an SkFlatData as one that should not be returned by flatToReplace.
84 // Takes the result of SkFlatData::index() as its parameter.
85 void markFlatForKeeping(int index) {
86 *fFlatsThatMustBeKept.append() = index;
87 }
88
89 void markAllFlatsSafeToDelete() {
90 fFlatsThatMustBeKept.reset();
91 }
92
93private:
94 // Keep track of the indices (i.e. the result of SkFlatData::index()) of
95 // flats that must be kept, since they are on the current paint.
96 SkTDArray<int> fFlatsThatMustBeKept;
97 SkTDArray<void*> fPointers;
98 const int fNumFlatsToKeep;
99};
100
101void FlattenableHeap::unalloc(void* ptr) {
102 int indexToRemove = fPointers.rfind(ptr);
103 if (indexToRemove >= 0) {
104 sk_free(ptr);
105 fPointers.remove(indexToRemove);
106 }
107}
108
109void* FlattenableHeap::allocThrow(size_t bytes) {
110 void* ptr = sk_malloc_throw(bytes);
111 *fPointers.append() = ptr;
112 return ptr;
113}
114
115const SkFlatData* FlattenableHeap::flatToReplace() const {
116 // First, determine whether we should replace one.
117 if (fPointers.count() > fNumFlatsToKeep) {
118 // Look through the flattenable heap.
119 // TODO: Return the LRU flat.
120 for (int i = 0; i < fPointers.count(); i++) {
121 SkFlatData* potential = (SkFlatData*)fPointers[i];
122 // Make sure that it is not one that must be kept.
123 bool mustKeep = false;
124 for (int j = 0; j < fFlatsThatMustBeKept.count(); j++) {
125 if (potential->index() == fFlatsThatMustBeKept[j]) {
126 mustKeep = true;
127 break;
128 }
129 }
130 if (!mustKeep) {
131 return potential;
132 }
133 }
134 }
135 return NULL;
136}
137
138///////////////////////////////////////////////////////////////////////////////
139
140class FlatDictionary : public SkFlatDictionary<SkFlattenable> {
141public:
scroggo@google.com15543602012-08-02 18:49:49 +0000142 FlatDictionary(FlattenableHeap* heap)
143 : SkFlatDictionary<SkFlattenable>(heap) {
scroggo@google.com4dffc592012-07-17 16:49:40 +0000144 fFlattenProc = &flattenFlattenableProc;
145 // No need to define fUnflattenProc since the writer will never
146 // unflatten the data.
147 }
148 static void flattenFlattenableProc(SkOrderedWriteBuffer& buffer,
149 const void* obj) {
150 buffer.writeFlattenable((SkFlattenable*)obj);
151 }
152
153};
154
155///////////////////////////////////////////////////////////////////////////////
156
scroggo@google.com8ae3c7f2012-06-13 17:47:52 +0000157/*
158 * Shared heap for storing large things that can be shared, for a stream
159 * used by multiple readers.
160 * TODO: Make the allocations all come from cross process safe address space
161 * TODO: Store paths (others?)
scroggo@google.com284bf502012-07-17 16:10:34 +0000162 * TODO: Generalize the LRU caching mechanism
scroggo@google.com8ae3c7f2012-06-13 17:47:52 +0000163 */
scroggo@google.com4dffc592012-07-17 16:49:40 +0000164class SharedHeap {
scroggo@google.com8ae3c7f2012-06-13 17:47:52 +0000165public:
scroggo@google.com4dffc592012-07-17 16:49:40 +0000166 SharedHeap(bool shallow, int numOfReaders)
scroggo@google.com284bf502012-07-17 16:10:34 +0000167 : fBitmapCount(0)
168 , fMostRecentlyUsed(NULL)
169 , fLeastRecentlyUsed(NULL)
170 , fCanDoShallowCopies(shallow)
scroggo@google.com15011ee2012-07-26 20:03:32 +0000171 , fNumberOfReaders(numOfReaders)
172 , fBytesAllocated(0) {}
scroggo@google.com4dffc592012-07-17 16:49:40 +0000173 ~SharedHeap() {
scroggo@google.com284bf502012-07-17 16:10:34 +0000174 BitmapInfo* iter = fMostRecentlyUsed;
175 while (iter != NULL) {
scroggo@google.com15011ee2012-07-26 20:03:32 +0000176 SkDEBUGCODE(fBytesAllocated -= (iter->fBytesAllocated + sizeof(BitmapInfo)));
scroggo@google.com284bf502012-07-17 16:10:34 +0000177 BitmapInfo* next = iter->fLessRecentlyUsed;
178 SkDELETE(iter);
179 fBitmapCount--;
180 iter = next;
scroggo@google.com8ae3c7f2012-06-13 17:47:52 +0000181 }
scroggo@google.com284bf502012-07-17 16:10:34 +0000182 SkASSERT(0 == fBitmapCount);
scroggo@google.com15011ee2012-07-26 20:03:32 +0000183 SkASSERT(0 == fBytesAllocated);
scroggo@google.com8ae3c7f2012-06-13 17:47:52 +0000184 }
185
186 /*
scroggo@google.com15011ee2012-07-26 20:03:32 +0000187 * Get the approximate number of bytes allocated.
188 *
189 * Not exact. Some SkBitmaps may share SkPixelRefs, in which case only one
190 * SkBitmap will take the size of the SkPixelRef into account (the first
191 * one). It is possible that the one which accounts for the SkPixelRef has
192 * been removed, in which case we will no longer be counting those bytes.
193 */
194 size_t bytesAllocated() { return fBytesAllocated; }
195
196 /*
scroggo@google.com8ae3c7f2012-06-13 17:47:52 +0000197 * Add a copy of a bitmap to the heap.
198 * @param bm The SkBitmap to be copied and placed in the heap.
scroggo@google.com284bf502012-07-17 16:10:34 +0000199 * @return void* Pointer to the BitmapInfo stored in the heap, which
200 * contains a copy of the SkBitmap. If NULL,
scroggo@google.com8ae3c7f2012-06-13 17:47:52 +0000201 * the bitmap could not be copied.
202 */
scroggo@google.com284bf502012-07-17 16:10:34 +0000203 const void* addBitmap(const SkBitmap& orig) {
scroggo@google.com4f1f6bf2012-07-02 13:35:09 +0000204 const uint32_t genID = orig.getGenerationID();
205 SkPixelRef* sharedPixelRef = NULL;
scroggo@google.com284bf502012-07-17 16:10:34 +0000206 // When looking to see if we've previously used this bitmap, start at
207 // the end, assuming that the caller is more likely to reuse a recent
208 // one.
209 BitmapInfo* iter = fMostRecentlyUsed;
210 while (iter != NULL) {
211 if (genID == iter->fGenID) {
212 SkBitmap* storedBitmap = iter->fBitmap;
scroggo@google.com15011ee2012-07-26 20:03:32 +0000213 // TODO: Perhaps we can share code with
214 // SkPictureRecord::PixelRefDictionaryEntry/
215 // BitmapIndexCacheEntry so we can do a binary search for a
216 // matching bitmap
scroggo@google.com6ea165d2012-07-03 14:52:08 +0000217 if (orig.pixelRefOffset() != storedBitmap->pixelRefOffset()
218 || orig.width() != storedBitmap->width()
219 || orig.height() != storedBitmap->height()) {
scroggo@google.com4f1f6bf2012-07-02 13:35:09 +0000220 // In this case, the bitmaps share a pixelRef, but have
scroggo@google.com6ea165d2012-07-03 14:52:08 +0000221 // different offsets or sizes. Keep track of the other
222 // bitmap so that instead of making another copy of the
223 // pixelRef we can use the copy we already made.
224 sharedPixelRef = storedBitmap->pixelRef();
scroggo@google.com4f1f6bf2012-07-02 13:35:09 +0000225 break;
226 }
scroggo@google.com284bf502012-07-17 16:10:34 +0000227 iter->addDraws(fNumberOfReaders);
228 this->setMostRecentlyUsed(iter);
229 return iter;
scroggo@google.com8ae3c7f2012-06-13 17:47:52 +0000230 }
scroggo@google.com284bf502012-07-17 16:10:34 +0000231 iter = iter->fLessRecentlyUsed;
scroggo@google.com8ae3c7f2012-06-13 17:47:52 +0000232 }
scroggo@google.com284bf502012-07-17 16:10:34 +0000233 SkAutoRef ar((SkRefCnt*)sharedPixelRef);
234 BitmapInfo* replace = this->bitmapToReplace(orig);
scroggo@google.com565254b2012-06-28 15:41:32 +0000235 SkBitmap* copy;
236 // If the bitmap is mutable, we still need to do a deep copy, since the
237 // caller may modify it afterwards. That said, if the bitmap is mutable,
238 // but has no pixelRef, the copy constructor actually does a deep copy.
scroggo@google.com4f1f6bf2012-07-02 13:35:09 +0000239 if (fCanDoShallowCopies && (orig.isImmutable() || !orig.pixelRef())) {
scroggo@google.com284bf502012-07-17 16:10:34 +0000240 if (NULL == replace) {
241 copy = SkNEW_ARGS(SkBitmap, (orig));
242 } else {
243 *replace->fBitmap = orig;
244 }
scroggo@google.com565254b2012-06-28 15:41:32 +0000245 } else {
scroggo@google.com4f1f6bf2012-07-02 13:35:09 +0000246 if (sharedPixelRef != NULL) {
scroggo@google.com284bf502012-07-17 16:10:34 +0000247 if (NULL == replace) {
248 // Do a shallow copy of the bitmap to get the width, height, etc
249 copy = SkNEW_ARGS(SkBitmap, (orig));
250 // Replace the pixelRef with the copy that was already made, and
251 // use the appropriate offset.
252 copy->setPixelRef(sharedPixelRef, orig.pixelRefOffset());
253 } else {
254 *replace->fBitmap = orig;
255 replace->fBitmap->setPixelRef(sharedPixelRef, orig.pixelRefOffset());
256 }
scroggo@google.com4f1f6bf2012-07-02 13:35:09 +0000257 } else {
scroggo@google.com284bf502012-07-17 16:10:34 +0000258 if (NULL == replace) {
259 copy = SkNEW(SkBitmap);
260 if (!orig.copyTo(copy, orig.getConfig())) {
261 delete copy;
262 return NULL;
263 }
264 } else {
265 if (!orig.copyTo(replace->fBitmap, orig.getConfig())) {
266 return NULL;
267 }
scroggo@google.com4f1f6bf2012-07-02 13:35:09 +0000268 }
scroggo@google.com565254b2012-06-28 15:41:32 +0000269 }
scroggo@google.com8ae3c7f2012-06-13 17:47:52 +0000270 }
scroggo@google.com284bf502012-07-17 16:10:34 +0000271 BitmapInfo* info;
272 if (NULL == replace) {
scroggo@google.com15011ee2012-07-26 20:03:32 +0000273 fBytesAllocated += sizeof(BitmapInfo);
scroggo@google.com284bf502012-07-17 16:10:34 +0000274 info = SkNEW_ARGS(BitmapInfo, (copy, genID, fNumberOfReaders));
275 fBitmapCount++;
276 } else {
scroggo@google.com15011ee2012-07-26 20:03:32 +0000277 fBytesAllocated -= replace->fBytesAllocated;
scroggo@google.com284bf502012-07-17 16:10:34 +0000278 replace->fGenID = genID;
279 replace->addDraws(fNumberOfReaders);
280 info = replace;
281 }
scroggo@google.com15011ee2012-07-26 20:03:32 +0000282 // Always include the size of the SkBitmap struct.
283 info->fBytesAllocated = sizeof(SkBitmap);
284 // If the SkBitmap does not share an SkPixelRef with an SkBitmap already
285 // in the SharedHeap, also include the size of its pixels.
286 if (NULL == sharedPixelRef) {
287 info->fBytesAllocated += orig.getSize();
288 }
289 fBytesAllocated += info->fBytesAllocated;
scroggo@google.com284bf502012-07-17 16:10:34 +0000290 this->setMostRecentlyUsed(info);
291 return info;
scroggo@google.com8ae3c7f2012-06-13 17:47:52 +0000292 }
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000293
294 size_t freeMemoryIfPossible(size_t bytesToFree) {
295 BitmapInfo* info = fLeastRecentlyUsed;
296 size_t origBytesAllocated = fBytesAllocated;
297 // Purge starting from LRU until a non-evictable bitmap is found
298 // or until everything is evicted.
299 while (info && info->drawCount() == 0) {
300 fBytesAllocated -= (info->fBytesAllocated + sizeof(BitmapInfo));
301 fBitmapCount--;
302 BitmapInfo* nextInfo = info->fMoreRecentlyUsed;
303 SkDELETE(info);
304 info = nextInfo;
305 if ((origBytesAllocated - fBytesAllocated) >= bytesToFree) {
306 break;
307 }
308 }
309
310 if (fLeastRecentlyUsed != info) { // at least one eviction
311 fLeastRecentlyUsed = info;
312 if (NULL != fLeastRecentlyUsed) {
313 fLeastRecentlyUsed->fLessRecentlyUsed = NULL;
314 } else {
315 // everything was evicted
316 fMostRecentlyUsed = NULL;
317 SkASSERT(0 == fBytesAllocated);
318 SkASSERT(0 == fBitmapCount);
319 }
320 }
321
322 return origBytesAllocated - fBytesAllocated;
323 }
324
scroggo@google.com8ae3c7f2012-06-13 17:47:52 +0000325private:
scroggo@google.com284bf502012-07-17 16:10:34 +0000326 void setMostRecentlyUsed(BitmapInfo* info);
327 BitmapInfo* bitmapToReplace(const SkBitmap& bm) const;
328
329 int fBitmapCount;
330 BitmapInfo* fLeastRecentlyUsed;
331 BitmapInfo* fMostRecentlyUsed;
332 const bool fCanDoShallowCopies;
333 const int fNumberOfReaders;
scroggo@google.com15011ee2012-07-26 20:03:32 +0000334 size_t fBytesAllocated;
scroggo@google.com8ae3c7f2012-06-13 17:47:52 +0000335};
336
scroggo@google.com284bf502012-07-17 16:10:34 +0000337// We just "used" info. Update our LRU accordingly
scroggo@google.com4dffc592012-07-17 16:49:40 +0000338void SharedHeap::setMostRecentlyUsed(BitmapInfo* info) {
scroggo@google.com284bf502012-07-17 16:10:34 +0000339 SkASSERT(info != NULL);
340 if (info == fMostRecentlyUsed) {
341 return;
342 }
343 // Remove info from its prior place, and make sure to cover the hole.
344 if (fLeastRecentlyUsed == info) {
345 SkASSERT(info->fMoreRecentlyUsed != NULL);
346 fLeastRecentlyUsed = info->fMoreRecentlyUsed;
347 }
348 if (info->fMoreRecentlyUsed != NULL) {
349 SkASSERT(fMostRecentlyUsed != info);
350 info->fMoreRecentlyUsed->fLessRecentlyUsed = info->fLessRecentlyUsed;
351 }
352 if (info->fLessRecentlyUsed != NULL) {
353 SkASSERT(fLeastRecentlyUsed != info);
354 info->fLessRecentlyUsed->fMoreRecentlyUsed = info->fMoreRecentlyUsed;
355 }
356 info->fMoreRecentlyUsed = NULL;
357 // Set up the head and tail pointers properly.
358 if (fMostRecentlyUsed != NULL) {
359 SkASSERT(NULL == fMostRecentlyUsed->fMoreRecentlyUsed);
360 fMostRecentlyUsed->fMoreRecentlyUsed = info;
361 info->fLessRecentlyUsed = fMostRecentlyUsed;
362 }
363 fMostRecentlyUsed = info;
364 if (NULL == fLeastRecentlyUsed) {
365 fLeastRecentlyUsed = info;
366 }
367}
368
369/**
370 * Given a new bitmap to be added to the cache, return an existing one that
371 * should be removed to make room, or NULL if there is already room.
372 */
scroggo@google.com4dffc592012-07-17 16:49:40 +0000373BitmapInfo* SharedHeap::bitmapToReplace(const SkBitmap& bm) const {
scroggo@google.com284bf502012-07-17 16:10:34 +0000374 // Arbitrarily set a limit of 5. We should test to find the best tradeoff
375 // between time and space. A lower limit means that we use less space, but
376 // it also means that we may have to insert the same bitmap into the heap
377 // multiple times (depending on the input), potentially taking more time.
378 // On the other hand, a lower limit also means searching through our stored
379 // bitmaps takes less time.
380 if (fBitmapCount > 5) {
381 BitmapInfo* iter = fLeastRecentlyUsed;
382 while (iter != NULL) {
383 if (iter->drawCount() > 0) {
384 // If the least recently used bitmap has not been drawn by some
385 // reader, then a more recently used one will not have been
386 // drawn yet either.
387 return NULL;
388 }
389 if (bm.pixelRef() != NULL
390 && bm.pixelRef() == iter->fBitmap->pixelRef()) {
391 // Do not replace a bitmap with a new one using the same
392 // pixel ref. Instead look for a different one that will
393 // potentially free up more space.
394 iter = iter->fMoreRecentlyUsed;
395 } else {
396 return iter;
397 }
398 }
399 }
400 return NULL;
401}
402
scroggo@google.com8ae3c7f2012-06-13 17:47:52 +0000403///////////////////////////////////////////////////////////////////////////////
404
reed@google.combb6992a2011-04-26 17:41:56 +0000405class SkGPipeCanvas : public SkCanvas {
406public:
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000407 SkGPipeCanvas(SkGPipeController*, SkWriter32*, uint32_t flags);
reed@google.combb6992a2011-04-26 17:41:56 +0000408 virtual ~SkGPipeCanvas();
409
410 void finish() {
411 if (!fDone) {
reed@google.comdbccc882011-07-08 18:53:39 +0000412 if (this->needOpBytes()) {
413 this->writeOp(kDone_DrawOp);
414 this->doNotify();
415 }
reed@google.combb6992a2011-04-26 17:41:56 +0000416 fDone = true;
417 }
418 }
419
junov@chromium.org77eec242012-07-18 17:54:45 +0000420 void flushRecording(bool detachCurrentBlock);
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000421 size_t freeMemoryIfPossible(size_t bytesToFree);
junov@chromium.org77eec242012-07-18 17:54:45 +0000422
scroggo@google.com15011ee2012-07-26 20:03:32 +0000423 size_t storageAllocatedForRecording() {
424 return fSharedHeap.bytesAllocated();
425 }
426
reed@google.combb6992a2011-04-26 17:41:56 +0000427 // overrides from SkCanvas
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000428 virtual int save(SaveFlags) SK_OVERRIDE;
429 virtual int saveLayer(const SkRect* bounds, const SkPaint*,
430 SaveFlags) SK_OVERRIDE;
431 virtual void restore() SK_OVERRIDE;
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000432 virtual bool isDrawingToLayer() const SK_OVERRIDE;
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000433 virtual bool translate(SkScalar dx, SkScalar dy) SK_OVERRIDE;
434 virtual bool scale(SkScalar sx, SkScalar sy) SK_OVERRIDE;
435 virtual bool rotate(SkScalar degrees) SK_OVERRIDE;
436 virtual bool skew(SkScalar sx, SkScalar sy) SK_OVERRIDE;
437 virtual bool concat(const SkMatrix& matrix) SK_OVERRIDE;
438 virtual void setMatrix(const SkMatrix& matrix) SK_OVERRIDE;
439 virtual bool clipRect(const SkRect& rect, SkRegion::Op op,
440 bool doAntiAlias = false) SK_OVERRIDE;
441 virtual bool clipPath(const SkPath& path, SkRegion::Op op,
442 bool doAntiAlias = false) SK_OVERRIDE;
443 virtual bool clipRegion(const SkRegion& region, SkRegion::Op op) SK_OVERRIDE;
444 virtual void clear(SkColor) SK_OVERRIDE;
445 virtual void drawPaint(const SkPaint& paint) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000446 virtual void drawPoints(PointMode, size_t count, const SkPoint pts[],
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000447 const SkPaint&) SK_OVERRIDE;
448 virtual void drawRect(const SkRect& rect, const SkPaint&) SK_OVERRIDE;
449 virtual void drawPath(const SkPath& path, const SkPaint&) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000450 virtual void drawBitmap(const SkBitmap&, SkScalar left, SkScalar top,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000451 const SkPaint*) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000452 virtual void drawBitmapRect(const SkBitmap&, const SkIRect* src,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000453 const SkRect& dst, const SkPaint*) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000454 virtual void drawBitmapMatrix(const SkBitmap&, const SkMatrix&,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000455 const SkPaint*) SK_OVERRIDE;
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000456 virtual void drawBitmapNine(const SkBitmap& bitmap, const SkIRect& center,
457 const SkRect& dst, const SkPaint* paint = NULL) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000458 virtual void drawSprite(const SkBitmap&, int left, int top,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000459 const SkPaint*) SK_OVERRIDE;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000460 virtual void drawText(const void* text, size_t byteLength, SkScalar x,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000461 SkScalar y, const SkPaint&) SK_OVERRIDE;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000462 virtual void drawPosText(const void* text, size_t byteLength,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000463 const SkPoint pos[], const SkPaint&) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000464 virtual void drawPosTextH(const void* text, size_t byteLength,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000465 const SkScalar xpos[], SkScalar constY,
466 const SkPaint&) SK_OVERRIDE;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000467 virtual void drawTextOnPath(const void* text, size_t byteLength,
468 const SkPath& path, const SkMatrix* matrix,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000469 const SkPaint&) SK_OVERRIDE;
470 virtual void drawPicture(SkPicture& picture) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000471 virtual void drawVertices(VertexMode, int vertexCount,
472 const SkPoint vertices[], const SkPoint texs[],
473 const SkColor colors[], SkXfermode*,
474 const uint16_t indices[], int indexCount,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000475 const SkPaint&) SK_OVERRIDE;
476 virtual void drawData(const void*, size_t) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000477
478private:
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000479 enum {
480 kNoSaveLayer = -1,
481 };
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000482 SkNamedFactorySet* fFactorySet;
483 int fFirstSaveLayerStackLevel;
484 SharedHeap fSharedHeap;
reed@google.comacd471f2011-05-03 21:26:46 +0000485 SkGPipeController* fController;
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000486 SkWriter32& fWriter;
487 size_t fBlockSize; // amount allocated for writer
488 size_t fBytesNotified;
489 bool fDone;
490 uint32_t fFlags;
reed@google.combb6992a2011-04-26 17:41:56 +0000491
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000492 SkRefCntSet fTypefaceSet;
reed@google.comf5842f72011-05-04 18:30:04 +0000493
494 uint32_t getTypefaceID(SkTypeface*);
495
reed@google.comacd471f2011-05-03 21:26:46 +0000496 inline void writeOp(DrawOps op, unsigned flags, unsigned data) {
reed@google.combb6992a2011-04-26 17:41:56 +0000497 fWriter.write32(DrawOp_packOpFlagData(op, flags, data));
498 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000499
reed@google.comacd471f2011-05-03 21:26:46 +0000500 inline void writeOp(DrawOps op) {
reed@google.combb6992a2011-04-26 17:41:56 +0000501 fWriter.write32(DrawOp_packOpFlagData(op, 0, 0));
502 }
reed@google.comacd471f2011-05-03 21:26:46 +0000503
504 bool needOpBytes(size_t size = 0);
505
506 inline void doNotify() {
507 if (!fDone) {
508 size_t bytes = fWriter.size() - fBytesNotified;
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000509 if (bytes > 0) {
510 fController->notifyWritten(bytes);
511 fBytesNotified += bytes;
512 }
reed@google.comacd471f2011-05-03 21:26:46 +0000513 }
514 }
reed@google.comb55d1182011-05-11 00:42:04 +0000515
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000516 // Should be called after any calls to an SkFlatDictionary::findAndReplace
517 // if a new SkFlatData was added when in cross process mode
518 void flattenFactoryNames();
519
scroggo@google.com4dffc592012-07-17 16:49:40 +0000520 // These are only used when in cross process, but with no shared address
521 // space, so bitmaps are flattened.
522 FlattenableHeap fBitmapHeap;
523 SkBitmapDictionary fBitmapDictionary;
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000524 int flattenToIndex(const SkBitmap&);
525
scroggo@google.com4dffc592012-07-17 16:49:40 +0000526 FlattenableHeap fFlattenableHeap;
527 FlatDictionary fFlatDictionary;
reed@google.comb55d1182011-05-11 00:42:04 +0000528 int fCurrFlatIndex[kCount_PaintFlats];
529 int flattenToIndex(SkFlattenable* obj, PaintFlats);
530
scroggo@google.com58be6822012-07-30 14:40:01 +0000531 // Common code used by drawBitmap* when flattening.
532 bool commonDrawBitmapFlatten(const SkBitmap& bm, DrawOps op, unsigned flags,
533 size_t opBytesNeeded, const SkPaint* paint);
534 // Common code used by drawBitmap* when storing in the heap.
535 bool commonDrawBitmapHeap(const SkBitmap& bm, DrawOps op, unsigned flags,
536 size_t opBytesNeeded, const SkPaint* paint);
537 // Convenience type for function pointer
538 typedef bool (SkGPipeCanvas::*BitmapCommonFunction)(const SkBitmap&,
539 DrawOps, unsigned,
540 size_t, const SkPaint*);
541
reed@google.com31891582011-05-12 03:03:56 +0000542 SkPaint fPaint;
543 void writePaint(const SkPaint&);
reed@google.combb6992a2011-04-26 17:41:56 +0000544
reed@google.comacd471f2011-05-03 21:26:46 +0000545 class AutoPipeNotify {
546 public:
547 AutoPipeNotify(SkGPipeCanvas* canvas) : fCanvas(canvas) {}
548 ~AutoPipeNotify() { fCanvas->doNotify(); }
549 private:
550 SkGPipeCanvas* fCanvas;
551 };
552 friend class AutoPipeNotify;
553
reed@google.combb6992a2011-04-26 17:41:56 +0000554 typedef SkCanvas INHERITED;
555};
556
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000557void SkGPipeCanvas::flattenFactoryNames() {
558 const char* name;
559 while ((name = fFactorySet->getNextAddedFactoryName()) != NULL) {
560 size_t len = strlen(name);
561 if (this->needOpBytes(len)) {
562 this->writeOp(kDef_Factory_DrawOp);
563 fWriter.writeString(name, len);
564 }
565 }
566}
567
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000568int SkGPipeCanvas::flattenToIndex(const SkBitmap & bitmap) {
scroggo@google.com565254b2012-06-28 15:41:32 +0000569 SkASSERT(shouldFlattenBitmaps(fFlags));
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000570 uint32_t flags = SkFlattenableWriteBuffer::kCrossProcess_Flag;
scroggo@google.com4dffc592012-07-17 16:49:40 +0000571 bool added, replaced;
572 const SkFlatData* flat = fBitmapDictionary.findAndReplace(
573 bitmap, flags, fBitmapHeap.flatToReplace(), &added, &replaced);
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000574
scroggo@google.com4dffc592012-07-17 16:49:40 +0000575 int index = flat->index();
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000576 if (added) {
577 this->flattenFactoryNames();
578 size_t flatSize = flat->flatSize();
579 if (this->needOpBytes(flatSize)) {
580 this->writeOp(kDef_Bitmap_DrawOp, 0, index);
581 fWriter.write(flat->data(), flatSize);
582 }
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000583 }
scroggo@google.com4dffc592012-07-17 16:49:40 +0000584 return index;
scroggo@google.comd3ba5cc2012-07-09 16:05:53 +0000585}
586
reed@google.comb55d1182011-05-11 00:42:04 +0000587// return 0 for NULL (or unflattenable obj), or index-base-1
scroggo@google.comd3ba5cc2012-07-09 16:05:53 +0000588// return ~(index-base-1) if an old flattenable was replaced
reed@google.comb55d1182011-05-11 00:42:04 +0000589int SkGPipeCanvas::flattenToIndex(SkFlattenable* obj, PaintFlats paintflat) {
590 if (NULL == obj) {
591 return 0;
592 }
reed@google.comb55d1182011-05-11 00:42:04 +0000593
scroggo@google.com4dffc592012-07-17 16:49:40 +0000594 uint32_t writeBufferFlags;
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000595 if (isCrossProcess(fFlags)) {
596 writeBufferFlags = SkFlattenableWriteBuffer::kCrossProcess_Flag;
scroggo@google.com565254b2012-06-28 15:41:32 +0000597 } else {
598 // Needed for bitmap shaders.
scroggo@google.com4dffc592012-07-17 16:49:40 +0000599 writeBufferFlags = SkFlattenableWriteBuffer::kForceFlattenBitmapPixels_Flag;
scroggo@google.com565254b2012-06-28 15:41:32 +0000600 }
reed@google.comdde09562011-05-23 12:21:05 +0000601
scroggo@google.com4dffc592012-07-17 16:49:40 +0000602 bool added, replaced;
603 const SkFlatData* flat = fFlatDictionary.findAndReplace(
604 *obj, writeBufferFlags, fFlattenableHeap.flatToReplace(), &added, &replaced);
605 int index = flat->index();
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000606 if (added) {
607 if (isCrossProcess(fFlags)) {
608 this->flattenFactoryNames();
609 }
610 size_t flatSize = flat->flatSize();
611 if (this->needOpBytes(flatSize)) {
612 this->writeOp(kDef_Flattenable_DrawOp, paintflat, index);
613 fWriter.write(flat->data(), flatSize);
614 }
scroggo@google.com4dffc592012-07-17 16:49:40 +0000615 }
616 if (replaced) {
reed@google.comb55d1182011-05-11 00:42:04 +0000617 index = ~index;
reed@google.comb55d1182011-05-11 00:42:04 +0000618 }
scroggo@google.com4dffc592012-07-17 16:49:40 +0000619 return index;
reed@google.comb55d1182011-05-11 00:42:04 +0000620}
621
reed@google.combb6992a2011-04-26 17:41:56 +0000622///////////////////////////////////////////////////////////////////////////////
623
reed@google.comacd471f2011-05-03 21:26:46 +0000624#define MIN_BLOCK_SIZE (16 * 1024)
scroggo@google.com4dffc592012-07-17 16:49:40 +0000625#define BITMAPS_TO_KEEP 5
626#define FLATTENABLES_TO_KEEP 10
reed@google.combb6992a2011-04-26 17:41:56 +0000627
reed@google.comacd471f2011-05-03 21:26:46 +0000628SkGPipeCanvas::SkGPipeCanvas(SkGPipeController* controller,
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000629 SkWriter32* writer, uint32_t flags)
630: fFactorySet(isCrossProcess(flags) ? SkNEW(SkNamedFactorySet) : NULL)
631, fSharedHeap(!isCrossProcess(flags), controller->numberOfReaders())
632, fWriter(*writer)
633, fFlags(flags)
scroggo@google.com15543602012-08-02 18:49:49 +0000634, fBitmapHeap(BITMAPS_TO_KEEP, fFactorySet)
635, fBitmapDictionary(&fBitmapHeap)
636, fFlattenableHeap(FLATTENABLES_TO_KEEP, fFactorySet)
637, fFlatDictionary(&fFlattenableHeap) {
reed@google.comacd471f2011-05-03 21:26:46 +0000638 fController = controller;
reed@google.combb6992a2011-04-26 17:41:56 +0000639 fDone = false;
reed@google.comacd471f2011-05-03 21:26:46 +0000640 fBlockSize = 0; // need first block from controller
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000641 fBytesNotified = 0;
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000642 fFirstSaveLayerStackLevel = kNoSaveLayer;
reed@google.comb55d1182011-05-11 00:42:04 +0000643 sk_bzero(fCurrFlatIndex, sizeof(fCurrFlatIndex));
reed@google.comacd471f2011-05-03 21:26:46 +0000644
reed@google.combb6793b2011-05-05 15:18:15 +0000645 // we need a device to limit our clip
646 // should the caller give us the bounds?
yangsu@google.com06b4da162011-06-17 15:04:40 +0000647 // We don't allocate pixels for the bitmap
reed@google.combb6793b2011-05-05 15:18:15 +0000648 SkBitmap bitmap;
649 bitmap.setConfig(SkBitmap::kARGB_8888_Config, 32767, 32767);
yangsu@google.com06b4da162011-06-17 15:04:40 +0000650 SkDevice* device = SkNEW_ARGS(SkDevice, (bitmap));
reed@google.combb6793b2011-05-05 15:18:15 +0000651 this->setDevice(device)->unref();
scroggo@google.com565254b2012-06-28 15:41:32 +0000652 // Tell the reader the appropriate flags to use.
653 if (this->needOpBytes()) {
654 this->writeOp(kReportFlags_DrawOp, fFlags, 0);
655 }
reed@google.combb6992a2011-04-26 17:41:56 +0000656}
657
658SkGPipeCanvas::~SkGPipeCanvas() {
659 this->finish();
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000660 SkSafeUnref(fFactorySet);
reed@google.combb6992a2011-04-26 17:41:56 +0000661}
662
reed@google.comacd471f2011-05-03 21:26:46 +0000663bool SkGPipeCanvas::needOpBytes(size_t needed) {
664 if (fDone) {
665 return false;
666 }
667
668 needed += 4; // size of DrawOp atom
669 if (fWriter.size() + needed > fBlockSize) {
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000670 // Before we wipe out any data that has already been written, read it
671 // out.
672 this->doNotify();
673 size_t blockSize = SkMax32(MIN_BLOCK_SIZE, needed);
674 void* block = fController->requestBlock(blockSize, &fBlockSize);
reed@google.comacd471f2011-05-03 21:26:46 +0000675 if (NULL == block) {
676 fDone = true;
677 return false;
678 }
679 fWriter.reset(block, fBlockSize);
680 fBytesNotified = 0;
681 }
682 return true;
683}
684
reed@google.comf5842f72011-05-04 18:30:04 +0000685uint32_t SkGPipeCanvas::getTypefaceID(SkTypeface* face) {
686 uint32_t id = 0; // 0 means default/null typeface
687 if (face) {
688 id = fTypefaceSet.find(face);
689 if (0 == id) {
690 id = fTypefaceSet.add(face);
691 size_t size = writeTypeface(NULL, face);
692 if (this->needOpBytes(size)) {
reed@google.combb6793b2011-05-05 15:18:15 +0000693 this->writeOp(kDef_Typeface_DrawOp);
reed@google.comf5842f72011-05-04 18:30:04 +0000694 writeTypeface(&fWriter, face);
695 }
696 }
697 }
698 return id;
699}
700
reed@google.combb6992a2011-04-26 17:41:56 +0000701///////////////////////////////////////////////////////////////////////////////
702
reed@google.comacd471f2011-05-03 21:26:46 +0000703#define NOTIFY_SETUP(canvas) \
704 AutoPipeNotify apn(canvas)
705
reed@google.combb6992a2011-04-26 17:41:56 +0000706int SkGPipeCanvas::save(SaveFlags flags) {
reed@google.comacd471f2011-05-03 21:26:46 +0000707 NOTIFY_SETUP(this);
708 if (this->needOpBytes()) {
709 this->writeOp(kSave_DrawOp, 0, flags);
710 }
reed@google.combb6992a2011-04-26 17:41:56 +0000711 return this->INHERITED::save(flags);
712}
713
714int SkGPipeCanvas::saveLayer(const SkRect* bounds, const SkPaint* paint,
715 SaveFlags saveFlags) {
reed@google.comacd471f2011-05-03 21:26:46 +0000716 NOTIFY_SETUP(this);
717 size_t size = 0;
reed@google.combb6992a2011-04-26 17:41:56 +0000718 unsigned opFlags = 0;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000719
reed@google.combb6992a2011-04-26 17:41:56 +0000720 if (bounds) {
721 opFlags |= kSaveLayer_HasBounds_DrawOpFlag;
reed@google.comacd471f2011-05-03 21:26:46 +0000722 size += sizeof(SkRect);
reed@google.combb6992a2011-04-26 17:41:56 +0000723 }
724 if (paint) {
725 opFlags |= kSaveLayer_HasPaint_DrawOpFlag;
reed@google.com31891582011-05-12 03:03:56 +0000726 this->writePaint(*paint);
reed@google.combb6992a2011-04-26 17:41:56 +0000727 }
728
reed@google.comacd471f2011-05-03 21:26:46 +0000729 if (this->needOpBytes(size)) {
730 this->writeOp(kSaveLayer_DrawOp, opFlags, saveFlags);
731 if (bounds) {
732 fWriter.writeRect(*bounds);
733 }
reed@google.combb6992a2011-04-26 17:41:56 +0000734 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000735
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000736 if (kNoSaveLayer == fFirstSaveLayerStackLevel){
737 fFirstSaveLayerStackLevel = this->getSaveCount();
738 }
reed@google.combb6992a2011-04-26 17:41:56 +0000739 // we just pass on the save, so we don't create a layer
740 return this->INHERITED::save(saveFlags);
741}
742
743void SkGPipeCanvas::restore() {
reed@google.comacd471f2011-05-03 21:26:46 +0000744 NOTIFY_SETUP(this);
745 if (this->needOpBytes()) {
746 this->writeOp(kRestore_DrawOp);
747 }
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000748
reed@google.combb6992a2011-04-26 17:41:56 +0000749 this->INHERITED::restore();
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000750
751 if (this->getSaveCount() == fFirstSaveLayerStackLevel){
752 fFirstSaveLayerStackLevel = kNoSaveLayer;
753 }
754}
755
756bool SkGPipeCanvas::isDrawingToLayer() const {
757 return kNoSaveLayer != fFirstSaveLayerStackLevel;
reed@google.combb6992a2011-04-26 17:41:56 +0000758}
759
760bool SkGPipeCanvas::translate(SkScalar dx, SkScalar dy) {
761 if (dx || dy) {
reed@google.comacd471f2011-05-03 21:26:46 +0000762 NOTIFY_SETUP(this);
763 if (this->needOpBytes(2 * sizeof(SkScalar))) {
764 this->writeOp(kTranslate_DrawOp);
765 fWriter.writeScalar(dx);
766 fWriter.writeScalar(dy);
767 }
reed@google.combb6992a2011-04-26 17:41:56 +0000768 }
769 return this->INHERITED::translate(dx, dy);
770}
771
772bool SkGPipeCanvas::scale(SkScalar sx, SkScalar sy) {
773 if (sx || sy) {
reed@google.comacd471f2011-05-03 21:26:46 +0000774 NOTIFY_SETUP(this);
775 if (this->needOpBytes(2 * sizeof(SkScalar))) {
776 this->writeOp(kScale_DrawOp);
777 fWriter.writeScalar(sx);
778 fWriter.writeScalar(sy);
779 }
reed@google.combb6992a2011-04-26 17:41:56 +0000780 }
781 return this->INHERITED::scale(sx, sy);
782}
783
784bool SkGPipeCanvas::rotate(SkScalar degrees) {
785 if (degrees) {
reed@google.comacd471f2011-05-03 21:26:46 +0000786 NOTIFY_SETUP(this);
787 if (this->needOpBytes(sizeof(SkScalar))) {
788 this->writeOp(kRotate_DrawOp);
789 fWriter.writeScalar(degrees);
790 }
reed@google.combb6992a2011-04-26 17:41:56 +0000791 }
792 return this->INHERITED::rotate(degrees);
793}
794
795bool SkGPipeCanvas::skew(SkScalar sx, SkScalar sy) {
796 if (sx || sy) {
reed@google.comacd471f2011-05-03 21:26:46 +0000797 NOTIFY_SETUP(this);
798 if (this->needOpBytes(2 * sizeof(SkScalar))) {
799 this->writeOp(kSkew_DrawOp);
800 fWriter.writeScalar(sx);
801 fWriter.writeScalar(sy);
802 }
reed@google.combb6992a2011-04-26 17:41:56 +0000803 }
804 return this->INHERITED::skew(sx, sy);
805}
806
807bool SkGPipeCanvas::concat(const SkMatrix& matrix) {
808 if (!matrix.isIdentity()) {
reed@google.comacd471f2011-05-03 21:26:46 +0000809 NOTIFY_SETUP(this);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000810 if (this->needOpBytes(matrix.writeToMemory(NULL))) {
reed@google.comacd471f2011-05-03 21:26:46 +0000811 this->writeOp(kConcat_DrawOp);
djsollen@google.com2b2ede32012-04-12 13:24:04 +0000812 fWriter.writeMatrix(matrix);
reed@google.comacd471f2011-05-03 21:26:46 +0000813 }
reed@google.combb6992a2011-04-26 17:41:56 +0000814 }
815 return this->INHERITED::concat(matrix);
816}
817
818void SkGPipeCanvas::setMatrix(const SkMatrix& matrix) {
reed@google.comacd471f2011-05-03 21:26:46 +0000819 NOTIFY_SETUP(this);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000820 if (this->needOpBytes(matrix.writeToMemory(NULL))) {
reed@google.comacd471f2011-05-03 21:26:46 +0000821 this->writeOp(kSetMatrix_DrawOp);
djsollen@google.com2b2ede32012-04-12 13:24:04 +0000822 fWriter.writeMatrix(matrix);
reed@google.comacd471f2011-05-03 21:26:46 +0000823 }
reed@google.combb6992a2011-04-26 17:41:56 +0000824 this->INHERITED::setMatrix(matrix);
825}
826
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000827bool SkGPipeCanvas::clipRect(const SkRect& rect, SkRegion::Op rgnOp,
828 bool doAntiAlias) {
reed@google.comacd471f2011-05-03 21:26:46 +0000829 NOTIFY_SETUP(this);
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000830 if (this->needOpBytes(sizeof(SkRect)) + sizeof(bool)) {
reed@google.comacd471f2011-05-03 21:26:46 +0000831 this->writeOp(kClipRect_DrawOp, 0, rgnOp);
832 fWriter.writeRect(rect);
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000833 fWriter.writeBool(doAntiAlias);
reed@google.comacd471f2011-05-03 21:26:46 +0000834 }
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000835 return this->INHERITED::clipRect(rect, rgnOp, doAntiAlias);
reed@google.combb6992a2011-04-26 17:41:56 +0000836}
837
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000838bool SkGPipeCanvas::clipPath(const SkPath& path, SkRegion::Op rgnOp,
839 bool doAntiAlias) {
reed@google.comacd471f2011-05-03 21:26:46 +0000840 NOTIFY_SETUP(this);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000841 if (this->needOpBytes(path.writeToMemory(NULL)) + sizeof(bool)) {
reed@google.comacd471f2011-05-03 21:26:46 +0000842 this->writeOp(kClipPath_DrawOp, 0, rgnOp);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000843 fWriter.writePath(path);
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000844 fWriter.writeBool(doAntiAlias);
reed@google.comacd471f2011-05-03 21:26:46 +0000845 }
reed@google.combb6992a2011-04-26 17:41:56 +0000846 // we just pass on the bounds of the path
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000847 return this->INHERITED::clipRect(path.getBounds(), rgnOp, doAntiAlias);
reed@google.combb6992a2011-04-26 17:41:56 +0000848}
849
850bool SkGPipeCanvas::clipRegion(const SkRegion& region, SkRegion::Op rgnOp) {
reed@google.comacd471f2011-05-03 21:26:46 +0000851 NOTIFY_SETUP(this);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000852 if (this->needOpBytes(region.writeToMemory(NULL))) {
reed@google.comacd471f2011-05-03 21:26:46 +0000853 this->writeOp(kClipRegion_DrawOp, 0, rgnOp);
djsollen@google.com2b2ede32012-04-12 13:24:04 +0000854 fWriter.writeRegion(region);
reed@google.comacd471f2011-05-03 21:26:46 +0000855 }
reed@google.combb6992a2011-04-26 17:41:56 +0000856 return this->INHERITED::clipRegion(region, rgnOp);
857}
858
859///////////////////////////////////////////////////////////////////////////////
860
861void SkGPipeCanvas::clear(SkColor color) {
reed@google.comacd471f2011-05-03 21:26:46 +0000862 NOTIFY_SETUP(this);
reed@google.combb6992a2011-04-26 17:41:56 +0000863 unsigned flags = 0;
864 if (color) {
865 flags |= kClear_HasColor_DrawOpFlag;
866 }
reed@google.comacd471f2011-05-03 21:26:46 +0000867 if (this->needOpBytes(sizeof(SkColor))) {
868 this->writeOp(kDrawClear_DrawOp, flags, 0);
869 if (color) {
870 fWriter.write32(color);
871 }
reed@google.combb6992a2011-04-26 17:41:56 +0000872 }
873}
874
875void SkGPipeCanvas::drawPaint(const SkPaint& paint) {
reed@google.comacd471f2011-05-03 21:26:46 +0000876 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000877 this->writePaint(paint);
reed@google.comacd471f2011-05-03 21:26:46 +0000878 if (this->needOpBytes()) {
reed@google.com31891582011-05-12 03:03:56 +0000879 this->writeOp(kDrawPaint_DrawOp);
reed@google.comacd471f2011-05-03 21:26:46 +0000880 }
reed@google.combb6992a2011-04-26 17:41:56 +0000881}
882
883void SkGPipeCanvas::drawPoints(PointMode mode, size_t count,
884 const SkPoint pts[], const SkPaint& paint) {
885 if (count) {
reed@google.comacd471f2011-05-03 21:26:46 +0000886 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000887 this->writePaint(paint);
reed@google.comacd471f2011-05-03 21:26:46 +0000888 if (this->needOpBytes(4 + count * sizeof(SkPoint))) {
reed@google.com31891582011-05-12 03:03:56 +0000889 this->writeOp(kDrawPoints_DrawOp, mode, 0);
reed@google.comacd471f2011-05-03 21:26:46 +0000890 fWriter.write32(count);
891 fWriter.write(pts, count * sizeof(SkPoint));
892 }
reed@google.combb6992a2011-04-26 17:41:56 +0000893 }
894}
895
896void SkGPipeCanvas::drawRect(const SkRect& rect, const SkPaint& paint) {
reed@google.comacd471f2011-05-03 21:26:46 +0000897 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000898 this->writePaint(paint);
reed@google.comacd471f2011-05-03 21:26:46 +0000899 if (this->needOpBytes(sizeof(SkRect))) {
reed@google.com31891582011-05-12 03:03:56 +0000900 this->writeOp(kDrawRect_DrawOp);
reed@google.comacd471f2011-05-03 21:26:46 +0000901 fWriter.writeRect(rect);
902 }
reed@google.combb6992a2011-04-26 17:41:56 +0000903}
904
905void SkGPipeCanvas::drawPath(const SkPath& path, const SkPaint& paint) {
reed@google.comacd471f2011-05-03 21:26:46 +0000906 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000907 this->writePaint(paint);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000908 if (this->needOpBytes(path.writeToMemory(NULL))) {
reed@google.com31891582011-05-12 03:03:56 +0000909 this->writeOp(kDrawPath_DrawOp);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000910 fWriter.writePath(path);
reed@google.comacd471f2011-05-03 21:26:46 +0000911 }
reed@google.combb6992a2011-04-26 17:41:56 +0000912}
913
scroggo@google.com58be6822012-07-30 14:40:01 +0000914bool SkGPipeCanvas::commonDrawBitmapFlatten(const SkBitmap& bm, DrawOps op,
915 unsigned flags,
916 size_t opBytesNeeded,
917 const SkPaint* paint) {
918 if (paint != NULL) {
919 flags |= kDrawBitmap_HasPaint_DrawOpsFlag;
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000920 this->writePaint(*paint);
921 }
scroggo@google.com58be6822012-07-30 14:40:01 +0000922 int bitmapIndex = this->flattenToIndex(bm);
scroggo@google.com565254b2012-06-28 15:41:32 +0000923 if (this->needOpBytes(opBytesNeeded)) {
scroggo@google.com58be6822012-07-30 14:40:01 +0000924 this->writeOp(op, flags, bitmapIndex);
925 return true;
926 }
927 return false;
928}
929
930bool SkGPipeCanvas::commonDrawBitmapHeap(const SkBitmap& bm, DrawOps op,
931 unsigned flags,
932 size_t opBytesNeeded,
933 const SkPaint* paint) {
934 const void* ptr = fSharedHeap.addBitmap(bm);
935 if (NULL == ptr) {
936 return false;
937 }
938 if (paint != NULL) {
939 flags |= kDrawBitmap_HasPaint_DrawOpsFlag;
940 this->writePaint(*paint);
941 }
942 if (this->needOpBytes(opBytesNeeded + sizeof(void*))) {
943 this->writeOp(op, flags, 0);
944 fWriter.writePtr(const_cast<void*>(ptr));
945 return true;
946 }
947 return false;
948}
949
950void SkGPipeCanvas::drawBitmap(const SkBitmap& bm, SkScalar left, SkScalar top,
951 const SkPaint* paint) {
952 NOTIFY_SETUP(this);
953 size_t opBytesNeeded = sizeof(SkScalar) * 2;
954
955 BitmapCommonFunction bitmapCommon = shouldFlattenBitmaps(fFlags) ?
956 &SkGPipeCanvas::commonDrawBitmapFlatten :
957 &SkGPipeCanvas::commonDrawBitmapHeap;
958
959 if ((*this.*bitmapCommon)(bm, kDrawBitmap_DrawOp, 0, opBytesNeeded, paint)) {
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000960 fWriter.writeScalar(left);
961 fWriter.writeScalar(top);
962 }
reed@google.combb6992a2011-04-26 17:41:56 +0000963}
964
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000965void SkGPipeCanvas::drawBitmapRect(const SkBitmap& bm, const SkIRect* src,
scroggo@google.com58be6822012-07-30 14:40:01 +0000966 const SkRect& dst, const SkPaint* paint) {
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000967 NOTIFY_SETUP(this);
scroggo@google.com58be6822012-07-30 14:40:01 +0000968 size_t opBytesNeeded = sizeof(SkRect);
969 bool hasSrc = src != NULL;
970 unsigned flags;
971 if (hasSrc) {
972 flags = kDrawBitmap_HasSrcRect_DrawOpsFlag;
973 opBytesNeeded += sizeof(int32_t) * 4;
974 } else {
975 flags = 0;
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000976 }
977
scroggo@google.com58be6822012-07-30 14:40:01 +0000978 BitmapCommonFunction bitmapCommon = shouldFlattenBitmaps(fFlags) ?
979 &SkGPipeCanvas::commonDrawBitmapFlatten :
980 &SkGPipeCanvas::commonDrawBitmapHeap;
981
982 if ((*this.*bitmapCommon)(bm, kDrawBitmapRect_DrawOp, flags, opBytesNeeded, paint)) {
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000983 if (hasSrc) {
984 fWriter.write32(src->fLeft);
985 fWriter.write32(src->fTop);
986 fWriter.write32(src->fRight);
987 fWriter.write32(src->fBottom);
988 }
989 fWriter.writeRect(dst);
990 }
reed@google.combb6992a2011-04-26 17:41:56 +0000991}
992
scroggo@google.com58be6822012-07-30 14:40:01 +0000993void SkGPipeCanvas::drawBitmapMatrix(const SkBitmap& bm, const SkMatrix& matrix,
994 const SkPaint* paint) {
995 NOTIFY_SETUP(this);
996 size_t opBytesNeeded = matrix.writeToMemory(NULL);
997
998 BitmapCommonFunction bitmapCommon = shouldFlattenBitmaps(fFlags) ?
999 &SkGPipeCanvas::commonDrawBitmapFlatten :
1000 &SkGPipeCanvas::commonDrawBitmapHeap;
1001
1002 if ((*this.*bitmapCommon)(bm, kDrawBitmapMatrix_DrawOp, 0, opBytesNeeded, paint)) {
1003 fWriter.writeMatrix(matrix);
1004 }
reed@google.combb6992a2011-04-26 17:41:56 +00001005}
scroggo@google.com16d1d0b2012-05-02 19:09:40 +00001006
1007void SkGPipeCanvas::drawBitmapNine(const SkBitmap& bm, const SkIRect& center,
scroggo@google.com5a2e8792012-04-20 17:39:51 +00001008 const SkRect& dst, const SkPaint* paint) {
scroggo@google.com16d1d0b2012-05-02 19:09:40 +00001009 NOTIFY_SETUP(this);
scroggo@google.com58be6822012-07-30 14:40:01 +00001010 size_t opBytesNeeded = sizeof(int32_t) * 4 + sizeof(SkRect);
scroggo@google.com16d1d0b2012-05-02 19:09:40 +00001011
scroggo@google.com58be6822012-07-30 14:40:01 +00001012 BitmapCommonFunction bitmapCommon = shouldFlattenBitmaps(fFlags) ?
1013 &SkGPipeCanvas::commonDrawBitmapFlatten :
1014 &SkGPipeCanvas::commonDrawBitmapHeap;
1015
1016 if ((*this.*bitmapCommon)(bm, kDrawBitmapNine_DrawOp, 0, opBytesNeeded, paint)) {
scroggo@google.com16d1d0b2012-05-02 19:09:40 +00001017 fWriter.write32(center.fLeft);
1018 fWriter.write32(center.fTop);
1019 fWriter.write32(center.fRight);
1020 fWriter.write32(center.fBottom);
1021 fWriter.writeRect(dst);
1022 }
scroggo@google.com5a2e8792012-04-20 17:39:51 +00001023}
scroggo@google.com16d1d0b2012-05-02 19:09:40 +00001024
1025void SkGPipeCanvas::drawSprite(const SkBitmap& bm, int left, int top,
1026 const SkPaint* paint) {
1027 NOTIFY_SETUP(this);
scroggo@google.com58be6822012-07-30 14:40:01 +00001028 size_t opBytesNeeded = sizeof(int32_t) * 2;
scroggo@google.com16d1d0b2012-05-02 19:09:40 +00001029
scroggo@google.com58be6822012-07-30 14:40:01 +00001030 BitmapCommonFunction bitmapCommon = shouldFlattenBitmaps(fFlags) ?
1031 &SkGPipeCanvas::commonDrawBitmapFlatten :
1032 &SkGPipeCanvas::commonDrawBitmapHeap;
1033
1034 if ((*this.*bitmapCommon)(bm, kDrawSprite_DrawOp, 0, opBytesNeeded, paint)) {
scroggo@google.com16d1d0b2012-05-02 19:09:40 +00001035 fWriter.write32(left);
1036 fWriter.write32(top);
1037 }
reed@google.combb6992a2011-04-26 17:41:56 +00001038}
1039
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001040void SkGPipeCanvas::drawText(const void* text, size_t byteLength, SkScalar x,
reed@google.combb6992a2011-04-26 17:41:56 +00001041 SkScalar y, const SkPaint& paint) {
1042 if (byteLength) {
reed@google.comacd471f2011-05-03 21:26:46 +00001043 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +00001044 this->writePaint(paint);
reed@google.comacd471f2011-05-03 21:26:46 +00001045 if (this->needOpBytes(4 + SkAlign4(byteLength) + 2 * sizeof(SkScalar))) {
reed@google.com31891582011-05-12 03:03:56 +00001046 this->writeOp(kDrawText_DrawOp);
reed@google.comacd471f2011-05-03 21:26:46 +00001047 fWriter.write32(byteLength);
1048 fWriter.writePad(text, byteLength);
1049 fWriter.writeScalar(x);
1050 fWriter.writeScalar(y);
1051 }
reed@google.combb6992a2011-04-26 17:41:56 +00001052 }
1053}
1054
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001055void SkGPipeCanvas::drawPosText(const void* text, size_t byteLength,
reed@google.combb6992a2011-04-26 17:41:56 +00001056 const SkPoint pos[], const SkPaint& paint) {
1057 if (byteLength) {
reed@google.comacd471f2011-05-03 21:26:46 +00001058 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +00001059 this->writePaint(paint);
reed@google.combb6992a2011-04-26 17:41:56 +00001060 int count = paint.textToGlyphs(text, byteLength, NULL);
reed@google.comacd471f2011-05-03 21:26:46 +00001061 if (this->needOpBytes(4 + SkAlign4(byteLength) + 4 + count * sizeof(SkPoint))) {
reed@google.com31891582011-05-12 03:03:56 +00001062 this->writeOp(kDrawPosText_DrawOp);
reed@google.comacd471f2011-05-03 21:26:46 +00001063 fWriter.write32(byteLength);
1064 fWriter.writePad(text, byteLength);
1065 fWriter.write32(count);
1066 fWriter.write(pos, count * sizeof(SkPoint));
1067 }
reed@google.combb6992a2011-04-26 17:41:56 +00001068 }
1069}
1070
1071void SkGPipeCanvas::drawPosTextH(const void* text, size_t byteLength,
1072 const SkScalar xpos[], SkScalar constY,
1073 const SkPaint& paint) {
1074 if (byteLength) {
reed@google.comacd471f2011-05-03 21:26:46 +00001075 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +00001076 this->writePaint(paint);
reed@google.combb6992a2011-04-26 17:41:56 +00001077 int count = paint.textToGlyphs(text, byteLength, NULL);
reed@google.comacd471f2011-05-03 21:26:46 +00001078 if (this->needOpBytes(4 + SkAlign4(byteLength) + 4 + count * sizeof(SkScalar) + 4)) {
reed@google.com31891582011-05-12 03:03:56 +00001079 this->writeOp(kDrawPosTextH_DrawOp);
reed@google.comacd471f2011-05-03 21:26:46 +00001080 fWriter.write32(byteLength);
1081 fWriter.writePad(text, byteLength);
1082 fWriter.write32(count);
1083 fWriter.write(xpos, count * sizeof(SkScalar));
1084 fWriter.writeScalar(constY);
1085 }
reed@google.combb6992a2011-04-26 17:41:56 +00001086 }
1087}
1088
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001089void SkGPipeCanvas::drawTextOnPath(const void* text, size_t byteLength,
1090 const SkPath& path, const SkMatrix* matrix,
reed@google.combb6992a2011-04-26 17:41:56 +00001091 const SkPaint& paint) {
1092 if (byteLength) {
reed@google.comacd471f2011-05-03 21:26:46 +00001093 NOTIFY_SETUP(this);
reed@google.combb6992a2011-04-26 17:41:56 +00001094 unsigned flags = 0;
djsollen@google.com94e75ee2012-06-08 18:30:46 +00001095 size_t size = 4 + SkAlign4(byteLength) + path.writeToMemory(NULL);
reed@google.combb6992a2011-04-26 17:41:56 +00001096 if (matrix) {
1097 flags |= kDrawTextOnPath_HasMatrix_DrawOpFlag;
djsollen@google.com94e75ee2012-06-08 18:30:46 +00001098 size += matrix->writeToMemory(NULL);
reed@google.combb6992a2011-04-26 17:41:56 +00001099 }
reed@google.com31891582011-05-12 03:03:56 +00001100 this->writePaint(paint);
reed@google.comacd471f2011-05-03 21:26:46 +00001101 if (this->needOpBytes(size)) {
reed@google.com31891582011-05-12 03:03:56 +00001102 this->writeOp(kDrawTextOnPath_DrawOp, flags, 0);
reed@google.combb6992a2011-04-26 17:41:56 +00001103
reed@google.comacd471f2011-05-03 21:26:46 +00001104 fWriter.write32(byteLength);
1105 fWriter.writePad(text, byteLength);
reed@google.combb6992a2011-04-26 17:41:56 +00001106
djsollen@google.com94e75ee2012-06-08 18:30:46 +00001107 fWriter.writePath(path);
reed@google.comacd471f2011-05-03 21:26:46 +00001108 if (matrix) {
djsollen@google.com2b2ede32012-04-12 13:24:04 +00001109 fWriter.writeMatrix(*matrix);
reed@google.comacd471f2011-05-03 21:26:46 +00001110 }
reed@google.combb6992a2011-04-26 17:41:56 +00001111 }
1112 }
1113}
1114
1115void SkGPipeCanvas::drawPicture(SkPicture& picture) {
reed@google.com0faac1e2011-05-11 05:58:58 +00001116 // we want to playback the picture into individual draw calls
1117 this->INHERITED::drawPicture(picture);
reed@google.combb6992a2011-04-26 17:41:56 +00001118}
1119
reed@google.combb6992a2011-04-26 17:41:56 +00001120void SkGPipeCanvas::drawVertices(VertexMode mode, int vertexCount,
1121 const SkPoint vertices[], const SkPoint texs[],
1122 const SkColor colors[], SkXfermode*,
1123 const uint16_t indices[], int indexCount,
1124 const SkPaint& paint) {
1125 if (0 == vertexCount) {
1126 return;
1127 }
1128
reed@google.comacd471f2011-05-03 21:26:46 +00001129 NOTIFY_SETUP(this);
1130 size_t size = 4 + vertexCount * sizeof(SkPoint);
reed@google.com31891582011-05-12 03:03:56 +00001131 this->writePaint(paint);
reed@google.combb6992a2011-04-26 17:41:56 +00001132 unsigned flags = 0;
1133 if (texs) {
1134 flags |= kDrawVertices_HasTexs_DrawOpFlag;
reed@google.comacd471f2011-05-03 21:26:46 +00001135 size += vertexCount * sizeof(SkPoint);
reed@google.combb6992a2011-04-26 17:41:56 +00001136 }
1137 if (colors) {
1138 flags |= kDrawVertices_HasColors_DrawOpFlag;
reed@google.comacd471f2011-05-03 21:26:46 +00001139 size += vertexCount * sizeof(SkColor);
reed@google.combb6992a2011-04-26 17:41:56 +00001140 }
1141 if (indices && indexCount > 0) {
1142 flags |= kDrawVertices_HasIndices_DrawOpFlag;
reed@google.comacd471f2011-05-03 21:26:46 +00001143 size += 4 + SkAlign4(indexCount * sizeof(uint16_t));
reed@google.combb6992a2011-04-26 17:41:56 +00001144 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001145
reed@google.comacd471f2011-05-03 21:26:46 +00001146 if (this->needOpBytes(size)) {
reed@google.com31891582011-05-12 03:03:56 +00001147 this->writeOp(kDrawVertices_DrawOp, flags, 0);
reed@google.comacd471f2011-05-03 21:26:46 +00001148 fWriter.write32(mode);
1149 fWriter.write32(vertexCount);
1150 fWriter.write(vertices, vertexCount * sizeof(SkPoint));
1151 if (texs) {
1152 fWriter.write(texs, vertexCount * sizeof(SkPoint));
1153 }
1154 if (colors) {
1155 fWriter.write(colors, vertexCount * sizeof(SkColor));
1156 }
reed@google.combb6992a2011-04-26 17:41:56 +00001157
reed@google.comacd471f2011-05-03 21:26:46 +00001158 // TODO: flatten xfermode
reed@google.combb6992a2011-04-26 17:41:56 +00001159
reed@google.comacd471f2011-05-03 21:26:46 +00001160 if (indices && indexCount > 0) {
1161 fWriter.write32(indexCount);
1162 fWriter.writePad(indices, indexCount * sizeof(uint16_t));
1163 }
reed@google.combb6992a2011-04-26 17:41:56 +00001164 }
1165}
1166
reed@google.comacd471f2011-05-03 21:26:46 +00001167void SkGPipeCanvas::drawData(const void* ptr, size_t size) {
1168 if (size && ptr) {
1169 NOTIFY_SETUP(this);
reed@google.combb6992a2011-04-26 17:41:56 +00001170 unsigned data = 0;
1171 if (size < (1 << DRAWOPS_DATA_BITS)) {
1172 data = (unsigned)size;
1173 }
reed@google.comacd471f2011-05-03 21:26:46 +00001174 if (this->needOpBytes(4 + SkAlign4(size))) {
1175 this->writeOp(kDrawData_DrawOp, 0, data);
1176 if (0 == data) {
1177 fWriter.write32(size);
1178 }
reed@google.combb6793b2011-05-05 15:18:15 +00001179 fWriter.writePad(ptr, size);
reed@google.combb6992a2011-04-26 17:41:56 +00001180 }
1181 }
1182}
1183
junov@chromium.org77eec242012-07-18 17:54:45 +00001184void SkGPipeCanvas::flushRecording(bool detachCurrentBlock) {
1185 doNotify();
1186 if (detachCurrentBlock) {
1187 // force a new block to be requested for the next recorded command
1188 fBlockSize = 0;
1189 }
1190}
1191
junov@chromium.org2e14ba82012-08-07 14:26:57 +00001192size_t SkGPipeCanvas::freeMemoryIfPossible(size_t bytesToFree) {
1193 return fSharedHeap.freeMemoryIfPossible(bytesToFree);
1194}
1195
reed@google.combb6992a2011-04-26 17:41:56 +00001196///////////////////////////////////////////////////////////////////////////////
1197
1198template <typename T> uint32_t castToU32(T value) {
1199 union {
1200 T fSrc;
1201 uint32_t fDst;
1202 } data;
1203 data.fSrc = value;
1204 return data.fDst;
1205}
1206
reed@google.com31891582011-05-12 03:03:56 +00001207void SkGPipeCanvas::writePaint(const SkPaint& paint) {
1208 SkPaint& base = fPaint;
reed@google.combb6992a2011-04-26 17:41:56 +00001209 uint32_t storage[32];
1210 uint32_t* ptr = storage;
reed@google.combb6992a2011-04-26 17:41:56 +00001211
1212 if (base.getFlags() != paint.getFlags()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001213 *ptr++ = PaintOp_packOpData(kFlags_PaintOp, paint.getFlags());
reed@google.comf5842f72011-05-04 18:30:04 +00001214 base.setFlags(paint.getFlags());
reed@google.combb6992a2011-04-26 17:41:56 +00001215 }
1216 if (base.getColor() != paint.getColor()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001217 *ptr++ = PaintOp_packOp(kColor_PaintOp);
1218 *ptr++ = paint.getColor();
reed@google.comf5842f72011-05-04 18:30:04 +00001219 base.setColor(paint.getColor());
reed@google.combb6992a2011-04-26 17:41:56 +00001220 }
1221 if (base.getStyle() != paint.getStyle()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001222 *ptr++ = PaintOp_packOpData(kStyle_PaintOp, paint.getStyle());
reed@google.comf5842f72011-05-04 18:30:04 +00001223 base.setStyle(paint.getStyle());
reed@google.combb6992a2011-04-26 17:41:56 +00001224 }
1225 if (base.getStrokeJoin() != paint.getStrokeJoin()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001226 *ptr++ = PaintOp_packOpData(kJoin_PaintOp, paint.getStrokeJoin());
reed@google.comf5842f72011-05-04 18:30:04 +00001227 base.setStrokeJoin(paint.getStrokeJoin());
reed@google.combb6992a2011-04-26 17:41:56 +00001228 }
1229 if (base.getStrokeCap() != paint.getStrokeCap()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001230 *ptr++ = PaintOp_packOpData(kCap_PaintOp, paint.getStrokeCap());
reed@google.comf5842f72011-05-04 18:30:04 +00001231 base.setStrokeCap(paint.getStrokeCap());
reed@google.combb6992a2011-04-26 17:41:56 +00001232 }
1233 if (base.getStrokeWidth() != paint.getStrokeWidth()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001234 *ptr++ = PaintOp_packOp(kWidth_PaintOp);
1235 *ptr++ = castToU32(paint.getStrokeWidth());
reed@google.comf5842f72011-05-04 18:30:04 +00001236 base.setStrokeWidth(paint.getStrokeWidth());
reed@google.combb6992a2011-04-26 17:41:56 +00001237 }
1238 if (base.getStrokeMiter() != paint.getStrokeMiter()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001239 *ptr++ = PaintOp_packOp(kMiter_PaintOp);
1240 *ptr++ = castToU32(paint.getStrokeMiter());
reed@google.comf5842f72011-05-04 18:30:04 +00001241 base.setStrokeMiter(paint.getStrokeMiter());
reed@google.combb6992a2011-04-26 17:41:56 +00001242 }
1243 if (base.getTextEncoding() != paint.getTextEncoding()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001244 *ptr++ = PaintOp_packOpData(kEncoding_PaintOp, paint.getTextEncoding());
reed@google.comf5842f72011-05-04 18:30:04 +00001245 base.setTextEncoding(paint.getTextEncoding());
reed@google.combb6992a2011-04-26 17:41:56 +00001246 }
1247 if (base.getHinting() != paint.getHinting()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001248 *ptr++ = PaintOp_packOpData(kHinting_PaintOp, paint.getHinting());
reed@google.comf5842f72011-05-04 18:30:04 +00001249 base.setHinting(paint.getHinting());
reed@google.combb6992a2011-04-26 17:41:56 +00001250 }
1251 if (base.getTextAlign() != paint.getTextAlign()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001252 *ptr++ = PaintOp_packOpData(kAlign_PaintOp, paint.getTextAlign());
reed@google.comf5842f72011-05-04 18:30:04 +00001253 base.setTextAlign(paint.getTextAlign());
reed@google.combb6992a2011-04-26 17:41:56 +00001254 }
1255 if (base.getTextSize() != paint.getTextSize()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001256 *ptr++ = PaintOp_packOp(kTextSize_PaintOp);
1257 *ptr++ = castToU32(paint.getTextSize());
reed@google.comf5842f72011-05-04 18:30:04 +00001258 base.setTextSize(paint.getTextSize());
reed@google.combb6992a2011-04-26 17:41:56 +00001259 }
1260 if (base.getTextScaleX() != paint.getTextScaleX()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001261 *ptr++ = PaintOp_packOp(kTextScaleX_PaintOp);
1262 *ptr++ = castToU32(paint.getTextScaleX());
reed@google.comf5842f72011-05-04 18:30:04 +00001263 base.setTextScaleX(paint.getTextScaleX());
reed@google.combb6992a2011-04-26 17:41:56 +00001264 }
1265 if (base.getTextSkewX() != paint.getTextSkewX()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001266 *ptr++ = PaintOp_packOp(kTextSkewX_PaintOp);
1267 *ptr++ = castToU32(paint.getTextSkewX());
reed@google.comf5842f72011-05-04 18:30:04 +00001268 base.setTextSkewX(paint.getTextSkewX());
1269 }
1270
1271 if (!SkTypeface::Equal(base.getTypeface(), paint.getTypeface())) {
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +00001272 if (isCrossProcess(fFlags)) {
scroggo@google.com3cb969f2012-07-27 20:39:19 +00001273 uint32_t id = this->getTypefaceID(paint.getTypeface());
1274 *ptr++ = PaintOp_packOpData(kTypeface_PaintOp, id);
1275 } else if (this->needOpBytes(sizeof(void*))) {
1276 // Add to the set for ref counting.
1277 fTypefaceSet.add(paint.getTypeface());
1278 // It is safe to write the typeface to the stream before the rest
1279 // of the paint unless we ever send a kReset_PaintOp, which we
1280 // currently never do.
1281 this->writeOp(kSetTypeface_DrawOp);
1282 fWriter.writePtr(paint.getTypeface());
1283 }
reed@google.comf5842f72011-05-04 18:30:04 +00001284 base.setTypeface(paint.getTypeface());
reed@google.combb6992a2011-04-26 17:41:56 +00001285 }
reed@google.combb6992a2011-04-26 17:41:56 +00001286
scroggo@google.com4dffc592012-07-17 16:49:40 +00001287 // This is a new paint, so all old flats can be safely purged, if necessary.
1288 fFlattenableHeap.markAllFlatsSafeToDelete();
reed@google.comb55d1182011-05-11 00:42:04 +00001289 for (int i = 0; i < kCount_PaintFlats; i++) {
1290 int index = this->flattenToIndex(get_paintflat(paint, i), (PaintFlats)i);
scroggo@google.comd3ba5cc2012-07-09 16:05:53 +00001291 bool replaced = index < 0;
1292 if (replaced) {
1293 index = ~index;
1294 }
scroggo@google.com4dffc592012-07-17 16:49:40 +00001295 // Store the index of any flat that needs to be kept. 0 means no flat.
1296 if (index > 0) {
1297 fFlattenableHeap.markFlatForKeeping(index);
1298 }
1299 SkASSERT(index >= 0 && index <= fFlatDictionary.count());
scroggo@google.comd3ba5cc2012-07-09 16:05:53 +00001300 if (index != fCurrFlatIndex[i] || replaced) {
reed@google.comb55d1182011-05-11 00:42:04 +00001301 *ptr++ = PaintOp_packOpFlagData(kFlatIndex_PaintOp, i, index);
1302 fCurrFlatIndex[i] = index;
1303 }
1304 }
1305
reed@google.comacd471f2011-05-03 21:26:46 +00001306 size_t size = (char*)ptr - (char*)storage;
1307 if (size && this->needOpBytes(size)) {
reed@google.comb55d1182011-05-11 00:42:04 +00001308 this->writeOp(kPaintOp_DrawOp, 0, size);
reed@google.comb55d1182011-05-11 00:42:04 +00001309 fWriter.write(storage, size);
reed@google.combb6992a2011-04-26 17:41:56 +00001310 for (size_t i = 0; i < size/4; i++) {
reed@google.comb55d1182011-05-11 00:42:04 +00001311// SkDebugf("[%d] %08X\n", i, storage[i]);
reed@google.combb6992a2011-04-26 17:41:56 +00001312 }
1313 }
reed@google.combb6992a2011-04-26 17:41:56 +00001314}
1315
1316///////////////////////////////////////////////////////////////////////////////
1317
1318#include "SkGPipe.h"
1319
scroggo@google.com3cb969f2012-07-27 20:39:19 +00001320SkGPipeController::~SkGPipeController() {
1321 SkSafeUnref(fCanvas);
1322}
1323
1324void SkGPipeController::setCanvas(SkGPipeCanvas* canvas) {
1325 SkRefCnt_SafeAssign(fCanvas, canvas);
1326}
1327
1328///////////////////////////////////////////////////////////////////////////////
1329
1330SkGPipeWriter::SkGPipeWriter()
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +00001331: fWriter(0) {
reed@google.combb6992a2011-04-26 17:41:56 +00001332 fCanvas = NULL;
1333}
1334
1335SkGPipeWriter::~SkGPipeWriter() {
reed@google.comacd471f2011-05-03 21:26:46 +00001336 this->endRecording();
reed@google.combb6992a2011-04-26 17:41:56 +00001337}
1338
scroggo@google.com565254b2012-06-28 15:41:32 +00001339SkCanvas* SkGPipeWriter::startRecording(SkGPipeController* controller, uint32_t flags) {
reed@google.combb6992a2011-04-26 17:41:56 +00001340 if (NULL == fCanvas) {
reed@google.comacd471f2011-05-03 21:26:46 +00001341 fWriter.reset(NULL, 0);
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +00001342 fCanvas = SkNEW_ARGS(SkGPipeCanvas, (controller, &fWriter, flags));
reed@google.combb6992a2011-04-26 17:41:56 +00001343 }
scroggo@google.com3cb969f2012-07-27 20:39:19 +00001344 controller->setCanvas(fCanvas);
reed@google.combb6992a2011-04-26 17:41:56 +00001345 return fCanvas;
1346}
1347
1348void SkGPipeWriter::endRecording() {
1349 if (fCanvas) {
1350 fCanvas->finish();
1351 fCanvas->unref();
1352 fCanvas = NULL;
1353 }
1354}
1355
junov@chromium.org2e14ba82012-08-07 14:26:57 +00001356void SkGPipeWriter::flushRecording(bool detachCurrentBlock) {
1357 if (fCanvas) {
1358 fCanvas->flushRecording(detachCurrentBlock);
1359 }
junov@chromium.org77eec242012-07-18 17:54:45 +00001360}
1361
junov@chromium.org2e14ba82012-08-07 14:26:57 +00001362size_t SkGPipeWriter::freeMemoryIfPossible(size_t bytesToFree) {
1363 if (fCanvas) {
1364 return fCanvas->freeMemoryIfPossible(bytesToFree);
1365 }
1366 return 0;
1367}
1368
1369size_t SkGPipeWriter::storageAllocatedForRecording() const {
scroggo@google.com15011ee2012-07-26 20:03:32 +00001370 return NULL == fCanvas ? 0 : fCanvas->storageAllocatedForRecording();
1371}
1372