The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 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 Agopian | c5b2c0b | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 33 | #include <binder/MemoryDealer.h> |
| 34 | #include <binder/MemoryBase.h> |
| 35 | #include <binder/MemoryHeapPmem.h> |
| 36 | #include <binder/MemoryHeapBase.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 37 | |
Mathias Agopian | a1ecca9 | 2009-05-21 19:21:59 -0700 | [diff] [blame] | 38 | #include <EGL/eglnatives.h> |
| 39 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 40 | #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 | |
| 49 | namespace 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 | */ |
| 58 | static const int PMEM_SIZE = int(8 * 1024 * 1024); |
| 59 | |
| 60 | int SurfaceHeapManager::global_pmem_heap = 0; |
| 61 | |
| 62 | // --------------------------------------------------------------------------- |
| 63 | |
| 64 | SurfaceHeapManager::SurfaceHeapManager(const sp<SurfaceFlinger>& flinger, |
| 65 | size_t clientHeapSize) |
| 66 | : mFlinger(flinger), mClientHeapSize(clientHeapSize) |
| 67 | { |
| 68 | SurfaceHeapManager::global_pmem_heap = 1; |
| 69 | } |
| 70 | |
| 71 | SurfaceHeapManager::~SurfaceHeapManager() |
| 72 | { |
| 73 | } |
| 74 | |
| 75 | void 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 | |
| 87 | sp<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 Agopian | 2b42fa7 | 2009-04-27 18:50:06 -0700 | [diff] [blame] | 103 | if ((flags & ISurfaceComposer::eGPU) && (mFlinger->getGPU() != 0)) { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 104 | // 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 | |
| 138 | sp<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 | |
| 156 | PMemHeap::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 | |
| 168 | PMemHeap::~PMemHeap() { |
| 169 | //LOGD("%s, %p, mFD=%d", __PRETTY_FUNCTION__, this, heapID()); |
| 170 | } |
| 171 | |
| 172 | sp<MemoryHeapPmem> PMemHeap::createClientHeap() { |
| 173 | sp<MemoryHeapBase> parentHeap(this); |
| 174 | return new MemoryHeapPmem(parentHeap); |
| 175 | } |
| 176 | |
| 177 | // --------------------------------------------------------------------------- |
| 178 | }; // namespace android |