Fix search results being clipped in app list.
[chromium-blink-merge.git] / ui / ozone / platform / drm / gpu / gbm_buffer.cc
blob4a4380c8ecfcbc9c5afa21ecb60f148692359edc
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/gbm_buffer.h"
7 #include <drm.h>
8 #include <fcntl.h>
9 #include <gbm.h>
10 #include <xf86drm.h>
12 #include "base/logging.h"
13 #include "base/trace_event/trace_event.h"
14 #include "ui/ozone/platform/drm/gpu/gbm_device.h"
16 namespace ui {
18 namespace {
20 int GetGbmFormatFromBufferFormat(SurfaceFactoryOzone::BufferFormat fmt) {
21 switch (fmt) {
22 case SurfaceFactoryOzone::RGBA_8888:
23 return GBM_BO_FORMAT_ARGB8888;
24 case SurfaceFactoryOzone::RGBX_8888:
25 return GBM_BO_FORMAT_XRGB8888;
26 default:
27 NOTREACHED();
28 return 0;
32 } // namespace
34 GbmBuffer::GbmBuffer(const scoped_refptr<GbmDevice>& gbm,
35 gbm_bo* bo,
36 bool scanout)
37 : GbmBufferBase(gbm, bo, scanout) {
40 GbmBuffer::~GbmBuffer() {
41 if (bo())
42 gbm_bo_destroy(bo());
45 // static
46 scoped_refptr<GbmBuffer> GbmBuffer::CreateBuffer(
47 const scoped_refptr<GbmDevice>& gbm,
48 SurfaceFactoryOzone::BufferFormat format,
49 const gfx::Size& size,
50 bool scanout) {
51 TRACE_EVENT2("drm", "GbmBuffer::CreateBuffer", "device",
52 gbm->device_path().value(), "size", size.ToString());
53 unsigned flags = GBM_BO_USE_RENDERING;
54 if (scanout)
55 flags |= GBM_BO_USE_SCANOUT;
56 gbm_bo* bo = gbm_bo_create(gbm->device(), size.width(), size.height(),
57 GetGbmFormatFromBufferFormat(format), flags);
58 if (!bo)
59 return NULL;
61 scoped_refptr<GbmBuffer> buffer(new GbmBuffer(gbm, bo, scanout));
62 if (scanout && !buffer->GetFramebufferId())
63 return NULL;
65 return buffer;
68 GbmPixmap::GbmPixmap(const scoped_refptr<GbmBuffer>& buffer)
69 : buffer_(buffer), dma_buf_(-1) {
72 bool GbmPixmap::Initialize() {
73 // We want to use the GBM API because it's going to call into libdrm
74 // which might do some optimizations on buffer allocation,
75 // especially when sharing buffers via DMABUF.
76 dma_buf_ = gbm_bo_get_fd(buffer_->bo());
77 if (dma_buf_ < 0) {
78 PLOG(ERROR) << "Failed to export buffer to dma_buf";
79 return false;
81 return true;
84 GbmPixmap::~GbmPixmap() {
85 if (dma_buf_ > 0)
86 close(dma_buf_);
89 void* GbmPixmap::GetEGLClientBuffer() {
90 return nullptr;
93 int GbmPixmap::GetDmaBufFd() {
94 return dma_buf_;
97 int GbmPixmap::GetDmaBufPitch() {
98 return gbm_bo_get_stride(buffer_->bo());
101 } // namespace ui