test_gles2_interface.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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 COMPONENTS_VIZ_TEST_TEST_GLES2_INTERFACE_H_
  5. #define COMPONENTS_VIZ_TEST_TEST_GLES2_INTERFACE_H_
  6. #include <stddef.h>
  7. #include <limits>
  8. #include <memory>
  9. #include <unordered_map>
  10. #include <unordered_set>
  11. #include <utility>
  12. #include <vector>
  13. #include "base/callback.h"
  14. #include "base/compiler_specific.h"
  15. #include "base/containers/contains.h"
  16. #include "base/containers/flat_map.h"
  17. #include "base/memory/raw_ptr.h"
  18. #include "base/memory/ref_counted.h"
  19. #include "base/memory/weak_ptr.h"
  20. #include "gpu/GLES2/gl2extchromium.h"
  21. #include "gpu/command_buffer/client/gles2_interface_stub.h"
  22. #include "gpu/command_buffer/common/capabilities.h"
  23. #include "gpu/command_buffer/common/sync_token.h"
  24. #include "ui/gfx/geometry/rect.h"
  25. namespace viz {
  26. class TestContextSupport;
  27. class TestGLES2Interface : public gpu::gles2::GLES2InterfaceStub {
  28. public:
  29. TestGLES2Interface();
  30. ~TestGLES2Interface() override;
  31. // Overridden from gpu::gles2::GLES2Interface
  32. void GenTextures(GLsizei n, GLuint* textures) override;
  33. void GenBuffers(GLsizei n, GLuint* buffers) override;
  34. void GenFramebuffers(GLsizei n, GLuint* framebuffers) override;
  35. void GenRenderbuffers(GLsizei n, GLuint* renderbuffers) override;
  36. void GenQueriesEXT(GLsizei n, GLuint* queries) override;
  37. void DeleteTextures(GLsizei n, const GLuint* textures) override;
  38. void DeleteBuffers(GLsizei n, const GLuint* buffers) override;
  39. void DeleteFramebuffers(GLsizei n, const GLuint* framebuffers) override;
  40. void DeleteQueriesEXT(GLsizei n, const GLuint* queries) override;
  41. GLuint CreateShader(GLenum type) override;
  42. GLuint CreateProgram() override;
  43. void BindTexture(GLenum target, GLuint texture) override;
  44. void GetIntegerv(GLenum pname, GLint* params) override;
  45. void GetShaderiv(GLuint shader, GLenum pname, GLint* params) override;
  46. void GetProgramiv(GLuint program, GLenum pname, GLint* params) override;
  47. void GetShaderPrecisionFormat(GLenum shadertype,
  48. GLenum precisiontype,
  49. GLint* range,
  50. GLint* precision) override;
  51. GLenum CheckFramebufferStatus(GLenum target) override;
  52. void UseProgram(GLuint program) override;
  53. void Flush() override;
  54. void Finish() override;
  55. void ShallowFinishCHROMIUM() override;
  56. void BindBuffer(GLenum target, GLuint buffer) override;
  57. void BindRenderbuffer(GLenum target, GLuint buffer) override;
  58. void BindFramebuffer(GLenum target, GLuint buffer) override;
  59. void PixelStorei(GLenum pname, GLint param) override;
  60. void* MapBufferCHROMIUM(GLuint target, GLenum access) override;
  61. GLboolean UnmapBufferCHROMIUM(GLuint target) override;
  62. void BufferData(GLenum target,
  63. GLsizeiptr size,
  64. const void* data,
  65. GLenum usage) override;
  66. void BeginQueryEXT(GLenum target, GLuint id) override;
  67. void EndQueryEXT(GLenum target) override;
  68. void GetQueryObjectuivEXT(GLuint id, GLenum pname, GLuint* params) override;
  69. void ProduceTextureDirectCHROMIUM(GLuint texture, GLbyte* mailbox) override;
  70. GLuint CreateAndConsumeTextureCHROMIUM(const GLbyte* mailbox) override;
  71. GLuint CreateAndTexStorage2DSharedImageCHROMIUM(
  72. const GLbyte* mailbox) override;
  73. void ResizeCHROMIUM(GLuint width,
  74. GLuint height,
  75. float device_scale,
  76. GLcolorSpace color_space,
  77. GLboolean has_alpha) override;
  78. void LoseContextCHROMIUM(GLenum current, GLenum other) override;
  79. GLenum GetGraphicsResetStatusKHR() override;
  80. // Overridden from gpu::InterfaceBase
  81. void GenSyncTokenCHROMIUM(GLbyte* sync_token) override;
  82. void GenUnverifiedSyncTokenCHROMIUM(GLbyte* sync_token) override;
  83. void VerifySyncTokensCHROMIUM(GLbyte** sync_tokens, GLsizei count) override;
  84. void WaitSyncTokenCHROMIUM(const GLbyte* sync_token) override;
  85. size_t NumTextures() const;
  86. size_t NumUsedTextures() const { return used_textures_.size(); }
  87. bool UsedTexture(int texture) const {
  88. return base::Contains(used_textures_, texture);
  89. }
  90. void ResetUsedTextures() { used_textures_.clear(); }
  91. size_t NumFramebuffers() const;
  92. size_t NumRenderbuffers() const;
  93. bool IsContextLost() { return context_lost_; }
  94. void set_test_support(TestContextSupport* test_support) {
  95. test_support_ = test_support;
  96. }
  97. const gpu::Capabilities& test_capabilities() const {
  98. return test_capabilities_;
  99. }
  100. const gpu::SyncToken& last_waited_sync_token() const {
  101. return last_waited_sync_token_;
  102. }
  103. void set_context_lost(bool context_lost) { context_lost_ = context_lost; }
  104. void set_times_bind_texture_succeeds(int times);
  105. void set_have_extension_io_surface(bool have);
  106. void set_have_extension_egl_image(bool have);
  107. void set_have_post_sub_buffer(bool have);
  108. void set_have_swap_buffers_with_bounds(bool have);
  109. void set_have_commit_overlay_planes(bool have);
  110. void set_have_discard_framebuffer(bool have);
  111. void set_support_compressed_texture_etc1(bool support);
  112. void set_support_texture_format_bgra8888(bool support);
  113. void set_support_texture_storage(bool support);
  114. void set_support_texture_usage(bool support);
  115. void set_support_sync_query(bool support);
  116. void set_support_texture_rectangle(bool support);
  117. void set_support_texture_half_float_linear(bool support);
  118. void set_support_texture_norm16(bool support);
  119. void set_msaa_is_slow(bool msaa_is_slow);
  120. void set_gpu_rasterization(bool gpu_rasterization);
  121. void set_avoid_stencil_buffers(bool avoid_stencil_buffers);
  122. void set_support_multisample_compatibility(bool support);
  123. void set_support_texture_storage_image(bool support);
  124. void set_support_texture_npot(bool support);
  125. void set_supports_oop_raster(bool support);
  126. void set_max_texture_size(int size);
  127. void set_supports_shared_image_swap_chain(bool support);
  128. void set_supports_gpu_memory_buffer_format(gfx::BufferFormat format,
  129. bool support);
  130. // When set, MapBufferCHROMIUM will return NULL after this many times.
  131. void set_times_map_buffer_chromium_succeeds(int times) {
  132. times_map_buffer_chromium_succeeds_ = times;
  133. }
  134. virtual GLuint NextTextureId();
  135. virtual void RetireTextureId(GLuint id);
  136. virtual GLuint NextBufferId();
  137. virtual void RetireBufferId(GLuint id);
  138. virtual GLuint NextImageId();
  139. virtual void RetireImageId(GLuint id);
  140. virtual GLuint NextFramebufferId();
  141. virtual void RetireFramebufferId(GLuint id);
  142. virtual GLuint NextRenderbufferId();
  143. virtual void RetireRenderbufferId(GLuint id);
  144. void set_context_lost_callback(base::OnceClosure callback) {
  145. context_lost_callback_ = std::move(callback);
  146. }
  147. int width() const { return width_; }
  148. int height() const { return height_; }
  149. bool reshape_called() const { return reshape_called_; }
  150. void clear_reshape_called() { reshape_called_ = false; }
  151. float scale_factor() const { return scale_factor_; }
  152. enum UpdateType { NO_UPDATE = 0, PREPARE_TEXTURE, POST_SUB_BUFFER };
  153. gfx::Rect update_rect() const { return update_rect_; }
  154. UpdateType last_update_type() { return last_update_type_; }
  155. protected:
  156. struct Buffer {
  157. Buffer();
  158. Buffer(const Buffer&) = delete;
  159. Buffer& operator=(const Buffer&) = delete;
  160. ~Buffer();
  161. GLenum target;
  162. std::unique_ptr<uint8_t[]> pixels;
  163. size_t size;
  164. };
  165. unsigned context_id_;
  166. gpu::Capabilities test_capabilities_;
  167. int times_bind_texture_succeeds_ = -1;
  168. int times_end_query_succeeds_ = -1;
  169. bool context_lost_ = false;
  170. int times_map_buffer_chromium_succeeds_ = -1;
  171. base::OnceClosure context_lost_callback_;
  172. std::unordered_set<unsigned> used_textures_;
  173. unsigned next_program_id_ = 1000;
  174. std::unordered_set<unsigned> program_set_;
  175. unsigned next_shader_id_ = 2000;
  176. std::unordered_set<unsigned> shader_set_;
  177. unsigned next_framebuffer_id_ = 1;
  178. std::unordered_set<unsigned> framebuffer_set_;
  179. unsigned current_framebuffer_ = 0;
  180. std::vector<TestGLES2Interface*> shared_contexts_;
  181. bool reshape_called_ = false;
  182. int width_ = 0;
  183. int height_ = 0;
  184. float scale_factor_ = -1.f;
  185. raw_ptr<TestContextSupport> test_support_ = nullptr;
  186. gfx::Rect update_rect_;
  187. UpdateType last_update_type_ = NO_UPDATE;
  188. GLuint64 next_insert_fence_sync_ = 1;
  189. gpu::SyncToken last_waited_sync_token_;
  190. int unpack_alignment_ = 4;
  191. base::flat_map<unsigned, unsigned> bound_buffer_;
  192. unsigned next_buffer_id_ = 1;
  193. unsigned next_image_id_ = 1;
  194. unsigned next_texture_id_ = 1;
  195. unsigned next_renderbuffer_id_ = 1;
  196. std::unordered_map<unsigned, std::unique_ptr<Buffer>> buffers_;
  197. std::unordered_set<unsigned> textures_;
  198. std::unordered_set<unsigned> renderbuffer_set_;
  199. base::WeakPtrFactory<TestGLES2Interface> weak_ptr_factory_{this};
  200. };
  201. } // namespace viz
  202. #endif // COMPONENTS_VIZ_TEST_TEST_GLES2_INTERFACE_H_