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