Add version checking to webview library loads.
[chromium-blink-merge.git] / android_webview / browser / test / fake_window.cc
blob16d9405727615890512f5d2decdd7e4b5db7c2a0
1 // Copyright 2014 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.
5 #include "android_webview/browser/test/fake_window.h"
7 #include "android_webview/browser/browser_view_renderer.h"
8 #include "base/location.h"
9 #include "base/synchronization/waitable_event.h"
10 #include "base/thread_task_runner_handle.h"
11 #include "base/threading/thread.h"
12 #include "ui/gl/gl_bindings.h"
14 namespace android_webview {
16 class FakeWindow::ScopedMakeCurrent {
17 public:
18 ScopedMakeCurrent(FakeWindow* view_root) : view_root_(view_root) {
19 DCHECK(!view_root_->context_current_);
20 view_root_->context_current_ = true;
21 bool result = view_root_->context_->MakeCurrent(view_root_->surface_.get());
22 DCHECK(result);
25 ~ScopedMakeCurrent() {
26 DCHECK(view_root_->context_current_);
27 view_root_->context_current_ = false;
29 // Release the underlying EGLContext. This is required because the real
30 // GLContextEGL may no longer be current here and to satisfy DCHECK in
31 // GLContextEGL::IsCurrent.
32 eglMakeCurrent(view_root_->surface_->GetDisplay(), EGL_NO_SURFACE,
33 EGL_NO_SURFACE, EGL_NO_CONTEXT);
34 view_root_->context_->ReleaseCurrent(view_root_->surface_.get());
37 private:
38 FakeWindow* view_root_;
41 FakeWindow::FakeWindow(BrowserViewRenderer* view,
42 WindowHooks* hooks,
43 gfx::Rect location)
44 : view_(view),
45 hooks_(hooks),
46 surface_size_(100, 100),
47 location_(location),
48 on_draw_hardware_pending_(false),
49 functor_(nullptr),
50 context_current_(false),
51 weak_ptr_factory_(this) {
52 CheckCurrentlyOnUIThread();
53 DCHECK(view_);
54 view_->OnAttachedToWindow(location_.width(), location_.height());
55 view_->SetWindowVisibility(true);
56 view_->SetViewVisibility(true);
59 FakeWindow::~FakeWindow() {
60 CheckCurrentlyOnUIThread();
63 void FakeWindow::Detach() {
64 CheckCurrentlyOnUIThread();
65 view_->OnDetachedFromWindow();
67 if (render_thread_loop_) {
68 base::WaitableEvent completion(true, false);
69 render_thread_loop_->PostTask(
70 FROM_HERE, base::Bind(&FakeWindow::DestroyOnRT, base::Unretained(this),
71 &completion));
72 completion.Wait();
75 render_thread_.reset();
76 functor_ = nullptr;
79 void FakeWindow::RequestDrawGL(bool wait_for_completion) {
80 CheckCurrentlyOnUIThread();
81 base::WaitableEvent completion(true, false);
82 render_thread_loop_->PostTask(
83 FROM_HERE,
84 base::Bind(&FakeWindow::ProcessFunctorOnRT, base::Unretained(this),
85 wait_for_completion ? &completion : nullptr));
86 if (wait_for_completion)
87 completion.Wait();
90 void FakeWindow::ProcessFunctorOnRT(base::WaitableEvent* sync) {
91 CheckCurrentlyOnRT();
92 AwDrawGLInfo process_info;
93 process_info.version = kAwDrawGLInfoVersion;
94 process_info.mode = AwDrawGLInfo::kModeProcess;
96 hooks_->WillProcessOnRT(functor_);
98 ScopedMakeCurrent make_current(this);
99 functor_->DrawGL(&process_info);
101 hooks_->DidProcessOnRT(functor_);
103 if (sync)
104 sync->Signal();
107 void FakeWindow::PostInvalidate() {
108 CheckCurrentlyOnUIThread();
109 if (on_draw_hardware_pending_)
110 return;
111 on_draw_hardware_pending_ = true;
112 base::ThreadTaskRunnerHandle::Get()->PostTask(
113 FROM_HERE,
114 base::Bind(&FakeWindow::OnDrawHardware, weak_ptr_factory_.GetWeakPtr()));
117 void FakeWindow::OnDrawHardware() {
118 CheckCurrentlyOnUIThread();
119 DCHECK(on_draw_hardware_pending_);
120 on_draw_hardware_pending_ = false;
122 view_->PrepareToDraw(gfx::Vector2d(), location_);
123 hooks_->WillOnDraw();
124 bool success = view_->OnDrawHardware();
125 hooks_->DidOnDraw(success);
126 if (success) {
127 CreateRenderThreadIfNeeded();
129 base::WaitableEvent completion(true, false);
130 render_thread_loop_->PostTask(
131 FROM_HERE, base::Bind(&FakeWindow::DrawFunctorOnRT,
132 base::Unretained(this), &completion));
133 completion.Wait();
137 void FakeWindow::DrawFunctorOnRT(base::WaitableEvent* sync) {
138 CheckCurrentlyOnRT();
139 // Ok to access UI functions until sync is signalled.
140 gfx::Rect location = location_;
142 AwDrawGLInfo process_info;
143 process_info.version = kAwDrawGLInfoVersion;
144 process_info.mode = AwDrawGLInfo::kModeSync;
146 hooks_->WillSyncOnRT(functor_);
147 functor_->DrawGL(&process_info);
148 hooks_->DidSyncOnRT(functor_);
150 sync->Signal();
152 AwDrawGLInfo draw_info;
153 draw_info.version = kAwDrawGLInfoVersion;
154 draw_info.mode = AwDrawGLInfo::kModeDraw;
155 draw_info.clip_left = location.x();
156 draw_info.clip_top = location.y();
157 draw_info.clip_right = location.x() + location.width();
158 draw_info.clip_bottom = location.y() + location.height();
160 if (!hooks_->WillDrawOnRT(functor_, &draw_info))
161 return;
164 ScopedMakeCurrent make_current(this);
165 functor_->DrawGL(&draw_info);
167 hooks_->DidDrawOnRT(functor_);
170 void FakeWindow::CheckCurrentlyOnUIThread() {
171 DCHECK(ui_checker_.CalledOnValidSequencedThread());
174 void FakeWindow::CreateRenderThreadIfNeeded() {
175 CheckCurrentlyOnUIThread();
176 if (functor_) {
177 DCHECK(render_thread_.get());
178 DCHECK(render_thread_loop_.get());
179 return;
181 functor_ = view_->GetAwDrawGLViewContext();
182 render_thread_.reset(new base::Thread("TestRenderThread"));
183 render_thread_->Start();
184 render_thread_loop_ = render_thread_->task_runner();
185 rt_checker_.DetachFromSequence();
187 base::WaitableEvent completion(true, false);
188 render_thread_loop_->PostTask(
189 FROM_HERE, base::Bind(&FakeWindow::InitializeOnRT, base::Unretained(this),
190 &completion));
191 completion.Wait();
194 void FakeWindow::InitializeOnRT(base::WaitableEvent* sync) {
195 CheckCurrentlyOnRT();
196 surface_ = gfx::GLSurface::CreateOffscreenGLSurface(surface_size_);
197 DCHECK(surface_.get());
198 DCHECK(surface_->GetHandle());
199 context_ = gfx::GLContext::CreateGLContext(nullptr, surface_.get(),
200 gfx::PreferDiscreteGpu);
201 DCHECK(context_.get());
202 sync->Signal();
205 void FakeWindow::DestroyOnRT(base::WaitableEvent* sync) {
206 CheckCurrentlyOnRT();
207 if (context_) {
208 DCHECK(!context_->IsCurrent(surface_.get()));
209 context_ = nullptr;
210 surface_ = nullptr;
212 sync->Signal();
215 void FakeWindow::CheckCurrentlyOnRT() {
216 DCHECK(rt_checker_.CalledOnValidSequencedThread());
219 } // namespace android_webview