| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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/common/gpu/stream_texture_android.h" | 5 #include "content/common/gpu/stream_texture_android.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "content/common/android/surface_texture_peer.h" | 8 #include "content/common/android/surface_texture_peer.h" |
| 9 #include "content/common/gpu/gpu_channel.h" | 9 #include "content/common/gpu/gpu_channel.h" |
| 10 #include "content/common/gpu/gpu_messages.h" | 10 #include "content/common/gpu/gpu_messages.h" |
| (...skipping 27 matching lines...) Expand all Loading... |
| 38 | 38 |
| 39 // TODO: Ideally a valid image id was returned to the client so that | 39 // TODO: Ideally a valid image id was returned to the client so that |
| 40 // it could then call glBindTexImage2D() for doing the following. | 40 // it could then call glBindTexImage2D() for doing the following. |
| 41 scoped_refptr<gfx::GLImage> gl_image( | 41 scoped_refptr<gfx::GLImage> gl_image( |
| 42 new StreamTexture(owner_stub, stream_id, texture->service_id())); | 42 new StreamTexture(owner_stub, stream_id, texture->service_id())); |
| 43 gfx::Size size = gl_image->GetSize(); | 43 gfx::Size size = gl_image->GetSize(); |
| 44 texture_manager->SetTarget(texture, GL_TEXTURE_EXTERNAL_OES); | 44 texture_manager->SetTarget(texture, GL_TEXTURE_EXTERNAL_OES); |
| 45 texture_manager->SetLevelInfo(texture, GL_TEXTURE_EXTERNAL_OES, 0, GL_RGBA, | 45 texture_manager->SetLevelInfo(texture, GL_TEXTURE_EXTERNAL_OES, 0, GL_RGBA, |
| 46 size.width(), size.height(), 1, 0, GL_RGBA, | 46 size.width(), size.height(), 1, 0, GL_RGBA, |
| 47 GL_UNSIGNED_BYTE, gfx::Rect(size)); | 47 GL_UNSIGNED_BYTE, gfx::Rect(size)); |
| 48 texture_manager->SetLevelImage( | 48 texture_manager->SetLevelImage(texture, GL_TEXTURE_EXTERNAL_OES, 0, |
| 49 texture, GL_TEXTURE_EXTERNAL_OES, 0, gl_image.get()); | 49 gl_image.get(), |
| 50 gpu::gles2::Texture::UNBOUND); |
| 50 return true; | 51 return true; |
| 51 } | 52 } |
| 52 | 53 |
| 53 return false; | 54 return false; |
| 54 } | 55 } |
| 55 | 56 |
| 56 StreamTexture::StreamTexture(GpuCommandBufferStub* owner_stub, | 57 StreamTexture::StreamTexture(GpuCommandBufferStub* owner_stub, |
| 57 int32 route_id, | 58 int32 route_id, |
| 58 uint32 texture_id) | 59 uint32 texture_id) |
| 59 : surface_texture_(gfx::SurfaceTexture::Create(texture_id)), | 60 : surface_texture_(gfx::SurfaceTexture::Create(texture_id)), |
| 60 size_(0, 0), | 61 size_(0, 0), |
| 61 has_valid_frame_(false), | 62 has_valid_frame_(false), |
| 62 has_pending_frame_(false), | 63 has_pending_frame_(false), |
| 63 owner_stub_(owner_stub), | 64 owner_stub_(owner_stub), |
| 64 route_id_(route_id), | 65 route_id_(route_id), |
| 65 has_listener_(false), | 66 has_listener_(false), |
| 67 texture_id_(texture_id), |
| 66 weak_factory_(this) { | 68 weak_factory_(this) { |
| 67 owner_stub->AddDestructionObserver(this); | 69 owner_stub->AddDestructionObserver(this); |
| 68 memset(current_matrix_, 0, sizeof(current_matrix_)); | 70 memset(current_matrix_, 0, sizeof(current_matrix_)); |
| 69 owner_stub->channel()->AddRoute(route_id, this); | 71 owner_stub->channel()->AddRoute(route_id, this); |
| 70 surface_texture_->SetFrameAvailableCallback(base::Bind( | 72 surface_texture_->SetFrameAvailableCallback(base::Bind( |
| 71 &StreamTexture::OnFrameAvailable, weak_factory_.GetWeakPtr())); | 73 &StreamTexture::OnFrameAvailable, weak_factory_.GetWeakPtr())); |
| 72 } | 74 } |
| 73 | 75 |
| 74 StreamTexture::~StreamTexture() { | 76 StreamTexture::~StreamTexture() { |
| 75 if (owner_stub_) { | 77 if (owner_stub_) { |
| 76 owner_stub_->RemoveDestructionObserver(this); | 78 owner_stub_->RemoveDestructionObserver(this); |
| 77 owner_stub_->channel()->RemoveRoute(route_id_); | 79 owner_stub_->channel()->RemoveRoute(route_id_); |
| 78 } | 80 } |
| 79 } | 81 } |
| 80 | 82 |
| 81 void StreamTexture::OnWillDestroyStub() { | 83 void StreamTexture::OnWillDestroyStub() { |
| 82 owner_stub_->RemoveDestructionObserver(this); | 84 owner_stub_->RemoveDestructionObserver(this); |
| 83 owner_stub_->channel()->RemoveRoute(route_id_); | 85 owner_stub_->channel()->RemoveRoute(route_id_); |
| 84 owner_stub_ = NULL; | 86 owner_stub_ = NULL; |
| 85 | 87 |
| 86 // If the owner goes away, there is no need to keep the SurfaceTexture around. | 88 // If the owner goes away, there is no need to keep the SurfaceTexture around. |
| 87 // The GL texture will keep working regardless with the currently bound frame. | 89 // The GL texture will keep working regardless with the currently bound frame. |
| 88 surface_texture_ = NULL; | 90 surface_texture_ = NULL; |
| 89 } | 91 } |
| 90 | 92 |
| 91 void StreamTexture::Destroy(bool have_context) { | 93 void StreamTexture::Destroy(bool have_context) { |
| 92 NOTREACHED(); | 94 NOTREACHED(); |
| 93 } | 95 } |
| 94 | 96 |
| 95 void StreamTexture::WillUseTexImage() { | 97 bool StreamTexture::CopyTexImage(unsigned target) { |
| 98 if (target != GL_TEXTURE_EXTERNAL_OES) |
| 99 return false; |
| 100 |
| 96 if (!owner_stub_ || !surface_texture_.get()) | 101 if (!owner_stub_ || !surface_texture_.get()) |
| 97 return; | 102 return true; |
| 103 |
| 104 GLint texture_id; |
| 105 glGetIntegerv(GL_TEXTURE_BINDING_EXTERNAL_OES, &texture_id); |
| 106 DCHECK(texture_id); |
| 107 |
| 108 // The following code only works if we're being asked to copy into |
| 109 // |texture_id_|. Copying into a different texture is not supported. |
| 110 if (static_cast<unsigned>(texture_id) != texture_id_) |
| 111 return false; |
| 98 | 112 |
| 99 if (has_pending_frame_) { | 113 if (has_pending_frame_) { |
| 100 scoped_ptr<ui::ScopedMakeCurrent> scoped_make_current; | 114 scoped_ptr<ui::ScopedMakeCurrent> scoped_make_current; |
| 101 bool needs_make_current = | 115 bool needs_make_current = |
| 102 !owner_stub_->decoder()->GetGLContext()->IsCurrent(NULL); | 116 !owner_stub_->decoder()->GetGLContext()->IsCurrent(NULL); |
| 103 // On Android we should not have to perform a real context switch here when | 117 // On Android we should not have to perform a real context switch here when |
| 104 // using virtual contexts. | 118 // using virtual contexts. |
| 105 DCHECK(!needs_make_current || !owner_stub_->decoder() | 119 DCHECK(!needs_make_current || !owner_stub_->decoder() |
| 106 ->GetContextGroup() | 120 ->GetContextGroup() |
| 107 ->feature_info() | 121 ->feature_info() |
| (...skipping 15 matching lines...) Expand all Loading... |
| 123 owner_stub_->decoder()->GetContextState(); | 137 owner_stub_->decoder()->GetContextState(); |
| 124 const gpu::gles2::TextureUnit& active_unit = | 138 const gpu::gles2::TextureUnit& active_unit = |
| 125 state->texture_units[state->active_texture_unit]; | 139 state->texture_units[state->active_texture_unit]; |
| 126 glBindTexture(GL_TEXTURE_EXTERNAL_OES, | 140 glBindTexture(GL_TEXTURE_EXTERNAL_OES, |
| 127 active_unit.bound_texture_external_oes.get() | 141 active_unit.bound_texture_external_oes.get() |
| 128 ? active_unit.bound_texture_external_oes->service_id() | 142 ? active_unit.bound_texture_external_oes->service_id() |
| 129 : 0); | 143 : 0); |
| 130 } | 144 } |
| 131 } | 145 } |
| 132 | 146 |
| 147 TextureManager* texture_manager = |
| 148 owner_stub_->decoder()->GetContextGroup()->texture_manager(); |
| 149 gpu::gles2::Texture* texture = |
| 150 texture_manager->GetTextureForServiceId(texture_id_); |
| 151 if (texture) { |
| 152 // By setting image state to UNBOUND instead of COPIED we ensure that |
| 153 // CopyTexImage() is called each time the surface texture is used for |
| 154 // drawing. |
| 155 texture->SetLevelImage(GL_TEXTURE_EXTERNAL_OES, 0, this, |
| 156 gpu::gles2::Texture::UNBOUND); |
| 157 } |
| 158 |
| 133 if (has_listener_ && has_valid_frame_) { | 159 if (has_listener_ && has_valid_frame_) { |
| 134 float mtx[16]; | 160 float mtx[16]; |
| 135 surface_texture_->GetTransformMatrix(mtx); | 161 surface_texture_->GetTransformMatrix(mtx); |
| 136 | 162 |
| 137 // Only query the matrix once we have bound a valid frame. | 163 // Only query the matrix once we have bound a valid frame. |
| 138 if (memcmp(current_matrix_, mtx, sizeof(mtx)) != 0) { | 164 if (memcmp(current_matrix_, mtx, sizeof(mtx)) != 0) { |
| 139 memcpy(current_matrix_, mtx, sizeof(mtx)); | 165 memcpy(current_matrix_, mtx, sizeof(mtx)); |
| 140 | 166 |
| 141 GpuStreamTextureMsg_MatrixChanged_Params params; | 167 GpuStreamTextureMsg_MatrixChanged_Params params; |
| 142 memcpy(¶ms.m00, mtx, sizeof(mtx)); | 168 memcpy(¶ms.m00, mtx, sizeof(mtx)); |
| 143 owner_stub_->channel()->Send( | 169 owner_stub_->channel()->Send( |
| 144 new GpuStreamTextureMsg_MatrixChanged(route_id_, params)); | 170 new GpuStreamTextureMsg_MatrixChanged(route_id_, params)); |
| 145 } | 171 } |
| 146 } | 172 } |
| 173 |
| 174 return true; |
| 147 } | 175 } |
| 148 | 176 |
| 149 void StreamTexture::OnFrameAvailable() { | 177 void StreamTexture::OnFrameAvailable() { |
| 150 has_pending_frame_ = true; | 178 has_pending_frame_ = true; |
| 151 if (has_listener_ && owner_stub_) { | 179 if (has_listener_ && owner_stub_) { |
| 152 owner_stub_->channel()->Send( | 180 owner_stub_->channel()->Send( |
| 153 new GpuStreamTextureMsg_FrameAvailable(route_id_)); | 181 new GpuStreamTextureMsg_FrameAvailable(route_id_)); |
| 154 } | 182 } |
| 155 } | 183 } |
| 156 | 184 |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 214 return false; | 242 return false; |
| 215 } | 243 } |
| 216 | 244 |
| 217 void StreamTexture::OnMemoryDump(base::trace_event::ProcessMemoryDump* pmd, | 245 void StreamTexture::OnMemoryDump(base::trace_event::ProcessMemoryDump* pmd, |
| 218 uint64_t process_tracing_id, | 246 uint64_t process_tracing_id, |
| 219 const std::string& dump_name) { | 247 const std::string& dump_name) { |
| 220 // TODO(ericrk): Add OnMemoryDump for GLImages. crbug.com/514914 | 248 // TODO(ericrk): Add OnMemoryDump for GLImages. crbug.com/514914 |
| 221 } | 249 } |
| 222 | 250 |
| 223 } // namespace content | 251 } // namespace content |
| OLD | NEW |