Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(917)

Side by Side Diff: Source/platform/graphics/ImagePattern.cpp

Issue 1290983003: SkImage-only bitmap patterns (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: drop non-repeat optimization Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « Source/platform/graphics/ImagePattern.h ('k') | Source/platform/graphics/Pattern.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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/SkShader.h"
13 #include "third_party/skia/include/core/SkSurface.h"
14
15 namespace blink {
16
17 PassRefPtr<ImagePattern> ImagePattern::create(PassRefPtr<Image> image, RepeatMod e repeatMode)
18 {
19 return adoptRef(new ImagePattern(image, repeatMode));
20 }
21
22 ImagePattern::ImagePattern(PassRefPtr<Image> image, RepeatMode repeatMode)
23 : Pattern(repeatMode)
24 , m_tileImage(image->imageForCurrentFrame())
25 {
26 if (m_tileImage) {
27 // TODO(fmalita): mechanism to extract the actual SkImageInfo from an Sk Image?
28 const SkImageInfo info =
29 SkImageInfo::MakeN32Premul(m_tileImage->width(), m_tileImage->height ());
30 adjustExternalMemoryAllocated(info.getSafeSize(info.minRowBytes()));
31 }
32 }
33
34 PassRefPtr<SkShader> ImagePattern::createShader()
35 {
36 if (!m_tileImage)
37 return adoptRef(SkShader::CreateColorShader(SK_ColorTRANSPARENT));
38
39 SkMatrix localMatrix = affineTransformToSkMatrix(m_patternSpaceTransformatio n);
40
41 if (isRepeatXY()) {
42 // Fast path: for repeatXY we just return a shader from the original ima ge.
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
f(malita) 2015/08/17 18:51:30 This is the legacy fallback code from BitmapPatter
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 SkShader::TileMode tileModeX = isRepeatX()
53 ? SkShader::kRepeat_TileMode : SkShader::kClamp_TileMode;
54 SkShader::TileMode tileModeY = isRepeatY()
55 ? SkShader::kRepeat_TileMode : SkShader::kClamp_TileMode;
56 int expandW = isRepeatX() ? 0 : 1;
57 int expandH = isRepeatY() ? 0 : 1;
58
59 // Create a transparent image 1 pixel wider and/or taller than the
60 // original, then copy the orignal into it.
61 // FIXME: Is there a better way to pad (not scale) an image in skia?
62 RefPtr<SkSurface> surface = adoptRef(SkSurface::NewRasterN32Premul(
63 m_tileImage->width() + expandW, m_tileImage->height() + expandH));
64 if (!surface)
65 return adoptRef(SkShader::CreateColorShader(SK_ColorTRANSPARENT));
66
67 surface->getCanvas()->clear(SK_ColorTRANSPARENT);
68 SkPaint paint;
69 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
70 surface->getCanvas()->drawImage(m_tileImage.get(), 0, 0, &paint);
71 RefPtr<SkImage> expandedImage = adoptRef(surface->newImageSnapshot());
72
73 return adoptRef(expandedImage->newShader(tileModeX, tileModeY, &localMatrix) );
74 }
75
76 } // namespace
OLDNEW
« no previous file with comments | « Source/platform/graphics/ImagePattern.h ('k') | Source/platform/graphics/Pattern.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698