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