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