blob: 25b7247b1b0a358c3335165c6d97589af60dd918 [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>
Mathias Agopiana9347642017-02-13 16:42:28 -080038#include <ui/GraphicBuffer.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070039
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
Jesse Halleeb4ac72017-07-06 14:02:29 -070047void GraphicBufferMapper::preloadHal() {
Marissa Walld380e2c2018-12-29 14:17:29 -080048 Gralloc2Mapper::preload();
Marissa Wall925bf7f2018-12-29 14:27:11 -080049 Gralloc3Mapper::preload();
Jesse Halleeb4ac72017-07-06 14:02:29 -070050}
51
Marissa Wall925bf7f2018-12-29 14:27:11 -080052GraphicBufferMapper::GraphicBufferMapper() {
53 mMapper = std::make_unique<const Gralloc3Mapper>();
Valerie Hau250c6542019-01-31 14:23:43 -080054 if (!mMapper->isLoaded()) {
Marissa Wall925bf7f2018-12-29 14:27:11 -080055 mMapper = std::make_unique<const Gralloc2Mapper>();
Valerie Haud2f4daf2019-02-15 13:49:00 -080056 mMapperVersion = Version::GRALLOC_2;
57 } else {
58 mMapperVersion = Version::GRALLOC_3;
Marissa Wall925bf7f2018-12-29 14:27:11 -080059 }
60
Valerie Hau250c6542019-01-31 14:23:43 -080061 if (!mMapper->isLoaded()) {
Marissa Wall925bf7f2018-12-29 14:27:11 -080062 LOG_ALWAYS_FATAL("gralloc-mapper is missing");
63 }
64}
Dan Stoza8deb4da2016-06-01 18:21:44 -070065
Chia-I Wu5bac7f32017-04-06 12:34:32 -070066status_t GraphicBufferMapper::importBuffer(buffer_handle_t rawHandle,
Chia-I Wudbbe33b2017-09-27 15:22:21 -070067 uint32_t width, uint32_t height, uint32_t layerCount,
68 PixelFormat format, uint64_t usage, uint32_t stride,
Chia-I Wu5bac7f32017-04-06 12:34:32 -070069 buffer_handle_t* outHandle)
Mathias Agopian076b1cc2009-04-10 14:24:30 -070070{
Mathias Agopiancf563192012-02-29 20:43:29 -080071 ATRACE_CALL();
Mathias Agopian0a757812010-12-08 16:40:01 -080072
Chia-I Wudbbe33b2017-09-27 15:22:21 -070073 buffer_handle_t bufferHandle;
Marissa Wall1e779252018-12-29 12:01:57 -080074 status_t error = mMapper->importBuffer(hardware::hidl_handle(rawHandle), &bufferHandle);
75 if (error != NO_ERROR) {
Chia-I Wudbbe33b2017-09-27 15:22:21 -070076 ALOGW("importBuffer(%p) failed: %d", rawHandle, error);
Marissa Wall1e779252018-12-29 12:01:57 -080077 return error;
Chia-I Wudbbe33b2017-09-27 15:22:21 -070078 }
Chia-I Wu5bac7f32017-04-06 12:34:32 -070079
Marissa Wall1e779252018-12-29 12:01:57 -080080 error = mMapper->validateBufferSize(bufferHandle, width, height, format, layerCount, usage,
81 stride);
82 if (error != NO_ERROR) {
Chia-I Wudbbe33b2017-09-27 15:22:21 -070083 ALOGE("validateBufferSize(%p) failed: %d", rawHandle, error);
84 freeBuffer(bufferHandle);
85 return static_cast<status_t>(error);
86 }
Chia-I Wu5bac7f32017-04-06 12:34:32 -070087
Chia-I Wudbbe33b2017-09-27 15:22:21 -070088 *outHandle = bufferHandle;
89
90 return NO_ERROR;
91}
92
93void GraphicBufferMapper::getTransportSize(buffer_handle_t handle,
94 uint32_t* outTransportNumFds, uint32_t* outTransportNumInts)
95{
96 mMapper->getTransportSize(handle, outTransportNumFds, outTransportNumInts);
Chia-I Wu5bac7f32017-04-06 12:34:32 -070097}
98
Chia-I Wu5bac7f32017-04-06 12:34:32 -070099status_t GraphicBufferMapper::freeBuffer(buffer_handle_t handle)
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700100{
Mathias Agopiancf563192012-02-29 20:43:29 -0800101 ATRACE_CALL();
Mathias Agopian0a757812010-12-08 16:40:01 -0800102
Chia-I Wucb8405e2017-04-17 15:20:19 -0700103 mMapper->freeBuffer(handle);
Chia-I Wu9ba189d2016-09-22 17:13:08 +0800104
Chia-I Wucb8405e2017-04-17 15:20:19 -0700105 return NO_ERROR;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700106}
107
Valerie Hau0c9fc362019-01-22 09:17:19 -0800108status_t GraphicBufferMapper::lock(buffer_handle_t handle, uint32_t usage, const Rect& bounds,
109 void** vaddr, int32_t* outBytesPerPixel,
110 int32_t* outBytesPerStride) {
111 return lockAsync(handle, usage, bounds, vaddr, -1, outBytesPerPixel, outBytesPerStride);
Dan Stoza8deb4da2016-06-01 18:21:44 -0700112}
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700113
Dan Stoza8deb4da2016-06-01 18:21:44 -0700114status_t GraphicBufferMapper::lockYCbCr(buffer_handle_t handle, uint32_t usage,
115 const Rect& bounds, android_ycbcr *ycbcr)
116{
117 return lockAsyncYCbCr(handle, usage, bounds, ycbcr, -1);
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700118}
119
Mathias Agopian3330b202009-10-05 17:07:12 -0700120status_t GraphicBufferMapper::unlock(buffer_handle_t handle)
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700121{
Dan Stoza8deb4da2016-06-01 18:21:44 -0700122 int32_t fenceFd = -1;
123 status_t error = unlockAsync(handle, &fenceFd);
Daniel Jaraie9147c22017-09-20 11:33:51 +0200124 if (error == NO_ERROR && fenceFd >= 0) {
Dan Stoza8deb4da2016-06-01 18:21:44 -0700125 sync_wait(fenceFd, -1);
126 close(fenceFd);
127 }
128 return error;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700129}
130
Valerie Hau0c9fc362019-01-22 09:17:19 -0800131status_t GraphicBufferMapper::lockAsync(buffer_handle_t handle, uint32_t usage, const Rect& bounds,
132 void** vaddr, int fenceFd, int32_t* outBytesPerPixel,
133 int32_t* outBytesPerStride) {
134 return lockAsync(handle, usage, usage, bounds, vaddr, fenceFd, outBytesPerPixel,
135 outBytesPerStride);
Craig Donnere96a3252017-02-02 12:13:34 -0800136}
137
Valerie Hau0c9fc362019-01-22 09:17:19 -0800138status_t GraphicBufferMapper::lockAsync(buffer_handle_t handle, uint64_t producerUsage,
139 uint64_t consumerUsage, const Rect& bounds, void** vaddr,
140 int fenceFd, int32_t* outBytesPerPixel,
141 int32_t* outBytesPerStride) {
Francis Hart8f396012014-04-01 15:30:53 +0300142 ATRACE_CALL();
Francis Hart8f396012014-04-01 15:30:53 +0300143
Chia-I Wucb8405e2017-04-17 15:20:19 -0700144 const uint64_t usage = static_cast<uint64_t>(
145 android_convertGralloc1To0Usage(producerUsage, consumerUsage));
Valerie Hau0c9fc362019-01-22 09:17:19 -0800146 return mMapper->lock(handle, usage, bounds, fenceFd, vaddr, outBytesPerPixel,
147 outBytesPerStride);
Dan Stoza8deb4da2016-06-01 18:21:44 -0700148}
149
Francis Hart8f396012014-04-01 15:30:53 +0300150status_t GraphicBufferMapper::lockAsyncYCbCr(buffer_handle_t handle,
Dan Stozad3182402014-11-17 12:03:59 -0800151 uint32_t usage, const Rect& bounds, android_ycbcr *ycbcr, int fenceFd)
Francis Hart8f396012014-04-01 15:30:53 +0300152{
153 ATRACE_CALL();
Francis Hart8f396012014-04-01 15:30:53 +0300154
Marissa Wall1e779252018-12-29 12:01:57 -0800155 return mMapper->lock(handle, usage, bounds, fenceFd, ycbcr);
Francis Hart8f396012014-04-01 15:30:53 +0300156}
157
158status_t GraphicBufferMapper::unlockAsync(buffer_handle_t handle, int *fenceFd)
159{
160 ATRACE_CALL();
Francis Hart8f396012014-04-01 15:30:53 +0300161
Chia-I Wucb8405e2017-04-17 15:20:19 -0700162 *fenceFd = mMapper->unlock(handle);
Francis Hart8f396012014-04-01 15:30:53 +0300163
Chia-I Wucb8405e2017-04-17 15:20:19 -0700164 return NO_ERROR;
Francis Hart8f396012014-04-01 15:30:53 +0300165}
166
Valerie Hauddbfaeb2019-02-01 09:54:20 -0800167status_t GraphicBufferMapper::isSupported(uint32_t width, uint32_t height,
168 android::PixelFormat format, uint32_t layerCount,
169 uint64_t usage, bool* outSupported) {
170 return mMapper->isSupported(width, height, format, layerCount, usage, outSupported);
171}
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700172// ---------------------------------------------------------------------------
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700173}; // namespace android