• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #include "ui/ozone/platform/dri/dri_buffer.h"
6 
7 #include "base/logging.h"
8 #include "ui/ozone/platform/dri/dri_wrapper.h"
9 
10 namespace ui {
11 
12 namespace {
13 
14 // Modesetting cannot happen from a buffer with transparencies. Return the size
15 // of a pixel without alpha.
GetColorDepth(SkColorType type)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;
32   }
33 }
34 
35 }  // namespace
36 
DriBuffer(DriWrapper * dri)37 DriBuffer::DriBuffer(DriWrapper* dri)
38     : dri_(dri), handle_(0), framebuffer_(0) {}
39 
~DriBuffer()40 DriBuffer::~DriBuffer() {
41   if (!surface_)
42     return;
43 
44   if (framebuffer_)
45     dri_->RemoveFramebuffer(framebuffer_);
46 
47   SkImageInfo info;
48   void* pixels = const_cast<void*>(surface_->peekPixels(&info, NULL));
49   if (!pixels)
50     return;
51 
52   dri_->DestroyDumbBuffer(info, handle_, stride_, pixels);
53 }
54 
Initialize(const SkImageInfo & info)55 bool DriBuffer::Initialize(const SkImageInfo& info) {
56   void* pixels = NULL;
57   if (!dri_->CreateDumbBuffer(info, &handle_, &stride_, &pixels)) {
58     VLOG(2) << "Cannot create drm dumb buffer";
59     return false;
60   }
61 
62   if (!dri_->AddFramebuffer(info.width(),
63                             info.height(),
64                             GetColorDepth(info.colorType()),
65                             info.bytesPerPixel() << 3,
66                             stride_,
67                             handle_,
68                             &framebuffer_)) {
69     VLOG(2) << "Failed to register framebuffer: " << strerror(errno);
70     return false;
71   }
72 
73   surface_ = skia::AdoptRef(SkSurface::NewRasterDirect(info, pixels, stride_));
74   if (!surface_) {
75     VLOG(2) << "Cannot install Skia pixels for drm buffer";
76     return false;
77   }
78 
79   return true;
80 }
81 
GetCanvas() const82 SkCanvas* DriBuffer::GetCanvas() const {
83   return surface_->getCanvas();
84 }
85 
GetFramebufferId() const86 uint32_t DriBuffer::GetFramebufferId() const {
87   return framebuffer_;
88 }
89 
GetHandle() const90 uint32_t DriBuffer::GetHandle() const {
91   return handle_;
92 }
93 
GetSize() const94 gfx::Size DriBuffer::GetSize() const {
95   return gfx::Size(surface_->width(), surface_->height());
96 }
97 
DriBufferGenerator(DriWrapper * dri)98 DriBufferGenerator::DriBufferGenerator(DriWrapper* dri) : dri_(dri) {}
99 
~DriBufferGenerator()100 DriBufferGenerator::~DriBufferGenerator() {}
101 
Create(const gfx::Size & size)102 scoped_refptr<ScanoutBuffer> DriBufferGenerator::Create(const gfx::Size& size) {
103   scoped_refptr<DriBuffer> buffer(new DriBuffer(dri_));
104   SkImageInfo info = SkImageInfo::MakeN32Premul(size.width(), size.height());
105   if (!buffer->Initialize(info))
106     return NULL;
107 
108   return buffer;
109 }
110 
111 }  // namespace ui
112