• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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,buffer_handle_t * outHandle)54 status_t GraphicBufferMapper::importBuffer(buffer_handle_t rawHandle,
55         buffer_handle_t* outHandle)
56 {
57     ATRACE_CALL();
58 
59     Gralloc2::Error error = mMapper->importBuffer(
60             hardware::hidl_handle(rawHandle), outHandle);
61 
62     ALOGW_IF(error != Gralloc2::Error::NONE, "importBuffer(%p) failed: %d",
63             rawHandle, error);
64 
65     return static_cast<status_t>(error);
66 }
67 
freeBuffer(buffer_handle_t handle)68 status_t GraphicBufferMapper::freeBuffer(buffer_handle_t handle)
69 {
70     ATRACE_CALL();
71 
72     mMapper->freeBuffer(handle);
73 
74     return NO_ERROR;
75 }
76 
asGralloc2Rect(const Rect & rect)77 static inline Gralloc2::IMapper::Rect asGralloc2Rect(const Rect& rect) {
78     Gralloc2::IMapper::Rect outRect{};
79     outRect.left = rect.left;
80     outRect.top = rect.top;
81     outRect.width = rect.width();
82     outRect.height = rect.height();
83     return outRect;
84 }
85 
lock(buffer_handle_t handle,uint32_t usage,const Rect & bounds,void ** vaddr)86 status_t GraphicBufferMapper::lock(buffer_handle_t handle, uint32_t usage,
87         const Rect& bounds, void** vaddr)
88 {
89     return lockAsync(handle, usage, bounds, vaddr, -1);
90 }
91 
lockYCbCr(buffer_handle_t handle,uint32_t usage,const Rect & bounds,android_ycbcr * ycbcr)92 status_t GraphicBufferMapper::lockYCbCr(buffer_handle_t handle, uint32_t usage,
93         const Rect& bounds, android_ycbcr *ycbcr)
94 {
95     return lockAsyncYCbCr(handle, usage, bounds, ycbcr, -1);
96 }
97 
unlock(buffer_handle_t handle)98 status_t GraphicBufferMapper::unlock(buffer_handle_t handle)
99 {
100     int32_t fenceFd = -1;
101     status_t error = unlockAsync(handle, &fenceFd);
102     if (error == NO_ERROR) {
103         sync_wait(fenceFd, -1);
104         close(fenceFd);
105     }
106     return error;
107 }
108 
lockAsync(buffer_handle_t handle,uint32_t usage,const Rect & bounds,void ** vaddr,int fenceFd)109 status_t GraphicBufferMapper::lockAsync(buffer_handle_t handle,
110         uint32_t usage, const Rect& bounds, void** vaddr, int fenceFd)
111 {
112     return lockAsync(handle, usage, usage, bounds, vaddr, fenceFd);
113 }
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 
121     const uint64_t usage = static_cast<uint64_t>(
122             android_convertGralloc1To0Usage(producerUsage, consumerUsage));
123     Gralloc2::Error error = mMapper->lock(handle, usage,
124             asGralloc2Rect(bounds), fenceFd, vaddr);
125 
126     ALOGW_IF(error != Gralloc2::Error::NONE, "lock(%p, ...) failed: %d",
127             handle, error);
128 
129     return static_cast<status_t>(error);
130 }
131 
isValidYCbCrPlane(const android_flex_plane_t & plane)132 static inline bool isValidYCbCrPlane(const android_flex_plane_t& plane) {
133     if (plane.bits_per_component != 8) {
134         ALOGV("Invalid number of bits per component: %d",
135                 plane.bits_per_component);
136         return false;
137     }
138     if (plane.bits_used != 8) {
139         ALOGV("Invalid number of bits used: %d", plane.bits_used);
140         return false;
141     }
142 
143     bool hasValidIncrement = plane.h_increment == 1 ||
144             (plane.component != FLEX_COMPONENT_Y && plane.h_increment == 2);
145     hasValidIncrement = hasValidIncrement && plane.v_increment > 0;
146     if (!hasValidIncrement) {
147         ALOGV("Invalid increment: h %d v %d", plane.h_increment,
148                 plane.v_increment);
149         return false;
150     }
151 
152     return true;
153 }
154 
lockAsyncYCbCr(buffer_handle_t handle,uint32_t usage,const Rect & bounds,android_ycbcr * ycbcr,int fenceFd)155 status_t GraphicBufferMapper::lockAsyncYCbCr(buffer_handle_t handle,
156         uint32_t usage, const Rect& bounds, android_ycbcr *ycbcr, int fenceFd)
157 {
158     ATRACE_CALL();
159 
160     Gralloc2::YCbCrLayout layout;
161     Gralloc2::Error error = mMapper->lock(handle, usage,
162             asGralloc2Rect(bounds), fenceFd, &layout);
163     if (error == Gralloc2::Error::NONE) {
164         ycbcr->y = layout.y;
165         ycbcr->cb = layout.cb;
166         ycbcr->cr = layout.cr;
167         ycbcr->ystride = static_cast<size_t>(layout.yStride);
168         ycbcr->cstride = static_cast<size_t>(layout.cStride);
169         ycbcr->chroma_step = static_cast<size_t>(layout.chromaStep);
170     }
171 
172     return static_cast<status_t>(error);
173 }
174 
unlockAsync(buffer_handle_t handle,int * fenceFd)175 status_t GraphicBufferMapper::unlockAsync(buffer_handle_t handle, int *fenceFd)
176 {
177     ATRACE_CALL();
178 
179     *fenceFd = mMapper->unlock(handle);
180 
181     return NO_ERROR;
182 }
183 
184 // ---------------------------------------------------------------------------
185 }; // namespace android
186