blob: d52c508003eb2740aaf3b7afb75855caa2f481e9 [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
Mathias Agopianfe2f54f2017-02-15 19:48:58 -080021#include <ui/GraphicBufferMapper.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070022
Chia-I Wu5bac7f32017-04-06 12:34:32 -070023#include <grallocusage/GrallocUsageConversion.h>
24
Dan Stozad3182402014-11-17 12:03:59 -080025// We would eliminate the non-conforming zero-length array, but we can't since
26// this is effectively included from the Linux kernel
27#pragma clang diagnostic push
28#pragma clang diagnostic ignored "-Wzero-length-array"
Francis Hart8f396012014-04-01 15:30:53 +030029#include <sync/sync.h>
Dan Stozad3182402014-11-17 12:03:59 -080030#pragma clang diagnostic pop
Francis Hart8f396012014-04-01 15:30:53 +030031
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
Chia-I Wu5bac7f32017-04-06 12:34:32 -070035#include <ui/Gralloc2.h>
Mathias Agopiana9347642017-02-13 16:42:28 -080036#include <ui/GraphicBuffer.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070037
Dan Stoza8deb4da2016-06-01 18:21:44 -070038#include <system/graphics.h>
Mathias Agopian8b765b72009-04-10 20:34:46 -070039
Mathias Agopian076b1cc2009-04-10 14:24:30 -070040namespace android {
41// ---------------------------------------------------------------------------
42
Mathias Agopian3330b202009-10-05 17:07:12 -070043ANDROID_SINGLETON_STATIC_INSTANCE( GraphicBufferMapper )
Mathias Agopian4243e662009-04-15 18:34:24 -070044
Jesse Halleeb4ac72017-07-06 14:02:29 -070045void GraphicBufferMapper::preloadHal() {
46 Gralloc2::Mapper::preload();
47}
48
Mathias Agopian3330b202009-10-05 17:07:12 -070049GraphicBufferMapper::GraphicBufferMapper()
Chia-I Wu9ba189d2016-09-22 17:13:08 +080050 : mMapper(std::make_unique<const Gralloc2::Mapper>())
51{
Chia-I Wu9ba189d2016-09-22 17:13:08 +080052}
Dan Stoza8deb4da2016-06-01 18:21:44 -070053
Chia-I Wu5bac7f32017-04-06 12:34:32 -070054status_t GraphicBufferMapper::importBuffer(buffer_handle_t rawHandle,
55 buffer_handle_t* outHandle)
Mathias Agopian076b1cc2009-04-10 14:24:30 -070056{
Mathias Agopiancf563192012-02-29 20:43:29 -080057 ATRACE_CALL();
Mathias Agopian0a757812010-12-08 16:40:01 -080058
Chia-I Wucb8405e2017-04-17 15:20:19 -070059 Gralloc2::Error error = mMapper->importBuffer(
60 hardware::hidl_handle(rawHandle), outHandle);
Chia-I Wu5bac7f32017-04-06 12:34:32 -070061
62 ALOGW_IF(error != Gralloc2::Error::NONE, "importBuffer(%p) failed: %d",
63 rawHandle, error);
64
65 return static_cast<status_t>(error);
66}
67
Chia-I Wu5bac7f32017-04-06 12:34:32 -070068status_t GraphicBufferMapper::freeBuffer(buffer_handle_t handle)
Mathias Agopian076b1cc2009-04-10 14:24:30 -070069{
Mathias Agopiancf563192012-02-29 20:43:29 -080070 ATRACE_CALL();
Mathias Agopian0a757812010-12-08 16:40:01 -080071
Chia-I Wucb8405e2017-04-17 15:20:19 -070072 mMapper->freeBuffer(handle);
Chia-I Wu9ba189d2016-09-22 17:13:08 +080073
Chia-I Wucb8405e2017-04-17 15:20:19 -070074 return NO_ERROR;
Mathias Agopian076b1cc2009-04-10 14:24:30 -070075}
76
Chia-I Wu5bac7f32017-04-06 12:34:32 -070077static inline Gralloc2::IMapper::Rect asGralloc2Rect(const Rect& rect) {
78 Gralloc2::IMapper::Rect outRect{};
79 outRect.left = rect.left;
80 outRect.top = rect.top;
81 outRect.width = rect.width();
82 outRect.height = rect.height();
83 return outRect;
Craig Donner58a1ef22017-02-02 12:40:05 -080084}
85
Dan Stoza8deb4da2016-06-01 18:21:44 -070086status_t GraphicBufferMapper::lock(buffer_handle_t handle, uint32_t usage,
87 const Rect& bounds, void** vaddr)
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -070088{
Dan Stoza8deb4da2016-06-01 18:21:44 -070089 return lockAsync(handle, usage, bounds, vaddr, -1);
90}
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -070091
Dan Stoza8deb4da2016-06-01 18:21:44 -070092status_t GraphicBufferMapper::lockYCbCr(buffer_handle_t handle, uint32_t usage,
93 const Rect& bounds, android_ycbcr *ycbcr)
94{
95 return lockAsyncYCbCr(handle, usage, bounds, ycbcr, -1);
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -070096}
97
Mathias Agopian3330b202009-10-05 17:07:12 -070098status_t GraphicBufferMapper::unlock(buffer_handle_t handle)
Mathias Agopian076b1cc2009-04-10 14:24:30 -070099{
Dan Stoza8deb4da2016-06-01 18:21:44 -0700100 int32_t fenceFd = -1;
101 status_t error = unlockAsync(handle, &fenceFd);
102 if (error == NO_ERROR) {
103 sync_wait(fenceFd, -1);
104 close(fenceFd);
105 }
106 return error;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700107}
108
Francis Hart8f396012014-04-01 15:30:53 +0300109status_t GraphicBufferMapper::lockAsync(buffer_handle_t handle,
Dan Stozad3182402014-11-17 12:03:59 -0800110 uint32_t usage, const Rect& bounds, void** vaddr, int fenceFd)
Francis Hart8f396012014-04-01 15:30:53 +0300111{
Craig Donnere96a3252017-02-02 12:13:34 -0800112 return lockAsync(handle, usage, usage, bounds, vaddr, fenceFd);
113}
114
115status_t GraphicBufferMapper::lockAsync(buffer_handle_t handle,
116 uint64_t producerUsage, uint64_t consumerUsage, const Rect& bounds,
117 void** vaddr, int fenceFd)
118{
Francis Hart8f396012014-04-01 15:30:53 +0300119 ATRACE_CALL();
Francis Hart8f396012014-04-01 15:30:53 +0300120
Chia-I Wucb8405e2017-04-17 15:20:19 -0700121 const uint64_t usage = static_cast<uint64_t>(
122 android_convertGralloc1To0Usage(producerUsage, consumerUsage));
123 Gralloc2::Error error = mMapper->lock(handle, usage,
124 asGralloc2Rect(bounds), fenceFd, vaddr);
Chia-I Wu9ba189d2016-09-22 17:13:08 +0800125
Chia-I Wucb8405e2017-04-17 15:20:19 -0700126 ALOGW_IF(error != Gralloc2::Error::NONE, "lock(%p, ...) failed: %d",
127 handle, error);
Dan Stoza8deb4da2016-06-01 18:21:44 -0700128
Chia-I Wucb8405e2017-04-17 15:20:19 -0700129 return static_cast<status_t>(error);
Dan Stoza8deb4da2016-06-01 18:21:44 -0700130}
131
132static inline bool isValidYCbCrPlane(const android_flex_plane_t& plane) {
133 if (plane.bits_per_component != 8) {
134 ALOGV("Invalid number of bits per component: %d",
135 plane.bits_per_component);
136 return false;
137 }
138 if (plane.bits_used != 8) {
139 ALOGV("Invalid number of bits used: %d", plane.bits_used);
140 return false;
Francis Hart8f396012014-04-01 15:30:53 +0300141 }
142
Dan Stoza8deb4da2016-06-01 18:21:44 -0700143 bool hasValidIncrement = plane.h_increment == 1 ||
144 (plane.component != FLEX_COMPONENT_Y && plane.h_increment == 2);
145 hasValidIncrement = hasValidIncrement && plane.v_increment > 0;
146 if (!hasValidIncrement) {
147 ALOGV("Invalid increment: h %d v %d", plane.h_increment,
148 plane.v_increment);
149 return false;
150 }
151
152 return true;
Francis Hart8f396012014-04-01 15:30:53 +0300153}
154
155status_t GraphicBufferMapper::lockAsyncYCbCr(buffer_handle_t handle,
Dan Stozad3182402014-11-17 12:03:59 -0800156 uint32_t usage, const Rect& bounds, android_ycbcr *ycbcr, int fenceFd)
Francis Hart8f396012014-04-01 15:30:53 +0300157{
158 ATRACE_CALL();
Francis Hart8f396012014-04-01 15:30:53 +0300159
Chia-I Wucb8405e2017-04-17 15:20:19 -0700160 Gralloc2::YCbCrLayout layout;
161 Gralloc2::Error error = mMapper->lock(handle, usage,
162 asGralloc2Rect(bounds), fenceFd, &layout);
163 if (error == Gralloc2::Error::NONE) {
164 ycbcr->y = layout.y;
165 ycbcr->cb = layout.cb;
166 ycbcr->cr = layout.cr;
167 ycbcr->ystride = static_cast<size_t>(layout.yStride);
168 ycbcr->cstride = static_cast<size_t>(layout.cStride);
169 ycbcr->chroma_step = static_cast<size_t>(layout.chromaStep);
Chia-I Wu9ba189d2016-09-22 17:13:08 +0800170 }
171
Chia-I Wucb8405e2017-04-17 15:20:19 -0700172 return static_cast<status_t>(error);
Francis Hart8f396012014-04-01 15:30:53 +0300173}
174
175status_t GraphicBufferMapper::unlockAsync(buffer_handle_t handle, int *fenceFd)
176{
177 ATRACE_CALL();
Francis Hart8f396012014-04-01 15:30:53 +0300178
Chia-I Wucb8405e2017-04-17 15:20:19 -0700179 *fenceFd = mMapper->unlock(handle);
Francis Hart8f396012014-04-01 15:30:53 +0300180
Chia-I Wucb8405e2017-04-17 15:20:19 -0700181 return NO_ERROR;
Francis Hart8f396012014-04-01 15:30:53 +0300182}
183
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700184// ---------------------------------------------------------------------------
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700185}; // namespace android