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