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/plugin/pepper_view.h" | 5 #include "remoting/client/plugin/pepper_view.h" |
6 | 6 |
| 7 #include <functional> |
| 8 |
7 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
8 #include "base/string_util.h" | 10 #include "base/string_util.h" |
| 11 #include "base/synchronization/waitable_event.h" |
9 #include "ppapi/cpp/completion_callback.h" | 12 #include "ppapi/cpp/completion_callback.h" |
10 #include "ppapi/cpp/graphics_2d.h" | 13 #include "ppapi/cpp/graphics_2d.h" |
11 #include "ppapi/cpp/image_data.h" | 14 #include "ppapi/cpp/image_data.h" |
12 #include "ppapi/cpp/point.h" | 15 #include "ppapi/cpp/point.h" |
13 #include "ppapi/cpp/rect.h" | 16 #include "ppapi/cpp/rect.h" |
14 #include "ppapi/cpp/size.h" | 17 #include "ppapi/cpp/size.h" |
15 #include "remoting/base/util.h" | 18 #include "remoting/base/util.h" |
16 #include "remoting/client/chromoting_stats.h" | 19 #include "remoting/client/chromoting_stats.h" |
17 #include "remoting/client/client_context.h" | 20 #include "remoting/client/client_context.h" |
| 21 #include "remoting/client/frame_producer.h" |
18 #include "remoting/client/plugin/chromoting_instance.h" | 22 #include "remoting/client/plugin/chromoting_instance.h" |
19 #include "remoting/client/plugin/pepper_util.h" | 23 #include "remoting/client/plugin/pepper_util.h" |
20 | 24 |
| 25 using base::Passed; |
| 26 |
21 namespace remoting { | 27 namespace remoting { |
22 | 28 |
23 namespace { | 29 namespace { |
24 | 30 |
| 31 // The maximum number of image buffers to be allocated at any point of time. |
| 32 const size_t kMaxPendingBuffersCount = 2; |
| 33 |
25 ChromotingScriptableObject::ConnectionError ConvertConnectionError( | 34 ChromotingScriptableObject::ConnectionError ConvertConnectionError( |
26 protocol::ConnectionToHost::Error error) { | 35 protocol::ConnectionToHost::Error error) { |
27 switch (error) { | 36 switch (error) { |
28 case protocol::ConnectionToHost::OK: | 37 case protocol::ConnectionToHost::OK: |
29 return ChromotingScriptableObject::ERROR_NONE; | 38 return ChromotingScriptableObject::ERROR_NONE; |
30 case protocol::ConnectionToHost::HOST_IS_OFFLINE: | 39 case protocol::ConnectionToHost::HOST_IS_OFFLINE: |
31 return ChromotingScriptableObject::ERROR_HOST_IS_OFFLINE; | 40 return ChromotingScriptableObject::ERROR_HOST_IS_OFFLINE; |
32 case protocol::ConnectionToHost::SESSION_REJECTED: | 41 case protocol::ConnectionToHost::SESSION_REJECTED: |
33 return ChromotingScriptableObject::ERROR_SESSION_REJECTED; | 42 return ChromotingScriptableObject::ERROR_SESSION_REJECTED; |
34 case protocol::ConnectionToHost::INCOMPATIBLE_PROTOCOL: | 43 case protocol::ConnectionToHost::INCOMPATIBLE_PROTOCOL: |
35 return ChromotingScriptableObject::ERROR_INCOMPATIBLE_PROTOCOL; | 44 return ChromotingScriptableObject::ERROR_INCOMPATIBLE_PROTOCOL; |
36 case protocol::ConnectionToHost::NETWORK_FAILURE: | 45 case protocol::ConnectionToHost::NETWORK_FAILURE: |
37 return ChromotingScriptableObject::ERROR_NETWORK_FAILURE; | 46 return ChromotingScriptableObject::ERROR_NETWORK_FAILURE; |
38 } | 47 } |
39 DLOG(FATAL) << "Unknown error code" << error; | 48 DLOG(FATAL) << "Unknown error code" << error; |
40 return ChromotingScriptableObject::ERROR_NONE; | 49 return ChromotingScriptableObject::ERROR_NONE; |
41 } | 50 } |
42 | 51 |
43 } // namespace | 52 } // namespace |
44 | 53 |
45 PepperView::PepperView(ChromotingInstance* instance, ClientContext* context) | 54 PepperView::PepperView(ChromotingInstance* instance, |
| 55 ClientContext* context, |
| 56 FrameProducer* producer) |
46 : instance_(instance), | 57 : instance_(instance), |
47 context_(context), | 58 context_(context), |
48 flush_blocked_(false), | 59 producer_(producer), |
49 is_static_fill_(false), | 60 merge_buffer_(NULL), |
50 static_fill_color_(0), | 61 merge_clip_area_(SkIRect::MakeEmpty()), |
| 62 view_size_(SkISize::Make(0, 0)), |
| 63 clip_area_(SkIRect::MakeEmpty()), |
| 64 source_size_(SkISize::Make(0, 0)), |
| 65 flush_pending_(false), |
| 66 producer_disabled_(false), |
51 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { | 67 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { |
52 } | 68 } |
53 | 69 |
54 PepperView::~PepperView() { | 70 PepperView::~PepperView() { |
| 71 DCHECK(merge_buffer_ == NULL); |
| 72 DCHECK(buffers_.empty()); |
55 } | 73 } |
56 | 74 |
57 bool PepperView::Initialize() { | 75 bool PepperView::Initialize() { |
58 return true; | 76 return true; |
59 } | 77 } |
60 | 78 |
61 void PepperView::TearDown() { | 79 void PepperView::TearDown() { |
62 DCHECK(context_->main_message_loop()->BelongsToCurrentThread()); | 80 DCHECK(context_->main_message_loop()->BelongsToCurrentThread()); |
63 | 81 |
| 82 // The producer should now return any pending buffers. At this point, however, |
| 83 // ReturnBuffer() tasks scheduled by the producer will not be delivered, |
| 84 // so we free all the buffers once the producer's queue is empty. |
| 85 producer_disabled_ = true; |
| 86 base::WaitableEvent done_event(true, false); |
| 87 producer_->RequestReturnBuffers( |
| 88 base::Bind(&base::WaitableEvent::Signal, base::Unretained(&done_event))); |
| 89 done_event.Wait(); |
| 90 |
| 91 merge_buffer_ = NULL; |
| 92 while (!buffers_.empty()) { |
| 93 FreeBuffer(buffers_.front()); |
| 94 } |
| 95 |
64 weak_factory_.InvalidateWeakPtrs(); | 96 weak_factory_.InvalidateWeakPtrs(); |
65 } | 97 } |
66 | 98 |
67 void PepperView::Paint() { | 99 void PepperView::SetConnectionState(protocol::ConnectionToHost::State state, |
68 DCHECK(context_->main_message_loop()->BelongsToCurrentThread()); | 100 protocol::ConnectionToHost::Error error) { |
69 | 101 DCHECK(context_->main_message_loop()->BelongsToCurrentThread()); |
70 if (is_static_fill_) { | 102 |
71 VLOG(1) << "Static filling " << static_fill_color_; | 103 // TODO(hclam): Re-consider the way we communicate with Javascript. |
72 pp::ImageData image(instance_, pp::ImageData::GetNativeImageDataFormat(), | 104 ChromotingScriptableObject* scriptable_obj = instance_->GetScriptableObject(); |
73 pp::Size(graphics2d_.size().width(), | 105 switch (state) { |
74 graphics2d_.size().height()), | 106 case protocol::ConnectionToHost::CONNECTING: |
75 false); | 107 scriptable_obj->SetConnectionStatus( |
76 if (image.is_null()) { | 108 ChromotingScriptableObject::STATUS_CONNECTING, |
77 LOG(ERROR) << "Unable to allocate image of size: " | 109 ConvertConnectionError(error)); |
78 << graphics2d_.size().width() << " x " | 110 break; |
79 << graphics2d_.size().height(); | 111 |
80 return; | 112 case protocol::ConnectionToHost::CONNECTED: |
| 113 scriptable_obj->SetConnectionStatus( |
| 114 ChromotingScriptableObject::STATUS_CONNECTED, |
| 115 ConvertConnectionError(error)); |
| 116 break; |
| 117 |
| 118 case protocol::ConnectionToHost::CLOSED: |
| 119 scriptable_obj->SetConnectionStatus( |
| 120 ChromotingScriptableObject::STATUS_CLOSED, |
| 121 ConvertConnectionError(error)); |
| 122 break; |
| 123 |
| 124 case protocol::ConnectionToHost::FAILED: |
| 125 scriptable_obj->SetConnectionStatus( |
| 126 ChromotingScriptableObject::STATUS_FAILED, |
| 127 ConvertConnectionError(error)); |
| 128 break; |
| 129 } |
| 130 } |
| 131 |
| 132 void PepperView::SetView(const SkISize& view_size, |
| 133 const SkIRect& clip_area) { |
| 134 bool view_changed = false; |
| 135 |
| 136 // TODO(alexeypa): Prevent upscaling because the YUV-to-RGB conversion code |
| 137 // currently does not support upscaling. Once it does, this code be removed. |
| 138 SkISize size = SkISize::Make( |
| 139 std::min(view_size.width(), source_size_.width()), |
| 140 std::min(view_size.height(), source_size_.height())); |
| 141 |
| 142 if (view_size_ != size) { |
| 143 view_changed = true; |
| 144 view_size_ = size; |
| 145 |
| 146 pp::Size pp_size = pp::Size(view_size_.width(), view_size_.height()); |
| 147 graphics2d_ = pp::Graphics2D(instance_, pp_size, true); |
| 148 bool result = instance_->BindGraphics(graphics2d_); |
| 149 |
| 150 // There is no good way to handle this error currently. |
| 151 DCHECK(result) << "Couldn't bind the device context."; |
| 152 } |
| 153 |
| 154 if (clip_area_ != clip_area) { |
| 155 view_changed = true; |
| 156 |
| 157 // YUV to RGB conversion may require even X and Y coordinates for |
| 158 // the top left corner of the clipping area. |
| 159 clip_area_ = AlignRect(clip_area); |
| 160 clip_area_.intersect(SkIRect::MakeSize(view_size_)); |
| 161 } |
| 162 |
| 163 if (view_changed) { |
| 164 producer_->SetOutputSizeAndClip(view_size_, clip_area_); |
| 165 InitiateDrawing(); |
| 166 } |
| 167 } |
| 168 |
| 169 void PepperView::ApplyBuffer(const SkISize& view_size, |
| 170 const SkIRect& clip_area, |
| 171 pp::ImageData* buffer, |
| 172 const SkRegion& region) { |
| 173 DCHECK(context_->main_message_loop()->BelongsToCurrentThread()); |
| 174 |
| 175 // Currently we cannot use the data in the buffer is scale factor has changed |
| 176 // already. |
| 177 // TODO(alexeypa): We could rescale and draw it (or even draw it without |
| 178 // rescaling) to reduce the perceived lag while we are waiting for |
| 179 // the properly scaled data. |
| 180 if (view_size_ != view_size) { |
| 181 FreeBuffer(buffer); |
| 182 InitiateDrawing(); |
| 183 } else { |
| 184 FlushBuffer(clip_area, buffer, region); |
| 185 } |
| 186 } |
| 187 |
| 188 void PepperView::ReturnBuffer(pp::ImageData* buffer) { |
| 189 DCHECK(context_->main_message_loop()->BelongsToCurrentThread()); |
| 190 |
| 191 // Free the buffer if there is nothing to paint. |
| 192 if (producer_disabled_) { |
| 193 FreeBuffer(buffer); |
| 194 return; |
| 195 } |
| 196 |
| 197 // Reuse the buffer if it is large enough, otherwise drop it on the floor |
| 198 // and allocate a new one. |
| 199 if (buffer->size().width() >= clip_area_.width() && |
| 200 buffer->size().height() >= clip_area_.height()) { |
| 201 producer_->DrawBuffer(buffer); |
| 202 } else { |
| 203 FreeBuffer(buffer); |
| 204 InitiateDrawing(); |
| 205 } |
| 206 } |
| 207 |
| 208 void PepperView::SetSourceSize(const SkISize& source_size) { |
| 209 DCHECK(context_->main_message_loop()->BelongsToCurrentThread()); |
| 210 |
| 211 if (source_size_ == source_size) |
| 212 return; |
| 213 |
| 214 source_size_ = source_size; |
| 215 |
| 216 // Notify JavaScript of the change in source size. |
| 217 instance_->GetScriptableObject()->SetDesktopSize( |
| 218 source_size.width(), source_size.height()); |
| 219 } |
| 220 |
| 221 pp::ImageData* PepperView::AllocateBuffer() { |
| 222 pp::ImageData* buffer = NULL; |
| 223 if (buffers_.size() < kMaxPendingBuffersCount) { |
| 224 pp::Size pp_size = pp::Size(clip_area_.width(), clip_area_.height()); |
| 225 buffer = new pp::ImageData(instance_, |
| 226 PP_IMAGEDATAFORMAT_BGRA_PREMUL, |
| 227 pp_size, |
| 228 false); |
| 229 if (buffer->is_null()) { |
| 230 LOG(WARNING) << "Not enough memory for frame buffers."; |
| 231 delete buffer; |
| 232 buffer = NULL; |
| 233 } else { |
| 234 buffers_.push_back(buffer); |
81 } | 235 } |
82 | 236 } |
83 for (int y = 0; y < image.size().height(); y++) { | 237 |
84 for (int x = 0; x < image.size().width(); x++) { | 238 return buffer; |
85 *image.GetAddr32(pp::Point(x, y)) = static_fill_color_; | 239 } |
86 } | 240 |
| 241 void PepperView::FreeBuffer(pp::ImageData* buffer) { |
| 242 DCHECK(std::find(buffers_.begin(), buffers_.end(), buffer) != buffers_.end()); |
| 243 |
| 244 buffers_.remove(buffer); |
| 245 delete buffer; |
| 246 } |
| 247 |
| 248 void PepperView::InitiateDrawing() { |
| 249 // Do not schedule drawing if there is nothing to paint. |
| 250 if (producer_disabled_) |
| 251 return; |
| 252 |
| 253 pp::ImageData* buffer = AllocateBuffer(); |
| 254 while (buffer) { |
| 255 producer_->DrawBuffer(buffer); |
| 256 buffer = AllocateBuffer(); |
| 257 } |
| 258 } |
| 259 |
| 260 void PepperView::FlushBuffer(const SkIRect& clip_area, |
| 261 pp::ImageData* buffer, |
| 262 const SkRegion& region) { |
| 263 |
| 264 // Defer drawing if the flush is already in progress. |
| 265 if (flush_pending_) { |
| 266 // |merge_buffer_| is guaranteed to be free here because we allocate only |
| 267 // two buffers simultaneously. If more buffers are allowed this code should |
| 268 // apply all pending changes to the screen. |
| 269 DCHECK(merge_buffer_ == NULL); |
| 270 |
| 271 merge_clip_area_ = clip_area; |
| 272 merge_buffer_ = buffer; |
| 273 merge_region_ = region; |
| 274 return; |
| 275 } |
| 276 |
| 277 // Notify Pepper API about the updated areas and flush pixels to the screen. |
| 278 base::Time start_time = base::Time::Now(); |
| 279 |
| 280 for (SkRegion::Iterator i(region); !i.done(); i.next()) { |
| 281 SkIRect rect = i.rect(); |
| 282 |
| 283 // Re-clip |region| with the current clipping area |clip_area_| because |
| 284 // the latter could change from the time the buffer was drawn. |
| 285 if (!rect.intersect(clip_area_)) |
| 286 continue; |
| 287 |
| 288 // Specify the rectangle coordinates relative to the clipping area. |
| 289 rect.offset(-clip_area.left(), -clip_area.top()); |
| 290 |
| 291 // Pepper Graphics 2D has a strange and badly documented API that the |
| 292 // point here is the offset from the source rect. Why? |
| 293 graphics2d_.PaintImageData( |
| 294 *buffer, |
| 295 pp::Point(clip_area.left(), clip_area.top()), |
| 296 pp::Rect(rect.left(), rect.top(), rect.width(), rect.height())); |
| 297 } |
| 298 |
| 299 // Notify the producer that some parts of the region weren't painted because |
| 300 // the clipping area has changed already. |
| 301 if (clip_area != clip_area_) { |
| 302 SkRegion not_painted = region; |
| 303 not_painted.op(clip_area_, SkRegion::kDifference_Op); |
| 304 if (!not_painted.isEmpty()) { |
| 305 producer_->InvalidateRegion(not_painted); |
87 } | 306 } |
88 | 307 } |
89 // For ReplaceContents, make sure the image size matches the device context | 308 |
90 // size! Otherwise, this will just silently do nothing. | 309 // Flush the updated areas to the screen. |
91 graphics2d_.ReplaceContents(&image); | |
92 FlushGraphics(base::Time::Now()); | |
93 } else { | |
94 // TODO(ajwong): We need to keep a backing store image of the host screen | |
95 // that has the data here which can be redrawn. | |
96 return; | |
97 } | |
98 } | |
99 | |
100 void PepperView::SetHostSize(const SkISize& host_size) { | |
101 DCHECK(context_->main_message_loop()->BelongsToCurrentThread()); | |
102 | |
103 if (host_size_ == host_size) | |
104 return; | |
105 | |
106 host_size_ = host_size; | |
107 | |
108 // Submit an update of desktop size to Javascript. | |
109 instance_->GetScriptableObject()->SetDesktopSize( | |
110 host_size.width(), host_size.height()); | |
111 } | |
112 | |
113 void PepperView::PaintFrame(media::VideoFrame* frame, const SkRegion& region) { | |
114 DCHECK(context_->main_message_loop()->BelongsToCurrentThread()); | |
115 | |
116 SetHostSize(SkISize::Make(frame->width(), frame->height())); | |
117 | |
118 if (!backing_store_.get() || backing_store_->is_null()) { | |
119 LOG(ERROR) << "Backing store is not available."; | |
120 return; | |
121 } | |
122 | |
123 base::Time start_time = base::Time::Now(); | |
124 | |
125 // Copy updated regions to the backing store and then paint the regions. | |
126 bool changes_made = false; | |
127 for (SkRegion::Iterator i(region); !i.done(); i.next()) | |
128 changes_made |= PaintRect(frame, i.rect()); | |
129 | |
130 if (changes_made) | |
131 FlushGraphics(start_time); | |
132 } | |
133 | |
134 bool PepperView::PaintRect(media::VideoFrame* frame, const SkIRect& r) { | |
135 const uint8* frame_data = frame->data(media::VideoFrame::kRGBPlane); | |
136 const int kFrameStride = frame->stride(media::VideoFrame::kRGBPlane); | |
137 const int kBytesPerPixel = GetBytesPerPixel(media::VideoFrame::RGB32); | |
138 | |
139 pp::Size backing_store_size = backing_store_->size(); | |
140 SkIRect rect(r); | |
141 if (!rect.intersect(SkIRect::MakeWH(backing_store_size.width(), | |
142 backing_store_size.height()))) { | |
143 return false; | |
144 } | |
145 | |
146 const uint8* in = | |
147 frame_data + | |
148 kFrameStride * rect.fTop + // Y offset. | |
149 kBytesPerPixel * rect.fLeft; // X offset. | |
150 uint8* out = | |
151 reinterpret_cast<uint8*>(backing_store_->data()) + | |
152 backing_store_->stride() * rect.fTop + // Y offset. | |
153 kBytesPerPixel * rect.fLeft; // X offset. | |
154 | |
155 // TODO(hclam): We really should eliminate this memory copy. | |
156 for (int j = 0; j < rect.height(); ++j) { | |
157 memcpy(out, in, rect.width() * kBytesPerPixel); | |
158 in += kFrameStride; | |
159 out += backing_store_->stride(); | |
160 } | |
161 | |
162 // Pepper Graphics 2D has a strange and badly documented API that the | |
163 // point here is the offset from the source rect. Why? | |
164 graphics2d_.PaintImageData( | |
165 *backing_store_.get(), | |
166 pp::Point(0, 0), | |
167 pp::Rect(rect.fLeft, rect.fTop, rect.width(), rect.height())); | |
168 return true; | |
169 } | |
170 | |
171 void PepperView::BlankRect(pp::ImageData& image_data, const pp::Rect& rect) { | |
172 const int kBytesPerPixel = GetBytesPerPixel(media::VideoFrame::RGB32); | |
173 for (int y = rect.y(); y < rect.bottom(); y++) { | |
174 uint8* to = reinterpret_cast<uint8*>(image_data.data()) + | |
175 (y * image_data.stride()) + (rect.x() * kBytesPerPixel); | |
176 memset(to, 0xff, rect.width() * kBytesPerPixel); | |
177 } | |
178 } | |
179 | |
180 void PepperView::FlushGraphics(base::Time paint_start) { | |
181 scoped_ptr<base::Closure> task( | 310 scoped_ptr<base::Closure> task( |
182 new base::Closure( | 311 new base::Closure( |
183 base::Bind(&PepperView::OnPaintDone, weak_factory_.GetWeakPtr(), | 312 base::Bind(&PepperView::OnFlushDone, weak_factory_.GetWeakPtr(), |
184 paint_start))); | 313 start_time, buffer))); |
185 | 314 |
186 // Flag needs to be set here in order to get a proper error code for Flush(). | 315 // Flag needs to be set here in order to get a proper error code for Flush(). |
187 // Otherwise Flush() will always return PP_OK_COMPLETIONPENDING and the error | 316 // Otherwise Flush() will always return PP_OK_COMPLETIONPENDING and the error |
188 // would be hidden. | 317 // would be hidden. |
189 // | 318 // |
190 // Note that we can also handle this by providing an actual callback which | 319 // Note that we can also handle this by providing an actual callback which |
191 // takes the result code. Right now everything goes to the task that doesn't | 320 // takes the result code. Right now everything goes to the task that doesn't |
192 // result value. | 321 // result value. |
193 pp::CompletionCallback pp_callback(&CompletionCallbackClosureAdapter, | 322 pp::CompletionCallback pp_callback(&CompletionCallbackClosureAdapter, |
194 task.get(), | 323 task.get(), |
195 PP_COMPLETIONCALLBACK_FLAG_OPTIONAL); | 324 PP_COMPLETIONCALLBACK_FLAG_OPTIONAL); |
196 int error = graphics2d_.Flush(pp_callback); | 325 int error = graphics2d_.Flush(pp_callback); |
197 | 326 |
198 // There is already a flush in progress so set this flag to true so that we | |
199 // can flush again later. | |
200 // |paint_start| is then discarded but this is fine because we're not aiming | |
201 // for precise measurement of timing, otherwise we need to keep a list of | |
202 // queued start time(s). | |
203 if (error == PP_ERROR_INPROGRESS) | |
204 flush_blocked_ = true; | |
205 else | |
206 flush_blocked_ = false; | |
207 | |
208 // If Flush() returns asynchronously then release the task. | 327 // If Flush() returns asynchronously then release the task. |
209 if (error == PP_OK_COMPLETIONPENDING) | 328 flush_pending_ = (error == PP_OK_COMPLETIONPENDING); |
| 329 if (flush_pending_) { |
210 ignore_result(task.release()); | 330 ignore_result(task.release()); |
211 } | 331 } else { |
212 | 332 instance_->GetStats()->video_paint_ms()->Record( |
213 void PepperView::SetSolidFill(uint32 color) { | 333 (base::Time::Now() - start_time).InMilliseconds()); |
214 DCHECK(context_->main_message_loop()->BelongsToCurrentThread()); | 334 |
215 | 335 ReturnBuffer(buffer); |
216 is_static_fill_ = true; | 336 |
217 static_fill_color_ = color; | 337 // Resume painting for the buffer that was previoulsy postponed because of |
218 | 338 // pending flush. |
219 Paint(); | 339 if (merge_buffer_ != NULL) { |
220 } | 340 buffer = merge_buffer_; |
221 | 341 merge_buffer_ = NULL; |
222 void PepperView::UnsetSolidFill() { | 342 FlushBuffer(merge_clip_area_, buffer, merge_region_); |
223 DCHECK(context_->main_message_loop()->BelongsToCurrentThread()); | 343 } |
224 | 344 } |
225 is_static_fill_ = false; | 345 } |
226 } | 346 |
227 | 347 void PepperView::OnFlushDone(base::Time paint_start, |
228 void PepperView::SetConnectionState(protocol::ConnectionToHost::State state, | 348 pp::ImageData* buffer) { |
229 protocol::ConnectionToHost::Error error) { | 349 DCHECK(context_->main_message_loop()->BelongsToCurrentThread()); |
230 DCHECK(context_->main_message_loop()->BelongsToCurrentThread()); | 350 DCHECK(flush_pending_); |
231 | 351 |
232 // TODO(hclam): Re-consider the way we communicate with Javascript. | |
233 ChromotingScriptableObject* scriptable_obj = instance_->GetScriptableObject(); | |
234 switch (state) { | |
235 case protocol::ConnectionToHost::CONNECTING: | |
236 SetSolidFill(kCreatedColor); | |
237 scriptable_obj->SetConnectionStatus( | |
238 ChromotingScriptableObject::STATUS_CONNECTING, | |
239 ConvertConnectionError(error)); | |
240 break; | |
241 | |
242 case protocol::ConnectionToHost::CONNECTED: | |
243 UnsetSolidFill(); | |
244 scriptable_obj->SetConnectionStatus( | |
245 ChromotingScriptableObject::STATUS_CONNECTED, | |
246 ConvertConnectionError(error)); | |
247 break; | |
248 | |
249 case protocol::ConnectionToHost::CLOSED: | |
250 SetSolidFill(kDisconnectedColor); | |
251 scriptable_obj->SetConnectionStatus( | |
252 ChromotingScriptableObject::STATUS_CLOSED, | |
253 ConvertConnectionError(error)); | |
254 break; | |
255 | |
256 case protocol::ConnectionToHost::FAILED: | |
257 SetSolidFill(kFailedColor); | |
258 scriptable_obj->SetConnectionStatus( | |
259 ChromotingScriptableObject::STATUS_FAILED, | |
260 ConvertConnectionError(error)); | |
261 break; | |
262 } | |
263 } | |
264 | |
265 bool PepperView::SetViewSize(const SkISize& view_size) { | |
266 if (view_size_ == view_size) | |
267 return false; | |
268 view_size_ = view_size; | |
269 | |
270 pp::Size pp_size = pp::Size(view_size.width(), view_size.height()); | |
271 | |
272 graphics2d_ = pp::Graphics2D(instance_, pp_size, true); | |
273 if (!instance_->BindGraphics(graphics2d_)) { | |
274 LOG(ERROR) << "Couldn't bind the device context."; | |
275 return false; | |
276 } | |
277 | |
278 if (view_size.isEmpty()) | |
279 return false; | |
280 | |
281 // Allocate the backing store to save the desktop image. | |
282 if ((backing_store_.get() == NULL) || | |
283 (backing_store_->size() != pp_size)) { | |
284 VLOG(1) << "Allocate backing store: " | |
285 << view_size.width() << " x " << view_size.height(); | |
286 backing_store_.reset( | |
287 new pp::ImageData(instance_, pp::ImageData::GetNativeImageDataFormat(), | |
288 pp_size, false)); | |
289 DCHECK(backing_store_.get() && !backing_store_->is_null()) | |
290 << "Not enough memory for backing store."; | |
291 } | |
292 return true; | |
293 } | |
294 | |
295 void PepperView::AllocateFrame(media::VideoFrame::Format format, | |
296 const SkISize& size, | |
297 scoped_refptr<media::VideoFrame>* frame_out, | |
298 const base::Closure& done) { | |
299 DCHECK(context_->main_message_loop()->BelongsToCurrentThread()); | |
300 | |
301 *frame_out = media::VideoFrame::CreateFrame( | |
302 media::VideoFrame::RGB32, size.width(), size.height(), | |
303 base::TimeDelta(), base::TimeDelta()); | |
304 (*frame_out)->AddRef(); | |
305 done.Run(); | |
306 } | |
307 | |
308 void PepperView::ReleaseFrame(media::VideoFrame* frame) { | |
309 DCHECK(context_->main_message_loop()->BelongsToCurrentThread()); | |
310 | |
311 if (frame) | |
312 frame->Release(); | |
313 } | |
314 | |
315 void PepperView::OnPartialFrameOutput(media::VideoFrame* frame, | |
316 SkRegion* region, | |
317 const base::Closure& done) { | |
318 DCHECK(context_->main_message_loop()->BelongsToCurrentThread()); | |
319 | |
320 // TODO(ajwong): Clean up this API to be async so we don't need to use a | |
321 // member variable as a hack. | |
322 PaintFrame(frame, *region); | |
323 done.Run(); | |
324 } | |
325 | |
326 void PepperView::OnPaintDone(base::Time paint_start) { | |
327 DCHECK(context_->main_message_loop()->BelongsToCurrentThread()); | |
328 instance_->GetStats()->video_paint_ms()->Record( | 352 instance_->GetStats()->video_paint_ms()->Record( |
329 (base::Time::Now() - paint_start).InMilliseconds()); | 353 (base::Time::Now() - paint_start).InMilliseconds()); |
330 | 354 |
331 // If the last flush failed because there was already another one in progress | 355 flush_pending_ = false; |
332 // then we perform the flush now. | 356 ReturnBuffer(buffer); |
333 if (flush_blocked_) | 357 |
334 FlushGraphics(base::Time::Now()); | 358 // Resume painting for the buffer that was previoulsy postponed because of |
335 return; | 359 // pending flush. |
| 360 if (merge_buffer_ != NULL) { |
| 361 buffer = merge_buffer_; |
| 362 merge_buffer_ = NULL; |
| 363 FlushBuffer(merge_clip_area_, buffer, merge_region_); |
| 364 } |
336 } | 365 } |
337 | 366 |
338 } // namespace remoting | 367 } // namespace remoting |
OLD | NEW |