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 | |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 16 | #define GL_CALL(X) GR_GL_CALL(GPUGL->glInterface(), X) |
| 17 | |
bsalomon@google.com | 1dcf506 | 2011-11-14 19:29:53 +0000 | [diff] [blame] | 18 | const GrGLenum* GrGLTexture::WrapMode2GLWrap() { |
| 19 | static const GrGLenum repeatModes[] = { |
twiz@google.com | b65e0cb | 2011-03-18 20:41:44 +0000 | [diff] [blame] | 20 | GR_GL_CLAMP_TO_EDGE, |
| 21 | GR_GL_REPEAT, |
| 22 | GR_GL_MIRRORED_REPEAT |
| 23 | }; |
bsalomon@google.com | 1dcf506 | 2011-11-14 19:29:53 +0000 | [diff] [blame] | 24 | return repeatModes; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 25 | }; |
| 26 | |
bsalomon@google.com | 5bfc217 | 2011-07-29 20:29:05 +0000 | [diff] [blame] | 27 | void GrGLTexture::init(GrGpuGL* gpu, |
| 28 | const Desc& textureDesc, |
bsalomon@google.com | 80d09b9 | 2011-11-05 21:21:13 +0000 | [diff] [blame] | 29 | const GrGLRenderTarget::Desc* rtDesc) { |
bsalomon@google.com | 5bfc217 | 2011-07-29 20:29:05 +0000 | [diff] [blame] | 30 | |
| 31 | GrAssert(0 != textureDesc.fTextureID); |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 32 | |
bsalomon@google.com | 80d09b9 | 2011-11-05 21:21:13 +0000 | [diff] [blame] | 33 | fTexParams.invalidate(); |
| 34 | fTexParamsTimestamp = GrGpu::kExpiredTimestamp; |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 35 | fTexIDObj = new GrGLTexID(GPUGL->glInterface(), |
| 36 | textureDesc.fTextureID, |
bsalomon@google.com | 5877ffd | 2011-04-11 17:58:48 +0000 | [diff] [blame] | 37 | textureDesc.fOwnsID); |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 38 | fUploadFormat = textureDesc.fUploadFormat; |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 39 | fUploadType = textureDesc.fUploadType; |
| 40 | fOrientation = textureDesc.fOrientation; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 41 | |
bsalomon@google.com | 5bfc217 | 2011-07-29 20:29:05 +0000 | [diff] [blame] | 42 | if (NULL != rtDesc) { |
bsalomon@google.com | 8895a7a | 2011-02-18 16:09:55 +0000 | [diff] [blame] | 43 | // we render to the top left |
| 44 | GrGLIRect vp; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 45 | vp.fLeft = 0; |
bsalomon@google.com | 9962108 | 2011-11-15 16:47:16 +0000 | [diff] [blame^] | 46 | vp.fWidth = textureDesc.fWidth; |
| 47 | vp.fBottom = 0; |
| 48 | vp.fHeight = textureDesc.fHeight; |
bsalomon@google.com | 8895a7a | 2011-02-18 16:09:55 +0000 | [diff] [blame] | 49 | |
bsalomon@google.com | 5bfc217 | 2011-07-29 20:29:05 +0000 | [diff] [blame] | 50 | fRenderTarget = new GrGLRenderTarget(gpu, *rtDesc, vp, fTexIDObj, this); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 51 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 52 | } |
| 53 | |
bsalomon@google.com | 5bfc217 | 2011-07-29 20:29:05 +0000 | [diff] [blame] | 54 | GrGLTexture::GrGLTexture(GrGpuGL* gpu, |
bsalomon@google.com | 80d09b9 | 2011-11-05 21:21:13 +0000 | [diff] [blame] | 55 | const Desc& textureDesc) |
bsalomon@google.com | 5bfc217 | 2011-07-29 20:29:05 +0000 | [diff] [blame] | 56 | : INHERITED(gpu, |
bsalomon@google.com | 9962108 | 2011-11-15 16:47:16 +0000 | [diff] [blame^] | 57 | textureDesc.fWidth, |
| 58 | textureDesc.fHeight, |
bsalomon@google.com | 64c4fe4 | 2011-11-05 14:51:01 +0000 | [diff] [blame] | 59 | textureDesc.fConfig) { |
bsalomon@google.com | 80d09b9 | 2011-11-05 21:21:13 +0000 | [diff] [blame] | 60 | this->init(gpu, textureDesc, NULL); |
bsalomon@google.com | 5bfc217 | 2011-07-29 20:29:05 +0000 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | GrGLTexture::GrGLTexture(GrGpuGL* gpu, |
| 64 | const Desc& textureDesc, |
bsalomon@google.com | 80d09b9 | 2011-11-05 21:21:13 +0000 | [diff] [blame] | 65 | const GrGLRenderTarget::Desc& rtDesc) |
bsalomon@google.com | 5bfc217 | 2011-07-29 20:29:05 +0000 | [diff] [blame] | 66 | : INHERITED(gpu, |
bsalomon@google.com | 9962108 | 2011-11-15 16:47:16 +0000 | [diff] [blame^] | 67 | textureDesc.fWidth, |
| 68 | textureDesc.fHeight, |
bsalomon@google.com | 64c4fe4 | 2011-11-05 14:51:01 +0000 | [diff] [blame] | 69 | textureDesc.fConfig) { |
bsalomon@google.com | 80d09b9 | 2011-11-05 21:21:13 +0000 | [diff] [blame] | 70 | this->init(gpu, textureDesc, &rtDesc); |
bsalomon@google.com | 5bfc217 | 2011-07-29 20:29:05 +0000 | [diff] [blame] | 71 | } |
| 72 | |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 73 | void GrGLTexture::onRelease() { |
bsalomon@google.com | 6dcf499 | 2011-04-05 21:16:14 +0000 | [diff] [blame] | 74 | INHERITED::onRelease(); |
Scroggo | c29d7cd | 2011-06-16 13:14:21 +0000 | [diff] [blame] | 75 | GPUGL->notifyTextureDelete(this); |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 76 | if (NULL != fTexIDObj) { |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 77 | fTexIDObj->unref(); |
| 78 | fTexIDObj = NULL; |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 79 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 80 | } |
| 81 | |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 82 | void GrGLTexture::onAbandon() { |
bsalomon@google.com | 6dcf499 | 2011-04-05 21:16:14 +0000 | [diff] [blame] | 83 | INHERITED::onAbandon(); |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 84 | if (NULL != fTexIDObj) { |
| 85 | fTexIDObj->abandon(); |
| 86 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 87 | } |
| 88 | |
bsalomon@google.com | 79d2dbe | 2011-06-13 19:28:02 +0000 | [diff] [blame] | 89 | void GrGLTexture::uploadTextureData(int x, |
| 90 | int y, |
| 91 | int width, |
| 92 | int height, |
junov@google.com | 4ee7ae5 | 2011-06-30 17:30:49 +0000 | [diff] [blame] | 93 | const void* srcData, |
| 94 | size_t rowBytes) { |
bsalomon@google.com | 9d6cfd8 | 2011-11-05 13:25:21 +0000 | [diff] [blame] | 95 | GrIRect bounds = GrIRect::MakeWH(this->width(), this->height()); |
| 96 | GrIRect subrect = GrIRect::MakeXYWH(x,y,width, height); |
| 97 | if (!bounds.contains(subrect)) { |
| 98 | return; |
| 99 | } |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 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 | 9d6cfd8 | 2011-11-05 13:25:21 +0000 | [diff] [blame] | 109 | size_t bpp = GrBytesPerPixel(this->config()); |
| 110 | size_t trimRowBytes = width * bpp; |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 111 | if (!rowBytes) { |
bsalomon@google.com | 9d6cfd8 | 2011-11-05 13:25:21 +0000 | [diff] [blame] | 112 | rowBytes = trimRowBytes; |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 113 | } |
junov@google.com | 4ee7ae5 | 2011-06-30 17:30:49 +0000 | [diff] [blame] | 114 | /* |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 115 | * check whether to allocate a temporary buffer for flipping y or |
| 116 | * because our srcData has extra bytes past each row. If so, we need |
bsalomon@google.com | 7107fa7 | 2011-11-10 14:54:14 +0000 | [diff] [blame] | 117 | * to trim those off here, since GL ES may not let us specify |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 118 | * GL_UNPACK_ROW_LENGTH. |
junov@google.com | 4ee7ae5 | 2011-06-30 17:30:49 +0000 | [diff] [blame] | 119 | */ |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 120 | bool restoreGLRowLength = false; |
| 121 | bool flipY = kBottomUp_Orientation == fOrientation; |
bsalomon@google.com | 7107fa7 | 2011-11-10 14:54:14 +0000 | [diff] [blame] | 122 | if (GPUGL->glCaps().fUnpackRowLengthSupport && !flipY) { |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 123 | // can't use this for flipping, only non-neg values allowed. :( |
bsalomon@google.com | 9d6cfd8 | 2011-11-05 13:25:21 +0000 | [diff] [blame] | 124 | if (srcData && rowBytes != trimRowBytes) { |
| 125 | GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp); |
| 126 | GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength)); |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 127 | restoreGLRowLength = true; |
junov@google.com | 4ee7ae5 | 2011-06-30 17:30:49 +0000 | [diff] [blame] | 128 | } |
| 129 | } else { |
bsalomon@google.com | 9d6cfd8 | 2011-11-05 13:25:21 +0000 | [diff] [blame] | 130 | if (srcData && (trimRowBytes != rowBytes || flipY)) { |
junov@google.com | 4ee7ae5 | 2011-06-30 17:30:49 +0000 | [diff] [blame] | 131 | // copy the data into our new storage, skipping the trailing bytes |
| 132 | size_t trimSize = height * trimRowBytes; |
| 133 | const char* src = (const char*)srcData; |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 134 | if (flipY) { |
| 135 | src += (height - 1) * rowBytes; |
| 136 | } |
bsalomon@google.com | 7d4679a | 2011-09-02 22:06:24 +0000 | [diff] [blame] | 137 | char* dst = (char*)tempStorage.reset(trimSize); |
junov@google.com | 4ee7ae5 | 2011-06-30 17:30:49 +0000 | [diff] [blame] | 138 | for (int y = 0; y < height; y++) { |
| 139 | memcpy(dst, src, trimRowBytes); |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 140 | if (flipY) { |
| 141 | src -= rowBytes; |
| 142 | } else { |
| 143 | src += rowBytes; |
| 144 | } |
junov@google.com | 4ee7ae5 | 2011-06-30 17:30:49 +0000 | [diff] [blame] | 145 | dst += trimRowBytes; |
| 146 | } |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 147 | // now point srcData to our copied version |
| 148 | srcData = tempStorage.get(); |
junov@google.com | 4ee7ae5 | 2011-06-30 17:30:49 +0000 | [diff] [blame] | 149 | } |
| 150 | } |
| 151 | |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 152 | if (flipY) { |
| 153 | y = this->height() - (y + height); |
| 154 | } |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 155 | GL_CALL(BindTexture(GR_GL_TEXTURE_2D, fTexIDObj->id())); |
bsalomon@google.com | 9d6cfd8 | 2011-11-05 13:25:21 +0000 | [diff] [blame] | 156 | GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, static_cast<GrGLint>(bpp))); |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 157 | GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D, 0, x, y, width, height, |
| 158 | fUploadFormat, fUploadType, srcData)); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 159 | |
bsalomon@google.com | 7107fa7 | 2011-11-10 14:54:14 +0000 | [diff] [blame] | 160 | if (restoreGLRowLength) { |
| 161 | GrAssert(GPUGL->glCaps().fUnpackRowLengthSupport); |
| 162 | GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0)); |
junov@google.com | 4ee7ae5 | 2011-06-30 17:30:49 +0000 | [diff] [blame] | 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 | |