blob: f03a30730bcca11d4df40426c220b303632fd7bb [file] [log] [blame]
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001/*
2 * Copyright (C) 2007 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
Mathias Agopian3330b202009-10-05 17:07:12 -070017#define LOG_TAG "GraphicBufferMapper"
Mathias Agopiancf563192012-02-29 20:43:29 -080018#define ATRACE_TAG ATRACE_TAG_GRAPHICS
Dan Stoza8deb4da2016-06-01 18:21:44 -070019//#define LOG_NDEBUG 0
Mathias Agopian076b1cc2009-04-10 14:24:30 -070020
21#include <stdint.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070022#include <errno.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070023
Dan Stozad3182402014-11-17 12:03:59 -080024// We would eliminate the non-conforming zero-length array, but we can't since
25// this is effectively included from the Linux kernel
26#pragma clang diagnostic push
27#pragma clang diagnostic ignored "-Wzero-length-array"
Francis Hart8f396012014-04-01 15:30:53 +030028#include <sync/sync.h>
Dan Stozad3182402014-11-17 12:03:59 -080029#pragma clang diagnostic pop
Francis Hart8f396012014-04-01 15:30:53 +030030
Mathias Agopian076b1cc2009-04-10 14:24:30 -070031#include <utils/Errors.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070032#include <utils/Log.h>
Mathias Agopiancf563192012-02-29 20:43:29 -080033#include <utils/Trace.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070034
Dan Stoza8deb4da2016-06-01 18:21:44 -070035#include <ui/Gralloc1On0Adapter.h>
Chia-I Wu9ba189d2016-09-22 17:13:08 +080036#include <ui/GrallocMapper.h>
Mathias Agopian3330b202009-10-05 17:07:12 -070037#include <ui/GraphicBufferMapper.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070038#include <ui/Rect.h>
39
Dan Stoza8deb4da2016-06-01 18:21:44 -070040#include <system/graphics.h>
Mathias Agopian8b765b72009-04-10 20:34:46 -070041
Mathias Agopian076b1cc2009-04-10 14:24:30 -070042namespace android {
43// ---------------------------------------------------------------------------
44
Mathias Agopian3330b202009-10-05 17:07:12 -070045ANDROID_SINGLETON_STATIC_INSTANCE( GraphicBufferMapper )
Mathias Agopian4243e662009-04-15 18:34:24 -070046
Mathias Agopian3330b202009-10-05 17:07:12 -070047GraphicBufferMapper::GraphicBufferMapper()
Chia-I Wu9ba189d2016-09-22 17:13:08 +080048 : mMapper(std::make_unique<const Gralloc2::Mapper>())
49{
50 if (!mMapper->valid()) {
51 mLoader = std::make_unique<Gralloc1::Loader>();
52 mDevice = mLoader->getDevice();
53 }
54}
Dan Stoza8deb4da2016-06-01 18:21:44 -070055
56
Mathias Agopian076b1cc2009-04-10 14:24:30 -070057
Mathias Agopian3330b202009-10-05 17:07:12 -070058status_t GraphicBufferMapper::registerBuffer(buffer_handle_t handle)
Mathias Agopian076b1cc2009-04-10 14:24:30 -070059{
Mathias Agopiancf563192012-02-29 20:43:29 -080060 ATRACE_CALL();
Mathias Agopian0a757812010-12-08 16:40:01 -080061
Chia-I Wu9ba189d2016-09-22 17:13:08 +080062 gralloc1_error_t error;
63 if (mMapper->valid()) {
64 error = static_cast<gralloc1_error_t>(mMapper->retain(handle));
65 } else {
66 error = mDevice->retain(handle);
67 }
68
Dan Stoza8deb4da2016-06-01 18:21:44 -070069 ALOGW_IF(error != GRALLOC1_ERROR_NONE, "registerBuffer(%p) failed: %d",
70 handle, error);
Mathias Agopian0a757812010-12-08 16:40:01 -080071
Dan Stoza8deb4da2016-06-01 18:21:44 -070072 return error;
73}
74
75status_t GraphicBufferMapper::registerBuffer(const GraphicBuffer* buffer)
76{
77 ATRACE_CALL();
78
Chia-I Wu9ba189d2016-09-22 17:13:08 +080079 gralloc1_error_t error;
80 if (mMapper->valid()) {
81 error = static_cast<gralloc1_error_t>(
82 mMapper->retain(buffer->getNativeBuffer()->handle));
83 } else {
84 error = mDevice->retain(buffer);
85 }
86
Dan Stoza8deb4da2016-06-01 18:21:44 -070087 ALOGW_IF(error != GRALLOC1_ERROR_NONE, "registerBuffer(%p) failed: %d",
88 buffer->getNativeBuffer()->handle, error);
89
90 return error;
Mathias Agopian076b1cc2009-04-10 14:24:30 -070091}
92
Mathias Agopian3330b202009-10-05 17:07:12 -070093status_t GraphicBufferMapper::unregisterBuffer(buffer_handle_t handle)
Mathias Agopian076b1cc2009-04-10 14:24:30 -070094{
Mathias Agopiancf563192012-02-29 20:43:29 -080095 ATRACE_CALL();
Mathias Agopian0a757812010-12-08 16:40:01 -080096
Chia-I Wu9ba189d2016-09-22 17:13:08 +080097 gralloc1_error_t error;
98 if (mMapper->valid()) {
99 mMapper->release(handle);
100 error = GRALLOC1_ERROR_NONE;
101 } else {
102 error = mDevice->release(handle);
103 }
104
Dan Stoza8deb4da2016-06-01 18:21:44 -0700105 ALOGW_IF(error != GRALLOC1_ERROR_NONE, "unregisterBuffer(%p): failed %d",
106 handle, error);
Mathias Agopian0a757812010-12-08 16:40:01 -0800107
Dan Stoza8deb4da2016-06-01 18:21:44 -0700108 return error;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700109}
110
Dan Stoza8deb4da2016-06-01 18:21:44 -0700111static inline gralloc1_rect_t asGralloc1Rect(const Rect& rect) {
112 gralloc1_rect_t outRect{};
113 outRect.left = rect.left;
114 outRect.top = rect.top;
115 outRect.width = rect.width();
116 outRect.height = rect.height();
117 return outRect;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700118}
119
Dan Stoza8deb4da2016-06-01 18:21:44 -0700120status_t GraphicBufferMapper::lock(buffer_handle_t handle, uint32_t usage,
121 const Rect& bounds, void** vaddr)
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700122{
Dan Stoza8deb4da2016-06-01 18:21:44 -0700123 return lockAsync(handle, usage, bounds, vaddr, -1);
124}
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700125
Dan Stoza8deb4da2016-06-01 18:21:44 -0700126status_t GraphicBufferMapper::lockYCbCr(buffer_handle_t handle, uint32_t usage,
127 const Rect& bounds, android_ycbcr *ycbcr)
128{
129 return lockAsyncYCbCr(handle, usage, bounds, ycbcr, -1);
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700130}
131
Mathias Agopian3330b202009-10-05 17:07:12 -0700132status_t GraphicBufferMapper::unlock(buffer_handle_t handle)
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700133{
Dan Stoza8deb4da2016-06-01 18:21:44 -0700134 int32_t fenceFd = -1;
135 status_t error = unlockAsync(handle, &fenceFd);
136 if (error == NO_ERROR) {
137 sync_wait(fenceFd, -1);
138 close(fenceFd);
139 }
140 return error;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700141}
142
Francis Hart8f396012014-04-01 15:30:53 +0300143status_t GraphicBufferMapper::lockAsync(buffer_handle_t handle,
Dan Stozad3182402014-11-17 12:03:59 -0800144 uint32_t usage, const Rect& bounds, void** vaddr, int fenceFd)
Francis Hart8f396012014-04-01 15:30:53 +0300145{
Craig Donnere96a3252017-02-02 12:13:34 -0800146 return lockAsync(handle, usage, usage, bounds, vaddr, fenceFd);
147}
148
149status_t GraphicBufferMapper::lockAsync(buffer_handle_t handle,
150 uint64_t producerUsage, uint64_t consumerUsage, const Rect& bounds,
151 void** vaddr, int fenceFd)
152{
Francis Hart8f396012014-04-01 15:30:53 +0300153 ATRACE_CALL();
Francis Hart8f396012014-04-01 15:30:53 +0300154
Dan Stoza8deb4da2016-06-01 18:21:44 -0700155 gralloc1_rect_t accessRegion = asGralloc1Rect(bounds);
Chia-I Wu9ba189d2016-09-22 17:13:08 +0800156 gralloc1_error_t error;
157 if (mMapper->valid()) {
Chia-I Wu31669472016-12-07 14:55:24 +0800158 const Gralloc2::IMapper::Rect& accessRect =
159 *reinterpret_cast<Gralloc2::IMapper::Rect*>(&accessRegion);
Chia-I Wu9ba189d2016-09-22 17:13:08 +0800160 error = static_cast<gralloc1_error_t>(mMapper->lock(
Craig Donnere96a3252017-02-02 12:13:34 -0800161 handle, producerUsage, consumerUsage, accessRect,
162 fenceFd, vaddr));
Chia-I Wu9ba189d2016-09-22 17:13:08 +0800163 } else {
164 sp<Fence> fence = new Fence(fenceFd);
165 error = mDevice->lock(handle,
Craig Donnere96a3252017-02-02 12:13:34 -0800166 static_cast<gralloc1_producer_usage_t>(producerUsage),
167 static_cast<gralloc1_consumer_usage_t>(consumerUsage),
Chia-I Wu9ba189d2016-09-22 17:13:08 +0800168 &accessRegion, vaddr, fence);
169 }
170
Dan Stoza8deb4da2016-06-01 18:21:44 -0700171 ALOGW_IF(error != GRALLOC1_ERROR_NONE, "lock(%p, ...) failed: %d", handle,
172 error);
173
174 return error;
175}
176
177static inline bool isValidYCbCrPlane(const android_flex_plane_t& plane) {
178 if (plane.bits_per_component != 8) {
179 ALOGV("Invalid number of bits per component: %d",
180 plane.bits_per_component);
181 return false;
182 }
183 if (plane.bits_used != 8) {
184 ALOGV("Invalid number of bits used: %d", plane.bits_used);
185 return false;
Francis Hart8f396012014-04-01 15:30:53 +0300186 }
187
Dan Stoza8deb4da2016-06-01 18:21:44 -0700188 bool hasValidIncrement = plane.h_increment == 1 ||
189 (plane.component != FLEX_COMPONENT_Y && plane.h_increment == 2);
190 hasValidIncrement = hasValidIncrement && plane.v_increment > 0;
191 if (!hasValidIncrement) {
192 ALOGV("Invalid increment: h %d v %d", plane.h_increment,
193 plane.v_increment);
194 return false;
195 }
196
197 return true;
Francis Hart8f396012014-04-01 15:30:53 +0300198}
199
200status_t GraphicBufferMapper::lockAsyncYCbCr(buffer_handle_t handle,
Dan Stozad3182402014-11-17 12:03:59 -0800201 uint32_t usage, const Rect& bounds, android_ycbcr *ycbcr, int fenceFd)
Francis Hart8f396012014-04-01 15:30:53 +0300202{
203 ATRACE_CALL();
Francis Hart8f396012014-04-01 15:30:53 +0300204
Dan Stoza8deb4da2016-06-01 18:21:44 -0700205 gralloc1_rect_t accessRegion = asGralloc1Rect(bounds);
Dan Stoza8deb4da2016-06-01 18:21:44 -0700206
Chia-I Wu31669472016-12-07 14:55:24 +0800207 std::vector<android_flex_plane_t> planes;
208 android_flex_layout_t flexLayout{};
209 gralloc1_error_t error;
210
211 if (mMapper->valid()) {
212 const Gralloc2::IMapper::Rect& accessRect =
213 *reinterpret_cast<Gralloc2::IMapper::Rect*>(&accessRegion);
214 Gralloc2::FlexLayout layout{};
215 error = static_cast<gralloc1_error_t>(mMapper->lock(
216 handle, usage, usage, accessRect, fenceFd, &layout));
217
218 if (error == GRALLOC1_ERROR_NONE) {
219 planes.resize(layout.planes.size());
220 memcpy(planes.data(), layout.planes.data(),
221 sizeof(planes[0]) * planes.size());
222
223 flexLayout.format = static_cast<android_flex_format_t>(
224 layout.format);
225 flexLayout.num_planes = static_cast<uint32_t>(planes.size());
226 flexLayout.planes = planes.data();
227 }
228 } else {
229 sp<Fence> fence = new Fence(fenceFd);
230
Chia-I Wu9ba189d2016-09-22 17:13:08 +0800231 if (mDevice->hasCapability(GRALLOC1_CAPABILITY_ON_ADAPTER)) {
Chia-I Wu31669472016-12-07 14:55:24 +0800232 error = mDevice->lockYCbCr(handle,
Chia-I Wu9ba189d2016-09-22 17:13:08 +0800233 static_cast<gralloc1_producer_usage_t>(usage),
234 static_cast<gralloc1_consumer_usage_t>(usage),
235 &accessRegion, ycbcr, fence);
236 ALOGW_IF(error != GRALLOC1_ERROR_NONE,
237 "lockYCbCr(%p, ...) failed: %d", handle, error);
238 return error;
239 }
Francis Hart8f396012014-04-01 15:30:53 +0300240
Chia-I Wu31669472016-12-07 14:55:24 +0800241 uint32_t numPlanes = 0;
Chia-I Wu9ba189d2016-09-22 17:13:08 +0800242 error = mDevice->getNumFlexPlanes(handle, &numPlanes);
Chia-I Wu9ba189d2016-09-22 17:13:08 +0800243
Chia-I Wu31669472016-12-07 14:55:24 +0800244 if (error != GRALLOC1_ERROR_NONE) {
245 ALOGV("Failed to retrieve number of flex planes: %d", error);
246 return error;
247 }
248 if (numPlanes < 3) {
249 ALOGV("Not enough planes for YCbCr (%u found)", numPlanes);
250 return GRALLOC1_ERROR_UNSUPPORTED;
251 }
Dan Stoza8deb4da2016-06-01 18:21:44 -0700252
Chia-I Wu31669472016-12-07 14:55:24 +0800253 planes.resize(numPlanes);
254 flexLayout.num_planes = numPlanes;
255 flexLayout.planes = planes.data();
Dan Stoza8deb4da2016-06-01 18:21:44 -0700256
Chia-I Wu9ba189d2016-09-22 17:13:08 +0800257 error = mDevice->lockFlex(handle,
258 static_cast<gralloc1_producer_usage_t>(usage),
259 static_cast<gralloc1_consumer_usage_t>(usage),
260 &accessRegion, &flexLayout, fence);
261 }
262
Dan Stoza8deb4da2016-06-01 18:21:44 -0700263 if (error != GRALLOC1_ERROR_NONE) {
264 ALOGW("lockFlex(%p, ...) failed: %d", handle, error);
265 return error;
266 }
267 if (flexLayout.format != FLEX_FORMAT_YCbCr) {
268 ALOGV("Unable to convert flex-format buffer to YCbCr");
269 unlock(handle);
270 return GRALLOC1_ERROR_UNSUPPORTED;
271 }
272
273 // Find planes
274 auto yPlane = planes.cend();
275 auto cbPlane = planes.cend();
276 auto crPlane = planes.cend();
277 for (auto planeIter = planes.cbegin(); planeIter != planes.cend();
278 ++planeIter) {
279 if (planeIter->component == FLEX_COMPONENT_Y) {
280 yPlane = planeIter;
281 } else if (planeIter->component == FLEX_COMPONENT_Cb) {
282 cbPlane = planeIter;
283 } else if (planeIter->component == FLEX_COMPONENT_Cr) {
284 crPlane = planeIter;
285 }
286 }
287 if (yPlane == planes.cend()) {
288 ALOGV("Unable to find Y plane");
289 unlock(handle);
290 return GRALLOC1_ERROR_UNSUPPORTED;
291 }
292 if (cbPlane == planes.cend()) {
293 ALOGV("Unable to find Cb plane");
294 unlock(handle);
295 return GRALLOC1_ERROR_UNSUPPORTED;
296 }
297 if (crPlane == planes.cend()) {
298 ALOGV("Unable to find Cr plane");
299 unlock(handle);
300 return GRALLOC1_ERROR_UNSUPPORTED;
301 }
302
303 // Validate planes
304 if (!isValidYCbCrPlane(*yPlane)) {
305 ALOGV("Y plane is invalid");
306 unlock(handle);
307 return GRALLOC1_ERROR_UNSUPPORTED;
308 }
309 if (!isValidYCbCrPlane(*cbPlane)) {
310 ALOGV("Cb plane is invalid");
311 unlock(handle);
312 return GRALLOC1_ERROR_UNSUPPORTED;
313 }
314 if (!isValidYCbCrPlane(*crPlane)) {
315 ALOGV("Cr plane is invalid");
316 unlock(handle);
317 return GRALLOC1_ERROR_UNSUPPORTED;
318 }
319 if (cbPlane->v_increment != crPlane->v_increment) {
320 ALOGV("Cb and Cr planes have different step (%d vs. %d)",
321 cbPlane->v_increment, crPlane->v_increment);
322 unlock(handle);
323 return GRALLOC1_ERROR_UNSUPPORTED;
324 }
325 if (cbPlane->h_increment != crPlane->h_increment) {
326 ALOGV("Cb and Cr planes have different stride (%d vs. %d)",
327 cbPlane->h_increment, crPlane->h_increment);
328 unlock(handle);
329 return GRALLOC1_ERROR_UNSUPPORTED;
330 }
331
332 // Pack plane data into android_ycbcr struct
333 ycbcr->y = yPlane->top_left;
334 ycbcr->cb = cbPlane->top_left;
335 ycbcr->cr = crPlane->top_left;
336 ycbcr->ystride = static_cast<size_t>(yPlane->v_increment);
337 ycbcr->cstride = static_cast<size_t>(cbPlane->v_increment);
338 ycbcr->chroma_step = static_cast<size_t>(cbPlane->h_increment);
339
340 return error;
Francis Hart8f396012014-04-01 15:30:53 +0300341}
342
343status_t GraphicBufferMapper::unlockAsync(buffer_handle_t handle, int *fenceFd)
344{
345 ATRACE_CALL();
Francis Hart8f396012014-04-01 15:30:53 +0300346
Chia-I Wu9ba189d2016-09-22 17:13:08 +0800347 gralloc1_error_t error;
348 if (mMapper->valid()) {
349 *fenceFd = mMapper->unlock(handle);
350 error = GRALLOC1_ERROR_NONE;
351 } else {
352 sp<Fence> fence = Fence::NO_FENCE;
353 error = mDevice->unlock(handle, &fence);
354 if (error != GRALLOC1_ERROR_NONE) {
355 ALOGE("unlock(%p) failed: %d", handle, error);
356 return error;
357 }
Francis Hart8f396012014-04-01 15:30:53 +0300358
Chia-I Wu9ba189d2016-09-22 17:13:08 +0800359 *fenceFd = fence->dup();
360 }
Dan Stoza8deb4da2016-06-01 18:21:44 -0700361 return error;
Francis Hart8f396012014-04-01 15:30:53 +0300362}
363
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700364// ---------------------------------------------------------------------------
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700365}; // namespace android