gl_image_io_surface.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // Copyright 2013 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_GL_GL_IMAGE_IO_SURFACE_H_
  5. #define UI_GL_GL_IMAGE_IO_SURFACE_H_
  6. #include <CoreVideo/CVPixelBuffer.h>
  7. #include <IOSurface/IOSurface.h>
  8. #include <stdint.h>
  9. #include "base/mac/scoped_cftyperef.h"
  10. #include "base/threading/thread_checker.h"
  11. #include "ui/gfx/buffer_types.h"
  12. #include "ui/gfx/color_space.h"
  13. #include "ui/gfx/generic_shared_memory_id.h"
  14. #include "ui/gl/gl_export.h"
  15. #include "ui/gl/gl_image.h"
  16. #if defined(__OBJC__)
  17. @class CALayer;
  18. #else
  19. typedef void* CALayer;
  20. #endif
  21. namespace gl {
  22. class GL_EXPORT GLImageIOSurface : public GLImage {
  23. public:
  24. static GLImageIOSurface* Create(const gfx::Size& size,
  25. unsigned internalformat);
  26. GLImageIOSurface(const GLImageIOSurface&) = delete;
  27. GLImageIOSurface& operator=(const GLImageIOSurface&) = delete;
  28. // Initialize to wrap of |io_surface|. The format of the plane to wrap is
  29. // specified in |format|. The index of the plane to wrap is
  30. // |io_surface_plane|. If |format| is a multi-planar format (e.g,
  31. // YUV_420_BIPLANAR or P010), then this will automatically convert from YUV
  32. // to RGB, and |io_surface_plane| is ignored.
  33. bool Initialize(IOSurfaceRef io_surface,
  34. uint32_t io_surface_plane,
  35. gfx::GenericSharedMemoryId io_surface_id,
  36. gfx::BufferFormat format);
  37. // IOSurfaces coming from video decode are wrapped in a CVPixelBuffer
  38. // and may be discarded if the owning CVPixelBuffer is destroyed. This
  39. // initialization will ensure that the CVPixelBuffer be retained for the
  40. // lifetime of the GLImage.
  41. bool InitializeWithCVPixelBuffer(CVPixelBufferRef cv_pixel_buffer,
  42. uint32_t io_surface_plane,
  43. gfx::GenericSharedMemoryId io_surface_id,
  44. gfx::BufferFormat format);
  45. // Overridden from GLImage:
  46. gfx::Size GetSize() override;
  47. unsigned GetInternalFormat() override;
  48. unsigned GetDataType() override;
  49. BindOrCopy ShouldBindOrCopy() override;
  50. bool BindTexImage(unsigned target) override;
  51. bool BindTexImageWithInternalformat(unsigned target,
  52. unsigned internalformat) override;
  53. void ReleaseTexImage(unsigned target) override {}
  54. bool CopyTexImage(unsigned target) override;
  55. bool CopyTexSubImage(unsigned target,
  56. const gfx::Point& offset,
  57. const gfx::Rect& rect) override;
  58. void SetColorSpace(const gfx::ColorSpace& color_space) override;
  59. void Flush() override {}
  60. void OnMemoryDump(base::trace_event::ProcessMemoryDump* pmd,
  61. uint64_t process_tracing_id,
  62. const std::string& dump_name) override;
  63. bool EmulatingRGB() const override;
  64. bool IsInUseByWindowServer() const override;
  65. void DisableInUseByWindowServer() override;
  66. gfx::GenericSharedMemoryId io_surface_id() const { return io_surface_id_; }
  67. base::ScopedCFTypeRef<IOSurfaceRef> io_surface();
  68. base::ScopedCFTypeRef<CVPixelBufferRef> cv_pixel_buffer();
  69. // For IOSurfaces that need manual conversion to a GL texture before being
  70. // sampled from, specify the color space in which to do the required YUV to
  71. // RGB transformation.
  72. void SetColorSpaceForYUVToRGBConversion(const gfx::ColorSpace& color_space);
  73. // Sets the color space of the GLImage without modifying the underlying
  74. // IOSurface. Callers should ensure the color spaces match.
  75. void SetColorSpaceShallow(const gfx::ColorSpace& color_space);
  76. static unsigned GetInternalFormatForTesting(gfx::BufferFormat format);
  77. // Downcasts from |image|. Returns |nullptr| on failure.
  78. static GLImageIOSurface* FromGLImage(GLImage* image);
  79. protected:
  80. GLImageIOSurface(const gfx::Size& size, unsigned internalformat);
  81. ~GLImageIOSurface() override;
  82. virtual bool BindTexImageImpl(unsigned target, unsigned internalformat);
  83. static bool ValidFormat(gfx::BufferFormat format);
  84. Type GetType() const override;
  85. class RGBConverter;
  86. const gfx::Size size_;
  87. // The "internalformat" exposed to the command buffer, which may not be
  88. // "internalformat" requested by the client.
  89. const unsigned internalformat_;
  90. // The "internalformat" requested by the client.
  91. const unsigned client_internalformat_;
  92. gfx::BufferFormat format_;
  93. base::ScopedCFTypeRef<IOSurfaceRef> io_surface_;
  94. base::ScopedCFTypeRef<CVPixelBufferRef> cv_pixel_buffer_;
  95. gfx::GenericSharedMemoryId io_surface_id_;
  96. // The plane that is bound for this image. If the plane is invalid, then
  97. // this is a multi-planar IOSurface, which will be copied instead of bound.
  98. static constexpr uint32_t kInvalidIOSurfacePlane = -1;
  99. uint32_t io_surface_plane_ = kInvalidIOSurfacePlane;
  100. base::ThreadChecker thread_checker_;
  101. // The default value of Rec. 601 is based on historical shader code.
  102. gfx::ColorSpace color_space_for_yuv_to_rgb_ = gfx::ColorSpace::CreateREC601();
  103. bool disable_in_use_by_window_server_ = false;
  104. };
  105. } // namespace gl
  106. #endif // UI_GL_GL_IMAGE_IO_SURFACE_H_