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