Separate Simple Backend creation from initialization.
[chromium-blink-merge.git] / ui / gl / gl_image_egl.cc
blobf7f2889e77ee0a202f14c626fab0ef21e2b4481a
1 // Copyright (c) 2013 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/gl/gl_image_egl.h"
7 #include "ui/gl/gl_bindings.h"
9 namespace gfx {
11 GLImageEGL::GLImageEGL(gfx::Size size)
12 : egl_image_(EGL_NO_IMAGE_KHR),
13 size_(size) {
16 GLImageEGL::~GLImageEGL() {
17 Destroy();
20 bool GLImageEGL::Initialize(gfx::GpuMemoryBufferHandle buffer) {
21 EGLClientBuffer cbuf = static_cast<EGLClientBuffer>(buffer);
22 EGLint attrs[] = {
23 EGL_IMAGE_PRESERVED_KHR, EGL_TRUE,
24 EGL_NONE,
26 egl_image_ = eglCreateImageKHR(
27 eglGetDisplay(EGL_DEFAULT_DISPLAY),
28 EGL_NO_CONTEXT,
29 EGL_NATIVE_BUFFER_ANDROID,
30 cbuf,
31 attrs);
33 if (egl_image_ == EGL_NO_IMAGE_KHR) {
34 EGLint error = eglGetError();
35 LOG(ERROR) << "Error creating EGLImage: " << error;
36 return false;
39 return true;
42 bool GLImageEGL::BindTexImage() {
43 if (egl_image_ == EGL_NO_IMAGE_KHR) {
44 LOG(ERROR) << "NULL EGLImage in BindTexImage";
45 return false;
48 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, egl_image_);
50 if (glGetError() != GL_NO_ERROR) {
51 return false;
54 return true;
57 gfx::Size GLImageEGL::GetSize() {
58 return size_;
61 void GLImageEGL::Destroy() {
62 if (egl_image_ == EGL_NO_IMAGE_KHR)
63 return;
65 EGLBoolean success = eglDestroyImageKHR(
66 eglGetDisplay(EGL_DEFAULT_DISPLAY), egl_image_);
68 if (success == EGL_FALSE) {
69 EGLint error = eglGetError();
70 LOG(ERROR) << "Error destroying EGLImage: " << error;
73 egl_image_ = EGL_NO_IMAGE_KHR;
76 void GLImageEGL::ReleaseTexImage() {
77 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 0, 0, 0, GL_RGBA, GL_UNSIGNED_BYTE,
78 NULL);
81 } // namespace gfx