2011-07-28 14:26:00 +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.
|
|
|
|
*/
|
2013-12-12 21:11:12 +00:00
|
|
|
|
2011-03-17 17:48:04 +00:00
|
|
|
#include "SkBitmap.h"
|
|
|
|
#include "SkCanvas.h"
|
2013-10-15 20:05:24 +00:00
|
|
|
#include "SkData.h"
|
2013-12-05 18:31:42 +00:00
|
|
|
#include "SkDiscardableMemoryPool.h"
|
2014-05-27 14:14:22 +00:00
|
|
|
#include "SkImageGeneratorPriv.h"
|
2014-01-24 20:56:26 +00:00
|
|
|
#include "SkMatrixUtils.h"
|
2013-10-15 20:05:24 +00:00
|
|
|
#include "SkPaint.h"
|
2014-01-24 20:56:26 +00:00
|
|
|
#include "SkRandom.h"
|
2012-05-03 20:14:26 +00:00
|
|
|
#include "SkShader.h"
|
2013-10-15 20:05:24 +00:00
|
|
|
#include "SkSurface.h"
|
2014-01-24 20:56:26 +00:00
|
|
|
#include "Test.h"
|
2013-01-02 20:19:45 +00:00
|
|
|
|
2013-10-15 20:05:24 +00:00
|
|
|
// A BitmapFactory that always fails when asked to return pixels.
|
2013-12-05 18:31:42 +00:00
|
|
|
class FailureImageGenerator : public SkImageGenerator {
|
|
|
|
public:
|
|
|
|
FailureImageGenerator() { }
|
|
|
|
virtual ~FailureImageGenerator() { }
|
2014-05-30 03:06:10 +00:00
|
|
|
|
2014-05-29 15:57:20 +00:00
|
|
|
protected:
|
|
|
|
virtual bool onGetInfo(SkImageInfo* info) SK_OVERRIDE {
|
2014-09-03 18:54:58 +00:00
|
|
|
*info = SkImageInfo::MakeN32Premul(100, 100);
|
2013-12-05 18:31:42 +00:00
|
|
|
return true;
|
|
|
|
}
|
2014-05-29 15:57:20 +00:00
|
|
|
// default onGetPixels() returns false, which is what we want.
|
2013-12-05 18:31:42 +00:00
|
|
|
};
|
2013-10-15 20:05:24 +00:00
|
|
|
|
|
|
|
// crbug.com/295895
|
|
|
|
// Crashing in skia when a pixelref fails in lockPixels
|
|
|
|
//
|
|
|
|
static void test_faulty_pixelref(skiatest::Reporter* reporter) {
|
|
|
|
// need a cache, but don't expect to use it, so the budget is not critical
|
2014-04-04 16:43:38 +00:00
|
|
|
SkAutoTUnref<SkDiscardableMemoryPool> pool(
|
|
|
|
SkDiscardableMemoryPool::Create(10 * 1000, NULL));
|
2013-10-15 20:05:24 +00:00
|
|
|
SkBitmap bm;
|
2013-12-10 21:11:12 +00:00
|
|
|
bool installSuccess = SkInstallDiscardablePixelRef(SkNEW(FailureImageGenerator), &bm, pool);
|
2013-12-05 18:31:42 +00:00
|
|
|
REPORTER_ASSERT(reporter, installSuccess);
|
2013-10-15 20:05:24 +00:00
|
|
|
// now our bitmap has a pixelref, but we know it will fail to lock
|
|
|
|
|
|
|
|
SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterPMColor(200, 200));
|
|
|
|
SkCanvas* canvas = surface->getCanvas();
|
|
|
|
|
|
|
|
const SkPaint::FilterLevel levels[] = {
|
|
|
|
SkPaint::kNone_FilterLevel,
|
|
|
|
SkPaint::kLow_FilterLevel,
|
|
|
|
SkPaint::kMedium_FilterLevel,
|
|
|
|
SkPaint::kHigh_FilterLevel,
|
|
|
|
};
|
|
|
|
|
|
|
|
SkPaint paint;
|
|
|
|
canvas->scale(2, 2); // need a scale, otherwise we may ignore filtering
|
|
|
|
for (size_t i = 0; i < SK_ARRAY_COUNT(levels); ++i) {
|
|
|
|
paint.setFilterLevel(levels[i]);
|
|
|
|
canvas->drawBitmap(bm, 0, 0, &paint);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2013-09-09 20:09:12 +00:00
|
|
|
static void rand_matrix(SkMatrix* mat, SkRandom& rand, unsigned mask) {
|
2013-01-02 20:19:45 +00:00
|
|
|
mat->setIdentity();
|
|
|
|
if (mask & SkMatrix::kTranslate_Mask) {
|
|
|
|
mat->postTranslate(rand.nextSScalar1(), rand.nextSScalar1());
|
|
|
|
}
|
|
|
|
if (mask & SkMatrix::kScale_Mask) {
|
|
|
|
mat->postScale(rand.nextSScalar1(), rand.nextSScalar1());
|
|
|
|
}
|
|
|
|
if (mask & SkMatrix::kAffine_Mask) {
|
|
|
|
mat->postRotate(rand.nextSScalar1() * 360);
|
|
|
|
}
|
|
|
|
if (mask & SkMatrix::kPerspective_Mask) {
|
|
|
|
mat->setPerspX(rand.nextSScalar1());
|
|
|
|
mat->setPerspY(rand.nextSScalar1());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-09 20:09:12 +00:00
|
|
|
static void rand_size(SkISize* size, SkRandom& rand) {
|
2013-01-03 15:22:40 +00:00
|
|
|
size->set(rand.nextU() & 0xFFFF, rand.nextU() & 0xFFFF);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool treat_as_sprite(const SkMatrix& mat, const SkISize& size,
|
|
|
|
unsigned bits) {
|
|
|
|
return SkTreatAsSprite(mat, size.width(), size.height(), bits);
|
2013-01-02 20:19:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void test_treatAsSprite(skiatest::Reporter* reporter) {
|
|
|
|
const unsigned bilerBits = kSkSubPixelBitsForBilerp;
|
|
|
|
|
|
|
|
SkMatrix mat;
|
2013-01-03 15:22:40 +00:00
|
|
|
SkISize size;
|
2013-09-09 20:09:12 +00:00
|
|
|
SkRandom rand;
|
2013-01-02 20:19:45 +00:00
|
|
|
|
|
|
|
// assert: translate-only no-filter can always be treated as sprite
|
|
|
|
for (int i = 0; i < 1000; ++i) {
|
|
|
|
rand_matrix(&mat, rand, SkMatrix::kTranslate_Mask);
|
|
|
|
for (int j = 0; j < 1000; ++j) {
|
2013-01-03 15:22:40 +00:00
|
|
|
rand_size(&size, rand);
|
|
|
|
REPORTER_ASSERT(reporter, treat_as_sprite(mat, size, 0));
|
2013-01-02 20:19:45 +00:00
|
|
|
}
|
|
|
|
}
|
2013-01-03 02:01:32 +00:00
|
|
|
|
2013-01-02 20:19:45 +00:00
|
|
|
// assert: rotate/perspect is never treated as sprite
|
|
|
|
for (int i = 0; i < 1000; ++i) {
|
|
|
|
rand_matrix(&mat, rand, SkMatrix::kAffine_Mask | SkMatrix::kPerspective_Mask);
|
|
|
|
for (int j = 0; j < 1000; ++j) {
|
2013-01-03 15:22:40 +00:00
|
|
|
rand_size(&size, rand);
|
|
|
|
REPORTER_ASSERT(reporter, !treat_as_sprite(mat, size, 0));
|
|
|
|
REPORTER_ASSERT(reporter, !treat_as_sprite(mat, size, bilerBits));
|
2013-01-02 20:19:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-03 15:22:40 +00:00
|
|
|
size.set(500, 600);
|
2013-01-02 20:19:45 +00:00
|
|
|
|
2013-11-25 19:44:07 +00:00
|
|
|
const SkScalar tooMuchSubpixel = 100.1f;
|
2013-01-02 20:19:45 +00:00
|
|
|
mat.setTranslate(tooMuchSubpixel, 0);
|
2013-01-03 15:22:40 +00:00
|
|
|
REPORTER_ASSERT(reporter, !treat_as_sprite(mat, size, bilerBits));
|
2013-01-02 20:19:45 +00:00
|
|
|
mat.setTranslate(0, tooMuchSubpixel);
|
2013-01-03 15:22:40 +00:00
|
|
|
REPORTER_ASSERT(reporter, !treat_as_sprite(mat, size, bilerBits));
|
2013-01-02 20:19:45 +00:00
|
|
|
|
2013-11-25 19:44:07 +00:00
|
|
|
const SkScalar tinySubPixel = 100.02f;
|
2013-01-02 20:19:45 +00:00
|
|
|
mat.setTranslate(tinySubPixel, 0);
|
2013-01-03 15:22:40 +00:00
|
|
|
REPORTER_ASSERT(reporter, treat_as_sprite(mat, size, bilerBits));
|
2013-01-02 20:19:45 +00:00
|
|
|
mat.setTranslate(0, tinySubPixel);
|
2013-01-03 15:22:40 +00:00
|
|
|
REPORTER_ASSERT(reporter, treat_as_sprite(mat, size, bilerBits));
|
2013-01-03 02:01:32 +00:00
|
|
|
|
2013-01-02 20:19:45 +00:00
|
|
|
const SkScalar twoThirds = SK_Scalar1 * 2 / 3;
|
2013-01-03 15:22:40 +00:00
|
|
|
const SkScalar bigScale = SkScalarDiv(size.width() + twoThirds, size.width());
|
2013-01-02 20:19:45 +00:00
|
|
|
mat.setScale(bigScale, bigScale);
|
2013-01-03 15:22:40 +00:00
|
|
|
REPORTER_ASSERT(reporter, !treat_as_sprite(mat, size, false));
|
|
|
|
REPORTER_ASSERT(reporter, !treat_as_sprite(mat, size, bilerBits));
|
2013-01-03 02:01:32 +00:00
|
|
|
|
2013-01-02 20:19:45 +00:00
|
|
|
const SkScalar oneThird = SK_Scalar1 / 3;
|
2013-01-03 15:22:40 +00:00
|
|
|
const SkScalar smallScale = SkScalarDiv(size.width() + oneThird, size.width());
|
2013-01-02 20:19:45 +00:00
|
|
|
mat.setScale(smallScale, smallScale);
|
2013-01-03 15:22:40 +00:00
|
|
|
REPORTER_ASSERT(reporter, treat_as_sprite(mat, size, false));
|
|
|
|
REPORTER_ASSERT(reporter, !treat_as_sprite(mat, size, bilerBits));
|
2013-01-03 02:01:32 +00:00
|
|
|
|
2013-01-02 20:19:45 +00:00
|
|
|
const SkScalar oneFortyth = SK_Scalar1 / 40;
|
2013-01-03 15:22:40 +00:00
|
|
|
const SkScalar tinyScale = SkScalarDiv(size.width() + oneFortyth, size.width());
|
2013-01-02 20:19:45 +00:00
|
|
|
mat.setScale(tinyScale, tinyScale);
|
2013-01-03 15:22:40 +00:00
|
|
|
REPORTER_ASSERT(reporter, treat_as_sprite(mat, size, false));
|
|
|
|
REPORTER_ASSERT(reporter, treat_as_sprite(mat, size, bilerBits));
|
2013-01-02 20:19:45 +00:00
|
|
|
}
|
2012-05-03 20:14:26 +00:00
|
|
|
|
|
|
|
static void assert_ifDrawnTo(skiatest::Reporter* reporter,
|
|
|
|
const SkBitmap& bm, bool shouldBeDrawn) {
|
|
|
|
for (int y = 0; y < bm.height(); ++y) {
|
|
|
|
for (int x = 0; x < bm.width(); ++x) {
|
|
|
|
if (shouldBeDrawn) {
|
2013-07-15 13:10:31 +00:00
|
|
|
if (SK_ColorTRANSPARENT == *bm.getAddr32(x, y)) {
|
2012-05-03 20:14:26 +00:00
|
|
|
REPORTER_ASSERT(reporter, false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// should not be drawn
|
2013-07-15 13:10:31 +00:00
|
|
|
if (SK_ColorTRANSPARENT != *bm.getAddr32(x, y)) {
|
2012-05-03 20:14:26 +00:00
|
|
|
REPORTER_ASSERT(reporter, false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_wacky_bitmapshader(skiatest::Reporter* reporter,
|
|
|
|
int width, int height, bool shouldBeDrawn) {
|
|
|
|
SkBitmap dev;
|
2014-02-13 22:00:04 +00:00
|
|
|
dev.allocN32Pixels(0x56F, 0x4f6);
|
2012-12-06 21:47:40 +00:00
|
|
|
dev.eraseColor(SK_ColorTRANSPARENT); // necessary, so we know if we draw to it
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2012-05-03 20:14:26 +00:00
|
|
|
SkMatrix matrix;
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2012-05-03 20:14:26 +00:00
|
|
|
SkCanvas c(dev);
|
2013-11-25 19:44:07 +00:00
|
|
|
matrix.setAll(-119.34097f,
|
|
|
|
-43.436558f,
|
|
|
|
93489.945f,
|
|
|
|
43.436558f,
|
|
|
|
-119.34097f,
|
|
|
|
123.98426f,
|
2012-05-14 16:33:36 +00:00
|
|
|
0, 0, SK_Scalar1);
|
2012-05-03 20:14:26 +00:00
|
|
|
c.concat(matrix);
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2012-05-03 20:14:26 +00:00
|
|
|
SkBitmap bm;
|
2014-09-02 19:50:45 +00:00
|
|
|
if (bm.tryAllocN32Pixels(width, height)) {
|
|
|
|
// allow this to fail silently, to test the code downstream
|
|
|
|
}
|
2012-05-03 20:14:26 +00:00
|
|
|
bm.eraseColor(SK_ColorRED);
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2013-11-25 19:44:07 +00:00
|
|
|
matrix.setAll(0.0078740157f,
|
2012-08-23 18:14:13 +00:00
|
|
|
0,
|
2012-05-14 16:33:36 +00:00
|
|
|
SkIntToScalar(249),
|
2012-08-23 18:14:13 +00:00
|
|
|
0,
|
2013-11-25 19:44:07 +00:00
|
|
|
0.0078740157f,
|
2012-05-14 16:33:36 +00:00
|
|
|
SkIntToScalar(239),
|
|
|
|
0, 0, SK_Scalar1);
|
2014-04-28 14:55:39 +00:00
|
|
|
SkShader* s = SkShader::CreateBitmapShader(bm, SkShader::kRepeat_TileMode,
|
|
|
|
SkShader::kRepeat_TileMode, &matrix);
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2012-05-03 20:14:26 +00:00
|
|
|
SkPaint paint;
|
|
|
|
paint.setShader(s)->unref();
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2012-05-03 20:14:26 +00:00
|
|
|
SkRect r = SkRect::MakeXYWH(681, 239, 695, 253);
|
|
|
|
c.drawRect(r, paint);
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2012-05-03 20:14:26 +00:00
|
|
|
assert_ifDrawnTo(reporter, dev, shouldBeDrawn);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Original bug was asserting that the matrix-proc had generated a (Y) value
|
|
|
|
* that was out of range. This led (in the release build) to the sampler-proc
|
|
|
|
* reading memory out-of-bounds of the original bitmap.
|
|
|
|
*
|
|
|
|
* We were numerically overflowing our 16bit coordinates that we communicate
|
|
|
|
* between these two procs. The fixes was in two parts:
|
|
|
|
*
|
|
|
|
* 1. Just don't draw bitmaps larger than 64K-1 in width or height, since we
|
|
|
|
* can't represent those coordinates in our transport format (yet).
|
|
|
|
* 2. Perform an unsigned shift during the calculation, so we don't get
|
|
|
|
* sign-extension bleed when packing the two values (X,Y) into our 32bit
|
|
|
|
* slot.
|
|
|
|
*
|
|
|
|
* This tests exercises the original setup, plus 3 more to ensure that we can,
|
|
|
|
* in fact, handle bitmaps at 64K-1 (assuming we don't exceed the total
|
|
|
|
* memory allocation limit).
|
|
|
|
*/
|
|
|
|
static void test_giantrepeat_crbug118018(skiatest::Reporter* reporter) {
|
|
|
|
static const struct {
|
|
|
|
int fWidth;
|
|
|
|
int fHeight;
|
|
|
|
bool fExpectedToDraw;
|
|
|
|
} gTests[] = {
|
|
|
|
{ 0x1b294, 0x7f, false }, // crbug 118018 (width exceeds 64K)
|
|
|
|
{ 0xFFFF, 0x7f, true }, // should draw, test max width
|
|
|
|
{ 0x7f, 0xFFFF, true }, // should draw, test max height
|
|
|
|
{ 0xFFFF, 0xFFFF, false }, // allocation fails (too much RAM)
|
|
|
|
};
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2012-05-03 20:14:26 +00:00
|
|
|
for (size_t i = 0; i < SK_ARRAY_COUNT(gTests); ++i) {
|
|
|
|
test_wacky_bitmapshader(reporter,
|
|
|
|
gTests[i].fWidth, gTests[i].fHeight,
|
|
|
|
gTests[i].fExpectedToDraw);
|
|
|
|
}
|
|
|
|
}
|
2012-05-03 20:26:06 +00:00
|
|
|
|
2012-05-03 20:14:26 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2011-03-17 17:48:04 +00:00
|
|
|
|
2013-02-27 19:17:41 +00:00
|
|
|
static void test_nan_antihair() {
|
2012-03-30 17:43:33 +00:00
|
|
|
SkBitmap bm;
|
2014-02-13 22:00:04 +00:00
|
|
|
bm.allocN32Pixels(20, 20);
|
2012-03-30 17:43:33 +00:00
|
|
|
|
|
|
|
SkCanvas canvas(bm);
|
|
|
|
|
|
|
|
SkPath path;
|
|
|
|
path.moveTo(0, 0);
|
|
|
|
path.lineTo(10, SK_ScalarNaN);
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2012-03-30 17:43:33 +00:00
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
|
|
|
paint.setStyle(SkPaint::kStroke_Style);
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2012-03-30 17:43:33 +00:00
|
|
|
// before our fix to SkScan_Antihair.cpp to check for integral NaN (0x800...)
|
|
|
|
// this would trigger an assert/crash.
|
|
|
|
//
|
|
|
|
// see rev. 3558
|
|
|
|
canvas.drawPath(path, paint);
|
|
|
|
}
|
|
|
|
|
2011-03-17 17:48:04 +00:00
|
|
|
static bool check_for_all_zeros(const SkBitmap& bm) {
|
|
|
|
SkAutoLockPixels alp(bm);
|
|
|
|
|
|
|
|
size_t count = bm.width() * bm.bytesPerPixel();
|
|
|
|
for (int y = 0; y < bm.height(); y++) {
|
|
|
|
const uint8_t* ptr = reinterpret_cast<const uint8_t*>(bm.getAddr(0, y));
|
|
|
|
for (size_t i = 0; i < count; i++) {
|
|
|
|
if (ptr[i]) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const int gWidth = 256;
|
|
|
|
static const int gHeight = 256;
|
|
|
|
|
2014-02-13 22:00:04 +00:00
|
|
|
static void create(SkBitmap* bm, SkColor color) {
|
|
|
|
bm->allocN32Pixels(gWidth, gHeight);
|
2011-03-17 17:48:04 +00:00
|
|
|
bm->eraseColor(color);
|
|
|
|
}
|
|
|
|
|
2013-12-12 21:11:12 +00:00
|
|
|
DEF_TEST(DrawBitmapRect, reporter) {
|
2011-03-17 17:48:04 +00:00
|
|
|
SkBitmap src, dst;
|
|
|
|
|
2014-02-13 22:00:04 +00:00
|
|
|
create(&src, 0xFFFFFFFF);
|
|
|
|
create(&dst, 0);
|
2011-03-17 17:48:04 +00:00
|
|
|
|
|
|
|
SkCanvas canvas(dst);
|
|
|
|
|
|
|
|
SkIRect srcR = { gWidth, 0, gWidth + 16, 16 };
|
|
|
|
SkRect dstR = { 0, 0, SkIntToScalar(16), SkIntToScalar(16) };
|
|
|
|
|
|
|
|
canvas.drawBitmapRect(src, &srcR, dstR, NULL);
|
|
|
|
|
|
|
|
// ensure that we draw nothing if srcR does not intersect the bitmap
|
|
|
|
REPORTER_ASSERT(reporter, check_for_all_zeros(dst));
|
2012-03-30 17:43:33 +00:00
|
|
|
|
2013-02-27 19:17:41 +00:00
|
|
|
test_nan_antihair();
|
2012-05-03 20:14:26 +00:00
|
|
|
test_giantrepeat_crbug118018(reporter);
|
2013-01-02 20:19:45 +00:00
|
|
|
|
|
|
|
test_treatAsSprite(reporter);
|
2013-10-15 20:14:00 +00:00
|
|
|
test_faulty_pixelref(reporter);
|
2011-03-17 17:48:04 +00:00
|
|
|
}
|