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_buffer.h"
7 #include "base/logging.h"
8 #include "ui/ozone/platform/drm/gpu/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 DrmBuffer::DrmBuffer(const scoped_refptr
<DrmDevice
>& drm
) : drm_(drm
) {
40 DrmBuffer::~DrmBuffer() {
43 if (framebuffer_
&& !drm_
->RemoveFramebuffer(framebuffer_
))
44 PLOG(ERROR
) << "DrmBuffer: RemoveFramebuffer: fb " << framebuffer_
;
46 if (mmap_base_
&& !drm_
->UnmapDumbBuffer(mmap_base_
, mmap_size_
))
47 PLOG(ERROR
) << "DrmBuffer: UnmapDumbBuffer: handle " << handle_
;
49 if (handle_
&& !drm_
->DestroyDumbBuffer(handle_
))
50 PLOG(ERROR
) << "DrmBuffer: DestroyDumbBuffer: handle " << handle_
;
53 bool DrmBuffer::Initialize(const SkImageInfo
& info
,
54 bool should_register_framebuffer
) {
55 if (!drm_
->CreateDumbBuffer(info
, &handle_
, &stride_
)) {
56 PLOG(ERROR
) << "DrmBuffer: CreateDumbBuffer: width " << info
.width()
57 << " height " << info
.height();
61 mmap_size_
= info
.getSafeSize(stride_
);
62 if (!drm_
->MapDumbBuffer(handle_
, mmap_size_
, &mmap_base_
)) {
63 PLOG(ERROR
) << "DrmBuffer: MapDumbBuffer: handle " << handle_
;
67 if (should_register_framebuffer
&&
68 !drm_
->AddFramebuffer(
69 info
.width(), info
.height(), GetColorDepth(info
.colorType()),
70 info
.bytesPerPixel() << 3, stride_
, handle_
, &framebuffer_
)) {
71 PLOG(ERROR
) << "DrmBuffer: AddFramebuffer: handle " << handle_
;
76 skia::AdoptRef(SkSurface::NewRasterDirect(info
, mmap_base_
, stride_
));
78 LOG(ERROR
) << "DrmBuffer: Failed to create SkSurface: handle " << handle_
;
85 SkCanvas
* DrmBuffer::GetCanvas() const {
86 return surface_
->getCanvas();
89 uint32_t DrmBuffer::GetFramebufferId() const {
93 uint32_t DrmBuffer::GetHandle() const {
97 gfx::Size
DrmBuffer::GetSize() const {
98 return gfx::Size(surface_
->width(), surface_
->height());
101 DrmBufferGenerator::DrmBufferGenerator() {
104 DrmBufferGenerator::~DrmBufferGenerator() {
107 scoped_refptr
<ScanoutBuffer
> DrmBufferGenerator::Create(
108 const scoped_refptr
<DrmDevice
>& drm
,
109 const gfx::Size
& size
) {
110 scoped_refptr
<DrmBuffer
> buffer(new DrmBuffer(drm
));
111 SkImageInfo info
= SkImageInfo::MakeN32Premul(size
.width(), size
.height());
112 if (!buffer
->Initialize(info
, true /* should_register_framebuffer */))