1 /* 2 // Copyright(c)2014 IntelCorporation 3 // 4 // LicensedundertheApacheLicense,Version2.0(the"License"); 5 // youmaynotusethisfileexceptincompliancewiththeLicense. 6 // YoumayobtainacopyoftheLicenseat 7 // 8 // http://www.apache.org/licenses/LICENSE-2.0 9 // 10 // Unlessrequiredbyapplicablelaworagreedtoinwriting,software 11 // distributedundertheLicenseisdistributedonan"ASIS"BASIS, 12 // WITHOUTWARRANTIESORCONDITIONSOFANYKIND,eitherexpressorimplied. 13 // SeetheLicenseforthespecificlanguagegoverningpermissionsand 14 // limitationsundertheLicense. 15 */ 16 #include <common/utils/HwcTrace.h> 17 #include <common/buffers/BufferCache.h> 18 19 namespace android { 20 namespace intel { 21 22 BufferCache::BufferCache(int size) 23 { 24 mBufferPool.setCapacity(size); 25 } 26 27 BufferCache::~BufferCache() 28 { 29 if (mBufferPool.size() != 0) { 30 ELOGTRACE("buffer cache is not empty"); 31 } 32 mBufferPool.clear(); 33 } 34 35 bool BufferCache::addMapper(uint64_t handle, BufferMapper* mapper) 36 { 37 ssize_t index = mBufferPool.indexOfKey(handle); 38 if (index >= 0) { 39 ELOGTRACE("buffer %#llx exists", handle); 40 return false; 41 } 42 43 // add mapper 44 index = mBufferPool.add(handle, mapper); 45 if (index < 0) { 46 ELOGTRACE("failed to add mapper. err = %d", index); 47 return false; 48 } 49 50 return true; 51 } 52 53 bool BufferCache::removeMapper(BufferMapper* mapper) 54 { 55 ssize_t index; 56 57 if (!mapper) { 58 ELOGTRACE("invalid mapper"); 59 return false; 60 } 61 62 index = mBufferPool.removeItem(mapper->getKey()); 63 if (index < 0) { 64 WLOGTRACE("failed to remove mapper. err = %d", index); 65 return false; 66 } 67 68 return true; 69 } 70 71 BufferMapper* BufferCache::getMapper(uint64_t handle) 72 { 73 ssize_t index = mBufferPool.indexOfKey(handle); 74 if (index < 0) { 75 // don't add ELOGTRACE here as this condition will happen frequently 76 return 0; 77 } 78 return mBufferPool.valueAt(index); 79 } 80 81 size_t BufferCache::getCacheSize() const 82 { 83 return mBufferPool.size(); 84 } 85 86 BufferMapper* BufferCache::getMapper(size_t index) 87 { 88 if (index >= mBufferPool.size()) { 89 ELOGTRACE("invalid index"); 90 return 0; 91 } 92 BufferMapper* mapper = mBufferPool.valueAt(index); 93 return mapper; 94 } 95 96 } // namespace intel 97 } // namespace android 98