本文整理汇总了C++中SkShader::setLocalMatrix方法的典型用法代码示例。如果您正苦于以下问题:C++ SkShader::setLocalMatrix方法的具体用法?C++ SkShader::setLocalMatrix怎么用?C++ SkShader::setLocalMatrix使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SkShader
的用法示例。
在下文中一共展示了SkShader::setLocalMatrix方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: getShader
SkShader* Gradient::getShader(SkShader::TileMode mode)
{
if (NULL == m_gradient)
m_gradient = new PlatformGradientRec;
else if (mode == m_gradient->m_tileMode)
return m_gradient->m_shader;
// need to ensure that the m_stops array is sorted. We call getColor()
// which, as a side effect, does the sort.
// TODO: refactor Gradient.h to formally expose a sort method
{
float r, g, b, a;
this->getColor(0, &r, &g, &b, &a);
}
SkPoint pts[2] = { m_p0, m_p1 }; // convert to SkPoint
const size_t count = m_stops.size();
SkAutoMalloc storage(count * (sizeof(SkColor) + sizeof(SkScalar)));
SkColor* colors = (SkColor*)storage.get();
SkScalar* pos = (SkScalar*)(colors + count);
Vector<ColorStop>::iterator iter = m_stops.begin();
for (int i = 0; iter != m_stops.end(); i++) {
pos[i] = SkFloatToScalar(iter->stop);
colors[i] = SkColorSetARGB(F2B(iter->alpha), F2B(iter->red),
F2B(iter->green), F2B(iter->blue));
++iter;
}
SkShader* s;
if (m_radial)
s = SkGradientShader::CreateTwoPointRadial(pts[0],
SkFloatToScalar(m_r0),
pts[1],
SkFloatToScalar(m_r1),
colors, pos, count, mode);
else
s = SkGradientShader::CreateLinear(pts, colors, pos, count, mode);
if (NULL == s)
s = new SkColorShader(0);
// zap our previous shader, if present
SkSafeUnref(m_gradient->m_shader);
m_gradient->m_shader = s;
m_gradient->m_tileMode = mode;
SkMatrix matrix = m_gradientSpaceTransformation;
s->setLocalMatrix(matrix);
return s;
}
示例2: str
ImageView() {
SkImageRef_GlobalPool::SetRAMBudget(32 * 1024);
int i, N = SK_ARRAY_COUNT(gNames);
fBitmaps = new SkBitmap[N];
for (i = 0; i < N; i++) {
SkString str("/skimages/");
str.append(gNames[i]);
SkFILEStream* stream = new SkFILEStream(str.c_str());
SetImageRef(&fBitmaps[i], stream, SkBitmap::kNo_Config, gNames[i]);
if (i & 1)
fBitmaps[i].buildMipMap();
stream->unref();
}
fShader = SkShader::CreateBitmapShader(fBitmaps[5],
SkShader::kRepeat_TileMode,
SkShader::kRepeat_TileMode);
if (true) {
SkMatrix m;
m.setRotate(SkIntToScalar(30));
fShader->setLocalMatrix(m);
}
#if 0
SkImageRef::DumpPool();
for (i = 0; i < N; i++) {
SkBitmap& bm = fBitmaps[i];
SkDebugf("<%s> addr=%p", gNames[i], bm.getPixels());
bool success = bm.lockPixels();
SkDebugf(" addr=%d", bm.getPixels());
if (success)
bm.unlockPixels();
SkDebugf(" addr=%p", bm.getPixels());
success = bm.lockPixels();
SkDebugf(" addr=%d", bm.getPixels());
if (success)
bm.unlockPixels();
SkDebugf("\n");
}
SkImageRef::DumpPool();
#endif
}
示例3: createChecker
static SkShader* createChecker() {
// SkColor colors[] = { 0xFFFDFDFD, 0xFFF4F4F4 };
SkColor colors[] = { 0xFFFFFFFF, 0xFFFFFFFF };
SkBitmap bm;
bm.allocN32Pixels(2, 2);
bm.lockPixels();
*bm.getAddr32(0, 0) = *bm.getAddr32(1, 1) = SkPreMultiplyColor(colors[0]);
*bm.getAddr32(0, 1) = *bm.getAddr32(1, 0) = SkPreMultiplyColor(colors[1]);
SkShader* s = SkShader::CreateBitmapShader(bm, SkShader::kRepeat_TileMode,
SkShader::kRepeat_TileMode);
SkMatrix m;
m.setScale(12, 12);
s->setLocalMatrix(m);
return s;
}
示例4: drawBitmapPattern
void PlatformGraphicsContextSkia::drawBitmapPattern(
const SkBitmap& bitmap, const SkMatrix& matrix,
CompositeOperator compositeOp, const FloatRect& destRect)
{
SkShader* shader = SkShader::CreateBitmapShader(bitmap,
SkShader::kRepeat_TileMode,
SkShader::kRepeat_TileMode);
shader->setLocalMatrix(matrix);
SkPaint paint;
setupPaintCommon(&paint);
paint.setAlpha(getNormalizedAlpha());
paint.setShader(shader)->unref();
paint.setXfermodeMode(WebCoreCompositeToSkiaComposite(compositeOp));
fixPaintForBitmapsThatMaySeam(&paint);
mCanvas->drawRect(destRect, paint);
}
示例5: make_bg_shader
static SkShader* make_bg_shader() {
SkBitmap bm;
bm.allocN32Pixels(2, 2);
*bm.getAddr32(0, 0) = *bm.getAddr32(1, 1) = 0xFFFFFFFF;
*bm.getAddr32(1, 0) = *bm.getAddr32(0, 1) = SkPackARGB32(0xFF, 0xCC,
0xCC, 0xCC);
SkShader* s = SkShader::CreateBitmapShader(bm,
SkShader::kRepeat_TileMode,
SkShader::kRepeat_TileMode);
SkMatrix m;
m.setScale(SkIntToScalar(6), SkIntToScalar(6));
s->setLocalMatrix(m);
return s;
}
示例6: setBitmapDash
static void setBitmapDash(SkPaint* paint, int width) {
SkColor c = paint->getColor();
SkBitmap bm;
bm.allocN32Pixels(2, 1);
bm.lockPixels();
*bm.getAddr32(0, 0) = SkPreMultiplyARGB(0xFF, SkColorGetR(c),
SkColorGetG(c), SkColorGetB(c));
*bm.getAddr32(1, 0) = 0;
bm.unlockPixels();
SkMatrix matrix;
matrix.setScale(SkIntToScalar(width), SK_Scalar1);
SkShader* s = SkShader::CreateBitmapShader(bm, SkShader::kRepeat_TileMode,
SkShader::kClamp_TileMode);
s->setLocalMatrix(matrix);
paint->setShader(s)->unref();
}
示例7: drawmarshmallow
static void drawmarshmallow(SkCanvas* canvas) {
SkBitmap bitmap;
SkPaint paint;
SkRect r;
SkMatrix m;
SkImageDecoder::DecodeFile("/Users/reed/Downloads/3elfs.jpg", &bitmap);
SkShader* s = SkShader::CreateBitmapShader(bitmap,
SkShader::kRepeat_TileMode,
SkShader::kRepeat_TileMode);
paint.setShader(s)->unref();
m.setTranslate(SkIntToScalar(250), SkIntToScalar(134));
s->setLocalMatrix(m);
r.set(SkIntToScalar(250),
SkIntToScalar(134),
SkIntToScalar(250 + 449),
SkIntToScalar(134 + 701));
paint.setFlags(2);
canvas->drawRect(r, paint);
}
示例8: drawLineForMisspellingOrBadGrammar
void GraphicsContext::drawLineForMisspellingOrBadGrammar(const IntPoint& pt,
int width,
bool grammar)
{
if (paintingDisabled())
return;
// Create the pattern we'll use to draw the underline.
static SkBitmap* misspellBitmap = 0;
if (!misspellBitmap) {
// We use a 2-pixel-high misspelling indicator because that seems to be
// what WebKit is designed for, and how much room there is in a typical
// page for it.
const int rowPixels = 32; // Must be multiple of 4 for pattern below.
const int colPixels = 2;
misspellBitmap = new SkBitmap;
misspellBitmap->setConfig(SkBitmap::kARGB_8888_Config,
rowPixels, colPixels);
misspellBitmap->allocPixels();
misspellBitmap->eraseARGB(0, 0, 0, 0);
const uint32_t lineColor = 0xFFFF0000; // Opaque red.
const uint32_t antiColor = 0x60600000; // Semitransparent red.
// Pattern: X o o X o o X
// o X o o X o
uint32_t* row1 = misspellBitmap->getAddr32(0, 0);
uint32_t* row2 = misspellBitmap->getAddr32(0, 1);
for (int x = 0; x < rowPixels; x++) {
switch (x % 4) {
case 0:
row1[x] = lineColor;
break;
case 1:
row1[x] = antiColor;
row2[x] = antiColor;
break;
case 2:
row2[x] = lineColor;
break;
case 3:
row1[x] = antiColor;
row2[x] = antiColor;
break;
}
}
}
// Offset it vertically by 1 so that there's some space under the text.
SkScalar originX = SkIntToScalar(pt.x());
SkScalar originY = SkIntToScalar(pt.y()) + 1;
// Make a shader for the bitmap with an origin of the box we'll draw. This
// shader is refcounted and will have an initial refcount of 1.
SkShader* shader = SkShader::CreateBitmapShader(
*misspellBitmap, SkShader::kRepeat_TileMode,
SkShader::kRepeat_TileMode);
SkMatrix matrix;
matrix.reset();
matrix.postTranslate(originX, originY);
shader->setLocalMatrix(matrix);
// Assign the shader to the paint & release our reference. The paint will
// now own the shader and the shader will be destroyed when the paint goes
// out of scope.
SkPaint paint;
paint.setShader(shader);
shader->unref();
SkRect rect;
rect.set(originX,
originY,
originX + SkIntToScalar(width),
originY + SkIntToScalar(misspellBitmap->height()));
platformContext()->canvas()->drawRect(rect, paint);
}
示例9: drawPattern
void Image::drawPattern(GraphicsContext* context,
const FloatRect& floatSrcRect,
const AffineTransform& patternTransform,
const FloatPoint& phase,
ColorSpace styleColorSpace,
CompositeOperator compositeOp,
const FloatRect& destRect)
{
FloatRect normSrcRect = normalizeRect(floatSrcRect);
if (destRect.isEmpty() || normSrcRect.isEmpty())
return; // nothing to draw
NativeImageSkia* bitmap = nativeImageForCurrentFrame();
if (!bitmap)
return;
SkIRect srcRect = enclosingIntRect(normSrcRect);
// Figure out what size the bitmap will be in the destination. The
// destination rect is the bounds of the pattern, we need to use the
// matrix to see how big it will be.
float destBitmapWidth, destBitmapHeight;
TransformDimensions(patternTransform, srcRect.width(), srcRect.height(),
&destBitmapWidth, &destBitmapHeight);
// Compute the resampling mode.
ResamplingMode resampling;
if (context->platformContext()->isAccelerated() || context->platformContext()->printing())
resampling = RESAMPLE_LINEAR;
else
resampling = computeResamplingMode(context->platformContext(), *bitmap, srcRect.width(), srcRect.height(), destBitmapWidth, destBitmapHeight);
// Load the transform WebKit requested.
SkMatrix matrix(patternTransform);
SkShader* shader;
if (resampling == RESAMPLE_AWESOME) {
// Do nice resampling.
int width = static_cast<int>(destBitmapWidth);
int height = static_cast<int>(destBitmapHeight);
SkBitmap resampled = bitmap->resizedBitmap(srcRect, width, height);
shader = SkShader::CreateBitmapShader(resampled, SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode);
// Since we just resized the bitmap, we need to undo the scale set in
// the image transform.
matrix.setScaleX(SkIntToScalar(1));
matrix.setScaleY(SkIntToScalar(1));
} else {
// No need to do nice resampling.
SkBitmap srcSubset;
bitmap->bitmap().extractSubset(&srcSubset, srcRect);
shader = SkShader::CreateBitmapShader(srcSubset, SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode);
}
// We also need to translate it such that the origin of the pattern is the
// origin of the destination rect, which is what WebKit expects. Skia uses
// the coordinate system origin as the base for the patter. If WebKit wants
// a shifted image, it will shift it from there using the patternTransform.
float adjustedX = phase.x() + normSrcRect.x() *
narrowPrecisionToFloat(patternTransform.a());
float adjustedY = phase.y() + normSrcRect.y() *
narrowPrecisionToFloat(patternTransform.d());
matrix.postTranslate(SkFloatToScalar(adjustedX),
SkFloatToScalar(adjustedY));
shader->setLocalMatrix(matrix);
SkPaint paint;
paint.setShader(shader)->unref();
paint.setXfermodeMode(WebCoreCompositeToSkiaComposite(compositeOp));
paint.setFilterBitmap(resampling == RESAMPLE_LINEAR);
context->platformContext()->paintSkPaint(destRect, paint);
}
示例10: MarkChanged
void
DrawTargetSkia::DrawSurfaceWithShadow(SourceSurface *aSurface,
const Point &aDest,
const Color &aColor,
const Point &aOffset,
Float aSigma,
CompositionOp aOperator)
{
MarkChanged();
mCanvas->save(SkCanvas::kMatrix_SaveFlag);
mCanvas->resetMatrix();
uint32_t blurFlags = SkBlurMaskFilter::kHighQuality_BlurFlag |
SkBlurMaskFilter::kIgnoreTransform_BlurFlag;
const SkBitmap& bitmap = static_cast<SourceSurfaceSkia*>(aSurface)->GetBitmap();
SkShader* shader = SkShader::CreateBitmapShader(bitmap, SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);
SkMatrix matrix;
matrix.reset();
matrix.setTranslateX(SkFloatToScalar(aDest.x));
matrix.setTranslateY(SkFloatToScalar(aDest.y));
shader->setLocalMatrix(matrix);
SkLayerDrawLooper* dl = new SkLayerDrawLooper;
SkLayerDrawLooper::LayerInfo info;
info.fPaintBits |= SkLayerDrawLooper::kShader_Bit;
SkPaint *layerPaint = dl->addLayer(info);
layerPaint->setShader(shader);
info.fPaintBits = 0;
info.fPaintBits |= SkLayerDrawLooper::kMaskFilter_Bit;
info.fPaintBits |= SkLayerDrawLooper::kColorFilter_Bit;
info.fColorMode = SkXfermode::kDst_Mode;
info.fOffset.set(SkFloatToScalar(aOffset.x), SkFloatToScalar(aOffset.y));
info.fPostTranslate = true;
SkMaskFilter* mf = SkBlurMaskFilter::Create(aSigma, SkBlurMaskFilter::kNormal_BlurStyle, blurFlags);
SkColor color = ColorToSkColor(aColor, 1);
SkColorFilter* cf = SkColorFilter::CreateModeFilter(color, SkXfermode::kSrcIn_Mode);
layerPaint = dl->addLayer(info);
SkSafeUnref(layerPaint->setMaskFilter(mf));
SkSafeUnref(layerPaint->setColorFilter(cf));
layerPaint->setColor(color);
// TODO: This is using the rasterizer to calculate an alpha mask
// on both the shadow and normal layers. We should fix this
// properly so it only happens for the shadow layer
SkLayerRasterizer *raster = new SkLayerRasterizer();
SkPaint maskPaint;
SkSafeUnref(maskPaint.setShader(shader));
raster->addLayer(maskPaint, 0, 0);
SkPaint paint;
paint.setAntiAlias(true);
SkSafeUnref(paint.setRasterizer(raster));
paint.setXfermodeMode(GfxOpToSkiaOp(aOperator));
SkSafeUnref(paint.setLooper(dl));
SkRect rect = RectToSkRect(Rect(Float(aDest.x), Float(aDest.y),
Float(bitmap.width()), Float(bitmap.height())));
mCanvas->drawRect(rect, paint);
mCanvas->restore();
}
示例11: drawLineForDocumentMarker
//.........这里部分代码省略.........
const int colPixels = 2;
#endif
misspellBitmap[index] = new SkBitmap;
misspellBitmap[index]->setConfig(SkBitmap::kARGB_8888_Config,
rowPixels, colPixels);
misspellBitmap[index]->allocPixels();
misspellBitmap[index]->eraseARGB(0, 0, 0, 0);
#if PLATFORM(CHROMIUM) && OS(DARWIN)
const uint32_t colors[2][6] = {
{ 0x2A2A0600, 0x57571000, 0xA8A81B00, 0xBFBF1F00, 0x70701200, 0xE0E02400 },
{ 0x2A001503, 0x57002A08, 0xA800540D, 0xBF005F0F, 0x70003809, 0xE0007012 }
};
const uint32_t transparentColor = 0x00000000;
// Pattern: a b a a b a
// c d c c d c
// e f e e f e
for (int x = 0; x < colPixels; ++x) {
uint32_t* row = misspellBitmap[index]->getAddr32(0, x);
row[0] = colors[index][x * 2];
row[1] = colors[index][x * 2 + 1];
row[2] = colors[index][x * 2];
row[3] = transparentColor;
}
#else
static const uint32_t lineColors[2] = {
0xFF << SK_A32_SHIFT | 0xFF << SK_R32_SHIFT, // Opaque red.
0xFF << SK_A32_SHIFT | 0xC0 << SK_R32_SHIFT | 0xC0 << SK_G32_SHIFT | 0xC0 << SK_B32_SHIFT, // Opaque gray.
};
static const uint32_t antiColors[2] = {
0x60 << SK_A32_SHIFT | 0x60 << SK_R32_SHIFT, // Semitransparent red
0xFF << SK_A32_SHIFT | 0xC0 << SK_R32_SHIFT | 0xC0 << SK_G32_SHIFT | 0xC0 << SK_B32_SHIFT, // Semitransparent gray
};
const uint32_t lineColor = lineColors[index];
const uint32_t antiColor = antiColors[index];
// Pattern: X o o X o o X
// o X o o X o
uint32_t* row1 = misspellBitmap[index]->getAddr32(0, 0);
uint32_t* row2 = misspellBitmap[index]->getAddr32(0, 1);
for (int x = 0; x < rowPixels; x++) {
switch (x % 4) {
case 0:
row1[x] = lineColor;
break;
case 1:
row1[x] = antiColor;
row2[x] = antiColor;
break;
case 2:
row2[x] = lineColor;
break;
case 3:
row1[x] = antiColor;
row2[x] = antiColor;
break;
}
}
#endif
}
SkScalar originX = WebCoreFloatToSkScalar(pt.x());
#if PLATFORM(CHROMIUM) && OS(DARWIN)
SkScalar originY = WebCoreFloatToSkScalar(pt.y());
// Make sure to draw only complete dots.
int rowPixels = misspellBitmap[index]->width();
float widthMod = fmodf(width, rowPixels);
if (rowPixels - widthMod > 1)
width -= widthMod;
#else
// Offset it vertically by 1 so that there's some space under the text.
SkScalar originY = WebCoreFloatToSkScalar(pt.y()) + 1;
#endif
// Make a shader for the bitmap with an origin of the box we'll draw. This
// shader is refcounted and will have an initial refcount of 1.
SkShader* shader = SkShader::CreateBitmapShader(
*misspellBitmap[index], SkShader::kRepeat_TileMode,
SkShader::kRepeat_TileMode);
SkMatrix matrix;
matrix.reset();
matrix.postTranslate(originX, originY);
shader->setLocalMatrix(matrix);
// Assign the shader to the paint & release our reference. The paint will
// now own the shader and the shader will be destroyed when the paint goes
// out of scope.
SkPaint paint;
paint.setShader(shader);
shader->unref();
SkRect rect;
rect.set(originX,
originY,
originX + WebCoreFloatToSkScalar(width),
originY + SkIntToScalar(misspellBitmap[index]->height()));
platformContext()->canvas()->drawRect(rect, paint);
platformContext()->didDrawRect(rect, paint);
}
示例12: SkColorShader
static SkShader*
source_to_sk_shader (cairo_skia_context_t *cr,
const cairo_pattern_t *pattern)
{
SkShader *shader = NULL;
if (pattern->type == CAIRO_PATTERN_TYPE_SOLID) {
cairo_solid_pattern_t *solid = (cairo_solid_pattern_t *) pattern;
return new SkColorShader (color_to_sk (solid->color));
} else if (pattern->type == CAIRO_PATTERN_TYPE_SURFACE) {
cairo_surface_t *surface = surface_from_pattern (pattern);
cr->source = cairo_surface_reference (surface);
if (surface->type == CAIRO_SURFACE_TYPE_SKIA) {
cairo_skia_surface_t *esurf = (cairo_skia_surface_t *) surface;
shader = SkShader::CreateBitmapShader (*esurf->bitmap,
extend_to_sk (pattern->extend),
extend_to_sk (pattern->extend));
} else {
SkBitmap bitmap;
if (! _cairo_surface_is_image (surface)) {
cairo_status_t status;
status = _cairo_surface_acquire_source_image (surface,
&cr->source_image,
&cr->source_extra);
if (status)
return NULL;
surface = &cr->source_image->base;
}
if (unlikely (! surface_to_sk_bitmap (surface, bitmap)))
return NULL;
shader = SkShader::CreateBitmapShader (bitmap,
extend_to_sk (pattern->extend),
extend_to_sk (pattern->extend));
}
} else if (pattern->type == CAIRO_PATTERN_TYPE_LINEAR
/* || pattern->type == CAIRO_PATTERN_TYPE_RADIAL */)
{
cairo_gradient_pattern_t *gradient = (cairo_gradient_pattern_t *) pattern;
SkColor colors_stack[10];
SkScalar pos_stack[10];
SkColor *colors = colors_stack;
SkScalar *pos = pos_stack;
if (gradient->n_stops > 10) {
colors = new SkColor[gradient->n_stops];
pos = new SkScalar[gradient->n_stops];
}
for (unsigned int i = 0; i < gradient->n_stops; i++) {
pos[i] = CAIRO_FIXED_TO_SK_SCALAR (gradient->stops[i].offset);
colors[i] = color_stop_to_sk (gradient->stops[i].color);
}
if (pattern->type == CAIRO_PATTERN_TYPE_LINEAR) {
cairo_linear_pattern_t *linear = (cairo_linear_pattern_t *) gradient;
SkPoint points[2];
points[0].set (SkFloatToScalar (linear->pd1.x),
SkFloatToScalar (linear->pd1.y));
points[1].set (SkFloatToScalar (linear->pd2.x),
SkFloatToScalar (linear->pd2.y));
shader = SkGradientShader::CreateLinear (points, colors, pos, gradient->n_stops,
extend_to_sk (pattern->extend));
} else {
// XXX todo -- implement real radial shaders in Skia
}
if (gradient->n_stops > 10) {
delete [] colors;
delete [] pos;
}
}
if (shader && ! _cairo_matrix_is_identity (&pattern->matrix))
shader->setLocalMatrix (matrix_inverse_to_sk (pattern->matrix));
return shader;
}
示例13: drawPattern
void Image::drawPattern(GraphicsContext* context,
const FloatRect& floatSrcRect,
const AffineTransform& patternTransform,
const FloatPoint& phase,
ColorSpace styleColorSpace,
CompositeOperator compositeOp,
const FloatRect& destRect)
{
#if PLATFORM(CHROMIUM)
TRACE_EVENT0("skia", "Image::drawPattern");
#endif
FloatRect normSrcRect = normalizeRect(floatSrcRect);
if (destRect.isEmpty() || normSrcRect.isEmpty())
return; // nothing to draw
NativeImageSkia* bitmap = nativeImageForCurrentFrame();
if (!bitmap)
return;
SkMatrix ctm = context->platformContext()->canvas()->getTotalMatrix();
SkMatrix totalMatrix;
totalMatrix.setConcat(ctm, patternTransform);
// Figure out what size the bitmap will be in the destination. The
// destination rect is the bounds of the pattern, we need to use the
// matrix to see how big it will be.
SkRect destRectTarget;
totalMatrix.mapRect(&destRectTarget, normSrcRect);
float destBitmapWidth = SkScalarToFloat(destRectTarget.width());
float destBitmapHeight = SkScalarToFloat(destRectTarget.height());
// Compute the resampling mode.
ResamplingMode resampling;
if (context->platformContext()->isAccelerated() || context->platformContext()->printing())
resampling = RESAMPLE_LINEAR;
else
resampling = computeResamplingMode(totalMatrix, *bitmap, normSrcRect.width(), normSrcRect.height(), destBitmapWidth, destBitmapHeight);
resampling = limitResamplingMode(context->platformContext(), resampling);
// Load the transform WebKit requested.
SkMatrix matrix(patternTransform);
SkShader* shader;
if (resampling == RESAMPLE_AWESOME) {
// Do nice resampling.
float scaleX = destBitmapWidth / normSrcRect.width();
float scaleY = destBitmapHeight / normSrcRect.height();
SkRect scaledSrcRect;
SkIRect enclosingScaledSrcRect;
// The image fragment generated here is not exactly what is
// requested. The scale factor used is approximated and image
// fragment is slightly larger to align to integer
// boundaries.
SkBitmap resampled = extractScaledImageFragment(*bitmap, normSrcRect, scaleX, scaleY, &scaledSrcRect, &enclosingScaledSrcRect);
shader = SkShader::CreateBitmapShader(resampled, SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode);
// Since we just resized the bitmap, we need to remove the scale
// applied to the pixels in the bitmap shader. This means we need
// CTM * patternTransform to have identity scale. Since we
// can't modify CTM (or the rectangle will be drawn in the wrong
// place), we must set patternTransform's scale to the inverse of
// CTM scale.
matrix.setScaleX(ctm.getScaleX() ? 1 / ctm.getScaleX() : 1);
matrix.setScaleY(ctm.getScaleY() ? 1 / ctm.getScaleY() : 1);
} else {
// No need to do nice resampling.
SkBitmap srcSubset;
bitmap->bitmap().extractSubset(&srcSubset, enclosingIntRect(normSrcRect));
shader = SkShader::CreateBitmapShader(srcSubset, SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode);
}
// We also need to translate it such that the origin of the pattern is the
// origin of the destination rect, which is what WebKit expects. Skia uses
// the coordinate system origin as the base for the patter. If WebKit wants
// a shifted image, it will shift it from there using the patternTransform.
float adjustedX = phase.x() + normSrcRect.x() *
narrowPrecisionToFloat(patternTransform.a());
float adjustedY = phase.y() + normSrcRect.y() *
narrowPrecisionToFloat(patternTransform.d());
matrix.postTranslate(SkFloatToScalar(adjustedX),
SkFloatToScalar(adjustedY));
shader->setLocalMatrix(matrix);
SkPaint paint;
paint.setShader(shader)->unref();
paint.setXfermodeMode(WebCoreCompositeToSkiaComposite(compositeOp));
paint.setFilterBitmap(resampling == RESAMPLE_LINEAR);
context->platformContext()->paintSkPaint(destRect, paint);
}
示例14: drawBitmapRect
void SkDevice::drawBitmapRect(const SkDraw& draw, const SkBitmap& bitmap,
const SkRect* src, const SkRect& dst,
const SkPaint& paint) {
SkMatrix matrix;
SkRect bitmapBounds, tmpSrc, tmpDst;
SkBitmap tmpBitmap;
bitmapBounds.isetWH(bitmap.width(), bitmap.height());
// Compute matrix from the two rectangles
if (src) {
tmpSrc = *src;
} else {
tmpSrc = bitmapBounds;
}
matrix.setRectToRect(tmpSrc, dst, SkMatrix::kFill_ScaleToFit);
const SkRect* dstPtr = &dst;
const SkBitmap* bitmapPtr = &bitmap;
// clip the tmpSrc to the bounds of the bitmap, and recompute dstRect if
// needed (if the src was clipped). No check needed if src==null.
if (src) {
if (!bitmapBounds.contains(*src)) {
if (!tmpSrc.intersect(bitmapBounds)) {
return; // nothing to draw
}
// recompute dst, based on the smaller tmpSrc
matrix.mapRect(&tmpDst, tmpSrc);
dstPtr = &tmpDst;
}
// since we may need to clamp to the borders of the src rect within
// the bitmap, we extract a subset.
SkIRect srcIR;
tmpSrc.roundOut(&srcIR);
if (!bitmap.extractSubset(&tmpBitmap, srcIR)) {
return;
}
bitmapPtr = &tmpBitmap;
// Since we did an extract, we need to adjust the matrix accordingly
SkScalar dx = 0, dy = 0;
if (srcIR.fLeft > 0) {
dx = SkIntToScalar(srcIR.fLeft);
}
if (srcIR.fTop > 0) {
dy = SkIntToScalar(srcIR.fTop);
}
if (dx || dy) {
matrix.preTranslate(dx, dy);
}
SkRect extractedBitmapBounds;
extractedBitmapBounds.isetWH(bitmapPtr->width(), bitmapPtr->height());
if (extractedBitmapBounds == tmpSrc) {
// no fractional part in src, we can just call drawBitmap
goto USE_DRAWBITMAP;
}
} else {
USE_DRAWBITMAP:
// We can go faster by just calling drawBitmap, which will concat the
// matrix with the CTM, and try to call drawSprite if it can. If not,
// it will make a shader and call drawRect, as we do below.
this->drawBitmap(draw, *bitmapPtr, matrix, paint);
return;
}
// construct a shader, so we can call drawRect with the dst
SkShader* s = SkShader::CreateBitmapShader(*bitmapPtr,
SkShader::kClamp_TileMode,
SkShader::kClamp_TileMode);
if (NULL == s) {
return;
}
s->setLocalMatrix(matrix);
SkPaint paintWithShader(paint);
paintWithShader.setStyle(SkPaint::kFill_Style);
paintWithShader.setShader(s)->unref();
// Call ourself, in case the subclass wanted to share this setup code
// but handle the drawRect code themselves.
this->drawRect(draw, *dstPtr, paintWithShader);
}
示例15: onDraw
virtual void onDraw(SkCanvas* canvas) {
canvas->translate(SkIntToScalar(10), SkIntToScalar(20));
this->drawBG(canvas);
const struct {
SkXfermode::Mode fMode;
const char* fLabel;
} gModes[] = {
{ SkXfermode::kClear_Mode, "Clear" },
{ SkXfermode::kSrc_Mode, "Src" },
{ SkXfermode::kDst_Mode, "Dst" },
{ SkXfermode::kSrcOver_Mode, "SrcOver" },
{ SkXfermode::kDstOver_Mode, "DstOver" },
{ SkXfermode::kSrcIn_Mode, "SrcIn" },
{ SkXfermode::kDstIn_Mode, "DstIn" },
{ SkXfermode::kSrcOut_Mode, "SrcOut" },
{ SkXfermode::kDstOut_Mode, "DstOut" },
{ SkXfermode::kSrcATop_Mode, "SrcATop" },
{ SkXfermode::kDstATop_Mode, "DstATop" },
{ SkXfermode::kXor_Mode, "Xor" },
{ SkXfermode::kPlus_Mode, "Plus" },
{ SkXfermode::kMultiply_Mode, "Multiply" },
{ SkXfermode::kScreen_Mode, "Screen" },
{ SkXfermode::kOverlay_Mode, "Overlay" },
{ SkXfermode::kDarken_Mode, "Darken" },
{ SkXfermode::kLighten_Mode, "Lighten" },
{ SkXfermode::kColorDodge_Mode, "ColorDodge" },
{ SkXfermode::kColorBurn_Mode, "ColorBurn" },
{ SkXfermode::kHardLight_Mode, "HardLight" },
{ SkXfermode::kSoftLight_Mode, "SoftLight" },
{ SkXfermode::kDifference_Mode, "Difference" },
{ SkXfermode::kExclusion_Mode, "Exclusion" },
};
const SkScalar w = SkIntToScalar(W);
const SkScalar h = SkIntToScalar(H);
SkShader* s = SkShader::CreateBitmapShader(fBG,
SkShader::kRepeat_TileMode,
SkShader::kRepeat_TileMode);
SkMatrix m;
m.setScale(SkIntToScalar(6), SkIntToScalar(6));
s->setLocalMatrix(m);
SkPaint labelP;
labelP.setAntiAlias(true);
labelP.setTextAlign(SkPaint::kCenter_Align);
const int W = 5;
SkScalar x0 = 0;
for (int twice = 0; twice < 2; twice++) {
SkScalar x = x0, y = 0;
for (size_t i = 0; i < SK_ARRAY_COUNT(gModes); i++) {
SkXfermode* mode = SkXfermode::Create(gModes[i].fMode);
SkAutoUnref aur(mode);
SkRect r;
r.set(x, y, x+w, y+h);
SkPaint p;
p.setStyle(SkPaint::kFill_Style);
p.setShader(s);
canvas->drawRect(r, p);
canvas->saveLayer(&r, NULL, SkCanvas::kARGB_ClipLayer_SaveFlag);
draw_mode(canvas, mode, twice ? 0x88 : 0xFF, r.fLeft, r.fTop);
canvas->restore();
r.inset(-SK_ScalarHalf, -SK_ScalarHalf);
p.setStyle(SkPaint::kStroke_Style);
p.setShader(NULL);
canvas->drawRect(r, p);
#if 1
canvas->drawText(gModes[i].fLabel, strlen(gModes[i].fLabel),
x + w/2, y - labelP.getTextSize()/2, labelP);
#endif
x += w + SkIntToScalar(10);
if ((i % W) == W - 1) {
x = x0;
y += h + SkIntToScalar(30);
}
}
x0 += SkIntToScalar(400);
}
s->unref();
}