blob: ea52cabb859251858eb8c857feb24a90440806fd [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
Robert Phillips6d344c32020-07-06 10:56:46 -04008#include "include/gpu/GrDirectContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -05009#include "src/gpu/GrContextPriv.h"
Robert Phillipse19babf2020-04-06 13:57:30 -040010#include "src/gpu/GrProxyProvider.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "src/gpu/GrRenderTargetContext.h"
12#include "src/gpu/GrShaderCaps.h"
13#include "src/gpu/GrSurfacePriv.h"
Greg Daniel456f9b52020-03-05 19:14:18 +000014#include "src/gpu/GrTexture.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "src/gpu/GrTexturePriv.h"
16#include "src/gpu/GrTextureProxyPriv.h"
17#include "src/gpu/gl/GrGLGpu.h"
18#include "src/gpu/gl/GrGLUtil.h"
19#include "tests/Test.h"
20#include "tests/TestUtils.h"
21#include "tools/gpu/GrContextFactory.h"
22#include "tools/gpu/gl/GLTestContext.h"
bsalomon7ea33f52015-11-22 14:51:00 -080023
John Rosascoa9b348f2019-11-08 13:18:15 -080024#ifdef SK_GL
25
bsalomon273c0f52016-03-31 10:59:06 -070026using sk_gpu_test::GLTestContext;
bsalomon3724e572016-03-30 18:56:19 -070027
Greg Daniel02611d92017-07-25 10:05:01 -040028static void cleanup(GLTestContext* glctx0, GrGLuint texID0, GLTestContext* glctx1,
Robert Phillips0c5bb2f2020-07-17 15:40:13 -040029 sk_sp<GrDirectContext> dContext, GrBackendTexture* backendTex1,
Greg Daniel02611d92017-07-25 10:05:01 -040030 GrEGLImage image1) {
bsalomon7ea33f52015-11-22 14:51:00 -080031 if (glctx1) {
32 glctx1->makeCurrent();
Robert Phillips0c5bb2f2020-07-17 15:40:13 -040033 if (dContext) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -050034 if (backendTex1 && backendTex1->isValid()) {
Robert Phillips0c5bb2f2020-07-17 15:40:13 -040035 dContext->deleteBackendTexture(*backendTex1);
bsalomon7ea33f52015-11-22 14:51:00 -080036 }
bsalomon7ea33f52015-11-22 14:51:00 -080037 }
38 if (GR_EGL_NO_IMAGE != image1) {
39 glctx1->destroyEGLImage(image1);
40 }
bsalomon7ea33f52015-11-22 14:51:00 -080041 }
42
43 glctx0->makeCurrent();
44 if (texID0) {
45 GR_GL_CALL(glctx0->gl(), DeleteTextures(1, &texID0));
46 }
47}
48
bsalomon758586c2016-04-06 14:02:39 -070049DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(EGLImageTest, reporter, ctxInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -040050 auto context0 = ctxInfo.directContext();
bsalomon8b7451a2016-05-11 06:33:06 -070051 sk_gpu_test::GLTestContext* glCtx0 = ctxInfo.glContext();
bsalomonf2f1c172016-04-05 12:59:06 -070052
kkinnunen59319222015-11-22 23:23:53 -080053 // Try to create a second GL context and then check if the contexts have necessary
54 // extensions to run this test.
bsalomon7ea33f52015-11-22 14:51:00 -080055
kkinnunen59319222015-11-22 23:23:53 -080056 if (kGLES_GrGLStandard != glCtx0->gl()->fStandard) {
57 return;
bsalomon7ea33f52015-11-22 14:51:00 -080058 }
Robert Phillips9da87e02019-02-04 13:26:26 -050059 GrGLGpu* gpu0 = static_cast<GrGLGpu*>(context0->priv().getGpu());
Brian Salomon1edc5b92016-11-29 13:43:46 -050060 if (!gpu0->glCaps().shaderCaps()->externalTextureSupport()) {
kkinnunen59319222015-11-22 23:23:53 -080061 return;
62 }
63
Ben Wagner145dbcd2016-11-03 14:40:50 -040064 std::unique_ptr<GLTestContext> glCtx1 = glCtx0->makeNew();
kkinnunen59319222015-11-22 23:23:53 -080065 if (!glCtx1) {
66 return;
67 }
Robert Phillipsf4f80112020-07-13 16:13:31 -040068 sk_sp<GrDirectContext> context1 = GrDirectContext::MakeGL(sk_ref_sp(glCtx1->gl()));
Robert Phillipsd21b2a52017-12-12 13:01:25 -050069 GrBackendTexture backendTexture1;
kkinnunen59319222015-11-22 23:23:53 -080070 GrEGLImage image = GR_EGL_NO_IMAGE;
71 GrGLTextureInfo externalTexture;
72 externalTexture.fID = 0;
73
74 if (!context1) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -050075 cleanup(glCtx0, externalTexture.fID, glCtx1.get(), context1, &backendTexture1, image);
kkinnunen59319222015-11-22 23:23:53 -080076 return;
77 }
78
79 if (!glCtx1->gl()->hasExtension("EGL_KHR_image") ||
80 !glCtx1->gl()->hasExtension("EGL_KHR_gl_texture_2D_image")) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -050081 cleanup(glCtx0, externalTexture.fID, glCtx1.get(), context1, &backendTexture1, image);
kkinnunen59319222015-11-22 23:23:53 -080082 return;
83 }
84
85 ///////////////////////////////// CONTEXT 1 ///////////////////////////////////
86
87 // Use GL Context 1 to create a texture unknown to GrContext.
Greg Daniel0a2464f2020-05-14 15:45:44 -040088 context1->flushAndSubmit();
kkinnunen59319222015-11-22 23:23:53 -080089 static const int kSize = 100;
Greg Danielc1ad77c2020-05-06 11:40:03 -040090
91 CreateBackendTexture(context1.get(), &backendTexture1, kSize, kSize, kRGBA_8888_SkColorType,
92 SkColors::kTransparent, GrMipMapped::kNo, GrRenderable::kNo,
93 GrProtected::kNo);
Greg Daniel5366e592018-01-10 09:57:53 -050094
Robert Phillips7c8af172019-07-08 15:55:24 -040095 if (!backendTexture1.isValid()) {
kkinnunen59319222015-11-22 23:23:53 -080096 ERRORF(reporter, "Error creating texture for EGL Image");
Robert Phillipsd21b2a52017-12-12 13:01:25 -050097 cleanup(glCtx0, externalTexture.fID, glCtx1.get(), context1, &backendTexture1, image);
kkinnunen59319222015-11-22 23:23:53 -080098 return;
99 }
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500100
Greg Daniel52e16d92018-04-10 09:34:07 -0400101 GrGLTextureInfo texInfo;
102 if (!backendTexture1.getGLTextureInfo(&texInfo)) {
103 ERRORF(reporter, "Failed to get GrGLTextureInfo");
104 return;
105 }
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500106
Greg Daniel52e16d92018-04-10 09:34:07 -0400107 if (GR_GL_TEXTURE_2D != texInfo.fTarget) {
kkinnunen59319222015-11-22 23:23:53 -0800108 ERRORF(reporter, "Expected backend texture to be 2D");
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500109 cleanup(glCtx0, externalTexture.fID, glCtx1.get(), context1, &backendTexture1, image);
kkinnunen59319222015-11-22 23:23:53 -0800110 return;
111 }
112
113 // Wrap the texture in an EGLImage
Greg Daniel52e16d92018-04-10 09:34:07 -0400114 image = glCtx1->texture2DToEGLImage(texInfo.fID);
kkinnunen59319222015-11-22 23:23:53 -0800115 if (GR_EGL_NO_IMAGE == image) {
116 ERRORF(reporter, "Error creating EGL Image from texture");
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500117 cleanup(glCtx0, externalTexture.fID, glCtx1.get(), context1, &backendTexture1, image);
kkinnunen59319222015-11-22 23:23:53 -0800118 return;
119 }
120
Greg Daniel261b8aa2017-10-23 09:37:36 -0400121 // Since we are dealing with two different GL contexts here, we need to call finish so that the
122 // clearing of the texture that happens in createTextingOnlyBackendTexture occurs before we call
123 // TexSubImage below on the other context. Otherwise, it is possible the calls get reordered and
124 // the clearing overwrites the TexSubImage writes.
125 GR_GL_CALL(glCtx1->gl(), Finish());
126
kkinnunen59319222015-11-22 23:23:53 -0800127 // Populate the texture using GL context 1. Important to use TexSubImage as TexImage orphans
128 // the EGL image. Also, this must be done after creating the EGLImage as the texture
129 // contents may not be preserved when the image is created.
130 SkAutoTMalloc<uint32_t> pixels(kSize * kSize);
131 for (int i = 0; i < kSize*kSize; ++i) {
132 pixels.get()[i] = 0xDDAABBCC;
133 }
134 GR_GL_CALL(glCtx1->gl(), ActiveTexture(GR_GL_TEXTURE0));
Greg Daniel52e16d92018-04-10 09:34:07 -0400135 GR_GL_CALL(glCtx1->gl(), BindTexture(texInfo.fTarget, texInfo.fID));
136 GR_GL_CALL(glCtx1->gl(), TexSubImage2D(texInfo.fTarget, 0, 0, 0, kSize, kSize,
kkinnunen59319222015-11-22 23:23:53 -0800137 GR_GL_RGBA, GR_GL_UNSIGNED_BYTE, pixels.get()));
138 GR_GL_CALL(glCtx1->gl(), Finish());
139 // We've been making direct GL calls in GL context 1, let GrContext 1 know its internal
140 // state is invalid.
141 context1->resetContext();
142
143 ///////////////////////////////// CONTEXT 0 ///////////////////////////////////
144
145 // Make a new texture ID in GL Context 0 from the EGL Image
146 glCtx0->makeCurrent();
147 externalTexture.fTarget = GR_GL_TEXTURE_EXTERNAL;
148 externalTexture.fID = glCtx0->eglImageToExternalTexture(image);
Greg Daniel2b5be0a2019-06-13 16:50:49 -0400149 externalTexture.fFormat = GR_GL_RGBA8;
Robert Phillipsfee2b4e2017-06-08 18:22:53 -0400150 if (0 == externalTexture.fID) {
151 ERRORF(reporter, "Error converting EGL Image back to texture");
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500152 cleanup(glCtx0, externalTexture.fID, glCtx1.get(), context1, &backendTexture1, image);
Robert Phillipsfee2b4e2017-06-08 18:22:53 -0400153 return;
154 }
kkinnunen59319222015-11-22 23:23:53 -0800155
156 // Wrap this texture ID in a GrTexture
Greg Daniel108bb232018-07-03 16:18:29 -0400157 GrBackendTexture backendTex(kSize, kSize, GrMipMapped::kNo, externalTexture);
Robert Phillipsd46697a2017-01-25 12:10:37 -0500158
Greg Danielbfa19c42019-12-19 16:41:40 -0500159 GrColorType colorType = GrColorType::kRGBA_8888;
160 SkAlphaType alphaType = kPremul_SkAlphaType;
Greg Daniel7ef28f32017-04-20 16:41:55 +0000161 // TODO: If I make this TopLeft origin to match resolve_origin calls for kDefault, this test
162 // fails on the Nexus5. Why?
Greg Danielbfa19c42019-12-19 16:41:40 -0500163 GrSurfaceOrigin origin = kBottomLeft_GrSurfaceOrigin;
164 sk_sp<GrSurfaceProxy> texProxy = context0->priv().proxyProvider()->wrapBackendTexture(
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400165 backendTex, kBorrow_GrWrapOwnership, GrWrapCacheable::kNo, kRW_GrIOType);
Greg Danielbfa19c42019-12-19 16:41:40 -0500166 if (!texProxy) {
167 ERRORF(reporter, "Error wrapping external texture in GrTextureProxy.");
168 cleanup(glCtx0, externalTexture.fID, glCtx1.get(), context1, &backendTexture1, image);
169 return;
170 }
Greg Danield2ccbb52020-02-05 10:45:39 -0500171 GrSwizzle swizzle =
172 context0->priv().caps()->getReadSwizzle(texProxy->backendFormat(), colorType);
173 GrSurfaceProxyView view(std::move(texProxy), origin, swizzle);
174 auto surfaceContext =
175 GrSurfaceContext::Make(context0, std::move(view), colorType, alphaType, nullptr);
Robert Phillipsd46697a2017-01-25 12:10:37 -0500176
Robert Phillips26caf892017-01-27 10:58:31 -0500177 if (!surfaceContext) {
178 ERRORF(reporter, "Error wrapping external texture in GrSurfaceContext.");
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500179 cleanup(glCtx0, externalTexture.fID, glCtx1.get(), context1, &backendTexture1, image);
Robert Phillips26caf892017-01-27 10:58:31 -0500180 return;
kkinnunen59319222015-11-22 23:23:53 -0800181 }
182
Robert Phillipsabf7b762018-03-21 12:13:37 -0400183 GrTextureProxy* proxy = surfaceContext->asTextureProxy();
Greg Daniel09c94002018-06-08 22:11:51 +0000184 REPORTER_ASSERT(reporter, proxy->mipMapped() == GrMipMapped::kNo);
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400185 REPORTER_ASSERT(reporter, proxy->peekTexture()->texturePriv().mipMapped() == GrMipMapped::kNo);
Robert Phillipsabf7b762018-03-21 12:13:37 -0400186
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400187 REPORTER_ASSERT(reporter, proxy->textureType() == GrTextureType::kExternal);
188 REPORTER_ASSERT(reporter,
189 proxy->peekTexture()->texturePriv().textureType() == GrTextureType::kExternal);
190 REPORTER_ASSERT(reporter, proxy->hasRestrictedSampling());
191 REPORTER_ASSERT(reporter, proxy->peekTexture()->texturePriv().hasRestrictedSampling());
Robert Phillipsabf7b762018-03-21 12:13:37 -0400192
bsalomona98419b2015-11-23 07:09:50 -0800193 // Should not be able to wrap as a RT
Robert Phillipsd46697a2017-01-25 12:10:37 -0500194 {
Greg Danielba0ff782020-01-07 15:42:57 -0500195 auto temp = GrRenderTargetContext::MakeFromBackendTexture(
Robert Phillipsa1121332020-06-29 13:05:29 -0400196 context0, colorType, nullptr, backendTex, 1, origin, nullptr, nullptr);
Robert Phillips26caf892017-01-27 10:58:31 -0500197 if (temp) {
Robert Phillipsd46697a2017-01-25 12:10:37 -0500198 ERRORF(reporter, "Should not be able to wrap an EXTERNAL texture as a RT.");
199 }
kkinnunen59319222015-11-22 23:23:53 -0800200 }
bsalomona98419b2015-11-23 07:09:50 -0800201
Brian Salomon28a8f282019-10-24 20:07:39 -0400202 TestReadPixels(reporter, surfaceContext.get(), pixels.get(), "EGLImageTest-read");
bsalomona98419b2015-11-23 07:09:50 -0800203
Robert Phillips3500b772017-01-27 10:11:42 -0500204 // We should not be able to write to a EXTERNAL texture
Brian Salomon28a8f282019-10-24 20:07:39 -0400205 TestWritePixels(reporter, surfaceContext.get(), false, "EGLImageTest-write");
bsalomona98419b2015-11-23 07:09:50 -0800206
Robert Phillips3500b772017-01-27 10:11:42 -0500207 // Only test RT-config
208 // TODO: why do we always need to draw to copy from an external texture?
Brian Salomon28a8f282019-10-24 20:07:39 -0400209 TestCopyFromSurface(reporter, context0, surfaceContext->asSurfaceProxy(),
Greg Daniel40903af2020-01-30 14:55:05 -0500210 surfaceContext->origin(), colorType, pixels.get(), "EGLImageTest-copy");
bsalomona98419b2015-11-23 07:09:50 -0800211
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500212 cleanup(glCtx0, externalTexture.fID, glCtx1.get(), context1, &backendTexture1, image);
bsalomon7ea33f52015-11-22 14:51:00 -0800213}
John Rosascoa9b348f2019-11-08 13:18:15 -0800214
215#endif // SK_GL