blob: 31bfb2d0fcbb7d478ad30990cce24db3f253b113 [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 {
134 sync_wait(fenceFd, -1);
135 close(fenceFd);
Dan Stozad3182402014-11-17 12:03:59 -0800136 err = mAllocMod->lock(mAllocMod, handle, static_cast<int>(usage),
Francis Hart8f396012014-04-01 15:30:53 +0300137 bounds.left, bounds.top, bounds.width(), bounds.height(),
138 vaddr);
139 }
140
141 ALOGW_IF(err, "lockAsync(...) failed %d (%s)", err, strerror(-err));
142 return err;
143}
144
145status_t GraphicBufferMapper::lockAsyncYCbCr(buffer_handle_t handle,
Dan Stozad3182402014-11-17 12:03:59 -0800146 uint32_t usage, const Rect& bounds, android_ycbcr *ycbcr, int fenceFd)
Francis Hart8f396012014-04-01 15:30:53 +0300147{
148 ATRACE_CALL();
149 status_t err;
150
Lajos Molnar1f9f71e2015-01-28 16:16:40 -0800151 if (mAllocMod->common.module_api_version >= GRALLOC_MODULE_API_VERSION_0_3
152 && mAllocMod->lockAsync_ycbcr != NULL) {
Dan Stozad3182402014-11-17 12:03:59 -0800153 err = mAllocMod->lockAsync_ycbcr(mAllocMod, handle,
154 static_cast<int>(usage), bounds.left, bounds.top,
155 bounds.width(), bounds.height(), ycbcr, fenceFd);
Lajos Molnar1f9f71e2015-01-28 16:16:40 -0800156 } else if (mAllocMod->lock_ycbcr != NULL) {
Francis Hart8f396012014-04-01 15:30:53 +0300157 sync_wait(fenceFd, -1);
158 close(fenceFd);
Dan Stozad3182402014-11-17 12:03:59 -0800159 err = mAllocMod->lock_ycbcr(mAllocMod, handle, static_cast<int>(usage),
Francis Hart8f396012014-04-01 15:30:53 +0300160 bounds.left, bounds.top, bounds.width(), bounds.height(),
161 ycbcr);
Lajos Molnar1f9f71e2015-01-28 16:16:40 -0800162 } else {
163 return -EINVAL; // do not log failure
Francis Hart8f396012014-04-01 15:30:53 +0300164 }
165
166 ALOGW_IF(err, "lock(...) failed %d (%s)", err, strerror(-err));
167 return err;
168}
169
170status_t GraphicBufferMapper::unlockAsync(buffer_handle_t handle, int *fenceFd)
171{
172 ATRACE_CALL();
173 status_t err;
174
175 if (mAllocMod->common.module_api_version >= GRALLOC_MODULE_API_VERSION_0_3) {
176 err = mAllocMod->unlockAsync(mAllocMod, handle, fenceFd);
177 } else {
178 *fenceFd = -1;
179 err = mAllocMod->unlock(mAllocMod, handle);
180 }
181
182 ALOGW_IF(err, "unlockAsync(...) failed %d (%s)", err, strerror(-err));
183 return err;
184}
185
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700186// ---------------------------------------------------------------------------
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700187}; // namespace android