Fix: warnings in media/capture/filters
[platform/framework/web/chromium-efl.git] / pdf / paint_manager.h
1 // Copyright 2010 The Chromium Authors
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 PDF_PAINT_MANAGER_H_
6 #define PDF_PAINT_MANAGER_H_
7
8 #include <vector>
9
10 #include "base/memory/raw_ptr.h"
11 #include "base/memory/weak_ptr.h"
12 #include "pdf/paint_aggregator.h"
13 #include "third_party/skia/include/core/SkRefCnt.h"
14 #include "ui/gfx/geometry/size.h"
15
16 class SkImage;
17 class SkSurface;
18
19 namespace gfx {
20 class Point;
21 class Rect;
22 class Vector2d;
23 class Vector2dF;
24 }  // namespace gfx
25
26 namespace chrome_pdf {
27
28 // Custom PaintManager for the PDF plugin.  This is branched from the Pepper
29 // version.  The difference is that this supports progressive rendering of dirty
30 // rects, where multiple calls to the rendering engine are needed.  It also
31 // supports having higher-priority rects flushing right away, i.e. the
32 // scrollbars.
33 //
34 // The client's OnPaint
35 class PaintManager {
36  public:
37   class Client {
38    public:
39     // Invalidates the entire plugin container, scheduling a repaint.
40     virtual void InvalidatePluginContainer() = 0;
41
42     // Paints the given invalid area of the plugin to the given graphics
43     // device. Returns true if anything was painted.
44     //
45     // You are given the list of rects to paint in `paint_rects`.  You can
46     // combine painting into less rectangles if it's more efficient.  When a
47     // rect is painted, information about that paint should be inserted into
48     // `ready`.  Otherwise if a paint needs more work, add the rect to
49     // `pending`.  If `pending` is not empty, your OnPaint function will get
50     // called again.  Once OnPaint is called and it returns no pending rects,
51     // all the previously ready rects will be flushed on screen.  The exception
52     // is for ready rects that have `flush_now` set to true.  These will be
53     // flushed right away.
54     //
55     // Do not call Flush() on the graphics device, this will be done
56     // automatically if you return true from this function since the
57     // PaintManager needs to handle the callback.
58     //
59     // Calling Invalidate/Scroll is not allowed while inside an OnPaint
60     virtual void OnPaint(const std::vector<gfx::Rect>& paint_rects,
61                          std::vector<PaintReadyRect>& ready,
62                          std::vector<gfx::Rect>& pending) = 0;
63
64     // Updates the client with the latest snapshot created by `Flush()`.
65     virtual void UpdateSnapshot(sk_sp<SkImage> snapshot) = 0;
66
67     // Updates the client with the latest output scale.
68     virtual void UpdateScale(float scale) = 0;
69
70     // Updates the client with the latest output layer transform.
71     virtual void UpdateLayerTransform(float scale,
72                                       const gfx::Vector2dF& translate) = 0;
73
74    protected:
75     // You shouldn't delete through this interface.
76     ~Client() = default;
77   };
78
79   // The Client is a non-owning pointer and must remain valid (normally the
80   // object implementing the Client interface will own the paint manager).
81   //
82   // You will need to call SetSize() before this class will do anything.
83   // Normally you do this from UpdateGeometryOnViewChanged() of your plugin
84   // instance.
85   explicit PaintManager(Client* client);
86   PaintManager(const PaintManager&) = delete;
87   PaintManager& operator=(const PaintManager&) = delete;
88   ~PaintManager();
89
90   // Returns the size of the graphics context to allocate for a given plugin
91   // size. We may allocated a slightly larger buffer than required so that we
92   // don't have to resize the context when scrollbars appear/dissapear due to
93   // zooming (which can result in flickering).
94   static gfx::Size GetNewContextSize(const gfx::Size& current_context_size,
95                                      const gfx::Size& plugin_size);
96
97   // Sets the size of the plugin. If the size is the same as the previous call,
98   // this will be a NOP. If the size has changed, a new device will be
99   // allocated to the given size and a paint to that device will be scheduled.
100   //
101   // This is intended to be called from ViewChanged with the size of the
102   // plugin. Since it tracks the old size and only allocates when the size
103   // changes, you can always call this function without worrying about whether
104   // the size changed or ViewChanged is called for another reason (like the
105   // position changed).
106   void SetSize(const gfx::Size& new_size, float new_device_scale);
107
108   // Invalidate the entire plugin.
109   void Invalidate();
110
111   // Invalidate the given rect.
112   void InvalidateRect(const gfx::Rect& rect);
113
114   // The given rect should be scrolled by the given amounts.
115   void ScrollRect(const gfx::Rect& clip_rect, const gfx::Vector2d& amount);
116
117   // Returns the size of the graphics context for the next paint operation.
118   // This is the pending size if a resize is pending (the plugin has called
119   // SetSize but we haven't actually painted it yet), or the current size of
120   // no resize is pending.
121   gfx::Size GetEffectiveSize() const;
122   float GetEffectiveDeviceScale() const;
123
124   // Set the transform for the graphics layer.
125   // If `schedule_flush` is true, it ensures a flush will be scheduled for
126   // this change. If `schedule_flush` is false, then the change will not take
127   // effect until another change causes a flush.
128   void SetTransform(float scale,
129                     const gfx::Point& origin,
130                     const gfx::Vector2d& translate,
131                     bool schedule_flush);
132   // Resets any transform for the graphics layer.
133   // This does not schedule a flush.
134   void ClearTransform();
135
136  private:
137   // Makes sure there is a callback that will trigger a paint at a later time.
138   // This will be either a Flush callback telling us we're allowed to generate
139   // more data, or, if there's no flush callback pending, a manual call back
140   // to the message loop via ExecuteOnMainThread.
141   void EnsureCallbackPending();
142
143   // Does the client paint and executes a Flush if necessary.
144   void DoPaint();
145
146   // Executes a Flush.
147   void Flush();
148
149   // Callback for asynchronous completion of Flush.
150   void OnFlushComplete();
151
152   // Callback for manual scheduling of paints when there is no flush callback
153   // pending.
154   void OnManualCallbackComplete();
155
156   // Non-owning pointer. See the constructor.
157   const raw_ptr<Client> client_;
158
159   // Backing Skia surface.
160   sk_sp<SkSurface> surface_;
161
162   PaintAggregator aggregator_;
163
164   // See comment for EnsureCallbackPending for more on how these work.
165   bool manual_callback_pending_ = false;
166   bool flush_pending_ = false;
167   bool flush_requested_ = false;
168
169   // When we get a resize, we don't do so right away (see `SetSize()`). The
170   // `has_pending_resize_` tells us that we need to do a resize for the next
171   // paint operation. When true, the new size is in `pending_size_`.
172   bool has_pending_resize_ = false;
173   gfx::Size pending_size_;
174   gfx::Size plugin_size_;
175   float pending_device_scale_ = 1.0f;
176   float device_scale_ = 1.0f;
177
178   // True iff we're in the middle of a paint.
179   bool in_paint_ = false;
180
181   // True if we haven't painted the plugin viewport yet.
182   bool first_paint_ = true;
183
184   // True when the view size just changed and we're waiting for a paint.
185   bool view_size_changed_waiting_for_paint_ = false;
186
187   base::WeakPtrFactory<PaintManager> weak_factory_{this};
188 };
189
190 }  // namespace chrome_pdf
191
192 #endif  // PDF_PAINT_MANAGER_H_