Avi Drissman | 3e1a26c | 2022-09-15 20:26:03 | [diff] [blame] | 1 | // Copyright 2015 The Chromium Authors |
rsesek | 5c7c3e9 | 2016-01-05 03:37:11 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef UI_GFX_MAC_IO_SURFACE_H_ |
| 6 | #define UI_GFX_MAC_IO_SURFACE_H_ |
| 7 | |
Dave Tapuska | e3a53f1 | 2023-02-03 19:02:56 | [diff] [blame] | 8 | #include <IOSurface/IOSurfaceRef.h> |
rsesek | 5c7c3e9 | 2016-01-05 03:37:11 | [diff] [blame] | 9 | #include <mach/mach.h> |
| 10 | |
Avi Drissman | a09d7dd | 2023-08-17 16:26:58 | [diff] [blame] | 11 | #include "base/apple/scoped_cftyperef.h" |
Kalvin Lee | 7d17d557 | 2024-11-10 13:52:45 | [diff] [blame] | 12 | #include "base/component_export.h" |
rsesek | 5c7c3e9 | 2016-01-05 03:37:11 | [diff] [blame] | 13 | #include "ui/gfx/buffer_types.h" |
Sunny Sachanandani | 88353d2 | 2017-10-26 01:52:24 | [diff] [blame] | 14 | #include "ui/gfx/color_space.h" |
rsesek | 5c7c3e9 | 2016-01-05 03:37:11 | [diff] [blame] | 15 | #include "ui/gfx/generic_shared_memory_id.h" |
| 16 | #include "ui/gfx/geometry/size.h" |
rsesek | 5c7c3e9 | 2016-01-05 03:37:11 | [diff] [blame] | 17 | |
| 18 | namespace gfx { |
| 19 | |
| 20 | namespace internal { |
| 21 | |
| 22 | struct IOSurfaceMachPortTraits { |
Kalvin Lee | 7d17d557 | 2024-11-10 13:52:45 | [diff] [blame] | 23 | COMPONENT_EXPORT(GFX) static mach_port_t InvalidValue() { |
| 24 | return MACH_PORT_NULL; |
| 25 | } |
| 26 | COMPONENT_EXPORT(GFX) static mach_port_t Retain(mach_port_t); |
| 27 | COMPONENT_EXPORT(GFX) static void Release(mach_port_t); |
rsesek | 5c7c3e9 | 2016-01-05 03:37:11 | [diff] [blame] | 28 | }; |
| 29 | |
ccameron | 69a2dae | 2016-03-28 03:23:45 | [diff] [blame] | 30 | struct ScopedInUseIOSurfaceTraits { |
| 31 | static IOSurfaceRef InvalidValue() { return nullptr; } |
| 32 | static IOSurfaceRef Retain(IOSurfaceRef io_surface) { |
| 33 | CFRetain(io_surface); |
| 34 | IOSurfaceIncrementUseCount(io_surface); |
| 35 | return io_surface; |
| 36 | } |
| 37 | static void Release(IOSurfaceRef io_surface) { |
| 38 | IOSurfaceDecrementUseCount(io_surface); |
| 39 | CFRelease(io_surface); |
| 40 | } |
| 41 | }; |
| 42 | |
rsesek | 5c7c3e9 | 2016-01-05 03:37:11 | [diff] [blame] | 43 | } // namespace internal |
| 44 | |
| 45 | using IOSurfaceId = GenericSharedMemoryId; |
| 46 | |
| 47 | // Helper function to create an IOSurface with a specified size and format. |
Sunny Sachanandani | ea6e12b | 2017-12-07 03:04:36 | [diff] [blame] | 48 | // The surface is zero-initialized if |should_clear| is true. This is not |
| 49 | // necessary for anonymous surfaces that are not exported to renderers and used |
Sunny Sachanandani | e9ea4a9 | 2023-05-25 19:01:25 | [diff] [blame] | 50 | // as render targets only. If |override_rgba_to_bgra| is true (default) a BGRA |
| 51 | // IOSurface is created for RGBA/X_8888 BufferFormat. This is needed for GL |
| 52 | // usage since neither ANGLE Metal nor CGL support importing RGBA IOSurfaces, |
| 53 | // whereas for non-GL backends (Dawn and Metal) we want the formats to match. |
| 54 | // TODO(sunnyps): Revisit this when we switch to ANGLE Metal completely since |
| 55 | // wrapping RGBA_8888 can be implemented with Metal quite easily. |
Kalvin Lee | 7d17d557 | 2024-11-10 13:52:45 | [diff] [blame] | 56 | COMPONENT_EXPORT(GFX) |
| 57 | base::apple::ScopedCFTypeRef<IOSurfaceRef> CreateIOSurface( |
Avi Drissman | f091d6b | 2023-06-21 03:08:40 | [diff] [blame] | 58 | const Size& size, |
| 59 | BufferFormat format, |
| 60 | bool should_clear = true, |
Ian Vollick | c53c6881 | 2023-07-20 15:45:55 | [diff] [blame] | 61 | #if BUILDFLAG(IS_IOS) |
| 62 | bool override_rgba_to_bgra = false |
| 63 | #else |
| 64 | bool override_rgba_to_bgra = true |
| 65 | #endif |
| 66 | ); |
rsesek | 5c7c3e9 | 2016-01-05 03:37:11 | [diff] [blame] | 67 | |
| 68 | // A scoper for handling Mach port names that are send rights for IOSurfaces. |
| 69 | // This scoper is both copyable and assignable, which will increase the kernel |
| 70 | // reference count of the right. On destruction, the reference count is |
| 71 | // decremented. |
| 72 | using ScopedRefCountedIOSurfaceMachPort = |
Avi Drissman | 28154a6 | 2023-08-22 04:06:45 | [diff] [blame] | 73 | base::apple::ScopedTypeRef<mach_port_t, internal::IOSurfaceMachPortTraits>; |
rsesek | 5c7c3e9 | 2016-01-05 03:37:11 | [diff] [blame] | 74 | |
ccameron | 69a2dae | 2016-03-28 03:23:45 | [diff] [blame] | 75 | // A scoper for holding a reference to an IOSurface and also incrementing its |
| 76 | // in-use counter while the scoper exists. |
| 77 | using ScopedInUseIOSurface = |
Avi Drissman | 28154a6 | 2023-08-22 04:06:45 | [diff] [blame] | 78 | base::apple::ScopedTypeRef<IOSurfaceRef, |
| 79 | internal::ScopedInUseIOSurfaceTraits>; |
ccameron | 69a2dae | 2016-03-28 03:23:45 | [diff] [blame] | 80 | |
Christopher Cameron | 4879263 | 2020-10-20 00:48:46 | [diff] [blame] | 81 | // A scoper for holding a reference to an IOSurface. |
Avi Drissman | 28154a6 | 2023-08-22 04:06:45 | [diff] [blame] | 82 | using ScopedIOSurface = base::apple::ScopedCFTypeRef<IOSurfaceRef>; |
Christopher Cameron | 4879263 | 2020-10-20 00:48:46 | [diff] [blame] | 83 | |
Christopher Cameron | 111548d | 2019-10-17 22:09:57 | [diff] [blame] | 84 | // Return true if there exists a value for IOSurfaceColorSpace or |
| 85 | // IOSurfaceICCProfile that will make CoreAnimation render using |color_space|. |
Kalvin Lee | 7d17d557 | 2024-11-10 13:52:45 | [diff] [blame] | 86 | COMPONENT_EXPORT(GFX) |
| 87 | bool IOSurfaceCanSetColorSpace(const gfx::ColorSpace& color_space); |
Christopher Cameron | 111548d | 2019-10-17 22:09:57 | [diff] [blame] | 88 | |
| 89 | // Set color space for given IOSurface. IOSurfaceCanSetColorSpace must return |
| 90 | // true for |color_space| otherwise this does nothing. |
Kalvin Lee | 7d17d557 | 2024-11-10 13:52:45 | [diff] [blame] | 91 | COMPONENT_EXPORT(GFX) |
| 92 | void IOSurfaceSetColorSpace(IOSurfaceRef io_surface, |
| 93 | const gfx::ColorSpace& color_space); |
Sunny Sachanandani | 88353d2 | 2017-10-26 01:52:24 | [diff] [blame] | 94 | |
Christopher Cameron | 85e4544c | 2020-09-08 23:01:27 | [diff] [blame] | 95 | // Return the expected four character code pixel format for an IOSurface with |
| 96 | // the specified gfx::BufferFormat. |
Kalvin Lee | 7d17d557 | 2024-11-10 13:52:45 | [diff] [blame] | 97 | COMPONENT_EXPORT(GFX) |
| 98 | uint32_t BufferFormatToIOSurfacePixelFormat(gfx::BufferFormat format, |
| 99 | bool override_rgba_to_bgra = true); |
Christopher Cameron | 85e4544c | 2020-09-08 23:01:27 | [diff] [blame] | 100 | |
Markus Handell | 1f3c85d | 2020-09-22 19:17:55 | [diff] [blame] | 101 | // Return an IOSurface consuming |io_surface_mach_port|. |
Kalvin Lee | 7d17d557 | 2024-11-10 13:52:45 | [diff] [blame] | 102 | COMPONENT_EXPORT(GFX) |
| 103 | base::apple::ScopedCFTypeRef<IOSurfaceRef> IOSurfaceMachPortToIOSurface( |
Markus Handell | 1f3c85d | 2020-09-22 19:17:55 | [diff] [blame] | 104 | ScopedRefCountedIOSurfaceMachPort io_surface_mach_port); |
| 105 | |
rsesek | 5c7c3e9 | 2016-01-05 03:37:11 | [diff] [blame] | 106 | } // namespace gfx |
| 107 | |
| 108 | #endif // UI_GFX_MAC_IO_SURFACE_H_ |