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