blob: fefdf1969f849e917b6ea8f54c4ea2326c65153f [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
8#ifndef GrTextureProducer_DEFINED
9#define GrTextureProducer_DEFINED
10
11#include "GrSamplerParams.h"
12#include "GrResourceKey.h"
13
14class GrColorSpaceXform;
15class GrTexture;
Robert Phillipsb66b42f2017-03-14 08:53:02 -040016class GrTextureProxy;
Brian Osmane8e54582016-11-28 10:06:27 -050017
18/**
19 * Different GPUs and API extensions have different requirements with respect to what texture
20 * sampling parameters may be used with textures of various types. This class facilitates making
21 * texture compatible with a given GrSamplerParams. There are two immediate subclasses defined
22 * below. One is a base class for sources that are inherently texture-backed (e.g. a texture-backed
23 * SkImage). It supports subsetting the original texture. The other is for use cases where the
24 * source can generate a texture that represents some content (e.g. cpu pixels, SkPicture, ...).
25 */
26class GrTextureProducer : public SkNoncopyable {
27public:
28 struct CopyParams {
29 GrSamplerParams::FilterMode fFilter;
30 int fWidth;
31 int fHeight;
32 };
33
34 enum FilterConstraint {
35 kYes_FilterConstraint,
36 kNo_FilterConstraint,
37 };
38
39 /**
40 * Helper for creating a fragment processor to sample the texture with a given filtering mode.
41 * It attempts to avoid making texture copies or using domains whenever possible.
42 *
43 * @param textureMatrix Matrix used to access the texture. It is applied to
44 * the local coords. The post-transformed coords should
45 * be in texel units (rather than normalized) with
46 * respect to this Producer's bounds (width()/height()).
47 * @param constraintRect A rect that represents the area of the texture to be
48 * sampled. It must be contained in the Producer's
49 * bounds as defined by width()/height().
50 * @param filterConstriant Indicates whether filtering is limited to
51 * constraintRect.
52 * @param coordsLimitedToConstraintRect Is it known that textureMatrix*localCoords is bound
53 * by the portion of the texture indicated by
54 * constraintRect (without consideration of filter
55 * width, just the raw coords).
56 * @param filterOrNullForBicubic If non-null indicates the filter mode. If null means
57 * use bicubic filtering.
58 **/
59 virtual sk_sp<GrFragmentProcessor> createFragmentProcessor(
60 const SkMatrix& textureMatrix,
61 const SkRect& constraintRect,
62 FilterConstraint filterConstraint,
63 bool coordsLimitedToConstraintRect,
64 const GrSamplerParams::FilterMode* filterOrNullForBicubic,
Brian Osman61624f02016-12-09 14:51:59 -050065 SkColorSpace* dstColorSpace) = 0;
Brian Osmane8e54582016-11-28 10:06:27 -050066
67 virtual ~GrTextureProducer() {}
68
69 int width() const { return fWidth; }
70 int height() const { return fHeight; }
71 bool isAlphaOnly() const { return fIsAlphaOnly; }
72 virtual SkAlphaType alphaType() const = 0;
73
74protected:
75 GrTextureProducer(int width, int height, bool isAlphaOnly)
76 : fWidth(width)
77 , fHeight(height)
78 , fIsAlphaOnly(isAlphaOnly) {}
79
80 /** Helper for creating a key for a copy from an original key. */
81 static void MakeCopyKeyFromOrigKey(const GrUniqueKey& origKey,
82 const CopyParams& copyParams,
83 GrUniqueKey* copyKey) {
84 SkASSERT(!copyKey->isValid());
85 if (origKey.isValid()) {
86 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
87 GrUniqueKey::Builder builder(copyKey, origKey, kDomain, 3);
88 builder[0] = copyParams.fFilter;
89 builder[1] = copyParams.fWidth;
90 builder[2] = copyParams.fHeight;
91 }
92 }
93
94 /**
95 * If we need to make a copy in order to be compatible with GrTextureParams producer is asked to
96 * return a key that identifies its original content + the CopyParms parameter. If the producer
97 * does not want to cache the stretched version (e.g. the producer is volatile), this should
98 * simply return without initializing the copyKey. If the texture generated by this producer
Brian Osman61624f02016-12-09 14:51:59 -050099 * depends on the destination color space, then that information should also be incorporated
100 * in the key.
Brian Osmane8e54582016-11-28 10:06:27 -0500101 */
102 virtual void makeCopyKey(const CopyParams&, GrUniqueKey* copyKey,
Brian Osman61624f02016-12-09 14:51:59 -0500103 SkColorSpace* dstColorSpace) = 0;
Brian Osmane8e54582016-11-28 10:06:27 -0500104
105 /**
106 * If a stretched version of the texture is generated, it may be cached (assuming that
107 * makeCopyKey() returns true). In that case, the maker is notified in case it
108 * wants to note that for when the maker is destroyed.
109 */
110 virtual void didCacheCopy(const GrUniqueKey& copyKey) = 0;
111
112
113 enum DomainMode {
114 kNoDomain_DomainMode,
115 kDomain_DomainMode,
116 kTightCopy_DomainMode
117 };
118
119 static GrTexture* CopyOnGpu(GrTexture* inputTexture, const SkIRect* subset,
120 const CopyParams& copyParams);
121
Robert Phillipsb66b42f2017-03-14 08:53:02 -0400122 static sk_sp<GrTextureProxy> CopyOnGpu(GrContext*, sk_sp<GrTextureProxy> inputProxy,
123 const SkIRect* subset, const CopyParams& copyParams);
124
Brian Osmane8e54582016-11-28 10:06:27 -0500125 static DomainMode DetermineDomainMode(
126 const SkRect& constraintRect,
127 FilterConstraint filterConstraint,
128 bool coordsLimitedToConstraintRect,
129 int texW, int texH,
130 const SkIRect* textureContentArea,
131 const GrSamplerParams::FilterMode* filterModeOrNullForBicubic,
132 SkRect* domainRect);
133
134 static sk_sp<GrFragmentProcessor> CreateFragmentProcessorForDomainAndFilter(
135 GrTexture* texture,
136 sk_sp<GrColorSpaceXform> colorSpaceXform,
137 const SkMatrix& textureMatrix,
138 DomainMode domainMode,
139 const SkRect& domain,
140 const GrSamplerParams::FilterMode* filterOrNullForBicubic);
141
Robert Phillipsb66b42f2017-03-14 08:53:02 -0400142 static sk_sp<GrFragmentProcessor> CreateFragmentProcessorForDomainAndFilter(
143 GrContext* context,
144 sk_sp<GrTextureProxy> proxy,
145 sk_sp<GrColorSpaceXform> colorSpaceXform,
146 const SkMatrix& textureMatrix,
147 DomainMode,
148 const SkRect& domain,
149 const GrSamplerParams::FilterMode* filterOrNullForBicubic);
150
Brian Osmane8e54582016-11-28 10:06:27 -0500151private:
152 const int fWidth;
153 const int fHeight;
154 const bool fIsAlphaOnly;
155
156 typedef SkNoncopyable INHERITED;
157};
158
159#endif