OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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 "gpu/ipc/service/child_window_surface_win.h" | 5 #include "gpu/ipc/service/child_window_win.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 | 8 |
9 #include "base/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
11 #include "base/threading/thread.h" | 11 #include "base/threading/thread.h" |
12 #include "base/win/scoped_hdc.h" | 12 #include "base/win/scoped_hdc.h" |
13 #include "base/win/wrapped_window_proc.h" | 13 #include "base/win/wrapped_window_proc.h" |
14 #include "gpu/ipc/common/gpu_messages.h" | 14 #include "gpu/ipc/common/gpu_messages.h" |
15 #include "gpu/ipc/service/gpu_channel_manager.h" | 15 #include "gpu/ipc/service/gpu_channel_manager.h" |
16 #include "gpu/ipc/service/gpu_channel_manager_delegate.h" | 16 #include "gpu/ipc/service/gpu_channel_manager_delegate.h" |
17 #include "ui/gfx/native_widget_types.h" | 17 #include "ui/gfx/native_widget_types.h" |
18 #include "ui/gfx/win/hwnd_util.h" | 18 #include "ui/gfx/win/hwnd_util.h" |
19 #include "ui/gfx/win/window_impl.h" | 19 #include "ui/gfx/win/window_impl.h" |
20 #include "ui/gl/egl_util.h" | |
21 #include "ui/gl/gl_context.h" | |
22 #include "ui/gl/gl_surface_egl.h" | |
23 #include "ui/gl/scoped_make_current.h" | |
24 | 20 |
25 namespace gpu { | 21 namespace gpu { |
26 | 22 |
27 // This owns the thread and contains data that's shared between the threads. | 23 // This owns the thread and contains data that's shared between the threads. |
28 struct SharedData { | 24 struct SharedData { |
29 SharedData() : thread("Window owner thread") {} | 25 SharedData() : thread("Window owner thread") {} |
30 | 26 |
31 base::Lock rect_lock; | 27 base::Lock rect_lock; |
32 gfx::Rect rect_to_clear; | 28 gfx::Rect rect_to_clear; |
33 | 29 |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
149 } | 145 } |
150 | 146 |
151 // This runs on the window owner thread. | 147 // This runs on the window owner thread. |
152 void DestroyWindowsOnThread(HWND child_window, HWND hidden_popup_window) { | 148 void DestroyWindowsOnThread(HWND child_window, HWND hidden_popup_window) { |
153 DestroyWindow(child_window); | 149 DestroyWindow(child_window); |
154 HiddenPopupWindow::Destroy(hidden_popup_window); | 150 HiddenPopupWindow::Destroy(hidden_popup_window); |
155 } | 151 } |
156 | 152 |
157 } // namespace | 153 } // namespace |
158 | 154 |
159 ChildWindowSurfaceWin::ChildWindowSurfaceWin( | 155 ChildWindowWin::ChildWindowWin( |
160 base::WeakPtr<ImageTransportSurfaceDelegate> delegate, | 156 base::WeakPtr<ImageTransportSurfaceDelegate> delegate, |
161 HWND parent_window) | 157 HWND parent_window) |
162 : gl::NativeViewGLSurfaceEGL(0), | 158 : parent_window_(parent_window), window_(nullptr), delegate_(delegate) {} |
163 parent_window_(parent_window), | |
164 delegate_(delegate), | |
165 alpha_(true), | |
166 first_swap_(true) { | |
167 // Don't use EGL_ANGLE_window_fixed_size so that we can avoid recreating the | |
168 // window surface, which can cause flicker on DirectComposition. | |
169 enable_fixed_size_angle_ = false; | |
170 } | |
171 | 159 |
172 EGLConfig ChildWindowSurfaceWin::GetConfig() { | 160 bool ChildWindowWin::Initialize() { |
173 if (!config_) { | |
174 int alpha_size = alpha_ ? 8 : EGL_DONT_CARE; | |
175 | |
176 EGLint config_attribs[] = {EGL_ALPHA_SIZE, | |
177 alpha_size, | |
178 EGL_BLUE_SIZE, | |
179 8, | |
180 EGL_GREEN_SIZE, | |
181 8, | |
182 EGL_RED_SIZE, | |
183 8, | |
184 EGL_RENDERABLE_TYPE, | |
185 EGL_OPENGL_ES2_BIT, | |
186 EGL_SURFACE_TYPE, | |
187 EGL_WINDOW_BIT | EGL_PBUFFER_BIT, | |
188 EGL_NONE}; | |
189 | |
190 EGLDisplay display = GetHardwareDisplay(); | |
191 EGLint num_configs; | |
192 if (!eglChooseConfig(display, config_attribs, &config_, 1, &num_configs)) { | |
193 LOG(ERROR) << "eglChooseConfig failed with error " | |
194 << ui::GetLastEGLErrorString(); | |
195 return NULL; | |
196 } | |
197 } | |
198 | |
199 return config_; | |
200 } | |
201 | |
202 bool ChildWindowSurfaceWin::InitializeNativeWindow() { | |
203 if (window_) | 161 if (window_) |
204 return true; | 162 return true; |
205 | 163 |
206 shared_data_ = base::MakeUnique<SharedData>(); | 164 shared_data_ = base::MakeUnique<SharedData>(); |
207 | 165 |
208 base::Thread::Options options(base::MessageLoop::TYPE_UI, 0); | 166 base::Thread::Options options(base::MessageLoop::TYPE_UI, 0); |
209 shared_data_->thread.StartWithOptions(options); | 167 shared_data_->thread.StartWithOptions(options); |
210 | 168 |
211 base::WaitableEvent event(base::WaitableEvent::ResetPolicy::AUTOMATIC, | 169 base::WaitableEvent event(base::WaitableEvent::ResetPolicy::AUTOMATIC, |
212 base::WaitableEvent::InitialState::NOT_SIGNALED); | 170 base::WaitableEvent::InitialState::NOT_SIGNALED); |
213 | 171 |
214 RECT window_rect; | 172 RECT window_rect; |
215 GetClientRect(parent_window_, &window_rect); | 173 GetClientRect(parent_window_, &window_rect); |
216 | 174 |
217 shared_data_->thread.task_runner()->PostTask( | 175 shared_data_->thread.task_runner()->PostTask( |
218 FROM_HERE, | 176 FROM_HERE, |
219 base::Bind(&CreateWindowsOnThread, gfx::Rect(window_rect).size(), &event, | 177 base::Bind(&CreateWindowsOnThread, gfx::Rect(window_rect).size(), &event, |
220 shared_data_.get(), &window_, &initial_parent_window_)); | 178 shared_data_.get(), &window_, &initial_parent_window_)); |
221 event.Wait(); | 179 event.Wait(); |
222 | 180 |
223 delegate_->DidCreateAcceleratedSurfaceChildWindow(parent_window_, window_); | 181 delegate_->DidCreateAcceleratedSurfaceChildWindow(parent_window_, window_); |
224 return true; | 182 return true; |
225 } | 183 } |
226 | 184 |
227 bool ChildWindowSurfaceWin::Resize(const gfx::Size& size, | 185 void ChildWindowWin::ClearInvalidContents() { |
228 float scale_factor, | |
229 bool has_alpha) { | |
230 if (!SupportsPostSubBuffer()) { | |
231 if (!MoveWindow(window_, 0, 0, size.width(), size.height(), FALSE)) { | |
232 return false; | |
233 } | |
234 alpha_ = has_alpha; | |
235 return gl::NativeViewGLSurfaceEGL::Resize(size, scale_factor, has_alpha); | |
236 } else { | |
237 if (size == GetSize() && has_alpha == alpha_) | |
238 return true; | |
239 | |
240 // Force a resize and redraw (but not a move, activate, etc.). | |
241 if (!SetWindowPos(window_, nullptr, 0, 0, size.width(), size.height(), | |
242 SWP_NOMOVE | SWP_NOACTIVATE | SWP_NOCOPYBITS | | |
243 SWP_NOOWNERZORDER | SWP_NOZORDER)) { | |
244 return false; | |
245 } | |
246 size_ = size; | |
247 if (has_alpha == alpha_) { | |
248 // A 0-size PostSubBuffer doesn't swap but forces the swap chain to resize | |
249 // to match the window. | |
250 PostSubBuffer(0, 0, 0, 0); | |
251 } else { | |
252 alpha_ = has_alpha; | |
253 config_ = nullptr; | |
254 | |
255 std::unique_ptr<ui::ScopedMakeCurrent> scoped_make_current; | |
256 gl::GLContext* current_context = gl::GLContext::GetCurrent(); | |
257 bool was_current = current_context && current_context->IsCurrent(this); | |
258 if (was_current) { | |
259 scoped_make_current.reset( | |
260 new ui::ScopedMakeCurrent(current_context, this)); | |
261 current_context->ReleaseCurrent(this); | |
262 } | |
263 | |
264 Destroy(); | |
265 | |
266 if (!Initialize()) { | |
267 LOG(ERROR) << "Failed to resize window."; | |
268 return false; | |
269 } | |
270 } | |
271 return true; | |
272 } | |
273 } | |
274 | |
275 gfx::SwapResult ChildWindowSurfaceWin::SwapBuffers() { | |
276 gfx::SwapResult result = NativeViewGLSurfaceEGL::SwapBuffers(); | |
277 // Force the driver to finish drawing before clearing the contents to | |
278 // transparent, to reduce or eliminate the period of time where the contents | |
279 // have flashed black. | |
280 if (first_swap_) { | |
281 glFinish(); | |
282 first_swap_ = false; | |
283 } | |
284 ClearInvalidContents(); | |
285 return result; | |
286 } | |
287 | |
288 gfx::SwapResult ChildWindowSurfaceWin::PostSubBuffer(int x, | |
289 int y, | |
290 int width, | |
291 int height) { | |
292 gfx::SwapResult result = | |
293 NativeViewGLSurfaceEGL::PostSubBuffer(x, y, width, height); | |
294 ClearInvalidContents(); | |
295 return result; | |
296 } | |
297 | |
298 void ChildWindowSurfaceWin::ClearInvalidContents() { | |
299 base::AutoLock lock(shared_data_->rect_lock); | 186 base::AutoLock lock(shared_data_->rect_lock); |
300 if (!shared_data_->rect_to_clear.IsEmpty()) { | 187 if (!shared_data_->rect_to_clear.IsEmpty()) { |
301 base::win::ScopedGetDC dc(window_); | 188 base::win::ScopedGetDC dc(window_); |
302 | 189 |
303 RECT rect = shared_data_->rect_to_clear.ToRECT(); | 190 RECT rect = shared_data_->rect_to_clear.ToRECT(); |
304 | 191 |
305 // DirectComposition composites with the contents under the SwapChain, | 192 // DirectComposition composites with the contents under the SwapChain, |
306 // so ensure that's cleared. GDI treats black as transparent. | 193 // so ensure that's cleared. GDI treats black as transparent. |
307 FillRect(dc, &rect, reinterpret_cast<HBRUSH>(GetStockObject(BLACK_BRUSH))); | 194 FillRect(dc, &rect, reinterpret_cast<HBRUSH>(GetStockObject(BLACK_BRUSH))); |
308 shared_data_->rect_to_clear = gfx::Rect(); | 195 shared_data_->rect_to_clear = gfx::Rect(); |
309 } | 196 } |
310 } | 197 } |
311 | 198 |
312 ChildWindowSurfaceWin::~ChildWindowSurfaceWin() { | 199 ChildWindowWin::~ChildWindowWin() { |
313 if (shared_data_) { | 200 if (shared_data_) { |
314 scoped_refptr<base::TaskRunner> task_runner = | 201 scoped_refptr<base::TaskRunner> task_runner = |
315 shared_data_->thread.task_runner(); | 202 shared_data_->thread.task_runner(); |
316 task_runner->PostTaskAndReply( | 203 task_runner->PostTaskAndReply( |
317 FROM_HERE, | 204 FROM_HERE, |
318 base::Bind(&DestroyWindowsOnThread, window_, initial_parent_window_), | 205 base::Bind(&DestroyWindowsOnThread, window_, initial_parent_window_), |
319 base::Bind(&DestroySharedData, base::Passed(std::move(shared_data_)))); | 206 base::Bind(&DestroySharedData, base::Passed(std::move(shared_data_)))); |
320 } | 207 } |
321 } | 208 } |
322 | 209 |
323 } // namespace gpu | 210 } // namespace gpu |
OLD | NEW |