reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 1 | /* |
| 2 | Copyright 2011 Google Inc. |
| 3 | |
| 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | you may not use this file except in compliance with the License. |
| 6 | You may obtain a copy of the License at |
| 7 | |
| 8 | http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | Unless required by applicable law or agreed to in writing, software |
| 11 | distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | See the License for the specific language governing permissions and |
| 14 | limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | |
| 18 | #include "SkCanvas.h" |
reed@google.com | bb6793b | 2011-05-05 15:18:15 +0000 | [diff] [blame] | 19 | #include "SkDevice.h" |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 20 | #include "SkPaint.h" |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 21 | #include "SkGPipe.h" |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 22 | #include "SkGPipePriv.h" |
reed@google.com | f5842f7 | 2011-05-04 18:30:04 +0000 | [diff] [blame] | 23 | #include "SkStream.h" |
reed@google.com | b55d118 | 2011-05-11 00:42:04 +0000 | [diff] [blame] | 24 | #include "SkTSearch.h" |
reed@google.com | f5842f7 | 2011-05-04 18:30:04 +0000 | [diff] [blame] | 25 | #include "SkTypeface.h" |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 26 | #include "SkWriter32.h" |
reed@google.com | b55d118 | 2011-05-11 00:42:04 +0000 | [diff] [blame] | 27 | #include "SkColorFilter.h" |
reed@google.com | 0faac1e | 2011-05-11 05:58:58 +0000 | [diff] [blame] | 28 | #include "SkDrawLooper.h" |
reed@google.com | b55d118 | 2011-05-11 00:42:04 +0000 | [diff] [blame] | 29 | #include "SkMaskFilter.h" |
| 30 | #include "SkRasterizer.h" |
| 31 | #include "SkShader.h" |
| 32 | |
| 33 | static SkFlattenable* get_paintflat(const SkPaint& paint, unsigned paintFlat) { |
| 34 | SkASSERT(paintFlat < kCount_PaintFlats); |
| 35 | switch (paintFlat) { |
| 36 | case kColorFilter_PaintFlat: return paint.getColorFilter(); |
reed@google.com | 0faac1e | 2011-05-11 05:58:58 +0000 | [diff] [blame] | 37 | case kDrawLooper_PaintFlat: return paint.getLooper(); |
reed@google.com | b55d118 | 2011-05-11 00:42:04 +0000 | [diff] [blame] | 38 | case kMaskFilter_PaintFlat: return paint.getMaskFilter(); |
| 39 | case kPathEffect_PaintFlat: return paint.getPathEffect(); |
| 40 | case kRasterizer_PaintFlat: return paint.getRasterizer(); |
| 41 | case kShader_PaintFlat: return paint.getShader(); |
| 42 | case kXfermode_PaintFlat: return paint.getXfermode(); |
| 43 | } |
| 44 | SkASSERT(!"never gets here"); |
| 45 | return NULL; |
| 46 | } |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 47 | |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 48 | static size_t estimateFlattenSize(const SkPath& path) { |
| 49 | int n = path.countPoints(); |
| 50 | size_t bytes = 3 * sizeof(int32_t); |
| 51 | bytes += n * sizeof(SkPoint); |
| 52 | bytes += SkAlign4(n + 2); // verbs: add 2 for move/close extras |
| 53 | |
| 54 | #ifdef SK_DEBUG |
| 55 | { |
| 56 | SkWriter32 writer(1024); |
| 57 | path.flatten(writer); |
| 58 | SkASSERT(writer.size() <= bytes); |
| 59 | } |
| 60 | #endif |
| 61 | return bytes; |
| 62 | } |
| 63 | |
reed@google.com | f5842f7 | 2011-05-04 18:30:04 +0000 | [diff] [blame] | 64 | static size_t writeTypeface(SkWriter32* writer, SkTypeface* typeface) { |
| 65 | SkASSERT(typeface); |
| 66 | SkDynamicMemoryWStream stream; |
| 67 | typeface->serialize(&stream); |
| 68 | size_t size = stream.getOffset(); |
| 69 | if (writer) { |
| 70 | writer->write32(size); |
| 71 | writer->write(stream.getStream(), size); |
| 72 | } |
| 73 | return 4 + size; |
| 74 | } |
| 75 | |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 76 | /////////////////////////////////////////////////////////////////////////////// |
| 77 | |
| 78 | class SkGPipeCanvas : public SkCanvas { |
| 79 | public: |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 80 | SkGPipeCanvas(SkGPipeController*, SkWriter32*); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 81 | virtual ~SkGPipeCanvas(); |
| 82 | |
| 83 | void finish() { |
| 84 | if (!fDone) { |
| 85 | this->writeOp(kDone_DrawOp); |
| 86 | fDone = true; |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | // overrides from SkCanvas |
| 91 | virtual int save(SaveFlags); |
| 92 | virtual int saveLayer(const SkRect* bounds, const SkPaint*, SaveFlags); |
| 93 | virtual void restore(); |
| 94 | virtual bool translate(SkScalar dx, SkScalar dy); |
| 95 | virtual bool scale(SkScalar sx, SkScalar sy); |
| 96 | virtual bool rotate(SkScalar degrees); |
| 97 | virtual bool skew(SkScalar sx, SkScalar sy); |
| 98 | virtual bool concat(const SkMatrix& matrix); |
| 99 | virtual void setMatrix(const SkMatrix& matrix); |
| 100 | virtual bool clipRect(const SkRect& rect, SkRegion::Op op); |
| 101 | virtual bool clipPath(const SkPath& path, SkRegion::Op op); |
| 102 | virtual bool clipRegion(const SkRegion& region, SkRegion::Op op); |
| 103 | virtual void clear(SkColor); |
| 104 | virtual void drawPaint(const SkPaint& paint); |
| 105 | virtual void drawPoints(PointMode, size_t count, const SkPoint pts[], |
| 106 | const SkPaint&); |
| 107 | virtual void drawRect(const SkRect& rect, const SkPaint&); |
| 108 | virtual void drawPath(const SkPath& path, const SkPaint&); |
| 109 | virtual void drawBitmap(const SkBitmap&, SkScalar left, SkScalar top, |
| 110 | const SkPaint*); |
| 111 | virtual void drawBitmapRect(const SkBitmap&, const SkIRect* src, |
| 112 | const SkRect& dst, const SkPaint*); |
| 113 | virtual void drawBitmapMatrix(const SkBitmap&, const SkMatrix&, |
| 114 | const SkPaint*); |
| 115 | virtual void drawSprite(const SkBitmap&, int left, int top, |
| 116 | const SkPaint*); |
| 117 | virtual void drawText(const void* text, size_t byteLength, SkScalar x, |
| 118 | SkScalar y, const SkPaint&); |
| 119 | virtual void drawPosText(const void* text, size_t byteLength, |
| 120 | const SkPoint pos[], const SkPaint&); |
| 121 | virtual void drawPosTextH(const void* text, size_t byteLength, |
| 122 | const SkScalar xpos[], SkScalar constY, const SkPaint&); |
| 123 | virtual void drawTextOnPath(const void* text, size_t byteLength, |
| 124 | const SkPath& path, const SkMatrix* matrix, |
| 125 | const SkPaint&); |
| 126 | virtual void drawPicture(SkPicture& picture); |
| 127 | virtual void drawShape(SkShape*); |
| 128 | virtual void drawVertices(VertexMode, int vertexCount, |
| 129 | const SkPoint vertices[], const SkPoint texs[], |
| 130 | const SkColor colors[], SkXfermode*, |
| 131 | const uint16_t indices[], int indexCount, |
| 132 | const SkPaint&); |
| 133 | virtual void drawData(const void*, size_t); |
| 134 | |
| 135 | private: |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 136 | SkGPipeController* fController; |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 137 | SkWriter32& fWriter; |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 138 | size_t fBlockSize; // amount allocated for writer |
| 139 | size_t fBytesNotified; |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 140 | bool fDone; |
| 141 | |
reed@google.com | f5842f7 | 2011-05-04 18:30:04 +0000 | [diff] [blame] | 142 | SkRefCntSet fTypefaceSet; |
| 143 | |
| 144 | uint32_t getTypefaceID(SkTypeface*); |
| 145 | |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 146 | inline void writeOp(DrawOps op, unsigned flags, unsigned data) { |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 147 | fWriter.write32(DrawOp_packOpFlagData(op, flags, data)); |
| 148 | } |
| 149 | |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 150 | inline void writeOp(DrawOps op) { |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 151 | fWriter.write32(DrawOp_packOpFlagData(op, 0, 0)); |
| 152 | } |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 153 | |
| 154 | bool needOpBytes(size_t size = 0); |
| 155 | |
| 156 | inline void doNotify() { |
| 157 | if (!fDone) { |
| 158 | size_t bytes = fWriter.size() - fBytesNotified; |
| 159 | fController->notifyWritten(bytes); |
| 160 | fBytesNotified += bytes; |
| 161 | } |
| 162 | } |
reed@google.com | b55d118 | 2011-05-11 00:42:04 +0000 | [diff] [blame] | 163 | |
| 164 | struct FlatData { |
| 165 | uint32_t fIndex; // always > 0 |
| 166 | uint32_t fSize; |
| 167 | |
| 168 | void* data() { return (char*)this + sizeof(*this); } |
| 169 | |
| 170 | static int Compare(const FlatData* a, const FlatData* b) { |
| 171 | return memcmp(&a->fSize, &b->fSize, a->fSize + sizeof(a->fSize)); |
| 172 | } |
| 173 | }; |
| 174 | SkTDArray<FlatData*> fFlatArray; |
| 175 | int fCurrFlatIndex[kCount_PaintFlats]; |
| 176 | int flattenToIndex(SkFlattenable* obj, PaintFlats); |
| 177 | |
reed@google.com | 3189158 | 2011-05-12 03:03:56 +0000 | [diff] [blame^] | 178 | SkPaint fPaint; |
| 179 | void writePaint(const SkPaint&); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 180 | |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 181 | class AutoPipeNotify { |
| 182 | public: |
| 183 | AutoPipeNotify(SkGPipeCanvas* canvas) : fCanvas(canvas) {} |
| 184 | ~AutoPipeNotify() { fCanvas->doNotify(); } |
| 185 | private: |
| 186 | SkGPipeCanvas* fCanvas; |
| 187 | }; |
| 188 | friend class AutoPipeNotify; |
| 189 | |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 190 | typedef SkCanvas INHERITED; |
| 191 | }; |
| 192 | |
reed@google.com | b55d118 | 2011-05-11 00:42:04 +0000 | [diff] [blame] | 193 | // return 0 for NULL (or unflattenable obj), or index-base-1 |
| 194 | int SkGPipeCanvas::flattenToIndex(SkFlattenable* obj, PaintFlats paintflat) { |
| 195 | if (NULL == obj) { |
| 196 | return 0; |
| 197 | } |
| 198 | |
| 199 | SkFlattenable::Factory fact = obj->getFactory(); |
| 200 | if (NULL == fact) { |
| 201 | return 0; |
| 202 | } |
| 203 | |
| 204 | SkFlattenableWriteBuffer tmpWriter(1024); |
| 205 | tmpWriter.writeFlattenable(obj); |
| 206 | size_t len = tmpWriter.size(); |
| 207 | size_t allocSize = len + sizeof(FlatData); |
| 208 | |
| 209 | SkAutoSMalloc<1024> storage(allocSize); |
| 210 | FlatData* flat = (FlatData*)storage.get(); |
| 211 | flat->fSize = len; |
| 212 | tmpWriter.flatten(flat->data()); |
| 213 | |
| 214 | int index = SkTSearch<FlatData>((const FlatData**)fFlatArray.begin(), |
| 215 | fFlatArray.count(), flat, sizeof(flat), |
| 216 | &FlatData::Compare); |
| 217 | if (index < 0) { |
| 218 | index = ~index; |
| 219 | FlatData* copy = (FlatData*)sk_malloc_throw(allocSize); |
| 220 | memcpy(copy, flat, allocSize); |
| 221 | *fFlatArray.insert(index) = copy; |
| 222 | // call this after the insert, so that count() will have been grown |
| 223 | copy->fIndex = fFlatArray.count(); |
| 224 | // SkDebugf("--- add flattenable[%d] size=%d index=%d\n", paintflat, len, copy->fIndex); |
| 225 | |
| 226 | if (this->needOpBytes(len)) { |
reed@google.com | 0faac1e | 2011-05-11 05:58:58 +0000 | [diff] [blame] | 227 | this->writeOp(kDef_Flattenable_DrawOp, paintflat, copy->fIndex); |
reed@google.com | b55d118 | 2011-05-11 00:42:04 +0000 | [diff] [blame] | 228 | fWriter.write(copy->data(), len); |
| 229 | } |
| 230 | } |
| 231 | return fFlatArray[index]->fIndex; |
| 232 | } |
| 233 | |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 234 | /////////////////////////////////////////////////////////////////////////////// |
| 235 | |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 236 | #define MIN_BLOCK_SIZE (16 * 1024) |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 237 | |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 238 | SkGPipeCanvas::SkGPipeCanvas(SkGPipeController* controller, |
| 239 | SkWriter32* writer) : fWriter(*writer) { |
| 240 | fController = controller; |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 241 | fDone = false; |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 242 | fBlockSize = 0; // need first block from controller |
reed@google.com | b55d118 | 2011-05-11 00:42:04 +0000 | [diff] [blame] | 243 | sk_bzero(fCurrFlatIndex, sizeof(fCurrFlatIndex)); |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 244 | |
reed@google.com | bb6793b | 2011-05-05 15:18:15 +0000 | [diff] [blame] | 245 | // we need a device to limit our clip |
| 246 | // should the caller give us the bounds? |
| 247 | SkBitmap bitmap; |
| 248 | bitmap.setConfig(SkBitmap::kARGB_8888_Config, 32767, 32767); |
| 249 | SkDevice* device = SkNEW_ARGS(SkDevice, (this, bitmap, false)); |
| 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; |
reed@google.com | acd471f | 2011-05-03 21:26:46 +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 | } |
| 327 | |
| 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); |
reed@google.com | b55d118 | 2011-05-11 00:42:04 +0000 | [diff] [blame] | 392 | SkWriteMatrix(&fWriter, 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); |
reed@google.com | b55d118 | 2011-05-11 00:42:04 +0000 | [diff] [blame] | 402 | SkWriteMatrix(&fWriter, 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); |
reed@google.com | b55d118 | 2011-05-11 00:42:04 +0000 | [diff] [blame] | 430 | SkWriteRegion(&fWriter, 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 | |
| 510 | void SkGPipeCanvas::drawText(const void* text, size_t byteLength, SkScalar x, |
| 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 | |
| 525 | void SkGPipeCanvas::drawPosText(const void* text, size_t byteLength, |
| 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 | |
| 559 | void SkGPipeCanvas::drawTextOnPath(const void* text, size_t byteLength, |
| 560 | const SkPath& path, const SkMatrix* matrix, |
| 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) { |
reed@google.com | b55d118 | 2011-05-11 00:42:04 +0000 | [diff] [blame] | 579 | SkWriteMatrix(&fWriter, *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 | |
| 590 | void SkGPipeCanvas::drawShape(SkShape* shape) { |
| 591 | UNIMPLEMENTED |
| 592 | } |
| 593 | |
| 594 | void SkGPipeCanvas::drawVertices(VertexMode mode, int vertexCount, |
| 595 | const SkPoint vertices[], const SkPoint texs[], |
| 596 | const SkColor colors[], SkXfermode*, |
| 597 | const uint16_t indices[], int indexCount, |
| 598 | const SkPaint& paint) { |
| 599 | if (0 == vertexCount) { |
| 600 | return; |
| 601 | } |
| 602 | |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 603 | NOTIFY_SETUP(this); |
| 604 | size_t size = 4 + vertexCount * sizeof(SkPoint); |
reed@google.com | 3189158 | 2011-05-12 03:03:56 +0000 | [diff] [blame^] | 605 | this->writePaint(paint); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 606 | unsigned flags = 0; |
| 607 | if (texs) { |
| 608 | flags |= kDrawVertices_HasTexs_DrawOpFlag; |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 609 | size += vertexCount * sizeof(SkPoint); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 610 | } |
| 611 | if (colors) { |
| 612 | flags |= kDrawVertices_HasColors_DrawOpFlag; |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 613 | size += vertexCount * sizeof(SkColor); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 614 | } |
| 615 | if (indices && indexCount > 0) { |
| 616 | flags |= kDrawVertices_HasIndices_DrawOpFlag; |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 617 | size += 4 + SkAlign4(indexCount * sizeof(uint16_t)); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 618 | } |
| 619 | |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 620 | if (this->needOpBytes(size)) { |
reed@google.com | 3189158 | 2011-05-12 03:03:56 +0000 | [diff] [blame^] | 621 | this->writeOp(kDrawVertices_DrawOp, flags, 0); |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 622 | fWriter.write32(mode); |
| 623 | fWriter.write32(vertexCount); |
| 624 | fWriter.write(vertices, vertexCount * sizeof(SkPoint)); |
| 625 | if (texs) { |
| 626 | fWriter.write(texs, vertexCount * sizeof(SkPoint)); |
| 627 | } |
| 628 | if (colors) { |
| 629 | fWriter.write(colors, vertexCount * sizeof(SkColor)); |
| 630 | } |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 631 | |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 632 | // TODO: flatten xfermode |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 633 | |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 634 | if (indices && indexCount > 0) { |
| 635 | fWriter.write32(indexCount); |
| 636 | fWriter.writePad(indices, indexCount * sizeof(uint16_t)); |
| 637 | } |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 638 | } |
| 639 | } |
| 640 | |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 641 | void SkGPipeCanvas::drawData(const void* ptr, size_t size) { |
| 642 | if (size && ptr) { |
| 643 | NOTIFY_SETUP(this); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 644 | unsigned data = 0; |
| 645 | if (size < (1 << DRAWOPS_DATA_BITS)) { |
| 646 | data = (unsigned)size; |
| 647 | } |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 648 | if (this->needOpBytes(4 + SkAlign4(size))) { |
| 649 | this->writeOp(kDrawData_DrawOp, 0, data); |
| 650 | if (0 == data) { |
| 651 | fWriter.write32(size); |
| 652 | } |
reed@google.com | bb6793b | 2011-05-05 15:18:15 +0000 | [diff] [blame] | 653 | fWriter.writePad(ptr, size); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 654 | } |
| 655 | } |
| 656 | } |
| 657 | |
| 658 | /////////////////////////////////////////////////////////////////////////////// |
| 659 | |
| 660 | template <typename T> uint32_t castToU32(T value) { |
| 661 | union { |
| 662 | T fSrc; |
| 663 | uint32_t fDst; |
| 664 | } data; |
| 665 | data.fSrc = value; |
| 666 | return data.fDst; |
| 667 | } |
| 668 | |
reed@google.com | 3189158 | 2011-05-12 03:03:56 +0000 | [diff] [blame^] | 669 | void SkGPipeCanvas::writePaint(const SkPaint& paint) { |
| 670 | SkPaint& base = fPaint; |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 671 | uint32_t storage[32]; |
| 672 | uint32_t* ptr = storage; |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 673 | |
| 674 | if (base.getFlags() != paint.getFlags()) { |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 675 | *ptr++ = PaintOp_packOpData(kFlags_PaintOp, paint.getFlags()); |
reed@google.com | f5842f7 | 2011-05-04 18:30:04 +0000 | [diff] [blame] | 676 | base.setFlags(paint.getFlags()); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 677 | } |
| 678 | if (base.getColor() != paint.getColor()) { |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 679 | *ptr++ = PaintOp_packOp(kColor_PaintOp); |
| 680 | *ptr++ = paint.getColor(); |
reed@google.com | f5842f7 | 2011-05-04 18:30:04 +0000 | [diff] [blame] | 681 | base.setColor(paint.getColor()); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 682 | } |
| 683 | if (base.getStyle() != paint.getStyle()) { |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 684 | *ptr++ = PaintOp_packOpData(kStyle_PaintOp, paint.getStyle()); |
reed@google.com | f5842f7 | 2011-05-04 18:30:04 +0000 | [diff] [blame] | 685 | base.setStyle(paint.getStyle()); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 686 | } |
| 687 | if (base.getStrokeJoin() != paint.getStrokeJoin()) { |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 688 | *ptr++ = PaintOp_packOpData(kJoin_PaintOp, paint.getStrokeJoin()); |
reed@google.com | f5842f7 | 2011-05-04 18:30:04 +0000 | [diff] [blame] | 689 | base.setStrokeJoin(paint.getStrokeJoin()); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 690 | } |
| 691 | if (base.getStrokeCap() != paint.getStrokeCap()) { |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 692 | *ptr++ = PaintOp_packOpData(kCap_PaintOp, paint.getStrokeCap()); |
reed@google.com | f5842f7 | 2011-05-04 18:30:04 +0000 | [diff] [blame] | 693 | base.setStrokeCap(paint.getStrokeCap()); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 694 | } |
| 695 | if (base.getStrokeWidth() != paint.getStrokeWidth()) { |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 696 | *ptr++ = PaintOp_packOp(kWidth_PaintOp); |
| 697 | *ptr++ = castToU32(paint.getStrokeWidth()); |
reed@google.com | f5842f7 | 2011-05-04 18:30:04 +0000 | [diff] [blame] | 698 | base.setStrokeWidth(paint.getStrokeWidth()); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 699 | } |
| 700 | if (base.getStrokeMiter() != paint.getStrokeMiter()) { |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 701 | *ptr++ = PaintOp_packOp(kMiter_PaintOp); |
| 702 | *ptr++ = castToU32(paint.getStrokeMiter()); |
reed@google.com | f5842f7 | 2011-05-04 18:30:04 +0000 | [diff] [blame] | 703 | base.setStrokeMiter(paint.getStrokeMiter()); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 704 | } |
| 705 | if (base.getTextEncoding() != paint.getTextEncoding()) { |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 706 | *ptr++ = PaintOp_packOpData(kEncoding_PaintOp, paint.getTextEncoding()); |
reed@google.com | f5842f7 | 2011-05-04 18:30:04 +0000 | [diff] [blame] | 707 | base.setTextEncoding(paint.getTextEncoding()); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 708 | } |
| 709 | if (base.getHinting() != paint.getHinting()) { |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 710 | *ptr++ = PaintOp_packOpData(kHinting_PaintOp, paint.getHinting()); |
reed@google.com | f5842f7 | 2011-05-04 18:30:04 +0000 | [diff] [blame] | 711 | base.setHinting(paint.getHinting()); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 712 | } |
| 713 | if (base.getTextAlign() != paint.getTextAlign()) { |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 714 | *ptr++ = PaintOp_packOpData(kAlign_PaintOp, paint.getTextAlign()); |
reed@google.com | f5842f7 | 2011-05-04 18:30:04 +0000 | [diff] [blame] | 715 | base.setTextAlign(paint.getTextAlign()); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 716 | } |
| 717 | if (base.getTextSize() != paint.getTextSize()) { |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 718 | *ptr++ = PaintOp_packOp(kTextSize_PaintOp); |
| 719 | *ptr++ = castToU32(paint.getTextSize()); |
reed@google.com | f5842f7 | 2011-05-04 18:30:04 +0000 | [diff] [blame] | 720 | base.setTextSize(paint.getTextSize()); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 721 | } |
| 722 | if (base.getTextScaleX() != paint.getTextScaleX()) { |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 723 | *ptr++ = PaintOp_packOp(kTextScaleX_PaintOp); |
| 724 | *ptr++ = castToU32(paint.getTextScaleX()); |
reed@google.com | f5842f7 | 2011-05-04 18:30:04 +0000 | [diff] [blame] | 725 | base.setTextScaleX(paint.getTextScaleX()); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 726 | } |
| 727 | if (base.getTextSkewX() != paint.getTextSkewX()) { |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 728 | *ptr++ = PaintOp_packOp(kTextSkewX_PaintOp); |
| 729 | *ptr++ = castToU32(paint.getTextSkewX()); |
reed@google.com | f5842f7 | 2011-05-04 18:30:04 +0000 | [diff] [blame] | 730 | base.setTextSkewX(paint.getTextSkewX()); |
| 731 | } |
| 732 | |
| 733 | if (!SkTypeface::Equal(base.getTypeface(), paint.getTypeface())) { |
| 734 | uint32_t id = this->getTypefaceID(paint.getTypeface()); |
reed@google.com | f5842f7 | 2011-05-04 18:30:04 +0000 | [diff] [blame] | 735 | *ptr++ = PaintOp_packOpData(kTypeface_PaintOp, id); |
| 736 | base.setTypeface(paint.getTypeface()); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 737 | } |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 738 | |
reed@google.com | b55d118 | 2011-05-11 00:42:04 +0000 | [diff] [blame] | 739 | for (int i = 0; i < kCount_PaintFlats; i++) { |
| 740 | int index = this->flattenToIndex(get_paintflat(paint, i), (PaintFlats)i); |
| 741 | SkASSERT(index >= 0 && index <= fFlatArray.count()); |
| 742 | if (index != fCurrFlatIndex[i]) { |
reed@google.com | b55d118 | 2011-05-11 00:42:04 +0000 | [diff] [blame] | 743 | *ptr++ = PaintOp_packOpFlagData(kFlatIndex_PaintOp, i, index); |
| 744 | fCurrFlatIndex[i] = index; |
| 745 | } |
| 746 | } |
| 747 | |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 748 | size_t size = (char*)ptr - (char*)storage; |
| 749 | if (size && this->needOpBytes(size)) { |
reed@google.com | b55d118 | 2011-05-11 00:42:04 +0000 | [diff] [blame] | 750 | this->writeOp(kPaintOp_DrawOp, 0, size); |
reed@google.com | b55d118 | 2011-05-11 00:42:04 +0000 | [diff] [blame] | 751 | fWriter.write(storage, size); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 752 | for (size_t i = 0; i < size/4; i++) { |
reed@google.com | b55d118 | 2011-05-11 00:42:04 +0000 | [diff] [blame] | 753 | // SkDebugf("[%d] %08X\n", i, storage[i]); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 754 | } |
| 755 | } |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 756 | } |
| 757 | |
| 758 | /////////////////////////////////////////////////////////////////////////////// |
| 759 | |
| 760 | #include "SkGPipe.h" |
| 761 | |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 762 | SkGPipeWriter::SkGPipeWriter() : fWriter(0) { |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 763 | fCanvas = NULL; |
| 764 | } |
| 765 | |
| 766 | SkGPipeWriter::~SkGPipeWriter() { |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 767 | this->endRecording(); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 768 | SkSafeUnref(fCanvas); |
| 769 | } |
| 770 | |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 771 | SkCanvas* SkGPipeWriter::startRecording(SkGPipeController* controller) { |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 772 | if (NULL == fCanvas) { |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 773 | fWriter.reset(NULL, 0); |
| 774 | fCanvas = SkNEW_ARGS(SkGPipeCanvas, (controller, &fWriter)); |
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 | |