1 // Copyright 2019 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef THIRD_PARTY_BLINK_RENDERER_PLATFORM_GRAPHICS_MEMORY_MANAGED_PAINT_CANVAS_H_
6 #define THIRD_PARTY_BLINK_RENDERER_PLATFORM_GRAPHICS_MEMORY_MANAGED_PAINT_CANVAS_H_
7 
8 #include <memory>
9 
10 #include "cc/paint/record_paint_canvas.h"
11 #include "third_party/blink/public/platform/platform.h"
12 
13 namespace blink {
14 
15 // MemoryManagedPaintCanvas overrides the potentially memory intensive image
16 // drawing methods of PaintCanvas and keeps track of how much memory is
17 // being pinned between flushes. This allows the rendering context to flush if
18 // too much memory is used.
19 class PLATFORM_EXPORT MemoryManagedPaintCanvas final
20     : public cc::RecordPaintCanvas {
21  public:
22   MemoryManagedPaintCanvas(cc::DisplayItemList* list,
23                            const SkRect& bounds,
24                            base::RepeatingClosure set_needs_flush_callback);
25   explicit MemoryManagedPaintCanvas(const cc::RecordPaintCanvas&) = delete;
26   ~MemoryManagedPaintCanvas() override;
27 
28   void drawImage(const cc::PaintImage& image,
29                  SkScalar left,
30                  SkScalar top,
31                  const cc::PaintFlags* flags) override;
32   void drawImageRect(const cc::PaintImage& image,
33                      const SkRect& src,
34                      const SkRect& dst,
35                      const cc::PaintFlags* flags,
36                      SrcRectConstraint constraint) override;
37 
38  private:
39   void UpdateMemoryUsage(const cc::PaintImage& image);
40 
41   base::flat_set<int> cached_image_ids_;
42   uint64_t total_stored_image_memory_ = 0;
43 
44   base::RepeatingClosure set_needs_flush_callback_;
45 
46   // The same value as is used in content::WebGraphicsConext3DProviderImpl.
47   static constexpr uint64_t kMaxPinnedMemory = 64 * 1024 * 1024;
48 };
49 
50 }  // namespace blink
51 
52 #endif  // THIRD_PARTY_BLINK_RENDERER_PLATFORM_GRAPHICS_MEMORY_MANAGED_PAINT_CANVAS_H_
53