blob: 4d087d151cb4c0ab38c8da143c9b5e2bb7895751 [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
Marissa Walld380e2c2018-12-29 14:17:29 -080035#include <ui/Gralloc.h>
Chia-I Wu5bac7f32017-04-06 12:34:32 -070036#include <ui/Gralloc2.h>
Marissa Wall925bf7f2018-12-29 14:27:11 -080037#include <ui/Gralloc3.h>
Marissa Wall87c8ba72019-06-20 14:20:52 -070038#include <ui/Gralloc4.h>
Mathias Agopiana9347642017-02-13 16:42:28 -080039#include <ui/GraphicBuffer.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070040
Dan Stoza8deb4da2016-06-01 18:21:44 -070041#include <system/graphics.h>
Mathias Agopian8b765b72009-04-10 20:34:46 -070042
Mathias Agopian076b1cc2009-04-10 14:24:30 -070043namespace android {
44// ---------------------------------------------------------------------------
45
Mathias Agopian3330b202009-10-05 17:07:12 -070046ANDROID_SINGLETON_STATIC_INSTANCE( GraphicBufferMapper )
Mathias Agopian4243e662009-04-15 18:34:24 -070047
Jesse Halleeb4ac72017-07-06 14:02:29 -070048void GraphicBufferMapper::preloadHal() {
Marissa Walld380e2c2018-12-29 14:17:29 -080049 Gralloc2Mapper::preload();
Marissa Wall925bf7f2018-12-29 14:27:11 -080050 Gralloc3Mapper::preload();
Marissa Wall87c8ba72019-06-20 14:20:52 -070051 Gralloc4Mapper::preload();
Jesse Halleeb4ac72017-07-06 14:02:29 -070052}
53
Marissa Wall925bf7f2018-12-29 14:27:11 -080054GraphicBufferMapper::GraphicBufferMapper() {
Marissa Wall87c8ba72019-06-20 14:20:52 -070055 mMapper = std::make_unique<const Gralloc4Mapper>();
56 if (mMapper->isLoaded()) {
57 mMapperVersion = Version::GRALLOC_4;
58 return;
59 }
Marissa Wall925bf7f2018-12-29 14:27:11 -080060 mMapper = std::make_unique<const Gralloc3Mapper>();
Marissa Wall87c8ba72019-06-20 14:20:52 -070061 if (mMapper->isLoaded()) {
Valerie Haud2f4daf2019-02-15 13:49:00 -080062 mMapperVersion = Version::GRALLOC_3;
Marissa Wall87c8ba72019-06-20 14:20:52 -070063 return;
64 }
65 mMapper = std::make_unique<const Gralloc2Mapper>();
66 if (mMapper->isLoaded()) {
67 mMapperVersion = Version::GRALLOC_2;
68 return;
Marissa Wall925bf7f2018-12-29 14:27:11 -080069 }
70
Marissa Wall87c8ba72019-06-20 14:20:52 -070071 LOG_ALWAYS_FATAL("gralloc-mapper is missing");
Marissa Wall925bf7f2018-12-29 14:27:11 -080072}
Dan Stoza8deb4da2016-06-01 18:21:44 -070073
Chia-I Wu5bac7f32017-04-06 12:34:32 -070074status_t GraphicBufferMapper::importBuffer(buffer_handle_t rawHandle,
Chia-I Wudbbe33b2017-09-27 15:22:21 -070075 uint32_t width, uint32_t height, uint32_t layerCount,
76 PixelFormat format, uint64_t usage, uint32_t stride,
Chia-I Wu5bac7f32017-04-06 12:34:32 -070077 buffer_handle_t* outHandle)
Mathias Agopian076b1cc2009-04-10 14:24:30 -070078{
Mathias Agopiancf563192012-02-29 20:43:29 -080079 ATRACE_CALL();
Mathias Agopian0a757812010-12-08 16:40:01 -080080
Chia-I Wudbbe33b2017-09-27 15:22:21 -070081 buffer_handle_t bufferHandle;
Marissa Wall1e779252018-12-29 12:01:57 -080082 status_t error = mMapper->importBuffer(hardware::hidl_handle(rawHandle), &bufferHandle);
83 if (error != NO_ERROR) {
Chia-I Wudbbe33b2017-09-27 15:22:21 -070084 ALOGW("importBuffer(%p) failed: %d", rawHandle, error);
Marissa Wall1e779252018-12-29 12:01:57 -080085 return error;
Chia-I Wudbbe33b2017-09-27 15:22:21 -070086 }
Chia-I Wu5bac7f32017-04-06 12:34:32 -070087
Marissa Wall1e779252018-12-29 12:01:57 -080088 error = mMapper->validateBufferSize(bufferHandle, width, height, format, layerCount, usage,
89 stride);
90 if (error != NO_ERROR) {
Chia-I Wudbbe33b2017-09-27 15:22:21 -070091 ALOGE("validateBufferSize(%p) failed: %d", rawHandle, error);
92 freeBuffer(bufferHandle);
93 return static_cast<status_t>(error);
94 }
Chia-I Wu5bac7f32017-04-06 12:34:32 -070095
Chia-I Wudbbe33b2017-09-27 15:22:21 -070096 *outHandle = bufferHandle;
97
98 return NO_ERROR;
99}
100
101void GraphicBufferMapper::getTransportSize(buffer_handle_t handle,
102 uint32_t* outTransportNumFds, uint32_t* outTransportNumInts)
103{
104 mMapper->getTransportSize(handle, outTransportNumFds, outTransportNumInts);
Chia-I Wu5bac7f32017-04-06 12:34:32 -0700105}
106
Chia-I Wu5bac7f32017-04-06 12:34:32 -0700107status_t GraphicBufferMapper::freeBuffer(buffer_handle_t handle)
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700108{
Mathias Agopiancf563192012-02-29 20:43:29 -0800109 ATRACE_CALL();
Mathias Agopian0a757812010-12-08 16:40:01 -0800110
Chia-I Wucb8405e2017-04-17 15:20:19 -0700111 mMapper->freeBuffer(handle);
Chia-I Wu9ba189d2016-09-22 17:13:08 +0800112
Chia-I Wucb8405e2017-04-17 15:20:19 -0700113 return NO_ERROR;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700114}
115
Valerie Hau0c9fc362019-01-22 09:17:19 -0800116status_t GraphicBufferMapper::lock(buffer_handle_t handle, uint32_t usage, const Rect& bounds,
117 void** vaddr, int32_t* outBytesPerPixel,
118 int32_t* outBytesPerStride) {
119 return lockAsync(handle, usage, bounds, vaddr, -1, outBytesPerPixel, outBytesPerStride);
Dan Stoza8deb4da2016-06-01 18:21:44 -0700120}
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700121
Dan Stoza8deb4da2016-06-01 18:21:44 -0700122status_t GraphicBufferMapper::lockYCbCr(buffer_handle_t handle, uint32_t usage,
123 const Rect& bounds, android_ycbcr *ycbcr)
124{
125 return lockAsyncYCbCr(handle, usage, bounds, ycbcr, -1);
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700126}
127
Mathias Agopian3330b202009-10-05 17:07:12 -0700128status_t GraphicBufferMapper::unlock(buffer_handle_t handle)
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700129{
Dan Stoza8deb4da2016-06-01 18:21:44 -0700130 int32_t fenceFd = -1;
131 status_t error = unlockAsync(handle, &fenceFd);
Daniel Jaraie9147c22017-09-20 11:33:51 +0200132 if (error == NO_ERROR && fenceFd >= 0) {
Dan Stoza8deb4da2016-06-01 18:21:44 -0700133 sync_wait(fenceFd, -1);
134 close(fenceFd);
135 }
136 return error;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700137}
138
Valerie Hau0c9fc362019-01-22 09:17:19 -0800139status_t GraphicBufferMapper::lockAsync(buffer_handle_t handle, uint32_t usage, const Rect& bounds,
140 void** vaddr, int fenceFd, int32_t* outBytesPerPixel,
141 int32_t* outBytesPerStride) {
142 return lockAsync(handle, usage, usage, bounds, vaddr, fenceFd, outBytesPerPixel,
143 outBytesPerStride);
Craig Donnere96a3252017-02-02 12:13:34 -0800144}
145
Valerie Hau0c9fc362019-01-22 09:17:19 -0800146status_t GraphicBufferMapper::lockAsync(buffer_handle_t handle, uint64_t producerUsage,
147 uint64_t consumerUsage, const Rect& bounds, void** vaddr,
148 int fenceFd, int32_t* outBytesPerPixel,
149 int32_t* outBytesPerStride) {
Francis Hart8f396012014-04-01 15:30:53 +0300150 ATRACE_CALL();
Francis Hart8f396012014-04-01 15:30:53 +0300151
Chia-I Wucb8405e2017-04-17 15:20:19 -0700152 const uint64_t usage = static_cast<uint64_t>(
153 android_convertGralloc1To0Usage(producerUsage, consumerUsage));
Valerie Hau0c9fc362019-01-22 09:17:19 -0800154 return mMapper->lock(handle, usage, bounds, fenceFd, vaddr, outBytesPerPixel,
155 outBytesPerStride);
Dan Stoza8deb4da2016-06-01 18:21:44 -0700156}
157
Francis Hart8f396012014-04-01 15:30:53 +0300158status_t GraphicBufferMapper::lockAsyncYCbCr(buffer_handle_t handle,
Dan Stozad3182402014-11-17 12:03:59 -0800159 uint32_t usage, const Rect& bounds, android_ycbcr *ycbcr, int fenceFd)
Francis Hart8f396012014-04-01 15:30:53 +0300160{
161 ATRACE_CALL();
Francis Hart8f396012014-04-01 15:30:53 +0300162
Marissa Wall1e779252018-12-29 12:01:57 -0800163 return mMapper->lock(handle, usage, bounds, fenceFd, ycbcr);
Francis Hart8f396012014-04-01 15:30:53 +0300164}
165
166status_t GraphicBufferMapper::unlockAsync(buffer_handle_t handle, int *fenceFd)
167{
168 ATRACE_CALL();
Francis Hart8f396012014-04-01 15:30:53 +0300169
Chia-I Wucb8405e2017-04-17 15:20:19 -0700170 *fenceFd = mMapper->unlock(handle);
Francis Hart8f396012014-04-01 15:30:53 +0300171
Chia-I Wucb8405e2017-04-17 15:20:19 -0700172 return NO_ERROR;
Francis Hart8f396012014-04-01 15:30:53 +0300173}
174
Valerie Hauddbfaeb2019-02-01 09:54:20 -0800175status_t GraphicBufferMapper::isSupported(uint32_t width, uint32_t height,
176 android::PixelFormat format, uint32_t layerCount,
177 uint64_t usage, bool* outSupported) {
178 return mMapper->isSupported(width, height, format, layerCount, usage, outSupported);
179}
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700180// ---------------------------------------------------------------------------
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700181}; // namespace android