gl_image.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. // Copyright (c) 2012 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_H_
  5. #define UI_GL_GL_IMAGE_H_
  6. #include <stdint.h>
  7. #include <string>
  8. #include "base/memory/ref_counted.h"
  9. #include "build/build_config.h"
  10. #include "ui/gfx/buffer_types.h"
  11. #include "ui/gfx/color_space.h"
  12. #include "ui/gfx/geometry/point.h"
  13. #include "ui/gfx/geometry/rect.h"
  14. #include "ui/gfx/geometry/rect_f.h"
  15. #include "ui/gfx/geometry/size.h"
  16. #include "ui/gfx/gpu_fence.h"
  17. #include "ui/gfx/native_pixmap.h"
  18. #include "ui/gfx/native_widget_types.h"
  19. #include "ui/gfx/overlay_transform.h"
  20. #include "ui/gl/gl_export.h"
  21. #if BUILDFLAG(IS_ANDROID)
  22. #include <android/hardware_buffer.h>
  23. #include <memory>
  24. #include "base/android/scoped_hardware_buffer_handle.h"
  25. #include "base/files/scoped_file.h"
  26. #endif
  27. namespace base {
  28. namespace trace_event {
  29. class ProcessMemoryDump;
  30. } // namespace trace_event
  31. namespace android {
  32. class ScopedHardwareBufferFenceSync;
  33. } // namespace android
  34. } // namespace base
  35. namespace gl {
  36. // Encapsulates an image that can be bound and/or copied to a texture, hiding
  37. // platform specific management.
  38. class GL_EXPORT GLImage : public base::RefCounted<GLImage> {
  39. public:
  40. GLImage() = default;
  41. GLImage(const GLImage&) = delete;
  42. GLImage& operator=(const GLImage&) = delete;
  43. // Get the size of the image.
  44. virtual gfx::Size GetSize();
  45. // Get the GL internal format, format, type of the image.
  46. // They are aligned with glTexImage{2|3}D's parameters |internalformat|,
  47. // |format|, and |type|.
  48. // The returned enums are based on ES2 contexts and are mostly ES3
  49. // compatible, except for GL_HALF_FLOAT_OES.
  50. virtual unsigned GetInternalFormat();
  51. virtual unsigned GetDataFormat();
  52. virtual unsigned GetDataType();
  53. enum BindOrCopy { BIND, COPY };
  54. // Returns whether this image is meant to be bound or copied to textures. The
  55. // suggested method is not guaranteed to succeed, but the alternative will
  56. // definitely fail.
  57. virtual BindOrCopy ShouldBindOrCopy();
  58. // Bind image to texture currently bound to |target|. Returns true on success.
  59. // It is valid for an implementation to always return false.
  60. virtual bool BindTexImage(unsigned target);
  61. // Bind image to texture currently bound to |target|, forcing the texture's
  62. // internal format to the specified one. This is a feature not available on
  63. // all platforms. Returns true on success. It is valid for an implementation
  64. // to always return false.
  65. virtual bool BindTexImageWithInternalformat(unsigned target,
  66. unsigned internalformat);
  67. // Release image from texture currently bound to |target|.
  68. virtual void ReleaseTexImage(unsigned target);
  69. // Define texture currently bound to |target| by copying image into it.
  70. // Returns true on success. It is valid for an implementation to always
  71. // return false.
  72. virtual bool CopyTexImage(unsigned target);
  73. // Copy |rect| of image to |offset| in texture currently bound to |target|.
  74. // Returns true on success. It is valid for an implementation to always
  75. // return false.
  76. virtual bool CopyTexSubImage(unsigned target,
  77. const gfx::Point& offset,
  78. const gfx::Rect& rect);
  79. // Set the color space when image is used as an overlay. The color space may
  80. // also be useful for images backed by YUV buffers: if the GL driver can
  81. // sample the YUV buffer as RGB, we need to tell it the encoding (BT.601,
  82. // BT.709, or BT.2020) and range (limited or null), and |color_space| conveys
  83. // this.
  84. virtual void SetColorSpace(const gfx::ColorSpace& color_space);
  85. const gfx::ColorSpace& color_space() const { return color_space_; }
  86. // Flush any preceding rendering for the image.
  87. virtual void Flush();
  88. // Dumps information about the memory backing the GLImage to a dump named
  89. // |dump_name|.
  90. virtual void OnMemoryDump(base::trace_event::ProcessMemoryDump* pmd,
  91. uint64_t process_tracing_id,
  92. const std::string& dump_name);
  93. // If this returns true, then the command buffer client has requested a
  94. // CHROMIUM image with internalformat GL_RGB, but the platform only supports
  95. // GL_RGBA. The client is responsible for implementing appropriate
  96. // workarounds. The only support that the command buffer provides is format
  97. // validation during calls to copyTexImage2D and copySubTexImage2D.
  98. //
  99. // This is a workaround that is not intended to become a permanent part of the
  100. // GLImage API. Theoretically, when Apple fixes their drivers, this can be
  101. // removed. https://crbug.com/581777#c36
  102. virtual bool EmulatingRGB() const;
  103. // Return true if the macOS WindowServer is currently using the underlying
  104. // storage for the image.
  105. virtual bool IsInUseByWindowServer() const;
  106. // If called, then IsInUseByWindowServer will always return false.
  107. virtual void DisableInUseByWindowServer();
  108. #if BUILDFLAG(IS_ANDROID)
  109. // Provides the buffer backing this image, if it is backed by an
  110. // AHardwareBuffer. The ScopedHardwareBuffer returned may include a fence
  111. // which will be signaled when all pending work for the buffer has been
  112. // finished and it can be safely read from.
  113. // The buffer is guaranteed to be valid until the lifetime of the object
  114. // returned.
  115. virtual std::unique_ptr<base::android::ScopedHardwareBufferFenceSync>
  116. GetAHardwareBuffer();
  117. #endif
  118. // An identifier for subclasses. Necessary for safe downcasting.
  119. enum class Type {
  120. NONE,
  121. MEMORY,
  122. IOSURFACE,
  123. DXGI_IMAGE,
  124. D3D,
  125. DCOMP_SURFACE,
  126. };
  127. virtual Type GetType() const;
  128. // Workaround for StreamTexture which must be re-copied on each access.
  129. // TODO(ericrk): Remove this once SharedImage transition is complete.
  130. virtual bool HasMutableState() const;
  131. // Returns the NativePixmap backing the GLImage. If not backed by a
  132. // NativePixmap, returns null.
  133. virtual scoped_refptr<gfx::NativePixmap> GetNativePixmap();
  134. virtual void* GetEGLImage() const;
  135. protected:
  136. virtual ~GLImage() = default;
  137. gfx::ColorSpace color_space_;
  138. private:
  139. friend class base::RefCounted<GLImage>;
  140. };
  141. } // namespace gl
  142. #endif // UI_GL_GL_IMAGE_H_