| OLD | NEW |
| (Empty) |
| 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 | |
| 5 #include "cc/test/test_context_provider.h" | |
| 6 | |
| 7 #include <set> | |
| 8 #include <vector> | |
| 9 | |
| 10 #include "base/bind.h" | |
| 11 #include "base/callback_helpers.h" | |
| 12 #include "base/logging.h" | |
| 13 #include "cc/test/test_gles2_interface.h" | |
| 14 #include "third_party/skia/include/gpu/GrContext.h" | |
| 15 #include "third_party/skia/include/gpu/gl/SkNullGLContext.h" | |
| 16 | |
| 17 namespace cc { | |
| 18 | |
| 19 // static | |
| 20 scoped_refptr<TestContextProvider> TestContextProvider::Create() { | |
| 21 return Create(TestWebGraphicsContext3D::Create().Pass()); | |
| 22 } | |
| 23 | |
| 24 // static | |
| 25 scoped_refptr<TestContextProvider> TestContextProvider::Create( | |
| 26 scoped_ptr<TestWebGraphicsContext3D> context) { | |
| 27 if (!context) | |
| 28 return NULL; | |
| 29 return new TestContextProvider(context.Pass()); | |
| 30 } | |
| 31 | |
| 32 TestContextProvider::TestContextProvider( | |
| 33 scoped_ptr<TestWebGraphicsContext3D> context) | |
| 34 : context3d_(context.Pass()), | |
| 35 context_gl_(new TestGLES2Interface(context3d_.get())), | |
| 36 bound_(false), | |
| 37 destroyed_(false), | |
| 38 weak_ptr_factory_(this) { | |
| 39 DCHECK(main_thread_checker_.CalledOnValidThread()); | |
| 40 DCHECK(context3d_); | |
| 41 context_thread_checker_.DetachFromThread(); | |
| 42 context3d_->set_test_support(&support_); | |
| 43 } | |
| 44 | |
| 45 TestContextProvider::~TestContextProvider() { | |
| 46 DCHECK(main_thread_checker_.CalledOnValidThread() || | |
| 47 context_thread_checker_.CalledOnValidThread()); | |
| 48 } | |
| 49 | |
| 50 bool TestContextProvider::BindToCurrentThread() { | |
| 51 // This is called on the thread the context will be used. | |
| 52 DCHECK(context_thread_checker_.CalledOnValidThread()); | |
| 53 | |
| 54 if (bound_) | |
| 55 return true; | |
| 56 | |
| 57 if (context3d_->isContextLost()) { | |
| 58 base::AutoLock lock(destroyed_lock_); | |
| 59 destroyed_ = true; | |
| 60 return false; | |
| 61 } | |
| 62 bound_ = true; | |
| 63 | |
| 64 context3d_->set_context_lost_callback( | |
| 65 base::Bind(&TestContextProvider::OnLostContext, | |
| 66 base::Unretained(this))); | |
| 67 | |
| 68 return true; | |
| 69 } | |
| 70 | |
| 71 void TestContextProvider::DetachFromThread() { | |
| 72 context_thread_checker_.DetachFromThread(); | |
| 73 } | |
| 74 | |
| 75 ContextProvider::Capabilities TestContextProvider::ContextCapabilities() { | |
| 76 DCHECK(bound_); | |
| 77 DCHECK(context_thread_checker_.CalledOnValidThread()); | |
| 78 | |
| 79 return context3d_->test_capabilities(); | |
| 80 } | |
| 81 | |
| 82 gpu::gles2::GLES2Interface* TestContextProvider::ContextGL() { | |
| 83 DCHECK(context3d_); | |
| 84 DCHECK(bound_); | |
| 85 DCHECK(context_thread_checker_.CalledOnValidThread()); | |
| 86 | |
| 87 return context_gl_.get(); | |
| 88 } | |
| 89 | |
| 90 gpu::ContextSupport* TestContextProvider::ContextSupport() { | |
| 91 return &support_; | |
| 92 } | |
| 93 | |
| 94 class GrContext* TestContextProvider::GrContext() { | |
| 95 DCHECK(bound_); | |
| 96 DCHECK(context_thread_checker_.CalledOnValidThread()); | |
| 97 | |
| 98 if (gr_context_) | |
| 99 return gr_context_.get(); | |
| 100 | |
| 101 skia::RefPtr<class SkGLContext> gl_context = | |
| 102 skia::AdoptRef(SkNullGLContext::Create(kNone_GrGLStandard)); | |
| 103 gl_context->makeCurrent(); | |
| 104 gr_context_ = skia::AdoptRef(GrContext::Create( | |
| 105 kOpenGL_GrBackend, reinterpret_cast<GrBackendContext>(gl_context->gl()))); | |
| 106 | |
| 107 // If GlContext is already lost, also abandon the new GrContext. | |
| 108 if (IsContextLost()) | |
| 109 gr_context_->abandonContext(); | |
| 110 | |
| 111 return gr_context_.get(); | |
| 112 } | |
| 113 | |
| 114 void TestContextProvider::SetupLock() { | |
| 115 } | |
| 116 | |
| 117 base::Lock* TestContextProvider::GetLock() { | |
| 118 return &context_lock_; | |
| 119 } | |
| 120 | |
| 121 bool TestContextProvider::IsContextLost() { | |
| 122 DCHECK(bound_); | |
| 123 DCHECK(context_thread_checker_.CalledOnValidThread()); | |
| 124 | |
| 125 return context3d_->isContextLost(); | |
| 126 } | |
| 127 | |
| 128 void TestContextProvider::VerifyContexts() { | |
| 129 DCHECK(bound_); | |
| 130 DCHECK(context_thread_checker_.CalledOnValidThread()); | |
| 131 | |
| 132 if (context3d_->isContextLost()) { | |
| 133 base::AutoLock lock(destroyed_lock_); | |
| 134 destroyed_ = true; | |
| 135 } | |
| 136 } | |
| 137 | |
| 138 void TestContextProvider::DeleteCachedResources() { | |
| 139 } | |
| 140 | |
| 141 bool TestContextProvider::DestroyedOnMainThread() { | |
| 142 DCHECK(main_thread_checker_.CalledOnValidThread()); | |
| 143 | |
| 144 base::AutoLock lock(destroyed_lock_); | |
| 145 return destroyed_; | |
| 146 } | |
| 147 | |
| 148 void TestContextProvider::OnLostContext() { | |
| 149 DCHECK(context_thread_checker_.CalledOnValidThread()); | |
| 150 { | |
| 151 base::AutoLock lock(destroyed_lock_); | |
| 152 if (destroyed_) | |
| 153 return; | |
| 154 destroyed_ = true; | |
| 155 } | |
| 156 if (!lost_context_callback_.is_null()) | |
| 157 base::ResetAndReturn(&lost_context_callback_).Run(); | |
| 158 if (gr_context_) | |
| 159 gr_context_->abandonContext(); | |
| 160 } | |
| 161 | |
| 162 TestWebGraphicsContext3D* TestContextProvider::TestContext3d() { | |
| 163 DCHECK(bound_); | |
| 164 DCHECK(context_thread_checker_.CalledOnValidThread()); | |
| 165 | |
| 166 return context3d_.get(); | |
| 167 } | |
| 168 | |
| 169 TestWebGraphicsContext3D* TestContextProvider::UnboundTestContext3d() { | |
| 170 return context3d_.get(); | |
| 171 } | |
| 172 | |
| 173 void TestContextProvider::SetMemoryAllocation( | |
| 174 const ManagedMemoryPolicy& policy) { | |
| 175 if (memory_policy_changed_callback_.is_null()) | |
| 176 return; | |
| 177 memory_policy_changed_callback_.Run(policy); | |
| 178 } | |
| 179 | |
| 180 void TestContextProvider::SetLostContextCallback( | |
| 181 const LostContextCallback& cb) { | |
| 182 DCHECK(context_thread_checker_.CalledOnValidThread()); | |
| 183 DCHECK(lost_context_callback_.is_null() || cb.is_null()); | |
| 184 lost_context_callback_ = cb; | |
| 185 } | |
| 186 | |
| 187 void TestContextProvider::SetMemoryPolicyChangedCallback( | |
| 188 const MemoryPolicyChangedCallback& cb) { | |
| 189 DCHECK(context_thread_checker_.CalledOnValidThread()); | |
| 190 DCHECK(memory_policy_changed_callback_.is_null() || cb.is_null()); | |
| 191 memory_policy_changed_callback_ = cb; | |
| 192 } | |
| 193 | |
| 194 void TestContextProvider::SetMaxTransferBufferUsageBytes( | |
| 195 size_t max_transfer_buffer_usage_bytes) { | |
| 196 context3d_->SetMaxTransferBufferUsageBytes(max_transfer_buffer_usage_bytes); | |
| 197 } | |
| 198 | |
| 199 } // namespace cc | |
| OLD | NEW |