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