OLD | NEW |
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_CACHE_H_ |
6 #define CC_TILES_GPU_IMAGE_DECODE_CONTROLLER_H_ | 6 #define CC_TILES_GPU_IMAGE_DECODE_CACHE_H_ |
7 | 7 |
8 #include <memory> | 8 #include <memory> |
9 #include <unordered_map> | 9 #include <unordered_map> |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
12 #include "base/containers/mru_cache.h" | 12 #include "base/containers/mru_cache.h" |
13 #include "base/memory/discardable_memory.h" | 13 #include "base/memory/discardable_memory.h" |
14 #include "base/memory/memory_coordinator_client.h" | 14 #include "base/memory/memory_coordinator_client.h" |
15 #include "base/synchronization/lock.h" | 15 #include "base/synchronization/lock.h" |
16 #include "base/trace_event/memory_dump_provider.h" | 16 #include "base/trace_event/memory_dump_provider.h" |
17 #include "cc/base/cc_export.h" | 17 #include "cc/base/cc_export.h" |
18 #include "cc/resources/resource_format.h" | 18 #include "cc/resources/resource_format.h" |
19 #include "cc/tiles/image_decode_controller.h" | 19 #include "cc/tiles/image_decode_cache.h" |
20 #include "third_party/skia/include/core/SkRefCnt.h" | 20 #include "third_party/skia/include/core/SkRefCnt.h" |
21 | 21 |
22 namespace cc { | 22 namespace cc { |
23 | 23 |
24 class ContextProvider; | 24 class ContextProvider; |
25 | 25 |
26 // OVERVIEW: | 26 // OVERVIEW: |
27 // | 27 // |
28 // GpuImageDecodeController handles the decode and upload of images that will | 28 // GpuImageDecodeCache handles the decode and upload of images that will |
29 // be used by Skia's GPU raster path. It also maintains a cache of these | 29 // be used by Skia's GPU raster path. It also maintains a cache of these |
30 // decoded/uploaded images for later re-use. | 30 // decoded/uploaded images for later re-use. |
31 // | 31 // |
32 // Generally, when an image is required for raster, GpuImageDecodeController | 32 // Generally, when an image is required for raster, GpuImageDecodeCache |
33 // creates two tasks, one to decode the image, and one to upload the image to | 33 // creates two tasks, one to decode the image, and one to upload the image to |
34 // the GPU. These tasks are completed before the raster task which depends on | 34 // the GPU. These tasks are completed before the raster task which depends on |
35 // the image. We need to seperate decode and upload tasks, as decode can occur | 35 // the image. We need to seperate decode and upload tasks, as decode can occur |
36 // simultaneously on multiple threads, while upload requires the GL context | 36 // simultaneously on multiple threads, while upload requires the GL context |
37 // lock must happen on our non-concurrent raster thread. | 37 // lock must happen on our non-concurrent raster thread. |
38 // | 38 // |
39 // Decoded and Uploaded image data share a single cache entry. Depending on how | 39 // Decoded and Uploaded image data share a single cache entry. Depending on how |
40 // far we've progressed, this cache entry may contain CPU-side decoded data, | 40 // far we've progressed, this cache entry may contain CPU-side decoded data, |
41 // GPU-side uploaded data, or both. Because CPU-side decoded data is stored in | 41 // GPU-side uploaded data, or both. Because CPU-side decoded data is stored in |
42 // discardable memory, and is only locked for short periods of time (until the | 42 // discardable memory, and is only locked for short periods of time (until the |
(...skipping 24 matching lines...) Expand all Loading... |
67 // | 67 // |
68 // The second cache, |in_use_cache_|, stores one image data per DrawImage - | 68 // The second cache, |in_use_cache_|, stores one image data per DrawImage - |
69 // this may be the same ImageData that is in the persistent_cache_. These | 69 // this may be the same ImageData that is in the persistent_cache_. These |
70 // cache entries are more transient and are deleted as soon as all refs to the | 70 // cache entries are more transient and are deleted as soon as all refs to the |
71 // given DrawImage are released (the image is no longer in-use). | 71 // given DrawImage are released (the image is no longer in-use). |
72 // | 72 // |
73 // For examples of raster-scale caching, see https://goo.gl/0zCd9Z | 73 // For examples of raster-scale caching, see https://goo.gl/0zCd9Z |
74 // | 74 // |
75 // REF COUNTING: | 75 // REF COUNTING: |
76 // | 76 // |
77 // In dealing with the two caches in GpuImageDecodeController, there are three | 77 // In dealing with the two caches in GpuImageDecodeCache, there are three |
78 // ref-counting concepts in use: | 78 // ref-counting concepts in use: |
79 // 1) ImageData upload/decode ref-counts. | 79 // 1) ImageData upload/decode ref-counts. |
80 // These ref-counts represent the overall number of references to the | 80 // These ref-counts represent the overall number of references to the |
81 // upload or decode portion of an ImageData. These ref-counts control | 81 // upload or decode portion of an ImageData. These ref-counts control |
82 // both whether the upload/decode data can be freed, as well as whether an | 82 // both whether the upload/decode data can be freed, as well as whether an |
83 // ImageData can be removed from the |persistent_cache_|. ImageDatas are | 83 // ImageData can be removed from the |persistent_cache_|. ImageDatas are |
84 // only removed from the |persistent_cache_| if their upload/decode | 84 // only removed from the |persistent_cache_| if their upload/decode |
85 // ref-counts are zero or if they are orphaned and replaced by a new entry. | 85 // ref-counts are zero or if they are orphaned and replaced by a new entry. |
86 // 2) InUseCacheEntry ref-counts. | 86 // 2) InUseCacheEntry ref-counts. |
87 // These ref-counts represent the number of references to an | 87 // These ref-counts represent the number of references to an |
88 // InUseCacheEntry from a specific DrawImage. When the InUseCacheEntry's | 88 // InUseCacheEntry from a specific DrawImage. When the InUseCacheEntry's |
89 // ref-count reaches 0 it will be deleted. | 89 // ref-count reaches 0 it will be deleted. |
90 // 3) scoped_refptr ref-counts. | 90 // 3) scoped_refptr ref-counts. |
91 // Because both the persistent_cache_ and the in_use_cache_ point at the | 91 // Because both the persistent_cache_ and the in_use_cache_ point at the |
92 // same ImageDatas (and may need to keep these ImageDatas alive independent | 92 // same ImageDatas (and may need to keep these ImageDatas alive independent |
93 // of each other), they hold ImageDatas by scoped_refptr. The scoped_refptr | 93 // of each other), they hold ImageDatas by scoped_refptr. The scoped_refptr |
94 // keeps an ImageData alive while it is present in either the | 94 // keeps an ImageData alive while it is present in either the |
95 // |persistent_cache_| or |in_use_cache_|. | 95 // |persistent_cache_| or |in_use_cache_|. |
96 // | 96 // |
97 class CC_EXPORT GpuImageDecodeController | 97 class CC_EXPORT GpuImageDecodeCache |
98 : public ImageDecodeController, | 98 : public ImageDecodeCache, |
99 public base::trace_event::MemoryDumpProvider, | 99 public base::trace_event::MemoryDumpProvider, |
100 public base::MemoryCoordinatorClient { | 100 public base::MemoryCoordinatorClient { |
101 public: | 101 public: |
102 explicit GpuImageDecodeController(ContextProvider* context, | 102 explicit GpuImageDecodeCache(ContextProvider* context, |
103 ResourceFormat decode_format, | 103 ResourceFormat decode_format, |
104 size_t max_gpu_image_bytes); | 104 size_t max_gpu_image_bytes); |
105 ~GpuImageDecodeController() override; | 105 ~GpuImageDecodeCache() override; |
106 | 106 |
107 // ImageDecodeController overrides. | 107 // ImageDecodeCache overrides. |
108 | 108 |
109 // Finds the existing uploaded image for the provided DrawImage. Creates an | 109 // Finds the existing uploaded image for the provided DrawImage. Creates an |
110 // upload task to upload the image if an exsiting image does not exist. | 110 // upload task to upload the image if an exsiting image does not exist. |
111 bool GetTaskForImageAndRef(const DrawImage& image, | 111 bool GetTaskForImageAndRef(const DrawImage& image, |
112 const TracingInfo& tracing_info, | 112 const TracingInfo& tracing_info, |
113 scoped_refptr<TileTask>* task) override; | 113 scoped_refptr<TileTask>* task) override; |
114 void UnrefImage(const DrawImage& image) override; | 114 void UnrefImage(const DrawImage& image) override; |
115 DecodedDrawImage GetDecodedImageForDraw(const DrawImage& draw_image) override; | 115 DecodedDrawImage GetDecodedImageForDraw(const DrawImage& draw_image) override; |
116 void DrawWithImageFinished(const DrawImage& image, | 116 void DrawWithImageFinished(const DrawImage& image, |
117 const DecodedDrawImage& decoded_image) override; | 117 const DecodedDrawImage& decoded_image) override; |
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
275 | 275 |
276 // Ensures that the cache can hold an element of |required_size|, freeing | 276 // Ensures that the cache can hold an element of |required_size|, freeing |
277 // unreferenced cache entries if necessary to make room. | 277 // unreferenced cache entries if necessary to make room. |
278 bool EnsureCapacity(size_t required_size); | 278 bool EnsureCapacity(size_t required_size); |
279 bool CanFitSize(size_t size) const; | 279 bool CanFitSize(size_t size) const; |
280 bool ExceedsPreferredCount() const; | 280 bool ExceedsPreferredCount() const; |
281 | 281 |
282 void DecodeImageIfNecessary(const DrawImage& draw_image, | 282 void DecodeImageIfNecessary(const DrawImage& draw_image, |
283 ImageData* image_data); | 283 ImageData* image_data); |
284 | 284 |
285 scoped_refptr<GpuImageDecodeController::ImageData> CreateImageData( | 285 scoped_refptr<GpuImageDecodeCache::ImageData> CreateImageData( |
286 const DrawImage& image); | 286 const DrawImage& image); |
287 SkImageInfo CreateImageInfoForDrawImage(const DrawImage& draw_image, | 287 SkImageInfo CreateImageInfoForDrawImage(const DrawImage& draw_image, |
288 int upload_scale_mip_level) const; | 288 int upload_scale_mip_level) const; |
289 | 289 |
290 // Finds the ImageData that should be used for the given DrawImage. Looks | 290 // Finds the ImageData that should be used for the given DrawImage. Looks |
291 // first in the |in_use_cache_|, and then in the |persistent_cache_|. | 291 // first in the |in_use_cache_|, and then in the |persistent_cache_|. |
292 ImageData* GetImageDataForDrawImage(const DrawImage& image); | 292 ImageData* GetImageDataForDrawImage(const DrawImage& image); |
293 | 293 |
294 // Returns true if the given ImageData can be used to draw the specified | 294 // Returns true if the given ImageData can be used to draw the specified |
295 // DrawImage. | 295 // DrawImage. |
(...skipping 28 matching lines...) Expand all Loading... |
324 base::MemoryState memory_state_ = base::MemoryState::NORMAL; | 324 base::MemoryState memory_state_ = base::MemoryState::NORMAL; |
325 | 325 |
326 // We can't release GPU backed SkImages without holding the context lock, | 326 // We can't release GPU backed SkImages without holding the context lock, |
327 // so we add them to this list and defer deletion until the next time the lock | 327 // so we add them to this list and defer deletion until the next time the lock |
328 // is held. | 328 // is held. |
329 std::vector<sk_sp<SkImage>> images_pending_deletion_; | 329 std::vector<sk_sp<SkImage>> images_pending_deletion_; |
330 }; | 330 }; |
331 | 331 |
332 } // namespace cc | 332 } // namespace cc |
333 | 333 |
334 #endif // CC_TILES_GPU_IMAGE_DECODE_CONTROLLER_H_ | 334 #endif // CC_TILES_GPU_IMAGE_DECODE_CACHE_H_ |
OLD | NEW |