OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 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 #include "remoting/client/jni/jni_display_handler.h" | 5 #include "remoting/client/jni/jni_display_handler.h" |
6 | 6 |
7 #include "base/android/jni_android.h" | 7 #include "base/android/jni_android.h" |
8 #include "base/bind.h" | 8 #include "base/bind.h" |
9 #include "base/location.h" | 9 #include "base/location.h" |
10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
11 #include "base/stl_util.h" | 11 #include "base/stl_util.h" |
12 #include "jni/Display_jni.h" | 12 #include "jni/Display_jni.h" |
| 13 #include "remoting/client/cursor_shape_stub_proxy.h" |
13 #include "remoting/client/jni/chromoting_jni_runtime.h" | 14 #include "remoting/client/jni/chromoting_jni_runtime.h" |
14 #include "remoting/client/jni/jni_client.h" | 15 #include "remoting/client/jni/jni_client.h" |
15 #include "remoting/client/jni/jni_video_renderer.h" | 16 #include "remoting/client/jni/jni_video_renderer.h" |
16 | 17 |
17 using base::android::JavaParamRef; | 18 using base::android::JavaParamRef; |
18 | 19 |
19 namespace { | 20 namespace { |
20 | 21 |
21 const int kBytesPerPixel = 4; | 22 const int kBytesPerPixel = 4; |
22 | 23 |
23 } | 24 } |
24 | 25 |
25 namespace remoting { | 26 namespace remoting { |
26 | 27 |
27 // CursorShapeStub with a lifetime separated from JniDisplayHandler. | |
28 class DisplayCursorShapeStub : public protocol::CursorShapeStub { | |
29 public: | |
30 DisplayCursorShapeStub( | |
31 base::WeakPtr<JniDisplayHandler> display, | |
32 scoped_refptr<base::SingleThreadTaskRunner> task_runner); | |
33 void SetCursorShape(const protocol::CursorShapeInfo& cursor_shape) override; | |
34 private: | |
35 base::WeakPtr<JniDisplayHandler> display_handler_; | |
36 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | |
37 }; | |
38 | |
39 DisplayCursorShapeStub::DisplayCursorShapeStub( | |
40 base::WeakPtr<JniDisplayHandler> display, | |
41 scoped_refptr<base::SingleThreadTaskRunner> task_runner) : | |
42 display_handler_(display), | |
43 task_runner_(task_runner) { | |
44 } | |
45 | |
46 void DisplayCursorShapeStub::SetCursorShape( | |
47 const protocol::CursorShapeInfo& cursor_shape) { | |
48 task_runner_->PostTask(FROM_HERE, | |
49 base::Bind(&JniDisplayHandler::UpdateCursorShape, | |
50 display_handler_, | |
51 cursor_shape)); | |
52 } | |
53 | |
54 // JniDisplayHandler definitions. | 28 // JniDisplayHandler definitions. |
55 JniDisplayHandler::JniDisplayHandler(ChromotingJniRuntime* runtime) | 29 JniDisplayHandler::JniDisplayHandler(ChromotingJniRuntime* runtime) |
56 : runtime_(runtime), | 30 : runtime_(runtime), |
57 weak_factory_(this) { | 31 weak_factory_(this) { |
58 weak_ptr_ = weak_factory_.GetWeakPtr(); | 32 weak_ptr_ = weak_factory_.GetWeakPtr(); |
59 JNIEnv* env = base::android::AttachCurrentThread(); | 33 JNIEnv* env = base::android::AttachCurrentThread(); |
60 java_display_.Reset(Java_Display_createJavaDisplayObject( | 34 java_display_.Reset(Java_Display_createJavaDisplayObject( |
61 env, reinterpret_cast<intptr_t>(this))); | 35 env, reinterpret_cast<intptr_t>(this))); |
62 } | 36 } |
63 | 37 |
64 JniDisplayHandler::~JniDisplayHandler() { | 38 JniDisplayHandler::~JniDisplayHandler() { |
65 DCHECK(runtime_->display_task_runner()->BelongsToCurrentThread()); | 39 DCHECK(runtime_->display_task_runner()->BelongsToCurrentThread()); |
66 Java_Display_invalidate(base::android::AttachCurrentThread(), | 40 Java_Display_invalidate(base::android::AttachCurrentThread(), |
67 java_display_.obj()); | 41 java_display_.obj()); |
68 } | 42 } |
69 | 43 |
70 void JniDisplayHandler::InitializeClient( | 44 void JniDisplayHandler::InitializeClient( |
71 const base::android::JavaRef<jobject>& java_client) { | 45 const base::android::JavaRef<jobject>& java_client) { |
72 return Java_Display_initializeClient(base::android::AttachCurrentThread(), | 46 return Java_Display_initializeClient(base::android::AttachCurrentThread(), |
73 java_display_.obj(), java_client.obj()); | 47 java_display_.obj(), java_client.obj()); |
74 } | 48 } |
75 | 49 |
76 void JniDisplayHandler::UpdateCursorShape( | |
77 const protocol::CursorShapeInfo& cursor_shape) { | |
78 DCHECK(runtime_->display_task_runner()->BelongsToCurrentThread()); | |
79 | |
80 // const_cast<> is safe as long as the Java updateCursorShape() method copies | |
81 // the data out of the buffer without mutating it, and doesn't keep any | |
82 // reference to the buffer afterwards. Unfortunately, there seems to be no way | |
83 // to create a read-only ByteBuffer from a pointer-to-const. | |
84 char* data = string_as_array(const_cast<std::string*>(&cursor_shape.data())); | |
85 int cursor_total_bytes = | |
86 cursor_shape.width() * cursor_shape.height() * kBytesPerPixel; | |
87 | |
88 JNIEnv* env = base::android::AttachCurrentThread(); | |
89 base::android::ScopedJavaLocalRef<jobject> buffer( | |
90 env, env->NewDirectByteBuffer(data, cursor_total_bytes)); | |
91 Java_Display_updateCursorShape( | |
92 env, java_display_.obj(), cursor_shape.width(), cursor_shape.height(), | |
93 cursor_shape.hotspot_x(), cursor_shape.hotspot_y(), buffer.obj()); | |
94 } | |
95 | |
96 std::unique_ptr<protocol::CursorShapeStub> | 50 std::unique_ptr<protocol::CursorShapeStub> |
97 JniDisplayHandler::CreateCursorShapeStub() { | 51 JniDisplayHandler::CreateCursorShapeStub() { |
98 return base::WrapUnique( | 52 return base::WrapUnique(new CursorShapeStubProxy( |
99 new DisplayCursorShapeStub(weak_ptr_, runtime_->display_task_runner())); | 53 weak_ptr_, runtime_->display_task_runner())); |
100 } | 54 } |
101 | 55 |
102 std::unique_ptr<protocol::VideoRenderer> | 56 std::unique_ptr<protocol::VideoRenderer> |
103 JniDisplayHandler::CreateVideoRenderer() { | 57 JniDisplayHandler::CreateVideoRenderer() { |
104 return base::WrapUnique( | 58 return base::WrapUnique( |
105 new JniVideoRenderer(runtime_, weak_ptr_)); | 59 new JniVideoRenderer(runtime_, weak_ptr_)); |
106 } | 60 } |
107 | 61 |
108 // static | 62 // static |
109 base::android::ScopedJavaLocalRef<jobject> JniDisplayHandler::NewBitmap( | 63 base::android::ScopedJavaLocalRef<jobject> JniDisplayHandler::NewBitmap( |
(...skipping 23 matching lines...) Expand all Loading... |
133 return RegisterNativesImpl(env); | 87 return RegisterNativesImpl(env); |
134 } | 88 } |
135 | 89 |
136 void JniDisplayHandler::ScheduleRedraw( | 90 void JniDisplayHandler::ScheduleRedraw( |
137 JNIEnv* env, | 91 JNIEnv* env, |
138 const base::android::JavaParamRef<jobject>& caller) { | 92 const base::android::JavaParamRef<jobject>& caller) { |
139 runtime_->display_task_runner()->PostTask( | 93 runtime_->display_task_runner()->PostTask( |
140 FROM_HERE, base::Bind(&JniDisplayHandler::RedrawCanvas, weak_ptr_)); | 94 FROM_HERE, base::Bind(&JniDisplayHandler::RedrawCanvas, weak_ptr_)); |
141 } | 95 } |
142 | 96 |
| 97 void JniDisplayHandler::SetCursorShape( |
| 98 const protocol::CursorShapeInfo& cursor_shape) { |
| 99 DCHECK(runtime_->display_task_runner()->BelongsToCurrentThread()); |
| 100 |
| 101 // const_cast<> is safe as long as the Java updateCursorShape() method copies |
| 102 // the data out of the buffer without mutating it, and doesn't keep any |
| 103 // reference to the buffer afterwards. Unfortunately, there seems to be no way |
| 104 // to create a read-only ByteBuffer from a pointer-to-const. |
| 105 char* data = string_as_array(const_cast<std::string*>(&cursor_shape.data())); |
| 106 int cursor_total_bytes = |
| 107 cursor_shape.width() * cursor_shape.height() * kBytesPerPixel; |
| 108 |
| 109 JNIEnv* env = base::android::AttachCurrentThread(); |
| 110 base::android::ScopedJavaLocalRef<jobject> buffer( |
| 111 env, env->NewDirectByteBuffer(data, cursor_total_bytes)); |
| 112 Java_Display_updateCursorShape( |
| 113 env, java_display_.obj(), cursor_shape.width(), cursor_shape.height(), |
| 114 cursor_shape.hotspot_x(), cursor_shape.hotspot_y(), buffer.obj()); |
| 115 } |
| 116 |
143 } // namespace remoting | 117 } // namespace remoting |
OLD | NEW |