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 "content/common/gpu/client/gl_helper_readback_support.h"
6 #include "base/logging.h"
7 #include "gpu/GLES2/gl2extchromium.h"
8 #include "third_party/skia/include/core/SkImageInfo.h"
12 GLHelperReadbackSupport::GLHelperReadbackSupport(gpu::gles2::GLES2Interface
* gl
)
14 InitializeReadbackSupport();
17 GLHelperReadbackSupport::~GLHelperReadbackSupport() {}
19 void GLHelperReadbackSupport::InitializeReadbackSupport() {
20 // We are concerned about 16, 32-bit formats only. The below are the most
21 // used 16, 32-bit formats. In future if any new format support is needed
22 // that should be added here. Initialize the array with
23 // GLHelperReadbackSupport::NOT_SUPPORTED as we dont know the supported
25 for (int i
= 0; i
<= kLastEnum_SkColorType
; ++i
) {
26 format_support_table_
[i
] = GLHelperReadbackSupport::NOT_SUPPORTED
;
28 CheckForReadbackSupport(kRGB_565_SkColorType
);
29 CheckForReadbackSupport(kARGB_4444_SkColorType
);
30 CheckForReadbackSupport(kRGBA_8888_SkColorType
);
31 CheckForReadbackSupport(kBGRA_8888_SkColorType
);
32 // Further any formats, support should be checked here.
35 void GLHelperReadbackSupport::CheckForReadbackSupport(
36 SkColorType texture_format
) {
37 bool supports_format
= false;
38 switch (texture_format
) {
39 case kRGB_565_SkColorType
:
40 supports_format
= SupportsFormat(GL_RGB
, GL_UNSIGNED_SHORT_5_6_5
);
42 case kRGBA_8888_SkColorType
:
43 // This is the baseline, assume always true.
44 supports_format
= true;
46 case kBGRA_8888_SkColorType
:
47 supports_format
= SupportsFormat(GL_BGRA_EXT
, GL_UNSIGNED_BYTE
);
49 case kARGB_4444_SkColorType
:
50 supports_format
= false;
54 supports_format
= false;
57 DCHECK((int)texture_format
<= (int)kLastEnum_SkColorType
);
58 format_support_table_
[texture_format
] =
59 supports_format
? GLHelperReadbackSupport::SUPPORTED
60 : GLHelperReadbackSupport::NOT_SUPPORTED
;
63 void GLHelperReadbackSupport::GetAdditionalFormat(GLenum format
,
67 for (unsigned int i
= 0; i
< format_cache_
.size(); i
++) {
68 if (format_cache_
[i
].format
== format
&& format_cache_
[i
].type
== type
) {
69 *format_out
= format_cache_
[i
].read_format
;
70 *type_out
= format_cache_
[i
].read_type
;
75 const int kTestSize
= 64;
76 content::ScopedTexture
dst_texture(gl_
);
77 ScopedTextureBinder
<GL_TEXTURE_2D
> texture_binder(gl_
, dst_texture
);
78 gl_
->TexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
79 gl_
->TexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR
);
80 gl_
->TexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
81 gl_
->TexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
83 GL_TEXTURE_2D
, 0, format
, kTestSize
, kTestSize
, 0, format
, type
, NULL
);
84 ScopedFramebuffer
dst_framebuffer(gl_
);
85 ScopedFramebufferBinder
<GL_FRAMEBUFFER
> framebuffer_binder(gl_
,
87 gl_
->FramebufferTexture2D(
88 GL_FRAMEBUFFER
, GL_COLOR_ATTACHMENT0
, GL_TEXTURE_2D
, dst_texture
, 0);
89 GLint format_tmp
= 0, type_tmp
= 0;
90 gl_
->GetIntegerv(GL_IMPLEMENTATION_COLOR_READ_FORMAT
, &format_tmp
);
91 gl_
->GetIntegerv(GL_IMPLEMENTATION_COLOR_READ_TYPE
, &type_tmp
);
92 *format_out
= format_tmp
;
95 struct FormatCacheEntry entry
= { format
, type
, *format_out
, *type_out
};
96 format_cache_
.push_back(entry
);
99 bool GLHelperReadbackSupport::SupportsFormat(GLenum format
, GLenum type
) {
100 // GLES2.0 Specification says this pairing is always supported
101 // with additional format from GL_IMPLEMENTATION_COLOR_READ_FORMAT/TYPE
102 if (format
== GL_RGBA
&& type
== GL_UNSIGNED_BYTE
)
105 if (format
== GL_BGRA_EXT
&& type
== GL_UNSIGNED_BYTE
) {
106 const GLubyte
* tmp
= gl_
->GetString(GL_EXTENSIONS
);
107 std::string extensions
=
108 " " + std::string(reinterpret_cast<const char*>(tmp
)) + " ";
109 if (extensions
.find(" GL_EXT_read_format_bgra ") != std::string::npos
) {
114 bool supports_format
= false;
115 GLenum ext_format
= 0, ext_type
= 0;
116 GetAdditionalFormat(format
, type
, &ext_format
, &ext_type
);
117 if ((ext_format
== format
) && (ext_type
== type
)) {
118 supports_format
= true;
120 return supports_format
;
123 GLHelperReadbackSupport::FormatSupport
124 GLHelperReadbackSupport::GetReadbackConfig(SkColorType color_type
,
128 size_t* bytes_per_pixel
) {
129 DCHECK(format
&& type
&& bytes_per_pixel
);
130 *bytes_per_pixel
= 4;
131 *type
= GL_UNSIGNED_BYTE
;
132 GLenum new_format
= 0, new_type
= 0;
133 switch (color_type
) {
134 case kRGB_565_SkColorType
:
135 if (format_support_table_
[color_type
] ==
136 GLHelperReadbackSupport::SUPPORTED
) {
138 *type
= GL_UNSIGNED_SHORT_5_6_5
;
139 *bytes_per_pixel
= 2;
140 return GLHelperReadbackSupport::SUPPORTED
;
143 case kRGBA_8888_SkColorType
:
146 // If GL_BGRA_EXT is advertised as the readback format through
147 // GL_IMPLEMENTATION_COLOR_READ_FORMAT then assume it is preferred by
148 // the implementation for performance.
149 GetAdditionalFormat(*format
, *type
, &new_format
, &new_type
);
151 if (new_format
== GL_BGRA_EXT
&& new_type
== GL_UNSIGNED_BYTE
) {
152 *format
= GL_BGRA_EXT
;
153 return GLHelperReadbackSupport::SWIZZLE
;
156 return GLHelperReadbackSupport::SUPPORTED
;
157 case kBGRA_8888_SkColorType
:
158 *format
= GL_BGRA_EXT
;
159 if (format_support_table_
[color_type
] ==
160 GLHelperReadbackSupport::SUPPORTED
)
161 return GLHelperReadbackSupport::SUPPORTED
;
165 return GLHelperReadbackSupport::SWIZZLE
;
169 case kARGB_4444_SkColorType
:
170 return GLHelperReadbackSupport::NOT_SUPPORTED
;
176 return GLHelperReadbackSupport::NOT_SUPPORTED
;
179 } // namespace content