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/GraphicsScreen.h" |
7 #include "platform/graphics/Image.h" | 9 #include "platform/graphics/Image.h" |
8 #include "platform/graphics/skia/SkiaUtils.h" | 10 #include "platform/graphics/skia/SkiaUtils.h" |
9 #include "third_party/skia/include/core/SkCanvas.h" | 11 #include "third_party/skia/include/core/SkCanvas.h" |
10 #include "third_party/skia/include/core/SkImage.h" | 12 #include "third_party/skia/include/core/SkImage.h" |
11 #include "third_party/skia/include/core/SkShader.h" | 13 #include "third_party/skia/include/core/SkShader.h" |
12 #include "third_party/skia/include/core/SkSurface.h" | 14 #include "third_party/skia/include/core/SkSurface.h" |
13 | 15 |
14 namespace blink { | 16 namespace blink { |
15 | 17 |
16 PassRefPtr<ImagePattern> ImagePattern::create(PassRefPtr<Image> image, RepeatMod
e repeatMode) | 18 PassRefPtr<ImagePattern> ImagePattern::create(PassRefPtr<Image> image, RepeatMod
e repeatMode) |
17 { | 19 { |
18 return adoptRef(new ImagePattern(image, repeatMode)); | 20 return adoptRef(new ImagePattern(image, repeatMode)); |
19 } | 21 } |
20 | 22 |
21 ImagePattern::ImagePattern(PassRefPtr<Image> image, RepeatMode repeatMode) | 23 ImagePattern::ImagePattern(PassRefPtr<Image> image, RepeatMode repeatMode) |
22 : Pattern(repeatMode) | 24 : Pattern(repeatMode) |
23 , m_tileImage(image->imageForCurrentFrame()) | 25 , m_tileImage(image->imageForCurrentFrame()) |
24 { | 26 { |
| 27 if (!m_tileImage) |
| 28 return; |
| 29 |
| 30 // Patterns of tagged images (those that have a color profile) are drawn to
sRGB |
| 31 // on supported platforms. The only allowed caller is the <canvas> element.
Grab |
| 32 // the image assuming <canvas>: createShader() checks for it again on draw. |
| 33 |
| 34 if (image->isBitmapImage()) { |
| 35 WillPaintForDevice device(ScreenDevice::sRGBCanvas); |
| 36 m_tileImage = toBitmapImage(image.get())->pictureForCurrentFrame(); |
| 37 } |
| 38 |
25 if (m_tileImage) { | 39 if (m_tileImage) { |
26 // TODO(fmalita): mechanism to extract the actual SkImageInfo from an Sk
Image? | 40 // TODO(fmalita): mechanism to extract the actual SkImageInfo from an Sk
Image? |
27 const SkImageInfo info = | 41 const SkImageInfo info = |
28 SkImageInfo::MakeN32Premul(m_tileImage->width(), m_tileImage->height
()); | 42 SkImageInfo::MakeN32Premul(m_tileImage->width(), m_tileImage->height
()); |
29 adjustExternalMemoryAllocated(info.getSafeSize(info.minRowBytes())); | 43 adjustExternalMemoryAllocated(info.getSafeSize(info.minRowBytes())); |
30 } | 44 } |
31 } | 45 } |
32 | 46 |
33 PassRefPtr<SkShader> ImagePattern::createShader() | 47 PassRefPtr<SkShader> ImagePattern::createShader() |
34 { | 48 { |
| 49 RELEASE_ASSERT(currentScreenId() == ScreenDevice::sRGBCanvas); |
| 50 |
35 if (!m_tileImage) | 51 if (!m_tileImage) |
36 return adoptRef(SkShader::CreateColorShader(SK_ColorTRANSPARENT)); | 52 return adoptRef(SkShader::CreateColorShader(SK_ColorTRANSPARENT)); |
37 | 53 |
38 SkMatrix localMatrix = affineTransformToSkMatrix(m_patternSpaceTransformatio
n); | 54 SkMatrix localMatrix = affineTransformToSkMatrix(m_patternSpaceTransformatio
n); |
39 | 55 |
40 if (isRepeatXY()) { | 56 if (isRepeatXY()) { |
41 // Fast path: for repeatXY we just return a shader from the original ima
ge. | 57 // Fast path: for repeatXY we just return a shader from the original ima
ge. |
42 return adoptRef(m_tileImage->newShader(SkShader::kRepeat_TileMode, | 58 return adoptRef(m_tileImage->newShader(SkShader::kRepeat_TileMode, |
43 SkShader::kRepeat_TileMode, &localMatrix)); | 59 SkShader::kRepeat_TileMode, &localMatrix)); |
44 } | 60 } |
45 | 61 |
46 // Skia does not have a "draw the tile only once" option. Clamp_TileMode | 62 // 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 | 63 // repeats the last line of the image after drawing one tile. To avoid |
48 // filling the space with arbitrary pixels, this workaround forces the | 64 // filling the space with arbitrary pixels, this workaround forces the |
49 // image to have a line of transparent pixels on the "repeated" edge(s), | 65 // image to have a line of transparent pixels on the "repeated" edge(s), |
50 // thus causing extra space to be transparent filled. | 66 // thus causing extra space to be transparent filled. |
51 SkShader::TileMode tileModeX = isRepeatX() | 67 SkShader::TileMode tileModeX = isRepeatX() |
52 ? SkShader::kRepeat_TileMode : SkShader::kClamp_TileMode; | 68 ? SkShader::kRepeat_TileMode : SkShader::kClamp_TileMode; |
53 SkShader::TileMode tileModeY = isRepeatY() | 69 SkShader::TileMode tileModeY = isRepeatY() |
54 ? SkShader::kRepeat_TileMode : SkShader::kClamp_TileMode; | 70 ? SkShader::kRepeat_TileMode : SkShader::kClamp_TileMode; |
55 int expandW = isRepeatX() ? 0 : 1; | 71 int expandW = isRepeatX() ? 0 : 1; |
56 int expandH = isRepeatY() ? 0 : 1; | 72 int expandH = isRepeatY() ? 0 : 1; |
57 | 73 |
58 // Create a transparent image 1 pixel wider and/or taller than the | 74 // Create a transparent image 1 pixel wider and/or taller than the |
59 // original, then copy the orignal into it. | 75 // original, then copy the orignal into it. |
60 // FIXME: Is there a better way to pad (not scale) an image in skia? | 76 // FIXME: Is there a better way to pad (not scale) an image in skia? |
| 77 // since the drawing is noticably pixelated compared to drawing the |
| 78 // same tiled image with Image:drawTiled (which is used to draw CSS |
| 79 // background-image repeat tiles). |
61 RefPtr<SkSurface> surface = adoptRef(SkSurface::NewRasterN32Premul( | 80 RefPtr<SkSurface> surface = adoptRef(SkSurface::NewRasterN32Premul( |
62 m_tileImage->width() + expandW, m_tileImage->height() + expandH)); | 81 m_tileImage->width() + expandW, m_tileImage->height() + expandH)); |
63 if (!surface) | 82 if (!surface) |
64 return adoptRef(SkShader::CreateColorShader(SK_ColorTRANSPARENT)); | 83 return adoptRef(SkShader::CreateColorShader(SK_ColorTRANSPARENT)); |
| 84 surface->getCanvas()->clear(SK_ColorTRANSPARENT); |
65 | 85 |
66 surface->getCanvas()->clear(SK_ColorTRANSPARENT); | |
67 SkPaint paint; | 86 SkPaint paint; |
68 paint.setXfermodeMode(SkXfermode::kSrc_Mode); | 87 paint.setXfermodeMode(SkXfermode::kSrc_Mode); |
69 surface->getCanvas()->drawImage(m_tileImage.get(), 0, 0, &paint); | 88 surface->getCanvas()->drawImage(m_tileImage.get(), 0, 0, &paint); |
70 RefPtr<SkImage> expandedImage = adoptRef(surface->newImageSnapshot()); | |
71 | 89 |
72 return adoptRef(expandedImage->newShader(tileModeX, tileModeY, &localMatrix)
); | 90 RefPtr<SkImage> image = adoptRef(surface->newImageSnapshot()); |
| 91 return adoptRef(image->newShader(tileModeX, tileModeY, &localMatrix)); |
73 } | 92 } |
74 | 93 |
75 bool ImagePattern::isTextureBacked() const | 94 bool ImagePattern::isTextureBacked() const |
76 { | 95 { |
77 return m_tileImage && m_tileImage->isTextureBacked(); | 96 return m_tileImage && m_tileImage->isTextureBacked(); |
78 } | 97 } |
79 | 98 |
80 } // namespace | 99 } // namespace |
OLD | NEW |