blob: 6a978743f604bde2902dd6a30006830bed711c59 [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
8#include "SkCanvas.h"
9#include "SkDrawLooper.h"
commit-bot@chromium.org79fbb402014-03-12 09:42:01 +000010#include "SkTypes.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000011#include "Test.h"
reed@google.com3d608122011-11-21 15:16:16 +000012
13/*
14 * Subclass of looper that just draws once, with an offset in X.
15 */
16class TestLooper : public SkDrawLooper {
17public:
reed@google.com3d608122011-11-21 15:16:16 +000018
mtklein36352bf2015-03-25 18:17:31 -070019 SkDrawLooper::Context* createContext(SkCanvas*, void* storage) const override {
halcanary385fe4d2015-08-26 13:07:48 -070020 return new (storage) TestDrawLooperContext;
reed@google.com3d608122011-11-21 15:16:16 +000021 }
22
mtklein36352bf2015-03-25 18:17:31 -070023 size_t contextSize() const override { return sizeof(TestDrawLooperContext); }
reed@google.com3d608122011-11-21 15:16:16 +000024
commit-bot@chromium.org0f10f7b2014-03-13 18:02:17 +000025#ifndef SK_IGNORE_TO_STRING
mtklein36352bf2015-03-25 18:17:31 -070026 void toString(SkString* str) const override {
robertphillips@google.com4991b8f2013-01-28 20:21:59 +000027 str->append("TestLooper:");
28 }
29#endif
30
mtklein7e44bb12015-01-07 09:06:08 -080031 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(TestLooper);
32
commit-bot@chromium.org79fbb402014-03-12 09:42:01 +000033private:
34 class TestDrawLooperContext : public SkDrawLooper::Context {
35 public:
36 TestDrawLooperContext() : fOnce(true) {}
37 virtual ~TestDrawLooperContext() {}
38
mtklein36352bf2015-03-25 18:17:31 -070039 bool next(SkCanvas* canvas, SkPaint*) override {
commit-bot@chromium.org79fbb402014-03-12 09:42:01 +000040 if (fOnce) {
41 fOnce = false;
42 canvas->translate(SkIntToScalar(10), 0);
43 return true;
44 }
45 return false;
46 }
47 private:
48 bool fOnce;
49 };
reed@google.com3d608122011-11-21 15:16:16 +000050};
51
reed60c9b582016-04-03 09:11:13 -070052sk_sp<SkFlattenable> TestLooper::CreateProc(SkReadBuffer&) { return sk_make_sp<TestLooper>(); }
mtklein7e44bb12015-01-07 09:06:08 -080053
reed@google.com3d608122011-11-21 15:16:16 +000054static void test_drawBitmap(skiatest::Reporter* reporter) {
55 SkBitmap src;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +000056 src.allocN32Pixels(10, 10);
reed@google.com3d608122011-11-21 15:16:16 +000057 src.eraseColor(SK_ColorWHITE);
58
59 SkBitmap dst;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +000060 dst.allocN32Pixels(10, 10);
junov@google.comdbfac8a2012-12-06 21:47:40 +000061 dst.eraseColor(SK_ColorTRANSPARENT);
reed@google.com3d608122011-11-21 15:16:16 +000062
63 SkCanvas canvas(dst);
64 SkPaint paint;
65
66 // we are initially transparent
67 REPORTER_ASSERT(reporter, 0 == *dst.getAddr32(5, 5));
68
69 // we see the bitmap drawn
70 canvas.drawBitmap(src, 0, 0, &paint);
71 REPORTER_ASSERT(reporter, 0xFFFFFFFF == *dst.getAddr32(5, 5));
72
73 // reverify we are clear again
junov@google.comdbfac8a2012-12-06 21:47:40 +000074 dst.eraseColor(SK_ColorTRANSPARENT);
reed@google.com3d608122011-11-21 15:16:16 +000075 REPORTER_ASSERT(reporter, 0 == *dst.getAddr32(5, 5));
76
77 // if the bitmap is clipped out, we don't draw it
78 canvas.drawBitmap(src, SkIntToScalar(-10), 0, &paint);
79 REPORTER_ASSERT(reporter, 0 == *dst.getAddr32(5, 5));
80
81 // now install our looper, which will draw, since it internally translates
82 // to the left. The test is to ensure that canvas' quickReject machinary
83 // allows us through, even though sans-looper we would look like we should
84 // be clipped out.
reed7b380d02016-03-21 13:25:16 -070085 paint.setLooper(sk_make_sp<TestLooper>());
reed@google.com3d608122011-11-21 15:16:16 +000086 canvas.drawBitmap(src, SkIntToScalar(-10), 0, &paint);
87 REPORTER_ASSERT(reporter, 0xFFFFFFFF == *dst.getAddr32(5, 5));
88}
89
reed9b3aa542015-03-11 08:47:12 -070090static void test_layers(skiatest::Reporter* reporter) {
91 SkCanvas canvas(100, 100);
92
93 SkRect r = SkRect::MakeWH(10, 10);
94 REPORTER_ASSERT(reporter, false == canvas.quickReject(r));
95
96 r.offset(300, 300);
97 REPORTER_ASSERT(reporter, true == canvas.quickReject(r));
98
99 // Test that saveLayer updates quickReject
100 SkRect bounds = SkRect::MakeLTRB(50, 50, 70, 70);
halcanary96fcdcc2015-08-27 07:41:13 -0700101 canvas.saveLayer(&bounds, nullptr);
reed9b3aa542015-03-11 08:47:12 -0700102 REPORTER_ASSERT(reporter, true == canvas.quickReject(SkRect::MakeWH(10, 10)));
103 REPORTER_ASSERT(reporter, false == canvas.quickReject(SkRect::MakeWH(60, 60)));
104}
105
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000106DEF_TEST(QuickReject, reporter) {
reed@google.com3d608122011-11-21 15:16:16 +0000107 test_drawBitmap(reporter);
reed9b3aa542015-03-11 08:47:12 -0700108 test_layers(reporter);
reed@google.com3d608122011-11-21 15:16:16 +0000109}