• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "Test.h"
9 #include "TestClassDef.h"
10 #include "SkCanvas.h"
11 #include "SkDrawLooper.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     bool fOnce;
19 
init(SkCanvas *)20     virtual void init(SkCanvas*) SK_OVERRIDE {
21         fOnce = true;
22     }
23 
next(SkCanvas * canvas,SkPaint *)24     virtual bool next(SkCanvas* canvas, SkPaint*) SK_OVERRIDE {
25         if (fOnce) {
26             fOnce = false;
27             canvas->translate(SkIntToScalar(10), 0);
28             return true;
29         }
30         return false;
31     }
32 
33 #ifdef SK_DEVELOPER
toString(SkString * str) const34     virtual void toString(SkString* str) const SK_OVERRIDE {
35         str->append("TestLooper:");
36     }
37 #endif
38 
39     SK_DECLARE_UNFLATTENABLE_OBJECT()
40 };
41 
test_drawBitmap(skiatest::Reporter * reporter)42 static void test_drawBitmap(skiatest::Reporter* reporter) {
43     SkBitmap src;
44     src.setConfig(SkBitmap::kARGB_8888_Config, 10, 10);
45     src.allocPixels();
46     src.eraseColor(SK_ColorWHITE);
47 
48     SkBitmap dst;
49     dst.setConfig(SkBitmap::kARGB_8888_Config, 10, 10);
50     dst.allocPixels();
51     dst.eraseColor(SK_ColorTRANSPARENT);
52 
53     SkCanvas canvas(dst);
54     SkPaint  paint;
55 
56     // we are initially transparent
57     REPORTER_ASSERT(reporter, 0 == *dst.getAddr32(5, 5));
58 
59     // we see the bitmap drawn
60     canvas.drawBitmap(src, 0, 0, &paint);
61     REPORTER_ASSERT(reporter, 0xFFFFFFFF == *dst.getAddr32(5, 5));
62 
63     // reverify we are clear again
64     dst.eraseColor(SK_ColorTRANSPARENT);
65     REPORTER_ASSERT(reporter, 0 == *dst.getAddr32(5, 5));
66 
67     // if the bitmap is clipped out, we don't draw it
68     canvas.drawBitmap(src, SkIntToScalar(-10), 0, &paint);
69     REPORTER_ASSERT(reporter, 0 == *dst.getAddr32(5, 5));
70 
71     // now install our looper, which will draw, since it internally translates
72     // to the left. The test is to ensure that canvas' quickReject machinary
73     // allows us through, even though sans-looper we would look like we should
74     // be clipped out.
75     paint.setLooper(new TestLooper)->unref();
76     canvas.drawBitmap(src, SkIntToScalar(-10), 0, &paint);
77     REPORTER_ASSERT(reporter, 0xFFFFFFFF == *dst.getAddr32(5, 5));
78 }
79 
DEF_TEST(QuickReject,reporter)80 DEF_TEST(QuickReject, reporter) {
81     test_drawBitmap(reporter);
82 }
83