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; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 121 | |
| 122 | GrAssert(0 != textureDesc.fTextureID); |
| 123 | |
| 124 | if (rtIDs.fTexFBOID) { |
bsalomon@google.com | 8895a7a | 2011-02-18 16:09:55 +0000 | [diff] [blame] | 125 | // we render to the top left |
| 126 | GrGLIRect vp; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 127 | vp.fLeft = 0; |
bsalomon@google.com | 8895a7a | 2011-02-18 16:09:55 +0000 | [diff] [blame] | 128 | vp.fWidth = textureDesc.fContentWidth; |
| 129 | vp.fHeight = textureDesc.fContentHeight; |
| 130 | vp.fBottom = textureDesc.fAllocHeight - textureDesc.fContentHeight; |
| 131 | |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 132 | fRenderTarget = new GrGLRenderTarget(gpu, rtIDs, fTexIDObj, |
bsalomon@google.com | 1da0746 | 2011-03-10 14:51:57 +0000 | [diff] [blame] | 133 | textureDesc.fStencilBits, |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 134 | vp, this); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 135 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 136 | } |
| 137 | |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 138 | void GrGLTexture::onRelease() { |
bsalomon@google.com | 6dcf499 | 2011-04-05 21:16:14 +0000 | [diff] [blame] | 139 | INHERITED::onRelease(); |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 140 | if (NULL != fTexIDObj) { |
| 141 | GPUGL->notifyTextureDelete(this); |
| 142 | fTexIDObj->unref(); |
| 143 | fTexIDObj = NULL; |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 144 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 145 | } |
| 146 | |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 147 | void GrGLTexture::onAbandon() { |
bsalomon@google.com | 6dcf499 | 2011-04-05 21:16:14 +0000 | [diff] [blame] | 148 | INHERITED::onAbandon(); |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 149 | if (NULL != fTexIDObj) { |
| 150 | fTexIDObj->abandon(); |
| 151 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | void GrGLTexture::uploadTextureData(uint32_t x, |
| 155 | uint32_t y, |
| 156 | uint32_t width, |
| 157 | uint32_t height, |
| 158 | const void* srcData) { |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 159 | |
| 160 | GPUGL->setSpareTextureUnit(); |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 161 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 162 | // glCompressedTexSubImage2D doesn't support any formats |
| 163 | // (at least without extensions) |
bsalomon@google.com | c312bf9 | 2011-03-21 21:10:33 +0000 | [diff] [blame] | 164 | GrAssert(fUploadFormat != GR_GL_PALETTE8_RGBA8); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 165 | |
| 166 | // If we need to update textures that are created upside down |
| 167 | // then we have to modify this code to flip the srcData |
| 168 | GrAssert(kTopDown_Orientation == fOrientation); |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 169 | GR_GL(BindTexture(GR_GL_TEXTURE_2D, fTexIDObj->id())); |
| 170 | GR_GL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, fUploadByteCount)); |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 171 | 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] | 172 | fUploadFormat, fUploadType, srcData)); |
| 173 | |
| 174 | } |
| 175 | |
| 176 | intptr_t GrGLTexture::getTextureHandle() { |
bsalomon@google.com | 1da0746 | 2011-03-10 14:51:57 +0000 | [diff] [blame] | 177 | return fTexIDObj->id(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 178 | } |
| 179 | |