OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "mojo/services/public/cpp/view_manager/lib/bitmap_uploader.h" |
| 6 |
| 7 #ifndef GL_GLEXT_PROTOTYPES |
| 8 #define GL_GLEXT_PROTOTYPES |
| 9 #endif // GL_GLEXT_PROTOTYPES |
| 10 |
| 11 #include "base/bind.h" |
| 12 #include "cc/surfaces/surface_id.h" |
| 13 #include "cc/surfaces/surface_id_allocator.h" |
| 14 #include "gpu/GLES2/gl2chromium.h" |
| 15 #include "gpu/GLES2/gl2extchromium.h" |
| 16 #include "gpu/command_buffer/common/mailbox.h" |
| 17 #include "mojo/services/public/cpp/geometry/geometry_type_converters.h" |
| 18 #include "mojo/services/public/cpp/surfaces/surfaces_type_converters.h" |
| 19 #include "mojo/services/public/cpp/surfaces/surfaces_utils.h" |
| 20 #include "third_party/khronos/GLES2/gl2.h" |
| 21 #include "ui/gfx/geometry/rect.h" |
| 22 |
| 23 namespace mojo { |
| 24 |
| 25 namespace { |
| 26 void LostContext(void*) { |
| 27 DCHECK(false); |
| 28 } |
| 29 |
| 30 uint32_t TextureFormat() { |
| 31 return SK_B32_SHIFT ? GL_RGBA : GL_BGRA_EXT; |
| 32 } |
| 33 } |
| 34 |
| 35 BitmapUploader::BitmapUploader(SurfacesServicePtr surfaces_service, |
| 36 GpuPtr gpu_service) |
| 37 : surfaces_service_(surfaces_service.Pass()), |
| 38 gpu_service_(gpu_service.Pass()), |
| 39 next_resource_id_(1u), |
| 40 weak_factory_(this) { |
| 41 surfaces_service_->CreateSurfaceConnection(base::Bind( |
| 42 &BitmapUploader::OnSurfaceConnectionCreated, weak_factory_.GetWeakPtr())); |
| 43 CommandBufferPtr gles2_client; |
| 44 gpu_service_->CreateOffscreenGLES2Context(Get(&gles2_client)); |
| 45 gles2_context_ = |
| 46 MojoGLES2CreateContext(gles2_client.PassMessagePipe().release().value(), |
| 47 &LostContext, |
| 48 NULL, |
| 49 Environment::GetDefaultAsyncWaiter()); |
| 50 MojoGLES2MakeCurrent(gles2_context_); |
| 51 } |
| 52 |
| 53 BitmapUploader::~BitmapUploader() { |
| 54 MojoGLES2DestroyContext(gles2_context_); |
| 55 } |
| 56 |
| 57 void BitmapUploader::OnSurfaceConnectionCreated(SurfacePtr surface, |
| 58 uint32_t id_namespace) { |
| 59 surface_ = surface.Pass(); |
| 60 surface_.set_client(this); |
| 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 } |
| 68 |
| 69 uint32_t BitmapUploader::BindTextureForSize(const gfx::Size size) { |
| 70 // TODO(jamesr): Recycle textures. |
| 71 GLuint texture = 0u; |
| 72 glGenTextures(1, &texture); |
| 73 glBindTexture(GL_TEXTURE_2D, texture); |
| 74 glTexImage2D(GL_TEXTURE_2D, |
| 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 } |
| 86 |
| 87 void BitmapUploader::Upload( |
| 88 SkBitmap bitmap, |
| 89 const base::Callback<void(SurfaceIdPtr)>& done_callback) { |
| 90 if (bitmap.width() == 0 || bitmap.height() == 0) { |
| 91 done_callback.Run(SurfaceId::New()); |
| 92 return; |
| 93 } |
| 94 if (!surface_) { // Can't upload yet, store for later. |
| 95 bitmap_ = bitmap; |
| 96 done_callback_ = done_callback; |
| 97 return; |
| 98 } |
| 99 gfx::Size bitmap_size(bitmap.width(), bitmap.height()); |
| 100 if (id_.is_null() || bitmap_size != surface_size_) { |
| 101 if (!id_.is_null()) |
| 102 surface_->DestroySurface(SurfaceId::From(id_)); |
| 103 id_ = id_allocator_->GenerateId(); |
| 104 surface_->CreateSurface(SurfaceId::From(id_), Size::From(bitmap_size)); |
| 105 surface_size_ = bitmap_size; |
| 106 } |
| 107 |
| 108 GLuint texture_id = BindTextureForSize(bitmap_size); |
| 109 bitmap.lockPixels(); |
| 110 glTexSubImage2D(GL_TEXTURE_2D, |
| 111 0, |
| 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 |
| 121 gpu::Mailbox mailbox = gpu::Mailbox::Generate(); |
| 122 glProduceTextureCHROMIUM(GL_TEXTURE_2D, mailbox.name); |
| 123 GLuint sync_point = glInsertSyncPointCHROMIUM(); |
| 124 |
| 125 TransferableResourcePtr resource = TransferableResource::New(); |
| 126 resource->id = next_resource_id_++; |
| 127 resource_to_texture_id_map_[resource->id] = texture_id; |
| 128 resource->format = mojo::RESOURCE_FORMAT_RGBA_8888; |
| 129 resource->filter = GL_LINEAR; |
| 130 resource->size = Size::From(bitmap_size); |
| 131 MailboxHolderPtr mailbox_holder = MailboxHolder::New(); |
| 132 mailbox_holder->mailbox = Mailbox::From(mailbox); |
| 133 mailbox_holder->texture_target = GL_TEXTURE_2D; |
| 134 mailbox_holder->sync_point = sync_point; |
| 135 resource->mailbox_holder = mailbox_holder.Pass(); |
| 136 resource->is_repeated = false; |
| 137 resource->is_software = false; |
| 138 |
| 139 gfx::Rect bitmap_rect(bitmap_size); |
| 140 |
| 141 QuadPtr quad = Quad::New(); |
| 142 quad->material = MATERIAL_TEXTURE_CONTENT; |
| 143 quad->rect = Rect::From(bitmap_rect); |
| 144 quad->opaque_rect = Rect::From(bitmap_rect); |
| 145 quad->visible_rect = Rect::From(bitmap_rect); |
| 146 quad->needs_blending = true; |
| 147 quad->shared_quad_state_index = 0u; |
| 148 |
| 149 TextureQuadStatePtr texture_state = TextureQuadState::New(); |
| 150 texture_state->resource_id = resource->id; |
| 151 texture_state->premultiplied_alpha = true; |
| 152 texture_state->uv_top_left = PointF::From(gfx::PointF(0.f, 0.f)); |
| 153 texture_state->uv_bottom_right = PointF::From(gfx::PointF(1.f, 1.f)); |
| 154 texture_state->background_color = Color::From(SK_ColorRED); |
| 155 for (int i = 0; i < 4; ++i) |
| 156 texture_state->vertex_opacity.push_back(1.f); |
| 157 texture_state->flipped = false; |
| 158 |
| 159 quad->texture_quad_state = texture_state.Pass(); |
| 160 |
| 161 SharedQuadStatePtr sqs = CreateDefaultSQS(bitmap_size); |
| 162 |
| 163 PassPtr pass = CreateDefaultPass(1, bitmap_rect); |
| 164 |
| 165 pass->quads.push_back(quad.Pass()); |
| 166 pass->shared_quad_states.push_back(sqs.Pass()); |
| 167 |
| 168 FramePtr frame = Frame::New(); |
| 169 frame->resources.push_back(resource.Pass()); |
| 170 frame->passes.push_back(pass.Pass()); |
| 171 |
| 172 surface_->SubmitFrame(SurfaceId::From(id_), frame.Pass()); |
| 173 done_callback.Run(SurfaceId::From(id_)); |
| 174 } |
| 175 |
| 176 void BitmapUploader::ReturnResources(Array<ReturnedResourcePtr> resources) { |
| 177 // TODO(jamesr): Recycle. |
| 178 for (size_t i = 0; i < resources.size(); ++i) { |
| 179 ReturnedResourcePtr resource = resources[i].Pass(); |
| 180 DCHECK_EQ(1, resource->count); |
| 181 glWaitSyncPointCHROMIUM(resource->sync_point); |
| 182 uint32_t texture_id = resource_to_texture_id_map_[resource->id]; |
| 183 DCHECK_NE(0u, texture_id); |
| 184 resource_to_texture_id_map_.erase(resource->id); |
| 185 glDeleteTextures(1, &texture_id); |
| 186 } |
| 187 } |
| 188 |
| 189 } // namespace mojo |
OLD | NEW |