blob: eb1de8abac984f1fa36002a8e1ec51addef6a3e0 [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.com18c9c192011-09-22 21:01:31 +0000249GrGpuGL::GrGpuGL(const GrGLInterface* gl, GrGLBinding glBinding) {
tomhudson@google.com747bf292011-06-14 18:16:52 +0000250
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000251 fPrintedCaps = false;
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000252
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000253 gl->ref();
254 fGL = gl;
255 fGLBinding = glBinding;
256 switch (glBinding) {
257 case kDesktop_GrGLBinding:
258 GrAssert(gl->supportsDesktop());
259 break;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000260 case kES2_GrGLBinding:
261 GrAssert(gl->supportsES2());
262 break;
263 default:
264 GrCrash("Expect exactly one valid GL binding bit to be in use.");
reed@google.comeeeb5a02010-12-23 15:12:59 +0000265 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000266
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000267 GrGLClearErr(fGL);
268
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000269 const GrGLubyte* ext;
270 GL_CALL_RET(ext, GetString(GR_GL_EXTENSIONS));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000271 if (gPrintStartupSpew) {
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000272 const GrGLubyte* vendor;
273 const GrGLubyte* renderer;
274 const GrGLubyte* version;
275 GL_CALL_RET(vendor, GetString(GR_GL_VENDOR));
276 GL_CALL_RET(renderer, GetString(GR_GL_RENDERER));
277 GL_CALL_RET(version, GetString(GR_GL_VERSION));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000278 GrPrintf("------------------------- create GrGpuGL %p --------------\n",
279 this);
280 GrPrintf("------ VENDOR %s\n", vendor);
281 GrPrintf("------ RENDERER %s\n", renderer);
282 GrPrintf("------ VERSION %s\n", version);
283 GrPrintf("------ EXTENSIONS\n %s \n", ext);
284 }
285
bsalomon@google.comc82b8892011-09-22 14:10:33 +0000286 fGLVersion = GrGLGetVersion(gl);
287 GrAssert(0 != fGLVersion);
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000288 fExtensionString = (const char*) ext;
reed@google.comac10a2d2010-12-22 21:39:39 +0000289
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000290 this->resetDirtyFlags();
bsalomon@google.com316f99232011-01-13 21:28:12 +0000291
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000292 this->initCaps();
tomhudson@google.com747bf292011-06-14 18:16:52 +0000293
bsalomon@google.comfe676522011-06-17 18:12:21 +0000294 fLastSuccessfulStencilFmtIdx = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000295}
296
297GrGpuGL::~GrGpuGL() {
bsalomon@google.com4a018bb2011-10-28 19:50:21 +0000298 // This must be called by before the GrDrawTarget destructor
299 this->releaseGeometry();
bsalomon@google.com15b11df2011-09-16 21:18:29 +0000300 // This subclass must do this before the base class destructor runs
301 // since we will unref the GrGLInterface.
302 this->releaseResources();
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000303 fGL->unref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000304}
305
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000306///////////////////////////////////////////////////////////////////////////////
307
308static const GrGLuint kUnknownBitCount = ~0;
309
310void GrGpuGL::initCaps() {
311 GrGLint maxTextureUnits;
312 // check FS and fixed-function texture unit limits
313 // we only use textures in the fragment stage currently.
314 // checks are > to make sure we have a spare unit.
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000315 GR_GL_GetIntegerv(fGL, GR_GL_MAX_TEXTURE_IMAGE_UNITS, &maxTextureUnits);
316 GrAssert(maxTextureUnits > GrDrawState::kNumStages);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000317 if (kES2_GrGLBinding != this->glBinding()) {
318 GR_GL_GetIntegerv(fGL, GR_GL_MAX_TEXTURE_UNITS, &maxTextureUnits);
tomhudson@google.com93813632011-10-27 20:21:16 +0000319 GrAssert(maxTextureUnits > GrDrawState::kNumStages);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000320 }
321 if (kES2_GrGLBinding == this->glBinding()) {
322 GR_GL_GetIntegerv(fGL, GR_GL_MAX_FRAGMENT_UNIFORM_VECTORS,
323 &fGLCaps.fMaxFragmentUniformVectors);
324 } else if (kDesktop_GrGLBinding != this->glBinding()) {
325 GrGLint max;
326 GR_GL_GetIntegerv(fGL, GR_GL_MAX_FRAGMENT_UNIFORM_COMPONENTS, &max);
327 fGLCaps.fMaxFragmentUniformVectors = max / 4;
328 } else {
329 fGLCaps.fMaxFragmentUniformVectors = 16;
330 }
331
332 GrGLint numFormats;
333 GR_GL_GetIntegerv(fGL, GR_GL_NUM_COMPRESSED_TEXTURE_FORMATS, &numFormats);
334 SkAutoSTMalloc<10, GrGLint> formats(numFormats);
335 GR_GL_GetIntegerv(fGL, GR_GL_COMPRESSED_TEXTURE_FORMATS, formats);
336 for (int i = 0; i < numFormats; ++i) {
337 if (formats[i] == GR_GL_PALETTE8_RGBA8) {
338 fCaps.f8BitPaletteSupport = true;
339 break;
340 }
341 }
342
343 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000344 // we could also look for GL_ATI_separate_stencil extension or
345 // GL_EXT_stencil_two_side but they use different function signatures
346 // than GL2.0+ (and than each other).
347 fCaps.fTwoSidedStencilSupport = (fGLVersion >= GR_GL_VER(2,0));
348 // supported on GL 1.4 and higher or by extension
349 fCaps.fStencilWrapOpsSupport = (fGLVersion >= GR_GL_VER(1,4)) ||
350 this->hasExtension("GL_EXT_stencil_wrap");
351 } else {
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000352 // ES 2 has two sided stencil and stencil wrap
353 fCaps.fTwoSidedStencilSupport = true;
354 fCaps.fStencilWrapOpsSupport = true;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000355 }
356
357 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000358 fGLCaps.fRGBA8RenderbufferSupport = true;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000359 } else {
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000360 fGLCaps.fRGBA8RenderbufferSupport =
361 this->hasExtension("GL_OES_rgb8_rgba8") ||
362 this->hasExtension("GL_ARM_rgba8");
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000363 }
364
365
bsalomon@google.comc4364992011-11-07 15:54:49 +0000366 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000367 fGLCaps.fBGRAFormatSupport = this->glVersion() >= GR_GL_VER(1,2) ||
368 this->hasExtension("GL_EXT_bgra");
bsalomon@google.comc4364992011-11-07 15:54:49 +0000369 } else {
370 bool hasBGRAExt = false;
371 if (this->hasExtension("GL_APPLE_texture_format_BGRA8888")) {
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000372 fGLCaps.fBGRAFormatSupport = true;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000373 } else if (this->hasExtension("GL_EXT_texture_format_BGRA8888")) {
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000374 fGLCaps.fBGRAFormatSupport = true;
375 fGLCaps.fBGRAIsInternalFormat = true;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000376 }
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000377 GrAssert(fGLCaps.fBGRAFormatSupport ||
bsalomon@google.comc4364992011-11-07 15:54:49 +0000378 kSkia8888_PM_GrPixelConfig != kBGRA_8888_PM_GrPixelConfig);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000379 }
380
381 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000382 fGLCaps.fTextureSwizzleSupport = this->glVersion() >= GR_GL_VER(3,3) ||
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000383 this->hasExtension("GL_ARB_texture_swizzle");
384 } else {
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000385 fGLCaps.fTextureSwizzleSupport = false;
386 }
387
388 if (kDesktop_GrGLBinding == this->glBinding()) {
389 fGLCaps.fUnpackRowLengthSupport = true;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000390 fGLCaps.fUnpackFlipYSupport = false;
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000391 fGLCaps.fPackRowLengthSupport = true;
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000392 fGLCaps.fPackFlipYSupport = false;
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000393 } else {
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000394 fGLCaps.fUnpackRowLengthSupport =this->hasExtension("GL_EXT_unpack_subimage");
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000395 fGLCaps.fUnpackFlipYSupport = this->hasExtension("GL_CHROMIUM_flipy");
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000396 // no extension for pack row length
397 fGLCaps.fPackRowLengthSupport = false;
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000398 fGLCaps.fPackFlipYSupport =
399 this->hasExtension("GL_ANGLE_pack_reverse_row_order");
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000400 }
401
402 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000403 fCaps.fBufferLockSupport = true; // we require VBO support and the desktop VBO
404 // extension includes glMapBuffer.
405 } else {
406 fCaps.fBufferLockSupport = this->hasExtension("GL_OES_mapbuffer");
407 }
408
409 if (kDesktop_GrGLBinding == this->glBinding()) {
410 if (fGLVersion >= GR_GL_VER(2,0) ||
411 this->hasExtension("GL_ARB_texture_non_power_of_two")) {
412 fCaps.fNPOTTextureTileSupport = true;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000413 } else {
414 fCaps.fNPOTTextureTileSupport = false;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000415 }
416 } else {
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000417 // Unextended ES2 supports NPOT textures with clamp_to_edge and non-mip filters only
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000418 fCaps.fNPOTTextureTileSupport = this->hasExtension("GL_OES_texture_npot");
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000419 }
420
bsalomon@google.com07dd2bf2011-12-09 19:40:36 +0000421 fGLCaps.fTextureUsageSupport = (kES2_GrGLBinding == this->glBinding()) &&
422 this->hasExtension("GL_ANGLE_texture_usage");
423
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000424 // Tex storage is in desktop 4.2 and can be an extension to desktop or ES.
425 fGLCaps.fTexStorageSupport = (kDesktop_GrGLBinding == this->glBinding() &&
426 fGLVersion >= GR_GL_VER(4,2)) ||
427 this->hasExtension("GL_ARB_texture_storage") ||
428 this->hasExtension("GL_EXT_texture_storage");
429
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000430 fCaps.fHWAALineSupport = (kDesktop_GrGLBinding == this->glBinding());
431
432 ////////////////////////////////////////////////////////////////////////////
433 // Experiments to determine limitations that can't be queried.
434 // TODO: Make these a preprocess that generate some compile time constants.
435 // TODO: probe once at startup, rather than once per context creation.
436
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000437 GR_GL_GetIntegerv(fGL, GR_GL_MAX_TEXTURE_SIZE, &fCaps.fMaxTextureSize);
438 GR_GL_GetIntegerv(fGL, GR_GL_MAX_RENDERBUFFER_SIZE, &fCaps.fMaxRenderTargetSize);
439 // Our render targets are always created with textures as the color
440 // attachment, hence this min:
441 fCaps.fMaxRenderTargetSize = GrMin(fCaps.fMaxTextureSize, fCaps.fMaxRenderTargetSize);
442
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000443 this->initFSAASupport();
444 this->initStencilFormats();
445}
446
447void GrGpuGL::initFSAASupport() {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000448
449 fGLCaps.fMSFBOType = GLCaps::kNone_MSFBO;
450 if (kDesktop_GrGLBinding != this->glBinding()) {
451 if (this->hasExtension("GL_CHROMIUM_framebuffer_multisample")) {
452 // chrome's extension is equivalent to the EXT msaa
453 // and fbo_blit extensions.
454 fGLCaps.fMSFBOType = GLCaps::kDesktopEXT_MSFBO;
455 } else if (this->hasExtension("GL_APPLE_framebuffer_multisample")) {
456 fGLCaps.fMSFBOType = GLCaps::kAppleES_MSFBO;
457 }
458 } else {
459 if ((fGLVersion >= GR_GL_VER(3,0)) || this->hasExtension("GL_ARB_framebuffer_object")) {
460 fGLCaps.fMSFBOType = GLCaps::kDesktopARB_MSFBO;
461 } else if (this->hasExtension("GL_EXT_framebuffer_multisample") &&
462 this->hasExtension("GL_EXT_framebuffer_blit")) {
463 fGLCaps.fMSFBOType = GLCaps::kDesktopEXT_MSFBO;
464 }
465 }
466
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000467 fCaps.fFSAASupport = GLCaps::kNone_MSFBO != fGLCaps.fMSFBOType;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000468}
469
470void GrGpuGL::initStencilFormats() {
471
472 // Build up list of legal stencil formats (though perhaps not supported on
473 // the particular gpu/driver) from most preferred to least.
474
475 // these consts are in order of most preferred to least preferred
476 // we don't bother with GL_STENCIL_INDEX1 or GL_DEPTH32F_STENCIL8
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000477
478 // Omitting fVerifiedColorConfigs from initializer list should init to 0.
479 static const GLCaps::StencilFormat
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000480 // internal Format stencil bits total bits packed?
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000481 gS8 = {{GR_GL_STENCIL_INDEX8, 8, 8, false}, {0U}},
482 gS16 = {{GR_GL_STENCIL_INDEX16, 16, 16, false}, {0U}},
483 gD24S8 = {{GR_GL_DEPTH24_STENCIL8, 8, 32, true }, {0U}},
484 gS4 = {{GR_GL_STENCIL_INDEX4, 4, 4, false}, {0U}},
485 gS = {{GR_GL_STENCIL_INDEX, kUnknownBitCount, kUnknownBitCount, false}, {0U}},
486 gDS = {{GR_GL_DEPTH_STENCIL, kUnknownBitCount, kUnknownBitCount, true }, {0U}};
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000487
488 if (kDesktop_GrGLBinding == this->glBinding()) {
489 bool supportsPackedDS = fGLVersion >= GR_GL_VER(3,0) ||
490 this->hasExtension("GL_EXT_packed_depth_stencil") ||
491 this->hasExtension("GL_ARB_framebuffer_object");
492
493 // S1 thru S16 formats are in GL 3.0+, EXT_FBO, and ARB_FBO since we
494 // require FBO support we can expect these are legal formats and don't
495 // check. These also all support the unsized GL_STENCIL_INDEX.
496 fGLCaps.fStencilFormats.push_back() = gS8;
497 fGLCaps.fStencilFormats.push_back() = gS16;
498 if (supportsPackedDS) {
499 fGLCaps.fStencilFormats.push_back() = gD24S8;
500 }
501 fGLCaps.fStencilFormats.push_back() = gS4;
502 if (supportsPackedDS) {
503 fGLCaps.fStencilFormats.push_back() = gDS;
504 }
505 } else {
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000506 // ES2 has STENCIL_INDEX8 without extensions but requires extensions
507 // for other formats.
508 // ES doesn't support using the unsized format.
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000509
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000510 fGLCaps.fStencilFormats.push_back() = gS8;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000511 //fStencilFormats.push_back() = gS16;
512 if (this->hasExtension("GL_OES_packed_depth_stencil")) {
513 fGLCaps.fStencilFormats.push_back() = gD24S8;
514 }
515 if (this->hasExtension("GL_OES_stencil4")) {
516 fGLCaps.fStencilFormats.push_back() = gS4;
517 }
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000518 }
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000519#if GR_DEBUG
520 // ensure that initially all color / stencil format combos have unverified
521 // fbo status.
522 for (int i = 0; i < fGLCaps.fStencilFormats.count(); ++i) {
523 int numU32 =
524 GR_ARRAY_COUNT(fGLCaps.fStencilFormats[i].fVerifiedColorConfigs);
525 for (int j = 0; j < numU32; ++j) {
526 GrAssert(0 == fGLCaps.fStencilFormats[i].fVerifiedColorConfigs[j]);
527 }
528 }
529#endif
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000530}
531
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000532GrPixelConfig GrGpuGL::preferredReadPixelsConfig(GrPixelConfig config) const {
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000533 if (GR_GL_RGBA_8888_PIXEL_OPS_SLOW && GrPixelConfigIsRGBA8888(config)) {
534 return GrPixelConfigSwapRAndB(config);
535 } else {
536 return config;
537 }
538}
539
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000540GrPixelConfig GrGpuGL::preferredWritePixelsConfig(GrPixelConfig config) const {
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000541 if (GR_GL_RGBA_8888_PIXEL_OPS_SLOW && GrPixelConfigIsRGBA8888(config)) {
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000542 return GrPixelConfigSwapRAndB(config);
543 } else {
544 return config;
545 }
546}
547
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000548bool GrGpuGL::fullReadPixelsIsFasterThanPartial() const {
549 return SkToBool(GR_GL_FULL_READPIXELS_FASTER_THAN_PARTIAL);
550}
551
bsalomon@google.com1bf1c212011-11-05 12:18:58 +0000552void GrGpuGL::onResetContext() {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000553 if (gPrintStartupSpew && !fPrintedCaps) {
554 fPrintedCaps = true;
555 this->getCaps().print();
556 fGLCaps.print();
557 }
558
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000559 // We detect cases when blending is effectively off
reed@google.comac10a2d2010-12-22 21:39:39 +0000560 fHWBlendDisabled = false;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000561 GL_CALL(Enable(GR_GL_BLEND));
reed@google.comac10a2d2010-12-22 21:39:39 +0000562
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000563 // we don't use the zb at all
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000564 GL_CALL(Disable(GR_GL_DEPTH_TEST));
565 GL_CALL(DepthMask(GR_GL_FALSE));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000566
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000567 GL_CALL(Disable(GR_GL_CULL_FACE));
568 GL_CALL(FrontFace(GR_GL_CCW));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000569 fHWDrawState.setDrawFace(GrDrawState::kBoth_DrawFace);
reed@google.comac10a2d2010-12-22 21:39:39 +0000570
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000571 GL_CALL(Disable(GR_GL_DITHER));
572 if (kDesktop_GrGLBinding == this->glBinding()) {
573 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
574 GL_CALL(Disable(GR_GL_POINT_SMOOTH));
575 GL_CALL(Disable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +0000576 fHWAAState.fMSAAEnabled = false;
577 fHWAAState.fSmoothLineEnabled = false;
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000578 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000579
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000580 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000581 fHWDrawState.resetStateFlags();
bsalomon@google.comd302f142011-03-03 13:54:13 +0000582
reed@google.comac10a2d2010-12-22 21:39:39 +0000583 // we only ever use lines in hairline mode
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000584 GL_CALL(LineWidth(1));
reed@google.comac10a2d2010-12-22 21:39:39 +0000585
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000586 // invalid
587 fActiveTextureUnitIdx = -1;
reed@google.comac10a2d2010-12-22 21:39:39 +0000588
reed@google.comac10a2d2010-12-22 21:39:39 +0000589 // illegal values
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000590 fHWDrawState.setBlendFunc((GrBlendCoeff)0xFF, (GrBlendCoeff)0xFF);
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000591
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000592 fHWDrawState.setBlendConstant(0x00000000);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000593 GL_CALL(BlendColor(0,0,0,0));
bsalomon@google.com080773c2011-03-15 19:09:25 +0000594
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000595 fHWDrawState.setColor(GrColor_ILLEGAL);
bsalomon@google.com316f99232011-01-13 21:28:12 +0000596
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000597 fHWDrawState.setViewMatrix(GrMatrix::InvalidMatrix());
bsalomon@google.com316f99232011-01-13 21:28:12 +0000598
tomhudson@google.com93813632011-10-27 20:21:16 +0000599 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000600 fHWDrawState.setTexture(s, NULL);
601 fHWDrawState.sampler(s)->setRadial2Params(-GR_ScalarMax,
602 -GR_ScalarMax,
603 true);
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000604 *fHWDrawState.sampler(s)->matrix() = GrMatrix::InvalidMatrix();
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000605 fHWDrawState.sampler(s)->setConvolutionParams(0, NULL, NULL);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000606 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000607
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000608 fHWBounds.fScissorRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000609 fHWBounds.fScissorEnabled = false;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000610 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000611 fHWBounds.fViewportRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000612
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000613 fHWDrawState.stencil()->invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000614 fHWStencilClip = false;
bsalomon@google.comdea2f8d2011-08-01 15:51:05 +0000615 fClipInStencil = false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000616
617 fHWGeometryState.fIndexBuffer = NULL;
618 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000619
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +0000620 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000621
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000622 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000623 fHWDrawState.setRenderTarget(NULL);
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000624
625 // we assume these values
626 if (this->glCaps().fUnpackRowLengthSupport) {
627 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
628 }
629 if (this->glCaps().fPackRowLengthSupport) {
630 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
631 }
632 if (this->glCaps().fUnpackFlipYSupport) {
633 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
634 }
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000635 if (this->glCaps().fPackFlipYSupport) {
636 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, GR_GL_FALSE));
637 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000638}
639
bsalomon@google.come269f212011-11-07 13:29:52 +0000640GrTexture* GrGpuGL::onCreatePlatformTexture(const GrPlatformTextureDesc& desc) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000641 GrGLTexture::Desc glTexDesc;
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000642 if (!configToGLFormats(desc.fConfig, false, NULL, NULL, NULL)) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000643 return NULL;
644 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000645
bsalomon@google.com99621082011-11-15 16:47:16 +0000646 glTexDesc.fWidth = desc.fWidth;
647 glTexDesc.fHeight = desc.fHeight;
bsalomon@google.come269f212011-11-07 13:29:52 +0000648 glTexDesc.fConfig = desc.fConfig;
649 glTexDesc.fTextureID = static_cast<GrGLuint>(desc.fTextureHandle);
650 glTexDesc.fOwnsID = false;
651 glTexDesc.fOrientation = GrGLTexture::kBottomUp_Orientation;
652
653 GrGLTexture* texture = NULL;
654 if (desc.fFlags & kRenderTarget_GrPlatformTextureFlag) {
655 GrGLRenderTarget::Desc glRTDesc;
656 glRTDesc.fRTFBOID = 0;
657 glRTDesc.fTexFBOID = 0;
658 glRTDesc.fMSColorRenderbufferID = 0;
659 glRTDesc.fOwnIDs = true;
660 glRTDesc.fConfig = desc.fConfig;
661 glRTDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.com99621082011-11-15 16:47:16 +0000662 if (!this->createRenderTargetObjects(glTexDesc.fWidth,
663 glTexDesc.fHeight,
bsalomon@google.come269f212011-11-07 13:29:52 +0000664 glTexDesc.fTextureID,
665 &glRTDesc)) {
666 return NULL;
667 }
668 texture = new GrGLTexture(this, glTexDesc, glRTDesc);
669 } else {
670 texture = new GrGLTexture(this, glTexDesc);
671 }
672 if (NULL == texture) {
673 return NULL;
674 }
675
676 this->setSpareTextureUnit();
677 return texture;
678}
679
680GrRenderTarget* GrGpuGL::onCreatePlatformRenderTarget(const GrPlatformRenderTargetDesc& desc) {
681 GrGLRenderTarget::Desc glDesc;
682 glDesc.fConfig = desc.fConfig;
683 glDesc.fRTFBOID = static_cast<GrGLuint>(desc.fRenderTargetHandle);
684 glDesc.fMSColorRenderbufferID = 0;
685 glDesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
686 glDesc.fSampleCnt = desc.fSampleCnt;
687 glDesc.fOwnIDs = false;
688 GrGLIRect viewport;
689 viewport.fLeft = 0;
690 viewport.fBottom = 0;
691 viewport.fWidth = desc.fWidth;
692 viewport.fHeight = desc.fHeight;
693
694 GrRenderTarget* tgt = new GrGLRenderTarget(this, glDesc, viewport);
695 if (desc.fStencilBits) {
696 GrGLStencilBuffer::Format format;
697 format.fInternalFormat = GrGLStencilBuffer::kUnknownInternalFormat;
698 format.fPacked = false;
699 format.fStencilBits = desc.fStencilBits;
700 format.fTotalBits = desc.fStencilBits;
701 GrGLStencilBuffer* sb = new GrGLStencilBuffer(this,
702 0,
703 desc.fWidth,
704 desc.fHeight,
705 desc.fSampleCnt,
706 format);
707 tgt->setStencilBuffer(sb);
708 sb->unref();
709 }
710 return tgt;
711}
712
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000713////////////////////////////////////////////////////////////////////////////////
714
bsalomon@google.com6f379512011-11-16 20:36:03 +0000715void GrGpuGL::onWriteTexturePixels(GrTexture* texture,
716 int left, int top, int width, int height,
717 GrPixelConfig config, const void* buffer,
718 size_t rowBytes) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000719 if (NULL == buffer) {
720 return;
721 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000722 GrGLTexture* glTex = static_cast<GrGLTexture*>(texture);
723
bsalomon@google.com6f379512011-11-16 20:36:03 +0000724 this->setSpareTextureUnit();
725 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTex->textureID()));
726 GrGLTexture::Desc desc;
727 desc.fConfig = glTex->config();
728 desc.fWidth = glTex->width();
729 desc.fHeight = glTex->height();
730 desc.fOrientation = glTex->orientation();
731 desc.fTextureID = glTex->textureID();
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000732
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000733 this->uploadTexData(desc, false,
734 left, top, width, height,
735 config, buffer, rowBytes);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000736}
737
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000738namespace {
739bool adjust_pixel_ops_params(int surfaceWidth,
740 int surfaceHeight,
741 size_t bpp,
742 int* left, int* top, int* width, int* height,
743 const void** data,
744 size_t* rowBytes) {
745 if (!*rowBytes) {
746 *rowBytes = *width * bpp;
747 }
748
749 GrIRect subRect = GrIRect::MakeXYWH(*left, *top, *width, *height);
750 GrIRect bounds = GrIRect::MakeWH(surfaceWidth, surfaceHeight);
751
752 if (!subRect.intersect(bounds)) {
753 return false;
754 }
755 *data = reinterpret_cast<const void*>(reinterpret_cast<intptr_t>(*data) +
756 (subRect.fTop - *top) * *rowBytes + (subRect.fLeft - *left) * bpp);
757
758 *left = subRect.fLeft;
759 *top = subRect.fTop;
760 *width = subRect.width();
761 *height = subRect.height();
762 return true;
763}
764}
765
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000766bool GrGpuGL::uploadTexData(const GrGLTexture::Desc& desc,
767 bool isNewTexture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000768 int left, int top, int width, int height,
769 GrPixelConfig dataConfig,
770 const void* data,
771 size_t rowBytes) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000772 GrAssert(NULL != data || isNewTexture);
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000773
774 size_t bpp = GrBytesPerPixel(dataConfig);
775 if (!adjust_pixel_ops_params(desc.fWidth, desc.fHeight, bpp, &left, &top,
776 &width, &height, &data, &rowBytes)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000777 return false;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000778 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000779 size_t trimRowBytes = width * bpp;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000780
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000781 // in case we need a temporary, trimmed copy of the src pixels
782 SkAutoSMalloc<128 * 128> tempStorage;
783
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000784 bool useTexStorage = isNewTexture &&
785 this->glCaps().fTexStorageSupport;
786 if (useTexStorage) {
787 if (kDesktop_GrGLBinding == this->glBinding()) {
788 // 565 is not a sized internal format on desktop GL. So on desktop
789 // with 565 we always use an unsized internal format to let the
790 // system pick the best sized format to convert the 565 data to.
791 // Since glTexStorage only allows sized internal formats we will
792 // instead fallback to glTexImage2D.
793 useTexStorage = desc.fConfig != kRGB_565_GrPixelConfig;
794 } else {
795 // ES doesn't allow paletted textures to be used with tex storage
796 useTexStorage = desc.fConfig != kIndex_8_GrPixelConfig;
797 }
798 }
799
800 GrGLenum internalFormat;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000801 GrGLenum externalFormat;
802 GrGLenum externalType;
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000803 // glTexStorage requires sized internal formats on both desktop and ES. ES
804 // glTexImage requires an unsized format.
805 if (!this->configToGLFormats(dataConfig, useTexStorage, &internalFormat,
806 &externalFormat, &externalType)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000807 return false;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000808 }
809
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000810 if (!isNewTexture && GR_GL_PALETTE8_RGBA8 == internalFormat) {
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000811 // paletted textures cannot be updated
812 return false;
813 }
814
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000815 /*
bsalomon@google.com6f379512011-11-16 20:36:03 +0000816 * check whether to allocate a temporary buffer for flipping y or
817 * because our srcData has extra bytes past each row. If so, we need
818 * to trim those off here, since GL ES may not let us specify
819 * GL_UNPACK_ROW_LENGTH.
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000820 */
bsalomon@google.com6f379512011-11-16 20:36:03 +0000821 bool restoreGLRowLength = false;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000822 bool swFlipY = false;
823 bool glFlipY = false;
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000824 if (NULL != data) {
825 if (GrGLTexture::kBottomUp_Orientation == desc.fOrientation) {
826 if (this->glCaps().fUnpackFlipYSupport) {
827 glFlipY = true;
828 } else {
829 swFlipY = true;
830 }
831 }
832 if (this->glCaps().fUnpackRowLengthSupport && !swFlipY) {
833 // can't use this for flipping, only non-neg values allowed. :(
834 if (rowBytes != trimRowBytes) {
835 GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
836 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength));
837 restoreGLRowLength = true;
838 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000839 } else {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000840 if (trimRowBytes != rowBytes || swFlipY) {
841 // copy data into our new storage, skipping the trailing bytes
842 size_t trimSize = height * trimRowBytes;
843 const char* src = (const char*)data;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000844 if (swFlipY) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000845 src += (height - 1) * rowBytes;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000846 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000847 char* dst = (char*)tempStorage.reset(trimSize);
848 for (int y = 0; y < height; y++) {
849 memcpy(dst, src, trimRowBytes);
850 if (swFlipY) {
851 src -= rowBytes;
852 } else {
853 src += rowBytes;
854 }
855 dst += trimRowBytes;
856 }
857 // now point data to our copied version
858 data = tempStorage.get();
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000859 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000860 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000861 if (glFlipY) {
862 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_TRUE));
863 }
864 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, static_cast<GrGLint>(bpp)));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000865 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000866 bool succeeded = true;
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000867 if (isNewTexture &&
868 0 == left && 0 == top &&
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000869 desc.fWidth == width && desc.fHeight == height) {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000870 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000871 if (useTexStorage) {
872 // We never resize or change formats of textures. We don't use
873 // mipmaps currently.
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000874 GL_ALLOC_CALL(this->glInterface(),
875 TexStorage2D(GR_GL_TEXTURE_2D,
876 1, // levels
877 internalFormat,
878 desc.fWidth, desc.fHeight));
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000879 } else {
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000880 if (GR_GL_PALETTE8_RGBA8 == internalFormat) {
881 GrGLsizei imageSize = desc.fWidth * desc.fHeight +
882 kGrColorTableSize;
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000883 GL_ALLOC_CALL(this->glInterface(),
884 CompressedTexImage2D(GR_GL_TEXTURE_2D,
885 0, // level
886 internalFormat,
887 desc.fWidth, desc.fHeight,
888 0, // border
889 imageSize,
890 data));
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000891 } else {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000892 GL_ALLOC_CALL(this->glInterface(),
893 TexImage2D(GR_GL_TEXTURE_2D,
894 0, // level
895 internalFormat,
896 desc.fWidth, desc.fHeight,
897 0, // border
898 externalFormat, externalType,
899 data));
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000900 }
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000901 }
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000902 GrGLenum error = CHECK_ALLOC_ERROR(this->glInterface());
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000903 if (error != GR_GL_NO_ERROR) {
904 succeeded = false;
905 } else {
906 // if we have data and we used TexStorage to create the texture, we
907 // now upload with TexSubImage.
908 if (NULL != data && useTexStorage) {
909 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D,
910 0, // level
911 left, top,
912 width, height,
913 externalFormat, externalType,
914 data));
915 }
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000916 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000917 } else {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000918 if (swFlipY || glFlipY) {
bsalomon@google.com6f379512011-11-16 20:36:03 +0000919 top = desc.fHeight - (top + height);
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000920 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000921 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D,
922 0, // level
923 left, top,
924 width, height,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000925 externalFormat, externalType, data));
926 }
927
928 if (restoreGLRowLength) {
929 GrAssert(this->glCaps().fUnpackRowLengthSupport);
930 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000931 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000932 if (glFlipY) {
933 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
934 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000935 return succeeded;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000936}
937
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000938bool GrGpuGL::createRenderTargetObjects(int width, int height,
939 GrGLuint texID,
940 GrGLRenderTarget::Desc* desc) {
941 desc->fMSColorRenderbufferID = 0;
942 desc->fRTFBOID = 0;
943 desc->fTexFBOID = 0;
944 desc->fOwnIDs = true;
945
946 GrGLenum status;
947 GrGLint err;
948
bsalomon@google.comab15d612011-08-09 12:57:56 +0000949 GrGLenum msColorFormat = 0; // suppress warning
950
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000951 GL_CALL(GenFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000952 if (!desc->fTexFBOID) {
953 goto FAILED;
954 }
955
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000956
957 // If we are using multisampling we will create two FBOS. We render
958 // to one and then resolve to the texture bound to the other.
bsalomon@google.come269f212011-11-07 13:29:52 +0000959 if (desc->fSampleCnt > 0) {
960 if (GLCaps::kNone_MSFBO == fGLCaps.fMSFBOType) {
961 goto FAILED;
962 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000963 GL_CALL(GenFramebuffers(1, &desc->fRTFBOID));
964 GL_CALL(GenRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000965 if (!desc->fRTFBOID ||
966 !desc->fMSColorRenderbufferID ||
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000967 !this->configToGLFormats(desc->fConfig,
968 // GLES requires sized internal formats
969 kES2_GrGLBinding == this->glBinding(),
970 &msColorFormat, NULL, NULL)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000971 goto FAILED;
972 }
973 } else {
974 desc->fRTFBOID = desc->fTexFBOID;
975 }
976
bsalomon@google.com0e9b41a2012-01-04 22:11:43 +0000977 // below here we may bind the FBO
978 fHWDrawState.setRenderTarget(NULL);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000979 if (desc->fRTFBOID != desc->fTexFBOID) {
980 GrAssert(desc->fSampleCnt > 1);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000981 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000982 desc->fMSColorRenderbufferID));
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000983 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
984 GL_ALLOC_CALL(this->glInterface(),
985 RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
986 desc->fSampleCnt,
987 msColorFormat,
988 width, height));
989 err = CHECK_ALLOC_ERROR(this->glInterface());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000990 if (err != GR_GL_NO_ERROR) {
991 goto FAILED;
992 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000993 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fRTFBOID));
994 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000995 GR_GL_COLOR_ATTACHMENT0,
996 GR_GL_RENDERBUFFER,
997 desc->fMSColorRenderbufferID));
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000998 if (!fGLCaps.isConfigVerifiedColorAttachment(desc->fConfig)) {
999 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1000 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
1001 goto FAILED;
1002 }
1003 fGLCaps.markConfigAsValidColorAttachment(desc->fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001004 }
1005 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001006 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001007
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001008 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
1009 GR_GL_COLOR_ATTACHMENT0,
1010 GR_GL_TEXTURE_2D,
1011 texID, 0));
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001012 if (!fGLCaps.isConfigVerifiedColorAttachment(desc->fConfig)) {
1013 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1014 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
1015 goto FAILED;
1016 }
1017 fGLCaps.markConfigAsValidColorAttachment(desc->fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001018 }
1019
1020 return true;
1021
1022FAILED:
1023 if (desc->fMSColorRenderbufferID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001024 GL_CALL(DeleteRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001025 }
1026 if (desc->fRTFBOID != desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001027 GL_CALL(DeleteFramebuffers(1, &desc->fRTFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001028 }
1029 if (desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001030 GL_CALL(DeleteFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001031 }
1032 return false;
1033}
1034
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001035// good to set a break-point here to know when createTexture fails
1036static GrTexture* return_null_texture() {
1037// GrAssert(!"null texture");
1038 return NULL;
1039}
1040
1041#if GR_DEBUG
1042static size_t as_size_t(int x) {
1043 return x;
1044}
1045#endif
1046
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001047GrTexture* GrGpuGL::onCreateTexture(const GrTextureDesc& desc,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001048 const void* srcData,
1049 size_t rowBytes) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001050
1051#if GR_COLLECT_STATS
1052 ++fStats.fTextureCreateCnt;
1053#endif
reed@google.com1fcd51e2011-01-05 15:50:27 +00001054
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001055 GrGLTexture::Desc glTexDesc;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001056 GrGLRenderTarget::Desc glRTDesc;
reed@google.comac10a2d2010-12-22 21:39:39 +00001057
bsalomon@google.com78d6cf92012-01-30 18:09:31 +00001058 // Attempt to catch un- or wrongly initialized sample counts;
1059 GrAssert(desc.fSampleCnt >= 0 && desc.fSampleCnt <= 64);
1060
bsalomon@google.com99621082011-11-15 16:47:16 +00001061 glTexDesc.fWidth = desc.fWidth;
1062 glTexDesc.fHeight = desc.fHeight;
bsalomon@google.com78d6cf92012-01-30 18:09:31 +00001063 glTexDesc.fConfig = desc.fConfig;
1064 glTexDesc.fOwnsID = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001065
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001066 glRTDesc.fMSColorRenderbufferID = 0;
1067 glRTDesc.fRTFBOID = 0;
1068 glRTDesc.fTexFBOID = 0;
1069 glRTDesc.fOwnIDs = true;
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001070 glRTDesc.fConfig = glTexDesc.fConfig;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001071
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001072 bool renderTarget = 0 != (desc.fFlags & kRenderTarget_GrTextureFlagBit);
reed@google.comac10a2d2010-12-22 21:39:39 +00001073
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001074 const Caps& caps = this->getCaps();
1075
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001076 // We keep GrRenderTargets in GL's normal orientation so that they
1077 // can be drawn to by the outside world without the client having
1078 // to render upside down.
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001079 glTexDesc.fOrientation = renderTarget ? GrGLTexture::kBottomUp_Orientation :
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001080 GrGLTexture::kTopDown_Orientation;
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001081
bsalomon@google.com78d6cf92012-01-30 18:09:31 +00001082 glRTDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001083 if (GLCaps::kNone_MSFBO == fGLCaps.fMSFBOType &&
bsalomon@google.com78d6cf92012-01-30 18:09:31 +00001084 desc.fSampleCnt) {
1085 GrPrintf("MSAA RT requested but not supported on this platform.");
reed@google.comac10a2d2010-12-22 21:39:39 +00001086 }
1087
reed@google.comac10a2d2010-12-22 21:39:39 +00001088 if (renderTarget) {
bsalomon@google.com99621082011-11-15 16:47:16 +00001089 if (glTexDesc.fWidth > caps.fMaxRenderTargetSize ||
1090 glTexDesc.fHeight > caps.fMaxRenderTargetSize) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001091 return return_null_texture();
1092 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001093 }
1094
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001095 GL_CALL(GenTextures(1, &glTexDesc.fTextureID));
bsalomon@google.com07dd2bf2011-12-09 19:40:36 +00001096 if (renderTarget && this->glCaps().fTextureUsageSupport) {
1097 // provides a hint about how this texture will be used
1098 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1099 GR_GL_TEXTURE_USAGE,
1100 GR_GL_FRAMEBUFFER_ATTACHMENT));
1101 }
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001102 if (!glTexDesc.fTextureID) {
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001103 return return_null_texture();
1104 }
1105
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001106 this->setSpareTextureUnit();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001107 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTexDesc.fTextureID));
bsalomon@google.come269f212011-11-07 13:29:52 +00001108
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001109 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
1110 // drivers have a bug where an FBO won't be complete if it includes a
1111 // texture that is not mipmap complete (considering the filter in use).
1112 GrGLTexture::TexParams initialTexParams;
1113 // we only set a subset here so invalidate first
1114 initialTexParams.invalidate();
1115 initialTexParams.fFilter = GR_GL_NEAREST;
1116 initialTexParams.fWrapS = GR_GL_CLAMP_TO_EDGE;
1117 initialTexParams.fWrapT = GR_GL_CLAMP_TO_EDGE;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001118 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1119 GR_GL_TEXTURE_MAG_FILTER,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001120 initialTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001121 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1122 GR_GL_TEXTURE_MIN_FILTER,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001123 initialTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001124 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1125 GR_GL_TEXTURE_WRAP_S,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001126 initialTexParams.fWrapS));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001127 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1128 GR_GL_TEXTURE_WRAP_T,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001129 initialTexParams.fWrapT));
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001130 if (!this->uploadTexData(glTexDesc, true, 0, 0,
1131 glTexDesc.fWidth, glTexDesc.fHeight,
1132 desc.fConfig, srcData, rowBytes)) {
1133 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
1134 return return_null_texture();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001135 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001136
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001137 GrGLTexture* tex;
reed@google.comac10a2d2010-12-22 21:39:39 +00001138 if (renderTarget) {
1139#if GR_COLLECT_STATS
1140 ++fStats.fRenderTargetCreateCnt;
1141#endif
bsalomon@google.com99621082011-11-15 16:47:16 +00001142 if (!this->createRenderTargetObjects(glTexDesc.fWidth,
1143 glTexDesc.fHeight,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001144 glTexDesc.fTextureID,
1145 &glRTDesc)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001146 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001147 return return_null_texture();
1148 }
bsalomon@google.com80d09b92011-11-05 21:21:13 +00001149 tex = new GrGLTexture(this, glTexDesc, glRTDesc);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001150 } else {
bsalomon@google.com80d09b92011-11-05 21:21:13 +00001151 tex = new GrGLTexture(this, glTexDesc);
reed@google.comac10a2d2010-12-22 21:39:39 +00001152 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001153 tex->setCachedTexParams(initialTexParams, this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00001154#ifdef TRACE_TEXTURE_CREATION
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001155 GrPrintf("--- new texture [%d] size=(%d %d) config=%d\n",
1156 glTexDesc.fTextureID, desc.fWidth, desc.fHeight, desc.fConfig);
reed@google.comac10a2d2010-12-22 21:39:39 +00001157#endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001158 return tex;
1159}
1160
1161namespace {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001162void inline get_stencil_rb_sizes(const GrGLInterface* gl,
1163 GrGLuint rb,
1164 GrGLStencilBuffer::Format* format) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001165 // we shouldn't ever know one size and not the other
1166 GrAssert((kUnknownBitCount == format->fStencilBits) ==
1167 (kUnknownBitCount == format->fTotalBits));
1168 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001169 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001170 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1171 (GrGLint*)&format->fStencilBits);
1172 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001173 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001174 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1175 (GrGLint*)&format->fTotalBits);
1176 format->fTotalBits += format->fStencilBits;
1177 } else {
1178 format->fTotalBits = format->fStencilBits;
1179 }
1180 }
1181}
1182}
1183
1184bool GrGpuGL::createStencilBufferForRenderTarget(GrRenderTarget* rt,
1185 int width, int height) {
1186
1187 // All internally created RTs are also textures. We don't create
1188 // SBs for a client's standalone RT (that is RT that isnt also a texture).
1189 GrAssert(rt->asTexture());
bsalomon@google.com99621082011-11-15 16:47:16 +00001190 GrAssert(width >= rt->width());
1191 GrAssert(height >= rt->height());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001192
1193 int samples = rt->numSamples();
1194 GrGLuint sbID;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001195 GL_CALL(GenRenderbuffers(1, &sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001196 if (!sbID) {
1197 return false;
1198 }
1199
1200 GrGLStencilBuffer* sb = NULL;
1201
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001202 int stencilFmtCnt = fGLCaps.fStencilFormats.count();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001203 for (int i = 0; i < stencilFmtCnt; ++i) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001204 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001205 // we start with the last stencil format that succeeded in hopes
1206 // that we won't go through this loop more than once after the
1207 // first (painful) stencil creation.
1208 int sIdx = (i + fLastSuccessfulStencilFmtIdx) % stencilFmtCnt;
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001209 const GLCaps::StencilFormat& sFmt = fGLCaps.fStencilFormats[sIdx];
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001210 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001211 // we do this "if" so that we don't call the multisample
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001212 // version on a GL that doesn't have an MSAA extension.
1213 if (samples > 1) {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001214 GL_ALLOC_CALL(this->glInterface(),
1215 RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
1216 samples,
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001217 sFmt.fFormat.fInternalFormat,
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001218 width, height));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001219 } else {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001220 GL_ALLOC_CALL(this->glInterface(),
1221 RenderbufferStorage(GR_GL_RENDERBUFFER,
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001222 sFmt.fFormat.fInternalFormat,
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001223 width, height));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001224 }
1225
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001226 GrGLenum err = CHECK_ALLOC_ERROR(this->glInterface());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001227 if (err == GR_GL_NO_ERROR) {
1228 // After sized formats we attempt an unsized format and take whatever
1229 // sizes GL gives us. In that case we query for the size.
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001230 GrGLStencilBuffer::Format format = sFmt.fFormat;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001231 get_stencil_rb_sizes(this->glInterface(), sbID, &format);
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001232 sb = new GrGLStencilBuffer(this, sbID, width, height,
1233 samples, format);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001234 if (this->attachStencilBufferToRenderTarget(sb, rt)) {
1235 fLastSuccessfulStencilFmtIdx = sIdx;
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001236 rt->setStencilBuffer(sb);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001237 sb->unref();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001238 return true;
1239 }
1240 sb->abandon(); // otherwise we lose sbID
1241 sb->unref();
1242 }
1243 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001244 GL_CALL(DeleteRenderbuffers(1, &sbID));
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001245 return false;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001246}
1247
1248bool GrGpuGL::attachStencilBufferToRenderTarget(GrStencilBuffer* sb,
1249 GrRenderTarget* rt) {
1250 GrGLRenderTarget* glrt = (GrGLRenderTarget*) rt;
1251
1252 GrGLuint fbo = glrt->renderFBOID();
1253
1254 if (NULL == sb) {
1255 if (NULL != rt->getStencilBuffer()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001256 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001257 GR_GL_STENCIL_ATTACHMENT,
1258 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001259 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001260 GR_GL_DEPTH_ATTACHMENT,
1261 GR_GL_RENDERBUFFER, 0));
1262#if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001263 GrGLenum status;
1264 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001265 GrAssert(GR_GL_FRAMEBUFFER_COMPLETE == status);
1266#endif
1267 }
1268 return true;
1269 } else {
1270 GrGLStencilBuffer* glsb = (GrGLStencilBuffer*) sb;
1271 GrGLuint rb = glsb->renderbufferID();
1272
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001273 fHWDrawState.setRenderTarget(NULL);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001274 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, fbo));
1275 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001276 GR_GL_STENCIL_ATTACHMENT,
1277 GR_GL_RENDERBUFFER, rb));
1278 if (glsb->format().fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001279 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001280 GR_GL_DEPTH_ATTACHMENT,
1281 GR_GL_RENDERBUFFER, rb));
1282 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001283 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001284 GR_GL_DEPTH_ATTACHMENT,
1285 GR_GL_RENDERBUFFER, 0));
reed@google.comac10a2d2010-12-22 21:39:39 +00001286 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001287
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001288 GrGLenum status;
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001289 if (!fGLCaps.isColorConfigAndStencilFormatVerified(rt->config(),
1290 glsb->format())) {
1291 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1292 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001293 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001294 GR_GL_STENCIL_ATTACHMENT,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001295 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001296 if (glsb->format().fPacked) {
1297 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1298 GR_GL_DEPTH_ATTACHMENT,
1299 GR_GL_RENDERBUFFER, 0));
1300 }
1301 return false;
1302 } else {
1303 fGLCaps.markColorConfigAndStencilFormatAsVerified(
1304 rt->config(),
1305 glsb->format());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001306 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001307 }
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001308 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001309 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001310}
1311
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001312////////////////////////////////////////////////////////////////////////////////
1313
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001314GrVertexBuffer* GrGpuGL::onCreateVertexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001315 GrGLuint id;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001316 GL_CALL(GenBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001317 if (id) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001318 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, id));
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001319 fHWGeometryState.fArrayPtrsDirty = true;
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001320 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001321 // make sure driver can allocate memory for this buffer
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001322 GL_ALLOC_CALL(this->glInterface(),
1323 BufferData(GR_GL_ARRAY_BUFFER,
1324 size,
1325 NULL, // data ptr
1326 dynamic ? GR_GL_DYNAMIC_DRAW :
1327 GR_GL_STATIC_DRAW));
1328 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001329 GL_CALL(DeleteBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001330 // deleting bound buffer does implicit bind to 0
1331 fHWGeometryState.fVertexBuffer = NULL;
1332 return NULL;
1333 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001334 GrGLVertexBuffer* vertexBuffer = new GrGLVertexBuffer(this, id,
reed@google.comac10a2d2010-12-22 21:39:39 +00001335 size, dynamic);
1336 fHWGeometryState.fVertexBuffer = vertexBuffer;
1337 return vertexBuffer;
1338 }
1339 return NULL;
1340}
1341
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001342GrIndexBuffer* GrGpuGL::onCreateIndexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001343 GrGLuint id;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001344 GL_CALL(GenBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001345 if (id) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001346 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, id));
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001347 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001348 // make sure driver can allocate memory for this buffer
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001349 GL_ALLOC_CALL(this->glInterface(),
1350 BufferData(GR_GL_ELEMENT_ARRAY_BUFFER,
1351 size,
1352 NULL, // data ptr
1353 dynamic ? GR_GL_DYNAMIC_DRAW :
1354 GR_GL_STATIC_DRAW));
1355 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001356 GL_CALL(DeleteBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001357 // deleting bound buffer does implicit bind to 0
1358 fHWGeometryState.fIndexBuffer = NULL;
1359 return NULL;
1360 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001361 GrIndexBuffer* indexBuffer = new GrGLIndexBuffer(this, id,
reed@google.comac10a2d2010-12-22 21:39:39 +00001362 size, dynamic);
1363 fHWGeometryState.fIndexBuffer = indexBuffer;
1364 return indexBuffer;
1365 }
1366 return NULL;
1367}
1368
reed@google.comac10a2d2010-12-22 21:39:39 +00001369void GrGpuGL::flushScissor(const GrIRect* rect) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001370 const GrDrawState& drawState = this->getDrawState();
1371 const GrGLRenderTarget* rt =
1372 static_cast<const GrGLRenderTarget*>(drawState.getRenderTarget());
1373
1374 GrAssert(NULL != rt);
1375 const GrGLIRect& vp = rt->getViewport();
reed@google.comac10a2d2010-12-22 21:39:39 +00001376
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001377 GrGLIRect scissor;
1378 if (NULL != rect) {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001379 scissor.setRelativeTo(vp, rect->fLeft, rect->fTop,
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001380 rect->width(), rect->height());
1381 if (scissor.contains(vp)) {
1382 rect = NULL;
1383 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001384 }
1385
1386 if (NULL != rect) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001387 if (fHWBounds.fScissorRect != scissor) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001388 scissor.pushToGLScissor(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001389 fHWBounds.fScissorRect = scissor;
1390 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001391 if (!fHWBounds.fScissorEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001392 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001393 fHWBounds.fScissorEnabled = true;
1394 }
1395 } else {
1396 if (fHWBounds.fScissorEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001397 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001398 fHWBounds.fScissorEnabled = false;
1399 }
1400 }
1401}
1402
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001403void GrGpuGL::onClear(const GrIRect* rect, GrColor color) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001404 const GrDrawState& drawState = this->getDrawState();
1405 const GrRenderTarget* rt = drawState.getRenderTarget();
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001406 // parent class should never let us get here with no RT
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001407 GrAssert(NULL != rt);
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001408
bsalomon@google.com74b98712011-11-11 19:46:16 +00001409 GrIRect clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001410 if (NULL != rect) {
1411 // flushScissor expects rect to be clipped to the target.
bsalomon@google.com74b98712011-11-11 19:46:16 +00001412 clippedRect = *rect;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001413 GrIRect rtRect = SkIRect::MakeWH(rt->width(), rt->height());
bsalomon@google.com74b98712011-11-11 19:46:16 +00001414 if (clippedRect.intersect(rtRect)) {
1415 rect = &clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001416 } else {
1417 return;
1418 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001419 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001420 this->flushRenderTarget(rect);
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001421 this->flushScissor(rect);
bsalomon@google.com74b98712011-11-11 19:46:16 +00001422
1423 GrGLfloat r, g, b, a;
1424 static const GrGLfloat scale255 = 1.f / 255.f;
1425 a = GrColorUnpackA(color) * scale255;
1426 GrGLfloat scaleRGB = scale255;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001427 if (GrPixelConfigIsUnpremultiplied(rt->config())) {
bsalomon@google.com74b98712011-11-11 19:46:16 +00001428 scaleRGB *= a;
1429 }
1430 r = GrColorUnpackR(color) * scaleRGB;
1431 g = GrColorUnpackG(color) * scaleRGB;
1432 b = GrColorUnpackB(color) * scaleRGB;
1433
1434 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001435 fHWDrawState.disableState(GrDrawState::kNoColorWrites_StateBit);
bsalomon@google.com74b98712011-11-11 19:46:16 +00001436 GL_CALL(ClearColor(r, g, b, a));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001437 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001438}
1439
bsalomon@google.comedc177d2011-08-05 15:46:40 +00001440void GrGpuGL::clearStencil() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001441 if (NULL == this->getDrawState().getRenderTarget()) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001442 return;
1443 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001444
1445 this->flushRenderTarget(&GrIRect::EmptyIRect());
1446
reed@google.comac10a2d2010-12-22 21:39:39 +00001447 if (fHWBounds.fScissorEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001448 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001449 fHWBounds.fScissorEnabled = false;
reed@google.comac10a2d2010-12-22 21:39:39 +00001450 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001451 GL_CALL(StencilMask(0xffffffff));
1452 GL_CALL(ClearStencil(0));
1453 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001454 fHWDrawState.stencil()->invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001455}
1456
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001457void GrGpuGL::clearStencilClip(const GrIRect& rect, bool insideClip) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001458 const GrDrawState& drawState = this->getDrawState();
1459 const GrRenderTarget* rt = drawState.getRenderTarget();
1460 GrAssert(NULL != rt);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001461
1462 // this should only be called internally when we know we have a
1463 // stencil buffer.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001464 GrAssert(NULL != rt->getStencilBuffer());
1465 GrGLint stencilBitCount = rt->getStencilBuffer()->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001466#if 0
reed@google.comac10a2d2010-12-22 21:39:39 +00001467 GrAssert(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00001468 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001469#else
1470 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001471 // ANGLE a partial stencil mask will cause clears to be
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001472 // turned into draws. Our contract on GrDrawTarget says that
1473 // changing the clip between stencil passes may or may not
1474 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00001475 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001476#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001477 GrGLint value;
1478 if (insideClip) {
1479 value = (1 << (stencilBitCount - 1));
1480 } else {
1481 value = 0;
1482 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001483 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001484 this->flushScissor(&rect);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001485 GL_CALL(StencilMask(clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001486 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001487 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001488 fHWDrawState.stencil()->invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001489}
1490
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001491void GrGpuGL::onForceRenderTargetFlush() {
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001492 this->flushRenderTarget(&GrIRect::EmptyIRect());
reed@google.comac10a2d2010-12-22 21:39:39 +00001493}
1494
bsalomon@google.comc4364992011-11-07 15:54:49 +00001495bool GrGpuGL::readPixelsWillPayForYFlip(GrRenderTarget* renderTarget,
1496 int left, int top,
1497 int width, int height,
1498 GrPixelConfig config,
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001499 size_t rowBytes) const {
1500 // if GL can do the flip then we'll never pay for it.
1501 if (this->glCaps().fPackFlipYSupport) {
1502 return false;
1503 }
1504
1505 // If we have to do memcpy to handle non-trim rowBytes then we
bsalomon@google.com7107fa72011-11-10 14:54:14 +00001506 // get the flip for free. Otherwise it costs.
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001507 if (this->glCaps().fPackRowLengthSupport) {
1508 return true;
1509 }
1510 // If we have to do memcpys to handle rowBytes then y-flip is free
1511 // Note the rowBytes might be tight to the passed in data, but if data
1512 // gets clipped in x to the target the rowBytes will no longer be tight.
1513 if (left >= 0 && (left + width) < renderTarget->width()) {
1514 return 0 == rowBytes ||
1515 GrBytesPerPixel(config) * width == rowBytes;
1516 } else {
1517 return false;
1518 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001519}
1520
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001521bool GrGpuGL::onReadPixels(GrRenderTarget* target,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001522 int left, int top,
1523 int width, int height,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001524 GrPixelConfig config,
1525 void* buffer,
1526 size_t rowBytes,
1527 bool invertY) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001528 GrGLenum format;
1529 GrGLenum type;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00001530 if (!this->configToGLFormats(config, false, NULL, &format, &type)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001531 return false;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001532 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001533 size_t bpp = GrBytesPerPixel(config);
1534 if (!adjust_pixel_ops_params(target->width(), target->height(), bpp,
1535 &left, &top, &width, &height,
1536 const_cast<const void**>(&buffer),
1537 &rowBytes)) {
1538 return false;
1539 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001540
bsalomon@google.comc6980972011-11-02 19:57:21 +00001541 // resolve the render target if necessary
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001542 GrGLRenderTarget* tgt = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001543 GrDrawState::AutoRenderTargetRestore artr;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001544 switch (tgt->getResolveType()) {
1545 case GrGLRenderTarget::kCantResolve_ResolveType:
1546 return false;
1547 case GrGLRenderTarget::kAutoResolves_ResolveType:
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001548 artr.set(this->drawState(), target);
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001549 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001550 break;
1551 case GrGLRenderTarget::kCanResolve_ResolveType:
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001552 this->onResolveRenderTarget(tgt);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001553 // we don't track the state of the READ FBO ID.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001554 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1555 tgt->textureFBOID()));
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001556 break;
1557 default:
1558 GrCrash("Unknown resolve type");
reed@google.comac10a2d2010-12-22 21:39:39 +00001559 }
1560
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001561 const GrGLIRect& glvp = tgt->getViewport();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001562
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001563 // the read rect is viewport-relative
1564 GrGLIRect readRect;
1565 readRect.setRelativeTo(glvp, left, top, width, height);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001566
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001567 size_t tightRowBytes = bpp * width;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001568 if (0 == rowBytes) {
1569 rowBytes = tightRowBytes;
1570 }
1571 size_t readDstRowBytes = tightRowBytes;
1572 void* readDst = buffer;
1573
1574 // determine if GL can read using the passed rowBytes or if we need
1575 // a scratch buffer.
1576 SkAutoSMalloc<32 * sizeof(GrColor)> scratch;
1577 if (rowBytes != tightRowBytes) {
bsalomon@google.com7107fa72011-11-10 14:54:14 +00001578 if (this->glCaps().fPackRowLengthSupport) {
bsalomon@google.comc6980972011-11-02 19:57:21 +00001579 GrAssert(!(rowBytes % sizeof(GrColor)));
1580 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, rowBytes / sizeof(GrColor)));
1581 readDstRowBytes = rowBytes;
1582 } else {
1583 scratch.reset(tightRowBytes * height);
1584 readDst = scratch.get();
1585 }
1586 }
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001587 if (!invertY && this->glCaps().fPackFlipYSupport) {
1588 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 1));
1589 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001590 GL_CALL(ReadPixels(readRect.fLeft, readRect.fBottom,
1591 readRect.fWidth, readRect.fHeight,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001592 format, type, readDst));
1593 if (readDstRowBytes != tightRowBytes) {
bsalomon@google.com7107fa72011-11-10 14:54:14 +00001594 GrAssert(this->glCaps().fPackRowLengthSupport);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001595 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
1596 }
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001597 if (!invertY && this->glCaps().fPackFlipYSupport) {
1598 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 0));
1599 invertY = true;
1600 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001601
1602 // now reverse the order of the rows, since GL's are bottom-to-top, but our
bsalomon@google.comc6980972011-11-02 19:57:21 +00001603 // API presents top-to-bottom. We must preserve the padding contents. Note
1604 // that the above readPixels did not overwrite the padding.
1605 if (readDst == buffer) {
1606 GrAssert(rowBytes == readDstRowBytes);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001607 if (!invertY) {
1608 scratch.reset(tightRowBytes);
1609 void* tmpRow = scratch.get();
1610 // flip y in-place by rows
1611 const int halfY = height >> 1;
1612 char* top = reinterpret_cast<char*>(buffer);
1613 char* bottom = top + (height - 1) * rowBytes;
1614 for (int y = 0; y < halfY; y++) {
1615 memcpy(tmpRow, top, tightRowBytes);
1616 memcpy(top, bottom, tightRowBytes);
1617 memcpy(bottom, tmpRow, tightRowBytes);
1618 top += rowBytes;
1619 bottom -= rowBytes;
1620 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001621 }
1622 } else {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001623 GrAssert(readDst != buffer); GrAssert(rowBytes != tightRowBytes);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001624 // copy from readDst to buffer while flipping y
1625 const int halfY = height >> 1;
1626 const char* src = reinterpret_cast<const char*>(readDst);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001627 char* dst = reinterpret_cast<char*>(buffer);
1628 if (!invertY) {
1629 dst += (height-1) * rowBytes;
1630 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001631 for (int y = 0; y < height; y++) {
1632 memcpy(dst, src, tightRowBytes);
1633 src += readDstRowBytes;
bsalomon@google.comc4364992011-11-07 15:54:49 +00001634 if (invertY) {
1635 dst += rowBytes;
1636 } else {
1637 dst -= rowBytes;
1638 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001639 }
1640 }
1641 return true;
1642}
1643
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001644void GrGpuGL::flushRenderTarget(const GrIRect* bound) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001645
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001646 GrGLRenderTarget* rt =
1647 static_cast<GrGLRenderTarget*>(this->drawState()->getRenderTarget());
1648 GrAssert(NULL != rt);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001649
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001650 if (fHWDrawState.getRenderTarget() != rt) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001651 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, rt->renderFBOID()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001652 #if GR_COLLECT_STATS
1653 ++fStats.fRenderTargetChngCnt;
1654 #endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001655 #if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001656 GrGLenum status;
1657 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001658 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
reed@google.comb9255d52011-06-13 18:54:59 +00001659 GrPrintf("GrGpuGL::flushRenderTarget glCheckFramebufferStatus %x\n", status);
reed@google.comac10a2d2010-12-22 21:39:39 +00001660 }
1661 #endif
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001662 fDirtyFlags.fRenderTargetChanged = true;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001663 fHWDrawState.setRenderTarget(rt);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001664 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com649a8622011-03-10 14:53:38 +00001665 if (fHWBounds.fViewportRect != vp) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001666 vp.pushToGLViewport(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001667 fHWBounds.fViewportRect = vp;
1668 }
1669 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001670 if (NULL == bound || !bound->isEmpty()) {
1671 rt->flagAsNeedingResolve(bound);
1672 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001673}
1674
twiz@google.com0f31ca72011-03-18 17:38:11 +00001675GrGLenum gPrimitiveType2GLMode[] = {
1676 GR_GL_TRIANGLES,
1677 GR_GL_TRIANGLE_STRIP,
1678 GR_GL_TRIANGLE_FAN,
1679 GR_GL_POINTS,
1680 GR_GL_LINES,
1681 GR_GL_LINE_STRIP
reed@google.comac10a2d2010-12-22 21:39:39 +00001682};
1683
bsalomon@google.comd302f142011-03-03 13:54:13 +00001684#define SWAP_PER_DRAW 0
1685
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001686#if SWAP_PER_DRAW
bsalomon@google.comd302f142011-03-03 13:54:13 +00001687 #if GR_MAC_BUILD
1688 #include <AGL/agl.h>
1689 #elif GR_WIN32_BUILD
1690 void SwapBuf() {
1691 DWORD procID = GetCurrentProcessId();
1692 HWND hwnd = GetTopWindow(GetDesktopWindow());
1693 while(hwnd) {
1694 DWORD wndProcID = 0;
1695 GetWindowThreadProcessId(hwnd, &wndProcID);
1696 if(wndProcID == procID) {
1697 SwapBuffers(GetDC(hwnd));
1698 }
1699 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
1700 }
1701 }
1702 #endif
1703#endif
1704
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001705void GrGpuGL::onGpuDrawIndexed(GrPrimitiveType type,
1706 uint32_t startVertex,
1707 uint32_t startIndex,
1708 uint32_t vertexCount,
1709 uint32_t indexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001710 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1711
twiz@google.com0f31ca72011-03-18 17:38:11 +00001712 GrGLvoid* indices = (GrGLvoid*)(sizeof(uint16_t) * startIndex);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001713
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001714 GrAssert(NULL != fHWGeometryState.fIndexBuffer);
1715 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1716
1717 // our setupGeometry better have adjusted this to zero since
1718 // DrawElements always draws from the begining of the arrays for idx 0.
1719 GrAssert(0 == startVertex);
reed@google.comac10a2d2010-12-22 21:39:39 +00001720
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001721 GL_CALL(DrawElements(gPrimitiveType2GLMode[type], indexCount,
1722 GR_GL_UNSIGNED_SHORT, indices));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001723#if SWAP_PER_DRAW
1724 glFlush();
1725 #if GR_MAC_BUILD
1726 aglSwapBuffers(aglGetCurrentContext());
1727 int set_a_break_pt_here = 9;
1728 aglSwapBuffers(aglGetCurrentContext());
1729 #elif GR_WIN32_BUILD
1730 SwapBuf();
1731 int set_a_break_pt_here = 9;
1732 SwapBuf();
1733 #endif
1734#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001735}
1736
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001737void GrGpuGL::onGpuDrawNonIndexed(GrPrimitiveType type,
1738 uint32_t startVertex,
1739 uint32_t vertexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001740 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1741
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001742 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1743
1744 // our setupGeometry better have adjusted this to zero.
1745 // DrawElements doesn't take an offset so we always adjus the startVertex.
1746 GrAssert(0 == startVertex);
1747
1748 // pass 0 for parameter first. We have to adjust gl*Pointer() to
1749 // account for startVertex in the DrawElements case. So we always
1750 // rely on setupGeometry to have accounted for startVertex.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001751 GL_CALL(DrawArrays(gPrimitiveType2GLMode[type], 0, vertexCount));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001752#if SWAP_PER_DRAW
1753 glFlush();
1754 #if GR_MAC_BUILD
1755 aglSwapBuffers(aglGetCurrentContext());
1756 int set_a_break_pt_here = 9;
1757 aglSwapBuffers(aglGetCurrentContext());
1758 #elif GR_WIN32_BUILD
1759 SwapBuf();
1760 int set_a_break_pt_here = 9;
1761 SwapBuf();
1762 #endif
1763#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001764}
1765
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001766void GrGpuGL::onResolveRenderTarget(GrRenderTarget* target) {
1767
1768 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(target);
reed@google.comac10a2d2010-12-22 21:39:39 +00001769
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001770 if (rt->needsResolve()) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001771 GrAssert(GLCaps::kNone_MSFBO != fGLCaps.fMSFBOType);
reed@google.comac10a2d2010-12-22 21:39:39 +00001772 GrAssert(rt->textureFBOID() != rt->renderFBOID());
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001773 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1774 rt->renderFBOID()));
1775 GL_CALL(BindFramebuffer(GR_GL_DRAW_FRAMEBUFFER,
1776 rt->textureFBOID()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001777 #if GR_COLLECT_STATS
1778 ++fStats.fRenderTargetChngCnt;
1779 #endif
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001780 // make sure we go through flushRenderTarget() since we've modified
1781 // the bound DRAW FBO ID.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001782 fHWDrawState.setRenderTarget(NULL);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001783 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001784 const GrIRect dirtyRect = rt->getResolveRect();
1785 GrGLIRect r;
1786 r.setRelativeTo(vp, dirtyRect.fLeft, dirtyRect.fTop,
1787 dirtyRect.width(), dirtyRect.height());
reed@google.comac10a2d2010-12-22 21:39:39 +00001788
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001789 if (GLCaps::kAppleES_MSFBO == fGLCaps.fMSFBOType) {
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001790 // Apple's extension uses the scissor as the blit bounds.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001791 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1792 GL_CALL(Scissor(r.fLeft, r.fBottom,
1793 r.fWidth, r.fHeight));
1794 GL_CALL(ResolveMultisampleFramebuffer());
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001795 fHWBounds.fScissorRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001796 fHWBounds.fScissorEnabled = true;
1797 } else {
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001798 if (GLCaps::kDesktopARB_MSFBO != fGLCaps.fMSFBOType) {
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001799 // this respects the scissor during the blit, so disable it.
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001800 GrAssert(GLCaps::kDesktopEXT_MSFBO == fGLCaps.fMSFBOType);
1801 this->flushScissor(NULL);
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001802 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001803 int right = r.fLeft + r.fWidth;
1804 int top = r.fBottom + r.fHeight;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001805 GL_CALL(BlitFramebuffer(r.fLeft, r.fBottom, right, top,
1806 r.fLeft, r.fBottom, right, top,
1807 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001808 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001809 rt->flagAsResolved();
reed@google.comac10a2d2010-12-22 21:39:39 +00001810 }
1811}
1812
twiz@google.com0f31ca72011-03-18 17:38:11 +00001813static const GrGLenum grToGLStencilFunc[] = {
1814 GR_GL_ALWAYS, // kAlways_StencilFunc
1815 GR_GL_NEVER, // kNever_StencilFunc
1816 GR_GL_GREATER, // kGreater_StencilFunc
1817 GR_GL_GEQUAL, // kGEqual_StencilFunc
1818 GR_GL_LESS, // kLess_StencilFunc
1819 GR_GL_LEQUAL, // kLEqual_StencilFunc,
1820 GR_GL_EQUAL, // kEqual_StencilFunc,
1821 GR_GL_NOTEQUAL, // kNotEqual_StencilFunc,
bsalomon@google.comd302f142011-03-03 13:54:13 +00001822};
1823GR_STATIC_ASSERT(GR_ARRAY_COUNT(grToGLStencilFunc) == kBasicStencilFuncCount);
1824GR_STATIC_ASSERT(0 == kAlways_StencilFunc);
1825GR_STATIC_ASSERT(1 == kNever_StencilFunc);
1826GR_STATIC_ASSERT(2 == kGreater_StencilFunc);
1827GR_STATIC_ASSERT(3 == kGEqual_StencilFunc);
1828GR_STATIC_ASSERT(4 == kLess_StencilFunc);
1829GR_STATIC_ASSERT(5 == kLEqual_StencilFunc);
1830GR_STATIC_ASSERT(6 == kEqual_StencilFunc);
1831GR_STATIC_ASSERT(7 == kNotEqual_StencilFunc);
1832
twiz@google.com0f31ca72011-03-18 17:38:11 +00001833static const GrGLenum grToGLStencilOp[] = {
1834 GR_GL_KEEP, // kKeep_StencilOp
1835 GR_GL_REPLACE, // kReplace_StencilOp
1836 GR_GL_INCR_WRAP, // kIncWrap_StencilOp
1837 GR_GL_INCR, // kIncClamp_StencilOp
1838 GR_GL_DECR_WRAP, // kDecWrap_StencilOp
1839 GR_GL_DECR, // kDecClamp_StencilOp
1840 GR_GL_ZERO, // kZero_StencilOp
1841 GR_GL_INVERT, // kInvert_StencilOp
bsalomon@google.comd302f142011-03-03 13:54:13 +00001842};
1843GR_STATIC_ASSERT(GR_ARRAY_COUNT(grToGLStencilOp) == kStencilOpCount);
1844GR_STATIC_ASSERT(0 == kKeep_StencilOp);
1845GR_STATIC_ASSERT(1 == kReplace_StencilOp);
1846GR_STATIC_ASSERT(2 == kIncWrap_StencilOp);
1847GR_STATIC_ASSERT(3 == kIncClamp_StencilOp);
1848GR_STATIC_ASSERT(4 == kDecWrap_StencilOp);
1849GR_STATIC_ASSERT(5 == kDecClamp_StencilOp);
1850GR_STATIC_ASSERT(6 == kZero_StencilOp);
1851GR_STATIC_ASSERT(7 == kInvert_StencilOp);
1852
reed@google.comac10a2d2010-12-22 21:39:39 +00001853void GrGpuGL::flushStencil() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001854 const GrDrawState& drawState = this->getDrawState();
1855
1856 const GrStencilSettings* settings = &drawState.getStencil();
reed@google.comac10a2d2010-12-22 21:39:39 +00001857
1858 // use stencil for clipping if clipping is enabled and the clip
1859 // has been written into the stencil.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001860 bool stencilClip = fClipInStencil && drawState.isClipState();
1861 bool drawClipToStencil =
1862 drawState.isStateFlagEnabled(kModifyStencilClip_StateBit);
bsalomon@google.com39dab772012-01-03 19:39:31 +00001863 bool stencilChange = (fHWDrawState.getStencil() != *settings) ||
1864 (fHWStencilClip != stencilClip) ||
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001865 (fHWDrawState.isStateFlagEnabled(kModifyStencilClip_StateBit) !=
1866 drawClipToStencil);
reed@google.comac10a2d2010-12-22 21:39:39 +00001867
1868 if (stencilChange) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001869
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001870 // we can't simultaneously perform stencil-clipping and
1871 // modify the stencil clip
1872 GrAssert(!stencilClip || !drawClipToStencil);
reed@google.comac10a2d2010-12-22 21:39:39 +00001873
bsalomon@google.comd302f142011-03-03 13:54:13 +00001874 if (settings->isDisabled()) {
1875 if (stencilClip) {
epoger@google.com8722d7c2012-01-31 17:18:58 +00001876 settings = &gClipStencilSettings;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001877 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001878 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00001879
1880 if (settings->isDisabled()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001881 GL_CALL(Disable(GR_GL_STENCIL_TEST));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001882 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001883 GL_CALL(Enable(GR_GL_STENCIL_TEST));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001884 #if GR_DEBUG
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001885 if (!this->getCaps().fStencilWrapOpsSupport) {
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001886 GrAssert(settings->frontPassOp() != kIncWrap_StencilOp);
1887 GrAssert(settings->frontPassOp() != kDecWrap_StencilOp);
1888 GrAssert(settings->frontFailOp() != kIncWrap_StencilOp);
1889 GrAssert(settings->backFailOp() != kDecWrap_StencilOp);
1890 GrAssert(settings->backPassOp() != kIncWrap_StencilOp);
1891 GrAssert(settings->backPassOp() != kDecWrap_StencilOp);
1892 GrAssert(settings->backFailOp() != kIncWrap_StencilOp);
1893 GrAssert(settings->frontFailOp() != kDecWrap_StencilOp);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001894 }
1895 #endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001896 int stencilBits = 0;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001897 GrStencilBuffer* stencilBuffer =
1898 drawState.getRenderTarget()->getStencilBuffer();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001899 if (NULL != stencilBuffer) {
1900 stencilBits = stencilBuffer->bits();
1901 }
1902 // TODO: dynamically attach a stencil buffer
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001903 GrAssert(stencilBits || settings->isDisabled());
bsalomon@google.com2cdfade2011-11-23 16:53:42 +00001904
1905 GrGLuint clipStencilMask = 0;
1906 GrGLuint userStencilMask = ~0;
1907 if (stencilBits > 0) {
1908 clipStencilMask = 1 << (stencilBits - 1);
1909 userStencilMask = clipStencilMask - 1;
1910 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00001911
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001912 unsigned int frontRef = settings->frontFuncRef();
1913 unsigned int frontMask = settings->frontFuncMask();
1914 unsigned int frontWriteMask = settings->frontWriteMask();
twiz@google.com0f31ca72011-03-18 17:38:11 +00001915 GrGLenum frontFunc;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001916
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001917 if (drawClipToStencil) {
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001918 GrAssert(settings->frontFunc() < kBasicStencilFuncCount);
1919 frontFunc = grToGLStencilFunc[settings->frontFunc()];
bsalomon@google.comd302f142011-03-03 13:54:13 +00001920 } else {
tomhudson@google.com62b09682011-11-09 16:39:17 +00001921 frontFunc = grToGLStencilFunc[ConvertStencilFunc(
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001922 stencilClip, settings->frontFunc())];
bsalomon@google.comd302f142011-03-03 13:54:13 +00001923
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001924 ConvertStencilFuncAndMask(settings->frontFunc(),
bsalomon@google.comd302f142011-03-03 13:54:13 +00001925 stencilClip,
1926 clipStencilMask,
1927 userStencilMask,
1928 &frontRef,
1929 &frontMask);
1930 frontWriteMask &= userStencilMask;
1931 }
tomhudson@google.com62b09682011-11-09 16:39:17 +00001932 GrAssert((size_t)
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001933 settings->frontFailOp() < GR_ARRAY_COUNT(grToGLStencilOp));
tomhudson@google.com62b09682011-11-09 16:39:17 +00001934 GrAssert((size_t)
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001935 settings->frontPassOp() < GR_ARRAY_COUNT(grToGLStencilOp));
tomhudson@google.com62b09682011-11-09 16:39:17 +00001936 GrAssert((size_t)
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001937 settings->backFailOp() < GR_ARRAY_COUNT(grToGLStencilOp));
tomhudson@google.com62b09682011-11-09 16:39:17 +00001938 GrAssert((size_t)
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001939 settings->backPassOp() < GR_ARRAY_COUNT(grToGLStencilOp));
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001940 if (this->getCaps().fTwoSidedStencilSupport) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001941 GrGLenum backFunc;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001942
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001943 unsigned int backRef = settings->backFuncRef();
1944 unsigned int backMask = settings->backFuncMask();
1945 unsigned int backWriteMask = settings->backWriteMask();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001946
1947
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001948 if (drawClipToStencil) {
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001949 GrAssert(settings->backFunc() < kBasicStencilFuncCount);
1950 backFunc = grToGLStencilFunc[settings->backFunc()];
bsalomon@google.comd302f142011-03-03 13:54:13 +00001951 } else {
tomhudson@google.com62b09682011-11-09 16:39:17 +00001952 backFunc = grToGLStencilFunc[ConvertStencilFunc(
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001953 stencilClip, settings->backFunc())];
1954 ConvertStencilFuncAndMask(settings->backFunc(),
bsalomon@google.comd302f142011-03-03 13:54:13 +00001955 stencilClip,
1956 clipStencilMask,
1957 userStencilMask,
1958 &backRef,
1959 &backMask);
1960 backWriteMask &= userStencilMask;
1961 }
1962
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001963 GL_CALL(StencilFuncSeparate(GR_GL_FRONT, frontFunc,
1964 frontRef, frontMask));
1965 GL_CALL(StencilMaskSeparate(GR_GL_FRONT, frontWriteMask));
1966 GL_CALL(StencilFuncSeparate(GR_GL_BACK, backFunc,
1967 backRef, backMask));
1968 GL_CALL(StencilMaskSeparate(GR_GL_BACK, backWriteMask));
1969 GL_CALL(StencilOpSeparate(GR_GL_FRONT,
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001970 grToGLStencilOp[settings->frontFailOp()],
1971 grToGLStencilOp[settings->frontPassOp()],
1972 grToGLStencilOp[settings->frontPassOp()]));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001973
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001974 GL_CALL(StencilOpSeparate(GR_GL_BACK,
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001975 grToGLStencilOp[settings->backFailOp()],
1976 grToGLStencilOp[settings->backPassOp()],
1977 grToGLStencilOp[settings->backPassOp()]));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001978 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001979 GL_CALL(StencilFunc(frontFunc, frontRef, frontMask));
1980 GL_CALL(StencilMask(frontWriteMask));
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001981 GL_CALL(StencilOp(grToGLStencilOp[settings->frontFailOp()],
1982 grToGLStencilOp[settings->frontPassOp()],
1983 grToGLStencilOp[settings->frontPassOp()]));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001984 }
1985 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001986 *fHWDrawState.stencil() = *settings;
reed@google.comac10a2d2010-12-22 21:39:39 +00001987 fHWStencilClip = stencilClip;
1988 }
1989}
1990
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001991void GrGpuGL::flushAAState(GrPrimitiveType type) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001992 const GrRenderTarget* rt = this->getDrawState().getRenderTarget();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001993 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001994 // ES doesn't support toggling GL_MULTISAMPLE and doesn't have
1995 // smooth lines.
1996
1997 // we prefer smooth lines over multisampled lines
1998 // msaa should be disabled if drawing smooth lines.
bsalomon@google.com0650e812011-04-08 18:07:53 +00001999 if (GrIsPrimTypeLines(type)) {
bsalomon@google.com471d4712011-08-23 15:45:25 +00002000 bool smooth = this->willUseHWAALines();
bsalomon@google.com0650e812011-04-08 18:07:53 +00002001 if (!fHWAAState.fSmoothLineEnabled && smooth) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002002 GL_CALL(Enable(GR_GL_LINE_SMOOTH));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00002003 fHWAAState.fSmoothLineEnabled = true;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002004 } else if (fHWAAState.fSmoothLineEnabled && !smooth) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002005 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00002006 fHWAAState.fSmoothLineEnabled = false;
2007 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002008 if (rt->isMultisampled() &&
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00002009 fHWAAState.fMSAAEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002010 GL_CALL(Disable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00002011 fHWAAState.fMSAAEnabled = false;
2012 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002013 } else if (rt->isMultisampled() &&
2014 this->getDrawState().isHWAntialiasState() !=
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00002015 fHWAAState.fMSAAEnabled) {
2016 if (fHWAAState.fMSAAEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002017 GL_CALL(Disable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00002018 fHWAAState.fMSAAEnabled = false;
2019 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002020 GL_CALL(Enable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00002021 fHWAAState.fMSAAEnabled = true;
2022 }
2023 }
2024 }
2025}
2026
bsalomon@google.com86c1f712011-10-12 14:54:26 +00002027void GrGpuGL::flushBlend(GrPrimitiveType type,
2028 GrBlendCoeff srcCoeff,
bsalomon@google.com271cffc2011-05-20 14:13:56 +00002029 GrBlendCoeff dstCoeff) {
bsalomon@google.com471d4712011-08-23 15:45:25 +00002030 if (GrIsPrimTypeLines(type) && this->willUseHWAALines()) {
bsalomon@google.com0650e812011-04-08 18:07:53 +00002031 if (fHWBlendDisabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002032 GL_CALL(Enable(GR_GL_BLEND));
bsalomon@google.com0650e812011-04-08 18:07:53 +00002033 fHWBlendDisabled = false;
2034 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002035 if (kSA_BlendCoeff != fHWDrawState.getSrcBlendCoeff() ||
2036 kISA_BlendCoeff != fHWDrawState.getDstBlendCoeff()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002037 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[kSA_BlendCoeff],
2038 gXfermodeCoeff2Blend[kISA_BlendCoeff]));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002039 fHWDrawState.setBlendFunc(kSA_BlendCoeff, kISA_BlendCoeff);
bsalomon@google.com0650e812011-04-08 18:07:53 +00002040 }
2041 } else {
bsalomon@google.com86c1f712011-10-12 14:54:26 +00002042 // any optimization to disable blending should
2043 // have already been applied and tweaked the coeffs
2044 // to (1, 0).
2045 bool blendOff = kOne_BlendCoeff == srcCoeff &&
2046 kZero_BlendCoeff == dstCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002047 if (fHWBlendDisabled != blendOff) {
2048 if (blendOff) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002049 GL_CALL(Disable(GR_GL_BLEND));
bsalomon@google.com0650e812011-04-08 18:07:53 +00002050 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002051 GL_CALL(Enable(GR_GL_BLEND));
bsalomon@google.com0650e812011-04-08 18:07:53 +00002052 }
2053 fHWBlendDisabled = blendOff;
2054 }
2055 if (!blendOff) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002056 if (fHWDrawState.getSrcBlendCoeff() != srcCoeff ||
2057 fHWDrawState.getDstBlendCoeff() != dstCoeff) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002058 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
2059 gXfermodeCoeff2Blend[dstCoeff]));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002060 fHWDrawState.setBlendFunc(srcCoeff, dstCoeff);
bsalomon@google.com0650e812011-04-08 18:07:53 +00002061 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002062 GrColor blendConst = fCurrDrawState.getBlendConstant();
bsalomon@google.com271cffc2011-05-20 14:13:56 +00002063 if ((BlendCoeffReferencesConstant(srcCoeff) ||
2064 BlendCoeffReferencesConstant(dstCoeff)) &&
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002065 fHWDrawState.getBlendConstant() != blendConst) {
bsalomon@google.com0650e812011-04-08 18:07:53 +00002066
2067 float c[] = {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002068 GrColorUnpackR(blendConst) / 255.f,
2069 GrColorUnpackG(blendConst) / 255.f,
2070 GrColorUnpackB(blendConst) / 255.f,
2071 GrColorUnpackA(blendConst) / 255.f
bsalomon@google.com0650e812011-04-08 18:07:53 +00002072 };
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002073 GL_CALL(BlendColor(c[0], c[1], c[2], c[3]));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002074 fHWDrawState.setBlendConstant(blendConst);
bsalomon@google.com0650e812011-04-08 18:07:53 +00002075 }
2076 }
2077 }
2078}
2079
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002080namespace {
2081
2082unsigned gr_to_gl_filter(GrSamplerState::Filter filter) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00002083 switch (filter) {
2084 case GrSamplerState::kBilinear_Filter:
2085 case GrSamplerState::k4x4Downsample_Filter:
2086 return GR_GL_LINEAR;
2087 case GrSamplerState::kNearest_Filter:
2088 case GrSamplerState::kConvolution_Filter:
2089 return GR_GL_NEAREST;
2090 default:
2091 GrAssert(!"Unknown filter type");
2092 return GR_GL_LINEAR;
2093 }
2094}
2095
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002096const GrGLenum* get_swizzle(GrPixelConfig config,
2097 const GrSamplerState& sampler) {
2098 if (GrPixelConfigIsAlphaOnly(config)) {
2099 static const GrGLenum gAlphaSmear[] = { GR_GL_ALPHA, GR_GL_ALPHA,
2100 GR_GL_ALPHA, GR_GL_ALPHA };
2101 return gAlphaSmear;
2102 } else if (sampler.swapsRAndB()) {
2103 static const GrGLenum gRedBlueSwap[] = { GR_GL_BLUE, GR_GL_GREEN,
2104 GR_GL_RED, GR_GL_ALPHA };
2105 return gRedBlueSwap;
2106 } else {
2107 static const GrGLenum gStraight[] = { GR_GL_RED, GR_GL_GREEN,
2108 GR_GL_BLUE, GR_GL_ALPHA };
2109 return gStraight;
2110 }
2111}
2112
2113void set_tex_swizzle(GrGLenum swizzle[4], const GrGLInterface* gl) {
2114 // should add texparameteri to interface to make 1 instead of 4 calls here
2115 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
2116 GR_GL_TEXTURE_SWIZZLE_R,
2117 swizzle[0]));
2118 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
2119 GR_GL_TEXTURE_SWIZZLE_G,
2120 swizzle[1]));
2121 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
2122 GR_GL_TEXTURE_SWIZZLE_B,
2123 swizzle[2]));
2124 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
2125 GR_GL_TEXTURE_SWIZZLE_A,
2126 swizzle[3]));
2127}
2128}
2129
bsalomon@google.comffca4002011-02-22 20:34:01 +00002130bool GrGpuGL::flushGLStateCommon(GrPrimitiveType type) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002131
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002132 GrDrawState* drawState = this->drawState();
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002133 // GrGpu::setupClipAndFlushState should have already checked this
2134 // and bailed if not true.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002135 GrAssert(NULL != drawState->getRenderTarget());
reed@google.comac10a2d2010-12-22 21:39:39 +00002136
tomhudson@google.com93813632011-10-27 20:21:16 +00002137 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002138 // bind texture and set sampler state
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002139 if (this->isStageEnabled(s)) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002140 GrGLTexture* nextTexture =
2141 static_cast<GrGLTexture*>(drawState->getTexture(s));
reed@google.comac10a2d2010-12-22 21:39:39 +00002142
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002143 // true for now, but maybe not with GrEffect.
2144 GrAssert(NULL != nextTexture);
2145 // if we created a rt/tex and rendered to it without using a
2146 // texture and now we're texuring from the rt it will still be
2147 // the last bound texture, but it needs resolving. So keep this
2148 // out of the "last != next" check.
2149 GrGLRenderTarget* texRT =
2150 static_cast<GrGLRenderTarget*>(nextTexture->asRenderTarget());
2151 if (NULL != texRT) {
bsalomon@google.com75f9f252012-01-31 13:35:56 +00002152 this->onResolveRenderTarget(texRT);
reed@google.comac10a2d2010-12-22 21:39:39 +00002153 }
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002154
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002155 if (fHWDrawState.getTexture(s) != nextTexture) {
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002156 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002157 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, nextTexture->textureID()));
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002158 #if GR_COLLECT_STATS
2159 ++fStats.fTextureChngCnt;
2160 #endif
2161 //GrPrintf("---- bindtexture %d\n", nextTexture->textureID());
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002162 fHWDrawState.setTexture(s, nextTexture);
bsalomon@google.comcd19a5f2011-06-15 14:00:23 +00002163 // The texture matrix has to compensate for texture width/height
2164 // and NPOT-embedded-in-POT
2165 fDirtyFlags.fTextureChangedMask |= (1 << s);
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002166 }
2167
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002168 const GrSamplerState& sampler = drawState->getSampler(s);
bsalomon@google.com80d09b92011-11-05 21:21:13 +00002169 ResetTimestamp timestamp;
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002170 const GrGLTexture::TexParams& oldTexParams =
bsalomon@google.com80d09b92011-11-05 21:21:13 +00002171 nextTexture->getCachedTexParams(&timestamp);
2172 bool setAll = timestamp < this->getResetTimestamp();
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002173 GrGLTexture::TexParams newTexParams;
2174
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002175 newTexParams.fFilter = gr_to_gl_filter(sampler.getFilter());
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00002176
bsalomon@google.com1dcf5062011-11-14 19:29:53 +00002177 const GrGLenum* wraps = GrGLTexture::WrapMode2GLWrap();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002178 newTexParams.fWrapS = wraps[sampler.getWrapX()];
2179 newTexParams.fWrapT = wraps[sampler.getWrapY()];
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002180 memcpy(newTexParams.fSwizzleRGBA,
2181 get_swizzle(nextTexture->config(), sampler),
2182 sizeof(newTexParams.fSwizzleRGBA));
2183 if (setAll || newTexParams.fFilter != oldTexParams.fFilter) {
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002184 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002185 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002186 GR_GL_TEXTURE_MAG_FILTER,
2187 newTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002188 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002189 GR_GL_TEXTURE_MIN_FILTER,
2190 newTexParams.fFilter));
2191 }
2192 if (setAll || newTexParams.fWrapS != oldTexParams.fWrapS) {
2193 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002194 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002195 GR_GL_TEXTURE_WRAP_S,
2196 newTexParams.fWrapS));
2197 }
2198 if (setAll || newTexParams.fWrapT != oldTexParams.fWrapT) {
2199 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002200 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002201 GR_GL_TEXTURE_WRAP_T,
2202 newTexParams.fWrapT));
2203 }
bsalomon@google.com7107fa72011-11-10 14:54:14 +00002204 if (this->glCaps().fTextureSwizzleSupport &&
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002205 (setAll ||
2206 memcmp(newTexParams.fSwizzleRGBA,
2207 oldTexParams.fSwizzleRGBA,
2208 sizeof(newTexParams.fSwizzleRGBA)))) {
2209 setTextureUnit(s);
2210 set_tex_swizzle(newTexParams.fSwizzleRGBA,
2211 this->glInterface());
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002212 }
bsalomon@google.com80d09b92011-11-05 21:21:13 +00002213 nextTexture->setCachedTexParams(newTexParams,
2214 this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00002215 }
2216 }
2217
bsalomon@google.com8295dc12011-05-02 12:53:34 +00002218 GrIRect* rect = NULL;
2219 GrIRect clipBounds;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002220 if (drawState->isClipState() &&
bsalomon@google.com8295dc12011-05-02 12:53:34 +00002221 fClip.hasConservativeBounds()) {
2222 fClip.getConservativeBounds().roundOut(&clipBounds);
2223 rect = &clipBounds;
2224 }
2225 this->flushRenderTarget(rect);
2226 this->flushAAState(type);
bsalomon@google.com0650e812011-04-08 18:07:53 +00002227
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002228 if (drawState->isDitherState() != fHWDrawState.isDitherState()) {
2229 if (drawState->isDitherState()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002230 GL_CALL(Enable(GR_GL_DITHER));
reed@google.comac10a2d2010-12-22 21:39:39 +00002231 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002232 GL_CALL(Disable(GR_GL_DITHER));
reed@google.comac10a2d2010-12-22 21:39:39 +00002233 }
2234 }
2235
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002236 if (drawState->isColorWriteDisabled() !=
2237 fHWDrawState.isColorWriteDisabled()) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002238 GrGLenum mask;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002239 if (drawState->isColorWriteDisabled()) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002240 mask = GR_GL_FALSE;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002241 } else {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002242 mask = GR_GL_TRUE;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002243 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002244 GL_CALL(ColorMask(mask, mask, mask, mask));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002245 }
2246
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002247 if (fHWDrawState.getDrawFace() != drawState->getDrawFace()) {
2248 switch (fCurrDrawState.getDrawFace()) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002249 case GrDrawState::kCCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002250 GL_CALL(Enable(GR_GL_CULL_FACE));
2251 GL_CALL(CullFace(GR_GL_BACK));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002252 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002253 case GrDrawState::kCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002254 GL_CALL(Enable(GR_GL_CULL_FACE));
2255 GL_CALL(CullFace(GR_GL_FRONT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002256 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002257 case GrDrawState::kBoth_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002258 GL_CALL(Disable(GR_GL_CULL_FACE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002259 break;
2260 default:
2261 GrCrash("Unknown draw face.");
2262 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002263 fHWDrawState.setDrawFace(drawState->getDrawFace());
bsalomon@google.comd302f142011-03-03 13:54:13 +00002264 }
2265
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002266#if GR_DEBUG
reed@google.comac10a2d2010-12-22 21:39:39 +00002267 // check for circular rendering
tomhudson@google.com93813632011-10-27 20:21:16 +00002268 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002269 GrAssert(!this->isStageEnabled(s) ||
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002270 NULL == drawState->getRenderTarget() ||
2271 NULL == drawState->getTexture(s) ||
2272 drawState->getTexture(s)->asRenderTarget() !=
2273 drawState->getRenderTarget());
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002274 }
2275#endif
bsalomon@google.com316f99232011-01-13 21:28:12 +00002276
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002277 this->flushStencil();
reed@google.comac10a2d2010-12-22 21:39:39 +00002278
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002279 // This copy must happen after flushStencil() is called. flushStencil()
2280 // relies on detecting when the kModifyStencilClip_StateBit state has
2281 // changed since the last draw.
2282 fHWDrawState.copyStateFlags(*drawState);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002283 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +00002284}
2285
2286void GrGpuGL::notifyVertexBufferBind(const GrGLVertexBuffer* buffer) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002287 if (fHWGeometryState.fVertexBuffer != buffer) {
2288 fHWGeometryState.fArrayPtrsDirty = true;
2289 fHWGeometryState.fVertexBuffer = buffer;
2290 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002291}
2292
2293void GrGpuGL::notifyVertexBufferDelete(const GrGLVertexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002294 if (fHWGeometryState.fVertexBuffer == buffer) {
2295 // deleting bound buffer does implied bind to 0
2296 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002297 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00002298 }
2299}
2300
2301void GrGpuGL::notifyIndexBufferBind(const GrGLIndexBuffer* buffer) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002302 fHWGeometryState.fIndexBuffer = buffer;
reed@google.comac10a2d2010-12-22 21:39:39 +00002303}
2304
2305void GrGpuGL::notifyIndexBufferDelete(const GrGLIndexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002306 if (fHWGeometryState.fIndexBuffer == buffer) {
2307 // deleting bound buffer does implied bind to 0
2308 fHWGeometryState.fIndexBuffer = NULL;
2309 }
2310}
2311
reed@google.comac10a2d2010-12-22 21:39:39 +00002312void GrGpuGL::notifyRenderTargetDelete(GrRenderTarget* renderTarget) {
2313 GrAssert(NULL != renderTarget);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002314 GrDrawState* drawState = this->drawState();
2315 if (drawState->getRenderTarget() == renderTarget) {
2316 drawState->setRenderTarget(NULL);
reed@google.comac10a2d2010-12-22 21:39:39 +00002317 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002318 if (fHWDrawState.getRenderTarget() == renderTarget) {
2319 fHWDrawState.setRenderTarget(NULL);
reed@google.comac10a2d2010-12-22 21:39:39 +00002320 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002321}
2322
2323void GrGpuGL::notifyTextureDelete(GrGLTexture* texture) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002324 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002325 GrDrawState* drawState = this->drawState();
2326 if (drawState->getTexture(s) == texture) {
2327 fCurrDrawState.setTexture(s, NULL);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002328 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002329 if (fHWDrawState.getTexture(s) == texture) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002330 // deleting bound texture does implied bind to 0
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002331 fHWDrawState.setTexture(s, NULL);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002332 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002333 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002334}
2335
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002336bool GrGpuGL::configToGLFormats(GrPixelConfig config,
2337 bool getSizedInternalFormat,
2338 GrGLenum* internalFormat,
2339 GrGLenum* externalFormat,
2340 GrGLenum* externalType) {
2341 GrGLenum dontCare;
2342 if (NULL == internalFormat) {
2343 internalFormat = &dontCare;
2344 }
2345 if (NULL == externalFormat) {
2346 externalFormat = &dontCare;
2347 }
2348 if (NULL == externalType) {
2349 externalType = &dontCare;
2350 }
2351
reed@google.comac10a2d2010-12-22 21:39:39 +00002352 switch (config) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002353 case kRGBA_8888_PM_GrPixelConfig:
2354 case kRGBA_8888_UPM_GrPixelConfig:
bsalomon@google.comc4364992011-11-07 15:54:49 +00002355 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002356 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002357 if (getSizedInternalFormat) {
2358 *internalFormat = GR_GL_RGBA8;
2359 } else {
2360 *internalFormat = GR_GL_RGBA;
2361 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002362 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.comc4364992011-11-07 15:54:49 +00002363 break;
2364 case kBGRA_8888_PM_GrPixelConfig:
2365 case kBGRA_8888_UPM_GrPixelConfig:
bsalomon@google.com7107fa72011-11-10 14:54:14 +00002366 if (!fGLCaps.fBGRAFormatSupport) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002367 return false;
2368 }
bsalomon@google.com7107fa72011-11-10 14:54:14 +00002369 if (fGLCaps.fBGRAIsInternalFormat) {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002370 if (getSizedInternalFormat) {
2371 *internalFormat = GR_GL_BGRA8;
2372 } else {
2373 *internalFormat = GR_GL_BGRA;
2374 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002375 } else {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002376 if (getSizedInternalFormat) {
2377 *internalFormat = GR_GL_RGBA8;
2378 } else {
2379 *internalFormat = GR_GL_RGBA;
2380 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002381 }
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002382 *externalFormat = GR_GL_BGRA;
bsalomon@google.com6f379512011-11-16 20:36:03 +00002383 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002384 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002385 case kRGB_565_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002386 *internalFormat = GR_GL_RGB;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002387 *externalFormat = GR_GL_RGB;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002388 if (getSizedInternalFormat) {
2389 if (this->glBinding() == kDesktop_GrGLBinding) {
2390 return false;
2391 } else {
2392 *internalFormat = GR_GL_RGB565;
2393 }
2394 } else {
2395 *internalFormat = GR_GL_RGB;
2396 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002397 *externalType = GR_GL_UNSIGNED_SHORT_5_6_5;
reed@google.comac10a2d2010-12-22 21:39:39 +00002398 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002399 case kRGBA_4444_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002400 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002401 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002402 if (getSizedInternalFormat) {
2403 *internalFormat = GR_GL_RGBA4;
2404 } else {
2405 *internalFormat = GR_GL_RGBA;
2406 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002407 *externalType = GR_GL_UNSIGNED_SHORT_4_4_4_4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002408 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002409 case kIndex_8_GrPixelConfig:
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002410 if (this->getCaps().f8BitPaletteSupport) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002411 *internalFormat = GR_GL_PALETTE8_RGBA8;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002412 // glCompressedTexImage doesn't take external params
2413 *externalFormat = GR_GL_PALETTE8_RGBA8;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002414 // no sized/unsized internal format distinction here
2415 *internalFormat = GR_GL_PALETTE8_RGBA8;
2416 // unused with CompressedTexImage
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002417 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002418 } else {
2419 return false;
2420 }
2421 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002422 case kAlpha_8_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002423 *internalFormat = GR_GL_ALPHA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002424 *externalFormat = GR_GL_ALPHA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002425 if (getSizedInternalFormat) {
2426 *internalFormat = GR_GL_ALPHA8;
2427 } else {
2428 *internalFormat = GR_GL_ALPHA;
2429 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002430 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002431 break;
2432 default:
2433 return false;
2434 }
2435 return true;
2436}
2437
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002438void GrGpuGL::setTextureUnit(int unit) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002439 GrAssert(unit >= 0 && unit < GrDrawState::kNumStages);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002440 if (fActiveTextureUnitIdx != unit) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002441 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002442 fActiveTextureUnitIdx = unit;
2443 }
2444}
bsalomon@google.com316f99232011-01-13 21:28:12 +00002445
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002446void GrGpuGL::setSpareTextureUnit() {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002447 if (fActiveTextureUnitIdx != (GR_GL_TEXTURE0 + SPARE_TEX_UNIT)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002448 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + SPARE_TEX_UNIT));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002449 fActiveTextureUnitIdx = SPARE_TEX_UNIT;
2450 }
2451}
2452
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00002453void GrGpuGL::resetDirtyFlags() {
2454 Gr_bzero(&fDirtyFlags, sizeof(fDirtyFlags));
2455}
2456
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002457void GrGpuGL::setBuffers(bool indexed,
2458 int* extraVertexOffset,
2459 int* extraIndexOffset) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002460
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002461 GrAssert(NULL != extraVertexOffset);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002462
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002463 const GeometryPoolState& geoPoolState = this->getGeomPoolState();
2464
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002465 GrGLVertexBuffer* vbuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002466 switch (this->getGeomSrc().fVertexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002467 case kBuffer_GeometrySrcType:
2468 *extraVertexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002469 vbuf = (GrGLVertexBuffer*) this->getGeomSrc().fVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002470 break;
2471 case kArray_GeometrySrcType:
2472 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002473 this->finalizeReservedVertices();
2474 *extraVertexOffset = geoPoolState.fPoolStartVertex;
2475 vbuf = (GrGLVertexBuffer*) geoPoolState.fPoolVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002476 break;
2477 default:
2478 vbuf = NULL; // suppress warning
2479 GrCrash("Unknown geometry src type!");
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002480 }
2481
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002482 GrAssert(NULL != vbuf);
2483 GrAssert(!vbuf->isLocked());
2484 if (fHWGeometryState.fVertexBuffer != vbuf) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002485 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, vbuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002486 fHWGeometryState.fArrayPtrsDirty = true;
2487 fHWGeometryState.fVertexBuffer = vbuf;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002488 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002489
2490 if (indexed) {
2491 GrAssert(NULL != extraIndexOffset);
2492
2493 GrGLIndexBuffer* ibuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002494 switch (this->getGeomSrc().fIndexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002495 case kBuffer_GeometrySrcType:
2496 *extraIndexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002497 ibuf = (GrGLIndexBuffer*)this->getGeomSrc().fIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002498 break;
2499 case kArray_GeometrySrcType:
2500 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002501 this->finalizeReservedIndices();
2502 *extraIndexOffset = geoPoolState.fPoolStartIndex;
2503 ibuf = (GrGLIndexBuffer*) geoPoolState.fPoolIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002504 break;
2505 default:
2506 ibuf = NULL; // suppress warning
2507 GrCrash("Unknown geometry src type!");
2508 }
2509
2510 GrAssert(NULL != ibuf);
2511 GrAssert(!ibuf->isLocked());
2512 if (fHWGeometryState.fIndexBuffer != ibuf) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002513 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, ibuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002514 fHWGeometryState.fIndexBuffer = ibuf;
2515 }
2516 }
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002517}
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +00002518
2519int GrGpuGL::getMaxEdges() const {
2520 // FIXME: This is a pessimistic estimate based on how many other things
2521 // want to add uniforms. This should be centralized somewhere.
tomhudson@google.com93813632011-10-27 20:21:16 +00002522 return GR_CT_MIN(fGLCaps.fMaxFragmentUniformVectors - 8,
2523 GrDrawState::kMaxEdges);
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +00002524}
bsalomon@google.comfe676522011-06-17 18:12:21 +00002525
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002526void GrGpuGL::GLCaps::print() const {
2527 for (int i = 0; i < fStencilFormats.count(); ++i) {
2528 GrPrintf("Stencil Format %d, stencil bits: %02d, total bits: %02d\n",
2529 i,
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00002530 fStencilFormats[i].fFormat.fStencilBits,
2531 fStencilFormats[i].fFormat.fTotalBits);
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002532 }
2533
2534 GR_STATIC_ASSERT(0 == kNone_MSFBO);
2535 GR_STATIC_ASSERT(1 == kDesktopARB_MSFBO);
2536 GR_STATIC_ASSERT(2 == kDesktopEXT_MSFBO);
2537 GR_STATIC_ASSERT(3 == kAppleES_MSFBO);
2538 static const char* gMSFBOExtStr[] = {
2539 "None",
2540 "ARB",
2541 "EXT",
2542 "Apple",
2543 };
2544 GrPrintf("MSAA Type: %s\n", gMSFBOExtStr[fMSFBOType]);
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002545 GrPrintf("Max FS Uniform Vectors: %d\n", fMaxFragmentUniformVectors);
2546 GrPrintf("Support RGBA8 Render Buffer: %s\n",
bsalomon@google.com7107fa72011-11-10 14:54:14 +00002547 (fRGBA8RenderbufferSupport ? "YES": "NO"));
bsalomon@google.comc4364992011-11-07 15:54:49 +00002548 GrPrintf("BGRA is an internal format: %s\n",
bsalomon@google.com7107fa72011-11-10 14:54:14 +00002549 (fBGRAIsInternalFormat ? "YES": "NO"));
bsalomon@google.com85b505b2011-11-07 14:56:51 +00002550 GrPrintf("Support texture swizzle: %s\n",
bsalomon@google.com7107fa72011-11-10 14:54:14 +00002551 (fTextureSwizzleSupport ? "YES": "NO"));
2552 GrPrintf("Unpack Row length support: %s\n",
2553 (fUnpackRowLengthSupport ? "YES": "NO"));
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +00002554 GrPrintf("Unpack Flip Y support: %s\n",
2555 (fUnpackFlipYSupport ? "YES": "NO"));
bsalomon@google.com7107fa72011-11-10 14:54:14 +00002556 GrPrintf("Pack Row length support: %s\n",
2557 (fPackRowLengthSupport ? "YES": "NO"));
bsalomon@google.com56d11e02011-11-30 19:59:08 +00002558 GrPrintf("Pack Flip Y support: %s\n",
2559 (fPackFlipYSupport ? "YES": "NO"));
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002560}