| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ppapi/proxy/compositor_resource.h" | 5 #include "ppapi/proxy/compositor_resource.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | |
| 8 #include "ppapi/proxy/ppapi_messages.h" | |
| 9 | |
| 10 namespace ppapi { | 7 namespace ppapi { |
| 11 namespace proxy { | 8 namespace proxy { |
| 12 | 9 |
| 13 CompositorResource::CompositorResource(Connection connection, | 10 CompositorResource::CompositorResource(Connection connection, |
| 14 PP_Instance instance) | 11 PP_Instance instance) |
| 15 : PluginResource(connection, instance), | 12 : PluginResource(connection, instance) { |
| 16 layer_reset_(true), | |
| 17 last_resource_id_(0) { | |
| 18 SendCreate(RENDERER, PpapiHostMsg_Compositor_Create()); | |
| 19 } | 13 } |
| 20 | 14 |
| 21 CompositorResource::~CompositorResource() { | 15 CompositorResource::~CompositorResource() { |
| 22 ResetLayersInternal(); | |
| 23 } | 16 } |
| 24 | 17 |
| 25 thunk::PPB_Compositor_API* CompositorResource::AsPPB_Compositor_API() { | 18 thunk::PPB_Compositor_API* CompositorResource::AsPPB_Compositor_API() { |
| 26 return this; | 19 return this; |
| 27 } | 20 } |
| 28 | 21 |
| 29 void CompositorResource::OnReplyReceived( | |
| 30 const ResourceMessageReplyParams& params, | |
| 31 const IPC::Message& msg) { | |
| 32 PPAPI_BEGIN_MESSAGE_MAP(CompositorResource, msg) | |
| 33 PPAPI_DISPATCH_PLUGIN_RESOURCE_CALL( | |
| 34 PpapiPluginMsg_Compositor_ReleaseResource, | |
| 35 OnPluginMsgReleaseResource) | |
| 36 PPAPI_DISPATCH_PLUGIN_RESOURCE_CALL_UNHANDLED( | |
| 37 PluginResource::OnReplyReceived(params, msg)) | |
| 38 PPAPI_END_MESSAGE_MAP() | |
| 39 } | |
| 40 | |
| 41 PP_Resource CompositorResource::AddLayer() { | 22 PP_Resource CompositorResource::AddLayer() { |
| 42 scoped_refptr<CompositorLayerResource> resource(new CompositorLayerResource( | 23 return 0; |
| 43 connection(), pp_instance(), this)); | |
| 44 layers_.push_back(resource); | |
| 45 return resource->GetReference(); | |
| 46 } | 24 } |
| 47 | 25 |
| 48 int32_t CompositorResource::CommitLayers( | 26 int32_t CompositorResource::CommitLayers( |
| 49 const scoped_refptr<ppapi::TrackedCallback>& callback) { | 27 const scoped_refptr<ppapi::TrackedCallback>& callback) { |
| 50 if (IsInProgress()) | 28 return PP_ERROR_NOTSUPPORTED; |
| 51 return PP_ERROR_INPROGRESS; | |
| 52 | |
| 53 std::vector<CompositorLayerData> layers; | |
| 54 layers.reserve(layers_.size()); | |
| 55 | |
| 56 for (LayerList::const_iterator it = layers_.begin(); | |
| 57 it != layers_.end(); ++it) { | |
| 58 if ((*it)->data().is_null()) | |
| 59 return PP_ERROR_FAILED; | |
| 60 layers.push_back((*it)->data()); | |
| 61 } | |
| 62 | |
| 63 commit_callback_ = callback; | |
| 64 Call<PpapiPluginMsg_Compositor_CommitLayersReply>( | |
| 65 RENDERER, | |
| 66 PpapiHostMsg_Compositor_CommitLayers(layers, layer_reset_), | |
| 67 base::Bind(&CompositorResource::OnPluginMsgCommitLayersReply, | |
| 68 base::Unretained(this)), | |
| 69 callback); | |
| 70 | |
| 71 return PP_OK_COMPLETIONPENDING; | |
| 72 } | 29 } |
| 73 | 30 |
| 74 int32_t CompositorResource::ResetLayers() { | 31 int32_t CompositorResource::ResetLayers() { |
| 75 if (IsInProgress()) | 32 return PP_ERROR_NOTSUPPORTED; |
| 76 return PP_ERROR_INPROGRESS; | |
| 77 ResetLayersInternal(); | |
| 78 return PP_OK; | |
| 79 } | |
| 80 | |
| 81 void CompositorResource::OnPluginMsgCommitLayersReply( | |
| 82 const ResourceMessageReplyParams& params) { | |
| 83 if (!TrackedCallback::IsPending(commit_callback_)) | |
| 84 return; | |
| 85 | |
| 86 // On success, we put layers' release_callbacks into a map, | |
| 87 // otherwise we will do nothing. So plugin may change layers and | |
| 88 // call CommitLayers() again. | |
| 89 if (params.result() == PP_OK) { | |
| 90 layer_reset_ = false; | |
| 91 for (LayerList::iterator it = layers_.begin(); | |
| 92 it != layers_.end(); ++it) { | |
| 93 ReleaseCallback release_callback = (*it)->release_callback(); | |
| 94 if (!release_callback.is_null()) { | |
| 95 release_callback_map_.insert(ReleaseCallbackMap::value_type( | |
| 96 (*it)->data().common.resource_id, release_callback)); | |
| 97 (*it)->ResetReleaseCallback(); | |
| 98 } | |
| 99 } | |
| 100 } | |
| 101 | |
| 102 scoped_refptr<TrackedCallback> callback; | |
| 103 callback.swap(commit_callback_); | |
| 104 callback->Run(params.result()); | |
| 105 } | |
| 106 | |
| 107 void CompositorResource::OnPluginMsgReleaseResource( | |
| 108 const ResourceMessageReplyParams& params, | |
| 109 int32_t id, | |
| 110 uint32_t sync_point, | |
| 111 bool is_lost) { | |
| 112 ReleaseCallbackMap::iterator it = release_callback_map_.find(id); | |
| 113 DCHECK(it != release_callback_map_.end()) << | |
| 114 "Can not found release_callback_ by id(" << id << ")!"; | |
| 115 it->second.Run(sync_point, is_lost); | |
| 116 release_callback_map_.erase(it); | |
| 117 } | |
| 118 | |
| 119 void CompositorResource::ResetLayersInternal() { | |
| 120 for (LayerList::iterator it = layers_.begin(); | |
| 121 it != layers_.end(); ++it) { | |
| 122 ReleaseCallback release_callback = (*it)->release_callback(); | |
| 123 if (!release_callback.is_null()) { | |
| 124 release_callback.Run(0, false); | |
| 125 (*it)->ResetReleaseCallback(); | |
| 126 } | |
| 127 (*it)->Invalidate(); | |
| 128 } | |
| 129 | |
| 130 layers_.clear(); | |
| 131 layer_reset_ = true; | |
| 132 } | 33 } |
| 133 | 34 |
| 134 } // namespace proxy | 35 } // namespace proxy |
| 135 } // namespace ppapi | 36 } // namespace ppapi |
| OLD | NEW |