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_delegate
)
39 : window_delegate_(window_delegate
), buffers_(), front_buffer_(0) {
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_delegate_
->GetController();
58 // For the display buffers use the mode size since a |viewport_size| smaller
59 // than the display size will not scanout.
60 for (size_t i
= 0; i
< arraysize(buffers_
); ++i
)
61 buffers_
[i
] = AllocateBuffer(controller
->GetAllocationDrmDevice(),
62 controller
->GetModeSize());
65 void DrmSurface::PresentCanvas(const gfx::Rect
& damage
) {
66 DCHECK(base::MessageLoopForUI::IsCurrent());
68 HardwareDisplayController
* controller
= window_delegate_
->GetController();
72 DCHECK(buffers_
[front_buffer_
^ 1].get());
73 controller
->QueueOverlayPlane(OverlayPlane(buffers_
[front_buffer_
^ 1]));
75 UpdateNativeSurface(damage
);
76 controller
->SchedulePageFlip(false /* is_sync */,
77 base::Bind(&base::DoNothing
));
79 // Update our front buffer pointer.
83 scoped_ptr
<gfx::VSyncProvider
> DrmSurface::CreateVSyncProvider() {
84 return make_scoped_ptr(new DrmVSyncProvider(window_delegate_
));
87 void DrmSurface::UpdateNativeSurface(const gfx::Rect
& damage
) {
88 SkCanvas
* canvas
= buffers_
[front_buffer_
^ 1]->GetCanvas();
90 // The DrmSurface is double buffered, so the current back buffer is
91 // missing the previous update. Expand damage region.
92 SkRect real_damage
= RectToSkRect(UnionRects(damage
, last_damage_
));
94 // Copy damage region.
95 skia::RefPtr
<SkImage
> image
= skia::AdoptRef(surface_
->newImageSnapshot());
96 canvas
->drawImageRect(image
.get(), &real_damage
, real_damage
, NULL
);
98 last_damage_
= damage
;