1 /*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <external_window.h>
17
18 #include "gles/device_gles.h"
19 #include "gles/egl_functions.h"
20 #include "gles/gl_functions.h"
21 #include "gles/gpu_image_gles.h"
22 #include "util/log.h"
23
RENDER_BEGIN_NAMESPACE()24 RENDER_BEGIN_NAMESPACE()
25 BASE_NS::unique_ptr<GpuImage> DeviceGLES::CreateGpuImageView(
26 const GpuImageDesc& desc, const BackendSpecificImageDesc& platformData)
27 {
28 BASE_NS::unique_ptr<GpuImage> image;
29 PLUGIN_ASSERT(IsActive());
30 GpuImagePlatformDataGL data {};
31 #if RENDER_HAS_GLES_BACKEND
32 if (backendType_ == DeviceBackendType::OPENGLES) {
33 const ImageDescGLES& tmp = (const ImageDescGLES&)platformData;
34 data.type = tmp.type;
35 data.image = tmp.image;
36 data.bytesperpixel = tmp.bytesperpixel;
37 data.dataType = tmp.dataType;
38 data.format = tmp.format;
39 data.internalFormat = tmp.internalFormat;
40 data.eglImage = tmp.eglImage;
41 data.hwBuffer = tmp.platformHwBuffer;
42 data.swizzle = { GL_RED, GL_GREEN, GL_BLUE, GL_ALPHA };
43
44 if (!data.hwBuffer) {
45 image = CreateGpuImageView(desc, data);
46 } else if (eglCreateImageKHR && eglDestroyImageKHR) {
47 auto* nativeBufferPtr = reinterpret_cast<OH_NativeBuffer*>(tmp.platformHwBuffer);
48 auto nativeWindowBufferPtr = OH_NativeWindow_CreateNativeWindowBufferFromNativeBuffer(nativeBufferPtr);
49
50 const auto dsp = static_cast<const DevicePlatformDataGLES&>(eglState_.GetPlatformData()).display;
51 static constexpr EGLint attrs[] = { EGL_IMAGE_PRESERVED, EGL_TRUE, EGL_NONE };
52 const auto eglImage =
53 eglCreateImageKHR(dsp, EGL_NO_CONTEXT, EGL_NATIVE_BUFFER_OHOS, nativeWindowBufferPtr, attrs);
54 if (!eglImage) {
55 PLUGIN_LOG_E("eglCreateImageKHR failed %d", eglGetError());
56 return {};
57 }
58 GpuImageDesc finalDesc = GetImageDescFromHwBufferDesc(tmp.platformHwBuffer);
59
60 data.type = GL_TEXTURE_EXTERNAL_OES;
61 data.eglImage = reinterpret_cast<uintptr_t>(eglImage);
62 image = CreateGpuImageView(finalDesc, data);
63
64 eglDestroyImageKHR(dsp, eglImage);
65 }
66 }
67 #endif
68 #if RENDER_HAS_GL_BACKEND
69 if (backendType_ == DeviceBackendType::OPENGL) {
70 const ImageDescGL& tmp = (const ImageDescGL&)platformData;
71 data.type = tmp.type;
72 data.image = tmp.image;
73 data.bytesperpixel = tmp.bytesperpixel;
74 data.dataType = tmp.dataType;
75 data.format = tmp.format;
76 data.internalFormat = tmp.internalFormat;
77 data.swizzle = { GL_RED, GL_GREEN, GL_BLUE, GL_ALPHA };
78 image = CreateGpuImageView(desc, data);
79 }
80 #endif
81 return image;
82 }
83 RENDER_END_NAMESPACE()
84