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"
23 void EmptyPageFlipCallback(gfx::SwapResult result
) {
26 scoped_refptr
<DrmBuffer
> AllocateBuffer(const scoped_refptr
<DrmDevice
>& drm
,
27 const gfx::Size
& size
) {
28 scoped_refptr
<DrmBuffer
> buffer(new DrmBuffer(drm
));
29 SkImageInfo info
= SkImageInfo::MakeN32Premul(size
.width(), size
.height());
32 buffer
->Initialize(info
, true /* should_register_framebuffer */);
33 DCHECK(initialized
) << "Failed to create drm buffer.";
40 DrmSurface::DrmSurface(DrmWindow
* window_delegate
)
41 : window_delegate_(window_delegate
), buffers_(), front_buffer_(0) {
44 DrmSurface::~DrmSurface() {
47 skia::RefPtr
<SkSurface
> DrmSurface::GetSurface() {
51 void DrmSurface::ResizeCanvas(const gfx::Size
& viewport_size
) {
52 SkImageInfo info
= SkImageInfo::MakeN32(
53 viewport_size
.width(), viewport_size
.height(), kOpaque_SkAlphaType
);
54 surface_
= skia::AdoptRef(SkSurface::NewRaster(info
));
56 HardwareDisplayController
* controller
= window_delegate_
->GetController();
60 // For the display buffers use the mode size since a |viewport_size| smaller
61 // than the display size will not scanout.
62 for (size_t i
= 0; i
< arraysize(buffers_
); ++i
)
63 buffers_
[i
] = AllocateBuffer(controller
->GetAllocationDrmDevice(),
64 controller
->GetModeSize());
67 void DrmSurface::PresentCanvas(const gfx::Rect
& damage
) {
68 DCHECK(base::MessageLoopForUI::IsCurrent());
70 DCHECK(buffers_
[front_buffer_
^ 1].get());
71 window_delegate_
->QueueOverlayPlane(
72 OverlayPlane(buffers_
[front_buffer_
^ 1]));
74 UpdateNativeSurface(damage
);
75 window_delegate_
->SchedulePageFlip(true /* is_sync */,
76 base::Bind(&EmptyPageFlipCallback
));
78 // Update our front buffer pointer.
82 scoped_ptr
<gfx::VSyncProvider
> DrmSurface::CreateVSyncProvider() {
83 return make_scoped_ptr(new DrmVSyncProvider(window_delegate_
));
86 void DrmSurface::UpdateNativeSurface(const gfx::Rect
& damage
) {
87 SkCanvas
* canvas
= buffers_
[front_buffer_
^ 1]->GetCanvas();
89 // The DrmSurface is double buffered, so the current back buffer is
90 // missing the previous update. Expand damage region.
91 SkRect real_damage
= RectToSkRect(UnionRects(damage
, last_damage_
));
93 // Copy damage region.
94 skia::RefPtr
<SkImage
> image
= skia::AdoptRef(surface_
->newImageSnapshot());
95 canvas
->drawImageRect(image
.get(), &real_damage
, real_damage
, NULL
);
97 last_damage_
= damage
;