Skip to content

Commit d9f1fe7

Browse files
authored
fix: Update flame_3d to support both old and newer Flutter APIs (#3663)
Several APIs started to return non-nullable types instead of null to indicate errors. However we want to support both versions. If we do not add `!`, it will fail on some versions, and if we do add `!`, the lint will complain on others: <img width="2428" height="1162" alt="image" src="https://github.com/user-attachments/assets/73ee6679-4017-4c2b-a5d3-510d63ce5f27" /> This is a compromise, using a hacky "unwrap" method (a line-level suppression would be fine; the unwrap just centralizes the hack in one function for both methods).
1 parent d591575 commit d9f1fe7

4 files changed

Lines changed: 53 additions & 4 deletions

File tree

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import 'package:flutter_gpu/gpu.dart' as gpu;
2+
3+
// TODO(luan): for now, we need to support both old (returns T?) and
4+
// newer (returns T!) versions of some Flutter GPU context methods.
5+
class GpuContextWrapper {
6+
static final Map<gpu.GpuContext, GpuContextWrapper> _instances = {};
7+
8+
final gpu.GpuContext _gpuContext;
9+
10+
factory GpuContextWrapper(gpu.GpuContext gpuContext) {
11+
return _instances.putIfAbsent(
12+
gpuContext,
13+
() => GpuContextWrapper._(gpuContext),
14+
);
15+
}
16+
17+
GpuContextWrapper._(this._gpuContext);
18+
19+
gpu.Texture createTexture(
20+
gpu.StorageMode storageMode,
21+
int width,
22+
int height, {
23+
gpu.PixelFormat format = gpu.PixelFormat.r8g8b8a8UNormInt,
24+
}) {
25+
return unwrap(
26+
_gpuContext.createTexture(
27+
storageMode,
28+
width,
29+
height,
30+
format: format,
31+
),
32+
);
33+
}
34+
35+
gpu.DeviceBuffer createDeviceBuffer(
36+
gpu.StorageMode storageMode,
37+
int sizeInBytes,
38+
) {
39+
return unwrap(_gpuContext.createDeviceBuffer(storageMode, sizeInBytes));
40+
}
41+
42+
static T unwrap<T>(T? t) {
43+
return t!;
44+
}
45+
}

packages/flame_3d/lib/src/graphics/graphics_device.dart

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import 'dart:ui';
33

44
import 'package:flame_3d/game.dart';
55
import 'package:flame_3d/resources.dart';
6+
import 'package:flame_3d/src/graphics/gpu_context_wrapper.dart';
67
import 'package:flame_3d/src/graphics/joints_info.dart';
78
import 'package:flutter_gpu/gpu.dart' as gpu;
89

@@ -166,13 +167,14 @@ class GraphicsDevice {
166167
if (_previousSize != size) {
167168
_previousSize = size;
168169

169-
final colorTexture = _gpuContext.createTexture(
170+
final gpuContext = GpuContextWrapper(_gpuContext);
171+
final colorTexture = gpuContext.createTexture(
170172
gpu.StorageMode.devicePrivate,
171173
size.width.toInt(),
172174
size.height.toInt(),
173175
);
174176

175-
final depthTexture = _gpuContext.createTexture(
177+
final depthTexture = gpuContext.createTexture(
176178
gpu.StorageMode.deviceTransient,
177179
size.width.toInt(),
178180
size.height.toInt(),

packages/flame_3d/lib/src/resources/mesh/surface.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import 'dart:typed_data';
33

44
import 'package:flame_3d/game.dart';
55
import 'package:flame_3d/resources.dart';
6+
import 'package:flame_3d/src/graphics/gpu_context_wrapper.dart';
67
import 'package:flutter_gpu/gpu.dart' as gpu;
78

89
enum PrimitiveType {
@@ -73,7 +74,7 @@ class Surface extends Resource<gpu.DeviceBuffer?> {
7374
gpu.DeviceBuffer? createResource() {
7475
final sizeInBytes = _vertices.lengthInBytes + _indices.lengthInBytes;
7576
resourceSizeInByes = sizeInBytes;
76-
return gpu.gpuContext.createDeviceBuffer(
77+
return GpuContextWrapper(gpu.gpuContext).createDeviceBuffer(
7778
gpu.StorageMode.hostVisible,
7879
sizeInBytes,
7980
)

packages/flame_3d/lib/src/resources/texture/texture.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import 'dart:typed_data';
22
import 'dart:ui';
33

44
import 'package:flame_3d/resources.dart';
5+
import 'package:flame_3d/src/graphics/gpu_context_wrapper.dart';
56
import 'package:flutter_gpu/gpu.dart' as gpu;
67

78
/// {@template texture}
@@ -23,7 +24,7 @@ class Texture extends Resource<gpu.Texture> {
2324

2425
@override
2526
gpu.Texture createResource() {
26-
return gpu.gpuContext.createTexture(
27+
return GpuContextWrapper(gpu.gpuContext).createTexture(
2728
gpu.StorageMode.hostVisible,
2829
width,
2930
height,

0 commit comments

Comments
 (0)