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

Side by Side Diff: mojo/ui/gl_renderer.cc

Issue 1556803002: Add helpers for creating UI components. (Closed) Base URL: git@github.com:domokit/mojo.git@moz-13
Patch Set: Created 4 years, 11 months 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
OLDNEW
(Empty)
1 // Copyright 2015 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/ui/gl_renderer.h"
6
7 #ifndef GL_GLEXT_PROTOTYPES
8 #define GL_GLEXT_PROTOTYPES
9 #endif
10 #include <GLES2/gl2.h>
11 #include <GLES2/gl2extmojo.h>
12
13 #include "mojo/gpu/gl_context.h"
14 #include "mojo/gpu/gl_texture.h"
15
16 namespace mojo {
17 namespace ui {
18
19 GLRenderer::GLRenderer(base::WeakPtr<mojo::GLContext> gl_context,
20 uint32_t max_recycled_textures)
21 : gl_context_(gl_context),
22 max_recycled_textures_(max_recycled_textures),
23 weak_factory_(this) {}
24
25 GLRenderer::~GLRenderer() {}
26
27 std::unique_ptr<mojo::GLTexture> GLRenderer::GetTexture(
28 const mojo::Size& requested_size) {
29 if (!gl_context_) {
30 recycled_textures_.clear();
31 return nullptr;
32 }
33
34 while (!recycled_textures_.empty()) {
35 GLRecycledTextureInfo texture_info(std::move(recycled_textures_.front()));
36 recycled_textures_.pop_front();
37 if (texture_info.first->size().Equals(requested_size)) {
38 gl_context_->MakeCurrent();
39 glWaitSyncPointCHROMIUM(texture_info.second);
40 return std::move(texture_info.first);
41 }
42 }
43
44 return std::unique_ptr<GLTexture>(new GLTexture(gl_context_, requested_size));
45 }
46
47 mojo::gfx::composition::ResourcePtr GLRenderer::BindTextureResource(
48 std::unique_ptr<GLTexture> texture) {
49 if (!gl_context_)
50 return nullptr;
51
52 // Produce the texture.
53 gl_context_->MakeCurrent();
54 glBindTexture(GL_TEXTURE_2D, texture->texture_id());
55 GLbyte mailbox[GL_MAILBOX_SIZE_CHROMIUM];
56 glGenMailboxCHROMIUM(mailbox);
57 glProduceTextureCHROMIUM(GL_TEXTURE_2D, mailbox);
58 glBindTexture(GL_TEXTURE_2D, 0);
59 GLuint sync_point = glInsertSyncPointCHROMIUM();
60
61 // Populate the resource description.
62 auto resource = mojo::gfx::composition::Resource::New();
63 resource->set_mailbox_texture(
64 mojo::gfx::composition::MailboxTextureResource::New());
65 resource->get_mailbox_texture()->mailbox_name.resize(sizeof(mailbox));
66 memcpy(resource->get_mailbox_texture()->mailbox_name.data(), mailbox,
67 sizeof(mailbox));
68 resource->get_mailbox_texture()->sync_point = sync_point;
69 resource->get_mailbox_texture()->size = texture->size().Clone();
70 resource->get_mailbox_texture()->callback =
71 (new GLTextureReleaser(
72 weak_factory_.GetWeakPtr(),
73 GLRecycledTextureInfo(std::move(texture), sync_point)))
74 ->StrongBind()
75 .Pass();
76
77 bound_textures_++;
78 DVLOG(2) << "bind: bound_textures=" << bound_textures_;
79 return resource;
80 }
81
82 mojo::gfx::composition::ResourcePtr GLRenderer::DrawGL(
83 const mojo::Size& size,
84 bool with_depth,
85 const DrawGLCallback& callback) {
86 std::unique_ptr<mojo::GLTexture> texture = GetTexture(size);
87 DCHECK(texture);
88
89 GLuint fbo = 0u;
90 glGenFramebuffers(1, &fbo);
91 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
92 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
93 texture->texture_id(), 0);
94
95 GLuint depth_buffer = 0u;
96 if (with_depth) {
97 glGenRenderbuffers(1, &depth_buffer);
98 glBindRenderbuffer(GL_RENDERBUFFER, depth_buffer);
99 glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, size.width,
100 size.height);
101 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT,
102 GL_RENDERBUFFER, depth_buffer);
103 }
104
105 DCHECK_EQ(static_cast<GLenum>(GL_FRAMEBUFFER_COMPLETE),
106 glCheckFramebufferStatus(GL_FRAMEBUFFER));
107
108 glViewport(0, 0, size.width, size.height);
109 callback.Run();
110
111 if (with_depth)
112 glDeleteRenderbuffers(1, &depth_buffer);
113 glDeleteFramebuffers(1, &fbo);
114
115 return BindTextureResource(std::move(texture));
116 }
117
118 void GLRenderer::ReleaseTexture(GLRecycledTextureInfo texture_info,
119 bool recyclable) {
120 DCHECK(bound_textures_);
121 bound_textures_--;
122 if (recyclable && recycled_textures_.size() < max_recycled_textures_) {
123 recycled_textures_.emplace_back(std::move(texture_info));
124 }
125 DVLOG(2) << "release: bound_textures=" << bound_textures_
126 << ", recycled_textures=" << recycled_textures_.size();
127 }
128
129 GLRenderer::GLTextureReleaser::GLTextureReleaser(
130 const base::WeakPtr<GLRenderer>& provider,
131 GLRecycledTextureInfo info)
132 : provider_(provider), texture_info_(std::move(info)), binding_(this) {}
133
134 GLRenderer::GLTextureReleaser::~GLTextureReleaser() {
135 // It's possible for the object to be destroyed due to a connection
136 // error on the callback pipe. When this happens we don't want to
137 // recycle the texture since we have too little knowledge about its
138 // state to confirm that it will be safe to do so.
139 Release(false /*recyclable*/);
140 }
141
142 mojo::gfx::composition::MailboxTextureCallbackPtr
143 GLRenderer::GLTextureReleaser::StrongBind() {
144 mojo::gfx::composition::MailboxTextureCallbackPtr callback;
145 binding_.Bind(mojo::GetProxy(&callback));
146 return callback;
147 }
148
149 void GLRenderer::GLTextureReleaser::OnMailboxTextureReleased() {
150 Release(true /*recyclable*/);
151 }
152
153 void GLRenderer::GLTextureReleaser::Release(bool recyclable) {
154 if (provider_) {
155 provider_->ReleaseTexture(std::move(texture_info_), recyclable);
156 provider_.reset();
157 }
158 }
159
160 } // namespace ui
161 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698