OLD | NEW |
| (Empty) |
1 // Copyright 2016 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. | |
4 | |
5 #include "remoting/client/gl_desktop.h" | |
6 | |
7 #include "base/logging.h" | |
8 #include "base/memory/ptr_util.h" | |
9 #include "remoting/client/gl_canvas.h" | |
10 #include "remoting/client/gl_math.h" | |
11 #include "remoting/client/gl_render_layer.h" | |
12 #include "remoting/client/gl_texture_ids.h" | |
13 #include "third_party/webrtc/modules/desktop_capture/desktop_frame.h" | |
14 #include "third_party/webrtc/modules/desktop_capture/desktop_region.h" | |
15 | |
16 namespace remoting { | |
17 | |
18 namespace { | |
19 | |
20 void UpdateDesktopRegion(const webrtc::DesktopFrame& frame, | |
21 const webrtc::DesktopRegion& region, | |
22 const webrtc::DesktopRect& texture_rect, | |
23 GlRenderLayer* layer) { | |
24 for (webrtc::DesktopRegion::Iterator i(region); !i.IsAtEnd(); i.Advance()) { | |
25 const uint8_t* rect_start = frame.GetFrameDataAtPos(i.rect().top_left()); | |
26 webrtc::DesktopVector update_pos = | |
27 i.rect().top_left().subtract(texture_rect.top_left()); | |
28 layer->UpdateTexture(rect_start, update_pos.x(), update_pos.y(), | |
29 i.rect().width(), i.rect().height(), frame.stride()); | |
30 } | |
31 } | |
32 | |
33 void SetFrameForTexture(const webrtc::DesktopFrame& frame, | |
34 GlRenderLayer* layer, | |
35 const webrtc::DesktopRect& rect) { | |
36 if (!layer->is_texture_set()) { | |
37 // First frame received. | |
38 layer->SetTexture(frame.GetFrameDataAtPos(rect.top_left()), rect.width(), | |
39 rect.height(), frame.stride()); | |
40 return; | |
41 } | |
42 // Incremental update. | |
43 if (frame.size().equals(rect.size())) { | |
44 // Single texture. No intersection is needed. | |
45 UpdateDesktopRegion(frame, frame.updated_region(), rect, layer); | |
46 } else { | |
47 webrtc::DesktopRegion intersected_region = frame.updated_region(); | |
48 intersected_region.IntersectWith(rect); | |
49 UpdateDesktopRegion(frame, intersected_region, rect, layer); | |
50 } | |
51 } | |
52 | |
53 } // namespace | |
54 | |
55 struct GlDesktop::GlDesktopTextureContainer { | |
56 std::unique_ptr<GlRenderLayer> layer; | |
57 webrtc::DesktopRect rect; | |
58 }; | |
59 | |
60 GlDesktop::GlDesktop() {} | |
61 | |
62 GlDesktop::~GlDesktop() {} | |
63 | |
64 void GlDesktop::SetCanvas(GlCanvas* canvas) { | |
65 last_desktop_size_.set(0, 0); | |
66 textures_.clear(); | |
67 canvas_ = canvas; | |
68 if (canvas) { | |
69 max_texture_size_ = canvas->GetMaxTextureSize(); | |
70 } | |
71 } | |
72 | |
73 void GlDesktop::SetVideoFrame(const webrtc::DesktopFrame& frame) { | |
74 if (!canvas_) { | |
75 return; | |
76 } | |
77 if (!frame.size().equals(last_desktop_size_)) { | |
78 last_desktop_size_.set(frame.size().width(), frame.size().height()); | |
79 ReallocateTextures(last_desktop_size_); | |
80 } | |
81 for (std::unique_ptr<GlDesktopTextureContainer>& texture : textures_) { | |
82 SetFrameForTexture(frame, texture->layer.get(), texture->rect); | |
83 } | |
84 } | |
85 | |
86 void GlDesktop::Draw() { | |
87 if (!textures_.empty() && !last_desktop_size_.is_empty()) { | |
88 for (std::unique_ptr<GlDesktopTextureContainer>& texture : textures_) { | |
89 texture->layer->Draw(1.0f); | |
90 } | |
91 } | |
92 } | |
93 | |
94 void GlDesktop::ReallocateTextures(const webrtc::DesktopSize& size) { | |
95 DCHECK(max_texture_size_); | |
96 DCHECK(canvas_); | |
97 textures_.clear(); | |
98 | |
99 int textures_per_row = | |
100 (size.width() + max_texture_size_ - 1) / max_texture_size_; | |
101 | |
102 int textures_per_column = | |
103 (size.height() + max_texture_size_ - 1) / max_texture_size_; | |
104 | |
105 webrtc::DesktopRect desktop_rect = webrtc::DesktopRect::MakeSize(size); | |
106 | |
107 int texture_id = kGlDesktopFirstTextureId; | |
108 std::array<float, 8> positions; | |
109 for (int x = 0; x < textures_per_row; x++) { | |
110 for (int y = 0; y < textures_per_column; y++) { | |
111 webrtc::DesktopRect rect = webrtc::DesktopRect::MakeXYWH( | |
112 x * max_texture_size_, y * max_texture_size_, max_texture_size_, | |
113 max_texture_size_); | |
114 rect.IntersectWith(desktop_rect); | |
115 std::unique_ptr<GlDesktopTextureContainer> container = | |
116 base::WrapUnique(new GlDesktopTextureContainer{ | |
117 base::MakeUnique<GlRenderLayer>(texture_id, canvas_), rect}); | |
118 FillRectangleVertexPositions(rect.left(), rect.top(), rect.width(), | |
119 rect.height(), &positions); | |
120 container->layer->SetVertexPositions(positions); | |
121 textures_.push_back(std::move(container)); | |
122 texture_id++; | |
123 } | |
124 } | |
125 } | |
126 | |
127 } // namespace remoting | |
OLD | NEW |