OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2012 Google Inc. | 2 * Copyright 2012 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkBitmapDevice.h" | 8 #include "SkBitmapDevice.h" |
9 #include "SkCanvas.h" | 9 #include "SkCanvas.h" |
10 #include "SkColorPriv.h" | 10 #include "SkColorPriv.h" |
(...skipping 398 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
409 const SkRect& subset) { | 409 const SkRect& subset) { |
410 for (int i = 0; i < count; ++i) { | 410 for (int i = 0; i < count; ++i) { |
411 SkRect rect = SkRect::MakeXYWH(pos[i].fX, pos[i].fY, w, h); | 411 SkRect rect = SkRect::MakeXYWH(pos[i].fX, pos[i].fY, w, h); |
412 | 412 |
413 if (SkRect::Intersects(subset, rect)) { | 413 if (SkRect::Intersects(subset, rect)) { |
414 result->append(analytic[i].count(), analytic[i].begin()); | 414 result->append(analytic[i].count(), analytic[i].begin()); |
415 } | 415 } |
416 } | 416 } |
417 } | 417 } |
418 | 418 |
419 static const DrawBitmapProc gProcs[] = { | 419 |
420 drawpaint_proc, | 420 static const struct { |
421 drawpoints_proc, | 421 const DrawBitmapProc proc; |
422 drawrect_proc, | 422 const char* const desc; |
423 drawoval_proc, | 423 } gProcs[] = { |
424 drawrrect_proc, | 424 {drawpaint_proc, "drawpaint"}, |
425 drawpath_proc, | 425 {drawpoints_proc, "drawpoints"}, |
426 drawbitmap_proc, | 426 {drawrect_proc, "drawrect"}, |
427 drawbitmap_withshader_proc, | 427 {drawoval_proc, "drawoval"}, |
428 drawsprite_proc, | 428 {drawrrect_proc, "drawrrect"}, |
| 429 {drawpath_proc, "drawpath"}, |
| 430 {drawbitmap_proc, "drawbitmap"}, |
| 431 {drawbitmap_withshader_proc, "drawbitmap_withshader"}, |
| 432 {drawsprite_proc, "drawsprite"}, |
429 #if 0 | 433 #if 0 |
430 drawsprite_withshader_proc, | 434 {drawsprite_withshader_proc, "drawsprite_withshader"}, |
431 #endif | 435 #endif |
432 drawbitmaprect_proc, | 436 {drawbitmaprect_proc, "drawbitmaprect"}, |
433 drawbitmaprect_withshader_proc, | 437 {drawbitmaprect_withshader_proc, "drawbitmaprect_withshader"}, |
434 drawtext_proc, | 438 {drawtext_proc, "drawtext"}, |
435 drawpostext_proc, | 439 {drawpostext_proc, "drawpostext"}, |
436 drawtextonpath_proc, | 440 {drawtextonpath_proc, "drawtextonpath"}, |
437 drawverts_proc, | 441 {drawverts_proc, "drawverts"}, |
438 }; | 442 }; |
439 | 443 |
440 static void create_textures(SkBitmap* bm, SkPixelRef** refs, int num, int w, int
h) { | 444 static void create_textures(SkBitmap* bm, SkPixelRef** refs, int num, int w, int
h) { |
441 // Our convention is that the color components contain an encoding of | 445 // Our convention is that the color components contain an encoding of |
442 // the index of their corresponding bitmap/pixelref. (0,0,0,0) is | 446 // the index of their corresponding bitmap/pixelref. (0,0,0,0) is |
443 // reserved for the background | 447 // reserved for the background |
444 for (int i = 0; i < num; ++i) { | 448 for (int i = 0; i < num; ++i) { |
445 make_bm(&bm[i], w, h, | 449 make_bm(&bm[i], w, h, |
446 SkColorSetARGB(0xFF, | 450 SkColorSetARGB(0xFF, |
447 gColorScale*i+gColorOffset, | 451 gColorScale*i+gColorOffset, |
(...skipping 22 matching lines...) Expand all Loading... |
470 SkTDArray<SkPixelRef*> analytic[N]; | 474 SkTDArray<SkPixelRef*> analytic[N]; |
471 | 475 |
472 const SkPoint pos[N] = { | 476 const SkPoint pos[N] = { |
473 { 0, 0 }, { W, 0 }, { 0, H }, { W, H } | 477 { 0, 0 }, { W, 0 }, { 0, H }, { W, H } |
474 }; | 478 }; |
475 | 479 |
476 create_textures(bm, refs, N, IW, IH); | 480 create_textures(bm, refs, N, IW, IH); |
477 | 481 |
478 SkRandom rand; | 482 SkRandom rand; |
479 for (size_t k = 0; k < SK_ARRAY_COUNT(gProcs); ++k) { | 483 for (size_t k = 0; k < SK_ARRAY_COUNT(gProcs); ++k) { |
480 SkAutoTUnref<SkPicture> pic(record_bitmaps(bm, pos, analytic, N, gProcs[
k])); | 484 SkAutoTUnref<SkPicture> pic( |
| 485 record_bitmaps(bm, pos, analytic, N, gProcs[k].proc)); |
481 | 486 |
482 REPORTER_ASSERT(reporter, pic->willPlayBackBitmaps() || N == 0); | 487 REPORTER_ASSERT(reporter, pic->willPlayBackBitmaps() || N == 0); |
483 // quick check for a small piece of each quadrant, which should just | 488 // quick check for a small piece of each quadrant, which should just |
484 // contain 1 or 2 bitmaps. | 489 // contain 1 or 2 bitmaps. |
485 for (size_t i = 0; i < SK_ARRAY_COUNT(pos); ++i) { | 490 for (size_t i = 0; i < SK_ARRAY_COUNT(pos); ++i) { |
486 SkRect r; | 491 SkRect r; |
487 r.set(2, 2, W - 2, H - 2); | 492 r.set(2, 2, W - 2, H - 2); |
488 r.offset(pos[i].fX, pos[i].fY); | 493 r.offset(pos[i].fX, pos[i].fY); |
489 SkAutoDataUnref data(SkPictureUtils::GatherPixelRefs(pic, r)); | 494 SkAutoDataUnref data(SkPictureUtils::GatherPixelRefs(pic, r)); |
490 REPORTER_ASSERT(reporter, data); | 495 if (!data) { |
491 if (data) { | 496 ERRORF(reporter, "SkPictureUtils::GatherPixelRefs returned " |
492 SkPixelRef** gatheredRefs = (SkPixelRef**)data->data(); | 497 "NULL for %s.", gProcs[k].desc); |
493 int count = static_cast<int>(data->size() / sizeof(SkPixelRef*))
; | 498 continue; |
494 REPORTER_ASSERT(reporter, 1 == count || 2 == count); | 499 } |
495 if (1 == count) { | 500 SkPixelRef** gatheredRefs = (SkPixelRef**)data->data(); |
496 REPORTER_ASSERT(reporter, gatheredRefs[0] == refs[i]); | 501 int count = static_cast<int>(data->size() / sizeof(SkPixelRef*)); |
497 } else if (2 == count) { | 502 REPORTER_ASSERT(reporter, 1 == count || 2 == count); |
498 REPORTER_ASSERT(reporter, | 503 if (1 == count) { |
499 (gatheredRefs[0] == refs[i] && gatheredRefs[1] == refs[i
+N]) || | 504 REPORTER_ASSERT(reporter, gatheredRefs[0] == refs[i]); |
500 (gatheredRefs[1] == refs[i] && gatheredRefs[0] == refs[i
+N])); | 505 } else if (2 == count) { |
501 } | 506 REPORTER_ASSERT(reporter, |
| 507 (gatheredRefs[0] == refs[i] && gatheredRefs[1] == refs[i+N])
|| |
| 508 (gatheredRefs[1] == refs[i] && gatheredRefs[0] == refs[i+N])
); |
502 } | 509 } |
503 } | 510 } |
504 | 511 |
505 SkBitmap image; | 512 SkBitmap image; |
506 draw(pic, 2*IW, 2*IH, &image); | 513 draw(pic, 2*IW, 2*IH, &image); |
507 | 514 |
508 // Test a bunch of random (mostly) rects, and compare the gather results | 515 // Test a bunch of random (mostly) rects, and compare the gather results |
509 // with a deduced list of refs by looking at the colors drawn. | 516 // with a deduced list of refs by looking at the colors drawn. |
510 for (int j = 0; j < 100; ++j) { | 517 for (int j = 0; j < 100; ++j) { |
511 SkRect r; | 518 SkRect r; |
512 rand_rect(&r, rand, 2*W, 2*H); | 519 rand_rect(&r, rand, 2*W, 2*H); |
513 | 520 |
514 SkTDArray<SkPixelRef*> fromImage; | 521 SkTDArray<SkPixelRef*> fromImage; |
515 gather_from_image(image, refs, N, &fromImage, r); | 522 gather_from_image(image, refs, N, &fromImage, r); |
516 | 523 |
517 SkTDArray<SkPixelRef*> fromAnalytic; | 524 SkTDArray<SkPixelRef*> fromAnalytic; |
518 gather_from_analytic(pos, W, H, analytic, N, &fromAnalytic, r); | 525 gather_from_analytic(pos, W, H, analytic, N, &fromAnalytic, r); |
519 | 526 |
520 SkData* data = SkPictureUtils::GatherPixelRefs(pic, r); | 527 SkData* data = SkPictureUtils::GatherPixelRefs(pic, r); |
521 size_t dataSize = data ? data->size() : 0; | 528 size_t dataSize = data ? data->size() : 0; |
522 int gatherCount = static_cast<int>(dataSize / sizeof(SkPixelRef*)); | 529 int gatherCount = static_cast<int>(dataSize / sizeof(SkPixelRef*)); |
523 SkASSERT(gatherCount * sizeof(SkPixelRef*) == dataSize); | 530 SkASSERT(gatherCount * sizeof(SkPixelRef*) == dataSize); |
524 SkPixelRef** gatherRefs = data ? (SkPixelRef**)(data->data()) : NULL
; | 531 SkPixelRef** gatherRefs = data ? (SkPixelRef**)(data->data()) : NULL
; |
525 SkAutoDataUnref adu(data); | 532 SkAutoDataUnref adu(data); |
526 | 533 |
527 // Everything that we saw drawn should appear in the analytic list | 534 // Everything that we saw drawn should appear in the analytic list |
528 // but the analytic list may contain some pixelRefs that were not | 535 // but the analytic list may contain some pixelRefs that were not |
529 // seen in the image (e.g., A8 textures used as masks) | 536 // seen in the image (e.g., A8 textures used as masks) |
530 for (int i = 0; i < fromImage.count(); ++i) { | 537 for (int i = 0; i < fromImage.count(); ++i) { |
531 REPORTER_ASSERT(reporter, -1 != fromAnalytic.find(fromImage[i]))
; | 538 if (-1 == fromAnalytic.find(fromImage[i])) { |
| 539 ERRORF(reporter, "PixelRef missing %d %s", |
| 540 i, gProcs[k].desc); |
| 541 } |
532 } | 542 } |
533 | 543 |
534 /* | 544 /* |
535 * GatherPixelRefs is conservative, so it can return more bitmaps | 545 * GatherPixelRefs is conservative, so it can return more bitmaps |
536 * than are strictly required. Thus our check here is only that | 546 * than are strictly required. Thus our check here is only that |
537 * Gather didn't miss any that we actually needed. Even that isn't | 547 * Gather didn't miss any that we actually needed. Even that isn't |
538 * a strict requirement on Gather, which is meant to be quick and | 548 * a strict requirement on Gather, which is meant to be quick and |
539 * only mostly-correct, but at the moment this test should work. | 549 * only mostly-correct, but at the moment this test should work. |
540 */ | 550 */ |
541 for (int i = 0; i < fromAnalytic.count(); ++i) { | 551 for (int i = 0; i < fromAnalytic.count(); ++i) { |
542 bool found = find(gatherRefs, fromAnalytic[i], gatherCount); | 552 bool found = find(gatherRefs, fromAnalytic[i], gatherCount); |
543 REPORTER_ASSERT(reporter, found); | 553 if (!found) { |
| 554 ERRORF(reporter, "PixelRef missing %d %s", |
| 555 i, gProcs[k].desc); |
| 556 } |
544 #if 0 | 557 #if 0 |
545 // enable this block of code to debug failures, as it will rerun | 558 // enable this block of code to debug failures, as it will rerun |
546 // the case that failed. | 559 // the case that failed. |
547 if (!found) { | 560 if (!found) { |
548 SkData* data = SkPictureUtils::GatherPixelRefs(pic, r); | 561 SkData* data = SkPictureUtils::GatherPixelRefs(pic, r); |
549 size_t dataSize = data ? data->size() : 0; | 562 size_t dataSize = data ? data->size() : 0; |
550 } | 563 } |
551 #endif | 564 #endif |
552 } | 565 } |
553 } | 566 } |
(...skipping 12 matching lines...) Expand all Loading... |
566 SkTDArray<SkPixelRef*> analytic[N]; | 579 SkTDArray<SkPixelRef*> analytic[N]; |
567 | 580 |
568 const SkPoint pos[N] = { | 581 const SkPoint pos[N] = { |
569 { 0, 0 }, { W, 0 }, { 0, H }, { W, H } | 582 { 0, 0 }, { W, 0 }, { 0, H }, { W, H } |
570 }; | 583 }; |
571 | 584 |
572 create_textures(bm, refs, N, IW, IH); | 585 create_textures(bm, refs, N, IW, IH); |
573 | 586 |
574 SkRandom rand; | 587 SkRandom rand; |
575 for (size_t k = 0; k < SK_ARRAY_COUNT(gProcs); ++k) { | 588 for (size_t k = 0; k < SK_ARRAY_COUNT(gProcs); ++k) { |
576 SkAutoTUnref<SkPicture> pic(record_bitmaps(bm, pos, analytic, N, gProcs[
k])); | 589 SkAutoTUnref<SkPicture> pic( |
| 590 record_bitmaps(bm, pos, analytic, N, gProcs[k].proc)); |
577 | 591 |
578 REPORTER_ASSERT(reporter, pic->willPlayBackBitmaps() || N == 0); | 592 REPORTER_ASSERT(reporter, pic->willPlayBackBitmaps() || N == 0); |
579 | 593 |
580 SkAutoTUnref<SkPictureUtils::SkPixelRefContainer> prCont( | 594 SkAutoTUnref<SkPictureUtils::SkPixelRefContainer> prCont( |
581 new SkPictureUtils::SkPixelRefsAndRectsList); | 595 new SkPictureUtils::SkPixelRefsAndRectsList); |
582 | 596 |
583 SkPictureUtils::GatherPixelRefsAndRects(pic, prCont); | 597 SkPictureUtils::GatherPixelRefsAndRects(pic, prCont); |
584 | 598 |
585 // quick check for a small piece of each quadrant, which should just | 599 // quick check for a small piece of each quadrant, which should just |
586 // contain 1 or 2 bitmaps. | 600 // contain 1 or 2 bitmaps. |
(...skipping 535 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1122 picture.endRecording(); | 1136 picture.endRecording(); |
1123 } | 1137 } |
1124 | 1138 |
1125 DEF_TEST(Canvas_EmptyBitmap, r) { | 1139 DEF_TEST(Canvas_EmptyBitmap, r) { |
1126 SkBitmap dst; | 1140 SkBitmap dst; |
1127 dst.allocN32Pixels(10, 10); | 1141 dst.allocN32Pixels(10, 10); |
1128 SkCanvas canvas(dst); | 1142 SkCanvas canvas(dst); |
1129 | 1143 |
1130 test_draw_bitmaps(&canvas); | 1144 test_draw_bitmaps(&canvas); |
1131 } | 1145 } |
OLD | NEW |