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 "grallocbufferhandler.h"
19 #include <xf86drmMode.h>
22 #include <hardware/hardware.h>
23 #include <hardware/hwcomposer.h>
24 #include <ui/GraphicBuffer.h>
25 #include <cutils/native_handle.h>
26 #include "commondrmutils.h"
29 #include <cros_gralloc_handle.h>
30 #include <cros_gralloc_helpers.h>
32 #include <gralloc_drm_handle.h>
37 #include "drmhwcgralloc.h"
38 #include "commondrmutils.h"
39 #include "utils_android.h"
41 namespace hwcomposer {
44 NativeBufferHandler *NativeBufferHandler::CreateInstance(uint32_t fd) {
45 GrallocBufferHandler *handler = new GrallocBufferHandler(fd);
49 if (!handler->Init()) {
50 ETRACE("Failed to initialize GralocBufferHandlers.");
57 GrallocBufferHandler::GrallocBufferHandler(uint32_t fd) : fd_(fd) {
60 GrallocBufferHandler::~GrallocBufferHandler() {
63 bool GrallocBufferHandler::Init() {
64 int ret = hw_get_module(GRALLOC_HARDWARE_MODULE_ID,
65 (const hw_module_t **)&gralloc_);
67 ETRACE("Failed to open gralloc module");
73 bool GrallocBufferHandler::CreateBuffer(uint32_t w, uint32_t h, int format,
74 HWCNativeHandle *handle) {
75 return CreateGraphicsBuffer(w, h, format, handle);
78 bool GrallocBufferHandler::ReleaseBuffer(HWCNativeHandle handle) {
79 return ReleaseGraphicsBuffer(handle, fd_);
82 void GrallocBufferHandler::DestroyHandle(HWCNativeHandle handle) {
83 DestroyBufferHandle(handle);
87 bool GrallocBufferHandler::ImportBuffer(HWCNativeHandle handle, HwcBuffer *bo) {
88 if (!handle->imported_handle_) {
89 ETRACE("could not find gralloc drm handle");
93 return ImportGraphicsBuffer(handle, bo, fd_);
96 uint32_t GrallocBufferHandler::GetTotalPlanes(HWCNativeHandle handle) {
97 auto gr_handle = (struct cros_gralloc_handle *)handle->imported_handle_;
99 ETRACE("could not find gralloc drm handle");
103 return drm_bo_get_num_planes(gr_handle->format);
106 bool GrallocBufferHandler::ImportBuffer(HWCNativeHandle handle, HwcBuffer *bo) {
108 int ret = gralloc_->perform(gralloc_, GRALLOC_MODULE_PERFORM_DRM_IMPORT, fd_,
109 handle->handle_, &hwc_bo);
111 ETRACE("GRALLOC_MODULE_PERFORM_DRM_IMPORT failed %d", ret);
115 memset(bo, 0, sizeof(struct HwcBuffer));
116 gralloc_->registerBuffer(gralloc_, handle->handle_);
117 gralloc_drm_handle_t *gr_handle = gralloc_drm_handle(handle->handle_);
118 bo->width = hwc_bo.width;
119 bo->height = hwc_bo.height;
120 bo->format = hwc_bo.format;
121 for (uint32_t i = 0; i < 4; i++) {
122 bo->pitches[i] = hwc_bo.pitches[i];
123 bo->offsets[i] = hwc_bo.offsets[i];
124 bo->gem_handles[i] = hwc_bo.gem_handles[i];
127 ETRACE("could not find gralloc drm handle");
131 if (gr_handle->usage & GRALLOC_USAGE_PROTECTED) {
132 bo->usage |= hwcomposer::kLayerProtected;
133 } else if (gr_handle->usage & GRALLOC_USAGE_CURSOR) {
134 bo->usage |= hwcomposer::kLayerCursor;
137 bo->prime_fd = gr_handle->prime_fd;
143 uint32_t GrallocBufferHandler::GetTotalPlanes(HWCNativeHandle /*handle*/) {
148 void GrallocBufferHandler::CopyHandle(HWCNativeHandle source,
149 HWCNativeHandle *target) {
150 CopyBufferHandle(source, target);
153 void *GrallocBufferHandler::Map(HWCNativeHandle /*handle*/, uint32_t /*x*/,
154 uint32_t /*y*/, uint32_t /*width*/,
155 uint32_t /*height*/, uint32_t * /*stride*/,
156 void ** /*map_data*/, size_t /*plane*/) {
160 void GrallocBufferHandler::UnMap(HWCNativeHandle /*handle*/,
161 void * /*map_data*/) {
164 } // namespace hwcomposer