Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(229)

Side by Side Diff: tests/BitmapCopyTest.cpp

Issue 112113005: Reland "Fix genID cloning bugs." (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: Add asserts for colortype and height Created 6 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « src/core/SkBitmap.cpp ('k') | tests/GpuBitmapCopyTest.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1
2 /* 1 /*
3 * Copyright 2011 Google Inc. 2 * Copyright 2011 Google Inc.
4 * 3 *
5 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file. 5 * found in the LICENSE file.
7 */ 6 */
8 #include "Test.h" 7 #include "Test.h"
9 #include "TestClassDef.h" 8 #include "TestClassDef.h"
10 #include "SkBitmap.h" 9 #include "SkBitmap.h"
11 #include "SkRect.h" 10 #include "SkRect.h"
(...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after
193 ERRORF(reporter, "%s [config = %s]", msg, getSkConfigName(bm1)); 192 ERRORF(reporter, "%s [config = %s]", msg, getSkConfigName(bm1));
194 } 193 }
195 } 194 }
196 195
197 // Writes unique pixel values at locations specified by coords. 196 // Writes unique pixel values at locations specified by coords.
198 static void writeCoordPixels(SkBitmap& bm, const Coordinates& coords) { 197 static void writeCoordPixels(SkBitmap& bm, const Coordinates& coords) {
199 for (int i = 0; i < coords.length; ++i) 198 for (int i = 0; i < coords.length; ++i)
200 setPixel(coords[i]->fX, coords[i]->fY, i, bm); 199 setPixel(coords[i]->fX, coords[i]->fY, i, bm);
201 } 200 }
202 201
202 static const Pair gPairs[] = {
203 { SkBitmap::kNo_Config, "0000000" },
204 { SkBitmap::kA8_Config, "0101010" },
205 { SkBitmap::kIndex8_Config, "0111010" },
206 { SkBitmap::kRGB_565_Config, "0101010" },
207 { SkBitmap::kARGB_4444_Config, "0101110" },
208 { SkBitmap::kARGB_8888_Config, "0101110" },
209 };
210
211 static const int W = 20;
212 static const int H = 33;
213
214 static void setup_src_bitmaps(SkBitmap* srcOpaque, SkBitmap* srcPremul,
215 SkBitmap::Config config) {
216 SkColorTable* ctOpaque = NULL;
217 SkColorTable* ctPremul = NULL;
218
219 srcOpaque->setConfig(config, W, H, 0, kOpaque_SkAlphaType);
220 srcPremul->setConfig(config, W, H, 0, kPremul_SkAlphaType);
221 if (SkBitmap::kIndex8_Config == config) {
222 ctOpaque = init_ctable(kOpaque_SkAlphaType);
223 ctPremul = init_ctable(kPremul_SkAlphaType);
224 }
225 srcOpaque->allocPixels(ctOpaque);
226 srcPremul->allocPixels(ctPremul);
227 SkSafeUnref(ctOpaque);
228 SkSafeUnref(ctPremul);
229 init_src(*srcOpaque);
230 init_src(*srcPremul);
231 }
232
233 DEF_TEST(BitmapCopy_extractSubset, reporter) {
234 for (size_t i = 0; i < SK_ARRAY_COUNT(gPairs); i++) {
235 SkBitmap srcOpaque, srcPremul;
236 setup_src_bitmaps(&srcOpaque, &srcPremul, gPairs[i].fConfig);
237
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);
245 if (bitmap.extractSubset(&subset, r)) {
246 REPORTER_ASSERT(reporter, subset.width() == W);
247 REPORTER_ASSERT(reporter, subset.height() == 2);
248 REPORTER_ASSERT(reporter, subset.alphaType() == bitmap.alphaType());
249 REPORTER_ASSERT(reporter, subset.isVolatile() == true);
250
251 // Test copying an extracted subset.
252 for (size_t j = 0; j < SK_ARRAY_COUNT(gPairs); j++) {
253 SkBitmap copy;
254 bool success = subset.copyTo(&copy, gPairs[j].fConfig);
255 if (!success) {
256 // Skip checking that success matches fValid, which is redun dant
257 // with the code below.
258 REPORTER_ASSERT(reporter, gPairs[i].fConfig != gPairs[j].fCo nfig);
259 continue;
260 }
261
262 // When performing a copy of an extracted subset, the gen id sho uld
263 // change.
264 REPORTER_ASSERT(reporter, copy.getGenerationID() != subset.getGe nerationID());
265
266 REPORTER_ASSERT(reporter, copy.width() == W);
267 REPORTER_ASSERT(reporter, copy.height() == 2);
268
269 if (gPairs[i].fConfig == gPairs[j].fConfig) {
270 SkAutoLockPixels alp0(subset);
271 SkAutoLockPixels alp1(copy);
272 // they should both have, or both not-have, a colortable
273 bool hasCT = subset.getColorTable() != NULL;
274 REPORTER_ASSERT(reporter, (copy.getColorTable() != NULL) == hasCT);
275 }
276 }
277 }
278
279 bitmap = srcPremul;
280 bitmap.setIsVolatile(false);
281 if (bitmap.extractSubset(&subset, r)) {
282 REPORTER_ASSERT(reporter, subset.alphaType() == bitmap.alphaType());
283 REPORTER_ASSERT(reporter, subset.isVolatile() == false);
284 }
285 }
286 }
287
203 DEF_TEST(BitmapCopy, reporter) { 288 DEF_TEST(BitmapCopy, reporter) {
204 static const Pair gPairs[] = {
205 { SkBitmap::kNo_Config, "0000000" },
206 { SkBitmap::kA8_Config, "0101010" },
207 { SkBitmap::kIndex8_Config, "0111010" },
208 { SkBitmap::kRGB_565_Config, "0101010" },
209 { SkBitmap::kARGB_4444_Config, "0101110" },
210 { SkBitmap::kARGB_8888_Config, "0101110" },
211 };
212
213 static const bool isExtracted[] = { 289 static const bool isExtracted[] = {
214 false, true 290 false, true
215 }; 291 };
216 292
217 const int W = 20; 293 for (size_t i = 0; i < SK_ARRAY_COUNT(gPairs); i++) {
218 const int H = 33; 294 SkBitmap srcOpaque, srcPremul;
295 setup_src_bitmaps(&srcOpaque, &srcPremul, gPairs[i].fConfig);
219 296
220 for (size_t i = 0; i < SK_ARRAY_COUNT(gPairs); i++) {
221 for (size_t j = 0; j < SK_ARRAY_COUNT(gPairs); j++) { 297 for (size_t j = 0; j < SK_ARRAY_COUNT(gPairs); j++) {
222 SkBitmap srcOpaque, srcPremul, dst; 298 SkBitmap dst;
223
224 {
225 SkColorTable* ctOpaque = NULL;
226 SkColorTable* ctPremul = NULL;
227
228 srcOpaque.setConfig(gPairs[i].fConfig, W, H, 0, kOpaque_SkAlphaT ype);
229 srcPremul.setConfig(gPairs[i].fConfig, W, H, 0, kPremul_SkAlphaT ype);
230 if (SkBitmap::kIndex8_Config == gPairs[i].fConfig) {
231 ctOpaque = init_ctable(kOpaque_SkAlphaType);
232 ctPremul = init_ctable(kPremul_SkAlphaType);
233 }
234 srcOpaque.allocPixels(ctOpaque);
235 srcPremul.allocPixels(ctPremul);
236 SkSafeUnref(ctOpaque);
237 SkSafeUnref(ctPremul);
238 }
239 init_src(srcOpaque);
240 init_src(srcPremul);
241 299
242 bool success = srcPremul.copyTo(&dst, gPairs[j].fConfig); 300 bool success = srcPremul.copyTo(&dst, gPairs[j].fConfig);
243 bool expected = gPairs[i].fValid[j] != '0'; 301 bool expected = gPairs[i].fValid[j] != '0';
244 if (success != expected) { 302 if (success != expected) {
245 ERRORF(reporter, "SkBitmap::copyTo from %s to %s. expected %s " 303 ERRORF(reporter, "SkBitmap::copyTo from %s to %s. expected %s "
246 "returned %s", gConfigName[i], gConfigName[j], 304 "returned %s", gConfigName[i], gConfigName[j],
247 boolStr(expected), boolStr(success)); 305 boolStr(expected), boolStr(success));
248 } 306 }
249 307
250 bool canSucceed = srcPremul.canCopyTo(gPairs[j].fConfig); 308 bool canSucceed = srcPremul.canCopyTo(gPairs[j].fConfig);
(...skipping 15 matching lines...) Expand all
266 REPORTER_ASSERT(reporter, dst.readyToDraw()); 324 REPORTER_ASSERT(reporter, dst.readyToDraw());
267 const char* srcP = (const char*)srcPremul.getAddr(0, 0); 325 const char* srcP = (const char*)srcPremul.getAddr(0, 0);
268 const char* dstP = (const char*)dst.getAddr(0, 0); 326 const char* dstP = (const char*)dst.getAddr(0, 0);
269 REPORTER_ASSERT(reporter, srcP != dstP); 327 REPORTER_ASSERT(reporter, srcP != dstP);
270 REPORTER_ASSERT(reporter, !memcmp(srcP, dstP, 328 REPORTER_ASSERT(reporter, !memcmp(srcP, dstP,
271 srcPremul.getSize())); 329 srcPremul.getSize()));
272 REPORTER_ASSERT(reporter, srcPremul.getGenerationID() == dst .getGenerationID()); 330 REPORTER_ASSERT(reporter, srcPremul.getGenerationID() == dst .getGenerationID());
273 } else { 331 } else {
274 REPORTER_ASSERT(reporter, srcPremul.getGenerationID() != dst .getGenerationID()); 332 REPORTER_ASSERT(reporter, srcPremul.getGenerationID() != dst .getGenerationID());
275 } 333 }
276 // test extractSubset
277 {
278 SkBitmap bitmap(srcOpaque);
279 SkBitmap subset;
280 SkIRect r;
281 r.set(1, 1, 2, 2);
282 bitmap.setIsVolatile(true);
283 if (bitmap.extractSubset(&subset, r)) {
284 REPORTER_ASSERT(reporter, subset.width() == 1);
285 REPORTER_ASSERT(reporter, subset.height() == 1);
286 REPORTER_ASSERT(reporter,
287 subset.alphaType() == bitmap.alphaType() );
288 REPORTER_ASSERT(reporter,
289 subset.isVolatile() == true);
290
291 SkBitmap copy;
292 REPORTER_ASSERT(reporter,
293 subset.copyTo(&copy, subset.config()));
294 REPORTER_ASSERT(reporter, copy.width() == 1);
295 REPORTER_ASSERT(reporter, copy.height() == 1);
296 REPORTER_ASSERT(reporter, copy.rowBytes() <= 4);
297
298 SkAutoLockPixels alp0(subset);
299 SkAutoLockPixels alp1(copy);
300 // they should both have, or both not-have, a colortable
301 bool hasCT = subset.getColorTable() != NULL;
302 REPORTER_ASSERT(reporter,
303 (copy.getColorTable() != NULL) == hasCT);
304 }
305 bitmap = srcPremul;
306 bitmap.setIsVolatile(false);
307 if (bitmap.extractSubset(&subset, r)) {
308 REPORTER_ASSERT(reporter,
309 subset.alphaType() == bitmap.alphaType() );
310 REPORTER_ASSERT(reporter,
311 subset.isVolatile() == false);
312 }
313 }
314 } else { 334 } else {
315 // dst should be unchanged from its initial state 335 // dst should be unchanged from its initial state
316 REPORTER_ASSERT(reporter, dst.config() == SkBitmap::kNo_Config); 336 REPORTER_ASSERT(reporter, dst.config() == SkBitmap::kNo_Config);
317 REPORTER_ASSERT(reporter, dst.width() == 0); 337 REPORTER_ASSERT(reporter, dst.width() == 0);
318 REPORTER_ASSERT(reporter, dst.height() == 0); 338 REPORTER_ASSERT(reporter, dst.height() == 0);
319 } 339 }
320 } // for (size_t j = ... 340 } // for (size_t j = ...
321 341
322 // Tests for getSafeSize(), getSafeSize64(), copyPixelsTo(), 342 // Tests for getSafeSize(), getSafeSize64(), copyPixelsTo(),
323 // copyPixelsFrom(). 343 // copyPixelsFrom().
(...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after
531 // for the transfer. 551 // for the transfer.
532 REPORTER_ASSERT(reporter, 552 REPORTER_ASSERT(reporter,
533 subset.copyPixelsFrom(buf, 1, subset.rowBytes()) == 553 subset.copyPixelsFrom(buf, 1, subset.rowBytes()) ==
534 false); 554 false);
535 555
536 #endif 556 #endif
537 } 557 }
538 } // for (size_t copyCase ... 558 } // for (size_t copyCase ...
539 } 559 }
540 } 560 }
OLDNEW
« no previous file with comments | « src/core/SkBitmap.cpp ('k') | tests/GpuBitmapCopyTest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698