bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include "GrTextureParamsAdjuster.h" |
| 9 | |
| 10 | #include "GrCaps.h" |
| 11 | #include "GrContext.h" |
| 12 | #include "GrDrawContext.h" |
| 13 | #include "GrGpu.h" |
bsalomon | 89fe56b | 2015-10-29 10:49:28 -0700 | [diff] [blame] | 14 | #include "GrGpuResourcePriv.h" |
| 15 | #include "GrResourceKey.h" |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 16 | #include "GrTexture.h" |
| 17 | #include "GrTextureParams.h" |
| 18 | #include "GrTextureProvider.h" |
| 19 | #include "SkCanvas.h" |
| 20 | #include "SkGr.h" |
| 21 | #include "SkGrPriv.h" |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 22 | #include "effects/GrBicubicEffect.h" |
bsalomon | 89fe56b | 2015-10-29 10:49:28 -0700 | [diff] [blame] | 23 | #include "effects/GrTextureDomain.h" |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 24 | |
bsalomon | 89fe56b | 2015-10-29 10:49:28 -0700 | [diff] [blame] | 25 | typedef GrTextureProducer::CopyParams CopyParams; |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 26 | |
bsalomon | 89fe56b | 2015-10-29 10:49:28 -0700 | [diff] [blame] | 27 | ////////////////////////////////////////////////////////////////////////////// |
| 28 | |
| 29 | static GrTexture* copy_on_gpu(GrTexture* inputTexture, const SkIRect* subset, |
| 30 | const CopyParams& copyParams) { |
| 31 | SkASSERT(!subset || !subset->isEmpty()); |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 32 | GrContext* context = inputTexture->getContext(); |
| 33 | SkASSERT(context); |
| 34 | const GrCaps* caps = context->caps(); |
| 35 | |
| 36 | // Either it's a cache miss or the original wasn't cached to begin with. |
| 37 | GrSurfaceDesc rtDesc = inputTexture->desc(); |
bsalomon | 89fe56b | 2015-10-29 10:49:28 -0700 | [diff] [blame] | 38 | rtDesc.fFlags = rtDesc.fFlags | kRenderTarget_GrSurfaceFlag; |
| 39 | rtDesc.fWidth = copyParams.fWidth; |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 40 | rtDesc.fHeight = copyParams.fHeight; |
| 41 | rtDesc.fConfig = GrMakePixelConfigUncompressed(rtDesc.fConfig); |
| 42 | |
| 43 | // If the config isn't renderable try converting to either A8 or an 32 bit config. Otherwise, |
| 44 | // fail. |
| 45 | if (!caps->isConfigRenderable(rtDesc.fConfig, false)) { |
| 46 | if (GrPixelConfigIsAlphaOnly(rtDesc.fConfig)) { |
| 47 | if (caps->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) { |
| 48 | rtDesc.fConfig = kAlpha_8_GrPixelConfig; |
| 49 | } else if (caps->isConfigRenderable(kSkia8888_GrPixelConfig, false)) { |
| 50 | rtDesc.fConfig = kSkia8888_GrPixelConfig; |
| 51 | } else { |
| 52 | return nullptr; |
| 53 | } |
| 54 | } else if (kRGB_GrColorComponentFlags == |
| 55 | (kRGB_GrColorComponentFlags & GrPixelConfigComponentMask(rtDesc.fConfig))) { |
| 56 | if (caps->isConfigRenderable(kSkia8888_GrPixelConfig, false)) { |
| 57 | rtDesc.fConfig = kSkia8888_GrPixelConfig; |
| 58 | } else { |
| 59 | return nullptr; |
| 60 | } |
| 61 | } else { |
| 62 | return nullptr; |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | SkAutoTUnref<GrTexture> copy(context->textureProvider()->createTexture(rtDesc, true)); |
| 67 | if (!copy) { |
| 68 | return nullptr; |
| 69 | } |
| 70 | |
bsalomon | 89fe56b | 2015-10-29 10:49:28 -0700 | [diff] [blame] | 71 | // TODO: If no scaling is being performed then use copySurface. |
| 72 | |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 73 | GrPaint paint; |
| 74 | |
egdaniel | 0a0605d | 2015-11-23 15:29:36 -0800 | [diff] [blame] | 75 | // TODO: Initializing these values for no reason cause the compiler is complaining |
| 76 | SkScalar sx = 0.f; |
| 77 | SkScalar sy = 0.f; |
bsalomon | 89fe56b | 2015-10-29 10:49:28 -0700 | [diff] [blame] | 78 | if (subset) { |
| 79 | sx = 1.f / inputTexture->width(); |
| 80 | sy = 1.f / inputTexture->height(); |
| 81 | } |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 82 | |
bsalomon | 89fe56b | 2015-10-29 10:49:28 -0700 | [diff] [blame] | 83 | if (copyParams.fFilter != GrTextureParams::kNone_FilterMode && subset && |
| 84 | (subset->width() != copyParams.fWidth || subset->height() != copyParams.fHeight)) { |
| 85 | SkRect domain; |
| 86 | domain.fLeft = (subset->fLeft + 0.5f) * sx; |
| 87 | domain.fTop = (subset->fTop + 0.5f)* sy; |
| 88 | domain.fRight = (subset->fRight - 0.5f) * sx; |
| 89 | domain.fBottom = (subset->fBottom - 0.5f) * sy; |
| 90 | // This would cause us to read values from outside the subset. Surely, the caller knows |
| 91 | // better! |
| 92 | SkASSERT(copyParams.fFilter != GrTextureParams::kMipMap_FilterMode); |
| 93 | paint.addColorFragmentProcessor( |
| 94 | GrTextureDomainEffect::Create(inputTexture, SkMatrix::I(), domain, |
| 95 | GrTextureDomain::kClamp_Mode, |
| 96 | copyParams.fFilter))->unref(); |
| 97 | } else { |
| 98 | GrTextureParams params(SkShader::kClamp_TileMode, copyParams.fFilter); |
| 99 | paint.addColorTextureProcessor(inputTexture, SkMatrix::I(), params); |
| 100 | } |
egdaniel | 813351f | 2015-11-23 15:12:23 -0800 | [diff] [blame] | 101 | paint.setPorterDuffXPFactory(SkXfermode::kSrc_Mode); |
bsalomon | 89fe56b | 2015-10-29 10:49:28 -0700 | [diff] [blame] | 102 | |
| 103 | SkRect localRect; |
| 104 | if (subset) { |
| 105 | localRect = SkRect::Make(*subset); |
| 106 | localRect.fLeft *= sx; |
| 107 | localRect.fTop *= sy; |
| 108 | localRect.fRight *= sx; |
| 109 | localRect.fBottom *= sy; |
| 110 | } else { |
| 111 | localRect = SkRect::MakeWH(1.f, 1.f); |
| 112 | } |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 113 | |
| 114 | SkAutoTUnref<GrDrawContext> drawContext(context->drawContext(copy->asRenderTarget())); |
| 115 | if (!drawContext) { |
| 116 | return nullptr; |
| 117 | } |
| 118 | |
bsalomon | 89fe56b | 2015-10-29 10:49:28 -0700 | [diff] [blame] | 119 | SkRect dstRect = SkRect::MakeWH(SkIntToScalar(rtDesc.fWidth), SkIntToScalar(rtDesc.fHeight)); |
bsalomon | a2e69fc | 2015-11-05 10:41:43 -0800 | [diff] [blame] | 120 | drawContext->fillRectToRect(GrClip::WideOpen(), paint, SkMatrix::I(), dstRect, localRect); |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 121 | return copy.detach(); |
| 122 | } |
| 123 | |
bsalomon | f1ecd21 | 2015-12-09 17:06:02 -0800 | [diff] [blame^] | 124 | GrTextureAdjuster::GrTextureAdjuster(GrTexture* original, |
| 125 | const SkIRect& contentArea, |
| 126 | bool isAlphaOnly) |
| 127 | : INHERITED(contentArea.width(), contentArea.height(), isAlphaOnly) |
bsalomon | 3aa5fce | 2015-11-12 09:59:44 -0800 | [diff] [blame] | 128 | , fOriginal(original) { |
| 129 | SkASSERT(SkIRect::MakeWH(original->width(), original->height()).contains(contentArea)); |
bsalomon | c75be34 | 2015-10-29 12:34:31 -0700 | [diff] [blame] | 130 | if (contentArea.fLeft > 0 || contentArea.fTop > 0 || |
| 131 | contentArea.fRight < original->width() || contentArea.fBottom < original->height()) { |
| 132 | fContentArea.set(contentArea); |
bsalomon | 89fe56b | 2015-10-29 10:49:28 -0700 | [diff] [blame] | 133 | } |
| 134 | } |
| 135 | |
| 136 | GrTexture* GrTextureAdjuster::refTextureSafeForParams(const GrTextureParams& params, |
| 137 | SkIPoint* outOffset) { |
| 138 | GrTexture* texture = this->originalTexture(); |
| 139 | GrContext* context = texture->getContext(); |
| 140 | CopyParams copyParams; |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 141 | const SkIRect* contentArea = this->contentAreaOrNull(); |
bsalomon | 89fe56b | 2015-10-29 10:49:28 -0700 | [diff] [blame] | 142 | |
bsalomon | c75be34 | 2015-10-29 12:34:31 -0700 | [diff] [blame] | 143 | if (contentArea && GrTextureParams::kMipMap_FilterMode == params.filterMode()) { |
| 144 | // If we generate a MIP chain for texture it will read pixel values from outside the content |
| 145 | // area. |
| 146 | copyParams.fWidth = contentArea->width(); |
| 147 | copyParams.fHeight = contentArea->height(); |
| 148 | copyParams.fFilter = GrTextureParams::kBilerp_FilterMode; |
| 149 | } else if (!context->getGpu()->makeCopyForTextureParams(texture->width(), texture->height(), |
| 150 | params, ©Params)) { |
bsalomon | 89fe56b | 2015-10-29 10:49:28 -0700 | [diff] [blame] | 151 | if (outOffset) { |
bsalomon | c75be34 | 2015-10-29 12:34:31 -0700 | [diff] [blame] | 152 | if (contentArea) { |
| 153 | outOffset->set(contentArea->fLeft, contentArea->fRight); |
bsalomon | 89fe56b | 2015-10-29 10:49:28 -0700 | [diff] [blame] | 154 | } else { |
| 155 | outOffset->set(0, 0); |
| 156 | } |
| 157 | } |
| 158 | return SkRef(texture); |
| 159 | } |
| 160 | GrUniqueKey key; |
| 161 | this->makeCopyKey(copyParams, &key); |
| 162 | if (key.isValid()) { |
| 163 | GrTexture* result = context->textureProvider()->findAndRefTextureByUniqueKey(key); |
| 164 | if (result) { |
| 165 | return result; |
| 166 | } |
| 167 | } |
bsalomon | c75be34 | 2015-10-29 12:34:31 -0700 | [diff] [blame] | 168 | GrTexture* result = copy_on_gpu(texture, contentArea, copyParams); |
bsalomon | 89fe56b | 2015-10-29 10:49:28 -0700 | [diff] [blame] | 169 | if (result) { |
| 170 | if (key.isValid()) { |
| 171 | result->resourcePriv().setUniqueKey(key); |
| 172 | this->didCacheCopy(key); |
| 173 | } |
| 174 | if (outOffset) { |
| 175 | outOffset->set(0, 0); |
| 176 | } |
| 177 | } |
| 178 | return result; |
| 179 | } |
| 180 | |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 181 | enum DomainMode { |
| 182 | kNoDomain_DomainMode, |
| 183 | kDomain_DomainMode, |
| 184 | kTightCopy_DomainMode |
| 185 | }; |
| 186 | |
| 187 | /** Determines whether a texture domain is necessary and if so what domain to use. There are two |
| 188 | * rectangles to consider: |
| 189 | * - The first is the content area specified by the texture adjuster. We can *never* allow |
| 190 | * filtering to cause bleed of pixels outside this rectangle. |
| 191 | * - The second rectangle is the constraint rectangle, which is known to be contained by the |
| 192 | * content area. The filterConstraint specifies whether we are allowed to bleed across this |
| 193 | * rect. |
| 194 | * |
| 195 | * We want to avoid using a domain if possible. We consider the above rectangles, the filter type, |
| 196 | * and whether the coords generated by the draw would all fall within the constraint rect. If the |
| 197 | * latter is true we only need to consider whether the filter would extend beyond the rects. |
| 198 | */ |
| 199 | static DomainMode determine_domain_mode( |
| 200 | const SkRect& constraintRect, |
| 201 | GrTextureAdjuster::FilterConstraint filterConstraint, |
| 202 | bool coordsLimitedToConstraintRect, |
| 203 | int texW, int texH, |
| 204 | const SkIRect* textureContentArea, |
| 205 | const GrTextureParams::FilterMode* filterModeOrNullForBicubic, |
| 206 | SkRect* domainRect) { |
| 207 | |
| 208 | SkASSERT(SkRect::MakeIWH(texW, texH).contains(constraintRect)); |
| 209 | // We only expect a content area rect if there is some non-content area. |
| 210 | SkASSERT(!textureContentArea || |
| 211 | (!textureContentArea->contains(SkIRect::MakeWH(texW, texH)) && |
| 212 | SkRect::Make(*textureContentArea).contains(constraintRect))); |
| 213 | |
| 214 | SkRect textureBounds = SkRect::MakeIWH(texW, texH); |
| 215 | // If the src rectangle contains the whole texture then no need for a domain. |
| 216 | if (constraintRect.contains(textureBounds)) { |
| 217 | return kNoDomain_DomainMode; |
| 218 | } |
| 219 | |
bsalomon | b1b0199 | 2015-11-18 10:56:08 -0800 | [diff] [blame] | 220 | bool restrictFilterToRect = (filterConstraint == GrTextureProducer::kYes_FilterConstraint); |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 221 | |
| 222 | // If we can filter outside the constraint rect, and there is no non-content area of the |
| 223 | // texture, and we aren't going to generate sample coords outside the constraint rect then we |
| 224 | // don't need a domain. |
| 225 | if (!restrictFilterToRect && !textureContentArea && coordsLimitedToConstraintRect) { |
| 226 | return kNoDomain_DomainMode; |
| 227 | } |
| 228 | |
| 229 | // Get the domain inset based on sampling mode (or bail if mipped) |
| 230 | SkScalar filterHalfWidth = 0.f; |
| 231 | if (filterModeOrNullForBicubic) { |
| 232 | switch (*filterModeOrNullForBicubic) { |
| 233 | case GrTextureParams::kNone_FilterMode: |
| 234 | if (coordsLimitedToConstraintRect) { |
| 235 | return kNoDomain_DomainMode; |
| 236 | } else { |
| 237 | filterHalfWidth = 0.f; |
| 238 | } |
| 239 | break; |
| 240 | case GrTextureParams::kBilerp_FilterMode: |
| 241 | filterHalfWidth = .5f; |
| 242 | break; |
| 243 | case GrTextureParams::kMipMap_FilterMode: |
bsalomon | b1b0199 | 2015-11-18 10:56:08 -0800 | [diff] [blame] | 244 | if (restrictFilterToRect || textureContentArea) { |
| 245 | // No domain can save us here. |
| 246 | return kTightCopy_DomainMode; |
| 247 | } |
| 248 | return kNoDomain_DomainMode; |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 249 | } |
| 250 | } else { |
| 251 | // bicubic does nearest filtering internally. |
| 252 | filterHalfWidth = 1.5f; |
| 253 | } |
| 254 | |
| 255 | // Both bilerp and bicubic use bilinear filtering and so need to be clamped to the center |
| 256 | // of the edge texel. Pinning to the texel center has no impact on nearest mode and MIP-maps |
| 257 | |
| 258 | static const SkScalar kDomainInset = 0.5f; |
| 259 | // Figure out the limits of pixels we're allowed to sample from. |
| 260 | // Unless we know the amount of outset and the texture matrix we have to conservatively enforce |
| 261 | // the domain. |
| 262 | if (restrictFilterToRect) { |
| 263 | domainRect->fLeft = constraintRect.fLeft + kDomainInset; |
| 264 | domainRect->fTop = constraintRect.fTop + kDomainInset; |
| 265 | domainRect->fRight = constraintRect.fRight - kDomainInset; |
| 266 | domainRect->fBottom = constraintRect.fBottom - kDomainInset; |
| 267 | } else if (textureContentArea) { |
| 268 | // If we got here then: there is a textureContentArea, the coords are limited to the |
| 269 | // constraint rect, and we're allowed to filter across the constraint rect boundary. So |
| 270 | // we check whether the filter would reach across the edge of the content area. |
| 271 | // We will only set the sides that are required. |
| 272 | |
| 273 | domainRect->setLargest(); |
| 274 | if (coordsLimitedToConstraintRect) { |
| 275 | // We may be able to use the fact that the texture coords are limited to the constraint |
| 276 | // rect in order to avoid having to add a domain. |
| 277 | bool needContentAreaConstraint = false; |
| 278 | if (textureContentArea->fLeft > 0 && |
| 279 | textureContentArea->fLeft + filterHalfWidth > constraintRect.fLeft) { |
| 280 | domainRect->fLeft = textureContentArea->fLeft + kDomainInset; |
| 281 | needContentAreaConstraint = true; |
| 282 | } |
| 283 | if (textureContentArea->fTop > 0 && |
| 284 | textureContentArea->fTop + filterHalfWidth > constraintRect.fTop) { |
| 285 | domainRect->fTop = textureContentArea->fTop + kDomainInset; |
| 286 | needContentAreaConstraint = true; |
| 287 | } |
| 288 | if (textureContentArea->fRight < texW && |
| 289 | textureContentArea->fRight - filterHalfWidth < constraintRect.fRight) { |
| 290 | domainRect->fRight = textureContentArea->fRight - kDomainInset; |
| 291 | needContentAreaConstraint = true; |
| 292 | } |
| 293 | if (textureContentArea->fBottom < texH && |
| 294 | textureContentArea->fBottom - filterHalfWidth < constraintRect.fBottom) { |
| 295 | domainRect->fBottom = textureContentArea->fBottom - kDomainInset; |
| 296 | needContentAreaConstraint = true; |
| 297 | } |
| 298 | if (!needContentAreaConstraint) { |
| 299 | return kNoDomain_DomainMode; |
| 300 | } |
| 301 | } else { |
| 302 | // Our sample coords for the texture are allowed to be outside the constraintRect so we |
| 303 | // don't consider it when computing the domain. |
| 304 | if (textureContentArea->fLeft != 0) { |
| 305 | domainRect->fLeft = textureContentArea->fLeft + kDomainInset; |
| 306 | } |
| 307 | if (textureContentArea->fTop != 0) { |
| 308 | domainRect->fTop = textureContentArea->fTop + kDomainInset; |
| 309 | } |
| 310 | if (textureContentArea->fRight != texW) { |
| 311 | domainRect->fRight = textureContentArea->fRight - kDomainInset; |
| 312 | } |
| 313 | if (textureContentArea->fBottom != texH) { |
| 314 | domainRect->fBottom = textureContentArea->fBottom - kDomainInset; |
| 315 | } |
| 316 | } |
| 317 | } else { |
| 318 | return kNoDomain_DomainMode; |
| 319 | } |
| 320 | |
| 321 | if (domainRect->fLeft > domainRect->fRight) { |
| 322 | domainRect->fLeft = domainRect->fRight = SkScalarAve(domainRect->fLeft, domainRect->fRight); |
| 323 | } |
| 324 | if (domainRect->fTop > domainRect->fBottom) { |
| 325 | domainRect->fTop = domainRect->fBottom = SkScalarAve(domainRect->fTop, domainRect->fBottom); |
| 326 | } |
| 327 | domainRect->fLeft /= texW; |
| 328 | domainRect->fTop /= texH; |
| 329 | domainRect->fRight /= texW; |
| 330 | domainRect->fBottom /= texH; |
| 331 | return kDomain_DomainMode; |
| 332 | } |
| 333 | |
bsalomon | b1b0199 | 2015-11-18 10:56:08 -0800 | [diff] [blame] | 334 | static const GrFragmentProcessor* create_fp_for_domain_and_filter( |
| 335 | GrTexture* texture, |
| 336 | const SkMatrix& textureMatrix, |
| 337 | DomainMode domainMode, |
| 338 | const SkRect& domain, |
| 339 | const GrTextureParams::FilterMode* filterOrNullForBicubic) { |
| 340 | SkASSERT(kTightCopy_DomainMode != domainMode); |
| 341 | if (filterOrNullForBicubic) { |
| 342 | if (kDomain_DomainMode == domainMode) { |
| 343 | return GrTextureDomainEffect::Create(texture, textureMatrix, domain, |
| 344 | GrTextureDomain::kClamp_Mode, |
| 345 | *filterOrNullForBicubic); |
| 346 | } else { |
| 347 | GrTextureParams params(SkShader::kClamp_TileMode, *filterOrNullForBicubic); |
| 348 | return GrSimpleTextureEffect::Create(texture, textureMatrix, params); |
| 349 | } |
| 350 | } else { |
| 351 | if (kDomain_DomainMode == domainMode) { |
| 352 | return GrBicubicEffect::Create(texture, textureMatrix, domain); |
| 353 | } else { |
| 354 | static const SkShader::TileMode kClampClamp[] = |
| 355 | { SkShader::kClamp_TileMode, SkShader::kClamp_TileMode }; |
| 356 | return GrBicubicEffect::Create(texture, textureMatrix, kClampClamp); |
| 357 | } |
| 358 | } |
| 359 | } |
| 360 | |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 361 | const GrFragmentProcessor* GrTextureAdjuster::createFragmentProcessor( |
bsalomon | 3aa5fce | 2015-11-12 09:59:44 -0800 | [diff] [blame] | 362 | const SkMatrix& origTextureMatrix, |
| 363 | const SkRect& origConstraintRect, |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 364 | FilterConstraint filterConstraint, |
| 365 | bool coordsLimitedToConstraintRect, |
| 366 | const GrTextureParams::FilterMode* filterOrNullForBicubic) { |
| 367 | |
bsalomon | 3aa5fce | 2015-11-12 09:59:44 -0800 | [diff] [blame] | 368 | SkMatrix textureMatrix = origTextureMatrix; |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 369 | const SkIRect* contentArea = this->contentAreaOrNull(); |
bsalomon | 3aa5fce | 2015-11-12 09:59:44 -0800 | [diff] [blame] | 370 | // Convert the constraintRect to be relative to the texture rather than the content area so |
| 371 | // that both rects are in the same coordinate system. |
| 372 | SkTCopyOnFirstWrite<SkRect> constraintRect(origConstraintRect); |
| 373 | if (contentArea) { |
| 374 | SkScalar l = SkIntToScalar(contentArea->fLeft); |
| 375 | SkScalar t = SkIntToScalar(contentArea->fTop); |
| 376 | constraintRect.writable()->offset(l, t); |
| 377 | textureMatrix.postTranslate(l, t); |
| 378 | } |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 379 | |
| 380 | SkRect domain; |
| 381 | GrTexture* texture = this->originalTexture(); |
| 382 | DomainMode domainMode = |
bsalomon | 3aa5fce | 2015-11-12 09:59:44 -0800 | [diff] [blame] | 383 | determine_domain_mode(*constraintRect, filterConstraint, coordsLimitedToConstraintRect, |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 384 | texture->width(), texture->height(), |
| 385 | contentArea, filterOrNullForBicubic, |
| 386 | &domain); |
| 387 | if (kTightCopy_DomainMode == domainMode) { |
| 388 | // TODO: Copy the texture and adjust the texture matrix (both parts need to consider |
| 389 | // non-int constraint rect) |
| 390 | // For now: treat as bilerp and ignore what goes on above level 0. |
| 391 | |
| 392 | // We only expect MIP maps to require a tight copy. |
| 393 | SkASSERT(filterOrNullForBicubic && |
| 394 | GrTextureParams::kMipMap_FilterMode == *filterOrNullForBicubic); |
| 395 | static const GrTextureParams::FilterMode kBilerp = GrTextureParams::kBilerp_FilterMode; |
| 396 | domainMode = |
bsalomon | 3aa5fce | 2015-11-12 09:59:44 -0800 | [diff] [blame] | 397 | determine_domain_mode(*constraintRect, filterConstraint, coordsLimitedToConstraintRect, |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 398 | texture->width(), texture->height(), |
| 399 | contentArea, &kBilerp, &domain); |
| 400 | SkASSERT(kTightCopy_DomainMode != domainMode); |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 401 | } |
| 402 | SkASSERT(kNoDomain_DomainMode == domainMode || |
| 403 | (domain.fLeft <= domain.fRight && domain.fTop <= domain.fBottom)); |
bsalomon | 3aa5fce | 2015-11-12 09:59:44 -0800 | [diff] [blame] | 404 | textureMatrix.postIDiv(texture->width(), texture->height()); |
bsalomon | b1b0199 | 2015-11-18 10:56:08 -0800 | [diff] [blame] | 405 | return create_fp_for_domain_and_filter(texture, textureMatrix, domainMode, domain, |
| 406 | filterOrNullForBicubic); |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 407 | } |
| 408 | |
bsalomon | 89fe56b | 2015-10-29 10:49:28 -0700 | [diff] [blame] | 409 | ////////////////////////////////////////////////////////////////////////////// |
| 410 | |
bsalomon | b1b0199 | 2015-11-18 10:56:08 -0800 | [diff] [blame] | 411 | GrTexture* GrTextureMaker::refTextureForParams(const GrTextureParams& params) { |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 412 | CopyParams copyParams; |
bsalomon | b1b0199 | 2015-11-18 10:56:08 -0800 | [diff] [blame] | 413 | if (!fContext->getGpu()->makeCopyForTextureParams(this->width(), this->height(), params, |
| 414 | ©Params)) { |
| 415 | return this->refOriginalTexture(); |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 416 | } |
| 417 | GrUniqueKey copyKey; |
| 418 | this->makeCopyKey(copyParams, ©Key); |
| 419 | if (copyKey.isValid()) { |
bsalomon | b1b0199 | 2015-11-18 10:56:08 -0800 | [diff] [blame] | 420 | GrTexture* result = fContext->textureProvider()->findAndRefTextureByUniqueKey(copyKey); |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 421 | if (result) { |
| 422 | return result; |
| 423 | } |
| 424 | } |
| 425 | |
bsalomon | b1b0199 | 2015-11-18 10:56:08 -0800 | [diff] [blame] | 426 | GrTexture* result = this->generateTextureForParams(copyParams); |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 427 | if (!result) { |
| 428 | return nullptr; |
| 429 | } |
| 430 | |
| 431 | if (copyKey.isValid()) { |
bsalomon | b1b0199 | 2015-11-18 10:56:08 -0800 | [diff] [blame] | 432 | fContext->textureProvider()->assignUniqueKeyToTexture(copyKey, result); |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 433 | this->didCacheCopy(copyKey); |
| 434 | } |
| 435 | return result; |
| 436 | } |
| 437 | |
bsalomon | b1b0199 | 2015-11-18 10:56:08 -0800 | [diff] [blame] | 438 | const GrFragmentProcessor* GrTextureMaker::createFragmentProcessor( |
| 439 | const SkMatrix& textureMatrix, |
| 440 | const SkRect& constraintRect, |
| 441 | FilterConstraint filterConstraint, |
| 442 | bool coordsLimitedToConstraintRect, |
| 443 | const GrTextureParams::FilterMode* filterOrNullForBicubic) { |
| 444 | |
| 445 | const GrTextureParams::FilterMode* fmForDetermineDomain = filterOrNullForBicubic; |
| 446 | if (filterOrNullForBicubic && GrTextureParams::kMipMap_FilterMode == *filterOrNullForBicubic && |
| 447 | kYes_FilterConstraint == filterConstraint) { |
| 448 | // TODo: Here we should force a copy restricted to the constraintRect since MIP maps will |
| 449 | // read outside the constraint rect. However, as in the adjuster case, we aren't currently |
| 450 | // doing that. |
| 451 | // We instead we compute the domain as though were bilerping which is only correct if we |
| 452 | // only sample level 0. |
| 453 | static const GrTextureParams::FilterMode kBilerp = GrTextureParams::kBilerp_FilterMode; |
| 454 | fmForDetermineDomain = &kBilerp; |
| 455 | } |
| 456 | |
| 457 | GrTextureParams params; |
| 458 | if (filterOrNullForBicubic) { |
| 459 | params.reset(SkShader::kClamp_TileMode, *filterOrNullForBicubic); |
| 460 | } else { |
| 461 | // Bicubic doesn't use filtering for it's texture accesses. |
| 462 | params.reset(SkShader::kClamp_TileMode, GrTextureParams::kNone_FilterMode); |
| 463 | } |
| 464 | SkAutoTUnref<GrTexture> texture(this->refTextureForParams(params)); |
| 465 | if (!texture) { |
| 466 | return nullptr; |
| 467 | } |
| 468 | SkRect domain; |
| 469 | DomainMode domainMode = |
| 470 | determine_domain_mode(constraintRect, filterConstraint, coordsLimitedToConstraintRect, |
| 471 | texture->width(), texture->height(), nullptr, fmForDetermineDomain, |
| 472 | &domain); |
| 473 | SkASSERT(kTightCopy_DomainMode != domainMode); |
| 474 | SkMatrix normalizedTextureMatrix = textureMatrix; |
| 475 | normalizedTextureMatrix.postIDiv(texture->width(), texture->height()); |
| 476 | return create_fp_for_domain_and_filter(texture, normalizedTextureMatrix, domainMode, domain, |
| 477 | filterOrNullForBicubic); |
| 478 | } |
| 479 | |
| 480 | GrTexture* GrTextureMaker::generateTextureForParams(const CopyParams& copyParams) { |
| 481 | SkAutoTUnref<GrTexture> original(this->refOriginalTexture()); |
bsalomon | 100b8f8 | 2015-10-28 08:37:44 -0700 | [diff] [blame] | 482 | if (!original) { |
| 483 | return nullptr; |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 484 | } |
bsalomon | 89fe56b | 2015-10-29 10:49:28 -0700 | [diff] [blame] | 485 | return copy_on_gpu(original, nullptr, copyParams); |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 486 | } |