diff options
Diffstat (limited to 'src/core/SkBitmapDevice.cpp')
-rw-r--r-- | src/core/SkBitmapDevice.cpp | 91 |
1 files changed, 91 insertions, 0 deletions
diff --git a/src/core/SkBitmapDevice.cpp b/src/core/SkBitmapDevice.cpp index 7ed8058c6a..098f0fc96c 100644 --- a/src/core/SkBitmapDevice.cpp +++ b/src/core/SkBitmapDevice.cpp @@ -199,6 +199,7 @@ bool SkBitmapDevice::onReadPixels(const SkBitmap& bitmap, return true; } +#ifdef SK_SUPPORT_LEGACY_WRITEPIXELSCONFIG void SkBitmapDevice::writePixels(const SkBitmap& bitmap, int x, int y, SkCanvas::Config8888 config8888) { @@ -265,6 +266,96 @@ void SkBitmapDevice::writePixels(const SkBitmap& bitmap, draw.fMatrix = &SkMatrix::I(); this->drawSprite(draw, *sprite, x, y, paint); } +#endif + +static void rect_memcpy(void* dst, size_t dstRB, const void* src, size_t srcRB, size_t bytesPerRow, + int rowCount) { + SkASSERT(bytesPerRow <= srcRB); + SkASSERT(bytesPerRow <= dstRB); + for (int i = 0; i < rowCount; ++i) { + memcpy(dst, src, bytesPerRow); + dst = (char*)dst + dstRB; + src = (const char*)src + srcRB; + } +} + +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 write_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()) { + 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()) { + case kRGB_565_SkColorType: + case kAlpha_8_SkColorType: + break; + case kARGB_4444_SkColorType: + if (srcInfo.alphaType() != dstInfo.alphaType()) { + return false; + } + break; + default: + return false; + } + rect_memcpy(dstPixels, dstRowBytes, srcPixels, srcRowBytes, + srcInfo.width() * srcInfo.bytesPerPixel(), srcInfo.height()); + } + // TODO: add support for more conversions as needed + return false; +} + +bool SkBitmapDevice::onWritePixels(const SkImageInfo& srcInfo, const void* srcPixels, + size_t srcRowBytes, int x, int y) { + SkImageInfo dstInfo = fBitmap.info(); + dstInfo.fWidth = srcInfo.width(); + dstInfo.fHeight = srcInfo.height(); + + void* dstPixels = fBitmap.getAddr(x, y); + size_t dstRowBytes = fBitmap.rowBytes(); + + if (write_pixels(dstInfo, dstPixels, dstRowBytes, srcInfo, srcPixels, srcRowBytes)) { + fBitmap.notifyPixelsChanged(); + return true; + } + return false; +} /////////////////////////////////////////////////////////////////////////////// |