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