io_surface.h 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // 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. #ifndef UI_GFX_MAC_IO_SURFACE_H_
  5. #define UI_GFX_MAC_IO_SURFACE_H_
  6. #include <IOSurface/IOSurface.h>
  7. #include <mach/mach.h>
  8. #include "base/mac/scoped_cftyperef.h"
  9. #include "ui/gfx/buffer_types.h"
  10. #include "ui/gfx/color_space.h"
  11. #include "ui/gfx/generic_shared_memory_id.h"
  12. #include "ui/gfx/geometry/size.h"
  13. #include "ui/gfx/gfx_export.h"
  14. namespace gfx {
  15. namespace internal {
  16. struct IOSurfaceMachPortTraits {
  17. GFX_EXPORT static mach_port_t InvalidValue() { return MACH_PORT_NULL; }
  18. GFX_EXPORT static mach_port_t Retain(mach_port_t);
  19. GFX_EXPORT static void Release(mach_port_t);
  20. };
  21. struct ScopedInUseIOSurfaceTraits {
  22. static IOSurfaceRef InvalidValue() { return nullptr; }
  23. static IOSurfaceRef Retain(IOSurfaceRef io_surface) {
  24. CFRetain(io_surface);
  25. IOSurfaceIncrementUseCount(io_surface);
  26. return io_surface;
  27. }
  28. static void Release(IOSurfaceRef io_surface) {
  29. IOSurfaceDecrementUseCount(io_surface);
  30. CFRelease(io_surface);
  31. }
  32. };
  33. } // namespace internal
  34. using IOSurfaceId = GenericSharedMemoryId;
  35. // Helper function to create an IOSurface with a specified size and format.
  36. // The surface is zero-initialized if |should_clear| is true. This is not
  37. // necessary for anonymous surfaces that are not exported to renderers and used
  38. // as render targets only.
  39. GFX_EXPORT IOSurfaceRef CreateIOSurface(const Size& size,
  40. BufferFormat format,
  41. bool should_clear = true);
  42. // A scoper for handling Mach port names that are send rights for IOSurfaces.
  43. // This scoper is both copyable and assignable, which will increase the kernel
  44. // reference count of the right. On destruction, the reference count is
  45. // decremented.
  46. using ScopedRefCountedIOSurfaceMachPort =
  47. base::ScopedTypeRef<mach_port_t, internal::IOSurfaceMachPortTraits>;
  48. // A scoper for holding a reference to an IOSurface and also incrementing its
  49. // in-use counter while the scoper exists.
  50. using ScopedInUseIOSurface =
  51. base::ScopedTypeRef<IOSurfaceRef, internal::ScopedInUseIOSurfaceTraits>;
  52. // A scoper for holding a reference to an IOSurface.
  53. using ScopedIOSurface = base::ScopedCFTypeRef<IOSurfaceRef>;
  54. // Return true if there exists a value for IOSurfaceColorSpace or
  55. // IOSurfaceICCProfile that will make CoreAnimation render using |color_space|.
  56. GFX_EXPORT bool IOSurfaceCanSetColorSpace(const gfx::ColorSpace& color_space);
  57. // Set color space for given IOSurface. IOSurfaceCanSetColorSpace must return
  58. // true for |color_space| otherwise this does nothing.
  59. GFX_EXPORT void IOSurfaceSetColorSpace(IOSurfaceRef io_surface,
  60. const gfx::ColorSpace& color_space);
  61. // Return the expected four character code pixel format for an IOSurface with
  62. // the specified gfx::BufferFormat.
  63. GFX_EXPORT uint32_t
  64. BufferFormatToIOSurfacePixelFormat(gfx::BufferFormat format);
  65. // Return an IOSurface consuming |io_surface_mach_port|.
  66. GFX_EXPORT base::ScopedCFTypeRef<IOSurfaceRef> IOSurfaceMachPortToIOSurface(
  67. ScopedRefCountedIOSurfaceMachPort io_surface_mach_port);
  68. } // namespace gfx
  69. #endif // UI_GFX_MAC_IO_SURFACE_H_