blob: cedac5a63ba959273f9b659b49e881b64f6a1730 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@google.comac10a2d2010-12-22 21:39:39 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
reed@google.comac10a2d2010-12-22 21:39:39 +00007 */
8
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
reed@google.comac10a2d2010-12-22 21:39:39 +000010#ifndef GrGLTexture_DEFINED
11#define GrGLTexture_DEFINED
12
bsalomon@google.com80d09b92011-11-05 21:21:13 +000013#include "GrGpu.h"
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000014#include "GrGLRenderTarget.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 */
19class GrGLTexID : public GrRefCnt {
bsalomon@google.com8fe72472011-03-30 21:26:44 +000020
bsalomon@google.com1da07462011-03-10 14:51:57 +000021public:
bsalomon@google.com0b77d682011-08-19 13:28:54 +000022 GrGLTexID(const GrGLInterface* gl, GrGLuint texID, bool ownsID)
23 : fGL(gl)
24 , fTexID(texID)
25 , fOwnsID(ownsID) {
26 }
bsalomon@google.com1da07462011-03-10 14:51:57 +000027
28 virtual ~GrGLTexID() {
bsalomon@google.com5877ffd2011-04-11 17:58:48 +000029 if (0 != fTexID && fOwnsID) {
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;
40 bool fOwnsID;
bsalomon@google.com1da07462011-03-10 14:51:57 +000041};
42
bsalomon@google.com5877ffd2011-04-11 17:58:48 +000043////////////////////////////////////////////////////////////////////////////////
44
bsalomon@google.com5877ffd2011-04-11 17:58:48 +000045
reed@google.comac10a2d2010-12-22 21:39:39 +000046class GrGLTexture : public GrTexture {
bsalomon@google.com8fe72472011-03-30 21:26:44 +000047
reed@google.comac10a2d2010-12-22 21:39:39 +000048public:
49 enum Orientation {
50 kBottomUp_Orientation,
51 kTopDown_Orientation,
52 };
bsalomon@google.com8fe72472011-03-30 21:26:44 +000053
bsalomon@google.comda96ea02010-12-23 16:53:57 +000054 struct TexParams {
twiz@google.com0f31ca72011-03-18 17:38:11 +000055 GrGLenum fFilter;
56 GrGLenum fWrapS;
57 GrGLenum fWrapT;
bsalomon@google.com0a97be22011-11-08 19:20:57 +000058 GrGLenum fSwizzleRGBA[4];
bsalomon@google.com5877ffd2011-04-11 17:58:48 +000059 void invalidate() { memset(this, 0xff, sizeof(TexParams)); }
bsalomon@google.comda96ea02010-12-23 16:53:57 +000060 };
reed@google.comac10a2d2010-12-22 21:39:39 +000061
bsalomon@google.com5bfc2172011-07-29 20:29:05 +000062 struct Desc {
bsalomon@google.com99621082011-11-15 16:47:16 +000063 int fWidth;
64 int fHeight;
bsalomon@google.com64c4fe42011-11-05 14:51:01 +000065 GrPixelConfig fConfig;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +000066 GrGLuint fTextureID;
67 bool fOwnsID;
68 GrGLenum fUploadFormat;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +000069 GrGLenum fUploadType;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +000070 Orientation fOrientation;
71 };
72
bsalomon@google.com5bfc2172011-07-29 20:29:05 +000073 // creates a texture that is also an RT
bsalomon@google.com5877ffd2011-04-11 17:58:48 +000074 GrGLTexture(GrGpuGL* gpu,
bsalomon@google.com5bfc2172011-07-29 20:29:05 +000075 const Desc& textureDesc,
bsalomon@google.com80d09b92011-11-05 21:21:13 +000076 const GrGLRenderTarget::Desc& rtDesc);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +000077
bsalomon@google.com5bfc2172011-07-29 20:29:05 +000078 // creates a non-RT texture
79 GrGLTexture(GrGpuGL* gpu,
bsalomon@google.com80d09b92011-11-05 21:21:13 +000080 const Desc& textureDesc);
bsalomon@google.com5bfc2172011-07-29 20:29:05 +000081
82
bsalomon@google.com8fe72472011-03-30 21:26:44 +000083 virtual ~GrGLTexture() { this->release(); }
reed@google.comac10a2d2010-12-22 21:39:39 +000084
bsalomon@google.com6dcf4992011-04-05 21:16:14 +000085 // overrides of GrTexture
bsalomon@google.com79d2dbe2011-06-13 19:28:02 +000086 virtual void uploadTextureData(int x,
87 int y,
88 int width,
89 int height,
junov@google.com4ee7ae52011-06-30 17:30:49 +000090 const void* srcData,
91 size_t rowBytes);
bsalomon@google.comcee661a2011-07-26 12:32:36 +000092 virtual intptr_t getTextureHandle() const;
reed@google.comac10a2d2010-12-22 21:39:39 +000093
bsalomon@google.com80d09b92011-11-05 21:21:13 +000094 // these functions
95 const TexParams& getCachedTexParams(GrGpu::ResetTimestamp* timestamp) const {
96 *timestamp = fTexParamsTimestamp;
97 return fTexParams;
98 }
99 void setCachedTexParams(const TexParams& texParams,
100 GrGpu::ResetTimestamp timestamp) {
101 fTexParams = texParams;
102 fTexParamsTimestamp = timestamp;
103 }
twiz@google.com0f31ca72011-03-18 17:38:11 +0000104 GrGLuint textureID() const { return fTexIDObj->id(); }
reed@google.comac10a2d2010-12-22 21:39:39 +0000105
twiz@google.com0f31ca72011-03-18 17:38:11 +0000106 GrGLenum uploadFormat() const { return fUploadFormat; }
twiz@google.com0f31ca72011-03-18 17:38:11 +0000107 GrGLenum uploadType() const { return fUploadType; }
reed@google.comac10a2d2010-12-22 21:39:39 +0000108
109 // Ganesh assumes texture coordinates have their origin
110 // in the top-left corner of the image. OpenGL, however,
111 // has the origin in the lower-left corner. For content that
112 // is loaded by Ganesh we just push the content "upside down"
bsalomon@google.com80d09b92011-11-05 21:21:13 +0000113 // (by GL's understanding of the world) in glTex*Image and the
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000114 // addressing just works out. However, content generated by GL
115 // (FBO or externally imported texture) will be updside down
reed@google.comac10a2d2010-12-22 21:39:39 +0000116 // and it is up to the GrGpuGL derivative to handle y-mirroing.
117 Orientation orientation() const { return fOrientation; }
118
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000119 static const GrGLenum* WrapMode2GLWrap();
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000120
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000121protected:
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000122
bsalomon@google.com6dcf4992011-04-05 21:16:14 +0000123 // overrides of GrTexture
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000124 virtual void onAbandon();
125 virtual void onRelease();
126
reed@google.comac10a2d2010-12-22 21:39:39 +0000127private:
bsalomon@google.com80d09b92011-11-05 21:21:13 +0000128 TexParams fTexParams;
129 GrGpu::ResetTimestamp fTexParamsTimestamp;
130 GrGLTexID* fTexIDObj;
131 GrGLenum fUploadFormat;
132 GrGLenum fUploadType;
bsalomon@google.com80d09b92011-11-05 21:21:13 +0000133 Orientation fOrientation;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000134
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000135 void init(GrGpuGL* gpu,
136 const Desc& textureDesc,
bsalomon@google.com80d09b92011-11-05 21:21:13 +0000137 const GrGLRenderTarget::Desc* rtDesc);
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000138
reed@google.comac10a2d2010-12-22 21:39:39 +0000139 typedef GrTexture INHERITED;
140};
141
142#endif