blob: f6a2e41f2546d9e6538a5fe4a287ca918a2fa70c [file] [log] [blame]
Saurabh Shah7d476ed2016-06-27 16:40:58 -07001/*
2* Copyright (c) 2017, The Linux Foundation. All rights reserved.
3*
4* Redistribution and use in source and binary forms, with or without
5* modification, are permitted provided that the following conditions are
6* met:
7* * Redistributions of source code must retain the above copyright
8* notice, this list of conditions and the following disclaimer.
9* * Redistributions in binary form must reproduce the above
10* copyright notice, this list of conditions and the following
11* disclaimer in the documentation and/or other materials provided
12* with the distribution.
13* * Neither the name of The Linux Foundation nor the names of its
14* contributors may be used to endorse or promote products derived
15* from this software without specific prior written permission.
16*
17* THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
18* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
20* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
21* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
24* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
26* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*/
29
30#include <errno.h>
31#include <fcntl.h>
32#include <sys/stat.h>
33#include <unistd.h>
34#include <xf86drm.h>
35#include <xf86drmMode.h>
36#include <drm/drm_fourcc.h>
37
38#include <algorithm>
39#include <iterator>
40
41#include "drm_master.h"
42
43#define __CLASS__ "DRMMaster"
44
45using std::mutex;
46using std::lock_guard;
47using std::begin;
48using std::copy;
49using std::end;
50using std::fill;
51
52namespace drm_utils {
53
54DRMLogger *DRMLogger::s_instance = nullptr;
55DRMMaster *DRMMaster::s_instance = nullptr;
56mutex DRMMaster::s_lock;
57
58int DRMMaster::GetInstance(DRMMaster **master) {
59 lock_guard<mutex> obj(s_lock);
60
61 if (!s_instance) {
62 s_instance = new DRMMaster();
63 if (s_instance->Init() < 0) {
64 delete s_instance;
65 s_instance = nullptr;
66 return -ENODEV;
67 }
68 }
69
70 *master = s_instance;
71 return 0;
72}
73
74int DRMMaster::Init() {
75 dev_fd_ = drmOpen("msm_drm", nullptr);
76 if (dev_fd_ < 0) {
77 DRM_LOGE("%s::%s: drmOpen failed with error %d", __CLASS__, __FUNCTION__, dev_fd_);
78 return -ENODEV;
79 }
80
81 return 0;
82}
83
84DRMMaster::~DRMMaster() {
85 drmClose(dev_fd_);
86 dev_fd_ = -1;
87}
88
89int DRMMaster::CreateFbId(const DRMBuffer &drm_buffer, uint32_t *gem_handle, uint32_t *fb_id) {
90 int ret = drmPrimeFDToHandle(dev_fd_, drm_buffer.fd, gem_handle);
91 if (ret) {
92 DRM_LOGE("%s::%s: drmPrimeFDToHandle failed with error %d", __CLASS__, __FUNCTION__, ret);
93 return ret;
94 }
95
96 uint32_t gem_handles[4] = {0};
97 uint32_t pitches[4] = {0};
98 uint32_t offsets[4] = {0};
99 uint64_t modifier[4] = {0};
100
101 fill(begin(gem_handles), begin(gem_handles) + drm_buffer.num_planes, *gem_handle);
102 copy(begin(drm_buffer.stride), end(drm_buffer.stride), begin(pitches));
103 copy(begin(drm_buffer.offset), end(drm_buffer.offset), begin(offsets));
104 fill(begin(modifier), begin(modifier) + drm_buffer.num_planes, drm_buffer.drm_format_modifier);
105
106 ret = drmModeAddFB3(dev_fd_, drm_buffer.width, drm_buffer.height, drm_buffer.drm_format,
107 gem_handles, pitches, offsets, modifier, fb_id, DRM_MODE_FB_MODIFIERS);
108 if (ret) {
109 DRM_LOGE("%s::%s: drmModeAddFB3 failed with error %d", __CLASS__, __FUNCTION__, ret);
110 struct drm_gem_close gem_close = {};
111 gem_close.handle = *gem_handle;
112 int ret1 = drmIoctl(dev_fd_, DRM_IOCTL_GEM_CLOSE, &gem_close);
113 if (ret1) {
114 DRM_LOGE("drmIoctl::DRM_IOCTL_GEM_CLOSE failed with error %d", ret1);
115 }
116 }
117
118 return ret;
119}
120
121int DRMMaster::RemoveFbId(uint32_t gem_handle, uint32_t fb_id) {
122 int ret = drmModeRmFB(dev_fd_, fb_id);
123 if (ret) {
124 DRM_LOGE("%s::%s: drmModeRmFB failed with error %d", __CLASS__, __FUNCTION__, ret);
125 }
126
127 struct drm_gem_close gem_close = {};
128 gem_close.handle = gem_handle;
129 ret = drmIoctl(dev_fd_, DRM_IOCTL_GEM_CLOSE, &gem_close);
130 if (ret) {
131 DRM_LOGE("drmIoctl::DRM_IOCTL_GEM_CLOSE failed with error %d", ret);
132 }
133
134 return ret;
135}
136
137} // namespace drm_utils