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