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

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

Issue 18718002: Use RefPtr to manage shaders in Gradient and Pattern. (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Created 7 years, 5 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 22 matching lines...) Expand all
33 #include "SkColorShader.h" 33 #include "SkColorShader.h"
34 #include "SkShader.h" 34 #include "SkShader.h"
35 #include "core/platform/graphics/Image.h" 35 #include "core/platform/graphics/Image.h"
36 36
37 namespace WebCore { 37 namespace WebCore {
38 38
39 Pattern::Pattern(PassRefPtr<Image> image, bool repeatX, bool repeatY) 39 Pattern::Pattern(PassRefPtr<Image> image, bool repeatX, bool repeatY)
40 : m_tileImage(image) 40 : m_tileImage(image)
41 , m_repeatX(repeatX) 41 , m_repeatX(repeatX)
42 , m_repeatY(repeatY) 42 , m_repeatY(repeatY)
43 , m_pattern(0)
44 , m_externalMemoryAllocated(0) 43 , m_externalMemoryAllocated(0)
45 { 44 {
46 ASSERT(m_tileImage); 45 ASSERT(m_tileImage);
47 } 46 }
48 47
49 Pattern::~Pattern() 48 Pattern::~Pattern()
50 { 49 {
51 SkSafeUnref(m_pattern); 50 if (m_externalMemoryAllocated)
52 m_pattern = 0;
53 if (m_externalMemoryAllocated) {
54 v8::V8::AdjustAmountOfExternalAllocatedMemory(-m_externalMemoryAllocated ); 51 v8::V8::AdjustAmountOfExternalAllocatedMemory(-m_externalMemoryAllocated );
55 m_externalMemoryAllocated = 0;
56 }
57 } 52 }
58 53
59 SkShader* Pattern::shader() 54 SkShader* Pattern::shader()
60 { 55 {
61 if (m_pattern) 56 if (m_pattern)
62 return m_pattern; 57 return m_pattern.get();
63 58
64 RefPtr<NativeImageSkia> image = m_tileImage->nativeImageForCurrentFrame(); 59 RefPtr<NativeImageSkia> image = m_tileImage->nativeImageForCurrentFrame();
65 // If we don't have a bitmap, return a transparent shader. 60 // If we don't have a bitmap, return a transparent shader.
66 if (!image) 61 if (!image)
67 m_pattern = new SkColorShader(SkColorSetARGB(0, 0, 0, 0)); 62 m_pattern = adoptRef(new SkColorShader(SK_ColorTRANSPARENT));
68 else if (m_repeatX && m_repeatY) 63 else if (m_repeatX && m_repeatY)
69 m_pattern = SkShader::CreateBitmapShader(image->bitmap(), SkShader::kRep eat_TileMode, SkShader::kRepeat_TileMode); 64 m_pattern = adoptRef(SkShader::CreateBitmapShader(image->bitmap(), SkSha der::kRepeat_TileMode, SkShader::kRepeat_TileMode));
70 else { 65 else {
71 // Skia does not have a "draw the tile only once" option. Clamp_TileMode 66 // Skia does not have a "draw the tile only once" option. Clamp_TileMode
72 // repeats the last line of the image after drawing one tile. To avoid 67 // repeats the last line of the image after drawing one tile. To avoid
73 // filling the space with arbitrary pixels, this workaround forces the 68 // filling the space with arbitrary pixels, this workaround forces the
74 // image to have a line of transparent pixels on the "repeated" edge(s), 69 // image to have a line of transparent pixels on the "repeated" edge(s),
75 // thus causing extra space to be transparent filled. 70 // thus causing extra space to be transparent filled.
76 SkShader::TileMode tileModeX = m_repeatX ? SkShader::kRepeat_TileMode : SkShader::kClamp_TileMode; 71 SkShader::TileMode tileModeX = m_repeatX ? SkShader::kRepeat_TileMode : SkShader::kClamp_TileMode;
77 SkShader::TileMode tileModeY = m_repeatY ? SkShader::kRepeat_TileMode : SkShader::kClamp_TileMode; 72 SkShader::TileMode tileModeY = m_repeatY ? SkShader::kRepeat_TileMode : SkShader::kClamp_TileMode;
78 int expandW = m_repeatX ? 0 : 1; 73 int expandW = m_repeatX ? 0 : 1;
79 int expandH = m_repeatY ? 0 : 1; 74 int expandH = m_repeatY ? 0 : 1;
80 75
81 // Create a transparent bitmap 1 pixel wider and/or taller than the 76 // Create a transparent bitmap 1 pixel wider and/or taller than the
82 // original, then copy the orignal into it. 77 // original, then copy the orignal into it.
83 // FIXME: Is there a better way to pad (not scale) an image in skia? 78 // FIXME: Is there a better way to pad (not scale) an image in skia?
84 SkBitmap bm2; 79 SkBitmap bm2;
85 bm2.setConfig(image->bitmap().config(), image->bitmap().width() + expand W, image->bitmap().height() + expandH); 80 bm2.setConfig(image->bitmap().config(), image->bitmap().width() + expand W, image->bitmap().height() + expandH);
86 bm2.allocPixels(); 81 bm2.allocPixels();
87 bm2.eraseARGB(0x00, 0x00, 0x00, 0x00); 82 bm2.eraseARGB(0x00, 0x00, 0x00, 0x00);
88 SkCanvas canvas(bm2); 83 SkCanvas canvas(bm2);
89 canvas.drawBitmap(image->bitmap(), 0, 0); 84 canvas.drawBitmap(image->bitmap(), 0, 0);
90 bm2.setImmutable(); 85 bm2.setImmutable();
91 m_pattern = SkShader::CreateBitmapShader(bm2, tileModeX, tileModeY); 86 m_pattern = adoptRef(SkShader::CreateBitmapShader(bm2, tileModeX, tileMo deY));
92 87
93 // Clamp to int, since that's what the adjust function takes. 88 // Clamp to int, since that's what the adjust function takes.
94 m_externalMemoryAllocated = static_cast<int>(std::min(static_cast<size_t >(INT_MAX), bm2.getSafeSize())); 89 m_externalMemoryAllocated = static_cast<int>(std::min(static_cast<size_t >(INT_MAX), bm2.getSafeSize()));
95 v8::V8::AdjustAmountOfExternalAllocatedMemory(m_externalMemoryAllocated) ; 90 v8::V8::AdjustAmountOfExternalAllocatedMemory(m_externalMemoryAllocated) ;
96 } 91 }
97 m_pattern->setLocalMatrix(m_patternSpaceTransformation); 92 m_pattern->setLocalMatrix(m_patternSpaceTransformation);
98 return m_pattern; 93 return m_pattern.get();
99 } 94 }
100 95
101 void Pattern::setPatternSpaceTransform(const AffineTransform& patternSpaceTransf ormation) 96 void Pattern::setPatternSpaceTransform(const AffineTransform& patternSpaceTransf ormation)
102 { 97 {
103 m_patternSpaceTransformation = patternSpaceTransformation; 98 m_patternSpaceTransformation = patternSpaceTransformation;
104 if (m_pattern) 99 if (m_pattern)
105 m_pattern->setLocalMatrix(m_patternSpaceTransformation); 100 m_pattern->setLocalMatrix(m_patternSpaceTransformation);
106 } 101 }
107 102
108 } 103 }
OLDNEW
« Source/core/platform/graphics/Gradient.cpp ('K') | « Source/core/platform/graphics/Pattern.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698