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