forked from flutter/engine
-
Notifications
You must be signed in to change notification settings - Fork 19
/
tizen_surface_gl.cc
371 lines (337 loc) · 10.6 KB
/
tizen_surface_gl.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
// Copyright 2020 Samsung Electronics Co., Ltd. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "tizen_surface_gl.h"
#include <GLES2/gl2.h>
#include <GLES2/gl2ext.h>
#include "flutter/shell/platform/tizen/tizen_log.h"
template <class T>
using EGLResult = std::pair<bool, T>;
static EGLResult<EGLContext> CreateContext(EGLDisplay display, EGLConfig config,
EGLContext share = EGL_NO_CONTEXT) {
EGLint attributes[] = {EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE};
EGLContext context = eglCreateContext(display, config, share, attributes);
if (context == EGL_NO_CONTEXT) {
LogLastEGLError();
}
return {context != EGL_NO_CONTEXT, context};
}
static EGLResult<EGLConfig> ChooseEGLConfiguration(EGLDisplay display) {
EGLint attributes[] = {
// clang-format off
EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
EGL_RED_SIZE, 8,
EGL_GREEN_SIZE, 8,
EGL_BLUE_SIZE, 8,
EGL_ALPHA_SIZE, 8,
EGL_DEPTH_SIZE, 0,
EGL_STENCIL_SIZE, 0,
EGL_NONE, // termination sentinel
// clang-format on
};
EGLint config_count = 0;
EGLConfig egl_config = nullptr;
if (eglChooseConfig(display, attributes, &egl_config, 1, &config_count) !=
EGL_TRUE) {
LogLastEGLError();
return {false, nullptr};
}
bool success = config_count > 0 && egl_config != nullptr;
return {success, success ? egl_config : nullptr};
}
TizenEGLSurface::~TizenEGLSurface() {
if (eglDestroySurface(tizen_native_egl_window_->GetEGLDisplayHandle(),
egl_surface_) != EGL_TRUE) {
LogLastEGLError();
}
tizen_native_egl_window_ = nullptr;
}
TizenEGLContext::TizenEGLContext(
std::shared_ptr<TizenNativeEGLWindow> tizen_native_egl_window)
: tizen_native_egl_window_(tizen_native_egl_window) {
EGLDisplay egl_display = tizen_native_egl_window_->GetEGLDisplayHandle();
auto config = ChooseEGLConfiguration(egl_display);
if (!config.first) {
FT_LOGE("Failed to ChooseEGLConfiguration");
return;
}
egl_config_ = config.second;
auto ctx = CreateContext(egl_display, egl_config_, EGL_NO_CONTEXT);
if (!ctx.first) {
FT_LOGE("Failed to create egl context");
return;
}
egl_context_ = ctx.second;
auto resource_ctx = CreateContext(egl_display, egl_config_, egl_context_);
if (!resource_ctx.first) {
FT_LOGE("Failed to create egl resource context");
return;
}
egl_resource_context_ = resource_ctx.second;
}
TizenEGLContext::~TizenEGLContext() {
if (eglDestroyContext(tizen_native_egl_window_->GetEGLDisplayHandle(),
egl_context_) != EGL_TRUE) {
FT_LOGE("Failed to destroy egl context");
LogLastEGLError();
}
if (eglDestroyContext(tizen_native_egl_window_->GetEGLDisplayHandle(),
egl_resource_context_) != EGL_TRUE) {
FT_LOGE("Failed to destroy egl resource context");
LogLastEGLError();
}
tizen_native_egl_window_ = nullptr;
}
bool TizenEGLContext::IsValid() {
return tizen_native_egl_window_ && tizen_native_egl_window_->IsValid() &&
egl_config_ != nullptr && egl_context_ != EGL_NO_CONTEXT &&
egl_resource_context_ != EGL_NO_CONTEXT;
}
std::unique_ptr<TizenEGLSurface>
TizenEGLContext::CreateTizenEGLWindowSurface() {
const EGLint attribs[] = {EGL_NONE};
EGLSurface surface = eglCreateWindowSurface(
tizen_native_egl_window_->GetEGLDisplayHandle(), egl_config_,
ecore_wl2_egl_window_native_get(
tizen_native_egl_window_->GetEglWindowHandle()),
attribs);
if (surface == EGL_NO_SURFACE) {
LogLastEGLError();
}
return std::make_unique<TizenEGLSurface>(tizen_native_egl_window_, surface);
}
std::unique_ptr<TizenEGLSurface>
TizenEGLContext::CreateTizenEGLPbufferSurface() {
const EGLint attribs[] = {EGL_WIDTH, 1, EGL_HEIGHT, 1, EGL_NONE};
EGLSurface surface = eglCreatePbufferSurface(
tizen_native_egl_window_->GetEGLDisplayHandle(), egl_config_, attribs);
if (surface == EGL_NO_SURFACE) {
LogLastEGLError();
}
return std::make_unique<TizenEGLSurface>(tizen_native_egl_window_, surface);
}
TizenSurfaceGL::TizenSurfaceGL(
std::shared_ptr<TizenNativeWindow> tizen_native_window)
: tizen_native_window_(tizen_native_window) {
if (!tizen_native_window_->IsValid()) {
FT_LOGE("Invalid native window");
return;
}
tizen_context_gl_ = std::make_unique<TizenEGLContext>(
tizen_native_window_->GetTizenNativeEGLWindow());
if (!tizen_context_gl_->IsValid()) {
FT_LOGE("Invalid context gl");
return;
}
tizen_egl_window_surface_ = tizen_context_gl_->CreateTizenEGLWindowSurface();
if (!tizen_egl_window_surface_->IsValid()) {
FT_LOGE("Invalid egl window surface");
return;
}
tizen_egl_pbuffer_surface_ =
tizen_context_gl_->CreateTizenEGLPbufferSurface();
if (!tizen_egl_pbuffer_surface_->IsValid()) {
FT_LOGE("Invalid egl puffer surface");
return;
}
is_valid_ = true;
}
bool TizenSurfaceGL::OnMakeCurrent() {
if (!is_valid_) {
FT_LOGE("Invalid TizenSurfaceGL");
return false;
}
if (eglMakeCurrent(tizen_native_window_->GetTizenNativeEGLWindow()
->GetEGLDisplayHandle(),
tizen_egl_window_surface_->GetEGLSurfaceHandle(),
tizen_egl_window_surface_->GetEGLSurfaceHandle(),
tizen_context_gl_->GetEGLContextHandle()) != EGL_TRUE) {
FT_LOGE("Could not make the onscreen context current");
LogLastEGLError();
return false;
}
return true;
}
bool TizenSurfaceGL::OnMakeResourceCurrent() {
if (!is_valid_) {
FT_LOGE("Invalid TizenSurfaceGL");
return false;
}
if (eglMakeCurrent(tizen_native_window_->GetTizenNativeEGLWindow()
->GetEGLDisplayHandle(),
tizen_egl_pbuffer_surface_->GetEGLSurfaceHandle(),
tizen_egl_pbuffer_surface_->GetEGLSurfaceHandle(),
tizen_context_gl_->GetEGLResourceContextHandle()) !=
EGL_TRUE) {
FT_LOGE("Could not make the offscreen context current");
LogLastEGLError();
return false;
}
return true;
}
bool TizenSurfaceGL::OnClearCurrent() {
if (!is_valid_) {
FT_LOGE("Invalid TizenSurfaceGL");
return false;
}
if (eglMakeCurrent(tizen_native_window_->GetTizenNativeEGLWindow()
->GetEGLDisplayHandle(),
EGL_NO_SURFACE, EGL_NO_SURFACE,
EGL_NO_CONTEXT) != EGL_TRUE) {
FT_LOGE("Could not clear context");
LogLastEGLError();
return false;
}
return true;
}
bool TizenSurfaceGL::OnPresent() {
if (!is_valid_) {
FT_LOGE("Invalid TizenSurfaceGL");
return false;
}
if (eglSwapBuffers(tizen_native_window_->GetTizenNativeEGLWindow()
->GetEGLDisplayHandle(),
tizen_egl_window_surface_->GetEGLSurfaceHandle()) !=
EGL_TRUE) {
FT_LOGE("Could not swap EGl buffer");
LogLastEGLError();
return false;
}
return true;
}
uint32_t TizenSurfaceGL::OnGetFBO() {
if (!is_valid_) {
FT_LOGE("Invalid TizenSurfaceGL");
return 999;
}
FT_LOGD("OnApplicationGetOnscreenFBO");
return 0; // FBO0
}
#define GL_FUNC(FunctionName) \
else if (strcmp(name, #FunctionName) == 0) { \
return reinterpret_cast<void*>(FunctionName); \
}
void* TizenSurfaceGL::OnProcResolver(const char* name) {
auto address = eglGetProcAddress(name);
if (address != nullptr) {
return reinterpret_cast<void*>(address);
}
GL_FUNC(eglGetCurrentDisplay)
GL_FUNC(eglQueryString)
GL_FUNC(glActiveTexture)
GL_FUNC(glAttachShader)
GL_FUNC(glBindAttribLocation)
GL_FUNC(glBindBuffer)
GL_FUNC(glBindFramebuffer)
GL_FUNC(glBindRenderbuffer)
GL_FUNC(glBindTexture)
GL_FUNC(glBlendColor)
GL_FUNC(glBlendEquation)
GL_FUNC(glBlendFunc)
GL_FUNC(glBufferData)
GL_FUNC(glBufferSubData)
GL_FUNC(glCheckFramebufferStatus)
GL_FUNC(glClear)
GL_FUNC(glClearColor)
GL_FUNC(glClearStencil)
GL_FUNC(glColorMask)
GL_FUNC(glCompileShader)
GL_FUNC(glCompressedTexImage2D)
GL_FUNC(glCompressedTexSubImage2D)
GL_FUNC(glCopyTexSubImage2D)
GL_FUNC(glCreateProgram)
GL_FUNC(glCreateShader)
GL_FUNC(glCullFace)
GL_FUNC(glDeleteBuffers)
GL_FUNC(glDeleteFramebuffers)
GL_FUNC(glDeleteProgram)
GL_FUNC(glDeleteRenderbuffers)
GL_FUNC(glDeleteShader)
GL_FUNC(glDeleteTextures)
GL_FUNC(glDepthMask)
GL_FUNC(glDisable)
GL_FUNC(glDisableVertexAttribArray)
GL_FUNC(glDrawArrays)
GL_FUNC(glDrawElements)
GL_FUNC(glEnable)
GL_FUNC(glEnableVertexAttribArray)
GL_FUNC(glFinish)
GL_FUNC(glFlush)
GL_FUNC(glFramebufferRenderbuffer)
GL_FUNC(glFramebufferTexture2D)
GL_FUNC(glFrontFace)
GL_FUNC(glGenBuffers)
GL_FUNC(glGenerateMipmap)
GL_FUNC(glGenFramebuffers)
GL_FUNC(glGenRenderbuffers)
GL_FUNC(glGenTextures)
GL_FUNC(glGetBufferParameteriv)
GL_FUNC(glGetError)
GL_FUNC(glGetFramebufferAttachmentParameteriv)
GL_FUNC(glGetIntegerv)
GL_FUNC(glGetProgramInfoLog)
GL_FUNC(glGetProgramiv)
GL_FUNC(glGetRenderbufferParameteriv)
GL_FUNC(glGetShaderInfoLog)
GL_FUNC(glGetShaderiv)
GL_FUNC(glGetShaderPrecisionFormat)
GL_FUNC(glGetString)
GL_FUNC(glGetUniformLocation)
GL_FUNC(glIsTexture)
GL_FUNC(glLineWidth)
GL_FUNC(glLinkProgram)
GL_FUNC(glPixelStorei)
GL_FUNC(glReadPixels)
GL_FUNC(glRenderbufferStorage)
GL_FUNC(glScissor)
GL_FUNC(glShaderSource)
GL_FUNC(glStencilFunc)
GL_FUNC(glStencilFuncSeparate)
GL_FUNC(glStencilMask)
GL_FUNC(glStencilMaskSeparate)
GL_FUNC(glStencilOp)
GL_FUNC(glStencilOpSeparate)
GL_FUNC(glTexImage2D)
GL_FUNC(glTexParameterf)
GL_FUNC(glTexParameterfv)
GL_FUNC(glTexParameteri)
GL_FUNC(glTexParameteriv)
GL_FUNC(glTexSubImage2D)
GL_FUNC(glUniform1f)
GL_FUNC(glUniform1fv)
GL_FUNC(glUniform1i)
GL_FUNC(glUniform1iv)
GL_FUNC(glUniform2f)
GL_FUNC(glUniform2fv)
GL_FUNC(glUniform2i)
GL_FUNC(glUniform2iv)
GL_FUNC(glUniform3f)
GL_FUNC(glUniform3fv)
GL_FUNC(glUniform3i)
GL_FUNC(glUniform3iv)
GL_FUNC(glUniform4f)
GL_FUNC(glUniform4fv)
GL_FUNC(glUniform4i)
GL_FUNC(glUniform4iv)
GL_FUNC(glUniformMatrix2fv)
GL_FUNC(glUniformMatrix3fv)
GL_FUNC(glUniformMatrix4fv)
GL_FUNC(glUseProgram)
GL_FUNC(glVertexAttrib1f)
GL_FUNC(glVertexAttrib2fv)
GL_FUNC(glVertexAttrib3fv)
GL_FUNC(glVertexAttrib4fv)
GL_FUNC(glVertexAttribPointer)
GL_FUNC(glViewport)
FT_LOGD("Could not resolve: %s", name);
return nullptr;
}
#undef GL_FUNC
TizenSurfaceGL::~TizenSurfaceGL() {
OnClearCurrent();
tizen_egl_window_surface_ = nullptr;
tizen_egl_pbuffer_surface_ = nullptr;
tizen_context_gl_ = nullptr;
tizen_native_window_ = nullptr;
}