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_buffer.h"
7 #include "base/logging.h"
8 #include "ui/ozone/platform/dri/drm_device.h"
14 // Modesetting cannot happen from a buffer with transparencies. Return the size
15 // of a pixel without alpha.
16 uint8_t GetColorDepth(SkColorType type
) {
18 case kUnknown_SkColorType
:
19 case kAlpha_8_SkColorType
:
21 case kIndex_8_SkColorType
:
23 case kRGB_565_SkColorType
:
25 case kARGB_4444_SkColorType
:
27 case kN32_SkColorType
:
37 DriBuffer::DriBuffer(const scoped_refptr
<DrmDevice
>& drm
)
38 : drm_(drm
), handle_(0), framebuffer_(0) {
41 DriBuffer::~DriBuffer() {
46 drm_
->RemoveFramebuffer(framebuffer_
);
49 void* pixels
= const_cast<void*>(surface_
->peekPixels(&info
, NULL
));
53 drm_
->DestroyDumbBuffer(info
, handle_
, stride_
, pixels
);
56 bool DriBuffer::Initialize(const SkImageInfo
& info
,
57 bool should_register_framebuffer
) {
59 if (!drm_
->CreateDumbBuffer(info
, &handle_
, &stride_
, &pixels
)) {
60 VLOG(2) << "Cannot create drm dumb buffer";
64 if (should_register_framebuffer
&&
65 !drm_
->AddFramebuffer(
66 info
.width(), info
.height(), GetColorDepth(info
.colorType()),
67 info
.bytesPerPixel() << 3, stride_
, handle_
, &framebuffer_
)) {
68 VLOG(2) << "Failed to register framebuffer: " << strerror(errno
);
72 surface_
= skia::AdoptRef(SkSurface::NewRasterDirect(info
, pixels
, stride_
));
74 VLOG(2) << "Cannot install Skia pixels for drm buffer";
81 SkCanvas
* DriBuffer::GetCanvas() const {
82 return surface_
->getCanvas();
85 uint32_t DriBuffer::GetFramebufferId() const {
89 uint32_t DriBuffer::GetHandle() const {
93 gfx::Size
DriBuffer::GetSize() const {
94 return gfx::Size(surface_
->width(), surface_
->height());
97 DriBufferGenerator::DriBufferGenerator() {
100 DriBufferGenerator::~DriBufferGenerator() {}
102 scoped_refptr
<ScanoutBuffer
> DriBufferGenerator::Create(
103 const scoped_refptr
<DrmDevice
>& drm
,
104 const gfx::Size
& size
) {
105 scoped_refptr
<DriBuffer
> buffer(new DriBuffer(drm
));
106 SkImageInfo info
= SkImageInfo::MakeN32Premul(size
.width(), size
.height());
107 if (!buffer
->Initialize(info
, true /* should_register_framebuffer */))