Index: Source/platform/graphics/BitmapPattern.cpp |
diff --git a/Source/platform/graphics/BitmapPattern.cpp b/Source/platform/graphics/BitmapPattern.cpp |
new file mode 100644 |
index 0000000000000000000000000000000000000000..502ab236a3a0c8472c86b6c207715771e803f723 |
--- /dev/null |
+++ b/Source/platform/graphics/BitmapPattern.cpp |
@@ -0,0 +1,97 @@ |
+// Copyright 2014 The Chromium Authors. All rights reserved. |
Justin Novosad
2014/07/29 14:56:51
Use the new abbreviated header (see http://www.chr
Rémi Piotaix
2014/07/29 17:18:22
Done.
|
+// |
+// The Chromium Authors can be found at |
+// http://src.chromium.org/svn/trunk/src/AUTHORS |
+// |
+// Redistribution and use in source and binary forms, with or without |
+// modification, are permitted provided that the following conditions are |
+// met: |
+// |
+// * Redistributions of source code must retain the above copyright |
+// notice, this list of conditions and the following disclaimer. |
+// * Redistributions in binary form must reproduce the above |
+// copyright notice, this list of conditions and the following disclaimer |
+// in the documentation and/or other materials provided with the |
+// distribution. |
+// * Neither the name of Google Inc. nor the names of its |
+// contributors may be used to endorse or promote products derived from |
+// this software without specific prior written permission. |
+// |
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
+// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
+// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
+// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
+// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
+// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
+// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
+// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
+ |
+#include "config.h" |
+#include "platform/graphics/BitmapPattern.h" |
+ |
+ |
+#include "SkCanvas.h" |
+#include "SkColorShader.h" |
+#include "platform/graphics/skia/SkiaUtils.h" |
+#include <v8.h> |
+ |
+namespace WebCore { |
Justin Novosad
2014/07/29 14:56:51
WebCore -> blink
Rémi Piotaix
2014/07/29 17:18:22
Done.
|
+ |
+BitmapPattern::BitmapPattern(PassRefPtr<Image> image, bool repeatX, bool repeatY) |
+: Pattern(repeatX, repeatY) |
+{ |
+ if (image) { |
+ m_tileImage = image->nativeImageForCurrentFrame(); |
+ } |
+} |
+ |
+PassRefPtr<SkShader> BitmapPattern::createShader() |
+{ |
+ SkMatrix localMatrix = affineTransformToSkMatrix(m_patternSpaceTransformation); |
+ |
+ RefPtr<SkShader> shader; |
+ |
+ // If we don't have a bitmap, return a transparent shader. |
+ if (!m_tileImage) { |
+ shader = adoptRef(new SkColorShader(SK_ColorTRANSPARENT)); |
Justin Novosad
2014/07/29 14:56:51
Why not just return nullptr to make calling code a
Rémi Piotaix
2014/07/29 17:18:22
Done.
|
+ } else if (m_repeatX && m_repeatY) { |
+ shader = adoptRef(SkShader::CreateBitmapShader(m_tileImage->bitmap(), SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode, &localMatrix)); |
+ } else { |
+ // Skia does not have a "draw the tile only once" option. Clamp_TileMode |
+ // repeats the last line of the image after drawing one tile. To avoid |
+ // filling the space with arbitrary pixels, this workaround forces the |
+ // image to have a line of transparent pixels on the "repeated" edge(s), |
+ // thus causing extra space to be transparent filled. |
+ SkShader::TileMode tileModeX = m_repeatX ? SkShader::kRepeat_TileMode : SkShader::kClamp_TileMode; |
+ SkShader::TileMode tileModeY = m_repeatY ? SkShader::kRepeat_TileMode : SkShader::kClamp_TileMode; |
+ int expandW = m_repeatX ? 0 : 1; |
+ int expandH = m_repeatY ? 0 : 1; |
+ |
+ // Create a transparent bitmap 1 pixel wider and/or taller than the |
+ // original, then copy the orignal into it. |
+ // FIXME: Is there a better way to pad (not scale) an image in skia? |
+ SkImageInfo info = m_tileImage->bitmap().info(); |
+ info.fWidth += expandW; |
+ info.fHeight += expandH; |
+ // we explicitly require non-opaquness, since we are going to add a transparent strip. |
+ info.fAlphaType = kPremul_SkAlphaType; |
+ |
+ SkBitmap bm2; |
+ bm2.allocPixels(info); |
+ bm2.eraseARGB(0x00, 0x00, 0x00, 0x00); |
Justin Novosad
2014/07/29 14:56:51
Would be more efficient to clear just the border a
Rémi Piotaix
2014/07/30 18:59:12
Done.
|
+ SkCanvas canvas(bm2); |
+ canvas.drawBitmap(m_tileImage->bitmap(), 0, 0); |
Justin Novosad
2014/07/29 14:56:51
You should set-up an SkPaint that uses the Src xfe
Rémi Piotaix
2014/07/30 18:59:12
Done.
|
+ bm2.setImmutable(); |
+ shader = adoptRef(SkShader::CreateBitmapShader(bm2, tileModeX, tileModeY, &localMatrix)); |
+ |
+ // Clamp to int, since that's what the adjust function takes. |
+ m_externalMemoryAllocated = static_cast<int> (std::min(static_cast<size_t> (INT_MAX), bm2.getSafeSize())); |
+ v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory(m_externalMemoryAllocated); |
Justin Novosad
2014/07/29 14:56:51
The adjustment needs to be a delta between the old
Rémi Piotaix
2014/07/30 18:59:12
Done.
|
+ } |
+ return shader; |
Justin Novosad
2014/07/29 14:56:51
return shader.release();
Rémi Piotaix
2014/07/29 17:18:22
Done.
|
+} |
+ |
+} // namespace |