blob: f78a0ea02abdd64280fa90b379de7ab55042cafb [file] [log] [blame]
bsalomon7ea33f52015-11-22 14:51:00 -08001/*
2 * Copyright 2015 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "Test.h"
9#if SK_SUPPORT_GPU
10#include "GrContext.h"
11#include "GrContextFactory.h"
Brian Salomon94efbf52016-11-29 13:43:05 -050012#include "GrShaderCaps.h"
bsalomon7ea33f52015-11-22 14:51:00 -080013#include "gl/GrGLGpu.h"
14#include "gl/GrGLUtil.h"
bsalomon273c0f52016-03-31 10:59:06 -070015#include "gl/GLTestContext.h"
bsalomon7ea33f52015-11-22 14:51:00 -080016
bsalomon273c0f52016-03-31 10:59:06 -070017using sk_gpu_test::GLTestContext;
bsalomon3724e572016-03-30 18:56:19 -070018
bsalomon273c0f52016-03-31 10:59:06 -070019static void cleanup(GLTestContext* glctx0, GrGLuint texID0, GLTestContext* glctx1, GrContext* grctx1,
bsalomon7ea33f52015-11-22 14:51:00 -080020 const GrGLTextureInfo* grbackendtex1, GrEGLImage image1) {
21 if (glctx1) {
22 glctx1->makeCurrent();
23 if (grctx1) {
24 if (grbackendtex1) {
25 GrGLGpu* gpu1 = static_cast<GrGLGpu*>(grctx1->getGpu());
26 GrBackendObject handle = reinterpret_cast<GrBackendObject>(grbackendtex1);
27 gpu1->deleteTestingOnlyBackendTexture(handle, false);
28 }
29 grctx1->unref();
30 }
31 if (GR_EGL_NO_IMAGE != image1) {
32 glctx1->destroyEGLImage(image1);
33 }
bsalomon7ea33f52015-11-22 14:51:00 -080034 }
35
36 glctx0->makeCurrent();
37 if (texID0) {
38 GR_GL_CALL(glctx0->gl(), DeleteTextures(1, &texID0));
39 }
40}
41
bsalomona98419b2015-11-23 07:09:50 -080042static void test_read_pixels(skiatest::Reporter* reporter, GrContext* context,
43 GrTexture* externalTexture, uint32_t expectedPixelValues[]) {
44 int pixelCnt = externalTexture->width() * externalTexture->height();
45 SkAutoTMalloc<uint32_t> pixels(pixelCnt);
46 memset(pixels.get(), 0, sizeof(uint32_t)*pixelCnt);
47 bool read = externalTexture->readPixels(0, 0, externalTexture->width(),
48 externalTexture->height(), kRGBA_8888_GrPixelConfig,
49 pixels.get());
50 if (!read) {
51 ERRORF(reporter, "Error reading external texture.");
52 }
53 for (int i = 0; i < pixelCnt; ++i) {
54 if (pixels.get()[i] != expectedPixelValues[i]) {
55 ERRORF(reporter, "Error, external texture pixel value %d should be 0x%08x,"
56 " got 0x%08x.", i, expectedPixelValues[i], pixels.get()[i]);
57 break;
58 }
59 }
60}
61
62static void test_write_pixels(skiatest::Reporter* reporter, GrContext* context,
63 GrTexture* externalTexture) {
64 int pixelCnt = externalTexture->width() * externalTexture->height();
65 SkAutoTMalloc<uint32_t> pixels(pixelCnt);
66 memset(pixels.get(), 0, sizeof(uint32_t)*pixelCnt);
67 bool write = externalTexture->writePixels(0, 0, 0, 0, kRGBA_8888_GrPixelConfig, pixels.get());
68 REPORTER_ASSERT_MESSAGE(reporter, !write, "Should not be able to write to a EXTERNAL"
69 " texture.");
70}
71
72static void test_copy_surface(skiatest::Reporter* reporter, GrContext* context,
73 GrTexture* externalTexture, uint32_t expectedPixelValues[]) {
74 GrSurfaceDesc copyDesc;
75 copyDesc.fConfig = kRGBA_8888_GrPixelConfig;
76 copyDesc.fWidth = externalTexture->width();
77 copyDesc.fHeight = externalTexture->height();
78 copyDesc.fFlags = kRenderTarget_GrSurfaceFlag;
Hal Canary342b7ac2016-11-04 11:49:42 -040079 sk_sp<GrTexture> copy(context->textureProvider()->createTexture(copyDesc, SkBudgeted::kYes));
80 context->copySurface(copy.get(), externalTexture);
81 test_read_pixels(reporter, context, copy.get(), expectedPixelValues);
bsalomona98419b2015-11-23 07:09:50 -080082}
83
bsalomon758586c2016-04-06 14:02:39 -070084DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(EGLImageTest, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -070085 GrContext* context0 = ctxInfo.grContext();
86 sk_gpu_test::GLTestContext* glCtx0 = ctxInfo.glContext();
bsalomonf2f1c172016-04-05 12:59:06 -070087
kkinnunen59319222015-11-22 23:23:53 -080088 // Try to create a second GL context and then check if the contexts have necessary
89 // extensions to run this test.
bsalomon7ea33f52015-11-22 14:51:00 -080090
kkinnunen59319222015-11-22 23:23:53 -080091 if (kGLES_GrGLStandard != glCtx0->gl()->fStandard) {
92 return;
bsalomon7ea33f52015-11-22 14:51:00 -080093 }
kkinnunen59319222015-11-22 23:23:53 -080094 GrGLGpu* gpu0 = static_cast<GrGLGpu*>(context0->getGpu());
Brian Salomon1edc5b92016-11-29 13:43:46 -050095 if (!gpu0->glCaps().shaderCaps()->externalTextureSupport()) {
kkinnunen59319222015-11-22 23:23:53 -080096 return;
97 }
98
Ben Wagner145dbcd2016-11-03 14:40:50 -040099 std::unique_ptr<GLTestContext> glCtx1 = glCtx0->makeNew();
kkinnunen59319222015-11-22 23:23:53 -0800100 if (!glCtx1) {
101 return;
102 }
103 GrContext* context1 = GrContext::Create(kOpenGL_GrBackend, (GrBackendContext)glCtx1->gl());
104 const GrGLTextureInfo* backendTexture1 = nullptr;
105 GrEGLImage image = GR_EGL_NO_IMAGE;
106 GrGLTextureInfo externalTexture;
107 externalTexture.fID = 0;
108
109 if (!context1) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400110 cleanup(glCtx0, externalTexture.fID, glCtx1.get(), context1, backendTexture1, image);
kkinnunen59319222015-11-22 23:23:53 -0800111 return;
112 }
113
114 if (!glCtx1->gl()->hasExtension("EGL_KHR_image") ||
115 !glCtx1->gl()->hasExtension("EGL_KHR_gl_texture_2D_image")) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400116 cleanup(glCtx0, externalTexture.fID, glCtx1.get(), context1, backendTexture1, image);
kkinnunen59319222015-11-22 23:23:53 -0800117 return;
118 }
119
120 ///////////////////////////////// CONTEXT 1 ///////////////////////////////////
121
122 // Use GL Context 1 to create a texture unknown to GrContext.
123 context1->flush();
124 GrGpu* gpu1 = context1->getGpu();
125 static const int kSize = 100;
126 backendTexture1 = reinterpret_cast<const GrGLTextureInfo*>(
127 gpu1->createTestingOnlyBackendTexture(nullptr, kSize, kSize, kRGBA_8888_GrPixelConfig));
128 if (!backendTexture1 || !backendTexture1->fID) {
129 ERRORF(reporter, "Error creating texture for EGL Image");
Ben Wagner145dbcd2016-11-03 14:40:50 -0400130 cleanup(glCtx0, externalTexture.fID, glCtx1.get(), context1, backendTexture1, image);
kkinnunen59319222015-11-22 23:23:53 -0800131 return;
132 }
133 if (GR_GL_TEXTURE_2D != backendTexture1->fTarget) {
134 ERRORF(reporter, "Expected backend texture to be 2D");
Ben Wagner145dbcd2016-11-03 14:40:50 -0400135 cleanup(glCtx0, externalTexture.fID, glCtx1.get(), context1, backendTexture1, image);
kkinnunen59319222015-11-22 23:23:53 -0800136 return;
137 }
138
139 // Wrap the texture in an EGLImage
140 image = glCtx1->texture2DToEGLImage(backendTexture1->fID);
141 if (GR_EGL_NO_IMAGE == image) {
142 ERRORF(reporter, "Error creating EGL Image from texture");
Ben Wagner145dbcd2016-11-03 14:40:50 -0400143 cleanup(glCtx0, externalTexture.fID, glCtx1.get(), context1, backendTexture1, image);
kkinnunen59319222015-11-22 23:23:53 -0800144 return;
145 }
146
147 // Populate the texture using GL context 1. Important to use TexSubImage as TexImage orphans
148 // the EGL image. Also, this must be done after creating the EGLImage as the texture
149 // contents may not be preserved when the image is created.
150 SkAutoTMalloc<uint32_t> pixels(kSize * kSize);
151 for (int i = 0; i < kSize*kSize; ++i) {
152 pixels.get()[i] = 0xDDAABBCC;
153 }
154 GR_GL_CALL(glCtx1->gl(), ActiveTexture(GR_GL_TEXTURE0));
155 GR_GL_CALL(glCtx1->gl(), BindTexture(backendTexture1->fTarget, backendTexture1->fID));
156 GR_GL_CALL(glCtx1->gl(), TexSubImage2D(backendTexture1->fTarget, 0, 0, 0, kSize, kSize,
157 GR_GL_RGBA, GR_GL_UNSIGNED_BYTE, pixels.get()));
158 GR_GL_CALL(glCtx1->gl(), Finish());
159 // We've been making direct GL calls in GL context 1, let GrContext 1 know its internal
160 // state is invalid.
161 context1->resetContext();
162
163 ///////////////////////////////// CONTEXT 0 ///////////////////////////////////
164
165 // Make a new texture ID in GL Context 0 from the EGL Image
166 glCtx0->makeCurrent();
167 externalTexture.fTarget = GR_GL_TEXTURE_EXTERNAL;
168 externalTexture.fID = glCtx0->eglImageToExternalTexture(image);
169
170 // Wrap this texture ID in a GrTexture
171 GrBackendTextureDesc externalDesc;
172 externalDesc.fConfig = kRGBA_8888_GrPixelConfig;
173 externalDesc.fWidth = kSize;
174 externalDesc.fHeight = kSize;
175 externalDesc.fTextureHandle = reinterpret_cast<GrBackendObject>(&externalTexture);
bungeman6bd52842016-10-27 09:30:08 -0700176 sk_sp<GrTexture> externalTextureObj(
kkinnunen59319222015-11-22 23:23:53 -0800177 context0->textureProvider()->wrapBackendTexture(externalDesc));
178 if (!externalTextureObj) {
179 ERRORF(reporter, "Error wrapping external texture in GrTexture.");
Ben Wagner145dbcd2016-11-03 14:40:50 -0400180 cleanup(glCtx0, externalTexture.fID, glCtx1.get(), context1, backendTexture1, image);
kkinnunen59319222015-11-22 23:23:53 -0800181 return;
182 }
183
bsalomona98419b2015-11-23 07:09:50 -0800184 // Should not be able to wrap as a RT
185 externalDesc.fFlags = kRenderTarget_GrBackendTextureFlag;
bungeman6bd52842016-10-27 09:30:08 -0700186 sk_sp<GrTexture> externalTextureRTObj(
bsalomona98419b2015-11-23 07:09:50 -0800187 context0->textureProvider()->wrapBackendTexture(externalDesc));
188 if (externalTextureRTObj) {
189 ERRORF(reporter, "Should not be able to wrap an EXTERNAL texture as a RT.");
kkinnunen59319222015-11-22 23:23:53 -0800190 }
bsalomona98419b2015-11-23 07:09:50 -0800191 externalDesc.fFlags = kNone_GrBackendTextureFlag;
192
193 // Should not be able to wrap with a sample count
194 externalDesc.fSampleCnt = 4;
bungeman6bd52842016-10-27 09:30:08 -0700195 sk_sp<GrTexture> externalTextureMSAAObj(
bsalomona98419b2015-11-23 07:09:50 -0800196 context0->textureProvider()->wrapBackendTexture(externalDesc));
197 if (externalTextureMSAAObj) {
198 ERRORF(reporter, "Should not be able to wrap an EXTERNAL texture with MSAA.");
kkinnunen59319222015-11-22 23:23:53 -0800199 }
bsalomona98419b2015-11-23 07:09:50 -0800200 externalDesc.fSampleCnt = 0;
201
bungeman6bd52842016-10-27 09:30:08 -0700202 test_read_pixels(reporter, context0, externalTextureObj.get(), pixels.get());
bsalomona98419b2015-11-23 07:09:50 -0800203
bungeman6bd52842016-10-27 09:30:08 -0700204 test_write_pixels(reporter, context0, externalTextureObj.get());
bsalomona98419b2015-11-23 07:09:50 -0800205
bungeman6bd52842016-10-27 09:30:08 -0700206 test_copy_surface(reporter, context0, externalTextureObj.get(), pixels.get());
bsalomona98419b2015-11-23 07:09:50 -0800207
Ben Wagner145dbcd2016-11-03 14:40:50 -0400208 cleanup(glCtx0, externalTexture.fID, glCtx1.get(), context1, backendTexture1, image);
bsalomon7ea33f52015-11-22 14:51:00 -0800209}
210
211#endif