| 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 // we explicitly require non-opaquness, since we are going to add a transpar
ent strip. | |
| 35 info = SkImageInfo::Make(info.width() + expandW, info.height() + expandH, in
fo.colorType(), kPremul_SkAlphaType); | |
| 36 | |
| 37 SkBitmap bm2; | |
| 38 bm2.allocPixels(info); | |
| 39 bm2.eraseColor(SK_ColorTRANSPARENT); | |
| 40 SkCanvas canvas(bm2); | |
| 41 | |
| 42 SkPaint paint; | |
| 43 paint.setXfermodeMode(SkXfermode::kSrc_Mode); | |
| 44 | |
| 45 this->drawBitmapToCanvas(canvas, paint); | |
| 46 | |
| 47 paint.setARGB(0x00, 0x00, 0x00, 0x00); | |
| 48 paint.setStyle(SkPaint::kFill_Style); | |
| 49 | |
| 50 if (!isRepeatX()) | |
| 51 canvas.drawRect(SkRect::MakeXYWH(info.width() - 1, 0, 1, info.height()),
paint); | |
| 52 | |
| 53 if (!isRepeatY()) | |
| 54 canvas.drawRect(SkRect::MakeXYWH(0, info.height() - 1, info.width(), 1),
paint); | |
| 55 | |
| 56 bm2.setImmutable(); | |
| 57 | |
| 58 adjustExternalMemoryAllocated(bm2.getSafeSize()); | |
| 59 | |
| 60 SkMatrix localMatrix = affineTransformToSkMatrix(m_patternSpaceTransformatio
n); | |
| 61 | |
| 62 return adoptRef(SkShader::CreateBitmapShader(bm2, tileModeX, tileModeY, &loc
alMatrix)); | |
| 63 } | |
| 64 | |
| 65 } // namespace | |
| OLD | NEW |