Chromium Code Reviews| 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..793372bc5a0222bf679b29ca11b0b4a50b67ec06 |
| --- /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 { |
| + |
| +// 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); |
|
vmpstr
2016/06/16 22:09:58
That's a silly function. Why not sure put the code
nyquist
2016/06/24 11:11:13
Done.
|
| +} |
| + |
| +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 = |
|
vmpstr
2016/06/16 22:09:58
I'm kind of nit picking here, but maybe
DCHECK(pi
nyquist
2016/06/24 11:11:13
Done.
|
| + 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) { |
|
vmpstr
2016/06/16 22:09:58
Don't call it |it| if it's not an iterator
nyquist
2016/06/24 11:11:13
Done.
|
| + auto entry = pictures_.find(it); |
| + DCHECK(entry != pictures_.end()); |
|
vmpstr
2016/06/16 22:09:58
likewise here,
DCHECK(pictures_.find(it) != pictu
nyquist
2016/06/24 11:11:13
Done.
|
| + 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() |
|
vmpstr
2016/06/16 22:09:58
Just to save on #if everywhere you call this, you
nyquist
2016/06/24 11:11:13
Done.
|
| +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 |