| 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 |  | 
 | 8 | #include "SkCanvas.h" | 
 | 9 | #include "SkDrawLooper.h" | 
| commit-bot@chromium.org | 79fbb40 | 2014-03-12 09:42:01 +0000 | [diff] [blame] | 10 | #include "SkTypes.h" | 
| tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 11 | #include "Test.h" | 
| reed@google.com | 3d60812 | 2011-11-21 15:16:16 +0000 | [diff] [blame] | 12 |  | 
 | 13 | /* | 
 | 14 |  *  Subclass of looper that just draws once, with an offset in X. | 
 | 15 |  */ | 
 | 16 | class TestLooper : public SkDrawLooper { | 
 | 17 | public: | 
| reed@google.com | 3d60812 | 2011-11-21 15:16:16 +0000 | [diff] [blame] | 18 |  | 
| mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 19 |     SkDrawLooper::Context* createContext(SkCanvas*, void* storage) const override { | 
| commit-bot@chromium.org | 79fbb40 | 2014-03-12 09:42:01 +0000 | [diff] [blame] | 20 |         return SkNEW_PLACEMENT(storage, TestDrawLooperContext); | 
| reed@google.com | 3d60812 | 2011-11-21 15:16:16 +0000 | [diff] [blame] | 21 |     } | 
 | 22 |  | 
| mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 23 |     size_t contextSize() const override { return sizeof(TestDrawLooperContext); } | 
| reed@google.com | 3d60812 | 2011-11-21 15:16:16 +0000 | [diff] [blame] | 24 |  | 
| commit-bot@chromium.org | 0f10f7b | 2014-03-13 18:02:17 +0000 | [diff] [blame] | 25 | #ifndef SK_IGNORE_TO_STRING | 
| mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 26 |     void toString(SkString* str) const override { | 
| robertphillips@google.com | 4991b8f | 2013-01-28 20:21:59 +0000 | [diff] [blame] | 27 |         str->append("TestLooper:"); | 
 | 28 |     } | 
 | 29 | #endif | 
 | 30 |  | 
| mtklein | 7e44bb1 | 2015-01-07 09:06:08 -0800 | [diff] [blame] | 31 |     SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(TestLooper); | 
 | 32 |  | 
| commit-bot@chromium.org | 79fbb40 | 2014-03-12 09:42:01 +0000 | [diff] [blame] | 33 | private: | 
 | 34 |     class TestDrawLooperContext : public SkDrawLooper::Context { | 
 | 35 |     public: | 
 | 36 |         TestDrawLooperContext() : fOnce(true) {} | 
 | 37 |         virtual ~TestDrawLooperContext() {} | 
 | 38 |  | 
| mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 39 |         bool next(SkCanvas* canvas, SkPaint*) override { | 
| commit-bot@chromium.org | 79fbb40 | 2014-03-12 09:42:01 +0000 | [diff] [blame] | 40 |             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.com | 3d60812 | 2011-11-21 15:16:16 +0000 | [diff] [blame] | 50 | }; | 
 | 51 |  | 
| mtklein | 7e44bb1 | 2015-01-07 09:06:08 -0800 | [diff] [blame] | 52 | SkFlattenable* TestLooper::CreateProc(SkReadBuffer&) { return SkNEW(TestLooper); } | 
 | 53 |  | 
| reed@google.com | 3d60812 | 2011-11-21 15:16:16 +0000 | [diff] [blame] | 54 | static void test_drawBitmap(skiatest::Reporter* reporter) { | 
 | 55 |     SkBitmap src; | 
| mike@reedtribe.org | deee496 | 2014-02-13 14:41:43 +0000 | [diff] [blame] | 56 |     src.allocN32Pixels(10, 10); | 
| reed@google.com | 3d60812 | 2011-11-21 15:16:16 +0000 | [diff] [blame] | 57 |     src.eraseColor(SK_ColorWHITE); | 
 | 58 |  | 
 | 59 |     SkBitmap dst; | 
| mike@reedtribe.org | deee496 | 2014-02-13 14:41:43 +0000 | [diff] [blame] | 60 |     dst.allocN32Pixels(10, 10); | 
| junov@google.com | dbfac8a | 2012-12-06 21:47:40 +0000 | [diff] [blame] | 61 |     dst.eraseColor(SK_ColorTRANSPARENT); | 
| reed@google.com | 3d60812 | 2011-11-21 15:16:16 +0000 | [diff] [blame] | 62 |  | 
 | 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.com | dbfac8a | 2012-12-06 21:47:40 +0000 | [diff] [blame] | 74 |     dst.eraseColor(SK_ColorTRANSPARENT); | 
| reed@google.com | 3d60812 | 2011-11-21 15:16:16 +0000 | [diff] [blame] | 75 |     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. | 
 | 85 |     paint.setLooper(new TestLooper)->unref(); | 
 | 86 |     canvas.drawBitmap(src, SkIntToScalar(-10), 0, &paint); | 
 | 87 |     REPORTER_ASSERT(reporter, 0xFFFFFFFF == *dst.getAddr32(5, 5)); | 
 | 88 | } | 
 | 89 |  | 
| reed | 9b3aa54 | 2015-03-11 08:47:12 -0700 | [diff] [blame] | 90 | static 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); | 
 | 101 |     canvas.saveLayer(&bounds, NULL); | 
 | 102 |     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.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 106 | DEF_TEST(QuickReject, reporter) { | 
| reed@google.com | 3d60812 | 2011-11-21 15:16:16 +0000 | [diff] [blame] | 107 |     test_drawBitmap(reporter); | 
| reed | 9b3aa54 | 2015-03-11 08:47:12 -0700 | [diff] [blame] | 108 |     test_layers(reporter); | 
| reed@google.com | 3d60812 | 2011-11-21 15:16:16 +0000 | [diff] [blame] | 109 | } |