Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(305)

Side by Side Diff: cc/tiles/gpu_image_decode_controller.h

Issue 1832573004: Gpu Image Decode Controller (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebase Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 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 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_GPU_IMAGE_DECODE_CONTROLLER_H_ 5 #ifndef CC_TILES_GPU_IMAGE_DECODE_CONTROLLER_H_
6 #define CC_TILES_GPU_IMAGE_DECODE_CONTROLLER_H_ 6 #define CC_TILES_GPU_IMAGE_DECODE_CONTROLLER_H_
7 7
8 #include <unordered_map> 8 #include <unordered_map>
9 #include <unordered_set> 9 #include <vector>
10 10
11 #include "base/containers/mru_cache.h"
12 #include "base/memory/discardable_memory.h"
13 #include "base/memory/scoped_ptr.h"
11 #include "base/synchronization/lock.h" 14 #include "base/synchronization/lock.h"
15 #include "base/trace_event/memory_dump_provider.h"
12 #include "cc/base/cc_export.h" 16 #include "cc/base/cc_export.h"
17 #include "cc/resources/resource_format.h"
13 #include "cc/tiles/image_decode_controller.h" 18 #include "cc/tiles/image_decode_controller.h"
19 #include "skia/ext/refptr.h"
20
21 class SkImageTextureData;
14 22
15 namespace cc { 23 namespace cc {
16 24
25 class ContextProvider;
26
27 // GpuImageDecodeController handles the decode and upload of images that will
28 // be used by Skia's GPU raster path. It also maintains a cache of these
29 // decoded/uploaded images for later re-use.
30 //
31 // Generally, when an image is required for raster, GpuImageDecodeController
32 // creates two tasks, one to decode the image, and one to upload the image to
33 // the GPU. These tasks are completed before the raster task which depends on
34 // the image. We need to seperate decode and upload tasks, as decode can occur
35 // simultaneously on multiple threads, while upload requires the GL context
36 // lock must happen on our non-concurrent raster thread.
37 //
38 // Decoded and Uploaded image data share a single cache entry. Depending on how
39 // far we've progressed, this cache entry may contain CPU-side decoded data,
40 // GPU-side uploaded data, or both. Because CPU-side decoded data is stored in
41 // discardable memory, and is only locked for short periods of time (until the
42 // upload completes), this memory is not counted against our sized cache
43 // limits. Uploaded GPU memory, being non-discardable, always counts against
44 // our limits.
45 //
46 // In cases where the number of images needed exceeds our cache limits, we
47 // operate in an "at-raster" mode. In this mode, there are no decode/upload
48 // tasks, and images are decoded/uploaded as needed, immediately before being
49 // used in raster. Cache entries for at-raster tasks are marked as such, which
50 // prevents future tasks from taking a dependency on them and extending their
51 // lifetime longer than is necessary.
17 class CC_EXPORT GpuImageDecodeController : public ImageDecodeController { 52 class CC_EXPORT GpuImageDecodeController : public ImageDecodeController {
18 public: 53 public:
19 GpuImageDecodeController(); 54 explicit GpuImageDecodeController(ContextProvider* context,
55 ResourceFormat decode_format);
20 ~GpuImageDecodeController() override; 56 ~GpuImageDecodeController() override;
21 57
22 // ImageDecodeController overrides. 58 // ImageDecodeController overrides.
59
60 // Finds the existing uploaded image for the provided DrawImage. Creates an
61 // upload task to upload the image if an exsiting image does not exist.
23 bool GetTaskForImageAndRef(const DrawImage& image, 62 bool GetTaskForImageAndRef(const DrawImage& image,
24 uint64_t prepare_tiles_id, 63 uint64_t prepare_tiles_id,
25 scoped_refptr<ImageDecodeTask>* task) override; 64 scoped_refptr<ImageDecodeTask>* task) override;
26 void UnrefImage(const DrawImage& image) override; 65 void UnrefImage(const DrawImage& image) override;
27 DecodedDrawImage GetDecodedImageForDraw(const DrawImage& draw_image) override; 66 DecodedDrawImage GetDecodedImageForDraw(const DrawImage& draw_image) override;
28 void DrawWithImageFinished(const DrawImage& image, 67 void DrawWithImageFinished(const DrawImage& image,
29 const DecodedDrawImage& decoded_image) override; 68 const DecodedDrawImage& decoded_image) override;
30 void ReduceCacheUsage() override; 69 void ReduceCacheUsage() override;
70 void SetShouldAggressivelyFreeResources(
71 bool aggressively_free_resources) override;
31 72
73 // Called by Decode / Upload tasks.
32 void DecodeImage(const DrawImage& image); 74 void DecodeImage(const DrawImage& image);
75 void UploadImage(const DrawImage& image);
76 void DecodeTaskCompleted(const DrawImage& image);
77 void UploadTaskCompleted(const DrawImage& image);
33 78
34 void RemovePendingTaskForImage(const DrawImage& image); 79 // For testing only.
80 void SetCachedItemLimitForTesting(size_t limit) {
81 cached_items_limit_ = limit;
82 }
83 void SetCachedBytesLimitForTesting(size_t limit) {
84 cached_bytes_limit_ = limit;
85 }
86 size_t GetBytesUsedForTesting() const { return bytes_used_; }
35 87
36 private: 88 private:
89 enum class DecodedDataMode { GPU, CPU };
90
91 // Stores the CPU-side decoded bits of an image and supporting fields.
92 struct DecodedImageData {
93 DecodedImageData();
94 ~DecodedImageData();
95
96 // May be null if image not yet decoded.
97 scoped_ptr<base::DiscardableMemory> data;
98 uint32_t ref_count;
99 bool is_locked;
100
101 // Set to true if the image was corrupt and could not be decoded.
102 bool decode_failure;
103 };
104
105 // Stores the GPU-side image and supporting fields.
106 struct UploadedImageData {
107 UploadedImageData();
108 ~UploadedImageData();
109
110 // May be null if image not yet uploaded / prepared.
111 skia::RefPtr<SkImage> image;
112 // True if the image is counting against our memory limits.
113 bool budgeted;
114 uint32_t ref_count;
115 };
116
117 struct ImageData {
118 ImageData(DecodedDataMode mode, size_t size);
119 ~ImageData();
120
121 const DecodedDataMode mode;
122 const size_t size;
123 bool is_at_raster;
124
125 DecodedImageData decode;
126 UploadedImageData upload;
127 };
128
129 using ImageDataMRUCache = base::MRUCache<uint32_t, scoped_ptr<ImageData>>;
130
131 // All private functions should only be called while holding |lock_|. Some
132 // functions also require the |context_| lock. These are indicated by
133 // additional comments.
134
135 // Similar to GetTaskForImageAndRef, but gets the dependent decode task
136 // rather than the upload task, if necessary.
137 scoped_refptr<ImageDecodeTask> GetImageDecodeTaskAndRef(
138 const DrawImage& image,
139 uint64_t prepare_tiles_id);
140
141 void RefImageDecode(const DrawImage& draw_image);
142 void UnrefImageDecode(const DrawImage& draw_image);
143 void RefImage(const DrawImage& draw_image);
144 void UnrefImageInternal(const DrawImage& draw_image);
145 void RefCountChanged(ImageData* image_data);
146
147 // Ensures that the cache can hold an element of |required_size|, freeing
148 // unreferenced cache entries if necessary to make room.
149 bool EnsureCapacity(size_t required_size);
150 bool CanFitSize(size_t size) const;
151 bool ExceedsPreferredCount() const;
152
153 void DecodeImageIfNecessary(const DrawImage& draw_image,
154 ImageData* image_data);
155
156 scoped_ptr<GpuImageDecodeController::ImageData> CreateImageData(
157 const DrawImage& image);
158 SkImageInfo CreateImageInfoForDrawImage(const DrawImage& draw_image) const;
159
160 // The following two functions also require the |context_| lock to be held.
161 void UploadImageIfNecessary(const DrawImage& draw_image,
162 ImageData* image_data);
163 void DeletePendingImages();
164
165 const ResourceFormat format_;
166 ContextProvider* context_;
167 skia::RefPtr<GrContextThreadSafeProxy> context_threadsafe_proxy_;
168
169 // All members below this point must only be accessed while holding |lock_|.
37 base::Lock lock_; 170 base::Lock lock_;
38 171
39 std::unordered_set<uint32_t> prerolled_images_;
40 std::unordered_map<uint32_t, scoped_refptr<ImageDecodeTask>> 172 std::unordered_map<uint32_t, scoped_refptr<ImageDecodeTask>>
41 pending_image_tasks_; 173 pending_image_upload_tasks_;
174 std::unordered_map<uint32_t, scoped_refptr<ImageDecodeTask>>
175 pending_image_decode_tasks_;
176
177 ImageDataMRUCache image_data_;
178
179 size_t cached_items_limit_;
180 size_t cached_bytes_limit_;
181 size_t bytes_used_;
182
183 // We can't release GPU backed SkImages without holding the context lock,
184 // so we add them to this list and defer deletion until the next time the lock
185 // is held.
186 std::vector<skia::RefPtr<SkImage>> images_pending_deletion_;
42 }; 187 };
43 188
44 } // namespace cc 189 } // namespace cc
45 190
46 #endif // CC_TILES_GPU_IMAGE_DECODE_CONTROLLER_H_ 191 #endif // CC_TILES_GPU_IMAGE_DECODE_CONTROLLER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698