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

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

Issue 358893002: Use newImageSnapshot() to get an image from a Canvas (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Again in StaticBitmapPattern Created 6 years, 2 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
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2006, 2007, 2008 Apple Computer, Inc. All rights reserved. 2 * Copyright (C) 2006, 2007, 2008 Apple Computer, Inc. All rights reserved.
3 * Copyright (C) 2008 Eric Seidel <eric@webkit.org> 3 * Copyright (C) 2008 Eric Seidel <eric@webkit.org>
4 * Copyright (C) 2013 Google, Inc. All rights reserved. 4 * Copyright (C) 2013 Google, Inc. All rights reserved.
5 * 5 *
6 * Redistribution and use in source and binary forms, with or without 6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions 7 * modification, are permitted provided that the following conditions
8 * are met: 8 * are met:
9 * 1. Redistributions of source code must retain the above copyright 9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer. 10 * notice, this list of conditions and the following disclaimer.
(...skipping 10 matching lines...) Expand all
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 22 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
23 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */ 26 */
27 27
28 #include "config.h" 28 #include "config.h"
29 #include "platform/graphics/Pattern.h" 29 #include "platform/graphics/Pattern.h"
30 30
31 #include "platform/graphics/BitmapPattern.h"
32 #include "platform/graphics/StaticBitmapPattern.h"
33 #include "third_party/skia/include/core/SkImage.h"
34 #include "third_party/skia/include/core/SkShader.h"
31 #include <v8.h> 35 #include <v8.h>
32 #include "SkCanvas.h"
33 #include "SkColorShader.h"
34 #include "platform/graphics/skia/SkiaUtils.h"
35 36
36 namespace blink { 37 namespace blink {
37 38
38 PassRefPtr<Pattern> Pattern::createBitmapPattern(PassRefPtr<Image> tileImage, Re peatMode repeatMode) 39 PassRefPtr<Pattern> Pattern::createBitmapPattern(PassRefPtr<Image> tileImage, Re peatMode repeatMode)
39 { 40 {
40 return adoptRef(new Pattern(tileImage, repeatMode)); 41 if (tileImage->image())
42 return StaticBitmapPattern::create(tileImage, repeatMode);
43
44 return BitmapPattern::create(tileImage, repeatMode);
41 } 45 }
42 46
43 Pattern::Pattern(PassRefPtr<Image> image, RepeatMode repeatMode) 47 Pattern::Pattern(RepeatMode repeatMode, int64_t externalMemoryAllocated)
44 : m_repeatMode(repeatMode) 48 : m_repeatMode(repeatMode)
45 , m_externalMemoryAllocated(0) 49 , m_externalMemoryAllocated(0)
46 { 50 {
47 if (image) { 51 adjustExternalMemoryAllocated(externalMemoryAllocated);
48 m_tileImage = image->nativeImageForCurrentFrame();
49 }
50 } 52 }
51 53
52 Pattern::~Pattern() 54 Pattern::~Pattern()
53 { 55 {
54 if (m_externalMemoryAllocated) 56 adjustExternalMemoryAllocated(-m_externalMemoryAllocated);
55 v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory(-m_exte rnalMemoryAllocated);
56 } 57 }
57 58
58 SkShader* Pattern::shader() 59 SkShader* Pattern::shader()
59 { 60 {
60 if (m_pattern) 61 if (!m_pattern) {
61 return m_pattern.get(); 62 m_pattern = createShader();
63 }
62 64
63 SkMatrix localMatrix = affineTransformToSkMatrix(m_patternSpaceTransformatio n);
64
65 // If we don't have a bitmap, return a transparent shader.
66 if (!m_tileImage) {
67 m_pattern = adoptRef(new SkColorShader(SK_ColorTRANSPARENT));
68 } else if (m_repeatMode == RepeatModeXY) {
69 m_pattern = adoptRef(SkShader::CreateBitmapShader(m_tileImage->bitmap(),
70 SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode, &localMatrix ));
71 } else {
72 // Skia does not have a "draw the tile only once" option. Clamp_TileMode
73 // repeats the last line of the image after drawing one tile. To avoid
74 // filling the space with arbitrary pixels, this workaround forces the
75 // image to have a line of transparent pixels on the "repeated" edge(s),
76 // thus causing extra space to be transparent filled.
77 SkShader::TileMode tileModeX = (m_repeatMode & RepeatModeX)
78 ? SkShader::kRepeat_TileMode
79 : SkShader::kClamp_TileMode;
80 SkShader::TileMode tileModeY = (m_repeatMode & RepeatModeY)
81 ? SkShader::kRepeat_TileMode
82 : SkShader::kClamp_TileMode;
83 int expandW = (m_repeatMode & RepeatModeX) ? 0 : 1;
84 int expandH = (m_repeatMode & RepeatModeY) ? 0 : 1;
85
86 // Create a transparent bitmap 1 pixel wider and/or taller than the
87 // original, then copy the orignal into it.
88 // FIXME: Is there a better way to pad (not scale) an image in skia?
89 SkImageInfo info = m_tileImage->bitmap().info();
90 info.fWidth += expandW;
91 info.fHeight += expandH;
92 // we explicitly require non-opaquness, since we are going to add a tran sparent strip.
93 info.fAlphaType = kPremul_SkAlphaType;
94
95 SkBitmap bm2;
96 bm2.allocPixels(info);
97 bm2.eraseARGB(0x00, 0x00, 0x00, 0x00);
98 SkCanvas canvas(bm2);
99 canvas.drawBitmap(m_tileImage->bitmap(), 0, 0);
100 bm2.setImmutable();
101 m_pattern = adoptRef(SkShader::CreateBitmapShader(bm2, tileModeX, tileMo deY, &localMatrix));
102
103 // Clamp to int, since that's what the adjust function takes.
104 m_externalMemoryAllocated = static_cast<int>(std::min(static_cast<size_t >(INT_MAX), bm2.getSafeSize()));
105 v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory(m_exter nalMemoryAllocated);
106 }
107 return m_pattern.get(); 65 return m_pattern.get();
108 } 66 }
109 67
110 void Pattern::setPatternSpaceTransform(const AffineTransform& patternSpaceTransf ormation) 68 void Pattern::setPatternSpaceTransform(const AffineTransform& patternSpaceTransf ormation)
111 { 69 {
112 if (patternSpaceTransformation == m_patternSpaceTransformation) 70 if (patternSpaceTransformation == m_patternSpaceTransformation)
113 return; 71 return;
114 72
115 m_patternSpaceTransformation = patternSpaceTransformation; 73 m_patternSpaceTransformation = patternSpaceTransformation;
116 m_pattern.clear(); 74 m_pattern.clear();
117 } 75 }
118 76
77 void Pattern::adjustExternalMemoryAllocated(int64_t delta)
78 {
79 delta = std::max(-m_externalMemoryAllocated, delta);
80
81 v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory(delta);
82
83 m_externalMemoryAllocated += delta;
84 }
85
119 } // namespace blink 86 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698