Fix tile bitmap code in SkGpuDevice to compute correct local coords.

This fixes a bug when using an alpha bitmap/image with a SkShader where the SkShader's GrFragmentProcessor would receive incorrect coordinates.

BUG=skia:5643

GOLD_TRYBOT_URL= https://gold.skia.org/search?issue=2249163004

Review-Url: https://codereview.chromium.org/2249163004
This commit is contained in:
bsalomon 2016-08-17 09:02:09 -07:00 committed by Commit bot
parent 31df31c12f
commit e553b64bca
2 changed files with 87 additions and 129 deletions

View File

@ -673,12 +673,13 @@ static int determine_tile_size(const SkIRect& src, int maxTileSize) {
static void determine_clipped_src_rect(int width, int height, static void determine_clipped_src_rect(int width, int height,
const GrClip& clip, const GrClip& clip,
const SkMatrix& viewMatrix, const SkMatrix& viewMatrix,
const SkMatrix& srcToDstRect,
const SkISize& imageSize, const SkISize& imageSize,
const SkRect* srcRectPtr, const SkRect* srcRectPtr,
SkIRect* clippedSrcIRect) { SkIRect* clippedSrcIRect) {
clip.getConservativeBounds(width, height, clippedSrcIRect, nullptr); clip.getConservativeBounds(width, height, clippedSrcIRect, nullptr);
SkMatrix inv; SkMatrix inv = SkMatrix::Concat(viewMatrix, srcToDstRect);
if (!viewMatrix.invert(&inv)) { if (!inv.invert(&inv)) {
clippedSrcIRect->setEmpty(); clippedSrcIRect->setEmpty();
return; return;
} }
@ -701,6 +702,7 @@ static void determine_clipped_src_rect(int width, int height,
bool SkGpuDevice::shouldTileImageID(uint32_t imageID, const SkIRect& imageRect, bool SkGpuDevice::shouldTileImageID(uint32_t imageID, const SkIRect& imageRect,
const SkMatrix& viewMatrix, const SkMatrix& viewMatrix,
const SkMatrix& srcToDstRect,
const GrTextureParams& params, const GrTextureParams& params,
const SkRect* srcRectPtr, const SkRect* srcRectPtr,
int maxTileSize, int maxTileSize,
@ -709,9 +711,8 @@ bool SkGpuDevice::shouldTileImageID(uint32_t imageID, const SkIRect& imageRect,
ASSERT_SINGLE_OWNER ASSERT_SINGLE_OWNER
// if it's larger than the max tile size, then we have no choice but tiling. // if it's larger than the max tile size, then we have no choice but tiling.
if (imageRect.width() > maxTileSize || imageRect.height() > maxTileSize) { if (imageRect.width() > maxTileSize || imageRect.height() > maxTileSize) {
determine_clipped_src_rect(fDrawContext->width(), fDrawContext->height(), determine_clipped_src_rect(fDrawContext->width(), fDrawContext->height(), fClip, viewMatrix,
fClip, viewMatrix, imageRect.size(), srcToDstRect, imageRect.size(), srcRectPtr, clippedSubset);
srcRectPtr, clippedSubset);
*tileSize = determine_tile_size(*clippedSubset, maxTileSize); *tileSize = determine_tile_size(*clippedSubset, maxTileSize);
return true; return true;
} }
@ -738,9 +739,8 @@ bool SkGpuDevice::shouldTileImageID(uint32_t imageID, const SkIRect& imageRect,
// Figure out how much of the src we will need based on the src rect and clipping. Reject if // Figure out how much of the src we will need based on the src rect and clipping. Reject if
// tiling memory savings would be < 50%. // tiling memory savings would be < 50%.
determine_clipped_src_rect(fDrawContext->width(), fDrawContext->height(), determine_clipped_src_rect(fDrawContext->width(), fDrawContext->height(), fClip, viewMatrix,
fClip, viewMatrix, imageRect.size(), srcRectPtr, srcToDstRect, imageRect.size(), srcRectPtr, clippedSubset);
clippedSubset);
*tileSize = kBmpSmallTileSize; // already know whole bitmap fits in one max sized tile. *tileSize = kBmpSmallTileSize; // already know whole bitmap fits in one max sized tile.
size_t usedTileBytes = get_tile_count(*clippedSubset, kBmpSmallTileSize) * size_t usedTileBytes = get_tile_count(*clippedSubset, kBmpSmallTileSize) *
kBmpSmallTileSize * kBmpSmallTileSize; kBmpSmallTileSize * kBmpSmallTileSize;
@ -748,21 +748,10 @@ bool SkGpuDevice::shouldTileImageID(uint32_t imageID, const SkIRect& imageRect,
return usedTileBytes < 2 * bmpSize; return usedTileBytes < 2 * bmpSize;
} }
bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
const SkMatrix& viewMatrix,
const GrTextureParams& params,
const SkRect* srcRectPtr,
int maxTileSize,
int* tileSize,
SkIRect* clippedSrcRect) const {
ASSERT_SINGLE_OWNER
return this->shouldTileImageID(bitmap.getGenerationID(), bitmap.getSubset(), viewMatrix, params,
srcRectPtr, maxTileSize, tileSize, clippedSrcRect);
}
bool SkGpuDevice::shouldTileImage(const SkImage* image, const SkRect* srcRectPtr, bool SkGpuDevice::shouldTileImage(const SkImage* image, const SkRect* srcRectPtr,
SkCanvas::SrcRectConstraint constraint, SkFilterQuality quality, SkCanvas::SrcRectConstraint constraint, SkFilterQuality quality,
const SkMatrix& viewMatrix) const { const SkMatrix& viewMatrix,
const SkMatrix& srcToDstRect) const {
ASSERT_SINGLE_OWNER ASSERT_SINGLE_OWNER
// if image is explictly texture backed then just use the texture // if image is explictly texture backed then just use the texture
if (as_IB(image)->peekTexture()) { if (as_IB(image)->peekTexture()) {
@ -772,7 +761,7 @@ bool SkGpuDevice::shouldTileImage(const SkImage* image, const SkRect* srcRectPtr
GrTextureParams params; GrTextureParams params;
bool doBicubic; bool doBicubic;
GrTextureParams::FilterMode textureFilterMode = GrTextureParams::FilterMode textureFilterMode =
GrSkFilterQualityToGrFilterMode(quality, viewMatrix, SkMatrix::I(), &doBicubic); GrSkFilterQualityToGrFilterMode(quality, viewMatrix, srcToDstRect, &doBicubic);
int tileFilterPad; int tileFilterPad;
if (doBicubic) { if (doBicubic) {
@ -790,8 +779,9 @@ bool SkGpuDevice::shouldTileImage(const SkImage* image, const SkRect* srcRectPtr
int outTileSize; int outTileSize;
SkIRect outClippedSrcRect; SkIRect outClippedSrcRect;
return this->shouldTileImageID(image->unique(), image->bounds(), viewMatrix, params, srcRectPtr, return this->shouldTileImageID(image->unique(), image->bounds(), viewMatrix, srcToDstRect,
maxTileSize, &outTileSize, &outClippedSrcRect); params, srcRectPtr, maxTileSize, &outTileSize,
&outClippedSrcRect);
} }
void SkGpuDevice::drawBitmap(const SkDraw& origDraw, void SkGpuDevice::drawBitmap(const SkDraw& origDraw,
@ -837,10 +827,12 @@ void SkGpuDevice::drawBitmap(const SkDraw& origDraw,
params.setFilterMode(textureFilterMode); params.setFilterMode(textureFilterMode);
int maxTileSizeForFilter = fContext->caps()->maxTileSize() - 2 * tileFilterPad; int maxTileSizeForFilter = fContext->caps()->maxTileSize() - 2 * tileFilterPad;
if (this->shouldTileBitmap(bitmap, viewMatrix, params, &srcRect, if (this->shouldTileImageID(bitmap.getGenerationID(), bitmap.getSubset(), viewMatrix,
maxTileSizeForFilter, &tileSize, &clippedSrcRect)) { SkMatrix::I(), params, &srcRect, maxTileSizeForFilter,
this->drawTiledBitmap(bitmap, viewMatrix, srcRect, clippedSrcRect, params, paint, &tileSize, &clippedSrcRect)) {
SkCanvas::kStrict_SrcRectConstraint, tileSize, doBicubic); this->drawTiledBitmap(bitmap, viewMatrix, SkMatrix::I(), srcRect, clippedSrcRect,
params, paint, SkCanvas::kStrict_SrcRectConstraint, tileSize,
doBicubic);
return; return;
} }
} }
@ -886,6 +878,7 @@ static inline void clamped_outset_with_offset(SkIRect* iRect,
// been determined to be too large to fit in VRAM // been determined to be too large to fit in VRAM
void SkGpuDevice::drawTiledBitmap(const SkBitmap& bitmap, void SkGpuDevice::drawTiledBitmap(const SkBitmap& bitmap,
const SkMatrix& viewMatrix, const SkMatrix& viewMatrix,
const SkMatrix& dstMatrix,
const SkRect& srcRect, const SkRect& srcRect,
const SkIRect& clippedSrcIRect, const SkIRect& clippedSrcIRect,
const GrTextureParams& params, const GrTextureParams& params,
@ -933,18 +926,13 @@ void SkGpuDevice::drawTiledBitmap(const SkBitmap& bitmap,
continue; continue;
} }
SkBitmap tmpB;
SkIRect iTileR; SkIRect iTileR;
tileR.roundOut(&iTileR); tileR.roundOut(&iTileR);
SkPoint offset = SkPoint::Make(SkIntToScalar(iTileR.fLeft), SkVector offset = SkPoint::Make(SkIntToScalar(iTileR.fLeft),
SkIntToScalar(iTileR.fTop)); SkIntToScalar(iTileR.fTop));
SkRect rectToDraw = SkRect::MakeXYWH(offset.fX, offset.fY,
// Adjust the context matrix to draw at the right x,y in device space tileR.width(), tileR.height());
SkMatrix viewM = viewMatrix; dstMatrix.mapRect(&rectToDraw);
SkMatrix tmpM;
tmpM.setTranslate(offset.fX - srcRect.fLeft, offset.fY - srcRect.fTop);
viewM.preConcat(tmpM);
if (GrTextureParams::kNone_FilterMode != params.filterMode() || bicubic) { if (GrTextureParams::kNone_FilterMode != params.filterMode() || bicubic) {
SkIRect iClampRect; SkIRect iClampRect;
@ -962,14 +950,16 @@ void SkGpuDevice::drawTiledBitmap(const SkBitmap& bitmap,
clamped_outset_with_offset(&iTileR, outset, &offset, iClampRect); clamped_outset_with_offset(&iTileR, outset, &offset, iClampRect);
} }
SkBitmap tmpB;
if (bitmap.extractSubset(&tmpB, iTileR)) { if (bitmap.extractSubset(&tmpB, iTileR)) {
// now offset it to make it "local" to our tmp bitmap // now offset it to make it "local" to our tmp bitmap
tileR.offset(-offset.fX, -offset.fY); tileR.offset(-offset.fX, -offset.fY);
GrTextureParams paramsTemp = params; GrTextureParams paramsTemp = params;
// de-optimized this determination // de-optimized this determination
bool needsTextureDomain = true; bool needsTextureDomain = true;
this->internalDrawBitmap(tmpB, this->drawBitmapTile(tmpB,
viewM, viewMatrix,
rectToDraw,
tileR, tileR,
paramsTemp, paramsTemp,
*paint, *paint,
@ -981,15 +971,9 @@ void SkGpuDevice::drawTiledBitmap(const SkBitmap& bitmap,
} }
} }
/* void SkGpuDevice::drawBitmapTile(const SkBitmap& bitmap,
* This is called by drawBitmap(), which has to handle images that may be too
* large to be represented by a single texture.
*
* internalDrawBitmap assumes that the specified bitmap will fit in a texture
* and that non-texture portion of the GrPaint has already been setup.
*/
void SkGpuDevice::internalDrawBitmap(const SkBitmap& bitmap,
const SkMatrix& viewMatrix, const SkMatrix& viewMatrix,
const SkRect& dstRect,
const SkRect& srcRect, const SkRect& srcRect,
const GrTextureParams& params, const GrTextureParams& params,
const SkPaint& paint, const SkPaint& paint,
@ -1011,26 +995,14 @@ void SkGpuDevice::internalDrawBitmap(const SkBitmap& bitmap,
sk_sp<GrColorSpaceXform> colorSpaceXform = sk_sp<GrColorSpaceXform> colorSpaceXform =
GrColorSpaceXform::Make(bitmap.colorSpace(), fDrawContext->getColorSpace()); GrColorSpaceXform::Make(bitmap.colorSpace(), fDrawContext->getColorSpace());
SkRect dstRect = {0, 0, srcRect.width(), srcRect.height() };
SkRect paintRect; SkScalar iw = 1.f / texture->width();
SkScalar wInv = SkScalarInvert(SkIntToScalar(texture->width())); SkScalar ih = 1.f / texture->height();
SkScalar hInv = SkScalarInvert(SkIntToScalar(texture->height()));
paintRect.setLTRB(SkScalarMul(srcRect.fLeft, wInv),
SkScalarMul(srcRect.fTop, hInv),
SkScalarMul(srcRect.fRight, wInv),
SkScalarMul(srcRect.fBottom, hInv));
SkMatrix texMatrix; SkMatrix texMatrix;
texMatrix.reset(); // Compute a matrix that maps the rect we will draw to the src rect.
if (kAlpha_8_SkColorType == bitmap.colorType() && paint.getShader()) { texMatrix.setRectToRect(dstRect, srcRect, SkMatrix::kStart_ScaleToFit);
// In cases where we are doing an A8 bitmap draw with a shader installed, we cannot use texMatrix.postScale(iw, ih);
// local coords with the bitmap draw since it may mess up texture look ups for the shader.
// Thus we need to pass in the transform matrix directly to the texture processor used for
// the bitmap draw.
texMatrix.setScale(wInv, hInv);
}
SkRect textureDomain = SkRect::MakeEmpty();
// Construct a GrPaint by setting the bitmap texture as the first effect and then configuring // Construct a GrPaint by setting the bitmap texture as the first effect and then configuring
// the rest from the SkPaint. // the rest from the SkPaint.
@ -1038,28 +1010,25 @@ void SkGpuDevice::internalDrawBitmap(const SkBitmap& bitmap,
if (needsTextureDomain && (SkCanvas::kStrict_SrcRectConstraint == constraint)) { if (needsTextureDomain && (SkCanvas::kStrict_SrcRectConstraint == constraint)) {
// Use a constrained texture domain to avoid color bleeding // Use a constrained texture domain to avoid color bleeding
SkScalar left, top, right, bottom; SkRect domain;
if (srcRect.width() > SK_Scalar1) { if (srcRect.width() > SK_Scalar1) {
SkScalar border = SK_ScalarHalf / texture->width(); domain.fLeft = (srcRect.fLeft + 0.5f) * iw;
left = paintRect.left() + border; domain.fRight = (srcRect.fRight - 0.5f) * iw;
right = paintRect.right() - border;
} else { } else {
left = right = SkScalarHalf(paintRect.left() + paintRect.right()); domain.fLeft = domain.fRight = srcRect.centerX() * iw;
} }
if (srcRect.height() > SK_Scalar1) { if (srcRect.height() > SK_Scalar1) {
SkScalar border = SK_ScalarHalf / texture->height(); domain.fTop = (srcRect.fTop + 0.5f) * ih;
top = paintRect.top() + border; domain.fBottom = (srcRect.fBottom - 0.5f) * ih;
bottom = paintRect.bottom() - border;
} else { } else {
top = bottom = SkScalarHalf(paintRect.top() + paintRect.bottom()); domain.fTop = domain.fBottom = srcRect.centerY() * ih;
} }
textureDomain.setLTRB(left, top, right, bottom);
if (bicubic) { if (bicubic) {
fp = GrBicubicEffect::Make(texture.get(), std::move(colorSpaceXform), texMatrix, fp = GrBicubicEffect::Make(texture.get(), std::move(colorSpaceXform), texMatrix,
textureDomain); domain);
} else { } else {
fp = GrTextureDomainEffect::Make(texture.get(), std::move(colorSpaceXform), texMatrix, fp = GrTextureDomainEffect::Make(texture.get(), std::move(colorSpaceXform), texMatrix,
textureDomain, GrTextureDomain::kClamp_Mode, domain, GrTextureDomain::kClamp_Mode,
params.filterMode()); params.filterMode());
} }
} else if (bicubic) { } else if (bicubic) {
@ -1077,13 +1046,7 @@ void SkGpuDevice::internalDrawBitmap(const SkBitmap& bitmap,
return; return;
} }
if (kAlpha_8_SkColorType == bitmap.colorType() && paint.getShader()) {
// We don't have local coords in this case and have previously set the transform
// matrices directly on the texture processor.
fDrawContext->drawRect(fClip, grPaint, viewMatrix, dstRect); fDrawContext->drawRect(fClip, grPaint, viewMatrix, dstRect);
} else {
fDrawContext->fillRectToRect(fClip, grPaint, viewMatrix, dstRect, paintRect);
}
} }
void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap, void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
@ -1248,15 +1211,11 @@ void SkGpuDevice::drawBitmapRect(const SkDraw& draw, const SkBitmap& bitmap,
params.setFilterMode(textureFilterMode); params.setFilterMode(textureFilterMode);
int maxTileSizeForFilter = fContext->caps()->maxTileSize() - 2 * tileFilterPad; int maxTileSizeForFilter = fContext->caps()->maxTileSize() - 2 * tileFilterPad;
// Fold the dst rect into the view matrix. This is only OK because we don't get here if if (this->shouldTileImageID(bitmap.getGenerationID(), bitmap.getSubset(), *draw.fMatrix,
// we have a mask filter. srcToDstMatrix, params, src, maxTileSizeForFilter, &tileSize,
SkMatrix viewMatrix = *draw.fMatrix; &clippedSrcRect)) {
viewMatrix.preTranslate(dst->fLeft, dst->fTop); this->drawTiledBitmap(bitmap, *draw.fMatrix, srcToDstMatrix, *src, clippedSrcRect,
viewMatrix.preScale(dst->width()/src->width(), dst->height()/src->height()); params, paint, constraint, tileSize, doBicubic);
if (this->shouldTileBitmap(bitmap, viewMatrix, params, src,
maxTileSizeForFilter, &tileSize, &clippedSrcRect)) {
this->drawTiledBitmap(bitmap, viewMatrix, *src, clippedSrcRect, params, paint,
constraint, tileSize, doBicubic);
return; return;
} }
} }
@ -1365,7 +1324,7 @@ void SkGpuDevice::drawImage(const SkDraw& draw, const SkImage* image, SkScalar x
} else { } else {
SkBitmap bm; SkBitmap bm;
if (this->shouldTileImage(image, nullptr, SkCanvas::kFast_SrcRectConstraint, if (this->shouldTileImage(image, nullptr, SkCanvas::kFast_SrcRectConstraint,
paint.getFilterQuality(), *draw.fMatrix)) { paint.getFilterQuality(), *draw.fMatrix, SkMatrix::I())) {
// only support tiling as bitmap at the moment, so force raster-version // only support tiling as bitmap at the moment, so force raster-version
if (!as_IB(image)->getROPixels(&bm)) { if (!as_IB(image)->getROPixels(&bm)) {
return; return;
@ -1393,10 +1352,11 @@ void SkGpuDevice::drawImageRect(const SkDraw& draw, const SkImage* image, const
return; return;
} }
SkBitmap bm; SkBitmap bm;
SkMatrix totalMatrix = *draw.fMatrix; SkMatrix srcToDstRect;
totalMatrix.preScale(dst.width() / (src ? src->width() : image->width()), srcToDstRect.setRectToRect((src ? *src : SkRect::MakeIWH(image->width(), image->height())),
dst.height() / (src ? src->height() : image->height())); dst, SkMatrix::kFill_ScaleToFit);
if (this->shouldTileImage(image, src, constraint, paint.getFilterQuality(), totalMatrix)) { if (this->shouldTileImage(image, src, constraint, paint.getFilterQuality(), *draw.fMatrix,
srcToDstRect)) {
// only support tiling as bitmap at the moment, so force raster-version // only support tiling as bitmap at the moment, so force raster-version
if (!as_IB(image)->getROPixels(&bm)) { if (!as_IB(image)->getROPixels(&bm)) {
return; return;

View File

@ -179,32 +179,17 @@ private:
// The tileSize and clippedSrcRect will be valid only if true is returned. // The tileSize and clippedSrcRect will be valid only if true is returned.
bool shouldTileImageID(uint32_t imageID, const SkIRect& imageRect, bool shouldTileImageID(uint32_t imageID, const SkIRect& imageRect,
const SkMatrix& viewMatrix, const SkMatrix& viewMatrix,
const SkMatrix& srcToDstRectMatrix,
const GrTextureParams& params, const GrTextureParams& params,
const SkRect* srcRectPtr, const SkRect* srcRectPtr,
int maxTileSize, int maxTileSize,
int* tileSize, int* tileSize,
SkIRect* clippedSubset) const; SkIRect* clippedSubset) const;
bool shouldTileBitmap(const SkBitmap& bitmap,
const SkMatrix& viewMatrix,
const GrTextureParams& sampler,
const SkRect* srcRectPtr,
int maxTileSize,
int* tileSize,
SkIRect* clippedSrcRect) const;
// Just returns the predicate, not the out-tileSize or out-clippedSubset, as they are not // Just returns the predicate, not the out-tileSize or out-clippedSubset, as they are not
// needed at the moment. // needed at the moment.
bool shouldTileImage(const SkImage* image, const SkRect* srcRectPtr, bool shouldTileImage(const SkImage* image, const SkRect* srcRectPtr,
SkCanvas::SrcRectConstraint constraint, SkFilterQuality quality, SkCanvas::SrcRectConstraint constraint, SkFilterQuality quality,
const SkMatrix& viewMatrix) const; const SkMatrix& viewMatrix, const SkMatrix& srcToDstRect) const;
void internalDrawBitmap(const SkBitmap&,
const SkMatrix& viewMatrix,
const SkRect&,
const GrTextureParams& params,
const SkPaint& paint,
SkCanvas::SrcRectConstraint,
bool bicubic,
bool needsTextureDomain);
sk_sp<SkSpecialImage> filterTexture(const SkDraw&, sk_sp<SkSpecialImage> filterTexture(const SkDraw&,
SkSpecialImage*, SkSpecialImage*,
@ -212,8 +197,10 @@ private:
SkIPoint* offset, SkIPoint* offset,
const SkImageFilter* filter); const SkImageFilter* filter);
// Splits bitmap into tiles of tileSize and draws them using separate textures for each tile.
void drawTiledBitmap(const SkBitmap& bitmap, void drawTiledBitmap(const SkBitmap& bitmap,
const SkMatrix& viewMatrix, const SkMatrix& viewMatrix,
const SkMatrix& srcToDstMatrix,
const SkRect& srcRect, const SkRect& srcRect,
const SkIRect& clippedSrcRect, const SkIRect& clippedSrcRect,
const GrTextureParams& params, const GrTextureParams& params,
@ -222,6 +209,17 @@ private:
int tileSize, int tileSize,
bool bicubic); bool bicubic);
// Used by drawTiledBitmap to draw each tile.
void drawBitmapTile(const SkBitmap&,
const SkMatrix& viewMatrix,
const SkRect& dstRect,
const SkRect& srcRect,
const GrTextureParams& params,
const SkPaint& paint,
SkCanvas::SrcRectConstraint,
bool bicubic,
bool needsTextureDomain);
void drawTextureProducer(GrTextureProducer*, void drawTextureProducer(GrTextureProducer*,
const SkRect* srcRect, const SkRect* srcRect,
const SkRect* dstRect, const SkRect* dstRect,