Index: cc/ipc/copy_output_result_struct_traits.cc |
diff --git a/cc/ipc/copy_output_result_struct_traits.cc b/cc/ipc/copy_output_result_struct_traits.cc |
index 0398fe115cd52463b56fbf2a1a1540d3d96b2935..5f9845e6af214ab1b81eab7dea4e275360f599b3 100644 |
--- a/cc/ipc/copy_output_result_struct_traits.cc |
+++ b/cc/ipc/copy_output_result_struct_traits.cc |
@@ -4,9 +4,78 @@ |
#include "cc/ipc/copy_output_result_struct_traits.h" |
+#include "mojo/public/cpp/bindings/strong_binding.h" |
+ |
+namespace { |
+ |
+// This class retains the release_callback_ of the CopyOutputResult that is |
+// being sent over mojo. A TextureMailboxReleaserPtr that talks to this impl |
+// object will be sent over mojo instead of the release_callback_ (which is not |
+// serializable). Once the client calls Release, the release_callback_ will be |
+// called. An object of this class will remain alive until the MessagePipe |
+// attached to it goes away (i.e. StrongBinding is used). |
+class TextureMailboxReleaserImpl : public cc::mojom::TextureMailboxReleaser { |
+ public: |
+ TextureMailboxReleaserImpl( |
+ std::unique_ptr<cc::SingleReleaseCallback> release_callback) |
+ : release_callback_(std::move(release_callback)) { |
+ DCHECK(release_callback_); |
+ } |
+ |
+ ~TextureMailboxReleaserImpl() override { |
+ // If the client fails to call Release, we should do it ourselves because |
+ // release_callback_ will fail if it's never called. |
+ if (release_callback_) |
+ release_callback_->Run(gpu::SyncToken(), true); |
+ } |
+ |
+ // mojom::TextureMailboxReleaser implementation: |
+ void Release(const gpu::SyncToken& sync_token, bool is_lost) override { |
+ if (!release_callback_) |
+ return; |
+ release_callback_->Run(sync_token, is_lost); |
+ release_callback_.reset(); |
+ } |
+ |
+ private: |
+ std::unique_ptr<cc::SingleReleaseCallback> release_callback_; |
+}; |
+ |
+void Release(cc::mojom::TextureMailboxReleaserPtr ptr, |
+ const gpu::SyncToken& sync_token, |
+ bool is_lost) { |
+ ptr->Release(sync_token, is_lost); |
+} |
+ |
+} // namespace |
+ |
namespace mojo { |
// static |
+const SkBitmap& StructTraits<cc::mojom::CopyOutputResultDataView, |
+ std::unique_ptr<cc::CopyOutputResult>>:: |
+ bitmap(const std::unique_ptr<cc::CopyOutputResult>& result) { |
+ static SkBitmap* null_bitmap = new SkBitmap(); |
+ if (!result->bitmap_) |
+ return *null_bitmap; |
+ return *result->bitmap_; |
+} |
+ |
+// static |
+cc::mojom::TextureMailboxReleaserPtr |
+StructTraits<cc::mojom::CopyOutputResultDataView, |
+ std::unique_ptr<cc::CopyOutputResult>>:: |
+ releaser(const std::unique_ptr<cc::CopyOutputResult>& result) { |
+ if (!result->release_callback_) |
+ return {}; |
+ cc::mojom::TextureMailboxReleaserPtr releaser; |
+ auto impl = base::MakeUnique<TextureMailboxReleaserImpl>( |
+ std::move(result->release_callback_)); |
+ MakeStrongBinding(std::move(impl), MakeRequest(&releaser)); |
+ return releaser; |
+} |
+ |
+// static |
bool StructTraits<cc::mojom::CopyOutputResultDataView, |
std::unique_ptr<cc::CopyOutputResult>>:: |
Read(cc::mojom::CopyOutputResultDataView data, |
@@ -23,19 +92,26 @@ bool StructTraits<cc::mojom::CopyOutputResultDataView, |
if (!data.ReadTextureMailbox(&result->texture_mailbox_)) |
return false; |
+ auto releaser = data.TakeReleaser<cc::mojom::TextureMailboxReleaserPtr>(); |
+ if (releaser) { |
+ // CopyOutputResult does not have a TextureMailboxReleaserPtr member. |
+ // We use base::Bind to turn TextureMailboxReleaser::Release into a |
+ // ReleaseCallback. |
+ result->release_callback_ = cc::SingleReleaseCallback::Create( |
+ base::Bind(Release, base::Passed(&releaser))); |
+ } |
+ |
+ // Check that release callback and texture mailbox are set together. |
+ if (!!result->release_callback_ != result->texture_mailbox_.IsValid()) |
+ return false; |
+ |
+ // We can't have both a bitmap and a texture at the same time. |
+ if (result->HasBitmap() && result->HasTexture()) |
+ return false; |
+ |
*out_p = std::move(result); |
return true; |
} |
-// static |
-const SkBitmap& StructTraits<cc::mojom::CopyOutputResultDataView, |
- std::unique_ptr<cc::CopyOutputResult>>:: |
- bitmap(const std::unique_ptr<cc::CopyOutputResult>& result) { |
- static SkBitmap* null_bitmap = new SkBitmap(); |
- if (!result->bitmap_) |
- return *null_bitmap; |
- return *result->bitmap_; |
-} |
- |
} // namespace mojo |