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 | #ifndef GrTextureMaker_DEFINED |
| 9 | #define GrTextureMaker_DEFINED |
| 10 | |
| 11 | #include "GrTextureParams.h" |
| 12 | #include "GrResourceKey.h" |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 13 | #include "GrTexture.h" |
bsalomon | 89fe56b | 2015-10-29 10:49:28 -0700 | [diff] [blame] | 14 | #include "SkTLazy.h" |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 15 | |
| 16 | class GrContext; |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 17 | class GrTextureParams; |
| 18 | class GrUniqueKey; |
| 19 | class SkBitmap; |
| 20 | |
| 21 | /** |
| 22 | * Different GPUs and API extensions have different requirements with respect to what texture |
| 23 | * sampling parameters may be used with textures of various types. This class facilitates making |
bsalomon | 89fe56b | 2015-10-29 10:49:28 -0700 | [diff] [blame] | 24 | * texture compatible with a given GrTextureParams. There are two immediate subclasses defined |
| 25 | * below. One is a base class for sources that are inherently texture-backed (e.g. a texture-backed |
| 26 | * SkImage). It supports subsetting the original texture. The other is for use cases where the |
| 27 | * source can generate a texture that represents some content (e.g. cpu pixels, SkPicture, ...). |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 28 | */ |
bsalomon | 89fe56b | 2015-10-29 10:49:28 -0700 | [diff] [blame] | 29 | class GrTextureProducer : public SkNoncopyable { |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 30 | public: |
| 31 | struct CopyParams { |
| 32 | GrTextureParams::FilterMode fFilter; |
| 33 | int fWidth; |
| 34 | int fHeight; |
| 35 | }; |
| 36 | |
bsalomon | b1b0199 | 2015-11-18 10:56:08 -0800 | [diff] [blame] | 37 | enum FilterConstraint { |
| 38 | kYes_FilterConstraint, |
| 39 | kNo_FilterConstraint, |
| 40 | }; |
| 41 | |
| 42 | /** |
| 43 | * Helper for creating a fragment processor to sample the texture with a given filtering mode. |
| 44 | * It attempts to avoid making texture copies or using domains whenever possible. |
| 45 | * |
| 46 | * @param textureMatrix Matrix used to access the texture. It is applied to |
| 47 | * the local coords. The post-transformed coords should |
| 48 | * be in texel units (rather than normalized) with |
| 49 | * respect to this Producer's bounds (width()/height()). |
| 50 | * @param constraintRect A rect that represents the area of the texture to be |
| 51 | * sampled. It must be contained in the Producer's bounds |
| 52 | * as defined by width()/height(). |
| 53 | * @param filterConstriant Indicates whether filtering is limited to |
| 54 | * constraintRect. |
| 55 | * @param coordsLimitedToConstraintRect Is it known that textureMatrix*localCoords is bound |
| 56 | * by the portion of the texture indicated by |
| 57 | * constraintRect (without consideration of filter |
| 58 | * width, just the raw coords). |
| 59 | * @param filterOrNullForBicubic If non-null indicates the filter mode. If null means |
| 60 | * use bicubic filtering. |
| 61 | **/ |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 62 | virtual sk_sp<GrFragmentProcessor> createFragmentProcessor( |
bsalomon | b1b0199 | 2015-11-18 10:56:08 -0800 | [diff] [blame] | 63 | const SkMatrix& textureMatrix, |
| 64 | const SkRect& constraintRect, |
| 65 | FilterConstraint filterConstraint, |
| 66 | bool coordsLimitedToConstraintRect, |
brianosman | 982eb7f | 2016-06-06 13:10:58 -0700 | [diff] [blame] | 67 | const GrTextureParams::FilterMode* filterOrNullForBicubic, |
| 68 | SkSourceGammaTreatment) = 0; |
bsalomon | b1b0199 | 2015-11-18 10:56:08 -0800 | [diff] [blame] | 69 | |
bsalomon | 89fe56b | 2015-10-29 10:49:28 -0700 | [diff] [blame] | 70 | virtual ~GrTextureProducer() {} |
| 71 | |
bsalomon | 3aa5fce | 2015-11-12 09:59:44 -0800 | [diff] [blame] | 72 | int width() const { return fWidth; } |
| 73 | int height() const { return fHeight; } |
bsalomon | f1ecd21 | 2015-12-09 17:06:02 -0800 | [diff] [blame] | 74 | bool isAlphaOnly() const { return fIsAlphaOnly; } |
bsalomon | 3aa5fce | 2015-11-12 09:59:44 -0800 | [diff] [blame] | 75 | |
bsalomon | 89fe56b | 2015-10-29 10:49:28 -0700 | [diff] [blame] | 76 | protected: |
bsalomon | f1ecd21 | 2015-12-09 17:06:02 -0800 | [diff] [blame] | 77 | GrTextureProducer(int width, int height, bool isAlphaOnly) |
| 78 | : fWidth(width) |
| 79 | , fHeight(height) |
| 80 | , fIsAlphaOnly(isAlphaOnly) {} |
bsalomon | 3aa5fce | 2015-11-12 09:59:44 -0800 | [diff] [blame] | 81 | |
bsalomon | 89fe56b | 2015-10-29 10:49:28 -0700 | [diff] [blame] | 82 | /** Helper for creating a key for a copy from an original key. */ |
| 83 | static void MakeCopyKeyFromOrigKey(const GrUniqueKey& origKey, |
| 84 | const CopyParams& copyParams, |
| 85 | GrUniqueKey* copyKey) { |
| 86 | SkASSERT(!copyKey->isValid()); |
| 87 | if (origKey.isValid()) { |
| 88 | static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain(); |
| 89 | GrUniqueKey::Builder builder(copyKey, origKey, kDomain, 3); |
| 90 | builder[0] = copyParams.fFilter; |
| 91 | builder[1] = copyParams.fWidth; |
| 92 | builder[2] = copyParams.fHeight; |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * If we need to make a copy in order to be compatible with GrTextureParams producer is asked to |
| 98 | * return a key that identifies its original content + the CopyParms parameter. If the producer |
| 99 | * does not want to cache the stretched version (e.g. the producer is volatile), this should |
| 100 | * simply return without initializing the copyKey. |
| 101 | */ |
| 102 | virtual void makeCopyKey(const CopyParams&, GrUniqueKey* copyKey) = 0; |
| 103 | |
| 104 | /** |
| 105 | * If a stretched version of the texture is generated, it may be cached (assuming that |
| 106 | * makeCopyKey() returns true). In that case, the maker is notified in case it |
| 107 | * wants to note that for when the maker is destroyed. |
| 108 | */ |
| 109 | virtual void didCacheCopy(const GrUniqueKey& copyKey) = 0; |
| 110 | |
brianosman | 4280366 | 2016-07-15 06:59:48 -0700 | [diff] [blame] | 111 | virtual SkColorSpace* getColorSpace() = 0; |
| 112 | |
bsalomon | 3aa5fce | 2015-11-12 09:59:44 -0800 | [diff] [blame] | 113 | private: |
bsalomon | f1ecd21 | 2015-12-09 17:06:02 -0800 | [diff] [blame] | 114 | const int fWidth; |
| 115 | const int fHeight; |
| 116 | const bool fIsAlphaOnly; |
bsalomon | 3aa5fce | 2015-11-12 09:59:44 -0800 | [diff] [blame] | 117 | |
bsalomon | 89fe56b | 2015-10-29 10:49:28 -0700 | [diff] [blame] | 118 | typedef SkNoncopyable INHERITED; |
| 119 | }; |
| 120 | |
bsalomon | c75be34 | 2015-10-29 12:34:31 -0700 | [diff] [blame] | 121 | /** |
| 122 | * Base class for sources that start out as textures. Optionally allows for a content area subrect. |
| 123 | * The intent is not to use content area for subrect rendering. Rather, the pixels outside the |
| 124 | * content area have undefined values and shouldn't be read *regardless* of filtering mode or |
| 125 | * the SkCanvas::SrcRectConstraint used for subrect draws. |
| 126 | */ |
bsalomon | 89fe56b | 2015-10-29 10:49:28 -0700 | [diff] [blame] | 127 | class GrTextureAdjuster : public GrTextureProducer { |
| 128 | public: |
| 129 | /** Makes the subset of the texture safe to use with the given texture parameters. |
| 130 | outOffset will be the top-left corner of the subset if a copy is not made. Otherwise, |
| 131 | the copy will be tight to the contents and outOffset will be (0, 0). If the copy's size |
| 132 | does not match subset's dimensions then the contents are scaled to fit the copy.*/ |
brianosman | 982eb7f | 2016-06-06 13:10:58 -0700 | [diff] [blame] | 133 | GrTexture* refTextureSafeForParams(const GrTextureParams&, SkSourceGammaTreatment, |
| 134 | SkIPoint* outOffset); |
bsalomon | 89fe56b | 2015-10-29 10:49:28 -0700 | [diff] [blame] | 135 | |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 136 | sk_sp<GrFragmentProcessor> createFragmentProcessor( |
bsalomon | b1b0199 | 2015-11-18 10:56:08 -0800 | [diff] [blame] | 137 | const SkMatrix& textureMatrix, |
| 138 | const SkRect& constraintRect, |
| 139 | FilterConstraint, |
| 140 | bool coordsLimitedToConstraintRect, |
brianosman | 982eb7f | 2016-06-06 13:10:58 -0700 | [diff] [blame] | 141 | const GrTextureParams::FilterMode* filterOrNullForBicubic, |
| 142 | SkSourceGammaTreatment) override; |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 143 | |
bsalomon | 89fe56b | 2015-10-29 10:49:28 -0700 | [diff] [blame] | 144 | protected: |
bsalomon | c75be34 | 2015-10-29 12:34:31 -0700 | [diff] [blame] | 145 | /** The whole texture is content. */ |
bsalomon | f1ecd21 | 2015-12-09 17:06:02 -0800 | [diff] [blame] | 146 | explicit GrTextureAdjuster(GrTexture* original, bool isAlphaOnly) |
| 147 | : INHERITED(original->width(), original->height(), isAlphaOnly) |
bsalomon | 3aa5fce | 2015-11-12 09:59:44 -0800 | [diff] [blame] | 148 | , fOriginal(original) {} |
bsalomon | 89fe56b | 2015-10-29 10:49:28 -0700 | [diff] [blame] | 149 | |
bsalomon | f1ecd21 | 2015-12-09 17:06:02 -0800 | [diff] [blame] | 150 | GrTextureAdjuster(GrTexture* original, const SkIRect& contentArea, bool isAlphaOnly); |
bsalomon | 89fe56b | 2015-10-29 10:49:28 -0700 | [diff] [blame] | 151 | |
bsalomon | 3aa5fce | 2015-11-12 09:59:44 -0800 | [diff] [blame] | 152 | GrTexture* originalTexture() const { return fOriginal; } |
| 153 | |
bsalomon | c75be34 | 2015-10-29 12:34:31 -0700 | [diff] [blame] | 154 | /** Returns the content area or null for the whole original texture */ |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 155 | const SkIRect* contentAreaOrNull() { return fContentArea.getMaybeNull(); } |
bsalomon | 89fe56b | 2015-10-29 10:49:28 -0700 | [diff] [blame] | 156 | |
| 157 | private: |
bsalomon | c75be34 | 2015-10-29 12:34:31 -0700 | [diff] [blame] | 158 | SkTLazy<SkIRect> fContentArea; |
bsalomon | 89fe56b | 2015-10-29 10:49:28 -0700 | [diff] [blame] | 159 | GrTexture* fOriginal; |
| 160 | |
bsalomon | e179a91 | 2016-01-20 06:18:10 -0800 | [diff] [blame] | 161 | GrTexture* refCopy(const CopyParams ©Params); |
| 162 | |
bsalomon | 89fe56b | 2015-10-29 10:49:28 -0700 | [diff] [blame] | 163 | typedef GrTextureProducer INHERITED; |
| 164 | }; |
| 165 | |
mtklein | 6459352 | 2015-11-12 10:41:05 -0800 | [diff] [blame] | 166 | /** |
bsalomon | 89fe56b | 2015-10-29 10:49:28 -0700 | [diff] [blame] | 167 | * Base class for sources that start out as something other than a texture (encoded image, |
| 168 | * picture, ...). |
| 169 | */ |
| 170 | class GrTextureMaker : public GrTextureProducer { |
| 171 | public: |
bsalomon | 89fe56b | 2015-10-29 10:49:28 -0700 | [diff] [blame] | 172 | /** Returns a texture that is safe for use with the params. If the size of the returned texture |
| 173 | does not match width()/height() then the contents of the original must be scaled to fit |
| 174 | the texture. */ |
brianosman | 982eb7f | 2016-06-06 13:10:58 -0700 | [diff] [blame] | 175 | GrTexture* refTextureForParams(const GrTextureParams&, SkSourceGammaTreatment); |
bsalomon | b1b0199 | 2015-11-18 10:56:08 -0800 | [diff] [blame] | 176 | |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 177 | sk_sp<GrFragmentProcessor> createFragmentProcessor( |
bsalomon | b1b0199 | 2015-11-18 10:56:08 -0800 | [diff] [blame] | 178 | const SkMatrix& textureMatrix, |
| 179 | const SkRect& constraintRect, |
| 180 | FilterConstraint filterConstraint, |
| 181 | bool coordsLimitedToConstraintRect, |
brianosman | 982eb7f | 2016-06-06 13:10:58 -0700 | [diff] [blame] | 182 | const GrTextureParams::FilterMode* filterOrNullForBicubic, |
| 183 | SkSourceGammaTreatment) override; |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 184 | |
| 185 | protected: |
bsalomon | f1ecd21 | 2015-12-09 17:06:02 -0800 | [diff] [blame] | 186 | GrTextureMaker(GrContext* context, int width, int height, bool isAlphaOnly) |
| 187 | : INHERITED(width, height, isAlphaOnly) |
bsalomon | b1b0199 | 2015-11-18 10:56:08 -0800 | [diff] [blame] | 188 | , fContext(context) {} |
bsalomon | 3aa5fce | 2015-11-12 09:59:44 -0800 | [diff] [blame] | 189 | |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 190 | /** |
bsalomon | b1b0199 | 2015-11-18 10:56:08 -0800 | [diff] [blame] | 191 | * Return the maker's "original" texture. It is the responsibility of the maker to handle any |
| 192 | * caching of the original if desired. |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 193 | */ |
brianosman | 982eb7f | 2016-06-06 13:10:58 -0700 | [diff] [blame] | 194 | virtual GrTexture* refOriginalTexture(bool willBeMipped, SkSourceGammaTreatment) = 0; |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 195 | |
| 196 | /** |
| 197 | * Return a new (uncached) texture that is the stretch of the maker's original. |
| 198 | * |
| 199 | * The base-class handles general logic for this, and only needs access to the following |
bsalomon | 100b8f8 | 2015-10-28 08:37:44 -0700 | [diff] [blame] | 200 | * method: |
| 201 | * - refOriginalTexture() |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 202 | * |
| 203 | * Subclass may override this if they can handle creating the texture more directly than |
| 204 | * by copying. |
| 205 | */ |
brianosman | 982eb7f | 2016-06-06 13:10:58 -0700 | [diff] [blame] | 206 | virtual GrTexture* generateTextureForParams(const CopyParams&, bool willBeMipped, |
| 207 | SkSourceGammaTreatment); |
bsalomon | b1b0199 | 2015-11-18 10:56:08 -0800 | [diff] [blame] | 208 | |
| 209 | GrContext* context() const { return fContext; } |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 210 | |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 211 | private: |
bsalomon | b1b0199 | 2015-11-18 10:56:08 -0800 | [diff] [blame] | 212 | GrContext* fContext; |
| 213 | |
bsalomon | 89fe56b | 2015-10-29 10:49:28 -0700 | [diff] [blame] | 214 | typedef GrTextureProducer INHERITED; |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 215 | }; |
| 216 | |
| 217 | #endif |