OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "blimp/engine/renderer/blimp_engine_picture_cache.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #include "base/memory/ptr_util.h" |
| 9 #include "third_party/skia/include/core/SkStream.h" |
| 10 |
| 11 namespace blimp { |
| 12 namespace engine { |
| 13 |
| 14 BlimpEnginePictureCache::BlimpEnginePictureCache( |
| 15 SkPixelSerializer* pixel_serializer) |
| 16 : pixel_serializer_(pixel_serializer) {} |
| 17 |
| 18 BlimpEnginePictureCache::~BlimpEnginePictureCache() = default; |
| 19 |
| 20 void BlimpEnginePictureCache::MarkUsed(const SkPicture* picture) { |
| 21 DCHECK(picture); |
| 22 reference_tracker_.IncrementRefCount(picture->uniqueID()); |
| 23 |
| 24 // Do not serialize multiple times, even though the item is referred to from |
| 25 // multiple places. |
| 26 if (pictures_.find(picture->uniqueID()) != pictures_.end()) { |
| 27 return; |
| 28 } |
| 29 |
| 30 Put(picture); |
| 31 } |
| 32 |
| 33 std::vector<cc::PictureData> |
| 34 BlimpEnginePictureCache::CalculateCacheUpdateAndFlush() { |
| 35 std::vector<uint32_t> added; |
| 36 std::vector<uint32_t> removed; |
| 37 reference_tracker_.CommitRefCounts(&added, &removed); |
| 38 |
| 39 // Create cache update consisting of new pictures. |
| 40 std::vector<cc::PictureData> update; |
| 41 for (const uint32_t item : added) { |
| 42 auto entry = pictures_.find(item); |
| 43 DCHECK(entry != pictures_.end()); |
| 44 update.push_back(entry->second); |
| 45 } |
| 46 |
| 47 // All new items will be sent to the client, so clear everything. |
| 48 pictures_.clear(); |
| 49 reference_tracker_.ClearRefCounts(); |
| 50 |
| 51 return update; |
| 52 } |
| 53 |
| 54 void BlimpEnginePictureCache::Put(const SkPicture* picture) { |
| 55 SkDynamicMemoryWStream stream; |
| 56 picture->serialize(&stream, pixel_serializer_); |
| 57 DCHECK_GE(stream.bytesWritten(), 0u); |
| 58 |
| 59 // Store the picture data until it is sent to the client. |
| 60 pictures_.insert( |
| 61 std::make_pair(picture->uniqueID(), |
| 62 cc::PictureData(picture->uniqueID(), |
| 63 sk_sp<SkData>(stream.copyToData())))); |
| 64 } |
| 65 |
| 66 } // namespace engine |
| 67 } // namespace blimp |
OLD | NEW |