Chromium Code Reviews| Index: Source/platform/graphics/StaticBitmapPattern.cpp |
| diff --git a/Source/platform/graphics/StaticBitmapPattern.cpp b/Source/platform/graphics/StaticBitmapPattern.cpp |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..8bb1941f30e860db462c2433fc99f6eca29d005d |
| --- /dev/null |
| +++ b/Source/platform/graphics/StaticBitmapPattern.cpp |
| @@ -0,0 +1,98 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
|
Justin Novosad
2014/07/29 14:56:53
Short header
Rémi Piotaix
2014/07/29 17:18:23
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/StaticBitmapPattern.h" |
| + |
|
Justin Novosad
2014/07/29 14:56:53
No need for 2 blank lines.
Rémi Piotaix
2014/07/29 17:18:23
Done.
|
| + |
| +#include "SkCanvas.h" |
|
Justin Novosad
2014/07/29 14:56:53
we now prefer third_party/skia/include/core/...
Rémi Piotaix
2014/07/29 17:18:23
Done.
|
| +#include "SkColorShader.h" |
| +#include "platform/graphics/skia/SkiaUtils.h" |
| +#include <v8.h> |
| + |
| +namespace WebCore { |
| + |
| +StaticBitmapPattern::StaticBitmapPattern(PassRefPtr<SkImage> image, bool repeatX, bool repeatY) |
| +: Pattern(repeatX, repeatY) |
| +{ |
| + if (image) { |
|
Justin Novosad
2014/07/29 14:56:53
Is this conditional necessary?
Rémi Piotaix
2014/07/29 17:18:23
Done.
|
| + m_tileImage = image; |
| + } |
| +} |
| + |
| +PassRefPtr<SkShader> StaticBitmapPattern::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:53
why not return null?
Rémi Piotaix
2014/07/29 17:18:23
Done.
Rémi Piotaix
2014/07/30 18:59:13
Reverted, was breaking some tests (and also breaks
|
| + } else if (m_repeatX && m_repeatY) { |
| + shader = adoptRef(m_tileImage->newShader(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->info(); |
| + info.fWidth = m_tileImage->width() + expandW; |
| + info.fHeight = m_tileImage->height() + 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:53
could clear just borders
Rémi Piotaix
2014/07/30 18:59:12
Done.
|
| + SkCanvas canvas(bm2); |
| + m_tileImage->draw(&canvas, SkIntToScalar(0), SkIntToScalar(0), 0); |
|
Justin Novosad
2014/07/29 14:56:53
use Src xfermode
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:53
delta?
Rémi Piotaix
2014/07/30 18:59:12
Done.
|
| + } |
| + return shader; |
| +} |
| + |
| +} // namespace |