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"
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"
29 #include "ui/gfx/x/x11_types.h"
32 #if defined(ARCH_CPU_X86_FAMILY) && defined(USE_X11)
33 #include "ui/gl/gl_surface_glx.h"
34 #define GL_VARIANT_GLX 1
36 #include "ui/gl/gl_surface_egl.h"
37 #define GL_VARIANT_EGL 1
40 #if defined(USE_OZONE)
41 #if defined(OS_CHROMEOS)
42 #include "ui/display/chromeos/display_configurator.h"
43 #endif // defined(OS_CHROMEOS)
44 #include "ui/ozone/public/ozone_platform.h"
45 #include "ui/platform_window/platform_window.h"
46 #include "ui/platform_window/platform_window_delegate.h"
47 #endif // defined(USE_OZONE)
49 // Helper for Shader creation.
50 static void CreateShader(GLuint program
,
54 GLuint shader
= glCreateShader(type
);
55 glShaderSource(shader
, 1, &source
, &size
);
56 glCompileShader(shader
);
57 int result
= GL_FALSE
;
58 glGetShaderiv(shader
, GL_COMPILE_STATUS
, &result
);
61 glGetShaderInfoLog(shader
, arraysize(log
), NULL
, log
);
64 glAttachShader(program
, shader
);
65 glDeleteShader(shader
);
66 CHECK_EQ(static_cast<int>(glGetError()), GL_NO_ERROR
);
71 #if defined(USE_OZONE)
73 class DisplayConfiguratorObserver
: public ui::DisplayConfigurator::Observer
{
75 DisplayConfiguratorObserver(base::RunLoop
* loop
) : loop_(loop
) {}
76 ~DisplayConfiguratorObserver() override
{}
79 // ui::DisplayConfigurator::Observer overrides:
80 void OnDisplayModeChanged(
81 const ui::DisplayConfigurator::DisplayStateList
& outputs
) override
{
87 void OnDisplayModeChangeFailed(
88 const ui::DisplayConfigurator::DisplayStateList
& outputs
,
89 ui::MultipleDisplayState failed_new_state
) override
{
90 LOG(FATAL
) << "Could not configure display";
95 DISALLOW_COPY_AND_ASSIGN(DisplayConfiguratorObserver
);
98 class RenderingHelper::StubOzoneDelegate
: public ui::PlatformWindowDelegate
{
100 StubOzoneDelegate() : accelerated_widget_(gfx::kNullAcceleratedWidget
) {
101 platform_window_
= ui::OzonePlatform::GetInstance()->CreatePlatformWindow(
104 ~StubOzoneDelegate() override
{}
106 void OnBoundsChanged(const gfx::Rect
& new_bounds
) override
{}
108 void OnDamageRect(const gfx::Rect
& damaged_region
) override
{}
110 void DispatchEvent(ui::Event
* event
) override
{}
112 void OnCloseRequest() override
{}
113 void OnClosed() override
{}
115 void OnWindowStateChanged(ui::PlatformWindowState new_state
) override
{}
117 void OnLostCapture() override
{};
119 void OnAcceleratedWidgetAvailable(gfx::AcceleratedWidget widget
) override
{
120 accelerated_widget_
= widget
;
123 void OnActivationChanged(bool active
) override
{};
125 gfx::AcceleratedWidget
accelerated_widget() const {
126 return accelerated_widget_
;
129 gfx::Size
GetSize() { return platform_window_
->GetBounds().size(); }
131 ui::PlatformWindow
* platform_window() const { return platform_window_
.get(); }
134 scoped_ptr
<ui::PlatformWindow
> platform_window_
;
135 gfx::AcceleratedWidget accelerated_widget_
;
137 DISALLOW_COPY_AND_ASSIGN(StubOzoneDelegate
);
140 #endif // defined(USE_OZONE)
142 RenderingHelperParams::RenderingHelperParams()
143 : rendering_fps(0), warm_up_iterations(0), render_as_thumbnails(false) {
146 RenderingHelperParams::~RenderingHelperParams() {}
148 VideoFrameTexture::VideoFrameTexture(uint32 texture_target
,
150 const base::Closure
& no_longer_needed_cb
)
151 : texture_target_(texture_target
),
152 texture_id_(texture_id
),
153 no_longer_needed_cb_(no_longer_needed_cb
) {
154 DCHECK(!no_longer_needed_cb_
.is_null());
157 VideoFrameTexture::~VideoFrameTexture() {
158 base::ResetAndReturn(&no_longer_needed_cb_
).Run();
161 RenderingHelper::RenderedVideo::RenderedVideo()
162 : is_flushing(false), frames_to_drop(0) {
165 RenderingHelper::RenderedVideo::~RenderedVideo() {
169 void RenderingHelper::InitializeOneOff(base::WaitableEvent
* done
) {
170 base::CommandLine
* cmd_line
= base::CommandLine::ForCurrentProcess();
172 cmd_line
->AppendSwitchASCII(switches::kUseGL
,
173 gfx::kGLImplementationDesktopName
);
175 cmd_line
->AppendSwitchASCII(switches::kUseGL
, gfx::kGLImplementationEGLName
);
178 if (!gfx::GLSurface::InitializeOneOff())
179 LOG(FATAL
) << "Could not initialize GL";
183 RenderingHelper::RenderingHelper() {
184 window_
= gfx::kNullAcceleratedWidget
;
188 RenderingHelper::~RenderingHelper() {
189 CHECK_EQ(videos_
.size(), 0U) << "Must call UnInitialize before dtor.";
193 void RenderingHelper::Setup() {
195 window_
= CreateWindowEx(0,
197 L
"VideoDecodeAcceleratorTest",
198 WS_OVERLAPPEDWINDOW
| WS_VISIBLE
,
201 GetSystemMetrics(SM_CXSCREEN
),
202 GetSystemMetrics(SM_CYSCREEN
),
207 #elif defined(USE_X11)
208 Display
* display
= gfx::GetXDisplay();
209 Screen
* screen
= DefaultScreenOfDisplay(display
);
213 XSetWindowAttributes window_attributes
;
214 memset(&window_attributes
, 0, sizeof(window_attributes
));
215 window_attributes
.background_pixel
=
216 BlackPixel(display
, DefaultScreen(display
));
217 window_attributes
.override_redirect
= true;
218 int depth
= DefaultDepth(display
, DefaultScreen(display
));
220 window_
= XCreateWindow(display
,
221 DefaultRootWindow(display
),
224 XWidthOfScreen(screen
),
225 XHeightOfScreen(screen
),
226 0 /* border width */,
228 CopyFromParent
/* class */,
229 CopyFromParent
/* visual */,
230 (CWBackPixel
| CWOverrideRedirect
),
232 XStoreName(display
, window_
, "VideoDecodeAcceleratorTest");
233 XSelectInput(display
, window_
, ExposureMask
);
234 XMapWindow(display
, window_
);
235 #elif defined(USE_OZONE)
236 base::MessageLoop::ScopedNestableTaskAllower
nest_loop(
237 base::MessageLoop::current());
238 base::RunLoop wait_window_resize
;
240 platform_window_delegate_
.reset(new RenderingHelper::StubOzoneDelegate());
241 window_
= platform_window_delegate_
->accelerated_widget();
242 #if defined(OS_CHROMEOS)
243 // We hold onto the main loop here to wait for the DisplayController
244 // to give us the size of the display so we can create a window of
246 base::RunLoop wait_display_setup
;
247 DisplayConfiguratorObserver
display_setup_observer(&wait_display_setup
);
248 display_configurator_
.reset(new ui::DisplayConfigurator());
249 display_configurator_
->AddObserver(&display_setup_observer
);
250 display_configurator_
->Init(true);
251 display_configurator_
->ForceInitialConfigure(0);
252 // Make sure all the display configuration is applied.
253 wait_display_setup
.Run();
254 display_configurator_
->RemoveObserver(&display_setup_observer
);
256 platform_window_delegate_
->platform_window()->SetBounds(
257 gfx::Rect(display_configurator_
->framebuffer_size()));
259 platform_window_delegate_
->platform_window()->SetBounds(gfx::Rect(800, 600));
262 // On Ozone/DRI, platform windows are associated with the physical
263 // outputs. Association is achieved by matching the bounds of the
264 // window with the origin & modeset of the display output. Until a
265 // window is associated with a display output, we cannot get vsync
266 // events, because there is no hardware to get events from. Here we
267 // wait for the window to resized and therefore associated with
268 // display output to be sure that we will get such events.
269 wait_window_resize
.RunUntilIdle();
271 #error unknown platform
273 CHECK(window_
!= gfx::kNullAcceleratedWidget
);
276 void RenderingHelper::TearDown() {
279 DestroyWindow(window_
);
280 #elif defined(USE_X11)
281 // Destroy resources acquired in Initialize, in reverse-acquisition order.
283 CHECK(XUnmapWindow(gfx::GetXDisplay(), window_
));
284 CHECK(XDestroyWindow(gfx::GetXDisplay(), window_
));
286 #elif defined(USE_OZONE)
287 platform_window_delegate_
.reset();
288 #if defined(OS_CHROMEOS)
289 display_configurator_
->PrepareForExit();
290 display_configurator_
.reset();
293 window_
= gfx::kNullAcceleratedWidget
;
296 void RenderingHelper::Initialize(const RenderingHelperParams
& params
,
297 base::WaitableEvent
* done
) {
298 // Use videos_.size() != 0 as a proxy for the class having already been
299 // Initialize()'d, and UnInitialize() before continuing.
300 if (videos_
.size()) {
301 base::WaitableEvent
done(false, false);
307 base::Bind(&RenderingHelper::RenderContent
, base::Unretained(this)));
309 frame_duration_
= params
.rendering_fps
> 0
310 ? base::TimeDelta::FromSeconds(1) / params
.rendering_fps
313 render_as_thumbnails_
= params
.render_as_thumbnails
;
314 message_loop_
= base::MessageLoop::current();
316 gl_surface_
= gfx::GLSurface::CreateViewGLSurface(window_
);
317 #if defined(USE_OZONE)
318 gl_surface_
->Resize(platform_window_delegate_
->GetSize());
319 #endif // defined(USE_OZONE)
320 screen_size_
= gl_surface_
->GetSize();
322 gl_context_
= gfx::GLContext::CreateGLContext(
323 NULL
, gl_surface_
.get(), gfx::PreferIntegratedGpu
);
324 CHECK(gl_context_
->MakeCurrent(gl_surface_
.get()));
326 CHECK_GT(params
.window_sizes
.size(), 0U);
327 videos_
.resize(params
.window_sizes
.size());
328 LayoutRenderingAreas(params
.window_sizes
);
330 if (render_as_thumbnails_
) {
331 CHECK_EQ(videos_
.size(), 1U);
333 GLint max_texture_size
;
334 glGetIntegerv(GL_MAX_TEXTURE_SIZE
, &max_texture_size
);
335 CHECK_GE(max_texture_size
, params
.thumbnails_page_size
.width());
336 CHECK_GE(max_texture_size
, params
.thumbnails_page_size
.height());
338 thumbnails_fbo_size_
= params
.thumbnails_page_size
;
339 thumbnail_size_
= params
.thumbnail_size
;
341 glGenFramebuffersEXT(1, &thumbnails_fbo_id_
);
342 glGenTextures(1, &thumbnails_texture_id_
);
343 glBindTexture(GL_TEXTURE_2D
, thumbnails_texture_id_
);
344 glTexImage2D(GL_TEXTURE_2D
,
347 thumbnails_fbo_size_
.width(),
348 thumbnails_fbo_size_
.height(),
351 GL_UNSIGNED_SHORT_5_6_5
,
353 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
354 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
355 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
356 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR
);
357 glBindTexture(GL_TEXTURE_2D
, 0);
359 glBindFramebufferEXT(GL_FRAMEBUFFER
, thumbnails_fbo_id_
);
360 glFramebufferTexture2DEXT(GL_FRAMEBUFFER
,
361 GL_COLOR_ATTACHMENT0
,
363 thumbnails_texture_id_
,
366 GLenum fb_status
= glCheckFramebufferStatusEXT(GL_FRAMEBUFFER
);
367 CHECK(fb_status
== GL_FRAMEBUFFER_COMPLETE
) << fb_status
;
368 glClearColor(0.0f
, 0.0f
, 0.0f
, 1.0f
);
369 glClear(GL_COLOR_BUFFER_BIT
);
370 glBindFramebufferEXT(GL_FRAMEBUFFER
,
371 gl_surface_
->GetBackingFrameBufferObject());
374 // These vertices and texture coords. map (0,0) in the texture to the
375 // bottom left of the viewport. Since we get the video frames with the
376 // the top left at (0,0) we need to flip the texture y coordinate
377 // in the vertex shader for this to be rendered the right way up.
378 // In the case of thumbnail rendering we use the same vertex shader
379 // to render the FBO the screen, where we do not want this flipping.
380 static const float kVertices
[] =
381 { -1.f
, 1.f
, -1.f
, -1.f
, 1.f
, 1.f
, 1.f
, -1.f
, };
382 static const float kTextureCoords
[] = { 0, 1, 0, 0, 1, 1, 1, 0, };
383 static const char kVertexShader
[] = STRINGIZE(
384 varying vec2 interp_tc
;
385 attribute vec4 in_pos
;
386 attribute vec2 in_tc
;
387 uniform
bool tex_flip
;
390 interp_tc
= vec2(in_tc
.x
, 1.0 - in_tc
.y
);
393 gl_Position
= in_pos
;
397 static const char kFragmentShader
[] =
398 "#extension GL_OES_EGL_image_external : enable\n"
399 "precision mediump float;\n"
400 "varying vec2 interp_tc;\n"
401 "uniform sampler2D tex;\n"
402 "#ifdef GL_OES_EGL_image_external\n"
403 "uniform samplerExternalOES tex_external;\n"
406 " vec4 color = texture2D(tex, interp_tc);\n"
407 "#ifdef GL_OES_EGL_image_external\n"
408 " color += texture2D(tex_external, interp_tc);\n"
410 " gl_FragColor = color;\n"
413 static const char kFragmentShader
[] = STRINGIZE(
414 varying vec2 interp_tc
;
415 uniform sampler2D tex
;
417 gl_FragColor
= texture2D(tex
, interp_tc
);
420 program_
= glCreateProgram();
422 program_
, GL_VERTEX_SHADER
, kVertexShader
, arraysize(kVertexShader
));
423 CreateShader(program_
,
426 arraysize(kFragmentShader
));
427 glLinkProgram(program_
);
428 int result
= GL_FALSE
;
429 glGetProgramiv(program_
, GL_LINK_STATUS
, &result
);
432 glGetShaderInfoLog(program_
, arraysize(log
), NULL
, log
);
435 glUseProgram(program_
);
436 glDeleteProgram(program_
);
438 glUniform1i(glGetUniformLocation(program_
, "tex_flip"), 0);
439 glUniform1i(glGetUniformLocation(program_
, "tex"), 0);
440 GLint tex_external
= glGetUniformLocation(program_
, "tex_external");
441 if (tex_external
!= -1) {
442 glUniform1i(tex_external
, 1);
444 int pos_location
= glGetAttribLocation(program_
, "in_pos");
445 glEnableVertexAttribArray(pos_location
);
446 glVertexAttribPointer(pos_location
, 2, GL_FLOAT
, GL_FALSE
, 0, kVertices
);
447 int tc_location
= glGetAttribLocation(program_
, "in_tc");
448 glEnableVertexAttribArray(tc_location
);
449 glVertexAttribPointer(tc_location
, 2, GL_FLOAT
, GL_FALSE
, 0, kTextureCoords
);
451 if (frame_duration_
!= base::TimeDelta()) {
452 int warm_up_iterations
= params
.warm_up_iterations
;
453 #if defined(USE_OZONE)
454 // On Ozone the VSyncProvider can't provide a vsync interval until
455 // we render at least a frame, so we warm up with at least one
457 // On top of this, we want to make sure all the buffers backing
458 // the GL surface are cleared, otherwise we can see the previous
459 // test's last frames, so we set warm up iterations to 2, to clear
460 // the front and back buffers.
461 warm_up_iterations
= std::max(2, warm_up_iterations
);
463 WarmUpRendering(warm_up_iterations
);
466 // It's safe to use Unretained here since |rendering_thread_| will be stopped
467 // in VideoDecodeAcceleratorTest.TearDown(), while the |rendering_helper_| is
468 // a member of that class. (See video_decode_accelerator_unittest.cc.)
469 gfx::VSyncProvider
* vsync_provider
= gl_surface_
->GetVSyncProvider();
470 if (vsync_provider
&& frame_duration_
!= base::TimeDelta())
471 vsync_provider
->GetVSyncParameters(base::Bind(
472 &RenderingHelper::UpdateVSyncParameters
, base::Unretained(this), done
));
477 // The rendering for the first few frames is slow (e.g., 100ms on Peach Pit).
478 // This affects the numbers measured in the performance test. We try to render
479 // several frames here to warm up the rendering.
480 void RenderingHelper::WarmUpRendering(int warm_up_iterations
) {
481 unsigned int texture_id
;
482 scoped_ptr
<GLubyte
[]> emptyData(new GLubyte
[screen_size_
.GetArea() * 2]());
483 glGenTextures(1, &texture_id
);
484 glBindTexture(GL_TEXTURE_2D
, texture_id
);
485 glTexImage2D(GL_TEXTURE_2D
,
488 screen_size_
.width(),
489 screen_size_
.height(),
492 GL_UNSIGNED_SHORT_5_6_5
,
494 for (int i
= 0; i
< warm_up_iterations
; ++i
) {
495 RenderTexture(GL_TEXTURE_2D
, texture_id
);
496 gl_surface_
->SwapBuffers();
498 glDeleteTextures(1, &texture_id
);
501 void RenderingHelper::UnInitialize(base::WaitableEvent
* done
) {
502 CHECK_EQ(base::MessageLoop::current(), message_loop_
);
504 render_task_
.Cancel();
506 if (render_as_thumbnails_
) {
507 glDeleteTextures(1, &thumbnails_texture_id_
);
508 glDeleteFramebuffersEXT(1, &thumbnails_fbo_id_
);
511 gl_surface_
->Destroy();
512 gl_context_
->ReleaseCurrent(gl_surface_
.get());
520 void RenderingHelper::CreateTexture(uint32 texture_target
,
522 const gfx::Size
& size
,
523 base::WaitableEvent
* done
) {
524 if (base::MessageLoop::current() != message_loop_
) {
525 message_loop_
->PostTask(FROM_HERE
,
526 base::Bind(&RenderingHelper::CreateTexture
,
527 base::Unretained(this),
534 glGenTextures(1, texture_id
);
535 glBindTexture(texture_target
, *texture_id
);
536 if (texture_target
== GL_TEXTURE_2D
) {
537 glTexImage2D(GL_TEXTURE_2D
,
547 glTexParameteri(texture_target
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR
);
548 glTexParameteri(texture_target
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
549 // OpenGLES2.0.25 section 3.8.2 requires CLAMP_TO_EDGE for NPOT textures.
550 glTexParameteri(texture_target
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
551 glTexParameteri(texture_target
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
552 CHECK_EQ(static_cast<int>(glGetError()), GL_NO_ERROR
);
556 // Helper function to set GL viewport.
557 static inline void GLSetViewPort(const gfx::Rect
& area
) {
558 glViewport(area
.x(), area
.y(), area
.width(), area
.height());
559 glScissor(area
.x(), area
.y(), area
.width(), area
.height());
562 void RenderingHelper::RenderThumbnail(uint32 texture_target
,
564 CHECK_EQ(base::MessageLoop::current(), message_loop_
);
565 const int width
= thumbnail_size_
.width();
566 const int height
= thumbnail_size_
.height();
567 const int thumbnails_in_row
= thumbnails_fbo_size_
.width() / width
;
568 const int thumbnails_in_column
= thumbnails_fbo_size_
.height() / height
;
569 const int row
= (frame_count_
/ thumbnails_in_row
) % thumbnails_in_column
;
570 const int col
= frame_count_
% thumbnails_in_row
;
572 gfx::Rect
area(col
* width
, row
* height
, width
, height
);
574 glUniform1i(glGetUniformLocation(program_
, "tex_flip"), 0);
575 glBindFramebufferEXT(GL_FRAMEBUFFER
, thumbnails_fbo_id_
);
577 RenderTexture(texture_target
, texture_id
);
578 glBindFramebufferEXT(GL_FRAMEBUFFER
,
579 gl_surface_
->GetBackingFrameBufferObject());
581 // Need to flush the GL commands before we return the tnumbnail texture to
587 void RenderingHelper::QueueVideoFrame(
589 scoped_refptr
<VideoFrameTexture
> video_frame
) {
590 CHECK_EQ(base::MessageLoop::current(), message_loop_
);
591 RenderedVideo
* video
= &videos_
[window_id
];
592 DCHECK(!video
->is_flushing
);
594 video
->pending_frames
.push(video_frame
);
596 if (video
->frames_to_drop
> 0 && video
->pending_frames
.size() > 1) {
597 --video
->frames_to_drop
;
598 video
->pending_frames
.pop();
601 // Schedules the first RenderContent() if need.
602 if (scheduled_render_time_
.is_null()) {
603 scheduled_render_time_
= base::TimeTicks::Now();
604 message_loop_
->PostTask(FROM_HERE
, render_task_
.callback());
608 void RenderingHelper::RenderTexture(uint32 texture_target
, uint32 texture_id
) {
609 // The ExternalOES sampler is bound to GL_TEXTURE1 and the Texture2D sampler
610 // is bound to GL_TEXTURE0.
611 if (texture_target
== GL_TEXTURE_2D
) {
612 glActiveTexture(GL_TEXTURE0
+ 0);
613 } else if (texture_target
== GL_TEXTURE_EXTERNAL_OES
) {
614 glActiveTexture(GL_TEXTURE0
+ 1);
616 glBindTexture(texture_target
, texture_id
);
617 glDrawArrays(GL_TRIANGLE_STRIP
, 0, 4);
618 glBindTexture(texture_target
, 0);
619 CHECK_EQ(static_cast<int>(glGetError()), GL_NO_ERROR
);
622 void RenderingHelper::DeleteTexture(uint32 texture_id
) {
623 CHECK_EQ(base::MessageLoop::current(), message_loop_
);
624 glDeleteTextures(1, &texture_id
);
625 CHECK_EQ(static_cast<int>(glGetError()), GL_NO_ERROR
);
628 scoped_refptr
<gfx::GLContext
> RenderingHelper::GetGLContext() {
632 void* RenderingHelper::GetGLContextHandle() {
633 return gl_context_
->GetHandle();
636 void* RenderingHelper::GetGLDisplay() {
637 return gl_surface_
->GetDisplay();
640 void RenderingHelper::Clear() {
642 message_loop_
= NULL
;
646 render_as_thumbnails_
= false;
648 thumbnails_fbo_id_
= 0;
649 thumbnails_texture_id_
= 0;
652 void RenderingHelper::GetThumbnailsAsRGB(std::vector
<unsigned char>* rgb
,
654 base::WaitableEvent
* done
) {
655 CHECK(render_as_thumbnails_
);
657 const size_t num_pixels
= thumbnails_fbo_size_
.GetArea();
658 std::vector
<unsigned char> rgba
;
659 rgba
.resize(num_pixels
* 4);
660 glBindFramebufferEXT(GL_FRAMEBUFFER
, thumbnails_fbo_id_
);
661 glPixelStorei(GL_PACK_ALIGNMENT
, 1);
662 // We can only count on GL_RGBA/GL_UNSIGNED_BYTE support.
665 thumbnails_fbo_size_
.width(),
666 thumbnails_fbo_size_
.height(),
670 glBindFramebufferEXT(GL_FRAMEBUFFER
,
671 gl_surface_
->GetBackingFrameBufferObject());
672 rgb
->resize(num_pixels
* 3);
673 // Drop the alpha channel, but check as we go that it is all 0xff.
675 unsigned char* rgb_ptr
= &((*rgb
)[0]);
676 unsigned char* rgba_ptr
= &rgba
[0];
677 for (size_t i
= 0; i
< num_pixels
; ++i
) {
678 *rgb_ptr
++ = *rgba_ptr
++;
679 *rgb_ptr
++ = *rgba_ptr
++;
680 *rgb_ptr
++ = *rgba_ptr
++;
681 solid
= solid
&& (*rgba_ptr
== 0xff);
684 *alpha_solid
= solid
;
689 void RenderingHelper::Flush(size_t window_id
) {
690 videos_
[window_id
].is_flushing
= true;
693 void RenderingHelper::RenderContent() {
694 CHECK_EQ(base::MessageLoop::current(), message_loop_
);
696 // Update the VSync params.
698 // It's safe to use Unretained here since |rendering_thread_| will be stopped
699 // in VideoDecodeAcceleratorTest.TearDown(), while the |rendering_helper_| is
700 // a member of that class. (See video_decode_accelerator_unittest.cc.)
701 gfx::VSyncProvider
* vsync_provider
= gl_surface_
->GetVSyncProvider();
702 if (vsync_provider
) {
703 vsync_provider
->GetVSyncParameters(base::Bind(
704 &RenderingHelper::UpdateVSyncParameters
, base::Unretained(this),
705 static_cast<base::WaitableEvent
*>(NULL
)));
709 #if defined(USE_OZONE)
710 // Ozone surfaceless renders flipped from normal GL, so there's no need to
713 #endif // defined(USE_OZONE)
714 glUniform1i(glGetUniformLocation(program_
, "tex_flip"), tex_flip
);
716 // Frames that will be returned to the client (via the no_longer_needed_cb)
717 // after this vector falls out of scope at the end of this method. We need
718 // to keep references to them until after SwapBuffers() call below.
719 std::vector
<scoped_refptr
<VideoFrameTexture
> > frames_to_be_returned
;
720 bool need_swap_buffer
= false;
721 if (render_as_thumbnails_
) {
722 // In render_as_thumbnails_ mode, we render the FBO content on the
723 // screen instead of the decoded textures.
724 GLSetViewPort(videos_
[0].render_area
);
725 RenderTexture(GL_TEXTURE_2D
, thumbnails_texture_id_
);
726 need_swap_buffer
= true;
728 for (RenderedVideo
& video
: videos_
) {
729 if (video
.pending_frames
.empty())
731 need_swap_buffer
= true;
732 scoped_refptr
<VideoFrameTexture
> frame
= video
.pending_frames
.front();
733 GLSetViewPort(video
.render_area
);
734 RenderTexture(frame
->texture_target(), frame
->texture_id());
736 if (video
.pending_frames
.size() > 1 || video
.is_flushing
) {
737 frames_to_be_returned
.push_back(video
.pending_frames
.front());
738 video
.pending_frames
.pop();
740 ++video
.frames_to_drop
;
745 if (need_swap_buffer
)
746 gl_surface_
->SwapBuffers();
748 ScheduleNextRenderContent();
751 // Helper function for the LayoutRenderingAreas(). The |lengths| are the
752 // heights(widths) of the rows(columns). It scales the elements in
753 // |lengths| proportionally so that the sum of them equal to |total_length|.
754 // It also outputs the coordinates of the rows(columns) to |offsets|.
755 static void ScaleAndCalculateOffsets(std::vector
<int>* lengths
,
756 std::vector
<int>* offsets
,
758 int sum
= std::accumulate(lengths
->begin(), lengths
->end(), 0);
759 for (size_t i
= 0; i
< lengths
->size(); ++i
) {
760 lengths
->at(i
) = lengths
->at(i
) * total_length
/ sum
;
761 offsets
->at(i
) = (i
== 0) ? 0 : offsets
->at(i
- 1) + lengths
->at(i
- 1);
765 void RenderingHelper::LayoutRenderingAreas(
766 const std::vector
<gfx::Size
>& window_sizes
) {
767 // Find the number of colums and rows.
768 // The smallest n * n or n * (n + 1) > number of windows.
769 size_t cols
= sqrt(videos_
.size() - 1) + 1;
770 size_t rows
= (videos_
.size() + cols
- 1) / cols
;
772 // Find the widths and heights of the grid.
773 std::vector
<int> widths(cols
);
774 std::vector
<int> heights(rows
);
775 std::vector
<int> offset_x(cols
);
776 std::vector
<int> offset_y(rows
);
778 for (size_t i
= 0; i
< window_sizes
.size(); ++i
) {
779 const gfx::Size
& size
= window_sizes
[i
];
780 widths
[i
% cols
] = std::max(widths
[i
% cols
], size
.width());
781 heights
[i
/ cols
] = std::max(heights
[i
/ cols
], size
.height());
784 ScaleAndCalculateOffsets(&widths
, &offset_x
, screen_size_
.width());
785 ScaleAndCalculateOffsets(&heights
, &offset_y
, screen_size_
.height());
787 // Put each render_area_ in the center of each cell.
788 for (size_t i
= 0; i
< window_sizes
.size(); ++i
) {
789 const gfx::Size
& size
= window_sizes
[i
];
791 std::min(static_cast<float>(widths
[i
% cols
]) / size
.width(),
792 static_cast<float>(heights
[i
/ cols
]) / size
.height());
794 // Don't scale up the texture.
795 scale
= std::min(1.0f
, scale
);
797 size_t w
= scale
* size
.width();
798 size_t h
= scale
* size
.height();
799 size_t x
= offset_x
[i
% cols
] + (widths
[i
% cols
] - w
) / 2;
800 size_t y
= offset_y
[i
/ cols
] + (heights
[i
/ cols
] - h
) / 2;
801 videos_
[i
].render_area
= gfx::Rect(x
, y
, w
, h
);
805 void RenderingHelper::UpdateVSyncParameters(base::WaitableEvent
* done
,
806 const base::TimeTicks timebase
,
807 const base::TimeDelta interval
) {
808 vsync_timebase_
= timebase
;
809 vsync_interval_
= interval
;
815 void RenderingHelper::DropOneFrameForAllVideos() {
816 for (RenderedVideo
& video
: videos_
) {
817 if (video
.pending_frames
.empty())
820 if (video
.pending_frames
.size() > 1 || video
.is_flushing
) {
821 video
.pending_frames
.pop();
823 ++video
.frames_to_drop
;
828 void RenderingHelper::ScheduleNextRenderContent() {
829 scheduled_render_time_
+= frame_duration_
;
830 base::TimeTicks now
= base::TimeTicks::Now();
831 base::TimeTicks target
;
833 if (vsync_interval_
!= base::TimeDelta()) {
834 // Schedules the next RenderContent() at latest VSYNC before the
835 // |scheduled_render_time_|.
836 target
= std::max(now
+ vsync_interval_
, scheduled_render_time_
);
838 int64 intervals
= (target
- vsync_timebase_
) / vsync_interval_
;
839 target
= vsync_timebase_
+ intervals
* vsync_interval_
;
841 target
= std::max(now
, scheduled_render_time_
);
844 // When the rendering falls behind, drops frames.
845 while (scheduled_render_time_
< target
) {
846 scheduled_render_time_
+= frame_duration_
;
847 DropOneFrameForAllVideos();
850 message_loop_
->PostDelayedTask(
851 FROM_HERE
, render_task_
.callback(), target
- now
);
853 } // namespace content