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