Backed out changeset 58dbd2146e24 (bug 944961) for bustage.
[gecko.git] / content / canvas / src / WebGLTexelConversions.h
blob85e49624d062d54d3e5501109d0ef294dc4543a1
1 /*
2 * Copyright (C) 2010 Apple Inc. All rights reserved.
3 * Copyright (C) 2010 Google Inc. All rights reserved.
4 * Copyright (C) 2010 Mozilla Corporation. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
16 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
19 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
23 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #ifndef WEBGLTEXELCONVERSIONS_H_
29 #define WEBGLTEXELCONVERSIONS_H_
31 #ifdef __SUNPRO_CC
32 #define __restrict
33 #endif
35 #include "WebGLTypes.h"
36 #include <stdint.h>
37 #include "mozilla/Attributes.h"
39 namespace mozilla {
41 MOZ_BEGIN_ENUM_CLASS(WebGLTexelPremultiplicationOp, int)
42 None,
43 Premultiply,
44 Unpremultiply
45 MOZ_END_ENUM_CLASS(WebGLTexelPremultiplicationOp)
47 namespace WebGLTexelConversions {
49 template<MOZ_ENUM_CLASS_ENUM_TYPE(WebGLTexelFormat) Format>
50 struct IsFloatFormat
52 static const bool Value =
53 Format == WebGLTexelFormat::RGBA32F ||
54 Format == WebGLTexelFormat::RGB32F ||
55 Format == WebGLTexelFormat::RA32F ||
56 Format == WebGLTexelFormat::R32F ||
57 Format == WebGLTexelFormat::A32F;
60 template<MOZ_ENUM_CLASS_ENUM_TYPE(WebGLTexelFormat) Format>
61 struct Is16bppFormat
63 static const bool Value =
64 Format == WebGLTexelFormat::RGBA4444 ||
65 Format == WebGLTexelFormat::RGBA5551 ||
66 Format == WebGLTexelFormat::RGB565;
69 template<MOZ_ENUM_CLASS_ENUM_TYPE(WebGLTexelFormat) Format,
70 bool IsFloat = IsFloatFormat<Format>::Value,
71 bool Is16bpp = Is16bppFormat<Format>::Value>
72 struct DataTypeForFormat
74 typedef uint8_t Type;
77 template<MOZ_ENUM_CLASS_ENUM_TYPE(WebGLTexelFormat) Format>
78 struct DataTypeForFormat<Format, true, false>
80 typedef float Type;
83 template<MOZ_ENUM_CLASS_ENUM_TYPE(WebGLTexelFormat) Format>
84 struct DataTypeForFormat<Format, false, true>
86 typedef uint16_t Type;
89 template<MOZ_ENUM_CLASS_ENUM_TYPE(WebGLTexelFormat) Format>
90 struct IntermediateFormat
92 static const MOZ_ENUM_CLASS_ENUM_TYPE(WebGLTexelFormat) Value
93 = IsFloatFormat<Format>::Value
94 ? WebGLTexelFormat::RGBA32F
95 : WebGLTexelFormat::RGBA8;
98 inline size_t TexelBytesForFormat(WebGLTexelFormat format) {
99 switch (format) {
100 case WebGLTexelFormat::R8:
101 case WebGLTexelFormat::A8:
102 return 1;
103 case WebGLTexelFormat::RA8:
104 case WebGLTexelFormat::RGBA5551:
105 case WebGLTexelFormat::RGBA4444:
106 case WebGLTexelFormat::RGB565:
107 case WebGLTexelFormat::D16:
108 return 2;
109 case WebGLTexelFormat::RGB8:
110 return 3;
111 case WebGLTexelFormat::RGBA8:
112 case WebGLTexelFormat::BGRA8:
113 case WebGLTexelFormat::BGRX8:
114 case WebGLTexelFormat::R32F:
115 case WebGLTexelFormat::A32F:
116 case WebGLTexelFormat::D32:
117 case WebGLTexelFormat::D24S8:
118 return 4;
119 case WebGLTexelFormat::RA32F:
120 return 8;
121 case WebGLTexelFormat::RGB32F:
122 return 12;
123 case WebGLTexelFormat::RGBA32F:
124 return 16;
125 default:
126 MOZ_ASSERT(false, "Unknown texel format. Coding mistake?");
127 return 0;
131 MOZ_ALWAYS_INLINE bool HasAlpha(WebGLTexelFormat format) {
132 return format == WebGLTexelFormat::A8 ||
133 format == WebGLTexelFormat::A32F ||
134 format == WebGLTexelFormat::RA8 ||
135 format == WebGLTexelFormat::RA32F ||
136 format == WebGLTexelFormat::RGBA8 ||
137 format == WebGLTexelFormat::BGRA8 ||
138 format == WebGLTexelFormat::RGBA32F ||
139 format == WebGLTexelFormat::RGBA4444 ||
140 format == WebGLTexelFormat::RGBA5551;
143 MOZ_ALWAYS_INLINE bool HasColor(WebGLTexelFormat format) {
144 return format == WebGLTexelFormat::R8 ||
145 format == WebGLTexelFormat::R32F ||
146 format == WebGLTexelFormat::RA8 ||
147 format == WebGLTexelFormat::RA32F ||
148 format == WebGLTexelFormat::RGB8 ||
149 format == WebGLTexelFormat::BGRX8 ||
150 format == WebGLTexelFormat::RGB565 ||
151 format == WebGLTexelFormat::RGB32F ||
152 format == WebGLTexelFormat::RGBA8 ||
153 format == WebGLTexelFormat::BGRA8 ||
154 format == WebGLTexelFormat::RGBA32F ||
155 format == WebGLTexelFormat::RGBA4444 ||
156 format == WebGLTexelFormat::RGBA5551;
160 /****** BEGIN CODE SHARED WITH WEBKIT ******/
162 // the pack/unpack functions here are originally from this file:
163 // http://trac.webkit.org/browser/trunk/WebCore/platform/graphics/GraphicsContext3D.cpp
165 //----------------------------------------------------------------------
166 // Pixel unpacking routines.
168 template<MOZ_ENUM_CLASS_ENUM_TYPE(WebGLTexelFormat) Format, typename SrcType, typename DstType>
169 MOZ_ALWAYS_INLINE void
170 unpack(const SrcType* __restrict src,
171 DstType* __restrict dst)
173 MOZ_ASSERT(false, "Unimplemented texture format conversion");
176 template<> MOZ_ALWAYS_INLINE void
177 unpack<WebGLTexelFormat::RGBA8, uint8_t, uint8_t>(const uint8_t* __restrict src, uint8_t* __restrict dst)
179 dst[0] = src[0];
180 dst[1] = src[1];
181 dst[2] = src[2];
182 dst[3] = src[3];
185 template<> MOZ_ALWAYS_INLINE void
186 unpack<WebGLTexelFormat::RGB8, uint8_t, uint8_t>(const uint8_t* __restrict src, uint8_t* __restrict dst)
188 dst[0] = src[0];
189 dst[1] = src[1];
190 dst[2] = src[2];
191 dst[3] = 0xFF;
194 template<> MOZ_ALWAYS_INLINE void
195 unpack<WebGLTexelFormat::BGRA8, uint8_t, uint8_t>(const uint8_t* __restrict src, uint8_t* __restrict dst)
197 dst[0] = src[2];
198 dst[1] = src[1];
199 dst[2] = src[0];
200 dst[3] = src[3];
203 template<> MOZ_ALWAYS_INLINE void
204 unpack<WebGLTexelFormat::BGRX8, uint8_t, uint8_t>(const uint8_t* __restrict src, uint8_t* __restrict dst)
206 dst[0] = src[2];
207 dst[1] = src[1];
208 dst[2] = src[0];
209 dst[3] = 0xFF;
212 template<> MOZ_ALWAYS_INLINE void
213 unpack<WebGLTexelFormat::RGBA5551, uint16_t, uint8_t>(const uint16_t* __restrict src, uint8_t* __restrict dst)
215 uint16_t packedValue = src[0];
216 uint8_t r = (packedValue >> 11) & 0x1F;
217 uint8_t g = (packedValue >> 6) & 0x1F;
218 uint8_t b = (packedValue >> 1) & 0x1F;
219 dst[0] = (r << 3) | (r & 0x7);
220 dst[1] = (g << 3) | (g & 0x7);
221 dst[2] = (b << 3) | (b & 0x7);
222 dst[3] = (packedValue & 0x1) ? 0xFF : 0;
225 template<> MOZ_ALWAYS_INLINE void
226 unpack<WebGLTexelFormat::RGBA4444, uint16_t, uint8_t>(const uint16_t* __restrict src, uint8_t* __restrict dst)
228 uint16_t packedValue = src[0];
229 uint8_t r = (packedValue >> 12) & 0x0F;
230 uint8_t g = (packedValue >> 8) & 0x0F;
231 uint8_t b = (packedValue >> 4) & 0x0F;
232 uint8_t a = packedValue & 0x0F;
233 dst[0] = (r << 4) | r;
234 dst[1] = (g << 4) | g;
235 dst[2] = (b << 4) | b;
236 dst[3] = (a << 4) | a;
239 template<> MOZ_ALWAYS_INLINE void
240 unpack<WebGLTexelFormat::RGB565, uint16_t, uint8_t>(const uint16_t* __restrict src, uint8_t* __restrict dst)
242 uint16_t packedValue = src[0];
243 uint8_t r = (packedValue >> 11) & 0x1F;
244 uint8_t g = (packedValue >> 5) & 0x3F;
245 uint8_t b = packedValue & 0x1F;
246 dst[0] = (r << 3) | (r & 0x7);
247 dst[1] = (g << 2) | (g & 0x3);
248 dst[2] = (b << 3) | (b & 0x7);
249 dst[3] = 0xFF;
252 template<> MOZ_ALWAYS_INLINE void
253 unpack<WebGLTexelFormat::R8, uint8_t, uint8_t>(const uint8_t* __restrict src, uint8_t* __restrict dst)
255 dst[0] = src[0];
256 dst[1] = src[0];
257 dst[2] = src[0];
258 dst[3] = 0xFF;
261 template<> MOZ_ALWAYS_INLINE void
262 unpack<WebGLTexelFormat::RA8, uint8_t, uint8_t>(const uint8_t* __restrict src, uint8_t* __restrict dst)
264 dst[0] = src[0];
265 dst[1] = src[0];
266 dst[2] = src[0];
267 dst[3] = src[1];
270 template<> MOZ_ALWAYS_INLINE void
271 unpack<WebGLTexelFormat::A8, uint8_t, uint8_t>(const uint8_t* __restrict src, uint8_t* __restrict dst)
273 dst[0] = 0;
274 dst[1] = 0;
275 dst[2] = 0;
276 dst[3] = src[0];
279 template<> MOZ_ALWAYS_INLINE void
280 unpack<WebGLTexelFormat::RGBA32F, float, float>(const float* __restrict src, float* __restrict dst)
282 dst[0] = src[0];
283 dst[1] = src[1];
284 dst[2] = src[2];
285 dst[3] = src[3];
288 template<> MOZ_ALWAYS_INLINE void
289 unpack<WebGLTexelFormat::RGB32F, float, float>(const float* __restrict src, float* __restrict dst)
291 dst[0] = src[0];
292 dst[1] = src[1];
293 dst[2] = src[2];
294 dst[3] = 1.0f;
297 template<> MOZ_ALWAYS_INLINE void
298 unpack<WebGLTexelFormat::R32F, float, float>(const float* __restrict src, float* __restrict dst)
300 dst[0] = src[0];
301 dst[1] = src[0];
302 dst[2] = src[0];
303 dst[3] = 1.0f;
306 template<> MOZ_ALWAYS_INLINE void
307 unpack<WebGLTexelFormat::RA32F, float, float>(const float* __restrict src, float* __restrict dst)
309 dst[0] = src[0];
310 dst[1] = src[0];
311 dst[2] = src[0];
312 dst[3] = src[1];
315 template<> MOZ_ALWAYS_INLINE void
316 unpack<WebGLTexelFormat::A32F, float, float>(const float* __restrict src, float* __restrict dst)
318 dst[0] = 0;
319 dst[1] = 0;
320 dst[2] = 0;
321 dst[3] = src[0];
324 //----------------------------------------------------------------------
325 // Pixel packing routines.
328 template<MOZ_ENUM_CLASS_ENUM_TYPE(WebGLTexelFormat) Format,
329 MOZ_ENUM_CLASS_ENUM_TYPE(WebGLTexelPremultiplicationOp) PremultiplicationOp,
330 typename SrcType,
331 typename DstType>
332 MOZ_ALWAYS_INLINE void
333 pack(const SrcType* __restrict src,
334 DstType* __restrict dst)
336 MOZ_ASSERT(false, "Unimplemented texture format conversion");
339 template<> MOZ_ALWAYS_INLINE void
340 pack<WebGLTexelFormat::A8, WebGLTexelPremultiplicationOp::None, uint8_t, uint8_t>(const uint8_t* __restrict src, uint8_t* __restrict dst)
342 dst[0] = src[3];
345 template<> MOZ_ALWAYS_INLINE void
346 pack<WebGLTexelFormat::A8, WebGLTexelPremultiplicationOp::Premultiply, uint8_t, uint8_t>(const uint8_t* __restrict src, uint8_t* __restrict dst)
348 dst[0] = src[3];
351 template<> MOZ_ALWAYS_INLINE void
352 pack<WebGLTexelFormat::A8, WebGLTexelPremultiplicationOp::Unpremultiply, uint8_t, uint8_t>(const uint8_t* __restrict src, uint8_t* __restrict dst)
354 dst[0] = src[3];
357 template<> MOZ_ALWAYS_INLINE void
358 pack<WebGLTexelFormat::R8, WebGLTexelPremultiplicationOp::None, uint8_t, uint8_t>(const uint8_t* __restrict src, uint8_t* __restrict dst)
360 dst[0] = src[0];
363 template<> MOZ_ALWAYS_INLINE void
364 pack<WebGLTexelFormat::R8, WebGLTexelPremultiplicationOp::Premultiply, uint8_t, uint8_t>(const uint8_t* __restrict src, uint8_t* __restrict dst)
366 float scaleFactor = src[3] / 255.0f;
367 uint8_t srcR = static_cast<uint8_t>(src[0] * scaleFactor);
368 dst[0] = srcR;
371 template<> MOZ_ALWAYS_INLINE void
372 pack<WebGLTexelFormat::R8, WebGLTexelPremultiplicationOp::Unpremultiply, uint8_t, uint8_t>(const uint8_t* __restrict src, uint8_t* __restrict dst)
374 float scaleFactor = src[3] ? 255.0f / src[3] : 1.0f;
375 uint8_t srcR = static_cast<uint8_t>(src[0] * scaleFactor);
376 dst[0] = srcR;
379 template<> MOZ_ALWAYS_INLINE void
380 pack<WebGLTexelFormat::RA8, WebGLTexelPremultiplicationOp::None, uint8_t, uint8_t>(const uint8_t* __restrict src, uint8_t* __restrict dst)
382 dst[0] = src[0];
383 dst[1] = src[3];
386 template<> MOZ_ALWAYS_INLINE void
387 pack<WebGLTexelFormat::RA8, WebGLTexelPremultiplicationOp::Premultiply, uint8_t, uint8_t>(const uint8_t* __restrict src, uint8_t* __restrict dst)
389 float scaleFactor = src[3] / 255.0f;
390 uint8_t srcR = static_cast<uint8_t>(src[0] * scaleFactor);
391 dst[0] = srcR;
392 dst[1] = src[3];
395 // FIXME: this routine is lossy and must be removed.
396 template<> MOZ_ALWAYS_INLINE void
397 pack<WebGLTexelFormat::RA8, WebGLTexelPremultiplicationOp::Unpremultiply, uint8_t, uint8_t>(const uint8_t* __restrict src, uint8_t* __restrict dst)
399 float scaleFactor = src[3] ? 255.0f / src[3] : 1.0f;
400 uint8_t srcR = static_cast<uint8_t>(src[0] * scaleFactor);
401 dst[0] = srcR;
402 dst[1] = src[3];
405 template<> MOZ_ALWAYS_INLINE void
406 pack<WebGLTexelFormat::RGB8, WebGLTexelPremultiplicationOp::None, uint8_t, uint8_t>(const uint8_t* __restrict src, uint8_t* __restrict dst)
408 dst[0] = src[0];
409 dst[1] = src[1];
410 dst[2] = src[2];
413 template<> MOZ_ALWAYS_INLINE void
414 pack<WebGLTexelFormat::RGB8, WebGLTexelPremultiplicationOp::Premultiply, uint8_t, uint8_t>(const uint8_t* __restrict src, uint8_t* __restrict dst)
416 float scaleFactor = src[3] / 255.0f;
417 uint8_t srcR = static_cast<uint8_t>(src[0] * scaleFactor);
418 uint8_t srcG = static_cast<uint8_t>(src[1] * scaleFactor);
419 uint8_t srcB = static_cast<uint8_t>(src[2] * scaleFactor);
420 dst[0] = srcR;
421 dst[1] = srcG;
422 dst[2] = srcB;
425 template<> MOZ_ALWAYS_INLINE void
426 pack<WebGLTexelFormat::RGB8, WebGLTexelPremultiplicationOp::Unpremultiply, uint8_t, uint8_t>(const uint8_t* __restrict src, uint8_t* __restrict dst)
428 float scaleFactor = src[3] ? 255.0f / src[3] : 1.0f;
429 uint8_t srcR = static_cast<uint8_t>(src[0] * scaleFactor);
430 uint8_t srcG = static_cast<uint8_t>(src[1] * scaleFactor);
431 uint8_t srcB = static_cast<uint8_t>(src[2] * scaleFactor);
432 dst[0] = srcR;
433 dst[1] = srcG;
434 dst[2] = srcB;
437 template<> MOZ_ALWAYS_INLINE void
438 pack<WebGLTexelFormat::RGBA8, WebGLTexelPremultiplicationOp::None, uint8_t, uint8_t>(const uint8_t* __restrict src, uint8_t* __restrict dst)
440 dst[0] = src[0];
441 dst[1] = src[1];
442 dst[2] = src[2];
443 dst[3] = src[3];
446 template<> MOZ_ALWAYS_INLINE void
447 pack<WebGLTexelFormat::RGBA8, WebGLTexelPremultiplicationOp::Premultiply, uint8_t, uint8_t>(const uint8_t* __restrict src, uint8_t* __restrict dst)
449 float scaleFactor = src[3] / 255.0f;
450 uint8_t srcR = static_cast<uint8_t>(src[0] * scaleFactor);
451 uint8_t srcG = static_cast<uint8_t>(src[1] * scaleFactor);
452 uint8_t srcB = static_cast<uint8_t>(src[2] * scaleFactor);
453 dst[0] = srcR;
454 dst[1] = srcG;
455 dst[2] = srcB;
456 dst[3] = src[3];
459 // FIXME: this routine is lossy and must be removed.
460 template<> MOZ_ALWAYS_INLINE void
461 pack<WebGLTexelFormat::RGBA8, WebGLTexelPremultiplicationOp::Unpremultiply, uint8_t, uint8_t>(const uint8_t* __restrict src, uint8_t* __restrict dst)
463 float scaleFactor = src[3] ? 255.0f / src[3] : 1.0f;
464 uint8_t srcR = static_cast<uint8_t>(src[0] * scaleFactor);
465 uint8_t srcG = static_cast<uint8_t>(src[1] * scaleFactor);
466 uint8_t srcB = static_cast<uint8_t>(src[2] * scaleFactor);
467 dst[0] = srcR;
468 dst[1] = srcG;
469 dst[2] = srcB;
470 dst[3] = src[3];
473 template<> MOZ_ALWAYS_INLINE void
474 pack<WebGLTexelFormat::RGBA4444, WebGLTexelPremultiplicationOp::None, uint8_t, uint16_t>(const uint8_t* __restrict src, uint16_t* __restrict dst)
476 *dst = ( ((src[0] & 0xF0) << 8)
477 | ((src[1] & 0xF0) << 4)
478 | (src[2] & 0xF0)
479 | (src[3] >> 4) );
482 template<> MOZ_ALWAYS_INLINE void
483 pack<WebGLTexelFormat::RGBA4444, WebGLTexelPremultiplicationOp::Premultiply, uint8_t, uint16_t>(const uint8_t* __restrict src, uint16_t* __restrict dst)
485 float scaleFactor = src[3] / 255.0f;
486 uint8_t srcR = static_cast<uint8_t>(src[0] * scaleFactor);
487 uint8_t srcG = static_cast<uint8_t>(src[1] * scaleFactor);
488 uint8_t srcB = static_cast<uint8_t>(src[2] * scaleFactor);
489 *dst = ( ((srcR & 0xF0) << 8)
490 | ((srcG & 0xF0) << 4)
491 | (srcB & 0xF0)
492 | (src[3] >> 4));
495 // FIXME: this routine is lossy and must be removed.
496 template<> MOZ_ALWAYS_INLINE void
497 pack<WebGLTexelFormat::RGBA4444, WebGLTexelPremultiplicationOp::Unpremultiply, uint8_t, uint16_t>(const uint8_t* __restrict src, uint16_t* __restrict dst)
499 float scaleFactor = src[3] ? 255.0f / src[3] : 1.0f;
500 uint8_t srcR = static_cast<uint8_t>(src[0] * scaleFactor);
501 uint8_t srcG = static_cast<uint8_t>(src[1] * scaleFactor);
502 uint8_t srcB = static_cast<uint8_t>(src[2] * scaleFactor);
503 *dst = ( ((srcR & 0xF0) << 8)
504 | ((srcG & 0xF0) << 4)
505 | (srcB & 0xF0)
506 | (src[3] >> 4));
509 template<> MOZ_ALWAYS_INLINE void
510 pack<WebGLTexelFormat::RGBA5551, WebGLTexelPremultiplicationOp::None, uint8_t, uint16_t>(const uint8_t* __restrict src, uint16_t* __restrict dst)
512 *dst = ( ((src[0] & 0xF8) << 8)
513 | ((src[1] & 0xF8) << 3)
514 | ((src[2] & 0xF8) >> 2)
515 | (src[3] >> 7));
518 template<> MOZ_ALWAYS_INLINE void
519 pack<WebGLTexelFormat::RGBA5551, WebGLTexelPremultiplicationOp::Premultiply, uint8_t, uint16_t>(const uint8_t* __restrict src, uint16_t* __restrict dst)
521 float scaleFactor = src[3] / 255.0f;
522 uint8_t srcR = static_cast<uint8_t>(src[0] * scaleFactor);
523 uint8_t srcG = static_cast<uint8_t>(src[1] * scaleFactor);
524 uint8_t srcB = static_cast<uint8_t>(src[2] * scaleFactor);
525 *dst = ( ((srcR & 0xF8) << 8)
526 | ((srcG & 0xF8) << 3)
527 | ((srcB & 0xF8) >> 2)
528 | (src[3] >> 7));
531 // FIXME: this routine is lossy and must be removed.
532 template<> MOZ_ALWAYS_INLINE void
533 pack<WebGLTexelFormat::RGBA5551, WebGLTexelPremultiplicationOp::Unpremultiply, uint8_t, uint16_t>(const uint8_t* __restrict src, uint16_t* __restrict dst)
535 float scaleFactor = src[3] ? 255.0f / src[3] : 1.0f;
536 uint8_t srcR = static_cast<uint8_t>(src[0] * scaleFactor);
537 uint8_t srcG = static_cast<uint8_t>(src[1] * scaleFactor);
538 uint8_t srcB = static_cast<uint8_t>(src[2] * scaleFactor);
539 *dst = ( ((srcR & 0xF8) << 8)
540 | ((srcG & 0xF8) << 3)
541 | ((srcB & 0xF8) >> 2)
542 | (src[3] >> 7));
545 template<> MOZ_ALWAYS_INLINE void
546 pack<WebGLTexelFormat::RGB565, WebGLTexelPremultiplicationOp::None, uint8_t, uint16_t>(const uint8_t* __restrict src, uint16_t* __restrict dst)
548 *dst = ( ((src[0] & 0xF8) << 8)
549 | ((src[1] & 0xFC) << 3)
550 | ((src[2] & 0xF8) >> 3));
553 template<> MOZ_ALWAYS_INLINE void
554 pack<WebGLTexelFormat::RGB565, WebGLTexelPremultiplicationOp::Premultiply, uint8_t, uint16_t>(const uint8_t* __restrict src, uint16_t* __restrict dst)
556 float scaleFactor = src[3] / 255.0f;
557 uint8_t srcR = static_cast<uint8_t>(src[0] * scaleFactor);
558 uint8_t srcG = static_cast<uint8_t>(src[1] * scaleFactor);
559 uint8_t srcB = static_cast<uint8_t>(src[2] * scaleFactor);
560 *dst = ( ((srcR & 0xF8) << 8)
561 | ((srcG & 0xFC) << 3)
562 | ((srcB & 0xF8) >> 3));
565 // FIXME: this routine is lossy and must be removed.
566 template<> MOZ_ALWAYS_INLINE void
567 pack<WebGLTexelFormat::RGB565, WebGLTexelPremultiplicationOp::Unpremultiply, uint8_t, uint16_t>(const uint8_t* __restrict src, uint16_t* __restrict dst)
569 float scaleFactor = src[3] ? 255.0f / src[3] : 1.0f;
570 uint8_t srcR = static_cast<uint8_t>(src[0] * scaleFactor);
571 uint8_t srcG = static_cast<uint8_t>(src[1] * scaleFactor);
572 uint8_t srcB = static_cast<uint8_t>(src[2] * scaleFactor);
573 *dst = ( ((srcR & 0xF8) << 8)
574 | ((srcG & 0xFC) << 3)
575 | ((srcB & 0xF8) >> 3));
578 template<> MOZ_ALWAYS_INLINE void
579 pack<WebGLTexelFormat::RGB32F, WebGLTexelPremultiplicationOp::None, float, float>(const float* __restrict src, float* __restrict dst)
581 dst[0] = src[0];
582 dst[1] = src[1];
583 dst[2] = src[2];
586 template<> MOZ_ALWAYS_INLINE void
587 pack<WebGLTexelFormat::RGB32F, WebGLTexelPremultiplicationOp::Premultiply, float, float>(const float* __restrict src, float* __restrict dst)
589 float scaleFactor = src[3];
590 dst[0] = src[0] * scaleFactor;
591 dst[1] = src[1] * scaleFactor;
592 dst[2] = src[2] * scaleFactor;
595 template<> MOZ_ALWAYS_INLINE void
596 pack<WebGLTexelFormat::RGBA32F, WebGLTexelPremultiplicationOp::None, float, float>(const float* __restrict src, float* __restrict dst)
598 dst[0] = src[0];
599 dst[1] = src[1];
600 dst[2] = src[2];
601 dst[3] = src[3];
604 template<> MOZ_ALWAYS_INLINE void
605 pack<WebGLTexelFormat::RGBA32F, WebGLTexelPremultiplicationOp::Premultiply, float, float>(const float* __restrict src, float* __restrict dst)
607 float scaleFactor = src[3];
608 dst[0] = src[0] * scaleFactor;
609 dst[1] = src[1] * scaleFactor;
610 dst[2] = src[2] * scaleFactor;
611 dst[3] = src[3];
614 template<> MOZ_ALWAYS_INLINE void
615 pack<WebGLTexelFormat::A32F, WebGLTexelPremultiplicationOp::None, float, float>(const float* __restrict src, float* __restrict dst)
617 dst[0] = src[3];
620 template<> MOZ_ALWAYS_INLINE void
621 pack<WebGLTexelFormat::A32F, WebGLTexelPremultiplicationOp::Premultiply, float, float>(const float* __restrict src, float* __restrict dst)
623 dst[0] = src[3];
626 template<> MOZ_ALWAYS_INLINE void
627 pack<WebGLTexelFormat::R32F, WebGLTexelPremultiplicationOp::None, float, float>(const float* __restrict src, float* __restrict dst)
629 dst[0] = src[0];
632 template<> MOZ_ALWAYS_INLINE void
633 pack<WebGLTexelFormat::R32F, WebGLTexelPremultiplicationOp::Premultiply, float, float>(const float* __restrict src, float* __restrict dst)
635 float scaleFactor = src[3];
636 dst[0] = src[0] * scaleFactor;
639 template<> MOZ_ALWAYS_INLINE void
640 pack<WebGLTexelFormat::RA32F, WebGLTexelPremultiplicationOp::None, float, float>(const float* __restrict src, float* __restrict dst)
642 dst[0] = src[0];
643 dst[1] = src[3];
646 template<> MOZ_ALWAYS_INLINE void
647 pack<WebGLTexelFormat::RA32F, WebGLTexelPremultiplicationOp::Premultiply, float, float>(const float* __restrict src, float* __restrict dst)
649 float scaleFactor = src[3];
650 dst[0] = src[0] * scaleFactor;
651 dst[1] = scaleFactor;
654 /****** END CODE SHARED WITH WEBKIT ******/
656 template<typename SrcType, typename DstType> MOZ_ALWAYS_INLINE void
657 convertType(const SrcType* __restrict src, DstType* __restrict dst)
659 MOZ_ASSERT(false, "Unimplemented texture format conversion");
662 template<> MOZ_ALWAYS_INLINE void
663 convertType<uint8_t, uint8_t>(const uint8_t* __restrict src, uint8_t* __restrict dst)
665 dst[0] = src[0];
666 dst[1] = src[1];
667 dst[2] = src[2];
668 dst[3] = src[3];
671 template<> MOZ_ALWAYS_INLINE void
672 convertType<float, float>(const float* __restrict src, float* __restrict dst)
674 dst[0] = src[0];
675 dst[1] = src[1];
676 dst[2] = src[2];
677 dst[3] = src[3];
680 template<> MOZ_ALWAYS_INLINE void
681 convertType<uint8_t, float>(const uint8_t* __restrict src, float* __restrict dst)
683 const float scaleFactor = 1.f / 255.0f;
684 dst[0] = src[0] * scaleFactor;
685 dst[1] = src[1] * scaleFactor;
686 dst[2] = src[2] * scaleFactor;
687 dst[3] = src[3] * scaleFactor;
690 } // end namespace WebGLTexelConversions
692 } // end namespace mozilla
694 #endif // WEBGLTEXELCONVERSIONS_H_