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 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 17 | #ifndef GrGLTexture_DEFINED |
| 18 | #define GrGLTexture_DEFINED |
| 19 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 20 | #include "GrTexture.h" |
bsalomon@google.com | 8895a7a | 2011-02-18 16:09:55 +0000 | [diff] [blame] | 21 | #include "GrScalar.h" |
| 22 | #include "GrGLIRect.h" |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 23 | |
| 24 | class GrGpuGL; |
| 25 | class GrGLTexture; |
| 26 | |
bsalomon@google.com | 1da0746 | 2011-03-10 14:51:57 +0000 | [diff] [blame] | 27 | /** |
| 28 | * A ref counted tex id that deletes the texture in its destructor. |
| 29 | */ |
| 30 | class GrGLTexID : public GrRefCnt { |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 31 | |
bsalomon@google.com | 1da0746 | 2011-03-10 14:51:57 +0000 | [diff] [blame] | 32 | public: |
bsalomon@google.com | 5877ffd | 2011-04-11 17:58:48 +0000 | [diff] [blame] | 33 | GrGLTexID(GrGLuint texID, bool ownsID) : fTexID(texID), fOwnsID(ownsID) {} |
bsalomon@google.com | 1da0746 | 2011-03-10 14:51:57 +0000 | [diff] [blame] | 34 | |
| 35 | virtual ~GrGLTexID() { |
bsalomon@google.com | 5877ffd | 2011-04-11 17:58:48 +0000 | [diff] [blame] | 36 | if (0 != fTexID && fOwnsID) { |
bsalomon@google.com | 1da0746 | 2011-03-10 14:51:57 +0000 | [diff] [blame] | 37 | GR_GL(DeleteTextures(1, &fTexID)); |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | void abandon() { fTexID = 0; } |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 42 | GrGLuint id() const { return fTexID; } |
bsalomon@google.com | 1da0746 | 2011-03-10 14:51:57 +0000 | [diff] [blame] | 43 | |
| 44 | private: |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 45 | GrGLuint fTexID; |
bsalomon@google.com | 5877ffd | 2011-04-11 17:58:48 +0000 | [diff] [blame] | 46 | bool fOwnsID; |
bsalomon@google.com | 1da0746 | 2011-03-10 14:51:57 +0000 | [diff] [blame] | 47 | }; |
| 48 | |
bsalomon@google.com | 5877ffd | 2011-04-11 17:58:48 +0000 | [diff] [blame] | 49 | //////////////////////////////////////////////////////////////////////////////// |
| 50 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 51 | class GrGLRenderTarget : public GrRenderTarget { |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 52 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 53 | public: |
bsalomon@google.com | 5877ffd | 2011-04-11 17:58:48 +0000 | [diff] [blame] | 54 | // set fTexFBOID to this value to indicate that it is multisampled but |
| 55 | // Gr doesn't know how to resolve it. |
| 56 | enum { kUnresolvableFBOID = 0 }; |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 57 | |
bsalomon@google.com | 8895a7a | 2011-02-18 16:09:55 +0000 | [diff] [blame] | 58 | struct GLRenderTargetIDs { |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 59 | GrGLuint fRTFBOID; |
| 60 | GrGLuint fTexFBOID; |
| 61 | GrGLuint fStencilRenderbufferID; |
| 62 | GrGLuint fMSColorRenderbufferID; |
bsalomon@google.com | f954d8d | 2011-04-06 17:50:02 +0000 | [diff] [blame] | 63 | bool fOwnIDs; |
bsalomon@google.com | 5877ffd | 2011-04-11 17:58:48 +0000 | [diff] [blame] | 64 | void reset() { memset(this, 0, sizeof(GLRenderTargetIDs)); } |
bsalomon@google.com | 8895a7a | 2011-02-18 16:09:55 +0000 | [diff] [blame] | 65 | }; |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 66 | |
| 67 | GrGLRenderTarget(GrGpuGL* gpu, |
| 68 | const GLRenderTargetIDs& ids, |
bsalomon@google.com | 1da0746 | 2011-03-10 14:51:57 +0000 | [diff] [blame] | 69 | GrGLTexID* texID, |
bsalomon@google.com | cee661a | 2011-07-26 12:32:36 +0000 | [diff] [blame] | 70 | GrPixelConfig config, |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 71 | GrGLuint stencilBits, |
bsalomon@google.com | f954d8d | 2011-04-06 17:50:02 +0000 | [diff] [blame] | 72 | bool isMultisampled, |
bsalomon@google.com | 8895a7a | 2011-02-18 16:09:55 +0000 | [diff] [blame] | 73 | const GrGLIRect& fViewport, |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 74 | GrGLTexture* texture); |
| 75 | |
bsalomon@google.com | 5877ffd | 2011-04-11 17:58:48 +0000 | [diff] [blame] | 76 | virtual ~GrGLRenderTarget() { this->release(); } |
| 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 | |
bsalomon@google.com | 5877ffd | 2011-04-11 17:58:48 +0000 | [diff] [blame] | 81 | // The following two functions return the same ID when a |
| 82 | // texture-rendertarget is multisampled, and different IDs when |
| 83 | // it is. |
| 84 | // FBO ID used to render into |
| 85 | GrGLuint renderFBOID() const { return fRTFBOID; } |
| 86 | // FBO ID that has texture ID attached. |
| 87 | GrGLuint textureFBOID() const { return fTexFBOID; } |
| 88 | |
| 89 | // override of GrRenderTarget |
bsalomon@google.com | 0b96a84 | 2011-07-13 21:53:49 +0000 | [diff] [blame] | 90 | virtual intptr_t getRenderTargetHandle() const { |
| 91 | return this->renderFBOID(); |
| 92 | } |
| 93 | virtual intptr_t getRenderTargetResolvedHandle() const { |
| 94 | return this->textureFBOID(); |
| 95 | } |
bsalomon@google.com | 5877ffd | 2011-04-11 17:58:48 +0000 | [diff] [blame] | 96 | virtual ResolveType getResolveType() const { |
| 97 | if (fRTFBOID == fTexFBOID) { |
| 98 | // catches FBO 0 and non MSAA case |
| 99 | return kAutoResolves_ResolveType; |
| 100 | } else if (kUnresolvableFBOID == fTexFBOID) { |
| 101 | return kCantResolve_ResolveType; |
| 102 | } else { |
| 103 | return kCanResolve_ResolveType; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | protected: |
| 108 | // override of GrResource |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 109 | virtual void onAbandon(); |
| 110 | virtual void onRelease(); |
| 111 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 112 | private: |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 113 | GrGLuint fRTFBOID; |
| 114 | GrGLuint fTexFBOID; |
| 115 | GrGLuint fStencilRenderbufferID; |
| 116 | GrGLuint fMSColorRenderbufferID; |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 117 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 118 | // Should this object delete IDs when it is destroyed or does someone |
| 119 | // else own them. |
| 120 | bool fOwnIDs; |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 121 | |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 122 | // 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] | 123 | // 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] | 124 | // 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] | 125 | GrGLIRect fViewport; |
bsalomon@google.com | 1da0746 | 2011-03-10 14:51:57 +0000 | [diff] [blame] | 126 | |
| 127 | // non-NULL if this RT was created by Gr with an associated GrGLTexture. |
| 128 | GrGLTexID* fTexIDObj; |
| 129 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 130 | typedef GrRenderTarget INHERITED; |
| 131 | }; |
| 132 | |
bsalomon@google.com | 5877ffd | 2011-04-11 17:58:48 +0000 | [diff] [blame] | 133 | //////////////////////////////////////////////////////////////////////////////// |
| 134 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 135 | class GrGLTexture : public GrTexture { |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 136 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 137 | public: |
| 138 | enum Orientation { |
| 139 | kBottomUp_Orientation, |
| 140 | kTopDown_Orientation, |
| 141 | }; |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 142 | |
bsalomon@google.com | da96ea0 | 2010-12-23 16:53:57 +0000 | [diff] [blame] | 143 | struct TexParams { |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 144 | GrGLenum fFilter; |
| 145 | GrGLenum fWrapS; |
| 146 | GrGLenum fWrapT; |
bsalomon@google.com | 5877ffd | 2011-04-11 17:58:48 +0000 | [diff] [blame] | 147 | void invalidate() { memset(this, 0xff, sizeof(TexParams)); } |
bsalomon@google.com | da96ea0 | 2010-12-23 16:53:57 +0000 | [diff] [blame] | 148 | }; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 149 | |
bsalomon@google.com | 5877ffd | 2011-04-11 17:58:48 +0000 | [diff] [blame] | 150 | struct GLTextureDesc { |
bsalomon@google.com | 79d2dbe | 2011-06-13 19:28:02 +0000 | [diff] [blame] | 151 | int fContentWidth; |
| 152 | int fContentHeight; |
| 153 | int fAllocWidth; |
| 154 | int fAllocHeight; |
bsalomon@google.com | 5877ffd | 2011-04-11 17:58:48 +0000 | [diff] [blame] | 155 | GrPixelConfig fFormat; |
| 156 | GrGLuint fTextureID; |
| 157 | bool fOwnsID; |
| 158 | GrGLenum fUploadFormat; |
| 159 | GrGLenum fUploadByteCount; |
| 160 | GrGLenum fUploadType; |
| 161 | GrGLuint fStencilBits; |
| 162 | Orientation fOrientation; |
| 163 | }; |
| 164 | |
| 165 | typedef GrGLRenderTarget::GLRenderTargetIDs GLRenderTargetIDs; |
| 166 | |
| 167 | GrGLTexture(GrGpuGL* gpu, |
| 168 | const GLTextureDesc& textureDesc, |
| 169 | const GLRenderTargetIDs& rtIDs, |
| 170 | const TexParams& initialTexParams); |
| 171 | |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 172 | virtual ~GrGLTexture() { this->release(); } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 173 | |
bsalomon@google.com | 6dcf499 | 2011-04-05 21:16:14 +0000 | [diff] [blame] | 174 | // overrides of GrTexture |
bsalomon@google.com | 79d2dbe | 2011-06-13 19:28:02 +0000 | [diff] [blame] | 175 | virtual void uploadTextureData(int x, |
| 176 | int y, |
| 177 | int width, |
| 178 | int height, |
junov@google.com | 4ee7ae5 | 2011-06-30 17:30:49 +0000 | [diff] [blame] | 179 | const void* srcData, |
| 180 | size_t rowBytes); |
bsalomon@google.com | cee661a | 2011-07-26 12:32:36 +0000 | [diff] [blame] | 181 | virtual intptr_t getTextureHandle() const; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 182 | |
bsalomon@google.com | da96ea0 | 2010-12-23 16:53:57 +0000 | [diff] [blame] | 183 | const TexParams& getTexParams() const { return fTexParams; } |
| 184 | void setTexParams(const TexParams& texParams) { fTexParams = texParams; } |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 185 | GrGLuint textureID() const { return fTexIDObj->id(); } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 186 | |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 187 | GrGLenum uploadFormat() const { return fUploadFormat; } |
| 188 | GrGLenum uploadByteCount() const { return fUploadByteCount; } |
| 189 | GrGLenum uploadType() const { return fUploadType; } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 190 | |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 191 | /** |
| 192 | * Retrieves the texture width actually allocated in texels. |
| 193 | * |
| 194 | * @return the width in texels |
| 195 | */ |
| 196 | int allocWidth() const { return fAllocWidth; } |
| 197 | |
| 198 | /** |
| 199 | * Retrieves the texture height actually allocated in texels. |
| 200 | * |
| 201 | * @return the height in texels |
| 202 | */ |
| 203 | int allocHeight() const { return fAllocHeight; } |
| 204 | |
| 205 | /** |
| 206 | * @return width() / allocWidth() |
| 207 | */ |
| 208 | GrScalar contentScaleX() const { return fScaleX; } |
| 209 | |
| 210 | /** |
| 211 | * @return height() / allocHeight() |
| 212 | */ |
| 213 | GrScalar contentScaleY() const { return fScaleY; } |
| 214 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 215 | // Ganesh assumes texture coordinates have their origin |
| 216 | // in the top-left corner of the image. OpenGL, however, |
| 217 | // has the origin in the lower-left corner. For content that |
| 218 | // is loaded by Ganesh we just push the content "upside down" |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 219 | // (by GL's understanding of the world ) in glTex*Image and the |
| 220 | // addressing just works out. However, content generated by GL |
| 221 | // (FBO or externally imported texture) will be updside down |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 222 | // and it is up to the GrGpuGL derivative to handle y-mirroing. |
| 223 | Orientation orientation() const { return fOrientation; } |
| 224 | |
bsalomon@google.com | 5877ffd | 2011-04-11 17:58:48 +0000 | [diff] [blame] | 225 | static const GrGLenum* WrapMode2GLWrap(); |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 226 | |
bsalomon@google.com | 5877ffd | 2011-04-11 17:58:48 +0000 | [diff] [blame] | 227 | protected: |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 228 | |
bsalomon@google.com | 6dcf499 | 2011-04-05 21:16:14 +0000 | [diff] [blame] | 229 | // overrides of GrTexture |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 230 | virtual void onAbandon(); |
| 231 | virtual void onRelease(); |
| 232 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 233 | private: |
bsalomon@google.com | da96ea0 | 2010-12-23 16:53:57 +0000 | [diff] [blame] | 234 | TexParams fTexParams; |
bsalomon@google.com | 1da0746 | 2011-03-10 14:51:57 +0000 | [diff] [blame] | 235 | GrGLTexID* fTexIDObj; |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 236 | GrGLenum fUploadFormat; |
| 237 | GrGLenum fUploadByteCount; |
| 238 | GrGLenum fUploadType; |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 239 | int fAllocWidth; |
| 240 | int fAllocHeight; |
| 241 | // precomputed content / alloc ratios |
| 242 | GrScalar fScaleX; |
| 243 | GrScalar fScaleY; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 244 | Orientation fOrientation; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 245 | GrGpuGL* fGpuGL; |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 246 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 247 | typedef GrTexture INHERITED; |
| 248 | }; |
| 249 | |
| 250 | #endif |