blob: ee3e40e099f3413de78425bca1a9537f440e84e9 [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
Mathias Agopiana8a75162009-04-10 14:24:31 -070017#include <errno.h>
Mark Salyzynd88dfe82017-04-11 08:56:09 -070018#include <limits.h>
Mathias Agopiana8a75162009-04-10 14:24:31 -070019#include <pthread.h>
Marco Nelissena4b587c2009-07-07 09:29:00 -070020#include <string.h>
Mathias Agopiana8a75162009-04-10 14:24:31 -070021#include <sys/mman.h>
22#include <sys/stat.h>
23#include <sys/types.h>
Mark Salyzynd88dfe82017-04-11 08:56:09 -070024#include <unistd.h>
Mathias Agopiana8a75162009-04-10 14:24:31 -070025
Mathias Agopiana8a75162009-04-10 14:24:31 -070026#include <cutils/atomic.h>
Mark Salyzynd88dfe82017-04-11 08:56:09 -070027#include <log/log.h>
Mathias Agopiana8a75162009-04-10 14:24:31 -070028
29#include <hardware/hardware.h>
30#include <hardware/gralloc.h>
31
32#include "gralloc_priv.h"
33
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -070034
Mathias Agopiana8a75162009-04-10 14:24:31 -070035/*****************************************************************************/
36
Colin Crossfebaaa92014-02-05 18:21:09 -080037static int gralloc_map(gralloc_module_t const* /*module*/,
Mathias Agopiana8a75162009-04-10 14:24:31 -070038 buffer_handle_t handle,
39 void** vaddr)
40{
Mathias Agopian51156652009-06-09 18:55:49 -070041 private_handle_t* hnd = (private_handle_t*)handle;
42 if (!(hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER)) {
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -070043 size_t size = hnd->size;
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -070044 void* mappedAddress = mmap(0, size,
45 PROT_READ|PROT_WRITE, MAP_SHARED, hnd->fd, 0);
Mathias Agopian51156652009-06-09 18:55:49 -070046 if (mappedAddress == MAP_FAILED) {
Steve Block60d056b2012-01-08 10:17:53 +000047 ALOGE("Could not mmap %s", strerror(errno));
Mathias Agopian51156652009-06-09 18:55:49 -070048 return -errno;
49 }
Colin Crossfebaaa92014-02-05 18:21:09 -080050 hnd->base = uintptr_t(mappedAddress) + hnd->offset;
Steve Blockcee85012011-12-20 16:25:12 +000051 //ALOGD("gralloc_map() succeeded fd=%d, off=%d, size=%d, vaddr=%p",
Mathias Agopian8c4ab1f2009-06-11 16:32:05 -070052 // hnd->fd, hnd->offset, hnd->size, mappedAddress);
Mathias Agopiana8a75162009-04-10 14:24:31 -070053 }
Mathias Agopian51156652009-06-09 18:55:49 -070054 *vaddr = (void*)hnd->base;
55 return 0;
Mathias Agopiana8a75162009-04-10 14:24:31 -070056}
57
Colin Crossfebaaa92014-02-05 18:21:09 -080058static int gralloc_unmap(gralloc_module_t const* /*module*/,
Mathias Agopiana8a75162009-04-10 14:24:31 -070059 buffer_handle_t handle)
60{
Mathias Agopian51156652009-06-09 18:55:49 -070061 private_handle_t* hnd = (private_handle_t*)handle;
62 if (!(hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER)) {
Mathias Agopianbd80b382009-07-07 17:53:43 -070063 void* base = (void*)hnd->base;
64 size_t size = hnd->size;
Steve Blockcee85012011-12-20 16:25:12 +000065 //ALOGD("unmapping from %p, size=%d", base, size);
Mathias Agopianbd80b382009-07-07 17:53:43 -070066 if (munmap(base, size) < 0) {
Steve Block60d056b2012-01-08 10:17:53 +000067 ALOGE("Could not unmap %s", strerror(errno));
Mathias Agopian51156652009-06-09 18:55:49 -070068 }
Mathias Agopiana8a75162009-04-10 14:24:31 -070069 }
Mathias Agopian51156652009-06-09 18:55:49 -070070 hnd->base = 0;
71 return 0;
Mathias Agopiana8a75162009-04-10 14:24:31 -070072}
73
Mathias Agopian51156652009-06-09 18:55:49 -070074/*****************************************************************************/
75
Mathias Agopian988b8bd2009-05-04 14:26:56 -070076int gralloc_register_buffer(gralloc_module_t const* module,
77 buffer_handle_t handle)
78{
79 if (private_handle_t::validate(handle) < 0)
80 return -EINVAL;
Mathias Agopian51156652009-06-09 18:55:49 -070081
Jesse Hall0d8f81a2013-03-28 16:51:25 -070082 // *** WARNING WARNING WARNING ***
83 //
84 // If a buffer handle is passed from the process that allocated it to a
85 // different process, and then back to the allocator process, we will
86 // create a second mapping of the buffer. If the process reads and writes
87 // through both mappings, normal memory ordering guarantees may be
88 // violated, depending on the processor cache implementation*.
89 //
90 // If you are deriving a new gralloc implementation from this code, don't
91 // do this. A "real" gralloc should provide a single reference-counted
92 // mapping for each buffer in a process.
93 //
94 // In the current system, there is one case that needs a buffer to be
95 // registered in the same process that allocated it. The SurfaceFlinger
96 // process acts as the IGraphicBufferAlloc Binder provider, so all gralloc
97 // allocations happen in its process. After returning the buffer handle to
98 // the IGraphicBufferAlloc client, SurfaceFlinger free's its handle to the
99 // buffer (unmapping it from the SurfaceFlinger process). If
100 // SurfaceFlinger later acts as the producer end of the buffer queue the
101 // buffer belongs to, it will get a new handle to the buffer in response
102 // to IGraphicBufferProducer::requestBuffer(). Like any buffer handle
103 // received through Binder, the SurfaceFlinger process will register it.
104 // Since it already freed its original handle, it will only end up with
105 // one mapping to the buffer and there will be no problem.
106 //
107 // Currently SurfaceFlinger only acts as a buffer producer for a remote
108 // consumer when taking screenshots and when using virtual displays.
109 //
110 // Eventually, each application should be allowed to make its own gralloc
111 // allocations, solving the problem. Also, this ashmem-based gralloc
112 // should go away, replaced with a real ion-based gralloc.
113 //
114 // * Specifically, associative virtually-indexed caches are likely to have
115 // problems. Most modern L1 caches fit that description.
116
117 private_handle_t* hnd = (private_handle_t*)handle;
118 ALOGD_IF(hnd->pid == getpid(),
119 "Registering a buffer in the process that created it. "
120 "This may cause memory ordering problems.");
121
Jesse Hallc71b6ca2013-03-28 11:04:16 -0700122 void *vaddr;
123 return gralloc_map(module, handle, &vaddr);
Mathias Agopian988b8bd2009-05-04 14:26:56 -0700124}
125
126int gralloc_unregister_buffer(gralloc_module_t const* module,
127 buffer_handle_t handle)
128{
129 if (private_handle_t::validate(handle) < 0)
130 return -EINVAL;
131
Mathias Agopianf96b2062009-12-14 18:27:09 -0800132 private_handle_t* hnd = (private_handle_t*)handle;
Jesse Hallc71b6ca2013-03-28 11:04:16 -0700133 if (hnd->base)
134 gralloc_unmap(module, handle);
135
Mathias Agopian988b8bd2009-05-04 14:26:56 -0700136 return 0;
137}
Mathias Agopiana8a75162009-04-10 14:24:31 -0700138
Mathias Agopianf96b2062009-12-14 18:27:09 -0800139int mapBuffer(gralloc_module_t const* module,
140 private_handle_t* hnd)
141{
142 void* vaddr;
143 return gralloc_map(module, hnd, &vaddr);
144}
145
Mathias Agopianbd80b382009-07-07 17:53:43 -0700146int terminateBuffer(gralloc_module_t const* module,
147 private_handle_t* hnd)
148{
Mathias Agopianf96b2062009-12-14 18:27:09 -0800149 if (hnd->base) {
Mathias Agopianbd80b382009-07-07 17:53:43 -0700150 // this buffer was mapped, unmap it now
Mathias Agopianf96b2062009-12-14 18:27:09 -0800151 gralloc_unmap(module, hnd);
Mathias Agopianbd80b382009-07-07 17:53:43 -0700152 }
153
154 return 0;
155}
156
Colin Crossfebaaa92014-02-05 18:21:09 -0800157int gralloc_lock(gralloc_module_t const* /*module*/,
158 buffer_handle_t handle, int /*usage*/,
159 int /*l*/, int /*t*/, int /*w*/, int /*h*/,
Mathias Agopian988b8bd2009-05-04 14:26:56 -0700160 void** vaddr)
Mathias Agopiana8a75162009-04-10 14:24:31 -0700161{
Mathias Agopianf96b2062009-12-14 18:27:09 -0800162 // this is called when a buffer is being locked for software
163 // access. in thin implementation we have nothing to do since
164 // not synchronization with the h/w is needed.
165 // typically this is used to wait for the h/w to finish with
166 // this buffer if relevant. the data cache may need to be
167 // flushed or invalidated depending on the usage bits and the
168 // hardware.
169
Mathias Agopian988b8bd2009-05-04 14:26:56 -0700170 if (private_handle_t::validate(handle) < 0)
171 return -EINVAL;
172
Mathias Agopian51156652009-06-09 18:55:49 -0700173 private_handle_t* hnd = (private_handle_t*)handle;
Mathias Agopianf96b2062009-12-14 18:27:09 -0800174 *vaddr = (void*)hnd->base;
175 return 0;
Mathias Agopiana8a75162009-04-10 14:24:31 -0700176}
177
Colin Crossfebaaa92014-02-05 18:21:09 -0800178int gralloc_unlock(gralloc_module_t const* /*module*/,
Mathias Agopiana8a75162009-04-10 14:24:31 -0700179 buffer_handle_t handle)
180{
Mathias Agopianf96b2062009-12-14 18:27:09 -0800181 // we're done with a software buffer. nothing to do in this
182 // implementation. typically this is used to flush the data cache.
183
Mathias Agopian988b8bd2009-05-04 14:26:56 -0700184 if (private_handle_t::validate(handle) < 0)
185 return -EINVAL;
Mathias Agopiana8a75162009-04-10 14:24:31 -0700186 return 0;
187}