| 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::DeferredCreate::DeferredCreate() = default; |
| 37 |
| 38 TestContextProvider::DeferredCreate::DeferredCreate( |
| 39 std::unique_ptr<TestWebGraphicsContext3D> context) |
| 40 : webcontext(std::move(context)) {} |
| 41 |
| 42 TestContextProvider::DeferredCreate::~DeferredCreate() = default; |
| 43 |
| 44 scoped_refptr<ContextProvider> |
| 45 TestContextProvider::DeferredCreate::CreateContext() { |
| 46 if (fail_create) |
| 47 return nullptr; |
| 48 |
| 49 if (!webcontext) { |
| 50 webcontext = TestWebGraphicsContext3D::Create(); |
| 51 webcontext->set_capabilities(capabilities); |
| 52 } |
| 53 scoped_refptr<TestContextProvider> context( |
| 54 new TestContextProvider(std::move(webcontext))); |
| 55 // If the share group is lost, fail create. |
| 56 if (context->ContextGL()->GetGraphicsResetStatusKHR() != GL_NO_ERROR) |
| 57 return nullptr; |
| 58 if (created_context) |
| 59 *created_context = context.get(); |
| 60 return context; |
| 61 } |
| 62 |
| 46 TestContextProvider::TestContextProvider( | 63 TestContextProvider::TestContextProvider( |
| 47 std::unique_ptr<TestWebGraphicsContext3D> context) | 64 std::unique_ptr<TestWebGraphicsContext3D> context) |
| 48 : context3d_(std::move(context)), | 65 : context3d_(std::move(context)), |
| 49 context_gl_(new TestGLES2Interface(context3d_.get())), | 66 context_gl_(new TestGLES2Interface(context3d_.get())), |
| 50 bound_(false), | |
| 51 weak_ptr_factory_(this) { | 67 weak_ptr_factory_(this) { |
| 52 DCHECK(main_thread_checker_.CalledOnValidThread()); | 68 DCHECK(thread_checker_.CalledOnValidThread()); |
| 53 DCHECK(context3d_); | 69 DCHECK(context3d_); |
| 54 context_thread_checker_.DetachFromThread(); | |
| 55 context3d_->set_test_support(&support_); | 70 context3d_->set_test_support(&support_); |
| 71 context3d_->set_context_lost_callback( |
| 72 base::Bind(&TestContextProvider::OnLostContext, |
| 73 base::Unretained(this))); |
| 56 } | 74 } |
| 57 | 75 |
| 58 TestContextProvider::~TestContextProvider() { | 76 TestContextProvider::~TestContextProvider() { |
| 59 DCHECK(main_thread_checker_.CalledOnValidThread() || | 77 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 } | 78 } |
| 81 | 79 |
| 82 void TestContextProvider::DetachFromThread() { | 80 void TestContextProvider::DetachFromThread() { |
| 83 context_thread_checker_.DetachFromThread(); | 81 thread_checker_.DetachFromThread(); |
| 84 } | 82 } |
| 85 | 83 |
| 86 gpu::Capabilities TestContextProvider::ContextCapabilities() { | 84 gpu::Capabilities TestContextProvider::ContextCapabilities() { |
| 87 DCHECK(bound_); | 85 DCHECK(thread_checker_.CalledOnValidThread()); |
| 88 DCHECK(context_thread_checker_.CalledOnValidThread()); | |
| 89 return context3d_->test_capabilities(); | 86 return context3d_->test_capabilities(); |
| 90 } | 87 } |
| 91 | 88 |
| 92 gpu::gles2::GLES2Interface* TestContextProvider::ContextGL() { | 89 gpu::gles2::GLES2Interface* TestContextProvider::ContextGL() { |
| 93 DCHECK(context3d_); | 90 DCHECK(context3d_); |
| 94 DCHECK(bound_); | 91 DCHECK(thread_checker_.CalledOnValidThread()); |
| 95 DCHECK(context_thread_checker_.CalledOnValidThread()); | |
| 96 | 92 |
| 97 return context_gl_.get(); | 93 return context_gl_.get(); |
| 98 } | 94 } |
| 99 | 95 |
| 100 gpu::ContextSupport* TestContextProvider::ContextSupport() { | 96 gpu::ContextSupport* TestContextProvider::ContextSupport() { |
| 101 return &support_; | 97 return &support_; |
| 102 } | 98 } |
| 103 | 99 |
| 104 class GrContext* TestContextProvider::GrContext() { | 100 class GrContext* TestContextProvider::GrContext() { |
| 105 DCHECK(bound_); | 101 DCHECK(thread_checker_.CalledOnValidThread()); |
| 106 DCHECK(context_thread_checker_.CalledOnValidThread()); | |
| 107 | 102 |
| 108 if (gr_context_) | 103 if (gr_context_) |
| 109 return gr_context_.get(); | 104 return gr_context_.get(); |
| 110 | 105 |
| 111 sk_sp<const GrGLInterface> gl_interface(GrGLCreateNullInterface()); | 106 sk_sp<const GrGLInterface> gl_interface(GrGLCreateNullInterface()); |
| 112 gr_context_ = sk_sp<::GrContext>(GrContext::Create( | 107 gr_context_ = sk_sp<::GrContext>(GrContext::Create( |
| 113 kOpenGL_GrBackend, | 108 kOpenGL_GrBackend, |
| 114 reinterpret_cast<GrBackendContext>(gl_interface.get()))); | 109 reinterpret_cast<GrBackendContext>(gl_interface.get()))); |
| 115 | 110 |
| 116 // If GlContext is already lost, also abandon the new GrContext. | 111 // If GlContext is already lost, also abandon the new GrContext. |
| 117 if (ContextGL()->GetGraphicsResetStatusKHR() != GL_NO_ERROR) | 112 if (ContextGL()->GetGraphicsResetStatusKHR() != GL_NO_ERROR) |
| 118 gr_context_->abandonContext(); | 113 gr_context_->abandonContext(); |
| 119 | 114 |
| 120 return gr_context_.get(); | 115 return gr_context_.get(); |
| 121 } | 116 } |
| 122 | 117 |
| 123 void TestContextProvider::InvalidateGrContext(uint32_t state) { | 118 void TestContextProvider::InvalidateGrContext(uint32_t state) { |
| 124 DCHECK(bound_); | 119 DCHECK(thread_checker_.CalledOnValidThread()); |
| 125 DCHECK(context_thread_checker_.CalledOnValidThread()); | |
| 126 | 120 |
| 127 if (gr_context_) | 121 if (gr_context_) |
| 128 gr_context_.get()->resetContext(state); | 122 gr_context_.get()->resetContext(state); |
| 129 } | 123 } |
| 130 | 124 |
| 131 base::Lock* TestContextProvider::GetLock() { | 125 base::Lock* TestContextProvider::GetLock() { |
| 132 return &context_lock_; | 126 return &context_lock_; |
| 133 } | 127 } |
| 134 | 128 |
| 135 void TestContextProvider::DeleteCachedResources() { | 129 void TestContextProvider::DeleteCachedResources() { |
| 136 } | 130 } |
| 137 | 131 |
| 138 void TestContextProvider::OnLostContext() { | 132 void TestContextProvider::OnLostContext() { |
| 139 DCHECK(context_thread_checker_.CalledOnValidThread()); | 133 DCHECK(thread_checker_.CalledOnValidThread()); |
| 140 if (!lost_context_callback_.is_null()) | 134 if (!lost_context_callback_.is_null()) |
| 141 lost_context_callback_.Run(); | 135 lost_context_callback_.Run(); |
| 142 if (gr_context_) | 136 if (gr_context_) |
| 143 gr_context_->abandonContext(); | 137 gr_context_->abandonContext(); |
| 144 } | 138 } |
| 145 | 139 |
| 146 TestWebGraphicsContext3D* TestContextProvider::TestContext3d() { | 140 TestWebGraphicsContext3D* TestContextProvider::TestContext3d() { |
| 147 DCHECK(bound_); | 141 DCHECK(thread_checker_.CalledOnValidThread()); |
| 148 DCHECK(context_thread_checker_.CalledOnValidThread()); | |
| 149 | |
| 150 return context3d_.get(); | 142 return context3d_.get(); |
| 151 } | 143 } |
| 152 | 144 |
| 153 TestWebGraphicsContext3D* TestContextProvider::UnboundTestContext3d() { | 145 TestWebGraphicsContext3D* TestContextProvider::UnboundTestContext3d() { |
| 146 DCHECK(thread_checker_.CalledOnValidThread()); |
| 154 return context3d_.get(); | 147 return context3d_.get(); |
| 155 } | 148 } |
| 156 | 149 |
| 157 void TestContextProvider::SetLostContextCallback( | 150 void TestContextProvider::SetLostContextCallback( |
| 158 const LostContextCallback& cb) { | 151 const LostContextCallback& cb) { |
| 159 DCHECK(context_thread_checker_.CalledOnValidThread()); | 152 DCHECK(thread_checker_.CalledOnValidThread()); |
| 160 DCHECK(lost_context_callback_.is_null() || cb.is_null()); | 153 DCHECK(lost_context_callback_.is_null() || cb.is_null()); |
| 161 lost_context_callback_ = cb; | 154 lost_context_callback_ = cb; |
| 162 } | 155 } |
| 163 | 156 |
| 164 } // namespace cc | 157 } // namespace cc |
| OLD | NEW |