blob: 6ce6089693c3bd49a445ec5485eb3a698b4c3b72 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@google.comac10a2d2010-12-22 21:39:39 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
reed@google.comac10a2d2010-12-22 21:39:39 +00007 */
8
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
reed@google.comac10a2d2010-12-22 21:39:39 +000010#include "GrGpuGL.h"
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000011#include "GrGLStencilBuffer.h"
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +000012#include "GrTypes.h"
bsalomon@google.com3582bf92011-06-30 21:32:31 +000013#include "SkTemplates.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000014
twiz@google.com0f31ca72011-03-18 17:38:11 +000015static const GrGLuint GR_MAX_GLUINT = ~0;
16static const GrGLint GR_INVAL_GLINT = ~0;
reed@google.comac10a2d2010-12-22 21:39:39 +000017
bsalomon@google.com0b77d682011-08-19 13:28:54 +000018#define GL_CALL(X) GR_GL_CALL(this->glInterface(), X)
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +000019#define GL_CALL_RET(RET, X) GR_GL_CALL_RET(this->glInterface(), RET, X)
bsalomon@google.com0b77d682011-08-19 13:28:54 +000020
bsalomon@google.com316f99232011-01-13 21:28:12 +000021// we use a spare texture unit to avoid
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000022// mucking with the state of any of the stages.
tomhudson@google.com93813632011-10-27 20:21:16 +000023static const int SPARE_TEX_UNIT = GrDrawState::kNumStages;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000024
reed@google.comac10a2d2010-12-22 21:39:39 +000025#define SKIP_CACHE_CHECK true
26
bsalomon@google.com4f3c2532012-01-19 16:16:52 +000027#if GR_GL_CHECK_ALLOC_WITH_GET_ERROR
28 #define CLEAR_ERROR_BEFORE_ALLOC(iface) GrGLClearErr(iface)
29 #define GL_ALLOC_CALL(iface, call) GR_GL_CALL_NOERRCHECK(iface, call)
30 #define CHECK_ALLOC_ERROR(iface) GR_GL_GET_ERROR(iface)
31#else
32 #define CLEAR_ERROR_BEFORE_ALLOC(iface)
33 #define GL_ALLOC_CALL(iface, call) GR_GL_CALL(iface, call)
34 #define CHECK_ALLOC_ERROR(iface) GR_GL_NO_ERROR
35#endif
36
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +000037///////////////////////////////////////////////////////////////////////////////
38
39void GrGpuGL::GLCaps::markConfigAsValidColorAttachment(GrPixelConfig config) {
40#if !GR_GL_CHECK_FBO_STATUS_ONCE_PER_FORMAT
41 return;
42#endif
43 GrAssert(config < kGrPixelConfigCount);
44 int u32Idx = config / 32;
45 int bitIdx = config % 32;
46 fVerifiedColorAttachmentConfigs[u32Idx] |= (1 << bitIdx);
47}
48
49bool GrGpuGL::GLCaps::isConfigVerifiedColorAttachment(
50 GrPixelConfig config) const {
51#if !GR_GL_CHECK_FBO_STATUS_ONCE_PER_FORMAT
52 return false;
53#endif
54 GrAssert((unsigned)config < kGrPixelConfigCount);
55 int u32Idx = config / 32;
56 int bitIdx = config % 32;
57 return SkToBool(fVerifiedColorAttachmentConfigs[u32Idx] & (1 << bitIdx));
58}
59
60void GrGpuGL::GLCaps::markColorConfigAndStencilFormatAsVerified(
61 GrPixelConfig config,
62 const GrGLStencilBuffer::Format& format) {
63#if !GR_GL_CHECK_FBO_STATUS_ONCE_PER_FORMAT
64 return;
65#endif
66 GrAssert((unsigned)config < kGrPixelConfigCount);
67 int count = fStencilFormats.count();
68 // we expect a really small number of possible formats so linear search
69 // should be OK
70 GrAssert(count < 16);
71 for (int i = 0; i < count; ++i) {
72 if (format.fInternalFormat ==
73 fStencilFormats[i].fFormat.fInternalFormat) {
74 int u32Idx = config / 32;
75 int bitIdx = config % 32;
76 fStencilFormats[i].fVerifiedColorConfigs[u32Idx] |= (1 << bitIdx);
77 return;
78 }
79 }
80 SkDEBUGFAIL("Why are we seeing a stencil format that GLCaps doesn't know about.");
81}
82
83bool GrGpuGL::GLCaps::isColorConfigAndStencilFormatVerified(
84 GrPixelConfig config,
85 const GrGLStencilBuffer::Format& format) const {
86#if !GR_GL_CHECK_FBO_STATUS_ONCE_PER_FORMAT
87 return false;
88#endif
89 GrAssert((unsigned)config < kGrPixelConfigCount);
90 int count = fStencilFormats.count();
91 // we expect a really small number of possible formats so linear search
92 // should be OK
93 GrAssert(count < 16);
94 for (int i = 0; i < count; ++i) {
95 if (format.fInternalFormat ==
96 fStencilFormats[i].fFormat.fInternalFormat) {
97 int u32Idx = config / 32;
98 int bitIdx = config % 32;
99 return SkToBool(fStencilFormats[i].fVerifiedColorConfigs[u32Idx] &
100 (1 << bitIdx));
101 }
102 }
103 SkDEBUGFAIL("Why are we seeing a stencil format that GLCaps doesn't know about.");
104 return false;
105}
106
107///////////////////////////////////////////////////////////////////////////////
108
twiz@google.com0f31ca72011-03-18 17:38:11 +0000109static const GrGLenum gXfermodeCoeff2Blend[] = {
110 GR_GL_ZERO,
111 GR_GL_ONE,
112 GR_GL_SRC_COLOR,
113 GR_GL_ONE_MINUS_SRC_COLOR,
114 GR_GL_DST_COLOR,
115 GR_GL_ONE_MINUS_DST_COLOR,
116 GR_GL_SRC_ALPHA,
117 GR_GL_ONE_MINUS_SRC_ALPHA,
118 GR_GL_DST_ALPHA,
119 GR_GL_ONE_MINUS_DST_ALPHA,
120 GR_GL_CONSTANT_COLOR,
121 GR_GL_ONE_MINUS_CONSTANT_COLOR,
122 GR_GL_CONSTANT_ALPHA,
123 GR_GL_ONE_MINUS_CONSTANT_ALPHA,
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000124
125 // extended blend coeffs
126 GR_GL_SRC1_COLOR,
127 GR_GL_ONE_MINUS_SRC1_COLOR,
128 GR_GL_SRC1_ALPHA,
129 GR_GL_ONE_MINUS_SRC1_ALPHA,
reed@google.comac10a2d2010-12-22 21:39:39 +0000130};
131
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000132bool GrGpuGL::BlendCoeffReferencesConstant(GrBlendCoeff coeff) {
bsalomon@google.com080773c2011-03-15 19:09:25 +0000133 static const bool gCoeffReferencesBlendConst[] = {
134 false,
135 false,
136 false,
137 false,
138 false,
139 false,
140 false,
141 false,
142 false,
143 false,
144 true,
145 true,
146 true,
147 true,
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000148
149 // extended blend coeffs
150 false,
151 false,
152 false,
153 false,
bsalomon@google.com080773c2011-03-15 19:09:25 +0000154 };
155 return gCoeffReferencesBlendConst[coeff];
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000156 GR_STATIC_ASSERT(kTotalBlendCoeffCount == GR_ARRAY_COUNT(gCoeffReferencesBlendConst));
157
158 GR_STATIC_ASSERT(0 == kZero_BlendCoeff);
159 GR_STATIC_ASSERT(1 == kOne_BlendCoeff);
160 GR_STATIC_ASSERT(2 == kSC_BlendCoeff);
161 GR_STATIC_ASSERT(3 == kISC_BlendCoeff);
162 GR_STATIC_ASSERT(4 == kDC_BlendCoeff);
163 GR_STATIC_ASSERT(5 == kIDC_BlendCoeff);
164 GR_STATIC_ASSERT(6 == kSA_BlendCoeff);
165 GR_STATIC_ASSERT(7 == kISA_BlendCoeff);
166 GR_STATIC_ASSERT(8 == kDA_BlendCoeff);
167 GR_STATIC_ASSERT(9 == kIDA_BlendCoeff);
168 GR_STATIC_ASSERT(10 == kConstC_BlendCoeff);
169 GR_STATIC_ASSERT(11 == kIConstC_BlendCoeff);
170 GR_STATIC_ASSERT(12 == kConstA_BlendCoeff);
171 GR_STATIC_ASSERT(13 == kIConstA_BlendCoeff);
172
173 GR_STATIC_ASSERT(14 == kS2C_BlendCoeff);
174 GR_STATIC_ASSERT(15 == kIS2C_BlendCoeff);
175 GR_STATIC_ASSERT(16 == kS2A_BlendCoeff);
176 GR_STATIC_ASSERT(17 == kIS2A_BlendCoeff);
177
178 // assertion for gXfermodeCoeff2Blend have to be in GrGpu scope
179 GR_STATIC_ASSERT(kTotalBlendCoeffCount == GR_ARRAY_COUNT(gXfermodeCoeff2Blend));
bsalomon@google.com080773c2011-03-15 19:09:25 +0000180}
181
reed@google.comac10a2d2010-12-22 21:39:39 +0000182///////////////////////////////////////////////////////////////////////////////
183
bsalomon@google.comd302f142011-03-03 13:54:13 +0000184void GrGpuGL::AdjustTextureMatrix(const GrGLTexture* texture,
185 GrSamplerState::SampleMode mode,
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000186 GrMatrix* matrix) {
187 GrAssert(NULL != texture);
188 GrAssert(NULL != matrix);
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000189 GrGLTexture::Orientation orientation = texture->orientation();
190 if (GrGLTexture::kBottomUp_Orientation == orientation) {
191 GrMatrix invY;
192 invY.setAll(GR_Scalar1, 0, 0,
193 0, -GR_Scalar1, GR_Scalar1,
194 0, 0, GrMatrix::I()[8]);
195 matrix->postConcat(invY);
196 } else {
197 GrAssert(GrGLTexture::kTopDown_Orientation == orientation);
198 }
199}
200
bsalomon@google.comd302f142011-03-03 13:54:13 +0000201bool GrGpuGL::TextureMatrixIsIdentity(const GrGLTexture* texture,
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000202 const GrSamplerState& sampler) {
203 GrAssert(NULL != texture);
204 if (!sampler.getMatrix().isIdentity()) {
205 return false;
206 }
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000207 GrGLTexture::Orientation orientation = texture->orientation();
208 if (GrGLTexture::kBottomUp_Orientation == orientation) {
209 return false;
210 } else {
211 GrAssert(GrGLTexture::kTopDown_Orientation == orientation);
212 }
213 return true;
214}
215
216///////////////////////////////////////////////////////////////////////////////
217
bsalomon@google.com42ab7ea2011-01-19 17:19:40 +0000218static bool gPrintStartupSpew;
219
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000220static bool fbo_test(const GrGLInterface* gl, int w, int h) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000221
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000222 GR_GL_CALL(gl, ActiveTexture(GR_GL_TEXTURE0 + SPARE_TEX_UNIT));
bsalomon@google.com316f99232011-01-13 21:28:12 +0000223
twiz@google.com0f31ca72011-03-18 17:38:11 +0000224 GrGLuint testFBO;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000225 GR_GL_CALL(gl, GenFramebuffers(1, &testFBO));
226 GR_GL_CALL(gl, BindFramebuffer(GR_GL_FRAMEBUFFER, testFBO));
twiz@google.com0f31ca72011-03-18 17:38:11 +0000227 GrGLuint testRTTex;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000228 GR_GL_CALL(gl, GenTextures(1, &testRTTex));
229 GR_GL_CALL(gl, BindTexture(GR_GL_TEXTURE_2D, testRTTex));
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +0000230 // some implementations require texture to be mip-map complete before
231 // FBO with level 0 bound as color attachment will be framebuffer complete.
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000232 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000233 GR_GL_TEXTURE_MIN_FILTER,
234 GR_GL_NEAREST));
235 GR_GL_CALL(gl, TexImage2D(GR_GL_TEXTURE_2D, 0, GR_GL_RGBA, w, h,
236 0, GR_GL_RGBA, GR_GL_UNSIGNED_BYTE, NULL));
237 GR_GL_CALL(gl, BindTexture(GR_GL_TEXTURE_2D, 0));
238 GR_GL_CALL(gl, FramebufferTexture2D(GR_GL_FRAMEBUFFER,
239 GR_GL_COLOR_ATTACHMENT0,
240 GR_GL_TEXTURE_2D, testRTTex, 0));
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000241 GrGLenum status;
242 GR_GL_CALL_RET(gl, status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000243 GR_GL_CALL(gl, DeleteFramebuffers(1, &testFBO));
244 GR_GL_CALL(gl, DeleteTextures(1, &testRTTex));
bsalomon@google.com316f99232011-01-13 21:28:12 +0000245
bsalomon@google.comc312bf92011-03-21 21:10:33 +0000246 return status == GR_GL_FRAMEBUFFER_COMPLETE;
reed@google.comac10a2d2010-12-22 21:39:39 +0000247}
248
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000249GrGpuGL::GrGpuGL(const GrGLContextInfo& ctxInfo) : fGLContextInfo(ctxInfo) {
250
251 GrAssert(ctxInfo.isInitialized());
tomhudson@google.com747bf292011-06-14 18:16:52 +0000252
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000253 fPrintedCaps = false;
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000254
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000255 GrGLClearErr(fGLContextInfo.interface());
reed@google.comac10a2d2010-12-22 21:39:39 +0000256
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000257 if (gPrintStartupSpew) {
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000258 const GrGLubyte* ext;
259 GL_CALL_RET(ext, GetString(GR_GL_EXTENSIONS));
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000260 const GrGLubyte* vendor;
261 const GrGLubyte* renderer;
262 const GrGLubyte* version;
263 GL_CALL_RET(vendor, GetString(GR_GL_VENDOR));
264 GL_CALL_RET(renderer, GetString(GR_GL_RENDERER));
265 GL_CALL_RET(version, GetString(GR_GL_VERSION));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000266 GrPrintf("------------------------- create GrGpuGL %p --------------\n",
267 this);
268 GrPrintf("------ VENDOR %s\n", vendor);
269 GrPrintf("------ RENDERER %s\n", renderer);
270 GrPrintf("------ VERSION %s\n", version);
271 GrPrintf("------ EXTENSIONS\n %s \n", ext);
272 }
273
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000274 this->resetDirtyFlags();
bsalomon@google.com316f99232011-01-13 21:28:12 +0000275
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000276 this->initCaps();
tomhudson@google.com747bf292011-06-14 18:16:52 +0000277
bsalomon@google.comfe676522011-06-17 18:12:21 +0000278 fLastSuccessfulStencilFmtIdx = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000279}
280
281GrGpuGL::~GrGpuGL() {
bsalomon@google.com4a018bb2011-10-28 19:50:21 +0000282 // This must be called by before the GrDrawTarget destructor
283 this->releaseGeometry();
bsalomon@google.com15b11df2011-09-16 21:18:29 +0000284 // This subclass must do this before the base class destructor runs
285 // since we will unref the GrGLInterface.
286 this->releaseResources();
reed@google.comac10a2d2010-12-22 21:39:39 +0000287}
288
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000289///////////////////////////////////////////////////////////////////////////////
290
291static const GrGLuint kUnknownBitCount = ~0;
292
293void GrGpuGL::initCaps() {
294 GrGLint maxTextureUnits;
295 // check FS and fixed-function texture unit limits
296 // we only use textures in the fragment stage currently.
297 // checks are > to make sure we have a spare unit.
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000298 const GrGLInterface* gl = this->glInterface();
299 GR_GL_GetIntegerv(gl, GR_GL_MAX_TEXTURE_IMAGE_UNITS, &maxTextureUnits);
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000300 GrAssert(maxTextureUnits > GrDrawState::kNumStages);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000301 if (kES2_GrGLBinding != this->glBinding()) {
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000302 GR_GL_GetIntegerv(gl, GR_GL_MAX_TEXTURE_UNITS, &maxTextureUnits);
tomhudson@google.com93813632011-10-27 20:21:16 +0000303 GrAssert(maxTextureUnits > GrDrawState::kNumStages);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000304 }
305 if (kES2_GrGLBinding == this->glBinding()) {
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000306 GR_GL_GetIntegerv(gl, GR_GL_MAX_FRAGMENT_UNIFORM_VECTORS,
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000307 &fGLCaps.fMaxFragmentUniformVectors);
308 } else if (kDesktop_GrGLBinding != this->glBinding()) {
309 GrGLint max;
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000310 GR_GL_GetIntegerv(gl, GR_GL_MAX_FRAGMENT_UNIFORM_COMPONENTS, &max);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000311 fGLCaps.fMaxFragmentUniformVectors = max / 4;
312 } else {
313 fGLCaps.fMaxFragmentUniformVectors = 16;
314 }
315
316 GrGLint numFormats;
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000317 GR_GL_GetIntegerv(gl, GR_GL_NUM_COMPRESSED_TEXTURE_FORMATS, &numFormats);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000318 SkAutoSTMalloc<10, GrGLint> formats(numFormats);
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000319 GR_GL_GetIntegerv(gl, GR_GL_COMPRESSED_TEXTURE_FORMATS, formats);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000320 for (int i = 0; i < numFormats; ++i) {
321 if (formats[i] == GR_GL_PALETTE8_RGBA8) {
322 fCaps.f8BitPaletteSupport = true;
323 break;
324 }
325 }
326
327 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000328 // we could also look for GL_ATI_separate_stencil extension or
329 // GL_EXT_stencil_two_side but they use different function signatures
330 // than GL2.0+ (and than each other).
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000331 fCaps.fTwoSidedStencilSupport = (this->glVersion() >= GR_GL_VER(2,0));
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000332 // supported on GL 1.4 and higher or by extension
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000333 fCaps.fStencilWrapOpsSupport = (this->glVersion() >= GR_GL_VER(1,4)) ||
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000334 this->hasExtension("GL_EXT_stencil_wrap");
335 } else {
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000336 // ES 2 has two sided stencil and stencil wrap
337 fCaps.fTwoSidedStencilSupport = true;
338 fCaps.fStencilWrapOpsSupport = true;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000339 }
340
341 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000342 fGLCaps.fRGBA8RenderbufferSupport = true;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000343 } else {
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000344 fGLCaps.fRGBA8RenderbufferSupport =
345 this->hasExtension("GL_OES_rgb8_rgba8") ||
346 this->hasExtension("GL_ARM_rgba8");
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000347 }
348
349
bsalomon@google.comc4364992011-11-07 15:54:49 +0000350 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000351 fGLCaps.fBGRAFormatSupport = this->glVersion() >= GR_GL_VER(1,2) ||
352 this->hasExtension("GL_EXT_bgra");
bsalomon@google.comc4364992011-11-07 15:54:49 +0000353 } else {
354 bool hasBGRAExt = false;
355 if (this->hasExtension("GL_APPLE_texture_format_BGRA8888")) {
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000356 fGLCaps.fBGRAFormatSupport = true;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000357 } else if (this->hasExtension("GL_EXT_texture_format_BGRA8888")) {
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000358 fGLCaps.fBGRAFormatSupport = true;
359 fGLCaps.fBGRAIsInternalFormat = true;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000360 }
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000361 GrAssert(fGLCaps.fBGRAFormatSupport ||
bsalomon@google.comc4364992011-11-07 15:54:49 +0000362 kSkia8888_PM_GrPixelConfig != kBGRA_8888_PM_GrPixelConfig);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000363 }
364
365 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000366 fGLCaps.fTextureSwizzleSupport = this->glVersion() >= GR_GL_VER(3,3) ||
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000367 this->hasExtension("GL_ARB_texture_swizzle");
368 } else {
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000369 fGLCaps.fTextureSwizzleSupport = false;
370 }
371
372 if (kDesktop_GrGLBinding == this->glBinding()) {
373 fGLCaps.fUnpackRowLengthSupport = true;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000374 fGLCaps.fUnpackFlipYSupport = false;
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000375 fGLCaps.fPackRowLengthSupport = true;
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000376 fGLCaps.fPackFlipYSupport = false;
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000377 } else {
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000378 fGLCaps.fUnpackRowLengthSupport =this->hasExtension("GL_EXT_unpack_subimage");
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000379 fGLCaps.fUnpackFlipYSupport = this->hasExtension("GL_CHROMIUM_flipy");
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000380 // no extension for pack row length
381 fGLCaps.fPackRowLengthSupport = false;
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000382 fGLCaps.fPackFlipYSupport =
383 this->hasExtension("GL_ANGLE_pack_reverse_row_order");
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000384 }
385
386 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000387 fCaps.fBufferLockSupport = true; // we require VBO support and the desktop VBO
388 // extension includes glMapBuffer.
389 } else {
390 fCaps.fBufferLockSupport = this->hasExtension("GL_OES_mapbuffer");
391 }
392
393 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000394 if (this->glVersion() >= GR_GL_VER(2,0) ||
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000395 this->hasExtension("GL_ARB_texture_non_power_of_two")) {
396 fCaps.fNPOTTextureTileSupport = true;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000397 } else {
398 fCaps.fNPOTTextureTileSupport = false;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000399 }
400 } else {
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000401 // Unextended ES2 supports NPOT textures with clamp_to_edge and non-mip filters only
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000402 fCaps.fNPOTTextureTileSupport = this->hasExtension("GL_OES_texture_npot");
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000403 }
404
bsalomon@google.com07dd2bf2011-12-09 19:40:36 +0000405 fGLCaps.fTextureUsageSupport = (kES2_GrGLBinding == this->glBinding()) &&
406 this->hasExtension("GL_ANGLE_texture_usage");
407
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000408 // Tex storage is in desktop 4.2 and can be an extension to desktop or ES.
409 fGLCaps.fTexStorageSupport = (kDesktop_GrGLBinding == this->glBinding() &&
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000410 this->glVersion() >= GR_GL_VER(4,2)) ||
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000411 this->hasExtension("GL_ARB_texture_storage") ||
412 this->hasExtension("GL_EXT_texture_storage");
413
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000414 fCaps.fHWAALineSupport = (kDesktop_GrGLBinding == this->glBinding());
415
416 ////////////////////////////////////////////////////////////////////////////
417 // Experiments to determine limitations that can't be queried.
418 // TODO: Make these a preprocess that generate some compile time constants.
419 // TODO: probe once at startup, rather than once per context creation.
420
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000421 GR_GL_GetIntegerv(gl, GR_GL_MAX_TEXTURE_SIZE, &fCaps.fMaxTextureSize);
422 GR_GL_GetIntegerv(gl, GR_GL_MAX_RENDERBUFFER_SIZE, &fCaps.fMaxRenderTargetSize);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000423 // Our render targets are always created with textures as the color
424 // attachment, hence this min:
425 fCaps.fMaxRenderTargetSize = GrMin(fCaps.fMaxTextureSize, fCaps.fMaxRenderTargetSize);
426
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000427 this->initFSAASupport();
428 this->initStencilFormats();
429}
430
431void GrGpuGL::initFSAASupport() {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000432
433 fGLCaps.fMSFBOType = GLCaps::kNone_MSFBO;
434 if (kDesktop_GrGLBinding != this->glBinding()) {
435 if (this->hasExtension("GL_CHROMIUM_framebuffer_multisample")) {
436 // chrome's extension is equivalent to the EXT msaa
437 // and fbo_blit extensions.
438 fGLCaps.fMSFBOType = GLCaps::kDesktopEXT_MSFBO;
439 } else if (this->hasExtension("GL_APPLE_framebuffer_multisample")) {
440 fGLCaps.fMSFBOType = GLCaps::kAppleES_MSFBO;
441 }
442 } else {
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000443 if ((this->glVersion() >= GR_GL_VER(3,0)) || this->hasExtension("GL_ARB_framebuffer_object")) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000444 fGLCaps.fMSFBOType = GLCaps::kDesktopARB_MSFBO;
445 } else if (this->hasExtension("GL_EXT_framebuffer_multisample") &&
446 this->hasExtension("GL_EXT_framebuffer_blit")) {
447 fGLCaps.fMSFBOType = GLCaps::kDesktopEXT_MSFBO;
448 }
449 }
450
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000451 fCaps.fFSAASupport = GLCaps::kNone_MSFBO != fGLCaps.fMSFBOType;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000452}
453
454void GrGpuGL::initStencilFormats() {
455
456 // Build up list of legal stencil formats (though perhaps not supported on
457 // the particular gpu/driver) from most preferred to least.
458
459 // these consts are in order of most preferred to least preferred
460 // we don't bother with GL_STENCIL_INDEX1 or GL_DEPTH32F_STENCIL8
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000461
462 // Omitting fVerifiedColorConfigs from initializer list should init to 0.
463 static const GLCaps::StencilFormat
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000464 // internal Format stencil bits total bits packed?
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000465 gS8 = {{GR_GL_STENCIL_INDEX8, 8, 8, false}, {0U}},
466 gS16 = {{GR_GL_STENCIL_INDEX16, 16, 16, false}, {0U}},
467 gD24S8 = {{GR_GL_DEPTH24_STENCIL8, 8, 32, true }, {0U}},
468 gS4 = {{GR_GL_STENCIL_INDEX4, 4, 4, false}, {0U}},
469 gS = {{GR_GL_STENCIL_INDEX, kUnknownBitCount, kUnknownBitCount, false}, {0U}},
470 gDS = {{GR_GL_DEPTH_STENCIL, kUnknownBitCount, kUnknownBitCount, true }, {0U}};
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000471
472 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000473 bool supportsPackedDS = this->glVersion() >= GR_GL_VER(3,0) ||
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000474 this->hasExtension("GL_EXT_packed_depth_stencil") ||
475 this->hasExtension("GL_ARB_framebuffer_object");
476
477 // S1 thru S16 formats are in GL 3.0+, EXT_FBO, and ARB_FBO since we
478 // require FBO support we can expect these are legal formats and don't
479 // check. These also all support the unsized GL_STENCIL_INDEX.
480 fGLCaps.fStencilFormats.push_back() = gS8;
481 fGLCaps.fStencilFormats.push_back() = gS16;
482 if (supportsPackedDS) {
483 fGLCaps.fStencilFormats.push_back() = gD24S8;
484 }
485 fGLCaps.fStencilFormats.push_back() = gS4;
486 if (supportsPackedDS) {
487 fGLCaps.fStencilFormats.push_back() = gDS;
488 }
489 } else {
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000490 // ES2 has STENCIL_INDEX8 without extensions but requires extensions
491 // for other formats.
492 // ES doesn't support using the unsized format.
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000493
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000494 fGLCaps.fStencilFormats.push_back() = gS8;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000495 //fStencilFormats.push_back() = gS16;
496 if (this->hasExtension("GL_OES_packed_depth_stencil")) {
497 fGLCaps.fStencilFormats.push_back() = gD24S8;
498 }
499 if (this->hasExtension("GL_OES_stencil4")) {
500 fGLCaps.fStencilFormats.push_back() = gS4;
501 }
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000502 }
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000503#if GR_DEBUG
504 // ensure that initially all color / stencil format combos have unverified
505 // fbo status.
506 for (int i = 0; i < fGLCaps.fStencilFormats.count(); ++i) {
507 int numU32 =
508 GR_ARRAY_COUNT(fGLCaps.fStencilFormats[i].fVerifiedColorConfigs);
509 for (int j = 0; j < numU32; ++j) {
510 GrAssert(0 == fGLCaps.fStencilFormats[i].fVerifiedColorConfigs[j]);
511 }
512 }
513#endif
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000514}
515
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000516GrPixelConfig GrGpuGL::preferredReadPixelsConfig(GrPixelConfig config) const {
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000517 if (GR_GL_RGBA_8888_PIXEL_OPS_SLOW && GrPixelConfigIsRGBA8888(config)) {
518 return GrPixelConfigSwapRAndB(config);
519 } else {
520 return config;
521 }
522}
523
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000524GrPixelConfig GrGpuGL::preferredWritePixelsConfig(GrPixelConfig config) const {
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000525 if (GR_GL_RGBA_8888_PIXEL_OPS_SLOW && GrPixelConfigIsRGBA8888(config)) {
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000526 return GrPixelConfigSwapRAndB(config);
527 } else {
528 return config;
529 }
530}
531
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000532bool GrGpuGL::fullReadPixelsIsFasterThanPartial() const {
533 return SkToBool(GR_GL_FULL_READPIXELS_FASTER_THAN_PARTIAL);
534}
535
bsalomon@google.com1bf1c212011-11-05 12:18:58 +0000536void GrGpuGL::onResetContext() {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000537 if (gPrintStartupSpew && !fPrintedCaps) {
538 fPrintedCaps = true;
539 this->getCaps().print();
540 fGLCaps.print();
541 }
542
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000543 // We detect cases when blending is effectively off
reed@google.comac10a2d2010-12-22 21:39:39 +0000544 fHWBlendDisabled = false;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000545 GL_CALL(Enable(GR_GL_BLEND));
reed@google.comac10a2d2010-12-22 21:39:39 +0000546
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000547 // we don't use the zb at all
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000548 GL_CALL(Disable(GR_GL_DEPTH_TEST));
549 GL_CALL(DepthMask(GR_GL_FALSE));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000550
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000551 GL_CALL(Disable(GR_GL_CULL_FACE));
552 GL_CALL(FrontFace(GR_GL_CCW));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000553 fHWDrawState.setDrawFace(GrDrawState::kBoth_DrawFace);
reed@google.comac10a2d2010-12-22 21:39:39 +0000554
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000555 GL_CALL(Disable(GR_GL_DITHER));
556 if (kDesktop_GrGLBinding == this->glBinding()) {
557 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
558 GL_CALL(Disable(GR_GL_POINT_SMOOTH));
559 GL_CALL(Disable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +0000560 fHWAAState.fMSAAEnabled = false;
561 fHWAAState.fSmoothLineEnabled = false;
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000562 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000563
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000564 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000565 fHWDrawState.resetStateFlags();
bsalomon@google.comd302f142011-03-03 13:54:13 +0000566
reed@google.comac10a2d2010-12-22 21:39:39 +0000567 // we only ever use lines in hairline mode
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000568 GL_CALL(LineWidth(1));
reed@google.comac10a2d2010-12-22 21:39:39 +0000569
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000570 // invalid
571 fActiveTextureUnitIdx = -1;
reed@google.comac10a2d2010-12-22 21:39:39 +0000572
reed@google.comac10a2d2010-12-22 21:39:39 +0000573 // illegal values
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000574 fHWDrawState.setBlendFunc((GrBlendCoeff)0xFF, (GrBlendCoeff)0xFF);
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000575
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000576 fHWDrawState.setBlendConstant(0x00000000);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000577 GL_CALL(BlendColor(0,0,0,0));
bsalomon@google.com080773c2011-03-15 19:09:25 +0000578
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000579 fHWDrawState.setColor(GrColor_ILLEGAL);
bsalomon@google.com316f99232011-01-13 21:28:12 +0000580
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000581 fHWDrawState.setViewMatrix(GrMatrix::InvalidMatrix());
bsalomon@google.com316f99232011-01-13 21:28:12 +0000582
tomhudson@google.com93813632011-10-27 20:21:16 +0000583 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000584 fHWDrawState.setTexture(s, NULL);
585 fHWDrawState.sampler(s)->setRadial2Params(-GR_ScalarMax,
586 -GR_ScalarMax,
587 true);
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000588 *fHWDrawState.sampler(s)->matrix() = GrMatrix::InvalidMatrix();
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000589 fHWDrawState.sampler(s)->setConvolutionParams(0, NULL, NULL);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000590 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000591
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000592 fHWBounds.fScissorRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000593 fHWBounds.fScissorEnabled = false;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000594 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000595 fHWBounds.fViewportRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000596
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000597 fHWDrawState.stencil()->invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000598 fHWStencilClip = false;
bsalomon@google.comdea2f8d2011-08-01 15:51:05 +0000599 fClipInStencil = false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000600
601 fHWGeometryState.fIndexBuffer = NULL;
602 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000603
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +0000604 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000605
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000606 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000607 fHWDrawState.setRenderTarget(NULL);
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000608
609 // we assume these values
610 if (this->glCaps().fUnpackRowLengthSupport) {
611 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
612 }
613 if (this->glCaps().fPackRowLengthSupport) {
614 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
615 }
616 if (this->glCaps().fUnpackFlipYSupport) {
617 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
618 }
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000619 if (this->glCaps().fPackFlipYSupport) {
620 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, GR_GL_FALSE));
621 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000622}
623
bsalomon@google.come269f212011-11-07 13:29:52 +0000624GrTexture* GrGpuGL::onCreatePlatformTexture(const GrPlatformTextureDesc& desc) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000625 GrGLTexture::Desc glTexDesc;
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000626 if (!configToGLFormats(desc.fConfig, false, NULL, NULL, NULL)) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000627 return NULL;
628 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000629
bsalomon@google.com99621082011-11-15 16:47:16 +0000630 glTexDesc.fWidth = desc.fWidth;
631 glTexDesc.fHeight = desc.fHeight;
bsalomon@google.come269f212011-11-07 13:29:52 +0000632 glTexDesc.fConfig = desc.fConfig;
633 glTexDesc.fTextureID = static_cast<GrGLuint>(desc.fTextureHandle);
634 glTexDesc.fOwnsID = false;
635 glTexDesc.fOrientation = GrGLTexture::kBottomUp_Orientation;
636
637 GrGLTexture* texture = NULL;
638 if (desc.fFlags & kRenderTarget_GrPlatformTextureFlag) {
639 GrGLRenderTarget::Desc glRTDesc;
640 glRTDesc.fRTFBOID = 0;
641 glRTDesc.fTexFBOID = 0;
642 glRTDesc.fMSColorRenderbufferID = 0;
643 glRTDesc.fOwnIDs = true;
644 glRTDesc.fConfig = desc.fConfig;
645 glRTDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.com99621082011-11-15 16:47:16 +0000646 if (!this->createRenderTargetObjects(glTexDesc.fWidth,
647 glTexDesc.fHeight,
bsalomon@google.come269f212011-11-07 13:29:52 +0000648 glTexDesc.fTextureID,
649 &glRTDesc)) {
650 return NULL;
651 }
652 texture = new GrGLTexture(this, glTexDesc, glRTDesc);
653 } else {
654 texture = new GrGLTexture(this, glTexDesc);
655 }
656 if (NULL == texture) {
657 return NULL;
658 }
659
660 this->setSpareTextureUnit();
661 return texture;
662}
663
664GrRenderTarget* GrGpuGL::onCreatePlatformRenderTarget(const GrPlatformRenderTargetDesc& desc) {
665 GrGLRenderTarget::Desc glDesc;
666 glDesc.fConfig = desc.fConfig;
667 glDesc.fRTFBOID = static_cast<GrGLuint>(desc.fRenderTargetHandle);
668 glDesc.fMSColorRenderbufferID = 0;
669 glDesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
670 glDesc.fSampleCnt = desc.fSampleCnt;
671 glDesc.fOwnIDs = false;
672 GrGLIRect viewport;
673 viewport.fLeft = 0;
674 viewport.fBottom = 0;
675 viewport.fWidth = desc.fWidth;
676 viewport.fHeight = desc.fHeight;
677
678 GrRenderTarget* tgt = new GrGLRenderTarget(this, glDesc, viewport);
679 if (desc.fStencilBits) {
680 GrGLStencilBuffer::Format format;
681 format.fInternalFormat = GrGLStencilBuffer::kUnknownInternalFormat;
682 format.fPacked = false;
683 format.fStencilBits = desc.fStencilBits;
684 format.fTotalBits = desc.fStencilBits;
685 GrGLStencilBuffer* sb = new GrGLStencilBuffer(this,
686 0,
687 desc.fWidth,
688 desc.fHeight,
689 desc.fSampleCnt,
690 format);
691 tgt->setStencilBuffer(sb);
692 sb->unref();
693 }
694 return tgt;
695}
696
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000697////////////////////////////////////////////////////////////////////////////////
698
bsalomon@google.com6f379512011-11-16 20:36:03 +0000699void GrGpuGL::onWriteTexturePixels(GrTexture* texture,
700 int left, int top, int width, int height,
701 GrPixelConfig config, const void* buffer,
702 size_t rowBytes) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000703 if (NULL == buffer) {
704 return;
705 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000706 GrGLTexture* glTex = static_cast<GrGLTexture*>(texture);
707
bsalomon@google.com6f379512011-11-16 20:36:03 +0000708 this->setSpareTextureUnit();
709 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTex->textureID()));
710 GrGLTexture::Desc desc;
711 desc.fConfig = glTex->config();
712 desc.fWidth = glTex->width();
713 desc.fHeight = glTex->height();
714 desc.fOrientation = glTex->orientation();
715 desc.fTextureID = glTex->textureID();
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000716
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000717 this->uploadTexData(desc, false,
718 left, top, width, height,
719 config, buffer, rowBytes);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000720}
721
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000722namespace {
723bool adjust_pixel_ops_params(int surfaceWidth,
724 int surfaceHeight,
725 size_t bpp,
726 int* left, int* top, int* width, int* height,
727 const void** data,
728 size_t* rowBytes) {
729 if (!*rowBytes) {
730 *rowBytes = *width * bpp;
731 }
732
733 GrIRect subRect = GrIRect::MakeXYWH(*left, *top, *width, *height);
734 GrIRect bounds = GrIRect::MakeWH(surfaceWidth, surfaceHeight);
735
736 if (!subRect.intersect(bounds)) {
737 return false;
738 }
739 *data = reinterpret_cast<const void*>(reinterpret_cast<intptr_t>(*data) +
740 (subRect.fTop - *top) * *rowBytes + (subRect.fLeft - *left) * bpp);
741
742 *left = subRect.fLeft;
743 *top = subRect.fTop;
744 *width = subRect.width();
745 *height = subRect.height();
746 return true;
747}
748}
749
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000750bool GrGpuGL::uploadTexData(const GrGLTexture::Desc& desc,
751 bool isNewTexture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000752 int left, int top, int width, int height,
753 GrPixelConfig dataConfig,
754 const void* data,
755 size_t rowBytes) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000756 GrAssert(NULL != data || isNewTexture);
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000757
758 size_t bpp = GrBytesPerPixel(dataConfig);
759 if (!adjust_pixel_ops_params(desc.fWidth, desc.fHeight, bpp, &left, &top,
760 &width, &height, &data, &rowBytes)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000761 return false;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000762 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000763 size_t trimRowBytes = width * bpp;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000764
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000765 // in case we need a temporary, trimmed copy of the src pixels
766 SkAutoSMalloc<128 * 128> tempStorage;
767
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000768 bool useTexStorage = isNewTexture &&
769 this->glCaps().fTexStorageSupport;
770 if (useTexStorage) {
771 if (kDesktop_GrGLBinding == this->glBinding()) {
772 // 565 is not a sized internal format on desktop GL. So on desktop
773 // with 565 we always use an unsized internal format to let the
774 // system pick the best sized format to convert the 565 data to.
775 // Since glTexStorage only allows sized internal formats we will
776 // instead fallback to glTexImage2D.
777 useTexStorage = desc.fConfig != kRGB_565_GrPixelConfig;
778 } else {
779 // ES doesn't allow paletted textures to be used with tex storage
780 useTexStorage = desc.fConfig != kIndex_8_GrPixelConfig;
781 }
782 }
783
784 GrGLenum internalFormat;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000785 GrGLenum externalFormat;
786 GrGLenum externalType;
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000787 // glTexStorage requires sized internal formats on both desktop and ES. ES
788 // glTexImage requires an unsized format.
789 if (!this->configToGLFormats(dataConfig, useTexStorage, &internalFormat,
790 &externalFormat, &externalType)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000791 return false;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000792 }
793
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000794 if (!isNewTexture && GR_GL_PALETTE8_RGBA8 == internalFormat) {
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000795 // paletted textures cannot be updated
796 return false;
797 }
798
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000799 /*
bsalomon@google.com6f379512011-11-16 20:36:03 +0000800 * check whether to allocate a temporary buffer for flipping y or
801 * because our srcData has extra bytes past each row. If so, we need
802 * to trim those off here, since GL ES may not let us specify
803 * GL_UNPACK_ROW_LENGTH.
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000804 */
bsalomon@google.com6f379512011-11-16 20:36:03 +0000805 bool restoreGLRowLength = false;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000806 bool swFlipY = false;
807 bool glFlipY = false;
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000808 if (NULL != data) {
809 if (GrGLTexture::kBottomUp_Orientation == desc.fOrientation) {
810 if (this->glCaps().fUnpackFlipYSupport) {
811 glFlipY = true;
812 } else {
813 swFlipY = true;
814 }
815 }
816 if (this->glCaps().fUnpackRowLengthSupport && !swFlipY) {
817 // can't use this for flipping, only non-neg values allowed. :(
818 if (rowBytes != trimRowBytes) {
819 GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
820 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength));
821 restoreGLRowLength = true;
822 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000823 } else {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000824 if (trimRowBytes != rowBytes || swFlipY) {
825 // copy data into our new storage, skipping the trailing bytes
826 size_t trimSize = height * trimRowBytes;
827 const char* src = (const char*)data;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000828 if (swFlipY) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000829 src += (height - 1) * rowBytes;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000830 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000831 char* dst = (char*)tempStorage.reset(trimSize);
832 for (int y = 0; y < height; y++) {
833 memcpy(dst, src, trimRowBytes);
834 if (swFlipY) {
835 src -= rowBytes;
836 } else {
837 src += rowBytes;
838 }
839 dst += trimRowBytes;
840 }
841 // now point data to our copied version
842 data = tempStorage.get();
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000843 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000844 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000845 if (glFlipY) {
846 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_TRUE));
847 }
848 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, static_cast<GrGLint>(bpp)));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000849 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000850 bool succeeded = true;
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000851 if (isNewTexture &&
852 0 == left && 0 == top &&
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000853 desc.fWidth == width && desc.fHeight == height) {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000854 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000855 if (useTexStorage) {
856 // We never resize or change formats of textures. We don't use
857 // mipmaps currently.
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000858 GL_ALLOC_CALL(this->glInterface(),
859 TexStorage2D(GR_GL_TEXTURE_2D,
860 1, // levels
861 internalFormat,
862 desc.fWidth, desc.fHeight));
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000863 } else {
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000864 if (GR_GL_PALETTE8_RGBA8 == internalFormat) {
865 GrGLsizei imageSize = desc.fWidth * desc.fHeight +
866 kGrColorTableSize;
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000867 GL_ALLOC_CALL(this->glInterface(),
868 CompressedTexImage2D(GR_GL_TEXTURE_2D,
869 0, // level
870 internalFormat,
871 desc.fWidth, desc.fHeight,
872 0, // border
873 imageSize,
874 data));
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000875 } else {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000876 GL_ALLOC_CALL(this->glInterface(),
877 TexImage2D(GR_GL_TEXTURE_2D,
878 0, // level
879 internalFormat,
880 desc.fWidth, desc.fHeight,
881 0, // border
882 externalFormat, externalType,
883 data));
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000884 }
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000885 }
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000886 GrGLenum error = CHECK_ALLOC_ERROR(this->glInterface());
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000887 if (error != GR_GL_NO_ERROR) {
888 succeeded = false;
889 } else {
890 // if we have data and we used TexStorage to create the texture, we
891 // now upload with TexSubImage.
892 if (NULL != data && useTexStorage) {
893 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D,
894 0, // level
895 left, top,
896 width, height,
897 externalFormat, externalType,
898 data));
899 }
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000900 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000901 } else {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000902 if (swFlipY || glFlipY) {
bsalomon@google.com6f379512011-11-16 20:36:03 +0000903 top = desc.fHeight - (top + height);
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000904 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000905 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D,
906 0, // level
907 left, top,
908 width, height,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000909 externalFormat, externalType, data));
910 }
911
912 if (restoreGLRowLength) {
913 GrAssert(this->glCaps().fUnpackRowLengthSupport);
914 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000915 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000916 if (glFlipY) {
917 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
918 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000919 return succeeded;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000920}
921
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000922bool GrGpuGL::createRenderTargetObjects(int width, int height,
923 GrGLuint texID,
924 GrGLRenderTarget::Desc* desc) {
925 desc->fMSColorRenderbufferID = 0;
926 desc->fRTFBOID = 0;
927 desc->fTexFBOID = 0;
928 desc->fOwnIDs = true;
929
930 GrGLenum status;
931 GrGLint err;
932
bsalomon@google.comab15d612011-08-09 12:57:56 +0000933 GrGLenum msColorFormat = 0; // suppress warning
934
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000935 GL_CALL(GenFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000936 if (!desc->fTexFBOID) {
937 goto FAILED;
938 }
939
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000940
941 // If we are using multisampling we will create two FBOS. We render
942 // to one and then resolve to the texture bound to the other.
bsalomon@google.come269f212011-11-07 13:29:52 +0000943 if (desc->fSampleCnt > 0) {
944 if (GLCaps::kNone_MSFBO == fGLCaps.fMSFBOType) {
945 goto FAILED;
946 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000947 GL_CALL(GenFramebuffers(1, &desc->fRTFBOID));
948 GL_CALL(GenRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000949 if (!desc->fRTFBOID ||
950 !desc->fMSColorRenderbufferID ||
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000951 !this->configToGLFormats(desc->fConfig,
952 // GLES requires sized internal formats
953 kES2_GrGLBinding == this->glBinding(),
954 &msColorFormat, NULL, NULL)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000955 goto FAILED;
956 }
957 } else {
958 desc->fRTFBOID = desc->fTexFBOID;
959 }
960
bsalomon@google.com0e9b41a2012-01-04 22:11:43 +0000961 // below here we may bind the FBO
962 fHWDrawState.setRenderTarget(NULL);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000963 if (desc->fRTFBOID != desc->fTexFBOID) {
964 GrAssert(desc->fSampleCnt > 1);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000965 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000966 desc->fMSColorRenderbufferID));
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000967 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
968 GL_ALLOC_CALL(this->glInterface(),
969 RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
970 desc->fSampleCnt,
971 msColorFormat,
972 width, height));
973 err = CHECK_ALLOC_ERROR(this->glInterface());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000974 if (err != GR_GL_NO_ERROR) {
975 goto FAILED;
976 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000977 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fRTFBOID));
978 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000979 GR_GL_COLOR_ATTACHMENT0,
980 GR_GL_RENDERBUFFER,
981 desc->fMSColorRenderbufferID));
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000982 if (!fGLCaps.isConfigVerifiedColorAttachment(desc->fConfig)) {
983 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
984 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
985 goto FAILED;
986 }
987 fGLCaps.markConfigAsValidColorAttachment(desc->fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000988 }
989 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000990 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000991
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000992 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
993 GR_GL_COLOR_ATTACHMENT0,
994 GR_GL_TEXTURE_2D,
995 texID, 0));
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000996 if (!fGLCaps.isConfigVerifiedColorAttachment(desc->fConfig)) {
997 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
998 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
999 goto FAILED;
1000 }
1001 fGLCaps.markConfigAsValidColorAttachment(desc->fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001002 }
1003
1004 return true;
1005
1006FAILED:
1007 if (desc->fMSColorRenderbufferID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001008 GL_CALL(DeleteRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001009 }
1010 if (desc->fRTFBOID != desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001011 GL_CALL(DeleteFramebuffers(1, &desc->fRTFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001012 }
1013 if (desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001014 GL_CALL(DeleteFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001015 }
1016 return false;
1017}
1018
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001019// good to set a break-point here to know when createTexture fails
1020static GrTexture* return_null_texture() {
1021// GrAssert(!"null texture");
1022 return NULL;
1023}
1024
1025#if GR_DEBUG
1026static size_t as_size_t(int x) {
1027 return x;
1028}
1029#endif
1030
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001031GrTexture* GrGpuGL::onCreateTexture(const GrTextureDesc& desc,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001032 const void* srcData,
1033 size_t rowBytes) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001034
1035#if GR_COLLECT_STATS
1036 ++fStats.fTextureCreateCnt;
1037#endif
reed@google.com1fcd51e2011-01-05 15:50:27 +00001038
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001039 GrGLTexture::Desc glTexDesc;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001040 GrGLRenderTarget::Desc glRTDesc;
reed@google.comac10a2d2010-12-22 21:39:39 +00001041
bsalomon@google.com78d6cf92012-01-30 18:09:31 +00001042 // Attempt to catch un- or wrongly initialized sample counts;
1043 GrAssert(desc.fSampleCnt >= 0 && desc.fSampleCnt <= 64);
1044
bsalomon@google.com99621082011-11-15 16:47:16 +00001045 glTexDesc.fWidth = desc.fWidth;
1046 glTexDesc.fHeight = desc.fHeight;
bsalomon@google.com78d6cf92012-01-30 18:09:31 +00001047 glTexDesc.fConfig = desc.fConfig;
1048 glTexDesc.fOwnsID = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001049
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001050 glRTDesc.fMSColorRenderbufferID = 0;
1051 glRTDesc.fRTFBOID = 0;
1052 glRTDesc.fTexFBOID = 0;
1053 glRTDesc.fOwnIDs = true;
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001054 glRTDesc.fConfig = glTexDesc.fConfig;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001055
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001056 bool renderTarget = 0 != (desc.fFlags & kRenderTarget_GrTextureFlagBit);
reed@google.comac10a2d2010-12-22 21:39:39 +00001057
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001058 const Caps& caps = this->getCaps();
1059
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001060 // We keep GrRenderTargets in GL's normal orientation so that they
1061 // can be drawn to by the outside world without the client having
1062 // to render upside down.
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001063 glTexDesc.fOrientation = renderTarget ? GrGLTexture::kBottomUp_Orientation :
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001064 GrGLTexture::kTopDown_Orientation;
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001065
bsalomon@google.com78d6cf92012-01-30 18:09:31 +00001066 glRTDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001067 if (GLCaps::kNone_MSFBO == fGLCaps.fMSFBOType &&
bsalomon@google.com78d6cf92012-01-30 18:09:31 +00001068 desc.fSampleCnt) {
1069 GrPrintf("MSAA RT requested but not supported on this platform.");
reed@google.comac10a2d2010-12-22 21:39:39 +00001070 }
1071
reed@google.comac10a2d2010-12-22 21:39:39 +00001072 if (renderTarget) {
bsalomon@google.com99621082011-11-15 16:47:16 +00001073 if (glTexDesc.fWidth > caps.fMaxRenderTargetSize ||
1074 glTexDesc.fHeight > caps.fMaxRenderTargetSize) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001075 return return_null_texture();
1076 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001077 }
1078
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001079 GL_CALL(GenTextures(1, &glTexDesc.fTextureID));
bsalomon@google.com07dd2bf2011-12-09 19:40:36 +00001080 if (renderTarget && this->glCaps().fTextureUsageSupport) {
1081 // provides a hint about how this texture will be used
1082 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1083 GR_GL_TEXTURE_USAGE,
1084 GR_GL_FRAMEBUFFER_ATTACHMENT));
1085 }
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001086 if (!glTexDesc.fTextureID) {
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001087 return return_null_texture();
1088 }
1089
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001090 this->setSpareTextureUnit();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001091 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTexDesc.fTextureID));
bsalomon@google.come269f212011-11-07 13:29:52 +00001092
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001093 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
1094 // drivers have a bug where an FBO won't be complete if it includes a
1095 // texture that is not mipmap complete (considering the filter in use).
1096 GrGLTexture::TexParams initialTexParams;
1097 // we only set a subset here so invalidate first
1098 initialTexParams.invalidate();
1099 initialTexParams.fFilter = GR_GL_NEAREST;
1100 initialTexParams.fWrapS = GR_GL_CLAMP_TO_EDGE;
1101 initialTexParams.fWrapT = GR_GL_CLAMP_TO_EDGE;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001102 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1103 GR_GL_TEXTURE_MAG_FILTER,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001104 initialTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001105 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1106 GR_GL_TEXTURE_MIN_FILTER,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001107 initialTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001108 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1109 GR_GL_TEXTURE_WRAP_S,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001110 initialTexParams.fWrapS));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001111 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1112 GR_GL_TEXTURE_WRAP_T,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001113 initialTexParams.fWrapT));
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001114 if (!this->uploadTexData(glTexDesc, true, 0, 0,
1115 glTexDesc.fWidth, glTexDesc.fHeight,
1116 desc.fConfig, srcData, rowBytes)) {
1117 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
1118 return return_null_texture();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001119 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001120
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001121 GrGLTexture* tex;
reed@google.comac10a2d2010-12-22 21:39:39 +00001122 if (renderTarget) {
1123#if GR_COLLECT_STATS
1124 ++fStats.fRenderTargetCreateCnt;
1125#endif
bsalomon@google.com99621082011-11-15 16:47:16 +00001126 if (!this->createRenderTargetObjects(glTexDesc.fWidth,
1127 glTexDesc.fHeight,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001128 glTexDesc.fTextureID,
1129 &glRTDesc)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001130 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001131 return return_null_texture();
1132 }
bsalomon@google.com80d09b92011-11-05 21:21:13 +00001133 tex = new GrGLTexture(this, glTexDesc, glRTDesc);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001134 } else {
bsalomon@google.com80d09b92011-11-05 21:21:13 +00001135 tex = new GrGLTexture(this, glTexDesc);
reed@google.comac10a2d2010-12-22 21:39:39 +00001136 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001137 tex->setCachedTexParams(initialTexParams, this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00001138#ifdef TRACE_TEXTURE_CREATION
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001139 GrPrintf("--- new texture [%d] size=(%d %d) config=%d\n",
1140 glTexDesc.fTextureID, desc.fWidth, desc.fHeight, desc.fConfig);
reed@google.comac10a2d2010-12-22 21:39:39 +00001141#endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001142 return tex;
1143}
1144
1145namespace {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001146void inline get_stencil_rb_sizes(const GrGLInterface* gl,
1147 GrGLuint rb,
1148 GrGLStencilBuffer::Format* format) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001149 // we shouldn't ever know one size and not the other
1150 GrAssert((kUnknownBitCount == format->fStencilBits) ==
1151 (kUnknownBitCount == format->fTotalBits));
1152 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001153 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001154 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1155 (GrGLint*)&format->fStencilBits);
1156 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001157 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001158 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1159 (GrGLint*)&format->fTotalBits);
1160 format->fTotalBits += format->fStencilBits;
1161 } else {
1162 format->fTotalBits = format->fStencilBits;
1163 }
1164 }
1165}
1166}
1167
1168bool GrGpuGL::createStencilBufferForRenderTarget(GrRenderTarget* rt,
1169 int width, int height) {
1170
1171 // All internally created RTs are also textures. We don't create
1172 // SBs for a client's standalone RT (that is RT that isnt also a texture).
1173 GrAssert(rt->asTexture());
bsalomon@google.com99621082011-11-15 16:47:16 +00001174 GrAssert(width >= rt->width());
1175 GrAssert(height >= rt->height());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001176
1177 int samples = rt->numSamples();
1178 GrGLuint sbID;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001179 GL_CALL(GenRenderbuffers(1, &sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001180 if (!sbID) {
1181 return false;
1182 }
1183
1184 GrGLStencilBuffer* sb = NULL;
1185
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001186 int stencilFmtCnt = fGLCaps.fStencilFormats.count();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001187 for (int i = 0; i < stencilFmtCnt; ++i) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001188 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001189 // we start with the last stencil format that succeeded in hopes
1190 // that we won't go through this loop more than once after the
1191 // first (painful) stencil creation.
1192 int sIdx = (i + fLastSuccessfulStencilFmtIdx) % stencilFmtCnt;
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001193 const GLCaps::StencilFormat& sFmt = fGLCaps.fStencilFormats[sIdx];
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001194 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001195 // we do this "if" so that we don't call the multisample
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001196 // version on a GL that doesn't have an MSAA extension.
1197 if (samples > 1) {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001198 GL_ALLOC_CALL(this->glInterface(),
1199 RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
1200 samples,
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001201 sFmt.fFormat.fInternalFormat,
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001202 width, height));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001203 } else {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001204 GL_ALLOC_CALL(this->glInterface(),
1205 RenderbufferStorage(GR_GL_RENDERBUFFER,
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001206 sFmt.fFormat.fInternalFormat,
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001207 width, height));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001208 }
1209
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001210 GrGLenum err = CHECK_ALLOC_ERROR(this->glInterface());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001211 if (err == GR_GL_NO_ERROR) {
1212 // After sized formats we attempt an unsized format and take whatever
1213 // sizes GL gives us. In that case we query for the size.
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001214 GrGLStencilBuffer::Format format = sFmt.fFormat;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001215 get_stencil_rb_sizes(this->glInterface(), sbID, &format);
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001216 sb = new GrGLStencilBuffer(this, sbID, width, height,
1217 samples, format);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001218 if (this->attachStencilBufferToRenderTarget(sb, rt)) {
1219 fLastSuccessfulStencilFmtIdx = sIdx;
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001220 rt->setStencilBuffer(sb);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001221 sb->unref();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001222 return true;
1223 }
1224 sb->abandon(); // otherwise we lose sbID
1225 sb->unref();
1226 }
1227 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001228 GL_CALL(DeleteRenderbuffers(1, &sbID));
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001229 return false;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001230}
1231
1232bool GrGpuGL::attachStencilBufferToRenderTarget(GrStencilBuffer* sb,
1233 GrRenderTarget* rt) {
1234 GrGLRenderTarget* glrt = (GrGLRenderTarget*) rt;
1235
1236 GrGLuint fbo = glrt->renderFBOID();
1237
1238 if (NULL == sb) {
1239 if (NULL != rt->getStencilBuffer()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001240 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001241 GR_GL_STENCIL_ATTACHMENT,
1242 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001243 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001244 GR_GL_DEPTH_ATTACHMENT,
1245 GR_GL_RENDERBUFFER, 0));
1246#if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001247 GrGLenum status;
1248 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001249 GrAssert(GR_GL_FRAMEBUFFER_COMPLETE == status);
1250#endif
1251 }
1252 return true;
1253 } else {
1254 GrGLStencilBuffer* glsb = (GrGLStencilBuffer*) sb;
1255 GrGLuint rb = glsb->renderbufferID();
1256
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001257 fHWDrawState.setRenderTarget(NULL);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001258 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, fbo));
1259 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001260 GR_GL_STENCIL_ATTACHMENT,
1261 GR_GL_RENDERBUFFER, rb));
1262 if (glsb->format().fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001263 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001264 GR_GL_DEPTH_ATTACHMENT,
1265 GR_GL_RENDERBUFFER, rb));
1266 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001267 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001268 GR_GL_DEPTH_ATTACHMENT,
1269 GR_GL_RENDERBUFFER, 0));
reed@google.comac10a2d2010-12-22 21:39:39 +00001270 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001271
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001272 GrGLenum status;
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001273 if (!fGLCaps.isColorConfigAndStencilFormatVerified(rt->config(),
1274 glsb->format())) {
1275 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1276 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001277 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001278 GR_GL_STENCIL_ATTACHMENT,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001279 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001280 if (glsb->format().fPacked) {
1281 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1282 GR_GL_DEPTH_ATTACHMENT,
1283 GR_GL_RENDERBUFFER, 0));
1284 }
1285 return false;
1286 } else {
1287 fGLCaps.markColorConfigAndStencilFormatAsVerified(
1288 rt->config(),
1289 glsb->format());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001290 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001291 }
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001292 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001293 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001294}
1295
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001296////////////////////////////////////////////////////////////////////////////////
1297
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001298GrVertexBuffer* GrGpuGL::onCreateVertexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001299 GrGLuint id;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001300 GL_CALL(GenBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001301 if (id) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001302 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, id));
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001303 fHWGeometryState.fArrayPtrsDirty = true;
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001304 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001305 // make sure driver can allocate memory for this buffer
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001306 GL_ALLOC_CALL(this->glInterface(),
1307 BufferData(GR_GL_ARRAY_BUFFER,
1308 size,
1309 NULL, // data ptr
1310 dynamic ? GR_GL_DYNAMIC_DRAW :
1311 GR_GL_STATIC_DRAW));
1312 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001313 GL_CALL(DeleteBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001314 // deleting bound buffer does implicit bind to 0
1315 fHWGeometryState.fVertexBuffer = NULL;
1316 return NULL;
1317 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001318 GrGLVertexBuffer* vertexBuffer = new GrGLVertexBuffer(this, id,
reed@google.comac10a2d2010-12-22 21:39:39 +00001319 size, dynamic);
1320 fHWGeometryState.fVertexBuffer = vertexBuffer;
1321 return vertexBuffer;
1322 }
1323 return NULL;
1324}
1325
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001326GrIndexBuffer* GrGpuGL::onCreateIndexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001327 GrGLuint id;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001328 GL_CALL(GenBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001329 if (id) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001330 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, id));
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001331 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001332 // make sure driver can allocate memory for this buffer
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001333 GL_ALLOC_CALL(this->glInterface(),
1334 BufferData(GR_GL_ELEMENT_ARRAY_BUFFER,
1335 size,
1336 NULL, // data ptr
1337 dynamic ? GR_GL_DYNAMIC_DRAW :
1338 GR_GL_STATIC_DRAW));
1339 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001340 GL_CALL(DeleteBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001341 // deleting bound buffer does implicit bind to 0
1342 fHWGeometryState.fIndexBuffer = NULL;
1343 return NULL;
1344 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001345 GrIndexBuffer* indexBuffer = new GrGLIndexBuffer(this, id,
reed@google.comac10a2d2010-12-22 21:39:39 +00001346 size, dynamic);
1347 fHWGeometryState.fIndexBuffer = indexBuffer;
1348 return indexBuffer;
1349 }
1350 return NULL;
1351}
1352
reed@google.comac10a2d2010-12-22 21:39:39 +00001353void GrGpuGL::flushScissor(const GrIRect* rect) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001354 const GrDrawState& drawState = this->getDrawState();
1355 const GrGLRenderTarget* rt =
1356 static_cast<const GrGLRenderTarget*>(drawState.getRenderTarget());
1357
1358 GrAssert(NULL != rt);
1359 const GrGLIRect& vp = rt->getViewport();
reed@google.comac10a2d2010-12-22 21:39:39 +00001360
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001361 GrGLIRect scissor;
1362 if (NULL != rect) {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001363 scissor.setRelativeTo(vp, rect->fLeft, rect->fTop,
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001364 rect->width(), rect->height());
1365 if (scissor.contains(vp)) {
1366 rect = NULL;
1367 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001368 }
1369
1370 if (NULL != rect) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001371 if (fHWBounds.fScissorRect != scissor) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001372 scissor.pushToGLScissor(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001373 fHWBounds.fScissorRect = scissor;
1374 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001375 if (!fHWBounds.fScissorEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001376 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001377 fHWBounds.fScissorEnabled = true;
1378 }
1379 } else {
1380 if (fHWBounds.fScissorEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001381 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001382 fHWBounds.fScissorEnabled = false;
1383 }
1384 }
1385}
1386
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001387void GrGpuGL::onClear(const GrIRect* rect, GrColor color) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001388 const GrDrawState& drawState = this->getDrawState();
1389 const GrRenderTarget* rt = drawState.getRenderTarget();
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001390 // parent class should never let us get here with no RT
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001391 GrAssert(NULL != rt);
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001392
bsalomon@google.com74b98712011-11-11 19:46:16 +00001393 GrIRect clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001394 if (NULL != rect) {
1395 // flushScissor expects rect to be clipped to the target.
bsalomon@google.com74b98712011-11-11 19:46:16 +00001396 clippedRect = *rect;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001397 GrIRect rtRect = SkIRect::MakeWH(rt->width(), rt->height());
bsalomon@google.com74b98712011-11-11 19:46:16 +00001398 if (clippedRect.intersect(rtRect)) {
1399 rect = &clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001400 } else {
1401 return;
1402 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001403 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001404 this->flushRenderTarget(rect);
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001405 this->flushScissor(rect);
bsalomon@google.com74b98712011-11-11 19:46:16 +00001406
1407 GrGLfloat r, g, b, a;
1408 static const GrGLfloat scale255 = 1.f / 255.f;
1409 a = GrColorUnpackA(color) * scale255;
1410 GrGLfloat scaleRGB = scale255;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001411 if (GrPixelConfigIsUnpremultiplied(rt->config())) {
bsalomon@google.com74b98712011-11-11 19:46:16 +00001412 scaleRGB *= a;
1413 }
1414 r = GrColorUnpackR(color) * scaleRGB;
1415 g = GrColorUnpackG(color) * scaleRGB;
1416 b = GrColorUnpackB(color) * scaleRGB;
1417
1418 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001419 fHWDrawState.disableState(GrDrawState::kNoColorWrites_StateBit);
bsalomon@google.com74b98712011-11-11 19:46:16 +00001420 GL_CALL(ClearColor(r, g, b, a));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001421 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001422}
1423
bsalomon@google.comedc177d2011-08-05 15:46:40 +00001424void GrGpuGL::clearStencil() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001425 if (NULL == this->getDrawState().getRenderTarget()) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001426 return;
1427 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001428
1429 this->flushRenderTarget(&GrIRect::EmptyIRect());
1430
reed@google.comac10a2d2010-12-22 21:39:39 +00001431 if (fHWBounds.fScissorEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001432 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001433 fHWBounds.fScissorEnabled = false;
reed@google.comac10a2d2010-12-22 21:39:39 +00001434 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001435 GL_CALL(StencilMask(0xffffffff));
1436 GL_CALL(ClearStencil(0));
1437 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001438 fHWDrawState.stencil()->invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001439}
1440
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001441void GrGpuGL::clearStencilClip(const GrIRect& rect, bool insideClip) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001442 const GrDrawState& drawState = this->getDrawState();
1443 const GrRenderTarget* rt = drawState.getRenderTarget();
1444 GrAssert(NULL != rt);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001445
1446 // this should only be called internally when we know we have a
1447 // stencil buffer.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001448 GrAssert(NULL != rt->getStencilBuffer());
1449 GrGLint stencilBitCount = rt->getStencilBuffer()->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001450#if 0
reed@google.comac10a2d2010-12-22 21:39:39 +00001451 GrAssert(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00001452 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001453#else
1454 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001455 // ANGLE a partial stencil mask will cause clears to be
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001456 // turned into draws. Our contract on GrDrawTarget says that
1457 // changing the clip between stencil passes may or may not
1458 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00001459 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001460#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001461 GrGLint value;
1462 if (insideClip) {
1463 value = (1 << (stencilBitCount - 1));
1464 } else {
1465 value = 0;
1466 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001467 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001468 this->flushScissor(&rect);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001469 GL_CALL(StencilMask(clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001470 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001471 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001472 fHWDrawState.stencil()->invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001473}
1474
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001475void GrGpuGL::onForceRenderTargetFlush() {
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001476 this->flushRenderTarget(&GrIRect::EmptyIRect());
reed@google.comac10a2d2010-12-22 21:39:39 +00001477}
1478
bsalomon@google.comc4364992011-11-07 15:54:49 +00001479bool GrGpuGL::readPixelsWillPayForYFlip(GrRenderTarget* renderTarget,
1480 int left, int top,
1481 int width, int height,
1482 GrPixelConfig config,
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001483 size_t rowBytes) const {
1484 // if GL can do the flip then we'll never pay for it.
1485 if (this->glCaps().fPackFlipYSupport) {
1486 return false;
1487 }
1488
1489 // If we have to do memcpy to handle non-trim rowBytes then we
bsalomon@google.com7107fa72011-11-10 14:54:14 +00001490 // get the flip for free. Otherwise it costs.
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001491 if (this->glCaps().fPackRowLengthSupport) {
1492 return true;
1493 }
1494 // If we have to do memcpys to handle rowBytes then y-flip is free
1495 // Note the rowBytes might be tight to the passed in data, but if data
1496 // gets clipped in x to the target the rowBytes will no longer be tight.
1497 if (left >= 0 && (left + width) < renderTarget->width()) {
1498 return 0 == rowBytes ||
1499 GrBytesPerPixel(config) * width == rowBytes;
1500 } else {
1501 return false;
1502 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001503}
1504
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001505bool GrGpuGL::onReadPixels(GrRenderTarget* target,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001506 int left, int top,
1507 int width, int height,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001508 GrPixelConfig config,
1509 void* buffer,
1510 size_t rowBytes,
1511 bool invertY) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001512 GrGLenum format;
1513 GrGLenum type;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00001514 if (!this->configToGLFormats(config, false, NULL, &format, &type)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001515 return false;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001516 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001517 size_t bpp = GrBytesPerPixel(config);
1518 if (!adjust_pixel_ops_params(target->width(), target->height(), bpp,
1519 &left, &top, &width, &height,
1520 const_cast<const void**>(&buffer),
1521 &rowBytes)) {
1522 return false;
1523 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001524
bsalomon@google.comc6980972011-11-02 19:57:21 +00001525 // resolve the render target if necessary
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001526 GrGLRenderTarget* tgt = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001527 GrDrawState::AutoRenderTargetRestore artr;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001528 switch (tgt->getResolveType()) {
1529 case GrGLRenderTarget::kCantResolve_ResolveType:
1530 return false;
1531 case GrGLRenderTarget::kAutoResolves_ResolveType:
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001532 artr.set(this->drawState(), target);
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001533 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001534 break;
1535 case GrGLRenderTarget::kCanResolve_ResolveType:
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001536 this->onResolveRenderTarget(tgt);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001537 // we don't track the state of the READ FBO ID.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001538 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1539 tgt->textureFBOID()));
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001540 break;
1541 default:
1542 GrCrash("Unknown resolve type");
reed@google.comac10a2d2010-12-22 21:39:39 +00001543 }
1544
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001545 const GrGLIRect& glvp = tgt->getViewport();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001546
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001547 // the read rect is viewport-relative
1548 GrGLIRect readRect;
1549 readRect.setRelativeTo(glvp, left, top, width, height);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001550
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001551 size_t tightRowBytes = bpp * width;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001552 if (0 == rowBytes) {
1553 rowBytes = tightRowBytes;
1554 }
1555 size_t readDstRowBytes = tightRowBytes;
1556 void* readDst = buffer;
1557
1558 // determine if GL can read using the passed rowBytes or if we need
1559 // a scratch buffer.
1560 SkAutoSMalloc<32 * sizeof(GrColor)> scratch;
1561 if (rowBytes != tightRowBytes) {
bsalomon@google.com7107fa72011-11-10 14:54:14 +00001562 if (this->glCaps().fPackRowLengthSupport) {
bsalomon@google.comc6980972011-11-02 19:57:21 +00001563 GrAssert(!(rowBytes % sizeof(GrColor)));
1564 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, rowBytes / sizeof(GrColor)));
1565 readDstRowBytes = rowBytes;
1566 } else {
1567 scratch.reset(tightRowBytes * height);
1568 readDst = scratch.get();
1569 }
1570 }
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001571 if (!invertY && this->glCaps().fPackFlipYSupport) {
1572 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 1));
1573 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001574 GL_CALL(ReadPixels(readRect.fLeft, readRect.fBottom,
1575 readRect.fWidth, readRect.fHeight,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001576 format, type, readDst));
1577 if (readDstRowBytes != tightRowBytes) {
bsalomon@google.com7107fa72011-11-10 14:54:14 +00001578 GrAssert(this->glCaps().fPackRowLengthSupport);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001579 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
1580 }
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001581 if (!invertY && this->glCaps().fPackFlipYSupport) {
1582 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 0));
1583 invertY = true;
1584 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001585
1586 // now reverse the order of the rows, since GL's are bottom-to-top, but our
bsalomon@google.comc6980972011-11-02 19:57:21 +00001587 // API presents top-to-bottom. We must preserve the padding contents. Note
1588 // that the above readPixels did not overwrite the padding.
1589 if (readDst == buffer) {
1590 GrAssert(rowBytes == readDstRowBytes);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001591 if (!invertY) {
1592 scratch.reset(tightRowBytes);
1593 void* tmpRow = scratch.get();
1594 // flip y in-place by rows
1595 const int halfY = height >> 1;
1596 char* top = reinterpret_cast<char*>(buffer);
1597 char* bottom = top + (height - 1) * rowBytes;
1598 for (int y = 0; y < halfY; y++) {
1599 memcpy(tmpRow, top, tightRowBytes);
1600 memcpy(top, bottom, tightRowBytes);
1601 memcpy(bottom, tmpRow, tightRowBytes);
1602 top += rowBytes;
1603 bottom -= rowBytes;
1604 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001605 }
1606 } else {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001607 GrAssert(readDst != buffer); GrAssert(rowBytes != tightRowBytes);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001608 // copy from readDst to buffer while flipping y
1609 const int halfY = height >> 1;
1610 const char* src = reinterpret_cast<const char*>(readDst);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001611 char* dst = reinterpret_cast<char*>(buffer);
1612 if (!invertY) {
1613 dst += (height-1) * rowBytes;
1614 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001615 for (int y = 0; y < height; y++) {
1616 memcpy(dst, src, tightRowBytes);
1617 src += readDstRowBytes;
bsalomon@google.comc4364992011-11-07 15:54:49 +00001618 if (invertY) {
1619 dst += rowBytes;
1620 } else {
1621 dst -= rowBytes;
1622 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001623 }
1624 }
1625 return true;
1626}
1627
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001628void GrGpuGL::flushRenderTarget(const GrIRect* bound) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001629
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001630 GrGLRenderTarget* rt =
1631 static_cast<GrGLRenderTarget*>(this->drawState()->getRenderTarget());
1632 GrAssert(NULL != rt);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001633
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001634 if (fHWDrawState.getRenderTarget() != rt) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001635 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, rt->renderFBOID()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001636 #if GR_COLLECT_STATS
1637 ++fStats.fRenderTargetChngCnt;
1638 #endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001639 #if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001640 GrGLenum status;
1641 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001642 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
reed@google.comb9255d52011-06-13 18:54:59 +00001643 GrPrintf("GrGpuGL::flushRenderTarget glCheckFramebufferStatus %x\n", status);
reed@google.comac10a2d2010-12-22 21:39:39 +00001644 }
1645 #endif
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001646 fDirtyFlags.fRenderTargetChanged = true;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001647 fHWDrawState.setRenderTarget(rt);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001648 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com649a8622011-03-10 14:53:38 +00001649 if (fHWBounds.fViewportRect != vp) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001650 vp.pushToGLViewport(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001651 fHWBounds.fViewportRect = vp;
1652 }
1653 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001654 if (NULL == bound || !bound->isEmpty()) {
1655 rt->flagAsNeedingResolve(bound);
1656 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001657}
1658
twiz@google.com0f31ca72011-03-18 17:38:11 +00001659GrGLenum gPrimitiveType2GLMode[] = {
1660 GR_GL_TRIANGLES,
1661 GR_GL_TRIANGLE_STRIP,
1662 GR_GL_TRIANGLE_FAN,
1663 GR_GL_POINTS,
1664 GR_GL_LINES,
1665 GR_GL_LINE_STRIP
reed@google.comac10a2d2010-12-22 21:39:39 +00001666};
1667
bsalomon@google.comd302f142011-03-03 13:54:13 +00001668#define SWAP_PER_DRAW 0
1669
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001670#if SWAP_PER_DRAW
bsalomon@google.comd302f142011-03-03 13:54:13 +00001671 #if GR_MAC_BUILD
1672 #include <AGL/agl.h>
1673 #elif GR_WIN32_BUILD
1674 void SwapBuf() {
1675 DWORD procID = GetCurrentProcessId();
1676 HWND hwnd = GetTopWindow(GetDesktopWindow());
1677 while(hwnd) {
1678 DWORD wndProcID = 0;
1679 GetWindowThreadProcessId(hwnd, &wndProcID);
1680 if(wndProcID == procID) {
1681 SwapBuffers(GetDC(hwnd));
1682 }
1683 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
1684 }
1685 }
1686 #endif
1687#endif
1688
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001689void GrGpuGL::onGpuDrawIndexed(GrPrimitiveType type,
1690 uint32_t startVertex,
1691 uint32_t startIndex,
1692 uint32_t vertexCount,
1693 uint32_t indexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001694 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1695
twiz@google.com0f31ca72011-03-18 17:38:11 +00001696 GrGLvoid* indices = (GrGLvoid*)(sizeof(uint16_t) * startIndex);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001697
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001698 GrAssert(NULL != fHWGeometryState.fIndexBuffer);
1699 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1700
1701 // our setupGeometry better have adjusted this to zero since
1702 // DrawElements always draws from the begining of the arrays for idx 0.
1703 GrAssert(0 == startVertex);
reed@google.comac10a2d2010-12-22 21:39:39 +00001704
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001705 GL_CALL(DrawElements(gPrimitiveType2GLMode[type], indexCount,
1706 GR_GL_UNSIGNED_SHORT, indices));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001707#if SWAP_PER_DRAW
1708 glFlush();
1709 #if GR_MAC_BUILD
1710 aglSwapBuffers(aglGetCurrentContext());
1711 int set_a_break_pt_here = 9;
1712 aglSwapBuffers(aglGetCurrentContext());
1713 #elif GR_WIN32_BUILD
1714 SwapBuf();
1715 int set_a_break_pt_here = 9;
1716 SwapBuf();
1717 #endif
1718#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001719}
1720
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001721void GrGpuGL::onGpuDrawNonIndexed(GrPrimitiveType type,
1722 uint32_t startVertex,
1723 uint32_t vertexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001724 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1725
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001726 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1727
1728 // our setupGeometry better have adjusted this to zero.
1729 // DrawElements doesn't take an offset so we always adjus the startVertex.
1730 GrAssert(0 == startVertex);
1731
1732 // pass 0 for parameter first. We have to adjust gl*Pointer() to
1733 // account for startVertex in the DrawElements case. So we always
1734 // rely on setupGeometry to have accounted for startVertex.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001735 GL_CALL(DrawArrays(gPrimitiveType2GLMode[type], 0, vertexCount));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001736#if SWAP_PER_DRAW
1737 glFlush();
1738 #if GR_MAC_BUILD
1739 aglSwapBuffers(aglGetCurrentContext());
1740 int set_a_break_pt_here = 9;
1741 aglSwapBuffers(aglGetCurrentContext());
1742 #elif GR_WIN32_BUILD
1743 SwapBuf();
1744 int set_a_break_pt_here = 9;
1745 SwapBuf();
1746 #endif
1747#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001748}
1749
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001750void GrGpuGL::onResolveRenderTarget(GrRenderTarget* target) {
1751
1752 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(target);
reed@google.comac10a2d2010-12-22 21:39:39 +00001753
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001754 if (rt->needsResolve()) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001755 GrAssert(GLCaps::kNone_MSFBO != fGLCaps.fMSFBOType);
reed@google.comac10a2d2010-12-22 21:39:39 +00001756 GrAssert(rt->textureFBOID() != rt->renderFBOID());
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001757 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1758 rt->renderFBOID()));
1759 GL_CALL(BindFramebuffer(GR_GL_DRAW_FRAMEBUFFER,
1760 rt->textureFBOID()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001761 #if GR_COLLECT_STATS
1762 ++fStats.fRenderTargetChngCnt;
1763 #endif
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001764 // make sure we go through flushRenderTarget() since we've modified
1765 // the bound DRAW FBO ID.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001766 fHWDrawState.setRenderTarget(NULL);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001767 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001768 const GrIRect dirtyRect = rt->getResolveRect();
1769 GrGLIRect r;
1770 r.setRelativeTo(vp, dirtyRect.fLeft, dirtyRect.fTop,
1771 dirtyRect.width(), dirtyRect.height());
reed@google.comac10a2d2010-12-22 21:39:39 +00001772
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001773 if (GLCaps::kAppleES_MSFBO == fGLCaps.fMSFBOType) {
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001774 // Apple's extension uses the scissor as the blit bounds.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001775 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1776 GL_CALL(Scissor(r.fLeft, r.fBottom,
1777 r.fWidth, r.fHeight));
1778 GL_CALL(ResolveMultisampleFramebuffer());
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001779 fHWBounds.fScissorRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001780 fHWBounds.fScissorEnabled = true;
1781 } else {
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001782 if (GLCaps::kDesktopARB_MSFBO != fGLCaps.fMSFBOType) {
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001783 // this respects the scissor during the blit, so disable it.
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001784 GrAssert(GLCaps::kDesktopEXT_MSFBO == fGLCaps.fMSFBOType);
1785 this->flushScissor(NULL);
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001786 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001787 int right = r.fLeft + r.fWidth;
1788 int top = r.fBottom + r.fHeight;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001789 GL_CALL(BlitFramebuffer(r.fLeft, r.fBottom, right, top,
1790 r.fLeft, r.fBottom, right, top,
1791 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001792 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001793 rt->flagAsResolved();
reed@google.comac10a2d2010-12-22 21:39:39 +00001794 }
1795}
1796
twiz@google.com0f31ca72011-03-18 17:38:11 +00001797static const GrGLenum grToGLStencilFunc[] = {
1798 GR_GL_ALWAYS, // kAlways_StencilFunc
1799 GR_GL_NEVER, // kNever_StencilFunc
1800 GR_GL_GREATER, // kGreater_StencilFunc
1801 GR_GL_GEQUAL, // kGEqual_StencilFunc
1802 GR_GL_LESS, // kLess_StencilFunc
1803 GR_GL_LEQUAL, // kLEqual_StencilFunc,
1804 GR_GL_EQUAL, // kEqual_StencilFunc,
1805 GR_GL_NOTEQUAL, // kNotEqual_StencilFunc,
bsalomon@google.comd302f142011-03-03 13:54:13 +00001806};
1807GR_STATIC_ASSERT(GR_ARRAY_COUNT(grToGLStencilFunc) == kBasicStencilFuncCount);
1808GR_STATIC_ASSERT(0 == kAlways_StencilFunc);
1809GR_STATIC_ASSERT(1 == kNever_StencilFunc);
1810GR_STATIC_ASSERT(2 == kGreater_StencilFunc);
1811GR_STATIC_ASSERT(3 == kGEqual_StencilFunc);
1812GR_STATIC_ASSERT(4 == kLess_StencilFunc);
1813GR_STATIC_ASSERT(5 == kLEqual_StencilFunc);
1814GR_STATIC_ASSERT(6 == kEqual_StencilFunc);
1815GR_STATIC_ASSERT(7 == kNotEqual_StencilFunc);
1816
twiz@google.com0f31ca72011-03-18 17:38:11 +00001817static const GrGLenum grToGLStencilOp[] = {
1818 GR_GL_KEEP, // kKeep_StencilOp
1819 GR_GL_REPLACE, // kReplace_StencilOp
1820 GR_GL_INCR_WRAP, // kIncWrap_StencilOp
1821 GR_GL_INCR, // kIncClamp_StencilOp
1822 GR_GL_DECR_WRAP, // kDecWrap_StencilOp
1823 GR_GL_DECR, // kDecClamp_StencilOp
1824 GR_GL_ZERO, // kZero_StencilOp
1825 GR_GL_INVERT, // kInvert_StencilOp
bsalomon@google.comd302f142011-03-03 13:54:13 +00001826};
1827GR_STATIC_ASSERT(GR_ARRAY_COUNT(grToGLStencilOp) == kStencilOpCount);
1828GR_STATIC_ASSERT(0 == kKeep_StencilOp);
1829GR_STATIC_ASSERT(1 == kReplace_StencilOp);
1830GR_STATIC_ASSERT(2 == kIncWrap_StencilOp);
1831GR_STATIC_ASSERT(3 == kIncClamp_StencilOp);
1832GR_STATIC_ASSERT(4 == kDecWrap_StencilOp);
1833GR_STATIC_ASSERT(5 == kDecClamp_StencilOp);
1834GR_STATIC_ASSERT(6 == kZero_StencilOp);
1835GR_STATIC_ASSERT(7 == kInvert_StencilOp);
1836
reed@google.comac10a2d2010-12-22 21:39:39 +00001837void GrGpuGL::flushStencil() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001838 const GrDrawState& drawState = this->getDrawState();
1839
1840 const GrStencilSettings* settings = &drawState.getStencil();
reed@google.comac10a2d2010-12-22 21:39:39 +00001841
1842 // use stencil for clipping if clipping is enabled and the clip
1843 // has been written into the stencil.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001844 bool stencilClip = fClipInStencil && drawState.isClipState();
1845 bool drawClipToStencil =
1846 drawState.isStateFlagEnabled(kModifyStencilClip_StateBit);
bsalomon@google.com39dab772012-01-03 19:39:31 +00001847 bool stencilChange = (fHWDrawState.getStencil() != *settings) ||
1848 (fHWStencilClip != stencilClip) ||
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001849 (fHWDrawState.isStateFlagEnabled(kModifyStencilClip_StateBit) !=
1850 drawClipToStencil);
reed@google.comac10a2d2010-12-22 21:39:39 +00001851
1852 if (stencilChange) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001853
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001854 // we can't simultaneously perform stencil-clipping and
1855 // modify the stencil clip
1856 GrAssert(!stencilClip || !drawClipToStencil);
reed@google.comac10a2d2010-12-22 21:39:39 +00001857
bsalomon@google.comd302f142011-03-03 13:54:13 +00001858 if (settings->isDisabled()) {
1859 if (stencilClip) {
epoger@google.com8722d7c2012-01-31 17:18:58 +00001860 settings = &gClipStencilSettings;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001861 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001862 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00001863
1864 if (settings->isDisabled()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001865 GL_CALL(Disable(GR_GL_STENCIL_TEST));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001866 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001867 GL_CALL(Enable(GR_GL_STENCIL_TEST));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001868 #if GR_DEBUG
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001869 if (!this->getCaps().fStencilWrapOpsSupport) {
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001870 GrAssert(settings->frontPassOp() != kIncWrap_StencilOp);
1871 GrAssert(settings->frontPassOp() != kDecWrap_StencilOp);
1872 GrAssert(settings->frontFailOp() != kIncWrap_StencilOp);
1873 GrAssert(settings->backFailOp() != kDecWrap_StencilOp);
1874 GrAssert(settings->backPassOp() != kIncWrap_StencilOp);
1875 GrAssert(settings->backPassOp() != kDecWrap_StencilOp);
1876 GrAssert(settings->backFailOp() != kIncWrap_StencilOp);
1877 GrAssert(settings->frontFailOp() != kDecWrap_StencilOp);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001878 }
1879 #endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001880 int stencilBits = 0;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001881 GrStencilBuffer* stencilBuffer =
1882 drawState.getRenderTarget()->getStencilBuffer();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001883 if (NULL != stencilBuffer) {
1884 stencilBits = stencilBuffer->bits();
1885 }
1886 // TODO: dynamically attach a stencil buffer
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001887 GrAssert(stencilBits || settings->isDisabled());
bsalomon@google.com2cdfade2011-11-23 16:53:42 +00001888
1889 GrGLuint clipStencilMask = 0;
1890 GrGLuint userStencilMask = ~0;
1891 if (stencilBits > 0) {
1892 clipStencilMask = 1 << (stencilBits - 1);
1893 userStencilMask = clipStencilMask - 1;
1894 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00001895
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001896 unsigned int frontRef = settings->frontFuncRef();
1897 unsigned int frontMask = settings->frontFuncMask();
1898 unsigned int frontWriteMask = settings->frontWriteMask();
twiz@google.com0f31ca72011-03-18 17:38:11 +00001899 GrGLenum frontFunc;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001900
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001901 if (drawClipToStencil) {
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001902 GrAssert(settings->frontFunc() < kBasicStencilFuncCount);
1903 frontFunc = grToGLStencilFunc[settings->frontFunc()];
bsalomon@google.comd302f142011-03-03 13:54:13 +00001904 } else {
tomhudson@google.com62b09682011-11-09 16:39:17 +00001905 frontFunc = grToGLStencilFunc[ConvertStencilFunc(
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001906 stencilClip, settings->frontFunc())];
bsalomon@google.comd302f142011-03-03 13:54:13 +00001907
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001908 ConvertStencilFuncAndMask(settings->frontFunc(),
bsalomon@google.comd302f142011-03-03 13:54:13 +00001909 stencilClip,
1910 clipStencilMask,
1911 userStencilMask,
1912 &frontRef,
1913 &frontMask);
1914 frontWriteMask &= userStencilMask;
1915 }
tomhudson@google.com62b09682011-11-09 16:39:17 +00001916 GrAssert((size_t)
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001917 settings->frontFailOp() < GR_ARRAY_COUNT(grToGLStencilOp));
tomhudson@google.com62b09682011-11-09 16:39:17 +00001918 GrAssert((size_t)
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001919 settings->frontPassOp() < GR_ARRAY_COUNT(grToGLStencilOp));
tomhudson@google.com62b09682011-11-09 16:39:17 +00001920 GrAssert((size_t)
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001921 settings->backFailOp() < GR_ARRAY_COUNT(grToGLStencilOp));
tomhudson@google.com62b09682011-11-09 16:39:17 +00001922 GrAssert((size_t)
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001923 settings->backPassOp() < GR_ARRAY_COUNT(grToGLStencilOp));
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001924 if (this->getCaps().fTwoSidedStencilSupport) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001925 GrGLenum backFunc;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001926
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001927 unsigned int backRef = settings->backFuncRef();
1928 unsigned int backMask = settings->backFuncMask();
1929 unsigned int backWriteMask = settings->backWriteMask();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001930
1931
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001932 if (drawClipToStencil) {
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001933 GrAssert(settings->backFunc() < kBasicStencilFuncCount);
1934 backFunc = grToGLStencilFunc[settings->backFunc()];
bsalomon@google.comd302f142011-03-03 13:54:13 +00001935 } else {
tomhudson@google.com62b09682011-11-09 16:39:17 +00001936 backFunc = grToGLStencilFunc[ConvertStencilFunc(
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001937 stencilClip, settings->backFunc())];
1938 ConvertStencilFuncAndMask(settings->backFunc(),
bsalomon@google.comd302f142011-03-03 13:54:13 +00001939 stencilClip,
1940 clipStencilMask,
1941 userStencilMask,
1942 &backRef,
1943 &backMask);
1944 backWriteMask &= userStencilMask;
1945 }
1946
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001947 GL_CALL(StencilFuncSeparate(GR_GL_FRONT, frontFunc,
1948 frontRef, frontMask));
1949 GL_CALL(StencilMaskSeparate(GR_GL_FRONT, frontWriteMask));
1950 GL_CALL(StencilFuncSeparate(GR_GL_BACK, backFunc,
1951 backRef, backMask));
1952 GL_CALL(StencilMaskSeparate(GR_GL_BACK, backWriteMask));
1953 GL_CALL(StencilOpSeparate(GR_GL_FRONT,
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001954 grToGLStencilOp[settings->frontFailOp()],
1955 grToGLStencilOp[settings->frontPassOp()],
1956 grToGLStencilOp[settings->frontPassOp()]));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001957
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001958 GL_CALL(StencilOpSeparate(GR_GL_BACK,
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001959 grToGLStencilOp[settings->backFailOp()],
1960 grToGLStencilOp[settings->backPassOp()],
1961 grToGLStencilOp[settings->backPassOp()]));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001962 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001963 GL_CALL(StencilFunc(frontFunc, frontRef, frontMask));
1964 GL_CALL(StencilMask(frontWriteMask));
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001965 GL_CALL(StencilOp(grToGLStencilOp[settings->frontFailOp()],
1966 grToGLStencilOp[settings->frontPassOp()],
1967 grToGLStencilOp[settings->frontPassOp()]));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001968 }
1969 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001970 *fHWDrawState.stencil() = *settings;
reed@google.comac10a2d2010-12-22 21:39:39 +00001971 fHWStencilClip = stencilClip;
1972 }
1973}
1974
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001975void GrGpuGL::flushAAState(GrPrimitiveType type) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001976 const GrRenderTarget* rt = this->getDrawState().getRenderTarget();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001977 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001978 // ES doesn't support toggling GL_MULTISAMPLE and doesn't have
1979 // smooth lines.
1980
1981 // we prefer smooth lines over multisampled lines
1982 // msaa should be disabled if drawing smooth lines.
bsalomon@google.com0650e812011-04-08 18:07:53 +00001983 if (GrIsPrimTypeLines(type)) {
bsalomon@google.com471d4712011-08-23 15:45:25 +00001984 bool smooth = this->willUseHWAALines();
bsalomon@google.com0650e812011-04-08 18:07:53 +00001985 if (!fHWAAState.fSmoothLineEnabled && smooth) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001986 GL_CALL(Enable(GR_GL_LINE_SMOOTH));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001987 fHWAAState.fSmoothLineEnabled = true;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001988 } else if (fHWAAState.fSmoothLineEnabled && !smooth) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001989 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001990 fHWAAState.fSmoothLineEnabled = false;
1991 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001992 if (rt->isMultisampled() &&
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001993 fHWAAState.fMSAAEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001994 GL_CALL(Disable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001995 fHWAAState.fMSAAEnabled = false;
1996 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001997 } else if (rt->isMultisampled() &&
1998 this->getDrawState().isHWAntialiasState() !=
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001999 fHWAAState.fMSAAEnabled) {
2000 if (fHWAAState.fMSAAEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002001 GL_CALL(Disable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00002002 fHWAAState.fMSAAEnabled = false;
2003 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002004 GL_CALL(Enable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00002005 fHWAAState.fMSAAEnabled = true;
2006 }
2007 }
2008 }
2009}
2010
bsalomon@google.com86c1f712011-10-12 14:54:26 +00002011void GrGpuGL::flushBlend(GrPrimitiveType type,
2012 GrBlendCoeff srcCoeff,
bsalomon@google.com271cffc2011-05-20 14:13:56 +00002013 GrBlendCoeff dstCoeff) {
bsalomon@google.com471d4712011-08-23 15:45:25 +00002014 if (GrIsPrimTypeLines(type) && this->willUseHWAALines()) {
bsalomon@google.com0650e812011-04-08 18:07:53 +00002015 if (fHWBlendDisabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002016 GL_CALL(Enable(GR_GL_BLEND));
bsalomon@google.com0650e812011-04-08 18:07:53 +00002017 fHWBlendDisabled = false;
2018 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002019 if (kSA_BlendCoeff != fHWDrawState.getSrcBlendCoeff() ||
2020 kISA_BlendCoeff != fHWDrawState.getDstBlendCoeff()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002021 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[kSA_BlendCoeff],
2022 gXfermodeCoeff2Blend[kISA_BlendCoeff]));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002023 fHWDrawState.setBlendFunc(kSA_BlendCoeff, kISA_BlendCoeff);
bsalomon@google.com0650e812011-04-08 18:07:53 +00002024 }
2025 } else {
bsalomon@google.com86c1f712011-10-12 14:54:26 +00002026 // any optimization to disable blending should
2027 // have already been applied and tweaked the coeffs
2028 // to (1, 0).
2029 bool blendOff = kOne_BlendCoeff == srcCoeff &&
2030 kZero_BlendCoeff == dstCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002031 if (fHWBlendDisabled != blendOff) {
2032 if (blendOff) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002033 GL_CALL(Disable(GR_GL_BLEND));
bsalomon@google.com0650e812011-04-08 18:07:53 +00002034 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002035 GL_CALL(Enable(GR_GL_BLEND));
bsalomon@google.com0650e812011-04-08 18:07:53 +00002036 }
2037 fHWBlendDisabled = blendOff;
2038 }
2039 if (!blendOff) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002040 if (fHWDrawState.getSrcBlendCoeff() != srcCoeff ||
2041 fHWDrawState.getDstBlendCoeff() != dstCoeff) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002042 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
2043 gXfermodeCoeff2Blend[dstCoeff]));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002044 fHWDrawState.setBlendFunc(srcCoeff, dstCoeff);
bsalomon@google.com0650e812011-04-08 18:07:53 +00002045 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002046 GrColor blendConst = fCurrDrawState.getBlendConstant();
bsalomon@google.com271cffc2011-05-20 14:13:56 +00002047 if ((BlendCoeffReferencesConstant(srcCoeff) ||
2048 BlendCoeffReferencesConstant(dstCoeff)) &&
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002049 fHWDrawState.getBlendConstant() != blendConst) {
bsalomon@google.com0650e812011-04-08 18:07:53 +00002050
2051 float c[] = {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002052 GrColorUnpackR(blendConst) / 255.f,
2053 GrColorUnpackG(blendConst) / 255.f,
2054 GrColorUnpackB(blendConst) / 255.f,
2055 GrColorUnpackA(blendConst) / 255.f
bsalomon@google.com0650e812011-04-08 18:07:53 +00002056 };
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002057 GL_CALL(BlendColor(c[0], c[1], c[2], c[3]));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002058 fHWDrawState.setBlendConstant(blendConst);
bsalomon@google.com0650e812011-04-08 18:07:53 +00002059 }
2060 }
2061 }
2062}
2063
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002064namespace {
2065
2066unsigned gr_to_gl_filter(GrSamplerState::Filter filter) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00002067 switch (filter) {
2068 case GrSamplerState::kBilinear_Filter:
2069 case GrSamplerState::k4x4Downsample_Filter:
2070 return GR_GL_LINEAR;
2071 case GrSamplerState::kNearest_Filter:
2072 case GrSamplerState::kConvolution_Filter:
2073 return GR_GL_NEAREST;
2074 default:
2075 GrAssert(!"Unknown filter type");
2076 return GR_GL_LINEAR;
2077 }
2078}
2079
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002080const GrGLenum* get_swizzle(GrPixelConfig config,
2081 const GrSamplerState& sampler) {
2082 if (GrPixelConfigIsAlphaOnly(config)) {
2083 static const GrGLenum gAlphaSmear[] = { GR_GL_ALPHA, GR_GL_ALPHA,
2084 GR_GL_ALPHA, GR_GL_ALPHA };
2085 return gAlphaSmear;
2086 } else if (sampler.swapsRAndB()) {
2087 static const GrGLenum gRedBlueSwap[] = { GR_GL_BLUE, GR_GL_GREEN,
2088 GR_GL_RED, GR_GL_ALPHA };
2089 return gRedBlueSwap;
2090 } else {
2091 static const GrGLenum gStraight[] = { GR_GL_RED, GR_GL_GREEN,
2092 GR_GL_BLUE, GR_GL_ALPHA };
2093 return gStraight;
2094 }
2095}
2096
2097void set_tex_swizzle(GrGLenum swizzle[4], const GrGLInterface* gl) {
2098 // should add texparameteri to interface to make 1 instead of 4 calls here
2099 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
2100 GR_GL_TEXTURE_SWIZZLE_R,
2101 swizzle[0]));
2102 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
2103 GR_GL_TEXTURE_SWIZZLE_G,
2104 swizzle[1]));
2105 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
2106 GR_GL_TEXTURE_SWIZZLE_B,
2107 swizzle[2]));
2108 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
2109 GR_GL_TEXTURE_SWIZZLE_A,
2110 swizzle[3]));
2111}
2112}
2113
bsalomon@google.comffca4002011-02-22 20:34:01 +00002114bool GrGpuGL::flushGLStateCommon(GrPrimitiveType type) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002115
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002116 GrDrawState* drawState = this->drawState();
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002117 // GrGpu::setupClipAndFlushState should have already checked this
2118 // and bailed if not true.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002119 GrAssert(NULL != drawState->getRenderTarget());
reed@google.comac10a2d2010-12-22 21:39:39 +00002120
tomhudson@google.com93813632011-10-27 20:21:16 +00002121 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002122 // bind texture and set sampler state
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002123 if (this->isStageEnabled(s)) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002124 GrGLTexture* nextTexture =
2125 static_cast<GrGLTexture*>(drawState->getTexture(s));
reed@google.comac10a2d2010-12-22 21:39:39 +00002126
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002127 // true for now, but maybe not with GrEffect.
2128 GrAssert(NULL != nextTexture);
2129 // if we created a rt/tex and rendered to it without using a
2130 // texture and now we're texuring from the rt it will still be
2131 // the last bound texture, but it needs resolving. So keep this
2132 // out of the "last != next" check.
2133 GrGLRenderTarget* texRT =
2134 static_cast<GrGLRenderTarget*>(nextTexture->asRenderTarget());
2135 if (NULL != texRT) {
bsalomon@google.com75f9f252012-01-31 13:35:56 +00002136 this->onResolveRenderTarget(texRT);
reed@google.comac10a2d2010-12-22 21:39:39 +00002137 }
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002138
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002139 if (fHWDrawState.getTexture(s) != nextTexture) {
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002140 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002141 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, nextTexture->textureID()));
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002142 #if GR_COLLECT_STATS
2143 ++fStats.fTextureChngCnt;
2144 #endif
2145 //GrPrintf("---- bindtexture %d\n", nextTexture->textureID());
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002146 fHWDrawState.setTexture(s, nextTexture);
bsalomon@google.comcd19a5f2011-06-15 14:00:23 +00002147 // The texture matrix has to compensate for texture width/height
2148 // and NPOT-embedded-in-POT
2149 fDirtyFlags.fTextureChangedMask |= (1 << s);
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002150 }
2151
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002152 const GrSamplerState& sampler = drawState->getSampler(s);
bsalomon@google.com80d09b92011-11-05 21:21:13 +00002153 ResetTimestamp timestamp;
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002154 const GrGLTexture::TexParams& oldTexParams =
bsalomon@google.com80d09b92011-11-05 21:21:13 +00002155 nextTexture->getCachedTexParams(&timestamp);
2156 bool setAll = timestamp < this->getResetTimestamp();
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002157 GrGLTexture::TexParams newTexParams;
2158
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002159 newTexParams.fFilter = gr_to_gl_filter(sampler.getFilter());
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00002160
bsalomon@google.com1dcf5062011-11-14 19:29:53 +00002161 const GrGLenum* wraps = GrGLTexture::WrapMode2GLWrap();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002162 newTexParams.fWrapS = wraps[sampler.getWrapX()];
2163 newTexParams.fWrapT = wraps[sampler.getWrapY()];
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002164 memcpy(newTexParams.fSwizzleRGBA,
2165 get_swizzle(nextTexture->config(), sampler),
2166 sizeof(newTexParams.fSwizzleRGBA));
2167 if (setAll || newTexParams.fFilter != oldTexParams.fFilter) {
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002168 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002169 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002170 GR_GL_TEXTURE_MAG_FILTER,
2171 newTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002172 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002173 GR_GL_TEXTURE_MIN_FILTER,
2174 newTexParams.fFilter));
2175 }
2176 if (setAll || newTexParams.fWrapS != oldTexParams.fWrapS) {
2177 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002178 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002179 GR_GL_TEXTURE_WRAP_S,
2180 newTexParams.fWrapS));
2181 }
2182 if (setAll || newTexParams.fWrapT != oldTexParams.fWrapT) {
2183 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002184 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002185 GR_GL_TEXTURE_WRAP_T,
2186 newTexParams.fWrapT));
2187 }
bsalomon@google.com7107fa72011-11-10 14:54:14 +00002188 if (this->glCaps().fTextureSwizzleSupport &&
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002189 (setAll ||
2190 memcmp(newTexParams.fSwizzleRGBA,
2191 oldTexParams.fSwizzleRGBA,
2192 sizeof(newTexParams.fSwizzleRGBA)))) {
2193 setTextureUnit(s);
2194 set_tex_swizzle(newTexParams.fSwizzleRGBA,
2195 this->glInterface());
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002196 }
bsalomon@google.com80d09b92011-11-05 21:21:13 +00002197 nextTexture->setCachedTexParams(newTexParams,
2198 this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00002199 }
2200 }
2201
bsalomon@google.com8295dc12011-05-02 12:53:34 +00002202 GrIRect* rect = NULL;
2203 GrIRect clipBounds;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002204 if (drawState->isClipState() &&
bsalomon@google.com8295dc12011-05-02 12:53:34 +00002205 fClip.hasConservativeBounds()) {
2206 fClip.getConservativeBounds().roundOut(&clipBounds);
2207 rect = &clipBounds;
2208 }
2209 this->flushRenderTarget(rect);
2210 this->flushAAState(type);
bsalomon@google.com0650e812011-04-08 18:07:53 +00002211
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002212 if (drawState->isDitherState() != fHWDrawState.isDitherState()) {
2213 if (drawState->isDitherState()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002214 GL_CALL(Enable(GR_GL_DITHER));
reed@google.comac10a2d2010-12-22 21:39:39 +00002215 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002216 GL_CALL(Disable(GR_GL_DITHER));
reed@google.comac10a2d2010-12-22 21:39:39 +00002217 }
2218 }
2219
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002220 if (drawState->isColorWriteDisabled() !=
2221 fHWDrawState.isColorWriteDisabled()) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002222 GrGLenum mask;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002223 if (drawState->isColorWriteDisabled()) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002224 mask = GR_GL_FALSE;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002225 } else {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002226 mask = GR_GL_TRUE;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002227 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002228 GL_CALL(ColorMask(mask, mask, mask, mask));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002229 }
2230
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002231 if (fHWDrawState.getDrawFace() != drawState->getDrawFace()) {
2232 switch (fCurrDrawState.getDrawFace()) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002233 case GrDrawState::kCCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002234 GL_CALL(Enable(GR_GL_CULL_FACE));
2235 GL_CALL(CullFace(GR_GL_BACK));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002236 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002237 case GrDrawState::kCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002238 GL_CALL(Enable(GR_GL_CULL_FACE));
2239 GL_CALL(CullFace(GR_GL_FRONT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002240 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002241 case GrDrawState::kBoth_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002242 GL_CALL(Disable(GR_GL_CULL_FACE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002243 break;
2244 default:
2245 GrCrash("Unknown draw face.");
2246 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002247 fHWDrawState.setDrawFace(drawState->getDrawFace());
bsalomon@google.comd302f142011-03-03 13:54:13 +00002248 }
2249
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002250#if GR_DEBUG
reed@google.comac10a2d2010-12-22 21:39:39 +00002251 // check for circular rendering
tomhudson@google.com93813632011-10-27 20:21:16 +00002252 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002253 GrAssert(!this->isStageEnabled(s) ||
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002254 NULL == drawState->getRenderTarget() ||
2255 NULL == drawState->getTexture(s) ||
2256 drawState->getTexture(s)->asRenderTarget() !=
2257 drawState->getRenderTarget());
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002258 }
2259#endif
bsalomon@google.com316f99232011-01-13 21:28:12 +00002260
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002261 this->flushStencil();
reed@google.comac10a2d2010-12-22 21:39:39 +00002262
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002263 // This copy must happen after flushStencil() is called. flushStencil()
2264 // relies on detecting when the kModifyStencilClip_StateBit state has
2265 // changed since the last draw.
2266 fHWDrawState.copyStateFlags(*drawState);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002267 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +00002268}
2269
2270void GrGpuGL::notifyVertexBufferBind(const GrGLVertexBuffer* buffer) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002271 if (fHWGeometryState.fVertexBuffer != buffer) {
2272 fHWGeometryState.fArrayPtrsDirty = true;
2273 fHWGeometryState.fVertexBuffer = buffer;
2274 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002275}
2276
2277void GrGpuGL::notifyVertexBufferDelete(const GrGLVertexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002278 if (fHWGeometryState.fVertexBuffer == buffer) {
2279 // deleting bound buffer does implied bind to 0
2280 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002281 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00002282 }
2283}
2284
2285void GrGpuGL::notifyIndexBufferBind(const GrGLIndexBuffer* buffer) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002286 fHWGeometryState.fIndexBuffer = buffer;
reed@google.comac10a2d2010-12-22 21:39:39 +00002287}
2288
2289void GrGpuGL::notifyIndexBufferDelete(const GrGLIndexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002290 if (fHWGeometryState.fIndexBuffer == buffer) {
2291 // deleting bound buffer does implied bind to 0
2292 fHWGeometryState.fIndexBuffer = NULL;
2293 }
2294}
2295
reed@google.comac10a2d2010-12-22 21:39:39 +00002296void GrGpuGL::notifyRenderTargetDelete(GrRenderTarget* renderTarget) {
2297 GrAssert(NULL != renderTarget);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002298 GrDrawState* drawState = this->drawState();
2299 if (drawState->getRenderTarget() == renderTarget) {
2300 drawState->setRenderTarget(NULL);
reed@google.comac10a2d2010-12-22 21:39:39 +00002301 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002302 if (fHWDrawState.getRenderTarget() == renderTarget) {
2303 fHWDrawState.setRenderTarget(NULL);
reed@google.comac10a2d2010-12-22 21:39:39 +00002304 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002305}
2306
2307void GrGpuGL::notifyTextureDelete(GrGLTexture* texture) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002308 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002309 GrDrawState* drawState = this->drawState();
2310 if (drawState->getTexture(s) == texture) {
2311 fCurrDrawState.setTexture(s, NULL);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002312 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002313 if (fHWDrawState.getTexture(s) == texture) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002314 // deleting bound texture does implied bind to 0
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002315 fHWDrawState.setTexture(s, NULL);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002316 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002317 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002318}
2319
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002320bool GrGpuGL::configToGLFormats(GrPixelConfig config,
2321 bool getSizedInternalFormat,
2322 GrGLenum* internalFormat,
2323 GrGLenum* externalFormat,
2324 GrGLenum* externalType) {
2325 GrGLenum dontCare;
2326 if (NULL == internalFormat) {
2327 internalFormat = &dontCare;
2328 }
2329 if (NULL == externalFormat) {
2330 externalFormat = &dontCare;
2331 }
2332 if (NULL == externalType) {
2333 externalType = &dontCare;
2334 }
2335
reed@google.comac10a2d2010-12-22 21:39:39 +00002336 switch (config) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002337 case kRGBA_8888_PM_GrPixelConfig:
2338 case kRGBA_8888_UPM_GrPixelConfig:
bsalomon@google.comc4364992011-11-07 15:54:49 +00002339 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002340 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002341 if (getSizedInternalFormat) {
2342 *internalFormat = GR_GL_RGBA8;
2343 } else {
2344 *internalFormat = GR_GL_RGBA;
2345 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002346 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.comc4364992011-11-07 15:54:49 +00002347 break;
2348 case kBGRA_8888_PM_GrPixelConfig:
2349 case kBGRA_8888_UPM_GrPixelConfig:
bsalomon@google.com7107fa72011-11-10 14:54:14 +00002350 if (!fGLCaps.fBGRAFormatSupport) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002351 return false;
2352 }
bsalomon@google.com7107fa72011-11-10 14:54:14 +00002353 if (fGLCaps.fBGRAIsInternalFormat) {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002354 if (getSizedInternalFormat) {
2355 *internalFormat = GR_GL_BGRA8;
2356 } else {
2357 *internalFormat = GR_GL_BGRA;
2358 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002359 } else {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002360 if (getSizedInternalFormat) {
2361 *internalFormat = GR_GL_RGBA8;
2362 } else {
2363 *internalFormat = GR_GL_RGBA;
2364 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002365 }
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002366 *externalFormat = GR_GL_BGRA;
bsalomon@google.com6f379512011-11-16 20:36:03 +00002367 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002368 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002369 case kRGB_565_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002370 *internalFormat = GR_GL_RGB;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002371 *externalFormat = GR_GL_RGB;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002372 if (getSizedInternalFormat) {
2373 if (this->glBinding() == kDesktop_GrGLBinding) {
2374 return false;
2375 } else {
2376 *internalFormat = GR_GL_RGB565;
2377 }
2378 } else {
2379 *internalFormat = GR_GL_RGB;
2380 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002381 *externalType = GR_GL_UNSIGNED_SHORT_5_6_5;
reed@google.comac10a2d2010-12-22 21:39:39 +00002382 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002383 case kRGBA_4444_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002384 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002385 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002386 if (getSizedInternalFormat) {
2387 *internalFormat = GR_GL_RGBA4;
2388 } else {
2389 *internalFormat = GR_GL_RGBA;
2390 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002391 *externalType = GR_GL_UNSIGNED_SHORT_4_4_4_4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002392 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002393 case kIndex_8_GrPixelConfig:
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002394 if (this->getCaps().f8BitPaletteSupport) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002395 *internalFormat = GR_GL_PALETTE8_RGBA8;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002396 // glCompressedTexImage doesn't take external params
2397 *externalFormat = GR_GL_PALETTE8_RGBA8;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002398 // no sized/unsized internal format distinction here
2399 *internalFormat = GR_GL_PALETTE8_RGBA8;
2400 // unused with CompressedTexImage
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002401 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002402 } else {
2403 return false;
2404 }
2405 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002406 case kAlpha_8_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002407 *internalFormat = GR_GL_ALPHA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002408 *externalFormat = GR_GL_ALPHA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002409 if (getSizedInternalFormat) {
2410 *internalFormat = GR_GL_ALPHA8;
2411 } else {
2412 *internalFormat = GR_GL_ALPHA;
2413 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002414 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002415 break;
2416 default:
2417 return false;
2418 }
2419 return true;
2420}
2421
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002422void GrGpuGL::setTextureUnit(int unit) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002423 GrAssert(unit >= 0 && unit < GrDrawState::kNumStages);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002424 if (fActiveTextureUnitIdx != unit) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002425 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002426 fActiveTextureUnitIdx = unit;
2427 }
2428}
bsalomon@google.com316f99232011-01-13 21:28:12 +00002429
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002430void GrGpuGL::setSpareTextureUnit() {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002431 if (fActiveTextureUnitIdx != (GR_GL_TEXTURE0 + SPARE_TEX_UNIT)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002432 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + SPARE_TEX_UNIT));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002433 fActiveTextureUnitIdx = SPARE_TEX_UNIT;
2434 }
2435}
2436
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00002437void GrGpuGL::resetDirtyFlags() {
2438 Gr_bzero(&fDirtyFlags, sizeof(fDirtyFlags));
2439}
2440
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002441void GrGpuGL::setBuffers(bool indexed,
2442 int* extraVertexOffset,
2443 int* extraIndexOffset) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002444
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002445 GrAssert(NULL != extraVertexOffset);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002446
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002447 const GeometryPoolState& geoPoolState = this->getGeomPoolState();
2448
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002449 GrGLVertexBuffer* vbuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002450 switch (this->getGeomSrc().fVertexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002451 case kBuffer_GeometrySrcType:
2452 *extraVertexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002453 vbuf = (GrGLVertexBuffer*) this->getGeomSrc().fVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002454 break;
2455 case kArray_GeometrySrcType:
2456 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002457 this->finalizeReservedVertices();
2458 *extraVertexOffset = geoPoolState.fPoolStartVertex;
2459 vbuf = (GrGLVertexBuffer*) geoPoolState.fPoolVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002460 break;
2461 default:
2462 vbuf = NULL; // suppress warning
2463 GrCrash("Unknown geometry src type!");
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002464 }
2465
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002466 GrAssert(NULL != vbuf);
2467 GrAssert(!vbuf->isLocked());
2468 if (fHWGeometryState.fVertexBuffer != vbuf) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002469 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, vbuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002470 fHWGeometryState.fArrayPtrsDirty = true;
2471 fHWGeometryState.fVertexBuffer = vbuf;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002472 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002473
2474 if (indexed) {
2475 GrAssert(NULL != extraIndexOffset);
2476
2477 GrGLIndexBuffer* ibuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002478 switch (this->getGeomSrc().fIndexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002479 case kBuffer_GeometrySrcType:
2480 *extraIndexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002481 ibuf = (GrGLIndexBuffer*)this->getGeomSrc().fIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002482 break;
2483 case kArray_GeometrySrcType:
2484 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002485 this->finalizeReservedIndices();
2486 *extraIndexOffset = geoPoolState.fPoolStartIndex;
2487 ibuf = (GrGLIndexBuffer*) geoPoolState.fPoolIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002488 break;
2489 default:
2490 ibuf = NULL; // suppress warning
2491 GrCrash("Unknown geometry src type!");
2492 }
2493
2494 GrAssert(NULL != ibuf);
2495 GrAssert(!ibuf->isLocked());
2496 if (fHWGeometryState.fIndexBuffer != ibuf) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002497 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, ibuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002498 fHWGeometryState.fIndexBuffer = ibuf;
2499 }
2500 }
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002501}
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +00002502
2503int GrGpuGL::getMaxEdges() const {
2504 // FIXME: This is a pessimistic estimate based on how many other things
2505 // want to add uniforms. This should be centralized somewhere.
tomhudson@google.com93813632011-10-27 20:21:16 +00002506 return GR_CT_MIN(fGLCaps.fMaxFragmentUniformVectors - 8,
2507 GrDrawState::kMaxEdges);
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +00002508}
bsalomon@google.comfe676522011-06-17 18:12:21 +00002509
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002510void GrGpuGL::GLCaps::print() const {
2511 for (int i = 0; i < fStencilFormats.count(); ++i) {
2512 GrPrintf("Stencil Format %d, stencil bits: %02d, total bits: %02d\n",
2513 i,
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00002514 fStencilFormats[i].fFormat.fStencilBits,
2515 fStencilFormats[i].fFormat.fTotalBits);
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002516 }
2517
2518 GR_STATIC_ASSERT(0 == kNone_MSFBO);
2519 GR_STATIC_ASSERT(1 == kDesktopARB_MSFBO);
2520 GR_STATIC_ASSERT(2 == kDesktopEXT_MSFBO);
2521 GR_STATIC_ASSERT(3 == kAppleES_MSFBO);
2522 static const char* gMSFBOExtStr[] = {
2523 "None",
2524 "ARB",
2525 "EXT",
2526 "Apple",
2527 };
2528 GrPrintf("MSAA Type: %s\n", gMSFBOExtStr[fMSFBOType]);
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002529 GrPrintf("Max FS Uniform Vectors: %d\n", fMaxFragmentUniformVectors);
2530 GrPrintf("Support RGBA8 Render Buffer: %s\n",
bsalomon@google.com7107fa72011-11-10 14:54:14 +00002531 (fRGBA8RenderbufferSupport ? "YES": "NO"));
bsalomon@google.comc4364992011-11-07 15:54:49 +00002532 GrPrintf("BGRA is an internal format: %s\n",
bsalomon@google.com7107fa72011-11-10 14:54:14 +00002533 (fBGRAIsInternalFormat ? "YES": "NO"));
bsalomon@google.com85b505b2011-11-07 14:56:51 +00002534 GrPrintf("Support texture swizzle: %s\n",
bsalomon@google.com7107fa72011-11-10 14:54:14 +00002535 (fTextureSwizzleSupport ? "YES": "NO"));
2536 GrPrintf("Unpack Row length support: %s\n",
2537 (fUnpackRowLengthSupport ? "YES": "NO"));
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +00002538 GrPrintf("Unpack Flip Y support: %s\n",
2539 (fUnpackFlipYSupport ? "YES": "NO"));
bsalomon@google.com7107fa72011-11-10 14:54:14 +00002540 GrPrintf("Pack Row length support: %s\n",
2541 (fPackRowLengthSupport ? "YES": "NO"));
bsalomon@google.com56d11e02011-11-30 19:59:08 +00002542 GrPrintf("Pack Flip Y support: %s\n",
2543 (fPackFlipYSupport ? "YES": "NO"));
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002544}