ClientCache.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. * Copyright 2019 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. //#define LOG_NDEBUG 0
  17. #undef LOG_TAG
  18. #define LOG_TAG "ClientCache"
  19. #define ATRACE_TAG ATRACE_TAG_GRAPHICS
  20. #include <cinttypes>
  21. #include "ClientCache.h"
  22. namespace android {
  23. ANDROID_SINGLETON_STATIC_INSTANCE(ClientCache);
  24. ClientCache::ClientCache() : mDeathRecipient(new CacheDeathRecipient) {}
  25. bool ClientCache::getBuffer(const client_cache_t& cacheId,
  26. ClientCacheBuffer** outClientCacheBuffer) {
  27. auto& [processToken, id] = cacheId;
  28. if (processToken == nullptr) {
  29. ALOGE("failed to get buffer, invalid (nullptr) process token");
  30. return false;
  31. }
  32. auto it = mBuffers.find(processToken);
  33. if (it == mBuffers.end()) {
  34. ALOGE("failed to get buffer, invalid process token");
  35. return false;
  36. }
  37. auto& processBuffers = it->second;
  38. auto bufItr = processBuffers.find(id);
  39. if (bufItr == processBuffers.end()) {
  40. ALOGE("failed to get buffer, invalid buffer id");
  41. return false;
  42. }
  43. ClientCacheBuffer& buf = bufItr->second;
  44. *outClientCacheBuffer = &buf;
  45. return true;
  46. }
  47. bool ClientCache::add(const client_cache_t& cacheId, const sp<GraphicBuffer>& buffer) {
  48. auto& [processToken, id] = cacheId;
  49. if (processToken == nullptr) {
  50. ALOGE("failed to cache buffer: invalid process token");
  51. return false;
  52. }
  53. if (!buffer) {
  54. ALOGE("failed to cache buffer: invalid buffer");
  55. return false;
  56. }
  57. std::lock_guard lock(mMutex);
  58. sp<IBinder> token;
  59. // If this is a new process token, set a death recipient. If the client process dies, we will
  60. // get a callback through binderDied.
  61. auto it = mBuffers.find(processToken);
  62. if (it == mBuffers.end()) {
  63. token = processToken.promote();
  64. if (!token) {
  65. ALOGE("failed to cache buffer: invalid token");
  66. return false;
  67. }
  68. status_t err = token->linkToDeath(mDeathRecipient);
  69. if (err != NO_ERROR) {
  70. ALOGE("failed to cache buffer: could not link to death");
  71. return false;
  72. }
  73. auto [itr, success] =
  74. mBuffers.emplace(processToken, std::unordered_map<uint64_t, ClientCacheBuffer>());
  75. LOG_ALWAYS_FATAL_IF(!success, "failed to insert new process into client cache");
  76. it = itr;
  77. }
  78. auto& processBuffers = it->second;
  79. if (processBuffers.size() > BUFFER_CACHE_MAX_SIZE) {
  80. ALOGE("failed to cache buffer: cache is full");
  81. return false;
  82. }
  83. processBuffers[id].buffer = buffer;
  84. return true;
  85. }
  86. void ClientCache::erase(const client_cache_t& cacheId) {
  87. auto& [processToken, id] = cacheId;
  88. std::vector<sp<ErasedRecipient>> pendingErase;
  89. {
  90. std::lock_guard lock(mMutex);
  91. ClientCacheBuffer* buf = nullptr;
  92. if (!getBuffer(cacheId, &buf)) {
  93. ALOGE("failed to erase buffer, could not retrieve buffer");
  94. return;
  95. }
  96. for (auto& recipient : buf->recipients) {
  97. sp<ErasedRecipient> erasedRecipient = recipient.promote();
  98. if (erasedRecipient) {
  99. pendingErase.push_back(erasedRecipient);
  100. }
  101. }
  102. mBuffers[processToken].erase(id);
  103. }
  104. for (auto& recipient : pendingErase) {
  105. recipient->bufferErased(cacheId);
  106. }
  107. }
  108. sp<GraphicBuffer> ClientCache::get(const client_cache_t& cacheId) {
  109. std::lock_guard lock(mMutex);
  110. ClientCacheBuffer* buf = nullptr;
  111. if (!getBuffer(cacheId, &buf)) {
  112. ALOGE("failed to get buffer, could not retrieve buffer");
  113. return nullptr;
  114. }
  115. return buf->buffer;
  116. }
  117. bool ClientCache::registerErasedRecipient(const client_cache_t& cacheId,
  118. const wp<ErasedRecipient>& recipient) {
  119. std::lock_guard lock(mMutex);
  120. ClientCacheBuffer* buf = nullptr;
  121. if (!getBuffer(cacheId, &buf)) {
  122. ALOGE("failed to register erased recipient, could not retrieve buffer");
  123. return false;
  124. }
  125. buf->recipients.insert(recipient);
  126. return true;
  127. }
  128. void ClientCache::unregisterErasedRecipient(const client_cache_t& cacheId,
  129. const wp<ErasedRecipient>& recipient) {
  130. std::lock_guard lock(mMutex);
  131. ClientCacheBuffer* buf = nullptr;
  132. if (!getBuffer(cacheId, &buf)) {
  133. ALOGE("failed to unregister erased recipient");
  134. return;
  135. }
  136. buf->recipients.erase(recipient);
  137. }
  138. void ClientCache::removeProcess(const wp<IBinder>& processToken) {
  139. std::vector<std::pair<sp<ErasedRecipient>, client_cache_t>> pendingErase;
  140. {
  141. if (processToken == nullptr) {
  142. ALOGE("failed to remove process, invalid (nullptr) process token");
  143. return;
  144. }
  145. std::lock_guard lock(mMutex);
  146. auto itr = mBuffers.find(processToken);
  147. if (itr == mBuffers.end()) {
  148. ALOGE("failed to remove process, could not find process");
  149. return;
  150. }
  151. for (auto& [id, clientCacheBuffer] : itr->second) {
  152. client_cache_t cacheId = {processToken, id};
  153. for (auto& recipient : clientCacheBuffer.recipients) {
  154. sp<ErasedRecipient> erasedRecipient = recipient.promote();
  155. if (erasedRecipient) {
  156. pendingErase.emplace_back(erasedRecipient, cacheId);
  157. }
  158. }
  159. }
  160. mBuffers.erase(itr);
  161. }
  162. for (auto& [recipient, cacheId] : pendingErase) {
  163. recipient->bufferErased(cacheId);
  164. }
  165. }
  166. void ClientCache::CacheDeathRecipient::binderDied(const wp<IBinder>& who) {
  167. ClientCache::getInstance().removeProcess(who);
  168. }
  169. }; // namespace android