reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1 | /* |
bsalomon@google.com | 1da0746 | 2011-03-10 14:51:57 +0000 | [diff] [blame^] | 2 | Copyright 2011 Google Inc. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 3 | |
| 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | you may not use this file except in compliance with the License. |
| 6 | You may obtain a copy of the License at |
| 7 | |
| 8 | http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | Unless required by applicable law or agreed to in writing, software |
| 11 | distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | See the License for the specific language governing permissions and |
| 14 | limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | |
| 18 | #ifndef GrGLTexture_DEFINED |
| 19 | #define GrGLTexture_DEFINED |
| 20 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 21 | #include "GrTexture.h" |
bsalomon@google.com | 8895a7a | 2011-02-18 16:09:55 +0000 | [diff] [blame] | 22 | #include "GrScalar.h" |
| 23 | #include "GrGLIRect.h" |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 24 | |
| 25 | class GrGpuGL; |
| 26 | class GrGLTexture; |
| 27 | |
bsalomon@google.com | 1da0746 | 2011-03-10 14:51:57 +0000 | [diff] [blame^] | 28 | /** |
| 29 | * A ref counted tex id that deletes the texture in its destructor. |
| 30 | */ |
| 31 | class GrGLTexID : public GrRefCnt { |
| 32 | public: |
| 33 | GrGLTexID(GLuint texID) : fTexID(texID) {} |
| 34 | |
| 35 | virtual ~GrGLTexID() { |
| 36 | if (0 != fTexID) { |
| 37 | GR_GL(DeleteTextures(1, &fTexID)); |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | void abandon() { fTexID = 0; } |
| 42 | GLuint id() const { return fTexID; } |
| 43 | |
| 44 | private: |
| 45 | GLuint fTexID; |
| 46 | }; |
| 47 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 48 | class GrGLRenderTarget : public GrRenderTarget { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 49 | public: |
| 50 | virtual ~GrGLRenderTarget(); |
| 51 | |
| 52 | bool resolveable() const { return fRTFBOID != fTexFBOID; } |
| 53 | bool needsResolve() const { return fNeedsResolve; } |
| 54 | void setDirty(bool dirty) { fNeedsResolve = resolveable() && dirty; } |
| 55 | |
| 56 | GLuint renderFBOID() const { return fRTFBOID; } |
| 57 | GLuint textureFBOID() const { return fTexFBOID; } |
| 58 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 59 | void abandon(); |
| 60 | |
bsalomon@google.com | 8895a7a | 2011-02-18 16:09:55 +0000 | [diff] [blame] | 61 | protected: |
| 62 | |
| 63 | struct GLRenderTargetIDs { |
| 64 | GLuint fRTFBOID; |
| 65 | GLuint fTexFBOID; |
| 66 | GLuint fStencilRenderbufferID; |
| 67 | GLuint fMSColorRenderbufferID; |
| 68 | bool fOwnIDs; |
| 69 | }; |
| 70 | |
| 71 | GrGLRenderTarget(const GLRenderTargetIDs& ids, |
bsalomon@google.com | 1da0746 | 2011-03-10 14:51:57 +0000 | [diff] [blame^] | 72 | GrGLTexID* texID, |
bsalomon@google.com | 8895a7a | 2011-02-18 16:09:55 +0000 | [diff] [blame] | 73 | GLuint stencilBits, |
| 74 | const GrGLIRect& fViewport, |
| 75 | GrGLTexture* texture, |
| 76 | GrGpuGL* gl); |
| 77 | |
| 78 | void setViewport(const GrGLIRect& rect) { fViewport = rect; } |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 79 | const GrGLIRect& getViewport() const { return fViewport; } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 80 | private: |
| 81 | GrGpuGL* fGL; |
| 82 | GLuint fRTFBOID; |
bsalomon@google.com | 1da0746 | 2011-03-10 14:51:57 +0000 | [diff] [blame^] | 83 | GLuint fTexFBOID; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 84 | GLuint fStencilRenderbufferID; |
| 85 | GLuint fMSColorRenderbufferID; |
| 86 | |
| 87 | // Should this object delete IDs when it is destroyed or does someone |
| 88 | // else own them. |
| 89 | bool fOwnIDs; |
| 90 | |
| 91 | // If there separate Texture and RenderTarget FBO IDs then the rendertarget |
| 92 | // must be resolved to the texture FBO before it is used as a texture. |
| 93 | bool fNeedsResolve; |
| 94 | |
| 95 | // when we switch to this rendertarget we want to set the viewport to |
| 96 | // only render to to content area (as opposed to the whole allocation) and |
| 97 | // we want the rendering to be at top left (GL has origin in bottom left) |
bsalomon@google.com | 8895a7a | 2011-02-18 16:09:55 +0000 | [diff] [blame] | 98 | GrGLIRect fViewport; |
bsalomon@google.com | 1da0746 | 2011-03-10 14:51:57 +0000 | [diff] [blame^] | 99 | |
| 100 | // non-NULL if this RT was created by Gr with an associated GrGLTexture. |
| 101 | GrGLTexID* fTexIDObj; |
| 102 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 103 | friend class GrGpuGL; |
| 104 | friend class GrGLTexture; |
| 105 | |
| 106 | typedef GrRenderTarget INHERITED; |
| 107 | }; |
| 108 | |
| 109 | class GrGLTexture : public GrTexture { |
| 110 | public: |
| 111 | enum Orientation { |
| 112 | kBottomUp_Orientation, |
| 113 | kTopDown_Orientation, |
| 114 | }; |
bsalomon@google.com | da96ea0 | 2010-12-23 16:53:57 +0000 | [diff] [blame] | 115 | |
| 116 | struct TexParams { |
| 117 | GLenum fFilter; |
| 118 | GLenum fWrapS; |
| 119 | GLenum fWrapT; |
| 120 | }; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 121 | |
| 122 | protected: |
| 123 | struct GLTextureDesc { |
| 124 | uint32_t fContentWidth; |
| 125 | uint32_t fContentHeight; |
| 126 | uint32_t fAllocWidth; |
bsalomon@google.com | da96ea0 | 2010-12-23 16:53:57 +0000 | [diff] [blame] | 127 | uint32_t fAllocHeight; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 128 | PixelConfig fFormat; |
| 129 | GLuint fTextureID; |
| 130 | GLenum fUploadFormat; |
| 131 | GLenum fUploadByteCount; |
| 132 | GLenum fUploadType; |
bsalomon@google.com | 8895a7a | 2011-02-18 16:09:55 +0000 | [diff] [blame] | 133 | GLuint fStencilBits; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 134 | Orientation fOrientation; |
| 135 | }; |
| 136 | typedef GrGLRenderTarget::GLRenderTargetIDs GLRenderTargetIDs; |
| 137 | GrGLTexture(const GLTextureDesc& textureDesc, |
| 138 | const GLRenderTargetIDs& rtIDs, |
bsalomon@google.com | da96ea0 | 2010-12-23 16:53:57 +0000 | [diff] [blame] | 139 | const TexParams& initialTexParams, |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 140 | GrGpuGL* gl); |
| 141 | |
| 142 | public: |
| 143 | virtual ~GrGLTexture(); |
| 144 | |
| 145 | // overloads of GrTexture |
| 146 | virtual void abandon(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 147 | virtual GrRenderTarget* asRenderTarget(); |
bsalomon@google.com | 1da0746 | 2011-03-10 14:51:57 +0000 | [diff] [blame^] | 148 | virtual void releaseRenderTarget(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 149 | virtual void uploadTextureData(uint32_t x, |
| 150 | uint32_t y, |
| 151 | uint32_t width, |
| 152 | uint32_t height, |
| 153 | const void* srcData); |
| 154 | virtual intptr_t getTextureHandle(); |
| 155 | |
bsalomon@google.com | da96ea0 | 2010-12-23 16:53:57 +0000 | [diff] [blame] | 156 | const TexParams& getTexParams() const { return fTexParams; } |
| 157 | void setTexParams(const TexParams& texParams) { fTexParams = texParams; } |
bsalomon@google.com | 1da0746 | 2011-03-10 14:51:57 +0000 | [diff] [blame^] | 158 | GLuint textureID() const { return fTexIDObj->id(); } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 159 | |
| 160 | GLenum uploadFormat() const { return fUploadFormat; } |
| 161 | GLenum uploadByteCount() const { return fUploadByteCount; } |
| 162 | GLenum uploadType() const { return fUploadType; } |
| 163 | |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 164 | /** |
| 165 | * Retrieves the texture width actually allocated in texels. |
| 166 | * |
| 167 | * @return the width in texels |
| 168 | */ |
| 169 | int allocWidth() const { return fAllocWidth; } |
| 170 | |
| 171 | /** |
| 172 | * Retrieves the texture height actually allocated in texels. |
| 173 | * |
| 174 | * @return the height in texels |
| 175 | */ |
| 176 | int allocHeight() const { return fAllocHeight; } |
| 177 | |
| 178 | /** |
| 179 | * @return width() / allocWidth() |
| 180 | */ |
| 181 | GrScalar contentScaleX() const { return fScaleX; } |
| 182 | |
| 183 | /** |
| 184 | * @return height() / allocHeight() |
| 185 | */ |
| 186 | GrScalar contentScaleY() const { return fScaleY; } |
| 187 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 188 | // Ganesh assumes texture coordinates have their origin |
| 189 | // in the top-left corner of the image. OpenGL, however, |
| 190 | // has the origin in the lower-left corner. For content that |
| 191 | // is loaded by Ganesh we just push the content "upside down" |
| 192 | // (by GL's understanding of the world ) in glTex*Image and the |
| 193 | // addressing just works out. However, content generated by GL |
| 194 | // (FBO or externally imported texture) will be updside down |
| 195 | // and it is up to the GrGpuGL derivative to handle y-mirroing. |
| 196 | Orientation orientation() const { return fOrientation; } |
| 197 | |
| 198 | private: |
bsalomon@google.com | da96ea0 | 2010-12-23 16:53:57 +0000 | [diff] [blame] | 199 | TexParams fTexParams; |
bsalomon@google.com | 1da0746 | 2011-03-10 14:51:57 +0000 | [diff] [blame^] | 200 | GrGLTexID* fTexIDObj; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 201 | GLenum fUploadFormat; |
| 202 | GLenum fUploadByteCount; |
| 203 | GLenum fUploadType; |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 204 | int fAllocWidth; |
| 205 | int fAllocHeight; |
| 206 | // precomputed content / alloc ratios |
| 207 | GrScalar fScaleX; |
| 208 | GrScalar fScaleY; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 209 | Orientation fOrientation; |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 210 | GrGLRenderTarget* fRenderTarget; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 211 | GrGpuGL* fGpuGL; |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 212 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 213 | static const GLenum gWrapMode2GLWrap[]; |
| 214 | |
| 215 | friend class GrGpuGL; |
| 216 | |
| 217 | typedef GrTexture INHERITED; |
| 218 | }; |
| 219 | |
| 220 | #endif |