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