blob: ae36e4cfcc739cf59b7dbf05a86e6d9ee0964366 [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.com8895a7a2011-02-18 16:09:55 +000021GrGLRenderTarget::GrGLRenderTarget(const GLRenderTargetIDs& ids,
bsalomon@google.com1da07462011-03-10 14:51:57 +000022 GrGLTexID* texID,
twiz@google.com0f31ca72011-03-18 17:38:11 +000023 GrGLuint stencilBits,
bsalomon@google.com8895a7a2011-02-18 16:09:55 +000024 const GrGLIRect& viewport,
reed@google.comac10a2d2010-12-22 21:39:39 +000025 GrGLTexture* texture,
bsalomon@google.comd302f142011-03-03 13:54:13 +000026 GrGpuGL* gl) : INHERITED(texture,
27 viewport.fWidth,
28 viewport.fHeight,
29 stencilBits) {
reed@google.comac10a2d2010-12-22 21:39:39 +000030 fGL = gl;
31 fRTFBOID = ids.fRTFBOID;
32 fTexFBOID = ids.fTexFBOID;
33 fStencilRenderbufferID = ids.fStencilRenderbufferID;
34 fMSColorRenderbufferID = ids.fMSColorRenderbufferID;
35 fNeedsResolve = false;
36 fViewport = viewport;
37 fOwnIDs = ids.fOwnIDs;
bsalomon@google.com1da07462011-03-10 14:51:57 +000038 fTexIDObj = texID;
39 GrSafeRef(fTexIDObj);
reed@google.comac10a2d2010-12-22 21:39:39 +000040}
41
42GrGLRenderTarget::~GrGLRenderTarget() {
43 fGL->notifyRenderTargetDelete(this);
44 if (fOwnIDs) {
45 if (fTexFBOID) {
twiz@google.com59a190b2011-03-14 21:23:01 +000046 GR_GL(DeleteFramebuffers(1, &fTexFBOID));
reed@google.comac10a2d2010-12-22 21:39:39 +000047 }
48 if (fRTFBOID && fRTFBOID != fTexFBOID) {
twiz@google.com59a190b2011-03-14 21:23:01 +000049 GR_GL(DeleteFramebuffers(1, &fRTFBOID));
reed@google.comac10a2d2010-12-22 21:39:39 +000050 }
51 if (fStencilRenderbufferID) {
twiz@google.com59a190b2011-03-14 21:23:01 +000052 GR_GL(DeleteRenderbuffers(1, &fStencilRenderbufferID));
reed@google.comac10a2d2010-12-22 21:39:39 +000053 }
54 if (fMSColorRenderbufferID) {
twiz@google.com59a190b2011-03-14 21:23:01 +000055 GR_GL(DeleteRenderbuffers(1, &fMSColorRenderbufferID));
reed@google.comac10a2d2010-12-22 21:39:39 +000056 }
57 }
bsalomon@google.com1da07462011-03-10 14:51:57 +000058 GrSafeUnref(fTexIDObj);
reed@google.comac10a2d2010-12-22 21:39:39 +000059}
60
61void GrGLRenderTarget::abandon() {
62 fRTFBOID = 0;
63 fTexFBOID = 0;
64 fStencilRenderbufferID = 0;
65 fMSColorRenderbufferID = 0;
bsalomon@google.com1da07462011-03-10 14:51:57 +000066 if (NULL != fTexIDObj) {
67 fTexIDObj->abandon();
68 }
reed@google.comac10a2d2010-12-22 21:39:39 +000069}
70
71
72////////////////////////////////////////////////////////////////////////////////
73
twiz@google.comb65e0cb2011-03-18 20:41:44 +000074const GrGLenum* GrGLTexture::WrapMode2GLWrap() {
75 static const GrGLenum mirrorRepeatModes[] = {
76 GR_GL_CLAMP_TO_EDGE,
77 GR_GL_REPEAT,
78 GR_GL_MIRRORED_REPEAT
79 };
80
81 static const GrGLenum repeatModes[] = {
82 GR_GL_CLAMP_TO_EDGE,
83 GR_GL_REPEAT,
84 GR_GL_REPEAT
85 };
86
87 if (GR_GL_SUPPORT_ES1 && !GR_GL_SUPPORT_ES2) {
88 return repeatModes; // GL_MIRRORED_REPEAT not supported.
89 } else {
90 return mirrorRepeatModes;
91 }
reed@google.comac10a2d2010-12-22 21:39:39 +000092};
93
94
95GrGLTexture::GrGLTexture(const GLTextureDesc& textureDesc,
96 const GLRenderTargetIDs& rtIDs,
bsalomon@google.comda96ea02010-12-23 16:53:57 +000097 const TexParams& initialTexParams,
bsalomon@google.comc6cf7232011-02-17 16:43:10 +000098 GrGpuGL* gl)
99 : INHERITED(textureDesc.fContentWidth,
100 textureDesc.fContentHeight,
101 textureDesc.fFormat) {
102
103 fTexParams = initialTexParams;
bsalomon@google.com1da07462011-03-10 14:51:57 +0000104 fTexIDObj = new GrGLTexID(textureDesc.fTextureID);
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000105 fUploadFormat = textureDesc.fUploadFormat;
106 fUploadByteCount = textureDesc.fUploadByteCount;
107 fUploadType = textureDesc.fUploadType;
108 fOrientation = textureDesc.fOrientation;
109 fAllocWidth = textureDesc.fAllocWidth;
110 fAllocHeight = textureDesc.fAllocHeight;
111 fScaleX = GrIntToScalar(textureDesc.fContentWidth) /
112 textureDesc.fAllocWidth;
113 fScaleY = GrIntToScalar(textureDesc.fContentHeight) /
114 textureDesc.fAllocHeight;
115 fRenderTarget = NULL;
116 fGpuGL = gl;
reed@google.comac10a2d2010-12-22 21:39:39 +0000117
118 GrAssert(0 != textureDesc.fTextureID);
119
120 if (rtIDs.fTexFBOID) {
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000121 // we render to the top left
122 GrGLIRect vp;
reed@google.comac10a2d2010-12-22 21:39:39 +0000123 vp.fLeft = 0;
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000124 vp.fWidth = textureDesc.fContentWidth;
125 vp.fHeight = textureDesc.fContentHeight;
126 vp.fBottom = textureDesc.fAllocHeight - textureDesc.fContentHeight;
127
bsalomon@google.com1da07462011-03-10 14:51:57 +0000128 fRenderTarget = new GrGLRenderTarget(rtIDs, fTexIDObj,
129 textureDesc.fStencilBits,
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000130 vp, this, gl);
reed@google.comac10a2d2010-12-22 21:39:39 +0000131 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000132}
133
134GrGLTexture::~GrGLTexture() {
bsalomon@google.com1da07462011-03-10 14:51:57 +0000135 fGpuGL->notifyTextureDelete(this);
136 fTexIDObj->unref();
137 GrSafeUnref(fRenderTarget);
reed@google.comac10a2d2010-12-22 21:39:39 +0000138}
139
140void GrGLTexture::abandon() {
bsalomon@google.com1da07462011-03-10 14:51:57 +0000141 fTexIDObj->abandon();
reed@google.comac10a2d2010-12-22 21:39:39 +0000142 if (NULL != fRenderTarget) {
bsalomon@google.comda96ea02010-12-23 16:53:57 +0000143 fRenderTarget->abandon();
reed@google.comac10a2d2010-12-22 21:39:39 +0000144 }
145}
146
reed@google.comac10a2d2010-12-22 21:39:39 +0000147GrRenderTarget* GrGLTexture::asRenderTarget() {
148 return (GrRenderTarget*)fRenderTarget;
149}
150
bsalomon@google.com1da07462011-03-10 14:51:57 +0000151void GrGLTexture::releaseRenderTarget() {
152 GrSafeUnref(fRenderTarget);
153 fRenderTarget = NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +0000154}
155
156void GrGLTexture::uploadTextureData(uint32_t x,
157 uint32_t y,
158 uint32_t width,
159 uint32_t height,
160 const void* srcData) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000161
162 fGpuGL->setSpareTextureUnit();
163
reed@google.comac10a2d2010-12-22 21:39:39 +0000164 // glCompressedTexSubImage2D doesn't support any formats
165 // (at least without extensions)
bsalomon@google.comc312bf92011-03-21 21:10:33 +0000166 GrAssert(fUploadFormat != GR_GL_PALETTE8_RGBA8);
reed@google.comac10a2d2010-12-22 21:39:39 +0000167
168 // If we need to update textures that are created upside down
169 // then we have to modify this code to flip the srcData
170 GrAssert(kTopDown_Orientation == fOrientation);
twiz@google.com0f31ca72011-03-18 17:38:11 +0000171 GR_GL(BindTexture(GR_GL_TEXTURE_2D, fTexIDObj->id()));
172 GR_GL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, fUploadByteCount));
173 GR_GL(TexSubImage2D(GR_GL_TEXTURE_2D, 0, x, y, width, height,
reed@google.comac10a2d2010-12-22 21:39:39 +0000174 fUploadFormat, fUploadType, srcData));
175
176}
177
178intptr_t GrGLTexture::getTextureHandle() {
bsalomon@google.com1da07462011-03-10 14:51:57 +0000179 return fTexIDObj->id();
reed@google.comac10a2d2010-12-22 21:39:39 +0000180}
181
182
183