2018-11-30 22:52:05 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
2012-05-21 15:12:37 +04:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2010-12-05 11:38:53 +03:00
|
|
|
|
|
|
|
#include "gfxAlphaRecovery.h"
|
2013-10-08 03:15:59 +04:00
|
|
|
#include "gfxImageSurface.h"
|
2020-11-23 19:21:38 +03:00
|
|
|
#include "nsDebug.h"
|
2010-12-05 11:38:53 +03:00
|
|
|
#include <emmintrin.h>
|
|
|
|
|
|
|
|
// This file should only be compiled on x86 and x64 systems. Additionally,
|
|
|
|
// you'll need to compile it with -msse2 if you're using GCC on x86.
|
|
|
|
|
|
|
|
#if defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_AMD64))
|
2012-08-22 19:56:38 +04:00
|
|
|
__declspec(align(16)) static uint32_t greenMaski[] = {0x0000ff00, 0x0000ff00,
|
2010-12-05 11:38:53 +03:00
|
|
|
0x0000ff00, 0x0000ff00};
|
2012-08-22 19:56:38 +04:00
|
|
|
__declspec(align(16)) static uint32_t alphaMaski[] = {0xff000000, 0xff000000,
|
2010-12-05 11:38:53 +03:00
|
|
|
0xff000000, 0xff000000};
|
|
|
|
#elif defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
|
2012-08-22 19:56:38 +04:00
|
|
|
static uint32_t greenMaski[] __attribute__((aligned(16))) = {
|
2010-12-05 11:38:53 +03:00
|
|
|
0x0000ff00, 0x0000ff00, 0x0000ff00, 0x0000ff00};
|
2012-08-22 19:56:38 +04:00
|
|
|
static uint32_t alphaMaski[] __attribute__((aligned(16))) = {
|
2010-12-05 11:38:53 +03:00
|
|
|
0xff000000, 0xff000000, 0xff000000, 0xff000000};
|
|
|
|
#elif defined(__SUNPRO_CC) && (defined(__i386) || defined(__x86_64__))
|
|
|
|
# pragma align 16(greenMaski, alphaMaski)
|
2012-08-22 19:56:38 +04:00
|
|
|
static uint32_t greenMaski[] = {0x0000ff00, 0x0000ff00, 0x0000ff00, 0x0000ff00};
|
|
|
|
static uint32_t alphaMaski[] = {0xff000000, 0xff000000, 0xff000000, 0xff000000};
|
2010-12-05 11:38:53 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
bool gfxAlphaRecovery::RecoverAlphaSSE2(gfxImageSurface* blackSurf,
|
|
|
|
const gfxImageSurface* whiteSurf) {
|
2015-06-01 11:26:19 +03:00
|
|
|
mozilla::gfx::IntSize size = blackSurf->GetSize();
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2010-12-05 11:38:53 +03:00
|
|
|
if (size != whiteSurf->GetSize() ||
|
2016-01-08 07:57:38 +03:00
|
|
|
(blackSurf->Format() != mozilla::gfx::SurfaceFormat::A8R8G8B8_UINT32 &&
|
|
|
|
blackSurf->Format() != mozilla::gfx::SurfaceFormat::X8R8G8B8_UINT32) ||
|
|
|
|
(whiteSurf->Format() != mozilla::gfx::SurfaceFormat::A8R8G8B8_UINT32 &&
|
|
|
|
whiteSurf->Format() != mozilla::gfx::SurfaceFormat::X8R8G8B8_UINT32))
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2010-12-05 11:38:53 +03:00
|
|
|
blackSurf->Flush();
|
|
|
|
whiteSurf->Flush();
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2010-12-05 11:38:53 +03:00
|
|
|
unsigned char* blackData = blackSurf->Data();
|
|
|
|
unsigned char* whiteData = whiteSurf->Data();
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2010-12-05 11:38:53 +03:00
|
|
|
if ((NS_PTR_TO_UINT32(blackData) & 0xf) !=
|
|
|
|
(NS_PTR_TO_UINT32(whiteData) & 0xf) ||
|
|
|
|
(blackSurf->Stride() - whiteSurf->Stride()) & 0xf) {
|
|
|
|
// Cannot keep these in alignment.
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2010-12-05 11:38:53 +03:00
|
|
|
__m128i greenMask = _mm_load_si128((__m128i*)greenMaski);
|
|
|
|
__m128i alphaMask = _mm_load_si128((__m128i*)alphaMaski);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
for (int32_t i = 0; i < size.height; ++i) {
|
|
|
|
int32_t j = 0;
|
2010-12-05 11:38:53 +03:00
|
|
|
// Loop single pixels until at 4 byte alignment.
|
|
|
|
while (NS_PTR_TO_UINT32(blackData) & 0xf && j < size.width) {
|
2012-08-22 19:56:38 +04:00
|
|
|
*((uint32_t*)blackData) =
|
|
|
|
RecoverPixel(*reinterpret_cast<uint32_t*>(blackData),
|
|
|
|
*reinterpret_cast<uint32_t*>(whiteData));
|
2010-12-05 11:38:53 +03:00
|
|
|
blackData += 4;
|
|
|
|
whiteData += 4;
|
2018-11-30 13:46:48 +03:00
|
|
|
j++;
|
2010-12-05 11:38:53 +03:00
|
|
|
}
|
|
|
|
// This extra loop allows the compiler to do some more clever registry
|
|
|
|
// management and makes it about 5% faster than with only the 4 pixel
|
|
|
|
// at a time loop.
|
|
|
|
for (; j < size.width - 8; j += 8) {
|
|
|
|
__m128i black1 = _mm_load_si128((__m128i*)blackData);
|
2012-08-22 19:56:38 +04:00
|
|
|
__m128i white1 = _mm_load_si128((__m128i*)whiteData);
|
2010-12-05 11:38:53 +03:00
|
|
|
__m128i black2 = _mm_load_si128((__m128i*)(blackData + 16));
|
|
|
|
__m128i white2 = _mm_load_si128((__m128i*)(whiteData + 16));
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2010-12-05 11:38:53 +03:00
|
|
|
// Execute the same instructions as described in RecoverPixel, only
|
|
|
|
// using an SSE2 packed saturated subtract.
|
|
|
|
white1 = _mm_subs_epu8(white1, black1);
|
|
|
|
white2 = _mm_subs_epu8(white2, black2);
|
|
|
|
white1 = _mm_subs_epu8(greenMask, white1);
|
|
|
|
white2 = _mm_subs_epu8(greenMask, white2);
|
|
|
|
// Producing the final black pixel in an XMM register and storing
|
|
|
|
// that is actually faster than doing a masked store since that
|
|
|
|
// does an unaligned storage. We have the black pixel in a register
|
|
|
|
// anyway.
|
|
|
|
black1 = _mm_andnot_si128(alphaMask, black1);
|
|
|
|
black2 = _mm_andnot_si128(alphaMask, black2);
|
|
|
|
white1 = _mm_slli_si128(white1, 2);
|
|
|
|
white2 = _mm_slli_si128(white2, 2);
|
|
|
|
white1 = _mm_and_si128(alphaMask, white1);
|
|
|
|
white2 = _mm_and_si128(alphaMask, white2);
|
|
|
|
black1 = _mm_or_si128(white1, black1);
|
|
|
|
black2 = _mm_or_si128(white2, black2);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2010-12-05 11:38:53 +03:00
|
|
|
_mm_store_si128((__m128i*)blackData, black1);
|
|
|
|
_mm_store_si128((__m128i*)(blackData + 16), black2);
|
|
|
|
blackData += 32;
|
|
|
|
whiteData += 32;
|
|
|
|
}
|
|
|
|
for (; j < size.width - 4; j += 4) {
|
|
|
|
__m128i black = _mm_load_si128((__m128i*)blackData);
|
|
|
|
__m128i white = _mm_load_si128((__m128i*)whiteData);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2010-12-05 11:38:53 +03:00
|
|
|
white = _mm_subs_epu8(white, black);
|
|
|
|
white = _mm_subs_epu8(greenMask, white);
|
|
|
|
black = _mm_andnot_si128(alphaMask, black);
|
|
|
|
white = _mm_slli_si128(white, 2);
|
|
|
|
white = _mm_and_si128(alphaMask, white);
|
|
|
|
black = _mm_or_si128(white, black);
|
|
|
|
_mm_store_si128((__m128i*)blackData, black);
|
|
|
|
blackData += 16;
|
|
|
|
whiteData += 16;
|
|
|
|
}
|
|
|
|
// Loop single pixels until we're done.
|
|
|
|
while (j < size.width) {
|
2012-08-22 19:56:38 +04:00
|
|
|
*((uint32_t*)blackData) =
|
|
|
|
RecoverPixel(*reinterpret_cast<uint32_t*>(blackData),
|
|
|
|
*reinterpret_cast<uint32_t*>(whiteData));
|
2010-12-05 11:38:53 +03:00
|
|
|
blackData += 4;
|
|
|
|
whiteData += 4;
|
2018-11-30 13:46:48 +03:00
|
|
|
j++;
|
|
|
|
}
|
2010-12-05 11:38:53 +03:00
|
|
|
blackData += blackSurf->Stride() - j * 4;
|
|
|
|
whiteData += whiteSurf->Stride() - j * 4;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2010-12-05 11:38:53 +03:00
|
|
|
|
|
|
|
blackSurf->MarkDirty();
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2010-12-05 11:38:53 +03:00
|
|
|
}
|
2011-02-17 01:43:30 +03:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
static int32_t ByteAlignment(int32_t aAlignToLog2, int32_t aX, int32_t aY = 0,
|
|
|
|
int32_t aStride = 1) {
|
2011-02-17 01:43:30 +03:00
|
|
|
return (aX + aStride * aY) & ((1 << aAlignToLog2) - 1);
|
|
|
|
}
|
|
|
|
|
2015-04-21 18:04:57 +03:00
|
|
|
/*static*/ mozilla::gfx::IntRect gfxAlphaRecovery::AlignRectForSubimageRecovery(
|
2011-02-17 01:43:30 +03:00
|
|
|
const mozilla::gfx::IntRect& aRect, gfxImageSurface* aSurface) {
|
2016-01-08 07:57:38 +03:00
|
|
|
NS_ASSERTION(
|
|
|
|
mozilla::gfx::SurfaceFormat::A8R8G8B8_UINT32 == aSurface->Format(),
|
2011-02-17 01:43:30 +03:00
|
|
|
"Thebes grew support for non-ARGB32 COLOR_ALPHA?");
|
2012-08-22 19:56:38 +04:00
|
|
|
static const int32_t kByteAlignLog2 = GoodAlignmentLog2();
|
|
|
|
static const int32_t bpp = 4;
|
|
|
|
static const int32_t pixPerAlign = (1 << kByteAlignLog2) / bpp;
|
2011-02-17 01:43:30 +03:00
|
|
|
//
|
|
|
|
// We're going to create a subimage of the surface with size
|
|
|
|
// <sw,sh> for alpha recovery, and want a SIMD fast-path. The
|
|
|
|
// rect <x,y, w,h> /needs/ to be redrawn, but it might not be
|
|
|
|
// properly aligned for SIMD. So we want to find a rect <x',y',
|
|
|
|
// w',h'> that's a superset of what needs to be redrawn but is
|
|
|
|
// properly aligned. Proper alignment is
|
|
|
|
//
|
|
|
|
// BPP * (x' + y' * sw) \cong 0 (mod ALIGN)
|
|
|
|
// BPP * w' \cong BPP * sw (mod ALIGN)
|
|
|
|
//
|
|
|
|
// (We assume the pixel at surface <0,0> is already ALIGN'd.)
|
|
|
|
// That rect (obviously) has to fit within the surface bounds, and
|
|
|
|
// we should also minimize the extra pixels redrawn only for
|
|
|
|
// alignment's sake. So we also want
|
|
|
|
//
|
|
|
|
// minimize <x',y', w',h'>
|
|
|
|
// 0 <= x' <= x
|
|
|
|
// 0 <= y' <= y
|
|
|
|
// w <= w' <= sw
|
|
|
|
// h <= h' <= sh
|
|
|
|
//
|
|
|
|
// This is a messy integer non-linear programming problem, except
|
|
|
|
// ... we can assume that ALIGN/BPP is a very small constant. So,
|
|
|
|
// brute force is viable. The algorithm below will find a
|
|
|
|
// solution if one exists, but isn't guaranteed to find the
|
|
|
|
// minimum solution. (For SSE2, ALIGN/BPP = 4, so it'll do at
|
|
|
|
// most 64 iterations below). In what's likely the common case,
|
|
|
|
// an already-aligned rectangle, it only needs 1 iteration.
|
|
|
|
//
|
|
|
|
// Is this alignment worth doing? Recovering alpha will take work
|
|
|
|
// proportional to w*h (assuming alpha recovery computation isn't
|
|
|
|
// memory bound). This analysis can lead to O(w+h) extra work
|
|
|
|
// (with small constants). In exchange, we expect to shave off a
|
|
|
|
// ALIGN/BPP constant by using SIMD-ized alpha recovery. So as
|
|
|
|
// w*h diverges from w+h, the win factor approaches ALIGN/BPP. We
|
|
|
|
// only really care about the w*h >> w+h case anyway; others
|
|
|
|
// should be fast enough even with the overhead. (Unless the cost
|
|
|
|
// of repainting the expanded rect is high, but in that case
|
|
|
|
// SIMD-ized alpha recovery won't make a difference so this code
|
|
|
|
// shouldn't be called.)
|
|
|
|
//
|
2015-06-01 11:26:19 +03:00
|
|
|
mozilla::gfx::IntSize surfaceSize = aSurface->GetSize();
|
2012-08-22 19:56:38 +04:00
|
|
|
const int32_t stride = bpp * surfaceSize.width;
|
2011-02-17 01:43:30 +03:00
|
|
|
if (stride != aSurface->Stride()) {
|
|
|
|
NS_WARNING("Unexpected stride, falling back on slow alpha recovery");
|
|
|
|
return aRect;
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2017-12-19 23:48:39 +03:00
|
|
|
const int32_t x = aRect.X(), y = aRect.Y(), w = aRect.Width(),
|
|
|
|
h = aRect.Height();
|
2012-08-22 19:56:38 +04:00
|
|
|
const int32_t r = x + w;
|
|
|
|
const int32_t sw = surfaceSize.width;
|
|
|
|
const int32_t strideAlign = ByteAlignment(kByteAlignLog2, stride);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2011-02-17 01:43:30 +03:00
|
|
|
// The outer two loops below keep the rightmost (|r| above) and
|
|
|
|
// bottommost pixels in |aRect| fixed wrt <x,y>, to ensure that we
|
|
|
|
// return only a superset of the original rect. These loops
|
|
|
|
// search for an aligned top-left pixel by trying to expand <x,y>
|
|
|
|
// left and up by <dx,dy> pixels, respectively.
|
|
|
|
//
|
|
|
|
// Then if a properly-aligned top-left pixel is found, the
|
|
|
|
// innermost loop tries to find an aligned stride by moving the
|
|
|
|
// rightmost pixel rightward by dr.
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t dx, dy, dr;
|
2011-02-17 01:43:30 +03:00
|
|
|
for (dy = 0; (dy < pixPerAlign) && (y - dy >= 0); ++dy) {
|
|
|
|
for (dx = 0; (dx < pixPerAlign) && (x - dx >= 0); ++dx) {
|
|
|
|
if (0 != ByteAlignment(kByteAlignLog2, bpp * (x - dx), y - dy, stride)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
for (dr = 0; (dr < pixPerAlign) && (r + dr <= sw); ++dr) {
|
|
|
|
if (strideAlign == ByteAlignment(kByteAlignLog2, bpp * (w + dr + dx))) {
|
|
|
|
goto FOUND_SOLUTION;
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2011-02-17 01:43:30 +03:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2011-02-17 01:43:30 +03:00
|
|
|
|
|
|
|
// Didn't find a solution.
|
|
|
|
return aRect;
|
|
|
|
|
|
|
|
FOUND_SOLUTION:
|
2015-04-21 18:04:57 +03:00
|
|
|
mozilla::gfx::IntRect solution =
|
|
|
|
mozilla::gfx::IntRect(x - dx, y - dy, w + dr + dx, h + dy);
|
|
|
|
MOZ_ASSERT(
|
|
|
|
mozilla::gfx::IntRect(0, 0, sw, surfaceSize.height).Contains(solution),
|
2015-02-10 01:34:50 +03:00
|
|
|
"'Solution' extends outside surface bounds!");
|
2011-02-17 01:43:30 +03:00
|
|
|
return solution;
|
|
|
|
}
|