blob: ec6fa12bbb16172ab0bff314594c7d434ecb0894 [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();
38 dev.eraseColor(0); // necessary, so we know if we draw to it
39
40 SkMatrix matrix;
41
42 SkCanvas c(dev);
43 matrix.setAll(-119.34097, -43.436558, 93489.945,
44 43.436558, -119.34097, 123.98426,
45 0, 0, 1);
46 c.concat(matrix);
47
48 SkBitmap bm;
49 bm.setConfig(SkBitmap::kARGB_8888_Config, width, height);
50 bm.allocPixels();
51 bm.eraseColor(SK_ColorRED);
52
53 SkShader* s = SkShader::CreateBitmapShader(bm, SkShader::kRepeat_TileMode,
54 SkShader::kRepeat_TileMode);
55 matrix.setAll(0.0078740157, 0, 249,
56 0, 0.0078740157, 239,
57 0, 0, 1);
58 s->setLocalMatrix(matrix);
59
60 SkPaint paint;
61 paint.setShader(s)->unref();
62
63 SkRect r = SkRect::MakeXYWH(681, 239, 695, 253);
64 c.drawRect(r, paint);
65
66 assert_ifDrawnTo(reporter, dev, shouldBeDrawn);
67}
68
69/*
70 * Original bug was asserting that the matrix-proc had generated a (Y) value
71 * that was out of range. This led (in the release build) to the sampler-proc
72 * reading memory out-of-bounds of the original bitmap.
73 *
74 * We were numerically overflowing our 16bit coordinates that we communicate
75 * between these two procs. The fixes was in two parts:
76 *
77 * 1. Just don't draw bitmaps larger than 64K-1 in width or height, since we
78 * can't represent those coordinates in our transport format (yet).
79 * 2. Perform an unsigned shift during the calculation, so we don't get
80 * sign-extension bleed when packing the two values (X,Y) into our 32bit
81 * slot.
82 *
83 * This tests exercises the original setup, plus 3 more to ensure that we can,
84 * in fact, handle bitmaps at 64K-1 (assuming we don't exceed the total
85 * memory allocation limit).
86 */
87static void test_giantrepeat_crbug118018(skiatest::Reporter* reporter) {
88 static const struct {
89 int fWidth;
90 int fHeight;
91 bool fExpectedToDraw;
92 } gTests[] = {
93 { 0x1b294, 0x7f, false }, // crbug 118018 (width exceeds 64K)
94 { 0xFFFF, 0x7f, true }, // should draw, test max width
95 { 0x7f, 0xFFFF, true }, // should draw, test max height
96 { 0xFFFF, 0xFFFF, false }, // allocation fails (too much RAM)
97 };
98
99 for (size_t i = 0; i < SK_ARRAY_COUNT(gTests); ++i) {
100 test_wacky_bitmapshader(reporter,
101 gTests[i].fWidth, gTests[i].fHeight,
102 gTests[i].fExpectedToDraw);
103 }
104}
105///////////////////////////////////////////////////////////////////////////////
reed@google.com2ade0862011-03-17 17:48:04 +0000106
reed@google.com6de0bfc2012-03-30 17:43:33 +0000107static void test_nan_antihair(skiatest::Reporter* reporter) {
108 SkBitmap bm;
109 bm.setConfig(SkBitmap::kARGB_8888_Config, 20, 20);
110 bm.allocPixels();
111
112 SkCanvas canvas(bm);
113
114 SkPath path;
115 path.moveTo(0, 0);
116 path.lineTo(10, SK_ScalarNaN);
117
118 SkPaint paint;
119 paint.setAntiAlias(true);
120 paint.setStyle(SkPaint::kStroke_Style);
121
122 // before our fix to SkScan_Antihair.cpp to check for integral NaN (0x800...)
123 // this would trigger an assert/crash.
124 //
125 // see rev. 3558
126 canvas.drawPath(path, paint);
127}
128
reed@google.com2ade0862011-03-17 17:48:04 +0000129static bool check_for_all_zeros(const SkBitmap& bm) {
130 SkAutoLockPixels alp(bm);
131
132 size_t count = bm.width() * bm.bytesPerPixel();
133 for (int y = 0; y < bm.height(); y++) {
134 const uint8_t* ptr = reinterpret_cast<const uint8_t*>(bm.getAddr(0, y));
135 for (size_t i = 0; i < count; i++) {
136 if (ptr[i]) {
137 return false;
138 }
139 }
140 }
141 return true;
142}
143
144static const int gWidth = 256;
145static const int gHeight = 256;
146
147static void create(SkBitmap* bm, SkBitmap::Config config, SkColor color) {
148 bm->setConfig(config, gWidth, gHeight);
149 bm->allocPixels();
150 bm->eraseColor(color);
151}
152
153static void TestDrawBitmapRect(skiatest::Reporter* reporter) {
154 SkBitmap src, dst;
155
156 create(&src, SkBitmap::kARGB_8888_Config, 0xFFFFFFFF);
157 create(&dst, SkBitmap::kARGB_8888_Config, 0);
158
159 SkCanvas canvas(dst);
160
161 SkIRect srcR = { gWidth, 0, gWidth + 16, 16 };
162 SkRect dstR = { 0, 0, SkIntToScalar(16), SkIntToScalar(16) };
163
164 canvas.drawBitmapRect(src, &srcR, dstR, NULL);
165
166 // ensure that we draw nothing if srcR does not intersect the bitmap
167 REPORTER_ASSERT(reporter, check_for_all_zeros(dst));
reed@google.com6de0bfc2012-03-30 17:43:33 +0000168
169 test_nan_antihair(reporter);
reed@google.com99c114e2012-05-03 20:14:26 +0000170 test_giantrepeat_crbug118018(reporter);
reed@google.com2ade0862011-03-17 17:48:04 +0000171}
172
173#include "TestClassDef.h"
174DEFINE_TESTCLASS("DrawBitmapRect", TestDrawBitmapRectClass, TestDrawBitmapRect)