blob: 900716336580537c979c58ba4a16f82b0fd06bb2 [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
353 // overrides from SkCanvas
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000354 virtual int save(SaveFlags) SK_OVERRIDE;
355 virtual int saveLayer(const SkRect* bounds, const SkPaint*,
356 SaveFlags) SK_OVERRIDE;
357 virtual void restore() SK_OVERRIDE;
358 virtual bool translate(SkScalar dx, SkScalar dy) SK_OVERRIDE;
359 virtual bool scale(SkScalar sx, SkScalar sy) SK_OVERRIDE;
360 virtual bool rotate(SkScalar degrees) SK_OVERRIDE;
361 virtual bool skew(SkScalar sx, SkScalar sy) SK_OVERRIDE;
362 virtual bool concat(const SkMatrix& matrix) SK_OVERRIDE;
363 virtual void setMatrix(const SkMatrix& matrix) SK_OVERRIDE;
364 virtual bool clipRect(const SkRect& rect, SkRegion::Op op,
365 bool doAntiAlias = false) SK_OVERRIDE;
366 virtual bool clipPath(const SkPath& path, SkRegion::Op op,
367 bool doAntiAlias = false) SK_OVERRIDE;
368 virtual bool clipRegion(const SkRegion& region, SkRegion::Op op) SK_OVERRIDE;
369 virtual void clear(SkColor) SK_OVERRIDE;
370 virtual void drawPaint(const SkPaint& paint) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000371 virtual void drawPoints(PointMode, size_t count, const SkPoint pts[],
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000372 const SkPaint&) SK_OVERRIDE;
373 virtual void drawRect(const SkRect& rect, const SkPaint&) SK_OVERRIDE;
374 virtual void drawPath(const SkPath& path, const SkPaint&) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000375 virtual void drawBitmap(const SkBitmap&, SkScalar left, SkScalar top,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000376 const SkPaint*) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000377 virtual void drawBitmapRect(const SkBitmap&, const SkIRect* src,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000378 const SkRect& dst, const SkPaint*) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000379 virtual void drawBitmapMatrix(const SkBitmap&, const SkMatrix&,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000380 const SkPaint*) SK_OVERRIDE;
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000381 virtual void drawBitmapNine(const SkBitmap& bitmap, const SkIRect& center,
382 const SkRect& dst, const SkPaint* paint = NULL) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000383 virtual void drawSprite(const SkBitmap&, int left, int top,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000384 const SkPaint*) SK_OVERRIDE;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000385 virtual void drawText(const void* text, size_t byteLength, SkScalar x,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000386 SkScalar y, const SkPaint&) SK_OVERRIDE;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000387 virtual void drawPosText(const void* text, size_t byteLength,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000388 const SkPoint pos[], const SkPaint&) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000389 virtual void drawPosTextH(const void* text, size_t byteLength,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000390 const SkScalar xpos[], SkScalar constY,
391 const SkPaint&) SK_OVERRIDE;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000392 virtual void drawTextOnPath(const void* text, size_t byteLength,
393 const SkPath& path, const SkMatrix* matrix,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000394 const SkPaint&) SK_OVERRIDE;
395 virtual void drawPicture(SkPicture& picture) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000396 virtual void drawVertices(VertexMode, int vertexCount,
397 const SkPoint vertices[], const SkPoint texs[],
398 const SkColor colors[], SkXfermode*,
399 const uint16_t indices[], int indexCount,
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000400 const SkPaint&) SK_OVERRIDE;
401 virtual void drawData(const void*, size_t) SK_OVERRIDE;
reed@google.combb6992a2011-04-26 17:41:56 +0000402
403private:
scroggo@google.com4dffc592012-07-17 16:49:40 +0000404 SharedHeap fSharedHeap;
reed@google.comacd471f2011-05-03 21:26:46 +0000405 SkGPipeController* fController;
reed@google.combb6992a2011-04-26 17:41:56 +0000406 SkWriter32& fWriter;
reed@google.comacd471f2011-05-03 21:26:46 +0000407 size_t fBlockSize; // amount allocated for writer
408 size_t fBytesNotified;
reed@google.combb6992a2011-04-26 17:41:56 +0000409 bool fDone;
scroggo@google.com565254b2012-06-28 15:41:32 +0000410 uint32_t fFlags;
reed@google.combb6992a2011-04-26 17:41:56 +0000411
reed@google.comf5842f72011-05-04 18:30:04 +0000412 SkRefCntSet fTypefaceSet;
413
414 uint32_t getTypefaceID(SkTypeface*);
415
reed@google.comacd471f2011-05-03 21:26:46 +0000416 inline void writeOp(DrawOps op, unsigned flags, unsigned data) {
reed@google.combb6992a2011-04-26 17:41:56 +0000417 fWriter.write32(DrawOp_packOpFlagData(op, flags, data));
418 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000419
reed@google.comacd471f2011-05-03 21:26:46 +0000420 inline void writeOp(DrawOps op) {
reed@google.combb6992a2011-04-26 17:41:56 +0000421 fWriter.write32(DrawOp_packOpFlagData(op, 0, 0));
422 }
reed@google.comacd471f2011-05-03 21:26:46 +0000423
424 bool needOpBytes(size_t size = 0);
425
426 inline void doNotify() {
427 if (!fDone) {
428 size_t bytes = fWriter.size() - fBytesNotified;
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000429 if (bytes > 0) {
430 fController->notifyWritten(bytes);
431 fBytesNotified += bytes;
432 }
reed@google.comacd471f2011-05-03 21:26:46 +0000433 }
434 }
reed@google.comb55d1182011-05-11 00:42:04 +0000435
scroggo@google.com4dffc592012-07-17 16:49:40 +0000436 // These are only used when in cross process, but with no shared address
437 // space, so bitmaps are flattened.
438 FlattenableHeap fBitmapHeap;
439 SkBitmapDictionary fBitmapDictionary;
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000440 int flattenToIndex(const SkBitmap&);
441
scroggo@google.com4dffc592012-07-17 16:49:40 +0000442 FlattenableHeap fFlattenableHeap;
443 FlatDictionary fFlatDictionary;
reed@google.comb55d1182011-05-11 00:42:04 +0000444 int fCurrFlatIndex[kCount_PaintFlats];
445 int flattenToIndex(SkFlattenable* obj, PaintFlats);
446
reed@google.com31891582011-05-12 03:03:56 +0000447 SkPaint fPaint;
448 void writePaint(const SkPaint&);
reed@google.combb6992a2011-04-26 17:41:56 +0000449
reed@google.comacd471f2011-05-03 21:26:46 +0000450 class AutoPipeNotify {
451 public:
452 AutoPipeNotify(SkGPipeCanvas* canvas) : fCanvas(canvas) {}
453 ~AutoPipeNotify() { fCanvas->doNotify(); }
454 private:
455 SkGPipeCanvas* fCanvas;
456 };
457 friend class AutoPipeNotify;
458
reed@google.combb6992a2011-04-26 17:41:56 +0000459 typedef SkCanvas INHERITED;
460};
461
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000462int SkGPipeCanvas::flattenToIndex(const SkBitmap & bitmap) {
scroggo@google.com565254b2012-06-28 15:41:32 +0000463 SkASSERT(shouldFlattenBitmaps(fFlags));
scroggo@google.com4dffc592012-07-17 16:49:40 +0000464 uint32_t flags = SkFlattenableWriteBuffer::kInlineFactoryNames_Flag
465 | SkFlattenableWriteBuffer::kCrossProcess_Flag;
466 bool added, replaced;
467 const SkFlatData* flat = fBitmapDictionary.findAndReplace(
468 bitmap, flags, fBitmapHeap.flatToReplace(), &added, &replaced);
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000469
scroggo@google.com4dffc592012-07-17 16:49:40 +0000470 int index = flat->index();
471 if (added && this->needOpBytes(flat->flatSize())) {
472 this->writeOp(kDef_Bitmap_DrawOp, 0, index);
473 fWriter.write(flat->data(), flat->flatSize());
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000474 }
scroggo@google.com4dffc592012-07-17 16:49:40 +0000475 return index;
scroggo@google.comd3ba5cc2012-07-09 16:05:53 +0000476}
477
reed@google.comb55d1182011-05-11 00:42:04 +0000478// return 0 for NULL (or unflattenable obj), or index-base-1
scroggo@google.comd3ba5cc2012-07-09 16:05:53 +0000479// return ~(index-base-1) if an old flattenable was replaced
reed@google.comb55d1182011-05-11 00:42:04 +0000480int SkGPipeCanvas::flattenToIndex(SkFlattenable* obj, PaintFlats paintflat) {
481 if (NULL == obj) {
482 return 0;
483 }
reed@google.comb55d1182011-05-11 00:42:04 +0000484
scroggo@google.com4dffc592012-07-17 16:49:40 +0000485 uint32_t writeBufferFlags;
scroggo@google.com565254b2012-06-28 15:41:32 +0000486 if (fFlags & SkGPipeWriter::kCrossProcess_Flag) {
scroggo@google.com4dffc592012-07-17 16:49:40 +0000487 writeBufferFlags = (SkFlattenableWriteBuffer::kInlineFactoryNames_Flag
488 | SkFlattenableWriteBuffer::kCrossProcess_Flag);
scroggo@google.com565254b2012-06-28 15:41:32 +0000489 } else {
490 // Needed for bitmap shaders.
scroggo@google.com4dffc592012-07-17 16:49:40 +0000491 writeBufferFlags = SkFlattenableWriteBuffer::kForceFlattenBitmapPixels_Flag;
scroggo@google.com565254b2012-06-28 15:41:32 +0000492 }
reed@google.comdde09562011-05-23 12:21:05 +0000493
scroggo@google.com4dffc592012-07-17 16:49:40 +0000494 bool added, replaced;
495 const SkFlatData* flat = fFlatDictionary.findAndReplace(
496 *obj, writeBufferFlags, fFlattenableHeap.flatToReplace(), &added, &replaced);
497 int index = flat->index();
498 if (added && this->needOpBytes(flat->flatSize())) {
499 this->writeOp(kDef_Flattenable_DrawOp, paintflat, index);
500 fWriter.write(flat->data(), flat->flatSize());
501 }
502 if (replaced) {
reed@google.comb55d1182011-05-11 00:42:04 +0000503 index = ~index;
reed@google.comb55d1182011-05-11 00:42:04 +0000504 }
scroggo@google.com4dffc592012-07-17 16:49:40 +0000505 return index;
reed@google.comb55d1182011-05-11 00:42:04 +0000506}
507
reed@google.combb6992a2011-04-26 17:41:56 +0000508///////////////////////////////////////////////////////////////////////////////
509
reed@google.comacd471f2011-05-03 21:26:46 +0000510#define MIN_BLOCK_SIZE (16 * 1024)
scroggo@google.com4dffc592012-07-17 16:49:40 +0000511#define BITMAPS_TO_KEEP 5
512#define FLATTENABLES_TO_KEEP 10
reed@google.combb6992a2011-04-26 17:41:56 +0000513
reed@google.comacd471f2011-05-03 21:26:46 +0000514SkGPipeCanvas::SkGPipeCanvas(SkGPipeController* controller,
scroggo@google.com565254b2012-06-28 15:41:32 +0000515 SkWriter32* writer, SkFactorySet* fset, uint32_t flags)
scroggo@google.com4dffc592012-07-17 16:49:40 +0000516: fSharedHeap(!(flags & SkGPipeWriter::kCrossProcess_Flag), controller->numberOfReaders())
517, fWriter(*writer), fFlags(flags)
518, fBitmapHeap(BITMAPS_TO_KEEP), fBitmapDictionary(&fBitmapHeap, NULL, NULL, fset)
519, fFlattenableHeap(FLATTENABLES_TO_KEEP), fFlatDictionary(&fFlattenableHeap, fset) {
reed@google.comacd471f2011-05-03 21:26:46 +0000520 fController = controller;
reed@google.combb6992a2011-04-26 17:41:56 +0000521 fDone = false;
reed@google.comacd471f2011-05-03 21:26:46 +0000522 fBlockSize = 0; // need first block from controller
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000523 fBytesNotified = 0;
reed@google.comb55d1182011-05-11 00:42:04 +0000524 sk_bzero(fCurrFlatIndex, sizeof(fCurrFlatIndex));
reed@google.comacd471f2011-05-03 21:26:46 +0000525
reed@google.combb6793b2011-05-05 15:18:15 +0000526 // we need a device to limit our clip
527 // should the caller give us the bounds?
yangsu@google.com06b4da162011-06-17 15:04:40 +0000528 // We don't allocate pixels for the bitmap
reed@google.combb6793b2011-05-05 15:18:15 +0000529 SkBitmap bitmap;
530 bitmap.setConfig(SkBitmap::kARGB_8888_Config, 32767, 32767);
yangsu@google.com06b4da162011-06-17 15:04:40 +0000531 SkDevice* device = SkNEW_ARGS(SkDevice, (bitmap));
reed@google.combb6793b2011-05-05 15:18:15 +0000532 this->setDevice(device)->unref();
scroggo@google.com565254b2012-06-28 15:41:32 +0000533 // Tell the reader the appropriate flags to use.
534 if (this->needOpBytes()) {
535 this->writeOp(kReportFlags_DrawOp, fFlags, 0);
536 }
reed@google.combb6992a2011-04-26 17:41:56 +0000537}
538
539SkGPipeCanvas::~SkGPipeCanvas() {
540 this->finish();
reed@google.combb6992a2011-04-26 17:41:56 +0000541}
542
reed@google.comacd471f2011-05-03 21:26:46 +0000543bool SkGPipeCanvas::needOpBytes(size_t needed) {
544 if (fDone) {
545 return false;
546 }
547
548 needed += 4; // size of DrawOp atom
549 if (fWriter.size() + needed > fBlockSize) {
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000550 // Before we wipe out any data that has already been written, read it
551 // out.
552 this->doNotify();
553 size_t blockSize = SkMax32(MIN_BLOCK_SIZE, needed);
554 void* block = fController->requestBlock(blockSize, &fBlockSize);
reed@google.comacd471f2011-05-03 21:26:46 +0000555 if (NULL == block) {
556 fDone = true;
557 return false;
558 }
559 fWriter.reset(block, fBlockSize);
560 fBytesNotified = 0;
561 }
562 return true;
563}
564
reed@google.comf5842f72011-05-04 18:30:04 +0000565uint32_t SkGPipeCanvas::getTypefaceID(SkTypeface* face) {
566 uint32_t id = 0; // 0 means default/null typeface
567 if (face) {
568 id = fTypefaceSet.find(face);
569 if (0 == id) {
570 id = fTypefaceSet.add(face);
571 size_t size = writeTypeface(NULL, face);
572 if (this->needOpBytes(size)) {
reed@google.combb6793b2011-05-05 15:18:15 +0000573 this->writeOp(kDef_Typeface_DrawOp);
reed@google.comf5842f72011-05-04 18:30:04 +0000574 writeTypeface(&fWriter, face);
575 }
576 }
577 }
578 return id;
579}
580
reed@google.combb6992a2011-04-26 17:41:56 +0000581///////////////////////////////////////////////////////////////////////////////
582
reed@google.comacd471f2011-05-03 21:26:46 +0000583#define NOTIFY_SETUP(canvas) \
584 AutoPipeNotify apn(canvas)
585
reed@google.combb6992a2011-04-26 17:41:56 +0000586int SkGPipeCanvas::save(SaveFlags flags) {
reed@google.comacd471f2011-05-03 21:26:46 +0000587 NOTIFY_SETUP(this);
588 if (this->needOpBytes()) {
589 this->writeOp(kSave_DrawOp, 0, flags);
590 }
reed@google.combb6992a2011-04-26 17:41:56 +0000591 return this->INHERITED::save(flags);
592}
593
594int SkGPipeCanvas::saveLayer(const SkRect* bounds, const SkPaint* paint,
595 SaveFlags saveFlags) {
reed@google.comacd471f2011-05-03 21:26:46 +0000596 NOTIFY_SETUP(this);
597 size_t size = 0;
reed@google.combb6992a2011-04-26 17:41:56 +0000598 unsigned opFlags = 0;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000599
reed@google.combb6992a2011-04-26 17:41:56 +0000600 if (bounds) {
601 opFlags |= kSaveLayer_HasBounds_DrawOpFlag;
reed@google.comacd471f2011-05-03 21:26:46 +0000602 size += sizeof(SkRect);
reed@google.combb6992a2011-04-26 17:41:56 +0000603 }
604 if (paint) {
605 opFlags |= kSaveLayer_HasPaint_DrawOpFlag;
reed@google.com31891582011-05-12 03:03:56 +0000606 this->writePaint(*paint);
reed@google.combb6992a2011-04-26 17:41:56 +0000607 }
608
reed@google.comacd471f2011-05-03 21:26:46 +0000609 if (this->needOpBytes(size)) {
610 this->writeOp(kSaveLayer_DrawOp, opFlags, saveFlags);
611 if (bounds) {
612 fWriter.writeRect(*bounds);
613 }
reed@google.combb6992a2011-04-26 17:41:56 +0000614 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000615
reed@google.combb6992a2011-04-26 17:41:56 +0000616 // we just pass on the save, so we don't create a layer
617 return this->INHERITED::save(saveFlags);
618}
619
620void SkGPipeCanvas::restore() {
reed@google.comacd471f2011-05-03 21:26:46 +0000621 NOTIFY_SETUP(this);
622 if (this->needOpBytes()) {
623 this->writeOp(kRestore_DrawOp);
624 }
reed@google.combb6992a2011-04-26 17:41:56 +0000625 this->INHERITED::restore();
626}
627
628bool SkGPipeCanvas::translate(SkScalar dx, SkScalar dy) {
629 if (dx || dy) {
reed@google.comacd471f2011-05-03 21:26:46 +0000630 NOTIFY_SETUP(this);
631 if (this->needOpBytes(2 * sizeof(SkScalar))) {
632 this->writeOp(kTranslate_DrawOp);
633 fWriter.writeScalar(dx);
634 fWriter.writeScalar(dy);
635 }
reed@google.combb6992a2011-04-26 17:41:56 +0000636 }
637 return this->INHERITED::translate(dx, dy);
638}
639
640bool SkGPipeCanvas::scale(SkScalar sx, SkScalar sy) {
641 if (sx || sy) {
reed@google.comacd471f2011-05-03 21:26:46 +0000642 NOTIFY_SETUP(this);
643 if (this->needOpBytes(2 * sizeof(SkScalar))) {
644 this->writeOp(kScale_DrawOp);
645 fWriter.writeScalar(sx);
646 fWriter.writeScalar(sy);
647 }
reed@google.combb6992a2011-04-26 17:41:56 +0000648 }
649 return this->INHERITED::scale(sx, sy);
650}
651
652bool SkGPipeCanvas::rotate(SkScalar degrees) {
653 if (degrees) {
reed@google.comacd471f2011-05-03 21:26:46 +0000654 NOTIFY_SETUP(this);
655 if (this->needOpBytes(sizeof(SkScalar))) {
656 this->writeOp(kRotate_DrawOp);
657 fWriter.writeScalar(degrees);
658 }
reed@google.combb6992a2011-04-26 17:41:56 +0000659 }
660 return this->INHERITED::rotate(degrees);
661}
662
663bool SkGPipeCanvas::skew(SkScalar sx, SkScalar sy) {
664 if (sx || sy) {
reed@google.comacd471f2011-05-03 21:26:46 +0000665 NOTIFY_SETUP(this);
666 if (this->needOpBytes(2 * sizeof(SkScalar))) {
667 this->writeOp(kSkew_DrawOp);
668 fWriter.writeScalar(sx);
669 fWriter.writeScalar(sy);
670 }
reed@google.combb6992a2011-04-26 17:41:56 +0000671 }
672 return this->INHERITED::skew(sx, sy);
673}
674
675bool SkGPipeCanvas::concat(const SkMatrix& matrix) {
676 if (!matrix.isIdentity()) {
reed@google.comacd471f2011-05-03 21:26:46 +0000677 NOTIFY_SETUP(this);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000678 if (this->needOpBytes(matrix.writeToMemory(NULL))) {
reed@google.comacd471f2011-05-03 21:26:46 +0000679 this->writeOp(kConcat_DrawOp);
djsollen@google.com2b2ede32012-04-12 13:24:04 +0000680 fWriter.writeMatrix(matrix);
reed@google.comacd471f2011-05-03 21:26:46 +0000681 }
reed@google.combb6992a2011-04-26 17:41:56 +0000682 }
683 return this->INHERITED::concat(matrix);
684}
685
686void SkGPipeCanvas::setMatrix(const SkMatrix& matrix) {
reed@google.comacd471f2011-05-03 21:26:46 +0000687 NOTIFY_SETUP(this);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000688 if (this->needOpBytes(matrix.writeToMemory(NULL))) {
reed@google.comacd471f2011-05-03 21:26:46 +0000689 this->writeOp(kSetMatrix_DrawOp);
djsollen@google.com2b2ede32012-04-12 13:24:04 +0000690 fWriter.writeMatrix(matrix);
reed@google.comacd471f2011-05-03 21:26:46 +0000691 }
reed@google.combb6992a2011-04-26 17:41:56 +0000692 this->INHERITED::setMatrix(matrix);
693}
694
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000695bool SkGPipeCanvas::clipRect(const SkRect& rect, SkRegion::Op rgnOp,
696 bool doAntiAlias) {
reed@google.comacd471f2011-05-03 21:26:46 +0000697 NOTIFY_SETUP(this);
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000698 if (this->needOpBytes(sizeof(SkRect)) + sizeof(bool)) {
reed@google.comacd471f2011-05-03 21:26:46 +0000699 this->writeOp(kClipRect_DrawOp, 0, rgnOp);
700 fWriter.writeRect(rect);
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000701 fWriter.writeBool(doAntiAlias);
reed@google.comacd471f2011-05-03 21:26:46 +0000702 }
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000703 return this->INHERITED::clipRect(rect, rgnOp, doAntiAlias);
reed@google.combb6992a2011-04-26 17:41:56 +0000704}
705
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000706bool SkGPipeCanvas::clipPath(const SkPath& path, SkRegion::Op rgnOp,
707 bool doAntiAlias) {
reed@google.comacd471f2011-05-03 21:26:46 +0000708 NOTIFY_SETUP(this);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000709 if (this->needOpBytes(path.writeToMemory(NULL)) + sizeof(bool)) {
reed@google.comacd471f2011-05-03 21:26:46 +0000710 this->writeOp(kClipPath_DrawOp, 0, rgnOp);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000711 fWriter.writePath(path);
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000712 fWriter.writeBool(doAntiAlias);
reed@google.comacd471f2011-05-03 21:26:46 +0000713 }
reed@google.combb6992a2011-04-26 17:41:56 +0000714 // we just pass on the bounds of the path
scroggo@google.com3b45cd52012-04-18 13:57:47 +0000715 return this->INHERITED::clipRect(path.getBounds(), rgnOp, doAntiAlias);
reed@google.combb6992a2011-04-26 17:41:56 +0000716}
717
718bool SkGPipeCanvas::clipRegion(const SkRegion& region, SkRegion::Op rgnOp) {
reed@google.comacd471f2011-05-03 21:26:46 +0000719 NOTIFY_SETUP(this);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000720 if (this->needOpBytes(region.writeToMemory(NULL))) {
reed@google.comacd471f2011-05-03 21:26:46 +0000721 this->writeOp(kClipRegion_DrawOp, 0, rgnOp);
djsollen@google.com2b2ede32012-04-12 13:24:04 +0000722 fWriter.writeRegion(region);
reed@google.comacd471f2011-05-03 21:26:46 +0000723 }
reed@google.combb6992a2011-04-26 17:41:56 +0000724 return this->INHERITED::clipRegion(region, rgnOp);
725}
726
727///////////////////////////////////////////////////////////////////////////////
728
729void SkGPipeCanvas::clear(SkColor color) {
reed@google.comacd471f2011-05-03 21:26:46 +0000730 NOTIFY_SETUP(this);
reed@google.combb6992a2011-04-26 17:41:56 +0000731 unsigned flags = 0;
732 if (color) {
733 flags |= kClear_HasColor_DrawOpFlag;
734 }
reed@google.comacd471f2011-05-03 21:26:46 +0000735 if (this->needOpBytes(sizeof(SkColor))) {
736 this->writeOp(kDrawClear_DrawOp, flags, 0);
737 if (color) {
738 fWriter.write32(color);
739 }
reed@google.combb6992a2011-04-26 17:41:56 +0000740 }
741}
742
743void SkGPipeCanvas::drawPaint(const SkPaint& paint) {
reed@google.comacd471f2011-05-03 21:26:46 +0000744 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000745 this->writePaint(paint);
reed@google.comacd471f2011-05-03 21:26:46 +0000746 if (this->needOpBytes()) {
reed@google.com31891582011-05-12 03:03:56 +0000747 this->writeOp(kDrawPaint_DrawOp);
reed@google.comacd471f2011-05-03 21:26:46 +0000748 }
reed@google.combb6992a2011-04-26 17:41:56 +0000749}
750
751void SkGPipeCanvas::drawPoints(PointMode mode, size_t count,
752 const SkPoint pts[], const SkPaint& paint) {
753 if (count) {
reed@google.comacd471f2011-05-03 21:26:46 +0000754 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000755 this->writePaint(paint);
reed@google.comacd471f2011-05-03 21:26:46 +0000756 if (this->needOpBytes(4 + count * sizeof(SkPoint))) {
reed@google.com31891582011-05-12 03:03:56 +0000757 this->writeOp(kDrawPoints_DrawOp, mode, 0);
reed@google.comacd471f2011-05-03 21:26:46 +0000758 fWriter.write32(count);
759 fWriter.write(pts, count * sizeof(SkPoint));
760 }
reed@google.combb6992a2011-04-26 17:41:56 +0000761 }
762}
763
764void SkGPipeCanvas::drawRect(const SkRect& rect, const SkPaint& paint) {
reed@google.comacd471f2011-05-03 21:26:46 +0000765 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000766 this->writePaint(paint);
reed@google.comacd471f2011-05-03 21:26:46 +0000767 if (this->needOpBytes(sizeof(SkRect))) {
reed@google.com31891582011-05-12 03:03:56 +0000768 this->writeOp(kDrawRect_DrawOp);
reed@google.comacd471f2011-05-03 21:26:46 +0000769 fWriter.writeRect(rect);
770 }
reed@google.combb6992a2011-04-26 17:41:56 +0000771}
772
773void SkGPipeCanvas::drawPath(const SkPath& path, const SkPaint& paint) {
reed@google.comacd471f2011-05-03 21:26:46 +0000774 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000775 this->writePaint(paint);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000776 if (this->needOpBytes(path.writeToMemory(NULL))) {
reed@google.com31891582011-05-12 03:03:56 +0000777 this->writeOp(kDrawPath_DrawOp);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000778 fWriter.writePath(path);
reed@google.comacd471f2011-05-03 21:26:46 +0000779 }
reed@google.combb6992a2011-04-26 17:41:56 +0000780}
781
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000782void SkGPipeCanvas::drawBitmap(const SkBitmap& bm, SkScalar left, SkScalar top,
783 const SkPaint* paint) {
scroggo@google.com565254b2012-06-28 15:41:32 +0000784 bool flatten = shouldFlattenBitmaps(fFlags);
785 const void* ptr = 0;
786 int bitmapIndex = 0;
787 if (flatten) {
788 bitmapIndex = this->flattenToIndex(bm);
789 } else {
scroggo@google.com4dffc592012-07-17 16:49:40 +0000790 ptr = fSharedHeap.addBitmap(bm);
scroggo@google.com565254b2012-06-28 15:41:32 +0000791 if (NULL == ptr) {
792 return;
793 }
scroggo@google.com8ae3c7f2012-06-13 17:47:52 +0000794 }
795
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000796 NOTIFY_SETUP(this);
797 if (paint) {
798 this->writePaint(*paint);
799 }
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000800
scroggo@google.com565254b2012-06-28 15:41:32 +0000801 size_t opBytesNeeded = sizeof(SkScalar) * 2 + sizeof(bool);
802 if (!flatten) {
803 opBytesNeeded += sizeof(void*);
804 }
805 if (this->needOpBytes(opBytesNeeded)) {
806 this->writeOp(kDrawBitmap_DrawOp, 0, bitmapIndex);
807 if (!flatten) {
808 fWriter.writePtr(const_cast<void*>(ptr));
809 }
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000810 fWriter.writeBool(paint != NULL);
811 fWriter.writeScalar(left);
812 fWriter.writeScalar(top);
813 }
reed@google.combb6992a2011-04-26 17:41:56 +0000814}
815
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000816void SkGPipeCanvas::drawBitmapRect(const SkBitmap& bm, const SkIRect* src,
817 const SkRect& dst, const SkPaint* paint) {
scroggo@google.com565254b2012-06-28 15:41:32 +0000818 bool flatten = shouldFlattenBitmaps(fFlags);
819 const void* ptr = 0;
820 int bitmapIndex = 0;
821 if (flatten) {
822 bitmapIndex = this->flattenToIndex(bm);
823 } else {
scroggo@google.com4dffc592012-07-17 16:49:40 +0000824 ptr = fSharedHeap.addBitmap(bm);
scroggo@google.com565254b2012-06-28 15:41:32 +0000825 if (NULL == ptr) {
826 return;
827 }
scroggo@google.com8ae3c7f2012-06-13 17:47:52 +0000828 }
829
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000830 NOTIFY_SETUP(this);
831 if (paint) {
832 this->writePaint(*paint);
833 }
834
scroggo@google.com565254b2012-06-28 15:41:32 +0000835 size_t opBytesNeeded = sizeof(SkRect) + sizeof(bool) * 2;
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000836 bool hasSrc = src != NULL;
837 if (hasSrc) {
838 opBytesNeeded += sizeof(int32_t) * 4;
839 }
scroggo@google.com565254b2012-06-28 15:41:32 +0000840 if (!flatten) {
841 opBytesNeeded += sizeof(void*);
842 }
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000843 if (this->needOpBytes(opBytesNeeded)) {
scroggo@google.com565254b2012-06-28 15:41:32 +0000844 this->writeOp(kDrawBitmapRect_DrawOp, 0, bitmapIndex);
845 if (!flatten) {
846 fWriter.writePtr(const_cast<void*>(ptr));
847 }
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000848 fWriter.writeBool(paint != NULL);
849 fWriter.writeBool(hasSrc);
850 if (hasSrc) {
851 fWriter.write32(src->fLeft);
852 fWriter.write32(src->fTop);
853 fWriter.write32(src->fRight);
854 fWriter.write32(src->fBottom);
855 }
856 fWriter.writeRect(dst);
857 }
reed@google.combb6992a2011-04-26 17:41:56 +0000858}
859
860void SkGPipeCanvas::drawBitmapMatrix(const SkBitmap&, const SkMatrix&,
861 const SkPaint*) {
862 UNIMPLEMENTED
863}
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000864
865void SkGPipeCanvas::drawBitmapNine(const SkBitmap& bm, const SkIRect& center,
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000866 const SkRect& dst, const SkPaint* paint) {
scroggo@google.com565254b2012-06-28 15:41:32 +0000867 bool flatten = shouldFlattenBitmaps(fFlags);
868 const void* ptr = 0;
869 int bitmapIndex = 0;
870 if (flatten) {
871 bitmapIndex = this->flattenToIndex(bm);
872 } else {
scroggo@google.com4dffc592012-07-17 16:49:40 +0000873 ptr = fSharedHeap.addBitmap(bm);
scroggo@google.com565254b2012-06-28 15:41:32 +0000874 if (NULL == ptr) {
875 return;
876 }
scroggo@google.com8ae3c7f2012-06-13 17:47:52 +0000877 }
878
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000879 NOTIFY_SETUP(this);
880 if (paint) {
881 this->writePaint(*paint);
882 }
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000883
scroggo@google.com565254b2012-06-28 15:41:32 +0000884 size_t opBytesNeeded = sizeof(int32_t) * 4 + sizeof(bool) + sizeof(SkRect);
885 if (!flatten) {
886 opBytesNeeded += sizeof(void*);
887 }
888 if (this->needOpBytes(opBytesNeeded)) {
889 this->writeOp(kDrawBitmapNine_DrawOp, 0, bitmapIndex);
890 if (!flatten) {
891 fWriter.writePtr(const_cast<void*>(ptr));
892 }
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000893 fWriter.writeBool(paint != NULL);
894 fWriter.write32(center.fLeft);
895 fWriter.write32(center.fTop);
896 fWriter.write32(center.fRight);
897 fWriter.write32(center.fBottom);
898 fWriter.writeRect(dst);
899 }
scroggo@google.com5a2e8792012-04-20 17:39:51 +0000900}
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000901
902void SkGPipeCanvas::drawSprite(const SkBitmap& bm, int left, int top,
903 const SkPaint* paint) {
scroggo@google.com565254b2012-06-28 15:41:32 +0000904 bool flatten = shouldFlattenBitmaps(fFlags);
905 const void* ptr = 0;
906 int bitmapIndex = 0;
907 if (flatten) {
908 bitmapIndex = this->flattenToIndex(bm);
909 } else {
scroggo@google.com4dffc592012-07-17 16:49:40 +0000910 ptr = fSharedHeap.addBitmap(bm);
scroggo@google.com565254b2012-06-28 15:41:32 +0000911 if (NULL == ptr) {
912 return;
913 }
scroggo@google.com8ae3c7f2012-06-13 17:47:52 +0000914 }
915
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000916 NOTIFY_SETUP(this);
917 if (paint) {
918 this->writePaint(*paint);
919 }
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000920
scroggo@google.com565254b2012-06-28 15:41:32 +0000921 size_t opBytesNeeded = sizeof(int32_t) * 2 + sizeof(bool);
922 if (!flatten) {
923 opBytesNeeded += sizeof(void*);
924 }
925 if (this->needOpBytes(opBytesNeeded)) {
926 this->writeOp(kDrawSprite_DrawOp, 0, bitmapIndex);
927 if (!flatten) {
928 fWriter.writePtr(const_cast<void*>(ptr));
929 }
scroggo@google.com16d1d0b2012-05-02 19:09:40 +0000930 fWriter.writeBool(paint != NULL);
931 fWriter.write32(left);
932 fWriter.write32(top);
933 }
reed@google.combb6992a2011-04-26 17:41:56 +0000934}
935
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000936void SkGPipeCanvas::drawText(const void* text, size_t byteLength, SkScalar x,
reed@google.combb6992a2011-04-26 17:41:56 +0000937 SkScalar y, const SkPaint& paint) {
938 if (byteLength) {
reed@google.comacd471f2011-05-03 21:26:46 +0000939 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000940 this->writePaint(paint);
reed@google.comacd471f2011-05-03 21:26:46 +0000941 if (this->needOpBytes(4 + SkAlign4(byteLength) + 2 * sizeof(SkScalar))) {
reed@google.com31891582011-05-12 03:03:56 +0000942 this->writeOp(kDrawText_DrawOp);
reed@google.comacd471f2011-05-03 21:26:46 +0000943 fWriter.write32(byteLength);
944 fWriter.writePad(text, byteLength);
945 fWriter.writeScalar(x);
946 fWriter.writeScalar(y);
947 }
reed@google.combb6992a2011-04-26 17:41:56 +0000948 }
949}
950
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000951void SkGPipeCanvas::drawPosText(const void* text, size_t byteLength,
reed@google.combb6992a2011-04-26 17:41:56 +0000952 const SkPoint pos[], const SkPaint& paint) {
953 if (byteLength) {
reed@google.comacd471f2011-05-03 21:26:46 +0000954 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000955 this->writePaint(paint);
reed@google.combb6992a2011-04-26 17:41:56 +0000956 int count = paint.textToGlyphs(text, byteLength, NULL);
reed@google.comacd471f2011-05-03 21:26:46 +0000957 if (this->needOpBytes(4 + SkAlign4(byteLength) + 4 + count * sizeof(SkPoint))) {
reed@google.com31891582011-05-12 03:03:56 +0000958 this->writeOp(kDrawPosText_DrawOp);
reed@google.comacd471f2011-05-03 21:26:46 +0000959 fWriter.write32(byteLength);
960 fWriter.writePad(text, byteLength);
961 fWriter.write32(count);
962 fWriter.write(pos, count * sizeof(SkPoint));
963 }
reed@google.combb6992a2011-04-26 17:41:56 +0000964 }
965}
966
967void SkGPipeCanvas::drawPosTextH(const void* text, size_t byteLength,
968 const SkScalar xpos[], SkScalar constY,
969 const SkPaint& paint) {
970 if (byteLength) {
reed@google.comacd471f2011-05-03 21:26:46 +0000971 NOTIFY_SETUP(this);
reed@google.com31891582011-05-12 03:03:56 +0000972 this->writePaint(paint);
reed@google.combb6992a2011-04-26 17:41:56 +0000973 int count = paint.textToGlyphs(text, byteLength, NULL);
reed@google.comacd471f2011-05-03 21:26:46 +0000974 if (this->needOpBytes(4 + SkAlign4(byteLength) + 4 + count * sizeof(SkScalar) + 4)) {
reed@google.com31891582011-05-12 03:03:56 +0000975 this->writeOp(kDrawPosTextH_DrawOp);
reed@google.comacd471f2011-05-03 21:26:46 +0000976 fWriter.write32(byteLength);
977 fWriter.writePad(text, byteLength);
978 fWriter.write32(count);
979 fWriter.write(xpos, count * sizeof(SkScalar));
980 fWriter.writeScalar(constY);
981 }
reed@google.combb6992a2011-04-26 17:41:56 +0000982 }
983}
984
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000985void SkGPipeCanvas::drawTextOnPath(const void* text, size_t byteLength,
986 const SkPath& path, const SkMatrix* matrix,
reed@google.combb6992a2011-04-26 17:41:56 +0000987 const SkPaint& paint) {
988 if (byteLength) {
reed@google.comacd471f2011-05-03 21:26:46 +0000989 NOTIFY_SETUP(this);
reed@google.combb6992a2011-04-26 17:41:56 +0000990 unsigned flags = 0;
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000991 size_t size = 4 + SkAlign4(byteLength) + path.writeToMemory(NULL);
reed@google.combb6992a2011-04-26 17:41:56 +0000992 if (matrix) {
993 flags |= kDrawTextOnPath_HasMatrix_DrawOpFlag;
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000994 size += matrix->writeToMemory(NULL);
reed@google.combb6992a2011-04-26 17:41:56 +0000995 }
reed@google.com31891582011-05-12 03:03:56 +0000996 this->writePaint(paint);
reed@google.comacd471f2011-05-03 21:26:46 +0000997 if (this->needOpBytes(size)) {
reed@google.com31891582011-05-12 03:03:56 +0000998 this->writeOp(kDrawTextOnPath_DrawOp, flags, 0);
reed@google.combb6992a2011-04-26 17:41:56 +0000999
reed@google.comacd471f2011-05-03 21:26:46 +00001000 fWriter.write32(byteLength);
1001 fWriter.writePad(text, byteLength);
reed@google.combb6992a2011-04-26 17:41:56 +00001002
djsollen@google.com94e75ee2012-06-08 18:30:46 +00001003 fWriter.writePath(path);
reed@google.comacd471f2011-05-03 21:26:46 +00001004 if (matrix) {
djsollen@google.com2b2ede32012-04-12 13:24:04 +00001005 fWriter.writeMatrix(*matrix);
reed@google.comacd471f2011-05-03 21:26:46 +00001006 }
reed@google.combb6992a2011-04-26 17:41:56 +00001007 }
1008 }
1009}
1010
1011void SkGPipeCanvas::drawPicture(SkPicture& picture) {
reed@google.com0faac1e2011-05-11 05:58:58 +00001012 // we want to playback the picture into individual draw calls
1013 this->INHERITED::drawPicture(picture);
reed@google.combb6992a2011-04-26 17:41:56 +00001014}
1015
reed@google.combb6992a2011-04-26 17:41:56 +00001016void SkGPipeCanvas::drawVertices(VertexMode mode, int vertexCount,
1017 const SkPoint vertices[], const SkPoint texs[],
1018 const SkColor colors[], SkXfermode*,
1019 const uint16_t indices[], int indexCount,
1020 const SkPaint& paint) {
1021 if (0 == vertexCount) {
1022 return;
1023 }
1024
reed@google.comacd471f2011-05-03 21:26:46 +00001025 NOTIFY_SETUP(this);
1026 size_t size = 4 + vertexCount * sizeof(SkPoint);
reed@google.com31891582011-05-12 03:03:56 +00001027 this->writePaint(paint);
reed@google.combb6992a2011-04-26 17:41:56 +00001028 unsigned flags = 0;
1029 if (texs) {
1030 flags |= kDrawVertices_HasTexs_DrawOpFlag;
reed@google.comacd471f2011-05-03 21:26:46 +00001031 size += vertexCount * sizeof(SkPoint);
reed@google.combb6992a2011-04-26 17:41:56 +00001032 }
1033 if (colors) {
1034 flags |= kDrawVertices_HasColors_DrawOpFlag;
reed@google.comacd471f2011-05-03 21:26:46 +00001035 size += vertexCount * sizeof(SkColor);
reed@google.combb6992a2011-04-26 17:41:56 +00001036 }
1037 if (indices && indexCount > 0) {
1038 flags |= kDrawVertices_HasIndices_DrawOpFlag;
reed@google.comacd471f2011-05-03 21:26:46 +00001039 size += 4 + SkAlign4(indexCount * sizeof(uint16_t));
reed@google.combb6992a2011-04-26 17:41:56 +00001040 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001041
reed@google.comacd471f2011-05-03 21:26:46 +00001042 if (this->needOpBytes(size)) {
reed@google.com31891582011-05-12 03:03:56 +00001043 this->writeOp(kDrawVertices_DrawOp, flags, 0);
reed@google.comacd471f2011-05-03 21:26:46 +00001044 fWriter.write32(mode);
1045 fWriter.write32(vertexCount);
1046 fWriter.write(vertices, vertexCount * sizeof(SkPoint));
1047 if (texs) {
1048 fWriter.write(texs, vertexCount * sizeof(SkPoint));
1049 }
1050 if (colors) {
1051 fWriter.write(colors, vertexCount * sizeof(SkColor));
1052 }
reed@google.combb6992a2011-04-26 17:41:56 +00001053
reed@google.comacd471f2011-05-03 21:26:46 +00001054 // TODO: flatten xfermode
reed@google.combb6992a2011-04-26 17:41:56 +00001055
reed@google.comacd471f2011-05-03 21:26:46 +00001056 if (indices && indexCount > 0) {
1057 fWriter.write32(indexCount);
1058 fWriter.writePad(indices, indexCount * sizeof(uint16_t));
1059 }
reed@google.combb6992a2011-04-26 17:41:56 +00001060 }
1061}
1062
reed@google.comacd471f2011-05-03 21:26:46 +00001063void SkGPipeCanvas::drawData(const void* ptr, size_t size) {
1064 if (size && ptr) {
1065 NOTIFY_SETUP(this);
reed@google.combb6992a2011-04-26 17:41:56 +00001066 unsigned data = 0;
1067 if (size < (1 << DRAWOPS_DATA_BITS)) {
1068 data = (unsigned)size;
1069 }
reed@google.comacd471f2011-05-03 21:26:46 +00001070 if (this->needOpBytes(4 + SkAlign4(size))) {
1071 this->writeOp(kDrawData_DrawOp, 0, data);
1072 if (0 == data) {
1073 fWriter.write32(size);
1074 }
reed@google.combb6793b2011-05-05 15:18:15 +00001075 fWriter.writePad(ptr, size);
reed@google.combb6992a2011-04-26 17:41:56 +00001076 }
1077 }
1078}
1079
1080///////////////////////////////////////////////////////////////////////////////
1081
1082template <typename T> uint32_t castToU32(T value) {
1083 union {
1084 T fSrc;
1085 uint32_t fDst;
1086 } data;
1087 data.fSrc = value;
1088 return data.fDst;
1089}
1090
reed@google.com31891582011-05-12 03:03:56 +00001091void SkGPipeCanvas::writePaint(const SkPaint& paint) {
1092 SkPaint& base = fPaint;
reed@google.combb6992a2011-04-26 17:41:56 +00001093 uint32_t storage[32];
1094 uint32_t* ptr = storage;
reed@google.combb6992a2011-04-26 17:41:56 +00001095
1096 if (base.getFlags() != paint.getFlags()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001097 *ptr++ = PaintOp_packOpData(kFlags_PaintOp, paint.getFlags());
reed@google.comf5842f72011-05-04 18:30:04 +00001098 base.setFlags(paint.getFlags());
reed@google.combb6992a2011-04-26 17:41:56 +00001099 }
1100 if (base.getColor() != paint.getColor()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001101 *ptr++ = PaintOp_packOp(kColor_PaintOp);
1102 *ptr++ = paint.getColor();
reed@google.comf5842f72011-05-04 18:30:04 +00001103 base.setColor(paint.getColor());
reed@google.combb6992a2011-04-26 17:41:56 +00001104 }
1105 if (base.getStyle() != paint.getStyle()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001106 *ptr++ = PaintOp_packOpData(kStyle_PaintOp, paint.getStyle());
reed@google.comf5842f72011-05-04 18:30:04 +00001107 base.setStyle(paint.getStyle());
reed@google.combb6992a2011-04-26 17:41:56 +00001108 }
1109 if (base.getStrokeJoin() != paint.getStrokeJoin()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001110 *ptr++ = PaintOp_packOpData(kJoin_PaintOp, paint.getStrokeJoin());
reed@google.comf5842f72011-05-04 18:30:04 +00001111 base.setStrokeJoin(paint.getStrokeJoin());
reed@google.combb6992a2011-04-26 17:41:56 +00001112 }
1113 if (base.getStrokeCap() != paint.getStrokeCap()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001114 *ptr++ = PaintOp_packOpData(kCap_PaintOp, paint.getStrokeCap());
reed@google.comf5842f72011-05-04 18:30:04 +00001115 base.setStrokeCap(paint.getStrokeCap());
reed@google.combb6992a2011-04-26 17:41:56 +00001116 }
1117 if (base.getStrokeWidth() != paint.getStrokeWidth()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001118 *ptr++ = PaintOp_packOp(kWidth_PaintOp);
1119 *ptr++ = castToU32(paint.getStrokeWidth());
reed@google.comf5842f72011-05-04 18:30:04 +00001120 base.setStrokeWidth(paint.getStrokeWidth());
reed@google.combb6992a2011-04-26 17:41:56 +00001121 }
1122 if (base.getStrokeMiter() != paint.getStrokeMiter()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001123 *ptr++ = PaintOp_packOp(kMiter_PaintOp);
1124 *ptr++ = castToU32(paint.getStrokeMiter());
reed@google.comf5842f72011-05-04 18:30:04 +00001125 base.setStrokeMiter(paint.getStrokeMiter());
reed@google.combb6992a2011-04-26 17:41:56 +00001126 }
1127 if (base.getTextEncoding() != paint.getTextEncoding()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001128 *ptr++ = PaintOp_packOpData(kEncoding_PaintOp, paint.getTextEncoding());
reed@google.comf5842f72011-05-04 18:30:04 +00001129 base.setTextEncoding(paint.getTextEncoding());
reed@google.combb6992a2011-04-26 17:41:56 +00001130 }
1131 if (base.getHinting() != paint.getHinting()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001132 *ptr++ = PaintOp_packOpData(kHinting_PaintOp, paint.getHinting());
reed@google.comf5842f72011-05-04 18:30:04 +00001133 base.setHinting(paint.getHinting());
reed@google.combb6992a2011-04-26 17:41:56 +00001134 }
1135 if (base.getTextAlign() != paint.getTextAlign()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001136 *ptr++ = PaintOp_packOpData(kAlign_PaintOp, paint.getTextAlign());
reed@google.comf5842f72011-05-04 18:30:04 +00001137 base.setTextAlign(paint.getTextAlign());
reed@google.combb6992a2011-04-26 17:41:56 +00001138 }
1139 if (base.getTextSize() != paint.getTextSize()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001140 *ptr++ = PaintOp_packOp(kTextSize_PaintOp);
1141 *ptr++ = castToU32(paint.getTextSize());
reed@google.comf5842f72011-05-04 18:30:04 +00001142 base.setTextSize(paint.getTextSize());
reed@google.combb6992a2011-04-26 17:41:56 +00001143 }
1144 if (base.getTextScaleX() != paint.getTextScaleX()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001145 *ptr++ = PaintOp_packOp(kTextScaleX_PaintOp);
1146 *ptr++ = castToU32(paint.getTextScaleX());
reed@google.comf5842f72011-05-04 18:30:04 +00001147 base.setTextScaleX(paint.getTextScaleX());
reed@google.combb6992a2011-04-26 17:41:56 +00001148 }
1149 if (base.getTextSkewX() != paint.getTextSkewX()) {
reed@google.combb6992a2011-04-26 17:41:56 +00001150 *ptr++ = PaintOp_packOp(kTextSkewX_PaintOp);
1151 *ptr++ = castToU32(paint.getTextSkewX());
reed@google.comf5842f72011-05-04 18:30:04 +00001152 base.setTextSkewX(paint.getTextSkewX());
1153 }
1154
1155 if (!SkTypeface::Equal(base.getTypeface(), paint.getTypeface())) {
1156 uint32_t id = this->getTypefaceID(paint.getTypeface());
reed@google.comf5842f72011-05-04 18:30:04 +00001157 *ptr++ = PaintOp_packOpData(kTypeface_PaintOp, id);
1158 base.setTypeface(paint.getTypeface());
reed@google.combb6992a2011-04-26 17:41:56 +00001159 }
reed@google.combb6992a2011-04-26 17:41:56 +00001160
scroggo@google.com4dffc592012-07-17 16:49:40 +00001161 // This is a new paint, so all old flats can be safely purged, if necessary.
1162 fFlattenableHeap.markAllFlatsSafeToDelete();
reed@google.comb55d1182011-05-11 00:42:04 +00001163 for (int i = 0; i < kCount_PaintFlats; i++) {
1164 int index = this->flattenToIndex(get_paintflat(paint, i), (PaintFlats)i);
scroggo@google.comd3ba5cc2012-07-09 16:05:53 +00001165 bool replaced = index < 0;
1166 if (replaced) {
1167 index = ~index;
1168 }
scroggo@google.com4dffc592012-07-17 16:49:40 +00001169 // Store the index of any flat that needs to be kept. 0 means no flat.
1170 if (index > 0) {
1171 fFlattenableHeap.markFlatForKeeping(index);
1172 }
1173 SkASSERT(index >= 0 && index <= fFlatDictionary.count());
scroggo@google.comd3ba5cc2012-07-09 16:05:53 +00001174 if (index != fCurrFlatIndex[i] || replaced) {
reed@google.comb55d1182011-05-11 00:42:04 +00001175 *ptr++ = PaintOp_packOpFlagData(kFlatIndex_PaintOp, i, index);
1176 fCurrFlatIndex[i] = index;
1177 }
1178 }
1179
reed@google.comacd471f2011-05-03 21:26:46 +00001180 size_t size = (char*)ptr - (char*)storage;
1181 if (size && this->needOpBytes(size)) {
reed@google.comb55d1182011-05-11 00:42:04 +00001182 this->writeOp(kPaintOp_DrawOp, 0, size);
reed@google.comb55d1182011-05-11 00:42:04 +00001183 fWriter.write(storage, size);
reed@google.combb6992a2011-04-26 17:41:56 +00001184 for (size_t i = 0; i < size/4; i++) {
reed@google.comb55d1182011-05-11 00:42:04 +00001185// SkDebugf("[%d] %08X\n", i, storage[i]);
reed@google.combb6992a2011-04-26 17:41:56 +00001186 }
1187 }
reed@google.combb6992a2011-04-26 17:41:56 +00001188}
1189
1190///////////////////////////////////////////////////////////////////////////////
1191
1192#include "SkGPipe.h"
1193
reed@google.comacd471f2011-05-03 21:26:46 +00001194SkGPipeWriter::SkGPipeWriter() : fWriter(0) {
reed@google.combb6992a2011-04-26 17:41:56 +00001195 fCanvas = NULL;
1196}
1197
1198SkGPipeWriter::~SkGPipeWriter() {
reed@google.comacd471f2011-05-03 21:26:46 +00001199 this->endRecording();
reed@google.combb6992a2011-04-26 17:41:56 +00001200 SkSafeUnref(fCanvas);
1201}
1202
scroggo@google.com565254b2012-06-28 15:41:32 +00001203SkCanvas* SkGPipeWriter::startRecording(SkGPipeController* controller, uint32_t flags) {
reed@google.combb6992a2011-04-26 17:41:56 +00001204 if (NULL == fCanvas) {
reed@google.comacd471f2011-05-03 21:26:46 +00001205 fWriter.reset(NULL, 0);
reed@google.comdde09562011-05-23 12:21:05 +00001206 fFactorySet.reset();
1207 fCanvas = SkNEW_ARGS(SkGPipeCanvas, (controller, &fWriter,
1208 (flags & kCrossProcess_Flag) ?
scroggo@google.com565254b2012-06-28 15:41:32 +00001209 &fFactorySet : NULL, flags));
reed@google.combb6992a2011-04-26 17:41:56 +00001210 }
1211 return fCanvas;
1212}
1213
1214void SkGPipeWriter::endRecording() {
1215 if (fCanvas) {
1216 fCanvas->finish();
1217 fCanvas->unref();
1218 fCanvas = NULL;
1219 }
1220}
1221