blob: b8fb989966f543393c5f38595685f579aedf76d3 [file] [log] [blame]
reed@google.com3d608122011-11-21 15:16:16 +00001/*
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 */
15class TestLooper : public SkDrawLooper {
16public:
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
djsollen@google.comba28d032012-03-26 17:57:35 +000032 SK_DECLARE_UNFLATTENABLE_OBJECT()
reed@google.com3d608122011-11-21 15:16:16 +000033};
34
35static void test_drawBitmap(skiatest::Reporter* reporter) {
36 SkBitmap src;
37 src.setConfig(SkBitmap::kARGB_8888_Config, 10, 10);
38 src.allocPixels();
39 src.eraseColor(SK_ColorWHITE);
40
41 SkBitmap dst;
42 dst.setConfig(SkBitmap::kARGB_8888_Config, 10, 10);
43 dst.allocPixels();
44 dst.eraseColor(0);
45
46 SkCanvas canvas(dst);
47 SkPaint paint;
48
49 // we are initially transparent
50 REPORTER_ASSERT(reporter, 0 == *dst.getAddr32(5, 5));
51
52 // we see the bitmap drawn
53 canvas.drawBitmap(src, 0, 0, &paint);
54 REPORTER_ASSERT(reporter, 0xFFFFFFFF == *dst.getAddr32(5, 5));
55
56 // reverify we are clear again
57 dst.eraseColor(0);
58 REPORTER_ASSERT(reporter, 0 == *dst.getAddr32(5, 5));
59
60 // if the bitmap is clipped out, we don't draw it
61 canvas.drawBitmap(src, SkIntToScalar(-10), 0, &paint);
62 REPORTER_ASSERT(reporter, 0 == *dst.getAddr32(5, 5));
63
64 // now install our looper, which will draw, since it internally translates
65 // to the left. The test is to ensure that canvas' quickReject machinary
66 // allows us through, even though sans-looper we would look like we should
67 // be clipped out.
68 paint.setLooper(new TestLooper)->unref();
69 canvas.drawBitmap(src, SkIntToScalar(-10), 0, &paint);
70 REPORTER_ASSERT(reporter, 0xFFFFFFFF == *dst.getAddr32(5, 5));
71}
72
73static void test(skiatest::Reporter* reporter) {
74 test_drawBitmap(reporter);
75}
76
77#include "TestClassDef.h"
78DEFINE_TESTCLASS("QuickReject", QuickRejectClass, test)