blob: 3852d51954a5ad2a359329b26ad1f788e69454ae [file] [log] [blame]
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -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#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
33#include <utils/MemoryDealer.h>
34#include <utils/MemoryBase.h>
35#include <utils/MemoryHeapPmem.h>
36#include <utils/MemoryHeapBase.h>
37
38#include <GLES/eglnatives.h>
39
40#include "VRamHeap.h"
41
42#if HAVE_ANDROID_OS
43#include <linux/android_pmem.h>
44#endif
45
46
47namespace android {
48
49// ---------------------------------------------------------------------------
50
51/*
52 * Amount of memory we reserve for surface, per client in PMEM
53 * (PMEM is used for 2D acceleration)
54 * 8 MB of address space per client should be enough.
55 */
56static const int PMEM_SIZE = int(8 * 1024 * 1024);
57
58int SurfaceHeapManager::global_pmem_heap = 0;
59
60// ---------------------------------------------------------------------------
61
62SurfaceHeapManager::SurfaceHeapManager(size_t clientHeapSize)
63 : mClientHeapSize(clientHeapSize)
64{
65 SurfaceHeapManager::global_pmem_heap = 1;
66}
67
68SurfaceHeapManager::~SurfaceHeapManager()
69{
70}
71
72void SurfaceHeapManager::onFirstRef()
73{
74 if (global_pmem_heap) {
75 const char* device = "/dev/pmem";
76 mPMemHeap = new PMemHeap(device, PMEM_SIZE);
77 if (mPMemHeap->base() == MAP_FAILED) {
78 mPMemHeap.clear();
79 global_pmem_heap = 0;
80 }
81 }
82}
83
84sp<MemoryDealer> SurfaceHeapManager::createHeap(int type)
85{
86 if (!global_pmem_heap && type==NATIVE_MEMORY_TYPE_PMEM)
87 type = NATIVE_MEMORY_TYPE_HEAP;
88
89 const sp<PMemHeap>& heap(mPMemHeap);
90 sp<MemoryDealer> dealer;
91 switch (type) {
92 case NATIVE_MEMORY_TYPE_HEAP:
93 dealer = new MemoryDealer(mClientHeapSize, 0, "SFNativeHeap");
94 break;
95
96 case NATIVE_MEMORY_TYPE_PMEM:
97 if (heap != 0) {
98 dealer = new MemoryDealer(
99 heap->createClientHeap(),
100 heap->getAllocator());
101 }
102 break;
103 }
104 return dealer;
105}
106
107sp<SimpleBestFitAllocator> SurfaceHeapManager::getAllocator(int type) const
108{
109 Mutex::Autolock _l(mLock);
110 sp<SimpleBestFitAllocator> allocator;
111
112 // this is only used for debugging
113 switch (type) {
114 case NATIVE_MEMORY_TYPE_PMEM:
115 if (mPMemHeap != 0) {
116 allocator = mPMemHeap->getAllocator();
117 }
118 break;
119 }
120 return allocator;
121}
122
123// ---------------------------------------------------------------------------
124
125PMemHeapInterface::PMemHeapInterface(int fd, size_t size)
126 : MemoryHeapBase(fd, size) {
127}
128PMemHeapInterface::PMemHeapInterface(const char* device, size_t size)
129 : MemoryHeapBase(device, size) {
130}
131PMemHeapInterface::PMemHeapInterface(size_t size, uint32_t flags, char const * name)
132 : MemoryHeapBase(size, flags, name) {
133}
134PMemHeapInterface::~PMemHeapInterface() {
135}
136
137// ---------------------------------------------------------------------------
138
139PMemHeap::PMemHeap(const char* const device, size_t size, size_t reserved)
140 : PMemHeapInterface(device, size)
141{
142 //LOGD("%s, %p, mFD=%d", __PRETTY_FUNCTION__, this, heapID());
143 if (base() != MAP_FAILED) {
144 //LOGD("%s, %u bytes", device, virtualSize());
145 if (reserved == 0)
146 reserved = virtualSize();
147 mAllocator = new SimpleBestFitAllocator(reserved);
148 }
149}
150
151PMemHeap::~PMemHeap() {
152 //LOGD("%s, %p, mFD=%d", __PRETTY_FUNCTION__, this, heapID());
153}
154
155sp<MemoryHeapPmem> PMemHeap::createClientHeap() {
156 sp<MemoryHeapBase> parentHeap(this);
157 return new MemoryHeapPmem(parentHeap);
158}
159
160// ---------------------------------------------------------------------------
161}; // namespace android