OLD | NEW |
(Empty) | |
| 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 #include "ppapi/proxy/graphics_2d_resource.h" |
| 6 |
| 7 #include "ppapi/c/ppb_graphics_2d.h" |
| 8 #include "ppapi/c/pp_bool.h" |
| 9 #include "ppapi/c/pp_point.h" |
| 10 #include "ppapi/c/pp_rect.h" |
| 11 #include "ppapi/c/pp_resource.h" |
| 12 #include "ppapi/c/pp_size.h" |
| 13 #include "ppapi/proxy/dispatch_reply_message.h" |
| 14 #include "ppapi/proxy/ppapi_messages.h" |
| 15 #include "ppapi/proxy/ppb_image_data_proxy.h" |
| 16 #include "ppapi/shared_impl/ppapi_globals.h" |
| 17 #include "ppapi/shared_impl/resource_tracker.h" |
| 18 #include "ppapi/shared_impl/tracked_callback.h" |
| 19 #include "ppapi/thunk/enter.h" |
| 20 |
| 21 namespace ppapi { |
| 22 namespace proxy { |
| 23 |
| 24 // static |
| 25 Graphics2DResource* Graphics2DResource::Create(Connection connection, |
| 26 PP_Instance instance, |
| 27 const PP_Size& size, |
| 28 PP_Bool is_always_opaque) { |
| 29 // These checks are copied from PPB_ImageData_Impl::Init to make tests passed. |
| 30 // Let's remove/refactor this when start to refactor ImageData. |
| 31 if (size.width <= 0 || size.height <= 0 || |
| 32 static_cast<int64>(size.width) * static_cast<int64>(size.height) * 4 >= |
| 33 std::numeric_limits<int32>::max()) { |
| 34 return NULL; |
| 35 } |
| 36 |
| 37 return new Graphics2DResource(connection, instance, size, is_always_opaque); |
| 38 } |
| 39 |
| 40 Graphics2DResource::Graphics2DResource(Connection connection, |
| 41 PP_Instance instance, |
| 42 const PP_Size& size, |
| 43 PP_Bool is_always_opaque) |
| 44 : PluginResource(connection, instance), |
| 45 size_(size), |
| 46 is_always_opaque_(is_always_opaque), |
| 47 scale_(1.0f) { |
| 48 if (!sent_create_to_renderer()) { |
| 49 SendCreateToRenderer( |
| 50 PpapiHostMsg_Graphics2D_Create(size, is_always_opaque)); |
| 51 } |
| 52 } |
| 53 |
| 54 Graphics2DResource::~Graphics2DResource() { |
| 55 } |
| 56 |
| 57 PP_Bool Graphics2DResource::Describe(PP_Size* size, PP_Bool* is_always_opaque) { |
| 58 *size = size_; |
| 59 *is_always_opaque = is_always_opaque_; |
| 60 return PP_TRUE; |
| 61 } |
| 62 |
| 63 thunk::PPB_Graphics2D_API* Graphics2DResource::AsPPB_Graphics2D_API() { |
| 64 return this; |
| 65 } |
| 66 |
| 67 void Graphics2DResource::PaintImageData(PP_Resource image_data, |
| 68 const PP_Point* top_left, |
| 69 const PP_Rect* src_rect) { |
| 70 Resource* image_object = |
| 71 PpapiGlobals::Get()->GetResourceTracker()->GetResource(image_data); |
| 72 if (!image_object || pp_instance() != image_object->pp_instance()) { |
| 73 Log(PP_LOGLEVEL_ERROR, |
| 74 "Graphics2DResource.PaintImageData: Bad image resource."); |
| 75 return; |
| 76 } |
| 77 |
| 78 PP_Rect dummy; |
| 79 memset(&dummy, 0, sizeof(PP_Rect)); |
| 80 PostToRenderer(PpapiHostMsg_Graphics2D_PaintImageData( |
| 81 kApiID, image_object->host_resource(), *top_left, |
| 82 !!src_rect, src_rect ? *src_rect : dummy)); |
| 83 } |
| 84 |
| 85 void Graphics2DResource::Scroll(const PP_Rect* clip_rect, |
| 86 const PP_Point* amount) { |
| 87 PP_Rect dummy; |
| 88 memset(&dummy, 0, sizeof(PP_Rect)); |
| 89 PostToRenderer(PpapiHostMsg_Graphics2D_Scroll( |
| 90 kApiID, !!clip_rect, clip_rect ? *clip_rect : dummy, *amount)); |
| 91 } |
| 92 |
| 93 void Graphics2DResource::ReplaceContents(PP_Resource image_data) { |
| 94 thunk::EnterResourceNoLock<thunk::PPB_ImageData_API> enter_image( |
| 95 image_data, true); |
| 96 if (enter_image.failed()) |
| 97 return; |
| 98 |
| 99 ImageData* image_object = static_cast<ImageData*>(enter_image.object()); |
| 100 if (pp_instance() != image_object->pp_instance()) { |
| 101 Log(PP_LOGLEVEL_ERROR, "Graphics2DResource.ReplaceContents: " |
| 102 "Image resource for another instance."); |
| 103 return; |
| 104 } |
| 105 image_object->set_used_in_replace_contents(); |
| 106 |
| 107 PostToRenderer(PpapiHostMsg_Graphics2D_ReplaceContents( |
| 108 kApiID, image_object->host_resource())); |
| 109 } |
| 110 |
| 111 bool Graphics2DResource::SetScale(float scale) { |
| 112 if (scale <= 0.0f) |
| 113 return false; |
| 114 PostToRenderer(PpapiHostMsg_Graphics2D_Dev_SetScale(kApiID, scale)); |
| 115 scale_ = scale; |
| 116 return true; |
| 117 } |
| 118 |
| 119 float Graphics2DResource::GetScale() { |
| 120 return scale_; |
| 121 } |
| 122 |
| 123 int32_t Graphics2DResource::Flush(scoped_refptr<TrackedCallback> callback, |
| 124 PP_Resource* old_image_data) { |
| 125 // We don't support this feature, it's for in-renderer only. |
| 126 if (old_image_data) |
| 127 *old_image_data = 0; |
| 128 |
| 129 if (TrackedCallback::IsPending(current_flush_callback_)) |
| 130 return PP_ERROR_INPROGRESS; // Can't have >1 flush pending. |
| 131 current_flush_callback_ = callback; |
| 132 |
| 133 CallRenderer(PpapiHostMsg_Graphics2D_Flush(kApiID)); |
| 134 return PP_OK_COMPLETIONPENDING; |
| 135 } |
| 136 |
| 137 bool Graphics2DResource::ReadImageData(PP_Resource image, |
| 138 const PP_Point* top_left) { |
| 139 if (!top_left) |
| 140 return false; |
| 141 PpapiMsg_Graphics2D_ReadImageDataAck reply; |
| 142 int32_t result = CallRendererSync( |
| 143 PpapiHostMsg_Graphics2D_ReadImageData(kApiID, |
| 144 image, |
| 145 *top_left), |
| 146 &reply); |
| 147 return result == PP_OK; |
| 148 } |
| 149 |
| 150 void Graphics2DResource::OnReplyReceived( |
| 151 const ResourceMessageReplyParams& params, |
| 152 const IPC::Message& msg) { |
| 153 IPC_BEGIN_MESSAGE_MAP(Graphics2DResource, msg) |
| 154 PPAPI_DISPATCH_RESOURCE_REPLY(PpapiMsg_Graphics2D_FlushACK, |
| 155 OnPluginMsgFlushACK) |
| 156 IPC_END_MESSAGE_MAP() |
| 157 } |
| 158 |
| 159 void Graphics2DResource::OnPluginMsgFlushACK( |
| 160 const ResourceMessageReplyParams& params, |
| 161 int32_t pp_error) { |
| 162 TrackedCallback::ClearAndRun(¤t_flush_callback_, pp_error); |
| 163 } |
| 164 |
| 165 } // namespace proxy |
| 166 } // namespace ppapi |
OLD | NEW |