1 /*
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 #include "TestUtils.h"
10 #include "GrContext.h"
11 #include "GrContextFactory.h"
12 #include "GrContextPriv.h"
13 #include "GrRenderTargetContext.h"
14 #include "GrShaderCaps.h"
15 #include "GrSurfacePriv.h"
16 #include "GrTexture.h"
17 #include "GrTextureContext.h"
18 #include "GrTexturePriv.h"
19 #include "GrTextureProxyPriv.h"
20 #include "gl/GLTestContext.h"
21 #include "gl/GrGLGpu.h"
22 #include "gl/GrGLUtil.h"
23 
24 using sk_gpu_test::GLTestContext;
25 
cleanup(GLTestContext * glctx0,GrGLuint texID0,GLTestContext * glctx1,sk_sp<GrContext> grctx1,GrBackendTexture * backendTex1,GrEGLImage image1)26 static void cleanup(GLTestContext* glctx0, GrGLuint texID0, GLTestContext* glctx1,
27                     sk_sp<GrContext> grctx1, GrBackendTexture* backendTex1,
28                     GrEGLImage image1) {
29     if (glctx1) {
30         glctx1->makeCurrent();
31         if (grctx1) {
32             if (backendTex1 && backendTex1->isValid()) {
33                 GrGLGpu* gpu1 = static_cast<GrGLGpu*>(grctx1->contextPriv().getGpu());
34                 gpu1->deleteTestingOnlyBackendTexture(*backendTex1);
35             }
36         }
37         if (GR_EGL_NO_IMAGE != image1) {
38             glctx1->destroyEGLImage(image1);
39         }
40     }
41 
42     glctx0->makeCurrent();
43     if (texID0) {
44         GR_GL_CALL(glctx0->gl(), DeleteTextures(1, &texID0));
45     }
46 }
47 
DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(EGLImageTest,reporter,ctxInfo)48 DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(EGLImageTest, reporter, ctxInfo) {
49     GrContext* context0 = ctxInfo.grContext();
50     sk_gpu_test::GLTestContext* glCtx0 = ctxInfo.glContext();
51 
52     // Try to create a second GL context and then check if the contexts have necessary
53     // extensions to run this test.
54 
55     if (kGLES_GrGLStandard != glCtx0->gl()->fStandard) {
56         return;
57     }
58     GrGLGpu* gpu0 = static_cast<GrGLGpu*>(context0->contextPriv().getGpu());
59     if (!gpu0->glCaps().shaderCaps()->externalTextureSupport()) {
60         return;
61     }
62 
63     std::unique_ptr<GLTestContext> glCtx1 = glCtx0->makeNew();
64     if (!glCtx1) {
65         return;
66     }
67     sk_sp<GrContext> context1 = GrContext::MakeGL(sk_ref_sp(glCtx1->gl()));
68     GrBackendTexture backendTexture1;
69     GrEGLImage image = GR_EGL_NO_IMAGE;
70     GrGLTextureInfo externalTexture;
71     externalTexture.fID = 0;
72 
73     if (!context1) {
74         cleanup(glCtx0, externalTexture.fID, glCtx1.get(), context1, &backendTexture1, image);
75         return;
76     }
77 
78     if (!glCtx1->gl()->hasExtension("EGL_KHR_image") ||
79         !glCtx1->gl()->hasExtension("EGL_KHR_gl_texture_2D_image")) {
80         cleanup(glCtx0, externalTexture.fID, glCtx1.get(), context1, &backendTexture1, image);
81         return;
82     }
83 
84     ///////////////////////////////// CONTEXT 1 ///////////////////////////////////
85 
86     // Use GL Context 1 to create a texture unknown to GrContext.
87     context1->flush();
88     GrGpu* gpu1 = context1->contextPriv().getGpu();
89     static const int kSize = 100;
90     backendTexture1 =
91         gpu1->createTestingOnlyBackendTexture(nullptr, kSize, kSize, GrColorType::kRGBA_8888,
92                                               false, GrMipMapped::kNo);
93 
94     if (!backendTexture1.isValid() || !gpu1->isTestingOnlyBackendTexture(backendTexture1)) {
95         ERRORF(reporter, "Error creating texture for EGL Image");
96         cleanup(glCtx0, externalTexture.fID, glCtx1.get(), context1, &backendTexture1, image);
97         return;
98     }
99 
100     GrGLTextureInfo texInfo;
101     if (!backendTexture1.getGLTextureInfo(&texInfo)) {
102         ERRORF(reporter, "Failed to get GrGLTextureInfo");
103         return;
104     }
105 
106     if (GR_GL_TEXTURE_2D != texInfo.fTarget) {
107         ERRORF(reporter, "Expected backend texture to be 2D");
108         cleanup(glCtx0, externalTexture.fID, glCtx1.get(), context1, &backendTexture1, image);
109         return;
110     }
111 
112     // Wrap the texture in an EGLImage
113     image = glCtx1->texture2DToEGLImage(texInfo.fID);
114     if (GR_EGL_NO_IMAGE == image) {
115         ERRORF(reporter, "Error creating EGL Image from texture");
116         cleanup(glCtx0, externalTexture.fID, glCtx1.get(), context1, &backendTexture1, image);
117         return;
118     }
119 
120     // Since we are dealing with two different GL contexts here, we need to call finish so that the
121     // clearing of the texture that happens in createTextingOnlyBackendTexture occurs before we call
122     // TexSubImage below on the other context. Otherwise, it is possible the calls get reordered and
123     // the clearing overwrites the TexSubImage writes.
124     GR_GL_CALL(glCtx1->gl(), Finish());
125 
126     // Populate the texture using GL context 1. Important to use TexSubImage as TexImage orphans
127     // the EGL image. Also, this must be done after creating the EGLImage as the texture
128     // contents may not be preserved when the image is created.
129     SkAutoTMalloc<uint32_t> pixels(kSize * kSize);
130     for (int i = 0; i < kSize*kSize; ++i) {
131         pixels.get()[i] = 0xDDAABBCC;
132     }
133     GR_GL_CALL(glCtx1->gl(), ActiveTexture(GR_GL_TEXTURE0));
134     GR_GL_CALL(glCtx1->gl(), BindTexture(texInfo.fTarget, texInfo.fID));
135     GR_GL_CALL(glCtx1->gl(), TexSubImage2D(texInfo.fTarget, 0, 0, 0, kSize, kSize,
136                                            GR_GL_RGBA, GR_GL_UNSIGNED_BYTE, pixels.get()));
137     GR_GL_CALL(glCtx1->gl(), Finish());
138     // We've been making direct GL calls in GL context 1, let GrContext 1 know its internal
139     // state is invalid.
140     context1->resetContext();
141 
142     ///////////////////////////////// CONTEXT 0 ///////////////////////////////////
143 
144     // Make a new texture ID in GL Context 0 from the EGL Image
145     glCtx0->makeCurrent();
146     externalTexture.fTarget = GR_GL_TEXTURE_EXTERNAL;
147     externalTexture.fID = glCtx0->eglImageToExternalTexture(image);
148     if (0 == externalTexture.fID) {
149         ERRORF(reporter, "Error converting EGL Image back to texture");
150         cleanup(glCtx0, externalTexture.fID, glCtx1.get(), context1, &backendTexture1, image);
151         return;
152     }
153 
154     // Wrap this texture ID in a GrTexture
155     GrBackendTexture backendTex(kSize, kSize, GrMipMapped::kNo, externalTexture);
156     backendTex.setPixelConfig(kRGBA_8888_GrPixelConfig);
157 
158     // TODO: If I make this TopLeft origin to match resolve_origin calls for kDefault, this test
159     // fails on the Nexus5. Why?
160     sk_sp<GrTextureContext> surfaceContext = context0->contextPriv().makeBackendTextureContext(
161             backendTex, kBottomLeft_GrSurfaceOrigin, nullptr);
162 
163     if (!surfaceContext) {
164         ERRORF(reporter, "Error wrapping external texture in GrSurfaceContext.");
165         cleanup(glCtx0, externalTexture.fID, glCtx1.get(), context1, &backendTexture1, image);
166         return;
167     }
168 
169     GrTextureProxy* proxy = surfaceContext->asTextureProxy();
170     REPORTER_ASSERT(reporter, proxy->mipMapped() == GrMipMapped::kNo);
171     REPORTER_ASSERT(reporter, proxy->peekTexture()->texturePriv().mipMapped() == GrMipMapped::kNo);
172 
173     REPORTER_ASSERT(reporter, proxy->textureType() == GrTextureType::kExternal);
174     REPORTER_ASSERT(reporter,
175                     proxy->peekTexture()->texturePriv().textureType() == GrTextureType::kExternal);
176     REPORTER_ASSERT(reporter, proxy->hasRestrictedSampling());
177     REPORTER_ASSERT(reporter, proxy->peekTexture()->texturePriv().hasRestrictedSampling());
178 
179     // Should not be able to wrap as a RT
180     {
181         sk_sp<GrRenderTargetContext> temp =
182                 context0->contextPriv().makeBackendTextureRenderTargetContext(
183                         backendTex, kBottomLeft_GrSurfaceOrigin, 1, nullptr);
184         if (temp) {
185             ERRORF(reporter, "Should not be able to wrap an EXTERNAL texture as a RT.");
186         }
187     }
188 
189     test_read_pixels(reporter, surfaceContext.get(), pixels.get(), "EGLImageTest-read");
190 
191     // We should not be able to write to a EXTERNAL texture
192     test_write_pixels(reporter, surfaceContext.get(), false, "EGLImageTest-write");
193 
194     // Only test RT-config
195     // TODO: why do we always need to draw to copy from an external texture?
196     test_copy_from_surface(reporter, context0, surfaceContext->asSurfaceProxy(),
197                            pixels.get(), true, "EGLImageTest-copy");
198 
199     cleanup(glCtx0, externalTexture.fID, glCtx1.get(), context1, &backendTexture1, image);
200 }
201