in_process_context_provider.cc 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. // Copyright (c) 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. #include "ui/compositor/test/in_process_context_provider.h"
  5. #include "base/bind.h"
  6. #include "base/callback_helpers.h"
  7. #include "base/lazy_instance.h"
  8. #include "base/memory/scoped_refptr.h"
  9. #include "base/strings/stringprintf.h"
  10. #include "base/threading/thread_task_runner_handle.h"
  11. #include "base/trace_event/trace_event.h"
  12. #include "components/viz/common/gpu/context_cache_controller.h"
  13. #include "components/viz/service/gl/gpu_service_impl.h"
  14. #include "components/viz/test/test_gpu_service_holder.h"
  15. #include "gpu/command_buffer/client/gles2_implementation.h"
  16. #include "gpu/command_buffer/client/raster_implementation.h"
  17. #include "gpu/command_buffer/client/raster_implementation_gles.h"
  18. #include "gpu/command_buffer/client/shared_memory_limits.h"
  19. #include "gpu/config/skia_limits.h"
  20. #include "gpu/ipc/gl_in_process_context.h"
  21. #include "gpu/skia_bindings/grcontext_for_gles2_interface.h"
  22. #include "ipc/common/surface_handle.h"
  23. #include "ipc/raster_in_process_context.h"
  24. #include "third_party/skia/include/gpu/GrDirectContext.h"
  25. #include "third_party/skia/include/gpu/gl/GrGLInterface.h"
  26. namespace ui {
  27. // static
  28. scoped_refptr<InProcessContextProvider>
  29. InProcessContextProvider::CreateOffscreen(
  30. gpu::GpuMemoryBufferManager* gpu_memory_buffer_manager,
  31. gpu::ImageFactory* image_factory,
  32. bool is_worker) {
  33. gpu::ContextCreationAttribs attribs;
  34. attribs.alpha_size = 8;
  35. attribs.blue_size = 8;
  36. attribs.green_size = 8;
  37. attribs.red_size = 8;
  38. attribs.depth_size = 0;
  39. attribs.stencil_size = 8;
  40. attribs.samples = 0;
  41. attribs.sample_buffers = 0;
  42. attribs.fail_if_major_perf_caveat = false;
  43. attribs.bind_generates_resource = false;
  44. attribs.enable_raster_interface = true;
  45. attribs.enable_gles2_interface = !is_worker;
  46. attribs.enable_oop_rasterization = is_worker;
  47. return new InProcessContextProvider(attribs, gpu_memory_buffer_manager,
  48. image_factory, is_worker);
  49. }
  50. InProcessContextProvider::InProcessContextProvider(
  51. const gpu::ContextCreationAttribs& attribs,
  52. gpu::GpuMemoryBufferManager* gpu_memory_buffer_manager,
  53. gpu::ImageFactory* image_factory,
  54. bool support_locking)
  55. : support_locking_(support_locking),
  56. attribs_(attribs),
  57. image_factory_(image_factory) {
  58. DCHECK(main_thread_checker_.CalledOnValidThread());
  59. context_thread_checker_.DetachFromThread();
  60. }
  61. InProcessContextProvider::~InProcessContextProvider() {
  62. DCHECK(main_thread_checker_.CalledOnValidThread() ||
  63. context_thread_checker_.CalledOnValidThread());
  64. }
  65. void InProcessContextProvider::AddRef() const {
  66. base::RefCountedThreadSafe<InProcessContextProvider>::AddRef();
  67. }
  68. void InProcessContextProvider::Release() const {
  69. base::RefCountedThreadSafe<InProcessContextProvider>::Release();
  70. }
  71. gpu::ContextResult InProcessContextProvider::BindToCurrentThread() {
  72. // This is called on the thread the context will be used.
  73. DCHECK(context_thread_checker_.CalledOnValidThread());
  74. if (bind_tried_)
  75. return bind_result_;
  76. bind_tried_ = true;
  77. auto* holder = viz::TestGpuServiceHolder::GetInstance();
  78. if (attribs_.enable_oop_rasterization) {
  79. DCHECK(!attribs_.enable_gles2_interface);
  80. DCHECK(!attribs_.enable_grcontext);
  81. raster_context_ = std::make_unique<gpu::RasterInProcessContext>();
  82. bind_result_ = raster_context_->Initialize(
  83. holder->task_executor(), attribs_, gpu::SharedMemoryLimits(),
  84. image_factory_, holder->gpu_service()->gr_shader_cache(), nullptr);
  85. impl_base_ = raster_context_->GetImplementation();
  86. } else {
  87. gles2_context_ = std::make_unique<gpu::GLInProcessContext>();
  88. bind_result_ = gles2_context_->Initialize(
  89. viz::TestGpuServiceHolder::GetInstance()->task_executor(), attribs_,
  90. gpu::SharedMemoryLimits(), image_factory_);
  91. impl_base_ = gles2_context_->GetImplementation();
  92. }
  93. if (bind_result_ != gpu::ContextResult::kSuccess)
  94. return bind_result_;
  95. cache_controller_ = std::make_unique<viz::ContextCacheController>(
  96. impl_base_, base::ThreadTaskRunnerHandle::Get());
  97. if (support_locking_)
  98. cache_controller_->SetLock(GetLock());
  99. if (gles2_context_) {
  100. gles2_raster_impl_ =
  101. std::make_unique<gpu::raster::RasterImplementationGLES>(
  102. ContextGL(), ContextSupport());
  103. }
  104. return bind_result_;
  105. }
  106. const gpu::Capabilities& InProcessContextProvider::ContextCapabilities() const {
  107. CheckValidThreadOrLockAcquired();
  108. return impl_base_->capabilities();
  109. }
  110. const gpu::GpuFeatureInfo& InProcessContextProvider::GetGpuFeatureInfo() const {
  111. CheckValidThreadOrLockAcquired();
  112. return gles2_context_ ? gles2_context_->GetGpuFeatureInfo()
  113. : raster_context_->GetGpuFeatureInfo();
  114. }
  115. gpu::gles2::GLES2Interface* InProcessContextProvider::ContextGL() {
  116. CheckValidThreadOrLockAcquired();
  117. if (!gles2_context_)
  118. return nullptr;
  119. return gles2_context_->GetImplementation();
  120. }
  121. gpu::raster::RasterInterface* InProcessContextProvider::RasterInterface() {
  122. CheckValidThreadOrLockAcquired();
  123. return raster_context_ ? raster_context_->GetImplementation()
  124. : gles2_raster_impl_.get();
  125. }
  126. gpu::ContextSupport* InProcessContextProvider::ContextSupport() {
  127. return impl_base_;
  128. }
  129. class GrDirectContext* InProcessContextProvider::GrContext() {
  130. CheckValidThreadOrLockAcquired();
  131. if (attribs_.enable_oop_rasterization)
  132. return nullptr;
  133. if (gr_context_)
  134. return gr_context_->get();
  135. size_t max_resource_cache_bytes;
  136. size_t max_glyph_cache_texture_bytes;
  137. gpu::DefaultGrCacheLimitsForTests(&max_resource_cache_bytes,
  138. &max_glyph_cache_texture_bytes);
  139. gr_context_ = std::make_unique<skia_bindings::GrContextForGLES2Interface>(
  140. ContextGL(), ContextSupport(), ContextCapabilities(),
  141. max_resource_cache_bytes, max_glyph_cache_texture_bytes);
  142. cache_controller_->SetGrContext(gr_context_->get());
  143. return gr_context_->get();
  144. }
  145. gpu::SharedImageInterface* InProcessContextProvider::SharedImageInterface() {
  146. return gles2_context_ ? gles2_context_->GetSharedImageInterface()
  147. : raster_context_->GetSharedImageInterface();
  148. }
  149. viz::ContextCacheController* InProcessContextProvider::CacheController() {
  150. CheckValidThreadOrLockAcquired();
  151. return cache_controller_.get();
  152. }
  153. base::Lock* InProcessContextProvider::GetLock() {
  154. if (!support_locking_)
  155. return nullptr;
  156. return &context_lock_;
  157. }
  158. void InProcessContextProvider::AddObserver(viz::ContextLostObserver* obs) {
  159. observers_.AddObserver(obs);
  160. }
  161. void InProcessContextProvider::RemoveObserver(viz::ContextLostObserver* obs) {
  162. observers_.RemoveObserver(obs);
  163. }
  164. uint32_t InProcessContextProvider::GetCopyTextureInternalFormat() {
  165. if (attribs_.alpha_size > 0)
  166. return GL_RGBA;
  167. DCHECK_NE(attribs_.red_size, 0);
  168. DCHECK_NE(attribs_.green_size, 0);
  169. DCHECK_NE(attribs_.blue_size, 0);
  170. return GL_RGB;
  171. }
  172. void InProcessContextProvider::SendOnContextLost() {
  173. for (auto& observer : observers_)
  174. observer.OnContextLost();
  175. }
  176. void InProcessContextProvider::CheckValidThreadOrLockAcquired() const {
  177. #if DCHECK_IS_ON()
  178. if (support_locking_) {
  179. context_lock_.AssertAcquired();
  180. } else {
  181. DCHECK(context_thread_checker_.CalledOnValidThread());
  182. }
  183. #endif
  184. }
  185. } // namespace ui