OSDN Git Service

Add Render Buffer Compression support in HWComposer.
[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 #ifdef USE_GRALLOC1
106   set_modifier_ = reinterpret_cast<GRALLOC1_PFN_SET_MODIFIER>(
107       gralloc1_dvc->getFunction(gralloc1_dvc, GRALLOC1_FUNCTION_SET_MODIFIER));
108 #endif
109   return true;
110 }
111
112 bool Gralloc1BufferHandler::CreateBuffer(uint32_t w, uint32_t h, int format,
113                                          HWCNativeHandle *handle,
114                                          uint32_t layer_type) const {
115   struct gralloc_handle *temp = new struct gralloc_handle();
116   gralloc1_device_t *gralloc1_dvc =
117       reinterpret_cast<gralloc1_device_t *>(device_);
118
119   create_descriptor_(gralloc1_dvc, &temp->gralloc1_buffer_descriptor_t_);
120   uint32_t usage = 0;
121   uint32_t pixel_format = 0;
122   bool force_normal_usage = false;
123   if (format != 0) {
124     pixel_format = DrmFormatToHALFormat(format);
125   }
126
127   if (pixel_format == 0) {
128     pixel_format = HAL_PIXEL_FORMAT_RGBA_8888;
129   }
130
131   set_format_(gralloc1_dvc, temp->gralloc1_buffer_descriptor_t_, pixel_format);
132
133 #ifdef ENABLE_RBC
134   // These formats are RBC supported in i915 driver
135   if ((format == DRM_FORMAT_XRGB8888) || (format == DRM_FORMAT_XBGR8888) ||
136       (format == DRM_FORMAT_ARGB8888) || (format == DRM_FORMAT_ABGR8888)) {
137 #ifdef USE_GRALLOC1
138     uint64_t modifier = I915_FORMAT_MOD_Y_TILED_CCS;
139     set_modifier_(gralloc1_dvc, temp->gralloc1_buffer_descriptor_t_, modifier);
140 #endif
141   }
142 #endif
143
144   if ((layer_type == hwcomposer::kLayerVideo) &&
145       !IsSupportedMediaFormat(format)) {
146     ETRACE("Forcing normal usage for Video Layer. \n");
147     force_normal_usage = true;
148   }
149
150   if ((layer_type == hwcomposer::kLayerNormal) || force_normal_usage) {
151     usage |= GRALLOC1_CONSUMER_USAGE_HWCOMPOSER |
152              GRALLOC1_PRODUCER_USAGE_GPU_RENDER_TARGET |
153              GRALLOC1_CONSUMER_USAGE_GPU_TEXTURE;
154   } else if (layer_type == hwcomposer::kLayerVideo) {
155     switch (pixel_format) {
156       case HAL_PIXEL_FORMAT_YCbCr_422_I:
157       case HAL_PIXEL_FORMAT_Y8:
158         usage |= GRALLOC1_CONSUMER_USAGE_GPU_TEXTURE |
159                  GRALLOC1_PRODUCER_USAGE_VIDEO_DECODER;
160         break;
161       default:
162         usage |= GRALLOC1_PRODUCER_USAGE_CAMERA |
163                  GRALLOC1_CONSUMER_USAGE_CAMERA |
164                  GRALLOC1_PRODUCER_USAGE_VIDEO_DECODER |
165                  GRALLOC1_CONSUMER_USAGE_GPU_TEXTURE;
166     }
167   } else if (layer_type == hwcomposer::kLayerCursor) {
168     usage |= GRALLOC1_CONSUMER_USAGE_CURSOR;
169   }
170
171   set_consumer_usage_(gralloc1_dvc, temp->gralloc1_buffer_descriptor_t_, usage);
172   set_producer_usage_(gralloc1_dvc, temp->gralloc1_buffer_descriptor_t_, usage);
173   set_dimensions_(gralloc1_dvc, temp->gralloc1_buffer_descriptor_t_, w, h);
174   allocate_(gralloc1_dvc, 1, &temp->gralloc1_buffer_descriptor_t_,
175             &temp->handle_);
176
177   if (!temp->handle_) {
178     ETRACE("Failed to allocate buffer \n");
179   }
180
181   temp->hwc_buffer_ = true;
182   *handle = temp;
183
184   return true;
185 }
186
187 bool Gralloc1BufferHandler::ReleaseBuffer(HWCNativeHandle handle) const {
188   gralloc1_device_t *gralloc1_dvc =
189       reinterpret_cast<gralloc1_device_t *>(device_);
190
191   if (handle->hwc_buffer_) {
192     release_(gralloc1_dvc, handle->handle_);
193   } else if (handle->imported_handle_) {
194     release_(gralloc1_dvc, handle->imported_handle_);
195   }
196
197   if (handle->gralloc1_buffer_descriptor_t_ > 0)
198     destroy_descriptor_(gralloc1_dvc, handle->gralloc1_buffer_descriptor_t_);
199
200   return true;
201 }
202
203 void Gralloc1BufferHandler::DestroyHandle(HWCNativeHandle handle) const {
204   DestroyBufferHandle(handle);
205 }
206
207 bool Gralloc1BufferHandler::ImportBuffer(HWCNativeHandle handle) const {
208   if (!handle->imported_handle_) {
209     ETRACE("could not find gralloc drm handle");
210     return false;
211   }
212
213   gralloc1_device_t *gralloc1_dvc =
214       reinterpret_cast<gralloc1_device_t *>(device_);
215   register_(gralloc1_dvc, handle->imported_handle_);
216   if (!ImportGraphicsBuffer(handle, fd_)) {
217     return false;
218   }
219
220   return true;
221 }
222
223 uint32_t Gralloc1BufferHandler::GetTotalPlanes(HWCNativeHandle handle) const {
224   return handle->meta_data_.num_planes_;
225 }
226
227 void Gralloc1BufferHandler::CopyHandle(HWCNativeHandle source,
228                                        HWCNativeHandle *target) const {
229   CopyBufferHandle(source, target);
230 }
231
232 void *Gralloc1BufferHandler::Map(HWCNativeHandle handle, uint32_t x, uint32_t y,
233                                  uint32_t width, uint32_t height,
234                                  uint32_t * /*stride*/, void **map_data,
235                                  size_t /*plane*/) const {
236   auto gr_handle = (struct cros_gralloc_handle *)handle->imported_handle_;
237   if (!gr_handle) {
238     ETRACE("could not find gralloc drm handle");
239     return NULL;
240   }
241
242   int acquireFence = -1;
243   gralloc1_rect_t rect{};
244   rect.left = x;
245   rect.top = y;
246   rect.width = width;
247   rect.height = height;
248
249   gralloc1_device_t *gralloc1_dvc =
250       reinterpret_cast<gralloc1_device_t *>(device_);
251   uint32_t status = lock_(gralloc1_dvc, handle->imported_handle_,
252                           GRALLOC1_PRODUCER_USAGE_CPU_WRITE_OFTEN,
253                           GRALLOC1_CONSUMER_USAGE_CPU_READ_OFTEN, &rect,
254                           map_data, acquireFence);
255   return (GRALLOC1_ERROR_NONE == status) ? *map_data : NULL;
256 }
257
258 int32_t Gralloc1BufferHandler::UnMap(HWCNativeHandle handle,
259                                      void * /*map_data*/) const {
260   auto gr_handle = (struct cros_gralloc_handle *)handle->imported_handle_;
261   if (!gr_handle) {
262     ETRACE("could not find gralloc drm handle");
263     return GRALLOC1_ERROR_BAD_HANDLE;
264   }
265
266   int releaseFence = 0;
267   gralloc1_device_t *gralloc1_dvc =
268       reinterpret_cast<gralloc1_device_t *>(device_);
269   return unlock_(gralloc1_dvc, handle->imported_handle_, &releaseFence);
270 }
271
272 }  // namespace hwcomposer