158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)/*
258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) * Copyright (C) 2007 The Android Open Source Project
358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) *
458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) * Licensed under the Apache License, Version 2.0 (the "License");
558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) * you may not use this file except in compliance with the License.
658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) * You may obtain a copy of the License at
758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) *
858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) *      http://www.apache.org/licenses/LICENSE-2.0
958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) *
1058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) * Unless required by applicable law or agreed to in writing, software
1158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) * distributed under the License is distributed on an "AS IS" BASIS,
1258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) * See the License for the specific language governing permissions and
1458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) * limitations under the License.
1558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) */
1658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
1758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#define LOG_TAG "GraphicBufferMapper"
1858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#define ATRACE_TAG ATRACE_TAG_GRAPHICS
1958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
2058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#include <stdint.h>
2158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#include <errno.h>
22a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch
23a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch#include <sync/sync.h>
2458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
2558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#include <utils/Errors.h>
2658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#include <utils/Log.h>
2758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#include <utils/Trace.h>
2858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
2958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#include <ui/GraphicBufferMapper.h>
3058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#include <ui/Rect.h>
3158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
3258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#include <hardware/gralloc.h>
3358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
3458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
3558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)namespace android {
3658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)// ---------------------------------------------------------------------------
3758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
3858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)ANDROID_SINGLETON_STATIC_INSTANCE( GraphicBufferMapper )
3958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
4058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)GraphicBufferMapper::GraphicBufferMapper()
4158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    : mAllocMod(0)
4258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles){
4358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    hw_module_t const* module;
4458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    int err = hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &module);
4558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    ALOGE_IF(err, "FATAL: can't find the %s module", GRALLOC_HARDWARE_MODULE_ID);
4658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    if (err == 0) {
4758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)        mAllocMod = (gralloc_module_t const *)module;
4858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    }
4958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)}
5058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
5158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)status_t GraphicBufferMapper::registerBuffer(buffer_handle_t handle)
5258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles){
5358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    ATRACE_CALL();
5458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    status_t err;
5558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
5658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    err = mAllocMod->registerBuffer(mAllocMod, handle);
5758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
5858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    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    if (mAllocMod->lock_ycbcr == NULL) {
96        return -EINVAL; // do not log failure
97    }
98
99    err = mAllocMod->lock_ycbcr(mAllocMod, handle, usage,
100            bounds.left, bounds.top, bounds.width(), bounds.height(),
101            ycbcr);
102
103    ALOGW_IF(err, "lock(...) failed %d (%s)", err, strerror(-err));
104    return err;
105}
106
107status_t GraphicBufferMapper::unlock(buffer_handle_t handle)
108{
109    ATRACE_CALL();
110    status_t err;
111
112    err = mAllocMod->unlock(mAllocMod, handle);
113
114    ALOGW_IF(err, "unlock(...) failed %d (%s)", err, strerror(-err));
115    return err;
116}
117
118status_t GraphicBufferMapper::lockAsync(buffer_handle_t handle,
119        int usage, const Rect& bounds, void** vaddr, int fenceFd)
120{
121    ATRACE_CALL();
122    status_t err;
123
124    if (mAllocMod->common.module_api_version >= GRALLOC_MODULE_API_VERSION_0_3) {
125        err = mAllocMod->lockAsync(mAllocMod, handle, usage,
126                bounds.left, bounds.top, bounds.width(), bounds.height(),
127                vaddr, fenceFd);
128    } else {
129        sync_wait(fenceFd, -1);
130        close(fenceFd);
131        err = mAllocMod->lock(mAllocMod, handle, usage,
132                bounds.left, bounds.top, bounds.width(), bounds.height(),
133                vaddr);
134    }
135
136    ALOGW_IF(err, "lockAsync(...) failed %d (%s)", err, strerror(-err));
137    return err;
138}
139
140status_t GraphicBufferMapper::lockAsyncYCbCr(buffer_handle_t handle,
141        int usage, const Rect& bounds, android_ycbcr *ycbcr, int fenceFd)
142{
143    ATRACE_CALL();
144    status_t err;
145
146    if (mAllocMod->common.module_api_version >= GRALLOC_MODULE_API_VERSION_0_3
147            && mAllocMod->lockAsync_ycbcr != NULL) {
148        err = mAllocMod->lockAsync_ycbcr(mAllocMod, handle, usage,
149                bounds.left, bounds.top, bounds.width(), bounds.height(),
150                ycbcr, fenceFd);
151    } else if (mAllocMod->lock_ycbcr != NULL) {
152        sync_wait(fenceFd, -1);
153        close(fenceFd);
154        err = mAllocMod->lock_ycbcr(mAllocMod, handle, usage,
155                bounds.left, bounds.top, bounds.width(), bounds.height(),
156                ycbcr);
157    } else {
158        return -EINVAL; // do not log failure
159    }
160
161    ALOGW_IF(err, "lock(...) failed %d (%s)", err, strerror(-err));
162    return err;
163}
164
165status_t GraphicBufferMapper::unlockAsync(buffer_handle_t handle, int *fenceFd)
166{
167    ATRACE_CALL();
168    status_t err;
169
170    if (mAllocMod->common.module_api_version >= GRALLOC_MODULE_API_VERSION_0_3) {
171        err = mAllocMod->unlockAsync(mAllocMod, handle, fenceFd);
172    } else {
173        *fenceFd = -1;
174        err = mAllocMod->unlock(mAllocMod, handle);
175    }
176
177    ALOGW_IF(err, "unlockAsync(...) failed %d (%s)", err, strerror(-err));
178    return err;
179}
180
181// ---------------------------------------------------------------------------
182}; // namespace android
183