Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(772)

Side by Side Diff: remoting/client/plugin/pepper_view.cc

Issue 18233015: Abstract PPAPI's ImageData behind webrtc::DesktopFrame interface (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Implement Lambros's suggestions Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « remoting/client/plugin/pepper_view.h ('k') | remoting/client/rectangle_update_decoder.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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> 7 #include <functional>
8 8
9 #include "base/message_loop.h" 9 #include "base/message_loop.h"
10 #include "base/strings/string_util.h" 10 #include "base/strings/string_util.h"
11 #include "base/synchronization/waitable_event.h" 11 #include "base/synchronization/waitable_event.h"
12 #include "base/time/time.h" 12 #include "base/time/time.h"
13 #include "ppapi/cpp/completion_callback.h" 13 #include "ppapi/cpp/completion_callback.h"
14 #include "ppapi/cpp/dev/graphics_2d_dev.h" 14 #include "ppapi/cpp/dev/graphics_2d_dev.h"
15 #include "ppapi/cpp/dev/view_dev.h" 15 #include "ppapi/cpp/dev/view_dev.h"
16 #include "ppapi/cpp/image_data.h" 16 #include "ppapi/cpp/image_data.h"
17 #include "ppapi/cpp/point.h" 17 #include "ppapi/cpp/point.h"
18 #include "ppapi/cpp/rect.h" 18 #include "ppapi/cpp/rect.h"
19 #include "ppapi/cpp/size.h" 19 #include "ppapi/cpp/size.h"
20 #include "remoting/base/util.h" 20 #include "remoting/base/util.h"
21 #include "remoting/client/chromoting_stats.h" 21 #include "remoting/client/chromoting_stats.h"
22 #include "remoting/client/client_context.h" 22 #include "remoting/client/client_context.h"
23 #include "remoting/client/frame_producer.h" 23 #include "remoting/client/frame_producer.h"
24 #include "remoting/client/plugin/chromoting_instance.h" 24 #include "remoting/client/plugin/chromoting_instance.h"
25 #include "remoting/client/plugin/pepper_util.h" 25 #include "remoting/client/plugin/pepper_util.h"
26 #include "third_party/webrtc/modules/desktop_capture/desktop_frame.h"
26 27
27 using base::Passed; 28 using base::Passed;
28 29
30 namespace {
31
32 // ImageBuffer that uses PPAPI to allocate space for a raw image.
33 class PepperDesktopFrame : public webrtc::DesktopFrame {
34 public:
35 // Wraps the supplied ImageData.
36 explicit PepperDesktopFrame(const pp::ImageData& buffer);
37
38 // Access to underlying pepper representation.
39 inline const pp::ImageData& get_buffer();
Lambros 2013/07/03 22:39:46 nit: Add another const after get_buffer().
40
41 private:
42 pp::ImageData buffer_;
43 };
44
45 PepperDesktopFrame::PepperDesktopFrame(const pp::ImageData& buffer)
46 : DesktopFrame(webrtc::DesktopSize(buffer.size().width(),
47 buffer.size().height()),
48 buffer.stride(),
49 reinterpret_cast<uint8_t*>(buffer.data()),
50 NULL),
51 buffer_(buffer) {}
52
53 const pp::ImageData& PepperDesktopFrame::get_buffer() {
54 return buffer_;
55 }
56
57 } // namespace
58
29 namespace remoting { 59 namespace remoting {
30 60
31 namespace { 61 namespace {
32 62
33 // The maximum number of image buffers to be allocated at any point of time. 63 // The maximum number of image buffers to be allocated at any point of time.
34 const size_t kMaxPendingBuffersCount = 2; 64 const size_t kMaxPendingBuffersCount = 2;
35 65
36 } // namespace 66 } // namespace
37 67
38 PepperView::PepperView(ChromotingInstance* instance, 68 PepperView::PepperView(ChromotingInstance* instance,
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
132 } 162 }
133 163
134 if (view_changed) { 164 if (view_changed) {
135 producer_->SetOutputSizeAndClip(view_size_, clip_area_); 165 producer_->SetOutputSizeAndClip(view_size_, clip_area_);
136 InitiateDrawing(); 166 InitiateDrawing();
137 } 167 }
138 } 168 }
139 169
140 void PepperView::ApplyBuffer(const SkISize& view_size, 170 void PepperView::ApplyBuffer(const SkISize& view_size,
141 const SkIRect& clip_area, 171 const SkIRect& clip_area,
142 pp::ImageData* buffer, 172 webrtc::DesktopFrame* buffer,
143 const SkRegion& region) { 173 const SkRegion& region) {
144 DCHECK(context_->main_task_runner()->BelongsToCurrentThread()); 174 DCHECK(context_->main_task_runner()->BelongsToCurrentThread());
145 175
146 if (!frame_received_) { 176 if (!frame_received_) {
147 instance_->OnFirstFrameReceived(); 177 instance_->OnFirstFrameReceived();
148 frame_received_ = true; 178 frame_received_ = true;
149 } 179 }
150 // We cannot use the data in the buffer if its dimensions don't match the 180 // We cannot use the data in the buffer if its dimensions don't match the
151 // current view size. 181 // current view size.
152 // TODO(alexeypa): We could rescale and draw it (or even draw it without 182 // TODO(alexeypa): We could rescale and draw it (or even draw it without
153 // rescaling) to reduce the perceived lag while we are waiting for 183 // rescaling) to reduce the perceived lag while we are waiting for
154 // the properly scaled data. 184 // the properly scaled data.
155 if (view_size_ != view_size) { 185 if (view_size_ != view_size) {
156 FreeBuffer(buffer); 186 FreeBuffer(buffer);
157 InitiateDrawing(); 187 InitiateDrawing();
158 } else { 188 } else {
159 FlushBuffer(clip_area, buffer, region); 189 FlushBuffer(clip_area, buffer, region);
160 } 190 }
161 } 191 }
162 192
163 void PepperView::ReturnBuffer(pp::ImageData* buffer) { 193 void PepperView::ReturnBuffer(webrtc::DesktopFrame* buffer) {
164 DCHECK(context_->main_task_runner()->BelongsToCurrentThread()); 194 DCHECK(context_->main_task_runner()->BelongsToCurrentThread());
165 195
166 // Reuse the buffer if it is large enough, otherwise drop it on the floor 196 // Reuse the buffer if it is large enough, otherwise drop it on the floor
167 // and allocate a new one. 197 // and allocate a new one.
168 if (buffer->size().width() >= clip_area_.width() && 198 if (buffer->size().width() >= clip_area_.width() &&
169 buffer->size().height() >= clip_area_.height()) { 199 buffer->size().height() >= clip_area_.height()) {
170 producer_->DrawBuffer(buffer); 200 producer_->DrawBuffer(buffer);
171 } else { 201 } else {
172 FreeBuffer(buffer); 202 FreeBuffer(buffer);
173 InitiateDrawing(); 203 InitiateDrawing();
174 } 204 }
175 } 205 }
176 206
177 void PepperView::SetSourceSize(const SkISize& source_size, 207 void PepperView::SetSourceSize(const SkISize& source_size,
178 const SkIPoint& source_dpi) { 208 const SkIPoint& source_dpi) {
179 DCHECK(context_->main_task_runner()->BelongsToCurrentThread()); 209 DCHECK(context_->main_task_runner()->BelongsToCurrentThread());
180 210
181 if (source_size_ == source_size && source_dpi_ == source_dpi) 211 if (source_size_ == source_size && source_dpi_ == source_dpi)
182 return; 212 return;
183 213
184 source_size_ = source_size; 214 source_size_ = source_size;
185 source_dpi_ = source_dpi; 215 source_dpi_ = source_dpi;
186 216
187 // Notify JavaScript of the change in source size. 217 // Notify JavaScript of the change in source size.
188 instance_->SetDesktopSize(source_size, source_dpi); 218 instance_->SetDesktopSize(source_size, source_dpi);
189 } 219 }
190 220
191 pp::ImageData* PepperView::AllocateBuffer() { 221 webrtc::DesktopFrame* PepperView::AllocateBuffer() {
192 if (buffers_.size() >= kMaxPendingBuffersCount) 222 if (buffers_.size() >= kMaxPendingBuffersCount)
193 return NULL; 223 return NULL;
194 224
195 pp::Size pp_size = pp::Size(clip_area_.width(), clip_area_.height()); 225 if (clip_area_.width()==0 || clip_area_.height()==0)
196 if (pp_size.IsEmpty())
197 return NULL; 226 return NULL;
198 227
199 // Create an image buffer of the required size, but don't zero it. 228 // Create an image buffer of the required size, but don't zero it.
200 pp::ImageData* buffer = new pp::ImageData( 229 webrtc::DesktopFrame* buffer = new PepperDesktopFrame(
201 instance_, PP_IMAGEDATAFORMAT_BGRA_PREMUL, pp_size, false); 230 pp::ImageData(instance_,
202 if (buffer->is_null()) { 231 PP_IMAGEDATAFORMAT_BGRA_PREMUL,
232 pp::Size(clip_area_.width(),
233 clip_area_.height()),
234 false));
235 if (static_cast<PepperDesktopFrame*>(buffer)->get_buffer().is_null()) {
203 LOG(WARNING) << "Not enough memory for frame buffers."; 236 LOG(WARNING) << "Not enough memory for frame buffers.";
204 delete buffer; 237 delete buffer;
205 return NULL; 238 return NULL;
206 } 239 }
207 240
208 buffers_.push_back(buffer); 241 buffers_.push_back(buffer);
209 return buffer; 242 return buffer;
210 } 243 }
211 244
212 void PepperView::FreeBuffer(pp::ImageData* buffer) { 245 void PepperView::FreeBuffer(webrtc::DesktopFrame* buffer) {
213 DCHECK(std::find(buffers_.begin(), buffers_.end(), buffer) != buffers_.end()); 246 DCHECK(std::find(buffers_.begin(), buffers_.end(), buffer) != buffers_.end());
214 247
215 buffers_.remove(buffer); 248 buffers_.remove(buffer);
216 delete buffer; 249 delete buffer;
217 } 250 }
218 251
219 void PepperView::InitiateDrawing() { 252 void PepperView::InitiateDrawing() {
220 pp::ImageData* buffer = AllocateBuffer(); 253 webrtc::DesktopFrame* buffer = AllocateBuffer();
221 while (buffer) { 254 while (buffer) {
222 producer_->DrawBuffer(buffer); 255 producer_->DrawBuffer(buffer);
223 buffer = AllocateBuffer(); 256 buffer = AllocateBuffer();
224 } 257 }
225 } 258 }
226 259
227 void PepperView::FlushBuffer(const SkIRect& clip_area, 260 void PepperView::FlushBuffer(const SkIRect& clip_area,
228 pp::ImageData* buffer, 261 webrtc::DesktopFrame* buffer,
229 const SkRegion& region) { 262 const SkRegion& region) {
230 // Defer drawing if the flush is already in progress. 263 // Defer drawing if the flush is already in progress.
231 if (flush_pending_) { 264 if (flush_pending_) {
232 // |merge_buffer_| is guaranteed to be free here because we allocate only 265 // |merge_buffer_| is guaranteed to be free here because we allocate only
233 // two buffers simultaneously. If more buffers are allowed this code should 266 // two buffers simultaneously. If more buffers are allowed this code should
234 // apply all pending changes to the screen. 267 // apply all pending changes to the screen.
235 DCHECK(merge_buffer_ == NULL); 268 DCHECK(merge_buffer_ == NULL);
236 269
237 merge_clip_area_ = clip_area; 270 merge_clip_area_ = clip_area;
238 merge_buffer_ = buffer; 271 merge_buffer_ = buffer;
(...skipping 11 matching lines...) Expand all
250 // the latter could change from the time the buffer was drawn. 283 // the latter could change from the time the buffer was drawn.
251 if (!rect.intersect(clip_area_)) 284 if (!rect.intersect(clip_area_))
252 continue; 285 continue;
253 286
254 // Specify the rectangle coordinates relative to the clipping area. 287 // Specify the rectangle coordinates relative to the clipping area.
255 rect.offset(-clip_area.left(), -clip_area.top()); 288 rect.offset(-clip_area.left(), -clip_area.top());
256 289
257 // Pepper Graphics 2D has a strange and badly documented API that the 290 // Pepper Graphics 2D has a strange and badly documented API that the
258 // point here is the offset from the source rect. Why? 291 // point here is the offset from the source rect. Why?
259 graphics2d_.PaintImageData( 292 graphics2d_.PaintImageData(
260 *buffer, 293 static_cast<PepperDesktopFrame*>(buffer)->get_buffer(),
261 pp::Point(clip_area.left(), clip_area.top()), 294 pp::Point(clip_area.left(), clip_area.top()),
262 pp::Rect(rect.left(), rect.top(), rect.width(), rect.height())); 295 pp::Rect(rect.left(), rect.top(), rect.width(), rect.height()));
263 } 296 }
264 297
265 // Notify the producer that some parts of the region weren't painted because 298 // Notify the producer that some parts of the region weren't painted because
266 // the clipping area has changed already. 299 // the clipping area has changed already.
267 if (clip_area != clip_area_) { 300 if (clip_area != clip_area_) {
268 SkRegion not_painted = region; 301 SkRegion not_painted = region;
269 not_painted.op(clip_area_, SkRegion::kDifference_Op); 302 not_painted.op(clip_area_, SkRegion::kDifference_Op);
270 if (!not_painted.isEmpty()) { 303 if (!not_painted.isEmpty()) {
271 producer_->InvalidateRegion(not_painted); 304 producer_->InvalidateRegion(not_painted);
272 } 305 }
273 } 306 }
274 307
275 // Flush the updated areas to the screen. 308 // Flush the updated areas to the screen.
276 int error = graphics2d_.Flush( 309 int error = graphics2d_.Flush(
277 PpCompletionCallback(base::Bind( 310 PpCompletionCallback(base::Bind(
278 &PepperView::OnFlushDone, AsWeakPtr(), start_time, buffer))); 311 &PepperView::OnFlushDone, AsWeakPtr(), start_time, buffer)));
279 CHECK(error == PP_OK_COMPLETIONPENDING); 312 CHECK(error == PP_OK_COMPLETIONPENDING);
280 flush_pending_ = true; 313 flush_pending_ = true;
281 314
282 // If the buffer we just rendered has a shape then pass that to JavaScript. 315 // If the buffer we just rendered has a shape then pass that to JavaScript.
283 const SkRegion* buffer_shape = producer_->GetBufferShape(); 316 const SkRegion* buffer_shape = producer_->GetBufferShape();
284 if (buffer_shape) 317 if (buffer_shape)
285 instance_->SetDesktopShape(*buffer_shape); 318 instance_->SetDesktopShape(*buffer_shape);
286 } 319 }
287 320
288 void PepperView::OnFlushDone(base::Time paint_start, 321 void PepperView::OnFlushDone(base::Time paint_start,
289 pp::ImageData* buffer, 322 webrtc::DesktopFrame* buffer,
290 int result) { 323 int result) {
291 DCHECK(context_->main_task_runner()->BelongsToCurrentThread()); 324 DCHECK(context_->main_task_runner()->BelongsToCurrentThread());
292 DCHECK(flush_pending_); 325 DCHECK(flush_pending_);
293 326
294 instance_->GetStats()->video_paint_ms()->Record( 327 instance_->GetStats()->video_paint_ms()->Record(
295 (base::Time::Now() - paint_start).InMilliseconds()); 328 (base::Time::Now() - paint_start).InMilliseconds());
296 329
297 flush_pending_ = false; 330 flush_pending_ = false;
298 ReturnBuffer(buffer); 331 ReturnBuffer(buffer);
299 332
300 // If there is a buffer queued for rendering then render it now. 333 // If there is a buffer queued for rendering then render it now.
301 if (merge_buffer_ != NULL) { 334 if (merge_buffer_ != NULL) {
302 buffer = merge_buffer_; 335 buffer = merge_buffer_;
303 merge_buffer_ = NULL; 336 merge_buffer_ = NULL;
304 FlushBuffer(merge_clip_area_, buffer, merge_region_); 337 FlushBuffer(merge_clip_area_, buffer, merge_region_);
305 } 338 }
306 } 339 }
307 340
308 } // namespace remoting 341 } // namespace remoting
OLDNEW
« no previous file with comments | « remoting/client/plugin/pepper_view.h ('k') | remoting/client/rectangle_update_decoder.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698