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 | f954d8d | 2011-04-06 17:50:02 +0000 | [diff] [blame] | 27 | bool isMultisampled, |
bsalomon@google.com | 8895a7a | 2011-02-18 16:09:55 +0000 | [diff] [blame] | 28 | const GrGLIRect& viewport, |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 29 | GrGLTexture* texture) |
bsalomon@google.com | f954d8d | 2011-04-06 17:50:02 +0000 | [diff] [blame] | 30 | : INHERITED(gpu, texture, viewport.fWidth, |
| 31 | viewport.fHeight, stencilBits, isMultisampled) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 32 | fRTFBOID = ids.fRTFBOID; |
| 33 | fTexFBOID = ids.fTexFBOID; |
| 34 | fStencilRenderbufferID = ids.fStencilRenderbufferID; |
| 35 | fMSColorRenderbufferID = ids.fMSColorRenderbufferID; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 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 | |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 42 | void GrGLRenderTarget::onRelease() { |
Scroggo | c29d7cd | 2011-06-16 13:14:21 +0000 | [diff] [blame^] | 43 | GPUGL->notifyRenderTargetDelete(this); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 44 | if (fOwnIDs) { |
| 45 | if (fTexFBOID) { |
twiz@google.com | 59a190b | 2011-03-14 21:23:01 +0000 | [diff] [blame] | 46 | GR_GL(DeleteFramebuffers(1, &fTexFBOID)); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 47 | } |
| 48 | if (fRTFBOID && fRTFBOID != fTexFBOID) { |
twiz@google.com | 59a190b | 2011-03-14 21:23:01 +0000 | [diff] [blame] | 49 | GR_GL(DeleteFramebuffers(1, &fRTFBOID)); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 50 | } |
| 51 | if (fStencilRenderbufferID) { |
twiz@google.com | 59a190b | 2011-03-14 21:23:01 +0000 | [diff] [blame] | 52 | GR_GL(DeleteRenderbuffers(1, &fStencilRenderbufferID)); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 53 | } |
| 54 | if (fMSColorRenderbufferID) { |
twiz@google.com | 59a190b | 2011-03-14 21:23:01 +0000 | [diff] [blame] | 55 | GR_GL(DeleteRenderbuffers(1, &fMSColorRenderbufferID)); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 56 | } |
| 57 | } |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 58 | fRTFBOID = 0; |
| 59 | fTexFBOID = 0; |
| 60 | fStencilRenderbufferID = 0; |
| 61 | fMSColorRenderbufferID = 0; |
bsalomon@google.com | 1da0746 | 2011-03-10 14:51:57 +0000 | [diff] [blame] | 62 | GrSafeUnref(fTexIDObj); |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 63 | fTexIDObj = NULL; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 64 | } |
| 65 | |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 66 | void GrGLRenderTarget::onAbandon() { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 67 | fRTFBOID = 0; |
| 68 | fTexFBOID = 0; |
| 69 | fStencilRenderbufferID = 0; |
| 70 | fMSColorRenderbufferID = 0; |
bsalomon@google.com | 1da0746 | 2011-03-10 14:51:57 +0000 | [diff] [blame] | 71 | if (NULL != fTexIDObj) { |
| 72 | fTexIDObj->abandon(); |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 73 | fTexIDObj = NULL; |
bsalomon@google.com | 1da0746 | 2011-03-10 14:51:57 +0000 | [diff] [blame] | 74 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | |
| 78 | //////////////////////////////////////////////////////////////////////////////// |
| 79 | |
twiz@google.com | b65e0cb | 2011-03-18 20:41:44 +0000 | [diff] [blame] | 80 | const GrGLenum* GrGLTexture::WrapMode2GLWrap() { |
| 81 | static const GrGLenum mirrorRepeatModes[] = { |
| 82 | GR_GL_CLAMP_TO_EDGE, |
| 83 | GR_GL_REPEAT, |
| 84 | GR_GL_MIRRORED_REPEAT |
| 85 | }; |
| 86 | |
| 87 | static const GrGLenum repeatModes[] = { |
| 88 | GR_GL_CLAMP_TO_EDGE, |
| 89 | GR_GL_REPEAT, |
| 90 | GR_GL_REPEAT |
| 91 | }; |
| 92 | |
| 93 | if (GR_GL_SUPPORT_ES1 && !GR_GL_SUPPORT_ES2) { |
| 94 | return repeatModes; // GL_MIRRORED_REPEAT not supported. |
| 95 | } else { |
| 96 | return mirrorRepeatModes; |
| 97 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 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 | 5877ffd | 2011-04-11 17:58:48 +0000 | [diff] [blame] | 110 | fTexIDObj = new GrGLTexID(textureDesc.fTextureID, |
| 111 | textureDesc.fOwnsID); |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 112 | fUploadFormat = textureDesc.fUploadFormat; |
| 113 | fUploadByteCount = textureDesc.fUploadByteCount; |
| 114 | fUploadType = textureDesc.fUploadType; |
| 115 | fOrientation = textureDesc.fOrientation; |
| 116 | fAllocWidth = textureDesc.fAllocWidth; |
| 117 | fAllocHeight = textureDesc.fAllocHeight; |
| 118 | fScaleX = GrIntToScalar(textureDesc.fContentWidth) / |
| 119 | textureDesc.fAllocWidth; |
| 120 | fScaleY = GrIntToScalar(textureDesc.fContentHeight) / |
| 121 | textureDesc.fAllocHeight; |
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 | f954d8d | 2011-04-06 17:50:02 +0000 | [diff] [blame] | 135 | rtIDs.fRTFBOID != rtIDs.fTexFBOID, |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 136 | vp, this); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 137 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 138 | } |
| 139 | |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 140 | void GrGLTexture::onRelease() { |
bsalomon@google.com | 6dcf499 | 2011-04-05 21:16:14 +0000 | [diff] [blame] | 141 | INHERITED::onRelease(); |
Scroggo | c29d7cd | 2011-06-16 13:14:21 +0000 | [diff] [blame^] | 142 | GPUGL->notifyTextureDelete(this); |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 143 | if (NULL != fTexIDObj) { |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 144 | fTexIDObj->unref(); |
| 145 | fTexIDObj = NULL; |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 146 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 147 | } |
| 148 | |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 149 | void GrGLTexture::onAbandon() { |
bsalomon@google.com | 6dcf499 | 2011-04-05 21:16:14 +0000 | [diff] [blame] | 150 | INHERITED::onAbandon(); |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 151 | if (NULL != fTexIDObj) { |
| 152 | fTexIDObj->abandon(); |
| 153 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 154 | } |
| 155 | |
bsalomon@google.com | 79d2dbe | 2011-06-13 19:28:02 +0000 | [diff] [blame] | 156 | void GrGLTexture::uploadTextureData(int x, |
| 157 | int y, |
| 158 | int width, |
| 159 | int height, |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 160 | const void* srcData) { |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 161 | |
| 162 | GPUGL->setSpareTextureUnit(); |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 163 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 164 | // glCompressedTexSubImage2D doesn't support any formats |
| 165 | // (at least without extensions) |
bsalomon@google.com | c312bf9 | 2011-03-21 21:10:33 +0000 | [diff] [blame] | 166 | GrAssert(fUploadFormat != GR_GL_PALETTE8_RGBA8); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 167 | |
| 168 | // If we need to update textures that are created upside down |
| 169 | // then we have to modify this code to flip the srcData |
| 170 | GrAssert(kTopDown_Orientation == fOrientation); |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 171 | GR_GL(BindTexture(GR_GL_TEXTURE_2D, fTexIDObj->id())); |
| 172 | GR_GL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, fUploadByteCount)); |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 173 | 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] | 174 | fUploadFormat, fUploadType, srcData)); |
| 175 | |
| 176 | } |
| 177 | |
| 178 | intptr_t GrGLTexture::getTextureHandle() { |
bsalomon@google.com | 1da0746 | 2011-03-10 14:51:57 +0000 | [diff] [blame] | 179 | return fTexIDObj->id(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 180 | } |
| 181 | |