Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "remoting/client/jni/jni_frame_consumer.h" | |
| 6 | |
| 7 #include "base/android/jni_android.h" | |
| 8 #include "base/logging.h" | |
| 9 #include "base/synchronization/waitable_event.h" | |
| 10 #include "remoting/client/frame_producer.h" | |
| 11 #include "remoting/client/jni/chromoting_jni.h" | |
| 12 #include "third_party/webrtc/modules/desktop_capture/desktop_frame.h" | |
| 13 | |
| 14 namespace { | |
| 15 | |
| 16 // Allocates its buffer within a Java direct byte buffer, where it can be | |
| 17 // accessed by both native and managed code. | |
| 18 class DirectDesktopFrame : public webrtc::BasicDesktopFrame { | |
| 19 public: | |
| 20 DirectDesktopFrame(int width, int height); | |
| 21 | |
| 22 virtual ~DirectDesktopFrame(); | |
| 23 | |
| 24 jobject buffer() const { | |
| 25 return buffer_; | |
| 26 } | |
| 27 | |
| 28 private: | |
| 29 jobject buffer_; | |
| 30 }; | |
| 31 | |
| 32 DirectDesktopFrame::DirectDesktopFrame(int width, int height) | |
| 33 : webrtc::BasicDesktopFrame(webrtc::DesktopSize(width, height)) { | |
| 34 JNIEnv* env = base::android::AttachCurrentThread(); | |
| 35 buffer_ = env->NewDirectByteBuffer(data(), stride()*height); | |
| 36 } | |
| 37 | |
| 38 DirectDesktopFrame::~DirectDesktopFrame() {} | |
| 39 | |
| 40 } // namespace | |
| 41 | |
| 42 namespace remoting { | |
| 43 | |
| 44 JniFrameConsumer::JniFrameConsumer() | |
| 45 : provide_buffer_(true), | |
|
Wez
2013/07/22 19:41:28
nit: This should be e.g. |shutting_down_| or |in_d
solb
2013/07/22 22:49:32
Done.
| |
| 46 frame_producer_(NULL) { | |
| 47 } | |
| 48 | |
| 49 JniFrameConsumer::~JniFrameConsumer() { | |
| 50 // Stop giving the producer a buffer to work with. | |
| 51 provide_buffer_ = false; | |
| 52 | |
| 53 // Don't destroy the object until we've deleted the buffer. | |
| 54 base::WaitableEvent done_event(true, false); | |
| 55 frame_producer_->RequestReturnBuffers( | |
| 56 base::Bind(&base::WaitableEvent::Signal, base::Unretained(&done_event))); | |
| 57 done_event.Wait(); | |
| 58 } | |
| 59 | |
| 60 void JniFrameConsumer::set_frame_producer(FrameProducer* producer) { | |
| 61 frame_producer_ = producer; | |
| 62 } | |
| 63 | |
| 64 void JniFrameConsumer::ApplyBuffer(const SkISize& view_size, | |
| 65 const SkIRect& clip_area, | |
| 66 webrtc::DesktopFrame* buffer, | |
| 67 const SkRegion& region) { | |
| 68 DCHECK(ChromotingJni::GetInstance()-> | |
| 69 display_task_runner()->BelongsToCurrentThread()); | |
| 70 | |
| 71 ChromotingJni::GetInstance()->RedrawCanvas(); | |
| 72 | |
| 73 if (view_size.width() > view_size_.width() || | |
| 74 view_size.height() > view_size_.height()) { | |
| 75 LOG(INFO) << "Existing buffer is too small"; | |
| 76 view_size_ = view_size; | |
| 77 delete buffer; | |
| 78 AllocateBuffer(); | |
| 79 } | |
| 80 | |
| 81 // Supply |frame_producer_| with a buffer to render the next frame into. | |
| 82 if (provide_buffer_) | |
|
Wez
2013/07/22 19:41:28
If |provide_buffer_| is false _and_ the view hasn'
solb
2013/07/22 22:49:32
Oops! Thanks for catching that.
| |
| 83 frame_producer_->DrawBuffer(buffer); | |
| 84 } | |
| 85 | |
| 86 void JniFrameConsumer::ReturnBuffer(webrtc::DesktopFrame* buffer) { | |
| 87 DCHECK(ChromotingJni::GetInstance()-> | |
| 88 display_task_runner()->BelongsToCurrentThread()); | |
| 89 LOG(INFO) << "Returning image buffer"; | |
| 90 delete buffer; | |
| 91 } | |
| 92 | |
| 93 void JniFrameConsumer::SetSourceSize(const SkISize& source_size, | |
| 94 const SkIPoint& dpi) { | |
| 95 DCHECK(ChromotingJni::GetInstance()-> | |
| 96 display_task_runner()->BelongsToCurrentThread()); | |
| 97 | |
| 98 // We currently render the desktop 1:1 and perform pan/zoom scaling | |
| 99 // and cropping on the managed canvas. | |
| 100 view_size_ = source_size; | |
| 101 clip_area_ = SkIRect::MakeSize(view_size_); | |
| 102 frame_producer_->SetOutputSizeAndClip(view_size_, clip_area_); | |
| 103 | |
| 104 // Unless being destructed, allocate buffer and start drawing frames onto it. | |
| 105 frame_producer_->RequestReturnBuffers(base::Bind( | |
| 106 &JniFrameConsumer::AllocateBuffer, base::Unretained(this))); | |
| 107 } | |
| 108 | |
| 109 void JniFrameConsumer::AllocateBuffer() { | |
| 110 // Only do anything if we're not being destructed. | |
| 111 if (provide_buffer_) { | |
| 112 if (!ChromotingJni::GetInstance()-> | |
| 113 display_task_runner()->BelongsToCurrentThread()) { | |
| 114 ChromotingJni::GetInstance()->display_task_runner()->PostTask(FROM_HERE, | |
| 115 base::Bind(&JniFrameConsumer::AllocateBuffer, | |
| 116 base::Unretained(this))); | |
| 117 return; | |
| 118 } | |
| 119 | |
| 120 DirectDesktopFrame* buffer = new DirectDesktopFrame(view_size_.width(), | |
| 121 view_size_.height()); | |
| 122 | |
| 123 // Update Java's reference to the buffer and record of its dimensions. | |
| 124 ChromotingJni::GetInstance()->UpdateImageBuffer( | |
| 125 view_size_.width(), | |
| 126 view_size_.height(), | |
| 127 buffer->buffer()); | |
| 128 | |
| 129 frame_producer_->DrawBuffer(buffer); | |
| 130 } | |
| 131 } | |
| 132 | |
| 133 } // namespace remoting | |
| OLD | NEW |