reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1 | /* |
| 2 | Copyright 2010 Google Inc. |
| 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 | |
| 28 | class GrGLRenderTarget : public GrRenderTarget { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 29 | public: |
| 30 | virtual ~GrGLRenderTarget(); |
| 31 | |
| 32 | bool resolveable() const { return fRTFBOID != fTexFBOID; } |
| 33 | bool needsResolve() const { return fNeedsResolve; } |
| 34 | void setDirty(bool dirty) { fNeedsResolve = resolveable() && dirty; } |
| 35 | |
| 36 | GLuint renderFBOID() const { return fRTFBOID; } |
| 37 | GLuint textureFBOID() const { return fTexFBOID; } |
| 38 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 39 | void abandon(); |
| 40 | |
bsalomon@google.com | 8895a7a | 2011-02-18 16:09:55 +0000 | [diff] [blame] | 41 | protected: |
| 42 | |
| 43 | struct GLRenderTargetIDs { |
| 44 | GLuint fRTFBOID; |
| 45 | GLuint fTexFBOID; |
| 46 | GLuint fStencilRenderbufferID; |
| 47 | GLuint fMSColorRenderbufferID; |
| 48 | bool fOwnIDs; |
| 49 | }; |
| 50 | |
| 51 | GrGLRenderTarget(const GLRenderTargetIDs& ids, |
| 52 | GLuint stencilBits, |
| 53 | const GrGLIRect& fViewport, |
| 54 | GrGLTexture* texture, |
| 55 | GrGpuGL* gl); |
| 56 | |
| 57 | void setViewport(const GrGLIRect& rect) { fViewport = rect; } |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 58 | const GrGLIRect& getViewport() const { return fViewport; } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 59 | private: |
| 60 | GrGpuGL* fGL; |
| 61 | GLuint fRTFBOID; |
| 62 | GLuint fTexFBOID; |
| 63 | GLuint fStencilRenderbufferID; |
| 64 | GLuint fMSColorRenderbufferID; |
| 65 | |
| 66 | // Should this object delete IDs when it is destroyed or does someone |
| 67 | // else own them. |
| 68 | bool fOwnIDs; |
| 69 | |
| 70 | // If there separate Texture and RenderTarget FBO IDs then the rendertarget |
| 71 | // must be resolved to the texture FBO before it is used as a texture. |
| 72 | bool fNeedsResolve; |
| 73 | |
| 74 | // when we switch to this rendertarget we want to set the viewport to |
| 75 | // only render to to content area (as opposed to the whole allocation) and |
| 76 | // 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] | 77 | GrGLIRect fViewport; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 78 | |
| 79 | friend class GrGpuGL; |
| 80 | friend class GrGLTexture; |
| 81 | |
| 82 | typedef GrRenderTarget INHERITED; |
| 83 | }; |
| 84 | |
| 85 | class GrGLTexture : public GrTexture { |
| 86 | public: |
| 87 | enum Orientation { |
| 88 | kBottomUp_Orientation, |
| 89 | kTopDown_Orientation, |
| 90 | }; |
bsalomon@google.com | da96ea0 | 2010-12-23 16:53:57 +0000 | [diff] [blame] | 91 | |
| 92 | struct TexParams { |
| 93 | GLenum fFilter; |
| 94 | GLenum fWrapS; |
| 95 | GLenum fWrapT; |
| 96 | }; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 97 | |
| 98 | protected: |
| 99 | struct GLTextureDesc { |
| 100 | uint32_t fContentWidth; |
| 101 | uint32_t fContentHeight; |
| 102 | uint32_t fAllocWidth; |
bsalomon@google.com | da96ea0 | 2010-12-23 16:53:57 +0000 | [diff] [blame] | 103 | uint32_t fAllocHeight; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 104 | PixelConfig fFormat; |
| 105 | GLuint fTextureID; |
| 106 | GLenum fUploadFormat; |
| 107 | GLenum fUploadByteCount; |
| 108 | GLenum fUploadType; |
bsalomon@google.com | 8895a7a | 2011-02-18 16:09:55 +0000 | [diff] [blame] | 109 | GLuint fStencilBits; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 110 | Orientation fOrientation; |
| 111 | }; |
| 112 | typedef GrGLRenderTarget::GLRenderTargetIDs GLRenderTargetIDs; |
| 113 | GrGLTexture(const GLTextureDesc& textureDesc, |
| 114 | const GLRenderTargetIDs& rtIDs, |
bsalomon@google.com | da96ea0 | 2010-12-23 16:53:57 +0000 | [diff] [blame] | 115 | const TexParams& initialTexParams, |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 116 | GrGpuGL* gl); |
| 117 | |
| 118 | public: |
| 119 | virtual ~GrGLTexture(); |
| 120 | |
| 121 | // overloads of GrTexture |
| 122 | virtual void abandon(); |
| 123 | virtual bool isRenderTarget() const; |
| 124 | virtual GrRenderTarget* asRenderTarget(); |
| 125 | virtual void removeRenderTarget(); |
| 126 | virtual void uploadTextureData(uint32_t x, |
| 127 | uint32_t y, |
| 128 | uint32_t width, |
| 129 | uint32_t height, |
| 130 | const void* srcData); |
| 131 | virtual intptr_t getTextureHandle(); |
| 132 | |
bsalomon@google.com | da96ea0 | 2010-12-23 16:53:57 +0000 | [diff] [blame] | 133 | const TexParams& getTexParams() const { return fTexParams; } |
| 134 | void setTexParams(const TexParams& texParams) { fTexParams = texParams; } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 135 | GLuint textureID() const { return fTextureID; } |
| 136 | |
| 137 | GLenum uploadFormat() const { return fUploadFormat; } |
| 138 | GLenum uploadByteCount() const { return fUploadByteCount; } |
| 139 | GLenum uploadType() const { return fUploadType; } |
| 140 | |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 141 | /** |
| 142 | * Retrieves the texture width actually allocated in texels. |
| 143 | * |
| 144 | * @return the width in texels |
| 145 | */ |
| 146 | int allocWidth() const { return fAllocWidth; } |
| 147 | |
| 148 | /** |
| 149 | * Retrieves the texture height actually allocated in texels. |
| 150 | * |
| 151 | * @return the height in texels |
| 152 | */ |
| 153 | int allocHeight() const { return fAllocHeight; } |
| 154 | |
| 155 | /** |
| 156 | * @return width() / allocWidth() |
| 157 | */ |
| 158 | GrScalar contentScaleX() const { return fScaleX; } |
| 159 | |
| 160 | /** |
| 161 | * @return height() / allocHeight() |
| 162 | */ |
| 163 | GrScalar contentScaleY() const { return fScaleY; } |
| 164 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 165 | // Ganesh assumes texture coordinates have their origin |
| 166 | // in the top-left corner of the image. OpenGL, however, |
| 167 | // has the origin in the lower-left corner. For content that |
| 168 | // is loaded by Ganesh we just push the content "upside down" |
| 169 | // (by GL's understanding of the world ) in glTex*Image and the |
| 170 | // addressing just works out. However, content generated by GL |
| 171 | // (FBO or externally imported texture) will be updside down |
| 172 | // and it is up to the GrGpuGL derivative to handle y-mirroing. |
| 173 | Orientation orientation() const { return fOrientation; } |
| 174 | |
| 175 | private: |
bsalomon@google.com | da96ea0 | 2010-12-23 16:53:57 +0000 | [diff] [blame] | 176 | TexParams fTexParams; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 177 | GLuint fTextureID; |
| 178 | GLenum fUploadFormat; |
| 179 | GLenum fUploadByteCount; |
| 180 | GLenum fUploadType; |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 181 | int fAllocWidth; |
| 182 | int fAllocHeight; |
| 183 | // precomputed content / alloc ratios |
| 184 | GrScalar fScaleX; |
| 185 | GrScalar fScaleY; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 186 | Orientation fOrientation; |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 187 | GrGLRenderTarget* fRenderTarget; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 188 | GrGpuGL* fGpuGL; |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 189 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 190 | static const GLenum gWrapMode2GLWrap[]; |
| 191 | |
| 192 | friend class GrGpuGL; |
| 193 | |
| 194 | typedef GrTexture INHERITED; |
| 195 | }; |
| 196 | |
| 197 | #endif |