blob: 13e3d1e877900a7c464f94c29c4b85d01d33e74d [file] [log] [blame]
reed@google.com3d608122011-11-21 15:16:16 +00001/*
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 Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkBitmap.h"
9#include "include/core/SkCanvas.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkPoint3.h"
11#include "include/core/SkTypes.h"
Michael Ludwig55edb502019-08-05 10:41:10 -040012#include "include/effects/SkImageFilters.h"
Ben Wagner729a23f2019-05-17 16:29:34 -040013#include "src/core/SkArenaAlloc.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "tests/Test.h"
reed@google.com3d608122011-11-21 15:16:16 +000015
reed@google.com3d608122011-11-21 15:16:16 +000016static void test_drawBitmap(skiatest::Reporter* reporter) {
17 SkBitmap src;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +000018 src.allocN32Pixels(10, 10);
reed@google.com3d608122011-11-21 15:16:16 +000019 src.eraseColor(SK_ColorWHITE);
20
21 SkBitmap dst;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +000022 dst.allocN32Pixels(10, 10);
junov@google.comdbfac8a2012-12-06 21:47:40 +000023 dst.eraseColor(SK_ColorTRANSPARENT);
reed@google.com3d608122011-11-21 15:16:16 +000024
25 SkCanvas canvas(dst);
reed@google.com3d608122011-11-21 15:16:16 +000026
27 // we are initially transparent
28 REPORTER_ASSERT(reporter, 0 == *dst.getAddr32(5, 5));
29
30 // we see the bitmap drawn
Mike Reed34a0c972021-01-25 17:49:32 -050031 canvas.drawImage(src.asImage(), 0, 0);
reed@google.com3d608122011-11-21 15:16:16 +000032 REPORTER_ASSERT(reporter, 0xFFFFFFFF == *dst.getAddr32(5, 5));
33
34 // reverify we are clear again
junov@google.comdbfac8a2012-12-06 21:47:40 +000035 dst.eraseColor(SK_ColorTRANSPARENT);
reed@google.com3d608122011-11-21 15:16:16 +000036 REPORTER_ASSERT(reporter, 0 == *dst.getAddr32(5, 5));
37
38 // if the bitmap is clipped out, we don't draw it
Mike Reed34a0c972021-01-25 17:49:32 -050039 canvas.drawImage(src.asImage(), SkIntToScalar(-10), 0);
reed@google.com3d608122011-11-21 15:16:16 +000040 REPORTER_ASSERT(reporter, 0 == *dst.getAddr32(5, 5));
reed@google.com3d608122011-11-21 15:16:16 +000041}
42
reed9b3aa542015-03-11 08:47:12 -070043static void test_layers(skiatest::Reporter* reporter) {
44 SkCanvas canvas(100, 100);
45
46 SkRect r = SkRect::MakeWH(10, 10);
47 REPORTER_ASSERT(reporter, false == canvas.quickReject(r));
48
49 r.offset(300, 300);
50 REPORTER_ASSERT(reporter, true == canvas.quickReject(r));
51
52 // Test that saveLayer updates quickReject
53 SkRect bounds = SkRect::MakeLTRB(50, 50, 70, 70);
halcanary96fcdcc2015-08-27 07:41:13 -070054 canvas.saveLayer(&bounds, nullptr);
reed9b3aa542015-03-11 08:47:12 -070055 REPORTER_ASSERT(reporter, true == canvas.quickReject(SkRect::MakeWH(10, 10)));
56 REPORTER_ASSERT(reporter, false == canvas.quickReject(SkRect::MakeWH(60, 60)));
57}
58
msarettfbfa2582016-08-12 08:29:08 -070059static void test_quick_reject(skiatest::Reporter* reporter) {
60 SkCanvas canvas(100, 100);
61 SkRect r0 = SkRect::MakeLTRB(-50.0f, -50.0f, 50.0f, 50.0f);
62 SkRect r1 = SkRect::MakeLTRB(-50.0f, 110.0f, 50.0f, 120.0f);
63 SkRect r2 = SkRect::MakeLTRB(110.0f, -50.0f, 120.0f, 50.0f);
64 SkRect r3 = SkRect::MakeLTRB(-120.0f, -50.0f, 120.0f, 50.0f);
65 SkRect r4 = SkRect::MakeLTRB(-50.0f, -120.0f, 50.0f, 120.0f);
66 SkRect r5 = SkRect::MakeLTRB(-120.0f, -120.0f, 120.0f, 120.0f);
67 SkRect r6 = SkRect::MakeLTRB(-120.0f, -120.0f, -110.0f, -110.0f);
68 SkRect r7 = SkRect::MakeLTRB(SK_ScalarNaN, -50.0f, 50.0f, 50.0f);
69 SkRect r8 = SkRect::MakeLTRB(-50.0f, SK_ScalarNaN, 50.0f, 50.0f);
70 SkRect r9 = SkRect::MakeLTRB(-50.0f, -50.0f, SK_ScalarNaN, 50.0f);
71 SkRect r10 = SkRect::MakeLTRB(-50.0f, -50.0f, 50.0f, SK_ScalarNaN);
72 REPORTER_ASSERT(reporter, false == canvas.quickReject(r0));
73 REPORTER_ASSERT(reporter, true == canvas.quickReject(r1));
74 REPORTER_ASSERT(reporter, true == canvas.quickReject(r2));
75 REPORTER_ASSERT(reporter, false == canvas.quickReject(r3));
76 REPORTER_ASSERT(reporter, false == canvas.quickReject(r4));
77 REPORTER_ASSERT(reporter, false == canvas.quickReject(r5));
78 REPORTER_ASSERT(reporter, true == canvas.quickReject(r6));
79 REPORTER_ASSERT(reporter, true == canvas.quickReject(r7));
80 REPORTER_ASSERT(reporter, true == canvas.quickReject(r8));
81 REPORTER_ASSERT(reporter, true == canvas.quickReject(r9));
82 REPORTER_ASSERT(reporter, true == canvas.quickReject(r10));
83
Mike Reed1f607332020-05-21 12:11:27 -040084 SkMatrix m = SkMatrix::Scale(2, 2);
msarettfbfa2582016-08-12 08:29:08 -070085 m.setTranslateX(10.0f);
86 m.setTranslateY(10.0f);
87 canvas.setMatrix(m);
88 SkRect r11 = SkRect::MakeLTRB(5.0f, 5.0f, 100.0f, 100.0f);
89 SkRect r12 = SkRect::MakeLTRB(5.0f, 50.0f, 100.0f, 100.0f);
90 SkRect r13 = SkRect::MakeLTRB(50.0f, 5.0f, 100.0f, 100.0f);
91 REPORTER_ASSERT(reporter, false == canvas.quickReject(r11));
92 REPORTER_ASSERT(reporter, true == canvas.quickReject(r12));
93 REPORTER_ASSERT(reporter, true == canvas.quickReject(r13));
94}
95
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +000096DEF_TEST(QuickReject, reporter) {
reed@google.com3d608122011-11-21 15:16:16 +000097 test_drawBitmap(reporter);
reed9b3aa542015-03-11 08:47:12 -070098 test_layers(reporter);
msarettfbfa2582016-08-12 08:29:08 -070099 test_quick_reject(reporter);
reed@google.com3d608122011-11-21 15:16:16 +0000100}
msarett9da5a5a2016-08-19 08:38:36 -0700101
102// Regression test to make sure that we keep fIsScaleTranslate up to date on the canvas.
103// It is possible to set a new matrix on the canvas without calling setMatrix(). This tests
104// that code path.
105DEF_TEST(QuickReject_MatrixState, reporter) {
106 SkCanvas canvas(100, 100);
107
108 SkMatrix matrix;
109 matrix.setRotate(45.0f);
110 canvas.setMatrix(matrix);
111
112 SkPaint paint;
Michael Ludwig55edb502019-08-05 10:41:10 -0400113 sk_sp<SkImageFilter> filter = SkImageFilters::DistantLitDiffuse(
msarett9da5a5a2016-08-19 08:38:36 -0700114 SkPoint3::Make(1.0f, 1.0f, 1.0f), 0xFF0000FF, 2.0f, 0.5f, nullptr);
115 REPORTER_ASSERT(reporter, filter);
116 paint.setImageFilter(filter);
117 SkCanvas::SaveLayerRec rec;
118 rec.fPaint = &paint;
119 canvas.saveLayer(rec);
120
121 // quickReject() will assert if the matrix is out of sync.
122 canvas.quickReject(SkRect::MakeWH(100.0f, 100.0f));
123}