OSDN Git Service

Track GemHandles and FB separately.
[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 "hwcutils.h"
29 #include "utils_android.h"
30
31 namespace hwcomposer {
32
33 // static
34 NativeBufferHandler *NativeBufferHandler::CreateInstance(uint32_t fd) {
35   Gralloc1BufferHandler *handler = new Gralloc1BufferHandler(fd);
36   if (!handler)
37     return NULL;
38
39   if (!handler->Init()) {
40     ETRACE("Failed to initialize GralocBufferHandlers.");
41     delete handler;
42     return NULL;
43   }
44   return handler;
45 }
46
47 Gralloc1BufferHandler::Gralloc1BufferHandler(uint32_t fd) : fd_(fd) {
48 }
49
50 Gralloc1BufferHandler::~Gralloc1BufferHandler() {
51   gralloc1_device_t *gralloc1_dvc =
52       reinterpret_cast<gralloc1_device_t *>(device_);
53   gralloc1_dvc->common.close(device_);
54 }
55
56 bool Gralloc1BufferHandler::Init() {
57   int ret = hw_get_module(GRALLOC_HARDWARE_MODULE_ID,
58                           (const hw_module_t **)&gralloc_);
59   if (ret) {
60     ETRACE("Failed to get gralloc module");
61     return false;
62   }
63
64   ret = gralloc_->methods->open(gralloc_, GRALLOC_HARDWARE_MODULE_ID, &device_);
65   if (ret) {
66     ETRACE("Failed to open gralloc module");
67     return false;
68   }
69
70   gralloc1_device_t *gralloc1_dvc =
71       reinterpret_cast<gralloc1_device_t *>(device_);
72   register_ = reinterpret_cast<GRALLOC1_PFN_RETAIN>(
73       gralloc1_dvc->getFunction(gralloc1_dvc, GRALLOC1_FUNCTION_RETAIN));
74   release_ = reinterpret_cast<GRALLOC1_PFN_RELEASE>(
75       gralloc1_dvc->getFunction(gralloc1_dvc, GRALLOC1_FUNCTION_RELEASE));
76   lock_ = reinterpret_cast<GRALLOC1_PFN_LOCK>(
77       gralloc1_dvc->getFunction(gralloc1_dvc, GRALLOC1_FUNCTION_LOCK));
78   unlock_ = reinterpret_cast<GRALLOC1_PFN_UNLOCK>(
79       gralloc1_dvc->getFunction(gralloc1_dvc, GRALLOC1_FUNCTION_UNLOCK));
80
81   dimensions_ =
82       reinterpret_cast<GRALLOC1_PFN_GET_DIMENSIONS>(gralloc1_dvc->getFunction(
83           gralloc1_dvc, GRALLOC1_FUNCTION_GET_DIMENSIONS));
84
85   create_descriptor_ = reinterpret_cast<GRALLOC1_PFN_CREATE_DESCRIPTOR>(
86       gralloc1_dvc->getFunction(gralloc1_dvc,
87                                 GRALLOC1_FUNCTION_CREATE_DESCRIPTOR));
88   destroy_descriptor_ = reinterpret_cast<GRALLOC1_PFN_DESTROY_DESCRIPTOR>(
89       gralloc1_dvc->getFunction(gralloc1_dvc,
90                                 GRALLOC1_FUNCTION_DESTROY_DESCRIPTOR));
91
92   set_consumer_usage_ = reinterpret_cast<GRALLOC1_PFN_SET_CONSUMER_USAGE>(
93       gralloc1_dvc->getFunction(gralloc1_dvc,
94                                 GRALLOC1_FUNCTION_SET_CONSUMER_USAGE));
95   set_dimensions_ =
96       reinterpret_cast<GRALLOC1_PFN_SET_DIMENSIONS>(gralloc1_dvc->getFunction(
97           gralloc1_dvc, GRALLOC1_FUNCTION_SET_DIMENSIONS));
98   set_format_ = reinterpret_cast<GRALLOC1_PFN_SET_FORMAT>(
99       gralloc1_dvc->getFunction(gralloc1_dvc, GRALLOC1_FUNCTION_SET_FORMAT));
100   set_producer_usage_ = reinterpret_cast<GRALLOC1_PFN_SET_PRODUCER_USAGE>(
101       gralloc1_dvc->getFunction(gralloc1_dvc,
102                                 GRALLOC1_FUNCTION_SET_PRODUCER_USAGE));
103   allocate_ = reinterpret_cast<GRALLOC1_PFN_ALLOCATE>(
104       gralloc1_dvc->getFunction(gralloc1_dvc, GRALLOC1_FUNCTION_ALLOCATE));
105
106   return true;
107 }
108
109 bool Gralloc1BufferHandler::CreateBuffer(uint32_t w, uint32_t h, int format,
110                                          HWCNativeHandle *handle,
111                                          uint32_t layer_type) const {
112   struct gralloc_handle *temp = new struct gralloc_handle();
113   gralloc1_device_t *gralloc1_dvc =
114       reinterpret_cast<gralloc1_device_t *>(device_);
115
116   create_descriptor_(gralloc1_dvc, &temp->gralloc1_buffer_descriptor_t_);
117   uint32_t usage = 0;
118   uint32_t pixel_format = 0;
119   bool force_normal_usage = false;
120   if (format != 0) {
121     pixel_format = DrmFormatToHALFormat(format);
122   }
123
124   if (pixel_format == 0) {
125     pixel_format = HAL_PIXEL_FORMAT_RGBA_8888;
126   }
127
128   set_format_(gralloc1_dvc, temp->gralloc1_buffer_descriptor_t_, pixel_format);
129
130   if ((layer_type == hwcomposer::kLayerVideo) &&
131       !IsSupportedMediaFormat(format)) {
132     ETRACE("Forcing normal usage for Video Layer. \n");
133     force_normal_usage = true;
134   }
135
136   if ((layer_type == hwcomposer::kLayerNormal) || force_normal_usage) {
137     usage |= GRALLOC1_CONSUMER_USAGE_HWCOMPOSER |
138              GRALLOC1_PRODUCER_USAGE_GPU_RENDER_TARGET |
139              GRALLOC1_CONSUMER_USAGE_GPU_TEXTURE;
140   } else if (layer_type == hwcomposer::kLayerVideo) {
141     switch (pixel_format) {
142       case HAL_PIXEL_FORMAT_YCbCr_422_I:
143       case HAL_PIXEL_FORMAT_Y8:
144         usage |= GRALLOC1_CONSUMER_USAGE_GPU_TEXTURE |
145                  GRALLOC1_PRODUCER_USAGE_VIDEO_DECODER;
146         break;
147       default:
148         usage |= GRALLOC1_PRODUCER_USAGE_CAMERA |
149                  GRALLOC1_CONSUMER_USAGE_CAMERA |
150                  GRALLOC1_PRODUCER_USAGE_VIDEO_DECODER |
151                  GRALLOC1_CONSUMER_USAGE_GPU_TEXTURE;
152     }
153   } else if (layer_type == hwcomposer::kLayerCursor) {
154     usage |= GRALLOC1_CONSUMER_USAGE_CURSOR;
155   }
156
157   set_consumer_usage_(gralloc1_dvc, temp->gralloc1_buffer_descriptor_t_, usage);
158   set_producer_usage_(gralloc1_dvc, temp->gralloc1_buffer_descriptor_t_, usage);
159   set_dimensions_(gralloc1_dvc, temp->gralloc1_buffer_descriptor_t_, w, h);
160   allocate_(gralloc1_dvc, 1, &temp->gralloc1_buffer_descriptor_t_,
161             &temp->handle_);
162
163   if (!temp->handle_) {
164     ETRACE("Failed to allocate buffer \n");
165   }
166
167   temp->hwc_buffer_ = true;
168   *handle = temp;
169
170   return true;
171 }
172
173 bool Gralloc1BufferHandler::ReleaseBuffer(HWCNativeHandle handle) const {
174   gralloc1_device_t *gralloc1_dvc =
175       reinterpret_cast<gralloc1_device_t *>(device_);
176
177   if (handle->hwc_buffer_) {
178     release_(gralloc1_dvc, handle->handle_);
179   } else if (handle->imported_handle_) {
180     release_(gralloc1_dvc, handle->imported_handle_);
181   }
182
183   if (handle->gralloc1_buffer_descriptor_t_ > 0)
184     destroy_descriptor_(gralloc1_dvc, handle->gralloc1_buffer_descriptor_t_);
185
186   return true;
187 }
188
189 void Gralloc1BufferHandler::DestroyHandle(HWCNativeHandle handle) const {
190   DestroyBufferHandle(handle);
191 }
192
193 bool Gralloc1BufferHandler::ImportBuffer(HWCNativeHandle handle) const {
194   if (!handle->imported_handle_) {
195     ETRACE("could not find gralloc drm handle");
196     return false;
197   }
198
199   gralloc1_device_t *gralloc1_dvc =
200       reinterpret_cast<gralloc1_device_t *>(device_);
201   register_(gralloc1_dvc, handle->imported_handle_);
202   return ImportGraphicsBuffer(handle, fd_);
203 }
204
205 uint32_t Gralloc1BufferHandler::GetTotalPlanes(HWCNativeHandle handle) const {
206   auto gr_handle = (struct cros_gralloc_handle *)handle->imported_handle_;
207   if (!gr_handle) {
208     ETRACE("could not find gralloc drm handle");
209     return false;
210   }
211
212   return drm_bo_get_num_planes(gr_handle->format);
213 }
214
215 void Gralloc1BufferHandler::CopyHandle(HWCNativeHandle source,
216                                        HWCNativeHandle *target) const {
217   CopyBufferHandle(source, target);
218 }
219
220 void *Gralloc1BufferHandler::Map(HWCNativeHandle handle, uint32_t x, uint32_t y,
221                                  uint32_t width, uint32_t height,
222                                  uint32_t * /*stride*/, void **map_data,
223                                  size_t /*plane*/) const {
224   auto gr_handle = (struct cros_gralloc_handle *)handle->imported_handle_;
225   if (!gr_handle) {
226     ETRACE("could not find gralloc drm handle");
227     return NULL;
228   }
229
230   int acquireFence = -1;
231   gralloc1_rect_t rect{};
232   rect.left = x;
233   rect.top = y;
234   rect.width = width;
235   rect.height = height;
236
237   gralloc1_device_t *gralloc1_dvc =
238       reinterpret_cast<gralloc1_device_t *>(device_);
239   uint32_t status = lock_(gralloc1_dvc, handle->imported_handle_,
240                           GRALLOC1_PRODUCER_USAGE_CPU_WRITE_OFTEN,
241                           GRALLOC1_CONSUMER_USAGE_CPU_READ_OFTEN, &rect,
242                           map_data, acquireFence);
243   return (GRALLOC1_ERROR_NONE == status) ? *map_data : NULL;
244 }
245
246 int32_t Gralloc1BufferHandler::UnMap(HWCNativeHandle handle,
247                                      void * /*map_data*/) const {
248   auto gr_handle = (struct cros_gralloc_handle *)handle->imported_handle_;
249   if (!gr_handle) {
250     ETRACE("could not find gralloc drm handle");
251     return GRALLOC1_ERROR_BAD_HANDLE;
252   }
253
254   int releaseFence = 0;
255   gralloc1_device_t *gralloc1_dvc =
256       reinterpret_cast<gralloc1_device_t *>(device_);
257   return unlock_(gralloc1_dvc, handle->imported_handle_, &releaseFence);
258 }
259
260 }  // namespace hwcomposer