GraphicBufferMapper.cpp revision d31824004277f554000417cea349d69f18655e95
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
20#include <stdint.h>
21#include <errno.h>
22
23// We would eliminate the non-conforming zero-length array, but we can't since
24// this is effectively included from the Linux kernel
25#pragma clang diagnostic push
26#pragma clang diagnostic ignored "-Wzero-length-array"
27#include <sync/sync.h>
28#pragma clang diagnostic pop
29
30#include <utils/Errors.h>
31#include <utils/Log.h>
32#include <utils/Trace.h>
33
34#include <ui/GraphicBufferMapper.h>
35#include <ui/Rect.h>
36
37#include <hardware/gralloc.h>
38
39
40namespace android {
41// ---------------------------------------------------------------------------
42
43ANDROID_SINGLETON_STATIC_INSTANCE( GraphicBufferMapper )
44
45GraphicBufferMapper::GraphicBufferMapper()
46    : mAllocMod(0)
47{
48    hw_module_t const* module;
49    int err = hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &module);
50    ALOGE_IF(err, "FATAL: can't find the %s module", GRALLOC_HARDWARE_MODULE_ID);
51    if (err == 0) {
52        mAllocMod = reinterpret_cast<gralloc_module_t const *>(module);
53    }
54}
55
56status_t GraphicBufferMapper::registerBuffer(buffer_handle_t handle)
57{
58    ATRACE_CALL();
59    status_t err;
60
61    err = mAllocMod->registerBuffer(mAllocMod, handle);
62
63    ALOGW_IF(err, "registerBuffer(%p) failed %d (%s)",
64            handle, err, strerror(-err));
65    return err;
66}
67
68status_t GraphicBufferMapper::unregisterBuffer(buffer_handle_t handle)
69{
70    ATRACE_CALL();
71    status_t err;
72
73    err = mAllocMod->unregisterBuffer(mAllocMod, handle);
74
75    ALOGW_IF(err, "unregisterBuffer(%p) failed %d (%s)",
76            handle, err, strerror(-err));
77    return err;
78}
79
80status_t GraphicBufferMapper::lock(buffer_handle_t handle,
81        uint32_t usage, const Rect& bounds, void** vaddr)
82{
83    ATRACE_CALL();
84    status_t err;
85
86    err = mAllocMod->lock(mAllocMod, handle, static_cast<int>(usage),
87            bounds.left, bounds.top, bounds.width(), bounds.height(),
88            vaddr);
89
90    ALOGW_IF(err, "lock(...) failed %d (%s)", err, strerror(-err));
91    return err;
92}
93
94status_t GraphicBufferMapper::lockYCbCr(buffer_handle_t handle,
95        uint32_t usage, const Rect& bounds, android_ycbcr *ycbcr)
96{
97    ATRACE_CALL();
98    status_t err;
99
100    err = mAllocMod->lock_ycbcr(mAllocMod, handle, static_cast<int>(usage),
101            bounds.left, bounds.top, bounds.width(), bounds.height(),
102            ycbcr);
103
104    ALOGW_IF(err, "lock(...) failed %d (%s)", err, strerror(-err));
105    return err;
106}
107
108status_t GraphicBufferMapper::unlock(buffer_handle_t handle)
109{
110    ATRACE_CALL();
111    status_t err;
112
113    err = mAllocMod->unlock(mAllocMod, handle);
114
115    ALOGW_IF(err, "unlock(...) failed %d (%s)", err, strerror(-err));
116    return err;
117}
118
119status_t GraphicBufferMapper::lockAsync(buffer_handle_t handle,
120        uint32_t usage, const Rect& bounds, void** vaddr, int fenceFd)
121{
122    ATRACE_CALL();
123    status_t err;
124
125    if (mAllocMod->common.module_api_version >= GRALLOC_MODULE_API_VERSION_0_3) {
126        err = mAllocMod->lockAsync(mAllocMod, handle, static_cast<int>(usage),
127                bounds.left, bounds.top, bounds.width(), bounds.height(),
128                vaddr, fenceFd);
129    } else {
130        sync_wait(fenceFd, -1);
131        close(fenceFd);
132        err = mAllocMod->lock(mAllocMod, handle, static_cast<int>(usage),
133                bounds.left, bounds.top, bounds.width(), bounds.height(),
134                vaddr);
135    }
136
137    ALOGW_IF(err, "lockAsync(...) failed %d (%s)", err, strerror(-err));
138    return err;
139}
140
141status_t GraphicBufferMapper::lockAsyncYCbCr(buffer_handle_t handle,
142        uint32_t usage, const Rect& bounds, android_ycbcr *ycbcr, int fenceFd)
143{
144    ATRACE_CALL();
145    status_t err;
146
147    if (mAllocMod->common.module_api_version >= GRALLOC_MODULE_API_VERSION_0_3) {
148        err = mAllocMod->lockAsync_ycbcr(mAllocMod, handle,
149                static_cast<int>(usage), bounds.left, bounds.top,
150                bounds.width(), bounds.height(), ycbcr, fenceFd);
151    } else {
152        sync_wait(fenceFd, -1);
153        close(fenceFd);
154        err = mAllocMod->lock_ycbcr(mAllocMod, handle, static_cast<int>(usage),
155                bounds.left, bounds.top, bounds.width(), bounds.height(),
156                ycbcr);
157    }
158
159    ALOGW_IF(err, "lock(...) failed %d (%s)", err, strerror(-err));
160    return err;
161}
162
163status_t GraphicBufferMapper::unlockAsync(buffer_handle_t handle, int *fenceFd)
164{
165    ATRACE_CALL();
166    status_t err;
167
168    if (mAllocMod->common.module_api_version >= GRALLOC_MODULE_API_VERSION_0_3) {
169        err = mAllocMod->unlockAsync(mAllocMod, handle, fenceFd);
170    } else {
171        *fenceFd = -1;
172        err = mAllocMod->unlock(mAllocMod, handle);
173    }
174
175    ALOGW_IF(err, "unlockAsync(...) failed %d (%s)", err, strerror(-err));
176    return err;
177}
178
179// ---------------------------------------------------------------------------
180}; // namespace android
181