From 27e5381781bf73a3c34bbda31593783b0a8a6c09 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABtan=20Renaudeau?= Date: Mon, 8 Feb 2016 10:36:45 +0100 Subject: [PATCH] improve implementation --- ios/GLCanvas.m | 27 +++++++++------------------ ios/GLTexture.h | 1 + ios/GLTexture.m | 22 ++++++++++++++++++++++ 3 files changed, 32 insertions(+), 18 deletions(-) diff --git a/ios/GLCanvas.m b/ios/GLCanvas.m index d6e202f..3a7513f 100644 --- a/ios/GLCanvas.m +++ b/ios/GLCanvas.m @@ -43,7 +43,7 @@ NSArray* diff (NSArray* a, NSArray* b) { GLRenderData *_renderData; - NSArray *_contentData; + NSArray *_rasterizedContent; NSArray *_contentTextures; NSDictionary *_images; // This caches the currently used images (imageSrc -> GLReactImage) @@ -88,7 +88,7 @@ RCT_NOT_IMPLEMENTED(-init) _images = nil; _preloaded = nil; _captureConfigs = nil; - _contentData = nil; + _rasterizedContent = nil; _contentTextures = nil; _data = nil; _renderData = nil; @@ -323,11 +323,8 @@ RCT_NOT_IMPLEMENTED(-init) - (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); - NSMutableArray *contentData = [[NSMutableArray alloc] init]; + NSMutableArray *rasterizedContent = [[NSMutableArray alloc] init]; int nb = [_nbContentTextures intValue]; for (int i = 0; i < nb; i++) { 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) imgData = [GLImageData genPixelsWithView:v withPixelRatio:self.contentScaleFactor]; } } - if (imgData) contentData[i] = imgData; + if (imgData) rasterizedContent[i] = imgData; } - _contentData = contentData; + _rasterizedContent = rasterizedContent; [self setNeedsDisplay]; RCT_PROFILE_END_EVENT(0, @"gl", nil); } @@ -355,7 +352,6 @@ RCT_NOT_IMPLEMENTED(-init) - (void)syncContentTextures { RCT_PROFILE_BEGIN_EVENT(0, @"GLCanvas syncContentTextures", nil); - NSMutableArray *contentData = _contentData.mutableCopy; unsigned long max = MIN([_nbContentTextures intValue], [_contentTextures count]); for (int i = 0; i < max; i++) { @@ -374,16 +370,11 @@ RCT_NOT_IMPLEMENTED(-init) [invocation invoke]; CVPixelBufferRef buffer; [invocation getReturnValue:&buffer]; - - int width = (int) CVPixelBufferGetWidth(buffer); - int height = (int) CVPixelBufferGetHeight(buffer); - contentData[i] = [[GLImageData alloc] - initWithData:CVPixelBufferGetBaseAddress(buffer) - withWidth:width - withHeight:height]; + [_contentTextures[i] setPixelsWithPixelBuffer:buffer]; + } + else { + [_contentTextures[i] setPixels:_rasterizedContent[i]]; } - - [_contentTextures[i] setPixels:contentData[i]]; } } RCT_PROFILE_END_EVENT(0, @"gl", nil); diff --git a/ios/GLTexture.h b/ios/GLTexture.h index e47019d..58e0bd3 100644 --- a/ios/GLTexture.h +++ b/ios/GLTexture.h @@ -14,5 +14,6 @@ - (void)setShapeWithWidth:(float)width withHeight:(float)height; - (void)setPixels: (GLImageData *)data; +- (void)setPixelsWithPixelBuffer: (CVPixelBufferRef)buffer; @end diff --git a/ios/GLTexture.m b/ios/GLTexture.m index 865e452..efaedca 100644 --- a/ios/GLTexture.m +++ b/ios/GLTexture.m @@ -6,6 +6,7 @@ { GLuint _handle; // The identifier of the gl texture GLImageData* dataCurrentlyUploaded; // The last set data (cache) + CVPixelBufferRef bufferCurrentlyUploaded; // The last set buffer (cache) } - (instancetype)init @@ -21,6 +22,7 @@ { glDeleteTextures(1, &_handle); dataCurrentlyUploaded = nil; + bufferCurrentlyUploaded = NULL; } - (void) makeTexture @@ -54,6 +56,7 @@ - (void)setPixels: (GLImageData *)data { GLImageData *d = data==nil ? [GLImageData empty] : data; + bufferCurrentlyUploaded = NULL; if (d != dataCurrentlyUploaded) { dataCurrentlyUploaded = d; [self bind]; @@ -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 -- 2.26.2