1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91

cc / resources / ui_resource_manager.h [blame]

// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CC_RESOURCES_UI_RESOURCE_MANAGER_H_
#define CC_RESOURCES_UI_RESOURCE_MANAGER_H_

#include <memory>
#include <unordered_map>
#include <vector>

#include "base/containers/flat_map.h"
#include "base/memory/raw_ptr.h"
#include "cc/cc_export.h"
#include "cc/resources/ui_resource_request.h"

namespace cc {
class ScopedUIResource;

// UIResourceManager creates and manages UIResourceBitmaps and UIResourceIDs
// for given bitmaps for a LayerTreeHost. There are two ways to use the
// interface:
//  1. `CreateUIResource`/`DeleteUIResource` to explicitly manage the lifetime
//     of UIResources.
//  2. `GetOrCreateUIResource` to create shared UIResources backed by the same
//     SkBitmap (SkPixelRef to be exact). The created UIResources are owned by
//     the manager and are released when all references outside the manager's
//     map are dropped.
class CC_EXPORT UIResourceManager {
 public:
  UIResourceManager();
  UIResourceManager(const UIResourceManager&) = delete;
  virtual ~UIResourceManager();

  UIResourceManager& operator=(const UIResourceManager&) = delete;

  // CreateUIResource creates a resource given a bitmap.  The bitmap is
  // generated via an interface function, which is called when initializing the
  // resource and when the resource has been lost (due to lost context).  The
  // parameter of the interface is a single boolean, which indicates whether the
  // resource has been lost or not.  CreateUIResource returns an Id of the
  // resource, which is always positive.
  virtual UIResourceId CreateUIResource(UIResourceClient* client);

  // Deletes a UI resource.  May safely be called more than once.
  virtual void DeleteUIResource(UIResourceId id);

  base::flat_map<UIResourceId, gfx::Size> GetUIResourceSizes() const;

  // Methods meant to be used only internally in cc ------------

  // The current UIResourceRequestQueue is moved to the caller.
  std::vector<UIResourceRequest> TakeUIResourcesRequests();

  // Put the recreation of all UI resources into the resource queue after they
  // were evicted on the impl thread.
  void RecreateUIResources();

  // Creates a resource given a SkBitmap. Multiple calls with bitmaps that
  // share the same SkPixelRef will share a single resource ID. The returned
  // `UIResourceId` will only be valid as long as something else holds a
  // reference to the SkBitmap
  UIResourceId GetOrCreateUIResource(const SkBitmap& bitmap);

  size_t owned_shared_resources_size_for_test() const {
    return owned_shared_resources_.size();
  }

 private:
  struct UIResourceClientData {
    raw_ptr<UIResourceClient> client;
    gfx::Size size;
  };

  std::unordered_map<UIResourceId, UIResourceClientData>
      ui_resource_client_map_;
  int next_ui_resource_id_;

  using UIResourceRequestQueue = std::vector<UIResourceRequest>;
  UIResourceRequestQueue ui_resource_request_queue_;

  // A map from bitmaps to the ScopedUIResource we've created for them. The
  // resources are released when all references of SkPixelRefs outside the map
  // are dropped.
  std::unordered_map<SkPixelRef*, std::unique_ptr<ScopedUIResource>>
      owned_shared_resources_;
};

}  // namespace cc

#endif  // CC_RESOURCES_UI_RESOURCE_MANAGER_H_