1 /*
2 * Copyright © Microsoft Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #include <pipe/p_state.h>
25 #include <util/u_inlines.h>
26 #include <frontend/api.h>
27
28 #include <main/mtypes.h>
29 #include <main/texobj.h>
30 #include <state_tracker/st_context.h>
31 #include <state_tracker/st_texture.h>
32
33 #include "stw_image.h"
34
35 struct stw_image *
stw_create_image_from_texture(struct stw_context * ctx,GLenum gl_target,GLuint texture,GLuint depth,GLint level,enum stw_image_error * error)36 stw_create_image_from_texture(struct stw_context *ctx, GLenum gl_target, GLuint texture,
37 GLuint depth, GLint level, enum stw_image_error *error)
38 {
39 struct st_context *st_ctx = (struct st_context *)ctx->st;
40 struct gl_context *gl_ctx = st_ctx->ctx;
41 struct gl_texture_object *obj;
42 struct pipe_resource *tex;
43 GLuint face = 0;
44
45 obj = _mesa_lookup_texture(gl_ctx, texture);
46 if (!obj || obj->Target != gl_target) {
47 *error = STW_IMAGE_ERROR_BAD_PARAMETER;
48 return NULL;
49 }
50
51 tex = st_get_texobj_resource(obj);
52 if (!tex) {
53 *error = STW_IMAGE_ERROR_BAD_PARAMETER;
54 return NULL;
55 }
56
57 if (gl_target == GL_TEXTURE_CUBE_MAP)
58 face = depth;
59
60 _mesa_test_texobj_completeness(gl_ctx, obj);
61 if (!obj->_BaseComplete || (level > 0 && !obj->_MipmapComplete)) {
62 *error = STW_IMAGE_ERROR_BAD_PARAMETER;
63 return NULL;
64 }
65
66 if (level < obj->Attrib.BaseLevel || level > obj->_MaxLevel) {
67 *error = STW_IMAGE_ERROR_BAD_MATCH;
68 return NULL;
69 }
70
71 if (gl_target == GL_TEXTURE_3D && obj->Image[face][level]->Depth < depth) {
72 *error = STW_IMAGE_ERROR_BAD_MATCH;
73 return NULL;
74 }
75
76 struct stw_image *ret = calloc(1, sizeof(struct stw_image));
77 pipe_resource_reference(&ret->pres, tex);
78 ret->level = level;
79 ret->layer = depth;
80 ret->format = tex->format;
81
82 gl_ctx->Shared->HasExternallySharedImages = true;
83 *error = STW_IMAGE_ERROR_SUCCESS;
84 return ret;
85 }
86
87 struct stw_image *
stw_create_image_from_renderbuffer(struct stw_context * ctx,GLuint renderbuffer,enum stw_image_error * error)88 stw_create_image_from_renderbuffer(struct stw_context *ctx, GLuint renderbuffer,
89 enum stw_image_error *error)
90 {
91 struct st_context *st_ctx = (struct st_context *)ctx->st;
92 struct gl_context *gl_ctx = st_ctx->ctx;
93 struct gl_renderbuffer *rb;
94 struct pipe_resource *tex;
95
96 /* Section 3.9 (EGLImage Specification and Management) of the EGL 1.5
97 * specification says:
98 *
99 * "If target is EGL_GL_RENDERBUFFER and buffer is not the name of a
100 * renderbuffer object, or if buffer is the name of a multisampled
101 * renderbuffer object, the error EGL_BAD_PARAMETER is generated."
102 *
103 * "If target is EGL_GL_TEXTURE_2D , EGL_GL_TEXTURE_CUBE_MAP_*,
104 * EGL_GL_RENDERBUFFER or EGL_GL_TEXTURE_3D and buffer refers to the
105 * default GL texture object (0) for the corresponding GL target, the
106 * error EGL_BAD_PARAMETER is generated."
107 * (rely on _mesa_lookup_renderbuffer returning NULL in this case)
108 */
109 rb = _mesa_lookup_renderbuffer(gl_ctx, renderbuffer);
110 if (!rb || rb->NumSamples > 0) {
111 *error = STW_IMAGE_ERROR_BAD_PARAMETER;
112 return NULL;
113 }
114
115 tex = rb->texture;
116 if (!tex) {
117 *error = STW_IMAGE_ERROR_BAD_PARAMETER;
118 return NULL;
119 }
120
121 struct stw_image *ret = calloc(1, sizeof(struct stw_image));
122 pipe_resource_reference(&ret->pres, tex);
123 ret->format = tex->format;
124
125 gl_ctx->Shared->HasExternallySharedImages = true;
126 *error = STW_IMAGE_ERROR_SUCCESS;
127 return ret;
128 }
129
130 void
stw_destroy_image(struct stw_image * img)131 stw_destroy_image(struct stw_image *img)
132 {
133 pipe_resource_reference(&img->pres, NULL);
134 free(img);
135 }
136
137 void
stw_translate_image(struct stw_image * in,struct st_egl_image * out)138 stw_translate_image(struct stw_image *in, struct st_egl_image *out)
139 {
140 pipe_resource_reference(&out->texture, in->pres);
141 out->format = in->format;
142 out->layer = in->layer;
143 out->level = in->level;
144 out->imported_dmabuf = false;
145 }
146