blob: 8f4357e0b04fff392a702940735cf73664d2624f [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"
commit-bot@chromium.org79fbb402014-03-12 09:42:01 +000010#include "SkTypes.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000011#include "Test.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:
reed@google.com3d608122011-11-21 15:16:16 +000018
commit-bot@chromium.org79fbb402014-03-12 09:42:01 +000019 virtual SkDrawLooper::Context* createContext(SkCanvas*, void* storage) const SK_OVERRIDE {
20 return SkNEW_PLACEMENT(storage, TestDrawLooperContext);
reed@google.com3d608122011-11-21 15:16:16 +000021 }
22
commit-bot@chromium.org79fbb402014-03-12 09:42:01 +000023 virtual size_t contextSize() const SK_OVERRIDE { return sizeof(TestDrawLooperContext); }
reed@google.com3d608122011-11-21 15:16:16 +000024
commit-bot@chromium.org0f10f7b2014-03-13 18:02:17 +000025#ifndef SK_IGNORE_TO_STRING
robertphillips@google.com4991b8f2013-01-28 20:21:59 +000026 virtual void toString(SkString* str) const SK_OVERRIDE {
27 str->append("TestLooper:");
28 }
29#endif
30
commit-bot@chromium.org79fbb402014-03-12 09:42:01 +000031private:
32 class TestDrawLooperContext : public SkDrawLooper::Context {
33 public:
34 TestDrawLooperContext() : fOnce(true) {}
35 virtual ~TestDrawLooperContext() {}
36
37 virtual bool next(SkCanvas* canvas, SkPaint*) SK_OVERRIDE {
38 if (fOnce) {
39 fOnce = false;
40 canvas->translate(SkIntToScalar(10), 0);
41 return true;
42 }
43 return false;
44 }
45 private:
46 bool fOnce;
47 };
48
djsollen@google.comba28d032012-03-26 17:57:35 +000049 SK_DECLARE_UNFLATTENABLE_OBJECT()
reed@google.com3d608122011-11-21 15:16:16 +000050};
51
52static void test_drawBitmap(skiatest::Reporter* reporter) {
53 SkBitmap src;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +000054 src.allocN32Pixels(10, 10);
reed@google.com3d608122011-11-21 15:16:16 +000055 src.eraseColor(SK_ColorWHITE);
56
57 SkBitmap dst;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +000058 dst.allocN32Pixels(10, 10);
junov@google.comdbfac8a2012-12-06 21:47:40 +000059 dst.eraseColor(SK_ColorTRANSPARENT);
reed@google.com3d608122011-11-21 15:16:16 +000060
61 SkCanvas canvas(dst);
62 SkPaint paint;
63
64 // we are initially transparent
65 REPORTER_ASSERT(reporter, 0 == *dst.getAddr32(5, 5));
66
67 // we see the bitmap drawn
68 canvas.drawBitmap(src, 0, 0, &paint);
69 REPORTER_ASSERT(reporter, 0xFFFFFFFF == *dst.getAddr32(5, 5));
70
71 // reverify we are clear again
junov@google.comdbfac8a2012-12-06 21:47:40 +000072 dst.eraseColor(SK_ColorTRANSPARENT);
reed@google.com3d608122011-11-21 15:16:16 +000073 REPORTER_ASSERT(reporter, 0 == *dst.getAddr32(5, 5));
74
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));
78
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
82 // be clipped out.
83 paint.setLooper(new TestLooper)->unref();
84 canvas.drawBitmap(src, SkIntToScalar(-10), 0, &paint);
85 REPORTER_ASSERT(reporter, 0xFFFFFFFF == *dst.getAddr32(5, 5));
86}
87
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +000088DEF_TEST(QuickReject, reporter) {
reed@google.com3d608122011-11-21 15:16:16 +000089 test_drawBitmap(reporter);
90}