OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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 "content/common/gpu/media/rendering_helper.h" | 5 #include "content/common/gpu/media/rendering_helper.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <numeric> | 8 #include <numeric> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/bind.h" | 11 #include "base/bind.h" |
12 #include "base/callback_helpers.h" | 12 #include "base/callback_helpers.h" |
13 #include "base/command_line.h" | 13 #include "base/command_line.h" |
14 #include "base/mac/scoped_nsautorelease_pool.h" | 14 #include "base/mac/scoped_nsautorelease_pool.h" |
15 #include "base/message_loop/message_loop.h" | 15 #include "base/message_loop/message_loop.h" |
16 #include "base/run_loop.h" | |
16 #include "base/strings/stringize_macros.h" | 17 #include "base/strings/stringize_macros.h" |
17 #include "base/synchronization/waitable_event.h" | 18 #include "base/synchronization/waitable_event.h" |
18 #include "base/time/time.h" | 19 #include "base/time/time.h" |
19 #include "ui/gl/gl_context.h" | 20 #include "ui/gl/gl_context.h" |
20 #include "ui/gl/gl_implementation.h" | 21 #include "ui/gl/gl_implementation.h" |
21 #include "ui/gl/gl_surface.h" | 22 #include "ui/gl/gl_surface.h" |
22 | 23 |
23 #if defined(OS_WIN) | 24 #if defined(OS_WIN) |
24 #include <windows.h> | 25 #include <windows.h> |
25 #endif | 26 #endif |
26 | 27 |
27 #if defined(USE_X11) | 28 #if defined(USE_X11) |
28 #include "ui/gfx/x/x11_types.h" | 29 #include "ui/gfx/x/x11_types.h" |
29 #endif | 30 #endif |
30 | 31 |
31 #if defined(ARCH_CPU_X86_FAMILY) && defined(USE_X11) | 32 #if defined(ARCH_CPU_X86_FAMILY) && defined(USE_X11) |
32 #include "ui/gl/gl_surface_glx.h" | 33 #include "ui/gl/gl_surface_glx.h" |
33 #define GL_VARIANT_GLX 1 | 34 #define GL_VARIANT_GLX 1 |
34 #else | 35 #else |
35 #include "ui/gl/gl_surface_egl.h" | 36 #include "ui/gl/gl_surface_egl.h" |
36 #define GL_VARIANT_EGL 1 | 37 #define GL_VARIANT_EGL 1 |
37 #endif | 38 #endif |
38 | 39 |
39 #if defined(USE_OZONE) | 40 #if defined(USE_OZONE) |
40 #if defined(OS_CHROMEOS) | 41 #if defined(OS_CHROMEOS) |
41 #include "ui/display/chromeos/display_configurator.h" | 42 #include "ui/display/chromeos/display_configurator.h" |
43 #include "ui/display/types/display_constants.h" | |
42 #endif // defined(OS_CHROMEOS) | 44 #endif // defined(OS_CHROMEOS) |
43 #include "ui/ozone/public/ozone_platform.h" | 45 #include "ui/ozone/public/ozone_platform.h" |
44 #include "ui/ozone/public/ui_thread_gpu.h" | 46 #include "ui/ozone/public/ui_thread_gpu.h" |
45 #include "ui/platform_window/platform_window.h" | 47 #include "ui/platform_window/platform_window.h" |
46 #include "ui/platform_window/platform_window_delegate.h" | 48 #include "ui/platform_window/platform_window_delegate.h" |
47 #endif // defined(USE_OZONE) | 49 #endif // defined(USE_OZONE) |
48 | 50 |
49 // Helper for Shader creation. | 51 // Helper for Shader creation. |
50 static void CreateShader(GLuint program, | 52 static void CreateShader(GLuint program, |
51 GLenum type, | 53 GLenum type, |
(...skipping 11 matching lines...) Expand all Loading... | |
63 } | 65 } |
64 glAttachShader(program, shader); | 66 glAttachShader(program, shader); |
65 glDeleteShader(shader); | 67 glDeleteShader(shader); |
66 CHECK_EQ(static_cast<int>(glGetError()), GL_NO_ERROR); | 68 CHECK_EQ(static_cast<int>(glGetError()), GL_NO_ERROR); |
67 } | 69 } |
68 | 70 |
69 namespace content { | 71 namespace content { |
70 | 72 |
71 #if defined(USE_OZONE) | 73 #if defined(USE_OZONE) |
72 | 74 |
75 class DisplayConfiguratorObserver : public ui::DisplayConfigurator::Observer { | |
76 public: | |
77 DisplayConfiguratorObserver(base::RunLoop* loop) : loop_(loop) {} | |
78 ~DisplayConfiguratorObserver() override {} | |
79 | |
80 private: | |
81 // ui::DisplayConfigurator::Observer overrides: | |
82 void OnDisplayModeChanged( | |
83 const ui::DisplayConfigurator::DisplayStateList& outputs) override { | |
84 if (!loop_) | |
85 return; | |
86 loop_->Quit(); | |
87 loop_ = nullptr; | |
88 } | |
89 void OnDisplayModeChangeFailed( | |
90 ui::MultipleDisplayState failed_new_state) override { | |
91 LOG(FATAL) << "Could not configure display"; | |
92 } | |
93 | |
94 base::RunLoop* loop_; | |
95 | |
96 DISALLOW_COPY_AND_ASSIGN(DisplayConfiguratorObserver); | |
97 }; | |
98 | |
73 class RenderingHelper::StubOzoneDelegate : public ui::PlatformWindowDelegate { | 99 class RenderingHelper::StubOzoneDelegate : public ui::PlatformWindowDelegate { |
74 public: | 100 public: |
75 StubOzoneDelegate() : accelerated_widget_(gfx::kNullAcceleratedWidget) { | 101 StubOzoneDelegate() : accelerated_widget_(gfx::kNullAcceleratedWidget) { |
76 ui_thread_gpu_.Initialize(); | 102 ui_thread_gpu_.Initialize(); |
77 platform_window_ = ui::OzonePlatform::GetInstance()->CreatePlatformWindow( | 103 platform_window_ = ui::OzonePlatform::GetInstance()->CreatePlatformWindow( |
78 this, gfx::Rect()); | 104 this, gfx::Rect()); |
79 } | 105 } |
80 virtual ~StubOzoneDelegate() {} | 106 virtual ~StubOzoneDelegate() {} |
81 | 107 |
82 void OnBoundsChanged(const gfx::Rect& new_bounds) override {} | 108 void OnBoundsChanged(const gfx::Rect& new_bounds) override {} |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
136 } | 162 } |
137 | 163 |
138 RenderingHelper::RenderedVideo::RenderedVideo() | 164 RenderingHelper::RenderedVideo::RenderedVideo() |
139 : is_flushing(false), frames_to_drop(0) { | 165 : is_flushing(false), frames_to_drop(0) { |
140 } | 166 } |
141 | 167 |
142 RenderingHelper::RenderedVideo::~RenderedVideo() { | 168 RenderingHelper::RenderedVideo::~RenderedVideo() { |
143 } | 169 } |
144 | 170 |
145 // static | 171 // static |
146 bool RenderingHelper::InitializeOneOff() { | 172 void RenderingHelper::InitializeOneOff(base::WaitableEvent* done) { |
147 base::CommandLine* cmd_line = base::CommandLine::ForCurrentProcess(); | 173 base::CommandLine* cmd_line = base::CommandLine::ForCurrentProcess(); |
148 #if GL_VARIANT_GLX | 174 #if GL_VARIANT_GLX |
149 cmd_line->AppendSwitchASCII(switches::kUseGL, | 175 cmd_line->AppendSwitchASCII(switches::kUseGL, |
150 gfx::kGLImplementationDesktopName); | 176 gfx::kGLImplementationDesktopName); |
151 #else | 177 #else |
152 cmd_line->AppendSwitchASCII(switches::kUseGL, gfx::kGLImplementationEGLName); | 178 cmd_line->AppendSwitchASCII(switches::kUseGL, gfx::kGLImplementationEGLName); |
153 #endif | 179 #endif |
154 #if defined(USE_OZONE) | 180 |
155 ui::OzonePlatform::InitializeForUI(); | 181 if (!gfx::GLSurface::InitializeOneOff()) |
156 #endif | 182 LOG(FATAL) << "Could not initialize GL"; |
157 return gfx::GLSurface::InitializeOneOff(); | 183 done->Signal(); |
158 } | 184 } |
159 | 185 |
160 RenderingHelper::RenderingHelper() { | 186 RenderingHelper::RenderingHelper() { |
161 window_ = gfx::kNullAcceleratedWidget; | 187 window_ = gfx::kNullAcceleratedWidget; |
162 Clear(); | 188 Clear(); |
163 } | 189 } |
164 | 190 |
165 RenderingHelper::~RenderingHelper() { | 191 RenderingHelper::~RenderingHelper() { |
166 CHECK_EQ(videos_.size(), 0U) << "Must call UnInitialize before dtor."; | 192 CHECK_EQ(videos_.size(), 0U) << "Must call UnInitialize before dtor."; |
167 Clear(); | 193 Clear(); |
168 } | 194 } |
169 | 195 |
170 void RenderingHelper::Initialize(const RenderingHelperParams& params, | 196 void RenderingHelper::Setup() { |
171 base::WaitableEvent* done) { | |
172 // Use videos_.size() != 0 as a proxy for the class having already been | |
173 // Initialize()'d, and UnInitialize() before continuing. | |
174 if (videos_.size()) { | |
175 base::WaitableEvent done(false, false); | |
176 UnInitialize(&done); | |
177 done.Wait(); | |
178 } | |
179 | |
180 render_task_.Reset( | |
181 base::Bind(&RenderingHelper::RenderContent, base::Unretained(this))); | |
182 | |
183 frame_duration_ = params.rendering_fps > 0 | |
184 ? base::TimeDelta::FromSeconds(1) / params.rendering_fps | |
185 : base::TimeDelta(); | |
186 | |
187 render_as_thumbnails_ = params.render_as_thumbnails; | |
188 message_loop_ = base::MessageLoop::current(); | |
189 | |
190 #if defined(OS_WIN) | 197 #if defined(OS_WIN) |
191 window_ = CreateWindowEx(0, | 198 window_ = CreateWindowEx(0, |
192 L"Static", | 199 L"Static", |
193 L"VideoDecodeAcceleratorTest", | 200 L"VideoDecodeAcceleratorTest", |
194 WS_OVERLAPPEDWINDOW | WS_VISIBLE, | 201 WS_OVERLAPPEDWINDOW | WS_VISIBLE, |
195 0, | 202 0, |
196 0, | 203 0, |
197 GetSystemMetrics(SM_CXSCREEN), | 204 GetSystemMetrics(SM_CXSCREEN), |
198 GetSystemMetrics(SM_CYSCREEN), | 205 GetSystemMetrics(SM_CYSCREEN), |
199 NULL, | 206 NULL, |
(...skipping 22 matching lines...) Expand all Loading... | |
222 0 /* border width */, | 229 0 /* border width */, |
223 depth, | 230 depth, |
224 CopyFromParent /* class */, | 231 CopyFromParent /* class */, |
225 CopyFromParent /* visual */, | 232 CopyFromParent /* visual */, |
226 (CWBackPixel | CWOverrideRedirect), | 233 (CWBackPixel | CWOverrideRedirect), |
227 &window_attributes); | 234 &window_attributes); |
228 XStoreName(display, window_, "VideoDecodeAcceleratorTest"); | 235 XStoreName(display, window_, "VideoDecodeAcceleratorTest"); |
229 XSelectInput(display, window_, ExposureMask); | 236 XSelectInput(display, window_, ExposureMask); |
230 XMapWindow(display, window_); | 237 XMapWindow(display, window_); |
231 #elif defined(USE_OZONE) | 238 #elif defined(USE_OZONE) |
239 base::MessageLoop::ScopedNestableTaskAllower nest_loop( | |
240 base::MessageLoop::current()); | |
241 base::RunLoop wait_window_resize; | |
242 | |
232 platform_window_delegate_.reset(new RenderingHelper::StubOzoneDelegate()); | 243 platform_window_delegate_.reset(new RenderingHelper::StubOzoneDelegate()); |
233 window_ = platform_window_delegate_->accelerated_widget(); | 244 window_ = platform_window_delegate_->accelerated_widget(); |
234 #if defined(OS_CHROMEOS) | 245 #if defined(OS_CHROMEOS) |
246 // We hold onto the main loop here to wait for the DisplayController | |
247 // to give us the size of the display so we can create a window of | |
248 // the same size. | |
249 base::RunLoop wait_display_setup; | |
250 DisplayConfiguratorObserver display_setup_observer(&wait_display_setup); | |
235 display_configurator_.reset(new ui::DisplayConfigurator()); | 251 display_configurator_.reset(new ui::DisplayConfigurator()); |
252 display_configurator_->AddObserver(&display_setup_observer); | |
236 display_configurator_->Init(true); | 253 display_configurator_->Init(true); |
237 display_configurator_->ForceInitialConfigure(0); | 254 display_configurator_->ForceInitialConfigure(0); |
255 // Make sure all the display configuration is applied. | |
256 wait_display_setup.Run(); | |
257 display_configurator_->RemoveObserver(&display_setup_observer); | |
258 | |
259 base::RunLoop wait_display_single; | |
Owen Lin
2015/01/26 07:25:29
Are you going to removing this part ?
llandwerlin-old
2015/01/26 11:35:48
Done.
| |
260 DisplayConfiguratorObserver display_single_observer(&wait_display_single); | |
261 display_configurator_->AddObserver(&display_single_observer); | |
262 display_configurator_->SetDisplayMode(ui::MULTIPLE_DISPLAY_STATE_SINGLE); | |
263 wait_display_single.Run(); | |
264 display_configurator_->RemoveObserver(&display_single_observer); | |
265 | |
238 platform_window_delegate_->platform_window()->SetBounds( | 266 platform_window_delegate_->platform_window()->SetBounds( |
239 gfx::Rect(display_configurator_->framebuffer_size())); | 267 gfx::Rect(display_configurator_->framebuffer_size())); |
240 #else | 268 #else |
241 platform_window_delegate_->platform_window()->SetBounds(gfx::Rect(800, 600)); | 269 platform_window_delegate_->platform_window()->SetBounds(gfx::Rect(800, 600)); |
242 #endif | 270 #endif |
271 wait_window_resize.RunUntilIdle(); | |
Owen Lin
2015/01/26 07:25:29
Please add comment to explain what this line is ab
llandwerlin-old
2015/01/26 11:35:48
Done.
| |
243 #else | 272 #else |
244 #error unknown platform | 273 #error unknown platform |
245 #endif | 274 #endif |
246 CHECK(window_ != gfx::kNullAcceleratedWidget); | 275 CHECK(window_ != gfx::kNullAcceleratedWidget); |
276 } | |
277 | |
278 void RenderingHelper::TearDown() { | |
279 #if defined(OS_WIN) | |
280 if (window_) | |
281 DestroyWindow(window_); | |
282 #elif defined(USE_X11) | |
283 // Destroy resources acquired in Initialize, in reverse-acquisition order. | |
284 if (window_) { | |
285 CHECK(XUnmapWindow(gfx::GetXDisplay(), window_)); | |
286 CHECK(XDestroyWindow(gfx::GetXDisplay(), window_)); | |
287 } | |
288 #elif defined(USE_OZONE) | |
289 platform_window_delegate_.reset(); | |
290 #if defined(OS_CHROMEOS) | |
291 display_configurator_->PrepareForExit(); | |
292 display_configurator_.reset(); | |
293 #endif | |
294 #endif | |
295 window_ = gfx::kNullAcceleratedWidget; | |
296 } | |
297 | |
298 void RenderingHelper::Initialize(const RenderingHelperParams& params, | |
299 base::WaitableEvent* done) { | |
300 // Use videos_.size() != 0 as a proxy for the class having already been | |
301 // Initialize()'d, and UnInitialize() before continuing. | |
302 if (videos_.size()) { | |
303 base::WaitableEvent done(false, false); | |
304 UnInitialize(&done); | |
305 done.Wait(); | |
306 } | |
307 | |
308 render_task_.Reset( | |
309 base::Bind(&RenderingHelper::RenderContent, base::Unretained(this))); | |
310 | |
311 frame_duration_ = params.rendering_fps > 0 | |
312 ? base::TimeDelta::FromSeconds(1) / params.rendering_fps | |
313 : base::TimeDelta(); | |
314 | |
315 render_as_thumbnails_ = params.render_as_thumbnails; | |
316 message_loop_ = base::MessageLoop::current(); | |
247 | 317 |
248 gl_surface_ = gfx::GLSurface::CreateViewGLSurface(window_); | 318 gl_surface_ = gfx::GLSurface::CreateViewGLSurface(window_); |
249 screen_size_ = gl_surface_->GetSize(); | 319 screen_size_ = gl_surface_->GetSize(); |
250 | 320 |
251 gl_context_ = gfx::GLContext::CreateGLContext( | 321 gl_context_ = gfx::GLContext::CreateGLContext( |
252 NULL, gl_surface_.get(), gfx::PreferIntegratedGpu); | 322 NULL, gl_surface_.get(), gfx::PreferIntegratedGpu); |
253 CHECK(gl_context_->MakeCurrent(gl_surface_.get())); | 323 CHECK(gl_context_->MakeCurrent(gl_surface_.get())); |
254 | 324 |
255 CHECK_GT(params.window_sizes.size(), 0U); | 325 CHECK_GT(params.window_sizes.size(), 0U); |
256 videos_.resize(params.window_sizes.size()); | 326 videos_.resize(params.window_sizes.size()); |
(...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
422 for (int i = 0; i < warm_up_iterations; ++i) { | 492 for (int i = 0; i < warm_up_iterations; ++i) { |
423 RenderTexture(GL_TEXTURE_2D, texture_id); | 493 RenderTexture(GL_TEXTURE_2D, texture_id); |
424 gl_surface_->SwapBuffers(); | 494 gl_surface_->SwapBuffers(); |
425 } | 495 } |
426 glDeleteTextures(1, &texture_id); | 496 glDeleteTextures(1, &texture_id); |
427 } | 497 } |
428 | 498 |
429 void RenderingHelper::UnInitialize(base::WaitableEvent* done) { | 499 void RenderingHelper::UnInitialize(base::WaitableEvent* done) { |
430 CHECK_EQ(base::MessageLoop::current(), message_loop_); | 500 CHECK_EQ(base::MessageLoop::current(), message_loop_); |
431 | 501 |
432 #if defined(USE_OZONE) && defined(OS_CHROMEOS) | |
433 display_configurator_->PrepareForExit(); | |
434 display_configurator_.reset(); | |
435 #endif | |
436 | |
437 render_task_.Cancel(); | 502 render_task_.Cancel(); |
438 | 503 |
439 if (render_as_thumbnails_) { | 504 if (render_as_thumbnails_) { |
440 glDeleteTextures(1, &thumbnails_texture_id_); | 505 glDeleteTextures(1, &thumbnails_texture_id_); |
441 glDeleteFramebuffersEXT(1, &thumbnails_fbo_id_); | 506 glDeleteFramebuffersEXT(1, &thumbnails_fbo_id_); |
442 } | 507 } |
443 | 508 |
444 gl_context_->ReleaseCurrent(gl_surface_.get()); | 509 gl_context_->ReleaseCurrent(gl_surface_.get()); |
445 gl_context_ = NULL; | 510 gl_context_ = NULL; |
446 gl_surface_ = NULL; | 511 gl_surface_ = NULL; |
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
571 void RenderingHelper::Clear() { | 636 void RenderingHelper::Clear() { |
572 videos_.clear(); | 637 videos_.clear(); |
573 message_loop_ = NULL; | 638 message_loop_ = NULL; |
574 gl_context_ = NULL; | 639 gl_context_ = NULL; |
575 gl_surface_ = NULL; | 640 gl_surface_ = NULL; |
576 | 641 |
577 render_as_thumbnails_ = false; | 642 render_as_thumbnails_ = false; |
578 frame_count_ = 0; | 643 frame_count_ = 0; |
579 thumbnails_fbo_id_ = 0; | 644 thumbnails_fbo_id_ = 0; |
580 thumbnails_texture_id_ = 0; | 645 thumbnails_texture_id_ = 0; |
581 | |
582 #if defined(OS_WIN) | |
583 if (window_) | |
584 DestroyWindow(window_); | |
585 #elif defined(USE_X11) | |
586 // Destroy resources acquired in Initialize, in reverse-acquisition order. | |
587 if (window_) { | |
588 CHECK(XUnmapWindow(gfx::GetXDisplay(), window_)); | |
589 CHECK(XDestroyWindow(gfx::GetXDisplay(), window_)); | |
590 } | |
591 #elif defined(USE_OZONE) | |
592 platform_window_delegate_.reset(); | |
593 #endif | |
594 window_ = gfx::kNullAcceleratedWidget; | |
595 } | 646 } |
596 | 647 |
597 void RenderingHelper::GetThumbnailsAsRGB(std::vector<unsigned char>* rgb, | 648 void RenderingHelper::GetThumbnailsAsRGB(std::vector<unsigned char>* rgb, |
598 bool* alpha_solid, | 649 bool* alpha_solid, |
599 base::WaitableEvent* done) { | 650 base::WaitableEvent* done) { |
600 CHECK(render_as_thumbnails_); | 651 CHECK(render_as_thumbnails_); |
601 | 652 |
602 const size_t num_pixels = thumbnails_fbo_size_.GetArea(); | 653 const size_t num_pixels = thumbnails_fbo_size_.GetArea(); |
603 std::vector<unsigned char> rgba; | 654 std::vector<unsigned char> rgba; |
604 rgba.resize(num_pixels * 4); | 655 rgba.resize(num_pixels * 4); |
(...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
782 // When the rendering falls behind, drops frames. | 833 // When the rendering falls behind, drops frames. |
783 while (scheduled_render_time_ < target) { | 834 while (scheduled_render_time_ < target) { |
784 scheduled_render_time_ += frame_duration_; | 835 scheduled_render_time_ += frame_duration_; |
785 DropOneFrameForAllVideos(); | 836 DropOneFrameForAllVideos(); |
786 } | 837 } |
787 | 838 |
788 message_loop_->PostDelayedTask( | 839 message_loop_->PostDelayedTask( |
789 FROM_HERE, render_task_.callback(), target - now); | 840 FROM_HERE, render_task_.callback(), target - now); |
790 } | 841 } |
791 } // namespace content | 842 } // namespace content |
OLD | NEW |