Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(309)

Side by Side Diff: examples/ganesh_app/painter.cc

Issue 728593003: Add a sample app that draws with Ganesh (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« examples/ganesh_app/painter.h ('K') | « examples/ganesh_app/painter.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/ganesh_app/painter.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
11 #include "base/bind.h" 11 #include "base/bind.h"
12 #include "base/logging.h"
12 #include "base/message_loop/message_loop.h" 13 #include "base/message_loop/message_loop.h"
13 #include "cc/output/compositor_frame.h" 14 #include "cc/output/compositor_frame.h"
14 #include "cc/output/delegated_frame_data.h" 15 #include "cc/output/delegated_frame_data.h"
15 #include "cc/quads/render_pass.h" 16 #include "cc/quads/render_pass.h"
16 #include "cc/quads/texture_draw_quad.h" 17 #include "cc/quads/texture_draw_quad.h"
17 #include "examples/surfaces_app/surfaces_util.h" 18 #include "examples/surfaces_app/surfaces_util.h"
18 #include "gpu/GLES2/gl2chromium.h" 19 #include "gpu/GLES2/gl2chromium.h"
19 #include "gpu/GLES2/gl2extchromium.h" 20 #include "gpu/GLES2/gl2extchromium.h"
21 #include "gpu/command_buffer/client/gles2_interface.h"
22 #include "gpu/command_buffer/client/gles2_lib.h"
20 #include "gpu/command_buffer/common/mailbox.h" 23 #include "gpu/command_buffer/common/mailbox.h"
21 #include "gpu/command_buffer/common/mailbox_holder.h" 24 #include "gpu/command_buffer/common/mailbox_holder.h"
25 #include "gpu/skia_bindings/gl_bindings_skia_cmd_buffer.h"
22 #include "mojo/converters/geometry/geometry_type_converters.h" 26 #include "mojo/converters/geometry/geometry_type_converters.h"
23 #include "mojo/converters/surfaces/surfaces_type_converters.h" 27 #include "mojo/converters/surfaces/surfaces_type_converters.h"
28 #include "mojo/public/c/gles2/gles2.h"
24 #include "mojo/public/cpp/application/application_connection.h" 29 #include "mojo/public/cpp/application/application_connection.h"
25 #include "mojo/public/cpp/environment/environment.h" 30 #include "mojo/public/cpp/environment/environment.h"
31 #include "mojo/public/cpp/utility/run_loop.h"
26 #include "mojo/services/public/interfaces/surfaces/surface_id.mojom.h" 32 #include "mojo/services/public/interfaces/surfaces/surface_id.mojom.h"
27 #include "mojo/services/public/interfaces/surfaces/surfaces.mojom.h" 33 #include "mojo/services/public/interfaces/surfaces/surfaces.mojom.h"
28 #include "third_party/khronos/GLES2/gl2.h" 34 #include "third_party/khronos/GLES2/gl2.h"
29 #include "third_party/khronos/GLES2/gl2ext.h" 35 #include "third_party/khronos/GLES2/gl2ext.h"
36 #include "third_party/skia/include/core/SkCanvas.h"
37 #include "third_party/skia/include/gpu/gl/GrGLInterface.h"
30 #include "ui/gfx/rect.h" 38 #include "ui/gfx/rect.h"
31 #include "ui/gfx/transform.h" 39 #include "ui/gfx/transform.h"
32 40
33 namespace mojo {
34 namespace examples {
35
36 using cc::RenderPass; 41 using cc::RenderPass;
37 using cc::RenderPassId; 42 using cc::RenderPassId;
38 using cc::DrawQuad; 43 using cc::DrawQuad;
39 using cc::TextureDrawQuad; 44 using cc::TextureDrawQuad;
40 using cc::DelegatedFrameData; 45 using cc::DelegatedFrameData;
41 using cc::CompositorFrame; 46 using cc::CompositorFrame;
42 47
48 namespace mojo {
49 namespace examples {
50 namespace {
51
52 // The limit of the number of GPU resources we hold in the GrContext's
53 // GPU cache.
54 const int kMaxGaneshResourceCacheCount = 2048;
55
56 // The limit of the bytes allocated toward GPU resources in the GrContext's
57 // GPU cache.
58 const size_t kMaxGaneshResourceCacheBytes = 96 * 1024 * 1024;
59
60 }
61
43 static void ContextLostThunk(void*) { 62 static void ContextLostThunk(void*) {
44 LOG(FATAL) << "Context lost"; 63 LOG(FATAL) << "Context lost";
45 } 64 }
46 65
47 ChildGLImpl::ChildGLImpl(ApplicationConnection* surfaces_service_connection, 66 Painter::Painter(ApplicationConnection* surfaces_service_connection,
48 CommandBufferPtr command_buffer) 67 CommandBufferPtr command_buffer)
49 : start_time_(base::TimeTicks::Now()), 68 : weak_factory_(this) {
50 next_resource_id_(1),
51 weak_factory_(this) {
52 surfaces_service_connection->ConnectToService(&surfaces_service_); 69 surfaces_service_connection->ConnectToService(&surfaces_service_);
53 surfaces_service_->CreateSurfaceConnection(base::Bind( 70 surfaces_service_->CreateSurfaceConnection(base::Bind(
54 &ChildGLImpl::SurfaceConnectionCreated, weak_factory_.GetWeakPtr())); 71 &Painter::SurfaceConnectionCreated, weak_factory_.GetWeakPtr()));
55 context_ = 72 context_ =
56 MojoGLES2CreateContext(command_buffer.PassMessagePipe().release().value(), 73 MojoGLES2CreateContext(command_buffer.PassMessagePipe().release().value(),
57 &ContextLostThunk, 74 &ContextLostThunk,
58 this, 75 this,
59 Environment::GetDefaultAsyncWaiter()); 76 mojo::Environment::GetDefaultAsyncWaiter());
60 DCHECK(context_); 77 DCHECK(context_);
61 MojoGLES2MakeCurrent(context_); 78 MojoGLES2MakeCurrent(context_);
79
80 gpu::gles2::GLES2Interface* gl = static_cast<gpu::gles2::GLES2Interface*>(
81 MojoGLES2GetGLES2Interface(context_));
82 gles2::SetGLContext(gl);
83
84 skia::RefPtr<GrGLInterface> interface = skia::AdoptRef(
85 skia_bindings::CreateCommandBufferSkiaGLBinding());
86 DCHECK(interface);
87
88 // // interface->fCallback
89 // // interface->fCallbackData
jamesr 2014/11/14 00:36:42 ?
90
91 gr_context_ = skia::AdoptRef(GrContext::Create(
92 kOpenGL_GrBackend,
93 reinterpret_cast<GrBackendContext>(interface.get())));
94 DCHECK(gr_context_);
95 gr_context_->setResourceCacheLimits(kMaxGaneshResourceCacheCount,
96 kMaxGaneshResourceCacheBytes);
62 } 97 }
63 98
64 ChildGLImpl::~ChildGLImpl() { 99 Painter::~Painter() {
65 MojoGLES2DestroyContext(context_); 100 MojoGLES2DestroyContext(context_);
66 surface_->DestroySurface(mojo::SurfaceId::From(id_)); 101 surface_->DestroySurface(mojo::SurfaceId::From(id_));
67 } 102 }
68 103
69 void ChildGLImpl::ProduceFrame( 104 void Painter::ProduceFrame(
70 ColorPtr color, 105 ColorPtr color,
71 SizePtr size, 106 SizePtr size,
72 const mojo::Callback<void(SurfaceIdPtr id)>& callback) { 107 const mojo::Callback<void(SurfaceIdPtr id)>& callback) {
73 color_ = color.To<SkColor>(); 108
74 size_ = size.To<gfx::Size>(); 109 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; 110 produce_callback_ = callback;
79 AllocateSurface(); 111 AllocateSurface();
80 } 112 }
81 113
82 void ChildGLImpl::SurfaceConnectionCreated(SurfacePtr surface, 114 void Painter::SurfaceConnectionCreated(SurfacePtr surface,
83 uint32_t id_namespace) { 115 uint32_t id_namespace) {
84 surface_ = surface.Pass(); 116 surface_ = surface.Pass();
85 surface_.set_client(this); 117 surface_.set_client(this);
86 allocator_.reset(new cc::SurfaceIdAllocator(id_namespace)); 118 allocator_.reset(new cc::SurfaceIdAllocator(id_namespace));
87 AllocateSurface(); 119 AllocateSurface();
88 } 120 }
89 121
90 void ChildGLImpl::ReturnResources(Array<ReturnedResourcePtr> resources) { 122 void Painter::ReturnResources(Array<ReturnedResourcePtr> resources) {
91 for (size_t i = 0; i < resources.size(); ++i) { 123 for (size_t i = 0; i < resources.size(); ++i) {
92 cc::ReturnedResource res = resources[i].To<cc::ReturnedResource>(); 124 cc::ReturnedResource res = resources[i].To<cc::ReturnedResource>();
93 GLuint returned_texture = id_to_tex_map_[res.id]; 125 GLuint returned_texture = id_to_tex_map_[res.id];
94 glDeleteTextures(1, &returned_texture); 126 glDeleteTextures(1, &returned_texture);
95 } 127 }
96 } 128 }
97 129
98 void ChildGLImpl::AllocateSurface() { 130 void Painter::AllocateSurface() {
99 if (produce_callback_.is_null() || !allocator_) 131 if (produce_callback_.is_null() || !allocator_)
100 return; 132 return;
101 133
102 id_ = allocator_->GenerateId(); 134 id_ = allocator_->GenerateId();
103 surface_->CreateSurface(mojo::SurfaceId::From(id_), mojo::Size::From(size_)); 135 surface_->CreateSurface(mojo::SurfaceId::From(id_), mojo::Size::From(size_));
104 produce_callback_.Run(SurfaceId::From(id_)); 136 produce_callback_.Run(SurfaceId::From(id_));
105 Draw(); 137 Draw();
106 } 138 }
107 139
108 void ChildGLImpl::Draw() { 140 void Painter::Draw() {
109 // First, generate a GL texture and draw the cube into it. 141 gpu::gles2::GLES2Interface* gl = static_cast<gpu::gles2::GLES2Interface*>(
110 GLuint texture = 0u; 142 MojoGLES2GetGLES2Interface(context_));
111 glGenTextures(1, &texture); 143
112 glBindTexture(GL_TEXTURE_2D, texture); 144 GLuint texture_id = 0u;
113 glTexImage2D(GL_TEXTURE_2D, 145 gl->GenTextures(1, &texture_id);
114 0, 146 gl->BindTexture(GL_TEXTURE_2D, texture_id);
115 GL_RGBA, 147 gl->TexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, size_.width(), size_.height(),
116 size_.width(), 148 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
117 size_.height(), 149 gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
118 0, 150
119 GL_RGBA, 151 GrBackendTextureDesc desc;
120 GL_UNSIGNED_BYTE, 152 desc.fFlags = kRenderTarget_GrBackendTextureFlag;
121 0); 153 desc.fWidth = size_.width();
122 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 154 desc.fHeight = size_.height();
123 GLuint fbo = 0u; 155 desc.fConfig = kSkia8888_GrPixelConfig;
124 glGenFramebuffers(1, &fbo); 156 desc.fOrigin = kTopLeft_GrSurfaceOrigin;
125 glBindFramebuffer(GL_FRAMEBUFFER, fbo); 157 desc.fTextureHandle = texture_id;
126 glFramebufferTexture2D( 158 skia::RefPtr<SkTexture> texture = skia::AdoptRef(
127 GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture, 0); 159 gr_context_->wrapBackendTexture(desc));
128 DCHECK_EQ(static_cast<GLenum>(GL_FRAMEBUFFER_COMPLETE), 160
129 glCheckFramebufferStatus(GL_FRAMEBUFFER)); 161 DCHECK(texture) << "No texture";
130 glClearColor(1, 0, 0, 0.5); 162 DCHECK(texture->asRenderTarget()) << "No render target";
131 cube_.UpdateForTimeDelta(0.16f); 163
132 cube_.Draw(); 164 skia::RefPtr<SkSurface> sk_surface = skia::AdoptRef(
165 SkSurface::NewRenderTargetDirect(texture->asRenderTarget()));
166
167 DCHECK(sk_surface);
168
169 SkCanvas* canvas = sk_surface->getCanvas();
170
171 SkPaint paint;
172 paint.setColor(SK_ColorRED);
173 paint.setFlags(SkPaint::kAntiAlias_Flag);
174 canvas->drawCircle(size_.width() / 2, 0, 100, paint);
175 canvas->flush();
133 176
134 // Then, put the texture into a mailbox. 177 // Then, put the texture into a mailbox.
135 gpu::Mailbox mailbox = gpu::Mailbox::Generate(); 178 gpu::Mailbox mailbox = gpu::Mailbox::Generate();
136 glProduceTextureCHROMIUM(GL_TEXTURE_2D, mailbox.name); 179 glProduceTextureCHROMIUM(GL_TEXTURE_2D, mailbox.name);
137 GLuint sync_point = glInsertSyncPointCHROMIUM(); 180 GLuint sync_point = glInsertSyncPointCHROMIUM();
138 gpu::MailboxHolder holder(mailbox, GL_TEXTURE_2D, sync_point); 181 gpu::MailboxHolder holder(mailbox, GL_TEXTURE_2D, sync_point);
139 182
140 // Then, put the mailbox into a TransferableResource 183 // Then, put the mailbox into a TransferableResource
141 cc::TransferableResource resource; 184 cc::TransferableResource resource;
142 resource.id = next_resource_id_++; 185 resource.id = next_resource_id_++;
143 id_to_tex_map_[resource.id] = texture; 186 id_to_tex_map_[resource.id] = texture_id;
144 resource.format = cc::RGBA_8888; 187 resource.format = cc::RGBA_8888;
145 resource.filter = GL_LINEAR; 188 resource.filter = GL_LINEAR;
146 resource.size = size_; 189 resource.size = size_;
147 resource.mailbox_holder = holder; 190 resource.mailbox_holder = holder;
148 resource.is_repeated = false; 191 resource.is_repeated = false;
149 resource.is_software = false; 192 resource.is_software = false;
150 193
151 gfx::Rect rect(size_); 194 gfx::Rect rect(size_);
152 RenderPassId id(1, 1); 195 cc::RenderPassId id(1, 1);
153 scoped_ptr<RenderPass> pass = RenderPass::Create(); 196 scoped_ptr<RenderPass> pass = RenderPass::Create();
154 pass->SetNew(id, rect, rect, gfx::Transform()); 197 pass->SetNew(id, rect, rect, gfx::Transform());
155 198
156 CreateAndAppendSimpleSharedQuadState(pass.get(), gfx::Transform(), size_); 199 CreateAndAppendSimpleSharedQuadState(pass.get(), gfx::Transform(), size_);
157 200
158 TextureDrawQuad* texture_quad = 201 TextureDrawQuad* texture_quad =
159 pass->CreateAndAppendDrawQuad<TextureDrawQuad>(); 202 pass->CreateAndAppendDrawQuad<TextureDrawQuad>();
160 float vertex_opacity[4] = {1.0f, 1.0f, 0.2f, 1.0f}; 203 float vertex_opacity[4] = {1.0f, 1.0f, 1.0f, 1.0f};
161 texture_quad->SetNew(pass->shared_quad_state_list.back(), 204 texture_quad->SetNew(pass->shared_quad_state_list.back(),
162 rect, 205 rect,
163 rect, 206 rect,
164 rect, 207 rect,
165 resource.id, 208 resource.id,
166 true, 209 true,
167 gfx::PointF(), 210 gfx::PointF(),
168 gfx::PointF(1.f, 1.f), 211 gfx::PointF(1.f, 1.f),
169 SK_ColorBLUE, 212 SK_ColorBLUE,
170 vertex_opacity, 213 vertex_opacity,
171 false); 214 false);
172 215
173 scoped_ptr<DelegatedFrameData> delegated_frame_data(new DelegatedFrameData); 216 scoped_ptr<DelegatedFrameData> delegated_frame_data(new DelegatedFrameData);
174 delegated_frame_data->render_pass_list.push_back(pass.Pass()); 217 delegated_frame_data->render_pass_list.push_back(pass.Pass());
175 delegated_frame_data->resource_list.push_back(resource); 218 delegated_frame_data->resource_list.push_back(resource);
176 219
177 scoped_ptr<CompositorFrame> frame(new CompositorFrame); 220 scoped_ptr<CompositorFrame> frame(new CompositorFrame);
178 frame->delegated_frame_data = delegated_frame_data.Pass(); 221 frame->delegated_frame_data = delegated_frame_data.Pass();
179 222
180 surface_->SubmitFrame(mojo::SurfaceId::From(id_), mojo::Frame::From(*frame)); 223 surface_->SubmitFrame(mojo::SurfaceId::From(id_), mojo::Frame::From(*frame));
181 224
182 base::MessageLoop::current()->PostDelayedTask( 225 base::MessageLoop::current()->PostDelayedTask(
183 FROM_HERE, 226 FROM_HERE,
184 base::Bind(&ChildGLImpl::Draw, base::Unretained(this)), 227 base::Bind(&Painter::Draw, base::Unretained(this)),
jamesr 2014/11/14 00:36:42 you have a weak_factory_ - should probably bind th
abarth-chromium 2014/11/14 00:40:14 Fixed.
185 base::TimeDelta::FromMilliseconds(50)); 228 base::TimeDelta::FromMilliseconds(50));
186 } 229 }
187 230
188 } // namespace examples 231 } // namespace examples
189 } // namespace mojo 232 } // namespace mojo
OLDNEW
« examples/ganesh_app/painter.h ('K') | « examples/ganesh_app/painter.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698