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

Unified Diff: cc/tiles/image_decode_controller.h

Issue 1418573002: cc: Add image decode control in the compositor. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: update Created 5 years 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 side-by-side diff with in-line comments
Download patch
Index: cc/tiles/image_decode_controller.h
diff --git a/cc/tiles/image_decode_controller.h b/cc/tiles/image_decode_controller.h
index f07892c13f5cc683b2a6df4acb9365c355ccb296..7902b7d7eeb5e4f6bab123dafacbe6f97d8ef3d4 100644
--- a/cc/tiles/image_decode_controller.h
+++ b/cc/tiles/image_decode_controller.h
@@ -6,46 +6,219 @@
#define CC_TILES_IMAGE_DECODE_CONTROLLER_H_
#include "base/containers/hash_tables.h"
+#include "base/memory/discardable_memory_allocator.h"
#include "base/memory/ref_counted.h"
+#include "base/numerics/safe_math.h"
+#include "base/threading/thread_checker.h"
#include "cc/base/cc_export.h"
-#include "cc/playback/discardable_image_map.h"
+#include "cc/playback/decoded_draw_image.h"
+#include "cc/playback/draw_image.h"
#include "cc/raster/tile_task_runner.h"
#include "skia/ext/refptr.h"
+#include "ui/gfx/transform.h"
+
namespace cc {
-class ImageDecodeController {
+// ImageDecodeControllerKey is a class that gets a cache key out of a given draw
+// image. That is, this key uniquely identifies an image in the cache. Note that
+// it's insufficient to use SkImage's unique id, since the same image can appear
+// in the cache multiple times at different scales and filter qualities.
+class CC_EXPORT ImageDecodeControllerKey {
public:
+ static ImageDecodeControllerKey FromDrawImage(const DrawImage& image);
+
+ bool operator==(const ImageDecodeControllerKey& other) const {
+ return image_id_ == other.image_id_ && size_ == other.size_ &&
+ filter_quality_ == other.filter_quality_;
+ }
+
+ bool operator!=(const ImageDecodeControllerKey& other) const {
+ return !(*this == other);
+ }
+
+ uint32_t image_id() const { return image_id_; }
+ SkFilterQuality filter_quality() const { return filter_quality_; }
+ gfx::Size target_size() const { return size_; }
+
+ // Helper to figure out how much memory this decoded image would take.
+ size_t target_bytes() const {
+ // TODO(vmpstr): Handle formats other than RGBA.
+ base::CheckedNumeric<size_t> result = 4;
+ result *= size_.width();
+ result *= size_.height();
+ return result.ValueOrDefault(std::numeric_limits<size_t>::max());
+ }
+
+ std::string ToString() const;
+
+ private:
+ ImageDecodeControllerKey(uint32_t image_id,
+ const gfx::Size& size,
+ SkFilterQuality filter_quality);
+
+ uint32_t image_id_;
+ gfx::Size size_;
+ SkFilterQuality filter_quality_;
+};
+
+} // namespace cc
+
+// Hash function for the above ImageDecodeControllerKey.
+namespace BASE_HASH_NAMESPACE {
+template <>
+struct hash<cc::ImageDecodeControllerKey> {
+ size_t operator()(const cc::ImageDecodeControllerKey& key) const {
+ return base::HashPair(
+ static_cast<uint64>(base::HashPair(key.target_size().width(),
ericrk 2015/12/04 00:50:46 nit: prefer uint64_t
+ key.target_size().height())),
+ static_cast<uint64>(
+ base::HashPair(key.image_id(), key.filter_quality())));
+ }
+};
+} // namespace BASE_HASH_NAMESPACE
+
+namespace cc {
+
+// ImageDecodeController is responsible for generating decode tasks, decoding
+// images, storing images in cache, and being able to return the decoded images
+// when requested.
+
+// ImageDecodeController is responsible for the following things:
+// 1. Given a DrawImage, it can return an ImageDecodeTask which when run will
+// decode and cache the resulting image. If the image does not need a task to
+// be decoded, then nullptr will be returned. The return value of the
+// function indicates whether the image was or is going to be locked, so an
+// unlock will be required.
+// 2. Given a cache key and a DrawImage, it can decode the image and store it in
+// the cache. Note that it is important that this function is only accessed
+// via an image decode task.
+// 3. Given a DrawImage, it can return a DecodedDrawImage, which represented the
+// decoded version of the image. Note that if the image is not in the cache
+// and it needs to be scaled/decoded, then this decode will happen as part of
+// getting the image. As such, this should only be accessed from a raster
+// thread.
+class CC_EXPORT ImageDecodeController {
+ public:
+ using ImageKey = ImageDecodeControllerKey;
+
ImageDecodeController();
~ImageDecodeController();
- scoped_refptr<ImageDecodeTask> GetTaskForImage(const DrawImage& image,
- int layer_id,
- uint64_t prepare_tiles_id);
+ // Fill in an ImageDecodeTask which will decode the given image when run. In
+ // case the image is already cached, fills in nullptr. Returns true if the
+ // image needs to be unreffed when the caller is finished with it.
+ //
+ // This is called by the tile manager (on the compositor thread) when creating
+ // a raster task.
+ bool GetTaskForImageAndRef(const DrawImage& image,
+ uint64_t prepare_tiles_id,
+ scoped_refptr<ImageDecodeTask>* task);
+ // Unrefs an image. When the tile is finished, this should be called for every
+ // GetTaskForImageAndRef call that returned true.
+ void UnrefImage(const DrawImage& image);
+
+ // Returns a decoded draw image. If the image isn't found in the cache, a
+ // decode will happen.
+ //
+ // This is called by a raster task (on a worker thread) when an image is
+ // required.
+ DecodedDrawImage GetDecodedImageForDraw(const DrawImage& image);
+ // Unrefs an image. This should be called for every GetDecodedImageForDraw
+ // when the draw with the image is finished.
+ void DrawWithImageFinished(const DrawImage& image,
+ const DecodedDrawImage& decoded_image);
- // Note that this function has to remain thread safe.
- void DecodeImage(const SkImage* image);
+ // Decode the given image and store it in the cache. This is only called by an
+ // image decode task from a worker thread.
+ void DecodeImage(const ImageKey& key, const DrawImage& image);
- // TODO(vmpstr): This should go away once the controller is decoding images
- // based on priority and memory.
- void AddLayerUsedCount(int layer_id);
- void SubtractLayerUsedCount(int layer_id);
+ void ReduceCacheUsage();
- void OnImageDecodeTaskCompleted(int layer_id,
- const SkImage* image,
- bool was_canceled);
+ void RemovePendingTask(const ImageKey& key);
+
+ // Info the controller whether we're using gpu rasterization or not. Since the
+ // decode and caching behavior is different for SW and GPU decodes, when the
+ // state changes, we clear all of the caches. This means that this is only
+ // safe to call when there are no pending tasks (and no refs on any images).
+ void SetIsUsingGpuRasterization(bool is_using_gpu_rasterization);
private:
- scoped_refptr<ImageDecodeTask> CreateTaskForImage(const SkImage* image,
- int layer_id,
- uint64_t prepare_tiles_id);
+ // DecodedImage is a convenience storage for discardable memory. It can also
+ // construct an image out of SkImageInfo and stored discardable memory.
+ class DecodedImage : public base::RefCounted<DecodedImage> {
+ public:
+ DecodedImage(const SkImageInfo& info,
+ scoped_ptr<base::DiscardableMemory> memory);
+
+ SkImage* image() const {
+ DCHECK(locked_);
+ return image_.get();
+ }
+
+ bool is_locked() const { return locked_; }
+ bool Lock();
+ void Unlock();
+
+ private:
+ friend class base::RefCounted<DecodedImage>;
+
+ ~DecodedImage();
+
+ bool locked_;
+ SkImageInfo image_info_;
+ scoped_ptr<base::DiscardableMemory> memory_;
+ skia::RefPtr<SkImage> image_;
+ };
+
+ // MemoryBudget is a convenience class for memory bookkeeping and ensuring
+ // that we don't go over the limit when pre-decoding.
+ class MemoryBudget {
ericrk 2015/12/04 00:50:46 Not as part of this CL :D, but can you add a TODO
+ public:
+ explicit MemoryBudget(size_t limit_bytes);
+
+ size_t AvailableMemoryBytes() const;
+ void AddUsage(size_t usage);
+ void SubtractUsage(size_t usage);
+ void ResetUsage();
+
+ private:
+ size_t GetCurrentUsageSafe() const;
+
+ size_t limit_bytes_;
+ base::CheckedNumeric<size_t> current_usage_bytes_;
+ };
+
+ using AnnotatedDecodedImage =
+ std::pair<ImageKey, scoped_refptr<DecodedImage>>;
+
+ // Looks for the key in the cache and returns true if it was found and was
+ // successfully locked (or if it was already locked). Note that if this
+ // function returns true, then a ref count is increased for the image.
+ bool LockDecodedImageIfPossibleAndRef(const ImageKey& key);
+
+ scoped_refptr<DecodedImage> DecodeImageInternal(const ImageKey& key,
+ const SkImage* image);
+ bool CanHandleFilterQuality(SkFilterQuality filter_quality);
+ size_t SanityCheckState(int line, bool lock_acquired);
+ void RefImage(const ImageKey& key);
+ void RefAtRasterImage(const ImageKey& key);
+ void UnrefAtRasterImage(const ImageKey& key);
+ bool ShouldDecodeAndScaleImage(const ImageKey& key, const DrawImage& image);
+
+ bool is_using_gpu_rasterization_;
+ base::hash_map<ImageKey, scoped_refptr<ImageDecodeTask>> pending_image_tasks_;
- using ImageTaskMap = base::hash_map<uint32_t, scoped_refptr<ImageDecodeTask>>;
- using LayerImageTaskMap = base::hash_map<int, ImageTaskMap>;
- LayerImageTaskMap image_decode_tasks_;
+ // The members can only be accessed if the lock is held to ensure that they
+ // are safe to access on multiple threads.
+ base::Lock lock_;
+ std::deque<AnnotatedDecodedImage> decoded_images_;
+ base::hash_map<ImageKey, int> decoded_images_ref_counts_;
+ std::deque<AnnotatedDecodedImage> at_raster_decoded_images_;
+ base::hash_map<ImageKey, int> at_raster_decoded_images_ref_counts_;
+ MemoryBudget locked_images_budget_;
- using LayerCountMap = base::hash_map<int, int>;
- LayerCountMap used_layer_counts_;
+ base::hash_set<uint32_t> prerolled_images_;
ericrk 2015/12/04 00:50:46 does this need to have the lock held? unclear whet
};
} // namespace cc

Powered by Google App Engine
This is Rietveld 408576698