Bug 835381 - Update libnestegg to 38c83d9d4c0c5c84373aa285bd30094a12d6b6f6. r=kinetik
[gecko.git] / gfx / thebes / gfxAlphaRecovery.h
blob07bf51b3135d6073e534f127a9b88e3f727e7dc9
1 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 * This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #ifndef _GFXALPHARECOVERY_H_
7 #define _GFXALPHARECOVERY_H_
9 #include "gfxContext.h"
10 #include "gfxImageSurface.h"
11 #include "mozilla/SSE.h"
12 #include "nsRect.h"
14 class THEBES_API gfxAlphaRecovery {
15 public:
16 struct Analysis {
17 bool uniformColor;
18 bool uniformAlpha;
19 gfxFloat alpha;
20 gfxFloat r, g, b;
23 /**
24 * Some SIMD fast-paths only can be taken if the relative
25 * byte-alignment of images' pointers and strides meets certain
26 * criteria. Aligning image pointers and strides by
27 * |GoodAlignmentLog2()| below will ensure that fast-paths aren't
28 * skipped because of misalignment. Fast-paths may still be taken
29 * even if GoodAlignmentLog2() is not met, in some conditions.
31 static uint32_t GoodAlignmentLog2() { return 4; /* for SSE2 */ }
33 /* Given two surfaces of equal size with the same rendering, one onto a
34 * black background and the other onto white, recovers alpha values from
35 * the difference and sets the alpha values on the black surface.
36 * The surfaces must have format RGB24 or ARGB32.
37 * Returns true on success.
39 static bool RecoverAlpha (gfxImageSurface *blackSurface,
40 const gfxImageSurface *whiteSurface,
41 Analysis *analysis = nullptr);
43 #ifdef MOZILLA_MAY_SUPPORT_SSE2
44 /* This does the same as the previous function, but uses SSE2
45 * optimizations. Usually this should not be called directly. Be sure to
46 * check mozilla::supports_sse2() before calling this function.
48 static bool RecoverAlphaSSE2 (gfxImageSurface *blackSurface,
49 const gfxImageSurface *whiteSurface);
51 /**
52 * A common use-case for alpha recovery is to paint into a
53 * temporary "white image", then paint onto a subrect of the
54 * surface, the "black image", into which alpha-recovered pixels
55 * are eventually to be written. This function returns a rect
56 * aligned so that recovering alpha for that rect will hit SIMD
57 * fast-paths, if possible. It's not always possible to align
58 * |aRect| so that fast-paths will be taken.
60 * The returned rect is always a superset of |aRect|.
62 static nsIntRect AlignRectForSubimageRecovery(const nsIntRect& aRect,
63 gfxImageSurface* aSurface);
64 #else
65 static nsIntRect AlignRectForSubimageRecovery(const nsIntRect& aRect,
66 gfxImageSurface*)
67 { return aRect; }
68 #endif
70 /** from cairo-xlib-utils.c, modified */
71 /**
72 * Given the RGB data for two image surfaces, one a source image composited
73 * with OVER onto a black background, and one a source image composited with
74 * OVER onto a white background, reconstruct the original image data into
75 * black_data.
77 * Consider a single color channel and a given pixel. Suppose the original
78 * premultiplied color value was C and the alpha value was A. Let the final
79 * on-black color be B and the final on-white color be W. All values range
80 * over 0-255.
82 * Then B=C and W=(255*(255 - A) + C*255)/255. Solving for A, we get
83 * A=255 - (W - C). Therefore it suffices to leave the black_data color
84 * data alone and set the alpha values using that simple formula. It shouldn't
85 * matter what color channel we pick for the alpha computation, but we'll
86 * pick green because if we went through a color channel downsample the green
87 * bits are likely to be the most accurate.
89 * This function needs to be in the header file since it's used by both
90 * gfxRecoverAlpha.cpp and gfxRecoverAlphaSSE2.cpp.
93 static inline uint32_t
94 RecoverPixel(uint32_t black, uint32_t white)
96 const uint32_t GREEN_MASK = 0x0000FF00;
97 const uint32_t ALPHA_MASK = 0xFF000000;
99 /* |diff| here is larger when the source image pixel is more transparent.
100 If both renderings are from the same source image composited with OVER,
101 then the color values on white will always be greater than those on
102 black, so |diff| would not overflow. However, overflow may happen, for
103 example, when a plugin plays a video and the image is rapidly changing.
104 If there is overflow, then behave as if we limit to the difference to
105 >= 0, which will make the rendering opaque. (Without this overflow
106 will make the rendering transparent.) */
107 uint32_t diff = (white & GREEN_MASK) - (black & GREEN_MASK);
108 /* |diff| is 0xFFFFxx00 on overflow and 0x0000xx00 otherwise, so use this
109 to limit the transparency. */
110 uint32_t limit = diff & ALPHA_MASK;
111 /* The alpha bits of the result */
112 uint32_t alpha = (ALPHA_MASK - (diff << 16)) | limit;
114 return alpha | (black & ~ALPHA_MASK);
118 #endif /* _GFXALPHARECOVERY_H_ */