blob: 01acdc8b4aedc28ceeb40e5a40ee87ee6fa18589 [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
Dan Stozad3182402014-11-17 12:03:59 -0800100 err = mAllocMod->lock_ycbcr(mAllocMod, handle, static_cast<int>(usage),
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700101 bounds.left, bounds.top, bounds.width(), bounds.height(),
102 ycbcr);
103
104 ALOGW_IF(err, "lock(...) failed %d (%s)", err, strerror(-err));
105 return err;
106}
107
Mathias Agopian3330b202009-10-05 17:07:12 -0700108status_t GraphicBufferMapper::unlock(buffer_handle_t handle)
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700109{
Mathias Agopiancf563192012-02-29 20:43:29 -0800110 ATRACE_CALL();
Mathias Agopianb26af232009-10-05 18:19:57 -0700111 status_t err;
Mathias Agopian0a757812010-12-08 16:40:01 -0800112
113 err = mAllocMod->unlock(mAllocMod, handle);
114
Steve Block32397c12012-01-05 23:22:43 +0000115 ALOGW_IF(err, "unlock(...) failed %d (%s)", err, strerror(-err));
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700116 return err;
117}
118
Francis Hart8f396012014-04-01 15:30:53 +0300119status_t GraphicBufferMapper::lockAsync(buffer_handle_t handle,
Dan Stozad3182402014-11-17 12:03:59 -0800120 uint32_t usage, const Rect& bounds, void** vaddr, int fenceFd)
Francis Hart8f396012014-04-01 15:30:53 +0300121{
122 ATRACE_CALL();
123 status_t err;
124
125 if (mAllocMod->common.module_api_version >= GRALLOC_MODULE_API_VERSION_0_3) {
Dan Stozad3182402014-11-17 12:03:59 -0800126 err = mAllocMod->lockAsync(mAllocMod, handle, static_cast<int>(usage),
Francis Hart8f396012014-04-01 15:30:53 +0300127 bounds.left, bounds.top, bounds.width(), bounds.height(),
128 vaddr, fenceFd);
129 } else {
130 sync_wait(fenceFd, -1);
131 close(fenceFd);
Dan Stozad3182402014-11-17 12:03:59 -0800132 err = mAllocMod->lock(mAllocMod, handle, static_cast<int>(usage),
Francis Hart8f396012014-04-01 15:30:53 +0300133 bounds.left, bounds.top, bounds.width(), bounds.height(),
134 vaddr);
135 }
136
137 ALOGW_IF(err, "lockAsync(...) failed %d (%s)", err, strerror(-err));
138 return err;
139}
140
141status_t GraphicBufferMapper::lockAsyncYCbCr(buffer_handle_t handle,
Dan Stozad3182402014-11-17 12:03:59 -0800142 uint32_t usage, const Rect& bounds, android_ycbcr *ycbcr, int fenceFd)
Francis Hart8f396012014-04-01 15:30:53 +0300143{
144 ATRACE_CALL();
145 status_t err;
146
147 if (mAllocMod->common.module_api_version >= GRALLOC_MODULE_API_VERSION_0_3) {
Dan Stozad3182402014-11-17 12:03:59 -0800148 err = mAllocMod->lockAsync_ycbcr(mAllocMod, handle,
149 static_cast<int>(usage), bounds.left, bounds.top,
150 bounds.width(), bounds.height(), ycbcr, fenceFd);
Francis Hart8f396012014-04-01 15:30:53 +0300151 } else {
152 sync_wait(fenceFd, -1);
153 close(fenceFd);
Dan Stozad3182402014-11-17 12:03:59 -0800154 err = mAllocMod->lock_ycbcr(mAllocMod, handle, static_cast<int>(usage),
Francis Hart8f396012014-04-01 15:30:53 +0300155 bounds.left, bounds.top, bounds.width(), bounds.height(),
156 ycbcr);
157 }
158
159 ALOGW_IF(err, "lock(...) failed %d (%s)", err, strerror(-err));
160 return err;
161}
162
163status_t GraphicBufferMapper::unlockAsync(buffer_handle_t handle, int *fenceFd)
164{
165 ATRACE_CALL();
166 status_t err;
167
168 if (mAllocMod->common.module_api_version >= GRALLOC_MODULE_API_VERSION_0_3) {
169 err = mAllocMod->unlockAsync(mAllocMod, handle, fenceFd);
170 } else {
171 *fenceFd = -1;
172 err = mAllocMod->unlock(mAllocMod, handle);
173 }
174
175 ALOGW_IF(err, "unlockAsync(...) failed %d (%s)", err, strerror(-err));
176 return err;
177}
178
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700179// ---------------------------------------------------------------------------
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700180}; // namespace android