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

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: Switch to using existing webrtc::DesktopFrame class 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
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_desktop_frame.h"
25 #include "remoting/client/plugin/pepper_util.h" 26 #include "remoting/client/plugin/pepper_util.h"
26 27
27 using base::Passed; 28 using base::Passed;
28 29
29 namespace remoting { 30 namespace remoting {
30 31
31 namespace { 32 namespace {
32 33
33 // The maximum number of image buffers to be allocated at any point of time. 34 // The maximum number of image buffers to be allocated at any point of time.
34 const size_t kMaxPendingBuffersCount = 2; 35 const size_t kMaxPendingBuffersCount = 2;
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after
132 } 133 }
133 134
134 if (view_changed) { 135 if (view_changed) {
135 producer_->SetOutputSizeAndClip(view_size_, clip_area_); 136 producer_->SetOutputSizeAndClip(view_size_, clip_area_);
136 InitiateDrawing(); 137 InitiateDrawing();
137 } 138 }
138 } 139 }
139 140
140 void PepperView::ApplyBuffer(const SkISize& view_size, 141 void PepperView::ApplyBuffer(const SkISize& view_size,
141 const SkIRect& clip_area, 142 const SkIRect& clip_area,
142 pp::ImageData* buffer, 143 webrtc::DesktopFrame* buffer,
143 const SkRegion& region) { 144 const SkRegion& region) {
144 DCHECK(context_->main_task_runner()->BelongsToCurrentThread()); 145 DCHECK(context_->main_task_runner()->BelongsToCurrentThread());
145 146
146 if (!frame_received_) { 147 if (!frame_received_) {
147 instance_->OnFirstFrameReceived(); 148 instance_->OnFirstFrameReceived();
148 frame_received_ = true; 149 frame_received_ = true;
149 } 150 }
150 // We cannot use the data in the buffer if its dimensions don't match the 151 // We cannot use the data in the buffer if its dimensions don't match the
151 // current view size. 152 // current view size.
152 // TODO(alexeypa): We could rescale and draw it (or even draw it without 153 // 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 154 // rescaling) to reduce the perceived lag while we are waiting for
154 // the properly scaled data. 155 // the properly scaled data.
155 if (view_size_ != view_size) { 156 if (view_size_ != view_size) {
156 FreeBuffer(buffer); 157 FreeBuffer(buffer);
157 InitiateDrawing(); 158 InitiateDrawing();
158 } else { 159 } else {
159 FlushBuffer(clip_area, buffer, region); 160 FlushBuffer(clip_area, buffer, region);
160 } 161 }
161 } 162 }
162 163
163 void PepperView::ReturnBuffer(pp::ImageData* buffer) { 164 void PepperView::ReturnBuffer(webrtc::DesktopFrame* buffer) {
164 DCHECK(context_->main_task_runner()->BelongsToCurrentThread()); 165 DCHECK(context_->main_task_runner()->BelongsToCurrentThread());
165 166
166 // Reuse the buffer if it is large enough, otherwise drop it on the floor 167 // Reuse the buffer if it is large enough, otherwise drop it on the floor
167 // and allocate a new one. 168 // and allocate a new one.
168 if (buffer->size().width() >= clip_area_.width() && 169 if (buffer->size().width() >= clip_area_.width() &&
169 buffer->size().height() >= clip_area_.height()) { 170 buffer->size().height() >= clip_area_.height()) {
170 producer_->DrawBuffer(buffer); 171 producer_->DrawBuffer(buffer);
171 } else { 172 } else {
172 FreeBuffer(buffer); 173 FreeBuffer(buffer);
173 InitiateDrawing(); 174 InitiateDrawing();
174 } 175 }
175 } 176 }
176 177
177 void PepperView::SetSourceSize(const SkISize& source_size, 178 void PepperView::SetSourceSize(const SkISize& source_size,
178 const SkIPoint& source_dpi) { 179 const SkIPoint& source_dpi) {
179 DCHECK(context_->main_task_runner()->BelongsToCurrentThread()); 180 DCHECK(context_->main_task_runner()->BelongsToCurrentThread());
180 181
181 if (source_size_ == source_size && source_dpi_ == source_dpi) 182 if (source_size_ == source_size && source_dpi_ == source_dpi)
182 return; 183 return;
183 184
184 source_size_ = source_size; 185 source_size_ = source_size;
185 source_dpi_ = source_dpi; 186 source_dpi_ = source_dpi;
186 187
187 // Notify JavaScript of the change in source size. 188 // Notify JavaScript of the change in source size.
188 instance_->SetDesktopSize(source_size, source_dpi); 189 instance_->SetDesktopSize(source_size, source_dpi);
189 } 190 }
190 191
191 pp::ImageData* PepperView::AllocateBuffer() { 192 webrtc::DesktopFrame* PepperView::AllocateBuffer() {
192 if (buffers_.size() >= kMaxPendingBuffersCount) 193 if (buffers_.size() >= kMaxPendingBuffersCount)
193 return NULL; 194 return NULL;
194 195
195 pp::Size pp_size = pp::Size(clip_area_.width(), clip_area_.height()); 196 if (clip_area_.width()==0 || clip_area_.height()==0)
196 if (pp_size.IsEmpty())
197 return NULL; 197 return NULL;
198 198
199 // Create an image buffer of the required size, but don't zero it. 199 // Create an image buffer of the required size, but don't zero it.
200 pp::ImageData* buffer = new pp::ImageData( 200 webrtc::DesktopFrame* buffer = PepperDesktopFrame::Create(
201 instance_, PP_IMAGEDATAFORMAT_BGRA_PREMUL, pp_size, false); 201 instance_, clip_area_.width(), clip_area_.height());
202 if (buffer->is_null()) { 202 if (static_cast<PepperDesktopFrame*>(buffer)->data_object().is_null()) {
203 LOG(WARNING) << "Not enough memory for frame buffers."; 203 LOG(WARNING) << "Not enough memory for frame buffers.";
204 delete buffer; 204 delete buffer;
205 return NULL; 205 return NULL;
206 } 206 }
207 207
208 buffers_.push_back(buffer); 208 buffers_.push_back(buffer);
209 return buffer; 209 return buffer;
210 } 210 }
211 211
212 void PepperView::FreeBuffer(pp::ImageData* buffer) { 212 void PepperView::FreeBuffer(webrtc::DesktopFrame* buffer) {
213 DCHECK(std::find(buffers_.begin(), buffers_.end(), buffer) != buffers_.end()); 213 DCHECK(std::find(buffers_.begin(), buffers_.end(), buffer) != buffers_.end());
214 214
215 buffers_.remove(buffer); 215 buffers_.remove(buffer);
216 delete buffer; 216 delete buffer;
217 } 217 }
218 218
219 void PepperView::InitiateDrawing() { 219 void PepperView::InitiateDrawing() {
220 pp::ImageData* buffer = AllocateBuffer(); 220 webrtc::DesktopFrame* buffer = AllocateBuffer();
221 while (buffer) { 221 while (buffer) {
222 producer_->DrawBuffer(buffer); 222 producer_->DrawBuffer(buffer);
223 buffer = AllocateBuffer(); 223 buffer = AllocateBuffer();
224 } 224 }
225 } 225 }
226 226
227 void PepperView::FlushBuffer(const SkIRect& clip_area, 227 void PepperView::FlushBuffer(const SkIRect& clip_area,
228 pp::ImageData* buffer, 228 webrtc::DesktopFrame* buffer,
229 const SkRegion& region) { 229 const SkRegion& region) {
230 // Defer drawing if the flush is already in progress. 230 // Defer drawing if the flush is already in progress.
231 if (flush_pending_) { 231 if (flush_pending_) {
232 // |merge_buffer_| is guaranteed to be free here because we allocate only 232 // |merge_buffer_| is guaranteed to be free here because we allocate only
233 // two buffers simultaneously. If more buffers are allowed this code should 233 // two buffers simultaneously. If more buffers are allowed this code should
234 // apply all pending changes to the screen. 234 // apply all pending changes to the screen.
235 DCHECK(merge_buffer_ == NULL); 235 DCHECK(merge_buffer_ == NULL);
236 236
237 merge_clip_area_ = clip_area; 237 merge_clip_area_ = clip_area;
238 merge_buffer_ = buffer; 238 merge_buffer_ = buffer;
(...skipping 11 matching lines...) Expand all
250 // the latter could change from the time the buffer was drawn. 250 // the latter could change from the time the buffer was drawn.
251 if (!rect.intersect(clip_area_)) 251 if (!rect.intersect(clip_area_))
252 continue; 252 continue;
253 253
254 // Specify the rectangle coordinates relative to the clipping area. 254 // Specify the rectangle coordinates relative to the clipping area.
255 rect.offset(-clip_area.left(), -clip_area.top()); 255 rect.offset(-clip_area.left(), -clip_area.top());
256 256
257 // Pepper Graphics 2D has a strange and badly documented API that the 257 // Pepper Graphics 2D has a strange and badly documented API that the
258 // point here is the offset from the source rect. Why? 258 // point here is the offset from the source rect. Why?
259 graphics2d_.PaintImageData( 259 graphics2d_.PaintImageData(
260 *buffer, 260 static_cast<PepperDesktopFrame*>(buffer)->data_object(),
261 pp::Point(clip_area.left(), clip_area.top()), 261 pp::Point(clip_area.left(), clip_area.top()),
262 pp::Rect(rect.left(), rect.top(), rect.width(), rect.height())); 262 pp::Rect(rect.left(), rect.top(), rect.width(), rect.height()));
263 } 263 }
264 264
265 // Notify the producer that some parts of the region weren't painted because 265 // Notify the producer that some parts of the region weren't painted because
266 // the clipping area has changed already. 266 // the clipping area has changed already.
267 if (clip_area != clip_area_) { 267 if (clip_area != clip_area_) {
268 SkRegion not_painted = region; 268 SkRegion not_painted = region;
269 not_painted.op(clip_area_, SkRegion::kDifference_Op); 269 not_painted.op(clip_area_, SkRegion::kDifference_Op);
270 if (!not_painted.isEmpty()) { 270 if (!not_painted.isEmpty()) {
271 producer_->InvalidateRegion(not_painted); 271 producer_->InvalidateRegion(not_painted);
272 } 272 }
273 } 273 }
274 274
275 // Flush the updated areas to the screen. 275 // Flush the updated areas to the screen.
276 int error = graphics2d_.Flush( 276 int error = graphics2d_.Flush(
277 PpCompletionCallback(base::Bind( 277 PpCompletionCallback(base::Bind(
278 &PepperView::OnFlushDone, AsWeakPtr(), start_time, buffer))); 278 &PepperView::OnFlushDone, AsWeakPtr(), start_time, buffer)));
279 CHECK(error == PP_OK_COMPLETIONPENDING); 279 CHECK(error == PP_OK_COMPLETIONPENDING);
280 flush_pending_ = true; 280 flush_pending_ = true;
281 281
282 // If the buffer we just rendered has a shape then pass that to JavaScript. 282 // If the buffer we just rendered has a shape then pass that to JavaScript.
283 const SkRegion* buffer_shape = producer_->GetBufferShape(); 283 const SkRegion* buffer_shape = producer_->GetBufferShape();
284 if (buffer_shape) 284 if (buffer_shape)
285 instance_->SetDesktopShape(*buffer_shape); 285 instance_->SetDesktopShape(*buffer_shape);
286 } 286 }
287 287
288 void PepperView::OnFlushDone(base::Time paint_start, 288 void PepperView::OnFlushDone(base::Time paint_start,
289 pp::ImageData* buffer, 289 webrtc::DesktopFrame* buffer,
290 int result) { 290 int result) {
291 DCHECK(context_->main_task_runner()->BelongsToCurrentThread()); 291 DCHECK(context_->main_task_runner()->BelongsToCurrentThread());
292 DCHECK(flush_pending_); 292 DCHECK(flush_pending_);
293 293
294 instance_->GetStats()->video_paint_ms()->Record( 294 instance_->GetStats()->video_paint_ms()->Record(
295 (base::Time::Now() - paint_start).InMilliseconds()); 295 (base::Time::Now() - paint_start).InMilliseconds());
296 296
297 flush_pending_ = false; 297 flush_pending_ = false;
298 ReturnBuffer(buffer); 298 ReturnBuffer(buffer);
299 299
300 // If there is a buffer queued for rendering then render it now. 300 // If there is a buffer queued for rendering then render it now.
301 if (merge_buffer_ != NULL) { 301 if (merge_buffer_ != NULL) {
302 buffer = merge_buffer_; 302 buffer = merge_buffer_;
303 merge_buffer_ = NULL; 303 merge_buffer_ = NULL;
304 FlushBuffer(merge_clip_area_, buffer, merge_region_); 304 FlushBuffer(merge_clip_area_, buffer, merge_region_);
305 } 305 }
306 } 306 }
307 307
308 } // namespace remoting 308 } // namespace remoting
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698