epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 2 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 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. |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 10 | |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 11 | #include "SkCanvas.h" |
reed@google.com | 8a85d0c | 2011-06-24 19:12:12 +0000 | [diff] [blame] | 12 | #include "SkData.h" |
reed@google.com | bb6793b | 2011-05-05 15:18:15 +0000 | [diff] [blame] | 13 | #include "SkDevice.h" |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 14 | #include "SkPaint.h" |
reed@google.com | 75a0972 | 2012-05-10 12:56:16 +0000 | [diff] [blame] | 15 | #include "SkPathEffect.h" |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 16 | #include "SkGPipe.h" |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 17 | #include "SkGPipePriv.h" |
scroggo@google.com | 16d1d0b | 2012-05-02 19:09:40 +0000 | [diff] [blame] | 18 | #include "SkImageFilter.h" |
reed@google.com | f5842f7 | 2011-05-04 18:30:04 +0000 | [diff] [blame] | 19 | #include "SkStream.h" |
reed@google.com | b55d118 | 2011-05-11 00:42:04 +0000 | [diff] [blame] | 20 | #include "SkTSearch.h" |
reed@google.com | f5842f7 | 2011-05-04 18:30:04 +0000 | [diff] [blame] | 21 | #include "SkTypeface.h" |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 22 | #include "SkWriter32.h" |
reed@google.com | b55d118 | 2011-05-11 00:42:04 +0000 | [diff] [blame] | 23 | #include "SkColorFilter.h" |
reed@google.com | 0faac1e | 2011-05-11 05:58:58 +0000 | [diff] [blame] | 24 | #include "SkDrawLooper.h" |
reed@google.com | b55d118 | 2011-05-11 00:42:04 +0000 | [diff] [blame] | 25 | #include "SkMaskFilter.h" |
| 26 | #include "SkRasterizer.h" |
| 27 | #include "SkShader.h" |
djsollen@google.com | 2b2ede3 | 2012-04-12 13:24:04 +0000 | [diff] [blame] | 28 | #include "SkOrderedWriteBuffer.h" |
reed@google.com | b55d118 | 2011-05-11 00:42:04 +0000 | [diff] [blame] | 29 | |
| 30 | static SkFlattenable* get_paintflat(const SkPaint& paint, unsigned paintFlat) { |
| 31 | SkASSERT(paintFlat < kCount_PaintFlats); |
| 32 | switch (paintFlat) { |
| 33 | case kColorFilter_PaintFlat: return paint.getColorFilter(); |
reed@google.com | 0faac1e | 2011-05-11 05:58:58 +0000 | [diff] [blame] | 34 | case kDrawLooper_PaintFlat: return paint.getLooper(); |
reed@google.com | b55d118 | 2011-05-11 00:42:04 +0000 | [diff] [blame] | 35 | case kMaskFilter_PaintFlat: return paint.getMaskFilter(); |
| 36 | case kPathEffect_PaintFlat: return paint.getPathEffect(); |
| 37 | case kRasterizer_PaintFlat: return paint.getRasterizer(); |
| 38 | case kShader_PaintFlat: return paint.getShader(); |
scroggo@google.com | 16d1d0b | 2012-05-02 19:09:40 +0000 | [diff] [blame] | 39 | case kImageFilter_PaintFlat: return paint.getImageFilter(); |
reed@google.com | b55d118 | 2011-05-11 00:42:04 +0000 | [diff] [blame] | 40 | case kXfermode_PaintFlat: return paint.getXfermode(); |
| 41 | } |
tomhudson@google.com | 0c00f21 | 2011-12-28 14:59:50 +0000 | [diff] [blame] | 42 | SkDEBUGFAIL("never gets here"); |
reed@google.com | b55d118 | 2011-05-11 00:42:04 +0000 | [diff] [blame] | 43 | return NULL; |
| 44 | } |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 45 | |
reed@google.com | f5842f7 | 2011-05-04 18:30:04 +0000 | [diff] [blame] | 46 | static size_t writeTypeface(SkWriter32* writer, SkTypeface* typeface) { |
| 47 | SkASSERT(typeface); |
| 48 | SkDynamicMemoryWStream stream; |
| 49 | typeface->serialize(&stream); |
| 50 | size_t size = stream.getOffset(); |
| 51 | if (writer) { |
| 52 | writer->write32(size); |
reed@google.com | 8a85d0c | 2011-06-24 19:12:12 +0000 | [diff] [blame] | 53 | SkAutoDataUnref data(stream.copyToData()); |
scroggo@google.com | 5af9b20 | 2012-06-04 17:17:36 +0000 | [diff] [blame] | 54 | writer->writePad(data.data(), size); |
reed@google.com | f5842f7 | 2011-05-04 18:30:04 +0000 | [diff] [blame] | 55 | } |
scroggo@google.com | 5af9b20 | 2012-06-04 17:17:36 +0000 | [diff] [blame] | 56 | return 4 + SkAlign4(size); |
reed@google.com | f5842f7 | 2011-05-04 18:30:04 +0000 | [diff] [blame] | 57 | } |
| 58 | |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 59 | /////////////////////////////////////////////////////////////////////////////// |
| 60 | |
scroggo@google.com | 8ae3c7f | 2012-06-13 17:47:52 +0000 | [diff] [blame] | 61 | /* |
| 62 | * Shared heap for storing large things that can be shared, for a stream |
| 63 | * used by multiple readers. |
| 64 | * TODO: Make the allocations all come from cross process safe address space |
| 65 | * TODO: Store paths (others?) |
| 66 | * TODO: Allow reclaiming of memory. Will require us to know when all readers |
| 67 | * have used the object. |
| 68 | */ |
| 69 | class Heap { |
| 70 | public: |
scroggo@google.com | 565254b | 2012-06-28 15:41:32 +0000 | [diff] [blame^] | 71 | Heap(bool shallow) : fCanDoShallowCopies(shallow) {} |
scroggo@google.com | 8ae3c7f | 2012-06-13 17:47:52 +0000 | [diff] [blame] | 72 | ~Heap() { |
| 73 | for (int i = 0; i < fBitmaps.count(); i++) { |
| 74 | delete fBitmaps[i].fBitmap; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | /* |
| 79 | * Add a copy of a bitmap to the heap. |
| 80 | * @param bm The SkBitmap to be copied and placed in the heap. |
| 81 | * @return void* Pointer to the heap's copy of the bitmap. If NULL, |
| 82 | * the bitmap could not be copied. |
| 83 | */ |
| 84 | const SkBitmap* addBitmap(const SkBitmap& bm) { |
| 85 | const uint32_t genID = bm.getGenerationID(); |
| 86 | for (int i = fBitmaps.count() - 1; i >= 0; i--) { |
| 87 | if (genID == fBitmaps[i].fGenID) { |
| 88 | return fBitmaps[i].fBitmap; |
| 89 | } |
| 90 | } |
scroggo@google.com | 565254b | 2012-06-28 15:41:32 +0000 | [diff] [blame^] | 91 | SkBitmap* copy; |
| 92 | // If the bitmap is mutable, we still need to do a deep copy, since the |
| 93 | // caller may modify it afterwards. That said, if the bitmap is mutable, |
| 94 | // but has no pixelRef, the copy constructor actually does a deep copy. |
| 95 | if (fCanDoShallowCopies && (bm.isImmutable() || !bm.pixelRef())) { |
| 96 | copy = new SkBitmap(bm); |
| 97 | } else { |
| 98 | copy = new SkBitmap(); |
| 99 | if (!bm.copyTo(copy, bm.getConfig())) { |
| 100 | delete copy; |
| 101 | return NULL; |
| 102 | } |
scroggo@google.com | 8ae3c7f | 2012-06-13 17:47:52 +0000 | [diff] [blame] | 103 | } |
scroggo@google.com | 565254b | 2012-06-28 15:41:32 +0000 | [diff] [blame^] | 104 | BitmapInfo* info = fBitmaps.append(); |
| 105 | info->fBitmap = copy; |
| 106 | info->fGenID = genID; |
| 107 | return copy; |
scroggo@google.com | 8ae3c7f | 2012-06-13 17:47:52 +0000 | [diff] [blame] | 108 | } |
| 109 | private: |
| 110 | struct BitmapInfo { |
| 111 | SkBitmap* fBitmap; |
| 112 | // Store the generation ID of the original bitmap, since copying does |
| 113 | // not copy this field, so fBitmap's generation ID will not be useful |
| 114 | // for comparing. |
| 115 | uint32_t fGenID; |
| 116 | }; |
scroggo@google.com | 565254b | 2012-06-28 15:41:32 +0000 | [diff] [blame^] | 117 | SkTDArray<BitmapInfo> fBitmaps; |
| 118 | const bool fCanDoShallowCopies; |
scroggo@google.com | 8ae3c7f | 2012-06-13 17:47:52 +0000 | [diff] [blame] | 119 | }; |
| 120 | |
| 121 | /////////////////////////////////////////////////////////////////////////////// |
| 122 | |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 123 | class SkGPipeCanvas : public SkCanvas { |
| 124 | public: |
scroggo@google.com | 565254b | 2012-06-28 15:41:32 +0000 | [diff] [blame^] | 125 | SkGPipeCanvas(SkGPipeController*, SkWriter32*, SkFactorySet*, uint32_t flags); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 126 | virtual ~SkGPipeCanvas(); |
| 127 | |
| 128 | void finish() { |
| 129 | if (!fDone) { |
reed@google.com | dbccc88 | 2011-07-08 18:53:39 +0000 | [diff] [blame] | 130 | if (this->needOpBytes()) { |
| 131 | this->writeOp(kDone_DrawOp); |
| 132 | this->doNotify(); |
| 133 | } |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 134 | fDone = true; |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | // overrides from SkCanvas |
scroggo@google.com | 3b45cd5 | 2012-04-18 13:57:47 +0000 | [diff] [blame] | 139 | virtual int save(SaveFlags) SK_OVERRIDE; |
| 140 | virtual int saveLayer(const SkRect* bounds, const SkPaint*, |
| 141 | SaveFlags) SK_OVERRIDE; |
| 142 | virtual void restore() SK_OVERRIDE; |
| 143 | virtual bool translate(SkScalar dx, SkScalar dy) SK_OVERRIDE; |
| 144 | virtual bool scale(SkScalar sx, SkScalar sy) SK_OVERRIDE; |
| 145 | virtual bool rotate(SkScalar degrees) SK_OVERRIDE; |
| 146 | virtual bool skew(SkScalar sx, SkScalar sy) SK_OVERRIDE; |
| 147 | virtual bool concat(const SkMatrix& matrix) SK_OVERRIDE; |
| 148 | virtual void setMatrix(const SkMatrix& matrix) SK_OVERRIDE; |
| 149 | virtual bool clipRect(const SkRect& rect, SkRegion::Op op, |
| 150 | bool doAntiAlias = false) SK_OVERRIDE; |
| 151 | virtual bool clipPath(const SkPath& path, SkRegion::Op op, |
| 152 | bool doAntiAlias = false) SK_OVERRIDE; |
| 153 | virtual bool clipRegion(const SkRegion& region, SkRegion::Op op) SK_OVERRIDE; |
| 154 | virtual void clear(SkColor) SK_OVERRIDE; |
| 155 | virtual void drawPaint(const SkPaint& paint) SK_OVERRIDE; |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 156 | virtual void drawPoints(PointMode, size_t count, const SkPoint pts[], |
scroggo@google.com | 3b45cd5 | 2012-04-18 13:57:47 +0000 | [diff] [blame] | 157 | const SkPaint&) SK_OVERRIDE; |
| 158 | virtual void drawRect(const SkRect& rect, const SkPaint&) SK_OVERRIDE; |
| 159 | virtual void drawPath(const SkPath& path, const SkPaint&) SK_OVERRIDE; |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 160 | virtual void drawBitmap(const SkBitmap&, SkScalar left, SkScalar top, |
scroggo@google.com | 3b45cd5 | 2012-04-18 13:57:47 +0000 | [diff] [blame] | 161 | const SkPaint*) SK_OVERRIDE; |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 162 | virtual void drawBitmapRect(const SkBitmap&, const SkIRect* src, |
scroggo@google.com | 3b45cd5 | 2012-04-18 13:57:47 +0000 | [diff] [blame] | 163 | const SkRect& dst, const SkPaint*) SK_OVERRIDE; |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 164 | virtual void drawBitmapMatrix(const SkBitmap&, const SkMatrix&, |
scroggo@google.com | 3b45cd5 | 2012-04-18 13:57:47 +0000 | [diff] [blame] | 165 | const SkPaint*) SK_OVERRIDE; |
scroggo@google.com | 5a2e879 | 2012-04-20 17:39:51 +0000 | [diff] [blame] | 166 | virtual void drawBitmapNine(const SkBitmap& bitmap, const SkIRect& center, |
| 167 | const SkRect& dst, const SkPaint* paint = NULL) SK_OVERRIDE; |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 168 | virtual void drawSprite(const SkBitmap&, int left, int top, |
scroggo@google.com | 3b45cd5 | 2012-04-18 13:57:47 +0000 | [diff] [blame] | 169 | const SkPaint*) SK_OVERRIDE; |
vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 170 | virtual void drawText(const void* text, size_t byteLength, SkScalar x, |
scroggo@google.com | 3b45cd5 | 2012-04-18 13:57:47 +0000 | [diff] [blame] | 171 | SkScalar y, const SkPaint&) SK_OVERRIDE; |
vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 172 | virtual void drawPosText(const void* text, size_t byteLength, |
scroggo@google.com | 3b45cd5 | 2012-04-18 13:57:47 +0000 | [diff] [blame] | 173 | const SkPoint pos[], const SkPaint&) SK_OVERRIDE; |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 174 | virtual void drawPosTextH(const void* text, size_t byteLength, |
scroggo@google.com | 3b45cd5 | 2012-04-18 13:57:47 +0000 | [diff] [blame] | 175 | const SkScalar xpos[], SkScalar constY, |
| 176 | const SkPaint&) SK_OVERRIDE; |
vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 177 | virtual void drawTextOnPath(const void* text, size_t byteLength, |
| 178 | const SkPath& path, const SkMatrix* matrix, |
scroggo@google.com | 3b45cd5 | 2012-04-18 13:57:47 +0000 | [diff] [blame] | 179 | const SkPaint&) SK_OVERRIDE; |
| 180 | virtual void drawPicture(SkPicture& picture) SK_OVERRIDE; |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 181 | virtual void drawVertices(VertexMode, int vertexCount, |
| 182 | const SkPoint vertices[], const SkPoint texs[], |
| 183 | const SkColor colors[], SkXfermode*, |
| 184 | const uint16_t indices[], int indexCount, |
scroggo@google.com | 3b45cd5 | 2012-04-18 13:57:47 +0000 | [diff] [blame] | 185 | const SkPaint&) SK_OVERRIDE; |
| 186 | virtual void drawData(const void*, size_t) SK_OVERRIDE; |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 187 | |
| 188 | private: |
scroggo@google.com | 8ae3c7f | 2012-06-13 17:47:52 +0000 | [diff] [blame] | 189 | Heap fHeap; |
reed@google.com | dde0956 | 2011-05-23 12:21:05 +0000 | [diff] [blame] | 190 | SkFactorySet* fFactorySet; // optional, only used if cross-process |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 191 | SkGPipeController* fController; |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 192 | SkWriter32& fWriter; |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 193 | size_t fBlockSize; // amount allocated for writer |
| 194 | size_t fBytesNotified; |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 195 | bool fDone; |
scroggo@google.com | 565254b | 2012-06-28 15:41:32 +0000 | [diff] [blame^] | 196 | uint32_t fFlags; |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 197 | |
reed@google.com | f5842f7 | 2011-05-04 18:30:04 +0000 | [diff] [blame] | 198 | SkRefCntSet fTypefaceSet; |
| 199 | |
| 200 | uint32_t getTypefaceID(SkTypeface*); |
| 201 | |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 202 | inline void writeOp(DrawOps op, unsigned flags, unsigned data) { |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 203 | fWriter.write32(DrawOp_packOpFlagData(op, flags, data)); |
| 204 | } |
vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 205 | |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 206 | inline void writeOp(DrawOps op) { |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 207 | fWriter.write32(DrawOp_packOpFlagData(op, 0, 0)); |
| 208 | } |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 209 | |
| 210 | bool needOpBytes(size_t size = 0); |
| 211 | |
| 212 | inline void doNotify() { |
| 213 | if (!fDone) { |
| 214 | size_t bytes = fWriter.size() - fBytesNotified; |
scroggo@google.com | 16d1d0b | 2012-05-02 19:09:40 +0000 | [diff] [blame] | 215 | if (bytes > 0) { |
| 216 | fController->notifyWritten(bytes); |
| 217 | fBytesNotified += bytes; |
| 218 | } |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 219 | } |
| 220 | } |
reed@google.com | b55d118 | 2011-05-11 00:42:04 +0000 | [diff] [blame] | 221 | |
| 222 | struct FlatData { |
| 223 | uint32_t fIndex; // always > 0 |
| 224 | uint32_t fSize; |
| 225 | |
| 226 | void* data() { return (char*)this + sizeof(*this); } |
vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 227 | |
reed@google.com | b55d118 | 2011-05-11 00:42:04 +0000 | [diff] [blame] | 228 | static int Compare(const FlatData* a, const FlatData* b) { |
| 229 | return memcmp(&a->fSize, &b->fSize, a->fSize + sizeof(a->fSize)); |
| 230 | } |
| 231 | }; |
scroggo@google.com | 16d1d0b | 2012-05-02 19:09:40 +0000 | [diff] [blame] | 232 | |
| 233 | SkTDArray<FlatData*> fBitmapArray; |
| 234 | int flattenToIndex(const SkBitmap&); |
| 235 | |
reed@google.com | b55d118 | 2011-05-11 00:42:04 +0000 | [diff] [blame] | 236 | SkTDArray<FlatData*> fFlatArray; |
| 237 | int fCurrFlatIndex[kCount_PaintFlats]; |
| 238 | int flattenToIndex(SkFlattenable* obj, PaintFlats); |
| 239 | |
reed@google.com | 3189158 | 2011-05-12 03:03:56 +0000 | [diff] [blame] | 240 | SkPaint fPaint; |
| 241 | void writePaint(const SkPaint&); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 242 | |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 243 | class AutoPipeNotify { |
| 244 | public: |
| 245 | AutoPipeNotify(SkGPipeCanvas* canvas) : fCanvas(canvas) {} |
| 246 | ~AutoPipeNotify() { fCanvas->doNotify(); } |
| 247 | private: |
| 248 | SkGPipeCanvas* fCanvas; |
| 249 | }; |
| 250 | friend class AutoPipeNotify; |
| 251 | |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 252 | typedef SkCanvas INHERITED; |
| 253 | }; |
| 254 | |
scroggo@google.com | 16d1d0b | 2012-05-02 19:09:40 +0000 | [diff] [blame] | 255 | int SkGPipeCanvas::flattenToIndex(const SkBitmap & bitmap) { |
scroggo@google.com | 565254b | 2012-06-28 15:41:32 +0000 | [diff] [blame^] | 256 | SkASSERT(shouldFlattenBitmaps(fFlags)); |
scroggo@google.com | 16d1d0b | 2012-05-02 19:09:40 +0000 | [diff] [blame] | 257 | SkOrderedWriteBuffer tmpWriter(1024); |
scroggo@google.com | 565254b | 2012-06-28 15:41:32 +0000 | [diff] [blame^] | 258 | tmpWriter.setFlags((SkFlattenableWriteBuffer::Flags) |
| 259 | (SkFlattenableWriteBuffer::kInlineFactoryNames_Flag |
| 260 | | SkFlattenableWriteBuffer::kCrossProcess_Flag)); |
| 261 | tmpWriter.setFactoryRecorder(fFactorySet); |
scroggo@google.com | 16d1d0b | 2012-05-02 19:09:40 +0000 | [diff] [blame] | 262 | bitmap.flatten(tmpWriter); |
| 263 | |
| 264 | size_t len = tmpWriter.size(); |
| 265 | size_t allocSize = len + sizeof(FlatData); |
| 266 | |
| 267 | SkAutoSMalloc<1024> storage(allocSize); |
| 268 | FlatData* flat = (FlatData*)storage.get(); |
| 269 | flat->fSize = len; |
| 270 | tmpWriter.flatten(flat->data()); |
| 271 | |
| 272 | int index = SkTSearch<FlatData>((const FlatData**)fBitmapArray.begin(), |
| 273 | fBitmapArray.count(), flat, sizeof(flat), |
| 274 | &FlatData::Compare); |
| 275 | if (index < 0) { |
| 276 | index = ~index; |
| 277 | FlatData* copy = (FlatData*)sk_malloc_throw(allocSize); |
| 278 | memcpy(copy, flat, allocSize); |
| 279 | // For bitmaps, we can use zero based indices, since we will never ask |
| 280 | // for a NULL bitmap (unlike with paint flattenables). |
| 281 | copy->fIndex = fBitmapArray.count(); |
| 282 | *fBitmapArray.insert(index) = copy; |
scroggo@google.com | 565254b | 2012-06-28 15:41:32 +0000 | [diff] [blame^] | 283 | if (this->needOpBytes(len)) { |
scroggo@google.com | 16d1d0b | 2012-05-02 19:09:40 +0000 | [diff] [blame] | 284 | this->writeOp(kDef_Bitmap_DrawOp, 0, copy->fIndex); |
scroggo@google.com | 16d1d0b | 2012-05-02 19:09:40 +0000 | [diff] [blame] | 285 | fWriter.write(copy->data(), len); |
| 286 | } |
| 287 | } |
| 288 | return fBitmapArray[index]->fIndex; |
| 289 | } |
| 290 | |
reed@google.com | b55d118 | 2011-05-11 00:42:04 +0000 | [diff] [blame] | 291 | // return 0 for NULL (or unflattenable obj), or index-base-1 |
| 292 | int SkGPipeCanvas::flattenToIndex(SkFlattenable* obj, PaintFlats paintflat) { |
| 293 | if (NULL == obj) { |
| 294 | return 0; |
| 295 | } |
reed@google.com | b55d118 | 2011-05-11 00:42:04 +0000 | [diff] [blame] | 296 | |
djsollen@google.com | 2b2ede3 | 2012-04-12 13:24:04 +0000 | [diff] [blame] | 297 | SkOrderedWriteBuffer tmpWriter(1024); |
scroggo@google.com | 5a2e879 | 2012-04-20 17:39:51 +0000 | [diff] [blame] | 298 | |
scroggo@google.com | 565254b | 2012-06-28 15:41:32 +0000 | [diff] [blame^] | 299 | if (fFlags & SkGPipeWriter::kCrossProcess_Flag) { |
| 300 | tmpWriter.setFlags((SkFlattenableWriteBuffer::Flags) |
| 301 | (SkFlattenableWriteBuffer::kInlineFactoryNames_Flag |
| 302 | | SkFlattenableWriteBuffer::kCrossProcess_Flag)); |
| 303 | tmpWriter.setFactoryRecorder(fFactorySet); |
| 304 | } else { |
| 305 | // Needed for bitmap shaders. |
| 306 | tmpWriter.setFlags(SkFlattenableWriteBuffer::kForceFlattenBitmapPixels_Flag); |
| 307 | } |
reed@google.com | dde0956 | 2011-05-23 12:21:05 +0000 | [diff] [blame] | 308 | |
reed@google.com | b55d118 | 2011-05-11 00:42:04 +0000 | [diff] [blame] | 309 | tmpWriter.writeFlattenable(obj); |
| 310 | size_t len = tmpWriter.size(); |
| 311 | size_t allocSize = len + sizeof(FlatData); |
| 312 | |
| 313 | SkAutoSMalloc<1024> storage(allocSize); |
| 314 | FlatData* flat = (FlatData*)storage.get(); |
| 315 | flat->fSize = len; |
| 316 | tmpWriter.flatten(flat->data()); |
| 317 | |
| 318 | int index = SkTSearch<FlatData>((const FlatData**)fFlatArray.begin(), |
| 319 | fFlatArray.count(), flat, sizeof(flat), |
| 320 | &FlatData::Compare); |
| 321 | if (index < 0) { |
| 322 | index = ~index; |
| 323 | FlatData* copy = (FlatData*)sk_malloc_throw(allocSize); |
| 324 | memcpy(copy, flat, allocSize); |
| 325 | *fFlatArray.insert(index) = copy; |
| 326 | // call this after the insert, so that count() will have been grown |
| 327 | copy->fIndex = fFlatArray.count(); |
| 328 | // SkDebugf("--- add flattenable[%d] size=%d index=%d\n", paintflat, len, copy->fIndex); |
| 329 | |
| 330 | if (this->needOpBytes(len)) { |
reed@google.com | 0faac1e | 2011-05-11 05:58:58 +0000 | [diff] [blame] | 331 | this->writeOp(kDef_Flattenable_DrawOp, paintflat, copy->fIndex); |
reed@google.com | b55d118 | 2011-05-11 00:42:04 +0000 | [diff] [blame] | 332 | fWriter.write(copy->data(), len); |
| 333 | } |
| 334 | } |
| 335 | return fFlatArray[index]->fIndex; |
| 336 | } |
| 337 | |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 338 | /////////////////////////////////////////////////////////////////////////////// |
| 339 | |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 340 | #define MIN_BLOCK_SIZE (16 * 1024) |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 341 | |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 342 | SkGPipeCanvas::SkGPipeCanvas(SkGPipeController* controller, |
scroggo@google.com | 565254b | 2012-06-28 15:41:32 +0000 | [diff] [blame^] | 343 | SkWriter32* writer, SkFactorySet* fset, uint32_t flags) |
| 344 | : fWriter(*writer), fFlags(flags), fHeap(!(flags & SkGPipeWriter::kCrossProcess_Flag)) { |
reed@google.com | 67908f2 | 2011-06-27 14:47:50 +0000 | [diff] [blame] | 345 | fFactorySet = fset; |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 346 | fController = controller; |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 347 | fDone = false; |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 348 | fBlockSize = 0; // need first block from controller |
scroggo@google.com | 5a2e879 | 2012-04-20 17:39:51 +0000 | [diff] [blame] | 349 | fBytesNotified = 0; |
reed@google.com | b55d118 | 2011-05-11 00:42:04 +0000 | [diff] [blame] | 350 | sk_bzero(fCurrFlatIndex, sizeof(fCurrFlatIndex)); |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 351 | |
reed@google.com | bb6793b | 2011-05-05 15:18:15 +0000 | [diff] [blame] | 352 | // we need a device to limit our clip |
| 353 | // should the caller give us the bounds? |
yangsu@google.com | 06b4da16 | 2011-06-17 15:04:40 +0000 | [diff] [blame] | 354 | // We don't allocate pixels for the bitmap |
reed@google.com | bb6793b | 2011-05-05 15:18:15 +0000 | [diff] [blame] | 355 | SkBitmap bitmap; |
| 356 | bitmap.setConfig(SkBitmap::kARGB_8888_Config, 32767, 32767); |
yangsu@google.com | 06b4da16 | 2011-06-17 15:04:40 +0000 | [diff] [blame] | 357 | SkDevice* device = SkNEW_ARGS(SkDevice, (bitmap)); |
reed@google.com | bb6793b | 2011-05-05 15:18:15 +0000 | [diff] [blame] | 358 | this->setDevice(device)->unref(); |
scroggo@google.com | 565254b | 2012-06-28 15:41:32 +0000 | [diff] [blame^] | 359 | // Tell the reader the appropriate flags to use. |
| 360 | if (this->needOpBytes()) { |
| 361 | this->writeOp(kReportFlags_DrawOp, fFlags, 0); |
| 362 | } |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | SkGPipeCanvas::~SkGPipeCanvas() { |
| 366 | this->finish(); |
| 367 | |
reed@google.com | b55d118 | 2011-05-11 00:42:04 +0000 | [diff] [blame] | 368 | fFlatArray.freeAll(); |
scroggo@google.com | 16d1d0b | 2012-05-02 19:09:40 +0000 | [diff] [blame] | 369 | fBitmapArray.freeAll(); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 370 | } |
| 371 | |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 372 | bool SkGPipeCanvas::needOpBytes(size_t needed) { |
| 373 | if (fDone) { |
| 374 | return false; |
| 375 | } |
| 376 | |
| 377 | needed += 4; // size of DrawOp atom |
| 378 | if (fWriter.size() + needed > fBlockSize) { |
scroggo@google.com | 5a2e879 | 2012-04-20 17:39:51 +0000 | [diff] [blame] | 379 | // Before we wipe out any data that has already been written, read it |
| 380 | // out. |
| 381 | this->doNotify(); |
| 382 | size_t blockSize = SkMax32(MIN_BLOCK_SIZE, needed); |
| 383 | void* block = fController->requestBlock(blockSize, &fBlockSize); |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 384 | if (NULL == block) { |
| 385 | fDone = true; |
| 386 | return false; |
| 387 | } |
| 388 | fWriter.reset(block, fBlockSize); |
| 389 | fBytesNotified = 0; |
| 390 | } |
| 391 | return true; |
| 392 | } |
| 393 | |
reed@google.com | f5842f7 | 2011-05-04 18:30:04 +0000 | [diff] [blame] | 394 | uint32_t SkGPipeCanvas::getTypefaceID(SkTypeface* face) { |
| 395 | uint32_t id = 0; // 0 means default/null typeface |
| 396 | if (face) { |
| 397 | id = fTypefaceSet.find(face); |
| 398 | if (0 == id) { |
| 399 | id = fTypefaceSet.add(face); |
| 400 | size_t size = writeTypeface(NULL, face); |
| 401 | if (this->needOpBytes(size)) { |
reed@google.com | bb6793b | 2011-05-05 15:18:15 +0000 | [diff] [blame] | 402 | this->writeOp(kDef_Typeface_DrawOp); |
reed@google.com | f5842f7 | 2011-05-04 18:30:04 +0000 | [diff] [blame] | 403 | writeTypeface(&fWriter, face); |
| 404 | } |
| 405 | } |
| 406 | } |
| 407 | return id; |
| 408 | } |
| 409 | |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 410 | /////////////////////////////////////////////////////////////////////////////// |
| 411 | |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 412 | #define NOTIFY_SETUP(canvas) \ |
| 413 | AutoPipeNotify apn(canvas) |
| 414 | |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 415 | int SkGPipeCanvas::save(SaveFlags flags) { |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 416 | NOTIFY_SETUP(this); |
| 417 | if (this->needOpBytes()) { |
| 418 | this->writeOp(kSave_DrawOp, 0, flags); |
| 419 | } |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 420 | return this->INHERITED::save(flags); |
| 421 | } |
| 422 | |
| 423 | int SkGPipeCanvas::saveLayer(const SkRect* bounds, const SkPaint* paint, |
| 424 | SaveFlags saveFlags) { |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 425 | NOTIFY_SETUP(this); |
| 426 | size_t size = 0; |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 427 | unsigned opFlags = 0; |
vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 428 | |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 429 | if (bounds) { |
| 430 | opFlags |= kSaveLayer_HasBounds_DrawOpFlag; |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 431 | size += sizeof(SkRect); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 432 | } |
| 433 | if (paint) { |
| 434 | opFlags |= kSaveLayer_HasPaint_DrawOpFlag; |
reed@google.com | 3189158 | 2011-05-12 03:03:56 +0000 | [diff] [blame] | 435 | this->writePaint(*paint); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 436 | } |
| 437 | |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 438 | if (this->needOpBytes(size)) { |
| 439 | this->writeOp(kSaveLayer_DrawOp, opFlags, saveFlags); |
| 440 | if (bounds) { |
| 441 | fWriter.writeRect(*bounds); |
| 442 | } |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 443 | } |
vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 444 | |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 445 | // we just pass on the save, so we don't create a layer |
| 446 | return this->INHERITED::save(saveFlags); |
| 447 | } |
| 448 | |
| 449 | void SkGPipeCanvas::restore() { |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 450 | NOTIFY_SETUP(this); |
| 451 | if (this->needOpBytes()) { |
| 452 | this->writeOp(kRestore_DrawOp); |
| 453 | } |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 454 | this->INHERITED::restore(); |
| 455 | } |
| 456 | |
| 457 | bool SkGPipeCanvas::translate(SkScalar dx, SkScalar dy) { |
| 458 | if (dx || dy) { |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 459 | NOTIFY_SETUP(this); |
| 460 | if (this->needOpBytes(2 * sizeof(SkScalar))) { |
| 461 | this->writeOp(kTranslate_DrawOp); |
| 462 | fWriter.writeScalar(dx); |
| 463 | fWriter.writeScalar(dy); |
| 464 | } |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 465 | } |
| 466 | return this->INHERITED::translate(dx, dy); |
| 467 | } |
| 468 | |
| 469 | bool SkGPipeCanvas::scale(SkScalar sx, SkScalar sy) { |
| 470 | if (sx || sy) { |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 471 | NOTIFY_SETUP(this); |
| 472 | if (this->needOpBytes(2 * sizeof(SkScalar))) { |
| 473 | this->writeOp(kScale_DrawOp); |
| 474 | fWriter.writeScalar(sx); |
| 475 | fWriter.writeScalar(sy); |
| 476 | } |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 477 | } |
| 478 | return this->INHERITED::scale(sx, sy); |
| 479 | } |
| 480 | |
| 481 | bool SkGPipeCanvas::rotate(SkScalar degrees) { |
| 482 | if (degrees) { |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 483 | NOTIFY_SETUP(this); |
| 484 | if (this->needOpBytes(sizeof(SkScalar))) { |
| 485 | this->writeOp(kRotate_DrawOp); |
| 486 | fWriter.writeScalar(degrees); |
| 487 | } |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 488 | } |
| 489 | return this->INHERITED::rotate(degrees); |
| 490 | } |
| 491 | |
| 492 | bool SkGPipeCanvas::skew(SkScalar sx, SkScalar sy) { |
| 493 | if (sx || sy) { |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 494 | NOTIFY_SETUP(this); |
| 495 | if (this->needOpBytes(2 * sizeof(SkScalar))) { |
| 496 | this->writeOp(kSkew_DrawOp); |
| 497 | fWriter.writeScalar(sx); |
| 498 | fWriter.writeScalar(sy); |
| 499 | } |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 500 | } |
| 501 | return this->INHERITED::skew(sx, sy); |
| 502 | } |
| 503 | |
| 504 | bool SkGPipeCanvas::concat(const SkMatrix& matrix) { |
| 505 | if (!matrix.isIdentity()) { |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 506 | NOTIFY_SETUP(this); |
djsollen@google.com | 94e75ee | 2012-06-08 18:30:46 +0000 | [diff] [blame] | 507 | if (this->needOpBytes(matrix.writeToMemory(NULL))) { |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 508 | this->writeOp(kConcat_DrawOp); |
djsollen@google.com | 2b2ede3 | 2012-04-12 13:24:04 +0000 | [diff] [blame] | 509 | fWriter.writeMatrix(matrix); |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 510 | } |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 511 | } |
| 512 | return this->INHERITED::concat(matrix); |
| 513 | } |
| 514 | |
| 515 | void SkGPipeCanvas::setMatrix(const SkMatrix& matrix) { |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 516 | NOTIFY_SETUP(this); |
djsollen@google.com | 94e75ee | 2012-06-08 18:30:46 +0000 | [diff] [blame] | 517 | if (this->needOpBytes(matrix.writeToMemory(NULL))) { |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 518 | this->writeOp(kSetMatrix_DrawOp); |
djsollen@google.com | 2b2ede3 | 2012-04-12 13:24:04 +0000 | [diff] [blame] | 519 | fWriter.writeMatrix(matrix); |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 520 | } |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 521 | this->INHERITED::setMatrix(matrix); |
| 522 | } |
| 523 | |
scroggo@google.com | 3b45cd5 | 2012-04-18 13:57:47 +0000 | [diff] [blame] | 524 | bool SkGPipeCanvas::clipRect(const SkRect& rect, SkRegion::Op rgnOp, |
| 525 | bool doAntiAlias) { |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 526 | NOTIFY_SETUP(this); |
scroggo@google.com | 3b45cd5 | 2012-04-18 13:57:47 +0000 | [diff] [blame] | 527 | if (this->needOpBytes(sizeof(SkRect)) + sizeof(bool)) { |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 528 | this->writeOp(kClipRect_DrawOp, 0, rgnOp); |
| 529 | fWriter.writeRect(rect); |
scroggo@google.com | 3b45cd5 | 2012-04-18 13:57:47 +0000 | [diff] [blame] | 530 | fWriter.writeBool(doAntiAlias); |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 531 | } |
scroggo@google.com | 3b45cd5 | 2012-04-18 13:57:47 +0000 | [diff] [blame] | 532 | return this->INHERITED::clipRect(rect, rgnOp, doAntiAlias); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 533 | } |
| 534 | |
scroggo@google.com | 3b45cd5 | 2012-04-18 13:57:47 +0000 | [diff] [blame] | 535 | bool SkGPipeCanvas::clipPath(const SkPath& path, SkRegion::Op rgnOp, |
| 536 | bool doAntiAlias) { |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 537 | NOTIFY_SETUP(this); |
djsollen@google.com | 94e75ee | 2012-06-08 18:30:46 +0000 | [diff] [blame] | 538 | if (this->needOpBytes(path.writeToMemory(NULL)) + sizeof(bool)) { |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 539 | this->writeOp(kClipPath_DrawOp, 0, rgnOp); |
djsollen@google.com | 94e75ee | 2012-06-08 18:30:46 +0000 | [diff] [blame] | 540 | fWriter.writePath(path); |
scroggo@google.com | 3b45cd5 | 2012-04-18 13:57:47 +0000 | [diff] [blame] | 541 | fWriter.writeBool(doAntiAlias); |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 542 | } |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 543 | // we just pass on the bounds of the path |
scroggo@google.com | 3b45cd5 | 2012-04-18 13:57:47 +0000 | [diff] [blame] | 544 | return this->INHERITED::clipRect(path.getBounds(), rgnOp, doAntiAlias); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | bool SkGPipeCanvas::clipRegion(const SkRegion& region, SkRegion::Op rgnOp) { |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 548 | NOTIFY_SETUP(this); |
djsollen@google.com | 94e75ee | 2012-06-08 18:30:46 +0000 | [diff] [blame] | 549 | if (this->needOpBytes(region.writeToMemory(NULL))) { |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 550 | this->writeOp(kClipRegion_DrawOp, 0, rgnOp); |
djsollen@google.com | 2b2ede3 | 2012-04-12 13:24:04 +0000 | [diff] [blame] | 551 | fWriter.writeRegion(region); |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 552 | } |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 553 | return this->INHERITED::clipRegion(region, rgnOp); |
| 554 | } |
| 555 | |
| 556 | /////////////////////////////////////////////////////////////////////////////// |
| 557 | |
| 558 | void SkGPipeCanvas::clear(SkColor color) { |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 559 | NOTIFY_SETUP(this); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 560 | unsigned flags = 0; |
| 561 | if (color) { |
| 562 | flags |= kClear_HasColor_DrawOpFlag; |
| 563 | } |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 564 | if (this->needOpBytes(sizeof(SkColor))) { |
| 565 | this->writeOp(kDrawClear_DrawOp, flags, 0); |
| 566 | if (color) { |
| 567 | fWriter.write32(color); |
| 568 | } |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 569 | } |
| 570 | } |
| 571 | |
| 572 | void SkGPipeCanvas::drawPaint(const SkPaint& paint) { |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 573 | NOTIFY_SETUP(this); |
reed@google.com | 3189158 | 2011-05-12 03:03:56 +0000 | [diff] [blame] | 574 | this->writePaint(paint); |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 575 | if (this->needOpBytes()) { |
reed@google.com | 3189158 | 2011-05-12 03:03:56 +0000 | [diff] [blame] | 576 | this->writeOp(kDrawPaint_DrawOp); |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 577 | } |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 578 | } |
| 579 | |
| 580 | void SkGPipeCanvas::drawPoints(PointMode mode, size_t count, |
| 581 | const SkPoint pts[], const SkPaint& paint) { |
| 582 | if (count) { |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 583 | NOTIFY_SETUP(this); |
reed@google.com | 3189158 | 2011-05-12 03:03:56 +0000 | [diff] [blame] | 584 | this->writePaint(paint); |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 585 | if (this->needOpBytes(4 + count * sizeof(SkPoint))) { |
reed@google.com | 3189158 | 2011-05-12 03:03:56 +0000 | [diff] [blame] | 586 | this->writeOp(kDrawPoints_DrawOp, mode, 0); |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 587 | fWriter.write32(count); |
| 588 | fWriter.write(pts, count * sizeof(SkPoint)); |
| 589 | } |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 590 | } |
| 591 | } |
| 592 | |
| 593 | void SkGPipeCanvas::drawRect(const SkRect& rect, const SkPaint& paint) { |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 594 | NOTIFY_SETUP(this); |
reed@google.com | 3189158 | 2011-05-12 03:03:56 +0000 | [diff] [blame] | 595 | this->writePaint(paint); |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 596 | if (this->needOpBytes(sizeof(SkRect))) { |
reed@google.com | 3189158 | 2011-05-12 03:03:56 +0000 | [diff] [blame] | 597 | this->writeOp(kDrawRect_DrawOp); |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 598 | fWriter.writeRect(rect); |
| 599 | } |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 600 | } |
| 601 | |
| 602 | void SkGPipeCanvas::drawPath(const SkPath& path, const SkPaint& paint) { |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 603 | NOTIFY_SETUP(this); |
reed@google.com | 3189158 | 2011-05-12 03:03:56 +0000 | [diff] [blame] | 604 | this->writePaint(paint); |
djsollen@google.com | 94e75ee | 2012-06-08 18:30:46 +0000 | [diff] [blame] | 605 | if (this->needOpBytes(path.writeToMemory(NULL))) { |
reed@google.com | 3189158 | 2011-05-12 03:03:56 +0000 | [diff] [blame] | 606 | this->writeOp(kDrawPath_DrawOp); |
djsollen@google.com | 94e75ee | 2012-06-08 18:30:46 +0000 | [diff] [blame] | 607 | fWriter.writePath(path); |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 608 | } |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 609 | } |
| 610 | |
scroggo@google.com | 5a2e879 | 2012-04-20 17:39:51 +0000 | [diff] [blame] | 611 | void SkGPipeCanvas::drawBitmap(const SkBitmap& bm, SkScalar left, SkScalar top, |
| 612 | const SkPaint* paint) { |
scroggo@google.com | 565254b | 2012-06-28 15:41:32 +0000 | [diff] [blame^] | 613 | bool flatten = shouldFlattenBitmaps(fFlags); |
| 614 | const void* ptr = 0; |
| 615 | int bitmapIndex = 0; |
| 616 | if (flatten) { |
| 617 | bitmapIndex = this->flattenToIndex(bm); |
| 618 | } else { |
| 619 | ptr = fHeap.addBitmap(bm); |
| 620 | if (NULL == ptr) { |
| 621 | return; |
| 622 | } |
scroggo@google.com | 8ae3c7f | 2012-06-13 17:47:52 +0000 | [diff] [blame] | 623 | } |
| 624 | |
scroggo@google.com | 5a2e879 | 2012-04-20 17:39:51 +0000 | [diff] [blame] | 625 | NOTIFY_SETUP(this); |
| 626 | if (paint) { |
| 627 | this->writePaint(*paint); |
| 628 | } |
scroggo@google.com | 16d1d0b | 2012-05-02 19:09:40 +0000 | [diff] [blame] | 629 | |
scroggo@google.com | 565254b | 2012-06-28 15:41:32 +0000 | [diff] [blame^] | 630 | size_t opBytesNeeded = sizeof(SkScalar) * 2 + sizeof(bool); |
| 631 | if (!flatten) { |
| 632 | opBytesNeeded += sizeof(void*); |
| 633 | } |
| 634 | if (this->needOpBytes(opBytesNeeded)) { |
| 635 | this->writeOp(kDrawBitmap_DrawOp, 0, bitmapIndex); |
| 636 | if (!flatten) { |
| 637 | fWriter.writePtr(const_cast<void*>(ptr)); |
| 638 | } |
scroggo@google.com | 5a2e879 | 2012-04-20 17:39:51 +0000 | [diff] [blame] | 639 | fWriter.writeBool(paint != NULL); |
| 640 | fWriter.writeScalar(left); |
| 641 | fWriter.writeScalar(top); |
| 642 | } |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 643 | } |
| 644 | |
scroggo@google.com | 16d1d0b | 2012-05-02 19:09:40 +0000 | [diff] [blame] | 645 | void SkGPipeCanvas::drawBitmapRect(const SkBitmap& bm, const SkIRect* src, |
| 646 | const SkRect& dst, const SkPaint* paint) { |
scroggo@google.com | 565254b | 2012-06-28 15:41:32 +0000 | [diff] [blame^] | 647 | bool flatten = shouldFlattenBitmaps(fFlags); |
| 648 | const void* ptr = 0; |
| 649 | int bitmapIndex = 0; |
| 650 | if (flatten) { |
| 651 | bitmapIndex = this->flattenToIndex(bm); |
| 652 | } else { |
| 653 | ptr = fHeap.addBitmap(bm); |
| 654 | if (NULL == ptr) { |
| 655 | return; |
| 656 | } |
scroggo@google.com | 8ae3c7f | 2012-06-13 17:47:52 +0000 | [diff] [blame] | 657 | } |
| 658 | |
scroggo@google.com | 16d1d0b | 2012-05-02 19:09:40 +0000 | [diff] [blame] | 659 | NOTIFY_SETUP(this); |
| 660 | if (paint) { |
| 661 | this->writePaint(*paint); |
| 662 | } |
| 663 | |
scroggo@google.com | 565254b | 2012-06-28 15:41:32 +0000 | [diff] [blame^] | 664 | size_t opBytesNeeded = sizeof(SkRect) + sizeof(bool) * 2; |
scroggo@google.com | 16d1d0b | 2012-05-02 19:09:40 +0000 | [diff] [blame] | 665 | bool hasSrc = src != NULL; |
| 666 | if (hasSrc) { |
| 667 | opBytesNeeded += sizeof(int32_t) * 4; |
| 668 | } |
scroggo@google.com | 565254b | 2012-06-28 15:41:32 +0000 | [diff] [blame^] | 669 | if (!flatten) { |
| 670 | opBytesNeeded += sizeof(void*); |
| 671 | } |
scroggo@google.com | 16d1d0b | 2012-05-02 19:09:40 +0000 | [diff] [blame] | 672 | if (this->needOpBytes(opBytesNeeded)) { |
scroggo@google.com | 565254b | 2012-06-28 15:41:32 +0000 | [diff] [blame^] | 673 | this->writeOp(kDrawBitmapRect_DrawOp, 0, bitmapIndex); |
| 674 | if (!flatten) { |
| 675 | fWriter.writePtr(const_cast<void*>(ptr)); |
| 676 | } |
scroggo@google.com | 16d1d0b | 2012-05-02 19:09:40 +0000 | [diff] [blame] | 677 | fWriter.writeBool(paint != NULL); |
| 678 | fWriter.writeBool(hasSrc); |
| 679 | if (hasSrc) { |
| 680 | fWriter.write32(src->fLeft); |
| 681 | fWriter.write32(src->fTop); |
| 682 | fWriter.write32(src->fRight); |
| 683 | fWriter.write32(src->fBottom); |
| 684 | } |
| 685 | fWriter.writeRect(dst); |
| 686 | } |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 687 | } |
| 688 | |
| 689 | void SkGPipeCanvas::drawBitmapMatrix(const SkBitmap&, const SkMatrix&, |
| 690 | const SkPaint*) { |
| 691 | UNIMPLEMENTED |
| 692 | } |
scroggo@google.com | 16d1d0b | 2012-05-02 19:09:40 +0000 | [diff] [blame] | 693 | |
| 694 | void SkGPipeCanvas::drawBitmapNine(const SkBitmap& bm, const SkIRect& center, |
scroggo@google.com | 5a2e879 | 2012-04-20 17:39:51 +0000 | [diff] [blame] | 695 | const SkRect& dst, const SkPaint* paint) { |
scroggo@google.com | 565254b | 2012-06-28 15:41:32 +0000 | [diff] [blame^] | 696 | bool flatten = shouldFlattenBitmaps(fFlags); |
| 697 | const void* ptr = 0; |
| 698 | int bitmapIndex = 0; |
| 699 | if (flatten) { |
| 700 | bitmapIndex = this->flattenToIndex(bm); |
| 701 | } else { |
| 702 | ptr = fHeap.addBitmap(bm); |
| 703 | if (NULL == ptr) { |
| 704 | return; |
| 705 | } |
scroggo@google.com | 8ae3c7f | 2012-06-13 17:47:52 +0000 | [diff] [blame] | 706 | } |
| 707 | |
scroggo@google.com | 16d1d0b | 2012-05-02 19:09:40 +0000 | [diff] [blame] | 708 | NOTIFY_SETUP(this); |
| 709 | if (paint) { |
| 710 | this->writePaint(*paint); |
| 711 | } |
scroggo@google.com | 16d1d0b | 2012-05-02 19:09:40 +0000 | [diff] [blame] | 712 | |
scroggo@google.com | 565254b | 2012-06-28 15:41:32 +0000 | [diff] [blame^] | 713 | size_t opBytesNeeded = sizeof(int32_t) * 4 + sizeof(bool) + sizeof(SkRect); |
| 714 | if (!flatten) { |
| 715 | opBytesNeeded += sizeof(void*); |
| 716 | } |
| 717 | if (this->needOpBytes(opBytesNeeded)) { |
| 718 | this->writeOp(kDrawBitmapNine_DrawOp, 0, bitmapIndex); |
| 719 | if (!flatten) { |
| 720 | fWriter.writePtr(const_cast<void*>(ptr)); |
| 721 | } |
scroggo@google.com | 16d1d0b | 2012-05-02 19:09:40 +0000 | [diff] [blame] | 722 | fWriter.writeBool(paint != NULL); |
| 723 | fWriter.write32(center.fLeft); |
| 724 | fWriter.write32(center.fTop); |
| 725 | fWriter.write32(center.fRight); |
| 726 | fWriter.write32(center.fBottom); |
| 727 | fWriter.writeRect(dst); |
| 728 | } |
scroggo@google.com | 5a2e879 | 2012-04-20 17:39:51 +0000 | [diff] [blame] | 729 | } |
scroggo@google.com | 16d1d0b | 2012-05-02 19:09:40 +0000 | [diff] [blame] | 730 | |
| 731 | void SkGPipeCanvas::drawSprite(const SkBitmap& bm, int left, int top, |
| 732 | const SkPaint* paint) { |
scroggo@google.com | 565254b | 2012-06-28 15:41:32 +0000 | [diff] [blame^] | 733 | bool flatten = shouldFlattenBitmaps(fFlags); |
| 734 | const void* ptr = 0; |
| 735 | int bitmapIndex = 0; |
| 736 | if (flatten) { |
| 737 | bitmapIndex = this->flattenToIndex(bm); |
| 738 | } else { |
| 739 | ptr = fHeap.addBitmap(bm); |
| 740 | if (NULL == ptr) { |
| 741 | return; |
| 742 | } |
scroggo@google.com | 8ae3c7f | 2012-06-13 17:47:52 +0000 | [diff] [blame] | 743 | } |
| 744 | |
scroggo@google.com | 16d1d0b | 2012-05-02 19:09:40 +0000 | [diff] [blame] | 745 | NOTIFY_SETUP(this); |
| 746 | if (paint) { |
| 747 | this->writePaint(*paint); |
| 748 | } |
scroggo@google.com | 16d1d0b | 2012-05-02 19:09:40 +0000 | [diff] [blame] | 749 | |
scroggo@google.com | 565254b | 2012-06-28 15:41:32 +0000 | [diff] [blame^] | 750 | size_t opBytesNeeded = sizeof(int32_t) * 2 + sizeof(bool); |
| 751 | if (!flatten) { |
| 752 | opBytesNeeded += sizeof(void*); |
| 753 | } |
| 754 | if (this->needOpBytes(opBytesNeeded)) { |
| 755 | this->writeOp(kDrawSprite_DrawOp, 0, bitmapIndex); |
| 756 | if (!flatten) { |
| 757 | fWriter.writePtr(const_cast<void*>(ptr)); |
| 758 | } |
scroggo@google.com | 16d1d0b | 2012-05-02 19:09:40 +0000 | [diff] [blame] | 759 | fWriter.writeBool(paint != NULL); |
| 760 | fWriter.write32(left); |
| 761 | fWriter.write32(top); |
| 762 | } |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 763 | } |
| 764 | |
vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 765 | void SkGPipeCanvas::drawText(const void* text, size_t byteLength, SkScalar x, |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 766 | SkScalar y, const SkPaint& paint) { |
| 767 | if (byteLength) { |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 768 | NOTIFY_SETUP(this); |
reed@google.com | 3189158 | 2011-05-12 03:03:56 +0000 | [diff] [blame] | 769 | this->writePaint(paint); |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 770 | if (this->needOpBytes(4 + SkAlign4(byteLength) + 2 * sizeof(SkScalar))) { |
reed@google.com | 3189158 | 2011-05-12 03:03:56 +0000 | [diff] [blame] | 771 | this->writeOp(kDrawText_DrawOp); |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 772 | fWriter.write32(byteLength); |
| 773 | fWriter.writePad(text, byteLength); |
| 774 | fWriter.writeScalar(x); |
| 775 | fWriter.writeScalar(y); |
| 776 | } |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 777 | } |
| 778 | } |
| 779 | |
vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 780 | void SkGPipeCanvas::drawPosText(const void* text, size_t byteLength, |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 781 | const SkPoint pos[], const SkPaint& paint) { |
| 782 | if (byteLength) { |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 783 | NOTIFY_SETUP(this); |
reed@google.com | 3189158 | 2011-05-12 03:03:56 +0000 | [diff] [blame] | 784 | this->writePaint(paint); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 785 | int count = paint.textToGlyphs(text, byteLength, NULL); |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 786 | if (this->needOpBytes(4 + SkAlign4(byteLength) + 4 + count * sizeof(SkPoint))) { |
reed@google.com | 3189158 | 2011-05-12 03:03:56 +0000 | [diff] [blame] | 787 | this->writeOp(kDrawPosText_DrawOp); |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 788 | fWriter.write32(byteLength); |
| 789 | fWriter.writePad(text, byteLength); |
| 790 | fWriter.write32(count); |
| 791 | fWriter.write(pos, count * sizeof(SkPoint)); |
| 792 | } |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 793 | } |
| 794 | } |
| 795 | |
| 796 | void SkGPipeCanvas::drawPosTextH(const void* text, size_t byteLength, |
| 797 | const SkScalar xpos[], SkScalar constY, |
| 798 | const SkPaint& paint) { |
| 799 | if (byteLength) { |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 800 | NOTIFY_SETUP(this); |
reed@google.com | 3189158 | 2011-05-12 03:03:56 +0000 | [diff] [blame] | 801 | this->writePaint(paint); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 802 | int count = paint.textToGlyphs(text, byteLength, NULL); |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 803 | if (this->needOpBytes(4 + SkAlign4(byteLength) + 4 + count * sizeof(SkScalar) + 4)) { |
reed@google.com | 3189158 | 2011-05-12 03:03:56 +0000 | [diff] [blame] | 804 | this->writeOp(kDrawPosTextH_DrawOp); |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 805 | fWriter.write32(byteLength); |
| 806 | fWriter.writePad(text, byteLength); |
| 807 | fWriter.write32(count); |
| 808 | fWriter.write(xpos, count * sizeof(SkScalar)); |
| 809 | fWriter.writeScalar(constY); |
| 810 | } |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 811 | } |
| 812 | } |
| 813 | |
vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 814 | void SkGPipeCanvas::drawTextOnPath(const void* text, size_t byteLength, |
| 815 | const SkPath& path, const SkMatrix* matrix, |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 816 | const SkPaint& paint) { |
| 817 | if (byteLength) { |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 818 | NOTIFY_SETUP(this); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 819 | unsigned flags = 0; |
djsollen@google.com | 94e75ee | 2012-06-08 18:30:46 +0000 | [diff] [blame] | 820 | size_t size = 4 + SkAlign4(byteLength) + path.writeToMemory(NULL); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 821 | if (matrix) { |
| 822 | flags |= kDrawTextOnPath_HasMatrix_DrawOpFlag; |
djsollen@google.com | 94e75ee | 2012-06-08 18:30:46 +0000 | [diff] [blame] | 823 | size += matrix->writeToMemory(NULL); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 824 | } |
reed@google.com | 3189158 | 2011-05-12 03:03:56 +0000 | [diff] [blame] | 825 | this->writePaint(paint); |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 826 | if (this->needOpBytes(size)) { |
reed@google.com | 3189158 | 2011-05-12 03:03:56 +0000 | [diff] [blame] | 827 | this->writeOp(kDrawTextOnPath_DrawOp, flags, 0); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 828 | |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 829 | fWriter.write32(byteLength); |
| 830 | fWriter.writePad(text, byteLength); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 831 | |
djsollen@google.com | 94e75ee | 2012-06-08 18:30:46 +0000 | [diff] [blame] | 832 | fWriter.writePath(path); |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 833 | if (matrix) { |
djsollen@google.com | 2b2ede3 | 2012-04-12 13:24:04 +0000 | [diff] [blame] | 834 | fWriter.writeMatrix(*matrix); |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 835 | } |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 836 | } |
| 837 | } |
| 838 | } |
| 839 | |
| 840 | void SkGPipeCanvas::drawPicture(SkPicture& picture) { |
reed@google.com | 0faac1e | 2011-05-11 05:58:58 +0000 | [diff] [blame] | 841 | // we want to playback the picture into individual draw calls |
| 842 | this->INHERITED::drawPicture(picture); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 843 | } |
| 844 | |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 845 | void SkGPipeCanvas::drawVertices(VertexMode mode, int vertexCount, |
| 846 | const SkPoint vertices[], const SkPoint texs[], |
| 847 | const SkColor colors[], SkXfermode*, |
| 848 | const uint16_t indices[], int indexCount, |
| 849 | const SkPaint& paint) { |
| 850 | if (0 == vertexCount) { |
| 851 | return; |
| 852 | } |
| 853 | |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 854 | NOTIFY_SETUP(this); |
| 855 | size_t size = 4 + vertexCount * sizeof(SkPoint); |
reed@google.com | 3189158 | 2011-05-12 03:03:56 +0000 | [diff] [blame] | 856 | this->writePaint(paint); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 857 | unsigned flags = 0; |
| 858 | if (texs) { |
| 859 | flags |= kDrawVertices_HasTexs_DrawOpFlag; |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 860 | size += vertexCount * sizeof(SkPoint); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 861 | } |
| 862 | if (colors) { |
| 863 | flags |= kDrawVertices_HasColors_DrawOpFlag; |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 864 | size += vertexCount * sizeof(SkColor); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 865 | } |
| 866 | if (indices && indexCount > 0) { |
| 867 | flags |= kDrawVertices_HasIndices_DrawOpFlag; |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 868 | size += 4 + SkAlign4(indexCount * sizeof(uint16_t)); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 869 | } |
vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 870 | |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 871 | if (this->needOpBytes(size)) { |
reed@google.com | 3189158 | 2011-05-12 03:03:56 +0000 | [diff] [blame] | 872 | this->writeOp(kDrawVertices_DrawOp, flags, 0); |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 873 | fWriter.write32(mode); |
| 874 | fWriter.write32(vertexCount); |
| 875 | fWriter.write(vertices, vertexCount * sizeof(SkPoint)); |
| 876 | if (texs) { |
| 877 | fWriter.write(texs, vertexCount * sizeof(SkPoint)); |
| 878 | } |
| 879 | if (colors) { |
| 880 | fWriter.write(colors, vertexCount * sizeof(SkColor)); |
| 881 | } |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 882 | |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 883 | // TODO: flatten xfermode |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 884 | |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 885 | if (indices && indexCount > 0) { |
| 886 | fWriter.write32(indexCount); |
| 887 | fWriter.writePad(indices, indexCount * sizeof(uint16_t)); |
| 888 | } |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 889 | } |
| 890 | } |
| 891 | |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 892 | void SkGPipeCanvas::drawData(const void* ptr, size_t size) { |
| 893 | if (size && ptr) { |
| 894 | NOTIFY_SETUP(this); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 895 | unsigned data = 0; |
| 896 | if (size < (1 << DRAWOPS_DATA_BITS)) { |
| 897 | data = (unsigned)size; |
| 898 | } |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 899 | if (this->needOpBytes(4 + SkAlign4(size))) { |
| 900 | this->writeOp(kDrawData_DrawOp, 0, data); |
| 901 | if (0 == data) { |
| 902 | fWriter.write32(size); |
| 903 | } |
reed@google.com | bb6793b | 2011-05-05 15:18:15 +0000 | [diff] [blame] | 904 | fWriter.writePad(ptr, size); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 905 | } |
| 906 | } |
| 907 | } |
| 908 | |
| 909 | /////////////////////////////////////////////////////////////////////////////// |
| 910 | |
| 911 | template <typename T> uint32_t castToU32(T value) { |
| 912 | union { |
| 913 | T fSrc; |
| 914 | uint32_t fDst; |
| 915 | } data; |
| 916 | data.fSrc = value; |
| 917 | return data.fDst; |
| 918 | } |
| 919 | |
reed@google.com | 3189158 | 2011-05-12 03:03:56 +0000 | [diff] [blame] | 920 | void SkGPipeCanvas::writePaint(const SkPaint& paint) { |
| 921 | SkPaint& base = fPaint; |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 922 | uint32_t storage[32]; |
| 923 | uint32_t* ptr = storage; |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 924 | |
| 925 | if (base.getFlags() != paint.getFlags()) { |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 926 | *ptr++ = PaintOp_packOpData(kFlags_PaintOp, paint.getFlags()); |
reed@google.com | f5842f7 | 2011-05-04 18:30:04 +0000 | [diff] [blame] | 927 | base.setFlags(paint.getFlags()); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 928 | } |
| 929 | if (base.getColor() != paint.getColor()) { |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 930 | *ptr++ = PaintOp_packOp(kColor_PaintOp); |
| 931 | *ptr++ = paint.getColor(); |
reed@google.com | f5842f7 | 2011-05-04 18:30:04 +0000 | [diff] [blame] | 932 | base.setColor(paint.getColor()); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 933 | } |
| 934 | if (base.getStyle() != paint.getStyle()) { |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 935 | *ptr++ = PaintOp_packOpData(kStyle_PaintOp, paint.getStyle()); |
reed@google.com | f5842f7 | 2011-05-04 18:30:04 +0000 | [diff] [blame] | 936 | base.setStyle(paint.getStyle()); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 937 | } |
| 938 | if (base.getStrokeJoin() != paint.getStrokeJoin()) { |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 939 | *ptr++ = PaintOp_packOpData(kJoin_PaintOp, paint.getStrokeJoin()); |
reed@google.com | f5842f7 | 2011-05-04 18:30:04 +0000 | [diff] [blame] | 940 | base.setStrokeJoin(paint.getStrokeJoin()); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 941 | } |
| 942 | if (base.getStrokeCap() != paint.getStrokeCap()) { |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 943 | *ptr++ = PaintOp_packOpData(kCap_PaintOp, paint.getStrokeCap()); |
reed@google.com | f5842f7 | 2011-05-04 18:30:04 +0000 | [diff] [blame] | 944 | base.setStrokeCap(paint.getStrokeCap()); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 945 | } |
| 946 | if (base.getStrokeWidth() != paint.getStrokeWidth()) { |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 947 | *ptr++ = PaintOp_packOp(kWidth_PaintOp); |
| 948 | *ptr++ = castToU32(paint.getStrokeWidth()); |
reed@google.com | f5842f7 | 2011-05-04 18:30:04 +0000 | [diff] [blame] | 949 | base.setStrokeWidth(paint.getStrokeWidth()); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 950 | } |
| 951 | if (base.getStrokeMiter() != paint.getStrokeMiter()) { |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 952 | *ptr++ = PaintOp_packOp(kMiter_PaintOp); |
| 953 | *ptr++ = castToU32(paint.getStrokeMiter()); |
reed@google.com | f5842f7 | 2011-05-04 18:30:04 +0000 | [diff] [blame] | 954 | base.setStrokeMiter(paint.getStrokeMiter()); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 955 | } |
| 956 | if (base.getTextEncoding() != paint.getTextEncoding()) { |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 957 | *ptr++ = PaintOp_packOpData(kEncoding_PaintOp, paint.getTextEncoding()); |
reed@google.com | f5842f7 | 2011-05-04 18:30:04 +0000 | [diff] [blame] | 958 | base.setTextEncoding(paint.getTextEncoding()); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 959 | } |
| 960 | if (base.getHinting() != paint.getHinting()) { |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 961 | *ptr++ = PaintOp_packOpData(kHinting_PaintOp, paint.getHinting()); |
reed@google.com | f5842f7 | 2011-05-04 18:30:04 +0000 | [diff] [blame] | 962 | base.setHinting(paint.getHinting()); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 963 | } |
| 964 | if (base.getTextAlign() != paint.getTextAlign()) { |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 965 | *ptr++ = PaintOp_packOpData(kAlign_PaintOp, paint.getTextAlign()); |
reed@google.com | f5842f7 | 2011-05-04 18:30:04 +0000 | [diff] [blame] | 966 | base.setTextAlign(paint.getTextAlign()); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 967 | } |
| 968 | if (base.getTextSize() != paint.getTextSize()) { |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 969 | *ptr++ = PaintOp_packOp(kTextSize_PaintOp); |
| 970 | *ptr++ = castToU32(paint.getTextSize()); |
reed@google.com | f5842f7 | 2011-05-04 18:30:04 +0000 | [diff] [blame] | 971 | base.setTextSize(paint.getTextSize()); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 972 | } |
| 973 | if (base.getTextScaleX() != paint.getTextScaleX()) { |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 974 | *ptr++ = PaintOp_packOp(kTextScaleX_PaintOp); |
| 975 | *ptr++ = castToU32(paint.getTextScaleX()); |
reed@google.com | f5842f7 | 2011-05-04 18:30:04 +0000 | [diff] [blame] | 976 | base.setTextScaleX(paint.getTextScaleX()); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 977 | } |
| 978 | if (base.getTextSkewX() != paint.getTextSkewX()) { |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 979 | *ptr++ = PaintOp_packOp(kTextSkewX_PaintOp); |
| 980 | *ptr++ = castToU32(paint.getTextSkewX()); |
reed@google.com | f5842f7 | 2011-05-04 18:30:04 +0000 | [diff] [blame] | 981 | base.setTextSkewX(paint.getTextSkewX()); |
| 982 | } |
| 983 | |
| 984 | if (!SkTypeface::Equal(base.getTypeface(), paint.getTypeface())) { |
| 985 | uint32_t id = this->getTypefaceID(paint.getTypeface()); |
reed@google.com | f5842f7 | 2011-05-04 18:30:04 +0000 | [diff] [blame] | 986 | *ptr++ = PaintOp_packOpData(kTypeface_PaintOp, id); |
| 987 | base.setTypeface(paint.getTypeface()); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 988 | } |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 989 | |
reed@google.com | b55d118 | 2011-05-11 00:42:04 +0000 | [diff] [blame] | 990 | for (int i = 0; i < kCount_PaintFlats; i++) { |
| 991 | int index = this->flattenToIndex(get_paintflat(paint, i), (PaintFlats)i); |
| 992 | SkASSERT(index >= 0 && index <= fFlatArray.count()); |
| 993 | if (index != fCurrFlatIndex[i]) { |
reed@google.com | b55d118 | 2011-05-11 00:42:04 +0000 | [diff] [blame] | 994 | *ptr++ = PaintOp_packOpFlagData(kFlatIndex_PaintOp, i, index); |
| 995 | fCurrFlatIndex[i] = index; |
| 996 | } |
| 997 | } |
| 998 | |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 999 | size_t size = (char*)ptr - (char*)storage; |
| 1000 | if (size && this->needOpBytes(size)) { |
reed@google.com | b55d118 | 2011-05-11 00:42:04 +0000 | [diff] [blame] | 1001 | this->writeOp(kPaintOp_DrawOp, 0, size); |
reed@google.com | b55d118 | 2011-05-11 00:42:04 +0000 | [diff] [blame] | 1002 | fWriter.write(storage, size); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1003 | for (size_t i = 0; i < size/4; i++) { |
reed@google.com | b55d118 | 2011-05-11 00:42:04 +0000 | [diff] [blame] | 1004 | // SkDebugf("[%d] %08X\n", i, storage[i]); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1005 | } |
| 1006 | } |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1007 | } |
| 1008 | |
| 1009 | /////////////////////////////////////////////////////////////////////////////// |
| 1010 | |
| 1011 | #include "SkGPipe.h" |
| 1012 | |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 1013 | SkGPipeWriter::SkGPipeWriter() : fWriter(0) { |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1014 | fCanvas = NULL; |
| 1015 | } |
| 1016 | |
| 1017 | SkGPipeWriter::~SkGPipeWriter() { |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 1018 | this->endRecording(); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1019 | SkSafeUnref(fCanvas); |
| 1020 | } |
| 1021 | |
scroggo@google.com | 565254b | 2012-06-28 15:41:32 +0000 | [diff] [blame^] | 1022 | SkCanvas* SkGPipeWriter::startRecording(SkGPipeController* controller, uint32_t flags) { |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1023 | if (NULL == fCanvas) { |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 1024 | fWriter.reset(NULL, 0); |
reed@google.com | dde0956 | 2011-05-23 12:21:05 +0000 | [diff] [blame] | 1025 | fFactorySet.reset(); |
| 1026 | fCanvas = SkNEW_ARGS(SkGPipeCanvas, (controller, &fWriter, |
| 1027 | (flags & kCrossProcess_Flag) ? |
scroggo@google.com | 565254b | 2012-06-28 15:41:32 +0000 | [diff] [blame^] | 1028 | &fFactorySet : NULL, flags)); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1029 | } |
| 1030 | return fCanvas; |
| 1031 | } |
| 1032 | |
| 1033 | void SkGPipeWriter::endRecording() { |
| 1034 | if (fCanvas) { |
| 1035 | fCanvas->finish(); |
| 1036 | fCanvas->unref(); |
| 1037 | fCanvas = NULL; |
| 1038 | } |
| 1039 | } |
| 1040 | |