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