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
|
|
|
|
|
|
|
|
#include "SkTypes.h"
|
|
|
|
|
|
|
|
#include "GrContextFactory.h"
|
|
|
|
#include "GrContextPriv.h"
|
|
|
|
#include "GrGpu.h"
|
|
|
|
#include "GrResourceProvider.h"
|
|
|
|
#include "GrSurfaceProxy.h"
|
|
|
|
#include "GrTexture.h"
|
|
|
|
#include "SkGr.h"
|
|
|
|
#include "SkSurface.h"
|
|
|
|
#include "Test.h"
|
|
|
|
|
|
|
|
using sk_gpu_test::GrContextFactory;
|
|
|
|
|
|
|
|
void fill_transfer_data(int left, int top, int width, int height, int bufferWidth,
|
|
|
|
GrColor* data) {
|
|
|
|
|
|
|
|
// build red-green gradient
|
|
|
|
for (int j = top; j < top + height; ++j) {
|
|
|
|
for (int i = left; i < left + width; ++i) {
|
|
|
|
unsigned int red = (unsigned int)(256.f*((i - left) / (float)width));
|
|
|
|
unsigned int green = (unsigned int)(256.f*((j - top) / (float)height));
|
|
|
|
data[i + j*bufferWidth] = GrColorPackRGBA(red - (red>>8),
|
|
|
|
green - (green>>8), 0xff, 0xff);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool does_full_buffer_contain_correct_values(GrColor* srcBuffer,
|
|
|
|
GrColor* dstBuffer,
|
|
|
|
int width,
|
|
|
|
int height,
|
|
|
|
int bufferWidth,
|
2018-06-01 19:33:20 +00:00
|
|
|
int bufferHeight) {
|
2017-06-21 19:55:46 +00:00
|
|
|
GrColor* srcPtr = srcBuffer;
|
2018-06-01 19:33:20 +00:00
|
|
|
GrColor* dstPtr = dstBuffer;
|
2017-06-21 19:55:46 +00:00
|
|
|
|
|
|
|
for (int j = 0; j < height; ++j) {
|
|
|
|
for (int i = 0; i < width; ++i) {
|
|
|
|
if (srcPtr[i] != dstPtr[i]) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
srcPtr += bufferWidth;
|
2018-06-01 19:33:20 +00:00
|
|
|
dstPtr += bufferWidth;
|
2017-06-21 19:55:46 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-02-20 19:05:36 +00:00
|
|
|
void basic_transfer_test(skiatest::Reporter* reporter, GrContext* context, GrColorType colorType,
|
2018-06-01 19:33:20 +00:00
|
|
|
bool renderTarget) {
|
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-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
|
|
|
// set up the data
|
|
|
|
const int kTextureWidth = 16;
|
|
|
|
const int kTextureHeight = 16;
|
2018-12-07 15:57:26 +00:00
|
|
|
#ifdef SK_BUILD_FOR_IOS
|
|
|
|
// UNPACK_ROW_LENGTH is broken on iOS so rowBytes needs to match data width
|
2019-01-30 18:08:28 +00:00
|
|
|
const int kBufferWidth = GrBackendApi::kOpenGL == context->backend() ? 16 : 20;
|
2018-12-07 15:57:26 +00:00
|
|
|
#else
|
2017-06-21 19:55:46 +00:00
|
|
|
const int kBufferWidth = 20;
|
2018-12-07 15:57:26 +00:00
|
|
|
#endif
|
2017-06-21 19:55:46 +00:00
|
|
|
const int kBufferHeight = 16;
|
|
|
|
size_t rowBytes = kBufferWidth * sizeof(GrColor);
|
|
|
|
SkAutoTMalloc<GrColor> srcBuffer(kBufferWidth*kBufferHeight);
|
|
|
|
SkAutoTMalloc<GrColor> dstBuffer(kBufferWidth*kBufferHeight);
|
|
|
|
|
|
|
|
fill_transfer_data(0, 0, kTextureWidth, kTextureHeight, kBufferWidth, srcBuffer.get());
|
|
|
|
|
|
|
|
// create and fill transfer buffer
|
|
|
|
size_t size = rowBytes*kBufferHeight;
|
2018-09-27 15:28:03 +00:00
|
|
|
auto bufferFlags = GrResourceProvider::Flags::kNoPendingIO;
|
2019-02-05 14:41:37 +00:00
|
|
|
sk_sp<GrBuffer> buffer(resourceProvider->createBuffer(size, GrGpuBufferType::kXferCpuToGpu,
|
|
|
|
kDynamic_GrAccessPattern, bufferFlags));
|
2017-06-21 19:55:46 +00:00
|
|
|
if (!buffer) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
void* data = buffer->map();
|
|
|
|
memcpy(data, srcBuffer.get(), size);
|
|
|
|
buffer->unmap();
|
|
|
|
|
2018-02-20 19:05:36 +00:00
|
|
|
for (auto srgbEncoding : {GrSRGBEncoded::kNo, GrSRGBEncoded::kYes}) {
|
|
|
|
// create texture
|
|
|
|
GrSurfaceDesc desc;
|
|
|
|
desc.fFlags = renderTarget ? kRenderTarget_GrSurfaceFlag : kNone_GrSurfaceFlags;
|
|
|
|
desc.fWidth = kTextureWidth;
|
|
|
|
desc.fHeight = kTextureHeight;
|
|
|
|
desc.fConfig = GrColorTypeToPixelConfig(colorType, srgbEncoding);
|
|
|
|
desc.fSampleCnt = 1;
|
|
|
|
|
|
|
|
if (kUnknown_GrPixelConfig == desc.fConfig) {
|
|
|
|
SkASSERT(GrSRGBEncoded::kYes == srgbEncoding);
|
|
|
|
continue;
|
|
|
|
}
|
2017-06-21 19:55:46 +00:00
|
|
|
|
2019-02-04 18:26:26 +00:00
|
|
|
if (!context->priv().caps()->isConfigTexturable(desc.fConfig) ||
|
|
|
|
(renderTarget && !context->priv().caps()->isConfigRenderable(desc.fConfig))) {
|
2018-02-20 19:05:36 +00:00
|
|
|
continue;
|
|
|
|
}
|
2017-06-21 19:55:46 +00:00
|
|
|
|
2018-02-20 19:05:36 +00:00
|
|
|
sk_sp<GrTexture> tex = resourceProvider->createTexture(desc, SkBudgeted::kNo);
|
2018-07-27 16:21:37 +00:00
|
|
|
if (!tex) {
|
|
|
|
continue;
|
|
|
|
}
|
2018-02-20 19:05:36 +00:00
|
|
|
|
|
|
|
//////////////////////////
|
|
|
|
// transfer full data
|
|
|
|
|
|
|
|
bool result;
|
|
|
|
result = gpu->transferPixels(tex.get(), 0, 0, kTextureWidth, kTextureHeight, colorType,
|
|
|
|
buffer.get(), 0, rowBytes);
|
|
|
|
REPORTER_ASSERT(reporter, result);
|
|
|
|
|
|
|
|
memset(dstBuffer.get(), 0xCDCD, size);
|
2018-06-01 19:33:20 +00:00
|
|
|
result = gpu->readPixels(tex.get(), 0, 0, kTextureWidth, kTextureHeight, colorType,
|
2018-02-20 19:05:36 +00:00
|
|
|
dstBuffer.get(), rowBytes);
|
|
|
|
if (result) {
|
|
|
|
REPORTER_ASSERT(reporter, does_full_buffer_contain_correct_values(srcBuffer,
|
|
|
|
dstBuffer,
|
|
|
|
kTextureWidth,
|
|
|
|
kTextureHeight,
|
|
|
|
kBufferWidth,
|
2018-06-01 19:33:20 +00:00
|
|
|
kBufferHeight));
|
2018-02-20 19:05:36 +00:00
|
|
|
}
|
2017-06-21 19:55:46 +00:00
|
|
|
|
2018-02-20 19:05:36 +00:00
|
|
|
//////////////////////////
|
|
|
|
// transfer partial data
|
2018-12-07 15:57:26 +00:00
|
|
|
#ifdef SK_BUILD_FOR_IOS
|
|
|
|
// UNPACK_ROW_LENGTH is broken on iOS so we can't do partial transfers
|
2019-01-30 18:08:28 +00:00
|
|
|
if (GrBackendApi::kOpenGL == context->backend()) {
|
2018-12-07 15:57:26 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
#endif
|
2018-02-20 19:05:36 +00:00
|
|
|
const int kLeft = 2;
|
|
|
|
const int kTop = 10;
|
|
|
|
const int kWidth = 10;
|
|
|
|
const int kHeight = 2;
|
|
|
|
|
|
|
|
// change color of subrectangle
|
|
|
|
fill_transfer_data(kLeft, kTop, kWidth, kHeight, kBufferWidth, srcBuffer.get());
|
|
|
|
data = buffer->map();
|
|
|
|
memcpy(data, srcBuffer.get(), size);
|
|
|
|
buffer->unmap();
|
|
|
|
|
|
|
|
size_t offset = sizeof(GrColor) * (kTop * kBufferWidth + kLeft);
|
|
|
|
result = gpu->transferPixels(tex.get(), kLeft, kTop, kWidth, kHeight, colorType,
|
|
|
|
buffer.get(), offset, rowBytes);
|
|
|
|
REPORTER_ASSERT(reporter, result);
|
|
|
|
|
|
|
|
memset(dstBuffer.get(), 0xCDCD, size);
|
2018-06-01 19:33:20 +00:00
|
|
|
result = gpu->readPixels(tex.get(), 0, 0, kTextureWidth, kTextureHeight, colorType,
|
2018-02-20 19:05:36 +00:00
|
|
|
dstBuffer.get(), rowBytes);
|
|
|
|
if (result) {
|
|
|
|
REPORTER_ASSERT(reporter, does_full_buffer_contain_correct_values(srcBuffer,
|
|
|
|
dstBuffer,
|
|
|
|
kTextureWidth,
|
|
|
|
kTextureHeight,
|
|
|
|
kBufferWidth,
|
2018-06-01 19:33:20 +00:00
|
|
|
kBufferHeight));
|
2018-02-20 19:05:36 +00:00
|
|
|
}
|
2017-06-27 14:36:56 +00:00
|
|
|
}
|
2017-06-21 19:55:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DEF_GPUTEST_FOR_RENDERING_CONTEXTS(TransferPixelsTest, reporter, ctxInfo) {
|
|
|
|
// RGBA
|
2018-06-01 19:33:20 +00:00
|
|
|
basic_transfer_test(reporter, ctxInfo.grContext(), GrColorType::kRGBA_8888, false);
|
|
|
|
basic_transfer_test(reporter, ctxInfo.grContext(), GrColorType::kRGBA_8888, true);
|
2017-06-21 19:55:46 +00:00
|
|
|
|
|
|
|
// BGRA
|
2018-06-01 19:33:20 +00:00
|
|
|
basic_transfer_test(reporter, ctxInfo.grContext(), GrColorType::kBGRA_8888, false);
|
|
|
|
basic_transfer_test(reporter, ctxInfo.grContext(), GrColorType::kBGRA_8888, true);
|
2017-06-21 19:55:46 +00:00
|
|
|
}
|