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.
5 #ifndef PDF_PAINT_MANAGER_H_
6 #define PDF_PAINT_MANAGER_H_
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"
26 namespace chrome_pdf {
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
34 // The client's OnPaint
39 // Invalidates the entire plugin container, scheduling a repaint.
40 virtual void InvalidatePluginContainer() = 0;
42 // Paints the given invalid area of the plugin to the given graphics
43 // device. Returns true if anything was painted.
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.
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.
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;
64 // Updates the client with the latest snapshot created by `Flush()`.
65 virtual void UpdateSnapshot(sk_sp<SkImage> snapshot) = 0;
67 // Updates the client with the latest output scale.
68 virtual void UpdateScale(float scale) = 0;
70 // Updates the client with the latest output layer transform.
71 virtual void UpdateLayerTransform(float scale,
72 const gfx::Vector2dF& translate) = 0;
75 // You shouldn't delete through this interface.
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).
82 // You will need to call SetSize() before this class will do anything.
83 // Normally you do this from UpdateGeometryOnViewChanged() of your plugin
85 explicit PaintManager(Client* client);
86 PaintManager(const PaintManager&) = delete;
87 PaintManager& operator=(const PaintManager&) = delete;
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);
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.
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);
108 // Invalidate the entire plugin.
111 // Invalidate the given rect.
112 void InvalidateRect(const gfx::Rect& rect);
114 // The given rect should be scrolled by the given amounts.
115 void ScrollRect(const gfx::Rect& clip_rect, const gfx::Vector2d& amount);
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;
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();
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();
143 // Does the client paint and executes a Flush if necessary.
149 // Callback for asynchronous completion of Flush.
150 void OnFlushComplete();
152 // Callback for manual scheduling of paints when there is no flush callback
154 void OnManualCallbackComplete();
156 // Non-owning pointer. See the constructor.
157 const raw_ptr<Client> client_;
159 // Backing Skia surface.
160 sk_sp<SkSurface> surface_;
162 PaintAggregator aggregator_;
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;
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;
178 // True iff we're in the middle of a paint.
179 bool in_paint_ = false;
181 // True if we haven't painted the plugin viewport yet.
182 bool first_paint_ = true;
184 // True when the view size just changed and we're waiting for a paint.
185 bool view_size_changed_waiting_for_paint_ = false;
187 base::WeakPtrFactory<PaintManager> weak_factory_{this};
190 } // namespace chrome_pdf
192 #endif // PDF_PAINT_MANAGER_H_