blob: 6301c51c8271c954e5109f9601adde5b7031b191 [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"
12#include "gl/GrGLGpu.h"
13#include "gl/GrGLUtil.h"
bsalomon273c0f52016-03-31 10:59:06 -070014#include "gl/GLTestContext.h"
bsalomon7ea33f52015-11-22 14:51:00 -080015
bsalomon273c0f52016-03-31 10:59:06 -070016using sk_gpu_test::GLTestContext;
bsalomon3724e572016-03-30 18:56:19 -070017
bsalomon273c0f52016-03-31 10:59:06 -070018static void cleanup(GLTestContext* glctx0, GrGLuint texID0, GLTestContext* glctx1, GrContext* grctx1,
bsalomon7ea33f52015-11-22 14:51:00 -080019 const GrGLTextureInfo* grbackendtex1, GrEGLImage image1) {
20 if (glctx1) {
21 glctx1->makeCurrent();
22 if (grctx1) {
23 if (grbackendtex1) {
24 GrGLGpu* gpu1 = static_cast<GrGLGpu*>(grctx1->getGpu());
25 GrBackendObject handle = reinterpret_cast<GrBackendObject>(grbackendtex1);
26 gpu1->deleteTestingOnlyBackendTexture(handle, false);
27 }
28 grctx1->unref();
29 }
30 if (GR_EGL_NO_IMAGE != image1) {
31 glctx1->destroyEGLImage(image1);
32 }
bsalomon7ea33f52015-11-22 14:51:00 -080033 }
34
35 glctx0->makeCurrent();
36 if (texID0) {
37 GR_GL_CALL(glctx0->gl(), DeleteTextures(1, &texID0));
38 }
39}
40
bsalomona98419b2015-11-23 07:09:50 -080041static void test_read_pixels(skiatest::Reporter* reporter, GrContext* context,
42 GrTexture* externalTexture, uint32_t expectedPixelValues[]) {
43 int pixelCnt = externalTexture->width() * externalTexture->height();
44 SkAutoTMalloc<uint32_t> pixels(pixelCnt);
45 memset(pixels.get(), 0, sizeof(uint32_t)*pixelCnt);
46 bool read = externalTexture->readPixels(0, 0, externalTexture->width(),
47 externalTexture->height(), kRGBA_8888_GrPixelConfig,
48 pixels.get());
49 if (!read) {
50 ERRORF(reporter, "Error reading external texture.");
51 }
52 for (int i = 0; i < pixelCnt; ++i) {
53 if (pixels.get()[i] != expectedPixelValues[i]) {
54 ERRORF(reporter, "Error, external texture pixel value %d should be 0x%08x,"
55 " got 0x%08x.", i, expectedPixelValues[i], pixels.get()[i]);
56 break;
57 }
58 }
59}
60
61static void test_write_pixels(skiatest::Reporter* reporter, GrContext* context,
62 GrTexture* externalTexture) {
63 int pixelCnt = externalTexture->width() * externalTexture->height();
64 SkAutoTMalloc<uint32_t> pixels(pixelCnt);
65 memset(pixels.get(), 0, sizeof(uint32_t)*pixelCnt);
66 bool write = externalTexture->writePixels(0, 0, 0, 0, kRGBA_8888_GrPixelConfig, pixels.get());
67 REPORTER_ASSERT_MESSAGE(reporter, !write, "Should not be able to write to a EXTERNAL"
68 " texture.");
69}
70
71static void test_copy_surface(skiatest::Reporter* reporter, GrContext* context,
72 GrTexture* externalTexture, uint32_t expectedPixelValues[]) {
73 GrSurfaceDesc copyDesc;
74 copyDesc.fConfig = kRGBA_8888_GrPixelConfig;
75 copyDesc.fWidth = externalTexture->width();
76 copyDesc.fHeight = externalTexture->height();
77 copyDesc.fFlags = kRenderTarget_GrSurfaceFlag;
Hal Canary342b7ac2016-11-04 11:49:42 -040078 sk_sp<GrTexture> copy(context->textureProvider()->createTexture(copyDesc, SkBudgeted::kYes));
79 context->copySurface(copy.get(), externalTexture);
80 test_read_pixels(reporter, context, copy.get(), expectedPixelValues);
bsalomona98419b2015-11-23 07:09:50 -080081}
82
bsalomon758586c2016-04-06 14:02:39 -070083DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(EGLImageTest, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -070084 GrContext* context0 = ctxInfo.grContext();
85 sk_gpu_test::GLTestContext* glCtx0 = ctxInfo.glContext();
bsalomonf2f1c172016-04-05 12:59:06 -070086
kkinnunen59319222015-11-22 23:23:53 -080087 // Try to create a second GL context and then check if the contexts have necessary
88 // extensions to run this test.
bsalomon7ea33f52015-11-22 14:51:00 -080089
kkinnunen59319222015-11-22 23:23:53 -080090 if (kGLES_GrGLStandard != glCtx0->gl()->fStandard) {
91 return;
bsalomon7ea33f52015-11-22 14:51:00 -080092 }
kkinnunen59319222015-11-22 23:23:53 -080093 GrGLGpu* gpu0 = static_cast<GrGLGpu*>(context0->getGpu());
cdalton9c3f1432016-03-11 10:07:37 -080094 if (!gpu0->glCaps().glslCaps()->externalTextureSupport()) {
kkinnunen59319222015-11-22 23:23:53 -080095 return;
96 }
97
Ben Wagner145dbcd2016-11-03 14:40:50 -040098 std::unique_ptr<GLTestContext> glCtx1 = glCtx0->makeNew();
kkinnunen59319222015-11-22 23:23:53 -080099 if (!glCtx1) {
100 return;
101 }
102 GrContext* context1 = GrContext::Create(kOpenGL_GrBackend, (GrBackendContext)glCtx1->gl());
103 const GrGLTextureInfo* backendTexture1 = nullptr;
104 GrEGLImage image = GR_EGL_NO_IMAGE;
105 GrGLTextureInfo externalTexture;
106 externalTexture.fID = 0;
107
108 if (!context1) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400109 cleanup(glCtx0, externalTexture.fID, glCtx1.get(), context1, backendTexture1, image);
kkinnunen59319222015-11-22 23:23:53 -0800110 return;
111 }
112
113 if (!glCtx1->gl()->hasExtension("EGL_KHR_image") ||
114 !glCtx1->gl()->hasExtension("EGL_KHR_gl_texture_2D_image")) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400115 cleanup(glCtx0, externalTexture.fID, glCtx1.get(), context1, backendTexture1, image);
kkinnunen59319222015-11-22 23:23:53 -0800116 return;
117 }
118
119 ///////////////////////////////// CONTEXT 1 ///////////////////////////////////
120
121 // Use GL Context 1 to create a texture unknown to GrContext.
122 context1->flush();
123 GrGpu* gpu1 = context1->getGpu();
124 static const int kSize = 100;
125 backendTexture1 = reinterpret_cast<const GrGLTextureInfo*>(
126 gpu1->createTestingOnlyBackendTexture(nullptr, kSize, kSize, kRGBA_8888_GrPixelConfig));
127 if (!backendTexture1 || !backendTexture1->fID) {
128 ERRORF(reporter, "Error creating texture for EGL Image");
Ben Wagner145dbcd2016-11-03 14:40:50 -0400129 cleanup(glCtx0, externalTexture.fID, glCtx1.get(), context1, backendTexture1, image);
kkinnunen59319222015-11-22 23:23:53 -0800130 return;
131 }
132 if (GR_GL_TEXTURE_2D != backendTexture1->fTarget) {
133 ERRORF(reporter, "Expected backend texture to be 2D");
Ben Wagner145dbcd2016-11-03 14:40:50 -0400134 cleanup(glCtx0, externalTexture.fID, glCtx1.get(), context1, backendTexture1, image);
kkinnunen59319222015-11-22 23:23:53 -0800135 return;
136 }
137
138 // Wrap the texture in an EGLImage
139 image = glCtx1->texture2DToEGLImage(backendTexture1->fID);
140 if (GR_EGL_NO_IMAGE == image) {
141 ERRORF(reporter, "Error creating EGL Image from texture");
Ben Wagner145dbcd2016-11-03 14:40:50 -0400142 cleanup(glCtx0, externalTexture.fID, glCtx1.get(), context1, backendTexture1, image);
kkinnunen59319222015-11-22 23:23:53 -0800143 return;
144 }
145
146 // Populate the texture using GL context 1. Important to use TexSubImage as TexImage orphans
147 // the EGL image. Also, this must be done after creating the EGLImage as the texture
148 // contents may not be preserved when the image is created.
149 SkAutoTMalloc<uint32_t> pixels(kSize * kSize);
150 for (int i = 0; i < kSize*kSize; ++i) {
151 pixels.get()[i] = 0xDDAABBCC;
152 }
153 GR_GL_CALL(glCtx1->gl(), ActiveTexture(GR_GL_TEXTURE0));
154 GR_GL_CALL(glCtx1->gl(), BindTexture(backendTexture1->fTarget, backendTexture1->fID));
155 GR_GL_CALL(glCtx1->gl(), TexSubImage2D(backendTexture1->fTarget, 0, 0, 0, kSize, kSize,
156 GR_GL_RGBA, GR_GL_UNSIGNED_BYTE, pixels.get()));
157 GR_GL_CALL(glCtx1->gl(), Finish());
158 // We've been making direct GL calls in GL context 1, let GrContext 1 know its internal
159 // state is invalid.
160 context1->resetContext();
161
162 ///////////////////////////////// CONTEXT 0 ///////////////////////////////////
163
164 // Make a new texture ID in GL Context 0 from the EGL Image
165 glCtx0->makeCurrent();
166 externalTexture.fTarget = GR_GL_TEXTURE_EXTERNAL;
167 externalTexture.fID = glCtx0->eglImageToExternalTexture(image);
168
169 // Wrap this texture ID in a GrTexture
170 GrBackendTextureDesc externalDesc;
171 externalDesc.fConfig = kRGBA_8888_GrPixelConfig;
172 externalDesc.fWidth = kSize;
173 externalDesc.fHeight = kSize;
174 externalDesc.fTextureHandle = reinterpret_cast<GrBackendObject>(&externalTexture);
bungeman6bd52842016-10-27 09:30:08 -0700175 sk_sp<GrTexture> externalTextureObj(
kkinnunen59319222015-11-22 23:23:53 -0800176 context0->textureProvider()->wrapBackendTexture(externalDesc));
177 if (!externalTextureObj) {
178 ERRORF(reporter, "Error wrapping external texture in GrTexture.");
Ben Wagner145dbcd2016-11-03 14:40:50 -0400179 cleanup(glCtx0, externalTexture.fID, glCtx1.get(), context1, backendTexture1, image);
kkinnunen59319222015-11-22 23:23:53 -0800180 return;
181 }
182
bsalomona98419b2015-11-23 07:09:50 -0800183 // Should not be able to wrap as a RT
184 externalDesc.fFlags = kRenderTarget_GrBackendTextureFlag;
bungeman6bd52842016-10-27 09:30:08 -0700185 sk_sp<GrTexture> externalTextureRTObj(
bsalomona98419b2015-11-23 07:09:50 -0800186 context0->textureProvider()->wrapBackendTexture(externalDesc));
187 if (externalTextureRTObj) {
188 ERRORF(reporter, "Should not be able to wrap an EXTERNAL texture as a RT.");
kkinnunen59319222015-11-22 23:23:53 -0800189 }
bsalomona98419b2015-11-23 07:09:50 -0800190 externalDesc.fFlags = kNone_GrBackendTextureFlag;
191
192 // Should not be able to wrap with a sample count
193 externalDesc.fSampleCnt = 4;
bungeman6bd52842016-10-27 09:30:08 -0700194 sk_sp<GrTexture> externalTextureMSAAObj(
bsalomona98419b2015-11-23 07:09:50 -0800195 context0->textureProvider()->wrapBackendTexture(externalDesc));
196 if (externalTextureMSAAObj) {
197 ERRORF(reporter, "Should not be able to wrap an EXTERNAL texture with MSAA.");
kkinnunen59319222015-11-22 23:23:53 -0800198 }
bsalomona98419b2015-11-23 07:09:50 -0800199 externalDesc.fSampleCnt = 0;
200
bungeman6bd52842016-10-27 09:30:08 -0700201 test_read_pixels(reporter, context0, externalTextureObj.get(), pixels.get());
bsalomona98419b2015-11-23 07:09:50 -0800202
bungeman6bd52842016-10-27 09:30:08 -0700203 test_write_pixels(reporter, context0, externalTextureObj.get());
bsalomona98419b2015-11-23 07:09:50 -0800204
bungeman6bd52842016-10-27 09:30:08 -0700205 test_copy_surface(reporter, context0, externalTextureObj.get(), pixels.get());
bsalomona98419b2015-11-23 07:09:50 -0800206
Ben Wagner145dbcd2016-11-03 14:40:50 -0400207 cleanup(glCtx0, externalTexture.fID, glCtx1.get(), context1, backendTexture1, image);
bsalomon7ea33f52015-11-22 14:51:00 -0800208}
209
210#endif