blob: 446b46cfe17d52acf1af1540bcadd2ed6229860f [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 <IOKit/IOReturn.h>
9#include <IOSurface/IOSurfaceRef.h>
rsesek5c7c3e92016-01-05 03:37:1110#include <mach/mach.h>
11
12#include "base/mac/scoped_cftyperef.h"
13#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"
17#include "ui/gfx/gfx_export.h"
18
19namespace gfx {
20
21namespace internal {
22
23struct 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
ccameron69a2dae2016-03-28 03:23:4529struct 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
rsesek5c7c3e92016-01-05 03:37:1142} // namespace internal
43
44using IOSurfaceId = GenericSharedMemoryId;
45
46// Helper function to create an IOSurface with a specified size and format.
Sunny Sachanandaniea6e12b2017-12-07 03:04:3647// 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 Sachanandanie9ea4a92023-05-25 19:01:2549// 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 Drissmanf091d6b2023-06-21 03:08:4055GFX_EXPORT base::ScopedCFTypeRef<IOSurfaceRef> CreateIOSurface(
56 const Size& size,
57 BufferFormat format,
58 bool should_clear = true,
59 bool override_rgba_to_bgra = true);
rsesek5c7c3e92016-01-05 03:37:1160
61// A scoper for handling Mach port names that are send rights for IOSurfaces.
62// This scoper is both copyable and assignable, which will increase the kernel
63// reference count of the right. On destruction, the reference count is
64// decremented.
65using ScopedRefCountedIOSurfaceMachPort =
66 base::ScopedTypeRef<mach_port_t, internal::IOSurfaceMachPortTraits>;
67
ccameron69a2dae2016-03-28 03:23:4568// A scoper for holding a reference to an IOSurface and also incrementing its
69// in-use counter while the scoper exists.
70using ScopedInUseIOSurface =
71 base::ScopedTypeRef<IOSurfaceRef, internal::ScopedInUseIOSurfaceTraits>;
72
Christopher Cameron48792632020-10-20 00:48:4673// A scoper for holding a reference to an IOSurface.
74using ScopedIOSurface = base::ScopedCFTypeRef<IOSurfaceRef>;
75
Christopher Cameron111548d2019-10-17 22:09:5776// Return true if there exists a value for IOSurfaceColorSpace or
77// IOSurfaceICCProfile that will make CoreAnimation render using |color_space|.
78GFX_EXPORT bool IOSurfaceCanSetColorSpace(const gfx::ColorSpace& color_space);
79
80// Set color space for given IOSurface. IOSurfaceCanSetColorSpace must return
81// true for |color_space| otherwise this does nothing.
Sunny Sachanandani88353d22017-10-26 01:52:2482GFX_EXPORT void IOSurfaceSetColorSpace(IOSurfaceRef io_surface,
83 const gfx::ColorSpace& color_space);
84
Christopher Cameron85e4544c2020-09-08 23:01:2785// Return the expected four character code pixel format for an IOSurface with
86// the specified gfx::BufferFormat.
87GFX_EXPORT uint32_t
Sunny Sachanandanie9ea4a92023-05-25 19:01:2588BufferFormatToIOSurfacePixelFormat(gfx::BufferFormat format,
89 bool override_rgba_to_bgra = true);
Christopher Cameron85e4544c2020-09-08 23:01:2790
Markus Handell1f3c85d2020-09-22 19:17:5591// Return an IOSurface consuming |io_surface_mach_port|.
92GFX_EXPORT base::ScopedCFTypeRef<IOSurfaceRef> IOSurfaceMachPortToIOSurface(
93 ScopedRefCountedIOSurfaceMachPort io_surface_mach_port);
94
rsesek5c7c3e92016-01-05 03:37:1195} // namespace gfx
96
97#endif // UI_GFX_MAC_IO_SURFACE_H_