From 20e0482db740a5995eda83135c04f87c97eac55b Mon Sep 17 00:00:00 2001 From: Chris Bracken Date: Sun, 17 Nov 2024 14:30:19 -0800 Subject: [PATCH] iOS: Clean up uses of string literal constants (flutter/engine#56658) Three changes related to string constants: 1. Uses the kIOSurfaceColorSpace constant, which is a CFStringRef pointing to the string "IOSurfaceColorSpace" 2. Uses the kCVPixelFormatType_32BGRA enum value from the CoreVideo headers (which is equal to 'BGRA') in place of hardcoding the string. From the headers: ``` kCVPixelFormatType_32BGRA = 'BGRA', /* 32 bit BGRA */ ``` 3. Declares kIOServicePlane as a `constexpr const char*` rather than `static const char*`, this ensures only a single instance is created, rather than one per translation unit into which the header is included. This string is part of IOKit, but see the comment at the top of the header as to why it's apparently needed. No test changes since there are no semantic changes. [C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style --- .../darwin/ios/framework/Source/FlutterMetalLayer.mm | 5 +++-- .../shell/platform/darwin/ios/framework/Source/IOKit.h | 2 +- .../platform/darwin/macos/framework/Source/FlutterSurface.mm | 5 +++-- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterMetalLayer.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterMetalLayer.mm index b26df26ca9..a4abff98d5 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterMetalLayer.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterMetalLayer.mm @@ -4,6 +4,7 @@ #import "flutter/shell/platform/darwin/ios/framework/Source/FlutterMetalLayer.h" +#include #include #include #include @@ -316,9 +317,9 @@ extern CFTimeInterval display_link_target; if (self.colorspace != nil) { CFStringRef name = CGColorSpaceGetName(self.colorspace); - IOSurfaceSetValue(res, CFSTR("IOSurfaceColorSpace"), name); + IOSurfaceSetValue(res, kIOSurfaceColorSpace, name); } else { - IOSurfaceSetValue(res, CFSTR("IOSurfaceColorSpace"), kCGColorSpaceSRGB); + IOSurfaceSetValue(res, kIOSurfaceColorSpace, kCGColorSpaceSRGB); } return (__bridge_transfer IOSurface*)res; } diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/IOKit.h b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/IOKit.h index 53e8fe1b35..4b1e8446af 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/IOKit.h +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/IOKit.h @@ -25,7 +25,7 @@ extern "C" { #define IOKIT #include -static const char* kIOServicePlane = "IOService"; +constexpr const char* kIOServicePlane = "IOService"; typedef io_object_t io_registry_entry_t; typedef io_object_t io_service_t; diff --git a/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterSurface.mm b/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterSurface.mm index 1097409426..83c4ea2f8e 100644 --- a/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterSurface.mm +++ b/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterSurface.mm @@ -4,6 +4,7 @@ #import "flutter/shell/platform/darwin/macos/framework/Source/FlutterSurface.h" +#import #import #import "flutter/fml/platform/darwin/cf_utils.h" @@ -74,7 +75,7 @@ } + (IOSurfaceRef)createIOSurfaceWithSize:(CGSize)size { - unsigned pixelFormat = 'BGRA'; + unsigned pixelFormat = kCVPixelFormatType_32BGRA; unsigned bytesPerElement = 4; size_t bytesPerRow = IOSurfaceAlignProperty(kIOSurfaceBytesPerRow, size.width * bytesPerElement); @@ -89,7 +90,7 @@ }; IOSurfaceRef res = IOSurfaceCreate((CFDictionaryRef)options); - IOSurfaceSetValue(res, CFSTR("IOSurfaceColorSpace"), kCGColorSpaceSRGB); + IOSurfaceSetValue(res, kIOSurfaceColorSpace, kCGColorSpaceSRGB); return res; }