blob: ae991e83629b796f93b4ca71b2d229ed78830d99 [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,
bsalomon@google.comd302f142011-03-03 13:54:13 +000025 GrGpuGL* gl) : INHERITED(texture,
26 viewport.fWidth,
27 viewport.fHeight,
28 stencilBits) {
reed@google.comac10a2d2010-12-22 21:39:39 +000029 fGL = gl;
30 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;
reed@google.comac10a2d2010-12-22 21:39:39 +000037}
38
39GrGLRenderTarget::~GrGLRenderTarget() {
40 fGL->notifyRenderTargetDelete(this);
41 if (fOwnIDs) {
42 if (fTexFBOID) {
43 GR_GLEXT(fGL->extensions(), DeleteFramebuffers(1, &fTexFBOID));
44 }
45 if (fRTFBOID && fRTFBOID != fTexFBOID) {
46 GR_GLEXT(fGL->extensions(), DeleteFramebuffers(1, &fRTFBOID));
47 }
48 if (fStencilRenderbufferID) {
49 GR_GLEXT(fGL->extensions(), DeleteRenderbuffers(1, &fStencilRenderbufferID));
50 }
51 if (fMSColorRenderbufferID) {
52 GR_GLEXT(fGL->extensions(), DeleteRenderbuffers(1, &fMSColorRenderbufferID));
53 }
54 }
55}
56
57void GrGLRenderTarget::abandon() {
58 fRTFBOID = 0;
59 fTexFBOID = 0;
60 fStencilRenderbufferID = 0;
61 fMSColorRenderbufferID = 0;
62}
63
64
65////////////////////////////////////////////////////////////////////////////////
66
67const GLenum GrGLTexture::gWrapMode2GLWrap[] = {
68 GL_CLAMP_TO_EDGE,
69 GL_REPEAT,
70#ifdef GL_MIRRORED_REPEAT
71 GL_MIRRORED_REPEAT
72#else
73 GL_REPEAT // GL_MIRRORED_REPEAT not supported :(
74#endif
75};
76
77
78GrGLTexture::GrGLTexture(const GLTextureDesc& textureDesc,
79 const GLRenderTargetIDs& rtIDs,
bsalomon@google.comda96ea02010-12-23 16:53:57 +000080 const TexParams& initialTexParams,
bsalomon@google.comc6cf7232011-02-17 16:43:10 +000081 GrGpuGL* gl)
82 : INHERITED(textureDesc.fContentWidth,
83 textureDesc.fContentHeight,
84 textureDesc.fFormat) {
85
86 fTexParams = initialTexParams;
87 fTextureID = textureDesc.fTextureID;
88 fUploadFormat = textureDesc.fUploadFormat;
89 fUploadByteCount = textureDesc.fUploadByteCount;
90 fUploadType = textureDesc.fUploadType;
91 fOrientation = textureDesc.fOrientation;
92 fAllocWidth = textureDesc.fAllocWidth;
93 fAllocHeight = textureDesc.fAllocHeight;
94 fScaleX = GrIntToScalar(textureDesc.fContentWidth) /
95 textureDesc.fAllocWidth;
96 fScaleY = GrIntToScalar(textureDesc.fContentHeight) /
97 textureDesc.fAllocHeight;
98 fRenderTarget = NULL;
99 fGpuGL = gl;
reed@google.comac10a2d2010-12-22 21:39:39 +0000100
101 GrAssert(0 != textureDesc.fTextureID);
102
103 if (rtIDs.fTexFBOID) {
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000104 // we render to the top left
105 GrGLIRect vp;
reed@google.comac10a2d2010-12-22 21:39:39 +0000106 vp.fLeft = 0;
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000107 vp.fWidth = textureDesc.fContentWidth;
108 vp.fHeight = textureDesc.fContentHeight;
109 vp.fBottom = textureDesc.fAllocHeight - textureDesc.fContentHeight;
110
111 fRenderTarget = new GrGLRenderTarget(rtIDs, textureDesc.fStencilBits,
112 vp, this, gl);
reed@google.comac10a2d2010-12-22 21:39:39 +0000113 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000114}
115
116GrGLTexture::~GrGLTexture() {
117 // make sure we haven't been abandoned
118 if (fTextureID) {
119 fGpuGL->notifyTextureDelete(this);
120 GR_GL(DeleteTextures(1, &fTextureID));
121 }
122 delete fRenderTarget;
123}
124
125void GrGLTexture::abandon() {
126 fTextureID = 0;
127 if (NULL != fRenderTarget) {
bsalomon@google.comda96ea02010-12-23 16:53:57 +0000128 fRenderTarget->abandon();
reed@google.comac10a2d2010-12-22 21:39:39 +0000129 }
130}
131
132bool GrGLTexture::isRenderTarget() const {
133 return NULL != fRenderTarget;
134}
135
136GrRenderTarget* GrGLTexture::asRenderTarget() {
137 return (GrRenderTarget*)fRenderTarget;
138}
139
140void GrGLTexture::removeRenderTarget() {
141 GrAssert(NULL != fRenderTarget);
142 if (NULL != fRenderTarget) {
143 // must do this notify before the delete
144 fGpuGL->notifyTextureRemoveRenderTarget(this);
145 delete fRenderTarget;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000146 fRenderTarget = NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +0000147 }
148}
149
150void GrGLTexture::uploadTextureData(uint32_t x,
151 uint32_t y,
152 uint32_t width,
153 uint32_t height,
154 const void* srcData) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000155
156 fGpuGL->setSpareTextureUnit();
157
reed@google.comac10a2d2010-12-22 21:39:39 +0000158 // glCompressedTexSubImage2D doesn't support any formats
159 // (at least without extensions)
160 GrAssert(fUploadFormat != GR_PALETTE8_RGBA8);
161
162 // If we need to update textures that are created upside down
163 // then we have to modify this code to flip the srcData
164 GrAssert(kTopDown_Orientation == fOrientation);
165 GR_GL(BindTexture(GL_TEXTURE_2D, fTextureID));
reed@google.comac10a2d2010-12-22 21:39:39 +0000166 GR_GL(PixelStorei(GL_UNPACK_ALIGNMENT, fUploadByteCount));
167 GR_GL(TexSubImage2D(GL_TEXTURE_2D, 0, x, y, width, height,
168 fUploadFormat, fUploadType, srcData));
169
170}
171
172intptr_t GrGLTexture::getTextureHandle() {
173 return fTextureID;
174}
175
176
177