blob: 2ea642e30a0c7510bcba9f9a990fd6596b443528 [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 }
293private:
scroggo@google.com284bf502012-07-17 16:10:34 +0000294 void setMostRecentlyUsed(BitmapInfo* info);
295 BitmapInfo* bitmapToReplace(const SkBitmap& bm) const;
296
297 int fBitmapCount;
298 BitmapInfo* fLeastRecentlyUsed;
299 BitmapInfo* fMostRecentlyUsed;
300 const bool fCanDoShallowCopies;
301 const int fNumberOfReaders;
scroggo@google.com15011ee2012-07-26 20:03:32 +0000302 size_t fBytesAllocated;
scroggo@google.com8ae3c7f2012-06-13 17:47:52 +0000303};
304
scroggo@google.com284bf502012-07-17 16:10:34 +0000305// We just "used" info. Update our LRU accordingly
scroggo@google.com4dffc592012-07-17 16:49:40 +0000306void SharedHeap::setMostRecentlyUsed(BitmapInfo* info) {
scroggo@google.com284bf502012-07-17 16:10:34 +0000307 SkASSERT(info != NULL);
308 if (info == fMostRecentlyUsed) {
309 return;
310 }
311 // Remove info from its prior place, and make sure to cover the hole.
312 if (fLeastRecentlyUsed == info) {
313 SkASSERT(info->fMoreRecentlyUsed != NULL);
314 fLeastRecentlyUsed = info->fMoreRecentlyUsed;
315 }
316 if (info->fMoreRecentlyUsed != NULL) {
317 SkASSERT(fMostRecentlyUsed != info);
318 info->fMoreRecentlyUsed->fLessRecentlyUsed = info->fLessRecentlyUsed;
319 }
320 if (info->fLessRecentlyUsed != NULL) {
321 SkASSERT(fLeastRecentlyUsed != info);
322 info->fLessRecentlyUsed->fMoreRecentlyUsed = info->fMoreRecentlyUsed;
323 }
324 info->fMoreRecentlyUsed = NULL;
325 // Set up the head and tail pointers properly.
326 if (fMostRecentlyUsed != NULL) {
327 SkASSERT(NULL == fMostRecentlyUsed->fMoreRecentlyUsed);
328 fMostRecentlyUsed->fMoreRecentlyUsed = info;
329 info->fLessRecentlyUsed = fMostRecentlyUsed;
330 }
331 fMostRecentlyUsed = info;
332 if (NULL == fLeastRecentlyUsed) {
333 fLeastRecentlyUsed = info;
334 }
335}
336
337/**
338 * Given a new bitmap to be added to the cache, return an existing one that
339 * should be removed to make room, or NULL if there is already room.
340 */
scroggo@google.com4dffc592012-07-17 16:49:40 +0000341BitmapInfo* SharedHeap::bitmapToReplace(const SkBitmap& bm) const {
scroggo@google.com284bf502012-07-17 16:10:34 +0000342 // Arbitrarily set a limit of 5. We should test to find the best tradeoff
343 // between time and space. A lower limit means that we use less space, but
344 // it also means that we may have to insert the same bitmap into the heap
345 // multiple times (depending on the input), potentially taking more time.
346 // On the other hand, a lower limit also means searching through our stored
347 // bitmaps takes less time.
348 if (fBitmapCount > 5) {
349 BitmapInfo* iter = fLeastRecentlyUsed;
350 while (iter != NULL) {
351 if (iter->drawCount() > 0) {
352 // If the least recently used bitmap has not been drawn by some
353 // reader, then a more recently used one will not have been
354 // drawn yet either.
355 return NULL;
356 }
357 if (bm.pixelRef() != NULL
358 && bm.pixelRef() == iter->fBitmap->pixelRef()) {
359 // Do not replace a bitmap with a new one using the same
360 // pixel ref. Instead look for a different one that will
361 // potentially free up more space.
362 iter = iter->fMoreRecentlyUsed;
363 } else {
364 return iter;
365 }
366 }
367 }
368 return NULL;
369}
370
scroggo@google.com8ae3c7f2012-06-13 17:47:52 +0000371///////////////////////////////////////////////////////////////////////////////
372
reed@google.combb6992a2011-04-26 17:41:56 +0000373class SkGPipeCanvas : public SkCanvas {
374public:
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000375 SkGPipeCanvas(SkGPipeController*, SkWriter32*, uint32_t flags);
reed@google.combb6992a2011-04-26 17:41:56 +0000376 virtual ~SkGPipeCanvas();
377
378 void finish() {
379 if (!fDone) {
reed@google.comdbccc882011-07-08 18:53:39 +0000380 if (this->needOpBytes()) {
381 this->writeOp(kDone_DrawOp);
382 this->doNotify();
383 }
reed@google.combb6992a2011-04-26 17:41:56 +0000384 fDone = true;
385 }
386 }
387
junov@chromium.org77eec242012-07-18 17:54:45 +0000388 void flushRecording(bool detachCurrentBlock);
389
scroggo@google.com15011ee2012-07-26 20:03:32 +0000390 size_t storageAllocatedForRecording() {
391 return fSharedHeap.bytesAllocated();
392 }
393
reed@google.combb6992a2011-04-26 17:41:56 +0000394 // overrides from SkCanvas
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000395 virtual int save(SaveFlags) SK_OVERRIDE;
396 virtual int saveLayer(const SkRect* bounds, const SkPaint*,
397 SaveFlags) SK_OVERRIDE;
398 virtual void restore() SK_OVERRIDE;
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000399 virtual bool isDrawingToLayer() const SK_OVERRIDE;
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000400 virtual bool translate(SkScalar dx, SkScalar dy) SK_OVERRIDE;
401 virtual bool scale(SkScalar sx, SkScalar sy) SK_OVERRIDE;
402 virtual bool rotate(SkScalar degrees) SK_OVERRIDE;
403 virtual bool skew(SkScalar sx, SkScalar sy) SK_OVERRIDE;
404 virtual bool concat(const SkMatrix& matrix) SK_OVERRIDE;
405 virtual void setMatrix(const SkMatrix& matrix) SK_OVERRIDE;
406 virtual bool clipRect(const SkRect& rect, SkRegion::Op op,
407 bool doAntiAlias = false) SK_OVERRIDE;
408 virtual bool clipPath(const SkPath& path, SkRegion::Op op,
409 bool doAntiAlias = false) SK_OVERRIDE;
410 virtual bool clipRegion(const SkRegion& region, SkRegion::Op op) SK_OVERRIDE;
411 virtual void clear(SkColor) SK_OVERRIDE;
412 virtual void drawPaint(const SkPaint& paint) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000413 virtual void drawPoints(PointMode, size_t count, const SkPoint pts[],
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000414 const SkPaint&) SK_OVERRIDE;
415 virtual void drawRect(const SkRect& rect, const SkPaint&) SK_OVERRIDE;
416 virtual void drawPath(const SkPath& path, const SkPaint&) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000417 virtual void drawBitmap(const SkBitmap&, SkScalar left, SkScalar top,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000418 const SkPaint*) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000419 virtual void drawBitmapRect(const SkBitmap&, const SkIRect* src,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000420 const SkRect& dst, const SkPaint*) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000421 virtual void drawBitmapMatrix(const SkBitmap&, const SkMatrix&,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000422 const SkPaint*) SK_OVERRIDE;
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000423 virtual void drawBitmapNine(const SkBitmap& bitmap, const SkIRect& center,
424 const SkRect& dst, const SkPaint* paint = NULL) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000425 virtual void drawSprite(const SkBitmap&, int left, int top,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000426 const SkPaint*) SK_OVERRIDE;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000427 virtual void drawText(const void* text, size_t byteLength, SkScalar x,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000428 SkScalar y, const SkPaint&) SK_OVERRIDE;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000429 virtual void drawPosText(const void* text, size_t byteLength,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000430 const SkPoint pos[], const SkPaint&) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000431 virtual void drawPosTextH(const void* text, size_t byteLength,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000432 const SkScalar xpos[], SkScalar constY,
433 const SkPaint&) SK_OVERRIDE;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000434 virtual void drawTextOnPath(const void* text, size_t byteLength,
435 const SkPath& path, const SkMatrix* matrix,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000436 const SkPaint&) SK_OVERRIDE;
437 virtual void drawPicture(SkPicture& picture) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000438 virtual void drawVertices(VertexMode, int vertexCount,
439 const SkPoint vertices[], const SkPoint texs[],
440 const SkColor colors[], SkXfermode*,
441 const uint16_t indices[], int indexCount,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000442 const SkPaint&) SK_OVERRIDE;
443 virtual void drawData(const void*, size_t) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000444
445private:
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000446 enum {
447 kNoSaveLayer = -1,
448 };
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000449 SkNamedFactorySet* fFactorySet;
450 int fFirstSaveLayerStackLevel;
451 SharedHeap fSharedHeap;
reed@google.comacd471f2011-05-03 21:26:46 +0000452 SkGPipeController* fController;
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000453 SkWriter32& fWriter;
454 size_t fBlockSize; // amount allocated for writer
455 size_t fBytesNotified;
456 bool fDone;
457 uint32_t fFlags;
reed@google.combb6992a2011-04-26 17:41:56 +0000458
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000459 SkRefCntSet fTypefaceSet;
reed@google.comf5842f72011-05-04 18:30:04 +0000460
461 uint32_t getTypefaceID(SkTypeface*);
462
reed@google.comacd471f2011-05-03 21:26:46 +0000463 inline void writeOp(DrawOps op, unsigned flags, unsigned data) {
reed@google.combb6992a2011-04-26 17:41:56 +0000464 fWriter.write32(DrawOp_packOpFlagData(op, flags, data));
465 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000466
reed@google.comacd471f2011-05-03 21:26:46 +0000467 inline void writeOp(DrawOps op) {
reed@google.combb6992a2011-04-26 17:41:56 +0000468 fWriter.write32(DrawOp_packOpFlagData(op, 0, 0));
469 }
reed@google.comacd471f2011-05-03 21:26:46 +0000470
471 bool needOpBytes(size_t size = 0);
472
473 inline void doNotify() {
474 if (!fDone) {
475 size_t bytes = fWriter.size() - fBytesNotified;
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000476 if (bytes > 0) {
477 fController->notifyWritten(bytes);
478 fBytesNotified += bytes;
479 }
reed@google.comacd471f2011-05-03 21:26:46 +0000480 }
481 }
reed@google.comb55d1182011-05-11 00:42:04 +0000482
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000483 // Should be called after any calls to an SkFlatDictionary::findAndReplace
484 // if a new SkFlatData was added when in cross process mode
485 void flattenFactoryNames();
486
scroggo@google.com4dffc592012-07-17 16:49:40 +0000487 // These are only used when in cross process, but with no shared address
488 // space, so bitmaps are flattened.
489 FlattenableHeap fBitmapHeap;
490 SkBitmapDictionary fBitmapDictionary;
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000491 int flattenToIndex(const SkBitmap&);
492
scroggo@google.com4dffc592012-07-17 16:49:40 +0000493 FlattenableHeap fFlattenableHeap;
494 FlatDictionary fFlatDictionary;
reed@google.comb55d1182011-05-11 00:42:04 +0000495 int fCurrFlatIndex[kCount_PaintFlats];
496 int flattenToIndex(SkFlattenable* obj, PaintFlats);
497
scroggo@google.com58be6822012-07-30 14:40:01 +0000498 // Common code used by drawBitmap* when flattening.
499 bool commonDrawBitmapFlatten(const SkBitmap& bm, DrawOps op, unsigned flags,
500 size_t opBytesNeeded, const SkPaint* paint);
501 // Common code used by drawBitmap* when storing in the heap.
502 bool commonDrawBitmapHeap(const SkBitmap& bm, DrawOps op, unsigned flags,
503 size_t opBytesNeeded, const SkPaint* paint);
504 // Convenience type for function pointer
505 typedef bool (SkGPipeCanvas::*BitmapCommonFunction)(const SkBitmap&,
506 DrawOps, unsigned,
507 size_t, const SkPaint*);
508
reed@google.com31891582011-05-12 03:03:56 +0000509 SkPaint fPaint;
510 void writePaint(const SkPaint&);
reed@google.combb6992a2011-04-26 17:41:56 +0000511
reed@google.comacd471f2011-05-03 21:26:46 +0000512 class AutoPipeNotify {
513 public:
514 AutoPipeNotify(SkGPipeCanvas* canvas) : fCanvas(canvas) {}
515 ~AutoPipeNotify() { fCanvas->doNotify(); }
516 private:
517 SkGPipeCanvas* fCanvas;
518 };
519 friend class AutoPipeNotify;
520
reed@google.combb6992a2011-04-26 17:41:56 +0000521 typedef SkCanvas INHERITED;
522};
523
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000524void SkGPipeCanvas::flattenFactoryNames() {
525 const char* name;
526 while ((name = fFactorySet->getNextAddedFactoryName()) != NULL) {
527 size_t len = strlen(name);
528 if (this->needOpBytes(len)) {
529 this->writeOp(kDef_Factory_DrawOp);
530 fWriter.writeString(name, len);
531 }
532 }
533}
534
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000535int SkGPipeCanvas::flattenToIndex(const SkBitmap & bitmap) {
scroggo@google.com565254b2012-06-28 15:41:32 +0000536 SkASSERT(shouldFlattenBitmaps(fFlags));
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000537 uint32_t flags = SkFlattenableWriteBuffer::kCrossProcess_Flag;
scroggo@google.com4dffc592012-07-17 16:49:40 +0000538 bool added, replaced;
539 const SkFlatData* flat = fBitmapDictionary.findAndReplace(
540 bitmap, flags, fBitmapHeap.flatToReplace(), &added, &replaced);
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000541
scroggo@google.com4dffc592012-07-17 16:49:40 +0000542 int index = flat->index();
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000543 if (added) {
544 this->flattenFactoryNames();
545 size_t flatSize = flat->flatSize();
546 if (this->needOpBytes(flatSize)) {
547 this->writeOp(kDef_Bitmap_DrawOp, 0, index);
548 fWriter.write(flat->data(), flatSize);
549 }
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000550 }
scroggo@google.com4dffc592012-07-17 16:49:40 +0000551 return index;
scroggo@google.comd3ba5cc2012-07-09 16:05:53 +0000552}
553
reed@google.comb55d1182011-05-11 00:42:04 +0000554// return 0 for NULL (or unflattenable obj), or index-base-1
scroggo@google.comd3ba5cc2012-07-09 16:05:53 +0000555// return ~(index-base-1) if an old flattenable was replaced
reed@google.comb55d1182011-05-11 00:42:04 +0000556int SkGPipeCanvas::flattenToIndex(SkFlattenable* obj, PaintFlats paintflat) {
557 if (NULL == obj) {
558 return 0;
559 }
reed@google.comb55d1182011-05-11 00:42:04 +0000560
scroggo@google.com4dffc592012-07-17 16:49:40 +0000561 uint32_t writeBufferFlags;
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000562 if (isCrossProcess(fFlags)) {
563 writeBufferFlags = SkFlattenableWriteBuffer::kCrossProcess_Flag;
scroggo@google.com565254b2012-06-28 15:41:32 +0000564 } else {
565 // Needed for bitmap shaders.
scroggo@google.com4dffc592012-07-17 16:49:40 +0000566 writeBufferFlags = SkFlattenableWriteBuffer::kForceFlattenBitmapPixels_Flag;
scroggo@google.com565254b2012-06-28 15:41:32 +0000567 }
reed@google.comdde09562011-05-23 12:21:05 +0000568
scroggo@google.com4dffc592012-07-17 16:49:40 +0000569 bool added, replaced;
570 const SkFlatData* flat = fFlatDictionary.findAndReplace(
571 *obj, writeBufferFlags, fFlattenableHeap.flatToReplace(), &added, &replaced);
572 int index = flat->index();
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000573 if (added) {
574 if (isCrossProcess(fFlags)) {
575 this->flattenFactoryNames();
576 }
577 size_t flatSize = flat->flatSize();
578 if (this->needOpBytes(flatSize)) {
579 this->writeOp(kDef_Flattenable_DrawOp, paintflat, index);
580 fWriter.write(flat->data(), flatSize);
581 }
scroggo@google.com4dffc592012-07-17 16:49:40 +0000582 }
583 if (replaced) {
reed@google.comb55d1182011-05-11 00:42:04 +0000584 index = ~index;
reed@google.comb55d1182011-05-11 00:42:04 +0000585 }
scroggo@google.com4dffc592012-07-17 16:49:40 +0000586 return index;
reed@google.comb55d1182011-05-11 00:42:04 +0000587}
588
reed@google.combb6992a2011-04-26 17:41:56 +0000589///////////////////////////////////////////////////////////////////////////////
590
reed@google.comacd471f2011-05-03 21:26:46 +0000591#define MIN_BLOCK_SIZE (16 * 1024)
scroggo@google.com4dffc592012-07-17 16:49:40 +0000592#define BITMAPS_TO_KEEP 5
593#define FLATTENABLES_TO_KEEP 10
reed@google.combb6992a2011-04-26 17:41:56 +0000594
reed@google.comacd471f2011-05-03 21:26:46 +0000595SkGPipeCanvas::SkGPipeCanvas(SkGPipeController* controller,
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000596 SkWriter32* writer, uint32_t flags)
597: fFactorySet(isCrossProcess(flags) ? SkNEW(SkNamedFactorySet) : NULL)
598, fSharedHeap(!isCrossProcess(flags), controller->numberOfReaders())
599, fWriter(*writer)
600, fFlags(flags)
scroggo@google.com15543602012-08-02 18:49:49 +0000601, fBitmapHeap(BITMAPS_TO_KEEP, fFactorySet)
602, fBitmapDictionary(&fBitmapHeap)
603, fFlattenableHeap(FLATTENABLES_TO_KEEP, fFactorySet)
604, fFlatDictionary(&fFlattenableHeap) {
reed@google.comacd471f2011-05-03 21:26:46 +0000605 fController = controller;
reed@google.combb6992a2011-04-26 17:41:56 +0000606 fDone = false;
reed@google.comacd471f2011-05-03 21:26:46 +0000607 fBlockSize = 0; // need first block from controller
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000608 fBytesNotified = 0;
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000609 fFirstSaveLayerStackLevel = kNoSaveLayer;
reed@google.comb55d1182011-05-11 00:42:04 +0000610 sk_bzero(fCurrFlatIndex, sizeof(fCurrFlatIndex));
reed@google.comacd471f2011-05-03 21:26:46 +0000611
reed@google.combb6793b2011-05-05 15:18:15 +0000612 // we need a device to limit our clip
613 // should the caller give us the bounds?
yangsu@google.com06b4da162011-06-17 15:04:40 +0000614 // We don't allocate pixels for the bitmap
reed@google.combb6793b2011-05-05 15:18:15 +0000615 SkBitmap bitmap;
616 bitmap.setConfig(SkBitmap::kARGB_8888_Config, 32767, 32767);
yangsu@google.com06b4da162011-06-17 15:04:40 +0000617 SkDevice* device = SkNEW_ARGS(SkDevice, (bitmap));
reed@google.combb6793b2011-05-05 15:18:15 +0000618 this->setDevice(device)->unref();
scroggo@google.com565254b2012-06-28 15:41:32 +0000619 // Tell the reader the appropriate flags to use.
620 if (this->needOpBytes()) {
621 this->writeOp(kReportFlags_DrawOp, fFlags, 0);
622 }
reed@google.combb6992a2011-04-26 17:41:56 +0000623}
624
625SkGPipeCanvas::~SkGPipeCanvas() {
626 this->finish();
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +0000627 SkSafeUnref(fFactorySet);
reed@google.combb6992a2011-04-26 17:41:56 +0000628}
629
reed@google.comacd471f2011-05-03 21:26:46 +0000630bool SkGPipeCanvas::needOpBytes(size_t needed) {
631 if (fDone) {
632 return false;
633 }
634
635 needed += 4; // size of DrawOp atom
636 if (fWriter.size() + needed > fBlockSize) {
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000637 // Before we wipe out any data that has already been written, read it
638 // out.
639 this->doNotify();
640 size_t blockSize = SkMax32(MIN_BLOCK_SIZE, needed);
641 void* block = fController->requestBlock(blockSize, &fBlockSize);
reed@google.comacd471f2011-05-03 21:26:46 +0000642 if (NULL == block) {
643 fDone = true;
644 return false;
645 }
646 fWriter.reset(block, fBlockSize);
647 fBytesNotified = 0;
648 }
649 return true;
650}
651
reed@google.comf5842f72011-05-04 18:30:04 +0000652uint32_t SkGPipeCanvas::getTypefaceID(SkTypeface* face) {
653 uint32_t id = 0; // 0 means default/null typeface
654 if (face) {
655 id = fTypefaceSet.find(face);
656 if (0 == id) {
657 id = fTypefaceSet.add(face);
658 size_t size = writeTypeface(NULL, face);
659 if (this->needOpBytes(size)) {
reed@google.combb6793b2011-05-05 15:18:15 +0000660 this->writeOp(kDef_Typeface_DrawOp);
reed@google.comf5842f72011-05-04 18:30:04 +0000661 writeTypeface(&fWriter, face);
662 }
663 }
664 }
665 return id;
666}
667
reed@google.combb6992a2011-04-26 17:41:56 +0000668///////////////////////////////////////////////////////////////////////////////
669
reed@google.comacd471f2011-05-03 21:26:46 +0000670#define NOTIFY_SETUP(canvas) \
671 AutoPipeNotify apn(canvas)
672
reed@google.combb6992a2011-04-26 17:41:56 +0000673int SkGPipeCanvas::save(SaveFlags flags) {
reed@google.comacd471f2011-05-03 21:26:46 +0000674 NOTIFY_SETUP(this);
675 if (this->needOpBytes()) {
676 this->writeOp(kSave_DrawOp, 0, flags);
677 }
reed@google.combb6992a2011-04-26 17:41:56 +0000678 return this->INHERITED::save(flags);
679}
680
681int SkGPipeCanvas::saveLayer(const SkRect* bounds, const SkPaint* paint,
682 SaveFlags saveFlags) {
reed@google.comacd471f2011-05-03 21:26:46 +0000683 NOTIFY_SETUP(this);
684 size_t size = 0;
reed@google.combb6992a2011-04-26 17:41:56 +0000685 unsigned opFlags = 0;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000686
reed@google.combb6992a2011-04-26 17:41:56 +0000687 if (bounds) {
688 opFlags |= kSaveLayer_HasBounds_DrawOpFlag;
reed@google.comacd471f2011-05-03 21:26:46 +0000689 size += sizeof(SkRect);
reed@google.combb6992a2011-04-26 17:41:56 +0000690 }
691 if (paint) {
692 opFlags |= kSaveLayer_HasPaint_DrawOpFlag;
reed@google.com31891582011-05-12 03:03:56 +0000693 this->writePaint(*paint);
reed@google.combb6992a2011-04-26 17:41:56 +0000694 }
695
reed@google.comacd471f2011-05-03 21:26:46 +0000696 if (this->needOpBytes(size)) {
697 this->writeOp(kSaveLayer_DrawOp, opFlags, saveFlags);
698 if (bounds) {
699 fWriter.writeRect(*bounds);
700 }
reed@google.combb6992a2011-04-26 17:41:56 +0000701 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000702
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000703 if (kNoSaveLayer == fFirstSaveLayerStackLevel){
704 fFirstSaveLayerStackLevel = this->getSaveCount();
705 }
reed@google.combb6992a2011-04-26 17:41:56 +0000706 // we just pass on the save, so we don't create a layer
707 return this->INHERITED::save(saveFlags);
708}
709
710void SkGPipeCanvas::restore() {
reed@google.comacd471f2011-05-03 21:26:46 +0000711 NOTIFY_SETUP(this);
712 if (this->needOpBytes()) {
713 this->writeOp(kRestore_DrawOp);
714 }
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000715
reed@google.combb6992a2011-04-26 17:41:56 +0000716 this->INHERITED::restore();
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000717
718 if (this->getSaveCount() == fFirstSaveLayerStackLevel){
719 fFirstSaveLayerStackLevel = kNoSaveLayer;
720 }
721}
722
723bool SkGPipeCanvas::isDrawingToLayer() const {
724 return kNoSaveLayer != fFirstSaveLayerStackLevel;
reed@google.combb6992a2011-04-26 17:41:56 +0000725}
726
727bool SkGPipeCanvas::translate(SkScalar dx, SkScalar dy) {
728 if (dx || dy) {
reed@google.comacd471f2011-05-03 21:26:46 +0000729 NOTIFY_SETUP(this);
730 if (this->needOpBytes(2 * sizeof(SkScalar))) {
731 this->writeOp(kTranslate_DrawOp);
732 fWriter.writeScalar(dx);
733 fWriter.writeScalar(dy);
734 }
reed@google.combb6992a2011-04-26 17:41:56 +0000735 }
736 return this->INHERITED::translate(dx, dy);
737}
738
739bool SkGPipeCanvas::scale(SkScalar sx, SkScalar sy) {
740 if (sx || sy) {
reed@google.comacd471f2011-05-03 21:26:46 +0000741 NOTIFY_SETUP(this);
742 if (this->needOpBytes(2 * sizeof(SkScalar))) {
743 this->writeOp(kScale_DrawOp);
744 fWriter.writeScalar(sx);
745 fWriter.writeScalar(sy);
746 }
reed@google.combb6992a2011-04-26 17:41:56 +0000747 }
748 return this->INHERITED::scale(sx, sy);
749}
750
751bool SkGPipeCanvas::rotate(SkScalar degrees) {
752 if (degrees) {
reed@google.comacd471f2011-05-03 21:26:46 +0000753 NOTIFY_SETUP(this);
754 if (this->needOpBytes(sizeof(SkScalar))) {
755 this->writeOp(kRotate_DrawOp);
756 fWriter.writeScalar(degrees);
757 }
reed@google.combb6992a2011-04-26 17:41:56 +0000758 }
759 return this->INHERITED::rotate(degrees);
760}
761
762bool SkGPipeCanvas::skew(SkScalar sx, SkScalar sy) {
763 if (sx || sy) {
reed@google.comacd471f2011-05-03 21:26:46 +0000764 NOTIFY_SETUP(this);
765 if (this->needOpBytes(2 * sizeof(SkScalar))) {
766 this->writeOp(kSkew_DrawOp);
767 fWriter.writeScalar(sx);
768 fWriter.writeScalar(sy);
769 }
reed@google.combb6992a2011-04-26 17:41:56 +0000770 }
771 return this->INHERITED::skew(sx, sy);
772}
773
774bool SkGPipeCanvas::concat(const SkMatrix& matrix) {
775 if (!matrix.isIdentity()) {
reed@google.comacd471f2011-05-03 21:26:46 +0000776 NOTIFY_SETUP(this);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000777 if (this->needOpBytes(matrix.writeToMemory(NULL))) {
reed@google.comacd471f2011-05-03 21:26:46 +0000778 this->writeOp(kConcat_DrawOp);
djsollen@google.com2b2ede32012-04-12 13:24:04 +0000779 fWriter.writeMatrix(matrix);
reed@google.comacd471f2011-05-03 21:26:46 +0000780 }
reed@google.combb6992a2011-04-26 17:41:56 +0000781 }
782 return this->INHERITED::concat(matrix);
783}
784
785void SkGPipeCanvas::setMatrix(const SkMatrix& matrix) {
reed@google.comacd471f2011-05-03 21:26:46 +0000786 NOTIFY_SETUP(this);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000787 if (this->needOpBytes(matrix.writeToMemory(NULL))) {
reed@google.comacd471f2011-05-03 21:26:46 +0000788 this->writeOp(kSetMatrix_DrawOp);
djsollen@google.com2b2ede32012-04-12 13:24:04 +0000789 fWriter.writeMatrix(matrix);
reed@google.comacd471f2011-05-03 21:26:46 +0000790 }
reed@google.combb6992a2011-04-26 17:41:56 +0000791 this->INHERITED::setMatrix(matrix);
792}
793
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000794bool SkGPipeCanvas::clipRect(const SkRect& rect, SkRegion::Op rgnOp,
795 bool doAntiAlias) {
reed@google.comacd471f2011-05-03 21:26:46 +0000796 NOTIFY_SETUP(this);
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000797 if (this->needOpBytes(sizeof(SkRect)) + sizeof(bool)) {
reed@google.comacd471f2011-05-03 21:26:46 +0000798 this->writeOp(kClipRect_DrawOp, 0, rgnOp);
799 fWriter.writeRect(rect);
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000800 fWriter.writeBool(doAntiAlias);
reed@google.comacd471f2011-05-03 21:26:46 +0000801 }
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000802 return this->INHERITED::clipRect(rect, rgnOp, doAntiAlias);
reed@google.combb6992a2011-04-26 17:41:56 +0000803}
804
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000805bool SkGPipeCanvas::clipPath(const SkPath& path, SkRegion::Op rgnOp,
806 bool doAntiAlias) {
reed@google.comacd471f2011-05-03 21:26:46 +0000807 NOTIFY_SETUP(this);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000808 if (this->needOpBytes(path.writeToMemory(NULL)) + sizeof(bool)) {
reed@google.comacd471f2011-05-03 21:26:46 +0000809 this->writeOp(kClipPath_DrawOp, 0, rgnOp);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000810 fWriter.writePath(path);
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000811 fWriter.writeBool(doAntiAlias);
reed@google.comacd471f2011-05-03 21:26:46 +0000812 }
reed@google.combb6992a2011-04-26 17:41:56 +0000813 // we just pass on the bounds of the path
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000814 return this->INHERITED::clipRect(path.getBounds(), rgnOp, doAntiAlias);
reed@google.combb6992a2011-04-26 17:41:56 +0000815}
816
817bool SkGPipeCanvas::clipRegion(const SkRegion& region, SkRegion::Op rgnOp) {
reed@google.comacd471f2011-05-03 21:26:46 +0000818 NOTIFY_SETUP(this);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000819 if (this->needOpBytes(region.writeToMemory(NULL))) {
reed@google.comacd471f2011-05-03 21:26:46 +0000820 this->writeOp(kClipRegion_DrawOp, 0, rgnOp);
djsollen@google.com2b2ede32012-04-12 13:24:04 +0000821 fWriter.writeRegion(region);
reed@google.comacd471f2011-05-03 21:26:46 +0000822 }
reed@google.combb6992a2011-04-26 17:41:56 +0000823 return this->INHERITED::clipRegion(region, rgnOp);
824}
825
826///////////////////////////////////////////////////////////////////////////////
827
828void SkGPipeCanvas::clear(SkColor color) {
reed@google.comacd471f2011-05-03 21:26:46 +0000829 NOTIFY_SETUP(this);
reed@google.combb6992a2011-04-26 17:41:56 +0000830 unsigned flags = 0;
831 if (color) {
832 flags |= kClear_HasColor_DrawOpFlag;
833 }
reed@google.comacd471f2011-05-03 21:26:46 +0000834 if (this->needOpBytes(sizeof(SkColor))) {
835 this->writeOp(kDrawClear_DrawOp, flags, 0);
836 if (color) {
837 fWriter.write32(color);
838 }
reed@google.combb6992a2011-04-26 17:41:56 +0000839 }
840}
841
842void SkGPipeCanvas::drawPaint(const SkPaint& paint) {
reed@google.comacd471f2011-05-03 21:26:46 +0000843 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000844 this->writePaint(paint);
reed@google.comacd471f2011-05-03 21:26:46 +0000845 if (this->needOpBytes()) {
reed@google.com31891582011-05-12 03:03:56 +0000846 this->writeOp(kDrawPaint_DrawOp);
reed@google.comacd471f2011-05-03 21:26:46 +0000847 }
reed@google.combb6992a2011-04-26 17:41:56 +0000848}
849
850void SkGPipeCanvas::drawPoints(PointMode mode, size_t count,
851 const SkPoint pts[], const SkPaint& paint) {
852 if (count) {
reed@google.comacd471f2011-05-03 21:26:46 +0000853 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000854 this->writePaint(paint);
reed@google.comacd471f2011-05-03 21:26:46 +0000855 if (this->needOpBytes(4 + count * sizeof(SkPoint))) {
reed@google.com31891582011-05-12 03:03:56 +0000856 this->writeOp(kDrawPoints_DrawOp, mode, 0);
reed@google.comacd471f2011-05-03 21:26:46 +0000857 fWriter.write32(count);
858 fWriter.write(pts, count * sizeof(SkPoint));
859 }
reed@google.combb6992a2011-04-26 17:41:56 +0000860 }
861}
862
863void SkGPipeCanvas::drawRect(const SkRect& rect, const SkPaint& paint) {
reed@google.comacd471f2011-05-03 21:26:46 +0000864 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000865 this->writePaint(paint);
reed@google.comacd471f2011-05-03 21:26:46 +0000866 if (this->needOpBytes(sizeof(SkRect))) {
reed@google.com31891582011-05-12 03:03:56 +0000867 this->writeOp(kDrawRect_DrawOp);
reed@google.comacd471f2011-05-03 21:26:46 +0000868 fWriter.writeRect(rect);
869 }
reed@google.combb6992a2011-04-26 17:41:56 +0000870}
871
872void SkGPipeCanvas::drawPath(const SkPath& path, const SkPaint& paint) {
reed@google.comacd471f2011-05-03 21:26:46 +0000873 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000874 this->writePaint(paint);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000875 if (this->needOpBytes(path.writeToMemory(NULL))) {
reed@google.com31891582011-05-12 03:03:56 +0000876 this->writeOp(kDrawPath_DrawOp);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000877 fWriter.writePath(path);
reed@google.comacd471f2011-05-03 21:26:46 +0000878 }
reed@google.combb6992a2011-04-26 17:41:56 +0000879}
880
scroggo@google.com58be6822012-07-30 14:40:01 +0000881bool SkGPipeCanvas::commonDrawBitmapFlatten(const SkBitmap& bm, DrawOps op,
882 unsigned flags,
883 size_t opBytesNeeded,
884 const SkPaint* paint) {
885 if (paint != NULL) {
886 flags |= kDrawBitmap_HasPaint_DrawOpsFlag;
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000887 this->writePaint(*paint);
888 }
scroggo@google.com58be6822012-07-30 14:40:01 +0000889 int bitmapIndex = this->flattenToIndex(bm);
scroggo@google.com565254b2012-06-28 15:41:32 +0000890 if (this->needOpBytes(opBytesNeeded)) {
scroggo@google.com58be6822012-07-30 14:40:01 +0000891 this->writeOp(op, flags, bitmapIndex);
892 return true;
893 }
894 return false;
895}
896
897bool SkGPipeCanvas::commonDrawBitmapHeap(const SkBitmap& bm, DrawOps op,
898 unsigned flags,
899 size_t opBytesNeeded,
900 const SkPaint* paint) {
901 const void* ptr = fSharedHeap.addBitmap(bm);
902 if (NULL == ptr) {
903 return false;
904 }
905 if (paint != NULL) {
906 flags |= kDrawBitmap_HasPaint_DrawOpsFlag;
907 this->writePaint(*paint);
908 }
909 if (this->needOpBytes(opBytesNeeded + sizeof(void*))) {
910 this->writeOp(op, flags, 0);
911 fWriter.writePtr(const_cast<void*>(ptr));
912 return true;
913 }
914 return false;
915}
916
917void SkGPipeCanvas::drawBitmap(const SkBitmap& bm, SkScalar left, SkScalar top,
918 const SkPaint* paint) {
919 NOTIFY_SETUP(this);
920 size_t opBytesNeeded = sizeof(SkScalar) * 2;
921
922 BitmapCommonFunction bitmapCommon = shouldFlattenBitmaps(fFlags) ?
923 &SkGPipeCanvas::commonDrawBitmapFlatten :
924 &SkGPipeCanvas::commonDrawBitmapHeap;
925
926 if ((*this.*bitmapCommon)(bm, kDrawBitmap_DrawOp, 0, opBytesNeeded, paint)) {
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000927 fWriter.writeScalar(left);
928 fWriter.writeScalar(top);
929 }
reed@google.combb6992a2011-04-26 17:41:56 +0000930}
931
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000932void SkGPipeCanvas::drawBitmapRect(const SkBitmap& bm, const SkIRect* src,
scroggo@google.com58be6822012-07-30 14:40:01 +0000933 const SkRect& dst, const SkPaint* paint) {
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000934 NOTIFY_SETUP(this);
scroggo@google.com58be6822012-07-30 14:40:01 +0000935 size_t opBytesNeeded = sizeof(SkRect);
936 bool hasSrc = src != NULL;
937 unsigned flags;
938 if (hasSrc) {
939 flags = kDrawBitmap_HasSrcRect_DrawOpsFlag;
940 opBytesNeeded += sizeof(int32_t) * 4;
941 } else {
942 flags = 0;
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000943 }
944
scroggo@google.com58be6822012-07-30 14:40:01 +0000945 BitmapCommonFunction bitmapCommon = shouldFlattenBitmaps(fFlags) ?
946 &SkGPipeCanvas::commonDrawBitmapFlatten :
947 &SkGPipeCanvas::commonDrawBitmapHeap;
948
949 if ((*this.*bitmapCommon)(bm, kDrawBitmapRect_DrawOp, flags, opBytesNeeded, paint)) {
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000950 if (hasSrc) {
951 fWriter.write32(src->fLeft);
952 fWriter.write32(src->fTop);
953 fWriter.write32(src->fRight);
954 fWriter.write32(src->fBottom);
955 }
956 fWriter.writeRect(dst);
957 }
reed@google.combb6992a2011-04-26 17:41:56 +0000958}
959
scroggo@google.com58be6822012-07-30 14:40:01 +0000960void SkGPipeCanvas::drawBitmapMatrix(const SkBitmap& bm, const SkMatrix& matrix,
961 const SkPaint* paint) {
962 NOTIFY_SETUP(this);
963 size_t opBytesNeeded = matrix.writeToMemory(NULL);
964
965 BitmapCommonFunction bitmapCommon = shouldFlattenBitmaps(fFlags) ?
966 &SkGPipeCanvas::commonDrawBitmapFlatten :
967 &SkGPipeCanvas::commonDrawBitmapHeap;
968
969 if ((*this.*bitmapCommon)(bm, kDrawBitmapMatrix_DrawOp, 0, opBytesNeeded, paint)) {
970 fWriter.writeMatrix(matrix);
971 }
reed@google.combb6992a2011-04-26 17:41:56 +0000972}
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000973
974void SkGPipeCanvas::drawBitmapNine(const SkBitmap& bm, const SkIRect& center,
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000975 const SkRect& dst, const SkPaint* paint) {
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000976 NOTIFY_SETUP(this);
scroggo@google.com58be6822012-07-30 14:40:01 +0000977 size_t opBytesNeeded = sizeof(int32_t) * 4 + sizeof(SkRect);
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000978
scroggo@google.com58be6822012-07-30 14:40:01 +0000979 BitmapCommonFunction bitmapCommon = shouldFlattenBitmaps(fFlags) ?
980 &SkGPipeCanvas::commonDrawBitmapFlatten :
981 &SkGPipeCanvas::commonDrawBitmapHeap;
982
983 if ((*this.*bitmapCommon)(bm, kDrawBitmapNine_DrawOp, 0, opBytesNeeded, paint)) {
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000984 fWriter.write32(center.fLeft);
985 fWriter.write32(center.fTop);
986 fWriter.write32(center.fRight);
987 fWriter.write32(center.fBottom);
988 fWriter.writeRect(dst);
989 }
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000990}
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000991
992void SkGPipeCanvas::drawSprite(const SkBitmap& bm, int left, int top,
993 const SkPaint* paint) {
994 NOTIFY_SETUP(this);
scroggo@google.com58be6822012-07-30 14:40:01 +0000995 size_t opBytesNeeded = sizeof(int32_t) * 2;
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000996
scroggo@google.com58be6822012-07-30 14:40:01 +0000997 BitmapCommonFunction bitmapCommon = shouldFlattenBitmaps(fFlags) ?
998 &SkGPipeCanvas::commonDrawBitmapFlatten :
999 &SkGPipeCanvas::commonDrawBitmapHeap;
1000
1001 if ((*this.*bitmapCommon)(bm, kDrawSprite_DrawOp, 0, opBytesNeeded, paint)) {
scroggo@google.com16d1d0b2012-05-02 19:09:40 +00001002 fWriter.write32(left);
1003 fWriter.write32(top);
1004 }
reed@google.combb6992a2011-04-26 17:41:56 +00001005}
1006
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001007void SkGPipeCanvas::drawText(const void* text, size_t byteLength, SkScalar x,
reed@google.combb6992a2011-04-26 17:41:56 +00001008 SkScalar y, const SkPaint& paint) {
1009 if (byteLength) {
reed@google.comacd471f2011-05-03 21:26:46 +00001010 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +00001011 this->writePaint(paint);
reed@google.comacd471f2011-05-03 21:26:46 +00001012 if (this->needOpBytes(4 + SkAlign4(byteLength) + 2 * sizeof(SkScalar))) {
reed@google.com31891582011-05-12 03:03:56 +00001013 this->writeOp(kDrawText_DrawOp);
reed@google.comacd471f2011-05-03 21:26:46 +00001014 fWriter.write32(byteLength);
1015 fWriter.writePad(text, byteLength);
1016 fWriter.writeScalar(x);
1017 fWriter.writeScalar(y);
1018 }
reed@google.combb6992a2011-04-26 17:41:56 +00001019 }
1020}
1021
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001022void SkGPipeCanvas::drawPosText(const void* text, size_t byteLength,
reed@google.combb6992a2011-04-26 17:41:56 +00001023 const SkPoint pos[], const SkPaint& paint) {
1024 if (byteLength) {
reed@google.comacd471f2011-05-03 21:26:46 +00001025 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +00001026 this->writePaint(paint);
reed@google.combb6992a2011-04-26 17:41:56 +00001027 int count = paint.textToGlyphs(text, byteLength, NULL);
reed@google.comacd471f2011-05-03 21:26:46 +00001028 if (this->needOpBytes(4 + SkAlign4(byteLength) + 4 + count * sizeof(SkPoint))) {
reed@google.com31891582011-05-12 03:03:56 +00001029 this->writeOp(kDrawPosText_DrawOp);
reed@google.comacd471f2011-05-03 21:26:46 +00001030 fWriter.write32(byteLength);
1031 fWriter.writePad(text, byteLength);
1032 fWriter.write32(count);
1033 fWriter.write(pos, count * sizeof(SkPoint));
1034 }
reed@google.combb6992a2011-04-26 17:41:56 +00001035 }
1036}
1037
1038void SkGPipeCanvas::drawPosTextH(const void* text, size_t byteLength,
1039 const SkScalar xpos[], SkScalar constY,
1040 const SkPaint& paint) {
1041 if (byteLength) {
reed@google.comacd471f2011-05-03 21:26:46 +00001042 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +00001043 this->writePaint(paint);
reed@google.combb6992a2011-04-26 17:41:56 +00001044 int count = paint.textToGlyphs(text, byteLength, NULL);
reed@google.comacd471f2011-05-03 21:26:46 +00001045 if (this->needOpBytes(4 + SkAlign4(byteLength) + 4 + count * sizeof(SkScalar) + 4)) {
reed@google.com31891582011-05-12 03:03:56 +00001046 this->writeOp(kDrawPosTextH_DrawOp);
reed@google.comacd471f2011-05-03 21:26:46 +00001047 fWriter.write32(byteLength);
1048 fWriter.writePad(text, byteLength);
1049 fWriter.write32(count);
1050 fWriter.write(xpos, count * sizeof(SkScalar));
1051 fWriter.writeScalar(constY);
1052 }
reed@google.combb6992a2011-04-26 17:41:56 +00001053 }
1054}
1055
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001056void SkGPipeCanvas::drawTextOnPath(const void* text, size_t byteLength,
1057 const SkPath& path, const SkMatrix* matrix,
reed@google.combb6992a2011-04-26 17:41:56 +00001058 const SkPaint& paint) {
1059 if (byteLength) {
reed@google.comacd471f2011-05-03 21:26:46 +00001060 NOTIFY_SETUP(this);
reed@google.combb6992a2011-04-26 17:41:56 +00001061 unsigned flags = 0;
djsollen@google.com94e75ee2012-06-08 18:30:46 +00001062 size_t size = 4 + SkAlign4(byteLength) + path.writeToMemory(NULL);
reed@google.combb6992a2011-04-26 17:41:56 +00001063 if (matrix) {
1064 flags |= kDrawTextOnPath_HasMatrix_DrawOpFlag;
djsollen@google.com94e75ee2012-06-08 18:30:46 +00001065 size += matrix->writeToMemory(NULL);
reed@google.combb6992a2011-04-26 17:41:56 +00001066 }
reed@google.com31891582011-05-12 03:03:56 +00001067 this->writePaint(paint);
reed@google.comacd471f2011-05-03 21:26:46 +00001068 if (this->needOpBytes(size)) {
reed@google.com31891582011-05-12 03:03:56 +00001069 this->writeOp(kDrawTextOnPath_DrawOp, flags, 0);
reed@google.combb6992a2011-04-26 17:41:56 +00001070
reed@google.comacd471f2011-05-03 21:26:46 +00001071 fWriter.write32(byteLength);
1072 fWriter.writePad(text, byteLength);
reed@google.combb6992a2011-04-26 17:41:56 +00001073
djsollen@google.com94e75ee2012-06-08 18:30:46 +00001074 fWriter.writePath(path);
reed@google.comacd471f2011-05-03 21:26:46 +00001075 if (matrix) {
djsollen@google.com2b2ede32012-04-12 13:24:04 +00001076 fWriter.writeMatrix(*matrix);
reed@google.comacd471f2011-05-03 21:26:46 +00001077 }
reed@google.combb6992a2011-04-26 17:41:56 +00001078 }
1079 }
1080}
1081
1082void SkGPipeCanvas::drawPicture(SkPicture& picture) {
reed@google.com0faac1e2011-05-11 05:58:58 +00001083 // we want to playback the picture into individual draw calls
1084 this->INHERITED::drawPicture(picture);
reed@google.combb6992a2011-04-26 17:41:56 +00001085}
1086
reed@google.combb6992a2011-04-26 17:41:56 +00001087void SkGPipeCanvas::drawVertices(VertexMode mode, int vertexCount,
1088 const SkPoint vertices[], const SkPoint texs[],
1089 const SkColor colors[], SkXfermode*,
1090 const uint16_t indices[], int indexCount,
1091 const SkPaint& paint) {
1092 if (0 == vertexCount) {
1093 return;
1094 }
1095
reed@google.comacd471f2011-05-03 21:26:46 +00001096 NOTIFY_SETUP(this);
1097 size_t size = 4 + vertexCount * sizeof(SkPoint);
reed@google.com31891582011-05-12 03:03:56 +00001098 this->writePaint(paint);
reed@google.combb6992a2011-04-26 17:41:56 +00001099 unsigned flags = 0;
1100 if (texs) {
1101 flags |= kDrawVertices_HasTexs_DrawOpFlag;
reed@google.comacd471f2011-05-03 21:26:46 +00001102 size += vertexCount * sizeof(SkPoint);
reed@google.combb6992a2011-04-26 17:41:56 +00001103 }
1104 if (colors) {
1105 flags |= kDrawVertices_HasColors_DrawOpFlag;
reed@google.comacd471f2011-05-03 21:26:46 +00001106 size += vertexCount * sizeof(SkColor);
reed@google.combb6992a2011-04-26 17:41:56 +00001107 }
1108 if (indices && indexCount > 0) {
1109 flags |= kDrawVertices_HasIndices_DrawOpFlag;
reed@google.comacd471f2011-05-03 21:26:46 +00001110 size += 4 + SkAlign4(indexCount * sizeof(uint16_t));
reed@google.combb6992a2011-04-26 17:41:56 +00001111 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001112
reed@google.comacd471f2011-05-03 21:26:46 +00001113 if (this->needOpBytes(size)) {
reed@google.com31891582011-05-12 03:03:56 +00001114 this->writeOp(kDrawVertices_DrawOp, flags, 0);
reed@google.comacd471f2011-05-03 21:26:46 +00001115 fWriter.write32(mode);
1116 fWriter.write32(vertexCount);
1117 fWriter.write(vertices, vertexCount * sizeof(SkPoint));
1118 if (texs) {
1119 fWriter.write(texs, vertexCount * sizeof(SkPoint));
1120 }
1121 if (colors) {
1122 fWriter.write(colors, vertexCount * sizeof(SkColor));
1123 }
reed@google.combb6992a2011-04-26 17:41:56 +00001124
reed@google.comacd471f2011-05-03 21:26:46 +00001125 // TODO: flatten xfermode
reed@google.combb6992a2011-04-26 17:41:56 +00001126
reed@google.comacd471f2011-05-03 21:26:46 +00001127 if (indices && indexCount > 0) {
1128 fWriter.write32(indexCount);
1129 fWriter.writePad(indices, indexCount * sizeof(uint16_t));
1130 }
reed@google.combb6992a2011-04-26 17:41:56 +00001131 }
1132}
1133
reed@google.comacd471f2011-05-03 21:26:46 +00001134void SkGPipeCanvas::drawData(const void* ptr, size_t size) {
1135 if (size && ptr) {
1136 NOTIFY_SETUP(this);
reed@google.combb6992a2011-04-26 17:41:56 +00001137 unsigned data = 0;
1138 if (size < (1 << DRAWOPS_DATA_BITS)) {
1139 data = (unsigned)size;
1140 }
reed@google.comacd471f2011-05-03 21:26:46 +00001141 if (this->needOpBytes(4 + SkAlign4(size))) {
1142 this->writeOp(kDrawData_DrawOp, 0, data);
1143 if (0 == data) {
1144 fWriter.write32(size);
1145 }
reed@google.combb6793b2011-05-05 15:18:15 +00001146 fWriter.writePad(ptr, size);
reed@google.combb6992a2011-04-26 17:41:56 +00001147 }
1148 }
1149}
1150
junov@chromium.org77eec242012-07-18 17:54:45 +00001151void SkGPipeCanvas::flushRecording(bool detachCurrentBlock) {
1152 doNotify();
1153 if (detachCurrentBlock) {
1154 // force a new block to be requested for the next recorded command
1155 fBlockSize = 0;
1156 }
1157}
1158
reed@google.combb6992a2011-04-26 17:41:56 +00001159///////////////////////////////////////////////////////////////////////////////
1160
1161template <typename T> uint32_t castToU32(T value) {
1162 union {
1163 T fSrc;
1164 uint32_t fDst;
1165 } data;
1166 data.fSrc = value;
1167 return data.fDst;
1168}
1169
reed@google.com31891582011-05-12 03:03:56 +00001170void SkGPipeCanvas::writePaint(const SkPaint& paint) {
1171 SkPaint& base = fPaint;
reed@google.combb6992a2011-04-26 17:41:56 +00001172 uint32_t storage[32];
1173 uint32_t* ptr = storage;
reed@google.combb6992a2011-04-26 17:41:56 +00001174
1175 if (base.getFlags() != paint.getFlags()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001176 *ptr++ = PaintOp_packOpData(kFlags_PaintOp, paint.getFlags());
reed@google.comf5842f72011-05-04 18:30:04 +00001177 base.setFlags(paint.getFlags());
reed@google.combb6992a2011-04-26 17:41:56 +00001178 }
1179 if (base.getColor() != paint.getColor()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001180 *ptr++ = PaintOp_packOp(kColor_PaintOp);
1181 *ptr++ = paint.getColor();
reed@google.comf5842f72011-05-04 18:30:04 +00001182 base.setColor(paint.getColor());
reed@google.combb6992a2011-04-26 17:41:56 +00001183 }
1184 if (base.getStyle() != paint.getStyle()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001185 *ptr++ = PaintOp_packOpData(kStyle_PaintOp, paint.getStyle());
reed@google.comf5842f72011-05-04 18:30:04 +00001186 base.setStyle(paint.getStyle());
reed@google.combb6992a2011-04-26 17:41:56 +00001187 }
1188 if (base.getStrokeJoin() != paint.getStrokeJoin()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001189 *ptr++ = PaintOp_packOpData(kJoin_PaintOp, paint.getStrokeJoin());
reed@google.comf5842f72011-05-04 18:30:04 +00001190 base.setStrokeJoin(paint.getStrokeJoin());
reed@google.combb6992a2011-04-26 17:41:56 +00001191 }
1192 if (base.getStrokeCap() != paint.getStrokeCap()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001193 *ptr++ = PaintOp_packOpData(kCap_PaintOp, paint.getStrokeCap());
reed@google.comf5842f72011-05-04 18:30:04 +00001194 base.setStrokeCap(paint.getStrokeCap());
reed@google.combb6992a2011-04-26 17:41:56 +00001195 }
1196 if (base.getStrokeWidth() != paint.getStrokeWidth()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001197 *ptr++ = PaintOp_packOp(kWidth_PaintOp);
1198 *ptr++ = castToU32(paint.getStrokeWidth());
reed@google.comf5842f72011-05-04 18:30:04 +00001199 base.setStrokeWidth(paint.getStrokeWidth());
reed@google.combb6992a2011-04-26 17:41:56 +00001200 }
1201 if (base.getStrokeMiter() != paint.getStrokeMiter()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001202 *ptr++ = PaintOp_packOp(kMiter_PaintOp);
1203 *ptr++ = castToU32(paint.getStrokeMiter());
reed@google.comf5842f72011-05-04 18:30:04 +00001204 base.setStrokeMiter(paint.getStrokeMiter());
reed@google.combb6992a2011-04-26 17:41:56 +00001205 }
1206 if (base.getTextEncoding() != paint.getTextEncoding()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001207 *ptr++ = PaintOp_packOpData(kEncoding_PaintOp, paint.getTextEncoding());
reed@google.comf5842f72011-05-04 18:30:04 +00001208 base.setTextEncoding(paint.getTextEncoding());
reed@google.combb6992a2011-04-26 17:41:56 +00001209 }
1210 if (base.getHinting() != paint.getHinting()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001211 *ptr++ = PaintOp_packOpData(kHinting_PaintOp, paint.getHinting());
reed@google.comf5842f72011-05-04 18:30:04 +00001212 base.setHinting(paint.getHinting());
reed@google.combb6992a2011-04-26 17:41:56 +00001213 }
1214 if (base.getTextAlign() != paint.getTextAlign()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001215 *ptr++ = PaintOp_packOpData(kAlign_PaintOp, paint.getTextAlign());
reed@google.comf5842f72011-05-04 18:30:04 +00001216 base.setTextAlign(paint.getTextAlign());
reed@google.combb6992a2011-04-26 17:41:56 +00001217 }
1218 if (base.getTextSize() != paint.getTextSize()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001219 *ptr++ = PaintOp_packOp(kTextSize_PaintOp);
1220 *ptr++ = castToU32(paint.getTextSize());
reed@google.comf5842f72011-05-04 18:30:04 +00001221 base.setTextSize(paint.getTextSize());
reed@google.combb6992a2011-04-26 17:41:56 +00001222 }
1223 if (base.getTextScaleX() != paint.getTextScaleX()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001224 *ptr++ = PaintOp_packOp(kTextScaleX_PaintOp);
1225 *ptr++ = castToU32(paint.getTextScaleX());
reed@google.comf5842f72011-05-04 18:30:04 +00001226 base.setTextScaleX(paint.getTextScaleX());
reed@google.combb6992a2011-04-26 17:41:56 +00001227 }
1228 if (base.getTextSkewX() != paint.getTextSkewX()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001229 *ptr++ = PaintOp_packOp(kTextSkewX_PaintOp);
1230 *ptr++ = castToU32(paint.getTextSkewX());
reed@google.comf5842f72011-05-04 18:30:04 +00001231 base.setTextSkewX(paint.getTextSkewX());
1232 }
1233
1234 if (!SkTypeface::Equal(base.getTypeface(), paint.getTypeface())) {
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +00001235 if (isCrossProcess(fFlags)) {
scroggo@google.com3cb969f2012-07-27 20:39:19 +00001236 uint32_t id = this->getTypefaceID(paint.getTypeface());
1237 *ptr++ = PaintOp_packOpData(kTypeface_PaintOp, id);
1238 } else if (this->needOpBytes(sizeof(void*))) {
1239 // Add to the set for ref counting.
1240 fTypefaceSet.add(paint.getTypeface());
1241 // It is safe to write the typeface to the stream before the rest
1242 // of the paint unless we ever send a kReset_PaintOp, which we
1243 // currently never do.
1244 this->writeOp(kSetTypeface_DrawOp);
1245 fWriter.writePtr(paint.getTypeface());
1246 }
reed@google.comf5842f72011-05-04 18:30:04 +00001247 base.setTypeface(paint.getTypeface());
reed@google.combb6992a2011-04-26 17:41:56 +00001248 }
reed@google.combb6992a2011-04-26 17:41:56 +00001249
scroggo@google.com4dffc592012-07-17 16:49:40 +00001250 // This is a new paint, so all old flats can be safely purged, if necessary.
1251 fFlattenableHeap.markAllFlatsSafeToDelete();
reed@google.comb55d1182011-05-11 00:42:04 +00001252 for (int i = 0; i < kCount_PaintFlats; i++) {
1253 int index = this->flattenToIndex(get_paintflat(paint, i), (PaintFlats)i);
scroggo@google.comd3ba5cc2012-07-09 16:05:53 +00001254 bool replaced = index < 0;
1255 if (replaced) {
1256 index = ~index;
1257 }
scroggo@google.com4dffc592012-07-17 16:49:40 +00001258 // Store the index of any flat that needs to be kept. 0 means no flat.
1259 if (index > 0) {
1260 fFlattenableHeap.markFlatForKeeping(index);
1261 }
1262 SkASSERT(index >= 0 && index <= fFlatDictionary.count());
scroggo@google.comd3ba5cc2012-07-09 16:05:53 +00001263 if (index != fCurrFlatIndex[i] || replaced) {
reed@google.comb55d1182011-05-11 00:42:04 +00001264 *ptr++ = PaintOp_packOpFlagData(kFlatIndex_PaintOp, i, index);
1265 fCurrFlatIndex[i] = index;
1266 }
1267 }
1268
reed@google.comacd471f2011-05-03 21:26:46 +00001269 size_t size = (char*)ptr - (char*)storage;
1270 if (size && this->needOpBytes(size)) {
reed@google.comb55d1182011-05-11 00:42:04 +00001271 this->writeOp(kPaintOp_DrawOp, 0, size);
reed@google.comb55d1182011-05-11 00:42:04 +00001272 fWriter.write(storage, size);
reed@google.combb6992a2011-04-26 17:41:56 +00001273 for (size_t i = 0; i < size/4; i++) {
reed@google.comb55d1182011-05-11 00:42:04 +00001274// SkDebugf("[%d] %08X\n", i, storage[i]);
reed@google.combb6992a2011-04-26 17:41:56 +00001275 }
1276 }
reed@google.combb6992a2011-04-26 17:41:56 +00001277}
1278
1279///////////////////////////////////////////////////////////////////////////////
1280
1281#include "SkGPipe.h"
1282
scroggo@google.com3cb969f2012-07-27 20:39:19 +00001283SkGPipeController::~SkGPipeController() {
1284 SkSafeUnref(fCanvas);
1285}
1286
1287void SkGPipeController::setCanvas(SkGPipeCanvas* canvas) {
1288 SkRefCnt_SafeAssign(fCanvas, canvas);
1289}
1290
1291///////////////////////////////////////////////////////////////////////////////
1292
1293SkGPipeWriter::SkGPipeWriter()
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +00001294: fWriter(0) {
reed@google.combb6992a2011-04-26 17:41:56 +00001295 fCanvas = NULL;
1296}
1297
1298SkGPipeWriter::~SkGPipeWriter() {
reed@google.comacd471f2011-05-03 21:26:46 +00001299 this->endRecording();
reed@google.combb6992a2011-04-26 17:41:56 +00001300}
1301
scroggo@google.com565254b2012-06-28 15:41:32 +00001302SkCanvas* SkGPipeWriter::startRecording(SkGPipeController* controller, uint32_t flags) {
reed@google.combb6992a2011-04-26 17:41:56 +00001303 if (NULL == fCanvas) {
reed@google.comacd471f2011-05-03 21:26:46 +00001304 fWriter.reset(NULL, 0);
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +00001305 fCanvas = SkNEW_ARGS(SkGPipeCanvas, (controller, &fWriter, flags));
reed@google.combb6992a2011-04-26 17:41:56 +00001306 }
scroggo@google.com3cb969f2012-07-27 20:39:19 +00001307 controller->setCanvas(fCanvas);
reed@google.combb6992a2011-04-26 17:41:56 +00001308 return fCanvas;
1309}
1310
1311void SkGPipeWriter::endRecording() {
1312 if (fCanvas) {
1313 fCanvas->finish();
1314 fCanvas->unref();
1315 fCanvas = NULL;
1316 }
1317}
1318
junov@chromium.org77eec242012-07-18 17:54:45 +00001319void SkGPipeWriter::flushRecording(bool detachCurrentBlock){
1320 fCanvas->flushRecording(detachCurrentBlock);
1321}
1322
scroggo@google.com15011ee2012-07-26 20:03:32 +00001323size_t SkGPipeWriter::storageAllocatedForRecording() {
1324 return NULL == fCanvas ? 0 : fCanvas->storageAllocatedForRecording();
1325}
1326