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