The idea here is that we will pass GrTextureProxys in (where we're currently passing GrTextures) and defer the normalization until the texture is actually instantiated (and possibly move it to the GPU entirely)
This CL does (intentionally) change the texturedomaineffect GM but I believe the new behavior is more correct.
Change-Id: I4e0510b3dfb65ff0d0ee5921f9a6f94151e602d3
Reviewed-on: https://skia-review.googlesource.com/6807
Commit-Queue: Robert Phillips <robertphillips@google.com>
Reviewed-by: Brian Salomon <bsalomon@google.com>
GrPaint grPaint;
grPaint.setXPFactory(GrPorterDuffXPFactory::Get(SkBlendMode::kSrc));
sk_sp<GrFragmentProcessor> fp(
- GrTextureDomainEffect::Make(texture.get(), nullptr, textureMatrices[tm],
- GrTextureDomain::MakeTexelDomain(texture.get(),
- texelDomains[d]),
- mode, GrSamplerParams::kNone_FilterMode));
+ GrTextureDomainEffect::Make(
+ texture.get(), nullptr, textureMatrices[tm],
+ GrTextureDomain::MakeTexelDomainForMode(texelDomains[d], mode),
+ mode, GrSamplerParams::kNone_FilterMode));
if (!fp) {
continue;
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);
+ SkRect domain = SkRect::Make(*srcBounds);
+ domain.inset((i < scaleFactorX) ? SK_ScalarHalf : 0.0f,
+ (i < scaleFactorY) ? SK_ScalarHalf : 0.0f);
sk_sp<GrFragmentProcessor> fp(GrTextureDomainEffect::Make(
srcTexture.get(),
nullptr,
GrColorSpaceXform::Make(background->getColorSpace(), outputProperties.colorSpace());
bgFP = GrTextureDomainEffect::Make(
backgroundTex.get(), std::move(bgXform), backgroundMatrix,
- GrTextureDomain::MakeTexelDomain(backgroundTex.get(), background->subset()),
+ GrTextureDomain::MakeTexelDomain(background->subset()),
GrTextureDomain::kDecal_Mode, GrSamplerParams::kNone_FilterMode);
} else {
bgFP = GrConstColorProcessor::Make(GrColor4f::TransparentBlack(),
foregroundFP = GrTextureDomainEffect::Make(
foregroundTex.get(), std::move(fgXform), foregroundMatrix,
- GrTextureDomain::MakeTexelDomain(foregroundTex.get(), foreground->subset()),
+ GrTextureDomain::MakeTexelDomain(foreground->subset()),
GrTextureDomain::kDecal_Mode, GrSamplerParams::kNone_FilterMode);
paint.addColorFragmentProcessor(std::move(foregroundFP));
: fDisplacementTransform(offsetMatrix, displacement, GrSamplerParams::kNone_FilterMode)
, fDisplacementSampler(displacement)
, fColorTransform(color, GrSamplerParams::kNone_FilterMode)
- , fDomain(GrTextureDomain::MakeTexelDomain(color, SkIRect::MakeSize(colorDimensions)),
+ , fDomain(color, GrTextureDomain::MakeTexelDomain(SkIRect::MakeSize(colorDimensions)),
GrTextureDomain::kDecal_Mode)
, fColorSampler(color)
, fColorSpaceXform(std::move(colorSpaceXform))
pdman.set2f(fScaleUni, SkScalarToFloat(scaleX),
colorTex->origin() == kTopLeft_GrSurfaceOrigin ?
SkScalarToFloat(scaleY) : SkScalarToFloat(-scaleY));
- fGLDomain.setData(pdman, displacementMap.domain(), colorTex->origin());
+ fGLDomain.setData(pdman, displacementMap.domain(), colorTex);
if (SkToBool(displacementMap.colorSpaceXform())) {
pdman.setSkMatrix44(fColorSpaceXformUni, displacementMap.colorSpaceXform()->srcToDst());
}
///////////////////////////////////////////////////////////////////////////////
-namespace {
-
-GrTextureDomain create_domain(GrTexture* texture, const SkIRect* srcBounds,
- GrTextureDomain::Mode mode) {
+static GrTextureDomain create_domain(GrTexture* texture, const SkIRect* srcBounds,
+ GrTextureDomain::Mode mode) {
if (srcBounds) {
- SkRect texelDomain = GrTextureDomain::MakeTexelDomainForMode(texture, *srcBounds, mode);
- return GrTextureDomain(texelDomain, mode);
+ SkRect texelDomain = GrTextureDomain::MakeTexelDomainForMode(*srcBounds, mode);
+ return GrTextureDomain(texture, texelDomain, mode);
} else {
- return GrTextureDomain(SkRect::MakeEmpty(), GrTextureDomain::kIgnore_Mode);
+ return GrTextureDomain::IgnoredDomain();
}
}
-};
-
GrLightingEffect::GrLightingEffect(GrTexture* texture,
const SkImageFilterLight* light,
SkScalar surfaceScale,
pdman.set1f(fSurfaceScaleUni, lighting.surfaceScale());
sk_sp<SkImageFilterLight> transformedLight(
lighting.light()->transform(lighting.filterMatrix()));
- fDomain.setData(pdman, lighting.domain(), texture->origin());
+ fDomain.setData(pdman, lighting.domain(), texture);
fLight->setData(pdman, transformedLight.get());
}
outputProperties.colorSpace());
bgFP = GrTextureDomainEffect::Make(
backgroundTex.get(), std::move(bgXform), backgroundMatrix,
- GrTextureDomain::MakeTexelDomain(backgroundTex.get(),
- background->subset()),
+ GrTextureDomain::MakeTexelDomain(background->subset()),
GrTextureDomain::kDecal_Mode,
GrSamplerParams::kNone_FilterMode);
} else {
foregroundFP = GrTextureDomainEffect::Make(
foregroundTex.get(), std::move(fgXform), foregroundMatrix,
- GrTextureDomain::MakeTexelDomain(foregroundTex.get(),
- foreground->subset()),
+ GrTextureDomain::MakeTexelDomain(foreground->subset()),
GrTextureDomain::kDecal_Mode,
GrSamplerParams::kNone_FilterMode);
SkRect domain;
DomainMode domainMode =
DetermineDomainMode(constraintRect, filterConstraint, coordsLimitedToConstraintRect,
- texture->width(), texture->height(), nullptr, fmForDetermineDomain,
- &domain);
+ texture->width(), texture->height(),
+ nullptr, fmForDetermineDomain, &domain);
SkASSERT(kTightCopy_DomainMode != domainMode);
SkMatrix normalizedTextureMatrix = textureMatrix;
normalizedTextureMatrix.postIDiv(texture->width(), texture->height());
GrPaint paint;
paint.setGammaCorrect(true);
- SkScalar sx SK_INIT_TO_AVOID_WARNING;
- SkScalar sy SK_INIT_TO_AVOID_WARNING;
- if (subset) {
- sx = 1.f / inputTexture->width();
- sy = 1.f / inputTexture->height();
- }
-
if (copyParams.fFilter != GrSamplerParams::kNone_FilterMode && subset &&
(subset->width() != copyParams.fWidth || subset->height() != copyParams.fHeight)) {
SkRect domain;
- domain.fLeft = (subset->fLeft + 0.5f) * sx;
- domain.fTop = (subset->fTop + 0.5f)* sy;
- domain.fRight = (subset->fRight - 0.5f) * sx;
- domain.fBottom = (subset->fBottom - 0.5f) * sy;
+ domain.fLeft = subset->fLeft + 0.5f;
+ domain.fTop = subset->fTop + 0.5f;
+ domain.fRight = subset->fRight - 0.5f;
+ domain.fBottom = subset->fBottom - 0.5f;
// This would cause us to read values from outside the subset. Surely, the caller knows
// better!
SkASSERT(copyParams.fFilter != GrSamplerParams::kMipMap_FilterMode);
SkRect localRect;
if (subset) {
+ SkScalar sx = SK_Scalar1 / inputTexture->width();
+ SkScalar sy = SK_Scalar1 / inputTexture->height();
+
localRect = SkRect::Make(*subset);
localRect.fLeft *= sx;
localRect.fTop *= sy;
// Unless we know the amount of outset and the texture matrix we have to conservatively enforce
// the domain.
if (restrictFilterToRect) {
- domainRect->fLeft = constraintRect.fLeft + kDomainInset;
- domainRect->fTop = constraintRect.fTop + kDomainInset;
- domainRect->fRight = constraintRect.fRight - kDomainInset;
- domainRect->fBottom = constraintRect.fBottom - kDomainInset;
+ *domainRect = constraintRect.makeInset(kDomainInset, kDomainInset);
} else if (textureContentArea) {
// If we got here then: there is a textureContentArea, the coords are limited to the
// constraint rect, and we're allowed to filter across the constraint rect boundary. So
if (domainRect->fTop > domainRect->fBottom) {
domainRect->fTop = domainRect->fBottom = SkScalarAve(domainRect->fTop, domainRect->fBottom);
}
- domainRect->fLeft /= texW;
- domainRect->fTop /= texH;
- domainRect->fRight /= texW;
- domainRect->fBottom /= texH;
return kDomain_DomainMode;
}
// Use a constrained texture domain to avoid color bleeding
SkRect domain;
if (srcRect.width() > SK_Scalar1) {
- domain.fLeft = (srcRect.fLeft + 0.5f) * iw;
- domain.fRight = (srcRect.fRight - 0.5f) * iw;
+ domain.fLeft = srcRect.fLeft + 0.5f;
+ domain.fRight = srcRect.fRight - 0.5f;
} else {
- domain.fLeft = domain.fRight = srcRect.centerX() * iw;
+ domain.fLeft = domain.fRight = srcRect.centerX();
}
if (srcRect.height() > SK_Scalar1) {
- domain.fTop = (srcRect.fTop + 0.5f) * ih;
- domain.fBottom = (srcRect.fBottom - 0.5f) * ih;
+ domain.fTop = srcRect.fTop + 0.5f;
+ domain.fBottom = srcRect.fBottom - 0.5f;
} else {
- domain.fTop = domain.fBottom = srcRect.centerY() * ih;
+ domain.fTop = domain.fBottom = srcRect.centerY();
}
if (bicubic) {
fp = GrBicubicEffect::Make(texture.get(), std::move(colorSpaceXform), texMatrix,
imageIncrement[0] = 1.0f / texture->width();
imageIncrement[1] = 1.0f / texture->height();
pdman.set2fv(fImageIncrementUni, 1, imageIncrement);
- fDomain.setData(pdman, bicubicEffect.domain(), texture->origin());
+ fDomain.setData(pdman, bicubicEffect.domain(), texture);
if (SkToBool(bicubicEffect.colorSpaceXform())) {
pdman.setSkMatrix44(fColorSpaceXformUni, bicubicEffect.colorSpaceXform()->srcToDst());
}
const SkRect& domain)
: INHERITED(texture, std::move(colorSpaceXform), matrix,
GrSamplerParams(SkShader::kClamp_TileMode, GrSamplerParams::kNone_FilterMode))
- , fDomain(domain, GrTextureDomain::kClamp_Mode) {
+ , fDomain(texture, domain, GrTextureDomain::kClamp_Mode) {
this->initClassID<GrBicubicEffect>();
}
pdman.set4fv(fKernelUni, arrayCount, conv.kernel());
pdman.set1f(fGainUni, conv.gain());
pdman.set1f(fBiasUni, conv.bias());
- fDomain.setData(pdman, conv.domain(), texture->origin());
+ fDomain.setData(pdman, conv.domain(), texture);
}
GrMatrixConvolutionEffect::GrMatrixConvolutionEffect(GrTexture* texture,
fGain(SkScalarToFloat(gain)),
fBias(SkScalarToFloat(bias) / 255.0f),
fConvolveAlpha(convolveAlpha),
- fDomain(GrTextureDomain::MakeTexelDomainForMode(texture, bounds, tileMode), tileMode) {
+ fDomain(texture, GrTextureDomain::MakeTexelDomainForMode(bounds, tileMode), tileMode) {
this->initClassID<GrMatrixConvolutionEffect>();
for (int i = 0; i < kernelSize.width() * kernelSize.height(); i++) {
fKernel[i] = SkScalarToFloat(kernel[i]);
#include "glsl/GrGLSLShaderBuilder.h"
#include "glsl/GrGLSLUniformHandler.h"
-GrTextureDomain::GrTextureDomain(const SkRect& domain, Mode mode, int index)
- : fIndex(index) {
+static bool can_ignore_rect(GrTexture* tex, const SkRect& domain) {
+ // This logic is relying on the instantiated size of 'tex'. In the deferred world it
+ // will have to change so this logic only fires for kExact texture proxies. This shouldn't
+ // change the actual behavior of Ganesh since shaders shouldn't be accessing pixels outside
+ // of the content rectangle.
+ const SkIRect kFullRect = SkIRect::MakeWH(tex->width(), tex->height());
+
+ return domain.contains(kFullRect);
+}
- static const SkRect kFullRect = {0, 0, SK_Scalar1, SK_Scalar1};
- if (domain.contains(kFullRect) && kClamp_Mode == mode) {
- fMode = kIgnore_Mode;
- } else {
- fMode = mode;
+GrTextureDomain::GrTextureDomain(GrTexture* tex, const SkRect& domain, Mode mode, int index)
+ : fMode(mode), fIndex(index) {
+
+ if (kIgnore_Mode == fMode) {
+ return;
}
- if (fMode != kIgnore_Mode) {
- // We don't currently handle domains that are empty or don't intersect the texture.
- // It is OK if the domain rect is a line or point, but it should not be inverted. We do not
- // handle rects that do not intersect the [0..1]x[0..1] rect.
- SkASSERT(domain.fLeft <= domain.fRight);
- SkASSERT(domain.fTop <= domain.fBottom);
- fDomain.fLeft = SkScalarPin(domain.fLeft, kFullRect.fLeft, kFullRect.fRight);
- fDomain.fRight = SkScalarPin(domain.fRight, kFullRect.fLeft, kFullRect.fRight);
- fDomain.fTop = SkScalarPin(domain.fTop, kFullRect.fTop, kFullRect.fBottom);
- fDomain.fBottom = SkScalarPin(domain.fBottom, kFullRect.fTop, kFullRect.fBottom);
- SkASSERT(fDomain.fLeft <= fDomain.fRight);
- SkASSERT(fDomain.fTop <= fDomain.fBottom);
+ if (kClamp_Mode == mode && can_ignore_rect(tex, domain)) {
+ fMode = kIgnore_Mode;
+ return;
}
+
+ const SkRect kFullRect = SkRect::MakeIWH(tex->width(), tex->height());
+
+ // We don't currently handle domains that are empty or don't intersect the texture.
+ // It is OK if the domain rect is a line or point, but it should not be inverted. We do not
+ // handle rects that do not intersect the [0..1]x[0..1] rect.
+ SkASSERT(domain.fLeft <= domain.fRight);
+ SkASSERT(domain.fTop <= domain.fBottom);
+ fDomain.fLeft = SkScalarPin(domain.fLeft, 0.0f, kFullRect.fRight);
+ fDomain.fRight = SkScalarPin(domain.fRight, fDomain.fLeft, kFullRect.fRight);
+ fDomain.fTop = SkScalarPin(domain.fTop, 0.0f, kFullRect.fBottom);
+ fDomain.fBottom = SkScalarPin(domain.fBottom, fDomain.fTop, kFullRect.fBottom);
+ SkASSERT(fDomain.fLeft <= fDomain.fRight);
+ SkASSERT(fDomain.fTop <= fDomain.fBottom);
}
//////////////////////////////////////////////////////////////////////////////
void GrTextureDomain::GLDomain::setData(const GrGLSLProgramDataManager& pdman,
const GrTextureDomain& textureDomain,
- GrSurfaceOrigin textureOrigin) {
+ GrTexture* tex) {
SkASSERT(textureDomain.mode() == fMode);
if (kIgnore_Mode != textureDomain.mode()) {
+ SkScalar wInv = SK_Scalar1 / tex->width();
+ SkScalar hInv = SK_Scalar1 / tex->height();
+
float values[kPrevDomainCount] = {
- SkScalarToFloat(textureDomain.domain().left()),
- SkScalarToFloat(textureDomain.domain().top()),
- SkScalarToFloat(textureDomain.domain().right()),
- SkScalarToFloat(textureDomain.domain().bottom())
+ SkScalarToFloat(textureDomain.domain().fLeft * wInv),
+ SkScalarToFloat(textureDomain.domain().fTop * hInv),
+ SkScalarToFloat(textureDomain.domain().fRight * wInv),
+ SkScalarToFloat(textureDomain.domain().fBottom * hInv)
};
+
+ SkASSERT(values[0] >= 0.0f && values[0] <= 1.0f);
+ SkASSERT(values[1] >= 0.0f && values[1] <= 1.0f);
+ SkASSERT(values[2] >= 0.0f && values[2] <= 1.0f);
+ SkASSERT(values[3] >= 0.0f && values[3] <= 1.0f);
+
// vertical flip if necessary
- if (kBottomLeft_GrSurfaceOrigin == textureOrigin) {
+ if (kBottomLeft_GrSurfaceOrigin == tex->origin()) {
values[1] = 1.0f - values[1];
values[3] = 1.0f - values[3];
// The top and bottom were just flipped, so correct the ordering
const SkRect& domain,
GrTextureDomain::Mode mode,
GrSamplerParams::FilterMode filterMode) {
- static const SkRect kFullRect = {0, 0, SK_Scalar1, SK_Scalar1};
if (GrTextureDomain::kIgnore_Mode == mode ||
- (GrTextureDomain::kClamp_Mode == mode && domain.contains(kFullRect))) {
+ (GrTextureDomain::kClamp_Mode == mode && can_ignore_rect(texture, domain))) {
return GrSimpleTextureEffect::Make(texture, std::move(colorSpaceXform), matrix, filterMode);
} else {
return sk_sp<GrFragmentProcessor>(
GrTextureDomain::Mode mode,
GrSamplerParams::FilterMode filterMode)
: GrSingleTextureEffect(texture, std::move(colorSpaceXform), matrix, filterMode)
- , fTextureDomain(domain, mode) {
+ , fTextureDomain(texture, domain, mode) {
SkASSERT(mode != GrTextureDomain::kRepeat_Mode ||
filterMode == GrSamplerParams::kNone_FilterMode);
this->initClassID<GrTextureDomainEffect>();
void onSetData(const GrGLSLProgramDataManager& pdman, const GrProcessor& fp) override {
const GrTextureDomainEffect& tde = fp.cast<GrTextureDomainEffect>();
const GrTextureDomain& domain = tde.fTextureDomain;
- fGLDomain.setData(pdman, domain, tde.textureSampler(0).texture()->origin());
+ fGLDomain.setData(pdman, domain, tde.textureSampler(0).texture());
if (SkToBool(tde.colorSpaceXform())) {
pdman.setSkMatrix44(fColorSpaceXformUni, tde.colorSpaceXform()->srcToDst());
}
sk_sp<GrFragmentProcessor> GrTextureDomainEffect::TestCreate(GrProcessorTestData* d) {
int texIdx = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx :
GrProcessorUnitTest::kAlphaTextureIdx;
+ GrTexture* tex = d->fTextures[texIdx];
SkRect domain;
- domain.fLeft = d->fRandom->nextUScalar1();
- domain.fRight = d->fRandom->nextRangeScalar(domain.fLeft, SK_Scalar1);
- domain.fTop = d->fRandom->nextUScalar1();
- domain.fBottom = d->fRandom->nextRangeScalar(domain.fTop, SK_Scalar1);
+ domain.fLeft = d->fRandom->nextRangeScalar(0, tex->width());
+ domain.fRight = d->fRandom->nextRangeScalar(domain.fLeft, tex->width());
+ domain.fTop = d->fRandom->nextRangeScalar(0, tex->height());
+ domain.fBottom = d->fRandom->nextRangeScalar(domain.fTop, tex->height());
GrTextureDomain::Mode mode =
(GrTextureDomain::Mode) d->fRandom->nextULessThan(GrTextureDomain::kModeCount);
const SkMatrix& matrix = GrTest::TestMatrix(d->fRandom);
bool bilerp = mode != GrTextureDomain::kRepeat_Mode ? d->fRandom->nextBool() : false;
auto colorSpaceXform = GrTest::TestColorXform(d->fRandom);
return GrTextureDomainEffect::Make(
- d->fTextures[texIdx],
+ tex,
colorSpaceXform,
matrix,
domain,
GrDeviceSpaceTextureDecalFragmentProcessor::GrDeviceSpaceTextureDecalFragmentProcessor(
GrTexture* texture, const SkIRect& subset, const SkIPoint& deviceSpaceOffset)
: fTextureSampler(texture, GrSamplerParams::ClampNoFilter())
- , fTextureDomain(GrTextureDomain::MakeTexelDomain(texture, subset),
+ , fTextureDomain(texture, GrTextureDomain::MakeTexelDomain(subset),
GrTextureDomain::kDecal_Mode) {
this->addTextureSampler(&fTextureSampler);
fDeviceSpaceOffset.fX = deviceSpaceOffset.fX - subset.fLeft;
const GrDeviceSpaceTextureDecalFragmentProcessor& dstdfp =
fp.cast<GrDeviceSpaceTextureDecalFragmentProcessor>();
GrTexture* texture = dstdfp.textureSampler(0).texture();
- fGLDomain.setData(pdman, dstdfp.fTextureDomain, texture->origin());
+ fGLDomain.setData(pdman, dstdfp.fTextureDomain, texture);
float iw = 1.f / texture->width();
float ih = 1.f / texture->height();
float scaleAndTransData[4] = {
static const int kModeCount = kLastMode + 1;
static const GrTextureDomain& IgnoredDomain() {
- static const SkRect gDummyRect = {0, 0, 0, 0};
- static const GrTextureDomain gDomain(gDummyRect, kIgnore_Mode);
+ static const GrTextureDomain gDomain(nullptr, SkRect::MakeEmpty(), kIgnore_Mode);
return gDomain;
}
* @param index Pass a value >= 0 if using multiple texture domains in the same effect.
* It is used to keep inserted variables from causing name collisions.
*/
- GrTextureDomain(const SkRect& domain, Mode, int index = -1);
+ GrTextureDomain(GrTexture*, const SkRect& domain, Mode, int index = -1);
const SkRect& domain() const { return fDomain; }
Mode mode() const { return fMode; }
/* Computes a domain that bounds all the texels in texelRect. Note that with bilerp enabled
texels neighboring the domain may be read. */
- static const SkRect MakeTexelDomain(const GrTexture* texture, const SkIRect& texelRect) {
- SkScalar wInv = SK_Scalar1 / texture->width();
- SkScalar hInv = SK_Scalar1 / texture->height();
- SkRect result = {
- texelRect.fLeft * wInv,
- texelRect.fTop * hInv,
- texelRect.fRight * wInv,
- texelRect.fBottom * hInv
- };
- return result;
+ static const SkRect MakeTexelDomain(const SkIRect& texelRect) {
+ return SkRect::Make(texelRect);
}
- static const SkRect MakeTexelDomainForMode(const GrTexture* texture, const SkIRect& texelRect, Mode mode) {
+ static const SkRect MakeTexelDomainForMode(const SkIRect& texelRect, Mode mode) {
// For Clamp mode, inset by half a texel.
- SkScalar wInv = SK_Scalar1 / texture->width();
- SkScalar hInv = SK_Scalar1 / texture->height();
SkScalar inset = (mode == kClamp_Mode && !texelRect.isEmpty()) ? SK_ScalarHalf : 0;
- return SkRect::MakeLTRB(
- (texelRect.fLeft + inset) * wInv,
- (texelRect.fTop + inset) * hInv,
- (texelRect.fRight - inset) * wInv,
- (texelRect.fBottom - inset) * hInv
- );
+ return SkRect::MakeLTRB(texelRect.fLeft + inset, texelRect.fTop + inset,
+ texelRect.fRight - inset, texelRect.fBottom - inset);
}
bool operator==(const GrTextureDomain& that) const {
* origin.
*/
void setData(const GrGLSLProgramDataManager& pdman, const GrTextureDomain& textureDomain,
- GrSurfaceOrigin textureOrigin);
+ GrTexture* texure);
enum {
kDomainKeyBits = 2, // See DomainKey().
Mode fMode;
SkRect fDomain;
int fIndex;
-
- typedef GrSingleTextureEffect INHERITED;
};
/**