OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 "content/common/gpu/client/gl_helper_readback_support.h" | 5 #include "content/common/gpu/client/gl_helper_readback_support.h" |
6 #include "base/logging.h" | 6 #include "base/logging.h" |
7 #include "gpu/GLES2/gl2extchromium.h" | |
8 #include "third_party/skia/include/core/SkImageInfo.h" | |
7 | 9 |
8 namespace content { | 10 namespace content { |
9 | 11 |
10 GLHelperReadbackSupport::GLHelperReadbackSupport(gpu::gles2::GLES2Interface* gl) | 12 GLHelperReadbackSupport::GLHelperReadbackSupport(gpu::gles2::GLES2Interface* gl) |
11 : gl_(gl) { | 13 : gl_(gl) { |
12 InitializeReadbackSupport(); | 14 InitializeReadbackSupport(); |
13 } | 15 } |
14 | 16 |
15 GLHelperReadbackSupport::~GLHelperReadbackSupport() {} | 17 GLHelperReadbackSupport::~GLHelperReadbackSupport() {} |
16 | 18 |
17 void GLHelperReadbackSupport::InitializeReadbackSupport() { | 19 void GLHelperReadbackSupport::InitializeReadbackSupport() { |
18 // We are concerned about 16, 32-bit formats only. | 20 // We are concerned about 16, 32-bit formats only. |
19 // The below are the most used 16, 32-bit formats. | 21 // The below are the most used 16, 32-bit formats. |
20 // In future if any new format support is needed that should be added here. | 22 // In future if any new format support is needed that should be added here. |
21 // Initialize the array with FORMAT_NOT_SUPPORTED as we dont know the | 23 // Initialize the array with kReadbackNotSupported as we dont know the |
22 // supported formats yet. | 24 // supported formats yet. |
23 for (int i = 0; i <= kLastEnum_SkColorType; ++i) { | 25 for (int i = 0; i <= kLastEnum_SkColorType; ++i) { |
24 format_support_table_[i] = FORMAT_NOT_SUPPORTED; | 26 format_support_table_[i] = kReadbackNotSupported; |
25 } | 27 } |
26 CheckForReadbackSupport(kRGB_565_SkColorType); | 28 CheckForReadbackSupport(kRGB_565_SkColorType); |
27 CheckForReadbackSupport(kARGB_4444_SkColorType); | 29 CheckForReadbackSupport(kARGB_4444_SkColorType); |
28 CheckForReadbackSupport(kN32_SkColorType); | 30 CheckForReadbackSupport(kRGBA_8888_SkColorType); |
31 CheckForReadbackSupport(kBGRA_8888_SkColorType); | |
29 // Further any formats, support should be checked here. | 32 // Further any formats, support should be checked here. |
30 } | 33 } |
31 | 34 |
32 void GLHelperReadbackSupport::CheckForReadbackSupport( | 35 void GLHelperReadbackSupport::CheckForReadbackSupport( |
33 SkColorType texture_format) { | 36 SkColorType texture_format) { |
34 bool supports_format = false; | 37 bool supports_format = false; |
35 switch (texture_format) { | 38 switch (texture_format) { |
36 case kRGB_565_SkColorType: | 39 case kRGB_565_SkColorType: |
37 supports_format = SupportsFormat(GL_RGB, GL_UNSIGNED_SHORT_5_6_5); | 40 supports_format = SupportsFormat(GL_RGB, GL_UNSIGNED_SHORT_5_6_5); |
38 break; | 41 break; |
39 case kN32_SkColorType: | 42 case kRGBA_8888_SkColorType: |
40 // This is the baseline, assume always true. | 43 // This is the baseline, assume always true. |
41 supports_format = true; | 44 supports_format = true; |
42 break; | 45 break; |
46 case kBGRA_8888_SkColorType: | |
47 supports_format = SupportsFormat(GL_BGRA_EXT, GL_UNSIGNED_BYTE); | |
48 break; | |
43 case kARGB_4444_SkColorType: | 49 case kARGB_4444_SkColorType: |
44 supports_format = false; | 50 supports_format = false; |
45 break; | 51 break; |
46 default: | 52 default: |
47 NOTREACHED(); | 53 NOTREACHED(); |
48 supports_format = false; | 54 supports_format = false; |
49 break; | 55 break; |
50 } | 56 } |
51 DCHECK((int)texture_format <= (int)kLastEnum_SkColorType); | 57 DCHECK((int)texture_format <= (int)kLastEnum_SkColorType); |
52 format_support_table_[texture_format] = | 58 format_support_table_[texture_format] = |
53 supports_format ? FORMAT_SUPPORTED : FORMAT_NOT_SUPPORTED; | 59 supports_format ? kReadbackSupported : kReadbackNotSupported; |
54 } | 60 } |
55 | 61 |
56 void GLHelperReadbackSupport::GetAdditionalFormat(GLint format, GLint type, | 62 void GLHelperReadbackSupport::GetAdditionalFormat(GLint format, GLint type, |
57 GLint *format_out, | 63 GLint *format_out, |
58 GLint *type_out) { | 64 GLint *type_out) { |
59 for (unsigned int i = 0; i < format_cache_.size(); i++) { | 65 for (unsigned int i = 0; i < format_cache_.size(); i++) { |
60 if (format_cache_[i].format == format && format_cache_[i].type == type) { | 66 if (format_cache_[i].format == format && format_cache_[i].type == type) { |
61 *format_out = format_cache_[i].read_format; | 67 *format_out = format_cache_[i].read_format; |
62 *type_out = format_cache_[i].read_type; | 68 *type_out = format_cache_[i].read_type; |
63 return; | 69 return; |
(...skipping 14 matching lines...) Expand all Loading... | |
78 dst_framebuffer); | 84 dst_framebuffer); |
79 gl_->FramebufferTexture2D( | 85 gl_->FramebufferTexture2D( |
80 GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, dst_texture, 0); | 86 GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, dst_texture, 0); |
81 gl_->GetIntegerv(GL_IMPLEMENTATION_COLOR_READ_FORMAT, format_out); | 87 gl_->GetIntegerv(GL_IMPLEMENTATION_COLOR_READ_FORMAT, format_out); |
82 gl_->GetIntegerv(GL_IMPLEMENTATION_COLOR_READ_TYPE, type_out); | 88 gl_->GetIntegerv(GL_IMPLEMENTATION_COLOR_READ_TYPE, type_out); |
83 | 89 |
84 struct FormatCacheEntry entry = { format, type, *format_out, *type_out }; | 90 struct FormatCacheEntry entry = { format, type, *format_out, *type_out }; |
85 format_cache_.push_back(entry); | 91 format_cache_.push_back(entry); |
86 } | 92 } |
87 | 93 |
88 bool GLHelperReadbackSupport::SupportsFormat(GLint format, GLint type) { | 94 bool GLHelperReadbackSupport::SupportsFormat(GLint format, GLint type) { |
robert.bradford
2014/07/23 16:58:45
I also needed to change this to GLenum too.
| |
89 // GLES2.0 Specification says this pairing is always supported | 95 // GLES2.0 Specification says this pairing is always supported |
90 // with additional format from GL_IMPLEMENTATION_COLOR_READ_FORMAT/TYPE | 96 // with additional format from GL_IMPLEMENTATION_COLOR_READ_FORMAT/TYPE |
91 if (format == GL_RGBA && type == GL_UNSIGNED_BYTE) | 97 if (format == GL_RGBA && type == GL_UNSIGNED_BYTE) |
92 return true; | 98 return true; |
99 | |
100 if (format == GL_BGRA_EXT && type == GL_UNSIGNED_BYTE) { | |
101 const GLubyte* tmp = gl_->GetString(GL_EXTENSIONS); | |
102 std::string extensions = | |
103 " " + std::string(reinterpret_cast<const char*>(tmp)) + " "; | |
104 if (extensions.find(" GL_EXT_read_format_bgra ") != std::string::npos) { | |
105 return true; | |
106 } | |
107 } | |
108 | |
93 bool supports_format = false; | 109 bool supports_format = false; |
94 GLint ext_format = 0, ext_type = 0; | 110 GLint ext_format = 0, ext_type = 0; |
95 GetAdditionalFormat(format, type, &ext_format, &ext_type); | 111 GetAdditionalFormat(format, type, &ext_format, &ext_type); |
96 if ((ext_format == format) && (ext_type == type)) { | 112 if ((ext_format == format) && (ext_type == type)) { |
97 supports_format = true; | 113 supports_format = true; |
98 } | 114 } |
99 return supports_format; | 115 return supports_format; |
100 } | 116 } |
101 | 117 |
102 bool GLHelperReadbackSupport::IsReadbackConfigSupported( | 118 FormatSupport GLHelperReadbackSupport::ReadbackConfig( |
103 SkColorType texture_format) { | 119 SkColorType color_type, |
104 switch (format_support_table_[texture_format]) { | 120 const bool& can_swizzle, |
105 case FORMAT_SUPPORTED: | 121 GLint& format, |
106 return true; | 122 GLint& type, |
107 case FORMAT_NOT_SUPPORTED: | 123 int32& bytes_per_pixel) { |
108 return false; | 124 bytes_per_pixel = 4; |
125 type = GL_UNSIGNED_BYTE; | |
126 GLint new_format = 0, new_type = 0; | |
piman
2014/07/22 19:57:55
nit: move these into the scope where they are need
robert.bradford
2014/07/23 16:58:45
Done.
| |
127 switch (color_type) { | |
128 case kRGB_565_SkColorType: | |
129 if (format_support_table_[color_type] == kReadbackSupported) { | |
130 format = GL_RGB; | |
131 type = GL_UNSIGNED_SHORT_5_6_5; | |
132 bytes_per_pixel = 2; | |
133 return kReadbackSupported; | |
134 } | |
135 break; | |
136 case kRGBA_8888_SkColorType: | |
137 format = GL_RGBA; | |
138 if (can_swizzle) { | |
139 // Handle preference for readback in GL_BGRA_EXT | |
140 GetAdditionalFormat(format, type, &new_format, &new_type); | |
piman
2014/07/22 19:57:55
Why do we need this path, given that we can always
robert.bradford
2014/07/23 16:58:45
This is to handle the case (Intel) where the prefe
piman
2014/07/23 18:07:38
Ok, can you add a comment to that effect, that we
no sievers
2014/08/21 19:41:08
Just wondering.... why do you assume that the addi
piman
2014/08/21 20:00:02
Well, the spec doesn't say anything, but the inten
| |
141 | |
142 if (new_format == GL_BGRA_EXT && new_type == GL_UNSIGNED_BYTE) { | |
143 format = GL_BGRA_EXT; | |
144 return kReadbackSwizzle; | |
145 } | |
146 } | |
147 return kReadbackSupported; | |
148 case kBGRA_8888_SkColorType: | |
149 format = GL_BGRA_EXT; | |
150 if (format_support_table_[color_type] == kReadbackSupported) | |
151 return kReadbackSupported; | |
152 | |
153 if (can_swizzle) { | |
154 format = GL_RGBA; | |
155 return kReadbackSwizzle; | |
156 } | |
157 | |
158 break; | |
159 case kARGB_4444_SkColorType: | |
160 return kReadbackNotSupported; | |
109 default: | 161 default: |
110 NOTREACHED(); | 162 NOTREACHED(); |
111 return false; | 163 break; |
112 } | 164 } |
165 | |
166 return kReadbackNotSupported; | |
113 } | 167 } |
114 | 168 |
115 } // namespace content | 169 } // namespace content |
OLD | NEW |