blob: f7d71fa18da9300f07cf4c51b5bf75909ae03607 [file] [log] [blame]
/*
* Copyright 2016 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "GrTextureProducer.h"
#include "GrClip.h"
#include "GrRenderTargetContext.h"
#include "GrResourceProvider.h"
#include "GrTextureProxy.h"
#include "effects/GrBicubicEffect.h"
#include "effects/GrSimpleTextureEffect.h"
#include "effects/GrTextureDomain.h"
sk_sp<GrTextureProxy> GrTextureProducer::CopyOnGpu(GrContext* context,
sk_sp<GrTextureProxy> inputProxy,
const CopyParams& copyParams) {
SkASSERT(context);
const SkRect dstRect = SkRect::MakeIWH(copyParams.fWidth, copyParams.fHeight);
sk_sp<GrRenderTargetContext> copyRTC = context->makeDeferredRenderTargetContextWithFallback(
SkBackingFit::kExact, dstRect.width(), dstRect.height(), inputProxy->config(), nullptr,
0, inputProxy->origin());
if (!copyRTC) {
return nullptr;
}
GrPaint paint;
paint.setGammaCorrect(true);
SkRect localRect = SkRect::MakeWH(inputProxy->width(), inputProxy->height());
bool needsDomain = false;
if (copyParams.fFilter != GrSamplerParams::kNone_FilterMode) {
bool resizing = localRect.width() != dstRect.width() ||
localRect.height() != dstRect.height();
needsDomain = resizing && !GrResourceProvider::IsFunctionallyExact(inputProxy.get());
}
if (needsDomain) {
const SkRect domain = localRect.makeInset(0.5f, 0.5f);
// This would cause us to read values from outside the subset. Surely, the caller knows
// better!
SkASSERT(copyParams.fFilter != GrSamplerParams::kMipMap_FilterMode);
paint.addColorFragmentProcessor(
GrTextureDomainEffect::Make(std::move(inputProxy), nullptr, SkMatrix::I(),
domain, GrTextureDomain::kClamp_Mode, copyParams.fFilter));
} else {
GrSamplerParams params(SkShader::kClamp_TileMode, copyParams.fFilter);
paint.addColorTextureProcessor(std::move(inputProxy), nullptr, SkMatrix::I(), params);
}
paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
copyRTC->fillRectToRect(GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(), dstRect,
localRect);
return copyRTC->asTextureProxyRef();
}
/** Determines whether a texture domain is necessary and if so what domain to use. There are two
* rectangles to consider:
* - The first is the content area specified by the texture adjuster (i.e., textureContentArea).
* We can *never* allow filtering to cause bleed of pixels outside this rectangle.
* - The second rectangle is the constraint rectangle (i.e., constraintRect), which is known to
* be contained by the content area. The filterConstraint specifies whether we are allowed to
* bleed across this rect.
*
* We want to avoid using a domain if possible. We consider the above rectangles, the filter type,
* and whether the coords generated by the draw would all fall within the constraint rect. If the
* latter is true we only need to consider whether the filter would extend beyond the rects.
*/
GrTextureProducer::DomainMode GrTextureProducer::DetermineDomainMode(
const SkRect& constraintRect,
FilterConstraint filterConstraint,
bool coordsLimitedToConstraintRect,
GrTextureProxy* proxy,
const GrSamplerParams::FilterMode* filterModeOrNullForBicubic,
SkRect* domainRect) {
const SkIRect proxyBounds = SkIRect::MakeWH(proxy->width(), proxy->height());
SkASSERT(proxyBounds.contains(constraintRect));
const bool proxyIsExact = GrResourceProvider::IsFunctionallyExact(proxy);
// We don't expect to have an image that is in an inexact proxy unless the caller was aware
// of the potential of sampling outside of the proxy's bounds and specified a constraint rect
// with a filter constraint.
SkASSERT(kYes_FilterConstraint == filterConstraint || proxyIsExact);
// If the constraint rectangle contains the whole proxy then no need for a domain.
if (constraintRect.contains(proxyBounds) && proxyIsExact) {
return kNoDomain_DomainMode;
}
bool restrictFilterToRect = (filterConstraint == GrTextureProducer::kYes_FilterConstraint);
// If we can filter outside the constraint rect, and there is no non-content area of the
// proxy, and we aren't going to generate sample coords outside the constraint rect then we
// don't need a domain.
if (!restrictFilterToRect && coordsLimitedToConstraintRect) {
return kNoDomain_DomainMode;
}
// Get the domain inset based on sampling mode (or bail if mipped)
if (filterModeOrNullForBicubic) {
switch (*filterModeOrNullForBicubic) {
case GrSamplerParams::kNone_FilterMode:
if (coordsLimitedToConstraintRect) {
return kNoDomain_DomainMode;
}
break;
case GrSamplerParams::kBilerp_FilterMode:
break;
case GrSamplerParams::kMipMap_FilterMode:
// No domain can save us with mip maps.
return restrictFilterToRect ? kTightCopy_DomainMode : kNoDomain_DomainMode;
}
}
// Both bilerp and bicubic use bilinear filtering and so need to be clamped to the center
// of the edge texel. Pinning to the texel center has no impact on nearest mode and MIP-maps
static const SkScalar kDomainInset = 0.5f;
// Figure out the limits of pixels we're allowed to sample from.
// Unless we know the amount of outset and the texture matrix we have to conservatively enforce
// the domain.
if (restrictFilterToRect) {
*domainRect = constraintRect.makeInset(kDomainInset, kDomainInset);
if (domainRect->fLeft > domainRect->fRight) {
domainRect->fLeft = domainRect->fRight =
SkScalarAve(domainRect->fLeft, domainRect->fRight);
}
if (domainRect->fTop > domainRect->fBottom) {
domainRect->fTop = domainRect->fBottom =
SkScalarAve(domainRect->fTop, domainRect->fBottom);
}
return kDomain_DomainMode;
}
return kNoDomain_DomainMode;
}
std::unique_ptr<GrFragmentProcessor> GrTextureProducer::CreateFragmentProcessorForDomainAndFilter(
sk_sp<GrTextureProxy> proxy,
sk_sp<GrColorSpaceXform> colorSpaceXform,
const SkMatrix& textureMatrix,
DomainMode domainMode,
const SkRect& domain,
const GrSamplerParams::FilterMode* filterOrNullForBicubic) {
SkASSERT(kTightCopy_DomainMode != domainMode);
if (filterOrNullForBicubic) {
if (kDomain_DomainMode == domainMode) {
return GrTextureDomainEffect::Make(std::move(proxy),
std::move(colorSpaceXform), textureMatrix,
domain, GrTextureDomain::kClamp_Mode,
*filterOrNullForBicubic);
} else {
GrSamplerParams params(SkShader::kClamp_TileMode, *filterOrNullForBicubic);
return GrSimpleTextureEffect::Make(std::move(proxy),
std::move(colorSpaceXform), textureMatrix,
params);
}
} else {
if (kDomain_DomainMode == domainMode) {
return GrBicubicEffect::Make(std::move(proxy), std::move(colorSpaceXform),
textureMatrix, domain);
} else {
static const SkShader::TileMode kClampClamp[] =
{ SkShader::kClamp_TileMode, SkShader::kClamp_TileMode };
return GrBicubicEffect::Make(std::move(proxy), std::move(colorSpaceXform),
textureMatrix, kClampClamp);
}
}
}