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