| 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 "examples/bitmap_uploader/bitmap_uploader.h" | |
| 6 | |
| 7 #ifndef GL_GLEXT_PROTOTYPES | |
| 8 #define GL_GLEXT_PROTOTYPES | |
| 9 #endif // GL_GLEXT_PROTOTYPES | |
| 10 | |
| 11 #include <GLES2/gl2.h> | |
| 12 #include <GLES2/gl2extmojo.h> | |
| 13 #include <MGL/mgl.h> | |
| 14 | |
| 15 #include "base/bind.h" | |
| 16 #include "mojo/public/cpp/application/connect.h" | |
| 17 #include "mojo/public/interfaces/application/shell.mojom.h" | |
| 18 #include "mojo/services/geometry/cpp/geometry_util.h" | |
| 19 #include "mojo/services/surfaces/cpp/surfaces_utils.h" | |
| 20 #include "mojo/services/view_manager/cpp/lib/view_manager_client_impl.h" | |
| 21 #include "ui/gfx/geometry/rect.h" | |
| 22 | |
| 23 #define TRANSPARENT_COLOR 0x00000000 | |
| 24 | |
| 25 namespace mojo { | |
| 26 | |
| 27 namespace { | |
| 28 void LostContext(void*) { | |
| 29 DCHECK(false); | |
| 30 } | |
| 31 | |
| 32 } | |
| 33 | |
| 34 BitmapUploader::BitmapUploader(View* view) | |
| 35 : view_(view), | |
| 36 color_(TRANSPARENT_COLOR), | |
| 37 width_(0), | |
| 38 height_(0), | |
| 39 format_(BGRA), | |
| 40 next_resource_id_(1u), | |
| 41 id_namespace_(0u), | |
| 42 local_id_(0u), | |
| 43 returner_binding_(this) { | |
| 44 } | |
| 45 | |
| 46 void BitmapUploader::Init(Shell* shell) { | |
| 47 ServiceProviderPtr surfaces_service_provider; | |
| 48 shell->ConnectToApplication("mojo:surfaces_service", | |
| 49 GetProxy(&surfaces_service_provider), nullptr); | |
| 50 ConnectToService(surfaces_service_provider.get(), &surface_); | |
| 51 surface_->GetIdNamespace( | |
| 52 base::Bind(&BitmapUploader::SetIdNamespace, base::Unretained(this))); | |
| 53 mojo::ResourceReturnerPtr returner_ptr; | |
| 54 returner_binding_.Bind(GetProxy(&returner_ptr)); | |
| 55 surface_->SetResourceReturner(returner_ptr.Pass()); | |
| 56 | |
| 57 ServiceProviderPtr gpu_service_provider; | |
| 58 shell->ConnectToApplication("mojo:native_viewport_service", | |
| 59 GetProxy(&gpu_service_provider), nullptr); | |
| 60 ConnectToService(gpu_service_provider.get(), &gpu_service_); | |
| 61 | |
| 62 CommandBufferPtr gles2_client; | |
| 63 gpu_service_->CreateOffscreenGLES2Context(GetProxy(&gles2_client)); | |
| 64 mgl_context_ = MGLCreateContext( | |
| 65 MGL_API_VERSION_GLES2, | |
| 66 gles2_client.PassInterface().PassHandle().release().value(), | |
| 67 MGL_NO_CONTEXT, &LostContext, nullptr, | |
| 68 Environment::GetDefaultAsyncWaiter()); | |
| 69 MGLMakeCurrent(mgl_context_); | |
| 70 } | |
| 71 | |
| 72 BitmapUploader::~BitmapUploader() { | |
| 73 MGLDestroyContext(mgl_context_); | |
| 74 } | |
| 75 | |
| 76 void BitmapUploader::SetColor(uint32_t color) { | |
| 77 if (color_ == color) | |
| 78 return; | |
| 79 color_ = color; | |
| 80 if (surface_) | |
| 81 Upload(); | |
| 82 } | |
| 83 | |
| 84 void BitmapUploader::SetBitmap(int width, | |
| 85 int height, | |
| 86 scoped_ptr<std::vector<unsigned char>> data, | |
| 87 Format format) { | |
| 88 width_ = width; | |
| 89 height_ = height; | |
| 90 bitmap_ = data.Pass(); | |
| 91 format_ = format; | |
| 92 if (surface_) | |
| 93 Upload(); | |
| 94 } | |
| 95 | |
| 96 void BitmapUploader::Upload() { | |
| 97 Size size; | |
| 98 size.width = view_->bounds().width; | |
| 99 size.height = view_->bounds().height; | |
| 100 if (!size.width || !size.height) { | |
| 101 view_->SetSurfaceId(SurfaceId::New()); | |
| 102 return; | |
| 103 } | |
| 104 if (id_namespace_ == 0u) // Can't generate a qualified ID yet. | |
| 105 return; | |
| 106 | |
| 107 if (size != surface_size_) { | |
| 108 if (local_id_ != 0u) { | |
| 109 surface_->DestroySurface(local_id_); | |
| 110 } | |
| 111 local_id_++; | |
| 112 surface_->CreateSurface(local_id_); | |
| 113 surface_size_ = size; | |
| 114 auto qualified_id = SurfaceId::New(); | |
| 115 qualified_id->id_namespace = id_namespace_; | |
| 116 qualified_id->local = local_id_; | |
| 117 view_->SetSurfaceId(qualified_id.Pass()); | |
| 118 } | |
| 119 | |
| 120 Rect bounds; | |
| 121 bounds.width = size.width; | |
| 122 bounds.height = size.height; | |
| 123 PassPtr pass = CreateDefaultPass(1, bounds); | |
| 124 FramePtr frame = Frame::New(); | |
| 125 frame->resources.resize(0u); | |
| 126 | |
| 127 pass->quads.resize(0u); | |
| 128 pass->shared_quad_states.push_back(CreateDefaultSQS(size)); | |
| 129 | |
| 130 MGLMakeCurrent(mgl_context_); | |
| 131 if (bitmap_.get()) { | |
| 132 Size bitmap_size; | |
| 133 bitmap_size.width = width_; | |
| 134 bitmap_size.height = height_; | |
| 135 GLuint texture_id = BindTextureForSize(bitmap_size); | |
| 136 glTexSubImage2D(GL_TEXTURE_2D, | |
| 137 0, | |
| 138 0, | |
| 139 0, | |
| 140 bitmap_size.width, | |
| 141 bitmap_size.height, | |
| 142 TextureFormat(), | |
| 143 GL_UNSIGNED_BYTE, | |
| 144 &((*bitmap_)[0])); | |
| 145 | |
| 146 GLbyte mailbox[GL_MAILBOX_SIZE_CHROMIUM]; | |
| 147 glGenMailboxCHROMIUM(mailbox); | |
| 148 glProduceTextureCHROMIUM(GL_TEXTURE_2D, mailbox); | |
| 149 GLuint sync_point = glInsertSyncPointCHROMIUM(); | |
| 150 | |
| 151 TransferableResourcePtr resource = TransferableResource::New(); | |
| 152 resource->id = next_resource_id_++; | |
| 153 resource_to_texture_id_map_[resource->id] = texture_id; | |
| 154 resource->format = mojo::ResourceFormat::RGBA_8888; | |
| 155 resource->filter = GL_LINEAR; | |
| 156 resource->size = bitmap_size.Clone(); | |
| 157 MailboxHolderPtr mailbox_holder = MailboxHolder::New(); | |
| 158 mailbox_holder->mailbox = Mailbox::New(); | |
| 159 for (int i = 0; i < GL_MAILBOX_SIZE_CHROMIUM; ++i) | |
| 160 mailbox_holder->mailbox->name.push_back(mailbox[i]); | |
| 161 mailbox_holder->texture_target = GL_TEXTURE_2D; | |
| 162 mailbox_holder->sync_point = sync_point; | |
| 163 resource->mailbox_holder = mailbox_holder.Pass(); | |
| 164 resource->is_repeated = false; | |
| 165 resource->is_software = false; | |
| 166 | |
| 167 QuadPtr quad = Quad::New(); | |
| 168 quad->material = Material::TEXTURE_CONTENT; | |
| 169 | |
| 170 RectPtr rect = Rect::New(); | |
| 171 if (width_ <= size.width && height_ <= size.height) { | |
| 172 rect->width = width_; | |
| 173 rect->height = height_; | |
| 174 } else { | |
| 175 // The source bitmap is larger than the viewport. Resize it while | |
| 176 // maintaining the aspect ratio. | |
| 177 float width_ratio = static_cast<float>(width_) / size.width; | |
| 178 float height_ratio = static_cast<float>(height_) / size.height; | |
| 179 if (width_ratio > height_ratio) { | |
| 180 rect->width = size.width; | |
| 181 rect->height = height_ / width_ratio; | |
| 182 } else { | |
| 183 rect->height = size.height; | |
| 184 rect->width = width_ / height_ratio; | |
| 185 } | |
| 186 } | |
| 187 quad->rect = rect.Clone(); | |
| 188 quad->opaque_rect = rect.Clone(); | |
| 189 quad->visible_rect = rect.Clone(); | |
| 190 quad->needs_blending = true; | |
| 191 quad->shared_quad_state_index = 0u; | |
| 192 | |
| 193 TextureQuadStatePtr texture_state = TextureQuadState::New(); | |
| 194 texture_state->resource_id = resource->id; | |
| 195 texture_state->premultiplied_alpha = true; | |
| 196 texture_state->uv_top_left = PointF::New(); | |
| 197 texture_state->uv_bottom_right = PointF::New(); | |
| 198 texture_state->uv_bottom_right->x = 1.f; | |
| 199 texture_state->uv_bottom_right->y = 1.f; | |
| 200 texture_state->background_color = Color::New(); | |
| 201 texture_state->background_color->rgba = TRANSPARENT_COLOR; | |
| 202 for (int i = 0; i < 4; ++i) | |
| 203 texture_state->vertex_opacity.push_back(1.f); | |
| 204 texture_state->flipped = false; | |
| 205 | |
| 206 frame->resources.push_back(resource.Pass()); | |
| 207 quad->texture_quad_state = texture_state.Pass(); | |
| 208 pass->quads.push_back(quad.Pass()); | |
| 209 } | |
| 210 | |
| 211 if (color_ != TRANSPARENT_COLOR) { | |
| 212 QuadPtr quad = Quad::New(); | |
| 213 quad->material = Material::SOLID_COLOR; | |
| 214 quad->rect = bounds.Clone(); | |
| 215 quad->opaque_rect = Rect::New(); | |
| 216 quad->visible_rect = bounds.Clone(); | |
| 217 quad->needs_blending = true; | |
| 218 quad->shared_quad_state_index = 0u; | |
| 219 | |
| 220 SolidColorQuadStatePtr color_state = SolidColorQuadState::New(); | |
| 221 color_state->color = Color::New(); | |
| 222 color_state->color->rgba = color_; | |
| 223 color_state->force_anti_aliasing_off = false; | |
| 224 | |
| 225 quad->solid_color_quad_state = color_state.Pass(); | |
| 226 pass->quads.push_back(quad.Pass()); | |
| 227 } | |
| 228 | |
| 229 frame->passes.push_back(pass.Pass()); | |
| 230 | |
| 231 surface_->SubmitFrame(local_id_, frame.Pass(), mojo::Closure()); | |
| 232 } | |
| 233 | |
| 234 void BitmapUploader::SetIdNamespace(uint32_t id_namespace) { | |
| 235 id_namespace_ = id_namespace; | |
| 236 if (color_ != TRANSPARENT_COLOR || bitmap_.get()) | |
| 237 Upload(); | |
| 238 } | |
| 239 | |
| 240 void BitmapUploader::ReturnResources(Array<ReturnedResourcePtr> resources) { | |
| 241 MGLMakeCurrent(mgl_context_); | |
| 242 // TODO(jamesr): Recycle. | |
| 243 for (size_t i = 0; i < resources.size(); ++i) { | |
| 244 ReturnedResourcePtr resource = resources[i].Pass(); | |
| 245 DCHECK_EQ(1, resource->count); | |
| 246 glWaitSyncPointCHROMIUM(resource->sync_point); | |
| 247 uint32_t texture_id = resource_to_texture_id_map_[resource->id]; | |
| 248 DCHECK_NE(0u, texture_id); | |
| 249 resource_to_texture_id_map_.erase(resource->id); | |
| 250 glDeleteTextures(1, &texture_id); | |
| 251 } | |
| 252 } | |
| 253 | |
| 254 uint32_t BitmapUploader::BindTextureForSize(const Size size) { | |
| 255 // TODO(jamesr): Recycle textures. | |
| 256 GLuint texture = 0u; | |
| 257 glGenTextures(1, &texture); | |
| 258 glBindTexture(GL_TEXTURE_2D, texture); | |
| 259 glTexImage2D(GL_TEXTURE_2D, | |
| 260 0, | |
| 261 TextureFormat(), | |
| 262 size.width, | |
| 263 size.height, | |
| 264 0, | |
| 265 TextureFormat(), | |
| 266 GL_UNSIGNED_BYTE, | |
| 267 0); | |
| 268 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); | |
| 269 return texture; | |
| 270 } | |
| 271 | |
| 272 uint32_t BitmapUploader::TextureFormat() { | |
| 273 return format_ == BGRA ? GL_BGRA_EXT : GL_RGBA; | |
| 274 } | |
| 275 | |
| 276 } // namespace mojo | |
| OLD | NEW |