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.
5 #include "ui/ozone/demo/surfaceless_gl_renderer.h"
8 #include "ui/gl/gl_bindings.h"
9 #include "ui/gl/gl_context.h"
10 #include "ui/gl/gl_image.h"
11 #include "ui/gl/gl_image_ozone_native_pixmap.h"
12 #include "ui/gl/gl_surface.h"
13 #include "ui/ozone/public/ozone_platform.h"
14 #include "ui/ozone/public/surface_factory_ozone.h"
18 SurfacelessGlRenderer::BufferWrapper::BufferWrapper() {
21 SurfacelessGlRenderer::BufferWrapper::~BufferWrapper() {
23 glDeleteFramebuffersEXT(1, &gl_fb_
);
26 image_
->ReleaseTexImage(GL_TEXTURE_2D
);
27 glDeleteTextures(1, &gl_tex_
);
28 image_
->Destroy(true);
32 bool SurfacelessGlRenderer::BufferWrapper::Initialize(
33 gfx::AcceleratedWidget widget
,
34 const gfx::Size
& size
) {
35 glGenFramebuffersEXT(1, &gl_fb_
);
36 glGenTextures(1, &gl_tex_
);
38 scoped_refptr
<NativePixmap
> pixmap
=
39 OzonePlatform::GetInstance()
40 ->GetSurfaceFactoryOzone()
41 ->CreateNativePixmap(widget
, size
, gfx::BufferFormat::RGBX_8888
,
42 gfx::BufferUsage::SCANOUT
);
43 scoped_refptr
<gfx::GLImageOzoneNativePixmap
> image(
44 new gfx::GLImageOzoneNativePixmap(size
, GL_RGB
));
45 if (!image
->Initialize(pixmap
.get(), gfx::BufferFormat::RGBX_8888
)) {
46 LOG(ERROR
) << "Failed to create GLImage";
51 glBindFramebufferEXT(GL_FRAMEBUFFER
, gl_fb_
);
52 glBindTexture(GL_TEXTURE_2D
, gl_tex_
);
53 image_
->BindTexImage(GL_TEXTURE_2D
);
55 glFramebufferTexture2DEXT(GL_FRAMEBUFFER
, GL_COLOR_ATTACHMENT0
, GL_TEXTURE_2D
,
57 if (glCheckFramebufferStatusEXT(GL_FRAMEBUFFER
) != GL_FRAMEBUFFER_COMPLETE
) {
58 LOG(ERROR
) << "Failed to create framebuffer "
59 << glCheckFramebufferStatusEXT(GL_FRAMEBUFFER
);
69 void SurfacelessGlRenderer::BufferWrapper::BindFramebuffer() {
70 glBindFramebufferEXT(GL_FRAMEBUFFER
, gl_fb_
);
73 void SurfacelessGlRenderer::BufferWrapper::SchedulePlane() {
74 image_
->ScheduleOverlayPlane(widget_
, 0, gfx::OVERLAY_TRANSFORM_NONE
,
75 gfx::Rect(size_
), gfx::RectF(0, 0, 1, 1));
78 SurfacelessGlRenderer::SurfacelessGlRenderer(gfx::AcceleratedWidget widget
,
79 const gfx::Size
& size
)
80 : GlRenderer(widget
, size
), weak_ptr_factory_(this) {}
82 SurfacelessGlRenderer::~SurfacelessGlRenderer() {
83 // Need to make current when deleting the framebuffer resources allocated in
85 context_
->MakeCurrent(surface_
.get());
88 bool SurfacelessGlRenderer::Initialize() {
89 if (!GlRenderer::Initialize())
92 for (size_t i
= 0; i
< arraysize(buffers_
); ++i
)
93 if (!buffers_
[i
].Initialize(widget_
, size_
))
96 PostRenderFrameTask(gfx::SwapResult::SWAP_ACK
);
100 void SurfacelessGlRenderer::RenderFrame() {
101 float fraction
= NextFraction();
103 context_
->MakeCurrent(surface_
.get());
104 buffers_
[back_buffer_
].BindFramebuffer();
106 glViewport(0, 0, size_
.width(), size_
.height());
107 glClearColor(1 - fraction
, 0.0, fraction
, 1.0);
108 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
110 buffers_
[back_buffer_
].SchedulePlane();
112 if (!surface_
->SwapBuffersAsync(base::Bind(&GlRenderer::PostRenderFrameTask
,
113 weak_ptr_factory_
.GetWeakPtr())))
114 LOG(FATAL
) << "Failed to swap buffers";
117 scoped_refptr
<gfx::GLSurface
> SurfacelessGlRenderer::CreateSurface() {
118 return gfx::GLSurface::CreateSurfacelessViewGLSurface(widget_
);