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