1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
|
/*
* Copyright (c) 2008, Google Inc. All rights reserved.
* Copyright (C) 2009 Dirk Schulze <krit@webkit.org>
* Copyright (C) 2010 Torch Mobile (Beijing) Co. Ltd. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "platform/graphics/ImageBuffer.h"
#include "GrContext.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/core/SkPicture.h"
#include "third_party/skia/include/effects/SkTableColorFilter.h"
#include "wtf/MathExtras.h"
#include "wtf/Vector.h"
#include "wtf/text/Base64.h"
#include "wtf/text/WTFString.h"
namespace blink {
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)
{
if (m_surface->canvas()) {
m_context = adoptPtr(new GraphicsContext(m_surface->canvas(), 0));
m_context->setAccelerated(m_surface->isAccelerated());
}
m_surface->setImageBuffer(this);
}
ImageBuffer::~ImageBuffer()
{
}
GraphicsContext* ImageBuffer::context() const
{
if (!isSurfaceValid())
return 0;
ASSERT(m_context.get());
return m_context.get();
}
const SkBitmap& ImageBuffer::bitmap() const
{
return m_surface->bitmap();
}
bool ImageBuffer::isSurfaceValid() const
{
return m_surface->isValid();
}
bool ImageBuffer::isDirty()
{
return m_client ? m_client->isDirty() : false;
}
void ImageBuffer::didFinalizeFrame()
{
if (m_client)
m_client->didFinalizeFrame();
}
void ImageBuffer::finalizeFrame(const FloatRect &dirtyRect)
{
m_surface->finalizeFrame(dirtyRect);
didFinalizeFrame();
}
bool ImageBuffer::restoreSurface() const
{
return m_surface->isValid() || m_surface->restore();
}
void ImageBuffer::notifySurfaceInvalid()
{
if (m_client)
m_client->notifySurfaceInvalid();
}
void ImageBuffer::resetCanvas(SkCanvas* canvas)
{
ASSERT(context()->canvas());
context()->resetCanvas(canvas);
if (m_client)
m_client->restoreCanvasMatrixClipStack();
}
PassRefPtr<SkImage> ImageBuffer::newImageSnapshot() const
{
return m_surface->newImageSnapshot();
}
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;
}
WebLayer* ImageBuffer::platformLayer() const
{
return m_surface->layer();
}
bool ImageBuffer::copyToPlatformTexture(WebGraphicsContext3D* context, Platform3DObject texture, GLenum internalFormat, GLenum destType, GLint level, bool premultiplyAlpha, bool flipY)
{
if (!m_surface->isAccelerated() || !getBackingTexture() || !isSurfaceValid())
return false;
if (!Extensions3DUtil::canUseCopyTextureCHROMIUM(internalFormat, destType, level))
return false;
OwnPtr<WebGraphicsContext3DProvider> provider = adoptPtr(Platform::current()->createSharedOffscreenGraphicsContext3DProvider());
if (!provider)
return false;
WebGraphicsContext3D* sharedContext = provider->context3d();
if (!sharedContext)
return false;
OwnPtr<WebExternalTextureMailbox> mailbox = adoptPtr(new WebExternalTextureMailbox);
// Contexts may be in a different share group. We must transfer the texture through a mailbox first
sharedContext->genMailboxCHROMIUM(mailbox->name);
sharedContext->produceTextureDirectCHROMIUM(getBackingTexture(), GL_TEXTURE_2D, mailbox->name);
sharedContext->flush();
mailbox->syncPoint = sharedContext->insertSyncPoint();
context->waitSyncPoint(mailbox->syncPoint);
Platform3DObject sourceTexture = context->createAndConsumeTextureCHROMIUM(GL_TEXTURE_2D, mailbox->name);
// The canvas is stored in a premultiplied format, so unpremultiply if necessary.
context->pixelStorei(GC3D_UNPACK_UNPREMULTIPLY_ALPHA_CHROMIUM, !premultiplyAlpha);
// The canvas is stored in an inverted position, so the flip semantics are reversed.
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->deleteTexture(sourceTexture);
context->flush();
sharedContext->waitSyncPoint(context->insertSyncPoint());
// Undo grContext texture binding changes introduced in this function
provider->grContext()->resetContext(kTextureBinding_GrGLBackendState);
return true;
}
static bool drawNeedsCopy(GraphicsContext* src, GraphicsContext* dst)
{
ASSERT(dst);
return (src == dst);
}
Platform3DObject ImageBuffer::getBackingTexture()
{
return m_surface->getBackingTexture();
}
void ImageBuffer::didModifyBackingTexture()
{
m_surface->didModifyBackingTexture();
}
bool ImageBuffer::copyRenderingResultsFromDrawingBuffer(DrawingBuffer* drawingBuffer, SourceDrawingBuffer sourceBuffer)
{
if (!drawingBuffer)
return false;
OwnPtr<WebGraphicsContext3DProvider> provider = adoptPtr(Platform::current()->createSharedOffscreenGraphicsContext3DProvider());
if (!provider)
return false;
WebGraphicsContext3D* context3D = provider->context3d();
Platform3DObject tex = m_surface->getBackingTexture();
if (!context3D || !tex)
return false;
m_surface->invalidateCachedBitmap();
bool result = drawingBuffer->copyToPlatformTexture(context3D, tex, GL_RGBA,
GL_UNSIGNED_BYTE, 0, true, false, sourceBuffer);
if (result) {
m_surface->didModifyBackingTexture();
}
return result;
}
void ImageBuffer::draw(GraphicsContext* context, const FloatRect& destRect, const FloatRect* srcPtr, CompositeOperator op, WebBlendMode blendMode)
{
if (!isSurfaceValid())
return;
FloatRect srcRect = srcPtr ? *srcPtr : FloatRect(FloatPoint(), size());
m_surface->draw(context, destRect, srcRect, op, blendMode, drawNeedsCopy(m_context.get(), context));
}
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, 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);
}
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));
}
PassRefPtr<Uint8ClampedArray> ImageBuffer::getImageData(Multiply multiplied, const IntRect& rect) const
{
if (!isSurfaceValid())
return Uint8ClampedArray::create(rect.width() * rect.height() * 4);
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() > m_surface->size().width()
|| rect.maxY() > m_surface->size().height())
result->zeroFill();
SkAlphaType alphaType = (multiplied == Premultiplied) ? kPremul_SkAlphaType : kUnpremul_SkAlphaType;
SkImageInfo info = SkImageInfo::Make(rect.width(), rect.height(), kRGBA_8888_SkColorType, alphaType);
m_surface->willAccessPixels();
context()->readPixels(info, result->data(), 4 * rect.width(), rect.x(), rect.y());
return result.release();
}
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;
SkAlphaType alphaType = (multiplied == Premultiplied) ? kPremul_SkAlphaType : kUnpremul_SkAlphaType;
SkImageInfo info = SkImageInfo::Make(sourceRect.width(), sourceRect.height(), kRGBA_8888_SkColorType, alphaType);
m_surface->willAccessPixels();
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:,";
return "data:" + mimeType + ";base64," + base64Encode(encodedImage);
}
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:,";
return "data:" + mimeType + ";base64," + base64Encode(encodedImage);
}
} // namespace blink
|