| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ui/ozone/platform/dri/dri_wrapper.h" | 5 #include "ui/ozone/platform/dri/dri_wrapper.h" |
| 6 | 6 |
| 7 #include <fcntl.h> | 7 #include <fcntl.h> |
| 8 #include <sys/mman.h> |
| 8 #include <unistd.h> | 9 #include <unistd.h> |
| 9 #include <xf86drm.h> | 10 #include <xf86drm.h> |
| 10 #include <xf86drmMode.h> | 11 #include <xf86drmMode.h> |
| 11 | 12 |
| 12 #include "base/logging.h" | 13 #include "base/logging.h" |
| 14 #include "third_party/skia/include/core/SkImageInfo.h" |
| 15 #include "ui/ozone/platform/dri/dri_util.h" |
| 13 | 16 |
| 14 namespace ui { | 17 namespace ui { |
| 18 |
| 15 namespace { | 19 namespace { |
| 20 |
| 16 uint32_t ToFixedPoint(double v) { | 21 uint32_t ToFixedPoint(double v) { |
| 17 // This returns a number in a 16-bit.16-bit fixed point. | 22 // This returns a number in a 16-bit.16-bit fixed point. |
| 18 return v * 65536.0; | 23 return v * 65536.0; |
| 19 } | 24 } |
| 25 |
| 26 bool DrmCreateDumbBuffer(int fd, |
| 27 const SkImageInfo& info, |
| 28 uint32_t* handle, |
| 29 uint32_t* stride) { |
| 30 struct drm_mode_create_dumb request; |
| 31 memset(&request, 0, sizeof(request)); |
| 32 request.width = info.width(); |
| 33 request.height = info.height(); |
| 34 request.bpp = info.bytesPerPixel() << 3; |
| 35 request.flags = 0; |
| 36 |
| 37 if (drmIoctl(fd, DRM_IOCTL_MODE_CREATE_DUMB, &request) < 0) { |
| 38 VLOG(2) << "Cannot create dumb buffer (" << errno << ") " |
| 39 << strerror(errno); |
| 40 return false; |
| 41 } |
| 42 |
| 43 // The driver may choose to align the last row as well. We don't care about |
| 44 // the last alignment bits since they aren't used for display purposes, so |
| 45 // just check that the expected size is <= to what the driver allocated. |
| 46 DCHECK_LE(info.getSafeSize(request.pitch), request.size); |
| 47 |
| 48 *handle = request.handle; |
| 49 *stride = request.pitch; |
| 50 return true; |
| 51 } |
| 52 |
| 53 void DrmDestroyDumbBuffer(int fd, uint32_t handle) { |
| 54 struct drm_mode_destroy_dumb destroy_request; |
| 55 memset(&destroy_request, 0, sizeof(destroy_request)); |
| 56 destroy_request.handle = handle; |
| 57 drmIoctl(fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_request); |
| 58 } |
| 59 |
| 20 } // namespace | 60 } // namespace |
| 21 | 61 |
| 22 DriWrapper::DriWrapper(const char* device_path) { | 62 DriWrapper::DriWrapper(const char* device_path) { |
| 23 fd_ = open(device_path, O_RDWR | O_CLOEXEC); | 63 fd_ = open(device_path, O_RDWR | O_CLOEXEC); |
| 24 } | 64 } |
| 25 | 65 |
| 26 DriWrapper::~DriWrapper() { | 66 DriWrapper::~DriWrapper() { |
| 27 if (fd_ >= 0) | 67 if (fd_ >= 0) |
| 28 close(fd_); | 68 close(fd_); |
| 29 } | 69 } |
| (...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 167 bool DriWrapper::MoveCursor(uint32_t crtc_id, int x, int y) { | 207 bool DriWrapper::MoveCursor(uint32_t crtc_id, int x, int y) { |
| 168 CHECK(fd_ >= 0); | 208 CHECK(fd_ >= 0); |
| 169 return !drmModeMoveCursor(fd_, crtc_id, x, y); | 209 return !drmModeMoveCursor(fd_, crtc_id, x, y); |
| 170 } | 210 } |
| 171 | 211 |
| 172 void DriWrapper::HandleEvent(drmEventContext& event) { | 212 void DriWrapper::HandleEvent(drmEventContext& event) { |
| 173 CHECK(fd_ >= 0); | 213 CHECK(fd_ >= 0); |
| 174 drmHandleEvent(fd_, &event); | 214 drmHandleEvent(fd_, &event); |
| 175 } | 215 } |
| 176 | 216 |
| 217 bool DriWrapper::CreateDumbBuffer(const SkImageInfo& info, |
| 218 uint32_t* handle, |
| 219 uint32_t* stride, |
| 220 void** pixels) { |
| 221 CHECK(fd_ >= 0); |
| 222 |
| 223 if (!DrmCreateDumbBuffer(fd_, info, handle, stride)) |
| 224 return false; |
| 225 |
| 226 if (!MapDumbBuffer(fd_, *handle, info.getSafeSize(*stride), pixels)) { |
| 227 DrmDestroyDumbBuffer(fd_, *handle); |
| 228 return false; |
| 229 } |
| 230 |
| 231 return true; |
| 232 } |
| 233 |
| 234 void DriWrapper::DestroyDumbBuffer(const SkImageInfo& info, |
| 235 uint32_t handle, |
| 236 uint32_t stride, |
| 237 void* pixels) { |
| 238 CHECK(fd_ >= 0); |
| 239 munmap(pixels, info.getSafeSize(stride)); |
| 240 DrmDestroyDumbBuffer(fd_, handle); |
| 241 } |
| 242 |
| 243 |
| 177 } // namespace ui | 244 } // namespace ui |
| OLD | NEW |