Index: blimp/client/feature/compositor/blimp_client_picture_cache.cc |
diff --git a/blimp/client/feature/compositor/blimp_client_picture_cache.cc b/blimp/client/feature/compositor/blimp_client_picture_cache.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..9b3a442e7b4072b3857dadc881a79622260b6443 |
--- /dev/null |
+++ b/blimp/client/feature/compositor/blimp_client_picture_cache.cc |
@@ -0,0 +1,109 @@ |
+// Copyright 2016 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "blimp/client/feature/compositor/blimp_client_picture_cache.h" |
+ |
+#include <utility> |
+#include <vector> |
+ |
+#include "third_party/skia/include/core/SkStream.h" |
+ |
+namespace blimp { |
+namespace client { |
+ |
+namespace { |
Kevin M
2016/06/11 00:29:32
newline after "namespace", but remove the one befo
nyquist
2016/06/14 01:37:56
Done.
|
+// Helper function to deserialize the content of |picture_data| into an |
+// SkPicture. |
+sk_sp<const SkPicture> DeserializePicture( |
+ SkPicture::InstallPixelRefProc pixel_deserializer, |
+ const cc::PictureData& picture_data) { |
+ SkMemoryStream stream(picture_data.data); |
+ return SkPicture::MakeFromStream(&stream, pixel_deserializer); |
+} |
+ |
+} // namespace |
+ |
+BlimpClientPictureCache::BlimpClientPictureCache( |
+ SkPicture::InstallPixelRefProc pixel_deserializer) |
+ : pixel_deserializer_(pixel_deserializer) {} |
+ |
+BlimpClientPictureCache::~BlimpClientPictureCache() = default; |
+ |
+sk_sp<const SkPicture> BlimpClientPictureCache::GetPicture( |
+ uint32_t engine_picture_id) { |
+ return GetPictureFromCache(engine_picture_id); |
+} |
+ |
+void BlimpClientPictureCache::ApplyCacheUpdate( |
+ const cc::PictureCacheUpdate& cache_update) { |
+ AddNewPicturesToCache(cache_update); |
+} |
+ |
+void BlimpClientPictureCache::Flush() { |
+ // Calculate which pictures can now be removed. |added| is only used for |
+ // verifying that what we calculated matches the new items that have been |
+ // inserted into the cache. |
+ std::vector<uint32_t> added; |
+ std::vector<uint32_t> removed; |
+ reference_tracker_.CommitRefCounts(&added, &removed); |
+ |
+#if DCHECK_IS_ON() |
+ VerifyCacheUpdateMatchesReferenceTrackerData(added); |
+#endif // DCHECK_IS_ON() |
+ |
+ RemoveUnusedPicturesFromCache(removed); |
+} |
+ |
+void BlimpClientPictureCache::MarkPictureForUnregistration( |
+ uint32_t engine_picture_id) { |
+ reference_tracker_.DecrementRefCount(engine_picture_id); |
+} |
+ |
+void BlimpClientPictureCache::MarkPictureForRegistration( |
+ uint32_t engine_picture_id) { |
+ reference_tracker_.IncrementRefCount(engine_picture_id); |
+} |
+ |
+void BlimpClientPictureCache::AddNewPicturesToCache( |
+ const cc::PictureCacheUpdate& cache_update) { |
+ for (const cc::PictureData& picture_data : cache_update) { |
+ sk_sp<const SkPicture> deserialized_picture = |
+ DeserializePicture(pixel_deserializer_, picture_data); |
+ DCHECK(pictures_.find(picture_data.unique_id) == pictures_.end()); |
+ |
+ pictures_[picture_data.unique_id] = std::move(deserialized_picture); |
+ |
+#if DCHECK_IS_ON() |
+ last_added_.insert(picture_data.unique_id); |
+#endif // DCHECK_IS_ON() |
+ } |
+} |
+ |
+void BlimpClientPictureCache::RemoveUnusedPicturesFromCache( |
+ const std::vector<uint32_t>& removed) { |
+ for (const auto& it : removed) { |
+ auto entry = pictures_.find(it); |
+ DCHECK(entry != pictures_.end()); |
+ pictures_.erase(entry); |
+ } |
+} |
+ |
+sk_sp<const SkPicture> BlimpClientPictureCache::GetPictureFromCache( |
+ uint32_t engine_picture_id) { |
+ DCHECK(pictures_.find(engine_picture_id) != pictures_.end()); |
+ return pictures_[engine_picture_id]; |
+} |
+ |
+#if DCHECK_IS_ON() |
+void BlimpClientPictureCache::VerifyCacheUpdateMatchesReferenceTrackerData( |
+ const std::vector<uint32_t>& new_tracked_items) { |
+ DCHECK_EQ(new_tracked_items.size(), last_added_.size()); |
+ DCHECK(std::unordered_set<uint32_t>(new_tracked_items.begin(), |
+ new_tracked_items.end()) == last_added_); |
+ last_added_.clear(); |
+} |
+#endif // DCHECK_IS_ON() |
+ |
+} // namespace client |
+} // namespace blimp |