blob: 75f1affa54887be986b0fa7de880bc6d653ae7be [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.com0c3e5fe2012-08-01 19:34:20 +000068 FlattenableHeap(int numFlatsToKeep)
69 : fNumFlatsToKeep(numFlatsToKeep) {
70 }
scroggo@google.com4dffc592012-07-17 16:49:40 +000071
72 ~FlattenableHeap() {
73 fPointers.freeAll();
74 }
75
76 virtual void* allocThrow(size_t bytes) SK_OVERRIDE;
77
78 virtual void unalloc(void* ptr) SK_OVERRIDE;
79
80 const SkFlatData* flatToReplace() const;
81
82 // Mark an SkFlatData as one that should not be returned by flatToReplace.
83 // Takes the result of SkFlatData::index() as its parameter.
84 void markFlatForKeeping(int index) {
85 *fFlatsThatMustBeKept.append() = index;
86 }
87
88 void markAllFlatsSafeToDelete() {
89 fFlatsThatMustBeKept.reset();
90 }
91
92private:
93 // Keep track of the indices (i.e. the result of SkFlatData::index()) of
94 // flats that must be kept, since they are on the current paint.
95 SkTDArray<int> fFlatsThatMustBeKept;
96 SkTDArray<void*> fPointers;
97 const int fNumFlatsToKeep;
98};
99
100void FlattenableHeap::unalloc(void* ptr) {
101 int indexToRemove = fPointers.rfind(ptr);
102 if (indexToRemove >= 0) {
103 sk_free(ptr);
104 fPointers.remove(indexToRemove);
105 }
106}
107
108void* FlattenableHeap::allocThrow(size_t bytes) {
109 void* ptr = sk_malloc_throw(bytes);
110 *fPointers.append() = ptr;
111 return ptr;
112}
113
114const SkFlatData* FlattenableHeap::flatToReplace() const {
115 // First, determine whether we should replace one.
116 if (fPointers.count() > fNumFlatsToKeep) {
117 // Look through the flattenable heap.
118 // TODO: Return the LRU flat.
119 for (int i = 0; i < fPointers.count(); i++) {
120 SkFlatData* potential = (SkFlatData*)fPointers[i];
121 // Make sure that it is not one that must be kept.
122 bool mustKeep = false;
123 for (int j = 0; j < fFlatsThatMustBeKept.count(); j++) {
124 if (potential->index() == fFlatsThatMustBeKept[j]) {
125 mustKeep = true;
126 break;
127 }
128 }
129 if (!mustKeep) {
130 return potential;
131 }
132 }
133 }
134 return NULL;
135}
136
137///////////////////////////////////////////////////////////////////////////////
138
139class FlatDictionary : public SkFlatDictionary<SkFlattenable> {
140public:
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000141 FlatDictionary(FlattenableHeap* heap, SkNamedFactorySet* factorySet)
scroggo@google.com4dffc592012-07-17 16:49:40 +0000142 : SkFlatDictionary<SkFlattenable>(heap, NULL, NULL, factorySet) {
143 fFlattenProc = &flattenFlattenableProc;
144 // No need to define fUnflattenProc since the writer will never
145 // unflatten the data.
146 }
147 static void flattenFlattenableProc(SkOrderedWriteBuffer& buffer,
148 const void* obj) {
149 buffer.writeFlattenable((SkFlattenable*)obj);
150 }
151
152};
153
154///////////////////////////////////////////////////////////////////////////////
155
scroggo@google.com8ae3c7f2012-06-13 17:47:52 +0000156/*
157 * Shared heap for storing large things that can be shared, for a stream
158 * used by multiple readers.
159 * TODO: Make the allocations all come from cross process safe address space
160 * TODO: Store paths (others?)
scroggo@google.com284bf502012-07-17 16:10:34 +0000161 * TODO: Generalize the LRU caching mechanism
scroggo@google.com8ae3c7f2012-06-13 17:47:52 +0000162 */
scroggo@google.com4dffc592012-07-17 16:49:40 +0000163class SharedHeap {
scroggo@google.com8ae3c7f2012-06-13 17:47:52 +0000164public:
scroggo@google.com4dffc592012-07-17 16:49:40 +0000165 SharedHeap(bool shallow, int numOfReaders)
scroggo@google.com284bf502012-07-17 16:10:34 +0000166 : fBitmapCount(0)
167 , fMostRecentlyUsed(NULL)
168 , fLeastRecentlyUsed(NULL)
169 , fCanDoShallowCopies(shallow)
scroggo@google.com15011ee2012-07-26 20:03:32 +0000170 , fNumberOfReaders(numOfReaders)
171 , fBytesAllocated(0) {}
scroggo@google.com4dffc592012-07-17 16:49:40 +0000172 ~SharedHeap() {
scroggo@google.com284bf502012-07-17 16:10:34 +0000173 BitmapInfo* iter = fMostRecentlyUsed;
174 while (iter != NULL) {
scroggo@google.com15011ee2012-07-26 20:03:32 +0000175 SkDEBUGCODE(fBytesAllocated -= (iter->fBytesAllocated + sizeof(BitmapInfo)));
scroggo@google.com284bf502012-07-17 16:10:34 +0000176 BitmapInfo* next = iter->fLessRecentlyUsed;
177 SkDELETE(iter);
178 fBitmapCount--;
179 iter = next;
scroggo@google.com8ae3c7f2012-06-13 17:47:52 +0000180 }
scroggo@google.com284bf502012-07-17 16:10:34 +0000181 SkASSERT(0 == fBitmapCount);
scroggo@google.com15011ee2012-07-26 20:03:32 +0000182 SkASSERT(0 == fBytesAllocated);
scroggo@google.com8ae3c7f2012-06-13 17:47:52 +0000183 }
184
185 /*
scroggo@google.com15011ee2012-07-26 20:03:32 +0000186 * Get the approximate number of bytes allocated.
187 *
188 * Not exact. Some SkBitmaps may share SkPixelRefs, in which case only one
189 * SkBitmap will take the size of the SkPixelRef into account (the first
190 * one). It is possible that the one which accounts for the SkPixelRef has
191 * been removed, in which case we will no longer be counting those bytes.
192 */
193 size_t bytesAllocated() { return fBytesAllocated; }
194
195 /*
scroggo@google.com8ae3c7f2012-06-13 17:47:52 +0000196 * Add a copy of a bitmap to the heap.
197 * @param bm The SkBitmap to be copied and placed in the heap.
scroggo@google.com284bf502012-07-17 16:10:34 +0000198 * @return void* Pointer to the BitmapInfo stored in the heap, which
199 * contains a copy of the SkBitmap. If NULL,
scroggo@google.com8ae3c7f2012-06-13 17:47:52 +0000200 * the bitmap could not be copied.
201 */
scroggo@google.com284bf502012-07-17 16:10:34 +0000202 const void* addBitmap(const SkBitmap& orig) {
scroggo@google.com4f1f6bf2012-07-02 13:35:09 +0000203 const uint32_t genID = orig.getGenerationID();
204 SkPixelRef* sharedPixelRef = NULL;
scroggo@google.com284bf502012-07-17 16:10:34 +0000205 // When looking to see if we've previously used this bitmap, start at
206 // the end, assuming that the caller is more likely to reuse a recent
207 // one.
208 BitmapInfo* iter = fMostRecentlyUsed;
209 while (iter != NULL) {
210 if (genID == iter->fGenID) {
211 SkBitmap* storedBitmap = iter->fBitmap;
scroggo@google.com15011ee2012-07-26 20:03:32 +0000212 // TODO: Perhaps we can share code with
213 // SkPictureRecord::PixelRefDictionaryEntry/
214 // BitmapIndexCacheEntry so we can do a binary search for a
215 // matching bitmap
scroggo@google.com6ea165d2012-07-03 14:52:08 +0000216 if (orig.pixelRefOffset() != storedBitmap->pixelRefOffset()
217 || orig.width() != storedBitmap->width()
218 || orig.height() != storedBitmap->height()) {
scroggo@google.com4f1f6bf2012-07-02 13:35:09 +0000219 // In this case, the bitmaps share a pixelRef, but have
scroggo@google.com6ea165d2012-07-03 14:52:08 +0000220 // different offsets or sizes. Keep track of the other
221 // bitmap so that instead of making another copy of the
222 // pixelRef we can use the copy we already made.
223 sharedPixelRef = storedBitmap->pixelRef();
scroggo@google.com4f1f6bf2012-07-02 13:35:09 +0000224 break;
225 }
scroggo@google.com284bf502012-07-17 16:10:34 +0000226 iter->addDraws(fNumberOfReaders);
227 this->setMostRecentlyUsed(iter);
228 return iter;
scroggo@google.com8ae3c7f2012-06-13 17:47:52 +0000229 }
scroggo@google.com284bf502012-07-17 16:10:34 +0000230 iter = iter->fLessRecentlyUsed;
scroggo@google.com8ae3c7f2012-06-13 17:47:52 +0000231 }
scroggo@google.com284bf502012-07-17 16:10:34 +0000232 SkAutoRef ar((SkRefCnt*)sharedPixelRef);
233 BitmapInfo* replace = this->bitmapToReplace(orig);
scroggo@google.com565254b2012-06-28 15:41:32 +0000234 SkBitmap* copy;
235 // If the bitmap is mutable, we still need to do a deep copy, since the
236 // caller may modify it afterwards. That said, if the bitmap is mutable,
237 // but has no pixelRef, the copy constructor actually does a deep copy.
scroggo@google.com4f1f6bf2012-07-02 13:35:09 +0000238 if (fCanDoShallowCopies && (orig.isImmutable() || !orig.pixelRef())) {
scroggo@google.com284bf502012-07-17 16:10:34 +0000239 if (NULL == replace) {
240 copy = SkNEW_ARGS(SkBitmap, (orig));
241 } else {
242 *replace->fBitmap = orig;
243 }
scroggo@google.com565254b2012-06-28 15:41:32 +0000244 } else {
scroggo@google.com4f1f6bf2012-07-02 13:35:09 +0000245 if (sharedPixelRef != NULL) {
scroggo@google.com284bf502012-07-17 16:10:34 +0000246 if (NULL == replace) {
247 // Do a shallow copy of the bitmap to get the width, height, etc
248 copy = SkNEW_ARGS(SkBitmap, (orig));
249 // Replace the pixelRef with the copy that was already made, and
250 // use the appropriate offset.
251 copy->setPixelRef(sharedPixelRef, orig.pixelRefOffset());
252 } else {
253 *replace->fBitmap = orig;
254 replace->fBitmap->setPixelRef(sharedPixelRef, orig.pixelRefOffset());
255 }
scroggo@google.com4f1f6bf2012-07-02 13:35:09 +0000256 } else {
scroggo@google.com284bf502012-07-17 16:10:34 +0000257 if (NULL == replace) {
258 copy = SkNEW(SkBitmap);
259 if (!orig.copyTo(copy, orig.getConfig())) {
260 delete copy;
261 return NULL;
262 }
263 } else {
264 if (!orig.copyTo(replace->fBitmap, orig.getConfig())) {
265 return NULL;
266 }
scroggo@google.com4f1f6bf2012-07-02 13:35:09 +0000267 }
scroggo@google.com565254b2012-06-28 15:41:32 +0000268 }
scroggo@google.com8ae3c7f2012-06-13 17:47:52 +0000269 }
scroggo@google.com284bf502012-07-17 16:10:34 +0000270 BitmapInfo* info;
271 if (NULL == replace) {
scroggo@google.com15011ee2012-07-26 20:03:32 +0000272 fBytesAllocated += sizeof(BitmapInfo);
scroggo@google.com284bf502012-07-17 16:10:34 +0000273 info = SkNEW_ARGS(BitmapInfo, (copy, genID, fNumberOfReaders));
274 fBitmapCount++;
275 } else {
scroggo@google.com15011ee2012-07-26 20:03:32 +0000276 fBytesAllocated -= replace->fBytesAllocated;
scroggo@google.com284bf502012-07-17 16:10:34 +0000277 replace->fGenID = genID;
278 replace->addDraws(fNumberOfReaders);
279 info = replace;
280 }
scroggo@google.com15011ee2012-07-26 20:03:32 +0000281 // Always include the size of the SkBitmap struct.
282 info->fBytesAllocated = sizeof(SkBitmap);
283 // If the SkBitmap does not share an SkPixelRef with an SkBitmap already
284 // in the SharedHeap, also include the size of its pixels.
285 if (NULL == sharedPixelRef) {
286 info->fBytesAllocated += orig.getSize();
287 }
288 fBytesAllocated += info->fBytesAllocated;
scroggo@google.com284bf502012-07-17 16:10:34 +0000289 this->setMostRecentlyUsed(info);
290 return info;
scroggo@google.com8ae3c7f2012-06-13 17:47:52 +0000291 }
292private:
scroggo@google.com284bf502012-07-17 16:10:34 +0000293 void setMostRecentlyUsed(BitmapInfo* info);
294 BitmapInfo* bitmapToReplace(const SkBitmap& bm) const;
295
296 int fBitmapCount;
297 BitmapInfo* fLeastRecentlyUsed;
298 BitmapInfo* fMostRecentlyUsed;
299 const bool fCanDoShallowCopies;
300 const int fNumberOfReaders;
scroggo@google.com15011ee2012-07-26 20:03:32 +0000301 size_t fBytesAllocated;
scroggo@google.com8ae3c7f2012-06-13 17:47:52 +0000302};
303
scroggo@google.com284bf502012-07-17 16:10:34 +0000304// We just "used" info. Update our LRU accordingly
scroggo@google.com4dffc592012-07-17 16:49:40 +0000305void SharedHeap::setMostRecentlyUsed(BitmapInfo* info) {
scroggo@google.com284bf502012-07-17 16:10:34 +0000306 SkASSERT(info != NULL);
307 if (info == fMostRecentlyUsed) {
308 return;
309 }
310 // Remove info from its prior place, and make sure to cover the hole.
311 if (fLeastRecentlyUsed == info) {
312 SkASSERT(info->fMoreRecentlyUsed != NULL);
313 fLeastRecentlyUsed = info->fMoreRecentlyUsed;
314 }
315 if (info->fMoreRecentlyUsed != NULL) {
316 SkASSERT(fMostRecentlyUsed != info);
317 info->fMoreRecentlyUsed->fLessRecentlyUsed = info->fLessRecentlyUsed;
318 }
319 if (info->fLessRecentlyUsed != NULL) {
320 SkASSERT(fLeastRecentlyUsed != info);
321 info->fLessRecentlyUsed->fMoreRecentlyUsed = info->fMoreRecentlyUsed;
322 }
323 info->fMoreRecentlyUsed = NULL;
324 // Set up the head and tail pointers properly.
325 if (fMostRecentlyUsed != NULL) {
326 SkASSERT(NULL == fMostRecentlyUsed->fMoreRecentlyUsed);
327 fMostRecentlyUsed->fMoreRecentlyUsed = info;
328 info->fLessRecentlyUsed = fMostRecentlyUsed;
329 }
330 fMostRecentlyUsed = info;
331 if (NULL == fLeastRecentlyUsed) {
332 fLeastRecentlyUsed = info;
333 }
334}
335
336/**
337 * Given a new bitmap to be added to the cache, return an existing one that
338 * should be removed to make room, or NULL if there is already room.
339 */
scroggo@google.com4dffc592012-07-17 16:49:40 +0000340BitmapInfo* SharedHeap::bitmapToReplace(const SkBitmap& bm) const {
scroggo@google.com284bf502012-07-17 16:10:34 +0000341 // Arbitrarily set a limit of 5. We should test to find the best tradeoff
342 // between time and space. A lower limit means that we use less space, but
343 // it also means that we may have to insert the same bitmap into the heap
344 // multiple times (depending on the input), potentially taking more time.
345 // On the other hand, a lower limit also means searching through our stored
346 // bitmaps takes less time.
347 if (fBitmapCount > 5) {
348 BitmapInfo* iter = fLeastRecentlyUsed;
349 while (iter != NULL) {
350 if (iter->drawCount() > 0) {
351 // If the least recently used bitmap has not been drawn by some
352 // reader, then a more recently used one will not have been
353 // drawn yet either.
354 return NULL;
355 }
356 if (bm.pixelRef() != NULL
357 && bm.pixelRef() == iter->fBitmap->pixelRef()) {
358 // Do not replace a bitmap with a new one using the same
359 // pixel ref. Instead look for a different one that will
360 // potentially free up more space.
361 iter = iter->fMoreRecentlyUsed;
362 } else {
363 return iter;
364 }
365 }
366 }
367 return NULL;
368}
369
scroggo@google.com8ae3c7f2012-06-13 17:47:52 +0000370///////////////////////////////////////////////////////////////////////////////
371
reed@google.combb6992a2011-04-26 17:41:56 +0000372class SkGPipeCanvas : public SkCanvas {
373public:
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000374 SkGPipeCanvas(SkGPipeController*, SkWriter32*, uint32_t flags);
reed@google.combb6992a2011-04-26 17:41:56 +0000375 virtual ~SkGPipeCanvas();
376
377 void finish() {
378 if (!fDone) {
reed@google.comdbccc882011-07-08 18:53:39 +0000379 if (this->needOpBytes()) {
380 this->writeOp(kDone_DrawOp);
381 this->doNotify();
382 }
reed@google.combb6992a2011-04-26 17:41:56 +0000383 fDone = true;
384 }
385 }
386
junov@chromium.org77eec242012-07-18 17:54:45 +0000387 void flushRecording(bool detachCurrentBlock);
388
scroggo@google.com15011ee2012-07-26 20:03:32 +0000389 size_t storageAllocatedForRecording() {
390 return fSharedHeap.bytesAllocated();
391 }
392
reed@google.combb6992a2011-04-26 17:41:56 +0000393 // overrides from SkCanvas
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000394 virtual int save(SaveFlags) SK_OVERRIDE;
395 virtual int saveLayer(const SkRect* bounds, const SkPaint*,
396 SaveFlags) SK_OVERRIDE;
397 virtual void restore() SK_OVERRIDE;
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000398 virtual bool isDrawingToLayer() const SK_OVERRIDE;
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000399 virtual bool translate(SkScalar dx, SkScalar dy) SK_OVERRIDE;
400 virtual bool scale(SkScalar sx, SkScalar sy) SK_OVERRIDE;
401 virtual bool rotate(SkScalar degrees) SK_OVERRIDE;
402 virtual bool skew(SkScalar sx, SkScalar sy) SK_OVERRIDE;
403 virtual bool concat(const SkMatrix& matrix) SK_OVERRIDE;
404 virtual void setMatrix(const SkMatrix& matrix) SK_OVERRIDE;
405 virtual bool clipRect(const SkRect& rect, SkRegion::Op op,
406 bool doAntiAlias = false) SK_OVERRIDE;
407 virtual bool clipPath(const SkPath& path, SkRegion::Op op,
408 bool doAntiAlias = false) SK_OVERRIDE;
409 virtual bool clipRegion(const SkRegion& region, SkRegion::Op op) SK_OVERRIDE;
410 virtual void clear(SkColor) SK_OVERRIDE;
411 virtual void drawPaint(const SkPaint& paint) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000412 virtual void drawPoints(PointMode, size_t count, const SkPoint pts[],
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000413 const SkPaint&) SK_OVERRIDE;
414 virtual void drawRect(const SkRect& rect, const SkPaint&) SK_OVERRIDE;
415 virtual void drawPath(const SkPath& path, const SkPaint&) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000416 virtual void drawBitmap(const SkBitmap&, SkScalar left, SkScalar top,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000417 const SkPaint*) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000418 virtual void drawBitmapRect(const SkBitmap&, const SkIRect* src,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000419 const SkRect& dst, const SkPaint*) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000420 virtual void drawBitmapMatrix(const SkBitmap&, const SkMatrix&,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000421 const SkPaint*) SK_OVERRIDE;
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000422 virtual void drawBitmapNine(const SkBitmap& bitmap, const SkIRect& center,
423 const SkRect& dst, const SkPaint* paint = NULL) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000424 virtual void drawSprite(const SkBitmap&, int left, int top,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000425 const SkPaint*) SK_OVERRIDE;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000426 virtual void drawText(const void* text, size_t byteLength, SkScalar x,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000427 SkScalar y, const SkPaint&) SK_OVERRIDE;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000428 virtual void drawPosText(const void* text, size_t byteLength,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000429 const SkPoint pos[], const SkPaint&) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000430 virtual void drawPosTextH(const void* text, size_t byteLength,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000431 const SkScalar xpos[], SkScalar constY,
432 const SkPaint&) SK_OVERRIDE;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000433 virtual void drawTextOnPath(const void* text, size_t byteLength,
434 const SkPath& path, const SkMatrix* matrix,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000435 const SkPaint&) SK_OVERRIDE;
436 virtual void drawPicture(SkPicture& picture) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000437 virtual void drawVertices(VertexMode, int vertexCount,
438 const SkPoint vertices[], const SkPoint texs[],
439 const SkColor colors[], SkXfermode*,
440 const uint16_t indices[], int indexCount,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000441 const SkPaint&) SK_OVERRIDE;
442 virtual void drawData(const void*, size_t) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000443
444private:
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000445 enum {
446 kNoSaveLayer = -1,
447 };
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000448 SkNamedFactorySet* fFactorySet;
449 int fFirstSaveLayerStackLevel;
450 SharedHeap fSharedHeap;
reed@google.comacd471f2011-05-03 21:26:46 +0000451 SkGPipeController* fController;
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000452 SkWriter32& fWriter;
453 size_t fBlockSize; // amount allocated for writer
454 size_t fBytesNotified;
455 bool fDone;
456 uint32_t fFlags;
reed@google.combb6992a2011-04-26 17:41:56 +0000457
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000458 SkRefCntSet fTypefaceSet;
reed@google.comf5842f72011-05-04 18:30:04 +0000459
460 uint32_t getTypefaceID(SkTypeface*);
461
reed@google.comacd471f2011-05-03 21:26:46 +0000462 inline void writeOp(DrawOps op, unsigned flags, unsigned data) {
reed@google.combb6992a2011-04-26 17:41:56 +0000463 fWriter.write32(DrawOp_packOpFlagData(op, flags, data));
464 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000465
reed@google.comacd471f2011-05-03 21:26:46 +0000466 inline void writeOp(DrawOps op) {
reed@google.combb6992a2011-04-26 17:41:56 +0000467 fWriter.write32(DrawOp_packOpFlagData(op, 0, 0));
468 }
reed@google.comacd471f2011-05-03 21:26:46 +0000469
470 bool needOpBytes(size_t size = 0);
471
472 inline void doNotify() {
473 if (!fDone) {
474 size_t bytes = fWriter.size() - fBytesNotified;
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000475 if (bytes > 0) {
476 fController->notifyWritten(bytes);
477 fBytesNotified += bytes;
478 }
reed@google.comacd471f2011-05-03 21:26:46 +0000479 }
480 }
reed@google.comb55d1182011-05-11 00:42:04 +0000481
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000482 // Should be called after any calls to an SkFlatDictionary::findAndReplace
483 // if a new SkFlatData was added when in cross process mode
484 void flattenFactoryNames();
485
scroggo@google.com4dffc592012-07-17 16:49:40 +0000486 // These are only used when in cross process, but with no shared address
487 // space, so bitmaps are flattened.
488 FlattenableHeap fBitmapHeap;
489 SkBitmapDictionary fBitmapDictionary;
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000490 int flattenToIndex(const SkBitmap&);
491
scroggo@google.com4dffc592012-07-17 16:49:40 +0000492 FlattenableHeap fFlattenableHeap;
493 FlatDictionary fFlatDictionary;
reed@google.comb55d1182011-05-11 00:42:04 +0000494 int fCurrFlatIndex[kCount_PaintFlats];
495 int flattenToIndex(SkFlattenable* obj, PaintFlats);
496
scroggo@google.com58be6822012-07-30 14:40:01 +0000497 // Common code used by drawBitmap* when flattening.
498 bool commonDrawBitmapFlatten(const SkBitmap& bm, DrawOps op, unsigned flags,
499 size_t opBytesNeeded, const SkPaint* paint);
500 // Common code used by drawBitmap* when storing in the heap.
501 bool commonDrawBitmapHeap(const SkBitmap& bm, DrawOps op, unsigned flags,
502 size_t opBytesNeeded, const SkPaint* paint);
503 // Convenience type for function pointer
504 typedef bool (SkGPipeCanvas::*BitmapCommonFunction)(const SkBitmap&,
505 DrawOps, unsigned,
506 size_t, const SkPaint*);
507
reed@google.com31891582011-05-12 03:03:56 +0000508 SkPaint fPaint;
509 void writePaint(const SkPaint&);
reed@google.combb6992a2011-04-26 17:41:56 +0000510
reed@google.comacd471f2011-05-03 21:26:46 +0000511 class AutoPipeNotify {
512 public:
513 AutoPipeNotify(SkGPipeCanvas* canvas) : fCanvas(canvas) {}
514 ~AutoPipeNotify() { fCanvas->doNotify(); }
515 private:
516 SkGPipeCanvas* fCanvas;
517 };
518 friend class AutoPipeNotify;
519
reed@google.combb6992a2011-04-26 17:41:56 +0000520 typedef SkCanvas INHERITED;
521};
522
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000523void SkGPipeCanvas::flattenFactoryNames() {
524 const char* name;
525 while ((name = fFactorySet->getNextAddedFactoryName()) != NULL) {
526 size_t len = strlen(name);
527 if (this->needOpBytes(len)) {
528 this->writeOp(kDef_Factory_DrawOp);
529 fWriter.writeString(name, len);
530 }
531 }
532}
533
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000534int SkGPipeCanvas::flattenToIndex(const SkBitmap & bitmap) {
scroggo@google.com565254b2012-06-28 15:41:32 +0000535 SkASSERT(shouldFlattenBitmaps(fFlags));
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000536 uint32_t flags = SkFlattenableWriteBuffer::kCrossProcess_Flag;
scroggo@google.com4dffc592012-07-17 16:49:40 +0000537 bool added, replaced;
538 const SkFlatData* flat = fBitmapDictionary.findAndReplace(
539 bitmap, flags, fBitmapHeap.flatToReplace(), &added, &replaced);
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000540
scroggo@google.com4dffc592012-07-17 16:49:40 +0000541 int index = flat->index();
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000542 if (added) {
543 this->flattenFactoryNames();
544 size_t flatSize = flat->flatSize();
545 if (this->needOpBytes(flatSize)) {
546 this->writeOp(kDef_Bitmap_DrawOp, 0, index);
547 fWriter.write(flat->data(), flatSize);
548 }
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000549 }
scroggo@google.com4dffc592012-07-17 16:49:40 +0000550 return index;
scroggo@google.comd3ba5cc2012-07-09 16:05:53 +0000551}
552
reed@google.comb55d1182011-05-11 00:42:04 +0000553// return 0 for NULL (or unflattenable obj), or index-base-1
scroggo@google.comd3ba5cc2012-07-09 16:05:53 +0000554// return ~(index-base-1) if an old flattenable was replaced
reed@google.comb55d1182011-05-11 00:42:04 +0000555int SkGPipeCanvas::flattenToIndex(SkFlattenable* obj, PaintFlats paintflat) {
556 if (NULL == obj) {
557 return 0;
558 }
reed@google.comb55d1182011-05-11 00:42:04 +0000559
scroggo@google.com4dffc592012-07-17 16:49:40 +0000560 uint32_t writeBufferFlags;
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000561 if (isCrossProcess(fFlags)) {
562 writeBufferFlags = SkFlattenableWriteBuffer::kCrossProcess_Flag;
scroggo@google.com565254b2012-06-28 15:41:32 +0000563 } else {
564 // Needed for bitmap shaders.
scroggo@google.com4dffc592012-07-17 16:49:40 +0000565 writeBufferFlags = SkFlattenableWriteBuffer::kForceFlattenBitmapPixels_Flag;
scroggo@google.com565254b2012-06-28 15:41:32 +0000566 }
reed@google.comdde09562011-05-23 12:21:05 +0000567
scroggo@google.com4dffc592012-07-17 16:49:40 +0000568 bool added, replaced;
569 const SkFlatData* flat = fFlatDictionary.findAndReplace(
570 *obj, writeBufferFlags, fFlattenableHeap.flatToReplace(), &added, &replaced);
571 int index = flat->index();
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000572 if (added) {
573 if (isCrossProcess(fFlags)) {
574 this->flattenFactoryNames();
575 }
576 size_t flatSize = flat->flatSize();
577 if (this->needOpBytes(flatSize)) {
578 this->writeOp(kDef_Flattenable_DrawOp, paintflat, index);
579 fWriter.write(flat->data(), flatSize);
580 }
scroggo@google.com4dffc592012-07-17 16:49:40 +0000581 }
582 if (replaced) {
reed@google.comb55d1182011-05-11 00:42:04 +0000583 index = ~index;
reed@google.comb55d1182011-05-11 00:42:04 +0000584 }
scroggo@google.com4dffc592012-07-17 16:49:40 +0000585 return index;
reed@google.comb55d1182011-05-11 00:42:04 +0000586}
587
reed@google.combb6992a2011-04-26 17:41:56 +0000588///////////////////////////////////////////////////////////////////////////////
589
reed@google.comacd471f2011-05-03 21:26:46 +0000590#define MIN_BLOCK_SIZE (16 * 1024)
scroggo@google.com4dffc592012-07-17 16:49:40 +0000591#define BITMAPS_TO_KEEP 5
592#define FLATTENABLES_TO_KEEP 10
reed@google.combb6992a2011-04-26 17:41:56 +0000593
reed@google.comacd471f2011-05-03 21:26:46 +0000594SkGPipeCanvas::SkGPipeCanvas(SkGPipeController* controller,
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000595 SkWriter32* writer, uint32_t flags)
596: fFactorySet(isCrossProcess(flags) ? SkNEW(SkNamedFactorySet) : NULL)
597, fSharedHeap(!isCrossProcess(flags), controller->numberOfReaders())
598, fWriter(*writer)
599, fFlags(flags)
600, fBitmapHeap(BITMAPS_TO_KEEP)
601, fBitmapDictionary(&fBitmapHeap, NULL, NULL, fFactorySet)
602, fFlattenableHeap(FLATTENABLES_TO_KEEP)
603, fFlatDictionary(&fFlattenableHeap, fFactorySet) {
reed@google.comacd471f2011-05-03 21:26:46 +0000604 fController = controller;
reed@google.combb6992a2011-04-26 17:41:56 +0000605 fDone = false;
reed@google.comacd471f2011-05-03 21:26:46 +0000606 fBlockSize = 0; // need first block from controller
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000607 fBytesNotified = 0;
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000608 fFirstSaveLayerStackLevel = kNoSaveLayer;
reed@google.comb55d1182011-05-11 00:42:04 +0000609 sk_bzero(fCurrFlatIndex, sizeof(fCurrFlatIndex));
reed@google.comacd471f2011-05-03 21:26:46 +0000610
reed@google.combb6793b2011-05-05 15:18:15 +0000611 // we need a device to limit our clip
612 // should the caller give us the bounds?
yangsu@google.com06b4da162011-06-17 15:04:40 +0000613 // We don't allocate pixels for the bitmap
reed@google.combb6793b2011-05-05 15:18:15 +0000614 SkBitmap bitmap;
615 bitmap.setConfig(SkBitmap::kARGB_8888_Config, 32767, 32767);
yangsu@google.com06b4da162011-06-17 15:04:40 +0000616 SkDevice* device = SkNEW_ARGS(SkDevice, (bitmap));
reed@google.combb6793b2011-05-05 15:18:15 +0000617 this->setDevice(device)->unref();
scroggo@google.com565254b2012-06-28 15:41:32 +0000618 // Tell the reader the appropriate flags to use.
619 if (this->needOpBytes()) {
620 this->writeOp(kReportFlags_DrawOp, fFlags, 0);
621 }
reed@google.combb6992a2011-04-26 17:41:56 +0000622}
623
624SkGPipeCanvas::~SkGPipeCanvas() {
625 this->finish();
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000626 SkSafeUnref(fFactorySet);
reed@google.combb6992a2011-04-26 17:41:56 +0000627}
628
reed@google.comacd471f2011-05-03 21:26:46 +0000629bool SkGPipeCanvas::needOpBytes(size_t needed) {
630 if (fDone) {
631 return false;
632 }
633
634 needed += 4; // size of DrawOp atom
635 if (fWriter.size() + needed > fBlockSize) {
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000636 // Before we wipe out any data that has already been written, read it
637 // out.
638 this->doNotify();
639 size_t blockSize = SkMax32(MIN_BLOCK_SIZE, needed);
640 void* block = fController->requestBlock(blockSize, &fBlockSize);
reed@google.comacd471f2011-05-03 21:26:46 +0000641 if (NULL == block) {
642 fDone = true;
643 return false;
644 }
645 fWriter.reset(block, fBlockSize);
646 fBytesNotified = 0;
647 }
648 return true;
649}
650
reed@google.comf5842f72011-05-04 18:30:04 +0000651uint32_t SkGPipeCanvas::getTypefaceID(SkTypeface* face) {
652 uint32_t id = 0; // 0 means default/null typeface
653 if (face) {
654 id = fTypefaceSet.find(face);
655 if (0 == id) {
656 id = fTypefaceSet.add(face);
657 size_t size = writeTypeface(NULL, face);
658 if (this->needOpBytes(size)) {
reed@google.combb6793b2011-05-05 15:18:15 +0000659 this->writeOp(kDef_Typeface_DrawOp);
reed@google.comf5842f72011-05-04 18:30:04 +0000660 writeTypeface(&fWriter, face);
661 }
662 }
663 }
664 return id;
665}
666
reed@google.combb6992a2011-04-26 17:41:56 +0000667///////////////////////////////////////////////////////////////////////////////
668
reed@google.comacd471f2011-05-03 21:26:46 +0000669#define NOTIFY_SETUP(canvas) \
670 AutoPipeNotify apn(canvas)
671
reed@google.combb6992a2011-04-26 17:41:56 +0000672int SkGPipeCanvas::save(SaveFlags flags) {
reed@google.comacd471f2011-05-03 21:26:46 +0000673 NOTIFY_SETUP(this);
674 if (this->needOpBytes()) {
675 this->writeOp(kSave_DrawOp, 0, flags);
676 }
reed@google.combb6992a2011-04-26 17:41:56 +0000677 return this->INHERITED::save(flags);
678}
679
680int SkGPipeCanvas::saveLayer(const SkRect* bounds, const SkPaint* paint,
681 SaveFlags saveFlags) {
reed@google.comacd471f2011-05-03 21:26:46 +0000682 NOTIFY_SETUP(this);
683 size_t size = 0;
reed@google.combb6992a2011-04-26 17:41:56 +0000684 unsigned opFlags = 0;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000685
reed@google.combb6992a2011-04-26 17:41:56 +0000686 if (bounds) {
687 opFlags |= kSaveLayer_HasBounds_DrawOpFlag;
reed@google.comacd471f2011-05-03 21:26:46 +0000688 size += sizeof(SkRect);
reed@google.combb6992a2011-04-26 17:41:56 +0000689 }
690 if (paint) {
691 opFlags |= kSaveLayer_HasPaint_DrawOpFlag;
reed@google.com31891582011-05-12 03:03:56 +0000692 this->writePaint(*paint);
reed@google.combb6992a2011-04-26 17:41:56 +0000693 }
694
reed@google.comacd471f2011-05-03 21:26:46 +0000695 if (this->needOpBytes(size)) {
696 this->writeOp(kSaveLayer_DrawOp, opFlags, saveFlags);
697 if (bounds) {
698 fWriter.writeRect(*bounds);
699 }
reed@google.combb6992a2011-04-26 17:41:56 +0000700 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000701
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000702 if (kNoSaveLayer == fFirstSaveLayerStackLevel){
703 fFirstSaveLayerStackLevel = this->getSaveCount();
704 }
reed@google.combb6992a2011-04-26 17:41:56 +0000705 // we just pass on the save, so we don't create a layer
706 return this->INHERITED::save(saveFlags);
707}
708
709void SkGPipeCanvas::restore() {
reed@google.comacd471f2011-05-03 21:26:46 +0000710 NOTIFY_SETUP(this);
711 if (this->needOpBytes()) {
712 this->writeOp(kRestore_DrawOp);
713 }
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000714
reed@google.combb6992a2011-04-26 17:41:56 +0000715 this->INHERITED::restore();
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000716
717 if (this->getSaveCount() == fFirstSaveLayerStackLevel){
718 fFirstSaveLayerStackLevel = kNoSaveLayer;
719 }
720}
721
722bool SkGPipeCanvas::isDrawingToLayer() const {
723 return kNoSaveLayer != fFirstSaveLayerStackLevel;
reed@google.combb6992a2011-04-26 17:41:56 +0000724}
725
726bool SkGPipeCanvas::translate(SkScalar dx, SkScalar dy) {
727 if (dx || dy) {
reed@google.comacd471f2011-05-03 21:26:46 +0000728 NOTIFY_SETUP(this);
729 if (this->needOpBytes(2 * sizeof(SkScalar))) {
730 this->writeOp(kTranslate_DrawOp);
731 fWriter.writeScalar(dx);
732 fWriter.writeScalar(dy);
733 }
reed@google.combb6992a2011-04-26 17:41:56 +0000734 }
735 return this->INHERITED::translate(dx, dy);
736}
737
738bool SkGPipeCanvas::scale(SkScalar sx, SkScalar sy) {
739 if (sx || sy) {
reed@google.comacd471f2011-05-03 21:26:46 +0000740 NOTIFY_SETUP(this);
741 if (this->needOpBytes(2 * sizeof(SkScalar))) {
742 this->writeOp(kScale_DrawOp);
743 fWriter.writeScalar(sx);
744 fWriter.writeScalar(sy);
745 }
reed@google.combb6992a2011-04-26 17:41:56 +0000746 }
747 return this->INHERITED::scale(sx, sy);
748}
749
750bool SkGPipeCanvas::rotate(SkScalar degrees) {
751 if (degrees) {
reed@google.comacd471f2011-05-03 21:26:46 +0000752 NOTIFY_SETUP(this);
753 if (this->needOpBytes(sizeof(SkScalar))) {
754 this->writeOp(kRotate_DrawOp);
755 fWriter.writeScalar(degrees);
756 }
reed@google.combb6992a2011-04-26 17:41:56 +0000757 }
758 return this->INHERITED::rotate(degrees);
759}
760
761bool SkGPipeCanvas::skew(SkScalar sx, SkScalar sy) {
762 if (sx || sy) {
reed@google.comacd471f2011-05-03 21:26:46 +0000763 NOTIFY_SETUP(this);
764 if (this->needOpBytes(2 * sizeof(SkScalar))) {
765 this->writeOp(kSkew_DrawOp);
766 fWriter.writeScalar(sx);
767 fWriter.writeScalar(sy);
768 }
reed@google.combb6992a2011-04-26 17:41:56 +0000769 }
770 return this->INHERITED::skew(sx, sy);
771}
772
773bool SkGPipeCanvas::concat(const SkMatrix& matrix) {
774 if (!matrix.isIdentity()) {
reed@google.comacd471f2011-05-03 21:26:46 +0000775 NOTIFY_SETUP(this);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000776 if (this->needOpBytes(matrix.writeToMemory(NULL))) {
reed@google.comacd471f2011-05-03 21:26:46 +0000777 this->writeOp(kConcat_DrawOp);
djsollen@google.com2b2ede32012-04-12 13:24:04 +0000778 fWriter.writeMatrix(matrix);
reed@google.comacd471f2011-05-03 21:26:46 +0000779 }
reed@google.combb6992a2011-04-26 17:41:56 +0000780 }
781 return this->INHERITED::concat(matrix);
782}
783
784void SkGPipeCanvas::setMatrix(const SkMatrix& matrix) {
reed@google.comacd471f2011-05-03 21:26:46 +0000785 NOTIFY_SETUP(this);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000786 if (this->needOpBytes(matrix.writeToMemory(NULL))) {
reed@google.comacd471f2011-05-03 21:26:46 +0000787 this->writeOp(kSetMatrix_DrawOp);
djsollen@google.com2b2ede32012-04-12 13:24:04 +0000788 fWriter.writeMatrix(matrix);
reed@google.comacd471f2011-05-03 21:26:46 +0000789 }
reed@google.combb6992a2011-04-26 17:41:56 +0000790 this->INHERITED::setMatrix(matrix);
791}
792
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000793bool SkGPipeCanvas::clipRect(const SkRect& rect, SkRegion::Op rgnOp,
794 bool doAntiAlias) {
reed@google.comacd471f2011-05-03 21:26:46 +0000795 NOTIFY_SETUP(this);
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000796 if (this->needOpBytes(sizeof(SkRect)) + sizeof(bool)) {
reed@google.comacd471f2011-05-03 21:26:46 +0000797 this->writeOp(kClipRect_DrawOp, 0, rgnOp);
798 fWriter.writeRect(rect);
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000799 fWriter.writeBool(doAntiAlias);
reed@google.comacd471f2011-05-03 21:26:46 +0000800 }
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000801 return this->INHERITED::clipRect(rect, rgnOp, doAntiAlias);
reed@google.combb6992a2011-04-26 17:41:56 +0000802}
803
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000804bool SkGPipeCanvas::clipPath(const SkPath& path, SkRegion::Op rgnOp,
805 bool doAntiAlias) {
reed@google.comacd471f2011-05-03 21:26:46 +0000806 NOTIFY_SETUP(this);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000807 if (this->needOpBytes(path.writeToMemory(NULL)) + sizeof(bool)) {
reed@google.comacd471f2011-05-03 21:26:46 +0000808 this->writeOp(kClipPath_DrawOp, 0, rgnOp);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000809 fWriter.writePath(path);
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000810 fWriter.writeBool(doAntiAlias);
reed@google.comacd471f2011-05-03 21:26:46 +0000811 }
reed@google.combb6992a2011-04-26 17:41:56 +0000812 // we just pass on the bounds of the path
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000813 return this->INHERITED::clipRect(path.getBounds(), rgnOp, doAntiAlias);
reed@google.combb6992a2011-04-26 17:41:56 +0000814}
815
816bool SkGPipeCanvas::clipRegion(const SkRegion& region, SkRegion::Op rgnOp) {
reed@google.comacd471f2011-05-03 21:26:46 +0000817 NOTIFY_SETUP(this);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000818 if (this->needOpBytes(region.writeToMemory(NULL))) {
reed@google.comacd471f2011-05-03 21:26:46 +0000819 this->writeOp(kClipRegion_DrawOp, 0, rgnOp);
djsollen@google.com2b2ede32012-04-12 13:24:04 +0000820 fWriter.writeRegion(region);
reed@google.comacd471f2011-05-03 21:26:46 +0000821 }
reed@google.combb6992a2011-04-26 17:41:56 +0000822 return this->INHERITED::clipRegion(region, rgnOp);
823}
824
825///////////////////////////////////////////////////////////////////////////////
826
827void SkGPipeCanvas::clear(SkColor color) {
reed@google.comacd471f2011-05-03 21:26:46 +0000828 NOTIFY_SETUP(this);
reed@google.combb6992a2011-04-26 17:41:56 +0000829 unsigned flags = 0;
830 if (color) {
831 flags |= kClear_HasColor_DrawOpFlag;
832 }
reed@google.comacd471f2011-05-03 21:26:46 +0000833 if (this->needOpBytes(sizeof(SkColor))) {
834 this->writeOp(kDrawClear_DrawOp, flags, 0);
835 if (color) {
836 fWriter.write32(color);
837 }
reed@google.combb6992a2011-04-26 17:41:56 +0000838 }
839}
840
841void SkGPipeCanvas::drawPaint(const SkPaint& paint) {
reed@google.comacd471f2011-05-03 21:26:46 +0000842 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000843 this->writePaint(paint);
reed@google.comacd471f2011-05-03 21:26:46 +0000844 if (this->needOpBytes()) {
reed@google.com31891582011-05-12 03:03:56 +0000845 this->writeOp(kDrawPaint_DrawOp);
reed@google.comacd471f2011-05-03 21:26:46 +0000846 }
reed@google.combb6992a2011-04-26 17:41:56 +0000847}
848
849void SkGPipeCanvas::drawPoints(PointMode mode, size_t count,
850 const SkPoint pts[], const SkPaint& paint) {
851 if (count) {
reed@google.comacd471f2011-05-03 21:26:46 +0000852 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000853 this->writePaint(paint);
reed@google.comacd471f2011-05-03 21:26:46 +0000854 if (this->needOpBytes(4 + count * sizeof(SkPoint))) {
reed@google.com31891582011-05-12 03:03:56 +0000855 this->writeOp(kDrawPoints_DrawOp, mode, 0);
reed@google.comacd471f2011-05-03 21:26:46 +0000856 fWriter.write32(count);
857 fWriter.write(pts, count * sizeof(SkPoint));
858 }
reed@google.combb6992a2011-04-26 17:41:56 +0000859 }
860}
861
862void SkGPipeCanvas::drawRect(const SkRect& rect, const SkPaint& paint) {
reed@google.comacd471f2011-05-03 21:26:46 +0000863 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000864 this->writePaint(paint);
reed@google.comacd471f2011-05-03 21:26:46 +0000865 if (this->needOpBytes(sizeof(SkRect))) {
reed@google.com31891582011-05-12 03:03:56 +0000866 this->writeOp(kDrawRect_DrawOp);
reed@google.comacd471f2011-05-03 21:26:46 +0000867 fWriter.writeRect(rect);
868 }
reed@google.combb6992a2011-04-26 17:41:56 +0000869}
870
871void SkGPipeCanvas::drawPath(const SkPath& path, const SkPaint& paint) {
reed@google.comacd471f2011-05-03 21:26:46 +0000872 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000873 this->writePaint(paint);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000874 if (this->needOpBytes(path.writeToMemory(NULL))) {
reed@google.com31891582011-05-12 03:03:56 +0000875 this->writeOp(kDrawPath_DrawOp);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000876 fWriter.writePath(path);
reed@google.comacd471f2011-05-03 21:26:46 +0000877 }
reed@google.combb6992a2011-04-26 17:41:56 +0000878}
879
scroggo@google.com58be6822012-07-30 14:40:01 +0000880bool SkGPipeCanvas::commonDrawBitmapFlatten(const SkBitmap& bm, DrawOps op,
881 unsigned flags,
882 size_t opBytesNeeded,
883 const SkPaint* paint) {
884 if (paint != NULL) {
885 flags |= kDrawBitmap_HasPaint_DrawOpsFlag;
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000886 this->writePaint(*paint);
887 }
scroggo@google.com58be6822012-07-30 14:40:01 +0000888 int bitmapIndex = this->flattenToIndex(bm);
scroggo@google.com565254b2012-06-28 15:41:32 +0000889 if (this->needOpBytes(opBytesNeeded)) {
scroggo@google.com58be6822012-07-30 14:40:01 +0000890 this->writeOp(op, flags, bitmapIndex);
891 return true;
892 }
893 return false;
894}
895
896bool SkGPipeCanvas::commonDrawBitmapHeap(const SkBitmap& bm, DrawOps op,
897 unsigned flags,
898 size_t opBytesNeeded,
899 const SkPaint* paint) {
900 const void* ptr = fSharedHeap.addBitmap(bm);
901 if (NULL == ptr) {
902 return false;
903 }
904 if (paint != NULL) {
905 flags |= kDrawBitmap_HasPaint_DrawOpsFlag;
906 this->writePaint(*paint);
907 }
908 if (this->needOpBytes(opBytesNeeded + sizeof(void*))) {
909 this->writeOp(op, flags, 0);
910 fWriter.writePtr(const_cast<void*>(ptr));
911 return true;
912 }
913 return false;
914}
915
916void SkGPipeCanvas::drawBitmap(const SkBitmap& bm, SkScalar left, SkScalar top,
917 const SkPaint* paint) {
918 NOTIFY_SETUP(this);
919 size_t opBytesNeeded = sizeof(SkScalar) * 2;
920
921 BitmapCommonFunction bitmapCommon = shouldFlattenBitmaps(fFlags) ?
922 &SkGPipeCanvas::commonDrawBitmapFlatten :
923 &SkGPipeCanvas::commonDrawBitmapHeap;
924
925 if ((*this.*bitmapCommon)(bm, kDrawBitmap_DrawOp, 0, opBytesNeeded, paint)) {
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000926 fWriter.writeScalar(left);
927 fWriter.writeScalar(top);
928 }
reed@google.combb6992a2011-04-26 17:41:56 +0000929}
930
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000931void SkGPipeCanvas::drawBitmapRect(const SkBitmap& bm, const SkIRect* src,
scroggo@google.com58be6822012-07-30 14:40:01 +0000932 const SkRect& dst, const SkPaint* paint) {
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000933 NOTIFY_SETUP(this);
scroggo@google.com58be6822012-07-30 14:40:01 +0000934 size_t opBytesNeeded = sizeof(SkRect);
935 bool hasSrc = src != NULL;
936 unsigned flags;
937 if (hasSrc) {
938 flags = kDrawBitmap_HasSrcRect_DrawOpsFlag;
939 opBytesNeeded += sizeof(int32_t) * 4;
940 } else {
941 flags = 0;
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000942 }
943
scroggo@google.com58be6822012-07-30 14:40:01 +0000944 BitmapCommonFunction bitmapCommon = shouldFlattenBitmaps(fFlags) ?
945 &SkGPipeCanvas::commonDrawBitmapFlatten :
946 &SkGPipeCanvas::commonDrawBitmapHeap;
947
948 if ((*this.*bitmapCommon)(bm, kDrawBitmapRect_DrawOp, flags, opBytesNeeded, paint)) {
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000949 if (hasSrc) {
950 fWriter.write32(src->fLeft);
951 fWriter.write32(src->fTop);
952 fWriter.write32(src->fRight);
953 fWriter.write32(src->fBottom);
954 }
955 fWriter.writeRect(dst);
956 }
reed@google.combb6992a2011-04-26 17:41:56 +0000957}
958
scroggo@google.com58be6822012-07-30 14:40:01 +0000959void SkGPipeCanvas::drawBitmapMatrix(const SkBitmap& bm, const SkMatrix& matrix,
960 const SkPaint* paint) {
961 NOTIFY_SETUP(this);
962 size_t opBytesNeeded = matrix.writeToMemory(NULL);
963
964 BitmapCommonFunction bitmapCommon = shouldFlattenBitmaps(fFlags) ?
965 &SkGPipeCanvas::commonDrawBitmapFlatten :
966 &SkGPipeCanvas::commonDrawBitmapHeap;
967
968 if ((*this.*bitmapCommon)(bm, kDrawBitmapMatrix_DrawOp, 0, opBytesNeeded, paint)) {
969 fWriter.writeMatrix(matrix);
970 }
reed@google.combb6992a2011-04-26 17:41:56 +0000971}
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000972
973void SkGPipeCanvas::drawBitmapNine(const SkBitmap& bm, const SkIRect& center,
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000974 const SkRect& dst, const SkPaint* paint) {
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000975 NOTIFY_SETUP(this);
scroggo@google.com58be6822012-07-30 14:40:01 +0000976 size_t opBytesNeeded = sizeof(int32_t) * 4 + sizeof(SkRect);
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000977
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, kDrawBitmapNine_DrawOp, 0, opBytesNeeded, paint)) {
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000983 fWriter.write32(center.fLeft);
984 fWriter.write32(center.fTop);
985 fWriter.write32(center.fRight);
986 fWriter.write32(center.fBottom);
987 fWriter.writeRect(dst);
988 }
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000989}
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000990
991void SkGPipeCanvas::drawSprite(const SkBitmap& bm, int left, int top,
992 const SkPaint* paint) {
993 NOTIFY_SETUP(this);
scroggo@google.com58be6822012-07-30 14:40:01 +0000994 size_t opBytesNeeded = sizeof(int32_t) * 2;
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000995
scroggo@google.com58be6822012-07-30 14:40:01 +0000996 BitmapCommonFunction bitmapCommon = shouldFlattenBitmaps(fFlags) ?
997 &SkGPipeCanvas::commonDrawBitmapFlatten :
998 &SkGPipeCanvas::commonDrawBitmapHeap;
999
1000 if ((*this.*bitmapCommon)(bm, kDrawSprite_DrawOp, 0, opBytesNeeded, paint)) {
scroggo@google.com16d1d0b2012-05-02 19:09:40 +00001001 fWriter.write32(left);
1002 fWriter.write32(top);
1003 }
reed@google.combb6992a2011-04-26 17:41:56 +00001004}
1005
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001006void SkGPipeCanvas::drawText(const void* text, size_t byteLength, SkScalar x,
reed@google.combb6992a2011-04-26 17:41:56 +00001007 SkScalar y, const SkPaint& paint) {
1008 if (byteLength) {
reed@google.comacd471f2011-05-03 21:26:46 +00001009 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +00001010 this->writePaint(paint);
reed@google.comacd471f2011-05-03 21:26:46 +00001011 if (this->needOpBytes(4 + SkAlign4(byteLength) + 2 * sizeof(SkScalar))) {
reed@google.com31891582011-05-12 03:03:56 +00001012 this->writeOp(kDrawText_DrawOp);
reed@google.comacd471f2011-05-03 21:26:46 +00001013 fWriter.write32(byteLength);
1014 fWriter.writePad(text, byteLength);
1015 fWriter.writeScalar(x);
1016 fWriter.writeScalar(y);
1017 }
reed@google.combb6992a2011-04-26 17:41:56 +00001018 }
1019}
1020
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001021void SkGPipeCanvas::drawPosText(const void* text, size_t byteLength,
reed@google.combb6992a2011-04-26 17:41:56 +00001022 const SkPoint pos[], const SkPaint& paint) {
1023 if (byteLength) {
reed@google.comacd471f2011-05-03 21:26:46 +00001024 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +00001025 this->writePaint(paint);
reed@google.combb6992a2011-04-26 17:41:56 +00001026 int count = paint.textToGlyphs(text, byteLength, NULL);
reed@google.comacd471f2011-05-03 21:26:46 +00001027 if (this->needOpBytes(4 + SkAlign4(byteLength) + 4 + count * sizeof(SkPoint))) {
reed@google.com31891582011-05-12 03:03:56 +00001028 this->writeOp(kDrawPosText_DrawOp);
reed@google.comacd471f2011-05-03 21:26:46 +00001029 fWriter.write32(byteLength);
1030 fWriter.writePad(text, byteLength);
1031 fWriter.write32(count);
1032 fWriter.write(pos, count * sizeof(SkPoint));
1033 }
reed@google.combb6992a2011-04-26 17:41:56 +00001034 }
1035}
1036
1037void SkGPipeCanvas::drawPosTextH(const void* text, size_t byteLength,
1038 const SkScalar xpos[], SkScalar constY,
1039 const SkPaint& paint) {
1040 if (byteLength) {
reed@google.comacd471f2011-05-03 21:26:46 +00001041 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +00001042 this->writePaint(paint);
reed@google.combb6992a2011-04-26 17:41:56 +00001043 int count = paint.textToGlyphs(text, byteLength, NULL);
reed@google.comacd471f2011-05-03 21:26:46 +00001044 if (this->needOpBytes(4 + SkAlign4(byteLength) + 4 + count * sizeof(SkScalar) + 4)) {
reed@google.com31891582011-05-12 03:03:56 +00001045 this->writeOp(kDrawPosTextH_DrawOp);
reed@google.comacd471f2011-05-03 21:26:46 +00001046 fWriter.write32(byteLength);
1047 fWriter.writePad(text, byteLength);
1048 fWriter.write32(count);
1049 fWriter.write(xpos, count * sizeof(SkScalar));
1050 fWriter.writeScalar(constY);
1051 }
reed@google.combb6992a2011-04-26 17:41:56 +00001052 }
1053}
1054
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001055void SkGPipeCanvas::drawTextOnPath(const void* text, size_t byteLength,
1056 const SkPath& path, const SkMatrix* matrix,
reed@google.combb6992a2011-04-26 17:41:56 +00001057 const SkPaint& paint) {
1058 if (byteLength) {
reed@google.comacd471f2011-05-03 21:26:46 +00001059 NOTIFY_SETUP(this);
reed@google.combb6992a2011-04-26 17:41:56 +00001060 unsigned flags = 0;
djsollen@google.com94e75ee2012-06-08 18:30:46 +00001061 size_t size = 4 + SkAlign4(byteLength) + path.writeToMemory(NULL);
reed@google.combb6992a2011-04-26 17:41:56 +00001062 if (matrix) {
1063 flags |= kDrawTextOnPath_HasMatrix_DrawOpFlag;
djsollen@google.com94e75ee2012-06-08 18:30:46 +00001064 size += matrix->writeToMemory(NULL);
reed@google.combb6992a2011-04-26 17:41:56 +00001065 }
reed@google.com31891582011-05-12 03:03:56 +00001066 this->writePaint(paint);
reed@google.comacd471f2011-05-03 21:26:46 +00001067 if (this->needOpBytes(size)) {
reed@google.com31891582011-05-12 03:03:56 +00001068 this->writeOp(kDrawTextOnPath_DrawOp, flags, 0);
reed@google.combb6992a2011-04-26 17:41:56 +00001069
reed@google.comacd471f2011-05-03 21:26:46 +00001070 fWriter.write32(byteLength);
1071 fWriter.writePad(text, byteLength);
reed@google.combb6992a2011-04-26 17:41:56 +00001072
djsollen@google.com94e75ee2012-06-08 18:30:46 +00001073 fWriter.writePath(path);
reed@google.comacd471f2011-05-03 21:26:46 +00001074 if (matrix) {
djsollen@google.com2b2ede32012-04-12 13:24:04 +00001075 fWriter.writeMatrix(*matrix);
reed@google.comacd471f2011-05-03 21:26:46 +00001076 }
reed@google.combb6992a2011-04-26 17:41:56 +00001077 }
1078 }
1079}
1080
1081void SkGPipeCanvas::drawPicture(SkPicture& picture) {
reed@google.com0faac1e2011-05-11 05:58:58 +00001082 // we want to playback the picture into individual draw calls
1083 this->INHERITED::drawPicture(picture);
reed@google.combb6992a2011-04-26 17:41:56 +00001084}
1085
reed@google.combb6992a2011-04-26 17:41:56 +00001086void SkGPipeCanvas::drawVertices(VertexMode mode, int vertexCount,
1087 const SkPoint vertices[], const SkPoint texs[],
1088 const SkColor colors[], SkXfermode*,
1089 const uint16_t indices[], int indexCount,
1090 const SkPaint& paint) {
1091 if (0 == vertexCount) {
1092 return;
1093 }
1094
reed@google.comacd471f2011-05-03 21:26:46 +00001095 NOTIFY_SETUP(this);
1096 size_t size = 4 + vertexCount * sizeof(SkPoint);
reed@google.com31891582011-05-12 03:03:56 +00001097 this->writePaint(paint);
reed@google.combb6992a2011-04-26 17:41:56 +00001098 unsigned flags = 0;
1099 if (texs) {
1100 flags |= kDrawVertices_HasTexs_DrawOpFlag;
reed@google.comacd471f2011-05-03 21:26:46 +00001101 size += vertexCount * sizeof(SkPoint);
reed@google.combb6992a2011-04-26 17:41:56 +00001102 }
1103 if (colors) {
1104 flags |= kDrawVertices_HasColors_DrawOpFlag;
reed@google.comacd471f2011-05-03 21:26:46 +00001105 size += vertexCount * sizeof(SkColor);
reed@google.combb6992a2011-04-26 17:41:56 +00001106 }
1107 if (indices && indexCount > 0) {
1108 flags |= kDrawVertices_HasIndices_DrawOpFlag;
reed@google.comacd471f2011-05-03 21:26:46 +00001109 size += 4 + SkAlign4(indexCount * sizeof(uint16_t));
reed@google.combb6992a2011-04-26 17:41:56 +00001110 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001111
reed@google.comacd471f2011-05-03 21:26:46 +00001112 if (this->needOpBytes(size)) {
reed@google.com31891582011-05-12 03:03:56 +00001113 this->writeOp(kDrawVertices_DrawOp, flags, 0);
reed@google.comacd471f2011-05-03 21:26:46 +00001114 fWriter.write32(mode);
1115 fWriter.write32(vertexCount);
1116 fWriter.write(vertices, vertexCount * sizeof(SkPoint));
1117 if (texs) {
1118 fWriter.write(texs, vertexCount * sizeof(SkPoint));
1119 }
1120 if (colors) {
1121 fWriter.write(colors, vertexCount * sizeof(SkColor));
1122 }
reed@google.combb6992a2011-04-26 17:41:56 +00001123
reed@google.comacd471f2011-05-03 21:26:46 +00001124 // TODO: flatten xfermode
reed@google.combb6992a2011-04-26 17:41:56 +00001125
reed@google.comacd471f2011-05-03 21:26:46 +00001126 if (indices && indexCount > 0) {
1127 fWriter.write32(indexCount);
1128 fWriter.writePad(indices, indexCount * sizeof(uint16_t));
1129 }
reed@google.combb6992a2011-04-26 17:41:56 +00001130 }
1131}
1132
reed@google.comacd471f2011-05-03 21:26:46 +00001133void SkGPipeCanvas::drawData(const void* ptr, size_t size) {
1134 if (size && ptr) {
1135 NOTIFY_SETUP(this);
reed@google.combb6992a2011-04-26 17:41:56 +00001136 unsigned data = 0;
1137 if (size < (1 << DRAWOPS_DATA_BITS)) {
1138 data = (unsigned)size;
1139 }
reed@google.comacd471f2011-05-03 21:26:46 +00001140 if (this->needOpBytes(4 + SkAlign4(size))) {
1141 this->writeOp(kDrawData_DrawOp, 0, data);
1142 if (0 == data) {
1143 fWriter.write32(size);
1144 }
reed@google.combb6793b2011-05-05 15:18:15 +00001145 fWriter.writePad(ptr, size);
reed@google.combb6992a2011-04-26 17:41:56 +00001146 }
1147 }
1148}
1149
junov@chromium.org77eec242012-07-18 17:54:45 +00001150void SkGPipeCanvas::flushRecording(bool detachCurrentBlock) {
1151 doNotify();
1152 if (detachCurrentBlock) {
1153 // force a new block to be requested for the next recorded command
1154 fBlockSize = 0;
1155 }
1156}
1157
reed@google.combb6992a2011-04-26 17:41:56 +00001158///////////////////////////////////////////////////////////////////////////////
1159
1160template <typename T> uint32_t castToU32(T value) {
1161 union {
1162 T fSrc;
1163 uint32_t fDst;
1164 } data;
1165 data.fSrc = value;
1166 return data.fDst;
1167}
1168
reed@google.com31891582011-05-12 03:03:56 +00001169void SkGPipeCanvas::writePaint(const SkPaint& paint) {
1170 SkPaint& base = fPaint;
reed@google.combb6992a2011-04-26 17:41:56 +00001171 uint32_t storage[32];
1172 uint32_t* ptr = storage;
reed@google.combb6992a2011-04-26 17:41:56 +00001173
1174 if (base.getFlags() != paint.getFlags()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001175 *ptr++ = PaintOp_packOpData(kFlags_PaintOp, paint.getFlags());
reed@google.comf5842f72011-05-04 18:30:04 +00001176 base.setFlags(paint.getFlags());
reed@google.combb6992a2011-04-26 17:41:56 +00001177 }
1178 if (base.getColor() != paint.getColor()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001179 *ptr++ = PaintOp_packOp(kColor_PaintOp);
1180 *ptr++ = paint.getColor();
reed@google.comf5842f72011-05-04 18:30:04 +00001181 base.setColor(paint.getColor());
reed@google.combb6992a2011-04-26 17:41:56 +00001182 }
1183 if (base.getStyle() != paint.getStyle()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001184 *ptr++ = PaintOp_packOpData(kStyle_PaintOp, paint.getStyle());
reed@google.comf5842f72011-05-04 18:30:04 +00001185 base.setStyle(paint.getStyle());
reed@google.combb6992a2011-04-26 17:41:56 +00001186 }
1187 if (base.getStrokeJoin() != paint.getStrokeJoin()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001188 *ptr++ = PaintOp_packOpData(kJoin_PaintOp, paint.getStrokeJoin());
reed@google.comf5842f72011-05-04 18:30:04 +00001189 base.setStrokeJoin(paint.getStrokeJoin());
reed@google.combb6992a2011-04-26 17:41:56 +00001190 }
1191 if (base.getStrokeCap() != paint.getStrokeCap()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001192 *ptr++ = PaintOp_packOpData(kCap_PaintOp, paint.getStrokeCap());
reed@google.comf5842f72011-05-04 18:30:04 +00001193 base.setStrokeCap(paint.getStrokeCap());
reed@google.combb6992a2011-04-26 17:41:56 +00001194 }
1195 if (base.getStrokeWidth() != paint.getStrokeWidth()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001196 *ptr++ = PaintOp_packOp(kWidth_PaintOp);
1197 *ptr++ = castToU32(paint.getStrokeWidth());
reed@google.comf5842f72011-05-04 18:30:04 +00001198 base.setStrokeWidth(paint.getStrokeWidth());
reed@google.combb6992a2011-04-26 17:41:56 +00001199 }
1200 if (base.getStrokeMiter() != paint.getStrokeMiter()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001201 *ptr++ = PaintOp_packOp(kMiter_PaintOp);
1202 *ptr++ = castToU32(paint.getStrokeMiter());
reed@google.comf5842f72011-05-04 18:30:04 +00001203 base.setStrokeMiter(paint.getStrokeMiter());
reed@google.combb6992a2011-04-26 17:41:56 +00001204 }
1205 if (base.getTextEncoding() != paint.getTextEncoding()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001206 *ptr++ = PaintOp_packOpData(kEncoding_PaintOp, paint.getTextEncoding());
reed@google.comf5842f72011-05-04 18:30:04 +00001207 base.setTextEncoding(paint.getTextEncoding());
reed@google.combb6992a2011-04-26 17:41:56 +00001208 }
1209 if (base.getHinting() != paint.getHinting()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001210 *ptr++ = PaintOp_packOpData(kHinting_PaintOp, paint.getHinting());
reed@google.comf5842f72011-05-04 18:30:04 +00001211 base.setHinting(paint.getHinting());
reed@google.combb6992a2011-04-26 17:41:56 +00001212 }
1213 if (base.getTextAlign() != paint.getTextAlign()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001214 *ptr++ = PaintOp_packOpData(kAlign_PaintOp, paint.getTextAlign());
reed@google.comf5842f72011-05-04 18:30:04 +00001215 base.setTextAlign(paint.getTextAlign());
reed@google.combb6992a2011-04-26 17:41:56 +00001216 }
1217 if (base.getTextSize() != paint.getTextSize()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001218 *ptr++ = PaintOp_packOp(kTextSize_PaintOp);
1219 *ptr++ = castToU32(paint.getTextSize());
reed@google.comf5842f72011-05-04 18:30:04 +00001220 base.setTextSize(paint.getTextSize());
reed@google.combb6992a2011-04-26 17:41:56 +00001221 }
1222 if (base.getTextScaleX() != paint.getTextScaleX()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001223 *ptr++ = PaintOp_packOp(kTextScaleX_PaintOp);
1224 *ptr++ = castToU32(paint.getTextScaleX());
reed@google.comf5842f72011-05-04 18:30:04 +00001225 base.setTextScaleX(paint.getTextScaleX());
reed@google.combb6992a2011-04-26 17:41:56 +00001226 }
1227 if (base.getTextSkewX() != paint.getTextSkewX()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001228 *ptr++ = PaintOp_packOp(kTextSkewX_PaintOp);
1229 *ptr++ = castToU32(paint.getTextSkewX());
reed@google.comf5842f72011-05-04 18:30:04 +00001230 base.setTextSkewX(paint.getTextSkewX());
1231 }
1232
1233 if (!SkTypeface::Equal(base.getTypeface(), paint.getTypeface())) {
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +00001234 if (isCrossProcess(fFlags)) {
scroggo@google.com3cb969f2012-07-27 20:39:19 +00001235 uint32_t id = this->getTypefaceID(paint.getTypeface());
1236 *ptr++ = PaintOp_packOpData(kTypeface_PaintOp, id);
1237 } else if (this->needOpBytes(sizeof(void*))) {
1238 // Add to the set for ref counting.
1239 fTypefaceSet.add(paint.getTypeface());
1240 // It is safe to write the typeface to the stream before the rest
1241 // of the paint unless we ever send a kReset_PaintOp, which we
1242 // currently never do.
1243 this->writeOp(kSetTypeface_DrawOp);
1244 fWriter.writePtr(paint.getTypeface());
1245 }
reed@google.comf5842f72011-05-04 18:30:04 +00001246 base.setTypeface(paint.getTypeface());
reed@google.combb6992a2011-04-26 17:41:56 +00001247 }
reed@google.combb6992a2011-04-26 17:41:56 +00001248
scroggo@google.com4dffc592012-07-17 16:49:40 +00001249 // This is a new paint, so all old flats can be safely purged, if necessary.
1250 fFlattenableHeap.markAllFlatsSafeToDelete();
reed@google.comb55d1182011-05-11 00:42:04 +00001251 for (int i = 0; i < kCount_PaintFlats; i++) {
1252 int index = this->flattenToIndex(get_paintflat(paint, i), (PaintFlats)i);
scroggo@google.comd3ba5cc2012-07-09 16:05:53 +00001253 bool replaced = index < 0;
1254 if (replaced) {
1255 index = ~index;
1256 }
scroggo@google.com4dffc592012-07-17 16:49:40 +00001257 // Store the index of any flat that needs to be kept. 0 means no flat.
1258 if (index > 0) {
1259 fFlattenableHeap.markFlatForKeeping(index);
1260 }
1261 SkASSERT(index >= 0 && index <= fFlatDictionary.count());
scroggo@google.comd3ba5cc2012-07-09 16:05:53 +00001262 if (index != fCurrFlatIndex[i] || replaced) {
reed@google.comb55d1182011-05-11 00:42:04 +00001263 *ptr++ = PaintOp_packOpFlagData(kFlatIndex_PaintOp, i, index);
1264 fCurrFlatIndex[i] = index;
1265 }
1266 }
1267
reed@google.comacd471f2011-05-03 21:26:46 +00001268 size_t size = (char*)ptr - (char*)storage;
1269 if (size && this->needOpBytes(size)) {
reed@google.comb55d1182011-05-11 00:42:04 +00001270 this->writeOp(kPaintOp_DrawOp, 0, size);
reed@google.comb55d1182011-05-11 00:42:04 +00001271 fWriter.write(storage, size);
reed@google.combb6992a2011-04-26 17:41:56 +00001272 for (size_t i = 0; i < size/4; i++) {
reed@google.comb55d1182011-05-11 00:42:04 +00001273// SkDebugf("[%d] %08X\n", i, storage[i]);
reed@google.combb6992a2011-04-26 17:41:56 +00001274 }
1275 }
reed@google.combb6992a2011-04-26 17:41:56 +00001276}
1277
1278///////////////////////////////////////////////////////////////////////////////
1279
1280#include "SkGPipe.h"
1281
scroggo@google.com3cb969f2012-07-27 20:39:19 +00001282SkGPipeController::~SkGPipeController() {
1283 SkSafeUnref(fCanvas);
1284}
1285
1286void SkGPipeController::setCanvas(SkGPipeCanvas* canvas) {
1287 SkRefCnt_SafeAssign(fCanvas, canvas);
1288}
1289
1290///////////////////////////////////////////////////////////////////////////////
1291
1292SkGPipeWriter::SkGPipeWriter()
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +00001293: fWriter(0) {
reed@google.combb6992a2011-04-26 17:41:56 +00001294 fCanvas = NULL;
1295}
1296
1297SkGPipeWriter::~SkGPipeWriter() {
reed@google.comacd471f2011-05-03 21:26:46 +00001298 this->endRecording();
reed@google.combb6992a2011-04-26 17:41:56 +00001299}
1300
scroggo@google.com565254b2012-06-28 15:41:32 +00001301SkCanvas* SkGPipeWriter::startRecording(SkGPipeController* controller, uint32_t flags) {
reed@google.combb6992a2011-04-26 17:41:56 +00001302 if (NULL == fCanvas) {
reed@google.comacd471f2011-05-03 21:26:46 +00001303 fWriter.reset(NULL, 0);
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +00001304 fCanvas = SkNEW_ARGS(SkGPipeCanvas, (controller, &fWriter, flags));
reed@google.combb6992a2011-04-26 17:41:56 +00001305 }
scroggo@google.com3cb969f2012-07-27 20:39:19 +00001306 controller->setCanvas(fCanvas);
reed@google.combb6992a2011-04-26 17:41:56 +00001307 return fCanvas;
1308}
1309
1310void SkGPipeWriter::endRecording() {
1311 if (fCanvas) {
1312 fCanvas->finish();
1313 fCanvas->unref();
1314 fCanvas = NULL;
1315 }
1316}
1317
junov@chromium.org77eec242012-07-18 17:54:45 +00001318void SkGPipeWriter::flushRecording(bool detachCurrentBlock){
1319 fCanvas->flushRecording(detachCurrentBlock);
1320}
1321
scroggo@google.com15011ee2012-07-26 20:03:32 +00001322size_t SkGPipeWriter::storageAllocatedForRecording() {
1323 return NULL == fCanvas ? 0 : fCanvas->storageAllocatedForRecording();
1324}
1325