OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef PPAPI_CPP_PAINT_MANAGER_H_ | 5 #ifndef PPAPI_CPP_PAINT_MANAGER_H_ |
6 #define PPAPI_CPP_PAINT_MANAGER_H_ | 6 #define PPAPI_CPP_PAINT_MANAGER_H_ |
7 | 7 |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "ppapi/cpp/completion_callback.h" | 10 #include "ppapi/cpp/completion_callback.h" |
11 #include "ppapi/cpp/graphics_2d.h" | 11 #include "ppapi/cpp/graphics_2d.h" |
12 #include "ppapi/cpp/paint_aggregator.h" | 12 #include "ppapi/cpp/paint_aggregator.h" |
13 | 13 |
| 14 /// @file |
| 15 /// This file defines the API to convert the "plugin push" model of painting |
| 16 /// in PPAPI to a paint request at a later time. |
| 17 |
14 namespace pp { | 18 namespace pp { |
15 | 19 |
16 class Graphics2D; | 20 class Graphics2D; |
17 class Instance; | 21 class Instance; |
18 class Point; | 22 class Point; |
19 class Rect; | 23 class Rect; |
20 | 24 |
21 // This class converts the "plugin push" model of painting in PPAPI to a paint | 25 /// This class converts the "instance push" model of painting in PPAPI to a |
22 // request at a later time. Usage is that you call Invalidate and Scroll, and | 26 /// paint request at a later time. Usage is that you call Invalidate and |
23 // implement the Client interface. Your OnPaint handler will then get called | 27 /// Scroll, and implement the Client interface. Your OnPaint handler will |
24 // with coalesced paint events. | 28 /// then get called with coalesced paint events. |
25 // | 29 /// |
26 // This class is basically a PaintAggregator that groups updates, plus | 30 /// This class is basically a <code>PaintAggregator</code> that groups updates, |
27 // management of callbacks for scheduling paints. | 31 /// plus management of callbacks for scheduling paints. |
28 // | 32 /// |
29 // Typical usage: | 33 /// <strong>Example:</strong> |
30 // | 34 /// |
31 // class MyClass : public pp::Instance, public PaintManager::Client { | 35 /// @code |
32 // public: | 36 /// |
33 // MyClass() { | 37 /// class MyClass : public pp::Instance, public PaintManager::Client { |
34 // paint_manager_.Initialize(this, this, false); | 38 /// public: |
35 // } | 39 /// MyClass() { |
36 // | 40 /// paint_manager_.Initialize(this, this, false); |
37 // void ViewChanged(const pp::Rect& position, const pp::Rect& clip) { | 41 /// } |
38 // paint_manager_.SetSize(position.size()); | 42 /// |
39 // } | 43 /// void ViewChanged(const pp::Rect& position, const pp::Rect& clip) { |
40 // | 44 /// paint_manager_.SetSize(position.size()); |
41 // void DoSomething() { | 45 /// } |
42 // // This function does something like respond to an event that causes | 46 /// |
43 // // the screen to need updating. | 47 /// void DoSomething() { |
44 // paint_manager_.InvalidateRect(some_rect); | 48 /// // This function does something like respond to an event that causes |
45 // } | 49 /// // the screen to need updating. |
46 // | 50 /// paint_manager_.InvalidateRect(some_rect); |
47 // // Implementation of PaintManager::Client | 51 /// } |
48 // virtual bool OnPaint(pp::Graphics2D& device, | 52 /// |
49 // const pp::PaintUpdate& update) { | 53 /// // Implementation of PaintManager::Client |
50 // // If our app needed scrolling, we would apply that first here. | 54 /// virtual bool OnPaint(pp::Graphics2D& device, |
51 // | 55 /// const pp::PaintUpdate& update) { |
52 // // Then we would either repaint the area returned by GetPaintBounds or | 56 /// // If our app needed scrolling, we would apply that first here. |
53 // // iterate through all the paint_rects. | 57 /// |
54 // | 58 /// // Then we would either repaint the area returned by GetPaintBounds or |
55 // // The caller will call Flush() for us, so don't do that here. | 59 /// // iterate through all the paint_rects. |
56 // return true; | 60 /// |
57 // } | 61 /// // The caller will call Flush() for us, so don't do that here. |
58 // | 62 /// return true; |
59 // private: | 63 /// } |
60 // pp::PaintManager paint_manager_; | 64 /// |
61 // }; | 65 /// private: |
| 66 /// pp::PaintManager paint_manager_; |
| 67 /// }; |
| 68 /// @endcode |
62 class PaintManager { | 69 class PaintManager { |
63 public: | 70 public: |
64 class Client { | 71 class Client { |
65 public: | 72 public: |
66 // Paints the given invalid area of the plugin to the given graphics | 73 /// OnPaint() paints the given invalid area of the instance to the given |
67 // device. Returns true if anything was painted. | 74 /// graphics device. Returns true if anything was painted. |
68 // | 75 /// |
69 // You are given the list of rects to paint in |paint_rects|, and the | 76 /// You are given the list of rects to paint in <code>paint_rects</code>, |
70 // union of all of these rects in |paint_bounds|. You only have to paint | 77 /// and the union of all of these rects in <code>paint_bounds</code>. You |
71 // the area inside each of the |paint_rects|, but can paint more if you | 78 /// only have to paint the area inside each of the |
72 // want (some apps may just want to paint the union). | 79 /// <code>paint_rects</code>, but can paint more if you want (some apps may |
73 // | 80 /// just want to paint the union). |
74 // Do not call Flush() on the graphics device, this will be done | 81 /// |
75 // automatically if you return true from this function since the | 82 /// Do not call Flush() on the graphics device, this will be done |
76 // PaintManager needs to handle the callback. | 83 /// automatically if you return true from this function since the |
77 // | 84 /// <code>PaintManager</code> needs to handle the callback. |
78 // It is legal for you to cause invalidates inside of Paint which will | 85 /// |
79 // then get executed as soon as the Flush for this update has completed. | 86 /// It is legal for you to cause invalidates inside of Paint which will |
80 // However, this is not very nice to the host system since it will spin the | 87 /// then get executed as soon as the Flush for this update has completed. |
81 // CPU, possibly updating much faster than necessary. It is best to have a | 88 /// However, this is not very nice to the host system since it will spin the |
82 // 1/60 second timer to do an invalidate instead. This will limit your | 89 /// CPU, possibly updating much faster than necessary. It is best to have a |
83 // animation to the slower of 60Hz or "however fast Flush can complete." | 90 /// 1/60 second timer to do an invalidate instead. This will limit your |
| 91 /// animation to the slower of 60Hz or "however fast Flush can complete." |
| 92 /// |
| 93 /// @param graphics A <code>Graphics2D</code> to be painted. |
| 94 /// @param paint_rects A list of rects to paint. |
| 95 /// @param paint_bounds A union of the rects to paint. |
84 virtual bool OnPaint(Graphics2D& graphics, | 96 virtual bool OnPaint(Graphics2D& graphics, |
85 const std::vector<Rect>& paint_rects, | 97 const std::vector<Rect>& paint_rects, |
86 const Rect& paint_bounds) = 0; | 98 const Rect& paint_bounds) = 0; |
87 | 99 |
88 protected: | 100 protected: |
89 // You shouldn't be doing deleting through this interface. | 101 // You shouldn't be doing deleting through this interface. |
90 virtual ~Client() {} | 102 virtual ~Client() {} |
91 }; | 103 }; |
92 | 104 |
93 // If you use this version of the constructor, you must call Initialize() | 105 /// Default constructor for creating an is_null() <code>PaintManager</code> |
94 // below. | 106 /// object. If you use this version of the constructor, you must call |
| 107 /// Initialize() below. |
95 PaintManager(); | 108 PaintManager(); |
96 | 109 |
97 // The instance is the plugin instance using this paint manager to do its | 110 /// A constructor to create a new <code>PaintManager</code> with an instance |
98 // painting. Painting will automatically go to this instance and you don't | 111 /// and client. |
99 // have to manually bind any device context (this is all handled by the | 112 /// |
100 // paint manager). | 113 /// <strong>Note:</strong> You will need to call SetSize() before this class |
101 // | 114 /// will do anything. Normally you do this from the <code>ViewChanged</code> |
102 // The Client is a non-owning pointer and must remain valid (normally the | 115 /// method of your instance. |
103 // object implementing the Client interface will own the paint manager). | 116 /// |
104 // | 117 /// @param instance The instance using this paint manager to do its |
105 // The is_always_opaque flag will be passed to the device contexts that this | 118 /// painting. Painting will automatically go to this instance and you don't |
106 // class creates. Set this to true if your plugin always draws an opaque | 119 /// have to manually bind any device context (this is all handled by the |
107 // image to the device. This is used as a hint to the browser that it does | 120 /// paint manager). |
108 // not need to do alpha blending, which speeds up painting. If you generate | 121 /// |
109 // non-opqaue pixels or aren't sure, set this to false for more general | 122 /// @param client A non-owning pointer and must remain valid (normally the |
110 // blending. | 123 /// object implementing the Client interface will own the paint manager). |
111 // | 124 /// |
112 // If you set is_always_opaque, your alpha channel should always be set to | 125 /// @param is_always_opaque A flag passed to the device contexts that this |
113 // 0xFF or there may be painting artifacts. Being opaque will allow the | 126 /// class creates. Set this to true if your instance always draws an opaque |
114 // browser to do a memcpy rather than a blend to paint the plugin, and this | 127 /// image to the device. This is used as a hint to the browser that it does |
115 // means your alpha values will get set on the page backing store. If these | 128 /// not need to do alpha blending, which speeds up painting. If you generate |
116 // values are incorrect, it could mess up future blending. If you aren't | 129 /// non-opqaue pixels or aren't sure, set this to false for more general |
117 // sure, it is always correct to specify that it it not opaque. | 130 /// blending. |
118 // | 131 /// |
119 // You will need to call SetSize before this class will do anything. Normally | 132 /// If you set is_always_opaque, your alpha channel should always be set to |
120 // you do this from the ViewChanged method of your plugin instance. | 133 /// 0xFF or there may be painting artifacts. Being opaque will allow the |
| 134 /// browser to do a memcpy rather than a blend to paint the plugin, and this |
| 135 /// means your alpha values will get set on the page backing store. If these |
| 136 /// values are incorrect, it could mess up future blending. If you aren't |
| 137 /// sure, it is always correct to specify that it it not opaque. |
121 PaintManager(Instance* instance, Client* client, bool is_always_opaque); | 138 PaintManager(Instance* instance, Client* client, bool is_always_opaque); |
122 | 139 |
| 140 /// Destructor. |
123 ~PaintManager(); | 141 ~PaintManager(); |
124 | 142 |
125 // You must call this function before using if you use the 0-arg constructor. | 143 /// Initialize() must be called if you are using the 0-arg constructor. |
126 // See the constructor for what these arguments mean. | 144 /// |
| 145 /// @param instance The instance using this paint manager to do its |
| 146 /// painting. Painting will automatically go to this instance and you don't |
| 147 /// have to manually bind any device context (this is all handled by the |
| 148 /// paint manager). |
| 149 /// @param client A non-owning pointer and must remain valid (normally the |
| 150 /// object implementing the Client interface will own the paint manager). |
| 151 /// @param is_always_opaque A flag passed to the device contexts that this |
| 152 /// class creates. Set this to true if your instance always draws an opaque |
| 153 /// image to the device. This is used as a hint to the browser that it does |
| 154 /// not need to do alpha blending, which speeds up painting. If you generate |
| 155 /// non-opqaue pixels or aren't sure, set this to false for more general |
| 156 /// blending. |
| 157 /// |
| 158 /// If you set is_always_opaque, your alpha channel should always be set to |
| 159 /// 0xFF or there may be painting artifacts. Being opaque will allow the |
| 160 /// browser to do a memcpy rather than a blend to paint the plugin, and this |
| 161 /// means your alpha values will get set on the page backing store. If these |
| 162 /// values are incorrect, it could mess up future blending. If you aren't |
| 163 /// sure, it is always correct to specify that it it not opaque. |
127 void Initialize(Instance* instance, Client* client, bool is_always_opaque); | 164 void Initialize(Instance* instance, Client* client, bool is_always_opaque); |
128 | 165 |
129 // Setters for the configuration settings in the paint aggregator. | 166 /// Setter function setting the max ratio of paint rect area to scroll rect |
130 // See paint_aggregator.h for what these mean. | 167 /// area that we will tolerate before downgrading the scroll into a repaint. |
| 168 /// |
| 169 /// If the combined area of paint rects contained within the scroll |
| 170 /// rect grows too large, then we might as well just treat |
| 171 /// the scroll rect as a paint rect. |
| 172 /// |
| 173 /// @param[in] area The max ratio of paint rect area to scroll rect area that |
| 174 /// we will tolerate before downgrading the scroll into a repaint. |
131 void set_max_redundant_paint_to_scroll_area(float area) { | 175 void set_max_redundant_paint_to_scroll_area(float area) { |
132 aggregator_.set_max_redundant_paint_to_scroll_area(area); | 176 aggregator_.set_max_redundant_paint_to_scroll_area(area); |
133 } | 177 } |
| 178 |
| 179 /// Setter function for setting the maximum number of paint rects. If we |
| 180 /// exceed this limit, then we'll start combining paint rects (see |
| 181 /// CombinePaintRects(). This limiting can be important since there is |
| 182 /// typically some overhead in deciding what to paint. If your module is fast |
| 183 /// at doing these computations, raise this threshold, if your module is |
| 184 /// slow, lower it (probably requires some tuning to find the right value). |
| 185 /// |
| 186 /// @param[in] max_rects The maximum number of paint rects. |
134 void set_max_paint_rects(size_t max_rects) { | 187 void set_max_paint_rects(size_t max_rects) { |
135 aggregator_.set_max_paint_rects(max_rects); | 188 aggregator_.set_max_paint_rects(max_rects); |
136 } | 189 } |
137 | 190 |
138 // Sets the size of the plugin. If the size is the same as the previous call, | 191 /// SetSize() sets the size of the instance. If the size is the same as the |
139 // this will be a NOP. If the size has changed, a new device will be | 192 /// previous call, this will be a NOP. If the size has changed, a new device |
140 // allocated to the given size and a paint to that device will be scheduled. | 193 /// will be allocated to the given size and a paint to that device will be |
141 // | 194 /// scheduled. |
142 // This is intended to be called from ViewChanged with the size of the | 195 /// |
143 // plugin. Since it tracks the old size and only allocates when the size | 196 /// This function is intended to be called from <code>ViewChanged</code> with |
144 // changes, you can always call this function without worrying about whether | 197 /// the size of the instance. Since it tracks the old size and only allocates |
145 // the size changed or ViewChanged is called for another reason (like the | 198 /// when the size changes, you can always call this function without worrying |
146 // position changed). | 199 /// about whether the size changed or ViewChanged() is called for another |
| 200 /// reason (like the position changed). |
| 201 /// |
| 202 /// @param new_size The new size for the instance. |
147 void SetSize(const Size& new_size); | 203 void SetSize(const Size& new_size); |
148 | 204 |
149 // Provides access to the underlying device in case you need it. If you have | 205 /// This function provides access to the underlying device in case you need |
150 // done a SetSize, note that the graphics context won't be updated until | 206 /// it. If you have done a SetSize(), note that the graphics context won't be |
151 // right before the next OnPaint call. | 207 /// updated until right before the next call to OnPaint(). |
152 // | 208 /// |
153 // Note: if you call Flush on this device the paint manager will get very | 209 /// <strong>Note:</strong> If you call Flush on this device the paint manager |
154 // confused, don't do this! | 210 /// will get very confused, don't do this! |
155 const Graphics2D& graphics() const { return graphics_; } | 211 const Graphics2D& graphics() const { return graphics_; } |
| 212 |
| 213 /// This function provides access to the underlying device in case you need |
| 214 /// it. If you have done a SetSize(), note that the graphics context won't be |
| 215 /// updated until right before the next call to OnPaint(). |
| 216 /// |
| 217 /// <strong>Note:</strong> If you call Flush on this device the paint manager |
| 218 /// will get very confused, don't do this! |
156 Graphics2D& graphics() { return graphics_; } | 219 Graphics2D& graphics() { return graphics_; } |
157 | 220 |
158 // Invalidate the entire plugin. | 221 /// Invalidate() invalidate the entire instance. |
159 void Invalidate(); | 222 void Invalidate(); |
160 | 223 |
161 // Invalidate the given rect. | 224 /// InvalidateRect() Invalidate the provided rect. |
| 225 /// |
| 226 /// @param rect The <code>Rect</code> to be invalidated. |
162 void InvalidateRect(const Rect& rect); | 227 void InvalidateRect(const Rect& rect); |
163 | 228 |
164 // The given rect should be scrolled by the given amounts. | 229 /// ScrollRect() scrolls the provided <code>clip_rect</code> by the |
| 230 /// <code>amount</code> argument. |
| 231 /// |
| 232 /// @param clip_rect The clip rectangle to scroll. |
| 233 /// @param amount The amount to scroll <code>clip_rect</code>. |
165 void ScrollRect(const Rect& clip_rect, const Point& amount); | 234 void ScrollRect(const Rect& clip_rect, const Point& amount); |
166 | 235 |
167 // Returns the size of the graphics context for the next paint operation. | 236 /// GetEffectiveSize() returns the size of the graphics context for the |
168 // This is the pending size if a resize is pending (the plugin has called | 237 /// next paint operation. This is the pending size if a resize is pending |
169 // SetSize but we haven't actually painted it yet), or the current size of | 238 /// (the instance has called SetSize() but we haven't actually painted it |
170 // no resize is pending. | 239 /// yet), or the current size of no resize is pending. |
| 240 /// |
| 241 /// @return The effetive size. |
171 Size GetEffectiveSize() const; | 242 Size GetEffectiveSize() const; |
172 | 243 |
173 private: | 244 private: |
174 // Disallow copy and assign (these are unimplemented). | 245 // Disallow copy and assign (these are unimplemented). |
175 PaintManager(const PaintManager&); | 246 PaintManager(const PaintManager&); |
176 PaintManager& operator=(const PaintManager&); | 247 PaintManager& operator=(const PaintManager&); |
177 | 248 |
178 // Makes sure there is a callback that will trigger a paint at a later time. | 249 // Makes sure there is a callback that will trigger a paint at a later time. |
179 // This will be either a Flush callback telling us we're allowed to generate | 250 // This will be either a Flush callback telling us we're allowed to generate |
180 // more data, or, if there's no flush callback pending, a manual call back | 251 // more data, or, if there's no flush callback pending, a manual call back |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
213 // When we get a resize, we don't bind right away (see SetSize). The | 284 // When we get a resize, we don't bind right away (see SetSize). The |
214 // has_pending_resize_ tells us that we need to do a resize for the next | 285 // has_pending_resize_ tells us that we need to do a resize for the next |
215 // paint operation. When true, the new size is in pending_size_. | 286 // paint operation. When true, the new size is in pending_size_. |
216 bool has_pending_resize_; | 287 bool has_pending_resize_; |
217 Size pending_size_; | 288 Size pending_size_; |
218 }; | 289 }; |
219 | 290 |
220 } // namespace pp | 291 } // namespace pp |
221 | 292 |
222 #endif // PPAPI_CPP_PAINT_MANAGER_H_ | 293 #endif // PPAPI_CPP_PAINT_MANAGER_H_ |
OLD | NEW |