| 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/bitmap_uploader/bitmap_uploader.h" | 5 #include "examples/bitmap_uploader/bitmap_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 // GL_GLEXT_PROTOTYPES | 9 #endif // GL_GLEXT_PROTOTYPES |
| 10 | 10 |
| (...skipping 20 matching lines...) Expand all Loading... |
| 31 | 31 |
| 32 } | 32 } |
| 33 | 33 |
| 34 BitmapUploader::BitmapUploader(View* view) | 34 BitmapUploader::BitmapUploader(View* view) |
| 35 : view_(view), | 35 : view_(view), |
| 36 color_(TRANSPARENT_COLOR), | 36 color_(TRANSPARENT_COLOR), |
| 37 width_(0), | 37 width_(0), |
| 38 height_(0), | 38 height_(0), |
| 39 format_(BGRA), | 39 format_(BGRA), |
| 40 next_resource_id_(1u), | 40 next_resource_id_(1u), |
| 41 id_namespace_(0), | 41 id_namespace_(0u), |
| 42 weak_factory_(this) { | 42 local_id_(0u) { |
| 43 } | 43 } |
| 44 | 44 |
| 45 void BitmapUploader::Init(Shell* shell) { | 45 void BitmapUploader::Init(Shell* shell) { |
| 46 ServiceProviderPtr surfaces_service_provider; | 46 ServiceProviderPtr surfaces_service_provider; |
| 47 shell->ConnectToApplication("mojo:surfaces_service", | 47 shell->ConnectToApplication("mojo:surfaces_service", |
| 48 GetProxy(&surfaces_service_provider)); | 48 GetProxy(&surfaces_service_provider)); |
| 49 ConnectToService(surfaces_service_provider.get(), &surfaces_service_); | 49 ConnectToService(surfaces_service_provider.get(), &surface_); |
| 50 surface_.set_client(this); |
| 51 |
| 50 ServiceProviderPtr gpu_service_provider; | 52 ServiceProviderPtr gpu_service_provider; |
| 51 shell->ConnectToApplication("mojo:native_viewport_service", | 53 shell->ConnectToApplication("mojo:native_viewport_service", |
| 52 GetProxy(&gpu_service_provider)); | 54 GetProxy(&gpu_service_provider)); |
| 53 ConnectToService(gpu_service_provider.get(), &gpu_service_); | 55 ConnectToService(gpu_service_provider.get(), &gpu_service_); |
| 54 | 56 |
| 55 surfaces_service_->CreateSurfaceConnection(base::Bind( | |
| 56 &BitmapUploader::OnSurfaceConnectionCreated, weak_factory_.GetWeakPtr())); | |
| 57 CommandBufferPtr gles2_client; | 57 CommandBufferPtr gles2_client; |
| 58 gpu_service_->CreateOffscreenGLES2Context(GetProxy(&gles2_client)); | 58 gpu_service_->CreateOffscreenGLES2Context(GetProxy(&gles2_client)); |
| 59 gles2_context_ = | 59 gles2_context_ = |
| 60 MojoGLES2CreateContext(gles2_client.PassMessagePipe().release().value(), | 60 MojoGLES2CreateContext(gles2_client.PassMessagePipe().release().value(), |
| 61 &LostContext, | 61 &LostContext, |
| 62 NULL, | 62 NULL, |
| 63 Environment::GetDefaultAsyncWaiter()); | 63 Environment::GetDefaultAsyncWaiter()); |
| 64 MojoGLES2MakeCurrent(gles2_context_); | 64 MojoGLES2MakeCurrent(gles2_context_); |
| 65 } | 65 } |
| 66 | 66 |
| (...skipping 22 matching lines...) Expand all Loading... |
| 89 } | 89 } |
| 90 | 90 |
| 91 void BitmapUploader::Upload() { | 91 void BitmapUploader::Upload() { |
| 92 Size size; | 92 Size size; |
| 93 size.width = view_->bounds().width; | 93 size.width = view_->bounds().width; |
| 94 size.height = view_->bounds().height; | 94 size.height = view_->bounds().height; |
| 95 if (!size.width || !size.height) { | 95 if (!size.width || !size.height) { |
| 96 view_->SetSurfaceId(SurfaceId::New()); | 96 view_->SetSurfaceId(SurfaceId::New()); |
| 97 return; | 97 return; |
| 98 } | 98 } |
| 99 if (!surface_) // Can't upload yet, store for later. | 99 if (id_namespace_ == 0u) // Can't generate a qualified ID yet. |
| 100 return; | 100 return; |
| 101 if (!surface_id_ || size != surface_size_) { | 101 |
| 102 if (surface_id_) { | 102 if (size != surface_size_) { |
| 103 surface_->DestroySurface(surface_id_.Clone()); | 103 if (local_id_ != 0u) { |
| 104 } else { | 104 surface_->DestroySurface(local_id_); |
| 105 surface_id_ = SurfaceId::New(); | |
| 106 surface_id_->id_namespace = id_namespace_; | |
| 107 } | 105 } |
| 108 surface_id_->local++; | 106 local_id_++; |
| 109 surface_->CreateSurface(surface_id_.Clone()); | 107 surface_->CreateSurface(local_id_); |
| 110 view_->SetSurfaceId(surface_id_.Clone()); | |
| 111 surface_size_ = size; | 108 surface_size_ = size; |
| 109 auto qualified_id = SurfaceId::New(); |
| 110 qualified_id->id_namespace = id_namespace_; |
| 111 qualified_id->local = local_id_; |
| 112 view_->SetSurfaceId(qualified_id.Pass()); |
| 112 } | 113 } |
| 113 | 114 |
| 114 Rect bounds; | 115 Rect bounds; |
| 115 bounds.width = size.width; | 116 bounds.width = size.width; |
| 116 bounds.height = size.height; | 117 bounds.height = size.height; |
| 117 PassPtr pass = CreateDefaultPass(1, bounds); | 118 PassPtr pass = CreateDefaultPass(1, bounds); |
| 118 FramePtr frame = Frame::New(); | 119 FramePtr frame = Frame::New(); |
| 119 frame->resources.resize(0u); | 120 frame->resources.resize(0u); |
| 120 | 121 |
| 121 pass->quads.resize(0u); | 122 pass->quads.resize(0u); |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 215 color_state->color = Color::New(); | 216 color_state->color = Color::New(); |
| 216 color_state->color->rgba = color_; | 217 color_state->color->rgba = color_; |
| 217 color_state->force_anti_aliasing_off = false; | 218 color_state->force_anti_aliasing_off = false; |
| 218 | 219 |
| 219 quad->solid_color_quad_state = color_state.Pass(); | 220 quad->solid_color_quad_state = color_state.Pass(); |
| 220 pass->quads.push_back(quad.Pass()); | 221 pass->quads.push_back(quad.Pass()); |
| 221 } | 222 } |
| 222 | 223 |
| 223 frame->passes.push_back(pass.Pass()); | 224 frame->passes.push_back(pass.Pass()); |
| 224 | 225 |
| 225 surface_->SubmitFrame(surface_id_.Clone(), frame.Pass(), mojo::Closure()); | 226 surface_->SubmitFrame(local_id_, frame.Pass(), mojo::Closure()); |
| 226 } | 227 } |
| 227 | 228 |
| 228 void BitmapUploader::SetIdNamespace(uint32_t id_namespace) { | 229 void BitmapUploader::SetIdNamespace(uint32_t id_namespace) { |
| 230 id_namespace_ = id_namespace; |
| 231 if (color_ != TRANSPARENT_COLOR || bitmap_.get()) |
| 232 Upload(); |
| 229 } | 233 } |
| 230 | 234 |
| 231 void BitmapUploader::ReturnResources(Array<ReturnedResourcePtr> resources) { | 235 void BitmapUploader::ReturnResources(Array<ReturnedResourcePtr> resources) { |
| 232 if (!resources.size()) | 236 if (!resources.size()) |
| 233 return; | 237 return; |
| 234 MojoGLES2MakeCurrent(gles2_context_); | 238 MojoGLES2MakeCurrent(gles2_context_); |
| 235 // TODO(jamesr): Recycle. | 239 // TODO(jamesr): Recycle. |
| 236 for (size_t i = 0; i < resources.size(); ++i) { | 240 for (size_t i = 0; i < resources.size(); ++i) { |
| 237 ReturnedResourcePtr resource = resources[i].Pass(); | 241 ReturnedResourcePtr resource = resources[i].Pass(); |
| 238 DCHECK_EQ(1, resource->count); | 242 DCHECK_EQ(1, resource->count); |
| 239 glWaitSyncPointCHROMIUM(resource->sync_point); | 243 glWaitSyncPointCHROMIUM(resource->sync_point); |
| 240 uint32_t texture_id = resource_to_texture_id_map_[resource->id]; | 244 uint32_t texture_id = resource_to_texture_id_map_[resource->id]; |
| 241 DCHECK_NE(0u, texture_id); | 245 DCHECK_NE(0u, texture_id); |
| 242 resource_to_texture_id_map_.erase(resource->id); | 246 resource_to_texture_id_map_.erase(resource->id); |
| 243 glDeleteTextures(1, &texture_id); | 247 glDeleteTextures(1, &texture_id); |
| 244 } | 248 } |
| 245 } | 249 } |
| 246 | 250 |
| 247 void BitmapUploader::OnSurfaceConnectionCreated(SurfacePtr surface, | |
| 248 uint32_t id_namespace) { | |
| 249 surface_ = surface.Pass(); | |
| 250 surface_.set_client(this); | |
| 251 id_namespace_ = id_namespace; | |
| 252 if (color_ != TRANSPARENT_COLOR || bitmap_.get()) | |
| 253 Upload(); | |
| 254 } | |
| 255 | |
| 256 uint32_t BitmapUploader::BindTextureForSize(const Size size) { | 251 uint32_t BitmapUploader::BindTextureForSize(const Size size) { |
| 257 // TODO(jamesr): Recycle textures. | 252 // TODO(jamesr): Recycle textures. |
| 258 GLuint texture = 0u; | 253 GLuint texture = 0u; |
| 259 glGenTextures(1, &texture); | 254 glGenTextures(1, &texture); |
| 260 glBindTexture(GL_TEXTURE_2D, texture); | 255 glBindTexture(GL_TEXTURE_2D, texture); |
| 261 glTexImage2D(GL_TEXTURE_2D, | 256 glTexImage2D(GL_TEXTURE_2D, |
| 262 0, | 257 0, |
| 263 TextureFormat(), | 258 TextureFormat(), |
| 264 size.width, | 259 size.width, |
| 265 size.height, | 260 size.height, |
| 266 0, | 261 0, |
| 267 TextureFormat(), | 262 TextureFormat(), |
| 268 GL_UNSIGNED_BYTE, | 263 GL_UNSIGNED_BYTE, |
| 269 0); | 264 0); |
| 270 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); | 265 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 271 return texture; | 266 return texture; |
| 272 } | 267 } |
| 273 | 268 |
| 274 uint32_t BitmapUploader::TextureFormat() { | 269 uint32_t BitmapUploader::TextureFormat() { |
| 275 return format_ == BGRA ? GL_BGRA_EXT : GL_RGBA; | 270 return format_ == BGRA ? GL_BGRA_EXT : GL_RGBA; |
| 276 } | 271 } |
| 277 | 272 |
| 278 } // namespace mojo | 273 } // namespace mojo |
| OLD | NEW |