2 * Copyright (C) 2016 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include "gralloc1bufferhandler.h"
19 #include <hardware/hardware.h>
20 #include <hardware/hwcomposer.h>
21 #include <ui/GraphicBuffer.h>
22 #include <cutils/native_handle.h>
27 #include "commondrmutils.h"
29 #include "utils_android.h"
31 namespace hwcomposer {
34 NativeBufferHandler *NativeBufferHandler::CreateInstance(uint32_t fd) {
35 Gralloc1BufferHandler *handler = new Gralloc1BufferHandler(fd);
39 if (!handler->Init()) {
40 ETRACE("Failed to initialize GralocBufferHandlers.");
47 Gralloc1BufferHandler::Gralloc1BufferHandler(uint32_t fd) : fd_(fd) {
50 Gralloc1BufferHandler::~Gralloc1BufferHandler() {
51 gralloc1_device_t *gralloc1_dvc =
52 reinterpret_cast<gralloc1_device_t *>(device_);
53 gralloc1_dvc->common.close(device_);
56 bool Gralloc1BufferHandler::Init() {
57 int ret = hw_get_module(GRALLOC_HARDWARE_MODULE_ID,
58 (const hw_module_t **)&gralloc_);
60 ETRACE("Failed to get gralloc module");
64 ret = gralloc_->methods->open(gralloc_, GRALLOC_HARDWARE_MODULE_ID, &device_);
66 ETRACE("Failed to open gralloc module");
70 gralloc1_device_t *gralloc1_dvc =
71 reinterpret_cast<gralloc1_device_t *>(device_);
72 register_ = reinterpret_cast<GRALLOC1_PFN_RETAIN>(
73 gralloc1_dvc->getFunction(gralloc1_dvc, GRALLOC1_FUNCTION_RETAIN));
74 release_ = reinterpret_cast<GRALLOC1_PFN_RELEASE>(
75 gralloc1_dvc->getFunction(gralloc1_dvc, GRALLOC1_FUNCTION_RELEASE));
76 lock_ = reinterpret_cast<GRALLOC1_PFN_LOCK>(
77 gralloc1_dvc->getFunction(gralloc1_dvc, GRALLOC1_FUNCTION_LOCK));
78 unlock_ = reinterpret_cast<GRALLOC1_PFN_UNLOCK>(
79 gralloc1_dvc->getFunction(gralloc1_dvc, GRALLOC1_FUNCTION_UNLOCK));
82 reinterpret_cast<GRALLOC1_PFN_GET_DIMENSIONS>(gralloc1_dvc->getFunction(
83 gralloc1_dvc, GRALLOC1_FUNCTION_GET_DIMENSIONS));
85 create_descriptor_ = reinterpret_cast<GRALLOC1_PFN_CREATE_DESCRIPTOR>(
86 gralloc1_dvc->getFunction(gralloc1_dvc,
87 GRALLOC1_FUNCTION_CREATE_DESCRIPTOR));
88 destroy_descriptor_ = reinterpret_cast<GRALLOC1_PFN_DESTROY_DESCRIPTOR>(
89 gralloc1_dvc->getFunction(gralloc1_dvc,
90 GRALLOC1_FUNCTION_DESTROY_DESCRIPTOR));
92 set_consumer_usage_ = reinterpret_cast<GRALLOC1_PFN_SET_CONSUMER_USAGE>(
93 gralloc1_dvc->getFunction(gralloc1_dvc,
94 GRALLOC1_FUNCTION_SET_CONSUMER_USAGE));
96 reinterpret_cast<GRALLOC1_PFN_SET_DIMENSIONS>(gralloc1_dvc->getFunction(
97 gralloc1_dvc, GRALLOC1_FUNCTION_SET_DIMENSIONS));
98 set_format_ = reinterpret_cast<GRALLOC1_PFN_SET_FORMAT>(
99 gralloc1_dvc->getFunction(gralloc1_dvc, GRALLOC1_FUNCTION_SET_FORMAT));
100 set_producer_usage_ = reinterpret_cast<GRALLOC1_PFN_SET_PRODUCER_USAGE>(
101 gralloc1_dvc->getFunction(gralloc1_dvc,
102 GRALLOC1_FUNCTION_SET_PRODUCER_USAGE));
103 allocate_ = reinterpret_cast<GRALLOC1_PFN_ALLOCATE>(
104 gralloc1_dvc->getFunction(gralloc1_dvc, GRALLOC1_FUNCTION_ALLOCATE));
106 set_modifier_ = reinterpret_cast<GRALLOC1_PFN_SET_MODIFIER>(
107 gralloc1_dvc->getFunction(gralloc1_dvc, GRALLOC1_FUNCTION_SET_MODIFIER));
112 bool Gralloc1BufferHandler::CreateBuffer(uint32_t w, uint32_t h, int format,
113 HWCNativeHandle *handle,
114 uint32_t layer_type) const {
115 struct gralloc_handle *temp = new struct gralloc_handle();
116 gralloc1_device_t *gralloc1_dvc =
117 reinterpret_cast<gralloc1_device_t *>(device_);
119 create_descriptor_(gralloc1_dvc, &temp->gralloc1_buffer_descriptor_t_);
121 uint32_t pixel_format = 0;
122 bool force_normal_usage = false;
124 pixel_format = DrmFormatToHALFormat(format);
127 if (pixel_format == 0) {
128 pixel_format = HAL_PIXEL_FORMAT_RGBA_8888;
131 set_format_(gralloc1_dvc, temp->gralloc1_buffer_descriptor_t_, pixel_format);
134 // These formats are RBC supported in i915 driver
135 if ((format == DRM_FORMAT_XRGB8888) || (format == DRM_FORMAT_XBGR8888) ||
136 (format == DRM_FORMAT_ARGB8888) || (format == DRM_FORMAT_ABGR8888)) {
138 uint64_t modifier = I915_FORMAT_MOD_Y_TILED_CCS;
139 set_modifier_(gralloc1_dvc, temp->gralloc1_buffer_descriptor_t_, modifier);
144 if ((layer_type == hwcomposer::kLayerVideo) &&
145 !IsSupportedMediaFormat(format)) {
146 ETRACE("Forcing normal usage for Video Layer. \n");
147 force_normal_usage = true;
150 if ((layer_type == hwcomposer::kLayerNormal) || force_normal_usage) {
151 usage |= GRALLOC1_CONSUMER_USAGE_HWCOMPOSER |
152 GRALLOC1_PRODUCER_USAGE_GPU_RENDER_TARGET |
153 GRALLOC1_CONSUMER_USAGE_GPU_TEXTURE;
154 } else if (layer_type == hwcomposer::kLayerVideo) {
155 switch (pixel_format) {
156 case HAL_PIXEL_FORMAT_YCbCr_422_I:
157 case HAL_PIXEL_FORMAT_Y8:
158 usage |= GRALLOC1_CONSUMER_USAGE_GPU_TEXTURE |
159 GRALLOC1_PRODUCER_USAGE_VIDEO_DECODER;
162 usage |= GRALLOC1_PRODUCER_USAGE_CAMERA |
163 GRALLOC1_CONSUMER_USAGE_CAMERA |
164 GRALLOC1_PRODUCER_USAGE_VIDEO_DECODER |
165 GRALLOC1_CONSUMER_USAGE_GPU_TEXTURE;
167 } else if (layer_type == hwcomposer::kLayerCursor) {
168 usage |= GRALLOC1_CONSUMER_USAGE_CURSOR;
171 set_consumer_usage_(gralloc1_dvc, temp->gralloc1_buffer_descriptor_t_, usage);
172 set_producer_usage_(gralloc1_dvc, temp->gralloc1_buffer_descriptor_t_, usage);
173 set_dimensions_(gralloc1_dvc, temp->gralloc1_buffer_descriptor_t_, w, h);
174 allocate_(gralloc1_dvc, 1, &temp->gralloc1_buffer_descriptor_t_,
177 if (!temp->handle_) {
178 ETRACE("Failed to allocate buffer \n");
181 temp->hwc_buffer_ = true;
187 bool Gralloc1BufferHandler::ReleaseBuffer(HWCNativeHandle handle) const {
188 gralloc1_device_t *gralloc1_dvc =
189 reinterpret_cast<gralloc1_device_t *>(device_);
191 if (handle->hwc_buffer_) {
192 release_(gralloc1_dvc, handle->handle_);
193 } else if (handle->imported_handle_) {
194 release_(gralloc1_dvc, handle->imported_handle_);
197 if (handle->gralloc1_buffer_descriptor_t_ > 0)
198 destroy_descriptor_(gralloc1_dvc, handle->gralloc1_buffer_descriptor_t_);
203 void Gralloc1BufferHandler::DestroyHandle(HWCNativeHandle handle) const {
204 DestroyBufferHandle(handle);
207 bool Gralloc1BufferHandler::ImportBuffer(HWCNativeHandle handle) const {
208 if (!handle->imported_handle_) {
209 ETRACE("could not find gralloc drm handle");
213 gralloc1_device_t *gralloc1_dvc =
214 reinterpret_cast<gralloc1_device_t *>(device_);
215 register_(gralloc1_dvc, handle->imported_handle_);
216 if (!ImportGraphicsBuffer(handle, fd_)) {
223 uint32_t Gralloc1BufferHandler::GetTotalPlanes(HWCNativeHandle handle) const {
224 return handle->meta_data_.num_planes_;
227 void Gralloc1BufferHandler::CopyHandle(HWCNativeHandle source,
228 HWCNativeHandle *target) const {
229 CopyBufferHandle(source, target);
232 void *Gralloc1BufferHandler::Map(HWCNativeHandle handle, uint32_t x, uint32_t y,
233 uint32_t width, uint32_t height,
234 uint32_t * /*stride*/, void **map_data,
235 size_t /*plane*/) const {
236 auto gr_handle = (struct cros_gralloc_handle *)handle->imported_handle_;
238 ETRACE("could not find gralloc drm handle");
242 int acquireFence = -1;
243 gralloc1_rect_t rect{};
247 rect.height = height;
249 gralloc1_device_t *gralloc1_dvc =
250 reinterpret_cast<gralloc1_device_t *>(device_);
251 uint32_t status = lock_(gralloc1_dvc, handle->imported_handle_,
252 GRALLOC1_PRODUCER_USAGE_CPU_WRITE_OFTEN,
253 GRALLOC1_CONSUMER_USAGE_CPU_READ_OFTEN, &rect,
254 map_data, acquireFence);
255 return (GRALLOC1_ERROR_NONE == status) ? *map_data : NULL;
258 int32_t Gralloc1BufferHandler::UnMap(HWCNativeHandle handle,
259 void * /*map_data*/) const {
260 auto gr_handle = (struct cros_gralloc_handle *)handle->imported_handle_;
262 ETRACE("could not find gralloc drm handle");
263 return GRALLOC1_ERROR_BAD_HANDLE;
266 int releaseFence = 0;
267 gralloc1_device_t *gralloc1_dvc =
268 reinterpret_cast<gralloc1_device_t *>(device_);
269 return unlock_(gralloc1_dvc, handle->imported_handle_, &releaseFence);
272 } // namespace hwcomposer