OLD | NEW |
(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/BitmapPatternBase.h" |
| 7 |
| 8 #include "platform/graphics/skia/SkiaUtils.h" |
| 9 #include "third_party/skia/include/core/SkCanvas.h" |
| 10 #include "third_party/skia/include/core/SkShader.h" |
| 11 |
| 12 namespace blink { |
| 13 |
| 14 BitmapPatternBase::BitmapPatternBase(RepeatMode repeatMode, int64_t externalMemo
ryAllocated) : Pattern(repeatMode, externalMemoryAllocated) { } |
| 15 |
| 16 BitmapPatternBase::~BitmapPatternBase() { } |
| 17 |
| 18 PassRefPtr<SkShader> BitmapPatternBase::createShader() |
| 19 { |
| 20 // Skia does not have a "draw the tile only once" option. Clamp_TileMode |
| 21 // repeats the last line of the image after drawing one tile. To avoid |
| 22 // filling the space with arbitrary pixels, this workaround forces the |
| 23 // image to have a line of transparent pixels on the "repeated" edge(s), |
| 24 // thus causing extra space to be transparent filled. |
| 25 SkShader::TileMode tileModeX = isRepeatX() ? SkShader::kRepeat_TileMode : Sk
Shader::kClamp_TileMode; |
| 26 SkShader::TileMode tileModeY = isRepeatY() ? SkShader::kRepeat_TileMode : Sk
Shader::kClamp_TileMode; |
| 27 int expandW = isRepeatX() ? 0 : 1; |
| 28 int expandH = isRepeatY() ? 0 : 1; |
| 29 |
| 30 // Create a transparent bitmap 1 pixel wider and/or taller than the |
| 31 // original, then copy the orignal into it. |
| 32 // FIXME: Is there a better way to pad (not scale) an image in skia? |
| 33 SkImageInfo info = this->getBitmapInfo(); |
| 34 info.fWidth = info.width() + expandW; |
| 35 info.fHeight = info.height() + expandH; |
| 36 // we explicitly require non-opaquness, since we are going to add a transpar
ent strip. |
| 37 info.fAlphaType = kPremul_SkAlphaType; |
| 38 |
| 39 SkBitmap bm2; |
| 40 bm2.allocPixels(info); |
| 41 SkCanvas canvas(bm2); |
| 42 |
| 43 SkPaint paint; |
| 44 paint.setXfermodeMode(SkXfermode::kSrc_Mode); |
| 45 |
| 46 this->drawBitmapToCanvas(canvas, paint); |
| 47 |
| 48 paint.setARGB(0x00, 0x00, 0x00, 0x00); |
| 49 paint.setStyle(SkPaint::kFill_Style); |
| 50 |
| 51 if (!isRepeatX()) |
| 52 canvas.drawRect(SkRect::MakeXYWH(info.width() - 1, 0, 1, info.height()),
paint); |
| 53 |
| 54 if (!isRepeatY()) |
| 55 canvas.drawRect(SkRect::MakeXYWH(0, info.height() - 1, info.width(), 1),
paint); |
| 56 |
| 57 bm2.setImmutable(); |
| 58 |
| 59 adjustExternalMemoryAllocated(bm2.getSafeSize()); |
| 60 |
| 61 SkMatrix localMatrix = affineTransformToSkMatrix(m_patternSpaceTransformatio
n); |
| 62 |
| 63 return adoptRef(SkShader::CreateBitmapShader(bm2, tileModeX, tileModeY, &loc
alMatrix)); |
| 64 } |
| 65 |
| 66 } // namespace |
OLD | NEW |