OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/common/gpu/media/rendering_helper.h" | 5 #include "content/common/gpu/media/rendering_helper.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <numeric> | 8 #include <numeric> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/bind.h" | 11 #include "base/bind.h" |
12 #include "base/callback_helpers.h" | |
12 #include "base/command_line.h" | 13 #include "base/command_line.h" |
13 #include "base/mac/scoped_nsautorelease_pool.h" | 14 #include "base/mac/scoped_nsautorelease_pool.h" |
14 #include "base/message_loop/message_loop.h" | 15 #include "base/message_loop/message_loop.h" |
16 #include "base/stl_util.h" | |
Pawel Osciak
2014/08/20 10:26:26
I think this is not needed anymore now that we use
Owen Lin
2014/08/21 03:45:18
Done.
| |
15 #include "base/strings/stringize_macros.h" | 17 #include "base/strings/stringize_macros.h" |
16 #include "base/synchronization/waitable_event.h" | 18 #include "base/synchronization/waitable_event.h" |
17 #include "ui/gl/gl_context.h" | 19 #include "ui/gl/gl_context.h" |
18 #include "ui/gl/gl_implementation.h" | 20 #include "ui/gl/gl_implementation.h" |
19 #include "ui/gl/gl_surface.h" | 21 #include "ui/gl/gl_surface.h" |
20 #include "ui/gl/gl_surface_egl.h" | 22 #include "ui/gl/gl_surface_egl.h" |
21 #include "ui/gl/gl_surface_glx.h" | 23 #include "ui/gl/gl_surface_glx.h" |
22 | 24 |
23 #if defined(OS_WIN) | 25 #if defined(OS_WIN) |
24 #include <windows.h> | 26 #include <windows.h> |
(...skipping 28 matching lines...) Expand all Loading... | |
53 glDeleteShader(shader); | 55 glDeleteShader(shader); |
54 CHECK_EQ(static_cast<int>(glGetError()), GL_NO_ERROR); | 56 CHECK_EQ(static_cast<int>(glGetError()), GL_NO_ERROR); |
55 } | 57 } |
56 | 58 |
57 namespace content { | 59 namespace content { |
58 | 60 |
59 RenderingHelperParams::RenderingHelperParams() {} | 61 RenderingHelperParams::RenderingHelperParams() {} |
60 | 62 |
61 RenderingHelperParams::~RenderingHelperParams() {} | 63 RenderingHelperParams::~RenderingHelperParams() {} |
62 | 64 |
65 VideoFrameTexture::VideoFrameTexture(uint32 texture_target, | |
66 uint32 texture_id, | |
67 const base::Closure& no_longer_needed_cb) | |
68 : texture_target_(texture_target), | |
69 texture_id_(texture_id), | |
70 no_longer_needed_cb_(no_longer_needed_cb) { | |
71 DCHECK(!no_longer_needed_cb_.is_null()); | |
72 } | |
73 | |
74 VideoFrameTexture::~VideoFrameTexture() { | |
75 base::ResetAndReturn(&no_longer_needed_cb_).Run(); | |
76 } | |
77 | |
63 // static | 78 // static |
64 bool RenderingHelper::InitializeOneOff() { | 79 bool RenderingHelper::InitializeOneOff() { |
65 base::CommandLine* cmd_line = base::CommandLine::ForCurrentProcess(); | 80 base::CommandLine* cmd_line = base::CommandLine::ForCurrentProcess(); |
66 #if GL_VARIANT_GLX | 81 #if GL_VARIANT_GLX |
67 cmd_line->AppendSwitchASCII(switches::kUseGL, | 82 cmd_line->AppendSwitchASCII(switches::kUseGL, |
68 gfx::kGLImplementationDesktopName); | 83 gfx::kGLImplementationDesktopName); |
69 #else | 84 #else |
70 cmd_line->AppendSwitchASCII(switches::kUseGL, gfx::kGLImplementationEGLName); | 85 cmd_line->AppendSwitchASCII(switches::kUseGL, gfx::kGLImplementationEGLName); |
71 #endif | 86 #endif |
72 return gfx::GLSurface::InitializeOneOff(); | 87 return gfx::GLSurface::InitializeOneOff(); |
73 } | 88 } |
74 | 89 |
75 RenderingHelper::RenderingHelper() { | 90 RenderingHelper::RenderingHelper() { |
76 window_ = gfx::kNullAcceleratedWidget; | 91 window_ = gfx::kNullAcceleratedWidget; |
77 Clear(); | 92 Clear(); |
78 } | 93 } |
79 | 94 |
80 RenderingHelper::~RenderingHelper() { | 95 RenderingHelper::~RenderingHelper() { |
81 CHECK_EQ(clients_.size(), 0U) << "Must call UnInitialize before dtor."; | 96 CHECK_EQ(videos_.size(), 0U) << "Must call UnInitialize before dtor."; |
82 Clear(); | 97 Clear(); |
83 } | 98 } |
84 | 99 |
85 void RenderingHelper::Initialize(const RenderingHelperParams& params, | 100 void RenderingHelper::Initialize(const RenderingHelperParams& params, |
86 base::WaitableEvent* done) { | 101 base::WaitableEvent* done) { |
87 // Use cients_.size() != 0 as a proxy for the class having already been | 102 // Use videos_.size() != 0 as a proxy for the class having already been |
88 // Initialize()'d, and UnInitialize() before continuing. | 103 // Initialize()'d, and UnInitialize() before continuing. |
89 if (clients_.size()) { | 104 if (videos_.size()) { |
90 base::WaitableEvent done(false, false); | 105 base::WaitableEvent done(false, false); |
91 UnInitialize(&done); | 106 UnInitialize(&done); |
92 done.Wait(); | 107 done.Wait(); |
93 } | 108 } |
94 | 109 |
95 frame_duration_ = params.rendering_fps > 0 | 110 frame_duration_ = params.rendering_fps > 0 |
96 ? base::TimeDelta::FromSeconds(1) / params.rendering_fps | 111 ? base::TimeDelta::FromSeconds(1) / params.rendering_fps |
97 : base::TimeDelta(); | 112 : base::TimeDelta(); |
98 | 113 |
99 render_as_thumbnails_ = params.render_as_thumbnails; | 114 render_as_thumbnails_ = params.render_as_thumbnails; |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
146 #else | 161 #else |
147 #error unknown platform | 162 #error unknown platform |
148 #endif | 163 #endif |
149 CHECK(window_ != gfx::kNullAcceleratedWidget); | 164 CHECK(window_ != gfx::kNullAcceleratedWidget); |
150 | 165 |
151 gl_surface_ = gfx::GLSurface::CreateViewGLSurface(window_); | 166 gl_surface_ = gfx::GLSurface::CreateViewGLSurface(window_); |
152 gl_context_ = gfx::GLContext::CreateGLContext( | 167 gl_context_ = gfx::GLContext::CreateGLContext( |
153 NULL, gl_surface_, gfx::PreferIntegratedGpu); | 168 NULL, gl_surface_, gfx::PreferIntegratedGpu); |
154 gl_context_->MakeCurrent(gl_surface_); | 169 gl_context_->MakeCurrent(gl_surface_); |
155 | 170 |
156 clients_ = params.clients; | 171 CHECK_GT(params.window_sizes.size(), 0U); |
157 CHECK_GT(clients_.size(), 0U); | 172 videos_.resize(params.window_sizes.size()); |
158 LayoutRenderingAreas(); | 173 LayoutRenderingAreas(params.window_sizes); |
159 | 174 |
160 if (render_as_thumbnails_) { | 175 if (render_as_thumbnails_) { |
161 CHECK_EQ(clients_.size(), 1U); | 176 CHECK_EQ(videos_.size(), 1U); |
162 | 177 |
163 GLint max_texture_size; | 178 GLint max_texture_size; |
164 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &max_texture_size); | 179 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &max_texture_size); |
165 CHECK_GE(max_texture_size, params.thumbnails_page_size.width()); | 180 CHECK_GE(max_texture_size, params.thumbnails_page_size.width()); |
166 CHECK_GE(max_texture_size, params.thumbnails_page_size.height()); | 181 CHECK_GE(max_texture_size, params.thumbnails_page_size.height()); |
167 | 182 |
168 thumbnails_fbo_size_ = params.thumbnails_page_size; | 183 thumbnails_fbo_size_ = params.thumbnails_page_size; |
169 thumbnail_size_ = params.thumbnail_size; | 184 thumbnail_size_ = params.thumbnail_size; |
170 | 185 |
171 glGenFramebuffersEXT(1, &thumbnails_fbo_id_); | 186 glGenFramebuffersEXT(1, &thumbnails_fbo_id_); |
(...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
363 GLSetViewPort(area); | 378 GLSetViewPort(area); |
364 RenderTexture(texture_target, texture_id); | 379 RenderTexture(texture_target, texture_id); |
365 glBindFramebufferEXT(GL_FRAMEBUFFER, 0); | 380 glBindFramebufferEXT(GL_FRAMEBUFFER, 0); |
366 | 381 |
367 // Need to flush the GL commands before we return the tnumbnail texture to | 382 // Need to flush the GL commands before we return the tnumbnail texture to |
368 // the decoder. | 383 // the decoder. |
369 glFlush(); | 384 glFlush(); |
370 ++frame_count_; | 385 ++frame_count_; |
371 } | 386 } |
372 | 387 |
388 void RenderingHelper::QueueVideoFrame( | |
389 size_t window_id, | |
390 scoped_refptr<VideoFrameTexture> video_frame) { | |
391 RenderedVideo* video = &videos_[window_id]; | |
392 | |
393 // Pop the front if it has been rendered. | |
Pawel Osciak
2014/08/20 10:26:26
This is what is confusing. Because this suggests t
Owen Lin
2014/08/21 03:45:18
Rephrased. Thanks.
| |
394 if (video->last_frame_rendered) { | |
395 // When last_frame_rendered is True, we should have only one pending frame. | |
Pawel Osciak
2014/08/20 10:26:26
s/True/true/
Owen Lin
2014/08/21 03:45:18
Done.
| |
396 // Since we are going to have a new frame, we can release the pending one. | |
397 DCHECK(video->pending_frames.size() == 1); | |
398 video->pending_frames.pop(); | |
399 video->last_frame_rendered = false; | |
400 } | |
401 | |
402 video->pending_frames.push(video_frame); | |
403 } | |
404 | |
405 void RenderingHelper::DropPendingFrames(size_t window_id) { | |
406 RenderedVideo* video = &videos_[window_id]; | |
407 video->pending_frames = std::queue<scoped_refptr<VideoFrameTexture> >(); | |
408 video->last_frame_rendered = false; | |
409 } | |
410 | |
373 void RenderingHelper::RenderTexture(uint32 texture_target, uint32 texture_id) { | 411 void RenderingHelper::RenderTexture(uint32 texture_target, uint32 texture_id) { |
374 // The ExternalOES sampler is bound to GL_TEXTURE1 and the Texture2D sampler | 412 // The ExternalOES sampler is bound to GL_TEXTURE1 and the Texture2D sampler |
375 // is bound to GL_TEXTURE0. | 413 // is bound to GL_TEXTURE0. |
376 if (texture_target == GL_TEXTURE_2D) { | 414 if (texture_target == GL_TEXTURE_2D) { |
377 glActiveTexture(GL_TEXTURE0 + 0); | 415 glActiveTexture(GL_TEXTURE0 + 0); |
378 } else if (texture_target == GL_TEXTURE_EXTERNAL_OES) { | 416 } else if (texture_target == GL_TEXTURE_EXTERNAL_OES) { |
379 glActiveTexture(GL_TEXTURE0 + 1); | 417 glActiveTexture(GL_TEXTURE0 + 1); |
380 } | 418 } |
381 glBindTexture(texture_target, texture_id); | 419 glBindTexture(texture_target, texture_id); |
382 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); | 420 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); |
383 glBindTexture(texture_target, 0); | 421 glBindTexture(texture_target, 0); |
384 CHECK_EQ(static_cast<int>(glGetError()), GL_NO_ERROR); | 422 CHECK_EQ(static_cast<int>(glGetError()), GL_NO_ERROR); |
385 } | 423 } |
386 | 424 |
387 void RenderingHelper::DeleteTexture(uint32 texture_id) { | 425 void RenderingHelper::DeleteTexture(uint32 texture_id) { |
388 glDeleteTextures(1, &texture_id); | 426 glDeleteTextures(1, &texture_id); |
389 CHECK_EQ(static_cast<int>(glGetError()), GL_NO_ERROR); | 427 CHECK_EQ(static_cast<int>(glGetError()), GL_NO_ERROR); |
390 } | 428 } |
391 | 429 |
392 void* RenderingHelper::GetGLContext() { | 430 void* RenderingHelper::GetGLContext() { |
393 return gl_context_->GetHandle(); | 431 return gl_context_->GetHandle(); |
394 } | 432 } |
395 | 433 |
396 void* RenderingHelper::GetGLDisplay() { | 434 void* RenderingHelper::GetGLDisplay() { |
397 return gl_surface_->GetDisplay(); | 435 return gl_surface_->GetDisplay(); |
398 } | 436 } |
399 | 437 |
400 void RenderingHelper::Clear() { | 438 void RenderingHelper::Clear() { |
401 clients_.clear(); | 439 videos_.clear(); |
402 message_loop_ = NULL; | 440 message_loop_ = NULL; |
403 gl_context_ = NULL; | 441 gl_context_ = NULL; |
404 gl_surface_ = NULL; | 442 gl_surface_ = NULL; |
405 | 443 |
406 render_as_thumbnails_ = false; | 444 render_as_thumbnails_ = false; |
407 frame_count_ = 0; | 445 frame_count_ = 0; |
408 thumbnails_fbo_id_ = 0; | 446 thumbnails_fbo_id_ = 0; |
409 thumbnails_texture_id_ = 0; | 447 thumbnails_texture_id_ = 0; |
410 | 448 |
411 #if defined(OS_WIN) | 449 #if defined(OS_WIN) |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
454 } | 492 } |
455 *alpha_solid = solid; | 493 *alpha_solid = solid; |
456 | 494 |
457 done->Signal(); | 495 done->Signal(); |
458 } | 496 } |
459 | 497 |
460 void RenderingHelper::RenderContent() { | 498 void RenderingHelper::RenderContent() { |
461 CHECK_EQ(base::MessageLoop::current(), message_loop_); | 499 CHECK_EQ(base::MessageLoop::current(), message_loop_); |
462 glUniform1i(glGetUniformLocation(program_, "tex_flip"), 1); | 500 glUniform1i(glGetUniformLocation(program_, "tex_flip"), 1); |
463 | 501 |
502 // To keep the frames before calling SwapBuffers(). Otherwise, those frames | |
Pawel Osciak
2014/08/20 10:26:26
I think this would be easier to understand:
"Fram
Owen Lin
2014/08/21 03:45:18
Thanks.
| |
503 // will be returned to client (via the no_longer_needed_cb) before being | |
504 // rendered. | |
505 std::vector<scoped_refptr<VideoFrameTexture> > frames_to_be_returned; | |
506 | |
464 if (render_as_thumbnails_) { | 507 if (render_as_thumbnails_) { |
465 // In render_as_thumbnails_ mode, we render the FBO content on the | 508 // In render_as_thumbnails_ mode, we render the FBO content on the |
466 // screen instead of the decoded textures. | 509 // screen instead of the decoded textures. |
467 GLSetViewPort(render_areas_[0]); | 510 GLSetViewPort(videos_[0].render_area); |
468 RenderTexture(GL_TEXTURE_2D, thumbnails_texture_id_); | 511 RenderTexture(GL_TEXTURE_2D, thumbnails_texture_id_); |
469 } else { | 512 } else { |
470 for (size_t i = 0; i < clients_.size(); ++i) { | 513 for (size_t i = 0; i < videos_.size(); ++i) { |
471 if (clients_[i]) { | 514 RenderedVideo* video = &videos_[i]; |
472 GLSetViewPort(render_areas_[i]); | 515 if (video->pending_frames.empty()) |
473 clients_[i]->RenderContent(this); | 516 continue; |
517 scoped_refptr<VideoFrameTexture> frame = video->pending_frames.front(); | |
518 GLSetViewPort(video->render_area); | |
519 RenderTexture(frame->texture_target(), frame->texture_id()); | |
520 | |
521 if (video->pending_frames.size() > 1) { | |
522 frames_to_be_returned.push_back(video->pending_frames.front()); | |
523 video->pending_frames.pop(); | |
524 } else { | |
525 video->last_frame_rendered = true; | |
474 } | 526 } |
475 } | 527 } |
476 } | 528 } |
477 | 529 |
478 gl_surface_->SwapBuffers(); | 530 gl_surface_->SwapBuffers(); |
479 } | 531 } |
480 | 532 |
481 // Helper function for the LayoutRenderingAreas(). The |lengths| are the | 533 // Helper function for the LayoutRenderingAreas(). The |lengths| are the |
482 // heights(widths) of the rows(columns). It scales the elements in | 534 // heights(widths) of the rows(columns). It scales the elements in |
483 // |lengths| proportionally so that the sum of them equal to |total_length|. | 535 // |lengths| proportionally so that the sum of them equal to |total_length|. |
484 // It also outputs the coordinates of the rows(columns) to |offsets|. | 536 // It also outputs the coordinates of the rows(columns) to |offsets|. |
485 static void ScaleAndCalculateOffsets(std::vector<int>* lengths, | 537 static void ScaleAndCalculateOffsets(std::vector<int>* lengths, |
486 std::vector<int>* offsets, | 538 std::vector<int>* offsets, |
487 int total_length) { | 539 int total_length) { |
488 int sum = std::accumulate(lengths->begin(), lengths->end(), 0); | 540 int sum = std::accumulate(lengths->begin(), lengths->end(), 0); |
489 for (size_t i = 0; i < lengths->size(); ++i) { | 541 for (size_t i = 0; i < lengths->size(); ++i) { |
490 lengths->at(i) = lengths->at(i) * total_length / sum; | 542 lengths->at(i) = lengths->at(i) * total_length / sum; |
491 offsets->at(i) = (i == 0) ? 0 : offsets->at(i - 1) + lengths->at(i - 1); | 543 offsets->at(i) = (i == 0) ? 0 : offsets->at(i - 1) + lengths->at(i - 1); |
492 } | 544 } |
493 } | 545 } |
494 | 546 |
495 void RenderingHelper::LayoutRenderingAreas() { | 547 void RenderingHelper::LayoutRenderingAreas( |
548 const std::vector<gfx::Size>& window_sizes) { | |
496 // Find the number of colums and rows. | 549 // Find the number of colums and rows. |
497 // The smallest n * n or n * (n + 1) > number of clients. | 550 // The smallest n * n or n * (n + 1) > number of windows. |
498 size_t cols = sqrt(clients_.size() - 1) + 1; | 551 size_t cols = sqrt(videos_.size() - 1) + 1; |
499 size_t rows = (clients_.size() + cols - 1) / cols; | 552 size_t rows = (videos_.size() + cols - 1) / cols; |
500 | 553 |
501 // Find the widths and heights of the grid. | 554 // Find the widths and heights of the grid. |
502 std::vector<int> widths(cols); | 555 std::vector<int> widths(cols); |
503 std::vector<int> heights(rows); | 556 std::vector<int> heights(rows); |
504 std::vector<int> offset_x(cols); | 557 std::vector<int> offset_x(cols); |
505 std::vector<int> offset_y(rows); | 558 std::vector<int> offset_y(rows); |
506 | 559 |
507 for (size_t i = 0; i < clients_.size(); ++i) { | 560 for (size_t i = 0; i < window_sizes.size(); ++i) { |
508 const gfx::Size& window_size = clients_[i]->GetWindowSize(); | 561 const gfx::Size& size = window_sizes[i]; |
509 widths[i % cols] = std::max(widths[i % cols], window_size.width()); | 562 widths[i % cols] = std::max(widths[i % cols], size.width()); |
510 heights[i / cols] = std::max(heights[i / cols], window_size.height()); | 563 heights[i / cols] = std::max(heights[i / cols], size.height()); |
511 } | 564 } |
512 | 565 |
513 ScaleAndCalculateOffsets(&widths, &offset_x, screen_size_.width()); | 566 ScaleAndCalculateOffsets(&widths, &offset_x, screen_size_.width()); |
514 ScaleAndCalculateOffsets(&heights, &offset_y, screen_size_.height()); | 567 ScaleAndCalculateOffsets(&heights, &offset_y, screen_size_.height()); |
515 | 568 |
516 // Put each render_area_ in the center of each cell. | 569 // Put each render_area_ in the center of each cell. |
517 render_areas_.clear(); | 570 for (size_t i = 0; i < window_sizes.size(); ++i) { |
518 for (size_t i = 0; i < clients_.size(); ++i) { | 571 const gfx::Size& size = window_sizes[i]; |
519 const gfx::Size& window_size = clients_[i]->GetWindowSize(); | |
520 float scale = | 572 float scale = |
521 std::min(static_cast<float>(widths[i % cols]) / window_size.width(), | 573 std::min(static_cast<float>(widths[i % cols]) / size.width(), |
522 static_cast<float>(heights[i / cols]) / window_size.height()); | 574 static_cast<float>(heights[i / cols]) / size.height()); |
523 | 575 |
524 // Don't scale up the texture. | 576 // Don't scale up the texture. |
525 scale = std::min(1.0f, scale); | 577 scale = std::min(1.0f, scale); |
526 | 578 |
527 size_t w = scale * window_size.width(); | 579 size_t w = scale * size.width(); |
528 size_t h = scale * window_size.height(); | 580 size_t h = scale * size.height(); |
529 size_t x = offset_x[i % cols] + (widths[i % cols] - w) / 2; | 581 size_t x = offset_x[i % cols] + (widths[i % cols] - w) / 2; |
530 size_t y = offset_y[i / cols] + (heights[i / cols] - h) / 2; | 582 size_t y = offset_y[i / cols] + (heights[i / cols] - h) / 2; |
531 render_areas_.push_back(gfx::Rect(x, y, w, h)); | 583 videos_[i].render_area = gfx::Rect(x, y, w, h); |
532 } | 584 } |
533 } | 585 } |
534 } // namespace content | 586 } // namespace content |
OLD | NEW |