Skip to content

Commit f916dd6

Browse files
authored
Implement macOS wide gamut (Display P3) support (#181769)
Adds wide gamut color support to macOS (matching iOS), upgrades the surface pixel format from 10-bit BGRA10_XR to 16-bit float RGBA16Float on both iOS and macOS when enabled, and fixes Impeller's blur filter P3 clamping on macOS. **macOS Wide Gamut Support** - Added DoesHardwareSupportWideGamut() hardware capability check (MTLGPUFamilyApple2 or MTLGPUFamilyMac2) - Wide gamut enabled when both hardware supports it and FLTEnableWideGamut plist flag is YES - Dynamic wide gamut switching when windows move between P3 and sRGB displays - Added flutter/screenshot method channel on macOS for integration testing **RGBA16Float Surface Format (iOS + macOS)** - macOS IOSurface: kCVPixelFormatType_64RGBAHalf + MTLPixelFormatRGBA16Float - iOS CAMetalLayer: MTLPixelFormatRGBA16Float - Image decoder: always uses kRGBA_F16_SkColorType for all wide gamut images (previously only transparent images used 16-bit) **Fix Blur P3 Clamping on macOS** macOS uses the compositor/embedder path, not GPUSurfaceMetalImpeller, so UpdateOffscreenLayerPixelFormat was never called. Added the call in embedder.cc after wrapping the Metal resolve texture. **Why RGBA16Float over BGRA10_XR?** BGRA10_XR has only 10 bits per channel — values outside sRGB gamut get clamped in intermediate render targets (e.g. blur filters). RGBA16Float has 16 bits per channel with full floating-point range, preventing P3 color clamping in multi-pass rendering. **Tests** - 9 new iOS FlutterView unit tests verifying RGBA16Float pixel format and extended sRGB color space - Updated macOS FlutterSurfaceManagerTest for RGBA16Float, dynamic switching, color space, and pixel format verification - Updated image decoder and Impeller display list tests for kR16G16B16A16Float - 11 macOS/iOS integration tests: image, saveLayer, codecImage, none, blur, drawnImage, container, linearGradient, radialGradient, conicalGradient, sweepGradient ### Issues #164557 ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant documentation (doc comments with `///`). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [ ] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [ ] All existing and new tests are passing.
1 parent 70d9d31 commit f916dd6

31 files changed

Lines changed: 770 additions & 187 deletions

‎.ci.yaml‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5355,6 +5355,20 @@ targets:
53555355
["devicelab", "ios", "mac"]
53565356
task_name: wide_gamut_ios
53575357

5358+
- name: Mac wide_gamut_macos
5359+
recipe: devicelab/devicelab_drone
5360+
bringup: true
5361+
presubmit: false
5362+
timeout: 60
5363+
properties:
5364+
dependencies: >-
5365+
[
5366+
{"dependency": "ruby", "version": "ruby_3.1-pod_1.13"}
5367+
]
5368+
tags: >
5369+
["devicelab", "hostonly", "mac"]
5370+
task_name: wide_gamut_macos
5371+
53585372
- name: Mac_x64_ios hot_mode_dev_cycle_ios__benchmark
53595373
recipe: devicelab/devicelab_drone
53605374
presubmit: false

‎TESTOWNERS‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,8 @@
115115
/dev/devicelab/bin/tasks/very_long_picture_scrolling_perf__e2e_summary.dart @flar @flutter/engine
116116
/dev/devicelab/bin/tasks/web_size__compile_test.dart @yjbanov @flutter/web
117117
/dev/devicelab/bin/tasks/wide_gamut_ios.dart @gaaclarke @flutter/engine
118+
/dev/devicelab/bin/tasks/wide_gamut_macos.dart @gaaclarke @flutter/engine
119+
118120

119121
## Windows Android DeviceLab tests
120122
/dev/devicelab/bin/tasks/basic_material_app_win__compile.dart @bkonyi @flutter/tool
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Copyright 2014 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'package:flutter_devicelab/framework/devices.dart';
6+
import 'package:flutter_devicelab/framework/framework.dart';
7+
import 'package:flutter_devicelab/tasks/integration_tests.dart';
8+
9+
Future<void> main() async {
10+
deviceOperatingSystem = DeviceOperatingSystem.macos;
11+
await task(createWideGamutTest());
12+
}

‎dev/devicelab/lib/tasks/integration_tests.dart‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ TaskFunction createWideGamutTest() {
243243
return IntegrationTest(
244244
'${flutterDirectory.path}/dev/integration_tests/wide_gamut_test',
245245
'integration_test/app_test.dart',
246-
createPlatforms: <String>['ios'],
246+
createPlatforms: <String>['ios', 'macos'],
247247
).call;
248248
}
249249

‎dev/integration_tests/wide_gamut_test/.metadata‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
# This file tracks properties of this Flutter project.
22
# Used by Flutter tool to assess capabilities and perform upgrades etc.
33
#
4-
# This file should be version controlled.
4+
# This file should be version controlled and should not be manually edited.
55

66
version:
7-
revision: c379133df6d1f8893ad8c9d0393496c7d6eaf828
8-
channel: wide-gamut-integration-test
7+
revision: "d7bb061cf8bc3176897c7a38bbefa4836d2f2549"
8+
channel: "[user-branch]"
99

1010
project_type: app
1111

1212
# Tracks metadata for the flutter migrate command
1313
migration:
1414
platforms:
1515
- platform: root
16-
create_revision: c379133df6d1f8893ad8c9d0393496c7d6eaf828
17-
base_revision: c379133df6d1f8893ad8c9d0393496c7d6eaf828
16+
create_revision: d7bb061cf8bc3176897c7a38bbefa4836d2f2549
17+
base_revision: d7bb061cf8bc3176897c7a38bbefa4836d2f2549
1818
- platform: ios
19-
create_revision: c379133df6d1f8893ad8c9d0393496c7d6eaf828
20-
base_revision: c379133df6d1f8893ad8c9d0393496c7d6eaf828
19+
create_revision: d7bb061cf8bc3176897c7a38bbefa4836d2f2549
20+
base_revision: d7bb061cf8bc3176897c7a38bbefa4836d2f2549
2121

2222
# User provided section
2323

‎dev/integration_tests/wide_gamut_test/integration_test/app_test.dart‎

Lines changed: 69 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,19 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5-
import 'dart:math' as math;
5+
import 'dart:convert' show base64Decode;
66
import 'dart:typed_data';
77

88
import 'package:flutter/services.dart';
9+
import 'package:flutter/widgets.dart';
910
import 'package:flutter_test/flutter_test.dart';
1011
import 'package:integration_test/integration_test.dart';
1112

1213
import 'package:wide_gamut_test/main.dart' as app;
1314

15+
/// BGRA10_XR has ~0.002 step size, so 0.01 is needed for 10-bit formats.
16+
const double _bgra10Epsilon = 0.01;
17+
1418
// See: https://developer.apple.com/documentation/metal/mtlpixelformat/mtlpixelformatbgr10_xr.
1519
double _decodeBGR10(int x) {
1620
const max = 1.25098;
@@ -20,23 +24,6 @@ double _decodeBGR10(int x) {
2024
return (x * slope) + intercept;
2125
}
2226

23-
double _decodeHalf(int x) {
24-
if (x == 0x7c00) {
25-
return double.infinity;
26-
}
27-
if (x == 0xfc00) {
28-
return -double.infinity;
29-
}
30-
final sign = x & 0x8000 == 0 ? 1.0 : -1.0;
31-
final int exponent = (x >> 10) & 0x1f;
32-
final int fraction = x & 0x3ff;
33-
if (exponent == 0) {
34-
return sign * math.pow(2.0, -14) * (fraction / 1024.0);
35-
} else {
36-
return sign * math.pow(2.0, exponent - 15) * (1.0 + fraction / 1024.0);
37-
}
38-
}
39-
4027
bool _isAlmost(double x, double y, double epsilon) {
4128
return (x - y).abs() < epsilon;
4229
}
@@ -49,38 +36,6 @@ double _distanceSquared(double r, double g, double b, List<double> color) {
4936

5037
List<double> _deepRed = <double>[1.0931, -0.2268, -0.1501];
5138

52-
(bool, List<double>) _findRGBAF16Color(
53-
Uint8List bytes,
54-
int width,
55-
int height,
56-
List<double> color, {
57-
required double epsilon,
58-
}) {
59-
final byteData = ByteData.sublistView(bytes);
60-
expect(bytes.lengthInBytes, width * height * 8);
61-
expect(bytes.lengthInBytes, byteData.lengthInBytes);
62-
var foundColor = false;
63-
double minDistance = double.infinity;
64-
var closestColor = <double>[0, 0, 0];
65-
for (var i = 0; i < bytes.lengthInBytes; i += 8) {
66-
final int pixel = byteData.getUint64(i, Endian.host);
67-
final double blue = _decodeHalf((pixel >> 32) & 0xffff);
68-
final double green = _decodeHalf((pixel >> 16) & 0xffff);
69-
final double red = _decodeHalf((pixel >> 0) & 0xffff);
70-
if (_isAlmost(red, color[0], epsilon) &&
71-
_isAlmost(green, color[1], epsilon) &&
72-
_isAlmost(blue, color[2], epsilon)) {
73-
foundColor = true;
74-
}
75-
final double currentDistance = _distanceSquared(red, green, blue, color);
76-
if (currentDistance < minDistance) {
77-
minDistance = currentDistance;
78-
closestColor = <double>[red, green, blue];
79-
}
80-
}
81-
return (foundColor, closestColor);
82-
}
83-
8439
(bool, List<double>) _findBGRA10Color(
8540
Uint8List bytes,
8641
int width,
@@ -113,55 +68,21 @@ List<double> _deepRed = <double>[1.0931, -0.2268, -0.1501];
11368
return (foundColor, closestColor);
11469
}
11570

116-
(bool, List<double>) _findBGR10Color(
117-
Uint8List bytes,
118-
int width,
119-
int height,
120-
List<double> color, {
121-
required double epsilon,
122-
}) {
123-
final byteData = ByteData.sublistView(bytes);
124-
expect(bytes.lengthInBytes, width * height * 4);
125-
expect(bytes.lengthInBytes, byteData.lengthInBytes);
126-
var foundColor = false;
127-
double minDistance = double.infinity;
128-
var closestColor = <double>[0, 0, 0];
129-
for (var i = 0; i < bytes.lengthInBytes; i += 4) {
130-
final int pixel = byteData.getUint32(i, Endian.host);
131-
final double blue = _decodeBGR10(pixel & 0x3ff);
132-
final double green = _decodeBGR10((pixel >> 10) & 0x3ff);
133-
final double red = _decodeBGR10((pixel >> 20) & 0x3ff);
134-
if (_isAlmost(red, color[0], epsilon) &&
135-
_isAlmost(green, color[1], epsilon) &&
136-
_isAlmost(blue, color[2], epsilon)) {
137-
foundColor = true;
138-
}
139-
final double currentDistance = _distanceSquared(red, green, blue, color);
140-
if (currentDistance < minDistance) {
141-
minDistance = currentDistance;
142-
closestColor = <double>[red, green, blue];
143-
}
144-
}
145-
return (foundColor, closestColor);
146-
}
147-
148-
(bool, List<double>) _findColor(List<dynamic> result, List<double> color, {double epsilon = 0.01}) {
71+
(bool, List<double>) _findColor(List<dynamic> result, List<double> color, {double? epsilon}) {
14972
expect(result, isNotNull);
15073
expect(result.length, 4);
15174
final [int width, int height, String format, Uint8List bytes] = result;
152-
return switch (format) {
153-
'MTLPixelFormatBGR10_XR' => _findBGR10Color(bytes, width, height, color, epsilon: epsilon),
154-
'MTLPixelFormatBGRA10_XR' => _findBGRA10Color(bytes, width, height, color, epsilon: epsilon),
155-
'MTLPixelFormatRGBA16Float' => _findRGBAF16Color(bytes, width, height, color, epsilon: epsilon),
156-
_ => fail('Unsupported pixel format: $format'),
157-
};
75+
if (format != 'MTLPixelFormatBGRA10_XR') {
76+
throw UnsupportedError('Unsupported pixel format: $format');
77+
}
78+
return _findBGRA10Color(bytes, width, height, color, epsilon: epsilon ?? _bgra10Epsilon);
15879
}
15980

16081
class _HasColor extends Matcher {
161-
const _HasColor(this.color, {this.epsilon = 0.01});
82+
const _HasColor(this.color, {this.epsilon});
16283

16384
final List<double> color;
164-
final double epsilon;
85+
final double? epsilon;
16586

16687
@override
16788
bool matches(dynamic item, Map<dynamic, dynamic> matchState) {
@@ -186,100 +107,123 @@ class _HasColor extends Matcher {
186107
Map<dynamic, dynamic> matchState,
187108
bool verbose,
188109
) {
189-
final closest = matchState['closest'] as List<double>;
110+
final closest = matchState['closest'] as List<double>?;
111+
if (closest == null) {
112+
return mismatchDescription.add('could not find any colors (unsupported pixel format?)');
113+
}
190114
return mismatchDescription.add('closest color to $color was $closest');
191115
}
192116
}
193117

118+
/// Precache the Display P3 test image so it is fully decoded before we take a
119+
/// screenshot. Must be called after [pumpAndSettle] so a [BuildContext] exists.
120+
Future<void> _precacheP3Image(WidgetTester tester) async {
121+
final BuildContext context = tester.element(find.byType(app.MyApp));
122+
await tester.runAsync(() => precacheImage(MemoryImage(base64Decode(app.displayP3Logo)), context));
123+
}
124+
194125
void main() {
195126
IntegrationTestWidgetsFlutterBinding.ensureInitialized();
196127

197128
group('end-to-end test', () {
129+
const screenshotChannel = MethodChannel('flutter/screenshot');
198130
testWidgets('look for display p3 deepest red', (WidgetTester tester) async {
199131
app.run(app.Setup.image);
200-
await tester.pumpAndSettle(const Duration(seconds: 2));
132+
await tester.pumpAndSettle();
133+
await _precacheP3Image(tester);
134+
await tester.pumpAndSettle();
201135

202-
const channel = MethodChannel('flutter/screenshot');
203-
final result = await channel.invokeMethod('test') as List<Object?>;
136+
final result = await screenshotChannel.invokeMethod('test') as List<Object?>;
204137
expect(result, _HasColor(_deepRed));
205138
});
206-
testWidgets('look for display p3 deepest red', (WidgetTester tester) async {
139+
testWidgets('look for display p3 deepest red (saveLayer)', (WidgetTester tester) async {
207140
app.run(app.Setup.canvasSaveLayer);
208-
await tester.pumpAndSettle(const Duration(seconds: 2));
141+
await tester.pumpAndSettle();
142+
await _precacheP3Image(tester);
143+
await tester.pumpAndSettle();
209144

210-
const channel = MethodChannel('flutter/screenshot');
211-
final result = await channel.invokeMethod('test') as List<Object?>;
145+
final result = await screenshotChannel.invokeMethod('test') as List<Object?>;
146+
expect(result, _HasColor(_deepRed));
147+
});
148+
testWidgets('p3 deepest red via codec API (ImageDescriptor)', (WidgetTester tester) async {
149+
app.run(app.Setup.codecImage);
150+
await tester.pumpAndSettle();
151+
// Wait for async _loadCodecImage() to complete and rebuild
152+
await tester.runAsync(() => Future<void>.delayed(const Duration(milliseconds: 500)));
153+
await tester.pumpAndSettle();
154+
155+
final result = await screenshotChannel.invokeMethod('test') as List<Object?>;
212156
expect(result, _HasColor(_deepRed));
213157
});
214158
testWidgets('no p3 deepest red without image', (WidgetTester tester) async {
215159
app.run(app.Setup.none);
216-
await tester.pumpAndSettle(const Duration(seconds: 2));
160+
await tester.pumpAndSettle();
217161

218-
const channel = MethodChannel('flutter/screenshot');
219-
final result = await channel.invokeMethod('test') as List<Object?>;
162+
final result = await screenshotChannel.invokeMethod('test') as List<Object?>;
220163
expect(result, isNot(_HasColor(_deepRed)));
221164
expect(result, isNot(const _HasColor(<double>[0.0, 1.0, 0.0])));
222165
});
223166
testWidgets('p3 deepest red with blur', (WidgetTester tester) async {
224167
app.run(app.Setup.blur);
225-
await tester.pumpAndSettle(const Duration(seconds: 2));
168+
await tester.pumpAndSettle();
169+
await _precacheP3Image(tester);
170+
await tester.pumpAndSettle();
226171

227-
const channel = MethodChannel('flutter/screenshot');
228-
final result = await channel.invokeMethod('test') as List<Object?>;
172+
final result = await screenshotChannel.invokeMethod('test') as List<Object?>;
229173
expect(result, _HasColor(_deepRed));
230174
expect(result, const _HasColor(<double>[0.0, 1.0, 0.0]));
231175
});
232176
testWidgets('draw image with wide gamut works', (WidgetTester tester) async {
233177
app.run(app.Setup.drawnImage);
234-
await tester.pumpAndSettle(const Duration(seconds: 2));
178+
await tester.pumpAndSettle();
179+
// Wait for async _drawImage() to complete and rebuild
180+
await tester.runAsync(() => Future<void>.delayed(const Duration(milliseconds: 500)));
181+
await tester.pumpAndSettle();
235182

236-
const channel = MethodChannel('flutter/screenshot');
237-
final result = await channel.invokeMethod('test') as List<Object?>;
183+
final result = await screenshotChannel.invokeMethod('test') as List<Object?>;
238184
expect(result, const _HasColor(<double>[0.0, 1.0, 0.0]));
239185
});
240186
testWidgets('draw container with wide gamut works', (WidgetTester tester) async {
241187
app.run(app.Setup.container);
242-
await tester.pumpAndSettle(const Duration(seconds: 2));
188+
await tester.pumpAndSettle();
243189

244-
const channel = MethodChannel('flutter/screenshot');
245-
final result = await channel.invokeMethod('test') as List<Object?>;
190+
final result = await screenshotChannel.invokeMethod('test') as List<Object?>;
246191
expect(result, _HasColor(_deepRed));
247192
});
248193

249194
testWidgets('draw wide gamut linear gradient works', (WidgetTester tester) async {
250195
app.run(app.Setup.linearGradient);
251-
await tester.pumpAndSettle(const Duration(seconds: 2));
196+
await tester.pumpAndSettle();
252197

253-
const channel = MethodChannel('flutter/screenshot');
254-
final result = await channel.invokeMethod('test') as List<Object?>;
255-
expect(result, _HasColor(_deepRed));
198+
final result = await screenshotChannel.invokeMethod('test') as List<Object?>;
199+
200+
// Gradients need larger epsilon due to pixel sampling between gradient stops.
201+
expect(result, _HasColor(_deepRed, epsilon: 0.02));
256202
});
257203

258204
testWidgets('draw wide gamut radial gradient works', (WidgetTester tester) async {
259205
app.run(app.Setup.radialGradient);
260-
await tester.pumpAndSettle(const Duration(seconds: 2));
206+
await tester.pumpAndSettle();
261207

262-
const channel = MethodChannel('flutter/screenshot');
263-
final result = await channel.invokeMethod('test') as List<Object?>;
208+
final result = await screenshotChannel.invokeMethod('test') as List<Object?>;
264209
expect(result, _HasColor(_deepRed, epsilon: 0.05));
265210
});
266211

267212
testWidgets('draw wide gamut conical gradient works', (WidgetTester tester) async {
268213
app.run(app.Setup.conicalGradient);
269-
await tester.pumpAndSettle(const Duration(seconds: 2));
214+
await tester.pumpAndSettle();
270215

271-
const channel = MethodChannel('flutter/screenshot');
272-
final result = await channel.invokeMethod('test') as List<Object?>;
216+
final result = await screenshotChannel.invokeMethod('test') as List<Object?>;
273217
expect(result, _HasColor(_deepRed, epsilon: 0.05));
274218
});
275219

276220
testWidgets('draw wide gamut sweep gradient works', (WidgetTester tester) async {
277221
app.run(app.Setup.sweepGradient);
278-
await tester.pumpAndSettle(const Duration(seconds: 2));
222+
await tester.pumpAndSettle();
279223

280-
const channel = MethodChannel('flutter/screenshot');
281-
final result = await channel.invokeMethod('test') as List<Object?>;
282-
expect(result, _HasColor(_deepRed));
224+
final result = await screenshotChannel.invokeMethod('test') as List<Object?>;
225+
// Sweep gradient endpoint may not be sampled exactly at a pixel center.
226+
expect(result, _HasColor(_deepRed, epsilon: 0.02));
283227
});
284228
});
285229
}

0 commit comments

Comments
 (0)