PictureTest.cpp revision d3e474e20c6f0f24ddb6b2643e92975d60190daa
1/* 2 * Copyright 2012 Google Inc. 3 * 4 * Use of this source code is governed by a BSD-style license that can be 5 * found in the LICENSE file. 6 */ 7 8#include "SkBlurImageFilter.h" 9#include "SkCanvas.h" 10#include "SkColorPriv.h" 11#include "SkDashPathEffect.h" 12#include "SkData.h" 13#include "SkDecodingImageGenerator.h" 14#include "SkError.h" 15#include "SkImageEncoder.h" 16#include "SkImageGenerator.h" 17#include "SkPaint.h" 18#include "SkPicture.h" 19#include "SkPictureRecorder.h" 20#include "SkPictureUtils.h" 21#include "SkRRect.h" 22#include "SkRandom.h" 23#include "SkShader.h" 24#include "SkStream.h" 25 26#if SK_SUPPORT_GPU 27#include "SkSurface.h" 28#include "GrContextFactory.h" 29#include "GrPictureUtils.h" 30#endif 31#include "Test.h" 32 33#include "SkLumaColorFilter.h" 34#include "SkColorFilterImageFilter.h" 35 36static const int gColorScale = 30; 37static const int gColorOffset = 60; 38 39static void make_bm(SkBitmap* bm, int w, int h, SkColor color, bool immutable) { 40 bm->allocN32Pixels(w, h); 41 bm->eraseColor(color); 42 if (immutable) { 43 bm->setImmutable(); 44 } 45} 46 47static void make_checkerboard(SkBitmap* bm, int w, int h, bool immutable) { 48 SkASSERT(w % 2 == 0); 49 SkASSERT(h % 2 == 0); 50 bm->allocPixels(SkImageInfo::Make(w, h, kAlpha_8_SkColorType, 51 kPremul_SkAlphaType)); 52 SkAutoLockPixels lock(*bm); 53 for (int y = 0; y < h; y += 2) { 54 uint8_t* s = bm->getAddr8(0, y); 55 for (int x = 0; x < w; x += 2) { 56 *s++ = 0xFF; 57 *s++ = 0x00; 58 } 59 s = bm->getAddr8(0, y + 1); 60 for (int x = 0; x < w; x += 2) { 61 *s++ = 0x00; 62 *s++ = 0xFF; 63 } 64 } 65 if (immutable) { 66 bm->setImmutable(); 67 } 68} 69 70static void init_paint(SkPaint* paint, const SkBitmap &bm) { 71 SkShader* shader = SkShader::CreateBitmapShader(bm, 72 SkShader::kClamp_TileMode, 73 SkShader::kClamp_TileMode); 74 paint->setShader(shader)->unref(); 75} 76 77typedef void (*DrawBitmapProc)(SkCanvas*, const SkBitmap&, 78 const SkBitmap&, const SkPoint&, 79 SkTDArray<SkPixelRef*>* usedPixRefs); 80 81static void drawpaint_proc(SkCanvas* canvas, const SkBitmap& bm, 82 const SkBitmap& altBM, const SkPoint& pos, 83 SkTDArray<SkPixelRef*>* usedPixRefs) { 84 SkPaint paint; 85 init_paint(&paint, bm); 86 87 canvas->drawPaint(paint); 88 *usedPixRefs->append() = bm.pixelRef(); 89} 90 91static void drawpoints_proc(SkCanvas* canvas, const SkBitmap& bm, 92 const SkBitmap& altBM, const SkPoint& pos, 93 SkTDArray<SkPixelRef*>* usedPixRefs) { 94 SkPaint paint; 95 init_paint(&paint, bm); 96 97 // draw a rect 98 SkPoint points[5] = { 99 { pos.fX, pos.fY }, 100 { pos.fX + bm.width() - 1, pos.fY }, 101 { pos.fX + bm.width() - 1, pos.fY + bm.height() - 1 }, 102 { pos.fX, pos.fY + bm.height() - 1 }, 103 { pos.fX, pos.fY }, 104 }; 105 106 canvas->drawPoints(SkCanvas::kPolygon_PointMode, 5, points, paint); 107 *usedPixRefs->append() = bm.pixelRef(); 108} 109 110static void drawrect_proc(SkCanvas* canvas, const SkBitmap& bm, 111 const SkBitmap& altBM, const SkPoint& pos, 112 SkTDArray<SkPixelRef*>* usedPixRefs) { 113 SkPaint paint; 114 init_paint(&paint, bm); 115 116 SkRect r = { 0, 0, SkIntToScalar(bm.width()), SkIntToScalar(bm.height()) }; 117 r.offset(pos.fX, pos.fY); 118 119 canvas->drawRect(r, paint); 120 *usedPixRefs->append() = bm.pixelRef(); 121} 122 123static void drawoval_proc(SkCanvas* canvas, const SkBitmap& bm, 124 const SkBitmap& altBM, const SkPoint& pos, 125 SkTDArray<SkPixelRef*>* usedPixRefs) { 126 SkPaint paint; 127 init_paint(&paint, bm); 128 129 SkRect r = { 0, 0, SkIntToScalar(bm.width()), SkIntToScalar(bm.height()) }; 130 r.offset(pos.fX, pos.fY); 131 132 canvas->drawOval(r, paint); 133 *usedPixRefs->append() = bm.pixelRef(); 134} 135 136static void drawrrect_proc(SkCanvas* canvas, const SkBitmap& bm, 137 const SkBitmap& altBM, const SkPoint& pos, 138 SkTDArray<SkPixelRef*>* usedPixRefs) { 139 SkPaint paint; 140 init_paint(&paint, bm); 141 142 SkRect r = { 0, 0, SkIntToScalar(bm.width()), SkIntToScalar(bm.height()) }; 143 r.offset(pos.fX, pos.fY); 144 145 SkRRect rr; 146 rr.setRectXY(r, SkIntToScalar(bm.width())/4, SkIntToScalar(bm.height())/4); 147 canvas->drawRRect(rr, paint); 148 *usedPixRefs->append() = bm.pixelRef(); 149} 150 151static void drawpath_proc(SkCanvas* canvas, const SkBitmap& bm, 152 const SkBitmap& altBM, const SkPoint& pos, 153 SkTDArray<SkPixelRef*>* usedPixRefs) { 154 SkPaint paint; 155 init_paint(&paint, bm); 156 157 SkPath path; 158 path.lineTo(bm.width()/2.0f, SkIntToScalar(bm.height())); 159 path.lineTo(SkIntToScalar(bm.width()), 0); 160 path.close(); 161 path.offset(pos.fX, pos.fY); 162 163 canvas->drawPath(path, paint); 164 *usedPixRefs->append() = bm.pixelRef(); 165} 166 167static void drawbitmap_proc(SkCanvas* canvas, const SkBitmap& bm, 168 const SkBitmap& altBM, const SkPoint& pos, 169 SkTDArray<SkPixelRef*>* usedPixRefs) { 170 canvas->drawBitmap(bm, pos.fX, pos.fY, NULL); 171 *usedPixRefs->append() = bm.pixelRef(); 172} 173 174static void drawbitmap_withshader_proc(SkCanvas* canvas, const SkBitmap& bm, 175 const SkBitmap& altBM, const SkPoint& pos, 176 SkTDArray<SkPixelRef*>* usedPixRefs) { 177 SkPaint paint; 178 init_paint(&paint, bm); 179 180 // The bitmap in the paint is ignored unless we're drawing an A8 bitmap 181 canvas->drawBitmap(altBM, pos.fX, pos.fY, &paint); 182 *usedPixRefs->append() = bm.pixelRef(); 183 *usedPixRefs->append() = altBM.pixelRef(); 184} 185 186static void drawsprite_proc(SkCanvas* canvas, const SkBitmap& bm, 187 const SkBitmap& altBM, const SkPoint& pos, 188 SkTDArray<SkPixelRef*>* usedPixRefs) { 189 const SkMatrix& ctm = canvas->getTotalMatrix(); 190 191 SkPoint p(pos); 192 ctm.mapPoints(&p, 1); 193 194 canvas->drawSprite(bm, (int)p.fX, (int)p.fY, NULL); 195 *usedPixRefs->append() = bm.pixelRef(); 196} 197 198#if 0 199// Although specifiable, this case doesn't seem to make sense (i.e., the 200// bitmap in the shader is never used). 201static void drawsprite_withshader_proc(SkCanvas* canvas, const SkBitmap& bm, 202 const SkBitmap& altBM, const SkPoint& pos, 203 SkTDArray<SkPixelRef*>* usedPixRefs) { 204 SkPaint paint; 205 init_paint(&paint, bm); 206 207 const SkMatrix& ctm = canvas->getTotalMatrix(); 208 209 SkPoint p(pos); 210 ctm.mapPoints(&p, 1); 211 212 canvas->drawSprite(altBM, (int)p.fX, (int)p.fY, &paint); 213 *usedPixRefs->append() = bm.pixelRef(); 214 *usedPixRefs->append() = altBM.pixelRef(); 215} 216#endif 217 218static void drawbitmaprect_proc(SkCanvas* canvas, const SkBitmap& bm, 219 const SkBitmap& altBM, const SkPoint& pos, 220 SkTDArray<SkPixelRef*>* usedPixRefs) { 221 SkRect r = { 0, 0, SkIntToScalar(bm.width()), SkIntToScalar(bm.height()) }; 222 223 r.offset(pos.fX, pos.fY); 224 canvas->drawBitmapRectToRect(bm, NULL, r, NULL); 225 *usedPixRefs->append() = bm.pixelRef(); 226} 227 228static void drawbitmaprect_withshader_proc(SkCanvas* canvas, 229 const SkBitmap& bm, 230 const SkBitmap& altBM, 231 const SkPoint& pos, 232 SkTDArray<SkPixelRef*>* usedPixRefs) { 233 SkPaint paint; 234 init_paint(&paint, bm); 235 236 SkRect r = { 0, 0, SkIntToScalar(bm.width()), SkIntToScalar(bm.height()) }; 237 r.offset(pos.fX, pos.fY); 238 239 // The bitmap in the paint is ignored unless we're drawing an A8 bitmap 240 canvas->drawBitmapRectToRect(altBM, NULL, r, &paint); 241 *usedPixRefs->append() = bm.pixelRef(); 242 *usedPixRefs->append() = altBM.pixelRef(); 243} 244 245static void drawtext_proc(SkCanvas* canvas, const SkBitmap& bm, 246 const SkBitmap& altBM, const SkPoint& pos, 247 SkTDArray<SkPixelRef*>* usedPixRefs) { 248 SkPaint paint; 249 init_paint(&paint, bm); 250 paint.setTextSize(SkIntToScalar(1.5*bm.width())); 251 252 canvas->drawText("0", 1, pos.fX, pos.fY+bm.width(), paint); 253 *usedPixRefs->append() = bm.pixelRef(); 254} 255 256static void drawpostext_proc(SkCanvas* canvas, const SkBitmap& bm, 257 const SkBitmap& altBM, const SkPoint& pos, 258 SkTDArray<SkPixelRef*>* usedPixRefs) { 259 SkPaint paint; 260 init_paint(&paint, bm); 261 paint.setTextSize(SkIntToScalar(1.5*bm.width())); 262 263 SkPoint point = { pos.fX, pos.fY + bm.height() }; 264 canvas->drawPosText("O", 1, &point, paint); 265 *usedPixRefs->append() = bm.pixelRef(); 266} 267 268static void drawtextonpath_proc(SkCanvas* canvas, const SkBitmap& bm, 269 const SkBitmap& altBM, const SkPoint& pos, 270 SkTDArray<SkPixelRef*>* usedPixRefs) { 271 SkPaint paint; 272 273 init_paint(&paint, bm); 274 paint.setTextSize(SkIntToScalar(1.5*bm.width())); 275 276 SkPath path; 277 path.lineTo(SkIntToScalar(bm.width()), 0); 278 path.offset(pos.fX, pos.fY+bm.height()); 279 280 canvas->drawTextOnPath("O", 1, path, NULL, paint); 281 *usedPixRefs->append() = bm.pixelRef(); 282} 283 284static void drawverts_proc(SkCanvas* canvas, const SkBitmap& bm, 285 const SkBitmap& altBM, const SkPoint& pos, 286 SkTDArray<SkPixelRef*>* usedPixRefs) { 287 SkPaint paint; 288 init_paint(&paint, bm); 289 290 SkPoint verts[4] = { 291 { pos.fX, pos.fY }, 292 { pos.fX + bm.width(), pos.fY }, 293 { pos.fX + bm.width(), pos.fY + bm.height() }, 294 { pos.fX, pos.fY + bm.height() } 295 }; 296 SkPoint texs[4] = { { 0, 0 }, 297 { SkIntToScalar(bm.width()), 0 }, 298 { SkIntToScalar(bm.width()), SkIntToScalar(bm.height()) }, 299 { 0, SkIntToScalar(bm.height()) } }; 300 uint16_t indices[6] = { 0, 1, 2, 0, 2, 3 }; 301 302 canvas->drawVertices(SkCanvas::kTriangles_VertexMode, 4, verts, texs, NULL, NULL, 303 indices, 6, paint); 304 *usedPixRefs->append() = bm.pixelRef(); 305} 306 307// Return a picture with the bitmaps drawn at the specified positions. 308static SkPicture* record_bitmaps(const SkBitmap bm[], 309 const SkPoint pos[], 310 SkTDArray<SkPixelRef*> analytic[], 311 int count, 312 DrawBitmapProc proc) { 313 SkPictureRecorder recorder; 314 SkCanvas* canvas = recorder.beginRecording(1000, 1000); 315 for (int i = 0; i < count; ++i) { 316 analytic[i].rewind(); 317 canvas->save(); 318 SkRect clipRect = SkRect::MakeXYWH(pos[i].fX, pos[i].fY, 319 SkIntToScalar(bm[i].width()), 320 SkIntToScalar(bm[i].height())); 321 canvas->clipRect(clipRect, SkRegion::kIntersect_Op); 322 proc(canvas, bm[i], bm[count+i], pos[i], &analytic[i]); 323 canvas->restore(); 324 } 325 return recorder.endRecording(); 326} 327 328static void rand_rect(SkRect* rect, SkRandom& rand, SkScalar W, SkScalar H) { 329 rect->fLeft = rand.nextRangeScalar(-W, 2*W); 330 rect->fTop = rand.nextRangeScalar(-H, 2*H); 331 rect->fRight = rect->fLeft + rand.nextRangeScalar(0, W); 332 rect->fBottom = rect->fTop + rand.nextRangeScalar(0, H); 333 334 // we integralize rect to make our tests more predictable, since Gather is 335 // a little sloppy. 336 SkIRect ir; 337 rect->round(&ir); 338 rect->set(ir); 339} 340 341static void draw(SkPicture* pic, int width, int height, SkBitmap* result) { 342 make_bm(result, width, height, SK_ColorBLACK, false); 343 344 SkCanvas canvas(*result); 345 canvas.drawPicture(pic); 346} 347 348template <typename T> int find_index(const T* array, T elem, int count) { 349 for (int i = 0; i < count; ++i) { 350 if (array[i] == elem) { 351 return i; 352 } 353 } 354 return -1; 355} 356 357// Return true if 'ref' is found in array[] 358static bool find(SkPixelRef const * const * array, SkPixelRef const * ref, int count) { 359 return find_index<const SkPixelRef*>(array, ref, count) >= 0; 360} 361 362// Look at each pixel that is inside 'subset', and if its color appears in 363// colors[], find the corresponding value in refs[] and append that ref into 364// array, skipping duplicates of the same value. 365// Note that gathering pixelRefs from rendered colors suffers from the problem 366// that multiple simultaneous textures (e.g., A8 for alpha and 8888 for color) 367// isn't easy to reconstruct. 368static void gather_from_image(const SkBitmap& bm, SkPixelRef* const refs[], 369 int count, SkTDArray<SkPixelRef*>* array, 370 const SkRect& subset) { 371 SkIRect ir; 372 subset.roundOut(&ir); 373 374 if (!ir.intersect(0, 0, bm.width()-1, bm.height()-1)) { 375 return; 376 } 377 378 // Since we only want to return unique values in array, when we scan we just 379 // set a bit for each index'd color found. In practice we only have a few 380 // distinct colors, so we just use an int's bits as our array. Hence the 381 // assert that count <= number-of-bits-in-our-int. 382 SkASSERT((unsigned)count <= 32); 383 uint32_t bitarray = 0; 384 385 SkAutoLockPixels alp(bm); 386 387 for (int y = ir.fTop; y < ir.fBottom; ++y) { 388 for (int x = ir.fLeft; x < ir.fRight; ++x) { 389 SkPMColor pmc = *bm.getAddr32(x, y); 390 // the only good case where the color is not found would be if 391 // the color is transparent, meaning no bitmap was drawn in that 392 // pixel. 393 if (pmc) { 394 uint32_t index = SkGetPackedR32(pmc); 395 SkASSERT(SkGetPackedG32(pmc) == index); 396 SkASSERT(SkGetPackedB32(pmc) == index); 397 if (0 == index) { 398 continue; // background color 399 } 400 SkASSERT(0 == (index - gColorOffset) % gColorScale); 401 index = (index - gColorOffset) / gColorScale; 402 SkASSERT(static_cast<int>(index) < count); 403 bitarray |= 1 << index; 404 } 405 } 406 } 407 408 for (int i = 0; i < count; ++i) { 409 if (bitarray & (1 << i)) { 410 *array->append() = refs[i]; 411 } 412 } 413} 414 415static void gather_from_analytic(const SkPoint pos[], SkScalar w, SkScalar h, 416 const SkTDArray<SkPixelRef*> analytic[], 417 int count, 418 SkTDArray<SkPixelRef*>* result, 419 const SkRect& subset) { 420 for (int i = 0; i < count; ++i) { 421 SkRect rect = SkRect::MakeXYWH(pos[i].fX, pos[i].fY, w, h); 422 423 if (SkRect::Intersects(subset, rect)) { 424 result->append(analytic[i].count(), analytic[i].begin()); 425 } 426 } 427} 428 429 430static const struct { 431 const DrawBitmapProc proc; 432 const char* const desc; 433} gProcs[] = { 434 {drawpaint_proc, "drawpaint"}, 435 {drawpoints_proc, "drawpoints"}, 436 {drawrect_proc, "drawrect"}, 437 {drawoval_proc, "drawoval"}, 438 {drawrrect_proc, "drawrrect"}, 439 {drawpath_proc, "drawpath"}, 440 {drawbitmap_proc, "drawbitmap"}, 441 {drawbitmap_withshader_proc, "drawbitmap_withshader"}, 442 {drawsprite_proc, "drawsprite"}, 443#if 0 444 {drawsprite_withshader_proc, "drawsprite_withshader"}, 445#endif 446 {drawbitmaprect_proc, "drawbitmaprect"}, 447 {drawbitmaprect_withshader_proc, "drawbitmaprect_withshader"}, 448 {drawtext_proc, "drawtext"}, 449 {drawpostext_proc, "drawpostext"}, 450 {drawtextonpath_proc, "drawtextonpath"}, 451 {drawverts_proc, "drawverts"}, 452}; 453 454static void create_textures(SkBitmap* bm, SkPixelRef** refs, int num, int w, int h) { 455 // Our convention is that the color components contain an encoding of 456 // the index of their corresponding bitmap/pixelref. (0,0,0,0) is 457 // reserved for the background 458 for (int i = 0; i < num; ++i) { 459 make_bm(&bm[i], w, h, 460 SkColorSetARGB(0xFF, 461 gColorScale*i+gColorOffset, 462 gColorScale*i+gColorOffset, 463 gColorScale*i+gColorOffset), 464 true); 465 refs[i] = bm[i].pixelRef(); 466 } 467 468 // The A8 alternate bitmaps are all BW checkerboards 469 for (int i = 0; i < num; ++i) { 470 make_checkerboard(&bm[num+i], w, h, true); 471 refs[num+i] = bm[num+i].pixelRef(); 472 } 473} 474 475static void test_gatherpixelrefs(skiatest::Reporter* reporter) { 476 const int IW = 32; 477 const int IH = IW; 478 const SkScalar W = SkIntToScalar(IW); 479 const SkScalar H = W; 480 481 static const int N = 4; 482 SkBitmap bm[2*N]; 483 SkPixelRef* refs[2*N]; 484 SkTDArray<SkPixelRef*> analytic[N]; 485 486 const SkPoint pos[N] = { 487 { 0, 0 }, { W, 0 }, { 0, H }, { W, H } 488 }; 489 490 create_textures(bm, refs, N, IW, IH); 491 492 SkRandom rand; 493 for (size_t k = 0; k < SK_ARRAY_COUNT(gProcs); ++k) { 494 SkAutoTUnref<SkPicture> pic( 495 record_bitmaps(bm, pos, analytic, N, gProcs[k].proc)); 496 497 REPORTER_ASSERT(reporter, pic->willPlayBackBitmaps() || N == 0); 498 // quick check for a small piece of each quadrant, which should just 499 // contain 1 or 2 bitmaps. 500 for (size_t i = 0; i < SK_ARRAY_COUNT(pos); ++i) { 501 SkRect r; 502 r.set(2, 2, W - 2, H - 2); 503 r.offset(pos[i].fX, pos[i].fY); 504 SkAutoDataUnref data(SkPictureUtils::GatherPixelRefs(pic, r)); 505 if (!data) { 506 ERRORF(reporter, "SkPictureUtils::GatherPixelRefs returned " 507 "NULL for %s.", gProcs[k].desc); 508 continue; 509 } 510 SkPixelRef** gatheredRefs = (SkPixelRef**)data->data(); 511 int count = static_cast<int>(data->size() / sizeof(SkPixelRef*)); 512 REPORTER_ASSERT(reporter, 1 == count || 2 == count); 513 if (1 == count) { 514 REPORTER_ASSERT(reporter, gatheredRefs[0] == refs[i]); 515 } else if (2 == count) { 516 REPORTER_ASSERT(reporter, 517 (gatheredRefs[0] == refs[i] && gatheredRefs[1] == refs[i+N]) || 518 (gatheredRefs[1] == refs[i] && gatheredRefs[0] == refs[i+N])); 519 } 520 } 521 522 SkBitmap image; 523 draw(pic, 2*IW, 2*IH, &image); 524 525 // Test a bunch of random (mostly) rects, and compare the gather results 526 // with a deduced list of refs by looking at the colors drawn. 527 for (int j = 0; j < 100; ++j) { 528 SkRect r; 529 rand_rect(&r, rand, 2*W, 2*H); 530 531 SkTDArray<SkPixelRef*> fromImage; 532 gather_from_image(image, refs, N, &fromImage, r); 533 534 SkTDArray<SkPixelRef*> fromAnalytic; 535 gather_from_analytic(pos, W, H, analytic, N, &fromAnalytic, r); 536 537 SkData* data = SkPictureUtils::GatherPixelRefs(pic, r); 538 size_t dataSize = data ? data->size() : 0; 539 int gatherCount = static_cast<int>(dataSize / sizeof(SkPixelRef*)); 540 SkASSERT(gatherCount * sizeof(SkPixelRef*) == dataSize); 541 SkPixelRef** gatherRefs = data ? (SkPixelRef**)(data->data()) : NULL; 542 SkAutoDataUnref adu(data); 543 544 // Everything that we saw drawn should appear in the analytic list 545 // but the analytic list may contain some pixelRefs that were not 546 // seen in the image (e.g., A8 textures used as masks) 547 for (int i = 0; i < fromImage.count(); ++i) { 548 if (-1 == fromAnalytic.find(fromImage[i])) { 549 ERRORF(reporter, "PixelRef missing %d %s", 550 i, gProcs[k].desc); 551 } 552 } 553 554 /* 555 * GatherPixelRefs is conservative, so it can return more bitmaps 556 * than are strictly required. Thus our check here is only that 557 * Gather didn't miss any that we actually needed. Even that isn't 558 * a strict requirement on Gather, which is meant to be quick and 559 * only mostly-correct, but at the moment this test should work. 560 */ 561 for (int i = 0; i < fromAnalytic.count(); ++i) { 562 bool found = find(gatherRefs, fromAnalytic[i], gatherCount); 563 if (!found) { 564 ERRORF(reporter, "PixelRef missing %d %s", 565 i, gProcs[k].desc); 566 } 567#if 0 568 // enable this block of code to debug failures, as it will rerun 569 // the case that failed. 570 if (!found) { 571 SkData* data = SkPictureUtils::GatherPixelRefs(pic, r); 572 size_t dataSize = data ? data->size() : 0; 573 } 574#endif 575 } 576 } 577 } 578} 579 580static void test_gatherpixelrefsandrects(skiatest::Reporter* reporter) { 581 const int IW = 32; 582 const int IH = IW; 583 const SkScalar W = SkIntToScalar(IW); 584 const SkScalar H = W; 585 586 static const int N = 4; 587 SkBitmap bm[2*N]; 588 SkPixelRef* refs[2*N]; 589 SkTDArray<SkPixelRef*> analytic[N]; 590 591 const SkPoint pos[N] = { 592 { 0, 0 }, { W, 0 }, { 0, H }, { W, H } 593 }; 594 595 create_textures(bm, refs, N, IW, IH); 596 597 SkRandom rand; 598 for (size_t k = 0; k < SK_ARRAY_COUNT(gProcs); ++k) { 599 SkAutoTUnref<SkPicture> pic( 600 record_bitmaps(bm, pos, analytic, N, gProcs[k].proc)); 601 602 REPORTER_ASSERT(reporter, pic->willPlayBackBitmaps() || N == 0); 603 604 SkAutoTUnref<SkPictureUtils::SkPixelRefContainer> prCont( 605 new SkPictureUtils::SkPixelRefsAndRectsList); 606 607 SkPictureUtils::GatherPixelRefsAndRects(pic, prCont); 608 609 // quick check for a small piece of each quadrant, which should just 610 // contain 1 or 2 bitmaps. 611 for (size_t i = 0; i < SK_ARRAY_COUNT(pos); ++i) { 612 SkRect r; 613 r.set(2, 2, W - 2, H - 2); 614 r.offset(pos[i].fX, pos[i].fY); 615 616 SkTDArray<SkPixelRef*> gatheredRefs; 617 prCont->query(r, &gatheredRefs); 618 619 int count = gatheredRefs.count(); 620 REPORTER_ASSERT(reporter, 1 == count || 2 == count); 621 if (1 == count) { 622 REPORTER_ASSERT(reporter, gatheredRefs[0] == refs[i]); 623 } else if (2 == count) { 624 REPORTER_ASSERT(reporter, 625 (gatheredRefs[0] == refs[i] && gatheredRefs[1] == refs[i+N]) || 626 (gatheredRefs[1] == refs[i] && gatheredRefs[0] == refs[i+N])); 627 } 628 } 629 630 SkBitmap image; 631 draw(pic, 2*IW, 2*IH, &image); 632 633 // Test a bunch of random (mostly) rects, and compare the gather results 634 // with the analytic results and the pixel refs seen in a rendering. 635 for (int j = 0; j < 100; ++j) { 636 SkRect r; 637 rand_rect(&r, rand, 2*W, 2*H); 638 639 SkTDArray<SkPixelRef*> fromImage; 640 gather_from_image(image, refs, N, &fromImage, r); 641 642 SkTDArray<SkPixelRef*> fromAnalytic; 643 gather_from_analytic(pos, W, H, analytic, N, &fromAnalytic, r); 644 645 SkTDArray<SkPixelRef*> gatheredRefs; 646 prCont->query(r, &gatheredRefs); 647 648 // Everything that we saw drawn should appear in the analytic list 649 // but the analytic list may contain some pixelRefs that were not 650 // seen in the image (e.g., A8 textures used as masks) 651 for (int i = 0; i < fromImage.count(); ++i) { 652 REPORTER_ASSERT(reporter, -1 != fromAnalytic.find(fromImage[i])); 653 } 654 655 // Everything in the analytic list should appear in the gathered 656 // list. 657 for (int i = 0; i < fromAnalytic.count(); ++i) { 658 REPORTER_ASSERT(reporter, -1 != gatheredRefs.find(fromAnalytic[i])); 659 } 660 } 661 } 662} 663 664#ifdef SK_DEBUG 665// Ensure that deleting SkPicturePlayback does not assert. Asserts only fire in debug mode, so only 666// run in debug mode. 667static void test_deleting_empty_playback() { 668 SkPictureRecorder recorder; 669 // Creates an SkPictureRecord 670 recorder.beginRecording(0, 0); 671 // Turns that into an SkPicturePlayback 672 SkAutoTUnref<SkPicture> picture(recorder.endRecording()); 673 // Deletes the old SkPicturePlayback, and creates a new SkPictureRecord 674 recorder.beginRecording(0, 0); 675} 676 677// Ensure that serializing an empty picture does not assert. Likewise only runs in debug mode. 678static void test_serializing_empty_picture() { 679 SkPictureRecorder recorder; 680 recorder.beginRecording(0, 0); 681 SkAutoTUnref<SkPicture> picture(recorder.endRecording()); 682 SkDynamicMemoryWStream stream; 683 picture->serialize(&stream); 684} 685#endif 686 687static void rand_op(SkCanvas* canvas, SkRandom& rand) { 688 SkPaint paint; 689 SkRect rect = SkRect::MakeWH(50, 50); 690 691 SkScalar unit = rand.nextUScalar1(); 692 if (unit <= 0.3) { 693// SkDebugf("save\n"); 694 canvas->save(); 695 } else if (unit <= 0.6) { 696// SkDebugf("restore\n"); 697 canvas->restore(); 698 } else if (unit <= 0.9) { 699// SkDebugf("clip\n"); 700 canvas->clipRect(rect); 701 } else { 702// SkDebugf("draw\n"); 703 canvas->drawPaint(paint); 704 } 705} 706 707#if SK_SUPPORT_GPU 708static void test_gpu_veto(skiatest::Reporter* reporter) { 709 710 SkPictureRecorder recorder; 711 712 SkCanvas* canvas = recorder.beginRecording(100, 100); 713 { 714 SkPath path; 715 path.moveTo(0, 0); 716 path.lineTo(50, 50); 717 718 SkScalar intervals[] = { 1.0f, 1.0f }; 719 SkAutoTUnref<SkDashPathEffect> dash(SkDashPathEffect::Create(intervals, 2, 0)); 720 721 SkPaint paint; 722 paint.setStyle(SkPaint::kStroke_Style); 723 paint.setPathEffect(dash); 724 725 canvas->drawPath(path, paint); 726 } 727 SkAutoTUnref<SkPicture> picture(recorder.endRecording()); 728 // path effects currently render an SkPicture undesireable for GPU rendering 729 730 const char *reason = NULL; 731 REPORTER_ASSERT(reporter, !picture->suitableForGpuRasterization(NULL, &reason)); 732 REPORTER_ASSERT(reporter, NULL != reason); 733 734 canvas = recorder.beginRecording(100, 100); 735 { 736 SkPath path; 737 738 path.moveTo(0, 0); 739 path.lineTo(0, 50); 740 path.lineTo(25, 25); 741 path.lineTo(50, 50); 742 path.lineTo(50, 0); 743 path.close(); 744 REPORTER_ASSERT(reporter, !path.isConvex()); 745 746 SkPaint paint; 747 paint.setAntiAlias(true); 748 for (int i = 0; i < 50; ++i) { 749 canvas->drawPath(path, paint); 750 } 751 } 752 picture.reset(recorder.endRecording()); 753 // A lot of AA concave paths currently render an SkPicture undesireable for GPU rendering 754 REPORTER_ASSERT(reporter, !picture->suitableForGpuRasterization(NULL)); 755 756 canvas = recorder.beginRecording(100, 100); 757 { 758 SkPath path; 759 760 path.moveTo(0, 0); 761 path.lineTo(0, 50); 762 path.lineTo(25, 25); 763 path.lineTo(50, 50); 764 path.lineTo(50, 0); 765 path.close(); 766 REPORTER_ASSERT(reporter, !path.isConvex()); 767 768 SkPaint paint; 769 paint.setAntiAlias(true); 770 paint.setStyle(SkPaint::kStroke_Style); 771 paint.setStrokeWidth(0); 772 for (int i = 0; i < 50; ++i) { 773 canvas->drawPath(path, paint); 774 } 775 } 776 picture.reset(recorder.endRecording()); 777 // hairline stroked AA concave paths are fine for GPU rendering 778 REPORTER_ASSERT(reporter, picture->suitableForGpuRasterization(NULL)); 779} 780 781static void test_gpu_picture_optimization(skiatest::Reporter* reporter, 782 GrContextFactory* factory) { 783 784 GrContext* context = factory->get(GrContextFactory::kNative_GLContextType); 785 786 static const int kWidth = 100; 787 static const int kHeight = 100; 788 789 SkAutoTUnref<SkPicture> pict; 790 791 // create a picture with the structure: 792 // 1) 793 // SaveLayer 794 // Restore 795 // 2) 796 // SaveLayer 797 // Translate 798 // SaveLayer w/ bound 799 // Restore 800 // Restore 801 // 3) 802 // SaveLayer w/ copyable paint 803 // Restore 804 // 4) 805 // SaveLayer w/ non-copyable paint 806 // Restore 807 { 808 SkPictureRecorder recorder; 809 810 SkCanvas* c = recorder.beginRecording(kWidth, kHeight); 811 // 1) 812 c->saveLayer(NULL, NULL); 813 c->restore(); 814 815 // 2) 816 c->saveLayer(NULL, NULL); 817 c->translate(kWidth/2, kHeight/2); 818 SkRect r = SkRect::MakeXYWH(0, 0, kWidth/2, kHeight/2); 819 c->saveLayer(&r, NULL); 820 c->restore(); 821 c->restore(); 822 823 // 3) 824 { 825 SkPaint p; 826 p.setColor(SK_ColorRED); 827 c->saveLayer(NULL, &p); 828 c->restore(); 829 } 830 // 4) 831 // TODO: this case will need to be removed once the paint's are immutable 832 { 833 SkPaint p; 834 SkAutoTUnref<SkColorFilter> cf(SkLumaColorFilter::Create()); 835 p.setImageFilter(SkColorFilterImageFilter::Create(cf.get()))->unref(); 836 c->saveLayer(NULL, &p); 837 c->restore(); 838 } 839 840 pict.reset(recorder.endRecording()); 841 } 842 843 // Now test out the SaveLayer extraction 844 { 845 SkImageInfo info = SkImageInfo::MakeN32Premul(kWidth, kHeight); 846 847 SkAutoTUnref<SkSurface> surface(SkSurface::NewScratchRenderTarget(context, info)); 848 849 SkCanvas* canvas = surface->getCanvas(); 850 851 canvas->EXPERIMENTAL_optimize(pict); 852 853 SkPicture::AccelData::Key key = GPUAccelData::ComputeAccelDataKey(); 854 855 const SkPicture::AccelData* data = pict->EXPERIMENTAL_getAccelData(key); 856 REPORTER_ASSERT(reporter, NULL != data); 857 858 const GPUAccelData *gpuData = static_cast<const GPUAccelData*>(data); 859 REPORTER_ASSERT(reporter, 5 == gpuData->numSaveLayers()); 860 861 const GPUAccelData::SaveLayerInfo& info0 = gpuData->saveLayerInfo(0); 862 // The parent/child layer appear in reverse order 863 const GPUAccelData::SaveLayerInfo& info1 = gpuData->saveLayerInfo(2); 864 const GPUAccelData::SaveLayerInfo& info2 = gpuData->saveLayerInfo(1); 865 const GPUAccelData::SaveLayerInfo& info3 = gpuData->saveLayerInfo(3); 866// const GPUAccelData::SaveLayerInfo& info4 = gpuData->saveLayerInfo(4); 867 868 REPORTER_ASSERT(reporter, info0.fValid); 869 REPORTER_ASSERT(reporter, kWidth == info0.fSize.fWidth && kHeight == info0.fSize.fHeight); 870 REPORTER_ASSERT(reporter, info0.fCTM.isIdentity()); 871 REPORTER_ASSERT(reporter, 0 == info0.fOffset.fX && 0 == info0.fOffset.fY); 872 REPORTER_ASSERT(reporter, NULL != info0.fPaint); 873 REPORTER_ASSERT(reporter, !info0.fIsNested && !info0.fHasNestedLayers); 874 875 REPORTER_ASSERT(reporter, info1.fValid); 876 REPORTER_ASSERT(reporter, kWidth == info1.fSize.fWidth && kHeight == info1.fSize.fHeight); 877 REPORTER_ASSERT(reporter, info1.fCTM.isIdentity()); 878 REPORTER_ASSERT(reporter, 0 == info1.fOffset.fX && 0 == info1.fOffset.fY); 879 REPORTER_ASSERT(reporter, NULL != info1.fPaint); 880 REPORTER_ASSERT(reporter, !info1.fIsNested && info1.fHasNestedLayers); // has a nested SL 881 882 REPORTER_ASSERT(reporter, info2.fValid); 883 REPORTER_ASSERT(reporter, kWidth/2 == info2.fSize.fWidth && 884 kHeight/2 == info2.fSize.fHeight); // bound reduces size 885 REPORTER_ASSERT(reporter, info2.fCTM.isIdentity()); // translated 886 REPORTER_ASSERT(reporter, kWidth/2 == info2.fOffset.fX && 887 kHeight/2 == info2.fOffset.fY); 888 REPORTER_ASSERT(reporter, NULL != info1.fPaint); 889 REPORTER_ASSERT(reporter, info2.fIsNested && !info2.fHasNestedLayers); // is nested 890 891 REPORTER_ASSERT(reporter, info3.fValid); 892 REPORTER_ASSERT(reporter, kWidth == info3.fSize.fWidth && kHeight == info3.fSize.fHeight); 893 REPORTER_ASSERT(reporter, info3.fCTM.isIdentity()); 894 REPORTER_ASSERT(reporter, 0 == info3.fOffset.fX && 0 == info3.fOffset.fY); 895 REPORTER_ASSERT(reporter, NULL != info3.fPaint); 896 REPORTER_ASSERT(reporter, !info3.fIsNested && !info3.fHasNestedLayers); 897 898#if 0 // needs more though for GrGatherCanvas 899 REPORTER_ASSERT(reporter, !info4.fValid); // paint is/was uncopyable 900 REPORTER_ASSERT(reporter, kWidth == info4.fSize.fWidth && kHeight == info4.fSize.fHeight); 901 REPORTER_ASSERT(reporter, 0 == info4.fOffset.fX && 0 == info4.fOffset.fY); 902 REPORTER_ASSERT(reporter, info4.fCTM.isIdentity()); 903 REPORTER_ASSERT(reporter, NULL == info4.fPaint); // paint is/was uncopyable 904 REPORTER_ASSERT(reporter, !info4.fIsNested && !info4.fHasNestedLayers); 905#endif 906 } 907} 908 909#endif 910 911static void set_canvas_to_save_count_4(SkCanvas* canvas) { 912 canvas->restoreToCount(1); 913 canvas->save(); 914 canvas->save(); 915 canvas->save(); 916} 917 918/** 919 * A canvas that records the number of saves, saveLayers and restores. 920 */ 921class SaveCountingCanvas : public SkCanvas { 922public: 923 SaveCountingCanvas(int width, int height) 924 : INHERITED(width, height) 925 , fSaveCount(0) 926 , fSaveLayerCount(0) 927 , fRestoreCount(0){ 928 } 929 930 virtual SaveLayerStrategy willSaveLayer(const SkRect* bounds, const SkPaint* paint, 931 SaveFlags flags) SK_OVERRIDE { 932 ++fSaveLayerCount; 933 return this->INHERITED::willSaveLayer(bounds, paint, flags); 934 } 935 936 virtual void willSave(SaveFlags flags) SK_OVERRIDE { 937 ++fSaveCount; 938 this->INHERITED::willSave(flags); 939 } 940 941 virtual void willRestore() SK_OVERRIDE { 942 ++fRestoreCount; 943 this->INHERITED::willRestore(); 944 } 945 946 unsigned int getSaveCount() const { return fSaveCount; } 947 unsigned int getSaveLayerCount() const { return fSaveLayerCount; } 948 unsigned int getRestoreCount() const { return fRestoreCount; } 949 950private: 951 unsigned int fSaveCount; 952 unsigned int fSaveLayerCount; 953 unsigned int fRestoreCount; 954 955 typedef SkCanvas INHERITED; 956}; 957 958void check_save_state(skiatest::Reporter* reporter, SkPicture* picture, 959 unsigned int numSaves, unsigned int numSaveLayers, 960 unsigned int numRestores) { 961 SaveCountingCanvas canvas(picture->width(), picture->height()); 962 963 picture->draw(&canvas); 964 965 REPORTER_ASSERT(reporter, numSaves == canvas.getSaveCount()); 966 REPORTER_ASSERT(reporter, numSaveLayers == canvas.getSaveLayerCount()); 967 REPORTER_ASSERT(reporter, numRestores == canvas.getRestoreCount()); 968} 969 970// This class exists so SkPicture can friend it and give it access to 971// the 'partialReplay' method. 972class SkPictureRecorderReplayTester { 973public: 974 static SkPicture* Copy(SkPictureRecorder* recorder) { 975 SkPictureRecorder recorder2; 976 977 SkCanvas* canvas = recorder2.beginRecording(10, 10); 978 979 recorder->partialReplay(canvas); 980 981 return recorder2.endRecording(); 982 } 983}; 984 985static void create_imbalance(SkCanvas* canvas) { 986 SkRect clipRect = SkRect::MakeWH(2, 2); 987 SkRect drawRect = SkRect::MakeWH(10, 10); 988 canvas->save(); 989 canvas->clipRect(clipRect, SkRegion::kReplace_Op); 990 canvas->translate(1.0f, 1.0f); 991 SkPaint p; 992 p.setColor(SK_ColorGREEN); 993 canvas->drawRect(drawRect, p); 994 // no restore 995} 996 997// This tests that replaying a potentially unbalanced picture into a canvas 998// doesn't affect the canvas' save count or matrix/clip state. 999static void check_balance(skiatest::Reporter* reporter, SkPicture* picture) { 1000 SkBitmap bm; 1001 bm.allocN32Pixels(4, 3); 1002 SkCanvas canvas(bm); 1003 1004 int beforeSaveCount = canvas.getSaveCount(); 1005 1006 SkMatrix beforeMatrix = canvas.getTotalMatrix(); 1007 1008 SkRect beforeClip; 1009 1010 canvas.getClipBounds(&beforeClip); 1011 1012 canvas.drawPicture(picture); 1013 1014 REPORTER_ASSERT(reporter, beforeSaveCount == canvas.getSaveCount()); 1015 REPORTER_ASSERT(reporter, beforeMatrix == canvas.getTotalMatrix()); 1016 1017 SkRect afterClip; 1018 1019 canvas.getClipBounds(&afterClip); 1020 1021 REPORTER_ASSERT(reporter, afterClip == beforeClip); 1022} 1023 1024// Test out SkPictureRecorder::partialReplay 1025DEF_TEST(PictureRecorder_replay, reporter) { 1026 // check save/saveLayer state 1027 { 1028 SkPictureRecorder recorder; 1029 1030 SkCanvas* canvas = recorder.beginRecording(10, 10); 1031 1032 canvas->saveLayer(NULL, NULL); 1033 1034 SkAutoTUnref<SkPicture> copy(SkPictureRecorderReplayTester::Copy(&recorder)); 1035 1036 // The extra save and restore comes from the Copy process. 1037 check_save_state(reporter, copy, 2, 1, 3); 1038 1039 canvas->saveLayer(NULL, NULL); 1040 1041 SkAutoTUnref<SkPicture> final(recorder.endRecording()); 1042 1043 check_save_state(reporter, final, 1, 2, 3); 1044 1045 // The copy shouldn't pick up any operations added after it was made 1046 check_save_state(reporter, copy, 2, 1, 3); 1047 } 1048 1049 // (partially) check leakage of draw ops 1050 { 1051 SkPictureRecorder recorder; 1052 1053 SkCanvas* canvas = recorder.beginRecording(10, 10); 1054 1055 SkRect r = SkRect::MakeWH(5, 5); 1056 SkPaint p; 1057 1058 canvas->drawRect(r, p); 1059 1060 SkAutoTUnref<SkPicture> copy(SkPictureRecorderReplayTester::Copy(&recorder)); 1061 1062 REPORTER_ASSERT(reporter, !copy->willPlayBackBitmaps()); 1063 1064 SkBitmap bm; 1065 make_bm(&bm, 10, 10, SK_ColorRED, true); 1066 1067 r.offset(5.0f, 5.0f); 1068 canvas->drawBitmapRectToRect(bm, NULL, r); 1069 1070 SkAutoTUnref<SkPicture> final(recorder.endRecording()); 1071 REPORTER_ASSERT(reporter, final->willPlayBackBitmaps()); 1072 1073 REPORTER_ASSERT(reporter, copy->uniqueID() != final->uniqueID()); 1074 1075 // The snapshot shouldn't pick up any operations added after it was made 1076 REPORTER_ASSERT(reporter, !copy->willPlayBackBitmaps()); 1077 } 1078 1079 // Recreate the Android partialReplay test case 1080 { 1081 SkPictureRecorder recorder; 1082 1083 SkCanvas* canvas = recorder.beginRecording(4, 3, NULL, 0); 1084 create_imbalance(canvas); 1085 1086 int expectedSaveCount = canvas->getSaveCount(); 1087 1088 SkAutoTUnref<SkPicture> copy(SkPictureRecorderReplayTester::Copy(&recorder)); 1089 check_balance(reporter, copy); 1090 1091 REPORTER_ASSERT(reporter, expectedSaveCount = canvas->getSaveCount()); 1092 1093 // End the recording of source to test the picture finalization 1094 // process isn't complicated by the partialReplay step 1095 SkAutoTUnref<SkPicture> final(recorder.endRecording()); 1096 } 1097} 1098 1099static void test_unbalanced_save_restores(skiatest::Reporter* reporter) { 1100 SkCanvas testCanvas(100, 100); 1101 set_canvas_to_save_count_4(&testCanvas); 1102 1103 REPORTER_ASSERT(reporter, 4 == testCanvas.getSaveCount()); 1104 1105 SkPaint paint; 1106 SkRect rect = SkRect::MakeLTRB(-10000000, -10000000, 10000000, 10000000); 1107 1108 SkPictureRecorder recorder; 1109 1110 { 1111 // Create picture with 2 unbalanced saves 1112 SkCanvas* canvas = recorder.beginRecording(100, 100); 1113 canvas->save(); 1114 canvas->translate(10, 10); 1115 canvas->drawRect(rect, paint); 1116 canvas->save(); 1117 canvas->translate(10, 10); 1118 canvas->drawRect(rect, paint); 1119 SkAutoTUnref<SkPicture> extraSavePicture(recorder.endRecording()); 1120 1121 testCanvas.drawPicture(extraSavePicture); 1122 REPORTER_ASSERT(reporter, 4 == testCanvas.getSaveCount()); 1123 } 1124 1125 set_canvas_to_save_count_4(&testCanvas); 1126 1127 { 1128 // Create picture with 2 unbalanced restores 1129 SkCanvas* canvas = recorder.beginRecording(100, 100); 1130 canvas->save(); 1131 canvas->translate(10, 10); 1132 canvas->drawRect(rect, paint); 1133 canvas->save(); 1134 canvas->translate(10, 10); 1135 canvas->drawRect(rect, paint); 1136 canvas->restore(); 1137 canvas->restore(); 1138 canvas->restore(); 1139 canvas->restore(); 1140 SkAutoTUnref<SkPicture> extraRestorePicture(recorder.endRecording()); 1141 1142 testCanvas.drawPicture(extraRestorePicture); 1143 REPORTER_ASSERT(reporter, 4 == testCanvas.getSaveCount()); 1144 } 1145 1146 set_canvas_to_save_count_4(&testCanvas); 1147 1148 { 1149 SkCanvas* canvas = recorder.beginRecording(100, 100); 1150 canvas->translate(10, 10); 1151 canvas->drawRect(rect, paint); 1152 SkAutoTUnref<SkPicture> noSavePicture(recorder.endRecording()); 1153 1154 testCanvas.drawPicture(noSavePicture); 1155 REPORTER_ASSERT(reporter, 4 == testCanvas.getSaveCount()); 1156 REPORTER_ASSERT(reporter, testCanvas.getTotalMatrix().isIdentity()); 1157 } 1158} 1159 1160static void test_peephole() { 1161 SkRandom rand; 1162 1163 SkPictureRecorder recorder; 1164 1165 for (int j = 0; j < 100; j++) { 1166 SkRandom rand2(rand); // remember the seed 1167 1168 SkCanvas* canvas = recorder.beginRecording(100, 100); 1169 1170 for (int i = 0; i < 1000; ++i) { 1171 rand_op(canvas, rand); 1172 } 1173 SkAutoTUnref<SkPicture> picture(recorder.endRecording()); 1174 1175 rand = rand2; 1176 } 1177 1178 { 1179 SkCanvas* canvas = recorder.beginRecording(100, 100); 1180 SkRect rect = SkRect::MakeWH(50, 50); 1181 1182 for (int i = 0; i < 100; ++i) { 1183 canvas->save(); 1184 } 1185 while (canvas->getSaveCount() > 1) { 1186 canvas->clipRect(rect); 1187 canvas->restore(); 1188 } 1189 SkAutoTUnref<SkPicture> picture(recorder.endRecording()); 1190 } 1191} 1192 1193#ifndef SK_DEBUG 1194// Only test this is in release mode. We deliberately crash in debug mode, since a valid caller 1195// should never do this. 1196static void test_bad_bitmap() { 1197 // This bitmap has a width and height but no pixels. As a result, attempting to record it will 1198 // fail. 1199 SkBitmap bm; 1200 bm.setInfo(SkImageInfo::MakeN32Premul(100, 100)); 1201 SkPictureRecorder recorder; 1202 SkCanvas* recordingCanvas = recorder.beginRecording(100, 100); 1203 recordingCanvas->drawBitmap(bm, 0, 0); 1204 SkAutoTUnref<SkPicture> picture(recorder.endRecording()); 1205 1206 SkCanvas canvas; 1207 canvas.drawPicture(picture); 1208} 1209#endif 1210 1211static SkData* encode_bitmap_to_data(size_t*, const SkBitmap& bm) { 1212 return SkImageEncoder::EncodeData(bm, SkImageEncoder::kPNG_Type, 100); 1213} 1214 1215static SkData* serialized_picture_from_bitmap(const SkBitmap& bitmap) { 1216 SkPictureRecorder recorder; 1217 SkCanvas* canvas = recorder.beginRecording(bitmap.width(), bitmap.height()); 1218 canvas->drawBitmap(bitmap, 0, 0); 1219 SkAutoTUnref<SkPicture> picture(recorder.endRecording()); 1220 1221 SkDynamicMemoryWStream wStream; 1222 picture->serialize(&wStream, &encode_bitmap_to_data); 1223 return wStream.copyToData(); 1224} 1225 1226struct ErrorContext { 1227 int fErrors; 1228 skiatest::Reporter* fReporter; 1229}; 1230 1231static void assert_one_parse_error_cb(SkError error, void* context) { 1232 ErrorContext* errorContext = static_cast<ErrorContext*>(context); 1233 errorContext->fErrors++; 1234 // This test only expects one error, and that is a kParseError. If there are others, 1235 // there is some unknown problem. 1236 REPORTER_ASSERT_MESSAGE(errorContext->fReporter, 1 == errorContext->fErrors, 1237 "This threw more errors than expected."); 1238 REPORTER_ASSERT_MESSAGE(errorContext->fReporter, kParseError_SkError == error, 1239 SkGetLastErrorString()); 1240} 1241 1242static void test_bitmap_with_encoded_data(skiatest::Reporter* reporter) { 1243 // Create a bitmap that will be encoded. 1244 SkBitmap original; 1245 make_bm(&original, 100, 100, SK_ColorBLUE, true); 1246 SkDynamicMemoryWStream wStream; 1247 if (!SkImageEncoder::EncodeStream(&wStream, original, SkImageEncoder::kPNG_Type, 100)) { 1248 return; 1249 } 1250 SkAutoDataUnref data(wStream.copyToData()); 1251 1252 SkBitmap bm; 1253 bool installSuccess = SkInstallDiscardablePixelRef( 1254 SkDecodingImageGenerator::Create(data, SkDecodingImageGenerator::Options()), &bm); 1255 REPORTER_ASSERT(reporter, installSuccess); 1256 1257 // Write both bitmaps to pictures, and ensure that the resulting data streams are the same. 1258 // Flattening original will follow the old path of performing an encode, while flattening bm 1259 // will use the already encoded data. 1260 SkAutoDataUnref picture1(serialized_picture_from_bitmap(original)); 1261 SkAutoDataUnref picture2(serialized_picture_from_bitmap(bm)); 1262 REPORTER_ASSERT(reporter, picture1->equals(picture2)); 1263 // Now test that a parse error was generated when trying to create a new SkPicture without 1264 // providing a function to decode the bitmap. 1265 ErrorContext context; 1266 context.fErrors = 0; 1267 context.fReporter = reporter; 1268 SkSetErrorCallback(assert_one_parse_error_cb, &context); 1269 SkMemoryStream pictureStream(picture1); 1270 SkClearLastError(); 1271 SkAutoUnref pictureFromStream(SkPicture::CreateFromStream(&pictureStream, NULL)); 1272 REPORTER_ASSERT(reporter, pictureFromStream.get() != NULL); 1273 SkClearLastError(); 1274 SkSetErrorCallback(NULL, NULL); 1275} 1276 1277static void test_draw_empty(skiatest::Reporter* reporter) { 1278 SkBitmap result; 1279 make_bm(&result, 2, 2, SK_ColorBLACK, false); 1280 1281 SkCanvas canvas(result); 1282 1283 { 1284 // stock SkPicture 1285 SkPictureRecorder recorder; 1286 recorder.beginRecording(1, 1); 1287 SkAutoTUnref<SkPicture> picture(recorder.endRecording()); 1288 1289 canvas.drawPicture(picture); 1290 } 1291 1292 { 1293 // tile grid 1294 SkTileGridFactory::TileGridInfo gridInfo; 1295 gridInfo.fMargin.setEmpty(); 1296 gridInfo.fOffset.setZero(); 1297 gridInfo.fTileInterval.set(1, 1); 1298 1299 SkTileGridFactory factory(gridInfo); 1300 SkPictureRecorder recorder; 1301 recorder.beginRecording(1, 1, &factory); 1302 SkAutoTUnref<SkPicture> picture(recorder.endRecording()); 1303 1304 canvas.drawPicture(picture); 1305 } 1306 1307 { 1308 // RTree 1309 SkRTreeFactory factory; 1310 SkPictureRecorder recorder; 1311 recorder.beginRecording(1, 1, &factory); 1312 SkAutoTUnref<SkPicture> picture(recorder.endRecording()); 1313 1314 canvas.drawPicture(picture); 1315 } 1316 1317 { 1318 // quad tree 1319 SkQuadTreeFactory factory; 1320 SkPictureRecorder recorder; 1321 recorder.beginRecording(1, 1, &factory); 1322 SkAutoTUnref<SkPicture> picture(recorder.endRecording()); 1323 1324 canvas.drawPicture(picture); 1325 } 1326} 1327 1328static void test_clip_bound_opt(skiatest::Reporter* reporter) { 1329 // Test for crbug.com/229011 1330 SkRect rect1 = SkRect::MakeXYWH(SkIntToScalar(4), SkIntToScalar(4), 1331 SkIntToScalar(2), SkIntToScalar(2)); 1332 SkRect rect2 = SkRect::MakeXYWH(SkIntToScalar(7), SkIntToScalar(7), 1333 SkIntToScalar(1), SkIntToScalar(1)); 1334 SkRect rect3 = SkRect::MakeXYWH(SkIntToScalar(6), SkIntToScalar(6), 1335 SkIntToScalar(1), SkIntToScalar(1)); 1336 1337 SkPath invPath; 1338 invPath.addOval(rect1); 1339 invPath.setFillType(SkPath::kInverseEvenOdd_FillType); 1340 SkPath path; 1341 path.addOval(rect2); 1342 SkPath path2; 1343 path2.addOval(rect3); 1344 SkIRect clipBounds; 1345 SkPictureRecorder recorder; 1346 // Minimalist test set for 100% code coverage of 1347 // SkPictureRecord::updateClipConservativelyUsingBounds 1348 { 1349 SkCanvas* canvas = recorder.beginRecording(10, 10); 1350 canvas->clipPath(invPath, SkRegion::kIntersect_Op); 1351 bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds); 1352 REPORTER_ASSERT(reporter, true == nonEmpty); 1353 REPORTER_ASSERT(reporter, 0 == clipBounds.fLeft); 1354 REPORTER_ASSERT(reporter, 0 == clipBounds.fTop); 1355 REPORTER_ASSERT(reporter, 10 == clipBounds.fBottom); 1356 REPORTER_ASSERT(reporter, 10 == clipBounds.fRight); 1357 } 1358 { 1359 SkCanvas* canvas = recorder.beginRecording(10, 10); 1360 canvas->clipPath(path, SkRegion::kIntersect_Op); 1361 canvas->clipPath(invPath, SkRegion::kIntersect_Op); 1362 bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds); 1363 REPORTER_ASSERT(reporter, true == nonEmpty); 1364 REPORTER_ASSERT(reporter, 7 == clipBounds.fLeft); 1365 REPORTER_ASSERT(reporter, 7 == clipBounds.fTop); 1366 REPORTER_ASSERT(reporter, 8 == clipBounds.fBottom); 1367 REPORTER_ASSERT(reporter, 8 == clipBounds.fRight); 1368 } 1369 { 1370 SkCanvas* canvas = recorder.beginRecording(10, 10); 1371 canvas->clipPath(path, SkRegion::kIntersect_Op); 1372 canvas->clipPath(invPath, SkRegion::kUnion_Op); 1373 bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds); 1374 REPORTER_ASSERT(reporter, true == nonEmpty); 1375 REPORTER_ASSERT(reporter, 0 == clipBounds.fLeft); 1376 REPORTER_ASSERT(reporter, 0 == clipBounds.fTop); 1377 REPORTER_ASSERT(reporter, 10 == clipBounds.fBottom); 1378 REPORTER_ASSERT(reporter, 10 == clipBounds.fRight); 1379 } 1380 { 1381 SkCanvas* canvas = recorder.beginRecording(10, 10); 1382 canvas->clipPath(path, SkRegion::kDifference_Op); 1383 bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds); 1384 REPORTER_ASSERT(reporter, true == nonEmpty); 1385 REPORTER_ASSERT(reporter, 0 == clipBounds.fLeft); 1386 REPORTER_ASSERT(reporter, 0 == clipBounds.fTop); 1387 REPORTER_ASSERT(reporter, 10 == clipBounds.fBottom); 1388 REPORTER_ASSERT(reporter, 10 == clipBounds.fRight); 1389 } 1390 { 1391 SkCanvas* canvas = recorder.beginRecording(10, 10); 1392 canvas->clipPath(path, SkRegion::kReverseDifference_Op); 1393 bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds); 1394 // True clip is actually empty in this case, but the best 1395 // determination we can make using only bounds as input is that the 1396 // clip is included in the bounds of 'path'. 1397 REPORTER_ASSERT(reporter, true == nonEmpty); 1398 REPORTER_ASSERT(reporter, 7 == clipBounds.fLeft); 1399 REPORTER_ASSERT(reporter, 7 == clipBounds.fTop); 1400 REPORTER_ASSERT(reporter, 8 == clipBounds.fBottom); 1401 REPORTER_ASSERT(reporter, 8 == clipBounds.fRight); 1402 } 1403 { 1404 SkCanvas* canvas = recorder.beginRecording(10, 10); 1405 canvas->clipPath(path, SkRegion::kIntersect_Op); 1406 canvas->clipPath(path2, SkRegion::kXOR_Op); 1407 bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds); 1408 REPORTER_ASSERT(reporter, true == nonEmpty); 1409 REPORTER_ASSERT(reporter, 6 == clipBounds.fLeft); 1410 REPORTER_ASSERT(reporter, 6 == clipBounds.fTop); 1411 REPORTER_ASSERT(reporter, 8 == clipBounds.fBottom); 1412 REPORTER_ASSERT(reporter, 8 == clipBounds.fRight); 1413 } 1414} 1415 1416/** 1417 * A canvas that records the number of clip commands. 1418 */ 1419class ClipCountingCanvas : public SkCanvas { 1420public: 1421 ClipCountingCanvas(int width, int height) 1422 : INHERITED(width, height) 1423 , fClipCount(0){ 1424 } 1425 1426 virtual void onClipRect(const SkRect& r, 1427 SkRegion::Op op, 1428 ClipEdgeStyle edgeStyle) SK_OVERRIDE { 1429 fClipCount += 1; 1430 this->INHERITED::onClipRect(r, op, edgeStyle); 1431 } 1432 1433 virtual void onClipRRect(const SkRRect& rrect, 1434 SkRegion::Op op, 1435 ClipEdgeStyle edgeStyle)SK_OVERRIDE { 1436 fClipCount += 1; 1437 this->INHERITED::onClipRRect(rrect, op, edgeStyle); 1438 } 1439 1440 virtual void onClipPath(const SkPath& path, 1441 SkRegion::Op op, 1442 ClipEdgeStyle edgeStyle) SK_OVERRIDE { 1443 fClipCount += 1; 1444 this->INHERITED::onClipPath(path, op, edgeStyle); 1445 } 1446 1447 virtual void onClipRegion(const SkRegion& deviceRgn, SkRegion::Op op) SK_OVERRIDE { 1448 fClipCount += 1; 1449 this->INHERITED::onClipRegion(deviceRgn, op); 1450 } 1451 1452 unsigned getClipCount() const { return fClipCount; } 1453 1454private: 1455 unsigned fClipCount; 1456 1457 typedef SkCanvas INHERITED; 1458}; 1459 1460static void test_clip_expansion(skiatest::Reporter* reporter) { 1461 SkPictureRecorder recorder; 1462 SkCanvas* canvas = recorder.beginRecording(10, 10); 1463 1464 canvas->clipRect(SkRect::MakeEmpty(), SkRegion::kReplace_Op); 1465 // The following expanding clip should not be skipped. 1466 canvas->clipRect(SkRect::MakeXYWH(4, 4, 3, 3), SkRegion::kUnion_Op); 1467 // Draw something so the optimizer doesn't just fold the world. 1468 SkPaint p; 1469 p.setColor(SK_ColorBLUE); 1470 canvas->drawPaint(p); 1471 SkAutoTUnref<SkPicture> picture(recorder.endRecording()); 1472 1473 ClipCountingCanvas testCanvas(10, 10); 1474 picture->draw(&testCanvas); 1475 1476 // Both clips should be present on playback. 1477 REPORTER_ASSERT(reporter, testCanvas.getClipCount() == 2); 1478} 1479 1480static void test_hierarchical(skiatest::Reporter* reporter) { 1481 SkBitmap bm; 1482 make_bm(&bm, 10, 10, SK_ColorRED, true); 1483 1484 SkPictureRecorder recorder; 1485 1486 recorder.beginRecording(10, 10); 1487 SkAutoTUnref<SkPicture> childPlain(recorder.endRecording()); 1488 REPORTER_ASSERT(reporter, !childPlain->willPlayBackBitmaps()); // 0 1489 1490 recorder.beginRecording(10, 10)->drawBitmap(bm, 0, 0); 1491 SkAutoTUnref<SkPicture> childWithBitmap(recorder.endRecording()); 1492 REPORTER_ASSERT(reporter, childWithBitmap->willPlayBackBitmaps()); // 1 1493 1494 { 1495 SkCanvas* canvas = recorder.beginRecording(10, 10); 1496 canvas->drawPicture(childPlain); 1497 SkAutoTUnref<SkPicture> parentPP(recorder.endRecording()); 1498 REPORTER_ASSERT(reporter, !parentPP->willPlayBackBitmaps()); // 0 1499 } 1500 { 1501 SkCanvas* canvas = recorder.beginRecording(10, 10); 1502 canvas->drawPicture(childWithBitmap); 1503 SkAutoTUnref<SkPicture> parentPWB(recorder.endRecording()); 1504 REPORTER_ASSERT(reporter, parentPWB->willPlayBackBitmaps()); // 1 1505 } 1506 { 1507 SkCanvas* canvas = recorder.beginRecording(10, 10); 1508 canvas->drawBitmap(bm, 0, 0); 1509 canvas->drawPicture(childPlain); 1510 SkAutoTUnref<SkPicture> parentWBP(recorder.endRecording()); 1511 REPORTER_ASSERT(reporter, parentWBP->willPlayBackBitmaps()); // 1 1512 } 1513 { 1514 SkCanvas* canvas = recorder.beginRecording(10, 10); 1515 canvas->drawBitmap(bm, 0, 0); 1516 canvas->drawPicture(childWithBitmap); 1517 SkAutoTUnref<SkPicture> parentWBWB(recorder.endRecording()); 1518 REPORTER_ASSERT(reporter, parentWBWB->willPlayBackBitmaps()); // 2 1519 } 1520} 1521 1522static void test_gen_id(skiatest::Reporter* reporter) { 1523 1524 SkPicture empty; 1525 1526 // Empty pictures should still have a valid ID 1527 REPORTER_ASSERT(reporter, empty.uniqueID() != SK_InvalidGenID); 1528 1529 SkPictureRecorder recorder; 1530 1531 SkCanvas* canvas = recorder.beginRecording(1, 1); 1532 canvas->drawARGB(255, 255, 255, 255); 1533 SkAutoTUnref<SkPicture> hasData(recorder.endRecording()); 1534 // picture should have a non-zero id after recording 1535 REPORTER_ASSERT(reporter, hasData->uniqueID() != SK_InvalidGenID); 1536 1537 // both pictures should have different ids 1538 REPORTER_ASSERT(reporter, hasData->uniqueID() != empty.uniqueID()); 1539 1540 // test out copy constructor 1541 SkPicture copyWithData(*hasData); 1542 REPORTER_ASSERT(reporter, hasData->uniqueID() == copyWithData.uniqueID()); 1543 1544 SkPicture emptyCopy(empty); 1545 REPORTER_ASSERT(reporter, empty.uniqueID() != emptyCopy.uniqueID()); 1546} 1547 1548DEF_TEST(Picture, reporter) { 1549#ifdef SK_DEBUG 1550 test_deleting_empty_playback(); 1551 test_serializing_empty_picture(); 1552#else 1553 test_bad_bitmap(); 1554#endif 1555 test_unbalanced_save_restores(reporter); 1556 test_peephole(); 1557#if SK_SUPPORT_GPU 1558 test_gpu_veto(reporter); 1559#endif 1560 test_gatherpixelrefs(reporter); 1561 test_gatherpixelrefsandrects(reporter); 1562 test_bitmap_with_encoded_data(reporter); 1563 test_draw_empty(reporter); 1564 test_clip_bound_opt(reporter); 1565 test_clip_expansion(reporter); 1566 test_hierarchical(reporter); 1567 test_gen_id(reporter); 1568} 1569 1570#if SK_SUPPORT_GPU 1571DEF_GPUTEST(GPUPicture, reporter, factory) { 1572 test_gpu_picture_optimization(reporter, factory); 1573} 1574#endif 1575 1576static void draw_bitmaps(const SkBitmap bitmap, SkCanvas* canvas) { 1577 const SkPaint paint; 1578 const SkRect rect = { 5.0f, 5.0f, 8.0f, 8.0f }; 1579 const SkIRect irect = { 2, 2, 3, 3 }; 1580 1581 // Don't care what these record, as long as they're legal. 1582 canvas->drawBitmap(bitmap, 0.0f, 0.0f, &paint); 1583 canvas->drawBitmapRectToRect(bitmap, &rect, rect, &paint, SkCanvas::kNone_DrawBitmapRectFlag); 1584 canvas->drawBitmapMatrix(bitmap, SkMatrix::I(), &paint); 1585 canvas->drawBitmapNine(bitmap, irect, rect, &paint); 1586 canvas->drawSprite(bitmap, 1, 1); 1587} 1588 1589static void test_draw_bitmaps(SkCanvas* canvas) { 1590 SkBitmap empty; 1591 draw_bitmaps(empty, canvas); 1592 empty.setInfo(SkImageInfo::MakeN32Premul(10, 10)); 1593 draw_bitmaps(empty, canvas); 1594} 1595 1596DEF_TEST(Picture_EmptyBitmap, r) { 1597 SkPictureRecorder recorder; 1598 test_draw_bitmaps(recorder.beginRecording(10, 10)); 1599 SkAutoTUnref<SkPicture> picture(recorder.endRecording()); 1600} 1601 1602DEF_TEST(Canvas_EmptyBitmap, r) { 1603 SkBitmap dst; 1604 dst.allocN32Pixels(10, 10); 1605 SkCanvas canvas(dst); 1606 1607 test_draw_bitmaps(&canvas); 1608} 1609