[email protected] | dbd3d21 | 2012-07-13 02:17:22 | [diff] [blame] | 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 | |
Sadrul Habib Chowdhury | 31c9871 | 2018-12-11 04:15:13 | [diff] [blame] | 5 | #include "content/renderer/stream_texture_host_android.h" |
[email protected] | dbd3d21 | 2012-07-13 02:17:22 | [diff] [blame] | 6 | |
siva.gunturi | 6216f31 | 2016-10-21 06:49:55 | [diff] [blame] | 7 | #include "base/unguessable_token.h" |
[email protected] | dbd3d21 | 2012-07-13 02:17:22 | [diff] [blame] | 8 | #include "content/renderer/render_thread_impl.h" |
penghuang | 346a46f9 | 2016-03-31 21:37:52 | [diff] [blame] | 9 | #include "gpu/ipc/client/gpu_channel_host.h" |
Eric Karl | 625afbbd | 2019-06-27 00:51:00 | [diff] [blame] | 10 | #include "gpu/ipc/common/command_buffer_id.h" |
fsamuel | 19acfae | 2016-03-22 05:38:24 | [diff] [blame] | 11 | #include "gpu/ipc/common/gpu_messages.h" |
[email protected] | dbd3d21 | 2012-07-13 02:17:22 | [diff] [blame] | 12 | #include "ipc/ipc_message_macros.h" |
| 13 | |
| 14 | namespace content { |
| 15 | |
siva.gunturi | 6216f31 | 2016-10-21 06:49:55 | [diff] [blame] | 16 | StreamTextureHost::StreamTextureHost(scoped_refptr<gpu::GpuChannelHost> channel, |
| 17 | int32_t route_id) |
Jeremy Roman | 877cf8b4 | 2019-08-14 20:18:28 | [diff] [blame] | 18 | : route_id_(route_id), listener_(nullptr), channel_(std::move(channel)) { |
piman | 9fc22f3 | 2016-05-02 22:21:22 | [diff] [blame] | 19 | DCHECK(channel_); |
tguilbert | 3717d9a | 2016-11-28 20:53:10 | [diff] [blame] | 20 | DCHECK(route_id_); |
[email protected] | dbd3d21 | 2012-07-13 02:17:22 | [diff] [blame] | 21 | } |
| 22 | |
| 23 | StreamTextureHost::~StreamTextureHost() { |
Eric Karl | 625afbbd | 2019-06-27 00:51:00 | [diff] [blame] | 24 | if (channel_) { |
| 25 | // We destroy the StreamTexture as a deferred message followed by a flush |
| 26 | // to ensure this is ordered correctly with regards to previous deferred |
| 27 | // messages, such as CreateSharedImage. |
| 28 | uint32_t flush_id = channel_->EnqueueDeferredMessage( |
| 29 | GpuStreamTextureMsg_Destroy(route_id_)); |
| 30 | channel_->EnsureFlush(flush_id); |
siva.gunturi | 6216f31 | 2016-10-21 06:49:55 | [diff] [blame] | 31 | channel_->RemoveRoute(route_id_); |
Eric Karl | 625afbbd | 2019-06-27 00:51:00 | [diff] [blame] | 32 | } |
[email protected] | dbd3d21 | 2012-07-13 02:17:22 | [diff] [blame] | 33 | } |
| 34 | |
siva.gunturi | 6216f31 | 2016-10-21 06:49:55 | [diff] [blame] | 35 | bool StreamTextureHost::BindToCurrentThread(Listener* listener) { |
[email protected] | 01af2638 | 2014-01-23 22:23:25 | [diff] [blame] | 36 | listener_ = listener; |
tguilbert | 3717d9a | 2016-11-28 20:53:10 | [diff] [blame] | 37 | |
| 38 | if (channel_) { |
siva.gunturi | 6216f31 | 2016-10-21 06:49:55 | [diff] [blame] | 39 | channel_->AddRoute(route_id_, weak_ptr_factory_.GetWeakPtr()); |
| 40 | channel_->Send(new GpuStreamTextureMsg_StartListening(route_id_)); |
[email protected] | dbd3d21 | 2012-07-13 02:17:22 | [diff] [blame] | 41 | return true; |
| 42 | } |
| 43 | |
| 44 | return false; |
| 45 | } |
| 46 | |
| 47 | bool StreamTextureHost::OnMessageReceived(const IPC::Message& message) { |
| 48 | bool handled = true; |
| 49 | IPC_BEGIN_MESSAGE_MAP(StreamTextureHost, message) |
tguilbert | 3717d9a | 2016-11-28 20:53:10 | [diff] [blame] | 50 | IPC_MESSAGE_HANDLER(GpuStreamTextureMsg_FrameAvailable, OnFrameAvailable); |
[email protected] | dbd3d21 | 2012-07-13 02:17:22 | [diff] [blame] | 51 | IPC_MESSAGE_UNHANDLED(handled = false) |
| 52 | IPC_END_MESSAGE_MAP() |
| 53 | DCHECK(handled); |
| 54 | return handled; |
| 55 | } |
| 56 | |
[email protected] | dbd3d21 | 2012-07-13 02:17:22 | [diff] [blame] | 57 | void StreamTextureHost::OnChannelError() { |
tguilbert | 3717d9a | 2016-11-28 20:53:10 | [diff] [blame] | 58 | channel_ = nullptr; |
[email protected] | dbd3d21 | 2012-07-13 02:17:22 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | void StreamTextureHost::OnFrameAvailable() { |
| 62 | if (listener_) |
| 63 | listener_->OnFrameAvailable(); |
| 64 | } |
| 65 | |
siva.gunturi | 6216f31 | 2016-10-21 06:49:55 | [diff] [blame] | 66 | void StreamTextureHost::ForwardStreamTextureForSurfaceRequest( |
| 67 | const base::UnguessableToken& request_token) { |
tguilbert | 3717d9a | 2016-11-28 20:53:10 | [diff] [blame] | 68 | if (channel_) { |
siva.gunturi | 6216f31 | 2016-10-21 06:49:55 | [diff] [blame] | 69 | channel_->Send(new GpuStreamTextureMsg_ForwardForSurfaceRequest( |
| 70 | route_id_, request_token)); |
| 71 | } |
| 72 | } |
| 73 | |
Eric Karl | 625afbbd | 2019-06-27 00:51:00 | [diff] [blame] | 74 | gpu::Mailbox StreamTextureHost::CreateSharedImage(const gfx::Size& size) { |
| 75 | if (!channel_) |
| 76 | return gpu::Mailbox(); |
| 77 | |
| 78 | auto mailbox = gpu::Mailbox::GenerateForSharedImage(); |
| 79 | channel_->EnqueueDeferredMessage(GpuStreamTextureMsg_CreateSharedImage( |
| 80 | route_id_, mailbox, size, ++release_id_)); |
| 81 | return mailbox; |
| 82 | } |
| 83 | |
| 84 | gpu::SyncToken StreamTextureHost::GenUnverifiedSyncToken() { |
Vikas Soni | a26e13fd | 2019-08-14 00:14:58 | [diff] [blame] | 85 | // |channel_| can be set to null via OnChannelError() which means |
| 86 | // StreamTextureHost could still be alive when |channel_| is gone. |
| 87 | if (!channel_) |
| 88 | return gpu::SyncToken(); |
vikassoni | b14d8781 | 2019-08-01 19:08:14 | [diff] [blame] | 89 | |
Eric Karl | 625afbbd | 2019-06-27 00:51:00 | [diff] [blame] | 90 | return gpu::SyncToken(gpu::CommandBufferNamespace::GPU_IO, |
| 91 | gpu::CommandBufferIdFromChannelAndRoute( |
| 92 | channel_->channel_id(), route_id_), |
| 93 | release_id_); |
| 94 | } |
| 95 | |
[email protected] | dbd3d21 | 2012-07-13 02:17:22 | [diff] [blame] | 96 | } // namespace content |