blob: 7566a58cf49a43fde369d4b03f0965bbfc482849 [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.com5877ffd2011-04-11 17:58:48 +000023 GrGLTexID(GrGLuint texID, bool ownsID) : fTexID(texID), fOwnsID(ownsID) {}
bsalomon@google.com1da07462011-03-10 14:51:57 +000024
25 virtual ~GrGLTexID() {
bsalomon@google.com5877ffd2011-04-11 17:58:48 +000026 if (0 != fTexID && fOwnsID) {
bsalomon@google.com1da07462011-03-10 14:51:57 +000027 GR_GL(DeleteTextures(1, &fTexID));
28 }
29 }
30
31 void abandon() { fTexID = 0; }
twiz@google.com0f31ca72011-03-18 17:38:11 +000032 GrGLuint id() const { return fTexID; }
bsalomon@google.com1da07462011-03-10 14:51:57 +000033
34private:
twiz@google.com0f31ca72011-03-18 17:38:11 +000035 GrGLuint fTexID;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +000036 bool fOwnsID;
bsalomon@google.com1da07462011-03-10 14:51:57 +000037};
38
bsalomon@google.com5877ffd2011-04-11 17:58:48 +000039////////////////////////////////////////////////////////////////////////////////
40
bsalomon@google.com5877ffd2011-04-11 17:58:48 +000041
reed@google.comac10a2d2010-12-22 21:39:39 +000042class GrGLTexture : public GrTexture {
bsalomon@google.com8fe72472011-03-30 21:26:44 +000043
reed@google.comac10a2d2010-12-22 21:39:39 +000044public:
45 enum Orientation {
46 kBottomUp_Orientation,
47 kTopDown_Orientation,
48 };
bsalomon@google.com8fe72472011-03-30 21:26:44 +000049
bsalomon@google.comda96ea02010-12-23 16:53:57 +000050 struct TexParams {
twiz@google.com0f31ca72011-03-18 17:38:11 +000051 GrGLenum fFilter;
52 GrGLenum fWrapS;
53 GrGLenum fWrapT;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +000054 void invalidate() { memset(this, 0xff, sizeof(TexParams)); }
bsalomon@google.comda96ea02010-12-23 16:53:57 +000055 };
reed@google.comac10a2d2010-12-22 21:39:39 +000056
bsalomon@google.com5877ffd2011-04-11 17:58:48 +000057 struct GLTextureDesc {
bsalomon@google.com79d2dbe2011-06-13 19:28:02 +000058 int fContentWidth;
59 int fContentHeight;
60 int fAllocWidth;
61 int fAllocHeight;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +000062 GrPixelConfig fFormat;
63 GrGLuint fTextureID;
64 bool fOwnsID;
65 GrGLenum fUploadFormat;
66 GrGLenum fUploadByteCount;
67 GrGLenum fUploadType;
68 GrGLuint fStencilBits;
69 Orientation fOrientation;
70 };
71
72 typedef GrGLRenderTarget::GLRenderTargetIDs GLRenderTargetIDs;
73
74 GrGLTexture(GrGpuGL* gpu,
75 const GLTextureDesc& textureDesc,
76 const GLRenderTargetIDs& rtIDs,
77 const TexParams& initialTexParams);
78
bsalomon@google.com8fe72472011-03-30 21:26:44 +000079 virtual ~GrGLTexture() { this->release(); }
reed@google.comac10a2d2010-12-22 21:39:39 +000080
bsalomon@google.com6dcf4992011-04-05 21:16:14 +000081 // overrides of GrTexture
bsalomon@google.com79d2dbe2011-06-13 19:28:02 +000082 virtual void uploadTextureData(int x,
83 int y,
84 int width,
85 int height,
junov@google.com4ee7ae52011-06-30 17:30:49 +000086 const void* srcData,
87 size_t rowBytes);
bsalomon@google.comcee661a2011-07-26 12:32:36 +000088 virtual intptr_t getTextureHandle() const;
reed@google.comac10a2d2010-12-22 21:39:39 +000089
bsalomon@google.comda96ea02010-12-23 16:53:57 +000090 const TexParams& getTexParams() const { return fTexParams; }
91 void setTexParams(const TexParams& texParams) { fTexParams = texParams; }
twiz@google.com0f31ca72011-03-18 17:38:11 +000092 GrGLuint textureID() const { return fTexIDObj->id(); }
reed@google.comac10a2d2010-12-22 21:39:39 +000093
twiz@google.com0f31ca72011-03-18 17:38:11 +000094 GrGLenum uploadFormat() const { return fUploadFormat; }
95 GrGLenum uploadByteCount() const { return fUploadByteCount; }
96 GrGLenum uploadType() const { return fUploadType; }
reed@google.comac10a2d2010-12-22 21:39:39 +000097
bsalomon@google.comc6cf7232011-02-17 16:43:10 +000098 /**
99 * Retrieves the texture width actually allocated in texels.
100 *
101 * @return the width in texels
102 */
103 int allocWidth() const { return fAllocWidth; }
104
105 /**
106 * Retrieves the texture height actually allocated in texels.
107 *
108 * @return the height in texels
109 */
110 int allocHeight() const { return fAllocHeight; }
111
112 /**
113 * @return width() / allocWidth()
114 */
115 GrScalar contentScaleX() const { return fScaleX; }
116
117 /**
118 * @return height() / allocHeight()
119 */
120 GrScalar contentScaleY() const { return fScaleY; }
121
reed@google.comac10a2d2010-12-22 21:39:39 +0000122 // Ganesh assumes texture coordinates have their origin
123 // in the top-left corner of the image. OpenGL, however,
124 // has the origin in the lower-left corner. For content that
125 // is loaded by Ganesh we just push the content "upside down"
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000126 // (by GL's understanding of the world ) in glTex*Image and the
127 // addressing just works out. However, content generated by GL
128 // (FBO or externally imported texture) will be updside down
reed@google.comac10a2d2010-12-22 21:39:39 +0000129 // and it is up to the GrGpuGL derivative to handle y-mirroing.
130 Orientation orientation() const { return fOrientation; }
131
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000132 static const GrGLenum* WrapMode2GLWrap();
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000133
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000134protected:
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000135
bsalomon@google.com6dcf4992011-04-05 21:16:14 +0000136 // overrides of GrTexture
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000137 virtual void onAbandon();
138 virtual void onRelease();
139
reed@google.comac10a2d2010-12-22 21:39:39 +0000140private:
bsalomon@google.comda96ea02010-12-23 16:53:57 +0000141 TexParams fTexParams;
bsalomon@google.com1da07462011-03-10 14:51:57 +0000142 GrGLTexID* fTexIDObj;
twiz@google.com0f31ca72011-03-18 17:38:11 +0000143 GrGLenum fUploadFormat;
144 GrGLenum fUploadByteCount;
145 GrGLenum fUploadType;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000146 int fAllocWidth;
147 int fAllocHeight;
148 // precomputed content / alloc ratios
149 GrScalar fScaleX;
150 GrScalar fScaleY;
reed@google.comac10a2d2010-12-22 21:39:39 +0000151 Orientation fOrientation;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000152
reed@google.comac10a2d2010-12-22 21:39:39 +0000153 typedef GrTexture INHERITED;
154};
155
156#endif