Index: third_party/WebKit/Source/platform/graphics/ImagePattern.cpp |
diff --git a/third_party/WebKit/Source/platform/graphics/ImagePattern.cpp b/third_party/WebKit/Source/platform/graphics/ImagePattern.cpp |
index 584246d45217a8a56b6e7f97b623e6d22ae042d7..e9dc659a822a88ab3722c95c7026ac5fb7426e5c 100644 |
--- a/third_party/WebKit/Source/platform/graphics/ImagePattern.cpp |
+++ b/third_party/WebKit/Source/platform/graphics/ImagePattern.cpp |
@@ -5,9 +5,14 @@ |
#include "config.h" |
#include "platform/graphics/ImagePattern.h" |
+#include "platform/graphics/BitmapImage.h" |
+#include "platform/graphics/ColorSpaceFilter.h" |
+#include "platform/graphics/ColorSpaceProfile.h" |
+#include "platform/graphics/GraphicsScreen.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/SkColorFilter.h" |
#include "third_party/skia/include/core/SkImage.h" |
#include "third_party/skia/include/core/SkShader.h" |
#include "third_party/skia/include/core/SkSurface.h" |
@@ -22,6 +27,7 @@ PassRefPtr<ImagePattern> ImagePattern::create(PassRefPtr<Image> image, RepeatMod |
ImagePattern::ImagePattern(PassRefPtr<Image> image, RepeatMode repeatMode) |
: Pattern(repeatMode) |
, m_tileImage(image->imageForCurrentFrame()) |
+ , m_image(image) |
{ |
if (m_tileImage) { |
// TODO(fmalita): mechanism to extract the actual SkImageInfo from an SkImage? |
@@ -31,14 +37,34 @@ ImagePattern::ImagePattern(PassRefPtr<Image> image, RepeatMode repeatMode) |
} |
} |
+inline PassRefPtr<ColorSpaceProfile> sourceColorProfile(Image* image) |
+{ |
+ return image->isBitmapImage() ? toBitmapImage(image)->colorProfile() : nullptr; |
+} |
+ |
PassRefPtr<SkShader> ImagePattern::createShader() |
{ |
if (!m_tileImage) |
return adoptRef(SkShader::CreateColorShader(SK_ColorTRANSPARENT)); |
+ // Patterns of tagged images (those that have a color profile) are drawn to sRGB |
+ // on supported platforms. Note the only allowed caller is the <canvas> element. |
+ RefPtr<SkColorFilter> colorTransform; |
+ if (RefPtr<ColorSpaceProfile> source = sourceColorProfile(m_image.get())) { |
+ RefPtr<ColorSpaceProfile> screen = screenColorProfile(currentScreenId()); |
+ colorTransform = createColorSpaceFilter(source.get(), screen.get()); |
+ |
+ if (currentScreenId()) { |
+ fprintf(stderr, "ImagePattern::createShader has a non-zero page %ld\n", (long int)currentScreenId()); |
+ fflush(stderr); |
+ } |
+ |
+ RELEASE_ASSERT(!currentScreenId()); // Check caller is an HTML <canvas>. |
+ } |
+ |
SkMatrix localMatrix = affineTransformToSkMatrix(m_patternSpaceTransformation); |
- if (isRepeatXY()) { |
+ if (isRepeatXY() && !colorTransform.get()) { |
// Fast path: for repeatXY we just return a shader from the original image. |
return adoptRef(m_tileImage->newShader(SkShader::kRepeat_TileMode, |
SkShader::kRepeat_TileMode, &localMatrix)); |
@@ -59,18 +85,22 @@ PassRefPtr<SkShader> ImagePattern::createShader() |
// Create a transparent image 1 pixel wider and/or taller than the |
// original, then copy the orignal into it. |
// FIXME: Is there a better way to pad (not scale) an image in skia? |
+ // since the drawing is noticably pixelated compared to drawing the |
+ // same tiled image with Image:drawTiled (which is used to draw CSS |
+ // background-image repeat tiles). |
RefPtr<SkSurface> surface = adoptRef(SkSurface::NewRasterN32Premul( |
m_tileImage->width() + expandW, m_tileImage->height() + expandH)); |
if (!surface) |
return adoptRef(SkShader::CreateColorShader(SK_ColorTRANSPARENT)); |
- |
surface->getCanvas()->clear(SK_ColorTRANSPARENT); |
+ |
SkPaint paint; |
+ paint.setColorFilter(colorTransform.get()); |
paint.setXfermodeMode(SkXfermode::kSrc_Mode); |
surface->getCanvas()->drawImage(m_tileImage.get(), 0, 0, &paint); |
- RefPtr<SkImage> expandedImage = adoptRef(surface->newImageSnapshot()); |
- return adoptRef(expandedImage->newShader(tileModeX, tileModeY, &localMatrix)); |
+ RefPtr<SkImage> image = adoptRef(surface->newImageSnapshot()); |
+ return adoptRef(image->newShader(tileModeX, tileModeY, &localMatrix)); |
} |
bool ImagePattern::isTextureBacked() const |