GrGLTexture.h revision cef213c97ea9992efacd028fe304a18b438f8147
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
16class GrGLGpu;
17
18class GrGLTexture : public GrTexture {
19public:
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;
28        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&);
36    GrGLTexture(GrGLGpu*, SkBudgeted, const GrSurfaceDesc&, const IDDesc&,
37                bool wasMipMapDataProvided);
38
39    ~GrGLTexture() override {
40        // check that invokeReleaseProc has been called (if needed)
41        SkASSERT(!fReleaseProc);
42    }
43
44    GrBackendObject getTextureHandle() const override;
45
46    void textureParamsModified() override { fTexParams.invalidate(); }
47
48    void setRelease(ReleaseProc proc, ReleaseCtx ctx) override {
49        fReleaseProc = proc;
50        fReleaseCtx = ctx;
51    }
52
53    // These functions are used to track the texture parameters associated with the texture.
54    const TexParams& getCachedTexParams(GrGpu::ResetTimestamp* timestamp) const {
55        *timestamp = fTexParamsTimestamp;
56        return fTexParams;
57    }
58
59    void setCachedTexParams(const TexParams& texParams,
60                            GrGpu::ResetTimestamp timestamp) {
61        fTexParams = texParams;
62        fTexParamsTimestamp = timestamp;
63    }
64
65    GrGLuint textureID() const { return fInfo.fID; }
66
67    GrGLenum target() const { return fInfo.fTarget; }
68
69    static sk_sp<GrGLTexture> MakeWrapped(GrGLGpu*, const GrSurfaceDesc&, const IDDesc&);
70protected:
71    // Constructor for subclasses.
72    GrGLTexture(GrGLGpu*, const GrSurfaceDesc&, const IDDesc&, bool wasMipMapDataProvided);
73
74    enum Wrapped { kWrapped };
75    // Constructor for instances wrapping backend objects.
76    GrGLTexture(GrGLGpu*, Wrapped, const GrSurfaceDesc&, const IDDesc&);
77
78    void init(const GrSurfaceDesc&, const IDDesc&);
79
80    void onAbandon() override;
81    void onRelease() override;
82    void setMemoryBacking(SkTraceMemoryDump* traceMemoryDump,
83                          const SkString& dumpName) const override;
84    std::unique_ptr<GrExternalTextureData> detachBackendTexture() override;
85
86private:
87    void invokeReleaseProc() {
88        if (fReleaseProc) {
89            fReleaseProc(fReleaseCtx);
90            fReleaseProc = nullptr;
91        }
92    }
93
94    TexParams                       fTexParams;
95    GrGpu::ResetTimestamp           fTexParamsTimestamp;
96    // Holds the texture target and ID. A pointer to this may be shared to external clients for
97    // direct interaction with the GL object.
98    GrGLTextureInfo                 fInfo;
99    GrBackendObjectOwnership        fTextureIDOwnership;
100
101    ReleaseProc                     fReleaseProc = nullptr;
102    ReleaseCtx                      fReleaseCtx = nullptr;
103
104    typedef GrTexture INHERITED;
105};
106
107#endif
108