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 | c75be34 | 2015-10-29 12:34:31 -0700 | [diff] [blame] | 124 | GrTextureAdjuster::GrTextureAdjuster(GrTexture* original, const SkIRect& contentArea) |
bsalomon | 3aa5fce | 2015-11-12 09:59:44 -0800 | [diff] [blame] | 125 | : INHERITED(contentArea.width(), contentArea.height()) |
| 126 | , fOriginal(original) { |
| 127 | SkASSERT(SkIRect::MakeWH(original->width(), original->height()).contains(contentArea)); |
bsalomon | c75be34 | 2015-10-29 12:34:31 -0700 | [diff] [blame] | 128 | if (contentArea.fLeft > 0 || contentArea.fTop > 0 || |
| 129 | contentArea.fRight < original->width() || contentArea.fBottom < original->height()) { |
| 130 | fContentArea.set(contentArea); |
bsalomon | 89fe56b | 2015-10-29 10:49:28 -0700 | [diff] [blame] | 131 | } |
| 132 | } |
| 133 | |
| 134 | GrTexture* GrTextureAdjuster::refTextureSafeForParams(const GrTextureParams& params, |
| 135 | SkIPoint* outOffset) { |
| 136 | GrTexture* texture = this->originalTexture(); |
| 137 | GrContext* context = texture->getContext(); |
| 138 | CopyParams copyParams; |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 139 | const SkIRect* contentArea = this->contentAreaOrNull(); |
bsalomon | 89fe56b | 2015-10-29 10:49:28 -0700 | [diff] [blame] | 140 | |
bsalomon | c75be34 | 2015-10-29 12:34:31 -0700 | [diff] [blame] | 141 | if (contentArea && GrTextureParams::kMipMap_FilterMode == params.filterMode()) { |
| 142 | // If we generate a MIP chain for texture it will read pixel values from outside the content |
| 143 | // area. |
| 144 | copyParams.fWidth = contentArea->width(); |
| 145 | copyParams.fHeight = contentArea->height(); |
| 146 | copyParams.fFilter = GrTextureParams::kBilerp_FilterMode; |
| 147 | } else if (!context->getGpu()->makeCopyForTextureParams(texture->width(), texture->height(), |
| 148 | params, ©Params)) { |
bsalomon | 89fe56b | 2015-10-29 10:49:28 -0700 | [diff] [blame] | 149 | if (outOffset) { |
bsalomon | c75be34 | 2015-10-29 12:34:31 -0700 | [diff] [blame] | 150 | if (contentArea) { |
| 151 | outOffset->set(contentArea->fLeft, contentArea->fRight); |
bsalomon | 89fe56b | 2015-10-29 10:49:28 -0700 | [diff] [blame] | 152 | } else { |
| 153 | outOffset->set(0, 0); |
| 154 | } |
| 155 | } |
| 156 | return SkRef(texture); |
| 157 | } |
| 158 | GrUniqueKey key; |
| 159 | this->makeCopyKey(copyParams, &key); |
| 160 | if (key.isValid()) { |
| 161 | GrTexture* result = context->textureProvider()->findAndRefTextureByUniqueKey(key); |
| 162 | if (result) { |
| 163 | return result; |
| 164 | } |
| 165 | } |
bsalomon | c75be34 | 2015-10-29 12:34:31 -0700 | [diff] [blame] | 166 | GrTexture* result = copy_on_gpu(texture, contentArea, copyParams); |
bsalomon | 89fe56b | 2015-10-29 10:49:28 -0700 | [diff] [blame] | 167 | if (result) { |
| 168 | if (key.isValid()) { |
| 169 | result->resourcePriv().setUniqueKey(key); |
| 170 | this->didCacheCopy(key); |
| 171 | } |
| 172 | if (outOffset) { |
| 173 | outOffset->set(0, 0); |
| 174 | } |
| 175 | } |
| 176 | return result; |
| 177 | } |
| 178 | |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 179 | enum DomainMode { |
| 180 | kNoDomain_DomainMode, |
| 181 | kDomain_DomainMode, |
| 182 | kTightCopy_DomainMode |
| 183 | }; |
| 184 | |
| 185 | /** Determines whether a texture domain is necessary and if so what domain to use. There are two |
| 186 | * rectangles to consider: |
| 187 | * - The first is the content area specified by the texture adjuster. We can *never* allow |
| 188 | * filtering to cause bleed of pixels outside this rectangle. |
| 189 | * - The second rectangle is the constraint rectangle, which is known to be contained by the |
| 190 | * content area. The filterConstraint specifies whether we are allowed to bleed across this |
| 191 | * rect. |
| 192 | * |
| 193 | * We want to avoid using a domain if possible. We consider the above rectangles, the filter type, |
| 194 | * and whether the coords generated by the draw would all fall within the constraint rect. If the |
| 195 | * latter is true we only need to consider whether the filter would extend beyond the rects. |
| 196 | */ |
| 197 | static DomainMode determine_domain_mode( |
| 198 | const SkRect& constraintRect, |
| 199 | GrTextureAdjuster::FilterConstraint filterConstraint, |
| 200 | bool coordsLimitedToConstraintRect, |
| 201 | int texW, int texH, |
| 202 | const SkIRect* textureContentArea, |
| 203 | const GrTextureParams::FilterMode* filterModeOrNullForBicubic, |
| 204 | SkRect* domainRect) { |
| 205 | |
| 206 | SkASSERT(SkRect::MakeIWH(texW, texH).contains(constraintRect)); |
| 207 | // We only expect a content area rect if there is some non-content area. |
| 208 | SkASSERT(!textureContentArea || |
| 209 | (!textureContentArea->contains(SkIRect::MakeWH(texW, texH)) && |
| 210 | SkRect::Make(*textureContentArea).contains(constraintRect))); |
| 211 | |
| 212 | SkRect textureBounds = SkRect::MakeIWH(texW, texH); |
| 213 | // If the src rectangle contains the whole texture then no need for a domain. |
| 214 | if (constraintRect.contains(textureBounds)) { |
| 215 | return kNoDomain_DomainMode; |
| 216 | } |
| 217 | |
bsalomon | b1b0199 | 2015-11-18 10:56:08 -0800 | [diff] [blame] | 218 | bool restrictFilterToRect = (filterConstraint == GrTextureProducer::kYes_FilterConstraint); |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 219 | |
| 220 | // If we can filter outside the constraint rect, and there is no non-content area of the |
| 221 | // texture, and we aren't going to generate sample coords outside the constraint rect then we |
| 222 | // don't need a domain. |
| 223 | if (!restrictFilterToRect && !textureContentArea && coordsLimitedToConstraintRect) { |
| 224 | return kNoDomain_DomainMode; |
| 225 | } |
| 226 | |
| 227 | // Get the domain inset based on sampling mode (or bail if mipped) |
| 228 | SkScalar filterHalfWidth = 0.f; |
| 229 | if (filterModeOrNullForBicubic) { |
| 230 | switch (*filterModeOrNullForBicubic) { |
| 231 | case GrTextureParams::kNone_FilterMode: |
| 232 | if (coordsLimitedToConstraintRect) { |
| 233 | return kNoDomain_DomainMode; |
| 234 | } else { |
| 235 | filterHalfWidth = 0.f; |
| 236 | } |
| 237 | break; |
| 238 | case GrTextureParams::kBilerp_FilterMode: |
| 239 | filterHalfWidth = .5f; |
| 240 | break; |
| 241 | case GrTextureParams::kMipMap_FilterMode: |
bsalomon | b1b0199 | 2015-11-18 10:56:08 -0800 | [diff] [blame] | 242 | if (restrictFilterToRect || textureContentArea) { |
| 243 | // No domain can save us here. |
| 244 | return kTightCopy_DomainMode; |
| 245 | } |
| 246 | return kNoDomain_DomainMode; |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 247 | } |
| 248 | } else { |
| 249 | // bicubic does nearest filtering internally. |
| 250 | filterHalfWidth = 1.5f; |
| 251 | } |
| 252 | |
| 253 | // Both bilerp and bicubic use bilinear filtering and so need to be clamped to the center |
| 254 | // of the edge texel. Pinning to the texel center has no impact on nearest mode and MIP-maps |
| 255 | |
| 256 | static const SkScalar kDomainInset = 0.5f; |
| 257 | // Figure out the limits of pixels we're allowed to sample from. |
| 258 | // Unless we know the amount of outset and the texture matrix we have to conservatively enforce |
| 259 | // the domain. |
| 260 | if (restrictFilterToRect) { |
| 261 | domainRect->fLeft = constraintRect.fLeft + kDomainInset; |
| 262 | domainRect->fTop = constraintRect.fTop + kDomainInset; |
| 263 | domainRect->fRight = constraintRect.fRight - kDomainInset; |
| 264 | domainRect->fBottom = constraintRect.fBottom - kDomainInset; |
| 265 | } else if (textureContentArea) { |
| 266 | // If we got here then: there is a textureContentArea, the coords are limited to the |
| 267 | // constraint rect, and we're allowed to filter across the constraint rect boundary. So |
| 268 | // we check whether the filter would reach across the edge of the content area. |
| 269 | // We will only set the sides that are required. |
| 270 | |
| 271 | domainRect->setLargest(); |
| 272 | if (coordsLimitedToConstraintRect) { |
| 273 | // We may be able to use the fact that the texture coords are limited to the constraint |
| 274 | // rect in order to avoid having to add a domain. |
| 275 | bool needContentAreaConstraint = false; |
| 276 | if (textureContentArea->fLeft > 0 && |
| 277 | textureContentArea->fLeft + filterHalfWidth > constraintRect.fLeft) { |
| 278 | domainRect->fLeft = textureContentArea->fLeft + kDomainInset; |
| 279 | needContentAreaConstraint = true; |
| 280 | } |
| 281 | if (textureContentArea->fTop > 0 && |
| 282 | textureContentArea->fTop + filterHalfWidth > constraintRect.fTop) { |
| 283 | domainRect->fTop = textureContentArea->fTop + kDomainInset; |
| 284 | needContentAreaConstraint = true; |
| 285 | } |
| 286 | if (textureContentArea->fRight < texW && |
| 287 | textureContentArea->fRight - filterHalfWidth < constraintRect.fRight) { |
| 288 | domainRect->fRight = textureContentArea->fRight - kDomainInset; |
| 289 | needContentAreaConstraint = true; |
| 290 | } |
| 291 | if (textureContentArea->fBottom < texH && |
| 292 | textureContentArea->fBottom - filterHalfWidth < constraintRect.fBottom) { |
| 293 | domainRect->fBottom = textureContentArea->fBottom - kDomainInset; |
| 294 | needContentAreaConstraint = true; |
| 295 | } |
| 296 | if (!needContentAreaConstraint) { |
| 297 | return kNoDomain_DomainMode; |
| 298 | } |
| 299 | } else { |
| 300 | // Our sample coords for the texture are allowed to be outside the constraintRect so we |
| 301 | // don't consider it when computing the domain. |
| 302 | if (textureContentArea->fLeft != 0) { |
| 303 | domainRect->fLeft = textureContentArea->fLeft + kDomainInset; |
| 304 | } |
| 305 | if (textureContentArea->fTop != 0) { |
| 306 | domainRect->fTop = textureContentArea->fTop + kDomainInset; |
| 307 | } |
| 308 | if (textureContentArea->fRight != texW) { |
| 309 | domainRect->fRight = textureContentArea->fRight - kDomainInset; |
| 310 | } |
| 311 | if (textureContentArea->fBottom != texH) { |
| 312 | domainRect->fBottom = textureContentArea->fBottom - kDomainInset; |
| 313 | } |
| 314 | } |
| 315 | } else { |
| 316 | return kNoDomain_DomainMode; |
| 317 | } |
| 318 | |
| 319 | if (domainRect->fLeft > domainRect->fRight) { |
| 320 | domainRect->fLeft = domainRect->fRight = SkScalarAve(domainRect->fLeft, domainRect->fRight); |
| 321 | } |
| 322 | if (domainRect->fTop > domainRect->fBottom) { |
| 323 | domainRect->fTop = domainRect->fBottom = SkScalarAve(domainRect->fTop, domainRect->fBottom); |
| 324 | } |
| 325 | domainRect->fLeft /= texW; |
| 326 | domainRect->fTop /= texH; |
| 327 | domainRect->fRight /= texW; |
| 328 | domainRect->fBottom /= texH; |
| 329 | return kDomain_DomainMode; |
| 330 | } |
| 331 | |
bsalomon | b1b0199 | 2015-11-18 10:56:08 -0800 | [diff] [blame] | 332 | static const GrFragmentProcessor* create_fp_for_domain_and_filter( |
| 333 | GrTexture* texture, |
| 334 | const SkMatrix& textureMatrix, |
| 335 | DomainMode domainMode, |
| 336 | const SkRect& domain, |
| 337 | const GrTextureParams::FilterMode* filterOrNullForBicubic) { |
| 338 | SkASSERT(kTightCopy_DomainMode != domainMode); |
| 339 | if (filterOrNullForBicubic) { |
| 340 | if (kDomain_DomainMode == domainMode) { |
| 341 | return GrTextureDomainEffect::Create(texture, textureMatrix, domain, |
| 342 | GrTextureDomain::kClamp_Mode, |
| 343 | *filterOrNullForBicubic); |
| 344 | } else { |
| 345 | GrTextureParams params(SkShader::kClamp_TileMode, *filterOrNullForBicubic); |
| 346 | return GrSimpleTextureEffect::Create(texture, textureMatrix, params); |
| 347 | } |
| 348 | } else { |
| 349 | if (kDomain_DomainMode == domainMode) { |
| 350 | return GrBicubicEffect::Create(texture, textureMatrix, domain); |
| 351 | } else { |
| 352 | static const SkShader::TileMode kClampClamp[] = |
| 353 | { SkShader::kClamp_TileMode, SkShader::kClamp_TileMode }; |
| 354 | return GrBicubicEffect::Create(texture, textureMatrix, kClampClamp); |
| 355 | } |
| 356 | } |
| 357 | } |
| 358 | |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 359 | const GrFragmentProcessor* GrTextureAdjuster::createFragmentProcessor( |
bsalomon | 3aa5fce | 2015-11-12 09:59:44 -0800 | [diff] [blame] | 360 | const SkMatrix& origTextureMatrix, |
| 361 | const SkRect& origConstraintRect, |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 362 | FilterConstraint filterConstraint, |
| 363 | bool coordsLimitedToConstraintRect, |
| 364 | const GrTextureParams::FilterMode* filterOrNullForBicubic) { |
| 365 | |
bsalomon | 3aa5fce | 2015-11-12 09:59:44 -0800 | [diff] [blame] | 366 | SkMatrix textureMatrix = origTextureMatrix; |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 367 | const SkIRect* contentArea = this->contentAreaOrNull(); |
bsalomon | 3aa5fce | 2015-11-12 09:59:44 -0800 | [diff] [blame] | 368 | // Convert the constraintRect to be relative to the texture rather than the content area so |
| 369 | // that both rects are in the same coordinate system. |
| 370 | SkTCopyOnFirstWrite<SkRect> constraintRect(origConstraintRect); |
| 371 | if (contentArea) { |
| 372 | SkScalar l = SkIntToScalar(contentArea->fLeft); |
| 373 | SkScalar t = SkIntToScalar(contentArea->fTop); |
| 374 | constraintRect.writable()->offset(l, t); |
| 375 | textureMatrix.postTranslate(l, t); |
| 376 | } |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 377 | |
| 378 | SkRect domain; |
| 379 | GrTexture* texture = this->originalTexture(); |
| 380 | DomainMode domainMode = |
bsalomon | 3aa5fce | 2015-11-12 09:59:44 -0800 | [diff] [blame] | 381 | determine_domain_mode(*constraintRect, filterConstraint, coordsLimitedToConstraintRect, |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 382 | texture->width(), texture->height(), |
| 383 | contentArea, filterOrNullForBicubic, |
| 384 | &domain); |
| 385 | if (kTightCopy_DomainMode == domainMode) { |
| 386 | // TODO: Copy the texture and adjust the texture matrix (both parts need to consider |
| 387 | // non-int constraint rect) |
| 388 | // For now: treat as bilerp and ignore what goes on above level 0. |
| 389 | |
| 390 | // We only expect MIP maps to require a tight copy. |
| 391 | SkASSERT(filterOrNullForBicubic && |
| 392 | GrTextureParams::kMipMap_FilterMode == *filterOrNullForBicubic); |
| 393 | static const GrTextureParams::FilterMode kBilerp = GrTextureParams::kBilerp_FilterMode; |
| 394 | domainMode = |
bsalomon | 3aa5fce | 2015-11-12 09:59:44 -0800 | [diff] [blame] | 395 | determine_domain_mode(*constraintRect, filterConstraint, coordsLimitedToConstraintRect, |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 396 | texture->width(), texture->height(), |
| 397 | contentArea, &kBilerp, &domain); |
| 398 | SkASSERT(kTightCopy_DomainMode != domainMode); |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 399 | } |
| 400 | SkASSERT(kNoDomain_DomainMode == domainMode || |
| 401 | (domain.fLeft <= domain.fRight && domain.fTop <= domain.fBottom)); |
bsalomon | 3aa5fce | 2015-11-12 09:59:44 -0800 | [diff] [blame] | 402 | textureMatrix.postIDiv(texture->width(), texture->height()); |
bsalomon | b1b0199 | 2015-11-18 10:56:08 -0800 | [diff] [blame] | 403 | return create_fp_for_domain_and_filter(texture, textureMatrix, domainMode, domain, |
| 404 | filterOrNullForBicubic); |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 405 | } |
| 406 | |
bsalomon | 89fe56b | 2015-10-29 10:49:28 -0700 | [diff] [blame] | 407 | ////////////////////////////////////////////////////////////////////////////// |
| 408 | |
bsalomon | b1b0199 | 2015-11-18 10:56:08 -0800 | [diff] [blame] | 409 | GrTexture* GrTextureMaker::refTextureForParams(const GrTextureParams& params) { |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 410 | CopyParams copyParams; |
bsalomon | b1b0199 | 2015-11-18 10:56:08 -0800 | [diff] [blame] | 411 | if (!fContext->getGpu()->makeCopyForTextureParams(this->width(), this->height(), params, |
| 412 | ©Params)) { |
| 413 | return this->refOriginalTexture(); |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 414 | } |
| 415 | GrUniqueKey copyKey; |
| 416 | this->makeCopyKey(copyParams, ©Key); |
| 417 | if (copyKey.isValid()) { |
bsalomon | b1b0199 | 2015-11-18 10:56:08 -0800 | [diff] [blame] | 418 | GrTexture* result = fContext->textureProvider()->findAndRefTextureByUniqueKey(copyKey); |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 419 | if (result) { |
| 420 | return result; |
| 421 | } |
| 422 | } |
| 423 | |
bsalomon | b1b0199 | 2015-11-18 10:56:08 -0800 | [diff] [blame] | 424 | GrTexture* result = this->generateTextureForParams(copyParams); |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 425 | if (!result) { |
| 426 | return nullptr; |
| 427 | } |
| 428 | |
| 429 | if (copyKey.isValid()) { |
bsalomon | b1b0199 | 2015-11-18 10:56:08 -0800 | [diff] [blame] | 430 | fContext->textureProvider()->assignUniqueKeyToTexture(copyKey, result); |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 431 | this->didCacheCopy(copyKey); |
| 432 | } |
| 433 | return result; |
| 434 | } |
| 435 | |
bsalomon | b1b0199 | 2015-11-18 10:56:08 -0800 | [diff] [blame] | 436 | const GrFragmentProcessor* GrTextureMaker::createFragmentProcessor( |
| 437 | const SkMatrix& textureMatrix, |
| 438 | const SkRect& constraintRect, |
| 439 | FilterConstraint filterConstraint, |
| 440 | bool coordsLimitedToConstraintRect, |
| 441 | const GrTextureParams::FilterMode* filterOrNullForBicubic) { |
| 442 | |
| 443 | const GrTextureParams::FilterMode* fmForDetermineDomain = filterOrNullForBicubic; |
| 444 | if (filterOrNullForBicubic && GrTextureParams::kMipMap_FilterMode == *filterOrNullForBicubic && |
| 445 | kYes_FilterConstraint == filterConstraint) { |
| 446 | // TODo: Here we should force a copy restricted to the constraintRect since MIP maps will |
| 447 | // read outside the constraint rect. However, as in the adjuster case, we aren't currently |
| 448 | // doing that. |
| 449 | // We instead we compute the domain as though were bilerping which is only correct if we |
| 450 | // only sample level 0. |
| 451 | static const GrTextureParams::FilterMode kBilerp = GrTextureParams::kBilerp_FilterMode; |
| 452 | fmForDetermineDomain = &kBilerp; |
| 453 | } |
| 454 | |
| 455 | GrTextureParams params; |
| 456 | if (filterOrNullForBicubic) { |
| 457 | params.reset(SkShader::kClamp_TileMode, *filterOrNullForBicubic); |
| 458 | } else { |
| 459 | // Bicubic doesn't use filtering for it's texture accesses. |
| 460 | params.reset(SkShader::kClamp_TileMode, GrTextureParams::kNone_FilterMode); |
| 461 | } |
| 462 | SkAutoTUnref<GrTexture> texture(this->refTextureForParams(params)); |
| 463 | if (!texture) { |
| 464 | return nullptr; |
| 465 | } |
| 466 | SkRect domain; |
| 467 | DomainMode domainMode = |
| 468 | determine_domain_mode(constraintRect, filterConstraint, coordsLimitedToConstraintRect, |
| 469 | texture->width(), texture->height(), nullptr, fmForDetermineDomain, |
| 470 | &domain); |
| 471 | SkASSERT(kTightCopy_DomainMode != domainMode); |
| 472 | SkMatrix normalizedTextureMatrix = textureMatrix; |
| 473 | normalizedTextureMatrix.postIDiv(texture->width(), texture->height()); |
| 474 | return create_fp_for_domain_and_filter(texture, normalizedTextureMatrix, domainMode, domain, |
| 475 | filterOrNullForBicubic); |
| 476 | } |
| 477 | |
| 478 | GrTexture* GrTextureMaker::generateTextureForParams(const CopyParams& copyParams) { |
| 479 | SkAutoTUnref<GrTexture> original(this->refOriginalTexture()); |
bsalomon | 100b8f8 | 2015-10-28 08:37:44 -0700 | [diff] [blame] | 480 | if (!original) { |
| 481 | return nullptr; |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 482 | } |
bsalomon | 89fe56b | 2015-10-29 10:49:28 -0700 | [diff] [blame] | 483 | return copy_on_gpu(original, nullptr, copyParams); |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 484 | } |