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