-
Notifications
You must be signed in to change notification settings - Fork 198
/
gpupixel_context.cc
executable file
·354 lines (308 loc) · 9.53 KB
/
gpupixel_context.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
/*
* GPUPixel
*
* Created by PixPark on 2021/6/24.
* Copyright © 2021 PixPark. All rights reserved.
*/
#include "gpupixel_context.h"
#include "util.h"
#if defined(GPUPIXEL_IOS) || defined(GPUPIXEL_MAC)
typedef void (^TaskBlock)(void);
@interface iOSHelper : NSObject {
bool isActive;
}
- (bool)isAppActive;
- (void)run:(TaskBlock)task;
@end
@implementation iOSHelper
- (id)init {
if (self = [super init]) {
#if defined(GPUPIXEL_IOS)
// register notification
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(willResignActive)
name:UIApplicationWillResignActiveNotification
object:nil];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(didBecomeActive)
name:UIApplicationDidBecomeActiveNotification
object:nil];
#endif
isActive = true;
}
return self;
}
- (void)willResignActive {
@synchronized(self) {
isActive = false;
}
}
- (void)didBecomeActive {
@synchronized(self) {
isActive = true;
}
}
- (bool)isAppActive {
@synchronized(self) {
return isActive;
}
}
- (void)run:(TaskBlock)task {
[self performSelectorOnMainThread:@selector(doRunTask:)
withObject:task
waitUntilDone:NO];
}
- (void)doRunTask:(TaskBlock)task {
task();
}
@end
#endif
NS_GPUPIXEL_BEGIN
#if defined(GPUPIXEL_IOS) || defined(GPUPIXEL_MAC)
iOSHelper* iosHelper;
#elif defined(GPUPIXEL_ANDROID)
const std::string kRtcLogTag = "Context";
#elif defined(GPUPIXEL_WIN) || defined(GPUPIXEL_LINUX)
const unsigned int VIEW_WIDTH = 1280;
const unsigned int VIEW_HEIGHT = 720;
#endif
GPUPixelContext* GPUPixelContext::_instance = 0;
std::mutex GPUPixelContext::_mutex;
GPUPixelContext::GPUPixelContext()
: _curShaderProgram(0),
isCapturingFrame(false),
captureUpToFilter(0),
capturedFrameData(0) {
_framebufferCache = new FramebufferCache();
task_queue_ = std::make_shared<LocalDispatchQueue>();
init();
}
GPUPixelContext::~GPUPixelContext() {
releaseContext();
delete _framebufferCache;
}
GPUPixelContext* GPUPixelContext::getInstance() {
if (!_instance) {
std::unique_lock<std::mutex> lock(_mutex);
if (!_instance) {
_instance = new (std::nothrow) GPUPixelContext;
}
}
return _instance;
};
void GPUPixelContext::destroy() {
if (_instance) {
delete _instance;
_instance = 0;
}
}
void GPUPixelContext::init() {
runSync([=] {
Util::Log("INFO", "start init GPUPixelContext");
this->createContext();
});
}
FramebufferCache* GPUPixelContext::getFramebufferCache() const {
return _framebufferCache;
}
void GPUPixelContext::setActiveShaderProgram(GLProgram* shaderProgram) {
if (_curShaderProgram != shaderProgram) {
_curShaderProgram = shaderProgram;
shaderProgram->use();
}
}
void GPUPixelContext::purge() {
_framebufferCache->purge();
}
void GPUPixelContext::createContext() {
#if defined(GPUPIXEL_IOS)
_eglContext = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
[EAGLContext setCurrentContext:_eglContext];
iosHelper = [[iOSHelper alloc] init];
#elif defined(GPUPIXEL_MAC)
NSOpenGLPixelFormatAttribute pixelFormatAttributes[] = {
NSOpenGLPFADoubleBuffer,
NSOpenGLPFAOpenGLProfile,
NSOpenGLProfileVersionLegacy,
NSOpenGLPFAAccelerated,
0,
NSOpenGLPFAColorSize,
24,
NSOpenGLPFAAlphaSize,
8,
NSOpenGLPFADepthSize,
24,
0};
_pixelFormat =
[[NSOpenGLPixelFormat alloc] initWithAttributes:pixelFormatAttributes];
imageProcessingContext =
[[NSOpenGLContext alloc] initWithFormat:_pixelFormat shareContext:nil];
GLint interval = 0;
[imageProcessingContext makeCurrentContext];
[imageProcessingContext setValues:&interval
forParameter:NSOpenGLContextParameterSwapInterval];
#elif defined(GPUPIXEL_ANDROID)
Util::Log("INFO", "GPUPixelContext::createContext start");
context_inited = true;
m_surfacewidth = 1;
m_surfaceheight = 1; // no use
m_gpu_context = new _gpu_context_t;
memset(m_gpu_context, 0, sizeof(_gpu_context_t));
m_gpu_context->egldisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
if (EGL_NO_DISPLAY == m_gpu_context->egldisplay) {
// err_log("eglGetDisplay Error!");
Util::Log("ERROR", "eglGetDisplay Error!");
return;
}
GLint majorVersion;
GLint minorVersion;
if (!eglInitialize(m_gpu_context->egldisplay, &majorVersion, &minorVersion)) {
// err_log("eglInitialize Error!");
Util::Log("ERROR", "eglInitialize Error!");
return;
}
// info_log("GL Version minor:%d major:%d", minorVersion, majorVersion);
// 如果创建WindowSurface使用EGL_WINDOW_BIT,PBufferSurface使用EGL_PBUFFER_BIT
EGLint config_attribs[] = {EGL_BLUE_SIZE,
8,
EGL_GREEN_SIZE,
8,
EGL_RED_SIZE,
8,
EGL_RENDERABLE_TYPE,
EGL_OPENGL_ES2_BIT,
EGL_SURFACE_TYPE,
EGL_PBUFFER_BIT,
EGL_NONE};
int num_configs = 0;
EGLConfig eglConfig;
if (!eglChooseConfig(m_gpu_context->egldisplay, config_attribs, &eglConfig, 1,
&num_configs)) {
// err_log("eglChooseConfig Error!");
Util::Log("ERROR", "eglChooseConfig Error!");
return;
}
EGLint context_attrib[] = {EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE};
m_gpu_context->eglcontext = eglCreateContext(
m_gpu_context->egldisplay, eglConfig, EGL_NO_CONTEXT, context_attrib);
if (EGL_NO_CONTEXT == m_gpu_context->eglcontext) {
// err_log("eglCreateContext Error!");
Util::Log("ERROR", "eglCreateContext Error!");
return;
}
int attribListPbuffer[] = {EGL_WIDTH, m_surfacewidth, EGL_HEIGHT,
m_surfaceheight, EGL_NONE};
m_gpu_context->eglsurface = eglCreatePbufferSurface(
m_gpu_context->egldisplay, eglConfig, attribListPbuffer);
if (EGL_NO_SURFACE == m_gpu_context->eglsurface) {
// err_log("eglCreatePbufferSurface Error!");
Util::Log("ERROR", "eglCreatePbufferSurface Error!");
return;
}
if (!eglQuerySurface(m_gpu_context->egldisplay, m_gpu_context->eglsurface,
EGL_WIDTH, &m_surfacewidth) ||
!eglQuerySurface(m_gpu_context->egldisplay, m_gpu_context->eglsurface,
EGL_HEIGHT, &m_surfaceheight)) {
// err_log("eglQuerySurface Error!");
Util::Log("ERROR", "eglQuerySurface Error!");
return;
}
// info_log("Create Surface width:%d height:%d", m_surfacewidth,
// m_surfaceheight);
Util::Log("INFO", "Create Surface width:%d height:%d", m_surfacewidth,
m_surfaceheight);
#elif defined(GPUPIXEL_WIN) || defined(GPUPIXEL_LINUX)
int ret = glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
// must use legacy opengl profile
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_COMPAT_PROFILE);
if (ret) {
glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
} else {
// todo log error
return;
}
gl_context_ = glfwCreateWindow(VIEW_WIDTH, VIEW_HEIGHT, "gpupixel opengl context", NULL, NULL);
if (!gl_context_) {
// todo log error
glfwTerminate();
return;
}
glfwMakeContextCurrent(gl_context_);
gladLoadGL();
#endif
}
void GPUPixelContext::useAsCurrent() {
#if defined(GPUPIXEL_IOS)
if ([EAGLContext currentContext] != _eglContext) {
[EAGLContext setCurrentContext:_eglContext];
}
#elif defined(GPUPIXEL_MAC)
if ([NSOpenGLContext currentContext] != imageProcessingContext) {
[imageProcessingContext makeCurrentContext];
}
#elif defined(GPUPIXEL_ANDROID)
if (!eglMakeCurrent(m_gpu_context->egldisplay, m_gpu_context->eglsurface,
m_gpu_context->eglsurface, m_gpu_context->eglcontext)) {
// err_log("Set Current Context Error.");
Util::Log("ERROR", "Set Current Context Error!");
}
#elif defined(GPUPIXEL_WIN) || defined(GPUPIXEL_LINUX)
if (glfwGetCurrentContext() != gl_context_) {
glfwMakeContextCurrent(gl_context_);
}
#endif
}
void GPUPixelContext::presentBufferForDisplay() {
#if defined(GPUPIXEL_IOS)
[_eglContext presentRenderbuffer:GL_RENDERBUFFER];
#elif defined(GPUPIXEL_MAC)
#endif
}
void GPUPixelContext::releaseContext() {
#if defined(GPUPIXEL_WIN) || defined(GPUPIXEL_LINUX)
if (gl_context_) {
glfwDestroyWindow(gl_context_);
}
glfwTerminate();
#elif defined(GPUPIXEL_ANDROID)
if (!context_inited) {
return;
}
context_inited = false;
if (m_gpu_context != nullptr && m_gpu_context->egldisplay != EGL_NO_DISPLAY) {
if (m_gpu_context->eglcontext != EGL_NO_CONTEXT) {
eglDestroyContext(m_gpu_context->egldisplay, m_gpu_context->eglcontext);
}
if (m_gpu_context->eglsurface != EGL_NO_SURFACE) {
eglDestroySurface(m_gpu_context->egldisplay, m_gpu_context->eglsurface);
}
eglMakeCurrent(m_gpu_context->egldisplay, EGL_NO_SURFACE, EGL_NO_SURFACE,
EGL_NO_CONTEXT);
if (!eglTerminate(m_gpu_context->egldisplay)) {
// err_log("Free egldisplay error!");
Util::Log("ERROR", "Free egldisplay Error!");
}
}
if (m_gpu_context != nullptr) {
delete m_gpu_context;
m_gpu_context = nullptr;
}
#endif
}
void GPUPixelContext::runSync(std::function<void(void)> func) {
// todo fix android
#if defined(GPUPIXEL_ANDROID)
func();
#else
task_queue_->add([=]() {
useAsCurrent();
func();
});
task_queue_->processOne();
#endif
}
NS_GPUPIXEL_END