blob: 631c5bee79163916737c6988a3a9810faa38ac87 [file] [log] [blame]
Avi Drissman3e1a26c2022-09-15 20:26:031// Copyright 2015 The Chromium Authors
rsesek5c7c3e92016-01-05 03:37:112// 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 Tapuskae3a53f12023-02-03 19:02:568#include <IOSurface/IOSurfaceRef.h>
rsesek5c7c3e92016-01-05 03:37:119#include <mach/mach.h>
10
Avi Drissmana09d7dd2023-08-17 16:26:5811#include "base/apple/scoped_cftyperef.h"
Kalvin Lee7d17d5572024-11-10 13:52:4512#include "base/component_export.h"
rsesek5c7c3e92016-01-05 03:37:1113#include "ui/gfx/buffer_types.h"
Sunny Sachanandani88353d22017-10-26 01:52:2414#include "ui/gfx/color_space.h"
rsesek5c7c3e92016-01-05 03:37:1115#include "ui/gfx/generic_shared_memory_id.h"
16#include "ui/gfx/geometry/size.h"
rsesek5c7c3e92016-01-05 03:37:1117
18namespace gfx {
19
20namespace internal {
21
22struct IOSurfaceMachPortTraits {
Kalvin Lee7d17d5572024-11-10 13:52:4523 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);
rsesek5c7c3e92016-01-05 03:37:1128};
29
ccameron69a2dae2016-03-28 03:23:4530struct 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
rsesek5c7c3e92016-01-05 03:37:1143} // namespace internal
44
45using IOSurfaceId = GenericSharedMemoryId;
46
47// Helper function to create an IOSurface with a specified size and format.
Sunny Sachanandaniea6e12b2017-12-07 03:04:3648// 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 Sachanandanie9ea4a92023-05-25 19:01:2550// 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 Lee7d17d5572024-11-10 13:52:4556COMPONENT_EXPORT(GFX)
57base::apple::ScopedCFTypeRef<IOSurfaceRef> CreateIOSurface(
Avi Drissmanf091d6b2023-06-21 03:08:4058 const Size& size,
59 BufferFormat format,
60 bool should_clear = true,
Ian Vollickc53c68812023-07-20 15:45:5561#if BUILDFLAG(IS_IOS)
62 bool override_rgba_to_bgra = false
63#else
64 bool override_rgba_to_bgra = true
65#endif
66);
rsesek5c7c3e92016-01-05 03:37:1167
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.
72using ScopedRefCountedIOSurfaceMachPort =
Avi Drissman28154a62023-08-22 04:06:4573 base::apple::ScopedTypeRef<mach_port_t, internal::IOSurfaceMachPortTraits>;
rsesek5c7c3e92016-01-05 03:37:1174
ccameron69a2dae2016-03-28 03:23:4575// A scoper for holding a reference to an IOSurface and also incrementing its
76// in-use counter while the scoper exists.
77using ScopedInUseIOSurface =
Avi Drissman28154a62023-08-22 04:06:4578 base::apple::ScopedTypeRef<IOSurfaceRef,
79 internal::ScopedInUseIOSurfaceTraits>;
ccameron69a2dae2016-03-28 03:23:4580
Christopher Cameron48792632020-10-20 00:48:4681// A scoper for holding a reference to an IOSurface.
Avi Drissman28154a62023-08-22 04:06:4582using ScopedIOSurface = base::apple::ScopedCFTypeRef<IOSurfaceRef>;
Christopher Cameron48792632020-10-20 00:48:4683
Christopher Cameron111548d2019-10-17 22:09:5784// Return true if there exists a value for IOSurfaceColorSpace or
85// IOSurfaceICCProfile that will make CoreAnimation render using |color_space|.
Kalvin Lee7d17d5572024-11-10 13:52:4586COMPONENT_EXPORT(GFX)
87bool IOSurfaceCanSetColorSpace(const gfx::ColorSpace& color_space);
Christopher Cameron111548d2019-10-17 22:09:5788
89// Set color space for given IOSurface. IOSurfaceCanSetColorSpace must return
90// true for |color_space| otherwise this does nothing.
Kalvin Lee7d17d5572024-11-10 13:52:4591COMPONENT_EXPORT(GFX)
92void IOSurfaceSetColorSpace(IOSurfaceRef io_surface,
93 const gfx::ColorSpace& color_space);
Sunny Sachanandani88353d22017-10-26 01:52:2494
Christopher Cameron85e4544c2020-09-08 23:01:2795// Return the expected four character code pixel format for an IOSurface with
96// the specified gfx::BufferFormat.
Kalvin Lee7d17d5572024-11-10 13:52:4597COMPONENT_EXPORT(GFX)
98uint32_t BufferFormatToIOSurfacePixelFormat(gfx::BufferFormat format,
99 bool override_rgba_to_bgra = true);
Christopher Cameron85e4544c2020-09-08 23:01:27100
Markus Handell1f3c85d2020-09-22 19:17:55101// Return an IOSurface consuming |io_surface_mach_port|.
Kalvin Lee7d17d5572024-11-10 13:52:45102COMPONENT_EXPORT(GFX)
103base::apple::ScopedCFTypeRef<IOSurfaceRef> IOSurfaceMachPortToIOSurface(
Markus Handell1f3c85d2020-09-22 19:17:55104 ScopedRefCountedIOSurfaceMachPort io_surface_mach_port);
105
rsesek5c7c3e92016-01-05 03:37:11106} // namespace gfx
107
108#endif // UI_GFX_MAC_IO_SURFACE_H_