blob: 1acb871c7936c09ef364e88ca0125c3588aaad36 [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,
bsalomon@google.com8895a7a2011-02-18 16:09:55 +000023 GLuint stencilBits,
24 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) {
46 GR_GLEXT(fGL->extensions(), DeleteFramebuffers(1, &fTexFBOID));
47 }
48 if (fRTFBOID && fRTFBOID != fTexFBOID) {
49 GR_GLEXT(fGL->extensions(), DeleteFramebuffers(1, &fRTFBOID));
50 }
51 if (fStencilRenderbufferID) {
52 GR_GLEXT(fGL->extensions(), DeleteRenderbuffers(1, &fStencilRenderbufferID));
53 }
54 if (fMSColorRenderbufferID) {
55 GR_GLEXT(fGL->extensions(), DeleteRenderbuffers(1, &fMSColorRenderbufferID));
56 }
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
74const GLenum GrGLTexture::gWrapMode2GLWrap[] = {
75 GL_CLAMP_TO_EDGE,
76 GL_REPEAT,
77#ifdef GL_MIRRORED_REPEAT
78 GL_MIRRORED_REPEAT
79#else
80 GL_REPEAT // GL_MIRRORED_REPEAT not supported :(
81#endif
82};
83
84
85GrGLTexture::GrGLTexture(const GLTextureDesc& textureDesc,
86 const GLRenderTargetIDs& rtIDs,
bsalomon@google.comda96ea02010-12-23 16:53:57 +000087 const TexParams& initialTexParams,
bsalomon@google.comc6cf7232011-02-17 16:43:10 +000088 GrGpuGL* gl)
89 : INHERITED(textureDesc.fContentWidth,
90 textureDesc.fContentHeight,
91 textureDesc.fFormat) {
92
93 fTexParams = initialTexParams;
bsalomon@google.com1da07462011-03-10 14:51:57 +000094 fTexIDObj = new GrGLTexID(textureDesc.fTextureID);
bsalomon@google.comc6cf7232011-02-17 16:43:10 +000095 fUploadFormat = textureDesc.fUploadFormat;
96 fUploadByteCount = textureDesc.fUploadByteCount;
97 fUploadType = textureDesc.fUploadType;
98 fOrientation = textureDesc.fOrientation;
99 fAllocWidth = textureDesc.fAllocWidth;
100 fAllocHeight = textureDesc.fAllocHeight;
101 fScaleX = GrIntToScalar(textureDesc.fContentWidth) /
102 textureDesc.fAllocWidth;
103 fScaleY = GrIntToScalar(textureDesc.fContentHeight) /
104 textureDesc.fAllocHeight;
105 fRenderTarget = NULL;
106 fGpuGL = gl;
reed@google.comac10a2d2010-12-22 21:39:39 +0000107
108 GrAssert(0 != textureDesc.fTextureID);
109
110 if (rtIDs.fTexFBOID) {
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000111 // we render to the top left
112 GrGLIRect vp;
reed@google.comac10a2d2010-12-22 21:39:39 +0000113 vp.fLeft = 0;
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000114 vp.fWidth = textureDesc.fContentWidth;
115 vp.fHeight = textureDesc.fContentHeight;
116 vp.fBottom = textureDesc.fAllocHeight - textureDesc.fContentHeight;
117
bsalomon@google.com1da07462011-03-10 14:51:57 +0000118 fRenderTarget = new GrGLRenderTarget(rtIDs, fTexIDObj,
119 textureDesc.fStencilBits,
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000120 vp, this, gl);
reed@google.comac10a2d2010-12-22 21:39:39 +0000121 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000122}
123
124GrGLTexture::~GrGLTexture() {
bsalomon@google.com1da07462011-03-10 14:51:57 +0000125 fGpuGL->notifyTextureDelete(this);
126 fTexIDObj->unref();
127 GrSafeUnref(fRenderTarget);
reed@google.comac10a2d2010-12-22 21:39:39 +0000128}
129
130void GrGLTexture::abandon() {
bsalomon@google.com1da07462011-03-10 14:51:57 +0000131 fTexIDObj->abandon();
reed@google.comac10a2d2010-12-22 21:39:39 +0000132 if (NULL != fRenderTarget) {
bsalomon@google.comda96ea02010-12-23 16:53:57 +0000133 fRenderTarget->abandon();
reed@google.comac10a2d2010-12-22 21:39:39 +0000134 }
135}
136
reed@google.comac10a2d2010-12-22 21:39:39 +0000137GrRenderTarget* GrGLTexture::asRenderTarget() {
138 return (GrRenderTarget*)fRenderTarget;
139}
140
bsalomon@google.com1da07462011-03-10 14:51:57 +0000141void GrGLTexture::releaseRenderTarget() {
142 GrSafeUnref(fRenderTarget);
143 fRenderTarget = NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +0000144}
145
146void GrGLTexture::uploadTextureData(uint32_t x,
147 uint32_t y,
148 uint32_t width,
149 uint32_t height,
150 const void* srcData) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000151
152 fGpuGL->setSpareTextureUnit();
153
reed@google.comac10a2d2010-12-22 21:39:39 +0000154 // glCompressedTexSubImage2D doesn't support any formats
155 // (at least without extensions)
156 GrAssert(fUploadFormat != GR_PALETTE8_RGBA8);
157
158 // If we need to update textures that are created upside down
159 // then we have to modify this code to flip the srcData
160 GrAssert(kTopDown_Orientation == fOrientation);
bsalomon@google.com1da07462011-03-10 14:51:57 +0000161 GR_GL(BindTexture(GL_TEXTURE_2D, fTexIDObj->id()));
reed@google.comac10a2d2010-12-22 21:39:39 +0000162 GR_GL(PixelStorei(GL_UNPACK_ALIGNMENT, fUploadByteCount));
163 GR_GL(TexSubImage2D(GL_TEXTURE_2D, 0, x, y, width, height,
164 fUploadFormat, fUploadType, srcData));
165
166}
167
168intptr_t GrGLTexture::getTextureHandle() {
bsalomon@google.com1da07462011-03-10 14:51:57 +0000169 return fTexIDObj->id();
reed@google.comac10a2d2010-12-22 21:39:39 +0000170}
171
172
173