| 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/stl_util.h" |
| 11 #include "jni/Display_jni.h" |
| 12 #include "remoting/client/jni/chromoting_jni_runtime.h" |
| 13 #include "remoting/client/jni/jni_client.h" |
| 14 |
| 15 using base::android::JavaParamRef; |
| 16 |
| 17 namespace { |
| 18 |
| 19 const int kBytesPerPixel = 4; |
| 20 } |
| 21 |
| 22 namespace remoting { |
| 23 |
| 24 JniDisplayHandler::JniDisplayHandler( |
| 25 scoped_refptr<base::SingleThreadTaskRunner> display_runner, |
| 26 base::android::ScopedJavaGlobalRef<jobject> java_display) |
| 27 : display_runner_(display_runner), |
| 28 java_display_(java_display), |
| 29 weak_factory_(this) {} |
| 30 |
| 31 JniDisplayHandler::~JniDisplayHandler() { |
| 32 DCHECK(display_runner_->BelongsToCurrentThread()); |
| 33 } |
| 34 |
| 35 // static |
| 36 base::android::ScopedJavaLocalRef<jobject> JniDisplayHandler::NewBitmap( |
| 37 int width, |
| 38 int height) { |
| 39 JNIEnv* env = base::android::AttachCurrentThread(); |
| 40 return Java_Display_newBitmap(env, width, height); |
| 41 } |
| 42 |
| 43 void JniDisplayHandler::UpdateFrameBitmap( |
| 44 base::android::ScopedJavaGlobalRef<jobject> bitmap) { |
| 45 DCHECK(display_runner_->BelongsToCurrentThread()); |
| 46 |
| 47 JNIEnv* env = base::android::AttachCurrentThread(); |
| 48 Java_Display_setVideoFrame(env, java_display_.obj(), bitmap.obj()); |
| 49 } |
| 50 |
| 51 void JniDisplayHandler::UpdateCursorShape( |
| 52 const protocol::CursorShapeInfo& cursor_shape) { |
| 53 DCHECK(display_runner_->BelongsToCurrentThread()); |
| 54 |
| 55 // const_cast<> is safe as long as the Java updateCursorShape() method copies |
| 56 // the data out of the buffer without mutating it, and doesn't keep any |
| 57 // reference to the buffer afterwards. Unfortunately, there seems to be no way |
| 58 // to create a read-only ByteBuffer from a pointer-to-const. |
| 59 char* data = string_as_array(const_cast<std::string*>(&cursor_shape.data())); |
| 60 int cursor_total_bytes = |
| 61 cursor_shape.width() * cursor_shape.height() * kBytesPerPixel; |
| 62 |
| 63 JNIEnv* env = base::android::AttachCurrentThread(); |
| 64 base::android::ScopedJavaLocalRef<jobject> buffer( |
| 65 env, env->NewDirectByteBuffer(data, cursor_total_bytes)); |
| 66 Java_Display_updateCursorShape( |
| 67 env, java_display_.obj(), cursor_shape.width(), cursor_shape.height(), |
| 68 cursor_shape.hotspot_x(), cursor_shape.hotspot_y(), buffer.obj()); |
| 69 } |
| 70 |
| 71 void JniDisplayHandler::RedrawCanvas() { |
| 72 DCHECK(display_runner_->BelongsToCurrentThread()); |
| 73 |
| 74 JNIEnv* env = base::android::AttachCurrentThread(); |
| 75 Java_Display_redrawGraphicsInternal(env, java_display_.obj()); |
| 76 } |
| 77 |
| 78 base::WeakPtr<JniDisplayHandler> JniDisplayHandler::GetWeakPtr() { |
| 79 return weak_factory_.GetWeakPtr(); |
| 80 } |
| 81 |
| 82 // static |
| 83 bool JniDisplayHandler::RegisterJni(JNIEnv* env) { |
| 84 return RegisterNativesImpl(env); |
| 85 } |
| 86 |
| 87 void JniDisplayHandler::Destroy( |
| 88 JNIEnv* env, |
| 89 const base::android::JavaParamRef<jobject>& caller) { |
| 90 if (display_runner_->BelongsToCurrentThread()) { |
| 91 delete this; |
| 92 } else { |
| 93 display_runner_->DeleteSoon(FROM_HERE, this); |
| 94 } |
| 95 } |
| 96 |
| 97 void JniDisplayHandler::ScheduleRedraw( |
| 98 JNIEnv* env, |
| 99 const base::android::JavaParamRef<jobject>& caller) { |
| 100 display_runner_->PostTask( |
| 101 FROM_HERE, base::Bind(&JniDisplayHandler::RedrawCanvas, GetWeakPtr())); |
| 102 } |
| 103 |
| 104 static jlong Init(JNIEnv* env, const JavaParamRef<jobject>& caller) { |
| 105 return reinterpret_cast<intptr_t>(new JniDisplayHandler( |
| 106 ChromotingJniRuntime::GetInstance()->display_task_runner(), |
| 107 base::android::ScopedJavaGlobalRef<jobject>(env, caller))); |
| 108 } |
| 109 |
| 110 } // namespace remoting |
| OLD | NEW |