| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/plugin/pepper_view.h" | 5 #include "remoting/client/plugin/pepper_view.h" |
| 6 | 6 |
| 7 #include "base/message_loop.h" | 7 #include "base/message_loop.h" |
| 8 #include "base/string_util.h" | 8 #include "base/string_util.h" |
| 9 #include "ppapi/cpp/graphics_2d.h" | 9 #include "ppapi/cpp/graphics_2d.h" |
| 10 #include "ppapi/cpp/image_data.h" | 10 #include "ppapi/cpp/image_data.h" |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 67 // size! Otherwise, this will just silently do nothing. | 67 // size! Otherwise, this will just silently do nothing. |
| 68 graphics2d_.ReplaceContents(&image); | 68 graphics2d_.ReplaceContents(&image); |
| 69 FlushGraphics(base::Time::Now()); | 69 FlushGraphics(base::Time::Now()); |
| 70 } else { | 70 } else { |
| 71 // TODO(ajwong): We need to keep a backing store image of the host screen | 71 // TODO(ajwong): We need to keep a backing store image of the host screen |
| 72 // that has the data here which can be redrawn. | 72 // that has the data here which can be redrawn. |
| 73 return; | 73 return; |
| 74 } | 74 } |
| 75 } | 75 } |
| 76 | 76 |
| 77 void PepperView::SetHostSize(const gfx::Size& host_size) { | 77 void PepperView::SetHostSize(const SkISize& host_size) { |
| 78 DCHECK(context_->main_message_loop()->BelongsToCurrentThread()); | 78 DCHECK(context_->main_message_loop()->BelongsToCurrentThread()); |
| 79 | 79 |
| 80 if (host_size_ == host_size) | 80 if (host_size_ == host_size) |
| 81 return; | 81 return; |
| 82 | 82 |
| 83 host_size_ = host_size; | 83 host_size_ = host_size; |
| 84 | 84 |
| 85 // Submit an update of desktop size to Javascript. | 85 // Submit an update of desktop size to Javascript. |
| 86 instance_->GetScriptableObject()->SetDesktopSize( | 86 instance_->GetScriptableObject()->SetDesktopSize( |
| 87 host_size.width(), host_size.height()); | 87 host_size.width(), host_size.height()); |
| 88 } | 88 } |
| 89 | 89 |
| 90 void PepperView::PaintFrame(media::VideoFrame* frame, UpdatedRects* rects) { | 90 void PepperView::PaintFrame(media::VideoFrame* frame, RectVector* rects) { |
| 91 DCHECK(context_->main_message_loop()->BelongsToCurrentThread()); | 91 DCHECK(context_->main_message_loop()->BelongsToCurrentThread()); |
| 92 | 92 |
| 93 SetHostSize(gfx::Size(frame->width(), frame->height())); | 93 SetHostSize(SkISize::Make(frame->width(), frame->height())); |
| 94 | 94 |
| 95 if (!backing_store_.get() || backing_store_->is_null()) { | 95 if (!backing_store_.get() || backing_store_->is_null()) { |
| 96 LOG(ERROR) << "Backing store is not available."; | 96 LOG(ERROR) << "Backing store is not available."; |
| 97 return; | 97 return; |
| 98 } | 98 } |
| 99 | 99 |
| 100 base::Time start_time = base::Time::Now(); | 100 base::Time start_time = base::Time::Now(); |
| 101 | 101 |
| 102 // Copy updated regions to the backing store and then paint the regions. | 102 // Copy updated regions to the backing store and then paint the regions. |
| 103 bool changes_made = false; | 103 bool changes_made = false; |
| 104 for (size_t i = 0; i < rects->size(); ++i) | 104 for (size_t i = 0; i < rects->size(); ++i) |
| 105 changes_made |= PaintRect(frame, (*rects)[i]); | 105 changes_made |= PaintRect(frame, (*rects)[i]); |
| 106 | 106 |
| 107 if (changes_made) | 107 if (changes_made) |
| 108 FlushGraphics(start_time); | 108 FlushGraphics(start_time); |
| 109 } | 109 } |
| 110 | 110 |
| 111 bool PepperView::PaintRect(media::VideoFrame* frame, const gfx::Rect& r) { | 111 bool PepperView::PaintRect(media::VideoFrame* frame, const SkIRect& r) { |
| 112 const uint8* frame_data = frame->data(media::VideoFrame::kRGBPlane); | 112 const uint8* frame_data = frame->data(media::VideoFrame::kRGBPlane); |
| 113 const int kFrameStride = frame->stride(media::VideoFrame::kRGBPlane); | 113 const int kFrameStride = frame->stride(media::VideoFrame::kRGBPlane); |
| 114 const int kBytesPerPixel = GetBytesPerPixel(media::VideoFrame::RGB32); | 114 const int kBytesPerPixel = GetBytesPerPixel(media::VideoFrame::RGB32); |
| 115 | 115 |
| 116 pp::Size backing_store_size = backing_store_->size(); | 116 pp::Size backing_store_size = backing_store_->size(); |
| 117 gfx::Rect rect = r.Intersect(gfx::Rect(0, 0, backing_store_size.width(), | 117 SkIRect rect(r); |
| 118 backing_store_size.height())); | 118 if (!rect.intersect(SkIRect::MakeWH(backing_store_size.width(), |
| 119 | 119 backing_store_size.height()))) { |
| 120 if (rect.IsEmpty()) | |
| 121 return false; | 120 return false; |
| 121 } |
| 122 | 122 |
| 123 const uint8* in = | 123 const uint8* in = |
| 124 frame_data + | 124 frame_data + |
| 125 kFrameStride * rect.y() + // Y offset. | 125 kFrameStride * rect.fTop + // Y offset. |
| 126 kBytesPerPixel * rect.x(); // X offset. | 126 kBytesPerPixel * rect.fLeft; // X offset. |
| 127 uint8* out = | 127 uint8* out = |
| 128 reinterpret_cast<uint8*>(backing_store_->data()) + | 128 reinterpret_cast<uint8*>(backing_store_->data()) + |
| 129 backing_store_->stride() * rect.y() + // Y offset. | 129 backing_store_->stride() * rect.fTop + // Y offset. |
| 130 kBytesPerPixel * rect.x(); // X offset. | 130 kBytesPerPixel * rect.fLeft; // X offset. |
| 131 | 131 |
| 132 // TODO(hclam): We really should eliminate this memory copy. | 132 // TODO(hclam): We really should eliminate this memory copy. |
| 133 for (int j = 0; j < rect.height(); ++j) { | 133 for (int j = 0; j < rect.height(); ++j) { |
| 134 memcpy(out, in, rect.width() * kBytesPerPixel); | 134 memcpy(out, in, rect.width() * kBytesPerPixel); |
| 135 in += kFrameStride; | 135 in += kFrameStride; |
| 136 out += backing_store_->stride(); | 136 out += backing_store_->stride(); |
| 137 } | 137 } |
| 138 | 138 |
| 139 // Pepper Graphics 2D has a strange and badly documented API that the | 139 // Pepper Graphics 2D has a strange and badly documented API that the |
| 140 // point here is the offset from the source rect. Why? | 140 // point here is the offset from the source rect. Why? |
| 141 graphics2d_.PaintImageData( | 141 graphics2d_.PaintImageData( |
| 142 *backing_store_.get(), | 142 *backing_store_.get(), |
| 143 pp::Point(0, 0), | 143 pp::Point(0, 0), |
| 144 pp::Rect(rect.x(), rect.y(), rect.width(), rect.height())); | 144 pp::Rect(rect.fLeft, rect.fTop, rect.width(), rect.height())); |
| 145 return true; | 145 return true; |
| 146 } | 146 } |
| 147 | 147 |
| 148 void PepperView::BlankRect(pp::ImageData& image_data, const pp::Rect& rect) { | 148 void PepperView::BlankRect(pp::ImageData& image_data, const pp::Rect& rect) { |
| 149 const int kBytesPerPixel = GetBytesPerPixel(media::VideoFrame::RGB32); | 149 const int kBytesPerPixel = GetBytesPerPixel(media::VideoFrame::RGB32); |
| 150 for (int y = rect.y(); y < rect.bottom(); y++) { | 150 for (int y = rect.y(); y < rect.bottom(); y++) { |
| 151 uint8* to = reinterpret_cast<uint8*>(image_data.data()) + | 151 uint8* to = reinterpret_cast<uint8*>(image_data.data()) + |
| 152 (y * image_data.stride()) + (rect.x() * kBytesPerPixel); | 152 (y * image_data.stride()) + (rect.x() * kBytesPerPixel); |
| 153 memset(to, 0xff, rect.width() * kBytesPerPixel); | 153 memset(to, 0xff, rect.width() * kBytesPerPixel); |
| 154 } | 154 } |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 232 DCHECK(context_->main_message_loop()->BelongsToCurrentThread()); | 232 DCHECK(context_->main_message_loop()->BelongsToCurrentThread()); |
| 233 | 233 |
| 234 // TODO(hclam): Re-consider the way we communicate with Javascript. | 234 // TODO(hclam): Re-consider the way we communicate with Javascript. |
| 235 ChromotingScriptableObject* scriptable_obj = instance_->GetScriptableObject(); | 235 ChromotingScriptableObject* scriptable_obj = instance_->GetScriptableObject(); |
| 236 if (success) | 236 if (success) |
| 237 scriptable_obj->SetConnectionInfo(STATUS_CONNECTED, QUALITY_UNKNOWN); | 237 scriptable_obj->SetConnectionInfo(STATUS_CONNECTED, QUALITY_UNKNOWN); |
| 238 else | 238 else |
| 239 scriptable_obj->SignalLoginChallenge(); | 239 scriptable_obj->SignalLoginChallenge(); |
| 240 } | 240 } |
| 241 | 241 |
| 242 bool PepperView::SetPluginSize(const gfx::Size& plugin_size) { | 242 bool PepperView::SetPluginSize(const SkISize& plugin_size) { |
| 243 if (plugin_size_ == plugin_size) | 243 if (plugin_size_ == plugin_size) |
| 244 return false; | 244 return false; |
| 245 plugin_size_ = plugin_size; | 245 plugin_size_ = plugin_size; |
| 246 | 246 |
| 247 pp::Size pp_size = pp::Size(plugin_size.width(), plugin_size.height()); | 247 pp::Size pp_size = pp::Size(plugin_size.width(), plugin_size.height()); |
| 248 | 248 |
| 249 graphics2d_ = pp::Graphics2D(instance_, pp_size, true); | 249 graphics2d_ = pp::Graphics2D(instance_, pp_size, true); |
| 250 if (!instance_->BindGraphics(graphics2d_)) { | 250 if (!instance_->BindGraphics(graphics2d_)) { |
| 251 LOG(ERROR) << "Couldn't bind the device context."; | 251 LOG(ERROR) << "Couldn't bind the device context."; |
| 252 return false; | 252 return false; |
| 253 } | 253 } |
| 254 | 254 |
| 255 if (plugin_size.IsEmpty()) | 255 if (plugin_size.isEmpty()) |
| 256 return false; | 256 return false; |
| 257 | 257 |
| 258 // Allocate the backing store to save the desktop image. | 258 // Allocate the backing store to save the desktop image. |
| 259 if ((backing_store_.get() == NULL) || | 259 if ((backing_store_.get() == NULL) || |
| 260 (backing_store_->size() != pp_size)) { | 260 (backing_store_->size() != pp_size)) { |
| 261 LOG(INFO) << "Allocate backing store: " | 261 LOG(INFO) << "Allocate backing store: " |
| 262 << plugin_size.width() << " x " << plugin_size.height(); | 262 << plugin_size.width() << " x " << plugin_size.height(); |
| 263 backing_store_.reset( | 263 backing_store_.reset( |
| 264 new pp::ImageData(instance_, pp::ImageData::GetNativeImageDataFormat(), | 264 new pp::ImageData(instance_, pp::ImageData::GetNativeImageDataFormat(), |
| 265 pp_size, false)); | 265 pp_size, false)); |
| 266 DCHECK(backing_store_.get() && !backing_store_->is_null()) | 266 DCHECK(backing_store_.get() && !backing_store_->is_null()) |
| 267 << "Not enough memory for backing store."; | 267 << "Not enough memory for backing store."; |
| 268 } | 268 } |
| 269 return true; | 269 return true; |
| 270 } | 270 } |
| 271 | 271 |
| 272 double PepperView::GetHorizontalScaleRatio() const { | 272 double PepperView::GetHorizontalScaleRatio() const { |
| 273 if (instance_->DoScaling()) { | 273 if (instance_->DoScaling()) { |
| 274 DCHECK(!host_size_.IsEmpty()); | 274 DCHECK(!host_size_.isEmpty()); |
| 275 return 1.0 * plugin_size_.width() / host_size_.width(); | 275 return 1.0 * plugin_size_.width() / host_size_.width(); |
| 276 } | 276 } |
| 277 return 1.0; | 277 return 1.0; |
| 278 } | 278 } |
| 279 | 279 |
| 280 double PepperView::GetVerticalScaleRatio() const { | 280 double PepperView::GetVerticalScaleRatio() const { |
| 281 if (instance_->DoScaling()) { | 281 if (instance_->DoScaling()) { |
| 282 DCHECK(!host_size_.IsEmpty()); | 282 DCHECK(!host_size_.isEmpty()); |
| 283 return 1.0 * plugin_size_.height() / host_size_.height(); | 283 return 1.0 * plugin_size_.height() / host_size_.height(); |
| 284 } | 284 } |
| 285 return 1.0; | 285 return 1.0; |
| 286 } | 286 } |
| 287 | 287 |
| 288 void PepperView::AllocateFrame(media::VideoFrame::Format format, | 288 void PepperView::AllocateFrame(media::VideoFrame::Format format, |
| 289 size_t width, | 289 size_t width, |
| 290 size_t height, | 290 size_t height, |
| 291 base::TimeDelta timestamp, | 291 base::TimeDelta timestamp, |
| 292 base::TimeDelta duration, | 292 base::TimeDelta duration, |
| (...skipping 13 matching lines...) Expand all Loading... |
| 306 void PepperView::ReleaseFrame(media::VideoFrame* frame) { | 306 void PepperView::ReleaseFrame(media::VideoFrame* frame) { |
| 307 DCHECK(context_->main_message_loop()->BelongsToCurrentThread()); | 307 DCHECK(context_->main_message_loop()->BelongsToCurrentThread()); |
| 308 | 308 |
| 309 if (frame) { | 309 if (frame) { |
| 310 LOG(WARNING) << "Frame released."; | 310 LOG(WARNING) << "Frame released."; |
| 311 frame->Release(); | 311 frame->Release(); |
| 312 } | 312 } |
| 313 } | 313 } |
| 314 | 314 |
| 315 void PepperView::OnPartialFrameOutput(media::VideoFrame* frame, | 315 void PepperView::OnPartialFrameOutput(media::VideoFrame* frame, |
| 316 UpdatedRects* rects, | 316 RectVector* rects, |
| 317 Task* done) { | 317 Task* done) { |
| 318 DCHECK(context_->main_message_loop()->BelongsToCurrentThread()); | 318 DCHECK(context_->main_message_loop()->BelongsToCurrentThread()); |
| 319 | 319 |
| 320 // TODO(ajwong): Clean up this API to be async so we don't need to use a | 320 // TODO(ajwong): Clean up this API to be async so we don't need to use a |
| 321 // member variable as a hack. | 321 // member variable as a hack. |
| 322 PaintFrame(frame, rects); | 322 PaintFrame(frame, rects); |
| 323 done->Run(); | 323 done->Run(); |
| 324 delete done; | 324 delete done; |
| 325 } | 325 } |
| 326 | 326 |
| 327 void PepperView::OnPaintDone(base::Time paint_start) { | 327 void PepperView::OnPaintDone(base::Time paint_start) { |
| 328 DCHECK(context_->main_message_loop()->BelongsToCurrentThread()); | 328 DCHECK(context_->main_message_loop()->BelongsToCurrentThread()); |
| 329 instance_->GetStats()->video_paint_ms()->Record( | 329 instance_->GetStats()->video_paint_ms()->Record( |
| 330 (base::Time::Now() - paint_start).InMilliseconds()); | 330 (base::Time::Now() - paint_start).InMilliseconds()); |
| 331 | 331 |
| 332 // If the last flush failed because there was already another one in progress | 332 // If the last flush failed because there was already another one in progress |
| 333 // then we perform the flush now. | 333 // then we perform the flush now. |
| 334 if (flush_blocked_) | 334 if (flush_blocked_) |
| 335 FlushGraphics(base::Time::Now()); | 335 FlushGraphics(base::Time::Now()); |
| 336 return; | 336 return; |
| 337 } | 337 } |
| 338 | 338 |
| 339 } // namespace remoting | 339 } // namespace remoting |
| OLD | NEW |