blob: d7bf4d558c407d7fbd262e24712acd711edc59fe [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>
Mathias Agopiana9347642017-02-13 16:42:28 -080037#include <ui/GraphicBuffer.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070038
Dan Stoza8deb4da2016-06-01 18:21:44 -070039#include <system/graphics.h>
Mathias Agopian8b765b72009-04-10 20:34:46 -070040
Mathias Agopian076b1cc2009-04-10 14:24:30 -070041namespace android {
42// ---------------------------------------------------------------------------
43
Mathias Agopian3330b202009-10-05 17:07:12 -070044ANDROID_SINGLETON_STATIC_INSTANCE( GraphicBufferMapper )
Mathias Agopian4243e662009-04-15 18:34:24 -070045
Jesse Halleeb4ac72017-07-06 14:02:29 -070046void GraphicBufferMapper::preloadHal() {
Marissa Walld380e2c2018-12-29 14:17:29 -080047 Gralloc2Mapper::preload();
Jesse Halleeb4ac72017-07-06 14:02:29 -070048}
49
Marissa Walld380e2c2018-12-29 14:17:29 -080050GraphicBufferMapper::GraphicBufferMapper() : mMapper(std::make_unique<const Gralloc2Mapper>()) {}
Dan Stoza8deb4da2016-06-01 18:21:44 -070051
Chia-I Wu5bac7f32017-04-06 12:34:32 -070052status_t GraphicBufferMapper::importBuffer(buffer_handle_t rawHandle,
Chia-I Wudbbe33b2017-09-27 15:22:21 -070053 uint32_t width, uint32_t height, uint32_t layerCount,
54 PixelFormat format, uint64_t usage, uint32_t stride,
Chia-I Wu5bac7f32017-04-06 12:34:32 -070055 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 Wudbbe33b2017-09-27 15:22:21 -070059 buffer_handle_t bufferHandle;
Marissa Wall1e779252018-12-29 12:01:57 -080060 status_t error = mMapper->importBuffer(hardware::hidl_handle(rawHandle), &bufferHandle);
61 if (error != NO_ERROR) {
Chia-I Wudbbe33b2017-09-27 15:22:21 -070062 ALOGW("importBuffer(%p) failed: %d", rawHandle, error);
Marissa Wall1e779252018-12-29 12:01:57 -080063 return error;
Chia-I Wudbbe33b2017-09-27 15:22:21 -070064 }
Chia-I Wu5bac7f32017-04-06 12:34:32 -070065
Marissa Wall1e779252018-12-29 12:01:57 -080066 error = mMapper->validateBufferSize(bufferHandle, width, height, format, layerCount, usage,
67 stride);
68 if (error != NO_ERROR) {
Chia-I Wudbbe33b2017-09-27 15:22:21 -070069 ALOGE("validateBufferSize(%p) failed: %d", rawHandle, error);
70 freeBuffer(bufferHandle);
71 return static_cast<status_t>(error);
72 }
Chia-I Wu5bac7f32017-04-06 12:34:32 -070073
Chia-I Wudbbe33b2017-09-27 15:22:21 -070074 *outHandle = bufferHandle;
75
76 return NO_ERROR;
77}
78
79void GraphicBufferMapper::getTransportSize(buffer_handle_t handle,
80 uint32_t* outTransportNumFds, uint32_t* outTransportNumInts)
81{
82 mMapper->getTransportSize(handle, outTransportNumFds, outTransportNumInts);
Chia-I Wu5bac7f32017-04-06 12:34:32 -070083}
84
Chia-I Wu5bac7f32017-04-06 12:34:32 -070085status_t GraphicBufferMapper::freeBuffer(buffer_handle_t handle)
Mathias Agopian076b1cc2009-04-10 14:24:30 -070086{
Mathias Agopiancf563192012-02-29 20:43:29 -080087 ATRACE_CALL();
Mathias Agopian0a757812010-12-08 16:40:01 -080088
Chia-I Wucb8405e2017-04-17 15:20:19 -070089 mMapper->freeBuffer(handle);
Chia-I Wu9ba189d2016-09-22 17:13:08 +080090
Chia-I Wucb8405e2017-04-17 15:20:19 -070091 return NO_ERROR;
Mathias Agopian076b1cc2009-04-10 14:24:30 -070092}
93
Dan Stoza8deb4da2016-06-01 18:21:44 -070094status_t GraphicBufferMapper::lock(buffer_handle_t handle, uint32_t usage,
95 const Rect& bounds, void** vaddr)
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -070096{
Dan Stoza8deb4da2016-06-01 18:21:44 -070097 return lockAsync(handle, usage, bounds, vaddr, -1);
98}
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -070099
Dan Stoza8deb4da2016-06-01 18:21:44 -0700100status_t GraphicBufferMapper::lockYCbCr(buffer_handle_t handle, uint32_t usage,
101 const Rect& bounds, android_ycbcr *ycbcr)
102{
103 return lockAsyncYCbCr(handle, usage, bounds, ycbcr, -1);
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700104}
105
Mathias Agopian3330b202009-10-05 17:07:12 -0700106status_t GraphicBufferMapper::unlock(buffer_handle_t handle)
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700107{
Dan Stoza8deb4da2016-06-01 18:21:44 -0700108 int32_t fenceFd = -1;
109 status_t error = unlockAsync(handle, &fenceFd);
Daniel Jaraie9147c22017-09-20 11:33:51 +0200110 if (error == NO_ERROR && fenceFd >= 0) {
Dan Stoza8deb4da2016-06-01 18:21:44 -0700111 sync_wait(fenceFd, -1);
112 close(fenceFd);
113 }
114 return error;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700115}
116
Francis Hart8f396012014-04-01 15:30:53 +0300117status_t GraphicBufferMapper::lockAsync(buffer_handle_t handle,
Dan Stozad3182402014-11-17 12:03:59 -0800118 uint32_t usage, const Rect& bounds, void** vaddr, int fenceFd)
Francis Hart8f396012014-04-01 15:30:53 +0300119{
Craig Donnere96a3252017-02-02 12:13:34 -0800120 return lockAsync(handle, usage, usage, bounds, vaddr, fenceFd);
121}
122
123status_t GraphicBufferMapper::lockAsync(buffer_handle_t handle,
124 uint64_t producerUsage, uint64_t consumerUsage, const Rect& bounds,
125 void** vaddr, int fenceFd)
126{
Francis Hart8f396012014-04-01 15:30:53 +0300127 ATRACE_CALL();
Francis Hart8f396012014-04-01 15:30:53 +0300128
Chia-I Wucb8405e2017-04-17 15:20:19 -0700129 const uint64_t usage = static_cast<uint64_t>(
130 android_convertGralloc1To0Usage(producerUsage, consumerUsage));
Marissa Wall1e779252018-12-29 12:01:57 -0800131 return mMapper->lock(handle, usage, bounds, fenceFd, vaddr);
Dan Stoza8deb4da2016-06-01 18:21:44 -0700132}
133
Francis Hart8f396012014-04-01 15:30:53 +0300134status_t GraphicBufferMapper::lockAsyncYCbCr(buffer_handle_t handle,
Dan Stozad3182402014-11-17 12:03:59 -0800135 uint32_t usage, const Rect& bounds, android_ycbcr *ycbcr, int fenceFd)
Francis Hart8f396012014-04-01 15:30:53 +0300136{
137 ATRACE_CALL();
Francis Hart8f396012014-04-01 15:30:53 +0300138
Marissa Wall1e779252018-12-29 12:01:57 -0800139 return mMapper->lock(handle, usage, bounds, fenceFd, ycbcr);
Francis Hart8f396012014-04-01 15:30:53 +0300140}
141
142status_t GraphicBufferMapper::unlockAsync(buffer_handle_t handle, int *fenceFd)
143{
144 ATRACE_CALL();
Francis Hart8f396012014-04-01 15:30:53 +0300145
Chia-I Wucb8405e2017-04-17 15:20:19 -0700146 *fenceFd = mMapper->unlock(handle);
Francis Hart8f396012014-04-01 15:30:53 +0300147
Chia-I Wucb8405e2017-04-17 15:20:19 -0700148 return NO_ERROR;
Francis Hart8f396012014-04-01 15:30:53 +0300149}
150
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700151// ---------------------------------------------------------------------------
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700152}; // namespace android