GraphicsContext3DSkia.cpp [plain text]
#include "config.h"
#if ENABLE(WEBGL)
#include "GraphicsContext3D.h"
#include "BitmapImage.h"
#include "Image.h"
#include "ImageSource.h"
#include "NativeImageSkia.h"
#include "SkColorPriv.h"
#include <wtf/OwnPtr.h>
#include <wtf/PassOwnPtr.h>
#include <algorithm>
namespace WebCore {
bool GraphicsContext3D::getImageData(Image* image,
GC3Denum format,
GC3Denum type,
bool premultiplyAlpha,
bool ignoreGammaAndColorProfile,
Vector<uint8_t>& outputVector)
{
if (!image)
return false;
OwnPtr<NativeImageSkia> pixels;
NativeImageSkia* skiaImage = image->nativeImageForCurrentFrame();
AlphaOp neededAlphaOp = AlphaDoNothing;
bool hasAlpha = skiaImage ? !skiaImage->bitmap().isOpaque() : true;
if ((!skiaImage || ignoreGammaAndColorProfile || (hasAlpha && !premultiplyAlpha)) && image->data()) {
ImageSource decoder(ImageSource::AlphaNotPremultiplied,
ignoreGammaAndColorProfile ? ImageSource::GammaAndColorProfileIgnored : ImageSource::GammaAndColorProfileApplied);
decoder.setData(image->data(), true);
if (!decoder.frameCount() || !decoder.frameIsCompleteAtIndex(0))
return false;
hasAlpha = decoder.frameHasAlphaAtIndex(0);
pixels = adoptPtr(decoder.createFrameAtIndex(0));
if (!pixels.get() || !pixels->isDataComplete() || !pixels->bitmap().width() || !pixels->bitmap().height())
return false;
SkBitmap::Config skiaConfig = pixels->bitmap().config();
if (skiaConfig != SkBitmap::kARGB_8888_Config)
return false;
skiaImage = pixels.get();
if (hasAlpha && premultiplyAlpha)
neededAlphaOp = AlphaDoPremultiply;
} else if (!premultiplyAlpha && hasAlpha)
neededAlphaOp = AlphaDoUnmultiply;
if (!skiaImage)
return false;
const SkBitmap& skiaImageRef = skiaImage->bitmap();
SkAutoLockPixels lock(skiaImageRef);
ASSERT(skiaImageRef.rowBytes() == skiaImageRef.width() * 4);
outputVector.resize(skiaImageRef.rowBytes() * skiaImageRef.height());
return packPixels(reinterpret_cast<const uint8_t*>(skiaImageRef.getPixels()),
SK_B32_SHIFT ? SourceFormatRGBA8 : SourceFormatBGRA8,
skiaImageRef.width(), skiaImageRef.height(), 0,
format, type, neededAlphaOp, outputVector.data());
}
}
#endif // ENABLE(WEBGL)