blob: 9fd1f57edf764920276d25817fce872e2f57b6f7 [file] [log] [blame]
reed@google.comac10a2d2010-12-22 21:39:39 +00001/*
2 Copyright 2010 Google Inc.
3
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.com8895a7a2011-02-18 16:09:55 +000021GrGLRenderTarget::GrGLRenderTarget(const GLRenderTargetIDs& ids,
22 GLuint stencilBits,
23 const GrGLIRect& viewport,
reed@google.comac10a2d2010-12-22 21:39:39 +000024 GrGLTexture* texture,
25 GrGpuGL* gl) : INHERITED(texture) {
26 fGL = gl;
27 fRTFBOID = ids.fRTFBOID;
28 fTexFBOID = ids.fTexFBOID;
29 fStencilRenderbufferID = ids.fStencilRenderbufferID;
bsalomon@google.com8895a7a2011-02-18 16:09:55 +000030 fStencilBits = stencilBits;
reed@google.comac10a2d2010-12-22 21:39:39 +000031 fMSColorRenderbufferID = ids.fMSColorRenderbufferID;
32 fNeedsResolve = false;
33 fViewport = viewport;
34 fOwnIDs = ids.fOwnIDs;
reed@google.comac10a2d2010-12-22 21:39:39 +000035}
36
37GrGLRenderTarget::~GrGLRenderTarget() {
38 fGL->notifyRenderTargetDelete(this);
39 if (fOwnIDs) {
40 if (fTexFBOID) {
41 GR_GLEXT(fGL->extensions(), DeleteFramebuffers(1, &fTexFBOID));
42 }
43 if (fRTFBOID && fRTFBOID != fTexFBOID) {
44 GR_GLEXT(fGL->extensions(), DeleteFramebuffers(1, &fRTFBOID));
45 }
46 if (fStencilRenderbufferID) {
47 GR_GLEXT(fGL->extensions(), DeleteRenderbuffers(1, &fStencilRenderbufferID));
48 }
49 if (fMSColorRenderbufferID) {
50 GR_GLEXT(fGL->extensions(), DeleteRenderbuffers(1, &fMSColorRenderbufferID));
51 }
52 }
53}
54
55void GrGLRenderTarget::abandon() {
56 fRTFBOID = 0;
57 fTexFBOID = 0;
58 fStencilRenderbufferID = 0;
59 fMSColorRenderbufferID = 0;
60}
61
62
63////////////////////////////////////////////////////////////////////////////////
64
65const GLenum GrGLTexture::gWrapMode2GLWrap[] = {
66 GL_CLAMP_TO_EDGE,
67 GL_REPEAT,
68#ifdef GL_MIRRORED_REPEAT
69 GL_MIRRORED_REPEAT
70#else
71 GL_REPEAT // GL_MIRRORED_REPEAT not supported :(
72#endif
73};
74
75
76GrGLTexture::GrGLTexture(const GLTextureDesc& textureDesc,
77 const GLRenderTargetIDs& rtIDs,
bsalomon@google.comda96ea02010-12-23 16:53:57 +000078 const TexParams& initialTexParams,
bsalomon@google.comc6cf7232011-02-17 16:43:10 +000079 GrGpuGL* gl)
80 : INHERITED(textureDesc.fContentWidth,
81 textureDesc.fContentHeight,
82 textureDesc.fFormat) {
83
84 fTexParams = initialTexParams;
85 fTextureID = textureDesc.fTextureID;
86 fUploadFormat = textureDesc.fUploadFormat;
87 fUploadByteCount = textureDesc.fUploadByteCount;
88 fUploadType = textureDesc.fUploadType;
89 fOrientation = textureDesc.fOrientation;
90 fAllocWidth = textureDesc.fAllocWidth;
91 fAllocHeight = textureDesc.fAllocHeight;
92 fScaleX = GrIntToScalar(textureDesc.fContentWidth) /
93 textureDesc.fAllocWidth;
94 fScaleY = GrIntToScalar(textureDesc.fContentHeight) /
95 textureDesc.fAllocHeight;
96 fRenderTarget = NULL;
97 fGpuGL = gl;
reed@google.comac10a2d2010-12-22 21:39:39 +000098
99 GrAssert(0 != textureDesc.fTextureID);
100
101 if (rtIDs.fTexFBOID) {
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000102 // we render to the top left
103 GrGLIRect vp;
reed@google.comac10a2d2010-12-22 21:39:39 +0000104 vp.fLeft = 0;
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000105 vp.fWidth = textureDesc.fContentWidth;
106 vp.fHeight = textureDesc.fContentHeight;
107 vp.fBottom = textureDesc.fAllocHeight - textureDesc.fContentHeight;
108
109 fRenderTarget = new GrGLRenderTarget(rtIDs, textureDesc.fStencilBits,
110 vp, this, gl);
reed@google.comac10a2d2010-12-22 21:39:39 +0000111 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000112}
113
114GrGLTexture::~GrGLTexture() {
115 // make sure we haven't been abandoned
116 if (fTextureID) {
117 fGpuGL->notifyTextureDelete(this);
118 GR_GL(DeleteTextures(1, &fTextureID));
119 }
120 delete fRenderTarget;
121}
122
123void GrGLTexture::abandon() {
124 fTextureID = 0;
125 if (NULL != fRenderTarget) {
bsalomon@google.comda96ea02010-12-23 16:53:57 +0000126 fRenderTarget->abandon();
reed@google.comac10a2d2010-12-22 21:39:39 +0000127 }
128}
129
130bool GrGLTexture::isRenderTarget() const {
131 return NULL != fRenderTarget;
132}
133
134GrRenderTarget* GrGLTexture::asRenderTarget() {
135 return (GrRenderTarget*)fRenderTarget;
136}
137
138void GrGLTexture::removeRenderTarget() {
139 GrAssert(NULL != fRenderTarget);
140 if (NULL != fRenderTarget) {
141 // must do this notify before the delete
142 fGpuGL->notifyTextureRemoveRenderTarget(this);
143 delete fRenderTarget;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000144 fRenderTarget = NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +0000145 }
146}
147
148void GrGLTexture::uploadTextureData(uint32_t x,
149 uint32_t y,
150 uint32_t width,
151 uint32_t height,
152 const void* srcData) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000153
154 fGpuGL->setSpareTextureUnit();
155
reed@google.comac10a2d2010-12-22 21:39:39 +0000156 // glCompressedTexSubImage2D doesn't support any formats
157 // (at least without extensions)
158 GrAssert(fUploadFormat != GR_PALETTE8_RGBA8);
159
160 // If we need to update textures that are created upside down
161 // then we have to modify this code to flip the srcData
162 GrAssert(kTopDown_Orientation == fOrientation);
163 GR_GL(BindTexture(GL_TEXTURE_2D, fTextureID));
reed@google.comac10a2d2010-12-22 21:39:39 +0000164 GR_GL(PixelStorei(GL_UNPACK_ALIGNMENT, fUploadByteCount));
165 GR_GL(TexSubImage2D(GL_TEXTURE_2D, 0, x, y, width, height,
166 fUploadFormat, fUploadType, srcData));
167
168}
169
170intptr_t GrGLTexture::getTextureHandle() {
171 return fTextureID;
172}
173
174
175