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