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" |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 14 | |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame^] | 15 | class GrContext; |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 16 | class GrColorSpaceXform; |
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 Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame^] | 70 | const GrSamplerState::Filter* filterOrNullForBicubic, |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 71 | SkColorSpace* dstColorSpace) = 0; |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 72 | |
| 73 | virtual ~GrTextureProducer() {} |
| 74 | |
| 75 | int width() const { return fWidth; } |
| 76 | int height() const { return fHeight; } |
| 77 | bool isAlphaOnly() const { return fIsAlphaOnly; } |
| 78 | virtual SkAlphaType alphaType() const = 0; |
| 79 | |
| 80 | protected: |
Robert Phillips | 51e7ca3 | 2017-03-27 10:14:08 -0400 | [diff] [blame] | 81 | friend class GrTextureProducer_TestAccess; |
| 82 | |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 83 | GrTextureProducer(int width, int height, bool isAlphaOnly) |
| 84 | : fWidth(width) |
| 85 | , fHeight(height) |
| 86 | , fIsAlphaOnly(isAlphaOnly) {} |
| 87 | |
| 88 | /** Helper for creating a key for a copy from an original key. */ |
| 89 | static void MakeCopyKeyFromOrigKey(const GrUniqueKey& origKey, |
| 90 | const CopyParams& copyParams, |
| 91 | GrUniqueKey* copyKey) { |
| 92 | SkASSERT(!copyKey->isValid()); |
| 93 | if (origKey.isValid()) { |
| 94 | static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain(); |
| 95 | GrUniqueKey::Builder builder(copyKey, origKey, kDomain, 3); |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame^] | 96 | builder[0] = static_cast<uint32_t>(copyParams.fFilter); |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 97 | builder[1] = copyParams.fWidth; |
| 98 | builder[2] = copyParams.fHeight; |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * If we need to make a copy in order to be compatible with GrTextureParams producer is asked to |
| 104 | * return a key that identifies its original content + the CopyParms parameter. If the producer |
| 105 | * does not want to cache the stretched version (e.g. the producer is volatile), this should |
| 106 | * 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] | 107 | * depends on the destination color space, then that information should also be incorporated |
| 108 | * in the key. |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 109 | */ |
| 110 | virtual void makeCopyKey(const CopyParams&, GrUniqueKey* copyKey, |
Brian Osman | 61624f0 | 2016-12-09 14:51:59 -0500 | [diff] [blame] | 111 | SkColorSpace* dstColorSpace) = 0; |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 112 | |
| 113 | /** |
| 114 | * If a stretched version of the texture is generated, it may be cached (assuming that |
| 115 | * makeCopyKey() returns true). In that case, the maker is notified in case it |
| 116 | * wants to note that for when the maker is destroyed. |
| 117 | */ |
| 118 | virtual void didCacheCopy(const GrUniqueKey& copyKey) = 0; |
| 119 | |
| 120 | |
| 121 | enum DomainMode { |
| 122 | kNoDomain_DomainMode, |
| 123 | kDomain_DomainMode, |
| 124 | kTightCopy_DomainMode |
| 125 | }; |
| 126 | |
Robert Phillips | b66b42f | 2017-03-14 08:53:02 -0400 | [diff] [blame] | 127 | static sk_sp<GrTextureProxy> CopyOnGpu(GrContext*, sk_sp<GrTextureProxy> inputProxy, |
Brian Salomon | 4df0092 | 2017-09-07 16:34:11 +0000 | [diff] [blame] | 128 | const SkIRect* subset, const CopyParams& copyParams); |
Robert Phillips | b66b42f | 2017-03-14 08:53:02 -0400 | [diff] [blame] | 129 | |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame^] | 130 | static DomainMode DetermineDomainMode(const SkRect& constraintRect, |
| 131 | FilterConstraint filterConstraint, |
| 132 | bool coordsLimitedToConstraintRect, |
| 133 | GrTextureProxy*, |
| 134 | const SkIRect* textureContentArea, |
| 135 | const GrSamplerState::Filter* filterModeOrNullForBicubic, |
| 136 | SkRect* domainRect); |
Robert Phillips | 51e7ca3 | 2017-03-27 10:14:08 -0400 | [diff] [blame] | 137 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 138 | static std::unique_ptr<GrFragmentProcessor> CreateFragmentProcessorForDomainAndFilter( |
| 139 | sk_sp<GrTextureProxy> proxy, |
| 140 | sk_sp<GrColorSpaceXform>, |
| 141 | const SkMatrix& textureMatrix, |
| 142 | DomainMode, |
| 143 | const SkRect& domain, |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame^] | 144 | const GrSamplerState::Filter* filterOrNullForBicubic); |
Robert Phillips | b66b42f | 2017-03-14 08:53:02 -0400 | [diff] [blame] | 145 | |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 146 | private: |
| 147 | const int fWidth; |
| 148 | const int fHeight; |
| 149 | const bool fIsAlphaOnly; |
| 150 | |
| 151 | typedef SkNoncopyable INHERITED; |
| 152 | }; |
| 153 | |
| 154 | #endif |