OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "examples/ganesh_app/texture_uploader.h" | 5 #include "examples/ganesh_app/texture_uploader.h" |
6 | 6 |
7 #ifndef GL_GLEXT_PROTOTYPES | 7 #ifndef GL_GLEXT_PROTOTYPES |
8 #define GL_GLEXT_PROTOTYPES | 8 #define GL_GLEXT_PROTOTYPES |
9 #endif | 9 #endif |
10 | 10 |
11 #include "base/bind.h" | 11 #include "base/bind.h" |
12 #include "gpu/GLES2/gl2chromium.h" | 12 #include "gpu/GLES2/gl2chromium.h" |
13 #include "gpu/GLES2/gl2extchromium.h" | 13 #include "gpu/GLES2/gl2extchromium.h" |
14 #include "mojo/public/c/gles2/gles2.h" | 14 #include "mojo/public/c/gles2/gles2.h" |
15 #include "mojo/public/cpp/application/connect.h" | 15 #include "mojo/public/cpp/application/connect.h" |
16 #include "mojo/public/interfaces/application/shell.mojom.h" | 16 #include "mojo/public/interfaces/application/shell.mojom.h" |
17 #include "mojo/services/geometry/public/cpp/geometry_util.h" | 17 #include "mojo/services/geometry/public/cpp/geometry_util.h" |
18 #include "mojo/services/surfaces/public/cpp/surfaces_utils.h" | 18 #include "mojo/services/surfaces/public/cpp/surfaces_utils.h" |
19 | 19 |
20 namespace examples { | 20 namespace examples { |
21 | 21 |
22 TextureUploader::Client::~Client() { | 22 TextureUploader::Client::~Client() { |
23 } | 23 } |
24 | 24 |
25 TextureUploader::TextureUploader(Client* client, | 25 TextureUploader::TextureUploader(Client* client, |
26 mojo::Shell* shell, | 26 mojo::Shell* shell, |
27 base::WeakPtr<mojo::GLContext> context) | 27 base::WeakPtr<mojo::GLContext> context) |
28 : client_(client), | 28 : client_(client), |
29 context_(context), | 29 context_(context), |
30 next_resource_id_(0), | 30 next_resource_id_(0u), |
31 id_namespace_(0), | 31 id_namespace_(0u), |
| 32 local_id_(0u), |
32 weak_factory_(this) { | 33 weak_factory_(this) { |
33 context_->AddObserver(this); | 34 context_->AddObserver(this); |
34 | 35 |
35 mojo::ServiceProviderPtr surfaces_service_provider; | 36 mojo::ServiceProviderPtr surfaces_service_provider; |
36 shell->ConnectToApplication("mojo:surfaces_service", | 37 shell->ConnectToApplication("mojo:surfaces_service", |
37 mojo::GetProxy(&surfaces_service_provider)); | 38 mojo::GetProxy(&surfaces_service_provider)); |
38 mojo::ConnectToService(surfaces_service_provider.get(), &surfaces_service_); | 39 mojo::ConnectToService(surfaces_service_provider.get(), &surface_); |
39 | 40 surface_.set_client(this); |
40 surfaces_service_->CreateSurfaceConnection( | |
41 base::Bind(&TextureUploader::OnSurfaceConnectionCreated, | |
42 weak_factory_.GetWeakPtr())); | |
43 } | 41 } |
44 | 42 |
45 TextureUploader::~TextureUploader() { | 43 TextureUploader::~TextureUploader() { |
46 if (surface_id_) | |
47 surface_->DestroySurface(surface_id_.Clone()); | |
48 if (context_.get()) | 44 if (context_.get()) |
49 context_->RemoveObserver(this); | 45 context_->RemoveObserver(this); |
50 } | 46 } |
51 | 47 |
52 void TextureUploader::Upload(scoped_ptr<mojo::GLTexture> texture) { | 48 void TextureUploader::Upload(scoped_ptr<mojo::GLTexture> texture) { |
53 if (!surface_) { | 49 if (!surface_) { |
54 pending_upload_ = texture.Pass(); | 50 pending_upload_ = texture.Pass(); |
55 return; | 51 return; |
56 } | 52 } |
57 | 53 |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
114 texture_state->background_color->rgba = 0; | 110 texture_state->background_color->rgba = 0; |
115 for (int i = 0; i < 4; ++i) | 111 for (int i = 0; i < 4; ++i) |
116 texture_state->vertex_opacity.push_back(1.f); | 112 texture_state->vertex_opacity.push_back(1.f); |
117 texture_state->flipped = false; | 113 texture_state->flipped = false; |
118 | 114 |
119 frame->resources.push_back(resource.Pass()); | 115 frame->resources.push_back(resource.Pass()); |
120 quad->texture_quad_state = texture_state.Pass(); | 116 quad->texture_quad_state = texture_state.Pass(); |
121 pass->quads.push_back(quad.Pass()); | 117 pass->quads.push_back(quad.Pass()); |
122 | 118 |
123 frame->passes.push_back(pass.Pass()); | 119 frame->passes.push_back(pass.Pass()); |
124 surface_->SubmitFrame(surface_id_.Clone(), frame.Pass(), mojo::Closure()); | 120 surface_->SubmitFrame(local_id_, frame.Pass(), mojo::Closure()); |
125 } | 121 } |
126 | 122 |
127 void TextureUploader::EnsureSurfaceForSize(const mojo::Size& size) { | 123 void TextureUploader::EnsureSurfaceForSize(const mojo::Size& size) { |
128 if (surface_id_ && size == surface_size_) | 124 if (local_id_ != 0u && size == surface_size_) |
129 return; | 125 return; |
130 | 126 |
131 if (surface_id_) { | 127 if (local_id_ != 0u) { |
132 surface_->DestroySurface(surface_id_.Clone()); | 128 surface_->DestroySurface(local_id_); |
133 } else { | |
134 surface_id_ = mojo::SurfaceId::New(); | |
135 surface_id_->id_namespace = id_namespace_; | |
136 } | 129 } |
137 | 130 |
138 surface_id_->local++; | 131 local_id_++; |
139 surface_->CreateSurface(surface_id_.Clone()); | 132 surface_->CreateSurface(local_id_); |
140 client_->OnSurfaceIdAvailable(surface_id_.Clone()); | |
141 surface_size_ = size; | 133 surface_size_ = size; |
| 134 if (id_namespace_ != 0u) |
| 135 SendFullyQualifiedID(); |
| 136 } |
| 137 void TextureUploader::SendFullyQualifiedID() { |
| 138 auto qualified_id = mojo::SurfaceId::New(); |
| 139 qualified_id->id_namespace = id_namespace_; |
| 140 qualified_id->local = local_id_; |
| 141 client_->OnSurfaceIdAvailable(qualified_id.Pass()); |
142 } | 142 } |
143 | 143 |
144 void TextureUploader::OnContextLost() { | 144 void TextureUploader::OnContextLost() { |
145 LOG(FATAL) << "Context lost."; | 145 LOG(FATAL) << "Context lost."; |
146 } | 146 } |
147 | 147 |
148 void TextureUploader::SetIdNamespace(uint32_t id_namespace) { | 148 void TextureUploader::SetIdNamespace(uint32_t id_namespace) { |
| 149 id_namespace_ = id_namespace; |
| 150 if (local_id_ != 0u) |
| 151 SendFullyQualifiedID(); |
149 } | 152 } |
150 | 153 |
151 void TextureUploader::ReturnResources( | 154 void TextureUploader::ReturnResources( |
152 mojo::Array<mojo::ReturnedResourcePtr> resources) { | 155 mojo::Array<mojo::ReturnedResourcePtr> resources) { |
153 if (!resources.size()) | 156 if (!resources.size()) |
154 return; | 157 return; |
155 context_->MakeCurrent(); | 158 context_->MakeCurrent(); |
156 for (size_t i = 0u; i < resources.size(); ++i) { | 159 for (size_t i = 0u; i < resources.size(); ++i) { |
157 mojo::ReturnedResourcePtr resource = resources[i].Pass(); | 160 mojo::ReturnedResourcePtr resource = resources[i].Pass(); |
158 DCHECK_EQ(1, resource->count); | 161 DCHECK_EQ(1, resource->count); |
159 glWaitSyncPointCHROMIUM(resource->sync_point); | 162 glWaitSyncPointCHROMIUM(resource->sync_point); |
160 mojo::GLTexture* texture = resource_to_texture_map_[resource->id]; | 163 mojo::GLTexture* texture = resource_to_texture_map_[resource->id]; |
161 DCHECK_NE(0u, texture->texture_id()); | 164 DCHECK_NE(0u, texture->texture_id()); |
162 resource_to_texture_map_.erase(resource->id); | 165 resource_to_texture_map_.erase(resource->id); |
163 delete texture; | 166 delete texture; |
164 } | 167 } |
165 } | 168 } |
166 | 169 |
167 void TextureUploader::OnSurfaceConnectionCreated(mojo::SurfacePtr surface, | |
168 uint32_t id_namespace) { | |
169 surface_ = surface.Pass(); | |
170 surface_.set_client(this); | |
171 id_namespace_ = id_namespace; | |
172 | |
173 if (pending_upload_) | |
174 Upload(pending_upload_.Pass()); | |
175 } | |
176 | |
177 } // namespace examples | 170 } // namespace examples |
OLD | NEW |