Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / content / renderer / pepper / pepper_graphics_2d_host.h
blobdf80cd556e7f44fa64c17c0d8b8054c91cb2a652
1 // Copyright (c) 2012 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.
5 #ifndef CONTENT_RENDERER_PEPPER_PEPPER_GRAPHICS_2D_HOST_H_
6 #define CONTENT_RENDERER_PEPPER_PEPPER_GRAPHICS_2D_HOST_H_
8 #include <vector>
10 #include "base/basictypes.h"
11 #include "base/compiler_specific.h"
12 #include "base/memory/weak_ptr.h"
13 #include "content/common/content_export.h"
14 #include "ppapi/c/ppb_graphics_2d.h"
15 #include "ppapi/host/host_message_context.h"
16 #include "ppapi/host/resource_host.h"
17 #include "third_party/WebKit/public/platform/WebCanvas.h"
18 #include "ui/events/latency_info.h"
19 #include "ui/gfx/geometry/point.h"
20 #include "ui/gfx/geometry/size.h"
22 namespace cc {
23 class SharedBitmap;
24 class SingleReleaseCallback;
25 class TextureMailbox;
28 namespace gfx {
29 class Rect;
32 namespace ppapi {
33 struct ViewData;
36 namespace content {
38 class PepperPluginInstanceImpl;
39 class PPB_ImageData_Impl;
40 class RendererPpapiHost;
42 class CONTENT_EXPORT PepperGraphics2DHost
43 : public ppapi::host::ResourceHost,
44 public base::SupportsWeakPtr<PepperGraphics2DHost> {
45 public:
46 static PepperGraphics2DHost* Create(
47 RendererPpapiHost* host,
48 PP_Instance instance,
49 PP_Resource resource,
50 const PP_Size& size,
51 PP_Bool is_always_opaque,
52 scoped_refptr<PPB_ImageData_Impl> backing_store);
54 ~PepperGraphics2DHost() override;
56 // ppapi::host::ResourceHost override.
57 int32_t OnResourceMessageReceived(
58 const IPC::Message& msg,
59 ppapi::host::HostMessageContext* context) override;
60 bool IsGraphics2DHost() override;
62 bool ReadImageData(PP_Resource image, const PP_Point* top_left);
63 // Assciates this device with the given plugin instance. You can pass NULL
64 // to clear the existing device. Returns true on success. In this case, a
65 // repaint of the page will also be scheduled. Failure means that the device
66 // is already bound to a different instance, and nothing will happen.
67 bool BindToInstance(PepperPluginInstanceImpl* new_instance);
68 // Paints the current backing store to the web page.
69 void Paint(blink::WebCanvas* canvas,
70 const gfx::Rect& plugin_rect,
71 const gfx::Rect& paint_rect);
73 bool PrepareTextureMailbox(
74 cc::TextureMailbox* mailbox,
75 scoped_ptr<cc::SingleReleaseCallback>* release_callback);
76 void AttachedToNewLayer();
78 // Notifications about the view's progress painting. See PluginInstance.
79 // These messages are used to send Flush callbacks to the plugin.
80 void ViewInitiatedPaint();
82 void SetScale(float scale);
83 float GetScale() const;
84 bool IsAlwaysOpaque() const;
85 PPB_ImageData_Impl* ImageData();
86 gfx::Size Size() const;
88 void ClearCache();
90 private:
91 PepperGraphics2DHost(RendererPpapiHost* host,
92 PP_Instance instance,
93 PP_Resource resource);
95 bool Init(int width,
96 int height,
97 bool is_always_opaque,
98 scoped_refptr<PPB_ImageData_Impl> backing_store);
100 int32_t OnHostMsgPaintImageData(ppapi::host::HostMessageContext* context,
101 const ppapi::HostResource& image_data,
102 const PP_Point& top_left,
103 bool src_rect_specified,
104 const PP_Rect& src_rect);
105 int32_t OnHostMsgScroll(ppapi::host::HostMessageContext* context,
106 bool clip_specified,
107 const PP_Rect& clip,
108 const PP_Point& amount);
109 int32_t OnHostMsgReplaceContents(ppapi::host::HostMessageContext* context,
110 const ppapi::HostResource& image_data);
111 int32_t OnHostMsgFlush(ppapi::host::HostMessageContext* context,
112 const std::vector<ui::LatencyInfo>& latency_info);
113 int32_t OnHostMsgSetScale(ppapi::host::HostMessageContext* context,
114 float scale);
115 int32_t OnHostMsgReadImageData(ppapi::host::HostMessageContext* context,
116 PP_Resource image,
117 const PP_Point& top_left);
119 // If |old_image_data| is not NULL, a previous used ImageData object will be
120 // reused. This is used by ReplaceContents.
121 int32_t Flush(PP_Resource* old_image_data);
123 // Called internally to execute the different queued commands. The
124 // parameters to these functions will have already been validated. The last
125 // rect argument will be filled by each function with the area affected by
126 // the update that requires invalidation. If there were no pixels changed,
127 // this rect can be untouched.
128 void ExecutePaintImageData(PPB_ImageData_Impl* image,
129 int x,
130 int y,
131 const gfx::Rect& src_rect,
132 gfx::Rect* invalidated_rect);
133 void ExecuteScroll(const gfx::Rect& clip,
134 int dx,
135 int dy,
136 gfx::Rect* invalidated_rect);
137 void ExecuteReplaceContents(PPB_ImageData_Impl* image,
138 gfx::Rect* invalidated_rect,
139 PP_Resource* old_image_data);
141 void SendFlushAck();
143 // Function scheduled to execute by ScheduleOffscreenFlushAck that actually
144 // issues the offscreen callbacks.
145 void SendOffscreenFlushAck();
147 // Schedules the offscreen flush ACK at a future time.
148 void ScheduleOffscreenFlushAck();
150 // Returns true if there is any type of flush callback pending.
151 bool HasPendingFlush() const;
153 // Scale |op_rect| to logical pixels, taking care to include partially-
154 // covered logical pixels (aka DIPs). Also scale optional |delta| to logical
155 // pixels as well for scrolling cases. Returns false for scrolling cases where
156 // scaling either |op_rect| or |delta| would require scrolling to fall back to
157 // invalidation due to rounding errors, true otherwise.
158 static bool ConvertToLogicalPixels(float scale,
159 gfx::Rect* op_rect,
160 gfx::Point* delta);
162 void ReleaseCallback(scoped_ptr<cc::SharedBitmap> bitmap,
163 const gfx::Size& bitmap_size,
164 uint32 sync_point,
165 bool lost_resource);
167 RendererPpapiHost* renderer_ppapi_host_;
169 scoped_refptr<PPB_ImageData_Impl> image_data_;
171 // Non-owning pointer to the plugin instance this context is currently bound
172 // to, if any. If the context is currently unbound, this will be NULL.
173 PepperPluginInstanceImpl* bound_instance_;
175 // Keeps track of all drawing commands queued before a Flush call.
176 struct QueuedOperation;
177 typedef std::vector<QueuedOperation> OperationQueue;
178 OperationQueue queued_operations_;
180 // True if we need to send an ACK to plugin.
181 bool need_flush_ack_;
183 // When doing offscreen flushes, we issue a task that issues the callback
184 // later. This is set when one of those tasks is pending so that we can
185 // enforce the "only one pending flush at a time" constraint in the API.
186 bool offscreen_flush_pending_;
188 // Set to true if the plugin declares that this device will always be opaque.
189 // This allows us to do more optimized painting in some cases.
190 bool is_always_opaque_;
192 // Set to the scale between what the plugin considers to be one pixel and one
193 // DIP
194 float scale_;
196 ppapi::host::ReplyMessageContext flush_reply_context_;
198 bool is_running_in_process_;
200 bool texture_mailbox_modified_;
201 bool is_using_texture_layer_;
203 // This is a bitmap that was recently released by the compositor and may be
204 // used to transfer bytes to the compositor again.
205 scoped_ptr<cc::SharedBitmap> cached_bitmap_;
206 gfx::Size cached_bitmap_size_;
208 friend class PepperGraphics2DHostTest;
209 DISALLOW_COPY_AND_ASSIGN(PepperGraphics2DHost);
212 } // namespace content
214 #endif // CONTENT_RENDERER_PEPPER_PEPPER_GRAPHICS_2D_HOST_H_