skia2/tests/CopySurfaceTest.cpp
Robert Phillips 2c86249465 Move read/write-Pixels up to GrSurfaceContext
This still needs to be propagated out in several ways:
  replace more instances of GrSurface::read/write-Pixels
  add colorSpace to more instances of the TextureContext

but it establishes a beach-head and is exciting enough as is.

Change-Id: If86035aa0245e70b54541e83722b3c75bc5ade13
Reviewed-on: https://skia-review.googlesource.com/7172
Reviewed-by: Brian Osman <brianosman@google.com>
Reviewed-by: Brian Salomon <bsalomon@google.com>
Commit-Queue: Robert Phillips <robertphillips@google.com>
2017-01-18 16:22:59 +00:00

178 lines
8.2 KiB
C++

/*
* Copyright 2016 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include <initializer_list>
#include "Test.h"
#if SK_SUPPORT_GPU
#include "GrContext.h"
#include "GrContextPriv.h"
#include "GrSurfaceContext.h"
#include "GrSurfaceProxy.h"
#include "GrTexture.h"
#include "GrTextureProvider.h"
#include "SkUtils.h"
DEF_GPUTEST_FOR_RENDERING_CONTEXTS(CopySurface, reporter, ctxInfo) {
GrContext* context = ctxInfo.grContext();
static const int kW = 10;
static const int kH = 10;
static const size_t kRowBytes = sizeof(uint32_t) * kW;
GrSurfaceDesc baseDesc;
baseDesc.fConfig = kRGBA_8888_GrPixelConfig;
baseDesc.fWidth = kW;
baseDesc.fHeight = kH;
SkAutoTMalloc<uint32_t> srcPixels(kW * kH);
for (int i = 0; i < kW * kH; ++i) {
srcPixels.get()[i] = i;
}
SkAutoTMalloc<uint32_t> dstPixels(kW * kH);
for (int i = 0; i < kW * kH; ++i) {
dstPixels.get()[i] = ~i;
}
static const SkIRect kSrcRects[] {
{ 0, 0, kW , kH },
{-1, -1, kW+1, kH+1},
{ 1, 1, kW-1, kH-1},
{ 5, 5, 6 , 6 },
};
static const SkIPoint kDstPoints[] {
{ 0 , 0 },
{ 1 , 1 },
{ kW/2, kH/4},
{ kW-1, kH-1},
{ kW , kH },
{ kW+1, kH+2},
{-1 , -1 },
};
SkAutoTMalloc<uint32_t> read(kW * kH);
for (auto sOrigin : {kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin}) {
for (auto dOrigin : {kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin}) {
for (auto sFlags: {kRenderTarget_GrSurfaceFlag, kNone_GrSurfaceFlags}) {
for (auto dFlags: {kRenderTarget_GrSurfaceFlag, kNone_GrSurfaceFlags}) {
for (auto srcRect : kSrcRects) {
for (auto dstPoint : kDstPoints) {
GrSurfaceDesc srcDesc = baseDesc;
srcDesc.fOrigin = sOrigin;
srcDesc.fFlags = sFlags;
GrSurfaceDesc dstDesc = baseDesc;
dstDesc.fOrigin = dOrigin;
dstDesc.fFlags = dFlags;
sk_sp<GrSurfaceProxy> src(GrSurfaceProxy::MakeDeferred(
*context->caps(),
context->textureProvider(),
srcDesc, SkBudgeted::kNo,
srcPixels.get(),
kRowBytes));
sk_sp<GrSurfaceProxy> dst(GrSurfaceProxy::MakeDeferred(
*context->caps(),
context->textureProvider(),
dstDesc, SkBudgeted::kNo,
dstPixels.get(),
kRowBytes));
if (!src || !dst) {
ERRORF(reporter,
"Could not create surfaces for copy surface test.");
continue;
}
sk_sp<GrSurfaceContext> sContext =
context->contextPriv().makeWrappedSurfaceContext(dst, nullptr);
bool result = sContext->copy(src.get(), srcRect, dstPoint);
bool expectedResult = true;
SkIPoint dstOffset = { dstPoint.fX - srcRect.fLeft,
dstPoint.fY - srcRect.fTop };
SkIRect copiedDstRect = SkIRect::MakeXYWH(dstPoint.fX,
dstPoint.fY,
srcRect.width(),
srcRect.height());
SkIRect copiedSrcRect;
if (!copiedSrcRect.intersect(srcRect, SkIRect::MakeWH(kW, kH))) {
expectedResult = false;
} else {
// If the src rect was clipped, apply same clipping to each side of
// copied dst rect.
copiedDstRect.fLeft += copiedSrcRect.fLeft - srcRect.fLeft;
copiedDstRect.fTop += copiedSrcRect.fTop - srcRect.fTop;
copiedDstRect.fRight -= copiedSrcRect.fRight - srcRect.fRight;
copiedDstRect.fBottom -= copiedSrcRect.fBottom - srcRect.fBottom;
}
if (copiedDstRect.isEmpty() ||
!copiedDstRect.intersect(SkIRect::MakeWH(kW, kH))) {
expectedResult = false;
}
// To make the copied src rect correct we would apply any dst clipping
// back to the src rect, but we don't use it again so don't bother.
if (expectedResult != result) {
ERRORF(reporter, "Expected return value %d from copySurface, got "
"%d.", expectedResult, result);
continue;
}
if (!expectedResult || !result) {
continue;
}
GrSurface* dstSurf = dst->instantiate(context->textureProvider());
sk_memset32(read.get(), 0, kW * kH);
if (!dstSurf->readPixels(0, 0, kW, kH, baseDesc.fConfig, read.get(),
kRowBytes)) {
ERRORF(reporter, "Error calling readPixels");
continue;
}
bool abort = false;
// Validate that pixels inside copiedDstRect received the correct value
// from src and that those outside were not modified.
for (int y = 0; y < kH && !abort; ++y) {
for (int x = 0; x < kW; ++x) {
uint32_t r = read.get()[y * kW + x];
if (copiedDstRect.contains(x, y)) {
int sx = x - dstOffset.fX;
int sy = y - dstOffset.fY;
uint32_t s = srcPixels.get()[sy * kW + sx];
if (s != r) {
ERRORF(reporter, "Expected dst %d,%d to contain "
"0x%08x copied from src location %d,%d. Got "
"0x%08x", x, y, s, sx, sy, r);
abort = true;
break;
}
} else {
uint32_t d = dstPixels.get()[y * kW + x];
if (d != r) {
ERRORF(reporter, "Expected dst %d,%d to be unmodified ("
"0x%08x). Got 0x%08x", x, y, d, r);
abort = true;
break;
}
}
}
}
}
}
}
}
}
}
}
#endif