OLD | NEW |
---|---|
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 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 "ui/gl/gl_image_egl.h" | 5 #include "ui/gl/gl_image_egl.h" |
6 | 6 |
7 #include "ui/gl/gl_bindings.h" | 7 #include "ui/gl/gl_bindings.h" |
8 #include "ui/gl/gl_surface_egl.h" | 8 #include "ui/gl/gl_surface_egl.h" |
9 #include "ui/gl/scoped_binders.h" | |
9 | 10 |
10 namespace gfx { | 11 namespace gfx { |
11 | 12 |
12 GLImageEGL::GLImageEGL(gfx::Size size) | 13 GLImageEGL::GLImageEGL(gfx::Size size) |
13 : egl_image_(EGL_NO_IMAGE_KHR), | 14 : egl_image_(EGL_NO_IMAGE_KHR), |
14 size_(size), | 15 size_(size), |
15 release_after_use_(false), | 16 release_after_use_(false), |
16 in_use_(false), | 17 in_use_(false), |
17 target_(0) { | 18 target_(0), |
18 } | 19 egl_image_for_unbind_(EGL_NO_IMAGE_KHR), |
20 texture_id_for_unbind_(0) {} | |
19 | 21 |
20 GLImageEGL::~GLImageEGL() { | 22 GLImageEGL::~GLImageEGL() { |
21 Destroy(); | 23 Destroy(); |
22 } | 24 } |
23 | 25 |
24 bool GLImageEGL::Initialize(gfx::GpuMemoryBufferHandle buffer) { | 26 bool GLImageEGL::Initialize(gfx::GpuMemoryBufferHandle buffer) { |
25 DCHECK(buffer.native_buffer); | 27 DCHECK(buffer.native_buffer); |
26 EGLint attrs[] = { | 28 EGLint attrs[] = { |
27 EGL_IMAGE_PRESERVED_KHR, EGL_TRUE, | 29 EGL_IMAGE_PRESERVED_KHR, EGL_TRUE, |
28 EGL_NONE, | 30 EGL_NONE, |
29 }; | 31 }; |
30 egl_image_ = eglCreateImageKHR( | 32 egl_image_ = eglCreateImageKHR(GLSurfaceEGL::GetHardwareDisplay(), |
31 GLSurfaceEGL::GetHardwareDisplay(), | 33 EGL_NO_CONTEXT, |
32 EGL_NO_CONTEXT, | 34 EGL_NATIVE_BUFFER_ANDROID, |
33 EGL_NATIVE_BUFFER_ANDROID, | 35 buffer.native_buffer, |
34 buffer.native_buffer, | 36 attrs); |
35 attrs); | |
36 | 37 |
37 if (egl_image_ == EGL_NO_IMAGE_KHR) { | 38 if (egl_image_ == EGL_NO_IMAGE_KHR) { |
38 EGLint error = eglGetError(); | 39 EGLint error = eglGetError(); |
39 LOG(ERROR) << "Error creating EGLImage: " << error; | 40 LOG(ERROR) << "Error creating EGLImage: " << error; |
40 return false; | 41 return false; |
41 } | 42 } |
42 | 43 |
43 return true; | 44 return true; |
44 } | 45 } |
45 | 46 |
46 void GLImageEGL::Destroy() { | 47 void GLImageEGL::Destroy() { |
47 if (egl_image_ == EGL_NO_IMAGE_KHR) | 48 if (egl_image_ != EGL_NO_IMAGE_KHR) { |
48 return; | 49 EGLBoolean success = |
49 | 50 eglDestroyImageKHR(GLSurfaceEGL::GetHardwareDisplay(), egl_image_); |
50 EGLBoolean success = eglDestroyImageKHR( | 51 if (success == EGL_FALSE) |
51 GLSurfaceEGL::GetHardwareDisplay(), egl_image_); | 52 LOG(ERROR) << "Error destroying EGLImage: " << eglGetError(); |
52 | 53 egl_image_ = EGL_NO_IMAGE_KHR; |
53 if (success == EGL_FALSE) { | |
54 EGLint error = eglGetError(); | |
55 LOG(ERROR) << "Error destroying EGLImage: " << error; | |
56 } | 54 } |
57 | 55 |
58 egl_image_ = EGL_NO_IMAGE_KHR; | 56 if (egl_image_for_unbind_ != EGL_NO_IMAGE_KHR) { |
57 EGLBoolean success = | |
58 eglDestroyImageKHR(eglGetCurrentDisplay(), egl_image_for_unbind_); | |
reveman
2014/03/22 12:38:57
does this need to be GLSurfaceEGL::GetHardwareDisp
| |
59 if (success == EGL_FALSE) | |
60 LOG(ERROR) << "Error destroying EGLImage: " << eglGetError(); | |
61 egl_image_for_unbind_ = EGL_NO_IMAGE_KHR; | |
62 } | |
63 | |
64 if (texture_id_for_unbind_) { | |
65 glDeleteTextures(1, &texture_id_for_unbind_); | |
66 texture_id_for_unbind_ = 0; | |
67 } | |
59 } | 68 } |
60 | 69 |
61 gfx::Size GLImageEGL::GetSize() { | 70 gfx::Size GLImageEGL::GetSize() { |
62 return size_; | 71 return size_; |
63 } | 72 } |
64 | 73 |
65 bool GLImageEGL::BindTexImage(unsigned target) { | 74 bool GLImageEGL::BindTexImage(unsigned target) { |
66 if (egl_image_ == EGL_NO_IMAGE_KHR) { | 75 if (egl_image_ == EGL_NO_IMAGE_KHR) { |
67 LOG(ERROR) << "NULL EGLImage in BindTexImage"; | 76 LOG(ERROR) << "NULL EGLImage in BindTexImage"; |
68 return false; | 77 return false; |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
101 DCHECK_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError()); | 110 DCHECK_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError()); |
102 } | 111 } |
103 | 112 |
104 void GLImageEGL::DidUseTexImage() { | 113 void GLImageEGL::DidUseTexImage() { |
105 DCHECK(in_use_); | 114 DCHECK(in_use_); |
106 in_use_ = false; | 115 in_use_ = false; |
107 | 116 |
108 if (!release_after_use_) | 117 if (!release_after_use_) |
109 return; | 118 return; |
110 | 119 |
111 char zero[4] = { 0, }; | 120 if (egl_image_for_unbind_ == EGL_NO_IMAGE_KHR) { |
112 glTexImage2D(target_, | 121 DCHECK_EQ(0u, texture_id_for_unbind_); |
113 0, | 122 glGenTextures(1, &texture_id_for_unbind_); |
114 GL_RGBA, | 123 |
115 1, | 124 ScopedTextureBinder texture_binder(GL_TEXTURE_2D, texture_id_for_unbind_); |
reveman
2014/03/22 12:38:57
I assume this is only needed until glTexImage2D is
| |
116 1, | 125 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
117 0, | 126 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
118 GL_RGBA, | 127 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
119 GL_UNSIGNED_BYTE, | 128 |
120 &zero); | 129 char zero[4] = {0, }; |
130 glTexImage2D( | |
131 GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, &zero); | |
132 | |
133 EGLint attrs[] = {EGL_GL_TEXTURE_LEVEL_KHR, 0, // mip-level. | |
134 EGL_IMAGE_PRESERVED_KHR, EGL_TRUE, EGL_NONE}; | |
135 egl_image_for_unbind_ = eglCreateImageKHR( | |
136 eglGetCurrentDisplay(), | |
reveman
2014/03/22 12:38:57
GLSurfaceEGL::GetHardwareDisplay() if possible
| |
137 eglGetCurrentContext(), | |
reveman
2014/03/22 12:38:57
EGL_NO_CONTEXT if possible?
| |
138 EGL_GL_TEXTURE_2D_KHR, | |
139 reinterpret_cast<EGLClientBuffer>(texture_id_for_unbind_), | |
140 attrs); | |
141 DCHECK_NE(EGL_NO_IMAGE_KHR, egl_image_for_unbind_) | |
142 << "Error creating EGLImage: " << eglGetError(); | |
143 } | |
144 | |
145 glEGLImageTargetTexture2DOES(target_, egl_image_for_unbind_); | |
146 DCHECK_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError()); | |
121 } | 147 } |
122 | 148 |
123 void GLImageEGL::WillModifyTexImage() { | 149 void GLImageEGL::WillModifyTexImage() { |
124 } | 150 } |
125 | 151 |
126 void GLImageEGL::DidModifyTexImage() { | 152 void GLImageEGL::DidModifyTexImage() { |
127 } | 153 } |
128 | 154 |
129 void GLImageEGL::SetReleaseAfterUse() { | 155 void GLImageEGL::SetReleaseAfterUse() { |
130 release_after_use_ = true; | 156 release_after_use_ = true; |
131 } | 157 } |
132 | 158 |
133 } // namespace gfx | 159 } // namespace gfx |
OLD | NEW |