blob: 3b1b152b949e2f132a31204616f1d65eca4018de [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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#define LOG_TAG "SurfaceFlinger"
18
19#include <stdlib.h>
20#include <stdio.h>
21#include <stdint.h>
22#include <unistd.h>
23#include <fcntl.h>
24#include <errno.h>
25#include <math.h>
26#include <sys/types.h>
27#include <sys/stat.h>
28#include <sys/ioctl.h>
29
30#include <cutils/log.h>
31#include <cutils/properties.h>
32
Mathias Agopian07952722009-05-19 19:08:10 -070033#include <binder/MemoryDealer.h>
34#include <binder/MemoryBase.h>
35#include <binder/MemoryHeapPmem.h>
36#include <binder/MemoryHeapBase.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037
Mathias Agopian627e7b52009-05-21 19:21:59 -070038#include <EGL/eglnatives.h>
39
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040#include "GPUHardware/GPUHardware.h"
41#include "SurfaceFlinger.h"
42#include "VRamHeap.h"
43
44#if HAVE_ANDROID_OS
45#include <linux/android_pmem.h>
46#endif
47
48
49namespace android {
50
51// ---------------------------------------------------------------------------
52
53/*
54 * Amount of memory we reserve for surface, per client in PMEM
55 * (PMEM is used for 2D acceleration)
56 * 8 MB of address space per client should be enough.
57 */
Mathias Agopianff80ceb2009-07-01 17:47:38 -070058static const int PMEM_SIZE = int(16 * 1024 * 1024);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059
60int SurfaceHeapManager::global_pmem_heap = 0;
61
62// ---------------------------------------------------------------------------
63
64SurfaceHeapManager::SurfaceHeapManager(const sp<SurfaceFlinger>& flinger,
65 size_t clientHeapSize)
66 : mFlinger(flinger), mClientHeapSize(clientHeapSize)
67{
68 SurfaceHeapManager::global_pmem_heap = 1;
69}
70
71SurfaceHeapManager::~SurfaceHeapManager()
72{
73}
74
75void SurfaceHeapManager::onFirstRef()
76{
77 if (global_pmem_heap) {
78 const char* device = "/dev/pmem";
79 mPMemHeap = new PMemHeap(device, PMEM_SIZE);
80 if (mPMemHeap->base() == MAP_FAILED) {
81 mPMemHeap.clear();
Mathias Agopianff80ceb2009-07-01 17:47:38 -070082 mPMemHeap = new PMemHeap(device, PMEM_SIZE/2);
83 if (mPMemHeap->base() == MAP_FAILED) {
84 mPMemHeap.clear();
85 global_pmem_heap = 0;
86 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087 }
88 }
89}
90
91sp<MemoryDealer> SurfaceHeapManager::createHeap(
92 uint32_t flags,
93 pid_t client_pid,
94 const sp<MemoryDealer>& defaultAllocator)
95{
96 sp<MemoryDealer> dealer;
97
98 if (flags & ISurfaceComposer::eGPU) {
99 // don't grant GPU memory if GPU is disabled
100 char value[PROPERTY_VALUE_MAX];
101 property_get("debug.egl.hw", value, "1");
102 if (atoi(value) == 0) {
103 flags &= ~ISurfaceComposer::eGPU;
104 }
105 }
106
Mathias Agopian0c6b5f62009-04-27 18:50:06 -0700107 if ((flags & ISurfaceComposer::eGPU) && (mFlinger->getGPU() != 0)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800108 // FIXME: this is msm7201A specific, where gpu surfaces may not be secure
109 if (!(flags & ISurfaceComposer::eSecure)) {
110 // if GPU doesn't work, we try eHardware
111 flags |= ISurfaceComposer::eHardware;
112 // asked for GPU memory, try that first
113 dealer = mFlinger->getGPU()->request(client_pid);
114 }
115 }
116
117 if (dealer == NULL) {
118 if (defaultAllocator != NULL)
119 // if a default allocator is given, use that
120 dealer = defaultAllocator;
121 }
122
123 if (dealer == NULL) {
124 // always try h/w accelerated memory first
125 if (global_pmem_heap) {
126 const sp<PMemHeap>& heap(mPMemHeap);
127 if (dealer == NULL && heap != NULL) {
128 dealer = new MemoryDealer(
129 heap->createClientHeap(),
130 heap->getAllocator());
131 }
132 }
133 }
134
135 if (dealer == NULL) {
136 // return the ashmem allocator (software rendering)
137 dealer = new MemoryDealer(mClientHeapSize, 0, "SFNativeHeap");
138 }
139 return dealer;
140}
141
142sp<SimpleBestFitAllocator> SurfaceHeapManager::getAllocator(int type) const
143{
144 Mutex::Autolock _l(mLock);
145 sp<SimpleBestFitAllocator> allocator;
146
147 // this is only used for debugging
148 switch (type) {
149 case NATIVE_MEMORY_TYPE_PMEM:
150 if (mPMemHeap != 0) {
151 allocator = mPMemHeap->getAllocator();
152 }
153 break;
154 }
155 return allocator;
156}
157
158// ---------------------------------------------------------------------------
159
160PMemHeap::PMemHeap(const char* const device, size_t size, size_t reserved)
161 : MemoryHeapBase(device, size)
162{
163 //LOGD("%s, %p, mFD=%d", __PRETTY_FUNCTION__, this, heapID());
164 if (base() != MAP_FAILED) {
165 //LOGD("%s, %u bytes", device, virtualSize());
166 if (reserved == 0)
167 reserved = virtualSize();
168 mAllocator = new SimpleBestFitAllocator(reserved);
169 }
170}
171
172PMemHeap::~PMemHeap() {
173 //LOGD("%s, %p, mFD=%d", __PRETTY_FUNCTION__, this, heapID());
174}
175
176sp<MemoryHeapPmem> PMemHeap::createClientHeap() {
177 sp<MemoryHeapBase> parentHeap(this);
178 return new MemoryHeapPmem(parentHeap);
179}
180
181// ---------------------------------------------------------------------------
182}; // namespace android