blob: a8c902424ccea6466538fe18c7a3e0087e07ac88 [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.com6e4e6502013-02-25 20:12:45 +0000150GrGpuGL::GrGpuGL(const GrGLContextInfo& ctxInfo, GrContext* context)
151 : GrGpu(context)
152 , fGLContextInfo(ctxInfo) {
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000153
154 GrAssert(ctxInfo.isInitialized());
tomhudson@google.com747bf292011-06-14 18:16:52 +0000155
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000156 fillInConfigRenderableTable();
157
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000158 fPrintedCaps = false;
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000159
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000160 GrGLClearErr(fGLContextInfo.interface());
reed@google.comac10a2d2010-12-22 21:39:39 +0000161
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000162 if (gPrintStartupSpew) {
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000163 const GrGLubyte* ext;
164 GL_CALL_RET(ext, GetString(GR_GL_EXTENSIONS));
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000165 const GrGLubyte* vendor;
166 const GrGLubyte* renderer;
167 const GrGLubyte* version;
168 GL_CALL_RET(vendor, GetString(GR_GL_VENDOR));
169 GL_CALL_RET(renderer, GetString(GR_GL_RENDERER));
170 GL_CALL_RET(version, GetString(GR_GL_VERSION));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000171 GrPrintf("------------------------- create GrGpuGL %p --------------\n",
172 this);
173 GrPrintf("------ VENDOR %s\n", vendor);
174 GrPrintf("------ RENDERER %s\n", renderer);
175 GrPrintf("------ VERSION %s\n", version);
176 GrPrintf("------ EXTENSIONS\n %s \n", ext);
177 }
178
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000179 this->initCaps();
tomhudson@google.com747bf292011-06-14 18:16:52 +0000180
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000181 fProgramCache = SkNEW_ARGS(ProgramCache, (this->glContextInfo()));
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000182
bsalomon@google.com880b8fc2013-02-19 20:17:28 +0000183 fHWGeometryState.setMaxAttribArrays(this->glCaps().maxVertexAttributes());
184
bsalomon@google.comfe676522011-06-17 18:12:21 +0000185 fLastSuccessfulStencilFmtIdx = 0;
caryclark@google.comcf6285b2012-06-06 12:09:01 +0000186 if (false) { // avoid bit rot, suppress warning
187 fbo_test(this->glInterface(), 0, 0);
188 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000189}
190
191GrGpuGL::~GrGpuGL() {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000192 if (0 != fHWProgramID) {
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000193 // detach the current program so there is no confusion on OpenGL's part
194 // that we want it to be deleted
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +0000195 GrAssert(fHWProgramID == fCurrentProgram->programID());
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000196 GL_CALL(UseProgram(0));
197 }
198
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000199 delete fProgramCache;
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000200
bsalomon@google.com4a018bb2011-10-28 19:50:21 +0000201 // This must be called by before the GrDrawTarget destructor
202 this->releaseGeometry();
bsalomon@google.com15b11df2011-09-16 21:18:29 +0000203 // This subclass must do this before the base class destructor runs
204 // since we will unref the GrGLInterface.
205 this->releaseResources();
reed@google.comac10a2d2010-12-22 21:39:39 +0000206}
207
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000208///////////////////////////////////////////////////////////////////////////////
209
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000210void GrGpuGL::initCaps() {
211 GrGLint maxTextureUnits;
212 // check FS and fixed-function texture unit limits
213 // we only use textures in the fragment stage currently.
214 // checks are > to make sure we have a spare unit.
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000215 const GrGLInterface* gl = this->glInterface();
216 GR_GL_GetIntegerv(gl, GR_GL_MAX_TEXTURE_IMAGE_UNITS, &maxTextureUnits);
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000217 GrAssert(maxTextureUnits > GrDrawState::kNumStages);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000218
bsalomon@google.comf6601872012-08-28 21:11:35 +0000219 CapsInternals* caps = this->capsInternals();
220
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000221 GrGLint numFormats;
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000222 GR_GL_GetIntegerv(gl, GR_GL_NUM_COMPRESSED_TEXTURE_FORMATS, &numFormats);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000223 SkAutoSTMalloc<10, GrGLint> formats(numFormats);
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000224 GR_GL_GetIntegerv(gl, GR_GL_COMPRESSED_TEXTURE_FORMATS, formats);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000225 for (int i = 0; i < numFormats; ++i) {
226 if (formats[i] == GR_GL_PALETTE8_RGBA8) {
bsalomon@google.comf6601872012-08-28 21:11:35 +0000227 caps->f8BitPaletteSupport = true;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000228 break;
229 }
230 }
231
232 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000233 // we could also look for GL_ATI_separate_stencil extension or
234 // GL_EXT_stencil_two_side but they use different function signatures
235 // than GL2.0+ (and than each other).
bsalomon@google.comf6601872012-08-28 21:11:35 +0000236 caps->fTwoSidedStencilSupport = (this->glVersion() >= GR_GL_VER(2,0));
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000237 // supported on GL 1.4 and higher or by extension
bsalomon@google.comf6601872012-08-28 21:11:35 +0000238 caps->fStencilWrapOpsSupport = (this->glVersion() >= GR_GL_VER(1,4)) ||
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000239 this->hasExtension("GL_EXT_stencil_wrap");
240 } else {
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000241 // ES 2 has two sided stencil and stencil wrap
bsalomon@google.comf6601872012-08-28 21:11:35 +0000242 caps->fTwoSidedStencilSupport = true;
243 caps->fStencilWrapOpsSupport = true;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000244 }
245
246 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.comf6601872012-08-28 21:11:35 +0000247 caps->fBufferLockSupport = true; // we require VBO support and the desktop VBO
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000248 // extension includes glMapBuffer.
249 } else {
bsalomon@google.comf6601872012-08-28 21:11:35 +0000250 caps->fBufferLockSupport = this->hasExtension("GL_OES_mapbuffer");
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000251 }
252
253 if (kDesktop_GrGLBinding == this->glBinding()) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000254 if (this->glVersion() >= GR_GL_VER(2,0) ||
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000255 this->hasExtension("GL_ARB_texture_non_power_of_two")) {
bsalomon@google.comf6601872012-08-28 21:11:35 +0000256 caps->fNPOTTextureTileSupport = true;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000257 } else {
bsalomon@google.comf6601872012-08-28 21:11:35 +0000258 caps->fNPOTTextureTileSupport = false;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000259 }
260 } else {
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000261 // Unextended ES2 supports NPOT textures with clamp_to_edge and non-mip filters only
bsalomon@google.comf6601872012-08-28 21:11:35 +0000262 caps->fNPOTTextureTileSupport = this->hasExtension("GL_OES_texture_npot");
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000263 }
264
bsalomon@google.comf6601872012-08-28 21:11:35 +0000265 caps->fHWAALineSupport = (kDesktop_GrGLBinding == this->glBinding());
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000266
bsalomon@google.comf6601872012-08-28 21:11:35 +0000267 GR_GL_GetIntegerv(gl, GR_GL_MAX_TEXTURE_SIZE, &caps->fMaxTextureSize);
268 GR_GL_GetIntegerv(gl, GR_GL_MAX_RENDERBUFFER_SIZE, &caps->fMaxRenderTargetSize);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000269 // Our render targets are always created with textures as the color
270 // attachment, hence this min:
bsalomon@google.comf6601872012-08-28 21:11:35 +0000271 caps->fMaxRenderTargetSize = GrMin(caps->fMaxTextureSize, caps->fMaxRenderTargetSize);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000272
bsalomon@google.comf6601872012-08-28 21:11:35 +0000273 caps->fFSAASupport = GrGLCaps::kNone_MSFBOType != this->glCaps().msFBOType();
274 caps->fPathStencilingSupport = GR_GL_USE_NV_PATH_RENDERING &&
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000275 this->hasExtension("GL_NV_path_rendering");
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000276
277 // Enable supported shader-related caps
278 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.comf6601872012-08-28 21:11:35 +0000279 caps->fDualSourceBlendingSupport =
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000280 this->glVersion() >= GR_GL_VER(3,3) ||
281 this->hasExtension("GL_ARB_blend_func_extended");
bsalomon@google.comf6601872012-08-28 21:11:35 +0000282 caps->fShaderDerivativeSupport = true;
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000283 // we don't support GL_ARB_geometry_shader4, just GL 3.2+ GS
bsalomon@google.comf6601872012-08-28 21:11:35 +0000284 caps->fGeometryShaderSupport =
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000285 this->glVersion() >= GR_GL_VER(3,2) &&
286 this->glslGeneration() >= k150_GrGLSLGeneration;
287 } else {
bsalomon@google.comf6601872012-08-28 21:11:35 +0000288 caps->fShaderDerivativeSupport =
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000289 this->hasExtension("GL_OES_standard_derivatives");
290 }
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000291}
292
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000293void GrGpuGL::fillInConfigRenderableTable() {
294
295 // OpenGL < 3.0
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000296 // no support for render targets unless the GL_ARB_framebuffer_object
297 // extension is supported (in which case we get ALPHA, RED, RG, RGB,
298 // RGBA (ALPHA8, RGBA4, RGBA8) for OpenGL > 1.1). Note that we
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000299 // probably don't get R8 in this case.
300
301 // OpenGL 3.0
302 // base color renderable: ALPHA, RED, RG, RGB, and RGBA
303 // sized derivatives: ALPHA8, R8, RGBA4, RGBA8
304
305 // >= OpenGL 3.1
306 // base color renderable: RED, RG, RGB, and RGBA
307 // sized derivatives: R8, RGBA4, RGBA8
308 // if the GL_ARB_compatibility extension is supported then we get back
309 // support for GL_ALPHA and ALPHA8
310
311 // GL_EXT_bgra adds BGRA render targets to any version
312
313 // ES 2.0
314 // color renderable: RGBA4, RGB5_A1, RGB565
315 // GL_EXT_texture_rg adds support for R8 as a color render target
316 // GL_OES_rgb8_rgba8 and/or GL_ARM_rgba8 adds support for RGBA8
317 // GL_EXT_texture_format_BGRA8888 and/or GL_APPLE_texture_format_BGRA8888
318 // added BGRA support
319
320 if (kDesktop_GrGLBinding == this->glBinding()) {
321 // Post 3.0 we will get R8
322 // Prior to 3.0 we will get ALPHA8 (with GL_ARB_framebuffer_object)
323 if (this->glVersion() >= GR_GL_VER(3,0) ||
324 this->hasExtension("GL_ARB_framebuffer_object")) {
325 fConfigRenderSupport[kAlpha_8_GrPixelConfig] = true;
326 }
327 } else {
328 // On ES we can only hope for R8
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000329 fConfigRenderSupport[kAlpha_8_GrPixelConfig] =
robertphillips@google.com443e5a52012-04-30 20:01:21 +0000330 this->glCaps().textureRedSupport();
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000331 }
332
333 if (kDesktop_GrGLBinding != this->glBinding()) {
334 // only available in ES
335 fConfigRenderSupport[kRGB_565_GrPixelConfig] = true;
336 }
337
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000338 // Pre 3.0, Ganesh relies on either GL_ARB_framebuffer_object or
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000339 // GL_EXT_framebuffer_object for FBO support. Both of these
340 // allow RGBA4 render targets so this is always supported.
341 fConfigRenderSupport[kRGBA_4444_GrPixelConfig] = true;
342
343 if (this->glCaps().rgba8RenderbufferSupport()) {
bsalomon@google.com0342a852012-08-20 19:22:38 +0000344 fConfigRenderSupport[kRGBA_8888_GrPixelConfig] = true;
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000345 }
346
347 if (this->glCaps().bgraFormatSupport()) {
bsalomon@google.com0342a852012-08-20 19:22:38 +0000348 fConfigRenderSupport[kBGRA_8888_GrPixelConfig] = true;
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000349 }
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000350}
351
bsalomon@google.com9c680582013-02-06 18:17:50 +0000352namespace {
353GrPixelConfig preferred_pixel_ops_config(GrPixelConfig config) {
354 if (GR_GL_RGBA_8888_PIXEL_OPS_SLOW && kRGBA_8888_GrPixelConfig == config) {
355 return kBGRA_8888_GrPixelConfig;
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000356 } else {
357 return config;
358 }
359}
bsalomon@google.com9c680582013-02-06 18:17:50 +0000360}
361
362GrPixelConfig GrGpuGL::preferredReadPixelsConfig(GrPixelConfig config) const {
363 return preferred_pixel_ops_config(config);
364}
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000365
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000366GrPixelConfig GrGpuGL::preferredWritePixelsConfig(GrPixelConfig config) const {
bsalomon@google.com9c680582013-02-06 18:17:50 +0000367 return preferred_pixel_ops_config(config);
368}
369
370bool GrGpuGL::canWriteTexturePixels(const GrTexture* texture, GrPixelConfig srcConfig) const {
371 if (kIndex_8_GrPixelConfig == srcConfig || kIndex_8_GrPixelConfig == texture->config()) {
372 return false;
373 }
374 if (srcConfig != texture->config() && kES2_GrGLBinding == this->glBinding()) {
375 // In general ES2 requires the internal format of the texture and the format of the src
376 // pixels to match. However, It may or may not be possible to upload BGRA data to a RGBA
377 // texture. It depends upon which extension added BGRA. The Apple extension allows it
378 // (BGRA's internal format is RGBA) while the EXT extension does not (BGRA is its own
379 // internal format).
380 if (this->glCaps().bgraFormatSupport() &&
381 !this->glCaps().bgraIsInternalFormat() &&
382 kBGRA_8888_GrPixelConfig == srcConfig &&
383 kRGBA_8888_GrPixelConfig == texture->config()) {
384 return true;
385 } else {
386 return false;
387 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000388 } else {
bsalomon@google.com9c680582013-02-06 18:17:50 +0000389 return true;
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000390 }
391}
392
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000393bool GrGpuGL::fullReadPixelsIsFasterThanPartial() const {
394 return SkToBool(GR_GL_FULL_READPIXELS_FASTER_THAN_PARTIAL);
395}
396
bsalomon@google.com1bf1c212011-11-05 12:18:58 +0000397void GrGpuGL::onResetContext() {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000398 if (gPrintStartupSpew && !fPrintedCaps) {
399 fPrintedCaps = true;
400 this->getCaps().print();
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000401 this->glCaps().print();
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000402 }
403
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000404 // we don't use the zb at all
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000405 GL_CALL(Disable(GR_GL_DEPTH_TEST));
406 GL_CALL(DepthMask(GR_GL_FALSE));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000407
bsalomon@google.com978c8c62012-05-21 14:45:49 +0000408 fHWDrawFace = GrDrawState::kInvalid_DrawFace;
409 fHWDitherEnabled = kUnknown_TriState;
reed@google.comac10a2d2010-12-22 21:39:39 +0000410
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000411 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com469d0dd2012-05-21 20:14:29 +0000412 // Desktop-only state that we never change
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000413 GL_CALL(Disable(GR_GL_POINT_SMOOTH));
bsalomon@google.com469d0dd2012-05-21 20:14:29 +0000414 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
415 GL_CALL(Disable(GR_GL_POLYGON_SMOOTH));
416 GL_CALL(Disable(GR_GL_POLYGON_STIPPLE));
417 GL_CALL(Disable(GR_GL_COLOR_LOGIC_OP));
bsalomon@google.come76b7cc2012-06-18 12:47:06 +0000418 if (this->glCaps().imagingSupport()) {
419 GL_CALL(Disable(GR_GL_COLOR_TABLE));
420 }
bsalomon@google.com469d0dd2012-05-21 20:14:29 +0000421 GL_CALL(Disable(GR_GL_INDEX_LOGIC_OP));
422 GL_CALL(Disable(GR_GL_POLYGON_OFFSET_FILL));
423 // Since ES doesn't support glPointSize at all we always use the VS to
424 // set the point size
425 GL_CALL(Enable(GR_GL_VERTEX_PROGRAM_POINT_SIZE));
426
427 // We should set glPolygonMode(FRONT_AND_BACK,FILL) here, too. It isn't
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000428 // currently part of our gl interface. There are probably others as
bsalomon@google.com469d0dd2012-05-21 20:14:29 +0000429 // well.
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000430 }
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +0000431 fHWAAState.invalidate();
bsalomon@google.com978c8c62012-05-21 14:45:49 +0000432 fHWWriteToColor = kUnknown_TriState;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000433
reed@google.comac10a2d2010-12-22 21:39:39 +0000434 // we only ever use lines in hairline mode
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000435 GL_CALL(LineWidth(1));
reed@google.comac10a2d2010-12-22 21:39:39 +0000436
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000437 // invalid
bsalomon@google.com49209392012-06-05 15:13:46 +0000438 fHWActiveTextureUnitIdx = -1;
reed@google.comac10a2d2010-12-22 21:39:39 +0000439
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +0000440 fHWBlendState.invalidate();
bsalomon@google.com080773c2011-03-15 19:09:25 +0000441
tomhudson@google.com93813632011-10-27 20:21:16 +0000442 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.comc811ea32012-05-21 15:33:09 +0000443 fHWBoundTextures[s] = NULL;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000444 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000445
bsalomon@google.coma3201942012-06-21 19:58:20 +0000446 fHWScissorSettings.invalidate();
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000447
bsalomon@google.coma3201942012-06-21 19:58:20 +0000448 fHWViewport.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000449
bsalomon@google.com457b8a32012-05-21 21:19:58 +0000450 fHWStencilSettings.invalidate();
bsalomon@google.coma3201942012-06-21 19:58:20 +0000451 fHWStencilTestEnabled = kUnknown_TriState;
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000452
bsalomon@google.com880b8fc2013-02-19 20:17:28 +0000453 fHWGeometryState.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000454
bsalomon@google.comc811ea32012-05-21 15:33:09 +0000455 fHWBoundRenderTarget = NULL;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000456
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +0000457 fHWPathStencilMatrixState.invalidate();
bsalomon@google.comf6601872012-08-28 21:11:35 +0000458 if (fCaps.pathStencilingSupport()) {
bsalomon@google.com05a718c2012-06-29 14:01:53 +0000459 // we don't use the model view matrix.
460 GL_CALL(MatrixMode(GR_GL_MODELVIEW));
461 GL_CALL(LoadIdentity());
462 }
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000463
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000464 // we assume these values
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000465 if (this->glCaps().unpackRowLengthSupport()) {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000466 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
467 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000468 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000469 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
470 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000471 if (this->glCaps().unpackFlipYSupport()) {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000472 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
473 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000474 if (this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000475 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, GR_GL_FALSE));
476 }
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000477
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000478 fHWProgramID = 0;
bsalomon@google.com91207482013-02-12 21:45:24 +0000479 fSharedGLProgramState.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000480}
481
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000482namespace {
483
484GrSurfaceOrigin resolve_origin(GrSurfaceOrigin origin, bool renderTarget) {
485 // By default, GrRenderTargets are GL's normal orientation so that they
486 // can be drawn to by the outside world without the client having
487 // to render upside down.
488 if (kDefault_GrSurfaceOrigin == origin) {
489 return renderTarget ? kBottomLeft_GrSurfaceOrigin : kTopLeft_GrSurfaceOrigin;
490 } else {
491 return origin;
492 }
493}
494
495}
496
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000497GrTexture* GrGpuGL::onWrapBackendTexture(const GrBackendTextureDesc& desc) {
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000498 if (!this->configToGLFormats(desc.fConfig, false, NULL, NULL, NULL)) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000499 return NULL;
500 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000501
robertphillips@google.comb72e5d32012-10-30 15:18:10 +0000502 if (0 == desc.fTextureHandle) {
503 return NULL;
504 }
505
506 int maxSize = this->getCaps().maxTextureSize();
507 if (desc.fWidth > maxSize || desc.fHeight > maxSize) {
508 return NULL;
509 }
510
511 GrGLTexture::Desc glTexDesc;
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000512 // next line relies on GrBackendTextureDesc's flags matching GrTexture's
robertphillips@google.com32716282012-06-04 12:48:45 +0000513 glTexDesc.fFlags = (GrTextureFlags) desc.fFlags;
bsalomon@google.com99621082011-11-15 16:47:16 +0000514 glTexDesc.fWidth = desc.fWidth;
515 glTexDesc.fHeight = desc.fHeight;
bsalomon@google.come269f212011-11-07 13:29:52 +0000516 glTexDesc.fConfig = desc.fConfig;
robertphillips@google.com32716282012-06-04 12:48:45 +0000517 glTexDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.come269f212011-11-07 13:29:52 +0000518 glTexDesc.fTextureID = static_cast<GrGLuint>(desc.fTextureHandle);
bsalomon@google.com72830222013-01-23 20:25:22 +0000519 glTexDesc.fIsWrapped = true;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000520 bool renderTarget = 0 != (desc.fFlags & kRenderTarget_GrBackendTextureFlag);
521 // FIXME: this should be calling resolve_origin(), but Chrome code is currently
522 // assuming the old behaviour, which is that backend textures are always
523 // BottomLeft, even for non-RT's. Once Chrome is fixed, change this to:
524 // glTexDesc.fOrigin = resolve_origin(desc.fOrigin, renderTarget);
525 if (kDefault_GrSurfaceOrigin == desc.fOrigin) {
526 glTexDesc.fOrigin = kBottomLeft_GrSurfaceOrigin;
527 } else {
528 glTexDesc.fOrigin = desc.fOrigin;
529 }
bsalomon@google.come269f212011-11-07 13:29:52 +0000530
531 GrGLTexture* texture = NULL;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000532 if (renderTarget) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000533 GrGLRenderTarget::Desc glRTDesc;
534 glRTDesc.fRTFBOID = 0;
535 glRTDesc.fTexFBOID = 0;
536 glRTDesc.fMSColorRenderbufferID = 0;
bsalomon@google.come269f212011-11-07 13:29:52 +0000537 glRTDesc.fConfig = desc.fConfig;
538 glRTDesc.fSampleCnt = desc.fSampleCnt;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000539 glRTDesc.fOrigin = glTexDesc.fOrigin;
bsalomon@google.com99621082011-11-15 16:47:16 +0000540 if (!this->createRenderTargetObjects(glTexDesc.fWidth,
541 glTexDesc.fHeight,
bsalomon@google.come269f212011-11-07 13:29:52 +0000542 glTexDesc.fTextureID,
543 &glRTDesc)) {
544 return NULL;
545 }
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000546 texture = SkNEW_ARGS(GrGLTexture, (this, glTexDesc, glRTDesc));
bsalomon@google.come269f212011-11-07 13:29:52 +0000547 } else {
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000548 texture = SkNEW_ARGS(GrGLTexture, (this, glTexDesc));
bsalomon@google.come269f212011-11-07 13:29:52 +0000549 }
550 if (NULL == texture) {
551 return NULL;
552 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000553
bsalomon@google.come269f212011-11-07 13:29:52 +0000554 this->setSpareTextureUnit();
555 return texture;
556}
557
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000558GrRenderTarget* GrGpuGL::onWrapBackendRenderTarget(const GrBackendRenderTargetDesc& desc) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000559 GrGLRenderTarget::Desc glDesc;
560 glDesc.fConfig = desc.fConfig;
561 glDesc.fRTFBOID = static_cast<GrGLuint>(desc.fRenderTargetHandle);
562 glDesc.fMSColorRenderbufferID = 0;
563 glDesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
564 glDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.com72830222013-01-23 20:25:22 +0000565 glDesc.fIsWrapped = true;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000566 glDesc.fOrigin = desc.fOrigin;
567 if (glDesc.fRTFBOID == 0) {
568 GrAssert(desc.fOrigin == kBottomLeft_GrSurfaceOrigin);
569 }
570
571 glDesc.fOrigin = resolve_origin(desc.fOrigin, true);
bsalomon@google.come269f212011-11-07 13:29:52 +0000572 GrGLIRect viewport;
573 viewport.fLeft = 0;
574 viewport.fBottom = 0;
575 viewport.fWidth = desc.fWidth;
576 viewport.fHeight = desc.fHeight;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000577
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000578 GrRenderTarget* tgt = SkNEW_ARGS(GrGLRenderTarget,
579 (this, glDesc, viewport));
bsalomon@google.come269f212011-11-07 13:29:52 +0000580 if (desc.fStencilBits) {
581 GrGLStencilBuffer::Format format;
582 format.fInternalFormat = GrGLStencilBuffer::kUnknownInternalFormat;
583 format.fPacked = false;
584 format.fStencilBits = desc.fStencilBits;
585 format.fTotalBits = desc.fStencilBits;
bsalomon@google.com1f0f1a32013-01-23 21:32:32 +0000586 static const bool kIsSBWrapped = false;
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000587 GrGLStencilBuffer* sb = SkNEW_ARGS(GrGLStencilBuffer,
588 (this,
bsalomon@google.com1f0f1a32013-01-23 21:32:32 +0000589 kIsSBWrapped,
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000590 0,
591 desc.fWidth,
592 desc.fHeight,
593 desc.fSampleCnt,
594 format));
bsalomon@google.come269f212011-11-07 13:29:52 +0000595 tgt->setStencilBuffer(sb);
596 sb->unref();
597 }
598 return tgt;
599}
600
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000601////////////////////////////////////////////////////////////////////////////////
602
bsalomon@google.com9c680582013-02-06 18:17:50 +0000603bool GrGpuGL::onWriteTexturePixels(GrTexture* texture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000604 int left, int top, int width, int height,
605 GrPixelConfig config, const void* buffer,
606 size_t rowBytes) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000607 if (NULL == buffer) {
bsalomon@google.com9c680582013-02-06 18:17:50 +0000608 return false;
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000609 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000610 GrGLTexture* glTex = static_cast<GrGLTexture*>(texture);
611
bsalomon@google.com6f379512011-11-16 20:36:03 +0000612 this->setSpareTextureUnit();
613 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTex->textureID()));
614 GrGLTexture::Desc desc;
robertphillips@google.com32716282012-06-04 12:48:45 +0000615 desc.fFlags = glTex->desc().fFlags;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000616 desc.fWidth = glTex->width();
617 desc.fHeight = glTex->height();
robertphillips@google.com32716282012-06-04 12:48:45 +0000618 desc.fConfig = glTex->config();
619 desc.fSampleCnt = glTex->desc().fSampleCnt;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000620 desc.fTextureID = glTex->textureID();
bsalomon@google.com2d0bade2012-10-26 19:01:17 +0000621 desc.fOrigin = glTex->origin();
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000622
bsalomon@google.com9c680582013-02-06 18:17:50 +0000623 return this->uploadTexData(desc, false,
624 left, top, width, height,
625 config, buffer, rowBytes);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000626}
627
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000628namespace {
629bool adjust_pixel_ops_params(int surfaceWidth,
630 int surfaceHeight,
631 size_t bpp,
632 int* left, int* top, int* width, int* height,
633 const void** data,
634 size_t* rowBytes) {
635 if (!*rowBytes) {
636 *rowBytes = *width * bpp;
637 }
638
639 GrIRect subRect = GrIRect::MakeXYWH(*left, *top, *width, *height);
640 GrIRect bounds = GrIRect::MakeWH(surfaceWidth, surfaceHeight);
641
642 if (!subRect.intersect(bounds)) {
643 return false;
644 }
645 *data = reinterpret_cast<const void*>(reinterpret_cast<intptr_t>(*data) +
646 (subRect.fTop - *top) * *rowBytes + (subRect.fLeft - *left) * bpp);
647
648 *left = subRect.fLeft;
649 *top = subRect.fTop;
650 *width = subRect.width();
651 *height = subRect.height();
652 return true;
653}
654}
655
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000656bool GrGpuGL::uploadTexData(const GrGLTexture::Desc& desc,
657 bool isNewTexture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000658 int left, int top, int width, int height,
659 GrPixelConfig dataConfig,
660 const void* data,
661 size_t rowBytes) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000662 GrAssert(NULL != data || isNewTexture);
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000663
664 size_t bpp = GrBytesPerPixel(dataConfig);
665 if (!adjust_pixel_ops_params(desc.fWidth, desc.fHeight, bpp, &left, &top,
666 &width, &height, &data, &rowBytes)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000667 return false;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000668 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000669 size_t trimRowBytes = width * bpp;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000670
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000671 // in case we need a temporary, trimmed copy of the src pixels
672 SkAutoSMalloc<128 * 128> tempStorage;
673
bsalomon@google.com313f0192012-07-10 17:21:02 +0000674 // paletted textures cannot be partially updated
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000675 bool useTexStorage = isNewTexture &&
bsalomon@google.com313f0192012-07-10 17:21:02 +0000676 desc.fConfig != kIndex_8_GrPixelConfig &&
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000677 this->glCaps().texStorageSupport();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000678
bsalomon@google.com313f0192012-07-10 17:21:02 +0000679 if (useTexStorage && kDesktop_GrGLBinding == this->glBinding()) {
680 // 565 is not a sized internal format on desktop GL. So on desktop with
681 // 565 we always use an unsized internal format to let the system pick
682 // the best sized format to convert the 565 data to. Since TexStorage
683 // only allows sized internal formats we will instead use TexImage2D.
684 useTexStorage = desc.fConfig != kRGB_565_GrPixelConfig;
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000685 }
686
687 GrGLenum internalFormat;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000688 GrGLenum externalFormat;
689 GrGLenum externalType;
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000690 // glTexStorage requires sized internal formats on both desktop and ES. ES
691 // glTexImage requires an unsized format.
692 if (!this->configToGLFormats(dataConfig, useTexStorage, &internalFormat,
693 &externalFormat, &externalType)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000694 return false;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000695 }
696
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000697 if (!isNewTexture && GR_GL_PALETTE8_RGBA8 == internalFormat) {
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000698 // paletted textures cannot be updated
699 return false;
700 }
701
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000702 /*
bsalomon@google.com6f379512011-11-16 20:36:03 +0000703 * check whether to allocate a temporary buffer for flipping y or
704 * because our srcData has extra bytes past each row. If so, we need
705 * to trim those off here, since GL ES may not let us specify
706 * GL_UNPACK_ROW_LENGTH.
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000707 */
bsalomon@google.com6f379512011-11-16 20:36:03 +0000708 bool restoreGLRowLength = false;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000709 bool swFlipY = false;
710 bool glFlipY = false;
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000711 if (NULL != data) {
senorblanco@chromium.orgef5dbe12013-01-28 16:42:38 +0000712 if (kBottomLeft_GrSurfaceOrigin == desc.fOrigin) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000713 if (this->glCaps().unpackFlipYSupport()) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000714 glFlipY = true;
715 } else {
716 swFlipY = true;
717 }
718 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000719 if (this->glCaps().unpackRowLengthSupport() && !swFlipY) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000720 // can't use this for flipping, only non-neg values allowed. :(
721 if (rowBytes != trimRowBytes) {
722 GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
723 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength));
724 restoreGLRowLength = true;
725 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000726 } else {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000727 if (trimRowBytes != rowBytes || swFlipY) {
728 // copy data into our new storage, skipping the trailing bytes
729 size_t trimSize = height * trimRowBytes;
730 const char* src = (const char*)data;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000731 if (swFlipY) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000732 src += (height - 1) * rowBytes;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000733 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000734 char* dst = (char*)tempStorage.reset(trimSize);
735 for (int y = 0; y < height; y++) {
736 memcpy(dst, src, trimRowBytes);
737 if (swFlipY) {
738 src -= rowBytes;
739 } else {
740 src += rowBytes;
741 }
742 dst += trimRowBytes;
743 }
744 // now point data to our copied version
745 data = tempStorage.get();
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000746 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000747 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000748 if (glFlipY) {
749 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_TRUE));
750 }
751 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, static_cast<GrGLint>(bpp)));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000752 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000753 bool succeeded = true;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000754 if (isNewTexture &&
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000755 0 == left && 0 == top &&
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000756 desc.fWidth == width && desc.fHeight == height) {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000757 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000758 if (useTexStorage) {
759 // We never resize or change formats of textures. We don't use
760 // mipmaps currently.
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000761 GL_ALLOC_CALL(this->glInterface(),
762 TexStorage2D(GR_GL_TEXTURE_2D,
763 1, // levels
764 internalFormat,
765 desc.fWidth, desc.fHeight));
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000766 } else {
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000767 if (GR_GL_PALETTE8_RGBA8 == internalFormat) {
768 GrGLsizei imageSize = desc.fWidth * desc.fHeight +
769 kGrColorTableSize;
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000770 GL_ALLOC_CALL(this->glInterface(),
771 CompressedTexImage2D(GR_GL_TEXTURE_2D,
772 0, // level
773 internalFormat,
774 desc.fWidth, desc.fHeight,
775 0, // border
776 imageSize,
777 data));
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000778 } else {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000779 GL_ALLOC_CALL(this->glInterface(),
780 TexImage2D(GR_GL_TEXTURE_2D,
781 0, // level
782 internalFormat,
783 desc.fWidth, desc.fHeight,
784 0, // border
785 externalFormat, externalType,
786 data));
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000787 }
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000788 }
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000789 GrGLenum error = CHECK_ALLOC_ERROR(this->glInterface());
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000790 if (error != GR_GL_NO_ERROR) {
791 succeeded = false;
792 } else {
793 // if we have data and we used TexStorage to create the texture, we
794 // now upload with TexSubImage.
795 if (NULL != data && useTexStorage) {
796 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D,
797 0, // level
798 left, top,
799 width, height,
800 externalFormat, externalType,
801 data));
802 }
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000803 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000804 } else {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000805 if (swFlipY || glFlipY) {
bsalomon@google.com6f379512011-11-16 20:36:03 +0000806 top = desc.fHeight - (top + height);
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000807 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000808 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D,
809 0, // level
810 left, top,
811 width, height,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000812 externalFormat, externalType, data));
813 }
814
815 if (restoreGLRowLength) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000816 GrAssert(this->glCaps().unpackRowLengthSupport());
bsalomon@google.com6f379512011-11-16 20:36:03 +0000817 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000818 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000819 if (glFlipY) {
820 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
821 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000822 return succeeded;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000823}
824
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000825namespace {
826bool renderbuffer_storage_msaa(GrGLContextInfo& ctxInfo,
827 int sampleCount,
828 GrGLenum format,
829 int width, int height) {
830 CLEAR_ERROR_BEFORE_ALLOC(ctxInfo.interface());
831 GrAssert(GrGLCaps::kNone_MSFBOType != ctxInfo.caps().msFBOType());
832 bool created = false;
833 if (GrGLCaps::kNVDesktop_CoverageAAType ==
834 ctxInfo.caps().coverageAAType()) {
835 const GrGLCaps::MSAACoverageMode& mode =
836 ctxInfo.caps().getMSAACoverageMode(sampleCount);
837 GL_ALLOC_CALL(ctxInfo.interface(),
838 RenderbufferStorageMultisampleCoverage(GR_GL_RENDERBUFFER,
839 mode.fCoverageSampleCnt,
840 mode.fColorSampleCnt,
841 format,
842 width, height));
843 created = (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctxInfo.interface()));
844 }
845 if (!created) {
bsalomon@google.comf6b070d2012-04-27 14:25:44 +0000846 // glRBMS will fail if requested samples is > max samples.
847 sampleCount = GrMin(sampleCount, ctxInfo.caps().maxSampleCount());
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000848 GL_ALLOC_CALL(ctxInfo.interface(),
849 RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
850 sampleCount,
851 format,
852 width, height));
853 created = (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctxInfo.interface()));
854 }
855 return created;
856}
857}
858
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000859bool GrGpuGL::createRenderTargetObjects(int width, int height,
860 GrGLuint texID,
861 GrGLRenderTarget::Desc* desc) {
862 desc->fMSColorRenderbufferID = 0;
863 desc->fRTFBOID = 0;
864 desc->fTexFBOID = 0;
bsalomon@google.com72830222013-01-23 20:25:22 +0000865 desc->fIsWrapped = false;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000866
867 GrGLenum status;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000868
bsalomon@google.comab15d612011-08-09 12:57:56 +0000869 GrGLenum msColorFormat = 0; // suppress warning
870
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000871 GL_CALL(GenFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000872 if (!desc->fTexFBOID) {
873 goto FAILED;
874 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000875
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000876
877 // If we are using multisampling we will create two FBOS. We render
878 // to one and then resolve to the texture bound to the other.
bsalomon@google.come269f212011-11-07 13:29:52 +0000879 if (desc->fSampleCnt > 0) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000880 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000881 goto FAILED;
882 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000883 GL_CALL(GenFramebuffers(1, &desc->fRTFBOID));
884 GL_CALL(GenRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000885 if (!desc->fRTFBOID ||
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000886 !desc->fMSColorRenderbufferID ||
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000887 !this->configToGLFormats(desc->fConfig,
888 // GLES requires sized internal formats
889 kES2_GrGLBinding == this->glBinding(),
890 &msColorFormat, NULL, NULL)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000891 goto FAILED;
892 }
893 } else {
894 desc->fRTFBOID = desc->fTexFBOID;
895 }
896
bsalomon@google.com0e9b41a2012-01-04 22:11:43 +0000897 // below here we may bind the FBO
bsalomon@google.comc811ea32012-05-21 15:33:09 +0000898 fHWBoundRenderTarget = NULL;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000899 if (desc->fRTFBOID != desc->fTexFBOID) {
bsalomon@google.com13ac3a32013-01-09 23:29:39 +0000900 GrAssert(desc->fSampleCnt > 0);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000901 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000902 desc->fMSColorRenderbufferID));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000903 if (!renderbuffer_storage_msaa(fGLContextInfo,
904 desc->fSampleCnt,
905 msColorFormat,
906 width, height)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000907 goto FAILED;
908 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000909 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fRTFBOID));
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000910 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000911 GR_GL_COLOR_ATTACHMENT0,
912 GR_GL_RENDERBUFFER,
913 desc->fMSColorRenderbufferID));
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000914 if (!this->glCaps().isConfigVerifiedColorAttachment(desc->fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000915 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
916 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
917 goto FAILED;
918 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000919 fGLContextInfo.caps().markConfigAsValidColorAttachment(
920 desc->fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000921 }
922 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000923 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000924
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000925 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
926 GR_GL_COLOR_ATTACHMENT0,
927 GR_GL_TEXTURE_2D,
928 texID, 0));
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000929 if (!this->glCaps().isConfigVerifiedColorAttachment(desc->fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000930 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
931 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
932 goto FAILED;
933 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000934 fGLContextInfo.caps().markConfigAsValidColorAttachment(desc->fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000935 }
936
937 return true;
938
939FAILED:
940 if (desc->fMSColorRenderbufferID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000941 GL_CALL(DeleteRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000942 }
943 if (desc->fRTFBOID != desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000944 GL_CALL(DeleteFramebuffers(1, &desc->fRTFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000945 }
946 if (desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000947 GL_CALL(DeleteFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000948 }
949 return false;
950}
951
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000952// good to set a break-point here to know when createTexture fails
953static GrTexture* return_null_texture() {
954// GrAssert(!"null texture");
955 return NULL;
956}
957
caryclark@google.comcf6285b2012-06-06 12:09:01 +0000958#if 0 && GR_DEBUG
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000959static size_t as_size_t(int x) {
960 return x;
961}
962#endif
963
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000964GrTexture* GrGpuGL::onCreateTexture(const GrTextureDesc& desc,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000965 const void* srcData,
966 size_t rowBytes) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000967
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000968 GrGLTexture::Desc glTexDesc;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000969 GrGLRenderTarget::Desc glRTDesc;
reed@google.comac10a2d2010-12-22 21:39:39 +0000970
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000971 // Attempt to catch un- or wrongly initialized sample counts;
972 GrAssert(desc.fSampleCnt >= 0 && desc.fSampleCnt <= 64);
973
robertphillips@google.com32716282012-06-04 12:48:45 +0000974 glTexDesc.fFlags = desc.fFlags;
bsalomon@google.com99621082011-11-15 16:47:16 +0000975 glTexDesc.fWidth = desc.fWidth;
976 glTexDesc.fHeight = desc.fHeight;
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000977 glTexDesc.fConfig = desc.fConfig;
robertphillips@google.com32716282012-06-04 12:48:45 +0000978 glTexDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.com72830222013-01-23 20:25:22 +0000979 glTexDesc.fIsWrapped = false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000980
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000981 glRTDesc.fMSColorRenderbufferID = 0;
982 glRTDesc.fRTFBOID = 0;
983 glRTDesc.fTexFBOID = 0;
bsalomon@google.com72830222013-01-23 20:25:22 +0000984 glRTDesc.fIsWrapped = false;
bsalomon@google.com64c4fe42011-11-05 14:51:01 +0000985 glRTDesc.fConfig = glTexDesc.fConfig;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000986
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000987 bool renderTarget = 0 != (desc.fFlags & kRenderTarget_GrTextureFlagBit);
reed@google.comac10a2d2010-12-22 21:39:39 +0000988
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000989 const Caps& caps = this->getCaps();
990
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000991 glTexDesc.fOrigin = resolve_origin(desc.fOrigin, renderTarget);
992 glRTDesc.fOrigin = glTexDesc.fOrigin;
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000993
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000994 glRTDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000995 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType() &&
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000996 desc.fSampleCnt) {
bsalomon@google.com945bbe12012-06-15 14:30:34 +0000997 //GrPrintf("MSAA RT requested but not supported on this platform.");
998 return return_null_texture();
reed@google.comac10a2d2010-12-22 21:39:39 +0000999 }
1000
reed@google.comac10a2d2010-12-22 21:39:39 +00001001 if (renderTarget) {
bsalomon@google.comf6601872012-08-28 21:11:35 +00001002 if (glTexDesc.fWidth > caps.maxRenderTargetSize() ||
1003 glTexDesc.fHeight > caps.maxRenderTargetSize()) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001004 return return_null_texture();
1005 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001006 }
1007
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001008 GL_CALL(GenTextures(1, &glTexDesc.fTextureID));
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001009 if (renderTarget && this->glCaps().textureUsageSupport()) {
bsalomon@google.com07dd2bf2011-12-09 19:40:36 +00001010 // provides a hint about how this texture will be used
1011 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1012 GR_GL_TEXTURE_USAGE,
1013 GR_GL_FRAMEBUFFER_ATTACHMENT));
1014 }
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001015 if (!glTexDesc.fTextureID) {
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001016 return return_null_texture();
1017 }
1018
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001019 this->setSpareTextureUnit();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001020 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTexDesc.fTextureID));
bsalomon@google.come269f212011-11-07 13:29:52 +00001021
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001022 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
1023 // drivers have a bug where an FBO won't be complete if it includes a
1024 // texture that is not mipmap complete (considering the filter in use).
1025 GrGLTexture::TexParams initialTexParams;
1026 // we only set a subset here so invalidate first
1027 initialTexParams.invalidate();
1028 initialTexParams.fFilter = GR_GL_NEAREST;
1029 initialTexParams.fWrapS = GR_GL_CLAMP_TO_EDGE;
1030 initialTexParams.fWrapT = GR_GL_CLAMP_TO_EDGE;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001031 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1032 GR_GL_TEXTURE_MAG_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_MIN_FILTER,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001036 initialTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001037 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1038 GR_GL_TEXTURE_WRAP_S,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001039 initialTexParams.fWrapS));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001040 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1041 GR_GL_TEXTURE_WRAP_T,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001042 initialTexParams.fWrapT));
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001043 if (!this->uploadTexData(glTexDesc, true, 0, 0,
1044 glTexDesc.fWidth, glTexDesc.fHeight,
1045 desc.fConfig, srcData, rowBytes)) {
1046 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
1047 return return_null_texture();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001048 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001049
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001050 GrGLTexture* tex;
reed@google.comac10a2d2010-12-22 21:39:39 +00001051 if (renderTarget) {
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001052 // unbind the texture from the texture unit before binding it to the frame buffer
1053 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, 0));
1054
bsalomon@google.com99621082011-11-15 16:47:16 +00001055 if (!this->createRenderTargetObjects(glTexDesc.fWidth,
1056 glTexDesc.fHeight,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001057 glTexDesc.fTextureID,
1058 &glRTDesc)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001059 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001060 return return_null_texture();
1061 }
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001062 tex = SkNEW_ARGS(GrGLTexture, (this, glTexDesc, glRTDesc));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001063 } else {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001064 tex = SkNEW_ARGS(GrGLTexture, (this, glTexDesc));
reed@google.comac10a2d2010-12-22 21:39:39 +00001065 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001066 tex->setCachedTexParams(initialTexParams, this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00001067#ifdef TRACE_TEXTURE_CREATION
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001068 GrPrintf("--- new texture [%d] size=(%d %d) config=%d\n",
1069 glTexDesc.fTextureID, desc.fWidth, desc.fHeight, desc.fConfig);
reed@google.comac10a2d2010-12-22 21:39:39 +00001070#endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001071 return tex;
1072}
1073
1074namespace {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001075
1076const GrGLuint kUnknownBitCount = GrGLStencilBuffer::kUnknownBitCount;
1077
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001078void inline get_stencil_rb_sizes(const GrGLInterface* gl,
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001079 GrGLuint rb,
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001080 GrGLStencilBuffer::Format* format) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001081 // we shouldn't ever know one size and not the other
1082 GrAssert((kUnknownBitCount == format->fStencilBits) ==
1083 (kUnknownBitCount == format->fTotalBits));
1084 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001085 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001086 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1087 (GrGLint*)&format->fStencilBits);
1088 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001089 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001090 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1091 (GrGLint*)&format->fTotalBits);
1092 format->fTotalBits += format->fStencilBits;
1093 } else {
1094 format->fTotalBits = format->fStencilBits;
1095 }
1096 }
1097}
1098}
1099
1100bool GrGpuGL::createStencilBufferForRenderTarget(GrRenderTarget* rt,
1101 int width, int height) {
1102
1103 // All internally created RTs are also textures. We don't create
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +00001104 // 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 +00001105 GrAssert(rt->asTexture());
bsalomon@google.com99621082011-11-15 16:47:16 +00001106 GrAssert(width >= rt->width());
1107 GrAssert(height >= rt->height());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001108
1109 int samples = rt->numSamples();
1110 GrGLuint sbID;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001111 GL_CALL(GenRenderbuffers(1, &sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001112 if (!sbID) {
1113 return false;
1114 }
1115
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001116 int stencilFmtCnt = this->glCaps().stencilFormats().count();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001117 for (int i = 0; i < stencilFmtCnt; ++i) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001118 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001119 // we start with the last stencil format that succeeded in hopes
1120 // that we won't go through this loop more than once after the
1121 // first (painful) stencil creation.
1122 int sIdx = (i + fLastSuccessfulStencilFmtIdx) % stencilFmtCnt;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001123 const GrGLCaps::StencilFormat& sFmt =
1124 this->glCaps().stencilFormats()[sIdx];
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001125 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001126 // we do this "if" so that we don't call the multisample
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001127 // version on a GL that doesn't have an MSAA extension.
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001128 bool created;
1129 if (samples > 0) {
1130 created = renderbuffer_storage_msaa(fGLContextInfo,
1131 samples,
1132 sFmt.fInternalFormat,
1133 width, height);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001134 } else {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001135 GL_ALLOC_CALL(this->glInterface(),
1136 RenderbufferStorage(GR_GL_RENDERBUFFER,
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001137 sFmt.fInternalFormat,
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001138 width, height));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001139 created =
1140 (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(this->glInterface()));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001141 }
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001142 if (created) {
1143 // After sized formats we attempt an unsized format and take
1144 // whatever sizes GL gives us. In that case we query for the size.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001145 GrGLStencilBuffer::Format format = sFmt;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001146 get_stencil_rb_sizes(this->glInterface(), sbID, &format);
bsalomon@google.com72830222013-01-23 20:25:22 +00001147 static const bool kIsWrapped = false;
robertphillips@google.comf2e93fc2012-09-05 19:44:18 +00001148 SkAutoTUnref<GrStencilBuffer> sb(SkNEW_ARGS(GrGLStencilBuffer,
bsalomon@google.com72830222013-01-23 20:25:22 +00001149 (this, kIsWrapped, sbID, width, height,
robertphillips@google.comf2e93fc2012-09-05 19:44:18 +00001150 samples, format)));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001151 if (this->attachStencilBufferToRenderTarget(sb, rt)) {
1152 fLastSuccessfulStencilFmtIdx = sIdx;
robertphillips@google.com9fbcad02012-09-09 14:44:15 +00001153 sb->transferToCache();
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001154 rt->setStencilBuffer(sb);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001155 return true;
1156 }
1157 sb->abandon(); // otherwise we lose sbID
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001158 }
1159 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001160 GL_CALL(DeleteRenderbuffers(1, &sbID));
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001161 return false;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001162}
1163
1164bool GrGpuGL::attachStencilBufferToRenderTarget(GrStencilBuffer* sb,
1165 GrRenderTarget* rt) {
1166 GrGLRenderTarget* glrt = (GrGLRenderTarget*) rt;
1167
1168 GrGLuint fbo = glrt->renderFBOID();
1169
1170 if (NULL == sb) {
1171 if (NULL != rt->getStencilBuffer()) {
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_STENCIL_ATTACHMENT,
1174 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001175 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001176 GR_GL_DEPTH_ATTACHMENT,
1177 GR_GL_RENDERBUFFER, 0));
1178#if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001179 GrGLenum status;
1180 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001181 GrAssert(GR_GL_FRAMEBUFFER_COMPLETE == status);
1182#endif
1183 }
1184 return true;
1185 } else {
1186 GrGLStencilBuffer* glsb = (GrGLStencilBuffer*) sb;
1187 GrGLuint rb = glsb->renderbufferID();
1188
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001189 fHWBoundRenderTarget = NULL;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001190 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, fbo));
1191 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001192 GR_GL_STENCIL_ATTACHMENT,
1193 GR_GL_RENDERBUFFER, rb));
1194 if (glsb->format().fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001195 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001196 GR_GL_DEPTH_ATTACHMENT,
1197 GR_GL_RENDERBUFFER, rb));
1198 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001199 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001200 GR_GL_DEPTH_ATTACHMENT,
1201 GR_GL_RENDERBUFFER, 0));
reed@google.comac10a2d2010-12-22 21:39:39 +00001202 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001203
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001204 GrGLenum status;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001205 if (!this->glCaps().isColorConfigAndStencilFormatVerified(rt->config(),
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001206 glsb->format())) {
1207 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1208 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001209 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001210 GR_GL_STENCIL_ATTACHMENT,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001211 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001212 if (glsb->format().fPacked) {
1213 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1214 GR_GL_DEPTH_ATTACHMENT,
1215 GR_GL_RENDERBUFFER, 0));
1216 }
1217 return false;
1218 } else {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001219 fGLContextInfo.caps().markColorConfigAndStencilFormatAsVerified(
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001220 rt->config(),
1221 glsb->format());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001222 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001223 }
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001224 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001225 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001226}
1227
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001228////////////////////////////////////////////////////////////////////////////////
1229
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001230GrVertexBuffer* GrGpuGL::onCreateVertexBuffer(uint32_t size, bool dynamic) {
bsalomon@google.come49ad452013-02-20 19:33:20 +00001231 GrGLVertexBuffer::Desc desc;
1232 desc.fDynamic = dynamic;
1233 desc.fSizeInBytes = size;
1234 desc.fIsWrapped = false;
1235
bsalomon@google.com96966a52013-02-21 16:34:21 +00001236 if (this->glCaps().useNonVBOVertexAndIndexDynamicData() && desc.fDynamic) {
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001237 desc.fID = 0;
bsalomon@google.come49ad452013-02-20 19:33:20 +00001238 GrGLVertexBuffer* vertexBuffer = SkNEW_ARGS(GrGLVertexBuffer, (this, desc));
reed@google.comac10a2d2010-12-22 21:39:39 +00001239 return vertexBuffer;
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001240 } else {
1241 GL_CALL(GenBuffers(1, &desc.fID));
1242 if (desc.fID) {
1243 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, desc.fID));
1244 fHWGeometryState.setVertexBufferID(desc.fID);
1245 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1246 // make sure driver can allocate memory for this buffer
1247 GL_ALLOC_CALL(this->glInterface(),
1248 BufferData(GR_GL_ARRAY_BUFFER,
1249 desc.fSizeInBytes,
1250 NULL, // data ptr
1251 desc.fDynamic ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW));
1252 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
1253 GL_CALL(DeleteBuffers(1, &desc.fID));
1254 // deleting bound buffer does implicit bind to 0
1255 fHWGeometryState.setVertexBufferID(0);
1256 return NULL;
1257 }
1258 GrGLVertexBuffer* vertexBuffer = SkNEW_ARGS(GrGLVertexBuffer, (this, desc));
1259 return vertexBuffer;
1260 }
1261 return NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +00001262 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001263}
1264
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001265GrIndexBuffer* GrGpuGL::onCreateIndexBuffer(uint32_t size, bool dynamic) {
bsalomon@google.come49ad452013-02-20 19:33:20 +00001266 GrGLIndexBuffer::Desc desc;
1267 desc.fDynamic = dynamic;
1268 desc.fSizeInBytes = size;
1269 desc.fIsWrapped = false;
1270
bsalomon@google.com96966a52013-02-21 16:34:21 +00001271 if (this->glCaps().useNonVBOVertexAndIndexDynamicData() && desc.fDynamic) {
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001272 desc.fID = 0;
bsalomon@google.come49ad452013-02-20 19:33:20 +00001273 GrIndexBuffer* indexBuffer = SkNEW_ARGS(GrGLIndexBuffer, (this, desc));
reed@google.comac10a2d2010-12-22 21:39:39 +00001274 return indexBuffer;
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001275 } else {
1276 GL_CALL(GenBuffers(1, &desc.fID));
1277 if (desc.fID) {
1278 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, desc.fID));
1279 fHWGeometryState.setIndexBufferID(desc.fID);
1280 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1281 // make sure driver can allocate memory for this buffer
1282 GL_ALLOC_CALL(this->glInterface(),
1283 BufferData(GR_GL_ELEMENT_ARRAY_BUFFER,
1284 desc.fSizeInBytes,
1285 NULL, // data ptr
1286 desc.fDynamic ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW));
1287 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
1288 GL_CALL(DeleteBuffers(1, &desc.fID));
1289 // deleting bound buffer does implicit bind to 0
1290 fHWGeometryState.setIndexBufferID(0);
1291 return NULL;
1292 }
1293 GrIndexBuffer* indexBuffer = SkNEW_ARGS(GrGLIndexBuffer, (this, desc));
1294 return indexBuffer;
1295 }
1296 return NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +00001297 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001298}
1299
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00001300GrPath* GrGpuGL::onCreatePath(const SkPath& inPath) {
bsalomon@google.comf6601872012-08-28 21:11:35 +00001301 GrAssert(fCaps.pathStencilingSupport());
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001302 return SkNEW_ARGS(GrGLPath, (this, inPath));
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00001303}
1304
bsalomon@google.coma3201942012-06-21 19:58:20 +00001305void GrGpuGL::flushScissor() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001306 const GrDrawState& drawState = this->getDrawState();
1307 const GrGLRenderTarget* rt =
1308 static_cast<const GrGLRenderTarget*>(drawState.getRenderTarget());
1309
1310 GrAssert(NULL != rt);
1311 const GrGLIRect& vp = rt->getViewport();
reed@google.comac10a2d2010-12-22 21:39:39 +00001312
bsalomon@google.coma3201942012-06-21 19:58:20 +00001313 if (fScissorState.fEnabled) {
1314 GrGLIRect scissor;
1315 scissor.setRelativeTo(vp,
1316 fScissorState.fRect.fLeft,
1317 fScissorState.fRect.fTop,
1318 fScissorState.fRect.width(),
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001319 fScissorState.fRect.height(),
1320 rt->origin());
bsalomon@google.coma3201942012-06-21 19:58:20 +00001321 // if the scissor fully contains the viewport then we fall through and
1322 // disable the scissor test.
1323 if (!scissor.contains(vp)) {
1324 if (fHWScissorSettings.fRect != scissor) {
1325 scissor.pushToGLScissor(this->glInterface());
1326 fHWScissorSettings.fRect = scissor;
1327 }
1328 if (kYes_TriState != fHWScissorSettings.fEnabled) {
1329 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1330 fHWScissorSettings.fEnabled = kYes_TriState;
1331 }
1332 return;
1333 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001334 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001335 if (kNo_TriState != fHWScissorSettings.fEnabled) {
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001336 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.coma3201942012-06-21 19:58:20 +00001337 fHWScissorSettings.fEnabled = kNo_TriState;
1338 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001339 }
1340}
1341
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001342void GrGpuGL::onClear(const GrIRect* rect, GrColor color) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001343 const GrDrawState& drawState = this->getDrawState();
1344 const GrRenderTarget* rt = drawState.getRenderTarget();
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001345 // parent class should never let us get here with no RT
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001346 GrAssert(NULL != rt);
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001347
bsalomon@google.com74b98712011-11-11 19:46:16 +00001348 GrIRect clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001349 if (NULL != rect) {
1350 // flushScissor expects rect to be clipped to the target.
bsalomon@google.com74b98712011-11-11 19:46:16 +00001351 clippedRect = *rect;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001352 GrIRect rtRect = SkIRect::MakeWH(rt->width(), rt->height());
bsalomon@google.com74b98712011-11-11 19:46:16 +00001353 if (clippedRect.intersect(rtRect)) {
1354 rect = &clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001355 } else {
1356 return;
1357 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001358 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001359 this->flushRenderTarget(rect);
bsalomon@google.coma3201942012-06-21 19:58:20 +00001360 GrAutoTRestore<ScissorState> asr(&fScissorState);
1361 fScissorState.fEnabled = (NULL != rect);
1362 if (fScissorState.fEnabled) {
1363 fScissorState.fRect = *rect;
1364 }
1365 this->flushScissor();
bsalomon@google.com74b98712011-11-11 19:46:16 +00001366
1367 GrGLfloat r, g, b, a;
1368 static const GrGLfloat scale255 = 1.f / 255.f;
1369 a = GrColorUnpackA(color) * scale255;
1370 GrGLfloat scaleRGB = scale255;
bsalomon@google.com74b98712011-11-11 19:46:16 +00001371 r = GrColorUnpackR(color) * scaleRGB;
1372 g = GrColorUnpackG(color) * scaleRGB;
1373 b = GrColorUnpackB(color) * scaleRGB;
1374
1375 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00001376 fHWWriteToColor = kYes_TriState;
bsalomon@google.com74b98712011-11-11 19:46:16 +00001377 GL_CALL(ClearColor(r, g, b, a));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001378 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001379}
1380
bsalomon@google.comedc177d2011-08-05 15:46:40 +00001381void GrGpuGL::clearStencil() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001382 if (NULL == this->getDrawState().getRenderTarget()) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001383 return;
1384 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001385
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001386 this->flushRenderTarget(&GrIRect::EmptyIRect());
1387
bsalomon@google.coma3201942012-06-21 19:58:20 +00001388 GrAutoTRestore<ScissorState> asr(&fScissorState);
1389 fScissorState.fEnabled = false;
1390 this->flushScissor();
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001391
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001392 GL_CALL(StencilMask(0xffffffff));
1393 GL_CALL(ClearStencil(0));
1394 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001395 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001396}
1397
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001398void GrGpuGL::clearStencilClip(const GrIRect& rect, bool insideClip) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001399 const GrDrawState& drawState = this->getDrawState();
1400 const GrRenderTarget* rt = drawState.getRenderTarget();
1401 GrAssert(NULL != rt);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001402
1403 // this should only be called internally when we know we have a
1404 // stencil buffer.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001405 GrAssert(NULL != rt->getStencilBuffer());
1406 GrGLint stencilBitCount = rt->getStencilBuffer()->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001407#if 0
reed@google.comac10a2d2010-12-22 21:39:39 +00001408 GrAssert(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00001409 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001410#else
1411 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001412 // ANGLE a partial stencil mask will cause clears to be
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001413 // turned into draws. Our contract on GrDrawTarget says that
1414 // changing the clip between stencil passes may or may not
1415 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00001416 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001417#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001418 GrGLint value;
1419 if (insideClip) {
1420 value = (1 << (stencilBitCount - 1));
1421 } else {
1422 value = 0;
1423 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001424 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.coma3201942012-06-21 19:58:20 +00001425
1426 GrAutoTRestore<ScissorState> asr(&fScissorState);
1427 fScissorState.fEnabled = true;
1428 fScissorState.fRect = rect;
1429 this->flushScissor();
1430
caryclark@google.comcf6285b2012-06-06 12:09:01 +00001431 GL_CALL(StencilMask((uint32_t) clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001432 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001433 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001434 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001435}
1436
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001437void GrGpuGL::onForceRenderTargetFlush() {
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001438 this->flushRenderTarget(&GrIRect::EmptyIRect());
reed@google.comac10a2d2010-12-22 21:39:39 +00001439}
1440
bsalomon@google.comc4364992011-11-07 15:54:49 +00001441bool GrGpuGL::readPixelsWillPayForYFlip(GrRenderTarget* renderTarget,
1442 int left, int top,
1443 int width, int height,
1444 GrPixelConfig config,
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001445 size_t rowBytes) const {
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001446 // If this rendertarget is aready TopLeft, we don't need to flip.
1447 if (kTopLeft_GrSurfaceOrigin == renderTarget->origin()) {
1448 return false;
1449 }
1450
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001451 // if GL can do the flip then we'll never pay for it.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001452 if (this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001453 return false;
1454 }
1455
1456 // If we have to do memcpy to handle non-trim rowBytes then we
bsalomon@google.com7107fa72011-11-10 14:54:14 +00001457 // get the flip for free. Otherwise it costs.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001458 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001459 return true;
1460 }
1461 // If we have to do memcpys to handle rowBytes then y-flip is free
1462 // Note the rowBytes might be tight to the passed in data, but if data
1463 // gets clipped in x to the target the rowBytes will no longer be tight.
1464 if (left >= 0 && (left + width) < renderTarget->width()) {
1465 return 0 == rowBytes ||
1466 GrBytesPerPixel(config) * width == rowBytes;
1467 } else {
1468 return false;
1469 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001470}
1471
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001472bool GrGpuGL::onReadPixels(GrRenderTarget* target,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001473 int left, int top,
1474 int width, int height,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001475 GrPixelConfig config,
1476 void* buffer,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001477 size_t rowBytes) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001478 GrGLenum format;
1479 GrGLenum type;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001480 bool flipY = kBottomLeft_GrSurfaceOrigin == target->origin();
bsalomon@google.com280e99f2012-01-05 16:17:38 +00001481 if (!this->configToGLFormats(config, false, NULL, &format, &type)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001482 return false;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001483 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001484 size_t bpp = GrBytesPerPixel(config);
1485 if (!adjust_pixel_ops_params(target->width(), target->height(), bpp,
1486 &left, &top, &width, &height,
1487 const_cast<const void**>(&buffer),
1488 &rowBytes)) {
1489 return false;
1490 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001491
bsalomon@google.comc6980972011-11-02 19:57:21 +00001492 // resolve the render target if necessary
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001493 GrGLRenderTarget* tgt = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001494 GrDrawState::AutoRenderTargetRestore artr;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001495 switch (tgt->getResolveType()) {
1496 case GrGLRenderTarget::kCantResolve_ResolveType:
1497 return false;
1498 case GrGLRenderTarget::kAutoResolves_ResolveType:
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001499 artr.set(this->drawState(), target);
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001500 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001501 break;
1502 case GrGLRenderTarget::kCanResolve_ResolveType:
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001503 this->onResolveRenderTarget(tgt);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001504 // we don't track the state of the READ FBO ID.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001505 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1506 tgt->textureFBOID()));
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001507 break;
1508 default:
1509 GrCrash("Unknown resolve type");
reed@google.comac10a2d2010-12-22 21:39:39 +00001510 }
1511
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001512 const GrGLIRect& glvp = tgt->getViewport();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001513
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001514 // the read rect is viewport-relative
1515 GrGLIRect readRect;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001516 readRect.setRelativeTo(glvp, left, top, width, height, target->origin());
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001517
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001518 size_t tightRowBytes = bpp * width;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001519 if (0 == rowBytes) {
1520 rowBytes = tightRowBytes;
1521 }
1522 size_t readDstRowBytes = tightRowBytes;
1523 void* readDst = buffer;
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001524
bsalomon@google.comc6980972011-11-02 19:57:21 +00001525 // determine if GL can read using the passed rowBytes or if we need
1526 // a scratch buffer.
1527 SkAutoSMalloc<32 * sizeof(GrColor)> scratch;
1528 if (rowBytes != tightRowBytes) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001529 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.comc6980972011-11-02 19:57:21 +00001530 GrAssert(!(rowBytes % sizeof(GrColor)));
1531 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, rowBytes / sizeof(GrColor)));
1532 readDstRowBytes = rowBytes;
1533 } else {
1534 scratch.reset(tightRowBytes * height);
1535 readDst = scratch.get();
1536 }
1537 }
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001538 if (flipY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001539 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 1));
1540 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001541 GL_CALL(ReadPixels(readRect.fLeft, readRect.fBottom,
1542 readRect.fWidth, readRect.fHeight,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001543 format, type, readDst));
1544 if (readDstRowBytes != tightRowBytes) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001545 GrAssert(this->glCaps().packRowLengthSupport());
bsalomon@google.comc6980972011-11-02 19:57:21 +00001546 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
1547 }
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001548 if (flipY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001549 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 0));
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001550 flipY = false;
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001551 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001552
1553 // now reverse the order of the rows, since GL's are bottom-to-top, but our
bsalomon@google.comc6980972011-11-02 19:57:21 +00001554 // API presents top-to-bottom. We must preserve the padding contents. Note
1555 // that the above readPixels did not overwrite the padding.
1556 if (readDst == buffer) {
1557 GrAssert(rowBytes == readDstRowBytes);
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001558 if (flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001559 scratch.reset(tightRowBytes);
1560 void* tmpRow = scratch.get();
1561 // flip y in-place by rows
1562 const int halfY = height >> 1;
1563 char* top = reinterpret_cast<char*>(buffer);
1564 char* bottom = top + (height - 1) * rowBytes;
1565 for (int y = 0; y < halfY; y++) {
1566 memcpy(tmpRow, top, tightRowBytes);
1567 memcpy(top, bottom, tightRowBytes);
1568 memcpy(bottom, tmpRow, tightRowBytes);
1569 top += rowBytes;
1570 bottom -= rowBytes;
1571 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001572 }
1573 } else {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001574 GrAssert(readDst != buffer); GrAssert(rowBytes != tightRowBytes);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001575 // copy from readDst to buffer while flipping y
caryclark@google.comcf6285b2012-06-06 12:09:01 +00001576 // const int halfY = height >> 1;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001577 const char* src = reinterpret_cast<const char*>(readDst);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001578 char* dst = reinterpret_cast<char*>(buffer);
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001579 if (flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001580 dst += (height-1) * rowBytes;
1581 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001582 for (int y = 0; y < height; y++) {
1583 memcpy(dst, src, tightRowBytes);
1584 src += readDstRowBytes;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001585 if (!flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001586 dst += rowBytes;
1587 } else {
1588 dst -= rowBytes;
1589 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001590 }
1591 }
1592 return true;
1593}
1594
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001595void GrGpuGL::flushRenderTarget(const GrIRect* bound) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001596
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001597 GrGLRenderTarget* rt =
1598 static_cast<GrGLRenderTarget*>(this->drawState()->getRenderTarget());
1599 GrAssert(NULL != rt);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001600
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001601 if (fHWBoundRenderTarget != rt) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001602 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, rt->renderFBOID()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001603 #if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001604 GrGLenum status;
1605 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001606 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
reed@google.comb9255d52011-06-13 18:54:59 +00001607 GrPrintf("GrGpuGL::flushRenderTarget glCheckFramebufferStatus %x\n", status);
reed@google.comac10a2d2010-12-22 21:39:39 +00001608 }
1609 #endif
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001610 fHWBoundRenderTarget = rt;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001611 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.coma3201942012-06-21 19:58:20 +00001612 if (fHWViewport != vp) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001613 vp.pushToGLViewport(this->glInterface());
bsalomon@google.coma3201942012-06-21 19:58:20 +00001614 fHWViewport = vp;
reed@google.comac10a2d2010-12-22 21:39:39 +00001615 }
1616 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001617 if (NULL == bound || !bound->isEmpty()) {
1618 rt->flagAsNeedingResolve(bound);
1619 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001620}
1621
twiz@google.com0f31ca72011-03-18 17:38:11 +00001622GrGLenum gPrimitiveType2GLMode[] = {
1623 GR_GL_TRIANGLES,
1624 GR_GL_TRIANGLE_STRIP,
1625 GR_GL_TRIANGLE_FAN,
1626 GR_GL_POINTS,
1627 GR_GL_LINES,
1628 GR_GL_LINE_STRIP
reed@google.comac10a2d2010-12-22 21:39:39 +00001629};
1630
bsalomon@google.comd302f142011-03-03 13:54:13 +00001631#define SWAP_PER_DRAW 0
1632
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001633#if SWAP_PER_DRAW
bsalomon@google.comd302f142011-03-03 13:54:13 +00001634 #if GR_MAC_BUILD
1635 #include <AGL/agl.h>
1636 #elif GR_WIN32_BUILD
bsalomon@google.comce7357d2012-06-25 17:49:25 +00001637 #include <gl/GL.h>
bsalomon@google.comd302f142011-03-03 13:54:13 +00001638 void SwapBuf() {
1639 DWORD procID = GetCurrentProcessId();
1640 HWND hwnd = GetTopWindow(GetDesktopWindow());
1641 while(hwnd) {
1642 DWORD wndProcID = 0;
1643 GetWindowThreadProcessId(hwnd, &wndProcID);
1644 if(wndProcID == procID) {
1645 SwapBuffers(GetDC(hwnd));
1646 }
1647 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
1648 }
1649 }
1650 #endif
1651#endif
1652
bsalomon@google.com74749cd2013-01-30 16:12:41 +00001653void GrGpuGL::onGpuDraw(const DrawInfo& info) {
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00001654 size_t indexOffsetInBytes;
1655 this->setupGeometry(info, &indexOffsetInBytes);
reed@google.comac10a2d2010-12-22 21:39:39 +00001656
bsalomon@google.com74749cd2013-01-30 16:12:41 +00001657 GrAssert((size_t)info.primitiveType() < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001658
bsalomon@google.com74749cd2013-01-30 16:12:41 +00001659 if (info.isIndexed()) {
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00001660 GrGLvoid* indices =
1661 reinterpret_cast<GrGLvoid*>(indexOffsetInBytes + sizeof(uint16_t) * info.startIndex());
bsalomon@google.com74749cd2013-01-30 16:12:41 +00001662 // info.startVertex() was accounted for by setupGeometry.
1663 GL_CALL(DrawElements(gPrimitiveType2GLMode[info.primitiveType()],
1664 info.indexCount(),
1665 GR_GL_UNSIGNED_SHORT,
1666 indices));
1667 } else {
1668 // Pass 0 for parameter first. We have to adjust glVertexAttribPointer() to account for
1669 // startVertex in the DrawElements case. So we always rely on setupGeometry to have
1670 // accounted for startVertex.
1671 GL_CALL(DrawArrays(gPrimitiveType2GLMode[info.primitiveType()], 0, info.vertexCount()));
1672 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00001673#if SWAP_PER_DRAW
1674 glFlush();
1675 #if GR_MAC_BUILD
1676 aglSwapBuffers(aglGetCurrentContext());
1677 int set_a_break_pt_here = 9;
1678 aglSwapBuffers(aglGetCurrentContext());
1679 #elif GR_WIN32_BUILD
1680 SwapBuf();
1681 int set_a_break_pt_here = 9;
1682 SwapBuf();
1683 #endif
1684#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001685}
1686
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001687namespace {
bsalomon@google.com100abf42012-09-05 17:40:04 +00001688
1689static const uint16_t kOnes16 = static_cast<uint16_t>(~0);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001690const GrStencilSettings& winding_nv_path_stencil_settings() {
1691 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
1692 kIncClamp_StencilOp,
1693 kIncClamp_StencilOp,
1694 kAlwaysIfInClip_StencilFunc,
bsalomon@google.com100abf42012-09-05 17:40:04 +00001695 kOnes16, kOnes16, kOnes16);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001696 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
1697}
1698const GrStencilSettings& even_odd_nv_path_stencil_settings() {
1699 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
1700 kInvert_StencilOp,
1701 kInvert_StencilOp,
1702 kAlwaysIfInClip_StencilFunc,
bsalomon@google.com100abf42012-09-05 17:40:04 +00001703 kOnes16, kOnes16, kOnes16);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001704 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
1705}
1706}
1707
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001708void GrGpuGL::setStencilPathSettings(const GrPath&,
sugoi@google.com12b4e272012-12-06 20:13:11 +00001709 SkPath::FillType fill,
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001710 GrStencilSettings* settings) {
1711 switch (fill) {
sugoi@google.com12b4e272012-12-06 20:13:11 +00001712 case SkPath::kEvenOdd_FillType:
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001713 *settings = even_odd_nv_path_stencil_settings();
1714 return;
sugoi@google.com12b4e272012-12-06 20:13:11 +00001715 case SkPath::kWinding_FillType:
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001716 *settings = winding_nv_path_stencil_settings();
1717 return;
1718 default:
1719 GrCrash("Unexpected path fill.");
1720 }
1721}
1722
sugoi@google.com12b4e272012-12-06 20:13:11 +00001723void GrGpuGL::onGpuStencilPath(const GrPath* path, SkPath::FillType fill) {
bsalomon@google.comf6601872012-08-28 21:11:35 +00001724 GrAssert(fCaps.pathStencilingSupport());
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001725
1726 GrGLuint id = static_cast<const GrGLPath*>(path)->pathID();
1727 GrDrawState* drawState = this->drawState();
1728 GrAssert(NULL != drawState->getRenderTarget());
1729 if (NULL == drawState->getRenderTarget()->getStencilBuffer()) {
1730 return;
1731 }
1732
1733 // Decide how to manipulate the stencil buffer based on the fill rule.
1734 // Also, assert that the stencil settings we set in setStencilPathSettings
1735 // are present.
1736 GrAssert(!fStencilSettings.isTwoSided());
1737 GrGLenum fillMode;
1738 switch (fill) {
sugoi@google.com12b4e272012-12-06 20:13:11 +00001739 case SkPath::kWinding_FillType:
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001740 fillMode = GR_GL_COUNT_UP;
1741 GrAssert(kIncClamp_StencilOp ==
1742 fStencilSettings.passOp(GrStencilSettings::kFront_Face));
1743 GrAssert(kIncClamp_StencilOp ==
1744 fStencilSettings.failOp(GrStencilSettings::kFront_Face));
1745 break;
sugoi@google.com12b4e272012-12-06 20:13:11 +00001746 case SkPath::kEvenOdd_FillType:
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001747 fillMode = GR_GL_INVERT;
1748 GrAssert(kInvert_StencilOp ==
1749 fStencilSettings.passOp(GrStencilSettings::kFront_Face));
1750 GrAssert(kInvert_StencilOp ==
1751 fStencilSettings.failOp(GrStencilSettings::kFront_Face));
1752 break;
1753 default:
1754 // Only the above two fill rules are allowed.
1755 GrCrash("Unexpected path fill.");
bsalomon@google.com548a4332012-07-11 19:45:22 +00001756 return; // suppress unused var warning.
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001757 }
1758 GrGLint writeMask = fStencilSettings.writeMask(GrStencilSettings::kFront_Face);
1759 GL_CALL(StencilFillPath(id, fillMode, writeMask));
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00001760}
1761
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001762void GrGpuGL::onResolveRenderTarget(GrRenderTarget* target) {
1763
1764 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(target);
reed@google.comac10a2d2010-12-22 21:39:39 +00001765
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001766 if (rt->needsResolve()) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001767 GrAssert(GrGLCaps::kNone_MSFBOType != this->glCaps().msFBOType());
reed@google.comac10a2d2010-12-22 21:39:39 +00001768 GrAssert(rt->textureFBOID() != rt->renderFBOID());
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001769 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1770 rt->renderFBOID()));
1771 GL_CALL(BindFramebuffer(GR_GL_DRAW_FRAMEBUFFER,
1772 rt->textureFBOID()));
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001773 // make sure we go through flushRenderTarget() since we've modified
1774 // the bound DRAW FBO ID.
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001775 fHWBoundRenderTarget = NULL;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001776 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001777 const GrIRect dirtyRect = rt->getResolveRect();
1778 GrGLIRect r;
bsalomon@google.coma3201942012-06-21 19:58:20 +00001779 r.setRelativeTo(vp, dirtyRect.fLeft, dirtyRect.fTop,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001780 dirtyRect.width(), dirtyRect.height(), target->origin());
reed@google.comac10a2d2010-12-22 21:39:39 +00001781
bsalomon@google.coma3201942012-06-21 19:58:20 +00001782 GrAutoTRestore<ScissorState> asr;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001783 if (GrGLCaps::kAppleES_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001784 // Apple's extension uses the scissor as the blit bounds.
bsalomon@google.coma3201942012-06-21 19:58:20 +00001785 asr.reset(&fScissorState);
1786 fScissorState.fEnabled = true;
1787 fScissorState.fRect = dirtyRect;
1788 this->flushScissor();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001789 GL_CALL(ResolveMultisampleFramebuffer());
reed@google.comac10a2d2010-12-22 21:39:39 +00001790 } else {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001791 if (GrGLCaps::kDesktopARB_MSFBOType != this->glCaps().msFBOType()) {
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001792 // this respects the scissor during the blit, so disable it.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001793 GrAssert(GrGLCaps::kDesktopEXT_MSFBOType ==
1794 this->glCaps().msFBOType());
bsalomon@google.coma3201942012-06-21 19:58:20 +00001795 asr.reset(&fScissorState);
1796 fScissorState.fEnabled = false;
1797 this->flushScissor();
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001798 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001799 int right = r.fLeft + r.fWidth;
1800 int top = r.fBottom + r.fHeight;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001801 GL_CALL(BlitFramebuffer(r.fLeft, r.fBottom, right, top,
1802 r.fLeft, r.fBottom, right, top,
1803 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001804 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001805 rt->flagAsResolved();
reed@google.comac10a2d2010-12-22 21:39:39 +00001806 }
1807}
1808
bsalomon@google.com411dad02012-06-05 20:24:20 +00001809namespace {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001810
bsalomon@google.com411dad02012-06-05 20:24:20 +00001811GrGLenum gr_to_gl_stencil_func(GrStencilFunc basicFunc) {
1812 static const GrGLenum gTable[] = {
1813 GR_GL_ALWAYS, // kAlways_StencilFunc
1814 GR_GL_NEVER, // kNever_StencilFunc
1815 GR_GL_GREATER, // kGreater_StencilFunc
1816 GR_GL_GEQUAL, // kGEqual_StencilFunc
1817 GR_GL_LESS, // kLess_StencilFunc
1818 GR_GL_LEQUAL, // kLEqual_StencilFunc,
1819 GR_GL_EQUAL, // kEqual_StencilFunc,
1820 GR_GL_NOTEQUAL, // kNotEqual_StencilFunc,
1821 };
1822 GR_STATIC_ASSERT(GR_ARRAY_COUNT(gTable) == kBasicStencilFuncCount);
1823 GR_STATIC_ASSERT(0 == kAlways_StencilFunc);
1824 GR_STATIC_ASSERT(1 == kNever_StencilFunc);
1825 GR_STATIC_ASSERT(2 == kGreater_StencilFunc);
1826 GR_STATIC_ASSERT(3 == kGEqual_StencilFunc);
1827 GR_STATIC_ASSERT(4 == kLess_StencilFunc);
1828 GR_STATIC_ASSERT(5 == kLEqual_StencilFunc);
1829 GR_STATIC_ASSERT(6 == kEqual_StencilFunc);
1830 GR_STATIC_ASSERT(7 == kNotEqual_StencilFunc);
1831 GrAssert((unsigned) basicFunc < kBasicStencilFuncCount);
1832
1833 return gTable[basicFunc];
1834}
1835
1836GrGLenum gr_to_gl_stencil_op(GrStencilOp op) {
1837 static const GrGLenum gTable[] = {
1838 GR_GL_KEEP, // kKeep_StencilOp
1839 GR_GL_REPLACE, // kReplace_StencilOp
1840 GR_GL_INCR_WRAP, // kIncWrap_StencilOp
1841 GR_GL_INCR, // kIncClamp_StencilOp
1842 GR_GL_DECR_WRAP, // kDecWrap_StencilOp
1843 GR_GL_DECR, // kDecClamp_StencilOp
1844 GR_GL_ZERO, // kZero_StencilOp
1845 GR_GL_INVERT, // kInvert_StencilOp
1846 };
1847 GR_STATIC_ASSERT(GR_ARRAY_COUNT(gTable) == kStencilOpCount);
1848 GR_STATIC_ASSERT(0 == kKeep_StencilOp);
1849 GR_STATIC_ASSERT(1 == kReplace_StencilOp);
1850 GR_STATIC_ASSERT(2 == kIncWrap_StencilOp);
1851 GR_STATIC_ASSERT(3 == kIncClamp_StencilOp);
1852 GR_STATIC_ASSERT(4 == kDecWrap_StencilOp);
1853 GR_STATIC_ASSERT(5 == kDecClamp_StencilOp);
1854 GR_STATIC_ASSERT(6 == kZero_StencilOp);
1855 GR_STATIC_ASSERT(7 == kInvert_StencilOp);
1856 GrAssert((unsigned) op < kStencilOpCount);
1857 return gTable[op];
1858}
1859
1860void set_gl_stencil(const GrGLInterface* gl,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001861 const GrStencilSettings& settings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00001862 GrGLenum glFace,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001863 GrStencilSettings::Face grFace) {
1864 GrGLenum glFunc = gr_to_gl_stencil_func(settings.func(grFace));
1865 GrGLenum glFailOp = gr_to_gl_stencil_op(settings.failOp(grFace));
1866 GrGLenum glPassOp = gr_to_gl_stencil_op(settings.passOp(grFace));
1867
1868 GrGLint ref = settings.funcRef(grFace);
1869 GrGLint mask = settings.funcMask(grFace);
1870 GrGLint writeMask = settings.writeMask(grFace);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001871
1872 if (GR_GL_FRONT_AND_BACK == glFace) {
1873 // we call the combined func just in case separate stencil is not
1874 // supported.
1875 GR_GL_CALL(gl, StencilFunc(glFunc, ref, mask));
1876 GR_GL_CALL(gl, StencilMask(writeMask));
1877 GR_GL_CALL(gl, StencilOp(glFailOp, glPassOp, glPassOp));
1878 } else {
1879 GR_GL_CALL(gl, StencilFuncSeparate(glFace, glFunc, ref, mask));
1880 GR_GL_CALL(gl, StencilMaskSeparate(glFace, writeMask));
1881 GR_GL_CALL(gl, StencilOpSeparate(glFace, glFailOp, glPassOp, glPassOp));
1882 }
1883}
1884}
bsalomon@google.comd302f142011-03-03 13:54:13 +00001885
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001886void GrGpuGL::flushStencil(DrawType type) {
1887 if (kStencilPath_DrawType == type) {
1888 GrAssert(!fStencilSettings.isTwoSided());
1889 // Just the func, ref, and mask is set here. The op and write mask are params to the call
1890 // that draws the path to the SB (glStencilFillPath)
1891 GrGLenum func =
1892 gr_to_gl_stencil_func(fStencilSettings.func(GrStencilSettings::kFront_Face));
1893 GL_CALL(PathStencilFunc(func,
1894 fStencilSettings.funcRef(GrStencilSettings::kFront_Face),
1895 fStencilSettings.funcMask(GrStencilSettings::kFront_Face)));
1896 } else if (fHWStencilSettings != fStencilSettings) {
1897 if (fStencilSettings.isDisabled()) {
1898 if (kNo_TriState != fHWStencilTestEnabled) {
1899 GL_CALL(Disable(GR_GL_STENCIL_TEST));
1900 fHWStencilTestEnabled = kNo_TriState;
1901 }
1902 } else {
1903 if (kYes_TriState != fHWStencilTestEnabled) {
1904 GL_CALL(Enable(GR_GL_STENCIL_TEST));
1905 fHWStencilTestEnabled = kYes_TriState;
1906 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001907 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001908 if (!fStencilSettings.isDisabled()) {
bsalomon@google.comf6601872012-08-28 21:11:35 +00001909 if (this->getCaps().twoSidedStencilSupport()) {
bsalomon@google.com411dad02012-06-05 20:24:20 +00001910 set_gl_stencil(this->glInterface(),
bsalomon@google.coma3201942012-06-21 19:58:20 +00001911 fStencilSettings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00001912 GR_GL_FRONT,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001913 GrStencilSettings::kFront_Face);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001914 set_gl_stencil(this->glInterface(),
bsalomon@google.coma3201942012-06-21 19:58:20 +00001915 fStencilSettings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00001916 GR_GL_BACK,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001917 GrStencilSettings::kBack_Face);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001918 } else {
bsalomon@google.com411dad02012-06-05 20:24:20 +00001919 set_gl_stencil(this->glInterface(),
bsalomon@google.coma3201942012-06-21 19:58:20 +00001920 fStencilSettings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00001921 GR_GL_FRONT_AND_BACK,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001922 GrStencilSettings::kFront_Face);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001923 }
1924 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001925 fHWStencilSettings = fStencilSettings;
reed@google.comac10a2d2010-12-22 21:39:39 +00001926 }
1927}
1928
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001929void GrGpuGL::flushAAState(DrawType type) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001930 const GrRenderTarget* rt = this->getDrawState().getRenderTarget();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001931 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001932 // ES doesn't support toggling GL_MULTISAMPLE and doesn't have
1933 // smooth lines.
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001934 // we prefer smooth lines over multisampled lines
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00001935 bool smoothLines = false;
1936
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001937 if (kDrawLines_DrawType == type) {
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00001938 smoothLines = this->willUseHWAALines();
1939 if (smoothLines) {
1940 if (kYes_TriState != fHWAAState.fSmoothLineEnabled) {
1941 GL_CALL(Enable(GR_GL_LINE_SMOOTH));
1942 fHWAAState.fSmoothLineEnabled = kYes_TriState;
1943 // must disable msaa to use line smoothing
1944 if (rt->isMultisampled() &&
1945 kNo_TriState != fHWAAState.fMSAAEnabled) {
1946 GL_CALL(Disable(GR_GL_MULTISAMPLE));
1947 fHWAAState.fMSAAEnabled = kNo_TriState;
1948 }
1949 }
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001950 } else {
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00001951 if (kNo_TriState != fHWAAState.fSmoothLineEnabled) {
1952 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
1953 fHWAAState.fSmoothLineEnabled = kNo_TriState;
1954 }
1955 }
1956 }
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001957 if (!smoothLines && rt->isMultisampled()) {
1958 // FIXME: GL_NV_pr doesn't seem to like MSAA disabled. The paths
1959 // convex hulls of each segment appear to get filled.
1960 bool enableMSAA = kStencilPath_DrawType == type ||
1961 this->getDrawState().isHWAntialiasState();
1962 if (enableMSAA) {
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00001963 if (kYes_TriState != fHWAAState.fMSAAEnabled) {
1964 GL_CALL(Enable(GR_GL_MULTISAMPLE));
1965 fHWAAState.fMSAAEnabled = kYes_TriState;
1966 }
1967 } else {
1968 if (kNo_TriState != fHWAAState.fMSAAEnabled) {
1969 GL_CALL(Disable(GR_GL_MULTISAMPLE));
1970 fHWAAState.fMSAAEnabled = kNo_TriState;
1971 }
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001972 }
1973 }
1974 }
1975}
1976
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00001977void GrGpuGL::flushBlend(bool isLines,
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001978 GrBlendCoeff srcCoeff,
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001979 GrBlendCoeff dstCoeff) {
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00001980 if (isLines && this->willUseHWAALines()) {
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001981 if (kYes_TriState != fHWBlendState.fEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001982 GL_CALL(Enable(GR_GL_BLEND));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001983 fHWBlendState.fEnabled = kYes_TriState;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001984 }
bsalomon@google.com47059542012-06-06 20:51:20 +00001985 if (kSA_GrBlendCoeff != fHWBlendState.fSrcCoeff ||
1986 kISA_GrBlendCoeff != fHWBlendState.fDstCoeff) {
1987 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[kSA_GrBlendCoeff],
1988 gXfermodeCoeff2Blend[kISA_GrBlendCoeff]));
1989 fHWBlendState.fSrcCoeff = kSA_GrBlendCoeff;
1990 fHWBlendState.fDstCoeff = kISA_GrBlendCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001991 }
1992 } else {
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001993 // any optimization to disable blending should
1994 // have already been applied and tweaked the coeffs
1995 // to (1, 0).
bsalomon@google.com47059542012-06-06 20:51:20 +00001996 bool blendOff = kOne_GrBlendCoeff == srcCoeff &&
1997 kZero_GrBlendCoeff == dstCoeff;
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001998 if (blendOff) {
1999 if (kNo_TriState != fHWBlendState.fEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002000 GL_CALL(Disable(GR_GL_BLEND));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002001 fHWBlendState.fEnabled = kNo_TriState;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002002 }
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002003 } else {
2004 if (kYes_TriState != fHWBlendState.fEnabled) {
2005 GL_CALL(Enable(GR_GL_BLEND));
2006 fHWBlendState.fEnabled = kYes_TriState;
2007 }
2008 if (fHWBlendState.fSrcCoeff != srcCoeff ||
2009 fHWBlendState.fDstCoeff != dstCoeff) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002010 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
2011 gXfermodeCoeff2Blend[dstCoeff]));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002012 fHWBlendState.fSrcCoeff = srcCoeff;
2013 fHWBlendState.fDstCoeff = dstCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002014 }
bsalomon@google.coma5d056a2012-03-27 15:59:58 +00002015 GrColor blendConst = this->getDrawState().getBlendConstant();
bsalomon@google.com271cffc2011-05-20 14:13:56 +00002016 if ((BlendCoeffReferencesConstant(srcCoeff) ||
2017 BlendCoeffReferencesConstant(dstCoeff)) &&
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002018 (!fHWBlendState.fConstColorValid ||
2019 fHWBlendState.fConstColor != blendConst)) {
bsalomon@google.com75347472012-09-17 17:23:21 +00002020 GrGLfloat c[4];
2021 GrColorToRGBAFloat(blendConst, c);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002022 GL_CALL(BlendColor(c[0], c[1], c[2], c[3]));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002023 fHWBlendState.fConstColor = blendConst;
2024 fHWBlendState.fConstColorValid = true;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002025 }
2026 }
2027 }
2028}
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002029namespace {
2030
bsalomon@google.com6d003d12012-09-11 15:45:20 +00002031inline void set_tex_swizzle(GrGLenum swizzle[4], const GrGLInterface* gl) {
bsalomon@google.com4d063de2012-05-31 17:59:23 +00002032 GR_GL_CALL(gl, TexParameteriv(GR_GL_TEXTURE_2D,
2033 GR_GL_TEXTURE_SWIZZLE_RGBA,
2034 reinterpret_cast<const GrGLint*>(swizzle)));
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002035}
bsalomon@google.comb8670992012-07-25 21:27:09 +00002036
bsalomon@google.com6c76d242012-09-07 16:52:24 +00002037inline GrGLenum tile_to_gl_wrap(SkShader::TileMode tm) {
bsalomon@google.comb8670992012-07-25 21:27:09 +00002038 static const GrGLenum gWrapModes[] = {
2039 GR_GL_CLAMP_TO_EDGE,
2040 GR_GL_REPEAT,
2041 GR_GL_MIRRORED_REPEAT
2042 };
2043 GrAssert((unsigned) tm <= SK_ARRAY_COUNT(gWrapModes));
2044 GR_STATIC_ASSERT(0 == SkShader::kClamp_TileMode);
2045 GR_STATIC_ASSERT(1 == SkShader::kRepeat_TileMode);
2046 GR_STATIC_ASSERT(2 == SkShader::kMirror_TileMode);
2047 return gWrapModes[tm];
2048}
2049
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002050}
2051
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002052void GrGpuGL::bindTexture(int unitIdx, const GrTextureParams& params, GrGLTexture* texture) {
2053 GrAssert(NULL != texture);
tomhudson@google.comd0c1a062012-07-12 17:23:52 +00002054
bsalomon@google.comb8670992012-07-25 21:27:09 +00002055 // If we created a rt/tex and rendered to it without using a texture and now we're texturing
2056 // 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 +00002057 // out of the "last != next" check.
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002058 GrGLRenderTarget* texRT = static_cast<GrGLRenderTarget*>(texture->asRenderTarget());
bsalomon@google.com4c883782012-06-04 19:05:11 +00002059 if (NULL != texRT) {
2060 this->onResolveRenderTarget(texRT);
2061 }
2062
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002063 if (fHWBoundTextures[unitIdx] != texture) {
2064 this->setTextureUnit(unitIdx);
2065 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, texture->textureID()));
2066 fHWBoundTextures[unitIdx] = texture;
bsalomon@google.com4c883782012-06-04 19:05:11 +00002067 }
2068
bsalomon@google.com4c883782012-06-04 19:05:11 +00002069 ResetTimestamp timestamp;
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002070 const GrGLTexture::TexParams& oldTexParams = texture->getCachedTexParams(&timestamp);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002071 bool setAll = timestamp < this->getResetTimestamp();
2072 GrGLTexture::TexParams newTexParams;
2073
bsalomon@google.comb8670992012-07-25 21:27:09 +00002074 newTexParams.fFilter = params.isBilerp() ? GR_GL_LINEAR : GR_GL_NEAREST;
bsalomon@google.com4c883782012-06-04 19:05:11 +00002075
bsalomon@google.comb8670992012-07-25 21:27:09 +00002076 newTexParams.fWrapS = tile_to_gl_wrap(params.getTileModeX());
2077 newTexParams.fWrapT = tile_to_gl_wrap(params.getTileModeY());
bsalomon@google.com4c883782012-06-04 19:05:11 +00002078 memcpy(newTexParams.fSwizzleRGBA,
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002079 GrGLShaderBuilder::GetTexParamSwizzle(texture->config(), this->glCaps()),
bsalomon@google.com4c883782012-06-04 19:05:11 +00002080 sizeof(newTexParams.fSwizzleRGBA));
2081 if (setAll || newTexParams.fFilter != oldTexParams.fFilter) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002082 this->setTextureUnit(unitIdx);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002083 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2084 GR_GL_TEXTURE_MAG_FILTER,
2085 newTexParams.fFilter));
2086 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2087 GR_GL_TEXTURE_MIN_FILTER,
2088 newTexParams.fFilter));
2089 }
2090 if (setAll || newTexParams.fWrapS != oldTexParams.fWrapS) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002091 this->setTextureUnit(unitIdx);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002092 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2093 GR_GL_TEXTURE_WRAP_S,
2094 newTexParams.fWrapS));
2095 }
2096 if (setAll || newTexParams.fWrapT != oldTexParams.fWrapT) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002097 this->setTextureUnit(unitIdx);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002098 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2099 GR_GL_TEXTURE_WRAP_T,
2100 newTexParams.fWrapT));
2101 }
2102 if (this->glCaps().textureSwizzleSupport() &&
2103 (setAll || memcmp(newTexParams.fSwizzleRGBA,
2104 oldTexParams.fSwizzleRGBA,
2105 sizeof(newTexParams.fSwizzleRGBA)))) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002106 this->setTextureUnit(unitIdx);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002107 set_tex_swizzle(newTexParams.fSwizzleRGBA,
2108 this->glInterface());
2109 }
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002110 texture->setCachedTexParams(newTexParams, this->getResetTimestamp());
bsalomon@google.com4c883782012-06-04 19:05:11 +00002111}
2112
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002113void GrGpuGL::flushMiscFixedFunctionState() {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002114
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002115 const GrDrawState& drawState = this->getDrawState();
reed@google.comac10a2d2010-12-22 21:39:39 +00002116
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002117 if (drawState.isDitherState()) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002118 if (kYes_TriState != fHWDitherEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002119 GL_CALL(Enable(GR_GL_DITHER));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002120 fHWDitherEnabled = kYes_TriState;
2121 }
2122 } else {
2123 if (kNo_TriState != fHWDitherEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002124 GL_CALL(Disable(GR_GL_DITHER));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002125 fHWDitherEnabled = kNo_TriState;
reed@google.comac10a2d2010-12-22 21:39:39 +00002126 }
2127 }
2128
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002129 if (drawState.isColorWriteDisabled()) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002130 if (kNo_TriState != fHWWriteToColor) {
2131 GL_CALL(ColorMask(GR_GL_FALSE, GR_GL_FALSE,
2132 GR_GL_FALSE, GR_GL_FALSE));
2133 fHWWriteToColor = kNo_TriState;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002134 }
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002135 } else {
2136 if (kYes_TriState != fHWWriteToColor) {
2137 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
2138 fHWWriteToColor = kYes_TriState;
2139 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00002140 }
2141
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002142 if (fHWDrawFace != drawState.getDrawFace()) {
bsalomon@google.coma5d056a2012-03-27 15:59:58 +00002143 switch (this->getDrawState().getDrawFace()) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002144 case GrDrawState::kCCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002145 GL_CALL(Enable(GR_GL_CULL_FACE));
2146 GL_CALL(CullFace(GR_GL_BACK));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002147 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002148 case GrDrawState::kCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002149 GL_CALL(Enable(GR_GL_CULL_FACE));
2150 GL_CALL(CullFace(GR_GL_FRONT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002151 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002152 case GrDrawState::kBoth_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002153 GL_CALL(Disable(GR_GL_CULL_FACE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002154 break;
2155 default:
2156 GrCrash("Unknown draw face.");
2157 }
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002158 fHWDrawFace = drawState.getDrawFace();
bsalomon@google.comd302f142011-03-03 13:54:13 +00002159 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002160}
2161
bsalomon@google.come49ad452013-02-20 19:33:20 +00002162void GrGpuGL::notifyVertexBufferBind(GrGLuint id) {
2163 fHWGeometryState.setVertexBufferID(id);
reed@google.comac10a2d2010-12-22 21:39:39 +00002164}
2165
bsalomon@google.come49ad452013-02-20 19:33:20 +00002166void GrGpuGL::notifyVertexBufferDelete(GrGLuint id) {
2167 fHWGeometryState.notifyVertexBufferDelete(id);
reed@google.comac10a2d2010-12-22 21:39:39 +00002168}
2169
bsalomon@google.come49ad452013-02-20 19:33:20 +00002170void GrGpuGL::notifyIndexBufferBind(GrGLuint id) {
2171 fHWGeometryState.setIndexBufferID(id);
reed@google.comac10a2d2010-12-22 21:39:39 +00002172}
2173
bsalomon@google.come49ad452013-02-20 19:33:20 +00002174void GrGpuGL::notifyIndexBufferDelete(GrGLuint id) {
2175 fHWGeometryState.notifyIndexBufferDelete(id);
reed@google.comac10a2d2010-12-22 21:39:39 +00002176}
2177
reed@google.comac10a2d2010-12-22 21:39:39 +00002178void GrGpuGL::notifyRenderTargetDelete(GrRenderTarget* renderTarget) {
2179 GrAssert(NULL != renderTarget);
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002180 if (fHWBoundRenderTarget == renderTarget) {
2181 fHWBoundRenderTarget = NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +00002182 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002183}
2184
2185void GrGpuGL::notifyTextureDelete(GrGLTexture* texture) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002186 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002187 if (fHWBoundTextures[s] == texture) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002188 // deleting bound texture does implied bind to 0
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002189 fHWBoundTextures[s] = NULL;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002190 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002191 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002192}
2193
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002194bool GrGpuGL::configToGLFormats(GrPixelConfig config,
2195 bool getSizedInternalFormat,
2196 GrGLenum* internalFormat,
2197 GrGLenum* externalFormat,
2198 GrGLenum* externalType) {
2199 GrGLenum dontCare;
2200 if (NULL == internalFormat) {
2201 internalFormat = &dontCare;
2202 }
2203 if (NULL == externalFormat) {
2204 externalFormat = &dontCare;
2205 }
2206 if (NULL == externalType) {
2207 externalType = &dontCare;
2208 }
2209
reed@google.comac10a2d2010-12-22 21:39:39 +00002210 switch (config) {
bsalomon@google.com0342a852012-08-20 19:22:38 +00002211 case kRGBA_8888_GrPixelConfig:
bsalomon@google.comc4364992011-11-07 15:54:49 +00002212 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002213 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002214 if (getSizedInternalFormat) {
2215 *internalFormat = GR_GL_RGBA8;
2216 } else {
2217 *internalFormat = GR_GL_RGBA;
2218 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002219 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.comc4364992011-11-07 15:54:49 +00002220 break;
bsalomon@google.com0342a852012-08-20 19:22:38 +00002221 case kBGRA_8888_GrPixelConfig:
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002222 if (!this->glCaps().bgraFormatSupport()) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002223 return false;
2224 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002225 if (this->glCaps().bgraIsInternalFormat()) {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002226 if (getSizedInternalFormat) {
2227 *internalFormat = GR_GL_BGRA8;
2228 } else {
2229 *internalFormat = GR_GL_BGRA;
2230 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002231 } else {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002232 if (getSizedInternalFormat) {
2233 *internalFormat = GR_GL_RGBA8;
2234 } else {
2235 *internalFormat = GR_GL_RGBA;
2236 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002237 }
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002238 *externalFormat = GR_GL_BGRA;
bsalomon@google.com6f379512011-11-16 20:36:03 +00002239 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002240 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002241 case kRGB_565_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002242 *internalFormat = GR_GL_RGB;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002243 *externalFormat = GR_GL_RGB;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002244 if (getSizedInternalFormat) {
2245 if (this->glBinding() == kDesktop_GrGLBinding) {
2246 return false;
2247 } else {
2248 *internalFormat = GR_GL_RGB565;
2249 }
2250 } else {
2251 *internalFormat = GR_GL_RGB;
2252 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002253 *externalType = GR_GL_UNSIGNED_SHORT_5_6_5;
reed@google.comac10a2d2010-12-22 21:39:39 +00002254 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002255 case kRGBA_4444_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002256 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002257 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002258 if (getSizedInternalFormat) {
2259 *internalFormat = GR_GL_RGBA4;
2260 } else {
2261 *internalFormat = GR_GL_RGBA;
2262 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002263 *externalType = GR_GL_UNSIGNED_SHORT_4_4_4_4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002264 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002265 case kIndex_8_GrPixelConfig:
bsalomon@google.comf6601872012-08-28 21:11:35 +00002266 if (this->getCaps().eightBitPaletteSupport()) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002267 *internalFormat = GR_GL_PALETTE8_RGBA8;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002268 // glCompressedTexImage doesn't take external params
2269 *externalFormat = GR_GL_PALETTE8_RGBA8;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002270 // no sized/unsized internal format distinction here
2271 *internalFormat = GR_GL_PALETTE8_RGBA8;
2272 // unused with CompressedTexImage
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002273 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002274 } else {
2275 return false;
2276 }
2277 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002278 case kAlpha_8_GrPixelConfig:
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002279 if (this->glCaps().textureRedSupport()) {
2280 *internalFormat = GR_GL_RED;
2281 *externalFormat = GR_GL_RED;
2282 if (getSizedInternalFormat) {
2283 *internalFormat = GR_GL_R8;
2284 } else {
2285 *internalFormat = GR_GL_RED;
2286 }
2287 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002288 } else {
2289 *internalFormat = GR_GL_ALPHA;
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002290 *externalFormat = GR_GL_ALPHA;
2291 if (getSizedInternalFormat) {
2292 *internalFormat = GR_GL_ALPHA8;
2293 } else {
2294 *internalFormat = GR_GL_ALPHA;
2295 }
2296 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002297 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002298 break;
2299 default:
2300 return false;
2301 }
2302 return true;
2303}
2304
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002305void GrGpuGL::setTextureUnit(int unit) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002306 GrAssert(unit >= 0 && unit < GrDrawState::kNumStages);
bsalomon@google.com49209392012-06-05 15:13:46 +00002307 if (fHWActiveTextureUnitIdx != unit) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002308 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com49209392012-06-05 15:13:46 +00002309 fHWActiveTextureUnitIdx = unit;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002310 }
2311}
bsalomon@google.com316f99232011-01-13 21:28:12 +00002312
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002313void GrGpuGL::setSpareTextureUnit() {
bsalomon@google.com49209392012-06-05 15:13:46 +00002314 if (fHWActiveTextureUnitIdx != (GR_GL_TEXTURE0 + SPARE_TEX_UNIT)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002315 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + SPARE_TEX_UNIT));
bsalomon@google.com49209392012-06-05 15:13:46 +00002316 fHWActiveTextureUnitIdx = SPARE_TEX_UNIT;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002317 }
2318}
2319
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00002320GrGLVertexBuffer* GrGpuGL::setBuffers(bool indexed,
2321 size_t* vertexOffsetInBytes,
2322 size_t* indexOffsetInBytes) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002323
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00002324 GrAssert(NULL != vertexOffsetInBytes);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002325
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002326 const GeometryPoolState& geoPoolState = this->getGeomPoolState();
2327
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002328 GrGLVertexBuffer* vbuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002329 switch (this->getGeomSrc().fVertexSrc) {
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00002330 case kBuffer_GeometrySrcType:
2331 *vertexOffsetInBytes = 0;
2332 vbuf = (GrGLVertexBuffer*) this->getGeomSrc().fVertexBuffer;
2333 break;
2334 case kArray_GeometrySrcType:
2335 case kReserved_GeometrySrcType:
2336 this->finalizeReservedVertices();
2337 *vertexOffsetInBytes = geoPoolState.fPoolStartVertex * this->getGeomSrc().fVertexSize;
2338 vbuf = (GrGLVertexBuffer*) geoPoolState.fPoolVertexBuffer;
2339 break;
2340 default:
2341 vbuf = NULL; // suppress warning
2342 GrCrash("Unknown geometry src type!");
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002343 }
2344
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002345 GrAssert(NULL != vbuf);
2346 GrAssert(!vbuf->isLocked());
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00002347 *vertexOffsetInBytes += vbuf->baseOffset();
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002348
2349 if (indexed) {
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00002350 GrAssert(NULL != indexOffsetInBytes);
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002351
2352 GrGLIndexBuffer* ibuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002353 switch (this->getGeomSrc().fIndexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002354 case kBuffer_GeometrySrcType:
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00002355 *indexOffsetInBytes = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002356 ibuf = (GrGLIndexBuffer*)this->getGeomSrc().fIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002357 break;
2358 case kArray_GeometrySrcType:
2359 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002360 this->finalizeReservedIndices();
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00002361 *indexOffsetInBytes = geoPoolState.fPoolStartIndex * sizeof(GrGLushort);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002362 ibuf = (GrGLIndexBuffer*) geoPoolState.fPoolIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002363 break;
2364 default:
2365 ibuf = NULL; // suppress warning
2366 GrCrash("Unknown geometry src type!");
2367 }
2368
2369 GrAssert(NULL != ibuf);
2370 GrAssert(!ibuf->isLocked());
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00002371 *indexOffsetInBytes += ibuf->baseOffset();
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00002372 if (!fHWGeometryState.isIndexBufferIDBound(ibuf->bufferID())) {
2373 ibuf->bind();
2374 fHWGeometryState.setIndexBufferID(ibuf->bufferID());
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002375 }
2376 }
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00002377 return vbuf;
2378}
2379
2380///////////////////////////////////////////////////////////////////////////////
2381
2382void GrGpuGL::HWGeometryState::AttribArray::set(const GrGpuGL* gpu,
2383 HWGeometryState* geoState,
2384 int index,
2385 GrGLVertexBuffer* buffer,
2386 GrGLint size,
2387 GrGLenum type,
2388 GrGLboolean normalized,
2389 GrGLsizei stride,
2390 GrGLvoid* offset) {
2391 if (!fEnableIsValid || !fEnabled) {
2392 GR_GL_CALL(gpu->glInterface(), EnableVertexAttribArray(index));
2393 fEnableIsValid = true;
2394 fEnabled = true;
2395 }
2396 if (!fAttribPointerIsValid ||
2397 fVertexBufferID != buffer->bufferID() ||
2398 fSize != size ||
2399 fNormalized != normalized ||
2400 fStride != stride ||
2401 offset != fOffset) {
2402
2403 GrGLuint bufferID = buffer->bufferID();
2404 if (!geoState->isVertexBufferIDBound(bufferID)) {
2405 buffer->bind();
2406 geoState->setVertexBufferID(bufferID);
2407 }
2408 GR_GL_CALL(gpu->glInterface(), VertexAttribPointer(index,
2409 size,
2410 type,
2411 normalized,
2412 stride,
2413 offset));
2414 fAttribPointerIsValid = true;
2415 fVertexBufferID = bufferID;
2416 fSize = size;
2417 fNormalized = normalized;
2418 fStride = stride;
2419 fOffset = offset;
2420 }
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002421}