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