aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/gpu/GrTextureParamsAdjuster.cpp
diff options
context:
space:
mode:
authorGravatar bsalomon <bsalomon@google.com>2015-11-09 11:55:57 -0800
committerGravatar Commit bot <commit-bot@chromium.org>2015-11-09 11:55:57 -0800
commitc55271f2551533b37043aa2e37f754832a43073c (patch)
tree5e9729a4c8fe05519805b97db38621efbc34956a /src/gpu/GrTextureParamsAdjuster.cpp
parent3a210bfd403815bebbc7efabe7bbd373e5a3d8f8 (diff)
Separate out natively-texture image/bmp draws from cached-as-texture image/bmp draws
This makes texture-backed images and bitmaps down a new code path. It adds a pinch point via the texture adjuster that will be used to handle copied necessary for different texture targets. It also fixes bugs in the existing code exhibited by recent updates to the bleed GM. The plan is to move the the sw/generator-backed imgs/bmps on to this code path with future changes. Review URL: https://codereview.chromium.org/1424313010
Diffstat (limited to 'src/gpu/GrTextureParamsAdjuster.cpp')
-rw-r--r--src/gpu/GrTextureParamsAdjuster.cpp208
1 files changed, 207 insertions, 1 deletions
diff --git a/src/gpu/GrTextureParamsAdjuster.cpp b/src/gpu/GrTextureParamsAdjuster.cpp
index 579cf96ef3..b98a126971 100644
--- a/src/gpu/GrTextureParamsAdjuster.cpp
+++ b/src/gpu/GrTextureParamsAdjuster.cpp
@@ -19,6 +19,7 @@
#include "SkCanvas.h"
#include "SkGr.h"
#include "SkGrPriv.h"
+#include "effects/GrBicubicEffect.h"
#include "effects/GrTextureDomain.h"
typedef GrTextureProducer::CopyParams CopyParams;
@@ -131,7 +132,7 @@ GrTexture* GrTextureAdjuster::refTextureSafeForParams(const GrTextureParams& par
GrTexture* texture = this->originalTexture();
GrContext* context = texture->getContext();
CopyParams copyParams;
- const SkIRect* contentArea = this->contentArea();
+ const SkIRect* contentArea = this->contentAreaOrNull();
if (contentArea && GrTextureParams::kMipMap_FilterMode == params.filterMode()) {
// If we generate a MIP chain for texture it will read pixel values from outside the content
@@ -171,6 +172,211 @@ GrTexture* GrTextureAdjuster::refTextureSafeForParams(const GrTextureParams& par
return result;
}
+enum DomainMode {
+ kNoDomain_DomainMode,
+ kDomain_DomainMode,
+ kTightCopy_DomainMode
+};
+
+/** 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. We can *never* allow
+ * filtering to cause bleed of pixels outside this rectangle.
+ * - The second rectangle is the constraint rectangle, 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.
+ */
+static DomainMode determine_domain_mode(
+ const SkRect& constraintRect,
+ GrTextureAdjuster::FilterConstraint filterConstraint,
+ bool coordsLimitedToConstraintRect,
+ int texW, int texH,
+ const SkIRect* textureContentArea,
+ const GrTextureParams::FilterMode* filterModeOrNullForBicubic,
+ SkRect* domainRect) {
+
+ SkASSERT(SkRect::MakeIWH(texW, texH).contains(constraintRect));
+ // We only expect a content area rect if there is some non-content area.
+ SkASSERT(!textureContentArea ||
+ (!textureContentArea->contains(SkIRect::MakeWH(texW, texH)) &&
+ SkRect::Make(*textureContentArea).contains(constraintRect)));
+
+ SkRect textureBounds = SkRect::MakeIWH(texW, texH);
+ // If the src rectangle contains the whole texture then no need for a domain.
+ if (constraintRect.contains(textureBounds)) {
+ return kNoDomain_DomainMode;
+ }
+
+ bool restrictFilterToRect = (filterConstraint == GrTextureAdjuster::kYes_FilterConstraint);
+
+ // If we can filter outside the constraint rect, and there is no non-content area of the
+ // texture, and we aren't going to generate sample coords outside the constraint rect then we
+ // don't need a domain.
+ if (!restrictFilterToRect && !textureContentArea && coordsLimitedToConstraintRect) {
+ return kNoDomain_DomainMode;
+ }
+
+ // Get the domain inset based on sampling mode (or bail if mipped)
+ SkScalar filterHalfWidth = 0.f;
+ if (filterModeOrNullForBicubic) {
+ switch (*filterModeOrNullForBicubic) {
+ case GrTextureParams::kNone_FilterMode:
+ if (coordsLimitedToConstraintRect) {
+ return kNoDomain_DomainMode;
+ } else {
+ filterHalfWidth = 0.f;
+ }
+ break;
+ case GrTextureParams::kBilerp_FilterMode:
+ filterHalfWidth = .5f;
+ break;
+ case GrTextureParams::kMipMap_FilterMode:
+ // No domain can save use here.
+ return kTightCopy_DomainMode;
+ }
+ } else {
+ // bicubic does nearest filtering internally.
+ filterHalfWidth = 1.5f;
+ }
+
+ // 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->fLeft = constraintRect.fLeft + kDomainInset;
+ domainRect->fTop = constraintRect.fTop + kDomainInset;
+ domainRect->fRight = constraintRect.fRight - kDomainInset;
+ domainRect->fBottom = constraintRect.fBottom - 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
+ // we check whether the filter would reach across the edge of the content area.
+ // We will only set the sides that are required.
+
+ domainRect->setLargest();
+ if (coordsLimitedToConstraintRect) {
+ // We may be able to use the fact that the texture coords are limited to the constraint
+ // rect in order to avoid having to add a domain.
+ bool needContentAreaConstraint = false;
+ if (textureContentArea->fLeft > 0 &&
+ textureContentArea->fLeft + filterHalfWidth > constraintRect.fLeft) {
+ domainRect->fLeft = textureContentArea->fLeft + kDomainInset;
+ needContentAreaConstraint = true;
+ }
+ if (textureContentArea->fTop > 0 &&
+ textureContentArea->fTop + filterHalfWidth > constraintRect.fTop) {
+ domainRect->fTop = textureContentArea->fTop + kDomainInset;
+ needContentAreaConstraint = true;
+ }
+ if (textureContentArea->fRight < texW &&
+ textureContentArea->fRight - filterHalfWidth < constraintRect.fRight) {
+ domainRect->fRight = textureContentArea->fRight - kDomainInset;
+ needContentAreaConstraint = true;
+ }
+ if (textureContentArea->fBottom < texH &&
+ textureContentArea->fBottom - filterHalfWidth < constraintRect.fBottom) {
+ domainRect->fBottom = textureContentArea->fBottom - kDomainInset;
+ needContentAreaConstraint = true;
+ }
+ if (!needContentAreaConstraint) {
+ return kNoDomain_DomainMode;
+ }
+ } else {
+ // Our sample coords for the texture are allowed to be outside the constraintRect so we
+ // don't consider it when computing the domain.
+ if (textureContentArea->fLeft != 0) {
+ domainRect->fLeft = textureContentArea->fLeft + kDomainInset;
+ }
+ if (textureContentArea->fTop != 0) {
+ domainRect->fTop = textureContentArea->fTop + kDomainInset;
+ }
+ if (textureContentArea->fRight != texW) {
+ domainRect->fRight = textureContentArea->fRight - kDomainInset;
+ }
+ if (textureContentArea->fBottom != texH) {
+ domainRect->fBottom = textureContentArea->fBottom - kDomainInset;
+ }
+ }
+ } else {
+ return kNoDomain_DomainMode;
+ }
+
+ 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);
+ }
+ domainRect->fLeft /= texW;
+ domainRect->fTop /= texH;
+ domainRect->fRight /= texW;
+ domainRect->fBottom /= texH;
+ return kDomain_DomainMode;
+}
+
+const GrFragmentProcessor* GrTextureAdjuster::createFragmentProcessor(
+ const SkMatrix& textureMatrix,
+ const SkRect& constraintRect,
+ FilterConstraint filterConstraint,
+ bool coordsLimitedToConstraintRect,
+ const GrTextureParams::FilterMode* filterOrNullForBicubic) {
+
+ const SkIRect* contentArea = this->contentAreaOrNull();
+
+ SkRect domain;
+ GrTexture* texture = this->originalTexture();
+ DomainMode domainMode =
+ determine_domain_mode(constraintRect, filterConstraint, coordsLimitedToConstraintRect,
+ texture->width(), texture->height(),
+ contentArea, filterOrNullForBicubic,
+ &domain);
+ if (kTightCopy_DomainMode == domainMode) {
+ // TODO: Copy the texture and adjust the texture matrix (both parts need to consider
+ // non-int constraint rect)
+ // For now: treat as bilerp and ignore what goes on above level 0.
+
+ // We only expect MIP maps to require a tight copy.
+ SkASSERT(filterOrNullForBicubic &&
+ GrTextureParams::kMipMap_FilterMode == *filterOrNullForBicubic);
+ static const GrTextureParams::FilterMode kBilerp = GrTextureParams::kBilerp_FilterMode;
+ domainMode =
+ determine_domain_mode(constraintRect, filterConstraint, coordsLimitedToConstraintRect,
+ texture->width(), texture->height(),
+ contentArea, &kBilerp, &domain);
+ SkASSERT(kTightCopy_DomainMode != domainMode);
+ filterOrNullForBicubic = &kBilerp;
+ }
+ SkASSERT(kNoDomain_DomainMode == domainMode ||
+ (domain.fLeft <= domain.fRight && domain.fTop <= domain.fBottom));
+ if (filterOrNullForBicubic) {
+ if (kDomain_DomainMode == domainMode) {
+ SkASSERT(*filterOrNullForBicubic != GrTextureParams::kMipMap_FilterMode);
+ return GrTextureDomainEffect::Create(texture, textureMatrix, domain,
+ GrTextureDomain::kClamp_Mode,
+ *filterOrNullForBicubic);
+ } else {
+ GrTextureParams params(SkShader::kClamp_TileMode, *filterOrNullForBicubic);
+ return GrSimpleTextureEffect::Create(texture, textureMatrix, params);
+ }
+ } else {
+ if (kDomain_DomainMode == domainMode) {
+ return GrBicubicEffect::Create(texture, textureMatrix, domain);
+ } else {
+ static const SkShader::TileMode kClampClamp[] =
+ { SkShader::kClamp_TileMode, SkShader::kClamp_TileMode };
+ return GrBicubicEffect::Create(texture, textureMatrix, kClampClamp);
+ }
+ }
+}
+
//////////////////////////////////////////////////////////////////////////////
GrTexture* GrTextureMaker::refTextureForParams(GrContext* ctx, const GrTextureParams& params) {