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