| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "content/common/gpu/media/rendering_helper.h" | |
| 6 | |
| 7 #include <string.h> | |
| 8 | |
| 9 #include <algorithm> | |
| 10 #include <memory> | |
| 11 #include <numeric> | |
| 12 #include <vector> | |
| 13 | |
| 14 #include "base/bind.h" | |
| 15 #include "base/callback_helpers.h" | |
| 16 #include "base/command_line.h" | |
| 17 #include "base/mac/scoped_nsautorelease_pool.h" | |
| 18 #include "base/macros.h" | |
| 19 #include "base/message_loop/message_loop.h" | |
| 20 #include "base/run_loop.h" | |
| 21 #include "base/strings/stringize_macros.h" | |
| 22 #include "base/synchronization/waitable_event.h" | |
| 23 #include "base/time/time.h" | |
| 24 #include "build/build_config.h" | |
| 25 #include "ui/gl/gl_context.h" | |
| 26 #include "ui/gl/gl_implementation.h" | |
| 27 #include "ui/gl/gl_surface.h" | |
| 28 | |
| 29 #if defined(OS_WIN) | |
| 30 #include <windows.h> | |
| 31 #endif | |
| 32 | |
| 33 #if defined(USE_X11) | |
| 34 #include "ui/gfx/x/x11_types.h" | |
| 35 #endif | |
| 36 | |
| 37 #if defined(ARCH_CPU_X86_FAMILY) && defined(USE_X11) | |
| 38 #include "ui/gl/gl_surface_glx.h" | |
| 39 #define GL_VARIANT_GLX 1 | |
| 40 #else | |
| 41 #include "ui/gl/gl_surface_egl.h" | |
| 42 #define GL_VARIANT_EGL 1 | |
| 43 #endif | |
| 44 | |
| 45 #if defined(USE_OZONE) | |
| 46 #if defined(OS_CHROMEOS) | |
| 47 #include "ui/display/chromeos/display_configurator.h" | |
| 48 #include "ui/display/types/native_display_delegate.h" | |
| 49 #endif // defined(OS_CHROMEOS) | |
| 50 #include "ui/ozone/public/ozone_platform.h" | |
| 51 #include "ui/platform_window/platform_window.h" | |
| 52 #include "ui/platform_window/platform_window_delegate.h" | |
| 53 #endif // defined(USE_OZONE) | |
| 54 | |
| 55 // Helper for Shader creation. | |
| 56 static void CreateShader(GLuint program, | |
| 57 GLenum type, | |
| 58 const char* source, | |
| 59 int size) { | |
| 60 GLuint shader = glCreateShader(type); | |
| 61 glShaderSource(shader, 1, &source, &size); | |
| 62 glCompileShader(shader); | |
| 63 int result = GL_FALSE; | |
| 64 glGetShaderiv(shader, GL_COMPILE_STATUS, &result); | |
| 65 if (!result) { | |
| 66 char log[4096]; | |
| 67 glGetShaderInfoLog(shader, arraysize(log), NULL, log); | |
| 68 LOG(FATAL) << log; | |
| 69 } | |
| 70 glAttachShader(program, shader); | |
| 71 glDeleteShader(shader); | |
| 72 CHECK_EQ(static_cast<int>(glGetError()), GL_NO_ERROR); | |
| 73 } | |
| 74 | |
| 75 namespace content { | |
| 76 namespace { | |
| 77 | |
| 78 void WaitForSwapAck(const base::Closure& callback, gfx::SwapResult result) { | |
| 79 callback.Run(); | |
| 80 } | |
| 81 | |
| 82 } // namespace | |
| 83 | |
| 84 #if defined(USE_OZONE) | |
| 85 | |
| 86 class DisplayConfiguratorObserver : public ui::DisplayConfigurator::Observer { | |
| 87 public: | |
| 88 explicit DisplayConfiguratorObserver(base::RunLoop* loop) : loop_(loop) {} | |
| 89 ~DisplayConfiguratorObserver() override {} | |
| 90 | |
| 91 private: | |
| 92 // ui::DisplayConfigurator::Observer overrides: | |
| 93 void OnDisplayModeChanged( | |
| 94 const ui::DisplayConfigurator::DisplayStateList& outputs) override { | |
| 95 if (!loop_) | |
| 96 return; | |
| 97 loop_->Quit(); | |
| 98 loop_ = nullptr; | |
| 99 } | |
| 100 void OnDisplayModeChangeFailed( | |
| 101 const ui::DisplayConfigurator::DisplayStateList& outputs, | |
| 102 ui::MultipleDisplayState failed_new_state) override { | |
| 103 LOG(FATAL) << "Could not configure display"; | |
| 104 } | |
| 105 | |
| 106 base::RunLoop* loop_; | |
| 107 | |
| 108 DISALLOW_COPY_AND_ASSIGN(DisplayConfiguratorObserver); | |
| 109 }; | |
| 110 | |
| 111 class RenderingHelper::StubOzoneDelegate : public ui::PlatformWindowDelegate { | |
| 112 public: | |
| 113 StubOzoneDelegate() : accelerated_widget_(gfx::kNullAcceleratedWidget) { | |
| 114 platform_window_ = ui::OzonePlatform::GetInstance()->CreatePlatformWindow( | |
| 115 this, gfx::Rect()); | |
| 116 } | |
| 117 ~StubOzoneDelegate() override {} | |
| 118 | |
| 119 void OnBoundsChanged(const gfx::Rect& new_bounds) override {} | |
| 120 | |
| 121 void OnDamageRect(const gfx::Rect& damaged_region) override {} | |
| 122 | |
| 123 void DispatchEvent(ui::Event* event) override {} | |
| 124 | |
| 125 void OnCloseRequest() override {} | |
| 126 void OnClosed() override {} | |
| 127 | |
| 128 void OnWindowStateChanged(ui::PlatformWindowState new_state) override {} | |
| 129 | |
| 130 void OnLostCapture() override {}; | |
| 131 | |
| 132 void OnAcceleratedWidgetAvailable(gfx::AcceleratedWidget widget, | |
| 133 float device_pixel_ratio) override { | |
| 134 accelerated_widget_ = widget; | |
| 135 } | |
| 136 | |
| 137 void OnAcceleratedWidgetDestroyed() override { | |
| 138 NOTREACHED(); | |
| 139 } | |
| 140 | |
| 141 void OnActivationChanged(bool active) override {}; | |
| 142 | |
| 143 gfx::AcceleratedWidget accelerated_widget() const { | |
| 144 return accelerated_widget_; | |
| 145 } | |
| 146 | |
| 147 gfx::Size GetSize() { return platform_window_->GetBounds().size(); } | |
| 148 | |
| 149 ui::PlatformWindow* platform_window() const { return platform_window_.get(); } | |
| 150 | |
| 151 private: | |
| 152 std::unique_ptr<ui::PlatformWindow> platform_window_; | |
| 153 gfx::AcceleratedWidget accelerated_widget_; | |
| 154 | |
| 155 DISALLOW_COPY_AND_ASSIGN(StubOzoneDelegate); | |
| 156 }; | |
| 157 | |
| 158 #endif // defined(USE_OZONE) | |
| 159 | |
| 160 RenderingHelperParams::RenderingHelperParams() | |
| 161 : rendering_fps(0), warm_up_iterations(0), render_as_thumbnails(false) { | |
| 162 } | |
| 163 | |
| 164 RenderingHelperParams::RenderingHelperParams( | |
| 165 const RenderingHelperParams& other) = default; | |
| 166 | |
| 167 RenderingHelperParams::~RenderingHelperParams() {} | |
| 168 | |
| 169 VideoFrameTexture::VideoFrameTexture(uint32_t texture_target, | |
| 170 uint32_t texture_id, | |
| 171 const base::Closure& no_longer_needed_cb) | |
| 172 : texture_target_(texture_target), | |
| 173 texture_id_(texture_id), | |
| 174 no_longer_needed_cb_(no_longer_needed_cb) { | |
| 175 DCHECK(!no_longer_needed_cb_.is_null()); | |
| 176 } | |
| 177 | |
| 178 VideoFrameTexture::~VideoFrameTexture() { | |
| 179 base::ResetAndReturn(&no_longer_needed_cb_).Run(); | |
| 180 } | |
| 181 | |
| 182 RenderingHelper::RenderedVideo::RenderedVideo() | |
| 183 : is_flushing(false), frames_to_drop(0) { | |
| 184 } | |
| 185 | |
| 186 RenderingHelper::RenderedVideo::RenderedVideo(const RenderedVideo& other) = | |
| 187 default; | |
| 188 | |
| 189 RenderingHelper::RenderedVideo::~RenderedVideo() { | |
| 190 } | |
| 191 | |
| 192 // static | |
| 193 void RenderingHelper::InitializeOneOff(base::WaitableEvent* done) { | |
| 194 base::CommandLine* cmd_line = base::CommandLine::ForCurrentProcess(); | |
| 195 #if GL_VARIANT_GLX | |
| 196 cmd_line->AppendSwitchASCII(switches::kUseGL, | |
| 197 gfx::kGLImplementationDesktopName); | |
| 198 #else | |
| 199 cmd_line->AppendSwitchASCII(switches::kUseGL, gfx::kGLImplementationEGLName); | |
| 200 #endif | |
| 201 | |
| 202 if (!gfx::GLSurface::InitializeOneOff()) | |
| 203 LOG(FATAL) << "Could not initialize GL"; | |
| 204 done->Signal(); | |
| 205 } | |
| 206 | |
| 207 RenderingHelper::RenderingHelper() : ignore_vsync_(false) { | |
| 208 window_ = gfx::kNullAcceleratedWidget; | |
| 209 Clear(); | |
| 210 } | |
| 211 | |
| 212 RenderingHelper::~RenderingHelper() { | |
| 213 CHECK_EQ(videos_.size(), 0U) << "Must call UnInitialize before dtor."; | |
| 214 Clear(); | |
| 215 } | |
| 216 | |
| 217 void RenderingHelper::Setup() { | |
| 218 #if defined(OS_WIN) | |
| 219 window_ = CreateWindowEx(0, | |
| 220 L"Static", | |
| 221 L"VideoDecodeAcceleratorTest", | |
| 222 WS_OVERLAPPEDWINDOW | WS_VISIBLE, | |
| 223 0, | |
| 224 0, | |
| 225 GetSystemMetrics(SM_CXSCREEN), | |
| 226 GetSystemMetrics(SM_CYSCREEN), | |
| 227 NULL, | |
| 228 NULL, | |
| 229 NULL, | |
| 230 NULL); | |
| 231 #elif defined(USE_X11) | |
| 232 Display* display = gfx::GetXDisplay(); | |
| 233 Screen* screen = DefaultScreenOfDisplay(display); | |
| 234 | |
| 235 CHECK(display); | |
| 236 | |
| 237 XSetWindowAttributes window_attributes; | |
| 238 memset(&window_attributes, 0, sizeof(window_attributes)); | |
| 239 window_attributes.background_pixel = | |
| 240 BlackPixel(display, DefaultScreen(display)); | |
| 241 window_attributes.override_redirect = true; | |
| 242 int depth = DefaultDepth(display, DefaultScreen(display)); | |
| 243 | |
| 244 window_ = XCreateWindow(display, | |
| 245 DefaultRootWindow(display), | |
| 246 0, | |
| 247 0, | |
| 248 XWidthOfScreen(screen), | |
| 249 XHeightOfScreen(screen), | |
| 250 0 /* border width */, | |
| 251 depth, | |
| 252 CopyFromParent /* class */, | |
| 253 CopyFromParent /* visual */, | |
| 254 (CWBackPixel | CWOverrideRedirect), | |
| 255 &window_attributes); | |
| 256 XStoreName(display, window_, "VideoDecodeAcceleratorTest"); | |
| 257 XSelectInput(display, window_, ExposureMask); | |
| 258 XMapWindow(display, window_); | |
| 259 #elif defined(USE_OZONE) | |
| 260 base::MessageLoop::ScopedNestableTaskAllower nest_loop( | |
| 261 base::MessageLoop::current()); | |
| 262 base::RunLoop wait_window_resize; | |
| 263 | |
| 264 platform_window_delegate_.reset(new RenderingHelper::StubOzoneDelegate()); | |
| 265 window_ = platform_window_delegate_->accelerated_widget(); | |
| 266 gfx::Size window_size(800, 600); | |
| 267 // Ignore the vsync provider by default. On ChromeOS this will be set | |
| 268 // accordingly based on the display configuration. | |
| 269 ignore_vsync_ = true; | |
| 270 #if defined(OS_CHROMEOS) | |
| 271 // We hold onto the main loop here to wait for the DisplayController | |
| 272 // to give us the size of the display so we can create a window of | |
| 273 // the same size. | |
| 274 base::RunLoop wait_display_setup; | |
| 275 DisplayConfiguratorObserver display_setup_observer(&wait_display_setup); | |
| 276 display_configurator_.reset(new ui::DisplayConfigurator()); | |
| 277 display_configurator_->SetDelegateForTesting(0); | |
| 278 display_configurator_->AddObserver(&display_setup_observer); | |
| 279 display_configurator_->Init( | |
| 280 ui::OzonePlatform::GetInstance()->CreateNativeDisplayDelegate(), | |
| 281 true); | |
| 282 display_configurator_->ForceInitialConfigure(0); | |
| 283 // Make sure all the display configuration is applied. | |
| 284 wait_display_setup.Run(); | |
| 285 display_configurator_->RemoveObserver(&display_setup_observer); | |
| 286 | |
| 287 gfx::Size framebuffer_size = display_configurator_->framebuffer_size(); | |
| 288 if (!framebuffer_size.IsEmpty()) { | |
| 289 window_size = framebuffer_size; | |
| 290 ignore_vsync_ = false; | |
| 291 } | |
| 292 #endif | |
| 293 if (ignore_vsync_) | |
| 294 DVLOG(1) << "Ignoring vsync provider"; | |
| 295 | |
| 296 platform_window_delegate_->platform_window()->SetBounds( | |
| 297 gfx::Rect(window_size)); | |
| 298 | |
| 299 // On Ozone/DRI, platform windows are associated with the physical | |
| 300 // outputs. Association is achieved by matching the bounds of the | |
| 301 // window with the origin & modeset of the display output. Until a | |
| 302 // window is associated with a display output, we cannot get vsync | |
| 303 // events, because there is no hardware to get events from. Here we | |
| 304 // wait for the window to resized and therefore associated with | |
| 305 // display output to be sure that we will get such events. | |
| 306 wait_window_resize.RunUntilIdle(); | |
| 307 #else | |
| 308 #error unknown platform | |
| 309 #endif | |
| 310 CHECK(window_ != gfx::kNullAcceleratedWidget); | |
| 311 } | |
| 312 | |
| 313 void RenderingHelper::TearDown() { | |
| 314 #if defined(OS_WIN) | |
| 315 if (window_) | |
| 316 DestroyWindow(window_); | |
| 317 #elif defined(USE_X11) | |
| 318 // Destroy resources acquired in Initialize, in reverse-acquisition order. | |
| 319 if (window_) { | |
| 320 CHECK(XUnmapWindow(gfx::GetXDisplay(), window_)); | |
| 321 CHECK(XDestroyWindow(gfx::GetXDisplay(), window_)); | |
| 322 } | |
| 323 #elif defined(USE_OZONE) | |
| 324 platform_window_delegate_.reset(); | |
| 325 #if defined(OS_CHROMEOS) | |
| 326 display_configurator_->PrepareForExit(); | |
| 327 display_configurator_.reset(); | |
| 328 #endif | |
| 329 #endif | |
| 330 window_ = gfx::kNullAcceleratedWidget; | |
| 331 } | |
| 332 | |
| 333 void RenderingHelper::Initialize(const RenderingHelperParams& params, | |
| 334 base::WaitableEvent* done) { | |
| 335 // Use videos_.size() != 0 as a proxy for the class having already been | |
| 336 // Initialize()'d, and UnInitialize() before continuing. | |
| 337 if (videos_.size()) { | |
| 338 base::WaitableEvent done(false, false); | |
| 339 UnInitialize(&done); | |
| 340 done.Wait(); | |
| 341 } | |
| 342 | |
| 343 render_task_.Reset( | |
| 344 base::Bind(&RenderingHelper::RenderContent, base::Unretained(this))); | |
| 345 | |
| 346 frame_duration_ = params.rendering_fps > 0 | |
| 347 ? base::TimeDelta::FromSeconds(1) / params.rendering_fps | |
| 348 : base::TimeDelta(); | |
| 349 | |
| 350 render_as_thumbnails_ = params.render_as_thumbnails; | |
| 351 message_loop_ = base::MessageLoop::current(); | |
| 352 | |
| 353 gl_surface_ = gfx::GLSurface::CreateViewGLSurface(window_); | |
| 354 #if defined(USE_OZONE) | |
| 355 gl_surface_->Resize(platform_window_delegate_->GetSize(), 1.f, true); | |
| 356 #endif // defined(USE_OZONE) | |
| 357 screen_size_ = gl_surface_->GetSize(); | |
| 358 | |
| 359 gl_context_ = gfx::GLContext::CreateGLContext( | |
| 360 NULL, gl_surface_.get(), gfx::PreferIntegratedGpu); | |
| 361 CHECK(gl_context_->MakeCurrent(gl_surface_.get())); | |
| 362 | |
| 363 CHECK_GT(params.window_sizes.size(), 0U); | |
| 364 videos_.resize(params.window_sizes.size()); | |
| 365 LayoutRenderingAreas(params.window_sizes); | |
| 366 | |
| 367 if (render_as_thumbnails_) { | |
| 368 CHECK_EQ(videos_.size(), 1U); | |
| 369 | |
| 370 GLint max_texture_size; | |
| 371 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &max_texture_size); | |
| 372 CHECK_GE(max_texture_size, params.thumbnails_page_size.width()); | |
| 373 CHECK_GE(max_texture_size, params.thumbnails_page_size.height()); | |
| 374 | |
| 375 thumbnails_fbo_size_ = params.thumbnails_page_size; | |
| 376 thumbnail_size_ = params.thumbnail_size; | |
| 377 | |
| 378 glGenFramebuffersEXT(1, &thumbnails_fbo_id_); | |
| 379 glGenTextures(1, &thumbnails_texture_id_); | |
| 380 glBindTexture(GL_TEXTURE_2D, thumbnails_texture_id_); | |
| 381 glTexImage2D(GL_TEXTURE_2D, | |
| 382 0, | |
| 383 GL_RGB, | |
| 384 thumbnails_fbo_size_.width(), | |
| 385 thumbnails_fbo_size_.height(), | |
| 386 0, | |
| 387 GL_RGB, | |
| 388 GL_UNSIGNED_SHORT_5_6_5, | |
| 389 NULL); | |
| 390 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); | |
| 391 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); | |
| 392 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); | |
| 393 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); | |
| 394 glBindTexture(GL_TEXTURE_2D, 0); | |
| 395 | |
| 396 glBindFramebufferEXT(GL_FRAMEBUFFER, thumbnails_fbo_id_); | |
| 397 glFramebufferTexture2DEXT(GL_FRAMEBUFFER, | |
| 398 GL_COLOR_ATTACHMENT0, | |
| 399 GL_TEXTURE_2D, | |
| 400 thumbnails_texture_id_, | |
| 401 0); | |
| 402 | |
| 403 GLenum fb_status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER); | |
| 404 CHECK(fb_status == GL_FRAMEBUFFER_COMPLETE) << fb_status; | |
| 405 glClearColor(0.0f, 0.0f, 0.0f, 1.0f); | |
| 406 glClear(GL_COLOR_BUFFER_BIT); | |
| 407 glBindFramebufferEXT(GL_FRAMEBUFFER, | |
| 408 gl_surface_->GetBackingFrameBufferObject()); | |
| 409 } | |
| 410 | |
| 411 // These vertices and texture coords. map (0,0) in the texture to the | |
| 412 // bottom left of the viewport. Since we get the video frames with the | |
| 413 // the top left at (0,0) we need to flip the texture y coordinate | |
| 414 // in the vertex shader for this to be rendered the right way up. | |
| 415 // In the case of thumbnail rendering we use the same vertex shader | |
| 416 // to render the FBO the screen, where we do not want this flipping. | |
| 417 static const float kVertices[] = | |
| 418 { -1.f, 1.f, -1.f, -1.f, 1.f, 1.f, 1.f, -1.f, }; | |
| 419 static const float kTextureCoords[] = { 0, 1, 0, 0, 1, 1, 1, 0, }; | |
| 420 static const char kVertexShader[] = STRINGIZE( | |
| 421 varying vec2 interp_tc; | |
| 422 attribute vec4 in_pos; | |
| 423 attribute vec2 in_tc; | |
| 424 uniform bool tex_flip; | |
| 425 void main() { | |
| 426 if (tex_flip) | |
| 427 interp_tc = vec2(in_tc.x, 1.0 - in_tc.y); | |
| 428 else | |
| 429 interp_tc = in_tc; | |
| 430 gl_Position = in_pos; | |
| 431 }); | |
| 432 | |
| 433 #if GL_VARIANT_EGL | |
| 434 static const char kFragmentShader[] = | |
| 435 "#extension GL_OES_EGL_image_external : enable\n" | |
| 436 "precision mediump float;\n" | |
| 437 "varying vec2 interp_tc;\n" | |
| 438 "uniform sampler2D tex;\n" | |
| 439 "#ifdef GL_OES_EGL_image_external\n" | |
| 440 "uniform samplerExternalOES tex_external;\n" | |
| 441 "#endif\n" | |
| 442 "void main() {\n" | |
| 443 " vec4 color = texture2D(tex, interp_tc);\n" | |
| 444 "#ifdef GL_OES_EGL_image_external\n" | |
| 445 " color += texture2D(tex_external, interp_tc);\n" | |
| 446 "#endif\n" | |
| 447 " gl_FragColor = color;\n" | |
| 448 "}\n"; | |
| 449 #else | |
| 450 static const char kFragmentShader[] = STRINGIZE( | |
| 451 varying vec2 interp_tc; | |
| 452 uniform sampler2D tex; | |
| 453 void main() { | |
| 454 gl_FragColor = texture2D(tex, interp_tc); | |
| 455 }); | |
| 456 #endif | |
| 457 program_ = glCreateProgram(); | |
| 458 CreateShader( | |
| 459 program_, GL_VERTEX_SHADER, kVertexShader, arraysize(kVertexShader)); | |
| 460 CreateShader(program_, | |
| 461 GL_FRAGMENT_SHADER, | |
| 462 kFragmentShader, | |
| 463 arraysize(kFragmentShader)); | |
| 464 glLinkProgram(program_); | |
| 465 int result = GL_FALSE; | |
| 466 glGetProgramiv(program_, GL_LINK_STATUS, &result); | |
| 467 if (!result) { | |
| 468 char log[4096]; | |
| 469 glGetShaderInfoLog(program_, arraysize(log), NULL, log); | |
| 470 LOG(FATAL) << log; | |
| 471 } | |
| 472 glUseProgram(program_); | |
| 473 glDeleteProgram(program_); | |
| 474 | |
| 475 glUniform1i(glGetUniformLocation(program_, "tex_flip"), 0); | |
| 476 glUniform1i(glGetUniformLocation(program_, "tex"), 0); | |
| 477 GLint tex_external = glGetUniformLocation(program_, "tex_external"); | |
| 478 if (tex_external != -1) { | |
| 479 glUniform1i(tex_external, 1); | |
| 480 } | |
| 481 int pos_location = glGetAttribLocation(program_, "in_pos"); | |
| 482 glEnableVertexAttribArray(pos_location); | |
| 483 glVertexAttribPointer(pos_location, 2, GL_FLOAT, GL_FALSE, 0, kVertices); | |
| 484 int tc_location = glGetAttribLocation(program_, "in_tc"); | |
| 485 glEnableVertexAttribArray(tc_location); | |
| 486 glVertexAttribPointer(tc_location, 2, GL_FLOAT, GL_FALSE, 0, kTextureCoords); | |
| 487 | |
| 488 if (!frame_duration_.is_zero()) { | |
| 489 int warm_up_iterations = params.warm_up_iterations; | |
| 490 #if defined(USE_OZONE) | |
| 491 // On Ozone the VSyncProvider can't provide a vsync interval until | |
| 492 // we render at least a frame, so we warm up with at least one | |
| 493 // frame. | |
| 494 // On top of this, we want to make sure all the buffers backing | |
| 495 // the GL surface are cleared, otherwise we can see the previous | |
| 496 // test's last frames, so we set warm up iterations to 2, to clear | |
| 497 // the front and back buffers. | |
| 498 warm_up_iterations = std::max(2, warm_up_iterations); | |
| 499 #endif | |
| 500 WarmUpRendering(warm_up_iterations); | |
| 501 } | |
| 502 | |
| 503 // It's safe to use Unretained here since |rendering_thread_| will be stopped | |
| 504 // in VideoDecodeAcceleratorTest.TearDown(), while the |rendering_helper_| is | |
| 505 // a member of that class. (See video_decode_accelerator_unittest.cc.) | |
| 506 gfx::VSyncProvider* vsync_provider = gl_surface_->GetVSyncProvider(); | |
| 507 | |
| 508 // VSync providers rely on the underlying CRTC to get the timing. In headless | |
| 509 // mode the surface isn't associated with a CRTC so the vsync provider can not | |
| 510 // get the timing, meaning it will not call UpdateVsyncParameters() ever. | |
| 511 if (!ignore_vsync_ && vsync_provider && !frame_duration_.is_zero()) { | |
| 512 vsync_provider->GetVSyncParameters(base::Bind( | |
| 513 &RenderingHelper::UpdateVSyncParameters, base::Unretained(this), done)); | |
| 514 } else { | |
| 515 done->Signal(); | |
| 516 } | |
| 517 } | |
| 518 | |
| 519 // The rendering for the first few frames is slow (e.g., 100ms on Peach Pit). | |
| 520 // This affects the numbers measured in the performance test. We try to render | |
| 521 // several frames here to warm up the rendering. | |
| 522 void RenderingHelper::WarmUpRendering(int warm_up_iterations) { | |
| 523 unsigned int texture_id; | |
| 524 std::unique_ptr<GLubyte[]> emptyData( | |
| 525 new GLubyte[screen_size_.GetArea() * 2]()); | |
| 526 glGenTextures(1, &texture_id); | |
| 527 glBindTexture(GL_TEXTURE_2D, texture_id); | |
| 528 glTexImage2D(GL_TEXTURE_2D, | |
| 529 0, | |
| 530 GL_RGB, | |
| 531 screen_size_.width(), | |
| 532 screen_size_.height(), | |
| 533 0, | |
| 534 GL_RGB, | |
| 535 GL_UNSIGNED_SHORT_5_6_5, | |
| 536 emptyData.get()); | |
| 537 for (int i = 0; i < warm_up_iterations; ++i) { | |
| 538 RenderTexture(GL_TEXTURE_2D, texture_id); | |
| 539 | |
| 540 // Need to allow nestable tasks since WarmUpRendering() is called from | |
| 541 // within another task on the renderer thread. | |
| 542 base::MessageLoop::ScopedNestableTaskAllower allow( | |
| 543 base::MessageLoop::current()); | |
| 544 base::RunLoop wait_for_swap_ack; | |
| 545 gl_surface_->SwapBuffersAsync( | |
| 546 base::Bind(&WaitForSwapAck, wait_for_swap_ack.QuitClosure())); | |
| 547 wait_for_swap_ack.Run(); | |
| 548 } | |
| 549 glDeleteTextures(1, &texture_id); | |
| 550 } | |
| 551 | |
| 552 void RenderingHelper::UnInitialize(base::WaitableEvent* done) { | |
| 553 CHECK_EQ(base::MessageLoop::current(), message_loop_); | |
| 554 | |
| 555 render_task_.Cancel(); | |
| 556 | |
| 557 if (render_as_thumbnails_) { | |
| 558 glDeleteTextures(1, &thumbnails_texture_id_); | |
| 559 glDeleteFramebuffersEXT(1, &thumbnails_fbo_id_); | |
| 560 } | |
| 561 | |
| 562 gl_context_->ReleaseCurrent(gl_surface_.get()); | |
| 563 gl_context_ = NULL; | |
| 564 gl_surface_ = NULL; | |
| 565 | |
| 566 Clear(); | |
| 567 done->Signal(); | |
| 568 } | |
| 569 | |
| 570 void RenderingHelper::CreateTexture(uint32_t texture_target, | |
| 571 uint32_t* texture_id, | |
| 572 const gfx::Size& size, | |
| 573 base::WaitableEvent* done) { | |
| 574 if (base::MessageLoop::current() != message_loop_) { | |
| 575 message_loop_->PostTask(FROM_HERE, | |
| 576 base::Bind(&RenderingHelper::CreateTexture, | |
| 577 base::Unretained(this), | |
| 578 texture_target, | |
| 579 texture_id, | |
| 580 size, | |
| 581 done)); | |
| 582 return; | |
| 583 } | |
| 584 glGenTextures(1, texture_id); | |
| 585 glBindTexture(texture_target, *texture_id); | |
| 586 if (texture_target == GL_TEXTURE_2D) { | |
| 587 glTexImage2D(GL_TEXTURE_2D, | |
| 588 0, | |
| 589 GL_RGBA, | |
| 590 size.width(), | |
| 591 size.height(), | |
| 592 0, | |
| 593 GL_RGBA, | |
| 594 GL_UNSIGNED_BYTE, | |
| 595 NULL); | |
| 596 } | |
| 597 glTexParameteri(texture_target, GL_TEXTURE_MIN_FILTER, GL_LINEAR); | |
| 598 glTexParameteri(texture_target, GL_TEXTURE_MAG_FILTER, GL_LINEAR); | |
| 599 // OpenGLES2.0.25 section 3.8.2 requires CLAMP_TO_EDGE for NPOT textures. | |
| 600 glTexParameteri(texture_target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); | |
| 601 glTexParameteri(texture_target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); | |
| 602 CHECK_EQ(static_cast<int>(glGetError()), GL_NO_ERROR); | |
| 603 done->Signal(); | |
| 604 } | |
| 605 | |
| 606 // Helper function to set GL viewport. | |
| 607 static inline void GLSetViewPort(const gfx::Rect& area) { | |
| 608 glViewport(area.x(), area.y(), area.width(), area.height()); | |
| 609 glScissor(area.x(), area.y(), area.width(), area.height()); | |
| 610 } | |
| 611 | |
| 612 void RenderingHelper::RenderThumbnail(uint32_t texture_target, | |
| 613 uint32_t texture_id) { | |
| 614 CHECK_EQ(base::MessageLoop::current(), message_loop_); | |
| 615 const int width = thumbnail_size_.width(); | |
| 616 const int height = thumbnail_size_.height(); | |
| 617 const int thumbnails_in_row = thumbnails_fbo_size_.width() / width; | |
| 618 const int thumbnails_in_column = thumbnails_fbo_size_.height() / height; | |
| 619 const int row = (frame_count_ / thumbnails_in_row) % thumbnails_in_column; | |
| 620 const int col = frame_count_ % thumbnails_in_row; | |
| 621 | |
| 622 gfx::Rect area(col * width, row * height, width, height); | |
| 623 | |
| 624 glUniform1i(glGetUniformLocation(program_, "tex_flip"), 0); | |
| 625 glBindFramebufferEXT(GL_FRAMEBUFFER, thumbnails_fbo_id_); | |
| 626 GLSetViewPort(area); | |
| 627 RenderTexture(texture_target, texture_id); | |
| 628 glBindFramebufferEXT(GL_FRAMEBUFFER, | |
| 629 gl_surface_->GetBackingFrameBufferObject()); | |
| 630 | |
| 631 // Need to flush the GL commands before we return the tnumbnail texture to | |
| 632 // the decoder. | |
| 633 glFlush(); | |
| 634 ++frame_count_; | |
| 635 } | |
| 636 | |
| 637 void RenderingHelper::QueueVideoFrame( | |
| 638 size_t window_id, | |
| 639 scoped_refptr<VideoFrameTexture> video_frame) { | |
| 640 CHECK_EQ(base::MessageLoop::current(), message_loop_); | |
| 641 RenderedVideo* video = &videos_[window_id]; | |
| 642 DCHECK(!video->is_flushing); | |
| 643 | |
| 644 video->pending_frames.push(video_frame); | |
| 645 | |
| 646 if (video->frames_to_drop > 0 && video->pending_frames.size() > 1) { | |
| 647 --video->frames_to_drop; | |
| 648 video->pending_frames.pop(); | |
| 649 } | |
| 650 | |
| 651 // Schedules the first RenderContent() if need. | |
| 652 if (scheduled_render_time_.is_null()) { | |
| 653 scheduled_render_time_ = base::TimeTicks::Now(); | |
| 654 message_loop_->PostTask(FROM_HERE, render_task_.callback()); | |
| 655 } | |
| 656 } | |
| 657 | |
| 658 void RenderingHelper::RenderTexture(uint32_t texture_target, | |
| 659 uint32_t texture_id) { | |
| 660 // The ExternalOES sampler is bound to GL_TEXTURE1 and the Texture2D sampler | |
| 661 // is bound to GL_TEXTURE0. | |
| 662 if (texture_target == GL_TEXTURE_2D) { | |
| 663 glActiveTexture(GL_TEXTURE0 + 0); | |
| 664 } else if (texture_target == GL_TEXTURE_EXTERNAL_OES) { | |
| 665 glActiveTexture(GL_TEXTURE0 + 1); | |
| 666 } | |
| 667 glBindTexture(texture_target, texture_id); | |
| 668 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); | |
| 669 glBindTexture(texture_target, 0); | |
| 670 CHECK_EQ(static_cast<int>(glGetError()), GL_NO_ERROR); | |
| 671 } | |
| 672 | |
| 673 void RenderingHelper::DeleteTexture(uint32_t texture_id) { | |
| 674 CHECK_EQ(base::MessageLoop::current(), message_loop_); | |
| 675 glDeleteTextures(1, &texture_id); | |
| 676 CHECK_EQ(static_cast<int>(glGetError()), GL_NO_ERROR); | |
| 677 } | |
| 678 | |
| 679 gfx::GLContext* RenderingHelper::GetGLContext() { | |
| 680 return gl_context_.get(); | |
| 681 } | |
| 682 | |
| 683 void* RenderingHelper::GetGLDisplay() { | |
| 684 return gl_surface_->GetDisplay(); | |
| 685 } | |
| 686 | |
| 687 void RenderingHelper::Clear() { | |
| 688 videos_.clear(); | |
| 689 message_loop_ = NULL; | |
| 690 gl_context_ = NULL; | |
| 691 gl_surface_ = NULL; | |
| 692 | |
| 693 render_as_thumbnails_ = false; | |
| 694 frame_count_ = 0; | |
| 695 thumbnails_fbo_id_ = 0; | |
| 696 thumbnails_texture_id_ = 0; | |
| 697 } | |
| 698 | |
| 699 void RenderingHelper::GetThumbnailsAsRGB(std::vector<unsigned char>* rgb, | |
| 700 bool* alpha_solid, | |
| 701 base::WaitableEvent* done) { | |
| 702 CHECK(render_as_thumbnails_); | |
| 703 | |
| 704 const size_t num_pixels = thumbnails_fbo_size_.GetArea(); | |
| 705 std::vector<unsigned char> rgba; | |
| 706 rgba.resize(num_pixels * 4); | |
| 707 glBindFramebufferEXT(GL_FRAMEBUFFER, thumbnails_fbo_id_); | |
| 708 glPixelStorei(GL_PACK_ALIGNMENT, 1); | |
| 709 // We can only count on GL_RGBA/GL_UNSIGNED_BYTE support. | |
| 710 glReadPixels(0, | |
| 711 0, | |
| 712 thumbnails_fbo_size_.width(), | |
| 713 thumbnails_fbo_size_.height(), | |
| 714 GL_RGBA, | |
| 715 GL_UNSIGNED_BYTE, | |
| 716 &rgba[0]); | |
| 717 glBindFramebufferEXT(GL_FRAMEBUFFER, | |
| 718 gl_surface_->GetBackingFrameBufferObject()); | |
| 719 rgb->resize(num_pixels * 3); | |
| 720 // Drop the alpha channel, but check as we go that it is all 0xff. | |
| 721 bool solid = true; | |
| 722 unsigned char* rgb_ptr = &((*rgb)[0]); | |
| 723 unsigned char* rgba_ptr = &rgba[0]; | |
| 724 for (size_t i = 0; i < num_pixels; ++i) { | |
| 725 *rgb_ptr++ = *rgba_ptr++; | |
| 726 *rgb_ptr++ = *rgba_ptr++; | |
| 727 *rgb_ptr++ = *rgba_ptr++; | |
| 728 solid = solid && (*rgba_ptr == 0xff); | |
| 729 rgba_ptr++; | |
| 730 } | |
| 731 *alpha_solid = solid; | |
| 732 | |
| 733 done->Signal(); | |
| 734 } | |
| 735 | |
| 736 void RenderingHelper::Flush(size_t window_id) { | |
| 737 videos_[window_id].is_flushing = true; | |
| 738 } | |
| 739 | |
| 740 void RenderingHelper::RenderContent() { | |
| 741 CHECK_EQ(base::MessageLoop::current(), message_loop_); | |
| 742 | |
| 743 // Update the VSync params. | |
| 744 // | |
| 745 // It's safe to use Unretained here since |rendering_thread_| will be stopped | |
| 746 // in VideoDecodeAcceleratorTest.TearDown(), while the |rendering_helper_| is | |
| 747 // a member of that class. (See video_decode_accelerator_unittest.cc.) | |
| 748 gfx::VSyncProvider* vsync_provider = gl_surface_->GetVSyncProvider(); | |
| 749 if (vsync_provider && !ignore_vsync_) { | |
| 750 vsync_provider->GetVSyncParameters(base::Bind( | |
| 751 &RenderingHelper::UpdateVSyncParameters, base::Unretained(this), | |
| 752 static_cast<base::WaitableEvent*>(NULL))); | |
| 753 } | |
| 754 | |
| 755 int tex_flip = 1; | |
| 756 #if defined(USE_OZONE) | |
| 757 // Ozone surfaceless renders flipped from normal GL, so there's no need to | |
| 758 // do an extra flip. | |
| 759 tex_flip = 0; | |
| 760 #endif // defined(USE_OZONE) | |
| 761 glUniform1i(glGetUniformLocation(program_, "tex_flip"), tex_flip); | |
| 762 | |
| 763 // Frames that will be returned to the client (via the no_longer_needed_cb) | |
| 764 // after this vector falls out of scope at the end of this method. We need | |
| 765 // to keep references to them until after SwapBuffers() call below. | |
| 766 std::vector<scoped_refptr<VideoFrameTexture> > frames_to_be_returned; | |
| 767 bool need_swap_buffer = false; | |
| 768 if (render_as_thumbnails_) { | |
| 769 // In render_as_thumbnails_ mode, we render the FBO content on the | |
| 770 // screen instead of the decoded textures. | |
| 771 GLSetViewPort(videos_[0].render_area); | |
| 772 RenderTexture(GL_TEXTURE_2D, thumbnails_texture_id_); | |
| 773 need_swap_buffer = true; | |
| 774 } else { | |
| 775 for (RenderedVideo& video : videos_) { | |
| 776 if (video.pending_frames.empty()) | |
| 777 continue; | |
| 778 need_swap_buffer = true; | |
| 779 scoped_refptr<VideoFrameTexture> frame = video.pending_frames.front(); | |
| 780 GLSetViewPort(video.render_area); | |
| 781 RenderTexture(frame->texture_target(), frame->texture_id()); | |
| 782 | |
| 783 if (video.pending_frames.size() > 1 || video.is_flushing) { | |
| 784 frames_to_be_returned.push_back(video.pending_frames.front()); | |
| 785 video.pending_frames.pop(); | |
| 786 } else { | |
| 787 ++video.frames_to_drop; | |
| 788 } | |
| 789 } | |
| 790 } | |
| 791 | |
| 792 base::Closure schedule_frame = base::Bind( | |
| 793 &RenderingHelper::ScheduleNextRenderContent, base::Unretained(this)); | |
| 794 if (!need_swap_buffer) { | |
| 795 schedule_frame.Run(); | |
| 796 return; | |
| 797 } | |
| 798 | |
| 799 gl_surface_->SwapBuffersAsync( | |
| 800 base::Bind(&WaitForSwapAck, schedule_frame)); | |
| 801 } | |
| 802 | |
| 803 // Helper function for the LayoutRenderingAreas(). The |lengths| are the | |
| 804 // heights(widths) of the rows(columns). It scales the elements in | |
| 805 // |lengths| proportionally so that the sum of them equal to |total_length|. | |
| 806 // It also outputs the coordinates of the rows(columns) to |offsets|. | |
| 807 static void ScaleAndCalculateOffsets(std::vector<int>* lengths, | |
| 808 std::vector<int>* offsets, | |
| 809 int total_length) { | |
| 810 int sum = std::accumulate(lengths->begin(), lengths->end(), 0); | |
| 811 for (size_t i = 0; i < lengths->size(); ++i) { | |
| 812 lengths->at(i) = lengths->at(i) * total_length / sum; | |
| 813 offsets->at(i) = (i == 0) ? 0 : offsets->at(i - 1) + lengths->at(i - 1); | |
| 814 } | |
| 815 } | |
| 816 | |
| 817 void RenderingHelper::LayoutRenderingAreas( | |
| 818 const std::vector<gfx::Size>& window_sizes) { | |
| 819 // Find the number of colums and rows. | |
| 820 // The smallest n * n or n * (n + 1) > number of windows. | |
| 821 size_t cols = sqrt(videos_.size() - 1) + 1; | |
| 822 size_t rows = (videos_.size() + cols - 1) / cols; | |
| 823 | |
| 824 // Find the widths and heights of the grid. | |
| 825 std::vector<int> widths(cols); | |
| 826 std::vector<int> heights(rows); | |
| 827 std::vector<int> offset_x(cols); | |
| 828 std::vector<int> offset_y(rows); | |
| 829 | |
| 830 for (size_t i = 0; i < window_sizes.size(); ++i) { | |
| 831 const gfx::Size& size = window_sizes[i]; | |
| 832 widths[i % cols] = std::max(widths[i % cols], size.width()); | |
| 833 heights[i / cols] = std::max(heights[i / cols], size.height()); | |
| 834 } | |
| 835 | |
| 836 ScaleAndCalculateOffsets(&widths, &offset_x, screen_size_.width()); | |
| 837 ScaleAndCalculateOffsets(&heights, &offset_y, screen_size_.height()); | |
| 838 | |
| 839 // Put each render_area_ in the center of each cell. | |
| 840 for (size_t i = 0; i < window_sizes.size(); ++i) { | |
| 841 const gfx::Size& size = window_sizes[i]; | |
| 842 float scale = | |
| 843 std::min(static_cast<float>(widths[i % cols]) / size.width(), | |
| 844 static_cast<float>(heights[i / cols]) / size.height()); | |
| 845 | |
| 846 // Don't scale up the texture. | |
| 847 scale = std::min(1.0f, scale); | |
| 848 | |
| 849 size_t w = scale * size.width(); | |
| 850 size_t h = scale * size.height(); | |
| 851 size_t x = offset_x[i % cols] + (widths[i % cols] - w) / 2; | |
| 852 size_t y = offset_y[i / cols] + (heights[i / cols] - h) / 2; | |
| 853 videos_[i].render_area = gfx::Rect(x, y, w, h); | |
| 854 } | |
| 855 } | |
| 856 | |
| 857 void RenderingHelper::UpdateVSyncParameters(base::WaitableEvent* done, | |
| 858 const base::TimeTicks timebase, | |
| 859 const base::TimeDelta interval) { | |
| 860 vsync_timebase_ = timebase; | |
| 861 vsync_interval_ = interval; | |
| 862 | |
| 863 if (done) | |
| 864 done->Signal(); | |
| 865 } | |
| 866 | |
| 867 void RenderingHelper::DropOneFrameForAllVideos() { | |
| 868 for (RenderedVideo& video : videos_) { | |
| 869 if (video.pending_frames.empty()) | |
| 870 continue; | |
| 871 | |
| 872 if (video.pending_frames.size() > 1 || video.is_flushing) { | |
| 873 video.pending_frames.pop(); | |
| 874 } else { | |
| 875 ++video.frames_to_drop; | |
| 876 } | |
| 877 } | |
| 878 } | |
| 879 | |
| 880 void RenderingHelper::ScheduleNextRenderContent() { | |
| 881 scheduled_render_time_ += frame_duration_; | |
| 882 base::TimeTicks now = base::TimeTicks::Now(); | |
| 883 base::TimeTicks target; | |
| 884 | |
| 885 if (vsync_interval_.is_zero()) { | |
| 886 target = std::max(now, scheduled_render_time_); | |
| 887 } else { | |
| 888 // Schedules the next RenderContent() at latest VSYNC before the | |
| 889 // |scheduled_render_time_|. | |
| 890 target = std::max(now + vsync_interval_, scheduled_render_time_); | |
| 891 | |
| 892 int64_t intervals = (target - vsync_timebase_) / vsync_interval_; | |
| 893 target = vsync_timebase_ + intervals * vsync_interval_; | |
| 894 } | |
| 895 | |
| 896 // When the rendering falls behind, drops frames. | |
| 897 while (scheduled_render_time_ < target) { | |
| 898 scheduled_render_time_ += frame_duration_; | |
| 899 DropOneFrameForAllVideos(); | |
| 900 } | |
| 901 | |
| 902 message_loop_->PostDelayedTask( | |
| 903 FROM_HERE, render_task_.callback(), target - now); | |
| 904 } | |
| 905 } // namespace content | |
| OLD | NEW |