2015-11-22 22:51:00 +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"
|
2017-01-25 17:10:37 +00:00
|
|
|
#include "GrContextPriv.h"
|
2015-11-22 22:51:00 +00:00
|
|
|
#include "GrContextFactory.h"
|
2016-11-29 18:43:05 +00:00
|
|
|
#include "GrShaderCaps.h"
|
2016-12-15 14:23:05 +00:00
|
|
|
#include "GrSurfaceContext.h"
|
2015-11-22 22:51:00 +00:00
|
|
|
#include "gl/GrGLGpu.h"
|
|
|
|
#include "gl/GrGLUtil.h"
|
2016-03-31 17:59:06 +00:00
|
|
|
#include "gl/GLTestContext.h"
|
2015-11-22 22:51:00 +00:00
|
|
|
|
2016-03-31 17:59:06 +00:00
|
|
|
using sk_gpu_test::GLTestContext;
|
2016-03-31 01:56:19 +00:00
|
|
|
|
2016-03-31 17:59:06 +00:00
|
|
|
static void cleanup(GLTestContext* glctx0, GrGLuint texID0, GLTestContext* glctx1, GrContext* grctx1,
|
2015-11-22 22:51:00 +00:00
|
|
|
const GrGLTextureInfo* grbackendtex1, GrEGLImage image1) {
|
|
|
|
if (glctx1) {
|
|
|
|
glctx1->makeCurrent();
|
|
|
|
if (grctx1) {
|
|
|
|
if (grbackendtex1) {
|
|
|
|
GrGLGpu* gpu1 = static_cast<GrGLGpu*>(grctx1->getGpu());
|
|
|
|
GrBackendObject handle = reinterpret_cast<GrBackendObject>(grbackendtex1);
|
|
|
|
gpu1->deleteTestingOnlyBackendTexture(handle, false);
|
|
|
|
}
|
|
|
|
grctx1->unref();
|
|
|
|
}
|
|
|
|
if (GR_EGL_NO_IMAGE != image1) {
|
|
|
|
glctx1->destroyEGLImage(image1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
glctx0->makeCurrent();
|
|
|
|
if (texID0) {
|
|
|
|
GR_GL_CALL(glctx0->gl(), DeleteTextures(1, &texID0));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-27 14:28:29 +00:00
|
|
|
static void test_read_pixels(skiatest::Reporter* reporter, GrContext* context,
|
|
|
|
GrSurfaceContext* externalTextureContext,
|
|
|
|
uint32_t expectedPixelValues[]) {
|
|
|
|
int pixelCnt = externalTextureContext->width() * externalTextureContext->height();
|
|
|
|
SkAutoTMalloc<uint32_t> pixels(pixelCnt);
|
|
|
|
memset(pixels.get(), 0, sizeof(uint32_t)*pixelCnt);
|
|
|
|
|
|
|
|
SkImageInfo ii = SkImageInfo::Make(externalTextureContext->width(),
|
|
|
|
externalTextureContext->height(),
|
|
|
|
kRGBA_8888_SkColorType, kPremul_SkAlphaType);
|
|
|
|
bool read = externalTextureContext->readPixels(ii, pixels.get(), 0, 0, 0);
|
|
|
|
if (!read) {
|
|
|
|
ERRORF(reporter, "Error reading external texture.");
|
|
|
|
}
|
|
|
|
for (int i = 0; i < pixelCnt; ++i) {
|
|
|
|
if (pixels.get()[i] != expectedPixelValues[i]) {
|
|
|
|
ERRORF(reporter, "Error, external texture pixel value %d should be 0x%08x,"
|
|
|
|
" got 0x%08x.", i, expectedPixelValues[i], pixels.get()[i]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_write_pixels(skiatest::Reporter* reporter, GrContext* context,
|
|
|
|
GrSurfaceContext* externalTextureContext) {
|
|
|
|
int pixelCnt = externalTextureContext->width() * externalTextureContext->height();
|
|
|
|
SkAutoTMalloc<uint32_t> pixels(pixelCnt);
|
|
|
|
memset(pixels.get(), 0, sizeof(uint32_t)*pixelCnt);
|
|
|
|
|
|
|
|
SkImageInfo ii = SkImageInfo::Make(externalTextureContext->width(),
|
|
|
|
externalTextureContext->height(),
|
|
|
|
kRGBA_8888_SkColorType, kPremul_SkAlphaType);
|
|
|
|
bool write = externalTextureContext->writePixels(ii, pixels.get(), 0, 0, 0);
|
|
|
|
REPORTER_ASSERT_MESSAGE(reporter, !write, "Should not be able to write to a EXTERNAL"
|
|
|
|
" texture.");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_copy_surface(skiatest::Reporter* reporter, GrContext* context,
|
|
|
|
GrSurfaceProxy* externalTextureProxy,
|
|
|
|
uint32_t expectedPixelValues[]) {
|
|
|
|
GrSurfaceDesc copyDesc;
|
|
|
|
copyDesc.fConfig = kRGBA_8888_GrPixelConfig;
|
|
|
|
copyDesc.fWidth = externalTextureProxy->width();
|
|
|
|
copyDesc.fHeight = externalTextureProxy->height();
|
|
|
|
copyDesc.fFlags = kRenderTarget_GrSurfaceFlag;
|
|
|
|
|
|
|
|
sk_sp<GrSurfaceContext> copyContext(GrSurfaceProxy::TestCopy(context, copyDesc,
|
|
|
|
externalTextureProxy));
|
|
|
|
|
|
|
|
test_read_pixels(reporter, context, copyContext.get(), expectedPixelValues);
|
|
|
|
}
|
|
|
|
|
2016-04-06 21:02:39 +00:00
|
|
|
DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(EGLImageTest, reporter, ctxInfo) {
|
2016-05-11 13:33:06 +00:00
|
|
|
GrContext* context0 = ctxInfo.grContext();
|
|
|
|
sk_gpu_test::GLTestContext* glCtx0 = ctxInfo.glContext();
|
2016-04-05 19:59:06 +00:00
|
|
|
|
2015-11-23 07:23:53 +00:00
|
|
|
// Try to create a second GL context and then check if the contexts have necessary
|
|
|
|
// extensions to run this test.
|
2015-11-22 22:51:00 +00:00
|
|
|
|
2015-11-23 07:23:53 +00:00
|
|
|
if (kGLES_GrGLStandard != glCtx0->gl()->fStandard) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
GrGLGpu* gpu0 = static_cast<GrGLGpu*>(context0->getGpu());
|
2016-11-29 18:43:46 +00:00
|
|
|
if (!gpu0->glCaps().shaderCaps()->externalTextureSupport()) {
|
2015-11-23 07:23:53 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-11-22 22:51:00 +00:00
|
|
|
|
2016-11-03 18:40:50 +00:00
|
|
|
std::unique_ptr<GLTestContext> glCtx1 = glCtx0->makeNew();
|
2015-11-23 07:23:53 +00:00
|
|
|
if (!glCtx1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
GrContext* context1 = GrContext::Create(kOpenGL_GrBackend, (GrBackendContext)glCtx1->gl());
|
|
|
|
const GrGLTextureInfo* backendTexture1 = nullptr;
|
|
|
|
GrEGLImage image = GR_EGL_NO_IMAGE;
|
|
|
|
GrGLTextureInfo externalTexture;
|
|
|
|
externalTexture.fID = 0;
|
2015-11-22 22:51:00 +00:00
|
|
|
|
2015-11-23 07:23:53 +00:00
|
|
|
if (!context1) {
|
2016-11-03 18:40:50 +00:00
|
|
|
cleanup(glCtx0, externalTexture.fID, glCtx1.get(), context1, backendTexture1, image);
|
2015-11-23 07:23:53 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-11-22 22:51:00 +00:00
|
|
|
|
2015-11-23 07:23:53 +00:00
|
|
|
if (!glCtx1->gl()->hasExtension("EGL_KHR_image") ||
|
|
|
|
!glCtx1->gl()->hasExtension("EGL_KHR_gl_texture_2D_image")) {
|
2016-11-03 18:40:50 +00:00
|
|
|
cleanup(glCtx0, externalTexture.fID, glCtx1.get(), context1, backendTexture1, image);
|
2015-11-23 07:23:53 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-11-22 22:51:00 +00:00
|
|
|
|
2015-11-23 07:23:53 +00:00
|
|
|
///////////////////////////////// CONTEXT 1 ///////////////////////////////////
|
2015-11-22 22:51:00 +00:00
|
|
|
|
2015-11-23 07:23:53 +00:00
|
|
|
// Use GL Context 1 to create a texture unknown to GrContext.
|
|
|
|
context1->flush();
|
|
|
|
GrGpu* gpu1 = context1->getGpu();
|
|
|
|
static const int kSize = 100;
|
|
|
|
backendTexture1 = reinterpret_cast<const GrGLTextureInfo*>(
|
|
|
|
gpu1->createTestingOnlyBackendTexture(nullptr, kSize, kSize, kRGBA_8888_GrPixelConfig));
|
|
|
|
if (!backendTexture1 || !backendTexture1->fID) {
|
|
|
|
ERRORF(reporter, "Error creating texture for EGL Image");
|
2016-11-03 18:40:50 +00:00
|
|
|
cleanup(glCtx0, externalTexture.fID, glCtx1.get(), context1, backendTexture1, image);
|
2015-11-23 07:23:53 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (GR_GL_TEXTURE_2D != backendTexture1->fTarget) {
|
|
|
|
ERRORF(reporter, "Expected backend texture to be 2D");
|
2016-11-03 18:40:50 +00:00
|
|
|
cleanup(glCtx0, externalTexture.fID, glCtx1.get(), context1, backendTexture1, image);
|
2015-11-23 07:23:53 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-11-22 22:51:00 +00:00
|
|
|
|
2015-11-23 07:23:53 +00:00
|
|
|
// Wrap the texture in an EGLImage
|
|
|
|
image = glCtx1->texture2DToEGLImage(backendTexture1->fID);
|
|
|
|
if (GR_EGL_NO_IMAGE == image) {
|
|
|
|
ERRORF(reporter, "Error creating EGL Image from texture");
|
2016-11-03 18:40:50 +00:00
|
|
|
cleanup(glCtx0, externalTexture.fID, glCtx1.get(), context1, backendTexture1, image);
|
2015-11-23 07:23:53 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Populate the texture using GL context 1. Important to use TexSubImage as TexImage orphans
|
|
|
|
// the EGL image. Also, this must be done after creating the EGLImage as the texture
|
|
|
|
// contents may not be preserved when the image is created.
|
|
|
|
SkAutoTMalloc<uint32_t> pixels(kSize * kSize);
|
|
|
|
for (int i = 0; i < kSize*kSize; ++i) {
|
|
|
|
pixels.get()[i] = 0xDDAABBCC;
|
|
|
|
}
|
|
|
|
GR_GL_CALL(glCtx1->gl(), ActiveTexture(GR_GL_TEXTURE0));
|
|
|
|
GR_GL_CALL(glCtx1->gl(), BindTexture(backendTexture1->fTarget, backendTexture1->fID));
|
|
|
|
GR_GL_CALL(glCtx1->gl(), TexSubImage2D(backendTexture1->fTarget, 0, 0, 0, kSize, kSize,
|
|
|
|
GR_GL_RGBA, GR_GL_UNSIGNED_BYTE, pixels.get()));
|
|
|
|
GR_GL_CALL(glCtx1->gl(), Finish());
|
|
|
|
// We've been making direct GL calls in GL context 1, let GrContext 1 know its internal
|
|
|
|
// state is invalid.
|
|
|
|
context1->resetContext();
|
|
|
|
|
|
|
|
///////////////////////////////// CONTEXT 0 ///////////////////////////////////
|
|
|
|
|
|
|
|
// Make a new texture ID in GL Context 0 from the EGL Image
|
|
|
|
glCtx0->makeCurrent();
|
|
|
|
externalTexture.fTarget = GR_GL_TEXTURE_EXTERNAL;
|
|
|
|
externalTexture.fID = glCtx0->eglImageToExternalTexture(image);
|
|
|
|
|
|
|
|
// Wrap this texture ID in a GrTexture
|
|
|
|
GrBackendTextureDesc externalDesc;
|
|
|
|
externalDesc.fConfig = kRGBA_8888_GrPixelConfig;
|
|
|
|
externalDesc.fWidth = kSize;
|
|
|
|
externalDesc.fHeight = kSize;
|
|
|
|
externalDesc.fTextureHandle = reinterpret_cast<GrBackendObject>(&externalTexture);
|
2017-01-25 17:10:37 +00:00
|
|
|
|
|
|
|
sk_sp<GrSurfaceContext> externalTextureContext;
|
|
|
|
|
|
|
|
{
|
|
|
|
sk_sp<GrTexture> externalTextureObj(
|
|
|
|
context0->textureProvider()->wrapBackendTexture(externalDesc));
|
|
|
|
if (!externalTextureObj) {
|
|
|
|
ERRORF(reporter, "Error wrapping external texture in GrTexture.");
|
|
|
|
cleanup(glCtx0, externalTexture.fID, glCtx1.get(), context1, backendTexture1, image);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
externalTextureContext = context0->contextPriv().makeWrappedSurfaceContext(
|
|
|
|
std::move(externalTextureObj));
|
2015-11-23 07:23:53 +00:00
|
|
|
}
|
|
|
|
|
2015-11-23 15:09:50 +00:00
|
|
|
// Should not be able to wrap as a RT
|
2017-01-25 17:10:37 +00:00
|
|
|
{
|
|
|
|
externalDesc.fFlags = kRenderTarget_GrBackendTextureFlag;
|
|
|
|
sk_sp<GrTexture> externalTextureRTObj(
|
|
|
|
context0->textureProvider()->wrapBackendTexture(externalDesc));
|
|
|
|
if (externalTextureRTObj) {
|
|
|
|
ERRORF(reporter, "Should not be able to wrap an EXTERNAL texture as a RT.");
|
|
|
|
}
|
|
|
|
externalDesc.fFlags = kNone_GrBackendTextureFlag;
|
2015-11-23 07:23:53 +00:00
|
|
|
}
|
2015-11-23 15:09:50 +00:00
|
|
|
|
|
|
|
// Should not be able to wrap with a sample count
|
2017-01-25 17:10:37 +00:00
|
|
|
{
|
|
|
|
externalDesc.fSampleCnt = 4;
|
|
|
|
sk_sp<GrTexture> externalTextureMSAAObj(
|
|
|
|
context0->textureProvider()->wrapBackendTexture(externalDesc));
|
|
|
|
if (externalTextureMSAAObj) {
|
|
|
|
ERRORF(reporter, "Should not be able to wrap an EXTERNAL texture with MSAA.");
|
|
|
|
}
|
|
|
|
externalDesc.fSampleCnt = 0;
|
2015-11-22 22:51:00 +00:00
|
|
|
}
|
2015-11-23 15:09:50 +00:00
|
|
|
|
2017-01-27 14:28:29 +00:00
|
|
|
test_read_pixels(reporter, context0, externalTextureContext.get(), pixels.get());
|
2015-11-23 15:09:50 +00:00
|
|
|
|
2017-01-27 14:28:29 +00:00
|
|
|
test_write_pixels(reporter, context0, externalTextureContext.get());
|
2015-11-23 15:09:50 +00:00
|
|
|
|
2017-01-27 14:28:29 +00:00
|
|
|
test_copy_surface(reporter, context0, externalTextureContext->asDeferredSurface(),
|
|
|
|
pixels.get());
|
2015-11-23 15:09:50 +00:00
|
|
|
|
2016-11-03 18:40:50 +00:00
|
|
|
cleanup(glCtx0, externalTexture.fID, glCtx1.get(), context1, backendTexture1, image);
|
2015-11-22 22:51:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|