blob: 71d581c14ab4839b9c30e6181678b9c5648ff9c8 [file] [log] [blame]
robertphillips76948d42016-05-04 12:47:41 -07001/*
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 GrTextureProxy_DEFINED
9#define GrTextureProxy_DEFINED
10
Brian Salomon2bbdcc42017-09-07 12:36:34 -040011#include "GrSamplerState.h"
robertphillips76948d42016-05-04 12:47:41 -070012#include "GrSurfaceProxy.h"
robertphillips76948d42016-05-04 12:47:41 -070013
Robert Phillips84a81202016-11-04 11:59:10 -040014class GrCaps;
Brian Osman099fa0f2017-10-02 16:38:32 -040015class GrDeferredProxyUploader;
Robert Phillips1afd4cd2018-01-08 13:40:32 -050016class GrProxyProvider;
Brian Osman32342f02017-03-04 08:12:46 -050017class GrResourceProvider;
Brian Osman45580d32016-11-23 09:37:01 -050018class GrTextureOpList;
Robert Phillips420c4cf2017-09-28 09:00:45 -040019class GrTextureProxyPriv;
robertphillips76948d42016-05-04 12:47:41 -070020
21// This class delays the acquisition of textures until they are actually required
Robert Phillips84a81202016-11-04 11:59:10 -040022class GrTextureProxy : virtual public GrSurfaceProxy {
robertphillips76948d42016-05-04 12:47:41 -070023public:
robertphillips76948d42016-05-04 12:47:41 -070024 GrTextureProxy* asTextureProxy() override { return this; }
25 const GrTextureProxy* asTextureProxy() const override { return this; }
26
27 // Actually instantiate the backing texture, if necessary
Robert Phillipseee4d6e2017-06-05 09:26:07 -040028 bool instantiate(GrResourceProvider*) override;
robertphillips76948d42016-05-04 12:47:41 -070029
Brian Salomon2bbdcc42017-09-07 12:36:34 -040030 GrSamplerState::Filter highestFilterMode() const;
Robert Phillips49081d12017-05-08 13:41:35 -040031
Greg Daniel3b2ebbb2018-02-09 10:49:23 -050032 // If we are instantiated and have a target, return the mip state of that target. Otherwise
33 // returns the proxy's mip state from creation time. This is useful for lazy proxies which may
34 // claim to not need mips at creation time, but the instantiation happens to give us a mipped
35 // target. In that case we should use that for our benefit to avoid possible copies/mip
36 // generation later.
37 GrMipMapped mipMapped() const;
Brian Salomonbb5711a2017-05-17 13:49:59 -040038
Brian Salomonfd98c2c2018-07-31 17:25:29 -040039 // Returns the GrMipMapped value of the proxy from creation time regardless of whether it has
40 // been instantiated or not.
41 GrMipMapped proxyMipMapped() const { return fMipMapped; }
42
Brian Salomon7da2fc72018-12-10 13:40:07 -050043 GrTextureType textureType() const { return this->backendFormat().textureType(); }
44
Brian Salomonfd98c2c2018-07-31 17:25:29 -040045 /** If true then the texture does not support MIP maps and only supports clamp wrap mode. */
Greg Daniel4065d452018-11-16 15:43:41 -050046 bool hasRestrictedSampling() const {
47 return GrTextureTypeHasRestrictedSampling(this->textureType());
48 }
Greg Daniel45723ac2018-11-30 10:12:43 -050049
50 // Returns true if the passed in proxies can be used as dynamic state together when flushing
51 // draws to the gpu.
52 static bool ProxiesAreCompatibleAsDynamicState(const GrTextureProxy* first,
53 const GrTextureProxy* second);
54
Robert Phillipsae7d3f32017-09-21 08:26:08 -040055 /**
56 * Return the texture proxy's unique key. It will be invalid if the proxy doesn't have one.
57 */
58 const GrUniqueKey& getUniqueKey() const {
59#ifdef SK_DEBUG
60 if (fTarget && fUniqueKey.isValid()) {
61 SkASSERT(fTarget->getUniqueKey().isValid());
62 // It is possible for a non-keyed proxy to have a uniquely keyed resource assigned to
63 // it. This just means that a future user of the resource will be filling it with unique
64 // data. However, if the proxy has a unique key its attached resource should also
65 // have that key.
66 SkASSERT(fUniqueKey == fTarget->getUniqueKey());
67 }
68#endif
69
70 return fUniqueKey;
71 }
72
73 /**
74 * Internal-only helper class used for manipulations of the resource by the cache.
75 */
76 class CacheAccess;
77 inline CacheAccess cacheAccess();
78 inline const CacheAccess cacheAccess() const;
79
Robert Phillips420c4cf2017-09-28 09:00:45 -040080 // Provides access to special purpose functions.
81 GrTextureProxyPriv texPriv();
82 const GrTextureProxyPriv texPriv() const;
83
Robert Phillips84a81202016-11-04 11:59:10 -040084protected:
Robert Phillips0bd24dc2018-01-16 08:06:32 -050085 // DDL TODO: rm the GrSurfaceProxy friending
Robert Phillips37430132016-11-09 06:50:43 -050086 friend class GrSurfaceProxy; // for ctors
Robert Phillips0bd24dc2018-01-16 08:06:32 -050087 friend class GrProxyProvider; // for ctors
Brian Osman099fa0f2017-10-02 16:38:32 -040088 friend class GrTextureProxyPriv;
Robert Phillips37430132016-11-09 06:50:43 -050089
Brian Salomon58389b92018-03-07 13:01:25 -050090 // Deferred version - when constructed with data the origin is always kTopLeft.
Greg Daniel4065d452018-11-16 15:43:41 -050091 GrTextureProxy(const GrBackendFormat&, const GrSurfaceDesc& srcDesc, GrMipMapped, SkBackingFit,
Brian Salomon7226c232018-07-30 13:13:17 -040092 SkBudgeted, const void* srcData, size_t srcRowBytes, GrInternalSurfaceFlags);
Brian Salomon58389b92018-03-07 13:01:25 -050093
94 // Deferred version - no data.
Greg Daniel4065d452018-11-16 15:43:41 -050095 GrTextureProxy(const GrBackendFormat&, const GrSurfaceDesc& srcDesc, GrSurfaceOrigin,
96 GrMipMapped, SkBackingFit, SkBudgeted, GrInternalSurfaceFlags);
Chris Dalton706a6ff2017-11-29 22:01:06 -070097
98 // Lazy-callback version
Greg Daniel65fa8ca2018-01-10 17:06:31 -050099 // There are two main use cases for lazily-instantiated proxies:
100 // basic knowledge - width, height, config, origin are known
101 // minimal knowledge - only config is known.
102 //
103 // The basic knowledge version is used for DDL where we know the type of proxy we are going to
104 // use, but we don't have access to the GPU yet to instantiate it.
105 //
106 // The minimal knowledge version is used for CCPR where we are generating an atlas but we do not
107 // know the final size until flush time.
Greg Daniel4065d452018-11-16 15:43:41 -0500108 GrTextureProxy(LazyInstantiateCallback&&, LazyInstantiationType, const GrBackendFormat&,
109 const GrSurfaceDesc& desc, GrSurfaceOrigin, GrMipMapped, SkBackingFit,
110 SkBudgeted, GrInternalSurfaceFlags);
Chris Dalton706a6ff2017-11-29 22:01:06 -0700111
robertphillips76948d42016-05-04 12:47:41 -0700112 // Wrapped version
Robert Phillips066f0202017-07-25 10:16:35 -0400113 GrTextureProxy(sk_sp<GrSurface>, GrSurfaceOrigin);
robertphillips76948d42016-05-04 12:47:41 -0700114
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400115 ~GrTextureProxy() override;
116
Robert Phillips5af44de2017-07-18 14:49:38 -0400117 sk_sp<GrSurface> createSurface(GrResourceProvider*) const override;
118
Robert Phillips84a81202016-11-04 11:59:10 -0400119private:
Brian Osman086679b2018-09-10 16:26:51 -0400120 // WARNING: Be careful when adding or removing fields here. ASAN is likely to trigger warnings
121 // when instantiating GrTextureRenderTargetProxy. The std::function in GrSurfaceProxy makes
122 // each class in the diamond require 16 byte alignment. Clang appears to layout the fields for
123 // each class to achieve the necessary alignment. However, ASAN checks the alignment of 'this'
124 // in the constructors, and always looks for the full 16 byte alignment, even if the fields in
125 // that particular class don't require it. Changing the size of this object can move the start
126 // address of other types, leading to this problem.
127
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400128 GrMipMapped fMipMapped;
Brian Salomonbb5711a2017-05-17 13:49:59 -0400129
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500130 GrUniqueKey fUniqueKey;
131 GrProxyProvider* fProxyProvider; // only set when fUniqueKey is valid
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400132
Brian Osman099fa0f2017-10-02 16:38:32 -0400133 // Only used for proxies whose contents are being prepared on a worker thread. This object
134 // stores the texture data, allowing the proxy to remain uninstantiated until flush. At that
135 // point, the proxy is instantiated, and this data is used to perform an ASAP upload.
136 std::unique_ptr<GrDeferredProxyUploader> fDeferredUploader;
137
Brian Salomonbb5711a2017-05-17 13:49:59 -0400138 size_t onUninstantiatedGpuMemorySize() const override;
Robert Phillips8bc06d02016-11-01 17:28:40 -0400139
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400140 // Methods made available via GrTextureProxy::CacheAccess
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500141 void setUniqueKey(GrProxyProvider*, const GrUniqueKey&);
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400142 void clearUniqueKey();
143
Greg Daniel849dce12018-04-24 14:32:53 -0400144 SkDEBUGCODE(void onValidateSurface(const GrSurface*) override;)
Chris Dalton706a6ff2017-11-29 22:01:06 -0700145
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400146 // For wrapped proxies the GrTexture pointer is stored in GrIORefProxy.
Robert Phillipsa4c41b32017-03-15 13:02:45 -0400147 // For deferred proxies that pointer will be filled in when we need to instantiate
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400148 // the deferred resource
robertphillips76948d42016-05-04 12:47:41 -0700149
150 typedef GrSurfaceProxy INHERITED;
151};
152
153#endif