blob: 11bb20eb94a036a6cd34c67a8a23ee1cbb5f8b2b [file] [log] [blame]
Brian Osmane8e54582016-11-28 10:06:27 -05001/*
2 * Copyright 2016 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
Robert Phillipsb7bfbc22020-07-01 12:55:01 -04008#include "include/gpu/GrRecordingContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -05009#include "src/gpu/GrColorSpaceXform.h"
10#include "src/gpu/GrGpu.h"
11#include "src/gpu/GrProxyProvider.h"
12#include "src/gpu/GrRecordingContextPriv.h"
13#include "src/gpu/GrTextureAdjuster.h"
14#include "src/gpu/SkGr.h"
Brian Osmane8e54582016-11-28 10:06:27 -050015
Brian Salomone7499c72019-06-24 12:12:36 -040016GrTextureAdjuster::GrTextureAdjuster(GrRecordingContext* context,
Greg Danielcc104db2020-02-03 14:17:08 -050017 GrSurfaceProxyView original,
Greg Daniela4828a12019-10-11 13:51:02 -040018 const GrColorInfo& colorInfo,
Brian Salomon777e1462020-02-28 21:10:31 -050019 uint32_t uniqueID)
20 : INHERITED(context, {colorInfo, original.proxy()->dimensions()})
Greg Daniela4828a12019-10-11 13:51:02 -040021 , fOriginal(std::move(original))
22 , fUniqueID(uniqueID) {}
23
Brian Salomonc8d092a2020-02-24 10:14:21 -050024GrSurfaceProxyView GrTextureAdjuster::makeMippedCopy() {
Robert Phillips9338c602019-02-19 12:52:29 -050025 GrProxyProvider* proxyProvider = this->context()->priv().proxyProvider();
Robert Phillips1afd4cd2018-01-08 13:40:32 -050026
Brian Salomonb62cee32020-02-28 13:34:16 -050027 GrUniqueKey baseKey, mipMappedKey;
28 GrMakeKeyFromImageID(&baseKey, fUniqueID, SkIRect::MakeSize(this->dimensions()));
29 if (baseKey.isValid()) {
30 static const GrUniqueKey::Domain kMipMappedDomain = GrUniqueKey::GenerateDomain();
31 GrUniqueKey::Builder builder(&mipMappedKey, baseKey, kMipMappedDomain, 0);
32 }
Greg Daniel09c94002018-06-08 22:11:51 +000033 sk_sp<GrTextureProxy> cachedCopy;
Brian Salomonb62cee32020-02-28 13:34:16 -050034 if (mipMappedKey.isValid()) {
Brian Salomondf1bd6d2020-03-26 20:37:01 -040035 cachedCopy = proxyProvider->findOrCreateProxyByUniqueKey(mipMappedKey);
Brian Salomonc8d092a2020-02-24 10:14:21 -050036 if (cachedCopy) {
Brian Salomonb62cee32020-02-28 13:34:16 -050037 return {std::move(cachedCopy), fOriginal.origin(), fOriginal.swizzle()};
Robert Phillips0c984a02017-03-16 07:51:56 -040038 }
39 }
40
Brian Salomonc5243782020-04-02 12:50:34 -040041 auto copy = GrCopyBaseMipMapToView(this->context(), fOriginal);
42 if (!copy) {
Brian Salomonc8d092a2020-02-24 10:14:21 -050043 return {};
Greg Danielcce65002020-01-23 11:16:49 -050044 }
Brian Salomonb62cee32020-02-28 13:34:16 -050045 if (mipMappedKey.isValid()) {
Brian Salomonb62cee32020-02-28 13:34:16 -050046 // TODO: If we move listeners up from SkImage_Lazy to SkImage_Base then add one here.
Brian Salomonc5243782020-04-02 12:50:34 -040047 proxyProvider->assignUniqueKeyToProxy(mipMappedKey, copy.asTextureProxy());
Brian Osmane8e54582016-11-28 10:06:27 -050048 }
Brian Salomonc5243782020-04-02 12:50:34 -040049 return copy;
Brian Osmane8e54582016-11-28 10:06:27 -050050}
51
Brian Salomonecbb0fb2020-02-28 18:07:32 -050052GrSurfaceProxyView GrTextureAdjuster::onView(GrMipMapped mipMapped) {
Robert Phillips9eb00022020-06-30 15:30:12 -040053 if (this->context()->abandoned()) {
Brian Osmane8e54582016-11-28 10:06:27 -050054 // The texture was abandoned.
Greg Danielcc104db2020-02-03 14:17:08 -050055 return {};
Brian Osmane8e54582016-11-28 10:06:27 -050056 }
Brian Salomon4df00922017-09-07 16:34:11 +000057
Robert Phillips9338c602019-02-19 12:52:29 -050058 SkASSERT(this->width() <= this->context()->priv().caps()->maxTextureSize() &&
59 this->height() <= this->context()->priv().caps()->maxTextureSize());
Brian Osman875f7852018-04-12 13:29:08 -040060
Brian Salomonb62cee32020-02-28 13:34:16 -050061 GrTextureProxy* texProxy = fOriginal.asTextureProxy();
Greg Danielcc104db2020-02-03 14:17:08 -050062 SkASSERT(texProxy);
Brian Salomonecbb0fb2020-02-28 18:07:32 -050063 if (mipMapped == GrMipMapped::kNo || texProxy->mipMapped() == GrMipMapped::kYes) {
Brian Salomonb62cee32020-02-28 13:34:16 -050064 return fOriginal;
Brian Osmane8e54582016-11-28 10:06:27 -050065 }
66
Brian Salomonc8d092a2020-02-24 10:14:21 -050067 GrSurfaceProxyView copy = this->makeMippedCopy();
68 if (!copy) {
Greg Daniel8f5bbda2018-06-08 17:22:23 -040069 // If we were unable to make a copy and we only needed a copy for mips, then we will return
70 // the source texture here and require that the GPU backend is able to fall back to using
Brian Salomona3b02f52020-07-15 16:02:01 -040071 // linear filtering if mips are required.
Brian Salomonb62cee32020-02-28 13:34:16 -050072 return fOriginal;
Greg Daniel8f5bbda2018-06-08 17:22:23 -040073 }
Brian Salomonc8d092a2020-02-24 10:14:21 -050074 SkASSERT(copy.asTextureProxy());
75 return copy;
Brian Osmane8e54582016-11-28 10:06:27 -050076}
77
Brian Salomonaff329b2017-08-11 09:40:37 -040078std::unique_ptr<GrFragmentProcessor> GrTextureAdjuster::createFragmentProcessor(
Brian Salomonc8d092a2020-02-24 10:14:21 -050079 const SkMatrix& textureMatrix,
Brian Salomon0ea33072020-07-14 10:43:42 -040080 const SkRect* subset,
81 const SkRect* domain,
82 GrSamplerState samplerState) {
83 return this->createFragmentProcessorForView(
84 this->view(samplerState.filter()), textureMatrix, subset, domain, samplerState);
85}
Brian Osmane8e54582016-11-28 10:06:27 -050086
Brian Salomon0ea33072020-07-14 10:43:42 -040087std::unique_ptr<GrFragmentProcessor> GrTextureAdjuster::createBicubicFragmentProcessor(
88 const SkMatrix& textureMatrix,
89 const SkRect* subset,
90 const SkRect* domain,
91 GrSamplerState::WrapMode wrapX,
92 GrSamplerState::WrapMode wrapY) {
93 return this->createBicubicFragmentProcessorForView(
94 this->view(GrMipMapped::kNo), textureMatrix, subset, domain, wrapX, wrapY);
Brian Osmane8e54582016-11-28 10:06:27 -050095}