blob: 320b6c03d4e7fa48f7176d775a9a3264587b068f [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
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
Mathias Agopian3330b202009-10-05 17:07:12 -0700103status_t GraphicBufferMapper::unlock(buffer_handle_t handle)
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700104{
Mathias Agopiancf563192012-02-29 20:43:29 -0800105 ATRACE_CALL();
Mathias Agopianb26af232009-10-05 18:19:57 -0700106 status_t err;
Mathias Agopian0a757812010-12-08 16:40:01 -0800107
108 err = mAllocMod->unlock(mAllocMod, handle);
109
Steve Block32397c12012-01-05 23:22:43 +0000110 ALOGW_IF(err, "unlock(...) failed %d (%s)", err, strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700111 return err;
112}
113
Francis Hart8f396012014-04-01 15:30:53 +0300114status_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
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700174// ---------------------------------------------------------------------------
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700175}; // namespace android