/* * Analyze filter-quality and matrix, and decide how to implement that. * * In general, we cascade down the request level [ High ... None ] * - for a given level, if we can fulfill it, fine, else * - else we downgrade to the next lower level and try again. * We can always fulfill requests for Low and None * - sometimes we will "ignore" Low and give None, but this is likely a legacy perf hack * and may be removed. */ bool SkBitmapProcState::chooseProcs(const SkMatrix& inv, const SkPaint& paint) { fPixmap.reset(); fInvMatrix = inv; fFilterLevel = paint.getFilterQuality(); const int origW = fProvider.info().width(); const int origH = fProvider.info().height(); bool allow_ignore_fractional_translate = true; // historical default if (kMedium_SkFilterQuality == fFilterLevel) { allow_ignore_fractional_translate = false; } SkDefaultBitmapController controller; fBMState = controller.requestBitmap(fProvider, inv, paint.getFilterQuality(), fBMStateStorage.get(), fBMStateStorage.size()); // Note : we allow the controller to return an empty (zero-dimension) result. Should we? if (nullptr == fBMState || fBMState->pixmap().info().isEmpty()) { return false; } fPixmap = fBMState->pixmap(); fInvMatrix = fBMState->invMatrix(); fFilterLevel = fBMState->quality(); SkASSERT(fPixmap.addr()); bool trivialMatrix = (fInvMatrix.getType() & ~SkMatrix::kTranslate_Mask) == 0; bool clampClamp = SkShader::kClamp_TileMode == fTileModeX && SkShader::kClamp_TileMode == fTileModeY; // Most of the scanline procs deal with "unit" texture coordinates, as this // makes it easy to perform tiling modes (repeat = (x & 0xFFFF)). To generate // those, we divide the matrix by its dimensions here. // // We don't do this if we're either trivial (can ignore the matrix) or clamping // in both X and Y since clamping to width,height is just as easy as to 0xFFFF. if (!(clampClamp || trivialMatrix)) { fInvMatrix.postIDiv(fPixmap.width(), fPixmap.height()); } // Now that all possible changes to the matrix have taken place, check // to see if we're really close to a no-scale matrix. If so, explicitly // set it to be so. Subsequent code may inspect this matrix to choose // a faster path in this case. // This code will only execute if the matrix has some scale component; // if it's already pure translate then we won't do this inversion. if (matrix_only_scale_translate(fInvMatrix)) { SkMatrix forward; if (fInvMatrix.invert(&forward)) { if ((clampClamp && allow_ignore_fractional_translate) ? just_trans_clamp(forward, fPixmap) : just_trans_general(forward)) { fInvMatrix.setTranslate(-forward.getTranslateX(), -forward.getTranslateY()); } } } fInvProc = fInvMatrix.getMapXYProc(); fInvType = fInvMatrix.getType(); fInvSx = SkScalarToFixed(fInvMatrix.getScaleX()); fInvSxFractionalInt = SkScalarToFractionalInt(fInvMatrix.getScaleX()); fInvKy = SkScalarToFixed(fInvMatrix.getSkewY()); fInvKyFractionalInt = SkScalarToFractionalInt(fInvMatrix.getSkewY()); fAlphaScale = SkAlpha255To256(paint.getAlpha()); fShaderProc32 = nullptr; fShaderProc16 = nullptr; fSampleProc32 = nullptr; // recompute the triviality of the matrix here because we may have // changed it! trivialMatrix = (fInvMatrix.getType() & ~SkMatrix::kTranslate_Mask) == 0; // If our target pixmap is the same as the original, then we revert back to legacy behavior // and allow the code to ignore fractional translate. // // The width/height check allows allow_ignore_fractional_translate to stay false if we // previously set it that way (e.g. we started in kMedium). // if (fPixmap.width() == origW && fPixmap.height() == origH) { allow_ignore_fractional_translate = true; } if (kLow_SkFilterQuality == fFilterLevel && allow_ignore_fractional_translate) { // Only try bilerp if the matrix is "interesting" and // the image has a suitable size. if (fInvType <= SkMatrix::kTranslate_Mask || !valid_for_filtering(fPixmap.width() | fPixmap.height())) { fFilterLevel = kNone_SkFilterQuality; } } return this->chooseScanlineProcs(trivialMatrix, clampClamp, paint); }
SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END bool SkImageShader::onAppendStages(const StageRec& rec) const { SkRasterPipeline* p = rec.fPipeline; SkArenaAlloc* alloc = rec.fAlloc; SkMatrix matrix; if (!this->computeTotalInverse(rec.fCTM, rec.fLocalM, &matrix)) { return false; } auto quality = rec.fPaint.getFilterQuality(); SkBitmapProvider provider(fImage.get(), rec.fDstCS); SkDefaultBitmapController controller; std::unique_ptr<SkBitmapController::State> state { controller.requestBitmap(provider, matrix, quality) }; if (!state) { return false; } const SkPixmap& pm = state->pixmap(); matrix = state->invMatrix(); quality = state->quality(); auto info = pm.info(); // When the matrix is just an integer translate, bilerp == nearest neighbor. if (quality == kLow_SkFilterQuality && matrix.getType() <= SkMatrix::kTranslate_Mask && matrix.getTranslateX() == (int)matrix.getTranslateX() && matrix.getTranslateY() == (int)matrix.getTranslateY()) { quality = kNone_SkFilterQuality; } // See skia:4649 and the GM image_scale_aligned. if (quality == kNone_SkFilterQuality) { if (matrix.getScaleX() >= 0) { matrix.setTranslateX(nextafterf(matrix.getTranslateX(), floorf(matrix.getTranslateX()))); } if (matrix.getScaleY() >= 0) { matrix.setTranslateY(nextafterf(matrix.getTranslateY(), floorf(matrix.getTranslateY()))); } } p->append(SkRasterPipeline::seed_shader); struct MiscCtx { std::unique_ptr<SkBitmapController::State> state; SkColor4f paint_color; }; auto misc = alloc->make<MiscCtx>(); misc->state = std::move(state); // Extend lifetime to match the pipeline's. misc->paint_color = SkColor4f_from_SkColor(rec.fPaint.getColor(), rec.fDstCS); p->append_matrix(alloc, matrix); auto gather = alloc->make<SkJumper_GatherCtx>(); gather->pixels = pm.addr(); gather->stride = pm.rowBytesAsPixels(); gather->width = pm.width(); gather->height = pm.height(); auto limit_x = alloc->make<SkJumper_TileCtx>(), limit_y = alloc->make<SkJumper_TileCtx>(); limit_x->scale = pm.width(); limit_x->invScale = 1.0f / pm.width(); limit_y->scale = pm.height(); limit_y->invScale = 1.0f / pm.height(); bool is_srgb = rec.fDstCS && (!info.colorSpace() || info.gammaCloseToSRGB()); SkJumper_DecalTileCtx* decal_ctx = nullptr; bool decal_x_and_y = fTileModeX == kDecal_TileMode && fTileModeY == kDecal_TileMode; if (fTileModeX == kDecal_TileMode || fTileModeY == kDecal_TileMode) { decal_ctx = alloc->make<SkJumper_DecalTileCtx>(); decal_ctx->limit_x = limit_x->scale; decal_ctx->limit_y = limit_y->scale; } auto append_tiling_and_gather = [&] { if (decal_x_and_y) { p->append(SkRasterPipeline::decal_x_and_y, decal_ctx); } else { switch (fTileModeX) { case kClamp_TileMode: /* The gather_xxx stage will clamp for us. */ break; case kMirror_TileMode: p->append(SkRasterPipeline::mirror_x, limit_x); break; case kRepeat_TileMode: p->append(SkRasterPipeline::repeat_x, limit_x); break; case kDecal_TileMode: p->append(SkRasterPipeline::decal_x, decal_ctx); break; } switch (fTileModeY) { case kClamp_TileMode: /* The gather_xxx stage will clamp for us. */ break; case kMirror_TileMode: p->append(SkRasterPipeline::mirror_y, limit_y); break; case kRepeat_TileMode: p->append(SkRasterPipeline::repeat_y, limit_y); break; case kDecal_TileMode: p->append(SkRasterPipeline::decal_y, decal_ctx); break; } } void* ctx = gather; switch (info.colorType()) { case kAlpha_8_SkColorType: p->append(SkRasterPipeline::gather_a8, ctx); break; case kGray_8_SkColorType: p->append(SkRasterPipeline::gather_g8, ctx); break; case kRGB_565_SkColorType: p->append(SkRasterPipeline::gather_565, ctx); break; case kARGB_4444_SkColorType: p->append(SkRasterPipeline::gather_4444, ctx); break; case kBGRA_8888_SkColorType: p->append(SkRasterPipeline::gather_bgra, ctx); break; case kRGBA_8888_SkColorType: p->append(SkRasterPipeline::gather_8888, ctx); break; case kRGBA_1010102_SkColorType: p->append(SkRasterPipeline::gather_1010102, ctx); break; case kRGBA_F16_SkColorType: p->append(SkRasterPipeline::gather_f16, ctx); break; case kRGB_888x_SkColorType: p->append(SkRasterPipeline::gather_8888, ctx); p->append(SkRasterPipeline::force_opaque ); break; case kRGB_101010x_SkColorType: p->append(SkRasterPipeline::gather_1010102, ctx); p->append(SkRasterPipeline::force_opaque ); break; default: SkASSERT(false); } if (decal_ctx) { p->append(SkRasterPipeline::check_decal_mask, decal_ctx); } if (is_srgb) { p->append(SkRasterPipeline::from_srgb); } }; auto append_misc = [&] { if (info.colorType() == kAlpha_8_SkColorType) { p->append(SkRasterPipeline::set_rgb, &misc->paint_color); } if (info.colorType() == kAlpha_8_SkColorType || info.alphaType() == kUnpremul_SkAlphaType) { p->append(SkRasterPipeline::premul); } if (quality > kLow_SkFilterQuality) { // Bicubic filtering naturally produces out of range values on both sides. p->append(SkRasterPipeline::clamp_0); p->append(fClampAsIfUnpremul ? SkRasterPipeline::clamp_1 : SkRasterPipeline::clamp_a); } append_gamut_transform(p, alloc, info.colorSpace(), rec.fDstCS, fClampAsIfUnpremul ? kUnpremul_SkAlphaType : kPremul_SkAlphaType); return true; }; // We've got a fast path for 8888 bilinear clamp/clamp non-color-managed sampling. auto ct = info.colorType(); if (true && (ct == kRGBA_8888_SkColorType || ct == kBGRA_8888_SkColorType) && quality == kLow_SkFilterQuality && fTileModeX == SkShader::kClamp_TileMode && fTileModeY == SkShader::kClamp_TileMode && !is_srgb) { p->append(SkRasterPipeline::bilerp_clamp_8888, gather); if (ct == kBGRA_8888_SkColorType) { p->append(SkRasterPipeline::swap_rb); } return append_misc(); } SkJumper_SamplerCtx* sampler = nullptr; if (quality != kNone_SkFilterQuality) { sampler = alloc->make<SkJumper_SamplerCtx>(); } auto sample = [&](SkRasterPipeline::StockStage setup_x, SkRasterPipeline::StockStage setup_y) { p->append(setup_x, sampler); p->append(setup_y, sampler); append_tiling_and_gather(); p->append(SkRasterPipeline::accumulate, sampler); }; if (quality == kNone_SkFilterQuality) { append_tiling_and_gather(); } else if (quality == kLow_SkFilterQuality) { p->append(SkRasterPipeline::save_xy, sampler); sample(SkRasterPipeline::bilinear_nx, SkRasterPipeline::bilinear_ny); sample(SkRasterPipeline::bilinear_px, SkRasterPipeline::bilinear_ny); sample(SkRasterPipeline::bilinear_nx, SkRasterPipeline::bilinear_py); sample(SkRasterPipeline::bilinear_px, SkRasterPipeline::bilinear_py); p->append(SkRasterPipeline::move_dst_src); } else { p->append(SkRasterPipeline::save_xy, sampler); sample(SkRasterPipeline::bicubic_n3x, SkRasterPipeline::bicubic_n3y); sample(SkRasterPipeline::bicubic_n1x, SkRasterPipeline::bicubic_n3y); sample(SkRasterPipeline::bicubic_p1x, SkRasterPipeline::bicubic_n3y); sample(SkRasterPipeline::bicubic_p3x, SkRasterPipeline::bicubic_n3y); sample(SkRasterPipeline::bicubic_n3x, SkRasterPipeline::bicubic_n1y); sample(SkRasterPipeline::bicubic_n1x, SkRasterPipeline::bicubic_n1y); sample(SkRasterPipeline::bicubic_p1x, SkRasterPipeline::bicubic_n1y); sample(SkRasterPipeline::bicubic_p3x, SkRasterPipeline::bicubic_n1y); sample(SkRasterPipeline::bicubic_n3x, SkRasterPipeline::bicubic_p1y); sample(SkRasterPipeline::bicubic_n1x, SkRasterPipeline::bicubic_p1y); sample(SkRasterPipeline::bicubic_p1x, SkRasterPipeline::bicubic_p1y); sample(SkRasterPipeline::bicubic_p3x, SkRasterPipeline::bicubic_p1y); sample(SkRasterPipeline::bicubic_n3x, SkRasterPipeline::bicubic_p3y); sample(SkRasterPipeline::bicubic_n1x, SkRasterPipeline::bicubic_p3y); sample(SkRasterPipeline::bicubic_p1x, SkRasterPipeline::bicubic_p3y); sample(SkRasterPipeline::bicubic_p3x, SkRasterPipeline::bicubic_p3y); p->append(SkRasterPipeline::move_dst_src); } return append_misc(); }