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

Unified 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: Review comments 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « examples/ganesh_app/painter.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: examples/ganesh_app/painter.cc
diff --git a/examples/surfaces_app/child_gl_impl.cc b/examples/ganesh_app/painter.cc
similarity index 57%
copy from examples/surfaces_app/child_gl_impl.cc
copy to examples/ganesh_app/painter.cc
index 4f91f3d9b7c44d8ad2ad721755458ba41148ebc3..941e3245b92920b91f68fc74f1a9398ced95e702 100644
--- a/examples/surfaces_app/child_gl_impl.cc
+++ b/examples/ganesh_app/painter.cc
@@ -2,13 +2,14 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-#include "examples/surfaces_app/child_gl_impl.h"
+#include "examples/ganesh_app/painter.h"
#ifndef GL_GLEXT_PROTOTYPES
#define GL_GLEXT_PROTOTYPES
#endif
#include "base/bind.h"
+#include "base/logging.h"
#include "base/message_loop/message_loop.h"
#include "cc/output/compositor_frame.h"
#include "cc/output/delegated_frame_data.h"
@@ -17,22 +18,26 @@
#include "examples/surfaces_app/surfaces_util.h"
#include "gpu/GLES2/gl2chromium.h"
#include "gpu/GLES2/gl2extchromium.h"
+#include "gpu/command_buffer/client/gles2_interface.h"
+#include "gpu/command_buffer/client/gles2_lib.h"
#include "gpu/command_buffer/common/mailbox.h"
#include "gpu/command_buffer/common/mailbox_holder.h"
+#include "gpu/skia_bindings/gl_bindings_skia_cmd_buffer.h"
#include "mojo/converters/geometry/geometry_type_converters.h"
#include "mojo/converters/surfaces/surfaces_type_converters.h"
+#include "mojo/public/c/gles2/gles2.h"
#include "mojo/public/cpp/application/application_connection.h"
#include "mojo/public/cpp/environment/environment.h"
#include "mojo/services/public/interfaces/surfaces/surface_id.mojom.h"
#include "mojo/services/public/interfaces/surfaces/surfaces.mojom.h"
#include "third_party/khronos/GLES2/gl2.h"
#include "third_party/khronos/GLES2/gl2ext.h"
+#include "third_party/skia/include/core/SkCanvas.h"
+#include "third_party/skia/include/core/SkSurface.h"
+#include "third_party/skia/include/gpu/gl/GrGLInterface.h"
#include "ui/gfx/rect.h"
#include "ui/gfx/transform.h"
-namespace mojo {
-namespace examples {
-
using cc::RenderPass;
using cc::RenderPassId;
using cc::DrawQuad;
@@ -40,54 +45,79 @@ using cc::TextureDrawQuad;
using cc::DelegatedFrameData;
using cc::CompositorFrame;
+namespace mojo {
+namespace examples {
+namespace {
+
+// The limit of the number of GPU resources we hold in the GrContext's
+// GPU cache.
+const int kMaxGaneshResourceCacheCount = 2048;
+
+// The limit of the bytes allocated toward GPU resources in the GrContext's
+// GPU cache.
+const size_t kMaxGaneshResourceCacheBytes = 96 * 1024 * 1024;
+
+}
+
static void ContextLostThunk(void*) {
LOG(FATAL) << "Context lost";
}
-ChildGLImpl::ChildGLImpl(ApplicationConnection* surfaces_service_connection,
- CommandBufferPtr command_buffer)
- : start_time_(base::TimeTicks::Now()),
- next_resource_id_(1),
+Painter::Painter(ApplicationConnection* surfaces_service_connection,
+ CommandBufferPtr command_buffer)
+ : next_resource_id_(0),
weak_factory_(this) {
surfaces_service_connection->ConnectToService(&surfaces_service_);
surfaces_service_->CreateSurfaceConnection(base::Bind(
- &ChildGLImpl::SurfaceConnectionCreated, weak_factory_.GetWeakPtr()));
+ &Painter::SurfaceConnectionCreated, weak_factory_.GetWeakPtr()));
context_ =
MojoGLES2CreateContext(command_buffer.PassMessagePipe().release().value(),
&ContextLostThunk,
this,
- Environment::GetDefaultAsyncWaiter());
+ mojo::Environment::GetDefaultAsyncWaiter());
DCHECK(context_);
MojoGLES2MakeCurrent(context_);
+
+ gpu::gles2::GLES2Interface* gl = static_cast<gpu::gles2::GLES2Interface*>(
+ MojoGLES2GetGLES2Interface(context_));
+ gles2::SetGLContext(gl);
+
+ skia::RefPtr<GrGLInterface> interface = skia::AdoptRef(
+ skia_bindings::CreateCommandBufferSkiaGLBinding());
+ DCHECK(interface);
+
+ gr_context_ = skia::AdoptRef(GrContext::Create(
+ kOpenGL_GrBackend,
+ reinterpret_cast<GrBackendContext>(interface.get())));
+ DCHECK(gr_context_);
+ gr_context_->setResourceCacheLimits(kMaxGaneshResourceCacheCount,
+ kMaxGaneshResourceCacheBytes);
}
-ChildGLImpl::~ChildGLImpl() {
+Painter::~Painter() {
MojoGLES2DestroyContext(context_);
surface_->DestroySurface(mojo::SurfaceId::From(id_));
}
-void ChildGLImpl::ProduceFrame(
+void Painter::ProduceFrame(
ColorPtr color,
SizePtr size,
const mojo::Callback<void(SurfaceIdPtr id)>& callback) {
- color_ = color.To<SkColor>();
+
size_ = size.To<gfx::Size>();
- cube_.Init(size_.width(), size_.height());
- cube_.set_color(
- SkColorGetR(color_), SkColorGetG(color_), SkColorGetB(color_));
produce_callback_ = callback;
AllocateSurface();
}
-void ChildGLImpl::SurfaceConnectionCreated(SurfacePtr surface,
- uint32_t id_namespace) {
+void Painter::SurfaceConnectionCreated(SurfacePtr surface,
+ uint32_t id_namespace) {
surface_ = surface.Pass();
surface_.set_client(this);
allocator_.reset(new cc::SurfaceIdAllocator(id_namespace));
AllocateSurface();
}
-void ChildGLImpl::ReturnResources(Array<ReturnedResourcePtr> resources) {
+void Painter::ReturnResources(Array<ReturnedResourcePtr> resources) {
for (size_t i = 0; i < resources.size(); ++i) {
cc::ReturnedResource res = resources[i].To<cc::ReturnedResource>();
GLuint returned_texture = id_to_tex_map_[res.id];
@@ -95,7 +125,7 @@ void ChildGLImpl::ReturnResources(Array<ReturnedResourcePtr> resources) {
}
}
-void ChildGLImpl::AllocateSurface() {
+void Painter::AllocateSurface() {
if (produce_callback_.is_null() || !allocator_)
return;
@@ -105,31 +135,42 @@ void ChildGLImpl::AllocateSurface() {
Draw();
}
-void ChildGLImpl::Draw() {
- // First, generate a GL texture and draw the cube into it.
- GLuint texture = 0u;
- glGenTextures(1, &texture);
- glBindTexture(GL_TEXTURE_2D, texture);
- glTexImage2D(GL_TEXTURE_2D,
- 0,
- GL_RGBA,
- size_.width(),
- size_.height(),
- 0,
- GL_RGBA,
- GL_UNSIGNED_BYTE,
- 0);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
- GLuint fbo = 0u;
- glGenFramebuffers(1, &fbo);
- glBindFramebuffer(GL_FRAMEBUFFER, fbo);
- glFramebufferTexture2D(
- GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture, 0);
- DCHECK_EQ(static_cast<GLenum>(GL_FRAMEBUFFER_COMPLETE),
- glCheckFramebufferStatus(GL_FRAMEBUFFER));
- glClearColor(1, 0, 0, 0.5);
- cube_.UpdateForTimeDelta(0.16f);
- cube_.Draw();
+void Painter::Draw() {
+ gpu::gles2::GLES2Interface* gl = static_cast<gpu::gles2::GLES2Interface*>(
+ MojoGLES2GetGLES2Interface(context_));
+
+ GLuint texture_id = 0u;
+ gl->GenTextures(1, &texture_id);
+ gl->BindTexture(GL_TEXTURE_2D, texture_id);
+ gl->TexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, size_.width(), size_.height(),
+ 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
+ gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
+
+ GrBackendTextureDesc desc;
+ desc.fFlags = kRenderTarget_GrBackendTextureFlag;
+ desc.fWidth = size_.width();
+ desc.fHeight = size_.height();
+ desc.fConfig = kSkia8888_GrPixelConfig;
+ desc.fOrigin = kTopLeft_GrSurfaceOrigin;
+ desc.fTextureHandle = texture_id;
+ skia::RefPtr<GrTexture> texture = skia::AdoptRef(
+ gr_context_->wrapBackendTexture(desc));
+
+ DCHECK(texture) << "No texture";
+ DCHECK(texture->asRenderTarget()) << "No render target";
+
+ skia::RefPtr<SkSurface> sk_surface = skia::AdoptRef(
+ SkSurface::NewRenderTargetDirect(texture->asRenderTarget()));
+
+ DCHECK(sk_surface);
+
+ SkCanvas* canvas = sk_surface->getCanvas();
+
+ SkPaint paint;
+ paint.setColor(SK_ColorRED);
+ paint.setFlags(SkPaint::kAntiAlias_Flag);
+ canvas->drawCircle(size_.width() / 2, 0, 100, paint);
+ canvas->flush();
// Then, put the texture into a mailbox.
gpu::Mailbox mailbox = gpu::Mailbox::Generate();
@@ -140,7 +181,7 @@ void ChildGLImpl::Draw() {
// Then, put the mailbox into a TransferableResource
cc::TransferableResource resource;
resource.id = next_resource_id_++;
- id_to_tex_map_[resource.id] = texture;
+ id_to_tex_map_[resource.id] = texture_id;
resource.format = cc::RGBA_8888;
resource.filter = GL_LINEAR;
resource.size = size_;
@@ -149,7 +190,7 @@ void ChildGLImpl::Draw() {
resource.is_software = false;
gfx::Rect rect(size_);
- RenderPassId id(1, 1);
+ cc::RenderPassId id(1, 1);
scoped_ptr<RenderPass> pass = RenderPass::Create();
pass->SetNew(id, rect, rect, gfx::Transform());
@@ -157,7 +198,7 @@ void ChildGLImpl::Draw() {
TextureDrawQuad* texture_quad =
pass->CreateAndAppendDrawQuad<TextureDrawQuad>();
- float vertex_opacity[4] = {1.0f, 1.0f, 0.2f, 1.0f};
+ float vertex_opacity[4] = {1.0f, 1.0f, 1.0f, 1.0f};
texture_quad->SetNew(pass->shared_quad_state_list.back(),
rect,
rect,
@@ -181,7 +222,7 @@ void ChildGLImpl::Draw() {
base::MessageLoop::current()->PostDelayedTask(
FROM_HERE,
- base::Bind(&ChildGLImpl::Draw, base::Unretained(this)),
+ base::Bind(&Painter::Draw, weak_factory_.GetWeakPtr()),
base::TimeDelta::FromMilliseconds(50));
}
« no previous file with comments | « examples/ganesh_app/painter.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698