| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "GrTextureParamsAdjuster.h" | 8 #include "GrTextureParamsAdjuster.h" |
| 9 | 9 |
| 10 #include "GrCaps.h" | 10 #include "GrCaps.h" |
| (...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 126 bool isAlphaOnly) | 126 bool isAlphaOnly) |
| 127 : INHERITED(contentArea.width(), contentArea.height(), isAlphaOnly) | 127 : INHERITED(contentArea.width(), contentArea.height(), isAlphaOnly) |
| 128 , fOriginal(original) { | 128 , fOriginal(original) { |
| 129 SkASSERT(SkIRect::MakeWH(original->width(), original->height()).contains(con
tentArea)); | 129 SkASSERT(SkIRect::MakeWH(original->width(), original->height()).contains(con
tentArea)); |
| 130 if (contentArea.fLeft > 0 || contentArea.fTop > 0 || | 130 if (contentArea.fLeft > 0 || contentArea.fTop > 0 || |
| 131 contentArea.fRight < original->width() || contentArea.fBottom < original
->height()) { | 131 contentArea.fRight < original->width() || contentArea.fBottom < original
->height()) { |
| 132 fContentArea.set(contentArea); | 132 fContentArea.set(contentArea); |
| 133 } | 133 } |
| 134 } | 134 } |
| 135 | 135 |
| 136 GrTexture* GrTextureAdjuster::refCopy(const CopyParams& copyParams) { |
| 137 GrTexture* texture = this->originalTexture(); |
| 138 GrContext* context = texture->getContext(); |
| 139 const SkIRect* contentArea = this->contentAreaOrNull(); |
| 140 GrUniqueKey key; |
| 141 this->makeCopyKey(copyParams, &key); |
| 142 if (key.isValid()) { |
| 143 GrTexture* cachedCopy = context->textureProvider()->findAndRefTextureByU
niqueKey(key); |
| 144 if (cachedCopy) { |
| 145 return cachedCopy; |
| 146 } |
| 147 } |
| 148 GrTexture* copy = copy_on_gpu(texture, contentArea, copyParams); |
| 149 if (copy) { |
| 150 if (key.isValid()) { |
| 151 copy->resourcePriv().setUniqueKey(key); |
| 152 this->didCacheCopy(key); |
| 153 } |
| 154 } |
| 155 return copy; |
| 156 } |
| 157 |
| 136 GrTexture* GrTextureAdjuster::refTextureSafeForParams(const GrTextureParams& par
ams, | 158 GrTexture* GrTextureAdjuster::refTextureSafeForParams(const GrTextureParams& par
ams, |
| 137 SkIPoint* outOffset) { | 159 SkIPoint* outOffset) { |
| 138 GrTexture* texture = this->originalTexture(); | 160 GrTexture* texture = this->originalTexture(); |
| 139 GrContext* context = texture->getContext(); | 161 GrContext* context = texture->getContext(); |
| 140 CopyParams copyParams; | 162 CopyParams copyParams; |
| 141 const SkIRect* contentArea = this->contentAreaOrNull(); | 163 const SkIRect* contentArea = this->contentAreaOrNull(); |
| 142 | 164 |
| 143 if (contentArea && GrTextureParams::kMipMap_FilterMode == params.filterMode(
)) { | 165 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 | 166 // If we generate a MIP chain for texture it will read pixel values from
outside the content |
| 145 // area. | 167 // area. |
| 146 copyParams.fWidth = contentArea->width(); | 168 copyParams.fWidth = contentArea->width(); |
| 147 copyParams.fHeight = contentArea->height(); | 169 copyParams.fHeight = contentArea->height(); |
| 148 copyParams.fFilter = GrTextureParams::kBilerp_FilterMode; | 170 copyParams.fFilter = GrTextureParams::kBilerp_FilterMode; |
| 149 } else if (!context->getGpu()->makeCopyForTextureParams(texture->width(), te
xture->height(), | 171 } else if (!context->getGpu()->makeCopyForTextureParams(texture, params, &co
pyParams)) { |
| 150 params, ©Params)
) { | |
| 151 if (outOffset) { | 172 if (outOffset) { |
| 152 if (contentArea) { | 173 if (contentArea) { |
| 153 outOffset->set(contentArea->fLeft, contentArea->fRight); | 174 outOffset->set(contentArea->fLeft, contentArea->fRight); |
| 154 } else { | 175 } else { |
| 155 outOffset->set(0, 0); | 176 outOffset->set(0, 0); |
| 156 } | 177 } |
| 157 } | 178 } |
| 158 return SkRef(texture); | 179 return SkRef(texture); |
| 159 } | 180 } |
| 160 GrUniqueKey key; | 181 |
| 161 this->makeCopyKey(copyParams, &key); | 182 GrTexture* copy = this->refCopy(copyParams); |
| 162 if (key.isValid()) { | 183 if (copy && outOffset) { |
| 163 GrTexture* result = context->textureProvider()->findAndRefTextureByUniqu
eKey(key); | 184 outOffset->set(0, 0); |
| 164 if (result) { | |
| 165 return result; | |
| 166 } | |
| 167 } | 185 } |
| 168 GrTexture* result = copy_on_gpu(texture, contentArea, copyParams); | 186 return copy; |
| 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 } | 187 } |
| 180 | 188 |
| 181 enum DomainMode { | 189 enum DomainMode { |
| 182 kNoDomain_DomainMode, | 190 kNoDomain_DomainMode, |
| 183 kDomain_DomainMode, | 191 kDomain_DomainMode, |
| 184 kTightCopy_DomainMode | 192 kTightCopy_DomainMode |
| 185 }; | 193 }; |
| 186 | 194 |
| 187 /** Determines whether a texture domain is necessary and if so what domain to us
e. There are two | 195 /** Determines whether a texture domain is necessary and if so what domain to us
e. There are two |
| 188 * rectangles to consider: | 196 * rectangles to consider: |
| (...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 345 *filterOrNullForBicubic); | 353 *filterOrNullForBicubic); |
| 346 } else { | 354 } else { |
| 347 GrTextureParams params(SkShader::kClamp_TileMode, *filterOrNullForBi
cubic); | 355 GrTextureParams params(SkShader::kClamp_TileMode, *filterOrNullForBi
cubic); |
| 348 return GrSimpleTextureEffect::Create(texture, textureMatrix, params)
; | 356 return GrSimpleTextureEffect::Create(texture, textureMatrix, params)
; |
| 349 } | 357 } |
| 350 } else { | 358 } else { |
| 351 if (kDomain_DomainMode == domainMode) { | 359 if (kDomain_DomainMode == domainMode) { |
| 352 return GrBicubicEffect::Create(texture, textureMatrix, domain); | 360 return GrBicubicEffect::Create(texture, textureMatrix, domain); |
| 353 } else { | 361 } else { |
| 354 static const SkShader::TileMode kClampClamp[] = | 362 static const SkShader::TileMode kClampClamp[] = |
| 355 { SkShader::kClamp_TileMode, SkShader::kClamp_TileMode }; | 363 { SkShader::kClamp_TileMode, SkShader::kClamp_TileMode }; |
| 356 return GrBicubicEffect::Create(texture, textureMatrix, kClampClamp); | 364 return GrBicubicEffect::Create(texture, textureMatrix, kClampClamp); |
| 357 } | 365 } |
| 358 } | 366 } |
| 359 } | 367 } |
| 360 | 368 |
| 361 const GrFragmentProcessor* GrTextureAdjuster::createFragmentProcessor( | 369 const GrFragmentProcessor* GrTextureAdjuster::createFragmentProcessor( |
| 362 const SkMatrix& origTextureMatrix, | 370 const SkMatrix& origTextureMatrix, |
| 363 const SkRect& origConstraintRect, | 371 const SkRect& origConstraintRect, |
| 364 FilterConstraint filterConstraint, | 372 FilterConstraint filterConstraint, |
| 365 bool coordsLimitedToConstraintRect, | 373 bool coordsLimitedToConstraintRect, |
| 366 const GrTextureParams::FilterMode* filte
rOrNullForBicubic) { | 374 const GrTextureParams::FilterMode* filte
rOrNullForBicubic) { |
| 367 | 375 |
| 368 SkMatrix textureMatrix = origTextureMatrix; | 376 SkMatrix textureMatrix = origTextureMatrix; |
| 369 const SkIRect* contentArea = this->contentAreaOrNull(); | 377 const SkIRect* contentArea = this->contentAreaOrNull(); |
| 370 // Convert the constraintRect to be relative to the texture rather than the
content area so | 378 // 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. | 379 // that both rects are in the same coordinate system. |
| 372 SkTCopyOnFirstWrite<SkRect> constraintRect(origConstraintRect); | 380 SkTCopyOnFirstWrite<SkRect> constraintRect(origConstraintRect); |
| 373 if (contentArea) { | 381 if (contentArea) { |
| 374 SkScalar l = SkIntToScalar(contentArea->fLeft); | 382 SkScalar l = SkIntToScalar(contentArea->fLeft); |
| 375 SkScalar t = SkIntToScalar(contentArea->fTop); | 383 SkScalar t = SkIntToScalar(contentArea->fTop); |
| 376 constraintRect.writable()->offset(l, t); | 384 constraintRect.writable()->offset(l, t); |
| 377 textureMatrix.postTranslate(l, t); | 385 textureMatrix.postTranslate(l, t); |
| 378 } | 386 } |
| 379 | 387 |
| 380 SkRect domain; | 388 SkRect domain; |
| 381 GrTexture* texture = this->originalTexture(); | 389 GrTextureParams params; |
| 390 if (filterOrNullForBicubic) { |
| 391 params.setFilterMode(*filterOrNullForBicubic); |
| 392 } |
| 393 SkAutoTUnref<GrTexture> texture(this->refTextureSafeForParams(params, nullpt
r)); |
| 394 if (!texture) { |
| 395 return nullptr; |
| 396 } |
| 397 // If we made a copy then we only copied the contentArea, in which case the
new texture is all |
| 398 // content. |
| 399 if (texture != this->originalTexture()) { |
| 400 contentArea = nullptr; |
| 401 } |
| 402 |
| 382 DomainMode domainMode = | 403 DomainMode domainMode = |
| 383 determine_domain_mode(*constraintRect, filterConstraint, coordsLimitedTo
ConstraintRect, | 404 determine_domain_mode(*constraintRect, filterConstraint, coordsLimitedTo
ConstraintRect, |
| 384 texture->width(), texture->height(), | 405 texture->width(), texture->height(), |
| 385 contentArea, filterOrNullForBicubic, | 406 contentArea, filterOrNullForBicubic, |
| 386 &domain); | 407 &domain); |
| 387 if (kTightCopy_DomainMode == domainMode) { | 408 if (kTightCopy_DomainMode == domainMode) { |
| 388 // TODO: Copy the texture and adjust the texture matrix (both parts need
to consider | 409 // TODO: Copy the texture and adjust the texture matrix (both parts need
to consider |
| 389 // non-int constraint rect) | 410 // non-int constraint rect) |
| 390 // For now: treat as bilerp and ignore what goes on above level 0. | 411 // For now: treat as bilerp and ignore what goes on above level 0. |
| 391 | 412 |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 477 filterOrNullForBicubic); | 498 filterOrNullForBicubic); |
| 478 } | 499 } |
| 479 | 500 |
| 480 GrTexture* GrTextureMaker::generateTextureForParams(const CopyParams& copyParams
) { | 501 GrTexture* GrTextureMaker::generateTextureForParams(const CopyParams& copyParams
) { |
| 481 SkAutoTUnref<GrTexture> original(this->refOriginalTexture()); | 502 SkAutoTUnref<GrTexture> original(this->refOriginalTexture()); |
| 482 if (!original) { | 503 if (!original) { |
| 483 return nullptr; | 504 return nullptr; |
| 484 } | 505 } |
| 485 return copy_on_gpu(original, nullptr, copyParams); | 506 return copy_on_gpu(original, nullptr, copyParams); |
| 486 } | 507 } |
| OLD | NEW |