| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "cc/test/test_context_provider.h" | 5 #include "cc/test/test_context_provider.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <set> | 10 #include <set> |
| 11 #include <vector> | 11 #include <vector> |
| 12 | 12 |
| 13 #include "base/bind.h" | 13 #include "base/bind.h" |
| 14 #include "base/callback_helpers.h" | 14 #include "base/callback_helpers.h" |
| 15 #include "base/logging.h" | 15 #include "base/logging.h" |
| 16 #include "cc/test/test_gles2_interface.h" | 16 #include "cc/test/test_gles2_interface.h" |
| 17 #include "cc/test/test_web_graphics_context_3d.h" | 17 #include "cc/test/test_web_graphics_context_3d.h" |
| 18 #include "third_party/skia/include/gpu/GrContext.h" | 18 #include "third_party/skia/include/gpu/GrContext.h" |
| 19 #include "third_party/skia/include/gpu/gl/GrGLInterface.h" | 19 #include "third_party/skia/include/gpu/gl/GrGLInterface.h" |
| 20 | 20 |
| 21 namespace cc { | 21 namespace cc { |
| 22 | 22 |
| 23 // static | 23 // static |
| 24 scoped_refptr<TestContextProvider> TestContextProvider::Create() { | 24 scoped_refptr<TestContextProvider> TestContextProvider::Create() { |
| 25 return Create(TestWebGraphicsContext3D::Create()); | 25 return Create(TestWebGraphicsContext3D::Create()); |
| 26 } | 26 } |
| 27 | 27 |
| 28 // static | 28 // static |
| 29 scoped_refptr<TestContextProvider> TestContextProvider::CreateWorker() { | |
| 30 scoped_refptr<TestContextProvider> worker_context_provider = | |
| 31 Create(TestWebGraphicsContext3D::Create()); | |
| 32 // Worker contexts are bound to the thread they are created on. | |
| 33 if (!worker_context_provider->BindToCurrentThread()) | |
| 34 return nullptr; | |
| 35 return worker_context_provider; | |
| 36 } | |
| 37 | |
| 38 // static | |
| 39 scoped_refptr<TestContextProvider> TestContextProvider::Create( | 29 scoped_refptr<TestContextProvider> TestContextProvider::Create( |
| 40 std::unique_ptr<TestWebGraphicsContext3D> context) { | 30 std::unique_ptr<TestWebGraphicsContext3D> context) { |
| 41 if (!context) | 31 if (!context) |
| 42 return NULL; | 32 return NULL; |
| 43 return new TestContextProvider(std::move(context)); | 33 return new TestContextProvider(std::move(context)); |
| 44 } | 34 } |
| 45 | 35 |
| 36 TestContextProvider::Factory::Factory() = default; |
| 37 |
| 38 TestContextProvider::Factory::Factory( |
| 39 std::unique_ptr<TestWebGraphicsContext3D> context) |
| 40 : context_(std::move(context)) {} |
| 41 |
| 42 TestContextProvider::Factory::Factory(const gpu::Capabilities& capabilities) |
| 43 : capabilities_(capabilities) {} |
| 44 |
| 45 TestContextProvider::Factory::Factory(FailCreate) : fail_create_(true) {} |
| 46 |
| 47 TestContextProvider::Factory::~Factory() = default; |
| 48 |
| 49 scoped_refptr<ContextProvider> TestContextProvider::Factory::CreateContext() { |
| 50 if (fail_create_) |
| 51 return nullptr; |
| 52 std::unique_ptr<TestWebGraphicsContext3D> context = std::move(context_); |
| 53 if (!context) { |
| 54 context = TestWebGraphicsContext3D::Create(); |
| 55 context->set_capabilities(capabilities_); |
| 56 } |
| 57 return make_scoped_refptr(new TestContextProvider(std::move(context))); |
| 58 } |
| 59 |
| 46 TestContextProvider::TestContextProvider( | 60 TestContextProvider::TestContextProvider( |
| 47 std::unique_ptr<TestWebGraphicsContext3D> context) | 61 std::unique_ptr<TestWebGraphicsContext3D> context) |
| 48 : context3d_(std::move(context)), | 62 : context3d_(std::move(context)), |
| 49 context_gl_(new TestGLES2Interface(context3d_.get())), | 63 context_gl_(new TestGLES2Interface(context3d_.get())), |
| 50 bound_(false), | |
| 51 weak_ptr_factory_(this) { | 64 weak_ptr_factory_(this) { |
| 52 DCHECK(main_thread_checker_.CalledOnValidThread()); | 65 DCHECK(thread_checker_.CalledOnValidThread()); |
| 53 DCHECK(context3d_); | 66 DCHECK(context3d_); |
| 54 context_thread_checker_.DetachFromThread(); | |
| 55 context3d_->set_test_support(&support_); | 67 context3d_->set_test_support(&support_); |
| 68 context3d_->set_context_lost_callback( |
| 69 base::Bind(&TestContextProvider::OnLostContext, |
| 70 base::Unretained(this))); |
| 56 } | 71 } |
| 57 | 72 |
| 58 TestContextProvider::~TestContextProvider() { | 73 TestContextProvider::~TestContextProvider() { |
| 59 DCHECK(main_thread_checker_.CalledOnValidThread() || | 74 DCHECK(thread_checker_.CalledOnValidThread()); |
| 60 context_thread_checker_.CalledOnValidThread()); | |
| 61 } | |
| 62 | |
| 63 bool TestContextProvider::BindToCurrentThread() { | |
| 64 // This is called on the thread the context will be used. | |
| 65 DCHECK(context_thread_checker_.CalledOnValidThread()); | |
| 66 | |
| 67 if (bound_) | |
| 68 return true; | |
| 69 | |
| 70 if (context_gl_->GetGraphicsResetStatusKHR() != GL_NO_ERROR) { | |
| 71 return false; | |
| 72 } | |
| 73 bound_ = true; | |
| 74 | |
| 75 context3d_->set_context_lost_callback( | |
| 76 base::Bind(&TestContextProvider::OnLostContext, | |
| 77 base::Unretained(this))); | |
| 78 | |
| 79 return true; | |
| 80 } | 75 } |
| 81 | 76 |
| 82 void TestContextProvider::DetachFromThread() { | 77 void TestContextProvider::DetachFromThread() { |
| 83 context_thread_checker_.DetachFromThread(); | 78 thread_checker_.DetachFromThread(); |
| 84 } | 79 } |
| 85 | 80 |
| 86 gpu::Capabilities TestContextProvider::ContextCapabilities() { | 81 gpu::Capabilities TestContextProvider::ContextCapabilities() { |
| 87 DCHECK(bound_); | 82 DCHECK(thread_checker_.CalledOnValidThread()); |
| 88 DCHECK(context_thread_checker_.CalledOnValidThread()); | |
| 89 return context3d_->test_capabilities(); | 83 return context3d_->test_capabilities(); |
| 90 } | 84 } |
| 91 | 85 |
| 92 gpu::gles2::GLES2Interface* TestContextProvider::ContextGL() { | 86 gpu::gles2::GLES2Interface* TestContextProvider::ContextGL() { |
| 93 DCHECK(context3d_); | 87 DCHECK(context3d_); |
| 94 DCHECK(bound_); | 88 DCHECK(thread_checker_.CalledOnValidThread()); |
| 95 DCHECK(context_thread_checker_.CalledOnValidThread()); | |
| 96 | 89 |
| 97 return context_gl_.get(); | 90 return context_gl_.get(); |
| 98 } | 91 } |
| 99 | 92 |
| 100 gpu::ContextSupport* TestContextProvider::ContextSupport() { | 93 gpu::ContextSupport* TestContextProvider::ContextSupport() { |
| 101 return &support_; | 94 return &support_; |
| 102 } | 95 } |
| 103 | 96 |
| 104 class GrContext* TestContextProvider::GrContext() { | 97 class GrContext* TestContextProvider::GrContext() { |
| 105 DCHECK(bound_); | 98 DCHECK(thread_checker_.CalledOnValidThread()); |
| 106 DCHECK(context_thread_checker_.CalledOnValidThread()); | |
| 107 | 99 |
| 108 if (gr_context_) | 100 if (gr_context_) |
| 109 return gr_context_.get(); | 101 return gr_context_.get(); |
| 110 | 102 |
| 111 sk_sp<const GrGLInterface> gl_interface(GrGLCreateNullInterface()); | 103 sk_sp<const GrGLInterface> gl_interface(GrGLCreateNullInterface()); |
| 112 gr_context_ = sk_sp<::GrContext>(GrContext::Create( | 104 gr_context_ = sk_sp<::GrContext>(GrContext::Create( |
| 113 kOpenGL_GrBackend, | 105 kOpenGL_GrBackend, |
| 114 reinterpret_cast<GrBackendContext>(gl_interface.get()))); | 106 reinterpret_cast<GrBackendContext>(gl_interface.get()))); |
| 115 | 107 |
| 116 // If GlContext is already lost, also abandon the new GrContext. | 108 // If GlContext is already lost, also abandon the new GrContext. |
| 117 if (ContextGL()->GetGraphicsResetStatusKHR() != GL_NO_ERROR) | 109 if (ContextGL()->GetGraphicsResetStatusKHR() != GL_NO_ERROR) |
| 118 gr_context_->abandonContext(); | 110 gr_context_->abandonContext(); |
| 119 | 111 |
| 120 return gr_context_.get(); | 112 return gr_context_.get(); |
| 121 } | 113 } |
| 122 | 114 |
| 123 void TestContextProvider::InvalidateGrContext(uint32_t state) { | 115 void TestContextProvider::InvalidateGrContext(uint32_t state) { |
| 124 DCHECK(bound_); | 116 DCHECK(thread_checker_.CalledOnValidThread()); |
| 125 DCHECK(context_thread_checker_.CalledOnValidThread()); | |
| 126 | 117 |
| 127 if (gr_context_) | 118 if (gr_context_) |
| 128 gr_context_.get()->resetContext(state); | 119 gr_context_.get()->resetContext(state); |
| 129 } | 120 } |
| 130 | 121 |
| 131 base::Lock* TestContextProvider::GetLock() { | 122 base::Lock* TestContextProvider::GetLock() { |
| 132 return &context_lock_; | 123 return &context_lock_; |
| 133 } | 124 } |
| 134 | 125 |
| 135 void TestContextProvider::DeleteCachedResources() { | 126 void TestContextProvider::DeleteCachedResources() { |
| 136 } | 127 } |
| 137 | 128 |
| 138 void TestContextProvider::OnLostContext() { | 129 void TestContextProvider::OnLostContext() { |
| 139 DCHECK(context_thread_checker_.CalledOnValidThread()); | 130 DCHECK(thread_checker_.CalledOnValidThread()); |
| 140 if (!lost_context_callback_.is_null()) | 131 if (!lost_context_callback_.is_null()) |
| 141 lost_context_callback_.Run(); | 132 lost_context_callback_.Run(); |
| 142 if (gr_context_) | 133 if (gr_context_) |
| 143 gr_context_->abandonContext(); | 134 gr_context_->abandonContext(); |
| 144 } | 135 } |
| 145 | 136 |
| 146 TestWebGraphicsContext3D* TestContextProvider::TestContext3d() { | 137 TestWebGraphicsContext3D* TestContextProvider::TestContext3d() { |
| 147 DCHECK(bound_); | 138 DCHECK(thread_checker_.CalledOnValidThread()); |
| 148 DCHECK(context_thread_checker_.CalledOnValidThread()); | |
| 149 | |
| 150 return context3d_.get(); | |
| 151 } | |
| 152 | |
| 153 TestWebGraphicsContext3D* TestContextProvider::UnboundTestContext3d() { | |
| 154 return context3d_.get(); | 139 return context3d_.get(); |
| 155 } | 140 } |
| 156 | 141 |
| 157 void TestContextProvider::SetLostContextCallback( | 142 void TestContextProvider::SetLostContextCallback( |
| 158 const LostContextCallback& cb) { | 143 const LostContextCallback& cb) { |
| 159 DCHECK(context_thread_checker_.CalledOnValidThread()); | 144 DCHECK(thread_checker_.CalledOnValidThread()); |
| 160 DCHECK(lost_context_callback_.is_null() || cb.is_null()); | 145 DCHECK(lost_context_callback_.is_null() || cb.is_null()); |
| 161 lost_context_callback_ = cb; | 146 lost_context_callback_ = cb; |
| 162 } | 147 } |
| 163 | 148 |
| 164 } // namespace cc | 149 } // namespace cc |
| OLD | NEW |