diff --git a/android/src/main/java/com/projectseptember/RNGL/GLCanvas.java b/android/src/main/java/com/projectseptember/RNGL/GLCanvas.java index 38a49caf0411e8ee260f15578de729ed75c3945b..eb3ca341cad5418cf6174031969829dc5545755b 100644 --- a/android/src/main/java/com/projectseptember/RNGL/GLCanvas.java +++ b/android/src/main/java/com/projectseptember/RNGL/GLCanvas.java @@ -70,6 +70,9 @@ public class GLCanvas extends GLSurfaceView private ExecutorSupplier executorSupplier; private final Queue mRunOnDraw = new LinkedList<>(); private boolean captureFrameRequested = false; + private float pixelRatio; + + private float displayDensity; public GLCanvas(ThemedReactContext context, ExecutorSupplier executorSupplier) { super(context); @@ -78,6 +81,10 @@ public class GLCanvas extends GLSurfaceView rnglContext = context.getNativeModule(RNGLContext.class); setEGLContextClientVersion(2); + DisplayMetrics dm = reactContext.getResources().getDisplayMetrics(); + displayDensity = dm.density; + pixelRatio = dm.density; + setEGLConfigChooser(8, 8, 8, 8, 16, 0); getHolder().setFormat(PixelFormat.RGB_888); setZOrderOnTop(true); @@ -123,6 +130,12 @@ public class GLCanvas extends GLSurfaceView @Override 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 public void onDrawFrame(GL10 gl) { runAll(mRunOnDraw); @@ -472,8 +485,8 @@ public class GLCanvas extends GLSurfaceView if (arraySizeForType(type) != arr.size()) { shader.runtimeException( "uniform '"+uniformName+ - "': Invalid array size: "+arr.size()+ - ". Expected: "+arraySizeForType(type)); + "': Invalid array size: "+arr.size()+ + ". Expected: "+arraySizeForType(type)); } uniformsFloatBuffer.put(uniformName, parseAsFloatArray(arr)); break; @@ -488,8 +501,8 @@ public class GLCanvas extends GLSurfaceView if (arraySizeForType(type) != arr2.size()) { shader.runtimeException( "uniform '"+uniformName+ - "': Invalid array size: "+arr2.size()+ - ". Expected: "+arraySizeForType(type)); + "': Invalid array size: "+arr2.size()+ + ". Expected: "+arraySizeForType(type)); } uniformsIntBuffer.put(uniformName, parseAsIntArray(arr2)); break; @@ -497,7 +510,7 @@ public class GLCanvas extends GLSurfaceView default: shader.runtimeException( "uniform '"+uniformName+ - "': type not supported: "+type); + "': type not supported: "+type); } } @@ -511,9 +524,9 @@ public class GLCanvas extends GLSurfaceView for (String uniformName: uniformTypes.keySet()) { if (!uniformsFloat.containsKey(uniformName) && - !uniformsInteger.containsKey(uniformName) && - !uniformsFloatBuffer.containsKey(uniformName) && - !uniformsIntBuffer.containsKey(uniformName)) { + !uniformsInteger.containsKey(uniformName) && + !uniformsFloatBuffer.containsKey(uniformName) && + !uniformsIntBuffer.containsKey(uniformName)) { shader.runtimeException("All defined uniforms must be provided. Missing '"+uniformName+"'"); } } @@ -525,8 +538,8 @@ public class GLCanvas extends GLSurfaceView uniformsIntBuffer, uniformsFloatBuffer, textures, - data.width, - data.height, + (int)(data.width * data.pixelRatio), + (int)(data.height * data.pixelRatio), data.fboId, contextChildren, children); @@ -538,7 +551,7 @@ public class GLCanvas extends GLSurfaceView .order(ByteOrder.nativeOrder()) .asFloatBuffer(); for (int i=0; i { private ExecutorSupplier executorSupplier; + @ReactProp(name="pixelRatio") + public void setPixelRatio (GLCanvas view, float pixelRatio) { + view.setPixelRatio(pixelRatio); + } + @ReactProp(name="nbContentTextures") public void setNbContentTextures (GLCanvas view, int nbContentTextures) { view.setNbContentTextures(nbContentTextures); diff --git a/android/src/main/java/com/projectseptember/RNGL/GLData.java b/android/src/main/java/com/projectseptember/RNGL/GLData.java index 61ad242ed20bfa5f898eceaea72dccdc726a4887..57964af70bc946a3e683319da5320c1d74ffadd0 100644 --- a/android/src/main/java/com/projectseptember/RNGL/GLData.java +++ b/android/src/main/java/com/projectseptember/RNGL/GLData.java @@ -10,17 +10,19 @@ public class GLData { final Integer shader; final ReadableMap uniforms; - final Integer width; - final Integer height; + final Double width; + final Double height; + final Double pixelRatio; final Integer fboId; final List contextChildren; final List children; - public GLData(Integer shader, ReadableMap uniforms, Integer width, Integer height, Integer fboId, List contextChildren, List children) { + public GLData(Integer shader, ReadableMap uniforms, Double width, Double height, Double pixelRatio, Integer fboId, List contextChildren, List children) { this.shader = shader; this.uniforms = uniforms; this.width = width; this.height = height; + this.pixelRatio = pixelRatio; this.fboId = fboId; this.contextChildren = contextChildren; this.children = children; @@ -37,11 +39,12 @@ public class GLData { public static GLData fromMap (ReadableMap map) { Integer shader = map.getInt("shader"); ReadableMap uniforms = map.getMap("uniforms"); - Integer width = (int) map.getDouble("width"); - Integer height = (int) map.getDouble("height"); + Double width = map.getDouble("width"); + Double height = map.getDouble("height"); + Double pixelRatio = map.getDouble("pixelRatio"); Integer fboId = map.getInt("fboId"); List children = fromArray(map.getArray("children")); List 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); } } diff --git a/ios/GLCanvas.m b/ios/GLCanvas.m index 178272a6af2c0e5ca69eec252a6d1ab2c804f1a4..d9708a6edfc0e2e3f8ef6116e0ad1a387d3ab009 100644 --- a/ios/GLCanvas.m +++ b/ios/GLCanvas.m @@ -178,6 +178,7 @@ RCT_NOT_IMPLEMENTED(-init) weak_traverseTree = traverseTree = ^GLRenderData *(GLData *data) { NSNumber *width = data.width; NSNumber *height = data.height; + NSNumber *pixelRatio = data.pixelRatio; int fboId = [data.fboId intValue]; NSMutableArray *contextChildren = [[NSMutableArray alloc] init]; @@ -275,8 +276,8 @@ RCT_NOT_IMPLEMENTED(-init) initWithShader:shader withUniforms:uniforms withTextures:textures - withWidth:width - withHeight:height + withWidth:(int)([width floatValue] * [pixelRatio floatValue]) + withHeight:(int)([height floatValue] * [pixelRatio floatValue]) withFboId:fboId withContextChildren:contextChildren withChildren:children]; @@ -404,16 +405,14 @@ RCT_NOT_IMPLEMENTED(-init) GLRenderData *rd = _renderData; if (!rd) return; RCT_PROFILE_BEGIN_EVENT(0, @"GLCanvas render", nil); - CGFloat scale = self.contentScaleFactor; @autoreleasepool { - CGFloat scale = RCTScreenScale(); void (^recDraw) (GLRenderData *renderData); __block __weak void (^weak_recDraw) (GLRenderData *renderData); weak_recDraw = recDraw = ^void(GLRenderData *renderData) { - float w = [renderData.width floatValue] * scale; - float h = [renderData.height floatValue] * scale; + int w = renderData.width; + int h = renderData.height; for (GLRenderData *child in renderData.contextChildren) weak_recDraw(child); diff --git a/ios/GLData.h b/ios/GLData.h index 4523918aff428a0fe32dfc80ffc7fc1a20184429..273330cd00ea301648bd8e8c2087f17b1a4f8639 100644 --- a/ios/GLData.h +++ b/ios/GLData.h @@ -8,6 +8,7 @@ @property (nonatomic) NSDictionary *uniforms; @property (nonatomic) NSNumber *width; @property (nonatomic) NSNumber *height; +@property (nonatomic) NSNumber *pixelRatio; @property (nonatomic) NSNumber *fboId; @property (nonatomic) NSArray *contextChildren; @property (nonatomic) NSArray *children; @@ -16,6 +17,7 @@ withUniforms: (NSDictionary *)uniforms withWidth: (NSNumber *)width withHeight: (NSNumber *)height + withPixelRatio: (NSNumber *)pixelRatio withFboId: (NSNumber *)fboId withContextChildren: (NSArray *)contextChildren withChildren: (NSArray *)children; diff --git a/ios/GLData.m b/ios/GLData.m index 246685431b6c4b4b8181950600ab8f28282ac212..faf9f36ea6030abca7a80639249ec234cfff27a9 100644 --- a/ios/GLData.m +++ b/ios/GLData.m @@ -6,6 +6,7 @@ withUniforms: (NSDictionary *)uniforms withWidth: (NSNumber *)width withHeight: (NSNumber *)height + withPixelRatio: (NSNumber *)pixelRatio withFboId: (NSNumber *)fboId withContextChildren: (NSArray *)contextChildren withChildren: (NSArray *)children @@ -15,6 +16,7 @@ self.uniforms = uniforms; self.width = width; self.height = height; + self.pixelRatio = pixelRatio; self.fboId = fboId; self.contextChildren = contextChildren; self.children = children; diff --git a/ios/GLRenderData.h b/ios/GLRenderData.h index 44ecf9d2fe03aa86509f21c650e658791bcde221..ae3ab257515de575a81b73e2f0a7b491fa38c004 100644 --- a/ios/GLRenderData.h +++ b/ios/GLRenderData.h @@ -7,8 +7,8 @@ @property (nonatomic) GLShader *shader; @property (nonatomic) NSDictionary *uniforms; @property (nonatomic) NSDictionary *textures; -@property (nonatomic) NSNumber *width; -@property (nonatomic) NSNumber *height; +@property (nonatomic) int width; +@property (nonatomic) int height; @property (nonatomic) int fboId; @property (nonatomic) NSArray *contextChildren; @property (nonatomic) NSArray *children; @@ -16,8 +16,8 @@ -(instancetype) initWithShader: (GLShader *)shader withUniforms:(NSDictionary *)uniforms withTextures: (NSDictionary *)textures - withWidth: (NSNumber *)width - withHeight: (NSNumber *)height + withWidth: (int)width + withHeight: (int)height withFboId: (int)fboId withContextChildren: (NSArray *)contextChildren withChildren: (NSArray *)children; diff --git a/ios/GLRenderData.m b/ios/GLRenderData.m index f4b6721c6ec01c8eeecf037813ed5a5f8e02633f..902b14a29c079ea260324829b3fcf2e149a30d0b 100644 --- a/ios/GLRenderData.m +++ b/ios/GLRenderData.m @@ -6,8 +6,8 @@ -(instancetype) initWithShader: (GLShader *)shader withUniforms:(NSDictionary *)uniforms withTextures: (NSDictionary *)textures - withWidth: (NSNumber *)width - withHeight: (NSNumber *)height + withWidth: (int)width + withHeight: (int)height withFboId: (int)fboId withContextChildren: (NSArray *)contextChildren withChildren: (NSArray *)children diff --git a/ios/RCTConvert+GLData.m b/ios/RCTConvert+GLData.m index e65f49cd84f828339c39663fbdfffb8900124148..786873ef66800a0b0a25d9eb1b5765fbe5f84c25 100644 --- a/ios/RCTConvert+GLData.m +++ b/ios/RCTConvert+GLData.m @@ -10,6 +10,7 @@ NSDictionary *uniforms = [self NSDictionary:json[@"uniforms"]]; NSNumber *width = [self NSNumber:json[@"width"]]; NSNumber *height = [self NSNumber:json[@"height"]]; + NSNumber *pixelRatio = [self NSNumber:json[@"pixelRatio"]]; NSNumber *fboId = [self NSNumber:json[@"fboId"]]; NSArray *contextChildrenJSON = [self NSArray: json[@"contextChildren"]]; NSArray *childrenJSON = [self NSArray: json[@"children"]]; @@ -30,6 +31,7 @@ withUniforms: uniforms withWidth: width withHeight: height + withPixelRatio: pixelRatio withFboId: fboId withContextChildren: contextChildren withChildren: children];