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