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