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 #include "remoting/client/jni/jni_display_handler.h" | |
6 | |
7 #include "base/android/jni_android.h" | |
8 #include "base/bind.h" | |
9 #include "base/location.h" | |
10 #include "base/memory/ptr_util.h" | |
11 #include "base/stl_util.h" | |
12 #include "jni/Display_jni.h" | |
13 #include "remoting/client/cursor_shape_stub_proxy.h" | |
14 #include "remoting/client/jni/chromoting_jni_runtime.h" | |
15 #include "remoting/client/jni/jni_client.h" | |
16 #include "remoting/client/jni/jni_video_renderer.h" | |
17 | |
18 using base::android::JavaParamRef; | |
19 | |
20 namespace { | |
21 | |
22 const int kBytesPerPixel = 4; | |
23 | |
24 } | |
25 | |
26 namespace remoting { | |
27 | |
28 // JniDisplayHandler definitions. | |
29 JniDisplayHandler::JniDisplayHandler(ChromotingJniRuntime* runtime) | |
30 : runtime_(runtime), | |
31 weak_factory_(this) { | |
32 weak_ptr_ = weak_factory_.GetWeakPtr(); | |
33 JNIEnv* env = base::android::AttachCurrentThread(); | |
34 java_display_.Reset(Java_Display_createJavaDisplayObject( | |
35 env, reinterpret_cast<intptr_t>(this))); | |
36 } | |
37 | |
38 JniDisplayHandler::~JniDisplayHandler() { | |
39 DCHECK(runtime_->display_task_runner()->BelongsToCurrentThread()); | |
40 Java_Display_invalidate(base::android::AttachCurrentThread(), java_display_); | |
41 } | |
42 | |
43 void JniDisplayHandler::InitializeClient( | |
44 const base::android::JavaRef<jobject>& java_client) { | |
45 return Java_Display_initializeClient(base::android::AttachCurrentThread(), | |
46 java_display_, java_client); | |
47 } | |
48 | |
49 std::unique_ptr<protocol::CursorShapeStub> | |
50 JniDisplayHandler::CreateCursorShapeStub() { | |
51 return base::WrapUnique(new CursorShapeStubProxy( | |
52 weak_ptr_, runtime_->display_task_runner())); | |
53 } | |
54 | |
55 std::unique_ptr<protocol::VideoRenderer> | |
56 JniDisplayHandler::CreateVideoRenderer() { | |
57 return base::WrapUnique( | |
58 new JniVideoRenderer(runtime_, weak_ptr_)); | |
59 } | |
60 | |
61 // static | |
62 base::android::ScopedJavaLocalRef<jobject> JniDisplayHandler::NewBitmap( | |
63 int width, | |
64 int height) { | |
65 JNIEnv* env = base::android::AttachCurrentThread(); | |
66 return Java_Display_newBitmap(env, width, height); | |
67 } | |
68 | |
69 void JniDisplayHandler::UpdateFrameBitmap( | |
70 base::android::ScopedJavaGlobalRef<jobject> bitmap) { | |
71 DCHECK(runtime_->display_task_runner()->BelongsToCurrentThread()); | |
72 | |
73 JNIEnv* env = base::android::AttachCurrentThread(); | |
74 Java_Display_setVideoFrame(env, java_display_, bitmap); | |
75 } | |
76 | |
77 void JniDisplayHandler::RedrawCanvas() { | |
78 DCHECK(runtime_->display_task_runner()->BelongsToCurrentThread()); | |
79 | |
80 JNIEnv* env = base::android::AttachCurrentThread(); | |
81 Java_Display_redrawGraphicsInternal(env, java_display_); | |
82 } | |
83 | |
84 // static | |
85 bool JniDisplayHandler::RegisterJni(JNIEnv* env) { | |
86 return RegisterNativesImpl(env); | |
87 } | |
88 | |
89 void JniDisplayHandler::ScheduleRedraw( | |
90 JNIEnv* env, | |
91 const base::android::JavaParamRef<jobject>& caller) { | |
92 runtime_->display_task_runner()->PostTask( | |
93 FROM_HERE, base::Bind(&JniDisplayHandler::RedrawCanvas, weak_ptr_)); | |
94 } | |
95 | |
96 void JniDisplayHandler::SetCursorShape( | |
97 const protocol::CursorShapeInfo& cursor_shape) { | |
98 DCHECK(runtime_->display_task_runner()->BelongsToCurrentThread()); | |
99 | |
100 // const_cast<> is safe as long as the Java updateCursorShape() method copies | |
101 // the data out of the buffer without mutating it, and doesn't keep any | |
102 // reference to the buffer afterwards. Unfortunately, there seems to be no way | |
103 // to create a read-only ByteBuffer from a pointer-to-const. | |
104 char* data = | |
105 base::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_, cursor_shape.width(), cursor_shape.height(), | |
114 cursor_shape.hotspot_x(), cursor_shape.hotspot_y(), buffer); | |
115 } | |
116 | |
117 } // namespace remoting | |
OLD | NEW |