2011-11-28 16:06:04 +00:00
|
|
|
/*
|
2012-02-22 21:00:42 +00:00
|
|
|
* Copyright 2012 Google Inc.
|
2011-11-28 16:06:04 +00:00
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
2012-02-22 21:00:42 +00:00
|
|
|
|
|
|
|
/* Description:
|
|
|
|
* This test defines a series of elementatry test steps that perform
|
|
|
|
* a single or a small group of canvas API calls. Each test step is
|
|
|
|
* used in several test cases that verify that different types of SkCanvas
|
|
|
|
* flavors and derivatives pass it and yield consistent behavior. The
|
|
|
|
* test cases analyse results that are queryable through the API. They do
|
|
|
|
* not look at rendering results.
|
|
|
|
*
|
|
|
|
* Adding test stepss:
|
|
|
|
* The general pattern for creating a new test step is to write a test
|
|
|
|
* function of the form:
|
|
|
|
*
|
2012-08-23 18:14:13 +00:00
|
|
|
* static void MyTestStepFunction(SkCanvas* canvas,
|
2016-05-18 14:24:16 +00:00
|
|
|
* const TestData& d,
|
2012-02-22 21:00:42 +00:00
|
|
|
* skiatest::Reporter* reporter,
|
|
|
|
* CanvasTestStep* testStep)
|
|
|
|
* {
|
|
|
|
* canvas->someCanvasAPImethod();
|
|
|
|
* (...)
|
2018-01-29 14:50:47 +00:00
|
|
|
* REPORTER_ASSERT(reporter, (...), \
|
2012-02-22 21:00:42 +00:00
|
|
|
* testStep->assertMessage());
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* The definition of the test step function should be followed by an
|
|
|
|
* invocation of the TEST_STEP macro, which generates a class and
|
|
|
|
* instance for the test step:
|
|
|
|
*
|
|
|
|
* TEST_STEP(MyTestStep, MyTestStepFunction)
|
|
|
|
*
|
|
|
|
* There are also short hand macros for defining simple test steps
|
|
|
|
* in a single line of code. A simple test step is a one that is made
|
|
|
|
* of a single canvas API call.
|
|
|
|
*
|
|
|
|
* SIMPLE_TEST_STEP(MytestStep, someCanvasAPIMethod());
|
|
|
|
*
|
|
|
|
* There is another macro called SIMPLE_TEST_STEP_WITH_ASSERT that
|
|
|
|
* works the same way as SIMPLE_TEST_STEP, and additionally verifies
|
|
|
|
* that the invoked method returns a non-zero value.
|
|
|
|
*/
|
2017-03-22 14:01:53 +00:00
|
|
|
|
2011-11-28 16:06:04 +00:00
|
|
|
#include "SkBitmap.h"
|
2018-03-12 17:46:21 +00:00
|
|
|
#include "SkBlendMode.h"
|
2011-11-28 16:06:04 +00:00
|
|
|
#include "SkCanvas.h"
|
2018-03-12 17:46:21 +00:00
|
|
|
#include "SkCanvasStack.h"
|
|
|
|
#include "SkClipOp.h"
|
|
|
|
#include "SkClipOpPriv.h"
|
|
|
|
#include "SkColor.h"
|
|
|
|
#include "SkImageFilter.h"
|
|
|
|
#include "SkImageInfo.h"
|
|
|
|
#include "SkMalloc.h"
|
2012-02-22 21:00:42 +00:00
|
|
|
#include "SkMatrix.h"
|
|
|
|
#include "SkNWayCanvas.h"
|
2018-09-07 18:33:14 +00:00
|
|
|
#include "SkPDFDocument.h"
|
2012-02-22 21:00:42 +00:00
|
|
|
#include "SkPaint.h"
|
2015-08-17 15:05:13 +00:00
|
|
|
#include "SkPaintFilterCanvas.h"
|
2012-02-22 21:00:42 +00:00
|
|
|
#include "SkPath.h"
|
2014-04-18 18:04:41 +00:00
|
|
|
#include "SkPictureRecorder.h"
|
2018-03-12 17:46:21 +00:00
|
|
|
#include "SkPixmap.h"
|
|
|
|
#include "SkPoint.h"
|
2012-02-22 21:00:42 +00:00
|
|
|
#include "SkRect.h"
|
2018-03-12 17:46:21 +00:00
|
|
|
#include "SkRefCnt.h"
|
2012-02-22 21:00:42 +00:00
|
|
|
#include "SkRegion.h"
|
2018-03-12 17:46:21 +00:00
|
|
|
#include "SkScalar.h"
|
2012-02-22 21:00:42 +00:00
|
|
|
#include "SkShader.h"
|
2018-03-12 17:46:21 +00:00
|
|
|
#include "SkSize.h"
|
2018-01-22 17:51:13 +00:00
|
|
|
#include "SkSpecialImage.h"
|
2012-02-22 21:00:42 +00:00
|
|
|
#include "SkStream.h"
|
2018-03-12 17:46:21 +00:00
|
|
|
#include "SkString.h"
|
2014-02-04 15:34:10 +00:00
|
|
|
#include "SkSurface.h"
|
2012-02-22 21:00:42 +00:00
|
|
|
#include "SkTDArray.h"
|
2018-01-22 17:51:13 +00:00
|
|
|
#include "SkTemplates.h"
|
2018-03-12 17:46:21 +00:00
|
|
|
#include "SkTypes.h"
|
2017-04-03 15:11:09 +00:00
|
|
|
#include "SkVertices.h"
|
2012-02-22 21:00:42 +00:00
|
|
|
#include "Test.h"
|
2011-11-28 16:06:04 +00:00
|
|
|
|
2018-03-12 17:46:21 +00:00
|
|
|
#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
|
|
|
|
#include "SkColorData.h"
|
|
|
|
#include "SkColorSpace.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
class SkColorSpaceXformer;
|
|
|
|
class SkReadBuffer;
|
|
|
|
template <typename T> class SkTCopyOnFirstWrite;
|
|
|
|
|
2017-01-23 16:39:45 +00:00
|
|
|
DEF_TEST(canvas_clipbounds, reporter) {
|
|
|
|
SkCanvas canvas(10, 10);
|
2017-01-24 14:13:40 +00:00
|
|
|
SkIRect irect, irect2;
|
|
|
|
SkRect rect, rect2;
|
2017-01-23 16:39:45 +00:00
|
|
|
|
|
|
|
irect = canvas.getDeviceClipBounds();
|
|
|
|
REPORTER_ASSERT(reporter, irect == SkIRect::MakeWH(10, 10));
|
2017-01-24 14:13:40 +00:00
|
|
|
REPORTER_ASSERT(reporter, canvas.getDeviceClipBounds(&irect2));
|
|
|
|
REPORTER_ASSERT(reporter, irect == irect2);
|
|
|
|
|
2017-01-23 16:39:45 +00:00
|
|
|
// local bounds are always too big today -- can we trim them?
|
|
|
|
rect = canvas.getLocalClipBounds();
|
|
|
|
REPORTER_ASSERT(reporter, rect.contains(SkRect::MakeWH(10, 10)));
|
2017-01-24 14:13:40 +00:00
|
|
|
REPORTER_ASSERT(reporter, canvas.getLocalClipBounds(&rect2));
|
|
|
|
REPORTER_ASSERT(reporter, rect == rect2);
|
2017-01-23 16:39:45 +00:00
|
|
|
|
|
|
|
canvas.clipRect(SkRect::MakeEmpty());
|
|
|
|
|
|
|
|
irect = canvas.getDeviceClipBounds();
|
|
|
|
REPORTER_ASSERT(reporter, irect == SkIRect::MakeEmpty());
|
2017-01-24 14:13:40 +00:00
|
|
|
REPORTER_ASSERT(reporter, !canvas.getDeviceClipBounds(&irect2));
|
|
|
|
REPORTER_ASSERT(reporter, irect == irect2);
|
|
|
|
|
2017-01-23 16:39:45 +00:00
|
|
|
rect = canvas.getLocalClipBounds();
|
|
|
|
REPORTER_ASSERT(reporter, rect == SkRect::MakeEmpty());
|
2017-01-24 14:13:40 +00:00
|
|
|
REPORTER_ASSERT(reporter, !canvas.getLocalClipBounds(&rect2));
|
|
|
|
REPORTER_ASSERT(reporter, rect == rect2);
|
2017-03-10 15:49:45 +00:00
|
|
|
|
|
|
|
// Test for wacky sizes that we (historically) have guarded against
|
|
|
|
{
|
|
|
|
SkCanvas c(-10, -20);
|
|
|
|
REPORTER_ASSERT(reporter, c.getBaseLayerSize() == SkISize::MakeEmpty());
|
2017-03-10 18:54:16 +00:00
|
|
|
|
|
|
|
SkPictureRecorder().beginRecording({ 5, 5, 4, 4 });
|
2017-03-10 15:49:45 +00:00
|
|
|
}
|
2017-01-23 16:39:45 +00:00
|
|
|
}
|
|
|
|
|
2017-09-13 19:25:47 +00:00
|
|
|
// Will call proc with multiple styles of canvas (recording, raster, pdf)
|
2017-03-23 16:34:15 +00:00
|
|
|
template <typename F> static void multi_canvas_driver(int w, int h, F proc) {
|
|
|
|
proc(SkPictureRecorder().beginRecording(SkRect::MakeIWH(w, h)));
|
|
|
|
|
|
|
|
SkNullWStream stream;
|
2018-09-07 18:33:14 +00:00
|
|
|
if (auto doc = SkPDF::MakeDocument(&stream)) {
|
2018-03-20 18:17:00 +00:00
|
|
|
proc(doc->beginPage(SkIntToScalar(w), SkIntToScalar(h)));
|
|
|
|
}
|
2017-03-23 16:34:15 +00:00
|
|
|
|
|
|
|
proc(SkSurface::MakeRasterN32Premul(w, h, nullptr)->getCanvas());
|
|
|
|
}
|
|
|
|
|
2017-03-22 14:01:53 +00:00
|
|
|
const SkIRect gBaseRestrictedR = { 0, 0, 10, 10 };
|
|
|
|
|
|
|
|
static void test_restriction(skiatest::Reporter* reporter, SkCanvas* canvas) {
|
|
|
|
REPORTER_ASSERT(reporter, canvas->getDeviceClipBounds() == gBaseRestrictedR);
|
|
|
|
|
|
|
|
const SkIRect restrictionR = { 2, 2, 8, 8 };
|
|
|
|
canvas->androidFramework_setDeviceClipRestriction(restrictionR);
|
|
|
|
REPORTER_ASSERT(reporter, canvas->getDeviceClipBounds() == restrictionR);
|
|
|
|
|
|
|
|
const SkIRect clipR = { 4, 4, 6, 6 };
|
|
|
|
canvas->clipRect(SkRect::Make(clipR), SkClipOp::kIntersect);
|
|
|
|
REPORTER_ASSERT(reporter, canvas->getDeviceClipBounds() == clipR);
|
|
|
|
|
2017-05-10 18:13:20 +00:00
|
|
|
#ifdef SK_SUPPORT_DEPRECATED_CLIPOPS
|
2017-03-22 14:01:53 +00:00
|
|
|
// now test that expanding clipops can't exceed the restriction
|
|
|
|
const SkClipOp expanders[] = {
|
|
|
|
SkClipOp::kUnion_deprecated,
|
|
|
|
SkClipOp::kXOR_deprecated,
|
|
|
|
SkClipOp::kReverseDifference_deprecated,
|
|
|
|
SkClipOp::kReplace_deprecated,
|
|
|
|
};
|
|
|
|
|
|
|
|
const SkRect expandR = { 0, 0, 5, 9 };
|
|
|
|
SkASSERT(!SkRect::Make(restrictionR).contains(expandR));
|
|
|
|
|
|
|
|
for (SkClipOp op : expanders) {
|
|
|
|
canvas->save();
|
|
|
|
canvas->clipRect(expandR, op);
|
|
|
|
REPORTER_ASSERT(reporter, gBaseRestrictedR.contains(canvas->getDeviceClipBounds()));
|
|
|
|
canvas->restore();
|
|
|
|
}
|
2017-05-10 18:13:20 +00:00
|
|
|
#endif
|
2017-03-22 14:01:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clip restriction logic exists in the canvas itself, and in various kinds of devices.
|
|
|
|
*
|
|
|
|
* This test explicitly tries to exercise that variety:
|
|
|
|
* - picture : empty device but exercises canvas itself
|
|
|
|
* - pdf : uses SkClipStack in its device (as does SVG and GPU)
|
|
|
|
* - raster : uses SkRasterClip in its device
|
|
|
|
*/
|
|
|
|
DEF_TEST(canvas_clip_restriction, reporter) {
|
2017-03-23 16:34:15 +00:00
|
|
|
multi_canvas_driver(gBaseRestrictedR.width(), gBaseRestrictedR.height(),
|
|
|
|
[reporter](SkCanvas* canvas) { test_restriction(reporter, canvas); });
|
|
|
|
}
|
2017-03-22 14:01:53 +00:00
|
|
|
|
2017-03-23 16:34:15 +00:00
|
|
|
DEF_TEST(canvas_empty_clip, reporter) {
|
|
|
|
multi_canvas_driver(50, 50, [reporter](SkCanvas* canvas) {
|
|
|
|
canvas->save();
|
|
|
|
canvas->clipRect({0, 0, 20, 40 });
|
|
|
|
REPORTER_ASSERT(reporter, !canvas->isClipEmpty());
|
|
|
|
canvas->clipRect({30, 0, 50, 40 });
|
|
|
|
REPORTER_ASSERT(reporter, canvas->isClipEmpty());
|
|
|
|
});
|
2017-03-22 14:01:53 +00:00
|
|
|
}
|
|
|
|
|
2014-10-03 20:26:55 +00:00
|
|
|
static const int kWidth = 2, kHeight = 2;
|
|
|
|
|
|
|
|
static void createBitmap(SkBitmap* bm, SkColor color) {
|
|
|
|
bm->allocN32Pixels(kWidth, kHeight);
|
|
|
|
bm->eraseColor(color);
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Constants used by test steps
|
|
|
|
const SkPoint kTestPoints[] = {
|
|
|
|
{SkIntToScalar(0), SkIntToScalar(0)},
|
|
|
|
{SkIntToScalar(2), SkIntToScalar(1)},
|
|
|
|
{SkIntToScalar(0), SkIntToScalar(2)}
|
|
|
|
};
|
|
|
|
const SkPoint kTestPoints2[] = {
|
|
|
|
{ SkIntToScalar(0), SkIntToScalar(1) },
|
|
|
|
{ SkIntToScalar(1), SkIntToScalar(1) },
|
|
|
|
{ SkIntToScalar(2), SkIntToScalar(1) },
|
|
|
|
{ SkIntToScalar(3), SkIntToScalar(1) },
|
|
|
|
{ SkIntToScalar(4), SkIntToScalar(1) },
|
|
|
|
{ SkIntToScalar(5), SkIntToScalar(1) },
|
|
|
|
{ SkIntToScalar(6), SkIntToScalar(1) },
|
|
|
|
{ SkIntToScalar(7), SkIntToScalar(1) },
|
|
|
|
{ SkIntToScalar(8), SkIntToScalar(1) },
|
|
|
|
{ SkIntToScalar(9), SkIntToScalar(1) },
|
|
|
|
{ SkIntToScalar(10), SkIntToScalar(1) }
|
|
|
|
};
|
|
|
|
|
|
|
|
struct TestData {
|
|
|
|
public:
|
|
|
|
TestData()
|
|
|
|
: fRect(SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0),
|
|
|
|
SkIntToScalar(2), SkIntToScalar(1)))
|
|
|
|
, fMatrix(TestMatrix())
|
|
|
|
, fPath(TestPath())
|
|
|
|
, fNearlyZeroLengthPath(TestNearlyZeroLengthPath())
|
|
|
|
, fIRect(SkIRect::MakeXYWH(0, 0, 2, 1))
|
|
|
|
, fRegion(TestRegion())
|
|
|
|
, fColor(0x01020304)
|
|
|
|
, fPoints(kTestPoints)
|
|
|
|
, fPointCount(3)
|
|
|
|
, fWidth(2)
|
|
|
|
, fHeight(2)
|
|
|
|
, fText("Hello World")
|
|
|
|
, fPoints2(kTestPoints2)
|
|
|
|
, fBitmap(TestBitmap())
|
|
|
|
{ }
|
|
|
|
|
|
|
|
SkRect fRect;
|
2015-04-27 14:01:44 +00:00
|
|
|
SkMatrix fMatrix;
|
2014-10-03 20:26:55 +00:00
|
|
|
SkPath fPath;
|
|
|
|
SkPath fNearlyZeroLengthPath;
|
|
|
|
SkIRect fIRect;
|
|
|
|
SkRegion fRegion;
|
|
|
|
SkColor fColor;
|
|
|
|
SkPaint fPaint;
|
|
|
|
const SkPoint* fPoints;
|
|
|
|
size_t fPointCount;
|
|
|
|
int fWidth;
|
|
|
|
int fHeight;
|
|
|
|
SkString fText;
|
|
|
|
const SkPoint* fPoints2;
|
|
|
|
SkBitmap fBitmap;
|
|
|
|
|
|
|
|
private:
|
|
|
|
static SkMatrix TestMatrix() {
|
|
|
|
SkMatrix matrix;
|
|
|
|
matrix.reset();
|
|
|
|
matrix.setScale(SkIntToScalar(2), SkIntToScalar(3));
|
|
|
|
|
|
|
|
return matrix;
|
|
|
|
}
|
|
|
|
static SkPath TestPath() {
|
|
|
|
SkPath path;
|
|
|
|
path.addRect(SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0),
|
|
|
|
SkIntToScalar(2), SkIntToScalar(1)));
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
static SkPath TestNearlyZeroLengthPath() {
|
|
|
|
SkPath path;
|
|
|
|
SkPoint pt1 = { 0, 0 };
|
|
|
|
SkPoint pt2 = { 0, SK_ScalarNearlyZero };
|
|
|
|
SkPoint pt3 = { SkIntToScalar(1), 0 };
|
|
|
|
SkPoint pt4 = { SkIntToScalar(1), SK_ScalarNearlyZero/2 };
|
|
|
|
path.moveTo(pt1);
|
|
|
|
path.lineTo(pt2);
|
|
|
|
path.lineTo(pt3);
|
|
|
|
path.lineTo(pt4);
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
static SkRegion TestRegion() {
|
|
|
|
SkRegion region;
|
|
|
|
SkIRect rect = SkIRect::MakeXYWH(0, 0, 2, 1);
|
|
|
|
region.setRect(rect);
|
|
|
|
return region;
|
|
|
|
}
|
|
|
|
static SkBitmap TestBitmap() {
|
|
|
|
SkBitmap bitmap;
|
|
|
|
createBitmap(&bitmap, 0x05060708);
|
|
|
|
return bitmap;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-02-22 21:00:42 +00:00
|
|
|
// Format strings that describe the test context. The %s token is where
|
|
|
|
// the name of the test step is inserted. The context is required for
|
|
|
|
// disambiguating the error in the case of failures that are reported in
|
|
|
|
// functions that are called multiple times in different contexts (test
|
|
|
|
// cases and test steps).
|
|
|
|
static const char* const kDefaultAssertMessageFormat = "%s";
|
2012-08-23 18:14:13 +00:00
|
|
|
static const char* const kCanvasDrawAssertMessageFormat =
|
2012-02-22 21:00:42 +00:00
|
|
|
"Drawing test step %s with SkCanvas";
|
2012-10-18 15:58:23 +00:00
|
|
|
static const char* const kPdfAssertMessageFormat =
|
|
|
|
"PDF sanity check failed %s";
|
2012-02-22 21:00:42 +00:00
|
|
|
|
|
|
|
class CanvasTestStep;
|
|
|
|
static SkTDArray<CanvasTestStep*>& testStepArray() {
|
|
|
|
static SkTDArray<CanvasTestStep*> theTests;
|
|
|
|
return theTests;
|
|
|
|
}
|
|
|
|
|
|
|
|
class CanvasTestStep {
|
|
|
|
public:
|
2012-10-18 15:58:23 +00:00
|
|
|
CanvasTestStep(bool fEnablePdfTesting = true) {
|
2012-02-22 21:00:42 +00:00
|
|
|
*testStepArray().append() = this;
|
|
|
|
fAssertMessageFormat = kDefaultAssertMessageFormat;
|
2012-10-18 15:58:23 +00:00
|
|
|
this->fEnablePdfTesting = fEnablePdfTesting;
|
2012-02-22 21:00:42 +00:00
|
|
|
}
|
2012-03-21 15:39:03 +00:00
|
|
|
virtual ~CanvasTestStep() { }
|
2012-02-22 21:00:42 +00:00
|
|
|
|
2014-10-03 20:26:55 +00:00
|
|
|
virtual void draw(SkCanvas*, const TestData&, skiatest::Reporter*) = 0;
|
2012-02-22 21:00:42 +00:00
|
|
|
virtual const char* name() const = 0;
|
|
|
|
|
|
|
|
const char* assertMessage() {
|
|
|
|
fAssertMessage.printf(fAssertMessageFormat, name());
|
|
|
|
return fAssertMessage.c_str();
|
|
|
|
}
|
|
|
|
|
|
|
|
void setAssertMessageFormat(const char* format) {
|
|
|
|
fAssertMessageFormat = format;
|
|
|
|
}
|
|
|
|
|
2012-10-18 15:58:23 +00:00
|
|
|
bool enablePdfTesting() { return fEnablePdfTesting; }
|
|
|
|
|
2012-02-22 21:00:42 +00:00
|
|
|
private:
|
|
|
|
SkString fAssertMessage;
|
|
|
|
const char* fAssertMessageFormat;
|
2012-10-18 15:58:23 +00:00
|
|
|
bool fEnablePdfTesting;
|
2012-02-22 21:00:42 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Macros for defining test steps
|
|
|
|
|
|
|
|
#define TEST_STEP(NAME, FUNCTION) \
|
|
|
|
class NAME##_TestStep : public CanvasTestStep{ \
|
|
|
|
public: \
|
2014-10-03 20:26:55 +00:00
|
|
|
virtual void draw(SkCanvas* canvas, const TestData& d, \
|
|
|
|
skiatest::Reporter* reporter) { \
|
|
|
|
FUNCTION (canvas, d, reporter, this); \
|
2012-02-22 21:00:42 +00:00
|
|
|
} \
|
|
|
|
virtual const char* name() const {return #NAME ;} \
|
|
|
|
}; \
|
|
|
|
static NAME##_TestStep NAME##_TestStepInstance;
|
|
|
|
|
2014-10-03 20:26:55 +00:00
|
|
|
#define TEST_STEP_NO_PDF(NAME, FUNCTION) \
|
2012-10-18 15:58:23 +00:00
|
|
|
class NAME##_TestStep : public CanvasTestStep{ \
|
|
|
|
public: \
|
|
|
|
NAME##_TestStep() : CanvasTestStep(false) {} \
|
2014-10-03 20:26:55 +00:00
|
|
|
virtual void draw(SkCanvas* canvas, const TestData& d, \
|
|
|
|
skiatest::Reporter* reporter) { \
|
|
|
|
FUNCTION (canvas, d, reporter, this); \
|
2012-10-18 15:58:23 +00:00
|
|
|
} \
|
|
|
|
virtual const char* name() const {return #NAME ;} \
|
|
|
|
}; \
|
|
|
|
static NAME##_TestStep NAME##_TestStepInstance;
|
|
|
|
|
2014-10-03 20:26:55 +00:00
|
|
|
#define SIMPLE_TEST_STEP(NAME, CALL) \
|
|
|
|
static void NAME##TestStep(SkCanvas* canvas, const TestData& d, \
|
|
|
|
skiatest::Reporter*, CanvasTestStep*) { \
|
|
|
|
canvas-> CALL ; \
|
|
|
|
} \
|
2012-02-22 21:00:42 +00:00
|
|
|
TEST_STEP(NAME, NAME##TestStep )
|
|
|
|
|
2018-01-29 14:50:47 +00:00
|
|
|
#define SIMPLE_TEST_STEP_WITH_ASSERT(NAME, CALL) \
|
|
|
|
static void NAME##TestStep(SkCanvas* canvas, const TestData& d, skiatest::Reporter*, \
|
|
|
|
CanvasTestStep* testStep) { \
|
|
|
|
REPORTER_ASSERT(reporter, canvas->CALL, testStep->assertMessage()); \
|
|
|
|
} \
|
|
|
|
TEST_STEP(NAME, NAME##TestStep)
|
2012-02-22 21:00:42 +00:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2012-08-23 18:14:13 +00:00
|
|
|
// Basic test steps for most virtual methods in SkCanvas that draw or affect
|
2012-02-22 21:00:42 +00:00
|
|
|
// the state of the canvas.
|
|
|
|
|
2014-03-18 12:51:48 +00:00
|
|
|
SIMPLE_TEST_STEP(Translate, translate(SkIntToScalar(1), SkIntToScalar(2)));
|
|
|
|
SIMPLE_TEST_STEP(Scale, scale(SkIntToScalar(1), SkIntToScalar(2)));
|
|
|
|
SIMPLE_TEST_STEP(Rotate, rotate(SkIntToScalar(1)));
|
|
|
|
SIMPLE_TEST_STEP(Skew, skew(SkIntToScalar(1), SkIntToScalar(2)));
|
2014-10-03 20:26:55 +00:00
|
|
|
SIMPLE_TEST_STEP(Concat, concat(d.fMatrix));
|
|
|
|
SIMPLE_TEST_STEP(SetMatrix, setMatrix(d.fMatrix));
|
|
|
|
SIMPLE_TEST_STEP(ClipRect, clipRect(d.fRect));
|
|
|
|
SIMPLE_TEST_STEP(ClipPath, clipPath(d.fPath));
|
2016-12-09 14:00:50 +00:00
|
|
|
SIMPLE_TEST_STEP(ClipRegion, clipRegion(d.fRegion, kReplace_SkClipOp));
|
2014-10-03 20:26:55 +00:00
|
|
|
SIMPLE_TEST_STEP(Clear, clear(d.fColor));
|
2012-02-22 21:00:42 +00:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Complex test steps
|
|
|
|
|
2014-10-03 20:26:55 +00:00
|
|
|
static void SaveMatrixClipStep(SkCanvas* canvas, const TestData& d,
|
|
|
|
skiatest::Reporter* reporter, CanvasTestStep* testStep) {
|
2012-07-16 14:04:59 +00:00
|
|
|
int saveCount = canvas->getSaveCount();
|
2014-06-30 14:13:28 +00:00
|
|
|
canvas->save();
|
2012-07-16 14:04:59 +00:00
|
|
|
canvas->translate(SkIntToScalar(1), SkIntToScalar(2));
|
2014-10-03 20:26:55 +00:00
|
|
|
canvas->clipRegion(d.fRegion);
|
2012-07-16 14:04:59 +00:00
|
|
|
canvas->restore();
|
2018-01-29 14:50:47 +00:00
|
|
|
REPORTER_ASSERT(reporter, canvas->getSaveCount() == saveCount, testStep->assertMessage());
|
|
|
|
REPORTER_ASSERT(reporter, canvas->getTotalMatrix().isIdentity(), testStep->assertMessage());
|
|
|
|
// REPORTER_ASSERT(reporter, canvas->getTotalClip() != kTestRegion,
|
|
|
|
// testStep->assertMessage());
|
2012-07-16 14:04:59 +00:00
|
|
|
}
|
|
|
|
TEST_STEP(SaveMatrixClip, SaveMatrixClipStep);
|
|
|
|
|
2014-10-03 20:26:55 +00:00
|
|
|
static void SaveLayerStep(SkCanvas* canvas, const TestData& d,
|
|
|
|
skiatest::Reporter* reporter, CanvasTestStep* testStep) {
|
2012-07-16 14:04:59 +00:00
|
|
|
int saveCount = canvas->getSaveCount();
|
2015-08-27 14:41:13 +00:00
|
|
|
canvas->saveLayer(nullptr, nullptr);
|
2012-07-16 14:04:59 +00:00
|
|
|
canvas->restore();
|
2018-01-29 14:50:47 +00:00
|
|
|
REPORTER_ASSERT(reporter, canvas->getSaveCount() == saveCount, testStep->assertMessage());
|
2012-07-16 14:04:59 +00:00
|
|
|
}
|
|
|
|
TEST_STEP(SaveLayer, SaveLayerStep);
|
|
|
|
|
2014-10-03 20:26:55 +00:00
|
|
|
static void BoundedSaveLayerStep(SkCanvas* canvas, const TestData& d,
|
|
|
|
skiatest::Reporter* reporter, CanvasTestStep* testStep) {
|
2012-07-16 14:04:59 +00:00
|
|
|
int saveCount = canvas->getSaveCount();
|
2015-08-27 14:41:13 +00:00
|
|
|
canvas->saveLayer(&d.fRect, nullptr);
|
2012-07-16 14:04:59 +00:00
|
|
|
canvas->restore();
|
2018-01-29 14:50:47 +00:00
|
|
|
REPORTER_ASSERT(reporter, canvas->getSaveCount() == saveCount, testStep->assertMessage());
|
2012-07-16 14:04:59 +00:00
|
|
|
}
|
|
|
|
TEST_STEP(BoundedSaveLayer, BoundedSaveLayerStep);
|
|
|
|
|
2014-10-03 20:26:55 +00:00
|
|
|
static void PaintSaveLayerStep(SkCanvas* canvas, const TestData& d,
|
|
|
|
skiatest::Reporter* reporter, CanvasTestStep* testStep) {
|
2012-07-16 14:04:59 +00:00
|
|
|
int saveCount = canvas->getSaveCount();
|
2015-08-27 14:41:13 +00:00
|
|
|
canvas->saveLayer(nullptr, &d.fPaint);
|
2012-07-16 14:04:59 +00:00
|
|
|
canvas->restore();
|
2018-01-29 14:50:47 +00:00
|
|
|
REPORTER_ASSERT(reporter, canvas->getSaveCount() == saveCount, testStep->assertMessage());
|
2012-07-16 14:04:59 +00:00
|
|
|
}
|
|
|
|
TEST_STEP(PaintSaveLayer, PaintSaveLayerStep);
|
|
|
|
|
2014-10-03 20:26:55 +00:00
|
|
|
static void TwoClipOpsStep(SkCanvas* canvas, const TestData& d,
|
|
|
|
skiatest::Reporter*, CanvasTestStep*) {
|
2012-07-12 17:47:34 +00:00
|
|
|
// This test exercises a functionality in SkPicture that leads to the
|
2012-08-23 18:14:13 +00:00
|
|
|
// recording of restore offset placeholders. This test will trigger an
|
2012-07-12 17:47:34 +00:00
|
|
|
// assertion at playback time if the placeholders are not properly
|
|
|
|
// filled when the recording ends.
|
2014-10-03 20:26:55 +00:00
|
|
|
canvas->clipRect(d.fRect);
|
|
|
|
canvas->clipRegion(d.fRegion);
|
2012-07-12 17:47:34 +00:00
|
|
|
}
|
|
|
|
TEST_STEP(TwoClipOps, TwoClipOpsStep);
|
|
|
|
|
2012-04-11 17:51:01 +00:00
|
|
|
// exercise fix for http://code.google.com/p/skia/issues/detail?id=560
|
|
|
|
// ('SkPathStroker::lineTo() fails for line with length SK_ScalarNearlyZero')
|
2014-10-03 20:26:55 +00:00
|
|
|
static void DrawNearlyZeroLengthPathTestStep(SkCanvas* canvas, const TestData& d,
|
|
|
|
skiatest::Reporter*, CanvasTestStep*) {
|
2012-04-11 17:51:01 +00:00
|
|
|
SkPaint paint;
|
|
|
|
paint.setStrokeWidth(SkIntToScalar(1));
|
|
|
|
paint.setStyle(SkPaint::kStroke_Style);
|
|
|
|
|
2014-10-03 20:26:55 +00:00
|
|
|
canvas->drawPath(d.fNearlyZeroLengthPath, paint);
|
2012-04-11 17:51:01 +00:00
|
|
|
}
|
|
|
|
TEST_STEP(DrawNearlyZeroLengthPath, DrawNearlyZeroLengthPathTestStep);
|
|
|
|
|
2014-10-03 20:26:55 +00:00
|
|
|
static void DrawVerticesShaderTestStep(SkCanvas* canvas, const TestData& d,
|
|
|
|
skiatest::Reporter*, CanvasTestStep*) {
|
2012-02-22 21:00:42 +00:00
|
|
|
SkPoint pts[4];
|
|
|
|
pts[0].set(0, 0);
|
2014-10-03 20:26:55 +00:00
|
|
|
pts[1].set(SkIntToScalar(d.fWidth), 0);
|
|
|
|
pts[2].set(SkIntToScalar(d.fWidth), SkIntToScalar(d.fHeight));
|
|
|
|
pts[3].set(0, SkIntToScalar(d.fHeight));
|
2012-02-22 21:00:42 +00:00
|
|
|
SkPaint paint;
|
2016-03-13 21:13:58 +00:00
|
|
|
paint.setShader(SkShader::MakeBitmapShader(d.fBitmap, SkShader::kClamp_TileMode,
|
|
|
|
SkShader::kClamp_TileMode));
|
2017-04-03 15:11:09 +00:00
|
|
|
canvas->drawVertices(SkVertices::MakeCopy(SkVertices::kTriangleFan_VertexMode, 4, pts, pts,
|
|
|
|
nullptr),
|
|
|
|
SkBlendMode::kModulate, paint);
|
2012-02-22 21:00:42 +00:00
|
|
|
}
|
2012-10-18 15:58:23 +00:00
|
|
|
// NYI: issue 240.
|
|
|
|
TEST_STEP_NO_PDF(DrawVerticesShader, DrawVerticesShaderTestStep);
|
2012-02-22 21:00:42 +00:00
|
|
|
|
2014-10-03 20:26:55 +00:00
|
|
|
static void DrawPictureTestStep(SkCanvas* canvas, const TestData& d,
|
|
|
|
skiatest::Reporter*, CanvasTestStep*) {
|
2014-04-13 19:09:42 +00:00
|
|
|
SkPictureRecorder recorder;
|
2014-10-03 20:26:55 +00:00
|
|
|
SkCanvas* testCanvas = recorder.beginRecording(SkIntToScalar(d.fWidth), SkIntToScalar(d.fHeight),
|
2015-08-27 14:41:13 +00:00
|
|
|
nullptr, 0);
|
2012-02-22 21:00:42 +00:00
|
|
|
testCanvas->scale(SkIntToScalar(2), SkIntToScalar(1));
|
2014-10-03 20:26:55 +00:00
|
|
|
testCanvas->clipRect(d.fRect);
|
|
|
|
testCanvas->drawRect(d.fRect, d.fPaint);
|
2014-04-13 19:09:42 +00:00
|
|
|
|
2016-03-18 14:25:55 +00:00
|
|
|
canvas->drawPicture(recorder.finishRecordingAsPicture());
|
2012-02-22 21:00:42 +00:00
|
|
|
}
|
|
|
|
TEST_STEP(DrawPicture, DrawPictureTestStep);
|
|
|
|
|
2014-10-03 20:26:55 +00:00
|
|
|
static void SaveRestoreTestStep(SkCanvas* canvas, const TestData& d,
|
|
|
|
skiatest::Reporter* reporter, CanvasTestStep* testStep) {
|
2012-07-16 14:04:59 +00:00
|
|
|
int baseSaveCount = canvas->getSaveCount();
|
2012-08-14 15:03:35 +00:00
|
|
|
int n = canvas->save();
|
2018-01-29 14:50:47 +00:00
|
|
|
REPORTER_ASSERT(reporter, baseSaveCount == n, testStep->assertMessage());
|
|
|
|
REPORTER_ASSERT(reporter, baseSaveCount + 1 == canvas->getSaveCount(),
|
|
|
|
testStep->assertMessage());
|
2012-02-22 21:00:42 +00:00
|
|
|
canvas->save();
|
|
|
|
canvas->save();
|
2018-01-29 14:50:47 +00:00
|
|
|
REPORTER_ASSERT(reporter, baseSaveCount + 3 == canvas->getSaveCount(),
|
|
|
|
testStep->assertMessage());
|
2012-07-16 14:04:59 +00:00
|
|
|
canvas->restoreToCount(baseSaveCount + 1);
|
2018-01-29 14:50:47 +00:00
|
|
|
REPORTER_ASSERT(reporter, baseSaveCount + 1 == canvas->getSaveCount(),
|
|
|
|
testStep->assertMessage());
|
2012-02-22 21:00:42 +00:00
|
|
|
|
|
|
|
// should this pin to 1, or be a no-op, or crash?
|
|
|
|
canvas->restoreToCount(0);
|
2018-01-29 14:50:47 +00:00
|
|
|
REPORTER_ASSERT(reporter, 1 == canvas->getSaveCount(), testStep->assertMessage());
|
2012-02-22 21:00:42 +00:00
|
|
|
}
|
|
|
|
TEST_STEP(SaveRestore, SaveRestoreTestStep);
|
2011-12-14 18:48:05 +00:00
|
|
|
|
2014-10-03 20:26:55 +00:00
|
|
|
static void NestedSaveRestoreWithSolidPaintTestStep(SkCanvas* canvas, const TestData& d,
|
|
|
|
skiatest::Reporter*, CanvasTestStep*) {
|
2012-08-16 20:53:31 +00:00
|
|
|
// This test step challenges the TestDeferredCanvasStateConsistency
|
|
|
|
// test cases because the opaque paint can trigger an optimization
|
|
|
|
// that discards previously recorded commands. The challenge is to maintain
|
|
|
|
// correct clip and matrix stack state.
|
|
|
|
canvas->resetMatrix();
|
|
|
|
canvas->rotate(SkIntToScalar(30));
|
|
|
|
canvas->save();
|
|
|
|
canvas->translate(SkIntToScalar(2), SkIntToScalar(1));
|
|
|
|
canvas->save();
|
|
|
|
canvas->scale(SkIntToScalar(3), SkIntToScalar(3));
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setColor(0xFFFFFFFF);
|
|
|
|
canvas->drawPaint(paint);
|
|
|
|
canvas->restore();
|
|
|
|
canvas->restore();
|
|
|
|
}
|
|
|
|
TEST_STEP(NestedSaveRestoreWithSolidPaint, \
|
|
|
|
NestedSaveRestoreWithSolidPaintTestStep);
|
|
|
|
|
2014-10-03 20:26:55 +00:00
|
|
|
static void NestedSaveRestoreWithFlushTestStep(SkCanvas* canvas, const TestData& d,
|
|
|
|
skiatest::Reporter*, CanvasTestStep*) {
|
2012-08-16 20:53:31 +00:00
|
|
|
// This test step challenges the TestDeferredCanvasStateConsistency
|
|
|
|
// test case because the canvas flush on a deferred canvas will
|
|
|
|
// reset the recording session. The challenge is to maintain correct
|
|
|
|
// clip and matrix stack state on the playback canvas.
|
|
|
|
canvas->resetMatrix();
|
|
|
|
canvas->rotate(SkIntToScalar(30));
|
|
|
|
canvas->save();
|
|
|
|
canvas->translate(SkIntToScalar(2), SkIntToScalar(1));
|
|
|
|
canvas->save();
|
|
|
|
canvas->scale(SkIntToScalar(3), SkIntToScalar(3));
|
2014-10-03 20:26:55 +00:00
|
|
|
canvas->drawRect(d.fRect,d.fPaint);
|
2012-08-16 20:53:31 +00:00
|
|
|
canvas->flush();
|
|
|
|
canvas->restore();
|
|
|
|
canvas->restore();
|
|
|
|
}
|
2014-10-03 20:26:55 +00:00
|
|
|
TEST_STEP(NestedSaveRestoreWithFlush, NestedSaveRestoreWithFlushTestStep);
|
2011-11-28 16:06:04 +00:00
|
|
|
|
2016-08-18 19:45:34 +00:00
|
|
|
static void TestPdfDevice(skiatest::Reporter* reporter, const TestData& d, CanvasTestStep* step) {
|
2015-03-01 14:55:20 +00:00
|
|
|
SkDynamicMemoryWStream outStream;
|
2018-09-07 18:33:14 +00:00
|
|
|
sk_sp<SkDocument> doc(SkPDF::MakeDocument(&outStream));
|
2015-08-11 20:35:12 +00:00
|
|
|
if (!doc) {
|
2018-03-20 18:17:00 +00:00
|
|
|
INFOF(reporter, "PDF disabled; TestPdfDevice test skipped.");
|
2015-08-11 20:35:12 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-03-01 14:55:20 +00:00
|
|
|
SkCanvas* canvas = doc->beginPage(SkIntToScalar(d.fWidth),
|
|
|
|
SkIntToScalar(d.fHeight));
|
|
|
|
REPORTER_ASSERT(reporter, canvas);
|
2016-08-18 19:45:34 +00:00
|
|
|
step->setAssertMessageFormat(kPdfAssertMessageFormat);
|
|
|
|
step->draw(canvas, d, reporter);
|
2012-10-18 15:58:23 +00:00
|
|
|
}
|
|
|
|
|
2012-02-22 21:00:42 +00:00
|
|
|
/*
|
|
|
|
* This sub-test verifies that the test step passes when executed
|
|
|
|
* with SkCanvas and with classes derrived from SkCanvas. It also verifies
|
|
|
|
* that the all canvas derivatives report the same state as an SkCanvas
|
|
|
|
* after having executed the test step.
|
|
|
|
*/
|
2014-10-03 20:26:55 +00:00
|
|
|
static void TestOverrideStateConsistency(skiatest::Reporter* reporter, const TestData& d,
|
2012-02-22 21:00:42 +00:00
|
|
|
CanvasTestStep* testStep) {
|
|
|
|
SkBitmap referenceStore;
|
2014-02-13 22:00:04 +00:00
|
|
|
createBitmap(&referenceStore, 0xFFFFFFFF);
|
2014-06-27 05:12:09 +00:00
|
|
|
SkCanvas referenceCanvas(referenceStore);
|
2012-02-22 21:00:42 +00:00
|
|
|
testStep->setAssertMessageFormat(kCanvasDrawAssertMessageFormat);
|
2014-10-03 20:26:55 +00:00
|
|
|
testStep->draw(&referenceCanvas, d, reporter);
|
2012-02-22 21:00:42 +00:00
|
|
|
}
|
|
|
|
|
2014-02-27 20:32:51 +00:00
|
|
|
static void test_newraster(skiatest::Reporter* reporter) {
|
|
|
|
SkImageInfo info = SkImageInfo::MakeN32Premul(10, 10);
|
2014-12-10 15:24:28 +00:00
|
|
|
const size_t minRowBytes = info.minRowBytes();
|
2017-10-03 18:47:21 +00:00
|
|
|
const size_t size = info.computeByteSize(minRowBytes);
|
2015-12-10 18:44:13 +00:00
|
|
|
SkAutoTMalloc<SkPMColor> storage(size);
|
|
|
|
SkPMColor* baseAddr = storage.get();
|
2014-12-10 15:24:28 +00:00
|
|
|
sk_bzero(baseAddr, size);
|
|
|
|
|
2016-11-12 14:06:55 +00:00
|
|
|
std::unique_ptr<SkCanvas> canvas = SkCanvas::MakeRasterDirect(info, baseAddr, minRowBytes);
|
2014-02-27 20:32:51 +00:00
|
|
|
REPORTER_ASSERT(reporter, canvas);
|
|
|
|
|
2016-03-09 22:26:26 +00:00
|
|
|
SkPixmap pmap;
|
|
|
|
const SkPMColor* addr = canvas->peekPixels(&pmap) ? pmap.addr32() : nullptr;
|
2014-02-27 20:32:51 +00:00
|
|
|
REPORTER_ASSERT(reporter, addr);
|
2016-03-09 22:26:26 +00:00
|
|
|
REPORTER_ASSERT(reporter, info == pmap.info());
|
|
|
|
REPORTER_ASSERT(reporter, minRowBytes == pmap.rowBytes());
|
2014-02-27 20:32:51 +00:00
|
|
|
for (int y = 0; y < info.height(); ++y) {
|
|
|
|
for (int x = 0; x < info.width(); ++x) {
|
|
|
|
REPORTER_ASSERT(reporter, 0 == addr[x]);
|
|
|
|
}
|
2016-03-09 22:26:26 +00:00
|
|
|
addr = (const SkPMColor*)((const char*)addr + pmap.rowBytes());
|
2014-02-27 20:32:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// now try a deliberately bad info
|
2014-09-03 18:54:58 +00:00
|
|
|
info = info.makeWH(-1, info.height());
|
2016-11-12 14:06:55 +00:00
|
|
|
REPORTER_ASSERT(reporter, nullptr == SkCanvas::MakeRasterDirect(info, baseAddr, minRowBytes));
|
2014-02-27 20:32:51 +00:00
|
|
|
|
|
|
|
// too big
|
2014-09-03 18:54:58 +00:00
|
|
|
info = info.makeWH(1 << 30, 1 << 30);
|
2016-11-12 14:06:55 +00:00
|
|
|
REPORTER_ASSERT(reporter, nullptr == SkCanvas::MakeRasterDirect(info, baseAddr, minRowBytes));
|
2014-02-28 03:02:05 +00:00
|
|
|
|
2014-02-27 20:32:51 +00:00
|
|
|
// not a valid pixel type
|
2014-09-03 18:54:58 +00:00
|
|
|
info = SkImageInfo::Make(10, 10, kUnknown_SkColorType, info.alphaType());
|
2016-11-12 14:06:55 +00:00
|
|
|
REPORTER_ASSERT(reporter, nullptr == SkCanvas::MakeRasterDirect(info, baseAddr, minRowBytes));
|
2014-02-27 20:32:51 +00:00
|
|
|
|
2018-10-10 17:20:38 +00:00
|
|
|
// We should not succeed with a zero-sized valid info
|
2014-02-27 20:32:51 +00:00
|
|
|
info = SkImageInfo::MakeN32Premul(0, 0);
|
2016-11-12 14:06:55 +00:00
|
|
|
canvas = SkCanvas::MakeRasterDirect(info, baseAddr, minRowBytes);
|
2018-10-10 17:20:38 +00:00
|
|
|
REPORTER_ASSERT(reporter, nullptr == canvas);
|
2014-02-27 20:32:51 +00:00
|
|
|
}
|
|
|
|
|
2013-12-12 21:11:12 +00:00
|
|
|
DEF_TEST(Canvas, reporter) {
|
2014-10-03 20:26:55 +00:00
|
|
|
TestData d;
|
2011-12-14 18:48:05 +00:00
|
|
|
|
2012-02-22 21:00:42 +00:00
|
|
|
for (int testStep = 0; testStep < testStepArray().count(); testStep++) {
|
2014-10-03 20:26:55 +00:00
|
|
|
TestOverrideStateConsistency(reporter, d, testStepArray()[testStep]);
|
2012-10-18 15:58:23 +00:00
|
|
|
if (testStepArray()[testStep]->enablePdfTesting()) {
|
2014-10-03 20:26:55 +00:00
|
|
|
TestPdfDevice(reporter, d, testStepArray()[testStep]);
|
2012-10-18 15:58:23 +00:00
|
|
|
}
|
2012-02-22 21:00:42 +00:00
|
|
|
}
|
2012-08-02 17:43:25 +00:00
|
|
|
|
2014-02-27 20:32:51 +00:00
|
|
|
test_newraster(reporter);
|
2011-11-28 16:06:04 +00:00
|
|
|
}
|
2014-11-26 16:55:51 +00:00
|
|
|
|
|
|
|
DEF_TEST(Canvas_SaveState, reporter) {
|
|
|
|
SkCanvas canvas(10, 10);
|
|
|
|
REPORTER_ASSERT(reporter, 1 == canvas.getSaveCount());
|
|
|
|
|
|
|
|
int n = canvas.save();
|
|
|
|
REPORTER_ASSERT(reporter, 1 == n);
|
|
|
|
REPORTER_ASSERT(reporter, 2 == canvas.getSaveCount());
|
|
|
|
|
2015-08-27 14:41:13 +00:00
|
|
|
n = canvas.saveLayer(nullptr, nullptr);
|
2014-11-26 16:55:51 +00:00
|
|
|
REPORTER_ASSERT(reporter, 2 == n);
|
|
|
|
REPORTER_ASSERT(reporter, 3 == canvas.getSaveCount());
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2014-11-26 16:55:51 +00:00
|
|
|
canvas.restore();
|
|
|
|
REPORTER_ASSERT(reporter, 2 == canvas.getSaveCount());
|
|
|
|
canvas.restore();
|
|
|
|
REPORTER_ASSERT(reporter, 1 == canvas.getSaveCount());
|
|
|
|
}
|
2015-03-13 15:48:26 +00:00
|
|
|
|
|
|
|
DEF_TEST(Canvas_ClipEmptyPath, reporter) {
|
|
|
|
SkCanvas canvas(10, 10);
|
|
|
|
canvas.save();
|
|
|
|
SkPath path;
|
|
|
|
canvas.clipPath(path);
|
|
|
|
canvas.restore();
|
|
|
|
canvas.save();
|
|
|
|
path.moveTo(5, 5);
|
|
|
|
canvas.clipPath(path);
|
|
|
|
canvas.restore();
|
|
|
|
canvas.save();
|
|
|
|
path.moveTo(7, 7);
|
|
|
|
canvas.clipPath(path); // should not assert here
|
|
|
|
canvas.restore();
|
|
|
|
}
|
2015-08-17 15:05:13 +00:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class MockFilterCanvas : public SkPaintFilterCanvas {
|
|
|
|
public:
|
|
|
|
MockFilterCanvas(SkCanvas* canvas) : INHERITED(canvas) { }
|
|
|
|
|
|
|
|
protected:
|
2016-01-12 15:21:11 +00:00
|
|
|
bool onFilter(SkTCopyOnFirstWrite<SkPaint>*, Type) const override { return true; }
|
2015-08-17 15:05:13 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
typedef SkPaintFilterCanvas INHERITED;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
// SkPaintFilterCanvas should inherit the initial target canvas state.
|
|
|
|
DEF_TEST(PaintFilterCanvas_ConsistentState, reporter) {
|
|
|
|
SkCanvas canvas(100, 100);
|
|
|
|
canvas.clipRect(SkRect::MakeXYWH(12.7f, 12.7f, 75, 75));
|
|
|
|
canvas.scale(0.5f, 0.75f);
|
|
|
|
|
|
|
|
MockFilterCanvas filterCanvas(&canvas);
|
|
|
|
REPORTER_ASSERT(reporter, canvas.getTotalMatrix() == filterCanvas.getTotalMatrix());
|
2017-01-23 16:39:45 +00:00
|
|
|
REPORTER_ASSERT(reporter, canvas.getLocalClipBounds() == filterCanvas.getLocalClipBounds());
|
2015-08-17 15:05:13 +00:00
|
|
|
|
|
|
|
filterCanvas.clipRect(SkRect::MakeXYWH(30.5f, 30.7f, 100, 100));
|
|
|
|
filterCanvas.scale(0.75f, 0.5f);
|
|
|
|
REPORTER_ASSERT(reporter, canvas.getTotalMatrix() == filterCanvas.getTotalMatrix());
|
2017-01-23 16:39:45 +00:00
|
|
|
REPORTER_ASSERT(reporter, filterCanvas.getLocalClipBounds().contains(canvas.getLocalClipBounds()));
|
2015-08-17 15:05:13 +00:00
|
|
|
}
|
2016-07-08 15:43:27 +00:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2016-11-15 16:52:55 +00:00
|
|
|
// Subclass that takes a bool*, which it updates in its construct (true) and destructor (false)
|
|
|
|
// to allow the caller to know how long the object is alive.
|
|
|
|
class LifeLineCanvas : public SkCanvas {
|
|
|
|
bool* fLifeLine;
|
|
|
|
public:
|
|
|
|
LifeLineCanvas(int w, int h, bool* lifeline) : SkCanvas(w, h), fLifeLine(lifeline) {
|
|
|
|
*fLifeLine = true;
|
|
|
|
}
|
|
|
|
~LifeLineCanvas() {
|
|
|
|
*fLifeLine = false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Check that NWayCanvas does NOT try to manage the lifetime of its sub-canvases
|
|
|
|
DEF_TEST(NWayCanvas, r) {
|
|
|
|
const int w = 10;
|
|
|
|
const int h = 10;
|
|
|
|
bool life[2];
|
|
|
|
{
|
|
|
|
LifeLineCanvas c0(w, h, &life[0]);
|
|
|
|
REPORTER_ASSERT(r, life[0]);
|
|
|
|
}
|
|
|
|
REPORTER_ASSERT(r, !life[0]);
|
|
|
|
|
|
|
|
|
|
|
|
std::unique_ptr<SkCanvas> c0 = std::unique_ptr<SkCanvas>(new LifeLineCanvas(w, h, &life[0]));
|
|
|
|
std::unique_ptr<SkCanvas> c1 = std::unique_ptr<SkCanvas>(new LifeLineCanvas(w, h, &life[1]));
|
|
|
|
REPORTER_ASSERT(r, life[0]);
|
|
|
|
REPORTER_ASSERT(r, life[1]);
|
|
|
|
|
|
|
|
{
|
|
|
|
SkNWayCanvas nway(w, h);
|
|
|
|
nway.addCanvas(c0.get());
|
|
|
|
nway.addCanvas(c1.get());
|
|
|
|
REPORTER_ASSERT(r, life[0]);
|
|
|
|
REPORTER_ASSERT(r, life[1]);
|
|
|
|
}
|
|
|
|
// Now assert that the death of the nway has NOT also killed the sub-canvases
|
|
|
|
REPORTER_ASSERT(r, life[0]);
|
|
|
|
REPORTER_ASSERT(r, life[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that CanvasStack DOES manage the lifetime of its sub-canvases
|
|
|
|
DEF_TEST(CanvasStack, r) {
|
|
|
|
const int w = 10;
|
|
|
|
const int h = 10;
|
|
|
|
bool life[2];
|
|
|
|
std::unique_ptr<SkCanvas> c0 = std::unique_ptr<SkCanvas>(new LifeLineCanvas(w, h, &life[0]));
|
|
|
|
std::unique_ptr<SkCanvas> c1 = std::unique_ptr<SkCanvas>(new LifeLineCanvas(w, h, &life[1]));
|
|
|
|
REPORTER_ASSERT(r, life[0]);
|
|
|
|
REPORTER_ASSERT(r, life[1]);
|
|
|
|
|
|
|
|
{
|
|
|
|
SkCanvasStack stack(w, h);
|
|
|
|
stack.pushCanvas(std::move(c0), {0,0});
|
|
|
|
stack.pushCanvas(std::move(c1), {0,0});
|
|
|
|
REPORTER_ASSERT(r, life[0]);
|
|
|
|
REPORTER_ASSERT(r, life[1]);
|
|
|
|
}
|
|
|
|
// Now assert that the death of the canvasstack has also killed the sub-canvases
|
|
|
|
REPORTER_ASSERT(r, !life[0]);
|
|
|
|
REPORTER_ASSERT(r, !life[1]);
|
|
|
|
}
|
2017-03-02 20:21:11 +00:00
|
|
|
|
2017-03-03 01:07:46 +00:00
|
|
|
static void test_cliptype(SkCanvas* canvas, skiatest::Reporter* r) {
|
2017-03-02 20:21:11 +00:00
|
|
|
REPORTER_ASSERT(r, !canvas->isClipEmpty());
|
|
|
|
REPORTER_ASSERT(r, canvas->isClipRect());
|
|
|
|
|
|
|
|
canvas->save();
|
|
|
|
canvas->clipRect({0, 0, 0, 0});
|
|
|
|
REPORTER_ASSERT(r, canvas->isClipEmpty());
|
|
|
|
REPORTER_ASSERT(r, !canvas->isClipRect());
|
|
|
|
canvas->restore();
|
|
|
|
|
|
|
|
canvas->save();
|
|
|
|
canvas->clipRect({2, 2, 6, 6});
|
|
|
|
REPORTER_ASSERT(r, !canvas->isClipEmpty());
|
|
|
|
REPORTER_ASSERT(r, canvas->isClipRect());
|
|
|
|
canvas->restore();
|
|
|
|
|
|
|
|
canvas->save();
|
|
|
|
canvas->clipRect({2, 2, 6, 6}, SkClipOp::kDifference); // punch a hole in the clip
|
|
|
|
REPORTER_ASSERT(r, !canvas->isClipEmpty());
|
|
|
|
REPORTER_ASSERT(r, !canvas->isClipRect());
|
|
|
|
canvas->restore();
|
|
|
|
|
|
|
|
REPORTER_ASSERT(r, !canvas->isClipEmpty());
|
|
|
|
REPORTER_ASSERT(r, canvas->isClipRect());
|
|
|
|
}
|
2017-03-03 01:07:46 +00:00
|
|
|
|
|
|
|
DEF_TEST(CanvasClipType, r) {
|
|
|
|
// test rasterclip backend
|
|
|
|
test_cliptype(SkSurface::MakeRasterN32Premul(10, 10)->getCanvas(), r);
|
|
|
|
|
|
|
|
// test clipstack backend
|
|
|
|
SkDynamicMemoryWStream stream;
|
2018-09-07 18:33:14 +00:00
|
|
|
if (auto doc = SkPDF::MakeDocument(&stream)) {
|
2018-03-20 18:17:00 +00:00
|
|
|
test_cliptype(doc->beginPage(100, 100), r);
|
|
|
|
}
|
2017-03-03 01:07:46 +00:00
|
|
|
}
|
2017-04-11 12:46:01 +00:00
|
|
|
|
|
|
|
#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
|
|
|
|
DEF_TEST(Canvas_LegacyColorBehavior, r) {
|
2019-01-04 17:07:44 +00:00
|
|
|
sk_sp<SkColorSpace> cs = SkColorSpace::MakeRGB(SkColorSpace::kSRGB_RenderTargetGamma,
|
|
|
|
SkColorSpace::kAdobeRGB_Gamut);
|
2017-04-11 12:46:01 +00:00
|
|
|
|
|
|
|
// Make a Adobe RGB bitmap.
|
|
|
|
SkBitmap bitmap;
|
|
|
|
bitmap.allocPixels(SkImageInfo::MakeN32(1, 1, kOpaque_SkAlphaType, cs));
|
|
|
|
bitmap.eraseColor(0xFF000000);
|
|
|
|
|
|
|
|
// Wrap it in a legacy canvas. Test that the canvas behaves like a legacy canvas.
|
|
|
|
SkCanvas canvas(bitmap, SkCanvas::ColorBehavior::kLegacy);
|
|
|
|
REPORTER_ASSERT(r, !canvas.imageInfo().colorSpace());
|
|
|
|
SkPaint p;
|
|
|
|
p.setColor(SK_ColorRED);
|
|
|
|
canvas.drawIRect(SkIRect::MakeWH(1, 1), p);
|
|
|
|
REPORTER_ASSERT(r, SK_ColorRED == SkSwizzle_BGRA_to_PMColor(*bitmap.getAddr32(0, 0)));
|
|
|
|
}
|
|
|
|
#endif
|
2018-01-22 17:51:13 +00:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class ZeroBoundsImageFilter : public SkImageFilter {
|
|
|
|
public:
|
|
|
|
static sk_sp<SkImageFilter> Make() { return sk_sp<SkImageFilter>(new ZeroBoundsImageFilter); }
|
|
|
|
|
|
|
|
protected:
|
|
|
|
sk_sp<SkSpecialImage> onFilterImage(SkSpecialImage*, const Context&, SkIPoint*) const override {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
sk_sp<SkImageFilter> onMakeColorSpace(SkColorSpaceXformer*) const override { return nullptr; }
|
2018-05-17 15:17:39 +00:00
|
|
|
SkIRect onFilterNodeBounds(const SkIRect&, const SkMatrix&,
|
|
|
|
MapDirection, const SkIRect* inputRect) const override {
|
2018-01-22 17:51:13 +00:00
|
|
|
return SkIRect::MakeEmpty();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2018-10-18 21:27:16 +00:00
|
|
|
SK_FLATTENABLE_HOOKS(ZeroBoundsImageFilter)
|
|
|
|
|
2018-01-22 17:51:13 +00:00
|
|
|
ZeroBoundsImageFilter() : INHERITED(nullptr, 0, nullptr) {}
|
|
|
|
|
|
|
|
typedef SkImageFilter INHERITED;
|
|
|
|
};
|
|
|
|
|
|
|
|
sk_sp<SkFlattenable> ZeroBoundsImageFilter::CreateProc(SkReadBuffer& buffer) {
|
|
|
|
SkDEBUGFAIL("Should never get here");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
DEF_TEST(Canvas_SaveLayerWithNullBoundsAndZeroBoundsImageFilter, r) {
|
|
|
|
SkCanvas canvas(10, 10);
|
|
|
|
SkPaint p;
|
|
|
|
p.setImageFilter(ZeroBoundsImageFilter::Make());
|
|
|
|
// This should not fail any assert.
|
|
|
|
canvas.saveLayer(nullptr, &p);
|
|
|
|
REPORTER_ASSERT(r, canvas.getDeviceClipBounds().isEmpty());
|
|
|
|
canvas.restore();
|
|
|
|
}
|
2018-04-13 19:34:16 +00:00
|
|
|
|
|
|
|
#include "SkPaintImageFilter.h"
|
|
|
|
|
|
|
|
// Test that we don't crash/assert when building a canvas with degenerate coordintes
|
|
|
|
// (esp. big ones, that might invoke tiling).
|
|
|
|
DEF_TEST(Canvas_degenerate_dimension, reporter) {
|
|
|
|
// Need a paint that will sneak us past the quickReject in SkCanvas, so we can test the
|
|
|
|
// raster code further downstream.
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setImageFilter(SkPaintImageFilter::Make(SkPaint(), nullptr));
|
|
|
|
REPORTER_ASSERT(reporter, !paint.canComputeFastBounds());
|
|
|
|
|
|
|
|
const int big = 100 * 1024; // big enough to definitely trigger tiling
|
|
|
|
const SkISize sizes[] {SkISize{0, big}, {big, 0}, {0, 0}};
|
|
|
|
for (SkISize size : sizes) {
|
|
|
|
SkBitmap bm;
|
|
|
|
bm.setInfo(SkImageInfo::MakeN32Premul(size.width(), size.height()));
|
|
|
|
SkCanvas canvas(bm);
|
|
|
|
canvas.drawRect({0, 0, 100, 90*1024}, paint);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-20 14:27:27 +00:00
|
|
|
#include "SkBlurImageFilter.h"
|
|
|
|
|
|
|
|
DEF_TEST(Canvas_ClippedOutImageFilter, reporter) {
|
|
|
|
SkCanvas canvas(100, 100);
|
|
|
|
|
|
|
|
SkPaint p;
|
|
|
|
p.setColor(SK_ColorGREEN);
|
|
|
|
p.setImageFilter(SkBlurImageFilter::Make(3.0f, 3.0f, nullptr, nullptr));
|
|
|
|
|
|
|
|
SkRect blurredRect = SkRect::MakeXYWH(60, 10, 30, 30);
|
|
|
|
|
|
|
|
SkMatrix invM;
|
|
|
|
invM.setRotate(-45);
|
|
|
|
invM.mapRect(&blurredRect);
|
|
|
|
|
|
|
|
const SkRect clipRect = SkRect::MakeXYWH(0, 50, 50, 50);
|
|
|
|
|
|
|
|
canvas.clipRect(clipRect);
|
|
|
|
|
|
|
|
canvas.rotate(45);
|
|
|
|
const SkMatrix preCTM = canvas.getTotalMatrix();
|
|
|
|
canvas.drawRect(blurredRect, p);
|
|
|
|
const SkMatrix postCTM = canvas.getTotalMatrix();
|
|
|
|
REPORTER_ASSERT(reporter, preCTM == postCTM);
|
|
|
|
}
|
|
|
|
|