reed@google.com | 3d60812 | 2011-11-21 15:16:16 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011 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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "include/core/SkBitmap.h" |
| 9 | #include "include/core/SkCanvas.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "include/core/SkPoint3.h" |
| 11 | #include "include/core/SkTypes.h" |
Michael Ludwig | 55edb50 | 2019-08-05 10:41:10 -0400 | [diff] [blame] | 12 | #include "include/effects/SkImageFilters.h" |
Ben Wagner | 729a23f | 2019-05-17 16:29:34 -0400 | [diff] [blame] | 13 | #include "src/core/SkArenaAlloc.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 14 | #include "tests/Test.h" |
reed@google.com | 3d60812 | 2011-11-21 15:16:16 +0000 | [diff] [blame] | 15 | |
reed@google.com | 3d60812 | 2011-11-21 15:16:16 +0000 | [diff] [blame] | 16 | static void test_drawBitmap(skiatest::Reporter* reporter) { |
| 17 | SkBitmap src; |
mike@reedtribe.org | deee496 | 2014-02-13 14:41:43 +0000 | [diff] [blame] | 18 | src.allocN32Pixels(10, 10); |
reed@google.com | 3d60812 | 2011-11-21 15:16:16 +0000 | [diff] [blame] | 19 | src.eraseColor(SK_ColorWHITE); |
| 20 | |
| 21 | SkBitmap dst; |
mike@reedtribe.org | deee496 | 2014-02-13 14:41:43 +0000 | [diff] [blame] | 22 | dst.allocN32Pixels(10, 10); |
junov@google.com | dbfac8a | 2012-12-06 21:47:40 +0000 | [diff] [blame] | 23 | dst.eraseColor(SK_ColorTRANSPARENT); |
reed@google.com | 3d60812 | 2011-11-21 15:16:16 +0000 | [diff] [blame] | 24 | |
| 25 | SkCanvas canvas(dst); |
| 26 | SkPaint paint; |
| 27 | |
| 28 | // we are initially transparent |
| 29 | REPORTER_ASSERT(reporter, 0 == *dst.getAddr32(5, 5)); |
| 30 | |
| 31 | // we see the bitmap drawn |
| 32 | canvas.drawBitmap(src, 0, 0, &paint); |
| 33 | REPORTER_ASSERT(reporter, 0xFFFFFFFF == *dst.getAddr32(5, 5)); |
| 34 | |
| 35 | // reverify we are clear again |
junov@google.com | dbfac8a | 2012-12-06 21:47:40 +0000 | [diff] [blame] | 36 | dst.eraseColor(SK_ColorTRANSPARENT); |
reed@google.com | 3d60812 | 2011-11-21 15:16:16 +0000 | [diff] [blame] | 37 | REPORTER_ASSERT(reporter, 0 == *dst.getAddr32(5, 5)); |
| 38 | |
| 39 | // if the bitmap is clipped out, we don't draw it |
| 40 | canvas.drawBitmap(src, SkIntToScalar(-10), 0, &paint); |
| 41 | REPORTER_ASSERT(reporter, 0 == *dst.getAddr32(5, 5)); |
reed@google.com | 3d60812 | 2011-11-21 15:16:16 +0000 | [diff] [blame] | 42 | } |
| 43 | |
reed | 9b3aa54 | 2015-03-11 08:47:12 -0700 | [diff] [blame] | 44 | static void test_layers(skiatest::Reporter* reporter) { |
| 45 | SkCanvas canvas(100, 100); |
| 46 | |
| 47 | SkRect r = SkRect::MakeWH(10, 10); |
| 48 | REPORTER_ASSERT(reporter, false == canvas.quickReject(r)); |
| 49 | |
| 50 | r.offset(300, 300); |
| 51 | REPORTER_ASSERT(reporter, true == canvas.quickReject(r)); |
| 52 | |
| 53 | // Test that saveLayer updates quickReject |
| 54 | SkRect bounds = SkRect::MakeLTRB(50, 50, 70, 70); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 55 | canvas.saveLayer(&bounds, nullptr); |
reed | 9b3aa54 | 2015-03-11 08:47:12 -0700 | [diff] [blame] | 56 | REPORTER_ASSERT(reporter, true == canvas.quickReject(SkRect::MakeWH(10, 10))); |
| 57 | REPORTER_ASSERT(reporter, false == canvas.quickReject(SkRect::MakeWH(60, 60))); |
| 58 | } |
| 59 | |
msarett | fbfa258 | 2016-08-12 08:29:08 -0700 | [diff] [blame] | 60 | static void test_quick_reject(skiatest::Reporter* reporter) { |
| 61 | SkCanvas canvas(100, 100); |
| 62 | SkRect r0 = SkRect::MakeLTRB(-50.0f, -50.0f, 50.0f, 50.0f); |
| 63 | SkRect r1 = SkRect::MakeLTRB(-50.0f, 110.0f, 50.0f, 120.0f); |
| 64 | SkRect r2 = SkRect::MakeLTRB(110.0f, -50.0f, 120.0f, 50.0f); |
| 65 | SkRect r3 = SkRect::MakeLTRB(-120.0f, -50.0f, 120.0f, 50.0f); |
| 66 | SkRect r4 = SkRect::MakeLTRB(-50.0f, -120.0f, 50.0f, 120.0f); |
| 67 | SkRect r5 = SkRect::MakeLTRB(-120.0f, -120.0f, 120.0f, 120.0f); |
| 68 | SkRect r6 = SkRect::MakeLTRB(-120.0f, -120.0f, -110.0f, -110.0f); |
| 69 | SkRect r7 = SkRect::MakeLTRB(SK_ScalarNaN, -50.0f, 50.0f, 50.0f); |
| 70 | SkRect r8 = SkRect::MakeLTRB(-50.0f, SK_ScalarNaN, 50.0f, 50.0f); |
| 71 | SkRect r9 = SkRect::MakeLTRB(-50.0f, -50.0f, SK_ScalarNaN, 50.0f); |
| 72 | SkRect r10 = SkRect::MakeLTRB(-50.0f, -50.0f, 50.0f, SK_ScalarNaN); |
| 73 | REPORTER_ASSERT(reporter, false == canvas.quickReject(r0)); |
| 74 | REPORTER_ASSERT(reporter, true == canvas.quickReject(r1)); |
| 75 | REPORTER_ASSERT(reporter, true == canvas.quickReject(r2)); |
| 76 | REPORTER_ASSERT(reporter, false == canvas.quickReject(r3)); |
| 77 | REPORTER_ASSERT(reporter, false == canvas.quickReject(r4)); |
| 78 | REPORTER_ASSERT(reporter, false == canvas.quickReject(r5)); |
| 79 | REPORTER_ASSERT(reporter, true == canvas.quickReject(r6)); |
| 80 | REPORTER_ASSERT(reporter, true == canvas.quickReject(r7)); |
| 81 | REPORTER_ASSERT(reporter, true == canvas.quickReject(r8)); |
| 82 | REPORTER_ASSERT(reporter, true == canvas.quickReject(r9)); |
| 83 | REPORTER_ASSERT(reporter, true == canvas.quickReject(r10)); |
| 84 | |
Mike Reed | 1f60733 | 2020-05-21 12:11:27 -0400 | [diff] [blame] | 85 | SkMatrix m = SkMatrix::Scale(2, 2); |
msarett | fbfa258 | 2016-08-12 08:29:08 -0700 | [diff] [blame] | 86 | m.setTranslateX(10.0f); |
| 87 | m.setTranslateY(10.0f); |
| 88 | canvas.setMatrix(m); |
| 89 | SkRect r11 = SkRect::MakeLTRB(5.0f, 5.0f, 100.0f, 100.0f); |
| 90 | SkRect r12 = SkRect::MakeLTRB(5.0f, 50.0f, 100.0f, 100.0f); |
| 91 | SkRect r13 = SkRect::MakeLTRB(50.0f, 5.0f, 100.0f, 100.0f); |
| 92 | REPORTER_ASSERT(reporter, false == canvas.quickReject(r11)); |
| 93 | REPORTER_ASSERT(reporter, true == canvas.quickReject(r12)); |
| 94 | REPORTER_ASSERT(reporter, true == canvas.quickReject(r13)); |
| 95 | } |
| 96 | |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 97 | DEF_TEST(QuickReject, reporter) { |
reed@google.com | 3d60812 | 2011-11-21 15:16:16 +0000 | [diff] [blame] | 98 | test_drawBitmap(reporter); |
reed | 9b3aa54 | 2015-03-11 08:47:12 -0700 | [diff] [blame] | 99 | test_layers(reporter); |
msarett | fbfa258 | 2016-08-12 08:29:08 -0700 | [diff] [blame] | 100 | test_quick_reject(reporter); |
reed@google.com | 3d60812 | 2011-11-21 15:16:16 +0000 | [diff] [blame] | 101 | } |
msarett | 9da5a5a | 2016-08-19 08:38:36 -0700 | [diff] [blame] | 102 | |
| 103 | // Regression test to make sure that we keep fIsScaleTranslate up to date on the canvas. |
| 104 | // It is possible to set a new matrix on the canvas without calling setMatrix(). This tests |
| 105 | // that code path. |
| 106 | DEF_TEST(QuickReject_MatrixState, reporter) { |
| 107 | SkCanvas canvas(100, 100); |
| 108 | |
| 109 | SkMatrix matrix; |
| 110 | matrix.setRotate(45.0f); |
| 111 | canvas.setMatrix(matrix); |
| 112 | |
| 113 | SkPaint paint; |
Michael Ludwig | 55edb50 | 2019-08-05 10:41:10 -0400 | [diff] [blame] | 114 | sk_sp<SkImageFilter> filter = SkImageFilters::DistantLitDiffuse( |
msarett | 9da5a5a | 2016-08-19 08:38:36 -0700 | [diff] [blame] | 115 | SkPoint3::Make(1.0f, 1.0f, 1.0f), 0xFF0000FF, 2.0f, 0.5f, nullptr); |
| 116 | REPORTER_ASSERT(reporter, filter); |
| 117 | paint.setImageFilter(filter); |
| 118 | SkCanvas::SaveLayerRec rec; |
| 119 | rec.fPaint = &paint; |
| 120 | canvas.saveLayer(rec); |
| 121 | |
| 122 | // quickReject() will assert if the matrix is out of sync. |
| 123 | canvas.quickReject(SkRect::MakeWH(100.0f, 100.0f)); |
| 124 | } |