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 | |
Saurabh Shah | 66c941b | 2016-07-06 17:34:05 -0700 | [diff] [blame] | 30 | #include <drm/drm_fourcc.h> |
Saurabh Shah | 7d476ed | 2016-06-27 16:40:58 -0700 | [diff] [blame] | 31 | #include <errno.h> |
| 32 | #include <fcntl.h> |
| 33 | #include <sys/stat.h> |
| 34 | #include <unistd.h> |
| 35 | #include <xf86drm.h> |
| 36 | #include <xf86drmMode.h> |
Saurabh Shah | 7d476ed | 2016-06-27 16:40:58 -0700 | [diff] [blame] | 37 | |
| 38 | #include <algorithm> |
| 39 | #include <iterator> |
| 40 | |
| 41 | #include "drm_master.h" |
| 42 | |
| 43 | #define __CLASS__ "DRMMaster" |
| 44 | |
| 45 | using std::mutex; |
| 46 | using std::lock_guard; |
| 47 | using std::begin; |
| 48 | using std::copy; |
| 49 | using std::end; |
| 50 | using std::fill; |
| 51 | |
| 52 | namespace drm_utils { |
| 53 | |
| 54 | DRMLogger *DRMLogger::s_instance = nullptr; |
| 55 | DRMMaster *DRMMaster::s_instance = nullptr; |
| 56 | mutex DRMMaster::s_lock; |
| 57 | |
| 58 | int 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 | |
| 74 | int DRMMaster::Init() { |
| 75 | dev_fd_ = drmOpen("msm_drm", nullptr); |
| 76 | if (dev_fd_ < 0) { |
Saurabh Shah | 66c941b | 2016-07-06 17:34:05 -0700 | [diff] [blame] | 77 | DRM_LOGE("drmOpen failed with error %d", dev_fd_); |
Saurabh Shah | 7d476ed | 2016-06-27 16:40:58 -0700 | [diff] [blame] | 78 | return -ENODEV; |
| 79 | } |
| 80 | |
| 81 | return 0; |
| 82 | } |
| 83 | |
| 84 | DRMMaster::~DRMMaster() { |
| 85 | drmClose(dev_fd_); |
| 86 | dev_fd_ = -1; |
| 87 | } |
| 88 | |
| 89 | int 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) { |
Saurabh Shah | 66c941b | 2016-07-06 17:34:05 -0700 | [diff] [blame] | 92 | DRM_LOGE("drmPrimeFDToHandle failed with error %d", ret); |
Saurabh Shah | 7d476ed | 2016-06-27 16:40:58 -0700 | [diff] [blame] | 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) { |
Saurabh Shah | 66c941b | 2016-07-06 17:34:05 -0700 | [diff] [blame] | 109 | DRM_LOGE("drmModeAddFB3 failed with error %d", ret); |
Saurabh Shah | 7d476ed | 2016-06-27 16:40:58 -0700 | [diff] [blame] | 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 | |
| 121 | int DRMMaster::RemoveFbId(uint32_t gem_handle, uint32_t fb_id) { |
| 122 | int ret = drmModeRmFB(dev_fd_, fb_id); |
| 123 | if (ret) { |
Saurabh Shah | 66c941b | 2016-07-06 17:34:05 -0700 | [diff] [blame] | 124 | DRM_LOGE("drmModeRmFB failed with error %d", ret); |
Saurabh Shah | 7d476ed | 2016-06-27 16:40:58 -0700 | [diff] [blame] | 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 |