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() {
52 bool Gralloc1BufferHandler::Init() {
53 int ret = hw_get_module(GRALLOC_HARDWARE_MODULE_ID,
54 (const hw_module_t **)&gralloc_);
56 ETRACE("Failed to get gralloc module");
60 ret = gralloc_->methods->open(gralloc_, GRALLOC_HARDWARE_MODULE_ID, &device_);
62 ETRACE("Failed to open gralloc module");
66 gralloc1_device_t *gralloc1_dvc =
67 reinterpret_cast<gralloc1_device_t *>(device_);
68 register_ = reinterpret_cast<GRALLOC1_PFN_RETAIN>(
69 gralloc1_dvc->getFunction(gralloc1_dvc, GRALLOC1_FUNCTION_RETAIN));
70 release_ = reinterpret_cast<GRALLOC1_PFN_RELEASE>(
71 gralloc1_dvc->getFunction(gralloc1_dvc, GRALLOC1_FUNCTION_RELEASE));
74 reinterpret_cast<GRALLOC1_PFN_GET_DIMENSIONS>(gralloc1_dvc->getFunction(
75 gralloc1_dvc, GRALLOC1_FUNCTION_GET_DIMENSIONS));
80 bool Gralloc1BufferHandler::CreateBuffer(uint32_t w, uint32_t h, int format,
81 HWCNativeHandle *handle) {
82 return CreateGraphicsBuffer(w, h, format, handle);
85 bool Gralloc1BufferHandler::ReleaseBuffer(HWCNativeHandle handle) {
86 if (!ReleaseGraphicsBuffer(handle, fd_))
89 gralloc1_device_t *gralloc1_dvc =
90 reinterpret_cast<gralloc1_device_t *>(device_);
91 release_(gralloc1_dvc, handle->imported_handle_);
96 void Gralloc1BufferHandler::DestroyHandle(HWCNativeHandle handle) {
97 DestroyBufferHandle(handle);
100 bool Gralloc1BufferHandler::ImportBuffer(HWCNativeHandle handle,
102 if (!handle->imported_handle_) {
103 ETRACE("could not find gralloc drm handle");
107 gralloc1_device_t *gralloc1_dvc =
108 reinterpret_cast<gralloc1_device_t *>(device_);
109 register_(gralloc1_dvc, handle->imported_handle_);
110 return ImportGraphicsBuffer(handle, bo, fd_);
113 uint32_t Gralloc1BufferHandler::GetTotalPlanes(HWCNativeHandle handle) {
114 auto gr_handle = (struct cros_gralloc_handle *)handle->imported_handle_;
116 ETRACE("could not find gralloc drm handle");
120 return drm_bo_get_num_planes(gr_handle->format);
123 void Gralloc1BufferHandler::CopyHandle(HWCNativeHandle source,
124 HWCNativeHandle *target) {
125 CopyBufferHandle(source, target);
128 void *Gralloc1BufferHandler::Map(HWCNativeHandle /*handle*/, uint32_t /*x*/,
129 uint32_t /*y*/, uint32_t /*width*/,
130 uint32_t /*height*/, uint32_t * /*stride*/,
131 void ** /*map_data*/, size_t /*plane*/) {
135 void Gralloc1BufferHandler::UnMap(HWCNativeHandle /*handle*/,
136 void * /*map_data*/) {
139 } // namespace hwcomposer