File: parkable_image_manager.h

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (145 lines) | stat: -rw-r--r-- 5,582 bytes parent folder | download | duplicates (9)
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef THIRD_PARTY_BLINK_RENDERER_PLATFORM_GRAPHICS_PARKABLE_IMAGE_MANAGER_H_
#define THIRD_PARTY_BLINK_RENDERER_PLATFORM_GRAPHICS_PARKABLE_IMAGE_MANAGER_H_

#include "base/feature_list.h"
#include "base/no_destructor.h"
#include "base/synchronization/lock.h"
#include "base/task/single_thread_task_runner.h"
#include "base/trace_event/memory_dump_provider.h"
#include "third_party/blink/public/common/features.h"
#include "third_party/blink/renderer/platform/disk_data_allocator.h"
#include "third_party/blink/renderer/platform/graphics/parkable_image.h"
#include "third_party/blink/renderer/platform/platform_export.h"
#include "third_party/blink/renderer/platform/wtf/hash_set.h"

namespace blink {

class ParkableImageImpl;
class ParkableImage;

// Manages parkable images, which are used in blink::BitmapImage. Currently,
// only records metrics for this. In the future we will park eligible images
// to disk.
// Main Thread only.
class PLATFORM_EXPORT ParkableImageManager
    : public base::trace_event::MemoryDumpProvider {
 public:
  static ParkableImageManager& Instance();
  ~ParkableImageManager() override = default;

  bool OnMemoryDump(const base::trace_event::MemoryDumpArgs&,
                    base::trace_event::ProcessMemoryDump*) override;

  // Number of parked and unparked images.
  size_t Size() const LOCKS_EXCLUDED(lock_);

  static bool IsParkableImagesToDiskEnabled() {
    return base::FeatureList::IsEnabled(features::kParkableImagesToDisk);
  }

  void MaybeParkImagesForTesting() { MaybeParkImages(); }

 private:
  struct Statistics;

  friend class ParkableImage;
  friend class ParkableImageImpl;
  friend class base::NoDestructor<ParkableImageManager>;
  friend class ParkableImageBaseTest;

  ParkableImageManager();

  DiskDataAllocator& data_allocator() const;

  // Register and unregister a ParkableImage with the manager. ParkableImage
  // should call these when created/destructed.
  void Add(ParkableImageImpl* image) LOCKS_EXCLUDED(lock_);
  void Remove(ParkableImageImpl* image) LOCKS_EXCLUDED(lock_)
      EXCLUSIVE_LOCKS_REQUIRED(image->lock_);

  scoped_refptr<ParkableImageImpl> CreateParkableImage(size_t offset);
  void DestroyParkableImageOnMainThread(scoped_refptr<ParkableImageImpl> image)
      LOCKS_EXCLUDED(lock_);
  void DestroyParkableImage(scoped_refptr<ParkableImageImpl> image)
      LOCKS_EXCLUDED(lock_);

  bool IsRegistered(ParkableImageImpl* image) LOCKS_EXCLUDED(lock_)
      EXCLUSIVE_LOCKS_REQUIRED(image->lock_);
  // bool IsRegistered(ParkableImage* image) LOCKS_EXCLUDED(lock_)
  //     EXCLUSIVE_LOCKS_REQUIRED(image->impl_->lock_);

  void ScheduleDelayedParkingTaskIfNeeded() EXCLUSIVE_LOCKS_REQUIRED(lock_);
  void MaybeParkImages() LOCKS_EXCLUDED(lock_);

  Statistics ComputeStatistics() const EXCLUSIVE_LOCKS_REQUIRED(lock_);

  void RecordStatisticsAfter5Minutes() const LOCKS_EXCLUDED(lock_);

  void MoveImage(ParkableImageImpl* image,
                 WTF::HashSet<ParkableImageImpl*>* from,
                 WTF::HashSet<ParkableImageImpl*>* to)
      EXCLUSIVE_LOCKS_REQUIRED(lock_);

  void RecordDiskWriteTime(base::TimeDelta write_time) LOCKS_EXCLUDED(lock_) {
    base::AutoLock lock(lock_);
    total_disk_write_time_ += write_time;
  }

  void RecordDiskReadTime(base::TimeDelta read_time) LOCKS_EXCLUDED(lock_) {
    base::AutoLock lock(lock_);
    total_disk_read_time_ += read_time;
  }

  // Keeps track of whether the image is unparked or on disk. ParkableImage
  // should call these when written to or read from disk.
  void OnWrittenToDisk(ParkableImageImpl* image) LOCKS_EXCLUDED(lock_);
  void OnReadFromDisk(ParkableImageImpl* image) LOCKS_EXCLUDED(lock_);

  void SetDataAllocatorForTesting(
      std::unique_ptr<DiskDataAllocator> allocator) {
    allocator_for_testing_ = std::move(allocator);
  }

  void SetTaskRunnerForTesting(
      scoped_refptr<base::SingleThreadTaskRunner> task_runner);

  void ResetForTesting();
  constexpr static auto kDelayedParkingInterval = base::Seconds(2);
  constexpr static const char* kAllocatorDumpName = "parkable_images";

  mutable base::Lock lock_;

  // The following two sets are used to keep track of all ParkableImages that
  // have been created. ParkableImages are added to |unparked_images_| upon
  // creation, and removed from whichever set they are in at the time of their
  // destruction.
  //
  // Parking or Unparking a ParkableImage moves the image to the appropriate
  // set, using |OnReadFromDisk| and |OnWrittenToDisk|.
  //
  // |unparked_images_| keeps track of all images that have a in-memory
  // representation.
  //
  // |on_disk_images_| keeps track of all images that do not have an in-memory
  // representation. Accessing the data for any image in |on_disk_images_|
  // involves a read from disk.
  WTF::HashSet<ParkableImageImpl*> unparked_images_ GUARDED_BY(lock_);
  WTF::HashSet<ParkableImageImpl*> on_disk_images_ GUARDED_BY(lock_);

  bool has_pending_parking_task_ GUARDED_BY(lock_) = false;
  bool has_posted_accounting_task_ GUARDED_BY(lock_) = false;

  base::TimeDelta total_disk_read_time_ GUARDED_BY(lock_) = base::TimeDelta();
  base::TimeDelta total_disk_write_time_ GUARDED_BY(lock_) = base::TimeDelta();

  scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
  std::unique_ptr<DiskDataAllocator> allocator_for_testing_;
};

}  // namespace blink

#endif  // THIRD_PARTY_BLINK_RENDERER_PLATFORM_GRAPHICS_PARKABLE_IMAGE_MANAGER_H_