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..df1bee25af84d97d9a2404ee7cb6115b2f1a6ae7 |
--- /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> |
Kevin M
2016/06/17 17:53:37
O_o
Is this needed?
nyquist
2016/06/17 21:31:43
Done.
|
+ |
+#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) |
+ : pixel_serializer_(pixel_serializer) {} |
+ |
+BlimpEnginePictureCache::~BlimpEnginePictureCache() = default; |
+ |
+void BlimpEnginePictureCache::MarkPictureForUnregistration( |
+ const SkPicture* picture) { |
+ DCHECK(picture); |
+ reference_tracker_.DecrementRefCount(picture->uniqueID()); |
+} |
+ |
+void BlimpEnginePictureCache::MarkPictureForRegistration( |
+ const SkPicture* picture) { |
+ DCHECK(picture); |
+ 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); |
+ DCHECK(entry != pictures_.end()); |
+ update.push_back(entry->second); |
+ } |
+ |
+ // All new items will be sent to the client, so clear everything. |
+ pictures_.clear(); |
+ |
+ return update; |
+} |
+ |
+bool BlimpEnginePictureCache::CacheContainsPicture( |
+ const SkPicture* picture) const { |
+ 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 |