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 "base/android/jni_android.h" | 7 #include "base/android/jni_android.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/synchronization/waitable_event.h" | 9 #include "base/synchronization/waitable_event.h" |
10 #include "remoting/client/frame_producer.h" | 10 #include "remoting/client/frame_producer.h" |
11 #include "remoting/client/jni/chromoting_jni.h" | 11 #include "remoting/client/jni/chromoting_jni_runtime.h" |
12 #include "third_party/webrtc/modules/desktop_capture/desktop_frame.h" | 12 #include "third_party/webrtc/modules/desktop_capture/desktop_frame.h" |
13 | 13 |
14 namespace { | 14 namespace { |
15 | 15 |
16 // Allocates its buffer within a Java direct byte buffer, where it can be | 16 // Allocates its buffer within a Java direct byte buffer, where it can be |
17 // accessed by both native and managed code. | 17 // accessed by both native and managed code. |
18 class DirectDesktopFrame : public webrtc::BasicDesktopFrame { | 18 class DirectDesktopFrame : public webrtc::BasicDesktopFrame { |
19 public: | 19 public: |
20 DirectDesktopFrame(int width, int height); | 20 DirectDesktopFrame(int width, int height); |
21 | 21 |
(...skipping 12 matching lines...) Expand all Loading... |
34 JNIEnv* env = base::android::AttachCurrentThread(); | 34 JNIEnv* env = base::android::AttachCurrentThread(); |
35 buffer_ = env->NewDirectByteBuffer(data(), stride()*height); | 35 buffer_ = env->NewDirectByteBuffer(data(), stride()*height); |
36 } | 36 } |
37 | 37 |
38 DirectDesktopFrame::~DirectDesktopFrame() {} | 38 DirectDesktopFrame::~DirectDesktopFrame() {} |
39 | 39 |
40 } // namespace | 40 } // namespace |
41 | 41 |
42 namespace remoting { | 42 namespace remoting { |
43 | 43 |
44 JniFrameConsumer::JniFrameConsumer() | 44 JniFrameConsumer::JniFrameConsumer(ChromotingJniRuntime* jni_runtime) |
45 : provide_buffer_(true), | 45 : jni_runtime_(jni_runtime), |
| 46 in_dtor_(false), |
46 frame_producer_(NULL) { | 47 frame_producer_(NULL) { |
47 } | 48 } |
48 | 49 |
49 JniFrameConsumer::~JniFrameConsumer() { | 50 JniFrameConsumer::~JniFrameConsumer() { |
50 // Stop giving the producer a buffer to work with. | 51 // Stop giving the producer a buffer to work with. |
51 provide_buffer_ = false; | 52 in_dtor_ = true; |
52 | 53 |
53 // Don't destroy the object until we've deleted the buffer. | 54 // Don't destroy the object until we've deleted the buffer. |
54 base::WaitableEvent done_event(true, false); | 55 base::WaitableEvent done_event(true, false); |
55 frame_producer_->RequestReturnBuffers( | 56 frame_producer_->RequestReturnBuffers( |
56 base::Bind(&base::WaitableEvent::Signal, base::Unretained(&done_event))); | 57 base::Bind(&base::WaitableEvent::Signal, base::Unretained(&done_event))); |
57 done_event.Wait(); | 58 done_event.Wait(); |
58 } | 59 } |
59 | 60 |
60 void JniFrameConsumer::set_frame_producer(FrameProducer* producer) { | 61 void JniFrameConsumer::set_frame_producer(FrameProducer* producer) { |
61 frame_producer_ = producer; | 62 frame_producer_ = producer; |
62 } | 63 } |
63 | 64 |
64 void JniFrameConsumer::ApplyBuffer(const SkISize& view_size, | 65 void JniFrameConsumer::ApplyBuffer(const SkISize& view_size, |
65 const SkIRect& clip_area, | 66 const SkIRect& clip_area, |
66 webrtc::DesktopFrame* buffer, | 67 webrtc::DesktopFrame* buffer, |
67 const SkRegion& region) { | 68 const SkRegion& region) { |
68 DCHECK(ChromotingJni::GetInstance()-> | 69 DCHECK(jni_runtime_->display_task_runner()->BelongsToCurrentThread()); |
69 display_task_runner()->BelongsToCurrentThread()); | |
70 | 70 |
71 ChromotingJni::GetInstance()->RedrawCanvas(); | 71 scoped_ptr<webrtc::DesktopFrame> buffer_scoped(buffer); |
| 72 jni_runtime_->RedrawCanvas(); |
72 | 73 |
73 if (view_size.width() > view_size_.width() || | 74 if (view_size.width() > view_size_.width() || |
74 view_size.height() > view_size_.height()) { | 75 view_size.height() > view_size_.height()) { |
75 LOG(INFO) << "Existing buffer is too small"; | 76 LOG(INFO) << "Existing buffer is too small"; |
76 view_size_ = view_size; | 77 view_size_ = view_size; |
77 delete buffer; | 78 |
| 79 // Manually destroy the old buffer before allocating a new one to prevent |
| 80 // our memory footprint from temporarily ballooning. |
| 81 buffer_scoped.reset(); |
78 AllocateBuffer(); | 82 AllocateBuffer(); |
79 } | 83 } |
80 | 84 |
81 // Supply |frame_producer_| with a buffer to render the next frame into. | 85 // Supply |frame_producer_| with a buffer to render the next frame into. |
82 if (provide_buffer_) | 86 if (!in_dtor_) |
83 frame_producer_->DrawBuffer(buffer); | 87 frame_producer_->DrawBuffer(buffer_scoped.release()); |
84 } | 88 } |
85 | 89 |
86 void JniFrameConsumer::ReturnBuffer(webrtc::DesktopFrame* buffer) { | 90 void JniFrameConsumer::ReturnBuffer(webrtc::DesktopFrame* buffer) { |
87 DCHECK(ChromotingJni::GetInstance()-> | 91 DCHECK(jni_runtime_->display_task_runner()->BelongsToCurrentThread()); |
88 display_task_runner()->BelongsToCurrentThread()); | |
89 LOG(INFO) << "Returning image buffer"; | 92 LOG(INFO) << "Returning image buffer"; |
90 delete buffer; | 93 delete buffer; |
91 } | 94 } |
92 | 95 |
93 void JniFrameConsumer::SetSourceSize(const SkISize& source_size, | 96 void JniFrameConsumer::SetSourceSize(const SkISize& source_size, |
94 const SkIPoint& dpi) { | 97 const SkIPoint& dpi) { |
95 DCHECK(ChromotingJni::GetInstance()-> | 98 DCHECK(jni_runtime_->display_task_runner()->BelongsToCurrentThread()); |
96 display_task_runner()->BelongsToCurrentThread()); | |
97 | 99 |
98 // We currently render the desktop 1:1 and perform pan/zoom scaling | 100 // We currently render the desktop 1:1 and perform pan/zoom scaling |
99 // and cropping on the managed canvas. | 101 // and cropping on the managed canvas. |
100 view_size_ = source_size; | 102 view_size_ = source_size; |
101 clip_area_ = SkIRect::MakeSize(view_size_); | 103 clip_area_ = SkIRect::MakeSize(view_size_); |
102 frame_producer_->SetOutputSizeAndClip(view_size_, clip_area_); | 104 frame_producer_->SetOutputSizeAndClip(view_size_, clip_area_); |
103 | 105 |
104 // Unless being destructed, allocate buffer and start drawing frames onto it. | 106 // Unless being destructed, allocate buffer and start drawing frames onto it. |
105 frame_producer_->RequestReturnBuffers(base::Bind( | 107 frame_producer_->RequestReturnBuffers(base::Bind( |
106 &JniFrameConsumer::AllocateBuffer, base::Unretained(this))); | 108 &JniFrameConsumer::AllocateBuffer, base::Unretained(this))); |
107 } | 109 } |
108 | 110 |
109 void JniFrameConsumer::AllocateBuffer() { | 111 void JniFrameConsumer::AllocateBuffer() { |
110 // Only do anything if we're not being destructed. | 112 // Only do anything if we're not being destructed. |
111 if (provide_buffer_) { | 113 if (!in_dtor_) { |
112 if (!ChromotingJni::GetInstance()-> | 114 if (!jni_runtime_->display_task_runner()->BelongsToCurrentThread()) { |
113 display_task_runner()->BelongsToCurrentThread()) { | 115 jni_runtime_->display_task_runner()->PostTask(FROM_HERE, |
114 ChromotingJni::GetInstance()->display_task_runner()->PostTask(FROM_HERE, | |
115 base::Bind(&JniFrameConsumer::AllocateBuffer, | 116 base::Bind(&JniFrameConsumer::AllocateBuffer, |
116 base::Unretained(this))); | 117 base::Unretained(this))); |
117 return; | 118 return; |
118 } | 119 } |
119 | 120 |
120 DirectDesktopFrame* buffer = new DirectDesktopFrame(view_size_.width(), | 121 DirectDesktopFrame* buffer = new DirectDesktopFrame(view_size_.width(), |
121 view_size_.height()); | 122 view_size_.height()); |
122 | 123 |
123 // Update Java's reference to the buffer and record of its dimensions. | 124 // Update Java's reference to the buffer and record of its dimensions. |
124 ChromotingJni::GetInstance()->UpdateImageBuffer( | 125 jni_runtime_->UpdateImageBuffer(view_size_.width(), |
125 view_size_.width(), | 126 view_size_.height(), |
126 view_size_.height(), | 127 buffer->buffer()); |
127 buffer->buffer()); | |
128 | 128 |
129 frame_producer_->DrawBuffer(buffer); | 129 frame_producer_->DrawBuffer(buffer); |
130 } | 130 } |
131 } | 131 } |
132 | 132 |
133 } // namespace remoting | 133 } // namespace remoting |
OLD | NEW |