CameraView() { fRX = fRY = fRZ = 0; fShaderIndex = 0; fFrontFace = false; for (int i = 0;; i++) { SkString str; str.printf("/skimages/elephant%d.jpeg", i); SkBitmap bm; if (SkImageDecoder::DecodeFile(str.c_str(), &bm)) { SkShader* s = SkShader::CreateBitmapShader(bm, SkShader::kClamp_TileMode, SkShader::kClamp_TileMode); SkRect src = { 0, 0, SkIntToScalar(bm.width()), SkIntToScalar(bm.height()) }; SkRect dst = { -150, -150, 150, 150 }; SkMatrix matrix; matrix.setRectToRect(src, dst, SkMatrix::kFill_ScaleToFit); s->setLocalMatrix(matrix); *fShaders.append() = s; } else { break; } } this->setBGColor(0xFFDDDDDD); }
virtual void onDraw(SkCanvas* canvas) { SkPoint pts[2] = { { 0, 0 }, { SkIntToScalar(100), SkIntToScalar(100) } }; SkShader::TileMode tm = SkShader::kClamp_TileMode; SkRect r = { 0, 0, SkIntToScalar(100), SkIntToScalar(100) }; SkPaint paint; paint.setAntiAlias(true); canvas->translate(SkIntToScalar(20), SkIntToScalar(20)); for (size_t i = 0; i < SK_ARRAY_COUNT(gGradData); i++) { canvas->save(); for (size_t j = 0; j < SK_ARRAY_COUNT(gGradMakers); j++) { SkMatrix scale = SkMatrix::I(); if (i == 5) { // if the clamp case scale.setScale(0.5f, 0.5f); scale.postTranslate(25.f, 25.f); } SkShader* shader = gGradMakers[j](pts, gGradData[i], tm, scale); paint.setShader(shader); canvas->drawRect(r, paint); shader->unref(); canvas->translate(0, SkIntToScalar(120)); } canvas->restore(); canvas->translate(SkIntToScalar(120), 0); } }
static void gradient_slide(SkCanvas* canvas) { SkPoint pts[2] = { { 0, 0 }, { SkIntToScalar(100), SkIntToScalar(100) } }; SkShader::TileMode tm = SkShader::kClamp_TileMode; SkRect r = { 0, 0, SkIntToScalar(100), SkIntToScalar(100) }; SkPaint paint; paint.setAntiAlias(true); paint.setDither(true); canvas->translate(SkIntToScalar(20), SkIntToScalar(10)); for (size_t i = 0; i < SK_ARRAY_COUNT(gGradData); i++) { canvas->save(); for (size_t j = 0; j < SK_ARRAY_COUNT(gGradMakers); j++) { SkShader* shader = gGradMakers[j](pts, gGradData[i], tm); paint.setShader(shader); canvas->drawRect(r, paint); shader->unref(); canvas->translate(0, SkIntToScalar(120)); } canvas->restore(); canvas->translate(SkIntToScalar(120), 0); } }
SkPDFShader::State::State(const SkShader& shader, const SkMatrix& canvasTransform, const SkIRect& bbox) : fCanvasTransform(canvasTransform), fBBox(bbox), fPixelGeneration(0) { fInfo.fColorCount = 0; fInfo.fColors = NULL; fInfo.fColorOffsets = NULL; fShaderTransform = shader.getLocalMatrix(); fImageTileModes[0] = fImageTileModes[1] = SkShader::kClamp_TileMode; fType = shader.asAGradient(&fInfo); if (fType == SkShader::kNone_GradientType) { SkShader::BitmapType bitmapType; SkMatrix matrix; bitmapType = shader.asABitmap(&fImage, &matrix, fImageTileModes); if (bitmapType != SkShader::kDefault_BitmapType) { fImage.reset(); return; } SkASSERT(matrix.isIdentity()); fPixelGeneration = fImage.getGenerationID(); } else { AllocateGradientInfoStorage(); shader.asAGradient(&fInfo); } }
/* Hit a few SkPicture::Analysis cases not handled elsewhere. */ static void test_analysis(skiatest::Reporter* reporter) { SkPictureRecorder recorder; SkCanvas* canvas = recorder.beginRecording(100, 100); { canvas->drawRect(SkRect::MakeWH(10, 10), SkPaint ()); } SkAutoTUnref<SkPicture> picture(recorder.endRecording()); REPORTER_ASSERT(reporter, !picture->willPlayBackBitmaps()); canvas = recorder.beginRecording(100, 100); { SkPaint paint; // CreateBitmapShader is too smart for us; an empty (or 1x1) bitmap shader // gets optimized into a non-bitmap form, so we create a 2x2 bitmap here. SkBitmap bitmap; bitmap.allocPixels(SkImageInfo::MakeN32Premul(2, 2)); bitmap.eraseColor(SK_ColorBLUE); *(bitmap.getAddr32(0, 0)) = SK_ColorGREEN; SkShader* shader = SkShader::CreateBitmapShader(bitmap, SkShader::kClamp_TileMode, SkShader::kClamp_TileMode); paint.setShader(shader)->unref(); REPORTER_ASSERT(reporter, shader->asABitmap(NULL, NULL, NULL) == SkShader::kDefault_BitmapType); canvas->drawRect(SkRect::MakeWH(10, 10), paint); } picture.reset(recorder.endRecording()); REPORTER_ASSERT(reporter, picture->willPlayBackBitmaps()); }
void PlatformContextSkiaJava::drawRect(SkRect rect) { SkPaint paint; int fillcolorNotTransparent = m_state->m_fillColor & 0xFF000000; if (fillcolorNotTransparent) { setupPaintForFilling(&paint); canvas()->drawRect(rect, paint); } if (m_state->m_strokeStyle != WebCore::NoStroke && (m_state->m_strokeColor & 0xFF000000)) { // We do a fill of four rects to simulate the stroke of a border. SkColor oldFillColor = m_state->m_fillColor; // setFillColor() will set the shader to NULL, so save a ref to it now. SkShader* oldFillShader = m_state->m_fillShader; oldFillShader->safeRef(); setFillColor(m_state->m_strokeColor); paint.reset(); setupPaintForFilling(&paint); SkRect topBorder = { rect.fLeft, rect.fTop, rect.fRight, rect.fTop + 1 }; canvas()->drawRect(topBorder, paint); SkRect bottomBorder = { rect.fLeft, rect.fBottom - 1, rect.fRight, rect.fBottom }; canvas()->drawRect(bottomBorder, paint); SkRect leftBorder = { rect.fLeft, rect.fTop + 1, rect.fLeft + 1, rect.fBottom - 1 }; canvas()->drawRect(leftBorder, paint); SkRect rightBorder = { rect.fRight - 1, rect.fTop + 1, rect.fRight, rect.fBottom - 1 }; canvas()->drawRect(rightBorder, paint); setFillColor(oldFillColor); setFillShader(oldFillShader); oldFillShader->safeUnref(); } }
SkPDFShader::State::State(const SkShader& shader, const SkMatrix& canvasTransform, const SkIRect& bbox) : fCanvasTransform(canvasTransform), fBBox(bbox), fPixelGeneration(0) { fInfo.fColorCount = 0; fInfo.fColors = NULL; fInfo.fColorOffsets = NULL; shader.getLocalMatrix(&fShaderTransform); fImageTileModes[0] = fImageTileModes[1] = SkShader::kClamp_TileMode; fType = shader.asAGradient(&fInfo); if (fType == SkShader::kNone_GradientType) { SkShader::BitmapType bitmapType; SkMatrix matrix; bitmapType = shader.asABitmap(&fImage, &matrix, fImageTileModes, NULL); if (bitmapType != SkShader::kDefault_BitmapType) { fImage.reset(); return; } SkASSERT(matrix.isIdentity()); fPixelGeneration = fImage.getGenerationID(); } else { fColorData.set(sk_malloc_throw( fInfo.fColorCount * (sizeof(SkColor) + sizeof(SkScalar)))); fInfo.fColors = reinterpret_cast<SkColor*>(fColorData.get()); fInfo.fColorOffsets = reinterpret_cast<SkScalar*>(fInfo.fColors + fInfo.fColorCount); shader.asAGradient(&fInfo); } }
void DrawTargetSkia::DrawSurface(SourceSurface *aSurface, const Rect &aDest, const Rect &aSource, const DrawSurfaceOptions &aSurfOptions, const DrawOptions &aOptions) { if (aSurface->GetType() != SURFACE_SKIA) { return; } if (aSource.IsEmpty()) { return; } MarkChanged(); SkRect destRect = RectToSkRect(aDest); SkRect sourceRect = RectToSkRect(aSource); SkMatrix matrix; matrix.setRectToRect(sourceRect, destRect, SkMatrix::kFill_ScaleToFit); const SkBitmap& bitmap = static_cast<SourceSurfaceSkia*>(aSurface)->GetBitmap(); AutoPaintSetup paint(mCanvas.get(), aOptions); SkShader *shader = SkShader::CreateBitmapShader(bitmap, SkShader::kClamp_TileMode, SkShader::kClamp_TileMode); shader->setLocalMatrix(matrix); SkSafeUnref(paint.mPaint.setShader(shader)); if (aSurfOptions.mFilter != FILTER_LINEAR) { paint.mPaint.setFilterBitmap(false); } mCanvas->drawRect(destRect, paint.mPaint); }
virtual void onDraw(SkCanvas* canvas) { SkPoint pts[2] = { { 0, 0 }, { SkIntToScalar(100), SkIntToScalar(100) } }; SkShader::TileMode tm = SkShader::kClamp_TileMode; SkRect r = { 0, 0, SkIntToScalar(100), SkIntToScalar(100) }; SkPaint paint; paint.setAntiAlias(true); canvas->translate(SkIntToScalar(20), SkIntToScalar(20)); for (size_t i = 0; i < SK_ARRAY_COUNT(gGradData); i++) { canvas->save(); for (size_t j = 0; j < SK_ARRAY_COUNT(gGradMakers); j++) { SkShader* shader = gGradMakers[j](pts, gGradData[i], tm, NULL); // apply an increasing y perspective as we move to the right SkMatrix perspective; perspective.setIdentity(); perspective.setPerspY(SkScalarDiv(SkIntToScalar((unsigned) i+1), SkIntToScalar(500))); perspective.setSkewX(SkScalarDiv(SkIntToScalar((unsigned) i+1), SkIntToScalar(10))); shader->setLocalMatrix(perspective); paint.setShader(shader); canvas->drawRect(r, paint); shader->unref(); canvas->translate(0, SkIntToScalar(120)); } canvas->restore(); canvas->translate(SkIntToScalar(120), 0); } }
/* * When a function want to use the paint associated with a canvas to * draw, it should call _prepare_paint() can make the paint ready. * And, call _finish_paint() when the paint is no more used. */ static void _prepare_paint(mbe_t *mbe, SkPaint::Style style) { SkPaint *paint = mbe->paint; mbe_pattern_t *ptn = mbe->states->ptn; SkShader *shader; co_aix matrix[6]; SkMatrix skmatrix; paint->setStyle(style); if(ptn != NULL) { /* Local matrix of SkShader is a mapping from source pattern to * user space. Unlikely, for Cairo is a mapping from user space * to source pattern. */ shader = ptn->shader; matrix_mul(mbe->states->matrix, ptn->matrix, matrix); MB_MATRIX_2_SKMATRIX(skmatrix, matrix); shader->setLocalMatrix(skmatrix); paint->setShader(shader); } if(style == SkPaint::kStroke_Style) paint->setStrokeWidth(CO_AIX_2_SKSCALAR(mbe->states->line_width)); if(ptn != NULL && ptn->has_size) _prepare_sized_pattern(mbe, ptn); }
ShaderView() { SkImageDecoder::DecodeFile("/cover.png", &fBitmap); SkPoint pts[2]; SkColor colors[2]; pts[0].set(0, 0); pts[1].set(SkIntToScalar(100), 0); colors[0] = SK_ColorRED; colors[1] = SK_ColorBLUE; SkShader* shaderA = SkGradientShader::CreateLinear(pts, colors, NULL, 2, SkShader::kClamp_TileMode); pts[0].set(0, 0); pts[1].set(0, SkIntToScalar(100)); colors[0] = SK_ColorBLACK; colors[1] = SkColorSetARGB(0x80, 0, 0, 0); SkShader* shaderB = SkGradientShader::CreateLinear(pts, colors, NULL, 2, SkShader::kClamp_TileMode); SkXfermode* mode = SkPorterDuff::CreateXfermode(SkPorterDuff::kDstIn_Mode); fShader = new SkComposeShader(shaderA, shaderB, mode); shaderA->unref(); shaderB->unref(); mode->unref(); }
static cairo_status_t _cairo_skia_context_set_source (void *abstract_cr, cairo_pattern_t *source) { cairo_skia_context_t *cr = (cairo_skia_context_t *) abstract_cr; SkColor color; if (cr->source != NULL) { if (cr->source_image != NULL) { _cairo_surface_release_source_image (cr->source, cr->source_image, cr->source_extra); cr->source_image = NULL; } cairo_surface_destroy (cr->source); cr->source = NULL; } if (pattern_to_sk_color (source, color)) { cr->paint->setColor (color); } else { SkShader *shader = source_to_sk_shader (cr, source); if (shader == NULL) { UNSUPPORTED; return CAIRO_STATUS_SUCCESS; } cr->paint->setShader (shader); shader->unref (); cr->paint->setFilterBitmap (pattern_filter_to_sk (source)); } /* XXX change notification */ return CAIRO_STATUS_SUCCESS; }
bool SkPaint2GrPaint(GrContext* context, GrRenderTarget* rt, const SkPaint& skPaint, const SkMatrix& viewM, bool constantColor, GrPaint* grPaint) { SkShader* shader = skPaint.getShader(); if (NULL == shader) { return SkPaint2GrPaintNoShader(context, rt, skPaint, SkColor2GrColor(skPaint.getColor()), constantColor, grPaint); } GrColor paintColor = SkColor2GrColor(skPaint.getColor()); // Start a new block here in order to preserve our context state after calling // asFragmentProcessor(). Since these calls get passed back to the client, we don't really // want them messing around with the context. { // Allow the shader to modify paintColor and also create an effect to be installed as // the first color effect on the GrPaint. GrFragmentProcessor* fp = NULL; if (!shader->asFragmentProcessor(context, skPaint, viewM, NULL, &paintColor, grPaint->getProcessorDataManager(), &fp)) { return false; } if (fp) { grPaint->addColorProcessor(fp)->unref(); constantColor = false; } } // The grcolor is automatically set when calling asFragmentProcessor. // If the shader can be seen as an effect it returns true and adds its effect to the grpaint. return SkPaint2GrPaintNoShader(context, rt, skPaint, paintColor, constantColor, grPaint); }
static void drawmarshmallow(SkCanvas* canvas) { SkBitmap bitmap; SkPaint paint; SkRect r; SkMatrix m; SkImageDecoder::DecodeFile("/Users/reed/Downloads/3elfs.jpg", &bitmap); if (!bitmap.pixelRef()) { return; } 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); }
GrFragmentProcessor* GrPerlinNoiseEffect::TestCreate(SkRandom* random, GrContext* context, const GrCaps&, GrTexture**) { int numOctaves = random->nextRangeU(2, 10); bool stitchTiles = random->nextBool(); SkScalar seed = SkIntToScalar(random->nextU()); SkISize tileSize = SkISize::Make(random->nextRangeU(4, 4096), random->nextRangeU(4, 4096)); SkScalar baseFrequencyX = random->nextRangeScalar(0.01f, 0.99f); SkScalar baseFrequencyY = random->nextRangeScalar(0.01f, 0.99f); SkShader* shader = random->nextBool() ? SkPerlinNoiseShader::CreateFractalNoise(baseFrequencyX, baseFrequencyY, numOctaves, seed, stitchTiles ? &tileSize : NULL) : SkPerlinNoiseShader::CreateTurbulence(baseFrequencyX, baseFrequencyY, numOctaves, seed, stitchTiles ? &tileSize : NULL); SkPaint paint; GrColor paintColor; GrFragmentProcessor* effect; SkAssertResult(shader->asFragmentProcessor(context, paint, GrTest::TestMatrix(random), NULL, &paintColor, &effect)); SkDELETE(shader); return effect; }
// Set a bitmap shader that mimics dashing by width-on, width-off. // Returns false if it could not succeed (e.g. there was an existing shader) static bool setBitmapDash(SkPaint* paint, int width) { if (width <= 0 || paint->getShader()) return false; SkColor c = paint->getColor(); SkBitmap bm; bm.setConfig(SkBitmap::kARGB_8888_Config, 2, 1); bm.allocPixels(); bm.lockPixels(); // set the ON pixel *bm.getAddr32(0, 0) = SkPreMultiplyARGB(0xFF, SkColorGetR(c), SkColorGetG(c), SkColorGetB(c)); // set the OFF pixel *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(); return true; }
int sk_test_linear_gradient(caskbench_context_t *ctx) { int w = ctx->canvas_width; int h = ctx->canvas_height; int stops = 10; SkPoint pts[2]; pts[0].iset(0, 0); pts[1].iset(100, 100); SkColor colors[10]; SkScalar pos[10]; for (int i = 0; i < stops; i++) { pos[i] = i / SkIntToScalar(stops - 1); colors[i] = skiaRandomColor(); } SkShader *shader = SkGradientShader::CreateLinear(pts, colors, pos, stops, SkShader::kClamp_TileMode); ctx->skia_paint->setShader(shader); shapes_t shape; shape_copy(&ctx->shape_defaults, &shape); for (int i=0; i<ctx->size; i++) { double x1 = (double)rnd()/RAND_MAX * w; double x2 = (double)rnd()/RAND_MAX * w; double y1 = (double)rnd()/RAND_MAX * h; double y2 = (double)rnd()/RAND_MAX * h; double xx = MIN(x1, x2); double yy = MIN(x1, x2); double ww = abs(x2 - x1); double hh = abs(y2 - y1); ctx->skia_canvas->save(); ctx->skia_canvas->translate(SkDoubleToScalar(xx), SkDoubleToScalar(yy)); ctx->skia_canvas->scale(SkDoubleToScalar(ww/100), SkDoubleToScalar(hh/100)); // transform(shape.width/100, 0, 0, shape.height/100, 0, 0) shape.x = 0; shape.y = 0; shape.width = 100; shape.height = 100; shape.fill_type = CB_FILL_LINEAR_GRADIENT; ctx->skia_paint->setStyle(SkPaint::kFill_Style); ctx->skia_paint->setShader(shader); skiaDrawRectangle(ctx, &shape); ctx->skia_canvas->restore(); } if (shader) shader->unref(); return 1; }
virtual void onDraw(SkCanvas* canvas) { SkMatrix m; m.reset(); m.setRotate(33 * SK_Scalar1); m.postScale(3000 * SK_Scalar1, 3000 * SK_Scalar1); m.postTranslate(6000 * SK_Scalar1, -5000 * SK_Scalar1); canvas->concat(m); SkPaint paint; paint.setColor(SK_ColorRED); paint.setAntiAlias(true); bool success = m.invert(&m); SkASSERT(success); (void) success; // silence compiler :( SkPath path; SkPoint pt = {10 * SK_Scalar1, 10 * SK_Scalar1}; SkScalar small = 1 / (500 * SK_Scalar1); m.mapPoints(&pt, 1); path.addCircle(pt.fX, pt.fY, small); canvas->drawPath(path, paint); pt.set(30 * SK_Scalar1, 10 * SK_Scalar1); m.mapPoints(&pt, 1); SkRect rect = {pt.fX - small, pt.fY - small, pt.fX + small, pt.fY + small}; canvas->drawRect(rect, paint); SkBitmap bmp; bmp.setConfig(SkBitmap::kARGB_8888_Config, 2, 2); bmp.allocPixels(); bmp.lockPixels(); uint32_t* pixels = reinterpret_cast<uint32_t*>(bmp.getPixels()); pixels[0] = SkPackARGB32(0xFF, 0xFF, 0x00, 0x00); pixels[1] = SkPackARGB32(0xFF, 0x00, 0xFF, 0x00); pixels[2] = SkPackARGB32(0x80, 0x00, 0x00, 0x00); pixels[3] = SkPackARGB32(0xFF, 0x00, 0x00, 0xFF); bmp.unlockPixels(); pt.set(30 * SK_Scalar1, 30 * SK_Scalar1); m.mapPoints(&pt, 1); SkShader* shader = SkShader::CreateBitmapShader( bmp, SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode); SkMatrix s; s.reset(); s.setScale(SK_Scalar1 / 1000, SK_Scalar1 / 1000); shader->setLocalMatrix(s); paint.setShader(shader)->unref(); paint.setAntiAlias(false); paint.setFilterLevel(SkPaint::kLow_FilterLevel); rect.setLTRB(pt.fX - small, pt.fY - small, pt.fX + small, pt.fY + small); canvas->drawRect(rect, paint); }
static void paintButtonLike(RenderTheme* theme, RenderObject* o, const RenderObject::PaintInfo& i, const IntRect& rect) { SkCanvas* const canvas = i.context->platformContext()->canvas(); SkPaint paint; SkRect skrect; const int right = rect.x() + rect.width(); const int bottom = rect.y() + rect.height(); SkColor baseColor = SkColorSetARGB(0xff, 0xdd, 0xdd, 0xdd); if (o->style()->hasBackground()) baseColor = o->style()->backgroundColor().rgb(); double h, s, l; Color(baseColor).getHSL(h, s, l); // Our standard gradient is from 0xdd to 0xf8. This is the amount of // increased luminance between those values. SkColor lightColor(brightenColor(h, s, l, 0.105)); // If the button is too small, fallback to drawing a single, solid color if (rect.width() < 5 || rect.height() < 5) { paint.setColor(baseColor); skrect.set(rect.x(), rect.y(), right, bottom); canvas->drawRect(skrect, paint); return; } const int borderAlpha = theme->isHovered(o) ? 0x80 : 0x55; paint.setARGB(borderAlpha, 0, 0, 0); canvas->drawLine(rect.x() + 1, rect.y(), right - 1, rect.y(), paint); canvas->drawLine(right - 1, rect.y() + 1, right - 1, bottom - 1, paint); canvas->drawLine(rect.x() + 1, bottom - 1, right - 1, bottom - 1, paint); canvas->drawLine(rect.x(), rect.y() + 1, rect.x(), bottom - 1, paint); paint.setARGB(0xff, 0, 0, 0); SkPoint p[2]; const int lightEnd = theme->isPressed(o) ? 1 : 0; const int darkEnd = !lightEnd; p[lightEnd].set(SkIntToScalar(rect.x()), SkIntToScalar(rect.y())); p[darkEnd].set(SkIntToScalar(rect.x()), SkIntToScalar(bottom - 1)); SkColor colors[2]; colors[0] = lightColor; colors[1] = baseColor; SkShader* shader = SkGradientShader::CreateLinear( p, colors, NULL, 2, SkShader::kClamp_TileMode, NULL); paint.setStyle(SkPaint::kFill_Style); paint.setShader(shader); shader->unref(); skrect.set(rect.x() + 1, rect.y() + 1, right - 1, bottom - 1); canvas->drawRect(skrect, paint); paint.setShader(NULL); paint.setColor(brightenColor(h, s, l, -0.0588)); canvas->drawPoint(rect.x() + 1, rect.y() + 1, paint); canvas->drawPoint(right - 2, rect.y() + 1, paint); canvas->drawPoint(rect.x() + 1, bottom - 2, paint); canvas->drawPoint(right - 2, bottom - 2, paint); }
void GraphicsContext::setPlatformFillPattern(Pattern* pattern) { if (paintingDisabled()) return; SkShader* pat = pattern->createPlatformPattern(getCTM()); platformContext()->setFillShader(pat); pat->safeUnref(); }
void addBitmapFromPaint(const SkPaint& paint) { SkShader* shader = paint.getShader(); if (shader) { SkBitmap bm; if (shader->asABitmap(&bm, NULL, NULL)) { fPRSet->add(bm.pixelRef()); } } }
void SkARGB32_Shader_Blitter::blitMask(const SkMask& mask, const SkIRect& clip) { // we only handle kA8 with an xfermode if (fXfermode && (SkMask::kA8_Format != mask.fFormat)) { this->INHERITED::blitMask(mask, clip); return; } SkASSERT(mask.fBounds.contains(clip)); SkBlitMask::RowProc proc = NULL; if (!fXfermode) { unsigned flags = 0; if (fShader->getFlags() & SkShader::kOpaqueAlpha_Flag) { flags |= SkBlitMask::kSrcIsOpaque_RowFlag; } proc = SkBlitMask::RowFactory(SkBitmap::kARGB_8888_Config, mask.fFormat, (SkBlitMask::RowFlags)flags); if (NULL == proc) { this->INHERITED::blitMask(mask, clip); return; } } const int x = clip.fLeft; const int width = clip.width(); int y = clip.fTop; int height = clip.height(); char* dstRow = (char*)fDevice.getAddr32(x, y); const size_t dstRB = fDevice.rowBytes(); const uint8_t* maskRow = (const uint8_t*)mask.getAddr(x, y); const size_t maskRB = mask.fRowBytes; SkShader* shader = fShader; SkPMColor* span = fBuffer; if (fXfermode) { SkASSERT(SkMask::kA8_Format == mask.fFormat); SkXfermode* xfer = fXfermode; do { shader->shadeSpan(x, y, span, width); xfer->xfer32((SkPMColor*)dstRow, span, width, maskRow); dstRow += dstRB; maskRow += maskRB; y += 1; } while (--height > 0); } else { do { shader->shadeSpan(x, y, span, width); proc(dstRow, maskRow, span, width); dstRow += dstRB; maskRow += maskRB; y += 1; } while (--height > 0); } }
void Image::drawPattern(GraphicsContext* ctxt, const FloatRect& tileRect, const TransformationMatrix& patternTransform, const FloatPoint& phase, CompositeOperator compositeOp, const FloatRect& destRect) { SkBitmapRef* image = this->nativeImageForCurrentFrame(); if (!image) { // If it's too early we won't have an image yet. return; } // in case we get called with an incomplete bitmap const SkBitmap& bitmap = image->bitmap(); if (bitmap.getPixels() == NULL && bitmap.pixelRef() == NULL) { return; } SkRect dstR; android_setrect(&dstR, destRect); if (dstR.isEmpty()) { return; } SkCanvas* canvas = ctxt->platformContext()->mCanvas; SkPaint paint; SkShader* shader = SkShader::CreateBitmapShader(bitmap, SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode); paint.setShader(shader)->unref(); // now paint is the only owner of shader paint.setPorterDuffXfermode(android_convert_compositeOp(compositeOp)); paint.setFilterBitmap(true); SkMatrix matrix(patternTransform); float scaleX = (float)image->origWidth() / bitmap.width(); float scaleY = (float)image->origHeight() / bitmap.height(); matrix.preScale(SkFloatToScalar(scaleX), SkFloatToScalar(scaleY)); matrix.postTranslate(SkFloatToScalar(phase.x()), SkFloatToScalar(phase.y())); shader->setLocalMatrix(matrix); canvas->drawRect(dstR, paint); #ifdef TRACE_SUBSAMPLED_BITMAPS if (bitmap.width() != image->origWidth() || bitmap.height() != image->origHeight()) { SkDebugf("--- Image::drawPattern [%d %d] orig [%d %d] dst [%g %g]\n", bitmap.width(), bitmap.height(), image->origWidth(), image->origHeight(), SkScalarToFloat(dstR.width()), SkScalarToFloat(dstR.height())); } #endif }
static cairo_int_status_t _cairo_skia_surface_paint (void *asurface, cairo_operator_t op, const cairo_pattern_t *source, cairo_clip_t *clip) { cairo_skia_surface_t *surface = (cairo_skia_surface_t *) asurface; cairo_image_surface_t *image = NULL; cairo_status_t status; void *image_extra; SkColor color; status = _cairo_surface_clipper_set_clip (&surface->clipper, clip); if (unlikely (status)) return (cairo_int_status_t) status; if (pattern_to_sk_color (source, color)) { surface->canvas->drawColor (color, operator_to_sk (op)); return CAIRO_INT_STATUS_SUCCESS; } SkMatrix bitmapMatrix; SkBitmap *bitmap = pattern_to_sk_bitmap (surface, source, &bitmapMatrix, &image, &image_extra); SkShader *shader = NULL; if (!bitmap) shader = pattern_to_sk_shader (surface, source, &image, &image_extra); if (!bitmap && !shader) return UNSUPPORTED("pattern to bitmap and shader conversion"); SkPaint paint; paint.setFilterBitmap (pattern_filter_to_sk (source)); paint.setXfermodeMode (operator_to_sk (op)); if (shader) { paint.setShader (shader); surface->canvas->drawPaint (paint); } else { surface->canvas->drawBitmapMatrix (*bitmap, bitmapMatrix, &paint); } if (bitmap) delete bitmap; if (shader) shader->unref (); if (image != NULL) { _cairo_surface_release_source_image (&surface->base, image, image_extra); } return CAIRO_INT_STATUS_SUCCESS; }
void make_bitmap() { fBitmap.allocN32Pixels(80, 80); SkCanvas canvas(fBitmap); canvas.clear(0x00000000); SkPaint paint; paint.setAntiAlias(true); SkShader* shader = MakeLinear(); paint.setShader(shader); SkRect r = { 0, 0, SkIntToScalar(80), SkIntToScalar(80) }; canvas.drawRect(r, paint); shader->unref(); }
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; }
SkGLDevice::TexCache* SkGLDevice::setupGLPaintShader(const SkPaint& paint) { SkGL::SetPaint(paint); SkShader* shader = paint.getShader(); if (NULL == shader) { return NULL; } if (!shader->setContext(this->accessBitmap(false), paint, this->matrix())) { return NULL; } SkBitmap bitmap; SkMatrix matrix; SkShader::TileMode tileModes[2]; if (!shader->asABitmap(&bitmap, &matrix, tileModes)) { return NULL; } bitmap.lockPixels(); if (!bitmap.readyToDraw()) { return NULL; } // see if we've already cached the bitmap from the shader SkPoint max; GLuint name; TexCache* cache = SkGLDevice::LockTexCache(bitmap, &name, &max); // the lock has already called glBindTexture for us SkGL::SetTexParams(paint.isFilterBitmap(), tileModes[0], tileModes[1]); // since our texture coords will be in local space, we wack the texture // matrix to map them back into 0...1 before we load it SkMatrix localM; if (shader->getLocalMatrix(&localM)) { SkMatrix inverse; if (localM.invert(&inverse)) { matrix.preConcat(inverse); } } matrix.postScale(max.fX / bitmap.width(), max.fY / bitmap.height()); glMatrixMode(GL_TEXTURE); SkGL::LoadMatrix(matrix); glMatrixMode(GL_MODELVIEW); // since we're going to use a shader/texture, we don't want the color, // just its alpha SkGL::SetAlpha(paint.getAlpha()); // report that we have setup the texture return cache; }
void addBitmapFromPaint(const SkPaint& paint) { SkShader* shader = paint.getShader(); if (shader) { SkBitmap bm; // Check whether the shader is a gradient in order to short-circuit // call to asABitmap to prevent generation of bitmaps from // gradient shaders, which implement asABitmap. if (SkShader::kNone_GradientType == shader->asAGradient(NULL) && shader->asABitmap(&bm, NULL, NULL)) { fPRSet->add(bm.pixelRef()); } } }
static cairo_int_status_t _cairo_skia_surface_fill (void *asurface, cairo_operator_t op, const cairo_pattern_t *source, cairo_path_fixed_t *path, cairo_fill_rule_t fill_rule, double tolerance, cairo_antialias_t antialias, cairo_clip_t *clip) { cairo_skia_surface_t *surface = (cairo_skia_surface_t *) asurface; cairo_image_surface_t *image = NULL; cairo_status_t status; void *image_extra; status = _cairo_surface_clipper_set_clip (&surface->clipper, clip); if (unlikely (status)) return (cairo_int_status_t) status; SkPaint paint; paint.setStyle (SkPaint::kFill_Style); SkColor color; if (pattern_to_sk_color (source, color)) { paint.setColor (color); } else { SkShader *shader = pattern_to_sk_shader (surface, source, &image, &image_extra); if (shader == NULL) return UNSUPPORTED("pattern to shader conversion"); paint.setShader (shader); shader->unref (); paint.setFilterBitmap (pattern_filter_to_sk (source)); } paint.setXfermodeMode (operator_to_sk (op)); paint.setAntiAlias (antialias != CAIRO_ANTIALIAS_NONE); surface->canvas->drawPath (path_to_sk (path, fill_rule), paint); if (image != NULL) { _cairo_surface_release_source_image (&surface->base, image, image_extra); } return CAIRO_INT_STATUS_SUCCESS; }
static void copybits(SkCanvas* canvas, const SkBitmap& bm, const SkRect& dst, const SkPaint& paint) { SkRect src; SkMatrix matrix; src.set(0, 0, SkIntToScalar(bm.width()), SkIntToScalar(bm.height())); if (matrix.setRectToRect(src, dst)) { SkPaint p(paint); SkShader* shader = SkShader::CreateBitmapShader(bm, false, SkPaint::kNo_FilterType, SkShader::kClamp_TileMode); p.setShader(shader)->unref(); shader->setLocalMatrix(matrix); canvas->drawRect(dst, p); } }