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"
|
2020-07-06 14:56:46 +00:00
|
|
|
#include "include/gpu/GrDirectContext.h"
|
2020-10-14 15:23:11 +00:00
|
|
|
#include "src/gpu/GrDirectContextPriv.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#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"
|
2020-03-05 19:14:18 +00:00
|
|
|
#include "src/gpu/GrTexture.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;
|
|
|
|
|
2021-05-18 19:03:27 +00:00
|
|
|
void fill_transfer_data(int left, int top, int width, int height, int rowBytes,
|
2019-07-16 13:47:32 +00:00
|
|
|
GrColorType dstType, char* dst) {
|
|
|
|
size_t dstBpp = GrColorTypeBytesPerPixel(dstType);
|
2021-05-18 19:03:27 +00:00
|
|
|
auto dstLocation = [dst, dstBpp, rowBytes](int x, int y) {
|
|
|
|
return dst + y * rowBytes + x * dstBpp;
|
2019-07-16 13:47:32 +00:00
|
|
|
};
|
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);
|
2021-03-30 20:14:37 +00:00
|
|
|
GrConvertPixels(GrPixmap(dstInfo, dstLocation(i, j), dstBpp),
|
|
|
|
GrPixmap(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
|
|
|
|
2019-08-01 19:19:29 +00:00
|
|
|
GrCaps::SupportedRead supportedRead =
|
2020-01-22 21:53:38 +00:00
|
|
|
caps->supportedReadPixelsColorType(colorType, 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]);
|
2021-05-24 16:40:46 +00:00
|
|
|
if (!gpu->readPixels(texture,
|
|
|
|
SkIRect::MakeWH(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);
|
2021-03-30 20:14:37 +00:00
|
|
|
return GrConvertPixels(GrPixmap(dstInfo, dst, rowBytes),
|
|
|
|
GrPixmap(tmpInfo, tmpPixels.get(), tmpRowBytes));
|
2017-06-21 19:55:46 +00:00
|
|
|
}
|
2021-05-24 16:40:46 +00:00
|
|
|
return gpu->readPixels(texture,
|
|
|
|
SkIRect::MakeWH(w, h),
|
|
|
|
colorType,
|
|
|
|
supportedRead.fColorType,
|
|
|
|
dst,
|
|
|
|
rowBytes);
|
2017-06-21 19:55:46 +00:00
|
|
|
}
|
|
|
|
|
2020-07-23 13:41:57 +00:00
|
|
|
void basic_transfer_to_test(skiatest::Reporter* reporter,
|
|
|
|
GrDirectContext* dContext,
|
|
|
|
GrColorType colorType,
|
2019-07-17 13:59:59 +00:00
|
|
|
GrRenderable renderable) {
|
2020-07-23 13:41:57 +00:00
|
|
|
if (GrCaps::kNone_MapFlags == dContext->priv().caps()->mapBufferFlags()) {
|
2017-10-09 17:02:49 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-07-23 13:41:57 +00:00
|
|
|
auto* caps = dContext->priv().caps();
|
2019-07-16 13:47:32 +00:00
|
|
|
|
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
|
|
|
|
2020-07-23 13:41:57 +00:00
|
|
|
auto resourceProvider = dContext->priv().resourceProvider();
|
|
|
|
GrGpu* gpu = dContext->priv().getGpu();
|
2018-01-16 20:07:54 +00:00
|
|
|
|
2020-02-07 19:17:25 +00:00
|
|
|
static constexpr SkISize kTexDims = {16, 16};
|
2021-06-07 17:39:27 +00:00
|
|
|
int srcBufferWidth = caps->transferPixelsToRowBytesSupport() ? 20 : 16;
|
2017-06-21 19:55:46 +00:00
|
|
|
const int kBufferHeight = 16;
|
|
|
|
|
2019-09-17 13:01:56 +00:00
|
|
|
sk_sp<GrTexture> tex =
|
2020-02-07 19:17:25 +00:00
|
|
|
resourceProvider->createTexture(kTexDims, backendFormat, renderable, 1,
|
2020-07-21 13:27:25 +00:00
|
|
|
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.
|
2021-08-10 17:49:51 +00:00
|
|
|
// TODO: Do this through SurfaceContext once it works for all color types or support
|
2019-07-26 16:12:26 +00:00
|
|
|
// 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);
|
2021-06-24 15:56:50 +00:00
|
|
|
if (!allowedSrc.fOffsetAlignmentForTransferBuffer) {
|
|
|
|
return;
|
|
|
|
}
|
2021-05-18 19:03:27 +00:00
|
|
|
size_t srcRowBytes = GrAlignTo(GrColorTypeBytesPerPixel(allowedSrc.fColorType) * srcBufferWidth,
|
|
|
|
caps->transferBufferAlignment());
|
|
|
|
|
2020-02-07 19:17:25 +00:00
|
|
|
std::unique_ptr<char[]> srcData(new char[kTexDims.fHeight * srcRowBytes]);
|
2019-07-16 13:47:32 +00:00
|
|
|
|
2021-05-18 19:03:27 +00:00
|
|
|
fill_transfer_data(0, 0, kTexDims.fWidth, kTexDims.fHeight, srcRowBytes,
|
2020-02-07 19:17:25 +00:00
|
|
|
allowedSrc.fColorType, srcData.get());
|
2019-07-16 13:47:32 +00:00
|
|
|
|
|
|
|
// 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;
|
2021-05-24 16:40:46 +00:00
|
|
|
result = gpu->transferPixelsTo(tex.get(),
|
|
|
|
SkIRect::MakeSize(kTexDims),
|
|
|
|
colorType,
|
|
|
|
allowedSrc.fColorType,
|
|
|
|
buffer,
|
|
|
|
0,
|
|
|
|
srcRowBytes);
|
2019-07-11 14:52:43 +00:00
|
|
|
REPORTER_ASSERT(reporter, result);
|
|
|
|
|
2020-02-07 19:17:25 +00:00
|
|
|
size_t dstRowBytes = GrColorTypeBytesPerPixel(colorType) * kTexDims.fWidth;
|
|
|
|
std::unique_ptr<char[]> dstBuffer(new char[dstRowBytes * kTexDims.fHeight]());
|
2019-07-16 13:47:32 +00:00
|
|
|
|
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,
|
2020-04-29 12:58:39 +00:00
|
|
|
"Error at (%d %d) in transfer, color type: %s, diffs: (%f, %f, %f, %f)",
|
|
|
|
x, y, GrColorTypeToStr(colorType),
|
|
|
|
diffs[0], diffs[1], diffs[2], diffs[3]);
|
2019-07-16 13:47:32 +00:00
|
|
|
});
|
2021-03-31 18:19:31 +00:00
|
|
|
GrImageInfo srcInfo(allowedSrc.fColorType, kUnpremul_SkAlphaType, nullptr, tex->dimensions());
|
|
|
|
GrImageInfo dstInfo( colorType, kUnpremul_SkAlphaType, nullptr, tex->dimensions());
|
|
|
|
ComparePixels(GrCPixmap(srcInfo, srcData.get(), srcRowBytes),
|
|
|
|
GrCPixmap(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
|
2021-06-07 17:39:27 +00:00
|
|
|
if (!caps->transferPixelsToRowBytesSupport()) {
|
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;
|
2021-05-18 19:03:27 +00:00
|
|
|
int top = 10;
|
2019-07-26 16:12:26 +00:00
|
|
|
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;
|
2021-05-18 19:03:27 +00:00
|
|
|
// In most cases we assume that the required alignment is 1 or a small multiple of the bpp,
|
|
|
|
// which it is for color types across all current backends except Direct3D. To correct for
|
|
|
|
// Direct3D's large alignment requirement we may adjust the top location as well.
|
|
|
|
if (left + width > tex->width()) {
|
|
|
|
left = 0;
|
|
|
|
++top;
|
|
|
|
offset = top * srcRowBytes;
|
|
|
|
}
|
2019-07-26 16:12:26 +00:00
|
|
|
SkASSERT(left + width <= tex->width());
|
2021-05-18 19:03:27 +00:00
|
|
|
SkASSERT(top + height <= tex->height());
|
2019-07-26 16:12:26 +00:00
|
|
|
}
|
2019-07-11 14:52:43 +00:00
|
|
|
|
|
|
|
// change color of subrectangle
|
2021-05-18 19:03:27 +00:00
|
|
|
fill_transfer_data(left, top, width, height, srcRowBytes, allowedSrc.fColorType,
|
2019-07-26 16:12:26 +00:00
|
|
|
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();
|
|
|
|
|
2021-05-24 16:40:46 +00:00
|
|
|
result = gpu->transferPixelsTo(tex.get(),
|
|
|
|
SkIRect::MakeXYWH(left, top, width, height),
|
|
|
|
colorType,
|
|
|
|
allowedSrc.fColorType,
|
|
|
|
buffer,
|
|
|
|
offset,
|
|
|
|
srcRowBytes);
|
2019-07-26 16:12:26 +00:00
|
|
|
if (!result) {
|
|
|
|
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
|
|
|
}
|
2021-03-31 18:19:31 +00:00
|
|
|
ComparePixels(GrCPixmap(srcInfo, srcData.get(), srcRowBytes),
|
|
|
|
GrCPixmap(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) {
|
2020-07-06 14:56:46 +00:00
|
|
|
auto context = ctxInfo.directContext();
|
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();
|
|
|
|
|
2020-02-07 19:17:25 +00:00
|
|
|
static constexpr SkISize kTexDims = {16, 16};
|
2019-04-08 15:59:07 +00:00
|
|
|
|
|
|
|
// 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
|
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);
|
2020-02-07 19:17:25 +00:00
|
|
|
size_t textureDataRowBytes = kTexDims.fWidth * textureDataBpp;
|
|
|
|
std::unique_ptr<char[]> textureData(new char[kTexDims.fHeight * textureDataRowBytes]);
|
2021-05-18 19:03:27 +00:00
|
|
|
fill_transfer_data(0, 0, kTexDims.fWidth, kTexDims.fHeight, textureDataRowBytes, colorType,
|
2019-07-16 13:47:32 +00:00
|
|
|
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;
|
2020-12-07 17:12:05 +00:00
|
|
|
sk_sp<GrTexture> tex = resourceProvider->createTexture(kTexDims, format, colorType, renderable,
|
|
|
|
1, SkBudgeted::kNo, GrMipMapped::kNo,
|
|
|
|
GrProtected::kNo, &data);
|
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);
|
2021-06-24 15:56:50 +00:00
|
|
|
if (!allowedRead.fOffsetAlignmentForTransferBuffer) {
|
|
|
|
return;
|
|
|
|
}
|
2020-02-07 19:17:25 +00:00
|
|
|
GrImageInfo readInfo(allowedRead.fColorType, kUnpremul_SkAlphaType, nullptr, kTexDims);
|
2019-07-16 13:47:32 +00:00
|
|
|
|
|
|
|
size_t bpp = GrColorTypeBytesPerPixel(allowedRead.fColorType);
|
2021-05-18 19:03:27 +00:00
|
|
|
size_t fullBufferRowBytes = GrAlignTo(kTexDims.fWidth * bpp, caps->transferBufferAlignment());
|
|
|
|
size_t partialBufferRowBytes = GrAlignTo(kPartialWidth * bpp, caps->transferBufferAlignment());
|
2019-07-26 13:14:01 +00:00
|
|
|
size_t offsetAlignment = allowedRead.fOffsetAlignmentForTransferBuffer;
|
2019-07-16 13:47:32 +00:00
|
|
|
SkASSERT(offsetAlignment);
|
|
|
|
|
2020-02-07 19:17:25 +00:00
|
|
|
size_t bufferSize = fullBufferRowBytes * kTexDims.fHeight;
|
2019-07-16 13:47:32 +00:00
|
|
|
// Arbitrary starting offset for the partial read.
|
2021-04-05 20:24:00 +00:00
|
|
|
static constexpr size_t kStartingOffset = 11;
|
|
|
|
size_t partialReadOffset = kStartingOffset +
|
|
|
|
(offsetAlignment - kStartingOffset%offsetAlignment)%offsetAlignment;
|
2021-05-18 19:03:27 +00:00
|
|
|
bufferSize = std::max(bufferSize,
|
|
|
|
partialReadOffset + partialBufferRowBytes * kPartialHeight);
|
2019-07-16 13:47:32 +00:00
|
|
|
|
|
|
|
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
|
2021-05-24 16:40:46 +00:00
|
|
|
bool result = gpu->transferPixelsFrom(tex.get(),
|
|
|
|
SkIRect::MakeSize(kTexDims),
|
|
|
|
colorType,
|
|
|
|
allowedRead.fColorType,
|
|
|
|
buffer,
|
|
|
|
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
|
|
|
if (context->priv().caps()->mapBufferFlags() & GrCaps::kAsyncRead_MapFlag) {
|
2020-04-10 17:43:51 +00:00
|
|
|
gpu->submitToGpu(true);
|
2019-07-11 14:52:43 +00:00
|
|
|
}
|
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;
|
|
|
|
}
|
2020-02-07 19:17:25 +00:00
|
|
|
std::unique_ptr<char[]> transferData(new char[kTexDims.fHeight * fullBufferRowBytes]);
|
|
|
|
memcpy(transferData.get(), map, fullBufferRowBytes * kTexDims.fHeight);
|
2019-07-11 14:52:43 +00:00
|
|
|
buffer->unmap();
|
2019-04-08 15:59:07 +00:00
|
|
|
|
2020-02-07 19:17:25 +00:00
|
|
|
GrImageInfo transferInfo(allowedRead.fColorType, kUnpremul_SkAlphaType, nullptr, kTexDims);
|
2019-07-16 13:47:32 +00:00
|
|
|
|
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,
|
2020-04-29 12:58:39 +00:00
|
|
|
"Error at (%d %d) in transfer, color type: %s, diffs: (%f, %f, %f, %f)",
|
|
|
|
x, y, GrColorTypeToStr(colorType),
|
|
|
|
diffs[0], diffs[1], diffs[2], diffs[3]);
|
2019-07-16 13:47:32 +00:00
|
|
|
});
|
2020-02-07 19:17:25 +00:00
|
|
|
GrImageInfo textureDataInfo(colorType, kUnpremul_SkAlphaType, nullptr, kTexDims);
|
2021-03-31 18:19:31 +00:00
|
|
|
ComparePixels(GrCPixmap(textureDataInfo, textureData.get(), textureDataRowBytes),
|
|
|
|
GrCPixmap( 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.
|
2021-05-24 16:40:46 +00:00
|
|
|
result = gpu->transferPixelsFrom(
|
|
|
|
tex.get(),
|
|
|
|
SkIRect::MakeXYWH(kPartialLeft, kPartialTop, kPartialWidth, kPartialHeight),
|
|
|
|
colorType,
|
|
|
|
allowedRead.fColorType,
|
|
|
|
buffer,
|
|
|
|
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) {
|
2020-04-10 17:43:51 +00:00
|
|
|
gpu->submitToGpu(true);
|
2019-07-11 14:52:43 +00:00
|
|
|
}
|
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;
|
2020-02-07 19:17:25 +00:00
|
|
|
memcpy(transferData.get(), bufferStart, partialBufferRowBytes * kTexDims.fHeight);
|
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);
|
2021-03-31 18:19:31 +00:00
|
|
|
ComparePixels(GrCPixmap(textureDataInfo, textureDataStart, textureDataRowBytes),
|
|
|
|
GrCPixmap(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
|
|
|
|
2020-01-10 18:04:45 +00:00
|
|
|
DEF_GPUTEST_FOR_RENDERING_CONTEXTS(TransferPixelsToTextureTest, reporter, ctxInfo) {
|
2020-07-06 14:56:46 +00:00
|
|
|
if (!ctxInfo.directContext()->priv().caps()->transferFromBufferToTextureSupport()) {
|
2019-04-08 15:59:07 +00:00
|
|
|
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,
|
2021-04-05 20:24:00 +00:00
|
|
|
GrColorType::kRGB_888x,
|
2019-07-26 16:12:26 +00:00
|
|
|
GrColorType::kRG_88,
|
|
|
|
GrColorType::kBGRA_8888,
|
|
|
|
GrColorType::kRGBA_1010102,
|
2020-04-29 12:58:39 +00:00
|
|
|
GrColorType::kBGRA_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,
|
|
|
|
}) {
|
2020-07-06 14:56:46 +00:00
|
|
|
basic_transfer_to_test(reporter, ctxInfo.directContext(), colorType, renderable);
|
2019-07-17 13:59:59 +00:00
|
|
|
}
|
|
|
|
}
|
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
|
2020-01-10 18:04:45 +00:00
|
|
|
DEF_GPUTEST_FOR_RENDERING_CONTEXTS(TransferPixelsFromTextureTest, reporter, ctxInfo) {
|
2020-07-06 14:56:46 +00:00
|
|
|
if (!ctxInfo.directContext()->priv().caps()->transferFromSurfaceToBufferSupport()) {
|
2019-04-08 15:59:07 +00:00
|
|
|
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,
|
2021-04-05 20:24:00 +00:00
|
|
|
GrColorType::kRGB_888x,
|
2019-11-04 21:59:29 +00:00
|
|
|
GrColorType::kRG_88,
|
|
|
|
GrColorType::kBGRA_8888,
|
|
|
|
GrColorType::kRGBA_1010102,
|
2020-04-29 12:58:39 +00:00
|
|
|
GrColorType::kBGRA_1010102,
|
2019-11-04 21:59:29 +00:00
|
|
|
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
|
|
|
}
|