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