blob: d9e555d63b9d99d9518ef17998f86a476b6a5a26 [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
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +00008#include "Test.h"
9#include "TestClassDef.h"
reed@google.com3d608122011-11-21 15:16:16 +000010#include "SkCanvas.h"
11#include "SkDrawLooper.h"
reed@google.com3d608122011-11-21 15:16:16 +000012
13/*
14 * Subclass of looper that just draws once, with an offset in X.
15 */
16class TestLooper : public SkDrawLooper {
17public:
18 bool fOnce;
19
20 virtual void init(SkCanvas*) SK_OVERRIDE {
21 fOnce = true;
22 }
23
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
robertphillips@google.com4991b8f2013-01-28 20:21:59 +000033#ifdef SK_DEVELOPER
34 virtual void toString(SkString* str) const SK_OVERRIDE {
35 str->append("TestLooper:");
36 }
37#endif
38
djsollen@google.comba28d032012-03-26 17:57:35 +000039 SK_DECLARE_UNFLATTENABLE_OBJECT()
reed@google.com3d608122011-11-21 15:16:16 +000040};
41
42static 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();
junov@google.comdbfac8a2012-12-06 21:47:40 +000051 dst.eraseColor(SK_ColorTRANSPARENT);
reed@google.com3d608122011-11-21 15:16:16 +000052
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
junov@google.comdbfac8a2012-12-06 21:47:40 +000064 dst.eraseColor(SK_ColorTRANSPARENT);
reed@google.com3d608122011-11-21 15:16:16 +000065 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
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +000080DEF_TEST(QuickReject, reporter) {
reed@google.com3d608122011-11-21 15:16:16 +000081 test_drawBitmap(reporter);
82}