diff options
-rw-r--r-- | include/core/SkCanvas.h | 8 | ||||
-rw-r--r-- | include/core/SkColorPriv.h | 140 | ||||
-rw-r--r-- | include/core/SkDevice.h | 2 | ||||
-rw-r--r-- | src/core/SkBitmapDevice.cpp | 48 | ||||
-rw-r--r-- | src/core/SkConfig8888.cpp | 333 | ||||
-rw-r--r-- | src/core/SkConfig8888.h | 82 | ||||
-rw-r--r-- | src/core/SkDevice.cpp | 10 | ||||
-rw-r--r-- | src/gpu/GrContext.cpp | 111 | ||||
-rw-r--r-- | src/gpu/SkGr.cpp | 21 | ||||
-rw-r--r-- | tests/DeferredCanvasTest.cpp | 27 | ||||
-rw-r--r-- | tests/WritePixelsTest.cpp | 213 | ||||
-rw-r--r-- | tools/sk_tool_utils.cpp | 31 | ||||
-rw-r--r-- | tools/sk_tool_utils.h | 5 |
13 files changed, 610 insertions, 421 deletions
diff --git a/include/core/SkCanvas.h b/include/core/SkCanvas.h index 0d8fd463fe..f5d4fe63cf 100644 --- a/include/core/SkCanvas.h +++ b/include/core/SkCanvas.h @@ -20,12 +20,6 @@ //#define SK_SUPPORT_LEGACY_READPIXELSCONFIG -#ifdef SK_SUPPORT_LEGACY_READPIXELSCONFIG - #ifndef SK_SUPPORT_LEGACY_CONFIG8888 - #define SK_SUPPORT_LEGACY_CONFIG8888 - #endif -#endif - // if not defined, we always assume ClipToLayer for saveLayer() //#define SK_SUPPORT_LEGACY_CLIPTOLAYERFLAG @@ -237,7 +231,6 @@ public: */ const void* peekPixels(SkImageInfo* info, size_t* rowBytes); -#ifdef SK_SUPPORT_LEGACY_CONFIG8888 /** * This enum can be used with read/writePixels to perform a pixel ops to or * from an 8888 config other than Skia's native config (SkPMColor). There @@ -272,7 +265,6 @@ public: kRGBA_Premul_Config8888, kRGBA_Unpremul_Config8888 }; -#endif #ifdef SK_SUPPORT_LEGACY_READPIXELSCONFIG /** diff --git a/include/core/SkColorPriv.h b/include/core/SkColorPriv.h index 9591f22725..7a74c4a034 100644 --- a/include/core/SkColorPriv.h +++ b/include/core/SkColorPriv.h @@ -1,3 +1,4 @@ + /* * Copyright 2006 The Android Open Source Project * @@ -5,6 +6,7 @@ * found in the LICENSE file. */ + #ifndef SkColorPriv_DEFINED #define SkColorPriv_DEFINED @@ -16,134 +18,6 @@ #include "SkColor.h" #include "SkMath.h" -////////////////////////////////////////////////////////////////////////////// - -#define SkASSERT_IS_BYTE(x) SkASSERT(0 == ((x) & ~0xFF)) - -/* - * Skia's 32bit backend only supports 1 sizzle order at a time (compile-time). - * This is specified by 4 defines SK_A32_SHIFT, SK_R32_SHIFT, ... for G and B. - * - * For easier compatibility with Skia's GPU backend, we further restrict these - * to either (in memory-byte-order) RGBA or BGRA. Note that this "order" does - * not directly correspond to the same shift-order, since we have to take endianess - * into account. - * - * Here we enforce this constraint. - */ - -#ifdef SK_CPU_BENDIAN - #define SK_RGBA_R32_SHIFT 24 - #define SK_RGBA_G32_SHIFT 16 - #define SK_RGBA_B32_SHIFT 8 - #define SK_RGBA_A32_SHIFT 0 - - #define SK_BGRA_B32_SHIFT 24 - #define SK_BGRA_G32_SHIFT 16 - #define SK_BGRA_R32_SHIFT 8 - #define SK_BGRA_A32_SHIFT 0 -#else - #define SK_RGBA_R32_SHIFT 0 - #define SK_RGBA_G32_SHIFT 8 - #define SK_RGBA_B32_SHIFT 16 - #define SK_RGBA_A32_SHIFT 24 - - #define SK_BGRA_B32_SHIFT 0 - #define SK_BGRA_G32_SHIFT 8 - #define SK_BGRA_R32_SHIFT 16 - #define SK_BGRA_A32_SHIFT 24 -#endif - -#if defined(SK_PMCOLOR_IS_RGBA) && defined(SK_PMCOLOR_IS_BGRA) - #error "can't define PMCOLOR to be RGBA and BGRA" -#endif - -#define LOCAL_PMCOLOR_SHIFTS_EQUIVALENT_TO_RGBA \ - (SK_A32_SHIFT == SK_RGBA_A32_SHIFT && \ - SK_R32_SHIFT == SK_RGBA_R32_SHIFT && \ - SK_G32_SHIFT == SK_RGBA_G32_SHIFT && \ - SK_B32_SHIFT == SK_RGBA_B32_SHIFT) - -#define LOCAL_PMCOLOR_SHIFTS_EQUIVALENT_TO_BGRA \ - (SK_A32_SHIFT == SK_BGRA_A32_SHIFT && \ - SK_R32_SHIFT == SK_BGRA_R32_SHIFT && \ - SK_G32_SHIFT == SK_BGRA_G32_SHIFT && \ - SK_B32_SHIFT == SK_BGRA_B32_SHIFT) - - -#if defined(SK_PMCOLOR_IS_RGBA) && !LOCAL_PMCOLOR_SHIFTS_EQUIVALENT_TO_RGBA - #error "SK_PMCOLOR_IS_RGBA does not match SK_*32_SHIFT values" -#endif - -#if defined(SK_PMCOLOR_IS_BGRA) && !LOCAL_PMCOLOR_SHIFTS_EQUIVALENT_TO_BGRA - #error "SK_PMCOLOR_IS_BGRA does not match SK_*32_SHIFT values" -#endif - -#if !defined(SK_PMCOLOR_IS_RGBA) && !defined(SK_PMCOLOR_IS_RGBA) - // deduce which to define from the _SHIFT defines - - #if LOCAL_PMCOLOR_SHIFTS_EQUIVALENT_TO_RGBA - #define SK_PMCOLOR_IS_RGBA - #elif LOCAL_PMCOLOR_SHIFTS_EQUIVALENT_TO_BGRA - #define SK_PMCOLOR_IS_BGRA - #else - #error "need 32bit packing to be either RGBA or BGRA" - #endif -#endif - -// hide these now that we're done -#undef LOCAL_PMCOLOR_SHIFTS_EQUIVALENT_TO_RGBA -#undef LOCAL_PMCOLOR_SHIFTS_EQUIVALENT_TO_BGRA - -////////////////////////////////////////////////////////////////////////////// - -// Reverse the bytes coorsponding to RED and BLUE in a packed pixels. Note the -// pair of them are in the same 2 slots in both RGBA and BGRA, thus there is -// no need to pass in the colortype to this function. -static inline uint32_t SkSwizzle_RB(uint32_t c) { - static const uint32_t kRBMask = (0xFF << SK_R32_SHIFT) | (0xFF << SK_B32_SHIFT); - - unsigned c0 = (c >> SK_R32_SHIFT) & 0xFF; - unsigned c1 = (c >> SK_B32_SHIFT) & 0xFF; - return (c & ~kRBMask) | (c0 << SK_B32_SHIFT) | (c1 << SK_R32_SHIFT); -} - -static inline uint32_t SkPackARGB_as_RGBA(U8CPU a, U8CPU r, U8CPU g, U8CPU b) { - SkASSERT_IS_BYTE(a); - SkASSERT_IS_BYTE(r); - SkASSERT_IS_BYTE(g); - SkASSERT_IS_BYTE(b); - return (a << SK_RGBA_A32_SHIFT) | (r << SK_RGBA_R32_SHIFT) | - (g << SK_RGBA_G32_SHIFT) | (b << SK_RGBA_B32_SHIFT); -} - -static inline uint32_t SkPackARGB_as_BGRA(U8CPU a, U8CPU r, U8CPU g, U8CPU b) { - SkASSERT_IS_BYTE(a); - SkASSERT_IS_BYTE(r); - SkASSERT_IS_BYTE(g); - SkASSERT_IS_BYTE(b); - return (a << SK_BGRA_A32_SHIFT) | (r << SK_BGRA_R32_SHIFT) | - (g << SK_BGRA_G32_SHIFT) | (b << SK_BGRA_B32_SHIFT); -} - -static inline SkPMColor SkSwizzle_RGBA_to_PMColor(uint32_t c) { -#ifdef SK_PMCOLOR_IS_RGBA - return c; -#else - return SkSwizzle_RB(c); -#endif -} - -static inline SkPMColor SkSwizzle_BGRA_to_PMColor(uint32_t c) { -#ifdef SK_PMCOLOR_IS_BGRA - return c; -#else - return SkSwizzle_RB(c); -#endif -} - -////////////////////////////////////////////////////////////////////////////// - ///@{ /** See ITU-R Recommendation BT.709 at http://www.itu.int/rec/R-REC-BT.709/ .*/ #define SK_ITU_BT709_LUM_COEFF_R (0.2126f) @@ -365,16 +239,6 @@ static inline SkPMColor SkPackARGB32(U8CPU a, U8CPU r, U8CPU g, U8CPU b) { (g << SK_G32_SHIFT) | (b << SK_B32_SHIFT); } -static inline uint32_t SkPackPMColor_as_RGBA(SkPMColor c) { - return SkPackARGB_as_RGBA(SkGetPackedA32(c), SkGetPackedR32(c), - SkGetPackedG32(c), SkGetPackedB32(c)); -} - -static inline uint32_t SkPackPMColor_as_BGRA(SkPMColor c) { - return SkPackARGB_as_BGRA(SkGetPackedA32(c), SkGetPackedR32(c), - SkGetPackedG32(c), SkGetPackedB32(c)); -} - /** * Abstract 4-byte interpolation, implemented on top of SkPMColor * utility functions. Third parameter controls blending of the first two: diff --git a/include/core/SkDevice.h b/include/core/SkDevice.h index 6b94dd635c..abd0a636ff 100644 --- a/include/core/SkDevice.h +++ b/include/core/SkDevice.h @@ -363,11 +363,9 @@ protected: const SkImageFilter::Context& ctx, SkBitmap* result, SkIPoint* offset) = 0; -#ifdef SK_SUPPORT_LEGACY_CONFIG8888 // This is equal kBGRA_Premul_Config8888 or kRGBA_Premul_Config8888 if // either is identical to kNative_Premul_Config8888. Otherwise, -1. static const SkCanvas::Config8888 kPMColorAlias; -#endif protected: // default impl returns NULL diff --git a/src/core/SkBitmapDevice.cpp b/src/core/SkBitmapDevice.cpp index d968cb850b..f0f4df41f1 100644 --- a/src/core/SkBitmapDevice.cpp +++ b/src/core/SkBitmapDevice.cpp @@ -221,27 +221,47 @@ static void rect_memcpy(void* dst, size_t dstRB, const void* src, size_t srcRB, } } -#include "SkConfig8888.h" +static bool info2config8888(const SkImageInfo& info, SkCanvas::Config8888* config) { + bool pre; + switch (info.alphaType()) { + case kPremul_SkAlphaType: + case kOpaque_SkAlphaType: + pre = true; + break; + case kUnpremul_SkAlphaType: + pre = false; + break; + default: + return false; + } + switch (info.colorType()) { + case kRGBA_8888_SkColorType: + *config = pre ? SkCanvas::kRGBA_Premul_Config8888 : SkCanvas::kRGBA_Unpremul_Config8888; + return true; + case kBGRA_8888_SkColorType: + *config = pre ? SkCanvas::kBGRA_Premul_Config8888 : SkCanvas::kBGRA_Unpremul_Config8888; + return true; + default: + return false; + } +} +// TODO: make this guy real, and not rely on legacy config8888 utility +#include "SkConfig8888.h" static bool copy_pixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes, const SkImageInfo& srcInfo, const void* srcPixels, size_t srcRowBytes) { if (srcInfo.dimensions() != dstInfo.dimensions()) { return false; } if (4 == srcInfo.bytesPerPixel() && 4 == dstInfo.bytesPerPixel()) { - SkDstPixelInfo dstPI; - dstPI.fColorType = dstInfo.colorType(); - dstPI.fAlphaType = dstInfo.alphaType(); - dstPI.fPixels = dstPixels; - dstPI.fRowBytes = dstRowBytes; - - SkSrcPixelInfo srcPI; - srcPI.fColorType = srcInfo.colorType(); - srcPI.fAlphaType = srcInfo.alphaType(); - srcPI.fPixels = srcPixels; - srcPI.fRowBytes = srcRowBytes; - - return srcPI.convertPixelsTo(&dstPI, srcInfo.width(), srcInfo.height()); + SkCanvas::Config8888 srcConfig, dstConfig; + if (!info2config8888(srcInfo, &srcConfig) || !info2config8888(dstInfo, &dstConfig)) { + return false; + } + SkConvertConfig8888Pixels((uint32_t*)dstPixels, dstRowBytes, dstConfig, + (const uint32_t*)srcPixels, srcRowBytes, srcConfig, + srcInfo.width(), srcInfo.height()); + return true; } if (srcInfo.colorType() == dstInfo.colorType()) { switch (srcInfo.colorType()) { diff --git a/src/core/SkConfig8888.cpp b/src/core/SkConfig8888.cpp index 189309dae6..dd5cbc4724 100644 --- a/src/core/SkConfig8888.cpp +++ b/src/core/SkConfig8888.cpp @@ -1,117 +1,280 @@ #include "SkConfig8888.h" -#include "SkColorPriv.h" #include "SkMathPriv.h" #include "SkUnPreMultiply.h" -enum AlphaVerb { - kNothing_AlphaVerb, - kPremul_AlphaVerb, - kUnpremul_AlphaVerb, -}; +namespace { -template <bool doSwapRB, AlphaVerb doAlpha> uint32_t convert32(uint32_t c) { - if (doSwapRB) { - c = SkSwizzle_RB(c); +template <int A_IDX, int R_IDX, int G_IDX, int B_IDX> +inline uint32_t pack_config8888(uint32_t a, uint32_t r, + uint32_t g, uint32_t b) { +#ifdef SK_CPU_LENDIAN + return (a << (A_IDX * 8)) | (r << (R_IDX * 8)) | + (g << (G_IDX * 8)) | (b << (B_IDX * 8)); +#else + return (a << ((3-A_IDX) * 8)) | (r << ((3-R_IDX) * 8)) | + (g << ((3-G_IDX) * 8)) | (b << ((3-B_IDX) * 8)); +#endif +} + +template <int A_IDX, int R_IDX, int G_IDX, int B_IDX> +inline void unpack_config8888(uint32_t color, + uint32_t* a, uint32_t* r, + uint32_t* g, uint32_t* b) { +#ifdef SK_CPU_LENDIAN + *a = (color >> (A_IDX * 8)) & 0xff; + *r = (color >> (R_IDX * 8)) & 0xff; + *g = (color >> (G_IDX * 8)) & 0xff; + *b = (color >> (B_IDX * 8)) & 0xff; +#else + *a = (color >> ((3 - A_IDX) * 8)) & 0xff; + *r = (color >> ((3 - R_IDX) * 8)) & 0xff; + *g = (color >> ((3 - G_IDX) * 8)) & 0xff; + *b = (color >> ((3 - B_IDX) * 8)) & 0xff; +#endif +} + +#ifdef SK_CPU_LENDIAN + static const int SK_NATIVE_A_IDX = SK_A32_SHIFT / 8; + static const int SK_NATIVE_R_IDX = SK_R32_SHIFT / 8; + static const int SK_NATIVE_G_IDX = SK_G32_SHIFT / 8; + static const int SK_NATIVE_B_IDX = SK_B32_SHIFT / 8; +#else + static const int SK_NATIVE_A_IDX = 3 - (SK_A32_SHIFT / 8); + static const int SK_NATIVE_R_IDX = 3 - (SK_R32_SHIFT / 8); + static const int SK_NATIVE_G_IDX = 3 - (SK_G32_SHIFT / 8); + static const int SK_NATIVE_B_IDX = 3 - (SK_B32_SHIFT / 8); +#endif + +/** + * convert_pixel<OUT_CFG, IN_CFG converts a pixel value from one Config8888 to + * another. It is implemented by first expanding OUT_CFG to r, g, b, a indices + * and an is_premul bool as params to another template function. Then IN_CFG is + * expanded via another function call. + */ + +template <bool OUT_PM, int OUT_A_IDX, int OUT_R_IDX, int OUT_G_IDX, int OUT_B_IDX, + bool IN_PM, int IN_A_IDX, int IN_R_IDX, int IN_G_IDX, int IN_B_IDX> +inline uint32_t convert_pixel(uint32_t pixel) { + uint32_t a, r, g, b; + unpack_config8888<IN_A_IDX, IN_R_IDX, IN_G_IDX, IN_B_IDX>(pixel, &a, &r, &g, &b); + if (IN_PM && !OUT_PM) { + // Using SkUnPreMultiply::ApplyScale is faster than (value * 0xff) / a. + if (a) { + SkUnPreMultiply::Scale scale = SkUnPreMultiply::GetScale(a); + r = SkUnPreMultiply::ApplyScale(scale, r); + g = SkUnPreMultiply::ApplyScale(scale, g); + b = SkUnPreMultiply::ApplyScale(scale, b); + } else { + return 0; + } + } else if (!IN_PM && OUT_PM) { + // This matches SkUnPreMultiply conversion which we are replacing. + r = SkMulDiv255Round(r, a); + g = SkMulDiv255Round(g, a); + b = SkMulDiv255Round(b, a); } + return pack_config8888<OUT_A_IDX, OUT_R_IDX, OUT_G_IDX, OUT_B_IDX>(a, r, g, b); +} - // Lucky for us, in both RGBA and BGRA, the alpha component is always in the same place, so - // we can perform premul or unpremul the same way without knowing the swizzles for RGB. - switch (doAlpha) { - case kNothing_AlphaVerb: - // no change +template <bool OUT_PM, int OUT_A_IDX, int OUT_R_IDX, int OUT_G_IDX, int OUT_B_IDX, SkCanvas::Config8888 IN_CFG> +inline uint32_t convert_pixel(uint32_t pixel) { + switch(IN_CFG) { + case SkCanvas::kNative_Premul_Config8888: + return convert_pixel<OUT_PM, OUT_A_IDX, OUT_R_IDX, OUT_G_IDX, OUT_B_IDX, + true, SK_NATIVE_A_IDX, SK_NATIVE_R_IDX, SK_NATIVE_G_IDX, SK_NATIVE_B_IDX>(pixel); + break; + case SkCanvas::kNative_Unpremul_Config8888: + return convert_pixel<OUT_PM, OUT_A_IDX, OUT_R_IDX, OUT_G_IDX, OUT_B_IDX, + false, SK_NATIVE_A_IDX, SK_NATIVE_R_IDX, SK_NATIVE_G_IDX, SK_NATIVE_B_IDX>(pixel); + break; + case SkCanvas::kBGRA_Premul_Config8888: + return convert_pixel<OUT_PM, OUT_A_IDX, OUT_R_IDX, OUT_G_IDX, OUT_B_IDX, + true, 3, 2, 1, 0>(pixel); + break; + case SkCanvas::kBGRA_Unpremul_Config8888: + return convert_pixel<OUT_PM, OUT_A_IDX, OUT_R_IDX, OUT_G_IDX, OUT_B_IDX, + false, 3, 2, 1, 0>(pixel); + break; + case SkCanvas::kRGBA_Premul_Config8888: + return convert_pixel<OUT_PM, OUT_A_IDX, OUT_R_IDX, OUT_G_IDX, OUT_B_IDX, + true, 3, 0, 1, 2>(pixel); break; - case kPremul_AlphaVerb: - c = SkPreMultiplyARGB(SkGetPackedA32(c), SkGetPackedR32(c), - SkGetPackedG32(c), SkGetPackedB32(c)); + case SkCanvas::kRGBA_Unpremul_Config8888: + return convert_pixel<OUT_PM, OUT_A_IDX, OUT_R_IDX, OUT_G_IDX, OUT_B_IDX, + false, 3, 0, 1, 2>(pixel); break; - case kUnpremul_AlphaVerb: - c = SkUnPreMultiply::UnPreMultiplyPreservingByteOrder(c); + default: + SkDEBUGFAIL("Unexpected config8888"); + return 0; break; } - return c; } -template <bool doSwapRB, AlphaVerb doAlpha> -void convert32_row(uint32_t* dst, const uint32_t* src, int count) { - // This has to be correct if src == dst (but not partial overlap) - for (int i = 0; i < count; ++i) { - dst[i] = convert32<doSwapRB, doAlpha>(src[i]); +template <SkCanvas::Config8888 OUT_CFG, SkCanvas::Config8888 IN_CFG> +inline uint32_t convert_pixel(uint32_t pixel) { + switch(OUT_CFG) { + case SkCanvas::kNative_Premul_Config8888: + return convert_pixel<true, SK_NATIVE_A_IDX, SK_NATIVE_R_IDX, SK_NATIVE_G_IDX, SK_NATIVE_B_IDX, IN_CFG>(pixel); + break; + case SkCanvas::kNative_Unpremul_Config8888: + return convert_pixel<false, SK_NATIVE_A_IDX, SK_NATIVE_R_IDX, SK_NATIVE_G_IDX, SK_NATIVE_B_IDX, IN_CFG>(pixel); + break; + case SkCanvas::kBGRA_Premul_Config8888: + return convert_pixel<true, 3, 2, 1, 0, IN_CFG>(pixel); + break; + case SkCanvas::kBGRA_Unpremul_Config8888: + return convert_pixel<false, 3, 2, 1, 0, IN_CFG>(pixel); + break; + case SkCanvas::kRGBA_Premul_Config8888: + return convert_pixel<true, 3, 0, 1, 2, IN_CFG>(pixel); + break; + case SkCanvas::kRGBA_Unpremul_Config8888: + return convert_pixel<false, 3, 0, 1, 2, IN_CFG>(pixel); + break; + default: + SkDEBUGFAIL("Unexpected config8888"); + return 0; + break; } } -static bool is_32bit_colortype(SkColorType ct) { - return kRGBA_8888_SkColorType == ct || kBGRA_8888_SkColorType == ct; -} +/** + * SkConvertConfig8888Pixels has 6 * 6 possible combinations of src and dst + * configs. Each is implemented as an instantiation templated function. Two + * levels of switch statements are used to select the correct instantiation, one + * for the src config and one for the dst config. + */ -static AlphaVerb compute_AlphaVerb(SkAlphaType src, SkAlphaType dst) { - SkASSERT(kIgnore_SkAlphaType != src); - SkASSERT(kIgnore_SkAlphaType != dst); +template <SkCanvas::Config8888 DST_CFG, SkCanvas::Config8888 SRC_CFG> +inline void convert_config8888(uint32_t* dstPixels, + size_t dstRowBytes, + const uint32_t* srcPixels, + size_t srcRowBytes, + int width, + int height) { + intptr_t dstPix = reinterpret_cast<intptr_t>(dstPixels); + intptr_t srcPix = reinterpret_cast<intptr_t>(srcPixels); - if (kOpaque_SkAlphaType == src || kOpaque_SkAlphaType == dst || src == dst) { - return kNothing_AlphaVerb; - } - if (kPremul_SkAlphaType == dst) { - SkASSERT(kUnpremul_SkAlphaType == src); - return kPremul_AlphaVerb; - } else { - SkASSERT(kPremul_SkAlphaType == src); - SkASSERT(kUnpremul_SkAlphaType == dst); - return kUnpremul_AlphaVerb; + for (int y = 0; y < height; ++y) { + srcPixels = reinterpret_cast<const uint32_t*>(srcPix); + dstPixels = reinterpret_cast<uint32_t*>(dstPix); + for (int x = 0; x < width; ++x) { + dstPixels[x] = convert_pixel<DST_CFG, SRC_CFG>(srcPixels[x]); + } + dstPix += dstRowBytes; + srcPix += srcRowBytes; } } -static void memcpy32_row(uint32_t* dst, const uint32_t* src, int count) { - memcpy(dst, src, count * 4); -} - -bool SkSrcPixelInfo::convertPixelsTo(SkDstPixelInfo* dst, int width, int height) const { - if (width <= 0 || height <= 0) { - return false; - } - - if (!is_32bit_colortype(fColorType) || !is_32bit_colortype(dst->fColorType)) { - return false; +template <SkCanvas::Config8888 SRC_CFG> +inline void convert_config8888(uint32_t* dstPixels, + size_t dstRowBytes, + SkCanvas::Config8888 dstConfig, + const uint32_t* srcPixels, + size_t srcRowBytes, + int width, + int height) { + switch(dstConfig) { + case SkCanvas::kNative_Premul_Config8888: + convert_config8888<SkCanvas::kNative_Premul_Config8888, SRC_CFG>(dstPixels, dstRowBytes, srcPixels, srcRowBytes, width, height); + break; + case SkCanvas::kNative_Unpremul_Config8888: + convert_config8888<SkCanvas::kNative_Unpremul_Config8888, SRC_CFG>(dstPixels, dstRowBytes, srcPixels, srcRowBytes, width, height); + break; + case SkCanvas::kBGRA_Premul_Config8888: + convert_config8888<SkCanvas::kBGRA_Premul_Config8888, SRC_CFG>(dstPixels, dstRowBytes, srcPixels, srcRowBytes, width, height); + break; + case SkCanvas::kBGRA_Unpremul_Config8888: + convert_config8888<SkCanvas::kBGRA_Unpremul_Config8888, SRC_CFG>(dstPixels, dstRowBytes, srcPixels, srcRowBytes, width, height); + break; + case SkCanvas::kRGBA_Premul_Config8888: + convert_config8888<SkCanvas::kRGBA_Premul_Config8888, SRC_CFG>(dstPixels, dstRowBytes, srcPixels, srcRowBytes, width, height); + break; + case SkCanvas::kRGBA_Unpremul_Config8888: + convert_config8888<SkCanvas::kRGBA_Unpremul_Config8888, SRC_CFG>(dstPixels, dstRowBytes, srcPixels, srcRowBytes, width, height); + break; + default: + SkDEBUGFAIL("Unexpected config8888"); + break; } +} - void (*proc)(uint32_t* dst, const uint32_t* src, int count); - AlphaVerb doAlpha = compute_AlphaVerb(fAlphaType, dst->fAlphaType); - bool doSwapRB = fColorType != dst->fColorType; +} - switch (doAlpha) { - case kNothing_AlphaVerb: - if (doSwapRB) { - proc = convert32_row<true, kNothing_AlphaVerb>; - } else { - if (fPixels == dst->fPixels) { - return true; - } - proc = memcpy32_row; +void SkConvertConfig8888Pixels(uint32_t* dstPixels, + size_t dstRowBytes, + SkCanvas::Config8888 dstConfig, + const uint32_t* srcPixels, + size_t srcRowBytes, + SkCanvas::Config8888 srcConfig, + int width, + int height) { + if (srcConfig == dstConfig) { + if (srcPixels == dstPixels) { + return; + } + if (dstRowBytes == srcRowBytes && + 4U * width == srcRowBytes) { + memcpy(dstPixels, srcPixels, srcRowBytes * height); + return; + } else { + intptr_t srcPix = reinterpret_cast<intptr_t>(srcPixels); + intptr_t dstPix = reinterpret_cast<intptr_t>(dstPixels); + for (int y = 0; y < height; ++y) { + srcPixels = reinterpret_cast<const uint32_t*>(srcPix); + dstPixels = reinterpret_cast<uint32_t*>(dstPix); + memcpy(dstPixels, srcPixels, 4 * width); + srcPix += srcRowBytes; + dstPix += dstRowBytes; } + return; + } + } + switch(srcConfig) { + case SkCanvas::kNative_Premul_Config8888: + convert_config8888<SkCanvas::kNative_Premul_Config8888>(dstPixels, dstRowBytes, dstConfig, srcPixels, srcRowBytes, width, height); break; - case kPremul_AlphaVerb: - if (doSwapRB) { - proc = convert32_row<true, kPremul_AlphaVerb>; - } else { - proc = convert32_row<false, kPremul_AlphaVerb>; - } + case SkCanvas::kNative_Unpremul_Config8888: + convert_config8888<SkCanvas::kNative_Unpremul_Config8888>(dstPixels, dstRowBytes, dstConfig, srcPixels, srcRowBytes, width, height); break; - case kUnpremul_AlphaVerb: - if (doSwapRB) { - proc = convert32_row<true, kUnpremul_AlphaVerb>; - } else { - proc = convert32_row<false, kUnpremul_AlphaVerb>; - } + case SkCanvas::kBGRA_Premul_Config8888: + convert_config8888<SkCanvas::kBGRA_Premul_Config8888>(dstPixels, dstRowBytes, dstConfig, srcPixels, srcRowBytes, width, height); + break; + case SkCanvas::kBGRA_Unpremul_Config8888: + convert_config8888<SkCanvas::kBGRA_Unpremul_Config8888>(dstPixels, dstRowBytes, dstConfig, srcPixels, srcRowBytes, width, height); + break; + case SkCanvas::kRGBA_Premul_Config8888: + convert_config8888<SkCanvas::kRGBA_Premul_Config8888>(dstPixels, dstRowBytes, dstConfig, srcPixels, srcRowBytes, width, height); + break; + case SkCanvas::kRGBA_Unpremul_Config8888: + convert_config8888<SkCanvas::kRGBA_Unpremul_Config8888>(dstPixels, dstRowBytes, dstConfig, srcPixels, srcRowBytes, width, height); + break; + default: + SkDEBUGFAIL("Unexpected config8888"); break; } +} - uint32_t* dstP = static_cast<uint32_t*>(dst->fPixels); - const uint32_t* srcP = static_cast<const uint32_t*>(fPixels); - size_t srcInc = fRowBytes >> 2; - size_t dstInc = dst->fRowBytes >> 2; - for (int y = 0; y < height; ++y) { - proc(dstP, srcP, width); - dstP += dstInc; - srcP += srcInc; +uint32_t SkPackConfig8888(SkCanvas::Config8888 config, + uint32_t a, + uint32_t r, + uint32_t g, + uint32_t b) { + switch (config) { + case SkCanvas::kNative_Premul_Config8888: + case SkCanvas::kNative_Unpremul_Config8888: + return pack_config8888<SK_NATIVE_A_IDX, + SK_NATIVE_R_IDX, + SK_NATIVE_G_IDX, + SK_NATIVE_B_IDX>(a, r, g, b); + case SkCanvas::kBGRA_Premul_Config8888: + case SkCanvas::kBGRA_Unpremul_Config8888: + return pack_config8888<3, 2, 1, 0>(a, r, g, b); + case SkCanvas::kRGBA_Premul_Config8888: + case SkCanvas::kRGBA_Unpremul_Config8888: + return pack_config8888<3, 0, 1, 2>(a, r, g, b); + default: + SkDEBUGFAIL("Unexpected config8888"); + return 0; } - return true; } diff --git a/src/core/SkConfig8888.h b/src/core/SkConfig8888.h index 97a3433ad2..041773e6df 100644 --- a/src/core/SkConfig8888.h +++ b/src/core/SkConfig8888.h @@ -5,27 +5,75 @@ * found in the LICENSE file. */ -#ifndef SkPixelInfo_DEFINED -#define SkPixelInfo_DEFINED +#ifndef SkConfig8888_DEFINED +#define SkConfig8888_DEFINED -#include "SkImageInfo.h" +#include "SkCanvas.h" +#include "SkColorPriv.h" -struct SkPixelInfo { - SkColorType fColorType; - SkAlphaType fAlphaType; - size_t fRowBytes; -}; +/** + * Converts pixels from one Config8888 to another Config8888 + */ +void SkConvertConfig8888Pixels(uint32_t* dstPixels, + size_t dstRowBytes, + SkCanvas::Config8888 dstConfig, + const uint32_t* srcPixels, + size_t srcRowBytes, + SkCanvas::Config8888 srcConfig, + int width, + int height); + +/** + * Packs a, r, g, b, values into byte order specified by config. + */ +uint32_t SkPackConfig8888(SkCanvas::Config8888 config, + uint32_t a, + uint32_t r, + uint32_t g, + uint32_t b); + +/////////////////////////////////////////////////////////////////////////////// +// Implementation + +namespace { -struct SkDstPixelInfo : SkPixelInfo { - void* fPixels; -}; +/** + Copies all pixels from a bitmap to a dst ptr with a given rowBytes and + Config8888. The bitmap must have kARGB_8888_Config. + */ + +static inline void SkCopyBitmapToConfig8888(uint32_t* dstPixels, + size_t dstRowBytes, + SkCanvas::Config8888 dstConfig8888, + const SkBitmap& srcBmp) { + SkASSERT(SkBitmap::kARGB_8888_Config == srcBmp.config()); + SkAutoLockPixels alp(srcBmp); + int w = srcBmp.width(); + int h = srcBmp.height(); + size_t srcRowBytes = srcBmp.rowBytes(); + const uint32_t* srcPixels = reinterpret_cast<uint32_t*>(srcBmp.getPixels()); + + SkConvertConfig8888Pixels(dstPixels, dstRowBytes, dstConfig8888, srcPixels, srcRowBytes, SkCanvas::kNative_Premul_Config8888, w, h); +} + +/** + Copies over all pixels in a bitmap from a src ptr with a given rowBytes and + Config8888. The bitmap must have pixels and be kARGB_8888_Config. + */ +static inline void SkCopyConfig8888ToBitmap(const SkBitmap& dstBmp, + const uint32_t* srcPixels, + size_t srcRowBytes, + SkCanvas::Config8888 srcConfig8888) { + SkASSERT(SkBitmap::kARGB_8888_Config == dstBmp.config()); + SkAutoLockPixels alp(dstBmp); + int w = dstBmp.width(); + int h = dstBmp.height(); + size_t dstRowBytes = dstBmp.rowBytes(); + uint32_t* dstPixels = reinterpret_cast<uint32_t*>(dstBmp.getPixels()); -struct SkSrcPixelInfo : SkPixelInfo { - const void* fPixels; + SkConvertConfig8888Pixels(dstPixels, dstRowBytes, SkCanvas::kNative_Premul_Config8888, srcPixels, srcRowBytes, srcConfig8888, w, h); +} - // Guaranteed to work even if src.fPixels and dst.fPixels are the same - // (but not if they overlap partially) - bool convertPixelsTo(SkDstPixelInfo* dst, int width, int height) const; -}; +} #endif diff --git a/src/core/SkDevice.cpp b/src/core/SkDevice.cpp index 4c75a36363..ac46755013 100644 --- a/src/core/SkDevice.cpp +++ b/src/core/SkDevice.cpp @@ -8,6 +8,16 @@ #include "SkDevice.h" #include "SkMetaData.h" +#if SK_PMCOLOR_BYTE_ORDER(B,G,R,A) + const SkCanvas::Config8888 SkBaseDevice::kPMColorAlias = SkCanvas::kBGRA_Premul_Config8888; +#elif SK_PMCOLOR_BYTE_ORDER(R,G,B,A) + const SkCanvas::Config8888 SkBaseDevice::kPMColorAlias = SkCanvas::kRGBA_Premul_Config8888; +#else + const SkCanvas::Config8888 SkBaseDevice::kPMColorAlias = (SkCanvas::Config8888) -1; +#endif + +/////////////////////////////////////////////////////////////////////////////// + SkBaseDevice::SkBaseDevice() : fLeakyProperties(SkDeviceProperties::MakeDefault()) #ifdef SK_DEBUG diff --git a/src/gpu/GrContext.cpp b/src/gpu/GrContext.cpp index 9a277b0591..c016d062ab 100644 --- a/src/gpu/GrContext.cpp +++ b/src/gpu/GrContext.cpp @@ -25,7 +25,6 @@ #include "GrSoftwarePathRenderer.h" #include "GrStencilBuffer.h" #include "GrTextStrike.h" -#include "SkGr.h" #include "SkRTConf.h" #include "SkRRect.h" #include "SkStrokeRec.h" @@ -1260,15 +1259,54 @@ bool GrContext::readTexturePixels(GrTexture* texture, #include "SkConfig8888.h" -// toggles between RGBA and BGRA -static SkColorType toggle_colortype32(SkColorType ct) { - if (kRGBA_8888_SkColorType == ct) { - return kBGRA_8888_SkColorType; - } else { - SkASSERT(kBGRA_8888_SkColorType == ct); - return kRGBA_8888_SkColorType; +namespace { +/** + * Converts a GrPixelConfig to a SkCanvas::Config8888. Only byte-per-channel + * formats are representable as Config8888 and so the function returns false + * if the GrPixelConfig has no equivalent Config8888. + */ +bool grconfig_to_config8888(GrPixelConfig config, + bool unpremul, + SkCanvas::Config8888* config8888) { + switch (config) { + case kRGBA_8888_GrPixelConfig: + if (unpremul) { + *config8888 = SkCanvas::kRGBA_Unpremul_Config8888; + } else { + *config8888 = SkCanvas::kRGBA_Premul_Config8888; + } + return true; + case kBGRA_8888_GrPixelConfig: + if (unpremul) { + *config8888 = SkCanvas::kBGRA_Unpremul_Config8888; + } else { + *config8888 = SkCanvas::kBGRA_Premul_Config8888; + } + return true; + default: + return false; + } +} + +// It returns a configuration with where the byte position of the R & B components are swapped in +// relation to the input config. This should only be called with the result of +// grconfig_to_config8888 as it will fail for other configs. +SkCanvas::Config8888 swap_config8888_red_and_blue(SkCanvas::Config8888 config8888) { + switch (config8888) { + case SkCanvas::kBGRA_Premul_Config8888: + return SkCanvas::kRGBA_Premul_Config8888; + case SkCanvas::kBGRA_Unpremul_Config8888: + return SkCanvas::kRGBA_Unpremul_Config8888; + case SkCanvas::kRGBA_Premul_Config8888: + return SkCanvas::kBGRA_Premul_Config8888; + case SkCanvas::kRGBA_Unpremul_Config8888: + return SkCanvas::kBGRA_Unpremul_Config8888; + default: + GrCrash("Unexpected input"); + return SkCanvas::kBGRA_Unpremul_Config8888;; } } +} bool GrContext::readRenderTargetPixels(GrRenderTarget* target, int left, int top, int width, int height, @@ -1394,21 +1432,22 @@ bool GrContext::readRenderTargetPixels(GrRenderTarget* target, } // Perform any conversions we weren't able to perform using a scratch texture. if (unpremul || swapRAndB) { - SkDstPixelInfo dstPI; - if (!GrPixelConfig2ColorType(dstConfig, &dstPI.fColorType)) { - return false; - } - dstPI.fAlphaType = kUnpremul_SkAlphaType; - dstPI.fPixels = buffer; - dstPI.fRowBytes = rowBytes; + // These are initialized to suppress a warning + SkCanvas::Config8888 srcC8888 = SkCanvas::kNative_Premul_Config8888; + SkCanvas::Config8888 dstC8888 = SkCanvas::kNative_Premul_Config8888; - SkSrcPixelInfo srcPI; - srcPI.fColorType = swapRAndB ? toggle_colortype32(dstPI.fColorType) : dstPI.fColorType; - srcPI.fAlphaType = kPremul_SkAlphaType; - srcPI.fPixels = buffer; - srcPI.fRowBytes = rowBytes; + SkDEBUGCODE(bool c8888IsValid =) grconfig_to_config8888(dstConfig, false, &srcC8888); + grconfig_to_config8888(dstConfig, unpremul, &dstC8888); - return srcPI.convertPixelsTo(&dstPI, width, height); + if (swapRAndB) { + SkASSERT(c8888IsValid); // we should only do r/b swap on 8888 configs + srcC8888 = swap_config8888_red_and_blue(srcC8888); + } + SkASSERT(c8888IsValid); + uint32_t* b32 = reinterpret_cast<uint32_t*>(buffer); + SkConvertConfig8888Pixels(b32, rowBytes, dstC8888, + b32, rowBytes, srcC8888, + width, height); } return true; } @@ -1528,24 +1567,18 @@ bool GrContext::writeRenderTargetPixels(GrRenderTarget* target, effect.reset(this->createUPMToPMEffect(texture, swapRAndB, textureMatrix)); // handle the unpremul step on the CPU if we couldn't create an effect to do it. if (NULL == effect) { - SkSrcPixelInfo srcPI; - if (!GrPixelConfig2ColorType(srcConfig, &srcPI.fColorType)) { - return false; - } - srcPI.fAlphaType = kUnpremul_SkAlphaType; - srcPI.fPixels = buffer; - srcPI.fRowBytes = rowBytes; - - SkDstPixelInfo dstPI; - dstPI.fColorType = srcPI.fColorType; - dstPI.fAlphaType = kPremul_SkAlphaType; - dstPI.fPixels = tmpPixels.get(); - dstPI.fRowBytes = 4 * width; - - if (!srcPI.convertPixelsTo(&dstPI, width, height)) { - return false; - } - + SkCanvas::Config8888 srcConfig8888, dstConfig8888; + SkDEBUGCODE(bool success = ) + grconfig_to_config8888(srcConfig, true, &srcConfig8888); + SkASSERT(success); + SkDEBUGCODE(success = ) + grconfig_to_config8888(srcConfig, false, &dstConfig8888); + SkASSERT(success); + const uint32_t* src = reinterpret_cast<const uint32_t*>(buffer); + tmpPixels.reset(width * height); + SkConvertConfig8888Pixels(tmpPixels.get(), 4 * width, dstConfig8888, + src, rowBytes, srcConfig8888, + width, height); buffer = tmpPixels.get(); rowBytes = 4 * width; } diff --git a/src/gpu/SkGr.cpp b/src/gpu/SkGr.cpp index db46b86901..94e4c8c35d 100644 --- a/src/gpu/SkGr.cpp +++ b/src/gpu/SkGr.cpp @@ -34,22 +34,11 @@ static void build_compressed_data(void* buffer, const SkBitmap& bitmap) { SkColorTable* ctable = bitmap.getColorTable(); char* dst = (char*)buffer; - const int count = ctable->count(); - - SkDstPixelInfo dstPI; - dstPI.fColorType = kRGBA_8888_SkColorType; - dstPI.fAlphaType = kPremul_SkAlphaType; - dstPI.fPixels = buffer; - dstPI.fRowBytes = count * sizeof(SkPMColor); - - SkSrcPixelInfo srcPI; - srcPI.fColorType = kPMColor_SkColorType; - srcPI.fAlphaType = kPremul_SkAlphaType; - srcPI.fPixels = ctable->lockColors(); - srcPI.fRowBytes = count * sizeof(SkPMColor); - - srcPI.convertPixelsTo(&dstPI, count, 1); - + uint32_t* colorTableDst = reinterpret_cast<uint32_t*>(dst); + const uint32_t* colorTableSrc = reinterpret_cast<const uint32_t*>(ctable->lockColors()); + SkConvertConfig8888Pixels(colorTableDst, 0, SkCanvas::kRGBA_Premul_Config8888, + colorTableSrc, 0, SkCanvas::kNative_Premul_Config8888, + ctable->count(), 1); ctable->unlockColors(); // always skip a full 256 number of entries, even if we memcpy'd fewer diff --git a/tests/DeferredCanvasTest.cpp b/tests/DeferredCanvasTest.cpp index 0af0c51fed..aa360be401 100644 --- a/tests/DeferredCanvasTest.cpp +++ b/tests/DeferredCanvasTest.cpp @@ -26,6 +26,21 @@ class GrContextFactory; static const int gWidth = 2; static const int gHeight = 2; +static void callWritePixels(SkCanvas* canvas, const SkBitmap& src, int x, int y, + SkCanvas::Config8888 config) { + SkBitmap bm(src); + bm.lockPixels(); + + SkImageInfo info = bm.info(); + sk_tool_utils::config8888_to_imagetypes(config, &info.fColorType, &info.fAlphaType); + + if (src.isOpaque()) { + info.fAlphaType = kOpaque_SkAlphaType; + } + + canvas->writePixels(info, bm.getPixels(), bm.rowBytes(), x, y); +} + static void create(SkBitmap* bm, SkColor color) { bm->allocN32Pixels(gWidth, gHeight); bm->eraseColor(color); @@ -107,7 +122,7 @@ static void TestDeferredCanvasWritePixelsToSurface(skiatest::Reporter* reporter) SkAutoTUnref<SkDeferredCanvas> canvas(SkDeferredCanvas::Create(surface.get())); SkBitmap srcBitmap; - srcBitmap.allocPixels(SkImageInfo::Make(10, 10, kRGBA_8888_SkColorType, kUnpremul_SkAlphaType)); + srcBitmap.allocN32Pixels(10, 10); srcBitmap.eraseColor(SK_ColorGREEN); // Tests below depend on this bitmap being recognized as opaque @@ -159,7 +174,7 @@ static void TestDeferredCanvasWritePixelsToSurface(skiatest::Reporter* reporter) REPORTER_ASSERT(reporter, 0 == surface->fRetainCount); surface->clearCounts(); - canvas->writePixels(srcBitmap, 0, 0); + callWritePixels(canvas, srcBitmap, 0, 0, SkCanvas::kRGBA_Unpremul_Config8888); REPORTER_ASSERT(reporter, 1 == surface->fDiscardCount); REPORTER_ASSERT(reporter, 0 == surface->fRetainCount); @@ -176,7 +191,7 @@ static void TestDeferredCanvasWritePixelsToSurface(skiatest::Reporter* reporter) REPORTER_ASSERT(reporter, 0 == surface->fRetainCount); surface->clearCounts(); - canvas->writePixels(srcBitmap, 5, 0); + callWritePixels(canvas, srcBitmap, 5, 0, SkCanvas::kRGBA_Unpremul_Config8888); REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount); REPORTER_ASSERT(reporter, 1 == surface->fRetainCount); @@ -198,7 +213,7 @@ static void TestDeferredCanvasWritePixelsToSurface(skiatest::Reporter* reporter) REPORTER_ASSERT(reporter, 0 == surface->fRetainCount); surface->clearCounts(); - canvas->writePixels(srcBitmap, 0, 0); + callWritePixels(canvas, srcBitmap, 0, 0, SkCanvas::kRGBA_Unpremul_Config8888); REPORTER_ASSERT(reporter, 1 == surface->fDiscardCount); REPORTER_ASSERT(reporter, 0 == surface->fRetainCount); @@ -220,7 +235,7 @@ static void TestDeferredCanvasWritePixelsToSurface(skiatest::Reporter* reporter) REPORTER_ASSERT(reporter, 0 == surface->fRetainCount); surface->clearCounts(); - canvas->writePixels(srcBitmap, 5, 0); + callWritePixels(canvas, srcBitmap, 5, 0, SkCanvas::kRGBA_Unpremul_Config8888); REPORTER_ASSERT(reporter, 1 == surface->fDiscardCount); // because of the clear REPORTER_ASSERT(reporter, 0 == surface->fRetainCount); @@ -244,7 +259,7 @@ static void TestDeferredCanvasWritePixelsToSurface(skiatest::Reporter* reporter) REPORTER_ASSERT(reporter, 0 == surface->fRetainCount); surface->clearCounts(); - canvas->writePixels(srcBitmap, 5, 0); + callWritePixels(canvas, srcBitmap, 5, 0, SkCanvas::kRGBA_Unpremul_Config8888); REPORTER_ASSERT(reporter, 0 == surface->fDiscardCount); REPORTER_ASSERT(reporter, 1 == surface->fRetainCount); diff --git a/tests/WritePixelsTest.cpp b/tests/WritePixelsTest.cpp index a1c978d1b1..fc0fe37d6c 100644 --- a/tests/WritePixelsTest.cpp +++ b/tests/WritePixelsTest.cpp @@ -56,18 +56,41 @@ static SkPMColor getCanvasColor(int x, int y) { return SkPremultiplyARGBInline(a, r, g, b); } +static bool config8888IsPremul(SkCanvas::Config8888 config8888) { + switch (config8888) { + case SkCanvas::kNative_Premul_Config8888: + case SkCanvas::kBGRA_Premul_Config8888: + case SkCanvas::kRGBA_Premul_Config8888: + return true; + case SkCanvas::kNative_Unpremul_Config8888: + case SkCanvas::kBGRA_Unpremul_Config8888: + case SkCanvas::kRGBA_Unpremul_Config8888: + return false; + default: + SkASSERT(0); + return false; + } +} + // assumes any premu/.unpremul has been applied -static uint32_t packColorType(SkColorType ct, U8CPU a, U8CPU r, U8CPU g, U8CPU b) { +static uint32_t packConfig8888(SkCanvas::Config8888 config8888, + U8CPU a, U8CPU r, U8CPU g, U8CPU b) { uint32_t r32; uint8_t* result = reinterpret_cast<uint8_t*>(&r32); - switch (ct) { - case kBGRA_8888_SkColorType: + switch (config8888) { + case SkCanvas::kNative_Premul_Config8888: + case SkCanvas::kNative_Unpremul_Config8888: + r32 = SkPackARGB32NoCheck(a, r, g, b); + break; + case SkCanvas::kBGRA_Premul_Config8888: + case SkCanvas::kBGRA_Unpremul_Config8888: result[0] = b; result[1] = g; result[2] = r; result[3] = a; break; - case kRGBA_8888_SkColorType: + case SkCanvas::kRGBA_Premul_Config8888: + case SkCanvas::kRGBA_Unpremul_Config8888: result[0] = r; result[1] = g; result[2] = b; @@ -80,7 +103,7 @@ static uint32_t packColorType(SkColorType ct, U8CPU a, U8CPU r, U8CPU g, U8CPU b return r32; } -static uint32_t getBitmapColor(int x, int y, int w, SkColorType ct, SkAlphaType at) { +static uint32_t getBitmapColor(int x, int y, int w, SkCanvas::Config8888 config8888) { int n = y * w + x; U8CPU b = n & 0xff; U8CPU g = (n >> 8) & 0xff; @@ -103,12 +126,12 @@ static uint32_t getBitmapColor(int x, int y, int w, SkColorType ct, SkAlphaType a = 0x00; break; } - if (kPremul_SkAlphaType == at) { + if (config8888IsPremul(config8888)) { r = SkMulDiv255Ceiling(r, a); g = SkMulDiv255Ceiling(g, a); b = SkMulDiv255Ceiling(b, a); } - return packColorType(ct, a, r, g , b); + return packConfig8888(config8888, a, r, g , b); } static void fillCanvas(SkCanvas* canvas) { @@ -131,38 +154,48 @@ static void fillCanvas(SkCanvas* canvas) { canvas->restore(); } -/** - * Lucky for us, alpha is always in the same spot (SK_A32_SHIFT), for both RGBA and BGRA. - * Thus this routine doesn't need to know the exact colortype - */ -static uint32_t premul(uint32_t color) { - unsigned a = SkGetPackedA32(color); - // these next three are not necessarily r,g,b in that order, but they are r,g,b in some order. - unsigned c0 = SkGetPackedR32(color); - unsigned c1 = SkGetPackedG32(color); - unsigned c2 = SkGetPackedB32(color); - c0 = SkMulDiv255Ceiling(c0, a); - c1 = SkMulDiv255Ceiling(c1, a); - c2 = SkMulDiv255Ceiling(c2, a); - return SkPackARGB32NoCheck(a, c0, c1, c2); -} - -static SkPMColor convert_to_PMColor(SkColorType ct, SkAlphaType at, uint32_t color) { - if (kUnpremul_SkAlphaType == at) { - color = premul(color); - } - switch (ct) { - case kRGBA_8888_SkColorType: - color = SkSwizzle_RGBA_to_PMColor(color); +static SkPMColor convertConfig8888ToPMColor(SkCanvas::Config8888 config8888, + uint32_t color, + bool* premul) { + const uint8_t* c = reinterpret_cast<uint8_t*>(&color); + U8CPU a,r,g,b; + *premul = false; + switch (config8888) { + case SkCanvas::kNative_Premul_Config8888: + return color; + case SkCanvas::kNative_Unpremul_Config8888: + *premul = true; + a = SkGetPackedA32(color); + r = SkGetPackedR32(color); + g = SkGetPackedG32(color); + b = SkGetPackedB32(color); break; - case kBGRA_8888_SkColorType: - color = SkSwizzle_BGRA_to_PMColor(color); + case SkCanvas::kBGRA_Unpremul_Config8888: + *premul = true; // fallthru + case SkCanvas::kBGRA_Premul_Config8888: + a = static_cast<U8CPU>(c[3]); + r = static_cast<U8CPU>(c[2]); + g = static_cast<U8CPU>(c[1]); + b = static_cast<U8CPU>(c[0]); break; - default: - SkASSERT(0); + case SkCanvas::kRGBA_Unpremul_Config8888: + *premul = true; // fallthru + case SkCanvas::kRGBA_Premul_Config8888: + a = static_cast<U8CPU>(c[3]); + r = static_cast<U8CPU>(c[0]); + g = static_cast<U8CPU>(c[1]); + b = static_cast<U8CPU>(c[2]); break; + default: + SkDEBUGFAIL("Unexpected Config8888"); + return 0; } - return color; + if (*premul) { + r = SkMulDiv255Ceiling(r, a); + g = SkMulDiv255Ceiling(g, a); + b = SkMulDiv255Ceiling(b, a); + } + return SkPackARGB32(a, r, g, b); } static bool checkPixel(SkPMColor a, SkPMColor b, bool didPremulConversion) { @@ -185,54 +218,39 @@ static bool checkPixel(SkPMColor a, SkPMColor b, bool didPremulConversion) { SkAbs32(aB - bB) <= 1; } -static bool checkWrite(skiatest::Reporter* reporter, SkCanvas* canvas, const SkBitmap& bitmap, - int writeX, int writeY) { - SkImageInfo canvasInfo; - size_t canvasRowBytes; - const uint32_t* canvasPixels; - - // Can't use canvas->peekPixels(), as we are trying to look at GPU pixels sometimes as well. - // At some point this will be unsupported, as we won't allow accessBitmap() to magically call - // readPixels for the client. - SkBitmap secretDevBitmap; - { - SkBaseDevice* dev = canvas->getDevice(); - if (!dev) { - return false; - } - secretDevBitmap = dev->accessBitmap(false); - } - SkAutoLockPixels alp(secretDevBitmap); - canvasInfo = secretDevBitmap.info(); - canvasRowBytes = secretDevBitmap.rowBytes(); - canvasPixels = static_cast<const uint32_t*>(secretDevBitmap.getPixels()); - - if (NULL == canvasPixels) { +static bool checkWrite(skiatest::Reporter* reporter, + SkCanvas* canvas, + const SkBitmap& bitmap, + int writeX, int writeY, + SkCanvas::Config8888 config8888) { + SkBaseDevice* dev = canvas->getDevice(); + if (!dev) { return false; } - - if (canvasInfo.width() != DEV_W || - canvasInfo.height() != DEV_H || - canvasInfo.colorType() != kPMColor_SkColorType) { + SkBitmap devBmp = dev->accessBitmap(false); + if (devBmp.width() != DEV_W || + devBmp.height() != DEV_H || + devBmp.config() != SkBitmap::kARGB_8888_Config || + devBmp.isNull()) { return false; } + SkAutoLockPixels alp(devBmp); - const SkImageInfo bmInfo = bitmap.info(); - + intptr_t canvasPixels = reinterpret_cast<intptr_t>(devBmp.getPixels()); + size_t canvasRowBytes = devBmp.rowBytes(); SkIRect writeRect = SkIRect::MakeXYWH(writeX, writeY, bitmap.width(), bitmap.height()); for (int cy = 0; cy < DEV_H; ++cy) { + const SkPMColor* canvasRow = reinterpret_cast<const SkPMColor*>(canvasPixels); for (int cx = 0; cx < DEV_W; ++cx) { - SkPMColor canvasPixel = canvasPixels[cx]; + SkPMColor canvasPixel = canvasRow[cx]; if (writeRect.contains(cx, cy)) { int bx = cx - writeX; int by = cy - writeY; - uint32_t bmpColor8888 = getBitmapColor(bx, by, bitmap.width(), - bmInfo.colorType(), bmInfo.alphaType()); - bool mul = (kUnpremul_SkAlphaType == bmInfo.alphaType()); - SkPMColor bmpPMColor = convert_to_PMColor(bmInfo.colorType(), bmInfo.alphaType(), - bmpColor8888); - bool check = checkPixel(bmpPMColor, canvasPixel, mul); - REPORTER_ASSERT(reporter, check); + uint32_t bmpColor8888 = getBitmapColor(bx, by, bitmap.width(), config8888); + bool mul; + SkPMColor bmpPMColor = convertConfig8888ToPMColor(config8888, bmpColor8888, &mul); + bool check; + REPORTER_ASSERT(reporter, check = checkPixel(bmpPMColor, canvasPixel, mul)); if (!check) { return false; } @@ -246,7 +264,7 @@ static bool checkWrite(skiatest::Reporter* reporter, SkCanvas* canvas, const SkB } } if (cy != DEV_H -1) { - const char* pad = reinterpret_cast<const char*>(canvasPixels + DEV_W); + const char* pad = reinterpret_cast<const char*>(canvasPixels + 4 * DEV_W); for (size_t px = 0; px < canvasRowBytes - 4 * DEV_W; ++px) { bool check; REPORTER_ASSERT(reporter, check = (pad[px] == static_cast<char>(DEV_PAD))); @@ -255,7 +273,7 @@ static bool checkWrite(skiatest::Reporter* reporter, SkCanvas* canvas, const SkB } } } - canvasPixels += canvasRowBytes/4; + canvasPixels += canvasRowBytes; } return true; @@ -336,16 +354,21 @@ static SkBaseDevice* createDevice(const CanvasConfig& c, GrContext* grCtx) { return NULL; } -static bool setupBitmap(SkBitmap* bm, SkColorType ct, SkAlphaType at, int w, int h, int tightRB) { - size_t rowBytes = tightRB ? 0 : 4 * w + 60; - SkImageInfo info = SkImageInfo::Make(w, h, ct, at); - if (!allocRowBytes(bm, info, rowBytes)) { +static bool setupBitmap(SkBitmap* bitmap, + SkCanvas::Config8888 config8888, + int w, int h, + bool tightRowBytes) { + size_t rowBytes = tightRowBytes ? 0 : 4 * w + 60; + SkImageInfo info = SkImageInfo::MakeN32Premul(w, h); + if (!allocRowBytes(bitmap, info, rowBytes)) { return false; } - SkAutoLockPixels alp(*bm); + SkAutoLockPixels alp(*bitmap); + intptr_t pixels = reinterpret_cast<intptr_t>(bitmap->getPixels()); for (int y = 0; y < h; ++y) { for (int x = 0; x < w; ++x) { - *bm->getAddr32(x, y) = getBitmapColor(x, y, w, ct, at); + uint32_t* pixel = reinterpret_cast<uint32_t*>(pixels + y * bitmap->rowBytes() + x * 4); + *pixel = getBitmapColor(x, y, w, config8888); } } return true; @@ -429,33 +452,31 @@ DEF_GPUTEST(WritePixels, reporter, factory) { SkAutoTUnref<SkBaseDevice> device(createDevice(gCanvasConfigs[i], context)); SkCanvas canvas(device); - static const struct { - SkColorType fColorType; - SkAlphaType fAlphaType; - } gSrcConfigs[] = { - { kRGBA_8888_SkColorType, kPremul_SkAlphaType }, - { kRGBA_8888_SkColorType, kUnpremul_SkAlphaType }, - { kBGRA_8888_SkColorType, kPremul_SkAlphaType }, - { kBGRA_8888_SkColorType, kUnpremul_SkAlphaType }, + static const SkCanvas::Config8888 gSrcConfigs[] = { + SkCanvas::kNative_Premul_Config8888, + SkCanvas::kNative_Unpremul_Config8888, + SkCanvas::kBGRA_Premul_Config8888, + SkCanvas::kBGRA_Unpremul_Config8888, + SkCanvas::kRGBA_Premul_Config8888, + SkCanvas::kRGBA_Unpremul_Config8888, }; for (size_t r = 0; r < SK_ARRAY_COUNT(testRects); ++r) { const SkIRect& rect = testRects[r]; for (int tightBmp = 0; tightBmp < 2; ++tightBmp) { for (size_t c = 0; c < SK_ARRAY_COUNT(gSrcConfigs); ++c) { - const SkColorType ct = gSrcConfigs[c].fColorType; - const SkAlphaType at = gSrcConfigs[c].fAlphaType; - fillCanvas(&canvas); + SkCanvas::Config8888 config8888 = gSrcConfigs[c]; SkBitmap bmp; - REPORTER_ASSERT(reporter, setupBitmap(&bmp, ct, at, rect.width(), - rect.height(), SkToBool(tightBmp))); + REPORTER_ASSERT(reporter, setupBitmap(&bmp, config8888, rect.width(), rect.height(), SkToBool(tightBmp))); uint32_t idBefore = canvas.getDevice()->accessBitmap(false).getGenerationID(); - // sk_tool_utils::write_pixels(&canvas, bmp, rect.fLeft, rect.fTop, ct, at); - canvas.writePixels(bmp, rect.fLeft, rect.fTop); + SkColorType ct; + SkAlphaType at; + sk_tool_utils::config8888_to_imagetypes(config8888, &ct, &at); + sk_tool_utils::write_pixels(&canvas, bmp, rect.fLeft, rect.fTop, ct, at); uint32_t idAfter = canvas.getDevice()->accessBitmap(false).getGenerationID(); - REPORTER_ASSERT(reporter, checkWrite(reporter, &canvas, bmp, rect.fLeft, rect.fTop)); + REPORTER_ASSERT(reporter, checkWrite(reporter, &canvas, bmp, rect.fLeft, rect.fTop, config8888)); // we should change the genID iff pixels were actually written. SkIRect canvasRect = SkIRect::MakeSize(canvas.getDeviceSize()); diff --git a/tools/sk_tool_utils.cpp b/tools/sk_tool_utils.cpp index 3eb55554b6..d13b0d81f8 100644 --- a/tools/sk_tool_utils.cpp +++ b/tools/sk_tool_utils.cpp @@ -17,6 +17,37 @@ const char* colortype_name(SkColorType ct) { } } +void config8888_to_imagetypes(SkCanvas::Config8888 config, SkColorType* ct, SkAlphaType* at) { + switch (config) { + case SkCanvas::kNative_Premul_Config8888: + *ct = kPMColor_SkColorType; + *at = kPremul_SkAlphaType; + break; + case SkCanvas::kNative_Unpremul_Config8888: + *ct = kPMColor_SkColorType; + *at = kUnpremul_SkAlphaType; + break; + case SkCanvas::kBGRA_Premul_Config8888: + *ct = kBGRA_8888_SkColorType; + *at = kPremul_SkAlphaType; + break; + case SkCanvas::kBGRA_Unpremul_Config8888: + *ct = kBGRA_8888_SkColorType; + *at = kUnpremul_SkAlphaType; + break; + case SkCanvas::kRGBA_Premul_Config8888: + *ct = kRGBA_8888_SkColorType; + *at = kPremul_SkAlphaType; + break; + case SkCanvas::kRGBA_Unpremul_Config8888: + *ct = kRGBA_8888_SkColorType; + *at = kUnpremul_SkAlphaType; + break; + default: + SkASSERT(0); + } +} + void write_pixels(SkCanvas* canvas, const SkBitmap& bitmap, int x, int y, SkColorType colorType, SkAlphaType alphaType) { SkBitmap tmp(bitmap); diff --git a/tools/sk_tool_utils.h b/tools/sk_tool_utils.h index 48fd716342..ac1cdabf15 100644 --- a/tools/sk_tool_utils.h +++ b/tools/sk_tool_utils.h @@ -16,6 +16,11 @@ namespace sk_tool_utils { const char* colortype_name(SkColorType); /** + * Return the colorType and alphaType that correspond to the specified Config8888 + */ + void config8888_to_imagetypes(SkCanvas::Config8888, SkColorType*, SkAlphaType*); + + /** * Call canvas->writePixels() by using the pixels from bitmap, but with an info that claims * the pixels are colorType + alphaType */ |