epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 3 | * Copyright 2011 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 10 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 11 | #include "GrGLTexture.h" |
| 12 | #include "GrGpuGL.h" |
| 13 | |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 14 | #define GPUGL static_cast<GrGpuGL*>(getGpu()) |
| 15 | |
| 16 | GrGLRenderTarget::GrGLRenderTarget(GrGpuGL* gpu, |
| 17 | const GLRenderTargetIDs& ids, |
bsalomon@google.com | 1da0746 | 2011-03-10 14:51:57 +0000 | [diff] [blame] | 18 | GrGLTexID* texID, |
bsalomon@google.com | cee661a | 2011-07-26 12:32:36 +0000 | [diff] [blame] | 19 | GrPixelConfig config, |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 20 | GrGLuint stencilBits, |
bsalomon@google.com | f954d8d | 2011-04-06 17:50:02 +0000 | [diff] [blame] | 21 | bool isMultisampled, |
bsalomon@google.com | 8895a7a | 2011-02-18 16:09:55 +0000 | [diff] [blame] | 22 | const GrGLIRect& viewport, |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 23 | GrGLTexture* texture) |
bsalomon@google.com | f954d8d | 2011-04-06 17:50:02 +0000 | [diff] [blame] | 24 | : INHERITED(gpu, texture, viewport.fWidth, |
bsalomon@google.com | cee661a | 2011-07-26 12:32:36 +0000 | [diff] [blame] | 25 | viewport.fHeight, config, |
| 26 | stencilBits, isMultisampled) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 27 | fRTFBOID = ids.fRTFBOID; |
| 28 | fTexFBOID = ids.fTexFBOID; |
| 29 | fStencilRenderbufferID = ids.fStencilRenderbufferID; |
| 30 | fMSColorRenderbufferID = ids.fMSColorRenderbufferID; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 31 | fViewport = viewport; |
| 32 | fOwnIDs = ids.fOwnIDs; |
bsalomon@google.com | 1da0746 | 2011-03-10 14:51:57 +0000 | [diff] [blame] | 33 | fTexIDObj = texID; |
| 34 | GrSafeRef(fTexIDObj); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 35 | } |
| 36 | |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 37 | void GrGLRenderTarget::onRelease() { |
Scroggo | c29d7cd | 2011-06-16 13:14:21 +0000 | [diff] [blame] | 38 | GPUGL->notifyRenderTargetDelete(this); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 39 | if (fOwnIDs) { |
| 40 | if (fTexFBOID) { |
twiz@google.com | 59a190b | 2011-03-14 21:23:01 +0000 | [diff] [blame] | 41 | GR_GL(DeleteFramebuffers(1, &fTexFBOID)); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 42 | } |
| 43 | if (fRTFBOID && fRTFBOID != fTexFBOID) { |
twiz@google.com | 59a190b | 2011-03-14 21:23:01 +0000 | [diff] [blame] | 44 | GR_GL(DeleteFramebuffers(1, &fRTFBOID)); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 45 | } |
| 46 | if (fStencilRenderbufferID) { |
twiz@google.com | 59a190b | 2011-03-14 21:23:01 +0000 | [diff] [blame] | 47 | GR_GL(DeleteRenderbuffers(1, &fStencilRenderbufferID)); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 48 | } |
| 49 | if (fMSColorRenderbufferID) { |
twiz@google.com | 59a190b | 2011-03-14 21:23:01 +0000 | [diff] [blame] | 50 | GR_GL(DeleteRenderbuffers(1, &fMSColorRenderbufferID)); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 51 | } |
| 52 | } |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 53 | fRTFBOID = 0; |
| 54 | fTexFBOID = 0; |
| 55 | fStencilRenderbufferID = 0; |
| 56 | fMSColorRenderbufferID = 0; |
bsalomon@google.com | 1da0746 | 2011-03-10 14:51:57 +0000 | [diff] [blame] | 57 | GrSafeUnref(fTexIDObj); |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 58 | fTexIDObj = NULL; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 59 | } |
| 60 | |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 61 | void GrGLRenderTarget::onAbandon() { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 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(); |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 68 | fTexIDObj = NULL; |
bsalomon@google.com | 1da0746 | 2011-03-10 14:51:57 +0000 | [diff] [blame] | 69 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | |
| 73 | //////////////////////////////////////////////////////////////////////////////// |
| 74 | |
twiz@google.com | b65e0cb | 2011-03-18 20:41:44 +0000 | [diff] [blame] | 75 | const GrGLenum* GrGLTexture::WrapMode2GLWrap() { |
| 76 | static const GrGLenum mirrorRepeatModes[] = { |
| 77 | GR_GL_CLAMP_TO_EDGE, |
| 78 | GR_GL_REPEAT, |
| 79 | GR_GL_MIRRORED_REPEAT |
| 80 | }; |
| 81 | |
| 82 | static const GrGLenum repeatModes[] = { |
| 83 | GR_GL_CLAMP_TO_EDGE, |
| 84 | GR_GL_REPEAT, |
| 85 | GR_GL_REPEAT |
| 86 | }; |
| 87 | |
| 88 | if (GR_GL_SUPPORT_ES1 && !GR_GL_SUPPORT_ES2) { |
| 89 | return repeatModes; // GL_MIRRORED_REPEAT not supported. |
| 90 | } else { |
| 91 | return mirrorRepeatModes; |
| 92 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 93 | }; |
| 94 | |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 95 | GrGLTexture::GrGLTexture(GrGpuGL* gpu, |
| 96 | const GLTextureDesc& textureDesc, |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 97 | const GLRenderTargetIDs& rtIDs, |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 98 | const TexParams& initialTexParams) |
| 99 | : INHERITED(gpu, |
| 100 | textureDesc.fContentWidth, |
| 101 | textureDesc.fContentHeight, |
| 102 | textureDesc.fFormat) { |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 103 | |
| 104 | fTexParams = initialTexParams; |
bsalomon@google.com | 5877ffd | 2011-04-11 17:58:48 +0000 | [diff] [blame] | 105 | fTexIDObj = new GrGLTexID(textureDesc.fTextureID, |
| 106 | textureDesc.fOwnsID); |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 107 | fUploadFormat = textureDesc.fUploadFormat; |
| 108 | fUploadByteCount = textureDesc.fUploadByteCount; |
| 109 | fUploadType = textureDesc.fUploadType; |
| 110 | fOrientation = textureDesc.fOrientation; |
| 111 | fAllocWidth = textureDesc.fAllocWidth; |
| 112 | fAllocHeight = textureDesc.fAllocHeight; |
| 113 | fScaleX = GrIntToScalar(textureDesc.fContentWidth) / |
| 114 | textureDesc.fAllocWidth; |
| 115 | fScaleY = GrIntToScalar(textureDesc.fContentHeight) / |
| 116 | textureDesc.fAllocHeight; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 117 | |
| 118 | GrAssert(0 != textureDesc.fTextureID); |
| 119 | |
| 120 | if (rtIDs.fTexFBOID) { |
bsalomon@google.com | 8895a7a | 2011-02-18 16:09:55 +0000 | [diff] [blame] | 121 | // we render to the top left |
| 122 | GrGLIRect vp; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 123 | vp.fLeft = 0; |
bsalomon@google.com | 8895a7a | 2011-02-18 16:09:55 +0000 | [diff] [blame] | 124 | vp.fWidth = textureDesc.fContentWidth; |
| 125 | vp.fHeight = textureDesc.fContentHeight; |
| 126 | vp.fBottom = textureDesc.fAllocHeight - textureDesc.fContentHeight; |
| 127 | |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 128 | fRenderTarget = new GrGLRenderTarget(gpu, rtIDs, fTexIDObj, |
bsalomon@google.com | cee661a | 2011-07-26 12:32:36 +0000 | [diff] [blame] | 129 | textureDesc.fFormat, |
bsalomon@google.com | 1da0746 | 2011-03-10 14:51:57 +0000 | [diff] [blame] | 130 | textureDesc.fStencilBits, |
bsalomon@google.com | f954d8d | 2011-04-06 17:50:02 +0000 | [diff] [blame] | 131 | rtIDs.fRTFBOID != rtIDs.fTexFBOID, |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 132 | vp, this); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 133 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 134 | } |
| 135 | |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 136 | void GrGLTexture::onRelease() { |
bsalomon@google.com | 6dcf499 | 2011-04-05 21:16:14 +0000 | [diff] [blame] | 137 | INHERITED::onRelease(); |
Scroggo | c29d7cd | 2011-06-16 13:14:21 +0000 | [diff] [blame] | 138 | GPUGL->notifyTextureDelete(this); |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 139 | if (NULL != fTexIDObj) { |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 140 | fTexIDObj->unref(); |
| 141 | fTexIDObj = NULL; |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 142 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 143 | } |
| 144 | |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 145 | void GrGLTexture::onAbandon() { |
bsalomon@google.com | 6dcf499 | 2011-04-05 21:16:14 +0000 | [diff] [blame] | 146 | INHERITED::onAbandon(); |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 147 | if (NULL != fTexIDObj) { |
| 148 | fTexIDObj->abandon(); |
| 149 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 150 | } |
| 151 | |
bsalomon@google.com | 79d2dbe | 2011-06-13 19:28:02 +0000 | [diff] [blame] | 152 | void GrGLTexture::uploadTextureData(int x, |
| 153 | int y, |
| 154 | int width, |
| 155 | int height, |
junov@google.com | 4ee7ae5 | 2011-06-30 17:30:49 +0000 | [diff] [blame] | 156 | const void* srcData, |
| 157 | size_t rowBytes) { |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 158 | |
| 159 | GPUGL->setSpareTextureUnit(); |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 160 | |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 161 | // ES2 glCompressedTexSubImage2D doesn't support any formats |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 162 | // (at least without extensions) |
bsalomon@google.com | c312bf9 | 2011-03-21 21:10:33 +0000 | [diff] [blame] | 163 | GrAssert(fUploadFormat != GR_GL_PALETTE8_RGBA8); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 164 | |
junov@google.com | 4ee7ae5 | 2011-06-30 17:30:49 +0000 | [diff] [blame] | 165 | // in case we need a temporary, trimmed copy of the src pixels |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 166 | SkAutoSMalloc<128 * 128> tempStorage; |
junov@google.com | 4ee7ae5 | 2011-06-30 17:30:49 +0000 | [diff] [blame] | 167 | |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 168 | if (!rowBytes) { |
| 169 | rowBytes = fUploadByteCount * width; |
| 170 | } |
junov@google.com | 4ee7ae5 | 2011-06-30 17:30:49 +0000 | [diff] [blame] | 171 | /* |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 172 | * check whether to allocate a temporary buffer for flipping y or |
| 173 | * because our srcData has extra bytes past each row. If so, we need |
| 174 | * to trim those off here, since GL ES doesn't let us specify |
| 175 | * GL_UNPACK_ROW_LENGTH. |
junov@google.com | 4ee7ae5 | 2011-06-30 17:30:49 +0000 | [diff] [blame] | 176 | */ |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 177 | bool restoreGLRowLength = false; |
| 178 | bool flipY = kBottomUp_Orientation == fOrientation; |
| 179 | if (GR_GL_SUPPORT_DESKTOP && !flipY) { |
| 180 | // can't use this for flipping, only non-neg values allowed. :( |
junov@google.com | 4ee7ae5 | 2011-06-30 17:30:49 +0000 | [diff] [blame] | 181 | if (srcData && rowBytes) { |
| 182 | GR_GL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, |
| 183 | rowBytes / fUploadByteCount)); |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 184 | restoreGLRowLength = true; |
junov@google.com | 4ee7ae5 | 2011-06-30 17:30:49 +0000 | [diff] [blame] | 185 | } |
| 186 | } else { |
| 187 | size_t trimRowBytes = width * fUploadByteCount; |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 188 | if (srcData && (trimRowBytes < rowBytes || flipY)) { |
junov@google.com | 4ee7ae5 | 2011-06-30 17:30:49 +0000 | [diff] [blame] | 189 | // copy the data into our new storage, skipping the trailing bytes |
| 190 | size_t trimSize = height * trimRowBytes; |
| 191 | const char* src = (const char*)srcData; |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 192 | if (flipY) { |
| 193 | src += (height - 1) * rowBytes; |
| 194 | } |
| 195 | char* dst = (char*)tempStorage.realloc(trimSize); |
junov@google.com | 4ee7ae5 | 2011-06-30 17:30:49 +0000 | [diff] [blame] | 196 | for (int y = 0; y < height; y++) { |
| 197 | memcpy(dst, src, trimRowBytes); |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 198 | if (flipY) { |
| 199 | src -= rowBytes; |
| 200 | } else { |
| 201 | src += rowBytes; |
| 202 | } |
junov@google.com | 4ee7ae5 | 2011-06-30 17:30:49 +0000 | [diff] [blame] | 203 | dst += trimRowBytes; |
| 204 | } |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 205 | // now point srcData to our copied version |
| 206 | srcData = tempStorage.get(); |
junov@google.com | 4ee7ae5 | 2011-06-30 17:30:49 +0000 | [diff] [blame] | 207 | } |
| 208 | } |
| 209 | |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 210 | if (flipY) { |
| 211 | y = this->height() - (y + height); |
| 212 | } |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 213 | GR_GL(BindTexture(GR_GL_TEXTURE_2D, fTexIDObj->id())); |
| 214 | GR_GL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, fUploadByteCount)); |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 215 | 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] | 216 | fUploadFormat, fUploadType, srcData)); |
| 217 | |
junov@google.com | 4ee7ae5 | 2011-06-30 17:30:49 +0000 | [diff] [blame] | 218 | if (GR_GL_SUPPORT_DESKTOP) { |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 219 | if (restoreGLRowLength) { |
junov@google.com | 4ee7ae5 | 2011-06-30 17:30:49 +0000 | [diff] [blame] | 220 | GR_GL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0)); |
| 221 | } |
| 222 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 223 | } |
| 224 | |
bsalomon@google.com | cee661a | 2011-07-26 12:32:36 +0000 | [diff] [blame] | 225 | intptr_t GrGLTexture::getTextureHandle() const { |
bsalomon@google.com | 1da0746 | 2011-03-10 14:51:57 +0000 | [diff] [blame] | 226 | return fTexIDObj->id(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 227 | } |
| 228 | |