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