ozone: evdev: Sync caps lock LED state to evdev
[chromium-blink-merge.git] / ui / ozone / platform / dri / dri_buffer.cc
blobbd2c2c26c5df060dda43c1d2fe461afdbfdfeb4b
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"
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 DriBuffer::DriBuffer(const scoped_refptr<DrmDevice>& drm)
38 : drm_(drm), handle_(0), framebuffer_(0) {
41 DriBuffer::~DriBuffer() {
42 if (!surface_)
43 return;
45 if (framebuffer_)
46 drm_->RemoveFramebuffer(framebuffer_);
48 SkImageInfo info;
49 void* pixels = const_cast<void*>(surface_->peekPixels(&info, NULL));
50 if (!pixels)
51 return;
53 drm_->DestroyDumbBuffer(info, handle_, stride_, pixels);
56 bool DriBuffer::Initialize(const SkImageInfo& info,
57 bool should_register_framebuffer) {
58 void* pixels = NULL;
59 if (!drm_->CreateDumbBuffer(info, &handle_, &stride_, &pixels)) {
60 VLOG(2) << "Cannot create drm dumb buffer";
61 return false;
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);
69 return false;
72 surface_ = skia::AdoptRef(SkSurface::NewRasterDirect(info, pixels, stride_));
73 if (!surface_) {
74 VLOG(2) << "Cannot install Skia pixels for drm buffer";
75 return false;
78 return true;
81 SkCanvas* DriBuffer::GetCanvas() const {
82 return surface_->getCanvas();
85 uint32_t DriBuffer::GetFramebufferId() const {
86 return framebuffer_;
89 uint32_t DriBuffer::GetHandle() const {
90 return handle_;
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 */))
108 return NULL;
110 return buffer;
113 } // namespace ui