Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / ui / ozone / platform / drm / gpu / drm_buffer.cc
blobe36c696f50e7088381e075daaa85e988ccf75c5b
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"
12 namespace ui {
14 namespace {
16 // Modesetting cannot happen from a buffer with transparencies. Return the size
17 // of a pixel without alpha.
18 uint8_t GetColorDepth(SkColorType type) {
19 switch (type) {
20 case kUnknown_SkColorType:
21 case kAlpha_8_SkColorType:
22 return 0;
23 case kIndex_8_SkColorType:
24 return 8;
25 case kRGB_565_SkColorType:
26 return 16;
27 case kARGB_4444_SkColorType:
28 return 12;
29 case kN32_SkColorType:
30 return 24;
31 default:
32 NOTREACHED();
33 return 0;
37 // We always ignore Alpha.
38 uint32_t GetFourCCCodeForSkColorType(SkColorType type) {
39 switch (type) {
40 case kUnknown_SkColorType:
41 case kAlpha_8_SkColorType:
42 return 0;
43 case kIndex_8_SkColorType:
44 return DRM_FORMAT_C8;
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;
51 default:
52 NOTREACHED();
53 return 0;
57 } // namespace
59 DrmBuffer::DrmBuffer(const scoped_refptr<DrmDevice>& drm) : drm_(drm) {
62 DrmBuffer::~DrmBuffer() {
63 surface_.clear();
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();
80 return false;
83 mmap_size_ = info.getSafeSize(stride_);
84 if (!drm_->MapDumbBuffer(handle_, mmap_size_, &mmap_base_)) {
85 PLOG(ERROR) << "DrmBuffer: MapDumbBuffer: handle " << handle_;
86 return false;
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_;
94 return false;
97 fb_pixel_format_ = GetFourCCCodeForSkColorType(info.colorType());
100 surface_ =
101 skia::AdoptRef(SkSurface::NewRasterDirect(info, mmap_base_, stride_));
102 if (!surface_) {
103 LOG(ERROR) << "DrmBuffer: Failed to create SkSurface: handle " << handle_;
104 return false;
107 return true;
110 SkCanvas* DrmBuffer::GetCanvas() const {
111 return surface_->getCanvas();
114 uint32_t DrmBuffer::GetFramebufferId() const {
115 return framebuffer_;
118 uint32_t DrmBuffer::GetFramebufferPixelFormat() const {
119 return fb_pixel_format_;
122 uint32_t DrmBuffer::GetHandle() const {
123 return handle_;
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 */))
143 return NULL;
145 return buffer;
148 } // namespace ui