File: ui_resource_manager.h

package info (click to toggle)
chromium 120.0.6099.224-1~deb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 6,112,112 kB
  • sloc: cpp: 32,907,025; ansic: 8,148,123; javascript: 3,679,536; python: 2,031,248; asm: 959,718; java: 804,675; xml: 617,256; sh: 111,417; objc: 100,835; perl: 88,443; cs: 53,032; makefile: 29,579; fortran: 24,137; php: 21,162; tcl: 21,147; sql: 20,809; ruby: 17,735; pascal: 12,864; yacc: 8,045; lisp: 3,388; lex: 1,323; ada: 727; awk: 329; jsp: 267; csh: 117; exp: 43; sed: 37
file content (95 lines) | stat: -rw-r--r-- 3,529 bytes parent folder | download | duplicates (6)
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
92
93
94
95
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef ASH_FRAME_SINK_UI_RESOURCE_MANAGER_H_
#define ASH_FRAME_SINK_UI_RESOURCE_MANAGER_H_

#include <memory>
#include <vector>

#include "ash/ash_export.h"
#include "ash/frame_sink/ui_resource.h"
#include "base/containers/flat_map.h"
#include "components/viz/common/resources/resource_id.h"
#include "components/viz/common/resources/transferable_resource.h"
#include "ui/gfx/geometry/size.h"

namespace ash {

// Responsible to keep track of resources that are currently in-use by display
// compositor and resources that are currently available to be reused.
// Once we offer a resource to the manager, UiResourceManager manages the
// lifetime of the resources and keep track if the resource is currently
// exported or is available to use. It is required that all the exported
// resources are reclaimed before we can delete the UiResourceManager.
class ASH_EXPORT UiResourceManager {
 public:
  UiResourceManager();

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

  ~UiResourceManager();

  // Returns the resource_id of an available resource of given `size`,
  // `format` and `ui_source_id`. If there is no matching resource available, we
  // return `viz::kInvalidResourceId`.
  viz::ResourceId FindResourceToReuse(const gfx::Size& size,
                                      viz::SharedImageFormat format,
                                      UiSourceId ui_source_id) const;

  const UiResource* PeekAvailableResource(viz::ResourceId resource_id) const;

  const UiResource* PeekExportedResource(viz::ResourceId resource_id) const;

  std::unique_ptr<UiResource> ReleaseAvailableResource(
      viz::ResourceId resource_id);

  void ReclaimResources(const std::vector<viz::ReturnedResource>& resources);

  // Give the `resourse` to be managed by the manager. The resource is
  // immediately available for use and can be exported.
  viz::ResourceId OfferResource(std::unique_ptr<UiResource> resource);

  // We take the available resource identified by `resource_id`, map it to a
  // transferable resource and mark it as an exported resource. Only a resource
  // that is currently being managed can be exported.
  viz::TransferableResource PrepareResourceForExport(
      viz::ResourceId resource_id);

  // Mark all the managed resources to be damaged.
  void DamageResources();

  void ClearAvailableResources();

  // Call the method when there is no way to reclaim back the exported
  // resources. i.e when the frame_sink is lost or deleted.
  void LostExportedResources();

  size_t exported_resources_count() const;

  size_t available_resources_count() const;

 private:
  // TODO(zoraiznaeem): If a feature ends up growing the size of pool past 40,
  // use a fixed size circular list as a pool and get rid of least recently used
  // resources.
  using ResourcePool =
      base::flat_map<viz::ResourceId, std::unique_ptr<UiResource>>;

  // The resources that are currently available to be used in a compositor
  // frame.
  ResourcePool available_resources_pool_;

  // The resources that are in-flight or will be exported to the display
  // compositor.
  ResourcePool exported_resources_pool_;

  // Generates transferable resource ids for transferable resources.
  viz::ResourceIdGenerator id_generator_;
};

}  // namespace ash

#endif  // ASH_FRAME_SINK_UI_RESOURCE_MANAGER_H_