Commit 6ae34c45 authored by Gaëtan Renaudeau's avatar Gaëtan Renaudeau

Add pixelRatio and context

parent 0a4c758f
...@@ -11,6 +11,7 @@ ...@@ -11,6 +11,7 @@
@property (nonatomic) BOOL visibleContent; @property (nonatomic) BOOL visibleContent;
@property (nonatomic) NSNumber *nbContentTextures; @property (nonatomic) NSNumber *nbContentTextures;
@property (nonatomic) NSNumber *renderId; @property (nonatomic) NSNumber *renderId;
@property (nonatomic) NSNumber *pixelRatio;
@property (nonatomic) NSArray *imagesToPreload; @property (nonatomic) NSArray *imagesToPreload;
@property (nonatomic, copy) RCTBubblingEventBlock onGLProgress; @property (nonatomic, copy) RCTBubblingEventBlock onGLProgress;
@property (nonatomic, copy) RCTBubblingEventBlock onGLLoad; @property (nonatomic, copy) RCTBubblingEventBlock onGLLoad;
......
...@@ -62,7 +62,6 @@ NSString* srcResource (id res) ...@@ -62,7 +62,6 @@ NSString* srcResource (id res)
_captureFrameRequested = false; _captureFrameRequested = false;
_preloadingDone = false; _preloadingDone = false;
self.context = [bridge.rnglContext getContext]; self.context = [bridge.rnglContext getContext];
self.contentScaleFactor = RCTScreenScale();
} }
return self; return self;
} }
...@@ -129,6 +128,12 @@ RCT_NOT_IMPLEMENTED(-init) ...@@ -129,6 +128,12 @@ RCT_NOT_IMPLEMENTED(-init)
} }
} }
- (void)setPixelRatio:(NSNumber *)pixelRatio
{
self.contentScaleFactor = [pixelRatio floatValue];
[self setNeedsDisplay];
}
- (void)setData:(GLData *)data - (void)setData:(GLData *)data
{ {
_data = data; _data = data;
...@@ -284,7 +289,7 @@ RCT_NOT_IMPLEMENTED(-init) ...@@ -284,7 +289,7 @@ RCT_NOT_IMPLEMENTED(-init)
UIView *v = [view.subviews count] == 1 ? UIView *v = [view.subviews count] == 1 ?
view.subviews[0] : view.subviews[0] :
view; view;
imgData = [GLImageData genPixelsWithView:v]; imgData = [GLImageData genPixelsWithView:v withPixelRatio:self.contentScaleFactor];
} else { } else {
imgData = nil; imgData = nil;
} }
...@@ -352,7 +357,7 @@ RCT_NOT_IMPLEMENTED(-init) ...@@ -352,7 +357,7 @@ RCT_NOT_IMPLEMENTED(-init)
{ {
if (!_renderData) return; if (!_renderData) return;
CGFloat scale = RCTScreenScale(); CGFloat scale = self.contentScaleFactor;
@autoreleasepool { @autoreleasepool {
void (^recDraw) (GLRenderData *renderData); void (^recDraw) (GLRenderData *renderData);
......
...@@ -22,6 +22,7 @@ RCT_EXPORT_VIEW_PROPERTY(opaque, BOOL); ...@@ -22,6 +22,7 @@ RCT_EXPORT_VIEW_PROPERTY(opaque, BOOL);
RCT_EXPORT_VIEW_PROPERTY(autoRedraw, BOOL); RCT_EXPORT_VIEW_PROPERTY(autoRedraw, BOOL);
RCT_EXPORT_VIEW_PROPERTY(data, GLData); RCT_EXPORT_VIEW_PROPERTY(data, GLData);
RCT_EXPORT_VIEW_PROPERTY(renderId, NSNumber); RCT_EXPORT_VIEW_PROPERTY(renderId, NSNumber);
RCT_EXPORT_VIEW_PROPERTY(pixelRatio, NSNumber);
RCT_EXPORT_VIEW_PROPERTY(imagesToPreload, NSArray); RCT_EXPORT_VIEW_PROPERTY(imagesToPreload, NSArray);
RCT_EXPORT_VIEW_PROPERTY(onGLLoad, RCTBubblingEventBlock); RCT_EXPORT_VIEW_PROPERTY(onGLLoad, RCTBubblingEventBlock);
RCT_EXPORT_VIEW_PROPERTY(onGLProgress, RCTBubblingEventBlock); RCT_EXPORT_VIEW_PROPERTY(onGLProgress, RCTBubblingEventBlock);
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
+ (GLImageData*) empty; + (GLImageData*) empty;
+ (GLImageData*) genPixelsWithImage: (UIImage *)image; + (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; - (instancetype)initWithData: (GLubyte *)data withWidth:(int)width withHeight:(int)height;
......
#import "GLImageData.h" #import "GLImageData.h"
#import "RCTUtils.h"
#import "RCTLog.h" #import "RCTLog.h"
// This structure aims to be used in an immutable way // This structure aims to be used in an immutable way
...@@ -55,16 +54,16 @@ GLImageData *EMPTY_PIXELS; ...@@ -55,16 +54,16 @@ GLImageData *EMPTY_PIXELS;
return [[GLImageData alloc] initWithData:data withWidth:width withHeight:height]; 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 width = pixelRatio * view.bounds.size.width;
float height = RCTScreenScale() * view.bounds.size.height; float height = pixelRatio * view.bounds.size.height;
GLubyte *data = (GLubyte *)malloc(4 * width * height); GLubyte *data = (GLubyte *)malloc(4 * width * height);
CGColorSpaceRef colourSpace = CGColorSpaceCreateDeviceRGB(); CGColorSpaceRef colourSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef ctx = CGBitmapContextCreate(data, width, height, 8, 4 * width, colourSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big); CGContextRef ctx = CGBitmapContextCreate(data, width, height, 8, 4 * width, colourSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
CGColorSpaceRelease(colourSpace); CGColorSpaceRelease(colourSpace);
CGContextClearRect(ctx, CGRectMake(0.0, 0.0, width, height)); CGContextClearRect(ctx, CGRectMake(0.0, 0.0, width, height));
CGContextScaleCTM(ctx, RCTScreenScale(), RCTScreenScale()); CGContextScaleCTM(ctx, pixelRatio, pixelRatio);
[view.layer renderInContext:ctx]; [view.layer renderInContext:ctx];
CGContextRelease(ctx); CGContextRelease(ctx);
return [[GLImageData alloc] initWithData:data withWidth:width withHeight:height]; return [[GLImageData alloc] initWithData:data withWidth:width withHeight:height];
......
...@@ -2,11 +2,13 @@ const invariant = require("invariant"); ...@@ -2,11 +2,13 @@ const invariant = require("invariant");
const {createSurface} = require("gl-react"); const {createSurface} = require("gl-react");
invariant(typeof createSurface === "function", "gl-react createSurface is not a function. Check your gl-react dependency"); invariant(typeof createSurface === "function", "gl-react createSurface is not a function. Check your gl-react dependency");
const React = require("react-native"); const React = require("react-native");
const GLCanvas = require("./GLCanvas");
const { const {
View, View,
PixelRatio
} = React; } = React;
const GLCanvas = require("./GLCanvas");
const getPixelRatio = props => props.scale || PixelRatio.get();
function renderVcontent (width, height, id, children, { visibleContent }) { function renderVcontent (width, height, id, children, { visibleContent }) {
const childrenStyle = { const childrenStyle = {
...@@ -40,4 +42,4 @@ function renderVcontainer ({ style, width, height, visibleContent, eventsThrough ...@@ -40,4 +42,4 @@ function renderVcontainer ({ style, width, height, visibleContent, eventsThrough
</View>; </View>;
} }
module.exports = createSurface(renderVcontainer, renderVcontent, renderVGL); module.exports = createSurface(renderVcontainer, renderVcontent, renderVGL, getPixelRatio);
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