Chromium Code Reviews| 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 GLHelperReadbackSupport::NOT_SUPPORTED as we dont |
| 24 // know the | |
| 22 // supported formats yet. | 25 // supported formats yet. |
|
piman
2014/07/23 18:07:38
nit: coalesce with previous line.
| |
| 23 for (int i = 0; i <= kLastEnum_SkColorType; ++i) { | 26 for (int i = 0; i <= kLastEnum_SkColorType; ++i) { |
| 24 format_support_table_[i] = FORMAT_NOT_SUPPORTED; | 27 format_support_table_[i] = GLHelperReadbackSupport::NOT_SUPPORTED; |
| 25 } | 28 } |
| 26 CheckForReadbackSupport(kRGB_565_SkColorType); | 29 CheckForReadbackSupport(kRGB_565_SkColorType); |
| 27 CheckForReadbackSupport(kARGB_4444_SkColorType); | 30 CheckForReadbackSupport(kARGB_4444_SkColorType); |
| 28 CheckForReadbackSupport(kN32_SkColorType); | 31 CheckForReadbackSupport(kRGBA_8888_SkColorType); |
| 32 CheckForReadbackSupport(kBGRA_8888_SkColorType); | |
| 29 // Further any formats, support should be checked here. | 33 // Further any formats, support should be checked here. |
| 30 } | 34 } |
| 31 | 35 |
| 32 void GLHelperReadbackSupport::CheckForReadbackSupport( | 36 void GLHelperReadbackSupport::CheckForReadbackSupport( |
| 33 SkColorType texture_format) { | 37 SkColorType texture_format) { |
| 34 bool supports_format = false; | 38 bool supports_format = false; |
| 35 switch (texture_format) { | 39 switch (texture_format) { |
| 36 case kRGB_565_SkColorType: | 40 case kRGB_565_SkColorType: |
| 37 supports_format = SupportsFormat(GL_RGB, GL_UNSIGNED_SHORT_5_6_5); | 41 supports_format = SupportsFormat(GL_RGB, GL_UNSIGNED_SHORT_5_6_5); |
| 38 break; | 42 break; |
| 39 case kN32_SkColorType: | 43 case kRGBA_8888_SkColorType: |
| 40 // This is the baseline, assume always true. | 44 // This is the baseline, assume always true. |
| 41 supports_format = true; | 45 supports_format = true; |
| 42 break; | 46 break; |
| 47 case kBGRA_8888_SkColorType: | |
| 48 supports_format = SupportsFormat(GL_BGRA_EXT, GL_UNSIGNED_BYTE); | |
| 49 break; | |
| 43 case kARGB_4444_SkColorType: | 50 case kARGB_4444_SkColorType: |
| 44 supports_format = false; | 51 supports_format = false; |
| 45 break; | 52 break; |
| 46 default: | 53 default: |
| 47 NOTREACHED(); | 54 NOTREACHED(); |
| 48 supports_format = false; | 55 supports_format = false; |
| 49 break; | 56 break; |
| 50 } | 57 } |
| 51 DCHECK((int)texture_format <= (int)kLastEnum_SkColorType); | 58 DCHECK((int)texture_format <= (int)kLastEnum_SkColorType); |
| 52 format_support_table_[texture_format] = | 59 format_support_table_[texture_format] = |
| 53 supports_format ? FORMAT_SUPPORTED : FORMAT_NOT_SUPPORTED; | 60 supports_format ? GLHelperReadbackSupport::SUPPORTED |
| 61 : GLHelperReadbackSupport::NOT_SUPPORTED; | |
| 54 } | 62 } |
| 55 | 63 |
| 56 void GLHelperReadbackSupport::GetAdditionalFormat(GLint format, GLint type, | 64 void GLHelperReadbackSupport::GetAdditionalFormat(GLenum format, |
| 57 GLint *format_out, | 65 GLenum type, |
| 58 GLint *type_out) { | 66 GLenum* format_out, |
| 67 GLenum* type_out) { | |
| 59 for (unsigned int i = 0; i < format_cache_.size(); i++) { | 68 for (unsigned int i = 0; i < format_cache_.size(); i++) { |
| 60 if (format_cache_[i].format == format && format_cache_[i].type == type) { | 69 if (format_cache_[i].format == format && format_cache_[i].type == type) { |
| 61 *format_out = format_cache_[i].read_format; | 70 *format_out = format_cache_[i].read_format; |
| 62 *type_out = format_cache_[i].read_type; | 71 *type_out = format_cache_[i].read_type; |
| 63 return; | 72 return; |
| 64 } | 73 } |
| 65 } | 74 } |
| 66 | 75 |
| 67 const int kTestSize = 64; | 76 const int kTestSize = 64; |
| 68 content::ScopedTexture dst_texture(gl_); | 77 content::ScopedTexture dst_texture(gl_); |
| 69 ScopedTextureBinder<GL_TEXTURE_2D> texture_binder(gl_, dst_texture); | 78 ScopedTextureBinder<GL_TEXTURE_2D> texture_binder(gl_, dst_texture); |
| 70 gl_->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); | 79 gl_->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
| 71 gl_->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); | 80 gl_->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 72 gl_->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); | 81 gl_->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 73 gl_->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); | 82 gl_->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| 74 gl_->TexImage2D( | 83 gl_->TexImage2D( |
| 75 GL_TEXTURE_2D, 0, format, kTestSize, kTestSize, 0, format, type, NULL); | 84 GL_TEXTURE_2D, 0, format, kTestSize, kTestSize, 0, format, type, NULL); |
| 76 ScopedFramebuffer dst_framebuffer(gl_); | 85 ScopedFramebuffer dst_framebuffer(gl_); |
| 77 ScopedFramebufferBinder<GL_FRAMEBUFFER> framebuffer_binder(gl_, | 86 ScopedFramebufferBinder<GL_FRAMEBUFFER> framebuffer_binder(gl_, |
| 78 dst_framebuffer); | 87 dst_framebuffer); |
| 79 gl_->FramebufferTexture2D( | 88 gl_->FramebufferTexture2D( |
| 80 GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, dst_texture, 0); | 89 GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, dst_texture, 0); |
| 81 gl_->GetIntegerv(GL_IMPLEMENTATION_COLOR_READ_FORMAT, format_out); | 90 GLint format_tmp = 0, type_tmp = 0; |
| 82 gl_->GetIntegerv(GL_IMPLEMENTATION_COLOR_READ_TYPE, type_out); | 91 gl_->GetIntegerv(GL_IMPLEMENTATION_COLOR_READ_FORMAT, &format_tmp); |
| 92 gl_->GetIntegerv(GL_IMPLEMENTATION_COLOR_READ_TYPE, &type_tmp); | |
| 93 *format_out = format_tmp; | |
| 94 *type_out = type_tmp; | |
| 83 | 95 |
| 84 struct FormatCacheEntry entry = { format, type, *format_out, *type_out }; | 96 struct FormatCacheEntry entry = { format, type, *format_out, *type_out }; |
| 85 format_cache_.push_back(entry); | 97 format_cache_.push_back(entry); |
| 86 } | 98 } |
| 87 | 99 |
| 88 bool GLHelperReadbackSupport::SupportsFormat(GLint format, GLint type) { | 100 bool GLHelperReadbackSupport::SupportsFormat(GLenum format, GLenum type) { |
| 89 // GLES2.0 Specification says this pairing is always supported | 101 // GLES2.0 Specification says this pairing is always supported |
| 90 // with additional format from GL_IMPLEMENTATION_COLOR_READ_FORMAT/TYPE | 102 // with additional format from GL_IMPLEMENTATION_COLOR_READ_FORMAT/TYPE |
| 91 if (format == GL_RGBA && type == GL_UNSIGNED_BYTE) | 103 if (format == GL_RGBA && type == GL_UNSIGNED_BYTE) |
| 92 return true; | 104 return true; |
| 105 | |
| 106 if (format == GL_BGRA_EXT && type == GL_UNSIGNED_BYTE) { | |
| 107 const GLubyte* tmp = gl_->GetString(GL_EXTENSIONS); | |
| 108 std::string extensions = | |
| 109 " " + std::string(reinterpret_cast<const char*>(tmp)) + " "; | |
| 110 if (extensions.find(" GL_EXT_read_format_bgra ") != std::string::npos) { | |
| 111 return true; | |
| 112 } | |
| 113 } | |
| 114 | |
| 93 bool supports_format = false; | 115 bool supports_format = false; |
| 94 GLint ext_format = 0, ext_type = 0; | 116 GLenum ext_format = 0, ext_type = 0; |
| 95 GetAdditionalFormat(format, type, &ext_format, &ext_type); | 117 GetAdditionalFormat(format, type, &ext_format, &ext_type); |
| 96 if ((ext_format == format) && (ext_type == type)) { | 118 if ((ext_format == format) && (ext_type == type)) { |
| 97 supports_format = true; | 119 supports_format = true; |
| 98 } | 120 } |
| 99 return supports_format; | 121 return supports_format; |
| 100 } | 122 } |
| 101 | 123 |
| 102 bool GLHelperReadbackSupport::IsReadbackConfigSupported( | 124 GLHelperReadbackSupport::FormatSupport |
| 103 SkColorType texture_format) { | 125 GLHelperReadbackSupport::GetReadbackConfig(SkColorType color_type, |
| 104 switch (format_support_table_[texture_format]) { | 126 bool can_swizzle, |
| 105 case FORMAT_SUPPORTED: | 127 GLenum* format, |
| 106 return true; | 128 GLenum* type, |
| 107 case FORMAT_NOT_SUPPORTED: | 129 size_t* bytes_per_pixel) { |
| 108 return false; | 130 DCHECK(format && type && bytes_per_pixel); |
| 131 *bytes_per_pixel = 4; | |
| 132 *type = GL_UNSIGNED_BYTE; | |
| 133 GLenum new_format = 0, new_type = 0; | |
| 134 switch (color_type) { | |
| 135 case kRGB_565_SkColorType: | |
| 136 if (format_support_table_[color_type] == | |
| 137 GLHelperReadbackSupport::SUPPORTED) { | |
| 138 *format = GL_RGB; | |
| 139 *type = GL_UNSIGNED_SHORT_5_6_5; | |
| 140 *bytes_per_pixel = 2; | |
| 141 return GLHelperReadbackSupport::SUPPORTED; | |
| 142 } | |
| 143 break; | |
| 144 case kRGBA_8888_SkColorType: | |
| 145 *format = GL_RGBA; | |
| 146 if (can_swizzle) { | |
| 147 // Handle preference for readback in GL_BGRA_EXT | |
| 148 GetAdditionalFormat(*format, *type, &new_format, &new_type); | |
| 149 | |
| 150 if (new_format == GL_BGRA_EXT && new_type == GL_UNSIGNED_BYTE) { | |
| 151 *format = GL_BGRA_EXT; | |
| 152 return GLHelperReadbackSupport::SWIZZLE; | |
| 153 } | |
| 154 } | |
| 155 return GLHelperReadbackSupport::SUPPORTED; | |
| 156 case kBGRA_8888_SkColorType: | |
| 157 *format = GL_BGRA_EXT; | |
| 158 if (format_support_table_[color_type] == | |
| 159 GLHelperReadbackSupport::SUPPORTED) | |
| 160 return GLHelperReadbackSupport::SUPPORTED; | |
| 161 | |
| 162 if (can_swizzle) { | |
| 163 *format = GL_RGBA; | |
| 164 return GLHelperReadbackSupport::SWIZZLE; | |
| 165 } | |
| 166 | |
| 167 break; | |
| 168 case kARGB_4444_SkColorType: | |
| 169 return GLHelperReadbackSupport::NOT_SUPPORTED; | |
| 109 default: | 170 default: |
| 110 NOTREACHED(); | 171 NOTREACHED(); |
| 111 return false; | 172 break; |
| 112 } | 173 } |
| 174 | |
| 175 return GLHelperReadbackSupport::NOT_SUPPORTED; | |
| 113 } | 176 } |
| 114 | 177 |
| 115 } // namespace content | 178 } // namespace content |
| OLD | NEW |