Migrate TabUmaTest from ChromeShell to ChromePublic.
[chromium-blink-merge.git] / ui / gl / gl_image_io_surface.cc
blobadcadee363ab5499ca14bd43fbf5bff11378ba51
1 // Copyright 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_io_surface.h"
7 #include "ui/gl/gl_bindings.h"
8 #include "ui/gl/gl_context.h"
10 // Note that this must be included after gl_bindings.h to avoid conflicts.
11 #include <OpenGL/CGLIOSurface.h>
13 namespace gfx {
14 namespace {
16 bool ValidInternalFormat(unsigned internalformat) {
17 switch (internalformat) {
18 case GL_R8:
19 case GL_BGRA_EXT:
20 return true;
21 default:
22 return false;
26 bool ValidFormat(GpuMemoryBuffer::Format format) {
27 switch (format) {
28 case GpuMemoryBuffer::R_8:
29 case GpuMemoryBuffer::BGRA_8888:
30 return true;
31 case GpuMemoryBuffer::ATC:
32 case GpuMemoryBuffer::ATCIA:
33 case GpuMemoryBuffer::DXT1:
34 case GpuMemoryBuffer::DXT5:
35 case GpuMemoryBuffer::ETC1:
36 case GpuMemoryBuffer::RGBA_4444:
37 case GpuMemoryBuffer::RGBA_8888:
38 case GpuMemoryBuffer::RGBX_8888:
39 case GpuMemoryBuffer::YUV_420:
40 return false;
43 NOTREACHED();
44 return false;
47 GLenum TextureFormat(GpuMemoryBuffer::Format format) {
48 switch (format) {
49 case GpuMemoryBuffer::R_8:
50 return GL_RED;
51 case GpuMemoryBuffer::BGRA_8888:
52 return GL_RGBA;
53 case GpuMemoryBuffer::ATC:
54 case GpuMemoryBuffer::ATCIA:
55 case GpuMemoryBuffer::DXT1:
56 case GpuMemoryBuffer::DXT5:
57 case GpuMemoryBuffer::ETC1:
58 case GpuMemoryBuffer::RGBA_4444:
59 case GpuMemoryBuffer::RGBA_8888:
60 case GpuMemoryBuffer::RGBX_8888:
61 case GpuMemoryBuffer::YUV_420:
62 NOTREACHED();
63 return 0;
66 NOTREACHED();
67 return 0;
70 GLenum DataFormat(GpuMemoryBuffer::Format format) {
71 switch (format) {
72 case GpuMemoryBuffer::R_8:
73 return GL_RED;
74 case GpuMemoryBuffer::BGRA_8888:
75 return GL_BGRA;
76 case GpuMemoryBuffer::ATC:
77 case GpuMemoryBuffer::ATCIA:
78 case GpuMemoryBuffer::DXT1:
79 case GpuMemoryBuffer::DXT5:
80 case GpuMemoryBuffer::ETC1:
81 case GpuMemoryBuffer::RGBA_4444:
82 case GpuMemoryBuffer::RGBA_8888:
83 case GpuMemoryBuffer::RGBX_8888:
84 case GpuMemoryBuffer::YUV_420:
85 NOTREACHED();
86 return 0;
89 NOTREACHED();
90 return 0;
93 GLenum DataType(GpuMemoryBuffer::Format format) {
94 switch (format) {
95 case GpuMemoryBuffer::R_8:
96 return GL_UNSIGNED_BYTE;
97 case GpuMemoryBuffer::BGRA_8888:
98 return GL_UNSIGNED_INT_8_8_8_8_REV;
99 case GpuMemoryBuffer::ATC:
100 case GpuMemoryBuffer::ATCIA:
101 case GpuMemoryBuffer::DXT1:
102 case GpuMemoryBuffer::DXT5:
103 case GpuMemoryBuffer::ETC1:
104 case GpuMemoryBuffer::RGBA_4444:
105 case GpuMemoryBuffer::RGBA_8888:
106 case GpuMemoryBuffer::RGBX_8888:
107 case GpuMemoryBuffer::YUV_420:
108 NOTREACHED();
109 return 0;
112 NOTREACHED();
113 return 0;
116 } // namespace
118 GLImageIOSurface::GLImageIOSurface(const gfx::Size& size,
119 unsigned internalformat)
120 : size_(size),
121 internalformat_(internalformat),
122 format_(GpuMemoryBuffer::RGBA_8888) {
125 GLImageIOSurface::~GLImageIOSurface() {
126 DCHECK(thread_checker_.CalledOnValidThread());
127 DCHECK(!io_surface_);
130 bool GLImageIOSurface::Initialize(IOSurfaceRef io_surface,
131 GpuMemoryBuffer::Format format) {
132 DCHECK(thread_checker_.CalledOnValidThread());
133 DCHECK(!io_surface_);
135 if (!ValidInternalFormat(internalformat_)) {
136 LOG(ERROR) << "Invalid internalformat: " << internalformat_;
137 return false;
140 if (!ValidFormat(format)) {
141 LOG(ERROR) << "Invalid format: " << format;
142 return false;
145 format_ = format;
146 io_surface_.reset(io_surface, base::scoped_policy::RETAIN);
147 return true;
150 void GLImageIOSurface::Destroy(bool have_context) {
151 DCHECK(thread_checker_.CalledOnValidThread());
152 io_surface_.reset();
155 gfx::Size GLImageIOSurface::GetSize() { return size_; }
157 unsigned GLImageIOSurface::GetInternalFormat() { return internalformat_; }
159 bool GLImageIOSurface::BindTexImage(unsigned target) {
160 DCHECK(thread_checker_.CalledOnValidThread());
161 if (target != GL_TEXTURE_RECTANGLE_ARB) {
162 // This might be supported in the future. For now, perform strict
163 // validation so we know what's going on.
164 LOG(ERROR) << "IOSurface requires TEXTURE_RECTANGLE_ARB target";
165 return false;
168 CGLContextObj cgl_context =
169 static_cast<CGLContextObj>(GLContext::GetCurrent()->GetHandle());
171 DCHECK(io_surface_);
172 CGLError cgl_error =
173 CGLTexImageIOSurface2D(cgl_context, target, TextureFormat(format_),
174 size_.width(), size_.height(), DataFormat(format_),
175 DataType(format_), io_surface_.get(), 0);
176 if (cgl_error != kCGLNoError) {
177 LOG(ERROR) << "Error in CGLTexImageIOSurface2D";
178 return false;
181 return true;
184 bool GLImageIOSurface::CopyTexSubImage(unsigned target,
185 const Point& offset,
186 const Rect& rect) {
187 return false;
190 bool GLImageIOSurface::ScheduleOverlayPlane(gfx::AcceleratedWidget widget,
191 int z_order,
192 OverlayTransform transform,
193 const Rect& bounds_rect,
194 const RectF& crop_rect) {
195 return false;
198 } // namespace gfx