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