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