-
-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathinstance_lifecycle_native_test.go
More file actions
274 lines (240 loc) · 8.15 KB
/
Copy pathinstance_lifecycle_native_test.go
File metadata and controls
274 lines (240 loc) · 8.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
//go:build !rust && !(js && wasm)
package wgpu
import (
"errors"
"slices"
"testing"
"github.com/gogpu/gputypes"
"github.com/gogpu/wgpu/core"
"github.com/gogpu/wgpu/hal"
"github.com/gogpu/wgpu/hal/noop"
)
type instanceLifecycleDevice struct {
noop.Device
events *[]string
}
func (d *instanceLifecycleDevice) Destroy() {
*d.events = append(*d.events, "device-destroy")
}
type instanceLifecycleSurface struct {
noop.Surface
events *[]string
}
func (s *instanceLifecycleSurface) Destroy() {
*s.events = append(*s.events, "surface-destroy")
}
type instanceLifecycleConfiguredSurface struct {
noop.Surface
events *[]string
}
func (s *instanceLifecycleConfiguredSurface) DiscardTexture(hal.SurfaceTexture) {
*s.events = append(*s.events, "surface-discard")
}
func (s *instanceLifecycleConfiguredSurface) Unconfigure(hal.Device) {
*s.events = append(*s.events, "surface-unconfigure")
}
func (s *instanceLifecycleConfiguredSurface) Destroy() {
*s.events = append(*s.events, "surface-destroy")
}
type instanceLifecycleTargetSource struct{}
func (*instanceLifecycleTargetSource) SurfaceTarget() (SurfaceTargetUnsafe, error) {
return SurfaceTargetFromAndroidNativeWindow(1), nil
}
type instanceLifecycleTargetSourceSurface struct {
noop.Surface
surface *Surface
events *[]string
}
func (s *instanceLifecycleTargetSourceSurface) Destroy() {
if s.surface.targetSource == nil {
*s.events = append(*s.events, "source-cleared-before-surface-destroy")
return
}
*s.events = append(*s.events, "surface-destroy-with-source")
}
func TestSurfaceReleaseRetainsTargetSourceThroughHALDestroy(t *testing.T) {
events := []string{}
targetSource := &instanceLifecycleTargetSource{}
firstRawSurface := &instanceLifecycleTargetSourceSurface{events: &events}
secondRawSurface := &instanceLifecycleTargetSourceSurface{events: &events}
surface := &Surface{
core: core.NewSurface(firstRawSurface, "target-source-lifecycle-test"),
targetSource: targetSource,
currentBackend: gputypes.BackendVulkan,
surfaceCreated: true,
halSurfaces: map[gputypes.Backend]hal.Surface{
gputypes.BackendVulkan: firstRawSurface,
gputypes.BackendGL: secondRawSurface,
},
}
firstRawSurface.surface = surface
secondRawSurface.surface = surface
surface.Release()
want := []string{"surface-destroy-with-source", "surface-destroy-with-source"}
if !slices.Equal(events, want) {
t.Fatalf("release events = %v, want %v", events, want)
}
if surface.targetSource != nil {
t.Fatal("surface retained target source after HAL destruction")
}
surface.Release()
if !slices.Equal(events, want) {
t.Fatalf("idempotent release changed events to %v", events)
}
}
func TestInstanceReleaseDestroysDevicesBeforeSurfaces(t *testing.T) {
events := []string{}
instance := &Instance{core: core.NewInstanceWithMock(nil)}
rawDevice := &instanceLifecycleDevice{events: &events}
queue := &Queue{hal: &noop.Queue{}, halDevice: rawDevice}
device := &Device{
core: core.NewDevice(rawDevice, nil, 0, gputypes.DefaultLimits(), "lifecycle-test"),
queue: queue,
}
queue.device = device
if err := instance.adoptDevice(device); err != nil {
t.Fatalf("adopt device: %v", err)
}
surface := &Surface{core: core.NewSurface(&instanceLifecycleSurface{events: &events}, "lifecycle-test")}
if err := instance.adoptSurface(surface); err != nil {
t.Fatalf("adopt surface: %v", err)
}
instance.Release()
want := []string{"device-destroy", "surface-destroy"}
if !slices.Equal(events, want) {
t.Fatalf("release events = %v, want %v", events, want)
}
if surface.HAL() != nil {
t.Fatal("retained surface exposed HAL after instance release")
}
instance.Release()
surface.Release()
device.Release()
if !slices.Equal(events, want) {
t.Fatalf("idempotent releases changed events to %v", events)
}
}
func TestInstanceReleaseRetiresAcquisitionBeforeDeviceAndPlatformSurface(t *testing.T) {
events := []string{}
instance := &Instance{core: core.NewInstanceWithMock(nil)}
rawDevice := &instanceLifecycleDevice{events: &events}
queue := &Queue{hal: &noop.Queue{}, halDevice: rawDevice}
device := &Device{
core: core.NewDevice(rawDevice, nil, 0, gputypes.DefaultLimits(), "configured-lifecycle-test"),
queue: queue,
}
queue.device = device
if err := instance.adoptDevice(device); err != nil {
t.Fatalf("adopt device: %v", err)
}
rawSurface := &instanceLifecycleConfiguredSurface{events: &events}
surface := &Surface{
core: core.NewSurface(rawSurface, "configured-lifecycle-test"),
surfaceCreated: true,
currentBackend: gputypes.BackendEmpty,
}
if err := instance.adoptSurface(surface); err != nil {
t.Fatalf("adopt surface: %v", err)
}
if err := surface.Configure(device, &SurfaceConfiguration{
Width: 1,
Height: 1,
Format: gputypes.TextureFormatRGBA8Unorm,
Usage: gputypes.TextureUsageRenderAttachment,
PresentMode: gputypes.PresentModeFifo,
AlphaMode: gputypes.CompositeAlphaModeAuto,
}); err != nil {
t.Fatalf("Configure: %v", err)
}
if _, _, err := surface.GetCurrentTexture(); err != nil {
t.Fatalf("GetCurrentTexture: %v", err)
}
instance.Release()
want := []string{"surface-discard", "device-destroy", "surface-destroy"}
if !slices.Equal(events, want) {
t.Fatalf("release events = %v, want %v", events, want)
}
if surface.core != nil {
t.Fatal("surface retained core state after instance release")
}
}
func TestSurfaceConfigureRejectsDeviceFromAnotherInstance(t *testing.T) {
first := &Instance{core: core.NewInstanceWithMock(nil)}
second := &Instance{core: core.NewInstanceWithMock(nil)}
defer first.Release()
defer second.Release()
rawDevice := &noop.Device{}
queue := &Queue{hal: &noop.Queue{}, halDevice: rawDevice}
device := &Device{
core: core.NewDevice(rawDevice, nil, 0, gputypes.DefaultLimits(), "cross-instance-test"),
queue: queue,
}
queue.device = device
if err := first.adoptDevice(device); err != nil {
t.Fatalf("adopt device: %v", err)
}
surface := &Surface{
core: core.NewSurface(&noop.Surface{}, "cross-instance-test"),
surfaceCreated: true,
currentBackend: gputypes.BackendEmpty,
}
if err := second.adoptSurface(surface); err != nil {
t.Fatalf("adopt surface: %v", err)
}
err := surface.Configure(device, &SurfaceConfiguration{
Width: 1,
Height: 1,
Format: gputypes.TextureFormatRGBA8Unorm,
Usage: gputypes.TextureUsageRenderAttachment,
PresentMode: gputypes.PresentModeFifo,
AlphaMode: gputypes.CompositeAlphaModeAuto,
})
if err == nil {
t.Fatal("Configure accepted a device from another instance")
}
if surface.core.State() != core.SurfaceStateUnconfigured {
t.Fatalf("surface state = %v, want unconfigured", surface.core.State())
}
}
func TestReleasedInstanceRejectsLateOwnership(t *testing.T) {
instance := &Instance{core: core.NewInstanceWithMock(nil)}
instance.Release()
instance.Release()
if !instance.isReleased() {
t.Fatal("released instance did not report released state")
}
var nilInstance *Instance
if !nilInstance.isReleased() {
t.Fatal("nil instance did not report released state")
}
rawDevice := &noop.Device{}
queue := &Queue{hal: &noop.Queue{}, halDevice: rawDevice}
device := &Device{
core: core.NewDevice(rawDevice, nil, 0, gputypes.DefaultLimits(), "late-device"),
queue: queue,
}
queue.device = device
if err := instance.adoptDevice(device); !errors.Is(err, ErrReleased) {
t.Fatalf("adoptDevice after release = %v, want ErrReleased", err)
}
if err := instance.adoptDevice(nil); err == nil {
t.Fatal("adoptDevice accepted nil")
}
surface := &Surface{core: core.NewSurface(&noop.Surface{}, "late-surface")}
if err := instance.adoptSurface(surface); !errors.Is(err, ErrReleased) {
t.Fatalf("adoptSurface after release = %v, want ErrReleased", err)
}
if err := instance.adoptSurface(nil); err == nil {
t.Fatal("adoptSurface accepted nil")
}
var nilAdapter *Adapter
if _, err := nilAdapter.RequestDevice(nil); !errors.Is(err, ErrReleased) {
t.Fatalf("nil Adapter.RequestDevice = %v, want ErrReleased", err)
}
detached := &Adapter{instance: instance}
if _, err := detached.RequestDevice(nil); !errors.Is(err, ErrReleased) {
t.Fatalf("released-instance Adapter.RequestDevice = %v, want ErrReleased", err)
}
device.Release()
surface.Release()
}