Commit 84261f28 authored by Gaëtan Renaudeau's avatar Gaëtan Renaudeau

Implement imagesToPreload support (GL.View preload prop)

parent 354af388
...@@ -29,13 +29,12 @@ function directionForPass (p, factor, total) { ...@@ -29,13 +29,12 @@ function directionForPass (p, factor, total) {
class Blur extends GL.Component { class Blur extends GL.Component {
render () { render () {
const { width, height, factor, children, passes } = this.props; const { width, height, factor, children, passes } = this.props;
const rec = p => p <= 0 ? children : const rec = p => p <= 0 ? children :
<Blur1D width={width} height={height} direction={directionForPass(p, factor, passes)}> <Blur1D width={width} height={height} direction={directionForPass(p, factor, passes)}>
{rec(p-1)} {rec(p-1)}
</Blur1D>; </Blur1D>;
return rec(passes); return rec(passes || 0);
} }
} }
......
...@@ -34,7 +34,7 @@ const Tests = React.createClass({ ...@@ -34,7 +34,7 @@ const Tests = React.createClass({
</Text>)} </Text>)}
</View>; </View>;
const img = "http://i.imgur.com/zJIxPEo.jpg"; const img = "http://i.imgur.com/zJIxPEo.jpg?t="+Date.now();
const blurredImage = const blurredImage =
<Blur factor={4} passes={6} width={200} height={200}> <Blur factor={4} passes={6} width={200} height={200}>
......
...@@ -15,7 +15,7 @@ ...@@ -15,7 +15,7 @@
@implementation GLCanvas @implementation GLCanvas
{ {
RCTBridge *_bridge; // bridge is required to instanciate GLReactImage RCTBridge *_bridge; // bridge is required to instanciate GLReactImage
GLRenderData *_renderData; GLRenderData *_renderData;
NSArray *_contentTextures; NSArray *_contentTextures;
...@@ -26,6 +26,9 @@ ...@@ -26,6 +26,9 @@
BOOL _deferredRendering; // This flag indicates a render has been deferred to the next frame (when using contents) BOOL _deferredRendering; // This flag indicates a render has been deferred to the next frame (when using contents)
GLint defaultFBO; GLint defaultFBO;
NSMutableArray *_preloaded;
BOOL _preloadingDone;
} }
- (instancetype)initWithBridge:(RCTBridge *)bridge - (instancetype)initWithBridge:(RCTBridge *)bridge
...@@ -34,6 +37,8 @@ ...@@ -34,6 +37,8 @@
if ((self = [super init])) { if ((self = [super init])) {
_bridge = bridge; _bridge = bridge;
_images = @{}; _images = @{};
_preloaded = [[NSMutableArray alloc] init];
_preloadingDone = false;
self.context = context; self.context = context;
} }
return self; return self;
...@@ -41,6 +46,14 @@ ...@@ -41,6 +46,14 @@
RCT_NOT_IMPLEMENTED(-init) RCT_NOT_IMPLEMENTED(-init)
-(void)setImagesToPreload:(NSArray *)imagesToPreload
{
if (_preloadingDone) return;
if ([imagesToPreload count] == 0) {
_preloadingDone = true;
}
_imagesToPreload = imagesToPreload;
}
- (void)setOpaque:(BOOL)opaque - (void)setOpaque:(BOOL)opaque
{ {
...@@ -83,7 +96,7 @@ NSString* srcResource (id res) ...@@ -83,7 +96,7 @@ NSString* srcResource (id res)
{ {
[EAGLContext setCurrentContext:self.context]; [EAGLContext setCurrentContext:self.context];
@autoreleasepool { @autoreleasepool {
NSDictionary *prevImages = _images; NSDictionary *prevImages = _images;
NSMutableDictionary *images = [[NSMutableDictionary alloc] init]; NSMutableDictionary *images = [[NSMutableDictionary alloc] init];
...@@ -135,7 +148,7 @@ NSString* srcResource (id res) ...@@ -135,7 +148,7 @@ NSString* srcResource (id res)
if (!src) { if (!src) {
RCTLogError(@"invalid uniform '%@' texture value '%@'", uniformName, value); RCTLogError(@"invalid uniform '%@' texture value '%@'", uniformName, value);
} }
GLImage *image = images[src]; GLImage *image = images[src];
if (image == nil) { if (image == nil) {
image = prevImages[src]; image = prevImages[src];
...@@ -144,7 +157,7 @@ NSString* srcResource (id res) ...@@ -144,7 +157,7 @@ NSString* srcResource (id res)
} }
if (image == nil) { if (image == nil) {
image = [[GLImage alloc] initWithBridge:_bridge withOnLoad:^{ image = [[GLImage alloc] initWithBridge:_bridge withOnLoad:^{
[self requestSyncData]; [self onImageLoad:src];
}]; }];
image.src = src; image.src = src;
images[src] = image; images[src] = image;
...@@ -171,7 +184,7 @@ NSString* srcResource (id res) ...@@ -171,7 +184,7 @@ NSString* srcResource (id res)
RCTLogError(@"All defined uniforms must be provided. Missing '%@'", uniformName); RCTLogError(@"All defined uniforms must be provided. Missing '%@'", uniformName);
} }
} }
return [[GLRenderData alloc] return [[GLRenderData alloc]
initWithShader:shader initWithShader:shader
withUniforms:uniforms withUniforms:uniforms
...@@ -190,6 +203,30 @@ NSString* srcResource (id res) ...@@ -190,6 +203,30 @@ NSString* srcResource (id res)
} }
} }
- (bool)allPreloaded
{
for (id toload in _imagesToPreload) {
if (![_preloaded containsObject:srcResource(toload)])
return false;
}
return true;
}
- (void)onImageLoad:(NSString *)loaded
{
if (!_preloadingDone) {
[_preloaded addObject:loaded];
if ([self allPreloaded]) {
_preloadingDone = true;
[self requestSyncData];
}
}
else {
// Any texture image load will trigger a future re-sync of data (if no preloaded)
[self requestSyncData];
}
}
- (void)setNbContentTextures:(NSNumber *)nbContentTextures - (void)setNbContentTextures:(NSNumber *)nbContentTextures
{ {
[self resizeUniformContentTextures:[nbContentTextures intValue]]; [self resizeUniformContentTextures:[nbContentTextures intValue]];
...@@ -233,6 +270,7 @@ NSString* srcResource (id res) ...@@ -233,6 +270,7 @@ NSString* srcResource (id res)
- (void)drawRect:(CGRect)rect - (void)drawRect:(CGRect)rect
{ {
if (!_preloadingDone) return;
BOOL needsDeferredRendering = _nbContentTextures > 0; BOOL needsDeferredRendering = _nbContentTextures > 0;
if (needsDeferredRendering && !_deferredRendering) { if (needsDeferredRendering && !_deferredRendering) {
dispatch_async(dispatch_get_main_queue(), ^{ dispatch_async(dispatch_get_main_queue(), ^{
......
...@@ -13,6 +13,7 @@ const GLCanvas = requireNativeComponent("GLCanvas", null); ...@@ -13,6 +13,7 @@ const GLCanvas = requireNativeComponent("GLCanvas", null);
const renderVcontent = function (width, height, id, children) { const renderVcontent = function (width, height, id, children) {
const childrenStyle = { const childrenStyle = {
key: id,
position: "absolute", position: "absolute",
top: 0, top: 0,
left: 0, left: 0,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment