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