blob: 0147a71fafcbe145a2a2c0d51bd1f4c4bf0eac58 [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
reed@google.com0da06272012-05-03 20:26:06 +000013#ifdef SK_SCALAR_IS_FLOAT
reed@google.com99c114e2012-05-03 20:14:26 +000014static void assert_ifDrawnTo(skiatest::Reporter* reporter,
15 const SkBitmap& bm, bool shouldBeDrawn) {
16 for (int y = 0; y < bm.height(); ++y) {
17 for (int x = 0; x < bm.width(); ++x) {
18 if (shouldBeDrawn) {
19 if (0 == *bm.getAddr32(x, y)) {
20 REPORTER_ASSERT(reporter, false);
21 return;
22 }
23 } else {
24 // should not be drawn
25 if (*bm.getAddr32(x, y)) {
26 REPORTER_ASSERT(reporter, false);
27 return;
28 }
29 }
30 }
31 }
32}
33
34static void test_wacky_bitmapshader(skiatest::Reporter* reporter,
35 int width, int height, bool shouldBeDrawn) {
36 SkBitmap dev;
37 dev.setConfig(SkBitmap::kARGB_8888_Config, 0x56F, 0x4f6);
38 dev.allocPixels();
39 dev.eraseColor(0); // necessary, so we know if we draw to it
rmistry@google.comd6176b02012-08-23 18:14:13 +000040
reed@google.com99c114e2012-05-03 20:14:26 +000041 SkMatrix matrix;
rmistry@google.comd6176b02012-08-23 18:14:13 +000042
reed@google.com99c114e2012-05-03 20:14:26 +000043 SkCanvas c(dev);
rmistry@google.comd6176b02012-08-23 18:14:13 +000044 matrix.setAll(SkFloatToScalar(-119.34097f),
45 SkFloatToScalar(-43.436558f),
robertphillips@google.com4debcac2012-05-14 16:33:36 +000046 SkFloatToScalar(93489.945f),
rmistry@google.comd6176b02012-08-23 18:14:13 +000047 SkFloatToScalar(43.436558f),
48 SkFloatToScalar(-119.34097f),
robertphillips@google.com4debcac2012-05-14 16:33:36 +000049 SkFloatToScalar(123.98426f),
50 0, 0, SK_Scalar1);
reed@google.com99c114e2012-05-03 20:14:26 +000051 c.concat(matrix);
rmistry@google.comd6176b02012-08-23 18:14:13 +000052
reed@google.com99c114e2012-05-03 20:14:26 +000053 SkBitmap bm;
54 bm.setConfig(SkBitmap::kARGB_8888_Config, width, height);
55 bm.allocPixels();
56 bm.eraseColor(SK_ColorRED);
rmistry@google.comd6176b02012-08-23 18:14:13 +000057
reed@google.com99c114e2012-05-03 20:14:26 +000058 SkShader* s = SkShader::CreateBitmapShader(bm, SkShader::kRepeat_TileMode,
59 SkShader::kRepeat_TileMode);
rmistry@google.comd6176b02012-08-23 18:14:13 +000060 matrix.setAll(SkFloatToScalar(0.0078740157f),
61 0,
robertphillips@google.com4debcac2012-05-14 16:33:36 +000062 SkIntToScalar(249),
rmistry@google.comd6176b02012-08-23 18:14:13 +000063 0,
64 SkFloatToScalar(0.0078740157f),
robertphillips@google.com4debcac2012-05-14 16:33:36 +000065 SkIntToScalar(239),
66 0, 0, SK_Scalar1);
reed@google.com99c114e2012-05-03 20:14:26 +000067 s->setLocalMatrix(matrix);
rmistry@google.comd6176b02012-08-23 18:14:13 +000068
reed@google.com99c114e2012-05-03 20:14:26 +000069 SkPaint paint;
70 paint.setShader(s)->unref();
rmistry@google.comd6176b02012-08-23 18:14:13 +000071
reed@google.com99c114e2012-05-03 20:14:26 +000072 SkRect r = SkRect::MakeXYWH(681, 239, 695, 253);
73 c.drawRect(r, paint);
rmistry@google.comd6176b02012-08-23 18:14:13 +000074
reed@google.com99c114e2012-05-03 20:14:26 +000075 assert_ifDrawnTo(reporter, dev, shouldBeDrawn);
76}
reed@google.com0da06272012-05-03 20:26:06 +000077#endif
reed@google.com99c114e2012-05-03 20:14:26 +000078
79/*
80 * Original bug was asserting that the matrix-proc had generated a (Y) value
81 * that was out of range. This led (in the release build) to the sampler-proc
82 * reading memory out-of-bounds of the original bitmap.
83 *
84 * We were numerically overflowing our 16bit coordinates that we communicate
85 * between these two procs. The fixes was in two parts:
86 *
87 * 1. Just don't draw bitmaps larger than 64K-1 in width or height, since we
88 * can't represent those coordinates in our transport format (yet).
89 * 2. Perform an unsigned shift during the calculation, so we don't get
90 * sign-extension bleed when packing the two values (X,Y) into our 32bit
91 * slot.
92 *
93 * This tests exercises the original setup, plus 3 more to ensure that we can,
94 * in fact, handle bitmaps at 64K-1 (assuming we don't exceed the total
95 * memory allocation limit).
96 */
97static void test_giantrepeat_crbug118018(skiatest::Reporter* reporter) {
reed@google.com0da06272012-05-03 20:26:06 +000098#ifdef SK_SCALAR_IS_FLOAT
reed@google.com99c114e2012-05-03 20:14:26 +000099 static const struct {
100 int fWidth;
101 int fHeight;
102 bool fExpectedToDraw;
103 } gTests[] = {
104 { 0x1b294, 0x7f, false }, // crbug 118018 (width exceeds 64K)
105 { 0xFFFF, 0x7f, true }, // should draw, test max width
106 { 0x7f, 0xFFFF, true }, // should draw, test max height
107 { 0xFFFF, 0xFFFF, false }, // allocation fails (too much RAM)
108 };
rmistry@google.comd6176b02012-08-23 18:14:13 +0000109
reed@google.com99c114e2012-05-03 20:14:26 +0000110 for (size_t i = 0; i < SK_ARRAY_COUNT(gTests); ++i) {
111 test_wacky_bitmapshader(reporter,
112 gTests[i].fWidth, gTests[i].fHeight,
113 gTests[i].fExpectedToDraw);
114 }
reed@google.com0da06272012-05-03 20:26:06 +0000115#endif
reed@google.com99c114e2012-05-03 20:14:26 +0000116}
reed@google.com0da06272012-05-03 20:26:06 +0000117
reed@google.com99c114e2012-05-03 20:14:26 +0000118///////////////////////////////////////////////////////////////////////////////
reed@google.com2ade0862011-03-17 17:48:04 +0000119
reed@google.com6de0bfc2012-03-30 17:43:33 +0000120static void test_nan_antihair(skiatest::Reporter* reporter) {
121 SkBitmap bm;
122 bm.setConfig(SkBitmap::kARGB_8888_Config, 20, 20);
123 bm.allocPixels();
124
125 SkCanvas canvas(bm);
126
127 SkPath path;
128 path.moveTo(0, 0);
129 path.lineTo(10, SK_ScalarNaN);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000130
reed@google.com6de0bfc2012-03-30 17:43:33 +0000131 SkPaint paint;
132 paint.setAntiAlias(true);
133 paint.setStyle(SkPaint::kStroke_Style);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000134
reed@google.com6de0bfc2012-03-30 17:43:33 +0000135 // before our fix to SkScan_Antihair.cpp to check for integral NaN (0x800...)
136 // this would trigger an assert/crash.
137 //
138 // see rev. 3558
139 canvas.drawPath(path, paint);
140}
141
reed@google.com2ade0862011-03-17 17:48:04 +0000142static bool check_for_all_zeros(const SkBitmap& bm) {
143 SkAutoLockPixels alp(bm);
144
145 size_t count = bm.width() * bm.bytesPerPixel();
146 for (int y = 0; y < bm.height(); y++) {
147 const uint8_t* ptr = reinterpret_cast<const uint8_t*>(bm.getAddr(0, y));
148 for (size_t i = 0; i < count; i++) {
149 if (ptr[i]) {
150 return false;
151 }
152 }
153 }
154 return true;
155}
156
157static const int gWidth = 256;
158static const int gHeight = 256;
159
160static void create(SkBitmap* bm, SkBitmap::Config config, SkColor color) {
161 bm->setConfig(config, gWidth, gHeight);
162 bm->allocPixels();
163 bm->eraseColor(color);
164}
165
166static void TestDrawBitmapRect(skiatest::Reporter* reporter) {
167 SkBitmap src, dst;
168
169 create(&src, SkBitmap::kARGB_8888_Config, 0xFFFFFFFF);
170 create(&dst, SkBitmap::kARGB_8888_Config, 0);
171
172 SkCanvas canvas(dst);
173
174 SkIRect srcR = { gWidth, 0, gWidth + 16, 16 };
175 SkRect dstR = { 0, 0, SkIntToScalar(16), SkIntToScalar(16) };
176
177 canvas.drawBitmapRect(src, &srcR, dstR, NULL);
178
179 // ensure that we draw nothing if srcR does not intersect the bitmap
180 REPORTER_ASSERT(reporter, check_for_all_zeros(dst));
reed@google.com6de0bfc2012-03-30 17:43:33 +0000181
182 test_nan_antihair(reporter);
reed@google.com99c114e2012-05-03 20:14:26 +0000183 test_giantrepeat_crbug118018(reporter);
reed@google.com2ade0862011-03-17 17:48:04 +0000184}
185
186#include "TestClassDef.h"
187DEFINE_TESTCLASS("DrawBitmapRect", TestDrawBitmapRectClass, TestDrawBitmapRect)