2017-06-21 19:55:46 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2017 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// This is a GPU-backend specific test. It relies on static intializers to work
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkTypes.h"
|
2017-06-21 19:55:46 +00:00
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkSurface.h"
|
|
|
|
#include "include/gpu/GrTexture.h"
|
|
|
|
#include "src/gpu/GrContextPriv.h"
|
|
|
|
#include "src/gpu/GrGpu.h"
|
2019-09-30 16:15:30 +00:00
|
|
|
#include "src/gpu/GrImageInfo.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "src/gpu/GrResourceProvider.h"
|
2019-06-18 13:58:02 +00:00
|
|
|
#include "src/gpu/GrSurfaceProxy.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "src/gpu/SkGr.h"
|
|
|
|
#include "tests/Test.h"
|
2019-07-25 14:00:52 +00:00
|
|
|
#include "tests/TestUtils.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "tools/gpu/GrContextFactory.h"
|
2017-06-21 19:55:46 +00:00
|
|
|
|
|
|
|
using sk_gpu_test::GrContextFactory;
|
|
|
|
|
|
|
|
void fill_transfer_data(int left, int top, int width, int height, int bufferWidth,
|
2019-07-16 13:47:32 +00:00
|
|
|
GrColorType dstType, char* dst) {
|
|
|
|
size_t dstBpp = GrColorTypeBytesPerPixel(dstType);
|
|
|
|
auto dstLocation = [dst, dstBpp, bufferWidth](int x, int y) {
|
|
|
|
return dst + y * dstBpp * bufferWidth + x * dstBpp;
|
|
|
|
};
|
2017-06-21 19:55:46 +00:00
|
|
|
// build red-green gradient
|
|
|
|
for (int j = top; j < top + height; ++j) {
|
|
|
|
for (int i = left; i < left + width; ++i) {
|
2019-07-26 16:12:26 +00:00
|
|
|
auto r = (unsigned int)(256.f*((i - left) / (float)width));
|
|
|
|
auto g = (unsigned int)(256.f*((j - top) / (float)height));
|
|
|
|
r -= (r >> 8);
|
|
|
|
g -= (g >> 8);
|
|
|
|
// set b and a channels to be inverse of r and g just to have interesting values to
|
|
|
|
// test.
|
|
|
|
uint32_t srcPixel = GrColorPackRGBA(r, g, 0xff - r, 0xff - g);
|
2019-09-30 16:15:30 +00:00
|
|
|
GrImageInfo srcInfo(GrColorType::kRGBA_8888, kUnpremul_SkAlphaType, nullptr, 1, 1);
|
|
|
|
GrImageInfo dstInfo(dstType, kUnpremul_SkAlphaType, nullptr, 1, 1);
|
2019-07-16 13:47:32 +00:00
|
|
|
GrConvertPixels(dstInfo, dstLocation(i, j), dstBpp, srcInfo, &srcPixel, 4);
|
2017-06-21 19:55:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-26 18:10:33 +00:00
|
|
|
void determine_tolerances(GrColorType a, GrColorType b, float tolerances[4]) {
|
2019-07-29 14:09:21 +00:00
|
|
|
std::fill_n(tolerances, 4, 0);
|
|
|
|
|
2019-07-26 18:10:33 +00:00
|
|
|
auto descA = GrGetColorTypeDesc(a);
|
|
|
|
auto descB = GrGetColorTypeDesc(b);
|
|
|
|
// For each channel x set the tolerance to 1 / (2^min(bits_in_a, bits_in_b) - 1) unless
|
|
|
|
// one color type is missing the channel. In that case leave it at 0. If the other color
|
|
|
|
// has the channel then it better be exactly 1 for alpha or 0 for rgb.
|
|
|
|
for (int i = 0; i < 4; ++i) {
|
|
|
|
if (descA[i] != descB[i]) {
|
|
|
|
auto m = std::min(descA[i], descB[i]);
|
|
|
|
if (m) {
|
|
|
|
tolerances[i] = 1.f / (m - 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-01 19:19:29 +00:00
|
|
|
bool read_pixels_from_texture(GrTexture* texture, GrColorType colorType, char* dst,
|
2019-07-26 16:12:26 +00:00
|
|
|
float tolerances[4]) {
|
2019-07-16 13:47:32 +00:00
|
|
|
auto* context = texture->getContext();
|
|
|
|
auto* gpu = context->priv().getGpu();
|
|
|
|
auto* caps = context->priv().caps();
|
|
|
|
|
|
|
|
int w = texture->width();
|
|
|
|
int h = texture->height();
|
2019-08-01 19:19:29 +00:00
|
|
|
size_t rowBytes = GrColorTypeBytesPerPixel(colorType) * w;
|
2019-07-17 20:02:00 +00:00
|
|
|
|
|
|
|
GrColorType srcCT = GrPixelConfigToColorType(texture->config());
|
2019-07-16 13:47:32 +00:00
|
|
|
|
2019-08-01 19:19:29 +00:00
|
|
|
GrCaps::SupportedRead supportedRead =
|
|
|
|
caps->supportedReadPixelsColorType(srcCT, texture->backendFormat(), colorType);
|
2019-07-26 16:12:26 +00:00
|
|
|
std::fill_n(tolerances, 4, 0);
|
2019-08-01 19:19:29 +00:00
|
|
|
if (supportedRead.fColorType != colorType) {
|
2019-07-16 13:47:32 +00:00
|
|
|
size_t tmpRowBytes = GrColorTypeBytesPerPixel(supportedRead.fColorType) * w;
|
|
|
|
std::unique_ptr<char[]> tmpPixels(new char[tmpRowBytes * h]);
|
2019-08-01 19:19:29 +00:00
|
|
|
if (!gpu->readPixels(texture, 0, 0, w, h, colorType, supportedRead.fColorType,
|
|
|
|
tmpPixels.get(), tmpRowBytes)) {
|
2019-07-16 13:47:32 +00:00
|
|
|
return false;
|
2017-06-21 19:55:46 +00:00
|
|
|
}
|
2019-09-30 16:15:30 +00:00
|
|
|
GrImageInfo tmpInfo(supportedRead.fColorType, kUnpremul_SkAlphaType, nullptr, w, h);
|
|
|
|
GrImageInfo dstInfo(colorType, kUnpremul_SkAlphaType, nullptr, w, h);
|
2019-07-26 18:10:33 +00:00
|
|
|
determine_tolerances(tmpInfo.colorType(), dstInfo.colorType(), tolerances);
|
2019-08-01 00:12:02 +00:00
|
|
|
return GrConvertPixels(dstInfo, dst, rowBytes, tmpInfo, tmpPixels.get(), tmpRowBytes,
|
|
|
|
false);
|
2017-06-21 19:55:46 +00:00
|
|
|
}
|
2019-08-01 19:19:29 +00:00
|
|
|
return gpu->readPixels(texture, 0, 0, w, h, colorType, supportedRead.fColorType, dst, rowBytes);
|
2017-06-21 19:55:46 +00:00
|
|
|
}
|
|
|
|
|
2019-04-08 15:59:07 +00:00
|
|
|
void basic_transfer_to_test(skiatest::Reporter* reporter, GrContext* context, GrColorType colorType,
|
2019-07-17 13:59:59 +00:00
|
|
|
GrRenderable renderable) {
|
2019-02-04 18:26:26 +00:00
|
|
|
if (GrCaps::kNone_MapFlags == context->priv().caps()->mapBufferFlags()) {
|
2017-10-09 17:02:49 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-07-16 13:47:32 +00:00
|
|
|
auto* caps = context->priv().caps();
|
|
|
|
|
2019-07-30 16:49:10 +00:00
|
|
|
auto backendFormat = caps->getDefaultBackendFormat(colorType, renderable);
|
|
|
|
if (!backendFormat.isValid()) {
|
2019-07-17 13:59:59 +00:00
|
|
|
return;
|
2019-07-16 20:16:21 +00:00
|
|
|
}
|
2019-07-16 13:47:32 +00:00
|
|
|
|
2019-02-04 18:26:26 +00:00
|
|
|
auto resourceProvider = context->priv().resourceProvider();
|
|
|
|
GrGpu* gpu = context->priv().getGpu();
|
2018-01-16 20:07:54 +00:00
|
|
|
|
2017-06-21 19:55:46 +00:00
|
|
|
const int kTextureWidth = 16;
|
|
|
|
const int kTextureHeight = 16;
|
2019-07-25 17:20:58 +00:00
|
|
|
int srcBufferWidth = caps->writePixelsRowBytesSupport() ? 20 : 16;
|
2017-06-21 19:55:46 +00:00
|
|
|
const int kBufferHeight = 16;
|
|
|
|
|
2019-07-11 14:52:43 +00:00
|
|
|
GrSurfaceDesc desc;
|
|
|
|
desc.fWidth = kTextureWidth;
|
|
|
|
desc.fHeight = kTextureHeight;
|
|
|
|
desc.fConfig = GrColorTypeToPixelConfig(colorType);
|
|
|
|
|
2019-09-17 13:01:56 +00:00
|
|
|
sk_sp<GrTexture> tex =
|
|
|
|
resourceProvider->createTexture(desc, backendFormat, renderable, 1, GrMipMapped::kNo,
|
|
|
|
SkBudgeted::kNo, GrProtected::kNo);
|
2019-07-11 14:52:43 +00:00
|
|
|
if (!tex) {
|
2019-07-16 13:47:32 +00:00
|
|
|
ERRORF(reporter, "Could not create texture");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-07-26 16:12:26 +00:00
|
|
|
// We validate the results using GrGpu::readPixels, so exit if this is not supported.
|
|
|
|
// TODO: Do this through GrSurfaceContext once it works for all color types or support
|
|
|
|
// kCopyToTexture2D here.
|
|
|
|
if (GrCaps::SurfaceReadPixelsSupport::kSupported !=
|
|
|
|
caps->surfaceSupportsReadPixels(tex.get())) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// GL requires a texture to be framebuffer bindable to call glReadPixels. However, we have not
|
|
|
|
// incorporated that test into surfaceSupportsReadPixels(). TODO: Remove this once we handle
|
|
|
|
// drawing to a bindable format.
|
2019-08-06 16:05:31 +00:00
|
|
|
if (!caps->isFormatAsColorTypeRenderable(colorType, tex->backendFormat())) {
|
2019-07-26 16:12:26 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-07-16 13:47:32 +00:00
|
|
|
// The caps tell us what color type we are allowed to upload and read back from this texture,
|
|
|
|
// either of which may differ from 'colorType'.
|
2019-07-26 16:12:26 +00:00
|
|
|
GrCaps::SupportedWrite allowedSrc =
|
2019-08-02 13:57:21 +00:00
|
|
|
caps->supportedWritePixelsColorType(colorType, tex->backendFormat(), colorType);
|
2019-07-26 16:12:26 +00:00
|
|
|
size_t srcRowBytes = GrColorTypeBytesPerPixel(allowedSrc.fColorType) * srcBufferWidth;
|
2019-07-25 17:20:58 +00:00
|
|
|
std::unique_ptr<char[]> srcData(new char[kTextureHeight * srcRowBytes]);
|
2019-07-16 13:47:32 +00:00
|
|
|
|
2019-07-26 16:12:26 +00:00
|
|
|
fill_transfer_data(0, 0, kTextureWidth, kTextureHeight, srcBufferWidth, allowedSrc.fColorType,
|
2019-07-16 13:47:32 +00:00
|
|
|
srcData.get());
|
|
|
|
|
|
|
|
// create and fill transfer buffer
|
|
|
|
size_t size = srcRowBytes * kBufferHeight;
|
|
|
|
sk_sp<GrGpuBuffer> buffer(resourceProvider->createBuffer(size, GrGpuBufferType::kXferCpuToGpu,
|
|
|
|
kDynamic_GrAccessPattern));
|
|
|
|
if (!buffer) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
void* data = buffer->map();
|
|
|
|
if (!buffer) {
|
|
|
|
ERRORF(reporter, "Could not map buffer");
|
2019-07-11 14:52:43 +00:00
|
|
|
return;
|
|
|
|
}
|
2019-07-16 13:47:32 +00:00
|
|
|
memcpy(data, srcData.get(), size);
|
|
|
|
buffer->unmap();
|
2017-06-21 19:55:46 +00:00
|
|
|
|
2019-07-11 14:52:43 +00:00
|
|
|
//////////////////////////
|
|
|
|
// transfer full data
|
2018-02-20 19:05:36 +00:00
|
|
|
|
2019-07-11 14:52:43 +00:00
|
|
|
bool result;
|
2019-08-01 19:19:29 +00:00
|
|
|
result = gpu->transferPixelsTo(tex.get(), 0, 0, kTextureWidth, kTextureHeight, colorType,
|
2019-07-26 16:12:26 +00:00
|
|
|
allowedSrc.fColorType, buffer.get(), 0, srcRowBytes);
|
2019-07-11 14:52:43 +00:00
|
|
|
REPORTER_ASSERT(reporter, result);
|
|
|
|
|
2019-07-16 13:47:32 +00:00
|
|
|
size_t dstRowBytes = GrColorTypeBytesPerPixel(colorType) * kTextureWidth;
|
|
|
|
std::unique_ptr<char[]> dstBuffer(new char[dstRowBytes * kTextureHeight]());
|
|
|
|
|
2019-07-26 16:12:26 +00:00
|
|
|
float compareTolerances[4] = {};
|
|
|
|
result = read_pixels_from_texture(tex.get(), colorType, dstBuffer.get(), compareTolerances);
|
2019-07-16 13:47:32 +00:00
|
|
|
if (!result) {
|
2019-07-26 16:12:26 +00:00
|
|
|
ERRORF(reporter, "Could not read pixels from texture, color type: %d",
|
|
|
|
static_cast<int>(colorType));
|
2019-07-16 13:47:32 +00:00
|
|
|
return;
|
2019-07-11 14:52:43 +00:00
|
|
|
}
|
2017-06-21 19:55:46 +00:00
|
|
|
|
2019-07-16 13:47:32 +00:00
|
|
|
auto error = std::function<ComparePixmapsErrorReporter>(
|
|
|
|
[reporter, colorType](int x, int y, const float diffs[4]) {
|
2019-07-26 16:12:26 +00:00
|
|
|
ERRORF(reporter,
|
|
|
|
"Error at (%d %d) in transfer, color type: %d, diffs: (%f, %f, %f, %f)", x,
|
|
|
|
y, colorType, diffs[0], diffs[1], diffs[2], diffs[3]);
|
2019-07-16 13:47:32 +00:00
|
|
|
});
|
2019-09-30 16:15:30 +00:00
|
|
|
GrImageInfo srcInfo(allowedSrc.fColorType, kUnpremul_SkAlphaType, nullptr, tex->width(),
|
2019-07-26 16:12:26 +00:00
|
|
|
tex->height());
|
2019-09-30 16:15:30 +00:00
|
|
|
GrImageInfo dstInfo(colorType, kUnpremul_SkAlphaType, nullptr, tex->width(), tex->height());
|
2019-10-25 00:07:39 +00:00
|
|
|
ComparePixels(srcInfo, srcData.get(), srcRowBytes, dstInfo, dstBuffer.get(), dstRowBytes,
|
|
|
|
compareTolerances, error);
|
2019-07-16 13:47:32 +00:00
|
|
|
|
2019-07-11 14:52:43 +00:00
|
|
|
//////////////////////////
|
|
|
|
// transfer partial data
|
2019-07-16 13:47:32 +00:00
|
|
|
|
|
|
|
// We're relying on this cap to write partial texture data
|
|
|
|
if (!caps->writePixelsRowBytesSupport()) {
|
2019-07-11 14:52:43 +00:00
|
|
|
return;
|
|
|
|
}
|
2019-07-26 16:12:26 +00:00
|
|
|
// We keep a 1 to 1 correspondence between pixels in the buffer and the entire texture. We
|
|
|
|
// update the contents of a sub-rect of the buffer and push that rect to the texture. We start
|
|
|
|
// with a left sub-rect inset of 2 but may adjust that so we can fulfill the transfer buffer
|
|
|
|
// offset alignment requirement.
|
|
|
|
int left = 2;
|
|
|
|
const int top = 10;
|
|
|
|
const int width = 10;
|
|
|
|
const int height = 2;
|
|
|
|
size_t offset = top * srcRowBytes + left * GrColorTypeBytesPerPixel(allowedSrc.fColorType);
|
|
|
|
while (offset % allowedSrc.fOffsetAlignmentForTransferBuffer) {
|
|
|
|
offset += GrColorTypeBytesPerPixel(allowedSrc.fColorType);
|
|
|
|
++left;
|
|
|
|
// We're assuming that the required alignment is 1 or a small multiple of the bpp, which
|
|
|
|
// it is currently for all color types across all backends.
|
|
|
|
SkASSERT(left + width <= tex->width());
|
|
|
|
}
|
2019-07-11 14:52:43 +00:00
|
|
|
|
|
|
|
// change color of subrectangle
|
2019-07-26 16:12:26 +00:00
|
|
|
fill_transfer_data(left, top, width, height, srcBufferWidth, allowedSrc.fColorType,
|
|
|
|
srcData.get());
|
2019-07-11 14:52:43 +00:00
|
|
|
data = buffer->map();
|
2019-07-16 13:47:32 +00:00
|
|
|
memcpy(data, srcData.get(), size);
|
2019-07-11 14:52:43 +00:00
|
|
|
buffer->unmap();
|
|
|
|
|
2019-08-01 19:19:29 +00:00
|
|
|
result = gpu->transferPixelsTo(tex.get(), left, top, width, height, colorType,
|
|
|
|
allowedSrc.fColorType, buffer.get(), offset, srcRowBytes);
|
2019-07-26 16:12:26 +00:00
|
|
|
if (!result) {
|
2019-08-01 19:19:29 +00:00
|
|
|
gpu->transferPixelsTo(tex.get(), left, top, width, height, colorType, allowedSrc.fColorType,
|
2019-07-26 16:12:26 +00:00
|
|
|
buffer.get(), offset, srcRowBytes);
|
|
|
|
ERRORF(reporter, "Could not transfer pixels to texture, color type: %d",
|
|
|
|
static_cast<int>(colorType));
|
|
|
|
return;
|
|
|
|
}
|
2019-07-11 14:52:43 +00:00
|
|
|
|
2019-07-26 16:12:26 +00:00
|
|
|
result = read_pixels_from_texture(tex.get(), colorType, dstBuffer.get(), compareTolerances);
|
2019-07-16 13:47:32 +00:00
|
|
|
if (!result) {
|
2019-07-26 16:12:26 +00:00
|
|
|
ERRORF(reporter, "Could not read pixels from texture, color type: %d",
|
|
|
|
static_cast<int>(colorType));
|
2019-07-16 13:47:32 +00:00
|
|
|
return;
|
2017-06-27 14:36:56 +00:00
|
|
|
}
|
2019-10-25 00:07:39 +00:00
|
|
|
ComparePixels(srcInfo, srcData.get(), srcRowBytes, dstInfo, dstBuffer.get(), dstRowBytes,
|
|
|
|
compareTolerances, error);
|
2017-06-21 19:55:46 +00:00
|
|
|
}
|
|
|
|
|
2019-04-09 19:00:05 +00:00
|
|
|
void basic_transfer_from_test(skiatest::Reporter* reporter, const sk_gpu_test::ContextInfo& ctxInfo,
|
2019-07-17 13:59:59 +00:00
|
|
|
GrColorType colorType, GrRenderable renderable) {
|
2019-04-09 19:00:05 +00:00
|
|
|
auto context = ctxInfo.grContext();
|
2019-07-16 13:47:32 +00:00
|
|
|
auto caps = context->priv().caps();
|
|
|
|
if (GrCaps::kNone_MapFlags == caps->mapBufferFlags()) {
|
2019-04-08 15:59:07 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto resourceProvider = context->priv().resourceProvider();
|
|
|
|
GrGpu* gpu = context->priv().getGpu();
|
|
|
|
|
|
|
|
const int kTextureWidth = 16;
|
|
|
|
const int kTextureHeight = 16;
|
|
|
|
|
|
|
|
// We'll do a full texture read into the buffer followed by a partial read. These values
|
|
|
|
// describe the partial read subrect.
|
|
|
|
const int kPartialLeft = 2;
|
|
|
|
const int kPartialTop = 10;
|
|
|
|
const int kPartialWidth = 10;
|
|
|
|
const int kPartialHeight = 2;
|
|
|
|
|
2019-07-11 14:52:43 +00:00
|
|
|
// create texture
|
|
|
|
GrSurfaceDesc desc;
|
|
|
|
desc.fWidth = kTextureWidth;
|
|
|
|
desc.fHeight = kTextureHeight;
|
|
|
|
desc.fConfig = GrColorTypeToPixelConfig(colorType);
|
|
|
|
|
2019-08-05 16:58:39 +00:00
|
|
|
auto format = context->priv().caps()->getDefaultBackendFormat(colorType, renderable);
|
|
|
|
if (!format.isValid()) {
|
2019-07-11 14:52:43 +00:00
|
|
|
return;
|
|
|
|
}
|
2019-04-08 15:59:07 +00:00
|
|
|
|
2019-07-16 13:47:32 +00:00
|
|
|
size_t textureDataBpp = GrColorTypeBytesPerPixel(colorType);
|
|
|
|
size_t textureDataRowBytes = kTextureWidth * textureDataBpp;
|
|
|
|
std::unique_ptr<char[]> textureData(new char[kTextureHeight * textureDataRowBytes]);
|
|
|
|
fill_transfer_data(0, 0, kTextureWidth, kTextureHeight, kTextureWidth, colorType,
|
|
|
|
textureData.get());
|
2019-07-11 14:52:43 +00:00
|
|
|
GrMipLevel data;
|
|
|
|
data.fPixels = textureData.get();
|
2019-07-16 13:47:32 +00:00
|
|
|
data.fRowBytes = textureDataRowBytes;
|
2019-08-05 16:58:39 +00:00
|
|
|
sk_sp<GrTexture> tex = resourceProvider->createTexture(
|
2019-09-17 13:01:56 +00:00
|
|
|
desc, format, colorType, renderable, 1, SkBudgeted::kNo, GrProtected::kNo, &data, 1);
|
2019-07-11 14:52:43 +00:00
|
|
|
if (!tex) {
|
|
|
|
return;
|
|
|
|
}
|
2019-04-08 15:59:07 +00:00
|
|
|
|
2019-07-26 18:10:33 +00:00
|
|
|
if (GrCaps::SurfaceReadPixelsSupport::kSupported !=
|
|
|
|
caps->surfaceSupportsReadPixels(tex.get())) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// GL requires a texture to be framebuffer bindable to call glReadPixels. However, we have not
|
|
|
|
// incorporated that test into surfaceSupportsReadPixels(). TODO: Remove this once we handle
|
|
|
|
// drawing to a bindable format.
|
2019-08-06 16:05:31 +00:00
|
|
|
if (!caps->isFormatAsColorTypeRenderable(colorType, tex->backendFormat())) {
|
2019-07-26 18:10:33 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-07-16 13:47:32 +00:00
|
|
|
// Create the transfer buffer.
|
|
|
|
auto allowedRead =
|
2019-07-18 18:28:01 +00:00
|
|
|
caps->supportedReadPixelsColorType(colorType, tex->backendFormat(), colorType);
|
2019-09-30 16:15:30 +00:00
|
|
|
GrImageInfo readInfo(allowedRead.fColorType, kUnpremul_SkAlphaType, nullptr, kTextureWidth,
|
2019-07-16 13:47:32 +00:00
|
|
|
kTextureHeight);
|
|
|
|
|
|
|
|
size_t bpp = GrColorTypeBytesPerPixel(allowedRead.fColorType);
|
|
|
|
size_t fullBufferRowBytes = kTextureWidth * bpp;
|
|
|
|
size_t partialBufferRowBytes = kPartialWidth * bpp;
|
2019-07-26 13:14:01 +00:00
|
|
|
size_t offsetAlignment = allowedRead.fOffsetAlignmentForTransferBuffer;
|
2019-07-16 13:47:32 +00:00
|
|
|
SkASSERT(offsetAlignment);
|
|
|
|
|
|
|
|
size_t bufferSize = fullBufferRowBytes * kTextureHeight;
|
|
|
|
// Arbitrary starting offset for the partial read.
|
|
|
|
size_t partialReadOffset = GrSizeAlignUp(11, offsetAlignment);
|
|
|
|
bufferSize = SkTMax(bufferSize, partialReadOffset + partialBufferRowBytes * kPartialHeight);
|
|
|
|
|
|
|
|
sk_sp<GrGpuBuffer> buffer(resourceProvider->createBuffer(
|
|
|
|
bufferSize, GrGpuBufferType::kXferGpuToCpu, kDynamic_GrAccessPattern));
|
|
|
|
REPORTER_ASSERT(reporter, buffer);
|
|
|
|
if (!buffer) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int expectedTransferCnt = 0;
|
|
|
|
gpu->stats()->reset();
|
|
|
|
|
2019-07-11 14:52:43 +00:00
|
|
|
//////////////////////////
|
|
|
|
// transfer full data
|
2019-08-01 19:19:29 +00:00
|
|
|
bool result = gpu->transferPixelsFrom(tex.get(), 0, 0, kTextureWidth, kTextureHeight, colorType,
|
2019-07-16 13:47:32 +00:00
|
|
|
allowedRead.fColorType, buffer.get(), 0);
|
2019-07-11 14:52:43 +00:00
|
|
|
if (!result) {
|
|
|
|
ERRORF(reporter, "transferPixelsFrom failed.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
++expectedTransferCnt;
|
2019-04-08 15:59:07 +00:00
|
|
|
|
2019-07-11 14:52:43 +00:00
|
|
|
GrFlushInfo flushInfo;
|
|
|
|
flushInfo.fFlags = kSyncCpu_GrFlushFlag;
|
|
|
|
if (context->priv().caps()->mapBufferFlags() & GrCaps::kAsyncRead_MapFlag) {
|
|
|
|
gpu->finishFlush(nullptr, 0, SkSurface::BackendSurfaceAccess::kNoAccess, flushInfo,
|
|
|
|
GrPrepareForExternalIORequests());
|
|
|
|
}
|
2019-04-08 15:59:07 +00:00
|
|
|
|
2019-07-16 13:47:32 +00:00
|
|
|
// Copy the transfer buffer contents to a temporary so we can manipulate it.
|
|
|
|
const auto* map = reinterpret_cast<const char*>(buffer->map());
|
2019-07-11 14:52:43 +00:00
|
|
|
REPORTER_ASSERT(reporter, map);
|
|
|
|
if (!map) {
|
2019-07-16 13:47:32 +00:00
|
|
|
ERRORF(reporter, "Failed to map transfer buffer.");
|
2019-07-11 14:52:43 +00:00
|
|
|
return;
|
|
|
|
}
|
2019-07-16 13:47:32 +00:00
|
|
|
std::unique_ptr<char[]> transferData(new char[kTextureHeight * fullBufferRowBytes]);
|
|
|
|
memcpy(transferData.get(), map, fullBufferRowBytes * kTextureHeight);
|
2019-07-11 14:52:43 +00:00
|
|
|
buffer->unmap();
|
2019-04-08 15:59:07 +00:00
|
|
|
|
2019-09-30 16:15:30 +00:00
|
|
|
GrImageInfo transferInfo(allowedRead.fColorType, kUnpremul_SkAlphaType, nullptr, kTextureWidth,
|
2019-07-16 13:47:32 +00:00
|
|
|
kTextureHeight);
|
|
|
|
|
2019-07-26 18:10:33 +00:00
|
|
|
float tol[4];
|
|
|
|
determine_tolerances(allowedRead.fColorType, colorType, tol);
|
2019-07-16 13:47:32 +00:00
|
|
|
auto error = std::function<ComparePixmapsErrorReporter>(
|
|
|
|
[reporter, colorType](int x, int y, const float diffs[4]) {
|
2019-07-26 16:12:26 +00:00
|
|
|
ERRORF(reporter,
|
|
|
|
"Error at (%d %d) in transfer, color type: %d, diffs: (%f, %f, %f, %f)", x,
|
|
|
|
y, colorType, diffs[0], diffs[1], diffs[2], diffs[3]);
|
2019-07-16 13:47:32 +00:00
|
|
|
});
|
2019-09-30 16:15:30 +00:00
|
|
|
GrImageInfo textureDataInfo(colorType, kUnpremul_SkAlphaType, nullptr, kTextureWidth,
|
2019-07-16 13:47:32 +00:00
|
|
|
kTextureHeight);
|
2019-10-25 00:07:39 +00:00
|
|
|
ComparePixels(textureDataInfo, textureData.get(), textureDataRowBytes, transferInfo,
|
|
|
|
transferData.get(), fullBufferRowBytes, tol, error);
|
2019-07-16 13:47:32 +00:00
|
|
|
|
2019-07-11 14:52:43 +00:00
|
|
|
///////////////////////
|
|
|
|
// Now test a partial read at an offset into the buffer.
|
|
|
|
result = gpu->transferPixelsFrom(tex.get(), kPartialLeft, kPartialTop, kPartialWidth,
|
2019-08-01 19:19:29 +00:00
|
|
|
kPartialHeight, colorType, allowedRead.fColorType,
|
|
|
|
buffer.get(), partialReadOffset);
|
2019-07-11 14:52:43 +00:00
|
|
|
if (!result) {
|
|
|
|
ERRORF(reporter, "transferPixelsFrom failed.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
++expectedTransferCnt;
|
2019-04-08 15:59:07 +00:00
|
|
|
|
2019-07-11 14:52:43 +00:00
|
|
|
if (context->priv().caps()->mapBufferFlags() & GrCaps::kAsyncRead_MapFlag) {
|
|
|
|
gpu->finishFlush(nullptr, 0, SkSurface::BackendSurfaceAccess::kNoAccess, flushInfo,
|
|
|
|
GrPrepareForExternalIORequests());
|
|
|
|
}
|
2019-04-08 15:59:07 +00:00
|
|
|
|
2019-07-16 13:47:32 +00:00
|
|
|
map = reinterpret_cast<const char*>(buffer->map());
|
2019-07-11 14:52:43 +00:00
|
|
|
REPORTER_ASSERT(reporter, map);
|
|
|
|
if (!map) {
|
2019-07-16 13:47:32 +00:00
|
|
|
ERRORF(reporter, "Failed to map transfer buffer.");
|
2019-07-11 14:52:43 +00:00
|
|
|
return;
|
2019-04-08 15:59:07 +00:00
|
|
|
}
|
2019-07-16 13:47:32 +00:00
|
|
|
const char* bufferStart = reinterpret_cast<const char*>(map) + partialReadOffset;
|
|
|
|
memcpy(transferData.get(), bufferStart, partialBufferRowBytes * kTextureHeight);
|
2019-07-11 14:52:43 +00:00
|
|
|
buffer->unmap();
|
2019-07-16 13:47:32 +00:00
|
|
|
|
|
|
|
transferInfo = transferInfo.makeWH(kPartialWidth, kPartialHeight);
|
|
|
|
const char* textureDataStart =
|
|
|
|
textureData.get() + textureDataRowBytes * kPartialTop + textureDataBpp * kPartialLeft;
|
|
|
|
textureDataInfo = textureDataInfo.makeWH(kPartialWidth, kPartialHeight);
|
2019-10-25 00:07:39 +00:00
|
|
|
ComparePixels(textureDataInfo, textureDataStart, textureDataRowBytes, transferInfo,
|
|
|
|
transferData.get(), partialBufferRowBytes, tol, error);
|
2019-04-08 15:59:07 +00:00
|
|
|
#if GR_GPU_STATS
|
|
|
|
REPORTER_ASSERT(reporter, gpu->stats()->transfersFromSurface() == expectedTransferCnt);
|
|
|
|
#else
|
|
|
|
(void)expectedTransferCnt;
|
|
|
|
#endif
|
|
|
|
}
|
2019-03-18 19:39:22 +00:00
|
|
|
|
2019-04-08 15:59:07 +00:00
|
|
|
DEF_GPUTEST_FOR_RENDERING_CONTEXTS(TransferPixelsToTest, reporter, ctxInfo) {
|
|
|
|
if (!ctxInfo.grContext()->priv().caps()->transferBufferSupport()) {
|
|
|
|
return;
|
|
|
|
}
|
2019-07-17 13:59:59 +00:00
|
|
|
for (auto renderable : {GrRenderable::kNo, GrRenderable::kYes}) {
|
2019-07-26 16:12:26 +00:00
|
|
|
for (auto colorType : {
|
|
|
|
GrColorType::kAlpha_8,
|
|
|
|
GrColorType::kBGR_565,
|
|
|
|
GrColorType::kABGR_4444,
|
|
|
|
GrColorType::kRGBA_8888,
|
|
|
|
GrColorType::kRGBA_8888_SRGB,
|
|
|
|
// GrColorType::kRGB_888x, Broken in GL until we have kRGB_888
|
|
|
|
GrColorType::kRG_88,
|
|
|
|
GrColorType::kBGRA_8888,
|
|
|
|
GrColorType::kRGBA_1010102,
|
2019-11-04 21:59:29 +00:00
|
|
|
GrColorType::kGray_8,
|
2019-07-26 16:12:26 +00:00
|
|
|
GrColorType::kAlpha_F16,
|
|
|
|
GrColorType::kRGBA_F16,
|
|
|
|
GrColorType::kRGBA_F16_Clamped,
|
|
|
|
GrColorType::kRGBA_F32,
|
2019-09-11 21:03:28 +00:00
|
|
|
GrColorType::kAlpha_16,
|
2019-07-26 16:12:26 +00:00
|
|
|
GrColorType::kRG_1616,
|
|
|
|
GrColorType::kRGBA_16161616,
|
|
|
|
GrColorType::kRG_F16,
|
|
|
|
}) {
|
2019-07-17 13:59:59 +00:00
|
|
|
basic_transfer_to_test(reporter, ctxInfo.grContext(), colorType, renderable);
|
|
|
|
}
|
|
|
|
}
|
2017-06-21 19:55:46 +00:00
|
|
|
}
|
2019-03-18 19:39:22 +00:00
|
|
|
|
2019-04-09 18:57:00 +00:00
|
|
|
// TODO(bsalomon): Metal
|
|
|
|
DEF_GPUTEST_FOR_RENDERING_CONTEXTS(TransferPixelsFromTest, reporter, ctxInfo) {
|
2019-04-08 15:59:07 +00:00
|
|
|
if (!ctxInfo.grContext()->priv().caps()->transferBufferSupport()) {
|
|
|
|
return;
|
|
|
|
}
|
2019-07-17 13:59:59 +00:00
|
|
|
for (auto renderable : {GrRenderable::kNo, GrRenderable::kYes}) {
|
2019-07-26 18:10:33 +00:00
|
|
|
for (auto colorType : {
|
2019-11-04 21:59:29 +00:00
|
|
|
GrColorType::kAlpha_8,
|
|
|
|
GrColorType::kAlpha_16,
|
|
|
|
GrColorType::kBGR_565,
|
|
|
|
GrColorType::kABGR_4444,
|
|
|
|
GrColorType::kRGBA_8888,
|
|
|
|
GrColorType::kRGBA_8888_SRGB,
|
|
|
|
// GrColorType::kRGB_888x, Broken in GL until we have kRGB_888
|
|
|
|
GrColorType::kRG_88,
|
|
|
|
GrColorType::kBGRA_8888,
|
|
|
|
GrColorType::kRGBA_1010102,
|
|
|
|
GrColorType::kGray_8,
|
|
|
|
GrColorType::kAlpha_F16,
|
|
|
|
GrColorType::kRGBA_F16,
|
|
|
|
GrColorType::kRGBA_F16_Clamped,
|
|
|
|
GrColorType::kRGBA_F32,
|
|
|
|
GrColorType::kRG_1616,
|
|
|
|
GrColorType::kRGBA_16161616,
|
|
|
|
GrColorType::kRG_F16,
|
|
|
|
}) {
|
2019-07-17 13:59:59 +00:00
|
|
|
basic_transfer_from_test(reporter, ctxInfo, colorType, renderable);
|
|
|
|
}
|
|
|
|
}
|
2019-04-08 15:59:07 +00:00
|
|
|
}
|