1 // Copyright 2014 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 "ui/ozone/platform/drm/gpu/drm_surface.h"
7 #include "base/bind_helpers.h"
8 #include "base/logging.h"
9 #include "base/message_loop/message_loop.h"
10 #include "third_party/skia/include/core/SkCanvas.h"
11 #include "third_party/skia/include/core/SkSurface.h"
12 #include "ui/gfx/geometry/rect.h"
13 #include "ui/gfx/skia_util.h"
14 #include "ui/ozone/platform/drm/gpu/drm_buffer.h"
15 #include "ui/ozone/platform/drm/gpu/drm_device.h"
16 #include "ui/ozone/platform/drm/gpu/drm_vsync_provider.h"
17 #include "ui/ozone/platform/drm/gpu/drm_window.h"
18 #include "ui/ozone/platform/drm/gpu/hardware_display_controller.h"
24 scoped_refptr
<DrmBuffer
> AllocateBuffer(const scoped_refptr
<DrmDevice
>& drm
,
25 const gfx::Size
& size
) {
26 scoped_refptr
<DrmBuffer
> buffer(new DrmBuffer(drm
));
27 SkImageInfo info
= SkImageInfo::MakeN32Premul(size
.width(), size
.height());
30 buffer
->Initialize(info
, true /* should_register_framebuffer */);
31 DCHECK(initialized
) << "Failed to create drm buffer.";
38 DrmSurface::DrmSurface(DrmWindow
* window
)
39 : window_(window
), weak_ptr_factory_(this) {
42 DrmSurface::~DrmSurface() {
45 skia::RefPtr
<SkSurface
> DrmSurface::GetSurface() {
49 void DrmSurface::ResizeCanvas(const gfx::Size
& viewport_size
) {
50 SkImageInfo info
= SkImageInfo::MakeN32(
51 viewport_size
.width(), viewport_size
.height(), kOpaque_SkAlphaType
);
52 surface_
= skia::AdoptRef(SkSurface::NewRaster(info
));
54 HardwareDisplayController
* controller
= window_
->GetController();
58 // For the display buffers use the mode size since a |viewport_size| smaller
59 // than the display size will not scanout.
60 front_buffer_
= AllocateBuffer(controller
->GetAllocationDrmDevice(),
61 controller
->GetModeSize());
62 back_buffer_
= AllocateBuffer(controller
->GetAllocationDrmDevice(),
63 controller
->GetModeSize());
66 void DrmSurface::PresentCanvas(const gfx::Rect
& damage
) {
67 DCHECK(base::MessageLoopForUI::IsCurrent());
69 // Create a snapshot of the requested drawing. If we get here again before
70 // presenting, just add the additional damage.
71 pending_image_damage_
.Union(damage
);
72 pending_image_
= skia::AdoptRef(surface_
->newImageSnapshot());
74 if (!pending_pageflip_
)
78 scoped_ptr
<gfx::VSyncProvider
> DrmSurface::CreateVSyncProvider() {
79 return make_scoped_ptr(new DrmVSyncProvider(window_
));
82 void DrmSurface::SchedulePageFlip() {
84 SkCanvas
* canvas
= back_buffer_
->GetCanvas();
86 // The DrmSurface is double buffered, so the current back buffer is
87 // missing the previous update. Expand damage region.
89 RectToSkRect(UnionRects(pending_image_damage_
, last_damage_
));
91 // Copy damage region.
92 canvas
->drawImageRect(pending_image_
.get(), &real_damage
, real_damage
, NULL
);
93 last_damage_
= pending_image_damage_
;
95 pending_image_
= nullptr;
96 pending_image_damage_
= gfx::Rect();
98 window_
->QueueOverlayPlane(OverlayPlane(back_buffer_
));
100 // Update our front buffer pointer.
101 std::swap(front_buffer_
, back_buffer_
);
102 pending_pageflip_
= window_
->SchedulePageFlip(
104 base::Bind(&DrmSurface::OnPageFlip
, weak_ptr_factory_
.GetWeakPtr()));
107 void DrmSurface::OnPageFlip(gfx::SwapResult result
) {
108 pending_pageflip_
= false;