| 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( | 50 void JniDisplayHandler::SetCursorShape( |
| 77 const protocol::CursorShapeInfo& cursor_shape) { | 51 const protocol::CursorShapeInfo& cursor_shape) { |
| 78 DCHECK(runtime_->display_task_runner()->BelongsToCurrentThread()); | 52 DCHECK(runtime_->display_task_runner()->BelongsToCurrentThread()); |
| 79 | 53 |
| 80 // const_cast<> is safe as long as the Java updateCursorShape() method copies | 54 // 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 | 55 // 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 | 56 // reference to the buffer afterwards. Unfortunately, there seems to be no way |
| 83 // to create a read-only ByteBuffer from a pointer-to-const. | 57 // to create a read-only ByteBuffer from a pointer-to-const. |
| 84 char* data = string_as_array(const_cast<std::string*>(&cursor_shape.data())); | 58 char* data = string_as_array(const_cast<std::string*>(&cursor_shape.data())); |
| 85 int cursor_total_bytes = | 59 int cursor_total_bytes = |
| 86 cursor_shape.width() * cursor_shape.height() * kBytesPerPixel; | 60 cursor_shape.width() * cursor_shape.height() * kBytesPerPixel; |
| 87 | 61 |
| 88 JNIEnv* env = base::android::AttachCurrentThread(); | 62 JNIEnv* env = base::android::AttachCurrentThread(); |
| 89 base::android::ScopedJavaLocalRef<jobject> buffer( | 63 base::android::ScopedJavaLocalRef<jobject> buffer( |
| 90 env, env->NewDirectByteBuffer(data, cursor_total_bytes)); | 64 env, env->NewDirectByteBuffer(data, cursor_total_bytes)); |
| 91 Java_Display_updateCursorShape( | 65 Java_Display_updateCursorShape( |
| 92 env, java_display_.obj(), cursor_shape.width(), cursor_shape.height(), | 66 env, java_display_.obj(), cursor_shape.width(), cursor_shape.height(), |
| 93 cursor_shape.hotspot_x(), cursor_shape.hotspot_y(), buffer.obj()); | 67 cursor_shape.hotspot_x(), cursor_shape.hotspot_y(), buffer.obj()); |
| 94 } | 68 } |
| 95 | 69 |
| 96 std::unique_ptr<protocol::CursorShapeStub> | 70 std::unique_ptr<protocol::CursorShapeStub> |
| 97 JniDisplayHandler::CreateCursorShapeStub() { | 71 JniDisplayHandler::CreateCursorShapeStub() { |
| 98 return base::WrapUnique( | 72 return base::WrapUnique(new CursorShapeStubProxy( |
| 99 new DisplayCursorShapeStub(weak_ptr_, runtime_->display_task_runner())); | 73 weak_ptr_, runtime_->display_task_runner())); |
| 100 } | 74 } |
| 101 | 75 |
| 102 std::unique_ptr<protocol::VideoRenderer> | 76 std::unique_ptr<protocol::VideoRenderer> |
| 103 JniDisplayHandler::CreateVideoRenderer() { | 77 JniDisplayHandler::CreateVideoRenderer() { |
| 104 return base::WrapUnique( | 78 return base::WrapUnique( |
| 105 new JniVideoRenderer(runtime_, weak_ptr_)); | 79 new JniVideoRenderer(runtime_, weak_ptr_)); |
| 106 } | 80 } |
| 107 | 81 |
| 108 // static | 82 // static |
| 109 base::android::ScopedJavaLocalRef<jobject> JniDisplayHandler::NewBitmap( | 83 base::android::ScopedJavaLocalRef<jobject> JniDisplayHandler::NewBitmap( |
| (...skipping 24 matching lines...) Expand all Loading... |
| 134 } | 108 } |
| 135 | 109 |
| 136 void JniDisplayHandler::ScheduleRedraw( | 110 void JniDisplayHandler::ScheduleRedraw( |
| 137 JNIEnv* env, | 111 JNIEnv* env, |
| 138 const base::android::JavaParamRef<jobject>& caller) { | 112 const base::android::JavaParamRef<jobject>& caller) { |
| 139 runtime_->display_task_runner()->PostTask( | 113 runtime_->display_task_runner()->PostTask( |
| 140 FROM_HERE, base::Bind(&JniDisplayHandler::RedrawCanvas, weak_ptr_)); | 114 FROM_HERE, base::Bind(&JniDisplayHandler::RedrawCanvas, weak_ptr_)); |
| 141 } | 115 } |
| 142 | 116 |
| 143 } // namespace remoting | 117 } // namespace remoting |
| OLD | NEW |