1 /*
2 // Copyright (c) 2014 Intel Corporation
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 #include <HwcTrace.h>
17 #include <BufferCache.h>
18
19 namespace android {
20 namespace intel {
21
BufferCache(int size)22 BufferCache::BufferCache(int size)
23 {
24 mBufferPool.setCapacity(size);
25 }
26
~BufferCache()27 BufferCache::~BufferCache()
28 {
29 if (mBufferPool.size() != 0) {
30 ETRACE("buffer cache is not empty");
31 }
32 mBufferPool.clear();
33 }
34
addMapper(uint64_t handle,BufferMapper * mapper)35 bool BufferCache::addMapper(uint64_t handle, BufferMapper* mapper)
36 {
37 ssize_t index = mBufferPool.indexOfKey(handle);
38 if (index >= 0) {
39 ETRACE("buffer %#llx exists", handle);
40 return false;
41 }
42
43 // add mapper
44 index = mBufferPool.add(handle, mapper);
45 if (index < 0) {
46 ETRACE("failed to add mapper. err = %d", (int)index);
47 return false;
48 }
49
50 return true;
51 }
52
removeMapper(BufferMapper * mapper)53 bool BufferCache::removeMapper(BufferMapper* mapper)
54 {
55 ssize_t index;
56
57 if (!mapper) {
58 ETRACE("invalid mapper");
59 return false;
60 }
61
62 index = mBufferPool.removeItem(mapper->getKey());
63 if (index < 0) {
64 WTRACE("failed to remove mapper. err = %d", (int)index);
65 return false;
66 }
67
68 return true;
69 }
70
getMapper(uint64_t handle)71 BufferMapper* BufferCache::getMapper(uint64_t handle)
72 {
73 ssize_t index = mBufferPool.indexOfKey(handle);
74 if (index < 0) {
75 // don't add ETRACE here as this condition will happen frequently
76 return 0;
77 }
78 return mBufferPool.valueAt(index);
79 }
80
getCacheSize() const81 size_t BufferCache::getCacheSize() const
82 {
83 return mBufferPool.size();
84 }
85
getMapper(uint32_t index)86 BufferMapper* BufferCache::getMapper(uint32_t index)
87 {
88 if (index >= mBufferPool.size()) {
89 ETRACE("invalid index");
90 return 0;
91 }
92 BufferMapper* mapper = mBufferPool.valueAt(index);
93 return mapper;
94 }
95
96 } // namespace intel
97 } // namespace android
98