Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(789)

Side by Side Diff: remoting/client/ios/display/gl_display_handler.mm

Issue 2828113002: Provide HostView with data required for creating a new and session, and render for CRD iOS. (Closed)
Patch Set: Adding main iOS build file change. Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2017 The Chromium Authors. All rights reserved. 1 // Copyright 2017 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 #if !defined(__has_feature) || !__has_feature(objc_arc) 5 #if !defined(__has_feature) || !__has_feature(objc_arc)
6 #error "This file requires ARC support." 6 #error "This file requires ARC support."
7 #endif 7 #endif
8 8
9 #include <array>
10
9 #import "remoting/client/ios/display/gl_display_handler.h" 11 #import "remoting/client/ios/display/gl_display_handler.h"
10 12
13 #import "base/mac/bind_objc_block.h"
11 #import "remoting/client/display/sys_opengl.h" 14 #import "remoting/client/display/sys_opengl.h"
12 #import "remoting/client/ios/display/gl_demo_screen.h" 15 #import "remoting/client/ios/display/gl_demo_screen.h"
13 16
14 #include "base/bind.h" 17 #include "base/bind.h"
15 #include "base/macros.h" 18 #include "base/macros.h"
16 #include "base/memory/ptr_util.h" 19 #include "base/memory/ptr_util.h"
17 #include "base/memory/weak_ptr.h" 20 #include "base/memory/weak_ptr.h"
18 #include "remoting/client/chromoting_client.h"
19 #include "remoting/client/chromoting_client_runtime.h" 21 #include "remoting/client/chromoting_client_runtime.h"
20 #include "remoting/client/cursor_shape_stub_proxy.h" 22 #include "remoting/client/cursor_shape_stub_proxy.h"
21 #include "remoting/client/display/gl_canvas.h" 23 #include "remoting/client/display/gl_canvas.h"
22 #include "remoting/client/display/gl_renderer.h" 24 #include "remoting/client/display/gl_renderer.h"
23 #include "remoting/client/display/gl_renderer_delegate.h" 25 #include "remoting/client/display/gl_renderer_delegate.h"
24 #include "remoting/client/dual_buffer_frame_consumer.h" 26 #include "remoting/client/dual_buffer_frame_consumer.h"
25 #include "remoting/client/software_video_renderer.h" 27 #include "remoting/client/software_video_renderer.h"
26 28
27 namespace remoting { 29 namespace remoting {
28 namespace GlDisplayHandler { 30 namespace GlDisplayHandler {
29 31
30 // The core that lives on the display thread. 32 // The core that lives on the display thread.
31 class Core : public protocol::CursorShapeStub, public GlRendererDelegate { 33 class Core : public protocol::CursorShapeStub, public GlRendererDelegate {
32 public: 34 public:
33 Core(); 35 Core();
34 ~Core() override; 36 ~Core() override;
35 37
38 void Initialize();
39
36 // CursorShapeStub interface. 40 // CursorShapeStub interface.
37 void SetCursorShape(const protocol::CursorShapeInfo& cursor_shape) override; 41 void SetCursorShape(const protocol::CursorShapeInfo& cursor_shape) override;
38 42
39 // GlRendererDelegate interface. 43 // GlRendererDelegate interface.
40 bool CanRenderFrame() override; 44 bool CanRenderFrame() override;
41 void OnFrameRendered() override; 45 void OnFrameRendered() override;
42 void OnSizeChanged(int width, int height) override; 46 void OnSizeChanged(int width, int height) override;
43 47
44 void Created(); 48 void OnFrameReceived(std::unique_ptr<webrtc::DesktopFrame> frame,
49 const base::Closure& done);
45 void Stop(); 50 void Stop();
46 void SurfaceChanged(int width, int height); 51 void SurfaceChanged(int width, int height);
47 std::unique_ptr<protocol::FrameConsumer> GrabFrameConsumer(); 52 std::unique_ptr<protocol::FrameConsumer> GrabFrameConsumer();
53 EAGLContext* GetEAGLContext();
48 base::WeakPtr<Core> GetWeakPtr(); 54 base::WeakPtr<Core> GetWeakPtr();
49 55
50 private: 56 private:
57 remoting::ChromotingClientRuntime* runtime_;
58
51 // Will be std::move'd when GrabFrameConsumer() is called. 59 // Will be std::move'd when GrabFrameConsumer() is called.
52 remoting::ChromotingClientRuntime* runtime_;
53 std::unique_ptr<DualBufferFrameConsumer> owned_frame_consumer_; 60 std::unique_ptr<DualBufferFrameConsumer> owned_frame_consumer_;
61 base::WeakPtr<DualBufferFrameConsumer> frame_consumer_;
54 62
55 base::WeakPtr<DualBufferFrameConsumer> frame_consumer_;
56 EAGLContext* eagl_context_; 63 EAGLContext* eagl_context_;
57 GlRenderer renderer_; 64 std::unique_ptr<GlRenderer> renderer_;
58 GlDemoScreen demo_screen_; 65 // GlDemoScreen *demo_screen_;
59 66
60 // Used on display thread. 67 // Used on display thread.
61 base::WeakPtr<Core> weak_ptr_; 68 base::WeakPtr<Core> weak_ptr_;
62 base::WeakPtrFactory<Core> weak_factory_; 69 base::WeakPtrFactory<Core> weak_factory_;
63 70
64 DISALLOW_COPY_AND_ASSIGN(Core); 71 DISALLOW_COPY_AND_ASSIGN(Core);
65 }; 72 };
66 73
67 Core::Core() : weak_factory_(this) { 74 Core::Core() : weak_factory_(this) {
68 runtime_ = ChromotingClientRuntime::GetInstance(); 75 runtime_ = ChromotingClientRuntime::GetInstance();
76 DCHECK(!runtime_->display_task_runner()->BelongsToCurrentThread());
77
69 weak_ptr_ = weak_factory_.GetWeakPtr(); 78 weak_ptr_ = weak_factory_.GetWeakPtr();
70 renderer_.SetDelegate(weak_ptr_); 79
80 runtime_->display_task_runner()->PostTask(
81 FROM_HERE, base::Bind(&Core::Initialize, base::Unretained(this)));
82
83 // Do not bind GlRenderer::OnFrameReceived. |renderer_| is not ready yet.
71 owned_frame_consumer_.reset(new remoting::DualBufferFrameConsumer( 84 owned_frame_consumer_.reset(new remoting::DualBufferFrameConsumer(
72 base::Bind(&remoting::GlRenderer::OnFrameReceived, 85 base::Bind(&Core::OnFrameReceived, weak_ptr_),
73 renderer_.GetWeakPtr()),
74 runtime_->display_task_runner(), 86 runtime_->display_task_runner(),
75 remoting::protocol::FrameConsumer::PixelFormat::FORMAT_RGBA)); 87 protocol::FrameConsumer::PixelFormat::FORMAT_RGBA));
76 frame_consumer_ = owned_frame_consumer_->GetWeakPtr(); 88 frame_consumer_ = owned_frame_consumer_->GetWeakPtr();
77 renderer_.AddDrawable(demo_screen_.GetWeakPtr());
78 } 89 }
79 90
80 Core::~Core() {} 91 Core::~Core() {
92 DCHECK(runtime_->display_task_runner()->BelongsToCurrentThread());
93 }
94
95 void Core::Initialize() {
96 DCHECK(runtime_->display_task_runner()->BelongsToCurrentThread());
97
98 eagl_context_ = [EAGLContext currentContext];
99 if (!eagl_context_) {
100 // TODO(nicholss): For prod code, make sure to check for ES3 support and
101 // fall back to ES2 if needed.
102 eagl_context_ =
103 [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES3];
104 [EAGLContext setCurrentContext:eagl_context_];
105 }
106
107 renderer_ = remoting::GlRenderer::CreateGlRendererWithDesktop();
108
109 // renderer_.RequestCanvasSize();
110
111 renderer_->OnSurfaceCreated(
112 base::MakeUnique<GlCanvas>(static_cast<int>([eagl_context_ API])));
113
114 SurfaceChanged(1024, 640); // TODO(nicholss): Where does this data comefrom?
115
116 // TODO(nicholss): This are wrong values but it lets us get something on the
117 // screen.
118 std::array<float, 9> matrix = {{1, 0, 0,
Yuwei 2017/04/24 19:27:39 I think you should sync with ToT?
nicholss 2017/04/24 20:41:52 I had to combine this cl with https://codereview.c
119 0, 1, 0,
120 0, 0, 1}};
121
122 renderer_->OnPixelTransformationChanged(matrix);
123
124 // demo_screen_ = new GlDemoScreen();
125 // renderer_->AddDrawable(demo_screen_->GetWeakPtr());
126 renderer_->SetDelegate(weak_ptr_);
127 }
81 128
82 void Core::SetCursorShape(const protocol::CursorShapeInfo& cursor_shape) { 129 void Core::SetCursorShape(const protocol::CursorShapeInfo& cursor_shape) {
83 DCHECK(runtime_->display_task_runner()->BelongsToCurrentThread()); 130 DCHECK(runtime_->display_task_runner()->BelongsToCurrentThread());
84 renderer_.OnCursorShapeChanged(cursor_shape); 131 renderer_->OnCursorShapeChanged(cursor_shape);
85 } 132 }
86 133
87 bool Core::CanRenderFrame() { 134 bool Core::CanRenderFrame() {
88 DCHECK(runtime_->display_task_runner()->BelongsToCurrentThread()); 135 DCHECK(runtime_->display_task_runner()->BelongsToCurrentThread());
89 return eagl_context_ != NULL; 136 return eagl_context_ != NULL;
90 } 137 }
91 138
92 std::unique_ptr<protocol::FrameConsumer> Core::GrabFrameConsumer() { 139 std::unique_ptr<protocol::FrameConsumer> Core::GrabFrameConsumer() {
93 DCHECK(owned_frame_consumer_) << "The frame consumer is already grabbed."; 140 DCHECK(owned_frame_consumer_) << "The frame consumer is already grabbed.";
94 return std::move(owned_frame_consumer_); 141 return std::move(owned_frame_consumer_);
95 } 142 }
96 143
144 void Core::OnFrameReceived(std::unique_ptr<webrtc::DesktopFrame> frame,
145 const base::Closure& done) {
146 DCHECK(runtime_->display_task_runner()->BelongsToCurrentThread());
147 renderer_->OnFrameReceived(std::move(frame), done);
148 }
149
97 void Core::OnFrameRendered() { 150 void Core::OnFrameRendered() {
98 // Nothing to do. 151 // Nothing to do.
99 } 152 }
100 153
101 void Core::OnSizeChanged(int width, int height) { 154 void Core::OnSizeChanged(int width, int height) {
102 // Nothing to do. 155 // Nothing to do.
103 } 156 }
104 157
105 void Core::Created() {
106 DCHECK(runtime_->display_task_runner()->BelongsToCurrentThread());
107 DCHECK(!eagl_context_);
108
109 eagl_context_ = [EAGLContext currentContext];
110 if (!eagl_context_) {
111 eagl_context_ =
112 [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES3];
113 [EAGLContext setCurrentContext:eagl_context_];
114 }
115 renderer_.RequestCanvasSize();
116
117 renderer_.OnSurfaceCreated(base::MakeUnique<GlCanvas>(
118 static_cast<int>([eagl_context_ API])));
119 }
120
121 void Core::Stop() { 158 void Core::Stop() {
122 DCHECK(runtime_->display_task_runner()->BelongsToCurrentThread()); 159 DCHECK(runtime_->display_task_runner()->BelongsToCurrentThread());
123 160
124 eagl_context_ = nil; 161 eagl_context_ = nil;
125 // demo_screen_ 162 // demo_screen_ = nil;
126 // renderer_ = nil;
127 } 163 }
128 164
129 void Core::SurfaceChanged(int width, int height) { 165 void Core::SurfaceChanged(int width, int height) {
130 DCHECK(runtime_->display_task_runner()->BelongsToCurrentThread()); 166 DCHECK(runtime_->display_task_runner()->BelongsToCurrentThread());
131 renderer_.OnSurfaceChanged(width, height); 167 renderer_->OnSurfaceChanged(width, height);
168 }
169
170 EAGLContext* Core::GetEAGLContext() {
171 return eagl_context_;
132 } 172 }
133 173
134 base::WeakPtr<remoting::GlDisplayHandler::Core> Core::GetWeakPtr() { 174 base::WeakPtr<remoting::GlDisplayHandler::Core> Core::GetWeakPtr() {
135 return weak_ptr_; 175 return weak_ptr_;
136 } 176 }
137 177
138 } // namespace GlDisplayHandler 178 } // namespace GlDisplayHandler
139 } // namespace remoting 179 } // namespace remoting
140 180
141 @interface GlDisplayHandler () 181 @interface GlDisplayHandler () {
142 @property(nonatomic) remoting::GlDisplayHandler::Core* core_; 182 remoting::GlDisplayHandler::Core* _core;
143 @property(nonatomic) remoting::ChromotingClientRuntime* runtime_; 183 remoting::ChromotingClientRuntime* _runtime;
184 }
144 @end 185 @end
145 186
146 @implementation GlDisplayHandler 187 @implementation GlDisplayHandler
147 188
148 @synthesize core_ = _core_; 189 - (id)init {
149 @synthesize runtime_ = _runtime_; 190 self = [super init];
150 191 if (self) {
151 - (id)initWithRuntime:(remoting::ChromotingClientRuntime*)runtime { 192 _runtime = remoting::ChromotingClientRuntime::GetInstance();
152 self.runtime_ = runtime; 193 _core = new remoting::GlDisplayHandler::Core();
194 }
153 return self; 195 return self;
154 } 196 }
155 197
156 - (void)created { 198 #pragma mark - Public
157 _core_ = new remoting::GlDisplayHandler::Core();
158
159 self.runtime_->display_task_runner()->PostTask(
160 FROM_HERE, base::Bind(&remoting::GlDisplayHandler::Core::Created,
161 self.core_->GetWeakPtr()));
162 }
163 199
164 - (void)stop { 200 - (void)stop {
165 self.runtime_->display_task_runner()->PostTask( 201 _runtime->display_task_runner()->PostTask(
166 FROM_HERE, base::Bind(&remoting::GlDisplayHandler::Core::Stop, 202 FROM_HERE,
167 self.core_->GetWeakPtr())); 203 base::Bind(&remoting::GlDisplayHandler::Core::Stop, _core->GetWeakPtr()));
168 } 204 }
169 205
170 - (std::unique_ptr<remoting::protocol::VideoRenderer>)CreateVideoRenderer { 206 - (std::unique_ptr<remoting::protocol::VideoRenderer>)CreateVideoRenderer {
171 return base::MakeUnique<remoting::SoftwareVideoRenderer>( 207 return base::MakeUnique<remoting::SoftwareVideoRenderer>(
172 _core_->GrabFrameConsumer()); 208 _core->GrabFrameConsumer());
173 } 209 }
174 210
175 - (std::unique_ptr<remoting::protocol::CursorShapeStub>)CreateCursorShapeStub { 211 - (std::unique_ptr<remoting::protocol::CursorShapeStub>)CreateCursorShapeStub {
176 return base::MakeUnique<remoting::CursorShapeStubProxy>( 212 return base::MakeUnique<remoting::CursorShapeStubProxy>(
177 _core_->GetWeakPtr(), self.runtime_->display_task_runner()); 213 _core->GetWeakPtr(), _runtime->display_task_runner());
178 } 214 }
179 215
180 // In general, avoid expensive work in this function to maximize frame rate. 216 - (EAGLContext*)GetEAGLContext {
217 return _core->GetEAGLContext();
218 }
219
220 // TODO(nicholss): Remove this function, it is not used in the final impl,
221 // or it should call RequestRender.
181 - (void)glkView:(GLKView*)view drawInRect:(CGRect)rect { 222 - (void)glkView:(GLKView*)view drawInRect:(CGRect)rect {
182 if (_core_) { 223 if (_core) {
183 _core_->SurfaceChanged(rect.size.width, rect.size.height); 224 _runtime->display_task_runner()->PostTask(
225 FROM_HERE,
226 base::Bind(&remoting::GlDisplayHandler::Core::SurfaceChanged,
227 _core->GetWeakPtr(), rect.size.width, rect.size.height));
184 } 228 }
185 } 229 }
186 230
187 @end 231 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698