virtual void onDrawContent(SkCanvas* canvas) { SkPaint paint; paint.setDither(true); paint.setFilterBitmap(true); for (size_t i = 0; i < SK_ARRAY_COUNT(fRecs); i++) { canvas->save(); paint.setShader(NULL); canvas->drawVertices(fRecs[i].fMode, fRecs[i].fCount, fRecs[i].fVerts, fRecs[i].fTexs, NULL, NULL, NULL, 0, paint); canvas->translate(SkIntToScalar(250), 0); paint.setShader(fShader0); canvas->drawVertices(fRecs[i].fMode, fRecs[i].fCount, fRecs[i].fVerts, fRecs[i].fTexs, NULL, NULL, NULL, 0, paint); canvas->translate(SkIntToScalar(250), 0); paint.setShader(fShader1); canvas->drawVertices(fRecs[i].fMode, fRecs[i].fCount, fRecs[i].fVerts, fRecs[i].fTexs, NULL, NULL, NULL, 0, paint); canvas->restore(); canvas->translate(0, SkIntToScalar(250)); } }
virtual void onDraw(SkCanvas* canvas) { this->drawBG(canvas); // canvas->scale(1.5f, 1.5f); canvas->drawBitmap(fBM, 0, 0); SkIRect margins; SkRect dst; int d = 25; margins.set(d, d, d, d); margins.fLeft = fBM.width()/2 - 1; margins.fTop = fBM.height()/2 - 1; margins.fRight = fBM.width() - margins.fLeft - 1; margins.fBottom = fBM.height() - margins.fTop - 1; // canvas->translate(fX/5, fY/5); canvas->translate(0, 76); dst.set(0, 0, SkIntToScalar(200), SkIntToScalar(200)); SkPaint paint; paint.setAntiAlias(false); paint.setDither(true); paint.setFilterBitmap(false); // SkNinePatch::DrawNine(canvas, dst, fBM, margins, &paint); test_rects(canvas, fBM, &paint); }
static void paintSkBitmap(PlatformContextSkia* platformContext, const NativeImageSkia& bitmap, const SkIRect& srcRect, const SkRect& destRect, const SkXfermode::Mode& compOp) { SkPaint paint; paint.setXfermodeMode(compOp); paint.setFilterBitmap(true); int alpha = roundf(platformContext->getAlpha() * 256); if (alpha > 255) alpha = 255; else if (alpha < 0) alpha = 0; paint.setAlpha(alpha); skia::PlatformCanvas* canvas = platformContext->canvas(); ResamplingMode resampling = platformContext->isPrinting() ? RESAMPLE_NONE : computeResamplingMode(bitmap, srcRect.width(), srcRect.height(), SkScalarToFloat(destRect.width()), SkScalarToFloat(destRect.height())); if (resampling == RESAMPLE_AWESOME) { drawResampledBitmap(*canvas, paint, bitmap, srcRect, destRect); } else { // No resampling necessary, we can just draw the bitmap. We want to // filter it if we decided to do linear interpolation above, or if there // is something interesting going on with the matrix (like a rotation). // Note: for serialization, we will want to subset the bitmap first so // we don't send extra pixels. canvas->drawBitmapRect(bitmap, &srcRect, destRect, &paint); } }
static void draw_image(SkCanvas* canvas, bool showGL, int flags) { SkPaint paint; paint.setAntiAlias(true); paint.setFilterBitmap(true); setFade(&paint, showGL); static SkBitmap* gBM; if (NULL == gBM) { gBM = new SkBitmap; SkImageDecoder::DecodeFile("/skimages/startrek.png", gBM); } SkRect r = SkRect::MakeWH(gBM->width(), gBM->height()); canvas->save(); canvas->translate(30, 30); canvas->scale(0.8f, 0.8f); canvas->drawBitmap(*gBM, 0, 0, &paint); if (showGL) { show_mesh(canvas, r); } canvas->restore(); canvas->translate(210, 290); canvas->rotate(-35); canvas->drawBitmap(*gBM, 0, 0, &paint); if (showGL) { show_mesh(canvas, r); } }
void ImageView::OnPaint(gfx::Canvas* canvas) { View::OnPaint(canvas); if(image_.empty()) { return; } gfx::Rect image_bounds(GetImageBounds()); if(image_bounds.IsEmpty()) { return; } if(image_bounds.size() != gfx::Size(image_.width(), image_.height())) { // Resize case image_.buildMipMap(false); SkPaint paint; paint.setFilterBitmap(true); canvas->DrawBitmapInt(image_, 0, 0, image_.width(), image_.height(), image_bounds.x(), image_bounds.y(), image_bounds.width(), image_bounds.height(), true, paint); } else { canvas->DrawBitmapInt(image_, image_bounds.x(), image_bounds.y()); } }
virtual void onDraw(SkCanvas* canvas) { SkBitmap bm; SkIRect center; make_bitmap(&bm, NULL /*SampleCode::GetGr()*/, ¢er); // amount of bm that should not be stretched (unless we have to) const SkScalar fixed = SkIntToScalar(bm.width() - center.width()); const SkTSize<SkScalar> size[] = { { fixed * 4 / 5, fixed * 4 / 5 }, // shrink in both axes { fixed * 4 / 5, fixed * 4 }, // shrink in X { fixed * 4, fixed * 4 / 5 }, // shrink in Y { fixed * 4, fixed * 4 } }; canvas->drawBitmap(bm, SkIntToScalar(10), SkIntToScalar(10), NULL); SkScalar x = SkIntToScalar(100); SkScalar y = SkIntToScalar(100); SkPaint paint; paint.setFilterBitmap(true); for (int iy = 0; iy < 2; ++iy) { for (int ix = 0; ix < 2; ++ix) { int i = ix * 2 + iy; SkRect r = SkRect::MakeXYWH(x + ix * fixed, y + iy * fixed, size[i].width(), size[i].height()); canvas->drawBitmapNine(bm, center, r, &paint); } } }
static void test_patch(SkCanvas* canvas, const SkBitmap& bm, SkScalar scale) { SkCubicBoundary cubic; set_cubic(cubic.fPts + 0, 0, 0, 100, 0, scale); set_cubic(cubic.fPts + 3, 100, 0, 100, 100, scale); set_cubic(cubic.fPts + 6, 100, 100, 0, 100, -scale); set_cubic(cubic.fPts + 9, 0, 100, 0, 0, 0); SkBoundaryPatch patch; patch.setBoundary(&cubic); const int Rows = 16; const int Cols = 16; SkPoint pts[Rows * Cols]; patch.evalPatch(pts, Rows, Cols); SkPaint paint; paint.setAntiAlias(true); paint.setFilterBitmap(true); paint.setStrokeWidth(1); paint.setStrokeCap(SkPaint::kRound_Cap); canvas->translate(50, 50); canvas->scale(3, 3); SkMeshUtils::Draw(canvas, bm, Rows, Cols, pts, NULL, paint); }
static void paintSkBitmap(PlatformContextSkia* platformContext, const NativeImageSkia& bitmap, const SkIRect& srcRect, const SkRect& destRect, const SkXfermode::Mode& compOp) { SkPaint paint; paint.setXfermodeMode(compOp); paint.setFilterBitmap(true); paint.setAlpha(platformContext->getNormalizedAlpha()); paint.setLooper(platformContext->getDrawLooper()); // only antialias if we're rotated or skewed paint.setAntiAlias(hasNon90rotation(platformContext)); SkCanvas* canvas = platformContext->canvas(); ResamplingMode resampling; if (platformContext->isAccelerated()) resampling = RESAMPLE_LINEAR; else resampling = platformContext->printing() ? RESAMPLE_NONE : computeResamplingMode(platformContext, bitmap, srcRect.width(), srcRect.height(), SkScalarToFloat(destRect.width()), SkScalarToFloat(destRect.height())); if (resampling == RESAMPLE_AWESOME) { drawResampledBitmap(*canvas, paint, bitmap, srcRect, destRect); } else { // No resampling necessary, we can just draw the bitmap. We want to // filter it if we decided to do linear interpolation above, or if there // is something interesting going on with the matrix (like a rotation). // Note: for serialization, we will want to subset the bitmap first so // we don't send extra pixels. canvas->drawBitmapRect(bitmap.bitmap(), &srcRect, destRect, &paint); } }
virtual void onDrawContent(SkCanvas* canvas) { SkIRect srcRect; SkRect dstRect; SkPaint paint; paint.setFilterBitmap(true); // Test that bitmap draws from malloc-backed bitmaps respect // the constrained texture domain. srcRect.setXYWH(1, 1, 3, 3); dstRect.setXYWH(5.0f, 5.0f, 305.0f, 305.0f); canvas->drawBitmapRect(fBM, &srcRect, dstRect, &paint); // Test that bitmap draws across separate devices also respect // the constrainted texture domain. // Note: GPU-backed bitmaps follow a different rendering path // when copying from one GPU device to another. SkAutoTUnref<SkDevice> secondDevice(canvas->createCompatibleDevice( SkBitmap::kARGB_8888_Config, 5, 5, true)); SkCanvas secondCanvas(secondDevice.get()); srcRect.setXYWH(1, 1, 3, 3); dstRect.setXYWH(1.0f, 1.0f, 3.0f, 3.0f); secondCanvas.drawBitmapRect(fBM, &srcRect, dstRect, &paint); SkBitmap deviceBitmap = secondDevice->accessBitmap(false); srcRect.setXYWH(1, 1, 3, 3); dstRect.setXYWH(405.0f, 5.0f, 305.0f, 305.0f); canvas->drawBitmapRect(deviceBitmap, &srcRect, dstRect, &paint); // Test that bitmap blurring using a subrect // renders correctly srcRect.setXYWH(1, 1, 3, 3); dstRect.setXYWH(5.0f, 405.0f, 305.0f, 305.0f); SkMaskFilter* mf = SkBlurMaskFilter::Create( 5, SkBlurMaskFilter::kNormal_BlurStyle, SkBlurMaskFilter::kHighQuality_BlurFlag | SkBlurMaskFilter::kIgnoreTransform_BlurFlag); paint.setMaskFilter(mf)->unref(); canvas->drawBitmapRect(deviceBitmap, &srcRect, dstRect, &paint); // Blur and a rotation + NULL src rect // This should not trigger the texture domain code // but it will test a code path in SkGpuDevice::drawBitmap // that handles blurs with rects transformed to non- // orthogonal rects. It also tests the NULL src rect handling mf = SkBlurMaskFilter::Create( 5, SkBlurMaskFilter::kNormal_BlurStyle, SkBlurMaskFilter::kHighQuality_BlurFlag); paint.setMaskFilter(mf)->unref(); dstRect.setXYWH(-150.0f, -150.0f, 300.0f, 300.0f); canvas->translate(550, 550); canvas->rotate(45); canvas->drawBitmapRect(fBM, NULL, dstRect, &paint); }
void TransparencyWin::compositeOpaqueComposite() { if (!m_validLayer) return; SkCanvas* destCanvas = canvasForContext(*m_destContext); destCanvas->save(); SkBitmap* bitmap = const_cast<SkBitmap*>( &bitmapForContext(*m_layerBuffer->context())); // This function will be called for WhiteLayer as well, which we don't want // to change. if (m_layerMode == OpaqueCompositeLayer) { // Fix up our bitmap, making it contain only the pixels which changed // and transparent everywhere else. SkAutoLockPixels sourceLock(*m_referenceBitmap); SkAutoLockPixels lock(*bitmap); for (int y = 0; y < bitmap->height(); y++) { uint32_t* source = m_referenceBitmap->getAddr32(0, y); uint32_t* dest = bitmap->getAddr32(0, y); for (int x = 0; x < bitmap->width(); x++) { // Clear out any pixels that were untouched. if (dest[x] == source[x]) dest[x] = 0; else dest[x] |= (0xFF << SK_A32_SHIFT); } } } else makeLayerOpaque(); SkRect destRect; if (m_transformMode != Untransform) { // We want to use Untransformed space. // // Note that we DON'T call m_layerBuffer->image() here. This actually // makes a copy of the image, which is unnecessary and slow. Instead, we // just draw the image from inside the destination context. SkMatrix identity; identity.reset(); destCanvas->setMatrix(identity); destRect.set(m_transformedSourceRect.x(), m_transformedSourceRect.y(), m_transformedSourceRect.maxX(), m_transformedSourceRect.maxY()); } else destRect.set(m_sourceRect.x(), m_sourceRect.y(), m_sourceRect.maxX(), m_sourceRect.maxY()); SkPaint paint; paint.setFilterBitmap(true); paint.setAntiAlias(true); // Note that we need to specify the source layer subset, since the bitmap // may have been cached and it could be larger than what we're using. SkIRect sourceRect = { 0, 0, m_layerSize.width(), m_layerSize.height() }; destCanvas->drawBitmapRect(*bitmap, &sourceRect, destRect, &paint); destCanvas->restore(); }
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 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 }
void BitmapImage::draw(GraphicsContext* ctxt, const FloatRect& dstRect, const FloatRect& srcRect, CompositeOperator compositeOp) { startAnimation(); 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) { #ifdef TRACE_SKIPPED_BITMAPS SkDebugf("----- skip bitmapimage: [%d %d] pixels %p pixelref %p\n", bitmap.width(), bitmap.height(), bitmap.getPixels(), bitmap.pixelRef()); #endif return; } SkIRect srcR; SkRect dstR; float invScaleX = (float)bitmap.width() / image->origWidth(); float invScaleY = (float)bitmap.height() / image->origHeight(); android_setrect(&dstR, dstRect); android_setrect_scaled(&srcR, srcRect, invScaleX, invScaleY); if (srcR.isEmpty() || dstR.isEmpty()) { #ifdef TRACE_SKIPPED_BITMAPS SkDebugf("----- skip bitmapimage: [%d %d] src-empty %d dst-empty %d\n", bitmap.width(), bitmap.height(), srcR.isEmpty(), dstR.isEmpty()); #endif return; } SkCanvas* canvas = ctxt->platformContext()->mCanvas; SkPaint paint; paint.setFilterBitmap(true); paint.setPorterDuffXfermode(android_convert_compositeOp(compositeOp)); canvas->drawBitmapRect(bitmap, &srcR, dstR, &paint); #ifdef TRACE_SUBSAMPLED_BITMAPS if (bitmap.width() != image->origWidth() || bitmap.height() != image->origHeight()) { SkDebugf("--- BitmapImage::draw [%d %d] orig [%d %d]\n", bitmap.width(), bitmap.height(), image->origWidth(), image->origHeight()); } #endif }
virtual void onDrawContent(SkCanvas* canvas) { canvas->translate(SkIntToScalar(10), SkIntToScalar(50)); const SkScalar W = SkIntToScalar(fBitmaps[0].width() + 1); const SkScalar H = SkIntToScalar(fBitmaps[0].height() + 1); SkPaint paint; const SkScalar scale = SkFloatToScalar(0.897917f); canvas->scale(SK_Scalar1, scale); for (int k = 0; k < 2; k++) { paint.setFilterBitmap(k == 1); for (int j = 0; j < 2; j++) { paint.setDither(j == 1); for (int i = 0; i < fBitmapCount; i++) { SkScalar x = (k * fBitmapCount + j) * W; SkScalar y = i * H; x = SkIntToScalar(SkScalarRound(x)); y = SkIntToScalar(SkScalarRound(y)); canvas->drawBitmap(fBitmaps[i], x, y, &paint); if (i == 0) { SkPaint p; p.setAntiAlias(true); p.setTextAlign(SkPaint::kCenter_Align); p.setTextSize(SkIntToScalar(18)); SkString s("dither="); s.appendS32(paint.isDither()); s.append(" filter="); s.appendS32(paint.isFilterBitmap()); canvas->drawText(s.c_str(), s.size(), x + W/2, y - p.getTextSize(), p); } if (k+j == 2) { SkPaint p; p.setAntiAlias(true); p.setTextSize(SkIntToScalar(18)); SkString s; s.append(" depth="); s.appendS32(fBitmaps[i].config() == SkBitmap::kRGB_565_Config ? 16 : 32); canvas->drawText(s.c_str(), s.size(), x + W + SkIntToScalar(4), y + H/2, p); } } } } }
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 drawBitmap__BitmapFFPaint(JNIEnv* env, jobject jcanvas, SkCanvas* canvas, SkBitmap* bitmap, jfloat left, jfloat top, SkPaint* paint, jint canvasDensity, jint screenDensity, jint bitmapDensity) { SkScalar left_ = SkFloatToScalar(left); SkScalar top_ = SkFloatToScalar(top); if (canvasDensity == bitmapDensity || canvasDensity == 0 || bitmapDensity == 0) { if (screenDensity != 0 && screenDensity != bitmapDensity) { SkPaint filteredPaint; if (paint) { filteredPaint = *paint; } filteredPaint.setFilterBitmap(true); canvas->drawBitmap(*bitmap, left_, top_, &filteredPaint); } else { canvas->drawBitmap(*bitmap, left_, top_, paint); } } else { canvas->save(); SkScalar scale = SkFloatToScalar(canvasDensity / (float)bitmapDensity); canvas->translate(left_, top_); canvas->scale(scale, scale); SkPaint filteredPaint; if (paint) { filteredPaint = *paint; } filteredPaint.setFilterBitmap(true); canvas->drawBitmap(*bitmap, 0, 0, &filteredPaint); canvas->restore(); } }
static void paintSkBitmap(PlatformContextSkia* platformContext, const NativeImageSkia& bitmap, const SkIRect& srcRect, const SkRect& destRect, const SkXfermode::Mode& compOp) { #if PLATFORM(CHROMIUM) TRACE_EVENT0("skia", "paintSkBitmap"); #endif SkPaint paint; paint.setXfermodeMode(compOp); paint.setAlpha(platformContext->getNormalizedAlpha()); paint.setLooper(platformContext->getDrawLooper()); // only antialias if we're rotated or skewed paint.setAntiAlias(hasNon90rotation(platformContext)); SkCanvas* canvas = platformContext->canvas(); ResamplingMode resampling; if (platformContext->isAccelerated()) resampling = RESAMPLE_LINEAR; else if (platformContext->printing()) resampling = RESAMPLE_NONE; else { // Take into account scale applied to the canvas when computing sampling mode (e.g. CSS scale or page scale). SkRect destRectTarget = destRect; if (!(canvas->getTotalMatrix().getType() & (SkMatrix::kAffine_Mask | SkMatrix::kPerspective_Mask))) canvas->getTotalMatrix().mapRect(&destRectTarget, destRect); resampling = computeResamplingMode(canvas->getTotalMatrix(), bitmap, srcRect.width(), srcRect.height(), SkScalarToFloat(destRectTarget.width()), SkScalarToFloat(destRectTarget.height())); } if (resampling == RESAMPLE_NONE) { // FIXME: This is to not break tests (it results in the filter bitmap flag // being set to true). We need to decide if we respect RESAMPLE_NONE // being returned from computeResamplingMode. resampling = RESAMPLE_LINEAR; } resampling = limitResamplingMode(platformContext, resampling); paint.setFilterBitmap(resampling == RESAMPLE_LINEAR); if (resampling == RESAMPLE_AWESOME) drawResampledBitmap(*canvas, paint, bitmap, srcRect, destRect); else { // No resampling necessary, we can just draw the bitmap. We want to // filter it if we decided to do linear interpolation above, or if there // is something interesting going on with the matrix (like a rotation). // Note: for serialization, we will want to subset the bitmap first so // we don't send extra pixels. canvas->drawBitmapRect(bitmap.bitmap(), &srcRect, destRect, &paint); } platformContext->didDrawRect(destRect, paint, &bitmap.bitmap()); }
bool SkDownSampleImageFilter::onFilterImage(Proxy* proxy, const SkBitmap& src, const SkMatrix& matrix, SkBitmap* result, SkIPoint*) { SkScalar scale = fScale; if (scale > SK_Scalar1 || scale <= 0) { return false; } int dstW = SkScalarRoundToInt(src.width() * scale); int dstH = SkScalarRoundToInt(src.height() * scale); if (dstW < 1) { dstW = 1; } if (dstH < 1) { dstH = 1; } SkBitmap tmp; // downsample { SkDevice* dev = proxy->createDevice(dstW, dstH); if (NULL == dev) { return false; } OwnDeviceCanvas canvas(dev); SkPaint paint; paint.setFilterBitmap(true); canvas.scale(scale, scale); canvas.drawBitmap(src, 0, 0, &paint); tmp = dev->accessBitmap(false); } // upscale { SkDevice* dev = proxy->createDevice(src.width(), src.height()); if (NULL == dev) { return false; } OwnDeviceCanvas canvas(dev); SkRect r = SkRect::MakeWH(SkIntToScalar(src.width()), SkIntToScalar(src.height())); canvas.drawBitmapRect(tmp, NULL, r, NULL); *result = dev->accessBitmap(false); } return true; }
bool RenderThemeAndroid::paintProgressBar(RenderObject* o, const PaintInfo& i, const IntRect& rect) { if (!o->isProgress()) return true; RenderProgress* renderProgress = toRenderProgress(o); IntRect valueRect = progressValueRectFor(renderProgress, rect);//progressRect; SkPaint paint; SkRect dstRect; SkShader* m_shader = NULL; SkPoint pts[2]; SkColor colors[3]; SkScalar pos[] = { 0.0f, 0.5f, 1.0f}; SkCanvas* const canvas = i.context->platformContext()->getCanvas(); pts[0].fX = valueRect.x(); pts[0].fY = valueRect.y(); pts[1].fX = valueRect.x(); pts[1].fY = valueRect.y() + valueRect.height(); colors[0] = 0xFFC4EEA4; colors[1] = 0xFF3DC032; colors[2] = 0xFFC4EEA4; m_shader = SkGradientShader::CreateLinear(pts, colors, pos, 3, SkShader::kClamp_TileMode); // Paint the bar, the bar is valueRect dstRect.set(valueRect.x(), valueRect.y(), (valueRect.x() + valueRect.width()), (valueRect.y() + valueRect.height())); paint.setAntiAlias(true); paint.setFilterBitmap(true); paint.setShader(m_shader); canvas->drawRect(dstRect, paint); // Paint the border for progress bar, set the rect to complete progress bar rect dstRect.set(rect.x(), rect.y(), (rect.x() + rect.width()), (rect.y() + rect.height())); paint.setShader(NULL); paint.setColor(0x80000000); paint.setStyle(SkPaint::kStroke_Style); canvas->drawRect(dstRect, paint); if (m_shader) { m_shader->unref(); } return false; }
static void test_drag(SkCanvas* canvas, const SkBitmap& bm, const SkPoint& p0, const SkPoint& p1) { SkCubicBoundary cubic; set_cubic(cubic.fPts + 0, 0, 0, 100, 0, 0); set_cubic(cubic.fPts + 3, 100, 0, 100, 100, 0); set_cubic(cubic.fPts + 6, 100, 100, 0, 100, 0); set_cubic(cubic.fPts + 9, 0, 100, 0, 0, 0); #if 0 cubic.fPts[1] += p1 - p0; cubic.fPts[2] += p1 - p0; #else SkScalar dx = p1.fX - p0.fX; if (dx > 0) dx = 0; SkScalar dy = p1.fY - p0.fY; if (dy > 0) dy = 0; cubic.fPts[1].fY += dy; cubic.fPts[2].fY += dy; cubic.fPts[10].fX += dx; cubic.fPts[11].fX += dx; #endif SkBoundaryPatch patch; patch.setBoundary(&cubic); const int Rows = 16; const int Cols = 16; SkPoint pts[Rows * Cols]; patch.evalPatch(pts, Rows, Cols); SkPaint paint; paint.setAntiAlias(true); paint.setFilterBitmap(true); paint.setStrokeWidth(1); paint.setStrokeCap(SkPaint::kRound_Cap); canvas->translate(50, 50); canvas->scale(3, 3); SkAutoCanvasRestore acr(canvas, true); SkRect r = { 0, 0, 100, 100 }; canvas->clipRect(r); SkMeshUtils::Draw(canvas, bm, Rows, Cols, pts, NULL, paint); }
static void mesh_slide(SkCanvas* canvas) { Rec fRecs[3]; SkIPoint size; SkShader* fShader0 = make_shader0(&size); SkShader* fShader1 = make_shader1(size); SkAutoUnref aur0(fShader0); SkAutoUnref aur1(fShader1); make_strip(&fRecs[0], size.fX, size.fY); make_fan(&fRecs[1], size.fX, size.fY); make_tris(&fRecs[2]); SkPaint paint; paint.setDither(true); paint.setFilterBitmap(true); for (size_t i = 0; i < SK_ARRAY_COUNT(fRecs); i++) { canvas->save(); paint.setShader(NULL); canvas->drawVertices(fRecs[i].fMode, fRecs[i].fCount, fRecs[i].fVerts, fRecs[i].fTexs, NULL, NULL, NULL, 0, paint); canvas->translate(SkIntToScalar(210), 0); paint.setShader(fShader0); canvas->drawVertices(fRecs[i].fMode, fRecs[i].fCount, fRecs[i].fVerts, fRecs[i].fTexs, NULL, NULL, NULL, 0, paint); canvas->translate(SkIntToScalar(210), 0); paint.setShader(fShader1); canvas->drawVertices(fRecs[i].fMode, fRecs[i].fCount, fRecs[i].fVerts, fRecs[i].fTexs, NULL, NULL, NULL, 0, paint); canvas->restore(); canvas->translate(0, SkIntToScalar(250)); } }
virtual void onDraw(SkCanvas* canvas) { drawBG(canvas); SkBitmap sprite; sprite.setConfig(SkBitmap::kARGB_8888_Config, 4, 4, 4*sizeof(SkColor)); const SkColor spriteData[16] = { SK_ColorBLACK, SK_ColorCYAN, SK_ColorMAGENTA, SK_ColorYELLOW, SK_ColorBLACK, SK_ColorWHITE, SK_ColorBLACK, SK_ColorRED, SK_ColorGREEN, SK_ColorBLACK, SK_ColorWHITE, SK_ColorBLUE, SK_ColorYELLOW, SK_ColorMAGENTA, SK_ColorCYAN, SK_ColorBLACK }; sprite.allocPixels(); sprite.lockPixels(); SkPMColor* addr = sprite.getAddr32(0, 0); for (size_t i = 0; i < SK_ARRAY_COUNT(spriteData); ++i) { addr[i] = SkPreMultiplyColor(spriteData[i]); } sprite.unlockPixels(); // We draw a magnified subrect of the sprite // sample interpolation may cause color bleeding around edges // the subrect is a pure white area SkIRect srcRect; SkRect dstRect; SkPaint paint; paint.setFilterBitmap(true); //First row : full texture with and without filtering srcRect.setXYWH(0, 0, 4, 4); dstRect.setXYWH(SkIntToScalar(0), SkIntToScalar(0) , SkIntToScalar(100), SkIntToScalar(100)); canvas->drawBitmapRect(sprite, &srcRect, dstRect, &paint); dstRect.setXYWH(SkIntToScalar(100), SkIntToScalar(0) , SkIntToScalar(100), SkIntToScalar(100)); canvas->drawBitmapRect(sprite, &srcRect, dstRect); //Second row : sub rect of texture with and without filtering srcRect.setXYWH(1, 1, 2, 2); dstRect.setXYWH(SkIntToScalar(25), SkIntToScalar(125) , SkIntToScalar(50), SkIntToScalar(50)); canvas->drawBitmapRect(sprite, &srcRect, dstRect, &paint); dstRect.setXYWH(SkIntToScalar(125), SkIntToScalar(125) , SkIntToScalar(50), SkIntToScalar(50)); canvas->drawBitmapRect(sprite, &srcRect, dstRect); }
virtual void onDraw(SkCanvas* canvas) { canvas->drawColor(SK_ColorLTGRAY); // test_bigblur(canvas); return; canvas->concat(fMatrix); SkPaint paint; paint.setFilterBitmap(true); paint.setShader(SkShader::CreateBitmapShader(fBitmap, SkShader::kClamp_TileMode, SkShader::kClamp_TileMode))->unref(); fMesh.draw(canvas, paint); //return; paint.setShader(NULL); paint.setColor(SK_ColorRED); fMesh.draw(canvas, paint); // test_drag(canvas, fBitmap, fP0, fP1); }
virtual void onDraw(SkCanvas* canvas) { this->drawBG(canvas); #if 1 canvas->drawColor(SK_ColorWHITE); canvas->translate(SK_Scalar1/2, SkIntToScalar(15) + SK_Scalar1/2); canvas->scale(SkIntToScalar(3)/2, SkIntToScalar(3)/2); drawbug(canvas, fScale); fScale += SK_Scalar1/93; this->inval(NULL); return; #endif SkPaint paint; paint.setDither(true); paint.setFilterBitmap(true); for (int i = 0; i < SK_ARRAY_COUNT(fRecs); i++) { canvas->save(); paint.setShader(NULL); canvas->drawVertices(fRecs[i].fMode, fRecs[i].fCount, fRecs[i].fVerts, fRecs[i].fTexs, NULL, NULL, NULL, 0, paint); canvas->translate(SkIntToScalar(250), 0); paint.setShader(fShader0); canvas->drawVertices(fRecs[i].fMode, fRecs[i].fCount, fRecs[i].fVerts, fRecs[i].fTexs, NULL, NULL, NULL, 0, paint); canvas->translate(SkIntToScalar(250), 0); paint.setShader(fShader1); canvas->drawVertices(fRecs[i].fMode, fRecs[i].fCount, fRecs[i].fVerts, fRecs[i].fTexs, NULL, NULL, NULL, 0, paint); canvas->restore(); canvas->translate(0, SkIntToScalar(250)); } }
virtual void onDraw(SkCanvas* canvas) { this->drawBG(canvas); canvas->translate(SkIntToScalar(10), SkIntToScalar(10)); SkScalar x = 0, y = 0; for (size_t i = 0; i < SK_ARRAY_COUNT(gNames); i++) { canvas->drawBitmap(fBitmaps[i], x, y); x += SkIntToScalar(fBitmaps[i].width() + 10); } canvas->translate(0, SkIntToScalar(120)); SkPaint paint; paint.setShader(fShader); paint.setFilterBitmap(true); SkRect r = { 0, 0, SkIntToScalar(300), SkIntToScalar(100) }; canvas->drawRect(r, paint); }
static void doDrawBitmap(JNIEnv* env, SkCanvas* canvas, SkBitmap* bitmap, jobject srcIRect, const SkRect& dst, SkPaint* paint, jint screenDensity, jint bitmapDensity) { SkIRect src, *srcPtr = NULL; if (NULL != srcIRect) { GraphicsJNI::jrect_to_irect(env, srcIRect, &src); srcPtr = &src; } if (screenDensity != 0 && screenDensity != bitmapDensity) { SkPaint filteredPaint; if (paint) { filteredPaint = *paint; } filteredPaint.setFilterBitmap(true); canvas->drawBitmapRect(*bitmap, srcPtr, dst, &filteredPaint); } else { canvas->drawBitmapRect(*bitmap, srcPtr, dst, paint); } }
void test_patch(SkCanvas* canvas, const SkBitmap& bm, SkScalar scale) { const float w = bm.width(); const float h = bm.height(); SkCubicBoundary cubic; set_cubic(cubic.fPts + 0, 0, 0, w, 0, scale); set_cubic(cubic.fPts + 3, w, 0, w, h, scale); set_cubic(cubic.fPts + 6, w, h, 0, h, -scale); set_cubic(cubic.fPts + 9, 0, h, 0, 0, scale); SkBoundaryPatch patch; patch.setBoundary(&cubic); const int Rows = 16; const int Cols = 16; SkPoint pts[Rows * Cols]; patch.evalPatch(pts, Rows, Cols); SkPaint paint; paint.setAntiAlias(true); paint.setFilterBitmap(true); SkMeshUtils::Draw(canvas, bm, Rows, Cols, pts, NULL, paint); }
static void SetPaintPattern(SkPaint& aPaint, const Pattern& aPattern, TempBitmap& aTmpBitmap, Float aAlpha = 1.0) { switch (aPattern.GetType()) { case PatternType::COLOR: { Color color = static_cast<const ColorPattern&>(aPattern).mColor; aPaint.setColor(ColorToSkColor(color, aAlpha)); break; } case PatternType::LINEAR_GRADIENT: { const LinearGradientPattern& pat = static_cast<const LinearGradientPattern&>(aPattern); GradientStopsSkia *stops = static_cast<GradientStopsSkia*>(pat.mStops.get()); SkShader::TileMode mode = ExtendModeToTileMode(stops->mExtendMode); if (stops->mCount >= 2) { SkPoint points[2]; points[0] = SkPoint::Make(SkFloatToScalar(pat.mBegin.x), SkFloatToScalar(pat.mBegin.y)); points[1] = SkPoint::Make(SkFloatToScalar(pat.mEnd.x), SkFloatToScalar(pat.mEnd.y)); SkShader* shader = SkGradientShader::CreateLinear(points, &stops->mColors.front(), &stops->mPositions.front(), stops->mCount, mode); if (shader) { SkMatrix mat; GfxMatrixToSkiaMatrix(pat.mMatrix, mat); shader->setLocalMatrix(mat); SkSafeUnref(aPaint.setShader(shader)); } } else { aPaint.setColor(SkColorSetARGB(0, 0, 0, 0)); } break; } case PatternType::RADIAL_GRADIENT: { const RadialGradientPattern& pat = static_cast<const RadialGradientPattern&>(aPattern); GradientStopsSkia *stops = static_cast<GradientStopsSkia*>(pat.mStops.get()); SkShader::TileMode mode = ExtendModeToTileMode(stops->mExtendMode); if (stops->mCount >= 2) { SkPoint points[2]; points[0] = SkPoint::Make(SkFloatToScalar(pat.mCenter1.x), SkFloatToScalar(pat.mCenter1.y)); points[1] = SkPoint::Make(SkFloatToScalar(pat.mCenter2.x), SkFloatToScalar(pat.mCenter2.y)); SkShader* shader = SkGradientShader::CreateTwoPointConical(points[0], SkFloatToScalar(pat.mRadius1), points[1], SkFloatToScalar(pat.mRadius2), &stops->mColors.front(), &stops->mPositions.front(), stops->mCount, mode); if (shader) { SkMatrix mat; GfxMatrixToSkiaMatrix(pat.mMatrix, mat); shader->setLocalMatrix(mat); SkSafeUnref(aPaint.setShader(shader)); } } else { aPaint.setColor(SkColorSetARGB(0, 0, 0, 0)); } break; } case PatternType::SURFACE: { const SurfacePattern& pat = static_cast<const SurfacePattern&>(aPattern); aTmpBitmap = GetBitmapForSurface(pat.mSurface); const SkBitmap& bitmap = aTmpBitmap.mBitmap; SkShader::TileMode mode = ExtendModeToTileMode(pat.mExtendMode); SkShader* shader = SkShader::CreateBitmapShader(bitmap, mode, mode); SkMatrix mat; GfxMatrixToSkiaMatrix(pat.mMatrix, mat); shader->setLocalMatrix(mat); SkSafeUnref(aPaint.setShader(shader)); if (pat.mFilter == Filter::POINT) { aPaint.setFilterBitmap(false); } break; } } }
void Font::drawGlyphs(GraphicsContext* gc, const SimpleFontData* font, const GlyphBuffer& glyphBuffer, int from, int numGlyphs, const FloatPoint& point) const { // compile-time assert SkASSERT(sizeof(GlyphBufferGlyph) == sizeof(uint16_t)); SkPaint paint; if (!setupForText(&paint, gc, font)) { return; } SkScalar x = SkFloatToScalar(point.x()); SkScalar y = SkFloatToScalar(point.y()); const GlyphBufferGlyph* glyphs = glyphBuffer.glyphs(from); const GlyphBufferAdvance* adv = glyphBuffer.advances(from); SkAutoSTMalloc<32, SkPoint> storage(numGlyphs); SkPoint* pos = storage.get(); SkCanvas* canvas = gc->platformContext()->mCanvas; /* We need an array of [x,y,x,y,x,y,...], but webkit is giving us point.xy + [width, height, width, height, ...], so we have to convert */ if (EmojiFont::IsAvailable()) { // set filtering, to make scaled images look nice(r) paint.setFilterBitmap(true); int localIndex = 0; int localCount = 0; for (int i = 0; i < numGlyphs; i++) { if (EmojiFont::IsEmojiGlyph(glyphs[i])) { if (localCount) canvas->drawPosText(&glyphs[localIndex], localCount * sizeof(uint16_t), &pos[localIndex], paint); EmojiFont::Draw(canvas, glyphs[i], x, y, paint); // reset local index/count track for "real" glyphs localCount = 0; localIndex = i + 1; } else { pos[i].set(x, y); localCount += 1; } x += SkFloatToScalar(adv[i].width()); y += SkFloatToScalar(adv[i].height()); } // draw the last run of glyphs (if any) if (localCount) canvas->drawPosText(&glyphs[localIndex], localCount * sizeof(uint16_t), &pos[localIndex], paint); } else { for (int i = 0; i < numGlyphs; i++) { pos[i].set(x, y); x += SkFloatToScalar(adv[i].width()); y += SkFloatToScalar(adv[i].height()); } canvas->drawPosText(glyphs, numGlyphs * sizeof(uint16_t), pos, paint); } }
void drawTextOverCanvas(RenderingContext* rc, SkCanvas* cv) { SkRect r = SkRect::MakeLTRB(0, 0, rc->getWidth(), rc->getHeight()); r.inset(-100, -100); quad_tree<TextDrawInfo*> boundsIntersect(r, 4, 0.6); SkPaint paintIcon; paintIcon.setStyle(SkPaint::kStroke_Style); paintIcon.setStrokeWidth(1); paintIcon.setColor(0xff000000); paintIcon.setFilterBitmap(true); SkPaint paintText; paintText.setStyle(SkPaint::kFill_Style); paintText.setStrokeWidth(1); paintText.setColor(0xff000000); paintText.setTextAlign(SkPaint::kCenter_Align); if(!sTypeface) sTypeface = SkTypeface::CreateFromName("Droid Serif", SkTypeface::kNormal); paintText.setTypeface(sTypeface); paintText.setAntiAlias(true); SkPaint::FontMetrics fm; // 1. Sort text using text order std::sort(rc->textToDraw.begin(), rc->textToDraw.end(), textOrder); for (uint32_t i = 0; i < rc->textToDraw.size(); i++) { TextDrawInfo* text = rc->textToDraw.at(i); if (text->text.length() > 0) { // sest text size before finding intersection (it is used there) float textSize = rc->getDensityValue(text->textSize); paintText.setTextSize(textSize); paintText.setFakeBoldText(text->bold); paintText.setColor(text->textColor); // align center y paintText.getFontMetrics(&fm); text->centerY += (-fm.fAscent); // calculate if there is intersection bool intersects = findTextIntersection(cv, rc, boundsIntersect, text, &paintText, &paintIcon); if (!intersects) { if(rc->interrupted()){ return; } if (text->drawOnPath && text->path != NULL) { if (text->textShadow > 0) { paintText.setColor(0xFFFFFFFF); paintText.setStyle(SkPaint::kStroke_Style); paintText.setStrokeWidth(2 + text->textShadow); rc->nativeOperations.Pause(); cv->drawTextOnPathHV(text->text.c_str(), text->text.length(), *text->path, text->hOffset, text->vOffset, paintText); rc->nativeOperations.Start(); // reset paintText.setStyle(SkPaint::kFill_Style); paintText.setStrokeWidth(2); paintText.setColor(text->textColor); } rc->nativeOperations.Pause(); cv->drawTextOnPathHV(text->text.c_str(), text->text.length(), *text->path, text->hOffset, text->vOffset, paintText); rc->nativeOperations.Start(); } else { if (text->shieldRes.length() > 0) { SkBitmap* ico = getCachedBitmap(rc, text->shieldRes); if (ico != NULL) { float left = text->centerX - rc->getDensityValue(ico->width() / 2) - 0.5f; float top = text->centerY - rc->getDensityValue(ico->height() / 2) - rc->getDensityValue(4.5f); SkRect r = SkRect::MakeXYWH(left, top, rc->getDensityValue(ico->width()), rc->getDensityValue(ico->height())); PROFILE_NATIVE_OPERATION(rc, cv->drawBitmapRect(*ico, (SkIRect*) NULL, r, &paintIcon)); } } drawWrappedText(rc, cv, text, textSize, paintText); } } } } }