SkGPipeRead.cpp revision ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976e
1 2/* 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. 7 */ 8 9 10 11#include "SkCanvas.h" 12#include "SkPaint.h" 13#include "SkGPipe.h" 14#include "SkGPipePriv.h" 15#include "SkReader32.h" 16#include "SkStream.h" 17 18#include "SkColorFilter.h" 19#include "SkDrawLooper.h" 20#include "SkMaskFilter.h" 21#include "SkPathEffect.h" 22#include "SkRasterizer.h" 23#include "SkShader.h" 24#include "SkTypeface.h" 25#include "SkXfermode.h" 26 27static void set_paintflat(SkPaint* paint, SkFlattenable* obj, unsigned paintFlat) { 28 SkASSERT(paintFlat < kCount_PaintFlats); 29 switch (paintFlat) { 30 case kColorFilter_PaintFlat: 31 paint->setColorFilter((SkColorFilter*)obj); 32 break; 33 case kDrawLooper_PaintFlat: 34 paint->setLooper((SkDrawLooper*)obj); 35 break; 36 case kMaskFilter_PaintFlat: 37 paint->setMaskFilter((SkMaskFilter*)obj); 38 break; 39 case kPathEffect_PaintFlat: 40 paint->setPathEffect((SkPathEffect*)obj); 41 break; 42 case kRasterizer_PaintFlat: 43 paint->setRasterizer((SkRasterizer*)obj); 44 break; 45 case kShader_PaintFlat: 46 paint->setShader((SkShader*)obj); 47 break; 48 case kXfermode_PaintFlat: 49 paint->setXfermode((SkXfermode*)obj); 50 break; 51 default: 52 SkASSERT(!"never gets here"); 53 } 54} 55 56template <typename T> class SkRefCntTDArray : public SkTDArray<T> { 57public: 58 ~SkRefCntTDArray() { this->unrefAll(); } 59}; 60 61class SkGPipeState { 62public: 63 SkGPipeState(); 64 ~SkGPipeState(); 65 66 void setReader(SkFlattenableReadBuffer* reader) { 67 fReader = reader; 68 fReader->setFactoryArray(&fFactoryArray); 69 } 70 71 const SkPaint& paint() const { return fPaint; } 72 SkPaint* editPaint() { return &fPaint; } 73 74 SkFlattenable* getFlat(unsigned index) const { 75 if (0 == index) { 76 return NULL; 77 } 78 return fFlatArray[index - 1]; 79 } 80 81 void defFlattenable(PaintFlats pf, int index) { 82 SkASSERT(index == fFlatArray.count() + 1); 83 SkFlattenable* obj = fReader->readFlattenable(); 84 *fFlatArray.append() = obj; 85 } 86 87 void addTypeface() { 88 size_t size = fReader->readU32(); 89 const void* data = fReader->skip(SkAlign4(size)); 90 SkMemoryStream stream(data, size, false); 91 *fTypefaces.append() = SkTypeface::Deserialize(&stream); 92 } 93 void setTypeface(SkPaint* paint, unsigned id) { 94 paint->setTypeface(id ? fTypefaces[id - 1] : NULL); 95 } 96 97 SkFlattenableReadBuffer* fReader; 98 99private: 100 SkPaint fPaint; 101 SkTDArray<SkFlattenable*> fFlatArray; 102 SkTDArray<SkTypeface*> fTypefaces; 103 SkTDArray<SkFlattenable::Factory> fFactoryArray; 104}; 105 106/////////////////////////////////////////////////////////////////////////////// 107 108template <typename T> const T* skip(SkReader32* reader, int count = 1) { 109 SkASSERT(count >= 0); 110 size_t size = sizeof(T) * count; 111 SkASSERT(SkAlign4(size) == size); 112 return reinterpret_cast<const T*>(reader->skip(size)); 113} 114 115template <typename T> const T* skipAlign(SkReader32* reader, int count = 1) { 116 SkASSERT(count >= 0); 117 size_t size = SkAlign4(sizeof(T) * count); 118 return reinterpret_cast<const T*>(reader->skip(size)); 119} 120 121/////////////////////////////////////////////////////////////////////////////// 122/////////////////////////////////////////////////////////////////////////////// 123 124static void clipPath_rp(SkCanvas* canvas, SkReader32* reader, uint32_t op32, 125 SkGPipeState* state) { 126 SkPath path; 127 path.unflatten(*reader); 128 canvas->clipPath(path, (SkRegion::Op)DrawOp_unpackData(op32)); 129} 130 131static void clipRegion_rp(SkCanvas* canvas, SkReader32* reader, uint32_t op32, 132 SkGPipeState* state) { 133 SkRegion rgn; 134 SkReadRegion(reader, &rgn); 135 canvas->clipRegion(rgn, (SkRegion::Op)DrawOp_unpackData(op32)); 136} 137 138static void clipRect_rp(SkCanvas* canvas, SkReader32* reader, uint32_t op32, 139 SkGPipeState* state) { 140 canvas->clipRect(*skip<SkRect>(reader), (SkRegion::Op)DrawOp_unpackData(op32)); 141} 142 143/////////////////////////////////////////////////////////////////////////////// 144 145static void setMatrix_rp(SkCanvas* canvas, SkReader32* reader, uint32_t op32, 146 SkGPipeState* state) { 147 SkMatrix matrix; 148 SkReadMatrix(reader, &matrix); 149 canvas->setMatrix(matrix); 150} 151 152static void concat_rp(SkCanvas* canvas, SkReader32* reader, uint32_t op32, 153 SkGPipeState* state) { 154 SkMatrix matrix; 155 SkReadMatrix(reader, &matrix); 156 canvas->concat(matrix); 157} 158 159static void scale_rp(SkCanvas* canvas, SkReader32* reader, uint32_t op32, 160 SkGPipeState* state) { 161 const SkScalar* param = skip<SkScalar>(reader, 2); 162 canvas->scale(param[0], param[1]); 163} 164 165static void skew_rp(SkCanvas* canvas, SkReader32* reader, uint32_t op32, 166 SkGPipeState* state) { 167 const SkScalar* param = skip<SkScalar>(reader, 2); 168 canvas->skew(param[0], param[1]); 169} 170 171static void rotate_rp(SkCanvas* canvas, SkReader32* reader, uint32_t op32, 172 SkGPipeState* state) { 173 canvas->rotate(reader->readScalar()); 174} 175 176static void translate_rp(SkCanvas* canvas, SkReader32* reader, uint32_t op32, 177 SkGPipeState* state) { 178 const SkScalar* param = skip<SkScalar>(reader, 2); 179 canvas->translate(param[0], param[1]); 180} 181 182/////////////////////////////////////////////////////////////////////////////// 183 184static void save_rp(SkCanvas* canvas, SkReader32* reader, uint32_t op32, 185 SkGPipeState* state) { 186 canvas->save((SkCanvas::SaveFlags)DrawOp_unpackData(op32)); 187} 188 189static void saveLayer_rp(SkCanvas* canvas, SkReader32* reader, uint32_t op32, 190 SkGPipeState* state) { 191 unsigned flags = DrawOp_unpackFlags(op32); 192 SkCanvas::SaveFlags saveFlags = (SkCanvas::SaveFlags)DrawOp_unpackData(op32); 193 194 const SkRect* bounds = NULL; 195 if (flags & kSaveLayer_HasBounds_DrawOpFlag) { 196 bounds = skip<SkRect>(reader); 197 } 198 const SkPaint* paint = NULL; 199 if (flags & kSaveLayer_HasPaint_DrawOpFlag) { 200 paint = &state->paint(); 201 } 202 canvas->saveLayer(bounds, paint, saveFlags); 203} 204 205static void restore_rp(SkCanvas* canvas, SkReader32* reader, uint32_t op32, 206 SkGPipeState* state) { 207 canvas->restore(); 208} 209 210/////////////////////////////////////////////////////////////////////////////// 211 212static void drawClear_rp(SkCanvas* canvas, SkReader32* reader, uint32_t op32, 213 SkGPipeState* state) { 214 SkColor color = 0; 215 if (DrawOp_unpackFlags(op32) & kClear_HasColor_DrawOpFlag) { 216 color = reader->readU32(); 217 } 218 canvas->clear(color); 219} 220 221static void drawPaint_rp(SkCanvas* canvas, SkReader32* reader, uint32_t op32, 222 SkGPipeState* state) { 223 canvas->drawPaint(state->paint()); 224} 225 226static void drawPoints_rp(SkCanvas* canvas, SkReader32* reader, uint32_t op32, 227 SkGPipeState* state) { 228 SkCanvas::PointMode mode = (SkCanvas::PointMode)DrawOp_unpackFlags(op32); 229 size_t count = reader->readU32(); 230 const SkPoint* pts = skip<SkPoint>(reader, count); 231 canvas->drawPoints(mode, count, pts, state->paint()); 232} 233 234static void drawRect_rp(SkCanvas* canvas, SkReader32* reader, uint32_t op32, 235 SkGPipeState* state) { 236 canvas->drawRect(*skip<SkRect>(reader), state->paint()); 237} 238 239static void drawPath_rp(SkCanvas* canvas, SkReader32* reader, uint32_t op32, 240 SkGPipeState* state) { 241 SkPath path; 242 path.unflatten(*reader); 243 canvas->drawPath(path, state->paint()); 244} 245 246static void drawVertices_rp(SkCanvas* canvas, SkReader32* reader, uint32_t op32, 247 SkGPipeState* state) { 248 unsigned flags = DrawOp_unpackFlags(op32); 249 250 SkCanvas::VertexMode mode = (SkCanvas::VertexMode)reader->readU32(); 251 int vertexCount = reader->readU32(); 252 const SkPoint* verts = skip<SkPoint>(reader, vertexCount); 253 254 const SkPoint* texs = NULL; 255 if (flags & kDrawVertices_HasTexs_DrawOpFlag) { 256 texs = skip<SkPoint>(reader, vertexCount); 257 } 258 259 const SkColor* colors = NULL; 260 if (flags & kDrawVertices_HasColors_DrawOpFlag) { 261 colors = skip<SkColor>(reader, vertexCount); 262 } 263 264 // TODO: flatten/unflatten xfermodes 265 SkXfermode* xfer = NULL; 266 267 int indexCount = 0; 268 const uint16_t* indices = NULL; 269 if (flags & kDrawVertices_HasIndices_DrawOpFlag) { 270 indexCount = reader->readU32(); 271 indices = skipAlign<uint16_t>(reader, indexCount); 272 } 273 274 canvas->drawVertices(mode, vertexCount, verts, texs, colors, xfer, 275 indices, indexCount, state->paint()); 276} 277 278/////////////////////////////////////////////////////////////////////////////// 279 280static void drawText_rp(SkCanvas* canvas, SkReader32* reader, uint32_t op32, 281 SkGPipeState* state) { 282 size_t len = reader->readU32(); 283 const void* text = reader->skip(SkAlign4(len)); 284 const SkScalar* xy = skip<SkScalar>(reader, 2); 285 canvas->drawText(text, len, xy[0], xy[1], state->paint()); 286} 287 288static void drawPosText_rp(SkCanvas* canvas, SkReader32* reader, uint32_t op32, 289 SkGPipeState* state) { 290 size_t len = reader->readU32(); 291 const void* text = reader->skip(SkAlign4(len)); 292 size_t posCount = reader->readU32(); // compute by our writer 293 const SkPoint* pos = skip<SkPoint>(reader, posCount); 294 canvas->drawPosText(text, len, pos, state->paint()); 295} 296 297static void drawPosTextH_rp(SkCanvas* canvas, SkReader32* reader, uint32_t op32, 298 SkGPipeState* state) { 299 size_t len = reader->readU32(); 300 const void* text = reader->skip(SkAlign4(len)); 301 size_t posCount = reader->readU32(); // compute by our writer 302 const SkScalar* xpos = skip<SkScalar>(reader, posCount); 303 SkScalar constY = reader->readScalar(); 304 canvas->drawPosTextH(text, len, xpos, constY, state->paint()); 305} 306 307static void drawTextOnPath_rp(SkCanvas* canvas, SkReader32* reader, uint32_t op32, 308 SkGPipeState* state) { 309 size_t len = reader->readU32(); 310 const void* text = reader->skip(SkAlign4(len)); 311 312 SkPath path; 313 path.unflatten(*reader); 314 315 SkMatrix matrixStorage; 316 const SkMatrix* matrix = NULL; 317 if (DrawOp_unpackFlags(op32) & kDrawTextOnPath_HasMatrix_DrawOpFlag) { 318 SkReadMatrix(reader, &matrixStorage); 319 matrix = &matrixStorage; 320 } 321 322 canvas->drawTextOnPath(text, len, path, matrix, state->paint()); 323} 324 325/////////////////////////////////////////////////////////////////////////////// 326 327static void drawBitmap_rp(SkCanvas* canvas, SkReader32* reader, uint32_t op32, 328 SkGPipeState* state) { 329 UNIMPLEMENTED 330} 331 332static void drawBitmapMatrix_rp(SkCanvas* canvas, SkReader32* reader, uint32_t op32, 333 SkGPipeState* state) { 334 UNIMPLEMENTED 335} 336 337static void drawBitmapRect_rp(SkCanvas* canvas, SkReader32* reader, uint32_t op32, 338 SkGPipeState* state) { 339 UNIMPLEMENTED 340} 341 342static void drawSprite_rp(SkCanvas* canvas, SkReader32* reader, uint32_t op32, 343 SkGPipeState* state) { 344 UNIMPLEMENTED 345} 346 347/////////////////////////////////////////////////////////////////////////////// 348 349static void drawData_rp(SkCanvas* canvas, SkReader32* reader, uint32_t op32, 350 SkGPipeState* state) { 351 // since we don't have a paint, we can use data for our (small) sizes 352 size_t size = DrawOp_unpackData(op32); 353 if (0 == size) { 354 size = reader->readU32(); 355 } 356 const void* data = reader->skip(SkAlign4(size)); 357 canvas->drawData(data, size); 358} 359 360static void drawPicture_rp(SkCanvas* canvas, SkReader32* reader, uint32_t op32, 361 SkGPipeState* state) { 362 UNIMPLEMENTED 363} 364 365/////////////////////////////////////////////////////////////////////////////// 366 367static void paintOp_rp(SkCanvas*, SkReader32* reader, uint32_t op32, 368 SkGPipeState* state) { 369 size_t offset = reader->offset(); 370 size_t stop = offset + PaintOp_unpackData(op32); 371 SkPaint* p = state->editPaint(); 372 373 do { 374 uint32_t p32 = reader->readU32(); 375 unsigned op = PaintOp_unpackOp(p32); 376 unsigned data = PaintOp_unpackData(p32); 377 378// SkDebugf(" read %08X op=%d flags=%d data=%d\n", p32, op, done, data); 379 380 switch (op) { 381 case kReset_PaintOp: p->reset(); break; 382 case kFlags_PaintOp: p->setFlags(data); break; 383 case kColor_PaintOp: p->setColor(reader->readU32()); break; 384 case kStyle_PaintOp: p->setStyle((SkPaint::Style)data); break; 385 case kJoin_PaintOp: p->setStrokeJoin((SkPaint::Join)data); break; 386 case kCap_PaintOp: p->setStrokeCap((SkPaint::Cap)data); break; 387 case kWidth_PaintOp: p->setStrokeWidth(reader->readScalar()); break; 388 case kMiter_PaintOp: p->setStrokeMiter(reader->readScalar()); break; 389 case kEncoding_PaintOp: 390 p->setTextEncoding((SkPaint::TextEncoding)data); 391 break; 392 case kHinting_PaintOp: p->setHinting((SkPaint::Hinting)data); break; 393 case kAlign_PaintOp: p->setTextAlign((SkPaint::Align)data); break; 394 case kTextSize_PaintOp: p->setTextSize(reader->readScalar()); break; 395 case kTextScaleX_PaintOp: p->setTextScaleX(reader->readScalar()); break; 396 case kTextSkewX_PaintOp: p->setTextSkewX(reader->readScalar()); break; 397 398 case kFlatIndex_PaintOp: { 399 PaintFlats pf = (PaintFlats)PaintOp_unpackFlags(p32); 400 unsigned index = data; 401 set_paintflat(p, state->getFlat(index), pf); 402 break; 403 } 404 405 case kTypeface_PaintOp: state->setTypeface(p, data); break; 406 default: SkASSERT(!"bad paintop"); return; 407 } 408 SkASSERT(reader->offset() <= stop); 409 } while (reader->offset() < stop); 410} 411 412/////////////////////////////////////////////////////////////////////////////// 413 414static void def_Typeface_rp(SkCanvas*, SkReader32*, uint32_t, SkGPipeState* state) { 415 state->addTypeface(); 416} 417 418static void def_PaintFlat_rp(SkCanvas*, SkReader32*, uint32_t op32, 419 SkGPipeState* state) { 420 PaintFlats pf = (PaintFlats)DrawOp_unpackFlags(op32); 421 unsigned index = DrawOp_unpackData(op32); 422 state->defFlattenable(pf, index); 423} 424 425/////////////////////////////////////////////////////////////////////////////// 426 427static void skip_rp(SkCanvas*, SkReader32* reader, uint32_t op32, SkGPipeState*) { 428 size_t bytes = DrawOp_unpackData(op32); 429 (void)reader->skip(bytes); 430} 431 432static void done_rp(SkCanvas*, SkReader32*, uint32_t, SkGPipeState*) {} 433 434typedef void (*ReadProc)(SkCanvas*, SkReader32*, uint32_t op32, SkGPipeState*); 435 436static const ReadProc gReadTable[] = { 437 skip_rp, 438 clipPath_rp, 439 clipRegion_rp, 440 clipRect_rp, 441 concat_rp, 442 drawBitmap_rp, 443 drawBitmapMatrix_rp, 444 drawBitmapRect_rp, 445 drawClear_rp, 446 drawData_rp, 447 drawPaint_rp, 448 drawPath_rp, 449 drawPicture_rp, 450 drawPoints_rp, 451 drawPosText_rp, 452 drawPosTextH_rp, 453 drawRect_rp, 454 drawSprite_rp, 455 drawText_rp, 456 drawTextOnPath_rp, 457 drawVertices_rp, 458 restore_rp, 459 rotate_rp, 460 save_rp, 461 saveLayer_rp, 462 scale_rp, 463 setMatrix_rp, 464 skew_rp, 465 translate_rp, 466 467 paintOp_rp, 468 def_Typeface_rp, 469 def_PaintFlat_rp, 470 471 done_rp 472}; 473 474/////////////////////////////////////////////////////////////////////////////// 475 476SkGPipeState::SkGPipeState() {} 477 478SkGPipeState::~SkGPipeState() { 479 fTypefaces.safeUnrefAll(); 480 fFlatArray.safeUnrefAll(); 481} 482 483/////////////////////////////////////////////////////////////////////////////// 484 485#include "SkGPipe.h" 486 487SkGPipeReader::SkGPipeReader(SkCanvas* target) { 488 SkSafeRef(target); 489 fCanvas = target; 490 fState = NULL; 491} 492 493SkGPipeReader::~SkGPipeReader() { 494 SkSafeUnref(fCanvas); 495 delete fState; 496} 497 498SkGPipeReader::Status SkGPipeReader::playback(const void* data, size_t length, 499 size_t* bytesRead, bool readAtom) { 500 if (NULL == fCanvas) { 501 return kError_Status; 502 } 503 504 if (NULL == fState) { 505 fState = new SkGPipeState; 506 } 507 508 SkASSERT(SK_ARRAY_COUNT(gReadTable) == (kDone_DrawOp + 1)); 509 510 const ReadProc* table = gReadTable; 511 SkFlattenableReadBuffer reader(data, length); 512 SkCanvas* canvas = fCanvas; 513 Status status = kEOF_Status; 514 515 fState->setReader(&reader); 516 while (!reader.eof()) { 517 uint32_t op32 = reader.readU32(); 518 unsigned op = DrawOp_unpackOp(op32); 519 SkDEBUGCODE(DrawOps drawOp = (DrawOps)op;) 520 521 if (op >= SK_ARRAY_COUNT(gReadTable)) { 522 SkDebugf("---- bad op during GPipeState::playback\n"); 523 status = kError_Status; 524 break; 525 } 526 if (kDone_DrawOp == op) { 527 status = kDone_Status; 528 break; 529 } 530 table[op](canvas, &reader, op32, fState); 531 if (readAtom && 532 (table[op] != paintOp_rp && 533 table[op] != def_Typeface_rp && 534 table[op] != def_PaintFlat_rp 535 )) { 536 status = kReadAtom_Status; 537 break; 538 } 539 } 540 541 if (bytesRead) { 542 *bytesRead = reader.offset(); 543 } 544 return status; 545} 546 547 548