-
-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathMGLKView.mm
More file actions
252 lines (214 loc) · 6.02 KB
/
MGLKView.mm
File metadata and controls
252 lines (214 loc) · 6.02 KB
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
//
// Copyright 2019 Le Hoang Quyen. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
#import "MGLKView+Private.h"
#import "MGLKViewController+Private.h"
#include <libGLESv2/entry_points_gles_2_0_autogen.h>
namespace
{
void Throw(NSString *msg)
{
[NSException raise:@"MGLSurfaceException" format:@"%@", msg];
}
}
@implementation MGLKView
- (id)initWithCoder:(NSCoder *)coder
{
if (self = [super initWithCoder:coder])
{
#if TARGET_OS_OSX
self.wantsLayer = YES;
self.autoresizingMask = kCALayerWidthSizable | kCALayerHeightSizable;
#else
self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
#endif
}
return self;
}
- (id)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame])
{
#if TARGET_OS_OSX
self.wantsLayer = YES;
self.autoresizingMask = kCALayerWidthSizable | kCALayerHeightSizable;
#else
self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
#endif
}
return self;
}
- (void)dealloc
{
_context = nil;
}
#if TARGET_OS_OSX
- (CALayer *)makeBackingLayer
{
return [MGLLayer layer];
}
#else
+ (Class)layerClass
{
return MGLLayer.class;
}
#endif
- (MGLLayer *)glLayer
{
_glLayer = static_cast<MGLLayer *>(self.layer);
return _glLayer;
}
- (void)setContext:(MGLContext *)context
{
if (_drawing)
{
Throw(@"Changing GL context when drawing is not allowed");
}
_context = context;
}
- (void)setRetainedBacking:(BOOL)retainedBacking
{
self.glLayer.retainedBacking = _retainedBacking = retainedBacking;
}
- (void)setDrawableColorFormat:(MGLDrawableColorFormat)drawableColorFormat
{
self.glLayer.drawableColorFormat = _drawableColorFormat = drawableColorFormat;
}
- (void)setDrawableDepthFormat:(MGLDrawableDepthFormat)drawableDepthFormat
{
self.glLayer.drawableDepthFormat = _drawableDepthFormat = drawableDepthFormat;
}
- (void)setDrawableStencilFormat:(MGLDrawableStencilFormat)drawableStencilFormat
{
self.glLayer.drawableStencilFormat = _drawableStencilFormat = drawableStencilFormat;
}
- (void)setDrawableMultisample:(MGLDrawableMultisample)drawableMultisample
{
self.glLayer.drawableMultisample = _drawableMultisample = drawableMultisample;
}
- (void)setDrawableColorSpace:(MGLDrawableColorSpace)drawableColorSpace
{
self.glLayer.drawableColorSpace = _drawableColorSpace = drawableColorSpace;
}
- (void)display
{
[self drawRect:self.bounds];
}
- (CGSize)drawableSize
{
if (!self.layer)
{
CGSize zero = {0};
return zero;
}
return self.glLayer.drawableSize;
}
#if TARGET_OS_OSX
- (void)viewDidMoveToWindow
{
[super viewDidMoveToWindow];
#else
- (void)didMoveToWindow
{
[super didMoveToWindow];
#endif
// notify view controller
if (_controller)
{
[_controller viewDidMoveToWindow];
}
}
- (void)drawRect:(CGRect)rect
{
[self drawRect:rect andCapture:nullptr];
}
- (void)drawRect:(CGRect)rect andCapture:(uint8_t **)pPixels
{
_drawing = YES;
if (_context)
{
if (![MGLContext setCurrentContext:_context forLayer:self.glLayer])
{
Throw(@"Failed to setCurrentContext");
}
}
if (_delegate)
{
[_delegate mglkView:self drawInRect:rect];
}
if (pPixels)
{
// Frame capture request
int width = static_cast<int>(self.drawableSize.width);
int height = static_cast<int>(self.drawableSize.height);
if (width && height)
{
// Capture framebuffer's content
*pPixels = new uint8_t[4 * width * height];
GLint prevPackAlignment;
gl::GetIntegerv(GL_PACK_ALIGNMENT, &prevPackAlignment);
gl::PixelStorei(GL_PACK_ALIGNMENT, 1);
// Clear errors
gl::GetError();
gl::ReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, *pPixels);
gl::PixelStorei(GL_PACK_ALIGNMENT, prevPackAlignment);
// Clear errors
gl::GetError();
}
else
{
*pPixels = nullptr;
}
} // if (pPixels)
if (![_context present:self.glLayer])
{
Throw(@"Failed to present framebuffer");
}
_drawing = NO;
}
#if TARGET_OS_IOS || TARGET_OS_TV
static void freeImageData(void *info, const void *data, size_t size)
{
delete[] static_cast<uint8_t *>(info);
}
- (UIImage *)snapshot
{
uint8_t *pixels = nullptr;
[self drawRect:self.bounds andCapture:&pixels];
if (!pixels)
{
return nil;
}
int width = static_cast<int>(self.drawableSize.width);
int height = static_cast<int>(self.drawableSize.height);
size_t dataLen = width * height * 4;
uint8_t *flippedPixels = new uint8_t[dataLen];
for (int y1 = 0; y1 < height; y1++)
{
for (int x1 = 0; x1 < width * 4; x1++)
{
flippedPixels[(height - 1 - y1) * width * 4 + x1] = pixels[y1 * 4 * width + x1];
}
}
delete[] pixels;
CGDataProviderRef provider =
CGDataProviderCreateWithData(flippedPixels, flippedPixels, dataLen, freeImageData);
int bitsPerComponent = 8;
int bitsPerPixel = 32;
int bytesPerRow = 4 * width;
CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault | kCGImageAlphaNoneSkipLast;
CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;
CGImageRef imageRef =
CGImageCreate(width, height, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpaceRef,
bitmapInfo, provider, NULL, NO, renderingIntent);
CGColorSpaceRelease(colorSpaceRef);
CGDataProviderRelease(provider);
UIImage *image = [UIImage imageWithCGImage:imageRef scale:1 orientation:UIImageOrientationUp];
CGImageRelease(imageRef);
return image;
}
#endif // TARGET_OS_IOS || TARGET_OS_TV
@end