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 "ui/gl/gl_image_memory.h" | 5 #include "ui/gl/gl_image_memory.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/trace_event/trace_event.h" | 8 #include "base/trace_event/trace_event.h" |
9 #include "ui/gl/gl_bindings.h" | 9 #include "ui/gl/gl_bindings.h" |
10 #include "ui/gl/scoped_binders.h" | 10 #include "ui/gl/scoped_binders.h" |
(...skipping 15 matching lines...) Expand all Loading... |
26 case GL_ETC1_RGB8_OES: | 26 case GL_ETC1_RGB8_OES: |
27 case GL_R8: | 27 case GL_R8: |
28 case GL_RGBA: | 28 case GL_RGBA: |
29 case GL_BGRA_EXT: | 29 case GL_BGRA_EXT: |
30 return true; | 30 return true; |
31 default: | 31 default: |
32 return false; | 32 return false; |
33 } | 33 } |
34 } | 34 } |
35 | 35 |
36 bool ValidFormat(GpuMemoryBuffer::Format format) { | 36 bool ValidFormat(BufferFormat format) { |
37 switch (format) { | 37 switch (format) { |
38 case GpuMemoryBuffer::ATC: | 38 case BufferFormat::ATC: |
39 case GpuMemoryBuffer::ATCIA: | 39 case BufferFormat::ATCIA: |
40 case GpuMemoryBuffer::DXT1: | 40 case BufferFormat::DXT1: |
41 case GpuMemoryBuffer::DXT5: | 41 case BufferFormat::DXT5: |
42 case GpuMemoryBuffer::ETC1: | 42 case BufferFormat::ETC1: |
43 case GpuMemoryBuffer::R_8: | 43 case BufferFormat::R_8: |
44 case GpuMemoryBuffer::RGBA_4444: | 44 case BufferFormat::RGBA_4444: |
45 case GpuMemoryBuffer::RGBA_8888: | 45 case BufferFormat::RGBA_8888: |
46 case GpuMemoryBuffer::BGRA_8888: | 46 case BufferFormat::BGRA_8888: |
47 return true; | 47 return true; |
48 case GpuMemoryBuffer::RGBX_8888: | 48 case BufferFormat::RGBX_8888: |
49 case GpuMemoryBuffer::YUV_420: | 49 case BufferFormat::YUV_420: |
50 return false; | 50 return false; |
51 } | 51 } |
52 | 52 |
53 NOTREACHED(); | 53 NOTREACHED(); |
54 return false; | 54 return false; |
55 } | 55 } |
56 | 56 |
57 bool IsCompressedFormat(GpuMemoryBuffer::Format format) { | 57 bool IsCompressedFormat(BufferFormat format) { |
58 switch (format) { | 58 switch (format) { |
59 case GpuMemoryBuffer::ATC: | 59 case BufferFormat::ATC: |
60 case GpuMemoryBuffer::ATCIA: | 60 case BufferFormat::ATCIA: |
61 case GpuMemoryBuffer::DXT1: | 61 case BufferFormat::DXT1: |
62 case GpuMemoryBuffer::DXT5: | 62 case BufferFormat::DXT5: |
63 case GpuMemoryBuffer::ETC1: | 63 case BufferFormat::ETC1: |
64 case GpuMemoryBuffer::YUV_420: | 64 case BufferFormat::YUV_420: |
65 return true; | 65 return true; |
66 case GpuMemoryBuffer::R_8: | 66 case BufferFormat::R_8: |
67 case GpuMemoryBuffer::RGBA_4444: | 67 case BufferFormat::RGBA_4444: |
68 case GpuMemoryBuffer::RGBA_8888: | 68 case BufferFormat::RGBA_8888: |
69 case GpuMemoryBuffer::BGRA_8888: | 69 case BufferFormat::BGRA_8888: |
70 case GpuMemoryBuffer::RGBX_8888: | 70 case BufferFormat::RGBX_8888: |
71 return false; | 71 return false; |
72 } | 72 } |
73 | 73 |
74 NOTREACHED(); | 74 NOTREACHED(); |
75 return false; | 75 return false; |
76 } | 76 } |
77 | 77 |
78 GLenum TextureFormat(GpuMemoryBuffer::Format format) { | 78 GLenum TextureFormat(BufferFormat format) { |
79 switch (format) { | 79 switch (format) { |
80 case GpuMemoryBuffer::ATC: | 80 case BufferFormat::ATC: |
81 return GL_ATC_RGB_AMD; | 81 return GL_ATC_RGB_AMD; |
82 case GpuMemoryBuffer::ATCIA: | 82 case BufferFormat::ATCIA: |
83 return GL_ATC_RGBA_INTERPOLATED_ALPHA_AMD; | 83 return GL_ATC_RGBA_INTERPOLATED_ALPHA_AMD; |
84 case GpuMemoryBuffer::DXT1: | 84 case BufferFormat::DXT1: |
85 return GL_COMPRESSED_RGB_S3TC_DXT1_EXT; | 85 return GL_COMPRESSED_RGB_S3TC_DXT1_EXT; |
86 case GpuMemoryBuffer::DXT5: | 86 case BufferFormat::DXT5: |
87 return GL_COMPRESSED_RGBA_S3TC_DXT5_EXT; | 87 return GL_COMPRESSED_RGBA_S3TC_DXT5_EXT; |
88 case GpuMemoryBuffer::ETC1: | 88 case BufferFormat::ETC1: |
89 return GL_ETC1_RGB8_OES; | 89 return GL_ETC1_RGB8_OES; |
90 case GpuMemoryBuffer::R_8: | 90 case BufferFormat::R_8: |
91 return GL_RED; | 91 return GL_RED; |
92 case GpuMemoryBuffer::RGBA_4444: | 92 case BufferFormat::RGBA_4444: |
93 case GpuMemoryBuffer::RGBA_8888: | 93 case BufferFormat::RGBA_8888: |
94 return GL_RGBA; | 94 return GL_RGBA; |
95 case GpuMemoryBuffer::BGRA_8888: | 95 case BufferFormat::BGRA_8888: |
96 return GL_BGRA_EXT; | 96 return GL_BGRA_EXT; |
97 case GpuMemoryBuffer::RGBX_8888: | 97 case BufferFormat::RGBX_8888: |
98 case GpuMemoryBuffer::YUV_420: | 98 case BufferFormat::YUV_420: |
99 NOTREACHED(); | 99 NOTREACHED(); |
100 return 0; | 100 return 0; |
101 } | 101 } |
102 | 102 |
103 NOTREACHED(); | 103 NOTREACHED(); |
104 return 0; | 104 return 0; |
105 } | 105 } |
106 | 106 |
107 GLenum DataFormat(GpuMemoryBuffer::Format format) { | 107 GLenum DataFormat(BufferFormat format) { |
108 return TextureFormat(format); | 108 return TextureFormat(format); |
109 } | 109 } |
110 | 110 |
111 GLenum DataType(GpuMemoryBuffer::Format format) { | 111 GLenum DataType(BufferFormat format) { |
112 switch (format) { | 112 switch (format) { |
113 case GpuMemoryBuffer::RGBA_4444: | 113 case BufferFormat::RGBA_4444: |
114 return GL_UNSIGNED_SHORT_4_4_4_4; | 114 return GL_UNSIGNED_SHORT_4_4_4_4; |
115 case GpuMemoryBuffer::RGBA_8888: | 115 case BufferFormat::RGBA_8888: |
116 case GpuMemoryBuffer::BGRA_8888: | 116 case BufferFormat::BGRA_8888: |
117 case GpuMemoryBuffer::R_8: | 117 case BufferFormat::R_8: |
118 return GL_UNSIGNED_BYTE; | 118 return GL_UNSIGNED_BYTE; |
119 case GpuMemoryBuffer::ATC: | 119 case BufferFormat::ATC: |
120 case GpuMemoryBuffer::ATCIA: | 120 case BufferFormat::ATCIA: |
121 case GpuMemoryBuffer::DXT1: | 121 case BufferFormat::DXT1: |
122 case GpuMemoryBuffer::DXT5: | 122 case BufferFormat::DXT5: |
123 case GpuMemoryBuffer::ETC1: | 123 case BufferFormat::ETC1: |
124 case GpuMemoryBuffer::RGBX_8888: | 124 case BufferFormat::RGBX_8888: |
125 case GpuMemoryBuffer::YUV_420: | 125 case BufferFormat::YUV_420: |
126 NOTREACHED(); | 126 NOTREACHED(); |
127 return 0; | 127 return 0; |
128 } | 128 } |
129 | 129 |
130 NOTREACHED(); | 130 NOTREACHED(); |
131 return 0; | 131 return 0; |
132 } | 132 } |
133 | 133 |
134 GLsizei SizeInBytes(const Size& size, | 134 GLsizei SizeInBytes(const Size& size, BufferFormat format) { |
135 GpuMemoryBuffer::Format format) { | |
136 size_t stride_in_bytes = 0; | 135 size_t stride_in_bytes = 0; |
137 bool valid_stride = GLImageMemory::StrideInBytes( | 136 bool valid_stride = GLImageMemory::StrideInBytes( |
138 size.width(), format, &stride_in_bytes); | 137 size.width(), format, &stride_in_bytes); |
139 DCHECK(valid_stride); | 138 DCHECK(valid_stride); |
140 return static_cast<GLsizei>(stride_in_bytes * size.height()); | 139 return static_cast<GLsizei>(stride_in_bytes * size.height()); |
141 } | 140 } |
142 | 141 |
143 } // namespace | 142 } // namespace |
144 | 143 |
145 GLImageMemory::GLImageMemory(const Size& size, unsigned internalformat) | 144 GLImageMemory::GLImageMemory(const Size& size, unsigned internalformat) |
146 : size_(size), | 145 : size_(size), |
147 internalformat_(internalformat), | 146 internalformat_(internalformat), |
148 memory_(NULL), | 147 memory_(NULL), |
149 format_(GpuMemoryBuffer::RGBA_8888), | 148 format_(BufferFormat::RGBA_8888), |
150 in_use_(false), | 149 in_use_(false), |
151 target_(0), | 150 target_(0), |
152 need_do_bind_tex_image_(false) | 151 need_do_bind_tex_image_(false) |
153 #if defined(OS_WIN) || defined(USE_X11) || defined(OS_ANDROID) || \ | 152 #if defined(OS_WIN) || defined(USE_X11) || defined(OS_ANDROID) || \ |
154 defined(USE_OZONE) | 153 defined(USE_OZONE) |
155 , | 154 , |
156 egl_texture_id_(0u), | 155 egl_texture_id_(0u), |
157 egl_image_(EGL_NO_IMAGE_KHR) | 156 egl_image_(EGL_NO_IMAGE_KHR) |
158 #endif | 157 #endif |
159 { | 158 { |
160 } | 159 } |
161 | 160 |
162 GLImageMemory::~GLImageMemory() { | 161 GLImageMemory::~GLImageMemory() { |
163 #if defined(OS_WIN) || defined(USE_X11) || defined(OS_ANDROID) || \ | 162 #if defined(OS_WIN) || defined(USE_X11) || defined(OS_ANDROID) || \ |
164 defined(USE_OZONE) | 163 defined(USE_OZONE) |
165 DCHECK_EQ(EGL_NO_IMAGE_KHR, egl_image_); | 164 DCHECK_EQ(EGL_NO_IMAGE_KHR, egl_image_); |
166 DCHECK_EQ(0u, egl_texture_id_); | 165 DCHECK_EQ(0u, egl_texture_id_); |
167 #endif | 166 #endif |
168 } | 167 } |
169 | 168 |
170 // static | 169 // static |
171 bool GLImageMemory::StrideInBytes(size_t width, | 170 bool GLImageMemory::StrideInBytes(size_t width, |
172 GpuMemoryBuffer::Format format, | 171 BufferFormat format, |
173 size_t* stride_in_bytes) { | 172 size_t* stride_in_bytes) { |
174 base::CheckedNumeric<size_t> checked_stride = width; | 173 base::CheckedNumeric<size_t> checked_stride = width; |
175 switch (format) { | 174 switch (format) { |
176 case GpuMemoryBuffer::ATCIA: | 175 case BufferFormat::ATCIA: |
177 case GpuMemoryBuffer::DXT5: | 176 case BufferFormat::DXT5: |
178 *stride_in_bytes = width; | 177 *stride_in_bytes = width; |
179 return true; | 178 return true; |
180 case GpuMemoryBuffer::ATC: | 179 case BufferFormat::ATC: |
181 case GpuMemoryBuffer::DXT1: | 180 case BufferFormat::DXT1: |
182 case GpuMemoryBuffer::ETC1: | 181 case BufferFormat::ETC1: |
183 DCHECK_EQ(width % 2, 0u); | 182 DCHECK_EQ(width % 2, 0u); |
184 *stride_in_bytes = width / 2; | 183 *stride_in_bytes = width / 2; |
185 return true; | 184 return true; |
186 case GpuMemoryBuffer::R_8: | 185 case BufferFormat::R_8: |
187 checked_stride += 3; | 186 checked_stride += 3; |
188 if (!checked_stride.IsValid()) | 187 if (!checked_stride.IsValid()) |
189 return false; | 188 return false; |
190 *stride_in_bytes = checked_stride.ValueOrDie() & ~0x3; | 189 *stride_in_bytes = checked_stride.ValueOrDie() & ~0x3; |
191 return true; | 190 return true; |
192 case GpuMemoryBuffer::RGBA_4444: | 191 case BufferFormat::RGBA_4444: |
193 checked_stride *= 2; | 192 checked_stride *= 2; |
194 if (!checked_stride.IsValid()) | 193 if (!checked_stride.IsValid()) |
195 return false; | 194 return false; |
196 *stride_in_bytes = checked_stride.ValueOrDie(); | 195 *stride_in_bytes = checked_stride.ValueOrDie(); |
197 return true; | 196 return true; |
198 case GpuMemoryBuffer::RGBA_8888: | 197 case BufferFormat::RGBA_8888: |
199 case GpuMemoryBuffer::BGRA_8888: | 198 case BufferFormat::BGRA_8888: |
200 checked_stride *= 4; | 199 checked_stride *= 4; |
201 if (!checked_stride.IsValid()) | 200 if (!checked_stride.IsValid()) |
202 return false; | 201 return false; |
203 *stride_in_bytes = checked_stride.ValueOrDie(); | 202 *stride_in_bytes = checked_stride.ValueOrDie(); |
204 return true; | 203 return true; |
205 case GpuMemoryBuffer::RGBX_8888: | 204 case BufferFormat::RGBX_8888: |
206 case GpuMemoryBuffer::YUV_420: | 205 case BufferFormat::YUV_420: |
207 NOTREACHED(); | 206 NOTREACHED(); |
208 return false; | 207 return false; |
209 } | 208 } |
210 | 209 |
211 NOTREACHED(); | 210 NOTREACHED(); |
212 return false; | 211 return false; |
213 } | 212 } |
214 | 213 |
215 bool GLImageMemory::Initialize(const unsigned char* memory, | 214 bool GLImageMemory::Initialize(const unsigned char* memory, |
216 GpuMemoryBuffer::Format format) { | 215 BufferFormat format) { |
217 if (!ValidInternalFormat(internalformat_)) { | 216 if (!ValidInternalFormat(internalformat_)) { |
218 LOG(ERROR) << "Invalid internalformat: " << internalformat_; | 217 LOG(ERROR) << "Invalid internalformat: " << internalformat_; |
219 return false; | 218 return false; |
220 } | 219 } |
221 | 220 |
222 if (!ValidFormat(format)) { | 221 if (!ValidFormat(format)) { |
223 LOG(ERROR) << "Invalid format: " << format; | 222 LOG(ERROR) << "Invalid format: " << static_cast<int>(format); |
224 return false; | 223 return false; |
225 } | 224 } |
226 | 225 |
227 DCHECK(memory); | 226 DCHECK(memory); |
228 DCHECK(!memory_); | 227 DCHECK(!memory_); |
229 DCHECK_IMPLIES(IsCompressedFormat(format), size_.width() % 4 == 0); | 228 DCHECK_IMPLIES(IsCompressedFormat(format), size_.width() % 4 == 0); |
230 DCHECK_IMPLIES(IsCompressedFormat(format), size_.height() % 4 == 0); | 229 DCHECK_IMPLIES(IsCompressedFormat(format), size_.height() % 4 == 0); |
231 memory_ = memory; | 230 memory_ = memory; |
232 format_ = format; | 231 format_ = format; |
233 return true; | 232 return true; |
(...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
433 size_.width(), | 432 size_.width(), |
434 size_.height(), | 433 size_.height(), |
435 0, // border | 434 0, // border |
436 DataFormat(format_), | 435 DataFormat(format_), |
437 DataType(format_), | 436 DataType(format_), |
438 memory_); | 437 memory_); |
439 } | 438 } |
440 } | 439 } |
441 | 440 |
442 } // namespace gfx | 441 } // namespace gfx |
OLD | NEW |