ozone: evdev: Sync caps lock LED state to evdev
[chromium-blink-merge.git] / ui / gl / gl_image_memory.cc
blob6b9a51967a0f2e42d41dc192585a4160236ef923
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/gl/gl_image_memory.h"
7 #include "base/logging.h"
8 #include "base/trace_event/trace_event.h"
9 #include "ui/gl/gl_bindings.h"
10 #include "ui/gl/scoped_binders.h"
12 #if defined(OS_WIN) || defined(USE_X11) || defined(OS_ANDROID) || \
13 defined(USE_OZONE)
14 #include "ui/gl/gl_surface_egl.h"
15 #endif
17 namespace gfx {
18 namespace {
20 bool ValidInternalFormat(unsigned internalformat) {
21 switch (internalformat) {
22 case GL_RGBA:
23 return true;
24 default:
25 return false;
29 bool ValidFormat(gfx::GpuMemoryBuffer::Format format) {
30 switch (format) {
31 case gfx::GpuMemoryBuffer::ATC:
32 case gfx::GpuMemoryBuffer::ATCIA:
33 case gfx::GpuMemoryBuffer::DXT1:
34 case gfx::GpuMemoryBuffer::DXT5:
35 case gfx::GpuMemoryBuffer::ETC1:
36 case gfx::GpuMemoryBuffer::RGBA_8888:
37 case gfx::GpuMemoryBuffer::BGRA_8888:
38 return true;
39 case gfx::GpuMemoryBuffer::RGBX_8888:
40 return false;
43 NOTREACHED();
44 return false;
47 bool IsCompressedFormat(gfx::GpuMemoryBuffer::Format format) {
48 switch (format) {
49 case gfx::GpuMemoryBuffer::ATC:
50 case gfx::GpuMemoryBuffer::ATCIA:
51 case gfx::GpuMemoryBuffer::DXT1:
52 case gfx::GpuMemoryBuffer::DXT5:
53 case gfx::GpuMemoryBuffer::ETC1:
54 return true;
55 case gfx::GpuMemoryBuffer::RGBA_8888:
56 case gfx::GpuMemoryBuffer::BGRA_8888:
57 case gfx::GpuMemoryBuffer::RGBX_8888:
58 return false;
61 NOTREACHED();
62 return false;
65 GLenum TextureFormat(gfx::GpuMemoryBuffer::Format format) {
66 switch (format) {
67 case gfx::GpuMemoryBuffer::ATC:
68 return GL_ATC_RGB_AMD;
69 case gfx::GpuMemoryBuffer::ATCIA:
70 return GL_ATC_RGBA_INTERPOLATED_ALPHA_AMD;
71 case gfx::GpuMemoryBuffer::DXT1:
72 return GL_COMPRESSED_RGB_S3TC_DXT1_EXT;
73 case gfx::GpuMemoryBuffer::DXT5:
74 return GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
75 case gfx::GpuMemoryBuffer::ETC1:
76 return GL_ETC1_RGB8_OES;
77 case gfx::GpuMemoryBuffer::RGBA_8888:
78 return GL_RGBA;
79 case gfx::GpuMemoryBuffer::BGRA_8888:
80 return GL_BGRA_EXT;
81 case gfx::GpuMemoryBuffer::RGBX_8888:
82 NOTREACHED();
83 return 0;
86 NOTREACHED();
87 return 0;
90 GLenum DataFormat(gfx::GpuMemoryBuffer::Format format) {
91 return TextureFormat(format);
94 GLenum DataType(gfx::GpuMemoryBuffer::Format format) {
95 switch (format) {
96 case gfx::GpuMemoryBuffer::RGBA_8888:
97 case gfx::GpuMemoryBuffer::BGRA_8888:
98 return GL_UNSIGNED_BYTE;
99 case gfx::GpuMemoryBuffer::ATC:
100 case gfx::GpuMemoryBuffer::ATCIA:
101 case gfx::GpuMemoryBuffer::DXT1:
102 case gfx::GpuMemoryBuffer::DXT5:
103 case gfx::GpuMemoryBuffer::ETC1:
104 case gfx::GpuMemoryBuffer::RGBX_8888:
105 NOTREACHED();
106 return 0;
109 NOTREACHED();
110 return 0;
113 GLsizei SizeInBytes(const gfx::Size& size,
114 gfx::GpuMemoryBuffer::Format format) {
115 size_t stride_in_bytes = 0;
116 bool valid_stride = GLImageMemory::StrideInBytes(
117 size.width(), format, &stride_in_bytes);
118 DCHECK(valid_stride);
119 return static_cast<GLsizei>(stride_in_bytes * size.height());
122 } // namespace
124 GLImageMemory::GLImageMemory(const gfx::Size& size, unsigned internalformat)
125 : size_(size),
126 internalformat_(internalformat),
127 memory_(NULL),
128 format_(gfx::GpuMemoryBuffer::RGBA_8888),
129 in_use_(false),
130 target_(0),
131 need_do_bind_tex_image_(false)
132 #if defined(OS_WIN) || defined(USE_X11) || defined(OS_ANDROID) || \
133 defined(USE_OZONE)
135 egl_texture_id_(0u),
136 egl_image_(EGL_NO_IMAGE_KHR)
137 #endif
141 GLImageMemory::~GLImageMemory() {
142 #if defined(OS_WIN) || defined(USE_X11) || defined(OS_ANDROID) || \
143 defined(USE_OZONE)
144 DCHECK_EQ(EGL_NO_IMAGE_KHR, egl_image_);
145 DCHECK_EQ(0u, egl_texture_id_);
146 #endif
149 // static
150 bool GLImageMemory::StrideInBytes(size_t width,
151 gfx::GpuMemoryBuffer::Format format,
152 size_t* stride_in_bytes) {
153 base::CheckedNumeric<size_t> s = width;
154 switch (format) {
155 case gfx::GpuMemoryBuffer::ATCIA:
156 case gfx::GpuMemoryBuffer::DXT5:
157 *stride_in_bytes = width;
158 return true;
159 case gfx::GpuMemoryBuffer::ATC:
160 case gfx::GpuMemoryBuffer::DXT1:
161 case gfx::GpuMemoryBuffer::ETC1:
162 DCHECK_EQ(width % 2, 0U);
163 s /= 2;
164 if (!s.IsValid())
165 return false;
167 *stride_in_bytes = s.ValueOrDie();
168 return true;
169 case gfx::GpuMemoryBuffer::RGBA_8888:
170 case gfx::GpuMemoryBuffer::BGRA_8888:
171 s *= 4;
172 if (!s.IsValid())
173 return false;
175 *stride_in_bytes = s.ValueOrDie();
176 return true;
177 case gfx::GpuMemoryBuffer::RGBX_8888:
178 NOTREACHED();
179 return false;
182 NOTREACHED();
183 return false;
186 // static
187 bool GLImageMemory::ValidSize(const gfx::Size& size,
188 gfx::GpuMemoryBuffer::Format format) {
189 switch (format) {
190 case gfx::GpuMemoryBuffer::ATC:
191 case gfx::GpuMemoryBuffer::ATCIA:
192 case gfx::GpuMemoryBuffer::DXT1:
193 case gfx::GpuMemoryBuffer::DXT5:
194 case gfx::GpuMemoryBuffer::ETC1:
195 // Compressed images must have a width and height that's evenly divisible
196 // by the block size.
197 return size.width() % 4 == 0 && size.height() % 4 == 0;
198 case gfx::GpuMemoryBuffer::RGBA_8888:
199 case gfx::GpuMemoryBuffer::BGRA_8888:
200 return true;
201 case gfx::GpuMemoryBuffer::RGBX_8888:
202 NOTREACHED();
203 return false;
206 NOTREACHED();
207 return false;
210 bool GLImageMemory::Initialize(const unsigned char* memory,
211 gfx::GpuMemoryBuffer::Format format) {
212 if (!ValidInternalFormat(internalformat_)) {
213 LOG(ERROR) << "Invalid internalformat: " << internalformat_;
214 return false;
217 if (!ValidFormat(format)) {
218 LOG(ERROR) << "Invalid format: " << format;
219 return false;
222 DCHECK(memory);
223 DCHECK(!memory_);
224 memory_ = memory;
225 format_ = format;
226 return true;
229 void GLImageMemory::Destroy(bool have_context) {
230 #if defined(OS_WIN) || defined(USE_X11) || defined(OS_ANDROID) || \
231 defined(USE_OZONE)
232 if (egl_image_ != EGL_NO_IMAGE_KHR) {
233 eglDestroyImageKHR(GLSurfaceEGL::GetHardwareDisplay(), egl_image_);
234 egl_image_ = EGL_NO_IMAGE_KHR;
237 if (egl_texture_id_) {
238 if (have_context)
239 glDeleteTextures(1, &egl_texture_id_);
240 egl_texture_id_ = 0u;
242 #endif
243 memory_ = NULL;
246 gfx::Size GLImageMemory::GetSize() {
247 return size_;
250 bool GLImageMemory::BindTexImage(unsigned target) {
251 if (target_ && target_ != target) {
252 LOG(ERROR) << "GLImage can only be bound to one target";
253 return false;
255 target_ = target;
257 // Defer DoBindTexImage if not currently in use.
258 if (!in_use_) {
259 need_do_bind_tex_image_ = true;
260 return true;
263 DoBindTexImage(target);
264 return true;
267 bool GLImageMemory::CopyTexImage(unsigned target) {
268 TRACE_EVENT0("gpu", "GLImageMemory::CopyTexImage");
270 // GL_TEXTURE_EXTERNAL_OES is not a supported CopyTexImage target.
271 if (target == GL_TEXTURE_EXTERNAL_OES)
272 return false;
274 DCHECK(memory_);
275 if (IsCompressedFormat(format_)) {
276 glCompressedTexSubImage2D(target,
277 0, // level
278 0, // x-offset
279 0, // y-offset
280 size_.width(), size_.height(),
281 DataFormat(format_), SizeInBytes(size_, format_),
282 memory_);
283 } else {
284 glTexSubImage2D(target, 0, // level
285 0, // x
286 0, // y
287 size_.width(), size_.height(), DataFormat(format_),
288 DataType(format_), memory_);
291 return true;
294 void GLImageMemory::WillUseTexImage() {
295 DCHECK(!in_use_);
296 in_use_ = true;
298 if (!need_do_bind_tex_image_)
299 return;
301 DCHECK(target_);
302 DoBindTexImage(target_);
305 void GLImageMemory::DidUseTexImage() {
306 DCHECK(in_use_);
307 in_use_ = false;
310 bool GLImageMemory::ScheduleOverlayPlane(gfx::AcceleratedWidget widget,
311 int z_order,
312 OverlayTransform transform,
313 const Rect& bounds_rect,
314 const RectF& crop_rect) {
315 return false;
318 void GLImageMemory::DoBindTexImage(unsigned target) {
319 TRACE_EVENT0("gpu", "GLImageMemory::DoBindTexImage");
321 DCHECK(need_do_bind_tex_image_);
322 need_do_bind_tex_image_ = false;
324 DCHECK(memory_);
325 #if defined(OS_WIN) || defined(USE_X11) || defined(OS_ANDROID) || \
326 defined(USE_OZONE)
327 if (target == GL_TEXTURE_EXTERNAL_OES) {
328 if (egl_image_ == EGL_NO_IMAGE_KHR) {
329 DCHECK_EQ(0u, egl_texture_id_);
330 glGenTextures(1, &egl_texture_id_);
333 ScopedTextureBinder texture_binder(GL_TEXTURE_2D, egl_texture_id_);
335 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
336 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
337 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
338 if (IsCompressedFormat(format_)) {
339 glCompressedTexImage2D(GL_TEXTURE_2D,
340 0, // mip level
341 TextureFormat(format_), size_.width(),
342 size_.height(),
343 0, // border
344 SizeInBytes(size_, format_), memory_);
345 } else {
346 glTexImage2D(GL_TEXTURE_2D,
347 0, // mip level
348 TextureFormat(format_),
349 size_.width(),
350 size_.height(),
351 0, // border
352 DataFormat(format_),
353 DataType(format_),
354 memory_);
358 EGLint attrs[] = {EGL_IMAGE_PRESERVED_KHR, EGL_TRUE, EGL_NONE};
359 // Need to pass current EGL rendering context to eglCreateImageKHR for
360 // target type EGL_GL_TEXTURE_2D_KHR.
361 egl_image_ =
362 eglCreateImageKHR(GLSurfaceEGL::GetHardwareDisplay(),
363 eglGetCurrentContext(),
364 EGL_GL_TEXTURE_2D_KHR,
365 reinterpret_cast<EGLClientBuffer>(egl_texture_id_),
366 attrs);
367 DCHECK_NE(EGL_NO_IMAGE_KHR, egl_image_)
368 << "Error creating EGLImage: " << eglGetError();
369 } else {
370 ScopedTextureBinder texture_binder(GL_TEXTURE_2D, egl_texture_id_);
372 if (IsCompressedFormat(format_)) {
373 glCompressedTexSubImage2D(GL_TEXTURE_2D,
374 0, // mip level
375 0, // x-offset
376 0, // y-offset
377 size_.width(), size_.height(),
378 DataFormat(format_),
379 SizeInBytes(size_, format_),
380 memory_);
381 } else {
382 glTexSubImage2D(GL_TEXTURE_2D,
383 0, // mip level
384 0, // x-offset
385 0, // y-offset
386 size_.width(),
387 size_.height(),
388 DataFormat(format_),
389 DataType(format_),
390 memory_);
394 glEGLImageTargetTexture2DOES(target, egl_image_);
395 DCHECK_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError());
396 return;
398 #endif
400 DCHECK_NE(static_cast<GLenum>(GL_TEXTURE_EXTERNAL_OES), target);
401 if (IsCompressedFormat(format_)) {
402 glCompressedTexImage2D(target,
403 0, // mip level
404 TextureFormat(format_), size_.width(),
405 size_.height(),
406 0, // border
407 SizeInBytes(size_, format_), memory_);
408 } else {
409 glTexImage2D(target,
410 0, // mip level
411 TextureFormat(format_),
412 size_.width(),
413 size_.height(),
414 0, // border
415 DataFormat(format_),
416 DataType(format_),
417 memory_);
421 } // namespace gfx