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 #ifndef REMOTING_CLIENT_GL_RENDERER_H_ | |
6 #define REMOTING_CLIENT_GL_RENDERER_H_ | |
7 | |
8 #include <queue> | |
9 | |
10 #include "base/callback.h" | |
11 #include "base/macros.h" | |
12 #include "base/memory/weak_ptr.h" | |
13 #include "base/threading/thread_checker.h" | |
14 #include "remoting/client/gl_cursor.h" | |
15 #include "remoting/client/gl_cursor_feedback.h" | |
16 #include "remoting/client/gl_desktop.h" | |
17 #include "remoting/proto/control.pb.h" | |
18 | |
19 namespace webrtc { | |
20 class DesktopFrame; | |
21 } // namespace webrtc | |
22 | |
23 namespace remoting { | |
24 | |
25 namespace protocol { | |
26 class CursorShapeInfo; | |
27 } // namespace protocol | |
28 | |
29 class GlCanvas; | |
30 class GlRendererDelegate; | |
31 class GlRendererTest; | |
32 | |
33 // Renders desktop and cursor on the OpenGL surface. Can be created on any | |
34 // thread but thereafter must be used and deleted on the same thread (usually | |
35 // the display thread. Or any Chromium thread with a task runner attached to | |
36 // it) unless otherwise noted. | |
37 // The unit of all length arguments is pixel. | |
38 class GlRenderer { | |
39 public: | |
40 explicit GlRenderer(); | |
41 ~GlRenderer(); | |
42 | |
43 // The delegate can be set on any hread no more than once before calling any | |
44 // On* functions. | |
45 void SetDelegate(base::WeakPtr<GlRendererDelegate> delegate); | |
46 | |
47 // Notifies the delegate with the current canvas size. Canvas size will be | |
48 // (0, 0) if no desktop frame is received yet. | |
49 // Caller can use this function to get the canvas size when the surface is | |
50 // recreated. | |
51 void RequestCanvasSize(); | |
52 | |
53 // Sets the pixel based transformation matrix related to the size of the | |
54 // canvas. | |
55 // 3 by 3 transformation matrix, [ m0, m1, m2, m3, m4, m5, m6, m7, m8 ]. | |
56 // | |
57 // | m0, m1, m2, | | x | | |
58 // | m3, m4, m5, | * | y | | |
59 // | m6, m7, m8 | | 1 | | |
60 // | |
61 // The final size of the canvas will be (m0*canvas_width, m4*canvas_height) | |
62 // and the top-left corner will be (m2, m5) in pixel coordinates. | |
63 void OnPixelTransformationChanged(const std::array<float, 9>& matrix); | |
64 | |
65 void OnCursorMoved(float x, float y); | |
66 | |
67 void OnCursorInputFeedback(float x, float y, float diameter); | |
68 | |
69 void OnCursorVisibilityChanged(bool visible); | |
70 | |
71 // Called when a desktop frame is received. | |
72 // The size of the canvas is determined by the dimension of the desktop frame. | |
73 // |done| will be queued up and called on the display thread after the actual | |
74 // rendering happens. | |
75 void OnFrameReceived(std::unique_ptr<webrtc::DesktopFrame> frame, | |
76 const base::Closure& done); | |
77 | |
78 void OnCursorShapeChanged(const protocol::CursorShapeInfo& shape); | |
79 | |
80 // Called after the EGL/EAGL context is established and the surface is created | |
81 // (or recreated). Previous desktop frame and canvas transformation will be | |
82 // lost after calling this function. | |
83 // Caller must call OnSurfaceDestroyed() before calling this function if the | |
84 // surface is recreated. | |
85 void OnSurfaceCreated(int gl_version); | |
86 | |
87 // Sets the size of the view. Called right after OnSurfaceCreated() or | |
88 // whenever the view size is changed. | |
89 void OnSurfaceChanged(int view_width, int view_height); | |
90 | |
91 // Called when the surface is destroyed. | |
92 void OnSurfaceDestroyed(); | |
93 | |
94 // Returns the weak pointer to be used on the display thread. | |
95 base::WeakPtr<GlRenderer> GetWeakPtr(); | |
96 | |
97 private: | |
98 friend class GlRendererTest; | |
99 | |
100 // Post a rendering task to the task runner of current thread. | |
101 // Do nothing if render_callback_ is not set yet or an existing rendering task | |
102 // in the queue will cover changes before this function is called. | |
103 void RequestRender(); | |
104 | |
105 // Draws out everything on current OpenGL buffer and runs closures in | |
106 // |pending_done_callbacks_|. | |
107 // Nothing will be drawn nor the done callbacks will be run if |delegate_| is | |
108 // invalid or !delegate_.CanRenderFrame(). | |
109 void OnRender(); | |
110 | |
111 base::WeakPtr<GlRendererDelegate> delegate_; | |
112 | |
113 // Done callbacks from OnFrameReceived. Will all be called once rendering | |
114 // takes place. | |
115 std::queue<base::Closure> pending_done_callbacks_; | |
116 | |
117 bool render_scheduled_ = false; | |
118 | |
119 int canvas_width_ = 0; | |
120 int canvas_height_ = 0; | |
121 | |
122 std::unique_ptr<GlCanvas> canvas_; | |
123 | |
124 GlCursor cursor_; | |
125 GlCursorFeedback cursor_feedback_; | |
126 GlDesktop desktop_; | |
127 | |
128 base::ThreadChecker thread_checker_; | |
129 base::WeakPtr<GlRenderer> weak_ptr_; | |
130 base::WeakPtrFactory<GlRenderer> weak_factory_; | |
131 | |
132 DISALLOW_COPY_AND_ASSIGN(GlRenderer); | |
133 }; | |
134 | |
135 } // namespace remoting | |
136 | |
137 #endif // REMOTING_CLIENT_GL_RENDERER_H_ | |
OLD | NEW |