blob: 8aadb4a2f6bb785812aafbbc9dc897b46682a72b [file] [log] [blame]
Mathias Agopiana8a75162009-04-10 14:24:31 -07001/*
2 * Copyright (C) 2008 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
17#include <limits.h>
18#include <errno.h>
19#include <pthread.h>
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -070020#include <unistd.h>
Marco Nelissena4b587c2009-07-07 09:29:00 -070021#include <string.h>
Mathias Agopiana8a75162009-04-10 14:24:31 -070022
23#include <sys/mman.h>
24#include <sys/stat.h>
25#include <sys/types.h>
26
27#include <cutils/log.h>
28#include <cutils/atomic.h>
29
30#include <hardware/hardware.h>
31#include <hardware/gralloc.h>
32
33#include "gralloc_priv.h"
34
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -070035
Marco Nelissena4b587c2009-07-07 09:29:00 -070036/* desktop Linux needs a little help with gettid() */
37#if defined(ARCH_X86) && !defined(HAVE_ANDROID_OS)
38#define __KERNEL__
39# include <linux/unistd.h>
40pid_t gettid() { return syscall(__NR_gettid);}
41#undef __KERNEL__
42#endif
43
Mathias Agopiana8a75162009-04-10 14:24:31 -070044/*****************************************************************************/
45
Mathias Agopian51156652009-06-09 18:55:49 -070046static int gralloc_map(gralloc_module_t const* module,
Mathias Agopiana8a75162009-04-10 14:24:31 -070047 buffer_handle_t handle,
48 void** vaddr)
49{
Mathias Agopian51156652009-06-09 18:55:49 -070050 private_handle_t* hnd = (private_handle_t*)handle;
51 if (!(hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER)) {
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -070052 size_t size = hnd->size;
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -070053 void* mappedAddress = mmap(0, size,
54 PROT_READ|PROT_WRITE, MAP_SHARED, hnd->fd, 0);
Mathias Agopian51156652009-06-09 18:55:49 -070055 if (mappedAddress == MAP_FAILED) {
Steve Block60d056b2012-01-08 10:17:53 +000056 ALOGE("Could not mmap %s", strerror(errno));
Mathias Agopian51156652009-06-09 18:55:49 -070057 return -errno;
58 }
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -070059 hnd->base = intptr_t(mappedAddress) + hnd->offset;
Steve Blockcee85012011-12-20 16:25:12 +000060 //ALOGD("gralloc_map() succeeded fd=%d, off=%d, size=%d, vaddr=%p",
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -070061 // hnd->fd, hnd->offset, hnd->size, mappedAddress);
Mathias Agopiana8a75162009-04-10 14:24:31 -070062 }
Mathias Agopian51156652009-06-09 18:55:49 -070063 *vaddr = (void*)hnd->base;
64 return 0;
Mathias Agopiana8a75162009-04-10 14:24:31 -070065}
66
Mathias Agopian51156652009-06-09 18:55:49 -070067static int gralloc_unmap(gralloc_module_t const* module,
Mathias Agopiana8a75162009-04-10 14:24:31 -070068 buffer_handle_t handle)
69{
Mathias Agopian51156652009-06-09 18:55:49 -070070 private_handle_t* hnd = (private_handle_t*)handle;
71 if (!(hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER)) {
Mathias Agopianbd80b382009-07-07 17:53:43 -070072 void* base = (void*)hnd->base;
73 size_t size = hnd->size;
Steve Blockcee85012011-12-20 16:25:12 +000074 //ALOGD("unmapping from %p, size=%d", base, size);
Mathias Agopianbd80b382009-07-07 17:53:43 -070075 if (munmap(base, size) < 0) {
Steve Block60d056b2012-01-08 10:17:53 +000076 ALOGE("Could not unmap %s", strerror(errno));
Mathias Agopian51156652009-06-09 18:55:49 -070077 }
Mathias Agopiana8a75162009-04-10 14:24:31 -070078 }
Mathias Agopian51156652009-06-09 18:55:49 -070079 hnd->base = 0;
80 return 0;
Mathias Agopiana8a75162009-04-10 14:24:31 -070081}
82
Mathias Agopian51156652009-06-09 18:55:49 -070083/*****************************************************************************/
84
Jesse Hallc71b6ca2013-03-28 11:04:16 -070085static pthread_mutex_t sMapLock = PTHREAD_MUTEX_INITIALIZER;
Mathias Agopian51156652009-06-09 18:55:49 -070086
87/*****************************************************************************/
88
Mathias Agopian988b8bd2009-05-04 14:26:56 -070089int gralloc_register_buffer(gralloc_module_t const* module,
90 buffer_handle_t handle)
91{
92 if (private_handle_t::validate(handle) < 0)
93 return -EINVAL;
Mathias Agopian51156652009-06-09 18:55:49 -070094
Jesse Hallc71b6ca2013-03-28 11:04:16 -070095 void *vaddr;
96 return gralloc_map(module, handle, &vaddr);
Mathias Agopian988b8bd2009-05-04 14:26:56 -070097}
98
99int gralloc_unregister_buffer(gralloc_module_t const* module,
100 buffer_handle_t handle)
101{
102 if (private_handle_t::validate(handle) < 0)
103 return -EINVAL;
104
Mathias Agopianf96b2062009-12-14 18:27:09 -0800105 private_handle_t* hnd = (private_handle_t*)handle;
Jesse Hallc71b6ca2013-03-28 11:04:16 -0700106 if (hnd->base)
107 gralloc_unmap(module, handle);
108
Mathias Agopian988b8bd2009-05-04 14:26:56 -0700109 return 0;
110}
Mathias Agopiana8a75162009-04-10 14:24:31 -0700111
Mathias Agopianf96b2062009-12-14 18:27:09 -0800112int mapBuffer(gralloc_module_t const* module,
113 private_handle_t* hnd)
114{
115 void* vaddr;
116 return gralloc_map(module, hnd, &vaddr);
117}
118
Mathias Agopianbd80b382009-07-07 17:53:43 -0700119int terminateBuffer(gralloc_module_t const* module,
120 private_handle_t* hnd)
121{
Mathias Agopianf96b2062009-12-14 18:27:09 -0800122 if (hnd->base) {
Mathias Agopianbd80b382009-07-07 17:53:43 -0700123 // this buffer was mapped, unmap it now
Mathias Agopianf96b2062009-12-14 18:27:09 -0800124 gralloc_unmap(module, hnd);
Mathias Agopianbd80b382009-07-07 17:53:43 -0700125 }
126
127 return 0;
128}
129
Mathias Agopiana8a75162009-04-10 14:24:31 -0700130int gralloc_lock(gralloc_module_t const* module,
131 buffer_handle_t handle, int usage,
Mathias Agopian988b8bd2009-05-04 14:26:56 -0700132 int l, int t, int w, int h,
133 void** vaddr)
Mathias Agopiana8a75162009-04-10 14:24:31 -0700134{
Mathias Agopianf96b2062009-12-14 18:27:09 -0800135 // this is called when a buffer is being locked for software
136 // access. in thin implementation we have nothing to do since
137 // not synchronization with the h/w is needed.
138 // typically this is used to wait for the h/w to finish with
139 // this buffer if relevant. the data cache may need to be
140 // flushed or invalidated depending on the usage bits and the
141 // hardware.
142
Mathias Agopian988b8bd2009-05-04 14:26:56 -0700143 if (private_handle_t::validate(handle) < 0)
144 return -EINVAL;
145
Mathias Agopian51156652009-06-09 18:55:49 -0700146 private_handle_t* hnd = (private_handle_t*)handle;
Mathias Agopianf96b2062009-12-14 18:27:09 -0800147 *vaddr = (void*)hnd->base;
148 return 0;
Mathias Agopiana8a75162009-04-10 14:24:31 -0700149}
150
Jesse Hallc71b6ca2013-03-28 11:04:16 -0700151int gralloc_unlock(gralloc_module_t const* module,
Mathias Agopiana8a75162009-04-10 14:24:31 -0700152 buffer_handle_t handle)
153{
Mathias Agopianf96b2062009-12-14 18:27:09 -0800154 // we're done with a software buffer. nothing to do in this
155 // implementation. typically this is used to flush the data cache.
156
Mathias Agopian988b8bd2009-05-04 14:26:56 -0700157 if (private_handle_t::validate(handle) < 0)
158 return -EINVAL;
Mathias Agopiana8a75162009-04-10 14:24:31 -0700159 return 0;
160}