blob: 639c51a5f1e95666f037a4748578fba5337f3090 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
2 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
commit-bot@chromium.org8ef51b92014-03-05 13:43:15 +00007
reed@android.com42263962009-05-01 04:00:01 +00008#include "SkBitmap.h"
bsalomon614d8f92016-07-13 15:42:40 -07009#include "SkGrPriv.h"
reed@android.com311c82d2009-05-05 23:13:23 +000010#include "SkRect.h"
scroggo565901d2015-12-10 10:44:13 -080011#include "SkTemplates.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000012#include "Test.h"
reed@android.com42263962009-05-01 04:00:01 +000013
14static const char* boolStr(bool value) {
15 return value ? "true" : "false";
16}
17
commit-bot@chromium.org8ef51b92014-03-05 13:43:15 +000018// these are in the same order as the SkColorType enum
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +000019static const char* gColorTypeName[] = {
20 "None", "A8", "565", "4444", "RGBA", "BGRA", "Index8"
reed@android.com42263962009-05-01 04:00:01 +000021};
22
reed@android.comcafc9f92009-08-22 03:44:57 +000023static void report_opaqueness(skiatest::Reporter* reporter, const SkBitmap& src,
24 const SkBitmap& dst) {
halcanary@google.coma9325fa2014-01-10 14:58:10 +000025 ERRORF(reporter, "src %s opaque:%d, dst %s opaque:%d",
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +000026 gColorTypeName[src.colorType()], src.isOpaque(),
27 gColorTypeName[dst.colorType()], dst.isOpaque());
reed@android.comcafc9f92009-08-22 03:44:57 +000028}
29
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +000030static bool canHaveAlpha(SkColorType ct) {
31 return kRGB_565_SkColorType != ct;
reed@android.comcafc9f92009-08-22 03:44:57 +000032}
33
34// copyTo() should preserve isOpaque when it makes sense
reed@google.com0a6151d2013-10-10 14:44:56 +000035static void test_isOpaque(skiatest::Reporter* reporter,
36 const SkBitmap& srcOpaque, const SkBitmap& srcPremul,
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +000037 SkColorType dstColorType) {
reed@android.comcafc9f92009-08-22 03:44:57 +000038 SkBitmap dst;
39
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +000040 if (canHaveAlpha(srcPremul.colorType()) && canHaveAlpha(dstColorType)) {
41 REPORTER_ASSERT(reporter, srcPremul.copyTo(&dst, dstColorType));
42 REPORTER_ASSERT(reporter, dst.colorType() == dstColorType);
reed@google.com0a6151d2013-10-10 14:44:56 +000043 if (srcPremul.isOpaque() != dst.isOpaque()) {
44 report_opaqueness(reporter, srcPremul, dst);
reed@android.comcafc9f92009-08-22 03:44:57 +000045 }
46 }
47
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +000048 REPORTER_ASSERT(reporter, srcOpaque.copyTo(&dst, dstColorType));
49 REPORTER_ASSERT(reporter, dst.colorType() == dstColorType);
reed@google.com0a6151d2013-10-10 14:44:56 +000050 if (srcOpaque.isOpaque() != dst.isOpaque()) {
51 report_opaqueness(reporter, srcOpaque, dst);
reed@android.comcafc9f92009-08-22 03:44:57 +000052 }
53}
54
reed@google.com0a6151d2013-10-10 14:44:56 +000055static void init_src(const SkBitmap& bitmap) {
weita@google.comf9ab99a2009-05-03 18:23:30 +000056 SkAutoLockPixels lock(bitmap);
reed@android.com42263962009-05-01 04:00:01 +000057 if (bitmap.getPixels()) {
reed@google.com0a6151d2013-10-10 14:44:56 +000058 if (bitmap.getColorTable()) {
reed@google.com9ce6e752011-01-10 14:04:07 +000059 sk_bzero(bitmap.getPixels(), bitmap.getSize());
60 } else {
61 bitmap.eraseColor(SK_ColorWHITE);
62 }
reed@android.com42263962009-05-01 04:00:01 +000063 }
64}
65
reedc5e15a12014-09-29 12:10:27 -070066static SkColorTable* init_ctable() {
reed@android.com42263962009-05-01 04:00:01 +000067 static const SkColor colors[] = {
68 SK_ColorBLACK, SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE
69 };
reedc5e15a12014-09-29 12:10:27 -070070 return new SkColorTable(colors, SK_ARRAY_COUNT(colors));
reed@android.com42263962009-05-01 04:00:01 +000071}
72
73struct Pair {
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +000074 SkColorType fColorType;
75 const char* fValid;
reed@android.com42263962009-05-01 04:00:01 +000076};
77
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +000078// Utility functions for copyPixelsTo()/copyPixelsFrom() tests.
79// getPixel()
80// setPixel()
81// getSkConfigName()
82// struct Coordinates
83// reportCopyVerification()
84// writeCoordPixels()
85
86// Utility function to read the value of a given pixel in bm. All
87// values converted to uint32_t for simplification of comparisons.
caryclark@google.com42639cd2012-06-06 12:03:39 +000088static uint32_t getPixel(int x, int y, const SkBitmap& bm) {
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +000089 uint32_t val = 0;
90 uint16_t val16;
rmistry@google.comd6bab022013-12-02 13:50:38 +000091 uint8_t val8;
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +000092 SkAutoLockPixels lock(bm);
93 const void* rawAddr = bm.getAddr(x,y);
94
commit-bot@chromium.org8ef51b92014-03-05 13:43:15 +000095 switch (bm.bytesPerPixel()) {
96 case 4:
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +000097 memcpy(&val, rawAddr, sizeof(uint32_t));
98 break;
commit-bot@chromium.org8ef51b92014-03-05 13:43:15 +000099 case 2:
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000100 memcpy(&val16, rawAddr, sizeof(uint16_t));
101 val = val16;
102 break;
commit-bot@chromium.org8ef51b92014-03-05 13:43:15 +0000103 case 1:
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000104 memcpy(&val8, rawAddr, sizeof(uint8_t));
105 val = val8;
106 break;
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000107 default:
108 break;
109 }
110 return val;
111}
112
113// Utility function to set value of any pixel in bm.
114// bm.getConfig() specifies what format 'val' must be
115// converted to, but at present uint32_t can handle all formats.
caryclark@google.com42639cd2012-06-06 12:03:39 +0000116static void setPixel(int x, int y, uint32_t val, SkBitmap& bm) {
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000117 uint16_t val16;
rmistry@google.comd6bab022013-12-02 13:50:38 +0000118 uint8_t val8;
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000119 SkAutoLockPixels lock(bm);
120 void* rawAddr = bm.getAddr(x,y);
121
commit-bot@chromium.org8ef51b92014-03-05 13:43:15 +0000122 switch (bm.bytesPerPixel()) {
123 case 4:
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000124 memcpy(rawAddr, &val, sizeof(uint32_t));
125 break;
commit-bot@chromium.org8ef51b92014-03-05 13:43:15 +0000126 case 2:
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000127 val16 = val & 0xFFFF;
128 memcpy(rawAddr, &val16, sizeof(uint16_t));
129 break;
commit-bot@chromium.org8ef51b92014-03-05 13:43:15 +0000130 case 1:
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000131 val8 = val & 0xFF;
132 memcpy(rawAddr, &val8, sizeof(uint8_t));
133 break;
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000134 default:
135 // Ignore.
136 break;
137 }
138}
139
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000140// Helper struct to contain pixel locations, while avoiding need for STL.
141struct Coordinates {
142
143 const int length;
144 SkIPoint* const data;
145
146 explicit Coordinates(int _length): length(_length)
147 , data(new SkIPoint[length]) { }
148
149 ~Coordinates(){
150 delete [] data;
151 }
152
153 SkIPoint* operator[](int i) const {
154 // Use with care, no bounds checking.
155 return data + i;
156 }
157};
158
159// A function to verify that two bitmaps contain the same pixel values
160// at all coordinates indicated by coords. Simplifies verification of
161// copied bitmaps.
caryclark@google.com42639cd2012-06-06 12:03:39 +0000162static void reportCopyVerification(const SkBitmap& bm1, const SkBitmap& bm2,
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000163 Coordinates& coords,
164 const char* msg,
165 skiatest::Reporter* reporter){
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000166 // Confirm all pixels in the list match.
scroggo@google.comd5764e82012-08-22 15:00:05 +0000167 for (int i = 0; i < coords.length; ++i) {
reed92fc2ae2015-05-22 08:06:21 -0700168 uint32_t p1 = getPixel(coords[i]->fX, coords[i]->fY, bm1);
169 uint32_t p2 = getPixel(coords[i]->fX, coords[i]->fY, bm2);
170// SkDebugf("[%d] (%d %d) p1=%x p2=%x\n", i, coords[i]->fX, coords[i]->fY, p1, p2);
171 if (p1 != p2) {
172 ERRORF(reporter, "%s [colortype = %s]", msg, gColorTypeName[bm1.colorType()]);
173 break;
174 }
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000175 }
176}
177
178// Writes unique pixel values at locations specified by coords.
caryclark@google.com42639cd2012-06-06 12:03:39 +0000179static void writeCoordPixels(SkBitmap& bm, const Coordinates& coords) {
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000180 for (int i = 0; i < coords.length; ++i)
181 setPixel(coords[i]->fX, coords[i]->fY, i, bm);
182}
183
scroggo@google.com5ccae2c2014-01-15 16:56:52 +0000184static const Pair gPairs[] = {
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000185 { kUnknown_SkColorType, "000000" },
186 { kAlpha_8_SkColorType, "010101" },
reedb184f7f2014-07-13 04:32:32 -0700187 { kIndex_8_SkColorType, "011111" },
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000188 { kRGB_565_SkColorType, "010101" },
189 { kARGB_4444_SkColorType, "010111" },
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000190 { kN32_SkColorType, "010111" },
scroggo@google.com5ccae2c2014-01-15 16:56:52 +0000191};
commit-bot@chromium.org5c6f1d42014-01-10 18:28:23 +0000192
scroggo@google.com5ccae2c2014-01-15 16:56:52 +0000193static const int W = 20;
194static const int H = 33;
195
196static void setup_src_bitmaps(SkBitmap* srcOpaque, SkBitmap* srcPremul,
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000197 SkColorType ct) {
halcanary96fcdcc2015-08-27 07:41:13 -0700198 SkColorTable* ctable = nullptr;
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000199 if (kIndex_8_SkColorType == ct) {
reedc5e15a12014-09-29 12:10:27 -0700200 ctable = init_ctable();
scroggo@google.com5ccae2c2014-01-15 16:56:52 +0000201 }
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000202
203 srcOpaque->allocPixels(SkImageInfo::Make(W, H, ct, kOpaque_SkAlphaType),
halcanary96fcdcc2015-08-27 07:41:13 -0700204 nullptr, ctable);
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000205 srcPremul->allocPixels(SkImageInfo::Make(W, H, ct, kPremul_SkAlphaType),
halcanary96fcdcc2015-08-27 07:41:13 -0700206 nullptr, ctable);
reedc5e15a12014-09-29 12:10:27 -0700207 SkSafeUnref(ctable);
scroggo@google.com5ccae2c2014-01-15 16:56:52 +0000208 init_src(*srcOpaque);
209 init_src(*srcPremul);
210}
211
212DEF_TEST(BitmapCopy_extractSubset, reporter) {
213 for (size_t i = 0; i < SK_ARRAY_COUNT(gPairs); i++) {
214 SkBitmap srcOpaque, srcPremul;
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000215 setup_src_bitmaps(&srcOpaque, &srcPremul, gPairs[i].fColorType);
scroggo@google.com5ccae2c2014-01-15 16:56:52 +0000216
217 SkBitmap bitmap(srcOpaque);
218 SkBitmap subset;
219 SkIRect r;
220 // Extract a subset which has the same width as the original. This
221 // catches a bug where we cloned the genID incorrectly.
222 r.set(0, 1, W, 3);
223 bitmap.setIsVolatile(true);
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000224 // Relies on old behavior of extractSubset failing if colortype is unknown
225 if (kUnknown_SkColorType != bitmap.colorType() && bitmap.extractSubset(&subset, r)) {
scroggo@google.com5ccae2c2014-01-15 16:56:52 +0000226 REPORTER_ASSERT(reporter, subset.width() == W);
227 REPORTER_ASSERT(reporter, subset.height() == 2);
228 REPORTER_ASSERT(reporter, subset.alphaType() == bitmap.alphaType());
229 REPORTER_ASSERT(reporter, subset.isVolatile() == true);
230
231 // Test copying an extracted subset.
232 for (size_t j = 0; j < SK_ARRAY_COUNT(gPairs); j++) {
233 SkBitmap copy;
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000234 bool success = subset.copyTo(&copy, gPairs[j].fColorType);
scroggo@google.com5ccae2c2014-01-15 16:56:52 +0000235 if (!success) {
236 // Skip checking that success matches fValid, which is redundant
237 // with the code below.
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000238 REPORTER_ASSERT(reporter, gPairs[i].fColorType != gPairs[j].fColorType);
scroggo@google.com5ccae2c2014-01-15 16:56:52 +0000239 continue;
240 }
241
242 // When performing a copy of an extracted subset, the gen id should
243 // change.
244 REPORTER_ASSERT(reporter, copy.getGenerationID() != subset.getGenerationID());
245
246 REPORTER_ASSERT(reporter, copy.width() == W);
247 REPORTER_ASSERT(reporter, copy.height() == 2);
248
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000249 if (gPairs[i].fColorType == gPairs[j].fColorType) {
scroggo@google.com5ccae2c2014-01-15 16:56:52 +0000250 SkAutoLockPixels alp0(subset);
251 SkAutoLockPixels alp1(copy);
252 // they should both have, or both not-have, a colortable
halcanary96fcdcc2015-08-27 07:41:13 -0700253 bool hasCT = subset.getColorTable() != nullptr;
254 REPORTER_ASSERT(reporter, (copy.getColorTable() != nullptr) == hasCT);
scroggo@google.com5ccae2c2014-01-15 16:56:52 +0000255 }
256 }
257 }
258
259 bitmap = srcPremul;
260 bitmap.setIsVolatile(false);
261 if (bitmap.extractSubset(&subset, r)) {
262 REPORTER_ASSERT(reporter, subset.alphaType() == bitmap.alphaType());
263 REPORTER_ASSERT(reporter, subset.isVolatile() == false);
264 }
265 }
266}
267
268DEF_TEST(BitmapCopy, reporter) {
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000269 static const bool isExtracted[] = {
270 false, true
271 };
272
commit-bot@chromium.org5c6f1d42014-01-10 18:28:23 +0000273 for (size_t i = 0; i < SK_ARRAY_COUNT(gPairs); i++) {
scroggo@google.com5ccae2c2014-01-15 16:56:52 +0000274 SkBitmap srcOpaque, srcPremul;
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000275 setup_src_bitmaps(&srcOpaque, &srcPremul, gPairs[i].fColorType);
scroggo@google.com5ccae2c2014-01-15 16:56:52 +0000276
reed@android.com42263962009-05-01 04:00:01 +0000277 for (size_t j = 0; j < SK_ARRAY_COUNT(gPairs); j++) {
scroggo@google.com5ccae2c2014-01-15 16:56:52 +0000278 SkBitmap dst;
reed@android.com42263962009-05-01 04:00:01 +0000279
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000280 bool success = srcPremul.copyTo(&dst, gPairs[j].fColorType);
reed@android.com42263962009-05-01 04:00:01 +0000281 bool expected = gPairs[i].fValid[j] != '0';
282 if (success != expected) {
halcanary@google.coma9325fa2014-01-10 14:58:10 +0000283 ERRORF(reporter, "SkBitmap::copyTo from %s to %s. expected %s "
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000284 "returned %s", gColorTypeName[i], gColorTypeName[j],
halcanary@google.coma9325fa2014-01-10 14:58:10 +0000285 boolStr(expected), boolStr(success));
reed@android.com42263962009-05-01 04:00:01 +0000286 }
reed@google.com1fcd51e2011-01-05 15:50:27 +0000287
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000288 bool canSucceed = srcPremul.canCopyTo(gPairs[j].fColorType);
reed@android.comfbaa88d2009-05-06 17:44:34 +0000289 if (success != canSucceed) {
halcanary@google.coma9325fa2014-01-10 14:58:10 +0000290 ERRORF(reporter, "SkBitmap::copyTo from %s to %s. returned %s "
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000291 "canCopyTo %s", gColorTypeName[i], gColorTypeName[j],
halcanary@google.coma9325fa2014-01-10 14:58:10 +0000292 boolStr(success), boolStr(canSucceed));
reed@android.comfbaa88d2009-05-06 17:44:34 +0000293 }
weita@google.comf9ab99a2009-05-03 18:23:30 +0000294
reed@android.com42263962009-05-01 04:00:01 +0000295 if (success) {
reed@google.com0a6151d2013-10-10 14:44:56 +0000296 REPORTER_ASSERT(reporter, srcPremul.width() == dst.width());
297 REPORTER_ASSERT(reporter, srcPremul.height() == dst.height());
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000298 REPORTER_ASSERT(reporter, dst.colorType() == gPairs[j].fColorType);
299 test_isOpaque(reporter, srcOpaque, srcPremul, dst.colorType());
commit-bot@chromium.org8ef51b92014-03-05 13:43:15 +0000300 if (srcPremul.colorType() == dst.colorType()) {
reed@google.com0a6151d2013-10-10 14:44:56 +0000301 SkAutoLockPixels srcLock(srcPremul);
reed@android.com42263962009-05-01 04:00:01 +0000302 SkAutoLockPixels dstLock(dst);
reed@google.com0a6151d2013-10-10 14:44:56 +0000303 REPORTER_ASSERT(reporter, srcPremul.readyToDraw());
reed@android.com42263962009-05-01 04:00:01 +0000304 REPORTER_ASSERT(reporter, dst.readyToDraw());
reed@google.com0a6151d2013-10-10 14:44:56 +0000305 const char* srcP = (const char*)srcPremul.getAddr(0, 0);
reed@android.com42263962009-05-01 04:00:01 +0000306 const char* dstP = (const char*)dst.getAddr(0, 0);
307 REPORTER_ASSERT(reporter, srcP != dstP);
308 REPORTER_ASSERT(reporter, !memcmp(srcP, dstP,
reed@google.com0a6151d2013-10-10 14:44:56 +0000309 srcPremul.getSize()));
310 REPORTER_ASSERT(reporter, srcPremul.getGenerationID() == dst.getGenerationID());
scroggo@google.comd5764e82012-08-22 15:00:05 +0000311 } else {
reed@google.com0a6151d2013-10-10 14:44:56 +0000312 REPORTER_ASSERT(reporter, srcPremul.getGenerationID() != dst.getGenerationID());
reed@android.com42263962009-05-01 04:00:01 +0000313 }
reed@android.com42263962009-05-01 04:00:01 +0000314 } else {
315 // dst should be unchanged from its initial state
commit-bot@chromium.org8ef51b92014-03-05 13:43:15 +0000316 REPORTER_ASSERT(reporter, dst.colorType() == kUnknown_SkColorType);
reed@android.com42263962009-05-01 04:00:01 +0000317 REPORTER_ASSERT(reporter, dst.width() == 0);
318 REPORTER_ASSERT(reporter, dst.height() == 0);
319 }
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000320 } // for (size_t j = ...
321
322 // Tests for getSafeSize(), getSafeSize64(), copyPixelsTo(),
323 // copyPixelsFrom().
324 //
325 for (size_t copyCase = 0; copyCase < SK_ARRAY_COUNT(isExtracted);
326 ++copyCase) {
327 // Test copying to/from external buffer.
328 // Note: the tests below have hard-coded values ---
329 // Please take care if modifying.
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000330
reed@google.com2cb14802013-06-26 14:35:02 +0000331 // Tests for getSafeSize64().
332 // Test with a very large configuration without pixel buffer
333 // attached.
334 SkBitmap tstSafeSize;
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +0000335 tstSafeSize.setInfo(SkImageInfo::Make(100000000U, 100000000U,
336 gPairs[i].fColorType, kPremul_SkAlphaType));
reed@google.com57212f92013-12-30 14:40:38 +0000337 int64_t safeSize = tstSafeSize.computeSafeSize64();
338 if (safeSize < 0) {
halcanary@google.coma9325fa2014-01-10 14:58:10 +0000339 ERRORF(reporter, "getSafeSize64() negative: %s",
commit-bot@chromium.org8ef51b92014-03-05 13:43:15 +0000340 gColorTypeName[tstSafeSize.colorType()]);
reed@google.com2cb14802013-06-26 14:35:02 +0000341 }
342 bool sizeFail = false;
343 // Compare against hand-computed values.
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000344 switch (gPairs[i].fColorType) {
345 case kUnknown_SkColorType:
reed@google.com2cb14802013-06-26 14:35:02 +0000346 break;
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000347
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000348 case kAlpha_8_SkColorType:
349 case kIndex_8_SkColorType:
reed@google.com57212f92013-12-30 14:40:38 +0000350 if (safeSize != 0x2386F26FC10000LL) {
reed@google.com2cb14802013-06-26 14:35:02 +0000351 sizeFail = true;
reed@google.com01c41a52013-12-20 14:24:21 +0000352 }
reed@google.com2cb14802013-06-26 14:35:02 +0000353 break;
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000354
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000355 case kRGB_565_SkColorType:
356 case kARGB_4444_SkColorType:
reed@google.com57212f92013-12-30 14:40:38 +0000357 if (safeSize != 0x470DE4DF820000LL) {
reed@google.com2cb14802013-06-26 14:35:02 +0000358 sizeFail = true;
reed@google.com01c41a52013-12-20 14:24:21 +0000359 }
reed@google.com2cb14802013-06-26 14:35:02 +0000360 break;
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000361
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000362 case kN32_SkColorType:
reed@google.com57212f92013-12-30 14:40:38 +0000363 if (safeSize != 0x8E1BC9BF040000LL) {
reed@google.com2cb14802013-06-26 14:35:02 +0000364 sizeFail = true;
reed@google.com01c41a52013-12-20 14:24:21 +0000365 }
reed@google.com2cb14802013-06-26 14:35:02 +0000366 break;
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000367
reed@google.com2cb14802013-06-26 14:35:02 +0000368 default:
369 break;
370 }
371 if (sizeFail) {
halcanary@google.coma9325fa2014-01-10 14:58:10 +0000372 ERRORF(reporter, "computeSafeSize64() wrong size: %s",
commit-bot@chromium.org8ef51b92014-03-05 13:43:15 +0000373 gColorTypeName[tstSafeSize.colorType()]);
reed@google.com2cb14802013-06-26 14:35:02 +0000374 }
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000375
rmistry@google.comd6bab022013-12-02 13:50:38 +0000376 int subW = 2;
377 int subH = 2;
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000378
reed@google.com2cb14802013-06-26 14:35:02 +0000379 // Create bitmap to act as source for copies and subsets.
380 SkBitmap src, subset;
halcanary96fcdcc2015-08-27 07:41:13 -0700381 SkColorTable* ct = nullptr;
commit-bot@chromium.org8ef51b92014-03-05 13:43:15 +0000382 if (kIndex_8_SkColorType == src.colorType()) {
reedc5e15a12014-09-29 12:10:27 -0700383 ct = init_ctable();
reed@google.com2cb14802013-06-26 14:35:02 +0000384 }
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000385
reed84825042014-09-02 12:50:45 -0700386 int localSubW;
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000387 if (isExtracted[copyCase]) { // A larger image to extract from.
reed84825042014-09-02 12:50:45 -0700388 localSubW = 2 * subW + 1;
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000389 } else { // Tests expect a 2x2 bitmap, so make smaller.
reed84825042014-09-02 12:50:45 -0700390 localSubW = subW;
391 }
392 // could fail if we pass kIndex_8 for the colortype
393 if (src.tryAllocPixels(SkImageInfo::Make(localSubW, subH, gPairs[i].fColorType,
394 kPremul_SkAlphaType))) {
395 // failure is fine, as we will notice later on
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000396 }
reed@google.com2cb14802013-06-26 14:35:02 +0000397 SkSafeUnref(ct);
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000398
reed@google.com2cb14802013-06-26 14:35:02 +0000399 // Either copy src or extract into 'subset', which is used
400 // for subsequent calls to copyPixelsTo/From.
401 bool srcReady = false;
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000402 // Test relies on older behavior that extractSubset will fail on
commit-bot@chromium.org8ef51b92014-03-05 13:43:15 +0000403 // kUnknown_SkColorType
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000404 if (kUnknown_SkColorType != src.colorType() &&
405 isExtracted[copyCase]) {
reed@google.com2cb14802013-06-26 14:35:02 +0000406 // The extractedSubset() test case allows us to test copy-
407 // ing when src and dst mave possibly different strides.
408 SkIRect r;
rmistry@google.comd6bab022013-12-02 13:50:38 +0000409 r.set(1, 0, 1 + subW, subH); // 2x2 extracted bitmap
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000410
reed@google.com2cb14802013-06-26 14:35:02 +0000411 srcReady = src.extractSubset(&subset, r);
412 } else {
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000413 srcReady = src.copyTo(&subset);
reed@google.com2cb14802013-06-26 14:35:02 +0000414 }
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000415
reed@google.com2cb14802013-06-26 14:35:02 +0000416 // Not all configurations will generate a valid 'subset'.
417 if (srcReady) {
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000418
reed@google.com2cb14802013-06-26 14:35:02 +0000419 // Allocate our target buffer 'buf' for all copies.
420 // To simplify verifying correctness of copies attach
421 // buf to a SkBitmap, but copies are done using the
422 // raw buffer pointer.
robertphillips@google.come9cd27d2013-10-16 17:48:11 +0000423 const size_t bufSize = subH *
commit-bot@chromium.org8ef51b92014-03-05 13:43:15 +0000424 SkColorTypeMinRowBytes(src.colorType(), subW) * 2;
scroggo565901d2015-12-10 10:44:13 -0800425 SkAutoTMalloc<uint8_t> autoBuf (bufSize);
426 uint8_t* buf = autoBuf.get();
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000427
reed@google.com2cb14802013-06-26 14:35:02 +0000428 SkBitmap bufBm; // Attach buf to this bitmap.
429 bool successExpected;
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000430
reed@google.com2cb14802013-06-26 14:35:02 +0000431 // Set up values for each pixel being copied.
432 Coordinates coords(subW * subH);
robertphillips@google.come9cd27d2013-10-16 17:48:11 +0000433 for (int x = 0; x < subW; ++x)
434 for (int y = 0; y < subH; ++y)
reed@google.com2cb14802013-06-26 14:35:02 +0000435 {
436 int index = y * subW + x;
437 SkASSERT(index < coords.length);
438 coords[index]->fX = x;
439 coords[index]->fY = y;
440 }
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000441
reed@google.com2cb14802013-06-26 14:35:02 +0000442 writeCoordPixels(subset, coords);
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000443
reed@google.com2cb14802013-06-26 14:35:02 +0000444 // Test #1 ////////////////////////////////////////////
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000445
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000446 const SkImageInfo info = SkImageInfo::Make(subW, subH,
447 gPairs[i].fColorType,
448 kPremul_SkAlphaType);
reed@google.com2cb14802013-06-26 14:35:02 +0000449 // Before/after comparisons easier if we attach buf
450 // to an appropriately configured SkBitmap.
451 memset(buf, 0xFF, bufSize);
452 // Config with stride greater than src but that fits in buf.
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000453 bufBm.installPixels(info, buf, info.minRowBytes() * 2);
reed@google.com2cb14802013-06-26 14:35:02 +0000454 successExpected = false;
455 // Then attempt to copy with a stride that is too large
456 // to fit in the buffer.
457 REPORTER_ASSERT(reporter,
458 subset.copyPixelsTo(buf, bufSize, bufBm.rowBytes() * 3)
459 == successExpected);
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000460
reed@google.com2cb14802013-06-26 14:35:02 +0000461 if (successExpected)
462 reportCopyVerification(subset, bufBm, coords,
463 "copyPixelsTo(buf, bufSize, 1.5*maxRowBytes)",
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000464 reporter);
465
reed@google.com2cb14802013-06-26 14:35:02 +0000466 // Test #2 ////////////////////////////////////////////
467 // This test should always succeed, but in the case
468 // of extracted bitmaps only because we handle the
469 // issue of getSafeSize(). Without getSafeSize()
470 // buffer overrun/read would occur.
471 memset(buf, 0xFF, bufSize);
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000472 bufBm.installPixels(info, buf, subset.rowBytes());
reed@google.com2cb14802013-06-26 14:35:02 +0000473 successExpected = subset.getSafeSize() <= bufSize;
474 REPORTER_ASSERT(reporter,
475 subset.copyPixelsTo(buf, bufSize) ==
476 successExpected);
477 if (successExpected)
478 reportCopyVerification(subset, bufBm, coords,
479 "copyPixelsTo(buf, bufSize)", reporter);
480
481 // Test #3 ////////////////////////////////////////////
482 // Copy with different stride between src and dst.
483 memset(buf, 0xFF, bufSize);
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000484 bufBm.installPixels(info, buf, subset.rowBytes()+1);
reed@google.com2cb14802013-06-26 14:35:02 +0000485 successExpected = true; // Should always work.
486 REPORTER_ASSERT(reporter,
487 subset.copyPixelsTo(buf, bufSize,
488 subset.rowBytes()+1) == successExpected);
489 if (successExpected)
490 reportCopyVerification(subset, bufBm, coords,
491 "copyPixelsTo(buf, bufSize, rowBytes+1)", reporter);
492
493 // Test #4 ////////////////////////////////////////////
494 // Test copy with stride too small.
495 memset(buf, 0xFF, bufSize);
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000496 bufBm.installPixels(info, buf, info.minRowBytes());
reed@google.com2cb14802013-06-26 14:35:02 +0000497 successExpected = false;
498 // Request copy with stride too small.
499 REPORTER_ASSERT(reporter,
500 subset.copyPixelsTo(buf, bufSize, bufBm.rowBytes()-1)
501 == successExpected);
502 if (successExpected)
503 reportCopyVerification(subset, bufBm, coords,
504 "copyPixelsTo(buf, bufSize, rowBytes()-1)", reporter);
505
506#if 0 // copyPixelsFrom is gone
507 // Test #5 ////////////////////////////////////////////
508 // Tests the case where the source stride is too small
509 // for the source configuration.
510 memset(buf, 0xFF, bufSize);
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000511 bufBm.installPixels(info, buf, info.minRowBytes());
reed@google.com2cb14802013-06-26 14:35:02 +0000512 writeCoordPixels(bufBm, coords);
513 REPORTER_ASSERT(reporter,
514 subset.copyPixelsFrom(buf, bufSize, 1) == false);
515
516 // Test #6 ///////////////////////////////////////////
517 // Tests basic copy from an external buffer to the bitmap.
518 // If the bitmap is "extracted", this also tests the case
519 // where the source stride is different from the dest.
520 // stride.
521 // We've made the buffer large enough to always succeed.
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000522 bufBm.installPixels(info, buf, info.minRowBytes());
reed@google.com2cb14802013-06-26 14:35:02 +0000523 writeCoordPixels(bufBm, coords);
524 REPORTER_ASSERT(reporter,
525 subset.copyPixelsFrom(buf, bufSize, bufBm.rowBytes()) ==
526 true);
527 reportCopyVerification(bufBm, subset, coords,
528 "copyPixelsFrom(buf, bufSize)",
529 reporter);
530
531 // Test #7 ////////////////////////////////////////////
532 // Tests the case where the source buffer is too small
533 // for the transfer.
534 REPORTER_ASSERT(reporter,
535 subset.copyPixelsFrom(buf, 1, subset.rowBytes()) ==
536 false);
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000537
reed@google.comab77aaf2011-11-01 16:03:35 +0000538#endif
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000539 }
540 } // for (size_t copyCase ...
reed@android.com42263962009-05-01 04:00:01 +0000541 }
542}
reedb184f7f2014-07-13 04:32:32 -0700543
544#include "SkColorPriv.h"
545#include "SkUtils.h"
546
547/**
548 * Construct 4x4 pixels where we can look at a color and determine where it should be in the grid.
549 * alpha = 0xFF, blue = 0x80, red = x, green = y
550 */
551static void fill_4x4_pixels(SkPMColor colors[16]) {
552 for (int y = 0; y < 4; ++y) {
553 for (int x = 0; x < 4; ++x) {
554 colors[y*4+x] = SkPackARGB32(0xFF, x, y, 0x80);
555 }
556 }
557}
558
559static bool check_4x4_pixel(SkPMColor color, unsigned x, unsigned y) {
560 SkASSERT(x < 4 && y < 4);
561 return 0xFF == SkGetPackedA32(color) &&
562 x == SkGetPackedR32(color) &&
563 y == SkGetPackedG32(color) &&
564 0x80 == SkGetPackedB32(color);
565}
566
567/**
568 * Fill with all zeros, which will never match any value from fill_4x4_pixels
569 */
570static void clear_4x4_pixels(SkPMColor colors[16]) {
571 sk_memset32(colors, 0, 16);
572}
573
574// Much of readPixels is exercised by copyTo testing, since readPixels is the backend for that
575// method. Here we explicitly test subset copies.
576//
577DEF_TEST(BitmapReadPixels, reporter) {
578 const int W = 4;
579 const int H = 4;
580 const size_t rowBytes = W * sizeof(SkPMColor);
581 const SkImageInfo srcInfo = SkImageInfo::MakeN32Premul(W, H);
582 SkPMColor srcPixels[16];
583 fill_4x4_pixels(srcPixels);
584 SkBitmap srcBM;
585 srcBM.installPixels(srcInfo, srcPixels, rowBytes);
586
587 SkImageInfo dstInfo = SkImageInfo::MakeN32Premul(W, H);
588 SkPMColor dstPixels[16];
589
590 const struct {
591 bool fExpectedSuccess;
592 SkIPoint fRequestedSrcLoc;
593 SkISize fRequestedDstSize;
594 // If fExpectedSuccess, check these, otherwise ignore
595 SkIPoint fExpectedDstLoc;
596 SkIRect fExpectedSrcR;
597 } gRec[] = {
598 { true, { 0, 0 }, { 4, 4 }, { 0, 0 }, { 0, 0, 4, 4 } },
599 { true, { 1, 1 }, { 2, 2 }, { 0, 0 }, { 1, 1, 3, 3 } },
600 { true, { 2, 2 }, { 4, 4 }, { 0, 0 }, { 2, 2, 4, 4 } },
601 { true, {-1,-1 }, { 2, 2 }, { 1, 1 }, { 0, 0, 1, 1 } },
602 { false, {-1,-1 }, { 1, 1 }, { 0, 0 }, { 0, 0, 0, 0 } },
603 };
604
605 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {
606 clear_4x4_pixels(dstPixels);
607
reede5ea5002014-09-03 11:54:58 -0700608 dstInfo = dstInfo.makeWH(gRec[i].fRequestedDstSize.width(),
609 gRec[i].fRequestedDstSize.height());
reedb184f7f2014-07-13 04:32:32 -0700610 bool success = srcBM.readPixels(dstInfo, dstPixels, rowBytes,
611 gRec[i].fRequestedSrcLoc.x(), gRec[i].fRequestedSrcLoc.y());
halcanary9d524f22016-03-29 09:03:52 -0700612
reedb184f7f2014-07-13 04:32:32 -0700613 REPORTER_ASSERT(reporter, gRec[i].fExpectedSuccess == success);
614 if (success) {
615 const SkIRect srcR = gRec[i].fExpectedSrcR;
616 const int dstX = gRec[i].fExpectedDstLoc.x();
617 const int dstY = gRec[i].fExpectedDstLoc.y();
618 // Walk the dst pixels, and check if we got what we expected
619 for (int y = 0; y < H; ++y) {
620 for (int x = 0; x < W; ++x) {
621 SkPMColor dstC = dstPixels[y*4+x];
622 // get into src coordinates
623 int sx = x - dstX + srcR.x();
624 int sy = y - dstY + srcR.y();
625 if (srcR.contains(sx, sy)) {
626 REPORTER_ASSERT(reporter, check_4x4_pixel(dstC, sx, sy));
627 } else {
628 REPORTER_ASSERT(reporter, 0 == dstC);
629 }
630 }
631 }
632 }
633 }
634}
635