SkYUVPlanesCache.cpp 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. * Copyright 2015 Google Inc.
  3. *
  4. * Use of this source code is governed by a BSD-style license that can be
  5. * found in the LICENSE file.
  6. */
  7. #include "src/core/SkBitmapCache.h"
  8. #include "src/core/SkResourceCache.h"
  9. #include "src/core/SkYUVPlanesCache.h"
  10. #define CHECK_LOCAL(localCache, localName, globalName, ...) \
  11. ((localCache) ? localCache->localName(__VA_ARGS__) : SkResourceCache::globalName(__VA_ARGS__))
  12. namespace {
  13. static unsigned gYUVPlanesKeyNamespaceLabel;
  14. struct YUVValue {
  15. SkYUVPlanesCache::Info fInfo;
  16. SkCachedData* fData;
  17. };
  18. struct YUVPlanesKey : public SkResourceCache::Key {
  19. YUVPlanesKey(uint32_t genID)
  20. : fGenID(genID)
  21. {
  22. this->init(&gYUVPlanesKeyNamespaceLabel, SkMakeResourceCacheSharedIDForBitmap(genID),
  23. sizeof(genID));
  24. }
  25. uint32_t fGenID;
  26. };
  27. struct YUVPlanesRec : public SkResourceCache::Rec {
  28. YUVPlanesRec(YUVPlanesKey key, SkCachedData* data, SkYUVPlanesCache::Info* info)
  29. : fKey(key)
  30. {
  31. fValue.fData = data;
  32. fValue.fInfo = *info;
  33. fValue.fData->attachToCacheAndRef();
  34. }
  35. ~YUVPlanesRec() override {
  36. fValue.fData->detachFromCacheAndUnref();
  37. }
  38. YUVPlanesKey fKey;
  39. YUVValue fValue;
  40. const Key& getKey() const override { return fKey; }
  41. size_t bytesUsed() const override { return sizeof(*this) + fValue.fData->size(); }
  42. const char* getCategory() const override { return "yuv-planes"; }
  43. SkDiscardableMemory* diagnostic_only_getDiscardable() const override {
  44. return fValue.fData->diagnostic_only_getDiscardable();
  45. }
  46. static bool Visitor(const SkResourceCache::Rec& baseRec, void* contextData) {
  47. const YUVPlanesRec& rec = static_cast<const YUVPlanesRec&>(baseRec);
  48. YUVValue* result = static_cast<YUVValue*>(contextData);
  49. SkCachedData* tmpData = rec.fValue.fData;
  50. tmpData->ref();
  51. if (nullptr == tmpData->data()) {
  52. tmpData->unref();
  53. return false;
  54. }
  55. result->fData = tmpData;
  56. result->fInfo = rec.fValue.fInfo;
  57. return true;
  58. }
  59. };
  60. } // namespace
  61. SkCachedData* SkYUVPlanesCache::FindAndRef(uint32_t genID, Info* info,
  62. SkResourceCache* localCache) {
  63. YUVValue result;
  64. YUVPlanesKey key(genID);
  65. if (!CHECK_LOCAL(localCache, find, Find, key, YUVPlanesRec::Visitor, &result)) {
  66. return nullptr;
  67. }
  68. *info = result.fInfo;
  69. return result.fData;
  70. }
  71. void SkYUVPlanesCache::Add(uint32_t genID, SkCachedData* data, Info* info,
  72. SkResourceCache* localCache) {
  73. YUVPlanesKey key(genID);
  74. return CHECK_LOCAL(localCache, add, Add, new YUVPlanesRec(key, data, info));
  75. }