blob: 025faded72a0a1213c433785c43e6ed4d7fca38c [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"
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000013#include "GrGLRenderTarget.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000014
bsalomon@google.com1da07462011-03-10 14:51:57 +000015/**
16 * A ref counted tex id that deletes the texture in its destructor.
17 */
18class GrGLTexID : public GrRefCnt {
19public:
reed@google.comfa35e3d2012-06-26 20:16:17 +000020 SK_DECLARE_INST_COUNT(GrGLTexID)
21
bsalomon@google.com72830222013-01-23 20:25:22 +000022 GrGLTexID(const GrGLInterface* gl, GrGLuint texID, bool isWrapped)
bsalomon@google.com0b77d682011-08-19 13:28:54 +000023 : fGL(gl)
24 , fTexID(texID)
bsalomon@google.com72830222013-01-23 20:25:22 +000025 , fIsWrapped(isWrapped) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +000026 }
bsalomon@google.com1da07462011-03-10 14:51:57 +000027
28 virtual ~GrGLTexID() {
bsalomon@google.com72830222013-01-23 20:25:22 +000029 if (0 != fTexID && !fIsWrapped) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +000030 GR_GL_CALL(fGL, DeleteTextures(1, &fTexID));
bsalomon@google.com1da07462011-03-10 14:51:57 +000031 }
32 }
33
34 void abandon() { fTexID = 0; }
twiz@google.com0f31ca72011-03-18 17:38:11 +000035 GrGLuint id() const { return fTexID; }
bsalomon@google.com1da07462011-03-10 14:51:57 +000036
37private:
bsalomon@google.com0b77d682011-08-19 13:28:54 +000038 const GrGLInterface* fGL;
39 GrGLuint fTexID;
bsalomon@google.com72830222013-01-23 20:25:22 +000040 bool fIsWrapped;
reed@google.comfa35e3d2012-06-26 20:16:17 +000041
42 typedef GrRefCnt INHERITED;
bsalomon@google.com1da07462011-03-10 14:51:57 +000043};
44
bsalomon@google.com5877ffd2011-04-11 17:58:48 +000045////////////////////////////////////////////////////////////////////////////////
46
bsalomon@google.com5877ffd2011-04-11 17:58:48 +000047
reed@google.comac10a2d2010-12-22 21:39:39 +000048class GrGLTexture : public GrTexture {
bsalomon@google.com8fe72472011-03-30 21:26:44 +000049
reed@google.comac10a2d2010-12-22 21:39:39 +000050public:
bsalomon@google.comda96ea02010-12-23 16:53:57 +000051 struct TexParams {
twiz@google.com0f31ca72011-03-18 17:38:11 +000052 GrGLenum fFilter;
53 GrGLenum fWrapS;
54 GrGLenum fWrapT;
bsalomon@google.com0a97be22011-11-08 19:20:57 +000055 GrGLenum fSwizzleRGBA[4];
bsalomon@google.com5877ffd2011-04-11 17:58:48 +000056 void invalidate() { memset(this, 0xff, sizeof(TexParams)); }
bsalomon@google.comda96ea02010-12-23 16:53:57 +000057 };
reed@google.comac10a2d2010-12-22 21:39:39 +000058
robertphillips@google.com32716282012-06-04 12:48:45 +000059 struct Desc : public GrTextureDesc {
bsalomon@google.com5877ffd2011-04-11 17:58:48 +000060 GrGLuint fTextureID;
bsalomon@google.com72830222013-01-23 20:25:22 +000061 bool fIsWrapped;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +000062 };
63
bsalomon@google.com5bfc2172011-07-29 20:29:05 +000064 // creates a texture that is also an RT
bsalomon@google.com5877ffd2011-04-11 17:58:48 +000065 GrGLTexture(GrGpuGL* gpu,
bsalomon@google.com5bfc2172011-07-29 20:29:05 +000066 const Desc& textureDesc,
bsalomon@google.com80d09b92011-11-05 21:21:13 +000067 const GrGLRenderTarget::Desc& rtDesc);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +000068
bsalomon@google.com5bfc2172011-07-29 20:29:05 +000069 // creates a non-RT texture
70 GrGLTexture(GrGpuGL* gpu,
bsalomon@google.com80d09b92011-11-05 21:21:13 +000071 const Desc& textureDesc);
bsalomon@google.com5bfc2172011-07-29 20:29:05 +000072
bsalomon@google.com8fe72472011-03-30 21:26:44 +000073 virtual ~GrGLTexture() { this->release(); }
reed@google.comac10a2d2010-12-22 21:39:39 +000074
bsalomon@google.com08afc842012-10-25 18:56:10 +000075 virtual GrBackendObject getTextureHandle() const SK_OVERRIDE;
junov@chromium.org957ebdd2012-06-12 13:58:36 +000076
77 virtual void invalidateCachedState() SK_OVERRIDE { fTexParams.invalidate(); }
reed@google.comac10a2d2010-12-22 21:39:39 +000078
commit-bot@chromium.org59e16e42013-07-17 21:39:58 +000079 // These functions are used to track the texture parameters associated with the texture.
bsalomon@google.com80d09b92011-11-05 21:21:13 +000080 const TexParams& getCachedTexParams(GrGpu::ResetTimestamp* timestamp) const {
81 *timestamp = fTexParamsTimestamp;
82 return fTexParams;
83 }
commit-bot@chromium.org59e16e42013-07-17 21:39:58 +000084
bsalomon@google.com80d09b92011-11-05 21:21:13 +000085 void setCachedTexParams(const TexParams& texParams,
86 GrGpu::ResetTimestamp timestamp) {
87 fTexParams = texParams;
88 fTexParamsTimestamp = timestamp;
89 }
commit-bot@chromium.org59e16e42013-07-17 21:39:58 +000090
91 GrGLuint textureID() const { return (NULL != fTexIDObj.get()) ? fTexIDObj->id() : 0; }
reed@google.comac10a2d2010-12-22 21:39:39 +000092
bsalomon@google.com5877ffd2011-04-11 17:58:48 +000093protected:
bsalomon@google.com6dcf4992011-04-05 21:16:14 +000094 // overrides of GrTexture
junov@chromium.org957ebdd2012-06-12 13:58:36 +000095 virtual void onAbandon() SK_OVERRIDE;
96 virtual void onRelease() SK_OVERRIDE;
bsalomon@google.com8fe72472011-03-30 21:26:44 +000097
reed@google.comac10a2d2010-12-22 21:39:39 +000098private:
bsalomon@google.com80d09b92011-11-05 21:21:13 +000099 TexParams fTexParams;
100 GrGpu::ResetTimestamp fTexParamsTimestamp;
commit-bot@chromium.org59e16e42013-07-17 21:39:58 +0000101 SkAutoTUnref<GrGLTexID> fTexIDObj;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000102
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000103 void init(GrGpuGL* gpu,
104 const Desc& textureDesc,
bsalomon@google.com80d09b92011-11-05 21:21:13 +0000105 const GrGLRenderTarget::Desc* rtDesc);
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000106
reed@google.comac10a2d2010-12-22 21:39:39 +0000107 typedef GrTexture INHERITED;
108};
109
110#endif