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 <drm_fourcc.h>
9 #include "base/logging.h"
10 #include "ui/ozone/platform/drm/gpu/drm_device.h"
16 // Modesetting cannot happen from a buffer with transparencies. Return the size
17 // of a pixel without alpha.
18 uint8_t GetColorDepth(SkColorType type
) {
20 case kUnknown_SkColorType
:
21 case kAlpha_8_SkColorType
:
23 case kIndex_8_SkColorType
:
25 case kRGB_565_SkColorType
:
27 case kARGB_4444_SkColorType
:
29 case kN32_SkColorType
:
37 // We always ignore Alpha.
38 uint32_t GetFourCCCodeForSkColorType(SkColorType type
) {
40 case kUnknown_SkColorType
:
41 case kAlpha_8_SkColorType
:
43 case kIndex_8_SkColorType
:
45 case kRGB_565_SkColorType
:
46 return DRM_FORMAT_RGB565
;
47 case kARGB_4444_SkColorType
:
48 return DRM_FORMAT_XRGB4444
;
49 case kN32_SkColorType
:
50 return DRM_FORMAT_XRGB8888
;
59 DrmBuffer::DrmBuffer(const scoped_refptr
<DrmDevice
>& drm
) : drm_(drm
) {
62 DrmBuffer::~DrmBuffer() {
65 if (framebuffer_
&& !drm_
->RemoveFramebuffer(framebuffer_
))
66 PLOG(ERROR
) << "DrmBuffer: RemoveFramebuffer: fb " << framebuffer_
;
68 if (mmap_base_
&& !drm_
->UnmapDumbBuffer(mmap_base_
, mmap_size_
))
69 PLOG(ERROR
) << "DrmBuffer: UnmapDumbBuffer: handle " << handle_
;
71 if (handle_
&& !drm_
->DestroyDumbBuffer(handle_
))
72 PLOG(ERROR
) << "DrmBuffer: DestroyDumbBuffer: handle " << handle_
;
75 bool DrmBuffer::Initialize(const SkImageInfo
& info
,
76 bool should_register_framebuffer
) {
77 if (!drm_
->CreateDumbBuffer(info
, &handle_
, &stride_
)) {
78 PLOG(ERROR
) << "DrmBuffer: CreateDumbBuffer: width " << info
.width()
79 << " height " << info
.height();
83 mmap_size_
= info
.getSafeSize(stride_
);
84 if (!drm_
->MapDumbBuffer(handle_
, mmap_size_
, &mmap_base_
)) {
85 PLOG(ERROR
) << "DrmBuffer: MapDumbBuffer: handle " << handle_
;
89 if (should_register_framebuffer
) {
90 if (!drm_
->AddFramebuffer(
91 info
.width(), info
.height(), GetColorDepth(info
.colorType()),
92 info
.bytesPerPixel() << 3, stride_
, handle_
, &framebuffer_
)) {
93 PLOG(ERROR
) << "DrmBuffer: AddFramebuffer: handle " << handle_
;
97 fb_pixel_format_
= GetFourCCCodeForSkColorType(info
.colorType());
101 skia::AdoptRef(SkSurface::NewRasterDirect(info
, mmap_base_
, stride_
));
103 LOG(ERROR
) << "DrmBuffer: Failed to create SkSurface: handle " << handle_
;
110 SkCanvas
* DrmBuffer::GetCanvas() const {
111 return surface_
->getCanvas();
114 uint32_t DrmBuffer::GetFramebufferId() const {
118 uint32_t DrmBuffer::GetFramebufferPixelFormat() const {
119 return fb_pixel_format_
;
122 uint32_t DrmBuffer::GetHandle() const {
126 gfx::Size
DrmBuffer::GetSize() const {
127 return gfx::Size(surface_
->width(), surface_
->height());
130 DrmBufferGenerator::DrmBufferGenerator() {
133 DrmBufferGenerator::~DrmBufferGenerator() {
136 scoped_refptr
<ScanoutBuffer
> DrmBufferGenerator::Create(
137 const scoped_refptr
<DrmDevice
>& drm
,
138 gfx::BufferFormat format
,
139 const gfx::Size
& size
) {
140 scoped_refptr
<DrmBuffer
> buffer(new DrmBuffer(drm
));
141 SkImageInfo info
= SkImageInfo::MakeN32Premul(size
.width(), size
.height());
142 if (!buffer
->Initialize(info
, true /* should_register_framebuffer */))