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 | |
| 10 | #include "GrGLTexture.h" |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame^] | 11 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 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 | |
twiz@google.com | b65e0cb | 2011-03-18 20:41:44 +0000 | [diff] [blame] | 16 | const GrGLenum* GrGLTexture::WrapMode2GLWrap() { |
| 17 | static const GrGLenum mirrorRepeatModes[] = { |
| 18 | GR_GL_CLAMP_TO_EDGE, |
| 19 | GR_GL_REPEAT, |
| 20 | GR_GL_MIRRORED_REPEAT |
| 21 | }; |
| 22 | |
| 23 | static const GrGLenum repeatModes[] = { |
| 24 | GR_GL_CLAMP_TO_EDGE, |
| 25 | GR_GL_REPEAT, |
| 26 | GR_GL_REPEAT |
| 27 | }; |
| 28 | |
| 29 | if (GR_GL_SUPPORT_ES1 && !GR_GL_SUPPORT_ES2) { |
| 30 | return repeatModes; // GL_MIRRORED_REPEAT not supported. |
| 31 | } else { |
| 32 | return mirrorRepeatModes; |
| 33 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 34 | }; |
| 35 | |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 36 | GrGLTexture::GrGLTexture(GrGpuGL* gpu, |
| 37 | const GLTextureDesc& textureDesc, |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 38 | const GLRenderTargetIDs& rtIDs, |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 39 | const TexParams& initialTexParams) |
| 40 | : INHERITED(gpu, |
| 41 | textureDesc.fContentWidth, |
| 42 | textureDesc.fContentHeight, |
| 43 | textureDesc.fFormat) { |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 44 | |
| 45 | fTexParams = initialTexParams; |
bsalomon@google.com | 5877ffd | 2011-04-11 17:58:48 +0000 | [diff] [blame] | 46 | fTexIDObj = new GrGLTexID(textureDesc.fTextureID, |
| 47 | textureDesc.fOwnsID); |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 48 | fUploadFormat = textureDesc.fUploadFormat; |
| 49 | fUploadByteCount = textureDesc.fUploadByteCount; |
| 50 | fUploadType = textureDesc.fUploadType; |
| 51 | fOrientation = textureDesc.fOrientation; |
| 52 | fAllocWidth = textureDesc.fAllocWidth; |
| 53 | fAllocHeight = textureDesc.fAllocHeight; |
| 54 | fScaleX = GrIntToScalar(textureDesc.fContentWidth) / |
| 55 | textureDesc.fAllocWidth; |
| 56 | fScaleY = GrIntToScalar(textureDesc.fContentHeight) / |
| 57 | textureDesc.fAllocHeight; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 58 | |
| 59 | GrAssert(0 != textureDesc.fTextureID); |
| 60 | |
| 61 | if (rtIDs.fTexFBOID) { |
bsalomon@google.com | 8895a7a | 2011-02-18 16:09:55 +0000 | [diff] [blame] | 62 | // we render to the top left |
| 63 | GrGLIRect vp; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 64 | vp.fLeft = 0; |
bsalomon@google.com | 8895a7a | 2011-02-18 16:09:55 +0000 | [diff] [blame] | 65 | vp.fWidth = textureDesc.fContentWidth; |
| 66 | vp.fHeight = textureDesc.fContentHeight; |
| 67 | vp.fBottom = textureDesc.fAllocHeight - textureDesc.fContentHeight; |
| 68 | |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 69 | fRenderTarget = new GrGLRenderTarget(gpu, rtIDs, fTexIDObj, |
bsalomon@google.com | cee661a | 2011-07-26 12:32:36 +0000 | [diff] [blame] | 70 | textureDesc.fFormat, |
bsalomon@google.com | 1da0746 | 2011-03-10 14:51:57 +0000 | [diff] [blame] | 71 | textureDesc.fStencilBits, |
bsalomon@google.com | f954d8d | 2011-04-06 17:50:02 +0000 | [diff] [blame] | 72 | rtIDs.fRTFBOID != rtIDs.fTexFBOID, |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 73 | vp, this); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 74 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 75 | } |
| 76 | |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 77 | void GrGLTexture::onRelease() { |
bsalomon@google.com | 6dcf499 | 2011-04-05 21:16:14 +0000 | [diff] [blame] | 78 | INHERITED::onRelease(); |
Scroggo | c29d7cd | 2011-06-16 13:14:21 +0000 | [diff] [blame] | 79 | GPUGL->notifyTextureDelete(this); |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 80 | if (NULL != fTexIDObj) { |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 81 | fTexIDObj->unref(); |
| 82 | fTexIDObj = NULL; |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 83 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 84 | } |
| 85 | |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 86 | void GrGLTexture::onAbandon() { |
bsalomon@google.com | 6dcf499 | 2011-04-05 21:16:14 +0000 | [diff] [blame] | 87 | INHERITED::onAbandon(); |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 88 | if (NULL != fTexIDObj) { |
| 89 | fTexIDObj->abandon(); |
| 90 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 91 | } |
| 92 | |
bsalomon@google.com | 79d2dbe | 2011-06-13 19:28:02 +0000 | [diff] [blame] | 93 | void GrGLTexture::uploadTextureData(int x, |
| 94 | int y, |
| 95 | int width, |
| 96 | int height, |
junov@google.com | 4ee7ae5 | 2011-06-30 17:30:49 +0000 | [diff] [blame] | 97 | const void* srcData, |
| 98 | size_t rowBytes) { |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 99 | |
| 100 | GPUGL->setSpareTextureUnit(); |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 101 | |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 102 | // ES2 glCompressedTexSubImage2D doesn't support any formats |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 103 | // (at least without extensions) |
bsalomon@google.com | c312bf9 | 2011-03-21 21:10:33 +0000 | [diff] [blame] | 104 | GrAssert(fUploadFormat != GR_GL_PALETTE8_RGBA8); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 105 | |
junov@google.com | 4ee7ae5 | 2011-06-30 17:30:49 +0000 | [diff] [blame] | 106 | // 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] | 107 | SkAutoSMalloc<128 * 128> tempStorage; |
junov@google.com | 4ee7ae5 | 2011-06-30 17:30:49 +0000 | [diff] [blame] | 108 | |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 109 | if (!rowBytes) { |
| 110 | rowBytes = fUploadByteCount * width; |
| 111 | } |
junov@google.com | 4ee7ae5 | 2011-06-30 17:30:49 +0000 | [diff] [blame] | 112 | /* |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 113 | * check whether to allocate a temporary buffer for flipping y or |
| 114 | * because our srcData has extra bytes past each row. If so, we need |
| 115 | * to trim those off here, since GL ES doesn't let us specify |
| 116 | * GL_UNPACK_ROW_LENGTH. |
junov@google.com | 4ee7ae5 | 2011-06-30 17:30:49 +0000 | [diff] [blame] | 117 | */ |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 118 | bool restoreGLRowLength = false; |
| 119 | bool flipY = kBottomUp_Orientation == fOrientation; |
| 120 | if (GR_GL_SUPPORT_DESKTOP && !flipY) { |
| 121 | // can't use this for flipping, only non-neg values allowed. :( |
junov@google.com | 4ee7ae5 | 2011-06-30 17:30:49 +0000 | [diff] [blame] | 122 | if (srcData && rowBytes) { |
| 123 | GR_GL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, |
| 124 | rowBytes / fUploadByteCount)); |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 125 | restoreGLRowLength = true; |
junov@google.com | 4ee7ae5 | 2011-06-30 17:30:49 +0000 | [diff] [blame] | 126 | } |
| 127 | } else { |
| 128 | size_t trimRowBytes = width * fUploadByteCount; |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 129 | if (srcData && (trimRowBytes < rowBytes || flipY)) { |
junov@google.com | 4ee7ae5 | 2011-06-30 17:30:49 +0000 | [diff] [blame] | 130 | // copy the data into our new storage, skipping the trailing bytes |
| 131 | size_t trimSize = height * trimRowBytes; |
| 132 | const char* src = (const char*)srcData; |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 133 | if (flipY) { |
| 134 | src += (height - 1) * rowBytes; |
| 135 | } |
| 136 | char* dst = (char*)tempStorage.realloc(trimSize); |
junov@google.com | 4ee7ae5 | 2011-06-30 17:30:49 +0000 | [diff] [blame] | 137 | for (int y = 0; y < height; y++) { |
| 138 | memcpy(dst, src, trimRowBytes); |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 139 | if (flipY) { |
| 140 | src -= rowBytes; |
| 141 | } else { |
| 142 | src += rowBytes; |
| 143 | } |
junov@google.com | 4ee7ae5 | 2011-06-30 17:30:49 +0000 | [diff] [blame] | 144 | dst += trimRowBytes; |
| 145 | } |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 146 | // now point srcData to our copied version |
| 147 | srcData = tempStorage.get(); |
junov@google.com | 4ee7ae5 | 2011-06-30 17:30:49 +0000 | [diff] [blame] | 148 | } |
| 149 | } |
| 150 | |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 151 | if (flipY) { |
| 152 | y = this->height() - (y + height); |
| 153 | } |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 154 | GR_GL(BindTexture(GR_GL_TEXTURE_2D, fTexIDObj->id())); |
| 155 | GR_GL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, fUploadByteCount)); |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 156 | 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] | 157 | fUploadFormat, fUploadType, srcData)); |
| 158 | |
junov@google.com | 4ee7ae5 | 2011-06-30 17:30:49 +0000 | [diff] [blame] | 159 | if (GR_GL_SUPPORT_DESKTOP) { |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 160 | if (restoreGLRowLength) { |
junov@google.com | 4ee7ae5 | 2011-06-30 17:30:49 +0000 | [diff] [blame] | 161 | GR_GL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0)); |
| 162 | } |
| 163 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 164 | } |
| 165 | |
bsalomon@google.com | cee661a | 2011-07-26 12:32:36 +0000 | [diff] [blame] | 166 | intptr_t GrGLTexture::getTextureHandle() const { |
bsalomon@google.com | 1da0746 | 2011-03-10 14:51:57 +0000 | [diff] [blame] | 167 | return fTexIDObj->id(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 168 | } |
| 169 | |