-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'upstream/main'
- Loading branch information
Showing
19 changed files
with
455 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
packages/webgpu/android/src/main/java/com/webgpu/SurfaceView2.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
88 changes: 88 additions & 0 deletions
88
packages/webgpu/android/src/main/java/com/webgpu/WebGPUAHBView.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
20
packages/webgpu/android/src/main/java/com/webgpu/WebGPUAPI.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
61 changes: 61 additions & 0 deletions
61
packages/webgpu/android/src/main/java/com/webgpu/WebGPUBaseView.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
41 changes: 41 additions & 0 deletions
41
packages/webgpu/android/src/main/java/com/webgpu/WebGPUSurfaceView.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
packages/webgpu/android/src/main/java/com/webgpu/WebGPUTextureView.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
Oops, something went wrong.