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