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));
109 bool Gralloc1BufferHandler::CreateBuffer(uint32_t w, uint32_t h, int format,
110 HWCNativeHandle *handle,
111 uint32_t layer_type) const {
112 struct gralloc_handle *temp = new struct gralloc_handle();
113 gralloc1_device_t *gralloc1_dvc =
114 reinterpret_cast<gralloc1_device_t *>(device_);
116 create_descriptor_(gralloc1_dvc, &temp->gralloc1_buffer_descriptor_t_);
118 uint32_t pixel_format = 0;
119 bool force_normal_usage = false;
121 pixel_format = DrmFormatToHALFormat(format);
124 if (pixel_format == 0) {
125 pixel_format = HAL_PIXEL_FORMAT_RGBA_8888;
128 set_format_(gralloc1_dvc, temp->gralloc1_buffer_descriptor_t_, pixel_format);
130 if ((layer_type == hwcomposer::kLayerVideo) &&
131 !IsSupportedMediaFormat(format)) {
132 ETRACE("Forcing normal usage for Video Layer. \n");
133 force_normal_usage = true;
136 if ((layer_type == hwcomposer::kLayerNormal) || force_normal_usage) {
137 usage |= GRALLOC1_CONSUMER_USAGE_HWCOMPOSER |
138 GRALLOC1_PRODUCER_USAGE_GPU_RENDER_TARGET |
139 GRALLOC1_CONSUMER_USAGE_GPU_TEXTURE;
140 } else if (layer_type == hwcomposer::kLayerVideo) {
141 switch (pixel_format) {
142 case HAL_PIXEL_FORMAT_YCbCr_422_I:
143 case HAL_PIXEL_FORMAT_Y8:
144 usage |= GRALLOC1_CONSUMER_USAGE_GPU_TEXTURE |
145 GRALLOC1_PRODUCER_USAGE_VIDEO_DECODER;
148 usage |= GRALLOC1_PRODUCER_USAGE_CAMERA |
149 GRALLOC1_CONSUMER_USAGE_CAMERA |
150 GRALLOC1_PRODUCER_USAGE_VIDEO_DECODER |
151 GRALLOC1_CONSUMER_USAGE_GPU_TEXTURE;
153 } else if (layer_type == hwcomposer::kLayerCursor) {
154 usage |= GRALLOC1_CONSUMER_USAGE_CURSOR;
157 set_consumer_usage_(gralloc1_dvc, temp->gralloc1_buffer_descriptor_t_, usage);
158 set_producer_usage_(gralloc1_dvc, temp->gralloc1_buffer_descriptor_t_, usage);
159 set_dimensions_(gralloc1_dvc, temp->gralloc1_buffer_descriptor_t_, w, h);
160 allocate_(gralloc1_dvc, 1, &temp->gralloc1_buffer_descriptor_t_,
163 if (!temp->handle_) {
164 ETRACE("Failed to allocate buffer \n");
167 temp->hwc_buffer_ = true;
173 bool Gralloc1BufferHandler::ReleaseBuffer(HWCNativeHandle handle) const {
174 gralloc1_device_t *gralloc1_dvc =
175 reinterpret_cast<gralloc1_device_t *>(device_);
177 if (handle->hwc_buffer_) {
178 release_(gralloc1_dvc, handle->handle_);
179 } else if (handle->imported_handle_) {
180 release_(gralloc1_dvc, handle->imported_handle_);
183 if (handle->gralloc1_buffer_descriptor_t_ > 0)
184 destroy_descriptor_(gralloc1_dvc, handle->gralloc1_buffer_descriptor_t_);
189 void Gralloc1BufferHandler::DestroyHandle(HWCNativeHandle handle) const {
190 DestroyBufferHandle(handle);
193 bool Gralloc1BufferHandler::ImportBuffer(HWCNativeHandle handle) const {
194 if (!handle->imported_handle_) {
195 ETRACE("could not find gralloc drm handle");
199 gralloc1_device_t *gralloc1_dvc =
200 reinterpret_cast<gralloc1_device_t *>(device_);
201 register_(gralloc1_dvc, handle->imported_handle_);
202 return ImportGraphicsBuffer(handle, fd_);
205 uint32_t Gralloc1BufferHandler::GetTotalPlanes(HWCNativeHandle handle) const {
206 auto gr_handle = (struct cros_gralloc_handle *)handle->imported_handle_;
208 ETRACE("could not find gralloc drm handle");
212 return drm_bo_get_num_planes(gr_handle->format);
215 void Gralloc1BufferHandler::CopyHandle(HWCNativeHandle source,
216 HWCNativeHandle *target) const {
217 CopyBufferHandle(source, target);
220 void *Gralloc1BufferHandler::Map(HWCNativeHandle handle, uint32_t x, uint32_t y,
221 uint32_t width, uint32_t height,
222 uint32_t * /*stride*/, void **map_data,
223 size_t /*plane*/) const {
224 auto gr_handle = (struct cros_gralloc_handle *)handle->imported_handle_;
226 ETRACE("could not find gralloc drm handle");
230 int acquireFence = -1;
231 gralloc1_rect_t rect{};
235 rect.height = height;
237 gralloc1_device_t *gralloc1_dvc =
238 reinterpret_cast<gralloc1_device_t *>(device_);
239 uint32_t status = lock_(gralloc1_dvc, handle->imported_handle_,
240 GRALLOC1_PRODUCER_USAGE_CPU_WRITE_OFTEN,
241 GRALLOC1_CONSUMER_USAGE_CPU_READ_OFTEN, &rect,
242 map_data, acquireFence);
243 return (GRALLOC1_ERROR_NONE == status) ? *map_data : NULL;
246 int32_t Gralloc1BufferHandler::UnMap(HWCNativeHandle handle,
247 void * /*map_data*/) const {
248 auto gr_handle = (struct cros_gralloc_handle *)handle->imported_handle_;
250 ETRACE("could not find gralloc drm handle");
251 return GRALLOC1_ERROR_BAD_HANDLE;
254 int releaseFence = 0;
255 gralloc1_device_t *gralloc1_dvc =
256 reinterpret_cast<gralloc1_device_t *>(device_);
257 return unlock_(gralloc1_dvc, handle->imported_handle_, &releaseFence);
260 } // namespace hwcomposer