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 | |
commit-bot@chromium.org | 79fbb40 | 2014-03-12 09:42:01 +0000 | [diff] [blame] | 19 | virtual SkDrawLooper::Context* createContext(SkCanvas*, void* storage) const SK_OVERRIDE { |
| 20 | return SkNEW_PLACEMENT(storage, TestDrawLooperContext); |
reed@google.com | 3d60812 | 2011-11-21 15:16:16 +0000 | [diff] [blame] | 21 | } |
| 22 | |
commit-bot@chromium.org | 79fbb40 | 2014-03-12 09:42:01 +0000 | [diff] [blame] | 23 | virtual size_t contextSize() const SK_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 |
robertphillips@google.com | 4991b8f | 2013-01-28 20:21:59 +0000 | [diff] [blame] | 26 | virtual void toString(SkString* str) const SK_OVERRIDE { |
| 27 | str->append("TestLooper:"); |
| 28 | } |
| 29 | #endif |
| 30 | |
commit-bot@chromium.org | 79fbb40 | 2014-03-12 09:42:01 +0000 | [diff] [blame] | 31 | private: |
| 32 | class TestDrawLooperContext : public SkDrawLooper::Context { |
| 33 | public: |
| 34 | TestDrawLooperContext() : fOnce(true) {} |
| 35 | virtual ~TestDrawLooperContext() {} |
| 36 | |
| 37 | virtual bool next(SkCanvas* canvas, SkPaint*) SK_OVERRIDE { |
| 38 | if (fOnce) { |
| 39 | fOnce = false; |
| 40 | canvas->translate(SkIntToScalar(10), 0); |
| 41 | return true; |
| 42 | } |
| 43 | return false; |
| 44 | } |
| 45 | private: |
| 46 | bool fOnce; |
| 47 | }; |
| 48 | |
djsollen@google.com | ba28d03 | 2012-03-26 17:57:35 +0000 | [diff] [blame] | 49 | SK_DECLARE_UNFLATTENABLE_OBJECT() |
reed@google.com | 3d60812 | 2011-11-21 15:16:16 +0000 | [diff] [blame] | 50 | }; |
| 51 | |
| 52 | static void test_drawBitmap(skiatest::Reporter* reporter) { |
| 53 | SkBitmap src; |
mike@reedtribe.org | deee496 | 2014-02-13 14:41:43 +0000 | [diff] [blame] | 54 | src.allocN32Pixels(10, 10); |
reed@google.com | 3d60812 | 2011-11-21 15:16:16 +0000 | [diff] [blame] | 55 | src.eraseColor(SK_ColorWHITE); |
| 56 | |
| 57 | SkBitmap dst; |
mike@reedtribe.org | deee496 | 2014-02-13 14:41:43 +0000 | [diff] [blame] | 58 | dst.allocN32Pixels(10, 10); |
junov@google.com | dbfac8a | 2012-12-06 21:47:40 +0000 | [diff] [blame] | 59 | dst.eraseColor(SK_ColorTRANSPARENT); |
reed@google.com | 3d60812 | 2011-11-21 15:16:16 +0000 | [diff] [blame] | 60 | |
| 61 | SkCanvas canvas(dst); |
| 62 | SkPaint paint; |
| 63 | |
| 64 | // we are initially transparent |
| 65 | REPORTER_ASSERT(reporter, 0 == *dst.getAddr32(5, 5)); |
| 66 | |
| 67 | // we see the bitmap drawn |
| 68 | canvas.drawBitmap(src, 0, 0, &paint); |
| 69 | REPORTER_ASSERT(reporter, 0xFFFFFFFF == *dst.getAddr32(5, 5)); |
| 70 | |
| 71 | // reverify we are clear again |
junov@google.com | dbfac8a | 2012-12-06 21:47:40 +0000 | [diff] [blame] | 72 | dst.eraseColor(SK_ColorTRANSPARENT); |
reed@google.com | 3d60812 | 2011-11-21 15:16:16 +0000 | [diff] [blame] | 73 | REPORTER_ASSERT(reporter, 0 == *dst.getAddr32(5, 5)); |
| 74 | |
| 75 | // if the bitmap is clipped out, we don't draw it |
| 76 | canvas.drawBitmap(src, SkIntToScalar(-10), 0, &paint); |
| 77 | REPORTER_ASSERT(reporter, 0 == *dst.getAddr32(5, 5)); |
| 78 | |
| 79 | // now install our looper, which will draw, since it internally translates |
| 80 | // to the left. The test is to ensure that canvas' quickReject machinary |
| 81 | // allows us through, even though sans-looper we would look like we should |
| 82 | // be clipped out. |
| 83 | paint.setLooper(new TestLooper)->unref(); |
| 84 | canvas.drawBitmap(src, SkIntToScalar(-10), 0, &paint); |
| 85 | REPORTER_ASSERT(reporter, 0xFFFFFFFF == *dst.getAddr32(5, 5)); |
| 86 | } |
| 87 | |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 88 | DEF_TEST(QuickReject, reporter) { |
reed@google.com | 3d60812 | 2011-11-21 15:16:16 +0000 | [diff] [blame] | 89 | test_drawBitmap(reporter); |
| 90 | } |