Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "components/bitmap_uploader/bitmap_uploader.h" | 5 #include "components/bitmap_uploader/bitmap_uploader.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/callback.h" | 11 #include "base/callback.h" |
| 12 #include "components/mus/public/cpp/gles2_context.h" | |
| 13 #include "components/mus/public/cpp/gpu_service.h" | |
| 12 #include "components/mus/public/cpp/window.h" | 14 #include "components/mus/public/cpp/window.h" |
| 13 #include "components/mus/public/cpp/window_surface.h" | 15 #include "components/mus/public/cpp/window_surface.h" |
| 14 #include "mojo/converters/geometry/geometry_type_converters.h" | 16 #include "mojo/converters/geometry/geometry_type_converters.h" |
| 15 #include "mojo/converters/surfaces/surfaces_type_converters.h" | 17 #include "mojo/converters/surfaces/surfaces_type_converters.h" |
| 16 #include "mojo/converters/surfaces/surfaces_utils.h" | 18 #include "mojo/converters/surfaces/surfaces_utils.h" |
| 17 #include "mojo/public/c/gles2/chromium_extension.h" | |
| 18 #include "mojo/public/c/gles2/gles2.h" | |
| 19 #include "services/shell/public/cpp/connector.h" | 19 #include "services/shell/public/cpp/connector.h" |
| 20 | 20 |
| 21 namespace bitmap_uploader { | 21 namespace bitmap_uploader { |
| 22 namespace { | 22 namespace { |
| 23 | 23 |
| 24 const uint32_t g_transparent_color = 0x00000000; | 24 const uint32_t g_transparent_color = 0x00000000; |
| 25 | 25 |
| 26 void LostContext(void*) { | 26 void LostContext(void*) { |
| 27 // TODO(fsamuel): Figure out if there's something useful to do here. | 27 // TODO(fsamuel): Figure out if there's something useful to do here. |
| 28 } | 28 } |
| 29 | 29 |
| 30 } // namespace | 30 } // namespace |
| 31 | 31 |
| 32 const char kBitmapUploaderForAcceleratedWidget[] = | 32 const char kBitmapUploaderForAcceleratedWidget[] = |
| 33 "__BITMAP_UPLOADER_ACCELERATED_WIDGET__"; | 33 "__BITMAP_UPLOADER_ACCELERATED_WIDGET__"; |
| 34 | 34 |
| 35 BitmapUploader::BitmapUploader(mus::Window* window) | 35 BitmapUploader::BitmapUploader(mus::Window* window) |
| 36 : window_(window), | 36 : window_(window), |
| 37 color_(g_transparent_color), | 37 color_(g_transparent_color), |
| 38 width_(0), | 38 width_(0), |
| 39 height_(0), | 39 height_(0), |
| 40 format_(BGRA), | 40 format_(BGRA), |
| 41 next_resource_id_(1u), | 41 next_resource_id_(1u), |
| 42 id_namespace_(0u) {} | 42 id_namespace_(0u) {} |
| 43 | 43 |
| 44 BitmapUploader::~BitmapUploader() { | 44 BitmapUploader::~BitmapUploader() { |
| 45 MojoGLES2DestroyContext(gles2_context_); | |
| 46 } | 45 } |
| 47 | 46 |
| 48 void BitmapUploader::Init(shell::Connector* connector) { | 47 void BitmapUploader::Init(shell::Connector* connector) { |
| 49 surface_ = window_->RequestSurface(mus::mojom::SurfaceType::DEFAULT); | 48 surface_ = window_->RequestSurface(mus::mojom::SurfaceType::DEFAULT); |
| 50 surface_->BindToThread(); | 49 surface_->BindToThread(); |
| 51 surface_->set_client(this); | 50 surface_->set_client(this); |
| 52 | 51 |
| 53 connector->ConnectToInterface("mojo:mus", &gpu_service_); | 52 mojo::ScopedMessagePipeHandle scoped_handle; |
| 54 mus::mojom::CommandBufferPtr gles2_client; | 53 if (!mus::GpuService::UseChromeGpuCommandBuffer()) { |
|
Fady Samuel
2016/05/25 17:06:47
Drive by: Could we hide these details in the clien
Peng
2016/05/25 19:42:55
Yes of course. But to make this CL less complex, p
| |
| 55 gpu_service_->CreateOffscreenGLES2Context(GetProxy(&gles2_client)); | 54 connector->ConnectToInterface("mojo:mus", &gpu_service_); |
| 56 gles2_context_ = MojoGLES2CreateContext( | 55 mus::mojom::CommandBufferPtr gles2_client; |
| 57 gles2_client.PassInterface().PassHandle().release().value(), nullptr, | 56 gpu_service_->CreateOffscreenGLES2Context(GetProxy(&gles2_client)); |
| 58 &LostContext, nullptr); | 57 scoped_handle = gles2_client.PassInterface().PassHandle(); |
| 59 MojoGLES2MakeCurrent(gles2_context_); | 58 } else { |
| 59 mus::GpuService::Initialize(connector); | |
| 60 } | |
| 61 | |
| 62 gles2_context_.reset(new mus::GLES2Context( | |
| 63 std::vector<int32_t>(), std::move(scoped_handle), &LostContext, nullptr)); | |
| 64 CHECK(gles2_context_->Initialize()); | |
| 60 } | 65 } |
| 61 | 66 |
| 62 // Sets the color which is RGBA. | 67 // Sets the color which is RGBA. |
| 63 void BitmapUploader::SetColor(uint32_t color) { | 68 void BitmapUploader::SetColor(uint32_t color) { |
| 64 if (color_ == color) | 69 if (color_ == color) |
| 65 return; | 70 return; |
| 66 color_ = color; | 71 color_ = color; |
| 67 if (surface_) | 72 if (surface_) |
| 68 Upload(); | 73 Upload(); |
| 69 } | 74 } |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 96 mus::mojom::CompositorFrameMetadata::New(); | 101 mus::mojom::CompositorFrameMetadata::New(); |
| 97 meta->device_scale_factor = 1.0f; | 102 meta->device_scale_factor = 1.0f; |
| 98 frame->metadata = std::move(meta); | 103 frame->metadata = std::move(meta); |
| 99 | 104 |
| 100 frame->resources.resize(0u); | 105 frame->resources.resize(0u); |
| 101 | 106 |
| 102 pass->quads.resize(0u); | 107 pass->quads.resize(0u); |
| 103 pass->shared_quad_states.push_back( | 108 pass->shared_quad_states.push_back( |
| 104 mojo::CreateDefaultSQS(bounds.size())); | 109 mojo::CreateDefaultSQS(bounds.size())); |
| 105 | 110 |
| 106 MojoGLES2MakeCurrent(gles2_context_); | |
| 107 if (bitmap_.get()) { | 111 if (bitmap_.get()) { |
| 108 mojo::Size bitmap_size; | 112 mojo::Size bitmap_size; |
| 109 bitmap_size.width = width_; | 113 bitmap_size.width = width_; |
| 110 bitmap_size.height = height_; | 114 bitmap_size.height = height_; |
| 115 gpu::gles2::GLES2Interface* gl = gles2_context_->interface(); | |
| 111 GLuint texture_id = BindTextureForSize(bitmap_size); | 116 GLuint texture_id = BindTextureForSize(bitmap_size); |
| 112 glTexSubImage2D(GL_TEXTURE_2D, | 117 gl->TexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bitmap_size.width, |
| 113 0, | 118 bitmap_size.height, TextureFormat(), GL_UNSIGNED_BYTE, |
| 114 0, | 119 &((*bitmap_)[0])); |
| 115 0, | |
| 116 bitmap_size.width, | |
| 117 bitmap_size.height, | |
| 118 TextureFormat(), | |
| 119 GL_UNSIGNED_BYTE, | |
| 120 &((*bitmap_)[0])); | |
| 121 | 120 |
| 122 gpu::Mailbox mailbox; | 121 gpu::Mailbox mailbox; |
| 123 glGenMailboxCHROMIUM(mailbox.name); | 122 gl->GenMailboxCHROMIUM(mailbox.name); |
| 124 glProduceTextureCHROMIUM(GL_TEXTURE_2D, mailbox.name); | 123 gl->ProduceTextureCHROMIUM(GL_TEXTURE_2D, mailbox.name); |
| 125 | 124 |
| 126 const GLuint64 fence_sync = glInsertFenceSyncCHROMIUM(); | 125 const GLuint64 fence_sync = gl->InsertFenceSyncCHROMIUM(); |
| 127 glShallowFlushCHROMIUM(); | 126 gl->ShallowFlushCHROMIUM(); |
| 128 | 127 |
| 129 gpu::SyncToken sync_token; | 128 gpu::SyncToken sync_token; |
| 130 glGenSyncTokenCHROMIUM(fence_sync, sync_token.GetData()); | 129 gl->GenSyncTokenCHROMIUM(fence_sync, sync_token.GetData()); |
| 131 | 130 |
| 132 mus::mojom::TransferableResourcePtr resource = | 131 mus::mojom::TransferableResourcePtr resource = |
| 133 mus::mojom::TransferableResource::New(); | 132 mus::mojom::TransferableResource::New(); |
| 134 resource->id = next_resource_id_++; | 133 resource->id = next_resource_id_++; |
| 135 resource_to_texture_id_map_[resource->id] = texture_id; | 134 resource_to_texture_id_map_[resource->id] = texture_id; |
| 136 resource->format = mus::mojom::ResourceFormat::RGBA_8888; | 135 resource->format = mus::mojom::ResourceFormat::RGBA_8888; |
| 137 resource->filter = GL_LINEAR; | 136 resource->filter = GL_LINEAR; |
| 138 resource->size = bitmap_size.Clone(); | 137 resource->size = bitmap_size.Clone(); |
| 139 resource->mailbox_holder = | 138 resource->mailbox_holder = |
| 140 gpu::MailboxHolder(mailbox, sync_token, GL_TEXTURE_2D); | 139 gpu::MailboxHolder(mailbox, sync_token, GL_TEXTURE_2D); |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 206 pass->quads.push_back(std::move(quad)); | 205 pass->quads.push_back(std::move(quad)); |
| 207 } | 206 } |
| 208 | 207 |
| 209 frame->passes.push_back(std::move(pass)); | 208 frame->passes.push_back(std::move(pass)); |
| 210 | 209 |
| 211 // TODO(rjkroege, fsamuel): We should throttle frames. | 210 // TODO(rjkroege, fsamuel): We should throttle frames. |
| 212 surface_->SubmitCompositorFrame(std::move(frame), mojo::Closure()); | 211 surface_->SubmitCompositorFrame(std::move(frame), mojo::Closure()); |
| 213 } | 212 } |
| 214 | 213 |
| 215 uint32_t BitmapUploader::BindTextureForSize(const mojo::Size size) { | 214 uint32_t BitmapUploader::BindTextureForSize(const mojo::Size size) { |
| 215 gpu::gles2::GLES2Interface* gl = gles2_context_->interface(); | |
| 216 // TODO(jamesr): Recycle textures. | 216 // TODO(jamesr): Recycle textures. |
| 217 GLuint texture = 0u; | 217 GLuint texture = 0u; |
| 218 glGenTextures(1, &texture); | 218 gl->GenTextures(1, &texture); |
| 219 glBindTexture(GL_TEXTURE_2D, texture); | 219 gl->BindTexture(GL_TEXTURE_2D, texture); |
| 220 glTexImage2D(GL_TEXTURE_2D, | 220 gl->TexImage2D(GL_TEXTURE_2D, 0, TextureFormat(), size.width, size.height, 0, |
| 221 0, | 221 TextureFormat(), GL_UNSIGNED_BYTE, 0); |
| 222 TextureFormat(), | 222 gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 223 size.width, | |
| 224 size.height, | |
| 225 0, | |
| 226 TextureFormat(), | |
| 227 GL_UNSIGNED_BYTE, | |
| 228 0); | |
| 229 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); | |
| 230 return texture; | 223 return texture; |
| 231 } | 224 } |
| 232 | 225 |
| 233 void BitmapUploader::SetIdNamespace(uint32_t id_namespace) { | 226 void BitmapUploader::SetIdNamespace(uint32_t id_namespace) { |
| 234 id_namespace_ = id_namespace; | 227 id_namespace_ = id_namespace; |
| 235 if (color_ != g_transparent_color || bitmap_.get()) | 228 if (color_ != g_transparent_color || bitmap_.get()) |
| 236 Upload(); | 229 Upload(); |
| 237 } | 230 } |
| 238 | 231 |
| 239 void BitmapUploader::OnResourcesReturned( | 232 void BitmapUploader::OnResourcesReturned( |
| 240 mus::WindowSurface* surface, | 233 mus::WindowSurface* surface, |
| 241 mojo::Array<mus::mojom::ReturnedResourcePtr> resources) { | 234 mojo::Array<mus::mojom::ReturnedResourcePtr> resources) { |
| 242 MojoGLES2MakeCurrent(gles2_context_); | 235 gpu::gles2::GLES2Interface* gl = gles2_context_->interface(); |
| 243 // TODO(jamesr): Recycle. | 236 // TODO(jamesr): Recycle. |
| 244 for (size_t i = 0; i < resources.size(); ++i) { | 237 for (size_t i = 0; i < resources.size(); ++i) { |
| 245 mus::mojom::ReturnedResourcePtr resource = std::move(resources[i]); | 238 mus::mojom::ReturnedResourcePtr resource = std::move(resources[i]); |
| 246 DCHECK_EQ(1, resource->count); | 239 DCHECK_EQ(1, resource->count); |
| 247 glWaitSyncTokenCHROMIUM( | 240 gl->WaitSyncTokenCHROMIUM(resource->sync_token.GetConstData()); |
| 248 resource->sync_token.GetConstData()); | |
| 249 uint32_t texture_id = resource_to_texture_id_map_[resource->id]; | 241 uint32_t texture_id = resource_to_texture_id_map_[resource->id]; |
| 250 DCHECK_NE(0u, texture_id); | 242 DCHECK_NE(0u, texture_id); |
| 251 resource_to_texture_id_map_.erase(resource->id); | 243 resource_to_texture_id_map_.erase(resource->id); |
| 252 glDeleteTextures(1, &texture_id); | 244 gl->DeleteTextures(1, &texture_id); |
| 253 } | 245 } |
| 254 } | 246 } |
| 255 | 247 |
| 256 } // namespace bitmap_uploader | 248 } // namespace bitmap_uploader |
| OLD | NEW |