| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/rectangle_update_decoder.h" | 5 #include "remoting/client/rectangle_update_decoder.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/callback.h" | 8 #include "base/callback.h" |
| 9 #include "base/callback_helpers.h" | 9 #include "base/callback_helpers.h" |
| 10 #include "base/location.h" | 10 #include "base/location.h" |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 | 24 |
| 25 namespace remoting { | 25 namespace remoting { |
| 26 | 26 |
| 27 RectangleUpdateDecoder::RectangleUpdateDecoder( | 27 RectangleUpdateDecoder::RectangleUpdateDecoder( |
| 28 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner, | 28 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner, |
| 29 scoped_refptr<base::SingleThreadTaskRunner> decode_task_runner, | 29 scoped_refptr<base::SingleThreadTaskRunner> decode_task_runner, |
| 30 scoped_refptr<FrameConsumerProxy> consumer) | 30 scoped_refptr<FrameConsumerProxy> consumer) |
| 31 : main_task_runner_(main_task_runner), | 31 : main_task_runner_(main_task_runner), |
| 32 decode_task_runner_(decode_task_runner), | 32 decode_task_runner_(decode_task_runner), |
| 33 consumer_(consumer), | 33 consumer_(consumer), |
| 34 source_size_(SkISize::Make(0, 0)), | |
| 35 source_dpi_(SkIPoint::Make(0, 0)), | |
| 36 view_size_(SkISize::Make(0, 0)), | |
| 37 clip_area_(SkIRect::MakeEmpty()), | |
| 38 paint_scheduled_(false), | 34 paint_scheduled_(false), |
| 39 latest_sequence_number_(0) { | 35 latest_sequence_number_(0) { |
| 40 } | 36 } |
| 41 | 37 |
| 42 RectangleUpdateDecoder::~RectangleUpdateDecoder() { | 38 RectangleUpdateDecoder::~RectangleUpdateDecoder() { |
| 43 } | 39 } |
| 44 | 40 |
| 45 void RectangleUpdateDecoder::Initialize(const SessionConfig& config) { | 41 void RectangleUpdateDecoder::Initialize(const SessionConfig& config) { |
| 46 // Initialize decoder based on the selected codec. | 42 // Initialize decoder based on the selected codec. |
| 47 ChannelConfig::Codec codec = config.video_config().codec; | 43 ChannelConfig::Codec codec = config.video_config().codec; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 59 DCHECK(decode_task_runner_->BelongsToCurrentThread()); | 55 DCHECK(decode_task_runner_->BelongsToCurrentThread()); |
| 60 | 56 |
| 61 base::ScopedClosureRunner done_runner(done); | 57 base::ScopedClosureRunner done_runner(done); |
| 62 | 58 |
| 63 bool decoder_needs_reset = false; | 59 bool decoder_needs_reset = false; |
| 64 bool notify_size_or_dpi_change = false; | 60 bool notify_size_or_dpi_change = false; |
| 65 | 61 |
| 66 // If the packet includes screen size or DPI information, store them. | 62 // If the packet includes screen size or DPI information, store them. |
| 67 if (packet->format().has_screen_width() && | 63 if (packet->format().has_screen_width() && |
| 68 packet->format().has_screen_height()) { | 64 packet->format().has_screen_height()) { |
| 69 SkISize source_size = SkISize::Make(packet->format().screen_width(), | 65 webrtc::DesktopSize source_size(packet->format().screen_width(), |
| 70 packet->format().screen_height()); | 66 packet->format().screen_height()); |
| 71 if (source_size_ != source_size) { | 67 if (!source_size_.equals(source_size)) { |
| 72 source_size_ = source_size; | 68 source_size_ = source_size; |
| 73 decoder_needs_reset = true; | 69 decoder_needs_reset = true; |
| 74 notify_size_or_dpi_change = true; | 70 notify_size_or_dpi_change = true; |
| 75 } | 71 } |
| 76 } | 72 } |
| 77 if (packet->format().has_x_dpi() && packet->format().has_y_dpi()) { | 73 if (packet->format().has_x_dpi() && packet->format().has_y_dpi()) { |
| 78 SkIPoint source_dpi(SkIPoint::Make(packet->format().x_dpi(), | 74 webrtc::DesktopVector source_dpi(packet->format().x_dpi(), |
| 79 packet->format().y_dpi())); | 75 packet->format().y_dpi()); |
| 80 if (source_dpi != source_dpi_) { | 76 if (!source_dpi.equals(source_dpi_)) { |
| 81 source_dpi_ = source_dpi; | 77 source_dpi_ = source_dpi; |
| 82 notify_size_or_dpi_change = true; | 78 notify_size_or_dpi_change = true; |
| 83 } | 79 } |
| 84 } | 80 } |
| 85 | 81 |
| 86 // If we've never seen a screen size, ignore the packet. | 82 // If we've never seen a screen size, ignore the packet. |
| 87 if (source_size_.isZero()) | 83 if (source_size_.is_empty()) |
| 88 return; | 84 return; |
| 89 | 85 |
| 90 if (decoder_needs_reset) | 86 if (decoder_needs_reset) |
| 91 decoder_->Initialize(source_size_); | 87 decoder_->Initialize(source_size_); |
| 92 if (notify_size_or_dpi_change) | 88 if (notify_size_or_dpi_change) |
| 93 consumer_->SetSourceSize(source_size_, source_dpi_); | 89 consumer_->SetSourceSize(source_size_, source_dpi_); |
| 94 | 90 |
| 95 if (!decoder_->IsReadyForData()) { | 91 if (!decoder_->IsReadyForData()) { |
| 96 // TODO(ajwong): This whole thing should move into an invalid state. | 92 // TODO(ajwong): This whole thing should move into an invalid state. |
| 97 LOG(ERROR) << "Decoder is unable to process data. Dropping packet."; | 93 LOG(ERROR) << "Decoder is unable to process data. Dropping packet."; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 108 paint_scheduled_ = true; | 104 paint_scheduled_ = true; |
| 109 decode_task_runner_->PostTask( | 105 decode_task_runner_->PostTask( |
| 110 FROM_HERE, base::Bind(&RectangleUpdateDecoder::DoPaint, this)); | 106 FROM_HERE, base::Bind(&RectangleUpdateDecoder::DoPaint, this)); |
| 111 } | 107 } |
| 112 | 108 |
| 113 void RectangleUpdateDecoder::DoPaint() { | 109 void RectangleUpdateDecoder::DoPaint() { |
| 114 DCHECK(paint_scheduled_); | 110 DCHECK(paint_scheduled_); |
| 115 paint_scheduled_ = false; | 111 paint_scheduled_ = false; |
| 116 | 112 |
| 117 // If the view size is empty or we have no output buffers ready, return. | 113 // If the view size is empty or we have no output buffers ready, return. |
| 118 if (buffers_.empty() || view_size_.isEmpty()) | 114 if (buffers_.empty() || view_size_.is_empty()) |
| 119 return; | 115 return; |
| 120 | 116 |
| 121 // If no Decoder is initialized, or the host dimensions are empty, return. | 117 // If no Decoder is initialized, or the host dimensions are empty, return. |
| 122 if (!decoder_.get() || source_size_.isEmpty()) | 118 if (!decoder_.get() || source_size_.is_empty()) |
| 123 return; | 119 return; |
| 124 | 120 |
| 125 // Draw the invalidated region to the buffer. | 121 // Draw the invalidated region to the buffer. |
| 126 webrtc::DesktopFrame* buffer = buffers_.front(); | 122 webrtc::DesktopFrame* buffer = buffers_.front(); |
| 127 SkRegion output_region; | 123 webrtc::DesktopRegion output_region; |
| 128 decoder_->RenderFrame(view_size_, clip_area_, | 124 decoder_->RenderFrame(view_size_, clip_area_, |
| 129 buffer->data(), | 125 buffer->data(), |
| 130 buffer->stride(), | 126 buffer->stride(), |
| 131 &output_region); | 127 &output_region); |
| 132 | 128 |
| 133 // Notify the consumer that painting is done. | 129 // Notify the consumer that painting is done. |
| 134 if (!output_region.isEmpty()) { | 130 if (!output_region.is_empty()) { |
| 135 buffers_.pop_front(); | 131 buffers_.pop_front(); |
| 136 consumer_->ApplyBuffer(view_size_, clip_area_, buffer, output_region); | 132 consumer_->ApplyBuffer(view_size_, clip_area_, buffer, output_region); |
| 137 } | 133 } |
| 138 } | 134 } |
| 139 | 135 |
| 140 void RectangleUpdateDecoder::RequestReturnBuffers(const base::Closure& done) { | 136 void RectangleUpdateDecoder::RequestReturnBuffers(const base::Closure& done) { |
| 141 if (!decode_task_runner_->BelongsToCurrentThread()) { | 137 if (!decode_task_runner_->BelongsToCurrentThread()) { |
| 142 decode_task_runner_->PostTask( | 138 decode_task_runner_->PostTask( |
| 143 FROM_HERE, base::Bind(&RectangleUpdateDecoder::RequestReturnBuffers, | 139 FROM_HERE, base::Bind(&RectangleUpdateDecoder::RequestReturnBuffers, |
| 144 this, done)); | 140 this, done)); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 162 return; | 158 return; |
| 163 } | 159 } |
| 164 | 160 |
| 165 DCHECK(clip_area_.width() <= buffer->size().width() && | 161 DCHECK(clip_area_.width() <= buffer->size().width() && |
| 166 clip_area_.height() <= buffer->size().height()); | 162 clip_area_.height() <= buffer->size().height()); |
| 167 | 163 |
| 168 buffers_.push_back(buffer); | 164 buffers_.push_back(buffer); |
| 169 SchedulePaint(); | 165 SchedulePaint(); |
| 170 } | 166 } |
| 171 | 167 |
| 172 void RectangleUpdateDecoder::InvalidateRegion(const SkRegion& region) { | 168 void RectangleUpdateDecoder::InvalidateRegion( |
| 169 const webrtc::DesktopRegion& region) { |
| 173 if (!decode_task_runner_->BelongsToCurrentThread()) { | 170 if (!decode_task_runner_->BelongsToCurrentThread()) { |
| 174 decode_task_runner_->PostTask( | 171 decode_task_runner_->PostTask( |
| 175 FROM_HERE, base::Bind(&RectangleUpdateDecoder::InvalidateRegion, | 172 FROM_HERE, base::Bind(&RectangleUpdateDecoder::InvalidateRegion, |
| 176 this, region)); | 173 this, region)); |
| 177 return; | 174 return; |
| 178 } | 175 } |
| 179 | 176 |
| 180 if (decoder_.get()) { | 177 if (decoder_.get()) { |
| 181 decoder_->Invalidate(view_size_, region); | 178 decoder_->Invalidate(view_size_, region); |
| 182 SchedulePaint(); | 179 SchedulePaint(); |
| 183 } | 180 } |
| 184 } | 181 } |
| 185 | 182 |
| 186 void RectangleUpdateDecoder::SetOutputSizeAndClip(const SkISize& view_size, | 183 void RectangleUpdateDecoder::SetOutputSizeAndClip( |
| 187 const SkIRect& clip_area) { | 184 const webrtc::DesktopSize& view_size, |
| 185 const webrtc::DesktopRect& clip_area) { |
| 188 if (!decode_task_runner_->BelongsToCurrentThread()) { | 186 if (!decode_task_runner_->BelongsToCurrentThread()) { |
| 189 decode_task_runner_->PostTask( | 187 decode_task_runner_->PostTask( |
| 190 FROM_HERE, base::Bind(&RectangleUpdateDecoder::SetOutputSizeAndClip, | 188 FROM_HERE, base::Bind(&RectangleUpdateDecoder::SetOutputSizeAndClip, |
| 191 this, view_size, clip_area)); | 189 this, view_size, clip_area)); |
| 192 return; | 190 return; |
| 193 } | 191 } |
| 194 | 192 |
| 195 // The whole frame needs to be repainted if the scaling factor has changed. | 193 // The whole frame needs to be repainted if the scaling factor has changed. |
| 196 if (view_size_ != view_size && decoder_.get()) { | 194 if (!view_size_.equals(view_size) && decoder_.get()) { |
| 197 SkRegion region; | 195 webrtc::DesktopRegion region; |
| 198 region.op(SkIRect::MakeSize(view_size), SkRegion::kUnion_Op); | 196 region.AddRect(webrtc::DesktopRect::MakeSize(view_size)); |
| 199 decoder_->Invalidate(view_size, region); | 197 decoder_->Invalidate(view_size, region); |
| 200 } | 198 } |
| 201 | 199 |
| 202 if (view_size_ != view_size || | 200 if (!view_size_.equals(view_size) || |
| 203 clip_area_ != clip_area) { | 201 !clip_area_.equals(clip_area)) { |
| 204 view_size_ = view_size; | 202 view_size_ = view_size; |
| 205 clip_area_ = clip_area; | 203 clip_area_ = clip_area; |
| 206 | 204 |
| 207 // Return buffers that are smaller than needed to the consumer for | 205 // Return buffers that are smaller than needed to the consumer for |
| 208 // reuse/reallocation. | 206 // reuse/reallocation. |
| 209 std::list<webrtc::DesktopFrame*>::iterator i = buffers_.begin(); | 207 std::list<webrtc::DesktopFrame*>::iterator i = buffers_.begin(); |
| 210 while (i != buffers_.end()) { | 208 while (i != buffers_.end()) { |
| 211 if ((*i)->size().width() < clip_area_.width() || | 209 if ((*i)->size().width() < clip_area_.width() || |
| 212 (*i)->size().height() < clip_area_.height()) { | 210 (*i)->size().height() < clip_area_.height()) { |
| 213 consumer_->ReturnBuffer(*i); | 211 consumer_->ReturnBuffer(*i); |
| 214 i = buffers_.erase(i); | 212 i = buffers_.erase(i); |
| 215 } else { | 213 } else { |
| 216 ++i; | 214 ++i; |
| 217 } | 215 } |
| 218 } | 216 } |
| 219 | 217 |
| 220 SchedulePaint(); | 218 SchedulePaint(); |
| 221 } | 219 } |
| 222 } | 220 } |
| 223 | 221 |
| 224 const SkRegion* RectangleUpdateDecoder::GetBufferShape() { | 222 const webrtc::DesktopRegion* RectangleUpdateDecoder::GetBufferShape() { |
| 225 return decoder_->GetImageShape(); | 223 return decoder_->GetImageShape(); |
| 226 } | 224 } |
| 227 | 225 |
| 228 void RectangleUpdateDecoder::ProcessVideoPacket(scoped_ptr<VideoPacket> packet, | 226 void RectangleUpdateDecoder::ProcessVideoPacket(scoped_ptr<VideoPacket> packet, |
| 229 const base::Closure& done) { | 227 const base::Closure& done) { |
| 230 DCHECK(main_task_runner_->BelongsToCurrentThread()); | 228 DCHECK(main_task_runner_->BelongsToCurrentThread()); |
| 231 | 229 |
| 232 // If the video packet is empty then drop it. Empty packets are used to | 230 // If the video packet is empty then drop it. Empty packets are used to |
| 233 // maintain activity on the network. | 231 // maintain activity on the network. |
| 234 if (!packet->has_data() || packet->data().size() == 0) { | 232 if (!packet->has_data() || packet->data().size() == 0) { |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 280 | 278 |
| 281 done.Run(); | 279 done.Run(); |
| 282 } | 280 } |
| 283 | 281 |
| 284 ChromotingStats* RectangleUpdateDecoder::GetStats() { | 282 ChromotingStats* RectangleUpdateDecoder::GetStats() { |
| 285 DCHECK(main_task_runner_->BelongsToCurrentThread()); | 283 DCHECK(main_task_runner_->BelongsToCurrentThread()); |
| 286 return &stats_; | 284 return &stats_; |
| 287 } | 285 } |
| 288 | 286 |
| 289 } // namespace remoting | 287 } // namespace remoting |
| OLD | NEW |