Chromium Code Reviews| Index: mojo/services/public/cpp/view_manager/lib/bitmap_uploader.cc |
| diff --git a/mojo/services/public/cpp/view_manager/lib/bitmap_uploader.cc b/mojo/services/public/cpp/view_manager/lib/bitmap_uploader.cc |
| index 6761a30765389809a629fe9e39e912331f6f5442..1a7ec58bf1088582dbf3290f9ef44dece17144fc 100644 |
| --- a/mojo/services/public/cpp/view_manager/lib/bitmap_uploader.cc |
| +++ b/mojo/services/public/cpp/view_manager/lib/bitmap_uploader.cc |
| @@ -17,6 +17,7 @@ |
| #include "mojo/services/public/cpp/geometry/geometry_type_converters.h" |
| #include "mojo/services/public/cpp/surfaces/surfaces_type_converters.h" |
| #include "mojo/services/public/cpp/surfaces/surfaces_utils.h" |
| +#include "mojo/services/public/cpp/view_manager/lib/view_manager_client_impl.h" |
| #include "third_party/khronos/GLES2/gl2.h" |
| #include "ui/gfx/geometry/rect.h" |
| @@ -32,10 +33,15 @@ uint32_t TextureFormat() { |
| } |
| } |
| -BitmapUploader::BitmapUploader(SurfacesServicePtr surfaces_service, |
| +BitmapUploader::BitmapUploader(ViewManagerClientImpl* client, |
| + Id view_id, |
| + SurfacesServicePtr surfaces_service, |
| GpuPtr gpu_service) |
| - : surfaces_service_(surfaces_service.Pass()), |
| + : client_(client), |
| + view_id_(view_id), |
| + surfaces_service_(surfaces_service.Pass()), |
| gpu_service_(gpu_service.Pass()), |
| + color_(SK_ColorTRANSPARENT), |
| next_resource_id_(1u), |
| weak_factory_(this) { |
| surfaces_service_->CreateSurfaceConnection(base::Bind( |
| @@ -54,123 +60,128 @@ BitmapUploader::~BitmapUploader() { |
| MojoGLES2DestroyContext(gles2_context_); |
| } |
| -void BitmapUploader::OnSurfaceConnectionCreated(SurfacePtr surface, |
| - uint32_t id_namespace) { |
| - surface_ = surface.Pass(); |
| - surface_.set_client(this); |
| - id_allocator_.reset(new cc::SurfaceIdAllocator(id_namespace)); |
| - if (!bitmap_.isNull()) { |
| - Upload(bitmap_, done_callback_); |
| - bitmap_.reset(); |
| - done_callback_.Reset(); |
| - } |
| +void BitmapUploader::SetSize(const gfx::Size& size) { |
| + if (size_ == size) |
| + return; |
| + size_ = size; |
| } |
| -uint32_t BitmapUploader::BindTextureForSize(const gfx::Size size) { |
| - // TODO(jamesr): Recycle textures. |
| - GLuint texture = 0u; |
| - glGenTextures(1, &texture); |
| - glBindTexture(GL_TEXTURE_2D, texture); |
| - glTexImage2D(GL_TEXTURE_2D, |
| - 0, |
| - TextureFormat(), |
| - size.width(), |
| - size.height(), |
| - 0, |
| - TextureFormat(), |
| - GL_UNSIGNED_BYTE, |
| - 0); |
| - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| - return texture; |
| +void BitmapUploader::SetColor(SkColor color) { |
| + if (color_ == color) |
| + return; |
| + color_ = color; |
| + if (surface_) |
| + Upload(); |
| +} |
| + |
| +void BitmapUploader::SetBitmap(SkBitmap bitmap) { |
| + bitmap_ = bitmap; |
| + if (surface_) |
| + Upload(); |
| } |
| -void BitmapUploader::Upload( |
| - SkBitmap bitmap, |
| - const base::Callback<void(SurfaceIdPtr)>& done_callback) { |
| - if (bitmap.width() == 0 || bitmap.height() == 0) { |
| - done_callback.Run(SurfaceId::New()); |
| +void BitmapUploader::Upload() { |
| + if (size_.width() == 0 || size_.height() == 0) { |
| + client_->SetSurfaceId(view_id_, SurfaceId::New()); |
| return; |
| } |
| if (!surface_) { // Can't upload yet, store for later. |
| - bitmap_ = bitmap; |
| - done_callback_ = done_callback; |
| + done_callback_ = done_callback_; |
| return; |
| } |
| - gfx::Size bitmap_size(bitmap.width(), bitmap.height()); |
| - if (id_.is_null() || bitmap_size != surface_size_) { |
| + if (id_.is_null() || size_ != surface_size_) { |
| if (!id_.is_null()) |
| surface_->DestroySurface(SurfaceId::From(id_)); |
| id_ = id_allocator_->GenerateId(); |
| - surface_->CreateSurface(SurfaceId::From(id_), Size::From(bitmap_size)); |
| - surface_size_ = bitmap_size; |
| + surface_->CreateSurface(SurfaceId::From(id_), Size::From(size_)); |
| + client_->SetSurfaceId(view_id_, SurfaceId::From(id_)); |
| + surface_size_ = size_; |
| } |
| - GLuint texture_id = BindTextureForSize(bitmap_size); |
| - bitmap.lockPixels(); |
| - glTexSubImage2D(GL_TEXTURE_2D, |
| - 0, |
| - 0, |
| - 0, |
| - bitmap_size.width(), |
| - bitmap_size.height(), |
| - TextureFormat(), |
| - GL_UNSIGNED_BYTE, |
| - bitmap.getPixels()); |
| - bitmap.unlockPixels(); |
| - |
| - gpu::Mailbox mailbox = gpu::Mailbox::Generate(); |
| - glProduceTextureCHROMIUM(GL_TEXTURE_2D, mailbox.name); |
| - GLuint sync_point = glInsertSyncPointCHROMIUM(); |
| - |
| - TransferableResourcePtr resource = TransferableResource::New(); |
| - resource->id = next_resource_id_++; |
| - resource_to_texture_id_map_[resource->id] = texture_id; |
| - resource->format = mojo::RESOURCE_FORMAT_RGBA_8888; |
| - resource->filter = GL_LINEAR; |
| - resource->size = Size::From(bitmap_size); |
| - MailboxHolderPtr mailbox_holder = MailboxHolder::New(); |
| - mailbox_holder->mailbox = Mailbox::From(mailbox); |
| - mailbox_holder->texture_target = GL_TEXTURE_2D; |
| - mailbox_holder->sync_point = sync_point; |
| - resource->mailbox_holder = mailbox_holder.Pass(); |
| - resource->is_repeated = false; |
| - resource->is_software = false; |
| - |
| - gfx::Rect bitmap_rect(bitmap_size); |
| - |
| - QuadPtr quad = Quad::New(); |
| - quad->material = MATERIAL_TEXTURE_CONTENT; |
| - quad->rect = Rect::From(bitmap_rect); |
| - quad->opaque_rect = Rect::From(bitmap_rect); |
| - quad->visible_rect = Rect::From(bitmap_rect); |
| - quad->needs_blending = true; |
| - quad->shared_quad_state_index = 0u; |
| - |
| - TextureQuadStatePtr texture_state = TextureQuadState::New(); |
| - texture_state->resource_id = resource->id; |
| - texture_state->premultiplied_alpha = true; |
| - texture_state->uv_top_left = PointF::From(gfx::PointF(0.f, 0.f)); |
| - texture_state->uv_bottom_right = PointF::From(gfx::PointF(1.f, 1.f)); |
| - texture_state->background_color = Color::From(SK_ColorRED); |
| - for (int i = 0; i < 4; ++i) |
| - texture_state->vertex_opacity.push_back(1.f); |
| - texture_state->flipped = false; |
| - |
| - quad->texture_quad_state = texture_state.Pass(); |
| - |
| - SharedQuadStatePtr sqs = CreateDefaultSQS(bitmap_size); |
| - |
| - PassPtr pass = CreateDefaultPass(1, bitmap_rect); |
| - |
| - pass->quads.push_back(quad.Pass()); |
| - pass->shared_quad_states.push_back(sqs.Pass()); |
| - |
| + gfx::Rect bounds(size_); |
| + PassPtr pass = CreateDefaultPass(1, bounds); |
| FramePtr frame = Frame::New(); |
| - frame->resources.push_back(resource.Pass()); |
| + frame->resources.resize(0u); |
| + |
| + pass->quads.resize(0u); |
| + pass->shared_quad_states.push_back(CreateDefaultSQS(size_)); |
| + |
| + if (!bitmap_.isNull()) { |
| + gfx::Size bitmap_size(bitmap_.width(), bitmap_.height()); |
| + GLuint texture_id = BindTextureForSize(bitmap_size); |
| + bitmap_.lockPixels(); |
| + glTexSubImage2D(GL_TEXTURE_2D, |
| + 0, |
| + 0, |
| + 0, |
| + bitmap_size.width(), |
| + bitmap_size.height(), |
| + TextureFormat(), |
| + GL_UNSIGNED_BYTE, |
| + bitmap_.getPixels()); |
| + bitmap_.unlockPixels(); |
| + |
| + gpu::Mailbox mailbox = gpu::Mailbox::Generate(); |
| + glProduceTextureCHROMIUM(GL_TEXTURE_2D, mailbox.name); |
| + GLuint sync_point = glInsertSyncPointCHROMIUM(); |
| + |
| + TransferableResourcePtr resource = TransferableResource::New(); |
| + resource->id = next_resource_id_++; |
| + resource_to_texture_id_map_[resource->id] = texture_id; |
| + resource->format = mojo::RESOURCE_FORMAT_RGBA_8888; |
| + resource->filter = GL_LINEAR; |
| + resource->size = Size::From(bitmap_size); |
| + MailboxHolderPtr mailbox_holder = MailboxHolder::New(); |
| + mailbox_holder->mailbox = Mailbox::From(mailbox); |
| + mailbox_holder->texture_target = GL_TEXTURE_2D; |
| + mailbox_holder->sync_point = sync_point; |
| + resource->mailbox_holder = mailbox_holder.Pass(); |
| + resource->is_repeated = false; |
| + resource->is_software = false; |
| + |
| + QuadPtr quad = Quad::New(); |
| + quad->material = MATERIAL_TEXTURE_CONTENT; |
| + quad->rect = Rect::From(bounds); |
| + quad->opaque_rect = Rect::From(bounds); |
| + quad->visible_rect = Rect::From(bounds); |
| + quad->needs_blending = true; |
| + quad->shared_quad_state_index = 0u; |
| + |
| + TextureQuadStatePtr texture_state = TextureQuadState::New(); |
| + texture_state->resource_id = resource->id; |
| + texture_state->premultiplied_alpha = true; |
| + texture_state->uv_top_left = PointF::From(gfx::PointF(0.f, 0.f)); |
| + texture_state->uv_bottom_right = PointF::From(gfx::PointF(1.f, 1.f)); |
| + texture_state->background_color = Color::From(SkColor(SK_ColorTRANSPARENT)); |
| + for (int i = 0; i < 4; ++i) |
| + 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
|
| + texture_state->flipped = false; |
| + |
| + frame->resources.push_back(resource.Pass()); |
| + quad->texture_quad_state = texture_state.Pass(); |
| + pass->quads.push_back(quad.Pass()); |
| + } |
| + |
| + if (color_ != SK_ColorTRANSPARENT) { |
| + QuadPtr quad = Quad::New(); |
| + quad->material = MATERIAL_SOLID_COLOR; |
| + quad->rect = Rect::From(bounds); |
| + quad->opaque_rect = Rect::From(gfx::Rect()); |
| + quad->visible_rect = Rect::From(bounds); |
| + quad->needs_blending = true; |
| + quad->shared_quad_state_index = 0u; |
| + |
| + SolidColorQuadStatePtr color_state = SolidColorQuadState::New(); |
| + color_state->color = Color::From(SkColorSetA(color_, 0x7f)); |
| + color_state->force_anti_aliasing_off = false; |
| + |
| + quad->solid_color_quad_state = color_state.Pass(); |
| + pass->quads.push_back(quad.Pass()); |
| + } |
| + |
| frame->passes.push_back(pass.Pass()); |
| surface_->SubmitFrame(SurfaceId::From(id_), frame.Pass()); |
| - done_callback.Run(SurfaceId::From(id_)); |
| } |
| void BitmapUploader::ReturnResources(Array<ReturnedResourcePtr> resources) { |
| @@ -186,4 +197,31 @@ void BitmapUploader::ReturnResources(Array<ReturnedResourcePtr> resources) { |
| } |
| } |
| +void BitmapUploader::OnSurfaceConnectionCreated(SurfacePtr surface, |
| + uint32_t id_namespace) { |
| + surface_ = surface.Pass(); |
| + surface_.set_client(this); |
| + id_allocator_.reset(new cc::SurfaceIdAllocator(id_namespace)); |
| + if (color_ != SK_ColorTRANSPARENT || !bitmap_.isNull()) |
| + Upload(); |
| +} |
| + |
| +uint32_t BitmapUploader::BindTextureForSize(const gfx::Size size) { |
| + // TODO(jamesr): Recycle textures. |
| + GLuint texture = 0u; |
| + glGenTextures(1, &texture); |
| + glBindTexture(GL_TEXTURE_2D, texture); |
| + glTexImage2D(GL_TEXTURE_2D, |
| + 0, |
| + TextureFormat(), |
| + size.width(), |
| + size.height(), |
| + 0, |
| + TextureFormat(), |
| + GL_UNSIGNED_BYTE, |
| + 0); |
| + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| + return texture; |
| +} |
| + |
| } // namespace mojo |