[M108 Migration][VD] Avoid pending frame counter becoming negative
[platform/framework/web/chromium-efl.git] / cc / tiles / image_decode_cache.h
1 // Copyright 2015 The Chromium Authors
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_IMAGE_DECODE_CACHE_H_
6 #define CC_TILES_IMAGE_DECODE_CACHE_H_
7
8 #include "base/memory/ref_counted.h"
9 #include "base/notreached.h"
10 #include "cc/base/devtools_instrumentation.h"
11 #include "cc/cc_export.h"
12 #include "cc/paint/decoded_draw_image.h"
13 #include "cc/paint/draw_image.h"
14 #include "cc/raster/tile_task.h"
15 #include "cc/tiles/image_decode_cache_utils.h"
16 #include "cc/tiles/tile_priority.h"
17
18 namespace cc {
19
20 // ImageDecodeCache is responsible for generating decode tasks, decoding
21 // images, storing images in cache, and being able to return the decoded images
22 // when requested.
23
24 // ImageDecodeCache is responsible for the following things:
25 // 1. Given a DrawImage, it can return an TileTask which when run will
26 //    decode and cache the resulting image. If the image does not need a task to
27 //    be decoded, then nullptr will be returned. The return value of the
28 //    function indicates whether the image was or is going to be locked, so an
29 //    unlock will be required.
30 // 2. Given a cache key and a DrawImage, it can decode the image and store it in
31 //    the cache. Note that it is important that this function is only accessed
32 //    via an image decode task.
33 // 3. Given a DrawImage, it can return a DecodedDrawImage, which represented the
34 //    decoded version of the image. Note that if the image is not in the cache
35 //    and it needs to be scaled/decoded, then this decode will happen as part of
36 //    getting the image. As such, this should only be accessed from a raster
37 //    thread.
38 class CC_EXPORT ImageDecodeCache {
39  public:
40   enum class TaskType { kInRaster, kOutOfRaster };
41
42   // This information should be used strictly in tracing, UMA, and any other
43   // reporting systems.
44   struct TracingInfo {
45     TracingInfo(uint64_t prepare_tiles_id,
46                 TilePriority::PriorityBin requesting_tile_bin,
47                 TaskType task_type)
48         : prepare_tiles_id(prepare_tiles_id),
49           requesting_tile_bin(requesting_tile_bin),
50           task_type(task_type) {}
51     TracingInfo() = default;
52
53     // ID for the current prepare tiles call.
54     const uint64_t prepare_tiles_id = 0;
55
56     // The bin of the tile that caused this image to be requested.
57     const TilePriority::PriorityBin requesting_tile_bin = TilePriority::NOW;
58
59     // Whether the decode is requested as a part of tile rasterization.
60     const TaskType task_type = TaskType::kInRaster;
61   };
62
63   static devtools_instrumentation::ScopedImageDecodeTask::TaskType
64   ToScopedTaskType(TaskType task_type) {
65     using ScopedTaskType =
66         devtools_instrumentation::ScopedImageDecodeTask::TaskType;
67     switch (task_type) {
68       case TaskType::kInRaster:
69         return ScopedTaskType::kInRaster;
70       case TaskType::kOutOfRaster:
71         return ScopedTaskType::kOutOfRaster;
72     }
73     NOTREACHED();
74     return ScopedTaskType::kInRaster;
75   }
76
77   static devtools_instrumentation::ScopedImageDecodeTask::ImageType
78   ToScopedImageType(ImageType image_type) {
79     using ScopedImageType =
80         devtools_instrumentation::ScopedImageDecodeTask::ImageType;
81     switch (image_type) {
82       case ImageType::kJXL:
83         return ScopedImageType::kJxl;
84       case ImageType::kAVIF:
85         return ScopedImageType::kAvif;
86       case ImageType::kBMP:
87         return ScopedImageType::kBmp;
88       case ImageType::kGIF:
89         return ScopedImageType::kGif;
90       case ImageType::kICO:
91         return ScopedImageType::kIco;
92       case ImageType::kJPEG:
93         return ScopedImageType::kJpeg;
94       case ImageType::kPNG:
95         return ScopedImageType::kPng;
96       case ImageType::kWEBP:
97         return ScopedImageType::kWebP;
98       case ImageType::kInvalid:
99         return ScopedImageType::kOther;
100     }
101   }
102
103   virtual ~ImageDecodeCache() {}
104
105   struct CC_EXPORT TaskResult {
106     explicit TaskResult(bool need_unref,
107                         bool is_at_raster_decode,
108                         bool can_do_hardware_accelerated_decode);
109     explicit TaskResult(scoped_refptr<TileTask> task,
110                         bool can_do_hardware_accelerated_decode);
111     TaskResult(const TaskResult& result);
112     ~TaskResult();
113
114     scoped_refptr<TileTask> task;
115     bool need_unref = false;
116     bool is_at_raster_decode = false;
117     bool can_do_hardware_accelerated_decode = false;
118   };
119   // Fill in an TileTask which will decode the given image when run. In
120   // case the image is already cached, fills in nullptr. Returns true if the
121   // image needs to be unreffed when the caller is finished with it.
122   //
123   // This is called by the tile manager (on the compositor thread) when creating
124   // a raster task.
125   virtual TaskResult GetTaskForImageAndRef(const DrawImage& image,
126                                            const TracingInfo& tracing_info) = 0;
127   // Similar to GetTaskForImageAndRef, except that it returns tasks that are not
128   // meant to be run as part of raster. That is, this is part of a predecode
129   // API. Note that this should only return a task responsible for decoding (and
130   // not uploading), since it will be run on a worker thread which may not have
131   // the right GPU context for upload.
132   virtual TaskResult GetOutOfRasterDecodeTaskForImageAndRef(
133       const DrawImage& image) = 0;
134
135   // Unrefs an image. When the tile is finished, this should be called for every
136   // GetTaskForImageAndRef call that returned true.
137   virtual void UnrefImage(const DrawImage& image) = 0;
138
139   // Returns a decoded draw image. This may cause a decode if the image was not
140   // predecoded.
141   //
142   // This is called by a raster task (on a worker thread) when an image is
143   // required.
144   //
145   // TODO(khushalsagar/vmpstr): Since the cache knows if it's a video frame, it
146   // should discard any frames from the same source not in use in the
147   // compositor.
148   virtual DecodedDrawImage GetDecodedImageForDraw(const DrawImage& image) = 0;
149   // Unrefs an image. This should be called for every GetDecodedImageForDraw
150   // when the draw with the image is finished.
151   virtual void DrawWithImageFinished(const DrawImage& image,
152                                      const DecodedDrawImage& decoded_image) = 0;
153
154   // This function informs the cache that now is a good time to clean up
155   // memory. This is called periodically from the compositor thread.
156   virtual void ReduceCacheUsage() = 0;
157
158   // This function informs the cache that we are hidden and should not be
159   // retaining cached resources longer than needed.
160   virtual void SetShouldAggressivelyFreeResources(
161       bool aggressively_free_resources) = 0;
162
163   // Clears all elements from the cache.
164   virtual void ClearCache() = 0;
165
166   // Returns the maximum amount of memory we would be able to lock. This ignores
167   // any temporary states, such as throttled, and return the maximum possible
168   // memory. It is used as an esimate of whether an image can fit into the
169   // locked budget before creating a task.
170   virtual size_t GetMaximumMemoryLimitBytes() const = 0;
171
172   // Returns true if the cache should be used for |image|. In certain cases the
173   // image can directly be used for raster (for instance bitmaps in a software
174   // draw).
175   virtual bool UseCacheForDrawImage(const DrawImage& image) const = 0;
176
177   // Should be called periodically to record statistics about cache use and
178   // performance.
179   virtual void RecordStats() = 0;
180 };
181
182 }  // namespace cc
183
184 #endif  // CC_TILES_IMAGE_DECODE_CACHE_H_