| 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..dbcee751d3b4d5cf902fed21ce15b789b85704ef 100644
 | 
| --- a/cc/tiles/image_decode_controller.h
 | 
| +++ b/cc/tiles/image_decode_controller.h
 | 
| @@ -6,46 +6,247 @@
 | 
|  #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_ && src_rect_ == other.src_rect_ &&
 | 
| +           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::Rect src_rect() const { return src_rect_; }
 | 
| +  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::Rect& src_rect,
 | 
| +                           const gfx::Size& size,
 | 
| +                           SkFilterQuality filter_quality);
 | 
| +
 | 
| +  uint32_t image_id_;
 | 
| +  gfx::Rect src_rect_;
 | 
| +  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 {
 | 
| +    // TODO(vmpstr): This is a mess. Maybe it's faster to just search the vector
 | 
| +    // always (forwards or backwards to account for LRU).
 | 
| +    uint64_t src_rect_hash =
 | 
| +        base::HashPair(static_cast<uint64_t>(base::HashPair(
 | 
| +                           key.src_rect().x(), key.src_rect().y())),
 | 
| +                       static_cast<uint64_t>(base::HashPair(
 | 
| +                           key.src_rect().width(), key.src_rect().height())));
 | 
| +
 | 
| +    uint64_t target_size_hash =
 | 
| +        base::HashPair(key.target_size().width(), key.target_size().height());
 | 
| +
 | 
| +    return base::HashPair(base::HashPair(src_rect_hash, target_size_hash),
 | 
| +                          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);
 | 
| +
 | 
| +  // 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);
 | 
|  
 | 
| -  // Note that this function has to remain thread safe.
 | 
| -  void DecodeImage(const SkImage* image);
 | 
| +  void ReduceCacheUsage();
 | 
|  
 | 
| -  // 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 RemovePendingTask(const ImageKey& key);
 | 
|  
 | 
| -  void OnImageDecodeTaskCompleted(int layer_id,
 | 
| -                                  const SkImage* image,
 | 
| -                                  bool was_canceled);
 | 
| +  // 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.
 | 
| +  // TODO(vmpstr): Make this scoped_ptr.
 | 
| +  class DecodedImage : public base::RefCounted<DecodedImage> {
 | 
| +   public:
 | 
| +    DecodedImage(const SkImageInfo& info,
 | 
| +                 scoped_ptr<base::DiscardableMemory> memory,
 | 
| +                 const SkSize& src_rect_offset);
 | 
| +
 | 
| +    SkImage* image() const {
 | 
| +      DCHECK(locked_);
 | 
| +      return image_.get();
 | 
| +    }
 | 
| +
 | 
| +    const SkSize& src_rect_offset() const { return src_rect_offset_; }
 | 
| +
 | 
| +    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_;
 | 
| +    SkSize src_rect_offset_;
 | 
| +  };
 | 
| +
 | 
| +  // MemoryBudget is a convenience class for memory bookkeeping and ensuring
 | 
| +  // that we don't go over the limit when pre-decoding.
 | 
| +  // TODO(vmpstr): Add memory infra to keep track of memory usage of this class.
 | 
| +  class MemoryBudget {
 | 
| +   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);
 | 
| +  void SanityCheckState(int line, bool lock_acquired);
 | 
| +  void RefImage(const ImageKey& key);
 | 
| +  void RefAtRasterImage(const ImageKey& key);
 | 
| +  void UnrefAtRasterImage(const ImageKey& key);
 | 
| +
 | 
| +  // These functions indicate whether the images can be handled and cached by
 | 
| +  // ImageDecodeController or whether they will fall through to Skia (with
 | 
| +  // exception of possibly prerolling them). Over time these should return
 | 
| +  // "false" in less cases, as the ImageDecodeController should start handling
 | 
| +  // more of them.
 | 
| +  bool CanHandleImage(const ImageKey& key, const DrawImage& image);
 | 
| +  bool CanHandleFilterQuality(SkFilterQuality filter_quality);
 | 
| +
 | 
| +  bool is_using_gpu_rasterization_;
 | 
| +
 | 
| +  base::hash_map<ImageKey, scoped_refptr<ImageDecodeTask>> pending_image_tasks_;
 | 
| +
 | 
| +  // The members below this comment can only be accessed if the lock is held to
 | 
| +  // ensure that they are safe to access on multiple threads.
 | 
| +  base::Lock lock_;
 | 
|  
 | 
| -  using ImageTaskMap = base::hash_map<uint32_t, scoped_refptr<ImageDecodeTask>>;
 | 
| -  using LayerImageTaskMap = base::hash_map<int, ImageTaskMap>;
 | 
| -  LayerImageTaskMap image_decode_tasks_;
 | 
| +  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_;
 | 
| +  // Note that this is used for cases where the only thing we do is preroll the
 | 
| +  // image the first time we see it. This mimics the previous behavior and
 | 
| +  // should over time change as the compositor starts to handle more cases.
 | 
| +  base::hash_set<uint32_t> prerolled_images_;
 | 
|  };
 | 
|  
 | 
|  }  // namespace cc
 | 
| 
 |