This source file includes following definitions.
- create
- create
- m_client
- context
- bitmap
- isSurfaceValid
- restoreSurface
- notifySurfaceInvalid
- deepSkBitmapCopy
- copyImage
- fastCopyImageMode
- platformLayer
- copyToPlatformTexture
- drawNeedsCopy
- getBackingTexture
- copyRenderingResultsFromDrawingBuffer
- draw
- flush
- drawPattern
- transformColorSpace
- createColorSpaceFilter
- getImageData
- getUnmultipliedImageData
- getPremultipliedImageData
- putByteArray
- encodeImage
- toDataURL
- ImageDataToDataURL
#include "config.h"
#include "platform/graphics/ImageBuffer.h"
#include "platform/MIMETypeRegistry.h"
#include "platform/geometry/IntRect.h"
#include "platform/graphics/BitmapImage.h"
#include "platform/graphics/GraphicsContext.h"
#include "platform/graphics/GraphicsTypes3D.h"
#include "platform/graphics/ImageBufferClient.h"
#include "platform/graphics/UnacceleratedImageBufferSurface.h"
#include "platform/graphics/gpu/DrawingBuffer.h"
#include "platform/graphics/gpu/Extensions3DUtil.h"
#include "platform/graphics/skia/NativeImageSkia.h"
#include "platform/graphics/skia/SkiaUtils.h"
#include "platform/image-encoders/skia/JPEGImageEncoder.h"
#include "platform/image-encoders/skia/PNGImageEncoder.h"
#include "platform/image-encoders/skia/WEBPImageEncoder.h"
#include "public/platform/Platform.h"
#include "public/platform/WebExternalTextureMailbox.h"
#include "public/platform/WebGraphicsContext3D.h"
#include "public/platform/WebGraphicsContext3DProvider.h"
#include "third_party/skia/include/effects/SkTableColorFilter.h"
#include "wtf/MathExtras.h"
#include "wtf/text/Base64.h"
#include "wtf/text/WTFString.h"
using namespace std;
namespace WebCore {
PassOwnPtr<ImageBuffer> ImageBuffer::create(PassOwnPtr<ImageBufferSurface> surface)
{
if (!surface->isValid())
return nullptr;
return adoptPtr(new ImageBuffer(surface));
}
PassOwnPtr<ImageBuffer> ImageBuffer::create(const IntSize& size, OpacityMode opacityMode)
{
OwnPtr<ImageBufferSurface> surface = adoptPtr(new UnacceleratedImageBufferSurface(size, opacityMode));
if (!surface->isValid())
return nullptr;
return adoptPtr(new ImageBuffer(surface.release()));
}
ImageBuffer::ImageBuffer(PassOwnPtr<ImageBufferSurface> surface)
: m_surface(surface)
, m_client(0)
{
m_surface->setImageBuffer(this);
if (m_surface->canvas()) {
m_context = adoptPtr(new GraphicsContext(m_surface->canvas()));
m_context->setCertainlyOpaque(m_surface->opacityMode() == Opaque);
m_context->setAccelerated(m_surface->isAccelerated());
}
}
ImageBuffer::~ImageBuffer()
{
}
GraphicsContext* ImageBuffer::context() const
{
if (!isSurfaceValid())
return 0;
m_surface->willUse();
ASSERT(m_context.get());
return m_context.get();
}
const SkBitmap& ImageBuffer::bitmap() const
{
m_surface->willUse();
return m_surface->bitmap();
}
bool ImageBuffer::isSurfaceValid() const
{
return m_surface->isValid();
}
bool ImageBuffer::restoreSurface() const
{
return m_surface->isValid() || m_surface->restore();
}
void ImageBuffer::notifySurfaceInvalid()
{
if (m_client)
m_client->notifySurfaceInvalid();
}
static SkBitmap deepSkBitmapCopy(const SkBitmap& bitmap)
{
SkBitmap tmp;
if (!bitmap.deepCopyTo(&tmp))
bitmap.copyTo(&tmp, bitmap.colorType());
return tmp;
}
PassRefPtr<Image> ImageBuffer::copyImage(BackingStoreCopy copyBehavior, ScaleBehavior) const
{
if (!isSurfaceValid())
return BitmapImage::create(NativeImageSkia::create());
const SkBitmap& bitmap = m_surface->bitmap();
return BitmapImage::create(NativeImageSkia::create(copyBehavior == CopyBackingStore ? deepSkBitmapCopy(bitmap) : bitmap));
}
BackingStoreCopy ImageBuffer::fastCopyImageMode()
{
return DontCopyBackingStore;
}
blink::WebLayer* ImageBuffer::platformLayer() const
{
return m_surface->layer();
}
bool ImageBuffer::copyToPlatformTexture(blink::WebGraphicsContext3D* context, Platform3DObject texture, GLenum internalFormat, GLenum destType, GLint level, bool premultiplyAlpha, bool flipY)
{
if (!m_surface->isAccelerated() || !platformLayer() || !isSurfaceValid())
return false;
if (!Extensions3DUtil::canUseCopyTextureCHROMIUM(internalFormat, destType, level))
return false;
OwnPtr<blink::WebGraphicsContext3DProvider> provider = adoptPtr(blink::Platform::current()->createSharedOffscreenGraphicsContext3DProvider());
if (!provider)
return false;
blink::WebGraphicsContext3D* sharedContext = provider->context3d();
if (!sharedContext || !sharedContext->makeContextCurrent())
return false;
OwnPtr<blink::WebExternalTextureMailbox> mailbox = adoptPtr(new blink::WebExternalTextureMailbox);
sharedContext->genMailboxCHROMIUM(mailbox->name);
sharedContext->bindTexture(GL_TEXTURE_2D, getBackingTexture());
sharedContext->produceTextureCHROMIUM(GL_TEXTURE_2D, mailbox->name);
sharedContext->flush();
mailbox->syncPoint = sharedContext->insertSyncPoint();
if (!context->makeContextCurrent())
return false;
Platform3DObject sourceTexture = context->createTexture();
context->bindTexture(GL_TEXTURE_2D, sourceTexture);
context->waitSyncPoint(mailbox->syncPoint);
context->consumeTextureCHROMIUM(GL_TEXTURE_2D, mailbox->name);
context->pixelStorei(GC3D_UNPACK_UNPREMULTIPLY_ALPHA_CHROMIUM, !premultiplyAlpha);
context->pixelStorei(GC3D_UNPACK_FLIP_Y_CHROMIUM, !flipY);
context->copyTextureCHROMIUM(GL_TEXTURE_2D, sourceTexture, texture, level, internalFormat, destType);
context->pixelStorei(GC3D_UNPACK_FLIP_Y_CHROMIUM, false);
context->pixelStorei(GC3D_UNPACK_UNPREMULTIPLY_ALPHA_CHROMIUM, false);
context->bindTexture(GL_TEXTURE_2D, 0);
context->deleteTexture(sourceTexture);
context->flush();
sharedContext->waitSyncPoint(context->insertSyncPoint());
return true;
}
static bool drawNeedsCopy(GraphicsContext* src, GraphicsContext* dst)
{
ASSERT(dst);
return (src == dst);
}
Platform3DObject ImageBuffer::getBackingTexture()
{
return m_surface->getBackingTexture();
}
bool ImageBuffer::copyRenderingResultsFromDrawingBuffer(DrawingBuffer* drawingBuffer)
{
if (!drawingBuffer)
return false;
OwnPtr<blink::WebGraphicsContext3DProvider> provider = adoptPtr(blink::Platform::current()->createSharedOffscreenGraphicsContext3DProvider());
if (!provider)
return false;
blink::WebGraphicsContext3D* context3D = provider->context3d();
Platform3DObject tex = m_surface->getBackingTexture();
if (!context3D || !tex)
return false;
m_surface->invalidateCachedBitmap();
return drawingBuffer->copyToPlatformTexture(context3D, tex, GL_RGBA,
GL_UNSIGNED_BYTE, 0, true, false);
}
void ImageBuffer::draw(GraphicsContext* context, const FloatRect& destRect, const FloatRect& srcRect, CompositeOperator op, blink::WebBlendMode blendMode)
{
if (!isSurfaceValid())
return;
SkBitmap bitmap = m_surface->bitmap();
if (!context->isAccelerated() && m_surface->isAccelerated() && m_surface->cachedBitmapEnabled() && isSurfaceValid()) {
m_surface->updateCachedBitmapIfNeeded();
bitmap = m_surface->cachedBitmap();
}
RefPtr<Image> image = BitmapImage::create(NativeImageSkia::create(drawNeedsCopy(m_context.get(), context) ? deepSkBitmapCopy(bitmap) : bitmap));
context->drawImage(image.get(), destRect, srcRect, op, blendMode, DoNotRespectImageOrientation);
}
void ImageBuffer::flush()
{
if (m_surface->canvas()) {
m_surface->canvas()->flush();
}
}
void ImageBuffer::drawPattern(GraphicsContext* context, const FloatRect& srcRect, const FloatSize& scale,
const FloatPoint& phase, CompositeOperator op, const FloatRect& destRect, blink::WebBlendMode blendMode, const IntSize& repeatSpacing)
{
if (!isSurfaceValid())
return;
const SkBitmap& bitmap = m_surface->bitmap();
RefPtr<Image> image = BitmapImage::create(NativeImageSkia::create(drawNeedsCopy(m_context.get(), context) ? deepSkBitmapCopy(bitmap) : bitmap));
image->drawPattern(context, srcRect, scale, phase, op, destRect, blendMode, repeatSpacing);
}
void ImageBuffer::transformColorSpace(ColorSpace srcColorSpace, ColorSpace dstColorSpace)
{
const uint8_t* lookUpTable = ColorSpaceUtilities::getConversionLUT(dstColorSpace, srcColorSpace);
if (!lookUpTable)
return;
if (context()->isAccelerated() || !isSurfaceValid())
return;
const SkBitmap& bitmap = m_surface->bitmap();
if (bitmap.isNull())
return;
ASSERT(bitmap.colorType() == kPMColor_SkColorType);
IntSize size = m_surface->size();
SkAutoLockPixels bitmapLock(bitmap);
for (int y = 0; y < size.height(); ++y) {
uint32_t* srcRow = bitmap.getAddr32(0, y);
for (int x = 0; x < size.width(); ++x) {
SkColor color = SkPMColorToColor(srcRow[x]);
srcRow[x] = SkPreMultiplyARGB(
SkColorGetA(color),
lookUpTable[SkColorGetR(color)],
lookUpTable[SkColorGetG(color)],
lookUpTable[SkColorGetB(color)]);
}
}
}
PassRefPtr<SkColorFilter> ImageBuffer::createColorSpaceFilter(ColorSpace srcColorSpace,
ColorSpace dstColorSpace)
{
const uint8_t* lut = ColorSpaceUtilities::getConversionLUT(dstColorSpace, srcColorSpace);
if (!lut)
return nullptr;
return adoptRef(SkTableColorFilter::CreateARGB(0, lut, lut, lut));
}
template <Multiply multiplied>
PassRefPtr<Uint8ClampedArray> getImageData(const IntRect& rect, GraphicsContext* context, const IntSize& size)
{
float area = 4.0f * rect.width() * rect.height();
if (area > static_cast<float>(std::numeric_limits<int>::max()))
return nullptr;
RefPtr<Uint8ClampedArray> result = Uint8ClampedArray::createUninitialized(rect.width() * rect.height() * 4);
if (rect.x() < 0
|| rect.y() < 0
|| rect.maxX() > size.width()
|| rect.maxY() > size.height())
result->zeroFill();
SkAlphaType alphaType = (multiplied == Premultiplied) ? kPremul_SkAlphaType : kUnpremul_SkAlphaType;
SkImageInfo info = SkImageInfo::Make(rect.width(), rect.height(), kRGBA_8888_SkColorType, alphaType);
context->readPixels(info, result->data(), 4 * rect.width(), rect.x(), rect.y());
return result.release();
}
PassRefPtr<Uint8ClampedArray> ImageBuffer::getUnmultipliedImageData(const IntRect& rect) const
{
if (!isSurfaceValid())
return Uint8ClampedArray::create(rect.width() * rect.height() * 4);
return getImageData<Unmultiplied>(rect, context(), m_surface->size());
}
PassRefPtr<Uint8ClampedArray> ImageBuffer::getPremultipliedImageData(const IntRect& rect) const
{
if (!isSurfaceValid())
return Uint8ClampedArray::create(rect.width() * rect.height() * 4);
return getImageData<Premultiplied>(rect, context(), m_surface->size());
}
void ImageBuffer::putByteArray(Multiply multiplied, Uint8ClampedArray* source, const IntSize& sourceSize, const IntRect& sourceRect, const IntPoint& destPoint)
{
if (!isSurfaceValid())
return;
ASSERT(sourceRect.width() > 0);
ASSERT(sourceRect.height() > 0);
int originX = sourceRect.x();
int destX = destPoint.x() + sourceRect.x();
ASSERT(destX >= 0);
ASSERT(destX < m_surface->size().width());
ASSERT(originX >= 0);
ASSERT(originX < sourceRect.maxX());
int originY = sourceRect.y();
int destY = destPoint.y() + sourceRect.y();
ASSERT(destY >= 0);
ASSERT(destY < m_surface->size().height());
ASSERT(originY >= 0);
ASSERT(originY < sourceRect.maxY());
const size_t srcBytesPerRow = 4 * sourceSize.width();
const void* srcAddr = source->data() + originY * srcBytesPerRow + originX * 4;
const SkAlphaType alphaType = (multiplied == Premultiplied) ? kPremul_SkAlphaType : kUnpremul_SkAlphaType;
SkImageInfo info = SkImageInfo::Make(sourceRect.width(), sourceRect.height(), kRGBA_8888_SkColorType, alphaType);
context()->writePixels(info, srcAddr, srcBytesPerRow, destX, destY);
}
template <typename T>
static bool encodeImage(T& source, const String& mimeType, const double* quality, Vector<char>* output)
{
Vector<unsigned char>* encodedImage = reinterpret_cast<Vector<unsigned char>*>(output);
if (mimeType == "image/jpeg") {
int compressionQuality = JPEGImageEncoder::DefaultCompressionQuality;
if (quality && *quality >= 0.0 && *quality <= 1.0)
compressionQuality = static_cast<int>(*quality * 100 + 0.5);
if (!JPEGImageEncoder::encode(source, compressionQuality, encodedImage))
return false;
} else if (mimeType == "image/webp") {
int compressionQuality = WEBPImageEncoder::DefaultCompressionQuality;
if (quality && *quality >= 0.0 && *quality <= 1.0)
compressionQuality = static_cast<int>(*quality * 100 + 0.5);
if (!WEBPImageEncoder::encode(source, compressionQuality, encodedImage))
return false;
} else {
if (!PNGImageEncoder::encode(source, encodedImage))
return false;
ASSERT(mimeType == "image/png");
}
return true;
}
String ImageBuffer::toDataURL(const String& mimeType, const double* quality) const
{
ASSERT(MIMETypeRegistry::isSupportedImageMIMETypeForEncoding(mimeType));
Vector<char> encodedImage;
if (!isSurfaceValid() || !encodeImage(m_surface->bitmap(), mimeType, quality, &encodedImage))
return "data:,";
Vector<char> base64Data;
base64Encode(encodedImage, base64Data);
return "data:" + mimeType + ";base64," + base64Data;
}
String ImageDataToDataURL(const ImageDataBuffer& imageData, const String& mimeType, const double* quality)
{
ASSERT(MIMETypeRegistry::isSupportedImageMIMETypeForEncoding(mimeType));
Vector<char> encodedImage;
if (!encodeImage(imageData, mimeType, quality, &encodedImage))
return "data:,";
Vector<char> base64Data;
base64Encode(encodedImage, base64Data);
return "data:" + mimeType + ";base64," + base64Data;
}
}