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.
|
|
|
|
*/
|
|
|
|
|
2017-02-08 20:12:19 +00:00
|
|
|
#include "SkArenaAlloc.h"
|
2017-07-06 18:16:18 +00:00
|
|
|
#include "SkBitmap.h"
|
2011-11-21 15:16:16 +00:00
|
|
|
#include "SkCanvas.h"
|
2017-03-30 16:41:48 +00:00
|
|
|
#include "SkColorSpaceXformer.h"
|
2011-11-21 15:16:16 +00:00
|
|
|
#include "SkDrawLooper.h"
|
2018-05-21 15:56:57 +00:00
|
|
|
#include "SkFlattenablePriv.h"
|
2016-08-19 15:38:36 +00:00
|
|
|
#include "SkLightingImageFilter.h"
|
2017-07-22 12:29:48 +00:00
|
|
|
#include "SkPoint3.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:
|
|
|
|
|
2017-02-08 20:12:19 +00:00
|
|
|
SkDrawLooper::Context* makeContext(SkCanvas*, SkArenaAlloc* alloc) const override {
|
|
|
|
return alloc->make<TestDrawLooperContext>();
|
2011-11-21 15:16:16 +00:00
|
|
|
}
|
|
|
|
|
2017-03-30 16:41:48 +00:00
|
|
|
sk_sp<SkDrawLooper> onMakeColorSpace(SkColorSpaceXformer*) const override {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2016-09-27 13:34:10 +00:00
|
|
|
SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(TestLooper)
|
2015-01-07 17:06:08 +00:00
|
|
|
|
2014-03-12 09:42:01 +00:00
|
|
|
private:
|
|
|
|
class TestDrawLooperContext : public SkDrawLooper::Context {
|
|
|
|
public:
|
|
|
|
TestDrawLooperContext() : fOnce(true) {}
|
2017-03-22 16:05:03 +00:00
|
|
|
~TestDrawLooperContext() override {}
|
2014-03-12 09:42:01 +00:00
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
bool next(SkCanvas* canvas, SkPaint*) override {
|
2014-03-12 09:42:01 +00:00
|
|
|
if (fOnce) {
|
|
|
|
fOnce = false;
|
|
|
|
canvas->translate(SkIntToScalar(10), 0);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2017-03-30 16:41:48 +00:00
|
|
|
|
2014-03-12 09:42:01 +00:00
|
|
|
private:
|
|
|
|
bool fOnce;
|
|
|
|
};
|
2011-11-21 15:16:16 +00:00
|
|
|
};
|
|
|
|
|
2016-04-03 16:11:13 +00:00
|
|
|
sk_sp<SkFlattenable> TestLooper::CreateProc(SkReadBuffer&) { return sk_make_sp<TestLooper>(); }
|
2015-01-07 17:06:08 +00:00
|
|
|
|
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.
|
2016-03-21 20:25:16 +00:00
|
|
|
paint.setLooper(sk_make_sp<TestLooper>());
|
2011-11-21 15:16:16 +00:00
|
|
|
canvas.drawBitmap(src, SkIntToScalar(-10), 0, &paint);
|
|
|
|
REPORTER_ASSERT(reporter, 0xFFFFFFFF == *dst.getAddr32(5, 5));
|
|
|
|
}
|
|
|
|
|
2015-03-11 15:47:12 +00:00
|
|
|
static void test_layers(skiatest::Reporter* reporter) {
|
|
|
|
SkCanvas canvas(100, 100);
|
|
|
|
|
|
|
|
SkRect r = SkRect::MakeWH(10, 10);
|
|
|
|
REPORTER_ASSERT(reporter, false == canvas.quickReject(r));
|
|
|
|
|
|
|
|
r.offset(300, 300);
|
|
|
|
REPORTER_ASSERT(reporter, true == canvas.quickReject(r));
|
|
|
|
|
|
|
|
// Test that saveLayer updates quickReject
|
|
|
|
SkRect bounds = SkRect::MakeLTRB(50, 50, 70, 70);
|
2015-08-27 14:41:13 +00:00
|
|
|
canvas.saveLayer(&bounds, nullptr);
|
2015-03-11 15:47:12 +00:00
|
|
|
REPORTER_ASSERT(reporter, true == canvas.quickReject(SkRect::MakeWH(10, 10)));
|
|
|
|
REPORTER_ASSERT(reporter, false == canvas.quickReject(SkRect::MakeWH(60, 60)));
|
|
|
|
}
|
|
|
|
|
2016-08-12 15:29:08 +00:00
|
|
|
static void test_quick_reject(skiatest::Reporter* reporter) {
|
|
|
|
SkCanvas canvas(100, 100);
|
|
|
|
SkRect r0 = SkRect::MakeLTRB(-50.0f, -50.0f, 50.0f, 50.0f);
|
|
|
|
SkRect r1 = SkRect::MakeLTRB(-50.0f, 110.0f, 50.0f, 120.0f);
|
|
|
|
SkRect r2 = SkRect::MakeLTRB(110.0f, -50.0f, 120.0f, 50.0f);
|
|
|
|
SkRect r3 = SkRect::MakeLTRB(-120.0f, -50.0f, 120.0f, 50.0f);
|
|
|
|
SkRect r4 = SkRect::MakeLTRB(-50.0f, -120.0f, 50.0f, 120.0f);
|
|
|
|
SkRect r5 = SkRect::MakeLTRB(-120.0f, -120.0f, 120.0f, 120.0f);
|
|
|
|
SkRect r6 = SkRect::MakeLTRB(-120.0f, -120.0f, -110.0f, -110.0f);
|
|
|
|
SkRect r7 = SkRect::MakeLTRB(SK_ScalarNaN, -50.0f, 50.0f, 50.0f);
|
|
|
|
SkRect r8 = SkRect::MakeLTRB(-50.0f, SK_ScalarNaN, 50.0f, 50.0f);
|
|
|
|
SkRect r9 = SkRect::MakeLTRB(-50.0f, -50.0f, SK_ScalarNaN, 50.0f);
|
|
|
|
SkRect r10 = SkRect::MakeLTRB(-50.0f, -50.0f, 50.0f, SK_ScalarNaN);
|
|
|
|
REPORTER_ASSERT(reporter, false == canvas.quickReject(r0));
|
|
|
|
REPORTER_ASSERT(reporter, true == canvas.quickReject(r1));
|
|
|
|
REPORTER_ASSERT(reporter, true == canvas.quickReject(r2));
|
|
|
|
REPORTER_ASSERT(reporter, false == canvas.quickReject(r3));
|
|
|
|
REPORTER_ASSERT(reporter, false == canvas.quickReject(r4));
|
|
|
|
REPORTER_ASSERT(reporter, false == canvas.quickReject(r5));
|
|
|
|
REPORTER_ASSERT(reporter, true == canvas.quickReject(r6));
|
|
|
|
REPORTER_ASSERT(reporter, true == canvas.quickReject(r7));
|
|
|
|
REPORTER_ASSERT(reporter, true == canvas.quickReject(r8));
|
|
|
|
REPORTER_ASSERT(reporter, true == canvas.quickReject(r9));
|
|
|
|
REPORTER_ASSERT(reporter, true == canvas.quickReject(r10));
|
|
|
|
|
|
|
|
SkMatrix m = SkMatrix::MakeScale(2.0f);
|
|
|
|
m.setTranslateX(10.0f);
|
|
|
|
m.setTranslateY(10.0f);
|
|
|
|
canvas.setMatrix(m);
|
|
|
|
SkRect r11 = SkRect::MakeLTRB(5.0f, 5.0f, 100.0f, 100.0f);
|
|
|
|
SkRect r12 = SkRect::MakeLTRB(5.0f, 50.0f, 100.0f, 100.0f);
|
|
|
|
SkRect r13 = SkRect::MakeLTRB(50.0f, 5.0f, 100.0f, 100.0f);
|
|
|
|
REPORTER_ASSERT(reporter, false == canvas.quickReject(r11));
|
|
|
|
REPORTER_ASSERT(reporter, true == canvas.quickReject(r12));
|
|
|
|
REPORTER_ASSERT(reporter, true == canvas.quickReject(r13));
|
|
|
|
}
|
|
|
|
|
2013-12-12 21:11:12 +00:00
|
|
|
DEF_TEST(QuickReject, reporter) {
|
2011-11-21 15:16:16 +00:00
|
|
|
test_drawBitmap(reporter);
|
2015-03-11 15:47:12 +00:00
|
|
|
test_layers(reporter);
|
2016-08-12 15:29:08 +00:00
|
|
|
test_quick_reject(reporter);
|
2011-11-21 15:16:16 +00:00
|
|
|
}
|
2016-08-19 15:38:36 +00:00
|
|
|
|
|
|
|
// Regression test to make sure that we keep fIsScaleTranslate up to date on the canvas.
|
|
|
|
// It is possible to set a new matrix on the canvas without calling setMatrix(). This tests
|
|
|
|
// that code path.
|
|
|
|
DEF_TEST(QuickReject_MatrixState, reporter) {
|
|
|
|
SkCanvas canvas(100, 100);
|
|
|
|
|
|
|
|
SkMatrix matrix;
|
|
|
|
matrix.setRotate(45.0f);
|
|
|
|
canvas.setMatrix(matrix);
|
|
|
|
|
|
|
|
SkPaint paint;
|
|
|
|
sk_sp<SkImageFilter> filter = SkLightingImageFilter::MakeDistantLitDiffuse(
|
|
|
|
SkPoint3::Make(1.0f, 1.0f, 1.0f), 0xFF0000FF, 2.0f, 0.5f, nullptr);
|
|
|
|
REPORTER_ASSERT(reporter, filter);
|
|
|
|
paint.setImageFilter(filter);
|
|
|
|
SkCanvas::SaveLayerRec rec;
|
|
|
|
rec.fPaint = &paint;
|
|
|
|
canvas.saveLayer(rec);
|
|
|
|
|
|
|
|
// quickReject() will assert if the matrix is out of sync.
|
|
|
|
canvas.quickReject(SkRect::MakeWH(100.0f, 100.0f));
|
|
|
|
}
|
2018-04-12 21:26:40 +00:00
|
|
|
|
|
|
|
#include "SkLayerDrawLooper.h"
|
|
|
|
#include "SkSurface.h"
|
|
|
|
DEF_TEST(looper_nothingtodraw, reporter) {
|
|
|
|
auto surf = SkSurface::MakeRasterN32Premul(20, 20);
|
|
|
|
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setColor(0);
|
|
|
|
REPORTER_ASSERT(reporter, paint.nothingToDraw());
|
|
|
|
|
|
|
|
SkLayerDrawLooper::Builder builder;
|
|
|
|
builder.addLayer();
|
|
|
|
paint.setDrawLooper(builder.detach());
|
|
|
|
// the presence of the looper fools this predicate, so we think it might draw
|
|
|
|
REPORTER_ASSERT(reporter, !paint.nothingToDraw());
|
|
|
|
|
|
|
|
// Before fixing, this would assert in ~AutoDrawLooper() in SkCanvas.cpp as it checked for
|
|
|
|
// a balance in the save/restore count after handling the looper. Before the fix, this
|
|
|
|
// code would call nothingToDraw() and since it now clears the looper, that predicate will
|
|
|
|
// return true, aborting the sequence prematurely, and not finishing the iterator on the looper
|
|
|
|
// which handles the final "restore". This was a bug -- we *must* call the looper's iterator
|
|
|
|
// until it returns done to keep the canvas balanced. The fix was to remove this early-exit
|
|
|
|
// in the autodrawlooper. Now this call won't assert.
|
|
|
|
// See https://skia-review.googlesource.com/c/skia/+/121220
|
|
|
|
surf->getCanvas()->drawRect({1, 1, 10, 10}, paint);
|
|
|
|
}
|