| 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/ganesh_app/texture_uploader.h" | |
| 6 | |
| 7 #ifndef GL_GLEXT_PROTOTYPES | |
| 8 #define GL_GLEXT_PROTOTYPES | |
| 9 #endif | |
| 10 | |
| 11 #include <GLES2/gl2.h> | |
| 12 #include <GLES2/gl2extmojo.h> | |
| 13 | |
| 14 #include "base/bind.h" | |
| 15 #include "base/trace_event/trace_event.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 | |
| 21 namespace examples { | |
| 22 | |
| 23 TextureUploader::Client::~Client() {} | |
| 24 | |
| 25 TextureUploader::TextureUploader(Client* client, | |
| 26 mojo::Shell* shell, | |
| 27 base::WeakPtr<mojo::GLContext> context) | |
| 28 : client_(client), | |
| 29 context_(context), | |
| 30 next_resource_id_(0u), | |
| 31 id_namespace_(0u), | |
| 32 local_id_(0u), | |
| 33 returner_binding_(this) { | |
| 34 TRACE_EVENT0("ganesh_app", __func__); | |
| 35 context_->AddObserver(this); | |
| 36 | |
| 37 mojo::ServiceProviderPtr surfaces_service_provider; | |
| 38 shell->ConnectToApplication("mojo:surfaces_service", | |
| 39 mojo::GetProxy(&surfaces_service_provider), | |
| 40 nullptr); | |
| 41 mojo::ConnectToService(surfaces_service_provider.get(), &surface_); | |
| 42 surface_->GetIdNamespace( | |
| 43 base::Bind(&TextureUploader::SetIdNamespace, base::Unretained(this))); | |
| 44 mojo::ResourceReturnerPtr returner_ptr; | |
| 45 returner_binding_.Bind(GetProxy(&returner_ptr)); | |
| 46 surface_->SetResourceReturner(returner_ptr.Pass()); | |
| 47 } | |
| 48 | |
| 49 TextureUploader::~TextureUploader() { | |
| 50 if (context_.get()) | |
| 51 context_->RemoveObserver(this); | |
| 52 } | |
| 53 | |
| 54 void TextureUploader::Upload(scoped_ptr<mojo::GLTexture> texture) { | |
| 55 TRACE_EVENT0("ganesh_app", __func__); | |
| 56 mojo::Size size = texture->size(); | |
| 57 EnsureSurfaceForSize(size); | |
| 58 | |
| 59 mojo::FramePtr frame = mojo::Frame::New(); | |
| 60 frame->resources.resize(0u); | |
| 61 | |
| 62 mojo::Rect bounds; | |
| 63 bounds.width = size.width; | |
| 64 bounds.height = size.height; | |
| 65 mojo::PassPtr pass = mojo::CreateDefaultPass(1, bounds); | |
| 66 pass->quads.resize(0u); | |
| 67 pass->shared_quad_states.push_back(mojo::CreateDefaultSQS(size)); | |
| 68 | |
| 69 context_->MakeCurrent(); | |
| 70 glBindTexture(GL_TEXTURE_2D, texture->texture_id()); | |
| 71 GLbyte mailbox[GL_MAILBOX_SIZE_CHROMIUM]; | |
| 72 glGenMailboxCHROMIUM(mailbox); | |
| 73 glProduceTextureCHROMIUM(GL_TEXTURE_2D, mailbox); | |
| 74 GLuint sync_point = glInsertSyncPointCHROMIUM(); | |
| 75 | |
| 76 mojo::TransferableResourcePtr resource = mojo::TransferableResource::New(); | |
| 77 resource->id = next_resource_id_++; | |
| 78 resource_to_texture_map_[resource->id] = texture.release(); | |
| 79 resource->format = mojo::ResourceFormat::RGBA_8888; | |
| 80 resource->filter = GL_LINEAR; | |
| 81 resource->size = size.Clone(); | |
| 82 mojo::MailboxHolderPtr mailbox_holder = mojo::MailboxHolder::New(); | |
| 83 mailbox_holder->mailbox = mojo::Mailbox::New(); | |
| 84 for (int i = 0; i < GL_MAILBOX_SIZE_CHROMIUM; ++i) | |
| 85 mailbox_holder->mailbox->name.push_back(mailbox[i]); | |
| 86 mailbox_holder->texture_target = GL_TEXTURE_2D; | |
| 87 mailbox_holder->sync_point = sync_point; | |
| 88 resource->mailbox_holder = mailbox_holder.Pass(); | |
| 89 resource->is_repeated = false; | |
| 90 resource->is_software = false; | |
| 91 | |
| 92 mojo::QuadPtr quad = mojo::Quad::New(); | |
| 93 quad->material = mojo::Material::TEXTURE_CONTENT; | |
| 94 | |
| 95 mojo::RectPtr rect = mojo::Rect::New(); | |
| 96 rect->width = size.width; | |
| 97 rect->height = size.height; | |
| 98 quad->rect = rect.Clone(); | |
| 99 quad->opaque_rect = rect.Clone(); | |
| 100 quad->visible_rect = rect.Clone(); | |
| 101 quad->needs_blending = true; | |
| 102 quad->shared_quad_state_index = 0u; | |
| 103 | |
| 104 mojo::TextureQuadStatePtr texture_state = mojo::TextureQuadState::New(); | |
| 105 texture_state->resource_id = resource->id; | |
| 106 texture_state->premultiplied_alpha = true; | |
| 107 texture_state->uv_top_left = mojo::PointF::New(); | |
| 108 texture_state->uv_bottom_right = mojo::PointF::New(); | |
| 109 texture_state->uv_bottom_right->x = 1.f; | |
| 110 texture_state->uv_bottom_right->y = 1.f; | |
| 111 texture_state->background_color = mojo::Color::New(); | |
| 112 texture_state->background_color->rgba = 0; | |
| 113 for (int i = 0; i < 4; ++i) | |
| 114 texture_state->vertex_opacity.push_back(1.f); | |
| 115 texture_state->flipped = false; | |
| 116 | |
| 117 frame->resources.push_back(resource.Pass()); | |
| 118 quad->texture_quad_state = texture_state.Pass(); | |
| 119 pass->quads.push_back(quad.Pass()); | |
| 120 | |
| 121 frame->passes.push_back(pass.Pass()); | |
| 122 surface_->SubmitFrame(local_id_, frame.Pass(), mojo::Closure()); | |
| 123 } | |
| 124 | |
| 125 void TextureUploader::OnContextLost() { | |
| 126 LOG(FATAL) << "Context lost."; | |
| 127 } | |
| 128 | |
| 129 void TextureUploader::ReturnResources( | |
| 130 mojo::Array<mojo::ReturnedResourcePtr> resources) { | |
| 131 TRACE_EVENT0("ganesh_app", __func__); | |
| 132 context_->MakeCurrent(); | |
| 133 for (size_t i = 0u; i < resources.size(); ++i) { | |
| 134 mojo::ReturnedResourcePtr resource = resources[i].Pass(); | |
| 135 DCHECK_EQ(1, resource->count); | |
| 136 glWaitSyncPointCHROMIUM(resource->sync_point); | |
| 137 mojo::GLTexture* texture = resource_to_texture_map_[resource->id]; | |
| 138 DCHECK_NE(0u, texture->texture_id()); | |
| 139 resource_to_texture_map_.erase(resource->id); | |
| 140 delete texture; | |
| 141 } | |
| 142 } | |
| 143 | |
| 144 void TextureUploader::EnsureSurfaceForSize(const mojo::Size& size) { | |
| 145 TRACE_EVENT0("ganesh_app", __func__); | |
| 146 if (local_id_ != 0u && size == surface_size_) | |
| 147 return; | |
| 148 | |
| 149 if (local_id_ != 0u) { | |
| 150 surface_->DestroySurface(local_id_); | |
| 151 } | |
| 152 | |
| 153 local_id_++; | |
| 154 surface_->CreateSurface(local_id_); | |
| 155 surface_size_ = size; | |
| 156 if (id_namespace_ != 0u) | |
| 157 SendFullyQualifiedID(); | |
| 158 } | |
| 159 void TextureUploader::SendFullyQualifiedID() { | |
| 160 auto qualified_id = mojo::SurfaceId::New(); | |
| 161 qualified_id->id_namespace = id_namespace_; | |
| 162 qualified_id->local = local_id_; | |
| 163 client_->OnSurfaceIdAvailable(qualified_id.Pass()); | |
| 164 } | |
| 165 | |
| 166 void TextureUploader::SetIdNamespace(uint32_t id_namespace) { | |
| 167 id_namespace_ = id_namespace; | |
| 168 if (local_id_ != 0u) | |
| 169 SendFullyQualifiedID(); | |
| 170 } | |
| 171 | |
| 172 } // namespace examples | |
| OLD | NEW |