react-native-webgpu 0.21.4 → 0.23.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +15 -2
- package/android/CMakeLists.txt +10 -8
- package/android/build.gradle +111 -20
- package/android/cpp/AndroidAnimationFrameSource.h +136 -0
- package/android/cpp/AndroidPlatformContext.h +55 -0
- package/android/cpp/cpp-adapter.cpp +148 -2
- package/android/src/main/java/com/webgpu/WebGPUModule.java +21 -4
- package/android/src/main/java/com/webgpu/WebGPUNativeBufferView.java +370 -0
- package/android/src/main/java/com/webgpu/WebGPUNativeBufferViewManager.java +56 -0
- package/android/src/main/java/com/webgpu/WebGPUTextureView.java +21 -0
- package/android/src/main/java/com/webgpu/WebGPUVideoPlayer.java +1 -1
- package/android/src/main/java/com/webgpu/WebGPUView.java +19 -3
- package/android/src/main/java/com/webgpu/WebGPUViewManager.java +10 -0
- package/android/src/main/java/com/webgpu/WebGPUViewPackage.java +2 -2
- package/android/src/newarch/com/webgpu/WebGPUNativeBufferViewManagerSpec.java +24 -0
- package/apple/AppleAnimationFrameSource.mm +208 -0
- package/apple/ApplePlatformContext.h +6 -0
- package/apple/ApplePlatformContext.mm +73 -0
- package/apple/MetalLayerColorSpace.h +17 -0
- package/apple/MetalLayerColorSpace.mm +36 -57
- package/apple/MetalView.h +4 -0
- package/apple/MetalView.mm +14 -0
- package/apple/NativeBufferFenceWait.mm +88 -0
- package/apple/RNWGUIKit.h +4 -1
- package/apple/WebGPUModule.mm +26 -1
- package/apple/WebGPUNativeBufferView.h +15 -0
- package/apple/WebGPUNativeBufferView.mm +200 -0
- package/apple/WebGPUView.h +2 -1
- package/apple/WebGPUView.mm +6 -0
- package/binaries.json +156 -0
- package/cpp/jsi/NativeObject.h +12 -5
- package/cpp/rnwgpu/AnimationFrames.cpp +576 -0
- package/cpp/rnwgpu/AnimationFrames.h +233 -0
- package/cpp/rnwgpu/CanvasColorSpace.h +16 -0
- package/cpp/rnwgpu/NativeBufferViewRegistry.cpp +396 -0
- package/cpp/rnwgpu/NativeBufferViewRegistry.h +163 -0
- package/cpp/rnwgpu/PlatformContext.h +20 -0
- package/cpp/rnwgpu/RNWebGPUManager.cpp +17 -4
- package/cpp/rnwgpu/SurfaceRegistry.h +26 -30
- package/cpp/rnwgpu/api/GPUAdapter.cpp +1 -1
- package/cpp/rnwgpu/api/GPUCanvasContext.cpp +41 -3
- package/cpp/rnwgpu/api/GPUCanvasContext.h +7 -3
- package/cpp/rnwgpu/api/GPUDevice.cpp +16 -131
- package/cpp/rnwgpu/api/GPUDevice.h +0 -18
- package/cpp/rnwgpu/api/GPUExternalTexture.cpp +35 -3
- package/cpp/rnwgpu/api/GPUExternalTexture.h +4 -4
- package/cpp/rnwgpu/api/GPUQueue.cpp +1 -1
- package/cpp/rnwgpu/api/GPUQueue.h +1 -1
- package/cpp/rnwgpu/api/GPUSharedFence.h +1 -0
- package/cpp/rnwgpu/api/GPUSharedTextureMemory.cpp +11 -5
- package/cpp/rnwgpu/api/GPUSharedTextureMemory.h +21 -2
- package/cpp/rnwgpu/api/GPUTexture.h +18 -0
- package/cpp/rnwgpu/api/NativeFrameFeatures.h +1 -1
- package/cpp/rnwgpu/api/NativeFrameImport.cpp +20 -17
- package/cpp/rnwgpu/api/NativeFrameImport.h +31 -20
- package/cpp/rnwgpu/api/RNWebGPU.h +201 -3
- package/cpp/rnwgpu/api/VideoFrame.h +5 -4
- package/cpp/rnwgpu/api/VideoFrameBlit.h +2 -2
- package/cpp/rnwgpu/api/descriptors/GPUExternalTextureDescriptor.h +21 -14
- package/cpp/rnwgpu/api/descriptors/GPUImageCopyExternalImage.h +1 -1
- package/cpp/rnwgpu/api/descriptors/GPUSharedTextureMemoryDescriptor.h +17 -9
- package/cpp/rnwgpu/async/RuntimeContext.cpp +21 -4
- package/cpp/rnwgpu/async/RuntimeContext.h +8 -0
- package/lib/commonjs/Canvas.js +33 -1
- package/lib/commonjs/Canvas.js.map +1 -1
- package/lib/commonjs/NativeBufferView.js +131 -0
- package/lib/commonjs/NativeBufferView.js.map +1 -0
- package/lib/commonjs/NativeBufferViewNativeComponent.js +10 -0
- package/lib/commonjs/NativeBufferViewNativeComponent.js.map +1 -0
- package/lib/commonjs/WebGPUViewNativeComponent.js.map +1 -1
- package/lib/commonjs/WebGPUViewNativeComponent.web.js +2 -0
- package/lib/commonjs/WebGPUViewNativeComponent.web.js.map +1 -1
- package/lib/commonjs/animationFrame.js +91 -0
- package/lib/commonjs/animationFrame.js.map +1 -0
- package/lib/commonjs/index.js +16 -1
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/main/index.js +12 -0
- package/lib/commonjs/main/index.js.map +1 -1
- package/lib/commonjs/mock.js +30 -1
- package/lib/commonjs/mock.js.map +1 -1
- package/lib/commonjs/video.js +14 -4
- package/lib/commonjs/video.js.map +1 -1
- package/lib/module/Canvas.js +33 -1
- package/lib/module/Canvas.js.map +1 -1
- package/lib/module/NativeBufferView.js +122 -0
- package/lib/module/NativeBufferView.js.map +1 -0
- package/lib/module/NativeBufferViewNativeComponent.js +4 -0
- package/lib/module/NativeBufferViewNativeComponent.js.map +1 -0
- package/lib/module/WebGPUViewNativeComponent.js.map +1 -1
- package/lib/module/WebGPUViewNativeComponent.web.js +2 -0
- package/lib/module/WebGPUViewNativeComponent.web.js.map +1 -1
- package/lib/module/animationFrame.js +83 -0
- package/lib/module/animationFrame.js.map +1 -0
- package/lib/module/index.js +1 -0
- package/lib/module/index.js.map +1 -1
- package/lib/module/main/index.js +1 -0
- package/lib/module/main/index.js.map +1 -1
- package/lib/module/mock.js +21 -0
- package/lib/module/mock.js.map +1 -1
- package/lib/module/video.js +11 -2
- package/lib/module/video.js.map +1 -1
- package/lib/typescript/lib/commonjs/Canvas.d.ts +3 -1
- package/lib/typescript/lib/commonjs/Canvas.d.ts.map +1 -1
- package/lib/typescript/lib/commonjs/NativeBufferView.d.ts +48 -0
- package/lib/typescript/lib/commonjs/NativeBufferView.d.ts.map +1 -0
- package/lib/typescript/lib/commonjs/NativeBufferViewNativeComponent.d.ts +5 -0
- package/lib/typescript/lib/commonjs/NativeBufferViewNativeComponent.d.ts.map +1 -0
- package/lib/typescript/lib/commonjs/WebGPUViewNativeComponent.web.d.ts.map +1 -1
- package/lib/typescript/lib/commonjs/animationFrame.d.ts +54 -0
- package/lib/typescript/lib/commonjs/animationFrame.d.ts.map +1 -0
- package/lib/typescript/lib/commonjs/index.d.ts +2 -0
- package/lib/typescript/lib/commonjs/mock.d.ts +12 -0
- package/lib/typescript/lib/commonjs/mock.d.ts.map +1 -1
- package/lib/typescript/lib/commonjs/video.d.ts +3 -2
- package/lib/typescript/lib/commonjs/video.d.ts.map +1 -1
- package/lib/typescript/lib/module/Canvas.d.ts +3 -1
- package/lib/typescript/lib/module/Canvas.d.ts.map +1 -1
- package/lib/typescript/lib/module/NativeBufferView.d.ts +14 -0
- package/lib/typescript/lib/module/NativeBufferView.d.ts.map +1 -0
- package/lib/typescript/lib/module/NativeBufferViewNativeComponent.d.ts +3 -0
- package/lib/typescript/lib/module/NativeBufferViewNativeComponent.d.ts.map +1 -0
- package/lib/typescript/lib/module/WebGPUViewNativeComponent.web.d.ts.map +1 -1
- package/lib/typescript/lib/module/animationFrame.d.ts +3 -0
- package/lib/typescript/lib/module/animationFrame.d.ts.map +1 -0
- package/lib/typescript/lib/module/index.d.ts +1 -0
- package/lib/typescript/lib/module/main/index.d.ts +1 -0
- package/lib/typescript/lib/module/mock.d.ts +14 -0
- package/lib/typescript/lib/module/mock.d.ts.map +1 -1
- package/lib/typescript/lib/module/video.d.ts +3 -2
- package/lib/typescript/lib/module/video.d.ts.map +1 -1
- package/lib/typescript/src/Canvas.d.ts +17 -1
- package/lib/typescript/src/Canvas.d.ts.map +1 -1
- package/lib/typescript/src/NativeBufferView.d.ts +99 -0
- package/lib/typescript/src/NativeBufferView.d.ts.map +1 -0
- package/lib/typescript/src/NativeBufferViewNativeComponent.d.ts +13 -0
- package/lib/typescript/src/NativeBufferViewNativeComponent.d.ts.map +1 -0
- package/lib/typescript/src/WebGPUViewNativeComponent.d.ts +2 -0
- package/lib/typescript/src/WebGPUViewNativeComponent.d.ts.map +1 -1
- package/lib/typescript/src/WebGPUViewNativeComponent.web.d.ts +2 -0
- package/lib/typescript/src/WebGPUViewNativeComponent.web.d.ts.map +1 -1
- package/lib/typescript/src/animationFrame.d.ts +59 -0
- package/lib/typescript/src/animationFrame.d.ts.map +1 -0
- package/lib/typescript/src/index.d.ts +18 -8
- package/lib/typescript/src/index.d.ts.map +1 -1
- package/lib/typescript/src/main/index.d.ts +1 -0
- package/lib/typescript/src/main/index.d.ts.map +1 -1
- package/lib/typescript/src/mock.d.ts +11 -0
- package/lib/typescript/src/mock.d.ts.map +1 -1
- package/lib/typescript/src/types.d.ts +32 -31
- package/lib/typescript/src/types.d.ts.map +1 -1
- package/lib/typescript/src/video.d.ts +12 -4
- package/lib/typescript/src/video.d.ts.map +1 -1
- package/package.json +8 -2
- package/react-native-webgpu.podspec +1 -1
- package/src/Canvas.tsx +67 -1
- package/src/NativeBufferView.tsx +199 -0
- package/src/NativeBufferViewNativeComponent.ts +20 -0
- package/src/WebGPUViewNativeComponent.ts +4 -0
- package/src/WebGPUViewNativeComponent.web.ts +6 -0
- package/src/animationFrame.ts +82 -0
- package/src/index.tsx +45 -39
- package/src/main/index.tsx +1 -0
- package/src/mock.ts +35 -0
- package/src/types.ts +61 -87
- package/src/video.ts +20 -7
- package/cpp/rnwgpu/api/GPUNativeBufferExternalTexture.h +0 -60
- package/cpp/rnwgpu/api/GPUNativeBufferTexture.h +0 -52
- package/cpp/rnwgpu/api/GPUNativeBufferWrapper.cpp +0 -144
- package/cpp/rnwgpu/api/GPUNativeBufferWrapper.h +0 -143
- package/cpp/rnwgpu/api/descriptors/GPUNativeBufferTextureDescriptor.h +0 -55
|
@@ -16,6 +16,7 @@ import com.facebook.proguard.annotations.DoNotStrip;
|
|
|
16
16
|
import com.facebook.react.bridge.ReactApplicationContext;
|
|
17
17
|
import com.facebook.react.bridge.JavaScriptContextHolder;
|
|
18
18
|
import com.facebook.react.bridge.LifecycleEventListener;
|
|
19
|
+
import com.facebook.react.common.LifecycleState;
|
|
19
20
|
import com.facebook.react.bridge.ReactMethod;
|
|
20
21
|
import com.facebook.react.bridge.UIManager;
|
|
21
22
|
import com.facebook.react.bridge.UiThreadUtil;
|
|
@@ -37,6 +38,8 @@ public class WebGPUModule extends NativeWebGPUModuleSpec implements LifecycleEve
|
|
|
37
38
|
super(reactContext);
|
|
38
39
|
// Initialize the C++ module
|
|
39
40
|
initialize();
|
|
41
|
+
// Animation frames follow the host activity: resumed, or not.
|
|
42
|
+
setForegroundNative(reactContext.getLifecycleState() == LifecycleState.RESUMED);
|
|
40
43
|
reactContext.addLifecycleEventListener(this);
|
|
41
44
|
}
|
|
42
45
|
|
|
@@ -47,15 +50,19 @@ public class WebGPUModule extends NativeWebGPUModuleSpec implements LifecycleEve
|
|
|
47
50
|
}
|
|
48
51
|
|
|
49
52
|
@Override
|
|
50
|
-
public void onHostResume() {
|
|
53
|
+
public void onHostResume() {
|
|
54
|
+
setForegroundNative(true);
|
|
55
|
+
}
|
|
51
56
|
|
|
52
57
|
/**
|
|
53
|
-
* The app is going to the background:
|
|
54
|
-
*
|
|
55
|
-
*
|
|
58
|
+
* The app is going to the background: no animation frames until it is back
|
|
59
|
+
* (see AnimationFrameDispatcher::setForeground), and the last moment to
|
|
60
|
+
* write what Dawn's pipeline caches hold before the process may be killed
|
|
61
|
+
* (see GPUDevice::flushPipelineCaches).
|
|
56
62
|
*/
|
|
57
63
|
@Override
|
|
58
64
|
public void onHostPause() {
|
|
65
|
+
setForegroundNative(false);
|
|
59
66
|
flushPipelineCaches();
|
|
60
67
|
}
|
|
61
68
|
|
|
@@ -65,6 +72,16 @@ public class WebGPUModule extends NativeWebGPUModuleSpec implements LifecycleEve
|
|
|
65
72
|
@DoNotStrip
|
|
66
73
|
private native void flushPipelineCaches();
|
|
67
74
|
|
|
75
|
+
/**
|
|
76
|
+
* Whether the app is in the foreground: the host activity is resumed (see
|
|
77
|
+
* AnimationFrameDispatcher::setForeground). An AChoreographer sends its
|
|
78
|
+
* frames to a paused app as well: without this, a requestAnimationFrame
|
|
79
|
+
* loop would keep rendering in the background, which one on React Native's
|
|
80
|
+
* own does not (JavaTimerManager.onHostPause).
|
|
81
|
+
*/
|
|
82
|
+
@DoNotStrip
|
|
83
|
+
private static native void setForegroundNative(boolean foreground);
|
|
84
|
+
|
|
68
85
|
@OptIn(markerClass = FrameworkAPI.class)
|
|
69
86
|
@ReactMethod(isBlockingSynchronousMethod = true)
|
|
70
87
|
public boolean install() {
|
|
@@ -0,0 +1,370 @@
|
|
|
1
|
+
package com.webgpu;
|
|
2
|
+
|
|
3
|
+
import android.annotation.SuppressLint;
|
|
4
|
+
import android.content.Context;
|
|
5
|
+
import android.graphics.Bitmap;
|
|
6
|
+
import android.graphics.Canvas;
|
|
7
|
+
import android.graphics.ColorSpace;
|
|
8
|
+
import android.graphics.Paint;
|
|
9
|
+
import android.graphics.Rect;
|
|
10
|
+
import android.hardware.HardwareBuffer;
|
|
11
|
+
import android.os.Build;
|
|
12
|
+
import android.os.Handler;
|
|
13
|
+
import android.os.Looper;
|
|
14
|
+
import android.util.Log;
|
|
15
|
+
import android.view.View;
|
|
16
|
+
|
|
17
|
+
import androidx.annotation.Keep;
|
|
18
|
+
import androidx.annotation.Nullable;
|
|
19
|
+
import androidx.annotation.RequiresApi;
|
|
20
|
+
|
|
21
|
+
import com.facebook.react.bridge.Arguments;
|
|
22
|
+
import com.facebook.react.bridge.ReactContext;
|
|
23
|
+
import com.facebook.react.bridge.WritableMap;
|
|
24
|
+
import com.facebook.react.uimanager.UIManagerHelper;
|
|
25
|
+
import com.facebook.react.uimanager.events.Event;
|
|
26
|
+
import com.facebook.react.uimanager.events.EventDispatcher;
|
|
27
|
+
|
|
28
|
+
import java.util.concurrent.atomic.AtomicBoolean;
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* NativeBufferView: displays a native buffer that JS presented
|
|
32
|
+
* (RNWebGPU.presentNativeBuffer), with no swap chain and no GPU device on the display side.
|
|
33
|
+
*
|
|
34
|
+
* <p>The contract: a buffer is never written again while it is displayed (the producer renders the
|
|
35
|
+
* next content into another buffer), and this view never allocates one. It reports its size in
|
|
36
|
+
* physical pixels (onPixelSizeChange) and draws whatever it is handed, scaled to its bounds by
|
|
37
|
+
* {@code resizeMode}, until the producer catches up with a buffer of the new size.
|
|
38
|
+
*
|
|
39
|
+
* <p>Native code waits for the buffer's fence off the UI thread (NativeBufferViewRegistry) and then
|
|
40
|
+
* wakes this view; it shows the buffer on the next vsync. API 29 and above: the buffer is drawn
|
|
41
|
+
* inline, zero-copy, through {@link Bitmap#wrapHardwareBuffer}, which takes its own reference on
|
|
42
|
+
* it. API 26 to 28: the pixels are read back into a regular Bitmap, and the buffer is free right
|
|
43
|
+
* after the copy.
|
|
44
|
+
*/
|
|
45
|
+
@SuppressLint("ViewConstructor")
|
|
46
|
+
public class WebGPUNativeBufferView extends View {
|
|
47
|
+
|
|
48
|
+
private static final String TAG = "WebGPUNativeBufferView";
|
|
49
|
+
|
|
50
|
+
static final String PIXEL_SIZE_CHANGE_EVENT = "topPixelSizeChange";
|
|
51
|
+
|
|
52
|
+
// NativeBufferContent metadata written by nTakeContent.
|
|
53
|
+
private static final int INFO_SEQUENCE = 0;
|
|
54
|
+
private static final int INFO_WIDTH = 1;
|
|
55
|
+
private static final int INFO_HEIGHT = 2;
|
|
56
|
+
private static final int INFO_HALF_FLOAT = 3;
|
|
57
|
+
private static final int INFO_DISPLAY_P3 = 4;
|
|
58
|
+
private static final int INFO_OPAQUE = 5;
|
|
59
|
+
private static final int INFO_SIZE = 6;
|
|
60
|
+
|
|
61
|
+
private enum ResizeMode { STRETCH, CONTAIN, COVER, TOP_LEFT }
|
|
62
|
+
|
|
63
|
+
private final Paint mPaint = new Paint(Paint.FILTER_BITMAP_FLAG);
|
|
64
|
+
private final Rect mSrc = new Rect();
|
|
65
|
+
private final Rect mDst = new Rect();
|
|
66
|
+
private final long[] mInfo = new long[INFO_SIZE];
|
|
67
|
+
|
|
68
|
+
// Native -> UI thread wake-up, coalesced into at most one pending consume().
|
|
69
|
+
private final Handler mMainHandler = new Handler(Looper.getMainLooper());
|
|
70
|
+
private final AtomicBoolean mConsumePosted = new AtomicBoolean(false);
|
|
71
|
+
private final Runnable mConsume = this::consume;
|
|
72
|
+
|
|
73
|
+
private int mViewId;
|
|
74
|
+
private long mSequence;
|
|
75
|
+
private ResizeMode mResizeMode = ResizeMode.STRETCH;
|
|
76
|
+
@Nullable private Bitmap mBitmap;
|
|
77
|
+
private int mReportedWidth;
|
|
78
|
+
private int mReportedHeight;
|
|
79
|
+
|
|
80
|
+
public WebGPUNativeBufferView(Context context) {
|
|
81
|
+
super(context);
|
|
82
|
+
setWillNotDraw(false);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// --- Props -------------------------------------------------------------------
|
|
86
|
+
|
|
87
|
+
public void setViewId(int viewId) {
|
|
88
|
+
if (viewId == mViewId) {
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
if (mViewId != 0) {
|
|
92
|
+
nDetach(mViewId);
|
|
93
|
+
}
|
|
94
|
+
mViewId = viewId;
|
|
95
|
+
mSequence = 0;
|
|
96
|
+
if (viewId != 0) {
|
|
97
|
+
// Calls onNativeContentReady right away when content is already there.
|
|
98
|
+
nAttach(viewId);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
public void setResizeMode(@Nullable String value) {
|
|
103
|
+
ResizeMode mode;
|
|
104
|
+
if ("contain".equals(value)) {
|
|
105
|
+
mode = ResizeMode.CONTAIN;
|
|
106
|
+
} else if ("cover".equals(value)) {
|
|
107
|
+
mode = ResizeMode.COVER;
|
|
108
|
+
} else if ("topLeft".equals(value)) {
|
|
109
|
+
mode = ResizeMode.TOP_LEFT;
|
|
110
|
+
} else {
|
|
111
|
+
mode = ResizeMode.STRETCH;
|
|
112
|
+
}
|
|
113
|
+
if (mode != mResizeMode) {
|
|
114
|
+
mResizeMode = mode;
|
|
115
|
+
invalidate();
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// --- Size --------------------------------------------------------------------
|
|
120
|
+
|
|
121
|
+
@Override
|
|
122
|
+
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
|
|
123
|
+
super.onSizeChanged(w, h, oldw, oldh);
|
|
124
|
+
// The size the producer should render at for a 1:1 display. Layout has
|
|
125
|
+
// already rounded it to whole pixels; JS gets exactly these numbers.
|
|
126
|
+
if (w > 0 && h > 0 && (w != mReportedWidth || h != mReportedHeight)) {
|
|
127
|
+
mReportedWidth = w;
|
|
128
|
+
mReportedHeight = h;
|
|
129
|
+
dispatchPixelSize(w, h);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
private void dispatchPixelSize(int width, int height) {
|
|
134
|
+
Context context = getContext();
|
|
135
|
+
if (!(context instanceof ReactContext)) {
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
ReactContext reactContext = (ReactContext) context;
|
|
139
|
+
EventDispatcher dispatcher = UIManagerHelper.getEventDispatcherForReactTag(reactContext, getId());
|
|
140
|
+
if (dispatcher != null) {
|
|
141
|
+
dispatcher.dispatchEvent(
|
|
142
|
+
new PixelSizeChangeEvent(UIManagerHelper.getSurfaceId(this), getId(), width, height));
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
// --- Content (UI thread, woken by native) ---------------------------------------
|
|
147
|
+
|
|
148
|
+
/** Called by native, from any thread, when new content is ready. */
|
|
149
|
+
@Keep
|
|
150
|
+
private void onNativeContentReady() {
|
|
151
|
+
if (mConsumePosted.compareAndSet(false, true)) {
|
|
152
|
+
mMainHandler.post(mConsume);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
private void consume() {
|
|
157
|
+
mConsumePosted.set(false);
|
|
158
|
+
if (mViewId == 0) {
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
HardwareBuffer buffer = nTakeContent(mViewId, mSequence, mInfo);
|
|
162
|
+
if (buffer == null) {
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
mSequence = mInfo[INFO_SEQUENCE];
|
|
166
|
+
Bitmap bitmap;
|
|
167
|
+
try {
|
|
168
|
+
bitmap = toBitmap(buffer);
|
|
169
|
+
} finally {
|
|
170
|
+
// The Bitmap holds its own reference (or a copy below API 29).
|
|
171
|
+
buffer.close();
|
|
172
|
+
}
|
|
173
|
+
if (bitmap == null) {
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
176
|
+
Bitmap previous = mBitmap;
|
|
177
|
+
mBitmap = bitmap;
|
|
178
|
+
invalidate();
|
|
179
|
+
// The display list recorded with the previous bitmap keeps its own native
|
|
180
|
+
// reference until it is replaced; this only drops ours.
|
|
181
|
+
if (previous != null && !previous.isRecycled()) {
|
|
182
|
+
previous.recycle();
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
@Nullable
|
|
187
|
+
private Bitmap toBitmap(HardwareBuffer buffer) {
|
|
188
|
+
final boolean halfFloat = mInfo[INFO_HALF_FLOAT] != 0;
|
|
189
|
+
final ColorSpace colorSpace = colorSpaceFor(halfFloat, mInfo[INFO_DISPLAY_P3] != 0);
|
|
190
|
+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
|
|
191
|
+
return wrap(buffer, colorSpace);
|
|
192
|
+
}
|
|
193
|
+
return readBack(buffer, halfFloat, colorSpace);
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
@RequiresApi(api = Build.VERSION_CODES.Q)
|
|
197
|
+
@Nullable
|
|
198
|
+
private static Bitmap wrap(HardwareBuffer buffer, ColorSpace colorSpace) {
|
|
199
|
+
try {
|
|
200
|
+
Bitmap bitmap = Bitmap.wrapHardwareBuffer(buffer, colorSpace);
|
|
201
|
+
if (bitmap == null) {
|
|
202
|
+
Log.e(TAG, "Bitmap.wrapHardwareBuffer failed: the buffer needs GPU_SAMPLED_IMAGE usage");
|
|
203
|
+
}
|
|
204
|
+
return bitmap;
|
|
205
|
+
} catch (IllegalArgumentException e) {
|
|
206
|
+
Log.e(TAG, "Bitmap.wrapHardwareBuffer failed", e);
|
|
207
|
+
return null;
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
// Android 8 and 9: no wrapHardwareBuffer. The pixels are copied into a
|
|
212
|
+
// regular Bitmap (the buffer needs CPU_READ_RARELY usage, which
|
|
213
|
+
// createNativeBuffer adds there); the copy is free to go right after.
|
|
214
|
+
@Nullable
|
|
215
|
+
private Bitmap readBack(HardwareBuffer buffer, boolean halfFloat, ColorSpace colorSpace) {
|
|
216
|
+
final int width = (int) mInfo[INFO_WIDTH];
|
|
217
|
+
final int height = (int) mInfo[INFO_HEIGHT];
|
|
218
|
+
final boolean hasAlpha = mInfo[INFO_OPAQUE] == 0;
|
|
219
|
+
final Bitmap.Config config = halfFloat ? Bitmap.Config.RGBA_F16 : Bitmap.Config.ARGB_8888;
|
|
220
|
+
Bitmap bitmap;
|
|
221
|
+
try {
|
|
222
|
+
bitmap = Bitmap.createBitmap(width, height, config, hasAlpha, colorSpace);
|
|
223
|
+
} catch (IllegalArgumentException e) {
|
|
224
|
+
// Android 8 and 9 only take a linear color space for RGBA_F16 bitmaps.
|
|
225
|
+
Log.w(TAG, "no " + colorSpace.getName() + " bitmap for this format; colors may be off");
|
|
226
|
+
bitmap = Bitmap.createBitmap(width, height, config);
|
|
227
|
+
}
|
|
228
|
+
if (!nReadPixels(buffer, bitmap)) {
|
|
229
|
+
Log.e(TAG, "reading the buffer back failed (it needs CPU_READ_RARELY usage below API 29)");
|
|
230
|
+
bitmap.recycle();
|
|
231
|
+
return null;
|
|
232
|
+
}
|
|
233
|
+
return bitmap;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
private static ColorSpace colorSpaceFor(boolean halfFloat, boolean displayP3) {
|
|
237
|
+
if (displayP3) {
|
|
238
|
+
return ColorSpace.get(ColorSpace.Named.DISPLAY_P3);
|
|
239
|
+
}
|
|
240
|
+
return ColorSpace.get(halfFloat ? ColorSpace.Named.EXTENDED_SRGB : ColorSpace.Named.SRGB);
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
// --- Draw --------------------------------------------------------------------
|
|
244
|
+
|
|
245
|
+
@Override
|
|
246
|
+
protected void onDraw(Canvas canvas) {
|
|
247
|
+
super.onDraw(canvas);
|
|
248
|
+
Bitmap bitmap = mBitmap;
|
|
249
|
+
if (bitmap == null || bitmap.isRecycled()) {
|
|
250
|
+
return;
|
|
251
|
+
}
|
|
252
|
+
final int bw = bitmap.getWidth();
|
|
253
|
+
final int bh = bitmap.getHeight();
|
|
254
|
+
final int vw = getWidth();
|
|
255
|
+
final int vh = getHeight();
|
|
256
|
+
if (bw <= 0 || bh <= 0 || vw <= 0 || vh <= 0) {
|
|
257
|
+
return;
|
|
258
|
+
}
|
|
259
|
+
// A buffer that lags the view size (mid-resize) is scaled, never blank.
|
|
260
|
+
switch (mResizeMode) {
|
|
261
|
+
case CONTAIN: {
|
|
262
|
+
final float scale = Math.min((float) vw / bw, (float) vh / bh);
|
|
263
|
+
final int dw = Math.round(bw * scale);
|
|
264
|
+
final int dh = Math.round(bh * scale);
|
|
265
|
+
mSrc.set(0, 0, bw, bh);
|
|
266
|
+
mDst.set((vw - dw) / 2, (vh - dh) / 2, (vw - dw) / 2 + dw, (vh - dh) / 2 + dh);
|
|
267
|
+
break;
|
|
268
|
+
}
|
|
269
|
+
case COVER: {
|
|
270
|
+
// Crop the source to the view's aspect ratio rather than clipping.
|
|
271
|
+
final float scale = Math.max((float) vw / bw, (float) vh / bh);
|
|
272
|
+
final int sw = Math.min(bw, Math.round(vw / scale));
|
|
273
|
+
final int sh = Math.min(bh, Math.round(vh / scale));
|
|
274
|
+
mSrc.set((bw - sw) / 2, (bh - sh) / 2, (bw - sw) / 2 + sw, (bh - sh) / 2 + sh);
|
|
275
|
+
mDst.set(0, 0, vw, vh);
|
|
276
|
+
break;
|
|
277
|
+
}
|
|
278
|
+
case TOP_LEFT: {
|
|
279
|
+
// No scaling: crop instead of blur.
|
|
280
|
+
final int w = Math.min(bw, vw);
|
|
281
|
+
final int h = Math.min(bh, vh);
|
|
282
|
+
mSrc.set(0, 0, w, h);
|
|
283
|
+
mDst.set(0, 0, w, h);
|
|
284
|
+
break;
|
|
285
|
+
}
|
|
286
|
+
case STRETCH:
|
|
287
|
+
default:
|
|
288
|
+
mSrc.set(0, 0, bw, bh);
|
|
289
|
+
mDst.set(0, 0, vw, vh);
|
|
290
|
+
break;
|
|
291
|
+
}
|
|
292
|
+
if (bitmap.getConfig() == Bitmap.Config.HARDWARE && !canvas.isHardwareAccelerated()) {
|
|
293
|
+
// A software canvas (a view capture with view.draw(new Canvas(bitmap)), a
|
|
294
|
+
// software layer) throws on a hardware bitmap: draw a copy of the pixels.
|
|
295
|
+
Bitmap copy = softwareCopy(bitmap);
|
|
296
|
+
if (copy != null) {
|
|
297
|
+
canvas.drawBitmap(copy, mSrc, mDst, mPaint);
|
|
298
|
+
copy.recycle();
|
|
299
|
+
}
|
|
300
|
+
return;
|
|
301
|
+
}
|
|
302
|
+
canvas.drawBitmap(bitmap, mSrc, mDst, mPaint);
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
// Reads the buffer back; null when the pixels can't be read (the capture
|
|
306
|
+
// then shows nothing for this view).
|
|
307
|
+
@Nullable
|
|
308
|
+
private static Bitmap softwareCopy(Bitmap hardware) {
|
|
309
|
+
try {
|
|
310
|
+
return hardware.copy(Bitmap.Config.ARGB_8888, false);
|
|
311
|
+
} catch (RuntimeException e) {
|
|
312
|
+
Log.w(TAG, "no software copy of the buffer for a software canvas", e);
|
|
313
|
+
return null;
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
// --- Lifecycle -----------------------------------------------------------------
|
|
318
|
+
|
|
319
|
+
/** RN dropped the view for good (WebGPUNativeBufferViewManager.onDropViewInstance). */
|
|
320
|
+
public void destroy() {
|
|
321
|
+
if (mViewId != 0) {
|
|
322
|
+
nDetach(mViewId);
|
|
323
|
+
mViewId = 0;
|
|
324
|
+
}
|
|
325
|
+
mMainHandler.removeCallbacks(mConsume);
|
|
326
|
+
mConsumePosted.set(false);
|
|
327
|
+
if (mBitmap != null && !mBitmap.isRecycled()) {
|
|
328
|
+
mBitmap.recycle();
|
|
329
|
+
}
|
|
330
|
+
mBitmap = null;
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
// --- Event -------------------------------------------------------------------
|
|
334
|
+
|
|
335
|
+
private static class PixelSizeChangeEvent extends Event<PixelSizeChangeEvent> {
|
|
336
|
+
private final int mWidth;
|
|
337
|
+
private final int mHeight;
|
|
338
|
+
|
|
339
|
+
PixelSizeChangeEvent(int surfaceId, int viewTag, int width, int height) {
|
|
340
|
+
super(surfaceId, viewTag);
|
|
341
|
+
mWidth = width;
|
|
342
|
+
mHeight = height;
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
@Override
|
|
346
|
+
public String getEventName() {
|
|
347
|
+
return PIXEL_SIZE_CHANGE_EVENT;
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
@Override
|
|
351
|
+
protected WritableMap getEventData() {
|
|
352
|
+
WritableMap data = Arguments.createMap();
|
|
353
|
+
data.putInt("width", mWidth);
|
|
354
|
+
data.putInt("height", mHeight);
|
|
355
|
+
return data;
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
// --- Native (cpp-adapter.cpp) ------------------------------------------------
|
|
360
|
+
|
|
361
|
+
private native void nAttach(int viewId);
|
|
362
|
+
|
|
363
|
+
private static native void nDetach(int viewId);
|
|
364
|
+
|
|
365
|
+
// The newest content after `afterSequence` as a HardwareBuffer the caller
|
|
366
|
+
// closes, or null; its metadata goes to `info` (see INFO_*).
|
|
367
|
+
private static native HardwareBuffer nTakeContent(int viewId, long afterSequence, long[] info);
|
|
368
|
+
|
|
369
|
+
private static native boolean nReadPixels(HardwareBuffer buffer, Bitmap bitmap);
|
|
370
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
package com.webgpu;
|
|
2
|
+
|
|
3
|
+
import androidx.annotation.NonNull;
|
|
4
|
+
import androidx.annotation.Nullable;
|
|
5
|
+
|
|
6
|
+
import com.facebook.react.common.MapBuilder;
|
|
7
|
+
import com.facebook.react.module.annotations.ReactModule;
|
|
8
|
+
import com.facebook.react.uimanager.ThemedReactContext;
|
|
9
|
+
import com.facebook.react.uimanager.annotations.ReactProp;
|
|
10
|
+
|
|
11
|
+
import java.util.Map;
|
|
12
|
+
|
|
13
|
+
@ReactModule(name = WebGPUNativeBufferViewManager.NAME)
|
|
14
|
+
public class WebGPUNativeBufferViewManager
|
|
15
|
+
extends WebGPUNativeBufferViewManagerSpec<WebGPUNativeBufferView> {
|
|
16
|
+
|
|
17
|
+
public static final String NAME = "WebGPUNativeBufferView";
|
|
18
|
+
|
|
19
|
+
@NonNull
|
|
20
|
+
@Override
|
|
21
|
+
public String getName() {
|
|
22
|
+
return NAME;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
@NonNull
|
|
26
|
+
@Override
|
|
27
|
+
public WebGPUNativeBufferView createViewInstance(@NonNull ThemedReactContext context) {
|
|
28
|
+
return new WebGPUNativeBufferView(context);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
@Override
|
|
32
|
+
public void onDropViewInstance(@NonNull WebGPUNativeBufferView view) {
|
|
33
|
+
super.onDropViewInstance(view);
|
|
34
|
+
view.destroy();
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
@Override
|
|
38
|
+
@ReactProp(name = "viewId")
|
|
39
|
+
public void setViewId(WebGPUNativeBufferView view, int value) {
|
|
40
|
+
view.setViewId(value);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
@Override
|
|
44
|
+
@ReactProp(name = "resizeMode")
|
|
45
|
+
public void setResizeMode(WebGPUNativeBufferView view, @Nullable String value) {
|
|
46
|
+
view.setResizeMode(value);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
@Nullable
|
|
50
|
+
@Override
|
|
51
|
+
public Map<String, Object> getExportedCustomDirectEventTypeConstants() {
|
|
52
|
+
return MapBuilder.<String, Object>of(
|
|
53
|
+
WebGPUNativeBufferView.PIXEL_SIZE_CHANGE_EVENT,
|
|
54
|
+
MapBuilder.of("registrationName", "onPixelSizeChange"));
|
|
55
|
+
}
|
|
56
|
+
}
|
|
@@ -6,6 +6,7 @@ import android.graphics.SurfaceTexture;
|
|
|
6
6
|
import android.os.Build;
|
|
7
7
|
import android.view.Surface;
|
|
8
8
|
import android.view.TextureView;
|
|
9
|
+
import android.view.View;
|
|
9
10
|
import androidx.annotation.NonNull;
|
|
10
11
|
|
|
11
12
|
@SuppressLint("ViewConstructor")
|
|
@@ -55,6 +56,26 @@ public class WebGPUTextureView extends TextureView implements TextureView.Surfac
|
|
|
55
56
|
return true;
|
|
56
57
|
}
|
|
57
58
|
|
|
59
|
+
/**
|
|
60
|
+
* A frame was just presented from the main thread: have this draw show it.
|
|
61
|
+
*
|
|
62
|
+
* TextureView hears of a new frame through its SurfaceTexture's
|
|
63
|
+
* frame-available listener, a plain message on the main looper. A frame
|
|
64
|
+
* presented on the main thread during the Choreographer's animation
|
|
65
|
+
* callbacks (the Reanimated UI runtime's requestAnimationFrame) is followed
|
|
66
|
+
* by that frame's draw before the message runs: the draw shows nothing new,
|
|
67
|
+
* the message schedules the next draw, and the canvas updates every other
|
|
68
|
+
* frame (60 fps on a 120 Hz display). What the listener does is private
|
|
69
|
+
* (updateLayerAndInvalidate()); onVisibilityChanged(VISIBLE) does the same
|
|
70
|
+
* for a visible view, so this calls it before the draw. Main thread.
|
|
71
|
+
*/
|
|
72
|
+
void latchPresentedFrame() {
|
|
73
|
+
if (getSurfaceTexture() == null || !isShown()) {
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
super.onVisibilityChanged(this, View.VISIBLE);
|
|
77
|
+
}
|
|
78
|
+
|
|
58
79
|
@Override
|
|
59
80
|
public void onSurfaceTextureUpdated(@NonNull SurfaceTexture surfaceTexture) {
|
|
60
81
|
// No implementation needed
|
|
@@ -26,7 +26,7 @@ import java.util.Set;
|
|
|
26
26
|
* buffers the GPU can sample ({@link ImageFormat#PRIVATE} with
|
|
27
27
|
* {@link HardwareBuffer#USAGE_GPU_SAMPLED_IMAGE}). Each frame is handed to C++
|
|
28
28
|
* (AndroidVideoPlayer) as an {@link Image} that stays acquired until the
|
|
29
|
-
*
|
|
29
|
+
* NativeBuffer is released, which hands the buffer back to the decoder.
|
|
30
30
|
*
|
|
31
31
|
* The frames keep the decoder's native YUV layout, so they are sampled through
|
|
32
32
|
* GPUDevice.importExternalTexture. MediaPlayer owns the audio, the clock and
|
|
@@ -194,7 +194,8 @@ public class WebGPUView extends ReactViewGroup implements WebGPUAPI {
|
|
|
194
194
|
if (votesFrameRate) {
|
|
195
195
|
mFrameRateVote.attach(surface);
|
|
196
196
|
}
|
|
197
|
-
|
|
197
|
+
boolean latchOnMainThread = mKind == BackingViewKind.TEXTURE_VIEW;
|
|
198
|
+
onSurfaceCreate(surface, mContextId, width, height, votesFrameRate, latchOnMainThread);
|
|
198
199
|
}
|
|
199
200
|
|
|
200
201
|
@Override
|
|
@@ -221,6 +222,18 @@ public class WebGPUView extends ReactViewGroup implements WebGPUAPI {
|
|
|
221
222
|
mFrameRateVote.onAnimating();
|
|
222
223
|
}
|
|
223
224
|
|
|
225
|
+
/**
|
|
226
|
+
* Called from native (cpp-adapter.cpp) right after a frame is presented to
|
|
227
|
+
* a TextureView from the main thread, so that the draw of this frame shows
|
|
228
|
+
* it (see WebGPUTextureView.latchPresentedFrame).
|
|
229
|
+
*/
|
|
230
|
+
@DoNotStrip
|
|
231
|
+
void onFramePresentedOnMainThread() {
|
|
232
|
+
if (mView instanceof WebGPUTextureView) {
|
|
233
|
+
((WebGPUTextureView) mView).latchPresentedFrame();
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
|
|
224
237
|
@Override
|
|
225
238
|
public int getContextId() {
|
|
226
239
|
return mContextId;
|
|
@@ -236,14 +249,17 @@ public class WebGPUView extends ReactViewGroup implements WebGPUAPI {
|
|
|
236
249
|
}
|
|
237
250
|
|
|
238
251
|
// reportAnimation asks native to call onAnimating() when frames start being
|
|
239
|
-
// presented to this surface back to back
|
|
252
|
+
// presented to this surface back to back; latchOnMainThread, to call
|
|
253
|
+
// onFramePresentedOnMainThread() after each frame presented from the main
|
|
254
|
+
// thread.
|
|
240
255
|
@DoNotStrip
|
|
241
256
|
private native void onSurfaceCreate(
|
|
242
257
|
Surface surface,
|
|
243
258
|
int contextId,
|
|
244
259
|
float width,
|
|
245
260
|
float height,
|
|
246
|
-
boolean reportAnimation
|
|
261
|
+
boolean reportAnimation,
|
|
262
|
+
boolean latchOnMainThread
|
|
247
263
|
);
|
|
248
264
|
|
|
249
265
|
@DoNotStrip
|
|
@@ -54,6 +54,16 @@ public class WebGPUViewManager extends WebGPUViewManagerSpec<WebGPUView> {
|
|
|
54
54
|
view.setZOrderMediaOverlay(value);
|
|
55
55
|
}
|
|
56
56
|
|
|
57
|
+
// The colorspace props of the canvas, read on Apple platforms only: Android
|
|
58
|
+
// renders in sRGB.
|
|
59
|
+
@Override
|
|
60
|
+
@ReactProp(name = "appleDisplayP3")
|
|
61
|
+
public void setAppleDisplayP3(WebGPUView view, boolean value) {}
|
|
62
|
+
|
|
63
|
+
@Override
|
|
64
|
+
@ReactProp(name = "appleExtendedDynamicRange")
|
|
65
|
+
public void setAppleExtendedDynamicRange(WebGPUView view, boolean value) {}
|
|
66
|
+
|
|
57
67
|
@Override
|
|
58
68
|
protected void onAfterUpdateTransaction(@NonNull WebGPUView view) {
|
|
59
69
|
super.onAfterUpdateTransaction(view);
|
|
@@ -9,7 +9,7 @@ import com.facebook.react.module.model.ReactModuleInfo;
|
|
|
9
9
|
import com.facebook.react.module.model.ReactModuleInfoProvider;
|
|
10
10
|
import com.facebook.react.uimanager.ViewManager;
|
|
11
11
|
|
|
12
|
-
import java.util.
|
|
12
|
+
import java.util.Arrays;
|
|
13
13
|
import java.util.HashMap;
|
|
14
14
|
import java.util.List;
|
|
15
15
|
import java.util.Map;
|
|
@@ -45,6 +45,6 @@ public class WebGPUViewPackage extends TurboReactPackage {
|
|
|
45
45
|
|
|
46
46
|
@Override
|
|
47
47
|
public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
|
|
48
|
-
return
|
|
48
|
+
return Arrays.<ViewManager>asList(new WebGPUViewManager(), new WebGPUNativeBufferViewManager());
|
|
49
49
|
}
|
|
50
50
|
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
package com.webgpu;
|
|
2
|
+
|
|
3
|
+
import android.view.View;
|
|
4
|
+
|
|
5
|
+
import androidx.annotation.Nullable;
|
|
6
|
+
|
|
7
|
+
import com.facebook.react.uimanager.SimpleViewManager;
|
|
8
|
+
import com.facebook.react.uimanager.ViewManagerDelegate;
|
|
9
|
+
import com.facebook.react.viewmanagers.WebGPUNativeBufferViewManagerDelegate;
|
|
10
|
+
import com.facebook.react.viewmanagers.WebGPUNativeBufferViewManagerInterface;
|
|
11
|
+
|
|
12
|
+
public abstract class WebGPUNativeBufferViewManagerSpec<T extends View> extends SimpleViewManager<T> implements WebGPUNativeBufferViewManagerInterface<T> {
|
|
13
|
+
private final ViewManagerDelegate<T> mDelegate;
|
|
14
|
+
|
|
15
|
+
public WebGPUNativeBufferViewManagerSpec() {
|
|
16
|
+
mDelegate = new WebGPUNativeBufferViewManagerDelegate(this);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
@Nullable
|
|
20
|
+
@Override
|
|
21
|
+
protected ViewManagerDelegate<T> getDelegate() {
|
|
22
|
+
return mDelegate;
|
|
23
|
+
}
|
|
24
|
+
}
|