|
| 1 | +import React, { useRef } from "react"; |
| 2 | +import { Button, ScrollView, Text, View } from "react-native"; |
| 3 | +import type { CanvasRef } from "react-native-webgpu"; |
| 4 | +import { Canvas } from "react-native-webgpu"; |
| 5 | + |
| 6 | +import { |
| 7 | + diagnosticStyles, |
| 8 | + drawClearFrame, |
| 9 | + initGPU, |
| 10 | + useDiagnosticLog, |
| 11 | +} from "./surfaceLifecycle"; |
| 12 | + |
| 13 | +// Repros for GPUCanvasContext entry points the WebGPU spec expects to fail |
| 14 | +// gracefully but that crash or silently misbehave in the native |
| 15 | +// implementation: |
| 16 | +// |
| 17 | +// 1. getCurrentTexture() before configure(): SurfaceInfo has no device yet. |
| 18 | +// The native size-changed path reconfigures with a null wgpu::Device |
| 19 | +// (CreateTexture on the offscreen path, Surface::Configure on-screen) and |
| 20 | +// crashes the app instead of throwing a catchable JS error. |
| 21 | +// |
| 22 | +// 2. configure() on a 0x0 canvas: Dawn refuses zero-sized textures, so |
| 23 | +// getCurrentTexture() wraps a null texture and createView() dereferences |
| 24 | +// it. A canvas can legitimately be measured at 0x0 for a frame (collapsed |
| 25 | +// layout, display: none equivalents), so this is reachable from ordinary |
| 26 | +// app code. |
| 27 | +// |
| 28 | +// 3. unconfigure(): the native method is an empty stub. After unconfigure() |
| 29 | +// the context keeps handing out textures as if still configured, where the |
| 30 | +// spec says the canvas should behave as if it was never configured. |
| 31 | +// |
| 32 | +// Each button is an independent repro; on a broken build the first two |
| 33 | +// terminate the app, so relaunch between attempts. |
| 34 | +export const ContextEdgeCases = () => { |
| 35 | + const ref = useRef<CanvasRef>(null); |
| 36 | + const { log, append } = useDiagnosticLog(); |
| 37 | + |
| 38 | + const getCurrentTextureUnconfigured = () => { |
| 39 | + try { |
| 40 | + const ctx = ref.current!.getContext("webgpu")!; |
| 41 | + append("getCurrentTexture() before configure()..."); |
| 42 | + const texture = ctx.getCurrentTexture(); |
| 43 | + append( |
| 44 | + `returned a ${texture.width}x${texture.height} texture (spec: should throw)`, |
| 45 | + ); |
| 46 | + } catch (e) { |
| 47 | + append(`threw (correct per spec): ${e}`); |
| 48 | + } |
| 49 | + }; |
| 50 | + |
| 51 | + const zeroSizedCanvas = async () => { |
| 52 | + try { |
| 53 | + const { device, format } = await initGPU(append); |
| 54 | + const ctx = ref.current!.getContext("webgpu")!; |
| 55 | + const canvas = ctx.canvas as HTMLCanvasElement; |
| 56 | + canvas.width = 0; |
| 57 | + canvas.height = 0; |
| 58 | + append("configure() with canvas.width = canvas.height = 0..."); |
| 59 | + ctx.configure({ device, format, alphaMode: "opaque" }); |
| 60 | + const texture = ctx.getCurrentTexture(); |
| 61 | + append(`getCurrentTexture() -> ${texture.width}x${texture.height}`); |
| 62 | + texture.createView(); |
| 63 | + append("createView() survived"); |
| 64 | + } catch (e) { |
| 65 | + append(`threw: ${e}`); |
| 66 | + } |
| 67 | + }; |
| 68 | + |
| 69 | + const unconfigureStub = async () => { |
| 70 | + try { |
| 71 | + const { device, format } = await initGPU(append); |
| 72 | + const ctx = ref.current!.getContext("webgpu")!; |
| 73 | + ctx.configure({ device, format, alphaMode: "opaque" }); |
| 74 | + drawClearFrame(device, ctx, 0); |
| 75 | + append("rendered one frame, calling unconfigure()..."); |
| 76 | + ctx.unconfigure(); |
| 77 | + const texture = ctx.getCurrentTexture(); |
| 78 | + append( |
| 79 | + `getCurrentTexture() after unconfigure() -> ${texture.width}x${texture.height} texture (spec: context should be unconfigured)`, |
| 80 | + ); |
| 81 | + } catch (e) { |
| 82 | + append(`threw (correct per spec): ${e}`); |
| 83 | + } |
| 84 | + }; |
| 85 | + |
| 86 | + return ( |
| 87 | + <View style={diagnosticStyles.container}> |
| 88 | + <View style={diagnosticStyles.controls}> |
| 89 | + <Text style={diagnosticStyles.description}> |
| 90 | + Each button exercises a context entry point that must fail gracefully |
| 91 | + per the WebGPU spec. On a broken build the first two crash the app. |
| 92 | + </Text> |
| 93 | + <Button |
| 94 | + title="getCurrentTexture() before configure()" |
| 95 | + onPress={getCurrentTextureUnconfigured} |
| 96 | + /> |
| 97 | + <Button title="configure() a 0x0 canvas" onPress={zeroSizedCanvas} /> |
| 98 | + <Button |
| 99 | + title="unconfigure() then getCurrentTexture()" |
| 100 | + onPress={unconfigureStub} |
| 101 | + /> |
| 102 | + </View> |
| 103 | + <Canvas ref={ref} style={diagnosticStyles.canvas} /> |
| 104 | + <ScrollView style={diagnosticStyles.log}> |
| 105 | + {log.map((line, i) => ( |
| 106 | + <Text key={i} style={diagnosticStyles.logLine}> |
| 107 | + {line} |
| 108 | + </Text> |
| 109 | + ))} |
| 110 | + </ScrollView> |
| 111 | + </View> |
| 112 | + ); |
| 113 | +}; |
0 commit comments