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 | #include "GrGLTexture.h" |
| 19 | #include "GrGpuGL.h" |
| 20 | |
bsalomon@google.com | 8895a7a | 2011-02-18 16:09:55 +0000 | [diff] [blame] | 21 | GrGLRenderTarget::GrGLRenderTarget(const GLRenderTargetIDs& ids, |
| 22 | GLuint stencilBits, |
| 23 | const GrGLIRect& viewport, |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 24 | GrGLTexture* texture, |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 25 | GrGpuGL* gl) : INHERITED(texture, |
| 26 | viewport.fWidth, |
| 27 | viewport.fHeight, |
| 28 | stencilBits) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 29 | fGL = gl; |
| 30 | fRTFBOID = ids.fRTFBOID; |
| 31 | fTexFBOID = ids.fTexFBOID; |
| 32 | fStencilRenderbufferID = ids.fStencilRenderbufferID; |
| 33 | fMSColorRenderbufferID = ids.fMSColorRenderbufferID; |
| 34 | fNeedsResolve = false; |
| 35 | fViewport = viewport; |
| 36 | fOwnIDs = ids.fOwnIDs; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | GrGLRenderTarget::~GrGLRenderTarget() { |
| 40 | fGL->notifyRenderTargetDelete(this); |
| 41 | if (fOwnIDs) { |
| 42 | if (fTexFBOID) { |
| 43 | GR_GLEXT(fGL->extensions(), DeleteFramebuffers(1, &fTexFBOID)); |
| 44 | } |
| 45 | if (fRTFBOID && fRTFBOID != fTexFBOID) { |
| 46 | GR_GLEXT(fGL->extensions(), DeleteFramebuffers(1, &fRTFBOID)); |
| 47 | } |
| 48 | if (fStencilRenderbufferID) { |
| 49 | GR_GLEXT(fGL->extensions(), DeleteRenderbuffers(1, &fStencilRenderbufferID)); |
| 50 | } |
| 51 | if (fMSColorRenderbufferID) { |
| 52 | GR_GLEXT(fGL->extensions(), DeleteRenderbuffers(1, &fMSColorRenderbufferID)); |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | void GrGLRenderTarget::abandon() { |
| 58 | fRTFBOID = 0; |
| 59 | fTexFBOID = 0; |
| 60 | fStencilRenderbufferID = 0; |
| 61 | fMSColorRenderbufferID = 0; |
| 62 | } |
| 63 | |
| 64 | |
| 65 | //////////////////////////////////////////////////////////////////////////////// |
| 66 | |
| 67 | const GLenum GrGLTexture::gWrapMode2GLWrap[] = { |
| 68 | GL_CLAMP_TO_EDGE, |
| 69 | GL_REPEAT, |
| 70 | #ifdef GL_MIRRORED_REPEAT |
| 71 | GL_MIRRORED_REPEAT |
| 72 | #else |
| 73 | GL_REPEAT // GL_MIRRORED_REPEAT not supported :( |
| 74 | #endif |
| 75 | }; |
| 76 | |
| 77 | |
| 78 | GrGLTexture::GrGLTexture(const GLTextureDesc& textureDesc, |
| 79 | const GLRenderTargetIDs& rtIDs, |
bsalomon@google.com | da96ea0 | 2010-12-23 16:53:57 +0000 | [diff] [blame] | 80 | const TexParams& initialTexParams, |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 81 | GrGpuGL* gl) |
| 82 | : INHERITED(textureDesc.fContentWidth, |
| 83 | textureDesc.fContentHeight, |
| 84 | textureDesc.fFormat) { |
| 85 | |
| 86 | fTexParams = initialTexParams; |
| 87 | fTextureID = textureDesc.fTextureID; |
| 88 | fUploadFormat = textureDesc.fUploadFormat; |
| 89 | fUploadByteCount = textureDesc.fUploadByteCount; |
| 90 | fUploadType = textureDesc.fUploadType; |
| 91 | fOrientation = textureDesc.fOrientation; |
| 92 | fAllocWidth = textureDesc.fAllocWidth; |
| 93 | fAllocHeight = textureDesc.fAllocHeight; |
| 94 | fScaleX = GrIntToScalar(textureDesc.fContentWidth) / |
| 95 | textureDesc.fAllocWidth; |
| 96 | fScaleY = GrIntToScalar(textureDesc.fContentHeight) / |
| 97 | textureDesc.fAllocHeight; |
| 98 | fRenderTarget = NULL; |
| 99 | fGpuGL = gl; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 100 | |
| 101 | GrAssert(0 != textureDesc.fTextureID); |
| 102 | |
| 103 | if (rtIDs.fTexFBOID) { |
bsalomon@google.com | 8895a7a | 2011-02-18 16:09:55 +0000 | [diff] [blame] | 104 | // we render to the top left |
| 105 | GrGLIRect vp; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 106 | vp.fLeft = 0; |
bsalomon@google.com | 8895a7a | 2011-02-18 16:09:55 +0000 | [diff] [blame] | 107 | vp.fWidth = textureDesc.fContentWidth; |
| 108 | vp.fHeight = textureDesc.fContentHeight; |
| 109 | vp.fBottom = textureDesc.fAllocHeight - textureDesc.fContentHeight; |
| 110 | |
| 111 | fRenderTarget = new GrGLRenderTarget(rtIDs, textureDesc.fStencilBits, |
| 112 | vp, this, gl); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 113 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | GrGLTexture::~GrGLTexture() { |
| 117 | // make sure we haven't been abandoned |
| 118 | if (fTextureID) { |
| 119 | fGpuGL->notifyTextureDelete(this); |
| 120 | GR_GL(DeleteTextures(1, &fTextureID)); |
| 121 | } |
| 122 | delete fRenderTarget; |
| 123 | } |
| 124 | |
| 125 | void GrGLTexture::abandon() { |
| 126 | fTextureID = 0; |
| 127 | if (NULL != fRenderTarget) { |
bsalomon@google.com | da96ea0 | 2010-12-23 16:53:57 +0000 | [diff] [blame] | 128 | fRenderTarget->abandon(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 129 | } |
| 130 | } |
| 131 | |
| 132 | bool GrGLTexture::isRenderTarget() const { |
| 133 | return NULL != fRenderTarget; |
| 134 | } |
| 135 | |
| 136 | GrRenderTarget* GrGLTexture::asRenderTarget() { |
| 137 | return (GrRenderTarget*)fRenderTarget; |
| 138 | } |
| 139 | |
| 140 | void GrGLTexture::removeRenderTarget() { |
| 141 | GrAssert(NULL != fRenderTarget); |
| 142 | if (NULL != fRenderTarget) { |
| 143 | // must do this notify before the delete |
| 144 | fGpuGL->notifyTextureRemoveRenderTarget(this); |
| 145 | delete fRenderTarget; |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 146 | fRenderTarget = NULL; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 147 | } |
| 148 | } |
| 149 | |
| 150 | void GrGLTexture::uploadTextureData(uint32_t x, |
| 151 | uint32_t y, |
| 152 | uint32_t width, |
| 153 | uint32_t height, |
| 154 | const void* srcData) { |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 155 | |
| 156 | fGpuGL->setSpareTextureUnit(); |
| 157 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 158 | // glCompressedTexSubImage2D doesn't support any formats |
| 159 | // (at least without extensions) |
| 160 | GrAssert(fUploadFormat != GR_PALETTE8_RGBA8); |
| 161 | |
| 162 | // If we need to update textures that are created upside down |
| 163 | // then we have to modify this code to flip the srcData |
| 164 | GrAssert(kTopDown_Orientation == fOrientation); |
| 165 | GR_GL(BindTexture(GL_TEXTURE_2D, fTextureID)); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 166 | GR_GL(PixelStorei(GL_UNPACK_ALIGNMENT, fUploadByteCount)); |
| 167 | GR_GL(TexSubImage2D(GL_TEXTURE_2D, 0, x, y, width, height, |
| 168 | fUploadFormat, fUploadType, srcData)); |
| 169 | |
| 170 | } |
| 171 | |
| 172 | intptr_t GrGLTexture::getTextureHandle() { |
| 173 | return fTextureID; |
| 174 | } |
| 175 | |
| 176 | |
| 177 | |