OLD | NEW |
| (Empty) |
1 // Copyright (c) 2010 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 WEBKIT_GLUE_PLUGINS_PEPPER_DEVICE_CONTEXT_2D_H_ | |
6 #define WEBKIT_GLUE_PLUGINS_PEPPER_DEVICE_CONTEXT_2D_H_ | |
7 | |
8 #include <vector> | |
9 | |
10 #include "base/basictypes.h" | |
11 #include "third_party/ppapi/c/pp_completion_callback.h" | |
12 #include "third_party/ppapi/c/ppb_device_context_2d.h" | |
13 #include "third_party/WebKit/WebKit/chromium/public/WebCanvas.h" | |
14 #include "webkit/glue/plugins/pepper_resource.h" | |
15 | |
16 typedef struct _ppb_DeviceContext2D PPB_DeviceContext2D; | |
17 | |
18 namespace gfx { | |
19 class Rect; | |
20 } | |
21 | |
22 namespace pepper { | |
23 | |
24 class ImageData; | |
25 class PluginInstance; | |
26 class PluginModule; | |
27 | |
28 class DeviceContext2D : public Resource { | |
29 public: | |
30 DeviceContext2D(PluginModule* module); | |
31 virtual ~DeviceContext2D(); | |
32 | |
33 // Returns a pointer to the interface implementing PPB_ImageData that is | |
34 // exposed to the plugin. | |
35 static const PPB_DeviceContext2D* GetInterface(); | |
36 | |
37 bool Init(int width, int height, bool is_always_opaque); | |
38 | |
39 // Resource override. | |
40 virtual DeviceContext2D* AsDeviceContext2D() { return this; } | |
41 | |
42 // PPB_DeviceContext2D functions. | |
43 bool Describe(PP_Size* size, bool* is_always_opaque); | |
44 bool PaintImageData(PP_Resource image, | |
45 const PP_Point* top_left, | |
46 const PP_Rect* src_rect); | |
47 bool Scroll(const PP_Rect* clip_rect, const PP_Point* amount); | |
48 bool ReplaceContents(PP_Resource image); | |
49 int32_t Flush(const PP_CompletionCallback& callback); | |
50 | |
51 bool ReadImageData(PP_Resource image, const PP_Point* top_left); | |
52 | |
53 // Assciates this device with the given plugin instance. You can pass NULL to | |
54 // clear the existing device. Returns true on success. In this case, a | |
55 // repaint of the page will also be scheduled. Failure means that the device | |
56 // is already bound to a different instance, and nothing will happen. | |
57 bool BindToInstance(PluginInstance* new_instance); | |
58 | |
59 // Paints the current backing store to the web page. | |
60 void Paint(WebKit::WebCanvas* canvas, | |
61 const gfx::Rect& plugin_rect, | |
62 const gfx::Rect& paint_rect); | |
63 | |
64 // Notifications that the view has rendered the page and that it has been | |
65 // flushed to the screen. These messages are used to send Flush callbacks to | |
66 // the plugin. See | |
67 void ViewInitiatedPaint(); | |
68 void ViewFlushedPaint(); | |
69 | |
70 ImageData* image_data() { return image_data_.get(); } | |
71 | |
72 private: | |
73 // Tracks a call to flush that requires a callback. | |
74 class FlushCallbackData { | |
75 public: | |
76 FlushCallbackData() { | |
77 Clear(); | |
78 } | |
79 | |
80 FlushCallbackData(const PP_CompletionCallback& callback) { | |
81 Set(callback); | |
82 } | |
83 | |
84 bool is_null() const { return !callback_.func; } | |
85 | |
86 void Set(const PP_CompletionCallback& callback) { | |
87 callback_ = callback; | |
88 } | |
89 | |
90 void Clear() { | |
91 callback_ = PP_MakeCompletionCallback(NULL, 0); | |
92 } | |
93 | |
94 void Execute(int32_t result) { | |
95 PP_RunCompletionCallback(&callback_, result); | |
96 } | |
97 | |
98 private: | |
99 PP_CompletionCallback callback_; | |
100 }; | |
101 | |
102 // Called internally to execute the different queued commands. The | |
103 // parameters to these functions will have already been validated. The last | |
104 // rect argument will be filled by each function with the area affected by | |
105 // the update that requires invalidation. If there were no pixels changed, | |
106 // this rect can be untouched. | |
107 void ExecutePaintImageData(ImageData* image, | |
108 int x, int y, | |
109 const gfx::Rect& src_rect, | |
110 gfx::Rect* invalidated_rect); | |
111 void ExecuteScroll(const gfx::Rect& clip, int dx, int dy, | |
112 gfx::Rect* invalidated_rect); | |
113 void ExecuteReplaceContents(ImageData* image, | |
114 gfx::Rect* invalidated_rect); | |
115 | |
116 // Schedules the offscreen callback to be fired at a future time. This | |
117 // will add the given item to the offscreen_flush_callbacks_ vector. | |
118 void ScheduleOffscreenCallback(const FlushCallbackData& callback); | |
119 | |
120 // Function scheduled to execute by ScheduleOffscreenCallback that actually | |
121 // issues the offscreen callbacks. | |
122 void ExecuteOffscreenCallback(FlushCallbackData data); | |
123 | |
124 // Returns true if there is any type of flush callback pending. | |
125 bool HasPendingFlush() const; | |
126 | |
127 scoped_refptr<ImageData> image_data_; | |
128 | |
129 // Non-owning pointer to the plugin instance this device context is currently | |
130 // bound to, if any. If the device context is currently unbound, this will | |
131 // be NULL. | |
132 PluginInstance* bound_instance_; | |
133 | |
134 // Keeps track of all drawing commands queued before a Flush call. | |
135 struct QueuedOperation; | |
136 typedef std::vector<QueuedOperation> OperationQueue; | |
137 OperationQueue queued_operations_; | |
138 | |
139 // Indicates whether any changes have been flushed to the backing store. | |
140 // This is initially false and is set to true at the first Flush() call. | |
141 bool flushed_any_data_; | |
142 | |
143 // The plugin can give us one "Flush" at a time. This flush will either be in | |
144 // the "unpainted" state (in which case unpainted_flush_callback_ will be | |
145 // non-NULL) or painted, in which case painted_flush_callback_ will be | |
146 // non-NULL). There can also be an offscreen callback which is handled | |
147 // separately (see offscreen_callback_pending_). Only one of these three | |
148 // things may be set at a time to enforce the "only one pending flush at a | |
149 // time" constraint. | |
150 // | |
151 // "Unpainted" ones are flush requests which have never been painted. These | |
152 // could have been done while the RenderView was already waiting for an ACK | |
153 // from a previous paint, so won't generate a new one yet. | |
154 // | |
155 // "Painted" ones are those flushes that have been painted by RenderView, but | |
156 // for which the ACK from the browser has not yet been received. | |
157 // | |
158 // When we get updates from a plugin with a callback, it is first added to | |
159 // the unpainted callbacks. When the renderer has initiated a paint, we'll | |
160 // move it to the painted callbacks list. When the renderer receives a flush, | |
161 // we'll execute the callback and remove it from the list. | |
162 FlushCallbackData unpainted_flush_callback_; | |
163 FlushCallbackData painted_flush_callback_; | |
164 | |
165 // When doing offscreen flushes, we issue a task that issues the callback | |
166 // later. This is set when one of those tasks is pending so that we can | |
167 // enforce the "only one pending flush at a time" constraint in the API. | |
168 bool offscreen_flush_pending_; | |
169 | |
170 DISALLOW_COPY_AND_ASSIGN(DeviceContext2D); | |
171 }; | |
172 | |
173 } // namespace pepper | |
174 | |
175 #endif // WEBKIT_GLUE_PLUGINS_PEPPER_DEVICE_CONTEXT_2D_H_ | |
OLD | NEW |