| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/gfx/ozone/impl/drm_skbitmap_ozone.h" | 5 #include "ui/gfx/ozone/impl/dri_skbitmap.h" |
| 6 | 6 |
| 7 #include <errno.h> | 7 #include <errno.h> |
| 8 #include <sys/mman.h> | 8 #include <sys/mman.h> |
| 9 #include <sys/types.h> | 9 #include <sys/types.h> |
| 10 #include <xf86drm.h> | 10 #include <xf86drm.h> |
| 11 | 11 |
| 12 #include "base/compiler_specific.h" | 12 #include "base/compiler_specific.h" |
| 13 #include "base/logging.h" | 13 #include "base/logging.h" |
| 14 #include "third_party/skia/include/core/SkPixelRef.h" | 14 #include "third_party/skia/include/core/SkPixelRef.h" |
| 15 | 15 |
| 16 namespace gfx { | 16 namespace gfx { |
| 17 | 17 |
| 18 namespace { | 18 namespace { |
| 19 | 19 |
| 20 void DestroyDumbBuffer(int fd, uint32_t handle) { | 20 void DestroyDumbBuffer(int fd, uint32_t handle) { |
| 21 struct drm_mode_destroy_dumb destroy_request; | 21 struct drm_mode_destroy_dumb destroy_request; |
| 22 destroy_request.handle = handle; | 22 destroy_request.handle = handle; |
| 23 drmIoctl(fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_request); | 23 drmIoctl(fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_request); |
| 24 } | 24 } |
| 25 | 25 |
| 26 // Special DRM implementation of a SkPixelRef. The DRM allocator will create a | 26 // Special DRM implementation of a SkPixelRef. The DRM allocator will create a |
| 27 // SkPixelRef for the backing pixels. It will then associate the SkPixelRef with | 27 // SkPixelRef for the backing pixels. It will then associate the SkPixelRef with |
| 28 // the SkBitmap. SkBitmap will access the allocated memory by locking the pixels | 28 // the SkBitmap. SkBitmap will access the allocated memory by locking the pixels |
| 29 // in the SkPixelRef. | 29 // in the SkPixelRef. |
| 30 // At the end of its life the SkPixelRef is responsible for deallocating the | 30 // At the end of its life the SkPixelRef is responsible for deallocating the |
| 31 // pixel memory. | 31 // pixel memory. |
| 32 class DrmSkPixelRef : public SkPixelRef { | 32 class DriSkPixelRef : public SkPixelRef { |
| 33 public: | 33 public: |
| 34 DrmSkPixelRef(void* pixels, | 34 DriSkPixelRef(void* pixels, |
| 35 SkColorTable* color_table_, | 35 SkColorTable* color_table_, |
| 36 size_t size, | 36 size_t size, |
| 37 int fd, | 37 int fd, |
| 38 uint32_t handle); | 38 uint32_t handle); |
| 39 virtual ~DrmSkPixelRef(); | 39 virtual ~DriSkPixelRef(); |
| 40 | 40 |
| 41 virtual void* onLockPixels(SkColorTable** ct) OVERRIDE; | 41 virtual void* onLockPixels(SkColorTable** ct) OVERRIDE; |
| 42 virtual void onUnlockPixels() OVERRIDE; | 42 virtual void onUnlockPixels() OVERRIDE; |
| 43 | 43 |
| 44 SK_DECLARE_UNFLATTENABLE_OBJECT() | 44 SK_DECLARE_UNFLATTENABLE_OBJECT() |
| 45 private: | 45 private: |
| 46 // Raw pointer to the pixel memory. | 46 // Raw pointer to the pixel memory. |
| 47 void* pixels_; | 47 void* pixels_; |
| 48 | 48 |
| 49 // Optional color table associated with the pixel memory. | 49 // Optional color table associated with the pixel memory. |
| 50 SkColorTable* color_table_; | 50 SkColorTable* color_table_; |
| 51 | 51 |
| 52 // Size of the allocated memory. | 52 // Size of the allocated memory. |
| 53 size_t size_; | 53 size_t size_; |
| 54 | 54 |
| 55 // File descriptor to the graphics card used to allocate/deallocate the | 55 // File descriptor to the graphics card used to allocate/deallocate the |
| 56 // memory. | 56 // memory. |
| 57 int fd_; | 57 int fd_; |
| 58 | 58 |
| 59 // Handle for the allocated memory. | 59 // Handle for the allocated memory. |
| 60 uint32_t handle_; | 60 uint32_t handle_; |
| 61 | 61 |
| 62 DISALLOW_COPY_AND_ASSIGN(DrmSkPixelRef); | 62 DISALLOW_COPY_AND_ASSIGN(DriSkPixelRef); |
| 63 }; | 63 }; |
| 64 | 64 |
| 65 //////////////////////////////////////////////////////////////////////////////// | 65 //////////////////////////////////////////////////////////////////////////////// |
| 66 // DrmSkPixelRef implementation | 66 // DriSkPixelRef implementation |
| 67 | 67 |
| 68 DrmSkPixelRef::DrmSkPixelRef( | 68 DriSkPixelRef::DriSkPixelRef( |
| 69 void* pixels, | 69 void* pixels, |
| 70 SkColorTable* color_table, | 70 SkColorTable* color_table, |
| 71 size_t size, | 71 size_t size, |
| 72 int fd, | 72 int fd, |
| 73 uint32_t handle) | 73 uint32_t handle) |
| 74 : pixels_(pixels), | 74 : pixels_(pixels), |
| 75 color_table_(color_table), | 75 color_table_(color_table), |
| 76 size_(size), | 76 size_(size), |
| 77 fd_(fd), | 77 fd_(fd), |
| 78 handle_(handle) { | 78 handle_(handle) { |
| 79 } | 79 } |
| 80 | 80 |
| 81 DrmSkPixelRef::~DrmSkPixelRef() { | 81 DriSkPixelRef::~DriSkPixelRef() { |
| 82 munmap(pixels_, size_); | 82 munmap(pixels_, size_); |
| 83 DestroyDumbBuffer(fd_, handle_); | 83 DestroyDumbBuffer(fd_, handle_); |
| 84 } | 84 } |
| 85 | 85 |
| 86 void* DrmSkPixelRef::onLockPixels(SkColorTable** ct) { | 86 void* DriSkPixelRef::onLockPixels(SkColorTable** ct) { |
| 87 *ct = color_table_; | 87 *ct = color_table_; |
| 88 return pixels_; | 88 return pixels_; |
| 89 } | 89 } |
| 90 | 90 |
| 91 void DrmSkPixelRef::onUnlockPixels() { | 91 void DriSkPixelRef::onUnlockPixels() { |
| 92 } | 92 } |
| 93 | 93 |
| 94 } // namespace | 94 } // namespace |
| 95 | 95 |
| 96 // Allocates pixel memory for a SkBitmap using DRM dumb buffers. | 96 // Allocates pixel memory for a SkBitmap using DRM dumb buffers. |
| 97 class DrmAllocator : public SkBitmap::Allocator { | 97 class DriAllocator : public SkBitmap::Allocator { |
| 98 public: | 98 public: |
| 99 DrmAllocator(); | 99 DriAllocator(); |
| 100 | 100 |
| 101 virtual bool allocPixelRef(SkBitmap* bitmap, | 101 virtual bool allocPixelRef(SkBitmap* bitmap, |
| 102 SkColorTable* color_table) OVERRIDE; | 102 SkColorTable* color_table) OVERRIDE; |
| 103 | 103 |
| 104 private: | 104 private: |
| 105 bool AllocatePixels(DrmSkBitmapOzone* bitmap, SkColorTable* color_table); | 105 bool AllocatePixels(DriSkBitmap* bitmap, SkColorTable* color_table); |
| 106 | 106 |
| 107 DISALLOW_COPY_AND_ASSIGN(DrmAllocator); | 107 DISALLOW_COPY_AND_ASSIGN(DriAllocator); |
| 108 }; | 108 }; |
| 109 | 109 |
| 110 //////////////////////////////////////////////////////////////////////////////// | 110 //////////////////////////////////////////////////////////////////////////////// |
| 111 // DrmAllocator implementation | 111 // DriAllocator implementation |
| 112 | 112 |
| 113 DrmAllocator::DrmAllocator() { | 113 DriAllocator::DriAllocator() { |
| 114 } | 114 } |
| 115 | 115 |
| 116 bool DrmAllocator::allocPixelRef(SkBitmap* bitmap, | 116 bool DriAllocator::allocPixelRef(SkBitmap* bitmap, |
| 117 SkColorTable* color_table) { | 117 SkColorTable* color_table) { |
| 118 return AllocatePixels(static_cast<DrmSkBitmapOzone*>(bitmap), color_table); | 118 return AllocatePixels(static_cast<DriSkBitmap*>(bitmap), color_table); |
| 119 } | 119 } |
| 120 | 120 |
| 121 bool DrmAllocator::AllocatePixels(DrmSkBitmapOzone* bitmap, | 121 bool DriAllocator::AllocatePixels(DriSkBitmap* bitmap, |
| 122 SkColorTable* color_table) { | 122 SkColorTable* color_table) { |
| 123 struct drm_mode_create_dumb request; | 123 struct drm_mode_create_dumb request; |
| 124 request.width = bitmap->width(); | 124 request.width = bitmap->width(); |
| 125 request.height = bitmap->height(); | 125 request.height = bitmap->height(); |
| 126 request.bpp = bitmap->bytesPerPixel() << 3; | 126 request.bpp = bitmap->bytesPerPixel() << 3; |
| 127 request.flags = 0; | 127 request.flags = 0; |
| 128 | 128 |
| 129 if (drmIoctl(bitmap->get_fd(), DRM_IOCTL_MODE_CREATE_DUMB, &request) < 0) { | 129 if (drmIoctl(bitmap->get_fd(), DRM_IOCTL_MODE_CREATE_DUMB, &request) < 0) { |
| 130 DLOG(ERROR) << "Cannot create dumb buffer (" << errno << ") " | 130 DLOG(ERROR) << "Cannot create dumb buffer (" << errno << ") " |
| 131 << strerror(errno); | 131 << strerror(errno); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 151 MAP_SHARED, | 151 MAP_SHARED, |
| 152 bitmap->get_fd(), | 152 bitmap->get_fd(), |
| 153 map_request.offset); | 153 map_request.offset); |
| 154 if (pixels == MAP_FAILED) { | 154 if (pixels == MAP_FAILED) { |
| 155 DLOG(ERROR) << "Cannot mmap dumb buffer (" << errno << ") " | 155 DLOG(ERROR) << "Cannot mmap dumb buffer (" << errno << ") " |
| 156 << strerror(errno); | 156 << strerror(errno); |
| 157 DestroyDumbBuffer(bitmap->get_fd(), bitmap->get_handle()); | 157 DestroyDumbBuffer(bitmap->get_fd(), bitmap->get_handle()); |
| 158 return false; | 158 return false; |
| 159 } | 159 } |
| 160 | 160 |
| 161 bitmap->setPixelRef(new DrmSkPixelRef( | 161 bitmap->setPixelRef(new DriSkPixelRef( |
| 162 pixels, | 162 pixels, |
| 163 color_table, | 163 color_table, |
| 164 bitmap->getSize(), | 164 bitmap->getSize(), |
| 165 bitmap->get_fd(), | 165 bitmap->get_fd(), |
| 166 bitmap->get_handle()))->unref(); | 166 bitmap->get_handle()))->unref(); |
| 167 bitmap->lockPixels(); | 167 bitmap->lockPixels(); |
| 168 | 168 |
| 169 return true; | 169 return true; |
| 170 } | 170 } |
| 171 | 171 |
| 172 //////////////////////////////////////////////////////////////////////////////// | 172 //////////////////////////////////////////////////////////////////////////////// |
| 173 // DrmSkBitmapOzone implementation | 173 // DriSkBitmap implementation |
| 174 | 174 |
| 175 DrmSkBitmapOzone::DrmSkBitmapOzone(int fd) | 175 DriSkBitmap::DriSkBitmap(int fd) |
| 176 : fd_(fd), | 176 : fd_(fd), |
| 177 handle_(0), | 177 handle_(0), |
| 178 framebuffer_(0) { | 178 framebuffer_(0) { |
| 179 } | 179 } |
| 180 | 180 |
| 181 DrmSkBitmapOzone::~DrmSkBitmapOzone() { | 181 DriSkBitmap::~DriSkBitmap() { |
| 182 } | 182 } |
| 183 | 183 |
| 184 bool DrmSkBitmapOzone::Initialize() { | 184 bool DriSkBitmap::Initialize() { |
| 185 DrmAllocator drm_allocator; | 185 DriAllocator drm_allocator; |
| 186 return allocPixels(&drm_allocator, NULL); | 186 return allocPixels(&drm_allocator, NULL); |
| 187 } | 187 } |
| 188 | 188 |
| 189 uint8_t DrmSkBitmapOzone::GetColorDepth() const { | 189 uint8_t DriSkBitmap::GetColorDepth() const { |
| 190 switch (config()) { | 190 switch (config()) { |
| 191 case SkBitmap::kNo_Config: | 191 case SkBitmap::kNo_Config: |
| 192 case SkBitmap::kA1_Config: | 192 case SkBitmap::kA1_Config: |
| 193 case SkBitmap::kA8_Config: | 193 case SkBitmap::kA8_Config: |
| 194 return 0; | 194 return 0; |
| 195 case SkBitmap::kIndex8_Config: | 195 case SkBitmap::kIndex8_Config: |
| 196 return 8; | 196 return 8; |
| 197 case SkBitmap::kRGB_565_Config: | 197 case SkBitmap::kRGB_565_Config: |
| 198 return 16; | 198 return 16; |
| 199 case SkBitmap::kARGB_4444_Config: | 199 case SkBitmap::kARGB_4444_Config: |
| 200 return 12; | 200 return 12; |
| 201 case SkBitmap::kARGB_8888_Config: | 201 case SkBitmap::kARGB_8888_Config: |
| 202 return 24; | 202 return 24; |
| 203 default: | 203 default: |
| 204 NOTREACHED(); | 204 NOTREACHED(); |
| 205 return 0; | 205 return 0; |
| 206 } | 206 } |
| 207 } | 207 } |
| 208 | 208 |
| 209 } // namespace gfx | 209 } // namespace gfx |
| OLD | NEW |