blob: 1dc8e6fe9520845ccc023fdbae20dac9403e8f35 [file] [log] [blame]
Hao Lu3617d5b2017-10-23 15:16:50 -07001#import <OpenGLES/EAGL.h>
2#import <OpenGLES/ES2/gl.h>
3
4#import <cpuinfo.h>
5#import <log.h>
6#import <gpu/api.h>
7
8
Marat Dukhan02527012017-10-23 16:03:05 -07009#if ! __has_feature(objc_arc)
10 #error "This file must be built with Automatic Reference Counting (-fobjc-arc option)"
11#endif
12
Hao Lu3617d5b2017-10-23 15:16:50 -070013void cpuinfo_gpu_ios_query_gles2(char gpu_name[restrict static CPUINFO_GPU_NAME_MAX]) {
Marat Dukhan41a7e352017-10-23 16:11:31 -070014 EAGLContext *const existing_context = [EAGLContext currentContext];
15 EAGLContext *new_context = nil;
16 if (!existing_context) {
Hao Lu3617d5b2017-10-23 15:16:50 -070017 /* No existing context: create new context */
18
19 /* OpenGL ES 2.0 is supported by iPhone 3GS and up */
Marat Dukhan41a7e352017-10-23 16:11:31 -070020 new_context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
21 if (!new_context) {
Hao Lu3617d5b2017-10-23 15:16:50 -070022 cpuinfo_log_warning("failed to initialize OpenGLES context");
23 }
24
25 /* Set context */
Marat Dukhan41a7e352017-10-23 16:11:31 -070026 if (![EAGLContext setCurrentContext:new_context]) {
Hao Lu3617d5b2017-10-23 15:16:50 -070027 cpuinfo_log_warning("failed to set current OpenGLES context");
28 }
29 }
Marat Dukhan41a7e352017-10-23 16:11:31 -070030
Marat Dukhan03f5c0b2017-10-25 16:09:39 -070031 const char* renderer_str = (const char*) glGetString(GL_RENDERER);
32 if (renderer_str != NULL) {
Hao Lu3617d5b2017-10-23 15:16:50 -070033 strncpy(gpu_name, renderer_str, CPUINFO_GPU_NAME_MAX - 1);
34 gpu_name[CPUINFO_GPU_NAME_MAX - 1] = '\0';
35 cpuinfo_log_debug("GL_RENDERER: %s", renderer_str);
36 } else {
Marat Dukhan41a7e352017-10-23 16:11:31 -070037 cpuinfo_log_warning("failed to get GL_RENDERER for OpenGLES context");
Hao Lu3617d5b2017-10-23 15:16:50 -070038 }
39
Marat Dukhan41a7e352017-10-23 16:11:31 -070040 if (new_context) {
41 /* Reset context back to the original one */
42 if (![EAGLContext setCurrentContext:nil]) {
Hao Lu3617d5b2017-10-23 15:16:50 -070043 cpuinfo_log_warning("failed to reset OpenGLES context");
44 }
Hao Lu3617d5b2017-10-23 15:16:50 -070045 }
46}