1 /*
2  * 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.
6  */
7 
8 
9 #ifndef GrGLTexture_DEFINED
10 #define GrGLTexture_DEFINED
11 
12 #include "GrGpu.h"
13 #include "GrTexture.h"
14 #include "GrGLUtil.h"
15 
16 class GrGLGpu;
17 
18 class GrGLTexture : public GrTexture {
19 public:
20     struct TexParams {
21         GrGLenum fMinFilter;
22         GrGLenum fMagFilter;
23         GrGLenum fWrapS;
24         GrGLenum fWrapT;
25         GrGLenum fMaxMipMapLevel;
26         GrGLenum fSwizzleRGBA[4];
27         GrGLenum fSRGBDecode;
invalidateTexParams28         void invalidate() { memset(this, 0xff, sizeof(TexParams)); }
29     };
30 
31     struct IDDesc {
32         GrGLTextureInfo             fInfo;
33         GrBackendObjectOwnership    fOwnership;
34     };
35     GrGLTexture(GrGLGpu*, SkBudgeted, const GrSurfaceDesc&, const IDDesc&, GrMipMapsStatus);
36 
~GrGLTexture()37     ~GrGLTexture() override {
38         // check that invokeReleaseProc has been called (if needed)
39         SkASSERT(!fReleaseHelper);
40     }
41 
42     GrBackendObject getTextureHandle() const override;
43     GrBackendTexture getBackendTexture() const override;
44 
textureParamsModified()45     void textureParamsModified() override { fTexParams.invalidate(); }
46 
setRelease(sk_sp<GrReleaseProcHelper> releaseHelper)47     void setRelease(sk_sp<GrReleaseProcHelper> releaseHelper) override {
48         fReleaseHelper = std::move(releaseHelper);
49     }
50 
51     // These functions are used to track the texture parameters associated with the texture.
getCachedTexParams(GrGpu::ResetTimestamp * timestamp)52     const TexParams& getCachedTexParams(GrGpu::ResetTimestamp* timestamp) const {
53         *timestamp = fTexParamsTimestamp;
54         return fTexParams;
55     }
56 
setCachedTexParams(const TexParams & texParams,GrGpu::ResetTimestamp timestamp)57     void setCachedTexParams(const TexParams& texParams,
58                             GrGpu::ResetTimestamp timestamp) {
59         fTexParams = texParams;
60         fTexParamsTimestamp = timestamp;
61     }
62 
textureID()63     GrGLuint textureID() const { return fInfo.fID; }
64 
target()65     GrGLenum target() const { return fInfo.fTarget; }
66 
hasBaseLevelBeenBoundToFBO()67     bool hasBaseLevelBeenBoundToFBO() const { return fBaseLevelHasBeenBoundToFBO; }
baseLevelWasBoundToFBO()68     void baseLevelWasBoundToFBO() { fBaseLevelHasBeenBoundToFBO = true; }
69 
70     static sk_sp<GrGLTexture> MakeWrapped(GrGLGpu*, const GrSurfaceDesc&, GrMipMapsStatus,
71                                           const IDDesc&);
72 
73 protected:
74     // Constructor for subclasses.
75     GrGLTexture(GrGLGpu*, const GrSurfaceDesc&, const IDDesc&, GrMipMapsStatus);
76 
77     enum Wrapped { kWrapped };
78     // Constructor for instances wrapping backend objects.
79     GrGLTexture(GrGLGpu*, Wrapped, const GrSurfaceDesc&, GrMipMapsStatus, const IDDesc&);
80 
81     void init(const GrSurfaceDesc&, const IDDesc&);
82 
83     void onAbandon() override;
84     void onRelease() override;
85     void setMemoryBacking(SkTraceMemoryDump* traceMemoryDump,
86                           const SkString& dumpName) const override;
87 
88     bool onStealBackendTexture(GrBackendTexture*, SkImage::BackendTextureReleaseProc*) override;
89 
90 private:
invokeReleaseProc()91     void invokeReleaseProc() {
92         if (fReleaseHelper) {
93             // Depending on the ref count of fReleaseHelper this may or may not actually trigger the
94             // ReleaseProc to be called.
95             fReleaseHelper.reset();
96         }
97     }
98 
99     TexParams                       fTexParams;
100     GrGpu::ResetTimestamp           fTexParamsTimestamp;
101     // Holds the texture target and ID. A pointer to this may be shared to external clients for
102     // direct interaction with the GL object.
103     GrGLTextureInfo                 fInfo;
104     GrBackendObjectOwnership        fTextureIDOwnership;
105     bool                            fBaseLevelHasBeenBoundToFBO = false;
106 
107     sk_sp<GrReleaseProcHelper>      fReleaseHelper;
108 
109     typedef GrTexture INHERITED;
110 };
111 
112 #endif
113