OSDN Git Service

Ensure we close all the gem handles.
[android-x86/external-IA-Hardware-Composer.git] / os / android / gralloc1bufferhandler.cpp
1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include "gralloc1bufferhandler.h"
18
19 #include <hardware/hardware.h>
20 #include <hardware/hwcomposer.h>
21 #include <ui/GraphicBuffer.h>
22 #include <cutils/native_handle.h>
23
24 #include <hwcdefs.h>
25 #include <hwctrace.h>
26
27 #include "commondrmutils.h"
28 #include "utils_android.h"
29
30 namespace hwcomposer {
31
32 // static
33 NativeBufferHandler *NativeBufferHandler::CreateInstance(uint32_t fd) {
34   Gralloc1BufferHandler *handler = new Gralloc1BufferHandler(fd);
35   if (!handler)
36     return NULL;
37
38   if (!handler->Init()) {
39     ETRACE("Failed to initialize GralocBufferHandlers.");
40     delete handler;
41     return NULL;
42   }
43   return handler;
44 }
45
46 Gralloc1BufferHandler::Gralloc1BufferHandler(uint32_t fd) : fd_(fd) {
47 }
48
49 Gralloc1BufferHandler::~Gralloc1BufferHandler() {
50 }
51
52 bool Gralloc1BufferHandler::Init() {
53   int ret = hw_get_module(GRALLOC_HARDWARE_MODULE_ID,
54                           (const hw_module_t **)&gralloc_);
55   if (ret) {
56     ETRACE("Failed to get gralloc module");
57     return false;
58   }
59
60   ret = gralloc_->methods->open(gralloc_, GRALLOC_HARDWARE_MODULE_ID, &device_);
61   if (ret) {
62     ETRACE("Failed to open gralloc module");
63     return false;
64   }
65
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));
72
73   dimensions_ =
74       reinterpret_cast<GRALLOC1_PFN_GET_DIMENSIONS>(gralloc1_dvc->getFunction(
75           gralloc1_dvc, GRALLOC1_FUNCTION_GET_DIMENSIONS));
76
77   return true;
78 }
79
80 bool Gralloc1BufferHandler::CreateBuffer(uint32_t w, uint32_t h, int format,
81                                          HWCNativeHandle *handle) {
82   return CreateGraphicsBuffer(w, h, format, handle);
83 }
84
85 bool Gralloc1BufferHandler::ReleaseBuffer(HWCNativeHandle handle) {
86   if (!ReleaseGraphicsBuffer(handle, fd_))
87     return false;
88
89   gralloc1_device_t *gralloc1_dvc =
90       reinterpret_cast<gralloc1_device_t *>(device_);
91   release_(gralloc1_dvc, handle->imported_handle_);
92
93   return true;
94 }
95
96 void Gralloc1BufferHandler::DestroyHandle(HWCNativeHandle handle) {
97   DestroyBufferHandle(handle);
98 }
99
100 bool Gralloc1BufferHandler::ImportBuffer(HWCNativeHandle handle,
101                                          HwcBuffer *bo) {
102   if (!handle->imported_handle_) {
103     ETRACE("could not find gralloc drm handle");
104     return false;
105   }
106
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_);
111 }
112
113 uint32_t Gralloc1BufferHandler::GetTotalPlanes(HWCNativeHandle handle) {
114   auto gr_handle = (struct cros_gralloc_handle *)handle->imported_handle_;
115   if (!gr_handle) {
116     ETRACE("could not find gralloc drm handle");
117     return false;
118   }
119
120   return drm_bo_get_num_planes(gr_handle->format);
121 }
122
123 void Gralloc1BufferHandler::CopyHandle(HWCNativeHandle source,
124                                        HWCNativeHandle *target) {
125   CopyBufferHandle(source, target);
126 }
127
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*/) {
132   return NULL;
133 }
134
135 void Gralloc1BufferHandler::UnMap(HWCNativeHandle /*handle*/,
136                                   void * /*map_data*/) {
137 }
138
139 }  // namespace hwcomposer