| 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/examples/surfaces_app/child_gl_impl.h" | |
| 6 | |
| 7 #ifndef GL_GLEXT_PROTOTYPES | |
| 8 #define GL_GLEXT_PROTOTYPES | |
| 9 #endif | |
| 10 | |
| 11 #include "base/bind.h" | |
| 12 #include "base/message_loop/message_loop.h" | |
| 13 #include "cc/output/compositor_frame.h" | |
| 14 #include "cc/output/delegated_frame_data.h" | |
| 15 #include "cc/quads/render_pass.h" | |
| 16 #include "cc/quads/texture_draw_quad.h" | |
| 17 #include "gpu/GLES2/gl2chromium.h" | |
| 18 #include "gpu/GLES2/gl2extchromium.h" | |
| 19 #include "mojo/examples/surfaces_app/surfaces_util.h" | |
| 20 #include "mojo/public/cpp/application/application_connection.h" | |
| 21 #include "mojo/public/cpp/environment/environment.h" | |
| 22 #include "mojo/services/public/cpp/geometry/geometry_type_converters.h" | |
| 23 #include "mojo/services/public/cpp/surfaces/surfaces_type_converters.h" | |
| 24 #include "mojo/services/public/interfaces/surfaces/surface_id.mojom.h" | |
| 25 #include "mojo/services/public/interfaces/surfaces/surfaces.mojom.h" | |
| 26 #include "third_party/khronos/GLES2/gl2.h" | |
| 27 #include "third_party/khronos/GLES2/gl2ext.h" | |
| 28 #include "ui/gfx/rect.h" | |
| 29 #include "ui/gfx/transform.h" | |
| 30 | |
| 31 namespace mojo { | |
| 32 namespace examples { | |
| 33 | |
| 34 using cc::RenderPass; | |
| 35 using cc::DrawQuad; | |
| 36 using cc::TextureDrawQuad; | |
| 37 using cc::DelegatedFrameData; | |
| 38 using cc::CompositorFrame; | |
| 39 | |
| 40 static void ContextLostThunk(void*) { | |
| 41 LOG(FATAL) << "Context lost"; | |
| 42 } | |
| 43 | |
| 44 ChildGLImpl::ChildGLImpl(ApplicationConnection* surfaces_service_connection, | |
| 45 CommandBufferPtr command_buffer) | |
| 46 : start_time_(base::TimeTicks::Now()), next_resource_id_(1) { | |
| 47 surfaces_service_connection->ConnectToService(&surface_); | |
| 48 surface_.set_client(this); | |
| 49 context_ = | |
| 50 MojoGLES2CreateContext(command_buffer.PassMessagePipe().release().value(), | |
| 51 &ContextLostThunk, | |
| 52 this, | |
| 53 Environment::GetDefaultAsyncWaiter()); | |
| 54 DCHECK(context_); | |
| 55 MojoGLES2MakeCurrent(context_); | |
| 56 } | |
| 57 | |
| 58 ChildGLImpl::~ChildGLImpl() { | |
| 59 MojoGLES2DestroyContext(context_); | |
| 60 surface_->DestroySurface(mojo::SurfaceId::From(id_)); | |
| 61 } | |
| 62 | |
| 63 void ChildGLImpl::ProduceFrame( | |
| 64 ColorPtr color, | |
| 65 SizePtr size, | |
| 66 const mojo::Callback<void(SurfaceIdPtr id)>& callback) { | |
| 67 color_ = color.To<SkColor>(); | |
| 68 size_ = size.To<gfx::Size>(); | |
| 69 cube_.Init(size_.width(), size_.height()); | |
| 70 cube_.set_color( | |
| 71 SkColorGetR(color_), SkColorGetG(color_), SkColorGetB(color_)); | |
| 72 produce_callback_ = callback; | |
| 73 if (allocator_) | |
| 74 AllocateSurface(); | |
| 75 } | |
| 76 | |
| 77 void ChildGLImpl::SetIdNamespace(uint32_t id_namespace) { | |
| 78 allocator_.reset(new cc::SurfaceIdAllocator(id_namespace)); | |
| 79 if (!produce_callback_.is_null()) | |
| 80 AllocateSurface(); | |
| 81 } | |
| 82 | |
| 83 void ChildGLImpl::ReturnResources(Array<ReturnedResourcePtr> resources) { | |
| 84 for (size_t i = 0; i < resources.size(); ++i) { | |
| 85 cc::ReturnedResource res = resources[i].To<cc::ReturnedResource>(); | |
| 86 GLuint returned_texture = id_to_tex_map_[res.id]; | |
| 87 glDeleteTextures(1, &returned_texture); | |
| 88 } | |
| 89 } | |
| 90 | |
| 91 void ChildGLImpl::AllocateSurface() { | |
| 92 if (produce_callback_.is_null() || !allocator_) | |
| 93 return; | |
| 94 | |
| 95 id_ = allocator_->GenerateId(); | |
| 96 surface_->CreateSurface(mojo::SurfaceId::From(id_), mojo::Size::From(size_)); | |
| 97 produce_callback_.Run(SurfaceId::From(id_)); | |
| 98 Draw(); | |
| 99 } | |
| 100 | |
| 101 void ChildGLImpl::Draw() { | |
| 102 // First, generate a GL texture and draw the cube into it. | |
| 103 GLuint texture = 0u; | |
| 104 glGenTextures(1, &texture); | |
| 105 glBindTexture(GL_TEXTURE_2D, texture); | |
| 106 glTexImage2D(GL_TEXTURE_2D, | |
| 107 0, | |
| 108 GL_RGBA, | |
| 109 size_.width(), | |
| 110 size_.height(), | |
| 111 0, | |
| 112 GL_RGBA, | |
| 113 GL_UNSIGNED_BYTE, | |
| 114 0); | |
| 115 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); | |
| 116 GLuint fbo = 0u; | |
| 117 glGenFramebuffers(1, &fbo); | |
| 118 glBindFramebuffer(GL_FRAMEBUFFER, fbo); | |
| 119 glFramebufferTexture2D( | |
| 120 GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture, 0); | |
| 121 DCHECK_EQ(static_cast<GLenum>(GL_FRAMEBUFFER_COMPLETE), | |
| 122 glCheckFramebufferStatus(GL_FRAMEBUFFER)); | |
| 123 glClearColor(1, 0, 0, 0.5); | |
| 124 cube_.UpdateForTimeDelta(0.16f); | |
| 125 cube_.Draw(); | |
| 126 | |
| 127 // Then, put the texture into a mailbox. | |
| 128 gpu::Mailbox mailbox = gpu::Mailbox::Generate(); | |
| 129 glProduceTextureCHROMIUM(GL_TEXTURE_2D, mailbox.name); | |
| 130 GLuint sync_point = glInsertSyncPointCHROMIUM(); | |
| 131 gpu::MailboxHolder holder(mailbox, GL_TEXTURE_2D, sync_point); | |
| 132 | |
| 133 // Then, put the mailbox into a TransferableResource | |
| 134 cc::TransferableResource resource; | |
| 135 resource.id = next_resource_id_++; | |
| 136 id_to_tex_map_[resource.id] = texture; | |
| 137 resource.format = cc::RGBA_8888; | |
| 138 resource.filter = GL_LINEAR; | |
| 139 resource.size = size_; | |
| 140 resource.mailbox_holder = holder; | |
| 141 resource.is_repeated = false; | |
| 142 resource.is_software = false; | |
| 143 | |
| 144 gfx::Rect rect(size_); | |
| 145 RenderPass::Id id(1, 1); | |
| 146 scoped_ptr<RenderPass> pass = RenderPass::Create(); | |
| 147 pass->SetNew(id, rect, rect, gfx::Transform()); | |
| 148 | |
| 149 CreateAndAppendSimpleSharedQuadState(pass.get(), gfx::Transform(), size_); | |
| 150 | |
| 151 TextureDrawQuad* texture_quad = | |
| 152 pass->CreateAndAppendDrawQuad<TextureDrawQuad>(); | |
| 153 float vertex_opacity[4] = {1.0f, 1.0f, 0.2f, 1.0f}; | |
| 154 texture_quad->SetNew(pass->shared_quad_state_list.back(), | |
| 155 rect, | |
| 156 rect, | |
| 157 rect, | |
| 158 resource.id, | |
| 159 true, | |
| 160 gfx::PointF(), | |
| 161 gfx::PointF(1.f, 1.f), | |
| 162 SK_ColorBLUE, | |
| 163 vertex_opacity, | |
| 164 false); | |
| 165 | |
| 166 scoped_ptr<DelegatedFrameData> delegated_frame_data(new DelegatedFrameData); | |
| 167 delegated_frame_data->render_pass_list.push_back(pass.Pass()); | |
| 168 delegated_frame_data->resource_list.push_back(resource); | |
| 169 | |
| 170 scoped_ptr<CompositorFrame> frame(new CompositorFrame); | |
| 171 frame->delegated_frame_data = delegated_frame_data.Pass(); | |
| 172 | |
| 173 surface_->SubmitFrame(mojo::SurfaceId::From(id_), mojo::Frame::From(*frame)); | |
| 174 | |
| 175 base::MessageLoop::current()->PostDelayedTask( | |
| 176 FROM_HERE, | |
| 177 base::Bind(&ChildGLImpl::Draw, base::Unretained(this)), | |
| 178 base::TimeDelta::FromMilliseconds(50)); | |
| 179 } | |
| 180 | |
| 181 } // namespace examples | |
| 182 } // namespace mojo | |
| OLD | NEW |