1a8229feaf567adcdecf2ea2fe55547f035736be
[platform/framework/web/chromium-efl.git] / cc / tiles / checker_image_tracker.h
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.
4
5 #ifndef CC_TILES_CHECKER_IMAGE_TRACKER_H_
6 #define CC_TILES_CHECKER_IMAGE_TRACKER_H_
7
8 #include <unordered_map>
9 #include <vector>
10
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"
16
17 namespace cc {
18
19 class CC_EXPORT CheckerImageTrackerClient {
20  public:
21   virtual ~CheckerImageTrackerClient() = default;
22
23   virtual void NeedsInvalidationForCheckerImagedTiles() = 0;
24 };
25
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 {
35  public:
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).
40     kRaster = 0,
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.
43     kPreDecode = 1,
44
45     kLast = kPreDecode
46   };
47
48   struct CC_EXPORT ImageDecodeRequest {
49     ImageDecodeRequest(PaintImage paint_image, DecodeType type);
50     PaintImage paint_image;
51     DecodeType type;
52   };
53
54   CheckerImageTracker(ImageController* image_controller,
55                       CheckerImageTrackerClient* client,
56                       bool enable_checker_imaging,
57                       size_t min_image_bytes_to_checker);
58   ~CheckerImageTracker();
59
60   // Returns true if the decode for |image| will be deferred to the image decode
61   // service and it should be be skipped during raster.
62   bool ShouldCheckerImage(const DrawImage& image, WhichTree tree);
63
64   // Provides a prioritized queue of images to decode.
65   using ImageDecodeQueue = std::vector<ImageDecodeRequest>;
66   void ScheduleImageDecodeQueue(ImageDecodeQueue image_decode_queue);
67
68   // Disables scheduling any decode work by the tracker.
69   void SetNoDecodesAllowed();
70
71   // The max decode priority type that is allowed to run.
72   void SetMaxDecodePriorityAllowed(DecodeType decode_type);
73
74   // Returns the set of images to invalidate on the sync tree.
75   const PaintImageIdFlatSet& TakeImagesToInvalidateOnSyncTree();
76
77   // Called when the sync tree is activated. Each call to
78   // TakeImagesToInvalidateOnSyncTree() must be followed by this when the
79   // invalidated sync tree is activated.
80   void DidActivateSyncTree();
81
82   // Called to reset the tracker state on navigation. This will release all
83   // cached images. Setting |can_clear_decode_policy_tracking| will also result
84   // in re-checkering any images already decoded by the tracker.
85   void ClearTracker(bool can_clear_decode_policy_tracking);
86
87   // Informs the tracker to not checker the given image. This can be used to opt
88   // out of the checkering behavior for certain images, such as ones that were
89   // decoded using the img.decode api.
90   // Note that if the image is already being checkered, then it will continue to
91   // do so. This call is meant to be issued prior to the image appearing during
92   // raster.
93   void DisallowCheckeringForImage(const PaintImage& image);
94
95   void set_force_disabled(bool force_disabled) {
96     force_disabled_ = force_disabled;
97   }
98
99   void UpdateImageDecodingHints(
100       base::flat_map<PaintImage::Id, PaintImage::DecodingMode>
101           decoding_mode_map);
102
103   bool has_locked_decodes_for_testing() const {
104     return !image_id_to_decode_.empty();
105   }
106
107   int decode_priority_allowed_for_testing() const {
108     return decode_priority_allowed_;
109   }
110   bool no_decodes_allowed_for_testing() const {
111     return decode_priority_allowed_ == kNoDecodeAllowedPriority;
112   }
113   PaintImage::DecodingMode get_decoding_mode_hint_for_testing(
114       PaintImage::Id id) {
115     CHECK(decoding_mode_map_.find(id) != decoding_mode_map_.end());
116     return decoding_mode_map_[id];
117   }
118
119  private:
120   static const int kNoDecodeAllowedPriority;
121
122   enum class DecodePolicy {
123     // The image can be decoded asynchronously from raster. When set, the image
124     // is always skipped during rasterization of content that includes this
125     // image until it has been decoded using the decode service.
126     ASYNC,
127     // The image has been decoded asynchronously once and should now be
128     // synchronously rasterized with the content or the image has been
129     // permanently vetoed from being decoded async.
130     SYNC
131   };
132
133   // Contains the information to construct a DrawImage from PaintImage when
134   // queuing the image decode.
135   struct DecodeState {
136     DecodePolicy policy = DecodePolicy::SYNC;
137     SkFilterQuality filter_quality = kNone_SkFilterQuality;
138     SkSize scale = SkSize::MakeEmpty();
139     size_t frame_index = PaintImage::kDefaultFrameIndex;
140   };
141
142   // Wrapper to unlock an image decode requested from the ImageController on
143   // destruction.
144   class ScopedDecodeHolder {
145    public:
146     ScopedDecodeHolder(ImageController* controller,
147                        ImageController::ImageDecodeRequestId request_id)
148         : controller_(controller), request_id_(request_id) {}
149     ~ScopedDecodeHolder() { controller_->UnlockImageDecode(request_id_); }
150
151    private:
152     ImageController* controller_;
153     ImageController::ImageDecodeRequestId request_id_;
154
155     DISALLOW_COPY_AND_ASSIGN(ScopedDecodeHolder);
156   };
157
158   void DidFinishImageDecode(PaintImage::Id image_id,
159                             ImageController::ImageDecodeRequestId request_id,
160                             ImageController::ImageDecodeResult result);
161
162   // Called when the next request in the |image_decode_queue_| should be
163   // scheduled with the image decode service.
164   void ScheduleNextImageDecode();
165   void UpdateDecodeState(const DrawImage& draw_image,
166                          PaintImage::Id paint_image_id,
167                          DecodeState* decode_state);
168
169   ImageController* image_controller_;
170   CheckerImageTrackerClient* client_;
171   const bool enable_checker_imaging_;
172   const size_t min_image_bytes_to_checker_;
173
174   // Disables checkering of all images if set. As opposed to
175   // |enable_checker_imaging_|, this setting can be toggled.
176   bool force_disabled_ = false;
177
178   // A set of images which have been decoded and are pending invalidation for
179   // raster on the checkered tiles.
180   PaintImageIdFlatSet images_pending_invalidation_;
181
182   // A set of images which were invalidated on the current sync tree.
183   PaintImageIdFlatSet invalidated_images_on_current_sync_tree_;
184
185   // The queue of images pending decode. We maintain a queue to ensure that the
186   // order in which images are decoded is aligned with the priority of the tiles
187   // dependent on these images.
188   ImageDecodeQueue image_decode_queue_;
189
190   // The max decode type that is allowed to run, if decodes are allowed to run.
191   int decode_priority_allowed_ = kNoDecodeAllowedPriority;
192
193   // The currently outstanding image decode that has been scheduled with the
194   // decode service. There can be only one outstanding decode at a time.
195   base::Optional<PaintImage> outstanding_image_decode_;
196
197   // A map of ImageId to its DecodePolicy.
198   std::unordered_map<PaintImage::Id, DecodeState> image_async_decode_state_;
199
200   // A map of image id to image decode request id for images to be unlocked.
201   std::unordered_map<PaintImage::Id, std::unique_ptr<ScopedDecodeHolder>>
202       image_id_to_decode_;
203
204   base::flat_map<PaintImage::Id, PaintImage::DecodingMode> decoding_mode_map_;
205
206   base::WeakPtrFactory<CheckerImageTracker> weak_factory_;
207
208   DISALLOW_COPY_AND_ASSIGN(CheckerImageTracker);
209 };
210
211 }  // namespace cc
212
213 #endif  // CC_TILES_CHECKER_IMAGE_TRACKER_H_