blob: b0dc368adf81f1560729de54c571aaa2a4a9b9c0 [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.comaa5b6732011-07-29 15:13:20 +000013#include "GrGLRenderTarget.h"
bsalomon@google.com8895a7a2011-02-18 16:09:55 +000014#include "GrScalar.h"
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000015#include "GrTexture.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000016
bsalomon@google.com1da07462011-03-10 14:51:57 +000017/**
18 * A ref counted tex id that deletes the texture in its destructor.
19 */
20class GrGLTexID : public GrRefCnt {
bsalomon@google.com8fe72472011-03-30 21:26:44 +000021
bsalomon@google.com1da07462011-03-10 14:51:57 +000022public:
bsalomon@google.com0b77d682011-08-19 13:28:54 +000023 GrGLTexID(const GrGLInterface* gl, GrGLuint texID, bool ownsID)
24 : fGL(gl)
25 , fTexID(texID)
26 , fOwnsID(ownsID) {
27 }
bsalomon@google.com1da07462011-03-10 14:51:57 +000028
29 virtual ~GrGLTexID() {
bsalomon@google.com5877ffd2011-04-11 17:58:48 +000030 if (0 != fTexID && fOwnsID) {
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;
41 bool fOwnsID;
bsalomon@google.com1da07462011-03-10 14:51:57 +000042};
43
bsalomon@google.com5877ffd2011-04-11 17:58:48 +000044////////////////////////////////////////////////////////////////////////////////
45
bsalomon@google.com5877ffd2011-04-11 17:58:48 +000046
reed@google.comac10a2d2010-12-22 21:39:39 +000047class GrGLTexture : public GrTexture {
bsalomon@google.com8fe72472011-03-30 21:26:44 +000048
reed@google.comac10a2d2010-12-22 21:39:39 +000049public:
50 enum Orientation {
51 kBottomUp_Orientation,
52 kTopDown_Orientation,
53 };
bsalomon@google.com8fe72472011-03-30 21:26:44 +000054
bsalomon@google.comda96ea02010-12-23 16:53:57 +000055 struct TexParams {
twiz@google.com0f31ca72011-03-18 17:38:11 +000056 GrGLenum fFilter;
57 GrGLenum fWrapS;
58 GrGLenum fWrapT;
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.com79d2dbe2011-06-13 19:28:02 +000063 int fContentWidth;
64 int fContentHeight;
65 int fAllocWidth;
66 int fAllocHeight;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +000067 GrPixelConfig fFormat;
68 GrGLuint fTextureID;
69 bool fOwnsID;
70 GrGLenum fUploadFormat;
71 GrGLenum fUploadByteCount;
72 GrGLenum fUploadType;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +000073 Orientation fOrientation;
74 };
75
bsalomon@google.com5bfc2172011-07-29 20:29:05 +000076 // creates a texture that is also an RT
bsalomon@google.com5877ffd2011-04-11 17:58:48 +000077 GrGLTexture(GrGpuGL* gpu,
bsalomon@google.com5bfc2172011-07-29 20:29:05 +000078 const Desc& textureDesc,
79 const GrGLRenderTarget::Desc& rtDesc,
bsalomon@google.com5877ffd2011-04-11 17:58:48 +000080 const TexParams& initialTexParams);
81
bsalomon@google.com5bfc2172011-07-29 20:29:05 +000082 // creates a non-RT texture
83 GrGLTexture(GrGpuGL* gpu,
84 const Desc& textureDesc,
85 const TexParams& initialTexParams);
86
87
bsalomon@google.com8fe72472011-03-30 21:26:44 +000088 virtual ~GrGLTexture() { this->release(); }
reed@google.comac10a2d2010-12-22 21:39:39 +000089
bsalomon@google.com6dcf4992011-04-05 21:16:14 +000090 // overrides of GrTexture
bsalomon@google.com79d2dbe2011-06-13 19:28:02 +000091 virtual void uploadTextureData(int x,
92 int y,
93 int width,
94 int height,
junov@google.com4ee7ae52011-06-30 17:30:49 +000095 const void* srcData,
96 size_t rowBytes);
bsalomon@google.comcee661a2011-07-26 12:32:36 +000097 virtual intptr_t getTextureHandle() const;
reed@google.comac10a2d2010-12-22 21:39:39 +000098
bsalomon@google.comda96ea02010-12-23 16:53:57 +000099 const TexParams& getTexParams() const { return fTexParams; }
100 void setTexParams(const TexParams& texParams) { fTexParams = texParams; }
twiz@google.com0f31ca72011-03-18 17:38:11 +0000101 GrGLuint textureID() const { return fTexIDObj->id(); }
reed@google.comac10a2d2010-12-22 21:39:39 +0000102
twiz@google.com0f31ca72011-03-18 17:38:11 +0000103 GrGLenum uploadFormat() const { return fUploadFormat; }
104 GrGLenum uploadByteCount() const { return fUploadByteCount; }
105 GrGLenum uploadType() const { return fUploadType; }
reed@google.comac10a2d2010-12-22 21:39:39 +0000106
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000107 /**
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000108 * @return width() / allocWidth()
109 */
110 GrScalar contentScaleX() const { return fScaleX; }
111
112 /**
113 * @return height() / allocHeight()
114 */
115 GrScalar contentScaleY() const { return fScaleY; }
116
reed@google.comac10a2d2010-12-22 21:39:39 +0000117 // Ganesh assumes texture coordinates have their origin
118 // in the top-left corner of the image. OpenGL, however,
119 // has the origin in the lower-left corner. For content that
120 // is loaded by Ganesh we just push the content "upside down"
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000121 // (by GL's understanding of the world ) in glTex*Image and the
122 // addressing just works out. However, content generated by GL
123 // (FBO or externally imported texture) will be updside down
reed@google.comac10a2d2010-12-22 21:39:39 +0000124 // and it is up to the GrGpuGL derivative to handle y-mirroing.
125 Orientation orientation() const { return fOrientation; }
126
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000127 static const GrGLenum* WrapMode2GLWrap(GrGLBinding binding);
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000128
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000129protected:
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000130
bsalomon@google.com6dcf4992011-04-05 21:16:14 +0000131 // overrides of GrTexture
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000132 virtual void onAbandon();
133 virtual void onRelease();
134
reed@google.comac10a2d2010-12-22 21:39:39 +0000135private:
bsalomon@google.comda96ea02010-12-23 16:53:57 +0000136 TexParams fTexParams;
bsalomon@google.com1da07462011-03-10 14:51:57 +0000137 GrGLTexID* fTexIDObj;
twiz@google.com0f31ca72011-03-18 17:38:11 +0000138 GrGLenum fUploadFormat;
139 GrGLenum fUploadByteCount;
140 GrGLenum fUploadType;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000141 // precomputed content / alloc ratios
142 GrScalar fScaleX;
143 GrScalar fScaleY;
reed@google.comac10a2d2010-12-22 21:39:39 +0000144 Orientation fOrientation;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000145
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000146 void init(GrGpuGL* gpu,
147 const Desc& textureDesc,
148 const GrGLRenderTarget::Desc* rtDesc,
149 const TexParams& initialTexParams);
150
reed@google.comac10a2d2010-12-22 21:39:39 +0000151 typedef GrTexture INHERITED;
152};
153
154#endif