1 /*
2 * Copyright (C) 2022 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "EmulatedEglImage.h"
18
19 #include "OpenGLESDispatch/DispatchTables.h"
20 #include "OpenGLESDispatch/EGLDispatch.h"
21 #include "host-common/logging.h"
22
23 namespace gfxstream {
24 namespace gl {
25
26 /*static*/
create(EGLDisplay display,EGLContext context,EGLenum target,EGLClientBuffer buffer)27 std::unique_ptr<EmulatedEglImage> EmulatedEglImage::create(EGLDisplay display,
28 EGLContext context,
29 EGLenum target,
30 EGLClientBuffer buffer) {
31 EGLImageKHR image = s_egl.eglCreateImageKHR(display, context, target, buffer, nullptr);
32 if (image == EGL_NO_IMAGE_KHR) {
33 ERR("Failed to create EGL image.");
34 return nullptr;
35 }
36
37 // Note: `handle` is (historically) the underlying image handle potentially so that
38 // it can be used directly by glEGLImageTargetTexture2DOES() without another lookup.
39 // In the future, EmulatedEglImage could be updated to use a handle generated by
40 // FrameBuffer.
41 HandleType handle = (HandleType)reinterpret_cast<uintptr_t>(image);
42
43 return std::unique_ptr<EmulatedEglImage>(new EmulatedEglImage(handle, display, image));
44 }
45
EmulatedEglImage(HandleType handle,EGLDisplay display,EGLImageKHR image)46 EmulatedEglImage::EmulatedEglImage(HandleType handle,
47 EGLDisplay display,
48 EGLImageKHR image)
49 : mHandle(handle),
50 mEglDisplay(display),
51 mEglImage(image) {}
52
~EmulatedEglImage()53 EmulatedEglImage::~EmulatedEglImage() {
54 destroy();
55 }
56
destroy()57 EGLBoolean EmulatedEglImage::destroy() {
58 if (mEglImage) {
59 EGLBoolean ret = s_egl.eglDestroyImageKHR(mEglDisplay, mEglImage);
60 if (!ret) {
61 ERR("Failed to destroy EGL image.");
62 }
63 mEglImage = EGL_NO_IMAGE_KHR;
64 return ret;
65 }
66 return EGL_TRUE;
67 }
68
69 } // namespace gl
70 } // namespace gfxstream
71