Debian Codegraph @

platform/ios/display_layer_ios.mm

raw
1 /**************************************************************************/
2 /* display_layer_ios.mm */
3 /**************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /**************************************************************************/
8 /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9 /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10 /* */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the */
13 /* "Software"), to deal in the Software without restriction, including */
14 /* without limitation the rights to use, copy, modify, merge, publish, */
15 /* distribute, sublicense, and/or sell copies of the Software, and to */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions: */
18 /* */
19 /* The above copyright notice and this permission notice shall be */
20 /* included in all copies or substantial portions of the Software. */
21 /* */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29 /**************************************************************************/
30
31 #import "display_layer_ios.h"
32
33 #import "display_server_ios.h"
34 #import "os_ios.h"
35
36 #include "core/config/project_settings.h"
37 #include "core/os/keyboard.h"
38 #include "main/main.h"
39 #include "servers/audio/audio_server.h"
40
41 #import <AudioToolbox/AudioServices.h>
42 #import <GameController/GameController.h>
43 #import <OpenGLES/EAGL.h>
44 #import <OpenGLES/ES1/gl.h>
45 #import <OpenGLES/ES1/glext.h>
46 #import <QuartzCore/QuartzCore.h>
47 #import <UIKit/UIKit.h>
48
49 @implementation GDTMetalLayer
50
51 - (void)initializeDisplayLayer {
52 #if defined(TARGET_OS_SIMULATOR) && TARGET_OS_SIMULATOR
53 if (@available(iOS 13, *)) {
54 // Simulator supports Metal since iOS 13
55 } else {
56 NSLog(@"iOS Simulator prior to iOS 13 does not support Metal rendering.");
57 }
58 #endif
59 }
60
61 - (void)layoutDisplayLayer {
62 }
63
64 - (void)startRenderDisplayLayer {
65 }
66
67 - (void)stopRenderDisplayLayer {
68 }
69
70 @end
71
72 @implementation GDTOpenGLLayer {
73 // The pixel dimensions of the backbuffer
74 GLint backingWidth;
75 GLint backingHeight;
76
77 EAGLContext *context;
78 GLuint viewRenderbuffer, viewFramebuffer;
79 GLuint depthRenderbuffer;
80 }
81
82 - (void)initializeDisplayLayer {
83 // Configure it so that it is opaque, does not retain the contents of the backbuffer when displayed, and uses RGBA8888 color.
84 self.opaque = YES;
85 self.drawableProperties = [NSDictionary
86 dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:FALSE],
87 kEAGLDrawablePropertyRetainedBacking,
88 kEAGLColorFormatRGBA8,
89 kEAGLDrawablePropertyColorFormat,
90 nil];
91
92 // Create GL ES 3 context
93 if (GLOBAL_GET("rendering/renderer/rendering_method") == "gl_compatibility") {
94 context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES3];
95 NSLog(@"Setting up an OpenGL ES 3.0 context.");
96 if (!context) {
97 NSLog(@"Failed to create OpenGL ES 3.0 context!");
98 return;
99 }
100 }
101
102 if (![EAGLContext setCurrentContext:context]) {
103 NSLog(@"Failed to set EAGLContext!");
104 return;
105 }
106 if (![self createFramebuffer]) {
107 NSLog(@"Failed to create frame buffer!");
108 return;
109 }
110 }
111
112 - (void)layoutDisplayLayer {
113 [EAGLContext setCurrentContext:context];
114 [self destroyFramebuffer];
115 [self createFramebuffer];
116 }
117
118 - (void)startRenderDisplayLayer {
119 [EAGLContext setCurrentContext:context];
120
121 glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);
122 }
123
124 - (void)stopRenderDisplayLayer {
125 glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
126 [context presentRenderbuffer:GL_RENDERBUFFER_OES];
127
128 #ifdef DEBUG_ENABLED
129 GLenum err = glGetError();
130 if (err) {
131 NSLog(@"DrawView: %x error", err);
132 }
133 #endif
134 }
135
136 - (void)dealloc {
137 if ([EAGLContext currentContext] == context) {
138 [EAGLContext setCurrentContext:nil];
139 }
140
141 if (context) {
142 context = nil;
143 }
144 }
145
146 - (BOOL)createFramebuffer {
147 glGenFramebuffersOES(1, &viewFramebuffer);
148 glGenRenderbuffersOES(1, &viewRenderbuffer);
149
150 glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);
151 glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
152 // This call associates the storage for the current render buffer with the EAGLDrawable (our CAself)
153 // allowing us to draw into a buffer that will later be rendered to screen wherever the layer is (which corresponds with our view).
154 [context renderbufferStorage:GL_RENDERBUFFER_OES fromDrawable:(id<EAGLDrawable>)self];
155 glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, viewRenderbuffer);
156
157 glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_WIDTH_OES, &backingWidth);
158 glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_HEIGHT_OES, &backingHeight);
159
160 // For this sample, we also need a depth buffer, so we'll create and attach one via another renderbuffer.
161 glGenRenderbuffersOES(1, &depthRenderbuffer);
162 glBindRenderbufferOES(GL_RENDERBUFFER_OES, depthRenderbuffer);
163 glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_DEPTH_COMPONENT16_OES, backingWidth, backingHeight);
164 glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_DEPTH_ATTACHMENT_OES, GL_RENDERBUFFER_OES, depthRenderbuffer);
165
166 if (glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES) != GL_FRAMEBUFFER_COMPLETE_OES) {
167 NSLog(@"failed to make complete framebuffer object %x", glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES));
168 return NO;
169 }
170
171 GLES3::TextureStorage::system_fbo = viewFramebuffer;
172
173 return YES;
174 }
175
176 // Clean up any buffers we have allocated.
177 - (void)destroyFramebuffer {
178 GLES3::TextureStorage::system_fbo = 0;
179
180 glDeleteFramebuffersOES(1, &viewFramebuffer);
181 viewFramebuffer = 0;
182 glDeleteRenderbuffersOES(1, &viewRenderbuffer);
183 viewRenderbuffer = 0;
184
185 if (depthRenderbuffer) {
186 glDeleteRenderbuffersOES(1, &depthRenderbuffer);
187 depthRenderbuffer = 0;
188 }
189 }
190
191 @end