blob: dff3e1423e3153355431e923e850472e54deea5d [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@google.combb6992a2011-04-26 17:41:56 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
reed@google.combb6992a2011-04-26 17:41:56 +00007 */
8
9
epoger@google.comec3ed6a2011-07-28 14:26:00 +000010
reed@google.combb6992a2011-04-26 17:41:56 +000011#include "SkCanvas.h"
reed@google.com8a85d0c2011-06-24 19:12:12 +000012#include "SkData.h"
reed@google.combb6793b2011-05-05 15:18:15 +000013#include "SkDevice.h"
reed@google.combb6992a2011-04-26 17:41:56 +000014#include "SkPaint.h"
reed@google.com75a09722012-05-10 12:56:16 +000015#include "SkPathEffect.h"
reed@google.comacd471f2011-05-03 21:26:46 +000016#include "SkGPipe.h"
reed@google.combb6992a2011-04-26 17:41:56 +000017#include "SkGPipePriv.h"
scroggo@google.com16d1d0b2012-05-02 19:09:40 +000018#include "SkImageFilter.h"
reed@google.comf5842f72011-05-04 18:30:04 +000019#include "SkStream.h"
reed@google.comb55d1182011-05-11 00:42:04 +000020#include "SkTSearch.h"
reed@google.comf5842f72011-05-04 18:30:04 +000021#include "SkTypeface.h"
reed@google.combb6992a2011-04-26 17:41:56 +000022#include "SkWriter32.h"
reed@google.comb55d1182011-05-11 00:42:04 +000023#include "SkColorFilter.h"
reed@google.com0faac1e2011-05-11 05:58:58 +000024#include "SkDrawLooper.h"
reed@google.comb55d1182011-05-11 00:42:04 +000025#include "SkMaskFilter.h"
26#include "SkRasterizer.h"
27#include "SkShader.h"
djsollen@google.com2b2ede32012-04-12 13:24:04 +000028#include "SkOrderedWriteBuffer.h"
scroggo@google.com4dffc592012-07-17 16:49:40 +000029#include "SkPictureFlat.h"
reed@google.comb55d1182011-05-11 00:42:04 +000030
31static SkFlattenable* get_paintflat(const SkPaint& paint, unsigned paintFlat) {
32 SkASSERT(paintFlat < kCount_PaintFlats);
33 switch (paintFlat) {
34 case kColorFilter_PaintFlat: return paint.getColorFilter();
reed@google.com0faac1e2011-05-11 05:58:58 +000035 case kDrawLooper_PaintFlat: return paint.getLooper();
reed@google.comb55d1182011-05-11 00:42:04 +000036 case kMaskFilter_PaintFlat: return paint.getMaskFilter();
37 case kPathEffect_PaintFlat: return paint.getPathEffect();
38 case kRasterizer_PaintFlat: return paint.getRasterizer();
39 case kShader_PaintFlat: return paint.getShader();
scroggo@google.com16d1d0b2012-05-02 19:09:40 +000040 case kImageFilter_PaintFlat: return paint.getImageFilter();
reed@google.comb55d1182011-05-11 00:42:04 +000041 case kXfermode_PaintFlat: return paint.getXfermode();
42 }
tomhudson@google.com0c00f212011-12-28 14:59:50 +000043 SkDEBUGFAIL("never gets here");
reed@google.comb55d1182011-05-11 00:42:04 +000044 return NULL;
45}
reed@google.combb6992a2011-04-26 17:41:56 +000046
reed@google.comf5842f72011-05-04 18:30:04 +000047static size_t writeTypeface(SkWriter32* writer, SkTypeface* typeface) {
48 SkASSERT(typeface);
49 SkDynamicMemoryWStream stream;
50 typeface->serialize(&stream);
51 size_t size = stream.getOffset();
52 if (writer) {
53 writer->write32(size);
reed@google.com8a85d0c2011-06-24 19:12:12 +000054 SkAutoDataUnref data(stream.copyToData());
robertphillips@google.com59f46b82012-07-10 17:30:58 +000055 writer->writePad(data->data(), size);
reed@google.comf5842f72011-05-04 18:30:04 +000056 }
scroggo@google.com5af9b202012-06-04 17:17:36 +000057 return 4 + SkAlign4(size);
reed@google.comf5842f72011-05-04 18:30:04 +000058}
59
reed@google.combb6992a2011-04-26 17:41:56 +000060///////////////////////////////////////////////////////////////////////////////
61
scroggo@google.com4dffc592012-07-17 16:49:40 +000062class FlattenableHeap : public SkFlatController {
63public:
64 FlattenableHeap(int numFlatsToKeep) : fNumFlatsToKeep(numFlatsToKeep) {}
65
66 ~FlattenableHeap() {
67 fPointers.freeAll();
68 }
69
70 virtual void* allocThrow(size_t bytes) SK_OVERRIDE;
71
72 virtual void unalloc(void* ptr) SK_OVERRIDE;
73
74 const SkFlatData* flatToReplace() const;
75
76 // Mark an SkFlatData as one that should not be returned by flatToReplace.
77 // Takes the result of SkFlatData::index() as its parameter.
78 void markFlatForKeeping(int index) {
79 *fFlatsThatMustBeKept.append() = index;
80 }
81
82 void markAllFlatsSafeToDelete() {
83 fFlatsThatMustBeKept.reset();
84 }
85
86private:
87 // Keep track of the indices (i.e. the result of SkFlatData::index()) of
88 // flats that must be kept, since they are on the current paint.
89 SkTDArray<int> fFlatsThatMustBeKept;
90 SkTDArray<void*> fPointers;
91 const int fNumFlatsToKeep;
92};
93
94void FlattenableHeap::unalloc(void* ptr) {
95 int indexToRemove = fPointers.rfind(ptr);
96 if (indexToRemove >= 0) {
97 sk_free(ptr);
98 fPointers.remove(indexToRemove);
99 }
100}
101
102void* FlattenableHeap::allocThrow(size_t bytes) {
103 void* ptr = sk_malloc_throw(bytes);
104 *fPointers.append() = ptr;
105 return ptr;
106}
107
108const SkFlatData* FlattenableHeap::flatToReplace() const {
109 // First, determine whether we should replace one.
110 if (fPointers.count() > fNumFlatsToKeep) {
111 // Look through the flattenable heap.
112 // TODO: Return the LRU flat.
113 for (int i = 0; i < fPointers.count(); i++) {
114 SkFlatData* potential = (SkFlatData*)fPointers[i];
115 // Make sure that it is not one that must be kept.
116 bool mustKeep = false;
117 for (int j = 0; j < fFlatsThatMustBeKept.count(); j++) {
118 if (potential->index() == fFlatsThatMustBeKept[j]) {
119 mustKeep = true;
120 break;
121 }
122 }
123 if (!mustKeep) {
124 return potential;
125 }
126 }
127 }
128 return NULL;
129}
130
131///////////////////////////////////////////////////////////////////////////////
132
133class FlatDictionary : public SkFlatDictionary<SkFlattenable> {
134public:
135 FlatDictionary(FlattenableHeap* heap, SkFactorySet* factorySet)
136 : SkFlatDictionary<SkFlattenable>(heap, NULL, NULL, factorySet) {
137 fFlattenProc = &flattenFlattenableProc;
138 // No need to define fUnflattenProc since the writer will never
139 // unflatten the data.
140 }
141 static void flattenFlattenableProc(SkOrderedWriteBuffer& buffer,
142 const void* obj) {
143 buffer.writeFlattenable((SkFlattenable*)obj);
144 }
145
146};
147
148///////////////////////////////////////////////////////////////////////////////
149
scroggo@google.com8ae3c7f2012-06-13 17:47:52 +0000150/*
151 * Shared heap for storing large things that can be shared, for a stream
152 * used by multiple readers.
153 * TODO: Make the allocations all come from cross process safe address space
154 * TODO: Store paths (others?)
scroggo@google.com284bf502012-07-17 16:10:34 +0000155 * TODO: Generalize the LRU caching mechanism
scroggo@google.com8ae3c7f2012-06-13 17:47:52 +0000156 */
scroggo@google.com4dffc592012-07-17 16:49:40 +0000157class SharedHeap {
scroggo@google.com8ae3c7f2012-06-13 17:47:52 +0000158public:
scroggo@google.com4dffc592012-07-17 16:49:40 +0000159 SharedHeap(bool shallow, int numOfReaders)
scroggo@google.com284bf502012-07-17 16:10:34 +0000160 : fBitmapCount(0)
161 , fMostRecentlyUsed(NULL)
162 , fLeastRecentlyUsed(NULL)
163 , fCanDoShallowCopies(shallow)
164 , fNumberOfReaders(numOfReaders) {}
scroggo@google.com4dffc592012-07-17 16:49:40 +0000165 ~SharedHeap() {
scroggo@google.com284bf502012-07-17 16:10:34 +0000166 BitmapInfo* iter = fMostRecentlyUsed;
167 while (iter != NULL) {
168 BitmapInfo* next = iter->fLessRecentlyUsed;
169 SkDELETE(iter);
170 fBitmapCount--;
171 iter = next;
scroggo@google.com8ae3c7f2012-06-13 17:47:52 +0000172 }
scroggo@google.com284bf502012-07-17 16:10:34 +0000173 SkASSERT(0 == fBitmapCount);
scroggo@google.com8ae3c7f2012-06-13 17:47:52 +0000174 }
175
176 /*
177 * Add a copy of a bitmap to the heap.
178 * @param bm The SkBitmap to be copied and placed in the heap.
scroggo@google.com284bf502012-07-17 16:10:34 +0000179 * @return void* Pointer to the BitmapInfo stored in the heap, which
180 * contains a copy of the SkBitmap. If NULL,
scroggo@google.com8ae3c7f2012-06-13 17:47:52 +0000181 * the bitmap could not be copied.
182 */
scroggo@google.com284bf502012-07-17 16:10:34 +0000183 const void* addBitmap(const SkBitmap& orig) {
scroggo@google.com4f1f6bf2012-07-02 13:35:09 +0000184 const uint32_t genID = orig.getGenerationID();
185 SkPixelRef* sharedPixelRef = NULL;
scroggo@google.com284bf502012-07-17 16:10:34 +0000186 // When looking to see if we've previously used this bitmap, start at
187 // the end, assuming that the caller is more likely to reuse a recent
188 // one.
189 BitmapInfo* iter = fMostRecentlyUsed;
190 while (iter != NULL) {
191 if (genID == iter->fGenID) {
192 SkBitmap* storedBitmap = iter->fBitmap;
scroggo@google.com6ea165d2012-07-03 14:52:08 +0000193 if (orig.pixelRefOffset() != storedBitmap->pixelRefOffset()
194 || orig.width() != storedBitmap->width()
195 || orig.height() != storedBitmap->height()) {
scroggo@google.com4f1f6bf2012-07-02 13:35:09 +0000196 // In this case, the bitmaps share a pixelRef, but have
scroggo@google.com6ea165d2012-07-03 14:52:08 +0000197 // different offsets or sizes. Keep track of the other
198 // bitmap so that instead of making another copy of the
199 // pixelRef we can use the copy we already made.
200 sharedPixelRef = storedBitmap->pixelRef();
scroggo@google.com4f1f6bf2012-07-02 13:35:09 +0000201 break;
202 }
scroggo@google.com284bf502012-07-17 16:10:34 +0000203 iter->addDraws(fNumberOfReaders);
204 this->setMostRecentlyUsed(iter);
205 return iter;
scroggo@google.com8ae3c7f2012-06-13 17:47:52 +0000206 }
scroggo@google.com284bf502012-07-17 16:10:34 +0000207 iter = iter->fLessRecentlyUsed;
scroggo@google.com8ae3c7f2012-06-13 17:47:52 +0000208 }
scroggo@google.com284bf502012-07-17 16:10:34 +0000209 SkAutoRef ar((SkRefCnt*)sharedPixelRef);
210 BitmapInfo* replace = this->bitmapToReplace(orig);
scroggo@google.com565254b2012-06-28 15:41:32 +0000211 SkBitmap* copy;
212 // If the bitmap is mutable, we still need to do a deep copy, since the
213 // caller may modify it afterwards. That said, if the bitmap is mutable,
214 // but has no pixelRef, the copy constructor actually does a deep copy.
scroggo@google.com4f1f6bf2012-07-02 13:35:09 +0000215 if (fCanDoShallowCopies && (orig.isImmutable() || !orig.pixelRef())) {
scroggo@google.com284bf502012-07-17 16:10:34 +0000216 if (NULL == replace) {
217 copy = SkNEW_ARGS(SkBitmap, (orig));
218 } else {
219 *replace->fBitmap = orig;
220 }
scroggo@google.com565254b2012-06-28 15:41:32 +0000221 } else {
scroggo@google.com4f1f6bf2012-07-02 13:35:09 +0000222 if (sharedPixelRef != NULL) {
scroggo@google.com284bf502012-07-17 16:10:34 +0000223 if (NULL == replace) {
224 // Do a shallow copy of the bitmap to get the width, height, etc
225 copy = SkNEW_ARGS(SkBitmap, (orig));
226 // Replace the pixelRef with the copy that was already made, and
227 // use the appropriate offset.
228 copy->setPixelRef(sharedPixelRef, orig.pixelRefOffset());
229 } else {
230 *replace->fBitmap = orig;
231 replace->fBitmap->setPixelRef(sharedPixelRef, orig.pixelRefOffset());
232 }
scroggo@google.com4f1f6bf2012-07-02 13:35:09 +0000233 } else {
scroggo@google.com284bf502012-07-17 16:10:34 +0000234 if (NULL == replace) {
235 copy = SkNEW(SkBitmap);
236 if (!orig.copyTo(copy, orig.getConfig())) {
237 delete copy;
238 return NULL;
239 }
240 } else {
241 if (!orig.copyTo(replace->fBitmap, orig.getConfig())) {
242 return NULL;
243 }
scroggo@google.com4f1f6bf2012-07-02 13:35:09 +0000244 }
scroggo@google.com565254b2012-06-28 15:41:32 +0000245 }
scroggo@google.com8ae3c7f2012-06-13 17:47:52 +0000246 }
scroggo@google.com284bf502012-07-17 16:10:34 +0000247 BitmapInfo* info;
248 if (NULL == replace) {
249 info = SkNEW_ARGS(BitmapInfo, (copy, genID, fNumberOfReaders));
250 fBitmapCount++;
251 } else {
252 replace->fGenID = genID;
253 replace->addDraws(fNumberOfReaders);
254 info = replace;
255 }
256 this->setMostRecentlyUsed(info);
257 return info;
scroggo@google.com8ae3c7f2012-06-13 17:47:52 +0000258 }
259private:
scroggo@google.com284bf502012-07-17 16:10:34 +0000260 void setMostRecentlyUsed(BitmapInfo* info);
261 BitmapInfo* bitmapToReplace(const SkBitmap& bm) const;
262
263 int fBitmapCount;
264 BitmapInfo* fLeastRecentlyUsed;
265 BitmapInfo* fMostRecentlyUsed;
266 const bool fCanDoShallowCopies;
267 const int fNumberOfReaders;
scroggo@google.com8ae3c7f2012-06-13 17:47:52 +0000268};
269
scroggo@google.com284bf502012-07-17 16:10:34 +0000270// We just "used" info. Update our LRU accordingly
scroggo@google.com4dffc592012-07-17 16:49:40 +0000271void SharedHeap::setMostRecentlyUsed(BitmapInfo* info) {
scroggo@google.com284bf502012-07-17 16:10:34 +0000272 SkASSERT(info != NULL);
273 if (info == fMostRecentlyUsed) {
274 return;
275 }
276 // Remove info from its prior place, and make sure to cover the hole.
277 if (fLeastRecentlyUsed == info) {
278 SkASSERT(info->fMoreRecentlyUsed != NULL);
279 fLeastRecentlyUsed = info->fMoreRecentlyUsed;
280 }
281 if (info->fMoreRecentlyUsed != NULL) {
282 SkASSERT(fMostRecentlyUsed != info);
283 info->fMoreRecentlyUsed->fLessRecentlyUsed = info->fLessRecentlyUsed;
284 }
285 if (info->fLessRecentlyUsed != NULL) {
286 SkASSERT(fLeastRecentlyUsed != info);
287 info->fLessRecentlyUsed->fMoreRecentlyUsed = info->fMoreRecentlyUsed;
288 }
289 info->fMoreRecentlyUsed = NULL;
290 // Set up the head and tail pointers properly.
291 if (fMostRecentlyUsed != NULL) {
292 SkASSERT(NULL == fMostRecentlyUsed->fMoreRecentlyUsed);
293 fMostRecentlyUsed->fMoreRecentlyUsed = info;
294 info->fLessRecentlyUsed = fMostRecentlyUsed;
295 }
296 fMostRecentlyUsed = info;
297 if (NULL == fLeastRecentlyUsed) {
298 fLeastRecentlyUsed = info;
299 }
300}
301
302/**
303 * Given a new bitmap to be added to the cache, return an existing one that
304 * should be removed to make room, or NULL if there is already room.
305 */
scroggo@google.com4dffc592012-07-17 16:49:40 +0000306BitmapInfo* SharedHeap::bitmapToReplace(const SkBitmap& bm) const {
scroggo@google.com284bf502012-07-17 16:10:34 +0000307 // Arbitrarily set a limit of 5. We should test to find the best tradeoff
308 // between time and space. A lower limit means that we use less space, but
309 // it also means that we may have to insert the same bitmap into the heap
310 // multiple times (depending on the input), potentially taking more time.
311 // On the other hand, a lower limit also means searching through our stored
312 // bitmaps takes less time.
313 if (fBitmapCount > 5) {
314 BitmapInfo* iter = fLeastRecentlyUsed;
315 while (iter != NULL) {
316 if (iter->drawCount() > 0) {
317 // If the least recently used bitmap has not been drawn by some
318 // reader, then a more recently used one will not have been
319 // drawn yet either.
320 return NULL;
321 }
322 if (bm.pixelRef() != NULL
323 && bm.pixelRef() == iter->fBitmap->pixelRef()) {
324 // Do not replace a bitmap with a new one using the same
325 // pixel ref. Instead look for a different one that will
326 // potentially free up more space.
327 iter = iter->fMoreRecentlyUsed;
328 } else {
329 return iter;
330 }
331 }
332 }
333 return NULL;
334}
335
scroggo@google.com8ae3c7f2012-06-13 17:47:52 +0000336///////////////////////////////////////////////////////////////////////////////
337
reed@google.combb6992a2011-04-26 17:41:56 +0000338class SkGPipeCanvas : public SkCanvas {
339public:
scroggo@google.com565254b2012-06-28 15:41:32 +0000340 SkGPipeCanvas(SkGPipeController*, SkWriter32*, SkFactorySet*, uint32_t flags);
reed@google.combb6992a2011-04-26 17:41:56 +0000341 virtual ~SkGPipeCanvas();
342
343 void finish() {
344 if (!fDone) {
reed@google.comdbccc882011-07-08 18:53:39 +0000345 if (this->needOpBytes()) {
346 this->writeOp(kDone_DrawOp);
347 this->doNotify();
348 }
reed@google.combb6992a2011-04-26 17:41:56 +0000349 fDone = true;
350 }
351 }
352
junov@chromium.org77eec242012-07-18 17:54:45 +0000353 void flushRecording(bool detachCurrentBlock);
354
reed@google.combb6992a2011-04-26 17:41:56 +0000355 // overrides from SkCanvas
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000356 virtual int save(SaveFlags) SK_OVERRIDE;
357 virtual int saveLayer(const SkRect* bounds, const SkPaint*,
358 SaveFlags) SK_OVERRIDE;
359 virtual void restore() SK_OVERRIDE;
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000360 virtual bool isDrawingToLayer() const SK_OVERRIDE;
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000361 virtual bool translate(SkScalar dx, SkScalar dy) SK_OVERRIDE;
362 virtual bool scale(SkScalar sx, SkScalar sy) SK_OVERRIDE;
363 virtual bool rotate(SkScalar degrees) SK_OVERRIDE;
364 virtual bool skew(SkScalar sx, SkScalar sy) SK_OVERRIDE;
365 virtual bool concat(const SkMatrix& matrix) SK_OVERRIDE;
366 virtual void setMatrix(const SkMatrix& matrix) SK_OVERRIDE;
367 virtual bool clipRect(const SkRect& rect, SkRegion::Op op,
368 bool doAntiAlias = false) SK_OVERRIDE;
369 virtual bool clipPath(const SkPath& path, SkRegion::Op op,
370 bool doAntiAlias = false) SK_OVERRIDE;
371 virtual bool clipRegion(const SkRegion& region, SkRegion::Op op) SK_OVERRIDE;
372 virtual void clear(SkColor) SK_OVERRIDE;
373 virtual void drawPaint(const SkPaint& paint) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000374 virtual void drawPoints(PointMode, size_t count, const SkPoint pts[],
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000375 const SkPaint&) SK_OVERRIDE;
376 virtual void drawRect(const SkRect& rect, const SkPaint&) SK_OVERRIDE;
377 virtual void drawPath(const SkPath& path, const SkPaint&) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000378 virtual void drawBitmap(const SkBitmap&, SkScalar left, SkScalar top,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000379 const SkPaint*) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000380 virtual void drawBitmapRect(const SkBitmap&, const SkIRect* src,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000381 const SkRect& dst, const SkPaint*) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000382 virtual void drawBitmapMatrix(const SkBitmap&, const SkMatrix&,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000383 const SkPaint*) SK_OVERRIDE;
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000384 virtual void drawBitmapNine(const SkBitmap& bitmap, const SkIRect& center,
385 const SkRect& dst, const SkPaint* paint = NULL) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000386 virtual void drawSprite(const SkBitmap&, int left, int top,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000387 const SkPaint*) SK_OVERRIDE;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000388 virtual void drawText(const void* text, size_t byteLength, SkScalar x,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000389 SkScalar y, const SkPaint&) SK_OVERRIDE;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000390 virtual void drawPosText(const void* text, size_t byteLength,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000391 const SkPoint pos[], const SkPaint&) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000392 virtual void drawPosTextH(const void* text, size_t byteLength,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000393 const SkScalar xpos[], SkScalar constY,
394 const SkPaint&) SK_OVERRIDE;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000395 virtual void drawTextOnPath(const void* text, size_t byteLength,
396 const SkPath& path, const SkMatrix* matrix,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000397 const SkPaint&) SK_OVERRIDE;
398 virtual void drawPicture(SkPicture& picture) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000399 virtual void drawVertices(VertexMode, int vertexCount,
400 const SkPoint vertices[], const SkPoint texs[],
401 const SkColor colors[], SkXfermode*,
402 const uint16_t indices[], int indexCount,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000403 const SkPaint&) SK_OVERRIDE;
404 virtual void drawData(const void*, size_t) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000405
406private:
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000407 enum {
408 kNoSaveLayer = -1,
409 };
410 int fFirstSaveLayerStackLevel;
411 SharedHeap fSharedHeap;
reed@google.comacd471f2011-05-03 21:26:46 +0000412 SkGPipeController* fController;
reed@google.combb6992a2011-04-26 17:41:56 +0000413 SkWriter32& fWriter;
reed@google.comacd471f2011-05-03 21:26:46 +0000414 size_t fBlockSize; // amount allocated for writer
415 size_t fBytesNotified;
reed@google.combb6992a2011-04-26 17:41:56 +0000416 bool fDone;
scroggo@google.com565254b2012-06-28 15:41:32 +0000417 uint32_t fFlags;
reed@google.combb6992a2011-04-26 17:41:56 +0000418
reed@google.comf5842f72011-05-04 18:30:04 +0000419 SkRefCntSet fTypefaceSet;
420
421 uint32_t getTypefaceID(SkTypeface*);
422
reed@google.comacd471f2011-05-03 21:26:46 +0000423 inline void writeOp(DrawOps op, unsigned flags, unsigned data) {
reed@google.combb6992a2011-04-26 17:41:56 +0000424 fWriter.write32(DrawOp_packOpFlagData(op, flags, data));
425 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000426
reed@google.comacd471f2011-05-03 21:26:46 +0000427 inline void writeOp(DrawOps op) {
reed@google.combb6992a2011-04-26 17:41:56 +0000428 fWriter.write32(DrawOp_packOpFlagData(op, 0, 0));
429 }
reed@google.comacd471f2011-05-03 21:26:46 +0000430
431 bool needOpBytes(size_t size = 0);
432
433 inline void doNotify() {
434 if (!fDone) {
435 size_t bytes = fWriter.size() - fBytesNotified;
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000436 if (bytes > 0) {
437 fController->notifyWritten(bytes);
438 fBytesNotified += bytes;
439 }
reed@google.comacd471f2011-05-03 21:26:46 +0000440 }
441 }
reed@google.comb55d1182011-05-11 00:42:04 +0000442
scroggo@google.com4dffc592012-07-17 16:49:40 +0000443 // These are only used when in cross process, but with no shared address
444 // space, so bitmaps are flattened.
445 FlattenableHeap fBitmapHeap;
446 SkBitmapDictionary fBitmapDictionary;
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000447 int flattenToIndex(const SkBitmap&);
448
scroggo@google.com4dffc592012-07-17 16:49:40 +0000449 FlattenableHeap fFlattenableHeap;
450 FlatDictionary fFlatDictionary;
reed@google.comb55d1182011-05-11 00:42:04 +0000451 int fCurrFlatIndex[kCount_PaintFlats];
452 int flattenToIndex(SkFlattenable* obj, PaintFlats);
453
reed@google.com31891582011-05-12 03:03:56 +0000454 SkPaint fPaint;
455 void writePaint(const SkPaint&);
reed@google.combb6992a2011-04-26 17:41:56 +0000456
reed@google.comacd471f2011-05-03 21:26:46 +0000457 class AutoPipeNotify {
458 public:
459 AutoPipeNotify(SkGPipeCanvas* canvas) : fCanvas(canvas) {}
460 ~AutoPipeNotify() { fCanvas->doNotify(); }
461 private:
462 SkGPipeCanvas* fCanvas;
463 };
464 friend class AutoPipeNotify;
465
reed@google.combb6992a2011-04-26 17:41:56 +0000466 typedef SkCanvas INHERITED;
467};
468
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000469int SkGPipeCanvas::flattenToIndex(const SkBitmap & bitmap) {
scroggo@google.com565254b2012-06-28 15:41:32 +0000470 SkASSERT(shouldFlattenBitmaps(fFlags));
scroggo@google.com4dffc592012-07-17 16:49:40 +0000471 uint32_t flags = SkFlattenableWriteBuffer::kInlineFactoryNames_Flag
472 | SkFlattenableWriteBuffer::kCrossProcess_Flag;
473 bool added, replaced;
474 const SkFlatData* flat = fBitmapDictionary.findAndReplace(
475 bitmap, flags, fBitmapHeap.flatToReplace(), &added, &replaced);
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000476
scroggo@google.com4dffc592012-07-17 16:49:40 +0000477 int index = flat->index();
478 if (added && this->needOpBytes(flat->flatSize())) {
479 this->writeOp(kDef_Bitmap_DrawOp, 0, index);
480 fWriter.write(flat->data(), flat->flatSize());
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000481 }
scroggo@google.com4dffc592012-07-17 16:49:40 +0000482 return index;
scroggo@google.comd3ba5cc2012-07-09 16:05:53 +0000483}
484
reed@google.comb55d1182011-05-11 00:42:04 +0000485// return 0 for NULL (or unflattenable obj), or index-base-1
scroggo@google.comd3ba5cc2012-07-09 16:05:53 +0000486// return ~(index-base-1) if an old flattenable was replaced
reed@google.comb55d1182011-05-11 00:42:04 +0000487int SkGPipeCanvas::flattenToIndex(SkFlattenable* obj, PaintFlats paintflat) {
488 if (NULL == obj) {
489 return 0;
490 }
reed@google.comb55d1182011-05-11 00:42:04 +0000491
scroggo@google.com4dffc592012-07-17 16:49:40 +0000492 uint32_t writeBufferFlags;
scroggo@google.com565254b2012-06-28 15:41:32 +0000493 if (fFlags & SkGPipeWriter::kCrossProcess_Flag) {
scroggo@google.com4dffc592012-07-17 16:49:40 +0000494 writeBufferFlags = (SkFlattenableWriteBuffer::kInlineFactoryNames_Flag
495 | SkFlattenableWriteBuffer::kCrossProcess_Flag);
scroggo@google.com565254b2012-06-28 15:41:32 +0000496 } else {
497 // Needed for bitmap shaders.
scroggo@google.com4dffc592012-07-17 16:49:40 +0000498 writeBufferFlags = SkFlattenableWriteBuffer::kForceFlattenBitmapPixels_Flag;
scroggo@google.com565254b2012-06-28 15:41:32 +0000499 }
reed@google.comdde09562011-05-23 12:21:05 +0000500
scroggo@google.com4dffc592012-07-17 16:49:40 +0000501 bool added, replaced;
502 const SkFlatData* flat = fFlatDictionary.findAndReplace(
503 *obj, writeBufferFlags, fFlattenableHeap.flatToReplace(), &added, &replaced);
504 int index = flat->index();
505 if (added && this->needOpBytes(flat->flatSize())) {
506 this->writeOp(kDef_Flattenable_DrawOp, paintflat, index);
507 fWriter.write(flat->data(), flat->flatSize());
508 }
509 if (replaced) {
reed@google.comb55d1182011-05-11 00:42:04 +0000510 index = ~index;
reed@google.comb55d1182011-05-11 00:42:04 +0000511 }
scroggo@google.com4dffc592012-07-17 16:49:40 +0000512 return index;
reed@google.comb55d1182011-05-11 00:42:04 +0000513}
514
reed@google.combb6992a2011-04-26 17:41:56 +0000515///////////////////////////////////////////////////////////////////////////////
516
reed@google.comacd471f2011-05-03 21:26:46 +0000517#define MIN_BLOCK_SIZE (16 * 1024)
scroggo@google.com4dffc592012-07-17 16:49:40 +0000518#define BITMAPS_TO_KEEP 5
519#define FLATTENABLES_TO_KEEP 10
reed@google.combb6992a2011-04-26 17:41:56 +0000520
reed@google.comacd471f2011-05-03 21:26:46 +0000521SkGPipeCanvas::SkGPipeCanvas(SkGPipeController* controller,
scroggo@google.com565254b2012-06-28 15:41:32 +0000522 SkWriter32* writer, SkFactorySet* fset, uint32_t flags)
scroggo@google.com4dffc592012-07-17 16:49:40 +0000523: fSharedHeap(!(flags & SkGPipeWriter::kCrossProcess_Flag), controller->numberOfReaders())
524, fWriter(*writer), fFlags(flags)
525, fBitmapHeap(BITMAPS_TO_KEEP), fBitmapDictionary(&fBitmapHeap, NULL, NULL, fset)
526, fFlattenableHeap(FLATTENABLES_TO_KEEP), fFlatDictionary(&fFlattenableHeap, fset) {
reed@google.comacd471f2011-05-03 21:26:46 +0000527 fController = controller;
reed@google.combb6992a2011-04-26 17:41:56 +0000528 fDone = false;
reed@google.comacd471f2011-05-03 21:26:46 +0000529 fBlockSize = 0; // need first block from controller
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000530 fBytesNotified = 0;
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000531 fFirstSaveLayerStackLevel = kNoSaveLayer;
reed@google.comb55d1182011-05-11 00:42:04 +0000532 sk_bzero(fCurrFlatIndex, sizeof(fCurrFlatIndex));
reed@google.comacd471f2011-05-03 21:26:46 +0000533
reed@google.combb6793b2011-05-05 15:18:15 +0000534 // we need a device to limit our clip
535 // should the caller give us the bounds?
yangsu@google.com06b4da12011-06-17 15:04:40 +0000536 // We don't allocate pixels for the bitmap
reed@google.combb6793b2011-05-05 15:18:15 +0000537 SkBitmap bitmap;
538 bitmap.setConfig(SkBitmap::kARGB_8888_Config, 32767, 32767);
yangsu@google.com06b4da12011-06-17 15:04:40 +0000539 SkDevice* device = SkNEW_ARGS(SkDevice, (bitmap));
reed@google.combb6793b2011-05-05 15:18:15 +0000540 this->setDevice(device)->unref();
scroggo@google.com565254b2012-06-28 15:41:32 +0000541 // Tell the reader the appropriate flags to use.
542 if (this->needOpBytes()) {
543 this->writeOp(kReportFlags_DrawOp, fFlags, 0);
544 }
reed@google.combb6992a2011-04-26 17:41:56 +0000545}
546
547SkGPipeCanvas::~SkGPipeCanvas() {
548 this->finish();
reed@google.combb6992a2011-04-26 17:41:56 +0000549}
550
reed@google.comacd471f2011-05-03 21:26:46 +0000551bool SkGPipeCanvas::needOpBytes(size_t needed) {
552 if (fDone) {
553 return false;
554 }
555
556 needed += 4; // size of DrawOp atom
557 if (fWriter.size() + needed > fBlockSize) {
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000558 // Before we wipe out any data that has already been written, read it
559 // out.
560 this->doNotify();
561 size_t blockSize = SkMax32(MIN_BLOCK_SIZE, needed);
562 void* block = fController->requestBlock(blockSize, &fBlockSize);
reed@google.comacd471f2011-05-03 21:26:46 +0000563 if (NULL == block) {
564 fDone = true;
565 return false;
566 }
567 fWriter.reset(block, fBlockSize);
568 fBytesNotified = 0;
569 }
570 return true;
571}
572
reed@google.comf5842f72011-05-04 18:30:04 +0000573uint32_t SkGPipeCanvas::getTypefaceID(SkTypeface* face) {
574 uint32_t id = 0; // 0 means default/null typeface
575 if (face) {
576 id = fTypefaceSet.find(face);
577 if (0 == id) {
578 id = fTypefaceSet.add(face);
579 size_t size = writeTypeface(NULL, face);
580 if (this->needOpBytes(size)) {
reed@google.combb6793b2011-05-05 15:18:15 +0000581 this->writeOp(kDef_Typeface_DrawOp);
reed@google.comf5842f72011-05-04 18:30:04 +0000582 writeTypeface(&fWriter, face);
583 }
584 }
585 }
586 return id;
587}
588
reed@google.combb6992a2011-04-26 17:41:56 +0000589///////////////////////////////////////////////////////////////////////////////
590
reed@google.comacd471f2011-05-03 21:26:46 +0000591#define NOTIFY_SETUP(canvas) \
592 AutoPipeNotify apn(canvas)
593
reed@google.combb6992a2011-04-26 17:41:56 +0000594int SkGPipeCanvas::save(SaveFlags flags) {
reed@google.comacd471f2011-05-03 21:26:46 +0000595 NOTIFY_SETUP(this);
596 if (this->needOpBytes()) {
597 this->writeOp(kSave_DrawOp, 0, flags);
598 }
reed@google.combb6992a2011-04-26 17:41:56 +0000599 return this->INHERITED::save(flags);
600}
601
602int SkGPipeCanvas::saveLayer(const SkRect* bounds, const SkPaint* paint,
603 SaveFlags saveFlags) {
reed@google.comacd471f2011-05-03 21:26:46 +0000604 NOTIFY_SETUP(this);
605 size_t size = 0;
reed@google.combb6992a2011-04-26 17:41:56 +0000606 unsigned opFlags = 0;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000607
reed@google.combb6992a2011-04-26 17:41:56 +0000608 if (bounds) {
609 opFlags |= kSaveLayer_HasBounds_DrawOpFlag;
reed@google.comacd471f2011-05-03 21:26:46 +0000610 size += sizeof(SkRect);
reed@google.combb6992a2011-04-26 17:41:56 +0000611 }
612 if (paint) {
613 opFlags |= kSaveLayer_HasPaint_DrawOpFlag;
reed@google.com31891582011-05-12 03:03:56 +0000614 this->writePaint(*paint);
reed@google.combb6992a2011-04-26 17:41:56 +0000615 }
616
reed@google.comacd471f2011-05-03 21:26:46 +0000617 if (this->needOpBytes(size)) {
618 this->writeOp(kSaveLayer_DrawOp, opFlags, saveFlags);
619 if (bounds) {
620 fWriter.writeRect(*bounds);
621 }
reed@google.combb6992a2011-04-26 17:41:56 +0000622 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000623
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000624 if (kNoSaveLayer == fFirstSaveLayerStackLevel){
625 fFirstSaveLayerStackLevel = this->getSaveCount();
626 }
reed@google.combb6992a2011-04-26 17:41:56 +0000627 // we just pass on the save, so we don't create a layer
628 return this->INHERITED::save(saveFlags);
629}
630
631void SkGPipeCanvas::restore() {
reed@google.comacd471f2011-05-03 21:26:46 +0000632 NOTIFY_SETUP(this);
633 if (this->needOpBytes()) {
634 this->writeOp(kRestore_DrawOp);
635 }
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000636
reed@google.combb6992a2011-04-26 17:41:56 +0000637 this->INHERITED::restore();
junov@chromium.orgfbe9c8f2012-07-18 20:22:52 +0000638
639 if (this->getSaveCount() == fFirstSaveLayerStackLevel){
640 fFirstSaveLayerStackLevel = kNoSaveLayer;
641 }
642}
643
644bool SkGPipeCanvas::isDrawingToLayer() const {
645 return kNoSaveLayer != fFirstSaveLayerStackLevel;
reed@google.combb6992a2011-04-26 17:41:56 +0000646}
647
648bool SkGPipeCanvas::translate(SkScalar dx, SkScalar dy) {
649 if (dx || dy) {
reed@google.comacd471f2011-05-03 21:26:46 +0000650 NOTIFY_SETUP(this);
651 if (this->needOpBytes(2 * sizeof(SkScalar))) {
652 this->writeOp(kTranslate_DrawOp);
653 fWriter.writeScalar(dx);
654 fWriter.writeScalar(dy);
655 }
reed@google.combb6992a2011-04-26 17:41:56 +0000656 }
657 return this->INHERITED::translate(dx, dy);
658}
659
660bool SkGPipeCanvas::scale(SkScalar sx, SkScalar sy) {
661 if (sx || sy) {
reed@google.comacd471f2011-05-03 21:26:46 +0000662 NOTIFY_SETUP(this);
663 if (this->needOpBytes(2 * sizeof(SkScalar))) {
664 this->writeOp(kScale_DrawOp);
665 fWriter.writeScalar(sx);
666 fWriter.writeScalar(sy);
667 }
reed@google.combb6992a2011-04-26 17:41:56 +0000668 }
669 return this->INHERITED::scale(sx, sy);
670}
671
672bool SkGPipeCanvas::rotate(SkScalar degrees) {
673 if (degrees) {
reed@google.comacd471f2011-05-03 21:26:46 +0000674 NOTIFY_SETUP(this);
675 if (this->needOpBytes(sizeof(SkScalar))) {
676 this->writeOp(kRotate_DrawOp);
677 fWriter.writeScalar(degrees);
678 }
reed@google.combb6992a2011-04-26 17:41:56 +0000679 }
680 return this->INHERITED::rotate(degrees);
681}
682
683bool SkGPipeCanvas::skew(SkScalar sx, SkScalar sy) {
684 if (sx || sy) {
reed@google.comacd471f2011-05-03 21:26:46 +0000685 NOTIFY_SETUP(this);
686 if (this->needOpBytes(2 * sizeof(SkScalar))) {
687 this->writeOp(kSkew_DrawOp);
688 fWriter.writeScalar(sx);
689 fWriter.writeScalar(sy);
690 }
reed@google.combb6992a2011-04-26 17:41:56 +0000691 }
692 return this->INHERITED::skew(sx, sy);
693}
694
695bool SkGPipeCanvas::concat(const SkMatrix& matrix) {
696 if (!matrix.isIdentity()) {
reed@google.comacd471f2011-05-03 21:26:46 +0000697 NOTIFY_SETUP(this);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000698 if (this->needOpBytes(matrix.writeToMemory(NULL))) {
reed@google.comacd471f2011-05-03 21:26:46 +0000699 this->writeOp(kConcat_DrawOp);
djsollen@google.com2b2ede32012-04-12 13:24:04 +0000700 fWriter.writeMatrix(matrix);
reed@google.comacd471f2011-05-03 21:26:46 +0000701 }
reed@google.combb6992a2011-04-26 17:41:56 +0000702 }
703 return this->INHERITED::concat(matrix);
704}
705
706void SkGPipeCanvas::setMatrix(const SkMatrix& matrix) {
reed@google.comacd471f2011-05-03 21:26:46 +0000707 NOTIFY_SETUP(this);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000708 if (this->needOpBytes(matrix.writeToMemory(NULL))) {
reed@google.comacd471f2011-05-03 21:26:46 +0000709 this->writeOp(kSetMatrix_DrawOp);
djsollen@google.com2b2ede32012-04-12 13:24:04 +0000710 fWriter.writeMatrix(matrix);
reed@google.comacd471f2011-05-03 21:26:46 +0000711 }
reed@google.combb6992a2011-04-26 17:41:56 +0000712 this->INHERITED::setMatrix(matrix);
713}
714
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000715bool SkGPipeCanvas::clipRect(const SkRect& rect, SkRegion::Op rgnOp,
716 bool doAntiAlias) {
reed@google.comacd471f2011-05-03 21:26:46 +0000717 NOTIFY_SETUP(this);
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000718 if (this->needOpBytes(sizeof(SkRect)) + sizeof(bool)) {
reed@google.comacd471f2011-05-03 21:26:46 +0000719 this->writeOp(kClipRect_DrawOp, 0, rgnOp);
720 fWriter.writeRect(rect);
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000721 fWriter.writeBool(doAntiAlias);
reed@google.comacd471f2011-05-03 21:26:46 +0000722 }
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000723 return this->INHERITED::clipRect(rect, rgnOp, doAntiAlias);
reed@google.combb6992a2011-04-26 17:41:56 +0000724}
725
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000726bool SkGPipeCanvas::clipPath(const SkPath& path, SkRegion::Op rgnOp,
727 bool doAntiAlias) {
reed@google.comacd471f2011-05-03 21:26:46 +0000728 NOTIFY_SETUP(this);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000729 if (this->needOpBytes(path.writeToMemory(NULL)) + sizeof(bool)) {
reed@google.comacd471f2011-05-03 21:26:46 +0000730 this->writeOp(kClipPath_DrawOp, 0, rgnOp);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000731 fWriter.writePath(path);
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000732 fWriter.writeBool(doAntiAlias);
reed@google.comacd471f2011-05-03 21:26:46 +0000733 }
reed@google.combb6992a2011-04-26 17:41:56 +0000734 // we just pass on the bounds of the path
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000735 return this->INHERITED::clipRect(path.getBounds(), rgnOp, doAntiAlias);
reed@google.combb6992a2011-04-26 17:41:56 +0000736}
737
738bool SkGPipeCanvas::clipRegion(const SkRegion& region, SkRegion::Op rgnOp) {
reed@google.comacd471f2011-05-03 21:26:46 +0000739 NOTIFY_SETUP(this);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000740 if (this->needOpBytes(region.writeToMemory(NULL))) {
reed@google.comacd471f2011-05-03 21:26:46 +0000741 this->writeOp(kClipRegion_DrawOp, 0, rgnOp);
djsollen@google.com2b2ede32012-04-12 13:24:04 +0000742 fWriter.writeRegion(region);
reed@google.comacd471f2011-05-03 21:26:46 +0000743 }
reed@google.combb6992a2011-04-26 17:41:56 +0000744 return this->INHERITED::clipRegion(region, rgnOp);
745}
746
747///////////////////////////////////////////////////////////////////////////////
748
749void SkGPipeCanvas::clear(SkColor color) {
reed@google.comacd471f2011-05-03 21:26:46 +0000750 NOTIFY_SETUP(this);
reed@google.combb6992a2011-04-26 17:41:56 +0000751 unsigned flags = 0;
752 if (color) {
753 flags |= kClear_HasColor_DrawOpFlag;
754 }
reed@google.comacd471f2011-05-03 21:26:46 +0000755 if (this->needOpBytes(sizeof(SkColor))) {
756 this->writeOp(kDrawClear_DrawOp, flags, 0);
757 if (color) {
758 fWriter.write32(color);
759 }
reed@google.combb6992a2011-04-26 17:41:56 +0000760 }
761}
762
763void SkGPipeCanvas::drawPaint(const SkPaint& paint) {
reed@google.comacd471f2011-05-03 21:26:46 +0000764 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000765 this->writePaint(paint);
reed@google.comacd471f2011-05-03 21:26:46 +0000766 if (this->needOpBytes()) {
reed@google.com31891582011-05-12 03:03:56 +0000767 this->writeOp(kDrawPaint_DrawOp);
reed@google.comacd471f2011-05-03 21:26:46 +0000768 }
reed@google.combb6992a2011-04-26 17:41:56 +0000769}
770
771void SkGPipeCanvas::drawPoints(PointMode mode, size_t count,
772 const SkPoint pts[], const SkPaint& paint) {
773 if (count) {
reed@google.comacd471f2011-05-03 21:26:46 +0000774 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000775 this->writePaint(paint);
reed@google.comacd471f2011-05-03 21:26:46 +0000776 if (this->needOpBytes(4 + count * sizeof(SkPoint))) {
reed@google.com31891582011-05-12 03:03:56 +0000777 this->writeOp(kDrawPoints_DrawOp, mode, 0);
reed@google.comacd471f2011-05-03 21:26:46 +0000778 fWriter.write32(count);
779 fWriter.write(pts, count * sizeof(SkPoint));
780 }
reed@google.combb6992a2011-04-26 17:41:56 +0000781 }
782}
783
784void SkGPipeCanvas::drawRect(const SkRect& rect, const SkPaint& paint) {
reed@google.comacd471f2011-05-03 21:26:46 +0000785 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000786 this->writePaint(paint);
reed@google.comacd471f2011-05-03 21:26:46 +0000787 if (this->needOpBytes(sizeof(SkRect))) {
reed@google.com31891582011-05-12 03:03:56 +0000788 this->writeOp(kDrawRect_DrawOp);
reed@google.comacd471f2011-05-03 21:26:46 +0000789 fWriter.writeRect(rect);
790 }
reed@google.combb6992a2011-04-26 17:41:56 +0000791}
792
793void SkGPipeCanvas::drawPath(const SkPath& path, const SkPaint& paint) {
reed@google.comacd471f2011-05-03 21:26:46 +0000794 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000795 this->writePaint(paint);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000796 if (this->needOpBytes(path.writeToMemory(NULL))) {
reed@google.com31891582011-05-12 03:03:56 +0000797 this->writeOp(kDrawPath_DrawOp);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000798 fWriter.writePath(path);
reed@google.comacd471f2011-05-03 21:26:46 +0000799 }
reed@google.combb6992a2011-04-26 17:41:56 +0000800}
801
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000802void SkGPipeCanvas::drawBitmap(const SkBitmap& bm, SkScalar left, SkScalar top,
803 const SkPaint* paint) {
scroggo@google.com565254b2012-06-28 15:41:32 +0000804 bool flatten = shouldFlattenBitmaps(fFlags);
805 const void* ptr = 0;
806 int bitmapIndex = 0;
807 if (flatten) {
808 bitmapIndex = this->flattenToIndex(bm);
809 } else {
scroggo@google.com4dffc592012-07-17 16:49:40 +0000810 ptr = fSharedHeap.addBitmap(bm);
scroggo@google.com565254b2012-06-28 15:41:32 +0000811 if (NULL == ptr) {
812 return;
813 }
scroggo@google.com8ae3c7f2012-06-13 17:47:52 +0000814 }
815
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000816 NOTIFY_SETUP(this);
817 if (paint) {
818 this->writePaint(*paint);
819 }
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000820
scroggo@google.com565254b2012-06-28 15:41:32 +0000821 size_t opBytesNeeded = sizeof(SkScalar) * 2 + sizeof(bool);
822 if (!flatten) {
823 opBytesNeeded += sizeof(void*);
824 }
825 if (this->needOpBytes(opBytesNeeded)) {
826 this->writeOp(kDrawBitmap_DrawOp, 0, bitmapIndex);
827 if (!flatten) {
828 fWriter.writePtr(const_cast<void*>(ptr));
829 }
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000830 fWriter.writeBool(paint != NULL);
831 fWriter.writeScalar(left);
832 fWriter.writeScalar(top);
833 }
reed@google.combb6992a2011-04-26 17:41:56 +0000834}
835
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000836void SkGPipeCanvas::drawBitmapRect(const SkBitmap& bm, const SkIRect* src,
837 const SkRect& dst, const SkPaint* paint) {
scroggo@google.com565254b2012-06-28 15:41:32 +0000838 bool flatten = shouldFlattenBitmaps(fFlags);
839 const void* ptr = 0;
840 int bitmapIndex = 0;
841 if (flatten) {
842 bitmapIndex = this->flattenToIndex(bm);
843 } else {
scroggo@google.com4dffc592012-07-17 16:49:40 +0000844 ptr = fSharedHeap.addBitmap(bm);
scroggo@google.com565254b2012-06-28 15:41:32 +0000845 if (NULL == ptr) {
846 return;
847 }
scroggo@google.com8ae3c7f2012-06-13 17:47:52 +0000848 }
849
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000850 NOTIFY_SETUP(this);
851 if (paint) {
852 this->writePaint(*paint);
853 }
854
scroggo@google.com565254b2012-06-28 15:41:32 +0000855 size_t opBytesNeeded = sizeof(SkRect) + sizeof(bool) * 2;
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000856 bool hasSrc = src != NULL;
857 if (hasSrc) {
858 opBytesNeeded += sizeof(int32_t) * 4;
859 }
scroggo@google.com565254b2012-06-28 15:41:32 +0000860 if (!flatten) {
861 opBytesNeeded += sizeof(void*);
862 }
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000863 if (this->needOpBytes(opBytesNeeded)) {
scroggo@google.com565254b2012-06-28 15:41:32 +0000864 this->writeOp(kDrawBitmapRect_DrawOp, 0, bitmapIndex);
865 if (!flatten) {
866 fWriter.writePtr(const_cast<void*>(ptr));
867 }
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000868 fWriter.writeBool(paint != NULL);
869 fWriter.writeBool(hasSrc);
870 if (hasSrc) {
871 fWriter.write32(src->fLeft);
872 fWriter.write32(src->fTop);
873 fWriter.write32(src->fRight);
874 fWriter.write32(src->fBottom);
875 }
876 fWriter.writeRect(dst);
877 }
reed@google.combb6992a2011-04-26 17:41:56 +0000878}
879
880void SkGPipeCanvas::drawBitmapMatrix(const SkBitmap&, const SkMatrix&,
881 const SkPaint*) {
882 UNIMPLEMENTED
883}
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000884
885void SkGPipeCanvas::drawBitmapNine(const SkBitmap& bm, const SkIRect& center,
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000886 const SkRect& dst, const SkPaint* paint) {
scroggo@google.com565254b2012-06-28 15:41:32 +0000887 bool flatten = shouldFlattenBitmaps(fFlags);
888 const void* ptr = 0;
889 int bitmapIndex = 0;
890 if (flatten) {
891 bitmapIndex = this->flattenToIndex(bm);
892 } else {
scroggo@google.com4dffc592012-07-17 16:49:40 +0000893 ptr = fSharedHeap.addBitmap(bm);
scroggo@google.com565254b2012-06-28 15:41:32 +0000894 if (NULL == ptr) {
895 return;
896 }
scroggo@google.com8ae3c7f2012-06-13 17:47:52 +0000897 }
898
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000899 NOTIFY_SETUP(this);
900 if (paint) {
901 this->writePaint(*paint);
902 }
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000903
scroggo@google.com565254b2012-06-28 15:41:32 +0000904 size_t opBytesNeeded = sizeof(int32_t) * 4 + sizeof(bool) + sizeof(SkRect);
905 if (!flatten) {
906 opBytesNeeded += sizeof(void*);
907 }
908 if (this->needOpBytes(opBytesNeeded)) {
909 this->writeOp(kDrawBitmapNine_DrawOp, 0, bitmapIndex);
910 if (!flatten) {
911 fWriter.writePtr(const_cast<void*>(ptr));
912 }
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000913 fWriter.writeBool(paint != NULL);
914 fWriter.write32(center.fLeft);
915 fWriter.write32(center.fTop);
916 fWriter.write32(center.fRight);
917 fWriter.write32(center.fBottom);
918 fWriter.writeRect(dst);
919 }
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000920}
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000921
922void SkGPipeCanvas::drawSprite(const SkBitmap& bm, int left, int top,
923 const SkPaint* paint) {
scroggo@google.com565254b2012-06-28 15:41:32 +0000924 bool flatten = shouldFlattenBitmaps(fFlags);
925 const void* ptr = 0;
926 int bitmapIndex = 0;
927 if (flatten) {
928 bitmapIndex = this->flattenToIndex(bm);
929 } else {
scroggo@google.com4dffc592012-07-17 16:49:40 +0000930 ptr = fSharedHeap.addBitmap(bm);
scroggo@google.com565254b2012-06-28 15:41:32 +0000931 if (NULL == ptr) {
932 return;
933 }
scroggo@google.com8ae3c7f2012-06-13 17:47:52 +0000934 }
935
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000936 NOTIFY_SETUP(this);
937 if (paint) {
938 this->writePaint(*paint);
939 }
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000940
scroggo@google.com565254b2012-06-28 15:41:32 +0000941 size_t opBytesNeeded = sizeof(int32_t) * 2 + sizeof(bool);
942 if (!flatten) {
943 opBytesNeeded += sizeof(void*);
944 }
945 if (this->needOpBytes(opBytesNeeded)) {
946 this->writeOp(kDrawSprite_DrawOp, 0, bitmapIndex);
947 if (!flatten) {
948 fWriter.writePtr(const_cast<void*>(ptr));
949 }
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000950 fWriter.writeBool(paint != NULL);
951 fWriter.write32(left);
952 fWriter.write32(top);
953 }
reed@google.combb6992a2011-04-26 17:41:56 +0000954}
955
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000956void SkGPipeCanvas::drawText(const void* text, size_t byteLength, SkScalar x,
reed@google.combb6992a2011-04-26 17:41:56 +0000957 SkScalar y, const SkPaint& paint) {
958 if (byteLength) {
reed@google.comacd471f2011-05-03 21:26:46 +0000959 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000960 this->writePaint(paint);
reed@google.comacd471f2011-05-03 21:26:46 +0000961 if (this->needOpBytes(4 + SkAlign4(byteLength) + 2 * sizeof(SkScalar))) {
reed@google.com31891582011-05-12 03:03:56 +0000962 this->writeOp(kDrawText_DrawOp);
reed@google.comacd471f2011-05-03 21:26:46 +0000963 fWriter.write32(byteLength);
964 fWriter.writePad(text, byteLength);
965 fWriter.writeScalar(x);
966 fWriter.writeScalar(y);
967 }
reed@google.combb6992a2011-04-26 17:41:56 +0000968 }
969}
970
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000971void SkGPipeCanvas::drawPosText(const void* text, size_t byteLength,
reed@google.combb6992a2011-04-26 17:41:56 +0000972 const SkPoint pos[], const SkPaint& paint) {
973 if (byteLength) {
reed@google.comacd471f2011-05-03 21:26:46 +0000974 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000975 this->writePaint(paint);
reed@google.combb6992a2011-04-26 17:41:56 +0000976 int count = paint.textToGlyphs(text, byteLength, NULL);
reed@google.comacd471f2011-05-03 21:26:46 +0000977 if (this->needOpBytes(4 + SkAlign4(byteLength) + 4 + count * sizeof(SkPoint))) {
reed@google.com31891582011-05-12 03:03:56 +0000978 this->writeOp(kDrawPosText_DrawOp);
reed@google.comacd471f2011-05-03 21:26:46 +0000979 fWriter.write32(byteLength);
980 fWriter.writePad(text, byteLength);
981 fWriter.write32(count);
982 fWriter.write(pos, count * sizeof(SkPoint));
983 }
reed@google.combb6992a2011-04-26 17:41:56 +0000984 }
985}
986
987void SkGPipeCanvas::drawPosTextH(const void* text, size_t byteLength,
988 const SkScalar xpos[], SkScalar constY,
989 const SkPaint& paint) {
990 if (byteLength) {
reed@google.comacd471f2011-05-03 21:26:46 +0000991 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000992 this->writePaint(paint);
reed@google.combb6992a2011-04-26 17:41:56 +0000993 int count = paint.textToGlyphs(text, byteLength, NULL);
reed@google.comacd471f2011-05-03 21:26:46 +0000994 if (this->needOpBytes(4 + SkAlign4(byteLength) + 4 + count * sizeof(SkScalar) + 4)) {
reed@google.com31891582011-05-12 03:03:56 +0000995 this->writeOp(kDrawPosTextH_DrawOp);
reed@google.comacd471f2011-05-03 21:26:46 +0000996 fWriter.write32(byteLength);
997 fWriter.writePad(text, byteLength);
998 fWriter.write32(count);
999 fWriter.write(xpos, count * sizeof(SkScalar));
1000 fWriter.writeScalar(constY);
1001 }
reed@google.combb6992a2011-04-26 17:41:56 +00001002 }
1003}
1004
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001005void SkGPipeCanvas::drawTextOnPath(const void* text, size_t byteLength,
1006 const SkPath& path, const SkMatrix* matrix,
reed@google.combb6992a2011-04-26 17:41:56 +00001007 const SkPaint& paint) {
1008 if (byteLength) {
reed@google.comacd471f2011-05-03 21:26:46 +00001009 NOTIFY_SETUP(this);
reed@google.combb6992a2011-04-26 17:41:56 +00001010 unsigned flags = 0;
djsollen@google.com94e75ee2012-06-08 18:30:46 +00001011 size_t size = 4 + SkAlign4(byteLength) + path.writeToMemory(NULL);
reed@google.combb6992a2011-04-26 17:41:56 +00001012 if (matrix) {
1013 flags |= kDrawTextOnPath_HasMatrix_DrawOpFlag;
djsollen@google.com94e75ee2012-06-08 18:30:46 +00001014 size += matrix->writeToMemory(NULL);
reed@google.combb6992a2011-04-26 17:41:56 +00001015 }
reed@google.com31891582011-05-12 03:03:56 +00001016 this->writePaint(paint);
reed@google.comacd471f2011-05-03 21:26:46 +00001017 if (this->needOpBytes(size)) {
reed@google.com31891582011-05-12 03:03:56 +00001018 this->writeOp(kDrawTextOnPath_DrawOp, flags, 0);
reed@google.combb6992a2011-04-26 17:41:56 +00001019
reed@google.comacd471f2011-05-03 21:26:46 +00001020 fWriter.write32(byteLength);
1021 fWriter.writePad(text, byteLength);
reed@google.combb6992a2011-04-26 17:41:56 +00001022
djsollen@google.com94e75ee2012-06-08 18:30:46 +00001023 fWriter.writePath(path);
reed@google.comacd471f2011-05-03 21:26:46 +00001024 if (matrix) {
djsollen@google.com2b2ede32012-04-12 13:24:04 +00001025 fWriter.writeMatrix(*matrix);
reed@google.comacd471f2011-05-03 21:26:46 +00001026 }
reed@google.combb6992a2011-04-26 17:41:56 +00001027 }
1028 }
1029}
1030
1031void SkGPipeCanvas::drawPicture(SkPicture& picture) {
reed@google.com0faac1e2011-05-11 05:58:58 +00001032 // we want to playback the picture into individual draw calls
1033 this->INHERITED::drawPicture(picture);
reed@google.combb6992a2011-04-26 17:41:56 +00001034}
1035
reed@google.combb6992a2011-04-26 17:41:56 +00001036void SkGPipeCanvas::drawVertices(VertexMode mode, int vertexCount,
1037 const SkPoint vertices[], const SkPoint texs[],
1038 const SkColor colors[], SkXfermode*,
1039 const uint16_t indices[], int indexCount,
1040 const SkPaint& paint) {
1041 if (0 == vertexCount) {
1042 return;
1043 }
1044
reed@google.comacd471f2011-05-03 21:26:46 +00001045 NOTIFY_SETUP(this);
1046 size_t size = 4 + vertexCount * sizeof(SkPoint);
reed@google.com31891582011-05-12 03:03:56 +00001047 this->writePaint(paint);
reed@google.combb6992a2011-04-26 17:41:56 +00001048 unsigned flags = 0;
1049 if (texs) {
1050 flags |= kDrawVertices_HasTexs_DrawOpFlag;
reed@google.comacd471f2011-05-03 21:26:46 +00001051 size += vertexCount * sizeof(SkPoint);
reed@google.combb6992a2011-04-26 17:41:56 +00001052 }
1053 if (colors) {
1054 flags |= kDrawVertices_HasColors_DrawOpFlag;
reed@google.comacd471f2011-05-03 21:26:46 +00001055 size += vertexCount * sizeof(SkColor);
reed@google.combb6992a2011-04-26 17:41:56 +00001056 }
1057 if (indices && indexCount > 0) {
1058 flags |= kDrawVertices_HasIndices_DrawOpFlag;
reed@google.comacd471f2011-05-03 21:26:46 +00001059 size += 4 + SkAlign4(indexCount * sizeof(uint16_t));
reed@google.combb6992a2011-04-26 17:41:56 +00001060 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001061
reed@google.comacd471f2011-05-03 21:26:46 +00001062 if (this->needOpBytes(size)) {
reed@google.com31891582011-05-12 03:03:56 +00001063 this->writeOp(kDrawVertices_DrawOp, flags, 0);
reed@google.comacd471f2011-05-03 21:26:46 +00001064 fWriter.write32(mode);
1065 fWriter.write32(vertexCount);
1066 fWriter.write(vertices, vertexCount * sizeof(SkPoint));
1067 if (texs) {
1068 fWriter.write(texs, vertexCount * sizeof(SkPoint));
1069 }
1070 if (colors) {
1071 fWriter.write(colors, vertexCount * sizeof(SkColor));
1072 }
reed@google.combb6992a2011-04-26 17:41:56 +00001073
reed@google.comacd471f2011-05-03 21:26:46 +00001074 // TODO: flatten xfermode
reed@google.combb6992a2011-04-26 17:41:56 +00001075
reed@google.comacd471f2011-05-03 21:26:46 +00001076 if (indices && indexCount > 0) {
1077 fWriter.write32(indexCount);
1078 fWriter.writePad(indices, indexCount * sizeof(uint16_t));
1079 }
reed@google.combb6992a2011-04-26 17:41:56 +00001080 }
1081}
1082
reed@google.comacd471f2011-05-03 21:26:46 +00001083void SkGPipeCanvas::drawData(const void* ptr, size_t size) {
1084 if (size && ptr) {
1085 NOTIFY_SETUP(this);
reed@google.combb6992a2011-04-26 17:41:56 +00001086 unsigned data = 0;
1087 if (size < (1 << DRAWOPS_DATA_BITS)) {
1088 data = (unsigned)size;
1089 }
reed@google.comacd471f2011-05-03 21:26:46 +00001090 if (this->needOpBytes(4 + SkAlign4(size))) {
1091 this->writeOp(kDrawData_DrawOp, 0, data);
1092 if (0 == data) {
1093 fWriter.write32(size);
1094 }
reed@google.combb6793b2011-05-05 15:18:15 +00001095 fWriter.writePad(ptr, size);
reed@google.combb6992a2011-04-26 17:41:56 +00001096 }
1097 }
1098}
1099
junov@chromium.org77eec242012-07-18 17:54:45 +00001100void SkGPipeCanvas::flushRecording(bool detachCurrentBlock) {
1101 doNotify();
1102 if (detachCurrentBlock) {
1103 // force a new block to be requested for the next recorded command
1104 fBlockSize = 0;
1105 }
1106}
1107
reed@google.combb6992a2011-04-26 17:41:56 +00001108///////////////////////////////////////////////////////////////////////////////
1109
1110template <typename T> uint32_t castToU32(T value) {
1111 union {
1112 T fSrc;
1113 uint32_t fDst;
1114 } data;
1115 data.fSrc = value;
1116 return data.fDst;
1117}
1118
reed@google.com31891582011-05-12 03:03:56 +00001119void SkGPipeCanvas::writePaint(const SkPaint& paint) {
1120 SkPaint& base = fPaint;
reed@google.combb6992a2011-04-26 17:41:56 +00001121 uint32_t storage[32];
1122 uint32_t* ptr = storage;
reed@google.combb6992a2011-04-26 17:41:56 +00001123
1124 if (base.getFlags() != paint.getFlags()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001125 *ptr++ = PaintOp_packOpData(kFlags_PaintOp, paint.getFlags());
reed@google.comf5842f72011-05-04 18:30:04 +00001126 base.setFlags(paint.getFlags());
reed@google.combb6992a2011-04-26 17:41:56 +00001127 }
1128 if (base.getColor() != paint.getColor()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001129 *ptr++ = PaintOp_packOp(kColor_PaintOp);
1130 *ptr++ = paint.getColor();
reed@google.comf5842f72011-05-04 18:30:04 +00001131 base.setColor(paint.getColor());
reed@google.combb6992a2011-04-26 17:41:56 +00001132 }
1133 if (base.getStyle() != paint.getStyle()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001134 *ptr++ = PaintOp_packOpData(kStyle_PaintOp, paint.getStyle());
reed@google.comf5842f72011-05-04 18:30:04 +00001135 base.setStyle(paint.getStyle());
reed@google.combb6992a2011-04-26 17:41:56 +00001136 }
1137 if (base.getStrokeJoin() != paint.getStrokeJoin()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001138 *ptr++ = PaintOp_packOpData(kJoin_PaintOp, paint.getStrokeJoin());
reed@google.comf5842f72011-05-04 18:30:04 +00001139 base.setStrokeJoin(paint.getStrokeJoin());
reed@google.combb6992a2011-04-26 17:41:56 +00001140 }
1141 if (base.getStrokeCap() != paint.getStrokeCap()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001142 *ptr++ = PaintOp_packOpData(kCap_PaintOp, paint.getStrokeCap());
reed@google.comf5842f72011-05-04 18:30:04 +00001143 base.setStrokeCap(paint.getStrokeCap());
reed@google.combb6992a2011-04-26 17:41:56 +00001144 }
1145 if (base.getStrokeWidth() != paint.getStrokeWidth()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001146 *ptr++ = PaintOp_packOp(kWidth_PaintOp);
1147 *ptr++ = castToU32(paint.getStrokeWidth());
reed@google.comf5842f72011-05-04 18:30:04 +00001148 base.setStrokeWidth(paint.getStrokeWidth());
reed@google.combb6992a2011-04-26 17:41:56 +00001149 }
1150 if (base.getStrokeMiter() != paint.getStrokeMiter()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001151 *ptr++ = PaintOp_packOp(kMiter_PaintOp);
1152 *ptr++ = castToU32(paint.getStrokeMiter());
reed@google.comf5842f72011-05-04 18:30:04 +00001153 base.setStrokeMiter(paint.getStrokeMiter());
reed@google.combb6992a2011-04-26 17:41:56 +00001154 }
1155 if (base.getTextEncoding() != paint.getTextEncoding()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001156 *ptr++ = PaintOp_packOpData(kEncoding_PaintOp, paint.getTextEncoding());
reed@google.comf5842f72011-05-04 18:30:04 +00001157 base.setTextEncoding(paint.getTextEncoding());
reed@google.combb6992a2011-04-26 17:41:56 +00001158 }
1159 if (base.getHinting() != paint.getHinting()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001160 *ptr++ = PaintOp_packOpData(kHinting_PaintOp, paint.getHinting());
reed@google.comf5842f72011-05-04 18:30:04 +00001161 base.setHinting(paint.getHinting());
reed@google.combb6992a2011-04-26 17:41:56 +00001162 }
1163 if (base.getTextAlign() != paint.getTextAlign()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001164 *ptr++ = PaintOp_packOpData(kAlign_PaintOp, paint.getTextAlign());
reed@google.comf5842f72011-05-04 18:30:04 +00001165 base.setTextAlign(paint.getTextAlign());
reed@google.combb6992a2011-04-26 17:41:56 +00001166 }
1167 if (base.getTextSize() != paint.getTextSize()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001168 *ptr++ = PaintOp_packOp(kTextSize_PaintOp);
1169 *ptr++ = castToU32(paint.getTextSize());
reed@google.comf5842f72011-05-04 18:30:04 +00001170 base.setTextSize(paint.getTextSize());
reed@google.combb6992a2011-04-26 17:41:56 +00001171 }
1172 if (base.getTextScaleX() != paint.getTextScaleX()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001173 *ptr++ = PaintOp_packOp(kTextScaleX_PaintOp);
1174 *ptr++ = castToU32(paint.getTextScaleX());
reed@google.comf5842f72011-05-04 18:30:04 +00001175 base.setTextScaleX(paint.getTextScaleX());
reed@google.combb6992a2011-04-26 17:41:56 +00001176 }
1177 if (base.getTextSkewX() != paint.getTextSkewX()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001178 *ptr++ = PaintOp_packOp(kTextSkewX_PaintOp);
1179 *ptr++ = castToU32(paint.getTextSkewX());
reed@google.comf5842f72011-05-04 18:30:04 +00001180 base.setTextSkewX(paint.getTextSkewX());
1181 }
1182
1183 if (!SkTypeface::Equal(base.getTypeface(), paint.getTypeface())) {
1184 uint32_t id = this->getTypefaceID(paint.getTypeface());
reed@google.comf5842f72011-05-04 18:30:04 +00001185 *ptr++ = PaintOp_packOpData(kTypeface_PaintOp, id);
1186 base.setTypeface(paint.getTypeface());
reed@google.combb6992a2011-04-26 17:41:56 +00001187 }
reed@google.combb6992a2011-04-26 17:41:56 +00001188
scroggo@google.com4dffc592012-07-17 16:49:40 +00001189 // This is a new paint, so all old flats can be safely purged, if necessary.
1190 fFlattenableHeap.markAllFlatsSafeToDelete();
reed@google.comb55d1182011-05-11 00:42:04 +00001191 for (int i = 0; i < kCount_PaintFlats; i++) {
1192 int index = this->flattenToIndex(get_paintflat(paint, i), (PaintFlats)i);
scroggo@google.comd3ba5cc2012-07-09 16:05:53 +00001193 bool replaced = index < 0;
1194 if (replaced) {
1195 index = ~index;
1196 }
scroggo@google.com4dffc592012-07-17 16:49:40 +00001197 // Store the index of any flat that needs to be kept. 0 means no flat.
1198 if (index > 0) {
1199 fFlattenableHeap.markFlatForKeeping(index);
1200 }
1201 SkASSERT(index >= 0 && index <= fFlatDictionary.count());
scroggo@google.comd3ba5cc2012-07-09 16:05:53 +00001202 if (index != fCurrFlatIndex[i] || replaced) {
reed@google.comb55d1182011-05-11 00:42:04 +00001203 *ptr++ = PaintOp_packOpFlagData(kFlatIndex_PaintOp, i, index);
1204 fCurrFlatIndex[i] = index;
1205 }
1206 }
1207
reed@google.comacd471f2011-05-03 21:26:46 +00001208 size_t size = (char*)ptr - (char*)storage;
1209 if (size && this->needOpBytes(size)) {
reed@google.comb55d1182011-05-11 00:42:04 +00001210 this->writeOp(kPaintOp_DrawOp, 0, size);
reed@google.comb55d1182011-05-11 00:42:04 +00001211 fWriter.write(storage, size);
reed@google.combb6992a2011-04-26 17:41:56 +00001212 for (size_t i = 0; i < size/4; i++) {
reed@google.comb55d1182011-05-11 00:42:04 +00001213// SkDebugf("[%d] %08X\n", i, storage[i]);
reed@google.combb6992a2011-04-26 17:41:56 +00001214 }
1215 }
reed@google.combb6992a2011-04-26 17:41:56 +00001216}
1217
1218///////////////////////////////////////////////////////////////////////////////
1219
1220#include "SkGPipe.h"
1221
reed@google.comacd471f2011-05-03 21:26:46 +00001222SkGPipeWriter::SkGPipeWriter() : fWriter(0) {
reed@google.combb6992a2011-04-26 17:41:56 +00001223 fCanvas = NULL;
1224}
1225
1226SkGPipeWriter::~SkGPipeWriter() {
reed@google.comacd471f2011-05-03 21:26:46 +00001227 this->endRecording();
reed@google.combb6992a2011-04-26 17:41:56 +00001228 SkSafeUnref(fCanvas);
1229}
1230
scroggo@google.com565254b2012-06-28 15:41:32 +00001231SkCanvas* SkGPipeWriter::startRecording(SkGPipeController* controller, uint32_t flags) {
reed@google.combb6992a2011-04-26 17:41:56 +00001232 if (NULL == fCanvas) {
reed@google.comacd471f2011-05-03 21:26:46 +00001233 fWriter.reset(NULL, 0);
reed@google.comdde09562011-05-23 12:21:05 +00001234 fFactorySet.reset();
1235 fCanvas = SkNEW_ARGS(SkGPipeCanvas, (controller, &fWriter,
1236 (flags & kCrossProcess_Flag) ?
scroggo@google.com565254b2012-06-28 15:41:32 +00001237 &fFactorySet : NULL, flags));
reed@google.combb6992a2011-04-26 17:41:56 +00001238 }
1239 return fCanvas;
1240}
1241
1242void SkGPipeWriter::endRecording() {
1243 if (fCanvas) {
1244 fCanvas->finish();
1245 fCanvas->unref();
1246 fCanvas = NULL;
1247 }
1248}
1249
junov@chromium.org77eec242012-07-18 17:54:45 +00001250void SkGPipeWriter::flushRecording(bool detachCurrentBlock){
1251 fCanvas->flushRecording(detachCurrentBlock);
1252}
1253