From 6ae34c459f2028350c2b8dc479895a6f0beb89a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABtan=20Renaudeau?= Date: Sun, 20 Dec 2015 17:56:53 +0100 Subject: [PATCH] Add pixelRatio and context --- ios/GLCanvas.h | 1 + ios/GLCanvas.m | 11 ++++++++--- ios/GLCanvasManager.m | 1 + ios/GLImageData.h | 2 +- ios/GLImageData.m | 9 ++++----- src/Surface.js | 8 +++++--- 6 files changed, 20 insertions(+), 12 deletions(-) diff --git a/ios/GLCanvas.h b/ios/GLCanvas.h index 4f04cde..44a9ba7 100644 --- a/ios/GLCanvas.h +++ b/ios/GLCanvas.h @@ -11,6 +11,7 @@ @property (nonatomic) BOOL visibleContent; @property (nonatomic) NSNumber *nbContentTextures; @property (nonatomic) NSNumber *renderId; +@property (nonatomic) NSNumber *pixelRatio; @property (nonatomic) NSArray *imagesToPreload; @property (nonatomic, copy) RCTBubblingEventBlock onGLProgress; @property (nonatomic, copy) RCTBubblingEventBlock onGLLoad; diff --git a/ios/GLCanvas.m b/ios/GLCanvas.m index 3260fa8..7401535 100644 --- a/ios/GLCanvas.m +++ b/ios/GLCanvas.m @@ -62,7 +62,6 @@ NSString* srcResource (id res) _captureFrameRequested = false; _preloadingDone = false; self.context = [bridge.rnglContext getContext]; - self.contentScaleFactor = RCTScreenScale(); } return self; } @@ -129,6 +128,12 @@ RCT_NOT_IMPLEMENTED(-init) } } +- (void)setPixelRatio:(NSNumber *)pixelRatio +{ + self.contentScaleFactor = [pixelRatio floatValue]; + [self setNeedsDisplay]; +} + - (void)setData:(GLData *)data { _data = data; @@ -284,7 +289,7 @@ RCT_NOT_IMPLEMENTED(-init) UIView *v = [view.subviews count] == 1 ? view.subviews[0] : view; - imgData = [GLImageData genPixelsWithView:v]; + imgData = [GLImageData genPixelsWithView:v withPixelRatio:self.contentScaleFactor]; } else { imgData = nil; } @@ -352,7 +357,7 @@ RCT_NOT_IMPLEMENTED(-init) { if (!_renderData) return; - CGFloat scale = RCTScreenScale(); + CGFloat scale = self.contentScaleFactor; @autoreleasepool { void (^recDraw) (GLRenderData *renderData); diff --git a/ios/GLCanvasManager.m b/ios/GLCanvasManager.m index 48c0212..65970b0 100644 --- a/ios/GLCanvasManager.m +++ b/ios/GLCanvasManager.m @@ -22,6 +22,7 @@ RCT_EXPORT_VIEW_PROPERTY(opaque, BOOL); RCT_EXPORT_VIEW_PROPERTY(autoRedraw, BOOL); RCT_EXPORT_VIEW_PROPERTY(data, GLData); RCT_EXPORT_VIEW_PROPERTY(renderId, NSNumber); +RCT_EXPORT_VIEW_PROPERTY(pixelRatio, NSNumber); RCT_EXPORT_VIEW_PROPERTY(imagesToPreload, NSArray); RCT_EXPORT_VIEW_PROPERTY(onGLLoad, RCTBubblingEventBlock); RCT_EXPORT_VIEW_PROPERTY(onGLProgress, RCTBubblingEventBlock); diff --git a/ios/GLImageData.h b/ios/GLImageData.h index c2010f3..5131ad0 100644 --- a/ios/GLImageData.h +++ b/ios/GLImageData.h @@ -8,7 +8,7 @@ + (GLImageData*) empty; + (GLImageData*) genPixelsWithImage: (UIImage *)image; -+ (GLImageData*) genPixelsWithView: (UIView *)view; ++ (GLImageData*) genPixelsWithView: (UIView *)view withPixelRatio:(float)pixelRatio; - (instancetype)initWithData: (GLubyte *)data withWidth:(int)width withHeight:(int)height; diff --git a/ios/GLImageData.m b/ios/GLImageData.m index 5ef0c81..8b301a9 100644 --- a/ios/GLImageData.m +++ b/ios/GLImageData.m @@ -1,6 +1,5 @@ #import "GLImageData.h" -#import "RCTUtils.h" #import "RCTLog.h" // This structure aims to be used in an immutable way @@ -55,16 +54,16 @@ GLImageData *EMPTY_PIXELS; return [[GLImageData alloc] initWithData:data withWidth:width withHeight:height]; } -+ (GLImageData *)genPixelsWithView: (UIView *)view ++ (GLImageData *)genPixelsWithView: (UIView *)view withPixelRatio:(float)pixelRatio { - float width = RCTScreenScale() * view.bounds.size.width; - float height = RCTScreenScale() * view.bounds.size.height; + float width = pixelRatio * view.bounds.size.width; + float height = pixelRatio * view.bounds.size.height; GLubyte *data = (GLubyte *)malloc(4 * width * height); CGColorSpaceRef colourSpace = CGColorSpaceCreateDeviceRGB(); CGContextRef ctx = CGBitmapContextCreate(data, width, height, 8, 4 * width, colourSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big); CGColorSpaceRelease(colourSpace); CGContextClearRect(ctx, CGRectMake(0.0, 0.0, width, height)); - CGContextScaleCTM(ctx, RCTScreenScale(), RCTScreenScale()); + CGContextScaleCTM(ctx, pixelRatio, pixelRatio); [view.layer renderInContext:ctx]; CGContextRelease(ctx); return [[GLImageData alloc] initWithData:data withWidth:width withHeight:height]; diff --git a/src/Surface.js b/src/Surface.js index ae0c8f8..d6d497e 100644 --- a/src/Surface.js +++ b/src/Surface.js @@ -2,11 +2,13 @@ const invariant = require("invariant"); const {createSurface} = require("gl-react"); invariant(typeof createSurface === "function", "gl-react createSurface is not a function. Check your gl-react dependency"); const React = require("react-native"); -const GLCanvas = require("./GLCanvas"); - const { View, + PixelRatio } = React; +const GLCanvas = require("./GLCanvas"); + +const getPixelRatio = props => props.scale || PixelRatio.get(); function renderVcontent (width, height, id, children, { visibleContent }) { const childrenStyle = { @@ -40,4 +42,4 @@ function renderVcontainer ({ style, width, height, visibleContent, eventsThrough ; } -module.exports = createSurface(renderVcontainer, renderVcontent, renderVGL); +module.exports = createSurface(renderVcontainer, renderVcontent, renderVGL, getPixelRatio); -- 2.26.2