OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "examples/surfaces_app/child_gl_impl.h" | 5 #include "examples/surfaces_app/child_gl_impl.h" |
6 | 6 |
7 #ifndef GL_GLEXT_PROTOTYPES | 7 #ifndef GL_GLEXT_PROTOTYPES |
8 #define GL_GLEXT_PROTOTYPES | 8 #define GL_GLEXT_PROTOTYPES |
9 #endif | 9 #endif |
10 | 10 |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
42 | 42 |
43 static void ContextLostThunk(void*) { | 43 static void ContextLostThunk(void*) { |
44 LOG(FATAL) << "Context lost"; | 44 LOG(FATAL) << "Context lost"; |
45 } | 45 } |
46 | 46 |
47 ChildGLImpl::ChildGLImpl(ApplicationConnection* surfaces_service_connection, | 47 ChildGLImpl::ChildGLImpl(ApplicationConnection* surfaces_service_connection, |
48 CommandBufferPtr command_buffer) | 48 CommandBufferPtr command_buffer) |
49 : start_time_(base::TimeTicks::Now()), | 49 : start_time_(base::TimeTicks::Now()), |
50 next_resource_id_(1), | 50 next_resource_id_(1), |
51 weak_factory_(this) { | 51 weak_factory_(this) { |
52 surfaces_service_connection->ConnectToService(&surfaces_service_); | 52 surfaces_service_connection->ConnectToService(&surface_); |
53 surfaces_service_->CreateSurfaceConnection(base::Bind( | 53 surface_.set_client(this); |
54 &ChildGLImpl::SurfaceConnectionCreated, weak_factory_.GetWeakPtr())); | 54 surface_.WaitForIncomingMethodCall(); // Wait for ID namespace to arrive. |
55 context_ = | 55 context_ = |
56 MojoGLES2CreateContext(command_buffer.PassMessagePipe().release().value(), | 56 MojoGLES2CreateContext(command_buffer.PassMessagePipe().release().value(), |
57 &ContextLostThunk, | 57 &ContextLostThunk, |
58 this, | 58 this, |
59 Environment::GetDefaultAsyncWaiter()); | 59 Environment::GetDefaultAsyncWaiter()); |
60 DCHECK(context_); | 60 DCHECK(context_); |
61 MojoGLES2MakeCurrent(context_); | 61 MojoGLES2MakeCurrent(context_); |
62 } | 62 } |
63 | 63 |
64 ChildGLImpl::~ChildGLImpl() { | 64 ChildGLImpl::~ChildGLImpl() { |
65 MojoGLES2DestroyContext(context_); | 65 MojoGLES2DestroyContext(context_); |
66 surface_->DestroySurface(mojo::SurfaceId::From(id_)); | 66 surface_->DestroySurface(mojo::SurfaceId::From(id_)); |
67 } | 67 } |
68 | 68 |
69 void ChildGLImpl::ProduceFrame( | 69 void ChildGLImpl::ProduceFrame( |
70 ColorPtr color, | 70 ColorPtr color, |
71 SizePtr size, | 71 SizePtr size, |
72 const mojo::Callback<void(SurfaceIdPtr id)>& callback) { | 72 const mojo::Callback<void(SurfaceIdPtr id)>& callback) { |
73 color_ = color.To<SkColor>(); | 73 color_ = color.To<SkColor>(); |
74 size_ = size.To<gfx::Size>(); | 74 size_ = size.To<gfx::Size>(); |
75 cube_.Init(size_.width(), size_.height()); | 75 cube_.Init(size_.width(), size_.height()); |
76 cube_.set_color( | 76 cube_.set_color( |
77 SkColorGetR(color_), SkColorGetG(color_), SkColorGetB(color_)); | 77 SkColorGetR(color_), SkColorGetG(color_), SkColorGetB(color_)); |
78 produce_callback_ = callback; | 78 id_ = allocator_->GenerateId(); |
79 AllocateSurface(); | 79 surface_->CreateSurface(mojo::SurfaceId::From(id_)); |
| 80 callback.Run(SurfaceId::From(id_)); |
| 81 Draw(); |
80 } | 82 } |
81 | 83 |
82 void ChildGLImpl::SurfaceConnectionCreated(SurfacePtr surface, | 84 void ChildGLImpl::SetIdNamespace(uint32_t id_namespace) { |
83 uint32_t id_namespace) { | |
84 surface_ = surface.Pass(); | |
85 surface_.set_client(this); | |
86 allocator_.reset(new cc::SurfaceIdAllocator(id_namespace)); | 85 allocator_.reset(new cc::SurfaceIdAllocator(id_namespace)); |
87 AllocateSurface(); | |
88 } | 86 } |
89 | 87 |
90 void ChildGLImpl::ReturnResources(Array<ReturnedResourcePtr> resources) { | 88 void ChildGLImpl::ReturnResources(Array<ReturnedResourcePtr> resources) { |
91 for (size_t i = 0; i < resources.size(); ++i) { | 89 for (size_t i = 0; i < resources.size(); ++i) { |
92 cc::ReturnedResource res = resources[i].To<cc::ReturnedResource>(); | 90 cc::ReturnedResource res = resources[i].To<cc::ReturnedResource>(); |
93 GLuint returned_texture = id_to_tex_map_[res.id]; | 91 GLuint returned_texture = id_to_tex_map_[res.id]; |
94 glDeleteTextures(1, &returned_texture); | 92 glDeleteTextures(1, &returned_texture); |
95 } | 93 } |
96 } | 94 } |
97 | 95 |
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() { | 96 void ChildGLImpl::Draw() { |
109 // First, generate a GL texture and draw the cube into it. | 97 // First, generate a GL texture and draw the cube into it. |
110 GLuint texture = 0u; | 98 GLuint texture = 0u; |
111 glGenTextures(1, &texture); | 99 glGenTextures(1, &texture); |
112 glBindTexture(GL_TEXTURE_2D, texture); | 100 glBindTexture(GL_TEXTURE_2D, texture); |
113 glTexImage2D(GL_TEXTURE_2D, | 101 glTexImage2D(GL_TEXTURE_2D, |
114 0, | 102 0, |
115 GL_RGBA, | 103 GL_RGBA, |
116 size_.width(), | 104 size_.width(), |
117 size_.height(), | 105 size_.height(), |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
185 mojo::Closure()); | 173 mojo::Closure()); |
186 | 174 |
187 base::MessageLoop::current()->PostDelayedTask( | 175 base::MessageLoop::current()->PostDelayedTask( |
188 FROM_HERE, | 176 FROM_HERE, |
189 base::Bind(&ChildGLImpl::Draw, base::Unretained(this)), | 177 base::Bind(&ChildGLImpl::Draw, base::Unretained(this)), |
190 base::TimeDelta::FromMilliseconds(50)); | 178 base::TimeDelta::FromMilliseconds(50)); |
191 } | 179 } |
192 | 180 |
193 } // namespace examples | 181 } // namespace examples |
194 } // namespace mojo | 182 } // namespace mojo |
OLD | NEW |