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.
5 #include "content/common/gpu/stream_texture_manager_android.h"
8 #include "content/common/gpu/gpu_channel.h"
9 #include "content/common/gpu/gpu_messages.h"
10 #include "gpu/command_buffer/service/stream_texture.h"
11 #include "ui/gfx/size.h"
12 #include "ui/gl/android/surface_texture.h"
13 #include "ui/gl/gl_bindings.h"
17 StreamTextureManagerAndroid::StreamTextureAndroid::StreamTextureAndroid(
18 GpuChannel
* channel
, int service_id
)
19 : surface_texture_(new gfx::SurfaceTexture(service_id
)),
23 memset(current_matrix_
, 0, sizeof(current_matrix_
));
26 StreamTextureManagerAndroid::StreamTextureAndroid::~StreamTextureAndroid() {
29 void StreamTextureManagerAndroid::StreamTextureAndroid::Update() {
31 glGetIntegerv(GL_TEXTURE_BINDING_EXTERNAL_OES
, &texture_id
);
32 surface_texture_
->UpdateTexImage();
33 glBindTexture(GL_TEXTURE_EXTERNAL_OES
, texture_id
);
34 if (matrix_callback_
.is_null())
38 surface_texture_
->GetTransformMatrix(mtx
);
40 // Only query the matrix once we have bound a valid frame.
41 if (has_updated_
&& memcmp(current_matrix_
, mtx
, sizeof(mtx
)) != 0) {
42 memcpy(current_matrix_
, mtx
, sizeof(mtx
));
44 GpuStreamTextureMsg_MatrixChanged_Params params
;
45 memcpy(¶ms
.m00
, mtx
, sizeof(mtx
));
46 matrix_callback_
.Run(params
);
50 void StreamTextureManagerAndroid::StreamTextureAndroid::OnFrameAvailable(
53 channel_
->Send(new GpuStreamTextureMsg_FrameAvailable(route_id
));
56 gfx::Size
StreamTextureManagerAndroid::StreamTextureAndroid::GetSize() {
60 StreamTextureManagerAndroid::StreamTextureManagerAndroid(
65 StreamTextureManagerAndroid::~StreamTextureManagerAndroid() {
66 DCHECK(textures_
.size() == textures_from_service_id_
.size());
67 if (!textures_
.IsEmpty())
68 LOG(WARNING
) << "Undestroyed surface textures while closing GPU channel.";
71 GLuint
StreamTextureManagerAndroid::CreateStreamTexture(uint32 service_id
,
73 // service_id: the actual GL texture name
74 // client_id: texture name given to the client in the renderer (unused here)
75 // The return value here is what glCreateStreamTextureCHROMIUM() will return
76 // to identify the stream (i.e. surface texture).
77 StreamTextureAndroid
* texture
= new StreamTextureAndroid(
78 channel_
, service_id
);
79 textures_from_service_id_
.AddWithID(texture
, service_id
);
80 return textures_
.Add(texture
);
83 void StreamTextureManagerAndroid::DestroyStreamTexture(uint32 service_id
) {
84 gpu::StreamTexture
* texture
= textures_from_service_id_
.Lookup(service_id
);
86 textures_from_service_id_
.Remove(service_id
);
88 for (TextureMap::Iterator
<StreamTextureAndroid
> it(&textures_
);
89 !it
.IsAtEnd(); it
.Advance()) {
90 if (it
.GetCurrentValue() == texture
) {
91 textures_
.Remove(it
.GetCurrentKey());
98 gpu::StreamTexture
* StreamTextureManagerAndroid::LookupStreamTexture(
100 return textures_from_service_id_
.Lookup(service_id
);
103 void StreamTextureManagerAndroid::SendMatrixChanged(
105 const GpuStreamTextureMsg_MatrixChanged_Params
& params
) {
106 channel_
->Send(new GpuStreamTextureMsg_MatrixChanged(route_id
, params
));
109 void StreamTextureManagerAndroid::RegisterStreamTextureProxy(
110 int32 stream_id
, int32 route_id
) {
111 StreamTextureAndroid
* stream_texture
= textures_
.Lookup(stream_id
);
112 if (stream_texture
) {
113 // TODO(sievers): Post from binder thread to IO thread directly.
114 base::Closure frame_cb
= base::Bind(
115 &StreamTextureAndroid::OnFrameAvailable
,
116 stream_texture
->AsWeakPtr(),
118 StreamTextureAndroid::MatrixChangedCB matrix_cb
= base::Bind(
119 &StreamTextureManagerAndroid::SendMatrixChanged
,
120 base::Unretained(this),
122 stream_texture
->set_matrix_changed_callback(matrix_cb
);
123 stream_texture
->surface_texture()->SetFrameAvailableCallback(
128 void StreamTextureManagerAndroid::EstablishStreamTexture(
129 int32 stream_id
, int32 primary_id
, int32 secondary_id
) {
130 StreamTextureAndroid
* stream_texture
= textures_
.Lookup(stream_id
);
131 base::ProcessHandle process
= channel_
->renderer_pid();
133 if (stream_texture
) {
134 SurfaceTexturePeer::GetInstance()->EstablishSurfaceTexturePeer(
136 stream_texture
->surface_texture(),
142 void StreamTextureManagerAndroid::SetStreamTextureSize(
143 int32 stream_id
, const gfx::Size
& size
) {
144 StreamTextureAndroid
* stream_texture
= textures_
.Lookup(stream_id
);
146 stream_texture
->SetSize(size
);
149 } // namespace content