@blotch/angular-lynx-native 22.2.8 → 22.2.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -0
- package/package.json +2 -1
- package/templates/android/README.md +6 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/AssetsModule.kt +41 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/CanvasUI.kt +76 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/MainActivity.kt +9 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/WebGLContext.kt +1442 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/WebGLModule.kt +220 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/WebGLOps.kt +211 -0
- package/templates/ios/App/AngularLynxWebGL.h +80 -0
- package/templates/ios/App/AngularLynxWebGL.m +338 -0
- package/templates/ios/App/AngularLynxWebGLContext.m +2220 -0
- package/templates/ios/App/AngularLynxWebGLOps.h +209 -0
- package/templates/ios/App/AssetsModule.swift +53 -0
- package/templates/ios/App/LynxViewController.swift +11 -0
- package/templates/ios/App/XElementRegistry.swift +6 -1
|
@@ -0,0 +1,338 @@
|
|
|
1
|
+
#import "AngularLynxWebGL.h"
|
|
2
|
+
|
|
3
|
+
#import <ImageIO/ImageIO.h>
|
|
4
|
+
#import <Lynx/LynxPropsProcessor.h>
|
|
5
|
+
#import <QuartzCore/QuartzCore.h>
|
|
6
|
+
|
|
7
|
+
#pragma mark - Registry
|
|
8
|
+
|
|
9
|
+
@implementation AngularLynxCanvasRegistry
|
|
10
|
+
|
|
11
|
+
// Weak both ways: the view belongs to Lynx, the context to the module.
|
|
12
|
+
static NSMapTable<NSString *, AngularLynxCanvasView *> *views(void) {
|
|
13
|
+
static NSMapTable *table;
|
|
14
|
+
static dispatch_once_t once;
|
|
15
|
+
dispatch_once(&once, ^{
|
|
16
|
+
table = [NSMapTable strongToWeakObjectsMapTable];
|
|
17
|
+
});
|
|
18
|
+
return table;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
static NSMapTable<NSString *, AngularLynxWebGLContext *> *contexts(void) {
|
|
22
|
+
static NSMapTable *table;
|
|
23
|
+
static dispatch_once_t once;
|
|
24
|
+
dispatch_once(&once, ^{
|
|
25
|
+
table = [NSMapTable strongToWeakObjectsMapTable];
|
|
26
|
+
});
|
|
27
|
+
return table;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
+ (void)setView:(AngularLynxCanvasView *)view forCanvasId:(NSString *)canvasId {
|
|
31
|
+
if (view) {
|
|
32
|
+
[views() setObject:view forKey:canvasId];
|
|
33
|
+
} else {
|
|
34
|
+
[views() removeObjectForKey:canvasId];
|
|
35
|
+
}
|
|
36
|
+
[[contexts() objectForKey:canvasId] attachView:view];
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
+ (AngularLynxCanvasView *)viewForCanvasId:(NSString *)canvasId {
|
|
40
|
+
return [views() objectForKey:canvasId];
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
+ (void)setContext:(AngularLynxWebGLContext *)context forCanvasId:(NSString *)canvasId {
|
|
44
|
+
if (context) {
|
|
45
|
+
[contexts() setObject:context forKey:canvasId];
|
|
46
|
+
[context attachView:[views() objectForKey:canvasId]];
|
|
47
|
+
} else {
|
|
48
|
+
[contexts() removeObjectForKey:canvasId];
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
+ (AngularLynxWebGLContext *)contextForCanvasId:(NSString *)canvasId {
|
|
53
|
+
return [contexts() objectForKey:canvasId];
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
@end
|
|
57
|
+
|
|
58
|
+
#pragma mark - Decoded images
|
|
59
|
+
|
|
60
|
+
@implementation AngularLynxDecodedImage
|
|
61
|
+
|
|
62
|
+
static NSMutableDictionary<NSNumber *, AngularLynxDecodedImage *> *decodedImages(void) {
|
|
63
|
+
static NSMutableDictionary *images;
|
|
64
|
+
static dispatch_once_t once;
|
|
65
|
+
dispatch_once(&once, ^{
|
|
66
|
+
images = [NSMutableDictionary dictionary];
|
|
67
|
+
});
|
|
68
|
+
return images;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
- (instancetype)initWithPixels:(NSData *)pixels
|
|
72
|
+
width:(NSInteger)width
|
|
73
|
+
height:(NSInteger)height
|
|
74
|
+
premultiplied:(BOOL)premultiplied {
|
|
75
|
+
if ((self = [super init])) {
|
|
76
|
+
_pixels = pixels;
|
|
77
|
+
_width = width;
|
|
78
|
+
_height = height;
|
|
79
|
+
_premultiplied = premultiplied;
|
|
80
|
+
}
|
|
81
|
+
return self;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
+ (AngularLynxDecodedImage *)imageWithId:(NSInteger)imageId {
|
|
85
|
+
return decodedImages()[@(imageId)];
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
@end
|
|
89
|
+
|
|
90
|
+
/// The bytes the runtime sends as 32-bit words, with the byte count last (see
|
|
91
|
+
/// toWords in webgl-bridge.ts). Every iOS device is little-endian, so the
|
|
92
|
+
/// words land in memory as the bytes were.
|
|
93
|
+
NSData *AngularLynxBytesFromWords(NSArray *words) {
|
|
94
|
+
NSUInteger count = words.count;
|
|
95
|
+
if (count == 0) return [NSData data];
|
|
96
|
+
NSUInteger byteLength = [words[count - 1] unsignedIntegerValue];
|
|
97
|
+
NSUInteger wordCount = count - 1;
|
|
98
|
+
NSMutableData *data = [NSMutableData dataWithLength:wordCount * 4];
|
|
99
|
+
uint32_t *out = data.mutableBytes;
|
|
100
|
+
__unsafe_unretained id *objects =
|
|
101
|
+
(__unsafe_unretained id *)malloc(sizeof(id) * (wordCount ? wordCount : 1));
|
|
102
|
+
[words getObjects:objects range:NSMakeRange(0, wordCount)];
|
|
103
|
+
for (NSUInteger i = 0; i < wordCount; i++) {
|
|
104
|
+
out[i] = (uint32_t)[(NSNumber *)objects[i] unsignedLongLongValue];
|
|
105
|
+
}
|
|
106
|
+
free(objects);
|
|
107
|
+
data.length = MIN(byteLength, wordCount * 4);
|
|
108
|
+
return data;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/// Decodes an image file to RGBA bytes, as createImageBitmap's options ask:
|
|
112
|
+
/// `imageOrientation: 'flipY'` flips it, `premultiplyAlpha: 'premultiply'`
|
|
113
|
+
/// keeps CoreGraphics' premultiplied pixels (otherwise they're divided back),
|
|
114
|
+
/// and `resizeWidth`/`resizeHeight` scale it.
|
|
115
|
+
static AngularLynxDecodedImage *decodeImage(NSData *bytes, NSDictionary *options,
|
|
116
|
+
NSString **error) {
|
|
117
|
+
CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef)bytes, NULL);
|
|
118
|
+
CGImageRef image = source ? CGImageSourceCreateImageAtIndex(source, 0, NULL) : NULL;
|
|
119
|
+
if (source) CFRelease(source);
|
|
120
|
+
if (!image) {
|
|
121
|
+
*error = @"The data is not an image iOS can decode";
|
|
122
|
+
return nil;
|
|
123
|
+
}
|
|
124
|
+
size_t width = CGImageGetWidth(image);
|
|
125
|
+
size_t height = CGImageGetHeight(image);
|
|
126
|
+
NSNumber *resizeWidth = options[@"resizeWidth"];
|
|
127
|
+
NSNumber *resizeHeight = options[@"resizeHeight"];
|
|
128
|
+
if ([resizeWidth isKindOfClass:NSNumber.class] && resizeWidth.integerValue > 0) {
|
|
129
|
+
size_t w = resizeWidth.unsignedIntegerValue;
|
|
130
|
+
height = [resizeHeight isKindOfClass:NSNumber.class] && resizeHeight.integerValue > 0
|
|
131
|
+
? resizeHeight.unsignedIntegerValue
|
|
132
|
+
: MAX(1, (size_t)llround((double)height * w / width));
|
|
133
|
+
width = w;
|
|
134
|
+
} else if ([resizeHeight isKindOfClass:NSNumber.class] && resizeHeight.integerValue > 0) {
|
|
135
|
+
size_t h = resizeHeight.unsignedIntegerValue;
|
|
136
|
+
width = MAX(1, (size_t)llround((double)width * h / height));
|
|
137
|
+
height = h;
|
|
138
|
+
}
|
|
139
|
+
NSMutableData *pixels = [NSMutableData dataWithLength:width * height * 4];
|
|
140
|
+
CGColorSpaceRef colorSpace = CGColorSpaceCreateWithName(kCGColorSpaceSRGB);
|
|
141
|
+
// CoreGraphics draws RGBA only premultiplied.
|
|
142
|
+
CGContextRef context = CGBitmapContextCreate(
|
|
143
|
+
pixels.mutableBytes, width, height, 8, width * 4, colorSpace,
|
|
144
|
+
kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
|
|
145
|
+
CGColorSpaceRelease(colorSpace);
|
|
146
|
+
if (!context) {
|
|
147
|
+
CGImageRelease(image);
|
|
148
|
+
*error = @"Could not allocate the image's pixels";
|
|
149
|
+
return nil;
|
|
150
|
+
}
|
|
151
|
+
CGContextSetBlendMode(context, kCGBlendModeCopy);
|
|
152
|
+
CGContextSetInterpolationQuality(context, kCGInterpolationHigh);
|
|
153
|
+
// A bitmap context's first row in memory is the top of what's drawn, so
|
|
154
|
+
// drawing as is gives rows top to bottom; flipping the context gives them
|
|
155
|
+
// bottom to top.
|
|
156
|
+
if ([options[@"imageOrientation"] isEqual:@"flipY"]) {
|
|
157
|
+
CGContextTranslateCTM(context, 0, height);
|
|
158
|
+
CGContextScaleCTM(context, 1, -1);
|
|
159
|
+
}
|
|
160
|
+
CGContextDrawImage(context, CGRectMake(0, 0, width, height), image);
|
|
161
|
+
CGContextRelease(context);
|
|
162
|
+
CGImageRelease(image);
|
|
163
|
+
BOOL premultiply = [options[@"premultiplyAlpha"] isEqual:@"premultiply"];
|
|
164
|
+
if (!premultiply) {
|
|
165
|
+
uint8_t *p = pixels.mutableBytes;
|
|
166
|
+
for (size_t i = 0; i < width * height * 4; i += 4) {
|
|
167
|
+
uint8_t alpha = p[i + 3];
|
|
168
|
+
if (alpha != 0 && alpha != 255) {
|
|
169
|
+
p[i] = (uint8_t)MIN(255, (p[i] * 255 + alpha / 2) / alpha);
|
|
170
|
+
p[i + 1] = (uint8_t)MIN(255, (p[i + 1] * 255 + alpha / 2) / alpha);
|
|
171
|
+
p[i + 2] = (uint8_t)MIN(255, (p[i + 2] * 255 + alpha / 2) / alpha);
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
return [[AngularLynxDecodedImage alloc] initWithPixels:pixels
|
|
176
|
+
width:width
|
|
177
|
+
height:height
|
|
178
|
+
premultiplied:premultiply];
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
#pragma mark - Module
|
|
182
|
+
|
|
183
|
+
@implementation AngularLynxWebGLModule {
|
|
184
|
+
NSMutableDictionary<NSNumber *, AngularLynxWebGLContext *> *_contexts;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
+ (NSString *)name {
|
|
188
|
+
return @"AngularLynxWebGL";
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
+ (NSDictionary<NSString *, NSString *> *)methodLookup {
|
|
192
|
+
return @{
|
|
193
|
+
@"create" : NSStringFromSelector(@selector(create:canvasId:attributes:)),
|
|
194
|
+
@"run" : NSStringFromSelector(@selector(run:commands:)),
|
|
195
|
+
@"destroy" : NSStringFromSelector(@selector(destroy:)),
|
|
196
|
+
@"decodeImage" : NSStringFromSelector(@selector(decodeImage:bytes:options:)),
|
|
197
|
+
@"releaseImage" : NSStringFromSelector(@selector(releaseImage:)),
|
|
198
|
+
};
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
- (instancetype)init {
|
|
202
|
+
if ((self = [super init])) {
|
|
203
|
+
_contexts = [NSMutableDictionary dictionary];
|
|
204
|
+
}
|
|
205
|
+
return self;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
- (instancetype)initWithParam:(id)param {
|
|
209
|
+
return [self init];
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
- (void)dealloc {
|
|
213
|
+
for (AngularLynxWebGLContext *context in _contexts.allValues) {
|
|
214
|
+
[context destroy];
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
- (NSDictionary *)create:(NSNumber *)contextId
|
|
219
|
+
canvasId:(NSString *)canvasId
|
|
220
|
+
attributes:(NSDictionary *)attributes {
|
|
221
|
+
[_contexts[contextId] destroy];
|
|
222
|
+
NSString *error = nil;
|
|
223
|
+
AngularLynxWebGLContext *context =
|
|
224
|
+
[[AngularLynxWebGLContext alloc] initWithCanvasId:canvasId
|
|
225
|
+
attributes:attributes ?: @{}
|
|
226
|
+
error:&error];
|
|
227
|
+
if (!context) {
|
|
228
|
+
return @{@"ok" : @NO, @"error" : error ?: @"OpenGL ES 3.0 is not available"};
|
|
229
|
+
}
|
|
230
|
+
_contexts[contextId] = context;
|
|
231
|
+
[AngularLynxCanvasRegistry setContext:context forCanvasId:canvasId];
|
|
232
|
+
return @{@"ok" : @YES};
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
- (NSArray *)run:(NSNumber *)contextId commands:(NSArray *)commands {
|
|
236
|
+
AngularLynxWebGLContext *context = _contexts[contextId];
|
|
237
|
+
if (!context || ![commands isKindOfClass:NSArray.class]) return @[];
|
|
238
|
+
return [context run:commands];
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
- (NSNumber *)destroy:(NSNumber *)contextId {
|
|
242
|
+
AngularLynxWebGLContext *context = _contexts[contextId];
|
|
243
|
+
if (context) {
|
|
244
|
+
[AngularLynxCanvasRegistry setContext:nil forCanvasId:context.canvasId];
|
|
245
|
+
[context destroy];
|
|
246
|
+
[_contexts removeObjectForKey:contextId];
|
|
247
|
+
}
|
|
248
|
+
return @YES;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
- (NSDictionary *)decodeImage:(NSNumber *)imageId
|
|
252
|
+
bytes:(NSArray *)bytes
|
|
253
|
+
options:(NSDictionary *)options {
|
|
254
|
+
NSString *error = nil;
|
|
255
|
+
AngularLynxDecodedImage *image =
|
|
256
|
+
[bytes isKindOfClass:NSArray.class]
|
|
257
|
+
? decodeImage(AngularLynxBytesFromWords(bytes),
|
|
258
|
+
[options isKindOfClass:NSDictionary.class] ? options : @{}, &error)
|
|
259
|
+
: nil;
|
|
260
|
+
if (!image) {
|
|
261
|
+
return @{@"width" : @0, @"height" : @0, @"error" : error ?: @"No image data"};
|
|
262
|
+
}
|
|
263
|
+
decodedImages()[imageId] = image;
|
|
264
|
+
return @{@"width" : @(image.width), @"height" : @(image.height)};
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
- (NSNumber *)releaseImage:(NSNumber *)imageId {
|
|
268
|
+
[decodedImages() removeObjectForKey:imageId];
|
|
269
|
+
return @YES;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
@end
|
|
273
|
+
|
|
274
|
+
#pragma mark - Canvas
|
|
275
|
+
|
|
276
|
+
@implementation AngularLynxCanvasView
|
|
277
|
+
|
|
278
|
+
+ (Class)layerClass {
|
|
279
|
+
return [CAEAGLLayer class];
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
- (instancetype)initWithFrame:(CGRect)frame {
|
|
283
|
+
if ((self = [super initWithFrame:frame])) {
|
|
284
|
+
CAEAGLLayer *layer = (CAEAGLLayer *)self.layer;
|
|
285
|
+
// A new drawable each frame, as WebGL's preserveDrawingBuffer: false.
|
|
286
|
+
layer.drawableProperties = @{
|
|
287
|
+
kEAGLDrawablePropertyRetainedBacking : @NO,
|
|
288
|
+
kEAGLDrawablePropertyColorFormat : kEAGLColorFormatRGBA8,
|
|
289
|
+
};
|
|
290
|
+
layer.opaque = NO;
|
|
291
|
+
self.opaque = NO;
|
|
292
|
+
self.contentScaleFactor = UIScreen.mainScreen.scale;
|
|
293
|
+
// Lynx dispatches touches itself, from the LynxView.
|
|
294
|
+
self.userInteractionEnabled = NO;
|
|
295
|
+
}
|
|
296
|
+
return self;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
- (void)setCanvasId:(NSString *)canvasId {
|
|
300
|
+
if ([_canvasId isEqualToString:canvasId]) return;
|
|
301
|
+
if (_canvasId && [AngularLynxCanvasRegistry viewForCanvasId:_canvasId] == self) {
|
|
302
|
+
[AngularLynxCanvasRegistry setView:nil forCanvasId:_canvasId];
|
|
303
|
+
}
|
|
304
|
+
_canvasId = [canvasId copy];
|
|
305
|
+
if (_canvasId) [AngularLynxCanvasRegistry setView:self forCanvasId:_canvasId];
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
- (void)layoutSubviews {
|
|
309
|
+
[super layoutSubviews];
|
|
310
|
+
if (_canvasId) [[AngularLynxCanvasRegistry contextForCanvasId:_canvasId] viewDidResize];
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
- (void)dealloc {
|
|
314
|
+
// The registry holds the view weakly; a context still showing frames in it
|
|
315
|
+
// stops.
|
|
316
|
+
NSString *canvasId = _canvasId;
|
|
317
|
+
if (canvasId) {
|
|
318
|
+
dispatch_async(dispatch_get_main_queue(), ^{
|
|
319
|
+
if (![AngularLynxCanvasRegistry viewForCanvasId:canvasId]) {
|
|
320
|
+
[[AngularLynxCanvasRegistry contextForCanvasId:canvasId] attachView:nil];
|
|
321
|
+
}
|
|
322
|
+
});
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
@end
|
|
327
|
+
|
|
328
|
+
@implementation AngularLynxCanvas
|
|
329
|
+
|
|
330
|
+
- (AngularLynxCanvasView *)createView {
|
|
331
|
+
return [[AngularLynxCanvasView alloc] initWithFrame:CGRectZero];
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
LYNX_PROP_SETTER("canvas-id", setCanvasId, NSString *) {
|
|
335
|
+
self.view.canvasId = requestReset || ![value isKindOfClass:NSString.class] ? nil : value;
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
@end
|