1 /*
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 #define LOG_TAG "GraphicBufferMapper"
17 #define ATRACE_TAG ATRACE_TAG_GRAPHICS
18 //#define LOG_NDEBUG 0
19 #include <ui/GraphicBufferMapper.h>
20 #include <grallocusage/GrallocUsageConversion.h>
21 // We would eliminate the non-conforming zero-length array, but we can't since
22 // this is effectively included from the Linux kernel
23 #pragma clang diagnostic push
24 #pragma clang diagnostic ignored "-Wzero-length-array"
25 #include <sync/sync.h>
26 #pragma clang diagnostic pop
27 #include <utils/Log.h>
28 #include <utils/Singleton.h>
29 #include <utils/Trace.h>
30 #include <ui/Gralloc2.h>
31 #include <ui/GraphicBuffer.h>
32 #include <system/graphics.h>
33 namespace android {
34 // ---------------------------------------------------------------------------
ANDROID_SINGLETON_STATIC_INSTANCE(GraphicBufferMapper)35 ANDROID_SINGLETON_STATIC_INSTANCE( GraphicBufferMapper )
36 void GraphicBufferMapper::preloadHal() {
37 Gralloc2::Mapper::preload();
38 }
GraphicBufferMapper()39 GraphicBufferMapper::GraphicBufferMapper()
40 : mMapper(std::make_unique<const Gralloc2::Mapper>())
41 {
42 }
importBuffer(buffer_handle_t rawHandle,uint32_t width,uint32_t height,uint32_t layerCount,PixelFormat format,uint64_t usage,uint32_t stride,buffer_handle_t * outHandle)43 status_t GraphicBufferMapper::importBuffer(buffer_handle_t rawHandle,
44 uint32_t width, uint32_t height, uint32_t layerCount,
45 PixelFormat format, uint64_t usage, uint32_t stride,
46 buffer_handle_t* outHandle)
47 {
48 ATRACE_CALL();
49 buffer_handle_t bufferHandle;
50 Gralloc2::Error error = mMapper->importBuffer(
51 hardware::hidl_handle(rawHandle), &bufferHandle);
52 if (error != Gralloc2::Error::NONE) {
53 ALOGW("importBuffer(%p) failed: %d", rawHandle, error);
54 return static_cast<status_t>(error);
55 }
56 Gralloc2::IMapper::BufferDescriptorInfo info = {};
57 info.width = width;
58 info.height = height;
59 info.layerCount = layerCount;
60 info.format = static_cast<Gralloc2::PixelFormat>(format);
61 info.usage = usage;
62 error = mMapper->validateBufferSize(bufferHandle, info, stride);
63 if (error != Gralloc2::Error::NONE) {
64 ALOGE("validateBufferSize(%p) failed: %d", rawHandle, error);
65 freeBuffer(bufferHandle);
66 return static_cast<status_t>(error);
67 }
68 *outHandle = bufferHandle;
69 return NO_ERROR;
70 }
getTransportSize(buffer_handle_t handle,uint32_t * outTransportNumFds,uint32_t * outTransportNumInts)71 void GraphicBufferMapper::getTransportSize(buffer_handle_t handle,
72 uint32_t* outTransportNumFds, uint32_t* outTransportNumInts)
73 {
74 mMapper->getTransportSize(handle, outTransportNumFds, outTransportNumInts);
75 }
freeBuffer(buffer_handle_t handle)76 status_t GraphicBufferMapper::freeBuffer(buffer_handle_t handle)
77 {
78 ATRACE_CALL();
79 mMapper->freeBuffer(handle);
80 return NO_ERROR;
81 }
asGralloc2Rect(const Rect & rect)82 static inline Gralloc2::IMapper::Rect asGralloc2Rect(const Rect& rect) {
83 Gralloc2::IMapper::Rect outRect{};
84 outRect.left = rect.left;
85 outRect.top = rect.top;
86 outRect.width = rect.width();
87 outRect.height = rect.height();
88 return outRect;
89 }
lock(buffer_handle_t handle,uint32_t usage,const Rect & bounds,void ** vaddr)90 status_t GraphicBufferMapper::lock(buffer_handle_t handle, uint32_t usage,
91 const Rect& bounds, void** vaddr)
92 {
93 return lockAsync(handle, usage, bounds, vaddr, -1);
94 }
lockYCbCr(buffer_handle_t handle,uint32_t usage,const Rect & bounds,android_ycbcr * ycbcr)95 status_t GraphicBufferMapper::lockYCbCr(buffer_handle_t handle, uint32_t usage,
96 const Rect& bounds, android_ycbcr *ycbcr)
97 {
98 return lockAsyncYCbCr(handle, usage, bounds, ycbcr, -1);
99 }
unlock(buffer_handle_t handle)100 status_t GraphicBufferMapper::unlock(buffer_handle_t handle)
101 {
102 int32_t fenceFd = -1;
103 status_t error = unlockAsync(handle, &fenceFd);
104 if (error == NO_ERROR && fenceFd >= 0) {
105 sync_wait(fenceFd, -1);
106 close(fenceFd);
107 }
108 return error;
109 }
lockAsync(buffer_handle_t handle,uint32_t usage,const Rect & bounds,void ** vaddr,int fenceFd)110 status_t GraphicBufferMapper::lockAsync(buffer_handle_t handle,
111 uint32_t usage, const Rect& bounds, void** vaddr, int fenceFd)
112 {
113 return lockAsync(handle, usage, usage, bounds, vaddr, fenceFd);
114 }
lockAsync(buffer_handle_t handle,uint64_t producerUsage,uint64_t consumerUsage,const Rect & bounds,void ** vaddr,int fenceFd)115 status_t GraphicBufferMapper::lockAsync(buffer_handle_t handle,
116 uint64_t producerUsage, uint64_t consumerUsage, const Rect& bounds,
117 void** vaddr, int fenceFd)
118 {
119 ATRACE_CALL();
120 const uint64_t usage = static_cast<uint64_t>(
121 android_convertGralloc1To0Usage(producerUsage, consumerUsage));
122 Gralloc2::Error error = mMapper->lock(handle, usage,
123 asGralloc2Rect(bounds), fenceFd, vaddr);
124 ALOGW_IF(error != Gralloc2::Error::NONE, "lock(%p, ...) failed: %d",
125 handle, error);
126 return static_cast<status_t>(error);
127 }
lockAsyncYCbCr(buffer_handle_t handle,uint32_t usage,const Rect & bounds,android_ycbcr * ycbcr,int fenceFd)128 status_t GraphicBufferMapper::lockAsyncYCbCr(buffer_handle_t handle,
129 uint32_t usage, const Rect& bounds, android_ycbcr *ycbcr, int fenceFd)
130 {
131 ATRACE_CALL();
132 Gralloc2::YCbCrLayout layout;
133 Gralloc2::Error error = mMapper->lock(handle, usage,
134 asGralloc2Rect(bounds), fenceFd, &layout);
135 if (error == Gralloc2::Error::NONE) {
136 ycbcr->y = layout.y;
137 ycbcr->cb = layout.cb;
138 ycbcr->cr = layout.cr;
139 ycbcr->ystride = static_cast<size_t>(layout.yStride);
140 ycbcr->cstride = static_cast<size_t>(layout.cStride);
141 ycbcr->chroma_step = static_cast<size_t>(layout.chromaStep);
142 }
143 return static_cast<status_t>(error);
144 }
unlockAsync(buffer_handle_t handle,int * fenceFd)145 status_t GraphicBufferMapper::unlockAsync(buffer_handle_t handle, int *fenceFd)
146 {
147 ATRACE_CALL();
148 *fenceFd = mMapper->unlock(handle);
149 return NO_ERROR;
150 }
151 // ---------------------------------------------------------------------------
152 }; // namespace android
153