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 GrTextureMaker_DEFINED
9 #define GrTextureMaker_DEFINED
10 
11 #include "GrTextureProducer.h"
12 
13 /**
14  * Base class for sources that start out as something other than a texture (encoded image,
15  * picture, ...).
16  */
17 class GrTextureMaker : public GrTextureProducer {
18 public:
19     enum class AllowedTexGenType : bool { kCheap, kAny };
20 
21     std::unique_ptr<GrFragmentProcessor> createFragmentProcessor(
22             const SkMatrix& textureMatrix,
23             const SkRect& constraintRect,
24             FilterConstraint filterConstraint,
25             bool coordsLimitedToConstraintRect,
26             const GrSamplerState::Filter* filterOrNullForBicubic,
27             SkColorSpace* dstColorSpace) override;
28 
29 protected:
30     GrTextureMaker(GrContext* context, int width, int height, bool isAlphaOnly)
31         : INHERITED(width, height, isAlphaOnly)
32         , fContext(context) {}
33 
34     /**
35      *  Return the maker's "original" texture. It is the responsibility of the maker to handle any
36      *  caching of the original if desired.
37      *  If "genType" argument equals AllowedTexGenType::kCheap and the texture is not trivial to
38      *  construct then refOriginalTextureProxy should return nullptr (for example if texture is made
39      *  by drawing into a render target).
40      */
41     virtual sk_sp<GrTextureProxy> refOriginalTextureProxy(bool willBeMipped,
42                                                           SkColorSpace* dstColorSpace,
43                                                           AllowedTexGenType genType) = 0;
44 
45     /**
46      *  Returns the color space of the maker's "original" texture, assuming it was retrieved with
47      *  the same destination color space.
48      */
49     virtual sk_sp<SkColorSpace> getColorSpace(SkColorSpace* dstColorSpace) = 0;
50 
51     /**
52      *  Return a new (uncached) texture that is the stretch of the maker's original.
53      *
54      *  The base-class handles general logic for this, and only needs access to the following
55      *  method:
56      *  - refOriginalTextureProxy()
57      *
58      *  Subclass may override this if they can handle creating the texture more directly than
59      *  by copying.
60      */
61     virtual sk_sp<GrTextureProxy> generateTextureProxyForParams(const CopyParams&,
62                                                                 bool willBeMipped,
63                                                                 SkColorSpace* dstColorSpace);
64 
65     GrContext* context() const { return fContext; }
66 
67 private:
68     sk_sp<GrTextureProxy> onRefTextureProxyForParams(const GrSamplerState&,
69                                                      SkColorSpace* dstColorSpace,
70                                                      sk_sp<SkColorSpace>* proxyColorSpace,
71                                                      SkScalar scaleAdjust[2]) override;
72 
73     GrContext*  fContext;
74 
75     typedef GrTextureProducer INHERITED;
76 };
77 
78 #endif
79