blob: e949b0c9b150d98eb6d39abf492adae08d066a46 [file] [log] [blame]
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001/*
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
Mathias Agopian3330b202009-10-05 17:07:12 -070017#define LOG_TAG "GraphicBufferMapper"
Mathias Agopiancf563192012-02-29 20:43:29 -080018#define ATRACE_TAG ATRACE_TAG_GRAPHICS
Mathias Agopian076b1cc2009-04-10 14:24:30 -070019
20#include <stdint.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070021#include <errno.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070022
Francis Hart8f396012014-04-01 15:30:53 +030023#include <sync/sync.h>
24
Mathias Agopian076b1cc2009-04-10 14:24:30 -070025#include <utils/Errors.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070026#include <utils/Log.h>
Mathias Agopiancf563192012-02-29 20:43:29 -080027#include <utils/Trace.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070028
Mathias Agopian3330b202009-10-05 17:07:12 -070029#include <ui/GraphicBufferMapper.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070030#include <ui/Rect.h>
31
Mathias Agopian076b1cc2009-04-10 14:24:30 -070032#include <hardware/gralloc.h>
33
Mathias Agopian8b765b72009-04-10 20:34:46 -070034
Mathias Agopian076b1cc2009-04-10 14:24:30 -070035namespace android {
36// ---------------------------------------------------------------------------
37
Mathias Agopian3330b202009-10-05 17:07:12 -070038ANDROID_SINGLETON_STATIC_INSTANCE( GraphicBufferMapper )
Mathias Agopian4243e662009-04-15 18:34:24 -070039
Mathias Agopian3330b202009-10-05 17:07:12 -070040GraphicBufferMapper::GraphicBufferMapper()
Mathias Agopian076b1cc2009-04-10 14:24:30 -070041 : mAllocMod(0)
42{
43 hw_module_t const* module;
44 int err = hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &module);
Steve Blocke6f43dd2012-01-06 19:20:56 +000045 ALOGE_IF(err, "FATAL: can't find the %s module", GRALLOC_HARDWARE_MODULE_ID);
Mathias Agopian076b1cc2009-04-10 14:24:30 -070046 if (err == 0) {
47 mAllocMod = (gralloc_module_t const *)module;
48 }
49}
50
Mathias Agopian3330b202009-10-05 17:07:12 -070051status_t GraphicBufferMapper::registerBuffer(buffer_handle_t handle)
Mathias Agopian076b1cc2009-04-10 14:24:30 -070052{
Mathias Agopiancf563192012-02-29 20:43:29 -080053 ATRACE_CALL();
Mathias Agopianb26af232009-10-05 18:19:57 -070054 status_t err;
Mathias Agopian0a757812010-12-08 16:40:01 -080055
56 err = mAllocMod->registerBuffer(mAllocMod, handle);
57
Steve Block32397c12012-01-05 23:22:43 +000058 ALOGW_IF(err, "registerBuffer(%p) failed %d (%s)",
Mathias Agopian0926f502009-05-04 14:17:04 -070059 handle, err, strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -070060 return err;
61}
62
Mathias Agopian3330b202009-10-05 17:07:12 -070063status_t GraphicBufferMapper::unregisterBuffer(buffer_handle_t handle)
Mathias Agopian076b1cc2009-04-10 14:24:30 -070064{
Mathias Agopiancf563192012-02-29 20:43:29 -080065 ATRACE_CALL();
Mathias Agopianb26af232009-10-05 18:19:57 -070066 status_t err;
Mathias Agopian0a757812010-12-08 16:40:01 -080067
68 err = mAllocMod->unregisterBuffer(mAllocMod, handle);
69
Steve Block32397c12012-01-05 23:22:43 +000070 ALOGW_IF(err, "unregisterBuffer(%p) failed %d (%s)",
Mathias Agopian0926f502009-05-04 14:17:04 -070071 handle, err, strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -070072 return err;
73}
74
Mathias Agopian3330b202009-10-05 17:07:12 -070075status_t GraphicBufferMapper::lock(buffer_handle_t handle,
Mathias Agopian0926f502009-05-04 14:17:04 -070076 int usage, const Rect& bounds, void** vaddr)
Mathias Agopian076b1cc2009-04-10 14:24:30 -070077{
Mathias Agopiancf563192012-02-29 20:43:29 -080078 ATRACE_CALL();
Mathias Agopianb26af232009-10-05 18:19:57 -070079 status_t err;
Mathias Agopian0a757812010-12-08 16:40:01 -080080
81 err = mAllocMod->lock(mAllocMod, handle, usage,
82 bounds.left, bounds.top, bounds.width(), bounds.height(),
83 vaddr);
84
Steve Block32397c12012-01-05 23:22:43 +000085 ALOGW_IF(err, "lock(...) failed %d (%s)", err, strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -070086 return err;
87}
88
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -070089status_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
Lajos Molnar1f9f71e2015-01-28 16:16:40 -080095 if (mAllocMod->lock_ycbcr == NULL) {
96 return -EINVAL; // do not log failure
97 }
98
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -070099 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
Mathias Agopian3330b202009-10-05 17:07:12 -0700107status_t GraphicBufferMapper::unlock(buffer_handle_t handle)
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700108{
Mathias Agopiancf563192012-02-29 20:43:29 -0800109 ATRACE_CALL();
Mathias Agopianb26af232009-10-05 18:19:57 -0700110 status_t err;
Mathias Agopian0a757812010-12-08 16:40:01 -0800111
112 err = mAllocMod->unlock(mAllocMod, handle);
113
Steve Block32397c12012-01-05 23:22:43 +0000114 ALOGW_IF(err, "unlock(...) failed %d (%s)", err, strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700115 return err;
116}
117
Francis Hart8f396012014-04-01 15:30:53 +0300118status_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
Lajos Molnar1f9f71e2015-01-28 16:16:40 -0800146 if (mAllocMod->common.module_api_version >= GRALLOC_MODULE_API_VERSION_0_3
147 && mAllocMod->lockAsync_ycbcr != NULL) {
Francis Hart8f396012014-04-01 15:30:53 +0300148 err = mAllocMod->lockAsync_ycbcr(mAllocMod, handle, usage,
149 bounds.left, bounds.top, bounds.width(), bounds.height(),
150 ycbcr, fenceFd);
Lajos Molnar1f9f71e2015-01-28 16:16:40 -0800151 } else if (mAllocMod->lock_ycbcr != NULL) {
Francis Hart8f396012014-04-01 15:30:53 +0300152 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);
Lajos Molnar1f9f71e2015-01-28 16:16:40 -0800157 } else {
158 return -EINVAL; // do not log failure
Francis Hart8f396012014-04-01 15:30:53 +0300159 }
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
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700181// ---------------------------------------------------------------------------
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700182}; // namespace android