2016-01-14 17:24:09 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2015 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "Test.h"
|
|
|
|
#if SK_SUPPORT_GPU
|
|
|
|
#include "GrContext.h"
|
2016-08-03 21:26:53 +00:00
|
|
|
#include "GrContextPriv.h"
|
2016-10-27 18:47:55 +00:00
|
|
|
#include "GrRenderTargetContext.h"
|
2016-01-14 17:24:09 +00:00
|
|
|
#include "gl/GrGLGpu.h"
|
|
|
|
#include "gl/GrGLUtil.h"
|
2016-03-31 17:59:06 +00:00
|
|
|
#include "gl/GLTestContext.h"
|
2016-01-14 17:24:09 +00:00
|
|
|
|
|
|
|
static void test_read_pixels(skiatest::Reporter* reporter, GrContext* context,
|
2016-11-07 14:53:44 +00:00
|
|
|
GrTexture* texture, uint32_t expectedPixelValues[]) {
|
|
|
|
int pixelCnt = texture->width() * texture->height();
|
2016-01-14 17:24:09 +00:00
|
|
|
SkAutoTMalloc<uint32_t> pixels(pixelCnt);
|
|
|
|
memset(pixels.get(), 0, sizeof(uint32_t)*pixelCnt);
|
2016-11-07 14:53:44 +00:00
|
|
|
bool read = texture->readPixels(0, 0, texture->width(), texture->height(),
|
|
|
|
kRGBA_8888_GrPixelConfig, pixels.get());
|
2016-01-14 17:24:09 +00:00
|
|
|
if (!read) {
|
|
|
|
ERRORF(reporter, "Error reading rectangle texture.");
|
|
|
|
}
|
|
|
|
for (int i = 0; i < pixelCnt; ++i) {
|
|
|
|
if (pixels.get()[i] != expectedPixelValues[i]) {
|
2016-11-07 14:53:44 +00:00
|
|
|
ERRORF(reporter, "Error, pixel value %d should be 0x%08x, got 0x%08x.", i,
|
|
|
|
expectedPixelValues[i], pixels.get()[i]);
|
2016-01-14 17:24:09 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_write_pixels(skiatest::Reporter* reporter, GrContext* context,
|
|
|
|
GrTexture* rectangleTexture) {
|
|
|
|
int pixelCnt = rectangleTexture->width() * rectangleTexture->height();
|
|
|
|
SkAutoTMalloc<uint32_t> pixels(pixelCnt);
|
|
|
|
for (int y = 0; y < rectangleTexture->width(); ++y) {
|
|
|
|
for (int x = 0; x < rectangleTexture->height(); ++x) {
|
|
|
|
pixels.get()[y * rectangleTexture->width() + x] = GrColorPackRGBA(x, y, x + y, x * y);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
bool write = rectangleTexture->writePixels(0, 0, rectangleTexture->width(),
|
|
|
|
rectangleTexture->height(), kRGBA_8888_GrPixelConfig,
|
|
|
|
pixels.get());
|
|
|
|
if (!write) {
|
|
|
|
ERRORF(reporter, "Error writing to rectangle texture.");
|
|
|
|
}
|
|
|
|
test_read_pixels(reporter, context, rectangleTexture, pixels.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_copy_surface_src(skiatest::Reporter* reporter, GrContext* context,
|
|
|
|
GrTexture* rectangleTexture, uint32_t expectedPixelValues[]) {
|
2016-11-03 19:21:06 +00:00
|
|
|
for (auto flags : {kNone_GrSurfaceFlags, kRenderTarget_GrSurfaceFlag}) {
|
|
|
|
GrSurfaceDesc copyDstDesc;
|
|
|
|
copyDstDesc.fConfig = kRGBA_8888_GrPixelConfig;
|
|
|
|
copyDstDesc.fWidth = rectangleTexture->width();
|
|
|
|
copyDstDesc.fHeight = rectangleTexture->height();
|
|
|
|
copyDstDesc.fFlags = flags;
|
2016-11-04 15:49:42 +00:00
|
|
|
sk_sp<GrTexture> dst(
|
|
|
|
context->textureProvider()->createTexture(copyDstDesc, SkBudgeted::kYes));
|
|
|
|
context->copySurface(dst.get(), rectangleTexture);
|
|
|
|
test_read_pixels(reporter, context, dst.get(), expectedPixelValues);
|
2016-11-03 19:21:06 +00:00
|
|
|
}
|
2016-01-14 17:24:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void test_copy_surface_dst(skiatest::Reporter* reporter, GrContext* context,
|
|
|
|
GrTexture* rectangleTexture) {
|
|
|
|
int pixelCnt = rectangleTexture->width() * rectangleTexture->height();
|
|
|
|
SkAutoTMalloc<uint32_t> pixels(pixelCnt);
|
|
|
|
for (int y = 0; y < rectangleTexture->width(); ++y) {
|
|
|
|
for (int x = 0; x < rectangleTexture->height(); ++x) {
|
|
|
|
pixels.get()[y * rectangleTexture->width() + x] = GrColorPackRGBA(y, x, x * y, x *+ y);
|
|
|
|
}
|
|
|
|
}
|
2016-11-03 19:21:06 +00:00
|
|
|
for (auto flags : {kNone_GrSurfaceFlags, kRenderTarget_GrSurfaceFlag}) {
|
|
|
|
GrSurfaceDesc copySrcDesc;
|
|
|
|
copySrcDesc.fConfig = kRGBA_8888_GrPixelConfig;
|
|
|
|
copySrcDesc.fWidth = rectangleTexture->width();
|
|
|
|
copySrcDesc.fHeight = rectangleTexture->height();
|
|
|
|
copySrcDesc.fFlags = flags;
|
2016-11-04 15:49:42 +00:00
|
|
|
sk_sp<GrTexture> src(context->textureProvider()->createTexture(
|
2016-11-03 19:21:06 +00:00
|
|
|
copySrcDesc, SkBudgeted::kYes, pixels.get(), 0));
|
2016-01-14 17:24:09 +00:00
|
|
|
|
2016-11-04 15:49:42 +00:00
|
|
|
context->copySurface(rectangleTexture, src.get());
|
2016-11-03 19:21:06 +00:00
|
|
|
test_read_pixels(reporter, context, rectangleTexture, pixels.get());
|
|
|
|
}
|
2016-01-14 17:24:09 +00:00
|
|
|
}
|
|
|
|
|
2016-11-07 14:53:44 +00:00
|
|
|
// skbug.com/5932
|
|
|
|
static void test_basic_draw(skiatest::Reporter* reporter, GrContext* context,
|
|
|
|
GrTexture* rectangleTexture, uint32_t expectedPixelValues[]) {
|
|
|
|
sk_sp<GrRenderTargetContext> rtContext(
|
|
|
|
context->makeRenderTargetContext(SkBackingFit::kExact, rectangleTexture->width(),
|
|
|
|
rectangleTexture->height(), rectangleTexture->config(),
|
|
|
|
nullptr));
|
|
|
|
SkMatrix m;
|
|
|
|
m.setIDiv(rectangleTexture->width(), rectangleTexture->height());
|
2016-11-17 20:17:07 +00:00
|
|
|
for (auto filter : {GrSamplerParams::kNone_FilterMode,
|
|
|
|
GrSamplerParams::kBilerp_FilterMode,
|
|
|
|
GrSamplerParams::kMipMap_FilterMode}) {
|
2016-11-07 14:53:44 +00:00
|
|
|
rtContext->clear(nullptr, 0xDDCCBBAA, true);
|
|
|
|
sk_sp<GrFragmentProcessor> fp(GrSimpleTextureEffect::Make(rectangleTexture,
|
|
|
|
nullptr, m, filter));
|
|
|
|
GrPaint paint;
|
|
|
|
paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
|
|
|
|
paint.addColorFragmentProcessor(std::move(fp));
|
|
|
|
rtContext->drawPaint(GrNoClip(), paint, SkMatrix::I());
|
|
|
|
test_read_pixels(reporter, context, rtContext->asTexture().get(), expectedPixelValues);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-14 17:24:09 +00:00
|
|
|
static void test_clear(skiatest::Reporter* reporter, GrContext* context,
|
|
|
|
GrTexture* rectangleTexture) {
|
|
|
|
if (rectangleTexture->asRenderTarget()) {
|
2016-10-27 18:47:55 +00:00
|
|
|
sk_sp<GrRenderTargetContext> rtc(context->contextPriv().makeWrappedRenderTargetContext(
|
2016-08-03 21:26:53 +00:00
|
|
|
sk_ref_sp(rectangleTexture->asRenderTarget()),
|
|
|
|
nullptr));
|
2016-10-27 18:47:55 +00:00
|
|
|
if (!rtc) {
|
|
|
|
ERRORF(reporter, "Could not get GrRenderTargetContext for rectangle texture.");
|
2016-01-14 17:24:09 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clear the whole thing.
|
|
|
|
GrColor color0 = GrColorPackRGBA(0xA, 0xB, 0xC, 0xD);
|
2016-10-27 18:47:55 +00:00
|
|
|
rtc->clear(nullptr, color0, false);
|
2016-01-14 17:24:09 +00:00
|
|
|
|
|
|
|
int w = rectangleTexture->width();
|
|
|
|
int h = rectangleTexture->height();
|
|
|
|
int pixelCnt = w * h;
|
|
|
|
SkAutoTMalloc<uint32_t> expectedPixels(pixelCnt);
|
|
|
|
|
|
|
|
// The clear color is a GrColor, our readback is to kRGBA_8888, which may be different.
|
2016-01-15 14:21:18 +00:00
|
|
|
uint32_t expectedColor0 = 0;
|
2016-01-14 17:24:09 +00:00
|
|
|
uint8_t* expectedBytes0 = SkTCast<uint8_t*>(&expectedColor0);
|
|
|
|
expectedBytes0[0] = GrColorUnpackR(color0);
|
|
|
|
expectedBytes0[1] = GrColorUnpackG(color0);
|
|
|
|
expectedBytes0[2] = GrColorUnpackB(color0);
|
|
|
|
expectedBytes0[3] = GrColorUnpackA(color0);
|
|
|
|
for (int i = 0; i < rectangleTexture->width() * rectangleTexture->height(); ++i) {
|
|
|
|
expectedPixels.get()[i] = expectedColor0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clear the the top to a different color.
|
|
|
|
GrColor color1 = GrColorPackRGBA(0x1, 0x2, 0x3, 0x4);
|
|
|
|
SkIRect rect = SkIRect::MakeWH(w, h/2);
|
2016-10-27 18:47:55 +00:00
|
|
|
rtc->clear(&rect, color1, false);
|
2016-01-14 17:24:09 +00:00
|
|
|
|
2016-01-15 14:21:18 +00:00
|
|
|
uint32_t expectedColor1 = 0;
|
2016-01-14 17:24:09 +00:00
|
|
|
uint8_t* expectedBytes1 = SkTCast<uint8_t*>(&expectedColor1);
|
|
|
|
expectedBytes1[0] = GrColorUnpackR(color1);
|
|
|
|
expectedBytes1[1] = GrColorUnpackG(color1);
|
|
|
|
expectedBytes1[2] = GrColorUnpackB(color1);
|
|
|
|
expectedBytes1[3] = GrColorUnpackA(color1);
|
|
|
|
|
|
|
|
for (int y = 0; y < h/2; ++y) {
|
|
|
|
for (int x = 0; x < w; ++x) {
|
|
|
|
expectedPixels.get()[y * h + x] = expectedColor1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
test_read_pixels(reporter, context, rectangleTexture, expectedPixels.get());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-06 21:02:39 +00:00
|
|
|
DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(RectangleTexture, reporter, ctxInfo) {
|
2016-05-11 13:33:06 +00:00
|
|
|
GrContext* context = ctxInfo.grContext();
|
|
|
|
sk_gpu_test::GLTestContext* glContext = ctxInfo.glContext();
|
2016-01-14 17:24:09 +00:00
|
|
|
static const int kWidth = 13;
|
|
|
|
static const int kHeight = 13;
|
|
|
|
|
|
|
|
GrColor pixels[kWidth * kHeight];
|
|
|
|
for (int y = 0; y < kHeight; ++y) {
|
|
|
|
for (int x = 0; x < kWidth; ++x) {
|
|
|
|
pixels[y * kWidth + x] = y * kWidth + x;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int origin = 0; origin < 2; ++origin) {
|
|
|
|
GrGLuint rectTexID = glContext->createTextureRectangle(kWidth, kHeight, GR_GL_RGBA,
|
|
|
|
GR_GL_RGBA, GR_GL_UNSIGNED_BYTE,
|
|
|
|
pixels);
|
|
|
|
|
|
|
|
if (!rectTexID) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Let GrContext know that we messed with the GL context directly.
|
|
|
|
context->resetContext();
|
|
|
|
|
|
|
|
// Wrap the rectangle texture ID in a GrTexture
|
|
|
|
GrGLTextureInfo rectangleInfo;
|
|
|
|
rectangleInfo.fID = rectTexID;
|
|
|
|
rectangleInfo.fTarget = GR_GL_TEXTURE_RECTANGLE;
|
|
|
|
|
|
|
|
GrBackendTextureDesc rectangleDesc;
|
|
|
|
rectangleDesc.fFlags = kRenderTarget_GrBackendTextureFlag;
|
|
|
|
rectangleDesc.fConfig = kRGBA_8888_GrPixelConfig;
|
|
|
|
rectangleDesc.fWidth = kWidth;
|
|
|
|
rectangleDesc.fHeight = kHeight;
|
|
|
|
rectangleDesc.fOrigin = origin ? kBottomLeft_GrSurfaceOrigin : kTopLeft_GrSurfaceOrigin;
|
|
|
|
rectangleDesc.fTextureHandle = reinterpret_cast<GrBackendObject>(&rectangleInfo);
|
|
|
|
|
|
|
|
GrColor refPixels[kWidth * kHeight];
|
|
|
|
bool flipRef = rectangleDesc.fOrigin == kBottomLeft_GrSurfaceOrigin;
|
|
|
|
for (int y = 0; y < kHeight; ++y) {
|
|
|
|
for (int x = 0; x < kWidth; ++x) {
|
|
|
|
int y0 = flipRef ? kHeight - y - 1 : y;
|
|
|
|
refPixels[y * kWidth + x] = pixels[y0 * kWidth + x];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-27 16:30:08 +00:00
|
|
|
sk_sp<GrTexture> rectangleTexture(
|
2016-01-14 17:24:09 +00:00
|
|
|
context->textureProvider()->wrapBackendTexture(rectangleDesc));
|
|
|
|
if (!rectangleTexture) {
|
|
|
|
ERRORF(reporter, "Error wrapping rectangle texture in GrTexture.");
|
|
|
|
GR_GL_CALL(glContext->gl(), DeleteTextures(1, &rectTexID));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-10-27 16:30:08 +00:00
|
|
|
test_read_pixels(reporter, context, rectangleTexture.get(), refPixels);
|
2016-01-14 17:24:09 +00:00
|
|
|
|
2016-11-07 14:53:44 +00:00
|
|
|
test_basic_draw(reporter, context, rectangleTexture.get(), refPixels);
|
|
|
|
|
2016-10-27 16:30:08 +00:00
|
|
|
test_copy_surface_src(reporter, context, rectangleTexture.get(), refPixels);
|
2016-01-14 17:24:09 +00:00
|
|
|
|
2016-10-27 16:30:08 +00:00
|
|
|
test_copy_surface_dst(reporter, context, rectangleTexture.get());
|
2016-01-14 17:24:09 +00:00
|
|
|
|
2016-10-27 16:30:08 +00:00
|
|
|
test_write_pixels(reporter, context, rectangleTexture.get());
|
2016-01-14 17:24:09 +00:00
|
|
|
|
2016-10-27 16:30:08 +00:00
|
|
|
test_clear(reporter, context, rectangleTexture.get());
|
2016-01-14 17:24:09 +00:00
|
|
|
|
|
|
|
GR_GL_CALL(glContext->gl(), DeleteTextures(1, &rectTexID));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|