OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "config.h" | |
6 #include "platform/graphics/ImagePattern.h" | |
7 | |
8 #include "platform/graphics/Image.h" | |
9 #include "platform/graphics/skia/SkiaUtils.h" | |
10 #include "third_party/skia/include/core/SkCanvas.h" | |
11 #include "third_party/skia/include/core/SkImage.h" | |
12 #include "third_party/skia/include/core/SkPicture.h" | |
13 #include "third_party/skia/include/core/SkPictureRecorder.h" | |
14 #include "third_party/skia/include/core/SkShader.h" | |
15 | |
16 namespace blink { | |
17 | |
18 PassRefPtr<ImagePattern> ImagePattern::create(PassRefPtr<Image> image, RepeatMod e repeatMode) | |
19 { | |
20 return adoptRef(new ImagePattern(image, repeatMode)); | |
21 } | |
22 | |
23 ImagePattern::ImagePattern(PassRefPtr<Image> image, RepeatMode repeatMode) | |
24 : Pattern(repeatMode) | |
25 , m_tileImage(image->imageForCurrentFrame()) | |
26 { | |
27 if (m_tileImage) { | |
28 // TODO(fmalita): mechanism to extract the actual SkImageInfo from an Sk Image? | |
29 const SkImageInfo info = | |
30 SkImageInfo::MakeN32Premul(m_tileImage->width(), m_tileImage->height ()); | |
31 adjustExternalMemoryAllocated(info.getSafeSize(info.minRowBytes())); | |
32 } | |
33 } | |
34 | |
35 PassRefPtr<SkShader> ImagePattern::createShader() | |
36 { | |
37 if (!m_tileImage) | |
38 return adoptRef(SkShader::CreateColorShader(SK_ColorTRANSPARENT)); | |
39 | |
40 SkMatrix localMatrix = affineTransformToSkMatrix(m_patternSpaceTransformatio n); | |
41 | |
42 if (isRepeatXY()) { | |
43 return adoptRef(m_tileImage->newShader(SkShader::kRepeat_TileMode, | |
44 SkShader::kRepeat_TileMode, &localMatrix)); | |
45 } | |
46 | |
47 // Skia does not have a "draw the tile only once" option. Clamp_TileMode | |
48 // repeats the last line of the image after drawing one tile. To avoid | |
49 // filling the space with arbitrary pixels, this workaround forces the | |
50 // image to have a line of transparent pixels on the "repeated" edge(s), | |
51 // thus causing extra space to be transparent filled. | |
52 SkISize tileSize = SkISize::Make( | |
53 m_tileImage->width() + (isRepeatX() ? 0 : 1), | |
54 m_tileImage->height() + (isRepeatY() ? 0 : 1)); | |
55 | |
56 SkPictureRecorder recorder; | |
57 SkCanvas* canvas = | |
58 recorder.beginRecording(SkRect::MakeIWH(tileSize.width(), tileSize.heigh t())); | |
59 | |
60 SkPaint paint; | |
61 paint.setXfermodeMode(SkXfermode::kSrc_Mode); | |
62 canvas->drawImage(m_tileImage.get(), 0, 0, &paint); | |
63 RefPtr<SkPicture> tilePicture = adoptRef(recorder.endRecording()); | |
64 RefPtr<SkImage> tileImage = | |
65 adoptRef(SkImage::NewFromPicture(tilePicture.get(), tileSize, nullptr, n ullptr)); | |
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
| |
66 | |
67 if (!tileImage) | |
68 return adoptRef(SkShader::CreateColorShader(SK_ColorTRANSPARENT)); | |
69 | |
70 return adoptRef(tileImage->newShader( | |
71 isRepeatX() ? SkShader::kRepeat_TileMode : SkShader::kClamp_TileMode, | |
72 isRepeatY() ? SkShader::kRepeat_TileMode : SkShader::kClamp_TileMode, | |
73 &localMatrix)); | |
74 } | |
75 | |
76 } // namespace | |
OLD | NEW |