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"
28 #include "utils_android.h"
30 namespace hwcomposer {
33 NativeBufferHandler *NativeBufferHandler::CreateInstance(uint32_t fd) {
34 Gralloc1BufferHandler *handler = new Gralloc1BufferHandler(fd);
38 if (!handler->Init()) {
39 ETRACE("Failed to initialize GralocBufferHandlers.");
46 Gralloc1BufferHandler::Gralloc1BufferHandler(uint32_t fd) : fd_(fd) {
49 Gralloc1BufferHandler::~Gralloc1BufferHandler() {
50 gralloc1_device_t *gralloc1_dvc =
51 reinterpret_cast<gralloc1_device_t *>(device_);
52 gralloc1_dvc->common.close(device_);
55 bool Gralloc1BufferHandler::Init() {
56 int ret = hw_get_module(GRALLOC_HARDWARE_MODULE_ID,
57 (const hw_module_t **)&gralloc_);
59 ETRACE("Failed to get gralloc module");
63 ret = gralloc_->methods->open(gralloc_, GRALLOC_HARDWARE_MODULE_ID, &device_);
65 ETRACE("Failed to open gralloc module");
69 gralloc1_device_t *gralloc1_dvc =
70 reinterpret_cast<gralloc1_device_t *>(device_);
71 register_ = reinterpret_cast<GRALLOC1_PFN_RETAIN>(
72 gralloc1_dvc->getFunction(gralloc1_dvc, GRALLOC1_FUNCTION_RETAIN));
73 release_ = reinterpret_cast<GRALLOC1_PFN_RELEASE>(
74 gralloc1_dvc->getFunction(gralloc1_dvc, GRALLOC1_FUNCTION_RELEASE));
75 lock_ = reinterpret_cast<GRALLOC1_PFN_LOCK>(
76 gralloc1_dvc->getFunction(gralloc1_dvc, GRALLOC1_FUNCTION_LOCK));
77 unlock_ = reinterpret_cast<GRALLOC1_PFN_UNLOCK>(
78 gralloc1_dvc->getFunction(gralloc1_dvc, GRALLOC1_FUNCTION_UNLOCK));
81 reinterpret_cast<GRALLOC1_PFN_GET_DIMENSIONS>(gralloc1_dvc->getFunction(
82 gralloc1_dvc, GRALLOC1_FUNCTION_GET_DIMENSIONS));
87 bool Gralloc1BufferHandler::CreateBuffer(uint32_t w, uint32_t h, int format,
88 HWCNativeHandle *handle,
89 uint32_t layer_type) {
90 return CreateGraphicsBuffer(w, h, format, handle, layer_type);
93 bool Gralloc1BufferHandler::ReleaseBuffer(HWCNativeHandle handle) {
94 if (!ReleaseGraphicsBuffer(handle, fd_))
97 gralloc1_device_t *gralloc1_dvc =
98 reinterpret_cast<gralloc1_device_t *>(device_);
99 release_(gralloc1_dvc, handle->imported_handle_);
104 void Gralloc1BufferHandler::DestroyHandle(HWCNativeHandle handle) {
105 DestroyBufferHandle(handle);
108 bool Gralloc1BufferHandler::ImportBuffer(HWCNativeHandle handle) {
109 if (!handle->imported_handle_) {
110 ETRACE("could not find gralloc drm handle");
114 gralloc1_device_t *gralloc1_dvc =
115 reinterpret_cast<gralloc1_device_t *>(device_);
116 register_(gralloc1_dvc, handle->imported_handle_);
117 return ImportGraphicsBuffer(handle, fd_);
120 uint32_t Gralloc1BufferHandler::GetTotalPlanes(HWCNativeHandle handle) {
121 auto gr_handle = (struct cros_gralloc_handle *)handle->imported_handle_;
123 ETRACE("could not find gralloc drm handle");
127 return drm_bo_get_num_planes(gr_handle->format);
130 void Gralloc1BufferHandler::CopyHandle(HWCNativeHandle source,
131 HWCNativeHandle *target) {
132 CopyBufferHandle(source, target);
135 void *Gralloc1BufferHandler::Map(HWCNativeHandle handle, uint32_t x,
136 uint32_t y, uint32_t width,
137 uint32_t height, uint32_t * /*stride*/,
138 void ** map_data, size_t /*plane*/) {
139 auto gr_handle = (struct cros_gralloc_handle *)handle->imported_handle_;
141 ETRACE("could not find gralloc drm handle");
145 int acquireFence = -1;
146 gralloc1_rect_t rect{};
150 rect.height = height;
152 gralloc1_device_t *gralloc1_dvc =
153 reinterpret_cast<gralloc1_device_t *>(device_);
154 uint32_t status = lock_(gralloc1_dvc, handle->imported_handle_, GRALLOC_USAGE_SW_READ_RARELY,
155 GRALLOC_USAGE_SW_READ_RARELY, &rect, map_data, acquireFence);
156 return (GRALLOC1_ERROR_NONE == status) ? *map_data : NULL;
159 int32_t Gralloc1BufferHandler::UnMap(HWCNativeHandle handle,
160 void * /*map_data*/) {
161 auto gr_handle = (struct cros_gralloc_handle *)handle->imported_handle_;
163 ETRACE("could not find gralloc drm handle");
164 return GRALLOC1_ERROR_BAD_HANDLE;
167 int releaseFence = 0;
168 gralloc1_device_t *gralloc1_dvc =
169 reinterpret_cast<gralloc1_device_t *>(device_);
170 return unlock_(gralloc1_dvc, handle->imported_handle_, &releaseFence);
173 } // namespace hwcomposer