Saurabh Shah | 7d476ed | 2016-06-27 16:40:58 -0700 | [diff] [blame] | 1 | /* |
| 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> |
Saurabh Shah | fad1afd | 2017-01-31 12:09:59 -0800 | [diff] [blame] | 36 | // Intentionally included after xf86 headers so that they in-turn include libdrm version of drm.h |
| 37 | // that doesn't use keyword "virtual" for a variable name. Not doing so leads to the kernel version |
| 38 | // of drm.h being included causing compilation to fail |
| 39 | #include <drm/msm_drm.h> |
Saurabh Shah | 7d476ed | 2016-06-27 16:40:58 -0700 | [diff] [blame] | 40 | #include <algorithm> |
| 41 | #include <iterator> |
| 42 | |
| 43 | #include "drm_master.h" |
| 44 | |
| 45 | #define __CLASS__ "DRMMaster" |
| 46 | |
| 47 | using std::mutex; |
| 48 | using std::lock_guard; |
| 49 | using std::begin; |
| 50 | using std::copy; |
| 51 | using std::end; |
| 52 | using std::fill; |
| 53 | |
| 54 | namespace drm_utils { |
| 55 | |
| 56 | DRMLogger *DRMLogger::s_instance = nullptr; |
| 57 | DRMMaster *DRMMaster::s_instance = nullptr; |
| 58 | mutex DRMMaster::s_lock; |
| 59 | |
| 60 | int DRMMaster::GetInstance(DRMMaster **master) { |
| 61 | lock_guard<mutex> obj(s_lock); |
| 62 | |
| 63 | if (!s_instance) { |
| 64 | s_instance = new DRMMaster(); |
| 65 | if (s_instance->Init() < 0) { |
| 66 | delete s_instance; |
| 67 | s_instance = nullptr; |
| 68 | return -ENODEV; |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | *master = s_instance; |
| 73 | return 0; |
| 74 | } |
| 75 | |
Saurabh Shah | a57cf16 | 2017-02-28 17:00:11 -0800 | [diff] [blame] | 76 | void DRMMaster::DestroyInstance() { |
| 77 | lock_guard<mutex> obj(s_lock); |
| 78 | delete s_instance; |
| 79 | s_instance = nullptr; |
| 80 | } |
| 81 | |
Saurabh Shah | 7d476ed | 2016-06-27 16:40:58 -0700 | [diff] [blame] | 82 | int DRMMaster::Init() { |
| 83 | dev_fd_ = drmOpen("msm_drm", nullptr); |
| 84 | if (dev_fd_ < 0) { |
Saurabh Shah | 66c941b | 2016-07-06 17:34:05 -0700 | [diff] [blame] | 85 | DRM_LOGE("drmOpen failed with error %d", dev_fd_); |
Saurabh Shah | 7d476ed | 2016-06-27 16:40:58 -0700 | [diff] [blame] | 86 | return -ENODEV; |
| 87 | } |
| 88 | |
| 89 | return 0; |
| 90 | } |
| 91 | |
| 92 | DRMMaster::~DRMMaster() { |
| 93 | drmClose(dev_fd_); |
| 94 | dev_fd_ = -1; |
| 95 | } |
| 96 | |
Saurabh Shah | f84c412 | 2017-04-07 10:34:40 -0700 | [diff] [blame] | 97 | int DRMMaster::CreateFbId(const DRMBuffer &drm_buffer, uint32_t *fb_id) { |
| 98 | uint32_t gem_handle = 0; |
| 99 | int ret = drmPrimeFDToHandle(dev_fd_, drm_buffer.fd, &gem_handle); |
Saurabh Shah | 7d476ed | 2016-06-27 16:40:58 -0700 | [diff] [blame] | 100 | if (ret) { |
Saurabh Shah | 66c941b | 2016-07-06 17:34:05 -0700 | [diff] [blame] | 101 | DRM_LOGE("drmPrimeFDToHandle failed with error %d", ret); |
Saurabh Shah | 7d476ed | 2016-06-27 16:40:58 -0700 | [diff] [blame] | 102 | return ret; |
| 103 | } |
| 104 | |
Saurabh Shah | b1bdf6e | 2017-02-16 10:57:19 -0800 | [diff] [blame] | 105 | struct drm_mode_fb_cmd2 cmd2 {}; |
| 106 | cmd2.width = drm_buffer.width; |
| 107 | cmd2.height = drm_buffer.height; |
| 108 | cmd2.pixel_format = drm_buffer.drm_format; |
| 109 | cmd2.flags = DRM_MODE_FB_MODIFIERS; |
Saurabh Shah | f84c412 | 2017-04-07 10:34:40 -0700 | [diff] [blame] | 110 | fill(begin(cmd2.handles), begin(cmd2.handles) + drm_buffer.num_planes, gem_handle); |
Saurabh Shah | b1bdf6e | 2017-02-16 10:57:19 -0800 | [diff] [blame] | 111 | copy(begin(drm_buffer.stride), end(drm_buffer.stride), begin(cmd2.pitches)); |
| 112 | copy(begin(drm_buffer.offset), end(drm_buffer.offset), begin(cmd2.offsets)); |
| 113 | fill(begin(cmd2.modifier), begin(cmd2.modifier) + drm_buffer.num_planes, |
| 114 | drm_buffer.drm_format_modifier); |
Saurabh Shah | 7d476ed | 2016-06-27 16:40:58 -0700 | [diff] [blame] | 115 | |
Saurabh Shah | b1bdf6e | 2017-02-16 10:57:19 -0800 | [diff] [blame] | 116 | if ((ret = drmIoctl(dev_fd_, DRM_IOCTL_MODE_ADDFB2, &cmd2))) { |
| 117 | DRM_LOGE("DRM_IOCTL_MODE_ADDFB2 failed with error %d", ret); |
Saurabh Shah | f84c412 | 2017-04-07 10:34:40 -0700 | [diff] [blame] | 118 | } else { |
| 119 | *fb_id = cmd2.fb_id; |
Saurabh Shah | 7d476ed | 2016-06-27 16:40:58 -0700 | [diff] [blame] | 120 | } |
| 121 | |
Saurabh Shah | 7d476ed | 2016-06-27 16:40:58 -0700 | [diff] [blame] | 122 | struct drm_gem_close gem_close = {}; |
| 123 | gem_close.handle = gem_handle; |
Saurabh Shah | f84c412 | 2017-04-07 10:34:40 -0700 | [diff] [blame] | 124 | int ret1 = drmIoctl(dev_fd_, DRM_IOCTL_GEM_CLOSE, &gem_close); |
| 125 | if (ret1) { |
| 126 | DRM_LOGE("drmIoctl::DRM_IOCTL_GEM_CLOSE failed with error %d", ret1); |
| 127 | return ret1; |
Saurabh Shah | 7d476ed | 2016-06-27 16:40:58 -0700 | [diff] [blame] | 128 | } |
| 129 | |
Saurabh Shah | f84c412 | 2017-04-07 10:34:40 -0700 | [diff] [blame] | 130 | return ret; |
| 131 | } |
| 132 | |
| 133 | int DRMMaster::RemoveFbId(uint32_t fb_id) { |
| 134 | int ret = 0; |
Saurabh Shah | fad1afd | 2017-01-31 12:09:59 -0800 | [diff] [blame] | 135 | #ifdef DRM_IOCTL_MSM_RMFB2 |
| 136 | ret = drmIoctl(dev_fd_, DRM_IOCTL_MSM_RMFB2, &fb_id); |
| 137 | if (ret) { |
| 138 | DRM_LOGE("drmIoctl::DRM_IOCTL_MSM_RMFB2 failed for fb_id %d with error %d", fb_id, errno); |
| 139 | } |
| 140 | #else |
| 141 | ret = drmModeRmFB(dev_fd_, fb_id); |
| 142 | if (ret) { |
| 143 | DRM_LOGE("drmModeRmFB failed for fb_id %d with error %d", fb_id, ret); |
| 144 | } |
| 145 | #endif |
Saurabh Shah | 7d476ed | 2016-06-27 16:40:58 -0700 | [diff] [blame] | 146 | return ret; |
| 147 | } |
| 148 | |
| 149 | } // namespace drm_utils |