blob: 25ba38aa47da8c978107bba65b5a3de5a8f6bbd8 [file] [log] [blame]
reed@google.comac10a2d2010-12-22 21:39:39 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2011 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.
reed@google.comac10a2d2010-12-22 21:39:39 +00006 */
7
epoger@google.comec3ed6a2011-07-28 14:26:00 +00008
reed@google.comac10a2d2010-12-22 21:39:39 +00009#ifndef GrGLTexture_DEFINED
10#define GrGLTexture_DEFINED
11
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000012#include "GrGpu.h"
bsalomon37dd3312014-11-03 08:47:23 -080013#include "GrTexture.h"
14#include "GrGLUtil.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000015
jvanverthd1e72872015-04-20 12:29:37 -070016class GrGLGpu;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +000017
bsalomonafbf2d62014-09-30 12:18:44 -070018class GrGLTexture : public GrTexture {
reed@google.comac10a2d2010-12-22 21:39:39 +000019public:
Brian Salomondc829942018-10-23 16:07:24 -040020 // Texture state that overlaps with sampler object state. We don't need to track this if we
21 // are using sampler objects.
22 struct SamplerParams {
23 // These are the OpenGL defaults.
24 GrGLenum fMinFilter = GR_GL_NEAREST_MIPMAP_LINEAR;
25 GrGLenum fMagFilter = GR_GL_LINEAR;
26 GrGLenum fWrapS = GR_GL_REPEAT;
27 GrGLenum fWrapT = GR_GL_REPEAT;
28 GrGLfloat fMinLOD = -1000.f;
29 GrGLfloat fMaxLOD = 1000.f;
30 void invalidate() {
31 fMinFilter = ~0U;
32 fMagFilter = ~0U;
33 fWrapS = ~0U;
34 fWrapT = ~0U;
35 fMinLOD = SK_ScalarNaN;
36 fMaxLOD = SK_ScalarNaN;
37 }
38 };
39
40 // Texture state that does not overlap with sampler object state.
41 struct NonSamplerParams {
42 // These are the OpenGL defaults.
43 uint32_t fSwizzleKey = GrSwizzle::RGBA().asKey();
44 GrGLint fBaseMipMapLevel = 0;
45 GrGLint fMaxMipMapLevel = 1000;
46 void invalidate() {
47 fSwizzleKey = ~0U;
48 fBaseMipMapLevel = ~0;
49 fMaxMipMapLevel = ~0;
50 }
bsalomon@google.comda96ea02010-12-23 16:53:57 +000051 };
reed@google.comac10a2d2010-12-22 21:39:39 +000052
bsalomonb15b4c12014-10-29 12:41:57 -070053 struct IDDesc {
bsalomon091f60c2015-11-10 11:54:56 -080054 GrGLTextureInfo fInfo;
kkinnunen2e6055b2016-04-22 01:48:29 -070055 GrBackendObjectOwnership fOwnership;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +000056 };
Brian Salomon7226c232018-07-30 13:13:17 -040057
58 static GrTextureType TextureTypeFromTarget(GrGLenum textureTarget);
59
Greg Daniel0fc4d2d2017-10-12 11:23:36 -040060 GrGLTexture(GrGLGpu*, SkBudgeted, const GrSurfaceDesc&, const IDDesc&, GrMipMapsStatus);
bsalomon@google.com5bfc2172011-07-29 20:29:05 +000061
Greg Danielcef213c2017-04-21 11:52:27 -040062 ~GrGLTexture() override {
63 // check that invokeReleaseProc has been called (if needed)
Greg Daniel6a0176b2018-01-30 09:28:44 -050064 SkASSERT(!fReleaseHelper);
Greg Danielcef213c2017-04-21 11:52:27 -040065 }
66
Robert Phillipsb67821d2017-12-13 15:00:45 -050067 GrBackendTexture getBackendTexture() const override;
junov@chromium.org957ebdd2012-06-12 13:58:36 +000068
Brian Salomondc829942018-10-23 16:07:24 -040069 void textureParamsModified() override {
70 fSamplerParams.invalidate();
71 fNonSamplerParams.invalidate();
72 }
reed@google.comac10a2d2010-12-22 21:39:39 +000073
Greg Daniel6a0176b2018-01-30 09:28:44 -050074 void setRelease(sk_sp<GrReleaseProcHelper> releaseHelper) override {
75 fReleaseHelper = std::move(releaseHelper);
Greg Danielcef213c2017-04-21 11:52:27 -040076 }
77
commit-bot@chromium.org59e16e42013-07-17 21:39:58 +000078 // These functions are used to track the texture parameters associated with the texture.
Brian Salomondc829942018-10-23 16:07:24 -040079 GrGpu::ResetTimestamp getCachedParamsTimestamp() const { return fParamsTimestamp; }
80 const SamplerParams& getCachedSamplerParams() const { return fSamplerParams; }
81 const NonSamplerParams& getCachedNonSamplerParams() const { return fNonSamplerParams; }
commit-bot@chromium.org59e16e42013-07-17 21:39:58 +000082
Brian Salomondc829942018-10-23 16:07:24 -040083 void setCachedParams(const SamplerParams* samplerParams,
84 const NonSamplerParams& nonSamplerParams,
85 GrGpu::ResetTimestamp currTimestamp) {
86 if (samplerParams) {
87 fSamplerParams = *samplerParams;
88 }
89 fNonSamplerParams = nonSamplerParams;
90 fParamsTimestamp = currTimestamp;
bsalomon@google.com80d09b92011-11-05 21:21:13 +000091 }
commit-bot@chromium.org59e16e42013-07-17 21:39:58 +000092
Brian Salomon60dd8c72018-07-30 10:24:13 -040093 GrGLuint textureID() const { return fID; }
reed@google.comac10a2d2010-12-22 21:39:39 +000094
Brian Salomon60dd8c72018-07-30 10:24:13 -040095 GrGLenum target() const;
bsalomon10528f12015-10-14 12:54:52 -070096
Brian Salomon9bada542017-06-12 12:09:30 -040097 bool hasBaseLevelBeenBoundToFBO() const { return fBaseLevelHasBeenBoundToFBO; }
98 void baseLevelWasBoundToFBO() { fBaseLevelHasBeenBoundToFBO = true; }
99
Greg Daniel177e6952017-10-12 12:27:11 -0400100 static sk_sp<GrGLTexture> MakeWrapped(GrGLGpu*, const GrSurfaceDesc&, GrMipMapsStatus,
Greg Daniel2268ad22018-11-15 09:27:38 -0500101 const IDDesc&, bool purgeImmediately);
Brian Salomon9bada542017-06-12 12:09:30 -0400102
Eric Karlaf770022018-03-19 13:04:03 -0700103 void dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const override;
104
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000105protected:
kkinnunen2e6055b2016-04-22 01:48:29 -0700106 // Constructor for subclasses.
Greg Daniel0fc4d2d2017-10-12 11:23:36 -0400107 GrGLTexture(GrGLGpu*, const GrSurfaceDesc&, const IDDesc&, GrMipMapsStatus);
kkinnunen2e6055b2016-04-22 01:48:29 -0700108
109 enum Wrapped { kWrapped };
110 // Constructor for instances wrapping backend objects.
Greg Daniel2268ad22018-11-15 09:27:38 -0500111 GrGLTexture(GrGLGpu*, Wrapped, const GrSurfaceDesc&, GrMipMapsStatus, const IDDesc&,
112 bool purgeImmediately);
bsalomon37dd3312014-11-03 08:47:23 -0800113
114 void init(const GrSurfaceDesc&, const IDDesc&);
115
mtklein36352bf2015-03-25 18:17:31 -0700116 void onAbandon() override;
117 void onRelease() override;
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000118
Eric Karl914a36b2017-10-12 12:44:50 -0700119 bool onStealBackendTexture(GrBackendTexture*, SkImage::BackendTextureReleaseProc*) override;
120
reed@google.comac10a2d2010-12-22 21:39:39 +0000121private:
Greg Danielcef213c2017-04-21 11:52:27 -0400122 void invokeReleaseProc() {
Greg Daniel6a0176b2018-01-30 09:28:44 -0500123 if (fReleaseHelper) {
124 // Depending on the ref count of fReleaseHelper this may or may not actually trigger the
125 // ReleaseProc to be called.
126 fReleaseHelper.reset();
Greg Danielcef213c2017-04-21 11:52:27 -0400127 }
128 }
129
Brian Salomondc829942018-10-23 16:07:24 -0400130 SamplerParams fSamplerParams;
131 NonSamplerParams fNonSamplerParams;
132 GrGpu::ResetTimestamp fParamsTimestamp;
133 sk_sp<GrReleaseProcHelper> fReleaseHelper;
134 GrGLuint fID;
135 GrGLenum fFormat;
136 GrBackendObjectOwnership fTextureIDOwnership;
137 bool fBaseLevelHasBeenBoundToFBO = false;
Greg Danielcef213c2017-04-21 11:52:27 -0400138
bsalomonafbf2d62014-09-30 12:18:44 -0700139 typedef GrTexture INHERITED;
reed@google.comac10a2d2010-12-22 21:39:39 +0000140};
141
142#endif