OSDN Git Service

Add Meta data field to our NativeHandle.
[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   gralloc1_device_t *gralloc1_dvc =
51       reinterpret_cast<gralloc1_device_t *>(device_);
52   gralloc1_dvc->common.close(device_);
53 }
54
55 bool Gralloc1BufferHandler::Init() {
56   int ret = hw_get_module(GRALLOC_HARDWARE_MODULE_ID,
57                           (const hw_module_t **)&gralloc_);
58   if (ret) {
59     ETRACE("Failed to get gralloc module");
60     return false;
61   }
62
63   ret = gralloc_->methods->open(gralloc_, GRALLOC_HARDWARE_MODULE_ID, &device_);
64   if (ret) {
65     ETRACE("Failed to open gralloc module");
66     return false;
67   }
68
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));
79
80   dimensions_ =
81       reinterpret_cast<GRALLOC1_PFN_GET_DIMENSIONS>(gralloc1_dvc->getFunction(
82           gralloc1_dvc, GRALLOC1_FUNCTION_GET_DIMENSIONS));
83
84   return true;
85 }
86
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);
91 }
92
93 bool Gralloc1BufferHandler::ReleaseBuffer(HWCNativeHandle handle) {
94   if (!ReleaseGraphicsBuffer(handle, fd_))
95     return false;
96
97   gralloc1_device_t *gralloc1_dvc =
98       reinterpret_cast<gralloc1_device_t *>(device_);
99   release_(gralloc1_dvc, handle->imported_handle_);
100
101   return true;
102 }
103
104 void Gralloc1BufferHandler::DestroyHandle(HWCNativeHandle handle) {
105   DestroyBufferHandle(handle);
106 }
107
108 bool Gralloc1BufferHandler::ImportBuffer(HWCNativeHandle handle) {
109   if (!handle->imported_handle_) {
110     ETRACE("could not find gralloc drm handle");
111     return false;
112   }
113
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_);
118 }
119
120 uint32_t Gralloc1BufferHandler::GetTotalPlanes(HWCNativeHandle handle) {
121   auto gr_handle = (struct cros_gralloc_handle *)handle->imported_handle_;
122   if (!gr_handle) {
123     ETRACE("could not find gralloc drm handle");
124     return false;
125   }
126
127   return drm_bo_get_num_planes(gr_handle->format);
128 }
129
130 void Gralloc1BufferHandler::CopyHandle(HWCNativeHandle source,
131                                        HWCNativeHandle *target) {
132   CopyBufferHandle(source, target);
133 }
134
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_;
140   if (!gr_handle) {
141     ETRACE("could not find gralloc drm handle");
142     return NULL;
143   }
144
145   int acquireFence = -1;
146   gralloc1_rect_t rect{};
147   rect.left = x;
148   rect.top = y;
149   rect.width = width;
150   rect.height = height;
151
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;
157 }
158
159 int32_t Gralloc1BufferHandler::UnMap(HWCNativeHandle handle,
160                                   void * /*map_data*/) {
161   auto gr_handle = (struct cros_gralloc_handle *)handle->imported_handle_;
162   if (!gr_handle) {
163     ETRACE("could not find gralloc drm handle");
164     return GRALLOC1_ERROR_BAD_HANDLE;
165   }
166
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);
171 }
172
173 }  // namespace hwcomposer