This source file includes following definitions.
- m_externalMemoryAllocated
- shader
- setPatternSpaceTransform
#include "config.h"
#include "platform/graphics/Pattern.h"
#include <v8.h>
#include "SkCanvas.h"
#include "SkColorShader.h"
#include "platform/graphics/skia/SkiaUtils.h"
namespace WebCore {
Pattern::Pattern(PassRefPtr<Image> image, bool repeatX, bool repeatY)
: m_repeatX(repeatX)
, m_repeatY(repeatY)
, m_externalMemoryAllocated(0)
{
if (image) {
m_tileImage = image->nativeImageForCurrentFrame();
}
}
Pattern::~Pattern()
{
if (m_externalMemoryAllocated)
v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory(-m_externalMemoryAllocated);
}
SkShader* Pattern::shader()
{
if (m_pattern)
return m_pattern.get();
if (!m_tileImage)
m_pattern = adoptRef(new SkColorShader(SK_ColorTRANSPARENT));
else if (m_repeatX && m_repeatY)
m_pattern = adoptRef(SkShader::CreateBitmapShader(m_tileImage->bitmap(), SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode));
else {
SkShader::TileMode tileModeX = m_repeatX ? SkShader::kRepeat_TileMode : SkShader::kClamp_TileMode;
SkShader::TileMode tileModeY = m_repeatY ? SkShader::kRepeat_TileMode : SkShader::kClamp_TileMode;
int expandW = m_repeatX ? 0 : 1;
int expandH = m_repeatY ? 0 : 1;
SkImageInfo info = m_tileImage->bitmap().info();
info.fWidth += expandW;
info.fHeight += expandH;
info.fAlphaType = kPremul_SkAlphaType;
SkBitmap bm2;
bm2.allocPixels(info);
bm2.eraseARGB(0x00, 0x00, 0x00, 0x00);
SkCanvas canvas(bm2);
canvas.drawBitmap(m_tileImage->bitmap(), 0, 0);
bm2.setImmutable();
m_pattern = adoptRef(SkShader::CreateBitmapShader(bm2, tileModeX, tileModeY));
m_externalMemoryAllocated = static_cast<int>(std::min(static_cast<size_t>(INT_MAX), bm2.getSafeSize()));
v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory(m_externalMemoryAllocated);
}
m_pattern->setLocalMatrix(affineTransformToSkMatrix(m_patternSpaceTransformation));
return m_pattern.get();
}
void Pattern::setPatternSpaceTransform(const AffineTransform& patternSpaceTransformation)
{
m_patternSpaceTransformation = patternSpaceTransformation;
if (m_pattern)
m_pattern->setLocalMatrix(affineTransformToSkMatrix(m_patternSpaceTransformation));
}
}