blob: 5c64eafe29e6fdc00501bc8b51e53fa771ec91e1 [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
bsalomon@google.com1da07462011-03-10 14:51:57 +000016/**
17 * A ref counted tex id that deletes the texture in its destructor.
18 */
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +000019class GrGLTexID : public SkRefCnt {
bsalomon@google.com1da07462011-03-10 14:51:57 +000020public:
reed@google.comfa35e3d2012-06-26 20:16:17 +000021 SK_DECLARE_INST_COUNT(GrGLTexID)
22
bsalomon@google.com72830222013-01-23 20:25:22 +000023 GrGLTexID(const GrGLInterface* gl, GrGLuint texID, bool isWrapped)
bsalomon@google.com0b77d682011-08-19 13:28:54 +000024 : fGL(gl)
25 , fTexID(texID)
bsalomon@google.com72830222013-01-23 20:25:22 +000026 , fIsWrapped(isWrapped) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +000027 }
bsalomon@google.com1da07462011-03-10 14:51:57 +000028
29 virtual ~GrGLTexID() {
bsalomon@google.com72830222013-01-23 20:25:22 +000030 if (0 != fTexID && !fIsWrapped) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +000031 GR_GL_CALL(fGL, DeleteTextures(1, &fTexID));
bsalomon@google.com1da07462011-03-10 14:51:57 +000032 }
33 }
34
35 void abandon() { fTexID = 0; }
twiz@google.com0f31ca72011-03-18 17:38:11 +000036 GrGLuint id() const { return fTexID; }
bsalomon@google.com1da07462011-03-10 14:51:57 +000037
38private:
bsalomon@google.com0b77d682011-08-19 13:28:54 +000039 const GrGLInterface* fGL;
40 GrGLuint fTexID;
bsalomon@google.com72830222013-01-23 20:25:22 +000041 bool fIsWrapped;
reed@google.comfa35e3d2012-06-26 20:16:17 +000042
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +000043 typedef SkRefCnt INHERITED;
bsalomon@google.com1da07462011-03-10 14:51:57 +000044};
45
bsalomon@google.com5877ffd2011-04-11 17:58:48 +000046////////////////////////////////////////////////////////////////////////////////
47
bsalomon@google.com5877ffd2011-04-11 17:58:48 +000048
bsalomonafbf2d62014-09-30 12:18:44 -070049class GrGLTexture : public GrTexture {
bsalomon@google.com8fe72472011-03-30 21:26:44 +000050
reed@google.comac10a2d2010-12-22 21:39:39 +000051public:
bsalomon@google.comda96ea02010-12-23 16:53:57 +000052 struct TexParams {
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +000053 GrGLenum fMinFilter;
54 GrGLenum fMagFilter;
twiz@google.com0f31ca72011-03-18 17:38:11 +000055 GrGLenum fWrapS;
56 GrGLenum fWrapT;
bsalomon@google.com0a97be22011-11-08 19:20:57 +000057 GrGLenum fSwizzleRGBA[4];
bsalomon@google.com5877ffd2011-04-11 17:58:48 +000058 void invalidate() { memset(this, 0xff, sizeof(TexParams)); }
bsalomon@google.comda96ea02010-12-23 16:53:57 +000059 };
reed@google.comac10a2d2010-12-22 21:39:39 +000060
bsalomonb15b4c12014-10-29 12:41:57 -070061 struct IDDesc {
bsalomon@google.com5877ffd2011-04-11 17:58:48 +000062 GrGLuint fTextureID;
bsalomon@google.com72830222013-01-23 20:25:22 +000063 bool fIsWrapped;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +000064 };
65
bsalomon37dd3312014-11-03 08:47:23 -080066 GrGLTexture(GrGpuGL*, const GrSurfaceDesc&, const IDDesc&);
bsalomon@google.com5bfc2172011-07-29 20:29:05 +000067
bsalomon@google.com8fe72472011-03-30 21:26:44 +000068 virtual ~GrGLTexture() { this->release(); }
reed@google.comac10a2d2010-12-22 21:39:39 +000069
bsalomon@google.com08afc842012-10-25 18:56:10 +000070 virtual GrBackendObject getTextureHandle() const SK_OVERRIDE;
junov@chromium.org957ebdd2012-06-12 13:58:36 +000071
commit-bot@chromium.orge49157f2014-05-09 20:46:48 +000072 virtual void textureParamsModified() SK_OVERRIDE { fTexParams.invalidate(); }
reed@google.comac10a2d2010-12-22 21:39:39 +000073
commit-bot@chromium.org59e16e42013-07-17 21:39:58 +000074 // These functions are used to track the texture parameters associated with the texture.
bsalomon@google.com80d09b92011-11-05 21:21:13 +000075 const TexParams& getCachedTexParams(GrGpu::ResetTimestamp* timestamp) const {
76 *timestamp = fTexParamsTimestamp;
77 return fTexParams;
78 }
commit-bot@chromium.org59e16e42013-07-17 21:39:58 +000079
bsalomon@google.com80d09b92011-11-05 21:21:13 +000080 void setCachedTexParams(const TexParams& texParams,
81 GrGpu::ResetTimestamp timestamp) {
82 fTexParams = texParams;
83 fTexParamsTimestamp = timestamp;
84 }
commit-bot@chromium.org59e16e42013-07-17 21:39:58 +000085
bsalomon49f085d2014-09-05 13:34:00 -070086 GrGLuint textureID() const { return (fTexIDObj.get()) ? fTexIDObj->id() : 0; }
reed@google.comac10a2d2010-12-22 21:39:39 +000087
bsalomon@google.com5877ffd2011-04-11 17:58:48 +000088protected:
bsalomon37dd3312014-11-03 08:47:23 -080089 // The public constructor registers this object with the cache. However, only the most derived
90 // class should register with the cache. This constructor does not do the registration and
91 // rather moves that burden onto the derived class.
92 enum Derived { kDerived };
93 GrGLTexture(GrGpuGL*, const GrSurfaceDesc&, const IDDesc&, Derived);
94
95 void init(const GrSurfaceDesc&, const IDDesc&);
96
junov@chromium.org957ebdd2012-06-12 13:58:36 +000097 virtual void onAbandon() SK_OVERRIDE;
98 virtual void onRelease() SK_OVERRIDE;
bsalomon@google.com8fe72472011-03-30 21:26:44 +000099
reed@google.comac10a2d2010-12-22 21:39:39 +0000100private:
bsalomon@google.com80d09b92011-11-05 21:21:13 +0000101 TexParams fTexParams;
102 GrGpu::ResetTimestamp fTexParamsTimestamp;
commit-bot@chromium.org59e16e42013-07-17 21:39:58 +0000103 SkAutoTUnref<GrGLTexID> fTexIDObj;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000104
bsalomonafbf2d62014-09-30 12:18:44 -0700105 typedef GrTexture INHERITED;
reed@google.comac10a2d2010-12-22 21:39:39 +0000106};
107
108#endif