blob: bc08c71af5959e3053fadb414c0cfee05e0a1fff [file] [log] [blame]
bsalomon045802d2015-10-20 07:58:01 -07001/*
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"
13
14class GrContext;
15class GrTexture;
16class GrTextureParams;
17class GrUniqueKey;
18class SkBitmap;
19
20/**
21 * Different GPUs and API extensions have different requirements with respect to what texture
22 * sampling parameters may be used with textures of various types. This class facilitates making
23 * texture compatible with a given GrTextureParams. It abstracts the source of the original data
24 * which may be an already existing texture, CPU pixels, a codec, ... so that various sources can
25 * be used with common code that scales or copies the data to make it compatible with a
26 * GrTextureParams.
27 */
28class GrTextureParamsAdjuster {
29public:
30 struct CopyParams {
31 GrTextureParams::FilterMode fFilter;
32 int fWidth;
33 int fHeight;
34 };
35
36 GrTextureParamsAdjuster(int width, int height) : fWidth(width), fHeight(height) {}
37 virtual ~GrTextureParamsAdjuster() {}
38
39 int width() const { return fWidth; }
40 int height() const { return fHeight; }
41
42 /** Returns a texture that is safe for use with the params */
43 GrTexture* refTextureForParams(GrContext*, const GrTextureParams&);
44
45protected:
bsalomon045802d2015-10-20 07:58:01 -070046 /**
47 * Return the maker's "original" texture. It is the responsibility of the maker
48 * to make this efficient ... if the texture is being generated, the maker must handle
49 * caching it (if desired).
50 */
51 virtual GrTexture* refOriginalTexture(GrContext*) = 0;
52
53 /**
54 * If we need to copy the maker's original texture, the maker is asked to return a key
55 * that identifies its original + the CopyParms parameter. If the maker does not want to cache
56 * the stretched version (e.g. the maker is volatile), this should simply return without
57 * initializing the copyKey.
58 */
59 virtual void makeCopyKey(const CopyParams&, GrUniqueKey* copyKey) = 0;
60
61 /**
62 * Return a new (uncached) texture that is the stretch of the maker's original.
63 *
64 * The base-class handles general logic for this, and only needs access to the following
bsalomon100b8f82015-10-28 08:37:44 -070065 * method:
66 * - refOriginalTexture()
bsalomon045802d2015-10-20 07:58:01 -070067 *
68 * Subclass may override this if they can handle creating the texture more directly than
69 * by copying.
70 */
71 virtual GrTexture* generateTextureForParams(GrContext*, const CopyParams&);
72
73 /**
74 * If a stretched version of the texture is generated, it may be cached (assuming that
75 * onMakeParamsKey() returns true). In that case, the maker is notified in case it
76 * wants to note that for when the maker is destroyed.
77 */
78 virtual void didCacheCopy(const GrUniqueKey& copyKey) = 0;
79
bsalomon045802d2015-10-20 07:58:01 -070080 /** 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
94private:
95 const int fWidth;
96 const int fHeight;
97};
98
99#endif