Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 1 | /* |
| 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 | |
| 8 | #ifndef GrTextureProducer_DEFINED |
| 9 | #define GrTextureProducer_DEFINED |
| 10 | |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 11 | #include "GrResourceKey.h" |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 12 | #include "GrSamplerState.h" |
| 13 | #include "SkImageInfo.h" |
Ben Wagner | d5148e3 | 2018-07-16 17:44:06 -0400 | [diff] [blame] | 14 | #include "SkNoncopyable.h" |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 15 | |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 16 | class GrContext; |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 17 | class GrFragmentProcessor; |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 18 | class GrTexture; |
Robert Phillips | b66b42f | 2017-03-14 08:53:02 -0400 | [diff] [blame] | 19 | class GrTextureProxy; |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 20 | class SkColorSpace; |
| 21 | class SkMatrix; |
| 22 | struct SkRect; |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 23 | |
| 24 | /** |
| 25 | * Different GPUs and API extensions have different requirements with respect to what texture |
| 26 | * sampling parameters may be used with textures of various types. This class facilitates making |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 27 | * texture compatible with a given GrSamplerState. There are two immediate subclasses defined |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 28 | * below. One is a base class for sources that are inherently texture-backed (e.g. a texture-backed |
| 29 | * SkImage). It supports subsetting the original texture. The other is for use cases where the |
| 30 | * source can generate a texture that represents some content (e.g. cpu pixels, SkPicture, ...). |
| 31 | */ |
| 32 | class GrTextureProducer : public SkNoncopyable { |
| 33 | public: |
| 34 | struct CopyParams { |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 35 | GrSamplerState::Filter fFilter; |
| 36 | int fWidth; |
| 37 | int fHeight; |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 38 | }; |
| 39 | |
| 40 | enum FilterConstraint { |
| 41 | kYes_FilterConstraint, |
| 42 | kNo_FilterConstraint, |
| 43 | }; |
| 44 | |
| 45 | /** |
| 46 | * Helper for creating a fragment processor to sample the texture with a given filtering mode. |
| 47 | * It attempts to avoid making texture copies or using domains whenever possible. |
| 48 | * |
| 49 | * @param textureMatrix Matrix used to access the texture. It is applied to |
| 50 | * the local coords. The post-transformed coords should |
| 51 | * be in texel units (rather than normalized) with |
| 52 | * respect to this Producer's bounds (width()/height()). |
| 53 | * @param constraintRect A rect that represents the area of the texture to be |
| 54 | * sampled. It must be contained in the Producer's |
| 55 | * bounds as defined by width()/height(). |
| 56 | * @param filterConstriant Indicates whether filtering is limited to |
| 57 | * constraintRect. |
| 58 | * @param coordsLimitedToConstraintRect Is it known that textureMatrix*localCoords is bound |
| 59 | * by the portion of the texture indicated by |
| 60 | * constraintRect (without consideration of filter |
| 61 | * width, just the raw coords). |
| 62 | * @param filterOrNullForBicubic If non-null indicates the filter mode. If null means |
| 63 | * use bicubic filtering. |
| 64 | **/ |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 65 | virtual std::unique_ptr<GrFragmentProcessor> createFragmentProcessor( |
| 66 | const SkMatrix& textureMatrix, |
| 67 | const SkRect& constraintRect, |
| 68 | FilterConstraint filterConstraint, |
| 69 | bool coordsLimitedToConstraintRect, |
Brian Osman | 05c8f46 | 2018-10-22 17:13:36 -0400 | [diff] [blame] | 70 | const GrSamplerState::Filter* filterOrNullForBicubic) = 0; |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 71 | |
Brian Salomon | 2a943df | 2018-05-04 13:43:19 -0400 | [diff] [blame] | 72 | /** |
| 73 | * Returns a texture that is safe for use with the params. |
| 74 | * |
| 75 | * If the size of the returned texture does not match width()/height() then the contents of the |
| 76 | * original may have been scaled to fit the texture or the original may have been copied into |
| 77 | * a subrect of the copy. 'scaleAdjust' must be applied to the normalized texture coordinates |
| 78 | * in order to correct for the latter case. |
| 79 | * |
| 80 | * If the GrSamplerState is known to clamp and use kNearest or kBilerp filter mode then the |
| 81 | * proxy will always be unscaled and nullptr can be passed for scaleAdjust. There is a weird |
| 82 | * contract that if scaleAdjust is not null it must be initialized to {1, 1} before calling |
| 83 | * this method. (TODO: Fix this and make this function always initialize scaleAdjust). |
Brian Salomon | 2a943df | 2018-05-04 13:43:19 -0400 | [diff] [blame] | 84 | */ |
| 85 | sk_sp<GrTextureProxy> refTextureProxyForParams(const GrSamplerState&, |
Brian Salomon | 2a943df | 2018-05-04 13:43:19 -0400 | [diff] [blame] | 86 | SkScalar scaleAdjust[2]); |
| 87 | |
| 88 | sk_sp<GrTextureProxy> refTextureProxyForParams(GrSamplerState::Filter filter, |
Brian Salomon | 2a943df | 2018-05-04 13:43:19 -0400 | [diff] [blame] | 89 | SkScalar scaleAdjust[2]) { |
| 90 | return this->refTextureProxyForParams( |
Brian Osman | 6064e1c | 2018-10-19 14:27:54 -0400 | [diff] [blame] | 91 | GrSamplerState(GrSamplerState::WrapMode::kClamp, filter), scaleAdjust); |
Brian Salomon | 2a943df | 2018-05-04 13:43:19 -0400 | [diff] [blame] | 92 | } |
| 93 | |
Greg Daniel | 5f4b09d | 2018-06-12 16:39:59 -0400 | [diff] [blame] | 94 | /** |
Brian Osman | e7fd8c3 | 2018-10-19 13:30:39 -0400 | [diff] [blame] | 95 | * Returns a texture. If willNeedMips is true then the returned texture is guaranteed to have |
| 96 | * allocated mip map levels. This can be a performance win if future draws with the texture |
| 97 | * require mip maps. |
Greg Daniel | 5f4b09d | 2018-06-12 16:39:59 -0400 | [diff] [blame] | 98 | */ |
| 99 | // TODO: Once we remove support for npot textures, we should add a flag for must support repeat |
| 100 | // wrap mode. To support that flag now would require us to support scaleAdjust array like in |
| 101 | // refTextureProxyForParams, however the current public API that uses this call does not expose |
| 102 | // that array. |
Brian Osman | 6064e1c | 2018-10-19 14:27:54 -0400 | [diff] [blame] | 103 | sk_sp<GrTextureProxy> refTextureProxy(GrMipMapped willNeedMips); |
Greg Daniel | 5f4b09d | 2018-06-12 16:39:59 -0400 | [diff] [blame] | 104 | |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 105 | virtual ~GrTextureProducer() {} |
| 106 | |
| 107 | int width() const { return fWidth; } |
| 108 | int height() const { return fHeight; } |
| 109 | bool isAlphaOnly() const { return fIsAlphaOnly; } |
| 110 | virtual SkAlphaType alphaType() const = 0; |
Brian Osman | 6064e1c | 2018-10-19 14:27:54 -0400 | [diff] [blame] | 111 | virtual SkColorSpace* colorSpace() const = 0; |
Jim Van Verth | 3e4c2f3 | 2019-01-11 13:32:45 -0500 | [diff] [blame] | 112 | virtual SkColorSpace* targetColorSpace() const { return nullptr; } |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 113 | |
| 114 | protected: |
Robert Phillips | 51e7ca3 | 2017-03-27 10:14:08 -0400 | [diff] [blame] | 115 | friend class GrTextureProducer_TestAccess; |
| 116 | |
Greg Daniel | 8e9b4c4 | 2018-07-20 10:30:48 -0400 | [diff] [blame] | 117 | GrTextureProducer(GrContext* context, int width, int height, bool isAlphaOnly) |
| 118 | : fContext(context) |
| 119 | , fWidth(width) |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 120 | , fHeight(height) |
| 121 | , fIsAlphaOnly(isAlphaOnly) {} |
| 122 | |
| 123 | /** Helper for creating a key for a copy from an original key. */ |
| 124 | static void MakeCopyKeyFromOrigKey(const GrUniqueKey& origKey, |
| 125 | const CopyParams& copyParams, |
| 126 | GrUniqueKey* copyKey) { |
| 127 | SkASSERT(!copyKey->isValid()); |
| 128 | if (origKey.isValid()) { |
| 129 | static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain(); |
| 130 | GrUniqueKey::Builder builder(copyKey, origKey, kDomain, 3); |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 131 | builder[0] = static_cast<uint32_t>(copyParams.fFilter); |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 132 | builder[1] = copyParams.fWidth; |
| 133 | builder[2] = copyParams.fHeight; |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * If we need to make a copy in order to be compatible with GrTextureParams producer is asked to |
| 139 | * return a key that identifies its original content + the CopyParms parameter. If the producer |
| 140 | * does not want to cache the stretched version (e.g. the producer is volatile), this should |
| 141 | * simply return without initializing the copyKey. If the texture generated by this producer |
Brian Osman | 61624f0 | 2016-12-09 14:51:59 -0500 | [diff] [blame] | 142 | * depends on the destination color space, then that information should also be incorporated |
| 143 | * in the key. |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 144 | */ |
Brian Osman | b3f3830 | 2018-09-07 15:24:44 -0400 | [diff] [blame] | 145 | virtual void makeCopyKey(const CopyParams&, GrUniqueKey* copyKey) = 0; |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 146 | |
| 147 | /** |
| 148 | * If a stretched version of the texture is generated, it may be cached (assuming that |
| 149 | * makeCopyKey() returns true). In that case, the maker is notified in case it |
| 150 | * wants to note that for when the maker is destroyed. |
| 151 | */ |
Brian Salomon | 238069b | 2018-07-11 15:58:57 -0400 | [diff] [blame] | 152 | virtual void didCacheCopy(const GrUniqueKey& copyKey, uint32_t contextUniqueID) = 0; |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 153 | |
| 154 | enum DomainMode { |
| 155 | kNoDomain_DomainMode, |
| 156 | kDomain_DomainMode, |
| 157 | kTightCopy_DomainMode |
| 158 | }; |
| 159 | |
Robert Phillips | b66b42f | 2017-03-14 08:53:02 -0400 | [diff] [blame] | 160 | static sk_sp<GrTextureProxy> CopyOnGpu(GrContext*, sk_sp<GrTextureProxy> inputProxy, |
Greg Daniel | c77085d | 2017-11-01 16:38:48 -0400 | [diff] [blame] | 161 | const CopyParams& copyParams, |
Greg Daniel | e1da1d9 | 2017-10-06 15:59:27 -0400 | [diff] [blame] | 162 | bool dstWillRequireMipMaps); |
Robert Phillips | b66b42f | 2017-03-14 08:53:02 -0400 | [diff] [blame] | 163 | |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 164 | static DomainMode DetermineDomainMode(const SkRect& constraintRect, |
| 165 | FilterConstraint filterConstraint, |
| 166 | bool coordsLimitedToConstraintRect, |
| 167 | GrTextureProxy*, |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 168 | const GrSamplerState::Filter* filterModeOrNullForBicubic, |
| 169 | SkRect* domainRect); |
Robert Phillips | 51e7ca3 | 2017-03-27 10:14:08 -0400 | [diff] [blame] | 170 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 171 | static std::unique_ptr<GrFragmentProcessor> CreateFragmentProcessorForDomainAndFilter( |
| 172 | sk_sp<GrTextureProxy> proxy, |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 173 | const SkMatrix& textureMatrix, |
| 174 | DomainMode, |
| 175 | const SkRect& domain, |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 176 | const GrSamplerState::Filter* filterOrNullForBicubic); |
Robert Phillips | b66b42f | 2017-03-14 08:53:02 -0400 | [diff] [blame] | 177 | |
Greg Daniel | 8e9b4c4 | 2018-07-20 10:30:48 -0400 | [diff] [blame] | 178 | GrContext* fContext; |
| 179 | |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 180 | private: |
Brian Salomon | 2a943df | 2018-05-04 13:43:19 -0400 | [diff] [blame] | 181 | virtual sk_sp<GrTextureProxy> onRefTextureProxyForParams(const GrSamplerState&, |
Greg Daniel | 8e9b4c4 | 2018-07-20 10:30:48 -0400 | [diff] [blame] | 182 | bool willBeMipped, |
Brian Salomon | 2a943df | 2018-05-04 13:43:19 -0400 | [diff] [blame] | 183 | SkScalar scaleAdjust[2]) = 0; |
| 184 | |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 185 | const int fWidth; |
| 186 | const int fHeight; |
| 187 | const bool fIsAlphaOnly; |
| 188 | |
| 189 | typedef SkNoncopyable INHERITED; |
| 190 | }; |
| 191 | |
| 192 | #endif |