Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
scottmas committed Oct 29, 2024
2 parents e89b458 + 68ff192 commit cbab86a
Show file tree
Hide file tree
Showing 19 changed files with 455 additions and 31 deletions.
2 changes: 1 addition & 1 deletion apps/example/src/Cube/Cube.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ export function Cube() {

return (
<View style={style.container}>
<Canvas ref={ref} style={style.webgpu} />
<Canvas ref={ref} style={style.webgpu} androidTransparency />
</View>
);
}
Expand Down
2 changes: 1 addition & 1 deletion apps/example/src/GradientTiles/GradientTiles.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export function GradientTiles() {

return (
<View style={style.container}>
<Canvas ref={ref} style={style.webgpu} />
<Canvas ref={ref} style={style.webgpu} androidTransparency />
<View style={style.controls}>
<View style={style.buttonRow}>
<Text style={style.spanText}>span x: </Text>
Expand Down
6 changes: 4 additions & 2 deletions apps/example/src/Resize/Resize.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export const Resize = () => {
{
view: renderTargetView,
resolveTarget: context.getCurrentTexture().createView(),
clearValue: [0.2, 0.2, 0.2, 1.0],
clearValue: [0.5, 0.5, 0.5, 1],
loadOp: "clear",
storeOp: "store",
},
Expand Down Expand Up @@ -117,7 +117,9 @@ export const Resize = () => {

return (
<View style={{ flex: 1, alignItems: "center" }}>
<Animated.View style={{ width: width.current, flex: 1 }}>
<Animated.View
style={{ width: width.current, flex: 1, backgroundColor: "red" }}
>
<Canvas ref={ref} style={{ flex: 1 }} />
</Animated.View>
</View>
Expand Down
6 changes: 5 additions & 1 deletion apps/example/src/Triangle/HelloTriangle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,11 @@ export function HelloTriangle() {
return (
<View style={style.container}>
<View style={{ flex: 1, backgroundColor: "#3498db" }} />
<Canvas ref={ref} style={StyleSheet.absoluteFill} />
<Canvas
ref={ref}
style={StyleSheet.absoluteFill}
androidTransparency={true}
/>
</View>
);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/webgpu/android/cpp/cpp-adapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,4 @@ extern "C" JNIEXPORT void JNICALL Java_com_webgpu_WebGPUView_onSurfaceDestroy(
JNIEnv *env, jobject thiz, jint contextId) {
auto &registry = rnwgpu::SurfaceRegistry::getInstance();
registry.removeSurfaceInfo(contextId);
}
}
79 changes: 79 additions & 0 deletions packages/webgpu/android/src/main/java/com/webgpu/SurfaceView2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package com.webgpu;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.PixelFormat;
import android.os.Build;
import android.view.Surface;
import android.view.SurfaceControl;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;


@SuppressLint("ViewConstructor")
@RequiresApi(api = Build.VERSION_CODES.Q)
public class SurfaceView2 extends SurfaceView implements SurfaceHolder.Callback {

WebGPUAPI mApi;
SurfaceControl mSurfaceControl;
Surface mSurface;

public SurfaceView2(Context context, WebGPUAPI api) {
super(context);
mApi = api;
getHolder().addCallback(this);
}

@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);

}

@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
}

@Override
public void surfaceCreated(@NonNull SurfaceHolder holder) {
if (mSurfaceControl != null) {
SurfaceControl.Transaction tr = new SurfaceControl.Transaction();
tr.setVisibility(mSurfaceControl, true);
tr.reparent(mSurfaceControl, getSurfaceControl());
tr.apply();
} else {
SurfaceControl.Builder scb = new SurfaceControl.Builder();
scb.setName("WebGPUView");
scb.setOpaque(false);
scb.setBufferSize(getWidth(), getHeight());
scb.setParent(getSurfaceControl());
scb.setFormat(PixelFormat.RGBA_8888);
mSurfaceControl = scb.build();
mSurface = new Surface(mSurfaceControl);
mApi.surfaceCreated(mSurface);
}
SurfaceControl.Transaction tr = new SurfaceControl.Transaction();
tr.setVisibility(mSurfaceControl, true);
tr.apply();
}

@Override
public void surfaceChanged(@NonNull SurfaceHolder holder, int format, int width, int height) {
mApi.surfaceChanged(mSurface);
SurfaceControl.Transaction tr = new SurfaceControl.Transaction();
tr.setVisibility(mSurfaceControl, true);
tr.setBufferSize(mSurfaceControl, getWidth(), getHeight());
tr.apply();
}

@Override
public void surfaceDestroyed(@NonNull SurfaceHolder holder) {
SurfaceControl.Transaction tr = new SurfaceControl.Transaction();
tr.reparent(mSurfaceControl, null);
tr.apply();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package com.webgpu;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.PixelFormat;
import android.hardware.HardwareBuffer;
import android.media.Image;
import android.media.ImageReader;
import android.os.Build;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;

import java.util.LinkedList;
import java.util.Queue;

@SuppressLint("ViewConstructor")
@RequiresApi(api = Build.VERSION_CODES.Q)
public class WebGPUAHBView extends View {

private final Queue<ImageReader> mImageReaders = new LinkedList<>();
private Bitmap mBitmap = null;

private final Matrix matrix = new Matrix();

WebGPUAPI mApi;

public WebGPUAHBView(Context context, WebGPUAPI api) {
super(context);
mApi = api;
}

@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
long usage = HardwareBuffer.USAGE_GPU_SAMPLED_IMAGE |
HardwareBuffer.USAGE_GPU_COLOR_OUTPUT;
ImageReader imageReader = ImageReader.newInstance(getWidth(), getHeight(), PixelFormat.RGBA_8888, 2, usage);
if (mImageReaders.isEmpty()) {
mApi.surfaceCreated(imageReader.getSurface());
} else {
mApi.surfaceChanged(imageReader.getSurface());
}
imageReader.setOnImageAvailableListener(new ImageReader.OnImageAvailableListener() {
@Override
public void onImageAvailable(ImageReader reader) {
try (Image image = reader.acquireLatestImage()) {
if (image != null) {
HardwareBuffer hb = image.getHardwareBuffer();
if (hb != null) {
Bitmap bitmap = Bitmap.wrapHardwareBuffer(hb, null);
if (bitmap != null) {
mBitmap = bitmap;
hb.close();
invalidate();
ImageReader imageReader = mImageReaders.poll();
ImageReader ir;
while((ir = mImageReaders.poll()) != null) {
ir.close();
}
mImageReaders.add(imageReader);
}
}
}
}
}
}, null);
mImageReaders.add(imageReader);
}

@Override
protected void onDraw(@NonNull Canvas canvas) {
super.onDraw(canvas);
if (mBitmap != null) {
canvas.drawBitmap(mBitmap, matrix, null);
}
}

@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
mApi.surfaceDestroyed();
}
}
20 changes: 20 additions & 0 deletions packages/webgpu/android/src/main/java/com/webgpu/WebGPUAPI.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.webgpu;

import android.view.Surface;

import com.facebook.proguard.annotations.DoNotStrip;

public interface WebGPUAPI {

void surfaceCreated(
Surface surface
);

void surfaceChanged(
Surface surface
);

void surfaceDestroyed();

void surfaceOffscreen();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.webgpu;

import android.content.Context;
import android.view.Surface;
import android.view.View;
import com.facebook.proguard.annotations.DoNotStrip;

public abstract class WebGPUBaseView extends View {
protected Integer mContextId;

public WebGPUBaseView(Context context, Integer contextId) {
super(context);
mContextId = contextId;
}

protected void handleSurfaceCreate(Surface surface) {
float density = getResources().getDisplayMetrics().density;
float scaledWidth = getWidth() / density;
float scaledHeight = getHeight() / density;
onSurfaceCreate(surface, mContextId, scaledWidth, scaledHeight);
}

protected void handleSurfaceChanged(Surface surface) {
float density = getResources().getDisplayMetrics().density;
float scaledWidth = getWidth() / density;
float scaledHeight = getHeight() / density;
onSurfaceChanged(surface, mContextId, scaledWidth, scaledHeight);
}

protected void handleSurfaceDestroyed() {
onSurfaceDestroy(mContextId);
}

@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
}


@DoNotStrip
private native void onSurfaceCreate(
Surface surface,
int contextId,
float width,
float height
);

@DoNotStrip
private native void onSurfaceChanged(
Surface surface,
int contextId,
float width,
float height
);

@DoNotStrip
private native void onSurfaceDestroy(int contextId);

@DoNotStrip
protected native void switchToOffscreenSurface(int contextId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.webgpu;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import androidx.annotation.NonNull;

@SuppressLint("ViewConstructor")
public class WebGPUSurfaceView extends SurfaceView implements SurfaceHolder.Callback {

WebGPUAPI mApi;

public WebGPUSurfaceView(Context context, WebGPUAPI api) {
super(context);
mApi = api;
getHolder().addCallback(this);
}

@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
mApi.surfaceDestroyed();
}

@Override
public void surfaceCreated(@NonNull SurfaceHolder holder) {
mApi.surfaceCreated(holder.getSurface());
}

@Override
public void surfaceChanged(@NonNull SurfaceHolder holder, int format, int width, int height) {
mApi.surfaceChanged(holder.getSurface());
}

@Override
public void surfaceDestroyed(@NonNull SurfaceHolder holder) {
mApi.surfaceOffscreen();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.webgpu;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.SurfaceTexture;
import android.view.Surface;
import android.view.TextureView;
import androidx.annotation.NonNull;

import org.w3c.dom.Text;

@SuppressLint("ViewConstructor")
public class WebGPUTextureView extends TextureView implements TextureView.SurfaceTextureListener {

WebGPUAPI mApi;

public WebGPUTextureView(Context context, WebGPUAPI api) {
super(context);
mApi = api;
setOpaque(false);
setSurfaceTextureListener(this);
}

@Override
public void onSurfaceTextureAvailable(@NonNull SurfaceTexture surfaceTexture, int width, int height) {
Surface surface = new Surface(surfaceTexture);
mApi.surfaceCreated(surface);
}

@Override
public void onSurfaceTextureSizeChanged(@NonNull SurfaceTexture surfaceTexture, int width, int height) {
Surface surface = new Surface(surfaceTexture);
mApi.surfaceChanged(surface);
}

@Override
public boolean onSurfaceTextureDestroyed(@NonNull SurfaceTexture surfaceTexture) {
mApi.surfaceDestroyed();
return true;
}

@Override
public void onSurfaceTextureUpdated(@NonNull SurfaceTexture surfaceTexture) {
// No implementation needed
}
}
Loading

0 comments on commit cbab86a

Please sign in to comment.