From 658a1a256a97d30488bf835e27cf9f341f6782c4 Mon Sep 17 00:00:00 2001 From: gaaclarke <30870216+gaaclarke@users.noreply.github.com> Date: Mon, 15 May 2023 10:25:03 -0700 Subject: [PATCH] Adds support for f16 surfaces to wide gamut unit tests. (#126712) issue: https://github.com/flutter/flutter/issues/126620 ## 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]. - [x] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. [Contributor Guide]: https://github.com/flutter/flutter/wiki/Tree-hygiene#overview [Tree Hygiene]: https://github.com/flutter/flutter/wiki/Tree-hygiene [test-exempt]: https://github.com/flutter/flutter/wiki/Tree-hygiene#tests [Flutter Style Guide]: https://github.com/flutter/flutter/wiki/Style-guide-for-Flutter-repo [Features we expect every widget to implement]: https://github.com/flutter/flutter/wiki/Style-guide-for-Flutter-repo#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/wiki/Tree-hygiene#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/wiki/Chat --- .../integration_test/app_test.dart | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/dev/integration_tests/wide_gamut_test/integration_test/app_test.dart b/dev/integration_tests/wide_gamut_test/integration_test/app_test.dart index 0b74189bfe..6f402e1dbe 100644 --- a/dev/integration_tests/wide_gamut_test/integration_test/app_test.dart +++ b/dev/integration_tests/wide_gamut_test/integration_test/app_test.dart @@ -2,6 +2,7 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +import 'dart:math' as math; import 'dart:typed_data'; import 'package:flutter/services.dart'; @@ -19,12 +20,49 @@ double _decodeBGR10(int x) { return (x * slope) + intercept; } +double _decodeHalf(int x) { + if (x == 0x7c00) { + return double.infinity; + } + if (x == 0xfc00) { + return -double.infinity; + } + final double sign = x & 0x8000 == 0 ? 1.0 : -1.0; + final int exponent = (x >> 10) & 0x1f; + final int fraction = x & 0x3ff; + if (exponent == 0) { + return sign * math.pow(2.0, -14) * (fraction / 1024.0); + } else { + return sign * math.pow(2.0, exponent - 15) * (1.0 + fraction / 1024.0); + } +} + bool _isAlmost(double x, double y, double epsilon) { return (x - y).abs() < epsilon; } List _deepRed = [1.0931, -0.2268, -0.1501]; +bool _findRGBAF16Color( + Uint8List bytes, int width, int height, List color) { + final ByteData byteData = ByteData.sublistView(bytes); + expect(bytes.lengthInBytes, width * height * 8); + expect(bytes.lengthInBytes, byteData.lengthInBytes); + bool foundDeepRed = false; + for (int i = 0; i < bytes.lengthInBytes; i += 8) { + final int pixel = byteData.getUint64(i, Endian.host); + final double blue = _decodeHalf((pixel >> 32) & 0xffff); + final double green = _decodeHalf((pixel >> 16) & 0xffff); + final double red = _decodeHalf((pixel >> 0) & 0xffff); + if (_isAlmost(red, color[0], 0.01) && + _isAlmost(green, color[1], 0.01) && + _isAlmost(blue, color[2], 0.01)) { + foundDeepRed = true; + } + } + return foundDeepRed; +} + bool _findBGRA10Color( Uint8List bytes, int width, int height, List color) { final ByteData byteData = ByteData.sublistView(bytes); @@ -75,6 +113,8 @@ bool _findColor(List result, List color) { return _findBGR10Color((result[3] as Uint8List?)!, width, height, color); } else if (format == 'MTLPixelFormatBGRA10_XR') { return _findBGRA10Color((result[3] as Uint8List?)!, width, height, color); + } else if (format == 'MTLPixelFormatRGBA16Float') { + return _findRGBAF16Color((result[3] as Uint8List?)!, width, height, color); } else { fail('Unsupported pixel format: $format'); }