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
{
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());
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());
38 FakeWindow
* view_root_
;
41 FakeWindow::FakeWindow(BrowserViewRenderer
* view
,
46 surface_size_(100, 100),
48 on_draw_hardware_pending_(false),
50 context_current_(false),
51 weak_ptr_factory_(this) {
52 CheckCurrentlyOnUIThread();
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),
75 render_thread_
.reset();
79 void FakeWindow::RequestDrawGL(bool wait_for_completion
) {
80 CheckCurrentlyOnUIThread();
81 base::WaitableEvent
completion(true, false);
82 render_thread_loop_
->PostTask(
84 base::Bind(&FakeWindow::ProcessFunctorOnRT
, base::Unretained(this),
85 wait_for_completion
? &completion
: nullptr));
86 if (wait_for_completion
)
90 void FakeWindow::ProcessFunctorOnRT(base::WaitableEvent
* sync
) {
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_
);
107 void FakeWindow::PostInvalidate() {
108 CheckCurrentlyOnUIThread();
109 if (on_draw_hardware_pending_
)
111 on_draw_hardware_pending_
= true;
112 base::ThreadTaskRunnerHandle::Get()->PostTask(
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
);
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
));
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_
);
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
))
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();
177 DCHECK(render_thread_
.get());
178 DCHECK(render_thread_loop_
.get());
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),
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());
205 void FakeWindow::DestroyOnRT(base::WaitableEvent
* sync
) {
206 CheckCurrentlyOnRT();
208 DCHECK(!context_
->IsCurrent(surface_
.get()));
215 void FakeWindow::CheckCurrentlyOnRT() {
216 DCHECK(rt_checker_
.CalledOnValidSequencedThread());
219 } // namespace android_webview