blob: abf5ac937bd9c549ccf06129fb2eb22e0ed6723a [file] [log] [blame]
reed@google.comac10a2d2010-12-22 21:39:39 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
reed@google.comac10a2d2010-12-22 21:39:39 +00006 */
7
epoger@google.comec3ed6a2011-07-28 14:26:00 +00008
reed@google.comac10a2d2010-12-22 21:39:39 +00009#include "GrGpuGL.h"
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000010#include "GrGLStencilBuffer.h"
bsalomon@google.com64aef2b2012-06-11 15:36:13 +000011#include "GrGLPath.h"
bsalomon@google.com6d003d12012-09-11 15:45:20 +000012#include "GrGLShaderBuilder.h"
bsalomon@google.coma3201942012-06-21 19:58:20 +000013#include "GrTemplates.h"
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +000014#include "GrTypes.h"
bsalomon@google.com3582bf92011-06-30 21:32:31 +000015#include "SkTemplates.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000016
caryclark@google.comcf6285b2012-06-06 12:09:01 +000017static const GrGLuint GR_MAX_GLUINT = ~0U;
twiz@google.com0f31ca72011-03-18 17:38:11 +000018static const GrGLint GR_INVAL_GLINT = ~0;
reed@google.comac10a2d2010-12-22 21:39:39 +000019
bsalomon@google.com0b77d682011-08-19 13:28:54 +000020#define GL_CALL(X) GR_GL_CALL(this->glInterface(), X)
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +000021#define GL_CALL_RET(RET, X) GR_GL_CALL_RET(this->glInterface(), RET, X)
bsalomon@google.com0b77d682011-08-19 13:28:54 +000022
bsalomon@google.com316f99232011-01-13 21:28:12 +000023// we use a spare texture unit to avoid
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000024// mucking with the state of any of the stages.
tomhudson@google.com93813632011-10-27 20:21:16 +000025static const int SPARE_TEX_UNIT = GrDrawState::kNumStages;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000026
reed@google.comac10a2d2010-12-22 21:39:39 +000027#define SKIP_CACHE_CHECK true
28
bsalomon@google.com4f3c2532012-01-19 16:16:52 +000029#if GR_GL_CHECK_ALLOC_WITH_GET_ERROR
30 #define CLEAR_ERROR_BEFORE_ALLOC(iface) GrGLClearErr(iface)
31 #define GL_ALLOC_CALL(iface, call) GR_GL_CALL_NOERRCHECK(iface, call)
32 #define CHECK_ALLOC_ERROR(iface) GR_GL_GET_ERROR(iface)
rmistry@google.comfbfcd562012-08-23 18:09:54 +000033#else
bsalomon@google.com4f3c2532012-01-19 16:16:52 +000034 #define CLEAR_ERROR_BEFORE_ALLOC(iface)
35 #define GL_ALLOC_CALL(iface, call) GR_GL_CALL(iface, call)
36 #define CHECK_ALLOC_ERROR(iface) GR_GL_NO_ERROR
37#endif
38
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +000039
40///////////////////////////////////////////////////////////////////////////////
41
twiz@google.com0f31ca72011-03-18 17:38:11 +000042static const GrGLenum gXfermodeCoeff2Blend[] = {
43 GR_GL_ZERO,
44 GR_GL_ONE,
45 GR_GL_SRC_COLOR,
46 GR_GL_ONE_MINUS_SRC_COLOR,
47 GR_GL_DST_COLOR,
48 GR_GL_ONE_MINUS_DST_COLOR,
49 GR_GL_SRC_ALPHA,
50 GR_GL_ONE_MINUS_SRC_ALPHA,
51 GR_GL_DST_ALPHA,
52 GR_GL_ONE_MINUS_DST_ALPHA,
53 GR_GL_CONSTANT_COLOR,
54 GR_GL_ONE_MINUS_CONSTANT_COLOR,
55 GR_GL_CONSTANT_ALPHA,
56 GR_GL_ONE_MINUS_CONSTANT_ALPHA,
bsalomon@google.com271cffc2011-05-20 14:13:56 +000057
58 // extended blend coeffs
59 GR_GL_SRC1_COLOR,
60 GR_GL_ONE_MINUS_SRC1_COLOR,
61 GR_GL_SRC1_ALPHA,
62 GR_GL_ONE_MINUS_SRC1_ALPHA,
reed@google.comac10a2d2010-12-22 21:39:39 +000063};
64
bsalomon@google.com271cffc2011-05-20 14:13:56 +000065bool GrGpuGL::BlendCoeffReferencesConstant(GrBlendCoeff coeff) {
bsalomon@google.com080773c2011-03-15 19:09:25 +000066 static const bool gCoeffReferencesBlendConst[] = {
67 false,
68 false,
69 false,
70 false,
71 false,
72 false,
73 false,
74 false,
75 false,
76 false,
77 true,
78 true,
79 true,
80 true,
bsalomon@google.com271cffc2011-05-20 14:13:56 +000081
82 // extended blend coeffs
83 false,
84 false,
85 false,
86 false,
bsalomon@google.com080773c2011-03-15 19:09:25 +000087 };
88 return gCoeffReferencesBlendConst[coeff];
bsalomon@google.com47059542012-06-06 20:51:20 +000089 GR_STATIC_ASSERT(kTotalGrBlendCoeffCount ==
90 GR_ARRAY_COUNT(gCoeffReferencesBlendConst));
bsalomon@google.com271cffc2011-05-20 14:13:56 +000091
bsalomon@google.com47059542012-06-06 20:51:20 +000092 GR_STATIC_ASSERT(0 == kZero_GrBlendCoeff);
93 GR_STATIC_ASSERT(1 == kOne_GrBlendCoeff);
94 GR_STATIC_ASSERT(2 == kSC_GrBlendCoeff);
95 GR_STATIC_ASSERT(3 == kISC_GrBlendCoeff);
96 GR_STATIC_ASSERT(4 == kDC_GrBlendCoeff);
97 GR_STATIC_ASSERT(5 == kIDC_GrBlendCoeff);
98 GR_STATIC_ASSERT(6 == kSA_GrBlendCoeff);
99 GR_STATIC_ASSERT(7 == kISA_GrBlendCoeff);
100 GR_STATIC_ASSERT(8 == kDA_GrBlendCoeff);
101 GR_STATIC_ASSERT(9 == kIDA_GrBlendCoeff);
102 GR_STATIC_ASSERT(10 == kConstC_GrBlendCoeff);
103 GR_STATIC_ASSERT(11 == kIConstC_GrBlendCoeff);
104 GR_STATIC_ASSERT(12 == kConstA_GrBlendCoeff);
105 GR_STATIC_ASSERT(13 == kIConstA_GrBlendCoeff);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000106
bsalomon@google.com47059542012-06-06 20:51:20 +0000107 GR_STATIC_ASSERT(14 == kS2C_GrBlendCoeff);
108 GR_STATIC_ASSERT(15 == kIS2C_GrBlendCoeff);
109 GR_STATIC_ASSERT(16 == kS2A_GrBlendCoeff);
110 GR_STATIC_ASSERT(17 == kIS2A_GrBlendCoeff);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000111
112 // assertion for gXfermodeCoeff2Blend have to be in GrGpu scope
bsalomon@google.com47059542012-06-06 20:51:20 +0000113 GR_STATIC_ASSERT(kTotalGrBlendCoeffCount ==
114 GR_ARRAY_COUNT(gXfermodeCoeff2Blend));
bsalomon@google.com080773c2011-03-15 19:09:25 +0000115}
116
reed@google.comac10a2d2010-12-22 21:39:39 +0000117///////////////////////////////////////////////////////////////////////////////
118
rileya@google.come38160c2012-07-03 18:03:04 +0000119static bool gPrintStartupSpew;
bsalomon@google.com42ab7ea2011-01-19 17:19:40 +0000120
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000121static bool fbo_test(const GrGLInterface* gl, int w, int h) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000122
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000123 GR_GL_CALL(gl, ActiveTexture(GR_GL_TEXTURE0 + SPARE_TEX_UNIT));
bsalomon@google.com316f99232011-01-13 21:28:12 +0000124
twiz@google.com0f31ca72011-03-18 17:38:11 +0000125 GrGLuint testFBO;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000126 GR_GL_CALL(gl, GenFramebuffers(1, &testFBO));
127 GR_GL_CALL(gl, BindFramebuffer(GR_GL_FRAMEBUFFER, testFBO));
twiz@google.com0f31ca72011-03-18 17:38:11 +0000128 GrGLuint testRTTex;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000129 GR_GL_CALL(gl, GenTextures(1, &testRTTex));
130 GR_GL_CALL(gl, BindTexture(GR_GL_TEXTURE_2D, testRTTex));
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +0000131 // some implementations require texture to be mip-map complete before
132 // FBO with level 0 bound as color attachment will be framebuffer complete.
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000133 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000134 GR_GL_TEXTURE_MIN_FILTER,
135 GR_GL_NEAREST));
136 GR_GL_CALL(gl, TexImage2D(GR_GL_TEXTURE_2D, 0, GR_GL_RGBA, w, h,
137 0, GR_GL_RGBA, GR_GL_UNSIGNED_BYTE, NULL));
138 GR_GL_CALL(gl, BindTexture(GR_GL_TEXTURE_2D, 0));
139 GR_GL_CALL(gl, FramebufferTexture2D(GR_GL_FRAMEBUFFER,
140 GR_GL_COLOR_ATTACHMENT0,
141 GR_GL_TEXTURE_2D, testRTTex, 0));
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000142 GrGLenum status;
143 GR_GL_CALL_RET(gl, status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000144 GR_GL_CALL(gl, DeleteFramebuffers(1, &testFBO));
145 GR_GL_CALL(gl, DeleteTextures(1, &testRTTex));
bsalomon@google.com316f99232011-01-13 21:28:12 +0000146
bsalomon@google.comc312bf92011-03-21 21:10:33 +0000147 return status == GR_GL_FRAMEBUFFER_COMPLETE;
reed@google.comac10a2d2010-12-22 21:39:39 +0000148}
149
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000150GrGpuGL::GrGpuGL(const GrGLContextInfo& ctxInfo) : fGLContextInfo(ctxInfo) {
151
152 GrAssert(ctxInfo.isInitialized());
tomhudson@google.com747bf292011-06-14 18:16:52 +0000153
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000154 fillInConfigRenderableTable();
155
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000156 fPrintedCaps = false;
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000157
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000158 GrGLClearErr(fGLContextInfo.interface());
reed@google.comac10a2d2010-12-22 21:39:39 +0000159
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000160 if (gPrintStartupSpew) {
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000161 const GrGLubyte* ext;
162 GL_CALL_RET(ext, GetString(GR_GL_EXTENSIONS));
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000163 const GrGLubyte* vendor;
164 const GrGLubyte* renderer;
165 const GrGLubyte* version;
166 GL_CALL_RET(vendor, GetString(GR_GL_VENDOR));
167 GL_CALL_RET(renderer, GetString(GR_GL_RENDERER));
168 GL_CALL_RET(version, GetString(GR_GL_VERSION));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000169 GrPrintf("------------------------- create GrGpuGL %p --------------\n",
170 this);
171 GrPrintf("------ VENDOR %s\n", vendor);
172 GrPrintf("------ RENDERER %s\n", renderer);
173 GrPrintf("------ VERSION %s\n", version);
174 GrPrintf("------ EXTENSIONS\n %s \n", ext);
175 }
176
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000177 this->initCaps();
tomhudson@google.com747bf292011-06-14 18:16:52 +0000178
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000179 fProgramCache = SkNEW_ARGS(ProgramCache, (this->glContextInfo()));
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000180
bsalomon@google.com880b8fc2013-02-19 20:17:28 +0000181 fHWGeometryState.setMaxAttribArrays(this->glCaps().maxVertexAttributes());
182
bsalomon@google.comfe676522011-06-17 18:12:21 +0000183 fLastSuccessfulStencilFmtIdx = 0;
caryclark@google.comcf6285b2012-06-06 12:09:01 +0000184 if (false) { // avoid bit rot, suppress warning
185 fbo_test(this->glInterface(), 0, 0);
186 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000187}
188
189GrGpuGL::~GrGpuGL() {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000190 if (0 != fHWProgramID) {
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000191 // detach the current program so there is no confusion on OpenGL's part
192 // that we want it to be deleted
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +0000193 GrAssert(fHWProgramID == fCurrentProgram->programID());
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000194 GL_CALL(UseProgram(0));
195 }
196
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000197 delete fProgramCache;
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000198
bsalomon@google.com4a018bb2011-10-28 19:50:21 +0000199 // This must be called by before the GrDrawTarget destructor
200 this->releaseGeometry();
bsalomon@google.com15b11df2011-09-16 21:18:29 +0000201 // This subclass must do this before the base class destructor runs
202 // since we will unref the GrGLInterface.
203 this->releaseResources();
reed@google.comac10a2d2010-12-22 21:39:39 +0000204}
205
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000206///////////////////////////////////////////////////////////////////////////////
207
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000208void GrGpuGL::initCaps() {
209 GrGLint maxTextureUnits;
210 // check FS and fixed-function texture unit limits
211 // we only use textures in the fragment stage currently.
212 // checks are > to make sure we have a spare unit.
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000213 const GrGLInterface* gl = this->glInterface();
214 GR_GL_GetIntegerv(gl, GR_GL_MAX_TEXTURE_IMAGE_UNITS, &maxTextureUnits);
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000215 GrAssert(maxTextureUnits > GrDrawState::kNumStages);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000216
bsalomon@google.comf6601872012-08-28 21:11:35 +0000217 CapsInternals* caps = this->capsInternals();
218
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000219 GrGLint numFormats;
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000220 GR_GL_GetIntegerv(gl, GR_GL_NUM_COMPRESSED_TEXTURE_FORMATS, &numFormats);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000221 SkAutoSTMalloc<10, GrGLint> formats(numFormats);
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000222 GR_GL_GetIntegerv(gl, GR_GL_COMPRESSED_TEXTURE_FORMATS, formats);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000223 for (int i = 0; i < numFormats; ++i) {
224 if (formats[i] == GR_GL_PALETTE8_RGBA8) {
bsalomon@google.comf6601872012-08-28 21:11:35 +0000225 caps->f8BitPaletteSupport = true;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000226 break;
227 }
228 }
229
230 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000231 // we could also look for GL_ATI_separate_stencil extension or
232 // GL_EXT_stencil_two_side but they use different function signatures
233 // than GL2.0+ (and than each other).
bsalomon@google.comf6601872012-08-28 21:11:35 +0000234 caps->fTwoSidedStencilSupport = (this->glVersion() >= GR_GL_VER(2,0));
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000235 // supported on GL 1.4 and higher or by extension
bsalomon@google.comf6601872012-08-28 21:11:35 +0000236 caps->fStencilWrapOpsSupport = (this->glVersion() >= GR_GL_VER(1,4)) ||
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000237 this->hasExtension("GL_EXT_stencil_wrap");
238 } else {
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000239 // ES 2 has two sided stencil and stencil wrap
bsalomon@google.comf6601872012-08-28 21:11:35 +0000240 caps->fTwoSidedStencilSupport = true;
241 caps->fStencilWrapOpsSupport = true;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000242 }
243
244 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.comf6601872012-08-28 21:11:35 +0000245 caps->fBufferLockSupport = true; // we require VBO support and the desktop VBO
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000246 // extension includes glMapBuffer.
247 } else {
bsalomon@google.comf6601872012-08-28 21:11:35 +0000248 caps->fBufferLockSupport = this->hasExtension("GL_OES_mapbuffer");
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000249 }
250
251 if (kDesktop_GrGLBinding == this->glBinding()) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000252 if (this->glVersion() >= GR_GL_VER(2,0) ||
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000253 this->hasExtension("GL_ARB_texture_non_power_of_two")) {
bsalomon@google.comf6601872012-08-28 21:11:35 +0000254 caps->fNPOTTextureTileSupport = true;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000255 } else {
bsalomon@google.comf6601872012-08-28 21:11:35 +0000256 caps->fNPOTTextureTileSupport = false;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000257 }
258 } else {
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000259 // Unextended ES2 supports NPOT textures with clamp_to_edge and non-mip filters only
bsalomon@google.comf6601872012-08-28 21:11:35 +0000260 caps->fNPOTTextureTileSupport = this->hasExtension("GL_OES_texture_npot");
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000261 }
262
bsalomon@google.comf6601872012-08-28 21:11:35 +0000263 caps->fHWAALineSupport = (kDesktop_GrGLBinding == this->glBinding());
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000264
bsalomon@google.comf6601872012-08-28 21:11:35 +0000265 GR_GL_GetIntegerv(gl, GR_GL_MAX_TEXTURE_SIZE, &caps->fMaxTextureSize);
266 GR_GL_GetIntegerv(gl, GR_GL_MAX_RENDERBUFFER_SIZE, &caps->fMaxRenderTargetSize);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000267 // Our render targets are always created with textures as the color
268 // attachment, hence this min:
bsalomon@google.comf6601872012-08-28 21:11:35 +0000269 caps->fMaxRenderTargetSize = GrMin(caps->fMaxTextureSize, caps->fMaxRenderTargetSize);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000270
bsalomon@google.comf6601872012-08-28 21:11:35 +0000271 caps->fFSAASupport = GrGLCaps::kNone_MSFBOType != this->glCaps().msFBOType();
272 caps->fPathStencilingSupport = GR_GL_USE_NV_PATH_RENDERING &&
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000273 this->hasExtension("GL_NV_path_rendering");
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000274
275 // Enable supported shader-related caps
276 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.comf6601872012-08-28 21:11:35 +0000277 caps->fDualSourceBlendingSupport =
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000278 this->glVersion() >= GR_GL_VER(3,3) ||
279 this->hasExtension("GL_ARB_blend_func_extended");
bsalomon@google.comf6601872012-08-28 21:11:35 +0000280 caps->fShaderDerivativeSupport = true;
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000281 // we don't support GL_ARB_geometry_shader4, just GL 3.2+ GS
bsalomon@google.comf6601872012-08-28 21:11:35 +0000282 caps->fGeometryShaderSupport =
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000283 this->glVersion() >= GR_GL_VER(3,2) &&
284 this->glslGeneration() >= k150_GrGLSLGeneration;
285 } else {
bsalomon@google.comf6601872012-08-28 21:11:35 +0000286 caps->fShaderDerivativeSupport =
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000287 this->hasExtension("GL_OES_standard_derivatives");
288 }
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000289}
290
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000291void GrGpuGL::fillInConfigRenderableTable() {
292
293 // OpenGL < 3.0
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000294 // no support for render targets unless the GL_ARB_framebuffer_object
295 // extension is supported (in which case we get ALPHA, RED, RG, RGB,
296 // RGBA (ALPHA8, RGBA4, RGBA8) for OpenGL > 1.1). Note that we
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000297 // probably don't get R8 in this case.
298
299 // OpenGL 3.0
300 // base color renderable: ALPHA, RED, RG, RGB, and RGBA
301 // sized derivatives: ALPHA8, R8, RGBA4, RGBA8
302
303 // >= OpenGL 3.1
304 // base color renderable: RED, RG, RGB, and RGBA
305 // sized derivatives: R8, RGBA4, RGBA8
306 // if the GL_ARB_compatibility extension is supported then we get back
307 // support for GL_ALPHA and ALPHA8
308
309 // GL_EXT_bgra adds BGRA render targets to any version
310
311 // ES 2.0
312 // color renderable: RGBA4, RGB5_A1, RGB565
313 // GL_EXT_texture_rg adds support for R8 as a color render target
314 // GL_OES_rgb8_rgba8 and/or GL_ARM_rgba8 adds support for RGBA8
315 // GL_EXT_texture_format_BGRA8888 and/or GL_APPLE_texture_format_BGRA8888
316 // added BGRA support
317
318 if (kDesktop_GrGLBinding == this->glBinding()) {
319 // Post 3.0 we will get R8
320 // Prior to 3.0 we will get ALPHA8 (with GL_ARB_framebuffer_object)
321 if (this->glVersion() >= GR_GL_VER(3,0) ||
322 this->hasExtension("GL_ARB_framebuffer_object")) {
323 fConfigRenderSupport[kAlpha_8_GrPixelConfig] = true;
324 }
325 } else {
326 // On ES we can only hope for R8
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000327 fConfigRenderSupport[kAlpha_8_GrPixelConfig] =
robertphillips@google.com443e5a52012-04-30 20:01:21 +0000328 this->glCaps().textureRedSupport();
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000329 }
330
331 if (kDesktop_GrGLBinding != this->glBinding()) {
332 // only available in ES
333 fConfigRenderSupport[kRGB_565_GrPixelConfig] = true;
334 }
335
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000336 // Pre 3.0, Ganesh relies on either GL_ARB_framebuffer_object or
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000337 // GL_EXT_framebuffer_object for FBO support. Both of these
338 // allow RGBA4 render targets so this is always supported.
339 fConfigRenderSupport[kRGBA_4444_GrPixelConfig] = true;
340
341 if (this->glCaps().rgba8RenderbufferSupport()) {
bsalomon@google.com0342a852012-08-20 19:22:38 +0000342 fConfigRenderSupport[kRGBA_8888_GrPixelConfig] = true;
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000343 }
344
345 if (this->glCaps().bgraFormatSupport()) {
bsalomon@google.com0342a852012-08-20 19:22:38 +0000346 fConfigRenderSupport[kBGRA_8888_GrPixelConfig] = true;
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000347 }
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000348}
349
bsalomon@google.com9c680582013-02-06 18:17:50 +0000350namespace {
351GrPixelConfig preferred_pixel_ops_config(GrPixelConfig config) {
352 if (GR_GL_RGBA_8888_PIXEL_OPS_SLOW && kRGBA_8888_GrPixelConfig == config) {
353 return kBGRA_8888_GrPixelConfig;
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000354 } else {
355 return config;
356 }
357}
bsalomon@google.com9c680582013-02-06 18:17:50 +0000358}
359
360GrPixelConfig GrGpuGL::preferredReadPixelsConfig(GrPixelConfig config) const {
361 return preferred_pixel_ops_config(config);
362}
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000363
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000364GrPixelConfig GrGpuGL::preferredWritePixelsConfig(GrPixelConfig config) const {
bsalomon@google.com9c680582013-02-06 18:17:50 +0000365 return preferred_pixel_ops_config(config);
366}
367
368bool GrGpuGL::canWriteTexturePixels(const GrTexture* texture, GrPixelConfig srcConfig) const {
369 if (kIndex_8_GrPixelConfig == srcConfig || kIndex_8_GrPixelConfig == texture->config()) {
370 return false;
371 }
372 if (srcConfig != texture->config() && kES2_GrGLBinding == this->glBinding()) {
373 // In general ES2 requires the internal format of the texture and the format of the src
374 // pixels to match. However, It may or may not be possible to upload BGRA data to a RGBA
375 // texture. It depends upon which extension added BGRA. The Apple extension allows it
376 // (BGRA's internal format is RGBA) while the EXT extension does not (BGRA is its own
377 // internal format).
378 if (this->glCaps().bgraFormatSupport() &&
379 !this->glCaps().bgraIsInternalFormat() &&
380 kBGRA_8888_GrPixelConfig == srcConfig &&
381 kRGBA_8888_GrPixelConfig == texture->config()) {
382 return true;
383 } else {
384 return false;
385 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000386 } else {
bsalomon@google.com9c680582013-02-06 18:17:50 +0000387 return true;
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000388 }
389}
390
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000391bool GrGpuGL::fullReadPixelsIsFasterThanPartial() const {
392 return SkToBool(GR_GL_FULL_READPIXELS_FASTER_THAN_PARTIAL);
393}
394
bsalomon@google.com1bf1c212011-11-05 12:18:58 +0000395void GrGpuGL::onResetContext() {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000396 if (gPrintStartupSpew && !fPrintedCaps) {
397 fPrintedCaps = true;
398 this->getCaps().print();
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000399 this->glCaps().print();
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000400 }
401
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000402 // we don't use the zb at all
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000403 GL_CALL(Disable(GR_GL_DEPTH_TEST));
404 GL_CALL(DepthMask(GR_GL_FALSE));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000405
bsalomon@google.com978c8c62012-05-21 14:45:49 +0000406 fHWDrawFace = GrDrawState::kInvalid_DrawFace;
407 fHWDitherEnabled = kUnknown_TriState;
reed@google.comac10a2d2010-12-22 21:39:39 +0000408
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000409 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com469d0dd2012-05-21 20:14:29 +0000410 // Desktop-only state that we never change
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000411 GL_CALL(Disable(GR_GL_POINT_SMOOTH));
bsalomon@google.com469d0dd2012-05-21 20:14:29 +0000412 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
413 GL_CALL(Disable(GR_GL_POLYGON_SMOOTH));
414 GL_CALL(Disable(GR_GL_POLYGON_STIPPLE));
415 GL_CALL(Disable(GR_GL_COLOR_LOGIC_OP));
bsalomon@google.come76b7cc2012-06-18 12:47:06 +0000416 if (this->glCaps().imagingSupport()) {
417 GL_CALL(Disable(GR_GL_COLOR_TABLE));
418 }
bsalomon@google.com469d0dd2012-05-21 20:14:29 +0000419 GL_CALL(Disable(GR_GL_INDEX_LOGIC_OP));
420 GL_CALL(Disable(GR_GL_POLYGON_OFFSET_FILL));
421 // Since ES doesn't support glPointSize at all we always use the VS to
422 // set the point size
423 GL_CALL(Enable(GR_GL_VERTEX_PROGRAM_POINT_SIZE));
424
425 // We should set glPolygonMode(FRONT_AND_BACK,FILL) here, too. It isn't
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000426 // currently part of our gl interface. There are probably others as
bsalomon@google.com469d0dd2012-05-21 20:14:29 +0000427 // well.
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000428 }
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +0000429 fHWAAState.invalidate();
bsalomon@google.com978c8c62012-05-21 14:45:49 +0000430 fHWWriteToColor = kUnknown_TriState;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000431
reed@google.comac10a2d2010-12-22 21:39:39 +0000432 // we only ever use lines in hairline mode
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000433 GL_CALL(LineWidth(1));
reed@google.comac10a2d2010-12-22 21:39:39 +0000434
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000435 // invalid
bsalomon@google.com49209392012-06-05 15:13:46 +0000436 fHWActiveTextureUnitIdx = -1;
reed@google.comac10a2d2010-12-22 21:39:39 +0000437
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +0000438 fHWBlendState.invalidate();
bsalomon@google.com080773c2011-03-15 19:09:25 +0000439
tomhudson@google.com93813632011-10-27 20:21:16 +0000440 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.comc811ea32012-05-21 15:33:09 +0000441 fHWBoundTextures[s] = NULL;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000442 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000443
bsalomon@google.coma3201942012-06-21 19:58:20 +0000444 fHWScissorSettings.invalidate();
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000445
bsalomon@google.coma3201942012-06-21 19:58:20 +0000446 fHWViewport.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000447
bsalomon@google.com457b8a32012-05-21 21:19:58 +0000448 fHWStencilSettings.invalidate();
bsalomon@google.coma3201942012-06-21 19:58:20 +0000449 fHWStencilTestEnabled = kUnknown_TriState;
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000450
bsalomon@google.com880b8fc2013-02-19 20:17:28 +0000451 fHWGeometryState.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000452
bsalomon@google.comc811ea32012-05-21 15:33:09 +0000453 fHWBoundRenderTarget = NULL;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000454
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +0000455 fHWPathStencilMatrixState.invalidate();
bsalomon@google.comf6601872012-08-28 21:11:35 +0000456 if (fCaps.pathStencilingSupport()) {
bsalomon@google.com05a718c2012-06-29 14:01:53 +0000457 // we don't use the model view matrix.
458 GL_CALL(MatrixMode(GR_GL_MODELVIEW));
459 GL_CALL(LoadIdentity());
460 }
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000461
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000462 // we assume these values
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000463 if (this->glCaps().unpackRowLengthSupport()) {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000464 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
465 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000466 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000467 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
468 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000469 if (this->glCaps().unpackFlipYSupport()) {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000470 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
471 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000472 if (this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000473 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, GR_GL_FALSE));
474 }
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000475
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000476 fHWProgramID = 0;
bsalomon@google.com91207482013-02-12 21:45:24 +0000477 fSharedGLProgramState.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000478}
479
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000480namespace {
481
482GrSurfaceOrigin resolve_origin(GrSurfaceOrigin origin, bool renderTarget) {
483 // By default, GrRenderTargets are GL's normal orientation so that they
484 // can be drawn to by the outside world without the client having
485 // to render upside down.
486 if (kDefault_GrSurfaceOrigin == origin) {
487 return renderTarget ? kBottomLeft_GrSurfaceOrigin : kTopLeft_GrSurfaceOrigin;
488 } else {
489 return origin;
490 }
491}
492
493}
494
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000495GrTexture* GrGpuGL::onWrapBackendTexture(const GrBackendTextureDesc& desc) {
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000496 if (!this->configToGLFormats(desc.fConfig, false, NULL, NULL, NULL)) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000497 return NULL;
498 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000499
robertphillips@google.comb72e5d32012-10-30 15:18:10 +0000500 if (0 == desc.fTextureHandle) {
501 return NULL;
502 }
503
504 int maxSize = this->getCaps().maxTextureSize();
505 if (desc.fWidth > maxSize || desc.fHeight > maxSize) {
506 return NULL;
507 }
508
509 GrGLTexture::Desc glTexDesc;
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000510 // next line relies on GrBackendTextureDesc's flags matching GrTexture's
robertphillips@google.com32716282012-06-04 12:48:45 +0000511 glTexDesc.fFlags = (GrTextureFlags) desc.fFlags;
bsalomon@google.com99621082011-11-15 16:47:16 +0000512 glTexDesc.fWidth = desc.fWidth;
513 glTexDesc.fHeight = desc.fHeight;
bsalomon@google.come269f212011-11-07 13:29:52 +0000514 glTexDesc.fConfig = desc.fConfig;
robertphillips@google.com32716282012-06-04 12:48:45 +0000515 glTexDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.come269f212011-11-07 13:29:52 +0000516 glTexDesc.fTextureID = static_cast<GrGLuint>(desc.fTextureHandle);
bsalomon@google.com72830222013-01-23 20:25:22 +0000517 glTexDesc.fIsWrapped = true;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000518 bool renderTarget = 0 != (desc.fFlags & kRenderTarget_GrBackendTextureFlag);
519 // FIXME: this should be calling resolve_origin(), but Chrome code is currently
520 // assuming the old behaviour, which is that backend textures are always
521 // BottomLeft, even for non-RT's. Once Chrome is fixed, change this to:
522 // glTexDesc.fOrigin = resolve_origin(desc.fOrigin, renderTarget);
523 if (kDefault_GrSurfaceOrigin == desc.fOrigin) {
524 glTexDesc.fOrigin = kBottomLeft_GrSurfaceOrigin;
525 } else {
526 glTexDesc.fOrigin = desc.fOrigin;
527 }
bsalomon@google.come269f212011-11-07 13:29:52 +0000528
529 GrGLTexture* texture = NULL;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000530 if (renderTarget) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000531 GrGLRenderTarget::Desc glRTDesc;
532 glRTDesc.fRTFBOID = 0;
533 glRTDesc.fTexFBOID = 0;
534 glRTDesc.fMSColorRenderbufferID = 0;
bsalomon@google.come269f212011-11-07 13:29:52 +0000535 glRTDesc.fConfig = desc.fConfig;
536 glRTDesc.fSampleCnt = desc.fSampleCnt;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000537 glRTDesc.fOrigin = glTexDesc.fOrigin;
bsalomon@google.com99621082011-11-15 16:47:16 +0000538 if (!this->createRenderTargetObjects(glTexDesc.fWidth,
539 glTexDesc.fHeight,
bsalomon@google.come269f212011-11-07 13:29:52 +0000540 glTexDesc.fTextureID,
541 &glRTDesc)) {
542 return NULL;
543 }
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000544 texture = SkNEW_ARGS(GrGLTexture, (this, glTexDesc, glRTDesc));
bsalomon@google.come269f212011-11-07 13:29:52 +0000545 } else {
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000546 texture = SkNEW_ARGS(GrGLTexture, (this, glTexDesc));
bsalomon@google.come269f212011-11-07 13:29:52 +0000547 }
548 if (NULL == texture) {
549 return NULL;
550 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000551
bsalomon@google.come269f212011-11-07 13:29:52 +0000552 this->setSpareTextureUnit();
553 return texture;
554}
555
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000556GrRenderTarget* GrGpuGL::onWrapBackendRenderTarget(const GrBackendRenderTargetDesc& desc) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000557 GrGLRenderTarget::Desc glDesc;
558 glDesc.fConfig = desc.fConfig;
559 glDesc.fRTFBOID = static_cast<GrGLuint>(desc.fRenderTargetHandle);
560 glDesc.fMSColorRenderbufferID = 0;
561 glDesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
562 glDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.com72830222013-01-23 20:25:22 +0000563 glDesc.fIsWrapped = true;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000564 glDesc.fOrigin = desc.fOrigin;
565 if (glDesc.fRTFBOID == 0) {
566 GrAssert(desc.fOrigin == kBottomLeft_GrSurfaceOrigin);
567 }
568
569 glDesc.fOrigin = resolve_origin(desc.fOrigin, true);
bsalomon@google.come269f212011-11-07 13:29:52 +0000570 GrGLIRect viewport;
571 viewport.fLeft = 0;
572 viewport.fBottom = 0;
573 viewport.fWidth = desc.fWidth;
574 viewport.fHeight = desc.fHeight;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000575
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000576 GrRenderTarget* tgt = SkNEW_ARGS(GrGLRenderTarget,
577 (this, glDesc, viewport));
bsalomon@google.come269f212011-11-07 13:29:52 +0000578 if (desc.fStencilBits) {
579 GrGLStencilBuffer::Format format;
580 format.fInternalFormat = GrGLStencilBuffer::kUnknownInternalFormat;
581 format.fPacked = false;
582 format.fStencilBits = desc.fStencilBits;
583 format.fTotalBits = desc.fStencilBits;
bsalomon@google.com1f0f1a32013-01-23 21:32:32 +0000584 static const bool kIsSBWrapped = false;
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000585 GrGLStencilBuffer* sb = SkNEW_ARGS(GrGLStencilBuffer,
586 (this,
bsalomon@google.com1f0f1a32013-01-23 21:32:32 +0000587 kIsSBWrapped,
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000588 0,
589 desc.fWidth,
590 desc.fHeight,
591 desc.fSampleCnt,
592 format));
bsalomon@google.come269f212011-11-07 13:29:52 +0000593 tgt->setStencilBuffer(sb);
594 sb->unref();
595 }
596 return tgt;
597}
598
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000599////////////////////////////////////////////////////////////////////////////////
600
bsalomon@google.com9c680582013-02-06 18:17:50 +0000601bool GrGpuGL::onWriteTexturePixels(GrTexture* texture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000602 int left, int top, int width, int height,
603 GrPixelConfig config, const void* buffer,
604 size_t rowBytes) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000605 if (NULL == buffer) {
bsalomon@google.com9c680582013-02-06 18:17:50 +0000606 return false;
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000607 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000608 GrGLTexture* glTex = static_cast<GrGLTexture*>(texture);
609
bsalomon@google.com6f379512011-11-16 20:36:03 +0000610 this->setSpareTextureUnit();
611 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTex->textureID()));
612 GrGLTexture::Desc desc;
robertphillips@google.com32716282012-06-04 12:48:45 +0000613 desc.fFlags = glTex->desc().fFlags;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000614 desc.fWidth = glTex->width();
615 desc.fHeight = glTex->height();
robertphillips@google.com32716282012-06-04 12:48:45 +0000616 desc.fConfig = glTex->config();
617 desc.fSampleCnt = glTex->desc().fSampleCnt;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000618 desc.fTextureID = glTex->textureID();
bsalomon@google.com2d0bade2012-10-26 19:01:17 +0000619 desc.fOrigin = glTex->origin();
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000620
bsalomon@google.com9c680582013-02-06 18:17:50 +0000621 return this->uploadTexData(desc, false,
622 left, top, width, height,
623 config, buffer, rowBytes);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000624}
625
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000626namespace {
627bool adjust_pixel_ops_params(int surfaceWidth,
628 int surfaceHeight,
629 size_t bpp,
630 int* left, int* top, int* width, int* height,
631 const void** data,
632 size_t* rowBytes) {
633 if (!*rowBytes) {
634 *rowBytes = *width * bpp;
635 }
636
637 GrIRect subRect = GrIRect::MakeXYWH(*left, *top, *width, *height);
638 GrIRect bounds = GrIRect::MakeWH(surfaceWidth, surfaceHeight);
639
640 if (!subRect.intersect(bounds)) {
641 return false;
642 }
643 *data = reinterpret_cast<const void*>(reinterpret_cast<intptr_t>(*data) +
644 (subRect.fTop - *top) * *rowBytes + (subRect.fLeft - *left) * bpp);
645
646 *left = subRect.fLeft;
647 *top = subRect.fTop;
648 *width = subRect.width();
649 *height = subRect.height();
650 return true;
651}
652}
653
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000654bool GrGpuGL::uploadTexData(const GrGLTexture::Desc& desc,
655 bool isNewTexture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000656 int left, int top, int width, int height,
657 GrPixelConfig dataConfig,
658 const void* data,
659 size_t rowBytes) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000660 GrAssert(NULL != data || isNewTexture);
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000661
662 size_t bpp = GrBytesPerPixel(dataConfig);
663 if (!adjust_pixel_ops_params(desc.fWidth, desc.fHeight, bpp, &left, &top,
664 &width, &height, &data, &rowBytes)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000665 return false;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000666 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000667 size_t trimRowBytes = width * bpp;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000668
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000669 // in case we need a temporary, trimmed copy of the src pixels
670 SkAutoSMalloc<128 * 128> tempStorage;
671
bsalomon@google.com313f0192012-07-10 17:21:02 +0000672 // paletted textures cannot be partially updated
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000673 bool useTexStorage = isNewTexture &&
bsalomon@google.com313f0192012-07-10 17:21:02 +0000674 desc.fConfig != kIndex_8_GrPixelConfig &&
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000675 this->glCaps().texStorageSupport();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000676
bsalomon@google.com313f0192012-07-10 17:21:02 +0000677 if (useTexStorage && kDesktop_GrGLBinding == this->glBinding()) {
678 // 565 is not a sized internal format on desktop GL. So on desktop with
679 // 565 we always use an unsized internal format to let the system pick
680 // the best sized format to convert the 565 data to. Since TexStorage
681 // only allows sized internal formats we will instead use TexImage2D.
682 useTexStorage = desc.fConfig != kRGB_565_GrPixelConfig;
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000683 }
684
685 GrGLenum internalFormat;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000686 GrGLenum externalFormat;
687 GrGLenum externalType;
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000688 // glTexStorage requires sized internal formats on both desktop and ES. ES
689 // glTexImage requires an unsized format.
690 if (!this->configToGLFormats(dataConfig, useTexStorage, &internalFormat,
691 &externalFormat, &externalType)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000692 return false;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000693 }
694
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000695 if (!isNewTexture && GR_GL_PALETTE8_RGBA8 == internalFormat) {
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000696 // paletted textures cannot be updated
697 return false;
698 }
699
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000700 /*
bsalomon@google.com6f379512011-11-16 20:36:03 +0000701 * check whether to allocate a temporary buffer for flipping y or
702 * because our srcData has extra bytes past each row. If so, we need
703 * to trim those off here, since GL ES may not let us specify
704 * GL_UNPACK_ROW_LENGTH.
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000705 */
bsalomon@google.com6f379512011-11-16 20:36:03 +0000706 bool restoreGLRowLength = false;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000707 bool swFlipY = false;
708 bool glFlipY = false;
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000709 if (NULL != data) {
senorblanco@chromium.orgef5dbe12013-01-28 16:42:38 +0000710 if (kBottomLeft_GrSurfaceOrigin == desc.fOrigin) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000711 if (this->glCaps().unpackFlipYSupport()) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000712 glFlipY = true;
713 } else {
714 swFlipY = true;
715 }
716 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000717 if (this->glCaps().unpackRowLengthSupport() && !swFlipY) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000718 // can't use this for flipping, only non-neg values allowed. :(
719 if (rowBytes != trimRowBytes) {
720 GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
721 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength));
722 restoreGLRowLength = true;
723 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000724 } else {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000725 if (trimRowBytes != rowBytes || swFlipY) {
726 // copy data into our new storage, skipping the trailing bytes
727 size_t trimSize = height * trimRowBytes;
728 const char* src = (const char*)data;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000729 if (swFlipY) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000730 src += (height - 1) * rowBytes;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000731 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000732 char* dst = (char*)tempStorage.reset(trimSize);
733 for (int y = 0; y < height; y++) {
734 memcpy(dst, src, trimRowBytes);
735 if (swFlipY) {
736 src -= rowBytes;
737 } else {
738 src += rowBytes;
739 }
740 dst += trimRowBytes;
741 }
742 // now point data to our copied version
743 data = tempStorage.get();
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000744 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000745 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000746 if (glFlipY) {
747 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_TRUE));
748 }
749 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, static_cast<GrGLint>(bpp)));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000750 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000751 bool succeeded = true;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000752 if (isNewTexture &&
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000753 0 == left && 0 == top &&
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000754 desc.fWidth == width && desc.fHeight == height) {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000755 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000756 if (useTexStorage) {
757 // We never resize or change formats of textures. We don't use
758 // mipmaps currently.
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000759 GL_ALLOC_CALL(this->glInterface(),
760 TexStorage2D(GR_GL_TEXTURE_2D,
761 1, // levels
762 internalFormat,
763 desc.fWidth, desc.fHeight));
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000764 } else {
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000765 if (GR_GL_PALETTE8_RGBA8 == internalFormat) {
766 GrGLsizei imageSize = desc.fWidth * desc.fHeight +
767 kGrColorTableSize;
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000768 GL_ALLOC_CALL(this->glInterface(),
769 CompressedTexImage2D(GR_GL_TEXTURE_2D,
770 0, // level
771 internalFormat,
772 desc.fWidth, desc.fHeight,
773 0, // border
774 imageSize,
775 data));
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000776 } else {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000777 GL_ALLOC_CALL(this->glInterface(),
778 TexImage2D(GR_GL_TEXTURE_2D,
779 0, // level
780 internalFormat,
781 desc.fWidth, desc.fHeight,
782 0, // border
783 externalFormat, externalType,
784 data));
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000785 }
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000786 }
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000787 GrGLenum error = CHECK_ALLOC_ERROR(this->glInterface());
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000788 if (error != GR_GL_NO_ERROR) {
789 succeeded = false;
790 } else {
791 // if we have data and we used TexStorage to create the texture, we
792 // now upload with TexSubImage.
793 if (NULL != data && useTexStorage) {
794 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D,
795 0, // level
796 left, top,
797 width, height,
798 externalFormat, externalType,
799 data));
800 }
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000801 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000802 } else {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000803 if (swFlipY || glFlipY) {
bsalomon@google.com6f379512011-11-16 20:36:03 +0000804 top = desc.fHeight - (top + height);
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000805 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000806 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D,
807 0, // level
808 left, top,
809 width, height,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000810 externalFormat, externalType, data));
811 }
812
813 if (restoreGLRowLength) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000814 GrAssert(this->glCaps().unpackRowLengthSupport());
bsalomon@google.com6f379512011-11-16 20:36:03 +0000815 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000816 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000817 if (glFlipY) {
818 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
819 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000820 return succeeded;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000821}
822
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000823namespace {
824bool renderbuffer_storage_msaa(GrGLContextInfo& ctxInfo,
825 int sampleCount,
826 GrGLenum format,
827 int width, int height) {
828 CLEAR_ERROR_BEFORE_ALLOC(ctxInfo.interface());
829 GrAssert(GrGLCaps::kNone_MSFBOType != ctxInfo.caps().msFBOType());
830 bool created = false;
831 if (GrGLCaps::kNVDesktop_CoverageAAType ==
832 ctxInfo.caps().coverageAAType()) {
833 const GrGLCaps::MSAACoverageMode& mode =
834 ctxInfo.caps().getMSAACoverageMode(sampleCount);
835 GL_ALLOC_CALL(ctxInfo.interface(),
836 RenderbufferStorageMultisampleCoverage(GR_GL_RENDERBUFFER,
837 mode.fCoverageSampleCnt,
838 mode.fColorSampleCnt,
839 format,
840 width, height));
841 created = (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctxInfo.interface()));
842 }
843 if (!created) {
bsalomon@google.comf6b070d2012-04-27 14:25:44 +0000844 // glRBMS will fail if requested samples is > max samples.
845 sampleCount = GrMin(sampleCount, ctxInfo.caps().maxSampleCount());
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000846 GL_ALLOC_CALL(ctxInfo.interface(),
847 RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
848 sampleCount,
849 format,
850 width, height));
851 created = (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctxInfo.interface()));
852 }
853 return created;
854}
855}
856
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000857bool GrGpuGL::createRenderTargetObjects(int width, int height,
858 GrGLuint texID,
859 GrGLRenderTarget::Desc* desc) {
860 desc->fMSColorRenderbufferID = 0;
861 desc->fRTFBOID = 0;
862 desc->fTexFBOID = 0;
bsalomon@google.com72830222013-01-23 20:25:22 +0000863 desc->fIsWrapped = false;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000864
865 GrGLenum status;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000866
bsalomon@google.comab15d612011-08-09 12:57:56 +0000867 GrGLenum msColorFormat = 0; // suppress warning
868
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000869 GL_CALL(GenFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000870 if (!desc->fTexFBOID) {
871 goto FAILED;
872 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000873
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000874
875 // If we are using multisampling we will create two FBOS. We render
876 // to one and then resolve to the texture bound to the other.
bsalomon@google.come269f212011-11-07 13:29:52 +0000877 if (desc->fSampleCnt > 0) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000878 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000879 goto FAILED;
880 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000881 GL_CALL(GenFramebuffers(1, &desc->fRTFBOID));
882 GL_CALL(GenRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000883 if (!desc->fRTFBOID ||
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000884 !desc->fMSColorRenderbufferID ||
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000885 !this->configToGLFormats(desc->fConfig,
886 // GLES requires sized internal formats
887 kES2_GrGLBinding == this->glBinding(),
888 &msColorFormat, NULL, NULL)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000889 goto FAILED;
890 }
891 } else {
892 desc->fRTFBOID = desc->fTexFBOID;
893 }
894
bsalomon@google.com0e9b41a2012-01-04 22:11:43 +0000895 // below here we may bind the FBO
bsalomon@google.comc811ea32012-05-21 15:33:09 +0000896 fHWBoundRenderTarget = NULL;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000897 if (desc->fRTFBOID != desc->fTexFBOID) {
bsalomon@google.com13ac3a32013-01-09 23:29:39 +0000898 GrAssert(desc->fSampleCnt > 0);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000899 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000900 desc->fMSColorRenderbufferID));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000901 if (!renderbuffer_storage_msaa(fGLContextInfo,
902 desc->fSampleCnt,
903 msColorFormat,
904 width, height)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000905 goto FAILED;
906 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000907 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fRTFBOID));
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000908 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000909 GR_GL_COLOR_ATTACHMENT0,
910 GR_GL_RENDERBUFFER,
911 desc->fMSColorRenderbufferID));
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000912 if (!this->glCaps().isConfigVerifiedColorAttachment(desc->fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000913 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
914 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
915 goto FAILED;
916 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000917 fGLContextInfo.caps().markConfigAsValidColorAttachment(
918 desc->fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000919 }
920 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000921 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000922
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000923 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
924 GR_GL_COLOR_ATTACHMENT0,
925 GR_GL_TEXTURE_2D,
926 texID, 0));
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000927 if (!this->glCaps().isConfigVerifiedColorAttachment(desc->fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000928 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
929 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
930 goto FAILED;
931 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000932 fGLContextInfo.caps().markConfigAsValidColorAttachment(desc->fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000933 }
934
935 return true;
936
937FAILED:
938 if (desc->fMSColorRenderbufferID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000939 GL_CALL(DeleteRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000940 }
941 if (desc->fRTFBOID != desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000942 GL_CALL(DeleteFramebuffers(1, &desc->fRTFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000943 }
944 if (desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000945 GL_CALL(DeleteFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000946 }
947 return false;
948}
949
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000950// good to set a break-point here to know when createTexture fails
951static GrTexture* return_null_texture() {
952// GrAssert(!"null texture");
953 return NULL;
954}
955
caryclark@google.comcf6285b2012-06-06 12:09:01 +0000956#if 0 && GR_DEBUG
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000957static size_t as_size_t(int x) {
958 return x;
959}
960#endif
961
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000962GrTexture* GrGpuGL::onCreateTexture(const GrTextureDesc& desc,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000963 const void* srcData,
964 size_t rowBytes) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000965
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000966 GrGLTexture::Desc glTexDesc;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000967 GrGLRenderTarget::Desc glRTDesc;
reed@google.comac10a2d2010-12-22 21:39:39 +0000968
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000969 // Attempt to catch un- or wrongly initialized sample counts;
970 GrAssert(desc.fSampleCnt >= 0 && desc.fSampleCnt <= 64);
971
robertphillips@google.com32716282012-06-04 12:48:45 +0000972 glTexDesc.fFlags = desc.fFlags;
bsalomon@google.com99621082011-11-15 16:47:16 +0000973 glTexDesc.fWidth = desc.fWidth;
974 glTexDesc.fHeight = desc.fHeight;
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000975 glTexDesc.fConfig = desc.fConfig;
robertphillips@google.com32716282012-06-04 12:48:45 +0000976 glTexDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.com72830222013-01-23 20:25:22 +0000977 glTexDesc.fIsWrapped = false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000978
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000979 glRTDesc.fMSColorRenderbufferID = 0;
980 glRTDesc.fRTFBOID = 0;
981 glRTDesc.fTexFBOID = 0;
bsalomon@google.com72830222013-01-23 20:25:22 +0000982 glRTDesc.fIsWrapped = false;
bsalomon@google.com64c4fe42011-11-05 14:51:01 +0000983 glRTDesc.fConfig = glTexDesc.fConfig;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000984
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000985 bool renderTarget = 0 != (desc.fFlags & kRenderTarget_GrTextureFlagBit);
reed@google.comac10a2d2010-12-22 21:39:39 +0000986
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000987 const Caps& caps = this->getCaps();
988
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000989 glTexDesc.fOrigin = resolve_origin(desc.fOrigin, renderTarget);
990 glRTDesc.fOrigin = glTexDesc.fOrigin;
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000991
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000992 glRTDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000993 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType() &&
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000994 desc.fSampleCnt) {
bsalomon@google.com945bbe12012-06-15 14:30:34 +0000995 //GrPrintf("MSAA RT requested but not supported on this platform.");
996 return return_null_texture();
reed@google.comac10a2d2010-12-22 21:39:39 +0000997 }
998
reed@google.comac10a2d2010-12-22 21:39:39 +0000999 if (renderTarget) {
bsalomon@google.comf6601872012-08-28 21:11:35 +00001000 if (glTexDesc.fWidth > caps.maxRenderTargetSize() ||
1001 glTexDesc.fHeight > caps.maxRenderTargetSize()) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001002 return return_null_texture();
1003 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001004 }
1005
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001006 GL_CALL(GenTextures(1, &glTexDesc.fTextureID));
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001007 if (renderTarget && this->glCaps().textureUsageSupport()) {
bsalomon@google.com07dd2bf2011-12-09 19:40:36 +00001008 // provides a hint about how this texture will be used
1009 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1010 GR_GL_TEXTURE_USAGE,
1011 GR_GL_FRAMEBUFFER_ATTACHMENT));
1012 }
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001013 if (!glTexDesc.fTextureID) {
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001014 return return_null_texture();
1015 }
1016
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001017 this->setSpareTextureUnit();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001018 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTexDesc.fTextureID));
bsalomon@google.come269f212011-11-07 13:29:52 +00001019
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001020 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
1021 // drivers have a bug where an FBO won't be complete if it includes a
1022 // texture that is not mipmap complete (considering the filter in use).
1023 GrGLTexture::TexParams initialTexParams;
1024 // we only set a subset here so invalidate first
1025 initialTexParams.invalidate();
1026 initialTexParams.fFilter = GR_GL_NEAREST;
1027 initialTexParams.fWrapS = GR_GL_CLAMP_TO_EDGE;
1028 initialTexParams.fWrapT = GR_GL_CLAMP_TO_EDGE;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001029 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1030 GR_GL_TEXTURE_MAG_FILTER,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001031 initialTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001032 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1033 GR_GL_TEXTURE_MIN_FILTER,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001034 initialTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001035 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1036 GR_GL_TEXTURE_WRAP_S,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001037 initialTexParams.fWrapS));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001038 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1039 GR_GL_TEXTURE_WRAP_T,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001040 initialTexParams.fWrapT));
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001041 if (!this->uploadTexData(glTexDesc, true, 0, 0,
1042 glTexDesc.fWidth, glTexDesc.fHeight,
1043 desc.fConfig, srcData, rowBytes)) {
1044 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
1045 return return_null_texture();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001046 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001047
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001048 GrGLTexture* tex;
reed@google.comac10a2d2010-12-22 21:39:39 +00001049 if (renderTarget) {
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001050 // unbind the texture from the texture unit before binding it to the frame buffer
1051 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, 0));
1052
bsalomon@google.com99621082011-11-15 16:47:16 +00001053 if (!this->createRenderTargetObjects(glTexDesc.fWidth,
1054 glTexDesc.fHeight,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001055 glTexDesc.fTextureID,
1056 &glRTDesc)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001057 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001058 return return_null_texture();
1059 }
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001060 tex = SkNEW_ARGS(GrGLTexture, (this, glTexDesc, glRTDesc));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001061 } else {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001062 tex = SkNEW_ARGS(GrGLTexture, (this, glTexDesc));
reed@google.comac10a2d2010-12-22 21:39:39 +00001063 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001064 tex->setCachedTexParams(initialTexParams, this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00001065#ifdef TRACE_TEXTURE_CREATION
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001066 GrPrintf("--- new texture [%d] size=(%d %d) config=%d\n",
1067 glTexDesc.fTextureID, desc.fWidth, desc.fHeight, desc.fConfig);
reed@google.comac10a2d2010-12-22 21:39:39 +00001068#endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001069 return tex;
1070}
1071
1072namespace {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001073
1074const GrGLuint kUnknownBitCount = GrGLStencilBuffer::kUnknownBitCount;
1075
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001076void inline get_stencil_rb_sizes(const GrGLInterface* gl,
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001077 GrGLuint rb,
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001078 GrGLStencilBuffer::Format* format) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001079 // we shouldn't ever know one size and not the other
1080 GrAssert((kUnknownBitCount == format->fStencilBits) ==
1081 (kUnknownBitCount == format->fTotalBits));
1082 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001083 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001084 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1085 (GrGLint*)&format->fStencilBits);
1086 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001087 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001088 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1089 (GrGLint*)&format->fTotalBits);
1090 format->fTotalBits += format->fStencilBits;
1091 } else {
1092 format->fTotalBits = format->fStencilBits;
1093 }
1094 }
1095}
1096}
1097
1098bool GrGpuGL::createStencilBufferForRenderTarget(GrRenderTarget* rt,
1099 int width, int height) {
1100
1101 // All internally created RTs are also textures. We don't create
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +00001102 // 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 +00001103 GrAssert(rt->asTexture());
bsalomon@google.com99621082011-11-15 16:47:16 +00001104 GrAssert(width >= rt->width());
1105 GrAssert(height >= rt->height());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001106
1107 int samples = rt->numSamples();
1108 GrGLuint sbID;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001109 GL_CALL(GenRenderbuffers(1, &sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001110 if (!sbID) {
1111 return false;
1112 }
1113
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001114 int stencilFmtCnt = this->glCaps().stencilFormats().count();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001115 for (int i = 0; i < stencilFmtCnt; ++i) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001116 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001117 // we start with the last stencil format that succeeded in hopes
1118 // that we won't go through this loop more than once after the
1119 // first (painful) stencil creation.
1120 int sIdx = (i + fLastSuccessfulStencilFmtIdx) % stencilFmtCnt;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001121 const GrGLCaps::StencilFormat& sFmt =
1122 this->glCaps().stencilFormats()[sIdx];
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001123 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001124 // we do this "if" so that we don't call the multisample
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001125 // version on a GL that doesn't have an MSAA extension.
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001126 bool created;
1127 if (samples > 0) {
1128 created = renderbuffer_storage_msaa(fGLContextInfo,
1129 samples,
1130 sFmt.fInternalFormat,
1131 width, height);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001132 } else {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001133 GL_ALLOC_CALL(this->glInterface(),
1134 RenderbufferStorage(GR_GL_RENDERBUFFER,
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001135 sFmt.fInternalFormat,
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001136 width, height));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001137 created =
1138 (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(this->glInterface()));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001139 }
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001140 if (created) {
1141 // After sized formats we attempt an unsized format and take
1142 // whatever sizes GL gives us. In that case we query for the size.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001143 GrGLStencilBuffer::Format format = sFmt;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001144 get_stencil_rb_sizes(this->glInterface(), sbID, &format);
bsalomon@google.com72830222013-01-23 20:25:22 +00001145 static const bool kIsWrapped = false;
robertphillips@google.comf2e93fc2012-09-05 19:44:18 +00001146 SkAutoTUnref<GrStencilBuffer> sb(SkNEW_ARGS(GrGLStencilBuffer,
bsalomon@google.com72830222013-01-23 20:25:22 +00001147 (this, kIsWrapped, sbID, width, height,
robertphillips@google.comf2e93fc2012-09-05 19:44:18 +00001148 samples, format)));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001149 if (this->attachStencilBufferToRenderTarget(sb, rt)) {
1150 fLastSuccessfulStencilFmtIdx = sIdx;
robertphillips@google.com9fbcad02012-09-09 14:44:15 +00001151 sb->transferToCache();
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001152 rt->setStencilBuffer(sb);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001153 return true;
1154 }
1155 sb->abandon(); // otherwise we lose sbID
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001156 }
1157 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001158 GL_CALL(DeleteRenderbuffers(1, &sbID));
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001159 return false;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001160}
1161
1162bool GrGpuGL::attachStencilBufferToRenderTarget(GrStencilBuffer* sb,
1163 GrRenderTarget* rt) {
1164 GrGLRenderTarget* glrt = (GrGLRenderTarget*) rt;
1165
1166 GrGLuint fbo = glrt->renderFBOID();
1167
1168 if (NULL == sb) {
1169 if (NULL != rt->getStencilBuffer()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001170 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001171 GR_GL_STENCIL_ATTACHMENT,
1172 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001173 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001174 GR_GL_DEPTH_ATTACHMENT,
1175 GR_GL_RENDERBUFFER, 0));
1176#if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001177 GrGLenum status;
1178 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001179 GrAssert(GR_GL_FRAMEBUFFER_COMPLETE == status);
1180#endif
1181 }
1182 return true;
1183 } else {
1184 GrGLStencilBuffer* glsb = (GrGLStencilBuffer*) sb;
1185 GrGLuint rb = glsb->renderbufferID();
1186
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001187 fHWBoundRenderTarget = NULL;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001188 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, fbo));
1189 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001190 GR_GL_STENCIL_ATTACHMENT,
1191 GR_GL_RENDERBUFFER, rb));
1192 if (glsb->format().fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001193 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001194 GR_GL_DEPTH_ATTACHMENT,
1195 GR_GL_RENDERBUFFER, rb));
1196 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001197 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001198 GR_GL_DEPTH_ATTACHMENT,
1199 GR_GL_RENDERBUFFER, 0));
reed@google.comac10a2d2010-12-22 21:39:39 +00001200 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001201
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001202 GrGLenum status;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001203 if (!this->glCaps().isColorConfigAndStencilFormatVerified(rt->config(),
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001204 glsb->format())) {
1205 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1206 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001207 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001208 GR_GL_STENCIL_ATTACHMENT,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001209 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001210 if (glsb->format().fPacked) {
1211 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1212 GR_GL_DEPTH_ATTACHMENT,
1213 GR_GL_RENDERBUFFER, 0));
1214 }
1215 return false;
1216 } else {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001217 fGLContextInfo.caps().markColorConfigAndStencilFormatAsVerified(
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001218 rt->config(),
1219 glsb->format());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001220 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001221 }
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001222 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001223 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001224}
1225
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001226////////////////////////////////////////////////////////////////////////////////
1227
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001228GrVertexBuffer* GrGpuGL::onCreateVertexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001229 GrGLuint id;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001230 GL_CALL(GenBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001231 if (id) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001232 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, id));
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001233 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001234 // make sure driver can allocate memory for this buffer
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001235 GL_ALLOC_CALL(this->glInterface(),
1236 BufferData(GR_GL_ARRAY_BUFFER,
1237 size,
1238 NULL, // data ptr
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00001239 dynamic ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW));
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001240 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001241 GL_CALL(DeleteBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001242 // deleting bound buffer does implicit bind to 0
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00001243 fHWGeometryState.setVertexBufferID(0);
reed@google.comac10a2d2010-12-22 21:39:39 +00001244 return NULL;
1245 }
bsalomon@google.com72830222013-01-23 20:25:22 +00001246 static const bool kIsWrapped = false;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00001247 GrGLVertexBuffer* vertexBuffer = SkNEW_ARGS(GrGLVertexBuffer, (this,
bsalomon@google.com19e393c2013-02-19 20:27:02 +00001248 kIsWrapped,
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00001249 id,
1250 size,
1251 dynamic));
1252 fHWGeometryState.setVertexBufferID(id);
reed@google.comac10a2d2010-12-22 21:39:39 +00001253 return vertexBuffer;
1254 }
1255 return NULL;
1256}
1257
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001258GrIndexBuffer* GrGpuGL::onCreateIndexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001259 GrGLuint id;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001260 GL_CALL(GenBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001261 if (id) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001262 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, id));
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001263 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001264 // make sure driver can allocate memory for this buffer
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001265 GL_ALLOC_CALL(this->glInterface(),
1266 BufferData(GR_GL_ELEMENT_ARRAY_BUFFER,
1267 size,
1268 NULL, // data ptr
1269 dynamic ? GR_GL_DYNAMIC_DRAW :
1270 GR_GL_STATIC_DRAW));
1271 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001272 GL_CALL(DeleteBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001273 // deleting bound buffer does implicit bind to 0
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00001274 fHWGeometryState.setIndexBufferID(0);
reed@google.comac10a2d2010-12-22 21:39:39 +00001275 return NULL;
1276 }
bsalomon@google.com72830222013-01-23 20:25:22 +00001277 static const bool kIsWrapped = false;
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001278 GrIndexBuffer* indexBuffer = SkNEW_ARGS(GrGLIndexBuffer,
bsalomon@google.com72830222013-01-23 20:25:22 +00001279 (this, kIsWrapped, id, size, dynamic));
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00001280 fHWGeometryState.setIndexBufferID(id);
reed@google.comac10a2d2010-12-22 21:39:39 +00001281 return indexBuffer;
1282 }
1283 return NULL;
1284}
1285
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00001286GrPath* GrGpuGL::onCreatePath(const SkPath& inPath) {
bsalomon@google.comf6601872012-08-28 21:11:35 +00001287 GrAssert(fCaps.pathStencilingSupport());
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001288 return SkNEW_ARGS(GrGLPath, (this, inPath));
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00001289}
1290
bsalomon@google.coma3201942012-06-21 19:58:20 +00001291void GrGpuGL::flushScissor() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001292 const GrDrawState& drawState = this->getDrawState();
1293 const GrGLRenderTarget* rt =
1294 static_cast<const GrGLRenderTarget*>(drawState.getRenderTarget());
1295
1296 GrAssert(NULL != rt);
1297 const GrGLIRect& vp = rt->getViewport();
reed@google.comac10a2d2010-12-22 21:39:39 +00001298
bsalomon@google.coma3201942012-06-21 19:58:20 +00001299 if (fScissorState.fEnabled) {
1300 GrGLIRect scissor;
1301 scissor.setRelativeTo(vp,
1302 fScissorState.fRect.fLeft,
1303 fScissorState.fRect.fTop,
1304 fScissorState.fRect.width(),
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001305 fScissorState.fRect.height(),
1306 rt->origin());
bsalomon@google.coma3201942012-06-21 19:58:20 +00001307 // if the scissor fully contains the viewport then we fall through and
1308 // disable the scissor test.
1309 if (!scissor.contains(vp)) {
1310 if (fHWScissorSettings.fRect != scissor) {
1311 scissor.pushToGLScissor(this->glInterface());
1312 fHWScissorSettings.fRect = scissor;
1313 }
1314 if (kYes_TriState != fHWScissorSettings.fEnabled) {
1315 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1316 fHWScissorSettings.fEnabled = kYes_TriState;
1317 }
1318 return;
1319 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001320 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001321 if (kNo_TriState != fHWScissorSettings.fEnabled) {
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001322 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.coma3201942012-06-21 19:58:20 +00001323 fHWScissorSettings.fEnabled = kNo_TriState;
1324 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001325 }
1326}
1327
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001328void GrGpuGL::onClear(const GrIRect* rect, GrColor color) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001329 const GrDrawState& drawState = this->getDrawState();
1330 const GrRenderTarget* rt = drawState.getRenderTarget();
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001331 // parent class should never let us get here with no RT
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001332 GrAssert(NULL != rt);
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001333
bsalomon@google.com74b98712011-11-11 19:46:16 +00001334 GrIRect clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001335 if (NULL != rect) {
1336 // flushScissor expects rect to be clipped to the target.
bsalomon@google.com74b98712011-11-11 19:46:16 +00001337 clippedRect = *rect;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001338 GrIRect rtRect = SkIRect::MakeWH(rt->width(), rt->height());
bsalomon@google.com74b98712011-11-11 19:46:16 +00001339 if (clippedRect.intersect(rtRect)) {
1340 rect = &clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001341 } else {
1342 return;
1343 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001344 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001345 this->flushRenderTarget(rect);
bsalomon@google.coma3201942012-06-21 19:58:20 +00001346 GrAutoTRestore<ScissorState> asr(&fScissorState);
1347 fScissorState.fEnabled = (NULL != rect);
1348 if (fScissorState.fEnabled) {
1349 fScissorState.fRect = *rect;
1350 }
1351 this->flushScissor();
bsalomon@google.com74b98712011-11-11 19:46:16 +00001352
1353 GrGLfloat r, g, b, a;
1354 static const GrGLfloat scale255 = 1.f / 255.f;
1355 a = GrColorUnpackA(color) * scale255;
1356 GrGLfloat scaleRGB = scale255;
bsalomon@google.com74b98712011-11-11 19:46:16 +00001357 r = GrColorUnpackR(color) * scaleRGB;
1358 g = GrColorUnpackG(color) * scaleRGB;
1359 b = GrColorUnpackB(color) * scaleRGB;
1360
1361 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00001362 fHWWriteToColor = kYes_TriState;
bsalomon@google.com74b98712011-11-11 19:46:16 +00001363 GL_CALL(ClearColor(r, g, b, a));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001364 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001365}
1366
bsalomon@google.comedc177d2011-08-05 15:46:40 +00001367void GrGpuGL::clearStencil() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001368 if (NULL == this->getDrawState().getRenderTarget()) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001369 return;
1370 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001371
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001372 this->flushRenderTarget(&GrIRect::EmptyIRect());
1373
bsalomon@google.coma3201942012-06-21 19:58:20 +00001374 GrAutoTRestore<ScissorState> asr(&fScissorState);
1375 fScissorState.fEnabled = false;
1376 this->flushScissor();
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001377
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001378 GL_CALL(StencilMask(0xffffffff));
1379 GL_CALL(ClearStencil(0));
1380 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001381 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001382}
1383
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001384void GrGpuGL::clearStencilClip(const GrIRect& rect, bool insideClip) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001385 const GrDrawState& drawState = this->getDrawState();
1386 const GrRenderTarget* rt = drawState.getRenderTarget();
1387 GrAssert(NULL != rt);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001388
1389 // this should only be called internally when we know we have a
1390 // stencil buffer.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001391 GrAssert(NULL != rt->getStencilBuffer());
1392 GrGLint stencilBitCount = rt->getStencilBuffer()->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001393#if 0
reed@google.comac10a2d2010-12-22 21:39:39 +00001394 GrAssert(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00001395 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001396#else
1397 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001398 // ANGLE a partial stencil mask will cause clears to be
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001399 // turned into draws. Our contract on GrDrawTarget says that
1400 // changing the clip between stencil passes may or may not
1401 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00001402 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001403#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001404 GrGLint value;
1405 if (insideClip) {
1406 value = (1 << (stencilBitCount - 1));
1407 } else {
1408 value = 0;
1409 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001410 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.coma3201942012-06-21 19:58:20 +00001411
1412 GrAutoTRestore<ScissorState> asr(&fScissorState);
1413 fScissorState.fEnabled = true;
1414 fScissorState.fRect = rect;
1415 this->flushScissor();
1416
caryclark@google.comcf6285b2012-06-06 12:09:01 +00001417 GL_CALL(StencilMask((uint32_t) clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001418 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001419 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001420 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001421}
1422
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001423void GrGpuGL::onForceRenderTargetFlush() {
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001424 this->flushRenderTarget(&GrIRect::EmptyIRect());
reed@google.comac10a2d2010-12-22 21:39:39 +00001425}
1426
bsalomon@google.comc4364992011-11-07 15:54:49 +00001427bool GrGpuGL::readPixelsWillPayForYFlip(GrRenderTarget* renderTarget,
1428 int left, int top,
1429 int width, int height,
1430 GrPixelConfig config,
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001431 size_t rowBytes) const {
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001432 // If this rendertarget is aready TopLeft, we don't need to flip.
1433 if (kTopLeft_GrSurfaceOrigin == renderTarget->origin()) {
1434 return false;
1435 }
1436
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001437 // if GL can do the flip then we'll never pay for it.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001438 if (this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001439 return false;
1440 }
1441
1442 // If we have to do memcpy to handle non-trim rowBytes then we
bsalomon@google.com7107fa72011-11-10 14:54:14 +00001443 // get the flip for free. Otherwise it costs.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001444 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001445 return true;
1446 }
1447 // If we have to do memcpys to handle rowBytes then y-flip is free
1448 // Note the rowBytes might be tight to the passed in data, but if data
1449 // gets clipped in x to the target the rowBytes will no longer be tight.
1450 if (left >= 0 && (left + width) < renderTarget->width()) {
1451 return 0 == rowBytes ||
1452 GrBytesPerPixel(config) * width == rowBytes;
1453 } else {
1454 return false;
1455 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001456}
1457
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001458bool GrGpuGL::onReadPixels(GrRenderTarget* target,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001459 int left, int top,
1460 int width, int height,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001461 GrPixelConfig config,
1462 void* buffer,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001463 size_t rowBytes) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001464 GrGLenum format;
1465 GrGLenum type;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001466 bool flipY = kBottomLeft_GrSurfaceOrigin == target->origin();
bsalomon@google.com280e99f2012-01-05 16:17:38 +00001467 if (!this->configToGLFormats(config, false, NULL, &format, &type)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001468 return false;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001469 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001470 size_t bpp = GrBytesPerPixel(config);
1471 if (!adjust_pixel_ops_params(target->width(), target->height(), bpp,
1472 &left, &top, &width, &height,
1473 const_cast<const void**>(&buffer),
1474 &rowBytes)) {
1475 return false;
1476 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001477
bsalomon@google.comc6980972011-11-02 19:57:21 +00001478 // resolve the render target if necessary
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001479 GrGLRenderTarget* tgt = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001480 GrDrawState::AutoRenderTargetRestore artr;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001481 switch (tgt->getResolveType()) {
1482 case GrGLRenderTarget::kCantResolve_ResolveType:
1483 return false;
1484 case GrGLRenderTarget::kAutoResolves_ResolveType:
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001485 artr.set(this->drawState(), target);
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001486 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001487 break;
1488 case GrGLRenderTarget::kCanResolve_ResolveType:
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001489 this->onResolveRenderTarget(tgt);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001490 // we don't track the state of the READ FBO ID.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001491 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1492 tgt->textureFBOID()));
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001493 break;
1494 default:
1495 GrCrash("Unknown resolve type");
reed@google.comac10a2d2010-12-22 21:39:39 +00001496 }
1497
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001498 const GrGLIRect& glvp = tgt->getViewport();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001499
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001500 // the read rect is viewport-relative
1501 GrGLIRect readRect;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001502 readRect.setRelativeTo(glvp, left, top, width, height, target->origin());
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001503
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001504 size_t tightRowBytes = bpp * width;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001505 if (0 == rowBytes) {
1506 rowBytes = tightRowBytes;
1507 }
1508 size_t readDstRowBytes = tightRowBytes;
1509 void* readDst = buffer;
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001510
bsalomon@google.comc6980972011-11-02 19:57:21 +00001511 // determine if GL can read using the passed rowBytes or if we need
1512 // a scratch buffer.
1513 SkAutoSMalloc<32 * sizeof(GrColor)> scratch;
1514 if (rowBytes != tightRowBytes) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001515 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.comc6980972011-11-02 19:57:21 +00001516 GrAssert(!(rowBytes % sizeof(GrColor)));
1517 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, rowBytes / sizeof(GrColor)));
1518 readDstRowBytes = rowBytes;
1519 } else {
1520 scratch.reset(tightRowBytes * height);
1521 readDst = scratch.get();
1522 }
1523 }
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001524 if (flipY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001525 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 1));
1526 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001527 GL_CALL(ReadPixels(readRect.fLeft, readRect.fBottom,
1528 readRect.fWidth, readRect.fHeight,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001529 format, type, readDst));
1530 if (readDstRowBytes != tightRowBytes) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001531 GrAssert(this->glCaps().packRowLengthSupport());
bsalomon@google.comc6980972011-11-02 19:57:21 +00001532 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
1533 }
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001534 if (flipY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001535 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 0));
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001536 flipY = false;
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001537 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001538
1539 // now reverse the order of the rows, since GL's are bottom-to-top, but our
bsalomon@google.comc6980972011-11-02 19:57:21 +00001540 // API presents top-to-bottom. We must preserve the padding contents. Note
1541 // that the above readPixels did not overwrite the padding.
1542 if (readDst == buffer) {
1543 GrAssert(rowBytes == readDstRowBytes);
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001544 if (flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001545 scratch.reset(tightRowBytes);
1546 void* tmpRow = scratch.get();
1547 // flip y in-place by rows
1548 const int halfY = height >> 1;
1549 char* top = reinterpret_cast<char*>(buffer);
1550 char* bottom = top + (height - 1) * rowBytes;
1551 for (int y = 0; y < halfY; y++) {
1552 memcpy(tmpRow, top, tightRowBytes);
1553 memcpy(top, bottom, tightRowBytes);
1554 memcpy(bottom, tmpRow, tightRowBytes);
1555 top += rowBytes;
1556 bottom -= rowBytes;
1557 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001558 }
1559 } else {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001560 GrAssert(readDst != buffer); GrAssert(rowBytes != tightRowBytes);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001561 // copy from readDst to buffer while flipping y
caryclark@google.comcf6285b2012-06-06 12:09:01 +00001562 // const int halfY = height >> 1;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001563 const char* src = reinterpret_cast<const char*>(readDst);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001564 char* dst = reinterpret_cast<char*>(buffer);
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001565 if (flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001566 dst += (height-1) * rowBytes;
1567 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001568 for (int y = 0; y < height; y++) {
1569 memcpy(dst, src, tightRowBytes);
1570 src += readDstRowBytes;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001571 if (!flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001572 dst += rowBytes;
1573 } else {
1574 dst -= rowBytes;
1575 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001576 }
1577 }
1578 return true;
1579}
1580
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001581void GrGpuGL::flushRenderTarget(const GrIRect* bound) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001582
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001583 GrGLRenderTarget* rt =
1584 static_cast<GrGLRenderTarget*>(this->drawState()->getRenderTarget());
1585 GrAssert(NULL != rt);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001586
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001587 if (fHWBoundRenderTarget != rt) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001588 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, rt->renderFBOID()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001589 #if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001590 GrGLenum status;
1591 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001592 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
reed@google.comb9255d52011-06-13 18:54:59 +00001593 GrPrintf("GrGpuGL::flushRenderTarget glCheckFramebufferStatus %x\n", status);
reed@google.comac10a2d2010-12-22 21:39:39 +00001594 }
1595 #endif
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001596 fHWBoundRenderTarget = rt;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001597 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.coma3201942012-06-21 19:58:20 +00001598 if (fHWViewport != vp) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001599 vp.pushToGLViewport(this->glInterface());
bsalomon@google.coma3201942012-06-21 19:58:20 +00001600 fHWViewport = vp;
reed@google.comac10a2d2010-12-22 21:39:39 +00001601 }
1602 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001603 if (NULL == bound || !bound->isEmpty()) {
1604 rt->flagAsNeedingResolve(bound);
1605 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001606}
1607
twiz@google.com0f31ca72011-03-18 17:38:11 +00001608GrGLenum gPrimitiveType2GLMode[] = {
1609 GR_GL_TRIANGLES,
1610 GR_GL_TRIANGLE_STRIP,
1611 GR_GL_TRIANGLE_FAN,
1612 GR_GL_POINTS,
1613 GR_GL_LINES,
1614 GR_GL_LINE_STRIP
reed@google.comac10a2d2010-12-22 21:39:39 +00001615};
1616
bsalomon@google.comd302f142011-03-03 13:54:13 +00001617#define SWAP_PER_DRAW 0
1618
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001619#if SWAP_PER_DRAW
bsalomon@google.comd302f142011-03-03 13:54:13 +00001620 #if GR_MAC_BUILD
1621 #include <AGL/agl.h>
1622 #elif GR_WIN32_BUILD
bsalomon@google.comce7357d2012-06-25 17:49:25 +00001623 #include <gl/GL.h>
bsalomon@google.comd302f142011-03-03 13:54:13 +00001624 void SwapBuf() {
1625 DWORD procID = GetCurrentProcessId();
1626 HWND hwnd = GetTopWindow(GetDesktopWindow());
1627 while(hwnd) {
1628 DWORD wndProcID = 0;
1629 GetWindowThreadProcessId(hwnd, &wndProcID);
1630 if(wndProcID == procID) {
1631 SwapBuffers(GetDC(hwnd));
1632 }
1633 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
1634 }
1635 }
1636 #endif
1637#endif
1638
bsalomon@google.com74749cd2013-01-30 16:12:41 +00001639void GrGpuGL::onGpuDraw(const DrawInfo& info) {
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00001640 size_t indexOffsetInBytes;
1641 this->setupGeometry(info, &indexOffsetInBytes);
reed@google.comac10a2d2010-12-22 21:39:39 +00001642
bsalomon@google.com74749cd2013-01-30 16:12:41 +00001643 GrAssert((size_t)info.primitiveType() < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001644
bsalomon@google.com74749cd2013-01-30 16:12:41 +00001645 if (info.isIndexed()) {
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00001646 GrGLvoid* indices =
1647 reinterpret_cast<GrGLvoid*>(indexOffsetInBytes + sizeof(uint16_t) * info.startIndex());
bsalomon@google.com74749cd2013-01-30 16:12:41 +00001648 // info.startVertex() was accounted for by setupGeometry.
1649 GL_CALL(DrawElements(gPrimitiveType2GLMode[info.primitiveType()],
1650 info.indexCount(),
1651 GR_GL_UNSIGNED_SHORT,
1652 indices));
1653 } else {
1654 // Pass 0 for parameter first. We have to adjust glVertexAttribPointer() to account for
1655 // startVertex in the DrawElements case. So we always rely on setupGeometry to have
1656 // accounted for startVertex.
1657 GL_CALL(DrawArrays(gPrimitiveType2GLMode[info.primitiveType()], 0, info.vertexCount()));
1658 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00001659#if SWAP_PER_DRAW
1660 glFlush();
1661 #if GR_MAC_BUILD
1662 aglSwapBuffers(aglGetCurrentContext());
1663 int set_a_break_pt_here = 9;
1664 aglSwapBuffers(aglGetCurrentContext());
1665 #elif GR_WIN32_BUILD
1666 SwapBuf();
1667 int set_a_break_pt_here = 9;
1668 SwapBuf();
1669 #endif
1670#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001671}
1672
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001673namespace {
bsalomon@google.com100abf42012-09-05 17:40:04 +00001674
1675static const uint16_t kOnes16 = static_cast<uint16_t>(~0);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001676const GrStencilSettings& winding_nv_path_stencil_settings() {
1677 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
1678 kIncClamp_StencilOp,
1679 kIncClamp_StencilOp,
1680 kAlwaysIfInClip_StencilFunc,
bsalomon@google.com100abf42012-09-05 17:40:04 +00001681 kOnes16, kOnes16, kOnes16);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001682 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
1683}
1684const GrStencilSettings& even_odd_nv_path_stencil_settings() {
1685 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
1686 kInvert_StencilOp,
1687 kInvert_StencilOp,
1688 kAlwaysIfInClip_StencilFunc,
bsalomon@google.com100abf42012-09-05 17:40:04 +00001689 kOnes16, kOnes16, kOnes16);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001690 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
1691}
1692}
1693
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001694void GrGpuGL::setStencilPathSettings(const GrPath&,
sugoi@google.com12b4e272012-12-06 20:13:11 +00001695 SkPath::FillType fill,
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001696 GrStencilSettings* settings) {
1697 switch (fill) {
sugoi@google.com12b4e272012-12-06 20:13:11 +00001698 case SkPath::kEvenOdd_FillType:
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001699 *settings = even_odd_nv_path_stencil_settings();
1700 return;
sugoi@google.com12b4e272012-12-06 20:13:11 +00001701 case SkPath::kWinding_FillType:
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001702 *settings = winding_nv_path_stencil_settings();
1703 return;
1704 default:
1705 GrCrash("Unexpected path fill.");
1706 }
1707}
1708
sugoi@google.com12b4e272012-12-06 20:13:11 +00001709void GrGpuGL::onGpuStencilPath(const GrPath* path, SkPath::FillType fill) {
bsalomon@google.comf6601872012-08-28 21:11:35 +00001710 GrAssert(fCaps.pathStencilingSupport());
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001711
1712 GrGLuint id = static_cast<const GrGLPath*>(path)->pathID();
1713 GrDrawState* drawState = this->drawState();
1714 GrAssert(NULL != drawState->getRenderTarget());
1715 if (NULL == drawState->getRenderTarget()->getStencilBuffer()) {
1716 return;
1717 }
1718
1719 // Decide how to manipulate the stencil buffer based on the fill rule.
1720 // Also, assert that the stencil settings we set in setStencilPathSettings
1721 // are present.
1722 GrAssert(!fStencilSettings.isTwoSided());
1723 GrGLenum fillMode;
1724 switch (fill) {
sugoi@google.com12b4e272012-12-06 20:13:11 +00001725 case SkPath::kWinding_FillType:
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001726 fillMode = GR_GL_COUNT_UP;
1727 GrAssert(kIncClamp_StencilOp ==
1728 fStencilSettings.passOp(GrStencilSettings::kFront_Face));
1729 GrAssert(kIncClamp_StencilOp ==
1730 fStencilSettings.failOp(GrStencilSettings::kFront_Face));
1731 break;
sugoi@google.com12b4e272012-12-06 20:13:11 +00001732 case SkPath::kEvenOdd_FillType:
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001733 fillMode = GR_GL_INVERT;
1734 GrAssert(kInvert_StencilOp ==
1735 fStencilSettings.passOp(GrStencilSettings::kFront_Face));
1736 GrAssert(kInvert_StencilOp ==
1737 fStencilSettings.failOp(GrStencilSettings::kFront_Face));
1738 break;
1739 default:
1740 // Only the above two fill rules are allowed.
1741 GrCrash("Unexpected path fill.");
bsalomon@google.com548a4332012-07-11 19:45:22 +00001742 return; // suppress unused var warning.
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001743 }
1744 GrGLint writeMask = fStencilSettings.writeMask(GrStencilSettings::kFront_Face);
1745 GL_CALL(StencilFillPath(id, fillMode, writeMask));
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00001746}
1747
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001748void GrGpuGL::onResolveRenderTarget(GrRenderTarget* target) {
1749
1750 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(target);
reed@google.comac10a2d2010-12-22 21:39:39 +00001751
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001752 if (rt->needsResolve()) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001753 GrAssert(GrGLCaps::kNone_MSFBOType != this->glCaps().msFBOType());
reed@google.comac10a2d2010-12-22 21:39:39 +00001754 GrAssert(rt->textureFBOID() != rt->renderFBOID());
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001755 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1756 rt->renderFBOID()));
1757 GL_CALL(BindFramebuffer(GR_GL_DRAW_FRAMEBUFFER,
1758 rt->textureFBOID()));
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001759 // make sure we go through flushRenderTarget() since we've modified
1760 // the bound DRAW FBO ID.
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001761 fHWBoundRenderTarget = NULL;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001762 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001763 const GrIRect dirtyRect = rt->getResolveRect();
1764 GrGLIRect r;
bsalomon@google.coma3201942012-06-21 19:58:20 +00001765 r.setRelativeTo(vp, dirtyRect.fLeft, dirtyRect.fTop,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001766 dirtyRect.width(), dirtyRect.height(), target->origin());
reed@google.comac10a2d2010-12-22 21:39:39 +00001767
bsalomon@google.coma3201942012-06-21 19:58:20 +00001768 GrAutoTRestore<ScissorState> asr;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001769 if (GrGLCaps::kAppleES_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001770 // Apple's extension uses the scissor as the blit bounds.
bsalomon@google.coma3201942012-06-21 19:58:20 +00001771 asr.reset(&fScissorState);
1772 fScissorState.fEnabled = true;
1773 fScissorState.fRect = dirtyRect;
1774 this->flushScissor();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001775 GL_CALL(ResolveMultisampleFramebuffer());
reed@google.comac10a2d2010-12-22 21:39:39 +00001776 } else {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001777 if (GrGLCaps::kDesktopARB_MSFBOType != this->glCaps().msFBOType()) {
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001778 // this respects the scissor during the blit, so disable it.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001779 GrAssert(GrGLCaps::kDesktopEXT_MSFBOType ==
1780 this->glCaps().msFBOType());
bsalomon@google.coma3201942012-06-21 19:58:20 +00001781 asr.reset(&fScissorState);
1782 fScissorState.fEnabled = false;
1783 this->flushScissor();
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001784 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001785 int right = r.fLeft + r.fWidth;
1786 int top = r.fBottom + r.fHeight;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001787 GL_CALL(BlitFramebuffer(r.fLeft, r.fBottom, right, top,
1788 r.fLeft, r.fBottom, right, top,
1789 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001790 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001791 rt->flagAsResolved();
reed@google.comac10a2d2010-12-22 21:39:39 +00001792 }
1793}
1794
bsalomon@google.com411dad02012-06-05 20:24:20 +00001795namespace {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001796
bsalomon@google.com411dad02012-06-05 20:24:20 +00001797GrGLenum gr_to_gl_stencil_func(GrStencilFunc basicFunc) {
1798 static const GrGLenum gTable[] = {
1799 GR_GL_ALWAYS, // kAlways_StencilFunc
1800 GR_GL_NEVER, // kNever_StencilFunc
1801 GR_GL_GREATER, // kGreater_StencilFunc
1802 GR_GL_GEQUAL, // kGEqual_StencilFunc
1803 GR_GL_LESS, // kLess_StencilFunc
1804 GR_GL_LEQUAL, // kLEqual_StencilFunc,
1805 GR_GL_EQUAL, // kEqual_StencilFunc,
1806 GR_GL_NOTEQUAL, // kNotEqual_StencilFunc,
1807 };
1808 GR_STATIC_ASSERT(GR_ARRAY_COUNT(gTable) == kBasicStencilFuncCount);
1809 GR_STATIC_ASSERT(0 == kAlways_StencilFunc);
1810 GR_STATIC_ASSERT(1 == kNever_StencilFunc);
1811 GR_STATIC_ASSERT(2 == kGreater_StencilFunc);
1812 GR_STATIC_ASSERT(3 == kGEqual_StencilFunc);
1813 GR_STATIC_ASSERT(4 == kLess_StencilFunc);
1814 GR_STATIC_ASSERT(5 == kLEqual_StencilFunc);
1815 GR_STATIC_ASSERT(6 == kEqual_StencilFunc);
1816 GR_STATIC_ASSERT(7 == kNotEqual_StencilFunc);
1817 GrAssert((unsigned) basicFunc < kBasicStencilFuncCount);
1818
1819 return gTable[basicFunc];
1820}
1821
1822GrGLenum gr_to_gl_stencil_op(GrStencilOp op) {
1823 static const GrGLenum gTable[] = {
1824 GR_GL_KEEP, // kKeep_StencilOp
1825 GR_GL_REPLACE, // kReplace_StencilOp
1826 GR_GL_INCR_WRAP, // kIncWrap_StencilOp
1827 GR_GL_INCR, // kIncClamp_StencilOp
1828 GR_GL_DECR_WRAP, // kDecWrap_StencilOp
1829 GR_GL_DECR, // kDecClamp_StencilOp
1830 GR_GL_ZERO, // kZero_StencilOp
1831 GR_GL_INVERT, // kInvert_StencilOp
1832 };
1833 GR_STATIC_ASSERT(GR_ARRAY_COUNT(gTable) == kStencilOpCount);
1834 GR_STATIC_ASSERT(0 == kKeep_StencilOp);
1835 GR_STATIC_ASSERT(1 == kReplace_StencilOp);
1836 GR_STATIC_ASSERT(2 == kIncWrap_StencilOp);
1837 GR_STATIC_ASSERT(3 == kIncClamp_StencilOp);
1838 GR_STATIC_ASSERT(4 == kDecWrap_StencilOp);
1839 GR_STATIC_ASSERT(5 == kDecClamp_StencilOp);
1840 GR_STATIC_ASSERT(6 == kZero_StencilOp);
1841 GR_STATIC_ASSERT(7 == kInvert_StencilOp);
1842 GrAssert((unsigned) op < kStencilOpCount);
1843 return gTable[op];
1844}
1845
1846void set_gl_stencil(const GrGLInterface* gl,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001847 const GrStencilSettings& settings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00001848 GrGLenum glFace,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001849 GrStencilSettings::Face grFace) {
1850 GrGLenum glFunc = gr_to_gl_stencil_func(settings.func(grFace));
1851 GrGLenum glFailOp = gr_to_gl_stencil_op(settings.failOp(grFace));
1852 GrGLenum glPassOp = gr_to_gl_stencil_op(settings.passOp(grFace));
1853
1854 GrGLint ref = settings.funcRef(grFace);
1855 GrGLint mask = settings.funcMask(grFace);
1856 GrGLint writeMask = settings.writeMask(grFace);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001857
1858 if (GR_GL_FRONT_AND_BACK == glFace) {
1859 // we call the combined func just in case separate stencil is not
1860 // supported.
1861 GR_GL_CALL(gl, StencilFunc(glFunc, ref, mask));
1862 GR_GL_CALL(gl, StencilMask(writeMask));
1863 GR_GL_CALL(gl, StencilOp(glFailOp, glPassOp, glPassOp));
1864 } else {
1865 GR_GL_CALL(gl, StencilFuncSeparate(glFace, glFunc, ref, mask));
1866 GR_GL_CALL(gl, StencilMaskSeparate(glFace, writeMask));
1867 GR_GL_CALL(gl, StencilOpSeparate(glFace, glFailOp, glPassOp, glPassOp));
1868 }
1869}
1870}
bsalomon@google.comd302f142011-03-03 13:54:13 +00001871
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001872void GrGpuGL::flushStencil(DrawType type) {
1873 if (kStencilPath_DrawType == type) {
1874 GrAssert(!fStencilSettings.isTwoSided());
1875 // Just the func, ref, and mask is set here. The op and write mask are params to the call
1876 // that draws the path to the SB (glStencilFillPath)
1877 GrGLenum func =
1878 gr_to_gl_stencil_func(fStencilSettings.func(GrStencilSettings::kFront_Face));
1879 GL_CALL(PathStencilFunc(func,
1880 fStencilSettings.funcRef(GrStencilSettings::kFront_Face),
1881 fStencilSettings.funcMask(GrStencilSettings::kFront_Face)));
1882 } else if (fHWStencilSettings != fStencilSettings) {
1883 if (fStencilSettings.isDisabled()) {
1884 if (kNo_TriState != fHWStencilTestEnabled) {
1885 GL_CALL(Disable(GR_GL_STENCIL_TEST));
1886 fHWStencilTestEnabled = kNo_TriState;
1887 }
1888 } else {
1889 if (kYes_TriState != fHWStencilTestEnabled) {
1890 GL_CALL(Enable(GR_GL_STENCIL_TEST));
1891 fHWStencilTestEnabled = kYes_TriState;
1892 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001893 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001894 if (!fStencilSettings.isDisabled()) {
bsalomon@google.comf6601872012-08-28 21:11:35 +00001895 if (this->getCaps().twoSidedStencilSupport()) {
bsalomon@google.com411dad02012-06-05 20:24:20 +00001896 set_gl_stencil(this->glInterface(),
bsalomon@google.coma3201942012-06-21 19:58:20 +00001897 fStencilSettings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00001898 GR_GL_FRONT,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001899 GrStencilSettings::kFront_Face);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001900 set_gl_stencil(this->glInterface(),
bsalomon@google.coma3201942012-06-21 19:58:20 +00001901 fStencilSettings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00001902 GR_GL_BACK,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001903 GrStencilSettings::kBack_Face);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001904 } else {
bsalomon@google.com411dad02012-06-05 20:24:20 +00001905 set_gl_stencil(this->glInterface(),
bsalomon@google.coma3201942012-06-21 19:58:20 +00001906 fStencilSettings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00001907 GR_GL_FRONT_AND_BACK,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001908 GrStencilSettings::kFront_Face);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001909 }
1910 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001911 fHWStencilSettings = fStencilSettings;
reed@google.comac10a2d2010-12-22 21:39:39 +00001912 }
1913}
1914
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001915void GrGpuGL::flushAAState(DrawType type) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001916 const GrRenderTarget* rt = this->getDrawState().getRenderTarget();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001917 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001918 // ES doesn't support toggling GL_MULTISAMPLE and doesn't have
1919 // smooth lines.
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001920 // we prefer smooth lines over multisampled lines
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00001921 bool smoothLines = false;
1922
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001923 if (kDrawLines_DrawType == type) {
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00001924 smoothLines = this->willUseHWAALines();
1925 if (smoothLines) {
1926 if (kYes_TriState != fHWAAState.fSmoothLineEnabled) {
1927 GL_CALL(Enable(GR_GL_LINE_SMOOTH));
1928 fHWAAState.fSmoothLineEnabled = kYes_TriState;
1929 // must disable msaa to use line smoothing
1930 if (rt->isMultisampled() &&
1931 kNo_TriState != fHWAAState.fMSAAEnabled) {
1932 GL_CALL(Disable(GR_GL_MULTISAMPLE));
1933 fHWAAState.fMSAAEnabled = kNo_TriState;
1934 }
1935 }
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001936 } else {
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00001937 if (kNo_TriState != fHWAAState.fSmoothLineEnabled) {
1938 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
1939 fHWAAState.fSmoothLineEnabled = kNo_TriState;
1940 }
1941 }
1942 }
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001943 if (!smoothLines && rt->isMultisampled()) {
1944 // FIXME: GL_NV_pr doesn't seem to like MSAA disabled. The paths
1945 // convex hulls of each segment appear to get filled.
1946 bool enableMSAA = kStencilPath_DrawType == type ||
1947 this->getDrawState().isHWAntialiasState();
1948 if (enableMSAA) {
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00001949 if (kYes_TriState != fHWAAState.fMSAAEnabled) {
1950 GL_CALL(Enable(GR_GL_MULTISAMPLE));
1951 fHWAAState.fMSAAEnabled = kYes_TriState;
1952 }
1953 } else {
1954 if (kNo_TriState != fHWAAState.fMSAAEnabled) {
1955 GL_CALL(Disable(GR_GL_MULTISAMPLE));
1956 fHWAAState.fMSAAEnabled = kNo_TriState;
1957 }
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001958 }
1959 }
1960 }
1961}
1962
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00001963void GrGpuGL::flushBlend(bool isLines,
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001964 GrBlendCoeff srcCoeff,
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001965 GrBlendCoeff dstCoeff) {
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00001966 if (isLines && this->willUseHWAALines()) {
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001967 if (kYes_TriState != fHWBlendState.fEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001968 GL_CALL(Enable(GR_GL_BLEND));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001969 fHWBlendState.fEnabled = kYes_TriState;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001970 }
bsalomon@google.com47059542012-06-06 20:51:20 +00001971 if (kSA_GrBlendCoeff != fHWBlendState.fSrcCoeff ||
1972 kISA_GrBlendCoeff != fHWBlendState.fDstCoeff) {
1973 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[kSA_GrBlendCoeff],
1974 gXfermodeCoeff2Blend[kISA_GrBlendCoeff]));
1975 fHWBlendState.fSrcCoeff = kSA_GrBlendCoeff;
1976 fHWBlendState.fDstCoeff = kISA_GrBlendCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001977 }
1978 } else {
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001979 // any optimization to disable blending should
1980 // have already been applied and tweaked the coeffs
1981 // to (1, 0).
bsalomon@google.com47059542012-06-06 20:51:20 +00001982 bool blendOff = kOne_GrBlendCoeff == srcCoeff &&
1983 kZero_GrBlendCoeff == dstCoeff;
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001984 if (blendOff) {
1985 if (kNo_TriState != fHWBlendState.fEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001986 GL_CALL(Disable(GR_GL_BLEND));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001987 fHWBlendState.fEnabled = kNo_TriState;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001988 }
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001989 } else {
1990 if (kYes_TriState != fHWBlendState.fEnabled) {
1991 GL_CALL(Enable(GR_GL_BLEND));
1992 fHWBlendState.fEnabled = kYes_TriState;
1993 }
1994 if (fHWBlendState.fSrcCoeff != srcCoeff ||
1995 fHWBlendState.fDstCoeff != dstCoeff) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001996 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
1997 gXfermodeCoeff2Blend[dstCoeff]));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001998 fHWBlendState.fSrcCoeff = srcCoeff;
1999 fHWBlendState.fDstCoeff = dstCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002000 }
bsalomon@google.coma5d056a2012-03-27 15:59:58 +00002001 GrColor blendConst = this->getDrawState().getBlendConstant();
bsalomon@google.com271cffc2011-05-20 14:13:56 +00002002 if ((BlendCoeffReferencesConstant(srcCoeff) ||
2003 BlendCoeffReferencesConstant(dstCoeff)) &&
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002004 (!fHWBlendState.fConstColorValid ||
2005 fHWBlendState.fConstColor != blendConst)) {
bsalomon@google.com75347472012-09-17 17:23:21 +00002006 GrGLfloat c[4];
2007 GrColorToRGBAFloat(blendConst, c);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002008 GL_CALL(BlendColor(c[0], c[1], c[2], c[3]));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002009 fHWBlendState.fConstColor = blendConst;
2010 fHWBlendState.fConstColorValid = true;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002011 }
2012 }
2013 }
2014}
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002015namespace {
2016
bsalomon@google.com6d003d12012-09-11 15:45:20 +00002017inline void set_tex_swizzle(GrGLenum swizzle[4], const GrGLInterface* gl) {
bsalomon@google.com4d063de2012-05-31 17:59:23 +00002018 GR_GL_CALL(gl, TexParameteriv(GR_GL_TEXTURE_2D,
2019 GR_GL_TEXTURE_SWIZZLE_RGBA,
2020 reinterpret_cast<const GrGLint*>(swizzle)));
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002021}
bsalomon@google.comb8670992012-07-25 21:27:09 +00002022
bsalomon@google.com6c76d242012-09-07 16:52:24 +00002023inline GrGLenum tile_to_gl_wrap(SkShader::TileMode tm) {
bsalomon@google.comb8670992012-07-25 21:27:09 +00002024 static const GrGLenum gWrapModes[] = {
2025 GR_GL_CLAMP_TO_EDGE,
2026 GR_GL_REPEAT,
2027 GR_GL_MIRRORED_REPEAT
2028 };
2029 GrAssert((unsigned) tm <= SK_ARRAY_COUNT(gWrapModes));
2030 GR_STATIC_ASSERT(0 == SkShader::kClamp_TileMode);
2031 GR_STATIC_ASSERT(1 == SkShader::kRepeat_TileMode);
2032 GR_STATIC_ASSERT(2 == SkShader::kMirror_TileMode);
2033 return gWrapModes[tm];
2034}
2035
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002036}
2037
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002038void GrGpuGL::bindTexture(int unitIdx, const GrTextureParams& params, GrGLTexture* texture) {
2039 GrAssert(NULL != texture);
tomhudson@google.comd0c1a062012-07-12 17:23:52 +00002040
bsalomon@google.comb8670992012-07-25 21:27:09 +00002041 // If we created a rt/tex and rendered to it without using a texture and now we're texturing
2042 // 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 +00002043 // out of the "last != next" check.
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002044 GrGLRenderTarget* texRT = static_cast<GrGLRenderTarget*>(texture->asRenderTarget());
bsalomon@google.com4c883782012-06-04 19:05:11 +00002045 if (NULL != texRT) {
2046 this->onResolveRenderTarget(texRT);
2047 }
2048
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002049 if (fHWBoundTextures[unitIdx] != texture) {
2050 this->setTextureUnit(unitIdx);
2051 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, texture->textureID()));
2052 fHWBoundTextures[unitIdx] = texture;
bsalomon@google.com4c883782012-06-04 19:05:11 +00002053 }
2054
bsalomon@google.com4c883782012-06-04 19:05:11 +00002055 ResetTimestamp timestamp;
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002056 const GrGLTexture::TexParams& oldTexParams = texture->getCachedTexParams(&timestamp);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002057 bool setAll = timestamp < this->getResetTimestamp();
2058 GrGLTexture::TexParams newTexParams;
2059
bsalomon@google.comb8670992012-07-25 21:27:09 +00002060 newTexParams.fFilter = params.isBilerp() ? GR_GL_LINEAR : GR_GL_NEAREST;
bsalomon@google.com4c883782012-06-04 19:05:11 +00002061
bsalomon@google.comb8670992012-07-25 21:27:09 +00002062 newTexParams.fWrapS = tile_to_gl_wrap(params.getTileModeX());
2063 newTexParams.fWrapT = tile_to_gl_wrap(params.getTileModeY());
bsalomon@google.com4c883782012-06-04 19:05:11 +00002064 memcpy(newTexParams.fSwizzleRGBA,
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002065 GrGLShaderBuilder::GetTexParamSwizzle(texture->config(), this->glCaps()),
bsalomon@google.com4c883782012-06-04 19:05:11 +00002066 sizeof(newTexParams.fSwizzleRGBA));
2067 if (setAll || newTexParams.fFilter != oldTexParams.fFilter) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002068 this->setTextureUnit(unitIdx);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002069 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2070 GR_GL_TEXTURE_MAG_FILTER,
2071 newTexParams.fFilter));
2072 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2073 GR_GL_TEXTURE_MIN_FILTER,
2074 newTexParams.fFilter));
2075 }
2076 if (setAll || newTexParams.fWrapS != oldTexParams.fWrapS) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002077 this->setTextureUnit(unitIdx);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002078 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2079 GR_GL_TEXTURE_WRAP_S,
2080 newTexParams.fWrapS));
2081 }
2082 if (setAll || newTexParams.fWrapT != oldTexParams.fWrapT) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002083 this->setTextureUnit(unitIdx);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002084 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2085 GR_GL_TEXTURE_WRAP_T,
2086 newTexParams.fWrapT));
2087 }
2088 if (this->glCaps().textureSwizzleSupport() &&
2089 (setAll || memcmp(newTexParams.fSwizzleRGBA,
2090 oldTexParams.fSwizzleRGBA,
2091 sizeof(newTexParams.fSwizzleRGBA)))) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002092 this->setTextureUnit(unitIdx);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002093 set_tex_swizzle(newTexParams.fSwizzleRGBA,
2094 this->glInterface());
2095 }
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002096 texture->setCachedTexParams(newTexParams, this->getResetTimestamp());
bsalomon@google.com4c883782012-06-04 19:05:11 +00002097}
2098
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002099void GrGpuGL::flushMiscFixedFunctionState() {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002100
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002101 const GrDrawState& drawState = this->getDrawState();
reed@google.comac10a2d2010-12-22 21:39:39 +00002102
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002103 if (drawState.isDitherState()) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002104 if (kYes_TriState != fHWDitherEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002105 GL_CALL(Enable(GR_GL_DITHER));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002106 fHWDitherEnabled = kYes_TriState;
2107 }
2108 } else {
2109 if (kNo_TriState != fHWDitherEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002110 GL_CALL(Disable(GR_GL_DITHER));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002111 fHWDitherEnabled = kNo_TriState;
reed@google.comac10a2d2010-12-22 21:39:39 +00002112 }
2113 }
2114
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002115 if (drawState.isColorWriteDisabled()) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002116 if (kNo_TriState != fHWWriteToColor) {
2117 GL_CALL(ColorMask(GR_GL_FALSE, GR_GL_FALSE,
2118 GR_GL_FALSE, GR_GL_FALSE));
2119 fHWWriteToColor = kNo_TriState;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002120 }
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002121 } else {
2122 if (kYes_TriState != fHWWriteToColor) {
2123 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
2124 fHWWriteToColor = kYes_TriState;
2125 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00002126 }
2127
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002128 if (fHWDrawFace != drawState.getDrawFace()) {
bsalomon@google.coma5d056a2012-03-27 15:59:58 +00002129 switch (this->getDrawState().getDrawFace()) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002130 case GrDrawState::kCCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002131 GL_CALL(Enable(GR_GL_CULL_FACE));
2132 GL_CALL(CullFace(GR_GL_BACK));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002133 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002134 case GrDrawState::kCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002135 GL_CALL(Enable(GR_GL_CULL_FACE));
2136 GL_CALL(CullFace(GR_GL_FRONT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002137 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002138 case GrDrawState::kBoth_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002139 GL_CALL(Disable(GR_GL_CULL_FACE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002140 break;
2141 default:
2142 GrCrash("Unknown draw face.");
2143 }
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002144 fHWDrawFace = drawState.getDrawFace();
bsalomon@google.comd302f142011-03-03 13:54:13 +00002145 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002146}
2147
2148void GrGpuGL::notifyVertexBufferBind(const GrGLVertexBuffer* buffer) {
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00002149 fHWGeometryState.setVertexBufferID(buffer->bufferID());
reed@google.comac10a2d2010-12-22 21:39:39 +00002150}
2151
2152void GrGpuGL::notifyVertexBufferDelete(const GrGLVertexBuffer* buffer) {
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00002153 fHWGeometryState.notifyVertexBufferDelete(buffer);
reed@google.comac10a2d2010-12-22 21:39:39 +00002154}
2155
2156void GrGpuGL::notifyIndexBufferBind(const GrGLIndexBuffer* buffer) {
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00002157 fHWGeometryState.setIndexBufferID(buffer->bufferID());
reed@google.comac10a2d2010-12-22 21:39:39 +00002158}
2159
2160void GrGpuGL::notifyIndexBufferDelete(const GrGLIndexBuffer* buffer) {
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00002161 fHWGeometryState.notifyIndexBufferDelete(buffer);
reed@google.comac10a2d2010-12-22 21:39:39 +00002162}
2163
reed@google.comac10a2d2010-12-22 21:39:39 +00002164void GrGpuGL::notifyRenderTargetDelete(GrRenderTarget* renderTarget) {
2165 GrAssert(NULL != renderTarget);
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002166 if (fHWBoundRenderTarget == renderTarget) {
2167 fHWBoundRenderTarget = NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +00002168 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002169}
2170
2171void GrGpuGL::notifyTextureDelete(GrGLTexture* texture) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002172 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002173 if (fHWBoundTextures[s] == texture) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002174 // deleting bound texture does implied bind to 0
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002175 fHWBoundTextures[s] = NULL;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002176 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002177 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002178}
2179
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002180bool GrGpuGL::configToGLFormats(GrPixelConfig config,
2181 bool getSizedInternalFormat,
2182 GrGLenum* internalFormat,
2183 GrGLenum* externalFormat,
2184 GrGLenum* externalType) {
2185 GrGLenum dontCare;
2186 if (NULL == internalFormat) {
2187 internalFormat = &dontCare;
2188 }
2189 if (NULL == externalFormat) {
2190 externalFormat = &dontCare;
2191 }
2192 if (NULL == externalType) {
2193 externalType = &dontCare;
2194 }
2195
reed@google.comac10a2d2010-12-22 21:39:39 +00002196 switch (config) {
bsalomon@google.com0342a852012-08-20 19:22:38 +00002197 case kRGBA_8888_GrPixelConfig:
bsalomon@google.comc4364992011-11-07 15:54:49 +00002198 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002199 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002200 if (getSizedInternalFormat) {
2201 *internalFormat = GR_GL_RGBA8;
2202 } else {
2203 *internalFormat = GR_GL_RGBA;
2204 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002205 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.comc4364992011-11-07 15:54:49 +00002206 break;
bsalomon@google.com0342a852012-08-20 19:22:38 +00002207 case kBGRA_8888_GrPixelConfig:
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002208 if (!this->glCaps().bgraFormatSupport()) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002209 return false;
2210 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002211 if (this->glCaps().bgraIsInternalFormat()) {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002212 if (getSizedInternalFormat) {
2213 *internalFormat = GR_GL_BGRA8;
2214 } else {
2215 *internalFormat = GR_GL_BGRA;
2216 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002217 } else {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002218 if (getSizedInternalFormat) {
2219 *internalFormat = GR_GL_RGBA8;
2220 } else {
2221 *internalFormat = GR_GL_RGBA;
2222 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002223 }
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002224 *externalFormat = GR_GL_BGRA;
bsalomon@google.com6f379512011-11-16 20:36:03 +00002225 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002226 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002227 case kRGB_565_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002228 *internalFormat = GR_GL_RGB;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002229 *externalFormat = GR_GL_RGB;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002230 if (getSizedInternalFormat) {
2231 if (this->glBinding() == kDesktop_GrGLBinding) {
2232 return false;
2233 } else {
2234 *internalFormat = GR_GL_RGB565;
2235 }
2236 } else {
2237 *internalFormat = GR_GL_RGB;
2238 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002239 *externalType = GR_GL_UNSIGNED_SHORT_5_6_5;
reed@google.comac10a2d2010-12-22 21:39:39 +00002240 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002241 case kRGBA_4444_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002242 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002243 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002244 if (getSizedInternalFormat) {
2245 *internalFormat = GR_GL_RGBA4;
2246 } else {
2247 *internalFormat = GR_GL_RGBA;
2248 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002249 *externalType = GR_GL_UNSIGNED_SHORT_4_4_4_4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002250 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002251 case kIndex_8_GrPixelConfig:
bsalomon@google.comf6601872012-08-28 21:11:35 +00002252 if (this->getCaps().eightBitPaletteSupport()) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002253 *internalFormat = GR_GL_PALETTE8_RGBA8;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002254 // glCompressedTexImage doesn't take external params
2255 *externalFormat = GR_GL_PALETTE8_RGBA8;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002256 // no sized/unsized internal format distinction here
2257 *internalFormat = GR_GL_PALETTE8_RGBA8;
2258 // unused with CompressedTexImage
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002259 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002260 } else {
2261 return false;
2262 }
2263 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002264 case kAlpha_8_GrPixelConfig:
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002265 if (this->glCaps().textureRedSupport()) {
2266 *internalFormat = GR_GL_RED;
2267 *externalFormat = GR_GL_RED;
2268 if (getSizedInternalFormat) {
2269 *internalFormat = GR_GL_R8;
2270 } else {
2271 *internalFormat = GR_GL_RED;
2272 }
2273 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002274 } else {
2275 *internalFormat = GR_GL_ALPHA;
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002276 *externalFormat = GR_GL_ALPHA;
2277 if (getSizedInternalFormat) {
2278 *internalFormat = GR_GL_ALPHA8;
2279 } else {
2280 *internalFormat = GR_GL_ALPHA;
2281 }
2282 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002283 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002284 break;
2285 default:
2286 return false;
2287 }
2288 return true;
2289}
2290
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002291void GrGpuGL::setTextureUnit(int unit) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002292 GrAssert(unit >= 0 && unit < GrDrawState::kNumStages);
bsalomon@google.com49209392012-06-05 15:13:46 +00002293 if (fHWActiveTextureUnitIdx != unit) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002294 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com49209392012-06-05 15:13:46 +00002295 fHWActiveTextureUnitIdx = unit;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002296 }
2297}
bsalomon@google.com316f99232011-01-13 21:28:12 +00002298
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002299void GrGpuGL::setSpareTextureUnit() {
bsalomon@google.com49209392012-06-05 15:13:46 +00002300 if (fHWActiveTextureUnitIdx != (GR_GL_TEXTURE0 + SPARE_TEX_UNIT)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002301 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + SPARE_TEX_UNIT));
bsalomon@google.com49209392012-06-05 15:13:46 +00002302 fHWActiveTextureUnitIdx = SPARE_TEX_UNIT;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002303 }
2304}
2305
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00002306GrGLVertexBuffer* GrGpuGL::setBuffers(bool indexed,
2307 size_t* vertexOffsetInBytes,
2308 size_t* indexOffsetInBytes) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002309
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00002310 GrAssert(NULL != vertexOffsetInBytes);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002311
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002312 const GeometryPoolState& geoPoolState = this->getGeomPoolState();
2313
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002314 GrGLVertexBuffer* vbuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002315 switch (this->getGeomSrc().fVertexSrc) {
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00002316 case kBuffer_GeometrySrcType:
2317 *vertexOffsetInBytes = 0;
2318 vbuf = (GrGLVertexBuffer*) this->getGeomSrc().fVertexBuffer;
2319 break;
2320 case kArray_GeometrySrcType:
2321 case kReserved_GeometrySrcType:
2322 this->finalizeReservedVertices();
2323 *vertexOffsetInBytes = geoPoolState.fPoolStartVertex * this->getGeomSrc().fVertexSize;
2324 vbuf = (GrGLVertexBuffer*) geoPoolState.fPoolVertexBuffer;
2325 break;
2326 default:
2327 vbuf = NULL; // suppress warning
2328 GrCrash("Unknown geometry src type!");
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002329 }
2330
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002331 GrAssert(NULL != vbuf);
2332 GrAssert(!vbuf->isLocked());
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00002333 *vertexOffsetInBytes += vbuf->baseOffset();
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002334
2335 if (indexed) {
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00002336 GrAssert(NULL != indexOffsetInBytes);
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002337
2338 GrGLIndexBuffer* ibuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002339 switch (this->getGeomSrc().fIndexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002340 case kBuffer_GeometrySrcType:
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00002341 *indexOffsetInBytes = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002342 ibuf = (GrGLIndexBuffer*)this->getGeomSrc().fIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002343 break;
2344 case kArray_GeometrySrcType:
2345 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002346 this->finalizeReservedIndices();
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00002347 *indexOffsetInBytes = geoPoolState.fPoolStartIndex * sizeof(GrGLushort);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002348 ibuf = (GrGLIndexBuffer*) geoPoolState.fPoolIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002349 break;
2350 default:
2351 ibuf = NULL; // suppress warning
2352 GrCrash("Unknown geometry src type!");
2353 }
2354
2355 GrAssert(NULL != ibuf);
2356 GrAssert(!ibuf->isLocked());
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00002357 if (!fHWGeometryState.isIndexBufferIDBound(ibuf->bufferID())) {
2358 ibuf->bind();
2359 fHWGeometryState.setIndexBufferID(ibuf->bufferID());
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002360 }
2361 }
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00002362 return vbuf;
2363}
2364
2365///////////////////////////////////////////////////////////////////////////////
2366
2367void GrGpuGL::HWGeometryState::AttribArray::set(const GrGpuGL* gpu,
2368 HWGeometryState* geoState,
2369 int index,
2370 GrGLVertexBuffer* buffer,
2371 GrGLint size,
2372 GrGLenum type,
2373 GrGLboolean normalized,
2374 GrGLsizei stride,
2375 GrGLvoid* offset) {
2376 if (!fEnableIsValid || !fEnabled) {
2377 GR_GL_CALL(gpu->glInterface(), EnableVertexAttribArray(index));
2378 fEnableIsValid = true;
2379 fEnabled = true;
2380 }
2381 if (!fAttribPointerIsValid ||
2382 fVertexBufferID != buffer->bufferID() ||
2383 fSize != size ||
2384 fNormalized != normalized ||
2385 fStride != stride ||
2386 offset != fOffset) {
2387
2388 GrGLuint bufferID = buffer->bufferID();
2389 if (!geoState->isVertexBufferIDBound(bufferID)) {
2390 buffer->bind();
2391 geoState->setVertexBufferID(bufferID);
2392 }
2393 GR_GL_CALL(gpu->glInterface(), VertexAttribPointer(index,
2394 size,
2395 type,
2396 normalized,
2397 stride,
2398 offset));
2399 fAttribPointerIsValid = true;
2400 fVertexBufferID = bufferID;
2401 fSize = size;
2402 fNormalized = normalized;
2403 fStride = stride;
2404 fOffset = offset;
2405 }
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002406}