void TileGrid::drawMissingRegion(const SkRegion& region, float opacity, const Color* background) { SkRegion::Iterator iterator(region); const float tileWidth = TilesManager::tileWidth() / m_scale; const float tileHeight = TilesManager::tileHeight() / m_scale; while (!iterator.done()) { SkIRect r = iterator.rect(); SkRect rect; rect.fLeft = r.x() * tileWidth; rect.fTop = r.y() * tileHeight; rect.fRight = rect.fLeft + tileWidth * r.width(); rect.fBottom = rect.fTop + tileHeight * r.height(); ALOGV("draw tile x y, %d %d (%d %d) opacity %f", r.x(), r.y(), r.width(), r.height(), opacity); // Skia is using pre-multiplied color. Color postAlpha = Color(background->red() * background->alpha() / 255, background->green() * background->alpha() / 255, background->blue() * background->alpha() / 255, background->alpha() ); PureColorQuadData backGroundData(postAlpha, BaseQuad, 0, &rect, opacity); TilesManager::instance()->shader()->drawQuad(&backGroundData); iterator.next(); } }
bool SkPixmap::readPixels(const SkImageInfo& requestedDstInfo, void* dstPixels, size_t dstRB, int x, int y) const { if (kUnknown_SkColorType == requestedDstInfo.colorType()) { return false; } if (nullptr == dstPixels || dstRB < requestedDstInfo.minRowBytes()) { return false; } if (0 == requestedDstInfo.width() || 0 == requestedDstInfo.height()) { return false; } SkIRect srcR = SkIRect::MakeXYWH(x, y, requestedDstInfo.width(), requestedDstInfo.height()); if (!srcR.intersect(0, 0, this->width(), this->height())) { return false; } // the intersect may have shrunk info's logical size const SkImageInfo dstInfo = requestedDstInfo.makeWH(srcR.width(), srcR.height()); // if x or y are negative, then we have to adjust pixels if (x > 0) { x = 0; } if (y > 0) { y = 0; } // here x,y are either 0 or negative dstPixels = ((char*)dstPixels - y * dstRB - x * dstInfo.bytesPerPixel()); const SkImageInfo srcInfo = this->info().makeWH(dstInfo.width(), dstInfo.height()); const void* srcPixels = this->addr(srcR.x(), srcR.y()); return SkPixelInfo::CopyPixels(dstInfo, dstPixels, dstRB, srcInfo, srcPixels, this->rowBytes(), this->ctable()); }
// This function is used to scale an image and extract a scaled fragment. // // ALGORITHM // // Because the scaled image size has to be integers, we approximate the real // scale with the following formula (only X direction is shown): // // scaledImageWidth = round(scaleX * imageRect.width()) // approximateScaleX = scaledImageWidth / imageRect.width() // // With this method we maintain a constant scale factor among fragments in // the scaled image. This allows fragments to stitch together to form the // full scaled image. The downside is there will be a small difference // between |scaleX| and |approximateScaleX|. // // A scaled image fragment is identified by: // // - Scaled image size // - Scaled image fragment rectangle (IntRect) // // Scaled image size has been determined and the next step is to compute the // rectangle for the scaled image fragment which needs to be an IntRect. // // scaledSrcRect = srcRect * (approximateScaleX, approximateScaleY) // enclosingScaledSrcRect = enclosingIntRect(scaledSrcRect) // // Finally we extract the scaled image fragment using // (scaledImageSize, enclosingScaledSrcRect). // SkBitmap NativeImageSkia::extractScaledImageFragment(const SkRect& srcRect, float scaleX, float scaleY, SkRect* scaledSrcRect) const { SkISize imageSize = SkISize::Make(bitmap().width(), bitmap().height()); SkISize scaledImageSize = SkISize::Make(clampToInteger(roundf(imageSize.width() * scaleX)), clampToInteger(roundf(imageSize.height() * scaleY))); SkRect imageRect = SkRect::MakeWH(imageSize.width(), imageSize.height()); SkRect scaledImageRect = SkRect::MakeWH(scaledImageSize.width(), scaledImageSize.height()); SkMatrix scaleTransform; scaleTransform.setRectToRect(imageRect, scaledImageRect, SkMatrix::kFill_ScaleToFit); scaleTransform.mapRect(scaledSrcRect, srcRect); bool ok = scaledSrcRect->intersect(scaledImageRect); ASSERT_UNUSED(ok, ok); SkIRect enclosingScaledSrcRect = enclosingIntRect(*scaledSrcRect); // |enclosingScaledSrcRect| can be larger than |scaledImageSize| because // of float inaccuracy so clip to get inside. ok = enclosingScaledSrcRect.intersect(SkIRect::MakeSize(scaledImageSize)); ASSERT_UNUSED(ok, ok); // scaledSrcRect is relative to the pixel snapped fragment we're extracting. scaledSrcRect->offset(-enclosingScaledSrcRect.x(), -enclosingScaledSrcRect.y()); return resizedBitmap(scaledImageSize, enclosingScaledSrcRect); }
bool SkMagnifierImageFilter::asFragmentProcessor(GrFragmentProcessor** fp, GrProcessorDataManager* procDataManager, GrTexture* texture, const SkMatrix&, const SkIRect&bounds) const { if (fp) { SkScalar yOffset = texture->origin() == kTopLeft_GrSurfaceOrigin ? fSrcRect.y() : texture->height() - fSrcRect.height() * texture->height() / bounds.height() - fSrcRect.y(); int boundsY = (texture->origin() == kTopLeft_GrSurfaceOrigin) ? bounds.y() : (texture->height() - bounds.height()); SkRect effectBounds = SkRect::MakeXYWH( SkIntToScalar(bounds.x()) / texture->width(), SkIntToScalar(boundsY) / texture->height(), SkIntToScalar(texture->width()) / bounds.width(), SkIntToScalar(texture->height()) / bounds.height()); SkScalar invInset = fInset > 0 ? SkScalarInvert(fInset) : SK_Scalar1; *fp = GrMagnifierEffect::Create(procDataManager, texture, effectBounds, fSrcRect.x() / texture->width(), yOffset / texture->height(), fSrcRect.width() / bounds.width(), fSrcRect.height() / bounds.height(), bounds.width() * invInset, bounds.height() * invInset); } return true; }
bool SkWritePixelsRec::trim(int dstWidth, int dstHeight) { if (nullptr == fPixels || fRowBytes < fInfo.minRowBytes()) { return false; } if (0 >= fInfo.width() || 0 >= fInfo.height()) { return false; } int x = fX; int y = fY; SkIRect dstR = SkIRect::MakeXYWH(x, y, fInfo.width(), fInfo.height()); if (!dstR.intersect(0, 0, dstWidth, dstHeight)) { return false; } // if x or y are negative, then we have to adjust pixels if (x > 0) { x = 0; } if (y > 0) { y = 0; } // here x,y are either 0 or negative fPixels = ((const char*)fPixels - y * fRowBytes - x * fInfo.bytesPerPixel()); // the intersect may have shrunk info's logical size fInfo = fInfo.makeWH(dstR.width(), dstR.height()); fX = dstR.x(); fY = dstR.y(); return true; }
void updateHandle(Handle hndl, const SkMatrix& ctm, const SkIRect& clip) override { CGContextRef cg = (CGContextRef)hndl; CGContextRestoreGState(cg); CGContextSaveGState(cg); CGContextClipToRect(cg, CGRectMake(clip.x(), clip.y(), clip.width(), clip.height())); CGContextConcatCTM(cg, matrix_to_transform(cg, ctm)); }
// Much of readPixels is exercised by copyTo testing, since readPixels is the backend for that // method. Here we explicitly test subset copies. // DEF_TEST(BitmapReadPixels, reporter) { const int W = 4; const int H = 4; const size_t rowBytes = W * sizeof(SkPMColor); const SkImageInfo srcInfo = SkImageInfo::MakeN32Premul(W, H); SkPMColor srcPixels[16]; fill_4x4_pixels(srcPixels); SkBitmap srcBM; srcBM.installPixels(srcInfo, srcPixels, rowBytes); SkImageInfo dstInfo = SkImageInfo::MakeN32Premul(W, H); SkPMColor dstPixels[16]; const struct { bool fExpectedSuccess; SkIPoint fRequestedSrcLoc; SkISize fRequestedDstSize; // If fExpectedSuccess, check these, otherwise ignore SkIPoint fExpectedDstLoc; SkIRect fExpectedSrcR; } gRec[] = { { true, { 0, 0 }, { 4, 4 }, { 0, 0 }, { 0, 0, 4, 4 } }, { true, { 1, 1 }, { 2, 2 }, { 0, 0 }, { 1, 1, 3, 3 } }, { true, { 2, 2 }, { 4, 4 }, { 0, 0 }, { 2, 2, 4, 4 } }, { true, {-1,-1 }, { 2, 2 }, { 1, 1 }, { 0, 0, 1, 1 } }, { false, {-1,-1 }, { 1, 1 }, { 0, 0 }, { 0, 0, 0, 0 } }, }; for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) { clear_4x4_pixels(dstPixels); dstInfo = dstInfo.makeWH(gRec[i].fRequestedDstSize.width(), gRec[i].fRequestedDstSize.height()); bool success = srcBM.readPixels(dstInfo, dstPixels, rowBytes, gRec[i].fRequestedSrcLoc.x(), gRec[i].fRequestedSrcLoc.y()); REPORTER_ASSERT(reporter, gRec[i].fExpectedSuccess == success); if (success) { const SkIRect srcR = gRec[i].fExpectedSrcR; const int dstX = gRec[i].fExpectedDstLoc.x(); const int dstY = gRec[i].fExpectedDstLoc.y(); // Walk the dst pixels, and check if we got what we expected for (int y = 0; y < H; ++y) { for (int x = 0; x < W; ++x) { SkPMColor dstC = dstPixels[y*4+x]; // get into src coordinates int sx = x - dstX + srcR.x(); int sy = y - dstY + srcR.y(); if (srcR.contains(sx, sy)) { REPORTER_ASSERT(reporter, check_4x4_pixel(dstC, sx, sy)); } else { REPORTER_ASSERT(reporter, 0 == dstC); } } } } } }
static void make_unstretched_key(GrUniqueKey* key, uint32_t imageID, const SkIRect& subset) { SkASSERT(SkIsU16(subset.width())); SkASSERT(SkIsU16(subset.height())); static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain(); GrUniqueKey::Builder builder(key, kDomain, 4); builder[0] = imageID; builder[1] = subset.x(); builder[2] = subset.y(); builder[3] = subset.width() | (subset.height() << 16); }
// Return true if the rectangle is aligned to integer boundaries. // See comments for computeBitmapDrawRects() for how this is used. static bool areBoundariesIntegerAligned(const SkRect& rect) { // Value is 1.19209e-007. This is the tolerance threshold. const float epsilon = std::numeric_limits<float>::epsilon(); SkIRect roundedRect = roundedIntRect(rect); return fabs(rect.x() - roundedRect.x()) < epsilon && fabs(rect.y() - roundedRect.y()) < epsilon && fabs(rect.right() - roundedRect.right()) < epsilon && fabs(rect.bottom() - roundedRect.bottom()) < epsilon; }
SkImage* SkImage_Base::onApplyFilter(SkImageFilter* filter, SkIPoint* offsetResult, bool forceResultToOriginalSize) const { SkBitmap src; if (!this->getROPixels(&src)) { return nullptr; } const SkIRect srcBounds = SkIRect::MakeWH(this->width(), this->height()); if (forceResultToOriginalSize) { const SkIRect clipBounds = srcBounds; SkRasterImageFilterProxy proxy; SkImageFilter::Context ctx(SkMatrix::I(), clipBounds, SkImageFilter::Cache::Get(), SkImageFilter::kExact_SizeConstraint); SkBitmap dst; if (filter->filterImage(&proxy, src, ctx, &dst, offsetResult)) { dst.setImmutable(); return SkImage::NewFromBitmap(dst); } } else { const SkIRect dstR = compute_fast_ibounds(filter, srcBounds); SkImageInfo info = SkImageInfo::MakeN32Premul(dstR.width(), dstR.height()); SkAutoTUnref<SkSurface> surface(this->onNewSurface(info)); SkPaint paint; paint.setImageFilter(filter); surface->getCanvas()->drawImage(this, SkIntToScalar(-dstR.x()), SkIntToScalar(-dstR.y()), &paint); offsetResult->set(dstR.x(), dstR.y()); return surface->newImageSnapshot(); } return nullptr; }
sk_sp<SkImage> SkImage_Generator::onMakeSubset(const SkIRect& subset) const { // TODO: make this lazy, by wrapping the subset inside a new generator or something // For now, we do effectively what we did before, make it a raster const SkImageInfo info = SkImageInfo::MakeN32(subset.width(), subset.height(), this->isOpaque() ? kOpaque_SkAlphaType : kPremul_SkAlphaType); auto surface(SkSurface::MakeRaster(info)); if (!surface) { return nullptr; } surface->getCanvas()->clear(0); surface->getCanvas()->drawImage(this, SkIntToScalar(-subset.x()), SkIntToScalar(-subset.y()), nullptr); return surface->makeImageSnapshot(); }
bool SkBlurImageFilter::filterImageGPUDeprecated(Proxy* proxy, const SkBitmap& src, const Context& ctx, SkBitmap* result, SkIPoint* offset) const { #if SK_SUPPORT_GPU SkBitmap input = src; SkIPoint srcOffset = SkIPoint::Make(0, 0); if (!this->filterInputGPUDeprecated(0, proxy, src, ctx, &input, &srcOffset)) { return false; } SkIRect srcBounds = input.bounds(); srcBounds.offset(srcOffset); SkIRect dstBounds; if (!this->applyCropRect(this->mapContext(ctx), srcBounds, &dstBounds)) { return false; } if (!srcBounds.intersect(dstBounds)) { return false; } SkVector sigma = map_sigma(fSigma, ctx.ctm()); if (sigma.x() == 0 && sigma.y() == 0) { input.extractSubset(result, srcBounds); offset->fX = srcBounds.x(); offset->fY = srcBounds.y(); return true; } offset->fX = dstBounds.fLeft; offset->fY = dstBounds.fTop; srcBounds.offset(-srcOffset); dstBounds.offset(-srcOffset); SkRect srcBoundsF(SkRect::Make(srcBounds)); GrTexture* inputTexture = input.getTexture(); SkAutoTUnref<GrTexture> tex(SkGpuBlurUtils::GaussianBlur(inputTexture->getContext(), inputTexture, false, SkRect::Make(dstBounds), &srcBoundsF, sigma.x(), sigma.y())); if (!tex) { return false; } GrWrapTextureInBitmap(tex, dstBounds.width(), dstBounds.height(), false, result); return true; #else SkDEBUGFAIL("Should not call in GPU-less build"); return false; #endif }
GrAlphaThresholdFragmentProcessor::GrAlphaThresholdFragmentProcessor(GrTexture* texture, GrTexture* maskTexture, float innerThreshold, float outerThreshold, const SkIRect& bounds) : fInnerThreshold(innerThreshold) , fOuterThreshold(outerThreshold) , fImageCoordTransform(kLocal_GrCoordSet, GrCoordTransform::MakeDivByTextureWHMatrix(texture), texture, GrTextureParams::kNone_FilterMode) , fImageTextureAccess(texture) , fMaskCoordTransform(kLocal_GrCoordSet, make_div_and_translate_matrix(maskTexture, -bounds.x(), -bounds.y()), maskTexture, GrTextureParams::kNone_FilterMode) , fMaskTextureAccess(maskTexture) { this->initClassID<GrAlphaThresholdFragmentProcessor>(); this->addCoordTransform(&fImageCoordTransform); this->addTextureAccess(&fImageTextureAccess); this->addCoordTransform(&fMaskCoordTransform); this->addTextureAccess(&fMaskTextureAccess); }
bool SkDisplacementMapEffect::filterImageGPU(Proxy* proxy, const SkBitmap& src, const Context& ctx, SkBitmap* result, SkIPoint* offset) const { SkBitmap colorBM = src; SkIPoint colorOffset = SkIPoint::Make(0, 0); if (getColorInput() && !getColorInput()->getInputResultGPU(proxy, src, ctx, &colorBM, &colorOffset)) { return false; } SkBitmap displacementBM = src; SkIPoint displacementOffset = SkIPoint::Make(0, 0); if (getDisplacementInput() && !getDisplacementInput()->getInputResultGPU(proxy, src, ctx, &displacementBM, &displacementOffset)) { return false; } SkIRect bounds; // Since GrDisplacementMapEffect does bounds checking on color pixel access, we don't need to // pad the color bitmap to bounds here. if (!this->applyCropRect(ctx, colorBM, colorOffset, &bounds)) { return false; } SkIRect displBounds; if (!this->applyCropRect(ctx, proxy, displacementBM, &displacementOffset, &displBounds, &displacementBM)) { return false; } if (!bounds.intersect(displBounds)) { return false; } GrTexture* color = colorBM.getTexture(); GrTexture* displacement = displacementBM.getTexture(); GrContext* context = color->getContext(); GrSurfaceDesc desc; desc.fFlags = kRenderTarget_GrSurfaceFlag; desc.fWidth = bounds.width(); desc.fHeight = bounds.height(); desc.fConfig = kSkia8888_GrPixelConfig; SkAutoTUnref<GrTexture> dst( context->refScratchTexture(desc, GrContext::kApprox_ScratchTexMatch)); if (!dst) { return false; } SkVector scale = SkVector::Make(fScale, fScale); ctx.ctm().mapVectors(&scale, 1); GrPaint paint; SkMatrix offsetMatrix = GrCoordTransform::MakeDivByTextureWHMatrix(displacement); offsetMatrix.preTranslate(SkIntToScalar(colorOffset.fX - displacementOffset.fX), SkIntToScalar(colorOffset.fY - displacementOffset.fY)); paint.addColorProcessor( GrDisplacementMapEffect::Create(fXChannelSelector, fYChannelSelector, scale, displacement, offsetMatrix, color, colorBM.dimensions()))->unref(); SkIRect colorBounds = bounds; colorBounds.offset(-colorOffset); SkMatrix matrix; matrix.setTranslate(-SkIntToScalar(colorBounds.x()), -SkIntToScalar(colorBounds.y())); context->drawRect(dst->asRenderTarget(), GrClip::WideOpen(), paint, matrix, SkRect::Make(colorBounds)); offset->fX = bounds.left(); offset->fY = bounds.top(); WrapTexture(dst, bounds.width(), bounds.height(), result); return true; }
bool SkJPEGImageDecoder::onDecodeRegion(SkBitmap* bm, SkIRect region) { if (index == NULL) { return false; } jpeg_decompress_struct *cinfo = index->cinfo; SkIRect rect = SkIRect::MakeWH(this->imageWidth, this->imageHeight); if (!rect.intersect(region)) { // If the requested region is entirely outsides the image, just // returns false return false; } SkAutoMalloc srcStorage; skjpeg_error_mgr sk_err; cinfo->err = jpeg_std_error(&sk_err); sk_err.error_exit = skjpeg_error_exit; if (setjmp(sk_err.fJmpBuf)) { return false; } int requestedSampleSize = this->getSampleSize(); cinfo->scale_denom = requestedSampleSize; if (this->getPreferQualityOverSpeed()) { cinfo->dct_method = JDCT_ISLOW; } else { cinfo->dct_method = JDCT_IFAST; } SkBitmap::Config config = this->getPrefConfig(k32Bit_SrcDepth, false); if (config != SkBitmap::kARGB_8888_Config && config != SkBitmap::kARGB_4444_Config && config != SkBitmap::kRGB_565_Config) { config = SkBitmap::kARGB_8888_Config; } /* default format is RGB */ cinfo->out_color_space = JCS_RGB; #ifdef ANDROID_RGB cinfo->dither_mode = JDITHER_NONE; if (config == SkBitmap::kARGB_8888_Config) { cinfo->out_color_space = JCS_RGBA_8888; } else if (config == SkBitmap::kRGB_565_Config) { cinfo->out_color_space = JCS_RGB_565; if (this->getDitherImage()) { cinfo->dither_mode = JDITHER_ORDERED; } } #endif int startX = rect.fLeft; int startY = rect.fTop; int width = rect.width(); int height = rect.height(); jpeg_init_read_tile_scanline(cinfo, index->index, &startX, &startY, &width, &height); int skiaSampleSize = recompute_sampleSize(requestedSampleSize, *cinfo); int actualSampleSize = skiaSampleSize * (DCTSIZE / cinfo->min_DCT_scaled_size); SkBitmap *bitmap = new SkBitmap; SkAutoTDelete<SkBitmap> adb(bitmap); #ifdef ANDROID_RGB /* short-circuit the SkScaledBitmapSampler when possible, as this gives a significant performance boost. */ if (skiaSampleSize == 1 && ((config == SkBitmap::kARGB_8888_Config && cinfo->out_color_space == JCS_RGBA_8888) || (config == SkBitmap::kRGB_565_Config && cinfo->out_color_space == JCS_RGB_565))) { bitmap->setConfig(config, cinfo->output_width, height); bitmap->setIsOpaque(true); // Check ahead of time if the swap(dest, src) is possible or not. // If yes, then we will stick to AllocPixelRef since it's cheaper // with the swap happening. If no, then we will use alloc to allocate // pixels to prevent garbage collection. // // Not using a recycled-bitmap and the output rect is same as the // decoded region. int w = rect.width() / actualSampleSize; int h = rect.height() / actualSampleSize; bool swapOnly = (rect == region) && bm->isNull() && (w == bitmap->width()) && (h == bitmap->height()) && ((startX - rect.x()) / actualSampleSize == 0) && ((startY - rect.y()) / actualSampleSize == 0); if (swapOnly) { if (!this->allocPixelRef(bitmap, NULL)) { return return_false(*cinfo, *bitmap, "allocPixelRef"); } } else { if (!bitmap->allocPixels()) { return return_false(*cinfo, *bitmap, "allocPixels"); } } SkAutoLockPixels alp(*bitmap); JSAMPLE* rowptr = (JSAMPLE*)bitmap->getPixels(); INT32 const bpr = bitmap->rowBytes(); int row_total_count = 0; while (row_total_count < height) { int row_count = jpeg_read_tile_scanline(cinfo, index->index, &rowptr); // if row_count == 0, then we didn't get a scanline, so abort. // if we supported partial images, we might return true in this case if (0 == row_count) { return return_false(*cinfo, *bitmap, "read_scanlines"); } if (this->shouldCancelDecode()) { return return_false(*cinfo, *bitmap, "shouldCancelDecode"); } row_total_count += row_count; rowptr += bpr; } if (swapOnly) { bm->swap(*bitmap); } else { cropBitmap(bm, bitmap, actualSampleSize, region.x(), region.y(), region.width(), region.height(), startX, startY); } return true; } #endif // check for supported formats SkScaledBitmapSampler::SrcConfig sc; if (3 == cinfo->out_color_components && JCS_RGB == cinfo->out_color_space) { sc = SkScaledBitmapSampler::kRGB; #ifdef ANDROID_RGB } else if (JCS_RGBA_8888 == cinfo->out_color_space) { sc = SkScaledBitmapSampler::kRGBX; } else if (JCS_RGB_565 == cinfo->out_color_space) { sc = SkScaledBitmapSampler::kRGB_565; #endif } else if (1 == cinfo->out_color_components && JCS_GRAYSCALE == cinfo->out_color_space) { sc = SkScaledBitmapSampler::kGray; } else { return return_false(*cinfo, *bm, "jpeg colorspace"); } SkScaledBitmapSampler sampler(width, height, skiaSampleSize); bitmap->setConfig(config, sampler.scaledWidth(), sampler.scaledHeight()); bitmap->setIsOpaque(true); // Check ahead of time if the swap(dest, src) is possible or not. // If yes, then we will stick to AllocPixelRef since it's cheaper with the // swap happening. If no, then we will use alloc to allocate pixels to // prevent garbage collection. int w = rect.width() / actualSampleSize; int h = rect.height() / actualSampleSize; bool swapOnly = (rect == region) && bm->isNull() && (w == bitmap->width()) && (h == bitmap->height()) && ((startX - rect.x()) / actualSampleSize == 0) && ((startY - rect.y()) / actualSampleSize == 0); if (swapOnly) { if (!this->allocPixelRef(bitmap, NULL)) { return return_false(*cinfo, *bitmap, "allocPixelRef"); } } else { if (!bitmap->allocPixels()) { return return_false(*cinfo, *bitmap, "allocPixels"); } } SkAutoLockPixels alp(*bitmap); if (!sampler.begin(bitmap, sc, this->getDitherImage())) { return return_false(*cinfo, *bitmap, "sampler.begin"); } uint8_t* srcRow = (uint8_t*)srcStorage.reset(width * 4); // Possibly skip initial rows [sampler.srcY0] if (!skip_src_rows_tile(cinfo, index->index, srcRow, sampler.srcY0())) { return return_false(*cinfo, *bitmap, "skip rows"); } // now loop through scanlines until y == bitmap->height() - 1 for (int y = 0;; y++) { JSAMPLE* rowptr = (JSAMPLE*)srcRow; int row_count = jpeg_read_tile_scanline(cinfo, index->index, &rowptr); if (0 == row_count) { return return_false(*cinfo, *bitmap, "read_scanlines"); } if (this->shouldCancelDecode()) { return return_false(*cinfo, *bitmap, "shouldCancelDecode"); } sampler.next(srcRow); if (bitmap->height() - 1 == y) { // we're done break; } if (!skip_src_rows_tile(cinfo, index->index, srcRow, sampler.srcDY() - 1)) { return return_false(*cinfo, *bitmap, "skip rows"); } } if (swapOnly) { bm->swap(*bitmap); } else { cropBitmap(bm, bitmap, actualSampleSize, region.x(), region.y(), region.width(), region.height(), startX, startY); } return true; }
SkCodec::Result SkSampledCodec::onGetAndroidPixels(const SkImageInfo& info, void* pixels, size_t rowBytes, const AndroidOptions& options) { // Create an Options struct for the codec. SkCodec::Options codecOptions; codecOptions.fZeroInitialized = options.fZeroInitialized; codecOptions.fPremulBehavior = SkTransferFunctionBehavior::kIgnore; SkIRect* subset = options.fSubset; if (!subset || subset->size() == this->codec()->getInfo().dimensions()) { if (this->codec()->dimensionsSupported(info.dimensions())) { return this->codec()->getPixels(info, pixels, rowBytes, &codecOptions); } // If the native codec does not support the requested scale, scale by sampling. return this->sampledDecode(info, pixels, rowBytes, options); } // We are performing a subset decode. int sampleSize = options.fSampleSize; SkISize scaledSize = this->getSampledDimensions(sampleSize); if (!this->codec()->dimensionsSupported(scaledSize)) { // If the native codec does not support the requested scale, scale by sampling. return this->sampledDecode(info, pixels, rowBytes, options); } // Calculate the scaled subset bounds. int scaledSubsetX = subset->x() / sampleSize; int scaledSubsetY = subset->y() / sampleSize; int scaledSubsetWidth = info.width(); int scaledSubsetHeight = info.height(); const SkImageInfo scaledInfo = info.makeWH(scaledSize.width(), scaledSize.height()); { // Although startScanlineDecode expects the bottom and top to match the // SkImageInfo, startIncrementalDecode uses them to determine which rows to // decode. SkIRect incrementalSubset = SkIRect::MakeXYWH(scaledSubsetX, scaledSubsetY, scaledSubsetWidth, scaledSubsetHeight); codecOptions.fSubset = &incrementalSubset; const SkCodec::Result startResult = this->codec()->startIncrementalDecode( scaledInfo, pixels, rowBytes, &codecOptions); if (SkCodec::kSuccess == startResult) { int rowsDecoded; const SkCodec::Result incResult = this->codec()->incrementalDecode(&rowsDecoded); if (incResult == SkCodec::kSuccess) { return SkCodec::kSuccess; } SkASSERT(SkCodec::kIncompleteInput == incResult); // FIXME: Can zero initialized be read from SkCodec::fOptions? this->codec()->fillIncompleteImage(scaledInfo, pixels, rowBytes, options.fZeroInitialized, scaledSubsetHeight, rowsDecoded); return SkCodec::kIncompleteInput; } else if (startResult != SkCodec::kUnimplemented) { return startResult; } // Otherwise fall down to use the old scanline decoder. // codecOptions.fSubset will be reset below, so it will not continue to // point to the object that is no longer on the stack. } // Start the scanline decode. SkIRect scanlineSubset = SkIRect::MakeXYWH(scaledSubsetX, 0, scaledSubsetWidth, scaledSize.height()); codecOptions.fSubset = &scanlineSubset; SkCodec::Result result = this->codec()->startScanlineDecode(scaledInfo, &codecOptions); if (SkCodec::kSuccess != result) { return result; } // At this point, we are only concerned with subsetting. Either no scale was // requested, or the this->codec() is handling the scale. // Note that subsetting is only supported for kTopDown, so this code will not be // reached for other orders. SkASSERT(this->codec()->getScanlineOrder() == SkCodec::kTopDown_SkScanlineOrder); if (!this->codec()->skipScanlines(scaledSubsetY)) { this->codec()->fillIncompleteImage(info, pixels, rowBytes, options.fZeroInitialized, scaledSubsetHeight, 0); return SkCodec::kIncompleteInput; } int decodedLines = this->codec()->getScanlines(pixels, scaledSubsetHeight, rowBytes); if (decodedLines != scaledSubsetHeight) { return SkCodec::kIncompleteInput; } return SkCodec::kSuccess; }
sk_sp<SkSpecialImage> SkDisplacementMapEffect::onFilterImage(SkSpecialImage* source, const Context& ctx, SkIPoint* offset) const { SkIPoint colorOffset = SkIPoint::Make(0, 0); sk_sp<SkSpecialImage> color(this->filterInput(1, source, ctx, &colorOffset)); if (!color) { return nullptr; } SkIPoint displOffset = SkIPoint::Make(0, 0); sk_sp<SkSpecialImage> displ(this->filterInput(0, source, ctx, &displOffset)); if (!displ) { return nullptr; } const SkIRect srcBounds = SkIRect::MakeXYWH(colorOffset.x(), colorOffset.y(), color->width(), color->height()); // Both paths do bounds checking on color pixel access, we don't need to // pad the color bitmap to bounds here. SkIRect bounds; if (!this->applyCropRect(ctx, srcBounds, &bounds)) { return nullptr; } SkIRect displBounds; displ = this->applyCropRect(ctx, displ.get(), &displOffset, &displBounds); if (!displ) { return nullptr; } if (!bounds.intersect(displBounds)) { return nullptr; } const SkIRect colorBounds = bounds.makeOffset(-colorOffset.x(), -colorOffset.y()); SkVector scale = SkVector::Make(fScale, fScale); ctx.ctm().mapVectors(&scale, 1); #if SK_SUPPORT_GPU if (source->isTextureBacked()) { GrContext* context = source->getContext(); sk_sp<GrTexture> colorTexture(color->asTextureRef(context)); sk_sp<GrTexture> displTexture(displ->asTextureRef(context)); if (!colorTexture || !displTexture) { return nullptr; } GrSurfaceDesc desc; desc.fFlags = kRenderTarget_GrSurfaceFlag; desc.fWidth = bounds.width(); desc.fHeight = bounds.height(); desc.fConfig = kSkia8888_GrPixelConfig; SkAutoTUnref<GrTexture> dst(context->textureProvider()->createApproxTexture(desc)); if (!dst) { return nullptr; } GrPaint paint; SkMatrix offsetMatrix = GrCoordTransform::MakeDivByTextureWHMatrix(displTexture.get()); offsetMatrix.preTranslate(SkIntToScalar(colorOffset.fX - displOffset.fX), SkIntToScalar(colorOffset.fY - displOffset.fY)); paint.addColorFragmentProcessor( GrDisplacementMapEffect::Create(fXChannelSelector, fYChannelSelector, scale, displTexture.get(), offsetMatrix, colorTexture.get(), SkISize::Make(color->width(), color->height())))->unref(); paint.setPorterDuffXPFactory(SkXfermode::kSrc_Mode); SkMatrix matrix; matrix.setTranslate(-SkIntToScalar(colorBounds.x()), -SkIntToScalar(colorBounds.y())); SkAutoTUnref<GrDrawContext> drawContext(context->drawContext(dst->asRenderTarget())); if (!drawContext) { return nullptr; } drawContext->drawRect(GrClip::WideOpen(), paint, matrix, SkRect::Make(colorBounds)); offset->fX = bounds.left(); offset->fY = bounds.top(); return SkSpecialImage::MakeFromGpu(SkIRect::MakeWH(bounds.width(), bounds.height()), kNeedNewImageUniqueID_SpecialImage, dst); } #endif SkBitmap colorBM, displBM; if (!color->getROPixels(&colorBM) || !displ->getROPixels(&displBM)) { return nullptr; } if ((colorBM.colorType() != kN32_SkColorType) || (displBM.colorType() != kN32_SkColorType)) { return nullptr; } SkAutoLockPixels colorLock(colorBM), displLock(displBM); if (!colorBM.getPixels() || !displBM.getPixels()) { return nullptr; } SkImageInfo info = SkImageInfo::MakeN32(bounds.width(), bounds.height(), colorBM.alphaType()); SkBitmap dst; if (!dst.tryAllocPixels(info)) { return nullptr; } SkAutoLockPixels dstLock(dst); computeDisplacement(fXChannelSelector, fYChannelSelector, scale, &dst, displBM, colorOffset - displOffset, colorBM, colorBounds); offset->fX = bounds.left(); offset->fY = bounds.top(); return SkSpecialImage::MakeFromRaster(SkIRect::MakeWH(bounds.width(), bounds.height()), dst); }
SkCodec::Result SkSampledCodec::onGetAndroidPixels(const SkImageInfo& info, void* pixels, size_t rowBytes, const AndroidOptions& options) { // Create an Options struct for the codec. SkCodec::Options codecOptions; codecOptions.fZeroInitialized = options.fZeroInitialized; SkIRect* subset = options.fSubset; if (!subset || subset->size() == this->codec()->getInfo().dimensions()) { if (this->codec()->dimensionsSupported(info.dimensions())) { return this->codec()->getPixels(info, pixels, rowBytes, &codecOptions, options.fColorPtr, options.fColorCount); } // If the native codec does not support the requested scale, scale by sampling. return this->sampledDecode(info, pixels, rowBytes, options); } // We are performing a subset decode. int sampleSize = options.fSampleSize; SkISize scaledSize = this->getSampledDimensions(sampleSize); if (!this->codec()->dimensionsSupported(scaledSize)) { // If the native codec does not support the requested scale, scale by sampling. return this->sampledDecode(info, pixels, rowBytes, options); } // Calculate the scaled subset bounds. int scaledSubsetX = subset->x() / sampleSize; int scaledSubsetY = subset->y() / sampleSize; int scaledSubsetWidth = info.width(); int scaledSubsetHeight = info.height(); // Start the scanline decode. SkIRect scanlineSubset = SkIRect::MakeXYWH(scaledSubsetX, 0, scaledSubsetWidth, scaledSize.height()); codecOptions.fSubset = &scanlineSubset; SkCodec::Result result = this->codec()->startScanlineDecode(info.makeWH(scaledSize.width(), scaledSize.height()), &codecOptions, options.fColorPtr, options.fColorCount); if (SkCodec::kSuccess != result) { return result; } // At this point, we are only concerned with subsetting. Either no scale was // requested, or the this->codec() is handling the scale. switch (this->codec()->getScanlineOrder()) { case SkCodec::kTopDown_SkScanlineOrder: case SkCodec::kNone_SkScanlineOrder: { if (!this->codec()->skipScanlines(scaledSubsetY)) { this->codec()->fillIncompleteImage(info, pixels, rowBytes, options.fZeroInitialized, scaledSubsetHeight, 0); return SkCodec::kIncompleteInput; } int decodedLines = this->codec()->getScanlines(pixels, scaledSubsetHeight, rowBytes); if (decodedLines != scaledSubsetHeight) { return SkCodec::kIncompleteInput; } return SkCodec::kSuccess; } default: SkASSERT(false); return SkCodec::kUnimplemented; } }
SkCodec::Result SkSampledCodec::sampledDecode(const SkImageInfo& info, void* pixels, size_t rowBytes, const AndroidOptions& options) { // We should only call this function when sampling. SkASSERT(options.fSampleSize > 1); // Create options struct for the codec. SkCodec::Options sampledOptions; sampledOptions.fZeroInitialized = options.fZeroInitialized; // FIXME: This was already called by onGetAndroidPixels. Can we reduce that? int sampleSize = options.fSampleSize; int nativeSampleSize; SkISize nativeSize = this->accountForNativeScaling(&sampleSize, &nativeSampleSize); // Check if there is a subset. SkIRect subset; int subsetY = 0; int subsetWidth = nativeSize.width(); int subsetHeight = nativeSize.height(); if (options.fSubset) { // We will need to know about subsetting in the y-dimension in order to use the // scanline decoder. // Update the subset to account for scaling done by this->codec(). SkIRect* subsetPtr = options.fSubset; // Do the divide ourselves, instead of calling get_scaled_dimension. If // X and Y are 0, they should remain 0, rather than being upgraded to 1 // due to being smaller than the sampleSize. const int subsetX = subsetPtr->x() / nativeSampleSize; subsetY = subsetPtr->y() / nativeSampleSize; subsetWidth = get_scaled_dimension(subsetPtr->width(), nativeSampleSize); subsetHeight = get_scaled_dimension(subsetPtr->height(), nativeSampleSize); // The scanline decoder only needs to be aware of subsetting in the x-dimension. subset.setXYWH(subsetX, 0, subsetWidth, nativeSize.height()); sampledOptions.fSubset = ⊂ } // Start the scanline decode. SkCodec::Result result = this->codec()->startScanlineDecode( info.makeWH(nativeSize.width(), nativeSize.height()), &sampledOptions, options.fColorPtr, options.fColorCount); if (SkCodec::kSuccess != result) { return result; } SkSampler* sampler = this->codec()->getSampler(true); if (!sampler) { return SkCodec::kUnimplemented; } // Since we guarantee that output dimensions are always at least one (even if the sampleSize // is greater than a given dimension), the input sampleSize is not always the sampleSize that // we use in practice. const int sampleX = subsetWidth / info.width(); const int sampleY = subsetHeight / info.height(); if (sampler->setSampleX(sampleX) != info.width()) { return SkCodec::kInvalidScale; } if (get_scaled_dimension(subsetHeight, sampleY) != info.height()) { return SkCodec::kInvalidScale; } const int samplingOffsetY = get_start_coord(sampleY); const int startY = samplingOffsetY + subsetY; int dstHeight = info.height(); switch(this->codec()->getScanlineOrder()) { case SkCodec::kTopDown_SkScanlineOrder: { if (!this->codec()->skipScanlines(startY)) { this->codec()->fillIncompleteImage(info, pixels, rowBytes, options.fZeroInitialized, dstHeight, 0); return SkCodec::kIncompleteInput; } void* pixelPtr = pixels; for (int y = 0; y < dstHeight; y++) { if (1 != this->codec()->getScanlines(pixelPtr, 1, rowBytes)) { this->codec()->fillIncompleteImage(info, pixels, rowBytes, options.fZeroInitialized, dstHeight, y + 1); return SkCodec::kIncompleteInput; } if (y < dstHeight - 1) { if (!this->codec()->skipScanlines(sampleY - 1)) { this->codec()->fillIncompleteImage(info, pixels, rowBytes, options.fZeroInitialized, dstHeight, y + 1); return SkCodec::kIncompleteInput; } } pixelPtr = SkTAddOffset<void>(pixelPtr, rowBytes); } return SkCodec::kSuccess; } case SkCodec::kOutOfOrder_SkScanlineOrder: case SkCodec::kBottomUp_SkScanlineOrder: { // Note that these modes do not support subsetting. SkASSERT(0 == subsetY && nativeSize.height() == subsetHeight); int y; for (y = 0; y < nativeSize.height(); y++) { int srcY = this->codec()->nextScanline(); if (is_coord_necessary(srcY, sampleY, dstHeight)) { void* pixelPtr = SkTAddOffset<void>(pixels, rowBytes * get_dst_coord(srcY, sampleY)); if (1 != this->codec()->getScanlines(pixelPtr, 1, rowBytes)) { break; } } else { if (!this->codec()->skipScanlines(1)) { break; } } } if (nativeSize.height() == y) { return SkCodec::kSuccess; } // We handle filling uninitialized memory here instead of using this->codec(). // this->codec() does not know that we are sampling. const uint32_t fillValue = this->codec()->getFillValue(info.colorType()); const SkImageInfo fillInfo = info.makeWH(info.width(), 1); for (; y < nativeSize.height(); y++) { int srcY = this->codec()->outputScanline(y); if (!is_coord_necessary(srcY, sampleY, dstHeight)) { continue; } void* rowPtr = SkTAddOffset<void>(pixels, rowBytes * get_dst_coord(srcY, sampleY)); SkSampler::Fill(fillInfo, rowPtr, rowBytes, fillValue, options.fZeroInitialized); } return SkCodec::kIncompleteInput; } case SkCodec::kNone_SkScanlineOrder: { const int linesNeeded = subsetHeight - samplingOffsetY; SkAutoTMalloc<uint8_t> storage(linesNeeded * rowBytes); uint8_t* storagePtr = storage.get(); if (!this->codec()->skipScanlines(startY)) { this->codec()->fillIncompleteImage(info, pixels, rowBytes, options.fZeroInitialized, dstHeight, 0); return SkCodec::kIncompleteInput; } int scanlines = this->codec()->getScanlines(storagePtr, linesNeeded, rowBytes); for (int y = 0; y < dstHeight; y++) { memcpy(pixels, storagePtr, info.minRowBytes()); storagePtr += sampleY * rowBytes; pixels = SkTAddOffset<void>(pixels, rowBytes); } if (scanlines < dstHeight) { // this->codec() has already handled filling uninitialized memory. return SkCodec::kIncompleteInput; } return SkCodec::kSuccess; } default: SkASSERT(false); return SkCodec::kUnimplemented; } }
sk_sp<SkSpecialImage> SkBlurImageFilter::onFilterImage(SkSpecialImage* source, const Context& ctx, SkIPoint* offset) const { SkIPoint inputOffset = SkIPoint::Make(0, 0); sk_sp<SkSpecialImage> input(this->filterInput(0, source, ctx, &inputOffset)); if (!input) { return nullptr; } SkIRect inputBounds = SkIRect::MakeXYWH(inputOffset.fX, inputOffset.fY, input->width(), input->height()); SkIRect dstBounds; if (!this->applyCropRect(this->mapContext(ctx), inputBounds, &dstBounds)) { return nullptr; } if (!inputBounds.intersect(dstBounds)) { return nullptr; } const SkVector sigma = map_sigma(fSigma, ctx.ctm()); #if SK_SUPPORT_GPU if (input->peekTexture() && input->peekTexture()->getContext()) { if (0 == sigma.x() && 0 == sigma.y()) { offset->fX = inputBounds.x(); offset->fY = inputBounds.y(); return input->makeSubset(inputBounds.makeOffset(-inputOffset.x(), -inputOffset.y())); } GrTexture* inputTexture = input->peekTexture(); offset->fX = dstBounds.fLeft; offset->fY = dstBounds.fTop; inputBounds.offset(-inputOffset); dstBounds.offset(-inputOffset); SkRect inputBoundsF(SkRect::Make(inputBounds)); SkAutoTUnref<GrTexture> tex(SkGpuBlurUtils::GaussianBlur(inputTexture->getContext(), inputTexture, false, source->props().allowSRGBInputs(), SkRect::Make(dstBounds), &inputBoundsF, sigma.x(), sigma.y())); if (!tex) { return nullptr; } return SkSpecialImage::MakeFromGpu(source->internal_getProxy(), SkIRect::MakeWH(dstBounds.width(), dstBounds.height()), kNeedNewImageUniqueID_SpecialImage, tex, &source->props()); } #endif int kernelSizeX, kernelSizeX3, lowOffsetX, highOffsetX; int kernelSizeY, kernelSizeY3, lowOffsetY, highOffsetY; get_box3_params(sigma.x(), &kernelSizeX, &kernelSizeX3, &lowOffsetX, &highOffsetX); get_box3_params(sigma.y(), &kernelSizeY, &kernelSizeY3, &lowOffsetY, &highOffsetY); if (kernelSizeX < 0 || kernelSizeY < 0) { return nullptr; } if (kernelSizeX == 0 && kernelSizeY == 0) { offset->fX = inputBounds.x(); offset->fY = inputBounds.y(); return input->makeSubset(inputBounds.makeOffset(-inputOffset.x(), -inputOffset.y())); } SkPixmap inputPixmap; if (!input->peekPixels(&inputPixmap)) { return nullptr; } if (inputPixmap.colorType() != kN32_SkColorType) { return nullptr; } SkImageInfo info = SkImageInfo::Make(dstBounds.width(), dstBounds.height(), inputPixmap.colorType(), inputPixmap.alphaType()); SkBitmap tmp, dst; if (!tmp.tryAllocPixels(info) || !dst.tryAllocPixels(info)) { return nullptr; } SkAutoLockPixels tmpLock(tmp), dstLock(dst); offset->fX = dstBounds.fLeft; offset->fY = dstBounds.fTop; SkPMColor* t = tmp.getAddr32(0, 0); SkPMColor* d = dst.getAddr32(0, 0); int w = dstBounds.width(), h = dstBounds.height(); const SkPMColor* s = inputPixmap.addr32(inputBounds.x() - inputOffset.x(), inputBounds.y() - inputOffset.y()); inputBounds.offset(-dstBounds.x(), -dstBounds.y()); dstBounds.offset(-dstBounds.x(), -dstBounds.y()); SkIRect inputBoundsT = SkIRect::MakeLTRB(inputBounds.top(), inputBounds.left(), inputBounds.bottom(), inputBounds.right()); SkIRect dstBoundsT = SkIRect::MakeWH(dstBounds.height(), dstBounds.width()); int sw = int(inputPixmap.rowBytes() >> 2); /** * * In order to make memory accesses cache-friendly, we reorder the passes to * use contiguous memory reads wherever possible. * * For example, the 6 passes of the X-and-Y blur case are rewritten as * follows. Instead of 3 passes in X and 3 passes in Y, we perform * 2 passes in X, 1 pass in X transposed to Y on write, 2 passes in X, * then 1 pass in X transposed to Y on write. * * +----+ +----+ +----+ +---+ +---+ +---+ +----+ * + AB + ----> | AB | ----> | AB | -----> | A | ----> | A | ----> | A | -----> | AB | * +----+ blurX +----+ blurX +----+ blurXY | B | blurX | B | blurX | B | blurXY +----+ * +---+ +---+ +---+ * * In this way, two of the y-blurs become x-blurs applied to transposed * images, and all memory reads are contiguous. */ if (kernelSizeX > 0 && kernelSizeY > 0) { SkOpts::box_blur_xx(s, sw, inputBounds, t, kernelSizeX, lowOffsetX, highOffsetX, w, h); SkOpts::box_blur_xx(t, w, dstBounds, d, kernelSizeX, highOffsetX, lowOffsetX, w, h); SkOpts::box_blur_xy(d, w, dstBounds, t, kernelSizeX3, highOffsetX, highOffsetX, w, h); SkOpts::box_blur_xx(t, h, dstBoundsT, d, kernelSizeY, lowOffsetY, highOffsetY, h, w); SkOpts::box_blur_xx(d, h, dstBoundsT, t, kernelSizeY, highOffsetY, lowOffsetY, h, w); SkOpts::box_blur_xy(t, h, dstBoundsT, d, kernelSizeY3, highOffsetY, highOffsetY, h, w); } else if (kernelSizeX > 0) { SkOpts::box_blur_xx(s, sw, inputBounds, d, kernelSizeX, lowOffsetX, highOffsetX, w, h); SkOpts::box_blur_xx(d, w, dstBounds, t, kernelSizeX, highOffsetX, lowOffsetX, w, h); SkOpts::box_blur_xx(t, w, dstBounds, d, kernelSizeX3, highOffsetX, highOffsetX, w, h); } else if (kernelSizeY > 0) { SkOpts::box_blur_yx(s, sw, inputBoundsT, d, kernelSizeY, lowOffsetY, highOffsetY, h, w); SkOpts::box_blur_xx(d, h, dstBoundsT, t, kernelSizeY, highOffsetY, lowOffsetY, h, w); SkOpts::box_blur_xy(t, h, dstBoundsT, d, kernelSizeY3, highOffsetY, highOffsetY, h, w); } return SkSpecialImage::MakeFromRaster(source->internal_getProxy(), SkIRect::MakeWH(dstBounds.width(), dstBounds.height()), dst, &source->props()); }
sk_sp<SkSpecialImage> SkMagnifierImageFilter::onFilterImage(SkSpecialImage* source, const Context& ctx, SkIPoint* offset) const { SkIPoint inputOffset = SkIPoint::Make(0, 0); sk_sp<SkSpecialImage> input(this->filterInput(0, source, ctx, &inputOffset)); if (!input) { return nullptr; } const SkIRect inputBounds = SkIRect::MakeXYWH(inputOffset.x(), inputOffset.y(), input->width(), input->height()); SkIRect bounds; if (!this->applyCropRect(ctx, inputBounds, &bounds)) { return nullptr; } SkScalar invInset = fInset > 0 ? SkScalarInvert(fInset) : SK_Scalar1; SkScalar invXZoom = fSrcRect.width() / bounds.width(); SkScalar invYZoom = fSrcRect.height() / bounds.height(); #if SK_SUPPORT_GPU if (source->isTextureBacked()) { GrContext* context = source->getContext(); sk_sp<GrTexture> inputTexture(input->asTextureRef(context)); SkASSERT(inputTexture); offset->fX = bounds.left(); offset->fY = bounds.top(); bounds.offset(-inputOffset); SkScalar yOffset = inputTexture->origin() == kTopLeft_GrSurfaceOrigin ? fSrcRect.y() : inputTexture->height() - fSrcRect.height() * inputTexture->height() / bounds.height() - fSrcRect.y(); int boundsY = inputTexture->origin() == kTopLeft_GrSurfaceOrigin ? bounds.y() : inputTexture->height() - bounds.height(); SkRect effectBounds = SkRect::MakeXYWH( SkIntToScalar(bounds.x()) / inputTexture->width(), SkIntToScalar(boundsY) / inputTexture->height(), SkIntToScalar(inputTexture->width()) / bounds.width(), SkIntToScalar(inputTexture->height()) / bounds.height()); // SRGBTODO: Handle sRGB here sk_sp<GrFragmentProcessor> fp(GrMagnifierEffect::Create( inputTexture.get(), effectBounds, fSrcRect.x() / inputTexture->width(), yOffset / inputTexture->height(), invXZoom, invYZoom, bounds.width() * invInset, bounds.height() * invInset)); if (!fp) { return nullptr; } return DrawWithFP(context, std::move(fp), bounds); } #endif SkBitmap inputBM; if (!input->getROPixels(&inputBM)) { return nullptr; } if ((inputBM.colorType() != kN32_SkColorType) || (fSrcRect.width() >= inputBM.width()) || (fSrcRect.height() >= inputBM.height())) { return nullptr; } SkAutoLockPixels alp(inputBM); SkASSERT(inputBM.getPixels()); if (!inputBM.getPixels() || inputBM.width() <= 0 || inputBM.height() <= 0) { return nullptr; } const SkImageInfo info = SkImageInfo::MakeN32Premul(bounds.width(), bounds.height()); SkBitmap dst; if (!dst.tryAllocPixels(info)) { return nullptr; } SkAutoLockPixels dstLock(dst); SkColor* dptr = dst.getAddr32(0, 0); int dstWidth = dst.width(), dstHeight = dst.height(); for (int y = 0; y < dstHeight; ++y) { for (int x = 0; x < dstWidth; ++x) { SkScalar x_dist = SkMin32(x, dstWidth - x - 1) * invInset; SkScalar y_dist = SkMin32(y, dstHeight - y - 1) * invInset; SkScalar weight = 0; static const SkScalar kScalar2 = SkScalar(2); // To create a smooth curve at the corners, we need to work on // a square twice the size of the inset. if (x_dist < kScalar2 && y_dist < kScalar2) { x_dist = kScalar2 - x_dist; y_dist = kScalar2 - y_dist; SkScalar dist = SkScalarSqrt(SkScalarSquare(x_dist) + SkScalarSquare(y_dist)); dist = SkMaxScalar(kScalar2 - dist, 0); weight = SkMinScalar(SkScalarSquare(dist), SK_Scalar1); } else { SkScalar sqDist = SkMinScalar(SkScalarSquare(x_dist), SkScalarSquare(y_dist)); weight = SkMinScalar(sqDist, SK_Scalar1); } SkScalar x_interp = SkScalarMul(weight, (fSrcRect.x() + x * invXZoom)) + (SK_Scalar1 - weight) * x; SkScalar y_interp = SkScalarMul(weight, (fSrcRect.y() + y * invYZoom)) + (SK_Scalar1 - weight) * y; int x_val = SkTPin(bounds.x() + SkScalarFloorToInt(x_interp), 0, inputBM.width() - 1); int y_val = SkTPin(bounds.y() + SkScalarFloorToInt(y_interp), 0, inputBM.height() - 1); *dptr = *inputBM.getAddr32(x_val, y_val); dptr++; } } offset->fX = bounds.left(); offset->fY = bounds.top(); return SkSpecialImage::MakeFromRaster(SkIRect::MakeWH(bounds.width(), bounds.height()), dst); }
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); SkShader* matrixShader = SkShader::CreateLocalMatrixShader(shader, mat); SkSafeUnref(shader); SkSafeUnref(aPaint.setShader(matrixShader)); } } 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); SkShader* matrixShader = SkShader::CreateLocalMatrixShader(shader, mat); SkSafeUnref(shader); SkSafeUnref(aPaint.setShader(matrixShader)); } } else { aPaint.setColor(SkColorSetARGB(0, 0, 0, 0)); } break; } case PatternType::SURFACE: { const SurfacePattern& pat = static_cast<const SurfacePattern&>(aPattern); aTmpBitmap = GetBitmapForSurface(pat.mSurface); SkBitmap& bitmap = aTmpBitmap.mBitmap; SkMatrix mat; GfxMatrixToSkiaMatrix(pat.mMatrix, mat); if (!pat.mSamplingRect.IsEmpty()) { SkIRect rect = IntRectToSkIRect(pat.mSamplingRect); bitmap.extractSubset(&bitmap, rect); mat.preTranslate(rect.x(), rect.y()); } SkShader::TileMode mode = ExtendModeToTileMode(pat.mExtendMode); SkShader* shader = SkShader::CreateBitmapShader(bitmap, mode, mode); SkShader* matrixShader = SkShader::CreateLocalMatrixShader(shader, mat); SkSafeUnref(shader); SkSafeUnref(aPaint.setShader(matrixShader)); if (pat.mFilter == Filter::POINT) { aPaint.setFilterLevel(SkPaint::kNone_FilterLevel); } break; } } }
bool SkBlurImageFilter::onFilterImageDeprecated(Proxy* proxy, const SkBitmap& source, const Context& ctx, SkBitmap* dst, SkIPoint* offset) const { SkBitmap src = source; SkIPoint srcOffset = SkIPoint::Make(0, 0); if (!this->filterInputDeprecated(0, proxy, source, ctx, &src, &srcOffset)) { return false; } if (src.colorType() != kN32_SkColorType) { return false; } SkIRect srcBounds = src.bounds(); srcBounds.offset(srcOffset); SkIRect dstBounds; if (!this->applyCropRect(this->mapContext(ctx), srcBounds, &dstBounds)) { return false; } if (!srcBounds.intersect(dstBounds)) { return false; } SkVector sigma = map_sigma(fSigma, ctx.ctm()); int kernelSizeX, kernelSizeX3, lowOffsetX, highOffsetX; int kernelSizeY, kernelSizeY3, lowOffsetY, highOffsetY; getBox3Params(sigma.x(), &kernelSizeX, &kernelSizeX3, &lowOffsetX, &highOffsetX); getBox3Params(sigma.y(), &kernelSizeY, &kernelSizeY3, &lowOffsetY, &highOffsetY); if (kernelSizeX < 0 || kernelSizeY < 0) { return false; } if (kernelSizeX == 0 && kernelSizeY == 0) { src.extractSubset(dst, srcBounds); offset->fX = srcBounds.x(); offset->fY = srcBounds.y(); return true; } SkAutoLockPixels alp(src); if (!src.getPixels()) { return false; } SkAutoTUnref<SkBaseDevice> device(proxy->createDevice(dstBounds.width(), dstBounds.height())); if (!device) { return false; } *dst = device->accessBitmap(false); SkAutoLockPixels alp_dst(*dst); SkAutoTUnref<SkBaseDevice> tempDevice(proxy->createDevice(dst->width(), dst->height())); if (!tempDevice) { return false; } SkBitmap temp = tempDevice->accessBitmap(false); SkAutoLockPixels alpTemp(temp); offset->fX = dstBounds.fLeft; offset->fY = dstBounds.fTop; SkPMColor* t = temp.getAddr32(0, 0); SkPMColor* d = dst->getAddr32(0, 0); int w = dstBounds.width(), h = dstBounds.height(); const SkPMColor* s = src.getAddr32(srcBounds.x() - srcOffset.x(), srcBounds.y() - srcOffset.y()); srcBounds.offset(-dstBounds.x(), -dstBounds.y()); dstBounds.offset(-dstBounds.x(), -dstBounds.y()); SkIRect srcBoundsT = SkIRect::MakeLTRB(srcBounds.top(), srcBounds.left(), srcBounds.bottom(), srcBounds.right()); SkIRect dstBoundsT = SkIRect::MakeWH(dstBounds.height(), dstBounds.width()); int sw = src.rowBytesAsPixels(); /** * * In order to make memory accesses cache-friendly, we reorder the passes to * use contiguous memory reads wherever possible. * * For example, the 6 passes of the X-and-Y blur case are rewritten as * follows. Instead of 3 passes in X and 3 passes in Y, we perform * 2 passes in X, 1 pass in X transposed to Y on write, 2 passes in X, * then 1 pass in X transposed to Y on write. * * +----+ +----+ +----+ +---+ +---+ +---+ +----+ * + AB + ----> | AB | ----> | AB | -----> | A | ----> | A | ----> | A | -----> | AB | * +----+ blurX +----+ blurX +----+ blurXY | B | blurX | B | blurX | B | blurXY +----+ * +---+ +---+ +---+ * * In this way, two of the y-blurs become x-blurs applied to transposed * images, and all memory reads are contiguous. */ if (kernelSizeX > 0 && kernelSizeY > 0) { SkOpts::box_blur_xx(s, sw, srcBounds, t, kernelSizeX, lowOffsetX, highOffsetX, w, h); SkOpts::box_blur_xx(t, w, dstBounds, d, kernelSizeX, highOffsetX, lowOffsetX, w, h); SkOpts::box_blur_xy(d, w, dstBounds, t, kernelSizeX3, highOffsetX, highOffsetX, w, h); SkOpts::box_blur_xx(t, h, dstBoundsT, d, kernelSizeY, lowOffsetY, highOffsetY, h, w); SkOpts::box_blur_xx(d, h, dstBoundsT, t, kernelSizeY, highOffsetY, lowOffsetY, h, w); SkOpts::box_blur_xy(t, h, dstBoundsT, d, kernelSizeY3, highOffsetY, highOffsetY, h, w); } else if (kernelSizeX > 0) { SkOpts::box_blur_xx(s, sw, srcBounds, d, kernelSizeX, lowOffsetX, highOffsetX, w, h); SkOpts::box_blur_xx(d, w, dstBounds, t, kernelSizeX, highOffsetX, lowOffsetX, w, h); SkOpts::box_blur_xx(t, w, dstBounds, d, kernelSizeX3, highOffsetX, highOffsetX, w, h); } else if (kernelSizeY > 0) { SkOpts::box_blur_yx(s, sw, srcBoundsT, d, kernelSizeY, lowOffsetY, highOffsetY, h, w); SkOpts::box_blur_xx(d, h, dstBoundsT, t, kernelSizeY, highOffsetY, lowOffsetY, h, w); SkOpts::box_blur_xy(t, h, dstBoundsT, d, kernelSizeY3, highOffsetY, highOffsetY, h, w); } return true; }
sk_sp<GrDrawContext> GaussianBlur(GrContext* context, GrTexture* origSrc, sk_sp<SkColorSpace> colorSpace, const SkIRect& dstBounds, const SkIRect* srcBounds, float sigmaX, float sigmaY, SkBackingFit fit) { SkASSERT(context); SkIRect clearRect; int scaleFactorX, radiusX; int scaleFactorY, radiusY; int maxTextureSize = context->caps()->maxTextureSize(); sigmaX = adjust_sigma(sigmaX, maxTextureSize, &scaleFactorX, &radiusX); sigmaY = adjust_sigma(sigmaY, maxTextureSize, &scaleFactorY, &radiusY); SkASSERT(sigmaX || sigmaY); SkIPoint srcOffset = SkIPoint::Make(-dstBounds.x(), -dstBounds.y()); SkIRect localDstBounds = SkIRect::MakeWH(dstBounds.width(), dstBounds.height()); SkIRect localSrcBounds; SkIRect srcRect; if (srcBounds) { srcRect = localSrcBounds = *srcBounds; srcRect.offset(srcOffset); srcBounds = &localSrcBounds; } else { srcRect = localDstBounds; } scale_irect_roundout(&srcRect, 1.0f / scaleFactorX, 1.0f / scaleFactorY); scale_irect(&srcRect, scaleFactorX, scaleFactorY); // setup new clip GrFixedClip clip(localDstBounds); sk_sp<GrTexture> srcTexture(sk_ref_sp(origSrc)); SkASSERT(kBGRA_8888_GrPixelConfig == srcTexture->config() || kRGBA_8888_GrPixelConfig == srcTexture->config() || kSRGBA_8888_GrPixelConfig == srcTexture->config() || kSBGRA_8888_GrPixelConfig == srcTexture->config() || kRGBA_half_GrPixelConfig == srcTexture->config() || kAlpha_8_GrPixelConfig == srcTexture->config()); const int width = dstBounds.width(); const int height = dstBounds.height(); const GrPixelConfig config = srcTexture->config(); sk_sp<GrDrawContext> dstDrawContext(context->makeDrawContext(fit, width, height, config, colorSpace, 0, kDefault_GrSurfaceOrigin)); if (!dstDrawContext) { return nullptr; } // For really small blurs (certainly no wider than 5x5 on desktop gpus) it is faster to just // launch a single non separable kernel vs two launches if (sigmaX > 0.0f && sigmaY > 0.0f && (2 * radiusX + 1) * (2 * radiusY + 1) <= MAX_KERNEL_SIZE) { // We shouldn't be scaling because this is a small size blur SkASSERT((1 == scaleFactorX) && (1 == scaleFactorY)); convolve_gaussian_2d(dstDrawContext.get(), clip, localDstBounds, srcOffset, srcTexture.get(), radiusX, radiusY, sigmaX, sigmaY, srcBounds); return dstDrawContext; } sk_sp<GrDrawContext> tmpDrawContext(context->makeDrawContext(fit, width, height, config, colorSpace, 0, kDefault_GrSurfaceOrigin)); if (!tmpDrawContext) { return nullptr; } sk_sp<GrDrawContext> srcDrawContext; SkASSERT(SkIsPow2(scaleFactorX) && SkIsPow2(scaleFactorY)); for (int i = 1; i < scaleFactorX || i < scaleFactorY; i *= 2) { GrPaint paint; paint.setGammaCorrect(dstDrawContext->isGammaCorrect()); SkMatrix matrix; matrix.setIDiv(srcTexture->width(), srcTexture->height()); SkIRect dstRect(srcRect); if (srcBounds && i == 1) { SkRect domain; matrix.mapRect(&domain, SkRect::Make(*srcBounds)); domain.inset((i < scaleFactorX) ? SK_ScalarHalf / srcTexture->width() : 0.0f, (i < scaleFactorY) ? SK_ScalarHalf / srcTexture->height() : 0.0f); sk_sp<GrFragmentProcessor> fp(GrTextureDomainEffect::Make( srcTexture.get(), nullptr, matrix, domain, GrTextureDomain::kDecal_Mode, GrTextureParams::kBilerp_FilterMode)); paint.addColorFragmentProcessor(std::move(fp)); srcRect.offset(-srcOffset); srcOffset.set(0, 0); } else { GrTextureParams params(SkShader::kClamp_TileMode, GrTextureParams::kBilerp_FilterMode); paint.addColorTextureProcessor(srcTexture.get(), nullptr, matrix, params); } paint.setPorterDuffXPFactory(SkBlendMode::kSrc); shrink_irect_by_2(&dstRect, i < scaleFactorX, i < scaleFactorY); dstDrawContext->fillRectToRect(clip, paint, SkMatrix::I(), SkRect::Make(dstRect), SkRect::Make(srcRect)); srcDrawContext = dstDrawContext; srcRect = dstRect; srcTexture = srcDrawContext->asTexture(); dstDrawContext.swap(tmpDrawContext); localSrcBounds = srcRect; } srcRect = localDstBounds; scale_irect_roundout(&srcRect, 1.0f / scaleFactorX, 1.0f / scaleFactorY); if (sigmaX > 0.0f) { if (scaleFactorX > 1) { SkASSERT(srcDrawContext); // Clear out a radius to the right of the srcRect to prevent the // X convolution from reading garbage. clearRect = SkIRect::MakeXYWH(srcRect.fRight, srcRect.fTop, radiusX, srcRect.height()); srcDrawContext->clear(&clearRect, 0x0, false); } convolve_gaussian(dstDrawContext.get(), clip, srcRect, srcTexture.get(), Gr1DKernelEffect::kX_Direction, radiusX, sigmaX, srcBounds, srcOffset); srcDrawContext = dstDrawContext; srcTexture = srcDrawContext->asTexture(); srcRect.offsetTo(0, 0); dstDrawContext.swap(tmpDrawContext); localSrcBounds = srcRect; srcOffset.set(0, 0); } if (sigmaY > 0.0f) { if (scaleFactorY > 1 || sigmaX > 0.0f) { SkASSERT(srcDrawContext); // Clear out a radius below the srcRect to prevent the Y // convolution from reading garbage. clearRect = SkIRect::MakeXYWH(srcRect.fLeft, srcRect.fBottom, srcRect.width(), radiusY); srcDrawContext->clear(&clearRect, 0x0, false); } convolve_gaussian(dstDrawContext.get(), clip, srcRect, srcTexture.get(), Gr1DKernelEffect::kY_Direction, radiusY, sigmaY, srcBounds, srcOffset); srcDrawContext = dstDrawContext; srcRect.offsetTo(0, 0); dstDrawContext.swap(tmpDrawContext); } SkASSERT(srcDrawContext); srcTexture = nullptr; // we don't use this from here on out if (scaleFactorX > 1 || scaleFactorY > 1) { // Clear one pixel to the right and below, to accommodate bilinear upsampling. clearRect = SkIRect::MakeXYWH(srcRect.fLeft, srcRect.fBottom, srcRect.width() + 1, 1); srcDrawContext->clear(&clearRect, 0x0, false); clearRect = SkIRect::MakeXYWH(srcRect.fRight, srcRect.fTop, 1, srcRect.height()); srcDrawContext->clear(&clearRect, 0x0, false); SkMatrix matrix; matrix.setIDiv(srcDrawContext->width(), srcDrawContext->height()); GrPaint paint; paint.setGammaCorrect(dstDrawContext->isGammaCorrect()); // FIXME: this should be mitchell, not bilinear. GrTextureParams params(SkShader::kClamp_TileMode, GrTextureParams::kBilerp_FilterMode); sk_sp<GrTexture> tex(srcDrawContext->asTexture()); paint.addColorTextureProcessor(tex.get(), nullptr, matrix, params); paint.setPorterDuffXPFactory(SkBlendMode::kSrc); SkIRect dstRect(srcRect); scale_irect(&dstRect, scaleFactorX, scaleFactorY); dstDrawContext->fillRectToRect(clip, paint, SkMatrix::I(), SkRect::Make(dstRect), SkRect::Make(srcRect)); srcDrawContext = dstDrawContext; srcRect = dstRect; dstDrawContext.swap(tmpDrawContext); } return srcDrawContext; }