Chromium Code Reviews| 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 "mojo/services/public/cpp/view_manager/lib/bitmap_uploader.h" | 5 #include "mojo/services/public/cpp/view_manager/lib/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 |
| 11 #include "base/bind.h" | 11 #include "base/bind.h" |
| 12 #include "cc/surfaces/surface_id.h" | 12 #include "cc/surfaces/surface_id.h" |
| 13 #include "cc/surfaces/surface_id_allocator.h" | 13 #include "cc/surfaces/surface_id_allocator.h" |
| 14 #include "gpu/GLES2/gl2chromium.h" | 14 #include "gpu/GLES2/gl2chromium.h" |
| 15 #include "gpu/GLES2/gl2extchromium.h" | 15 #include "gpu/GLES2/gl2extchromium.h" |
| 16 #include "gpu/command_buffer/common/mailbox.h" | 16 #include "gpu/command_buffer/common/mailbox.h" |
| 17 #include "mojo/services/public/cpp/geometry/geometry_type_converters.h" | 17 #include "mojo/services/public/cpp/geometry/geometry_type_converters.h" |
| 18 #include "mojo/services/public/cpp/surfaces/surfaces_type_converters.h" | 18 #include "mojo/services/public/cpp/surfaces/surfaces_type_converters.h" |
| 19 #include "mojo/services/public/cpp/surfaces/surfaces_utils.h" | 19 #include "mojo/services/public/cpp/surfaces/surfaces_utils.h" |
| 20 #include "mojo/services/public/cpp/view_manager/lib/view_manager_client_impl.h" | |
| 20 #include "third_party/khronos/GLES2/gl2.h" | 21 #include "third_party/khronos/GLES2/gl2.h" |
| 21 #include "ui/gfx/geometry/rect.h" | 22 #include "ui/gfx/geometry/rect.h" |
| 22 | 23 |
| 23 namespace mojo { | 24 namespace mojo { |
| 24 | 25 |
| 25 namespace { | 26 namespace { |
| 26 void LostContext(void*) { | 27 void LostContext(void*) { |
| 27 DCHECK(false); | 28 DCHECK(false); |
| 28 } | 29 } |
| 29 | 30 |
| 30 uint32_t TextureFormat() { | 31 uint32_t TextureFormat() { |
| 31 return SK_B32_SHIFT ? GL_RGBA : GL_BGRA_EXT; | 32 return SK_B32_SHIFT ? GL_RGBA : GL_BGRA_EXT; |
| 32 } | 33 } |
| 33 } | 34 } |
| 34 | 35 |
| 35 BitmapUploader::BitmapUploader(SurfacesServicePtr surfaces_service, | 36 BitmapUploader::BitmapUploader(ViewManagerClientImpl* client, |
| 37 Id view_id, | |
| 38 SurfacesServicePtr surfaces_service, | |
| 36 GpuPtr gpu_service) | 39 GpuPtr gpu_service) |
| 37 : surfaces_service_(surfaces_service.Pass()), | 40 : client_(client), |
| 41 view_id_(view_id), | |
| 42 surfaces_service_(surfaces_service.Pass()), | |
| 38 gpu_service_(gpu_service.Pass()), | 43 gpu_service_(gpu_service.Pass()), |
| 44 color_(SK_ColorTRANSPARENT), | |
| 39 next_resource_id_(1u), | 45 next_resource_id_(1u), |
| 40 weak_factory_(this) { | 46 weak_factory_(this) { |
| 41 surfaces_service_->CreateSurfaceConnection(base::Bind( | 47 surfaces_service_->CreateSurfaceConnection(base::Bind( |
| 42 &BitmapUploader::OnSurfaceConnectionCreated, weak_factory_.GetWeakPtr())); | 48 &BitmapUploader::OnSurfaceConnectionCreated, weak_factory_.GetWeakPtr())); |
| 43 CommandBufferPtr gles2_client; | 49 CommandBufferPtr gles2_client; |
| 44 gpu_service_->CreateOffscreenGLES2Context(Get(&gles2_client)); | 50 gpu_service_->CreateOffscreenGLES2Context(Get(&gles2_client)); |
| 45 gles2_context_ = | 51 gles2_context_ = |
| 46 MojoGLES2CreateContext(gles2_client.PassMessagePipe().release().value(), | 52 MojoGLES2CreateContext(gles2_client.PassMessagePipe().release().value(), |
| 47 &LostContext, | 53 &LostContext, |
| 48 NULL, | 54 NULL, |
| 49 Environment::GetDefaultAsyncWaiter()); | 55 Environment::GetDefaultAsyncWaiter()); |
| 50 MojoGLES2MakeCurrent(gles2_context_); | 56 MojoGLES2MakeCurrent(gles2_context_); |
| 51 } | 57 } |
| 52 | 58 |
| 53 BitmapUploader::~BitmapUploader() { | 59 BitmapUploader::~BitmapUploader() { |
| 54 MojoGLES2DestroyContext(gles2_context_); | 60 MojoGLES2DestroyContext(gles2_context_); |
| 55 } | 61 } |
| 56 | 62 |
| 57 void BitmapUploader::OnSurfaceConnectionCreated(SurfacePtr surface, | 63 void BitmapUploader::SetSize(const gfx::Size& size) { |
| 58 uint32_t id_namespace) { | 64 if (size_ == size) |
| 59 surface_ = surface.Pass(); | 65 return; |
| 60 surface_.set_client(this); | 66 size_ = size; |
| 61 id_allocator_.reset(new cc::SurfaceIdAllocator(id_namespace)); | |
| 62 if (!bitmap_.isNull()) { | |
| 63 Upload(bitmap_, done_callback_); | |
| 64 bitmap_.reset(); | |
| 65 done_callback_.Reset(); | |
| 66 } | |
| 67 } | 67 } |
| 68 | 68 |
| 69 uint32_t BitmapUploader::BindTextureForSize(const gfx::Size size) { | 69 void BitmapUploader::SetColor(SkColor color) { |
| 70 // TODO(jamesr): Recycle textures. | 70 if (color_ == color) |
| 71 GLuint texture = 0u; | 71 return; |
| 72 glGenTextures(1, &texture); | 72 color_ = color; |
| 73 glBindTexture(GL_TEXTURE_2D, texture); | 73 if (surface_) |
| 74 glTexImage2D(GL_TEXTURE_2D, | 74 Upload(); |
| 75 0, | |
| 76 TextureFormat(), | |
| 77 size.width(), | |
| 78 size.height(), | |
| 79 0, | |
| 80 TextureFormat(), | |
| 81 GL_UNSIGNED_BYTE, | |
| 82 0); | |
| 83 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); | |
| 84 return texture; | |
| 85 } | 75 } |
| 86 | 76 |
| 87 void BitmapUploader::Upload( | 77 void BitmapUploader::SetBitmap(SkBitmap bitmap) { |
| 88 SkBitmap bitmap, | 78 bitmap_ = bitmap; |
| 89 const base::Callback<void(SurfaceIdPtr)>& done_callback) { | 79 if (surface_) |
| 90 if (bitmap.width() == 0 || bitmap.height() == 0) { | 80 Upload(); |
| 91 done_callback.Run(SurfaceId::New()); | 81 } |
| 82 | |
| 83 void BitmapUploader::Upload() { | |
| 84 if (size_.width() == 0 || size_.height() == 0) { | |
| 85 client_->SetSurfaceId(view_id_, SurfaceId::New()); | |
| 92 return; | 86 return; |
| 93 } | 87 } |
| 94 if (!surface_) { // Can't upload yet, store for later. | 88 if (!surface_) { // Can't upload yet, store for later. |
| 95 bitmap_ = bitmap; | 89 done_callback_ = done_callback_; |
| 96 done_callback_ = done_callback; | |
| 97 return; | 90 return; |
| 98 } | 91 } |
| 99 gfx::Size bitmap_size(bitmap.width(), bitmap.height()); | 92 if (id_.is_null() || size_ != surface_size_) { |
| 100 if (id_.is_null() || bitmap_size != surface_size_) { | |
| 101 if (!id_.is_null()) | 93 if (!id_.is_null()) |
| 102 surface_->DestroySurface(SurfaceId::From(id_)); | 94 surface_->DestroySurface(SurfaceId::From(id_)); |
| 103 id_ = id_allocator_->GenerateId(); | 95 id_ = id_allocator_->GenerateId(); |
| 104 surface_->CreateSurface(SurfaceId::From(id_), Size::From(bitmap_size)); | 96 surface_->CreateSurface(SurfaceId::From(id_), Size::From(size_)); |
| 105 surface_size_ = bitmap_size; | 97 client_->SetSurfaceId(view_id_, SurfaceId::From(id_)); |
| 98 surface_size_ = size_; | |
| 106 } | 99 } |
| 107 | 100 |
| 108 GLuint texture_id = BindTextureForSize(bitmap_size); | 101 gfx::Rect bounds(size_); |
| 109 bitmap.lockPixels(); | 102 PassPtr pass = CreateDefaultPass(1, bounds); |
| 110 glTexSubImage2D(GL_TEXTURE_2D, | 103 FramePtr frame = Frame::New(); |
| 111 0, | 104 frame->resources.resize(0u); |
| 112 0, | |
| 113 0, | |
| 114 bitmap_size.width(), | |
| 115 bitmap_size.height(), | |
| 116 TextureFormat(), | |
| 117 GL_UNSIGNED_BYTE, | |
| 118 bitmap.getPixels()); | |
| 119 bitmap.unlockPixels(); | |
| 120 | 105 |
| 121 gpu::Mailbox mailbox = gpu::Mailbox::Generate(); | 106 pass->quads.resize(0u); |
| 122 glProduceTextureCHROMIUM(GL_TEXTURE_2D, mailbox.name); | 107 pass->shared_quad_states.push_back(CreateDefaultSQS(size_)); |
| 123 GLuint sync_point = glInsertSyncPointCHROMIUM(); | |
| 124 | 108 |
| 125 TransferableResourcePtr resource = TransferableResource::New(); | 109 if (!bitmap_.isNull()) { |
| 126 resource->id = next_resource_id_++; | 110 gfx::Size bitmap_size(bitmap_.width(), bitmap_.height()); |
| 127 resource_to_texture_id_map_[resource->id] = texture_id; | 111 GLuint texture_id = BindTextureForSize(bitmap_size); |
| 128 resource->format = mojo::RESOURCE_FORMAT_RGBA_8888; | 112 bitmap_.lockPixels(); |
| 129 resource->filter = GL_LINEAR; | 113 glTexSubImage2D(GL_TEXTURE_2D, |
| 130 resource->size = Size::From(bitmap_size); | 114 0, |
| 131 MailboxHolderPtr mailbox_holder = MailboxHolder::New(); | 115 0, |
| 132 mailbox_holder->mailbox = Mailbox::From(mailbox); | 116 0, |
| 133 mailbox_holder->texture_target = GL_TEXTURE_2D; | 117 bitmap_size.width(), |
| 134 mailbox_holder->sync_point = sync_point; | 118 bitmap_size.height(), |
| 135 resource->mailbox_holder = mailbox_holder.Pass(); | 119 TextureFormat(), |
| 136 resource->is_repeated = false; | 120 GL_UNSIGNED_BYTE, |
| 137 resource->is_software = false; | 121 bitmap_.getPixels()); |
| 122 bitmap_.unlockPixels(); | |
| 138 | 123 |
| 139 gfx::Rect bitmap_rect(bitmap_size); | 124 gpu::Mailbox mailbox = gpu::Mailbox::Generate(); |
| 125 glProduceTextureCHROMIUM(GL_TEXTURE_2D, mailbox.name); | |
| 126 GLuint sync_point = glInsertSyncPointCHROMIUM(); | |
| 140 | 127 |
| 141 QuadPtr quad = Quad::New(); | 128 TransferableResourcePtr resource = TransferableResource::New(); |
| 142 quad->material = MATERIAL_TEXTURE_CONTENT; | 129 resource->id = next_resource_id_++; |
| 143 quad->rect = Rect::From(bitmap_rect); | 130 resource_to_texture_id_map_[resource->id] = texture_id; |
| 144 quad->opaque_rect = Rect::From(bitmap_rect); | 131 resource->format = mojo::RESOURCE_FORMAT_RGBA_8888; |
| 145 quad->visible_rect = Rect::From(bitmap_rect); | 132 resource->filter = GL_LINEAR; |
| 146 quad->needs_blending = true; | 133 resource->size = Size::From(bitmap_size); |
| 147 quad->shared_quad_state_index = 0u; | 134 MailboxHolderPtr mailbox_holder = MailboxHolder::New(); |
| 135 mailbox_holder->mailbox = Mailbox::From(mailbox); | |
| 136 mailbox_holder->texture_target = GL_TEXTURE_2D; | |
| 137 mailbox_holder->sync_point = sync_point; | |
| 138 resource->mailbox_holder = mailbox_holder.Pass(); | |
| 139 resource->is_repeated = false; | |
| 140 resource->is_software = false; | |
| 148 | 141 |
| 149 TextureQuadStatePtr texture_state = TextureQuadState::New(); | 142 QuadPtr quad = Quad::New(); |
| 150 texture_state->resource_id = resource->id; | 143 quad->material = MATERIAL_TEXTURE_CONTENT; |
| 151 texture_state->premultiplied_alpha = true; | 144 quad->rect = Rect::From(bounds); |
| 152 texture_state->uv_top_left = PointF::From(gfx::PointF(0.f, 0.f)); | 145 quad->opaque_rect = Rect::From(bounds); |
| 153 texture_state->uv_bottom_right = PointF::From(gfx::PointF(1.f, 1.f)); | 146 quad->visible_rect = Rect::From(bounds); |
| 154 texture_state->background_color = Color::From(SK_ColorRED); | 147 quad->needs_blending = true; |
| 155 for (int i = 0; i < 4; ++i) | 148 quad->shared_quad_state_index = 0u; |
| 156 texture_state->vertex_opacity.push_back(1.f); | |
| 157 texture_state->flipped = false; | |
| 158 | 149 |
| 159 quad->texture_quad_state = texture_state.Pass(); | 150 TextureQuadStatePtr texture_state = TextureQuadState::New(); |
| 151 texture_state->resource_id = resource->id; | |
| 152 texture_state->premultiplied_alpha = true; | |
| 153 texture_state->uv_top_left = PointF::From(gfx::PointF(0.f, 0.f)); | |
| 154 texture_state->uv_bottom_right = PointF::From(gfx::PointF(1.f, 1.f)); | |
| 155 texture_state->background_color = Color::From(SkColor(SK_ColorTRANSPARENT)); | |
| 156 for (int i = 0; i < 4; ++i) | |
| 157 texture_state->vertex_opacity.push_back(0.25f * i); | |
|
jamesr
2014/09/13 01:13:55
don't want to land this since it makes everything
| |
| 158 texture_state->flipped = false; | |
| 160 | 159 |
| 161 SharedQuadStatePtr sqs = CreateDefaultSQS(bitmap_size); | 160 frame->resources.push_back(resource.Pass()); |
| 161 quad->texture_quad_state = texture_state.Pass(); | |
| 162 pass->quads.push_back(quad.Pass()); | |
| 163 } | |
| 162 | 164 |
| 163 PassPtr pass = CreateDefaultPass(1, bitmap_rect); | 165 if (color_ != SK_ColorTRANSPARENT) { |
| 166 QuadPtr quad = Quad::New(); | |
| 167 quad->material = MATERIAL_SOLID_COLOR; | |
| 168 quad->rect = Rect::From(bounds); | |
| 169 quad->opaque_rect = Rect::From(gfx::Rect()); | |
| 170 quad->visible_rect = Rect::From(bounds); | |
| 171 quad->needs_blending = true; | |
| 172 quad->shared_quad_state_index = 0u; | |
| 164 | 173 |
| 165 pass->quads.push_back(quad.Pass()); | 174 SolidColorQuadStatePtr color_state = SolidColorQuadState::New(); |
| 166 pass->shared_quad_states.push_back(sqs.Pass()); | 175 color_state->color = Color::From(SkColorSetA(color_, 0x7f)); |
| 176 color_state->force_anti_aliasing_off = false; | |
| 167 | 177 |
| 168 FramePtr frame = Frame::New(); | 178 quad->solid_color_quad_state = color_state.Pass(); |
| 169 frame->resources.push_back(resource.Pass()); | 179 pass->quads.push_back(quad.Pass()); |
| 180 } | |
| 181 | |
| 170 frame->passes.push_back(pass.Pass()); | 182 frame->passes.push_back(pass.Pass()); |
| 171 | 183 |
| 172 surface_->SubmitFrame(SurfaceId::From(id_), frame.Pass()); | 184 surface_->SubmitFrame(SurfaceId::From(id_), frame.Pass()); |
| 173 done_callback.Run(SurfaceId::From(id_)); | |
| 174 } | 185 } |
| 175 | 186 |
| 176 void BitmapUploader::ReturnResources(Array<ReturnedResourcePtr> resources) { | 187 void BitmapUploader::ReturnResources(Array<ReturnedResourcePtr> resources) { |
| 177 // TODO(jamesr): Recycle. | 188 // TODO(jamesr): Recycle. |
| 178 for (size_t i = 0; i < resources.size(); ++i) { | 189 for (size_t i = 0; i < resources.size(); ++i) { |
| 179 ReturnedResourcePtr resource = resources[i].Pass(); | 190 ReturnedResourcePtr resource = resources[i].Pass(); |
| 180 DCHECK_EQ(1, resource->count); | 191 DCHECK_EQ(1, resource->count); |
| 181 glWaitSyncPointCHROMIUM(resource->sync_point); | 192 glWaitSyncPointCHROMIUM(resource->sync_point); |
| 182 uint32_t texture_id = resource_to_texture_id_map_[resource->id]; | 193 uint32_t texture_id = resource_to_texture_id_map_[resource->id]; |
| 183 DCHECK_NE(0u, texture_id); | 194 DCHECK_NE(0u, texture_id); |
| 184 resource_to_texture_id_map_.erase(resource->id); | 195 resource_to_texture_id_map_.erase(resource->id); |
| 185 glDeleteTextures(1, &texture_id); | 196 glDeleteTextures(1, &texture_id); |
| 186 } | 197 } |
| 187 } | 198 } |
| 188 | 199 |
| 200 void BitmapUploader::OnSurfaceConnectionCreated(SurfacePtr surface, | |
| 201 uint32_t id_namespace) { | |
| 202 surface_ = surface.Pass(); | |
| 203 surface_.set_client(this); | |
| 204 id_allocator_.reset(new cc::SurfaceIdAllocator(id_namespace)); | |
| 205 if (color_ != SK_ColorTRANSPARENT || !bitmap_.isNull()) | |
| 206 Upload(); | |
| 207 } | |
| 208 | |
| 209 uint32_t BitmapUploader::BindTextureForSize(const gfx::Size size) { | |
| 210 // TODO(jamesr): Recycle textures. | |
| 211 GLuint texture = 0u; | |
| 212 glGenTextures(1, &texture); | |
| 213 glBindTexture(GL_TEXTURE_2D, texture); | |
| 214 glTexImage2D(GL_TEXTURE_2D, | |
| 215 0, | |
| 216 TextureFormat(), | |
| 217 size.width(), | |
| 218 size.height(), | |
| 219 0, | |
| 220 TextureFormat(), | |
| 221 GL_UNSIGNED_BYTE, | |
| 222 0); | |
| 223 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); | |
| 224 return texture; | |
| 225 } | |
| 226 | |
| 189 } // namespace mojo | 227 } // namespace mojo |
| OLD | NEW |