blob: ce9345b7eda801f3fa9906f62f5ed496b556be54 [file] [log] [blame]
reed@google.comac10a2d2010-12-22 21:39:39 +00001/*
bsalomon@google.com1da07462011-03-10 14:51:57 +00002 Copyright 2011 Google Inc.
reed@google.comac10a2d2010-12-22 21:39:39 +00003
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
bsalomon@google.com8fe72472011-03-30 21:26:44 +000021#define GPUGL static_cast<GrGpuGL*>(getGpu())
22
23GrGLRenderTarget::GrGLRenderTarget(GrGpuGL* gpu,
24 const GLRenderTargetIDs& ids,
bsalomon@google.com1da07462011-03-10 14:51:57 +000025 GrGLTexID* texID,
twiz@google.com0f31ca72011-03-18 17:38:11 +000026 GrGLuint stencilBits,
bsalomon@google.com8895a7a2011-02-18 16:09:55 +000027 const GrGLIRect& viewport,
bsalomon@google.com8fe72472011-03-30 21:26:44 +000028 GrGLTexture* texture)
29 : INHERITED(gpu, texture, viewport.fWidth, viewport.fHeight, stencilBits) {
reed@google.comac10a2d2010-12-22 21:39:39 +000030 fRTFBOID = ids.fRTFBOID;
31 fTexFBOID = ids.fTexFBOID;
32 fStencilRenderbufferID = ids.fStencilRenderbufferID;
33 fMSColorRenderbufferID = ids.fMSColorRenderbufferID;
34 fNeedsResolve = false;
35 fViewport = viewport;
36 fOwnIDs = ids.fOwnIDs;
bsalomon@google.com1da07462011-03-10 14:51:57 +000037 fTexIDObj = texID;
38 GrSafeRef(fTexIDObj);
reed@google.comac10a2d2010-12-22 21:39:39 +000039}
40
bsalomon@google.com8fe72472011-03-30 21:26:44 +000041void GrGLRenderTarget::onRelease() {
reed@google.comac10a2d2010-12-22 21:39:39 +000042 if (fOwnIDs) {
43 if (fTexFBOID) {
bsalomon@google.com8fe72472011-03-30 21:26:44 +000044 GPUGL->notifyRenderTargetDelete(this);
twiz@google.com59a190b2011-03-14 21:23:01 +000045 GR_GL(DeleteFramebuffers(1, &fTexFBOID));
reed@google.comac10a2d2010-12-22 21:39:39 +000046 }
47 if (fRTFBOID && fRTFBOID != fTexFBOID) {
twiz@google.com59a190b2011-03-14 21:23:01 +000048 GR_GL(DeleteFramebuffers(1, &fRTFBOID));
reed@google.comac10a2d2010-12-22 21:39:39 +000049 }
50 if (fStencilRenderbufferID) {
twiz@google.com59a190b2011-03-14 21:23:01 +000051 GR_GL(DeleteRenderbuffers(1, &fStencilRenderbufferID));
reed@google.comac10a2d2010-12-22 21:39:39 +000052 }
53 if (fMSColorRenderbufferID) {
twiz@google.com59a190b2011-03-14 21:23:01 +000054 GR_GL(DeleteRenderbuffers(1, &fMSColorRenderbufferID));
reed@google.comac10a2d2010-12-22 21:39:39 +000055 }
56 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +000057 fRTFBOID = 0;
58 fTexFBOID = 0;
59 fStencilRenderbufferID = 0;
60 fMSColorRenderbufferID = 0;
bsalomon@google.com1da07462011-03-10 14:51:57 +000061 GrSafeUnref(fTexIDObj);
bsalomon@google.com8fe72472011-03-30 21:26:44 +000062 fTexIDObj = NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +000063}
64
bsalomon@google.com8fe72472011-03-30 21:26:44 +000065void GrGLRenderTarget::onAbandon() {
reed@google.comac10a2d2010-12-22 21:39:39 +000066 fRTFBOID = 0;
67 fTexFBOID = 0;
68 fStencilRenderbufferID = 0;
69 fMSColorRenderbufferID = 0;
bsalomon@google.com1da07462011-03-10 14:51:57 +000070 if (NULL != fTexIDObj) {
71 fTexIDObj->abandon();
bsalomon@google.com8fe72472011-03-30 21:26:44 +000072 fTexIDObj = NULL;
bsalomon@google.com1da07462011-03-10 14:51:57 +000073 }
reed@google.comac10a2d2010-12-22 21:39:39 +000074}
75
76
77////////////////////////////////////////////////////////////////////////////////
78
twiz@google.comb65e0cb2011-03-18 20:41:44 +000079const GrGLenum* GrGLTexture::WrapMode2GLWrap() {
80 static const GrGLenum mirrorRepeatModes[] = {
81 GR_GL_CLAMP_TO_EDGE,
82 GR_GL_REPEAT,
83 GR_GL_MIRRORED_REPEAT
84 };
85
86 static const GrGLenum repeatModes[] = {
87 GR_GL_CLAMP_TO_EDGE,
88 GR_GL_REPEAT,
89 GR_GL_REPEAT
90 };
91
92 if (GR_GL_SUPPORT_ES1 && !GR_GL_SUPPORT_ES2) {
93 return repeatModes; // GL_MIRRORED_REPEAT not supported.
94 } else {
95 return mirrorRepeatModes;
96 }
reed@google.comac10a2d2010-12-22 21:39:39 +000097};
98
99
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000100GrGLTexture::GrGLTexture(GrGpuGL* gpu,
101 const GLTextureDesc& textureDesc,
reed@google.comac10a2d2010-12-22 21:39:39 +0000102 const GLRenderTargetIDs& rtIDs,
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000103 const TexParams& initialTexParams)
104 : INHERITED(gpu,
105 textureDesc.fContentWidth,
106 textureDesc.fContentHeight,
107 textureDesc.fFormat) {
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000108
109 fTexParams = initialTexParams;
bsalomon@google.com1da07462011-03-10 14:51:57 +0000110 fTexIDObj = new GrGLTexID(textureDesc.fTextureID);
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000111 fUploadFormat = textureDesc.fUploadFormat;
112 fUploadByteCount = textureDesc.fUploadByteCount;
113 fUploadType = textureDesc.fUploadType;
114 fOrientation = textureDesc.fOrientation;
115 fAllocWidth = textureDesc.fAllocWidth;
116 fAllocHeight = textureDesc.fAllocHeight;
117 fScaleX = GrIntToScalar(textureDesc.fContentWidth) /
118 textureDesc.fAllocWidth;
119 fScaleY = GrIntToScalar(textureDesc.fContentHeight) /
120 textureDesc.fAllocHeight;
reed@google.comac10a2d2010-12-22 21:39:39 +0000121
122 GrAssert(0 != textureDesc.fTextureID);
123
124 if (rtIDs.fTexFBOID) {
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000125 // we render to the top left
126 GrGLIRect vp;
reed@google.comac10a2d2010-12-22 21:39:39 +0000127 vp.fLeft = 0;
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000128 vp.fWidth = textureDesc.fContentWidth;
129 vp.fHeight = textureDesc.fContentHeight;
130 vp.fBottom = textureDesc.fAllocHeight - textureDesc.fContentHeight;
131
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000132 fRenderTarget = new GrGLRenderTarget(gpu, rtIDs, fTexIDObj,
bsalomon@google.com1da07462011-03-10 14:51:57 +0000133 textureDesc.fStencilBits,
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000134 vp, this);
reed@google.comac10a2d2010-12-22 21:39:39 +0000135 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000136}
137
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000138void GrGLTexture::onRelease() {
bsalomon@google.com6dcf4992011-04-05 21:16:14 +0000139 INHERITED::onRelease();
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000140 if (NULL != fTexIDObj) {
141 GPUGL->notifyTextureDelete(this);
142 fTexIDObj->unref();
143 fTexIDObj = NULL;
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000144 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000145}
146
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000147void GrGLTexture::onAbandon() {
bsalomon@google.com6dcf4992011-04-05 21:16:14 +0000148 INHERITED::onAbandon();
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000149 if (NULL != fTexIDObj) {
150 fTexIDObj->abandon();
151 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000152}
153
154void GrGLTexture::uploadTextureData(uint32_t x,
155 uint32_t y,
156 uint32_t width,
157 uint32_t height,
158 const void* srcData) {
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000159
160 GPUGL->setSpareTextureUnit();
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000161
reed@google.comac10a2d2010-12-22 21:39:39 +0000162 // glCompressedTexSubImage2D doesn't support any formats
163 // (at least without extensions)
bsalomon@google.comc312bf92011-03-21 21:10:33 +0000164 GrAssert(fUploadFormat != GR_GL_PALETTE8_RGBA8);
reed@google.comac10a2d2010-12-22 21:39:39 +0000165
166 // If we need to update textures that are created upside down
167 // then we have to modify this code to flip the srcData
168 GrAssert(kTopDown_Orientation == fOrientation);
twiz@google.com0f31ca72011-03-18 17:38:11 +0000169 GR_GL(BindTexture(GR_GL_TEXTURE_2D, fTexIDObj->id()));
170 GR_GL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, fUploadByteCount));
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000171 GR_GL(TexSubImage2D(GR_GL_TEXTURE_2D, 0, x, y, width, height,
reed@google.comac10a2d2010-12-22 21:39:39 +0000172 fUploadFormat, fUploadType, srcData));
173
174}
175
176intptr_t GrGLTexture::getTextureHandle() {
bsalomon@google.com1da07462011-03-10 14:51:57 +0000177 return fTexIDObj->id();
reed@google.comac10a2d2010-12-22 21:39:39 +0000178}
179