1 // Copyright 2017 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.
5 #ifndef CC_TILES_CHECKER_IMAGE_TRACKER_H_
6 #define CC_TILES_CHECKER_IMAGE_TRACKER_H_
8 #include <unordered_map>
11 #include "base/optional.h"
12 #include "cc/cc_export.h"
13 #include "cc/paint/image_id.h"
14 #include "cc/tiles/image_controller.h"
15 #include "third_party/skia/include/core/SkImage.h"
19 class CC_EXPORT CheckerImageTrackerClient {
21 virtual ~CheckerImageTrackerClient() = default;
23 virtual void NeedsInvalidationForCheckerImagedTiles() = 0;
26 // CheckerImageTracker is used to track the set of images in a frame which are
27 // decoded asynchronously, using the ImageDecodeService, from the rasterization
28 // of tiles which depend on them. Once decoded, these images are stored for
29 // invalidation on the next sync tree. TakeImagesToInvalidateOnSyncTree will
30 // return this set and maintain a copy to keeps these images locked until the
31 // sync tree is activated.
32 // Note: It is illegal to call TakeImagesToInvalidateOnSyncTree for the next
33 // sync tree until the previous tree is activated.
34 class CC_EXPORT CheckerImageTracker {
36 // The priority type for a decode. Note we use int to specify a decreasing
37 // order of priority with higher values.
38 enum DecodeType : int {
39 // Priority for images on tiles being rasterized (visible or pre-paint).
41 // Lowest priority for images on tiles in pre-decode region. These are tiles
42 // which are beyond the pre-paint region, but have their images decoded.
48 struct CC_EXPORT ImageDecodeRequest {
49 ImageDecodeRequest(PaintImage paint_image, DecodeType type);
50 PaintImage paint_image;
54 CheckerImageTracker(ImageController* image_controller,
55 CheckerImageTrackerClient* client,
56 bool enable_checker_imaging,
57 size_t min_image_bytes_to_checker);
58 CheckerImageTracker(const CheckerImageTracker&) = delete;
59 ~CheckerImageTracker();
61 CheckerImageTracker& operator=(const CheckerImageTracker&) = delete;
63 // Returns true if the decode for |image| will be deferred to the image decode
64 // service and it should be be skipped during raster.
65 bool ShouldCheckerImage(const DrawImage& image, WhichTree tree);
67 // Provides a prioritized queue of images to decode.
68 using ImageDecodeQueue = std::vector<ImageDecodeRequest>;
69 void ScheduleImageDecodeQueue(ImageDecodeQueue image_decode_queue);
71 // Disables scheduling any decode work by the tracker.
72 void SetNoDecodesAllowed();
74 // The max decode priority type that is allowed to run.
75 void SetMaxDecodePriorityAllowed(DecodeType decode_type);
77 // Returns the set of images to invalidate on the sync tree.
78 const PaintImageIdFlatSet& TakeImagesToInvalidateOnSyncTree();
80 // Called when the sync tree is activated. Each call to
81 // TakeImagesToInvalidateOnSyncTree() must be followed by this when the
82 // invalidated sync tree is activated.
83 void DidActivateSyncTree();
85 // Called to reset the tracker state on navigation. This will release all
86 // cached images. Setting |can_clear_decode_policy_tracking| will also result
87 // in re-checkering any images already decoded by the tracker.
88 void ClearTracker(bool can_clear_decode_policy_tracking);
90 // Informs the tracker to not checker the given image. This can be used to opt
91 // out of the checkering behavior for certain images, such as ones that were
92 // decoded using the img.decode api.
93 // Note that if the image is already being checkered, then it will continue to
94 // do so. This call is meant to be issued prior to the image appearing during
96 void DisallowCheckeringForImage(const PaintImage& image);
98 void set_force_disabled(bool force_disabled) {
99 force_disabled_ = force_disabled;
102 void UpdateImageDecodingHints(
103 base::flat_map<PaintImage::Id, PaintImage::DecodingMode>
106 bool has_locked_decodes_for_testing() const {
107 return !image_id_to_decode_.empty();
110 int decode_priority_allowed_for_testing() const {
111 return decode_priority_allowed_;
113 bool no_decodes_allowed_for_testing() const {
114 return decode_priority_allowed_ == kNoDecodeAllowedPriority;
116 PaintImage::DecodingMode get_decoding_mode_hint_for_testing(
118 CHECK(decoding_mode_map_.find(id) != decoding_mode_map_.end());
119 return decoding_mode_map_[id];
123 static const int kNoDecodeAllowedPriority;
125 enum class DecodePolicy {
126 // The image can be decoded asynchronously from raster. When set, the image
127 // is always skipped during rasterization of content that includes this
128 // image until it has been decoded using the decode service.
130 // The image has been decoded asynchronously once and should now be
131 // synchronously rasterized with the content or the image has been
132 // permanently vetoed from being decoded async.
136 // Contains the information to construct a DrawImage from PaintImage when
137 // queuing the image decode.
139 DecodePolicy policy = DecodePolicy::SYNC;
140 SkFilterQuality filter_quality = kNone_SkFilterQuality;
141 SkSize scale = SkSize::MakeEmpty();
142 gfx::ColorSpace color_space;
143 size_t frame_index = PaintImage::kDefaultFrameIndex;
146 // Wrapper to unlock an image decode requested from the ImageController on
148 class ScopedDecodeHolder {
150 ScopedDecodeHolder(ImageController* controller,
151 ImageController::ImageDecodeRequestId request_id)
152 : controller_(controller), request_id_(request_id) {}
153 ScopedDecodeHolder(const ScopedDecodeHolder&) = delete;
154 ~ScopedDecodeHolder() { controller_->UnlockImageDecode(request_id_); }
156 ScopedDecodeHolder& operator=(const ScopedDecodeHolder&) = delete;
159 ImageController* controller_;
160 ImageController::ImageDecodeRequestId request_id_;
163 void DidFinishImageDecode(PaintImage::Id image_id,
164 ImageController::ImageDecodeRequestId request_id,
165 ImageController::ImageDecodeResult result);
167 // Called when the next request in the |image_decode_queue_| should be
168 // scheduled with the image decode service.
169 void ScheduleNextImageDecode();
170 void UpdateDecodeState(const DrawImage& draw_image,
171 PaintImage::Id paint_image_id,
172 DecodeState* decode_state);
174 ImageController* image_controller_;
175 CheckerImageTrackerClient* client_;
176 const bool enable_checker_imaging_;
177 const size_t min_image_bytes_to_checker_;
179 // Disables checkering of all images if set. As opposed to
180 // |enable_checker_imaging_|, this setting can be toggled.
181 bool force_disabled_ = false;
183 // A set of images which have been decoded and are pending invalidation for
184 // raster on the checkered tiles.
185 PaintImageIdFlatSet images_pending_invalidation_;
187 // A set of images which were invalidated on the current sync tree.
188 PaintImageIdFlatSet invalidated_images_on_current_sync_tree_;
190 // The queue of images pending decode. We maintain a queue to ensure that the
191 // order in which images are decoded is aligned with the priority of the tiles
192 // dependent on these images.
193 ImageDecodeQueue image_decode_queue_;
195 // The max decode type that is allowed to run, if decodes are allowed to run.
196 int decode_priority_allowed_ = kNoDecodeAllowedPriority;
198 // The currently outstanding image decode that has been scheduled with the
199 // decode service. There can be only one outstanding decode at a time.
200 base::Optional<PaintImage> outstanding_image_decode_;
202 // A map of ImageId to its DecodePolicy.
203 std::unordered_map<PaintImage::Id, DecodeState> image_async_decode_state_;
205 // A map of image id to image decode request id for images to be unlocked.
206 std::unordered_map<PaintImage::Id, std::unique_ptr<ScopedDecodeHolder>>
209 base::flat_map<PaintImage::Id, PaintImage::DecodingMode> decoding_mode_map_;
211 base::WeakPtrFactory<CheckerImageTracker> weak_factory_{this};
216 #endif // CC_TILES_CHECKER_IMAGE_TRACKER_H_