blob: 5c2e0cbcb30cd42015fc449a042285a72e6fe723 [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
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000479namespace {
480
481GrSurfaceOrigin resolve_origin(GrSurfaceOrigin origin, bool renderTarget) {
482 // By default, GrRenderTargets are GL's normal orientation so that they
483 // can be drawn to by the outside world without the client having
484 // to render upside down.
485 if (kDefault_GrSurfaceOrigin == origin) {
486 return renderTarget ? kBottomLeft_GrSurfaceOrigin : kTopLeft_GrSurfaceOrigin;
487 } else {
488 return origin;
489 }
490}
491
492}
493
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000494GrTexture* GrGpuGL::onWrapBackendTexture(const GrBackendTextureDesc& desc) {
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000495 if (!this->configToGLFormats(desc.fConfig, false, NULL, NULL, NULL)) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000496 return NULL;
497 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000498
robertphillips@google.comb72e5d32012-10-30 15:18:10 +0000499 if (0 == desc.fTextureHandle) {
500 return NULL;
501 }
502
503 int maxSize = this->getCaps().maxTextureSize();
504 if (desc.fWidth > maxSize || desc.fHeight > maxSize) {
505 return NULL;
506 }
507
508 GrGLTexture::Desc glTexDesc;
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000509 // next line relies on GrBackendTextureDesc's flags matching GrTexture's
robertphillips@google.com32716282012-06-04 12:48:45 +0000510 glTexDesc.fFlags = (GrTextureFlags) desc.fFlags;
bsalomon@google.com99621082011-11-15 16:47:16 +0000511 glTexDesc.fWidth = desc.fWidth;
512 glTexDesc.fHeight = desc.fHeight;
bsalomon@google.come269f212011-11-07 13:29:52 +0000513 glTexDesc.fConfig = desc.fConfig;
robertphillips@google.com32716282012-06-04 12:48:45 +0000514 glTexDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.come269f212011-11-07 13:29:52 +0000515 glTexDesc.fTextureID = static_cast<GrGLuint>(desc.fTextureHandle);
bsalomon@google.com72830222013-01-23 20:25:22 +0000516 glTexDesc.fIsWrapped = true;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000517 bool renderTarget = 0 != (desc.fFlags & kRenderTarget_GrBackendTextureFlag);
518 // FIXME: this should be calling resolve_origin(), but Chrome code is currently
519 // assuming the old behaviour, which is that backend textures are always
520 // BottomLeft, even for non-RT's. Once Chrome is fixed, change this to:
521 // glTexDesc.fOrigin = resolve_origin(desc.fOrigin, renderTarget);
522 if (kDefault_GrSurfaceOrigin == desc.fOrigin) {
523 glTexDesc.fOrigin = kBottomLeft_GrSurfaceOrigin;
524 } else {
525 glTexDesc.fOrigin = desc.fOrigin;
526 }
bsalomon@google.come269f212011-11-07 13:29:52 +0000527
528 GrGLTexture* texture = NULL;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000529 if (renderTarget) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000530 GrGLRenderTarget::Desc glRTDesc;
531 glRTDesc.fRTFBOID = 0;
532 glRTDesc.fTexFBOID = 0;
533 glRTDesc.fMSColorRenderbufferID = 0;
bsalomon@google.come269f212011-11-07 13:29:52 +0000534 glRTDesc.fConfig = desc.fConfig;
535 glRTDesc.fSampleCnt = desc.fSampleCnt;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000536 glRTDesc.fOrigin = glTexDesc.fOrigin;
bsalomon@google.com99621082011-11-15 16:47:16 +0000537 if (!this->createRenderTargetObjects(glTexDesc.fWidth,
538 glTexDesc.fHeight,
bsalomon@google.come269f212011-11-07 13:29:52 +0000539 glTexDesc.fTextureID,
540 &glRTDesc)) {
541 return NULL;
542 }
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000543 texture = SkNEW_ARGS(GrGLTexture, (this, glTexDesc, glRTDesc));
bsalomon@google.come269f212011-11-07 13:29:52 +0000544 } else {
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000545 texture = SkNEW_ARGS(GrGLTexture, (this, glTexDesc));
bsalomon@google.come269f212011-11-07 13:29:52 +0000546 }
547 if (NULL == texture) {
548 return NULL;
549 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000550
bsalomon@google.come269f212011-11-07 13:29:52 +0000551 this->setSpareTextureUnit();
552 return texture;
553}
554
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000555GrRenderTarget* GrGpuGL::onWrapBackendRenderTarget(const GrBackendRenderTargetDesc& desc) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000556 GrGLRenderTarget::Desc glDesc;
557 glDesc.fConfig = desc.fConfig;
558 glDesc.fRTFBOID = static_cast<GrGLuint>(desc.fRenderTargetHandle);
559 glDesc.fMSColorRenderbufferID = 0;
560 glDesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
561 glDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.com72830222013-01-23 20:25:22 +0000562 glDesc.fIsWrapped = true;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000563 glDesc.fOrigin = desc.fOrigin;
564 if (glDesc.fRTFBOID == 0) {
565 GrAssert(desc.fOrigin == kBottomLeft_GrSurfaceOrigin);
566 }
567
568 glDesc.fOrigin = resolve_origin(desc.fOrigin, true);
bsalomon@google.come269f212011-11-07 13:29:52 +0000569 GrGLIRect viewport;
570 viewport.fLeft = 0;
571 viewport.fBottom = 0;
572 viewport.fWidth = desc.fWidth;
573 viewport.fHeight = desc.fHeight;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000574
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000575 GrRenderTarget* tgt = SkNEW_ARGS(GrGLRenderTarget,
576 (this, glDesc, viewport));
bsalomon@google.come269f212011-11-07 13:29:52 +0000577 if (desc.fStencilBits) {
578 GrGLStencilBuffer::Format format;
579 format.fInternalFormat = GrGLStencilBuffer::kUnknownInternalFormat;
580 format.fPacked = false;
581 format.fStencilBits = desc.fStencilBits;
582 format.fTotalBits = desc.fStencilBits;
bsalomon@google.com1f0f1a32013-01-23 21:32:32 +0000583 static const bool kIsSBWrapped = false;
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000584 GrGLStencilBuffer* sb = SkNEW_ARGS(GrGLStencilBuffer,
585 (this,
bsalomon@google.com1f0f1a32013-01-23 21:32:32 +0000586 kIsSBWrapped,
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000587 0,
588 desc.fWidth,
589 desc.fHeight,
590 desc.fSampleCnt,
591 format));
bsalomon@google.come269f212011-11-07 13:29:52 +0000592 tgt->setStencilBuffer(sb);
593 sb->unref();
594 }
595 return tgt;
596}
597
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000598////////////////////////////////////////////////////////////////////////////////
599
bsalomon@google.com6f379512011-11-16 20:36:03 +0000600void GrGpuGL::onWriteTexturePixels(GrTexture* texture,
601 int left, int top, int width, int height,
602 GrPixelConfig config, const void* buffer,
603 size_t rowBytes) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000604 if (NULL == buffer) {
605 return;
606 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000607 GrGLTexture* glTex = static_cast<GrGLTexture*>(texture);
608
bsalomon@google.com6f379512011-11-16 20:36:03 +0000609 this->setSpareTextureUnit();
610 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTex->textureID()));
611 GrGLTexture::Desc desc;
robertphillips@google.com32716282012-06-04 12:48:45 +0000612 desc.fFlags = glTex->desc().fFlags;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000613 desc.fWidth = glTex->width();
614 desc.fHeight = glTex->height();
robertphillips@google.com32716282012-06-04 12:48:45 +0000615 desc.fConfig = glTex->config();
616 desc.fSampleCnt = glTex->desc().fSampleCnt;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000617 desc.fTextureID = glTex->textureID();
bsalomon@google.com2d0bade2012-10-26 19:01:17 +0000618 desc.fOrigin = glTex->origin();
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000619
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000620 this->uploadTexData(desc, false,
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000621 left, top, width, height,
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000622 config, buffer, rowBytes);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000623}
624
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000625namespace {
626bool adjust_pixel_ops_params(int surfaceWidth,
627 int surfaceHeight,
628 size_t bpp,
629 int* left, int* top, int* width, int* height,
630 const void** data,
631 size_t* rowBytes) {
632 if (!*rowBytes) {
633 *rowBytes = *width * bpp;
634 }
635
636 GrIRect subRect = GrIRect::MakeXYWH(*left, *top, *width, *height);
637 GrIRect bounds = GrIRect::MakeWH(surfaceWidth, surfaceHeight);
638
639 if (!subRect.intersect(bounds)) {
640 return false;
641 }
642 *data = reinterpret_cast<const void*>(reinterpret_cast<intptr_t>(*data) +
643 (subRect.fTop - *top) * *rowBytes + (subRect.fLeft - *left) * bpp);
644
645 *left = subRect.fLeft;
646 *top = subRect.fTop;
647 *width = subRect.width();
648 *height = subRect.height();
649 return true;
650}
651}
652
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000653bool GrGpuGL::uploadTexData(const GrGLTexture::Desc& desc,
654 bool isNewTexture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000655 int left, int top, int width, int height,
656 GrPixelConfig dataConfig,
657 const void* data,
658 size_t rowBytes) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000659 GrAssert(NULL != data || isNewTexture);
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000660
661 size_t bpp = GrBytesPerPixel(dataConfig);
662 if (!adjust_pixel_ops_params(desc.fWidth, desc.fHeight, bpp, &left, &top,
663 &width, &height, &data, &rowBytes)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000664 return false;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000665 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000666 size_t trimRowBytes = width * bpp;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000667
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000668 // in case we need a temporary, trimmed copy of the src pixels
669 SkAutoSMalloc<128 * 128> tempStorage;
670
bsalomon@google.com313f0192012-07-10 17:21:02 +0000671 // paletted textures cannot be partially updated
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000672 bool useTexStorage = isNewTexture &&
bsalomon@google.com313f0192012-07-10 17:21:02 +0000673 desc.fConfig != kIndex_8_GrPixelConfig &&
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000674 this->glCaps().texStorageSupport();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000675
bsalomon@google.com313f0192012-07-10 17:21:02 +0000676 if (useTexStorage && kDesktop_GrGLBinding == this->glBinding()) {
677 // 565 is not a sized internal format on desktop GL. So on desktop with
678 // 565 we always use an unsized internal format to let the system pick
679 // the best sized format to convert the 565 data to. Since TexStorage
680 // only allows sized internal formats we will instead use TexImage2D.
681 useTexStorage = desc.fConfig != kRGB_565_GrPixelConfig;
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000682 }
683
684 GrGLenum internalFormat;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000685 GrGLenum externalFormat;
686 GrGLenum externalType;
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000687 // glTexStorage requires sized internal formats on both desktop and ES. ES
688 // glTexImage requires an unsized format.
689 if (!this->configToGLFormats(dataConfig, useTexStorage, &internalFormat,
690 &externalFormat, &externalType)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000691 return false;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000692 }
693
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000694 if (!isNewTexture && GR_GL_PALETTE8_RGBA8 == internalFormat) {
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000695 // paletted textures cannot be updated
696 return false;
697 }
698
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000699 /*
bsalomon@google.com6f379512011-11-16 20:36:03 +0000700 * check whether to allocate a temporary buffer for flipping y or
701 * because our srcData has extra bytes past each row. If so, we need
702 * to trim those off here, since GL ES may not let us specify
703 * GL_UNPACK_ROW_LENGTH.
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000704 */
bsalomon@google.com6f379512011-11-16 20:36:03 +0000705 bool restoreGLRowLength = false;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000706 bool swFlipY = false;
707 bool glFlipY = false;
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000708 if (NULL != data) {
senorblanco@chromium.orgef5dbe12013-01-28 16:42:38 +0000709 if (kBottomLeft_GrSurfaceOrigin == desc.fOrigin) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000710 if (this->glCaps().unpackFlipYSupport()) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000711 glFlipY = true;
712 } else {
713 swFlipY = true;
714 }
715 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000716 if (this->glCaps().unpackRowLengthSupport() && !swFlipY) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000717 // can't use this for flipping, only non-neg values allowed. :(
718 if (rowBytes != trimRowBytes) {
719 GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
720 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength));
721 restoreGLRowLength = true;
722 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000723 } else {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000724 if (trimRowBytes != rowBytes || swFlipY) {
725 // copy data into our new storage, skipping the trailing bytes
726 size_t trimSize = height * trimRowBytes;
727 const char* src = (const char*)data;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000728 if (swFlipY) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000729 src += (height - 1) * rowBytes;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000730 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000731 char* dst = (char*)tempStorage.reset(trimSize);
732 for (int y = 0; y < height; y++) {
733 memcpy(dst, src, trimRowBytes);
734 if (swFlipY) {
735 src -= rowBytes;
736 } else {
737 src += rowBytes;
738 }
739 dst += trimRowBytes;
740 }
741 // now point data to our copied version
742 data = tempStorage.get();
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000743 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000744 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000745 if (glFlipY) {
746 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_TRUE));
747 }
748 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, static_cast<GrGLint>(bpp)));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000749 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000750 bool succeeded = true;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000751 if (isNewTexture &&
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000752 0 == left && 0 == top &&
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000753 desc.fWidth == width && desc.fHeight == height) {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000754 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000755 if (useTexStorage) {
756 // We never resize or change formats of textures. We don't use
757 // mipmaps currently.
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000758 GL_ALLOC_CALL(this->glInterface(),
759 TexStorage2D(GR_GL_TEXTURE_2D,
760 1, // levels
761 internalFormat,
762 desc.fWidth, desc.fHeight));
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000763 } else {
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000764 if (GR_GL_PALETTE8_RGBA8 == internalFormat) {
765 GrGLsizei imageSize = desc.fWidth * desc.fHeight +
766 kGrColorTableSize;
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000767 GL_ALLOC_CALL(this->glInterface(),
768 CompressedTexImage2D(GR_GL_TEXTURE_2D,
769 0, // level
770 internalFormat,
771 desc.fWidth, desc.fHeight,
772 0, // border
773 imageSize,
774 data));
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000775 } else {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000776 GL_ALLOC_CALL(this->glInterface(),
777 TexImage2D(GR_GL_TEXTURE_2D,
778 0, // level
779 internalFormat,
780 desc.fWidth, desc.fHeight,
781 0, // border
782 externalFormat, externalType,
783 data));
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000784 }
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000785 }
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000786 GrGLenum error = CHECK_ALLOC_ERROR(this->glInterface());
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000787 if (error != GR_GL_NO_ERROR) {
788 succeeded = false;
789 } else {
790 // if we have data and we used TexStorage to create the texture, we
791 // now upload with TexSubImage.
792 if (NULL != data && useTexStorage) {
793 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D,
794 0, // level
795 left, top,
796 width, height,
797 externalFormat, externalType,
798 data));
799 }
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000800 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000801 } else {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000802 if (swFlipY || glFlipY) {
bsalomon@google.com6f379512011-11-16 20:36:03 +0000803 top = desc.fHeight - (top + height);
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000804 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000805 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D,
806 0, // level
807 left, top,
808 width, height,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000809 externalFormat, externalType, data));
810 }
811
812 if (restoreGLRowLength) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000813 GrAssert(this->glCaps().unpackRowLengthSupport());
bsalomon@google.com6f379512011-11-16 20:36:03 +0000814 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000815 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000816 if (glFlipY) {
817 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
818 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000819 return succeeded;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000820}
821
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000822namespace {
823bool renderbuffer_storage_msaa(GrGLContextInfo& ctxInfo,
824 int sampleCount,
825 GrGLenum format,
826 int width, int height) {
827 CLEAR_ERROR_BEFORE_ALLOC(ctxInfo.interface());
828 GrAssert(GrGLCaps::kNone_MSFBOType != ctxInfo.caps().msFBOType());
829 bool created = false;
830 if (GrGLCaps::kNVDesktop_CoverageAAType ==
831 ctxInfo.caps().coverageAAType()) {
832 const GrGLCaps::MSAACoverageMode& mode =
833 ctxInfo.caps().getMSAACoverageMode(sampleCount);
834 GL_ALLOC_CALL(ctxInfo.interface(),
835 RenderbufferStorageMultisampleCoverage(GR_GL_RENDERBUFFER,
836 mode.fCoverageSampleCnt,
837 mode.fColorSampleCnt,
838 format,
839 width, height));
840 created = (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctxInfo.interface()));
841 }
842 if (!created) {
bsalomon@google.comf6b070d2012-04-27 14:25:44 +0000843 // glRBMS will fail if requested samples is > max samples.
844 sampleCount = GrMin(sampleCount, ctxInfo.caps().maxSampleCount());
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000845 GL_ALLOC_CALL(ctxInfo.interface(),
846 RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
847 sampleCount,
848 format,
849 width, height));
850 created = (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctxInfo.interface()));
851 }
852 return created;
853}
854}
855
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000856bool GrGpuGL::createRenderTargetObjects(int width, int height,
857 GrGLuint texID,
858 GrGLRenderTarget::Desc* desc) {
859 desc->fMSColorRenderbufferID = 0;
860 desc->fRTFBOID = 0;
861 desc->fTexFBOID = 0;
bsalomon@google.com72830222013-01-23 20:25:22 +0000862 desc->fIsWrapped = false;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000863
864 GrGLenum status;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000865
bsalomon@google.comab15d612011-08-09 12:57:56 +0000866 GrGLenum msColorFormat = 0; // suppress warning
867
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000868 GL_CALL(GenFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000869 if (!desc->fTexFBOID) {
870 goto FAILED;
871 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000872
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000873
874 // If we are using multisampling we will create two FBOS. We render
875 // to one and then resolve to the texture bound to the other.
bsalomon@google.come269f212011-11-07 13:29:52 +0000876 if (desc->fSampleCnt > 0) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000877 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000878 goto FAILED;
879 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000880 GL_CALL(GenFramebuffers(1, &desc->fRTFBOID));
881 GL_CALL(GenRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000882 if (!desc->fRTFBOID ||
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000883 !desc->fMSColorRenderbufferID ||
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000884 !this->configToGLFormats(desc->fConfig,
885 // GLES requires sized internal formats
886 kES2_GrGLBinding == this->glBinding(),
887 &msColorFormat, NULL, NULL)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000888 goto FAILED;
889 }
890 } else {
891 desc->fRTFBOID = desc->fTexFBOID;
892 }
893
bsalomon@google.com0e9b41a2012-01-04 22:11:43 +0000894 // below here we may bind the FBO
bsalomon@google.comc811ea32012-05-21 15:33:09 +0000895 fHWBoundRenderTarget = NULL;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000896 if (desc->fRTFBOID != desc->fTexFBOID) {
bsalomon@google.com13ac3a32013-01-09 23:29:39 +0000897 GrAssert(desc->fSampleCnt > 0);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000898 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000899 desc->fMSColorRenderbufferID));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000900 if (!renderbuffer_storage_msaa(fGLContextInfo,
901 desc->fSampleCnt,
902 msColorFormat,
903 width, height)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000904 goto FAILED;
905 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000906 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fRTFBOID));
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000907 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000908 GR_GL_COLOR_ATTACHMENT0,
909 GR_GL_RENDERBUFFER,
910 desc->fMSColorRenderbufferID));
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000911 if (!this->glCaps().isConfigVerifiedColorAttachment(desc->fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000912 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
913 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
914 goto FAILED;
915 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000916 fGLContextInfo.caps().markConfigAsValidColorAttachment(
917 desc->fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000918 }
919 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000920 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000921
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000922 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
923 GR_GL_COLOR_ATTACHMENT0,
924 GR_GL_TEXTURE_2D,
925 texID, 0));
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000926 if (!this->glCaps().isConfigVerifiedColorAttachment(desc->fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000927 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
928 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
929 goto FAILED;
930 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000931 fGLContextInfo.caps().markConfigAsValidColorAttachment(desc->fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000932 }
933
934 return true;
935
936FAILED:
937 if (desc->fMSColorRenderbufferID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000938 GL_CALL(DeleteRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000939 }
940 if (desc->fRTFBOID != desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000941 GL_CALL(DeleteFramebuffers(1, &desc->fRTFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000942 }
943 if (desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000944 GL_CALL(DeleteFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000945 }
946 return false;
947}
948
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000949// good to set a break-point here to know when createTexture fails
950static GrTexture* return_null_texture() {
951// GrAssert(!"null texture");
952 return NULL;
953}
954
caryclark@google.comcf6285b2012-06-06 12:09:01 +0000955#if 0 && GR_DEBUG
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000956static size_t as_size_t(int x) {
957 return x;
958}
959#endif
960
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000961GrTexture* GrGpuGL::onCreateTexture(const GrTextureDesc& desc,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000962 const void* srcData,
963 size_t rowBytes) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000964
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000965 GrGLTexture::Desc glTexDesc;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000966 GrGLRenderTarget::Desc glRTDesc;
reed@google.comac10a2d2010-12-22 21:39:39 +0000967
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000968 // Attempt to catch un- or wrongly initialized sample counts;
969 GrAssert(desc.fSampleCnt >= 0 && desc.fSampleCnt <= 64);
970
robertphillips@google.com32716282012-06-04 12:48:45 +0000971 glTexDesc.fFlags = desc.fFlags;
bsalomon@google.com99621082011-11-15 16:47:16 +0000972 glTexDesc.fWidth = desc.fWidth;
973 glTexDesc.fHeight = desc.fHeight;
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000974 glTexDesc.fConfig = desc.fConfig;
robertphillips@google.com32716282012-06-04 12:48:45 +0000975 glTexDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.com72830222013-01-23 20:25:22 +0000976 glTexDesc.fIsWrapped = false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000977
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000978 glRTDesc.fMSColorRenderbufferID = 0;
979 glRTDesc.fRTFBOID = 0;
980 glRTDesc.fTexFBOID = 0;
bsalomon@google.com72830222013-01-23 20:25:22 +0000981 glRTDesc.fIsWrapped = false;
bsalomon@google.com64c4fe42011-11-05 14:51:01 +0000982 glRTDesc.fConfig = glTexDesc.fConfig;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000983
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000984 bool renderTarget = 0 != (desc.fFlags & kRenderTarget_GrTextureFlagBit);
reed@google.comac10a2d2010-12-22 21:39:39 +0000985
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000986 const Caps& caps = this->getCaps();
987
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000988 glTexDesc.fOrigin = resolve_origin(desc.fOrigin, renderTarget);
989 glRTDesc.fOrigin = glTexDesc.fOrigin;
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000990
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000991 glRTDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000992 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType() &&
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000993 desc.fSampleCnt) {
bsalomon@google.com945bbe12012-06-15 14:30:34 +0000994 //GrPrintf("MSAA RT requested but not supported on this platform.");
995 return return_null_texture();
reed@google.comac10a2d2010-12-22 21:39:39 +0000996 }
997
reed@google.comac10a2d2010-12-22 21:39:39 +0000998 if (renderTarget) {
bsalomon@google.comf6601872012-08-28 21:11:35 +0000999 if (glTexDesc.fWidth > caps.maxRenderTargetSize() ||
1000 glTexDesc.fHeight > caps.maxRenderTargetSize()) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001001 return return_null_texture();
1002 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001003 }
1004
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001005 GL_CALL(GenTextures(1, &glTexDesc.fTextureID));
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001006 if (renderTarget && this->glCaps().textureUsageSupport()) {
bsalomon@google.com07dd2bf2011-12-09 19:40:36 +00001007 // provides a hint about how this texture will be used
1008 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1009 GR_GL_TEXTURE_USAGE,
1010 GR_GL_FRAMEBUFFER_ATTACHMENT));
1011 }
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001012 if (!glTexDesc.fTextureID) {
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001013 return return_null_texture();
1014 }
1015
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001016 this->setSpareTextureUnit();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001017 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTexDesc.fTextureID));
bsalomon@google.come269f212011-11-07 13:29:52 +00001018
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001019 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
1020 // drivers have a bug where an FBO won't be complete if it includes a
1021 // texture that is not mipmap complete (considering the filter in use).
1022 GrGLTexture::TexParams initialTexParams;
1023 // we only set a subset here so invalidate first
1024 initialTexParams.invalidate();
1025 initialTexParams.fFilter = GR_GL_NEAREST;
1026 initialTexParams.fWrapS = GR_GL_CLAMP_TO_EDGE;
1027 initialTexParams.fWrapT = GR_GL_CLAMP_TO_EDGE;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001028 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1029 GR_GL_TEXTURE_MAG_FILTER,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001030 initialTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001031 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1032 GR_GL_TEXTURE_MIN_FILTER,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001033 initialTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001034 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1035 GR_GL_TEXTURE_WRAP_S,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001036 initialTexParams.fWrapS));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001037 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1038 GR_GL_TEXTURE_WRAP_T,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001039 initialTexParams.fWrapT));
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001040 if (!this->uploadTexData(glTexDesc, true, 0, 0,
1041 glTexDesc.fWidth, glTexDesc.fHeight,
1042 desc.fConfig, srcData, rowBytes)) {
1043 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
1044 return return_null_texture();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001045 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001046
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001047 GrGLTexture* tex;
reed@google.comac10a2d2010-12-22 21:39:39 +00001048 if (renderTarget) {
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001049 // unbind the texture from the texture unit before binding it to the frame buffer
1050 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, 0));
1051
bsalomon@google.com99621082011-11-15 16:47:16 +00001052 if (!this->createRenderTargetObjects(glTexDesc.fWidth,
1053 glTexDesc.fHeight,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001054 glTexDesc.fTextureID,
1055 &glRTDesc)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001056 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001057 return return_null_texture();
1058 }
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001059 tex = SkNEW_ARGS(GrGLTexture, (this, glTexDesc, glRTDesc));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001060 } else {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001061 tex = SkNEW_ARGS(GrGLTexture, (this, glTexDesc));
reed@google.comac10a2d2010-12-22 21:39:39 +00001062 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001063 tex->setCachedTexParams(initialTexParams, this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00001064#ifdef TRACE_TEXTURE_CREATION
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001065 GrPrintf("--- new texture [%d] size=(%d %d) config=%d\n",
1066 glTexDesc.fTextureID, desc.fWidth, desc.fHeight, desc.fConfig);
reed@google.comac10a2d2010-12-22 21:39:39 +00001067#endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001068 return tex;
1069}
1070
1071namespace {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001072
1073const GrGLuint kUnknownBitCount = GrGLStencilBuffer::kUnknownBitCount;
1074
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001075void inline get_stencil_rb_sizes(const GrGLInterface* gl,
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001076 GrGLuint rb,
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001077 GrGLStencilBuffer::Format* format) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001078 // we shouldn't ever know one size and not the other
1079 GrAssert((kUnknownBitCount == format->fStencilBits) ==
1080 (kUnknownBitCount == format->fTotalBits));
1081 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001082 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001083 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1084 (GrGLint*)&format->fStencilBits);
1085 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001086 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001087 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1088 (GrGLint*)&format->fTotalBits);
1089 format->fTotalBits += format->fStencilBits;
1090 } else {
1091 format->fTotalBits = format->fStencilBits;
1092 }
1093 }
1094}
1095}
1096
1097bool GrGpuGL::createStencilBufferForRenderTarget(GrRenderTarget* rt,
1098 int width, int height) {
1099
1100 // All internally created RTs are also textures. We don't create
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +00001101 // 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 +00001102 GrAssert(rt->asTexture());
bsalomon@google.com99621082011-11-15 16:47:16 +00001103 GrAssert(width >= rt->width());
1104 GrAssert(height >= rt->height());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001105
1106 int samples = rt->numSamples();
1107 GrGLuint sbID;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001108 GL_CALL(GenRenderbuffers(1, &sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001109 if (!sbID) {
1110 return false;
1111 }
1112
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001113 int stencilFmtCnt = this->glCaps().stencilFormats().count();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001114 for (int i = 0; i < stencilFmtCnt; ++i) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001115 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001116 // we start with the last stencil format that succeeded in hopes
1117 // that we won't go through this loop more than once after the
1118 // first (painful) stencil creation.
1119 int sIdx = (i + fLastSuccessfulStencilFmtIdx) % stencilFmtCnt;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001120 const GrGLCaps::StencilFormat& sFmt =
1121 this->glCaps().stencilFormats()[sIdx];
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001122 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001123 // we do this "if" so that we don't call the multisample
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001124 // version on a GL that doesn't have an MSAA extension.
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001125 bool created;
1126 if (samples > 0) {
1127 created = renderbuffer_storage_msaa(fGLContextInfo,
1128 samples,
1129 sFmt.fInternalFormat,
1130 width, height);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001131 } else {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001132 GL_ALLOC_CALL(this->glInterface(),
1133 RenderbufferStorage(GR_GL_RENDERBUFFER,
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001134 sFmt.fInternalFormat,
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001135 width, height));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001136 created =
1137 (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(this->glInterface()));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001138 }
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001139 if (created) {
1140 // After sized formats we attempt an unsized format and take
1141 // whatever sizes GL gives us. In that case we query for the size.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001142 GrGLStencilBuffer::Format format = sFmt;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001143 get_stencil_rb_sizes(this->glInterface(), sbID, &format);
bsalomon@google.com72830222013-01-23 20:25:22 +00001144 static const bool kIsWrapped = false;
robertphillips@google.comf2e93fc2012-09-05 19:44:18 +00001145 SkAutoTUnref<GrStencilBuffer> sb(SkNEW_ARGS(GrGLStencilBuffer,
bsalomon@google.com72830222013-01-23 20:25:22 +00001146 (this, kIsWrapped, sbID, width, height,
robertphillips@google.comf2e93fc2012-09-05 19:44:18 +00001147 samples, format)));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001148 if (this->attachStencilBufferToRenderTarget(sb, rt)) {
1149 fLastSuccessfulStencilFmtIdx = sIdx;
robertphillips@google.com9fbcad02012-09-09 14:44:15 +00001150 sb->transferToCache();
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001151 rt->setStencilBuffer(sb);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001152 return true;
1153 }
1154 sb->abandon(); // otherwise we lose sbID
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001155 }
1156 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001157 GL_CALL(DeleteRenderbuffers(1, &sbID));
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001158 return false;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001159}
1160
1161bool GrGpuGL::attachStencilBufferToRenderTarget(GrStencilBuffer* sb,
1162 GrRenderTarget* rt) {
1163 GrGLRenderTarget* glrt = (GrGLRenderTarget*) rt;
1164
1165 GrGLuint fbo = glrt->renderFBOID();
1166
1167 if (NULL == sb) {
1168 if (NULL != rt->getStencilBuffer()) {
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_STENCIL_ATTACHMENT,
1171 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001172 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001173 GR_GL_DEPTH_ATTACHMENT,
1174 GR_GL_RENDERBUFFER, 0));
1175#if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001176 GrGLenum status;
1177 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001178 GrAssert(GR_GL_FRAMEBUFFER_COMPLETE == status);
1179#endif
1180 }
1181 return true;
1182 } else {
1183 GrGLStencilBuffer* glsb = (GrGLStencilBuffer*) sb;
1184 GrGLuint rb = glsb->renderbufferID();
1185
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001186 fHWBoundRenderTarget = NULL;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001187 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, fbo));
1188 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001189 GR_GL_STENCIL_ATTACHMENT,
1190 GR_GL_RENDERBUFFER, rb));
1191 if (glsb->format().fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001192 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001193 GR_GL_DEPTH_ATTACHMENT,
1194 GR_GL_RENDERBUFFER, rb));
1195 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001196 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001197 GR_GL_DEPTH_ATTACHMENT,
1198 GR_GL_RENDERBUFFER, 0));
reed@google.comac10a2d2010-12-22 21:39:39 +00001199 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001200
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001201 GrGLenum status;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001202 if (!this->glCaps().isColorConfigAndStencilFormatVerified(rt->config(),
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001203 glsb->format())) {
1204 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1205 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001206 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001207 GR_GL_STENCIL_ATTACHMENT,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001208 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001209 if (glsb->format().fPacked) {
1210 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1211 GR_GL_DEPTH_ATTACHMENT,
1212 GR_GL_RENDERBUFFER, 0));
1213 }
1214 return false;
1215 } else {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001216 fGLContextInfo.caps().markColorConfigAndStencilFormatAsVerified(
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001217 rt->config(),
1218 glsb->format());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001219 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001220 }
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001221 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001222 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001223}
1224
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001225////////////////////////////////////////////////////////////////////////////////
1226
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001227GrVertexBuffer* GrGpuGL::onCreateVertexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001228 GrGLuint id;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001229 GL_CALL(GenBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001230 if (id) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001231 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, id));
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001232 fHWGeometryState.fArrayPtrsDirty = true;
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001233 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001234 // make sure driver can allocate memory for this buffer
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001235 GL_ALLOC_CALL(this->glInterface(),
1236 BufferData(GR_GL_ARRAY_BUFFER,
1237 size,
1238 NULL, // data ptr
1239 dynamic ? GR_GL_DYNAMIC_DRAW :
1240 GR_GL_STATIC_DRAW));
1241 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001242 GL_CALL(DeleteBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001243 // deleting bound buffer does implicit bind to 0
1244 fHWGeometryState.fVertexBuffer = NULL;
1245 return NULL;
1246 }
bsalomon@google.com72830222013-01-23 20:25:22 +00001247 static const bool kIsWrapped = false;
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001248 GrGLVertexBuffer* vertexBuffer = SkNEW_ARGS(GrGLVertexBuffer,
bsalomon@google.com72830222013-01-23 20:25:22 +00001249 (this, kIsWrapped, id,
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001250 size, dynamic));
reed@google.comac10a2d2010-12-22 21:39:39 +00001251 fHWGeometryState.fVertexBuffer = vertexBuffer;
1252 return vertexBuffer;
1253 }
1254 return NULL;
1255}
1256
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001257GrIndexBuffer* GrGpuGL::onCreateIndexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001258 GrGLuint id;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001259 GL_CALL(GenBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001260 if (id) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001261 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, id));
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001262 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001263 // make sure driver can allocate memory for this buffer
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001264 GL_ALLOC_CALL(this->glInterface(),
1265 BufferData(GR_GL_ELEMENT_ARRAY_BUFFER,
1266 size,
1267 NULL, // data ptr
1268 dynamic ? GR_GL_DYNAMIC_DRAW :
1269 GR_GL_STATIC_DRAW));
1270 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001271 GL_CALL(DeleteBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001272 // deleting bound buffer does implicit bind to 0
1273 fHWGeometryState.fIndexBuffer = NULL;
1274 return NULL;
1275 }
bsalomon@google.com72830222013-01-23 20:25:22 +00001276 static const bool kIsWrapped = false;
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001277 GrIndexBuffer* indexBuffer = SkNEW_ARGS(GrGLIndexBuffer,
bsalomon@google.com72830222013-01-23 20:25:22 +00001278 (this, kIsWrapped, id, size, dynamic));
reed@google.comac10a2d2010-12-22 21:39:39 +00001279 fHWGeometryState.fIndexBuffer = indexBuffer;
1280 return indexBuffer;
1281 }
1282 return NULL;
1283}
1284
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00001285GrPath* GrGpuGL::onCreatePath(const SkPath& inPath) {
bsalomon@google.comf6601872012-08-28 21:11:35 +00001286 GrAssert(fCaps.pathStencilingSupport());
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001287 return SkNEW_ARGS(GrGLPath, (this, inPath));
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00001288}
1289
bsalomon@google.coma3201942012-06-21 19:58:20 +00001290void GrGpuGL::flushScissor() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001291 const GrDrawState& drawState = this->getDrawState();
1292 const GrGLRenderTarget* rt =
1293 static_cast<const GrGLRenderTarget*>(drawState.getRenderTarget());
1294
1295 GrAssert(NULL != rt);
1296 const GrGLIRect& vp = rt->getViewport();
reed@google.comac10a2d2010-12-22 21:39:39 +00001297
bsalomon@google.coma3201942012-06-21 19:58:20 +00001298 if (fScissorState.fEnabled) {
1299 GrGLIRect scissor;
1300 scissor.setRelativeTo(vp,
1301 fScissorState.fRect.fLeft,
1302 fScissorState.fRect.fTop,
1303 fScissorState.fRect.width(),
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001304 fScissorState.fRect.height(),
1305 rt->origin());
bsalomon@google.coma3201942012-06-21 19:58:20 +00001306 // if the scissor fully contains the viewport then we fall through and
1307 // disable the scissor test.
1308 if (!scissor.contains(vp)) {
1309 if (fHWScissorSettings.fRect != scissor) {
1310 scissor.pushToGLScissor(this->glInterface());
1311 fHWScissorSettings.fRect = scissor;
1312 }
1313 if (kYes_TriState != fHWScissorSettings.fEnabled) {
1314 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1315 fHWScissorSettings.fEnabled = kYes_TriState;
1316 }
1317 return;
1318 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001319 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001320 if (kNo_TriState != fHWScissorSettings.fEnabled) {
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001321 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.coma3201942012-06-21 19:58:20 +00001322 fHWScissorSettings.fEnabled = kNo_TriState;
1323 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001324 }
1325}
1326
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001327void GrGpuGL::onClear(const GrIRect* rect, GrColor color) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001328 const GrDrawState& drawState = this->getDrawState();
1329 const GrRenderTarget* rt = drawState.getRenderTarget();
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001330 // parent class should never let us get here with no RT
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001331 GrAssert(NULL != rt);
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001332
bsalomon@google.com74b98712011-11-11 19:46:16 +00001333 GrIRect clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001334 if (NULL != rect) {
1335 // flushScissor expects rect to be clipped to the target.
bsalomon@google.com74b98712011-11-11 19:46:16 +00001336 clippedRect = *rect;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001337 GrIRect rtRect = SkIRect::MakeWH(rt->width(), rt->height());
bsalomon@google.com74b98712011-11-11 19:46:16 +00001338 if (clippedRect.intersect(rtRect)) {
1339 rect = &clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001340 } else {
1341 return;
1342 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001343 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001344 this->flushRenderTarget(rect);
bsalomon@google.coma3201942012-06-21 19:58:20 +00001345 GrAutoTRestore<ScissorState> asr(&fScissorState);
1346 fScissorState.fEnabled = (NULL != rect);
1347 if (fScissorState.fEnabled) {
1348 fScissorState.fRect = *rect;
1349 }
1350 this->flushScissor();
bsalomon@google.com74b98712011-11-11 19:46:16 +00001351
1352 GrGLfloat r, g, b, a;
1353 static const GrGLfloat scale255 = 1.f / 255.f;
1354 a = GrColorUnpackA(color) * scale255;
1355 GrGLfloat scaleRGB = scale255;
bsalomon@google.com74b98712011-11-11 19:46:16 +00001356 r = GrColorUnpackR(color) * scaleRGB;
1357 g = GrColorUnpackG(color) * scaleRGB;
1358 b = GrColorUnpackB(color) * scaleRGB;
1359
1360 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00001361 fHWWriteToColor = kYes_TriState;
bsalomon@google.com74b98712011-11-11 19:46:16 +00001362 GL_CALL(ClearColor(r, g, b, a));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001363 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001364}
1365
bsalomon@google.comedc177d2011-08-05 15:46:40 +00001366void GrGpuGL::clearStencil() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001367 if (NULL == this->getDrawState().getRenderTarget()) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001368 return;
1369 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001370
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001371 this->flushRenderTarget(&GrIRect::EmptyIRect());
1372
bsalomon@google.coma3201942012-06-21 19:58:20 +00001373 GrAutoTRestore<ScissorState> asr(&fScissorState);
1374 fScissorState.fEnabled = false;
1375 this->flushScissor();
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001376
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001377 GL_CALL(StencilMask(0xffffffff));
1378 GL_CALL(ClearStencil(0));
1379 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001380 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001381}
1382
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001383void GrGpuGL::clearStencilClip(const GrIRect& rect, bool insideClip) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001384 const GrDrawState& drawState = this->getDrawState();
1385 const GrRenderTarget* rt = drawState.getRenderTarget();
1386 GrAssert(NULL != rt);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001387
1388 // this should only be called internally when we know we have a
1389 // stencil buffer.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001390 GrAssert(NULL != rt->getStencilBuffer());
1391 GrGLint stencilBitCount = rt->getStencilBuffer()->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001392#if 0
reed@google.comac10a2d2010-12-22 21:39:39 +00001393 GrAssert(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00001394 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001395#else
1396 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001397 // ANGLE a partial stencil mask will cause clears to be
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001398 // turned into draws. Our contract on GrDrawTarget says that
1399 // changing the clip between stencil passes may or may not
1400 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00001401 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001402#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001403 GrGLint value;
1404 if (insideClip) {
1405 value = (1 << (stencilBitCount - 1));
1406 } else {
1407 value = 0;
1408 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001409 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.coma3201942012-06-21 19:58:20 +00001410
1411 GrAutoTRestore<ScissorState> asr(&fScissorState);
1412 fScissorState.fEnabled = true;
1413 fScissorState.fRect = rect;
1414 this->flushScissor();
1415
caryclark@google.comcf6285b2012-06-06 12:09:01 +00001416 GL_CALL(StencilMask((uint32_t) clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001417 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001418 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001419 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001420}
1421
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001422void GrGpuGL::onForceRenderTargetFlush() {
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001423 this->flushRenderTarget(&GrIRect::EmptyIRect());
reed@google.comac10a2d2010-12-22 21:39:39 +00001424}
1425
bsalomon@google.comc4364992011-11-07 15:54:49 +00001426bool GrGpuGL::readPixelsWillPayForYFlip(GrRenderTarget* renderTarget,
1427 int left, int top,
1428 int width, int height,
1429 GrPixelConfig config,
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001430 size_t rowBytes) const {
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001431 // If this rendertarget is aready TopLeft, we don't need to flip.
1432 if (kTopLeft_GrSurfaceOrigin == renderTarget->origin()) {
1433 return false;
1434 }
1435
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001436 // if GL can do the flip then we'll never pay for it.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001437 if (this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001438 return false;
1439 }
1440
1441 // If we have to do memcpy to handle non-trim rowBytes then we
bsalomon@google.com7107fa72011-11-10 14:54:14 +00001442 // get the flip for free. Otherwise it costs.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001443 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001444 return true;
1445 }
1446 // If we have to do memcpys to handle rowBytes then y-flip is free
1447 // Note the rowBytes might be tight to the passed in data, but if data
1448 // gets clipped in x to the target the rowBytes will no longer be tight.
1449 if (left >= 0 && (left + width) < renderTarget->width()) {
1450 return 0 == rowBytes ||
1451 GrBytesPerPixel(config) * width == rowBytes;
1452 } else {
1453 return false;
1454 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001455}
1456
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001457bool GrGpuGL::onReadPixels(GrRenderTarget* target,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001458 int left, int top,
1459 int width, int height,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001460 GrPixelConfig config,
1461 void* buffer,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001462 size_t rowBytes) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001463 GrGLenum format;
1464 GrGLenum type;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001465 bool flipY = kBottomLeft_GrSurfaceOrigin == target->origin();
bsalomon@google.com280e99f2012-01-05 16:17:38 +00001466 if (!this->configToGLFormats(config, false, NULL, &format, &type)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001467 return false;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001468 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001469 size_t bpp = GrBytesPerPixel(config);
1470 if (!adjust_pixel_ops_params(target->width(), target->height(), bpp,
1471 &left, &top, &width, &height,
1472 const_cast<const void**>(&buffer),
1473 &rowBytes)) {
1474 return false;
1475 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001476
bsalomon@google.comc6980972011-11-02 19:57:21 +00001477 // resolve the render target if necessary
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001478 GrGLRenderTarget* tgt = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001479 GrDrawState::AutoRenderTargetRestore artr;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001480 switch (tgt->getResolveType()) {
1481 case GrGLRenderTarget::kCantResolve_ResolveType:
1482 return false;
1483 case GrGLRenderTarget::kAutoResolves_ResolveType:
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001484 artr.set(this->drawState(), target);
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001485 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001486 break;
1487 case GrGLRenderTarget::kCanResolve_ResolveType:
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001488 this->onResolveRenderTarget(tgt);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001489 // we don't track the state of the READ FBO ID.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001490 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1491 tgt->textureFBOID()));
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001492 break;
1493 default:
1494 GrCrash("Unknown resolve type");
reed@google.comac10a2d2010-12-22 21:39:39 +00001495 }
1496
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001497 const GrGLIRect& glvp = tgt->getViewport();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001498
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001499 // the read rect is viewport-relative
1500 GrGLIRect readRect;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001501 readRect.setRelativeTo(glvp, left, top, width, height, target->origin());
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001502
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001503 size_t tightRowBytes = bpp * width;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001504 if (0 == rowBytes) {
1505 rowBytes = tightRowBytes;
1506 }
1507 size_t readDstRowBytes = tightRowBytes;
1508 void* readDst = buffer;
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001509
bsalomon@google.comc6980972011-11-02 19:57:21 +00001510 // determine if GL can read using the passed rowBytes or if we need
1511 // a scratch buffer.
1512 SkAutoSMalloc<32 * sizeof(GrColor)> scratch;
1513 if (rowBytes != tightRowBytes) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001514 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.comc6980972011-11-02 19:57:21 +00001515 GrAssert(!(rowBytes % sizeof(GrColor)));
1516 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, rowBytes / sizeof(GrColor)));
1517 readDstRowBytes = rowBytes;
1518 } else {
1519 scratch.reset(tightRowBytes * height);
1520 readDst = scratch.get();
1521 }
1522 }
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001523 if (flipY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001524 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 1));
1525 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001526 GL_CALL(ReadPixels(readRect.fLeft, readRect.fBottom,
1527 readRect.fWidth, readRect.fHeight,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001528 format, type, readDst));
1529 if (readDstRowBytes != tightRowBytes) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001530 GrAssert(this->glCaps().packRowLengthSupport());
bsalomon@google.comc6980972011-11-02 19:57:21 +00001531 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
1532 }
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001533 if (flipY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001534 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 0));
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001535 flipY = false;
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001536 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001537
1538 // now reverse the order of the rows, since GL's are bottom-to-top, but our
bsalomon@google.comc6980972011-11-02 19:57:21 +00001539 // API presents top-to-bottom. We must preserve the padding contents. Note
1540 // that the above readPixels did not overwrite the padding.
1541 if (readDst == buffer) {
1542 GrAssert(rowBytes == readDstRowBytes);
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001543 if (flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001544 scratch.reset(tightRowBytes);
1545 void* tmpRow = scratch.get();
1546 // flip y in-place by rows
1547 const int halfY = height >> 1;
1548 char* top = reinterpret_cast<char*>(buffer);
1549 char* bottom = top + (height - 1) * rowBytes;
1550 for (int y = 0; y < halfY; y++) {
1551 memcpy(tmpRow, top, tightRowBytes);
1552 memcpy(top, bottom, tightRowBytes);
1553 memcpy(bottom, tmpRow, tightRowBytes);
1554 top += rowBytes;
1555 bottom -= rowBytes;
1556 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001557 }
1558 } else {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001559 GrAssert(readDst != buffer); GrAssert(rowBytes != tightRowBytes);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001560 // copy from readDst to buffer while flipping y
caryclark@google.comcf6285b2012-06-06 12:09:01 +00001561 // const int halfY = height >> 1;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001562 const char* src = reinterpret_cast<const char*>(readDst);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001563 char* dst = reinterpret_cast<char*>(buffer);
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001564 if (flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001565 dst += (height-1) * rowBytes;
1566 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001567 for (int y = 0; y < height; y++) {
1568 memcpy(dst, src, tightRowBytes);
1569 src += readDstRowBytes;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001570 if (!flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001571 dst += rowBytes;
1572 } else {
1573 dst -= rowBytes;
1574 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001575 }
1576 }
1577 return true;
1578}
1579
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001580void GrGpuGL::flushRenderTarget(const GrIRect* bound) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001581
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001582 GrGLRenderTarget* rt =
1583 static_cast<GrGLRenderTarget*>(this->drawState()->getRenderTarget());
1584 GrAssert(NULL != rt);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001585
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001586 if (fHWBoundRenderTarget != rt) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001587 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, rt->renderFBOID()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001588 #if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001589 GrGLenum status;
1590 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001591 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
reed@google.comb9255d52011-06-13 18:54:59 +00001592 GrPrintf("GrGpuGL::flushRenderTarget glCheckFramebufferStatus %x\n", status);
reed@google.comac10a2d2010-12-22 21:39:39 +00001593 }
1594 #endif
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001595 fHWBoundRenderTarget = rt;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001596 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.coma3201942012-06-21 19:58:20 +00001597 if (fHWViewport != vp) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001598 vp.pushToGLViewport(this->glInterface());
bsalomon@google.coma3201942012-06-21 19:58:20 +00001599 fHWViewport = vp;
reed@google.comac10a2d2010-12-22 21:39:39 +00001600 }
1601 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001602 if (NULL == bound || !bound->isEmpty()) {
1603 rt->flagAsNeedingResolve(bound);
1604 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001605}
1606
twiz@google.com0f31ca72011-03-18 17:38:11 +00001607GrGLenum gPrimitiveType2GLMode[] = {
1608 GR_GL_TRIANGLES,
1609 GR_GL_TRIANGLE_STRIP,
1610 GR_GL_TRIANGLE_FAN,
1611 GR_GL_POINTS,
1612 GR_GL_LINES,
1613 GR_GL_LINE_STRIP
reed@google.comac10a2d2010-12-22 21:39:39 +00001614};
1615
bsalomon@google.comd302f142011-03-03 13:54:13 +00001616#define SWAP_PER_DRAW 0
1617
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001618#if SWAP_PER_DRAW
bsalomon@google.comd302f142011-03-03 13:54:13 +00001619 #if GR_MAC_BUILD
1620 #include <AGL/agl.h>
1621 #elif GR_WIN32_BUILD
bsalomon@google.comce7357d2012-06-25 17:49:25 +00001622 #include <gl/GL.h>
bsalomon@google.comd302f142011-03-03 13:54:13 +00001623 void SwapBuf() {
1624 DWORD procID = GetCurrentProcessId();
1625 HWND hwnd = GetTopWindow(GetDesktopWindow());
1626 while(hwnd) {
1627 DWORD wndProcID = 0;
1628 GetWindowThreadProcessId(hwnd, &wndProcID);
1629 if(wndProcID == procID) {
1630 SwapBuffers(GetDC(hwnd));
1631 }
1632 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
1633 }
1634 }
1635 #endif
1636#endif
1637
bsalomon@google.com74749cd2013-01-30 16:12:41 +00001638void GrGpuGL::onGpuDraw(const DrawInfo& info) {
1639 int extraStartIndexOffset;
1640 this->setupGeometry(info, &extraStartIndexOffset);
reed@google.comac10a2d2010-12-22 21:39:39 +00001641
bsalomon@google.com74749cd2013-01-30 16:12:41 +00001642 GrAssert((size_t)info.primitiveType() < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001643 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1644
bsalomon@google.com74749cd2013-01-30 16:12:41 +00001645 if (info.isIndexed()) {
1646 GrAssert(NULL != fHWGeometryState.fIndexBuffer);
1647 GrGLvoid* indices = (GrGLvoid*)(sizeof(uint16_t) * (info.startIndex() +
1648 extraStartIndexOffset));
1649 // info.startVertex() was accounted for by setupGeometry.
1650 GL_CALL(DrawElements(gPrimitiveType2GLMode[info.primitiveType()],
1651 info.indexCount(),
1652 GR_GL_UNSIGNED_SHORT,
1653 indices));
1654 } else {
1655 // Pass 0 for parameter first. We have to adjust glVertexAttribPointer() to account for
1656 // startVertex in the DrawElements case. So we always rely on setupGeometry to have
1657 // accounted for startVertex.
1658 GL_CALL(DrawArrays(gPrimitiveType2GLMode[info.primitiveType()], 0, info.vertexCount()));
1659 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00001660#if SWAP_PER_DRAW
1661 glFlush();
1662 #if GR_MAC_BUILD
1663 aglSwapBuffers(aglGetCurrentContext());
1664 int set_a_break_pt_here = 9;
1665 aglSwapBuffers(aglGetCurrentContext());
1666 #elif GR_WIN32_BUILD
1667 SwapBuf();
1668 int set_a_break_pt_here = 9;
1669 SwapBuf();
1670 #endif
1671#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001672}
1673
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001674namespace {
bsalomon@google.com100abf42012-09-05 17:40:04 +00001675
1676static const uint16_t kOnes16 = static_cast<uint16_t>(~0);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001677const GrStencilSettings& winding_nv_path_stencil_settings() {
1678 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
1679 kIncClamp_StencilOp,
1680 kIncClamp_StencilOp,
1681 kAlwaysIfInClip_StencilFunc,
bsalomon@google.com100abf42012-09-05 17:40:04 +00001682 kOnes16, kOnes16, kOnes16);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001683 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
1684}
1685const GrStencilSettings& even_odd_nv_path_stencil_settings() {
1686 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
1687 kInvert_StencilOp,
1688 kInvert_StencilOp,
1689 kAlwaysIfInClip_StencilFunc,
bsalomon@google.com100abf42012-09-05 17:40:04 +00001690 kOnes16, kOnes16, kOnes16);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001691 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
1692}
1693}
1694
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001695void GrGpuGL::setStencilPathSettings(const GrPath&,
sugoi@google.com12b4e272012-12-06 20:13:11 +00001696 SkPath::FillType fill,
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001697 GrStencilSettings* settings) {
1698 switch (fill) {
sugoi@google.com12b4e272012-12-06 20:13:11 +00001699 case SkPath::kEvenOdd_FillType:
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001700 *settings = even_odd_nv_path_stencil_settings();
1701 return;
sugoi@google.com12b4e272012-12-06 20:13:11 +00001702 case SkPath::kWinding_FillType:
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001703 *settings = winding_nv_path_stencil_settings();
1704 return;
1705 default:
1706 GrCrash("Unexpected path fill.");
1707 }
1708}
1709
sugoi@google.com12b4e272012-12-06 20:13:11 +00001710void GrGpuGL::onGpuStencilPath(const GrPath* path, SkPath::FillType fill) {
bsalomon@google.comf6601872012-08-28 21:11:35 +00001711 GrAssert(fCaps.pathStencilingSupport());
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001712
1713 GrGLuint id = static_cast<const GrGLPath*>(path)->pathID();
1714 GrDrawState* drawState = this->drawState();
1715 GrAssert(NULL != drawState->getRenderTarget());
1716 if (NULL == drawState->getRenderTarget()->getStencilBuffer()) {
1717 return;
1718 }
1719
1720 // Decide how to manipulate the stencil buffer based on the fill rule.
1721 // Also, assert that the stencil settings we set in setStencilPathSettings
1722 // are present.
1723 GrAssert(!fStencilSettings.isTwoSided());
1724 GrGLenum fillMode;
1725 switch (fill) {
sugoi@google.com12b4e272012-12-06 20:13:11 +00001726 case SkPath::kWinding_FillType:
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001727 fillMode = GR_GL_COUNT_UP;
1728 GrAssert(kIncClamp_StencilOp ==
1729 fStencilSettings.passOp(GrStencilSettings::kFront_Face));
1730 GrAssert(kIncClamp_StencilOp ==
1731 fStencilSettings.failOp(GrStencilSettings::kFront_Face));
1732 break;
sugoi@google.com12b4e272012-12-06 20:13:11 +00001733 case SkPath::kEvenOdd_FillType:
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001734 fillMode = GR_GL_INVERT;
1735 GrAssert(kInvert_StencilOp ==
1736 fStencilSettings.passOp(GrStencilSettings::kFront_Face));
1737 GrAssert(kInvert_StencilOp ==
1738 fStencilSettings.failOp(GrStencilSettings::kFront_Face));
1739 break;
1740 default:
1741 // Only the above two fill rules are allowed.
1742 GrCrash("Unexpected path fill.");
bsalomon@google.com548a4332012-07-11 19:45:22 +00001743 return; // suppress unused var warning.
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001744 }
1745 GrGLint writeMask = fStencilSettings.writeMask(GrStencilSettings::kFront_Face);
1746 GL_CALL(StencilFillPath(id, fillMode, writeMask));
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00001747}
1748
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001749void GrGpuGL::onResolveRenderTarget(GrRenderTarget* target) {
1750
1751 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(target);
reed@google.comac10a2d2010-12-22 21:39:39 +00001752
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001753 if (rt->needsResolve()) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001754 GrAssert(GrGLCaps::kNone_MSFBOType != this->glCaps().msFBOType());
reed@google.comac10a2d2010-12-22 21:39:39 +00001755 GrAssert(rt->textureFBOID() != rt->renderFBOID());
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001756 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1757 rt->renderFBOID()));
1758 GL_CALL(BindFramebuffer(GR_GL_DRAW_FRAMEBUFFER,
1759 rt->textureFBOID()));
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001760 // make sure we go through flushRenderTarget() since we've modified
1761 // the bound DRAW FBO ID.
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001762 fHWBoundRenderTarget = NULL;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001763 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001764 const GrIRect dirtyRect = rt->getResolveRect();
1765 GrGLIRect r;
bsalomon@google.coma3201942012-06-21 19:58:20 +00001766 r.setRelativeTo(vp, dirtyRect.fLeft, dirtyRect.fTop,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001767 dirtyRect.width(), dirtyRect.height(), target->origin());
reed@google.comac10a2d2010-12-22 21:39:39 +00001768
bsalomon@google.coma3201942012-06-21 19:58:20 +00001769 GrAutoTRestore<ScissorState> asr;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001770 if (GrGLCaps::kAppleES_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001771 // Apple's extension uses the scissor as the blit bounds.
bsalomon@google.coma3201942012-06-21 19:58:20 +00001772 asr.reset(&fScissorState);
1773 fScissorState.fEnabled = true;
1774 fScissorState.fRect = dirtyRect;
1775 this->flushScissor();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001776 GL_CALL(ResolveMultisampleFramebuffer());
reed@google.comac10a2d2010-12-22 21:39:39 +00001777 } else {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001778 if (GrGLCaps::kDesktopARB_MSFBOType != this->glCaps().msFBOType()) {
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001779 // this respects the scissor during the blit, so disable it.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001780 GrAssert(GrGLCaps::kDesktopEXT_MSFBOType ==
1781 this->glCaps().msFBOType());
bsalomon@google.coma3201942012-06-21 19:58:20 +00001782 asr.reset(&fScissorState);
1783 fScissorState.fEnabled = false;
1784 this->flushScissor();
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001785 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001786 int right = r.fLeft + r.fWidth;
1787 int top = r.fBottom + r.fHeight;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001788 GL_CALL(BlitFramebuffer(r.fLeft, r.fBottom, right, top,
1789 r.fLeft, r.fBottom, right, top,
1790 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001791 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001792 rt->flagAsResolved();
reed@google.comac10a2d2010-12-22 21:39:39 +00001793 }
1794}
1795
bsalomon@google.com411dad02012-06-05 20:24:20 +00001796namespace {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001797
bsalomon@google.com411dad02012-06-05 20:24:20 +00001798GrGLenum gr_to_gl_stencil_func(GrStencilFunc basicFunc) {
1799 static const GrGLenum gTable[] = {
1800 GR_GL_ALWAYS, // kAlways_StencilFunc
1801 GR_GL_NEVER, // kNever_StencilFunc
1802 GR_GL_GREATER, // kGreater_StencilFunc
1803 GR_GL_GEQUAL, // kGEqual_StencilFunc
1804 GR_GL_LESS, // kLess_StencilFunc
1805 GR_GL_LEQUAL, // kLEqual_StencilFunc,
1806 GR_GL_EQUAL, // kEqual_StencilFunc,
1807 GR_GL_NOTEQUAL, // kNotEqual_StencilFunc,
1808 };
1809 GR_STATIC_ASSERT(GR_ARRAY_COUNT(gTable) == kBasicStencilFuncCount);
1810 GR_STATIC_ASSERT(0 == kAlways_StencilFunc);
1811 GR_STATIC_ASSERT(1 == kNever_StencilFunc);
1812 GR_STATIC_ASSERT(2 == kGreater_StencilFunc);
1813 GR_STATIC_ASSERT(3 == kGEqual_StencilFunc);
1814 GR_STATIC_ASSERT(4 == kLess_StencilFunc);
1815 GR_STATIC_ASSERT(5 == kLEqual_StencilFunc);
1816 GR_STATIC_ASSERT(6 == kEqual_StencilFunc);
1817 GR_STATIC_ASSERT(7 == kNotEqual_StencilFunc);
1818 GrAssert((unsigned) basicFunc < kBasicStencilFuncCount);
1819
1820 return gTable[basicFunc];
1821}
1822
1823GrGLenum gr_to_gl_stencil_op(GrStencilOp op) {
1824 static const GrGLenum gTable[] = {
1825 GR_GL_KEEP, // kKeep_StencilOp
1826 GR_GL_REPLACE, // kReplace_StencilOp
1827 GR_GL_INCR_WRAP, // kIncWrap_StencilOp
1828 GR_GL_INCR, // kIncClamp_StencilOp
1829 GR_GL_DECR_WRAP, // kDecWrap_StencilOp
1830 GR_GL_DECR, // kDecClamp_StencilOp
1831 GR_GL_ZERO, // kZero_StencilOp
1832 GR_GL_INVERT, // kInvert_StencilOp
1833 };
1834 GR_STATIC_ASSERT(GR_ARRAY_COUNT(gTable) == kStencilOpCount);
1835 GR_STATIC_ASSERT(0 == kKeep_StencilOp);
1836 GR_STATIC_ASSERT(1 == kReplace_StencilOp);
1837 GR_STATIC_ASSERT(2 == kIncWrap_StencilOp);
1838 GR_STATIC_ASSERT(3 == kIncClamp_StencilOp);
1839 GR_STATIC_ASSERT(4 == kDecWrap_StencilOp);
1840 GR_STATIC_ASSERT(5 == kDecClamp_StencilOp);
1841 GR_STATIC_ASSERT(6 == kZero_StencilOp);
1842 GR_STATIC_ASSERT(7 == kInvert_StencilOp);
1843 GrAssert((unsigned) op < kStencilOpCount);
1844 return gTable[op];
1845}
1846
1847void set_gl_stencil(const GrGLInterface* gl,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001848 const GrStencilSettings& settings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00001849 GrGLenum glFace,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001850 GrStencilSettings::Face grFace) {
1851 GrGLenum glFunc = gr_to_gl_stencil_func(settings.func(grFace));
1852 GrGLenum glFailOp = gr_to_gl_stencil_op(settings.failOp(grFace));
1853 GrGLenum glPassOp = gr_to_gl_stencil_op(settings.passOp(grFace));
1854
1855 GrGLint ref = settings.funcRef(grFace);
1856 GrGLint mask = settings.funcMask(grFace);
1857 GrGLint writeMask = settings.writeMask(grFace);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001858
1859 if (GR_GL_FRONT_AND_BACK == glFace) {
1860 // we call the combined func just in case separate stencil is not
1861 // supported.
1862 GR_GL_CALL(gl, StencilFunc(glFunc, ref, mask));
1863 GR_GL_CALL(gl, StencilMask(writeMask));
1864 GR_GL_CALL(gl, StencilOp(glFailOp, glPassOp, glPassOp));
1865 } else {
1866 GR_GL_CALL(gl, StencilFuncSeparate(glFace, glFunc, ref, mask));
1867 GR_GL_CALL(gl, StencilMaskSeparate(glFace, writeMask));
1868 GR_GL_CALL(gl, StencilOpSeparate(glFace, glFailOp, glPassOp, glPassOp));
1869 }
1870}
1871}
bsalomon@google.comd302f142011-03-03 13:54:13 +00001872
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001873void GrGpuGL::flushStencil(DrawType type) {
1874 if (kStencilPath_DrawType == type) {
1875 GrAssert(!fStencilSettings.isTwoSided());
1876 // Just the func, ref, and mask is set here. The op and write mask are params to the call
1877 // that draws the path to the SB (glStencilFillPath)
1878 GrGLenum func =
1879 gr_to_gl_stencil_func(fStencilSettings.func(GrStencilSettings::kFront_Face));
1880 GL_CALL(PathStencilFunc(func,
1881 fStencilSettings.funcRef(GrStencilSettings::kFront_Face),
1882 fStencilSettings.funcMask(GrStencilSettings::kFront_Face)));
1883 } else if (fHWStencilSettings != fStencilSettings) {
1884 if (fStencilSettings.isDisabled()) {
1885 if (kNo_TriState != fHWStencilTestEnabled) {
1886 GL_CALL(Disable(GR_GL_STENCIL_TEST));
1887 fHWStencilTestEnabled = kNo_TriState;
1888 }
1889 } else {
1890 if (kYes_TriState != fHWStencilTestEnabled) {
1891 GL_CALL(Enable(GR_GL_STENCIL_TEST));
1892 fHWStencilTestEnabled = kYes_TriState;
1893 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001894 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001895 if (!fStencilSettings.isDisabled()) {
bsalomon@google.comf6601872012-08-28 21:11:35 +00001896 if (this->getCaps().twoSidedStencilSupport()) {
bsalomon@google.com411dad02012-06-05 20:24:20 +00001897 set_gl_stencil(this->glInterface(),
bsalomon@google.coma3201942012-06-21 19:58:20 +00001898 fStencilSettings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00001899 GR_GL_FRONT,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001900 GrStencilSettings::kFront_Face);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001901 set_gl_stencil(this->glInterface(),
bsalomon@google.coma3201942012-06-21 19:58:20 +00001902 fStencilSettings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00001903 GR_GL_BACK,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001904 GrStencilSettings::kBack_Face);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001905 } else {
bsalomon@google.com411dad02012-06-05 20:24:20 +00001906 set_gl_stencil(this->glInterface(),
bsalomon@google.coma3201942012-06-21 19:58:20 +00001907 fStencilSettings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00001908 GR_GL_FRONT_AND_BACK,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001909 GrStencilSettings::kFront_Face);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001910 }
1911 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001912 fHWStencilSettings = fStencilSettings;
reed@google.comac10a2d2010-12-22 21:39:39 +00001913 }
1914}
1915
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001916void GrGpuGL::flushAAState(DrawType type) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001917 const GrRenderTarget* rt = this->getDrawState().getRenderTarget();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001918 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001919 // ES doesn't support toggling GL_MULTISAMPLE and doesn't have
1920 // smooth lines.
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001921 // we prefer smooth lines over multisampled lines
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00001922 bool smoothLines = false;
1923
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001924 if (kDrawLines_DrawType == type) {
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00001925 smoothLines = this->willUseHWAALines();
1926 if (smoothLines) {
1927 if (kYes_TriState != fHWAAState.fSmoothLineEnabled) {
1928 GL_CALL(Enable(GR_GL_LINE_SMOOTH));
1929 fHWAAState.fSmoothLineEnabled = kYes_TriState;
1930 // must disable msaa to use line smoothing
1931 if (rt->isMultisampled() &&
1932 kNo_TriState != fHWAAState.fMSAAEnabled) {
1933 GL_CALL(Disable(GR_GL_MULTISAMPLE));
1934 fHWAAState.fMSAAEnabled = kNo_TriState;
1935 }
1936 }
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001937 } else {
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00001938 if (kNo_TriState != fHWAAState.fSmoothLineEnabled) {
1939 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
1940 fHWAAState.fSmoothLineEnabled = kNo_TriState;
1941 }
1942 }
1943 }
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001944 if (!smoothLines && rt->isMultisampled()) {
1945 // FIXME: GL_NV_pr doesn't seem to like MSAA disabled. The paths
1946 // convex hulls of each segment appear to get filled.
1947 bool enableMSAA = kStencilPath_DrawType == type ||
1948 this->getDrawState().isHWAntialiasState();
1949 if (enableMSAA) {
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00001950 if (kYes_TriState != fHWAAState.fMSAAEnabled) {
1951 GL_CALL(Enable(GR_GL_MULTISAMPLE));
1952 fHWAAState.fMSAAEnabled = kYes_TriState;
1953 }
1954 } else {
1955 if (kNo_TriState != fHWAAState.fMSAAEnabled) {
1956 GL_CALL(Disable(GR_GL_MULTISAMPLE));
1957 fHWAAState.fMSAAEnabled = kNo_TriState;
1958 }
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001959 }
1960 }
1961 }
1962}
1963
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00001964void GrGpuGL::flushBlend(bool isLines,
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001965 GrBlendCoeff srcCoeff,
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001966 GrBlendCoeff dstCoeff) {
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00001967 if (isLines && this->willUseHWAALines()) {
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001968 if (kYes_TriState != fHWBlendState.fEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001969 GL_CALL(Enable(GR_GL_BLEND));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001970 fHWBlendState.fEnabled = kYes_TriState;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001971 }
bsalomon@google.com47059542012-06-06 20:51:20 +00001972 if (kSA_GrBlendCoeff != fHWBlendState.fSrcCoeff ||
1973 kISA_GrBlendCoeff != fHWBlendState.fDstCoeff) {
1974 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[kSA_GrBlendCoeff],
1975 gXfermodeCoeff2Blend[kISA_GrBlendCoeff]));
1976 fHWBlendState.fSrcCoeff = kSA_GrBlendCoeff;
1977 fHWBlendState.fDstCoeff = kISA_GrBlendCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001978 }
1979 } else {
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001980 // any optimization to disable blending should
1981 // have already been applied and tweaked the coeffs
1982 // to (1, 0).
bsalomon@google.com47059542012-06-06 20:51:20 +00001983 bool blendOff = kOne_GrBlendCoeff == srcCoeff &&
1984 kZero_GrBlendCoeff == dstCoeff;
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001985 if (blendOff) {
1986 if (kNo_TriState != fHWBlendState.fEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001987 GL_CALL(Disable(GR_GL_BLEND));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001988 fHWBlendState.fEnabled = kNo_TriState;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001989 }
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001990 } else {
1991 if (kYes_TriState != fHWBlendState.fEnabled) {
1992 GL_CALL(Enable(GR_GL_BLEND));
1993 fHWBlendState.fEnabled = kYes_TriState;
1994 }
1995 if (fHWBlendState.fSrcCoeff != srcCoeff ||
1996 fHWBlendState.fDstCoeff != dstCoeff) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001997 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
1998 gXfermodeCoeff2Blend[dstCoeff]));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001999 fHWBlendState.fSrcCoeff = srcCoeff;
2000 fHWBlendState.fDstCoeff = dstCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002001 }
bsalomon@google.coma5d056a2012-03-27 15:59:58 +00002002 GrColor blendConst = this->getDrawState().getBlendConstant();
bsalomon@google.com271cffc2011-05-20 14:13:56 +00002003 if ((BlendCoeffReferencesConstant(srcCoeff) ||
2004 BlendCoeffReferencesConstant(dstCoeff)) &&
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002005 (!fHWBlendState.fConstColorValid ||
2006 fHWBlendState.fConstColor != blendConst)) {
bsalomon@google.com75347472012-09-17 17:23:21 +00002007 GrGLfloat c[4];
2008 GrColorToRGBAFloat(blendConst, c);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002009 GL_CALL(BlendColor(c[0], c[1], c[2], c[3]));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002010 fHWBlendState.fConstColor = blendConst;
2011 fHWBlendState.fConstColorValid = true;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002012 }
2013 }
2014 }
2015}
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002016namespace {
2017
bsalomon@google.com6d003d12012-09-11 15:45:20 +00002018inline void set_tex_swizzle(GrGLenum swizzle[4], const GrGLInterface* gl) {
bsalomon@google.com4d063de2012-05-31 17:59:23 +00002019 GR_GL_CALL(gl, TexParameteriv(GR_GL_TEXTURE_2D,
2020 GR_GL_TEXTURE_SWIZZLE_RGBA,
2021 reinterpret_cast<const GrGLint*>(swizzle)));
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002022}
bsalomon@google.comb8670992012-07-25 21:27:09 +00002023
bsalomon@google.com6c76d242012-09-07 16:52:24 +00002024inline GrGLenum tile_to_gl_wrap(SkShader::TileMode tm) {
bsalomon@google.comb8670992012-07-25 21:27:09 +00002025 static const GrGLenum gWrapModes[] = {
2026 GR_GL_CLAMP_TO_EDGE,
2027 GR_GL_REPEAT,
2028 GR_GL_MIRRORED_REPEAT
2029 };
2030 GrAssert((unsigned) tm <= SK_ARRAY_COUNT(gWrapModes));
2031 GR_STATIC_ASSERT(0 == SkShader::kClamp_TileMode);
2032 GR_STATIC_ASSERT(1 == SkShader::kRepeat_TileMode);
2033 GR_STATIC_ASSERT(2 == SkShader::kMirror_TileMode);
2034 return gWrapModes[tm];
2035}
2036
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002037}
2038
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002039void GrGpuGL::bindTexture(int unitIdx, const GrTextureParams& params, GrGLTexture* texture) {
2040 GrAssert(NULL != texture);
tomhudson@google.comd0c1a062012-07-12 17:23:52 +00002041
bsalomon@google.comb8670992012-07-25 21:27:09 +00002042 // If we created a rt/tex and rendered to it without using a texture and now we're texturing
2043 // 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 +00002044 // out of the "last != next" check.
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002045 GrGLRenderTarget* texRT = static_cast<GrGLRenderTarget*>(texture->asRenderTarget());
bsalomon@google.com4c883782012-06-04 19:05:11 +00002046 if (NULL != texRT) {
2047 this->onResolveRenderTarget(texRT);
2048 }
2049
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002050 if (fHWBoundTextures[unitIdx] != texture) {
2051 this->setTextureUnit(unitIdx);
2052 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, texture->textureID()));
2053 fHWBoundTextures[unitIdx] = texture;
bsalomon@google.com4c883782012-06-04 19:05:11 +00002054 }
2055
bsalomon@google.com4c883782012-06-04 19:05:11 +00002056 ResetTimestamp timestamp;
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002057 const GrGLTexture::TexParams& oldTexParams = texture->getCachedTexParams(&timestamp);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002058 bool setAll = timestamp < this->getResetTimestamp();
2059 GrGLTexture::TexParams newTexParams;
2060
bsalomon@google.comb8670992012-07-25 21:27:09 +00002061 newTexParams.fFilter = params.isBilerp() ? GR_GL_LINEAR : GR_GL_NEAREST;
bsalomon@google.com4c883782012-06-04 19:05:11 +00002062
bsalomon@google.comb8670992012-07-25 21:27:09 +00002063 newTexParams.fWrapS = tile_to_gl_wrap(params.getTileModeX());
2064 newTexParams.fWrapT = tile_to_gl_wrap(params.getTileModeY());
bsalomon@google.com4c883782012-06-04 19:05:11 +00002065 memcpy(newTexParams.fSwizzleRGBA,
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002066 GrGLShaderBuilder::GetTexParamSwizzle(texture->config(), this->glCaps()),
bsalomon@google.com4c883782012-06-04 19:05:11 +00002067 sizeof(newTexParams.fSwizzleRGBA));
2068 if (setAll || newTexParams.fFilter != oldTexParams.fFilter) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002069 this->setTextureUnit(unitIdx);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002070 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2071 GR_GL_TEXTURE_MAG_FILTER,
2072 newTexParams.fFilter));
2073 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2074 GR_GL_TEXTURE_MIN_FILTER,
2075 newTexParams.fFilter));
2076 }
2077 if (setAll || newTexParams.fWrapS != oldTexParams.fWrapS) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002078 this->setTextureUnit(unitIdx);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002079 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2080 GR_GL_TEXTURE_WRAP_S,
2081 newTexParams.fWrapS));
2082 }
2083 if (setAll || newTexParams.fWrapT != oldTexParams.fWrapT) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002084 this->setTextureUnit(unitIdx);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002085 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2086 GR_GL_TEXTURE_WRAP_T,
2087 newTexParams.fWrapT));
2088 }
2089 if (this->glCaps().textureSwizzleSupport() &&
2090 (setAll || memcmp(newTexParams.fSwizzleRGBA,
2091 oldTexParams.fSwizzleRGBA,
2092 sizeof(newTexParams.fSwizzleRGBA)))) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002093 this->setTextureUnit(unitIdx);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002094 set_tex_swizzle(newTexParams.fSwizzleRGBA,
2095 this->glInterface());
2096 }
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002097 texture->setCachedTexParams(newTexParams, this->getResetTimestamp());
bsalomon@google.com4c883782012-06-04 19:05:11 +00002098}
2099
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002100void GrGpuGL::flushMiscFixedFunctionState() {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002101
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002102 const GrDrawState& drawState = this->getDrawState();
reed@google.comac10a2d2010-12-22 21:39:39 +00002103
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002104 if (drawState.isDitherState()) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002105 if (kYes_TriState != fHWDitherEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002106 GL_CALL(Enable(GR_GL_DITHER));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002107 fHWDitherEnabled = kYes_TriState;
2108 }
2109 } else {
2110 if (kNo_TriState != fHWDitherEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002111 GL_CALL(Disable(GR_GL_DITHER));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002112 fHWDitherEnabled = kNo_TriState;
reed@google.comac10a2d2010-12-22 21:39:39 +00002113 }
2114 }
2115
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002116 if (drawState.isColorWriteDisabled()) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002117 if (kNo_TriState != fHWWriteToColor) {
2118 GL_CALL(ColorMask(GR_GL_FALSE, GR_GL_FALSE,
2119 GR_GL_FALSE, GR_GL_FALSE));
2120 fHWWriteToColor = kNo_TriState;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002121 }
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002122 } else {
2123 if (kYes_TriState != fHWWriteToColor) {
2124 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
2125 fHWWriteToColor = kYes_TriState;
2126 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00002127 }
2128
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002129 if (fHWDrawFace != drawState.getDrawFace()) {
bsalomon@google.coma5d056a2012-03-27 15:59:58 +00002130 switch (this->getDrawState().getDrawFace()) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002131 case GrDrawState::kCCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002132 GL_CALL(Enable(GR_GL_CULL_FACE));
2133 GL_CALL(CullFace(GR_GL_BACK));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002134 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002135 case GrDrawState::kCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002136 GL_CALL(Enable(GR_GL_CULL_FACE));
2137 GL_CALL(CullFace(GR_GL_FRONT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002138 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002139 case GrDrawState::kBoth_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002140 GL_CALL(Disable(GR_GL_CULL_FACE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002141 break;
2142 default:
2143 GrCrash("Unknown draw face.");
2144 }
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002145 fHWDrawFace = drawState.getDrawFace();
bsalomon@google.comd302f142011-03-03 13:54:13 +00002146 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002147}
2148
2149void GrGpuGL::notifyVertexBufferBind(const GrGLVertexBuffer* buffer) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002150 if (fHWGeometryState.fVertexBuffer != buffer) {
2151 fHWGeometryState.fArrayPtrsDirty = true;
2152 fHWGeometryState.fVertexBuffer = buffer;
2153 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002154}
2155
2156void GrGpuGL::notifyVertexBufferDelete(const GrGLVertexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002157 if (fHWGeometryState.fVertexBuffer == buffer) {
2158 // deleting bound buffer does implied bind to 0
2159 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002160 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00002161 }
2162}
2163
2164void GrGpuGL::notifyIndexBufferBind(const GrGLIndexBuffer* buffer) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002165 fHWGeometryState.fIndexBuffer = buffer;
reed@google.comac10a2d2010-12-22 21:39:39 +00002166}
2167
2168void GrGpuGL::notifyIndexBufferDelete(const GrGLIndexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002169 if (fHWGeometryState.fIndexBuffer == buffer) {
2170 // deleting bound buffer does implied bind to 0
2171 fHWGeometryState.fIndexBuffer = NULL;
2172 }
2173}
2174
reed@google.comac10a2d2010-12-22 21:39:39 +00002175void GrGpuGL::notifyRenderTargetDelete(GrRenderTarget* renderTarget) {
2176 GrAssert(NULL != renderTarget);
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002177 if (fHWBoundRenderTarget == renderTarget) {
2178 fHWBoundRenderTarget = NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +00002179 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002180}
2181
2182void GrGpuGL::notifyTextureDelete(GrGLTexture* texture) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002183 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002184 if (fHWBoundTextures[s] == texture) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002185 // deleting bound texture does implied bind to 0
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002186 fHWBoundTextures[s] = NULL;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002187 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002188 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002189}
2190
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002191bool GrGpuGL::configToGLFormats(GrPixelConfig config,
2192 bool getSizedInternalFormat,
2193 GrGLenum* internalFormat,
2194 GrGLenum* externalFormat,
2195 GrGLenum* externalType) {
2196 GrGLenum dontCare;
2197 if (NULL == internalFormat) {
2198 internalFormat = &dontCare;
2199 }
2200 if (NULL == externalFormat) {
2201 externalFormat = &dontCare;
2202 }
2203 if (NULL == externalType) {
2204 externalType = &dontCare;
2205 }
2206
reed@google.comac10a2d2010-12-22 21:39:39 +00002207 switch (config) {
bsalomon@google.com0342a852012-08-20 19:22:38 +00002208 case kRGBA_8888_GrPixelConfig:
bsalomon@google.comc4364992011-11-07 15:54:49 +00002209 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002210 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002211 if (getSizedInternalFormat) {
2212 *internalFormat = GR_GL_RGBA8;
2213 } else {
2214 *internalFormat = GR_GL_RGBA;
2215 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002216 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.comc4364992011-11-07 15:54:49 +00002217 break;
bsalomon@google.com0342a852012-08-20 19:22:38 +00002218 case kBGRA_8888_GrPixelConfig:
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002219 if (!this->glCaps().bgraFormatSupport()) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002220 return false;
2221 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002222 if (this->glCaps().bgraIsInternalFormat()) {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002223 if (getSizedInternalFormat) {
2224 *internalFormat = GR_GL_BGRA8;
2225 } else {
2226 *internalFormat = GR_GL_BGRA;
2227 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002228 } else {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002229 if (getSizedInternalFormat) {
2230 *internalFormat = GR_GL_RGBA8;
2231 } else {
2232 *internalFormat = GR_GL_RGBA;
2233 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002234 }
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002235 *externalFormat = GR_GL_BGRA;
bsalomon@google.com6f379512011-11-16 20:36:03 +00002236 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002237 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002238 case kRGB_565_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002239 *internalFormat = GR_GL_RGB;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002240 *externalFormat = GR_GL_RGB;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002241 if (getSizedInternalFormat) {
2242 if (this->glBinding() == kDesktop_GrGLBinding) {
2243 return false;
2244 } else {
2245 *internalFormat = GR_GL_RGB565;
2246 }
2247 } else {
2248 *internalFormat = GR_GL_RGB;
2249 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002250 *externalType = GR_GL_UNSIGNED_SHORT_5_6_5;
reed@google.comac10a2d2010-12-22 21:39:39 +00002251 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002252 case kRGBA_4444_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002253 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002254 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002255 if (getSizedInternalFormat) {
2256 *internalFormat = GR_GL_RGBA4;
2257 } else {
2258 *internalFormat = GR_GL_RGBA;
2259 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002260 *externalType = GR_GL_UNSIGNED_SHORT_4_4_4_4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002261 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002262 case kIndex_8_GrPixelConfig:
bsalomon@google.comf6601872012-08-28 21:11:35 +00002263 if (this->getCaps().eightBitPaletteSupport()) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002264 *internalFormat = GR_GL_PALETTE8_RGBA8;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002265 // glCompressedTexImage doesn't take external params
2266 *externalFormat = GR_GL_PALETTE8_RGBA8;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002267 // no sized/unsized internal format distinction here
2268 *internalFormat = GR_GL_PALETTE8_RGBA8;
2269 // unused with CompressedTexImage
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002270 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002271 } else {
2272 return false;
2273 }
2274 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002275 case kAlpha_8_GrPixelConfig:
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002276 if (this->glCaps().textureRedSupport()) {
2277 *internalFormat = GR_GL_RED;
2278 *externalFormat = GR_GL_RED;
2279 if (getSizedInternalFormat) {
2280 *internalFormat = GR_GL_R8;
2281 } else {
2282 *internalFormat = GR_GL_RED;
2283 }
2284 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002285 } else {
2286 *internalFormat = GR_GL_ALPHA;
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002287 *externalFormat = GR_GL_ALPHA;
2288 if (getSizedInternalFormat) {
2289 *internalFormat = GR_GL_ALPHA8;
2290 } else {
2291 *internalFormat = GR_GL_ALPHA;
2292 }
2293 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002294 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002295 break;
2296 default:
2297 return false;
2298 }
2299 return true;
2300}
2301
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002302void GrGpuGL::setTextureUnit(int unit) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002303 GrAssert(unit >= 0 && unit < GrDrawState::kNumStages);
bsalomon@google.com49209392012-06-05 15:13:46 +00002304 if (fHWActiveTextureUnitIdx != unit) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002305 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com49209392012-06-05 15:13:46 +00002306 fHWActiveTextureUnitIdx = unit;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002307 }
2308}
bsalomon@google.com316f99232011-01-13 21:28:12 +00002309
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002310void GrGpuGL::setSpareTextureUnit() {
bsalomon@google.com49209392012-06-05 15:13:46 +00002311 if (fHWActiveTextureUnitIdx != (GR_GL_TEXTURE0 + SPARE_TEX_UNIT)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002312 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + SPARE_TEX_UNIT));
bsalomon@google.com49209392012-06-05 15:13:46 +00002313 fHWActiveTextureUnitIdx = SPARE_TEX_UNIT;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002314 }
2315}
2316
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002317void GrGpuGL::setBuffers(bool indexed,
2318 int* extraVertexOffset,
2319 int* extraIndexOffset) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002320
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002321 GrAssert(NULL != extraVertexOffset);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002322
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002323 const GeometryPoolState& geoPoolState = this->getGeomPoolState();
2324
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002325 GrGLVertexBuffer* vbuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002326 switch (this->getGeomSrc().fVertexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002327 case kBuffer_GeometrySrcType:
2328 *extraVertexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002329 vbuf = (GrGLVertexBuffer*) this->getGeomSrc().fVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002330 break;
2331 case kArray_GeometrySrcType:
2332 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002333 this->finalizeReservedVertices();
2334 *extraVertexOffset = geoPoolState.fPoolStartVertex;
2335 vbuf = (GrGLVertexBuffer*) geoPoolState.fPoolVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002336 break;
2337 default:
2338 vbuf = NULL; // suppress warning
2339 GrCrash("Unknown geometry src type!");
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002340 }
2341
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002342 GrAssert(NULL != vbuf);
2343 GrAssert(!vbuf->isLocked());
2344 if (fHWGeometryState.fVertexBuffer != vbuf) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002345 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, vbuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002346 fHWGeometryState.fArrayPtrsDirty = true;
2347 fHWGeometryState.fVertexBuffer = vbuf;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002348 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002349
2350 if (indexed) {
2351 GrAssert(NULL != extraIndexOffset);
2352
2353 GrGLIndexBuffer* ibuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002354 switch (this->getGeomSrc().fIndexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002355 case kBuffer_GeometrySrcType:
2356 *extraIndexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002357 ibuf = (GrGLIndexBuffer*)this->getGeomSrc().fIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002358 break;
2359 case kArray_GeometrySrcType:
2360 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002361 this->finalizeReservedIndices();
2362 *extraIndexOffset = geoPoolState.fPoolStartIndex;
2363 ibuf = (GrGLIndexBuffer*) geoPoolState.fPoolIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002364 break;
2365 default:
2366 ibuf = NULL; // suppress warning
2367 GrCrash("Unknown geometry src type!");
2368 }
2369
2370 GrAssert(NULL != ibuf);
2371 GrAssert(!ibuf->isLocked());
2372 if (fHWGeometryState.fIndexBuffer != ibuf) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002373 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, ibuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002374 fHWGeometryState.fIndexBuffer = ibuf;
2375 }
2376 }
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002377}