| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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_frame_consumer.h" | 5 #include "remoting/client/jni/jni_frame_consumer.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include "base/android/jni_android.h" | 9 #include "base/android/jni_android.h" |
| 10 #include "base/android/scoped_java_ref.h" | 10 #include "base/android/scoped_java_ref.h" |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/memory/ptr_util.h" | 12 #include "base/memory/ptr_util.h" |
| 13 #include "remoting/base/util.h" | 13 #include "remoting/base/util.h" |
| 14 #include "remoting/client/jni/chromoting_jni_instance.h" | 14 #include "remoting/client/jni/chromoting_jni_instance.h" |
| 15 #include "remoting/client/jni/chromoting_jni_runtime.h" | 15 #include "remoting/client/jni/chromoting_jni_runtime.h" |
| 16 #include "remoting/client/jni/jni_client.h" | |
| 17 #include "third_party/webrtc/modules/desktop_capture/desktop_frame.h" | 16 #include "third_party/webrtc/modules/desktop_capture/desktop_frame.h" |
| 18 #include "third_party/webrtc/modules/desktop_capture/desktop_region.h" | 17 #include "third_party/webrtc/modules/desktop_capture/desktop_region.h" |
| 19 #include "ui/gfx/android/java_bitmap.h" | 18 #include "ui/gfx/android/java_bitmap.h" |
| 20 | 19 |
| 21 namespace remoting { | 20 namespace remoting { |
| 22 | 21 |
| 23 class JniFrameConsumer::Renderer { | 22 class JniFrameConsumer::Renderer { |
| 24 public: | 23 public: |
| 25 Renderer(ChromotingJniRuntime* jni_runtime, JniClient* jni_client) : | 24 Renderer(ChromotingJniRuntime* jni_runtime) : jni_runtime_(jni_runtime) {} |
| 26 jni_runtime_(jni_runtime), | |
| 27 jni_client_(jni_client) {} | |
| 28 ~Renderer() { | 25 ~Renderer() { |
| 29 DCHECK(jni_runtime_->display_task_runner()->BelongsToCurrentThread()); | 26 DCHECK(jni_runtime_->display_task_runner()->BelongsToCurrentThread()); |
| 30 } | 27 } |
| 31 | 28 |
| 32 void RenderFrame(std::unique_ptr<webrtc::DesktopFrame> frame); | 29 void RenderFrame(std::unique_ptr<webrtc::DesktopFrame> frame); |
| 33 | 30 |
| 34 private: | 31 private: |
| 35 // Used to obtain task runner references and make calls to Java methods. | 32 // Used to obtain task runner references and make calls to Java methods. |
| 36 ChromotingJniRuntime* jni_runtime_; | 33 ChromotingJniRuntime* jni_runtime_; |
| 37 | 34 |
| 38 JniClient* jni_client_; | |
| 39 | |
| 40 // This global reference is required, instead of a local reference, so it | 35 // This global reference is required, instead of a local reference, so it |
| 41 // remains valid for the lifetime of |bitmap_| - gfx::JavaBitmap does not | 36 // remains valid for the lifetime of |bitmap_| - gfx::JavaBitmap does not |
| 42 // create its own global reference internally. And this global ref must be | 37 // create its own global reference internally. And this global ref must be |
| 43 // destroyed (released) after |bitmap_| is destroyed. | 38 // destroyed (released) after |bitmap_| is destroyed. |
| 44 base::android::ScopedJavaGlobalRef<jobject> bitmap_global_ref_; | 39 base::android::ScopedJavaGlobalRef<jobject> bitmap_global_ref_; |
| 45 | 40 |
| 46 // Reference to the frame bitmap that is passed to Java when the frame is | 41 // Reference to the frame bitmap that is passed to Java when the frame is |
| 47 // allocated. This provides easy access to the underlying pixels. | 42 // allocated. This provides easy access to the underlying pixels. |
| 48 std::unique_ptr<gfx::JavaBitmap> bitmap_; | 43 std::unique_ptr<gfx::JavaBitmap> bitmap_; |
| 49 }; | 44 }; |
| 50 | 45 |
| 51 // Function called on the display thread to render the frame. | 46 // Function called on the display thread to render the frame. |
| 52 void JniFrameConsumer::Renderer::RenderFrame( | 47 void JniFrameConsumer::Renderer::RenderFrame( |
| 53 std::unique_ptr<webrtc::DesktopFrame> frame) { | 48 std::unique_ptr<webrtc::DesktopFrame> frame) { |
| 54 DCHECK(jni_runtime_->display_task_runner()->BelongsToCurrentThread()); | 49 DCHECK(jni_runtime_->display_task_runner()->BelongsToCurrentThread()); |
| 55 | 50 |
| 56 if (!bitmap_ || bitmap_->size().width() != frame->size().width() || | 51 if (!bitmap_ || bitmap_->size().width() != frame->size().width() || |
| 57 bitmap_->size().height() != frame->size().height()) { | 52 bitmap_->size().height() != frame->size().height()) { |
| 58 // Allocate a new Bitmap, store references here, and pass it to Java. | 53 // Allocate a new Bitmap, store references here, and pass it to Java. |
| 59 JNIEnv* env = base::android::AttachCurrentThread(); | 54 JNIEnv* env = base::android::AttachCurrentThread(); |
| 60 | 55 |
| 61 // |bitmap_| must be deleted before |bitmap_global_ref_| is released. | 56 // |bitmap_| must be deleted before |bitmap_global_ref_| is released. |
| 62 bitmap_.reset(); | 57 bitmap_.reset(); |
| 63 bitmap_global_ref_.Reset( | 58 bitmap_global_ref_.Reset( |
| 64 env, | 59 env, |
| 65 jni_client_->NewBitmap(frame->size().width(), frame->size().height()) | 60 jni_runtime_->NewBitmap(frame->size().width(), frame->size().height()) |
| 66 .obj()); | 61 .obj()); |
| 67 bitmap_.reset(new gfx::JavaBitmap(bitmap_global_ref_.obj())); | 62 bitmap_.reset(new gfx::JavaBitmap(bitmap_global_ref_.obj())); |
| 68 jni_client_->UpdateFrameBitmap(bitmap_global_ref_.obj()); | 63 jni_runtime_->UpdateFrameBitmap(bitmap_global_ref_.obj()); |
| 69 } | 64 } |
| 70 | 65 |
| 71 // Copy pixels from |frame| into the Java Bitmap. | 66 // Copy pixels from |frame| into the Java Bitmap. |
| 72 // TODO(lambroslambrou): Optimize away this copy by having the VideoDecoder | 67 // TODO(lambroslambrou): Optimize away this copy by having the VideoDecoder |
| 73 // decode directly into the Bitmap's pixel memory. This currently doesn't | 68 // decode directly into the Bitmap's pixel memory. This currently doesn't |
| 74 // work very well because the VideoDecoder writes the decoded data in BGRA, | 69 // work very well because the VideoDecoder writes the decoded data in BGRA, |
| 75 // and then the R/B channels are swapped in place (on the decoding thread). | 70 // and then the R/B channels are swapped in place (on the decoding thread). |
| 76 // If a repaint is triggered from a Java event handler, the unswapped pixels | 71 // If a repaint is triggered from a Java event handler, the unswapped pixels |
| 77 // can sometimes appear on the display. | 72 // can sometimes appear on the display. |
| 78 uint8_t* dest_buffer = static_cast<uint8_t*>(bitmap_->pixels()); | 73 uint8_t* dest_buffer = static_cast<uint8_t*>(bitmap_->pixels()); |
| 79 webrtc::DesktopRect buffer_rect = | 74 webrtc::DesktopRect buffer_rect = |
| 80 webrtc::DesktopRect::MakeSize(frame->size()); | 75 webrtc::DesktopRect::MakeSize(frame->size()); |
| 81 for (webrtc::DesktopRegion::Iterator i(frame->updated_region()); !i.IsAtEnd(); | 76 for (webrtc::DesktopRegion::Iterator i(frame->updated_region()); !i.IsAtEnd(); |
| 82 i.Advance()) { | 77 i.Advance()) { |
| 83 CopyRGB32Rect(frame->data(), frame->stride(), buffer_rect, dest_buffer, | 78 CopyRGB32Rect(frame->data(), frame->stride(), buffer_rect, dest_buffer, |
| 84 bitmap_->stride(), buffer_rect, i.rect()); | 79 bitmap_->stride(), buffer_rect, i.rect()); |
| 85 } | 80 } |
| 86 | 81 |
| 87 jni_client_->RedrawCanvas(); | 82 jni_runtime_->RedrawCanvas(); |
| 88 } | 83 } |
| 89 | 84 |
| 90 JniFrameConsumer::JniFrameConsumer(ChromotingJniRuntime* jni_runtime, | 85 JniFrameConsumer::JniFrameConsumer(ChromotingJniRuntime* jni_runtime) |
| 91 JniClient* jni_client) | |
| 92 : jni_runtime_(jni_runtime), | 86 : jni_runtime_(jni_runtime), |
| 93 renderer_(new Renderer(jni_runtime, jni_client)), | 87 renderer_(new Renderer(jni_runtime)), |
| 94 weak_factory_(this) {} | 88 weak_factory_(this) {} |
| 95 | 89 |
| 96 JniFrameConsumer::~JniFrameConsumer() { | 90 JniFrameConsumer::~JniFrameConsumer() { |
| 97 jni_runtime_->display_task_runner()->DeleteSoon(FROM_HERE, | 91 jni_runtime_->display_task_runner()->DeleteSoon(FROM_HERE, |
| 98 renderer_.release()); | 92 renderer_.release()); |
| 99 } | 93 } |
| 100 | 94 |
| 101 std::unique_ptr<webrtc::DesktopFrame> JniFrameConsumer::AllocateFrame( | 95 std::unique_ptr<webrtc::DesktopFrame> JniFrameConsumer::AllocateFrame( |
| 102 const webrtc::DesktopSize& size) { | 96 const webrtc::DesktopSize& size) { |
| 103 return base::WrapUnique(new webrtc::BasicDesktopFrame(size)); | 97 return base::WrapUnique(new webrtc::BasicDesktopFrame(size)); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 120 | 114 |
| 121 if (!done.is_null()) | 115 if (!done.is_null()) |
| 122 done.Run(); | 116 done.Run(); |
| 123 } | 117 } |
| 124 | 118 |
| 125 protocol::FrameConsumer::PixelFormat JniFrameConsumer::GetPixelFormat() { | 119 protocol::FrameConsumer::PixelFormat JniFrameConsumer::GetPixelFormat() { |
| 126 return FORMAT_RGBA; | 120 return FORMAT_RGBA; |
| 127 } | 121 } |
| 128 | 122 |
| 129 } // namespace remoting | 123 } // namespace remoting |
| OLD | NEW |