| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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 "cc/resources/resource_format.h" | 5 #include "cc/resources/resource_format.h" |
| 6 | 6 |
| 7 #include "third_party/khronos/GLES2/gl2.h" | 7 #include "third_party/khronos/GLES2/gl2.h" |
| 8 #include "third_party/khronos/GLES2/gl2ext.h" | 8 #include "third_party/khronos/GLES2/gl2ext.h" |
| 9 #include "ui/gfx/gpu_memory_buffer.h" | 9 #include "ui/gfx/gpu_memory_buffer.h" |
| 10 | 10 |
| 11 namespace cc { | 11 namespace cc { |
| 12 | 12 |
| 13 int BitsPerPixel(ResourceFormat format) { | 13 int BitsPerPixel(ResourceFormat format) { |
| 14 switch (format) { | 14 switch (format) { |
| 15 case BGRA_8888: | 15 case BGRA_8888: |
| 16 case RGBA_8888: | 16 case RGBA_8888: |
| 17 return 32; | 17 return 32; |
| 18 case RGBA_4444: | 18 case RGBA_4444: |
| 19 case RGB_565: | 19 case RGB_565: |
| 20 case RG_88: |
| 20 case LUMINANCE_F16: | 21 case LUMINANCE_F16: |
| 21 return 16; | 22 return 16; |
| 22 case ALPHA_8: | 23 case ALPHA_8: |
| 23 case LUMINANCE_8: | 24 case LUMINANCE_8: |
| 24 case RED_8: | 25 case RED_8: |
| 25 return 8; | 26 return 8; |
| 26 case ETC1: | 27 case ETC1: |
| 27 return 4; | 28 return 4; |
| 28 } | 29 } |
| 29 NOTREACHED(); | 30 NOTREACHED(); |
| 30 return 0; | 31 return 0; |
| 31 } | 32 } |
| 32 | 33 |
| 33 GLenum GLDataType(ResourceFormat format) { | 34 GLenum GLDataType(ResourceFormat format) { |
| 34 DCHECK_LE(format, RESOURCE_FORMAT_MAX); | 35 DCHECK_LE(format, RESOURCE_FORMAT_MAX); |
| 35 static const GLenum format_gl_data_type[] = { | 36 static const GLenum format_gl_data_type[] = { |
| 36 GL_UNSIGNED_BYTE, // RGBA_8888 | 37 GL_UNSIGNED_BYTE, // RGBA_8888 |
| 37 GL_UNSIGNED_SHORT_4_4_4_4, // RGBA_4444 | 38 GL_UNSIGNED_SHORT_4_4_4_4, // RGBA_4444 |
| 38 GL_UNSIGNED_BYTE, // BGRA_8888 | 39 GL_UNSIGNED_BYTE, // BGRA_8888 |
| 39 GL_UNSIGNED_BYTE, // ALPHA_8 | 40 GL_UNSIGNED_BYTE, // ALPHA_8 |
| 40 GL_UNSIGNED_BYTE, // LUMINANCE_8 | 41 GL_UNSIGNED_BYTE, // LUMINANCE_8 |
| 41 GL_UNSIGNED_SHORT_5_6_5, // RGB_565, | 42 GL_UNSIGNED_SHORT_5_6_5, // RGB_565, |
| 42 GL_UNSIGNED_BYTE, // ETC1 | 43 GL_UNSIGNED_BYTE, // ETC1 |
| 43 GL_UNSIGNED_BYTE, // RED_8 | 44 GL_UNSIGNED_BYTE, // RED_8 |
| 45 GL_UNSIGNED_BYTE, // RG_88 |
| 44 GL_HALF_FLOAT_OES, // LUMINANCE_F16 | 46 GL_HALF_FLOAT_OES, // LUMINANCE_F16 |
| 45 }; | 47 }; |
| 46 static_assert(arraysize(format_gl_data_type) == (RESOURCE_FORMAT_MAX + 1), | 48 static_assert(arraysize(format_gl_data_type) == (RESOURCE_FORMAT_MAX + 1), |
| 47 "format_gl_data_type does not handle all cases."); | 49 "format_gl_data_type does not handle all cases."); |
| 48 | 50 |
| 49 return format_gl_data_type[format]; | 51 return format_gl_data_type[format]; |
| 50 } | 52 } |
| 51 | 53 |
| 52 GLenum GLDataFormat(ResourceFormat format) { | 54 GLenum GLDataFormat(ResourceFormat format) { |
| 53 DCHECK_LE(format, RESOURCE_FORMAT_MAX); | 55 DCHECK_LE(format, RESOURCE_FORMAT_MAX); |
| 54 static const GLenum format_gl_data_format[] = { | 56 static const GLenum format_gl_data_format[] = { |
| 55 GL_RGBA, // RGBA_8888 | 57 GL_RGBA, // RGBA_8888 |
| 56 GL_RGBA, // RGBA_4444 | 58 GL_RGBA, // RGBA_4444 |
| 57 GL_BGRA_EXT, // BGRA_8888 | 59 GL_BGRA_EXT, // BGRA_8888 |
| 58 GL_ALPHA, // ALPHA_8 | 60 GL_ALPHA, // ALPHA_8 |
| 59 GL_LUMINANCE, // LUMINANCE_8 | 61 GL_LUMINANCE, // LUMINANCE_8 |
| 60 GL_RGB, // RGB_565 | 62 GL_RGB, // RGB_565 |
| 61 GL_ETC1_RGB8_OES, // ETC1 | 63 GL_ETC1_RGB8_OES, // ETC1 |
| 62 GL_RED_EXT, // RED_8 | 64 GL_RED_EXT, // RED_8 |
| 65 GL_RG_EXT, // RG_88 |
| 63 GL_LUMINANCE, // LUMINANCE_F16 | 66 GL_LUMINANCE, // LUMINANCE_F16 |
| 64 }; | 67 }; |
| 65 static_assert(arraysize(format_gl_data_format) == (RESOURCE_FORMAT_MAX + 1), | 68 static_assert(arraysize(format_gl_data_format) == (RESOURCE_FORMAT_MAX + 1), |
| 66 "format_gl_data_format does not handle all cases."); | 69 "format_gl_data_format does not handle all cases."); |
| 67 return format_gl_data_format[format]; | 70 return format_gl_data_format[format]; |
| 68 } | 71 } |
| 69 | 72 |
| 70 GLenum GLInternalFormat(ResourceFormat format) { | 73 GLenum GLInternalFormat(ResourceFormat format) { |
| 71 // In GLES2, the internal format must match the texture format. (It no longer | 74 // In GLES2, the internal format must match the texture format. (It no longer |
| 72 // is true in GLES3, however it still holds for the BGRA extension.) | 75 // is true in GLES3, however it still holds for the BGRA extension.) |
| 73 return GLDataFormat(format); | 76 DCHECK_LE(format, RESOURCE_FORMAT_MAX); |
| 77 static const GLenum internal_format_gl_data_format[] = { |
| 78 GL_RGBA, // RGBA_8888 |
| 79 GL_RGBA, // RGBA_4444 |
| 80 GL_BGRA_EXT, // BGRA_8888 |
| 81 GL_ALPHA, // ALPHA_8 |
| 82 GL_LUMINANCE, // LUMINANCE_8 |
| 83 GL_RGB, // RGB_565 |
| 84 GL_ETC1_RGB8_OES, // ETC1 |
| 85 GL_R8_EXT, // RED_8 |
| 86 GL_RG8_EXT, // RG_88 |
| 87 GL_LUMINANCE, // LUMINANCE_F16 |
| 88 }; |
| 89 static_assert( |
| 90 arraysize(internal_format_gl_data_format) == (RESOURCE_FORMAT_MAX + 1), |
| 91 "internal_format_gl_data_format does not handle all cases."); |
| 92 return internal_format_gl_data_format[format]; |
| 74 } | 93 } |
| 75 | 94 |
| 76 GLenum GLCopyTextureInternalFormat(ResourceFormat format) { | 95 GLenum GLCopyTextureInternalFormat(ResourceFormat format) { |
| 77 // In GLES2, valid formats for glCopyTexImage2D are: GL_ALPHA, GL_LUMINANCE, | 96 // In GLES2, valid formats for glCopyTexImage2D are: GL_ALPHA, GL_LUMINANCE, |
| 78 // GL_LUMINANCE_ALPHA, GL_RGB, or GL_RGBA. | 97 // GL_LUMINANCE_ALPHA, GL_RGB, or GL_RGBA. |
| 79 // Extensions typically used for glTexImage2D do not also work for | 98 // Extensions typically used for glTexImage2D do not also work for |
| 80 // glCopyTexImage2D. For instance GL_BGRA_EXT may not be used for | 99 // glCopyTexImage2D. For instance GL_BGRA_EXT may not be used for |
| 81 // anything but gl(Sub)TexImage2D: | 100 // anything but gl(Sub)TexImage2D: |
| 82 // https://www.khronos.org/registry/gles/extensions/EXT/EXT_texture_format_BGR
A8888.txt | 101 // https://www.khronos.org/registry/gles/extensions/EXT/EXT_texture_format_BGR
A8888.txt |
| 83 DCHECK_LE(format, RESOURCE_FORMAT_MAX); | 102 DCHECK_LE(format, RESOURCE_FORMAT_MAX); |
| 84 static const GLenum format_gl_data_format[] = { | 103 static const GLenum format_gl_data_format[] = { |
| 85 GL_RGBA, // RGBA_8888 | 104 GL_RGBA, // RGBA_8888 |
| 86 GL_RGBA, // RGBA_4444 | 105 GL_RGBA, // RGBA_4444 |
| 87 GL_RGBA, // BGRA_8888 | 106 GL_RGBA, // BGRA_8888 |
| 88 GL_ALPHA, // ALPHA_8 | 107 GL_ALPHA, // ALPHA_8 |
| 89 GL_LUMINANCE, // LUMINANCE_8 | 108 GL_LUMINANCE, // LUMINANCE_8 |
| 90 GL_RGB, // RGB_565 | 109 GL_RGB, // RGB_565 |
| 91 GL_RGB, // ETC1 | 110 GL_RGB, // ETC1 |
| 92 GL_LUMINANCE, // RED_8 | 111 GL_R8_EXT, // RED_8 |
| 112 GL_RG8_EXT, // RG_88: |
| 93 GL_LUMINANCE, // LUMINANCE_F16 | 113 GL_LUMINANCE, // LUMINANCE_F16 |
| 94 }; | 114 }; |
| 95 static_assert(arraysize(format_gl_data_format) == (RESOURCE_FORMAT_MAX + 1), | 115 static_assert(arraysize(format_gl_data_format) == (RESOURCE_FORMAT_MAX + 1), |
| 96 "format_gl_data_format does not handle all cases."); | 116 "format_gl_data_format does not handle all cases."); |
| 97 return format_gl_data_format[format]; | 117 return format_gl_data_format[format]; |
| 98 } | 118 } |
| 99 | 119 |
| 100 gfx::BufferFormat BufferFormat(ResourceFormat format) { | 120 gfx::BufferFormat BufferFormat(ResourceFormat format) { |
| 101 switch (format) { | 121 switch (format) { |
| 102 case BGRA_8888: | 122 case BGRA_8888: |
| 103 return gfx::BufferFormat::BGRA_8888; | 123 return gfx::BufferFormat::BGRA_8888; |
| 104 case RED_8: | 124 case RED_8: |
| 105 return gfx::BufferFormat::R_8; | 125 return gfx::BufferFormat::R_8; |
| 126 case RG_88: |
| 127 return gfx::BufferFormat::RG_88; |
| 106 case RGBA_4444: | 128 case RGBA_4444: |
| 107 return gfx::BufferFormat::RGBA_4444; | 129 return gfx::BufferFormat::RGBA_4444; |
| 108 case RGBA_8888: | 130 case RGBA_8888: |
| 109 return gfx::BufferFormat::RGBA_8888; | 131 return gfx::BufferFormat::RGBA_8888; |
| 110 case ETC1: | 132 case ETC1: |
| 111 return gfx::BufferFormat::ETC1; | 133 return gfx::BufferFormat::ETC1; |
| 112 case ALPHA_8: | 134 case ALPHA_8: |
| 113 case LUMINANCE_8: | 135 case LUMINANCE_8: |
| 114 case RGB_565: | 136 case RGB_565: |
| 115 case LUMINANCE_F16: | 137 case LUMINANCE_F16: |
| (...skipping 11 matching lines...) Expand all Loading... |
| 127 switch (format) { | 149 switch (format) { |
| 128 case RGBA_4444: | 150 case RGBA_4444: |
| 129 case RGBA_8888: | 151 case RGBA_8888: |
| 130 case BGRA_8888: | 152 case BGRA_8888: |
| 131 case ALPHA_8: | 153 case ALPHA_8: |
| 132 return true; | 154 return true; |
| 133 case LUMINANCE_8: | 155 case LUMINANCE_8: |
| 134 case RGB_565: | 156 case RGB_565: |
| 135 case ETC1: | 157 case ETC1: |
| 136 case RED_8: | 158 case RED_8: |
| 159 case RG_88: |
| 137 case LUMINANCE_F16: | 160 case LUMINANCE_F16: |
| 138 return false; | 161 return false; |
| 139 } | 162 } |
| 140 NOTREACHED(); | 163 NOTREACHED(); |
| 141 return false; | 164 return false; |
| 142 } | 165 } |
| 143 | 166 |
| 144 } // namespace cc | 167 } // namespace cc |
| OLD | NEW |