OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CC_TILES_IMAGE_DECODE_CONTROLLER_H_ | 5 #ifndef CC_TILES_IMAGE_DECODE_CONTROLLER_H_ |
6 #define CC_TILES_IMAGE_DECODE_CONTROLLER_H_ | 6 #define CC_TILES_IMAGE_DECODE_CONTROLLER_H_ |
7 | 7 |
8 #include "base/containers/hash_tables.h" | 8 #include "base/containers/hash_tables.h" |
9 #include "base/memory/discardable_memory_allocator.h" | |
9 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
11 #include "base/numerics/safe_math.h" | |
10 #include "cc/base/cc_export.h" | 12 #include "cc/base/cc_export.h" |
11 #include "cc/playback/discardable_image_map.h" | 13 #include "cc/playback/discardable_image_map.h" |
12 #include "cc/raster/tile_task_runner.h" | 14 #include "cc/raster/tile_task_runner.h" |
13 #include "skia/ext/refptr.h" | 15 #include "skia/ext/refptr.h" |
14 | 16 |
17 #include "ui/gfx/transform.h" | |
18 | |
15 namespace cc { | 19 namespace cc { |
16 | 20 |
21 class ImageDecodeControllerKey { | |
22 public: | |
23 static ImageDecodeControllerKey FromDrawImage(const DrawImage& image); | |
24 | |
25 ImageDecodeControllerKey(uint32_t image_id, | |
26 const gfx::Size& size, | |
27 SkFilterQuality filter_quality); | |
28 | |
29 bool operator==(const ImageDecodeControllerKey& other) const { | |
30 return image_id_ == other.image_id_ && size_ == other.size_ && | |
31 filter_quality_ == other.filter_quality_; | |
32 } | |
33 | |
34 uint32_t image_id() const { return image_id_; } | |
35 gfx::Size target_size() const { return size_; } | |
36 size_t target_bytes() const { | |
37 base::CheckedNumeric<size_t> result = 4; | |
38 result *= size_.width(); | |
39 result *= size_.height(); | |
40 return result.ValueOrDefault(std::numeric_limits<size_t>::max()); | |
41 } | |
42 SkFilterQuality filter_quality() const { return filter_quality_; } | |
43 | |
44 std::string ToString() const; | |
45 | |
46 private: | |
47 uint32_t image_id_; | |
48 gfx::Size size_; | |
49 SkFilterQuality filter_quality_; | |
50 }; | |
51 | |
52 } // namespace cc | |
53 | |
54 namespace BASE_HASH_NAMESPACE { | |
55 template <> | |
56 struct hash<cc::ImageDecodeControllerKey> { | |
57 size_t operator()(const cc::ImageDecodeControllerKey& key) const { | |
58 return base::HashPair( | |
59 base::HashPair(key.target_size().width(), key.target_size().height()), | |
60 base::HashPair(key.image_id(), key.filter_quality())); | |
61 } | |
62 }; | |
63 } // namespace BASE_HASH_NAMESPACE | |
64 | |
65 namespace cc { | |
17 class ImageDecodeController { | 66 class ImageDecodeController { |
18 public: | 67 public: |
68 using ImageKey = ImageDecodeControllerKey; | |
69 | |
19 ImageDecodeController(); | 70 ImageDecodeController(); |
20 ~ImageDecodeController(); | 71 ~ImageDecodeController(); |
21 | 72 |
22 scoped_refptr<ImageDecodeTask> GetTaskForImage(const DrawImage& image, | 73 // Returns an ImageDecodeTask which will decode the given image when run. In |
23 int layer_id, | 74 // case the image is already cached, returns nullptr. |
24 uint64_t prepare_tiles_id); | 75 // Note that every time this function is called, a ref count is accumulated. |
76 // This count is decremented when UnlockImageForTile is called. When the | |
77 // count reaches 0, the image is unpinned. | |
78 scoped_refptr<ImageDecodeTask> GetTaskForImageAndRef( | |
79 const DrawImage& image, | |
80 uint64_t prepare_tiles_id); | |
81 DecodedDrawImage GetDecodedImageAndRef(const DrawImage& image); | |
82 | |
83 void DrawWithImageFinished(const DrawImage& image); | |
enne (OOO)
2015/10/29 22:59:45
What's the Finished part here mean? Could this fun
| |
84 void UnrefImage(const DrawImage& image); | |
25 | 85 |
26 // Note that this function has to remain thread safe. | 86 // Note that this function has to remain thread safe. |
enne (OOO)
2015/10/29 22:59:45
Should other functions check that they're called o
| |
27 void DecodeImage(const SkImage* image); | 87 void DecodeImage(const ImageKey& key, const DrawImage& image); |
28 | 88 |
29 // TODO(vmpstr): This should go away once the controller is decoding images | 89 void OnImageDecodeTaskCompleted(const ImageKey& key, bool was_canceled); |
30 // based on priority and memory. | 90 void ReduceCacheUsage(); |
31 void AddLayerUsedCount(int layer_id); | |
32 void SubtractLayerUsedCount(int layer_id); | |
33 | |
34 void OnImageDecodeTaskCompleted(int layer_id, | |
35 const SkImage* image, | |
36 bool was_canceled); | |
37 | 91 |
38 private: | 92 private: |
39 scoped_refptr<ImageDecodeTask> CreateTaskForImage(const SkImage* image, | 93 class DecodedImage : public base::RefCounted<DecodedImage> { |
40 int layer_id, | 94 public: |
95 DecodedImage(const SkImageInfo& info, | |
96 scoped_ptr<base::DiscardableMemory> memory); | |
97 | |
98 SkImage* image() const { | |
99 DCHECK(locked_); | |
100 return image_.get(); | |
101 } | |
102 | |
103 bool is_locked() const { return locked_; } | |
104 bool Lock(); | |
105 void Unlock(); | |
106 | |
107 private: | |
108 friend class base::RefCounted<DecodedImage>; | |
109 | |
110 ~DecodedImage(); | |
111 void CreateImageFromLockedMemory(); | |
112 | |
113 bool locked_; | |
114 SkImageInfo image_info_; | |
115 scoped_ptr<base::DiscardableMemory> memory_; | |
116 skia::RefPtr<SkImage> image_; | |
117 }; | |
118 | |
119 class MemoryBudget { | |
120 public: | |
121 explicit MemoryBudget(size_t limit_bytes); | |
122 | |
123 size_t AvailableMemoryBytes() const; | |
124 void AddUsage(size_t usage); | |
125 void SubtractUsage(size_t usage); | |
126 void ResetUsage(); | |
127 bool NeedsReset() const; | |
128 | |
129 private: | |
130 size_t GetCurrentUsageSafe() const; | |
131 | |
132 size_t limit_bytes_; | |
133 base::CheckedNumeric<size_t> current_usage_bytes_; | |
134 }; | |
135 | |
136 scoped_refptr<ImageDecodeTask> CreateTaskForImage(const ImageKey& key, | |
137 const DrawImage& image, | |
41 uint64_t prepare_tiles_id); | 138 uint64_t prepare_tiles_id); |
139 bool LockDecodedImageIfPossibleAndRef(const ImageKey& key); | |
140 scoped_refptr<DecodedImage> DecodeImageInternal(const ImageKey& key, | |
141 const SkImage* image); | |
142 bool CanHandleFilterQuality(SkFilterQuality filter_quality); | |
143 void SanityCheckState(int line, bool lock_acquired); | |
42 | 144 |
43 using ImageTaskMap = base::hash_map<uint32_t, scoped_refptr<ImageDecodeTask>>; | 145 base::hash_map<ImageKey, scoped_refptr<ImageDecodeTask>> pending_image_tasks_; |
44 using LayerImageTaskMap = base::hash_map<int, ImageTaskMap>; | 146 using AnnotatedDecodedImage = |
45 LayerImageTaskMap image_decode_tasks_; | 147 std::pair<ImageKey, scoped_refptr<DecodedImage>>; |
46 | 148 |
47 using LayerCountMap = base::hash_map<int, int>; | 149 // The rest of the members can only be accessed if the lock is held to ensure |
48 LayerCountMap used_layer_counts_; | 150 // that they are safe to access on multiple threads. |
151 base::Lock lock_; | |
152 std::deque<AnnotatedDecodedImage> decoded_images_; | |
153 base::hash_map<ImageKey, int> decoded_images_ref_counts_; | |
154 MemoryBudget locked_images_budget_; | |
49 }; | 155 }; |
50 | 156 |
51 } // namespace cc | 157 } // namespace cc |
52 | 158 |
53 #endif // CC_TILES_IMAGE_DECODE_CONTROLLER_H_ | 159 #endif // CC_TILES_IMAGE_DECODE_CONTROLLER_H_ |
OLD | NEW |