2011-11-21 15:16:16 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2011 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "SkCanvas.h"
|
|
|
|
#include "SkDrawLooper.h"
|
2014-03-12 09:42:01 +00:00
|
|
|
#include "SkTypes.h"
|
2014-01-24 20:56:26 +00:00
|
|
|
#include "Test.h"
|
2011-11-21 15:16:16 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Subclass of looper that just draws once, with an offset in X.
|
|
|
|
*/
|
|
|
|
class TestLooper : public SkDrawLooper {
|
|
|
|
public:
|
|
|
|
|
2014-03-12 09:42:01 +00:00
|
|
|
virtual SkDrawLooper::Context* createContext(SkCanvas*, void* storage) const SK_OVERRIDE {
|
|
|
|
return SkNEW_PLACEMENT(storage, TestDrawLooperContext);
|
2011-11-21 15:16:16 +00:00
|
|
|
}
|
|
|
|
|
2014-03-12 09:42:01 +00:00
|
|
|
virtual size_t contextSize() const SK_OVERRIDE { return sizeof(TestDrawLooperContext); }
|
2011-11-21 15:16:16 +00:00
|
|
|
|
2014-03-13 18:02:17 +00:00
|
|
|
#ifndef SK_IGNORE_TO_STRING
|
2013-01-28 20:21:59 +00:00
|
|
|
virtual void toString(SkString* str) const SK_OVERRIDE {
|
|
|
|
str->append("TestLooper:");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-01-07 17:06:08 +00:00
|
|
|
SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(TestLooper);
|
|
|
|
|
2014-03-12 09:42:01 +00:00
|
|
|
private:
|
|
|
|
class TestDrawLooperContext : public SkDrawLooper::Context {
|
|
|
|
public:
|
|
|
|
TestDrawLooperContext() : fOnce(true) {}
|
|
|
|
virtual ~TestDrawLooperContext() {}
|
|
|
|
|
|
|
|
virtual bool next(SkCanvas* canvas, SkPaint*) SK_OVERRIDE {
|
|
|
|
if (fOnce) {
|
|
|
|
fOnce = false;
|
|
|
|
canvas->translate(SkIntToScalar(10), 0);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
bool fOnce;
|
|
|
|
};
|
2011-11-21 15:16:16 +00:00
|
|
|
};
|
|
|
|
|
2015-01-07 17:06:08 +00:00
|
|
|
SkFlattenable* TestLooper::CreateProc(SkReadBuffer&) { return SkNEW(TestLooper); }
|
|
|
|
|
2011-11-21 15:16:16 +00:00
|
|
|
static void test_drawBitmap(skiatest::Reporter* reporter) {
|
|
|
|
SkBitmap src;
|
2014-02-13 14:41:43 +00:00
|
|
|
src.allocN32Pixels(10, 10);
|
2011-11-21 15:16:16 +00:00
|
|
|
src.eraseColor(SK_ColorWHITE);
|
|
|
|
|
|
|
|
SkBitmap dst;
|
2014-02-13 14:41:43 +00:00
|
|
|
dst.allocN32Pixels(10, 10);
|
2012-12-06 21:47:40 +00:00
|
|
|
dst.eraseColor(SK_ColorTRANSPARENT);
|
2011-11-21 15:16:16 +00:00
|
|
|
|
|
|
|
SkCanvas canvas(dst);
|
|
|
|
SkPaint paint;
|
|
|
|
|
|
|
|
// we are initially transparent
|
|
|
|
REPORTER_ASSERT(reporter, 0 == *dst.getAddr32(5, 5));
|
|
|
|
|
|
|
|
// we see the bitmap drawn
|
|
|
|
canvas.drawBitmap(src, 0, 0, &paint);
|
|
|
|
REPORTER_ASSERT(reporter, 0xFFFFFFFF == *dst.getAddr32(5, 5));
|
|
|
|
|
|
|
|
// reverify we are clear again
|
2012-12-06 21:47:40 +00:00
|
|
|
dst.eraseColor(SK_ColorTRANSPARENT);
|
2011-11-21 15:16:16 +00:00
|
|
|
REPORTER_ASSERT(reporter, 0 == *dst.getAddr32(5, 5));
|
|
|
|
|
|
|
|
// if the bitmap is clipped out, we don't draw it
|
|
|
|
canvas.drawBitmap(src, SkIntToScalar(-10), 0, &paint);
|
|
|
|
REPORTER_ASSERT(reporter, 0 == *dst.getAddr32(5, 5));
|
|
|
|
|
|
|
|
// now install our looper, which will draw, since it internally translates
|
|
|
|
// to the left. The test is to ensure that canvas' quickReject machinary
|
|
|
|
// allows us through, even though sans-looper we would look like we should
|
|
|
|
// be clipped out.
|
|
|
|
paint.setLooper(new TestLooper)->unref();
|
|
|
|
canvas.drawBitmap(src, SkIntToScalar(-10), 0, &paint);
|
|
|
|
REPORTER_ASSERT(reporter, 0xFFFFFFFF == *dst.getAddr32(5, 5));
|
|
|
|
}
|
|
|
|
|
2013-12-12 21:11:12 +00:00
|
|
|
DEF_TEST(QuickReject, reporter) {
|
2011-11-21 15:16:16 +00:00
|
|
|
test_drawBitmap(reporter);
|
|
|
|
}
|