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/dri/dri_surface.h"
7 #include "base/logging.h"
8 #include "base/message_loop/message_loop.h"
9 #include "third_party/skia/include/core/SkCanvas.h"
10 #include "third_party/skia/include/core/SkSurface.h"
11 #include "ui/gfx/geometry/rect.h"
12 #include "ui/gfx/skia_util.h"
13 #include "ui/ozone/platform/dri/dri_buffer.h"
14 #include "ui/ozone/platform/dri/dri_vsync_provider.h"
15 #include "ui/ozone/platform/dri/dri_wrapper.h"
16 #include "ui/ozone/platform/dri/hardware_display_controller.h"
22 scoped_refptr<DriBuffer> AllocateBuffer(DriWrapper* dri,
23 const gfx::Size& size) {
24 scoped_refptr<DriBuffer> buffer(new DriBuffer(dri));
25 SkImageInfo info = SkImageInfo::MakeN32Premul(size.width(), size.height());
27 bool initialized = buffer->Initialize(info);
28 DCHECK(initialized) << "Failed to create drm buffer.";
35 DriSurface::DriSurface(
37 const base::WeakPtr<HardwareDisplayController>& controller)
41 controller_(controller) {
44 DriSurface::~DriSurface() {
47 skia::RefPtr<SkCanvas> DriSurface::GetCanvas() {
48 return skia::SharePtr(surface_->getCanvas());
51 void DriSurface::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));
59 // For the display buffers use the mode size since a |viewport_size| smaller
60 // than the display size will not scanout.
61 gfx::Size mode_size(controller_->get_mode().hdisplay,
62 controller_->get_mode().vdisplay);
63 for (size_t i = 0; i < arraysize(buffers_); ++i)
64 buffers_[i] = AllocateBuffer(dri_, mode_size);
67 void DriSurface::PresentCanvas(const gfx::Rect& damage) {
68 DCHECK(base::MessageLoopForUI::IsCurrent());
69 DCHECK(buffers_[front_buffer_ ^ 1]);
74 controller_->QueueOverlayPlane(OverlayPlane(buffers_[front_buffer_ ^ 1]));
76 UpdateNativeSurface(damage);
77 controller_->SchedulePageFlip();
78 controller_->WaitForPageFlipEvent();
80 // Update our front buffer pointer.
84 scoped_ptr<gfx::VSyncProvider> DriSurface::CreateVSyncProvider() {
85 return scoped_ptr<gfx::VSyncProvider>(new DriVSyncProvider(controller_));
88 void DriSurface::UpdateNativeSurface(const gfx::Rect& damage) {
89 SkCanvas* canvas = buffers_[front_buffer_ ^ 1]->GetCanvas();
91 // The DriSurface is double buffered, so the current back buffer is
92 // missing the previous update. Expand damage region.
93 SkRect real_damage = RectToSkRect(UnionRects(damage, last_damage_));
95 // Copy damage region.
96 skia::RefPtr<SkImage> image = skia::AdoptRef(surface_->newImageSnapshot());
97 image->draw(canvas, &real_damage, real_damage, NULL);
99 last_damage_ = damage;