- add sources.
[platform/framework/web/crosswalk.git] / src / cc / test / test_context_provider.cc
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 "base/strings/string_split.h"
14 #include "cc/test/test_web_graphics_context_3d.h"
15
16 namespace cc {
17
18 class TestContextProvider::LostContextCallbackProxy
19     : public WebKit::WebGraphicsContext3D::WebGraphicsContextLostCallback {
20  public:
21   explicit LostContextCallbackProxy(TestContextProvider* provider)
22       : provider_(provider) {
23     provider_->context3d_->setContextLostCallback(this);
24   }
25
26   virtual ~LostContextCallbackProxy() {
27     provider_->context3d_->setContextLostCallback(NULL);
28   }
29
30   virtual void onContextLost() {
31     provider_->OnLostContext();
32   }
33
34  private:
35   TestContextProvider* provider_;
36 };
37
38 class TestContextProvider::SwapBuffersCompleteCallbackProxy
39     : public WebKit::WebGraphicsContext3D::
40           WebGraphicsSwapBuffersCompleteCallbackCHROMIUM {
41  public:
42   explicit SwapBuffersCompleteCallbackProxy(TestContextProvider* provider)
43       : provider_(provider) {
44     provider_->context3d_->setSwapBuffersCompleteCallbackCHROMIUM(this);
45   }
46
47   virtual ~SwapBuffersCompleteCallbackProxy() {
48     provider_->context3d_->setSwapBuffersCompleteCallbackCHROMIUM(NULL);
49   }
50
51   virtual void onSwapBuffersComplete() {
52     provider_->OnSwapBuffersComplete();
53   }
54
55  private:
56   TestContextProvider* provider_;
57 };
58
59 // static
60 scoped_refptr<TestContextProvider> TestContextProvider::Create() {
61   return Create(TestWebGraphicsContext3D::Create().Pass());
62 }
63
64 // static
65 scoped_refptr<TestContextProvider> TestContextProvider::Create(
66     scoped_ptr<TestWebGraphicsContext3D> context) {
67   if (!context)
68     return NULL;
69   return new TestContextProvider(context.Pass());
70 }
71
72 TestContextProvider::TestContextProvider(
73     scoped_ptr<TestWebGraphicsContext3D> context)
74     : context3d_(context.Pass()), bound_(false), destroyed_(false) {
75   DCHECK(main_thread_checker_.CalledOnValidThread());
76   DCHECK(context3d_);
77   context_thread_checker_.DetachFromThread();
78   context3d_->set_test_support(&support_);
79 }
80
81 TestContextProvider::~TestContextProvider() {
82   DCHECK(main_thread_checker_.CalledOnValidThread() ||
83          context_thread_checker_.CalledOnValidThread());
84 }
85
86 bool TestContextProvider::BindToCurrentThread() {
87   DCHECK(context3d_);
88
89   // This is called on the thread the context will be used.
90   DCHECK(context_thread_checker_.CalledOnValidThread());
91
92   if (bound_)
93     return true;
94
95   bound_ = true;
96   if (!context3d_->makeContextCurrent()) {
97     base::AutoLock lock(destroyed_lock_);
98     destroyed_ = true;
99     return false;
100   }
101
102   lost_context_callback_proxy_.reset(new LostContextCallbackProxy(this));
103   swap_buffers_complete_callback_proxy_.reset(
104       new SwapBuffersCompleteCallbackProxy(this));
105
106   return true;
107 }
108
109 ContextProvider::Capabilities TestContextProvider::ContextCapabilities() {
110   DCHECK(context3d_);
111   DCHECK(bound_);
112   DCHECK(context_thread_checker_.CalledOnValidThread());
113
114   return context3d_->test_capabilities();
115 }
116
117 WebKit::WebGraphicsContext3D* TestContextProvider::Context3d() {
118   DCHECK(context3d_);
119   DCHECK(bound_);
120   DCHECK(context_thread_checker_.CalledOnValidThread());
121
122   return context3d_.get();
123 }
124
125 gpu::ContextSupport* TestContextProvider::ContextSupport() {
126   DCHECK(context3d_);
127   DCHECK(bound_);
128   DCHECK(context_thread_checker_.CalledOnValidThread());
129
130   return &support_;
131 }
132
133 class GrContext* TestContextProvider::GrContext() {
134   DCHECK(context3d_);
135   DCHECK(bound_);
136   DCHECK(context_thread_checker_.CalledOnValidThread());
137
138   // TODO(danakj): Make a test GrContext that works with a test Context3d.
139   return NULL;
140 }
141
142 void TestContextProvider::VerifyContexts() {
143   DCHECK(context3d_);
144   DCHECK(bound_);
145   DCHECK(context_thread_checker_.CalledOnValidThread());
146
147   if (context3d_->isContextLost()) {
148     base::AutoLock lock(destroyed_lock_);
149     destroyed_ = true;
150   }
151 }
152
153 bool TestContextProvider::DestroyedOnMainThread() {
154   DCHECK(main_thread_checker_.CalledOnValidThread());
155
156   base::AutoLock lock(destroyed_lock_);
157   return destroyed_;
158 }
159
160 void TestContextProvider::OnLostContext() {
161   DCHECK(context_thread_checker_.CalledOnValidThread());
162   {
163     base::AutoLock lock(destroyed_lock_);
164     if (destroyed_)
165       return;
166     destroyed_ = true;
167   }
168   if (!lost_context_callback_.is_null())
169     base::ResetAndReturn(&lost_context_callback_).Run();
170 }
171
172 void TestContextProvider::OnSwapBuffersComplete() {
173   DCHECK(context_thread_checker_.CalledOnValidThread());
174   if (!swap_buffers_complete_callback_.is_null())
175     swap_buffers_complete_callback_.Run();
176 }
177
178 TestWebGraphicsContext3D* TestContextProvider::TestContext3d() {
179   DCHECK(context3d_);
180   DCHECK(bound_);
181   DCHECK(context_thread_checker_.CalledOnValidThread());
182
183   return context3d_.get();
184 }
185
186 TestWebGraphicsContext3D* TestContextProvider::UnboundTestContext3d() {
187   DCHECK(context3d_);
188   DCHECK(context_thread_checker_.CalledOnValidThread());
189
190   return context3d_.get();
191 }
192
193 void TestContextProvider::SetMemoryAllocation(
194     const ManagedMemoryPolicy& policy) {
195   if (memory_policy_changed_callback_.is_null())
196     return;
197   memory_policy_changed_callback_.Run(policy);
198 }
199
200 void TestContextProvider::SetLostContextCallback(
201     const LostContextCallback& cb) {
202   DCHECK(context_thread_checker_.CalledOnValidThread());
203   DCHECK(lost_context_callback_.is_null() || cb.is_null());
204   lost_context_callback_ = cb;
205 }
206
207 void TestContextProvider::SetSwapBuffersCompleteCallback(
208     const SwapBuffersCompleteCallback& cb) {
209   DCHECK(context_thread_checker_.CalledOnValidThread());
210   DCHECK(swap_buffers_complete_callback_.is_null() || cb.is_null());
211   swap_buffers_complete_callback_ = cb;
212 }
213
214 void TestContextProvider::SetMemoryPolicyChangedCallback(
215     const MemoryPolicyChangedCallback& cb) {
216   DCHECK(context_thread_checker_.CalledOnValidThread());
217   DCHECK(memory_policy_changed_callback_.is_null() || cb.is_null());
218   memory_policy_changed_callback_ = cb;
219 }
220
221 void TestContextProvider::SetMaxTransferBufferUsageBytes(
222     size_t max_transfer_buffer_usage_bytes) {
223   context3d_->SetMaxTransferBufferUsageBytes(max_transfer_buffer_usage_bytes);
224 }
225
226 }  // namespace cc