blob: 7dc51c04ef8c519956da041aa169f7f1517c6a52 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
2/*
3 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
reed@google.com2ade0862011-03-17 17:48:04 +00008#include "Test.h"
9#include "SkBitmap.h"
10#include "SkCanvas.h"
reed@google.com99c114e2012-05-03 20:14:26 +000011#include "SkShader.h"
12
13static void assert_ifDrawnTo(skiatest::Reporter* reporter,
14 const SkBitmap& bm, bool shouldBeDrawn) {
15 for (int y = 0; y < bm.height(); ++y) {
16 for (int x = 0; x < bm.width(); ++x) {
17 if (shouldBeDrawn) {
18 if (0 == *bm.getAddr32(x, y)) {
19 REPORTER_ASSERT(reporter, false);
20 return;
21 }
22 } else {
23 // should not be drawn
24 if (*bm.getAddr32(x, y)) {
25 REPORTER_ASSERT(reporter, false);
26 return;
27 }
28 }
29 }
30 }
31}
32
33static void test_wacky_bitmapshader(skiatest::Reporter* reporter,
34 int width, int height, bool shouldBeDrawn) {
35 SkBitmap dev;
36 dev.setConfig(SkBitmap::kARGB_8888_Config, 0x56F, 0x4f6);
37 dev.allocPixels();
junov@google.comdbfac8a2012-12-06 21:47:40 +000038 dev.eraseColor(SK_ColorTRANSPARENT); // necessary, so we know if we draw to it
rmistry@google.comd6176b02012-08-23 18:14:13 +000039
reed@google.com99c114e2012-05-03 20:14:26 +000040 SkMatrix matrix;
rmistry@google.comd6176b02012-08-23 18:14:13 +000041
reed@google.com99c114e2012-05-03 20:14:26 +000042 SkCanvas c(dev);
rmistry@google.comd6176b02012-08-23 18:14:13 +000043 matrix.setAll(SkFloatToScalar(-119.34097f),
44 SkFloatToScalar(-43.436558f),
robertphillips@google.com4debcac2012-05-14 16:33:36 +000045 SkFloatToScalar(93489.945f),
rmistry@google.comd6176b02012-08-23 18:14:13 +000046 SkFloatToScalar(43.436558f),
47 SkFloatToScalar(-119.34097f),
robertphillips@google.com4debcac2012-05-14 16:33:36 +000048 SkFloatToScalar(123.98426f),
49 0, 0, SK_Scalar1);
reed@google.com99c114e2012-05-03 20:14:26 +000050 c.concat(matrix);
rmistry@google.comd6176b02012-08-23 18:14:13 +000051
reed@google.com99c114e2012-05-03 20:14:26 +000052 SkBitmap bm;
53 bm.setConfig(SkBitmap::kARGB_8888_Config, width, height);
54 bm.allocPixels();
55 bm.eraseColor(SK_ColorRED);
rmistry@google.comd6176b02012-08-23 18:14:13 +000056
reed@google.com99c114e2012-05-03 20:14:26 +000057 SkShader* s = SkShader::CreateBitmapShader(bm, SkShader::kRepeat_TileMode,
58 SkShader::kRepeat_TileMode);
rmistry@google.comd6176b02012-08-23 18:14:13 +000059 matrix.setAll(SkFloatToScalar(0.0078740157f),
60 0,
robertphillips@google.com4debcac2012-05-14 16:33:36 +000061 SkIntToScalar(249),
rmistry@google.comd6176b02012-08-23 18:14:13 +000062 0,
63 SkFloatToScalar(0.0078740157f),
robertphillips@google.com4debcac2012-05-14 16:33:36 +000064 SkIntToScalar(239),
65 0, 0, SK_Scalar1);
reed@google.com99c114e2012-05-03 20:14:26 +000066 s->setLocalMatrix(matrix);
rmistry@google.comd6176b02012-08-23 18:14:13 +000067
reed@google.com99c114e2012-05-03 20:14:26 +000068 SkPaint paint;
69 paint.setShader(s)->unref();
rmistry@google.comd6176b02012-08-23 18:14:13 +000070
reed@google.com99c114e2012-05-03 20:14:26 +000071 SkRect r = SkRect::MakeXYWH(681, 239, 695, 253);
72 c.drawRect(r, paint);
rmistry@google.comd6176b02012-08-23 18:14:13 +000073
reed@google.com99c114e2012-05-03 20:14:26 +000074 assert_ifDrawnTo(reporter, dev, shouldBeDrawn);
75}
76
77/*
78 * Original bug was asserting that the matrix-proc had generated a (Y) value
79 * that was out of range. This led (in the release build) to the sampler-proc
80 * reading memory out-of-bounds of the original bitmap.
81 *
82 * We were numerically overflowing our 16bit coordinates that we communicate
83 * between these two procs. The fixes was in two parts:
84 *
85 * 1. Just don't draw bitmaps larger than 64K-1 in width or height, since we
86 * can't represent those coordinates in our transport format (yet).
87 * 2. Perform an unsigned shift during the calculation, so we don't get
88 * sign-extension bleed when packing the two values (X,Y) into our 32bit
89 * slot.
90 *
91 * This tests exercises the original setup, plus 3 more to ensure that we can,
92 * in fact, handle bitmaps at 64K-1 (assuming we don't exceed the total
93 * memory allocation limit).
94 */
95static void test_giantrepeat_crbug118018(skiatest::Reporter* reporter) {
reed@google.com0da06272012-05-03 20:26:06 +000096#ifdef SK_SCALAR_IS_FLOAT
reed@google.com99c114e2012-05-03 20:14:26 +000097 static const struct {
98 int fWidth;
99 int fHeight;
100 bool fExpectedToDraw;
101 } gTests[] = {
102 { 0x1b294, 0x7f, false }, // crbug 118018 (width exceeds 64K)
103 { 0xFFFF, 0x7f, true }, // should draw, test max width
104 { 0x7f, 0xFFFF, true }, // should draw, test max height
105 { 0xFFFF, 0xFFFF, false }, // allocation fails (too much RAM)
106 };
rmistry@google.comd6176b02012-08-23 18:14:13 +0000107
reed@google.com99c114e2012-05-03 20:14:26 +0000108 for (size_t i = 0; i < SK_ARRAY_COUNT(gTests); ++i) {
109 test_wacky_bitmapshader(reporter,
110 gTests[i].fWidth, gTests[i].fHeight,
111 gTests[i].fExpectedToDraw);
112 }
reed@google.com0da06272012-05-03 20:26:06 +0000113#endif
reed@google.com99c114e2012-05-03 20:14:26 +0000114}
reed@google.com0da06272012-05-03 20:26:06 +0000115
reed@google.com99c114e2012-05-03 20:14:26 +0000116///////////////////////////////////////////////////////////////////////////////
reed@google.com2ade0862011-03-17 17:48:04 +0000117
reed@google.com6de0bfc2012-03-30 17:43:33 +0000118static void test_nan_antihair(skiatest::Reporter* reporter) {
119 SkBitmap bm;
120 bm.setConfig(SkBitmap::kARGB_8888_Config, 20, 20);
121 bm.allocPixels();
122
123 SkCanvas canvas(bm);
124
125 SkPath path;
126 path.moveTo(0, 0);
127 path.lineTo(10, SK_ScalarNaN);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000128
reed@google.com6de0bfc2012-03-30 17:43:33 +0000129 SkPaint paint;
130 paint.setAntiAlias(true);
131 paint.setStyle(SkPaint::kStroke_Style);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000132
reed@google.com6de0bfc2012-03-30 17:43:33 +0000133 // before our fix to SkScan_Antihair.cpp to check for integral NaN (0x800...)
134 // this would trigger an assert/crash.
135 //
136 // see rev. 3558
137 canvas.drawPath(path, paint);
138}
139
reed@google.com2ade0862011-03-17 17:48:04 +0000140static bool check_for_all_zeros(const SkBitmap& bm) {
141 SkAutoLockPixels alp(bm);
142
143 size_t count = bm.width() * bm.bytesPerPixel();
144 for (int y = 0; y < bm.height(); y++) {
145 const uint8_t* ptr = reinterpret_cast<const uint8_t*>(bm.getAddr(0, y));
146 for (size_t i = 0; i < count; i++) {
147 if (ptr[i]) {
148 return false;
149 }
150 }
151 }
152 return true;
153}
154
155static const int gWidth = 256;
156static const int gHeight = 256;
157
158static void create(SkBitmap* bm, SkBitmap::Config config, SkColor color) {
159 bm->setConfig(config, gWidth, gHeight);
160 bm->allocPixels();
161 bm->eraseColor(color);
162}
163
164static void TestDrawBitmapRect(skiatest::Reporter* reporter) {
165 SkBitmap src, dst;
166
167 create(&src, SkBitmap::kARGB_8888_Config, 0xFFFFFFFF);
168 create(&dst, SkBitmap::kARGB_8888_Config, 0);
169
170 SkCanvas canvas(dst);
171
172 SkIRect srcR = { gWidth, 0, gWidth + 16, 16 };
173 SkRect dstR = { 0, 0, SkIntToScalar(16), SkIntToScalar(16) };
174
175 canvas.drawBitmapRect(src, &srcR, dstR, NULL);
176
177 // ensure that we draw nothing if srcR does not intersect the bitmap
178 REPORTER_ASSERT(reporter, check_for_all_zeros(dst));
reed@google.com6de0bfc2012-03-30 17:43:33 +0000179
180 test_nan_antihair(reporter);
reed@google.com99c114e2012-05-03 20:14:26 +0000181 test_giantrepeat_crbug118018(reporter);
reed@google.com2ade0862011-03-17 17:48:04 +0000182}
183
184#include "TestClassDef.h"
185DEFINE_TESTCLASS("DrawBitmapRect", TestDrawBitmapRectClass, TestDrawBitmapRect)