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/Image.h" | 7 #include "platform/graphics/Image.h" |
| 8 #include "platform/graphics/paint/PaintShader.h" |
8 #include "platform/graphics/skia/SkiaUtils.h" | 9 #include "platform/graphics/skia/SkiaUtils.h" |
9 #include "third_party/skia/include/core/SkCanvas.h" | |
10 #include "third_party/skia/include/core/SkImage.h" | 10 #include "third_party/skia/include/core/SkImage.h" |
11 #include "third_party/skia/include/core/SkShader.h" | |
12 #include "third_party/skia/include/core/SkSurface.h" | 11 #include "third_party/skia/include/core/SkSurface.h" |
13 | 12 |
14 namespace blink { | 13 namespace blink { |
15 | 14 |
16 PassRefPtr<ImagePattern> ImagePattern::create(PassRefPtr<Image> image, | 15 PassRefPtr<ImagePattern> ImagePattern::create(PassRefPtr<Image> image, |
17 RepeatMode repeatMode) { | 16 RepeatMode repeatMode) { |
18 return adoptRef(new ImagePattern(std::move(image), repeatMode)); | 17 return adoptRef(new ImagePattern(std::move(image), repeatMode)); |
19 } | 18 } |
20 | 19 |
21 // TODO(ccameron): ImagePattern should not draw to a globally set color space. | 20 // TODO(ccameron): ImagePattern should not draw to a globally set color space. |
(...skipping 12 matching lines...) Expand all Loading... |
34 adjustExternalMemoryAllocated(info.getSafeSize(info.minRowBytes())); | 33 adjustExternalMemoryAllocated(info.getSafeSize(info.minRowBytes())); |
35 } | 34 } |
36 } | 35 } |
37 | 36 |
38 bool ImagePattern::isLocalMatrixChanged(const SkMatrix& localMatrix) const { | 37 bool ImagePattern::isLocalMatrixChanged(const SkMatrix& localMatrix) const { |
39 if (isRepeatXY()) | 38 if (isRepeatXY()) |
40 return Pattern::isLocalMatrixChanged(localMatrix); | 39 return Pattern::isLocalMatrixChanged(localMatrix); |
41 return localMatrix != m_previousLocalMatrix; | 40 return localMatrix != m_previousLocalMatrix; |
42 } | 41 } |
43 | 42 |
44 sk_sp<SkShader> ImagePattern::createShader(const SkMatrix& localMatrix) { | 43 sk_sp<PaintShader> ImagePattern::createShader(const SkMatrix& localMatrix) { |
45 if (!m_tileImage) | 44 if (!m_tileImage) |
46 return SkShader::MakeColorShader(SK_ColorTRANSPARENT); | 45 return WrapSkShader(SkShader::MakeColorShader(SK_ColorTRANSPARENT)); |
47 | 46 |
48 if (isRepeatXY()) { | 47 if (isRepeatXY()) { |
49 // Fast path: for repeatXY we just return a shader from the original image. | 48 // Fast path: for repeatXY we just return a shader from the original image. |
50 return m_tileImage->makeShader(SkShader::kRepeat_TileMode, | 49 return MakePaintShaderImage(m_tileImage, SkShader::kRepeat_TileMode, |
51 SkShader::kRepeat_TileMode, &localMatrix); | 50 SkShader::kRepeat_TileMode, &localMatrix); |
52 } | 51 } |
53 | 52 |
54 // Skia does not have a "draw the tile only once" option. Clamp_TileMode | 53 // Skia does not have a "draw the tile only once" option. Clamp_TileMode |
55 // repeats the last line of the image after drawing one tile. To avoid | 54 // repeats the last line of the image after drawing one tile. To avoid |
56 // filling the space with arbitrary pixels, this workaround forces the | 55 // filling the space with arbitrary pixels, this workaround forces the |
57 // image to have a line of transparent pixels on the "repeated" edge(s), | 56 // image to have a line of transparent pixels on the "repeated" edge(s), |
58 // thus causing extra space to be transparent filled. | 57 // thus causing extra space to be transparent filled. |
59 SkShader::TileMode tileModeX = | 58 SkShader::TileMode tileModeX = |
60 isRepeatX() ? SkShader::kRepeat_TileMode : SkShader::kClamp_TileMode; | 59 isRepeatX() ? SkShader::kRepeat_TileMode : SkShader::kClamp_TileMode; |
61 SkShader::TileMode tileModeY = | 60 SkShader::TileMode tileModeY = |
62 isRepeatY() ? SkShader::kRepeat_TileMode : SkShader::kClamp_TileMode; | 61 isRepeatY() ? SkShader::kRepeat_TileMode : SkShader::kClamp_TileMode; |
63 int borderPixelX = isRepeatX() ? 0 : 1; | 62 int borderPixelX = isRepeatX() ? 0 : 1; |
64 int borderPixelY = isRepeatY() ? 0 : 1; | 63 int borderPixelY = isRepeatY() ? 0 : 1; |
65 | 64 |
66 // Create a transparent image 2 pixels wider and/or taller than the | 65 // Create a transparent image 2 pixels wider and/or taller than the |
67 // original, then copy the orignal into the middle of it. | 66 // original, then copy the orignal into the middle of it. |
68 // FIXME: Is there a better way to pad (not scale) an image in skia? | 67 // FIXME: Is there a better way to pad (not scale) an image in skia? |
69 sk_sp<SkSurface> surface = | 68 sk_sp<SkSurface> surface = |
70 SkSurface::MakeRasterN32Premul(m_tileImage->width() + 2 * borderPixelX, | 69 SkSurface::MakeRasterN32Premul(m_tileImage->width() + 2 * borderPixelX, |
71 m_tileImage->height() + 2 * borderPixelY); | 70 m_tileImage->height() + 2 * borderPixelY); |
72 if (!surface) | 71 if (!surface) |
73 return SkShader::MakeColorShader(SK_ColorTRANSPARENT); | 72 return WrapSkShader(SkShader::MakeColorShader(SK_ColorTRANSPARENT)); |
74 | 73 |
75 SkPaint paint; | 74 SkPaint paint; |
76 paint.setBlendMode(SkBlendMode::kSrc); | 75 paint.setBlendMode(SkBlendMode::kSrc); |
77 surface->getCanvas()->drawImage(m_tileImage, borderPixelX, borderPixelY, | 76 surface->getCanvas()->drawImage(m_tileImage, borderPixelX, borderPixelY, |
78 &paint); | 77 &paint); |
79 | 78 |
80 m_previousLocalMatrix = localMatrix; | 79 m_previousLocalMatrix = localMatrix; |
81 SkMatrix adjustedMatrix(localMatrix); | 80 SkMatrix adjustedMatrix(localMatrix); |
82 adjustedMatrix.postTranslate(-borderPixelX, -borderPixelY); | 81 adjustedMatrix.postTranslate(-borderPixelX, -borderPixelY); |
83 | 82 |
84 return surface->makeImageSnapshot()->makeShader(tileModeX, tileModeY, | 83 return MakePaintShaderImage(surface->makeImageSnapshot(), tileModeX, |
85 &adjustedMatrix); | 84 tileModeY, &adjustedMatrix); |
86 } | 85 } |
87 | 86 |
88 bool ImagePattern::isTextureBacked() const { | 87 bool ImagePattern::isTextureBacked() const { |
89 return m_tileImage && m_tileImage->isTextureBacked(); | 88 return m_tileImage && m_tileImage->isTextureBacked(); |
90 } | 89 } |
91 | 90 |
92 } // namespace blink | 91 } // namespace blink |
OLD | NEW |