blob: 88c6a5dba21fe05b7cd43f0a358b1c8577ee83b5 [file] [log] [blame]
rsesek5c7c3e92016-01-05 03:37:111// Copyright 2015 The Chromium Authors. All rights reserved.
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
8#include <IOSurface/IOSurface.h>
9#include <mach/mach.h>
10
11#include "base/mac/scoped_cftyperef.h"
12#include "ui/gfx/buffer_types.h"
Sunny Sachanandani88353d22017-10-26 01:52:2413#include "ui/gfx/color_space.h"
rsesek5c7c3e92016-01-05 03:37:1114#include "ui/gfx/generic_shared_memory_id.h"
15#include "ui/gfx/geometry/size.h"
16#include "ui/gfx/gfx_export.h"
17
18namespace gfx {
19
20namespace internal {
21
22struct IOSurfaceMachPortTraits {
23 GFX_EXPORT static mach_port_t InvalidValue() { return MACH_PORT_NULL; }
24 GFX_EXPORT static mach_port_t Retain(mach_port_t);
25 GFX_EXPORT static void Release(mach_port_t);
26};
27
ccameron69a2dae2016-03-28 03:23:4528struct ScopedInUseIOSurfaceTraits {
29 static IOSurfaceRef InvalidValue() { return nullptr; }
30 static IOSurfaceRef Retain(IOSurfaceRef io_surface) {
31 CFRetain(io_surface);
32 IOSurfaceIncrementUseCount(io_surface);
33 return io_surface;
34 }
35 static void Release(IOSurfaceRef io_surface) {
36 IOSurfaceDecrementUseCount(io_surface);
37 CFRelease(io_surface);
38 }
39};
40
rsesek5c7c3e92016-01-05 03:37:1141} // namespace internal
42
43using IOSurfaceId = GenericSharedMemoryId;
44
45// Helper function to create an IOSurface with a specified size and format.
Sunny Sachanandaniea6e12b2017-12-07 03:04:3646// The surface is zero-initialized if |should_clear| is true. This is not
47// necessary for anonymous surfaces that are not exported to renderers and used
48// as render targets only.
49GFX_EXPORT IOSurfaceRef CreateIOSurface(const Size& size,
50 BufferFormat format,
51 bool should_clear = true);
rsesek5c7c3e92016-01-05 03:37:1152
53// A scoper for handling Mach port names that are send rights for IOSurfaces.
54// This scoper is both copyable and assignable, which will increase the kernel
55// reference count of the right. On destruction, the reference count is
56// decremented.
57using ScopedRefCountedIOSurfaceMachPort =
58 base::ScopedTypeRef<mach_port_t, internal::IOSurfaceMachPortTraits>;
59
ccameron69a2dae2016-03-28 03:23:4560// A scoper for holding a reference to an IOSurface and also incrementing its
61// in-use counter while the scoper exists.
62using ScopedInUseIOSurface =
63 base::ScopedTypeRef<IOSurfaceRef, internal::ScopedInUseIOSurfaceTraits>;
64
Christopher Cameron48792632020-10-20 00:48:4665// A scoper for holding a reference to an IOSurface.
66using ScopedIOSurface = base::ScopedCFTypeRef<IOSurfaceRef>;
67
Christopher Cameron111548d2019-10-17 22:09:5768// Return true if there exists a value for IOSurfaceColorSpace or
69// IOSurfaceICCProfile that will make CoreAnimation render using |color_space|.
70GFX_EXPORT bool IOSurfaceCanSetColorSpace(const gfx::ColorSpace& color_space);
71
72// Set color space for given IOSurface. IOSurfaceCanSetColorSpace must return
73// true for |color_space| otherwise this does nothing.
Sunny Sachanandani88353d22017-10-26 01:52:2474GFX_EXPORT void IOSurfaceSetColorSpace(IOSurfaceRef io_surface,
75 const gfx::ColorSpace& color_space);
76
Christopher Cameron85e4544c2020-09-08 23:01:2777// Return the expected four character code pixel format for an IOSurface with
78// the specified gfx::BufferFormat.
79GFX_EXPORT uint32_t
80BufferFormatToIOSurfacePixelFormat(gfx::BufferFormat format);
81
Markus Handell1f3c85d2020-09-22 19:17:5582// Return an IOSurface consuming |io_surface_mach_port|.
83GFX_EXPORT base::ScopedCFTypeRef<IOSurfaceRef> IOSurfaceMachPortToIOSurface(
84 ScopedRefCountedIOSurfaceMachPort io_surface_mach_port);
85
rsesek5c7c3e92016-01-05 03:37:1186} // namespace gfx
87
88#endif // UI_GFX_MAC_IO_SURFACE_H_