Index: blimp/engine/renderer/blimp_engine_picture_cache.cc |
diff --git a/blimp/engine/renderer/blimp_engine_picture_cache.cc b/blimp/engine/renderer/blimp_engine_picture_cache.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..fd7f9cbde28f8f7e557a6aae3b9b0917eb5d5c2b |
--- /dev/null |
+++ b/blimp/engine/renderer/blimp_engine_picture_cache.cc |
@@ -0,0 +1,80 @@ |
+// 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/engine/renderer/blimp_engine_picture_cache.h" |
+ |
+#include <sstream> |
+ |
+#include "base/logging.h" |
+#include "base/memory/ptr_util.h" |
+#include "third_party/skia/include/core/SkStream.h" |
+ |
+namespace blimp { |
+namespace engine { |
+ |
+BlimpEnginePictureCache::BlimpEnginePictureCache( |
+ SkPixelSerializer* pixel_serializer) |
Kevin M
2016/06/11 00:29:33
DCHECK(pixel_serializer)
nyquist
2016/06/14 01:37:57
A nullptr pixel serializer ends up using the defau
|
+ : pixel_serializer_(pixel_serializer) {} |
+ |
+BlimpEnginePictureCache::~BlimpEnginePictureCache() = default; |
+ |
+void BlimpEnginePictureCache::MarkPictureForUnregistration( |
+ const SkPicture* picture) { |
Kevin M
2016/06/11 00:29:32
DCHECK(picture)
nyquist
2016/06/14 01:37:57
Done.
|
+ reference_tracker_.DecrementRefCount(picture->uniqueID()); |
+} |
+ |
+void BlimpEnginePictureCache::MarkPictureForRegistration( |
+ const SkPicture* picture) { |
Kevin M
2016/06/11 00:29:33
DCHECK(picture)
nyquist
2016/06/14 01:37:57
Done.
|
+ reference_tracker_.IncrementRefCount(picture->uniqueID()); |
+ |
+ // Do not serialize multiple times, even though the item is referred to from |
+ // multiple places. |
+ if (CacheContainsPicture(picture)) { |
+ return; |
+ } |
+ |
+ SerializePictureAndAddToCache(picture); |
+} |
+ |
+cc::PictureCacheUpdate BlimpEnginePictureCache::CalculateCacheUpdateAndFlush() { |
+ std::vector<uint32_t> added; |
+ std::vector<uint32_t> removed; |
+ reference_tracker_.CommitRefCounts(&added, &removed); |
+ |
+ // Create cache update consisting of new pictures. |
+ cc::PictureCacheUpdate update; |
+ for (const uint32_t item : added) { |
+ auto entry = pictures_.find(item); |
+ if (entry == pictures_.end()) { |
+ DLOG(ERROR) << "Picture map does not contain item " << item; |
Kevin M
2016/06/11 00:29:33
How messed up is it if this happens? If the answer
nyquist
2016/06/14 01:37:57
Pretty messed up.
Done.
|
+ continue; |
+ } |
+ update.push_back(entry->second); |
+ } |
+ |
+ // All new items will be sent to client, so clear everything. |
Kevin M
2016/06/11 00:29:32
the client
nyquist
2016/06/14 01:37:57
Done.
|
+ pictures_.clear(); |
+ |
+ return update; |
+} |
+ |
+bool BlimpEnginePictureCache::CacheContainsPicture(const SkPicture* picture) { |
+ return pictures_.find(picture->uniqueID()) != pictures_.end(); |
+} |
+ |
+void BlimpEnginePictureCache::SerializePictureAndAddToCache( |
+ const SkPicture* picture) { |
+ SkDynamicMemoryWStream stream; |
+ picture->serialize(&stream, pixel_serializer_); |
+ DCHECK_GE(stream.bytesWritten(), 0u); |
+ |
+ // Store the picture data until it is sent to the client. |
+ cc::PictureData picture_data; |
+ picture_data.unique_id = picture->uniqueID(); |
+ picture_data.data.reset(stream.copyToData()); |
+ pictures_[picture_data.unique_id] = picture_data; |
+} |
+ |
+} // namespace engine |
+} // namespace blimp |