#ifndef UI_GFX_MAC_IO_SURFACE_H_
#define UI_GFX_MAC_IO_SURFACE_H_
#include <IOSurface/IOSurfaceRef.h>
#include <mach/mach.h>
#include "base/apple/scoped_cftyperef.h"
#include "base/component_export.h"
#include "base/memory/unsafe_shared_memory_region.h"
#include "ui/gfx/buffer_types.h"
#include "ui/gfx/color_space.h"
#include "ui/gfx/generic_shared_memory_id.h"
#include "ui/gfx/geometry/size.h"
namespace viz {
class SharedImageFormat;
}
namespace gfx {
namespace internal {
struct IOSurfaceMachPortTraits {
COMPONENT_EXPORT(GFX) static mach_port_t InvalidValue() {
return MACH_PORT_NULL;
}
COMPONENT_EXPORT(GFX) static mach_port_t Retain(mach_port_t);
COMPONENT_EXPORT(GFX) static void Release(mach_port_t);
};
struct ScopedInUseIOSurfaceTraits {
static IOSurfaceRef InvalidValue() { return nullptr; }
static IOSurfaceRef Retain(IOSurfaceRef io_surface) {
CFRetain(io_surface);
IOSurfaceIncrementUseCount(io_surface);
return io_surface;
}
static void Release(IOSurfaceRef io_surface) {
IOSurfaceDecrementUseCount(io_surface);
CFRelease(io_surface);
}
};
}
using IOSurfaceId = GenericSharedMemoryId;
using ScopedRefCountedIOSurfaceMachPort =
base::apple::ScopedTypeRef<mach_port_t, internal::IOSurfaceMachPortTraits>;
using ScopedInUseIOSurface =
base::apple::ScopedTypeRef<IOSurfaceRef,
internal::ScopedInUseIOSurfaceTraits>;
using ScopedIOSurface = base::apple::ScopedCFTypeRef<IOSurfaceRef>;
static constexpr size_t kMaxIOSurfacePlanes = 3;
COMPONENT_EXPORT(GFX)
ScopedIOSurface CreateIOSurface(const Size& size,
viz::SharedImageFormat format,
bool should_clear = true,
#if BUILDFLAG(IS_IOS)
bool override_rgba_to_bgra = false
#else
bool override_rgba_to_bgra = true
#endif
);
#if BUILDFLAG(IS_IOS)
COMPONENT_EXPORT(GFX)
void ExportIOSurfaceSharedMemoryRegion(
IOSurfaceRef io_surface,
base::UnsafeSharedMemoryRegion& shared_memory_region,
std::array<uint32_t, kMaxIOSurfacePlanes>& plane_strides,
std::array<uint32_t, kMaxIOSurfacePlanes>& plane_offsets);
#endif
COMPONENT_EXPORT(GFX)
bool IOSurfaceCanSetColorSpace(const gfx::ColorSpace& color_space);
COMPONENT_EXPORT(GFX)
void IOSurfaceSetColorSpace(IOSurfaceRef io_surface,
const gfx::ColorSpace& color_space);
COMPONENT_EXPORT(GFX)
ScopedIOSurface IOSurfaceMachPortToIOSurface(
ScopedRefCountedIOSurfaceMachPort io_surface_mach_port);
}
#endif