OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "content/renderer/media/android/stream_texture_factory_android_synchron
ous_impl.h" | 5 #include "content/renderer/media/android/stream_texture_factory_android_synchron
ous_impl.h" |
6 | 6 |
| 7 #include <algorithm> |
| 8 |
| 9 #include "base/bind.h" |
| 10 #include "base/callback.h" |
| 11 #include "base/location.h" |
| 12 #include "base/memory/weak_ptr.h" |
| 13 #include "base/message_loop/message_loop_proxy.h" |
| 14 #include "base/process/process.h" |
| 15 #include "base/synchronization/lock.h" |
| 16 #include "cc/output/context_provider.h" |
| 17 #include "content/common/android/surface_texture_peer.h" |
| 18 #include "third_party/WebKit/public/platform/WebGraphicsContext3D.h" |
| 19 #include "ui/gl/android/surface_texture_bridge.h" |
| 20 |
7 namespace content { | 21 namespace content { |
8 | 22 |
9 StreamTextureFactorySynchronousImpl::StreamTextureFactorySynchronousImpl() {} | 23 namespace { |
| 24 |
| 25 class StreamTextureProxyImpl |
| 26 : public StreamTextureProxy, |
| 27 public base::SupportsWeakPtr<StreamTextureProxyImpl> { |
| 28 public: |
| 29 explicit StreamTextureProxyImpl( |
| 30 StreamTextureFactorySynchronousImpl::ContextProvider* provider); |
| 31 virtual ~StreamTextureProxyImpl(); |
| 32 |
| 33 // StreamTextureProxy implementation: |
| 34 virtual void BindToCurrentThread(int32 stream_id) OVERRIDE; |
| 35 virtual bool IsBoundToThread() OVERRIDE { return loop_.get() != NULL; } |
| 36 virtual void SetClient(cc::VideoFrameProvider::Client* client) OVERRIDE; |
| 37 virtual void Release() OVERRIDE; |
| 38 |
| 39 private: |
| 40 void OnFrameAvailable(); |
| 41 |
| 42 scoped_refptr<base::MessageLoopProxy> loop_; |
| 43 base::Lock client_lock_; |
| 44 cc::VideoFrameProvider::Client* client_; |
| 45 base::Closure callback_; |
| 46 |
| 47 scoped_refptr<StreamTextureFactorySynchronousImpl::ContextProvider> |
| 48 context_provider_; |
| 49 scoped_refptr<gfx::SurfaceTextureBridge> surface_texture_; |
| 50 |
| 51 float current_matrix_[16]; |
| 52 bool has_updated_; |
| 53 |
| 54 DISALLOW_IMPLICIT_CONSTRUCTORS(StreamTextureProxyImpl); |
| 55 }; |
| 56 |
| 57 StreamTextureProxyImpl::StreamTextureProxyImpl( |
| 58 StreamTextureFactorySynchronousImpl::ContextProvider* provider) |
| 59 : context_provider_(provider), has_updated_(false) { |
| 60 std::fill(current_matrix_, current_matrix_ + 16, 0); |
| 61 } |
| 62 |
| 63 StreamTextureProxyImpl::~StreamTextureProxyImpl() {} |
| 64 |
| 65 void StreamTextureProxyImpl::Release() { |
| 66 SetClient(NULL); |
| 67 if (!loop_->BelongsToCurrentThread()) |
| 68 loop_->DeleteSoon(FROM_HERE, this); |
| 69 else |
| 70 delete this; |
| 71 } |
| 72 |
| 73 void StreamTextureProxyImpl::SetClient(cc::VideoFrameProvider::Client* client) { |
| 74 base::AutoLock lock(client_lock_); |
| 75 client_ = client; |
| 76 } |
| 77 |
| 78 void StreamTextureProxyImpl::BindToCurrentThread(int stream_id) { |
| 79 loop_ = base::MessageLoopProxy::current(); |
| 80 |
| 81 surface_texture_ = context_provider_->GetSurfaceTexture(stream_id); |
| 82 if (!surface_texture_) { |
| 83 LOG(ERROR) << "Failed to get SurfaceTexture for stream."; |
| 84 return; |
| 85 } |
| 86 |
| 87 callback_ = |
| 88 base::Bind(&StreamTextureProxyImpl::OnFrameAvailable, AsWeakPtr()); |
| 89 surface_texture_->SetFrameAvailableCallback(callback_); |
| 90 } |
| 91 |
| 92 void StreamTextureProxyImpl::OnFrameAvailable() { |
| 93 // GetTransformMatrix only returns something valid after both is true: |
| 94 // - OnFrameAvailable was called |
| 95 // - we called UpdateTexImage |
| 96 if (has_updated_) { |
| 97 float matrix[16]; |
| 98 surface_texture_->GetTransformMatrix(matrix); |
| 99 |
| 100 if (memcmp(current_matrix_, matrix, sizeof(matrix)) != 0) { |
| 101 memcpy(current_matrix_, matrix, sizeof(matrix)); |
| 102 |
| 103 base::AutoLock lock(client_lock_); |
| 104 if (client_) |
| 105 client_->DidUpdateMatrix(current_matrix_); |
| 106 } |
| 107 } |
| 108 // OnFrameAvailable being called a second time implies that we called |
| 109 // updateTexImage since after we received the first frame. |
| 110 has_updated_ = true; |
| 111 |
| 112 base::AutoLock lock(client_lock_); |
| 113 if (client_) |
| 114 client_->DidReceiveFrame(); |
| 115 } |
| 116 |
| 117 } // namespace |
| 118 |
| 119 StreamTextureFactorySynchronousImpl::StreamTextureFactorySynchronousImpl( |
| 120 ContextProvider* context_provider, |
| 121 int view_id) |
| 122 : context_provider_(context_provider), view_id_(view_id) {} |
| 123 |
10 StreamTextureFactorySynchronousImpl::~StreamTextureFactorySynchronousImpl() {} | 124 StreamTextureFactorySynchronousImpl::~StreamTextureFactorySynchronousImpl() {} |
11 | 125 |
12 StreamTextureProxy* StreamTextureFactorySynchronousImpl::CreateProxy() { | 126 StreamTextureProxy* StreamTextureFactorySynchronousImpl::CreateProxy() { |
13 return NULL; | 127 return new StreamTextureProxyImpl(context_provider_); |
14 } | 128 } |
15 | 129 |
16 void StreamTextureFactorySynchronousImpl::EstablishPeer(int32 stream_id, | 130 void StreamTextureFactorySynchronousImpl::EstablishPeer(int32 stream_id, |
17 int player_id) {} | 131 int player_id) { |
| 132 scoped_refptr<gfx::SurfaceTextureBridge> surface_texture = |
| 133 context_provider_->GetSurfaceTexture(stream_id); |
| 134 if (surface_texture) { |
| 135 SurfaceTexturePeer::GetInstance()->EstablishSurfaceTexturePeer( |
| 136 base::Process::Current().handle(), |
| 137 surface_texture, |
| 138 view_id_, |
| 139 player_id); |
| 140 } |
| 141 } |
18 | 142 |
19 unsigned StreamTextureFactorySynchronousImpl::CreateStreamTexture( | 143 unsigned StreamTextureFactorySynchronousImpl::CreateStreamTexture( |
20 unsigned texture_target, | 144 unsigned texture_target, |
21 unsigned* texture_id, | 145 unsigned* texture_id, |
22 gpu::Mailbox* texture_mailbox, | 146 gpu::Mailbox* texture_mailbox, |
23 unsigned* texture_mailbox_sync_point) { | 147 unsigned* texture_mailbox_sync_point) { |
24 return 0; | 148 WebKit::WebGraphicsContext3D* context = context_provider_->Context3d(); |
| 149 unsigned stream_id = 0; |
| 150 if (context->makeContextCurrent()) { |
| 151 *texture_id = context->createTexture(); |
| 152 stream_id = context->createStreamTextureCHROMIUM(*texture_id); |
| 153 |
| 154 context->genMailboxCHROMIUM(texture_mailbox->name); |
| 155 context->bindTexture(texture_target, *texture_id); |
| 156 context->produceTextureCHROMIUM(texture_target, texture_mailbox->name); |
| 157 |
| 158 context->flush(); |
| 159 *texture_mailbox_sync_point = context->insertSyncPoint(); |
| 160 } |
| 161 return stream_id; |
25 } | 162 } |
26 | 163 |
27 void StreamTextureFactorySynchronousImpl::DestroyStreamTexture( | 164 void StreamTextureFactorySynchronousImpl::DestroyStreamTexture( |
28 unsigned texture_id) {} | 165 unsigned texture_id) { |
| 166 WebKit::WebGraphicsContext3D* context = context_provider_->Context3d(); |
| 167 if (context->makeContextCurrent()) { |
| 168 context->destroyStreamTextureCHROMIUM(texture_id); |
| 169 context->deleteTexture(texture_id); |
| 170 context->flush(); |
| 171 } |
| 172 } |
29 | 173 |
30 void StreamTextureFactorySynchronousImpl::SetStreamTextureSize( | 174 void StreamTextureFactorySynchronousImpl::SetStreamTextureSize( |
31 int32 texture_id, | 175 int32 stream_id, |
32 const gfx::Size& size) {} | 176 const gfx::Size& size) {} |
33 | 177 |
34 } // namespace content | 178 } // namespace content |
OLD | NEW |