blob: 7f5f999e10a77a8c37703258b1591fec7f7c37d6 [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
senorblanco@chromium.orged8659b2013-02-04 17:29:51 +0000150static GrSurfaceOrigin resolve_origin(GrSurfaceOrigin origin, bool renderTarget) {
151 // By default, GrRenderTargets are GL's normal orientation so that they
152 // can be drawn to by the outside world without the client having
153 // to render upside down.
154 if (kDefault_GrSurfaceOrigin == origin) {
155 return renderTarget ? kBottomLeft_GrSurfaceOrigin : kTopLeft_GrSurfaceOrigin;
156 } else {
157 return origin;
158 }
159}
160
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000161GrGpuGL::GrGpuGL(const GrGLContextInfo& ctxInfo) : fGLContextInfo(ctxInfo) {
162
163 GrAssert(ctxInfo.isInitialized());
tomhudson@google.com747bf292011-06-14 18:16:52 +0000164
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000165 fillInConfigRenderableTable();
166
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000167 fPrintedCaps = false;
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000168
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000169 GrGLClearErr(fGLContextInfo.interface());
reed@google.comac10a2d2010-12-22 21:39:39 +0000170
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000171 if (gPrintStartupSpew) {
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000172 const GrGLubyte* ext;
173 GL_CALL_RET(ext, GetString(GR_GL_EXTENSIONS));
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000174 const GrGLubyte* vendor;
175 const GrGLubyte* renderer;
176 const GrGLubyte* version;
177 GL_CALL_RET(vendor, GetString(GR_GL_VENDOR));
178 GL_CALL_RET(renderer, GetString(GR_GL_RENDERER));
179 GL_CALL_RET(version, GetString(GR_GL_VERSION));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000180 GrPrintf("------------------------- create GrGpuGL %p --------------\n",
181 this);
182 GrPrintf("------ VENDOR %s\n", vendor);
183 GrPrintf("------ RENDERER %s\n", renderer);
184 GrPrintf("------ VERSION %s\n", version);
185 GrPrintf("------ EXTENSIONS\n %s \n", ext);
186 }
187
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000188 this->initCaps();
tomhudson@google.com747bf292011-06-14 18:16:52 +0000189
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000190 fProgramCache = SkNEW_ARGS(ProgramCache, (this->glContextInfo()));
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000191
bsalomon@google.comfe676522011-06-17 18:12:21 +0000192 fLastSuccessfulStencilFmtIdx = 0;
caryclark@google.comcf6285b2012-06-06 12:09:01 +0000193 if (false) { // avoid bit rot, suppress warning
194 fbo_test(this->glInterface(), 0, 0);
195 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000196}
197
198GrGpuGL::~GrGpuGL() {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000199 if (0 != fHWProgramID) {
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000200 // detach the current program so there is no confusion on OpenGL's part
201 // that we want it to be deleted
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000202 GrAssert(fHWProgramID == fCurrentProgram->fProgramID);
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000203 GL_CALL(UseProgram(0));
204 }
205
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000206 delete fProgramCache;
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000207
bsalomon@google.com4a018bb2011-10-28 19:50:21 +0000208 // This must be called by before the GrDrawTarget destructor
209 this->releaseGeometry();
bsalomon@google.com15b11df2011-09-16 21:18:29 +0000210 // This subclass must do this before the base class destructor runs
211 // since we will unref the GrGLInterface.
212 this->releaseResources();
reed@google.comac10a2d2010-12-22 21:39:39 +0000213}
214
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000215///////////////////////////////////////////////////////////////////////////////
216
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000217void GrGpuGL::initCaps() {
218 GrGLint maxTextureUnits;
219 // check FS and fixed-function texture unit limits
220 // we only use textures in the fragment stage currently.
221 // checks are > to make sure we have a spare unit.
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000222 const GrGLInterface* gl = this->glInterface();
223 GR_GL_GetIntegerv(gl, GR_GL_MAX_TEXTURE_IMAGE_UNITS, &maxTextureUnits);
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000224 GrAssert(maxTextureUnits > GrDrawState::kNumStages);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000225
bsalomon@google.comf6601872012-08-28 21:11:35 +0000226 CapsInternals* caps = this->capsInternals();
227
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000228 GrGLint numFormats;
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000229 GR_GL_GetIntegerv(gl, GR_GL_NUM_COMPRESSED_TEXTURE_FORMATS, &numFormats);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000230 SkAutoSTMalloc<10, GrGLint> formats(numFormats);
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000231 GR_GL_GetIntegerv(gl, GR_GL_COMPRESSED_TEXTURE_FORMATS, formats);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000232 for (int i = 0; i < numFormats; ++i) {
233 if (formats[i] == GR_GL_PALETTE8_RGBA8) {
bsalomon@google.comf6601872012-08-28 21:11:35 +0000234 caps->f8BitPaletteSupport = true;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000235 break;
236 }
237 }
238
239 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000240 // we could also look for GL_ATI_separate_stencil extension or
241 // GL_EXT_stencil_two_side but they use different function signatures
242 // than GL2.0+ (and than each other).
bsalomon@google.comf6601872012-08-28 21:11:35 +0000243 caps->fTwoSidedStencilSupport = (this->glVersion() >= GR_GL_VER(2,0));
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000244 // supported on GL 1.4 and higher or by extension
bsalomon@google.comf6601872012-08-28 21:11:35 +0000245 caps->fStencilWrapOpsSupport = (this->glVersion() >= GR_GL_VER(1,4)) ||
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000246 this->hasExtension("GL_EXT_stencil_wrap");
247 } else {
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000248 // ES 2 has two sided stencil and stencil wrap
bsalomon@google.comf6601872012-08-28 21:11:35 +0000249 caps->fTwoSidedStencilSupport = true;
250 caps->fStencilWrapOpsSupport = true;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000251 }
252
253 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.comf6601872012-08-28 21:11:35 +0000254 caps->fBufferLockSupport = true; // we require VBO support and the desktop VBO
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000255 // extension includes glMapBuffer.
256 } else {
bsalomon@google.comf6601872012-08-28 21:11:35 +0000257 caps->fBufferLockSupport = this->hasExtension("GL_OES_mapbuffer");
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000258 }
259
260 if (kDesktop_GrGLBinding == this->glBinding()) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000261 if (this->glVersion() >= GR_GL_VER(2,0) ||
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000262 this->hasExtension("GL_ARB_texture_non_power_of_two")) {
bsalomon@google.comf6601872012-08-28 21:11:35 +0000263 caps->fNPOTTextureTileSupport = true;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000264 } else {
bsalomon@google.comf6601872012-08-28 21:11:35 +0000265 caps->fNPOTTextureTileSupport = false;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000266 }
267 } else {
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000268 // Unextended ES2 supports NPOT textures with clamp_to_edge and non-mip filters only
bsalomon@google.comf6601872012-08-28 21:11:35 +0000269 caps->fNPOTTextureTileSupport = this->hasExtension("GL_OES_texture_npot");
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000270 }
271
bsalomon@google.comf6601872012-08-28 21:11:35 +0000272 caps->fHWAALineSupport = (kDesktop_GrGLBinding == this->glBinding());
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000273
bsalomon@google.comf6601872012-08-28 21:11:35 +0000274 GR_GL_GetIntegerv(gl, GR_GL_MAX_TEXTURE_SIZE, &caps->fMaxTextureSize);
275 GR_GL_GetIntegerv(gl, GR_GL_MAX_RENDERBUFFER_SIZE, &caps->fMaxRenderTargetSize);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000276 // Our render targets are always created with textures as the color
277 // attachment, hence this min:
bsalomon@google.comf6601872012-08-28 21:11:35 +0000278 caps->fMaxRenderTargetSize = GrMin(caps->fMaxTextureSize, caps->fMaxRenderTargetSize);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000279
bsalomon@google.comf6601872012-08-28 21:11:35 +0000280 caps->fFSAASupport = GrGLCaps::kNone_MSFBOType != this->glCaps().msFBOType();
281 caps->fPathStencilingSupport = GR_GL_USE_NV_PATH_RENDERING &&
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000282 this->hasExtension("GL_NV_path_rendering");
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000283
284 // Enable supported shader-related caps
285 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.comf6601872012-08-28 21:11:35 +0000286 caps->fDualSourceBlendingSupport =
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000287 this->glVersion() >= GR_GL_VER(3,3) ||
288 this->hasExtension("GL_ARB_blend_func_extended");
bsalomon@google.comf6601872012-08-28 21:11:35 +0000289 caps->fShaderDerivativeSupport = true;
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000290 // we don't support GL_ARB_geometry_shader4, just GL 3.2+ GS
bsalomon@google.comf6601872012-08-28 21:11:35 +0000291 caps->fGeometryShaderSupport =
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000292 this->glVersion() >= GR_GL_VER(3,2) &&
293 this->glslGeneration() >= k150_GrGLSLGeneration;
294 } else {
bsalomon@google.comf6601872012-08-28 21:11:35 +0000295 caps->fShaderDerivativeSupport =
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000296 this->hasExtension("GL_OES_standard_derivatives");
297 }
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000298}
299
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000300void GrGpuGL::fillInConfigRenderableTable() {
301
302 // OpenGL < 3.0
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000303 // no support for render targets unless the GL_ARB_framebuffer_object
304 // extension is supported (in which case we get ALPHA, RED, RG, RGB,
305 // RGBA (ALPHA8, RGBA4, RGBA8) for OpenGL > 1.1). Note that we
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000306 // probably don't get R8 in this case.
307
308 // OpenGL 3.0
309 // base color renderable: ALPHA, RED, RG, RGB, and RGBA
310 // sized derivatives: ALPHA8, R8, RGBA4, RGBA8
311
312 // >= OpenGL 3.1
313 // base color renderable: RED, RG, RGB, and RGBA
314 // sized derivatives: R8, RGBA4, RGBA8
315 // if the GL_ARB_compatibility extension is supported then we get back
316 // support for GL_ALPHA and ALPHA8
317
318 // GL_EXT_bgra adds BGRA render targets to any version
319
320 // ES 2.0
321 // color renderable: RGBA4, RGB5_A1, RGB565
322 // GL_EXT_texture_rg adds support for R8 as a color render target
323 // GL_OES_rgb8_rgba8 and/or GL_ARM_rgba8 adds support for RGBA8
324 // GL_EXT_texture_format_BGRA8888 and/or GL_APPLE_texture_format_BGRA8888
325 // added BGRA support
326
327 if (kDesktop_GrGLBinding == this->glBinding()) {
328 // Post 3.0 we will get R8
329 // Prior to 3.0 we will get ALPHA8 (with GL_ARB_framebuffer_object)
330 if (this->glVersion() >= GR_GL_VER(3,0) ||
331 this->hasExtension("GL_ARB_framebuffer_object")) {
332 fConfigRenderSupport[kAlpha_8_GrPixelConfig] = true;
333 }
334 } else {
335 // On ES we can only hope for R8
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000336 fConfigRenderSupport[kAlpha_8_GrPixelConfig] =
robertphillips@google.com443e5a52012-04-30 20:01:21 +0000337 this->glCaps().textureRedSupport();
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000338 }
339
340 if (kDesktop_GrGLBinding != this->glBinding()) {
341 // only available in ES
342 fConfigRenderSupport[kRGB_565_GrPixelConfig] = true;
343 }
344
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000345 // Pre 3.0, Ganesh relies on either GL_ARB_framebuffer_object or
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000346 // GL_EXT_framebuffer_object for FBO support. Both of these
347 // allow RGBA4 render targets so this is always supported.
348 fConfigRenderSupport[kRGBA_4444_GrPixelConfig] = true;
349
350 if (this->glCaps().rgba8RenderbufferSupport()) {
bsalomon@google.com0342a852012-08-20 19:22:38 +0000351 fConfigRenderSupport[kRGBA_8888_GrPixelConfig] = true;
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000352 }
353
354 if (this->glCaps().bgraFormatSupport()) {
bsalomon@google.com0342a852012-08-20 19:22:38 +0000355 fConfigRenderSupport[kBGRA_8888_GrPixelConfig] = true;
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000356 }
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000357}
358
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000359GrPixelConfig GrGpuGL::preferredReadPixelsConfig(GrPixelConfig config) const {
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000360 if (GR_GL_RGBA_8888_PIXEL_OPS_SLOW && GrPixelConfigIsRGBA8888(config)) {
361 return GrPixelConfigSwapRAndB(config);
362 } else {
363 return config;
364 }
365}
366
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000367GrPixelConfig GrGpuGL::preferredWritePixelsConfig(GrPixelConfig config) const {
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000368 if (GR_GL_RGBA_8888_PIXEL_OPS_SLOW && GrPixelConfigIsRGBA8888(config)) {
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000369 return GrPixelConfigSwapRAndB(config);
370 } else {
371 return config;
372 }
373}
374
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000375bool GrGpuGL::fullReadPixelsIsFasterThanPartial() const {
376 return SkToBool(GR_GL_FULL_READPIXELS_FASTER_THAN_PARTIAL);
377}
378
bsalomon@google.com1bf1c212011-11-05 12:18:58 +0000379void GrGpuGL::onResetContext() {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000380 if (gPrintStartupSpew && !fPrintedCaps) {
381 fPrintedCaps = true;
382 this->getCaps().print();
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000383 this->glCaps().print();
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000384 }
385
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000386 // we don't use the zb at all
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000387 GL_CALL(Disable(GR_GL_DEPTH_TEST));
388 GL_CALL(DepthMask(GR_GL_FALSE));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000389
bsalomon@google.com978c8c62012-05-21 14:45:49 +0000390 fHWDrawFace = GrDrawState::kInvalid_DrawFace;
391 fHWDitherEnabled = kUnknown_TriState;
reed@google.comac10a2d2010-12-22 21:39:39 +0000392
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000393 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com469d0dd2012-05-21 20:14:29 +0000394 // Desktop-only state that we never change
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000395 GL_CALL(Disable(GR_GL_POINT_SMOOTH));
bsalomon@google.com469d0dd2012-05-21 20:14:29 +0000396 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
397 GL_CALL(Disable(GR_GL_POLYGON_SMOOTH));
398 GL_CALL(Disable(GR_GL_POLYGON_STIPPLE));
399 GL_CALL(Disable(GR_GL_COLOR_LOGIC_OP));
bsalomon@google.come76b7cc2012-06-18 12:47:06 +0000400 if (this->glCaps().imagingSupport()) {
401 GL_CALL(Disable(GR_GL_COLOR_TABLE));
402 }
bsalomon@google.com469d0dd2012-05-21 20:14:29 +0000403 GL_CALL(Disable(GR_GL_INDEX_LOGIC_OP));
404 GL_CALL(Disable(GR_GL_POLYGON_OFFSET_FILL));
405 // Since ES doesn't support glPointSize at all we always use the VS to
406 // set the point size
407 GL_CALL(Enable(GR_GL_VERTEX_PROGRAM_POINT_SIZE));
408
409 // We should set glPolygonMode(FRONT_AND_BACK,FILL) here, too. It isn't
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000410 // currently part of our gl interface. There are probably others as
bsalomon@google.com469d0dd2012-05-21 20:14:29 +0000411 // well.
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000412 }
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +0000413 fHWAAState.invalidate();
bsalomon@google.com978c8c62012-05-21 14:45:49 +0000414 fHWWriteToColor = kUnknown_TriState;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000415
reed@google.comac10a2d2010-12-22 21:39:39 +0000416 // we only ever use lines in hairline mode
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000417 GL_CALL(LineWidth(1));
reed@google.comac10a2d2010-12-22 21:39:39 +0000418
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000419 // invalid
bsalomon@google.com49209392012-06-05 15:13:46 +0000420 fHWActiveTextureUnitIdx = -1;
reed@google.comac10a2d2010-12-22 21:39:39 +0000421
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +0000422 fHWBlendState.invalidate();
bsalomon@google.com080773c2011-03-15 19:09:25 +0000423
tomhudson@google.com93813632011-10-27 20:21:16 +0000424 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.comc811ea32012-05-21 15:33:09 +0000425 fHWBoundTextures[s] = NULL;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000426 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000427
bsalomon@google.coma3201942012-06-21 19:58:20 +0000428 fHWScissorSettings.invalidate();
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000429
bsalomon@google.coma3201942012-06-21 19:58:20 +0000430 fHWViewport.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000431
bsalomon@google.com457b8a32012-05-21 21:19:58 +0000432 fHWStencilSettings.invalidate();
bsalomon@google.coma3201942012-06-21 19:58:20 +0000433 fHWStencilTestEnabled = kUnknown_TriState;
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000434
reed@google.comac10a2d2010-12-22 21:39:39 +0000435 fHWGeometryState.fIndexBuffer = NULL;
436 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.coma3201942012-06-21 19:58:20 +0000437
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +0000438 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000439
bsalomon@google.comc811ea32012-05-21 15:33:09 +0000440 fHWBoundRenderTarget = NULL;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000441
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000442 fHWPathMatrixState.invalidate();
bsalomon@google.comf6601872012-08-28 21:11:35 +0000443 if (fCaps.pathStencilingSupport()) {
bsalomon@google.com05a718c2012-06-29 14:01:53 +0000444 // we don't use the model view matrix.
445 GL_CALL(MatrixMode(GR_GL_MODELVIEW));
446 GL_CALL(LoadIdentity());
447 }
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000448
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000449 // we assume these values
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000450 if (this->glCaps().unpackRowLengthSupport()) {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000451 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
452 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000453 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000454 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
455 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000456 if (this->glCaps().unpackFlipYSupport()) {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000457 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
458 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000459 if (this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000460 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, GR_GL_FALSE));
461 }
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000462
caryclark@google.comcf6285b2012-06-06 12:09:01 +0000463 fHWGeometryState.fVertexOffset = ~0U;
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000464
465 // Third party GL code may have left vertex attributes enabled. Some GL
466 // implementations (osmesa) may read vetex attributes that are not required
467 // by the current shader. Therefore, we have to ensure that only the
468 // attributes we require for the current draw are enabled or we may cause an
469 // invalid read.
470
471 // Disable all vertex layout bits so that next flush will assume all
472 // optional vertex attributes are disabled.
473 fHWGeometryState.fVertexLayout = 0;
474
475 // We always use the this attribute and assume it is always enabled.
476 int posAttrIdx = GrGLProgram::PositionAttributeIdx();
477 GL_CALL(EnableVertexAttribArray(posAttrIdx));
478 // Disable all other vertex attributes.
bsalomon@google.com60da4172012-06-01 19:25:00 +0000479 for (int va = 0; va < this->glCaps().maxVertexAttributes(); ++va) {
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000480 if (va != posAttrIdx) {
481 GL_CALL(DisableVertexAttribArray(va));
482 }
483 }
484
485 fHWProgramID = 0;
486 fHWConstAttribColor = GrColor_ILLEGAL;
487 fHWConstAttribCoverage = GrColor_ILLEGAL;
reed@google.comac10a2d2010-12-22 21:39:39 +0000488}
489
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000490GrTexture* GrGpuGL::onWrapBackendTexture(const GrBackendTextureDesc& desc) {
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000491 if (!this->configToGLFormats(desc.fConfig, false, NULL, NULL, NULL)) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000492 return NULL;
493 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000494
robertphillips@google.comb72e5d32012-10-30 15:18:10 +0000495 if (0 == desc.fTextureHandle) {
496 return NULL;
497 }
498
499 int maxSize = this->getCaps().maxTextureSize();
500 if (desc.fWidth > maxSize || desc.fHeight > maxSize) {
501 return NULL;
502 }
503
504 GrGLTexture::Desc glTexDesc;
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000505 // next line relies on GrBackendTextureDesc's flags matching GrTexture's
robertphillips@google.com32716282012-06-04 12:48:45 +0000506 glTexDesc.fFlags = (GrTextureFlags) desc.fFlags;
bsalomon@google.com99621082011-11-15 16:47:16 +0000507 glTexDesc.fWidth = desc.fWidth;
508 glTexDesc.fHeight = desc.fHeight;
bsalomon@google.come269f212011-11-07 13:29:52 +0000509 glTexDesc.fConfig = desc.fConfig;
robertphillips@google.com32716282012-06-04 12:48:45 +0000510 glTexDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.come269f212011-11-07 13:29:52 +0000511 glTexDesc.fTextureID = static_cast<GrGLuint>(desc.fTextureHandle);
bsalomon@google.com72830222013-01-23 20:25:22 +0000512 glTexDesc.fIsWrapped = true;
senorblanco@chromium.orged8659b2013-02-04 17:29:51 +0000513 bool renderTarget = 0 != (desc.fFlags & kRenderTarget_GrBackendTextureFlag);
514 glTexDesc.fOrigin = resolve_origin(desc.fOrigin, renderTarget);
bsalomon@google.come269f212011-11-07 13:29:52 +0000515
516 GrGLTexture* texture = NULL;
senorblanco@chromium.orged8659b2013-02-04 17:29:51 +0000517 if (renderTarget) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000518 GrGLRenderTarget::Desc glRTDesc;
519 glRTDesc.fRTFBOID = 0;
520 glRTDesc.fTexFBOID = 0;
521 glRTDesc.fMSColorRenderbufferID = 0;
bsalomon@google.come269f212011-11-07 13:29:52 +0000522 glRTDesc.fConfig = desc.fConfig;
523 glRTDesc.fSampleCnt = desc.fSampleCnt;
senorblanco@chromium.orged8659b2013-02-04 17:29:51 +0000524 glRTDesc.fOrigin = glTexDesc.fOrigin;
bsalomon@google.com99621082011-11-15 16:47:16 +0000525 if (!this->createRenderTargetObjects(glTexDesc.fWidth,
526 glTexDesc.fHeight,
bsalomon@google.come269f212011-11-07 13:29:52 +0000527 glTexDesc.fTextureID,
528 &glRTDesc)) {
529 return NULL;
530 }
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000531 texture = SkNEW_ARGS(GrGLTexture, (this, glTexDesc, glRTDesc));
bsalomon@google.come269f212011-11-07 13:29:52 +0000532 } else {
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000533 texture = SkNEW_ARGS(GrGLTexture, (this, glTexDesc));
bsalomon@google.come269f212011-11-07 13:29:52 +0000534 }
535 if (NULL == texture) {
536 return NULL;
537 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000538
bsalomon@google.come269f212011-11-07 13:29:52 +0000539 this->setSpareTextureUnit();
540 return texture;
541}
542
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000543GrRenderTarget* GrGpuGL::onWrapBackendRenderTarget(const GrBackendRenderTargetDesc& desc) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000544 GrGLRenderTarget::Desc glDesc;
545 glDesc.fConfig = desc.fConfig;
546 glDesc.fRTFBOID = static_cast<GrGLuint>(desc.fRenderTargetHandle);
547 glDesc.fMSColorRenderbufferID = 0;
548 glDesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
549 glDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.com72830222013-01-23 20:25:22 +0000550 glDesc.fIsWrapped = true;
senorblanco@chromium.orged8659b2013-02-04 17:29:51 +0000551 glDesc.fOrigin = desc.fOrigin;
552 if (glDesc.fRTFBOID == 0) {
553 GrAssert(desc.fOrigin == kBottomLeft_GrSurfaceOrigin);
554 }
555
556 glDesc.fOrigin = resolve_origin(desc.fOrigin, true);
bsalomon@google.come269f212011-11-07 13:29:52 +0000557 GrGLIRect viewport;
558 viewport.fLeft = 0;
559 viewport.fBottom = 0;
560 viewport.fWidth = desc.fWidth;
561 viewport.fHeight = desc.fHeight;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000562
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000563 GrRenderTarget* tgt = SkNEW_ARGS(GrGLRenderTarget,
564 (this, glDesc, viewport));
bsalomon@google.come269f212011-11-07 13:29:52 +0000565 if (desc.fStencilBits) {
566 GrGLStencilBuffer::Format format;
567 format.fInternalFormat = GrGLStencilBuffer::kUnknownInternalFormat;
568 format.fPacked = false;
569 format.fStencilBits = desc.fStencilBits;
570 format.fTotalBits = desc.fStencilBits;
bsalomon@google.com1f0f1a32013-01-23 21:32:32 +0000571 static const bool kIsSBWrapped = false;
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000572 GrGLStencilBuffer* sb = SkNEW_ARGS(GrGLStencilBuffer,
573 (this,
bsalomon@google.com1f0f1a32013-01-23 21:32:32 +0000574 kIsSBWrapped,
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000575 0,
576 desc.fWidth,
577 desc.fHeight,
578 desc.fSampleCnt,
579 format));
bsalomon@google.come269f212011-11-07 13:29:52 +0000580 tgt->setStencilBuffer(sb);
581 sb->unref();
582 }
583 return tgt;
584}
585
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000586////////////////////////////////////////////////////////////////////////////////
587
bsalomon@google.com6f379512011-11-16 20:36:03 +0000588void GrGpuGL::onWriteTexturePixels(GrTexture* texture,
589 int left, int top, int width, int height,
590 GrPixelConfig config, const void* buffer,
591 size_t rowBytes) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000592 if (NULL == buffer) {
593 return;
594 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000595 GrGLTexture* glTex = static_cast<GrGLTexture*>(texture);
596
bsalomon@google.com6f379512011-11-16 20:36:03 +0000597 this->setSpareTextureUnit();
598 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTex->textureID()));
599 GrGLTexture::Desc desc;
robertphillips@google.com32716282012-06-04 12:48:45 +0000600 desc.fFlags = glTex->desc().fFlags;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000601 desc.fWidth = glTex->width();
602 desc.fHeight = glTex->height();
robertphillips@google.com32716282012-06-04 12:48:45 +0000603 desc.fConfig = glTex->config();
604 desc.fSampleCnt = glTex->desc().fSampleCnt;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000605 desc.fTextureID = glTex->textureID();
bsalomon@google.com2d0bade2012-10-26 19:01:17 +0000606 desc.fOrigin = glTex->origin();
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000607
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000608 this->uploadTexData(desc, false,
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000609 left, top, width, height,
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000610 config, buffer, rowBytes);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000611}
612
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000613namespace {
614bool adjust_pixel_ops_params(int surfaceWidth,
615 int surfaceHeight,
616 size_t bpp,
617 int* left, int* top, int* width, int* height,
618 const void** data,
619 size_t* rowBytes) {
620 if (!*rowBytes) {
621 *rowBytes = *width * bpp;
622 }
623
624 GrIRect subRect = GrIRect::MakeXYWH(*left, *top, *width, *height);
625 GrIRect bounds = GrIRect::MakeWH(surfaceWidth, surfaceHeight);
626
627 if (!subRect.intersect(bounds)) {
628 return false;
629 }
630 *data = reinterpret_cast<const void*>(reinterpret_cast<intptr_t>(*data) +
631 (subRect.fTop - *top) * *rowBytes + (subRect.fLeft - *left) * bpp);
632
633 *left = subRect.fLeft;
634 *top = subRect.fTop;
635 *width = subRect.width();
636 *height = subRect.height();
637 return true;
638}
639}
640
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000641bool GrGpuGL::uploadTexData(const GrGLTexture::Desc& desc,
642 bool isNewTexture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000643 int left, int top, int width, int height,
644 GrPixelConfig dataConfig,
645 const void* data,
646 size_t rowBytes) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000647 GrAssert(NULL != data || isNewTexture);
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000648
649 size_t bpp = GrBytesPerPixel(dataConfig);
650 if (!adjust_pixel_ops_params(desc.fWidth, desc.fHeight, bpp, &left, &top,
651 &width, &height, &data, &rowBytes)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000652 return false;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000653 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000654 size_t trimRowBytes = width * bpp;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000655
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000656 // in case we need a temporary, trimmed copy of the src pixels
657 SkAutoSMalloc<128 * 128> tempStorage;
658
bsalomon@google.com313f0192012-07-10 17:21:02 +0000659 // paletted textures cannot be partially updated
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000660 bool useTexStorage = isNewTexture &&
bsalomon@google.com313f0192012-07-10 17:21:02 +0000661 desc.fConfig != kIndex_8_GrPixelConfig &&
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000662 this->glCaps().texStorageSupport();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000663
bsalomon@google.com313f0192012-07-10 17:21:02 +0000664 if (useTexStorage && kDesktop_GrGLBinding == this->glBinding()) {
665 // 565 is not a sized internal format on desktop GL. So on desktop with
666 // 565 we always use an unsized internal format to let the system pick
667 // the best sized format to convert the 565 data to. Since TexStorage
668 // only allows sized internal formats we will instead use TexImage2D.
669 useTexStorage = desc.fConfig != kRGB_565_GrPixelConfig;
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000670 }
671
672 GrGLenum internalFormat;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000673 GrGLenum externalFormat;
674 GrGLenum externalType;
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000675 // glTexStorage requires sized internal formats on both desktop and ES. ES
676 // glTexImage requires an unsized format.
677 if (!this->configToGLFormats(dataConfig, useTexStorage, &internalFormat,
678 &externalFormat, &externalType)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000679 return false;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000680 }
681
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000682 if (!isNewTexture && GR_GL_PALETTE8_RGBA8 == internalFormat) {
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000683 // paletted textures cannot be updated
684 return false;
685 }
686
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000687 /*
bsalomon@google.com6f379512011-11-16 20:36:03 +0000688 * check whether to allocate a temporary buffer for flipping y or
689 * because our srcData has extra bytes past each row. If so, we need
690 * to trim those off here, since GL ES may not let us specify
691 * GL_UNPACK_ROW_LENGTH.
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000692 */
bsalomon@google.com6f379512011-11-16 20:36:03 +0000693 bool restoreGLRowLength = false;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000694 bool swFlipY = false;
695 bool glFlipY = false;
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000696 if (NULL != data) {
senorblanco@chromium.orgef5dbe12013-01-28 16:42:38 +0000697 if (kBottomLeft_GrSurfaceOrigin == desc.fOrigin) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000698 if (this->glCaps().unpackFlipYSupport()) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000699 glFlipY = true;
700 } else {
701 swFlipY = true;
702 }
703 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000704 if (this->glCaps().unpackRowLengthSupport() && !swFlipY) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000705 // can't use this for flipping, only non-neg values allowed. :(
706 if (rowBytes != trimRowBytes) {
707 GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
708 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength));
709 restoreGLRowLength = true;
710 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000711 } else {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000712 if (trimRowBytes != rowBytes || swFlipY) {
713 // copy data into our new storage, skipping the trailing bytes
714 size_t trimSize = height * trimRowBytes;
715 const char* src = (const char*)data;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000716 if (swFlipY) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000717 src += (height - 1) * rowBytes;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000718 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000719 char* dst = (char*)tempStorage.reset(trimSize);
720 for (int y = 0; y < height; y++) {
721 memcpy(dst, src, trimRowBytes);
722 if (swFlipY) {
723 src -= rowBytes;
724 } else {
725 src += rowBytes;
726 }
727 dst += trimRowBytes;
728 }
729 // now point data to our copied version
730 data = tempStorage.get();
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000731 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000732 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000733 if (glFlipY) {
734 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_TRUE));
735 }
736 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, static_cast<GrGLint>(bpp)));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000737 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000738 bool succeeded = true;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000739 if (isNewTexture &&
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000740 0 == left && 0 == top &&
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000741 desc.fWidth == width && desc.fHeight == height) {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000742 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000743 if (useTexStorage) {
744 // We never resize or change formats of textures. We don't use
745 // mipmaps currently.
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000746 GL_ALLOC_CALL(this->glInterface(),
747 TexStorage2D(GR_GL_TEXTURE_2D,
748 1, // levels
749 internalFormat,
750 desc.fWidth, desc.fHeight));
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000751 } else {
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000752 if (GR_GL_PALETTE8_RGBA8 == internalFormat) {
753 GrGLsizei imageSize = desc.fWidth * desc.fHeight +
754 kGrColorTableSize;
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000755 GL_ALLOC_CALL(this->glInterface(),
756 CompressedTexImage2D(GR_GL_TEXTURE_2D,
757 0, // level
758 internalFormat,
759 desc.fWidth, desc.fHeight,
760 0, // border
761 imageSize,
762 data));
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000763 } else {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000764 GL_ALLOC_CALL(this->glInterface(),
765 TexImage2D(GR_GL_TEXTURE_2D,
766 0, // level
767 internalFormat,
768 desc.fWidth, desc.fHeight,
769 0, // border
770 externalFormat, externalType,
771 data));
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000772 }
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000773 }
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000774 GrGLenum error = CHECK_ALLOC_ERROR(this->glInterface());
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000775 if (error != GR_GL_NO_ERROR) {
776 succeeded = false;
777 } else {
778 // if we have data and we used TexStorage to create the texture, we
779 // now upload with TexSubImage.
780 if (NULL != data && useTexStorage) {
781 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D,
782 0, // level
783 left, top,
784 width, height,
785 externalFormat, externalType,
786 data));
787 }
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000788 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000789 } else {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000790 if (swFlipY || glFlipY) {
bsalomon@google.com6f379512011-11-16 20:36:03 +0000791 top = desc.fHeight - (top + height);
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000792 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000793 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D,
794 0, // level
795 left, top,
796 width, height,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000797 externalFormat, externalType, data));
798 }
799
800 if (restoreGLRowLength) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000801 GrAssert(this->glCaps().unpackRowLengthSupport());
bsalomon@google.com6f379512011-11-16 20:36:03 +0000802 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000803 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000804 if (glFlipY) {
805 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
806 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000807 return succeeded;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000808}
809
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000810namespace {
811bool renderbuffer_storage_msaa(GrGLContextInfo& ctxInfo,
812 int sampleCount,
813 GrGLenum format,
814 int width, int height) {
815 CLEAR_ERROR_BEFORE_ALLOC(ctxInfo.interface());
816 GrAssert(GrGLCaps::kNone_MSFBOType != ctxInfo.caps().msFBOType());
817 bool created = false;
818 if (GrGLCaps::kNVDesktop_CoverageAAType ==
819 ctxInfo.caps().coverageAAType()) {
820 const GrGLCaps::MSAACoverageMode& mode =
821 ctxInfo.caps().getMSAACoverageMode(sampleCount);
822 GL_ALLOC_CALL(ctxInfo.interface(),
823 RenderbufferStorageMultisampleCoverage(GR_GL_RENDERBUFFER,
824 mode.fCoverageSampleCnt,
825 mode.fColorSampleCnt,
826 format,
827 width, height));
828 created = (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctxInfo.interface()));
829 }
830 if (!created) {
bsalomon@google.comf6b070d2012-04-27 14:25:44 +0000831 // glRBMS will fail if requested samples is > max samples.
832 sampleCount = GrMin(sampleCount, ctxInfo.caps().maxSampleCount());
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000833 GL_ALLOC_CALL(ctxInfo.interface(),
834 RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
835 sampleCount,
836 format,
837 width, height));
838 created = (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctxInfo.interface()));
839 }
840 return created;
841}
842}
843
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000844bool GrGpuGL::createRenderTargetObjects(int width, int height,
845 GrGLuint texID,
846 GrGLRenderTarget::Desc* desc) {
847 desc->fMSColorRenderbufferID = 0;
848 desc->fRTFBOID = 0;
849 desc->fTexFBOID = 0;
bsalomon@google.com72830222013-01-23 20:25:22 +0000850 desc->fIsWrapped = false;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000851
852 GrGLenum status;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000853
bsalomon@google.comab15d612011-08-09 12:57:56 +0000854 GrGLenum msColorFormat = 0; // suppress warning
855
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000856 GL_CALL(GenFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000857 if (!desc->fTexFBOID) {
858 goto FAILED;
859 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000860
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000861
862 // If we are using multisampling we will create two FBOS. We render
863 // to one and then resolve to the texture bound to the other.
bsalomon@google.come269f212011-11-07 13:29:52 +0000864 if (desc->fSampleCnt > 0) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000865 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000866 goto FAILED;
867 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000868 GL_CALL(GenFramebuffers(1, &desc->fRTFBOID));
869 GL_CALL(GenRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000870 if (!desc->fRTFBOID ||
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000871 !desc->fMSColorRenderbufferID ||
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000872 !this->configToGLFormats(desc->fConfig,
873 // GLES requires sized internal formats
874 kES2_GrGLBinding == this->glBinding(),
875 &msColorFormat, NULL, NULL)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000876 goto FAILED;
877 }
878 } else {
879 desc->fRTFBOID = desc->fTexFBOID;
880 }
881
bsalomon@google.com0e9b41a2012-01-04 22:11:43 +0000882 // below here we may bind the FBO
bsalomon@google.comc811ea32012-05-21 15:33:09 +0000883 fHWBoundRenderTarget = NULL;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000884 if (desc->fRTFBOID != desc->fTexFBOID) {
bsalomon@google.com13ac3a32013-01-09 23:29:39 +0000885 GrAssert(desc->fSampleCnt > 0);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000886 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000887 desc->fMSColorRenderbufferID));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000888 if (!renderbuffer_storage_msaa(fGLContextInfo,
889 desc->fSampleCnt,
890 msColorFormat,
891 width, height)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000892 goto FAILED;
893 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000894 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fRTFBOID));
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000895 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000896 GR_GL_COLOR_ATTACHMENT0,
897 GR_GL_RENDERBUFFER,
898 desc->fMSColorRenderbufferID));
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000899 if (!this->glCaps().isConfigVerifiedColorAttachment(desc->fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000900 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
901 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
902 goto FAILED;
903 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000904 fGLContextInfo.caps().markConfigAsValidColorAttachment(
905 desc->fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000906 }
907 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000908 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000909
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000910 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
911 GR_GL_COLOR_ATTACHMENT0,
912 GR_GL_TEXTURE_2D,
913 texID, 0));
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(desc->fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000920 }
921
922 return true;
923
924FAILED:
925 if (desc->fMSColorRenderbufferID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000926 GL_CALL(DeleteRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000927 }
928 if (desc->fRTFBOID != desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000929 GL_CALL(DeleteFramebuffers(1, &desc->fRTFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000930 }
931 if (desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000932 GL_CALL(DeleteFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000933 }
934 return false;
935}
936
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000937// good to set a break-point here to know when createTexture fails
938static GrTexture* return_null_texture() {
939// GrAssert(!"null texture");
940 return NULL;
941}
942
caryclark@google.comcf6285b2012-06-06 12:09:01 +0000943#if 0 && GR_DEBUG
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000944static size_t as_size_t(int x) {
945 return x;
946}
947#endif
948
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000949GrTexture* GrGpuGL::onCreateTexture(const GrTextureDesc& desc,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000950 const void* srcData,
951 size_t rowBytes) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000952
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000953 GrGLTexture::Desc glTexDesc;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000954 GrGLRenderTarget::Desc glRTDesc;
reed@google.comac10a2d2010-12-22 21:39:39 +0000955
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000956 // Attempt to catch un- or wrongly initialized sample counts;
957 GrAssert(desc.fSampleCnt >= 0 && desc.fSampleCnt <= 64);
958
robertphillips@google.com32716282012-06-04 12:48:45 +0000959 glTexDesc.fFlags = desc.fFlags;
bsalomon@google.com99621082011-11-15 16:47:16 +0000960 glTexDesc.fWidth = desc.fWidth;
961 glTexDesc.fHeight = desc.fHeight;
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000962 glTexDesc.fConfig = desc.fConfig;
robertphillips@google.com32716282012-06-04 12:48:45 +0000963 glTexDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.com72830222013-01-23 20:25:22 +0000964 glTexDesc.fIsWrapped = false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000965
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000966 glRTDesc.fMSColorRenderbufferID = 0;
967 glRTDesc.fRTFBOID = 0;
968 glRTDesc.fTexFBOID = 0;
bsalomon@google.com72830222013-01-23 20:25:22 +0000969 glRTDesc.fIsWrapped = false;
bsalomon@google.com64c4fe42011-11-05 14:51:01 +0000970 glRTDesc.fConfig = glTexDesc.fConfig;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000971
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000972 bool renderTarget = 0 != (desc.fFlags & kRenderTarget_GrTextureFlagBit);
reed@google.comac10a2d2010-12-22 21:39:39 +0000973
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000974 const Caps& caps = this->getCaps();
975
senorblanco@chromium.orged8659b2013-02-04 17:29:51 +0000976 glTexDesc.fOrigin = resolve_origin(desc.fOrigin, renderTarget);
977 glRTDesc.fOrigin = glTexDesc.fOrigin;
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000978
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000979 glRTDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000980 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType() &&
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000981 desc.fSampleCnt) {
bsalomon@google.com945bbe12012-06-15 14:30:34 +0000982 //GrPrintf("MSAA RT requested but not supported on this platform.");
983 return return_null_texture();
reed@google.comac10a2d2010-12-22 21:39:39 +0000984 }
985
reed@google.comac10a2d2010-12-22 21:39:39 +0000986 if (renderTarget) {
bsalomon@google.comf6601872012-08-28 21:11:35 +0000987 if (glTexDesc.fWidth > caps.maxRenderTargetSize() ||
988 glTexDesc.fHeight > caps.maxRenderTargetSize()) {
bsalomon@google.com91958362011-06-13 17:58:13 +0000989 return return_null_texture();
990 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000991 }
992
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000993 GL_CALL(GenTextures(1, &glTexDesc.fTextureID));
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000994 if (renderTarget && this->glCaps().textureUsageSupport()) {
bsalomon@google.com07dd2bf2011-12-09 19:40:36 +0000995 // provides a hint about how this texture will be used
996 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
997 GR_GL_TEXTURE_USAGE,
998 GR_GL_FRAMEBUFFER_ATTACHMENT));
999 }
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001000 if (!glTexDesc.fTextureID) {
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001001 return return_null_texture();
1002 }
1003
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001004 this->setSpareTextureUnit();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001005 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTexDesc.fTextureID));
bsalomon@google.come269f212011-11-07 13:29:52 +00001006
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001007 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
1008 // drivers have a bug where an FBO won't be complete if it includes a
1009 // texture that is not mipmap complete (considering the filter in use).
1010 GrGLTexture::TexParams initialTexParams;
1011 // we only set a subset here so invalidate first
1012 initialTexParams.invalidate();
1013 initialTexParams.fFilter = GR_GL_NEAREST;
1014 initialTexParams.fWrapS = GR_GL_CLAMP_TO_EDGE;
1015 initialTexParams.fWrapT = GR_GL_CLAMP_TO_EDGE;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001016 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1017 GR_GL_TEXTURE_MAG_FILTER,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001018 initialTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001019 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1020 GR_GL_TEXTURE_MIN_FILTER,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001021 initialTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001022 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1023 GR_GL_TEXTURE_WRAP_S,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001024 initialTexParams.fWrapS));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001025 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1026 GR_GL_TEXTURE_WRAP_T,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001027 initialTexParams.fWrapT));
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001028 if (!this->uploadTexData(glTexDesc, true, 0, 0,
1029 glTexDesc.fWidth, glTexDesc.fHeight,
1030 desc.fConfig, srcData, rowBytes)) {
1031 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
1032 return return_null_texture();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001033 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001034
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001035 GrGLTexture* tex;
reed@google.comac10a2d2010-12-22 21:39:39 +00001036 if (renderTarget) {
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001037 // unbind the texture from the texture unit before binding it to the frame buffer
1038 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, 0));
1039
bsalomon@google.com99621082011-11-15 16:47:16 +00001040 if (!this->createRenderTargetObjects(glTexDesc.fWidth,
1041 glTexDesc.fHeight,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001042 glTexDesc.fTextureID,
1043 &glRTDesc)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001044 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001045 return return_null_texture();
1046 }
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001047 tex = SkNEW_ARGS(GrGLTexture, (this, glTexDesc, glRTDesc));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001048 } else {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001049 tex = SkNEW_ARGS(GrGLTexture, (this, glTexDesc));
reed@google.comac10a2d2010-12-22 21:39:39 +00001050 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001051 tex->setCachedTexParams(initialTexParams, this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00001052#ifdef TRACE_TEXTURE_CREATION
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001053 GrPrintf("--- new texture [%d] size=(%d %d) config=%d\n",
1054 glTexDesc.fTextureID, desc.fWidth, desc.fHeight, desc.fConfig);
reed@google.comac10a2d2010-12-22 21:39:39 +00001055#endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001056 return tex;
1057}
1058
1059namespace {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001060
1061const GrGLuint kUnknownBitCount = GrGLStencilBuffer::kUnknownBitCount;
1062
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001063void inline get_stencil_rb_sizes(const GrGLInterface* gl,
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001064 GrGLuint rb,
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001065 GrGLStencilBuffer::Format* format) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001066 // we shouldn't ever know one size and not the other
1067 GrAssert((kUnknownBitCount == format->fStencilBits) ==
1068 (kUnknownBitCount == format->fTotalBits));
1069 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001070 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001071 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1072 (GrGLint*)&format->fStencilBits);
1073 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001074 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001075 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1076 (GrGLint*)&format->fTotalBits);
1077 format->fTotalBits += format->fStencilBits;
1078 } else {
1079 format->fTotalBits = format->fStencilBits;
1080 }
1081 }
1082}
1083}
1084
1085bool GrGpuGL::createStencilBufferForRenderTarget(GrRenderTarget* rt,
1086 int width, int height) {
1087
1088 // All internally created RTs are also textures. We don't create
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +00001089 // 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 +00001090 GrAssert(rt->asTexture());
bsalomon@google.com99621082011-11-15 16:47:16 +00001091 GrAssert(width >= rt->width());
1092 GrAssert(height >= rt->height());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001093
1094 int samples = rt->numSamples();
1095 GrGLuint sbID;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001096 GL_CALL(GenRenderbuffers(1, &sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001097 if (!sbID) {
1098 return false;
1099 }
1100
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001101 int stencilFmtCnt = this->glCaps().stencilFormats().count();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001102 for (int i = 0; i < stencilFmtCnt; ++i) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001103 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001104 // we start with the last stencil format that succeeded in hopes
1105 // that we won't go through this loop more than once after the
1106 // first (painful) stencil creation.
1107 int sIdx = (i + fLastSuccessfulStencilFmtIdx) % stencilFmtCnt;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001108 const GrGLCaps::StencilFormat& sFmt =
1109 this->glCaps().stencilFormats()[sIdx];
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001110 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001111 // we do this "if" so that we don't call the multisample
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001112 // version on a GL that doesn't have an MSAA extension.
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001113 bool created;
1114 if (samples > 0) {
1115 created = renderbuffer_storage_msaa(fGLContextInfo,
1116 samples,
1117 sFmt.fInternalFormat,
1118 width, height);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001119 } else {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001120 GL_ALLOC_CALL(this->glInterface(),
1121 RenderbufferStorage(GR_GL_RENDERBUFFER,
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001122 sFmt.fInternalFormat,
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001123 width, height));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001124 created =
1125 (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(this->glInterface()));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001126 }
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001127 if (created) {
1128 // After sized formats we attempt an unsized format and take
1129 // whatever sizes GL gives us. In that case we query for the size.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001130 GrGLStencilBuffer::Format format = sFmt;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001131 get_stencil_rb_sizes(this->glInterface(), sbID, &format);
bsalomon@google.com72830222013-01-23 20:25:22 +00001132 static const bool kIsWrapped = false;
robertphillips@google.comf2e93fc2012-09-05 19:44:18 +00001133 SkAutoTUnref<GrStencilBuffer> sb(SkNEW_ARGS(GrGLStencilBuffer,
bsalomon@google.com72830222013-01-23 20:25:22 +00001134 (this, kIsWrapped, sbID, width, height,
robertphillips@google.comf2e93fc2012-09-05 19:44:18 +00001135 samples, format)));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001136 if (this->attachStencilBufferToRenderTarget(sb, rt)) {
1137 fLastSuccessfulStencilFmtIdx = sIdx;
robertphillips@google.com9fbcad02012-09-09 14:44:15 +00001138 sb->transferToCache();
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001139 rt->setStencilBuffer(sb);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001140 return true;
1141 }
1142 sb->abandon(); // otherwise we lose sbID
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001143 }
1144 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001145 GL_CALL(DeleteRenderbuffers(1, &sbID));
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001146 return false;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001147}
1148
1149bool GrGpuGL::attachStencilBufferToRenderTarget(GrStencilBuffer* sb,
1150 GrRenderTarget* rt) {
1151 GrGLRenderTarget* glrt = (GrGLRenderTarget*) rt;
1152
1153 GrGLuint fbo = glrt->renderFBOID();
1154
1155 if (NULL == sb) {
1156 if (NULL != rt->getStencilBuffer()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001157 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001158 GR_GL_STENCIL_ATTACHMENT,
1159 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001160 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001161 GR_GL_DEPTH_ATTACHMENT,
1162 GR_GL_RENDERBUFFER, 0));
1163#if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001164 GrGLenum status;
1165 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001166 GrAssert(GR_GL_FRAMEBUFFER_COMPLETE == status);
1167#endif
1168 }
1169 return true;
1170 } else {
1171 GrGLStencilBuffer* glsb = (GrGLStencilBuffer*) sb;
1172 GrGLuint rb = glsb->renderbufferID();
1173
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001174 fHWBoundRenderTarget = NULL;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001175 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, fbo));
1176 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001177 GR_GL_STENCIL_ATTACHMENT,
1178 GR_GL_RENDERBUFFER, rb));
1179 if (glsb->format().fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001180 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001181 GR_GL_DEPTH_ATTACHMENT,
1182 GR_GL_RENDERBUFFER, rb));
1183 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001184 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001185 GR_GL_DEPTH_ATTACHMENT,
1186 GR_GL_RENDERBUFFER, 0));
reed@google.comac10a2d2010-12-22 21:39:39 +00001187 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001188
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001189 GrGLenum status;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001190 if (!this->glCaps().isColorConfigAndStencilFormatVerified(rt->config(),
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001191 glsb->format())) {
1192 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1193 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001194 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001195 GR_GL_STENCIL_ATTACHMENT,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001196 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001197 if (glsb->format().fPacked) {
1198 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1199 GR_GL_DEPTH_ATTACHMENT,
1200 GR_GL_RENDERBUFFER, 0));
1201 }
1202 return false;
1203 } else {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001204 fGLContextInfo.caps().markColorConfigAndStencilFormatAsVerified(
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001205 rt->config(),
1206 glsb->format());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001207 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001208 }
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001209 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001210 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001211}
1212
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001213////////////////////////////////////////////////////////////////////////////////
1214
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001215GrVertexBuffer* GrGpuGL::onCreateVertexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001216 GrGLuint id;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001217 GL_CALL(GenBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001218 if (id) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001219 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, id));
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001220 fHWGeometryState.fArrayPtrsDirty = true;
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001221 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001222 // make sure driver can allocate memory for this buffer
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001223 GL_ALLOC_CALL(this->glInterface(),
1224 BufferData(GR_GL_ARRAY_BUFFER,
1225 size,
1226 NULL, // data ptr
1227 dynamic ? GR_GL_DYNAMIC_DRAW :
1228 GR_GL_STATIC_DRAW));
1229 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001230 GL_CALL(DeleteBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001231 // deleting bound buffer does implicit bind to 0
1232 fHWGeometryState.fVertexBuffer = NULL;
1233 return NULL;
1234 }
bsalomon@google.com72830222013-01-23 20:25:22 +00001235 static const bool kIsWrapped = false;
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001236 GrGLVertexBuffer* vertexBuffer = SkNEW_ARGS(GrGLVertexBuffer,
bsalomon@google.com72830222013-01-23 20:25:22 +00001237 (this, kIsWrapped, id,
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001238 size, dynamic));
reed@google.comac10a2d2010-12-22 21:39:39 +00001239 fHWGeometryState.fVertexBuffer = vertexBuffer;
1240 return vertexBuffer;
1241 }
1242 return NULL;
1243}
1244
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001245GrIndexBuffer* GrGpuGL::onCreateIndexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001246 GrGLuint id;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001247 GL_CALL(GenBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001248 if (id) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001249 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, id));
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001250 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001251 // make sure driver can allocate memory for this buffer
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001252 GL_ALLOC_CALL(this->glInterface(),
1253 BufferData(GR_GL_ELEMENT_ARRAY_BUFFER,
1254 size,
1255 NULL, // data ptr
1256 dynamic ? GR_GL_DYNAMIC_DRAW :
1257 GR_GL_STATIC_DRAW));
1258 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001259 GL_CALL(DeleteBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001260 // deleting bound buffer does implicit bind to 0
1261 fHWGeometryState.fIndexBuffer = NULL;
1262 return NULL;
1263 }
bsalomon@google.com72830222013-01-23 20:25:22 +00001264 static const bool kIsWrapped = false;
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001265 GrIndexBuffer* indexBuffer = SkNEW_ARGS(GrGLIndexBuffer,
bsalomon@google.com72830222013-01-23 20:25:22 +00001266 (this, kIsWrapped, id, size, dynamic));
reed@google.comac10a2d2010-12-22 21:39:39 +00001267 fHWGeometryState.fIndexBuffer = indexBuffer;
1268 return indexBuffer;
1269 }
1270 return NULL;
1271}
1272
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00001273GrPath* GrGpuGL::onCreatePath(const SkPath& inPath) {
bsalomon@google.comf6601872012-08-28 21:11:35 +00001274 GrAssert(fCaps.pathStencilingSupport());
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001275 return SkNEW_ARGS(GrGLPath, (this, inPath));
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00001276}
1277
bsalomon@google.coma3201942012-06-21 19:58:20 +00001278void GrGpuGL::flushScissor() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001279 const GrDrawState& drawState = this->getDrawState();
1280 const GrGLRenderTarget* rt =
1281 static_cast<const GrGLRenderTarget*>(drawState.getRenderTarget());
1282
1283 GrAssert(NULL != rt);
1284 const GrGLIRect& vp = rt->getViewport();
reed@google.comac10a2d2010-12-22 21:39:39 +00001285
bsalomon@google.coma3201942012-06-21 19:58:20 +00001286 if (fScissorState.fEnabled) {
1287 GrGLIRect scissor;
1288 scissor.setRelativeTo(vp,
1289 fScissorState.fRect.fLeft,
1290 fScissorState.fRect.fTop,
1291 fScissorState.fRect.width(),
senorblanco@chromium.orged8659b2013-02-04 17:29:51 +00001292 fScissorState.fRect.height(),
1293 rt->origin());
bsalomon@google.coma3201942012-06-21 19:58:20 +00001294 // if the scissor fully contains the viewport then we fall through and
1295 // disable the scissor test.
1296 if (!scissor.contains(vp)) {
1297 if (fHWScissorSettings.fRect != scissor) {
1298 scissor.pushToGLScissor(this->glInterface());
1299 fHWScissorSettings.fRect = scissor;
1300 }
1301 if (kYes_TriState != fHWScissorSettings.fEnabled) {
1302 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1303 fHWScissorSettings.fEnabled = kYes_TriState;
1304 }
1305 return;
1306 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001307 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001308 if (kNo_TriState != fHWScissorSettings.fEnabled) {
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001309 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.coma3201942012-06-21 19:58:20 +00001310 fHWScissorSettings.fEnabled = kNo_TriState;
1311 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001312 }
1313}
1314
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001315void GrGpuGL::onClear(const GrIRect* rect, GrColor color) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001316 const GrDrawState& drawState = this->getDrawState();
1317 const GrRenderTarget* rt = drawState.getRenderTarget();
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001318 // parent class should never let us get here with no RT
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001319 GrAssert(NULL != rt);
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001320
bsalomon@google.com74b98712011-11-11 19:46:16 +00001321 GrIRect clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001322 if (NULL != rect) {
1323 // flushScissor expects rect to be clipped to the target.
bsalomon@google.com74b98712011-11-11 19:46:16 +00001324 clippedRect = *rect;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001325 GrIRect rtRect = SkIRect::MakeWH(rt->width(), rt->height());
bsalomon@google.com74b98712011-11-11 19:46:16 +00001326 if (clippedRect.intersect(rtRect)) {
1327 rect = &clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001328 } else {
1329 return;
1330 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001331 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001332 this->flushRenderTarget(rect);
bsalomon@google.coma3201942012-06-21 19:58:20 +00001333 GrAutoTRestore<ScissorState> asr(&fScissorState);
1334 fScissorState.fEnabled = (NULL != rect);
1335 if (fScissorState.fEnabled) {
1336 fScissorState.fRect = *rect;
1337 }
1338 this->flushScissor();
bsalomon@google.com74b98712011-11-11 19:46:16 +00001339
1340 GrGLfloat r, g, b, a;
1341 static const GrGLfloat scale255 = 1.f / 255.f;
1342 a = GrColorUnpackA(color) * scale255;
1343 GrGLfloat scaleRGB = scale255;
bsalomon@google.com74b98712011-11-11 19:46:16 +00001344 r = GrColorUnpackR(color) * scaleRGB;
1345 g = GrColorUnpackG(color) * scaleRGB;
1346 b = GrColorUnpackB(color) * scaleRGB;
1347
1348 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00001349 fHWWriteToColor = kYes_TriState;
bsalomon@google.com74b98712011-11-11 19:46:16 +00001350 GL_CALL(ClearColor(r, g, b, a));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001351 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001352}
1353
bsalomon@google.comedc177d2011-08-05 15:46:40 +00001354void GrGpuGL::clearStencil() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001355 if (NULL == this->getDrawState().getRenderTarget()) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001356 return;
1357 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001358
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001359 this->flushRenderTarget(&GrIRect::EmptyIRect());
1360
bsalomon@google.coma3201942012-06-21 19:58:20 +00001361 GrAutoTRestore<ScissorState> asr(&fScissorState);
1362 fScissorState.fEnabled = false;
1363 this->flushScissor();
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001364
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001365 GL_CALL(StencilMask(0xffffffff));
1366 GL_CALL(ClearStencil(0));
1367 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001368 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001369}
1370
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001371void GrGpuGL::clearStencilClip(const GrIRect& rect, bool insideClip) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001372 const GrDrawState& drawState = this->getDrawState();
1373 const GrRenderTarget* rt = drawState.getRenderTarget();
1374 GrAssert(NULL != rt);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001375
1376 // this should only be called internally when we know we have a
1377 // stencil buffer.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001378 GrAssert(NULL != rt->getStencilBuffer());
1379 GrGLint stencilBitCount = rt->getStencilBuffer()->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001380#if 0
reed@google.comac10a2d2010-12-22 21:39:39 +00001381 GrAssert(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00001382 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001383#else
1384 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001385 // ANGLE a partial stencil mask will cause clears to be
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001386 // turned into draws. Our contract on GrDrawTarget says that
1387 // changing the clip between stencil passes may or may not
1388 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00001389 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001390#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001391 GrGLint value;
1392 if (insideClip) {
1393 value = (1 << (stencilBitCount - 1));
1394 } else {
1395 value = 0;
1396 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001397 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.coma3201942012-06-21 19:58:20 +00001398
1399 GrAutoTRestore<ScissorState> asr(&fScissorState);
1400 fScissorState.fEnabled = true;
1401 fScissorState.fRect = rect;
1402 this->flushScissor();
1403
caryclark@google.comcf6285b2012-06-06 12:09:01 +00001404 GL_CALL(StencilMask((uint32_t) clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001405 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001406 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001407 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001408}
1409
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001410void GrGpuGL::onForceRenderTargetFlush() {
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001411 this->flushRenderTarget(&GrIRect::EmptyIRect());
reed@google.comac10a2d2010-12-22 21:39:39 +00001412}
1413
bsalomon@google.comc4364992011-11-07 15:54:49 +00001414bool GrGpuGL::readPixelsWillPayForYFlip(GrRenderTarget* renderTarget,
1415 int left, int top,
1416 int width, int height,
1417 GrPixelConfig config,
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001418 size_t rowBytes) const {
senorblanco@chromium.orged8659b2013-02-04 17:29:51 +00001419 // If this rendertarget is aready TopLeft, we don't need to flip.
1420 if (kTopLeft_GrSurfaceOrigin == renderTarget->origin()) {
1421 return false;
1422 }
1423
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001424 // if GL can do the flip then we'll never pay for it.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001425 if (this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001426 return false;
1427 }
1428
1429 // If we have to do memcpy to handle non-trim rowBytes then we
bsalomon@google.com7107fa72011-11-10 14:54:14 +00001430 // get the flip for free. Otherwise it costs.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001431 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001432 return true;
1433 }
1434 // If we have to do memcpys to handle rowBytes then y-flip is free
1435 // Note the rowBytes might be tight to the passed in data, but if data
1436 // gets clipped in x to the target the rowBytes will no longer be tight.
1437 if (left >= 0 && (left + width) < renderTarget->width()) {
1438 return 0 == rowBytes ||
1439 GrBytesPerPixel(config) * width == rowBytes;
1440 } else {
1441 return false;
1442 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001443}
1444
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001445bool GrGpuGL::onReadPixels(GrRenderTarget* target,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001446 int left, int top,
1447 int width, int height,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001448 GrPixelConfig config,
1449 void* buffer,
senorblanco@chromium.orged8659b2013-02-04 17:29:51 +00001450 size_t rowBytes) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001451 GrGLenum format;
1452 GrGLenum type;
senorblanco@chromium.orged8659b2013-02-04 17:29:51 +00001453 bool flipY = kBottomLeft_GrSurfaceOrigin == target->origin();
bsalomon@google.com280e99f2012-01-05 16:17:38 +00001454 if (!this->configToGLFormats(config, false, NULL, &format, &type)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001455 return false;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001456 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001457 size_t bpp = GrBytesPerPixel(config);
1458 if (!adjust_pixel_ops_params(target->width(), target->height(), bpp,
1459 &left, &top, &width, &height,
1460 const_cast<const void**>(&buffer),
1461 &rowBytes)) {
1462 return false;
1463 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001464
bsalomon@google.comc6980972011-11-02 19:57:21 +00001465 // resolve the render target if necessary
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001466 GrGLRenderTarget* tgt = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001467 GrDrawState::AutoRenderTargetRestore artr;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001468 switch (tgt->getResolveType()) {
1469 case GrGLRenderTarget::kCantResolve_ResolveType:
1470 return false;
1471 case GrGLRenderTarget::kAutoResolves_ResolveType:
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001472 artr.set(this->drawState(), target);
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001473 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001474 break;
1475 case GrGLRenderTarget::kCanResolve_ResolveType:
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001476 this->onResolveRenderTarget(tgt);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001477 // we don't track the state of the READ FBO ID.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001478 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1479 tgt->textureFBOID()));
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001480 break;
1481 default:
1482 GrCrash("Unknown resolve type");
reed@google.comac10a2d2010-12-22 21:39:39 +00001483 }
1484
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001485 const GrGLIRect& glvp = tgt->getViewport();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001486
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001487 // the read rect is viewport-relative
1488 GrGLIRect readRect;
senorblanco@chromium.orged8659b2013-02-04 17:29:51 +00001489 readRect.setRelativeTo(glvp, left, top, width, height, target->origin());
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001490
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001491 size_t tightRowBytes = bpp * width;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001492 if (0 == rowBytes) {
1493 rowBytes = tightRowBytes;
1494 }
1495 size_t readDstRowBytes = tightRowBytes;
1496 void* readDst = buffer;
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001497
bsalomon@google.comc6980972011-11-02 19:57:21 +00001498 // determine if GL can read using the passed rowBytes or if we need
1499 // a scratch buffer.
1500 SkAutoSMalloc<32 * sizeof(GrColor)> scratch;
1501 if (rowBytes != tightRowBytes) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001502 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.comc6980972011-11-02 19:57:21 +00001503 GrAssert(!(rowBytes % sizeof(GrColor)));
1504 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, rowBytes / sizeof(GrColor)));
1505 readDstRowBytes = rowBytes;
1506 } else {
1507 scratch.reset(tightRowBytes * height);
1508 readDst = scratch.get();
1509 }
1510 }
senorblanco@chromium.orged8659b2013-02-04 17:29:51 +00001511 if (flipY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001512 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 1));
1513 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001514 GL_CALL(ReadPixels(readRect.fLeft, readRect.fBottom,
1515 readRect.fWidth, readRect.fHeight,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001516 format, type, readDst));
1517 if (readDstRowBytes != tightRowBytes) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001518 GrAssert(this->glCaps().packRowLengthSupport());
bsalomon@google.comc6980972011-11-02 19:57:21 +00001519 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
1520 }
senorblanco@chromium.orged8659b2013-02-04 17:29:51 +00001521 if (flipY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001522 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 0));
senorblanco@chromium.orged8659b2013-02-04 17:29:51 +00001523 flipY = false;
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001524 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001525
1526 // now reverse the order of the rows, since GL's are bottom-to-top, but our
bsalomon@google.comc6980972011-11-02 19:57:21 +00001527 // API presents top-to-bottom. We must preserve the padding contents. Note
1528 // that the above readPixels did not overwrite the padding.
1529 if (readDst == buffer) {
1530 GrAssert(rowBytes == readDstRowBytes);
senorblanco@chromium.orged8659b2013-02-04 17:29:51 +00001531 if (flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001532 scratch.reset(tightRowBytes);
1533 void* tmpRow = scratch.get();
1534 // flip y in-place by rows
1535 const int halfY = height >> 1;
1536 char* top = reinterpret_cast<char*>(buffer);
1537 char* bottom = top + (height - 1) * rowBytes;
1538 for (int y = 0; y < halfY; y++) {
1539 memcpy(tmpRow, top, tightRowBytes);
1540 memcpy(top, bottom, tightRowBytes);
1541 memcpy(bottom, tmpRow, tightRowBytes);
1542 top += rowBytes;
1543 bottom -= rowBytes;
1544 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001545 }
1546 } else {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001547 GrAssert(readDst != buffer); GrAssert(rowBytes != tightRowBytes);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001548 // copy from readDst to buffer while flipping y
caryclark@google.comcf6285b2012-06-06 12:09:01 +00001549 // const int halfY = height >> 1;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001550 const char* src = reinterpret_cast<const char*>(readDst);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001551 char* dst = reinterpret_cast<char*>(buffer);
senorblanco@chromium.orged8659b2013-02-04 17:29:51 +00001552 if (flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001553 dst += (height-1) * rowBytes;
1554 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001555 for (int y = 0; y < height; y++) {
1556 memcpy(dst, src, tightRowBytes);
1557 src += readDstRowBytes;
senorblanco@chromium.orged8659b2013-02-04 17:29:51 +00001558 if (!flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001559 dst += rowBytes;
1560 } else {
1561 dst -= rowBytes;
1562 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001563 }
1564 }
1565 return true;
1566}
1567
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001568void GrGpuGL::flushRenderTarget(const GrIRect* bound) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001569
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001570 GrGLRenderTarget* rt =
1571 static_cast<GrGLRenderTarget*>(this->drawState()->getRenderTarget());
1572 GrAssert(NULL != rt);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001573
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001574 if (fHWBoundRenderTarget != rt) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001575 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, rt->renderFBOID()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001576 #if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001577 GrGLenum status;
1578 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001579 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
reed@google.comb9255d52011-06-13 18:54:59 +00001580 GrPrintf("GrGpuGL::flushRenderTarget glCheckFramebufferStatus %x\n", status);
reed@google.comac10a2d2010-12-22 21:39:39 +00001581 }
1582 #endif
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001583 fHWBoundRenderTarget = rt;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001584 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.coma3201942012-06-21 19:58:20 +00001585 if (fHWViewport != vp) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001586 vp.pushToGLViewport(this->glInterface());
bsalomon@google.coma3201942012-06-21 19:58:20 +00001587 fHWViewport = vp;
reed@google.comac10a2d2010-12-22 21:39:39 +00001588 }
1589 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001590 if (NULL == bound || !bound->isEmpty()) {
1591 rt->flagAsNeedingResolve(bound);
1592 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001593}
1594
twiz@google.com0f31ca72011-03-18 17:38:11 +00001595GrGLenum gPrimitiveType2GLMode[] = {
1596 GR_GL_TRIANGLES,
1597 GR_GL_TRIANGLE_STRIP,
1598 GR_GL_TRIANGLE_FAN,
1599 GR_GL_POINTS,
1600 GR_GL_LINES,
1601 GR_GL_LINE_STRIP
reed@google.comac10a2d2010-12-22 21:39:39 +00001602};
1603
bsalomon@google.comd302f142011-03-03 13:54:13 +00001604#define SWAP_PER_DRAW 0
1605
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001606#if SWAP_PER_DRAW
bsalomon@google.comd302f142011-03-03 13:54:13 +00001607 #if GR_MAC_BUILD
1608 #include <AGL/agl.h>
1609 #elif GR_WIN32_BUILD
bsalomon@google.comce7357d2012-06-25 17:49:25 +00001610 #include <gl/GL.h>
bsalomon@google.comd302f142011-03-03 13:54:13 +00001611 void SwapBuf() {
1612 DWORD procID = GetCurrentProcessId();
1613 HWND hwnd = GetTopWindow(GetDesktopWindow());
1614 while(hwnd) {
1615 DWORD wndProcID = 0;
1616 GetWindowThreadProcessId(hwnd, &wndProcID);
1617 if(wndProcID == procID) {
1618 SwapBuffers(GetDC(hwnd));
1619 }
1620 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
1621 }
1622 }
1623 #endif
1624#endif
1625
bsalomon@google.com74749cd2013-01-30 16:12:41 +00001626void GrGpuGL::onGpuDraw(const DrawInfo& info) {
1627 int extraStartIndexOffset;
1628 this->setupGeometry(info, &extraStartIndexOffset);
reed@google.comac10a2d2010-12-22 21:39:39 +00001629
bsalomon@google.com74749cd2013-01-30 16:12:41 +00001630 GrAssert((size_t)info.primitiveType() < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001631 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1632
bsalomon@google.com74749cd2013-01-30 16:12:41 +00001633 if (info.isIndexed()) {
1634 GrAssert(NULL != fHWGeometryState.fIndexBuffer);
1635 GrGLvoid* indices = (GrGLvoid*)(sizeof(uint16_t) * (info.startIndex() +
1636 extraStartIndexOffset));
1637 // info.startVertex() was accounted for by setupGeometry.
1638 GL_CALL(DrawElements(gPrimitiveType2GLMode[info.primitiveType()],
1639 info.indexCount(),
1640 GR_GL_UNSIGNED_SHORT,
1641 indices));
1642 } else {
1643 // Pass 0 for parameter first. We have to adjust glVertexAttribPointer() to account for
1644 // startVertex in the DrawElements case. So we always rely on setupGeometry to have
1645 // accounted for startVertex.
1646 GL_CALL(DrawArrays(gPrimitiveType2GLMode[info.primitiveType()], 0, info.vertexCount()));
1647 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00001648#if SWAP_PER_DRAW
1649 glFlush();
1650 #if GR_MAC_BUILD
1651 aglSwapBuffers(aglGetCurrentContext());
1652 int set_a_break_pt_here = 9;
1653 aglSwapBuffers(aglGetCurrentContext());
1654 #elif GR_WIN32_BUILD
1655 SwapBuf();
1656 int set_a_break_pt_here = 9;
1657 SwapBuf();
1658 #endif
1659#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001660}
1661
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001662namespace {
bsalomon@google.com100abf42012-09-05 17:40:04 +00001663
1664static const uint16_t kOnes16 = static_cast<uint16_t>(~0);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001665const GrStencilSettings& winding_nv_path_stencil_settings() {
1666 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
1667 kIncClamp_StencilOp,
1668 kIncClamp_StencilOp,
1669 kAlwaysIfInClip_StencilFunc,
bsalomon@google.com100abf42012-09-05 17:40:04 +00001670 kOnes16, kOnes16, kOnes16);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001671 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
1672}
1673const GrStencilSettings& even_odd_nv_path_stencil_settings() {
1674 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
1675 kInvert_StencilOp,
1676 kInvert_StencilOp,
1677 kAlwaysIfInClip_StencilFunc,
bsalomon@google.com100abf42012-09-05 17:40:04 +00001678 kOnes16, kOnes16, kOnes16);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001679 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
1680}
1681}
1682
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001683void GrGpuGL::setStencilPathSettings(const GrPath&,
sugoi@google.com12b4e272012-12-06 20:13:11 +00001684 SkPath::FillType fill,
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001685 GrStencilSettings* settings) {
1686 switch (fill) {
sugoi@google.com12b4e272012-12-06 20:13:11 +00001687 case SkPath::kEvenOdd_FillType:
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001688 *settings = even_odd_nv_path_stencil_settings();
1689 return;
sugoi@google.com12b4e272012-12-06 20:13:11 +00001690 case SkPath::kWinding_FillType:
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001691 *settings = winding_nv_path_stencil_settings();
1692 return;
1693 default:
1694 GrCrash("Unexpected path fill.");
1695 }
1696}
1697
sugoi@google.com12b4e272012-12-06 20:13:11 +00001698void GrGpuGL::onGpuStencilPath(const GrPath* path, SkPath::FillType fill) {
bsalomon@google.comf6601872012-08-28 21:11:35 +00001699 GrAssert(fCaps.pathStencilingSupport());
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001700
1701 GrGLuint id = static_cast<const GrGLPath*>(path)->pathID();
1702 GrDrawState* drawState = this->drawState();
1703 GrAssert(NULL != drawState->getRenderTarget());
1704 if (NULL == drawState->getRenderTarget()->getStencilBuffer()) {
1705 return;
1706 }
1707
1708 // Decide how to manipulate the stencil buffer based on the fill rule.
1709 // Also, assert that the stencil settings we set in setStencilPathSettings
1710 // are present.
1711 GrAssert(!fStencilSettings.isTwoSided());
1712 GrGLenum fillMode;
1713 switch (fill) {
sugoi@google.com12b4e272012-12-06 20:13:11 +00001714 case SkPath::kWinding_FillType:
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001715 fillMode = GR_GL_COUNT_UP;
1716 GrAssert(kIncClamp_StencilOp ==
1717 fStencilSettings.passOp(GrStencilSettings::kFront_Face));
1718 GrAssert(kIncClamp_StencilOp ==
1719 fStencilSettings.failOp(GrStencilSettings::kFront_Face));
1720 break;
sugoi@google.com12b4e272012-12-06 20:13:11 +00001721 case SkPath::kEvenOdd_FillType:
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001722 fillMode = GR_GL_INVERT;
1723 GrAssert(kInvert_StencilOp ==
1724 fStencilSettings.passOp(GrStencilSettings::kFront_Face));
1725 GrAssert(kInvert_StencilOp ==
1726 fStencilSettings.failOp(GrStencilSettings::kFront_Face));
1727 break;
1728 default:
1729 // Only the above two fill rules are allowed.
1730 GrCrash("Unexpected path fill.");
bsalomon@google.com548a4332012-07-11 19:45:22 +00001731 return; // suppress unused var warning.
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001732 }
1733 GrGLint writeMask = fStencilSettings.writeMask(GrStencilSettings::kFront_Face);
1734 GL_CALL(StencilFillPath(id, fillMode, writeMask));
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00001735}
1736
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001737void GrGpuGL::onResolveRenderTarget(GrRenderTarget* target) {
1738
1739 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(target);
reed@google.comac10a2d2010-12-22 21:39:39 +00001740
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001741 if (rt->needsResolve()) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001742 GrAssert(GrGLCaps::kNone_MSFBOType != this->glCaps().msFBOType());
reed@google.comac10a2d2010-12-22 21:39:39 +00001743 GrAssert(rt->textureFBOID() != rt->renderFBOID());
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001744 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1745 rt->renderFBOID()));
1746 GL_CALL(BindFramebuffer(GR_GL_DRAW_FRAMEBUFFER,
1747 rt->textureFBOID()));
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001748 // make sure we go through flushRenderTarget() since we've modified
1749 // the bound DRAW FBO ID.
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001750 fHWBoundRenderTarget = NULL;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001751 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001752 const GrIRect dirtyRect = rt->getResolveRect();
1753 GrGLIRect r;
bsalomon@google.coma3201942012-06-21 19:58:20 +00001754 r.setRelativeTo(vp, dirtyRect.fLeft, dirtyRect.fTop,
senorblanco@chromium.orged8659b2013-02-04 17:29:51 +00001755 dirtyRect.width(), dirtyRect.height(), target->origin());
reed@google.comac10a2d2010-12-22 21:39:39 +00001756
bsalomon@google.coma3201942012-06-21 19:58:20 +00001757 GrAutoTRestore<ScissorState> asr;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001758 if (GrGLCaps::kAppleES_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001759 // Apple's extension uses the scissor as the blit bounds.
bsalomon@google.coma3201942012-06-21 19:58:20 +00001760 asr.reset(&fScissorState);
1761 fScissorState.fEnabled = true;
1762 fScissorState.fRect = dirtyRect;
1763 this->flushScissor();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001764 GL_CALL(ResolveMultisampleFramebuffer());
reed@google.comac10a2d2010-12-22 21:39:39 +00001765 } else {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001766 if (GrGLCaps::kDesktopARB_MSFBOType != this->glCaps().msFBOType()) {
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001767 // this respects the scissor during the blit, so disable it.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001768 GrAssert(GrGLCaps::kDesktopEXT_MSFBOType ==
1769 this->glCaps().msFBOType());
bsalomon@google.coma3201942012-06-21 19:58:20 +00001770 asr.reset(&fScissorState);
1771 fScissorState.fEnabled = false;
1772 this->flushScissor();
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001773 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001774 int right = r.fLeft + r.fWidth;
1775 int top = r.fBottom + r.fHeight;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001776 GL_CALL(BlitFramebuffer(r.fLeft, r.fBottom, right, top,
1777 r.fLeft, r.fBottom, right, top,
1778 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001779 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001780 rt->flagAsResolved();
reed@google.comac10a2d2010-12-22 21:39:39 +00001781 }
1782}
1783
bsalomon@google.com411dad02012-06-05 20:24:20 +00001784namespace {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001785
bsalomon@google.com411dad02012-06-05 20:24:20 +00001786GrGLenum gr_to_gl_stencil_func(GrStencilFunc basicFunc) {
1787 static const GrGLenum gTable[] = {
1788 GR_GL_ALWAYS, // kAlways_StencilFunc
1789 GR_GL_NEVER, // kNever_StencilFunc
1790 GR_GL_GREATER, // kGreater_StencilFunc
1791 GR_GL_GEQUAL, // kGEqual_StencilFunc
1792 GR_GL_LESS, // kLess_StencilFunc
1793 GR_GL_LEQUAL, // kLEqual_StencilFunc,
1794 GR_GL_EQUAL, // kEqual_StencilFunc,
1795 GR_GL_NOTEQUAL, // kNotEqual_StencilFunc,
1796 };
1797 GR_STATIC_ASSERT(GR_ARRAY_COUNT(gTable) == kBasicStencilFuncCount);
1798 GR_STATIC_ASSERT(0 == kAlways_StencilFunc);
1799 GR_STATIC_ASSERT(1 == kNever_StencilFunc);
1800 GR_STATIC_ASSERT(2 == kGreater_StencilFunc);
1801 GR_STATIC_ASSERT(3 == kGEqual_StencilFunc);
1802 GR_STATIC_ASSERT(4 == kLess_StencilFunc);
1803 GR_STATIC_ASSERT(5 == kLEqual_StencilFunc);
1804 GR_STATIC_ASSERT(6 == kEqual_StencilFunc);
1805 GR_STATIC_ASSERT(7 == kNotEqual_StencilFunc);
1806 GrAssert((unsigned) basicFunc < kBasicStencilFuncCount);
1807
1808 return gTable[basicFunc];
1809}
1810
1811GrGLenum gr_to_gl_stencil_op(GrStencilOp op) {
1812 static const GrGLenum gTable[] = {
1813 GR_GL_KEEP, // kKeep_StencilOp
1814 GR_GL_REPLACE, // kReplace_StencilOp
1815 GR_GL_INCR_WRAP, // kIncWrap_StencilOp
1816 GR_GL_INCR, // kIncClamp_StencilOp
1817 GR_GL_DECR_WRAP, // kDecWrap_StencilOp
1818 GR_GL_DECR, // kDecClamp_StencilOp
1819 GR_GL_ZERO, // kZero_StencilOp
1820 GR_GL_INVERT, // kInvert_StencilOp
1821 };
1822 GR_STATIC_ASSERT(GR_ARRAY_COUNT(gTable) == kStencilOpCount);
1823 GR_STATIC_ASSERT(0 == kKeep_StencilOp);
1824 GR_STATIC_ASSERT(1 == kReplace_StencilOp);
1825 GR_STATIC_ASSERT(2 == kIncWrap_StencilOp);
1826 GR_STATIC_ASSERT(3 == kIncClamp_StencilOp);
1827 GR_STATIC_ASSERT(4 == kDecWrap_StencilOp);
1828 GR_STATIC_ASSERT(5 == kDecClamp_StencilOp);
1829 GR_STATIC_ASSERT(6 == kZero_StencilOp);
1830 GR_STATIC_ASSERT(7 == kInvert_StencilOp);
1831 GrAssert((unsigned) op < kStencilOpCount);
1832 return gTable[op];
1833}
1834
1835void set_gl_stencil(const GrGLInterface* gl,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001836 const GrStencilSettings& settings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00001837 GrGLenum glFace,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001838 GrStencilSettings::Face grFace) {
1839 GrGLenum glFunc = gr_to_gl_stencil_func(settings.func(grFace));
1840 GrGLenum glFailOp = gr_to_gl_stencil_op(settings.failOp(grFace));
1841 GrGLenum glPassOp = gr_to_gl_stencil_op(settings.passOp(grFace));
1842
1843 GrGLint ref = settings.funcRef(grFace);
1844 GrGLint mask = settings.funcMask(grFace);
1845 GrGLint writeMask = settings.writeMask(grFace);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001846
1847 if (GR_GL_FRONT_AND_BACK == glFace) {
1848 // we call the combined func just in case separate stencil is not
1849 // supported.
1850 GR_GL_CALL(gl, StencilFunc(glFunc, ref, mask));
1851 GR_GL_CALL(gl, StencilMask(writeMask));
1852 GR_GL_CALL(gl, StencilOp(glFailOp, glPassOp, glPassOp));
1853 } else {
1854 GR_GL_CALL(gl, StencilFuncSeparate(glFace, glFunc, ref, mask));
1855 GR_GL_CALL(gl, StencilMaskSeparate(glFace, writeMask));
1856 GR_GL_CALL(gl, StencilOpSeparate(glFace, glFailOp, glPassOp, glPassOp));
1857 }
1858}
1859}
bsalomon@google.comd302f142011-03-03 13:54:13 +00001860
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001861void GrGpuGL::flushStencil(DrawType type) {
1862 if (kStencilPath_DrawType == type) {
1863 GrAssert(!fStencilSettings.isTwoSided());
1864 // Just the func, ref, and mask is set here. The op and write mask are params to the call
1865 // that draws the path to the SB (glStencilFillPath)
1866 GrGLenum func =
1867 gr_to_gl_stencil_func(fStencilSettings.func(GrStencilSettings::kFront_Face));
1868 GL_CALL(PathStencilFunc(func,
1869 fStencilSettings.funcRef(GrStencilSettings::kFront_Face),
1870 fStencilSettings.funcMask(GrStencilSettings::kFront_Face)));
1871 } else if (fHWStencilSettings != fStencilSettings) {
1872 if (fStencilSettings.isDisabled()) {
1873 if (kNo_TriState != fHWStencilTestEnabled) {
1874 GL_CALL(Disable(GR_GL_STENCIL_TEST));
1875 fHWStencilTestEnabled = kNo_TriState;
1876 }
1877 } else {
1878 if (kYes_TriState != fHWStencilTestEnabled) {
1879 GL_CALL(Enable(GR_GL_STENCIL_TEST));
1880 fHWStencilTestEnabled = kYes_TriState;
1881 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001882 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001883 if (!fStencilSettings.isDisabled()) {
bsalomon@google.comf6601872012-08-28 21:11:35 +00001884 if (this->getCaps().twoSidedStencilSupport()) {
bsalomon@google.com411dad02012-06-05 20:24:20 +00001885 set_gl_stencil(this->glInterface(),
bsalomon@google.coma3201942012-06-21 19:58:20 +00001886 fStencilSettings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00001887 GR_GL_FRONT,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001888 GrStencilSettings::kFront_Face);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001889 set_gl_stencil(this->glInterface(),
bsalomon@google.coma3201942012-06-21 19:58:20 +00001890 fStencilSettings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00001891 GR_GL_BACK,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001892 GrStencilSettings::kBack_Face);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001893 } else {
bsalomon@google.com411dad02012-06-05 20:24:20 +00001894 set_gl_stencil(this->glInterface(),
bsalomon@google.coma3201942012-06-21 19:58:20 +00001895 fStencilSettings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00001896 GR_GL_FRONT_AND_BACK,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001897 GrStencilSettings::kFront_Face);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001898 }
1899 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001900 fHWStencilSettings = fStencilSettings;
reed@google.comac10a2d2010-12-22 21:39:39 +00001901 }
1902}
1903
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001904void GrGpuGL::flushAAState(DrawType type) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001905 const GrRenderTarget* rt = this->getDrawState().getRenderTarget();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001906 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001907 // ES doesn't support toggling GL_MULTISAMPLE and doesn't have
1908 // smooth lines.
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001909 // we prefer smooth lines over multisampled lines
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00001910 bool smoothLines = false;
1911
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001912 if (kDrawLines_DrawType == type) {
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00001913 smoothLines = this->willUseHWAALines();
1914 if (smoothLines) {
1915 if (kYes_TriState != fHWAAState.fSmoothLineEnabled) {
1916 GL_CALL(Enable(GR_GL_LINE_SMOOTH));
1917 fHWAAState.fSmoothLineEnabled = kYes_TriState;
1918 // must disable msaa to use line smoothing
1919 if (rt->isMultisampled() &&
1920 kNo_TriState != fHWAAState.fMSAAEnabled) {
1921 GL_CALL(Disable(GR_GL_MULTISAMPLE));
1922 fHWAAState.fMSAAEnabled = kNo_TriState;
1923 }
1924 }
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001925 } else {
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00001926 if (kNo_TriState != fHWAAState.fSmoothLineEnabled) {
1927 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
1928 fHWAAState.fSmoothLineEnabled = kNo_TriState;
1929 }
1930 }
1931 }
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001932 if (!smoothLines && rt->isMultisampled()) {
1933 // FIXME: GL_NV_pr doesn't seem to like MSAA disabled. The paths
1934 // convex hulls of each segment appear to get filled.
1935 bool enableMSAA = kStencilPath_DrawType == type ||
1936 this->getDrawState().isHWAntialiasState();
1937 if (enableMSAA) {
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00001938 if (kYes_TriState != fHWAAState.fMSAAEnabled) {
1939 GL_CALL(Enable(GR_GL_MULTISAMPLE));
1940 fHWAAState.fMSAAEnabled = kYes_TriState;
1941 }
1942 } else {
1943 if (kNo_TriState != fHWAAState.fMSAAEnabled) {
1944 GL_CALL(Disable(GR_GL_MULTISAMPLE));
1945 fHWAAState.fMSAAEnabled = kNo_TriState;
1946 }
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001947 }
1948 }
1949 }
1950}
1951
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00001952void GrGpuGL::flushBlend(bool isLines,
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001953 GrBlendCoeff srcCoeff,
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001954 GrBlendCoeff dstCoeff) {
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00001955 if (isLines && this->willUseHWAALines()) {
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001956 if (kYes_TriState != fHWBlendState.fEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001957 GL_CALL(Enable(GR_GL_BLEND));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001958 fHWBlendState.fEnabled = kYes_TriState;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001959 }
bsalomon@google.com47059542012-06-06 20:51:20 +00001960 if (kSA_GrBlendCoeff != fHWBlendState.fSrcCoeff ||
1961 kISA_GrBlendCoeff != fHWBlendState.fDstCoeff) {
1962 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[kSA_GrBlendCoeff],
1963 gXfermodeCoeff2Blend[kISA_GrBlendCoeff]));
1964 fHWBlendState.fSrcCoeff = kSA_GrBlendCoeff;
1965 fHWBlendState.fDstCoeff = kISA_GrBlendCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001966 }
1967 } else {
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001968 // any optimization to disable blending should
1969 // have already been applied and tweaked the coeffs
1970 // to (1, 0).
bsalomon@google.com47059542012-06-06 20:51:20 +00001971 bool blendOff = kOne_GrBlendCoeff == srcCoeff &&
1972 kZero_GrBlendCoeff == dstCoeff;
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001973 if (blendOff) {
1974 if (kNo_TriState != fHWBlendState.fEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001975 GL_CALL(Disable(GR_GL_BLEND));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001976 fHWBlendState.fEnabled = kNo_TriState;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001977 }
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001978 } else {
1979 if (kYes_TriState != fHWBlendState.fEnabled) {
1980 GL_CALL(Enable(GR_GL_BLEND));
1981 fHWBlendState.fEnabled = kYes_TriState;
1982 }
1983 if (fHWBlendState.fSrcCoeff != srcCoeff ||
1984 fHWBlendState.fDstCoeff != dstCoeff) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001985 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
1986 gXfermodeCoeff2Blend[dstCoeff]));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001987 fHWBlendState.fSrcCoeff = srcCoeff;
1988 fHWBlendState.fDstCoeff = dstCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001989 }
bsalomon@google.coma5d056a2012-03-27 15:59:58 +00001990 GrColor blendConst = this->getDrawState().getBlendConstant();
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001991 if ((BlendCoeffReferencesConstant(srcCoeff) ||
1992 BlendCoeffReferencesConstant(dstCoeff)) &&
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001993 (!fHWBlendState.fConstColorValid ||
1994 fHWBlendState.fConstColor != blendConst)) {
bsalomon@google.com75347472012-09-17 17:23:21 +00001995 GrGLfloat c[4];
1996 GrColorToRGBAFloat(blendConst, c);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001997 GL_CALL(BlendColor(c[0], c[1], c[2], c[3]));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001998 fHWBlendState.fConstColor = blendConst;
1999 fHWBlendState.fConstColorValid = true;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002000 }
2001 }
2002 }
2003}
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002004namespace {
2005
bsalomon@google.com6d003d12012-09-11 15:45:20 +00002006inline void set_tex_swizzle(GrGLenum swizzle[4], const GrGLInterface* gl) {
bsalomon@google.com4d063de2012-05-31 17:59:23 +00002007 GR_GL_CALL(gl, TexParameteriv(GR_GL_TEXTURE_2D,
2008 GR_GL_TEXTURE_SWIZZLE_RGBA,
2009 reinterpret_cast<const GrGLint*>(swizzle)));
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002010}
bsalomon@google.comb8670992012-07-25 21:27:09 +00002011
bsalomon@google.com6c76d242012-09-07 16:52:24 +00002012inline GrGLenum tile_to_gl_wrap(SkShader::TileMode tm) {
bsalomon@google.comb8670992012-07-25 21:27:09 +00002013 static const GrGLenum gWrapModes[] = {
2014 GR_GL_CLAMP_TO_EDGE,
2015 GR_GL_REPEAT,
2016 GR_GL_MIRRORED_REPEAT
2017 };
2018 GrAssert((unsigned) tm <= SK_ARRAY_COUNT(gWrapModes));
2019 GR_STATIC_ASSERT(0 == SkShader::kClamp_TileMode);
2020 GR_STATIC_ASSERT(1 == SkShader::kRepeat_TileMode);
2021 GR_STATIC_ASSERT(2 == SkShader::kMirror_TileMode);
2022 return gWrapModes[tm];
2023}
2024
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002025}
2026
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002027void GrGpuGL::bindTexture(int unitIdx, const GrTextureParams& params, GrGLTexture* texture) {
2028 GrAssert(NULL != texture);
tomhudson@google.comd0c1a062012-07-12 17:23:52 +00002029
bsalomon@google.comb8670992012-07-25 21:27:09 +00002030 // If we created a rt/tex and rendered to it without using a texture and now we're texturing
2031 // 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 +00002032 // out of the "last != next" check.
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002033 GrGLRenderTarget* texRT = static_cast<GrGLRenderTarget*>(texture->asRenderTarget());
bsalomon@google.com4c883782012-06-04 19:05:11 +00002034 if (NULL != texRT) {
2035 this->onResolveRenderTarget(texRT);
2036 }
2037
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002038 if (fHWBoundTextures[unitIdx] != texture) {
2039 this->setTextureUnit(unitIdx);
2040 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, texture->textureID()));
2041 fHWBoundTextures[unitIdx] = texture;
bsalomon@google.com4c883782012-06-04 19:05:11 +00002042 }
2043
bsalomon@google.com4c883782012-06-04 19:05:11 +00002044 ResetTimestamp timestamp;
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002045 const GrGLTexture::TexParams& oldTexParams = texture->getCachedTexParams(&timestamp);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002046 bool setAll = timestamp < this->getResetTimestamp();
2047 GrGLTexture::TexParams newTexParams;
2048
bsalomon@google.comb8670992012-07-25 21:27:09 +00002049 newTexParams.fFilter = params.isBilerp() ? GR_GL_LINEAR : GR_GL_NEAREST;
bsalomon@google.com4c883782012-06-04 19:05:11 +00002050
bsalomon@google.comb8670992012-07-25 21:27:09 +00002051 newTexParams.fWrapS = tile_to_gl_wrap(params.getTileModeX());
2052 newTexParams.fWrapT = tile_to_gl_wrap(params.getTileModeY());
bsalomon@google.com4c883782012-06-04 19:05:11 +00002053 memcpy(newTexParams.fSwizzleRGBA,
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002054 GrGLShaderBuilder::GetTexParamSwizzle(texture->config(), this->glCaps()),
bsalomon@google.com4c883782012-06-04 19:05:11 +00002055 sizeof(newTexParams.fSwizzleRGBA));
2056 if (setAll || newTexParams.fFilter != oldTexParams.fFilter) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002057 this->setTextureUnit(unitIdx);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002058 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2059 GR_GL_TEXTURE_MAG_FILTER,
2060 newTexParams.fFilter));
2061 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2062 GR_GL_TEXTURE_MIN_FILTER,
2063 newTexParams.fFilter));
2064 }
2065 if (setAll || newTexParams.fWrapS != oldTexParams.fWrapS) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002066 this->setTextureUnit(unitIdx);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002067 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2068 GR_GL_TEXTURE_WRAP_S,
2069 newTexParams.fWrapS));
2070 }
2071 if (setAll || newTexParams.fWrapT != oldTexParams.fWrapT) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002072 this->setTextureUnit(unitIdx);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002073 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2074 GR_GL_TEXTURE_WRAP_T,
2075 newTexParams.fWrapT));
2076 }
2077 if (this->glCaps().textureSwizzleSupport() &&
2078 (setAll || memcmp(newTexParams.fSwizzleRGBA,
2079 oldTexParams.fSwizzleRGBA,
2080 sizeof(newTexParams.fSwizzleRGBA)))) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002081 this->setTextureUnit(unitIdx);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002082 set_tex_swizzle(newTexParams.fSwizzleRGBA,
2083 this->glInterface());
2084 }
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002085 texture->setCachedTexParams(newTexParams, this->getResetTimestamp());
bsalomon@google.com4c883782012-06-04 19:05:11 +00002086}
2087
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002088void GrGpuGL::flushMiscFixedFunctionState() {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002089
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002090 const GrDrawState& drawState = this->getDrawState();
reed@google.comac10a2d2010-12-22 21:39:39 +00002091
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002092 if (drawState.isDitherState()) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002093 if (kYes_TriState != fHWDitherEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002094 GL_CALL(Enable(GR_GL_DITHER));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002095 fHWDitherEnabled = kYes_TriState;
2096 }
2097 } else {
2098 if (kNo_TriState != fHWDitherEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002099 GL_CALL(Disable(GR_GL_DITHER));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002100 fHWDitherEnabled = kNo_TriState;
reed@google.comac10a2d2010-12-22 21:39:39 +00002101 }
2102 }
2103
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002104 if (drawState.isColorWriteDisabled()) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002105 if (kNo_TriState != fHWWriteToColor) {
2106 GL_CALL(ColorMask(GR_GL_FALSE, GR_GL_FALSE,
2107 GR_GL_FALSE, GR_GL_FALSE));
2108 fHWWriteToColor = kNo_TriState;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002109 }
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002110 } else {
2111 if (kYes_TriState != fHWWriteToColor) {
2112 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
2113 fHWWriteToColor = kYes_TriState;
2114 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00002115 }
2116
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002117 if (fHWDrawFace != drawState.getDrawFace()) {
bsalomon@google.coma5d056a2012-03-27 15:59:58 +00002118 switch (this->getDrawState().getDrawFace()) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002119 case GrDrawState::kCCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002120 GL_CALL(Enable(GR_GL_CULL_FACE));
2121 GL_CALL(CullFace(GR_GL_BACK));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002122 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002123 case GrDrawState::kCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002124 GL_CALL(Enable(GR_GL_CULL_FACE));
2125 GL_CALL(CullFace(GR_GL_FRONT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002126 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002127 case GrDrawState::kBoth_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002128 GL_CALL(Disable(GR_GL_CULL_FACE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002129 break;
2130 default:
2131 GrCrash("Unknown draw face.");
2132 }
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002133 fHWDrawFace = drawState.getDrawFace();
bsalomon@google.comd302f142011-03-03 13:54:13 +00002134 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002135}
2136
2137void GrGpuGL::notifyVertexBufferBind(const GrGLVertexBuffer* buffer) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002138 if (fHWGeometryState.fVertexBuffer != buffer) {
2139 fHWGeometryState.fArrayPtrsDirty = true;
2140 fHWGeometryState.fVertexBuffer = buffer;
2141 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002142}
2143
2144void GrGpuGL::notifyVertexBufferDelete(const GrGLVertexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002145 if (fHWGeometryState.fVertexBuffer == buffer) {
2146 // deleting bound buffer does implied bind to 0
2147 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002148 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00002149 }
2150}
2151
2152void GrGpuGL::notifyIndexBufferBind(const GrGLIndexBuffer* buffer) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002153 fHWGeometryState.fIndexBuffer = buffer;
reed@google.comac10a2d2010-12-22 21:39:39 +00002154}
2155
2156void GrGpuGL::notifyIndexBufferDelete(const GrGLIndexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002157 if (fHWGeometryState.fIndexBuffer == buffer) {
2158 // deleting bound buffer does implied bind to 0
2159 fHWGeometryState.fIndexBuffer = NULL;
2160 }
2161}
2162
reed@google.comac10a2d2010-12-22 21:39:39 +00002163void GrGpuGL::notifyRenderTargetDelete(GrRenderTarget* renderTarget) {
2164 GrAssert(NULL != renderTarget);
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002165 if (fHWBoundRenderTarget == renderTarget) {
2166 fHWBoundRenderTarget = NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +00002167 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002168}
2169
2170void GrGpuGL::notifyTextureDelete(GrGLTexture* texture) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002171 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002172 if (fHWBoundTextures[s] == texture) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002173 // deleting bound texture does implied bind to 0
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002174 fHWBoundTextures[s] = NULL;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002175 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002176 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002177}
2178
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002179bool GrGpuGL::configToGLFormats(GrPixelConfig config,
2180 bool getSizedInternalFormat,
2181 GrGLenum* internalFormat,
2182 GrGLenum* externalFormat,
2183 GrGLenum* externalType) {
2184 GrGLenum dontCare;
2185 if (NULL == internalFormat) {
2186 internalFormat = &dontCare;
2187 }
2188 if (NULL == externalFormat) {
2189 externalFormat = &dontCare;
2190 }
2191 if (NULL == externalType) {
2192 externalType = &dontCare;
2193 }
2194
reed@google.comac10a2d2010-12-22 21:39:39 +00002195 switch (config) {
bsalomon@google.com0342a852012-08-20 19:22:38 +00002196 case kRGBA_8888_GrPixelConfig:
bsalomon@google.comc4364992011-11-07 15:54:49 +00002197 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002198 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002199 if (getSizedInternalFormat) {
2200 *internalFormat = GR_GL_RGBA8;
2201 } else {
2202 *internalFormat = GR_GL_RGBA;
2203 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002204 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.comc4364992011-11-07 15:54:49 +00002205 break;
bsalomon@google.com0342a852012-08-20 19:22:38 +00002206 case kBGRA_8888_GrPixelConfig:
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002207 if (!this->glCaps().bgraFormatSupport()) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002208 return false;
2209 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002210 if (this->glCaps().bgraIsInternalFormat()) {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002211 if (getSizedInternalFormat) {
2212 *internalFormat = GR_GL_BGRA8;
2213 } else {
2214 *internalFormat = GR_GL_BGRA;
2215 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002216 } else {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002217 if (getSizedInternalFormat) {
2218 *internalFormat = GR_GL_RGBA8;
2219 } else {
2220 *internalFormat = GR_GL_RGBA;
2221 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002222 }
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002223 *externalFormat = GR_GL_BGRA;
bsalomon@google.com6f379512011-11-16 20:36:03 +00002224 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002225 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002226 case kRGB_565_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002227 *internalFormat = GR_GL_RGB;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002228 *externalFormat = GR_GL_RGB;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002229 if (getSizedInternalFormat) {
2230 if (this->glBinding() == kDesktop_GrGLBinding) {
2231 return false;
2232 } else {
2233 *internalFormat = GR_GL_RGB565;
2234 }
2235 } else {
2236 *internalFormat = GR_GL_RGB;
2237 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002238 *externalType = GR_GL_UNSIGNED_SHORT_5_6_5;
reed@google.comac10a2d2010-12-22 21:39:39 +00002239 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002240 case kRGBA_4444_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002241 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002242 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002243 if (getSizedInternalFormat) {
2244 *internalFormat = GR_GL_RGBA4;
2245 } else {
2246 *internalFormat = GR_GL_RGBA;
2247 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002248 *externalType = GR_GL_UNSIGNED_SHORT_4_4_4_4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002249 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002250 case kIndex_8_GrPixelConfig:
bsalomon@google.comf6601872012-08-28 21:11:35 +00002251 if (this->getCaps().eightBitPaletteSupport()) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002252 *internalFormat = GR_GL_PALETTE8_RGBA8;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002253 // glCompressedTexImage doesn't take external params
2254 *externalFormat = GR_GL_PALETTE8_RGBA8;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002255 // no sized/unsized internal format distinction here
2256 *internalFormat = GR_GL_PALETTE8_RGBA8;
2257 // unused with CompressedTexImage
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002258 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002259 } else {
2260 return false;
2261 }
2262 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002263 case kAlpha_8_GrPixelConfig:
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002264 if (this->glCaps().textureRedSupport()) {
2265 *internalFormat = GR_GL_RED;
2266 *externalFormat = GR_GL_RED;
2267 if (getSizedInternalFormat) {
2268 *internalFormat = GR_GL_R8;
2269 } else {
2270 *internalFormat = GR_GL_RED;
2271 }
2272 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002273 } else {
2274 *internalFormat = GR_GL_ALPHA;
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002275 *externalFormat = GR_GL_ALPHA;
2276 if (getSizedInternalFormat) {
2277 *internalFormat = GR_GL_ALPHA8;
2278 } else {
2279 *internalFormat = GR_GL_ALPHA;
2280 }
2281 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002282 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002283 break;
2284 default:
2285 return false;
2286 }
2287 return true;
2288}
2289
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002290void GrGpuGL::setTextureUnit(int unit) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002291 GrAssert(unit >= 0 && unit < GrDrawState::kNumStages);
bsalomon@google.com49209392012-06-05 15:13:46 +00002292 if (fHWActiveTextureUnitIdx != unit) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002293 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com49209392012-06-05 15:13:46 +00002294 fHWActiveTextureUnitIdx = unit;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002295 }
2296}
bsalomon@google.com316f99232011-01-13 21:28:12 +00002297
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002298void GrGpuGL::setSpareTextureUnit() {
bsalomon@google.com49209392012-06-05 15:13:46 +00002299 if (fHWActiveTextureUnitIdx != (GR_GL_TEXTURE0 + SPARE_TEX_UNIT)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002300 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + SPARE_TEX_UNIT));
bsalomon@google.com49209392012-06-05 15:13:46 +00002301 fHWActiveTextureUnitIdx = SPARE_TEX_UNIT;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002302 }
2303}
2304
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002305void GrGpuGL::setBuffers(bool indexed,
2306 int* extraVertexOffset,
2307 int* extraIndexOffset) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002308
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002309 GrAssert(NULL != extraVertexOffset);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002310
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002311 const GeometryPoolState& geoPoolState = this->getGeomPoolState();
2312
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002313 GrGLVertexBuffer* vbuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002314 switch (this->getGeomSrc().fVertexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002315 case kBuffer_GeometrySrcType:
2316 *extraVertexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002317 vbuf = (GrGLVertexBuffer*) this->getGeomSrc().fVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002318 break;
2319 case kArray_GeometrySrcType:
2320 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002321 this->finalizeReservedVertices();
2322 *extraVertexOffset = geoPoolState.fPoolStartVertex;
2323 vbuf = (GrGLVertexBuffer*) geoPoolState.fPoolVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002324 break;
2325 default:
2326 vbuf = NULL; // suppress warning
2327 GrCrash("Unknown geometry src type!");
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002328 }
2329
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002330 GrAssert(NULL != vbuf);
2331 GrAssert(!vbuf->isLocked());
2332 if (fHWGeometryState.fVertexBuffer != vbuf) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002333 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, vbuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002334 fHWGeometryState.fArrayPtrsDirty = true;
2335 fHWGeometryState.fVertexBuffer = vbuf;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002336 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002337
2338 if (indexed) {
2339 GrAssert(NULL != extraIndexOffset);
2340
2341 GrGLIndexBuffer* ibuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002342 switch (this->getGeomSrc().fIndexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002343 case kBuffer_GeometrySrcType:
2344 *extraIndexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002345 ibuf = (GrGLIndexBuffer*)this->getGeomSrc().fIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002346 break;
2347 case kArray_GeometrySrcType:
2348 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002349 this->finalizeReservedIndices();
2350 *extraIndexOffset = geoPoolState.fPoolStartIndex;
2351 ibuf = (GrGLIndexBuffer*) geoPoolState.fPoolIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002352 break;
2353 default:
2354 ibuf = NULL; // suppress warning
2355 GrCrash("Unknown geometry src type!");
2356 }
2357
2358 GrAssert(NULL != ibuf);
2359 GrAssert(!ibuf->isLocked());
2360 if (fHWGeometryState.fIndexBuffer != ibuf) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002361 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, ibuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002362 fHWGeometryState.fIndexBuffer = ibuf;
2363 }
2364 }
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002365}