blob: 71b9a606559e8021c5526acdbcf0b341b081cca9 [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"
reed@android.com311c82d2009-05-05 23:13:23 +00009#include "SkRect.h"
scroggo565901d2015-12-10 10:44:13 -080010#include "SkTemplates.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000011#include "Test.h"
reed@android.com42263962009-05-01 04:00:01 +000012
13static const char* boolStr(bool value) {
14 return value ? "true" : "false";
15}
16
Matt Sarettd9836f42017-04-05 15:41:53 -040017static const char* color_type_name(SkColorType colorType) {
18 switch (colorType) {
19 case kUnknown_SkColorType:
20 return "None";
21 case kAlpha_8_SkColorType:
22 return "A8";
23 case kRGB_565_SkColorType:
24 return "565";
25 case kARGB_4444_SkColorType:
26 return "4444";
27 case kRGBA_8888_SkColorType:
28 return "RGBA";
29 case kBGRA_8888_SkColorType:
30 return "BGRA";
31 case kIndex_8_SkColorType:
32 return "Index8";
33 case kGray_8_SkColorType:
34 return "Gray8";
35 case kRGBA_F16_SkColorType:
36 return "F16";
37 }
38 return "";
39}
reed@android.com42263962009-05-01 04:00:01 +000040
reed@android.comcafc9f92009-08-22 03:44:57 +000041static void report_opaqueness(skiatest::Reporter* reporter, const SkBitmap& src,
42 const SkBitmap& dst) {
halcanary@google.coma9325fa2014-01-10 14:58:10 +000043 ERRORF(reporter, "src %s opaque:%d, dst %s opaque:%d",
Matt Sarettd9836f42017-04-05 15:41:53 -040044 color_type_name(src.colorType()), src.isOpaque(),
45 color_type_name(dst.colorType()), dst.isOpaque());
reed@android.comcafc9f92009-08-22 03:44:57 +000046}
47
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +000048static bool canHaveAlpha(SkColorType ct) {
49 return kRGB_565_SkColorType != ct;
reed@android.comcafc9f92009-08-22 03:44:57 +000050}
51
52// copyTo() should preserve isOpaque when it makes sense
reed@google.com0a6151d2013-10-10 14:44:56 +000053static void test_isOpaque(skiatest::Reporter* reporter,
54 const SkBitmap& srcOpaque, const SkBitmap& srcPremul,
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +000055 SkColorType dstColorType) {
reed@android.comcafc9f92009-08-22 03:44:57 +000056 SkBitmap dst;
57
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +000058 if (canHaveAlpha(srcPremul.colorType()) && canHaveAlpha(dstColorType)) {
59 REPORTER_ASSERT(reporter, srcPremul.copyTo(&dst, dstColorType));
60 REPORTER_ASSERT(reporter, dst.colorType() == dstColorType);
reed@google.com0a6151d2013-10-10 14:44:56 +000061 if (srcPremul.isOpaque() != dst.isOpaque()) {
62 report_opaqueness(reporter, srcPremul, dst);
reed@android.comcafc9f92009-08-22 03:44:57 +000063 }
64 }
65
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +000066 REPORTER_ASSERT(reporter, srcOpaque.copyTo(&dst, dstColorType));
67 REPORTER_ASSERT(reporter, dst.colorType() == dstColorType);
reed@google.com0a6151d2013-10-10 14:44:56 +000068 if (srcOpaque.isOpaque() != dst.isOpaque()) {
69 report_opaqueness(reporter, srcOpaque, dst);
reed@android.comcafc9f92009-08-22 03:44:57 +000070 }
71}
72
reed@google.com0a6151d2013-10-10 14:44:56 +000073static void init_src(const SkBitmap& bitmap) {
weita@google.comf9ab99a2009-05-03 18:23:30 +000074 SkAutoLockPixels lock(bitmap);
reed@android.com42263962009-05-01 04:00:01 +000075 if (bitmap.getPixels()) {
reed@google.com0a6151d2013-10-10 14:44:56 +000076 if (bitmap.getColorTable()) {
reed@google.com9ce6e752011-01-10 14:04:07 +000077 sk_bzero(bitmap.getPixels(), bitmap.getSize());
78 } else {
79 bitmap.eraseColor(SK_ColorWHITE);
80 }
reed@android.com42263962009-05-01 04:00:01 +000081 }
82}
83
Mike Reed6b3155c2017-04-03 14:41:44 -040084static sk_sp<SkColorTable> init_ctable() {
reed@android.com42263962009-05-01 04:00:01 +000085 static const SkColor colors[] = {
86 SK_ColorBLACK, SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE
87 };
Mike Reed6b3155c2017-04-03 14:41:44 -040088 return SkColorTable::Make(colors, SK_ARRAY_COUNT(colors));
reed@android.com42263962009-05-01 04:00:01 +000089}
90
91struct Pair {
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +000092 SkColorType fColorType;
93 const char* fValid;
reed@android.com42263962009-05-01 04:00:01 +000094};
95
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +000096// Utility functions for copyPixelsTo()/copyPixelsFrom() tests.
97// getPixel()
98// setPixel()
99// getSkConfigName()
100// struct Coordinates
101// reportCopyVerification()
102// writeCoordPixels()
103
104// Utility function to read the value of a given pixel in bm. All
105// values converted to uint32_t for simplification of comparisons.
caryclark@google.com42639cd2012-06-06 12:03:39 +0000106static uint32_t getPixel(int x, int y, const SkBitmap& bm) {
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000107 uint32_t val = 0;
108 uint16_t val16;
rmistry@google.comd6bab022013-12-02 13:50:38 +0000109 uint8_t val8;
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000110 SkAutoLockPixels lock(bm);
111 const void* rawAddr = bm.getAddr(x,y);
112
commit-bot@chromium.org8ef51b92014-03-05 13:43:15 +0000113 switch (bm.bytesPerPixel()) {
114 case 4:
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000115 memcpy(&val, rawAddr, sizeof(uint32_t));
116 break;
commit-bot@chromium.org8ef51b92014-03-05 13:43:15 +0000117 case 2:
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000118 memcpy(&val16, rawAddr, sizeof(uint16_t));
119 val = val16;
120 break;
commit-bot@chromium.org8ef51b92014-03-05 13:43:15 +0000121 case 1:
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000122 memcpy(&val8, rawAddr, sizeof(uint8_t));
123 val = val8;
124 break;
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000125 default:
126 break;
127 }
128 return val;
129}
130
131// Utility function to set value of any pixel in bm.
132// bm.getConfig() specifies what format 'val' must be
133// converted to, but at present uint32_t can handle all formats.
caryclark@google.com42639cd2012-06-06 12:03:39 +0000134static void setPixel(int x, int y, uint32_t val, SkBitmap& bm) {
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000135 uint16_t val16;
rmistry@google.comd6bab022013-12-02 13:50:38 +0000136 uint8_t val8;
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000137 SkAutoLockPixels lock(bm);
138 void* rawAddr = bm.getAddr(x,y);
139
commit-bot@chromium.org8ef51b92014-03-05 13:43:15 +0000140 switch (bm.bytesPerPixel()) {
141 case 4:
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000142 memcpy(rawAddr, &val, sizeof(uint32_t));
143 break;
commit-bot@chromium.org8ef51b92014-03-05 13:43:15 +0000144 case 2:
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000145 val16 = val & 0xFFFF;
146 memcpy(rawAddr, &val16, sizeof(uint16_t));
147 break;
commit-bot@chromium.org8ef51b92014-03-05 13:43:15 +0000148 case 1:
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000149 val8 = val & 0xFF;
150 memcpy(rawAddr, &val8, sizeof(uint8_t));
151 break;
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000152 default:
153 // Ignore.
154 break;
155 }
156}
157
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000158// Helper struct to contain pixel locations, while avoiding need for STL.
159struct Coordinates {
160
161 const int length;
162 SkIPoint* const data;
163
164 explicit Coordinates(int _length): length(_length)
165 , data(new SkIPoint[length]) { }
166
167 ~Coordinates(){
168 delete [] data;
169 }
170
171 SkIPoint* operator[](int i) const {
172 // Use with care, no bounds checking.
173 return data + i;
174 }
175};
176
177// A function to verify that two bitmaps contain the same pixel values
178// at all coordinates indicated by coords. Simplifies verification of
179// copied bitmaps.
caryclark@google.com42639cd2012-06-06 12:03:39 +0000180static void reportCopyVerification(const SkBitmap& bm1, const SkBitmap& bm2,
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000181 Coordinates& coords,
182 const char* msg,
183 skiatest::Reporter* reporter){
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000184 // Confirm all pixels in the list match.
scroggo@google.comd5764e82012-08-22 15:00:05 +0000185 for (int i = 0; i < coords.length; ++i) {
reed92fc2ae2015-05-22 08:06:21 -0700186 uint32_t p1 = getPixel(coords[i]->fX, coords[i]->fY, bm1);
187 uint32_t p2 = getPixel(coords[i]->fX, coords[i]->fY, bm2);
188// SkDebugf("[%d] (%d %d) p1=%x p2=%x\n", i, coords[i]->fX, coords[i]->fY, p1, p2);
189 if (p1 != p2) {
Matt Sarettd9836f42017-04-05 15:41:53 -0400190 ERRORF(reporter, "%s [colortype = %s]", msg, color_type_name(bm1.colorType()));
reed92fc2ae2015-05-22 08:06:21 -0700191 break;
192 }
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000193 }
194}
195
196// Writes unique pixel values at locations specified by coords.
caryclark@google.com42639cd2012-06-06 12:03:39 +0000197static void writeCoordPixels(SkBitmap& bm, const Coordinates& coords) {
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000198 for (int i = 0; i < coords.length; ++i)
199 setPixel(coords[i]->fX, coords[i]->fY, i, bm);
200}
201
scroggo@google.com5ccae2c2014-01-15 16:56:52 +0000202static const Pair gPairs[] = {
Matt Sarettd9836f42017-04-05 15:41:53 -0400203 { kUnknown_SkColorType, "0000000" },
204 { kAlpha_8_SkColorType, "0100000" },
205 { kIndex_8_SkColorType, "0101111" },
206 { kRGB_565_SkColorType, "0101011" },
207 { kARGB_4444_SkColorType, "0101111" },
208 { kN32_SkColorType, "0101111" },
209 { kRGBA_F16_SkColorType, "0101011" },
scroggo@google.com5ccae2c2014-01-15 16:56:52 +0000210};
commit-bot@chromium.org5c6f1d42014-01-10 18:28:23 +0000211
scroggo@google.com5ccae2c2014-01-15 16:56:52 +0000212static const int W = 20;
213static const int H = 33;
214
215static void setup_src_bitmaps(SkBitmap* srcOpaque, SkBitmap* srcPremul,
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000216 SkColorType ct) {
Mike Reed6b3155c2017-04-03 14:41:44 -0400217 sk_sp<SkColorTable> ctable;
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000218 if (kIndex_8_SkColorType == ct) {
reedc5e15a12014-09-29 12:10:27 -0700219 ctable = init_ctable();
scroggo@google.com5ccae2c2014-01-15 16:56:52 +0000220 }
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000221
Matt Sarettd9836f42017-04-05 15:41:53 -0400222 sk_sp<SkColorSpace> colorSpace = nullptr;
223 if (kRGBA_F16_SkColorType == ct) {
224 colorSpace = SkColorSpace::MakeSRGBLinear();
225 }
226
227 srcOpaque->allocPixels(SkImageInfo::Make(W, H, ct, kOpaque_SkAlphaType, colorSpace), ctable);
228 srcPremul->allocPixels(SkImageInfo::Make(W, H, ct, kPremul_SkAlphaType, colorSpace), ctable);
scroggo@google.com5ccae2c2014-01-15 16:56:52 +0000229 init_src(*srcOpaque);
230 init_src(*srcPremul);
231}
232
233DEF_TEST(BitmapCopy_extractSubset, reporter) {
234 for (size_t i = 0; i < SK_ARRAY_COUNT(gPairs); i++) {
235 SkBitmap srcOpaque, srcPremul;
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000236 setup_src_bitmaps(&srcOpaque, &srcPremul, gPairs[i].fColorType);
scroggo@google.com5ccae2c2014-01-15 16:56:52 +0000237
238 SkBitmap bitmap(srcOpaque);
239 SkBitmap subset;
240 SkIRect r;
241 // Extract a subset which has the same width as the original. This
242 // catches a bug where we cloned the genID incorrectly.
243 r.set(0, 1, W, 3);
244 bitmap.setIsVolatile(true);
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000245 // Relies on old behavior of extractSubset failing if colortype is unknown
246 if (kUnknown_SkColorType != bitmap.colorType() && bitmap.extractSubset(&subset, r)) {
scroggo@google.com5ccae2c2014-01-15 16:56:52 +0000247 REPORTER_ASSERT(reporter, subset.width() == W);
248 REPORTER_ASSERT(reporter, subset.height() == 2);
249 REPORTER_ASSERT(reporter, subset.alphaType() == bitmap.alphaType());
250 REPORTER_ASSERT(reporter, subset.isVolatile() == true);
251
252 // Test copying an extracted subset.
253 for (size_t j = 0; j < SK_ARRAY_COUNT(gPairs); j++) {
254 SkBitmap copy;
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000255 bool success = subset.copyTo(&copy, gPairs[j].fColorType);
scroggo@google.com5ccae2c2014-01-15 16:56:52 +0000256 if (!success) {
257 // Skip checking that success matches fValid, which is redundant
258 // with the code below.
Matt Sarett03dd6d52017-01-23 12:15:09 -0500259 REPORTER_ASSERT(reporter, kIndex_8_SkColorType == gPairs[i].fColorType ||
260 gPairs[i].fColorType != gPairs[j].fColorType);
scroggo@google.com5ccae2c2014-01-15 16:56:52 +0000261 continue;
262 }
263
264 // When performing a copy of an extracted subset, the gen id should
265 // change.
266 REPORTER_ASSERT(reporter, copy.getGenerationID() != subset.getGenerationID());
267
268 REPORTER_ASSERT(reporter, copy.width() == W);
269 REPORTER_ASSERT(reporter, copy.height() == 2);
270
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000271 if (gPairs[i].fColorType == gPairs[j].fColorType) {
scroggo@google.com5ccae2c2014-01-15 16:56:52 +0000272 SkAutoLockPixels alp0(subset);
273 SkAutoLockPixels alp1(copy);
274 // they should both have, or both not-have, a colortable
halcanary96fcdcc2015-08-27 07:41:13 -0700275 bool hasCT = subset.getColorTable() != nullptr;
276 REPORTER_ASSERT(reporter, (copy.getColorTable() != nullptr) == hasCT);
scroggo@google.com5ccae2c2014-01-15 16:56:52 +0000277 }
278 }
279 }
280
281 bitmap = srcPremul;
282 bitmap.setIsVolatile(false);
283 if (bitmap.extractSubset(&subset, r)) {
284 REPORTER_ASSERT(reporter, subset.alphaType() == bitmap.alphaType());
285 REPORTER_ASSERT(reporter, subset.isVolatile() == false);
286 }
287 }
288}
289
290DEF_TEST(BitmapCopy, reporter) {
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000291 static const bool isExtracted[] = {
292 false, true
293 };
294
commit-bot@chromium.org5c6f1d42014-01-10 18:28:23 +0000295 for (size_t i = 0; i < SK_ARRAY_COUNT(gPairs); i++) {
scroggo@google.com5ccae2c2014-01-15 16:56:52 +0000296 SkBitmap srcOpaque, srcPremul;
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000297 setup_src_bitmaps(&srcOpaque, &srcPremul, gPairs[i].fColorType);
scroggo@google.com5ccae2c2014-01-15 16:56:52 +0000298
reed@android.com42263962009-05-01 04:00:01 +0000299 for (size_t j = 0; j < SK_ARRAY_COUNT(gPairs); j++) {
scroggo@google.com5ccae2c2014-01-15 16:56:52 +0000300 SkBitmap dst;
reed@android.com42263962009-05-01 04:00:01 +0000301
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000302 bool success = srcPremul.copyTo(&dst, gPairs[j].fColorType);
reed@android.com42263962009-05-01 04:00:01 +0000303 bool expected = gPairs[i].fValid[j] != '0';
304 if (success != expected) {
halcanary@google.coma9325fa2014-01-10 14:58:10 +0000305 ERRORF(reporter, "SkBitmap::copyTo from %s to %s. expected %s "
Matt Sarettd9836f42017-04-05 15:41:53 -0400306 "returned %s", color_type_name(gPairs[i].fColorType),
307 color_type_name(gPairs[j].fColorType),
halcanary@google.coma9325fa2014-01-10 14:58:10 +0000308 boolStr(expected), boolStr(success));
reed@android.com42263962009-05-01 04:00:01 +0000309 }
reed@google.com1fcd51e2011-01-05 15:50:27 +0000310
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000311 bool canSucceed = srcPremul.canCopyTo(gPairs[j].fColorType);
reed@android.comfbaa88d2009-05-06 17:44:34 +0000312 if (success != canSucceed) {
halcanary@google.coma9325fa2014-01-10 14:58:10 +0000313 ERRORF(reporter, "SkBitmap::copyTo from %s to %s. returned %s "
Matt Sarettd9836f42017-04-05 15:41:53 -0400314 "canCopyTo %s", color_type_name(gPairs[i].fColorType),
315 color_type_name(gPairs[j].fColorType),
halcanary@google.coma9325fa2014-01-10 14:58:10 +0000316 boolStr(success), boolStr(canSucceed));
reed@android.comfbaa88d2009-05-06 17:44:34 +0000317 }
weita@google.comf9ab99a2009-05-03 18:23:30 +0000318
reed@android.com42263962009-05-01 04:00:01 +0000319 if (success) {
reed@google.com0a6151d2013-10-10 14:44:56 +0000320 REPORTER_ASSERT(reporter, srcPremul.width() == dst.width());
321 REPORTER_ASSERT(reporter, srcPremul.height() == dst.height());
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000322 REPORTER_ASSERT(reporter, dst.colorType() == gPairs[j].fColorType);
323 test_isOpaque(reporter, srcOpaque, srcPremul, dst.colorType());
commit-bot@chromium.org8ef51b92014-03-05 13:43:15 +0000324 if (srcPremul.colorType() == dst.colorType()) {
reed@google.com0a6151d2013-10-10 14:44:56 +0000325 SkAutoLockPixels srcLock(srcPremul);
reed@android.com42263962009-05-01 04:00:01 +0000326 SkAutoLockPixels dstLock(dst);
reed@google.com0a6151d2013-10-10 14:44:56 +0000327 REPORTER_ASSERT(reporter, srcPremul.readyToDraw());
reed@android.com42263962009-05-01 04:00:01 +0000328 REPORTER_ASSERT(reporter, dst.readyToDraw());
reed@google.com0a6151d2013-10-10 14:44:56 +0000329 const char* srcP = (const char*)srcPremul.getAddr(0, 0);
reed@android.com42263962009-05-01 04:00:01 +0000330 const char* dstP = (const char*)dst.getAddr(0, 0);
331 REPORTER_ASSERT(reporter, srcP != dstP);
332 REPORTER_ASSERT(reporter, !memcmp(srcP, dstP,
reed@google.com0a6151d2013-10-10 14:44:56 +0000333 srcPremul.getSize()));
334 REPORTER_ASSERT(reporter, srcPremul.getGenerationID() == dst.getGenerationID());
scroggo@google.comd5764e82012-08-22 15:00:05 +0000335 } else {
reed@google.com0a6151d2013-10-10 14:44:56 +0000336 REPORTER_ASSERT(reporter, srcPremul.getGenerationID() != dst.getGenerationID());
reed@android.com42263962009-05-01 04:00:01 +0000337 }
reed@android.com42263962009-05-01 04:00:01 +0000338 } else {
339 // dst should be unchanged from its initial state
commit-bot@chromium.org8ef51b92014-03-05 13:43:15 +0000340 REPORTER_ASSERT(reporter, dst.colorType() == kUnknown_SkColorType);
reed@android.com42263962009-05-01 04:00:01 +0000341 REPORTER_ASSERT(reporter, dst.width() == 0);
342 REPORTER_ASSERT(reporter, dst.height() == 0);
343 }
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000344 } // for (size_t j = ...
345
346 // Tests for getSafeSize(), getSafeSize64(), copyPixelsTo(),
347 // copyPixelsFrom().
348 //
349 for (size_t copyCase = 0; copyCase < SK_ARRAY_COUNT(isExtracted);
350 ++copyCase) {
351 // Test copying to/from external buffer.
352 // Note: the tests below have hard-coded values ---
353 // Please take care if modifying.
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000354
reed@google.com2cb14802013-06-26 14:35:02 +0000355 // Tests for getSafeSize64().
356 // Test with a very large configuration without pixel buffer
357 // attached.
358 SkBitmap tstSafeSize;
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +0000359 tstSafeSize.setInfo(SkImageInfo::Make(100000000U, 100000000U,
360 gPairs[i].fColorType, kPremul_SkAlphaType));
reed@google.com57212f92013-12-30 14:40:38 +0000361 int64_t safeSize = tstSafeSize.computeSafeSize64();
362 if (safeSize < 0) {
halcanary@google.coma9325fa2014-01-10 14:58:10 +0000363 ERRORF(reporter, "getSafeSize64() negative: %s",
Matt Sarettd9836f42017-04-05 15:41:53 -0400364 color_type_name(tstSafeSize.colorType()));
reed@google.com2cb14802013-06-26 14:35:02 +0000365 }
366 bool sizeFail = false;
367 // Compare against hand-computed values.
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000368 switch (gPairs[i].fColorType) {
369 case kUnknown_SkColorType:
reed@google.com2cb14802013-06-26 14:35:02 +0000370 break;
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000371
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000372 case kAlpha_8_SkColorType:
373 case kIndex_8_SkColorType:
reed@google.com57212f92013-12-30 14:40:38 +0000374 if (safeSize != 0x2386F26FC10000LL) {
reed@google.com2cb14802013-06-26 14:35:02 +0000375 sizeFail = true;
reed@google.com01c41a52013-12-20 14:24:21 +0000376 }
reed@google.com2cb14802013-06-26 14:35:02 +0000377 break;
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000378
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000379 case kRGB_565_SkColorType:
380 case kARGB_4444_SkColorType:
reed@google.com57212f92013-12-30 14:40:38 +0000381 if (safeSize != 0x470DE4DF820000LL) {
reed@google.com2cb14802013-06-26 14:35:02 +0000382 sizeFail = true;
reed@google.com01c41a52013-12-20 14:24:21 +0000383 }
reed@google.com2cb14802013-06-26 14:35:02 +0000384 break;
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000385
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000386 case kN32_SkColorType:
reed@google.com57212f92013-12-30 14:40:38 +0000387 if (safeSize != 0x8E1BC9BF040000LL) {
reed@google.com2cb14802013-06-26 14:35:02 +0000388 sizeFail = true;
reed@google.com01c41a52013-12-20 14:24:21 +0000389 }
reed@google.com2cb14802013-06-26 14:35:02 +0000390 break;
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000391
reed@google.com2cb14802013-06-26 14:35:02 +0000392 default:
393 break;
394 }
395 if (sizeFail) {
halcanary@google.coma9325fa2014-01-10 14:58:10 +0000396 ERRORF(reporter, "computeSafeSize64() wrong size: %s",
Matt Sarettd9836f42017-04-05 15:41:53 -0400397 color_type_name(tstSafeSize.colorType()));
reed@google.com2cb14802013-06-26 14:35:02 +0000398 }
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000399
rmistry@google.comd6bab022013-12-02 13:50:38 +0000400 int subW = 2;
401 int subH = 2;
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000402
reed@google.com2cb14802013-06-26 14:35:02 +0000403 // Create bitmap to act as source for copies and subsets.
404 SkBitmap src, subset;
Mike Reed6b3155c2017-04-03 14:41:44 -0400405 sk_sp<SkColorTable> ct;
commit-bot@chromium.org8ef51b92014-03-05 13:43:15 +0000406 if (kIndex_8_SkColorType == src.colorType()) {
reedc5e15a12014-09-29 12:10:27 -0700407 ct = init_ctable();
reed@google.com2cb14802013-06-26 14:35:02 +0000408 }
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000409
reed84825042014-09-02 12:50:45 -0700410 int localSubW;
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000411 if (isExtracted[copyCase]) { // A larger image to extract from.
reed84825042014-09-02 12:50:45 -0700412 localSubW = 2 * subW + 1;
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000413 } else { // Tests expect a 2x2 bitmap, so make smaller.
reed84825042014-09-02 12:50:45 -0700414 localSubW = subW;
415 }
416 // could fail if we pass kIndex_8 for the colortype
417 if (src.tryAllocPixels(SkImageInfo::Make(localSubW, subH, gPairs[i].fColorType,
418 kPremul_SkAlphaType))) {
419 // failure is fine, as we will notice later on
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000420 }
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000421
reed@google.com2cb14802013-06-26 14:35:02 +0000422 // Either copy src or extract into 'subset', which is used
423 // for subsequent calls to copyPixelsTo/From.
424 bool srcReady = false;
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000425 // Test relies on older behavior that extractSubset will fail on
commit-bot@chromium.org8ef51b92014-03-05 13:43:15 +0000426 // kUnknown_SkColorType
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000427 if (kUnknown_SkColorType != src.colorType() &&
428 isExtracted[copyCase]) {
reed@google.com2cb14802013-06-26 14:35:02 +0000429 // The extractedSubset() test case allows us to test copy-
430 // ing when src and dst mave possibly different strides.
431 SkIRect r;
rmistry@google.comd6bab022013-12-02 13:50:38 +0000432 r.set(1, 0, 1 + subW, subH); // 2x2 extracted bitmap
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000433
reed@google.com2cb14802013-06-26 14:35:02 +0000434 srcReady = src.extractSubset(&subset, r);
435 } else {
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000436 srcReady = src.copyTo(&subset);
reed@google.com2cb14802013-06-26 14:35:02 +0000437 }
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000438
reed@google.com2cb14802013-06-26 14:35:02 +0000439 // Not all configurations will generate a valid 'subset'.
440 if (srcReady) {
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000441
reed@google.com2cb14802013-06-26 14:35:02 +0000442 // Allocate our target buffer 'buf' for all copies.
443 // To simplify verifying correctness of copies attach
444 // buf to a SkBitmap, but copies are done using the
445 // raw buffer pointer.
robertphillips@google.come9cd27d2013-10-16 17:48:11 +0000446 const size_t bufSize = subH *
commit-bot@chromium.org8ef51b92014-03-05 13:43:15 +0000447 SkColorTypeMinRowBytes(src.colorType(), subW) * 2;
scroggo565901d2015-12-10 10:44:13 -0800448 SkAutoTMalloc<uint8_t> autoBuf (bufSize);
449 uint8_t* buf = autoBuf.get();
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000450
reed@google.com2cb14802013-06-26 14:35:02 +0000451 SkBitmap bufBm; // Attach buf to this bitmap.
452 bool successExpected;
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000453
reed@google.com2cb14802013-06-26 14:35:02 +0000454 // Set up values for each pixel being copied.
455 Coordinates coords(subW * subH);
robertphillips@google.come9cd27d2013-10-16 17:48:11 +0000456 for (int x = 0; x < subW; ++x)
457 for (int y = 0; y < subH; ++y)
reed@google.com2cb14802013-06-26 14:35:02 +0000458 {
459 int index = y * subW + x;
460 SkASSERT(index < coords.length);
461 coords[index]->fX = x;
462 coords[index]->fY = y;
463 }
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000464
reed@google.com2cb14802013-06-26 14:35:02 +0000465 writeCoordPixels(subset, coords);
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000466
reed@google.com2cb14802013-06-26 14:35:02 +0000467 // Test #1 ////////////////////////////////////////////
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000468
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000469 const SkImageInfo info = SkImageInfo::Make(subW, subH,
470 gPairs[i].fColorType,
471 kPremul_SkAlphaType);
reed@google.com2cb14802013-06-26 14:35:02 +0000472 // Before/after comparisons easier if we attach buf
473 // to an appropriately configured SkBitmap.
474 memset(buf, 0xFF, bufSize);
475 // Config with stride greater than src but that fits in buf.
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000476 bufBm.installPixels(info, buf, info.minRowBytes() * 2);
reed@google.com2cb14802013-06-26 14:35:02 +0000477 successExpected = false;
478 // Then attempt to copy with a stride that is too large
479 // to fit in the buffer.
480 REPORTER_ASSERT(reporter,
481 subset.copyPixelsTo(buf, bufSize, bufBm.rowBytes() * 3)
482 == successExpected);
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000483
reed@google.com2cb14802013-06-26 14:35:02 +0000484 if (successExpected)
485 reportCopyVerification(subset, bufBm, coords,
486 "copyPixelsTo(buf, bufSize, 1.5*maxRowBytes)",
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000487 reporter);
488
reed@google.com2cb14802013-06-26 14:35:02 +0000489 // Test #2 ////////////////////////////////////////////
490 // This test should always succeed, but in the case
491 // of extracted bitmaps only because we handle the
492 // issue of getSafeSize(). Without getSafeSize()
493 // buffer overrun/read would occur.
494 memset(buf, 0xFF, bufSize);
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000495 bufBm.installPixels(info, buf, subset.rowBytes());
reed@google.com2cb14802013-06-26 14:35:02 +0000496 successExpected = subset.getSafeSize() <= bufSize;
497 REPORTER_ASSERT(reporter,
498 subset.copyPixelsTo(buf, bufSize) ==
499 successExpected);
500 if (successExpected)
501 reportCopyVerification(subset, bufBm, coords,
502 "copyPixelsTo(buf, bufSize)", reporter);
503
504 // Test #3 ////////////////////////////////////////////
505 // Copy with different stride between src and dst.
506 memset(buf, 0xFF, bufSize);
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000507 bufBm.installPixels(info, buf, subset.rowBytes()+1);
reed@google.com2cb14802013-06-26 14:35:02 +0000508 successExpected = true; // Should always work.
509 REPORTER_ASSERT(reporter,
510 subset.copyPixelsTo(buf, bufSize,
511 subset.rowBytes()+1) == successExpected);
512 if (successExpected)
513 reportCopyVerification(subset, bufBm, coords,
514 "copyPixelsTo(buf, bufSize, rowBytes+1)", reporter);
515
516 // Test #4 ////////////////////////////////////////////
517 // Test copy with stride too small.
518 memset(buf, 0xFF, bufSize);
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000519 bufBm.installPixels(info, buf, info.minRowBytes());
reed@google.com2cb14802013-06-26 14:35:02 +0000520 successExpected = false;
521 // Request copy with stride too small.
522 REPORTER_ASSERT(reporter,
523 subset.copyPixelsTo(buf, bufSize, bufBm.rowBytes()-1)
524 == successExpected);
525 if (successExpected)
526 reportCopyVerification(subset, bufBm, coords,
527 "copyPixelsTo(buf, bufSize, rowBytes()-1)", reporter);
528
529#if 0 // copyPixelsFrom is gone
530 // Test #5 ////////////////////////////////////////////
531 // Tests the case where the source stride is too small
532 // for the source configuration.
533 memset(buf, 0xFF, bufSize);
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000534 bufBm.installPixels(info, buf, info.minRowBytes());
reed@google.com2cb14802013-06-26 14:35:02 +0000535 writeCoordPixels(bufBm, coords);
536 REPORTER_ASSERT(reporter,
537 subset.copyPixelsFrom(buf, bufSize, 1) == false);
538
539 // Test #6 ///////////////////////////////////////////
540 // Tests basic copy from an external buffer to the bitmap.
541 // If the bitmap is "extracted", this also tests the case
542 // where the source stride is different from the dest.
543 // stride.
544 // We've made the buffer large enough to always succeed.
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000545 bufBm.installPixels(info, buf, info.minRowBytes());
reed@google.com2cb14802013-06-26 14:35:02 +0000546 writeCoordPixels(bufBm, coords);
547 REPORTER_ASSERT(reporter,
548 subset.copyPixelsFrom(buf, bufSize, bufBm.rowBytes()) ==
549 true);
550 reportCopyVerification(bufBm, subset, coords,
551 "copyPixelsFrom(buf, bufSize)",
552 reporter);
553
554 // Test #7 ////////////////////////////////////////////
555 // Tests the case where the source buffer is too small
556 // for the transfer.
557 REPORTER_ASSERT(reporter,
558 subset.copyPixelsFrom(buf, 1, subset.rowBytes()) ==
559 false);
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000560
reed@google.comab77aaf2011-11-01 16:03:35 +0000561#endif
wjmaclean@chromium.org86bff1f2010-11-16 20:22:41 +0000562 }
563 } // for (size_t copyCase ...
reed@android.com42263962009-05-01 04:00:01 +0000564 }
565}
reedb184f7f2014-07-13 04:32:32 -0700566
567#include "SkColorPriv.h"
568#include "SkUtils.h"
569
570/**
571 * Construct 4x4 pixels where we can look at a color and determine where it should be in the grid.
572 * alpha = 0xFF, blue = 0x80, red = x, green = y
573 */
574static void fill_4x4_pixels(SkPMColor colors[16]) {
575 for (int y = 0; y < 4; ++y) {
576 for (int x = 0; x < 4; ++x) {
577 colors[y*4+x] = SkPackARGB32(0xFF, x, y, 0x80);
578 }
579 }
580}
581
582static bool check_4x4_pixel(SkPMColor color, unsigned x, unsigned y) {
583 SkASSERT(x < 4 && y < 4);
584 return 0xFF == SkGetPackedA32(color) &&
585 x == SkGetPackedR32(color) &&
586 y == SkGetPackedG32(color) &&
587 0x80 == SkGetPackedB32(color);
588}
589
590/**
591 * Fill with all zeros, which will never match any value from fill_4x4_pixels
592 */
593static void clear_4x4_pixels(SkPMColor colors[16]) {
594 sk_memset32(colors, 0, 16);
595}
596
597// Much of readPixels is exercised by copyTo testing, since readPixels is the backend for that
598// method. Here we explicitly test subset copies.
599//
600DEF_TEST(BitmapReadPixels, reporter) {
601 const int W = 4;
602 const int H = 4;
603 const size_t rowBytes = W * sizeof(SkPMColor);
604 const SkImageInfo srcInfo = SkImageInfo::MakeN32Premul(W, H);
605 SkPMColor srcPixels[16];
606 fill_4x4_pixels(srcPixels);
607 SkBitmap srcBM;
608 srcBM.installPixels(srcInfo, srcPixels, rowBytes);
609
610 SkImageInfo dstInfo = SkImageInfo::MakeN32Premul(W, H);
611 SkPMColor dstPixels[16];
612
613 const struct {
614 bool fExpectedSuccess;
615 SkIPoint fRequestedSrcLoc;
616 SkISize fRequestedDstSize;
617 // If fExpectedSuccess, check these, otherwise ignore
618 SkIPoint fExpectedDstLoc;
619 SkIRect fExpectedSrcR;
620 } gRec[] = {
621 { true, { 0, 0 }, { 4, 4 }, { 0, 0 }, { 0, 0, 4, 4 } },
622 { true, { 1, 1 }, { 2, 2 }, { 0, 0 }, { 1, 1, 3, 3 } },
623 { true, { 2, 2 }, { 4, 4 }, { 0, 0 }, { 2, 2, 4, 4 } },
624 { true, {-1,-1 }, { 2, 2 }, { 1, 1 }, { 0, 0, 1, 1 } },
625 { false, {-1,-1 }, { 1, 1 }, { 0, 0 }, { 0, 0, 0, 0 } },
626 };
627
628 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {
629 clear_4x4_pixels(dstPixels);
630
reede5ea5002014-09-03 11:54:58 -0700631 dstInfo = dstInfo.makeWH(gRec[i].fRequestedDstSize.width(),
632 gRec[i].fRequestedDstSize.height());
reedb184f7f2014-07-13 04:32:32 -0700633 bool success = srcBM.readPixels(dstInfo, dstPixels, rowBytes,
634 gRec[i].fRequestedSrcLoc.x(), gRec[i].fRequestedSrcLoc.y());
halcanary9d524f22016-03-29 09:03:52 -0700635
reedb184f7f2014-07-13 04:32:32 -0700636 REPORTER_ASSERT(reporter, gRec[i].fExpectedSuccess == success);
637 if (success) {
638 const SkIRect srcR = gRec[i].fExpectedSrcR;
639 const int dstX = gRec[i].fExpectedDstLoc.x();
640 const int dstY = gRec[i].fExpectedDstLoc.y();
641 // Walk the dst pixels, and check if we got what we expected
642 for (int y = 0; y < H; ++y) {
643 for (int x = 0; x < W; ++x) {
644 SkPMColor dstC = dstPixels[y*4+x];
645 // get into src coordinates
646 int sx = x - dstX + srcR.x();
647 int sy = y - dstY + srcR.y();
648 if (srcR.contains(sx, sy)) {
649 REPORTER_ASSERT(reporter, check_4x4_pixel(dstC, sx, sy));
650 } else {
651 REPORTER_ASSERT(reporter, 0 == dstC);
652 }
653 }
654 }
655 }
656 }
657}
658
Matt Sarettd9836f42017-04-05 15:41:53 -0400659DEF_TEST(BitmapCopy_ColorSpaceMatch, r) {
660 // We should support matching color spaces, even if they are parametric.
661 SkColorSpaceTransferFn fn;
662 fn.fA = 1.f; fn.fB = 0.f; fn.fC = 0.f; fn.fD = 0.f; fn.fF = 0.f; fn.fG = 1.8f;
663 sk_sp<SkColorSpace> cs = SkColorSpace::MakeRGB(fn, SkColorSpace::kRec2020_Gamut);
664
665 SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1, cs);
666 SkBitmap bitmap;
667 bitmap.allocPixels(info);
668 bitmap.eraseColor(0);
669
670 SkBitmap copy;
671 bool success = bitmap.copyTo(&copy, kN32_SkColorType, nullptr);
672 REPORTER_ASSERT(r, success);
673 REPORTER_ASSERT(r, cs.get() == copy.colorSpace());
674}