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" 10 #include "SkTypes.h" 11 #include "Test.h" 12 13 /* 14 * Subclass of looper that just draws once, with an offset in X. 15 */ 16 class TestLooper : public SkDrawLooper { 17 public: 18 19 virtual SkDrawLooper::Context* createContext(SkCanvas*, void* storage) const SK_OVERRIDE { 20 return SkNEW_PLACEMENT(storage, TestDrawLooperContext); 21 } 22 23 virtual size_t contextSize() const SK_OVERRIDE { return sizeof(TestDrawLooperContext); } 24 25 #ifndef SK_IGNORE_TO_STRING 26 virtual void toString(SkString* str) const SK_OVERRIDE { 27 str->append("TestLooper:"); 28 } 29 #endif 30 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 49 SK_DECLARE_UNFLATTENABLE_OBJECT() 50 }; 51 52 static void test_drawBitmap(skiatest::Reporter* reporter) { 53 SkBitmap src; 54 src.allocN32Pixels(10, 10); 55 src.eraseColor(SK_ColorWHITE); 56 57 SkBitmap dst; 58 dst.allocN32Pixels(10, 10); 59 dst.eraseColor(SK_ColorTRANSPARENT); 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 72 dst.eraseColor(SK_ColorTRANSPARENT); 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 88 DEF_TEST(QuickReject, reporter) { 89 test_drawBitmap(reporter); 90 } 91