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 "Test.h"
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
init(SkCanvas *)19 virtual void init(SkCanvas*) SK_OVERRIDE {
20 fOnce = true;
21 }
22
next(SkCanvas * canvas,SkPaint *)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
getFactory()32 virtual Factory getFactory() SK_OVERRIDE {
33 return NULL;
34 }
35 };
36
test_drawBitmap(skiatest::Reporter * reporter)37 static void test_drawBitmap(skiatest::Reporter* reporter) {
38 SkBitmap src;
39 src.setConfig(SkBitmap::kARGB_8888_Config, 10, 10);
40 src.allocPixels();
41 src.eraseColor(SK_ColorWHITE);
42
43 SkBitmap dst;
44 dst.setConfig(SkBitmap::kARGB_8888_Config, 10, 10);
45 dst.allocPixels();
46 dst.eraseColor(0);
47
48 SkCanvas canvas(dst);
49 SkPaint paint;
50
51 // we are initially transparent
52 REPORTER_ASSERT(reporter, 0 == *dst.getAddr32(5, 5));
53
54 // we see the bitmap drawn
55 canvas.drawBitmap(src, 0, 0, &paint);
56 REPORTER_ASSERT(reporter, 0xFFFFFFFF == *dst.getAddr32(5, 5));
57
58 // reverify we are clear again
59 dst.eraseColor(0);
60 REPORTER_ASSERT(reporter, 0 == *dst.getAddr32(5, 5));
61
62 // if the bitmap is clipped out, we don't draw it
63 canvas.drawBitmap(src, SkIntToScalar(-10), 0, &paint);
64 REPORTER_ASSERT(reporter, 0 == *dst.getAddr32(5, 5));
65
66 // now install our looper, which will draw, since it internally translates
67 // to the left. The test is to ensure that canvas' quickReject machinary
68 // allows us through, even though sans-looper we would look like we should
69 // be clipped out.
70 paint.setLooper(new TestLooper)->unref();
71 canvas.drawBitmap(src, SkIntToScalar(-10), 0, &paint);
72 REPORTER_ASSERT(reporter, 0xFFFFFFFF == *dst.getAddr32(5, 5));
73 }
74
test(skiatest::Reporter * reporter)75 static void test(skiatest::Reporter* reporter) {
76 test_drawBitmap(reporter);
77 }
78
79 #include "TestClassDef.h"
80 DEFINE_TESTCLASS("QuickReject", QuickRejectClass, test)
81