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" | |
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 VideoFrame::VideoFrame(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 } | |
72 | |
73 VideoFrame::~VideoFrame() { | |
74 if (!no_longer_needed_cb_.is_null()) | |
Pawel Osciak
2014/08/14 04:48:23
I don't think we can allow this to be optional, be
Owen Lin
2014/08/14 09:20:41
Done.
| |
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(clients_.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 clients_.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 (clients_.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(); |
(...skipping 48 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 clients_.resize(params.window_sizes.size()); |
Pawel Osciak
2014/08/14 04:48:22
This should go to LayoutRenderingAreas() maybe?
Owen Lin
2014/08/14 09:20:41
mmm... I would prefer keep it here. As the name su
Pawel Osciak
2014/08/15 05:45:15
Ok, my issue was that we use videos_.size and wind
| |
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(clients_.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; |
(...skipping 194 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(size_t window_id, | |
389 scoped_ptr<VideoFrame> video_frame) { | |
390 RenderingClient* client = &clients_[window_id]; | |
391 | |
392 // Pop the front if it has been rendered. | |
393 if (client->last_frame_rendered && !client->pending_frames.empty()) { | |
394 delete client->pending_frames.front(); | |
Pawel Osciak
2014/08/14 04:48:23
Please use container of scoped_refptrs and just po
Owen Lin
2014/08/14 09:20:40
Done
| |
395 client->pending_frames.pop_front(); | |
396 } | |
397 | |
398 client->pending_frames.push_back(video_frame.release()); | |
399 client->last_frame_rendered = false; | |
Pawel Osciak
2014/08/14 04:48:23
Technically this should go to the if clause?
Owen Lin
2014/08/14 09:20:41
Yes, the code is modified.
| |
400 } | |
401 | |
402 void RenderingHelper::ClearVideoFrames(size_t window_id) { | |
403 STLDeleteElements(&clients_[window_id].pending_frames); | |
Pawel Osciak
2014/08/14 04:48:23
client->last_frame_rendered = false just in case?
Owen Lin
2014/08/14 09:20:40
Good catch. Thanks.
| |
404 } | |
405 | |
373 void RenderingHelper::RenderTexture(uint32 texture_target, uint32 texture_id) { | 406 void RenderingHelper::RenderTexture(uint32 texture_target, uint32 texture_id) { |
374 // The ExternalOES sampler is bound to GL_TEXTURE1 and the Texture2D sampler | 407 // The ExternalOES sampler is bound to GL_TEXTURE1 and the Texture2D sampler |
375 // is bound to GL_TEXTURE0. | 408 // is bound to GL_TEXTURE0. |
376 if (texture_target == GL_TEXTURE_2D) { | 409 if (texture_target == GL_TEXTURE_2D) { |
377 glActiveTexture(GL_TEXTURE0 + 0); | 410 glActiveTexture(GL_TEXTURE0 + 0); |
378 } else if (texture_target == GL_TEXTURE_EXTERNAL_OES) { | 411 } else if (texture_target == GL_TEXTURE_EXTERNAL_OES) { |
379 glActiveTexture(GL_TEXTURE0 + 1); | 412 glActiveTexture(GL_TEXTURE0 + 1); |
380 } | 413 } |
381 glBindTexture(texture_target, texture_id); | 414 glBindTexture(texture_target, texture_id); |
382 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); | 415 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
457 done->Signal(); | 490 done->Signal(); |
458 } | 491 } |
459 | 492 |
460 void RenderingHelper::RenderContent() { | 493 void RenderingHelper::RenderContent() { |
461 CHECK_EQ(base::MessageLoop::current(), message_loop_); | 494 CHECK_EQ(base::MessageLoop::current(), message_loop_); |
462 glUniform1i(glGetUniformLocation(program_, "tex_flip"), 1); | 495 glUniform1i(glGetUniformLocation(program_, "tex_flip"), 1); |
463 | 496 |
464 if (render_as_thumbnails_) { | 497 if (render_as_thumbnails_) { |
465 // In render_as_thumbnails_ mode, we render the FBO content on the | 498 // In render_as_thumbnails_ mode, we render the FBO content on the |
466 // screen instead of the decoded textures. | 499 // screen instead of the decoded textures. |
467 GLSetViewPort(render_areas_[0]); | 500 GLSetViewPort(clients_[0].render_area); |
468 RenderTexture(GL_TEXTURE_2D, thumbnails_texture_id_); | 501 RenderTexture(GL_TEXTURE_2D, thumbnails_texture_id_); |
469 } else { | 502 gl_surface_->SwapBuffers(); |
470 for (size_t i = 0; i < clients_.size(); ++i) { | 503 return; |
471 if (clients_[i]) { | 504 } |
472 GLSetViewPort(render_areas_[i]); | 505 |
473 clients_[i]->RenderContent(this); | 506 for (size_t i = 0; i < clients_.size(); ++i) { |
474 } | 507 RenderingClient* client = &clients_[i]; |
475 } | 508 if (client->pending_frames.empty()) |
509 continue; | |
510 VideoFrame* frame = client->pending_frames.front(); | |
511 GLSetViewPort(client->render_area); | |
512 RenderTexture(frame->texture_target(), frame->texture_id()); | |
476 } | 513 } |
477 | 514 |
478 gl_surface_->SwapBuffers(); | 515 gl_surface_->SwapBuffers(); |
516 | |
517 // Releases the rendered frame after SwapBuffers() if it is not the last one. | |
Pawel Osciak
2014/08/14 04:48:22
Instead of this, just check if client->pending_fra
Owen Lin
2014/08/14 09:20:41
Done. I was avoid using RefCounted object since t
| |
518 for (size_t i = 0; i < clients_.size(); ++i) { | |
519 RenderingClient* client = &clients_[i]; | |
520 if (client->pending_frames.size() > 1) { | |
521 delete client->pending_frames.front(); | |
522 client->pending_frames.pop_front(); | |
523 client->last_frame_rendered = false; | |
524 continue; | |
525 } | |
526 client->last_frame_rendered = true; | |
527 } | |
479 } | 528 } |
480 | 529 |
481 // Helper function for the LayoutRenderingAreas(). The |lengths| are the | 530 // Helper function for the LayoutRenderingAreas(). The |lengths| are the |
482 // heights(widths) of the rows(columns). It scales the elements in | 531 // heights(widths) of the rows(columns). It scales the elements in |
483 // |lengths| proportionally so that the sum of them equal to |total_length|. | 532 // |lengths| proportionally so that the sum of them equal to |total_length|. |
484 // It also outputs the coordinates of the rows(columns) to |offsets|. | 533 // It also outputs the coordinates of the rows(columns) to |offsets|. |
485 static void ScaleAndCalculateOffsets(std::vector<int>* lengths, | 534 static void ScaleAndCalculateOffsets(std::vector<int>* lengths, |
486 std::vector<int>* offsets, | 535 std::vector<int>* offsets, |
487 int total_length) { | 536 int total_length) { |
488 int sum = std::accumulate(lengths->begin(), lengths->end(), 0); | 537 int sum = std::accumulate(lengths->begin(), lengths->end(), 0); |
489 for (size_t i = 0; i < lengths->size(); ++i) { | 538 for (size_t i = 0; i < lengths->size(); ++i) { |
490 lengths->at(i) = lengths->at(i) * total_length / sum; | 539 lengths->at(i) = lengths->at(i) * total_length / sum; |
491 offsets->at(i) = (i == 0) ? 0 : offsets->at(i - 1) + lengths->at(i - 1); | 540 offsets->at(i) = (i == 0) ? 0 : offsets->at(i - 1) + lengths->at(i - 1); |
492 } | 541 } |
493 } | 542 } |
494 | 543 |
495 void RenderingHelper::LayoutRenderingAreas() { | 544 void RenderingHelper::LayoutRenderingAreas( |
545 const std::vector<gfx::Size>& window_sizes) { | |
496 // Find the number of colums and rows. | 546 // Find the number of colums and rows. |
497 // The smallest n * n or n * (n + 1) > number of clients. | 547 // The smallest n * n or n * (n + 1) > number of clients. |
498 size_t cols = sqrt(clients_.size() - 1) + 1; | 548 size_t cols = sqrt(clients_.size() - 1) + 1; |
499 size_t rows = (clients_.size() + cols - 1) / cols; | 549 size_t rows = (clients_.size() + cols - 1) / cols; |
500 | 550 |
501 // Find the widths and heights of the grid. | 551 // Find the widths and heights of the grid. |
502 std::vector<int> widths(cols); | 552 std::vector<int> widths(cols); |
503 std::vector<int> heights(rows); | 553 std::vector<int> heights(rows); |
504 std::vector<int> offset_x(cols); | 554 std::vector<int> offset_x(cols); |
505 std::vector<int> offset_y(rows); | 555 std::vector<int> offset_y(rows); |
506 | 556 |
507 for (size_t i = 0; i < clients_.size(); ++i) { | 557 for (size_t i = 0; i < window_sizes.size(); ++i) { |
508 const gfx::Size& window_size = clients_[i]->GetWindowSize(); | 558 const gfx::Size& size = window_sizes[i]; |
509 widths[i % cols] = std::max(widths[i % cols], window_size.width()); | 559 widths[i % cols] = std::max(widths[i % cols], size.width()); |
510 heights[i / cols] = std::max(heights[i / cols], window_size.height()); | 560 heights[i / cols] = std::max(heights[i / cols], size.height()); |
511 } | 561 } |
512 | 562 |
513 ScaleAndCalculateOffsets(&widths, &offset_x, screen_size_.width()); | 563 ScaleAndCalculateOffsets(&widths, &offset_x, screen_size_.width()); |
514 ScaleAndCalculateOffsets(&heights, &offset_y, screen_size_.height()); | 564 ScaleAndCalculateOffsets(&heights, &offset_y, screen_size_.height()); |
515 | 565 |
516 // Put each render_area_ in the center of each cell. | 566 // Put each render_area_ in the center of each cell. |
517 render_areas_.clear(); | 567 for (size_t i = 0; i < window_sizes.size(); ++i) { |
518 for (size_t i = 0; i < clients_.size(); ++i) { | 568 const gfx::Size& size = window_sizes[i]; |
519 const gfx::Size& window_size = clients_[i]->GetWindowSize(); | |
520 float scale = | 569 float scale = |
521 std::min(static_cast<float>(widths[i % cols]) / window_size.width(), | 570 std::min(static_cast<float>(widths[i % cols]) / size.width(), |
522 static_cast<float>(heights[i / cols]) / window_size.height()); | 571 static_cast<float>(heights[i / cols]) / size.height()); |
523 | 572 |
524 // Don't scale up the texture. | 573 // Don't scale up the texture. |
525 scale = std::min(1.0f, scale); | 574 scale = std::min(1.0f, scale); |
526 | 575 |
527 size_t w = scale * window_size.width(); | 576 size_t w = scale * size.width(); |
528 size_t h = scale * window_size.height(); | 577 size_t h = scale * size.height(); |
529 size_t x = offset_x[i % cols] + (widths[i % cols] - w) / 2; | 578 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; | 579 size_t y = offset_y[i / cols] + (heights[i / cols] - h) / 2; |
531 render_areas_.push_back(gfx::Rect(x, y, w, h)); | 580 clients_[i].render_area = gfx::Rect(x, y, w, h); |
532 } | 581 } |
533 } | 582 } |
534 } // namespace content | 583 } // namespace content |
OLD | NEW |