2 * Copyright 2011 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
11 #include "SkDiscardableMemoryPool.h"
12 #include "SkImageGeneratorPriv.h"
13 #include "SkMatrixUtils.h"
17 #include "SkSurface.h"
20 // A BitmapFactory that always fails when asked to return pixels.
21 class FailureImageGenerator : public SkImageGenerator {
23 FailureImageGenerator() : SkImageGenerator(SkImageInfo::MakeN32Premul(100, 100)) {}
25 #ifdef SK_SUPPORT_LEGACY_BOOL_ONGETINFO
26 bool onGetInfo(SkImageInfo* info) SK_OVERRIDE {
27 *info = SkImageInfo::MakeN32Premul(100, 100);
31 // default onGetPixels() returns kUnimplemented, which is what we want.
35 // Crashing in skia when a pixelref fails in lockPixels
37 static void test_faulty_pixelref(skiatest::Reporter* reporter) {
38 // need a cache, but don't expect to use it, so the budget is not critical
39 SkAutoTUnref<SkDiscardableMemoryPool> pool(
40 SkDiscardableMemoryPool::Create(10 * 1000, NULL));
42 bool installSuccess = SkInstallDiscardablePixelRef(SkNEW(FailureImageGenerator), &bm, pool);
43 REPORTER_ASSERT(reporter, installSuccess);
44 // now our bitmap has a pixelref, but we know it will fail to lock
46 SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterN32Premul(200, 200));
47 SkCanvas* canvas = surface->getCanvas();
49 const SkFilterQuality levels[] = {
50 kNone_SkFilterQuality,
52 kMedium_SkFilterQuality,
53 kHigh_SkFilterQuality,
57 canvas->scale(2, 2); // need a scale, otherwise we may ignore filtering
58 for (size_t i = 0; i < SK_ARRAY_COUNT(levels); ++i) {
59 paint.setFilterQuality(levels[i]);
60 canvas->drawBitmap(bm, 0, 0, &paint);
64 ///////////////////////////////////////////////////////////////////////////////
66 static void rand_matrix(SkMatrix* mat, SkRandom& rand, unsigned mask) {
68 if (mask & SkMatrix::kTranslate_Mask) {
69 mat->postTranslate(rand.nextSScalar1(), rand.nextSScalar1());
71 if (mask & SkMatrix::kScale_Mask) {
72 mat->postScale(rand.nextSScalar1(), rand.nextSScalar1());
74 if (mask & SkMatrix::kAffine_Mask) {
75 mat->postRotate(rand.nextSScalar1() * 360);
77 if (mask & SkMatrix::kPerspective_Mask) {
78 mat->setPerspX(rand.nextSScalar1());
79 mat->setPerspY(rand.nextSScalar1());
83 static void rand_size(SkISize* size, SkRandom& rand) {
84 size->set(rand.nextU() & 0xFFFF, rand.nextU() & 0xFFFF);
87 static bool treat_as_sprite(const SkMatrix& mat, const SkISize& size,
89 return SkTreatAsSprite(mat, size.width(), size.height(), bits);
92 static void test_treatAsSprite(skiatest::Reporter* reporter) {
93 const unsigned bilerBits = kSkSubPixelBitsForBilerp;
99 // assert: translate-only no-filter can always be treated as sprite
100 for (int i = 0; i < 1000; ++i) {
101 rand_matrix(&mat, rand, SkMatrix::kTranslate_Mask);
102 for (int j = 0; j < 1000; ++j) {
103 rand_size(&size, rand);
104 REPORTER_ASSERT(reporter, treat_as_sprite(mat, size, 0));
108 // assert: rotate/perspect is never treated as sprite
109 for (int i = 0; i < 1000; ++i) {
110 rand_matrix(&mat, rand, SkMatrix::kAffine_Mask | SkMatrix::kPerspective_Mask);
111 for (int j = 0; j < 1000; ++j) {
112 rand_size(&size, rand);
113 REPORTER_ASSERT(reporter, !treat_as_sprite(mat, size, 0));
114 REPORTER_ASSERT(reporter, !treat_as_sprite(mat, size, bilerBits));
120 const SkScalar tooMuchSubpixel = 100.1f;
121 mat.setTranslate(tooMuchSubpixel, 0);
122 REPORTER_ASSERT(reporter, !treat_as_sprite(mat, size, bilerBits));
123 mat.setTranslate(0, tooMuchSubpixel);
124 REPORTER_ASSERT(reporter, !treat_as_sprite(mat, size, bilerBits));
126 const SkScalar tinySubPixel = 100.02f;
127 mat.setTranslate(tinySubPixel, 0);
128 REPORTER_ASSERT(reporter, treat_as_sprite(mat, size, bilerBits));
129 mat.setTranslate(0, tinySubPixel);
130 REPORTER_ASSERT(reporter, treat_as_sprite(mat, size, bilerBits));
132 const SkScalar twoThirds = SK_Scalar1 * 2 / 3;
133 const SkScalar bigScale = SkScalarDiv(size.width() + twoThirds, size.width());
134 mat.setScale(bigScale, bigScale);
135 REPORTER_ASSERT(reporter, !treat_as_sprite(mat, size, false));
136 REPORTER_ASSERT(reporter, !treat_as_sprite(mat, size, bilerBits));
138 const SkScalar oneThird = SK_Scalar1 / 3;
139 const SkScalar smallScale = SkScalarDiv(size.width() + oneThird, size.width());
140 mat.setScale(smallScale, smallScale);
141 REPORTER_ASSERT(reporter, treat_as_sprite(mat, size, false));
142 REPORTER_ASSERT(reporter, !treat_as_sprite(mat, size, bilerBits));
144 const SkScalar oneFortyth = SK_Scalar1 / 40;
145 const SkScalar tinyScale = SkScalarDiv(size.width() + oneFortyth, size.width());
146 mat.setScale(tinyScale, tinyScale);
147 REPORTER_ASSERT(reporter, treat_as_sprite(mat, size, false));
148 REPORTER_ASSERT(reporter, treat_as_sprite(mat, size, bilerBits));
151 static void assert_ifDrawnTo(skiatest::Reporter* reporter,
152 const SkBitmap& bm, bool shouldBeDrawn) {
153 for (int y = 0; y < bm.height(); ++y) {
154 for (int x = 0; x < bm.width(); ++x) {
156 if (SK_ColorTRANSPARENT == *bm.getAddr32(x, y)) {
157 REPORTER_ASSERT(reporter, false);
161 // should not be drawn
162 if (SK_ColorTRANSPARENT != *bm.getAddr32(x, y)) {
163 REPORTER_ASSERT(reporter, false);
171 static void test_wacky_bitmapshader(skiatest::Reporter* reporter,
172 int width, int height, bool shouldBeDrawn) {
174 dev.allocN32Pixels(0x56F, 0x4f6);
175 dev.eraseColor(SK_ColorTRANSPARENT); // necessary, so we know if we draw to it
180 matrix.setAll(-119.34097f,
190 if (bm.tryAllocN32Pixels(width, height)) {
191 // allow this to fail silently, to test the code downstream
193 bm.eraseColor(SK_ColorRED);
195 matrix.setAll(0.0078740157f,
202 SkShader* s = SkShader::CreateBitmapShader(bm, SkShader::kRepeat_TileMode,
203 SkShader::kRepeat_TileMode, &matrix);
206 paint.setShader(s)->unref();
208 SkRect r = SkRect::MakeXYWH(681, 239, 695, 253);
209 c.drawRect(r, paint);
211 assert_ifDrawnTo(reporter, dev, shouldBeDrawn);
215 * Original bug was asserting that the matrix-proc had generated a (Y) value
216 * that was out of range. This led (in the release build) to the sampler-proc
217 * reading memory out-of-bounds of the original bitmap.
219 * We were numerically overflowing our 16bit coordinates that we communicate
220 * between these two procs. The fixes was in two parts:
222 * 1. Just don't draw bitmaps larger than 64K-1 in width or height, since we
223 * can't represent those coordinates in our transport format (yet).
224 * 2. Perform an unsigned shift during the calculation, so we don't get
225 * sign-extension bleed when packing the two values (X,Y) into our 32bit
228 * This tests exercises the original setup, plus 3 more to ensure that we can,
229 * in fact, handle bitmaps at 64K-1 (assuming we don't exceed the total
230 * memory allocation limit).
232 static void test_giantrepeat_crbug118018(skiatest::Reporter* reporter) {
233 static const struct {
236 bool fExpectedToDraw;
238 { 0x1b294, 0x7f, false }, // crbug 118018 (width exceeds 64K)
239 { 0xFFFF, 0x7f, true }, // should draw, test max width
240 { 0x7f, 0xFFFF, true }, // should draw, test max height
241 { 0xFFFF, 0xFFFF, false }, // allocation fails (too much RAM)
244 for (size_t i = 0; i < SK_ARRAY_COUNT(gTests); ++i) {
245 test_wacky_bitmapshader(reporter,
246 gTests[i].fWidth, gTests[i].fHeight,
247 gTests[i].fExpectedToDraw);
251 ///////////////////////////////////////////////////////////////////////////////
253 static void test_nan_antihair() {
255 bm.allocN32Pixels(20, 20);
261 path.lineTo(10, SK_ScalarNaN);
264 paint.setAntiAlias(true);
265 paint.setStyle(SkPaint::kStroke_Style);
267 // before our fix to SkScan_Antihair.cpp to check for integral NaN (0x800...)
268 // this would trigger an assert/crash.
271 canvas.drawPath(path, paint);
274 static bool check_for_all_zeros(const SkBitmap& bm) {
275 SkAutoLockPixels alp(bm);
277 size_t count = bm.width() * bm.bytesPerPixel();
278 for (int y = 0; y < bm.height(); y++) {
279 const uint8_t* ptr = reinterpret_cast<const uint8_t*>(bm.getAddr(0, y));
280 for (size_t i = 0; i < count; i++) {
289 static const int gWidth = 256;
290 static const int gHeight = 256;
292 static void create(SkBitmap* bm, SkColor color) {
293 bm->allocN32Pixels(gWidth, gHeight);
294 bm->eraseColor(color);
297 DEF_TEST(DrawBitmapRect, reporter) {
300 create(&src, 0xFFFFFFFF);
303 SkCanvas canvas(dst);
305 SkIRect srcR = { gWidth, 0, gWidth + 16, 16 };
306 SkRect dstR = { 0, 0, SkIntToScalar(16), SkIntToScalar(16) };
308 canvas.drawBitmapRect(src, &srcR, dstR, NULL);
310 // ensure that we draw nothing if srcR does not intersect the bitmap
311 REPORTER_ASSERT(reporter, check_for_all_zeros(dst));
314 test_giantrepeat_crbug118018(reporter);
316 test_treatAsSprite(reporter);
317 test_faulty_pixelref(reporter);