blob: 110535f3ea9643cede74c76599b2cb3f24212a4f [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
21GrGLRenderTarget::GrGLRenderTarget(const GLRenderTargetIDs& ids,
22 const GrIRect& viewport,
23 GrGLTexture* texture,
24 GrGpuGL* gl) : INHERITED(texture) {
25 fGL = gl;
26 fRTFBOID = ids.fRTFBOID;
27 fTexFBOID = ids.fTexFBOID;
28 fStencilRenderbufferID = ids.fStencilRenderbufferID;
29 fMSColorRenderbufferID = ids.fMSColorRenderbufferID;
30 fNeedsResolve = false;
31 fViewport = viewport;
32 fOwnIDs = ids.fOwnIDs;
33 // viewport should be GL's viewport with top >= bottom
34 GrAssert(viewport.height() <= 0);
35}
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) {
102 GrIRect vp;
103 vp.fLeft = 0;
104 vp.fRight = (int32_t) textureDesc.fContentWidth;
105 // viewport for GL is top > bottom
106 vp.fTop = (int32_t) textureDesc.fAllocHeight;
107 vp.fBottom = (int32_t) textureDesc.fAllocHeight -
108 (int32_t)textureDesc.fContentHeight;
109 fRenderTarget = new GrGLRenderTarget(rtIDs, vp, this, gl);
110 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000111}
112
113GrGLTexture::~GrGLTexture() {
114 // make sure we haven't been abandoned
115 if (fTextureID) {
116 fGpuGL->notifyTextureDelete(this);
117 GR_GL(DeleteTextures(1, &fTextureID));
118 }
119 delete fRenderTarget;
120}
121
122void GrGLTexture::abandon() {
123 fTextureID = 0;
124 if (NULL != fRenderTarget) {
bsalomon@google.comda96ea02010-12-23 16:53:57 +0000125 fRenderTarget->abandon();
reed@google.comac10a2d2010-12-22 21:39:39 +0000126 }
127}
128
129bool GrGLTexture::isRenderTarget() const {
130 return NULL != fRenderTarget;
131}
132
133GrRenderTarget* GrGLTexture::asRenderTarget() {
134 return (GrRenderTarget*)fRenderTarget;
135}
136
137void GrGLTexture::removeRenderTarget() {
138 GrAssert(NULL != fRenderTarget);
139 if (NULL != fRenderTarget) {
140 // must do this notify before the delete
141 fGpuGL->notifyTextureRemoveRenderTarget(this);
142 delete fRenderTarget;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000143 fRenderTarget = NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +0000144 }
145}
146
147void GrGLTexture::uploadTextureData(uint32_t x,
148 uint32_t y,
149 uint32_t width,
150 uint32_t height,
151 const void* srcData) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000152
153 fGpuGL->setSpareTextureUnit();
154
reed@google.comac10a2d2010-12-22 21:39:39 +0000155 // glCompressedTexSubImage2D doesn't support any formats
156 // (at least without extensions)
157 GrAssert(fUploadFormat != GR_PALETTE8_RGBA8);
158
159 // If we need to update textures that are created upside down
160 // then we have to modify this code to flip the srcData
161 GrAssert(kTopDown_Orientation == fOrientation);
162 GR_GL(BindTexture(GL_TEXTURE_2D, fTextureID));
reed@google.comac10a2d2010-12-22 21:39:39 +0000163 GR_GL(PixelStorei(GL_UNPACK_ALIGNMENT, fUploadByteCount));
164 GR_GL(TexSubImage2D(GL_TEXTURE_2D, 0, x, y, width, height,
165 fUploadFormat, fUploadType, srcData));
166
167}
168
169intptr_t GrGLTexture::getTextureHandle() {
170 return fTextureID;
171}
172
173
174