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_buffer.h" | 5 #include "ui/ozone/platform/dri/dri_buffer.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/logging.h" | 12 #include "base/logging.h" |
13 #include "third_party/skia/include/core/SkCanvas.h" | 13 #include "third_party/skia/include/core/SkCanvas.h" |
14 #include "ui/ozone/platform/dri/dri_wrapper.h" | 14 #include "ui/ozone/platform/dri/dri_wrapper.h" |
15 | 15 |
16 namespace ui { | 16 namespace ui { |
17 | 17 |
18 namespace { | 18 namespace { |
19 | 19 |
| 20 // Modesetting cannot happen from a buffer with transparencies. Return the size |
| 21 // of a pixel without alpha. |
| 22 uint8_t GetColorDepth(SkColorType type) { |
| 23 switch (type) { |
| 24 case kUnknown_SkColorType: |
| 25 case kAlpha_8_SkColorType: |
| 26 return 0; |
| 27 case kIndex_8_SkColorType: |
| 28 return 8; |
| 29 case kRGB_565_SkColorType: |
| 30 return 16; |
| 31 case kARGB_4444_SkColorType: |
| 32 return 12; |
| 33 case kPMColor_SkColorType: |
| 34 return 24; |
| 35 default: |
| 36 NOTREACHED(); |
| 37 return 0; |
| 38 } |
| 39 } |
| 40 |
20 void DestroyDumbBuffer(int fd, uint32_t handle) { | 41 void DestroyDumbBuffer(int fd, uint32_t handle) { |
21 struct drm_mode_destroy_dumb destroy_request; | 42 struct drm_mode_destroy_dumb destroy_request; |
22 destroy_request.handle = handle; | 43 destroy_request.handle = handle; |
23 drmIoctl(fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_request); | 44 drmIoctl(fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_request); |
24 } | 45 } |
25 | 46 |
26 bool CreateDumbBuffer(DriWrapper* dri, | 47 bool CreateDumbBuffer(DriWrapper* dri, |
27 const SkImageInfo& info, | 48 const SkImageInfo& info, |
28 uint32_t* handle, | 49 uint32_t* handle, |
29 uint32_t* stride, | 50 uint32_t* stride, |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
70 | 91 |
71 } // namespace | 92 } // namespace |
72 | 93 |
73 DriBuffer::DriBuffer(DriWrapper* dri) | 94 DriBuffer::DriBuffer(DriWrapper* dri) |
74 : dri_(dri), handle_(0), framebuffer_(0) {} | 95 : dri_(dri), handle_(0), framebuffer_(0) {} |
75 | 96 |
76 DriBuffer::~DriBuffer() { | 97 DriBuffer::~DriBuffer() { |
77 if (!surface_) | 98 if (!surface_) |
78 return; | 99 return; |
79 | 100 |
| 101 if (framebuffer_) |
| 102 dri_->RemoveFramebuffer(framebuffer_); |
| 103 |
80 SkImageInfo info; | 104 SkImageInfo info; |
81 void* pixels = const_cast<void*>(surface_->peekPixels(&info, NULL)); | 105 void* pixels = const_cast<void*>(surface_->peekPixels(&info, NULL)); |
82 if (!pixels) | 106 if (!pixels) |
83 return; | 107 return; |
84 | 108 |
85 munmap(pixels, info.getSafeSize(stride_)); | 109 munmap(pixels, info.getSafeSize(stride_)); |
86 DestroyDumbBuffer(dri_->get_fd(), handle_); | 110 DestroyDumbBuffer(dri_->get_fd(), handle_); |
87 } | 111 } |
88 | 112 |
89 bool DriBuffer::Initialize(const SkImageInfo& info) { | 113 bool DriBuffer::Initialize(const SkImageInfo& info) { |
90 void* pixels = NULL; | 114 void* pixels = NULL; |
91 if (!CreateDumbBuffer(dri_, info, &handle_, &stride_, &pixels)) { | 115 if (!CreateDumbBuffer(dri_, info, &handle_, &stride_, &pixels)) { |
92 DLOG(ERROR) << "Cannot allocate drm dumb buffer"; | 116 DLOG(ERROR) << "Cannot allocate drm dumb buffer"; |
93 return false; | 117 return false; |
94 } | 118 } |
95 | 119 |
| 120 if (!dri_->AddFramebuffer(info.width(), |
| 121 info.height(), |
| 122 GetColorDepth(info.colorType()), |
| 123 info.bytesPerPixel() << 3, |
| 124 stride_, |
| 125 handle_, |
| 126 &framebuffer_)) { |
| 127 DLOG(ERROR) << "Failed to register framebuffer: " << strerror(errno); |
| 128 return false; |
| 129 } |
| 130 |
96 surface_ = skia::AdoptRef(SkSurface::NewRasterDirect(info, pixels, stride_)); | 131 surface_ = skia::AdoptRef(SkSurface::NewRasterDirect(info, pixels, stride_)); |
97 if (!surface_) { | 132 if (!surface_) { |
98 DLOG(ERROR) << "Cannot install Skia pixels for drm buffer"; | 133 DLOG(ERROR) << "Cannot install Skia pixels for drm buffer"; |
99 return false; | 134 return false; |
100 } | 135 } |
101 | 136 |
102 return true; | 137 return true; |
103 } | 138 } |
104 | 139 |
105 uint8_t DriBuffer::GetColorDepth() const { | |
106 switch (surface_->getCanvas()->imageInfo().colorType()) { | |
107 case kUnknown_SkColorType: | |
108 case kAlpha_8_SkColorType: | |
109 return 0; | |
110 case kIndex_8_SkColorType: | |
111 return 8; | |
112 case kRGB_565_SkColorType: | |
113 return 16; | |
114 case kARGB_4444_SkColorType: | |
115 return 12; | |
116 case kPMColor_SkColorType: | |
117 return 24; | |
118 default: | |
119 NOTREACHED(); | |
120 return 0; | |
121 } | |
122 } | |
123 | |
124 } // namespace ui | 140 } // namespace ui |
OLD | NEW |