blob: 585091f299e99993d0536a1eb8f9cea2219b4bc8 [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
Avi Drissmana09d7dd2023-08-17 16:26:5812#include "base/apple/scoped_cftyperef.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"
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,
Ian Vollickc53c68812023-07-20 15:45:5559#if BUILDFLAG(IS_IOS)
60 bool override_rgba_to_bgra = false
61#else
62 bool override_rgba_to_bgra = true
63#endif
64);
rsesek5c7c3e92016-01-05 03:37:1165
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.
70using ScopedRefCountedIOSurfaceMachPort =
71 base::ScopedTypeRef<mach_port_t, internal::IOSurfaceMachPortTraits>;
72
ccameron69a2dae2016-03-28 03:23:4573// A scoper for holding a reference to an IOSurface and also incrementing its
74// in-use counter while the scoper exists.
75using ScopedInUseIOSurface =
76 base::ScopedTypeRef<IOSurfaceRef, internal::ScopedInUseIOSurfaceTraits>;
77
Christopher Cameron48792632020-10-20 00:48:4678// A scoper for holding a reference to an IOSurface.
79using ScopedIOSurface = base::ScopedCFTypeRef<IOSurfaceRef>;
80
Christopher Cameron111548d2019-10-17 22:09:5781// Return true if there exists a value for IOSurfaceColorSpace or
82// IOSurfaceICCProfile that will make CoreAnimation render using |color_space|.
83GFX_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 Sachanandani88353d22017-10-26 01:52:2487GFX_EXPORT void IOSurfaceSetColorSpace(IOSurfaceRef io_surface,
88 const gfx::ColorSpace& color_space);
89
Christopher Cameron85e4544c2020-09-08 23:01:2790// Return the expected four character code pixel format for an IOSurface with
91// the specified gfx::BufferFormat.
92GFX_EXPORT uint32_t
Sunny Sachanandanie9ea4a92023-05-25 19:01:2593BufferFormatToIOSurfacePixelFormat(gfx::BufferFormat format,
94 bool override_rgba_to_bgra = true);
Christopher Cameron85e4544c2020-09-08 23:01:2795
Markus Handell1f3c85d2020-09-22 19:17:5596// Return an IOSurface consuming |io_surface_mach_port|.
97GFX_EXPORT base::ScopedCFTypeRef<IOSurfaceRef> IOSurfaceMachPortToIOSurface(
98 ScopedRefCountedIOSurfaceMachPort io_surface_mach_port);
99
rsesek5c7c3e92016-01-05 03:37:11100} // namespace gfx
101
102#endif // UI_GFX_MAC_IO_SURFACE_H_