blob: 784ec5d99eefe2864a279f4bd36dd8c6a9e24bf8 [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"
Robert Phillips3500b772017-01-27 10:11:42 -05009#include "TestUtils.h"
bsalomon7ea33f52015-11-22 14:51:00 -080010#if SK_SUPPORT_GPU
11#include "GrContext.h"
12#include "GrContextFactory.h"
Brian Salomond17f6582017-07-19 18:28:58 -040013#include "GrContextPriv.h"
14#include "GrRenderTargetContext.h"
Brian Salomon94efbf52016-11-29 13:43:05 -050015#include "GrShaderCaps.h"
Greg Daniel7ef28f32017-04-20 16:41:55 +000016#include "GrTest.h"
Brian Salomond17f6582017-07-19 18:28:58 -040017#include "GrTextureContext.h"
18#include "gl/GLTestContext.h"
bsalomon7ea33f52015-11-22 14:51:00 -080019#include "gl/GrGLGpu.h"
20#include "gl/GrGLUtil.h"
bsalomon7ea33f52015-11-22 14:51:00 -080021
bsalomon273c0f52016-03-31 10:59:06 -070022using sk_gpu_test::GLTestContext;
bsalomon3724e572016-03-30 18:56:19 -070023
Greg Daniel02611d92017-07-25 10:05:01 -040024static void cleanup(GLTestContext* glctx0, GrGLuint texID0, GLTestContext* glctx1,
25 sk_sp<GrContext> grctx1, const GrGLTextureInfo* grbackendtex1,
26 GrEGLImage image1) {
bsalomon7ea33f52015-11-22 14:51:00 -080027 if (glctx1) {
28 glctx1->makeCurrent();
29 if (grctx1) {
30 if (grbackendtex1) {
31 GrGLGpu* gpu1 = static_cast<GrGLGpu*>(grctx1->getGpu());
32 GrBackendObject handle = reinterpret_cast<GrBackendObject>(grbackendtex1);
33 gpu1->deleteTestingOnlyBackendTexture(handle, false);
34 }
bsalomon7ea33f52015-11-22 14:51:00 -080035 }
36 if (GR_EGL_NO_IMAGE != image1) {
37 glctx1->destroyEGLImage(image1);
38 }
bsalomon7ea33f52015-11-22 14:51:00 -080039 }
40
41 glctx0->makeCurrent();
42 if (texID0) {
43 GR_GL_CALL(glctx0->gl(), DeleteTextures(1, &texID0));
44 }
45}
46
bsalomon758586c2016-04-06 14:02:39 -070047DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(EGLImageTest, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -070048 GrContext* context0 = ctxInfo.grContext();
49 sk_gpu_test::GLTestContext* glCtx0 = ctxInfo.glContext();
bsalomonf2f1c172016-04-05 12:59:06 -070050
kkinnunen59319222015-11-22 23:23:53 -080051 // Try to create a second GL context and then check if the contexts have necessary
52 // extensions to run this test.
bsalomon7ea33f52015-11-22 14:51:00 -080053
kkinnunen59319222015-11-22 23:23:53 -080054 if (kGLES_GrGLStandard != glCtx0->gl()->fStandard) {
55 return;
bsalomon7ea33f52015-11-22 14:51:00 -080056 }
kkinnunen59319222015-11-22 23:23:53 -080057 GrGLGpu* gpu0 = static_cast<GrGLGpu*>(context0->getGpu());
Brian Salomon1edc5b92016-11-29 13:43:46 -050058 if (!gpu0->glCaps().shaderCaps()->externalTextureSupport()) {
kkinnunen59319222015-11-22 23:23:53 -080059 return;
60 }
61
Ben Wagner145dbcd2016-11-03 14:40:50 -040062 std::unique_ptr<GLTestContext> glCtx1 = glCtx0->makeNew();
kkinnunen59319222015-11-22 23:23:53 -080063 if (!glCtx1) {
64 return;
65 }
Greg Daniel02611d92017-07-25 10:05:01 -040066 sk_sp<GrContext> context1 = GrContext::MakeGL(glCtx1->gl());
kkinnunen59319222015-11-22 23:23:53 -080067 const GrGLTextureInfo* backendTexture1 = nullptr;
68 GrEGLImage image = GR_EGL_NO_IMAGE;
69 GrGLTextureInfo externalTexture;
70 externalTexture.fID = 0;
71
72 if (!context1) {
Ben Wagner145dbcd2016-11-03 14:40:50 -040073 cleanup(glCtx0, externalTexture.fID, glCtx1.get(), context1, backendTexture1, image);
kkinnunen59319222015-11-22 23:23:53 -080074 return;
75 }
76
77 if (!glCtx1->gl()->hasExtension("EGL_KHR_image") ||
78 !glCtx1->gl()->hasExtension("EGL_KHR_gl_texture_2D_image")) {
Ben Wagner145dbcd2016-11-03 14:40:50 -040079 cleanup(glCtx0, externalTexture.fID, glCtx1.get(), context1, backendTexture1, image);
kkinnunen59319222015-11-22 23:23:53 -080080 return;
81 }
82
83 ///////////////////////////////// CONTEXT 1 ///////////////////////////////////
84
85 // Use GL Context 1 to create a texture unknown to GrContext.
86 context1->flush();
87 GrGpu* gpu1 = context1->getGpu();
88 static const int kSize = 100;
89 backendTexture1 = reinterpret_cast<const GrGLTextureInfo*>(
90 gpu1->createTestingOnlyBackendTexture(nullptr, kSize, kSize, kRGBA_8888_GrPixelConfig));
91 if (!backendTexture1 || !backendTexture1->fID) {
92 ERRORF(reporter, "Error creating texture for EGL Image");
Ben Wagner145dbcd2016-11-03 14:40:50 -040093 cleanup(glCtx0, externalTexture.fID, glCtx1.get(), context1, backendTexture1, image);
kkinnunen59319222015-11-22 23:23:53 -080094 return;
95 }
96 if (GR_GL_TEXTURE_2D != backendTexture1->fTarget) {
97 ERRORF(reporter, "Expected backend texture to be 2D");
Ben Wagner145dbcd2016-11-03 14:40:50 -040098 cleanup(glCtx0, externalTexture.fID, glCtx1.get(), context1, backendTexture1, image);
kkinnunen59319222015-11-22 23:23:53 -080099 return;
100 }
101
102 // Wrap the texture in an EGLImage
103 image = glCtx1->texture2DToEGLImage(backendTexture1->fID);
104 if (GR_EGL_NO_IMAGE == image) {
105 ERRORF(reporter, "Error creating EGL Image from texture");
Ben Wagner145dbcd2016-11-03 14:40:50 -0400106 cleanup(glCtx0, externalTexture.fID, glCtx1.get(), context1, backendTexture1, image);
kkinnunen59319222015-11-22 23:23:53 -0800107 return;
108 }
109
110 // Populate the texture using GL context 1. Important to use TexSubImage as TexImage orphans
111 // the EGL image. Also, this must be done after creating the EGLImage as the texture
112 // contents may not be preserved when the image is created.
113 SkAutoTMalloc<uint32_t> pixels(kSize * kSize);
114 for (int i = 0; i < kSize*kSize; ++i) {
115 pixels.get()[i] = 0xDDAABBCC;
116 }
117 GR_GL_CALL(glCtx1->gl(), ActiveTexture(GR_GL_TEXTURE0));
118 GR_GL_CALL(glCtx1->gl(), BindTexture(backendTexture1->fTarget, backendTexture1->fID));
119 GR_GL_CALL(glCtx1->gl(), TexSubImage2D(backendTexture1->fTarget, 0, 0, 0, kSize, kSize,
120 GR_GL_RGBA, GR_GL_UNSIGNED_BYTE, pixels.get()));
121 GR_GL_CALL(glCtx1->gl(), Finish());
122 // We've been making direct GL calls in GL context 1, let GrContext 1 know its internal
123 // state is invalid.
124 context1->resetContext();
125
126 ///////////////////////////////// CONTEXT 0 ///////////////////////////////////
127
128 // Make a new texture ID in GL Context 0 from the EGL Image
129 glCtx0->makeCurrent();
130 externalTexture.fTarget = GR_GL_TEXTURE_EXTERNAL;
131 externalTexture.fID = glCtx0->eglImageToExternalTexture(image);
Robert Phillipsfee2b4e2017-06-08 18:22:53 -0400132 if (0 == externalTexture.fID) {
133 ERRORF(reporter, "Error converting EGL Image back to texture");
134 cleanup(glCtx0, externalTexture.fID, glCtx1.get(), context1, backendTexture1, image);
135 return;
136 }
kkinnunen59319222015-11-22 23:23:53 -0800137
138 // Wrap this texture ID in a GrTexture
Greg Daniel207282e2017-04-26 13:29:21 -0400139 GrBackendTexture backendTex(kSize, kSize, kRGBA_8888_GrPixelConfig, externalTexture);
Robert Phillipsd46697a2017-01-25 12:10:37 -0500140
Greg Daniel7ef28f32017-04-20 16:41:55 +0000141 // TODO: If I make this TopLeft origin to match resolve_origin calls for kDefault, this test
142 // fails on the Nexus5. Why?
Brian Salomond17f6582017-07-19 18:28:58 -0400143 sk_sp<GrTextureContext> surfaceContext = context0->contextPriv().makeBackendTextureContext(
144 backendTex, kBottomLeft_GrSurfaceOrigin, nullptr);
Robert Phillipsd46697a2017-01-25 12:10:37 -0500145
Robert Phillips26caf892017-01-27 10:58:31 -0500146 if (!surfaceContext) {
147 ERRORF(reporter, "Error wrapping external texture in GrSurfaceContext.");
148 cleanup(glCtx0, externalTexture.fID, glCtx1.get(), context1, backendTexture1, image);
149 return;
kkinnunen59319222015-11-22 23:23:53 -0800150 }
151
bsalomona98419b2015-11-23 07:09:50 -0800152 // Should not be able to wrap as a RT
Robert Phillipsd46697a2017-01-25 12:10:37 -0500153 {
Brian Salomond17f6582017-07-19 18:28:58 -0400154 sk_sp<GrRenderTargetContext> temp =
155 context0->contextPriv().makeBackendTextureRenderTargetContext(
156 backendTex, kBottomLeft_GrSurfaceOrigin, 0, nullptr);
Robert Phillips26caf892017-01-27 10:58:31 -0500157 if (temp) {
Robert Phillipsd46697a2017-01-25 12:10:37 -0500158 ERRORF(reporter, "Should not be able to wrap an EXTERNAL texture as a RT.");
159 }
kkinnunen59319222015-11-22 23:23:53 -0800160 }
bsalomona98419b2015-11-23 07:09:50 -0800161
Robert Phillips26c90e02017-03-14 14:39:29 -0400162 test_read_pixels(reporter, surfaceContext.get(), pixels.get(), "EGLImageTest-read");
bsalomona98419b2015-11-23 07:09:50 -0800163
Robert Phillips3500b772017-01-27 10:11:42 -0500164 // We should not be able to write to a EXTERNAL texture
Robert Phillips26c90e02017-03-14 14:39:29 -0400165 test_write_pixels(reporter, surfaceContext.get(), false, "EGLImageTest-write");
bsalomona98419b2015-11-23 07:09:50 -0800166
Robert Phillips3500b772017-01-27 10:11:42 -0500167 // Only test RT-config
168 // TODO: why do we always need to draw to copy from an external texture?
Robert Phillipsf200a902017-01-30 13:27:37 -0500169 test_copy_from_surface(reporter, context0, surfaceContext->asSurfaceProxy(),
Robert Phillips3500b772017-01-27 10:11:42 -0500170 pixels.get(), true, "EGLImageTest-copy");
bsalomona98419b2015-11-23 07:09:50 -0800171
Ben Wagner145dbcd2016-11-03 14:40:50 -0400172 cleanup(glCtx0, externalTexture.fID, glCtx1.get(), context1, backendTexture1, image);
bsalomon7ea33f52015-11-22 14:51:00 -0800173}
174
175#endif