Commit 27e53817 authored by Gaëtan Renaudeau's avatar Gaëtan Renaudeau

improve implementation

parent 24f13a0d
...@@ -43,7 +43,7 @@ NSArray* diff (NSArray* a, NSArray* b) { ...@@ -43,7 +43,7 @@ NSArray* diff (NSArray* a, NSArray* b) {
GLRenderData *_renderData; GLRenderData *_renderData;
NSArray *_contentData; NSArray *_rasterizedContent;
NSArray *_contentTextures; NSArray *_contentTextures;
NSDictionary *_images; // This caches the currently used images (imageSrc -> GLReactImage) NSDictionary *_images; // This caches the currently used images (imageSrc -> GLReactImage)
...@@ -88,7 +88,7 @@ RCT_NOT_IMPLEMENTED(-init) ...@@ -88,7 +88,7 @@ RCT_NOT_IMPLEMENTED(-init)
_images = nil; _images = nil;
_preloaded = nil; _preloaded = nil;
_captureConfigs = nil; _captureConfigs = nil;
_contentData = nil; _rasterizedContent = nil;
_contentTextures = nil; _contentTextures = nil;
_data = nil; _data = nil;
_renderData = nil; _renderData = nil;
...@@ -323,11 +323,8 @@ RCT_NOT_IMPLEMENTED(-init) ...@@ -323,11 +323,8 @@ RCT_NOT_IMPLEMENTED(-init)
- (void)rasterizeContent - (void)rasterizeContent
{ {
// TODO: we need to refactor how this stuff work...
// we should no longer do any rasterize work if all the work is to be done in syncContentTextures
RCT_PROFILE_BEGIN_EVENT(0, @"GLCanvas rasterizeContent", nil); RCT_PROFILE_BEGIN_EVENT(0, @"GLCanvas rasterizeContent", nil);
NSMutableArray *contentData = [[NSMutableArray alloc] init]; NSMutableArray *rasterizedContent = [[NSMutableArray alloc] init];
int nb = [_nbContentTextures intValue]; int nb = [_nbContentTextures intValue];
for (int i = 0; i < nb; i++) { for (int i = 0; i < nb; i++) {
UIView *view = self.superview.subviews[i]; // We take siblings by index (closely related to the JS code) UIView *view = self.superview.subviews[i]; // We take siblings by index (closely related to the JS code)
...@@ -345,9 +342,9 @@ RCT_NOT_IMPLEMENTED(-init) ...@@ -345,9 +342,9 @@ RCT_NOT_IMPLEMENTED(-init)
imgData = [GLImageData genPixelsWithView:v withPixelRatio:self.contentScaleFactor]; imgData = [GLImageData genPixelsWithView:v withPixelRatio:self.contentScaleFactor];
} }
} }
if (imgData) contentData[i] = imgData; if (imgData) rasterizedContent[i] = imgData;
} }
_contentData = contentData; _rasterizedContent = rasterizedContent;
[self setNeedsDisplay]; [self setNeedsDisplay];
RCT_PROFILE_END_EVENT(0, @"gl", nil); RCT_PROFILE_END_EVENT(0, @"gl", nil);
} }
...@@ -355,7 +352,6 @@ RCT_NOT_IMPLEMENTED(-init) ...@@ -355,7 +352,6 @@ RCT_NOT_IMPLEMENTED(-init)
- (void)syncContentTextures - (void)syncContentTextures
{ {
RCT_PROFILE_BEGIN_EVENT(0, @"GLCanvas syncContentTextures", nil); RCT_PROFILE_BEGIN_EVENT(0, @"GLCanvas syncContentTextures", nil);
NSMutableArray *contentData = _contentData.mutableCopy;
unsigned long max = MIN([_nbContentTextures intValue], [_contentTextures count]); unsigned long max = MIN([_nbContentTextures intValue], [_contentTextures count]);
for (int i = 0; i < max; i++) { for (int i = 0; i < max; i++) {
...@@ -374,16 +370,11 @@ RCT_NOT_IMPLEMENTED(-init) ...@@ -374,16 +370,11 @@ RCT_NOT_IMPLEMENTED(-init)
[invocation invoke]; [invocation invoke];
CVPixelBufferRef buffer; CVPixelBufferRef buffer;
[invocation getReturnValue:&buffer]; [invocation getReturnValue:&buffer];
[_contentTextures[i] setPixelsWithPixelBuffer:buffer];
int width = (int) CVPixelBufferGetWidth(buffer); }
int height = (int) CVPixelBufferGetHeight(buffer); else {
contentData[i] = [[GLImageData alloc] [_contentTextures[i] setPixels:_rasterizedContent[i]];
initWithData:CVPixelBufferGetBaseAddress(buffer)
withWidth:width
withHeight:height];
} }
[_contentTextures[i] setPixels:contentData[i]];
} }
} }
RCT_PROFILE_END_EVENT(0, @"gl", nil); RCT_PROFILE_END_EVENT(0, @"gl", nil);
......
...@@ -14,5 +14,6 @@ ...@@ -14,5 +14,6 @@
- (void)setShapeWithWidth:(float)width withHeight:(float)height; - (void)setShapeWithWidth:(float)width withHeight:(float)height;
- (void)setPixels: (GLImageData *)data; - (void)setPixels: (GLImageData *)data;
- (void)setPixelsWithPixelBuffer: (CVPixelBufferRef)buffer;
@end @end
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
{ {
GLuint _handle; // The identifier of the gl texture GLuint _handle; // The identifier of the gl texture
GLImageData* dataCurrentlyUploaded; // The last set data (cache) GLImageData* dataCurrentlyUploaded; // The last set data (cache)
CVPixelBufferRef bufferCurrentlyUploaded; // The last set buffer (cache)
} }
- (instancetype)init - (instancetype)init
...@@ -21,6 +22,7 @@ ...@@ -21,6 +22,7 @@
{ {
glDeleteTextures(1, &_handle); glDeleteTextures(1, &_handle);
dataCurrentlyUploaded = nil; dataCurrentlyUploaded = nil;
bufferCurrentlyUploaded = NULL;
} }
- (void) makeTexture - (void) makeTexture
...@@ -54,6 +56,7 @@ ...@@ -54,6 +56,7 @@
- (void)setPixels: (GLImageData *)data - (void)setPixels: (GLImageData *)data
{ {
GLImageData *d = data==nil ? [GLImageData empty] : data; GLImageData *d = data==nil ? [GLImageData empty] : data;
bufferCurrentlyUploaded = NULL;
if (d != dataCurrentlyUploaded) { if (d != dataCurrentlyUploaded) {
dataCurrentlyUploaded = d; dataCurrentlyUploaded = d;
[self bind]; [self bind];
...@@ -61,4 +64,23 @@ ...@@ -61,4 +64,23 @@
} }
} }
- (void)setPixelsWithPixelBuffer: (CVPixelBufferRef)buffer
{
if (buffer == NULL) {
[self setPixels:[GLImageData empty]];
}
else {
dataCurrentlyUploaded = nil;
if (buffer != bufferCurrentlyUploaded) {
bufferCurrentlyUploaded = buffer;
[self bind];
int width = (int) CVPixelBufferGetWidth(buffer);
int height = (int) CVPixelBufferGetHeight(buffer);
GLubyte* data = CVPixelBufferGetBaseAddress(buffer);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
}
}
}
@end @end
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