1cb93a386Sopenharmony_ci/* 2cb93a386Sopenharmony_ci * Copyright 2017 Google Inc. 3cb93a386Sopenharmony_ci * 4cb93a386Sopenharmony_ci * Use of this source code is governed by a BSD-style license that can be 5cb93a386Sopenharmony_ci * found in the LICENSE file. 6cb93a386Sopenharmony_ci */ 7cb93a386Sopenharmony_ci 8cb93a386Sopenharmony_ci// This is a GPU-backend specific test. It relies on static intializers to work 9cb93a386Sopenharmony_ci 10cb93a386Sopenharmony_ci#include "include/core/SkTypes.h" 11cb93a386Sopenharmony_ci 12cb93a386Sopenharmony_ci#include "include/core/SkSurface.h" 13cb93a386Sopenharmony_ci#include "include/gpu/GrDirectContext.h" 14cb93a386Sopenharmony_ci#include "src/gpu/GrDirectContextPriv.h" 15cb93a386Sopenharmony_ci#include "src/gpu/GrGpu.h" 16cb93a386Sopenharmony_ci#include "src/gpu/GrImageInfo.h" 17cb93a386Sopenharmony_ci#include "src/gpu/GrResourceProvider.h" 18cb93a386Sopenharmony_ci#include "src/gpu/GrSurfaceProxy.h" 19cb93a386Sopenharmony_ci#include "src/gpu/GrTexture.h" 20cb93a386Sopenharmony_ci#include "src/gpu/SkGr.h" 21cb93a386Sopenharmony_ci#include "tests/Test.h" 22cb93a386Sopenharmony_ci#include "tests/TestUtils.h" 23cb93a386Sopenharmony_ci#include "tools/gpu/GrContextFactory.h" 24cb93a386Sopenharmony_ci 25cb93a386Sopenharmony_ciusing sk_gpu_test::GrContextFactory; 26cb93a386Sopenharmony_ci 27cb93a386Sopenharmony_civoid fill_transfer_data(int left, int top, int width, int height, int rowBytes, 28cb93a386Sopenharmony_ci GrColorType dstType, char* dst) { 29cb93a386Sopenharmony_ci size_t dstBpp = GrColorTypeBytesPerPixel(dstType); 30cb93a386Sopenharmony_ci auto dstLocation = [dst, dstBpp, rowBytes](int x, int y) { 31cb93a386Sopenharmony_ci return dst + y * rowBytes + x * dstBpp; 32cb93a386Sopenharmony_ci }; 33cb93a386Sopenharmony_ci // build red-green gradient 34cb93a386Sopenharmony_ci for (int j = top; j < top + height; ++j) { 35cb93a386Sopenharmony_ci for (int i = left; i < left + width; ++i) { 36cb93a386Sopenharmony_ci auto r = (unsigned int)(256.f*((i - left) / (float)width)); 37cb93a386Sopenharmony_ci auto g = (unsigned int)(256.f*((j - top) / (float)height)); 38cb93a386Sopenharmony_ci r -= (r >> 8); 39cb93a386Sopenharmony_ci g -= (g >> 8); 40cb93a386Sopenharmony_ci // set b and a channels to be inverse of r and g just to have interesting values to 41cb93a386Sopenharmony_ci // test. 42cb93a386Sopenharmony_ci uint32_t srcPixel = GrColorPackRGBA(r, g, 0xff - r, 0xff - g); 43cb93a386Sopenharmony_ci GrImageInfo srcInfo(GrColorType::kRGBA_8888, kUnpremul_SkAlphaType, nullptr, 1, 1); 44cb93a386Sopenharmony_ci GrImageInfo dstInfo(dstType, kUnpremul_SkAlphaType, nullptr, 1, 1); 45cb93a386Sopenharmony_ci GrConvertPixels(GrPixmap(dstInfo, dstLocation(i, j), dstBpp), 46cb93a386Sopenharmony_ci GrPixmap(srcInfo, &srcPixel, 4)); 47cb93a386Sopenharmony_ci } 48cb93a386Sopenharmony_ci } 49cb93a386Sopenharmony_ci} 50cb93a386Sopenharmony_ci 51cb93a386Sopenharmony_civoid determine_tolerances(GrColorType a, GrColorType b, float tolerances[4]) { 52cb93a386Sopenharmony_ci std::fill_n(tolerances, 4, 0); 53cb93a386Sopenharmony_ci 54cb93a386Sopenharmony_ci auto descA = GrGetColorTypeDesc(a); 55cb93a386Sopenharmony_ci auto descB = GrGetColorTypeDesc(b); 56cb93a386Sopenharmony_ci // For each channel x set the tolerance to 1 / (2^min(bits_in_a, bits_in_b) - 1) unless 57cb93a386Sopenharmony_ci // one color type is missing the channel. In that case leave it at 0. If the other color 58cb93a386Sopenharmony_ci // has the channel then it better be exactly 1 for alpha or 0 for rgb. 59cb93a386Sopenharmony_ci for (int i = 0; i < 4; ++i) { 60cb93a386Sopenharmony_ci if (descA[i] != descB[i]) { 61cb93a386Sopenharmony_ci auto m = std::min(descA[i], descB[i]); 62cb93a386Sopenharmony_ci if (m) { 63cb93a386Sopenharmony_ci tolerances[i] = 1.f / (m - 1); 64cb93a386Sopenharmony_ci } 65cb93a386Sopenharmony_ci } 66cb93a386Sopenharmony_ci } 67cb93a386Sopenharmony_ci} 68cb93a386Sopenharmony_ci 69cb93a386Sopenharmony_cibool read_pixels_from_texture(GrTexture* texture, GrColorType colorType, char* dst, 70cb93a386Sopenharmony_ci float tolerances[4]) { 71cb93a386Sopenharmony_ci auto* context = texture->getContext(); 72cb93a386Sopenharmony_ci auto* gpu = context->priv().getGpu(); 73cb93a386Sopenharmony_ci auto* caps = context->priv().caps(); 74cb93a386Sopenharmony_ci 75cb93a386Sopenharmony_ci int w = texture->width(); 76cb93a386Sopenharmony_ci int h = texture->height(); 77cb93a386Sopenharmony_ci size_t rowBytes = GrColorTypeBytesPerPixel(colorType) * w; 78cb93a386Sopenharmony_ci 79cb93a386Sopenharmony_ci GrCaps::SupportedRead supportedRead = 80cb93a386Sopenharmony_ci caps->supportedReadPixelsColorType(colorType, texture->backendFormat(), colorType); 81cb93a386Sopenharmony_ci std::fill_n(tolerances, 4, 0); 82cb93a386Sopenharmony_ci if (supportedRead.fColorType != colorType) { 83cb93a386Sopenharmony_ci size_t tmpRowBytes = GrColorTypeBytesPerPixel(supportedRead.fColorType) * w; 84cb93a386Sopenharmony_ci std::unique_ptr<char[]> tmpPixels(new char[tmpRowBytes * h]); 85cb93a386Sopenharmony_ci if (!gpu->readPixels(texture, 86cb93a386Sopenharmony_ci SkIRect::MakeWH(w, h), 87cb93a386Sopenharmony_ci colorType, 88cb93a386Sopenharmony_ci supportedRead.fColorType, 89cb93a386Sopenharmony_ci tmpPixels.get(), 90cb93a386Sopenharmony_ci tmpRowBytes)) { 91cb93a386Sopenharmony_ci return false; 92cb93a386Sopenharmony_ci } 93cb93a386Sopenharmony_ci GrImageInfo tmpInfo(supportedRead.fColorType, kUnpremul_SkAlphaType, nullptr, w, h); 94cb93a386Sopenharmony_ci GrImageInfo dstInfo(colorType, kUnpremul_SkAlphaType, nullptr, w, h); 95cb93a386Sopenharmony_ci determine_tolerances(tmpInfo.colorType(), dstInfo.colorType(), tolerances); 96cb93a386Sopenharmony_ci return GrConvertPixels(GrPixmap(dstInfo, dst, rowBytes), 97cb93a386Sopenharmony_ci GrPixmap(tmpInfo, tmpPixels.get(), tmpRowBytes)); 98cb93a386Sopenharmony_ci } 99cb93a386Sopenharmony_ci return gpu->readPixels(texture, 100cb93a386Sopenharmony_ci SkIRect::MakeWH(w, h), 101cb93a386Sopenharmony_ci colorType, 102cb93a386Sopenharmony_ci supportedRead.fColorType, 103cb93a386Sopenharmony_ci dst, 104cb93a386Sopenharmony_ci rowBytes); 105cb93a386Sopenharmony_ci} 106cb93a386Sopenharmony_ci 107cb93a386Sopenharmony_civoid basic_transfer_to_test(skiatest::Reporter* reporter, 108cb93a386Sopenharmony_ci GrDirectContext* dContext, 109cb93a386Sopenharmony_ci GrColorType colorType, 110cb93a386Sopenharmony_ci GrRenderable renderable) { 111cb93a386Sopenharmony_ci if (GrCaps::kNone_MapFlags == dContext->priv().caps()->mapBufferFlags()) { 112cb93a386Sopenharmony_ci return; 113cb93a386Sopenharmony_ci } 114cb93a386Sopenharmony_ci 115cb93a386Sopenharmony_ci auto* caps = dContext->priv().caps(); 116cb93a386Sopenharmony_ci 117cb93a386Sopenharmony_ci auto backendFormat = caps->getDefaultBackendFormat(colorType, renderable); 118cb93a386Sopenharmony_ci if (!backendFormat.isValid()) { 119cb93a386Sopenharmony_ci return; 120cb93a386Sopenharmony_ci } 121cb93a386Sopenharmony_ci 122cb93a386Sopenharmony_ci auto resourceProvider = dContext->priv().resourceProvider(); 123cb93a386Sopenharmony_ci GrGpu* gpu = dContext->priv().getGpu(); 124cb93a386Sopenharmony_ci 125cb93a386Sopenharmony_ci static constexpr SkISize kTexDims = {16, 16}; 126cb93a386Sopenharmony_ci int srcBufferWidth = caps->transferPixelsToRowBytesSupport() ? 20 : 16; 127cb93a386Sopenharmony_ci const int kBufferHeight = 16; 128cb93a386Sopenharmony_ci 129cb93a386Sopenharmony_ci sk_sp<GrTexture> tex = 130cb93a386Sopenharmony_ci resourceProvider->createTexture(kTexDims, backendFormat, GrTextureType::k2D, renderable, 131cb93a386Sopenharmony_ci 1, GrMipmapped::kNo, SkBudgeted::kNo, GrProtected::kNo); 132cb93a386Sopenharmony_ci if (!tex) { 133cb93a386Sopenharmony_ci ERRORF(reporter, "Could not create texture"); 134cb93a386Sopenharmony_ci return; 135cb93a386Sopenharmony_ci } 136cb93a386Sopenharmony_ci 137cb93a386Sopenharmony_ci // We validate the results using GrGpu::readPixels, so exit if this is not supported. 138cb93a386Sopenharmony_ci // TODO: Do this through SurfaceContext once it works for all color types or support 139cb93a386Sopenharmony_ci // kCopyToTexture2D here. 140cb93a386Sopenharmony_ci if (GrCaps::SurfaceReadPixelsSupport::kSupported != 141cb93a386Sopenharmony_ci caps->surfaceSupportsReadPixels(tex.get())) { 142cb93a386Sopenharmony_ci return; 143cb93a386Sopenharmony_ci } 144cb93a386Sopenharmony_ci // GL requires a texture to be framebuffer bindable to call glReadPixels. However, we have not 145cb93a386Sopenharmony_ci // incorporated that test into surfaceSupportsReadPixels(). TODO: Remove this once we handle 146cb93a386Sopenharmony_ci // drawing to a bindable format. 147cb93a386Sopenharmony_ci if (!caps->isFormatAsColorTypeRenderable(colorType, tex->backendFormat())) { 148cb93a386Sopenharmony_ci return; 149cb93a386Sopenharmony_ci } 150cb93a386Sopenharmony_ci 151cb93a386Sopenharmony_ci // The caps tell us what color type we are allowed to upload and read back from this texture, 152cb93a386Sopenharmony_ci // either of which may differ from 'colorType'. 153cb93a386Sopenharmony_ci GrCaps::SupportedWrite allowedSrc = 154cb93a386Sopenharmony_ci caps->supportedWritePixelsColorType(colorType, tex->backendFormat(), colorType); 155cb93a386Sopenharmony_ci if (!allowedSrc.fOffsetAlignmentForTransferBuffer) { 156cb93a386Sopenharmony_ci return; 157cb93a386Sopenharmony_ci } 158cb93a386Sopenharmony_ci size_t srcRowBytes = SkAlignTo(GrColorTypeBytesPerPixel(allowedSrc.fColorType) * srcBufferWidth, 159cb93a386Sopenharmony_ci caps->transferBufferAlignment()); 160cb93a386Sopenharmony_ci 161cb93a386Sopenharmony_ci std::unique_ptr<char[]> srcData(new char[kTexDims.fHeight * srcRowBytes]); 162cb93a386Sopenharmony_ci 163cb93a386Sopenharmony_ci fill_transfer_data(0, 0, kTexDims.fWidth, kTexDims.fHeight, srcRowBytes, 164cb93a386Sopenharmony_ci allowedSrc.fColorType, srcData.get()); 165cb93a386Sopenharmony_ci 166cb93a386Sopenharmony_ci // create and fill transfer buffer 167cb93a386Sopenharmony_ci size_t size = srcRowBytes * kBufferHeight; 168cb93a386Sopenharmony_ci sk_sp<GrGpuBuffer> buffer(resourceProvider->createBuffer(size, GrGpuBufferType::kXferCpuToGpu, 169cb93a386Sopenharmony_ci kDynamic_GrAccessPattern)); 170cb93a386Sopenharmony_ci if (!buffer) { 171cb93a386Sopenharmony_ci return; 172cb93a386Sopenharmony_ci } 173cb93a386Sopenharmony_ci void* data = buffer->map(); 174cb93a386Sopenharmony_ci if (!buffer) { 175cb93a386Sopenharmony_ci ERRORF(reporter, "Could not map buffer"); 176cb93a386Sopenharmony_ci return; 177cb93a386Sopenharmony_ci } 178cb93a386Sopenharmony_ci memcpy(data, srcData.get(), size); 179cb93a386Sopenharmony_ci buffer->unmap(); 180cb93a386Sopenharmony_ci 181cb93a386Sopenharmony_ci ////////////////////////// 182cb93a386Sopenharmony_ci // transfer full data 183cb93a386Sopenharmony_ci 184cb93a386Sopenharmony_ci bool result; 185cb93a386Sopenharmony_ci result = gpu->transferPixelsTo(tex.get(), 186cb93a386Sopenharmony_ci SkIRect::MakeSize(kTexDims), 187cb93a386Sopenharmony_ci colorType, 188cb93a386Sopenharmony_ci allowedSrc.fColorType, 189cb93a386Sopenharmony_ci buffer, 190cb93a386Sopenharmony_ci 0, 191cb93a386Sopenharmony_ci srcRowBytes); 192cb93a386Sopenharmony_ci REPORTER_ASSERT(reporter, result); 193cb93a386Sopenharmony_ci 194cb93a386Sopenharmony_ci size_t dstRowBytes = GrColorTypeBytesPerPixel(colorType) * kTexDims.fWidth; 195cb93a386Sopenharmony_ci std::unique_ptr<char[]> dstBuffer(new char[dstRowBytes * kTexDims.fHeight]()); 196cb93a386Sopenharmony_ci 197cb93a386Sopenharmony_ci float compareTolerances[4] = {}; 198cb93a386Sopenharmony_ci result = read_pixels_from_texture(tex.get(), colorType, dstBuffer.get(), compareTolerances); 199cb93a386Sopenharmony_ci if (!result) { 200cb93a386Sopenharmony_ci ERRORF(reporter, "Could not read pixels from texture, color type: %d", 201cb93a386Sopenharmony_ci static_cast<int>(colorType)); 202cb93a386Sopenharmony_ci return; 203cb93a386Sopenharmony_ci } 204cb93a386Sopenharmony_ci 205cb93a386Sopenharmony_ci auto error = std::function<ComparePixmapsErrorReporter>( 206cb93a386Sopenharmony_ci [reporter, colorType](int x, int y, const float diffs[4]) { 207cb93a386Sopenharmony_ci ERRORF(reporter, 208cb93a386Sopenharmony_ci "Error at (%d %d) in transfer, color type: %s, diffs: (%f, %f, %f, %f)", 209cb93a386Sopenharmony_ci x, y, GrColorTypeToStr(colorType), 210cb93a386Sopenharmony_ci diffs[0], diffs[1], diffs[2], diffs[3]); 211cb93a386Sopenharmony_ci }); 212cb93a386Sopenharmony_ci GrImageInfo srcInfo(allowedSrc.fColorType, kUnpremul_SkAlphaType, nullptr, tex->dimensions()); 213cb93a386Sopenharmony_ci GrImageInfo dstInfo( colorType, kUnpremul_SkAlphaType, nullptr, tex->dimensions()); 214cb93a386Sopenharmony_ci ComparePixels(GrCPixmap(srcInfo, srcData.get(), srcRowBytes), 215cb93a386Sopenharmony_ci GrCPixmap(dstInfo, dstBuffer.get(), dstRowBytes), 216cb93a386Sopenharmony_ci compareTolerances, 217cb93a386Sopenharmony_ci error); 218cb93a386Sopenharmony_ci 219cb93a386Sopenharmony_ci ////////////////////////// 220cb93a386Sopenharmony_ci // transfer partial data 221cb93a386Sopenharmony_ci 222cb93a386Sopenharmony_ci // We're relying on this cap to write partial texture data 223cb93a386Sopenharmony_ci if (!caps->transferPixelsToRowBytesSupport()) { 224cb93a386Sopenharmony_ci return; 225cb93a386Sopenharmony_ci } 226cb93a386Sopenharmony_ci // We keep a 1 to 1 correspondence between pixels in the buffer and the entire texture. We 227cb93a386Sopenharmony_ci // update the contents of a sub-rect of the buffer and push that rect to the texture. We start 228cb93a386Sopenharmony_ci // with a left sub-rect inset of 2 but may adjust that so we can fulfill the transfer buffer 229cb93a386Sopenharmony_ci // offset alignment requirement. 230cb93a386Sopenharmony_ci int left = 2; 231cb93a386Sopenharmony_ci int top = 10; 232cb93a386Sopenharmony_ci const int width = 10; 233cb93a386Sopenharmony_ci const int height = 2; 234cb93a386Sopenharmony_ci size_t offset = top * srcRowBytes + left * GrColorTypeBytesPerPixel(allowedSrc.fColorType); 235cb93a386Sopenharmony_ci while (offset % allowedSrc.fOffsetAlignmentForTransferBuffer) { 236cb93a386Sopenharmony_ci offset += GrColorTypeBytesPerPixel(allowedSrc.fColorType); 237cb93a386Sopenharmony_ci ++left; 238cb93a386Sopenharmony_ci // In most cases we assume that the required alignment is 1 or a small multiple of the bpp, 239cb93a386Sopenharmony_ci // which it is for color types across all current backends except Direct3D. To correct for 240cb93a386Sopenharmony_ci // Direct3D's large alignment requirement we may adjust the top location as well. 241cb93a386Sopenharmony_ci if (left + width > tex->width()) { 242cb93a386Sopenharmony_ci left = 0; 243cb93a386Sopenharmony_ci ++top; 244cb93a386Sopenharmony_ci offset = top * srcRowBytes; 245cb93a386Sopenharmony_ci } 246cb93a386Sopenharmony_ci SkASSERT(left + width <= tex->width()); 247cb93a386Sopenharmony_ci SkASSERT(top + height <= tex->height()); 248cb93a386Sopenharmony_ci } 249cb93a386Sopenharmony_ci 250cb93a386Sopenharmony_ci // change color of subrectangle 251cb93a386Sopenharmony_ci fill_transfer_data(left, top, width, height, srcRowBytes, allowedSrc.fColorType, 252cb93a386Sopenharmony_ci srcData.get()); 253cb93a386Sopenharmony_ci data = buffer->map(); 254cb93a386Sopenharmony_ci memcpy(data, srcData.get(), size); 255cb93a386Sopenharmony_ci buffer->unmap(); 256cb93a386Sopenharmony_ci 257cb93a386Sopenharmony_ci result = gpu->transferPixelsTo(tex.get(), 258cb93a386Sopenharmony_ci SkIRect::MakeXYWH(left, top, width, height), 259cb93a386Sopenharmony_ci colorType, 260cb93a386Sopenharmony_ci allowedSrc.fColorType, 261cb93a386Sopenharmony_ci buffer, 262cb93a386Sopenharmony_ci offset, 263cb93a386Sopenharmony_ci srcRowBytes); 264cb93a386Sopenharmony_ci if (!result) { 265cb93a386Sopenharmony_ci ERRORF(reporter, "Could not transfer pixels to texture, color type: %d", 266cb93a386Sopenharmony_ci static_cast<int>(colorType)); 267cb93a386Sopenharmony_ci return; 268cb93a386Sopenharmony_ci } 269cb93a386Sopenharmony_ci 270cb93a386Sopenharmony_ci result = read_pixels_from_texture(tex.get(), colorType, dstBuffer.get(), compareTolerances); 271cb93a386Sopenharmony_ci if (!result) { 272cb93a386Sopenharmony_ci ERRORF(reporter, "Could not read pixels from texture, color type: %d", 273cb93a386Sopenharmony_ci static_cast<int>(colorType)); 274cb93a386Sopenharmony_ci return; 275cb93a386Sopenharmony_ci } 276cb93a386Sopenharmony_ci ComparePixels(GrCPixmap(srcInfo, srcData.get(), srcRowBytes), 277cb93a386Sopenharmony_ci GrCPixmap(dstInfo, dstBuffer.get(), dstRowBytes), 278cb93a386Sopenharmony_ci compareTolerances, 279cb93a386Sopenharmony_ci error); 280cb93a386Sopenharmony_ci} 281cb93a386Sopenharmony_ci 282cb93a386Sopenharmony_civoid basic_transfer_from_test(skiatest::Reporter* reporter, const sk_gpu_test::ContextInfo& ctxInfo, 283cb93a386Sopenharmony_ci GrColorType colorType, GrRenderable renderable) { 284cb93a386Sopenharmony_ci auto context = ctxInfo.directContext(); 285cb93a386Sopenharmony_ci auto caps = context->priv().caps(); 286cb93a386Sopenharmony_ci if (GrCaps::kNone_MapFlags == caps->mapBufferFlags()) { 287cb93a386Sopenharmony_ci return; 288cb93a386Sopenharmony_ci } 289cb93a386Sopenharmony_ci 290cb93a386Sopenharmony_ci auto resourceProvider = context->priv().resourceProvider(); 291cb93a386Sopenharmony_ci GrGpu* gpu = context->priv().getGpu(); 292cb93a386Sopenharmony_ci 293cb93a386Sopenharmony_ci static constexpr SkISize kTexDims = {16, 16}; 294cb93a386Sopenharmony_ci 295cb93a386Sopenharmony_ci // We'll do a full texture read into the buffer followed by a partial read. These values 296cb93a386Sopenharmony_ci // describe the partial read subrect. 297cb93a386Sopenharmony_ci const int kPartialLeft = 2; 298cb93a386Sopenharmony_ci const int kPartialTop = 10; 299cb93a386Sopenharmony_ci const int kPartialWidth = 10; 300cb93a386Sopenharmony_ci const int kPartialHeight = 2; 301cb93a386Sopenharmony_ci 302cb93a386Sopenharmony_ci // create texture 303cb93a386Sopenharmony_ci auto format = context->priv().caps()->getDefaultBackendFormat(colorType, renderable); 304cb93a386Sopenharmony_ci if (!format.isValid()) { 305cb93a386Sopenharmony_ci return; 306cb93a386Sopenharmony_ci } 307cb93a386Sopenharmony_ci 308cb93a386Sopenharmony_ci size_t textureDataBpp = GrColorTypeBytesPerPixel(colorType); 309cb93a386Sopenharmony_ci size_t textureDataRowBytes = kTexDims.fWidth * textureDataBpp; 310cb93a386Sopenharmony_ci std::unique_ptr<char[]> textureData(new char[kTexDims.fHeight * textureDataRowBytes]); 311cb93a386Sopenharmony_ci fill_transfer_data(0, 0, kTexDims.fWidth, kTexDims.fHeight, textureDataRowBytes, colorType, 312cb93a386Sopenharmony_ci textureData.get()); 313cb93a386Sopenharmony_ci GrMipLevel data; 314cb93a386Sopenharmony_ci data.fPixels = textureData.get(); 315cb93a386Sopenharmony_ci data.fRowBytes = textureDataRowBytes; 316cb93a386Sopenharmony_ci sk_sp<GrTexture> tex = resourceProvider->createTexture(kTexDims, format, GrTextureType::k2D, 317cb93a386Sopenharmony_ci colorType, renderable, 1, 318cb93a386Sopenharmony_ci SkBudgeted::kNo, GrMipMapped::kNo, 319cb93a386Sopenharmony_ci GrProtected::kNo, &data); 320cb93a386Sopenharmony_ci if (!tex) { 321cb93a386Sopenharmony_ci return; 322cb93a386Sopenharmony_ci } 323cb93a386Sopenharmony_ci 324cb93a386Sopenharmony_ci if (GrCaps::SurfaceReadPixelsSupport::kSupported != 325cb93a386Sopenharmony_ci caps->surfaceSupportsReadPixels(tex.get())) { 326cb93a386Sopenharmony_ci return; 327cb93a386Sopenharmony_ci } 328cb93a386Sopenharmony_ci // GL requires a texture to be framebuffer bindable to call glReadPixels. However, we have not 329cb93a386Sopenharmony_ci // incorporated that test into surfaceSupportsReadPixels(). TODO: Remove this once we handle 330cb93a386Sopenharmony_ci // drawing to a bindable format. 331cb93a386Sopenharmony_ci if (!caps->isFormatAsColorTypeRenderable(colorType, tex->backendFormat())) { 332cb93a386Sopenharmony_ci return; 333cb93a386Sopenharmony_ci } 334cb93a386Sopenharmony_ci 335cb93a386Sopenharmony_ci // Create the transfer buffer. 336cb93a386Sopenharmony_ci auto allowedRead = 337cb93a386Sopenharmony_ci caps->supportedReadPixelsColorType(colorType, tex->backendFormat(), colorType); 338cb93a386Sopenharmony_ci if (!allowedRead.fOffsetAlignmentForTransferBuffer) { 339cb93a386Sopenharmony_ci return; 340cb93a386Sopenharmony_ci } 341cb93a386Sopenharmony_ci GrImageInfo readInfo(allowedRead.fColorType, kUnpremul_SkAlphaType, nullptr, kTexDims); 342cb93a386Sopenharmony_ci 343cb93a386Sopenharmony_ci size_t bpp = GrColorTypeBytesPerPixel(allowedRead.fColorType); 344cb93a386Sopenharmony_ci size_t fullBufferRowBytes = SkAlignTo(kTexDims.fWidth * bpp, caps->transferBufferAlignment()); 345cb93a386Sopenharmony_ci size_t partialBufferRowBytes = SkAlignTo(kPartialWidth * bpp, caps->transferBufferAlignment()); 346cb93a386Sopenharmony_ci size_t offsetAlignment = allowedRead.fOffsetAlignmentForTransferBuffer; 347cb93a386Sopenharmony_ci SkASSERT(offsetAlignment); 348cb93a386Sopenharmony_ci 349cb93a386Sopenharmony_ci size_t bufferSize = fullBufferRowBytes * kTexDims.fHeight; 350cb93a386Sopenharmony_ci // Arbitrary starting offset for the partial read. 351cb93a386Sopenharmony_ci static constexpr size_t kStartingOffset = 11; 352cb93a386Sopenharmony_ci size_t partialReadOffset = kStartingOffset + 353cb93a386Sopenharmony_ci (offsetAlignment - kStartingOffset%offsetAlignment)%offsetAlignment; 354cb93a386Sopenharmony_ci bufferSize = std::max(bufferSize, 355cb93a386Sopenharmony_ci partialReadOffset + partialBufferRowBytes * kPartialHeight); 356cb93a386Sopenharmony_ci 357cb93a386Sopenharmony_ci sk_sp<GrGpuBuffer> buffer(resourceProvider->createBuffer( 358cb93a386Sopenharmony_ci bufferSize, GrGpuBufferType::kXferGpuToCpu, kDynamic_GrAccessPattern)); 359cb93a386Sopenharmony_ci REPORTER_ASSERT(reporter, buffer); 360cb93a386Sopenharmony_ci if (!buffer) { 361cb93a386Sopenharmony_ci return; 362cb93a386Sopenharmony_ci } 363cb93a386Sopenharmony_ci 364cb93a386Sopenharmony_ci int expectedTransferCnt = 0; 365cb93a386Sopenharmony_ci gpu->stats()->reset(); 366cb93a386Sopenharmony_ci 367cb93a386Sopenharmony_ci ////////////////////////// 368cb93a386Sopenharmony_ci // transfer full data 369cb93a386Sopenharmony_ci bool result = gpu->transferPixelsFrom(tex.get(), 370cb93a386Sopenharmony_ci SkIRect::MakeSize(kTexDims), 371cb93a386Sopenharmony_ci colorType, 372cb93a386Sopenharmony_ci allowedRead.fColorType, 373cb93a386Sopenharmony_ci buffer, 374cb93a386Sopenharmony_ci 0); 375cb93a386Sopenharmony_ci if (!result) { 376cb93a386Sopenharmony_ci ERRORF(reporter, "transferPixelsFrom failed."); 377cb93a386Sopenharmony_ci return; 378cb93a386Sopenharmony_ci } 379cb93a386Sopenharmony_ci ++expectedTransferCnt; 380cb93a386Sopenharmony_ci 381cb93a386Sopenharmony_ci if (context->priv().caps()->mapBufferFlags() & GrCaps::kAsyncRead_MapFlag) { 382cb93a386Sopenharmony_ci gpu->submitToGpu(true); 383cb93a386Sopenharmony_ci } 384cb93a386Sopenharmony_ci 385cb93a386Sopenharmony_ci // Copy the transfer buffer contents to a temporary so we can manipulate it. 386cb93a386Sopenharmony_ci const auto* map = reinterpret_cast<const char*>(buffer->map()); 387cb93a386Sopenharmony_ci REPORTER_ASSERT(reporter, map); 388cb93a386Sopenharmony_ci if (!map) { 389cb93a386Sopenharmony_ci ERRORF(reporter, "Failed to map transfer buffer."); 390cb93a386Sopenharmony_ci return; 391cb93a386Sopenharmony_ci } 392cb93a386Sopenharmony_ci std::unique_ptr<char[]> transferData(new char[kTexDims.fHeight * fullBufferRowBytes]); 393cb93a386Sopenharmony_ci memcpy(transferData.get(), map, fullBufferRowBytes * kTexDims.fHeight); 394cb93a386Sopenharmony_ci buffer->unmap(); 395cb93a386Sopenharmony_ci 396cb93a386Sopenharmony_ci GrImageInfo transferInfo(allowedRead.fColorType, kUnpremul_SkAlphaType, nullptr, kTexDims); 397cb93a386Sopenharmony_ci 398cb93a386Sopenharmony_ci float tol[4]; 399cb93a386Sopenharmony_ci determine_tolerances(allowedRead.fColorType, colorType, tol); 400cb93a386Sopenharmony_ci auto error = std::function<ComparePixmapsErrorReporter>( 401cb93a386Sopenharmony_ci [reporter, colorType](int x, int y, const float diffs[4]) { 402cb93a386Sopenharmony_ci ERRORF(reporter, 403cb93a386Sopenharmony_ci "Error at (%d %d) in transfer, color type: %s, diffs: (%f, %f, %f, %f)", 404cb93a386Sopenharmony_ci x, y, GrColorTypeToStr(colorType), 405cb93a386Sopenharmony_ci diffs[0], diffs[1], diffs[2], diffs[3]); 406cb93a386Sopenharmony_ci }); 407cb93a386Sopenharmony_ci GrImageInfo textureDataInfo(colorType, kUnpremul_SkAlphaType, nullptr, kTexDims); 408cb93a386Sopenharmony_ci ComparePixels(GrCPixmap(textureDataInfo, textureData.get(), textureDataRowBytes), 409cb93a386Sopenharmony_ci GrCPixmap( transferInfo, transferData.get(), fullBufferRowBytes), 410cb93a386Sopenharmony_ci tol, 411cb93a386Sopenharmony_ci error); 412cb93a386Sopenharmony_ci 413cb93a386Sopenharmony_ci /////////////////////// 414cb93a386Sopenharmony_ci // Now test a partial read at an offset into the buffer. 415cb93a386Sopenharmony_ci result = gpu->transferPixelsFrom( 416cb93a386Sopenharmony_ci tex.get(), 417cb93a386Sopenharmony_ci SkIRect::MakeXYWH(kPartialLeft, kPartialTop, kPartialWidth, kPartialHeight), 418cb93a386Sopenharmony_ci colorType, 419cb93a386Sopenharmony_ci allowedRead.fColorType, 420cb93a386Sopenharmony_ci buffer, 421cb93a386Sopenharmony_ci partialReadOffset); 422cb93a386Sopenharmony_ci if (!result) { 423cb93a386Sopenharmony_ci ERRORF(reporter, "transferPixelsFrom failed."); 424cb93a386Sopenharmony_ci return; 425cb93a386Sopenharmony_ci } 426cb93a386Sopenharmony_ci ++expectedTransferCnt; 427cb93a386Sopenharmony_ci 428cb93a386Sopenharmony_ci if (context->priv().caps()->mapBufferFlags() & GrCaps::kAsyncRead_MapFlag) { 429cb93a386Sopenharmony_ci gpu->submitToGpu(true); 430cb93a386Sopenharmony_ci } 431cb93a386Sopenharmony_ci 432cb93a386Sopenharmony_ci map = reinterpret_cast<const char*>(buffer->map()); 433cb93a386Sopenharmony_ci REPORTER_ASSERT(reporter, map); 434cb93a386Sopenharmony_ci if (!map) { 435cb93a386Sopenharmony_ci ERRORF(reporter, "Failed to map transfer buffer."); 436cb93a386Sopenharmony_ci return; 437cb93a386Sopenharmony_ci } 438cb93a386Sopenharmony_ci const char* bufferStart = reinterpret_cast<const char*>(map) + partialReadOffset; 439cb93a386Sopenharmony_ci memcpy(transferData.get(), bufferStart, partialBufferRowBytes * kTexDims.fHeight); 440cb93a386Sopenharmony_ci buffer->unmap(); 441cb93a386Sopenharmony_ci 442cb93a386Sopenharmony_ci transferInfo = transferInfo.makeWH(kPartialWidth, kPartialHeight); 443cb93a386Sopenharmony_ci const char* textureDataStart = 444cb93a386Sopenharmony_ci textureData.get() + textureDataRowBytes * kPartialTop + textureDataBpp * kPartialLeft; 445cb93a386Sopenharmony_ci textureDataInfo = textureDataInfo.makeWH(kPartialWidth, kPartialHeight); 446cb93a386Sopenharmony_ci ComparePixels(GrCPixmap(textureDataInfo, textureDataStart, textureDataRowBytes), 447cb93a386Sopenharmony_ci GrCPixmap(transferInfo , transferData.get(), partialBufferRowBytes), 448cb93a386Sopenharmony_ci tol, 449cb93a386Sopenharmony_ci error); 450cb93a386Sopenharmony_ci#if GR_GPU_STATS 451cb93a386Sopenharmony_ci REPORTER_ASSERT(reporter, gpu->stats()->transfersFromSurface() == expectedTransferCnt); 452cb93a386Sopenharmony_ci#else 453cb93a386Sopenharmony_ci (void)expectedTransferCnt; 454cb93a386Sopenharmony_ci#endif 455cb93a386Sopenharmony_ci} 456cb93a386Sopenharmony_ci 457cb93a386Sopenharmony_ciDEF_GPUTEST_FOR_RENDERING_CONTEXTS(TransferPixelsToTextureTest, reporter, ctxInfo) { 458cb93a386Sopenharmony_ci if (!ctxInfo.directContext()->priv().caps()->transferFromBufferToTextureSupport()) { 459cb93a386Sopenharmony_ci return; 460cb93a386Sopenharmony_ci } 461cb93a386Sopenharmony_ci for (auto renderable : {GrRenderable::kNo, GrRenderable::kYes}) { 462cb93a386Sopenharmony_ci for (auto colorType : { 463cb93a386Sopenharmony_ci GrColorType::kAlpha_8, 464cb93a386Sopenharmony_ci GrColorType::kBGR_565, 465cb93a386Sopenharmony_ci GrColorType::kABGR_4444, 466cb93a386Sopenharmony_ci GrColorType::kRGBA_8888, 467cb93a386Sopenharmony_ci GrColorType::kRGBA_8888_SRGB, 468cb93a386Sopenharmony_ci GrColorType::kRGB_888x, 469cb93a386Sopenharmony_ci GrColorType::kRG_88, 470cb93a386Sopenharmony_ci GrColorType::kBGRA_8888, 471cb93a386Sopenharmony_ci GrColorType::kRGBA_1010102, 472cb93a386Sopenharmony_ci GrColorType::kBGRA_1010102, 473cb93a386Sopenharmony_ci GrColorType::kGray_8, 474cb93a386Sopenharmony_ci GrColorType::kAlpha_F16, 475cb93a386Sopenharmony_ci GrColorType::kRGBA_F16, 476cb93a386Sopenharmony_ci GrColorType::kRGBA_F16_Clamped, 477cb93a386Sopenharmony_ci GrColorType::kRGBA_F32, 478cb93a386Sopenharmony_ci GrColorType::kAlpha_16, 479cb93a386Sopenharmony_ci GrColorType::kRG_1616, 480cb93a386Sopenharmony_ci GrColorType::kRGBA_16161616, 481cb93a386Sopenharmony_ci GrColorType::kRG_F16, 482cb93a386Sopenharmony_ci }) { 483cb93a386Sopenharmony_ci basic_transfer_to_test(reporter, ctxInfo.directContext(), colorType, renderable); 484cb93a386Sopenharmony_ci } 485cb93a386Sopenharmony_ci } 486cb93a386Sopenharmony_ci} 487cb93a386Sopenharmony_ci 488cb93a386Sopenharmony_ci// TODO(bsalomon): Metal 489cb93a386Sopenharmony_ciDEF_GPUTEST_FOR_RENDERING_CONTEXTS(TransferPixelsFromTextureTest, reporter, ctxInfo) { 490cb93a386Sopenharmony_ci if (!ctxInfo.directContext()->priv().caps()->transferFromSurfaceToBufferSupport()) { 491cb93a386Sopenharmony_ci return; 492cb93a386Sopenharmony_ci } 493cb93a386Sopenharmony_ci for (auto renderable : {GrRenderable::kNo, GrRenderable::kYes}) { 494cb93a386Sopenharmony_ci for (auto colorType : { 495cb93a386Sopenharmony_ci GrColorType::kAlpha_8, 496cb93a386Sopenharmony_ci GrColorType::kAlpha_16, 497cb93a386Sopenharmony_ci GrColorType::kBGR_565, 498cb93a386Sopenharmony_ci GrColorType::kABGR_4444, 499cb93a386Sopenharmony_ci GrColorType::kRGBA_8888, 500cb93a386Sopenharmony_ci GrColorType::kRGBA_8888_SRGB, 501cb93a386Sopenharmony_ci GrColorType::kRGB_888x, 502cb93a386Sopenharmony_ci GrColorType::kRG_88, 503cb93a386Sopenharmony_ci GrColorType::kBGRA_8888, 504cb93a386Sopenharmony_ci GrColorType::kRGBA_1010102, 505cb93a386Sopenharmony_ci GrColorType::kBGRA_1010102, 506cb93a386Sopenharmony_ci GrColorType::kGray_8, 507cb93a386Sopenharmony_ci GrColorType::kAlpha_F16, 508cb93a386Sopenharmony_ci GrColorType::kRGBA_F16, 509cb93a386Sopenharmony_ci GrColorType::kRGBA_F16_Clamped, 510cb93a386Sopenharmony_ci GrColorType::kRGBA_F32, 511cb93a386Sopenharmony_ci GrColorType::kRG_1616, 512cb93a386Sopenharmony_ci GrColorType::kRGBA_16161616, 513cb93a386Sopenharmony_ci GrColorType::kRG_F16, 514cb93a386Sopenharmony_ci }) { 515cb93a386Sopenharmony_ci basic_transfer_from_test(reporter, ctxInfo, colorType, renderable); 516cb93a386Sopenharmony_ci } 517cb93a386Sopenharmony_ci } 518cb93a386Sopenharmony_ci} 519