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
11#include "include/core/SkImageInfo.h"
12#include "include/private/GrResourceKey.h"
13#include "include/private/SkNoncopyable.h"
14#include "src/gpu/GrImageInfo.h"
15#include "src/gpu/GrSamplerState.h"
16
17class GrFragmentProcessor;
18class GrRecordingContext;
19class GrTexture;
20class GrTextureProxy;
21class SkColorSpace;
22class SkMatrix;
23struct SkRect;
24
25/**
26 * Different GPUs and API extensions have different requirements with respect to what texture
27 * sampling parameters may be used with textures of various types. This class facilitates making
28 * texture compatible with a given GrSamplerState. There are two immediate subclasses defined
29 * below. One is a base class for sources that are inherently texture-backed (e.g. a texture-backed
30 * SkImage). It supports subsetting the original texture. The other is for use cases where the
31 * source can generate a texture that represents some content (e.g. cpu pixels, SkPicture, ...).
32 */
33class GrTextureProducer : public SkNoncopyable {
34public:
35 virtual ~GrTextureProducer() {}
36
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
52 * bounds 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 **/
62 virtual std::unique_ptr<GrFragmentProcessor> createFragmentProcessor(
63 const SkMatrix& textureMatrix,
64 const SkRect& constraintRect,
65 FilterConstraint filterConstraint,
66 bool coordsLimitedToConstraintRect,
67 GrSamplerState::WrapMode wrapX,
68 GrSamplerState::WrapMode wrapY,
69 const GrSamplerState::Filter* filterOrNullForBicubic) = 0;
70
71 /**
72 * Returns a texture view, possibly with MIP maps. The request for MIP maps may not be honored
73 * base on caps, format, and whether the texture is 1x1. A non-MIP mapped request may still
74 * receive a MIP mapped texture (if that is what is available in the cache).
75 */
76 GrSurfaceProxyView view(GrMipMapped);
77
78 /** Helper version of above that determines MIP mapping requirement from Filter. */
79 GrSurfaceProxyView view(GrSamplerState::Filter);
80
81 int width() const { return fImageInfo.width(); }
82 int height() const { return fImageInfo.height(); }
83 SkISize dimensions() const { return fImageInfo.dimensions(); }
84 GrColorType colorType() const { return fImageInfo.colorType(); }
85 SkAlphaType alphaType() const { return fImageInfo.alphaType(); }
86 SkColorSpace* colorSpace() const { return fImageInfo.colorSpace(); }
87 bool isAlphaOnly() const { return GrColorTypeIsAlphaOnly(fImageInfo.colorType()); }
88 /* Is it a planar image consisting of multiple textures that may have different resolutions? */
89 virtual bool isPlanar() const { return false; }
90
91protected:
92 friend class GrTextureProducer_TestAccess;
93
94 GrTextureProducer(GrRecordingContext* context, const GrImageInfo& imageInfo)
95 : fContext(context), fImageInfo(imageInfo) {}
96
97 enum DomainMode {
98 kNoDomain_DomainMode,
99 kDomain_DomainMode,
100 kTightCopy_DomainMode
101 };
102
103 static DomainMode DetermineDomainMode(const SkRect& constraintRect,
104 FilterConstraint filterConstraint,
105 bool coordsLimitedToConstraintRect,
106 GrSurfaceProxy*,
107 const GrSamplerState::Filter* filterModeOrNullForBicubic,
108 SkRect* domainRect);
109
110 std::unique_ptr<GrFragmentProcessor> createFragmentProcessorForSubsetAndFilter(
111 GrSurfaceProxyView view,
112 const SkMatrix& textureMatrix,
113 DomainMode,
114 const SkRect& domain,
115 GrSamplerState::WrapMode wrapX,
116 GrSamplerState::WrapMode wrapY,
117 const GrSamplerState::Filter* filterOrNullForBicubic);
118
119 GrRecordingContext* context() const { return fContext; }
120
121private:
122 virtual GrSurfaceProxyView onView(GrMipMapped) = 0;
123
124 GrRecordingContext* fContext;
125 const GrImageInfo fImageInfo;
126
127 typedef SkNoncopyable INHERITED;
128};
129
130#endif
131