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