blob: 527bab0e84aa43b3e0932bb73c625a85740149ae [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.com2d0bade2012-10-26 19:01:17 +000062 Origin fOrigin;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +000063 };
64
bsalomon@google.com5bfc2172011-07-29 20:29:05 +000065 // creates a texture that is also an RT
bsalomon@google.com5877ffd2011-04-11 17:58:48 +000066 GrGLTexture(GrGpuGL* gpu,
bsalomon@google.com5bfc2172011-07-29 20:29:05 +000067 const Desc& textureDesc,
bsalomon@google.com80d09b92011-11-05 21:21:13 +000068 const GrGLRenderTarget::Desc& rtDesc);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +000069
bsalomon@google.com5bfc2172011-07-29 20:29:05 +000070 // creates a non-RT texture
71 GrGLTexture(GrGpuGL* gpu,
bsalomon@google.com80d09b92011-11-05 21:21:13 +000072 const Desc& textureDesc);
bsalomon@google.com5bfc2172011-07-29 20:29:05 +000073
74
bsalomon@google.com8fe72472011-03-30 21:26:44 +000075 virtual ~GrGLTexture() { this->release(); }
reed@google.comac10a2d2010-12-22 21:39:39 +000076
bsalomon@google.com08afc842012-10-25 18:56:10 +000077 virtual GrBackendObject getTextureHandle() const SK_OVERRIDE;
junov@chromium.org957ebdd2012-06-12 13:58:36 +000078
79 virtual void invalidateCachedState() SK_OVERRIDE { fTexParams.invalidate(); }
reed@google.comac10a2d2010-12-22 21:39:39 +000080
rmistry@google.comd6176b02012-08-23 18:14:13 +000081 // these functions
bsalomon@google.com80d09b92011-11-05 21:21:13 +000082 const TexParams& getCachedTexParams(GrGpu::ResetTimestamp* timestamp) const {
83 *timestamp = fTexParamsTimestamp;
84 return fTexParams;
85 }
86 void setCachedTexParams(const TexParams& texParams,
87 GrGpu::ResetTimestamp timestamp) {
88 fTexParams = texParams;
89 fTexParamsTimestamp = timestamp;
90 }
twiz@google.com0f31ca72011-03-18 17:38:11 +000091 GrGLuint textureID() const { return fTexIDObj->id(); }
reed@google.comac10a2d2010-12-22 21:39:39 +000092
bsalomon@google.com5877ffd2011-04-11 17:58:48 +000093protected:
bsalomon@google.com8fe72472011-03-30 21:26:44 +000094
bsalomon@google.com6dcf4992011-04-05 21:16:14 +000095 // overrides of GrTexture
junov@chromium.org957ebdd2012-06-12 13:58:36 +000096 virtual void onAbandon() SK_OVERRIDE;
97 virtual void onRelease() SK_OVERRIDE;
bsalomon@google.com8fe72472011-03-30 21:26:44 +000098
reed@google.comac10a2d2010-12-22 21:39:39 +000099private:
bsalomon@google.com80d09b92011-11-05 21:21:13 +0000100 TexParams fTexParams;
101 GrGpu::ResetTimestamp fTexParamsTimestamp;
102 GrGLTexID* fTexIDObj;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000103
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000104 void init(GrGpuGL* gpu,
105 const Desc& textureDesc,
bsalomon@google.com80d09b92011-11-05 21:21:13 +0000106 const GrGLRenderTarget::Desc* rtDesc);
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000107
reed@google.comac10a2d2010-12-22 21:39:39 +0000108 typedef GrTexture INHERITED;
109};
110
111#endif