blob: bd157d26bb1db5a1ceac68ec570a34dbbbab4ba3 [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;
121 fRenderTarget = NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +0000122
123 GrAssert(0 != textureDesc.fTextureID);
124
125 if (rtIDs.fTexFBOID) {
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000126 // we render to the top left
127 GrGLIRect vp;
reed@google.comac10a2d2010-12-22 21:39:39 +0000128 vp.fLeft = 0;
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000129 vp.fWidth = textureDesc.fContentWidth;
130 vp.fHeight = textureDesc.fContentHeight;
131 vp.fBottom = textureDesc.fAllocHeight - textureDesc.fContentHeight;
132
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000133 fRenderTarget = new GrGLRenderTarget(gpu, rtIDs, fTexIDObj,
bsalomon@google.com1da07462011-03-10 14:51:57 +0000134 textureDesc.fStencilBits,
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000135 vp, this);
reed@google.comac10a2d2010-12-22 21:39:39 +0000136 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000137}
138
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000139void GrGLTexture::onRelease() {
140 if (NULL != fTexIDObj) {
141 GPUGL->notifyTextureDelete(this);
142 fTexIDObj->unref();
143 fTexIDObj = NULL;
144 GrSafeUnref(fRenderTarget);
145 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000146}
147
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000148void GrGLTexture::onAbandon() {
149 if (NULL != fTexIDObj) {
150 fTexIDObj->abandon();
151 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000152 if (NULL != fRenderTarget) {
bsalomon@google.comda96ea02010-12-23 16:53:57 +0000153 fRenderTarget->abandon();
reed@google.comac10a2d2010-12-22 21:39:39 +0000154 }
155}
156
reed@google.comac10a2d2010-12-22 21:39:39 +0000157GrRenderTarget* GrGLTexture::asRenderTarget() {
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000158 return fRenderTarget;
reed@google.comac10a2d2010-12-22 21:39:39 +0000159}
160
bsalomon@google.com1da07462011-03-10 14:51:57 +0000161void GrGLTexture::releaseRenderTarget() {
162 GrSafeUnref(fRenderTarget);
163 fRenderTarget = NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +0000164}
165
166void GrGLTexture::uploadTextureData(uint32_t x,
167 uint32_t y,
168 uint32_t width,
169 uint32_t height,
170 const void* srcData) {
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000171
172 GPUGL->setSpareTextureUnit();
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000173
reed@google.comac10a2d2010-12-22 21:39:39 +0000174 // glCompressedTexSubImage2D doesn't support any formats
175 // (at least without extensions)
bsalomon@google.comc312bf92011-03-21 21:10:33 +0000176 GrAssert(fUploadFormat != GR_GL_PALETTE8_RGBA8);
reed@google.comac10a2d2010-12-22 21:39:39 +0000177
178 // If we need to update textures that are created upside down
179 // then we have to modify this code to flip the srcData
180 GrAssert(kTopDown_Orientation == fOrientation);
twiz@google.com0f31ca72011-03-18 17:38:11 +0000181 GR_GL(BindTexture(GR_GL_TEXTURE_2D, fTexIDObj->id()));
182 GR_GL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, fUploadByteCount));
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000183 GR_GL(TexSubImage2D(GR_GL_TEXTURE_2D, 0, x, y, width, height,
reed@google.comac10a2d2010-12-22 21:39:39 +0000184 fUploadFormat, fUploadType, srcData));
185
186}
187
188intptr_t GrGLTexture::getTextureHandle() {
bsalomon@google.com1da07462011-03-10 14:51:57 +0000189 return fTexIDObj->id();
reed@google.comac10a2d2010-12-22 21:39:39 +0000190}
191
192
193