| 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/strings/stringize_macros.h" | 16 #include "base/strings/stringize_macros.h" |
| 17 #include "base/synchronization/waitable_event.h" | 17 #include "base/synchronization/waitable_event.h" |
| 18 #include "base/time/time.h" | 18 #include "base/time/time.h" |
| 19 #include "ui/gl/gl_context.h" | 19 #include "ui/gl/gl_context.h" |
| 20 #include "ui/gl/gl_implementation.h" | 20 #include "ui/gl/gl_implementation.h" |
| 21 #include "ui/gl/gl_surface.h" | 21 #include "ui/gl/gl_surface.h" |
| 22 #include "ui/gl/gl_surface_egl.h" | |
| 23 #include "ui/gl/gl_surface_glx.h" | |
| 24 | 22 |
| 25 #if defined(OS_WIN) | 23 #if defined(OS_WIN) |
| 26 #include <windows.h> | 24 #include <windows.h> |
| 27 #endif | 25 #endif |
| 28 | 26 |
| 29 #if defined(USE_X11) | 27 #if defined(USE_X11) |
| 30 #include "ui/gfx/x/x11_types.h" | 28 #include "ui/gfx/x/x11_types.h" |
| 31 #endif | 29 #endif |
| 32 | 30 |
| 33 #if !defined(OS_WIN) && defined(ARCH_CPU_X86_FAMILY) | 31 #if defined(ARCH_CPU_X86_FAMILY) && defined(USE_X11) |
| 32 #include "ui/gl/gl_surface_glx.h" |
| 34 #define GL_VARIANT_GLX 1 | 33 #define GL_VARIANT_GLX 1 |
| 35 #else | 34 #else |
| 35 #include "ui/gl/gl_surface_egl.h" |
| 36 #define GL_VARIANT_EGL 1 | 36 #define GL_VARIANT_EGL 1 |
| 37 #endif | 37 #endif |
| 38 | 38 |
| 39 // Helper for Shader creation. | 39 // Helper for Shader creation. |
| 40 static void CreateShader(GLuint program, | 40 static void CreateShader(GLuint program, |
| 41 GLenum type, | 41 GLenum type, |
| 42 const char* source, | 42 const char* source, |
| 43 int size) { | 43 int size) { |
| 44 GLuint shader = glCreateShader(type); | 44 GLuint shader = glCreateShader(type); |
| 45 glShaderSource(shader, 1, &source, &size); | 45 glShaderSource(shader, 1, &source, &size); |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 120 base::Bind(&RenderingHelper::RenderContent, base::Unretained(this))); | 120 base::Bind(&RenderingHelper::RenderContent, base::Unretained(this))); |
| 121 | 121 |
| 122 frame_duration_ = params.rendering_fps > 0 | 122 frame_duration_ = params.rendering_fps > 0 |
| 123 ? base::TimeDelta::FromSeconds(1) / params.rendering_fps | 123 ? base::TimeDelta::FromSeconds(1) / params.rendering_fps |
| 124 : base::TimeDelta(); | 124 : base::TimeDelta(); |
| 125 | 125 |
| 126 render_as_thumbnails_ = params.render_as_thumbnails; | 126 render_as_thumbnails_ = params.render_as_thumbnails; |
| 127 message_loop_ = base::MessageLoop::current(); | 127 message_loop_ = base::MessageLoop::current(); |
| 128 | 128 |
| 129 #if defined(OS_WIN) | 129 #if defined(OS_WIN) |
| 130 screen_size_ = | |
| 131 gfx::Size(GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN)); | |
| 132 window_ = CreateWindowEx(0, | 130 window_ = CreateWindowEx(0, |
| 133 L"Static", | 131 L"Static", |
| 134 L"VideoDecodeAcceleratorTest", | 132 L"VideoDecodeAcceleratorTest", |
| 135 WS_OVERLAPPEDWINDOW | WS_VISIBLE, | 133 WS_OVERLAPPEDWINDOW | WS_VISIBLE, |
| 136 0, | 134 0, |
| 137 0, | 135 0, |
| 138 screen_size_.width(), | 136 GetSystemMetrics(SM_CXSCREEN), |
| 139 screen_size_.height(), | 137 GetSystemMetrics(SM_CYSCREEN), |
| 140 NULL, | 138 NULL, |
| 141 NULL, | 139 NULL, |
| 142 NULL, | 140 NULL, |
| 143 NULL); | 141 NULL); |
| 144 #elif defined(USE_X11) | 142 #elif defined(USE_X11) |
| 145 Display* display = gfx::GetXDisplay(); | 143 Display* display = gfx::GetXDisplay(); |
| 146 Screen* screen = DefaultScreenOfDisplay(display); | 144 Screen* screen = DefaultScreenOfDisplay(display); |
| 147 screen_size_ = gfx::Size(XWidthOfScreen(screen), XHeightOfScreen(screen)); | |
| 148 | 145 |
| 149 CHECK(display); | 146 CHECK(display); |
| 150 | 147 |
| 151 XSetWindowAttributes window_attributes; | 148 XSetWindowAttributes window_attributes; |
| 152 memset(&window_attributes, 0, sizeof(window_attributes)); | 149 memset(&window_attributes, 0, sizeof(window_attributes)); |
| 153 window_attributes.background_pixel = | 150 window_attributes.background_pixel = |
| 154 BlackPixel(display, DefaultScreen(display)); | 151 BlackPixel(display, DefaultScreen(display)); |
| 155 window_attributes.override_redirect = true; | 152 window_attributes.override_redirect = true; |
| 156 int depth = DefaultDepth(display, DefaultScreen(display)); | 153 int depth = DefaultDepth(display, DefaultScreen(display)); |
| 157 | 154 |
| 158 window_ = XCreateWindow(display, | 155 window_ = XCreateWindow(display, |
| 159 DefaultRootWindow(display), | 156 DefaultRootWindow(display), |
| 160 0, | 157 0, |
| 161 0, | 158 0, |
| 162 screen_size_.width(), | 159 XWidthOfScreen(screen), |
| 163 screen_size_.height(), | 160 XHeightOfScreen(screen), |
| 164 0 /* border width */, | 161 0 /* border width */, |
| 165 depth, | 162 depth, |
| 166 CopyFromParent /* class */, | 163 CopyFromParent /* class */, |
| 167 CopyFromParent /* visual */, | 164 CopyFromParent /* visual */, |
| 168 (CWBackPixel | CWOverrideRedirect), | 165 (CWBackPixel | CWOverrideRedirect), |
| 169 &window_attributes); | 166 &window_attributes); |
| 170 XStoreName(display, window_, "VideoDecodeAcceleratorTest"); | 167 XStoreName(display, window_, "VideoDecodeAcceleratorTest"); |
| 171 XSelectInput(display, window_, ExposureMask); | 168 XSelectInput(display, window_, ExposureMask); |
| 172 XMapWindow(display, window_); | 169 XMapWindow(display, window_); |
| 173 #else | 170 #else |
| 174 #error unknown platform | 171 #error unknown platform |
| 175 #endif | 172 #endif |
| 176 CHECK(window_ != gfx::kNullAcceleratedWidget); | 173 CHECK(window_ != gfx::kNullAcceleratedWidget); |
| 177 | 174 |
| 178 gl_surface_ = gfx::GLSurface::CreateViewGLSurface(window_); | 175 gl_surface_ = gfx::GLSurface::CreateViewGLSurface(window_); |
| 176 screen_size_ = gl_surface_->GetSize(); |
| 177 |
| 179 gl_context_ = gfx::GLContext::CreateGLContext( | 178 gl_context_ = gfx::GLContext::CreateGLContext( |
| 180 NULL, gl_surface_.get(), gfx::PreferIntegratedGpu); | 179 NULL, gl_surface_.get(), gfx::PreferIntegratedGpu); |
| 181 gl_context_->MakeCurrent(gl_surface_.get()); | 180 CHECK(gl_context_->MakeCurrent(gl_surface_.get())); |
| 182 | 181 |
| 183 CHECK_GT(params.window_sizes.size(), 0U); | 182 CHECK_GT(params.window_sizes.size(), 0U); |
| 184 videos_.resize(params.window_sizes.size()); | 183 videos_.resize(params.window_sizes.size()); |
| 185 LayoutRenderingAreas(params.window_sizes); | 184 LayoutRenderingAreas(params.window_sizes); |
| 186 | 185 |
| 187 if (render_as_thumbnails_) { | 186 if (render_as_thumbnails_) { |
| 188 CHECK_EQ(videos_.size(), 1U); | 187 CHECK_EQ(videos_.size(), 1U); |
| 189 | 188 |
| 190 GLint max_texture_size; | 189 GLint max_texture_size; |
| 191 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &max_texture_size); | 190 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &max_texture_size); |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 297 if (tex_external != -1) { | 296 if (tex_external != -1) { |
| 298 glUniform1i(tex_external, 1); | 297 glUniform1i(tex_external, 1); |
| 299 } | 298 } |
| 300 int pos_location = glGetAttribLocation(program_, "in_pos"); | 299 int pos_location = glGetAttribLocation(program_, "in_pos"); |
| 301 glEnableVertexAttribArray(pos_location); | 300 glEnableVertexAttribArray(pos_location); |
| 302 glVertexAttribPointer(pos_location, 2, GL_FLOAT, GL_FALSE, 0, kVertices); | 301 glVertexAttribPointer(pos_location, 2, GL_FLOAT, GL_FALSE, 0, kVertices); |
| 303 int tc_location = glGetAttribLocation(program_, "in_tc"); | 302 int tc_location = glGetAttribLocation(program_, "in_tc"); |
| 304 glEnableVertexAttribArray(tc_location); | 303 glEnableVertexAttribArray(tc_location); |
| 305 glVertexAttribPointer(tc_location, 2, GL_FLOAT, GL_FALSE, 0, kTextureCoords); | 304 glVertexAttribPointer(tc_location, 2, GL_FLOAT, GL_FALSE, 0, kTextureCoords); |
| 306 | 305 |
| 307 if (frame_duration_ != base::TimeDelta()) | 306 if (frame_duration_ != base::TimeDelta()) { |
| 308 WarmUpRendering(params.warm_up_iterations); | 307 int warm_up_iterations = params.warm_up_iterations; |
| 308 WarmUpRendering(warm_up_iterations); |
| 309 } |
| 309 | 310 |
| 310 // It's safe to use Unretained here since |rendering_thread_| will be stopped | 311 // It's safe to use Unretained here since |rendering_thread_| will be stopped |
| 311 // in VideoDecodeAcceleratorTest.TearDown(), while the |rendering_helper_| is | 312 // in VideoDecodeAcceleratorTest.TearDown(), while the |rendering_helper_| is |
| 312 // a member of that class. (See video_decode_accelerator_unittest.cc.) | 313 // a member of that class. (See video_decode_accelerator_unittest.cc.) |
| 313 gfx::VSyncProvider* vsync_provider = gl_surface_->GetVSyncProvider(); | 314 gfx::VSyncProvider* vsync_provider = gl_surface_->GetVSyncProvider(); |
| 314 if (vsync_provider) | 315 if (vsync_provider && frame_duration_ != base::TimeDelta()) |
| 315 vsync_provider->GetVSyncParameters(base::Bind( | 316 vsync_provider->GetVSyncParameters(base::Bind( |
| 316 &RenderingHelper::UpdateVSyncParameters, base::Unretained(this), done)); | 317 &RenderingHelper::UpdateVSyncParameters, base::Unretained(this), done)); |
| 317 else | 318 else |
| 318 done->Signal(); | 319 done->Signal(); |
| 319 } | 320 } |
| 320 | 321 |
| 321 // The rendering for the first few frames is slow (e.g., 100ms on Peach Pit). | 322 // The rendering for the first few frames is slow (e.g., 100ms on Peach Pit). |
| 322 // This affects the numbers measured in the performance test. We try to render | 323 // This affects the numbers measured in the performance test. We try to render |
| 323 // several frames here to warm up the rendering. | 324 // several frames here to warm up the rendering. |
| 324 void RenderingHelper::WarmUpRendering(int warm_up_iterations) { | 325 void RenderingHelper::WarmUpRendering(int warm_up_iterations) { |
| 325 unsigned int texture_id; | 326 unsigned int texture_id; |
| 326 scoped_ptr<GLubyte[]> emptyData(new GLubyte[screen_size_.GetArea() * 2]); | 327 scoped_ptr<GLubyte[]> emptyData(new GLubyte[screen_size_.GetArea() * 2]()); |
| 327 glGenTextures(1, &texture_id); | 328 glGenTextures(1, &texture_id); |
| 328 glBindTexture(GL_TEXTURE_2D, texture_id); | 329 glBindTexture(GL_TEXTURE_2D, texture_id); |
| 329 glTexImage2D(GL_TEXTURE_2D, | 330 glTexImage2D(GL_TEXTURE_2D, |
| 330 0, | 331 0, |
| 331 GL_RGB, | 332 GL_RGB, |
| 332 screen_size_.width(), | 333 screen_size_.width(), |
| 333 screen_size_.height(), | 334 screen_size_.height(), |
| 334 0, | 335 0, |
| 335 GL_RGB, | 336 GL_RGB, |
| 336 GL_UNSIGNED_SHORT_5_6_5, | 337 GL_UNSIGNED_SHORT_5_6_5, |
| (...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 460 glBindTexture(texture_target, 0); | 461 glBindTexture(texture_target, 0); |
| 461 CHECK_EQ(static_cast<int>(glGetError()), GL_NO_ERROR); | 462 CHECK_EQ(static_cast<int>(glGetError()), GL_NO_ERROR); |
| 462 } | 463 } |
| 463 | 464 |
| 464 void RenderingHelper::DeleteTexture(uint32 texture_id) { | 465 void RenderingHelper::DeleteTexture(uint32 texture_id) { |
| 465 CHECK_EQ(base::MessageLoop::current(), message_loop_); | 466 CHECK_EQ(base::MessageLoop::current(), message_loop_); |
| 466 glDeleteTextures(1, &texture_id); | 467 glDeleteTextures(1, &texture_id); |
| 467 CHECK_EQ(static_cast<int>(glGetError()), GL_NO_ERROR); | 468 CHECK_EQ(static_cast<int>(glGetError()), GL_NO_ERROR); |
| 468 } | 469 } |
| 469 | 470 |
| 470 void* RenderingHelper::GetGLContext() { | 471 scoped_refptr<gfx::GLContext> RenderingHelper::GetGLContext() { |
| 472 return gl_context_; |
| 473 } |
| 474 |
| 475 void* RenderingHelper::GetGLContextHandle() { |
| 471 return gl_context_->GetHandle(); | 476 return gl_context_->GetHandle(); |
| 472 } | 477 } |
| 473 | 478 |
| 474 void* RenderingHelper::GetGLDisplay() { | 479 void* RenderingHelper::GetGLDisplay() { |
| 475 return gl_surface_->GetDisplay(); | 480 return gl_surface_->GetDisplay(); |
| 476 } | 481 } |
| 477 | 482 |
| 478 void RenderingHelper::Clear() { | 483 void RenderingHelper::Clear() { |
| 479 videos_.clear(); | 484 videos_.clear(); |
| 480 message_loop_ = NULL; | 485 message_loop_ = NULL; |
| 481 gl_context_ = NULL; | 486 gl_context_ = NULL; |
| 482 gl_surface_ = NULL; | 487 gl_surface_ = NULL; |
| 483 | 488 |
| 484 render_as_thumbnails_ = false; | 489 render_as_thumbnails_ = false; |
| 485 frame_count_ = 0; | 490 frame_count_ = 0; |
| 486 thumbnails_fbo_id_ = 0; | 491 thumbnails_fbo_id_ = 0; |
| 487 thumbnails_texture_id_ = 0; | 492 thumbnails_texture_id_ = 0; |
| 488 | 493 |
| 489 #if defined(OS_WIN) | 494 #if defined(OS_WIN) |
| 490 if (window_) | 495 if (window_) |
| 491 DestroyWindow(window_); | 496 DestroyWindow(window_); |
| 492 #else | 497 #elif defined(USE_X11) |
| 493 // Destroy resources acquired in Initialize, in reverse-acquisition order. | 498 // Destroy resources acquired in Initialize, in reverse-acquisition order. |
| 494 if (window_) { | 499 if (window_) { |
| 495 CHECK(XUnmapWindow(gfx::GetXDisplay(), window_)); | 500 CHECK(XUnmapWindow(gfx::GetXDisplay(), window_)); |
| 496 CHECK(XDestroyWindow(gfx::GetXDisplay(), window_)); | 501 CHECK(XDestroyWindow(gfx::GetXDisplay(), window_)); |
| 497 } | 502 } |
| 498 #endif | 503 #endif |
| 499 window_ = gfx::kNullAcceleratedWidget; | 504 window_ = gfx::kNullAcceleratedWidget; |
| 500 } | 505 } |
| 501 | 506 |
| 502 void RenderingHelper::GetThumbnailsAsRGB(std::vector<unsigned char>* rgb, | 507 void RenderingHelper::GetThumbnailsAsRGB(std::vector<unsigned char>* rgb, |
| (...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 687 // When the rendering falls behind, drops frames. | 692 // When the rendering falls behind, drops frames. |
| 688 while (scheduled_render_time_ < target) { | 693 while (scheduled_render_time_ < target) { |
| 689 scheduled_render_time_ += frame_duration_; | 694 scheduled_render_time_ += frame_duration_; |
| 690 DropOneFrameForAllVideos(); | 695 DropOneFrameForAllVideos(); |
| 691 } | 696 } |
| 692 | 697 |
| 693 message_loop_->PostDelayedTask( | 698 message_loop_->PostDelayedTask( |
| 694 FROM_HERE, render_task_.callback(), target - now); | 699 FROM_HERE, render_task_.callback(), target - now); |
| 695 } | 700 } |
| 696 } // namespace content | 701 } // namespace content |
| OLD | NEW |