blob: 49002af62a514bacf73c8d095478d5ed6fe4eaaf [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.com64c4fe42011-11-05 14:51:01 +000067 GrPixelConfig fConfig;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +000068 GrGLuint fTextureID;
69 bool fOwnsID;
70 GrGLenum fUploadFormat;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +000071 GrGLenum fUploadType;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +000072 Orientation fOrientation;
73 };
74
bsalomon@google.com5bfc2172011-07-29 20:29:05 +000075 // creates a texture that is also an RT
bsalomon@google.com5877ffd2011-04-11 17:58:48 +000076 GrGLTexture(GrGpuGL* gpu,
bsalomon@google.com5bfc2172011-07-29 20:29:05 +000077 const Desc& textureDesc,
78 const GrGLRenderTarget::Desc& rtDesc,
bsalomon@google.com5877ffd2011-04-11 17:58:48 +000079 const TexParams& initialTexParams);
80
bsalomon@google.com5bfc2172011-07-29 20:29:05 +000081 // creates a non-RT texture
82 GrGLTexture(GrGpuGL* gpu,
83 const Desc& textureDesc,
84 const TexParams& initialTexParams);
85
86
bsalomon@google.com8fe72472011-03-30 21:26:44 +000087 virtual ~GrGLTexture() { this->release(); }
reed@google.comac10a2d2010-12-22 21:39:39 +000088
bsalomon@google.com6dcf4992011-04-05 21:16:14 +000089 // overrides of GrTexture
bsalomon@google.com79d2dbe2011-06-13 19:28:02 +000090 virtual void uploadTextureData(int x,
91 int y,
92 int width,
93 int height,
junov@google.com4ee7ae52011-06-30 17:30:49 +000094 const void* srcData,
95 size_t rowBytes);
bsalomon@google.comcee661a2011-07-26 12:32:36 +000096 virtual intptr_t getTextureHandle() const;
reed@google.comac10a2d2010-12-22 21:39:39 +000097
bsalomon@google.comda96ea02010-12-23 16:53:57 +000098 const TexParams& getTexParams() const { return fTexParams; }
99 void setTexParams(const TexParams& texParams) { fTexParams = texParams; }
twiz@google.com0f31ca72011-03-18 17:38:11 +0000100 GrGLuint textureID() const { return fTexIDObj->id(); }
reed@google.comac10a2d2010-12-22 21:39:39 +0000101
twiz@google.com0f31ca72011-03-18 17:38:11 +0000102 GrGLenum uploadFormat() const { return fUploadFormat; }
twiz@google.com0f31ca72011-03-18 17:38:11 +0000103 GrGLenum uploadType() const { return fUploadType; }
reed@google.comac10a2d2010-12-22 21:39:39 +0000104
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000105 /**
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000106 * @return width() / allocWidth()
107 */
108 GrScalar contentScaleX() const { return fScaleX; }
109
110 /**
111 * @return height() / allocHeight()
112 */
113 GrScalar contentScaleY() const { return fScaleY; }
114
reed@google.comac10a2d2010-12-22 21:39:39 +0000115 // Ganesh assumes texture coordinates have their origin
116 // in the top-left corner of the image. OpenGL, however,
117 // has the origin in the lower-left corner. For content that
118 // is loaded by Ganesh we just push the content "upside down"
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000119 // (by GL's understanding of the world ) in glTex*Image and the
120 // addressing just works out. However, content generated by GL
121 // (FBO or externally imported texture) will be updside down
reed@google.comac10a2d2010-12-22 21:39:39 +0000122 // and it is up to the GrGpuGL derivative to handle y-mirroing.
123 Orientation orientation() const { return fOrientation; }
124
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000125 static const GrGLenum* WrapMode2GLWrap(GrGLBinding binding);
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000126
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000127protected:
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000128
bsalomon@google.com6dcf4992011-04-05 21:16:14 +0000129 // overrides of GrTexture
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000130 virtual void onAbandon();
131 virtual void onRelease();
132
reed@google.comac10a2d2010-12-22 21:39:39 +0000133private:
bsalomon@google.comda96ea02010-12-23 16:53:57 +0000134 TexParams fTexParams;
bsalomon@google.com1da07462011-03-10 14:51:57 +0000135 GrGLTexID* fTexIDObj;
twiz@google.com0f31ca72011-03-18 17:38:11 +0000136 GrGLenum fUploadFormat;
twiz@google.com0f31ca72011-03-18 17:38:11 +0000137 GrGLenum fUploadType;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000138 // precomputed content / alloc ratios
139 GrScalar fScaleX;
140 GrScalar fScaleY;
reed@google.comac10a2d2010-12-22 21:39:39 +0000141 Orientation fOrientation;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000142
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000143 void init(GrGpuGL* gpu,
144 const Desc& textureDesc,
145 const GrGLRenderTarget::Desc* rtDesc,
146 const TexParams& initialTexParams);
147
reed@google.comac10a2d2010-12-22 21:39:39 +0000148 typedef GrTexture INHERITED;
149};
150
151#endif