blob: a1a5bee5f262087001b8b3a9bfc4535c0bbb9aca [file] [log] [blame]
reed@google.comac10a2d2010-12-22 21:39:39 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
reed@google.comac10a2d2010-12-22 21:39:39 +00006 */
7
epoger@google.comec3ed6a2011-07-28 14:26:00 +00008
reed@google.comac10a2d2010-12-22 21:39:39 +00009#include "GrGpuGL.h"
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000010#include "GrGLStencilBuffer.h"
bsalomon@google.com64aef2b2012-06-11 15:36:13 +000011#include "GrGLPath.h"
bsalomon@google.com6d003d12012-09-11 15:45:20 +000012#include "GrGLShaderBuilder.h"
bsalomon@google.coma3201942012-06-21 19:58:20 +000013#include "GrTemplates.h"
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +000014#include "GrTypes.h"
bsalomon@google.com3582bf92011-06-30 21:32:31 +000015#include "SkTemplates.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000016
caryclark@google.comcf6285b2012-06-06 12:09:01 +000017static const GrGLuint GR_MAX_GLUINT = ~0U;
twiz@google.com0f31ca72011-03-18 17:38:11 +000018static const GrGLint GR_INVAL_GLINT = ~0;
reed@google.comac10a2d2010-12-22 21:39:39 +000019
bsalomon@google.com0b77d682011-08-19 13:28:54 +000020#define GL_CALL(X) GR_GL_CALL(this->glInterface(), X)
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +000021#define GL_CALL_RET(RET, X) GR_GL_CALL_RET(this->glInterface(), RET, X)
bsalomon@google.com0b77d682011-08-19 13:28:54 +000022
bsalomon@google.com316f99232011-01-13 21:28:12 +000023// we use a spare texture unit to avoid
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000024// mucking with the state of any of the stages.
tomhudson@google.com93813632011-10-27 20:21:16 +000025static const int SPARE_TEX_UNIT = GrDrawState::kNumStages;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000026
reed@google.comac10a2d2010-12-22 21:39:39 +000027#define SKIP_CACHE_CHECK true
28
bsalomon@google.com4f3c2532012-01-19 16:16:52 +000029#if GR_GL_CHECK_ALLOC_WITH_GET_ERROR
30 #define CLEAR_ERROR_BEFORE_ALLOC(iface) GrGLClearErr(iface)
31 #define GL_ALLOC_CALL(iface, call) GR_GL_CALL_NOERRCHECK(iface, call)
32 #define CHECK_ALLOC_ERROR(iface) GR_GL_GET_ERROR(iface)
rmistry@google.comfbfcd562012-08-23 18:09:54 +000033#else
bsalomon@google.com4f3c2532012-01-19 16:16:52 +000034 #define CLEAR_ERROR_BEFORE_ALLOC(iface)
35 #define GL_ALLOC_CALL(iface, call) GR_GL_CALL(iface, call)
36 #define CHECK_ALLOC_ERROR(iface) GR_GL_NO_ERROR
37#endif
38
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +000039
40///////////////////////////////////////////////////////////////////////////////
41
twiz@google.com0f31ca72011-03-18 17:38:11 +000042static const GrGLenum gXfermodeCoeff2Blend[] = {
43 GR_GL_ZERO,
44 GR_GL_ONE,
45 GR_GL_SRC_COLOR,
46 GR_GL_ONE_MINUS_SRC_COLOR,
47 GR_GL_DST_COLOR,
48 GR_GL_ONE_MINUS_DST_COLOR,
49 GR_GL_SRC_ALPHA,
50 GR_GL_ONE_MINUS_SRC_ALPHA,
51 GR_GL_DST_ALPHA,
52 GR_GL_ONE_MINUS_DST_ALPHA,
53 GR_GL_CONSTANT_COLOR,
54 GR_GL_ONE_MINUS_CONSTANT_COLOR,
55 GR_GL_CONSTANT_ALPHA,
56 GR_GL_ONE_MINUS_CONSTANT_ALPHA,
bsalomon@google.com271cffc2011-05-20 14:13:56 +000057
58 // extended blend coeffs
59 GR_GL_SRC1_COLOR,
60 GR_GL_ONE_MINUS_SRC1_COLOR,
61 GR_GL_SRC1_ALPHA,
62 GR_GL_ONE_MINUS_SRC1_ALPHA,
reed@google.comac10a2d2010-12-22 21:39:39 +000063};
64
bsalomon@google.com271cffc2011-05-20 14:13:56 +000065bool GrGpuGL::BlendCoeffReferencesConstant(GrBlendCoeff coeff) {
bsalomon@google.com080773c2011-03-15 19:09:25 +000066 static const bool gCoeffReferencesBlendConst[] = {
67 false,
68 false,
69 false,
70 false,
71 false,
72 false,
73 false,
74 false,
75 false,
76 false,
77 true,
78 true,
79 true,
80 true,
bsalomon@google.com271cffc2011-05-20 14:13:56 +000081
82 // extended blend coeffs
83 false,
84 false,
85 false,
86 false,
bsalomon@google.com080773c2011-03-15 19:09:25 +000087 };
88 return gCoeffReferencesBlendConst[coeff];
bsalomon@google.com47059542012-06-06 20:51:20 +000089 GR_STATIC_ASSERT(kTotalGrBlendCoeffCount ==
90 GR_ARRAY_COUNT(gCoeffReferencesBlendConst));
bsalomon@google.com271cffc2011-05-20 14:13:56 +000091
bsalomon@google.com47059542012-06-06 20:51:20 +000092 GR_STATIC_ASSERT(0 == kZero_GrBlendCoeff);
93 GR_STATIC_ASSERT(1 == kOne_GrBlendCoeff);
94 GR_STATIC_ASSERT(2 == kSC_GrBlendCoeff);
95 GR_STATIC_ASSERT(3 == kISC_GrBlendCoeff);
96 GR_STATIC_ASSERT(4 == kDC_GrBlendCoeff);
97 GR_STATIC_ASSERT(5 == kIDC_GrBlendCoeff);
98 GR_STATIC_ASSERT(6 == kSA_GrBlendCoeff);
99 GR_STATIC_ASSERT(7 == kISA_GrBlendCoeff);
100 GR_STATIC_ASSERT(8 == kDA_GrBlendCoeff);
101 GR_STATIC_ASSERT(9 == kIDA_GrBlendCoeff);
102 GR_STATIC_ASSERT(10 == kConstC_GrBlendCoeff);
103 GR_STATIC_ASSERT(11 == kIConstC_GrBlendCoeff);
104 GR_STATIC_ASSERT(12 == kConstA_GrBlendCoeff);
105 GR_STATIC_ASSERT(13 == kIConstA_GrBlendCoeff);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000106
bsalomon@google.com47059542012-06-06 20:51:20 +0000107 GR_STATIC_ASSERT(14 == kS2C_GrBlendCoeff);
108 GR_STATIC_ASSERT(15 == kIS2C_GrBlendCoeff);
109 GR_STATIC_ASSERT(16 == kS2A_GrBlendCoeff);
110 GR_STATIC_ASSERT(17 == kIS2A_GrBlendCoeff);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000111
112 // assertion for gXfermodeCoeff2Blend have to be in GrGpu scope
bsalomon@google.com47059542012-06-06 20:51:20 +0000113 GR_STATIC_ASSERT(kTotalGrBlendCoeffCount ==
114 GR_ARRAY_COUNT(gXfermodeCoeff2Blend));
bsalomon@google.com080773c2011-03-15 19:09:25 +0000115}
116
reed@google.comac10a2d2010-12-22 21:39:39 +0000117///////////////////////////////////////////////////////////////////////////////
118
rileya@google.come38160c2012-07-03 18:03:04 +0000119static bool gPrintStartupSpew;
bsalomon@google.com42ab7ea2011-01-19 17:19:40 +0000120
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000121static bool fbo_test(const GrGLInterface* gl, int w, int h) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000122
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000123 GR_GL_CALL(gl, ActiveTexture(GR_GL_TEXTURE0 + SPARE_TEX_UNIT));
bsalomon@google.com316f99232011-01-13 21:28:12 +0000124
twiz@google.com0f31ca72011-03-18 17:38:11 +0000125 GrGLuint testFBO;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000126 GR_GL_CALL(gl, GenFramebuffers(1, &testFBO));
127 GR_GL_CALL(gl, BindFramebuffer(GR_GL_FRAMEBUFFER, testFBO));
twiz@google.com0f31ca72011-03-18 17:38:11 +0000128 GrGLuint testRTTex;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000129 GR_GL_CALL(gl, GenTextures(1, &testRTTex));
130 GR_GL_CALL(gl, BindTexture(GR_GL_TEXTURE_2D, testRTTex));
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +0000131 // some implementations require texture to be mip-map complete before
132 // FBO with level 0 bound as color attachment will be framebuffer complete.
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000133 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000134 GR_GL_TEXTURE_MIN_FILTER,
135 GR_GL_NEAREST));
136 GR_GL_CALL(gl, TexImage2D(GR_GL_TEXTURE_2D, 0, GR_GL_RGBA, w, h,
137 0, GR_GL_RGBA, GR_GL_UNSIGNED_BYTE, NULL));
138 GR_GL_CALL(gl, BindTexture(GR_GL_TEXTURE_2D, 0));
139 GR_GL_CALL(gl, FramebufferTexture2D(GR_GL_FRAMEBUFFER,
140 GR_GL_COLOR_ATTACHMENT0,
141 GR_GL_TEXTURE_2D, testRTTex, 0));
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000142 GrGLenum status;
143 GR_GL_CALL_RET(gl, status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000144 GR_GL_CALL(gl, DeleteFramebuffers(1, &testFBO));
145 GR_GL_CALL(gl, DeleteTextures(1, &testRTTex));
bsalomon@google.com316f99232011-01-13 21:28:12 +0000146
bsalomon@google.comc312bf92011-03-21 21:10:33 +0000147 return status == GR_GL_FRAMEBUFFER_COMPLETE;
reed@google.comac10a2d2010-12-22 21:39:39 +0000148}
149
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000150GrGpuGL::GrGpuGL(const GrGLContextInfo& ctxInfo) : fGLContextInfo(ctxInfo) {
151
152 GrAssert(ctxInfo.isInitialized());
tomhudson@google.com747bf292011-06-14 18:16:52 +0000153
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000154 fillInConfigRenderableTable();
155
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000156 fPrintedCaps = false;
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000157
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000158 GrGLClearErr(fGLContextInfo.interface());
reed@google.comac10a2d2010-12-22 21:39:39 +0000159
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000160 if (gPrintStartupSpew) {
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000161 const GrGLubyte* ext;
162 GL_CALL_RET(ext, GetString(GR_GL_EXTENSIONS));
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000163 const GrGLubyte* vendor;
164 const GrGLubyte* renderer;
165 const GrGLubyte* version;
166 GL_CALL_RET(vendor, GetString(GR_GL_VENDOR));
167 GL_CALL_RET(renderer, GetString(GR_GL_RENDERER));
168 GL_CALL_RET(version, GetString(GR_GL_VERSION));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000169 GrPrintf("------------------------- create GrGpuGL %p --------------\n",
170 this);
171 GrPrintf("------ VENDOR %s\n", vendor);
172 GrPrintf("------ RENDERER %s\n", renderer);
173 GrPrintf("------ VERSION %s\n", version);
174 GrPrintf("------ EXTENSIONS\n %s \n", ext);
175 }
176
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000177 this->initCaps();
tomhudson@google.com747bf292011-06-14 18:16:52 +0000178
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000179 fProgramCache = SkNEW_ARGS(ProgramCache, (this->glContextInfo()));
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000180
bsalomon@google.comfe676522011-06-17 18:12:21 +0000181 fLastSuccessfulStencilFmtIdx = 0;
caryclark@google.comcf6285b2012-06-06 12:09:01 +0000182 if (false) { // avoid bit rot, suppress warning
183 fbo_test(this->glInterface(), 0, 0);
184 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000185}
186
187GrGpuGL::~GrGpuGL() {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000188 if (0 != fHWProgramID) {
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000189 // detach the current program so there is no confusion on OpenGL's part
190 // that we want it to be deleted
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000191 GrAssert(fHWProgramID == fCurrentProgram->fProgramID);
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000192 GL_CALL(UseProgram(0));
193 }
194
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000195 delete fProgramCache;
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000196
bsalomon@google.com4a018bb2011-10-28 19:50:21 +0000197 // This must be called by before the GrDrawTarget destructor
198 this->releaseGeometry();
bsalomon@google.com15b11df2011-09-16 21:18:29 +0000199 // This subclass must do this before the base class destructor runs
200 // since we will unref the GrGLInterface.
201 this->releaseResources();
reed@google.comac10a2d2010-12-22 21:39:39 +0000202}
203
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000204///////////////////////////////////////////////////////////////////////////////
205
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000206void GrGpuGL::initCaps() {
207 GrGLint maxTextureUnits;
208 // check FS and fixed-function texture unit limits
209 // we only use textures in the fragment stage currently.
210 // checks are > to make sure we have a spare unit.
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000211 const GrGLInterface* gl = this->glInterface();
212 GR_GL_GetIntegerv(gl, GR_GL_MAX_TEXTURE_IMAGE_UNITS, &maxTextureUnits);
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000213 GrAssert(maxTextureUnits > GrDrawState::kNumStages);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000214
bsalomon@google.comf6601872012-08-28 21:11:35 +0000215 CapsInternals* caps = this->capsInternals();
216
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000217 GrGLint numFormats;
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000218 GR_GL_GetIntegerv(gl, GR_GL_NUM_COMPRESSED_TEXTURE_FORMATS, &numFormats);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000219 SkAutoSTMalloc<10, GrGLint> formats(numFormats);
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000220 GR_GL_GetIntegerv(gl, GR_GL_COMPRESSED_TEXTURE_FORMATS, formats);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000221 for (int i = 0; i < numFormats; ++i) {
222 if (formats[i] == GR_GL_PALETTE8_RGBA8) {
bsalomon@google.comf6601872012-08-28 21:11:35 +0000223 caps->f8BitPaletteSupport = true;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000224 break;
225 }
226 }
227
228 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000229 // we could also look for GL_ATI_separate_stencil extension or
230 // GL_EXT_stencil_two_side but they use different function signatures
231 // than GL2.0+ (and than each other).
bsalomon@google.comf6601872012-08-28 21:11:35 +0000232 caps->fTwoSidedStencilSupport = (this->glVersion() >= GR_GL_VER(2,0));
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000233 // supported on GL 1.4 and higher or by extension
bsalomon@google.comf6601872012-08-28 21:11:35 +0000234 caps->fStencilWrapOpsSupport = (this->glVersion() >= GR_GL_VER(1,4)) ||
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000235 this->hasExtension("GL_EXT_stencil_wrap");
236 } else {
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000237 // ES 2 has two sided stencil and stencil wrap
bsalomon@google.comf6601872012-08-28 21:11:35 +0000238 caps->fTwoSidedStencilSupport = true;
239 caps->fStencilWrapOpsSupport = true;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000240 }
241
242 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.comf6601872012-08-28 21:11:35 +0000243 caps->fBufferLockSupport = true; // we require VBO support and the desktop VBO
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000244 // extension includes glMapBuffer.
245 } else {
bsalomon@google.comf6601872012-08-28 21:11:35 +0000246 caps->fBufferLockSupport = this->hasExtension("GL_OES_mapbuffer");
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000247 }
248
249 if (kDesktop_GrGLBinding == this->glBinding()) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000250 if (this->glVersion() >= GR_GL_VER(2,0) ||
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000251 this->hasExtension("GL_ARB_texture_non_power_of_two")) {
bsalomon@google.comf6601872012-08-28 21:11:35 +0000252 caps->fNPOTTextureTileSupport = true;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000253 } else {
bsalomon@google.comf6601872012-08-28 21:11:35 +0000254 caps->fNPOTTextureTileSupport = false;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000255 }
256 } else {
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000257 // Unextended ES2 supports NPOT textures with clamp_to_edge and non-mip filters only
bsalomon@google.comf6601872012-08-28 21:11:35 +0000258 caps->fNPOTTextureTileSupport = this->hasExtension("GL_OES_texture_npot");
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000259 }
260
bsalomon@google.comf6601872012-08-28 21:11:35 +0000261 caps->fHWAALineSupport = (kDesktop_GrGLBinding == this->glBinding());
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000262
bsalomon@google.comf6601872012-08-28 21:11:35 +0000263 GR_GL_GetIntegerv(gl, GR_GL_MAX_TEXTURE_SIZE, &caps->fMaxTextureSize);
264 GR_GL_GetIntegerv(gl, GR_GL_MAX_RENDERBUFFER_SIZE, &caps->fMaxRenderTargetSize);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000265 // Our render targets are always created with textures as the color
266 // attachment, hence this min:
bsalomon@google.comf6601872012-08-28 21:11:35 +0000267 caps->fMaxRenderTargetSize = GrMin(caps->fMaxTextureSize, caps->fMaxRenderTargetSize);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000268
bsalomon@google.comf6601872012-08-28 21:11:35 +0000269 caps->fFSAASupport = GrGLCaps::kNone_MSFBOType != this->glCaps().msFBOType();
270 caps->fPathStencilingSupport = GR_GL_USE_NV_PATH_RENDERING &&
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000271 this->hasExtension("GL_NV_path_rendering");
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000272
273 // Enable supported shader-related caps
274 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.comf6601872012-08-28 21:11:35 +0000275 caps->fDualSourceBlendingSupport =
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000276 this->glVersion() >= GR_GL_VER(3,3) ||
277 this->hasExtension("GL_ARB_blend_func_extended");
bsalomon@google.comf6601872012-08-28 21:11:35 +0000278 caps->fShaderDerivativeSupport = true;
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000279 // we don't support GL_ARB_geometry_shader4, just GL 3.2+ GS
bsalomon@google.comf6601872012-08-28 21:11:35 +0000280 caps->fGeometryShaderSupport =
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000281 this->glVersion() >= GR_GL_VER(3,2) &&
282 this->glslGeneration() >= k150_GrGLSLGeneration;
283 } else {
bsalomon@google.comf6601872012-08-28 21:11:35 +0000284 caps->fShaderDerivativeSupport =
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000285 this->hasExtension("GL_OES_standard_derivatives");
286 }
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000287}
288
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000289void GrGpuGL::fillInConfigRenderableTable() {
290
291 // OpenGL < 3.0
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000292 // no support for render targets unless the GL_ARB_framebuffer_object
293 // extension is supported (in which case we get ALPHA, RED, RG, RGB,
294 // RGBA (ALPHA8, RGBA4, RGBA8) for OpenGL > 1.1). Note that we
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000295 // probably don't get R8 in this case.
296
297 // OpenGL 3.0
298 // base color renderable: ALPHA, RED, RG, RGB, and RGBA
299 // sized derivatives: ALPHA8, R8, RGBA4, RGBA8
300
301 // >= OpenGL 3.1
302 // base color renderable: RED, RG, RGB, and RGBA
303 // sized derivatives: R8, RGBA4, RGBA8
304 // if the GL_ARB_compatibility extension is supported then we get back
305 // support for GL_ALPHA and ALPHA8
306
307 // GL_EXT_bgra adds BGRA render targets to any version
308
309 // ES 2.0
310 // color renderable: RGBA4, RGB5_A1, RGB565
311 // GL_EXT_texture_rg adds support for R8 as a color render target
312 // GL_OES_rgb8_rgba8 and/or GL_ARM_rgba8 adds support for RGBA8
313 // GL_EXT_texture_format_BGRA8888 and/or GL_APPLE_texture_format_BGRA8888
314 // added BGRA support
315
316 if (kDesktop_GrGLBinding == this->glBinding()) {
317 // Post 3.0 we will get R8
318 // Prior to 3.0 we will get ALPHA8 (with GL_ARB_framebuffer_object)
319 if (this->glVersion() >= GR_GL_VER(3,0) ||
320 this->hasExtension("GL_ARB_framebuffer_object")) {
321 fConfigRenderSupport[kAlpha_8_GrPixelConfig] = true;
322 }
323 } else {
324 // On ES we can only hope for R8
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000325 fConfigRenderSupport[kAlpha_8_GrPixelConfig] =
robertphillips@google.com443e5a52012-04-30 20:01:21 +0000326 this->glCaps().textureRedSupport();
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000327 }
328
329 if (kDesktop_GrGLBinding != this->glBinding()) {
330 // only available in ES
331 fConfigRenderSupport[kRGB_565_GrPixelConfig] = true;
332 }
333
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000334 // Pre 3.0, Ganesh relies on either GL_ARB_framebuffer_object or
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000335 // GL_EXT_framebuffer_object for FBO support. Both of these
336 // allow RGBA4 render targets so this is always supported.
337 fConfigRenderSupport[kRGBA_4444_GrPixelConfig] = true;
338
339 if (this->glCaps().rgba8RenderbufferSupport()) {
bsalomon@google.com0342a852012-08-20 19:22:38 +0000340 fConfigRenderSupport[kRGBA_8888_GrPixelConfig] = true;
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000341 }
342
343 if (this->glCaps().bgraFormatSupport()) {
bsalomon@google.com0342a852012-08-20 19:22:38 +0000344 fConfigRenderSupport[kBGRA_8888_GrPixelConfig] = true;
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000345 }
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000346}
347
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000348GrPixelConfig GrGpuGL::preferredReadPixelsConfig(GrPixelConfig config) const {
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000349 if (GR_GL_RGBA_8888_PIXEL_OPS_SLOW && GrPixelConfigIsRGBA8888(config)) {
350 return GrPixelConfigSwapRAndB(config);
351 } else {
352 return config;
353 }
354}
355
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000356GrPixelConfig GrGpuGL::preferredWritePixelsConfig(GrPixelConfig config) const {
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000357 if (GR_GL_RGBA_8888_PIXEL_OPS_SLOW && GrPixelConfigIsRGBA8888(config)) {
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000358 return GrPixelConfigSwapRAndB(config);
359 } else {
360 return config;
361 }
362}
363
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000364bool GrGpuGL::fullReadPixelsIsFasterThanPartial() const {
365 return SkToBool(GR_GL_FULL_READPIXELS_FASTER_THAN_PARTIAL);
366}
367
bsalomon@google.com1bf1c212011-11-05 12:18:58 +0000368void GrGpuGL::onResetContext() {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000369 if (gPrintStartupSpew && !fPrintedCaps) {
370 fPrintedCaps = true;
371 this->getCaps().print();
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000372 this->glCaps().print();
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000373 }
374
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000375 // we don't use the zb at all
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000376 GL_CALL(Disable(GR_GL_DEPTH_TEST));
377 GL_CALL(DepthMask(GR_GL_FALSE));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000378
bsalomon@google.com978c8c62012-05-21 14:45:49 +0000379 fHWDrawFace = GrDrawState::kInvalid_DrawFace;
380 fHWDitherEnabled = kUnknown_TriState;
reed@google.comac10a2d2010-12-22 21:39:39 +0000381
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000382 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com469d0dd2012-05-21 20:14:29 +0000383 // Desktop-only state that we never change
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000384 GL_CALL(Disable(GR_GL_POINT_SMOOTH));
bsalomon@google.com469d0dd2012-05-21 20:14:29 +0000385 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
386 GL_CALL(Disable(GR_GL_POLYGON_SMOOTH));
387 GL_CALL(Disable(GR_GL_POLYGON_STIPPLE));
388 GL_CALL(Disable(GR_GL_COLOR_LOGIC_OP));
bsalomon@google.come76b7cc2012-06-18 12:47:06 +0000389 if (this->glCaps().imagingSupport()) {
390 GL_CALL(Disable(GR_GL_COLOR_TABLE));
391 }
bsalomon@google.com469d0dd2012-05-21 20:14:29 +0000392 GL_CALL(Disable(GR_GL_INDEX_LOGIC_OP));
393 GL_CALL(Disable(GR_GL_POLYGON_OFFSET_FILL));
394 // Since ES doesn't support glPointSize at all we always use the VS to
395 // set the point size
396 GL_CALL(Enable(GR_GL_VERTEX_PROGRAM_POINT_SIZE));
397
398 // We should set glPolygonMode(FRONT_AND_BACK,FILL) here, too. It isn't
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000399 // currently part of our gl interface. There are probably others as
bsalomon@google.com469d0dd2012-05-21 20:14:29 +0000400 // well.
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000401 }
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +0000402 fHWAAState.invalidate();
bsalomon@google.com978c8c62012-05-21 14:45:49 +0000403 fHWWriteToColor = kUnknown_TriState;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000404
reed@google.comac10a2d2010-12-22 21:39:39 +0000405 // we only ever use lines in hairline mode
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000406 GL_CALL(LineWidth(1));
reed@google.comac10a2d2010-12-22 21:39:39 +0000407
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000408 // invalid
bsalomon@google.com49209392012-06-05 15:13:46 +0000409 fHWActiveTextureUnitIdx = -1;
reed@google.comac10a2d2010-12-22 21:39:39 +0000410
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +0000411 fHWBlendState.invalidate();
bsalomon@google.com080773c2011-03-15 19:09:25 +0000412
tomhudson@google.com93813632011-10-27 20:21:16 +0000413 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.comc811ea32012-05-21 15:33:09 +0000414 fHWBoundTextures[s] = NULL;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000415 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000416
bsalomon@google.coma3201942012-06-21 19:58:20 +0000417 fHWScissorSettings.invalidate();
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000418
bsalomon@google.coma3201942012-06-21 19:58:20 +0000419 fHWViewport.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000420
bsalomon@google.com457b8a32012-05-21 21:19:58 +0000421 fHWStencilSettings.invalidate();
bsalomon@google.coma3201942012-06-21 19:58:20 +0000422 fHWStencilTestEnabled = kUnknown_TriState;
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000423
reed@google.comac10a2d2010-12-22 21:39:39 +0000424 fHWGeometryState.fIndexBuffer = NULL;
425 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.coma3201942012-06-21 19:58:20 +0000426
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +0000427 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000428
bsalomon@google.comc811ea32012-05-21 15:33:09 +0000429 fHWBoundRenderTarget = NULL;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000430
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000431 fHWPathMatrixState.invalidate();
bsalomon@google.comf6601872012-08-28 21:11:35 +0000432 if (fCaps.pathStencilingSupport()) {
bsalomon@google.com05a718c2012-06-29 14:01:53 +0000433 // we don't use the model view matrix.
434 GL_CALL(MatrixMode(GR_GL_MODELVIEW));
435 GL_CALL(LoadIdentity());
436 }
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000437
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000438 // we assume these values
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000439 if (this->glCaps().unpackRowLengthSupport()) {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000440 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
441 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000442 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000443 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
444 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000445 if (this->glCaps().unpackFlipYSupport()) {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000446 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
447 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000448 if (this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000449 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, GR_GL_FALSE));
450 }
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000451
caryclark@google.comcf6285b2012-06-06 12:09:01 +0000452 fHWGeometryState.fVertexOffset = ~0U;
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000453
454 // Third party GL code may have left vertex attributes enabled. Some GL
455 // implementations (osmesa) may read vetex attributes that are not required
456 // by the current shader. Therefore, we have to ensure that only the
457 // attributes we require for the current draw are enabled or we may cause an
458 // invalid read.
459
460 // Disable all vertex layout bits so that next flush will assume all
461 // optional vertex attributes are disabled.
462 fHWGeometryState.fVertexLayout = 0;
463
464 // We always use the this attribute and assume it is always enabled.
465 int posAttrIdx = GrGLProgram::PositionAttributeIdx();
466 GL_CALL(EnableVertexAttribArray(posAttrIdx));
467 // Disable all other vertex attributes.
bsalomon@google.com60da4172012-06-01 19:25:00 +0000468 for (int va = 0; va < this->glCaps().maxVertexAttributes(); ++va) {
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000469 if (va != posAttrIdx) {
470 GL_CALL(DisableVertexAttribArray(va));
471 }
472 }
473
474 fHWProgramID = 0;
475 fHWConstAttribColor = GrColor_ILLEGAL;
476 fHWConstAttribCoverage = GrColor_ILLEGAL;
reed@google.comac10a2d2010-12-22 21:39:39 +0000477}
478
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000479GrTexture* GrGpuGL::onWrapBackendTexture(const GrBackendTextureDesc& desc) {
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000480 if (!this->configToGLFormats(desc.fConfig, false, NULL, NULL, NULL)) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000481 return NULL;
482 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000483
robertphillips@google.comb72e5d32012-10-30 15:18:10 +0000484 if (0 == desc.fTextureHandle) {
485 return NULL;
486 }
487
senorblanco@chromium.orgef5dbe12013-01-28 16:42:38 +0000488 // FIXME: add support for TopLeft RT's by flipping all draws.
489 if (desc.fFlags & kRenderTarget_GrBackendTextureFlag &&
490 kBottomLeft_GrSurfaceOrigin != desc.fOrigin) {
491 return NULL;
492 }
493
robertphillips@google.comb72e5d32012-10-30 15:18:10 +0000494 int maxSize = this->getCaps().maxTextureSize();
495 if (desc.fWidth > maxSize || desc.fHeight > maxSize) {
496 return NULL;
497 }
498
499 GrGLTexture::Desc glTexDesc;
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000500 // next line relies on GrBackendTextureDesc's flags matching GrTexture's
robertphillips@google.com32716282012-06-04 12:48:45 +0000501 glTexDesc.fFlags = (GrTextureFlags) desc.fFlags;
bsalomon@google.com99621082011-11-15 16:47:16 +0000502 glTexDesc.fWidth = desc.fWidth;
503 glTexDesc.fHeight = desc.fHeight;
bsalomon@google.come269f212011-11-07 13:29:52 +0000504 glTexDesc.fConfig = desc.fConfig;
robertphillips@google.com32716282012-06-04 12:48:45 +0000505 glTexDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.come269f212011-11-07 13:29:52 +0000506 glTexDesc.fTextureID = static_cast<GrGLuint>(desc.fTextureHandle);
bsalomon@google.com72830222013-01-23 20:25:22 +0000507 glTexDesc.fIsWrapped = true;
senorblanco@chromium.orgef5dbe12013-01-28 16:42:38 +0000508 glTexDesc.fOrigin = desc.fOrigin;
bsalomon@google.come269f212011-11-07 13:29:52 +0000509
510 GrGLTexture* texture = NULL;
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000511 if (desc.fFlags & kRenderTarget_GrBackendTextureFlag) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000512 GrGLRenderTarget::Desc glRTDesc;
513 glRTDesc.fRTFBOID = 0;
514 glRTDesc.fTexFBOID = 0;
515 glRTDesc.fMSColorRenderbufferID = 0;
bsalomon@google.come269f212011-11-07 13:29:52 +0000516 glRTDesc.fConfig = desc.fConfig;
517 glRTDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.com99621082011-11-15 16:47:16 +0000518 if (!this->createRenderTargetObjects(glTexDesc.fWidth,
519 glTexDesc.fHeight,
bsalomon@google.come269f212011-11-07 13:29:52 +0000520 glTexDesc.fTextureID,
521 &glRTDesc)) {
522 return NULL;
523 }
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000524 texture = SkNEW_ARGS(GrGLTexture, (this, glTexDesc, glRTDesc));
bsalomon@google.come269f212011-11-07 13:29:52 +0000525 } else {
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000526 texture = SkNEW_ARGS(GrGLTexture, (this, glTexDesc));
bsalomon@google.come269f212011-11-07 13:29:52 +0000527 }
528 if (NULL == texture) {
529 return NULL;
530 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000531
bsalomon@google.come269f212011-11-07 13:29:52 +0000532 this->setSpareTextureUnit();
533 return texture;
534}
535
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000536GrRenderTarget* GrGpuGL::onWrapBackendRenderTarget(const GrBackendRenderTargetDesc& desc) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000537 GrGLRenderTarget::Desc glDesc;
538 glDesc.fConfig = desc.fConfig;
539 glDesc.fRTFBOID = static_cast<GrGLuint>(desc.fRenderTargetHandle);
540 glDesc.fMSColorRenderbufferID = 0;
541 glDesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
542 glDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.com72830222013-01-23 20:25:22 +0000543 glDesc.fIsWrapped = true;
bsalomon@google.come269f212011-11-07 13:29:52 +0000544 GrGLIRect viewport;
545 viewport.fLeft = 0;
546 viewport.fBottom = 0;
547 viewport.fWidth = desc.fWidth;
548 viewport.fHeight = desc.fHeight;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000549
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000550 GrRenderTarget* tgt = SkNEW_ARGS(GrGLRenderTarget,
551 (this, glDesc, viewport));
bsalomon@google.come269f212011-11-07 13:29:52 +0000552 if (desc.fStencilBits) {
553 GrGLStencilBuffer::Format format;
554 format.fInternalFormat = GrGLStencilBuffer::kUnknownInternalFormat;
555 format.fPacked = false;
556 format.fStencilBits = desc.fStencilBits;
557 format.fTotalBits = desc.fStencilBits;
bsalomon@google.com1f0f1a32013-01-23 21:32:32 +0000558 static const bool kIsSBWrapped = false;
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000559 GrGLStencilBuffer* sb = SkNEW_ARGS(GrGLStencilBuffer,
560 (this,
bsalomon@google.com1f0f1a32013-01-23 21:32:32 +0000561 kIsSBWrapped,
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000562 0,
563 desc.fWidth,
564 desc.fHeight,
565 desc.fSampleCnt,
566 format));
bsalomon@google.come269f212011-11-07 13:29:52 +0000567 tgt->setStencilBuffer(sb);
568 sb->unref();
569 }
570 return tgt;
571}
572
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000573////////////////////////////////////////////////////////////////////////////////
574
bsalomon@google.com6f379512011-11-16 20:36:03 +0000575void GrGpuGL::onWriteTexturePixels(GrTexture* texture,
576 int left, int top, int width, int height,
577 GrPixelConfig config, const void* buffer,
578 size_t rowBytes) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000579 if (NULL == buffer) {
580 return;
581 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000582 GrGLTexture* glTex = static_cast<GrGLTexture*>(texture);
583
bsalomon@google.com6f379512011-11-16 20:36:03 +0000584 this->setSpareTextureUnit();
585 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTex->textureID()));
586 GrGLTexture::Desc desc;
robertphillips@google.com32716282012-06-04 12:48:45 +0000587 desc.fFlags = glTex->desc().fFlags;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000588 desc.fWidth = glTex->width();
589 desc.fHeight = glTex->height();
robertphillips@google.com32716282012-06-04 12:48:45 +0000590 desc.fConfig = glTex->config();
591 desc.fSampleCnt = glTex->desc().fSampleCnt;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000592 desc.fTextureID = glTex->textureID();
bsalomon@google.com2d0bade2012-10-26 19:01:17 +0000593 desc.fOrigin = glTex->origin();
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000594
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000595 this->uploadTexData(desc, false,
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000596 left, top, width, height,
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000597 config, buffer, rowBytes);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000598}
599
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000600namespace {
601bool adjust_pixel_ops_params(int surfaceWidth,
602 int surfaceHeight,
603 size_t bpp,
604 int* left, int* top, int* width, int* height,
605 const void** data,
606 size_t* rowBytes) {
607 if (!*rowBytes) {
608 *rowBytes = *width * bpp;
609 }
610
611 GrIRect subRect = GrIRect::MakeXYWH(*left, *top, *width, *height);
612 GrIRect bounds = GrIRect::MakeWH(surfaceWidth, surfaceHeight);
613
614 if (!subRect.intersect(bounds)) {
615 return false;
616 }
617 *data = reinterpret_cast<const void*>(reinterpret_cast<intptr_t>(*data) +
618 (subRect.fTop - *top) * *rowBytes + (subRect.fLeft - *left) * bpp);
619
620 *left = subRect.fLeft;
621 *top = subRect.fTop;
622 *width = subRect.width();
623 *height = subRect.height();
624 return true;
625}
626}
627
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000628bool GrGpuGL::uploadTexData(const GrGLTexture::Desc& desc,
629 bool isNewTexture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000630 int left, int top, int width, int height,
631 GrPixelConfig dataConfig,
632 const void* data,
633 size_t rowBytes) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000634 GrAssert(NULL != data || isNewTexture);
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000635
636 size_t bpp = GrBytesPerPixel(dataConfig);
637 if (!adjust_pixel_ops_params(desc.fWidth, desc.fHeight, bpp, &left, &top,
638 &width, &height, &data, &rowBytes)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000639 return false;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000640 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000641 size_t trimRowBytes = width * bpp;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000642
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000643 // in case we need a temporary, trimmed copy of the src pixels
644 SkAutoSMalloc<128 * 128> tempStorage;
645
bsalomon@google.com313f0192012-07-10 17:21:02 +0000646 // paletted textures cannot be partially updated
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000647 bool useTexStorage = isNewTexture &&
bsalomon@google.com313f0192012-07-10 17:21:02 +0000648 desc.fConfig != kIndex_8_GrPixelConfig &&
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000649 this->glCaps().texStorageSupport();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000650
bsalomon@google.com313f0192012-07-10 17:21:02 +0000651 if (useTexStorage && kDesktop_GrGLBinding == this->glBinding()) {
652 // 565 is not a sized internal format on desktop GL. So on desktop with
653 // 565 we always use an unsized internal format to let the system pick
654 // the best sized format to convert the 565 data to. Since TexStorage
655 // only allows sized internal formats we will instead use TexImage2D.
656 useTexStorage = desc.fConfig != kRGB_565_GrPixelConfig;
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000657 }
658
659 GrGLenum internalFormat;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000660 GrGLenum externalFormat;
661 GrGLenum externalType;
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000662 // glTexStorage requires sized internal formats on both desktop and ES. ES
663 // glTexImage requires an unsized format.
664 if (!this->configToGLFormats(dataConfig, useTexStorage, &internalFormat,
665 &externalFormat, &externalType)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000666 return false;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000667 }
668
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000669 if (!isNewTexture && GR_GL_PALETTE8_RGBA8 == internalFormat) {
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000670 // paletted textures cannot be updated
671 return false;
672 }
673
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000674 /*
bsalomon@google.com6f379512011-11-16 20:36:03 +0000675 * check whether to allocate a temporary buffer for flipping y or
676 * because our srcData has extra bytes past each row. If so, we need
677 * to trim those off here, since GL ES may not let us specify
678 * GL_UNPACK_ROW_LENGTH.
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000679 */
bsalomon@google.com6f379512011-11-16 20:36:03 +0000680 bool restoreGLRowLength = false;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000681 bool swFlipY = false;
682 bool glFlipY = false;
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000683 if (NULL != data) {
senorblanco@chromium.orgef5dbe12013-01-28 16:42:38 +0000684 if (kBottomLeft_GrSurfaceOrigin == desc.fOrigin) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000685 if (this->glCaps().unpackFlipYSupport()) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000686 glFlipY = true;
687 } else {
688 swFlipY = true;
689 }
690 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000691 if (this->glCaps().unpackRowLengthSupport() && !swFlipY) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000692 // can't use this for flipping, only non-neg values allowed. :(
693 if (rowBytes != trimRowBytes) {
694 GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
695 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength));
696 restoreGLRowLength = true;
697 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000698 } else {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000699 if (trimRowBytes != rowBytes || swFlipY) {
700 // copy data into our new storage, skipping the trailing bytes
701 size_t trimSize = height * trimRowBytes;
702 const char* src = (const char*)data;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000703 if (swFlipY) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000704 src += (height - 1) * rowBytes;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000705 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000706 char* dst = (char*)tempStorage.reset(trimSize);
707 for (int y = 0; y < height; y++) {
708 memcpy(dst, src, trimRowBytes);
709 if (swFlipY) {
710 src -= rowBytes;
711 } else {
712 src += rowBytes;
713 }
714 dst += trimRowBytes;
715 }
716 // now point data to our copied version
717 data = tempStorage.get();
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000718 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000719 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000720 if (glFlipY) {
721 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_TRUE));
722 }
723 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, static_cast<GrGLint>(bpp)));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000724 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000725 bool succeeded = true;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000726 if (isNewTexture &&
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000727 0 == left && 0 == top &&
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000728 desc.fWidth == width && desc.fHeight == height) {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000729 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000730 if (useTexStorage) {
731 // We never resize or change formats of textures. We don't use
732 // mipmaps currently.
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000733 GL_ALLOC_CALL(this->glInterface(),
734 TexStorage2D(GR_GL_TEXTURE_2D,
735 1, // levels
736 internalFormat,
737 desc.fWidth, desc.fHeight));
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000738 } else {
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000739 if (GR_GL_PALETTE8_RGBA8 == internalFormat) {
740 GrGLsizei imageSize = desc.fWidth * desc.fHeight +
741 kGrColorTableSize;
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000742 GL_ALLOC_CALL(this->glInterface(),
743 CompressedTexImage2D(GR_GL_TEXTURE_2D,
744 0, // level
745 internalFormat,
746 desc.fWidth, desc.fHeight,
747 0, // border
748 imageSize,
749 data));
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000750 } else {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000751 GL_ALLOC_CALL(this->glInterface(),
752 TexImage2D(GR_GL_TEXTURE_2D,
753 0, // level
754 internalFormat,
755 desc.fWidth, desc.fHeight,
756 0, // border
757 externalFormat, externalType,
758 data));
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000759 }
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000760 }
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000761 GrGLenum error = CHECK_ALLOC_ERROR(this->glInterface());
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000762 if (error != GR_GL_NO_ERROR) {
763 succeeded = false;
764 } else {
765 // if we have data and we used TexStorage to create the texture, we
766 // now upload with TexSubImage.
767 if (NULL != data && useTexStorage) {
768 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D,
769 0, // level
770 left, top,
771 width, height,
772 externalFormat, externalType,
773 data));
774 }
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000775 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000776 } else {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000777 if (swFlipY || glFlipY) {
bsalomon@google.com6f379512011-11-16 20:36:03 +0000778 top = desc.fHeight - (top + height);
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000779 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000780 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D,
781 0, // level
782 left, top,
783 width, height,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000784 externalFormat, externalType, data));
785 }
786
787 if (restoreGLRowLength) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000788 GrAssert(this->glCaps().unpackRowLengthSupport());
bsalomon@google.com6f379512011-11-16 20:36:03 +0000789 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000790 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000791 if (glFlipY) {
792 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
793 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000794 return succeeded;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000795}
796
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000797namespace {
798bool renderbuffer_storage_msaa(GrGLContextInfo& ctxInfo,
799 int sampleCount,
800 GrGLenum format,
801 int width, int height) {
802 CLEAR_ERROR_BEFORE_ALLOC(ctxInfo.interface());
803 GrAssert(GrGLCaps::kNone_MSFBOType != ctxInfo.caps().msFBOType());
804 bool created = false;
805 if (GrGLCaps::kNVDesktop_CoverageAAType ==
806 ctxInfo.caps().coverageAAType()) {
807 const GrGLCaps::MSAACoverageMode& mode =
808 ctxInfo.caps().getMSAACoverageMode(sampleCount);
809 GL_ALLOC_CALL(ctxInfo.interface(),
810 RenderbufferStorageMultisampleCoverage(GR_GL_RENDERBUFFER,
811 mode.fCoverageSampleCnt,
812 mode.fColorSampleCnt,
813 format,
814 width, height));
815 created = (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctxInfo.interface()));
816 }
817 if (!created) {
bsalomon@google.comf6b070d2012-04-27 14:25:44 +0000818 // glRBMS will fail if requested samples is > max samples.
819 sampleCount = GrMin(sampleCount, ctxInfo.caps().maxSampleCount());
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000820 GL_ALLOC_CALL(ctxInfo.interface(),
821 RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
822 sampleCount,
823 format,
824 width, height));
825 created = (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctxInfo.interface()));
826 }
827 return created;
828}
829}
830
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000831bool GrGpuGL::createRenderTargetObjects(int width, int height,
832 GrGLuint texID,
833 GrGLRenderTarget::Desc* desc) {
834 desc->fMSColorRenderbufferID = 0;
835 desc->fRTFBOID = 0;
836 desc->fTexFBOID = 0;
bsalomon@google.com72830222013-01-23 20:25:22 +0000837 desc->fIsWrapped = false;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000838
839 GrGLenum status;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000840
bsalomon@google.comab15d612011-08-09 12:57:56 +0000841 GrGLenum msColorFormat = 0; // suppress warning
842
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000843 GL_CALL(GenFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000844 if (!desc->fTexFBOID) {
845 goto FAILED;
846 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000847
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000848
849 // If we are using multisampling we will create two FBOS. We render
850 // to one and then resolve to the texture bound to the other.
bsalomon@google.come269f212011-11-07 13:29:52 +0000851 if (desc->fSampleCnt > 0) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000852 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000853 goto FAILED;
854 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000855 GL_CALL(GenFramebuffers(1, &desc->fRTFBOID));
856 GL_CALL(GenRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000857 if (!desc->fRTFBOID ||
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000858 !desc->fMSColorRenderbufferID ||
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000859 !this->configToGLFormats(desc->fConfig,
860 // GLES requires sized internal formats
861 kES2_GrGLBinding == this->glBinding(),
862 &msColorFormat, NULL, NULL)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000863 goto FAILED;
864 }
865 } else {
866 desc->fRTFBOID = desc->fTexFBOID;
867 }
868
bsalomon@google.com0e9b41a2012-01-04 22:11:43 +0000869 // below here we may bind the FBO
bsalomon@google.comc811ea32012-05-21 15:33:09 +0000870 fHWBoundRenderTarget = NULL;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000871 if (desc->fRTFBOID != desc->fTexFBOID) {
bsalomon@google.com13ac3a32013-01-09 23:29:39 +0000872 GrAssert(desc->fSampleCnt > 0);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000873 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000874 desc->fMSColorRenderbufferID));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000875 if (!renderbuffer_storage_msaa(fGLContextInfo,
876 desc->fSampleCnt,
877 msColorFormat,
878 width, height)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000879 goto FAILED;
880 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000881 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fRTFBOID));
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000882 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000883 GR_GL_COLOR_ATTACHMENT0,
884 GR_GL_RENDERBUFFER,
885 desc->fMSColorRenderbufferID));
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000886 if (!this->glCaps().isConfigVerifiedColorAttachment(desc->fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000887 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
888 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
889 goto FAILED;
890 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000891 fGLContextInfo.caps().markConfigAsValidColorAttachment(
892 desc->fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000893 }
894 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000895 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000896
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000897 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
898 GR_GL_COLOR_ATTACHMENT0,
899 GR_GL_TEXTURE_2D,
900 texID, 0));
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000901 if (!this->glCaps().isConfigVerifiedColorAttachment(desc->fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000902 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
903 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
904 goto FAILED;
905 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000906 fGLContextInfo.caps().markConfigAsValidColorAttachment(desc->fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000907 }
908
909 return true;
910
911FAILED:
912 if (desc->fMSColorRenderbufferID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000913 GL_CALL(DeleteRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000914 }
915 if (desc->fRTFBOID != desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000916 GL_CALL(DeleteFramebuffers(1, &desc->fRTFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000917 }
918 if (desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000919 GL_CALL(DeleteFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000920 }
921 return false;
922}
923
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000924// good to set a break-point here to know when createTexture fails
925static GrTexture* return_null_texture() {
926// GrAssert(!"null texture");
927 return NULL;
928}
929
caryclark@google.comcf6285b2012-06-06 12:09:01 +0000930#if 0 && GR_DEBUG
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000931static size_t as_size_t(int x) {
932 return x;
933}
934#endif
935
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000936GrTexture* GrGpuGL::onCreateTexture(const GrTextureDesc& desc,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000937 const void* srcData,
938 size_t rowBytes) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000939
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000940 GrGLTexture::Desc glTexDesc;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000941 GrGLRenderTarget::Desc glRTDesc;
reed@google.comac10a2d2010-12-22 21:39:39 +0000942
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000943 // Attempt to catch un- or wrongly initialized sample counts;
944 GrAssert(desc.fSampleCnt >= 0 && desc.fSampleCnt <= 64);
945
robertphillips@google.com32716282012-06-04 12:48:45 +0000946 glTexDesc.fFlags = desc.fFlags;
bsalomon@google.com99621082011-11-15 16:47:16 +0000947 glTexDesc.fWidth = desc.fWidth;
948 glTexDesc.fHeight = desc.fHeight;
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000949 glTexDesc.fConfig = desc.fConfig;
robertphillips@google.com32716282012-06-04 12:48:45 +0000950 glTexDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.com72830222013-01-23 20:25:22 +0000951 glTexDesc.fIsWrapped = false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000952
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000953 glRTDesc.fMSColorRenderbufferID = 0;
954 glRTDesc.fRTFBOID = 0;
955 glRTDesc.fTexFBOID = 0;
bsalomon@google.com72830222013-01-23 20:25:22 +0000956 glRTDesc.fIsWrapped = false;
bsalomon@google.com64c4fe42011-11-05 14:51:01 +0000957 glRTDesc.fConfig = glTexDesc.fConfig;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000958
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000959 bool renderTarget = 0 != (desc.fFlags & kRenderTarget_GrTextureFlagBit);
reed@google.comac10a2d2010-12-22 21:39:39 +0000960
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000961 const Caps& caps = this->getCaps();
962
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000963 // We keep GrRenderTargets in GL's normal orientation so that they
964 // can be drawn to by the outside world without the client having
965 // to render upside down.
senorblanco@chromium.orgef5dbe12013-01-28 16:42:38 +0000966 glTexDesc.fOrigin = renderTarget ? kBottomLeft_GrSurfaceOrigin : kTopLeft_GrSurfaceOrigin;
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000967
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000968 glRTDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000969 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType() &&
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000970 desc.fSampleCnt) {
bsalomon@google.com945bbe12012-06-15 14:30:34 +0000971 //GrPrintf("MSAA RT requested but not supported on this platform.");
972 return return_null_texture();
reed@google.comac10a2d2010-12-22 21:39:39 +0000973 }
974
reed@google.comac10a2d2010-12-22 21:39:39 +0000975 if (renderTarget) {
bsalomon@google.comf6601872012-08-28 21:11:35 +0000976 if (glTexDesc.fWidth > caps.maxRenderTargetSize() ||
977 glTexDesc.fHeight > caps.maxRenderTargetSize()) {
bsalomon@google.com91958362011-06-13 17:58:13 +0000978 return return_null_texture();
979 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000980 }
981
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000982 GL_CALL(GenTextures(1, &glTexDesc.fTextureID));
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000983 if (renderTarget && this->glCaps().textureUsageSupport()) {
bsalomon@google.com07dd2bf2011-12-09 19:40:36 +0000984 // provides a hint about how this texture will be used
985 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
986 GR_GL_TEXTURE_USAGE,
987 GR_GL_FRAMEBUFFER_ATTACHMENT));
988 }
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000989 if (!glTexDesc.fTextureID) {
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000990 return return_null_texture();
991 }
992
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000993 this->setSpareTextureUnit();
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000994 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTexDesc.fTextureID));
bsalomon@google.come269f212011-11-07 13:29:52 +0000995
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000996 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
997 // drivers have a bug where an FBO won't be complete if it includes a
998 // texture that is not mipmap complete (considering the filter in use).
999 GrGLTexture::TexParams initialTexParams;
1000 // we only set a subset here so invalidate first
1001 initialTexParams.invalidate();
1002 initialTexParams.fFilter = GR_GL_NEAREST;
1003 initialTexParams.fWrapS = GR_GL_CLAMP_TO_EDGE;
1004 initialTexParams.fWrapT = GR_GL_CLAMP_TO_EDGE;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001005 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1006 GR_GL_TEXTURE_MAG_FILTER,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001007 initialTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001008 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1009 GR_GL_TEXTURE_MIN_FILTER,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001010 initialTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001011 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1012 GR_GL_TEXTURE_WRAP_S,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001013 initialTexParams.fWrapS));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001014 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1015 GR_GL_TEXTURE_WRAP_T,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001016 initialTexParams.fWrapT));
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001017 if (!this->uploadTexData(glTexDesc, true, 0, 0,
1018 glTexDesc.fWidth, glTexDesc.fHeight,
1019 desc.fConfig, srcData, rowBytes)) {
1020 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
1021 return return_null_texture();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001022 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001023
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001024 GrGLTexture* tex;
reed@google.comac10a2d2010-12-22 21:39:39 +00001025 if (renderTarget) {
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001026 // unbind the texture from the texture unit before binding it to the frame buffer
1027 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, 0));
1028
bsalomon@google.com99621082011-11-15 16:47:16 +00001029 if (!this->createRenderTargetObjects(glTexDesc.fWidth,
1030 glTexDesc.fHeight,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001031 glTexDesc.fTextureID,
1032 &glRTDesc)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001033 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001034 return return_null_texture();
1035 }
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001036 tex = SkNEW_ARGS(GrGLTexture, (this, glTexDesc, glRTDesc));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001037 } else {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001038 tex = SkNEW_ARGS(GrGLTexture, (this, glTexDesc));
reed@google.comac10a2d2010-12-22 21:39:39 +00001039 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001040 tex->setCachedTexParams(initialTexParams, this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00001041#ifdef TRACE_TEXTURE_CREATION
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001042 GrPrintf("--- new texture [%d] size=(%d %d) config=%d\n",
1043 glTexDesc.fTextureID, desc.fWidth, desc.fHeight, desc.fConfig);
reed@google.comac10a2d2010-12-22 21:39:39 +00001044#endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001045 return tex;
1046}
1047
1048namespace {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001049
1050const GrGLuint kUnknownBitCount = GrGLStencilBuffer::kUnknownBitCount;
1051
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001052void inline get_stencil_rb_sizes(const GrGLInterface* gl,
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001053 GrGLuint rb,
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001054 GrGLStencilBuffer::Format* format) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001055 // we shouldn't ever know one size and not the other
1056 GrAssert((kUnknownBitCount == format->fStencilBits) ==
1057 (kUnknownBitCount == format->fTotalBits));
1058 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001059 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001060 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1061 (GrGLint*)&format->fStencilBits);
1062 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001063 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001064 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1065 (GrGLint*)&format->fTotalBits);
1066 format->fTotalBits += format->fStencilBits;
1067 } else {
1068 format->fTotalBits = format->fStencilBits;
1069 }
1070 }
1071}
1072}
1073
1074bool GrGpuGL::createStencilBufferForRenderTarget(GrRenderTarget* rt,
1075 int width, int height) {
1076
1077 // All internally created RTs are also textures. We don't create
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +00001078 // SBs for a client's standalone RT (that is a RT that isn't also a texture).
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001079 GrAssert(rt->asTexture());
bsalomon@google.com99621082011-11-15 16:47:16 +00001080 GrAssert(width >= rt->width());
1081 GrAssert(height >= rt->height());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001082
1083 int samples = rt->numSamples();
1084 GrGLuint sbID;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001085 GL_CALL(GenRenderbuffers(1, &sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001086 if (!sbID) {
1087 return false;
1088 }
1089
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001090 int stencilFmtCnt = this->glCaps().stencilFormats().count();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001091 for (int i = 0; i < stencilFmtCnt; ++i) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001092 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001093 // we start with the last stencil format that succeeded in hopes
1094 // that we won't go through this loop more than once after the
1095 // first (painful) stencil creation.
1096 int sIdx = (i + fLastSuccessfulStencilFmtIdx) % stencilFmtCnt;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001097 const GrGLCaps::StencilFormat& sFmt =
1098 this->glCaps().stencilFormats()[sIdx];
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001099 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001100 // we do this "if" so that we don't call the multisample
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001101 // version on a GL that doesn't have an MSAA extension.
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001102 bool created;
1103 if (samples > 0) {
1104 created = renderbuffer_storage_msaa(fGLContextInfo,
1105 samples,
1106 sFmt.fInternalFormat,
1107 width, height);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001108 } else {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001109 GL_ALLOC_CALL(this->glInterface(),
1110 RenderbufferStorage(GR_GL_RENDERBUFFER,
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001111 sFmt.fInternalFormat,
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001112 width, height));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001113 created =
1114 (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(this->glInterface()));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001115 }
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001116 if (created) {
1117 // After sized formats we attempt an unsized format and take
1118 // whatever sizes GL gives us. In that case we query for the size.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001119 GrGLStencilBuffer::Format format = sFmt;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001120 get_stencil_rb_sizes(this->glInterface(), sbID, &format);
bsalomon@google.com72830222013-01-23 20:25:22 +00001121 static const bool kIsWrapped = false;
robertphillips@google.comf2e93fc2012-09-05 19:44:18 +00001122 SkAutoTUnref<GrStencilBuffer> sb(SkNEW_ARGS(GrGLStencilBuffer,
bsalomon@google.com72830222013-01-23 20:25:22 +00001123 (this, kIsWrapped, sbID, width, height,
robertphillips@google.comf2e93fc2012-09-05 19:44:18 +00001124 samples, format)));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001125 if (this->attachStencilBufferToRenderTarget(sb, rt)) {
1126 fLastSuccessfulStencilFmtIdx = sIdx;
robertphillips@google.com9fbcad02012-09-09 14:44:15 +00001127 sb->transferToCache();
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001128 rt->setStencilBuffer(sb);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001129 return true;
1130 }
1131 sb->abandon(); // otherwise we lose sbID
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001132 }
1133 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001134 GL_CALL(DeleteRenderbuffers(1, &sbID));
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001135 return false;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001136}
1137
1138bool GrGpuGL::attachStencilBufferToRenderTarget(GrStencilBuffer* sb,
1139 GrRenderTarget* rt) {
1140 GrGLRenderTarget* glrt = (GrGLRenderTarget*) rt;
1141
1142 GrGLuint fbo = glrt->renderFBOID();
1143
1144 if (NULL == sb) {
1145 if (NULL != rt->getStencilBuffer()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001146 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001147 GR_GL_STENCIL_ATTACHMENT,
1148 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001149 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001150 GR_GL_DEPTH_ATTACHMENT,
1151 GR_GL_RENDERBUFFER, 0));
1152#if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001153 GrGLenum status;
1154 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001155 GrAssert(GR_GL_FRAMEBUFFER_COMPLETE == status);
1156#endif
1157 }
1158 return true;
1159 } else {
1160 GrGLStencilBuffer* glsb = (GrGLStencilBuffer*) sb;
1161 GrGLuint rb = glsb->renderbufferID();
1162
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001163 fHWBoundRenderTarget = NULL;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001164 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, fbo));
1165 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001166 GR_GL_STENCIL_ATTACHMENT,
1167 GR_GL_RENDERBUFFER, rb));
1168 if (glsb->format().fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001169 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001170 GR_GL_DEPTH_ATTACHMENT,
1171 GR_GL_RENDERBUFFER, rb));
1172 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001173 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001174 GR_GL_DEPTH_ATTACHMENT,
1175 GR_GL_RENDERBUFFER, 0));
reed@google.comac10a2d2010-12-22 21:39:39 +00001176 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001177
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001178 GrGLenum status;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001179 if (!this->glCaps().isColorConfigAndStencilFormatVerified(rt->config(),
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001180 glsb->format())) {
1181 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1182 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001183 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001184 GR_GL_STENCIL_ATTACHMENT,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001185 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001186 if (glsb->format().fPacked) {
1187 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1188 GR_GL_DEPTH_ATTACHMENT,
1189 GR_GL_RENDERBUFFER, 0));
1190 }
1191 return false;
1192 } else {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001193 fGLContextInfo.caps().markColorConfigAndStencilFormatAsVerified(
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001194 rt->config(),
1195 glsb->format());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001196 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001197 }
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001198 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001199 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001200}
1201
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001202////////////////////////////////////////////////////////////////////////////////
1203
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001204GrVertexBuffer* GrGpuGL::onCreateVertexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001205 GrGLuint id;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001206 GL_CALL(GenBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001207 if (id) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001208 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, id));
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001209 fHWGeometryState.fArrayPtrsDirty = true;
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001210 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001211 // make sure driver can allocate memory for this buffer
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001212 GL_ALLOC_CALL(this->glInterface(),
1213 BufferData(GR_GL_ARRAY_BUFFER,
1214 size,
1215 NULL, // data ptr
1216 dynamic ? GR_GL_DYNAMIC_DRAW :
1217 GR_GL_STATIC_DRAW));
1218 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001219 GL_CALL(DeleteBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001220 // deleting bound buffer does implicit bind to 0
1221 fHWGeometryState.fVertexBuffer = NULL;
1222 return NULL;
1223 }
bsalomon@google.com72830222013-01-23 20:25:22 +00001224 static const bool kIsWrapped = false;
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001225 GrGLVertexBuffer* vertexBuffer = SkNEW_ARGS(GrGLVertexBuffer,
bsalomon@google.com72830222013-01-23 20:25:22 +00001226 (this, kIsWrapped, id,
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001227 size, dynamic));
reed@google.comac10a2d2010-12-22 21:39:39 +00001228 fHWGeometryState.fVertexBuffer = vertexBuffer;
1229 return vertexBuffer;
1230 }
1231 return NULL;
1232}
1233
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001234GrIndexBuffer* GrGpuGL::onCreateIndexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001235 GrGLuint id;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001236 GL_CALL(GenBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001237 if (id) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001238 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, id));
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001239 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001240 // make sure driver can allocate memory for this buffer
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001241 GL_ALLOC_CALL(this->glInterface(),
1242 BufferData(GR_GL_ELEMENT_ARRAY_BUFFER,
1243 size,
1244 NULL, // data ptr
1245 dynamic ? GR_GL_DYNAMIC_DRAW :
1246 GR_GL_STATIC_DRAW));
1247 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001248 GL_CALL(DeleteBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001249 // deleting bound buffer does implicit bind to 0
1250 fHWGeometryState.fIndexBuffer = NULL;
1251 return NULL;
1252 }
bsalomon@google.com72830222013-01-23 20:25:22 +00001253 static const bool kIsWrapped = false;
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001254 GrIndexBuffer* indexBuffer = SkNEW_ARGS(GrGLIndexBuffer,
bsalomon@google.com72830222013-01-23 20:25:22 +00001255 (this, kIsWrapped, id, size, dynamic));
reed@google.comac10a2d2010-12-22 21:39:39 +00001256 fHWGeometryState.fIndexBuffer = indexBuffer;
1257 return indexBuffer;
1258 }
1259 return NULL;
1260}
1261
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00001262GrPath* GrGpuGL::onCreatePath(const SkPath& inPath) {
bsalomon@google.comf6601872012-08-28 21:11:35 +00001263 GrAssert(fCaps.pathStencilingSupport());
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001264 return SkNEW_ARGS(GrGLPath, (this, inPath));
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00001265}
1266
bsalomon@google.coma3201942012-06-21 19:58:20 +00001267void GrGpuGL::flushScissor() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001268 const GrDrawState& drawState = this->getDrawState();
1269 const GrGLRenderTarget* rt =
1270 static_cast<const GrGLRenderTarget*>(drawState.getRenderTarget());
1271
1272 GrAssert(NULL != rt);
1273 const GrGLIRect& vp = rt->getViewport();
reed@google.comac10a2d2010-12-22 21:39:39 +00001274
bsalomon@google.coma3201942012-06-21 19:58:20 +00001275 if (fScissorState.fEnabled) {
1276 GrGLIRect scissor;
1277 scissor.setRelativeTo(vp,
1278 fScissorState.fRect.fLeft,
1279 fScissorState.fRect.fTop,
1280 fScissorState.fRect.width(),
1281 fScissorState.fRect.height());
1282 // if the scissor fully contains the viewport then we fall through and
1283 // disable the scissor test.
1284 if (!scissor.contains(vp)) {
1285 if (fHWScissorSettings.fRect != scissor) {
1286 scissor.pushToGLScissor(this->glInterface());
1287 fHWScissorSettings.fRect = scissor;
1288 }
1289 if (kYes_TriState != fHWScissorSettings.fEnabled) {
1290 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1291 fHWScissorSettings.fEnabled = kYes_TriState;
1292 }
1293 return;
1294 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001295 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001296 if (kNo_TriState != fHWScissorSettings.fEnabled) {
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001297 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.coma3201942012-06-21 19:58:20 +00001298 fHWScissorSettings.fEnabled = kNo_TriState;
1299 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001300 }
1301}
1302
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001303void GrGpuGL::onClear(const GrIRect* rect, GrColor color) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001304 const GrDrawState& drawState = this->getDrawState();
1305 const GrRenderTarget* rt = drawState.getRenderTarget();
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001306 // parent class should never let us get here with no RT
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001307 GrAssert(NULL != rt);
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001308
bsalomon@google.com74b98712011-11-11 19:46:16 +00001309 GrIRect clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001310 if (NULL != rect) {
1311 // flushScissor expects rect to be clipped to the target.
bsalomon@google.com74b98712011-11-11 19:46:16 +00001312 clippedRect = *rect;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001313 GrIRect rtRect = SkIRect::MakeWH(rt->width(), rt->height());
bsalomon@google.com74b98712011-11-11 19:46:16 +00001314 if (clippedRect.intersect(rtRect)) {
1315 rect = &clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001316 } else {
1317 return;
1318 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001319 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001320 this->flushRenderTarget(rect);
bsalomon@google.coma3201942012-06-21 19:58:20 +00001321 GrAutoTRestore<ScissorState> asr(&fScissorState);
1322 fScissorState.fEnabled = (NULL != rect);
1323 if (fScissorState.fEnabled) {
1324 fScissorState.fRect = *rect;
1325 }
1326 this->flushScissor();
bsalomon@google.com74b98712011-11-11 19:46:16 +00001327
1328 GrGLfloat r, g, b, a;
1329 static const GrGLfloat scale255 = 1.f / 255.f;
1330 a = GrColorUnpackA(color) * scale255;
1331 GrGLfloat scaleRGB = scale255;
bsalomon@google.com74b98712011-11-11 19:46:16 +00001332 r = GrColorUnpackR(color) * scaleRGB;
1333 g = GrColorUnpackG(color) * scaleRGB;
1334 b = GrColorUnpackB(color) * scaleRGB;
1335
1336 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00001337 fHWWriteToColor = kYes_TriState;
bsalomon@google.com74b98712011-11-11 19:46:16 +00001338 GL_CALL(ClearColor(r, g, b, a));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001339 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001340}
1341
bsalomon@google.comedc177d2011-08-05 15:46:40 +00001342void GrGpuGL::clearStencil() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001343 if (NULL == this->getDrawState().getRenderTarget()) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001344 return;
1345 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001346
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001347 this->flushRenderTarget(&GrIRect::EmptyIRect());
1348
bsalomon@google.coma3201942012-06-21 19:58:20 +00001349 GrAutoTRestore<ScissorState> asr(&fScissorState);
1350 fScissorState.fEnabled = false;
1351 this->flushScissor();
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001352
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001353 GL_CALL(StencilMask(0xffffffff));
1354 GL_CALL(ClearStencil(0));
1355 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001356 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001357}
1358
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001359void GrGpuGL::clearStencilClip(const GrIRect& rect, bool insideClip) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001360 const GrDrawState& drawState = this->getDrawState();
1361 const GrRenderTarget* rt = drawState.getRenderTarget();
1362 GrAssert(NULL != rt);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001363
1364 // this should only be called internally when we know we have a
1365 // stencil buffer.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001366 GrAssert(NULL != rt->getStencilBuffer());
1367 GrGLint stencilBitCount = rt->getStencilBuffer()->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001368#if 0
reed@google.comac10a2d2010-12-22 21:39:39 +00001369 GrAssert(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00001370 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001371#else
1372 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001373 // ANGLE a partial stencil mask will cause clears to be
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001374 // turned into draws. Our contract on GrDrawTarget says that
1375 // changing the clip between stencil passes may or may not
1376 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00001377 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001378#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001379 GrGLint value;
1380 if (insideClip) {
1381 value = (1 << (stencilBitCount - 1));
1382 } else {
1383 value = 0;
1384 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001385 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.coma3201942012-06-21 19:58:20 +00001386
1387 GrAutoTRestore<ScissorState> asr(&fScissorState);
1388 fScissorState.fEnabled = true;
1389 fScissorState.fRect = rect;
1390 this->flushScissor();
1391
caryclark@google.comcf6285b2012-06-06 12:09:01 +00001392 GL_CALL(StencilMask((uint32_t) clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001393 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001394 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001395 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001396}
1397
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001398void GrGpuGL::onForceRenderTargetFlush() {
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001399 this->flushRenderTarget(&GrIRect::EmptyIRect());
reed@google.comac10a2d2010-12-22 21:39:39 +00001400}
1401
bsalomon@google.comc4364992011-11-07 15:54:49 +00001402bool GrGpuGL::readPixelsWillPayForYFlip(GrRenderTarget* renderTarget,
1403 int left, int top,
1404 int width, int height,
1405 GrPixelConfig config,
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001406 size_t rowBytes) const {
1407 // if GL can do the flip then we'll never pay for it.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001408 if (this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001409 return false;
1410 }
1411
1412 // If we have to do memcpy to handle non-trim rowBytes then we
bsalomon@google.com7107fa72011-11-10 14:54:14 +00001413 // get the flip for free. Otherwise it costs.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001414 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001415 return true;
1416 }
1417 // If we have to do memcpys to handle rowBytes then y-flip is free
1418 // Note the rowBytes might be tight to the passed in data, but if data
1419 // gets clipped in x to the target the rowBytes will no longer be tight.
1420 if (left >= 0 && (left + width) < renderTarget->width()) {
1421 return 0 == rowBytes ||
1422 GrBytesPerPixel(config) * width == rowBytes;
1423 } else {
1424 return false;
1425 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001426}
1427
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001428bool GrGpuGL::onReadPixels(GrRenderTarget* target,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001429 int left, int top,
1430 int width, int height,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001431 GrPixelConfig config,
1432 void* buffer,
1433 size_t rowBytes,
1434 bool invertY) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001435 GrGLenum format;
1436 GrGLenum type;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00001437 if (!this->configToGLFormats(config, false, NULL, &format, &type)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001438 return false;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001439 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001440 size_t bpp = GrBytesPerPixel(config);
1441 if (!adjust_pixel_ops_params(target->width(), target->height(), bpp,
1442 &left, &top, &width, &height,
1443 const_cast<const void**>(&buffer),
1444 &rowBytes)) {
1445 return false;
1446 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001447
bsalomon@google.comc6980972011-11-02 19:57:21 +00001448 // resolve the render target if necessary
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001449 GrGLRenderTarget* tgt = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001450 GrDrawState::AutoRenderTargetRestore artr;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001451 switch (tgt->getResolveType()) {
1452 case GrGLRenderTarget::kCantResolve_ResolveType:
1453 return false;
1454 case GrGLRenderTarget::kAutoResolves_ResolveType:
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001455 artr.set(this->drawState(), target);
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001456 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001457 break;
1458 case GrGLRenderTarget::kCanResolve_ResolveType:
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001459 this->onResolveRenderTarget(tgt);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001460 // we don't track the state of the READ FBO ID.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001461 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1462 tgt->textureFBOID()));
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001463 break;
1464 default:
1465 GrCrash("Unknown resolve type");
reed@google.comac10a2d2010-12-22 21:39:39 +00001466 }
1467
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001468 const GrGLIRect& glvp = tgt->getViewport();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001469
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001470 // the read rect is viewport-relative
1471 GrGLIRect readRect;
1472 readRect.setRelativeTo(glvp, left, top, width, height);
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001473
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001474 size_t tightRowBytes = bpp * width;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001475 if (0 == rowBytes) {
1476 rowBytes = tightRowBytes;
1477 }
1478 size_t readDstRowBytes = tightRowBytes;
1479 void* readDst = buffer;
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001480
bsalomon@google.comc6980972011-11-02 19:57:21 +00001481 // determine if GL can read using the passed rowBytes or if we need
1482 // a scratch buffer.
1483 SkAutoSMalloc<32 * sizeof(GrColor)> scratch;
1484 if (rowBytes != tightRowBytes) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001485 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.comc6980972011-11-02 19:57:21 +00001486 GrAssert(!(rowBytes % sizeof(GrColor)));
1487 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, rowBytes / sizeof(GrColor)));
1488 readDstRowBytes = rowBytes;
1489 } else {
1490 scratch.reset(tightRowBytes * height);
1491 readDst = scratch.get();
1492 }
1493 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001494 if (!invertY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001495 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 1));
1496 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001497 GL_CALL(ReadPixels(readRect.fLeft, readRect.fBottom,
1498 readRect.fWidth, readRect.fHeight,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001499 format, type, readDst));
1500 if (readDstRowBytes != tightRowBytes) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001501 GrAssert(this->glCaps().packRowLengthSupport());
bsalomon@google.comc6980972011-11-02 19:57:21 +00001502 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
1503 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001504 if (!invertY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001505 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 0));
1506 invertY = true;
1507 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001508
1509 // now reverse the order of the rows, since GL's are bottom-to-top, but our
bsalomon@google.comc6980972011-11-02 19:57:21 +00001510 // API presents top-to-bottom. We must preserve the padding contents. Note
1511 // that the above readPixels did not overwrite the padding.
1512 if (readDst == buffer) {
1513 GrAssert(rowBytes == readDstRowBytes);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001514 if (!invertY) {
1515 scratch.reset(tightRowBytes);
1516 void* tmpRow = scratch.get();
1517 // flip y in-place by rows
1518 const int halfY = height >> 1;
1519 char* top = reinterpret_cast<char*>(buffer);
1520 char* bottom = top + (height - 1) * rowBytes;
1521 for (int y = 0; y < halfY; y++) {
1522 memcpy(tmpRow, top, tightRowBytes);
1523 memcpy(top, bottom, tightRowBytes);
1524 memcpy(bottom, tmpRow, tightRowBytes);
1525 top += rowBytes;
1526 bottom -= rowBytes;
1527 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001528 }
1529 } else {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001530 GrAssert(readDst != buffer); GrAssert(rowBytes != tightRowBytes);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001531 // copy from readDst to buffer while flipping y
caryclark@google.comcf6285b2012-06-06 12:09:01 +00001532 // const int halfY = height >> 1;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001533 const char* src = reinterpret_cast<const char*>(readDst);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001534 char* dst = reinterpret_cast<char*>(buffer);
1535 if (!invertY) {
1536 dst += (height-1) * rowBytes;
1537 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001538 for (int y = 0; y < height; y++) {
1539 memcpy(dst, src, tightRowBytes);
1540 src += readDstRowBytes;
bsalomon@google.comc4364992011-11-07 15:54:49 +00001541 if (invertY) {
1542 dst += rowBytes;
1543 } else {
1544 dst -= rowBytes;
1545 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001546 }
1547 }
1548 return true;
1549}
1550
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001551void GrGpuGL::flushRenderTarget(const GrIRect* bound) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001552
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001553 GrGLRenderTarget* rt =
1554 static_cast<GrGLRenderTarget*>(this->drawState()->getRenderTarget());
1555 GrAssert(NULL != rt);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001556
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001557 if (fHWBoundRenderTarget != rt) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001558 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, rt->renderFBOID()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001559 #if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001560 GrGLenum status;
1561 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001562 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
reed@google.comb9255d52011-06-13 18:54:59 +00001563 GrPrintf("GrGpuGL::flushRenderTarget glCheckFramebufferStatus %x\n", status);
reed@google.comac10a2d2010-12-22 21:39:39 +00001564 }
1565 #endif
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001566 fHWBoundRenderTarget = rt;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001567 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.coma3201942012-06-21 19:58:20 +00001568 if (fHWViewport != vp) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001569 vp.pushToGLViewport(this->glInterface());
bsalomon@google.coma3201942012-06-21 19:58:20 +00001570 fHWViewport = vp;
reed@google.comac10a2d2010-12-22 21:39:39 +00001571 }
1572 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001573 if (NULL == bound || !bound->isEmpty()) {
1574 rt->flagAsNeedingResolve(bound);
1575 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001576}
1577
twiz@google.com0f31ca72011-03-18 17:38:11 +00001578GrGLenum gPrimitiveType2GLMode[] = {
1579 GR_GL_TRIANGLES,
1580 GR_GL_TRIANGLE_STRIP,
1581 GR_GL_TRIANGLE_FAN,
1582 GR_GL_POINTS,
1583 GR_GL_LINES,
1584 GR_GL_LINE_STRIP
reed@google.comac10a2d2010-12-22 21:39:39 +00001585};
1586
bsalomon@google.comd302f142011-03-03 13:54:13 +00001587#define SWAP_PER_DRAW 0
1588
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001589#if SWAP_PER_DRAW
bsalomon@google.comd302f142011-03-03 13:54:13 +00001590 #if GR_MAC_BUILD
1591 #include <AGL/agl.h>
1592 #elif GR_WIN32_BUILD
bsalomon@google.comce7357d2012-06-25 17:49:25 +00001593 #include <gl/GL.h>
bsalomon@google.comd302f142011-03-03 13:54:13 +00001594 void SwapBuf() {
1595 DWORD procID = GetCurrentProcessId();
1596 HWND hwnd = GetTopWindow(GetDesktopWindow());
1597 while(hwnd) {
1598 DWORD wndProcID = 0;
1599 GetWindowThreadProcessId(hwnd, &wndProcID);
1600 if(wndProcID == procID) {
1601 SwapBuffers(GetDC(hwnd));
1602 }
1603 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
1604 }
1605 }
1606 #endif
1607#endif
1608
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001609void GrGpuGL::onGpuDrawIndexed(GrPrimitiveType type,
1610 uint32_t startVertex,
1611 uint32_t startIndex,
1612 uint32_t vertexCount,
1613 uint32_t indexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001614 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1615
twiz@google.com0f31ca72011-03-18 17:38:11 +00001616 GrGLvoid* indices = (GrGLvoid*)(sizeof(uint16_t) * startIndex);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001617
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001618 GrAssert(NULL != fHWGeometryState.fIndexBuffer);
1619 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1620
1621 // our setupGeometry better have adjusted this to zero since
1622 // DrawElements always draws from the begining of the arrays for idx 0.
1623 GrAssert(0 == startVertex);
reed@google.comac10a2d2010-12-22 21:39:39 +00001624
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001625 GL_CALL(DrawElements(gPrimitiveType2GLMode[type], indexCount,
1626 GR_GL_UNSIGNED_SHORT, indices));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001627#if SWAP_PER_DRAW
1628 glFlush();
1629 #if GR_MAC_BUILD
1630 aglSwapBuffers(aglGetCurrentContext());
1631 int set_a_break_pt_here = 9;
1632 aglSwapBuffers(aglGetCurrentContext());
1633 #elif GR_WIN32_BUILD
1634 SwapBuf();
1635 int set_a_break_pt_here = 9;
1636 SwapBuf();
1637 #endif
1638#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001639}
1640
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001641void GrGpuGL::onGpuDrawNonIndexed(GrPrimitiveType type,
1642 uint32_t startVertex,
1643 uint32_t vertexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001644 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1645
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001646 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1647
1648 // our setupGeometry better have adjusted this to zero.
1649 // DrawElements doesn't take an offset so we always adjus the startVertex.
1650 GrAssert(0 == startVertex);
1651
1652 // pass 0 for parameter first. We have to adjust gl*Pointer() to
1653 // account for startVertex in the DrawElements case. So we always
1654 // rely on setupGeometry to have accounted for startVertex.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001655 GL_CALL(DrawArrays(gPrimitiveType2GLMode[type], 0, vertexCount));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001656#if SWAP_PER_DRAW
1657 glFlush();
1658 #if GR_MAC_BUILD
1659 aglSwapBuffers(aglGetCurrentContext());
1660 int set_a_break_pt_here = 9;
1661 aglSwapBuffers(aglGetCurrentContext());
1662 #elif GR_WIN32_BUILD
1663 SwapBuf();
1664 int set_a_break_pt_here = 9;
1665 SwapBuf();
1666 #endif
1667#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001668}
1669
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001670namespace {
bsalomon@google.com100abf42012-09-05 17:40:04 +00001671
1672static const uint16_t kOnes16 = static_cast<uint16_t>(~0);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001673const GrStencilSettings& winding_nv_path_stencil_settings() {
1674 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
1675 kIncClamp_StencilOp,
1676 kIncClamp_StencilOp,
1677 kAlwaysIfInClip_StencilFunc,
bsalomon@google.com100abf42012-09-05 17:40:04 +00001678 kOnes16, kOnes16, kOnes16);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001679 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
1680}
1681const GrStencilSettings& even_odd_nv_path_stencil_settings() {
1682 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
1683 kInvert_StencilOp,
1684 kInvert_StencilOp,
1685 kAlwaysIfInClip_StencilFunc,
bsalomon@google.com100abf42012-09-05 17:40:04 +00001686 kOnes16, kOnes16, kOnes16);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001687 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
1688}
1689}
1690
1691
1692void GrGpuGL::setStencilPathSettings(const GrPath&,
sugoi@google.com12b4e272012-12-06 20:13:11 +00001693 SkPath::FillType fill,
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001694 GrStencilSettings* settings) {
1695 switch (fill) {
sugoi@google.com12b4e272012-12-06 20:13:11 +00001696 case SkPath::kEvenOdd_FillType:
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001697 *settings = even_odd_nv_path_stencil_settings();
1698 return;
sugoi@google.com12b4e272012-12-06 20:13:11 +00001699 case SkPath::kWinding_FillType:
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001700 *settings = winding_nv_path_stencil_settings();
1701 return;
1702 default:
1703 GrCrash("Unexpected path fill.");
1704 }
1705}
1706
sugoi@google.com12b4e272012-12-06 20:13:11 +00001707void GrGpuGL::onGpuStencilPath(const GrPath* path, SkPath::FillType fill) {
bsalomon@google.comf6601872012-08-28 21:11:35 +00001708 GrAssert(fCaps.pathStencilingSupport());
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001709
1710 GrGLuint id = static_cast<const GrGLPath*>(path)->pathID();
1711 GrDrawState* drawState = this->drawState();
1712 GrAssert(NULL != drawState->getRenderTarget());
1713 if (NULL == drawState->getRenderTarget()->getStencilBuffer()) {
1714 return;
1715 }
1716
1717 // Decide how to manipulate the stencil buffer based on the fill rule.
1718 // Also, assert that the stencil settings we set in setStencilPathSettings
1719 // are present.
1720 GrAssert(!fStencilSettings.isTwoSided());
1721 GrGLenum fillMode;
1722 switch (fill) {
sugoi@google.com12b4e272012-12-06 20:13:11 +00001723 case SkPath::kWinding_FillType:
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001724 fillMode = GR_GL_COUNT_UP;
1725 GrAssert(kIncClamp_StencilOp ==
1726 fStencilSettings.passOp(GrStencilSettings::kFront_Face));
1727 GrAssert(kIncClamp_StencilOp ==
1728 fStencilSettings.failOp(GrStencilSettings::kFront_Face));
1729 break;
sugoi@google.com12b4e272012-12-06 20:13:11 +00001730 case SkPath::kEvenOdd_FillType:
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001731 fillMode = GR_GL_INVERT;
1732 GrAssert(kInvert_StencilOp ==
1733 fStencilSettings.passOp(GrStencilSettings::kFront_Face));
1734 GrAssert(kInvert_StencilOp ==
1735 fStencilSettings.failOp(GrStencilSettings::kFront_Face));
1736 break;
1737 default:
1738 // Only the above two fill rules are allowed.
1739 GrCrash("Unexpected path fill.");
bsalomon@google.com548a4332012-07-11 19:45:22 +00001740 return; // suppress unused var warning.
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001741 }
1742 GrGLint writeMask = fStencilSettings.writeMask(GrStencilSettings::kFront_Face);
1743 GL_CALL(StencilFillPath(id, fillMode, writeMask));
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00001744}
1745
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001746void GrGpuGL::onResolveRenderTarget(GrRenderTarget* target) {
1747
1748 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(target);
reed@google.comac10a2d2010-12-22 21:39:39 +00001749
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001750 if (rt->needsResolve()) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001751 GrAssert(GrGLCaps::kNone_MSFBOType != this->glCaps().msFBOType());
reed@google.comac10a2d2010-12-22 21:39:39 +00001752 GrAssert(rt->textureFBOID() != rt->renderFBOID());
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001753 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1754 rt->renderFBOID()));
1755 GL_CALL(BindFramebuffer(GR_GL_DRAW_FRAMEBUFFER,
1756 rt->textureFBOID()));
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001757 // make sure we go through flushRenderTarget() since we've modified
1758 // the bound DRAW FBO ID.
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001759 fHWBoundRenderTarget = NULL;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001760 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001761 const GrIRect dirtyRect = rt->getResolveRect();
1762 GrGLIRect r;
bsalomon@google.coma3201942012-06-21 19:58:20 +00001763 r.setRelativeTo(vp, dirtyRect.fLeft, dirtyRect.fTop,
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001764 dirtyRect.width(), dirtyRect.height());
reed@google.comac10a2d2010-12-22 21:39:39 +00001765
bsalomon@google.coma3201942012-06-21 19:58:20 +00001766 GrAutoTRestore<ScissorState> asr;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001767 if (GrGLCaps::kAppleES_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001768 // Apple's extension uses the scissor as the blit bounds.
bsalomon@google.coma3201942012-06-21 19:58:20 +00001769 asr.reset(&fScissorState);
1770 fScissorState.fEnabled = true;
1771 fScissorState.fRect = dirtyRect;
1772 this->flushScissor();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001773 GL_CALL(ResolveMultisampleFramebuffer());
reed@google.comac10a2d2010-12-22 21:39:39 +00001774 } else {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001775 if (GrGLCaps::kDesktopARB_MSFBOType != this->glCaps().msFBOType()) {
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001776 // this respects the scissor during the blit, so disable it.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001777 GrAssert(GrGLCaps::kDesktopEXT_MSFBOType ==
1778 this->glCaps().msFBOType());
bsalomon@google.coma3201942012-06-21 19:58:20 +00001779 asr.reset(&fScissorState);
1780 fScissorState.fEnabled = false;
1781 this->flushScissor();
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001782 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001783 int right = r.fLeft + r.fWidth;
1784 int top = r.fBottom + r.fHeight;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001785 GL_CALL(BlitFramebuffer(r.fLeft, r.fBottom, right, top,
1786 r.fLeft, r.fBottom, right, top,
1787 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001788 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001789 rt->flagAsResolved();
reed@google.comac10a2d2010-12-22 21:39:39 +00001790 }
1791}
1792
bsalomon@google.com411dad02012-06-05 20:24:20 +00001793namespace {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001794
bsalomon@google.com411dad02012-06-05 20:24:20 +00001795GrGLenum gr_to_gl_stencil_func(GrStencilFunc basicFunc) {
1796 static const GrGLenum gTable[] = {
1797 GR_GL_ALWAYS, // kAlways_StencilFunc
1798 GR_GL_NEVER, // kNever_StencilFunc
1799 GR_GL_GREATER, // kGreater_StencilFunc
1800 GR_GL_GEQUAL, // kGEqual_StencilFunc
1801 GR_GL_LESS, // kLess_StencilFunc
1802 GR_GL_LEQUAL, // kLEqual_StencilFunc,
1803 GR_GL_EQUAL, // kEqual_StencilFunc,
1804 GR_GL_NOTEQUAL, // kNotEqual_StencilFunc,
1805 };
1806 GR_STATIC_ASSERT(GR_ARRAY_COUNT(gTable) == kBasicStencilFuncCount);
1807 GR_STATIC_ASSERT(0 == kAlways_StencilFunc);
1808 GR_STATIC_ASSERT(1 == kNever_StencilFunc);
1809 GR_STATIC_ASSERT(2 == kGreater_StencilFunc);
1810 GR_STATIC_ASSERT(3 == kGEqual_StencilFunc);
1811 GR_STATIC_ASSERT(4 == kLess_StencilFunc);
1812 GR_STATIC_ASSERT(5 == kLEqual_StencilFunc);
1813 GR_STATIC_ASSERT(6 == kEqual_StencilFunc);
1814 GR_STATIC_ASSERT(7 == kNotEqual_StencilFunc);
1815 GrAssert((unsigned) basicFunc < kBasicStencilFuncCount);
1816
1817 return gTable[basicFunc];
1818}
1819
1820GrGLenum gr_to_gl_stencil_op(GrStencilOp op) {
1821 static const GrGLenum gTable[] = {
1822 GR_GL_KEEP, // kKeep_StencilOp
1823 GR_GL_REPLACE, // kReplace_StencilOp
1824 GR_GL_INCR_WRAP, // kIncWrap_StencilOp
1825 GR_GL_INCR, // kIncClamp_StencilOp
1826 GR_GL_DECR_WRAP, // kDecWrap_StencilOp
1827 GR_GL_DECR, // kDecClamp_StencilOp
1828 GR_GL_ZERO, // kZero_StencilOp
1829 GR_GL_INVERT, // kInvert_StencilOp
1830 };
1831 GR_STATIC_ASSERT(GR_ARRAY_COUNT(gTable) == kStencilOpCount);
1832 GR_STATIC_ASSERT(0 == kKeep_StencilOp);
1833 GR_STATIC_ASSERT(1 == kReplace_StencilOp);
1834 GR_STATIC_ASSERT(2 == kIncWrap_StencilOp);
1835 GR_STATIC_ASSERT(3 == kIncClamp_StencilOp);
1836 GR_STATIC_ASSERT(4 == kDecWrap_StencilOp);
1837 GR_STATIC_ASSERT(5 == kDecClamp_StencilOp);
1838 GR_STATIC_ASSERT(6 == kZero_StencilOp);
1839 GR_STATIC_ASSERT(7 == kInvert_StencilOp);
1840 GrAssert((unsigned) op < kStencilOpCount);
1841 return gTable[op];
1842}
1843
1844void set_gl_stencil(const GrGLInterface* gl,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001845 const GrStencilSettings& settings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00001846 GrGLenum glFace,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001847 GrStencilSettings::Face grFace) {
1848 GrGLenum glFunc = gr_to_gl_stencil_func(settings.func(grFace));
1849 GrGLenum glFailOp = gr_to_gl_stencil_op(settings.failOp(grFace));
1850 GrGLenum glPassOp = gr_to_gl_stencil_op(settings.passOp(grFace));
1851
1852 GrGLint ref = settings.funcRef(grFace);
1853 GrGLint mask = settings.funcMask(grFace);
1854 GrGLint writeMask = settings.writeMask(grFace);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001855
1856 if (GR_GL_FRONT_AND_BACK == glFace) {
1857 // we call the combined func just in case separate stencil is not
1858 // supported.
1859 GR_GL_CALL(gl, StencilFunc(glFunc, ref, mask));
1860 GR_GL_CALL(gl, StencilMask(writeMask));
1861 GR_GL_CALL(gl, StencilOp(glFailOp, glPassOp, glPassOp));
1862 } else {
1863 GR_GL_CALL(gl, StencilFuncSeparate(glFace, glFunc, ref, mask));
1864 GR_GL_CALL(gl, StencilMaskSeparate(glFace, writeMask));
1865 GR_GL_CALL(gl, StencilOpSeparate(glFace, glFailOp, glPassOp, glPassOp));
1866 }
1867}
1868}
bsalomon@google.comd302f142011-03-03 13:54:13 +00001869
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001870void GrGpuGL::flushStencil(DrawType type) {
1871 if (kStencilPath_DrawType == type) {
1872 GrAssert(!fStencilSettings.isTwoSided());
1873 // Just the func, ref, and mask is set here. The op and write mask are params to the call
1874 // that draws the path to the SB (glStencilFillPath)
1875 GrGLenum func =
1876 gr_to_gl_stencil_func(fStencilSettings.func(GrStencilSettings::kFront_Face));
1877 GL_CALL(PathStencilFunc(func,
1878 fStencilSettings.funcRef(GrStencilSettings::kFront_Face),
1879 fStencilSettings.funcMask(GrStencilSettings::kFront_Face)));
1880 } else if (fHWStencilSettings != fStencilSettings) {
1881 if (fStencilSettings.isDisabled()) {
1882 if (kNo_TriState != fHWStencilTestEnabled) {
1883 GL_CALL(Disable(GR_GL_STENCIL_TEST));
1884 fHWStencilTestEnabled = kNo_TriState;
1885 }
1886 } else {
1887 if (kYes_TriState != fHWStencilTestEnabled) {
1888 GL_CALL(Enable(GR_GL_STENCIL_TEST));
1889 fHWStencilTestEnabled = kYes_TriState;
1890 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001891 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001892 if (!fStencilSettings.isDisabled()) {
bsalomon@google.comf6601872012-08-28 21:11:35 +00001893 if (this->getCaps().twoSidedStencilSupport()) {
bsalomon@google.com411dad02012-06-05 20:24:20 +00001894 set_gl_stencil(this->glInterface(),
bsalomon@google.coma3201942012-06-21 19:58:20 +00001895 fStencilSettings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00001896 GR_GL_FRONT,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001897 GrStencilSettings::kFront_Face);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001898 set_gl_stencil(this->glInterface(),
bsalomon@google.coma3201942012-06-21 19:58:20 +00001899 fStencilSettings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00001900 GR_GL_BACK,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001901 GrStencilSettings::kBack_Face);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001902 } else {
bsalomon@google.com411dad02012-06-05 20:24:20 +00001903 set_gl_stencil(this->glInterface(),
bsalomon@google.coma3201942012-06-21 19:58:20 +00001904 fStencilSettings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00001905 GR_GL_FRONT_AND_BACK,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001906 GrStencilSettings::kFront_Face);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001907 }
1908 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001909 fHWStencilSettings = fStencilSettings;
reed@google.comac10a2d2010-12-22 21:39:39 +00001910 }
1911}
1912
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001913void GrGpuGL::flushAAState(DrawType type) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001914 const GrRenderTarget* rt = this->getDrawState().getRenderTarget();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001915 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001916 // ES doesn't support toggling GL_MULTISAMPLE and doesn't have
1917 // smooth lines.
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001918 // we prefer smooth lines over multisampled lines
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00001919 bool smoothLines = false;
1920
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001921 if (kDrawLines_DrawType == type) {
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00001922 smoothLines = this->willUseHWAALines();
1923 if (smoothLines) {
1924 if (kYes_TriState != fHWAAState.fSmoothLineEnabled) {
1925 GL_CALL(Enable(GR_GL_LINE_SMOOTH));
1926 fHWAAState.fSmoothLineEnabled = kYes_TriState;
1927 // must disable msaa to use line smoothing
1928 if (rt->isMultisampled() &&
1929 kNo_TriState != fHWAAState.fMSAAEnabled) {
1930 GL_CALL(Disable(GR_GL_MULTISAMPLE));
1931 fHWAAState.fMSAAEnabled = kNo_TriState;
1932 }
1933 }
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001934 } else {
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00001935 if (kNo_TriState != fHWAAState.fSmoothLineEnabled) {
1936 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
1937 fHWAAState.fSmoothLineEnabled = kNo_TriState;
1938 }
1939 }
1940 }
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001941 if (!smoothLines && rt->isMultisampled()) {
1942 // FIXME: GL_NV_pr doesn't seem to like MSAA disabled. The paths
1943 // convex hulls of each segment appear to get filled.
1944 bool enableMSAA = kStencilPath_DrawType == type ||
1945 this->getDrawState().isHWAntialiasState();
1946 if (enableMSAA) {
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00001947 if (kYes_TriState != fHWAAState.fMSAAEnabled) {
1948 GL_CALL(Enable(GR_GL_MULTISAMPLE));
1949 fHWAAState.fMSAAEnabled = kYes_TriState;
1950 }
1951 } else {
1952 if (kNo_TriState != fHWAAState.fMSAAEnabled) {
1953 GL_CALL(Disable(GR_GL_MULTISAMPLE));
1954 fHWAAState.fMSAAEnabled = kNo_TriState;
1955 }
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001956 }
1957 }
1958 }
1959}
1960
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00001961void GrGpuGL::flushBlend(bool isLines,
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001962 GrBlendCoeff srcCoeff,
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001963 GrBlendCoeff dstCoeff) {
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00001964 if (isLines && this->willUseHWAALines()) {
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001965 if (kYes_TriState != fHWBlendState.fEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001966 GL_CALL(Enable(GR_GL_BLEND));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001967 fHWBlendState.fEnabled = kYes_TriState;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001968 }
bsalomon@google.com47059542012-06-06 20:51:20 +00001969 if (kSA_GrBlendCoeff != fHWBlendState.fSrcCoeff ||
1970 kISA_GrBlendCoeff != fHWBlendState.fDstCoeff) {
1971 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[kSA_GrBlendCoeff],
1972 gXfermodeCoeff2Blend[kISA_GrBlendCoeff]));
1973 fHWBlendState.fSrcCoeff = kSA_GrBlendCoeff;
1974 fHWBlendState.fDstCoeff = kISA_GrBlendCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001975 }
1976 } else {
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001977 // any optimization to disable blending should
1978 // have already been applied and tweaked the coeffs
1979 // to (1, 0).
bsalomon@google.com47059542012-06-06 20:51:20 +00001980 bool blendOff = kOne_GrBlendCoeff == srcCoeff &&
1981 kZero_GrBlendCoeff == dstCoeff;
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001982 if (blendOff) {
1983 if (kNo_TriState != fHWBlendState.fEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001984 GL_CALL(Disable(GR_GL_BLEND));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001985 fHWBlendState.fEnabled = kNo_TriState;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001986 }
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001987 } else {
1988 if (kYes_TriState != fHWBlendState.fEnabled) {
1989 GL_CALL(Enable(GR_GL_BLEND));
1990 fHWBlendState.fEnabled = kYes_TriState;
1991 }
1992 if (fHWBlendState.fSrcCoeff != srcCoeff ||
1993 fHWBlendState.fDstCoeff != dstCoeff) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001994 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
1995 gXfermodeCoeff2Blend[dstCoeff]));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001996 fHWBlendState.fSrcCoeff = srcCoeff;
1997 fHWBlendState.fDstCoeff = dstCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001998 }
bsalomon@google.coma5d056a2012-03-27 15:59:58 +00001999 GrColor blendConst = this->getDrawState().getBlendConstant();
bsalomon@google.com271cffc2011-05-20 14:13:56 +00002000 if ((BlendCoeffReferencesConstant(srcCoeff) ||
2001 BlendCoeffReferencesConstant(dstCoeff)) &&
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002002 (!fHWBlendState.fConstColorValid ||
2003 fHWBlendState.fConstColor != blendConst)) {
bsalomon@google.com75347472012-09-17 17:23:21 +00002004 GrGLfloat c[4];
2005 GrColorToRGBAFloat(blendConst, c);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002006 GL_CALL(BlendColor(c[0], c[1], c[2], c[3]));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002007 fHWBlendState.fConstColor = blendConst;
2008 fHWBlendState.fConstColorValid = true;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002009 }
2010 }
2011 }
2012}
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002013namespace {
2014
bsalomon@google.com6d003d12012-09-11 15:45:20 +00002015inline void set_tex_swizzle(GrGLenum swizzle[4], const GrGLInterface* gl) {
bsalomon@google.com4d063de2012-05-31 17:59:23 +00002016 GR_GL_CALL(gl, TexParameteriv(GR_GL_TEXTURE_2D,
2017 GR_GL_TEXTURE_SWIZZLE_RGBA,
2018 reinterpret_cast<const GrGLint*>(swizzle)));
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002019}
bsalomon@google.comb8670992012-07-25 21:27:09 +00002020
bsalomon@google.com6c76d242012-09-07 16:52:24 +00002021inline GrGLenum tile_to_gl_wrap(SkShader::TileMode tm) {
bsalomon@google.comb8670992012-07-25 21:27:09 +00002022 static const GrGLenum gWrapModes[] = {
2023 GR_GL_CLAMP_TO_EDGE,
2024 GR_GL_REPEAT,
2025 GR_GL_MIRRORED_REPEAT
2026 };
2027 GrAssert((unsigned) tm <= SK_ARRAY_COUNT(gWrapModes));
2028 GR_STATIC_ASSERT(0 == SkShader::kClamp_TileMode);
2029 GR_STATIC_ASSERT(1 == SkShader::kRepeat_TileMode);
2030 GR_STATIC_ASSERT(2 == SkShader::kMirror_TileMode);
2031 return gWrapModes[tm];
2032}
2033
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002034}
2035
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002036void GrGpuGL::bindTexture(int unitIdx, const GrTextureParams& params, GrGLTexture* texture) {
2037 GrAssert(NULL != texture);
tomhudson@google.comd0c1a062012-07-12 17:23:52 +00002038
bsalomon@google.comb8670992012-07-25 21:27:09 +00002039 // If we created a rt/tex and rendered to it without using a texture and now we're texturing
2040 // from the rt it will still be the last bound texture, but it needs resolving. So keep this
bsalomon@google.com4c883782012-06-04 19:05:11 +00002041 // out of the "last != next" check.
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002042 GrGLRenderTarget* texRT = static_cast<GrGLRenderTarget*>(texture->asRenderTarget());
bsalomon@google.com4c883782012-06-04 19:05:11 +00002043 if (NULL != texRT) {
2044 this->onResolveRenderTarget(texRT);
2045 }
2046
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002047 if (fHWBoundTextures[unitIdx] != texture) {
2048 this->setTextureUnit(unitIdx);
2049 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, texture->textureID()));
2050 fHWBoundTextures[unitIdx] = texture;
bsalomon@google.com4c883782012-06-04 19:05:11 +00002051 }
2052
bsalomon@google.com4c883782012-06-04 19:05:11 +00002053 ResetTimestamp timestamp;
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002054 const GrGLTexture::TexParams& oldTexParams = texture->getCachedTexParams(&timestamp);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002055 bool setAll = timestamp < this->getResetTimestamp();
2056 GrGLTexture::TexParams newTexParams;
2057
bsalomon@google.comb8670992012-07-25 21:27:09 +00002058 newTexParams.fFilter = params.isBilerp() ? GR_GL_LINEAR : GR_GL_NEAREST;
bsalomon@google.com4c883782012-06-04 19:05:11 +00002059
bsalomon@google.comb8670992012-07-25 21:27:09 +00002060 newTexParams.fWrapS = tile_to_gl_wrap(params.getTileModeX());
2061 newTexParams.fWrapT = tile_to_gl_wrap(params.getTileModeY());
bsalomon@google.com4c883782012-06-04 19:05:11 +00002062 memcpy(newTexParams.fSwizzleRGBA,
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002063 GrGLShaderBuilder::GetTexParamSwizzle(texture->config(), this->glCaps()),
bsalomon@google.com4c883782012-06-04 19:05:11 +00002064 sizeof(newTexParams.fSwizzleRGBA));
2065 if (setAll || newTexParams.fFilter != oldTexParams.fFilter) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002066 this->setTextureUnit(unitIdx);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002067 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2068 GR_GL_TEXTURE_MAG_FILTER,
2069 newTexParams.fFilter));
2070 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2071 GR_GL_TEXTURE_MIN_FILTER,
2072 newTexParams.fFilter));
2073 }
2074 if (setAll || newTexParams.fWrapS != oldTexParams.fWrapS) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002075 this->setTextureUnit(unitIdx);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002076 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2077 GR_GL_TEXTURE_WRAP_S,
2078 newTexParams.fWrapS));
2079 }
2080 if (setAll || newTexParams.fWrapT != oldTexParams.fWrapT) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002081 this->setTextureUnit(unitIdx);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002082 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2083 GR_GL_TEXTURE_WRAP_T,
2084 newTexParams.fWrapT));
2085 }
2086 if (this->glCaps().textureSwizzleSupport() &&
2087 (setAll || memcmp(newTexParams.fSwizzleRGBA,
2088 oldTexParams.fSwizzleRGBA,
2089 sizeof(newTexParams.fSwizzleRGBA)))) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002090 this->setTextureUnit(unitIdx);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002091 set_tex_swizzle(newTexParams.fSwizzleRGBA,
2092 this->glInterface());
2093 }
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002094 texture->setCachedTexParams(newTexParams, this->getResetTimestamp());
bsalomon@google.com4c883782012-06-04 19:05:11 +00002095}
2096
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002097void GrGpuGL::flushMiscFixedFunctionState() {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002098
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002099 const GrDrawState& drawState = this->getDrawState();
reed@google.comac10a2d2010-12-22 21:39:39 +00002100
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002101 if (drawState.isDitherState()) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002102 if (kYes_TriState != fHWDitherEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002103 GL_CALL(Enable(GR_GL_DITHER));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002104 fHWDitherEnabled = kYes_TriState;
2105 }
2106 } else {
2107 if (kNo_TriState != fHWDitherEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002108 GL_CALL(Disable(GR_GL_DITHER));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002109 fHWDitherEnabled = kNo_TriState;
reed@google.comac10a2d2010-12-22 21:39:39 +00002110 }
2111 }
2112
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002113 if (drawState.isColorWriteDisabled()) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002114 if (kNo_TriState != fHWWriteToColor) {
2115 GL_CALL(ColorMask(GR_GL_FALSE, GR_GL_FALSE,
2116 GR_GL_FALSE, GR_GL_FALSE));
2117 fHWWriteToColor = kNo_TriState;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002118 }
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002119 } else {
2120 if (kYes_TriState != fHWWriteToColor) {
2121 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
2122 fHWWriteToColor = kYes_TriState;
2123 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00002124 }
2125
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002126 if (fHWDrawFace != drawState.getDrawFace()) {
bsalomon@google.coma5d056a2012-03-27 15:59:58 +00002127 switch (this->getDrawState().getDrawFace()) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002128 case GrDrawState::kCCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002129 GL_CALL(Enable(GR_GL_CULL_FACE));
2130 GL_CALL(CullFace(GR_GL_BACK));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002131 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002132 case GrDrawState::kCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002133 GL_CALL(Enable(GR_GL_CULL_FACE));
2134 GL_CALL(CullFace(GR_GL_FRONT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002135 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002136 case GrDrawState::kBoth_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002137 GL_CALL(Disable(GR_GL_CULL_FACE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002138 break;
2139 default:
2140 GrCrash("Unknown draw face.");
2141 }
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002142 fHWDrawFace = drawState.getDrawFace();
bsalomon@google.comd302f142011-03-03 13:54:13 +00002143 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002144}
2145
2146void GrGpuGL::notifyVertexBufferBind(const GrGLVertexBuffer* buffer) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002147 if (fHWGeometryState.fVertexBuffer != buffer) {
2148 fHWGeometryState.fArrayPtrsDirty = true;
2149 fHWGeometryState.fVertexBuffer = buffer;
2150 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002151}
2152
2153void GrGpuGL::notifyVertexBufferDelete(const GrGLVertexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002154 if (fHWGeometryState.fVertexBuffer == buffer) {
2155 // deleting bound buffer does implied bind to 0
2156 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002157 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00002158 }
2159}
2160
2161void GrGpuGL::notifyIndexBufferBind(const GrGLIndexBuffer* buffer) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002162 fHWGeometryState.fIndexBuffer = buffer;
reed@google.comac10a2d2010-12-22 21:39:39 +00002163}
2164
2165void GrGpuGL::notifyIndexBufferDelete(const GrGLIndexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002166 if (fHWGeometryState.fIndexBuffer == buffer) {
2167 // deleting bound buffer does implied bind to 0
2168 fHWGeometryState.fIndexBuffer = NULL;
2169 }
2170}
2171
reed@google.comac10a2d2010-12-22 21:39:39 +00002172void GrGpuGL::notifyRenderTargetDelete(GrRenderTarget* renderTarget) {
2173 GrAssert(NULL != renderTarget);
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002174 if (fHWBoundRenderTarget == renderTarget) {
2175 fHWBoundRenderTarget = NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +00002176 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002177}
2178
2179void GrGpuGL::notifyTextureDelete(GrGLTexture* texture) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002180 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002181 if (fHWBoundTextures[s] == texture) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002182 // deleting bound texture does implied bind to 0
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002183 fHWBoundTextures[s] = NULL;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002184 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002185 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002186}
2187
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002188bool GrGpuGL::configToGLFormats(GrPixelConfig config,
2189 bool getSizedInternalFormat,
2190 GrGLenum* internalFormat,
2191 GrGLenum* externalFormat,
2192 GrGLenum* externalType) {
2193 GrGLenum dontCare;
2194 if (NULL == internalFormat) {
2195 internalFormat = &dontCare;
2196 }
2197 if (NULL == externalFormat) {
2198 externalFormat = &dontCare;
2199 }
2200 if (NULL == externalType) {
2201 externalType = &dontCare;
2202 }
2203
reed@google.comac10a2d2010-12-22 21:39:39 +00002204 switch (config) {
bsalomon@google.com0342a852012-08-20 19:22:38 +00002205 case kRGBA_8888_GrPixelConfig:
bsalomon@google.comc4364992011-11-07 15:54:49 +00002206 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002207 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002208 if (getSizedInternalFormat) {
2209 *internalFormat = GR_GL_RGBA8;
2210 } else {
2211 *internalFormat = GR_GL_RGBA;
2212 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002213 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.comc4364992011-11-07 15:54:49 +00002214 break;
bsalomon@google.com0342a852012-08-20 19:22:38 +00002215 case kBGRA_8888_GrPixelConfig:
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002216 if (!this->glCaps().bgraFormatSupport()) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002217 return false;
2218 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002219 if (this->glCaps().bgraIsInternalFormat()) {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002220 if (getSizedInternalFormat) {
2221 *internalFormat = GR_GL_BGRA8;
2222 } else {
2223 *internalFormat = GR_GL_BGRA;
2224 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002225 } else {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002226 if (getSizedInternalFormat) {
2227 *internalFormat = GR_GL_RGBA8;
2228 } else {
2229 *internalFormat = GR_GL_RGBA;
2230 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002231 }
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002232 *externalFormat = GR_GL_BGRA;
bsalomon@google.com6f379512011-11-16 20:36:03 +00002233 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002234 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002235 case kRGB_565_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002236 *internalFormat = GR_GL_RGB;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002237 *externalFormat = GR_GL_RGB;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002238 if (getSizedInternalFormat) {
2239 if (this->glBinding() == kDesktop_GrGLBinding) {
2240 return false;
2241 } else {
2242 *internalFormat = GR_GL_RGB565;
2243 }
2244 } else {
2245 *internalFormat = GR_GL_RGB;
2246 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002247 *externalType = GR_GL_UNSIGNED_SHORT_5_6_5;
reed@google.comac10a2d2010-12-22 21:39:39 +00002248 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002249 case kRGBA_4444_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002250 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002251 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002252 if (getSizedInternalFormat) {
2253 *internalFormat = GR_GL_RGBA4;
2254 } else {
2255 *internalFormat = GR_GL_RGBA;
2256 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002257 *externalType = GR_GL_UNSIGNED_SHORT_4_4_4_4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002258 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002259 case kIndex_8_GrPixelConfig:
bsalomon@google.comf6601872012-08-28 21:11:35 +00002260 if (this->getCaps().eightBitPaletteSupport()) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002261 *internalFormat = GR_GL_PALETTE8_RGBA8;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002262 // glCompressedTexImage doesn't take external params
2263 *externalFormat = GR_GL_PALETTE8_RGBA8;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002264 // no sized/unsized internal format distinction here
2265 *internalFormat = GR_GL_PALETTE8_RGBA8;
2266 // unused with CompressedTexImage
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002267 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002268 } else {
2269 return false;
2270 }
2271 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002272 case kAlpha_8_GrPixelConfig:
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002273 if (this->glCaps().textureRedSupport()) {
2274 *internalFormat = GR_GL_RED;
2275 *externalFormat = GR_GL_RED;
2276 if (getSizedInternalFormat) {
2277 *internalFormat = GR_GL_R8;
2278 } else {
2279 *internalFormat = GR_GL_RED;
2280 }
2281 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002282 } else {
2283 *internalFormat = GR_GL_ALPHA;
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002284 *externalFormat = GR_GL_ALPHA;
2285 if (getSizedInternalFormat) {
2286 *internalFormat = GR_GL_ALPHA8;
2287 } else {
2288 *internalFormat = GR_GL_ALPHA;
2289 }
2290 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002291 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002292 break;
2293 default:
2294 return false;
2295 }
2296 return true;
2297}
2298
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002299void GrGpuGL::setTextureUnit(int unit) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002300 GrAssert(unit >= 0 && unit < GrDrawState::kNumStages);
bsalomon@google.com49209392012-06-05 15:13:46 +00002301 if (fHWActiveTextureUnitIdx != unit) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002302 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com49209392012-06-05 15:13:46 +00002303 fHWActiveTextureUnitIdx = unit;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002304 }
2305}
bsalomon@google.com316f99232011-01-13 21:28:12 +00002306
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002307void GrGpuGL::setSpareTextureUnit() {
bsalomon@google.com49209392012-06-05 15:13:46 +00002308 if (fHWActiveTextureUnitIdx != (GR_GL_TEXTURE0 + SPARE_TEX_UNIT)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002309 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + SPARE_TEX_UNIT));
bsalomon@google.com49209392012-06-05 15:13:46 +00002310 fHWActiveTextureUnitIdx = SPARE_TEX_UNIT;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002311 }
2312}
2313
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002314void GrGpuGL::setBuffers(bool indexed,
2315 int* extraVertexOffset,
2316 int* extraIndexOffset) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002317
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002318 GrAssert(NULL != extraVertexOffset);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002319
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002320 const GeometryPoolState& geoPoolState = this->getGeomPoolState();
2321
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002322 GrGLVertexBuffer* vbuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002323 switch (this->getGeomSrc().fVertexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002324 case kBuffer_GeometrySrcType:
2325 *extraVertexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002326 vbuf = (GrGLVertexBuffer*) this->getGeomSrc().fVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002327 break;
2328 case kArray_GeometrySrcType:
2329 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002330 this->finalizeReservedVertices();
2331 *extraVertexOffset = geoPoolState.fPoolStartVertex;
2332 vbuf = (GrGLVertexBuffer*) geoPoolState.fPoolVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002333 break;
2334 default:
2335 vbuf = NULL; // suppress warning
2336 GrCrash("Unknown geometry src type!");
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002337 }
2338
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002339 GrAssert(NULL != vbuf);
2340 GrAssert(!vbuf->isLocked());
2341 if (fHWGeometryState.fVertexBuffer != vbuf) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002342 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, vbuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002343 fHWGeometryState.fArrayPtrsDirty = true;
2344 fHWGeometryState.fVertexBuffer = vbuf;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002345 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002346
2347 if (indexed) {
2348 GrAssert(NULL != extraIndexOffset);
2349
2350 GrGLIndexBuffer* ibuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002351 switch (this->getGeomSrc().fIndexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002352 case kBuffer_GeometrySrcType:
2353 *extraIndexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002354 ibuf = (GrGLIndexBuffer*)this->getGeomSrc().fIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002355 break;
2356 case kArray_GeometrySrcType:
2357 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002358 this->finalizeReservedIndices();
2359 *extraIndexOffset = geoPoolState.fPoolStartIndex;
2360 ibuf = (GrGLIndexBuffer*) geoPoolState.fPoolIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002361 break;
2362 default:
2363 ibuf = NULL; // suppress warning
2364 GrCrash("Unknown geometry src type!");
2365 }
2366
2367 GrAssert(NULL != ibuf);
2368 GrAssert(!ibuf->isLocked());
2369 if (fHWGeometryState.fIndexBuffer != ibuf) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002370 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, ibuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002371 fHWGeometryState.fIndexBuffer = ibuf;
2372 }
2373 }
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002374}