skia2/tests/BitmapCopyTest.cpp

560 lines
22 KiB
C++
Raw Normal View History

Automatic update of all copyright notices to reflect new license terms. I have manually examined all of these diffs and restored a few files that seem to require manual adjustment. The following files still need to be modified manually, in a separate CL: android_sample/SampleApp/AndroidManifest.xml android_sample/SampleApp/res/layout/layout.xml android_sample/SampleApp/res/menu/sample.xml android_sample/SampleApp/res/values/strings.xml android_sample/SampleApp/src/com/skia/sampleapp/SampleApp.java android_sample/SampleApp/src/com/skia/sampleapp/SampleView.java experimental/CiCarbonSampleMain.c experimental/CocoaDebugger/main.m experimental/FileReaderApp/main.m experimental/SimpleCocoaApp/main.m experimental/iOSSampleApp/Shared/SkAlertPrompt.h experimental/iOSSampleApp/Shared/SkAlertPrompt.m experimental/iOSSampleApp/SkiOSSampleApp-Base.xcconfig experimental/iOSSampleApp/SkiOSSampleApp-Debug.xcconfig experimental/iOSSampleApp/SkiOSSampleApp-Release.xcconfig gpu/src/android/GrGLDefaultInterface_android.cpp gyp/common.gypi gyp_skia include/ports/SkHarfBuzzFont.h include/views/SkOSWindow_wxwidgets.h make.bat make.py src/opts/memset.arm.S src/opts/memset16_neon.S src/opts/memset32_neon.S src/opts/opts_check_arm.cpp src/ports/SkDebug_brew.cpp src/ports/SkMemory_brew.cpp src/ports/SkOSFile_brew.cpp src/ports/SkXMLParser_empty.cpp src/utils/ios/SkImageDecoder_iOS.mm src/utils/ios/SkOSFile_iOS.mm src/utils/ios/SkStream_NSData.mm tests/FillPathTest.cpp Review URL: http://codereview.appspot.com/4816058 git-svn-id: http://skia.googlecode.com/svn/trunk@1982 2bbb7eff-a529-9590-31e7-b0007b416f81
2011-07-28 14:26:00 +00:00
/*
* Copyright 2011 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "SkBitmap.h"
#include "SkRect.h"
#include "Test.h"
static const char* boolStr(bool value) {
return value ? "true" : "false";
}
// these are in the same order as the SkBitmap::Config enum
static const char* gConfigName[] = {
"None", "A8", "Index8", "565", "4444", "8888"
};
static void report_opaqueness(skiatest::Reporter* reporter, const SkBitmap& src,
const SkBitmap& dst) {
ERRORF(reporter, "src %s opaque:%d, dst %s opaque:%d",
gConfigName[src.config()], src.isOpaque(),
gConfigName[dst.config()], dst.isOpaque());
}
static bool canHaveAlpha(SkBitmap::Config config) {
return config != SkBitmap::kRGB_565_Config;
}
// copyTo() should preserve isOpaque when it makes sense
static void test_isOpaque(skiatest::Reporter* reporter,
const SkBitmap& srcOpaque, const SkBitmap& srcPremul,
SkBitmap::Config dstConfig) {
SkBitmap dst;
if (canHaveAlpha(srcPremul.config()) && canHaveAlpha(dstConfig)) {
REPORTER_ASSERT(reporter, srcPremul.copyTo(&dst, dstConfig));
REPORTER_ASSERT(reporter, dst.config() == dstConfig);
if (srcPremul.isOpaque() != dst.isOpaque()) {
report_opaqueness(reporter, srcPremul, dst);
}
}
REPORTER_ASSERT(reporter, srcOpaque.copyTo(&dst, dstConfig));
REPORTER_ASSERT(reporter, dst.config() == dstConfig);
if (srcOpaque.isOpaque() != dst.isOpaque()) {
report_opaqueness(reporter, srcOpaque, dst);
}
}
static void init_src(const SkBitmap& bitmap) {
SkAutoLockPixels lock(bitmap);
if (bitmap.getPixels()) {
if (bitmap.getColorTable()) {
sk_bzero(bitmap.getPixels(), bitmap.getSize());
} else {
bitmap.eraseColor(SK_ColorWHITE);
}
}
}
static SkColorTable* init_ctable(SkAlphaType alphaType) {
static const SkColor colors[] = {
SK_ColorBLACK, SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE
};
return new SkColorTable(colors, SK_ARRAY_COUNT(colors), alphaType);
}
struct Pair {
SkBitmap::Config fConfig;
const char* fValid;
};
// Utility functions for copyPixelsTo()/copyPixelsFrom() tests.
// getPixel()
// setPixel()
// getSkConfigName()
// struct Coordinates
// reportCopyVerification()
// writeCoordPixels()
// Utility function to read the value of a given pixel in bm. All
// values converted to uint32_t for simplification of comparisons.
static uint32_t getPixel(int x, int y, const SkBitmap& bm) {
uint32_t val = 0;
uint16_t val16;
uint8_t val8;
SkAutoLockPixels lock(bm);
const void* rawAddr = bm.getAddr(x,y);
switch (bm.config()) {
case SkBitmap::kARGB_8888_Config:
memcpy(&val, rawAddr, sizeof(uint32_t));
break;
case SkBitmap::kARGB_4444_Config:
case SkBitmap::kRGB_565_Config:
memcpy(&val16, rawAddr, sizeof(uint16_t));
val = val16;
break;
case SkBitmap::kA8_Config:
case SkBitmap::kIndex8_Config:
memcpy(&val8, rawAddr, sizeof(uint8_t));
val = val8;
break;
default:
break;
}
return val;
}
// Utility function to set value of any pixel in bm.
// bm.getConfig() specifies what format 'val' must be
// converted to, but at present uint32_t can handle all formats.
static void setPixel(int x, int y, uint32_t val, SkBitmap& bm) {
uint16_t val16;
uint8_t val8;
SkAutoLockPixels lock(bm);
void* rawAddr = bm.getAddr(x,y);
switch (bm.config()) {
case SkBitmap::kARGB_8888_Config:
memcpy(rawAddr, &val, sizeof(uint32_t));
break;
case SkBitmap::kARGB_4444_Config:
case SkBitmap::kRGB_565_Config:
val16 = val & 0xFFFF;
memcpy(rawAddr, &val16, sizeof(uint16_t));
break;
case SkBitmap::kA8_Config:
case SkBitmap::kIndex8_Config:
val8 = val & 0xFF;
memcpy(rawAddr, &val8, sizeof(uint8_t));
break;
default:
// Ignore.
break;
}
}
// Utility to return string containing name of each format, to
// simplify diagnostic output.
static const char* getSkConfigName(const SkBitmap& bm) {
switch (bm.config()) {
case SkBitmap::kNo_Config: return "SkBitmap::kNo_Config";
case SkBitmap::kA8_Config: return "SkBitmap::kA8_Config";
case SkBitmap::kIndex8_Config: return "SkBitmap::kIndex8_Config";
case SkBitmap::kRGB_565_Config: return "SkBitmap::kRGB_565_Config";
case SkBitmap::kARGB_4444_Config: return "SkBitmap::kARGB_4444_Config";
case SkBitmap::kARGB_8888_Config: return "SkBitmap::kARGB_8888_Config";
default: return "Unknown SkBitmap configuration.";
}
}
// Helper struct to contain pixel locations, while avoiding need for STL.
struct Coordinates {
const int length;
SkIPoint* const data;
explicit Coordinates(int _length): length(_length)
, data(new SkIPoint[length]) { }
~Coordinates(){
delete [] data;
}
SkIPoint* operator[](int i) const {
// Use with care, no bounds checking.
return data + i;
}
};
// A function to verify that two bitmaps contain the same pixel values
// at all coordinates indicated by coords. Simplifies verification of
// copied bitmaps.
static void reportCopyVerification(const SkBitmap& bm1, const SkBitmap& bm2,
Coordinates& coords,
const char* msg,
skiatest::Reporter* reporter){
bool success = true;
// Confirm all pixels in the list match.
for (int i = 0; i < coords.length; ++i) {
success = success &&
(getPixel(coords[i]->fX, coords[i]->fY, bm1) ==
getPixel(coords[i]->fX, coords[i]->fY, bm2));
}
if (!success) {
ERRORF(reporter, "%s [config = %s]", msg, getSkConfigName(bm1));
}
}
// Writes unique pixel values at locations specified by coords.
static void writeCoordPixels(SkBitmap& bm, const Coordinates& coords) {
for (int i = 0; i < coords.length; ++i)
setPixel(coords[i]->fX, coords[i]->fY, i, bm);
}
Reland "Fix genID cloning bugs." SkBitmap.cpp: When copyTo calls readPixels, only clone the genID if the resulting SkPixelRef has the same dimensions as the original. This catches a bug where copying an SkBitmap representing the subset of an SkPixelRef (which implements onReadPixels) would result in the copy sharing the genID. (Thanks to r6710, this case can only happen using setPixelRef, so the updated GpuBitmapCopyTest checks for that.) Move some unnecessary NULL checks to asserts. When copyTo performs a memcpy, only clone the genID if the resulting SkPixelRef has the same dimensions as the original. This catches a bug where copying an extracted SkBitmap with the same width as its original SkPixelRef would incorrectly have the same genID. Add a comment and assert in deepCopyTo, when cloning the genID, since that case correctly clones it. BitmapCopyTest.cpp: Pull redundant work out of the inner loop (setting up the source bitmaps and testing extractSubset). Create a new inner loop for extractSubset, to test copying the result to each different config. Extract a subset that has the same width as the original, to catch the bug mentioned above. Remove the reporter assert which checks for the resulting rowbytes. Add checks to ensure that copying the extracted subset changes the genID. GpuBitmapCopyTest: Create an SkBitmap that shares an existing SkPixelRef, but only represents a subset. This is to test the first call to cloneGenID in SkBitmap::copyTo. In this case, the genID should NOT be copied, since only a portion of the SkPixelRef was copied. Also test deepCopy on this subset. TestIndividualCopy now takes a parameter stating whether the genID should change in the copy. It also does a read back using the appropriate subset. It no longer differentiates between copyTo and deepCopyTo, since that distinction was only necessary for copying from/to configs other than 8888 (which are no longer being tested), where copyTo did a read back in 8888 and then drew the result to the desired config (resulting in an imperfect copy). BUG=skia:1742 Committed: http://code.google.com/p/skia/source/detail?r=13021 R=mtklein@google.com Review URL: https://codereview.chromium.org/112113005 git-svn-id: http://skia.googlecode.com/svn/trunk@13090 2bbb7eff-a529-9590-31e7-b0007b416f81
2014-01-15 16:56:52 +00:00
static const Pair gPairs[] = {
{ SkBitmap::kNo_Config, "0000000" },
{ SkBitmap::kA8_Config, "0101010" },
{ SkBitmap::kIndex8_Config, "0111010" },
{ SkBitmap::kRGB_565_Config, "0101010" },
{ SkBitmap::kARGB_4444_Config, "0101110" },
{ SkBitmap::kARGB_8888_Config, "0101110" },
};
Reland "Fix genID cloning bugs." SkBitmap.cpp: When copyTo calls readPixels, only clone the genID if the resulting SkPixelRef has the same dimensions as the original. This catches a bug where copying an SkBitmap representing the subset of an SkPixelRef (which implements onReadPixels) would result in the copy sharing the genID. (Thanks to r6710, this case can only happen using setPixelRef, so the updated GpuBitmapCopyTest checks for that.) Move some unnecessary NULL checks to asserts. When copyTo performs a memcpy, only clone the genID if the resulting SkPixelRef has the same dimensions as the original. This catches a bug where copying an extracted SkBitmap with the same width as its original SkPixelRef would incorrectly have the same genID. Add a comment and assert in deepCopyTo, when cloning the genID, since that case correctly clones it. BitmapCopyTest.cpp: Pull redundant work out of the inner loop (setting up the source bitmaps and testing extractSubset). Create a new inner loop for extractSubset, to test copying the result to each different config. Extract a subset that has the same width as the original, to catch the bug mentioned above. Remove the reporter assert which checks for the resulting rowbytes. Add checks to ensure that copying the extracted subset changes the genID. GpuBitmapCopyTest: Create an SkBitmap that shares an existing SkPixelRef, but only represents a subset. This is to test the first call to cloneGenID in SkBitmap::copyTo. In this case, the genID should NOT be copied, since only a portion of the SkPixelRef was copied. Also test deepCopy on this subset. TestIndividualCopy now takes a parameter stating whether the genID should change in the copy. It also does a read back using the appropriate subset. It no longer differentiates between copyTo and deepCopyTo, since that distinction was only necessary for copying from/to configs other than 8888 (which are no longer being tested), where copyTo did a read back in 8888 and then drew the result to the desired config (resulting in an imperfect copy). BUG=skia:1742 Committed: http://code.google.com/p/skia/source/detail?r=13021 R=mtklein@google.com Review URL: https://codereview.chromium.org/112113005 git-svn-id: http://skia.googlecode.com/svn/trunk@13090 2bbb7eff-a529-9590-31e7-b0007b416f81
2014-01-15 16:56:52 +00:00
static const int W = 20;
static const int H = 33;
Reland "Fix genID cloning bugs." SkBitmap.cpp: When copyTo calls readPixels, only clone the genID if the resulting SkPixelRef has the same dimensions as the original. This catches a bug where copying an SkBitmap representing the subset of an SkPixelRef (which implements onReadPixels) would result in the copy sharing the genID. (Thanks to r6710, this case can only happen using setPixelRef, so the updated GpuBitmapCopyTest checks for that.) Move some unnecessary NULL checks to asserts. When copyTo performs a memcpy, only clone the genID if the resulting SkPixelRef has the same dimensions as the original. This catches a bug where copying an extracted SkBitmap with the same width as its original SkPixelRef would incorrectly have the same genID. Add a comment and assert in deepCopyTo, when cloning the genID, since that case correctly clones it. BitmapCopyTest.cpp: Pull redundant work out of the inner loop (setting up the source bitmaps and testing extractSubset). Create a new inner loop for extractSubset, to test copying the result to each different config. Extract a subset that has the same width as the original, to catch the bug mentioned above. Remove the reporter assert which checks for the resulting rowbytes. Add checks to ensure that copying the extracted subset changes the genID. GpuBitmapCopyTest: Create an SkBitmap that shares an existing SkPixelRef, but only represents a subset. This is to test the first call to cloneGenID in SkBitmap::copyTo. In this case, the genID should NOT be copied, since only a portion of the SkPixelRef was copied. Also test deepCopy on this subset. TestIndividualCopy now takes a parameter stating whether the genID should change in the copy. It also does a read back using the appropriate subset. It no longer differentiates between copyTo and deepCopyTo, since that distinction was only necessary for copying from/to configs other than 8888 (which are no longer being tested), where copyTo did a read back in 8888 and then drew the result to the desired config (resulting in an imperfect copy). BUG=skia:1742 Committed: http://code.google.com/p/skia/source/detail?r=13021 R=mtklein@google.com Review URL: https://codereview.chromium.org/112113005 git-svn-id: http://skia.googlecode.com/svn/trunk@13090 2bbb7eff-a529-9590-31e7-b0007b416f81
2014-01-15 16:56:52 +00:00
static void setup_src_bitmaps(SkBitmap* srcOpaque, SkBitmap* srcPremul,
SkBitmap::Config config) {
SkColorTable* ctOpaque = NULL;
SkColorTable* ctPremul = NULL;
Fix genID cloning bugs. SkBitmap.cpp: When copyTo calls readPixels, only clone the genID if the resulting SkPixelRef has the same dimensions as the original. This catches a bug where copying an SkBitmap representing the subset of an SkPixelRef (which implements onReadPixels) would result in the copy sharing the genID. (Thanks to r6710, this case can only happen using setPixelRef, so the updated GpuBitmapCopyTest checks for that.) Move some unnecessary NULL checks to asserts. When copyTo performs a memcpy, only clone the genID if the resulting SkPixelRef has the same dimensions as the original. This catches a bug where copying an extracted SkBitmap with the same width as its original SkPixelRef would incorrectly have the same genID. Add a comment and assert in deepCopyTo, when cloning the genID, since that case correctly clones it. BitmapCopyTest.cpp: Pull redundant work out of the inner loop (setting up the source bitmaps and testing extractSubset). Create a new inner loop for extractSubset, to test copying the result to each different config. Extract a subset that has the same width as the original, to catch the bug mentioned above. Remove the reporter assert which checks for the resulting rowbytes. Add checks to ensure that copying the extracted subset changes the genID. GpuBitmapCopyTest: Create an SkBitmap that shares an existing SkPixelRef, but only represents a subset. This is to test the first call to cloneGenID in SkBitmap::copyTo. In this case, the genID should NOT be copied, since only a portion of the SkPixelRef was copied. Also test deepCopy on this subset. TestIndividualCopy now takes a parameter stating whether the genID should change in the copy. It also does a read back using the appropriate subset. It no longer differentiates between copyTo and deepCopyTo, since that distinction was only necessary for copying from/to configs other than 8888 (which are no longer being tested), where copyTo did a read back in 8888 and then drew the result to the desired config (resulting in an imperfect copy). BUG=skia:1742 R=mtklein@google.com, bsalomon@google.com, reed@google.com Author: scroggo@google.com Review URL: https://codereview.chromium.org/112113005 git-svn-id: http://skia.googlecode.com/svn/trunk@13021 2bbb7eff-a529-9590-31e7-b0007b416f81
2014-01-10 17:22:01 +00:00
Reland "Fix genID cloning bugs." SkBitmap.cpp: When copyTo calls readPixels, only clone the genID if the resulting SkPixelRef has the same dimensions as the original. This catches a bug where copying an SkBitmap representing the subset of an SkPixelRef (which implements onReadPixels) would result in the copy sharing the genID. (Thanks to r6710, this case can only happen using setPixelRef, so the updated GpuBitmapCopyTest checks for that.) Move some unnecessary NULL checks to asserts. When copyTo performs a memcpy, only clone the genID if the resulting SkPixelRef has the same dimensions as the original. This catches a bug where copying an extracted SkBitmap with the same width as its original SkPixelRef would incorrectly have the same genID. Add a comment and assert in deepCopyTo, when cloning the genID, since that case correctly clones it. BitmapCopyTest.cpp: Pull redundant work out of the inner loop (setting up the source bitmaps and testing extractSubset). Create a new inner loop for extractSubset, to test copying the result to each different config. Extract a subset that has the same width as the original, to catch the bug mentioned above. Remove the reporter assert which checks for the resulting rowbytes. Add checks to ensure that copying the extracted subset changes the genID. GpuBitmapCopyTest: Create an SkBitmap that shares an existing SkPixelRef, but only represents a subset. This is to test the first call to cloneGenID in SkBitmap::copyTo. In this case, the genID should NOT be copied, since only a portion of the SkPixelRef was copied. Also test deepCopy on this subset. TestIndividualCopy now takes a parameter stating whether the genID should change in the copy. It also does a read back using the appropriate subset. It no longer differentiates between copyTo and deepCopyTo, since that distinction was only necessary for copying from/to configs other than 8888 (which are no longer being tested), where copyTo did a read back in 8888 and then drew the result to the desired config (resulting in an imperfect copy). BUG=skia:1742 Committed: http://code.google.com/p/skia/source/detail?r=13021 R=mtklein@google.com Review URL: https://codereview.chromium.org/112113005 git-svn-id: http://skia.googlecode.com/svn/trunk@13090 2bbb7eff-a529-9590-31e7-b0007b416f81
2014-01-15 16:56:52 +00:00
srcOpaque->setConfig(config, W, H, 0, kOpaque_SkAlphaType);
srcPremul->setConfig(config, W, H, 0, kPremul_SkAlphaType);
if (SkBitmap::kIndex8_Config == config) {
ctOpaque = init_ctable(kOpaque_SkAlphaType);
ctPremul = init_ctable(kPremul_SkAlphaType);
}
srcOpaque->allocPixels(ctOpaque);
srcPremul->allocPixels(ctPremul);
SkSafeUnref(ctOpaque);
SkSafeUnref(ctPremul);
init_src(*srcOpaque);
init_src(*srcPremul);
}
DEF_TEST(BitmapCopy_extractSubset, reporter) {
for (size_t i = 0; i < SK_ARRAY_COUNT(gPairs); i++) {
Reland "Fix genID cloning bugs." SkBitmap.cpp: When copyTo calls readPixels, only clone the genID if the resulting SkPixelRef has the same dimensions as the original. This catches a bug where copying an SkBitmap representing the subset of an SkPixelRef (which implements onReadPixels) would result in the copy sharing the genID. (Thanks to r6710, this case can only happen using setPixelRef, so the updated GpuBitmapCopyTest checks for that.) Move some unnecessary NULL checks to asserts. When copyTo performs a memcpy, only clone the genID if the resulting SkPixelRef has the same dimensions as the original. This catches a bug where copying an extracted SkBitmap with the same width as its original SkPixelRef would incorrectly have the same genID. Add a comment and assert in deepCopyTo, when cloning the genID, since that case correctly clones it. BitmapCopyTest.cpp: Pull redundant work out of the inner loop (setting up the source bitmaps and testing extractSubset). Create a new inner loop for extractSubset, to test copying the result to each different config. Extract a subset that has the same width as the original, to catch the bug mentioned above. Remove the reporter assert which checks for the resulting rowbytes. Add checks to ensure that copying the extracted subset changes the genID. GpuBitmapCopyTest: Create an SkBitmap that shares an existing SkPixelRef, but only represents a subset. This is to test the first call to cloneGenID in SkBitmap::copyTo. In this case, the genID should NOT be copied, since only a portion of the SkPixelRef was copied. Also test deepCopy on this subset. TestIndividualCopy now takes a parameter stating whether the genID should change in the copy. It also does a read back using the appropriate subset. It no longer differentiates between copyTo and deepCopyTo, since that distinction was only necessary for copying from/to configs other than 8888 (which are no longer being tested), where copyTo did a read back in 8888 and then drew the result to the desired config (resulting in an imperfect copy). BUG=skia:1742 Committed: http://code.google.com/p/skia/source/detail?r=13021 R=mtklein@google.com Review URL: https://codereview.chromium.org/112113005 git-svn-id: http://skia.googlecode.com/svn/trunk@13090 2bbb7eff-a529-9590-31e7-b0007b416f81
2014-01-15 16:56:52 +00:00
SkBitmap srcOpaque, srcPremul;
setup_src_bitmaps(&srcOpaque, &srcPremul, gPairs[i].fConfig);
SkBitmap bitmap(srcOpaque);
SkBitmap subset;
SkIRect r;
// Extract a subset which has the same width as the original. This
// catches a bug where we cloned the genID incorrectly.
r.set(0, 1, W, 3);
bitmap.setIsVolatile(true);
if (bitmap.extractSubset(&subset, r)) {
REPORTER_ASSERT(reporter, subset.width() == W);
REPORTER_ASSERT(reporter, subset.height() == 2);
REPORTER_ASSERT(reporter, subset.alphaType() == bitmap.alphaType());
REPORTER_ASSERT(reporter, subset.isVolatile() == true);
// Test copying an extracted subset.
for (size_t j = 0; j < SK_ARRAY_COUNT(gPairs); j++) {
SkBitmap copy;
bool success = subset.copyTo(&copy, gPairs[j].fConfig);
if (!success) {
// Skip checking that success matches fValid, which is redundant
// with the code below.
REPORTER_ASSERT(reporter, gPairs[i].fConfig != gPairs[j].fConfig);
continue;
}
// When performing a copy of an extracted subset, the gen id should
// change.
REPORTER_ASSERT(reporter, copy.getGenerationID() != subset.getGenerationID());
Reland "Fix genID cloning bugs." SkBitmap.cpp: When copyTo calls readPixels, only clone the genID if the resulting SkPixelRef has the same dimensions as the original. This catches a bug where copying an SkBitmap representing the subset of an SkPixelRef (which implements onReadPixels) would result in the copy sharing the genID. (Thanks to r6710, this case can only happen using setPixelRef, so the updated GpuBitmapCopyTest checks for that.) Move some unnecessary NULL checks to asserts. When copyTo performs a memcpy, only clone the genID if the resulting SkPixelRef has the same dimensions as the original. This catches a bug where copying an extracted SkBitmap with the same width as its original SkPixelRef would incorrectly have the same genID. Add a comment and assert in deepCopyTo, when cloning the genID, since that case correctly clones it. BitmapCopyTest.cpp: Pull redundant work out of the inner loop (setting up the source bitmaps and testing extractSubset). Create a new inner loop for extractSubset, to test copying the result to each different config. Extract a subset that has the same width as the original, to catch the bug mentioned above. Remove the reporter assert which checks for the resulting rowbytes. Add checks to ensure that copying the extracted subset changes the genID. GpuBitmapCopyTest: Create an SkBitmap that shares an existing SkPixelRef, but only represents a subset. This is to test the first call to cloneGenID in SkBitmap::copyTo. In this case, the genID should NOT be copied, since only a portion of the SkPixelRef was copied. Also test deepCopy on this subset. TestIndividualCopy now takes a parameter stating whether the genID should change in the copy. It also does a read back using the appropriate subset. It no longer differentiates between copyTo and deepCopyTo, since that distinction was only necessary for copying from/to configs other than 8888 (which are no longer being tested), where copyTo did a read back in 8888 and then drew the result to the desired config (resulting in an imperfect copy). BUG=skia:1742 Committed: http://code.google.com/p/skia/source/detail?r=13021 R=mtklein@google.com Review URL: https://codereview.chromium.org/112113005 git-svn-id: http://skia.googlecode.com/svn/trunk@13090 2bbb7eff-a529-9590-31e7-b0007b416f81
2014-01-15 16:56:52 +00:00
REPORTER_ASSERT(reporter, copy.width() == W);
REPORTER_ASSERT(reporter, copy.height() == 2);
Reland "Fix genID cloning bugs." SkBitmap.cpp: When copyTo calls readPixels, only clone the genID if the resulting SkPixelRef has the same dimensions as the original. This catches a bug where copying an SkBitmap representing the subset of an SkPixelRef (which implements onReadPixels) would result in the copy sharing the genID. (Thanks to r6710, this case can only happen using setPixelRef, so the updated GpuBitmapCopyTest checks for that.) Move some unnecessary NULL checks to asserts. When copyTo performs a memcpy, only clone the genID if the resulting SkPixelRef has the same dimensions as the original. This catches a bug where copying an extracted SkBitmap with the same width as its original SkPixelRef would incorrectly have the same genID. Add a comment and assert in deepCopyTo, when cloning the genID, since that case correctly clones it. BitmapCopyTest.cpp: Pull redundant work out of the inner loop (setting up the source bitmaps and testing extractSubset). Create a new inner loop for extractSubset, to test copying the result to each different config. Extract a subset that has the same width as the original, to catch the bug mentioned above. Remove the reporter assert which checks for the resulting rowbytes. Add checks to ensure that copying the extracted subset changes the genID. GpuBitmapCopyTest: Create an SkBitmap that shares an existing SkPixelRef, but only represents a subset. This is to test the first call to cloneGenID in SkBitmap::copyTo. In this case, the genID should NOT be copied, since only a portion of the SkPixelRef was copied. Also test deepCopy on this subset. TestIndividualCopy now takes a parameter stating whether the genID should change in the copy. It also does a read back using the appropriate subset. It no longer differentiates between copyTo and deepCopyTo, since that distinction was only necessary for copying from/to configs other than 8888 (which are no longer being tested), where copyTo did a read back in 8888 and then drew the result to the desired config (resulting in an imperfect copy). BUG=skia:1742 Committed: http://code.google.com/p/skia/source/detail?r=13021 R=mtklein@google.com Review URL: https://codereview.chromium.org/112113005 git-svn-id: http://skia.googlecode.com/svn/trunk@13090 2bbb7eff-a529-9590-31e7-b0007b416f81
2014-01-15 16:56:52 +00:00
if (gPairs[i].fConfig == gPairs[j].fConfig) {
SkAutoLockPixels alp0(subset);
SkAutoLockPixels alp1(copy);
// they should both have, or both not-have, a colortable
bool hasCT = subset.getColorTable() != NULL;
REPORTER_ASSERT(reporter, (copy.getColorTable() != NULL) == hasCT);
}
}
Reland "Fix genID cloning bugs." SkBitmap.cpp: When copyTo calls readPixels, only clone the genID if the resulting SkPixelRef has the same dimensions as the original. This catches a bug where copying an SkBitmap representing the subset of an SkPixelRef (which implements onReadPixels) would result in the copy sharing the genID. (Thanks to r6710, this case can only happen using setPixelRef, so the updated GpuBitmapCopyTest checks for that.) Move some unnecessary NULL checks to asserts. When copyTo performs a memcpy, only clone the genID if the resulting SkPixelRef has the same dimensions as the original. This catches a bug where copying an extracted SkBitmap with the same width as its original SkPixelRef would incorrectly have the same genID. Add a comment and assert in deepCopyTo, when cloning the genID, since that case correctly clones it. BitmapCopyTest.cpp: Pull redundant work out of the inner loop (setting up the source bitmaps and testing extractSubset). Create a new inner loop for extractSubset, to test copying the result to each different config. Extract a subset that has the same width as the original, to catch the bug mentioned above. Remove the reporter assert which checks for the resulting rowbytes. Add checks to ensure that copying the extracted subset changes the genID. GpuBitmapCopyTest: Create an SkBitmap that shares an existing SkPixelRef, but only represents a subset. This is to test the first call to cloneGenID in SkBitmap::copyTo. In this case, the genID should NOT be copied, since only a portion of the SkPixelRef was copied. Also test deepCopy on this subset. TestIndividualCopy now takes a parameter stating whether the genID should change in the copy. It also does a read back using the appropriate subset. It no longer differentiates between copyTo and deepCopyTo, since that distinction was only necessary for copying from/to configs other than 8888 (which are no longer being tested), where copyTo did a read back in 8888 and then drew the result to the desired config (resulting in an imperfect copy). BUG=skia:1742 Committed: http://code.google.com/p/skia/source/detail?r=13021 R=mtklein@google.com Review URL: https://codereview.chromium.org/112113005 git-svn-id: http://skia.googlecode.com/svn/trunk@13090 2bbb7eff-a529-9590-31e7-b0007b416f81
2014-01-15 16:56:52 +00:00
}
bitmap = srcPremul;
bitmap.setIsVolatile(false);
if (bitmap.extractSubset(&subset, r)) {
REPORTER_ASSERT(reporter, subset.alphaType() == bitmap.alphaType());
REPORTER_ASSERT(reporter, subset.isVolatile() == false);
}
}
}
DEF_TEST(BitmapCopy, reporter) {
static const bool isExtracted[] = {
false, true
};
for (size_t i = 0; i < SK_ARRAY_COUNT(gPairs); i++) {
SkBitmap srcOpaque, srcPremul;
setup_src_bitmaps(&srcOpaque, &srcPremul, gPairs[i].fConfig);
for (size_t j = 0; j < SK_ARRAY_COUNT(gPairs); j++) {
SkBitmap dst;
bool success = srcPremul.copyTo(&dst, gPairs[j].fConfig);
bool expected = gPairs[i].fValid[j] != '0';
if (success != expected) {
ERRORF(reporter, "SkBitmap::copyTo from %s to %s. expected %s "
"returned %s", gConfigName[i], gConfigName[j],
boolStr(expected), boolStr(success));
}
bool canSucceed = srcPremul.canCopyTo(gPairs[j].fConfig);
if (success != canSucceed) {
ERRORF(reporter, "SkBitmap::copyTo from %s to %s. returned %s "
"canCopyTo %s", gConfigName[i], gConfigName[j],
boolStr(success), boolStr(canSucceed));
}
if (success) {
REPORTER_ASSERT(reporter, srcPremul.width() == dst.width());
REPORTER_ASSERT(reporter, srcPremul.height() == dst.height());
REPORTER_ASSERT(reporter, dst.config() == gPairs[j].fConfig);
test_isOpaque(reporter, srcOpaque, srcPremul, dst.config());
if (srcPremul.config() == dst.config()) {
SkAutoLockPixels srcLock(srcPremul);
SkAutoLockPixels dstLock(dst);
REPORTER_ASSERT(reporter, srcPremul.readyToDraw());
REPORTER_ASSERT(reporter, dst.readyToDraw());
const char* srcP = (const char*)srcPremul.getAddr(0, 0);
const char* dstP = (const char*)dst.getAddr(0, 0);
REPORTER_ASSERT(reporter, srcP != dstP);
REPORTER_ASSERT(reporter, !memcmp(srcP, dstP,
srcPremul.getSize()));
REPORTER_ASSERT(reporter, srcPremul.getGenerationID() == dst.getGenerationID());
} else {
REPORTER_ASSERT(reporter, srcPremul.getGenerationID() != dst.getGenerationID());
}
} else {
// dst should be unchanged from its initial state
REPORTER_ASSERT(reporter, dst.config() == SkBitmap::kNo_Config);
REPORTER_ASSERT(reporter, dst.width() == 0);
REPORTER_ASSERT(reporter, dst.height() == 0);
}
} // for (size_t j = ...
// Tests for getSafeSize(), getSafeSize64(), copyPixelsTo(),
// copyPixelsFrom().
//
for (size_t copyCase = 0; copyCase < SK_ARRAY_COUNT(isExtracted);
++copyCase) {
// Test copying to/from external buffer.
// Note: the tests below have hard-coded values ---
// Please take care if modifying.
// Tests for getSafeSize64().
// Test with a very large configuration without pixel buffer
// attached.
SkBitmap tstSafeSize;
tstSafeSize.setConfig(gPairs[i].fConfig, 100000000U,
100000000U);
int64_t safeSize = tstSafeSize.computeSafeSize64();
if (safeSize < 0) {
ERRORF(reporter, "getSafeSize64() negative: %s",
getSkConfigName(tstSafeSize));
}
bool sizeFail = false;
// Compare against hand-computed values.
switch (gPairs[i].fConfig) {
case SkBitmap::kNo_Config:
break;
case SkBitmap::kA8_Config:
case SkBitmap::kIndex8_Config:
if (safeSize != 0x2386F26FC10000LL) {
sizeFail = true;
}
break;
case SkBitmap::kRGB_565_Config:
case SkBitmap::kARGB_4444_Config:
if (safeSize != 0x470DE4DF820000LL) {
sizeFail = true;
}
break;
case SkBitmap::kARGB_8888_Config:
if (safeSize != 0x8E1BC9BF040000LL) {
sizeFail = true;
}
break;
default:
break;
}
if (sizeFail) {
ERRORF(reporter, "computeSafeSize64() wrong size: %s",
getSkConfigName(tstSafeSize));
}
int subW = 2;
int subH = 2;
// Create bitmap to act as source for copies and subsets.
SkBitmap src, subset;
SkColorTable* ct = NULL;
if (isExtracted[copyCase]) { // A larger image to extract from.
src.setConfig(gPairs[i].fConfig, 2 * subW + 1, subH);
} else { // Tests expect a 2x2 bitmap, so make smaller.
src.setConfig(gPairs[i].fConfig, subW, subH);
}
if (SkBitmap::kIndex8_Config == src.config()) {
ct = init_ctable(kPremul_SkAlphaType);
}
src.allocPixels(ct);
SkSafeUnref(ct);
// Either copy src or extract into 'subset', which is used
// for subsequent calls to copyPixelsTo/From.
bool srcReady = false;
if (isExtracted[copyCase]) {
// The extractedSubset() test case allows us to test copy-
// ing when src and dst mave possibly different strides.
SkIRect r;
r.set(1, 0, 1 + subW, subH); // 2x2 extracted bitmap
srcReady = src.extractSubset(&subset, r);
} else {
srcReady = src.copyTo(&subset, src.config());
}
// Not all configurations will generate a valid 'subset'.
if (srcReady) {
// Allocate our target buffer 'buf' for all copies.
// To simplify verifying correctness of copies attach
// buf to a SkBitmap, but copies are done using the
// raw buffer pointer.
const size_t bufSize = subH *
SkBitmap::ComputeRowBytes(src.config(), subW) * 2;
SkAutoMalloc autoBuf (bufSize);
uint8_t* buf = static_cast<uint8_t*>(autoBuf.get());
SkBitmap bufBm; // Attach buf to this bitmap.
bool successExpected;
// Set up values for each pixel being copied.
Coordinates coords(subW * subH);
for (int x = 0; x < subW; ++x)
for (int y = 0; y < subH; ++y)
{
int index = y * subW + x;
SkASSERT(index < coords.length);
coords[index]->fX = x;
coords[index]->fY = y;
}
writeCoordPixels(subset, coords);
// Test #1 ////////////////////////////////////////////
// Before/after comparisons easier if we attach buf
// to an appropriately configured SkBitmap.
memset(buf, 0xFF, bufSize);
// Config with stride greater than src but that fits in buf.
bufBm.setConfig(gPairs[i].fConfig, subW, subH,
SkBitmap::ComputeRowBytes(subset.config(), subW) * 2);
bufBm.setPixels(buf);
successExpected = false;
// Then attempt to copy with a stride that is too large
// to fit in the buffer.
REPORTER_ASSERT(reporter,
subset.copyPixelsTo(buf, bufSize, bufBm.rowBytes() * 3)
== successExpected);
if (successExpected)
reportCopyVerification(subset, bufBm, coords,
"copyPixelsTo(buf, bufSize, 1.5*maxRowBytes)",
reporter);
// Test #2 ////////////////////////////////////////////
// This test should always succeed, but in the case
// of extracted bitmaps only because we handle the
// issue of getSafeSize(). Without getSafeSize()
// buffer overrun/read would occur.
memset(buf, 0xFF, bufSize);
bufBm.setConfig(gPairs[i].fConfig, subW, subH,
subset.rowBytes());
bufBm.setPixels(buf);
successExpected = subset.getSafeSize() <= bufSize;
REPORTER_ASSERT(reporter,
subset.copyPixelsTo(buf, bufSize) ==
successExpected);
if (successExpected)
reportCopyVerification(subset, bufBm, coords,
"copyPixelsTo(buf, bufSize)", reporter);
// Test #3 ////////////////////////////////////////////
// Copy with different stride between src and dst.
memset(buf, 0xFF, bufSize);
bufBm.setConfig(gPairs[i].fConfig, subW, subH,
subset.rowBytes()+1);
bufBm.setPixels(buf);
successExpected = true; // Should always work.
REPORTER_ASSERT(reporter,
subset.copyPixelsTo(buf, bufSize,
subset.rowBytes()+1) == successExpected);
if (successExpected)
reportCopyVerification(subset, bufBm, coords,
"copyPixelsTo(buf, bufSize, rowBytes+1)", reporter);
// Test #4 ////////////////////////////////////////////
// Test copy with stride too small.
memset(buf, 0xFF, bufSize);
bufBm.setConfig(gPairs[i].fConfig, subW, subH);
bufBm.setPixels(buf);
successExpected = false;
// Request copy with stride too small.
REPORTER_ASSERT(reporter,
subset.copyPixelsTo(buf, bufSize, bufBm.rowBytes()-1)
== successExpected);
if (successExpected)
reportCopyVerification(subset, bufBm, coords,
"copyPixelsTo(buf, bufSize, rowBytes()-1)", reporter);
#if 0 // copyPixelsFrom is gone
// Test #5 ////////////////////////////////////////////
// Tests the case where the source stride is too small
// for the source configuration.
memset(buf, 0xFF, bufSize);
bufBm.setConfig(gPairs[i].fConfig, subW, subH);
bufBm.setPixels(buf);
writeCoordPixels(bufBm, coords);
REPORTER_ASSERT(reporter,
subset.copyPixelsFrom(buf, bufSize, 1) == false);
// Test #6 ///////////////////////////////////////////
// Tests basic copy from an external buffer to the bitmap.
// If the bitmap is "extracted", this also tests the case
// where the source stride is different from the dest.
// stride.
// We've made the buffer large enough to always succeed.
bufBm.setConfig(gPairs[i].fConfig, subW, subH);
bufBm.setPixels(buf);
writeCoordPixels(bufBm, coords);
REPORTER_ASSERT(reporter,
subset.copyPixelsFrom(buf, bufSize, bufBm.rowBytes()) ==
true);
reportCopyVerification(bufBm, subset, coords,
"copyPixelsFrom(buf, bufSize)",
reporter);
// Test #7 ////////////////////////////////////////////
// Tests the case where the source buffer is too small
// for the transfer.
REPORTER_ASSERT(reporter,
subset.copyPixelsFrom(buf, 1, subset.rowBytes()) ==
false);
#endif
}
} // for (size_t copyCase ...
}
}