blob: 90a1c1110d259479a4a55aeb03382762ec36ac12 [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
Dan Stozad3182402014-11-17 12:03:59 -080023// 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"
Francis Hart8f396012014-04-01 15:30:53 +030027#include <sync/sync.h>
Dan Stozad3182402014-11-17 12:03:59 -080028#pragma clang diagnostic pop
Francis Hart8f396012014-04-01 15:30:53 +030029
Mathias Agopian076b1cc2009-04-10 14:24:30 -070030#include <utils/Errors.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070031#include <utils/Log.h>
Mathias Agopiancf563192012-02-29 20:43:29 -080032#include <utils/Trace.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070033
Mathias Agopian3330b202009-10-05 17:07:12 -070034#include <ui/GraphicBufferMapper.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070035#include <ui/Rect.h>
36
Mathias Agopian076b1cc2009-04-10 14:24:30 -070037#include <hardware/gralloc.h>
38
Mathias Agopian8b765b72009-04-10 20:34:46 -070039
Mathias Agopian076b1cc2009-04-10 14:24:30 -070040namespace android {
41// ---------------------------------------------------------------------------
42
Mathias Agopian3330b202009-10-05 17:07:12 -070043ANDROID_SINGLETON_STATIC_INSTANCE( GraphicBufferMapper )
Mathias Agopian4243e662009-04-15 18:34:24 -070044
Mathias Agopian3330b202009-10-05 17:07:12 -070045GraphicBufferMapper::GraphicBufferMapper()
Mathias Agopian076b1cc2009-04-10 14:24:30 -070046 : mAllocMod(0)
47{
48 hw_module_t const* module;
49 int err = hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &module);
Steve Blocke6f43dd2012-01-06 19:20:56 +000050 ALOGE_IF(err, "FATAL: can't find the %s module", GRALLOC_HARDWARE_MODULE_ID);
Mathias Agopian076b1cc2009-04-10 14:24:30 -070051 if (err == 0) {
Dan Stozad3182402014-11-17 12:03:59 -080052 mAllocMod = reinterpret_cast<gralloc_module_t const *>(module);
Mathias Agopian076b1cc2009-04-10 14:24:30 -070053 }
54}
55
Mathias Agopian3330b202009-10-05 17:07:12 -070056status_t GraphicBufferMapper::registerBuffer(buffer_handle_t handle)
Mathias Agopian076b1cc2009-04-10 14:24:30 -070057{
Mathias Agopiancf563192012-02-29 20:43:29 -080058 ATRACE_CALL();
Mathias Agopianb26af232009-10-05 18:19:57 -070059 status_t err;
Mathias Agopian0a757812010-12-08 16:40:01 -080060
61 err = mAllocMod->registerBuffer(mAllocMod, handle);
62
Steve Block32397c12012-01-05 23:22:43 +000063 ALOGW_IF(err, "registerBuffer(%p) failed %d (%s)",
Mathias Agopian0926f502009-05-04 14:17:04 -070064 handle, err, strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -070065 return err;
66}
67
Mathias Agopian3330b202009-10-05 17:07:12 -070068status_t GraphicBufferMapper::unregisterBuffer(buffer_handle_t handle)
Mathias Agopian076b1cc2009-04-10 14:24:30 -070069{
Mathias Agopiancf563192012-02-29 20:43:29 -080070 ATRACE_CALL();
Mathias Agopianb26af232009-10-05 18:19:57 -070071 status_t err;
Mathias Agopian0a757812010-12-08 16:40:01 -080072
73 err = mAllocMod->unregisterBuffer(mAllocMod, handle);
74
Steve Block32397c12012-01-05 23:22:43 +000075 ALOGW_IF(err, "unregisterBuffer(%p) failed %d (%s)",
Mathias Agopian0926f502009-05-04 14:17:04 -070076 handle, err, strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -070077 return err;
78}
79
Dan Stozad3182402014-11-17 12:03:59 -080080status_t GraphicBufferMapper::lock(buffer_handle_t handle,
81 uint32_t usage, const Rect& bounds, void** vaddr)
Mathias Agopian076b1cc2009-04-10 14:24:30 -070082{
Mathias Agopiancf563192012-02-29 20:43:29 -080083 ATRACE_CALL();
Mathias Agopianb26af232009-10-05 18:19:57 -070084 status_t err;
Mathias Agopian0a757812010-12-08 16:40:01 -080085
Dan Stozad3182402014-11-17 12:03:59 -080086 err = mAllocMod->lock(mAllocMod, handle, static_cast<int>(usage),
Mathias Agopian0a757812010-12-08 16:40:01 -080087 bounds.left, bounds.top, bounds.width(), bounds.height(),
88 vaddr);
89
Steve Block32397c12012-01-05 23:22:43 +000090 ALOGW_IF(err, "lock(...) failed %d (%s)", err, strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -070091 return err;
92}
93
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -070094status_t GraphicBufferMapper::lockYCbCr(buffer_handle_t handle,
Dan Stozad3182402014-11-17 12:03:59 -080095 uint32_t usage, const Rect& bounds, android_ycbcr *ycbcr)
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -070096{
97 ATRACE_CALL();
98 status_t err;
99
Lajos Molnar1f9f71e2015-01-28 16:16:40 -0800100 if (mAllocMod->lock_ycbcr == NULL) {
101 return -EINVAL; // do not log failure
102 }
103
Dan Stozad3182402014-11-17 12:03:59 -0800104 err = mAllocMod->lock_ycbcr(mAllocMod, handle, static_cast<int>(usage),
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700105 bounds.left, bounds.top, bounds.width(), bounds.height(),
106 ycbcr);
107
108 ALOGW_IF(err, "lock(...) failed %d (%s)", err, strerror(-err));
109 return err;
110}
111
Mathias Agopian3330b202009-10-05 17:07:12 -0700112status_t GraphicBufferMapper::unlock(buffer_handle_t handle)
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700113{
Mathias Agopiancf563192012-02-29 20:43:29 -0800114 ATRACE_CALL();
Mathias Agopianb26af232009-10-05 18:19:57 -0700115 status_t err;
Mathias Agopian0a757812010-12-08 16:40:01 -0800116
117 err = mAllocMod->unlock(mAllocMod, handle);
118
Steve Block32397c12012-01-05 23:22:43 +0000119 ALOGW_IF(err, "unlock(...) failed %d (%s)", err, strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700120 return err;
121}
122
Francis Hart8f396012014-04-01 15:30:53 +0300123status_t GraphicBufferMapper::lockAsync(buffer_handle_t handle,
Dan Stozad3182402014-11-17 12:03:59 -0800124 uint32_t usage, const Rect& bounds, void** vaddr, int fenceFd)
Francis Hart8f396012014-04-01 15:30:53 +0300125{
126 ATRACE_CALL();
127 status_t err;
128
129 if (mAllocMod->common.module_api_version >= GRALLOC_MODULE_API_VERSION_0_3) {
Dan Stozad3182402014-11-17 12:03:59 -0800130 err = mAllocMod->lockAsync(mAllocMod, handle, static_cast<int>(usage),
Francis Hart8f396012014-04-01 15:30:53 +0300131 bounds.left, bounds.top, bounds.width(), bounds.height(),
132 vaddr, fenceFd);
133 } else {
Taiju Tsuikidcfe91e2015-04-30 22:13:14 +0900134 if (fenceFd >= 0) {
135 sync_wait(fenceFd, -1);
136 close(fenceFd);
137 }
Dan Stozad3182402014-11-17 12:03:59 -0800138 err = mAllocMod->lock(mAllocMod, handle, static_cast<int>(usage),
Francis Hart8f396012014-04-01 15:30:53 +0300139 bounds.left, bounds.top, bounds.width(), bounds.height(),
140 vaddr);
141 }
142
143 ALOGW_IF(err, "lockAsync(...) failed %d (%s)", err, strerror(-err));
144 return err;
145}
146
147status_t GraphicBufferMapper::lockAsyncYCbCr(buffer_handle_t handle,
Dan Stozad3182402014-11-17 12:03:59 -0800148 uint32_t usage, const Rect& bounds, android_ycbcr *ycbcr, int fenceFd)
Francis Hart8f396012014-04-01 15:30:53 +0300149{
150 ATRACE_CALL();
151 status_t err;
152
Lajos Molnar1f9f71e2015-01-28 16:16:40 -0800153 if (mAllocMod->common.module_api_version >= GRALLOC_MODULE_API_VERSION_0_3
154 && mAllocMod->lockAsync_ycbcr != NULL) {
Dan Stozad3182402014-11-17 12:03:59 -0800155 err = mAllocMod->lockAsync_ycbcr(mAllocMod, handle,
156 static_cast<int>(usage), bounds.left, bounds.top,
157 bounds.width(), bounds.height(), ycbcr, fenceFd);
Lajos Molnar1f9f71e2015-01-28 16:16:40 -0800158 } else if (mAllocMod->lock_ycbcr != NULL) {
Taiju Tsuikidcfe91e2015-04-30 22:13:14 +0900159 if (fenceFd >= 0) {
160 sync_wait(fenceFd, -1);
161 close(fenceFd);
162 }
Dan Stozad3182402014-11-17 12:03:59 -0800163 err = mAllocMod->lock_ycbcr(mAllocMod, handle, static_cast<int>(usage),
Francis Hart8f396012014-04-01 15:30:53 +0300164 bounds.left, bounds.top, bounds.width(), bounds.height(),
165 ycbcr);
Lajos Molnar1f9f71e2015-01-28 16:16:40 -0800166 } else {
Taiju Tsuikic7263fb2015-04-30 22:15:33 +0900167 if (fenceFd >= 0) {
168 close(fenceFd);
169 }
Lajos Molnar1f9f71e2015-01-28 16:16:40 -0800170 return -EINVAL; // do not log failure
Francis Hart8f396012014-04-01 15:30:53 +0300171 }
172
173 ALOGW_IF(err, "lock(...) failed %d (%s)", err, strerror(-err));
174 return err;
175}
176
177status_t GraphicBufferMapper::unlockAsync(buffer_handle_t handle, int *fenceFd)
178{
179 ATRACE_CALL();
180 status_t err;
181
182 if (mAllocMod->common.module_api_version >= GRALLOC_MODULE_API_VERSION_0_3) {
183 err = mAllocMod->unlockAsync(mAllocMod, handle, fenceFd);
184 } else {
185 *fenceFd = -1;
186 err = mAllocMod->unlock(mAllocMod, handle);
187 }
188
189 ALOGW_IF(err, "unlockAsync(...) failed %d (%s)", err, strerror(-err));
190 return err;
191}
192
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700193// ---------------------------------------------------------------------------
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700194}; // namespace android