1e5c31af7Sopenharmony_ci/*
2e5c31af7Sopenharmony_ci * Copyright (c) 2022 Shenzhen Kaihong Digital Industry Development Co., Ltd.
3e5c31af7Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4e5c31af7Sopenharmony_ci * you may not use this file except in compliance with the License.
5e5c31af7Sopenharmony_ci * You may obtain a copy of the License at
6e5c31af7Sopenharmony_ci *
7e5c31af7Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8e5c31af7Sopenharmony_ci *
9e5c31af7Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10e5c31af7Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11e5c31af7Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12e5c31af7Sopenharmony_ci * See the License for the specific language governing permissions and
13e5c31af7Sopenharmony_ci * limitations under the License.
14e5c31af7Sopenharmony_ci */
15e5c31af7Sopenharmony_ci#include "tcuOhosNativeContext.hpp"
16e5c31af7Sopenharmony_ci#include "egluGLContextFactory.hpp"
17e5c31af7Sopenharmony_ci#include "eglwLibrary.hpp"
18e5c31af7Sopenharmony_ci#include "eglwFunctions.hpp"
19e5c31af7Sopenharmony_ci#include "eglwEnums.hpp"
20e5c31af7Sopenharmony_ci#include "deUniquePtr.hpp"
21e5c31af7Sopenharmony_ci#include "tcuCommandLine.hpp"
22e5c31af7Sopenharmony_ci#include "deDynamicLibrary.hpp"
23e5c31af7Sopenharmony_ci
24e5c31af7Sopenharmony_ci#include "ohos_context_i.h"
25e5c31af7Sopenharmony_ci
26e5c31af7Sopenharmony_ciusing namespace tcu;
27e5c31af7Sopenharmony_ciusing namespace OHOS_ROSEN;
28e5c31af7Sopenharmony_ciusing namespace egl;
29e5c31af7Sopenharmony_ci
30e5c31af7Sopenharmony_ciusing de::MovePtr;
31e5c31af7Sopenharmony_ciusing de::UniquePtr;
32e5c31af7Sopenharmony_ciusing eglu::GLContextFactory;
33e5c31af7Sopenharmony_ciusing eglu::NativeDisplay;
34e5c31af7Sopenharmony_ciusing eglu::NativeDisplayFactory;
35e5c31af7Sopenharmony_ciusing eglu::NativePixmap;
36e5c31af7Sopenharmony_ciusing eglu::NativePixmapFactory;
37e5c31af7Sopenharmony_ciusing eglu::NativeWindow;
38e5c31af7Sopenharmony_ciusing eglu::NativeWindowFactory;
39e5c31af7Sopenharmony_ciusing eglu::WindowParams;
40e5c31af7Sopenharmony_ciusing glu::ContextFactory;
41e5c31af7Sopenharmony_ciusing std::string;
42e5c31af7Sopenharmony_ciusing tcu::TextureLevel;
43e5c31af7Sopenharmony_ci
44e5c31af7Sopenharmony_ciclass GLFunctionLoader : public glw::FunctionLoader
45e5c31af7Sopenharmony_ci{
46e5c31af7Sopenharmony_cipublic:
47e5c31af7Sopenharmony_ci    GLFunctionLoader(const char *path)
48e5c31af7Sopenharmony_ci        : m_library(path)
49e5c31af7Sopenharmony_ci    {
50e5c31af7Sopenharmony_ci    }
51e5c31af7Sopenharmony_ci
52e5c31af7Sopenharmony_ci    glw::GenericFuncType get(const char *name) const
53e5c31af7Sopenharmony_ci    {
54e5c31af7Sopenharmony_ci        return m_library.getFunction(name);
55e5c31af7Sopenharmony_ci    }
56e5c31af7Sopenharmony_ci
57e5c31af7Sopenharmony_ciprivate:
58e5c31af7Sopenharmony_ci    de::DynamicLibrary m_library;
59e5c31af7Sopenharmony_ci};
60e5c31af7Sopenharmony_ci
61e5c31af7Sopenharmony_ciOhosRendContext::OhosRendContext(const glu::RenderConfig &config, const tcu::CommandLine &cmdLine)
62e5c31af7Sopenharmony_ci    : m_contextType(config.type)
63e5c31af7Sopenharmony_ci{
64e5c31af7Sopenharmony_ci    DE_UNREF(cmdLine);
65e5c31af7Sopenharmony_ci
66e5c31af7Sopenharmony_ci    printf("~~~~~~~OhosRendContext~~~~~~~~\n");
67e5c31af7Sopenharmony_ci    printf("config.width = %d\n", config.width);
68e5c31af7Sopenharmony_ci    printf("config.height = %d\n", config.height);
69e5c31af7Sopenharmony_ci    printf("config.redBits = %d\n", config.redBits);
70e5c31af7Sopenharmony_ci    printf("config.greenBits = %d\n", config.greenBits);
71e5c31af7Sopenharmony_ci    printf("config.blueBits = %d\n", config.blueBits);
72e5c31af7Sopenharmony_ci    printf("config.alphaBits = %d\n", config.alphaBits);
73e5c31af7Sopenharmony_ci    printf("config.depthBits = %d\n", config.depthBits);
74e5c31af7Sopenharmony_ci    printf("config.stencilBits = %d\n", config.stencilBits);
75e5c31af7Sopenharmony_ci    printf("config.numSamples = %d\n", config.numSamples);
76e5c31af7Sopenharmony_ci    printf("config.type.getMajorVersion() = %d\n", config.type.getMajorVersion());
77e5c31af7Sopenharmony_ci    printf("config.type.getMinorVersion() = %d\n", config.type.getMinorVersion());
78e5c31af7Sopenharmony_ci    printf("~~~~~~~~~~~~~~~\n");
79e5c31af7Sopenharmony_ci    // TODO:
80e5c31af7Sopenharmony_ci    int32_t w = config.width;
81e5c31af7Sopenharmony_ci    int32_t h = config.height;
82e5c31af7Sopenharmony_ci    if (w == -1)
83e5c31af7Sopenharmony_ci    {
84e5c31af7Sopenharmony_ci        w = 512;
85e5c31af7Sopenharmony_ci    }
86e5c31af7Sopenharmony_ci    if (h == -1)
87e5c31af7Sopenharmony_ci    {
88e5c31af7Sopenharmony_ci        h = 512;
89e5c31af7Sopenharmony_ci    }
90e5c31af7Sopenharmony_ci
91e5c31af7Sopenharmony_ci    OHOS::RCI_GLES_VERSION ver;
92e5c31af7Sopenharmony_ci    if (config.type.getMajorVersion() == 2 && config.type.getMinorVersion() == 0)
93e5c31af7Sopenharmony_ci    {
94e5c31af7Sopenharmony_ci        ver = OHOS::RCI_GLES_VERSION::V20;
95e5c31af7Sopenharmony_ci    }
96e5c31af7Sopenharmony_ci    else if (config.type.getMajorVersion() == 3 && config.type.getMinorVersion() == 0)
97e5c31af7Sopenharmony_ci    {
98e5c31af7Sopenharmony_ci        ver = OHOS::RCI_GLES_VERSION::V30;
99e5c31af7Sopenharmony_ci    }
100e5c31af7Sopenharmony_ci    else if (config.type.getMajorVersion() == 3 && config.type.getMinorVersion() == 1)
101e5c31af7Sopenharmony_ci    {
102e5c31af7Sopenharmony_ci        ver = OHOS::RCI_GLES_VERSION::V31;
103e5c31af7Sopenharmony_ci    }
104e5c31af7Sopenharmony_ci    else if (config.type.getMajorVersion() == 3 && config.type.getMinorVersion() == 2)
105e5c31af7Sopenharmony_ci    {
106e5c31af7Sopenharmony_ci        ver = OHOS::RCI_GLES_VERSION::V32;
107e5c31af7Sopenharmony_ci    }
108e5c31af7Sopenharmony_ci    else
109e5c31af7Sopenharmony_ci    {
110e5c31af7Sopenharmony_ci        throw tcu::NotSupportedError("not support version");
111e5c31af7Sopenharmony_ci    }
112e5c31af7Sopenharmony_ci
113e5c31af7Sopenharmony_ci    OHOS::RCI_PIXEL_FORMAT pf = {
114e5c31af7Sopenharmony_ci        .redBits = 8,
115e5c31af7Sopenharmony_ci        .greenBits = 8,
116e5c31af7Sopenharmony_ci        .blueBits = 8,
117e5c31af7Sopenharmony_ci        .alphaBits = 8,
118e5c31af7Sopenharmony_ci        .depthBits = 24,
119e5c31af7Sopenharmony_ci        .stencilBits = 8,
120e5c31af7Sopenharmony_ci        .numSamples = 4,
121e5c31af7Sopenharmony_ci    };
122e5c31af7Sopenharmony_ci    if (config.redBits != -1)
123e5c31af7Sopenharmony_ci    {
124e5c31af7Sopenharmony_ci        pf.redBits = config.redBits;
125e5c31af7Sopenharmony_ci    }
126e5c31af7Sopenharmony_ci    if (config.greenBits != -1)
127e5c31af7Sopenharmony_ci    {
128e5c31af7Sopenharmony_ci        pf.greenBits = config.greenBits;
129e5c31af7Sopenharmony_ci    }
130e5c31af7Sopenharmony_ci    if (config.blueBits != -1)
131e5c31af7Sopenharmony_ci    {
132e5c31af7Sopenharmony_ci        pf.blueBits = config.blueBits;
133e5c31af7Sopenharmony_ci    }
134e5c31af7Sopenharmony_ci    if (config.alphaBits != -1)
135e5c31af7Sopenharmony_ci    {
136e5c31af7Sopenharmony_ci        pf.alphaBits = config.alphaBits;
137e5c31af7Sopenharmony_ci    }
138e5c31af7Sopenharmony_ci    if (config.depthBits != -1)
139e5c31af7Sopenharmony_ci    {
140e5c31af7Sopenharmony_ci        pf.depthBits = config.depthBits;
141e5c31af7Sopenharmony_ci    }
142e5c31af7Sopenharmony_ci    if (config.stencilBits != -1)
143e5c31af7Sopenharmony_ci    {
144e5c31af7Sopenharmony_ci        pf.stencilBits = config.stencilBits;
145e5c31af7Sopenharmony_ci    }
146e5c31af7Sopenharmony_ci    if (config.numSamples != -1)
147e5c31af7Sopenharmony_ci    {
148e5c31af7Sopenharmony_ci        pf.numSamples = config.numSamples;
149e5c31af7Sopenharmony_ci    }
150e5c31af7Sopenharmony_ci
151e5c31af7Sopenharmony_ci    OHOS::RCI_SURFACE_TYPE surfaceType;
152e5c31af7Sopenharmony_ci    switch (config.surfaceType)
153e5c31af7Sopenharmony_ci    {
154e5c31af7Sopenharmony_ci    case glu::RenderConfig::SURFACETYPE_DONT_CARE:
155e5c31af7Sopenharmony_ci        surfaceType = OHOS::RCI_SURFACE_TYPE::NONE;
156e5c31af7Sopenharmony_ci        break;
157e5c31af7Sopenharmony_ci    case glu::RenderConfig::SURFACETYPE_OFFSCREEN_NATIVE:
158e5c31af7Sopenharmony_ci        surfaceType = OHOS::RCI_SURFACE_TYPE::PIXMAP;
159e5c31af7Sopenharmony_ci        break;
160e5c31af7Sopenharmony_ci    case glu::RenderConfig::SURFACETYPE_OFFSCREEN_GENERIC:
161e5c31af7Sopenharmony_ci        surfaceType = OHOS::RCI_SURFACE_TYPE::PBUFFER;
162e5c31af7Sopenharmony_ci        break;
163e5c31af7Sopenharmony_ci    case glu::RenderConfig::SURFACETYPE_WINDOW:
164e5c31af7Sopenharmony_ci        surfaceType = OHOS::RCI_SURFACE_TYPE::WINDOW;
165e5c31af7Sopenharmony_ci        break;
166e5c31af7Sopenharmony_ci    case glu::RenderConfig::SURFACETYPE_LAST:
167e5c31af7Sopenharmony_ci        TCU_CHECK_INTERNAL(false);
168e5c31af7Sopenharmony_ci    }
169e5c31af7Sopenharmony_ci
170e5c31af7Sopenharmony_ci    OHOS::RCI_PROFILE profile;
171e5c31af7Sopenharmony_ci    switch (config.type.getProfile())
172e5c31af7Sopenharmony_ci    {
173e5c31af7Sopenharmony_ci    case glu::PROFILE_ES:
174e5c31af7Sopenharmony_ci        profile = OHOS::RCI_PROFILE::ES;
175e5c31af7Sopenharmony_ci        break;
176e5c31af7Sopenharmony_ci    case glu::PROFILE_CORE:
177e5c31af7Sopenharmony_ci        profile = OHOS::RCI_PROFILE::CORE;
178e5c31af7Sopenharmony_ci        break;
179e5c31af7Sopenharmony_ci    case glu::PROFILE_COMPATIBILITY:
180e5c31af7Sopenharmony_ci        profile = OHOS::RCI_PROFILE::COMPATIBILITY;
181e5c31af7Sopenharmony_ci        break;
182e5c31af7Sopenharmony_ci    case glu::PROFILE_LAST:
183e5c31af7Sopenharmony_ci        TCU_CHECK_INTERNAL(false);
184e5c31af7Sopenharmony_ci    }
185e5c31af7Sopenharmony_ci
186e5c31af7Sopenharmony_ci    int flags = 0;
187e5c31af7Sopenharmony_ci    if ((config.type.getFlags() & glu::CONTEXT_DEBUG) != 0)
188e5c31af7Sopenharmony_ci        flags |= static_cast<int>(OHOS::RCI_CONTEXT_FLAG::DEBUG);
189e5c31af7Sopenharmony_ci
190e5c31af7Sopenharmony_ci    if ((config.type.getFlags() & glu::CONTEXT_ROBUST) != 0)
191e5c31af7Sopenharmony_ci        flags |= static_cast<int>(OHOS::RCI_CONTEXT_FLAG::ROBUST);
192e5c31af7Sopenharmony_ci
193e5c31af7Sopenharmony_ci    if ((config.type.getFlags() & glu::CONTEXT_FORWARD_COMPATIBLE) != 0)
194e5c31af7Sopenharmony_ci        flags |= static_cast<int>(OHOS::RCI_CONTEXT_FLAG::FORWARD_COMPATIBLE);
195e5c31af7Sopenharmony_ci
196e5c31af7Sopenharmony_ci    if (!OHOS::OhosContextI::GetInstance().SetConfig(w, h, ver, pf, surfaceType, profile, static_cast<OHOS::RCI_CONTEXT_FLAG>(flags)))
197e5c31af7Sopenharmony_ci    {
198e5c31af7Sopenharmony_ci        throw tcu::NotSupportedError("not support context");
199e5c31af7Sopenharmony_ci    }
200e5c31af7Sopenharmony_ci    OHOS::OhosContextI::GetInstance().InitNativeWindow();
201e5c31af7Sopenharmony_ci    OHOS::OhosContextI::GetInstance().InitEglSurface();
202e5c31af7Sopenharmony_ci    OHOS::OhosContextI::GetInstance().InitEglContext();
203e5c31af7Sopenharmony_ci    OHOS::OhosContextI::GetInstance().MakeCurrent();
204e5c31af7Sopenharmony_ci
205e5c31af7Sopenharmony_ci    if (config.type.getMajorVersion() == 2 || config.type.getMajorVersion() == 3)
206e5c31af7Sopenharmony_ci    {
207e5c31af7Sopenharmony_ci        GLFunctionLoader loader("libGLESv3.so");
208e5c31af7Sopenharmony_ci        glu::initFunctions(&m_glFunctions, &loader, config.type.getAPI());
209e5c31af7Sopenharmony_ci    }
210e5c31af7Sopenharmony_ci    else
211e5c31af7Sopenharmony_ci    {
212e5c31af7Sopenharmony_ci        throw tcu::NotSupportedError("not support version");
213e5c31af7Sopenharmony_ci    }
214e5c31af7Sopenharmony_ci    while(m_glFunctions.getError()!=0)
215e5c31af7Sopenharmony_ci    {
216e5c31af7Sopenharmony_ci        printf("err pass\n");
217e5c31af7Sopenharmony_ci    }
218e5c31af7Sopenharmony_ci
219e5c31af7Sopenharmony_ci    m_renderTarget = tcu::RenderTarget(512, 512, PixelFormat(8, 8, 8, 8),
220e5c31af7Sopenharmony_ci                                       OHOS::OhosContextI::GetInstance().GetAttrib(EGL_DEPTH_SIZE),
221e5c31af7Sopenharmony_ci                                       OHOS::OhosContextI::GetInstance().GetAttrib(EGL_STENCIL_SIZE),
222e5c31af7Sopenharmony_ci                                       OHOS::OhosContextI::GetInstance().GetAttrib(EGL_SAMPLES));
223e5c31af7Sopenharmony_ci};
224e5c31af7Sopenharmony_ci
225e5c31af7Sopenharmony_ciOhosRendContext::~OhosRendContext(void)
226e5c31af7Sopenharmony_ci{
227e5c31af7Sopenharmony_ci    printf("~~~~~~~DEOhosRendContext~~~~~~~~\n");
228e5c31af7Sopenharmony_ci    OHOS::OhosContextI::GetInstance().SwapBuffer();
229e5c31af7Sopenharmony_ci};
230e5c31af7Sopenharmony_ci
231e5c31af7Sopenharmony_civoid OhosRendContext::postIterate(void)
232e5c31af7Sopenharmony_ci{
233e5c31af7Sopenharmony_ci    OHOS::OhosContextI::GetInstance().SwapBuffer();
234e5c31af7Sopenharmony_ci}
235