2 * Copyright 2011 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
9 #include "SkDrawLooper.h"
14 * Subclass of looper that just draws once, with an offset in X.
16 class TestLooper : public SkDrawLooper {
19 virtual SkDrawLooper::Context* createContext(SkCanvas*, void* storage) const SK_OVERRIDE {
20 return SkNEW_PLACEMENT(storage, TestDrawLooperContext);
23 virtual size_t contextSize() const SK_OVERRIDE { return sizeof(TestDrawLooperContext); }
25 #ifndef SK_IGNORE_TO_STRING
26 virtual void toString(SkString* str) const SK_OVERRIDE {
27 str->append("TestLooper:");
32 class TestDrawLooperContext : public SkDrawLooper::Context {
34 TestDrawLooperContext() : fOnce(true) {}
35 virtual ~TestDrawLooperContext() {}
37 virtual bool next(SkCanvas* canvas, SkPaint*) SK_OVERRIDE {
40 canvas->translate(SkIntToScalar(10), 0);
49 SK_DECLARE_UNFLATTENABLE_OBJECT()
52 static void test_drawBitmap(skiatest::Reporter* reporter) {
54 src.allocN32Pixels(10, 10);
55 src.eraseColor(SK_ColorWHITE);
58 dst.allocN32Pixels(10, 10);
59 dst.eraseColor(SK_ColorTRANSPARENT);
64 // we are initially transparent
65 REPORTER_ASSERT(reporter, 0 == *dst.getAddr32(5, 5));
67 // we see the bitmap drawn
68 canvas.drawBitmap(src, 0, 0, &paint);
69 REPORTER_ASSERT(reporter, 0xFFFFFFFF == *dst.getAddr32(5, 5));
71 // reverify we are clear again
72 dst.eraseColor(SK_ColorTRANSPARENT);
73 REPORTER_ASSERT(reporter, 0 == *dst.getAddr32(5, 5));
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));
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
83 paint.setLooper(new TestLooper)->unref();
84 canvas.drawBitmap(src, SkIntToScalar(-10), 0, &paint);
85 REPORTER_ASSERT(reporter, 0xFFFFFFFF == *dst.getAddr32(5, 5));
88 DEF_TEST(QuickReject, reporter) {
89 test_drawBitmap(reporter);