Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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 "cc/ipc/copy_output_result_struct_traits.h" | 5 #include "cc/ipc/copy_output_result_struct_traits.h" |
| 6 | 6 |
| 7 #include "mojo/public/cpp/bindings/strong_binding.h" | |
| 8 | |
| 9 namespace { | |
| 10 | |
| 11 // This class retains the release_callback_ of the CopyOutputResult that is | |
| 12 // being sent over mojo. A TextureMailboxReleaserPtr that talks to this impl | |
| 13 // object will be sent over mojo instead of the release_callback_ (which is not | |
| 14 // serializable). Once the client calls Release, the release_callback_ will be | |
| 15 // called. An object of this class will remain alive until the MessagePipe | |
| 16 // attached to it goes away (i.e. StrongBinding is used). | |
| 17 class TextureMailboxReleaserImpl : public cc::mojom::TextureMailboxReleaser { | |
| 18 public: | |
| 19 TextureMailboxReleaserImpl( | |
| 20 std::unique_ptr<cc::SingleReleaseCallback> release_callback) | |
| 21 : release_callback_(std::move(release_callback)) { | |
| 22 DCHECK(release_callback_); | |
| 23 } | |
| 24 | |
| 25 ~TextureMailboxReleaserImpl() override { | |
| 26 // If the client fails to call Release, we should do it ourselves because | |
| 27 // release_callback_ will fail if it's never called. | |
| 28 if (release_callback_) | |
| 29 release_callback_->Run(gpu::SyncToken(), true); | |
| 30 } | |
| 31 | |
| 32 // mojom::TextureMailboxReleaser implementation: | |
| 33 void Release(const gpu::SyncToken& sync_token, bool is_lost) override { | |
| 34 if (!release_callback_) | |
| 35 return; | |
| 36 release_callback_->Run(sync_token, is_lost); | |
| 37 release_callback_.reset(); | |
| 38 } | |
| 39 | |
| 40 private: | |
| 41 std::unique_ptr<cc::SingleReleaseCallback> release_callback_; | |
| 42 }; | |
| 43 | |
| 44 void Release(cc::mojom::TextureMailboxReleaserPtr ptr, | |
| 45 const gpu::SyncToken& sync_token, | |
| 46 bool is_lost) { | |
| 47 ptr->Release(sync_token, is_lost); | |
| 48 } | |
| 49 | |
| 50 } // namespace | |
| 51 | |
| 7 namespace mojo { | 52 namespace mojo { |
| 8 | 53 |
| 9 // static | 54 // static |
| 10 bool StructTraits<cc::mojom::CopyOutputResultDataView, | |
| 11 std::unique_ptr<cc::CopyOutputResult>>:: | |
| 12 Read(cc::mojom::CopyOutputResultDataView data, | |
| 13 std::unique_ptr<cc::CopyOutputResult>* out_p) { | |
| 14 auto result = cc::CopyOutputResult::CreateEmptyResult(); | |
| 15 | |
| 16 if (!data.ReadSize(&result->size_)) | |
| 17 return false; | |
| 18 | |
| 19 result->bitmap_ = base::MakeUnique<SkBitmap>(); | |
| 20 if (!data.ReadBitmap(result->bitmap_.get())) | |
| 21 return false; | |
| 22 | |
| 23 if (!data.ReadTextureMailbox(&result->texture_mailbox_)) | |
| 24 return false; | |
| 25 | |
| 26 *out_p = std::move(result); | |
| 27 | |
| 28 return true; | |
| 29 } | |
| 30 | |
| 31 // static | |
| 32 const SkBitmap& StructTraits<cc::mojom::CopyOutputResultDataView, | 55 const SkBitmap& StructTraits<cc::mojom::CopyOutputResultDataView, |
| 33 std::unique_ptr<cc::CopyOutputResult>>:: | 56 std::unique_ptr<cc::CopyOutputResult>>:: |
| 34 bitmap(const std::unique_ptr<cc::CopyOutputResult>& result) { | 57 bitmap(const std::unique_ptr<cc::CopyOutputResult>& result) { |
| 35 static SkBitmap* null_bitmap = new SkBitmap(); | 58 static SkBitmap* null_bitmap = new SkBitmap(); |
| 36 if (!result->bitmap_) | 59 if (!result->bitmap_) |
| 37 return *null_bitmap; | 60 return *null_bitmap; |
| 38 return *result->bitmap_; | 61 return *result->bitmap_; |
| 39 } | 62 } |
| 40 | 63 |
| 64 // static | |
| 65 cc::mojom::TextureMailboxReleaserPtr | |
| 66 StructTraits<cc::mojom::CopyOutputResultDataView, | |
| 67 std::unique_ptr<cc::CopyOutputResult>>:: | |
| 68 releaser(const std::unique_ptr<cc::CopyOutputResult>& result) { | |
| 69 if (!result->release_callback_) | |
| 70 return {}; | |
| 71 cc::mojom::TextureMailboxReleaserPtr releaser; | |
| 72 auto impl = base::MakeUnique<TextureMailboxReleaserImpl>( | |
| 73 std::move(result->release_callback_)); | |
| 74 MakeStrongBinding(std::move(impl), MakeRequest(&releaser)); | |
| 75 return releaser; | |
| 76 } | |
| 77 | |
| 78 // static | |
| 79 bool StructTraits<cc::mojom::CopyOutputResultDataView, | |
| 80 std::unique_ptr<cc::CopyOutputResult>>:: | |
| 81 Read(cc::mojom::CopyOutputResultDataView data, | |
| 82 std::unique_ptr<cc::CopyOutputResult>* out_p) { | |
| 83 // We first read into local variables and then call the appropriate | |
| 84 // constructor of cc::CopyOutputResult. | |
| 85 gfx::Size size; | |
| 86 auto bitmap = base::MakeUnique<SkBitmap>(); | |
| 87 cc::TextureMailbox texture_mailbox; | |
| 88 std::unique_ptr<cc::SingleReleaseCallback> release_callback; | |
| 89 | |
| 90 if (!data.ReadSize(&size)) | |
| 91 return false; | |
| 92 | |
| 93 if (!data.ReadBitmap(bitmap.get())) | |
| 94 return false; | |
| 95 | |
| 96 if (!data.ReadTextureMailbox(&texture_mailbox)) | |
| 97 return false; | |
| 98 | |
| 99 auto releaser = data.TakeReleaser<cc::mojom::TextureMailboxReleaserPtr>(); | |
| 100 if (releaser) { | |
| 101 // CopyOutputResult does not have a TextureMailboxReleaserPtr member. | |
| 102 // We use base::Bind to turn TextureMailboxReleaser::Release into a | |
| 103 // ReleaseCallback. | |
| 104 release_callback = cc::SingleReleaseCallback::Create( | |
| 105 base::Bind(Release, base::Passed(&releaser))); | |
| 106 } | |
| 107 | |
| 108 // Empty result | |
|
danakj
2017/02/14 23:49:34
comments are sentences, needs periods.
| |
| 109 if (bitmap->isNull() && !texture_mailbox.IsTexture()) { | |
| 110 *out_p = cc::CopyOutputResult::CreateEmptyResult(); | |
| 111 return true; | |
| 112 } | |
| 113 | |
| 114 // Bitmap result | |
| 115 if (!bitmap->isNull()) { | |
| 116 // We can't have both a bitmap and a texture. | |
| 117 if (texture_mailbox.IsTexture()) | |
| 118 return false; | |
| 119 *out_p = cc::CopyOutputResult::CreateBitmapResult(std::move(bitmap)); | |
| 120 return true; | |
| 121 } | |
| 122 | |
| 123 // Texture result | |
| 124 DCHECK(texture_mailbox.IsTexture()); | |
| 125 if (size.IsEmpty()) | |
| 126 return false; | |
| 127 if (!release_callback) | |
| 128 return false; | |
| 129 *out_p = cc::CopyOutputResult::CreateTextureResult( | |
| 130 size, texture_mailbox, std::move(release_callback)); | |
| 131 return true; | |
| 132 } | |
| 133 | |
| 41 } // namespace mojo | 134 } // namespace mojo |
| OLD | NEW |