OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "platform/graphics/ImagePattern.h" | 5 #include "platform/graphics/ImagePattern.h" |
6 | 6 |
| 7 #include "platform/graphics/BitmapImage.h" |
| 8 #include "platform/graphics/ColorSpaceFilter.h" |
| 9 #include "platform/graphics/ColorSpaceProfile.h" |
| 10 #include "platform/graphics/GraphicsScreen.h" |
7 #include "platform/graphics/Image.h" | 11 #include "platform/graphics/Image.h" |
8 #include "platform/graphics/skia/SkiaUtils.h" | 12 #include "platform/graphics/skia/SkiaUtils.h" |
9 #include "third_party/skia/include/core/SkCanvas.h" | 13 #include "third_party/skia/include/core/SkCanvas.h" |
| 14 #include "third_party/skia/include/core/SkColorFilter.h" |
10 #include "third_party/skia/include/core/SkImage.h" | 15 #include "third_party/skia/include/core/SkImage.h" |
11 #include "third_party/skia/include/core/SkShader.h" | 16 #include "third_party/skia/include/core/SkShader.h" |
12 #include "third_party/skia/include/core/SkSurface.h" | 17 #include "third_party/skia/include/core/SkSurface.h" |
13 | 18 |
14 namespace blink { | 19 namespace blink { |
15 | 20 |
16 PassRefPtr<ImagePattern> ImagePattern::create(PassRefPtr<Image> image, RepeatMod
e repeatMode) | 21 PassRefPtr<ImagePattern> ImagePattern::create(PassRefPtr<Image> image, RepeatMod
e repeatMode) |
17 { | 22 { |
18 return adoptRef(new ImagePattern(image, repeatMode)); | 23 return adoptRef(new ImagePattern(image, repeatMode)); |
19 } | 24 } |
20 | 25 |
21 ImagePattern::ImagePattern(PassRefPtr<Image> image, RepeatMode repeatMode) | 26 ImagePattern::ImagePattern(PassRefPtr<Image> image, RepeatMode repeatMode) |
22 : Pattern(repeatMode) | 27 : Pattern(repeatMode) |
23 , m_tileImage(image->imageForCurrentFrame()) | 28 , m_tileImage(image->imageForCurrentFrame()) |
| 29 , m_image(image) |
24 { | 30 { |
25 if (m_tileImage) { | 31 if (m_tileImage) { |
26 // TODO(fmalita): mechanism to extract the actual SkImageInfo from an Sk
Image? | 32 // TODO(fmalita): mechanism to extract the actual SkImageInfo from an Sk
Image? |
27 const SkImageInfo info = | 33 const SkImageInfo info = |
28 SkImageInfo::MakeN32Premul(m_tileImage->width(), m_tileImage->height
()); | 34 SkImageInfo::MakeN32Premul(m_tileImage->width(), m_tileImage->height
()); |
29 adjustExternalMemoryAllocated(info.getSafeSize(info.minRowBytes())); | 35 adjustExternalMemoryAllocated(info.getSafeSize(info.minRowBytes())); |
30 } | 36 } |
31 } | 37 } |
32 | 38 |
| 39 inline PassRefPtr<ColorSpaceProfile> sourceColorProfile(Image* image) |
| 40 { |
| 41 return image->isBitmapImage() ? toBitmapImage(image)->colorProfile() : nullp
tr; |
| 42 } |
| 43 |
33 PassRefPtr<SkShader> ImagePattern::createShader() | 44 PassRefPtr<SkShader> ImagePattern::createShader() |
34 { | 45 { |
35 if (!m_tileImage) | 46 if (!m_tileImage) |
36 return adoptRef(SkShader::CreateColorShader(SK_ColorTRANSPARENT)); | 47 return adoptRef(SkShader::CreateColorShader(SK_ColorTRANSPARENT)); |
37 | 48 |
| 49 // Patterns of tagged images (those that have a color profile) are drawn to
sRGB |
| 50 // on supported platforms. Note the only allowed caller is the <canvas> elem
ent. |
| 51 RefPtr<SkColorFilter> colorTransform; |
| 52 if (RefPtr<ColorSpaceProfile> source = sourceColorProfile(m_image.get())) { |
| 53 RefPtr<ColorSpaceProfile> screen = screenColorProfile(currentScreenId())
; |
| 54 colorTransform = createColorSpaceFilter(source.get(), screen.get()); |
| 55 |
| 56 if (currentScreenId()) { |
| 57 fprintf(stderr, "ImagePattern::createShader has a non-zero page %ld\
n", (long int)currentScreenId()); |
| 58 fflush(stderr); |
| 59 } |
| 60 |
| 61 RELEASE_ASSERT(!currentScreenId()); // Check caller is an HTML <canvas>. |
| 62 } |
| 63 |
38 SkMatrix localMatrix = affineTransformToSkMatrix(m_patternSpaceTransformatio
n); | 64 SkMatrix localMatrix = affineTransformToSkMatrix(m_patternSpaceTransformatio
n); |
39 | 65 |
40 if (isRepeatXY()) { | 66 if (isRepeatXY() && !colorTransform.get()) { |
41 // Fast path: for repeatXY we just return a shader from the original ima
ge. | 67 // Fast path: for repeatXY we just return a shader from the original ima
ge. |
42 return adoptRef(m_tileImage->newShader(SkShader::kRepeat_TileMode, | 68 return adoptRef(m_tileImage->newShader(SkShader::kRepeat_TileMode, |
43 SkShader::kRepeat_TileMode, &localMatrix)); | 69 SkShader::kRepeat_TileMode, &localMatrix)); |
44 } | 70 } |
45 | 71 |
46 // Skia does not have a "draw the tile only once" option. Clamp_TileMode | 72 // Skia does not have a "draw the tile only once" option. Clamp_TileMode |
47 // repeats the last line of the image after drawing one tile. To avoid | 73 // repeats the last line of the image after drawing one tile. To avoid |
48 // filling the space with arbitrary pixels, this workaround forces the | 74 // filling the space with arbitrary pixels, this workaround forces the |
49 // image to have a line of transparent pixels on the "repeated" edge(s), | 75 // image to have a line of transparent pixels on the "repeated" edge(s), |
50 // thus causing extra space to be transparent filled. | 76 // thus causing extra space to be transparent filled. |
51 SkShader::TileMode tileModeX = isRepeatX() | 77 SkShader::TileMode tileModeX = isRepeatX() |
52 ? SkShader::kRepeat_TileMode : SkShader::kClamp_TileMode; | 78 ? SkShader::kRepeat_TileMode : SkShader::kClamp_TileMode; |
53 SkShader::TileMode tileModeY = isRepeatY() | 79 SkShader::TileMode tileModeY = isRepeatY() |
54 ? SkShader::kRepeat_TileMode : SkShader::kClamp_TileMode; | 80 ? SkShader::kRepeat_TileMode : SkShader::kClamp_TileMode; |
55 int expandW = isRepeatX() ? 0 : 1; | 81 int expandW = isRepeatX() ? 0 : 1; |
56 int expandH = isRepeatY() ? 0 : 1; | 82 int expandH = isRepeatY() ? 0 : 1; |
57 | 83 |
58 // Create a transparent image 1 pixel wider and/or taller than the | 84 // Create a transparent image 1 pixel wider and/or taller than the |
59 // original, then copy the orignal into it. | 85 // original, then copy the orignal into it. |
60 // FIXME: Is there a better way to pad (not scale) an image in skia? | 86 // FIXME: Is there a better way to pad (not scale) an image in skia? |
| 87 // since the drawing is noticably pixelated compared to drawing the |
| 88 // same tiled image with Image:drawTiled (which is used to draw CSS |
| 89 // background-image repeat tiles). |
61 RefPtr<SkSurface> surface = adoptRef(SkSurface::NewRasterN32Premul( | 90 RefPtr<SkSurface> surface = adoptRef(SkSurface::NewRasterN32Premul( |
62 m_tileImage->width() + expandW, m_tileImage->height() + expandH)); | 91 m_tileImage->width() + expandW, m_tileImage->height() + expandH)); |
63 if (!surface) | 92 if (!surface) |
64 return adoptRef(SkShader::CreateColorShader(SK_ColorTRANSPARENT)); | 93 return adoptRef(SkShader::CreateColorShader(SK_ColorTRANSPARENT)); |
| 94 surface->getCanvas()->clear(SK_ColorTRANSPARENT); |
65 | 95 |
66 surface->getCanvas()->clear(SK_ColorTRANSPARENT); | |
67 SkPaint paint; | 96 SkPaint paint; |
| 97 paint.setColorFilter(colorTransform.get()); |
68 paint.setXfermodeMode(SkXfermode::kSrc_Mode); | 98 paint.setXfermodeMode(SkXfermode::kSrc_Mode); |
69 surface->getCanvas()->drawImage(m_tileImage.get(), 0, 0, &paint); | 99 surface->getCanvas()->drawImage(m_tileImage.get(), 0, 0, &paint); |
70 RefPtr<SkImage> expandedImage = adoptRef(surface->newImageSnapshot()); | |
71 | 100 |
72 return adoptRef(expandedImage->newShader(tileModeX, tileModeY, &localMatrix)
); | 101 RefPtr<SkImage> image = adoptRef(surface->newImageSnapshot()); |
| 102 return adoptRef(image->newShader(tileModeX, tileModeY, &localMatrix)); |
73 } | 103 } |
74 | 104 |
75 bool ImagePattern::isTextureBacked() const | 105 bool ImagePattern::isTextureBacked() const |
76 { | 106 { |
77 return m_tileImage && m_tileImage->isTextureBacked(); | 107 return m_tileImage && m_tileImage->isTextureBacked(); |
78 } | 108 } |
79 | 109 |
80 } // namespace | 110 } // namespace |
OLD | NEW |