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