blob: 68c0a5edda7a47a60310f048575059063e0af26f [file] [log] [blame]
The Android Open Source Projectedbf3b62009-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 Agopianc5b2c0b2009-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 Projectedbf3b62009-03-03 19:31:44 -080037
Mathias Agopiana1ecca92009-05-21 19:21:59 -070038#include <EGL/eglnatives.h>
39
The Android Open Source Projectedbf3b62009-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 */
58static const int PMEM_SIZE = int(8 * 1024 * 1024);
59
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();
82 global_pmem_heap = 0;
83 }
84 }
85}
86
87sp<MemoryDealer> SurfaceHeapManager::createHeap(
88 uint32_t flags,
89 pid_t client_pid,
90 const sp<MemoryDealer>& defaultAllocator)
91{
92 sp<MemoryDealer> dealer;
93
94 if (flags & ISurfaceComposer::eGPU) {
95 // don't grant GPU memory if GPU is disabled
96 char value[PROPERTY_VALUE_MAX];
97 property_get("debug.egl.hw", value, "1");
98 if (atoi(value) == 0) {
99 flags &= ~ISurfaceComposer::eGPU;
100 }
101 }
102
Mathias Agopian2b42fa72009-04-27 18:50:06 -0700103 if ((flags & ISurfaceComposer::eGPU) && (mFlinger->getGPU() != 0)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800104 // FIXME: this is msm7201A specific, where gpu surfaces may not be secure
105 if (!(flags & ISurfaceComposer::eSecure)) {
106 // if GPU doesn't work, we try eHardware
107 flags |= ISurfaceComposer::eHardware;
108 // asked for GPU memory, try that first
109 dealer = mFlinger->getGPU()->request(client_pid);
110 }
111 }
112
113 if (dealer == NULL) {
114 if (defaultAllocator != NULL)
115 // if a default allocator is given, use that
116 dealer = defaultAllocator;
117 }
118
119 if (dealer == NULL) {
120 // always try h/w accelerated memory first
121 if (global_pmem_heap) {
122 const sp<PMemHeap>& heap(mPMemHeap);
123 if (dealer == NULL && heap != NULL) {
124 dealer = new MemoryDealer(
125 heap->createClientHeap(),
126 heap->getAllocator());
127 }
128 }
129 }
130
131 if (dealer == NULL) {
132 // return the ashmem allocator (software rendering)
133 dealer = new MemoryDealer(mClientHeapSize, 0, "SFNativeHeap");
134 }
135 return dealer;
136}
137
138sp<SimpleBestFitAllocator> SurfaceHeapManager::getAllocator(int type) const
139{
140 Mutex::Autolock _l(mLock);
141 sp<SimpleBestFitAllocator> allocator;
142
143 // this is only used for debugging
144 switch (type) {
145 case NATIVE_MEMORY_TYPE_PMEM:
146 if (mPMemHeap != 0) {
147 allocator = mPMemHeap->getAllocator();
148 }
149 break;
150 }
151 return allocator;
152}
153
154// ---------------------------------------------------------------------------
155
156PMemHeap::PMemHeap(const char* const device, size_t size, size_t reserved)
157 : MemoryHeapBase(device, size)
158{
159 //LOGD("%s, %p, mFD=%d", __PRETTY_FUNCTION__, this, heapID());
160 if (base() != MAP_FAILED) {
161 //LOGD("%s, %u bytes", device, virtualSize());
162 if (reserved == 0)
163 reserved = virtualSize();
164 mAllocator = new SimpleBestFitAllocator(reserved);
165 }
166}
167
168PMemHeap::~PMemHeap() {
169 //LOGD("%s, %p, mFD=%d", __PRETTY_FUNCTION__, this, heapID());
170}
171
172sp<MemoryHeapPmem> PMemHeap::createClientHeap() {
173 sp<MemoryHeapBase> parentHeap(this);
174 return new MemoryHeapPmem(parentHeap);
175}
176
177// ---------------------------------------------------------------------------
178}; // namespace android