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 | 89fe56b | 2015-10-29 10:49:28 -0700 | [diff] [blame] | 13 | #include "SkTLazy.h" |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 14 | |
| 15 | class GrContext; |
| 16 | class GrTexture; |
| 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 | 89fe56b | 2015-10-29 10:49:28 -0700 | [diff] [blame] | 37 | virtual ~GrTextureProducer() {} |
| 38 | |
| 39 | protected: |
| 40 | /** Helper for creating a key for a copy from an original key. */ |
| 41 | static void MakeCopyKeyFromOrigKey(const GrUniqueKey& origKey, |
| 42 | const CopyParams& copyParams, |
| 43 | GrUniqueKey* copyKey) { |
| 44 | SkASSERT(!copyKey->isValid()); |
| 45 | if (origKey.isValid()) { |
| 46 | static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain(); |
| 47 | GrUniqueKey::Builder builder(copyKey, origKey, kDomain, 3); |
| 48 | builder[0] = copyParams.fFilter; |
| 49 | builder[1] = copyParams.fWidth; |
| 50 | builder[2] = copyParams.fHeight; |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * If we need to make a copy in order to be compatible with GrTextureParams producer is asked to |
| 56 | * return a key that identifies its original content + the CopyParms parameter. If the producer |
| 57 | * does not want to cache the stretched version (e.g. the producer is volatile), this should |
| 58 | * simply return without initializing the copyKey. |
| 59 | */ |
| 60 | virtual void makeCopyKey(const CopyParams&, GrUniqueKey* copyKey) = 0; |
| 61 | |
| 62 | /** |
| 63 | * If a stretched version of the texture is generated, it may be cached (assuming that |
| 64 | * makeCopyKey() returns true). In that case, the maker is notified in case it |
| 65 | * wants to note that for when the maker is destroyed. |
| 66 | */ |
| 67 | virtual void didCacheCopy(const GrUniqueKey& copyKey) = 0; |
| 68 | |
| 69 | typedef SkNoncopyable INHERITED; |
| 70 | }; |
| 71 | |
bsalomon | c75be34 | 2015-10-29 12:34:31 -0700 | [diff] [blame^] | 72 | /** |
| 73 | * Base class for sources that start out as textures. Optionally allows for a content area subrect. |
| 74 | * The intent is not to use content area for subrect rendering. Rather, the pixels outside the |
| 75 | * content area have undefined values and shouldn't be read *regardless* of filtering mode or |
| 76 | * the SkCanvas::SrcRectConstraint used for subrect draws. |
| 77 | */ |
bsalomon | 89fe56b | 2015-10-29 10:49:28 -0700 | [diff] [blame] | 78 | class GrTextureAdjuster : public GrTextureProducer { |
| 79 | public: |
| 80 | /** Makes the subset of the texture safe to use with the given texture parameters. |
| 81 | outOffset will be the top-left corner of the subset if a copy is not made. Otherwise, |
| 82 | the copy will be tight to the contents and outOffset will be (0, 0). If the copy's size |
| 83 | does not match subset's dimensions then the contents are scaled to fit the copy.*/ |
| 84 | GrTexture* refTextureSafeForParams(const GrTextureParams&, SkIPoint* outOffset); |
| 85 | |
| 86 | protected: |
bsalomon | c75be34 | 2015-10-29 12:34:31 -0700 | [diff] [blame^] | 87 | /** The whole texture is content. */ |
bsalomon | 89fe56b | 2015-10-29 10:49:28 -0700 | [diff] [blame] | 88 | explicit GrTextureAdjuster(GrTexture* original): fOriginal(original) {} |
| 89 | |
bsalomon | c75be34 | 2015-10-29 12:34:31 -0700 | [diff] [blame^] | 90 | GrTextureAdjuster(GrTexture* original, const SkIRect& contentArea); |
bsalomon | 89fe56b | 2015-10-29 10:49:28 -0700 | [diff] [blame] | 91 | |
| 92 | GrTexture* originalTexture() { return fOriginal; } |
| 93 | |
bsalomon | c75be34 | 2015-10-29 12:34:31 -0700 | [diff] [blame^] | 94 | /** Returns the content area or null for the whole original texture */ |
| 95 | const SkIRect* contentArea() { return fContentArea.getMaybeNull(); } |
bsalomon | 89fe56b | 2015-10-29 10:49:28 -0700 | [diff] [blame] | 96 | |
| 97 | private: |
bsalomon | c75be34 | 2015-10-29 12:34:31 -0700 | [diff] [blame^] | 98 | SkTLazy<SkIRect> fContentArea; |
bsalomon | 89fe56b | 2015-10-29 10:49:28 -0700 | [diff] [blame] | 99 | GrTexture* fOriginal; |
| 100 | |
| 101 | typedef GrTextureProducer INHERITED; |
| 102 | }; |
| 103 | |
| 104 | /** |
| 105 | * Base class for sources that start out as something other than a texture (encoded image, |
| 106 | * picture, ...). |
| 107 | */ |
| 108 | class GrTextureMaker : public GrTextureProducer { |
| 109 | public: |
| 110 | |
| 111 | GrTextureMaker(int width, int height) : fWidth(width), fHeight(height) {} |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 112 | |
| 113 | int width() const { return fWidth; } |
| 114 | int height() const { return fHeight; } |
| 115 | |
bsalomon | 89fe56b | 2015-10-29 10:49:28 -0700 | [diff] [blame] | 116 | /** Returns a texture that is safe for use with the params. If the size of the returned texture |
| 117 | does not match width()/height() then the contents of the original must be scaled to fit |
| 118 | the texture. */ |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 119 | GrTexture* refTextureForParams(GrContext*, const GrTextureParams&); |
| 120 | |
| 121 | protected: |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 122 | /** |
| 123 | * Return the maker's "original" texture. It is the responsibility of the maker |
| 124 | * to make this efficient ... if the texture is being generated, the maker must handle |
| 125 | * caching it (if desired). |
| 126 | */ |
| 127 | virtual GrTexture* refOriginalTexture(GrContext*) = 0; |
| 128 | |
| 129 | /** |
bsalomon | 89fe56b | 2015-10-29 10:49:28 -0700 | [diff] [blame] | 130 | * If we need to copy the producer's original texture, the producer is asked to return a key |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 131 | * that identifies its original + the CopyParms parameter. If the maker does not want to cache |
bsalomon | 89fe56b | 2015-10-29 10:49:28 -0700 | [diff] [blame] | 132 | * the stretched version (e.g. the producer is volatile), this should simply return without |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 133 | * initializing the copyKey. |
| 134 | */ |
| 135 | virtual void makeCopyKey(const CopyParams&, GrUniqueKey* copyKey) = 0; |
| 136 | |
| 137 | /** |
| 138 | * Return a new (uncached) texture that is the stretch of the maker's original. |
| 139 | * |
| 140 | * 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] | 141 | * method: |
| 142 | * - refOriginalTexture() |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 143 | * |
| 144 | * Subclass may override this if they can handle creating the texture more directly than |
| 145 | * by copying. |
| 146 | */ |
| 147 | virtual GrTexture* generateTextureForParams(GrContext*, const CopyParams&); |
| 148 | |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 149 | private: |
| 150 | const int fWidth; |
| 151 | const int fHeight; |
bsalomon | 89fe56b | 2015-10-29 10:49:28 -0700 | [diff] [blame] | 152 | |
| 153 | typedef GrTextureProducer INHERITED; |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 154 | }; |
| 155 | |
| 156 | #endif |