db6c2af4dc5f4fb1a6341eef9a4c669ce6c05cb1
[platform/upstream/libSkiaSharp.git] / src / pipe / SkGPipeWrite.cpp
1
2 /*
3  * 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.
7  */
8
9 #include "SkAnnotation.h"
10 #include "SkBitmapDevice.h"
11 #include "SkBitmapHeap.h"
12 #include "SkCanvas.h"
13 #include "SkColorFilter.h"
14 #include "SkData.h"
15 #include "SkDrawLooper.h"
16 #include "SkGPipe.h"
17 #include "SkGPipePriv.h"
18 #include "SkImageFilter.h"
19 #include "SkMaskFilter.h"
20 #include "SkWriteBuffer.h"
21 #include "SkPaint.h"
22 #include "SkPatchUtils.h"
23 #include "SkPathEffect.h"
24 #include "SkPictureFlat.h"
25 #include "SkPtrRecorder.h"
26 #include "SkRasterizer.h"
27 #include "SkRRect.h"
28 #include "SkShader.h"
29 #include "SkStream.h"
30 #include "SkTextBlob.h"
31 #include "SkTSearch.h"
32 #include "SkTypeface.h"
33 #include "SkWriter32.h"
34
35 enum {
36     kSizeOfFlatRRect = sizeof(SkRect) + 4 * sizeof(SkVector)
37 };
38
39 static bool is_cross_process(uint32_t flags) {
40     return SkToBool(flags & SkGPipeWriter::kCrossProcess_Flag);
41 }
42
43 static SkFlattenable* get_paintflat(const SkPaint& paint, unsigned paintFlat) {
44     SkASSERT(paintFlat < kCount_PaintFlats);
45     switch (paintFlat) {
46         case kColorFilter_PaintFlat:    return paint.getColorFilter();
47         case kDrawLooper_PaintFlat:     return paint.getLooper();
48         case kMaskFilter_PaintFlat:     return paint.getMaskFilter();
49         case kPathEffect_PaintFlat:     return paint.getPathEffect();
50         case kRasterizer_PaintFlat:     return paint.getRasterizer();
51         case kShader_PaintFlat:         return paint.getShader();
52         case kImageFilter_PaintFlat:    return paint.getImageFilter();
53         case kXfermode_PaintFlat:       return paint.getXfermode();
54     }
55     SkDEBUGFAIL("never gets here");
56     return NULL;
57 }
58
59 static size_t writeTypeface(SkWriter32* writer, SkTypeface* typeface) {
60     SkASSERT(typeface);
61     SkDynamicMemoryWStream stream;
62     typeface->serialize(&stream);
63     size_t size = stream.getOffset();
64     if (writer) {
65         writer->write32(SkToU32(size));
66         SkAutoDataUnref data(stream.copyToData());
67         writer->writePad(data->data(), size);
68     }
69     return 4 + SkAlign4(size);
70 }
71
72 ///////////////////////////////////////////////////////////////////////////////
73
74 class FlattenableHeap : public SkFlatController {
75 public:
76     FlattenableHeap(int numFlatsToKeep, SkNamedFactorySet* fset, bool isCrossProcess)
77     : INHERITED(isCrossProcess ? SkWriteBuffer::kCrossProcess_Flag : 0)
78     , fNumFlatsToKeep(numFlatsToKeep) {
79         SkASSERT((isCrossProcess && fset != NULL) || (!isCrossProcess && NULL == fset));
80         if (isCrossProcess) {
81             this->setNamedFactorySet(fset);
82         }
83     }
84
85     ~FlattenableHeap() {
86         fPointers.freeAll();
87     }
88
89     void* allocThrow(size_t bytes) SK_OVERRIDE;
90
91     void unalloc(void* ptr) SK_OVERRIDE;
92
93     void setBitmapStorage(SkBitmapHeap* heap) {
94         this->setBitmapHeap(heap);
95     }
96
97     const SkFlatData* flatToReplace() const;
98
99     // Mark an SkFlatData as one that should not be returned by flatToReplace.
100     // Takes the result of SkFlatData::index() as its parameter.
101     void markFlatForKeeping(int index) {
102         *fFlatsThatMustBeKept.append() = index;
103     }
104
105     void markAllFlatsSafeToDelete() {
106         fFlatsThatMustBeKept.reset();
107     }
108
109 private:
110     // Keep track of the indices (i.e. the result of SkFlatData::index()) of
111     // flats that must be kept, since they are on the current paint.
112     SkTDArray<int>   fFlatsThatMustBeKept;
113     SkTDArray<void*> fPointers;
114     const int        fNumFlatsToKeep;
115
116     typedef SkFlatController INHERITED;
117 };
118
119 void FlattenableHeap::unalloc(void* ptr) {
120     int indexToRemove = fPointers.rfind(ptr);
121     if (indexToRemove >= 0) {
122         sk_free(ptr);
123         fPointers.remove(indexToRemove);
124     }
125 }
126
127 void* FlattenableHeap::allocThrow(size_t bytes) {
128     void* ptr = sk_malloc_throw(bytes);
129     *fPointers.append() = ptr;
130     return ptr;
131 }
132
133 const SkFlatData* FlattenableHeap::flatToReplace() const {
134     // First, determine whether we should replace one.
135     if (fPointers.count() > fNumFlatsToKeep) {
136         // Look through the flattenable heap.
137         // TODO: Return the LRU flat.
138         for (int i = 0; i < fPointers.count(); i++) {
139             SkFlatData* potential = (SkFlatData*)fPointers[i];
140             // Make sure that it is not one that must be kept.
141             bool mustKeep = false;
142             for (int j = 0; j < fFlatsThatMustBeKept.count(); j++) {
143                 if (potential->index() == fFlatsThatMustBeKept[j]) {
144                     mustKeep = true;
145                     break;
146                 }
147             }
148             if (!mustKeep) {
149                 return potential;
150             }
151         }
152     }
153     return NULL;
154 }
155
156 ///////////////////////////////////////////////////////////////////////////////
157
158 struct SkFlattenableTraits {
159     static void Flatten(SkWriteBuffer& buffer, const SkFlattenable& flattenable) {
160         buffer.writeFlattenable(&flattenable);
161     }
162     // No need to define unflatten if we never call it.
163 };
164 typedef SkFlatDictionary<SkFlattenable, SkFlattenableTraits> FlatDictionary;
165
166 ///////////////////////////////////////////////////////////////////////////////
167
168 /**
169  * If SkBitmaps are to be flattened to send to the reader, this class is
170  * provided to the SkBitmapHeap to tell the SkGPipeCanvas to do so.
171  */
172 class BitmapShuttle : public SkBitmapHeap::ExternalStorage {
173 public:
174     BitmapShuttle(SkGPipeCanvas*);
175
176     ~BitmapShuttle();
177
178     bool insert(const SkBitmap& bitmap, int32_t slot) SK_OVERRIDE;
179
180     /**
181      *  Remove the SkGPipeCanvas used for insertion. After this, calls to
182      *  insert will crash.
183      */
184     void removeCanvas();
185
186 private:
187     SkGPipeCanvas*    fCanvas;
188 };
189
190 ///////////////////////////////////////////////////////////////////////////////
191
192 class SkGPipeCanvas : public SkCanvas {
193 public:
194     SkGPipeCanvas(SkGPipeController*, SkWriter32*, uint32_t flags,
195                   uint32_t width, uint32_t height);
196     virtual ~SkGPipeCanvas();
197
198     /**
199      *  Called when nothing else is to be written to the stream. Any repeated
200      *  calls are ignored.
201      *
202      *  @param notifyReaders Whether to send a message to the reader(s) that
203      *      the writer is through sending commands. Should generally be true,
204      *      unless there is an error which prevents further messages from
205      *      being sent.
206      */
207     void finish(bool notifyReaders) {
208         if (fDone) {
209             return;
210         }
211         if (notifyReaders && this->needOpBytes()) {
212             this->writeOp(kDone_DrawOp);
213             this->doNotify();
214         }
215         if (shouldFlattenBitmaps(fFlags)) {
216             // The following circular references exist:
217             // fFlattenableHeap -> fWriteBuffer -> fBitmapStorage -> fExternalStorage -> fCanvas
218             // fBitmapHeap -> fExternalStorage -> fCanvas
219             // fFlattenableHeap -> fBitmapStorage -> fExternalStorage -> fCanvas
220
221             // Break them all by destroying the final link to this SkGPipeCanvas.
222             fBitmapShuttle->removeCanvas();
223         }
224         fDone = true;
225     }
226
227     void flushRecording(bool detachCurrentBlock);
228     size_t freeMemoryIfPossible(size_t bytesToFree);
229
230     size_t storageAllocatedForRecording() {
231         return (NULL == fBitmapHeap) ? 0 : fBitmapHeap->bytesAllocated();
232     }
233
234     void beginCommentGroup(const char* description) SK_OVERRIDE;
235     void addComment(const char* kywd, const char* value) SK_OVERRIDE;
236     void endCommentGroup() SK_OVERRIDE;
237
238     /**
239      * Flatten an SkBitmap to send to the reader, where it will be referenced
240      * according to slot.
241      */
242     bool shuttleBitmap(const SkBitmap&, int32_t slot);
243
244 protected:
245     void willSave() SK_OVERRIDE;
246     SaveLayerStrategy willSaveLayer(const SkRect*, const SkPaint*, SaveFlags) SK_OVERRIDE;
247     void willRestore() SK_OVERRIDE;
248
249     void didConcat(const SkMatrix&) SK_OVERRIDE;
250     void didSetMatrix(const SkMatrix&) SK_OVERRIDE;
251
252     void onDrawDRRect(const SkRRect&, const SkRRect&, const SkPaint&) SK_OVERRIDE;
253     virtual void onDrawText(const void* text, size_t byteLength, SkScalar x, SkScalar y,
254                             const SkPaint&) SK_OVERRIDE;
255     virtual void onDrawPosText(const void* text, size_t byteLength, const SkPoint pos[],
256                                const SkPaint&) SK_OVERRIDE;
257     virtual void onDrawPosTextH(const void* text, size_t byteLength, const SkScalar xpos[],
258                                 SkScalar constY, const SkPaint&) SK_OVERRIDE;
259     virtual void onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& path,
260                                   const SkMatrix* matrix, const SkPaint&) SK_OVERRIDE;
261     virtual void onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
262                                 const SkPaint& paint) SK_OVERRIDE;
263     virtual void onDrawPatch(const SkPoint cubics[12], const SkColor colors[4],
264                              const SkPoint texCoords[4], SkXfermode* xmode,
265                              const SkPaint& paint) SK_OVERRIDE;
266     void onDrawPaint(const SkPaint&) SK_OVERRIDE;
267     void onDrawPoints(PointMode, size_t count, const SkPoint pts[], const SkPaint&) SK_OVERRIDE;
268     void onDrawRect(const SkRect&, const SkPaint&) SK_OVERRIDE;
269     void onDrawOval(const SkRect&, const SkPaint&) SK_OVERRIDE;
270     void onDrawRRect(const SkRRect&, const SkPaint&) SK_OVERRIDE;
271     void onDrawPath(const SkPath&, const SkPaint&) SK_OVERRIDE;
272     void onDrawBitmap(const SkBitmap&, SkScalar left, SkScalar top, const SkPaint*) SK_OVERRIDE;
273     void onDrawBitmapRect(const SkBitmap&, const SkRect* src, const SkRect& dst, const SkPaint*,
274                           DrawBitmapRectFlags flags) SK_OVERRIDE;
275 #if 0
276     // rely on decomposition into bitmap (for now)
277     void onDrawImage(const SkImage*, SkScalar left, SkScalar top, const SkPaint*) SK_OVERRIDE;
278     void onDrawImageRect(const SkImage*, const SkRect* src, const SkRect& dst,
279                          const SkPaint*) SK_OVERRIDE;
280 #endif
281     void onDrawBitmapNine(const SkBitmap&, const SkIRect& center, const SkRect& dst,
282                           const SkPaint*) SK_OVERRIDE;
283     void onDrawSprite(const SkBitmap&, int left, int top, const SkPaint*) SK_OVERRIDE;
284     void onDrawVertices(VertexMode vmode, int vertexCount,
285                         const SkPoint vertices[], const SkPoint texs[],
286                         const SkColor colors[], SkXfermode* xmode,
287                         const uint16_t indices[], int indexCount,
288                         const SkPaint&) SK_OVERRIDE;
289     void onClipRect(const SkRect&, SkRegion::Op, ClipEdgeStyle) SK_OVERRIDE;
290     void onClipRRect(const SkRRect&, SkRegion::Op, ClipEdgeStyle) SK_OVERRIDE;
291     void onClipPath(const SkPath&, SkRegion::Op, ClipEdgeStyle) SK_OVERRIDE;
292     void onClipRegion(const SkRegion&, SkRegion::Op) SK_OVERRIDE;
293
294     void onDrawPicture(const SkPicture*, const SkMatrix*, const SkPaint*) SK_OVERRIDE;
295
296 private:
297     void recordTranslate(const SkMatrix&);
298     void recordScale(const SkMatrix&);
299     void recordConcat(const SkMatrix&);
300
301     SkNamedFactorySet* fFactorySet;
302     SkBitmapHeap*      fBitmapHeap;
303     SkGPipeController* fController;
304     SkWriter32&        fWriter;
305     size_t             fBlockSize; // amount allocated for writer
306     size_t             fBytesNotified;
307     bool               fDone;
308     const uint32_t     fFlags;
309
310     SkRefCntSet        fTypefaceSet;
311
312     uint32_t getTypefaceID(SkTypeface*);
313
314     inline void writeOp(DrawOps op, unsigned flags, unsigned data) {
315         fWriter.write32(DrawOp_packOpFlagData(op, flags, data));
316     }
317
318     inline void writeOp(DrawOps op) {
319         fWriter.write32(DrawOp_packOpFlagData(op, 0, 0));
320     }
321
322     bool needOpBytes(size_t size = 0);
323
324     inline void doNotify() {
325         if (!fDone) {
326             size_t bytes = fWriter.bytesWritten() - fBytesNotified;
327             if (bytes > 0) {
328                 fController->notifyWritten(bytes);
329                 fBytesNotified += bytes;
330             }
331         }
332     }
333
334     typedef SkAutoSTMalloc<128, uint8_t> TypefaceBuffer;
335     size_t getInProcessTypefaces(const SkRefCntSet& typefaceSet, TypefaceBuffer*);
336     size_t getCrossProcessTypefaces(const SkRefCntSet& typefaceSet, TypefaceBuffer*);
337
338     // Should be called after any calls to an SkFlatDictionary::findAndReplace
339     // if a new SkFlatData was added when in cross process mode
340     void flattenFactoryNames();
341
342     FlattenableHeap             fFlattenableHeap;
343     FlatDictionary              fFlatDictionary;
344     SkAutoTUnref<BitmapShuttle> fBitmapShuttle;
345     int                         fCurrFlatIndex[kCount_PaintFlats];
346
347     int flattenToIndex(SkFlattenable* obj, PaintFlats);
348
349     // Common code used by drawBitmap*. Behaves differently depending on the
350     // type of SkBitmapHeap being used, which is determined by the flags used.
351     bool commonDrawBitmap(const SkBitmap& bm, DrawOps op, unsigned flags,
352                           size_t opBytesNeeded, const SkPaint* paint);
353
354     SkPaint fPaint;
355     void writePaint(const SkPaint&);
356
357     class AutoPipeNotify {
358     public:
359         AutoPipeNotify(SkGPipeCanvas* canvas) : fCanvas(canvas) {}
360         ~AutoPipeNotify() { fCanvas->doNotify(); }
361     private:
362         SkGPipeCanvas* fCanvas;
363     };
364     friend class AutoPipeNotify;
365
366     typedef SkCanvas INHERITED;
367 };
368
369 void SkGPipeCanvas::flattenFactoryNames() {
370     const char* name;
371     while ((name = fFactorySet->getNextAddedFactoryName()) != NULL) {
372         size_t len = strlen(name);
373         if (this->needOpBytes(SkWriter32::WriteStringSize(name, len))) {
374             this->writeOp(kDef_Factory_DrawOp);
375             fWriter.writeString(name, len);
376         }
377     }
378 }
379
380 bool SkGPipeCanvas::shuttleBitmap(const SkBitmap& bm, int32_t slot) {
381     SkASSERT(shouldFlattenBitmaps(fFlags));
382     SkWriteBuffer buffer;
383     buffer.setNamedFactoryRecorder(fFactorySet);
384     buffer.writeBitmap(bm);
385     this->flattenFactoryNames();
386     size_t size = buffer.bytesWritten();
387     if (this->needOpBytes(size)) {
388         this->writeOp(kDef_Bitmap_DrawOp, 0, slot);
389         void* dst = static_cast<void*>(fWriter.reserve(size));
390         buffer.writeToMemory(dst);
391         return true;
392     }
393     return false;
394 }
395
396 // return 0 for NULL (or unflattenable obj), or index-base-1
397 // return ~(index-base-1) if an old flattenable was replaced
398 int SkGPipeCanvas::flattenToIndex(SkFlattenable* obj, PaintFlats paintflat) {
399     SkASSERT(!fDone && fBitmapHeap != NULL);
400     if (NULL == obj) {
401         return 0;
402     }
403
404     fBitmapHeap->deferAddingOwners();
405     bool added, replaced;
406     const SkFlatData* flat = fFlatDictionary.findAndReplace(*obj, fFlattenableHeap.flatToReplace(),
407                                                             &added, &replaced);
408     fBitmapHeap->endAddingOwnersDeferral(added);
409     int index = flat->index();
410     if (added) {
411         if (is_cross_process(fFlags)) {
412             this->flattenFactoryNames();
413         }
414         size_t flatSize = flat->flatSize();
415         if (this->needOpBytes(flatSize)) {
416             this->writeOp(kDef_Flattenable_DrawOp, paintflat, index);
417             fWriter.write(flat->data(), flatSize);
418         }
419     }
420     if (replaced) {
421         index = ~index;
422     }
423     return index;
424 }
425
426 ///////////////////////////////////////////////////////////////////////////////
427
428 #define MIN_BLOCK_SIZE  (16 * 1024)
429 #define BITMAPS_TO_KEEP 5
430 #define FLATTENABLES_TO_KEEP 10
431
432 SkGPipeCanvas::SkGPipeCanvas(SkGPipeController* controller,
433                              SkWriter32* writer, uint32_t flags,
434                              uint32_t width, uint32_t height)
435     : SkCanvas(width, height)
436     , fFactorySet(is_cross_process(flags) ? SkNEW(SkNamedFactorySet) : NULL)
437     , fWriter(*writer)
438     , fFlags(flags)
439     , fFlattenableHeap(FLATTENABLES_TO_KEEP, fFactorySet, is_cross_process(flags))
440     , fFlatDictionary(&fFlattenableHeap)
441 {
442     fController = controller;
443     fDone = false;
444     fBlockSize = 0; // need first block from controller
445     fBytesNotified = 0;
446     sk_bzero(fCurrFlatIndex, sizeof(fCurrFlatIndex));
447
448     // Tell the reader the appropriate flags to use.
449     if (this->needOpBytes()) {
450         this->writeOp(kReportFlags_DrawOp, fFlags, 0);
451     }
452
453     if (shouldFlattenBitmaps(flags)) {
454         fBitmapShuttle.reset(SkNEW_ARGS(BitmapShuttle, (this)));
455         fBitmapHeap = SkNEW_ARGS(SkBitmapHeap, (fBitmapShuttle.get(), BITMAPS_TO_KEEP));
456     } else {
457         fBitmapHeap = SkNEW_ARGS(SkBitmapHeap,
458                                  (BITMAPS_TO_KEEP, controller->numberOfReaders()));
459         if (this->needOpBytes(sizeof(void*))) {
460             this->writeOp(kShareBitmapHeap_DrawOp);
461             fWriter.writePtr(static_cast<void*>(fBitmapHeap));
462         }
463     }
464     fFlattenableHeap.setBitmapStorage(fBitmapHeap);
465     this->doNotify();
466 }
467
468 SkGPipeCanvas::~SkGPipeCanvas() {
469     this->finish(true);
470     SkSafeUnref(fFactorySet);
471     SkSafeUnref(fBitmapHeap);
472 }
473
474 bool SkGPipeCanvas::needOpBytes(size_t needed) {
475     if (fDone) {
476         return false;
477     }
478
479     needed += 4;  // size of DrawOp atom
480     needed = SkAlign4(needed);
481     if (fWriter.bytesWritten() + needed > fBlockSize) {
482         // Before we wipe out any data that has already been written, read it out.
483         this->doNotify();
484
485         // If we're going to allocate a new block, allocate enough to make it worthwhile.
486         needed = SkTMax<size_t>(MIN_BLOCK_SIZE, needed);
487
488         void* block = fController->requestBlock(needed, &fBlockSize);
489         if (NULL == block) {
490             // Do not notify the readers, which would call this function again.
491             this->finish(false);
492             return false;
493         }
494         SkASSERT(SkIsAlign4(fBlockSize));
495         fWriter.reset(block, fBlockSize);
496         fBytesNotified = 0;
497     }
498     return true;
499 }
500
501 uint32_t SkGPipeCanvas::getTypefaceID(SkTypeface* face) {
502     uint32_t id = 0; // 0 means default/null typeface
503     if (face) {
504         id = fTypefaceSet.find(face);
505         if (0 == id) {
506             id = fTypefaceSet.add(face);
507             size_t size = writeTypeface(NULL, face);
508             if (this->needOpBytes(size)) {
509                 this->writeOp(kDef_Typeface_DrawOp);
510                 writeTypeface(&fWriter, face);
511             }
512         }
513     }
514     return id;
515 }
516
517 ///////////////////////////////////////////////////////////////////////////////
518
519 #define NOTIFY_SETUP(canvas)    \
520     AutoPipeNotify apn(canvas)
521
522 void SkGPipeCanvas::willSave() {
523     NOTIFY_SETUP(this);
524     if (this->needOpBytes()) {
525         this->writeOp(kSave_DrawOp);
526     }
527
528     this->INHERITED::willSave();
529 }
530
531 SkCanvas::SaveLayerStrategy SkGPipeCanvas::willSaveLayer(const SkRect* bounds, const SkPaint* paint,
532                                                          SaveFlags saveFlags) {
533     NOTIFY_SETUP(this);
534     size_t size = 0;
535     unsigned opFlags = 0;
536
537     if (bounds) {
538         opFlags |= kSaveLayer_HasBounds_DrawOpFlag;
539         size += sizeof(SkRect);
540     }
541     if (paint) {
542         opFlags |= kSaveLayer_HasPaint_DrawOpFlag;
543         this->writePaint(*paint);
544     }
545
546     if (this->needOpBytes(size)) {
547         this->writeOp(kSaveLayer_DrawOp, opFlags, saveFlags);
548         if (bounds) {
549             fWriter.writeRect(*bounds);
550         }
551     }
552
553     this->INHERITED::willSaveLayer(bounds, paint, saveFlags);
554     // we don't create a layer
555     return kNoLayer_SaveLayerStrategy;
556 }
557
558 void SkGPipeCanvas::willRestore() {
559     NOTIFY_SETUP(this);
560     if (this->needOpBytes()) {
561         this->writeOp(kRestore_DrawOp);
562     }
563
564     this->INHERITED::willRestore();
565 }
566
567 void SkGPipeCanvas::recordTranslate(const SkMatrix& m) {
568     if (this->needOpBytes(2 * sizeof(SkScalar))) {
569         this->writeOp(kTranslate_DrawOp);
570         fWriter.writeScalar(m.getTranslateX());
571         fWriter.writeScalar(m.getTranslateY());
572     }
573 }
574
575 void SkGPipeCanvas::recordScale(const SkMatrix& m) {
576     if (this->needOpBytes(2 * sizeof(SkScalar))) {
577         this->writeOp(kScale_DrawOp);
578         fWriter.writeScalar(m.getScaleX());
579         fWriter.writeScalar(m.getScaleY());
580     }
581 }
582
583 void SkGPipeCanvas::recordConcat(const SkMatrix& m) {
584     if (this->needOpBytes(m.writeToMemory(NULL))) {
585         this->writeOp(kConcat_DrawOp);
586         fWriter.writeMatrix(m);
587     }
588 }
589
590 void SkGPipeCanvas::didConcat(const SkMatrix& matrix) {
591     if (!matrix.isIdentity()) {
592         NOTIFY_SETUP(this);
593         switch (matrix.getType()) {
594             case SkMatrix::kTranslate_Mask:
595                 this->recordTranslate(matrix);
596                 break;
597             case SkMatrix::kScale_Mask:
598                 this->recordScale(matrix);
599                 break;
600             default:
601                 this->recordConcat(matrix);
602                 break;
603         }
604     }
605
606     this->INHERITED::didConcat(matrix);
607 }
608
609 void SkGPipeCanvas::didSetMatrix(const SkMatrix& matrix) {
610     NOTIFY_SETUP(this);
611     if (this->needOpBytes(matrix.writeToMemory(NULL))) {
612         this->writeOp(kSetMatrix_DrawOp);
613         fWriter.writeMatrix(matrix);
614     }
615     this->INHERITED::didSetMatrix(matrix);
616 }
617
618 void SkGPipeCanvas::onClipRect(const SkRect& rect, SkRegion::Op rgnOp,
619                                ClipEdgeStyle edgeStyle) {
620     NOTIFY_SETUP(this);
621     if (this->needOpBytes(sizeof(SkRect))) {
622         unsigned flags = 0;
623         if (kSoft_ClipEdgeStyle == edgeStyle) {
624             flags = kClip_HasAntiAlias_DrawOpFlag;
625         }
626         this->writeOp(kClipRect_DrawOp, flags, rgnOp);
627         fWriter.writeRect(rect);
628     }
629     this->INHERITED::onClipRect(rect, rgnOp, edgeStyle);
630 }
631
632 void SkGPipeCanvas::onClipRRect(const SkRRect& rrect, SkRegion::Op rgnOp,
633                                 ClipEdgeStyle edgeStyle) {
634     NOTIFY_SETUP(this);
635     if (this->needOpBytes(kSizeOfFlatRRect)) {
636         unsigned flags = 0;
637         if (kSoft_ClipEdgeStyle == edgeStyle) {
638             flags = kClip_HasAntiAlias_DrawOpFlag;
639         }
640         this->writeOp(kClipRRect_DrawOp, flags, rgnOp);
641         fWriter.writeRRect(rrect);
642     }
643     this->INHERITED::onClipRRect(rrect, rgnOp, edgeStyle);
644 }
645
646 void SkGPipeCanvas::onClipPath(const SkPath& path, SkRegion::Op rgnOp,
647                                ClipEdgeStyle edgeStyle) {
648     NOTIFY_SETUP(this);
649     if (this->needOpBytes(path.writeToMemory(NULL))) {
650         unsigned flags = 0;
651         if (kSoft_ClipEdgeStyle == edgeStyle) {
652             flags = kClip_HasAntiAlias_DrawOpFlag;
653         }
654         this->writeOp(kClipPath_DrawOp, flags, rgnOp);
655         fWriter.writePath(path);
656     }
657     // we just pass on the bounds of the path
658     this->INHERITED::onClipRect(path.getBounds(), rgnOp, edgeStyle);
659 }
660
661 void SkGPipeCanvas::onClipRegion(const SkRegion& region, SkRegion::Op rgnOp) {
662     NOTIFY_SETUP(this);
663     if (this->needOpBytes(region.writeToMemory(NULL))) {
664         this->writeOp(kClipRegion_DrawOp, 0, rgnOp);
665         fWriter.writeRegion(region);
666     }
667     this->INHERITED::onClipRegion(region, rgnOp);
668 }
669
670 ///////////////////////////////////////////////////////////////////////////////
671
672 void SkGPipeCanvas::onDrawPaint(const SkPaint& paint) {
673     NOTIFY_SETUP(this);
674     this->writePaint(paint);
675     if (this->needOpBytes()) {
676         this->writeOp(kDrawPaint_DrawOp);
677     }
678 }
679
680 void SkGPipeCanvas::onDrawPoints(PointMode mode, size_t count,
681                                  const SkPoint pts[], const SkPaint& paint) {
682     if (count) {
683         NOTIFY_SETUP(this);
684         this->writePaint(paint);
685         if (this->needOpBytes(4 + count * sizeof(SkPoint))) {
686             this->writeOp(kDrawPoints_DrawOp, mode, 0);
687             fWriter.write32(SkToU32(count));
688             fWriter.write(pts, count * sizeof(SkPoint));
689         }
690     }
691 }
692
693 void SkGPipeCanvas::onDrawOval(const SkRect& rect, const SkPaint& paint) {
694     NOTIFY_SETUP(this);
695     this->writePaint(paint);
696     if (this->needOpBytes(sizeof(SkRect))) {
697         this->writeOp(kDrawOval_DrawOp);
698         fWriter.writeRect(rect);
699     }
700 }
701
702 void SkGPipeCanvas::onDrawRect(const SkRect& rect, const SkPaint& paint) {
703     NOTIFY_SETUP(this);
704     this->writePaint(paint);
705     if (this->needOpBytes(sizeof(SkRect))) {
706         this->writeOp(kDrawRect_DrawOp);
707         fWriter.writeRect(rect);
708     }
709 }
710
711 void SkGPipeCanvas::onDrawRRect(const SkRRect& rrect, const SkPaint& paint) {
712     NOTIFY_SETUP(this);
713     this->writePaint(paint);
714     if (this->needOpBytes(kSizeOfFlatRRect)) {
715         this->writeOp(kDrawRRect_DrawOp);
716         fWriter.writeRRect(rrect);
717     }
718 }
719
720 void SkGPipeCanvas::onDrawDRRect(const SkRRect& outer, const SkRRect& inner,
721                                  const SkPaint& paint) {
722     NOTIFY_SETUP(this);
723     this->writePaint(paint);
724     if (this->needOpBytes(kSizeOfFlatRRect * 2)) {
725         this->writeOp(kDrawDRRect_DrawOp);
726         fWriter.writeRRect(outer);
727         fWriter.writeRRect(inner);
728     }
729 }
730
731 void SkGPipeCanvas::onDrawPath(const SkPath& path, const SkPaint& paint) {
732     NOTIFY_SETUP(this);
733     this->writePaint(paint);
734     if (this->needOpBytes(path.writeToMemory(NULL))) {
735         this->writeOp(kDrawPath_DrawOp);
736         fWriter.writePath(path);
737     }
738 }
739
740 bool SkGPipeCanvas::commonDrawBitmap(const SkBitmap& bm, DrawOps op,
741                                      unsigned flags,
742                                      size_t opBytesNeeded,
743                                      const SkPaint* paint) {
744     if (fDone) {
745         return false;
746     }
747
748     if (paint != NULL) {
749         flags |= kDrawBitmap_HasPaint_DrawOpFlag;
750         this->writePaint(*paint);
751     }
752     // This needs to run first so its calls to needOpBytes() and its writes
753     // don't interlace with the needOpBytes() and write below.
754     SkASSERT(fBitmapHeap != NULL);
755     int32_t bitmapIndex = fBitmapHeap->insert(bm);
756     if (SkBitmapHeap::INVALID_SLOT == bitmapIndex) {
757         return false;
758     }
759
760     if (this->needOpBytes(opBytesNeeded)) {
761         this->writeOp(op, flags, bitmapIndex);
762         return true;
763     }
764     return false;
765 }
766
767 void SkGPipeCanvas::onDrawBitmap(const SkBitmap& bm, SkScalar left, SkScalar top,
768                                  const SkPaint* paint) {
769     NOTIFY_SETUP(this);
770     size_t opBytesNeeded = sizeof(SkScalar) * 2;
771
772     if (this->commonDrawBitmap(bm, kDrawBitmap_DrawOp, 0, opBytesNeeded, paint)) {
773         fWriter.writeScalar(left);
774         fWriter.writeScalar(top);
775     }
776 }
777
778 void SkGPipeCanvas::onDrawBitmapRect(const SkBitmap& bm, const SkRect* src, const SkRect& dst,
779                                      const SkPaint* paint, DrawBitmapRectFlags dbmrFlags) {
780     NOTIFY_SETUP(this);
781     size_t opBytesNeeded = sizeof(SkRect);
782     bool hasSrc = src != NULL;
783     unsigned flags;
784     if (hasSrc) {
785         flags = kDrawBitmap_HasSrcRect_DrawOpFlag;
786         opBytesNeeded += sizeof(int32_t) * 4;
787     } else {
788         flags = 0;
789     }
790     if (dbmrFlags & kBleed_DrawBitmapRectFlag) {
791         flags |= kDrawBitmap_Bleed_DrawOpFlag;
792     }
793
794     if (this->commonDrawBitmap(bm, kDrawBitmapRectToRect_DrawOp, flags, opBytesNeeded, paint)) {
795         if (hasSrc) {
796             fWriter.writeRect(*src);
797         }
798         fWriter.writeRect(dst);
799     }
800 }
801
802 void SkGPipeCanvas::onDrawBitmapNine(const SkBitmap& bm, const SkIRect& center,
803                                      const SkRect& dst, const SkPaint* paint) {
804     NOTIFY_SETUP(this);
805     size_t opBytesNeeded = sizeof(int32_t) * 4 + sizeof(SkRect);
806
807     if (this->commonDrawBitmap(bm, kDrawBitmapNine_DrawOp, 0, opBytesNeeded, paint)) {
808         fWriter.write32(center.fLeft);
809         fWriter.write32(center.fTop);
810         fWriter.write32(center.fRight);
811         fWriter.write32(center.fBottom);
812         fWriter.writeRect(dst);
813     }
814 }
815
816 void SkGPipeCanvas::onDrawSprite(const SkBitmap& bm, int left, int top, const SkPaint* paint) {
817     NOTIFY_SETUP(this);
818     size_t opBytesNeeded = sizeof(int32_t) * 2;
819
820     if (this->commonDrawBitmap(bm, kDrawSprite_DrawOp, 0, opBytesNeeded, paint)) {
821         fWriter.write32(left);
822         fWriter.write32(top);
823     }
824 }
825
826 void SkGPipeCanvas::onDrawText(const void* text, size_t byteLength, SkScalar x, SkScalar y,
827                                const SkPaint& paint) {
828     if (byteLength) {
829         NOTIFY_SETUP(this);
830         this->writePaint(paint);
831         if (this->needOpBytes(4 + SkAlign4(byteLength) + 2 * sizeof(SkScalar))) {
832             this->writeOp(kDrawText_DrawOp);
833             fWriter.write32(SkToU32(byteLength));
834             fWriter.writePad(text, byteLength);
835             fWriter.writeScalar(x);
836             fWriter.writeScalar(y);
837         }
838     }
839 }
840
841 void SkGPipeCanvas::onDrawPosText(const void* text, size_t byteLength, const SkPoint pos[],
842                                   const SkPaint& paint) {
843     if (byteLength) {
844         NOTIFY_SETUP(this);
845         this->writePaint(paint);
846         int count = paint.textToGlyphs(text, byteLength, NULL);
847         if (this->needOpBytes(4 + SkAlign4(byteLength) + 4 + count * sizeof(SkPoint))) {
848             this->writeOp(kDrawPosText_DrawOp);
849             fWriter.write32(SkToU32(byteLength));
850             fWriter.writePad(text, byteLength);
851             fWriter.write32(count);
852             fWriter.write(pos, count * sizeof(SkPoint));
853         }
854     }
855 }
856
857 void SkGPipeCanvas::onDrawPosTextH(const void* text, size_t byteLength, const SkScalar xpos[],
858                                    SkScalar constY, const SkPaint& paint) {
859     if (byteLength) {
860         NOTIFY_SETUP(this);
861         this->writePaint(paint);
862         int count = paint.textToGlyphs(text, byteLength, NULL);
863         if (this->needOpBytes(4 + SkAlign4(byteLength) + 4 + count * sizeof(SkScalar) + 4)) {
864             this->writeOp(kDrawPosTextH_DrawOp);
865             fWriter.write32(SkToU32(byteLength));
866             fWriter.writePad(text, byteLength);
867             fWriter.write32(count);
868             fWriter.write(xpos, count * sizeof(SkScalar));
869             fWriter.writeScalar(constY);
870         }
871     }
872 }
873
874 void SkGPipeCanvas::onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& path,
875                                      const SkMatrix* matrix, const SkPaint& paint) {
876     if (byteLength) {
877         NOTIFY_SETUP(this);
878         unsigned flags = 0;
879         size_t size = 4 + SkAlign4(byteLength) + path.writeToMemory(NULL);
880         if (matrix) {
881             flags |= kDrawTextOnPath_HasMatrix_DrawOpFlag;
882             size += matrix->writeToMemory(NULL);
883         }
884         this->writePaint(paint);
885         if (this->needOpBytes(size)) {
886             this->writeOp(kDrawTextOnPath_DrawOp, flags, 0);
887
888             fWriter.write32(SkToU32(byteLength));
889             fWriter.writePad(text, byteLength);
890
891             fWriter.writePath(path);
892             if (matrix) {
893                 fWriter.writeMatrix(*matrix);
894             }
895         }
896     }
897 }
898
899 size_t SkGPipeCanvas::getInProcessTypefaces(const SkRefCntSet& typefaceSet,
900                                             TypefaceBuffer* buffer) {
901     // When in-process, we simply write out the typeface pointers.
902     size_t size = typefaceSet.count() * sizeof(SkTypeface*);
903     buffer->reset(size);
904     typefaceSet.copyToArray(reinterpret_cast<SkRefCnt**>(buffer->get()));
905
906     return size;
907 }
908
909 size_t SkGPipeCanvas::getCrossProcessTypefaces(const SkRefCntSet& typefaceSet,
910                                                TypefaceBuffer* buffer) {
911     // For cross-process we use typeface IDs.
912     size_t size = typefaceSet.count() * sizeof(uint32_t);
913     buffer->reset(size);
914
915     uint32_t* idBuffer = reinterpret_cast<uint32_t*>(buffer->get());
916     SkRefCntSet::Iter iter(typefaceSet);
917     int i = 0;
918
919     for (void* setPtr = iter.next(); setPtr; setPtr = iter.next()) {
920         idBuffer[i++] = this->getTypefaceID(reinterpret_cast<SkTypeface*>(setPtr));
921     }
922
923     SkASSERT(i == typefaceSet.count());
924
925     return size;
926 }
927
928 void SkGPipeCanvas::onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
929                                    const SkPaint& paint) {
930     NOTIFY_SETUP(this);
931     this->writePaint(paint);
932
933     // FIXME: this is inefficient but avoids duplicating the blob serialization logic.
934     SkRefCntSet typefaceSet;
935     SkWriteBuffer blobBuffer;
936     blobBuffer.setTypefaceRecorder(&typefaceSet);
937     blob->flatten(blobBuffer);
938
939     // Unlike most draw ops (which only use one paint/typeface), text blobs may reference
940     // an arbitrary number of typefaces. Since the one-paint-per-op model is not applicable,
941     // we need to serialize these explicitly.
942     TypefaceBuffer typefaceBuffer;
943     size_t typefaceSize = is_cross_process(fFlags)
944         ? this->getCrossProcessTypefaces(typefaceSet, &typefaceBuffer)
945         : this->getInProcessTypefaces(typefaceSet, &typefaceBuffer);
946
947     // blob byte count + typeface count + x + y + blob data + an index (cross-process)
948     // or pointer (in-process) for each typeface
949     size_t size = 2 * sizeof(uint32_t)
950                 + 2 * sizeof(SkScalar)
951                 + blobBuffer.bytesWritten()
952                 + typefaceSize;
953
954     if (this->needOpBytes(size)) {
955         this->writeOp(kDrawTextBlob_DrawOp);
956         SkDEBUGCODE(size_t initialOffset = fWriter.bytesWritten();)
957
958         fWriter.writeScalar(x);
959         fWriter.writeScalar(y);
960
961         fWriter.write32(typefaceSet.count());
962         fWriter.write(typefaceBuffer.get(), typefaceSize);
963
964         fWriter.write32(SkToU32(blobBuffer.bytesWritten()));
965         uint32_t* pad = fWriter.reservePad(blobBuffer.bytesWritten());
966         blobBuffer.writeToMemory(pad);
967
968         SkASSERT(initialOffset + size == fWriter.bytesWritten());
969     }
970 }
971
972 void SkGPipeCanvas::onDrawPicture(const SkPicture* picture, const SkMatrix* matrix,
973                                   const SkPaint* paint) {
974     // we want to playback the picture into individual draw calls
975     //
976     // todo: do we always have to unroll? If the pipe is not cross-process, seems like
977     // we could just ref the picture and move on...? <reed, scroggo>
978     //
979     this->INHERITED::onDrawPicture(picture, matrix, paint);
980 }
981
982 void SkGPipeCanvas::onDrawVertices(VertexMode vmode, int vertexCount,
983                                    const SkPoint vertices[], const SkPoint texs[],
984                                    const SkColor colors[], SkXfermode* xfer,
985                                    const uint16_t indices[], int indexCount,
986                                    const SkPaint& paint) {
987     if (0 == vertexCount) {
988         return;
989     }
990
991     NOTIFY_SETUP(this);
992     this->writePaint(paint);
993
994     unsigned flags = 0;  // packs with the op, so needs no extra space
995
996     size_t size = 0;
997     size += 4;                              // vmode
998     size += 4;                              // vertex count
999     size += vertexCount * sizeof(SkPoint);  // vertices
1000
1001     if (texs) {
1002         flags |= kDrawVertices_HasTexs_DrawOpFlag;
1003         size += vertexCount * sizeof(SkPoint);
1004     }
1005     if (colors) {
1006         flags |= kDrawVertices_HasColors_DrawOpFlag;
1007         size += vertexCount * sizeof(SkColor);
1008     }
1009     if (xfer && !SkXfermode::IsMode(xfer, SkXfermode::kModulate_Mode)) {
1010         flags |= kDrawVertices_HasXfermode_DrawOpFlag;
1011         size += sizeof(int32_t);    // SkXfermode::Mode
1012     }
1013     if (indices && indexCount > 0) {
1014         flags |= kDrawVertices_HasIndices_DrawOpFlag;
1015         size += 4;                                        // index count
1016         size += SkAlign4(indexCount * sizeof(uint16_t));  // indices
1017     }
1018
1019     if (this->needOpBytes(size)) {
1020         this->writeOp(kDrawVertices_DrawOp, flags, 0);
1021         fWriter.write32(vmode);
1022         fWriter.write32(vertexCount);
1023         fWriter.write(vertices, vertexCount * sizeof(SkPoint));
1024         if (flags & kDrawVertices_HasTexs_DrawOpFlag) {
1025             fWriter.write(texs, vertexCount * sizeof(SkPoint));
1026         }
1027         if (flags & kDrawVertices_HasColors_DrawOpFlag) {
1028             fWriter.write(colors, vertexCount * sizeof(SkColor));
1029         }
1030         if (flags & kDrawVertices_HasXfermode_DrawOpFlag) {
1031             SkXfermode::Mode mode = SkXfermode::kModulate_Mode;
1032             SkAssertResult(xfer->asMode(&mode));
1033             fWriter.write32(mode);
1034         }
1035         if (flags & kDrawVertices_HasIndices_DrawOpFlag) {
1036             fWriter.write32(indexCount);
1037             fWriter.writePad(indices, indexCount * sizeof(uint16_t));
1038         }
1039     }
1040 }
1041
1042 void SkGPipeCanvas::onDrawPatch(const SkPoint cubics[12], const SkColor colors[4],
1043                                 const SkPoint texCoords[4], SkXfermode* xmode,
1044                                 const SkPaint& paint) {
1045     NOTIFY_SETUP(this);
1046     
1047     size_t size = SkPatchUtils::kNumCtrlPts * sizeof(SkPoint);
1048     unsigned flags = 0;
1049     if (colors) {
1050         flags |= kDrawVertices_HasColors_DrawOpFlag;
1051         size += SkPatchUtils::kNumCorners * sizeof(SkColor);
1052     }
1053     if (texCoords) {
1054         flags |= kDrawVertices_HasTexs_DrawOpFlag;
1055         size += SkPatchUtils::kNumCorners * sizeof(SkPoint);
1056     }
1057     if (xmode) {
1058         SkXfermode::Mode mode;
1059         if (xmode->asMode(&mode) && SkXfermode::kModulate_Mode != mode) {
1060             flags |= kDrawVertices_HasXfermode_DrawOpFlag;
1061             size += sizeof(int32_t);
1062         }
1063     }
1064     
1065     this->writePaint(paint);
1066     if (this->needOpBytes(size)) {
1067         this->writeOp(kDrawPatch_DrawOp, flags, 0);
1068         
1069         fWriter.write(cubics, SkPatchUtils::kNumCtrlPts * sizeof(SkPoint));
1070         
1071         if (colors) {
1072             fWriter.write(colors, SkPatchUtils::kNumCorners * sizeof(SkColor));
1073         }
1074         
1075         if (texCoords) {
1076             fWriter.write(texCoords, SkPatchUtils::kNumCorners * sizeof(SkPoint));
1077         }
1078         
1079         if (flags & kDrawVertices_HasXfermode_DrawOpFlag) {
1080             SkXfermode::Mode mode = SkXfermode::kModulate_Mode;
1081             SkAssertResult(xmode->asMode(&mode));
1082             fWriter.write32(mode);
1083         }
1084     }
1085 }
1086
1087 void SkGPipeCanvas::beginCommentGroup(const char* description) {
1088     // ignore for now
1089 }
1090
1091 void SkGPipeCanvas::addComment(const char* kywd, const char* value) {
1092     // ignore for now
1093 }
1094
1095 void SkGPipeCanvas::endCommentGroup() {
1096     // ignore for now
1097 }
1098
1099 void SkGPipeCanvas::flushRecording(bool detachCurrentBlock) {
1100     this->doNotify();
1101     if (detachCurrentBlock) {
1102         // force a new block to be requested for the next recorded command
1103         fBlockSize = 0;
1104     }
1105 }
1106
1107 size_t SkGPipeCanvas::freeMemoryIfPossible(size_t bytesToFree) {
1108     return (NULL == fBitmapHeap) ? 0 : fBitmapHeap->freeMemoryIfPossible(bytesToFree);
1109 }
1110
1111 ///////////////////////////////////////////////////////////////////////////////
1112
1113 template <typename T> uint32_t castToU32(T value) {
1114     union {
1115         T           fSrc;
1116         uint32_t    fDst;
1117     } data;
1118     data.fSrc = value;
1119     return data.fDst;
1120 }
1121
1122 void SkGPipeCanvas::writePaint(const SkPaint& paint) {
1123     if (fDone) {
1124         return;
1125     }
1126     SkPaint& base = fPaint;
1127     uint32_t storage[32];
1128     uint32_t* ptr = storage;
1129
1130     if (base.getFlags() != paint.getFlags()) {
1131         *ptr++ = PaintOp_packOpData(kFlags_PaintOp, paint.getFlags());
1132         base.setFlags(paint.getFlags());
1133     }
1134     if (base.getColor() != paint.getColor()) {
1135         *ptr++ = PaintOp_packOp(kColor_PaintOp);
1136         *ptr++ = paint.getColor();
1137         base.setColor(paint.getColor());
1138     }
1139     if (base.getFilterQuality() != paint.getFilterQuality()) {
1140         *ptr++ = PaintOp_packOpData(kFilterLevel_PaintOp, paint.getFilterQuality());
1141         base.setFilterQuality(paint.getFilterQuality());
1142     }
1143     if (base.getStyle() != paint.getStyle()) {
1144         *ptr++ = PaintOp_packOpData(kStyle_PaintOp, paint.getStyle());
1145         base.setStyle(paint.getStyle());
1146     }
1147     if (base.getStrokeJoin() != paint.getStrokeJoin()) {
1148         *ptr++ = PaintOp_packOpData(kJoin_PaintOp, paint.getStrokeJoin());
1149         base.setStrokeJoin(paint.getStrokeJoin());
1150     }
1151     if (base.getStrokeCap() != paint.getStrokeCap()) {
1152         *ptr++ = PaintOp_packOpData(kCap_PaintOp, paint.getStrokeCap());
1153         base.setStrokeCap(paint.getStrokeCap());
1154     }
1155     if (base.getStrokeWidth() != paint.getStrokeWidth()) {
1156         *ptr++ = PaintOp_packOp(kWidth_PaintOp);
1157         *ptr++ = castToU32(paint.getStrokeWidth());
1158         base.setStrokeWidth(paint.getStrokeWidth());
1159     }
1160     if (base.getStrokeMiter() != paint.getStrokeMiter()) {
1161         *ptr++ = PaintOp_packOp(kMiter_PaintOp);
1162         *ptr++ = castToU32(paint.getStrokeMiter());
1163         base.setStrokeMiter(paint.getStrokeMiter());
1164     }
1165     if (base.getTextEncoding() != paint.getTextEncoding()) {
1166         *ptr++ = PaintOp_packOpData(kEncoding_PaintOp, paint.getTextEncoding());
1167         base.setTextEncoding(paint.getTextEncoding());
1168     }
1169     if (base.getHinting() != paint.getHinting()) {
1170         *ptr++ = PaintOp_packOpData(kHinting_PaintOp, paint.getHinting());
1171         base.setHinting(paint.getHinting());
1172     }
1173     if (base.getTextAlign() != paint.getTextAlign()) {
1174         *ptr++ = PaintOp_packOpData(kAlign_PaintOp, paint.getTextAlign());
1175         base.setTextAlign(paint.getTextAlign());
1176     }
1177     if (base.getTextSize() != paint.getTextSize()) {
1178         *ptr++ = PaintOp_packOp(kTextSize_PaintOp);
1179         *ptr++ = castToU32(paint.getTextSize());
1180         base.setTextSize(paint.getTextSize());
1181     }
1182     if (base.getTextScaleX() != paint.getTextScaleX()) {
1183         *ptr++ = PaintOp_packOp(kTextScaleX_PaintOp);
1184         *ptr++ = castToU32(paint.getTextScaleX());
1185         base.setTextScaleX(paint.getTextScaleX());
1186     }
1187     if (base.getTextSkewX() != paint.getTextSkewX()) {
1188         *ptr++ = PaintOp_packOp(kTextSkewX_PaintOp);
1189         *ptr++ = castToU32(paint.getTextSkewX());
1190         base.setTextSkewX(paint.getTextSkewX());
1191     }
1192
1193     if (!SkTypeface::Equal(base.getTypeface(), paint.getTypeface())) {
1194         if (is_cross_process(fFlags)) {
1195             uint32_t id = this->getTypefaceID(paint.getTypeface());
1196             *ptr++ = PaintOp_packOpData(kTypeface_PaintOp, id);
1197         } else if (this->needOpBytes(sizeof(void*))) {
1198             // Add to the set for ref counting.
1199             fTypefaceSet.add(paint.getTypeface());
1200             // It is safe to write the typeface to the stream before the rest
1201             // of the paint unless we ever send a kReset_PaintOp, which we
1202             // currently never do.
1203             this->writeOp(kSetTypeface_DrawOp);
1204             fWriter.writePtr(paint.getTypeface());
1205         }
1206         base.setTypeface(paint.getTypeface());
1207     }
1208
1209     // This is a new paint, so all old flats can be safely purged, if necessary.
1210     fFlattenableHeap.markAllFlatsSafeToDelete();
1211     for (int i = 0; i < kCount_PaintFlats; i++) {
1212         int index = this->flattenToIndex(get_paintflat(paint, i), (PaintFlats)i);
1213         bool replaced = index < 0;
1214         if (replaced) {
1215             index = ~index;
1216         }
1217         // Store the index of any flat that needs to be kept. 0 means no flat.
1218         if (index > 0) {
1219             fFlattenableHeap.markFlatForKeeping(index);
1220         }
1221         SkASSERT(index >= 0 && index <= fFlatDictionary.count());
1222         if (index != fCurrFlatIndex[i] || replaced) {
1223             *ptr++ = PaintOp_packOpFlagData(kFlatIndex_PaintOp, i, index);
1224             fCurrFlatIndex[i] = index;
1225         }
1226     }
1227
1228     size_t size = (char*)ptr - (char*)storage;
1229     if (size && this->needOpBytes(size)) {
1230         this->writeOp(kPaintOp_DrawOp, 0, SkToU32(size));
1231         fWriter.write(storage, size);
1232         for (size_t i = 0; i < size/4; i++) {
1233 //            SkDebugf("[%d] %08X\n", i, storage[i]);
1234         }
1235     }
1236
1237     //
1238     //  Do these after we've written kPaintOp_DrawOp
1239
1240     if (base.getAnnotation() != paint.getAnnotation()) {
1241         if (NULL == paint.getAnnotation()) {
1242             if (this->needOpBytes()) {
1243                 this->writeOp(kSetAnnotation_DrawOp, 0, 0);
1244             }
1245         } else {
1246             SkWriteBuffer buffer;
1247             paint.getAnnotation()->writeToBuffer(buffer);
1248             const size_t size = buffer.bytesWritten();
1249             if (this->needOpBytes(size)) {
1250                 this->writeOp(kSetAnnotation_DrawOp, 0, SkToU32(size));
1251                 buffer.writeToMemory(fWriter.reserve(size));
1252             }
1253         }
1254         base.setAnnotation(paint.getAnnotation());
1255     }
1256 }
1257
1258 ///////////////////////////////////////////////////////////////////////////////
1259
1260 #include "SkGPipe.h"
1261
1262 SkGPipeController::~SkGPipeController() {
1263     SkSafeUnref(fCanvas);
1264 }
1265
1266 void SkGPipeController::setCanvas(SkGPipeCanvas* canvas) {
1267     SkRefCnt_SafeAssign(fCanvas, canvas);
1268 }
1269
1270 ///////////////////////////////////////////////////////////////////////////////
1271
1272 SkGPipeWriter::SkGPipeWriter()
1273 : fWriter(0) {
1274     fCanvas = NULL;
1275 }
1276
1277 SkGPipeWriter::~SkGPipeWriter() {
1278     this->endRecording();
1279 }
1280
1281 SkCanvas* SkGPipeWriter::startRecording(SkGPipeController* controller, uint32_t flags,
1282                                         uint32_t width, uint32_t height) {
1283     if (NULL == fCanvas) {
1284         fWriter.reset(NULL, 0);
1285         fCanvas = SkNEW_ARGS(SkGPipeCanvas, (controller, &fWriter, flags, width, height));
1286     }
1287     controller->setCanvas(fCanvas);
1288     return fCanvas;
1289 }
1290
1291 void SkGPipeWriter::endRecording() {
1292     if (fCanvas) {
1293         fCanvas->finish(true);
1294         fCanvas->unref();
1295         fCanvas = NULL;
1296     }
1297 }
1298
1299 void SkGPipeWriter::flushRecording(bool detachCurrentBlock) {
1300     if (fCanvas) {
1301         fCanvas->flushRecording(detachCurrentBlock);
1302     }
1303 }
1304
1305 size_t SkGPipeWriter::freeMemoryIfPossible(size_t bytesToFree) {
1306     if (fCanvas) {
1307         return fCanvas->freeMemoryIfPossible(bytesToFree);
1308     }
1309     return 0;
1310 }
1311
1312 size_t SkGPipeWriter::storageAllocatedForRecording() const {
1313     return NULL == fCanvas ? 0 : fCanvas->storageAllocatedForRecording();
1314 }
1315
1316 ///////////////////////////////////////////////////////////////////////////////
1317
1318 BitmapShuttle::BitmapShuttle(SkGPipeCanvas* canvas) {
1319     SkASSERT(canvas != NULL);
1320     fCanvas = canvas;
1321     fCanvas->ref();
1322 }
1323
1324 BitmapShuttle::~BitmapShuttle() {
1325     this->removeCanvas();
1326 }
1327
1328 bool BitmapShuttle::insert(const SkBitmap& bitmap, int32_t slot) {
1329     SkASSERT(fCanvas != NULL);
1330     return fCanvas->shuttleBitmap(bitmap, slot);
1331 }
1332
1333 void BitmapShuttle::removeCanvas() {
1334     if (NULL == fCanvas) {
1335         return;
1336     }
1337     fCanvas->unref();
1338     fCanvas = NULL;
1339 }