Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / ui / ozone / platform / drm / gpu / drm_buffer.cc
blob0ab1f61aa3fd53f80b621da6566fd504fce94e9e
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"
10 namespace ui {
12 namespace {
14 // Modesetting cannot happen from a buffer with transparencies. Return the size
15 // of a pixel without alpha.
16 uint8_t GetColorDepth(SkColorType type) {
17 switch (type) {
18 case kUnknown_SkColorType:
19 case kAlpha_8_SkColorType:
20 return 0;
21 case kIndex_8_SkColorType:
22 return 8;
23 case kRGB_565_SkColorType:
24 return 16;
25 case kARGB_4444_SkColorType:
26 return 12;
27 case kN32_SkColorType:
28 return 24;
29 default:
30 NOTREACHED();
31 return 0;
35 } // namespace
37 DrmBuffer::DrmBuffer(const scoped_refptr<DrmDevice>& drm) : drm_(drm) {
40 DrmBuffer::~DrmBuffer() {
41 surface_.clear();
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();
58 return false;
61 mmap_size_ = info.getSafeSize(stride_);
62 if (!drm_->MapDumbBuffer(handle_, mmap_size_, &mmap_base_)) {
63 PLOG(ERROR) << "DrmBuffer: MapDumbBuffer: handle " << handle_;
64 return false;
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_;
72 return false;
75 surface_ =
76 skia::AdoptRef(SkSurface::NewRasterDirect(info, mmap_base_, stride_));
77 if (!surface_) {
78 LOG(ERROR) << "DrmBuffer: Failed to create SkSurface: handle " << handle_;
79 return false;
82 return true;
85 SkCanvas* DrmBuffer::GetCanvas() const {
86 return surface_->getCanvas();
89 uint32_t DrmBuffer::GetFramebufferId() const {
90 return framebuffer_;
93 uint32_t DrmBuffer::GetHandle() const {
94 return handle_;
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 */))
113 return NULL;
115 return buffer;
118 } // namespace ui