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
3 // found in the LICENSE file.
5 #include "remoting/client/plugin/pepper_view.h"
9 #include "base/message_loop.h"
10 #include "base/strings/string_util.h"
11 #include "base/synchronization/waitable_event.h"
12 #include "base/time/time.h"
13 #include "ppapi/cpp/completion_callback.h"
14 #include "ppapi/cpp/dev/graphics_2d_dev.h"
15 #include "ppapi/cpp/dev/view_dev.h"
16 #include "ppapi/cpp/image_data.h"
17 #include "ppapi/cpp/point.h"
18 #include "ppapi/cpp/rect.h"
19 #include "ppapi/cpp/size.h"
20 #include "remoting/base/util.h"
21 #include "remoting/client/chromoting_stats.h"
22 #include "remoting/client/client_context.h"
23 #include "remoting/client/frame_producer.h"
24 #include "remoting/client/plugin/chromoting_instance.h"
25 #include "remoting/client/plugin/pepper_util.h"
33 // The maximum number of image buffers to be allocated at any point of time.
34 const size_t kMaxPendingBuffersCount
= 2;
38 PepperView::PepperView(ChromotingInstance
* instance
,
39 ClientContext
* context
,
40 FrameProducer
* producer
)
41 : instance_(instance
),
45 merge_clip_area_(SkIRect::MakeEmpty()),
46 dips_size_(SkISize::Make(0, 0)),
47 dips_to_device_scale_(1.0f
),
48 view_size_(SkISize::Make(0, 0)),
49 dips_to_view_scale_(1.0f
),
50 clip_area_(SkIRect::MakeEmpty()),
51 source_size_(SkISize::Make(0, 0)),
52 source_dpi_(SkIPoint::Make(0, 0)),
53 flush_pending_(false),
54 is_initialized_(false),
55 frame_received_(false) {
59 PepperView::~PepperView() {
60 // The producer should now return any pending buffers. At this point, however,
61 // ReturnBuffer() tasks scheduled by the producer will not be delivered,
62 // so we free all the buffers once the producer's queue is empty.
63 base::WaitableEvent
done_event(true, false);
64 producer_
->RequestReturnBuffers(
65 base::Bind(&base::WaitableEvent::Signal
, base::Unretained(&done_event
)));
69 while (!buffers_
.empty()) {
70 FreeBuffer(buffers_
.front());
74 void PepperView::SetView(const pp::View
& view
) {
75 bool view_changed
= false;
77 pp::Rect pp_size
= view
.GetRect();
78 SkISize new_dips_size
= SkISize::Make(pp_size
.width(), pp_size
.height());
79 pp::ViewDev
view_dev(view
);
80 float new_dips_to_device_scale
= view_dev
.GetDeviceScale();
82 if (dips_size_
!= new_dips_size
||
83 dips_to_device_scale_
!= new_dips_to_device_scale
) {
85 dips_to_device_scale_
= new_dips_to_device_scale
;
86 dips_size_
= new_dips_size
;
88 // If |dips_to_device_scale_| is > 1.0 then the device is high-DPI, and
89 // there are actually |view_device_scale_| physical pixels for every one
90 // Density Independent Pixel (DIP). If we specify a scale of 1.0 to
91 // Graphics2D then we can render at DIP resolution and let PPAPI up-scale
92 // for high-DPI devices.
93 dips_to_view_scale_
= 1.0f
;
94 view_size_
= dips_size_
;
96 // If the view's DIP dimensions don't match the source then let the frame
97 // producer do the scaling, and render at device resolution.
98 if (dips_size_
!= source_size_
) {
99 dips_to_view_scale_
= dips_to_device_scale_
;
100 view_size_
= SkISize::Make(
101 ceilf(dips_size_
.width() * dips_to_view_scale_
),
102 ceilf(dips_size_
.height() * dips_to_view_scale_
));
105 // Create a 2D rendering context at the chosen frame dimensions.
106 pp::Size pp_size
= pp::Size(view_size_
.width(), view_size_
.height());
107 graphics2d_
= pp::Graphics2D(instance_
, pp_size
, false);
109 // Specify the scale from our coordinates to DIPs.
110 pp::Graphics2D_Dev
graphics2d_dev(graphics2d_
);
111 graphics2d_dev
.SetScale(1.0f
/ dips_to_view_scale_
);
113 bool result
= instance_
->BindGraphics(graphics2d_
);
115 // There is no good way to handle this error currently.
116 DCHECK(result
) << "Couldn't bind the device context.";
119 pp::Rect pp_clip
= view
.GetClipRect();
120 SkIRect new_clip
= SkIRect::MakeLTRB(
121 floorf(pp_clip
.x() * dips_to_view_scale_
),
122 floorf(pp_clip
.y() * dips_to_view_scale_
),
123 ceilf(pp_clip
.right() * dips_to_view_scale_
),
124 ceilf(pp_clip
.bottom() * dips_to_view_scale_
));
125 if (clip_area_
!= new_clip
) {
128 // YUV to RGB conversion may require even X and Y coordinates for
129 // the top left corner of the clipping area.
130 clip_area_
= AlignRect(new_clip
);
131 clip_area_
.intersect(SkIRect::MakeSize(view_size_
));
135 producer_
->SetOutputSizeAndClip(view_size_
, clip_area_
);
140 void PepperView::ApplyBuffer(const SkISize
& view_size
,
141 const SkIRect
& clip_area
,
142 pp::ImageData
* buffer
,
143 const SkRegion
& region
) {
144 DCHECK(context_
->main_task_runner()->BelongsToCurrentThread());
146 if (!frame_received_
) {
147 instance_
->OnFirstFrameReceived();
148 frame_received_
= true;
150 // We cannot use the data in the buffer if its dimensions don't match the
151 // current view size.
152 // 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 // the properly scaled data.
155 if (view_size_
!= view_size
) {
159 FlushBuffer(clip_area
, buffer
, region
);
163 void PepperView::ReturnBuffer(pp::ImageData
* buffer
) {
164 DCHECK(context_
->main_task_runner()->BelongsToCurrentThread());
166 // Reuse the buffer if it is large enough, otherwise drop it on the floor
167 // and allocate a new one.
168 if (buffer
->size().width() >= clip_area_
.width() &&
169 buffer
->size().height() >= clip_area_
.height()) {
170 producer_
->DrawBuffer(buffer
);
177 void PepperView::SetSourceSize(const SkISize
& source_size
,
178 const SkIPoint
& source_dpi
) {
179 DCHECK(context_
->main_task_runner()->BelongsToCurrentThread());
181 if (source_size_
== source_size
&& source_dpi_
== source_dpi
)
184 source_size_
= source_size
;
185 source_dpi_
= source_dpi
;
187 // Notify JavaScript of the change in source size.
188 instance_
->SetDesktopSize(source_size
, source_dpi
);
191 pp::ImageData
* PepperView::AllocateBuffer() {
192 if (buffers_
.size() >= kMaxPendingBuffersCount
)
195 pp::Size pp_size
= pp::Size(clip_area_
.width(), clip_area_
.height());
196 if (pp_size
.IsEmpty())
199 // Create an image buffer of the required size, but don't zero it.
200 pp::ImageData
* buffer
= new pp::ImageData(
201 instance_
, PP_IMAGEDATAFORMAT_BGRA_PREMUL
, pp_size
, false);
202 if (buffer
->is_null()) {
203 LOG(WARNING
) << "Not enough memory for frame buffers.";
208 buffers_
.push_back(buffer
);
212 void PepperView::FreeBuffer(pp::ImageData
* buffer
) {
213 DCHECK(std::find(buffers_
.begin(), buffers_
.end(), buffer
) != buffers_
.end());
215 buffers_
.remove(buffer
);
219 void PepperView::InitiateDrawing() {
220 pp::ImageData
* buffer
= AllocateBuffer();
222 producer_
->DrawBuffer(buffer
);
223 buffer
= AllocateBuffer();
227 void PepperView::FlushBuffer(const SkIRect
& clip_area
,
228 pp::ImageData
* buffer
,
229 const SkRegion
& region
) {
230 // Defer drawing if the flush is already in progress.
231 if (flush_pending_
) {
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
234 // apply all pending changes to the screen.
235 DCHECK(merge_buffer_
== NULL
);
237 merge_clip_area_
= clip_area
;
238 merge_buffer_
= buffer
;
239 merge_region_
= region
;
243 // Notify Pepper API about the updated areas and flush pixels to the screen.
244 base::Time start_time
= base::Time::Now();
246 for (SkRegion::Iterator
i(region
); !i
.done(); i
.next()) {
247 SkIRect rect
= i
.rect();
249 // Re-clip |region| with the current clipping area |clip_area_| because
250 // the latter could change from the time the buffer was drawn.
251 if (!rect
.intersect(clip_area_
))
254 // Specify the rectangle coordinates relative to the clipping area.
255 rect
.offset(-clip_area
.left(), -clip_area
.top());
257 // Pepper Graphics 2D has a strange and badly documented API that the
258 // point here is the offset from the source rect. Why?
259 graphics2d_
.PaintImageData(
261 pp::Point(clip_area
.left(), clip_area
.top()),
262 pp::Rect(rect
.left(), rect
.top(), rect
.width(), rect
.height()));
265 // Notify the producer that some parts of the region weren't painted because
266 // the clipping area has changed already.
267 if (clip_area
!= clip_area_
) {
268 SkRegion not_painted
= region
;
269 not_painted
.op(clip_area_
, SkRegion::kDifference_Op
);
270 if (!not_painted
.isEmpty()) {
271 producer_
->InvalidateRegion(not_painted
);
275 // Flush the updated areas to the screen.
276 int error
= graphics2d_
.Flush(
277 PpCompletionCallback(base::Bind(
278 &PepperView::OnFlushDone
, AsWeakPtr(), start_time
, buffer
)));
279 CHECK(error
== PP_OK_COMPLETIONPENDING
);
280 flush_pending_
= true;
282 // If the buffer we just rendered has a shape then pass that to JavaScript.
283 const SkRegion
* buffer_shape
= producer_
->GetBufferShape();
285 instance_
->SetDesktopShape(*buffer_shape
);
288 void PepperView::OnFlushDone(base::Time paint_start
,
289 pp::ImageData
* buffer
,
291 DCHECK(context_
->main_task_runner()->BelongsToCurrentThread());
292 DCHECK(flush_pending_
);
294 instance_
->GetStats()->video_paint_ms()->Record(
295 (base::Time::Now() - paint_start
).InMilliseconds());
297 flush_pending_
= false;
298 ReturnBuffer(buffer
);
300 // If there is a buffer queued for rendering then render it now.
301 if (merge_buffer_
!= NULL
) {
302 buffer
= merge_buffer_
;
303 merge_buffer_
= NULL
;
304 FlushBuffer(merge_clip_area_
, buffer
, merge_region_
);
308 } // namespace remoting