Chromium Code Reviews| Index: Source/platform/graphics/ImagePattern.cpp |
| diff --git a/Source/platform/graphics/ImagePattern.cpp b/Source/platform/graphics/ImagePattern.cpp |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..20d4f0ea9011c0f56b1ed2cea0ec4354619e31f5 |
| --- /dev/null |
| +++ b/Source/platform/graphics/ImagePattern.cpp |
| @@ -0,0 +1,76 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "config.h" |
| +#include "platform/graphics/ImagePattern.h" |
| + |
| +#include "platform/graphics/Image.h" |
| +#include "platform/graphics/skia/SkiaUtils.h" |
| +#include "third_party/skia/include/core/SkCanvas.h" |
| +#include "third_party/skia/include/core/SkImage.h" |
| +#include "third_party/skia/include/core/SkPicture.h" |
| +#include "third_party/skia/include/core/SkPictureRecorder.h" |
| +#include "third_party/skia/include/core/SkShader.h" |
| + |
| +namespace blink { |
| + |
| +PassRefPtr<ImagePattern> ImagePattern::create(PassRefPtr<Image> image, RepeatMode repeatMode) |
| +{ |
| + return adoptRef(new ImagePattern(image, repeatMode)); |
| +} |
| + |
| +ImagePattern::ImagePattern(PassRefPtr<Image> image, RepeatMode repeatMode) |
| + : Pattern(repeatMode) |
| + , m_tileImage(image->imageForCurrentFrame()) |
| +{ |
| + if (m_tileImage) { |
| + // TODO(fmalita): mechanism to extract the actual SkImageInfo from an SkImage? |
| + const SkImageInfo info = |
| + SkImageInfo::MakeN32Premul(m_tileImage->width(), m_tileImage->height()); |
| + adjustExternalMemoryAllocated(info.getSafeSize(info.minRowBytes())); |
| + } |
| +} |
| + |
| +PassRefPtr<SkShader> ImagePattern::createShader() |
| +{ |
| + if (!m_tileImage) |
| + return adoptRef(SkShader::CreateColorShader(SK_ColorTRANSPARENT)); |
| + |
| + SkMatrix localMatrix = affineTransformToSkMatrix(m_patternSpaceTransformation); |
| + |
| + if (isRepeatXY()) { |
| + return adoptRef(m_tileImage->newShader(SkShader::kRepeat_TileMode, |
| + SkShader::kRepeat_TileMode, &localMatrix)); |
| + } |
| + |
| + // Skia does not have a "draw the tile only once" option. Clamp_TileMode |
| + // repeats the last line of the image after drawing one tile. To avoid |
| + // filling the space with arbitrary pixels, this workaround forces the |
| + // image to have a line of transparent pixels on the "repeated" edge(s), |
| + // thus causing extra space to be transparent filled. |
| + SkISize tileSize = SkISize::Make( |
| + m_tileImage->width() + (isRepeatX() ? 0 : 1), |
| + m_tileImage->height() + (isRepeatY() ? 0 : 1)); |
| + |
| + SkPictureRecorder recorder; |
| + SkCanvas* canvas = |
| + recorder.beginRecording(SkRect::MakeIWH(tileSize.width(), tileSize.height())); |
| + |
| + SkPaint paint; |
| + paint.setXfermodeMode(SkXfermode::kSrc_Mode); |
| + canvas->drawImage(m_tileImage.get(), 0, 0, &paint); |
| + RefPtr<SkPicture> tilePicture = adoptRef(recorder.endRecording()); |
| + RefPtr<SkImage> tileImage = |
| + adoptRef(SkImage::NewFromPicture(tilePicture.get(), tileSize, nullptr, nullptr)); |
|
Justin Novosad
2015/08/17 15:03:24
Why is it necessary to go through a picture here?
f(malita)
2015/08/17 15:09:30
It is a way to avoid record-time rasterization. N
|
| + |
| + if (!tileImage) |
| + return adoptRef(SkShader::CreateColorShader(SK_ColorTRANSPARENT)); |
| + |
| + return adoptRef(tileImage->newShader( |
| + isRepeatX() ? SkShader::kRepeat_TileMode : SkShader::kClamp_TileMode, |
| + isRepeatY() ? SkShader::kRepeat_TileMode : SkShader::kClamp_TileMode, |
| + &localMatrix)); |
| +} |
| + |
| +} // namespace |