Commit 8acaae75 authored by Gaëtan Renaudeau's avatar Gaëtan Renaudeau

implement the new pixelRatio per node

parent c3a98a8e
...@@ -70,6 +70,9 @@ public class GLCanvas extends GLSurfaceView ...@@ -70,6 +70,9 @@ public class GLCanvas extends GLSurfaceView
private ExecutorSupplier executorSupplier; private ExecutorSupplier executorSupplier;
private final Queue<Runnable> mRunOnDraw = new LinkedList<>(); private final Queue<Runnable> mRunOnDraw = new LinkedList<>();
private boolean captureFrameRequested = false; private boolean captureFrameRequested = false;
private float pixelRatio;
private float displayDensity;
public GLCanvas(ThemedReactContext context, ExecutorSupplier executorSupplier) { public GLCanvas(ThemedReactContext context, ExecutorSupplier executorSupplier) {
super(context); super(context);
...@@ -78,6 +81,10 @@ public class GLCanvas extends GLSurfaceView ...@@ -78,6 +81,10 @@ public class GLCanvas extends GLSurfaceView
rnglContext = context.getNativeModule(RNGLContext.class); rnglContext = context.getNativeModule(RNGLContext.class);
setEGLContextClientVersion(2); setEGLContextClientVersion(2);
DisplayMetrics dm = reactContext.getResources().getDisplayMetrics();
displayDensity = dm.density;
pixelRatio = dm.density;
setEGLConfigChooser(8, 8, 8, 8, 16, 0); setEGLConfigChooser(8, 8, 8, 8, 16, 0);
getHolder().setFormat(PixelFormat.RGB_888); getHolder().setFormat(PixelFormat.RGB_888);
setZOrderOnTop(true); setZOrderOnTop(true);
...@@ -123,6 +130,12 @@ public class GLCanvas extends GLSurfaceView ...@@ -123,6 +130,12 @@ public class GLCanvas extends GLSurfaceView
@Override @Override
public void onSurfaceChanged(GL10 gl, int width, int height) {} public void onSurfaceChanged(GL10 gl, int width, int height) {}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
syncSize(w, h, pixelRatio);
}
@Override @Override
public void onDrawFrame(GL10 gl) { public void onDrawFrame(GL10 gl) {
runAll(mRunOnDraw); runAll(mRunOnDraw);
...@@ -472,8 +485,8 @@ public class GLCanvas extends GLSurfaceView ...@@ -472,8 +485,8 @@ public class GLCanvas extends GLSurfaceView
if (arraySizeForType(type) != arr.size()) { if (arraySizeForType(type) != arr.size()) {
shader.runtimeException( shader.runtimeException(
"uniform '"+uniformName+ "uniform '"+uniformName+
"': Invalid array size: "+arr.size()+ "': Invalid array size: "+arr.size()+
". Expected: "+arraySizeForType(type)); ". Expected: "+arraySizeForType(type));
} }
uniformsFloatBuffer.put(uniformName, parseAsFloatArray(arr)); uniformsFloatBuffer.put(uniformName, parseAsFloatArray(arr));
break; break;
...@@ -488,8 +501,8 @@ public class GLCanvas extends GLSurfaceView ...@@ -488,8 +501,8 @@ public class GLCanvas extends GLSurfaceView
if (arraySizeForType(type) != arr2.size()) { if (arraySizeForType(type) != arr2.size()) {
shader.runtimeException( shader.runtimeException(
"uniform '"+uniformName+ "uniform '"+uniformName+
"': Invalid array size: "+arr2.size()+ "': Invalid array size: "+arr2.size()+
". Expected: "+arraySizeForType(type)); ". Expected: "+arraySizeForType(type));
} }
uniformsIntBuffer.put(uniformName, parseAsIntArray(arr2)); uniformsIntBuffer.put(uniformName, parseAsIntArray(arr2));
break; break;
...@@ -497,7 +510,7 @@ public class GLCanvas extends GLSurfaceView ...@@ -497,7 +510,7 @@ public class GLCanvas extends GLSurfaceView
default: default:
shader.runtimeException( shader.runtimeException(
"uniform '"+uniformName+ "uniform '"+uniformName+
"': type not supported: "+type); "': type not supported: "+type);
} }
} }
...@@ -511,9 +524,9 @@ public class GLCanvas extends GLSurfaceView ...@@ -511,9 +524,9 @@ public class GLCanvas extends GLSurfaceView
for (String uniformName: uniformTypes.keySet()) { for (String uniformName: uniformTypes.keySet()) {
if (!uniformsFloat.containsKey(uniformName) && if (!uniformsFloat.containsKey(uniformName) &&
!uniformsInteger.containsKey(uniformName) && !uniformsInteger.containsKey(uniformName) &&
!uniformsFloatBuffer.containsKey(uniformName) && !uniformsFloatBuffer.containsKey(uniformName) &&
!uniformsIntBuffer.containsKey(uniformName)) { !uniformsIntBuffer.containsKey(uniformName)) {
shader.runtimeException("All defined uniforms must be provided. Missing '"+uniformName+"'"); shader.runtimeException("All defined uniforms must be provided. Missing '"+uniformName+"'");
} }
} }
...@@ -525,8 +538,8 @@ public class GLCanvas extends GLSurfaceView ...@@ -525,8 +538,8 @@ public class GLCanvas extends GLSurfaceView
uniformsIntBuffer, uniformsIntBuffer,
uniformsFloatBuffer, uniformsFloatBuffer,
textures, textures,
data.width, (int)(data.width * data.pixelRatio),
data.height, (int)(data.height * data.pixelRatio),
data.fboId, data.fboId,
contextChildren, contextChildren,
children); children);
...@@ -538,7 +551,7 @@ public class GLCanvas extends GLSurfaceView ...@@ -538,7 +551,7 @@ public class GLCanvas extends GLSurfaceView
.order(ByteOrder.nativeOrder()) .order(ByteOrder.nativeOrder())
.asFloatBuffer(); .asFloatBuffer();
for (int i=0; i<size; i++) for (int i=0; i<size; i++)
buf.put((float) array.getDouble(i)); buf.put((float) array.getDouble(i));
buf.position(0); buf.position(0);
return buf; return buf;
} }
...@@ -598,11 +611,8 @@ public class GLCanvas extends GLSurfaceView ...@@ -598,11 +611,8 @@ public class GLCanvas extends GLSurfaceView
} }
private void recRender (GLRenderData renderData) { private void recRender (GLRenderData renderData) {
DisplayMetrics dm = reactContext.getResources().getDisplayMetrics(); int w = renderData.width;
int h = renderData.height;
int w = Float.valueOf(renderData.width.floatValue() * dm.density).intValue();
int h = Float.valueOf(renderData.height.floatValue() * dm.density).intValue();
for (GLRenderData child: renderData.contextChildren) for (GLRenderData child: renderData.contextChildren)
recRender(child); recRender(child);
...@@ -751,4 +761,16 @@ public class GLCanvas extends GLSurfaceView ...@@ -751,4 +761,16 @@ public class GLCanvas extends GLSurfaceView
d.removeAll(b); d.removeAll(b);
return d; return d;
} }
public void setPixelRatio(float pixelRatio) {
this.pixelRatio = pixelRatio;
syncSize(this.getWidth(), this.getHeight(), pixelRatio);
}
private void syncSize (int w, int h, float pixelRatio) {
int width = (int) (w * pixelRatio / displayDensity);
int height = (int) (h * pixelRatio / displayDensity);
getHolder().setFixedSize(width, height);
}
} }
...@@ -27,6 +27,11 @@ public class GLCanvasManager extends SimpleViewManager<GLCanvas> { ...@@ -27,6 +27,11 @@ public class GLCanvasManager extends SimpleViewManager<GLCanvas> {
private ExecutorSupplier executorSupplier; private ExecutorSupplier executorSupplier;
@ReactProp(name="pixelRatio")
public void setPixelRatio (GLCanvas view, float pixelRatio) {
view.setPixelRatio(pixelRatio);
}
@ReactProp(name="nbContentTextures") @ReactProp(name="nbContentTextures")
public void setNbContentTextures (GLCanvas view, int nbContentTextures) { public void setNbContentTextures (GLCanvas view, int nbContentTextures) {
view.setNbContentTextures(nbContentTextures); view.setNbContentTextures(nbContentTextures);
......
...@@ -10,17 +10,19 @@ public class GLData { ...@@ -10,17 +10,19 @@ public class GLData {
final Integer shader; final Integer shader;
final ReadableMap uniforms; final ReadableMap uniforms;
final Integer width; final Double width;
final Integer height; final Double height;
final Double pixelRatio;
final Integer fboId; final Integer fboId;
final List<GLData> contextChildren; final List<GLData> contextChildren;
final List<GLData> children; final List<GLData> children;
public GLData(Integer shader, ReadableMap uniforms, Integer width, Integer height, Integer fboId, List<GLData> contextChildren, List<GLData> children) { public GLData(Integer shader, ReadableMap uniforms, Double width, Double height, Double pixelRatio, Integer fboId, List<GLData> contextChildren, List<GLData> children) {
this.shader = shader; this.shader = shader;
this.uniforms = uniforms; this.uniforms = uniforms;
this.width = width; this.width = width;
this.height = height; this.height = height;
this.pixelRatio = pixelRatio;
this.fboId = fboId; this.fboId = fboId;
this.contextChildren = contextChildren; this.contextChildren = contextChildren;
this.children = children; this.children = children;
...@@ -37,11 +39,12 @@ public class GLData { ...@@ -37,11 +39,12 @@ public class GLData {
public static GLData fromMap (ReadableMap map) { public static GLData fromMap (ReadableMap map) {
Integer shader = map.getInt("shader"); Integer shader = map.getInt("shader");
ReadableMap uniforms = map.getMap("uniforms"); ReadableMap uniforms = map.getMap("uniforms");
Integer width = (int) map.getDouble("width"); Double width = map.getDouble("width");
Integer height = (int) map.getDouble("height"); Double height = map.getDouble("height");
Double pixelRatio = map.getDouble("pixelRatio");
Integer fboId = map.getInt("fboId"); Integer fboId = map.getInt("fboId");
List<GLData> children = fromArray(map.getArray("children")); List<GLData> children = fromArray(map.getArray("children"));
List<GLData> contextChildren = fromArray(map.getArray("contextChildren")); List<GLData> contextChildren = fromArray(map.getArray("contextChildren"));
return new GLData(shader, uniforms, width, height, fboId, contextChildren, children); return new GLData(shader, uniforms, width, height, pixelRatio, fboId, contextChildren, children);
} }
} }
...@@ -178,6 +178,7 @@ RCT_NOT_IMPLEMENTED(-init) ...@@ -178,6 +178,7 @@ RCT_NOT_IMPLEMENTED(-init)
weak_traverseTree = traverseTree = ^GLRenderData *(GLData *data) { weak_traverseTree = traverseTree = ^GLRenderData *(GLData *data) {
NSNumber *width = data.width; NSNumber *width = data.width;
NSNumber *height = data.height; NSNumber *height = data.height;
NSNumber *pixelRatio = data.pixelRatio;
int fboId = [data.fboId intValue]; int fboId = [data.fboId intValue];
NSMutableArray *contextChildren = [[NSMutableArray alloc] init]; NSMutableArray *contextChildren = [[NSMutableArray alloc] init];
...@@ -275,8 +276,8 @@ RCT_NOT_IMPLEMENTED(-init) ...@@ -275,8 +276,8 @@ RCT_NOT_IMPLEMENTED(-init)
initWithShader:shader initWithShader:shader
withUniforms:uniforms withUniforms:uniforms
withTextures:textures withTextures:textures
withWidth:width withWidth:(int)([width floatValue] * [pixelRatio floatValue])
withHeight:height withHeight:(int)([height floatValue] * [pixelRatio floatValue])
withFboId:fboId withFboId:fboId
withContextChildren:contextChildren withContextChildren:contextChildren
withChildren:children]; withChildren:children];
...@@ -404,16 +405,14 @@ RCT_NOT_IMPLEMENTED(-init) ...@@ -404,16 +405,14 @@ RCT_NOT_IMPLEMENTED(-init)
GLRenderData *rd = _renderData; GLRenderData *rd = _renderData;
if (!rd) return; if (!rd) return;
RCT_PROFILE_BEGIN_EVENT(0, @"GLCanvas render", nil); RCT_PROFILE_BEGIN_EVENT(0, @"GLCanvas render", nil);
CGFloat scale = self.contentScaleFactor;
@autoreleasepool { @autoreleasepool {
CGFloat scale = RCTScreenScale();
void (^recDraw) (GLRenderData *renderData); void (^recDraw) (GLRenderData *renderData);
__block __weak void (^weak_recDraw) (GLRenderData *renderData); __block __weak void (^weak_recDraw) (GLRenderData *renderData);
weak_recDraw = recDraw = ^void(GLRenderData *renderData) { weak_recDraw = recDraw = ^void(GLRenderData *renderData) {
float w = [renderData.width floatValue] * scale; int w = renderData.width;
float h = [renderData.height floatValue] * scale; int h = renderData.height;
for (GLRenderData *child in renderData.contextChildren) for (GLRenderData *child in renderData.contextChildren)
weak_recDraw(child); weak_recDraw(child);
......
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
@property (nonatomic) NSDictionary *uniforms; @property (nonatomic) NSDictionary *uniforms;
@property (nonatomic) NSNumber *width; @property (nonatomic) NSNumber *width;
@property (nonatomic) NSNumber *height; @property (nonatomic) NSNumber *height;
@property (nonatomic) NSNumber *pixelRatio;
@property (nonatomic) NSNumber *fboId; @property (nonatomic) NSNumber *fboId;
@property (nonatomic) NSArray *contextChildren; @property (nonatomic) NSArray *contextChildren;
@property (nonatomic) NSArray *children; @property (nonatomic) NSArray *children;
...@@ -16,6 +17,7 @@ ...@@ -16,6 +17,7 @@
withUniforms: (NSDictionary *)uniforms withUniforms: (NSDictionary *)uniforms
withWidth: (NSNumber *)width withWidth: (NSNumber *)width
withHeight: (NSNumber *)height withHeight: (NSNumber *)height
withPixelRatio: (NSNumber *)pixelRatio
withFboId: (NSNumber *)fboId withFboId: (NSNumber *)fboId
withContextChildren: (NSArray *)contextChildren withContextChildren: (NSArray *)contextChildren
withChildren: (NSArray *)children; withChildren: (NSArray *)children;
......
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
withUniforms: (NSDictionary *)uniforms withUniforms: (NSDictionary *)uniforms
withWidth: (NSNumber *)width withWidth: (NSNumber *)width
withHeight: (NSNumber *)height withHeight: (NSNumber *)height
withPixelRatio: (NSNumber *)pixelRatio
withFboId: (NSNumber *)fboId withFboId: (NSNumber *)fboId
withContextChildren: (NSArray *)contextChildren withContextChildren: (NSArray *)contextChildren
withChildren: (NSArray *)children withChildren: (NSArray *)children
...@@ -15,6 +16,7 @@ ...@@ -15,6 +16,7 @@
self.uniforms = uniforms; self.uniforms = uniforms;
self.width = width; self.width = width;
self.height = height; self.height = height;
self.pixelRatio = pixelRatio;
self.fboId = fboId; self.fboId = fboId;
self.contextChildren = contextChildren; self.contextChildren = contextChildren;
self.children = children; self.children = children;
......
...@@ -7,8 +7,8 @@ ...@@ -7,8 +7,8 @@
@property (nonatomic) GLShader *shader; @property (nonatomic) GLShader *shader;
@property (nonatomic) NSDictionary *uniforms; @property (nonatomic) NSDictionary *uniforms;
@property (nonatomic) NSDictionary *textures; @property (nonatomic) NSDictionary *textures;
@property (nonatomic) NSNumber *width; @property (nonatomic) int width;
@property (nonatomic) NSNumber *height; @property (nonatomic) int height;
@property (nonatomic) int fboId; @property (nonatomic) int fboId;
@property (nonatomic) NSArray *contextChildren; @property (nonatomic) NSArray *contextChildren;
@property (nonatomic) NSArray *children; @property (nonatomic) NSArray *children;
...@@ -16,8 +16,8 @@ ...@@ -16,8 +16,8 @@
-(instancetype) initWithShader: (GLShader *)shader -(instancetype) initWithShader: (GLShader *)shader
withUniforms:(NSDictionary *)uniforms withUniforms:(NSDictionary *)uniforms
withTextures: (NSDictionary *)textures withTextures: (NSDictionary *)textures
withWidth: (NSNumber *)width withWidth: (int)width
withHeight: (NSNumber *)height withHeight: (int)height
withFboId: (int)fboId withFboId: (int)fboId
withContextChildren: (NSArray *)contextChildren withContextChildren: (NSArray *)contextChildren
withChildren: (NSArray *)children; withChildren: (NSArray *)children;
......
...@@ -6,8 +6,8 @@ ...@@ -6,8 +6,8 @@
-(instancetype) initWithShader: (GLShader *)shader -(instancetype) initWithShader: (GLShader *)shader
withUniforms:(NSDictionary *)uniforms withUniforms:(NSDictionary *)uniforms
withTextures: (NSDictionary *)textures withTextures: (NSDictionary *)textures
withWidth: (NSNumber *)width withWidth: (int)width
withHeight: (NSNumber *)height withHeight: (int)height
withFboId: (int)fboId withFboId: (int)fboId
withContextChildren: (NSArray *)contextChildren withContextChildren: (NSArray *)contextChildren
withChildren: (NSArray *)children withChildren: (NSArray *)children
......
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
NSDictionary *uniforms = [self NSDictionary:json[@"uniforms"]]; NSDictionary *uniforms = [self NSDictionary:json[@"uniforms"]];
NSNumber *width = [self NSNumber:json[@"width"]]; NSNumber *width = [self NSNumber:json[@"width"]];
NSNumber *height = [self NSNumber:json[@"height"]]; NSNumber *height = [self NSNumber:json[@"height"]];
NSNumber *pixelRatio = [self NSNumber:json[@"pixelRatio"]];
NSNumber *fboId = [self NSNumber:json[@"fboId"]]; NSNumber *fboId = [self NSNumber:json[@"fboId"]];
NSArray *contextChildrenJSON = [self NSArray: json[@"contextChildren"]]; NSArray *contextChildrenJSON = [self NSArray: json[@"contextChildren"]];
NSArray *childrenJSON = [self NSArray: json[@"children"]]; NSArray *childrenJSON = [self NSArray: json[@"children"]];
...@@ -30,6 +31,7 @@ ...@@ -30,6 +31,7 @@
withUniforms: uniforms withUniforms: uniforms
withWidth: width withWidth: width
withHeight: height withHeight: height
withPixelRatio: pixelRatio
withFboId: fboId withFboId: fboId
withContextChildren: contextChildren withContextChildren: contextChildren
withChildren: children]; withChildren: children];
......
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