blob: 33e3d6173bb332891906d7ee323536cc66d08cf9 [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
robertphillips@google.com6177e692013-02-28 20:16:25 +0000150GrGpuGL::GrGpuGL(const GrGLContext& ctx, GrContext* context)
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000151 : GrGpu(context)
robertphillips@google.com6177e692013-02-28 20:16:25 +0000152 , fGLContext(ctx) {
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000153
robertphillips@google.com6177e692013-02-28 20:16:25 +0000154 GrAssert(ctx.isInitialized());
tomhudson@google.com747bf292011-06-14 18:16:52 +0000155
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000156 fillInConfigRenderableTable();
157
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000158 fPrintedCaps = false;
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000159
robertphillips@google.com6177e692013-02-28 20:16:25 +0000160 GrGLClearErr(fGLContext.interface());
reed@google.comac10a2d2010-12-22 21:39:39 +0000161
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000162 if (gPrintStartupSpew) {
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000163 const GrGLubyte* ext;
164 GL_CALL_RET(ext, GetString(GR_GL_EXTENSIONS));
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000165 const GrGLubyte* vendor;
166 const GrGLubyte* renderer;
167 const GrGLubyte* version;
168 GL_CALL_RET(vendor, GetString(GR_GL_VENDOR));
169 GL_CALL_RET(renderer, GetString(GR_GL_RENDERER));
170 GL_CALL_RET(version, GetString(GR_GL_VERSION));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000171 GrPrintf("------------------------- create GrGpuGL %p --------------\n",
172 this);
173 GrPrintf("------ VENDOR %s\n", vendor);
174 GrPrintf("------ RENDERER %s\n", renderer);
175 GrPrintf("------ VERSION %s\n", version);
176 GrPrintf("------ EXTENSIONS\n %s \n", ext);
177 }
178
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000179 this->initCaps();
tomhudson@google.com747bf292011-06-14 18:16:52 +0000180
robertphillips@google.com6177e692013-02-28 20:16:25 +0000181 fProgramCache = SkNEW_ARGS(ProgramCache, (this->glContext()));
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000182
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000183 GrAssert(this->glCaps().maxVertexAttributes() >= GrDrawState::kVertexAttribCnt);
184 GrAssert(this->glCaps().maxVertexAttributes() > GrDrawState::kColorOverrideAttribIndexValue);
185 GrAssert(this->glCaps().maxVertexAttributes() > GrDrawState::kCoverageOverrideAttribIndexValue);
186
bsalomon@google.comfe676522011-06-17 18:12:21 +0000187 fLastSuccessfulStencilFmtIdx = 0;
caryclark@google.comcf6285b2012-06-06 12:09:01 +0000188 if (false) { // avoid bit rot, suppress warning
189 fbo_test(this->glInterface(), 0, 0);
190 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000191}
192
193GrGpuGL::~GrGpuGL() {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000194 if (0 != fHWProgramID) {
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000195 // detach the current program so there is no confusion on OpenGL's part
196 // that we want it to be deleted
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +0000197 GrAssert(fHWProgramID == fCurrentProgram->programID());
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000198 GL_CALL(UseProgram(0));
199 }
200
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000201 delete fProgramCache;
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000202
bsalomon@google.com4a018bb2011-10-28 19:50:21 +0000203 // This must be called by before the GrDrawTarget destructor
204 this->releaseGeometry();
bsalomon@google.com15b11df2011-09-16 21:18:29 +0000205 // This subclass must do this before the base class destructor runs
206 // since we will unref the GrGLInterface.
207 this->releaseResources();
reed@google.comac10a2d2010-12-22 21:39:39 +0000208}
209
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000210///////////////////////////////////////////////////////////////////////////////
211
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000212void GrGpuGL::initCaps() {
213 GrGLint maxTextureUnits;
214 // check FS and fixed-function texture unit limits
215 // we only use textures in the fragment stage currently.
216 // checks are > to make sure we have a spare unit.
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000217 const GrGLInterface* gl = this->glInterface();
218 GR_GL_GetIntegerv(gl, GR_GL_MAX_TEXTURE_IMAGE_UNITS, &maxTextureUnits);
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000219 GrAssert(maxTextureUnits > GrDrawState::kNumStages);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000220
bsalomon@google.comf6601872012-08-28 21:11:35 +0000221 CapsInternals* caps = this->capsInternals();
222
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000223 GrGLint numFormats;
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000224 GR_GL_GetIntegerv(gl, GR_GL_NUM_COMPRESSED_TEXTURE_FORMATS, &numFormats);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000225 SkAutoSTMalloc<10, GrGLint> formats(numFormats);
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000226 GR_GL_GetIntegerv(gl, GR_GL_COMPRESSED_TEXTURE_FORMATS, formats);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000227 for (int i = 0; i < numFormats; ++i) {
228 if (formats[i] == GR_GL_PALETTE8_RGBA8) {
bsalomon@google.comf6601872012-08-28 21:11:35 +0000229 caps->f8BitPaletteSupport = true;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000230 break;
231 }
232 }
233
234 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000235 // we could also look for GL_ATI_separate_stencil extension or
236 // GL_EXT_stencil_two_side but they use different function signatures
237 // than GL2.0+ (and than each other).
bsalomon@google.comf6601872012-08-28 21:11:35 +0000238 caps->fTwoSidedStencilSupport = (this->glVersion() >= GR_GL_VER(2,0));
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000239 // supported on GL 1.4 and higher or by extension
bsalomon@google.comf6601872012-08-28 21:11:35 +0000240 caps->fStencilWrapOpsSupport = (this->glVersion() >= GR_GL_VER(1,4)) ||
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000241 this->hasExtension("GL_EXT_stencil_wrap");
242 } else {
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000243 // ES 2 has two sided stencil and stencil wrap
bsalomon@google.comf6601872012-08-28 21:11:35 +0000244 caps->fTwoSidedStencilSupport = true;
245 caps->fStencilWrapOpsSupport = true;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000246 }
247
248 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.comf6601872012-08-28 21:11:35 +0000249 caps->fBufferLockSupport = true; // we require VBO support and the desktop VBO
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000250 // extension includes glMapBuffer.
251 } else {
bsalomon@google.comf6601872012-08-28 21:11:35 +0000252 caps->fBufferLockSupport = this->hasExtension("GL_OES_mapbuffer");
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000253 }
254
255 if (kDesktop_GrGLBinding == this->glBinding()) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000256 if (this->glVersion() >= GR_GL_VER(2,0) ||
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000257 this->hasExtension("GL_ARB_texture_non_power_of_two")) {
bsalomon@google.comf6601872012-08-28 21:11:35 +0000258 caps->fNPOTTextureTileSupport = true;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000259 } else {
bsalomon@google.comf6601872012-08-28 21:11:35 +0000260 caps->fNPOTTextureTileSupport = false;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000261 }
262 } else {
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000263 // Unextended ES2 supports NPOT textures with clamp_to_edge and non-mip filters only
bsalomon@google.comf6601872012-08-28 21:11:35 +0000264 caps->fNPOTTextureTileSupport = this->hasExtension("GL_OES_texture_npot");
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000265 }
266
bsalomon@google.comf6601872012-08-28 21:11:35 +0000267 caps->fHWAALineSupport = (kDesktop_GrGLBinding == this->glBinding());
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000268
bsalomon@google.comf6601872012-08-28 21:11:35 +0000269 GR_GL_GetIntegerv(gl, GR_GL_MAX_TEXTURE_SIZE, &caps->fMaxTextureSize);
270 GR_GL_GetIntegerv(gl, GR_GL_MAX_RENDERBUFFER_SIZE, &caps->fMaxRenderTargetSize);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000271 // Our render targets are always created with textures as the color
272 // attachment, hence this min:
bsalomon@google.comf6601872012-08-28 21:11:35 +0000273 caps->fMaxRenderTargetSize = GrMin(caps->fMaxTextureSize, caps->fMaxRenderTargetSize);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000274
bsalomon@google.comf6601872012-08-28 21:11:35 +0000275 caps->fFSAASupport = GrGLCaps::kNone_MSFBOType != this->glCaps().msFBOType();
276 caps->fPathStencilingSupport = GR_GL_USE_NV_PATH_RENDERING &&
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000277 this->hasExtension("GL_NV_path_rendering");
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000278
279 // Enable supported shader-related caps
280 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000281 caps->fDualSourceBlendingSupport = this->glVersion() >= GR_GL_VER(3,3) ||
282 this->hasExtension("GL_ARB_blend_func_extended");
bsalomon@google.comf6601872012-08-28 21:11:35 +0000283 caps->fShaderDerivativeSupport = true;
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000284 // we don't support GL_ARB_geometry_shader4, just GL 3.2+ GS
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000285 caps->fGeometryShaderSupport = this->glVersion() >= GR_GL_VER(3,2) &&
286 this->glslGeneration() >= k150_GrGLSLGeneration;
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000287 } else {
bsalomon@google.comf6601872012-08-28 21:11:35 +0000288 caps->fShaderDerivativeSupport =
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000289 this->hasExtension("GL_OES_standard_derivatives");
290 }
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000291
292 if (GrGLCaps::kNone_MSFBOType != this->glCaps().msFBOType()) {
293 GR_GL_GetIntegerv(this->glInterface(), GR_GL_MAX_SAMPLES, &caps->fMaxSampleCount);
294 }
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000295}
296
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000297void GrGpuGL::fillInConfigRenderableTable() {
298
299 // OpenGL < 3.0
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000300 // no support for render targets unless the GL_ARB_framebuffer_object
301 // extension is supported (in which case we get ALPHA, RED, RG, RGB,
302 // RGBA (ALPHA8, RGBA4, RGBA8) for OpenGL > 1.1). Note that we
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000303 // probably don't get R8 in this case.
304
305 // OpenGL 3.0
306 // base color renderable: ALPHA, RED, RG, RGB, and RGBA
307 // sized derivatives: ALPHA8, R8, RGBA4, RGBA8
308
309 // >= OpenGL 3.1
310 // base color renderable: RED, RG, RGB, and RGBA
311 // sized derivatives: R8, RGBA4, RGBA8
312 // if the GL_ARB_compatibility extension is supported then we get back
313 // support for GL_ALPHA and ALPHA8
314
315 // GL_EXT_bgra adds BGRA render targets to any version
316
317 // ES 2.0
318 // color renderable: RGBA4, RGB5_A1, RGB565
319 // GL_EXT_texture_rg adds support for R8 as a color render target
320 // GL_OES_rgb8_rgba8 and/or GL_ARM_rgba8 adds support for RGBA8
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000321 // GL_EXT_texture_format_BGRA8888 and/or GL_APPLE_texture_format_BGRA8888 added BGRA support
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000322
323 if (kDesktop_GrGLBinding == this->glBinding()) {
324 // Post 3.0 we will get R8
325 // Prior to 3.0 we will get ALPHA8 (with GL_ARB_framebuffer_object)
326 if (this->glVersion() >= GR_GL_VER(3,0) ||
327 this->hasExtension("GL_ARB_framebuffer_object")) {
328 fConfigRenderSupport[kAlpha_8_GrPixelConfig] = true;
329 }
330 } else {
331 // On ES we can only hope for R8
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000332 fConfigRenderSupport[kAlpha_8_GrPixelConfig] =
robertphillips@google.com443e5a52012-04-30 20:01:21 +0000333 this->glCaps().textureRedSupport();
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000334 }
335
336 if (kDesktop_GrGLBinding != this->glBinding()) {
337 // only available in ES
338 fConfigRenderSupport[kRGB_565_GrPixelConfig] = true;
339 }
340
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000341 // Pre 3.0, Ganesh relies on either GL_ARB_framebuffer_object or
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000342 // GL_EXT_framebuffer_object for FBO support. Both of these
343 // allow RGBA4 render targets so this is always supported.
344 fConfigRenderSupport[kRGBA_4444_GrPixelConfig] = true;
345
346 if (this->glCaps().rgba8RenderbufferSupport()) {
bsalomon@google.com0342a852012-08-20 19:22:38 +0000347 fConfigRenderSupport[kRGBA_8888_GrPixelConfig] = true;
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000348 }
349
350 if (this->glCaps().bgraFormatSupport()) {
bsalomon@google.com0342a852012-08-20 19:22:38 +0000351 fConfigRenderSupport[kBGRA_8888_GrPixelConfig] = true;
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000352 }
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000353}
354
bsalomon@google.com9c680582013-02-06 18:17:50 +0000355namespace {
356GrPixelConfig preferred_pixel_ops_config(GrPixelConfig config) {
357 if (GR_GL_RGBA_8888_PIXEL_OPS_SLOW && kRGBA_8888_GrPixelConfig == config) {
358 return kBGRA_8888_GrPixelConfig;
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000359 } else {
360 return config;
361 }
362}
bsalomon@google.com9c680582013-02-06 18:17:50 +0000363}
364
365GrPixelConfig GrGpuGL::preferredReadPixelsConfig(GrPixelConfig config) const {
366 return preferred_pixel_ops_config(config);
367}
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000368
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000369GrPixelConfig GrGpuGL::preferredWritePixelsConfig(GrPixelConfig config) const {
bsalomon@google.com9c680582013-02-06 18:17:50 +0000370 return preferred_pixel_ops_config(config);
371}
372
373bool GrGpuGL::canWriteTexturePixels(const GrTexture* texture, GrPixelConfig srcConfig) const {
374 if (kIndex_8_GrPixelConfig == srcConfig || kIndex_8_GrPixelConfig == texture->config()) {
375 return false;
376 }
377 if (srcConfig != texture->config() && kES2_GrGLBinding == this->glBinding()) {
378 // In general ES2 requires the internal format of the texture and the format of the src
379 // pixels to match. However, It may or may not be possible to upload BGRA data to a RGBA
380 // texture. It depends upon which extension added BGRA. The Apple extension allows it
381 // (BGRA's internal format is RGBA) while the EXT extension does not (BGRA is its own
382 // internal format).
383 if (this->glCaps().bgraFormatSupport() &&
384 !this->glCaps().bgraIsInternalFormat() &&
385 kBGRA_8888_GrPixelConfig == srcConfig &&
386 kRGBA_8888_GrPixelConfig == texture->config()) {
387 return true;
388 } else {
389 return false;
390 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000391 } else {
bsalomon@google.com9c680582013-02-06 18:17:50 +0000392 return true;
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000393 }
394}
395
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000396bool GrGpuGL::fullReadPixelsIsFasterThanPartial() const {
397 return SkToBool(GR_GL_FULL_READPIXELS_FASTER_THAN_PARTIAL);
398}
399
bsalomon@google.com1bf1c212011-11-05 12:18:58 +0000400void GrGpuGL::onResetContext() {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000401 if (gPrintStartupSpew && !fPrintedCaps) {
402 fPrintedCaps = true;
403 this->getCaps().print();
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000404 this->glCaps().print();
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000405 }
406
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000407 // we don't use the zb at all
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000408 GL_CALL(Disable(GR_GL_DEPTH_TEST));
409 GL_CALL(DepthMask(GR_GL_FALSE));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000410
bsalomon@google.com978c8c62012-05-21 14:45:49 +0000411 fHWDrawFace = GrDrawState::kInvalid_DrawFace;
412 fHWDitherEnabled = kUnknown_TriState;
reed@google.comac10a2d2010-12-22 21:39:39 +0000413
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000414 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com469d0dd2012-05-21 20:14:29 +0000415 // Desktop-only state that we never change
bsalomon@google.com2b1b8c02013-02-28 22:06:02 +0000416 if (!this->glCaps().isCoreProfile()) {
417 GL_CALL(Disable(GR_GL_POINT_SMOOTH));
418 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
419 GL_CALL(Disable(GR_GL_POLYGON_SMOOTH));
420 GL_CALL(Disable(GR_GL_POLYGON_STIPPLE));
421 GL_CALL(Disable(GR_GL_COLOR_LOGIC_OP));
422 GL_CALL(Disable(GR_GL_INDEX_LOGIC_OP));
423 }
bsalomon@google.come76b7cc2012-06-18 12:47:06 +0000424 if (this->glCaps().imagingSupport()) {
bsalomon@google.com6918d482013-03-07 19:09:11 +0000425 // This produces a GL error on the windows NVIDIA driver when using a core profile but
426 // I think that is a driver bug since GL_ARB_imaging is in the extension string.
bsalomon@google.come76b7cc2012-06-18 12:47:06 +0000427 GL_CALL(Disable(GR_GL_COLOR_TABLE));
428 }
bsalomon@google.com469d0dd2012-05-21 20:14:29 +0000429 GL_CALL(Disable(GR_GL_POLYGON_OFFSET_FILL));
430 // Since ES doesn't support glPointSize at all we always use the VS to
431 // set the point size
432 GL_CALL(Enable(GR_GL_VERTEX_PROGRAM_POINT_SIZE));
433
434 // We should set glPolygonMode(FRONT_AND_BACK,FILL) here, too. It isn't
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000435 // currently part of our gl interface. There are probably others as
bsalomon@google.com469d0dd2012-05-21 20:14:29 +0000436 // well.
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000437 }
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +0000438 fHWAAState.invalidate();
bsalomon@google.com978c8c62012-05-21 14:45:49 +0000439 fHWWriteToColor = kUnknown_TriState;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000440
reed@google.comac10a2d2010-12-22 21:39:39 +0000441 // we only ever use lines in hairline mode
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000442 GL_CALL(LineWidth(1));
reed@google.comac10a2d2010-12-22 21:39:39 +0000443
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000444 // invalid
bsalomon@google.com49209392012-06-05 15:13:46 +0000445 fHWActiveTextureUnitIdx = -1;
reed@google.comac10a2d2010-12-22 21:39:39 +0000446
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +0000447 fHWBlendState.invalidate();
bsalomon@google.com080773c2011-03-15 19:09:25 +0000448
tomhudson@google.com93813632011-10-27 20:21:16 +0000449 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.comc811ea32012-05-21 15:33:09 +0000450 fHWBoundTextures[s] = NULL;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000451 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000452
bsalomon@google.coma3201942012-06-21 19:58:20 +0000453 fHWScissorSettings.invalidate();
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000454
bsalomon@google.coma3201942012-06-21 19:58:20 +0000455 fHWViewport.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000456
bsalomon@google.com457b8a32012-05-21 21:19:58 +0000457 fHWStencilSettings.invalidate();
bsalomon@google.coma3201942012-06-21 19:58:20 +0000458 fHWStencilTestEnabled = kUnknown_TriState;
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000459
bsalomon@google.com880b8fc2013-02-19 20:17:28 +0000460 fHWGeometryState.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000461
bsalomon@google.comc811ea32012-05-21 15:33:09 +0000462 fHWBoundRenderTarget = NULL;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000463
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +0000464 fHWPathStencilMatrixState.invalidate();
bsalomon@google.comf6601872012-08-28 21:11:35 +0000465 if (fCaps.pathStencilingSupport()) {
bsalomon@google.com05a718c2012-06-29 14:01:53 +0000466 // we don't use the model view matrix.
467 GL_CALL(MatrixMode(GR_GL_MODELVIEW));
468 GL_CALL(LoadIdentity());
469 }
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000470
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000471 // we assume these values
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000472 if (this->glCaps().unpackRowLengthSupport()) {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000473 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
474 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000475 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000476 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
477 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000478 if (this->glCaps().unpackFlipYSupport()) {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000479 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
480 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000481 if (this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000482 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, GR_GL_FALSE));
483 }
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000484
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000485 fHWProgramID = 0;
bsalomon@google.com91207482013-02-12 21:45:24 +0000486 fSharedGLProgramState.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000487}
488
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000489namespace {
490
491GrSurfaceOrigin resolve_origin(GrSurfaceOrigin origin, bool renderTarget) {
492 // By default, GrRenderTargets are GL's normal orientation so that they
493 // can be drawn to by the outside world without the client having
494 // to render upside down.
495 if (kDefault_GrSurfaceOrigin == origin) {
496 return renderTarget ? kBottomLeft_GrSurfaceOrigin : kTopLeft_GrSurfaceOrigin;
497 } else {
498 return origin;
499 }
500}
501
502}
503
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000504GrTexture* GrGpuGL::onWrapBackendTexture(const GrBackendTextureDesc& desc) {
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000505 if (!this->configToGLFormats(desc.fConfig, false, NULL, NULL, NULL)) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000506 return NULL;
507 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000508
robertphillips@google.comb72e5d32012-10-30 15:18:10 +0000509 if (0 == desc.fTextureHandle) {
510 return NULL;
511 }
512
513 int maxSize = this->getCaps().maxTextureSize();
514 if (desc.fWidth > maxSize || desc.fHeight > maxSize) {
515 return NULL;
516 }
517
518 GrGLTexture::Desc glTexDesc;
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000519 // next line relies on GrBackendTextureDesc's flags matching GrTexture's
robertphillips@google.com32716282012-06-04 12:48:45 +0000520 glTexDesc.fFlags = (GrTextureFlags) desc.fFlags;
bsalomon@google.com99621082011-11-15 16:47:16 +0000521 glTexDesc.fWidth = desc.fWidth;
522 glTexDesc.fHeight = desc.fHeight;
bsalomon@google.come269f212011-11-07 13:29:52 +0000523 glTexDesc.fConfig = desc.fConfig;
robertphillips@google.com32716282012-06-04 12:48:45 +0000524 glTexDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.come269f212011-11-07 13:29:52 +0000525 glTexDesc.fTextureID = static_cast<GrGLuint>(desc.fTextureHandle);
bsalomon@google.com72830222013-01-23 20:25:22 +0000526 glTexDesc.fIsWrapped = true;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000527 bool renderTarget = 0 != (desc.fFlags & kRenderTarget_GrBackendTextureFlag);
528 // FIXME: this should be calling resolve_origin(), but Chrome code is currently
529 // assuming the old behaviour, which is that backend textures are always
530 // BottomLeft, even for non-RT's. Once Chrome is fixed, change this to:
531 // glTexDesc.fOrigin = resolve_origin(desc.fOrigin, renderTarget);
532 if (kDefault_GrSurfaceOrigin == desc.fOrigin) {
533 glTexDesc.fOrigin = kBottomLeft_GrSurfaceOrigin;
534 } else {
535 glTexDesc.fOrigin = desc.fOrigin;
536 }
bsalomon@google.come269f212011-11-07 13:29:52 +0000537
538 GrGLTexture* texture = NULL;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000539 if (renderTarget) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000540 GrGLRenderTarget::Desc glRTDesc;
541 glRTDesc.fRTFBOID = 0;
542 glRTDesc.fTexFBOID = 0;
543 glRTDesc.fMSColorRenderbufferID = 0;
bsalomon@google.come269f212011-11-07 13:29:52 +0000544 glRTDesc.fConfig = desc.fConfig;
545 glRTDesc.fSampleCnt = desc.fSampleCnt;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000546 glRTDesc.fOrigin = glTexDesc.fOrigin;
bsalomon@google.com99621082011-11-15 16:47:16 +0000547 if (!this->createRenderTargetObjects(glTexDesc.fWidth,
548 glTexDesc.fHeight,
bsalomon@google.come269f212011-11-07 13:29:52 +0000549 glTexDesc.fTextureID,
550 &glRTDesc)) {
551 return NULL;
552 }
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000553 texture = SkNEW_ARGS(GrGLTexture, (this, glTexDesc, glRTDesc));
bsalomon@google.come269f212011-11-07 13:29:52 +0000554 } else {
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000555 texture = SkNEW_ARGS(GrGLTexture, (this, glTexDesc));
bsalomon@google.come269f212011-11-07 13:29:52 +0000556 }
557 if (NULL == texture) {
558 return NULL;
559 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000560
bsalomon@google.come269f212011-11-07 13:29:52 +0000561 this->setSpareTextureUnit();
562 return texture;
563}
564
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000565GrRenderTarget* GrGpuGL::onWrapBackendRenderTarget(const GrBackendRenderTargetDesc& desc) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000566 GrGLRenderTarget::Desc glDesc;
567 glDesc.fConfig = desc.fConfig;
568 glDesc.fRTFBOID = static_cast<GrGLuint>(desc.fRenderTargetHandle);
569 glDesc.fMSColorRenderbufferID = 0;
570 glDesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
571 glDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.com72830222013-01-23 20:25:22 +0000572 glDesc.fIsWrapped = true;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000573 glDesc.fOrigin = desc.fOrigin;
574 if (glDesc.fRTFBOID == 0) {
575 GrAssert(desc.fOrigin == kBottomLeft_GrSurfaceOrigin);
576 }
577
578 glDesc.fOrigin = resolve_origin(desc.fOrigin, true);
bsalomon@google.come269f212011-11-07 13:29:52 +0000579 GrGLIRect viewport;
580 viewport.fLeft = 0;
581 viewport.fBottom = 0;
582 viewport.fWidth = desc.fWidth;
583 viewport.fHeight = desc.fHeight;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000584
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000585 GrRenderTarget* tgt = SkNEW_ARGS(GrGLRenderTarget,
586 (this, glDesc, viewport));
bsalomon@google.come269f212011-11-07 13:29:52 +0000587 if (desc.fStencilBits) {
588 GrGLStencilBuffer::Format format;
589 format.fInternalFormat = GrGLStencilBuffer::kUnknownInternalFormat;
590 format.fPacked = false;
591 format.fStencilBits = desc.fStencilBits;
592 format.fTotalBits = desc.fStencilBits;
bsalomon@google.com1f0f1a32013-01-23 21:32:32 +0000593 static const bool kIsSBWrapped = false;
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000594 GrGLStencilBuffer* sb = SkNEW_ARGS(GrGLStencilBuffer,
595 (this,
bsalomon@google.com1f0f1a32013-01-23 21:32:32 +0000596 kIsSBWrapped,
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000597 0,
598 desc.fWidth,
599 desc.fHeight,
600 desc.fSampleCnt,
601 format));
bsalomon@google.come269f212011-11-07 13:29:52 +0000602 tgt->setStencilBuffer(sb);
603 sb->unref();
604 }
605 return tgt;
606}
607
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000608////////////////////////////////////////////////////////////////////////////////
609
bsalomon@google.com9c680582013-02-06 18:17:50 +0000610bool GrGpuGL::onWriteTexturePixels(GrTexture* texture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000611 int left, int top, int width, int height,
612 GrPixelConfig config, const void* buffer,
613 size_t rowBytes) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000614 if (NULL == buffer) {
bsalomon@google.com9c680582013-02-06 18:17:50 +0000615 return false;
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000616 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000617 GrGLTexture* glTex = static_cast<GrGLTexture*>(texture);
618
bsalomon@google.com6f379512011-11-16 20:36:03 +0000619 this->setSpareTextureUnit();
620 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTex->textureID()));
621 GrGLTexture::Desc desc;
robertphillips@google.com32716282012-06-04 12:48:45 +0000622 desc.fFlags = glTex->desc().fFlags;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000623 desc.fWidth = glTex->width();
624 desc.fHeight = glTex->height();
robertphillips@google.com32716282012-06-04 12:48:45 +0000625 desc.fConfig = glTex->config();
626 desc.fSampleCnt = glTex->desc().fSampleCnt;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000627 desc.fTextureID = glTex->textureID();
bsalomon@google.com2d0bade2012-10-26 19:01:17 +0000628 desc.fOrigin = glTex->origin();
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000629
bsalomon@google.com9c680582013-02-06 18:17:50 +0000630 return this->uploadTexData(desc, false,
631 left, top, width, height,
632 config, buffer, rowBytes);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000633}
634
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000635namespace {
636bool adjust_pixel_ops_params(int surfaceWidth,
637 int surfaceHeight,
638 size_t bpp,
639 int* left, int* top, int* width, int* height,
640 const void** data,
641 size_t* rowBytes) {
642 if (!*rowBytes) {
643 *rowBytes = *width * bpp;
644 }
645
646 GrIRect subRect = GrIRect::MakeXYWH(*left, *top, *width, *height);
647 GrIRect bounds = GrIRect::MakeWH(surfaceWidth, surfaceHeight);
648
649 if (!subRect.intersect(bounds)) {
650 return false;
651 }
652 *data = reinterpret_cast<const void*>(reinterpret_cast<intptr_t>(*data) +
653 (subRect.fTop - *top) * *rowBytes + (subRect.fLeft - *left) * bpp);
654
655 *left = subRect.fLeft;
656 *top = subRect.fTop;
657 *width = subRect.width();
658 *height = subRect.height();
659 return true;
660}
661}
662
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000663bool GrGpuGL::uploadTexData(const GrGLTexture::Desc& desc,
664 bool isNewTexture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000665 int left, int top, int width, int height,
666 GrPixelConfig dataConfig,
667 const void* data,
668 size_t rowBytes) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000669 GrAssert(NULL != data || isNewTexture);
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000670
671 size_t bpp = GrBytesPerPixel(dataConfig);
672 if (!adjust_pixel_ops_params(desc.fWidth, desc.fHeight, bpp, &left, &top,
673 &width, &height, &data, &rowBytes)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000674 return false;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000675 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000676 size_t trimRowBytes = width * bpp;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000677
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000678 // in case we need a temporary, trimmed copy of the src pixels
679 SkAutoSMalloc<128 * 128> tempStorage;
680
bsalomon@google.com313f0192012-07-10 17:21:02 +0000681 // paletted textures cannot be partially updated
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000682 bool useTexStorage = isNewTexture &&
bsalomon@google.com313f0192012-07-10 17:21:02 +0000683 desc.fConfig != kIndex_8_GrPixelConfig &&
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000684 this->glCaps().texStorageSupport();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000685
bsalomon@google.com313f0192012-07-10 17:21:02 +0000686 if (useTexStorage && kDesktop_GrGLBinding == this->glBinding()) {
687 // 565 is not a sized internal format on desktop GL. So on desktop with
688 // 565 we always use an unsized internal format to let the system pick
689 // the best sized format to convert the 565 data to. Since TexStorage
690 // only allows sized internal formats we will instead use TexImage2D.
691 useTexStorage = desc.fConfig != kRGB_565_GrPixelConfig;
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000692 }
693
694 GrGLenum internalFormat;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000695 GrGLenum externalFormat;
696 GrGLenum externalType;
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000697 // glTexStorage requires sized internal formats on both desktop and ES. ES
698 // glTexImage requires an unsized format.
699 if (!this->configToGLFormats(dataConfig, useTexStorage, &internalFormat,
700 &externalFormat, &externalType)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000701 return false;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000702 }
703
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000704 if (!isNewTexture && GR_GL_PALETTE8_RGBA8 == internalFormat) {
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000705 // paletted textures cannot be updated
706 return false;
707 }
708
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000709 /*
bsalomon@google.com6f379512011-11-16 20:36:03 +0000710 * check whether to allocate a temporary buffer for flipping y or
711 * because our srcData has extra bytes past each row. If so, we need
712 * to trim those off here, since GL ES may not let us specify
713 * GL_UNPACK_ROW_LENGTH.
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000714 */
bsalomon@google.com6f379512011-11-16 20:36:03 +0000715 bool restoreGLRowLength = false;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000716 bool swFlipY = false;
717 bool glFlipY = false;
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000718 if (NULL != data) {
senorblanco@chromium.orgef5dbe12013-01-28 16:42:38 +0000719 if (kBottomLeft_GrSurfaceOrigin == desc.fOrigin) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000720 if (this->glCaps().unpackFlipYSupport()) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000721 glFlipY = true;
722 } else {
723 swFlipY = true;
724 }
725 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000726 if (this->glCaps().unpackRowLengthSupport() && !swFlipY) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000727 // can't use this for flipping, only non-neg values allowed. :(
728 if (rowBytes != trimRowBytes) {
729 GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
730 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength));
731 restoreGLRowLength = true;
732 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000733 } else {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000734 if (trimRowBytes != rowBytes || swFlipY) {
735 // copy data into our new storage, skipping the trailing bytes
736 size_t trimSize = height * trimRowBytes;
737 const char* src = (const char*)data;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000738 if (swFlipY) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000739 src += (height - 1) * rowBytes;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000740 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000741 char* dst = (char*)tempStorage.reset(trimSize);
742 for (int y = 0; y < height; y++) {
743 memcpy(dst, src, trimRowBytes);
744 if (swFlipY) {
745 src -= rowBytes;
746 } else {
747 src += rowBytes;
748 }
749 dst += trimRowBytes;
750 }
751 // now point data to our copied version
752 data = tempStorage.get();
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000753 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000754 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000755 if (glFlipY) {
756 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_TRUE));
757 }
758 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, static_cast<GrGLint>(bpp)));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000759 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000760 bool succeeded = true;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000761 if (isNewTexture &&
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000762 0 == left && 0 == top &&
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000763 desc.fWidth == width && desc.fHeight == height) {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000764 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000765 if (useTexStorage) {
766 // We never resize or change formats of textures. We don't use
767 // mipmaps currently.
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000768 GL_ALLOC_CALL(this->glInterface(),
769 TexStorage2D(GR_GL_TEXTURE_2D,
770 1, // levels
771 internalFormat,
772 desc.fWidth, desc.fHeight));
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000773 } else {
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000774 if (GR_GL_PALETTE8_RGBA8 == internalFormat) {
775 GrGLsizei imageSize = desc.fWidth * desc.fHeight +
776 kGrColorTableSize;
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000777 GL_ALLOC_CALL(this->glInterface(),
778 CompressedTexImage2D(GR_GL_TEXTURE_2D,
779 0, // level
780 internalFormat,
781 desc.fWidth, desc.fHeight,
782 0, // border
783 imageSize,
784 data));
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000785 } else {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000786 GL_ALLOC_CALL(this->glInterface(),
787 TexImage2D(GR_GL_TEXTURE_2D,
788 0, // level
789 internalFormat,
790 desc.fWidth, desc.fHeight,
791 0, // border
792 externalFormat, externalType,
793 data));
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000794 }
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000795 }
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000796 GrGLenum error = CHECK_ALLOC_ERROR(this->glInterface());
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000797 if (error != GR_GL_NO_ERROR) {
798 succeeded = false;
799 } else {
800 // if we have data and we used TexStorage to create the texture, we
801 // now upload with TexSubImage.
802 if (NULL != data && useTexStorage) {
803 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D,
804 0, // level
805 left, top,
806 width, height,
807 externalFormat, externalType,
808 data));
809 }
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000810 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000811 } else {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000812 if (swFlipY || glFlipY) {
bsalomon@google.com6f379512011-11-16 20:36:03 +0000813 top = desc.fHeight - (top + height);
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000814 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000815 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D,
816 0, // level
817 left, top,
818 width, height,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000819 externalFormat, externalType, data));
820 }
821
822 if (restoreGLRowLength) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000823 GrAssert(this->glCaps().unpackRowLengthSupport());
bsalomon@google.com6f379512011-11-16 20:36:03 +0000824 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000825 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000826 if (glFlipY) {
827 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
828 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000829 return succeeded;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000830}
831
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000832namespace {
robertphillips@google.com6177e692013-02-28 20:16:25 +0000833bool renderbuffer_storage_msaa(GrGLContext& ctx,
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000834 int sampleCount,
835 GrGLenum format,
836 int width, int height) {
robertphillips@google.com6177e692013-02-28 20:16:25 +0000837 CLEAR_ERROR_BEFORE_ALLOC(ctx.interface());
838 GrAssert(GrGLCaps::kNone_MSFBOType != ctx.info().caps().msFBOType());
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000839 bool created = false;
840 if (GrGLCaps::kNVDesktop_CoverageAAType ==
robertphillips@google.com6177e692013-02-28 20:16:25 +0000841 ctx.info().caps().coverageAAType()) {
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000842 const GrGLCaps::MSAACoverageMode& mode =
robertphillips@google.com6177e692013-02-28 20:16:25 +0000843 ctx.info().caps().getMSAACoverageMode(sampleCount);
844 GL_ALLOC_CALL(ctx.interface(),
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000845 RenderbufferStorageMultisampleCoverage(GR_GL_RENDERBUFFER,
846 mode.fCoverageSampleCnt,
847 mode.fColorSampleCnt,
848 format,
849 width, height));
robertphillips@google.com6177e692013-02-28 20:16:25 +0000850 created = (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctx.interface()));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000851 }
852 if (!created) {
robertphillips@google.com6177e692013-02-28 20:16:25 +0000853 GL_ALLOC_CALL(ctx.interface(),
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000854 RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
855 sampleCount,
856 format,
857 width, height));
robertphillips@google.com6177e692013-02-28 20:16:25 +0000858 created = (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctx.interface()));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000859 }
860 return created;
861}
862}
863
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000864bool GrGpuGL::createRenderTargetObjects(int width, int height,
865 GrGLuint texID,
866 GrGLRenderTarget::Desc* desc) {
867 desc->fMSColorRenderbufferID = 0;
868 desc->fRTFBOID = 0;
869 desc->fTexFBOID = 0;
bsalomon@google.com72830222013-01-23 20:25:22 +0000870 desc->fIsWrapped = false;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000871
872 GrGLenum status;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000873
bsalomon@google.comab15d612011-08-09 12:57:56 +0000874 GrGLenum msColorFormat = 0; // suppress warning
875
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000876 GL_CALL(GenFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000877 if (!desc->fTexFBOID) {
878 goto FAILED;
879 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000880
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000881
882 // If we are using multisampling we will create two FBOS. We render
883 // to one and then resolve to the texture bound to the other.
bsalomon@google.come269f212011-11-07 13:29:52 +0000884 if (desc->fSampleCnt > 0) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000885 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000886 goto FAILED;
887 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000888 GL_CALL(GenFramebuffers(1, &desc->fRTFBOID));
889 GL_CALL(GenRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000890 if (!desc->fRTFBOID ||
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000891 !desc->fMSColorRenderbufferID ||
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000892 !this->configToGLFormats(desc->fConfig,
893 // GLES requires sized internal formats
894 kES2_GrGLBinding == this->glBinding(),
895 &msColorFormat, NULL, NULL)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000896 goto FAILED;
897 }
898 } else {
899 desc->fRTFBOID = desc->fTexFBOID;
900 }
901
bsalomon@google.com0e9b41a2012-01-04 22:11:43 +0000902 // below here we may bind the FBO
bsalomon@google.comc811ea32012-05-21 15:33:09 +0000903 fHWBoundRenderTarget = NULL;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000904 if (desc->fRTFBOID != desc->fTexFBOID) {
bsalomon@google.com13ac3a32013-01-09 23:29:39 +0000905 GrAssert(desc->fSampleCnt > 0);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000906 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000907 desc->fMSColorRenderbufferID));
robertphillips@google.com6177e692013-02-28 20:16:25 +0000908 if (!renderbuffer_storage_msaa(fGLContext,
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000909 desc->fSampleCnt,
910 msColorFormat,
911 width, height)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000912 goto FAILED;
913 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000914 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fRTFBOID));
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000915 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000916 GR_GL_COLOR_ATTACHMENT0,
917 GR_GL_RENDERBUFFER,
918 desc->fMSColorRenderbufferID));
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000919 if (!this->glCaps().isConfigVerifiedColorAttachment(desc->fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000920 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
921 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
922 goto FAILED;
923 }
robertphillips@google.com6177e692013-02-28 20:16:25 +0000924 fGLContext.info().caps().markConfigAsValidColorAttachment(
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000925 desc->fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000926 }
927 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000928 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000929
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000930 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
931 GR_GL_COLOR_ATTACHMENT0,
932 GR_GL_TEXTURE_2D,
933 texID, 0));
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000934 if (!this->glCaps().isConfigVerifiedColorAttachment(desc->fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000935 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
936 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
937 goto FAILED;
938 }
robertphillips@google.com6177e692013-02-28 20:16:25 +0000939 fGLContext.info().caps().markConfigAsValidColorAttachment(desc->fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000940 }
941
942 return true;
943
944FAILED:
945 if (desc->fMSColorRenderbufferID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000946 GL_CALL(DeleteRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000947 }
948 if (desc->fRTFBOID != desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000949 GL_CALL(DeleteFramebuffers(1, &desc->fRTFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000950 }
951 if (desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000952 GL_CALL(DeleteFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000953 }
954 return false;
955}
956
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000957// good to set a break-point here to know when createTexture fails
958static GrTexture* return_null_texture() {
959// GrAssert(!"null texture");
960 return NULL;
961}
962
caryclark@google.comcf6285b2012-06-06 12:09:01 +0000963#if 0 && GR_DEBUG
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000964static size_t as_size_t(int x) {
965 return x;
966}
967#endif
968
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000969GrTexture* GrGpuGL::onCreateTexture(const GrTextureDesc& desc,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000970 const void* srcData,
971 size_t rowBytes) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000972
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000973 GrGLTexture::Desc glTexDesc;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000974 GrGLRenderTarget::Desc glRTDesc;
reed@google.comac10a2d2010-12-22 21:39:39 +0000975
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000976 // Attempt to catch un- or wrongly initialized sample counts;
977 GrAssert(desc.fSampleCnt >= 0 && desc.fSampleCnt <= 64);
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000978 // We fail if the MSAA was requested and is not available.
979 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType() && desc.fSampleCnt) {
980 //GrPrintf("MSAA RT requested but not supported on this platform.");
981 return return_null_texture();
982 }
983 // If the sample count exceeds the max then we clamp it.
984 glTexDesc.fSampleCnt = GrMin(desc.fSampleCnt, this->getCaps().maxSampleCount());
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000985
robertphillips@google.com32716282012-06-04 12:48:45 +0000986 glTexDesc.fFlags = desc.fFlags;
bsalomon@google.com99621082011-11-15 16:47:16 +0000987 glTexDesc.fWidth = desc.fWidth;
988 glTexDesc.fHeight = desc.fHeight;
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000989 glTexDesc.fConfig = desc.fConfig;
bsalomon@google.com72830222013-01-23 20:25:22 +0000990 glTexDesc.fIsWrapped = false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000991
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000992 glRTDesc.fMSColorRenderbufferID = 0;
993 glRTDesc.fRTFBOID = 0;
994 glRTDesc.fTexFBOID = 0;
bsalomon@google.com72830222013-01-23 20:25:22 +0000995 glRTDesc.fIsWrapped = false;
bsalomon@google.com64c4fe42011-11-05 14:51:01 +0000996 glRTDesc.fConfig = glTexDesc.fConfig;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000997
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000998 bool renderTarget = 0 != (desc.fFlags & kRenderTarget_GrTextureFlagBit);
reed@google.comac10a2d2010-12-22 21:39:39 +0000999
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001000 const Caps& caps = this->getCaps();
1001
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001002 glTexDesc.fOrigin = resolve_origin(desc.fOrigin, renderTarget);
1003 glRTDesc.fOrigin = glTexDesc.fOrigin;
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001004
bsalomon@google.com8a70eef2013-03-19 13:58:55 +00001005 glRTDesc.fSampleCnt = glTexDesc.fSampleCnt;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001006 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType() &&
bsalomon@google.com78d6cf92012-01-30 18:09:31 +00001007 desc.fSampleCnt) {
bsalomon@google.com945bbe12012-06-15 14:30:34 +00001008 //GrPrintf("MSAA RT requested but not supported on this platform.");
1009 return return_null_texture();
reed@google.comac10a2d2010-12-22 21:39:39 +00001010 }
1011
reed@google.comac10a2d2010-12-22 21:39:39 +00001012 if (renderTarget) {
bsalomon@google.comf6601872012-08-28 21:11:35 +00001013 if (glTexDesc.fWidth > caps.maxRenderTargetSize() ||
1014 glTexDesc.fHeight > caps.maxRenderTargetSize()) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001015 return return_null_texture();
1016 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001017 }
1018
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001019 GL_CALL(GenTextures(1, &glTexDesc.fTextureID));
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001020 if (renderTarget && this->glCaps().textureUsageSupport()) {
bsalomon@google.com07dd2bf2011-12-09 19:40:36 +00001021 // provides a hint about how this texture will be used
1022 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1023 GR_GL_TEXTURE_USAGE,
1024 GR_GL_FRAMEBUFFER_ATTACHMENT));
1025 }
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001026 if (!glTexDesc.fTextureID) {
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001027 return return_null_texture();
1028 }
1029
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001030 this->setSpareTextureUnit();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001031 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTexDesc.fTextureID));
bsalomon@google.come269f212011-11-07 13:29:52 +00001032
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001033 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
1034 // drivers have a bug where an FBO won't be complete if it includes a
1035 // texture that is not mipmap complete (considering the filter in use).
1036 GrGLTexture::TexParams initialTexParams;
1037 // we only set a subset here so invalidate first
1038 initialTexParams.invalidate();
1039 initialTexParams.fFilter = GR_GL_NEAREST;
1040 initialTexParams.fWrapS = GR_GL_CLAMP_TO_EDGE;
1041 initialTexParams.fWrapT = GR_GL_CLAMP_TO_EDGE;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001042 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1043 GR_GL_TEXTURE_MAG_FILTER,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001044 initialTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001045 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1046 GR_GL_TEXTURE_MIN_FILTER,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001047 initialTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001048 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1049 GR_GL_TEXTURE_WRAP_S,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001050 initialTexParams.fWrapS));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001051 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1052 GR_GL_TEXTURE_WRAP_T,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001053 initialTexParams.fWrapT));
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001054 if (!this->uploadTexData(glTexDesc, true, 0, 0,
1055 glTexDesc.fWidth, glTexDesc.fHeight,
1056 desc.fConfig, srcData, rowBytes)) {
1057 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
1058 return return_null_texture();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001059 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001060
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001061 GrGLTexture* tex;
reed@google.comac10a2d2010-12-22 21:39:39 +00001062 if (renderTarget) {
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001063 // unbind the texture from the texture unit before binding it to the frame buffer
1064 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, 0));
1065
bsalomon@google.com99621082011-11-15 16:47:16 +00001066 if (!this->createRenderTargetObjects(glTexDesc.fWidth,
1067 glTexDesc.fHeight,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001068 glTexDesc.fTextureID,
1069 &glRTDesc)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001070 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001071 return return_null_texture();
1072 }
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001073 tex = SkNEW_ARGS(GrGLTexture, (this, glTexDesc, glRTDesc));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001074 } else {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001075 tex = SkNEW_ARGS(GrGLTexture, (this, glTexDesc));
reed@google.comac10a2d2010-12-22 21:39:39 +00001076 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001077 tex->setCachedTexParams(initialTexParams, this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00001078#ifdef TRACE_TEXTURE_CREATION
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001079 GrPrintf("--- new texture [%d] size=(%d %d) config=%d\n",
1080 glTexDesc.fTextureID, desc.fWidth, desc.fHeight, desc.fConfig);
reed@google.comac10a2d2010-12-22 21:39:39 +00001081#endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001082 return tex;
1083}
1084
1085namespace {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001086
1087const GrGLuint kUnknownBitCount = GrGLStencilBuffer::kUnknownBitCount;
1088
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001089void inline get_stencil_rb_sizes(const GrGLInterface* gl,
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001090 GrGLuint rb,
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001091 GrGLStencilBuffer::Format* format) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001092 // we shouldn't ever know one size and not the other
1093 GrAssert((kUnknownBitCount == format->fStencilBits) ==
1094 (kUnknownBitCount == format->fTotalBits));
1095 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001096 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001097 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1098 (GrGLint*)&format->fStencilBits);
1099 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001100 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001101 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1102 (GrGLint*)&format->fTotalBits);
1103 format->fTotalBits += format->fStencilBits;
1104 } else {
1105 format->fTotalBits = format->fStencilBits;
1106 }
1107 }
1108}
1109}
1110
1111bool GrGpuGL::createStencilBufferForRenderTarget(GrRenderTarget* rt,
1112 int width, int height) {
1113
1114 // All internally created RTs are also textures. We don't create
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +00001115 // 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 +00001116 GrAssert(rt->asTexture());
bsalomon@google.com99621082011-11-15 16:47:16 +00001117 GrAssert(width >= rt->width());
1118 GrAssert(height >= rt->height());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001119
1120 int samples = rt->numSamples();
1121 GrGLuint sbID;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001122 GL_CALL(GenRenderbuffers(1, &sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001123 if (!sbID) {
1124 return false;
1125 }
1126
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001127 int stencilFmtCnt = this->glCaps().stencilFormats().count();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001128 for (int i = 0; i < stencilFmtCnt; ++i) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001129 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001130 // we start with the last stencil format that succeeded in hopes
1131 // that we won't go through this loop more than once after the
1132 // first (painful) stencil creation.
1133 int sIdx = (i + fLastSuccessfulStencilFmtIdx) % stencilFmtCnt;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001134 const GrGLCaps::StencilFormat& sFmt =
1135 this->glCaps().stencilFormats()[sIdx];
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001136 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001137 // we do this "if" so that we don't call the multisample
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001138 // version on a GL that doesn't have an MSAA extension.
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001139 bool created;
1140 if (samples > 0) {
robertphillips@google.com6177e692013-02-28 20:16:25 +00001141 created = renderbuffer_storage_msaa(fGLContext,
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001142 samples,
1143 sFmt.fInternalFormat,
1144 width, height);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001145 } else {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001146 GL_ALLOC_CALL(this->glInterface(),
1147 RenderbufferStorage(GR_GL_RENDERBUFFER,
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001148 sFmt.fInternalFormat,
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001149 width, height));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001150 created =
1151 (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(this->glInterface()));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001152 }
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001153 if (created) {
1154 // After sized formats we attempt an unsized format and take
1155 // whatever sizes GL gives us. In that case we query for the size.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001156 GrGLStencilBuffer::Format format = sFmt;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001157 get_stencil_rb_sizes(this->glInterface(), sbID, &format);
bsalomon@google.com72830222013-01-23 20:25:22 +00001158 static const bool kIsWrapped = false;
robertphillips@google.comf2e93fc2012-09-05 19:44:18 +00001159 SkAutoTUnref<GrStencilBuffer> sb(SkNEW_ARGS(GrGLStencilBuffer,
bsalomon@google.com72830222013-01-23 20:25:22 +00001160 (this, kIsWrapped, sbID, width, height,
robertphillips@google.comf2e93fc2012-09-05 19:44:18 +00001161 samples, format)));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001162 if (this->attachStencilBufferToRenderTarget(sb, rt)) {
1163 fLastSuccessfulStencilFmtIdx = sIdx;
robertphillips@google.com9fbcad02012-09-09 14:44:15 +00001164 sb->transferToCache();
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001165 rt->setStencilBuffer(sb);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001166 return true;
1167 }
1168 sb->abandon(); // otherwise we lose sbID
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001169 }
1170 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001171 GL_CALL(DeleteRenderbuffers(1, &sbID));
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001172 return false;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001173}
1174
1175bool GrGpuGL::attachStencilBufferToRenderTarget(GrStencilBuffer* sb,
1176 GrRenderTarget* rt) {
1177 GrGLRenderTarget* glrt = (GrGLRenderTarget*) rt;
1178
1179 GrGLuint fbo = glrt->renderFBOID();
1180
1181 if (NULL == sb) {
1182 if (NULL != rt->getStencilBuffer()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001183 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001184 GR_GL_STENCIL_ATTACHMENT,
1185 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001186 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001187 GR_GL_DEPTH_ATTACHMENT,
1188 GR_GL_RENDERBUFFER, 0));
1189#if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001190 GrGLenum status;
1191 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001192 GrAssert(GR_GL_FRAMEBUFFER_COMPLETE == status);
1193#endif
1194 }
1195 return true;
1196 } else {
1197 GrGLStencilBuffer* glsb = (GrGLStencilBuffer*) sb;
1198 GrGLuint rb = glsb->renderbufferID();
1199
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001200 fHWBoundRenderTarget = NULL;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001201 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, fbo));
1202 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001203 GR_GL_STENCIL_ATTACHMENT,
1204 GR_GL_RENDERBUFFER, rb));
1205 if (glsb->format().fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001206 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001207 GR_GL_DEPTH_ATTACHMENT,
1208 GR_GL_RENDERBUFFER, rb));
1209 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001210 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001211 GR_GL_DEPTH_ATTACHMENT,
1212 GR_GL_RENDERBUFFER, 0));
reed@google.comac10a2d2010-12-22 21:39:39 +00001213 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001214
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001215 GrGLenum status;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001216 if (!this->glCaps().isColorConfigAndStencilFormatVerified(rt->config(),
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001217 glsb->format())) {
1218 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1219 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001220 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001221 GR_GL_STENCIL_ATTACHMENT,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001222 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001223 if (glsb->format().fPacked) {
1224 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1225 GR_GL_DEPTH_ATTACHMENT,
1226 GR_GL_RENDERBUFFER, 0));
1227 }
1228 return false;
1229 } else {
robertphillips@google.com6177e692013-02-28 20:16:25 +00001230 fGLContext.info().caps().markColorConfigAndStencilFormatAsVerified(
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001231 rt->config(),
1232 glsb->format());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001233 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001234 }
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001235 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001236 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001237}
1238
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001239////////////////////////////////////////////////////////////////////////////////
1240
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001241GrVertexBuffer* GrGpuGL::onCreateVertexBuffer(uint32_t size, bool dynamic) {
bsalomon@google.come49ad452013-02-20 19:33:20 +00001242 GrGLVertexBuffer::Desc desc;
1243 desc.fDynamic = dynamic;
1244 desc.fSizeInBytes = size;
1245 desc.fIsWrapped = false;
1246
bsalomon@google.com96966a52013-02-21 16:34:21 +00001247 if (this->glCaps().useNonVBOVertexAndIndexDynamicData() && desc.fDynamic) {
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001248 desc.fID = 0;
bsalomon@google.come49ad452013-02-20 19:33:20 +00001249 GrGLVertexBuffer* vertexBuffer = SkNEW_ARGS(GrGLVertexBuffer, (this, desc));
reed@google.comac10a2d2010-12-22 21:39:39 +00001250 return vertexBuffer;
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001251 } else {
1252 GL_CALL(GenBuffers(1, &desc.fID));
1253 if (desc.fID) {
bsalomon@google.com6918d482013-03-07 19:09:11 +00001254 fHWGeometryState.setVertexBufferID(this, desc.fID);
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001255 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1256 // make sure driver can allocate memory for this buffer
1257 GL_ALLOC_CALL(this->glInterface(),
1258 BufferData(GR_GL_ARRAY_BUFFER,
1259 desc.fSizeInBytes,
1260 NULL, // data ptr
1261 desc.fDynamic ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW));
1262 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
1263 GL_CALL(DeleteBuffers(1, &desc.fID));
bsalomon@google.com6918d482013-03-07 19:09:11 +00001264 this->notifyVertexBufferDelete(desc.fID);
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001265 return NULL;
1266 }
1267 GrGLVertexBuffer* vertexBuffer = SkNEW_ARGS(GrGLVertexBuffer, (this, desc));
1268 return vertexBuffer;
1269 }
1270 return NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +00001271 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001272}
1273
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001274GrIndexBuffer* GrGpuGL::onCreateIndexBuffer(uint32_t size, bool dynamic) {
bsalomon@google.come49ad452013-02-20 19:33:20 +00001275 GrGLIndexBuffer::Desc desc;
1276 desc.fDynamic = dynamic;
1277 desc.fSizeInBytes = size;
1278 desc.fIsWrapped = false;
1279
bsalomon@google.com96966a52013-02-21 16:34:21 +00001280 if (this->glCaps().useNonVBOVertexAndIndexDynamicData() && desc.fDynamic) {
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001281 desc.fID = 0;
bsalomon@google.come49ad452013-02-20 19:33:20 +00001282 GrIndexBuffer* indexBuffer = SkNEW_ARGS(GrGLIndexBuffer, (this, desc));
reed@google.comac10a2d2010-12-22 21:39:39 +00001283 return indexBuffer;
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001284 } else {
1285 GL_CALL(GenBuffers(1, &desc.fID));
1286 if (desc.fID) {
bsalomon@google.com6918d482013-03-07 19:09:11 +00001287 fHWGeometryState.setIndexBufferIDOnDefaultVertexArray(this, desc.fID);
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001288 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1289 // make sure driver can allocate memory for this buffer
1290 GL_ALLOC_CALL(this->glInterface(),
1291 BufferData(GR_GL_ELEMENT_ARRAY_BUFFER,
1292 desc.fSizeInBytes,
1293 NULL, // data ptr
1294 desc.fDynamic ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW));
1295 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
1296 GL_CALL(DeleteBuffers(1, &desc.fID));
bsalomon@google.com6918d482013-03-07 19:09:11 +00001297 this->notifyIndexBufferDelete(desc.fID);
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001298 return NULL;
1299 }
1300 GrIndexBuffer* indexBuffer = SkNEW_ARGS(GrGLIndexBuffer, (this, desc));
1301 return indexBuffer;
1302 }
1303 return NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +00001304 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001305}
1306
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00001307GrPath* GrGpuGL::onCreatePath(const SkPath& inPath) {
bsalomon@google.comf6601872012-08-28 21:11:35 +00001308 GrAssert(fCaps.pathStencilingSupport());
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001309 return SkNEW_ARGS(GrGLPath, (this, inPath));
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00001310}
1311
bsalomon@google.coma3201942012-06-21 19:58:20 +00001312void GrGpuGL::flushScissor() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001313 const GrDrawState& drawState = this->getDrawState();
1314 const GrGLRenderTarget* rt =
1315 static_cast<const GrGLRenderTarget*>(drawState.getRenderTarget());
1316
1317 GrAssert(NULL != rt);
1318 const GrGLIRect& vp = rt->getViewport();
reed@google.comac10a2d2010-12-22 21:39:39 +00001319
bsalomon@google.coma3201942012-06-21 19:58:20 +00001320 if (fScissorState.fEnabled) {
1321 GrGLIRect scissor;
1322 scissor.setRelativeTo(vp,
1323 fScissorState.fRect.fLeft,
1324 fScissorState.fRect.fTop,
1325 fScissorState.fRect.width(),
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001326 fScissorState.fRect.height(),
1327 rt->origin());
bsalomon@google.coma3201942012-06-21 19:58:20 +00001328 // if the scissor fully contains the viewport then we fall through and
1329 // disable the scissor test.
1330 if (!scissor.contains(vp)) {
1331 if (fHWScissorSettings.fRect != scissor) {
1332 scissor.pushToGLScissor(this->glInterface());
1333 fHWScissorSettings.fRect = scissor;
1334 }
1335 if (kYes_TriState != fHWScissorSettings.fEnabled) {
1336 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1337 fHWScissorSettings.fEnabled = kYes_TriState;
1338 }
1339 return;
1340 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001341 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001342 if (kNo_TriState != fHWScissorSettings.fEnabled) {
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001343 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.coma3201942012-06-21 19:58:20 +00001344 fHWScissorSettings.fEnabled = kNo_TriState;
1345 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001346 }
1347}
1348
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001349void GrGpuGL::onClear(const GrIRect* rect, GrColor color) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001350 const GrDrawState& drawState = this->getDrawState();
1351 const GrRenderTarget* rt = drawState.getRenderTarget();
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001352 // parent class should never let us get here with no RT
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001353 GrAssert(NULL != rt);
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001354
bsalomon@google.com74b98712011-11-11 19:46:16 +00001355 GrIRect clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001356 if (NULL != rect) {
1357 // flushScissor expects rect to be clipped to the target.
bsalomon@google.com74b98712011-11-11 19:46:16 +00001358 clippedRect = *rect;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001359 GrIRect rtRect = SkIRect::MakeWH(rt->width(), rt->height());
bsalomon@google.com74b98712011-11-11 19:46:16 +00001360 if (clippedRect.intersect(rtRect)) {
1361 rect = &clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001362 } else {
1363 return;
1364 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001365 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001366 this->flushRenderTarget(rect);
bsalomon@google.coma3201942012-06-21 19:58:20 +00001367 GrAutoTRestore<ScissorState> asr(&fScissorState);
1368 fScissorState.fEnabled = (NULL != rect);
1369 if (fScissorState.fEnabled) {
1370 fScissorState.fRect = *rect;
1371 }
1372 this->flushScissor();
bsalomon@google.com74b98712011-11-11 19:46:16 +00001373
1374 GrGLfloat r, g, b, a;
1375 static const GrGLfloat scale255 = 1.f / 255.f;
1376 a = GrColorUnpackA(color) * scale255;
1377 GrGLfloat scaleRGB = scale255;
bsalomon@google.com74b98712011-11-11 19:46:16 +00001378 r = GrColorUnpackR(color) * scaleRGB;
1379 g = GrColorUnpackG(color) * scaleRGB;
1380 b = GrColorUnpackB(color) * scaleRGB;
1381
1382 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00001383 fHWWriteToColor = kYes_TriState;
bsalomon@google.com74b98712011-11-11 19:46:16 +00001384 GL_CALL(ClearColor(r, g, b, a));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001385 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001386}
1387
bsalomon@google.comedc177d2011-08-05 15:46:40 +00001388void GrGpuGL::clearStencil() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001389 if (NULL == this->getDrawState().getRenderTarget()) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001390 return;
1391 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001392
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001393 this->flushRenderTarget(&GrIRect::EmptyIRect());
1394
bsalomon@google.coma3201942012-06-21 19:58:20 +00001395 GrAutoTRestore<ScissorState> asr(&fScissorState);
1396 fScissorState.fEnabled = false;
1397 this->flushScissor();
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001398
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001399 GL_CALL(StencilMask(0xffffffff));
1400 GL_CALL(ClearStencil(0));
1401 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001402 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001403}
1404
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001405void GrGpuGL::clearStencilClip(const GrIRect& rect, bool insideClip) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001406 const GrDrawState& drawState = this->getDrawState();
1407 const GrRenderTarget* rt = drawState.getRenderTarget();
1408 GrAssert(NULL != rt);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001409
1410 // this should only be called internally when we know we have a
1411 // stencil buffer.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001412 GrAssert(NULL != rt->getStencilBuffer());
1413 GrGLint stencilBitCount = rt->getStencilBuffer()->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001414#if 0
reed@google.comac10a2d2010-12-22 21:39:39 +00001415 GrAssert(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00001416 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001417#else
1418 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001419 // ANGLE a partial stencil mask will cause clears to be
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001420 // turned into draws. Our contract on GrDrawTarget says that
1421 // changing the clip between stencil passes may or may not
1422 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00001423 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001424#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001425 GrGLint value;
1426 if (insideClip) {
1427 value = (1 << (stencilBitCount - 1));
1428 } else {
1429 value = 0;
1430 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001431 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.coma3201942012-06-21 19:58:20 +00001432
1433 GrAutoTRestore<ScissorState> asr(&fScissorState);
1434 fScissorState.fEnabled = true;
1435 fScissorState.fRect = rect;
1436 this->flushScissor();
1437
caryclark@google.comcf6285b2012-06-06 12:09:01 +00001438 GL_CALL(StencilMask((uint32_t) clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001439 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001440 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001441 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001442}
1443
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001444void GrGpuGL::onForceRenderTargetFlush() {
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001445 this->flushRenderTarget(&GrIRect::EmptyIRect());
reed@google.comac10a2d2010-12-22 21:39:39 +00001446}
1447
bsalomon@google.comc4364992011-11-07 15:54:49 +00001448bool GrGpuGL::readPixelsWillPayForYFlip(GrRenderTarget* renderTarget,
1449 int left, int top,
1450 int width, int height,
1451 GrPixelConfig config,
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001452 size_t rowBytes) const {
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001453 // If this rendertarget is aready TopLeft, we don't need to flip.
1454 if (kTopLeft_GrSurfaceOrigin == renderTarget->origin()) {
1455 return false;
1456 }
1457
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001458 // if GL can do the flip then we'll never pay for it.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001459 if (this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001460 return false;
1461 }
1462
1463 // If we have to do memcpy to handle non-trim rowBytes then we
bsalomon@google.com7107fa72011-11-10 14:54:14 +00001464 // get the flip for free. Otherwise it costs.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001465 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001466 return true;
1467 }
1468 // If we have to do memcpys to handle rowBytes then y-flip is free
1469 // Note the rowBytes might be tight to the passed in data, but if data
1470 // gets clipped in x to the target the rowBytes will no longer be tight.
1471 if (left >= 0 && (left + width) < renderTarget->width()) {
1472 return 0 == rowBytes ||
1473 GrBytesPerPixel(config) * width == rowBytes;
1474 } else {
1475 return false;
1476 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001477}
1478
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001479bool GrGpuGL::onReadPixels(GrRenderTarget* target,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001480 int left, int top,
1481 int width, int height,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001482 GrPixelConfig config,
1483 void* buffer,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001484 size_t rowBytes) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001485 GrGLenum format;
1486 GrGLenum type;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001487 bool flipY = kBottomLeft_GrSurfaceOrigin == target->origin();
bsalomon@google.com280e99f2012-01-05 16:17:38 +00001488 if (!this->configToGLFormats(config, false, NULL, &format, &type)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001489 return false;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001490 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001491 size_t bpp = GrBytesPerPixel(config);
1492 if (!adjust_pixel_ops_params(target->width(), target->height(), bpp,
1493 &left, &top, &width, &height,
1494 const_cast<const void**>(&buffer),
1495 &rowBytes)) {
1496 return false;
1497 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001498
bsalomon@google.comc6980972011-11-02 19:57:21 +00001499 // resolve the render target if necessary
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001500 GrGLRenderTarget* tgt = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001501 GrDrawState::AutoRenderTargetRestore artr;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001502 switch (tgt->getResolveType()) {
1503 case GrGLRenderTarget::kCantResolve_ResolveType:
1504 return false;
1505 case GrGLRenderTarget::kAutoResolves_ResolveType:
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001506 artr.set(this->drawState(), target);
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001507 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001508 break;
1509 case GrGLRenderTarget::kCanResolve_ResolveType:
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001510 this->onResolveRenderTarget(tgt);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001511 // we don't track the state of the READ FBO ID.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001512 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1513 tgt->textureFBOID()));
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001514 break;
1515 default:
1516 GrCrash("Unknown resolve type");
reed@google.comac10a2d2010-12-22 21:39:39 +00001517 }
1518
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001519 const GrGLIRect& glvp = tgt->getViewport();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001520
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001521 // the read rect is viewport-relative
1522 GrGLIRect readRect;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001523 readRect.setRelativeTo(glvp, left, top, width, height, target->origin());
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001524
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001525 size_t tightRowBytes = bpp * width;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001526 if (0 == rowBytes) {
1527 rowBytes = tightRowBytes;
1528 }
1529 size_t readDstRowBytes = tightRowBytes;
1530 void* readDst = buffer;
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001531
bsalomon@google.comc6980972011-11-02 19:57:21 +00001532 // determine if GL can read using the passed rowBytes or if we need
1533 // a scratch buffer.
1534 SkAutoSMalloc<32 * sizeof(GrColor)> scratch;
1535 if (rowBytes != tightRowBytes) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001536 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.comc6980972011-11-02 19:57:21 +00001537 GrAssert(!(rowBytes % sizeof(GrColor)));
1538 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, rowBytes / sizeof(GrColor)));
1539 readDstRowBytes = rowBytes;
1540 } else {
1541 scratch.reset(tightRowBytes * height);
1542 readDst = scratch.get();
1543 }
1544 }
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001545 if (flipY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001546 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 1));
1547 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001548 GL_CALL(ReadPixels(readRect.fLeft, readRect.fBottom,
1549 readRect.fWidth, readRect.fHeight,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001550 format, type, readDst));
1551 if (readDstRowBytes != tightRowBytes) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001552 GrAssert(this->glCaps().packRowLengthSupport());
bsalomon@google.comc6980972011-11-02 19:57:21 +00001553 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
1554 }
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001555 if (flipY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001556 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 0));
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001557 flipY = false;
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001558 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001559
1560 // now reverse the order of the rows, since GL's are bottom-to-top, but our
bsalomon@google.comc6980972011-11-02 19:57:21 +00001561 // API presents top-to-bottom. We must preserve the padding contents. Note
1562 // that the above readPixels did not overwrite the padding.
1563 if (readDst == buffer) {
1564 GrAssert(rowBytes == readDstRowBytes);
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001565 if (flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001566 scratch.reset(tightRowBytes);
1567 void* tmpRow = scratch.get();
1568 // flip y in-place by rows
1569 const int halfY = height >> 1;
1570 char* top = reinterpret_cast<char*>(buffer);
1571 char* bottom = top + (height - 1) * rowBytes;
1572 for (int y = 0; y < halfY; y++) {
1573 memcpy(tmpRow, top, tightRowBytes);
1574 memcpy(top, bottom, tightRowBytes);
1575 memcpy(bottom, tmpRow, tightRowBytes);
1576 top += rowBytes;
1577 bottom -= rowBytes;
1578 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001579 }
1580 } else {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001581 GrAssert(readDst != buffer); GrAssert(rowBytes != tightRowBytes);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001582 // copy from readDst to buffer while flipping y
caryclark@google.comcf6285b2012-06-06 12:09:01 +00001583 // const int halfY = height >> 1;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001584 const char* src = reinterpret_cast<const char*>(readDst);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001585 char* dst = reinterpret_cast<char*>(buffer);
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001586 if (flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001587 dst += (height-1) * rowBytes;
1588 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001589 for (int y = 0; y < height; y++) {
1590 memcpy(dst, src, tightRowBytes);
1591 src += readDstRowBytes;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001592 if (!flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001593 dst += rowBytes;
1594 } else {
1595 dst -= rowBytes;
1596 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001597 }
1598 }
1599 return true;
1600}
1601
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001602void GrGpuGL::flushRenderTarget(const GrIRect* bound) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001603
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001604 GrGLRenderTarget* rt =
1605 static_cast<GrGLRenderTarget*>(this->drawState()->getRenderTarget());
1606 GrAssert(NULL != rt);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001607
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001608 if (fHWBoundRenderTarget != rt) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001609 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, rt->renderFBOID()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001610 #if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001611 GrGLenum status;
1612 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001613 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
reed@google.comb9255d52011-06-13 18:54:59 +00001614 GrPrintf("GrGpuGL::flushRenderTarget glCheckFramebufferStatus %x\n", status);
reed@google.comac10a2d2010-12-22 21:39:39 +00001615 }
1616 #endif
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001617 fHWBoundRenderTarget = rt;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001618 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.coma3201942012-06-21 19:58:20 +00001619 if (fHWViewport != vp) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001620 vp.pushToGLViewport(this->glInterface());
bsalomon@google.coma3201942012-06-21 19:58:20 +00001621 fHWViewport = vp;
reed@google.comac10a2d2010-12-22 21:39:39 +00001622 }
1623 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001624 if (NULL == bound || !bound->isEmpty()) {
1625 rt->flagAsNeedingResolve(bound);
1626 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001627}
1628
twiz@google.com0f31ca72011-03-18 17:38:11 +00001629GrGLenum gPrimitiveType2GLMode[] = {
1630 GR_GL_TRIANGLES,
1631 GR_GL_TRIANGLE_STRIP,
1632 GR_GL_TRIANGLE_FAN,
1633 GR_GL_POINTS,
1634 GR_GL_LINES,
1635 GR_GL_LINE_STRIP
reed@google.comac10a2d2010-12-22 21:39:39 +00001636};
1637
bsalomon@google.comd302f142011-03-03 13:54:13 +00001638#define SWAP_PER_DRAW 0
1639
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001640#if SWAP_PER_DRAW
bsalomon@google.comd302f142011-03-03 13:54:13 +00001641 #if GR_MAC_BUILD
1642 #include <AGL/agl.h>
1643 #elif GR_WIN32_BUILD
bsalomon@google.comce7357d2012-06-25 17:49:25 +00001644 #include <gl/GL.h>
bsalomon@google.comd302f142011-03-03 13:54:13 +00001645 void SwapBuf() {
1646 DWORD procID = GetCurrentProcessId();
1647 HWND hwnd = GetTopWindow(GetDesktopWindow());
1648 while(hwnd) {
1649 DWORD wndProcID = 0;
1650 GetWindowThreadProcessId(hwnd, &wndProcID);
1651 if(wndProcID == procID) {
1652 SwapBuffers(GetDC(hwnd));
1653 }
1654 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
1655 }
1656 }
1657 #endif
1658#endif
1659
bsalomon@google.com74749cd2013-01-30 16:12:41 +00001660void GrGpuGL::onGpuDraw(const DrawInfo& info) {
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00001661 size_t indexOffsetInBytes;
1662 this->setupGeometry(info, &indexOffsetInBytes);
reed@google.comac10a2d2010-12-22 21:39:39 +00001663
bsalomon@google.com74749cd2013-01-30 16:12:41 +00001664 GrAssert((size_t)info.primitiveType() < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001665
bsalomon@google.com74749cd2013-01-30 16:12:41 +00001666 if (info.isIndexed()) {
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00001667 GrGLvoid* indices =
1668 reinterpret_cast<GrGLvoid*>(indexOffsetInBytes + sizeof(uint16_t) * info.startIndex());
bsalomon@google.com74749cd2013-01-30 16:12:41 +00001669 // info.startVertex() was accounted for by setupGeometry.
1670 GL_CALL(DrawElements(gPrimitiveType2GLMode[info.primitiveType()],
1671 info.indexCount(),
1672 GR_GL_UNSIGNED_SHORT,
1673 indices));
1674 } else {
1675 // Pass 0 for parameter first. We have to adjust glVertexAttribPointer() to account for
1676 // startVertex in the DrawElements case. So we always rely on setupGeometry to have
1677 // accounted for startVertex.
1678 GL_CALL(DrawArrays(gPrimitiveType2GLMode[info.primitiveType()], 0, info.vertexCount()));
1679 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00001680#if SWAP_PER_DRAW
1681 glFlush();
1682 #if GR_MAC_BUILD
1683 aglSwapBuffers(aglGetCurrentContext());
1684 int set_a_break_pt_here = 9;
1685 aglSwapBuffers(aglGetCurrentContext());
1686 #elif GR_WIN32_BUILD
1687 SwapBuf();
1688 int set_a_break_pt_here = 9;
1689 SwapBuf();
1690 #endif
1691#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001692}
1693
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001694namespace {
bsalomon@google.com100abf42012-09-05 17:40:04 +00001695
1696static const uint16_t kOnes16 = static_cast<uint16_t>(~0);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001697const GrStencilSettings& winding_nv_path_stencil_settings() {
1698 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
1699 kIncClamp_StencilOp,
1700 kIncClamp_StencilOp,
1701 kAlwaysIfInClip_StencilFunc,
bsalomon@google.com100abf42012-09-05 17:40:04 +00001702 kOnes16, kOnes16, kOnes16);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001703 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
1704}
1705const GrStencilSettings& even_odd_nv_path_stencil_settings() {
1706 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
1707 kInvert_StencilOp,
1708 kInvert_StencilOp,
1709 kAlwaysIfInClip_StencilFunc,
bsalomon@google.com100abf42012-09-05 17:40:04 +00001710 kOnes16, kOnes16, kOnes16);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001711 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
1712}
1713}
1714
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001715void GrGpuGL::setStencilPathSettings(const GrPath&,
sugoi@google.com12b4e272012-12-06 20:13:11 +00001716 SkPath::FillType fill,
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001717 GrStencilSettings* settings) {
1718 switch (fill) {
sugoi@google.com12b4e272012-12-06 20:13:11 +00001719 case SkPath::kEvenOdd_FillType:
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001720 *settings = even_odd_nv_path_stencil_settings();
1721 return;
sugoi@google.com12b4e272012-12-06 20:13:11 +00001722 case SkPath::kWinding_FillType:
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001723 *settings = winding_nv_path_stencil_settings();
1724 return;
1725 default:
1726 GrCrash("Unexpected path fill.");
1727 }
1728}
1729
sugoi@google.com12b4e272012-12-06 20:13:11 +00001730void GrGpuGL::onGpuStencilPath(const GrPath* path, SkPath::FillType fill) {
bsalomon@google.comf6601872012-08-28 21:11:35 +00001731 GrAssert(fCaps.pathStencilingSupport());
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001732
1733 GrGLuint id = static_cast<const GrGLPath*>(path)->pathID();
1734 GrDrawState* drawState = this->drawState();
1735 GrAssert(NULL != drawState->getRenderTarget());
1736 if (NULL == drawState->getRenderTarget()->getStencilBuffer()) {
1737 return;
1738 }
1739
1740 // Decide how to manipulate the stencil buffer based on the fill rule.
1741 // Also, assert that the stencil settings we set in setStencilPathSettings
1742 // are present.
1743 GrAssert(!fStencilSettings.isTwoSided());
1744 GrGLenum fillMode;
1745 switch (fill) {
sugoi@google.com12b4e272012-12-06 20:13:11 +00001746 case SkPath::kWinding_FillType:
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001747 fillMode = GR_GL_COUNT_UP;
1748 GrAssert(kIncClamp_StencilOp ==
1749 fStencilSettings.passOp(GrStencilSettings::kFront_Face));
1750 GrAssert(kIncClamp_StencilOp ==
1751 fStencilSettings.failOp(GrStencilSettings::kFront_Face));
1752 break;
sugoi@google.com12b4e272012-12-06 20:13:11 +00001753 case SkPath::kEvenOdd_FillType:
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001754 fillMode = GR_GL_INVERT;
1755 GrAssert(kInvert_StencilOp ==
1756 fStencilSettings.passOp(GrStencilSettings::kFront_Face));
1757 GrAssert(kInvert_StencilOp ==
1758 fStencilSettings.failOp(GrStencilSettings::kFront_Face));
1759 break;
1760 default:
1761 // Only the above two fill rules are allowed.
1762 GrCrash("Unexpected path fill.");
bsalomon@google.com548a4332012-07-11 19:45:22 +00001763 return; // suppress unused var warning.
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001764 }
1765 GrGLint writeMask = fStencilSettings.writeMask(GrStencilSettings::kFront_Face);
1766 GL_CALL(StencilFillPath(id, fillMode, writeMask));
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00001767}
1768
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001769void GrGpuGL::onResolveRenderTarget(GrRenderTarget* target) {
1770
1771 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(target);
reed@google.comac10a2d2010-12-22 21:39:39 +00001772
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001773 if (rt->needsResolve()) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001774 GrAssert(GrGLCaps::kNone_MSFBOType != this->glCaps().msFBOType());
reed@google.comac10a2d2010-12-22 21:39:39 +00001775 GrAssert(rt->textureFBOID() != rt->renderFBOID());
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001776 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1777 rt->renderFBOID()));
1778 GL_CALL(BindFramebuffer(GR_GL_DRAW_FRAMEBUFFER,
1779 rt->textureFBOID()));
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001780 // make sure we go through flushRenderTarget() since we've modified
1781 // the bound DRAW FBO ID.
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001782 fHWBoundRenderTarget = NULL;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001783 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001784 const GrIRect dirtyRect = rt->getResolveRect();
1785 GrGLIRect r;
bsalomon@google.coma3201942012-06-21 19:58:20 +00001786 r.setRelativeTo(vp, dirtyRect.fLeft, dirtyRect.fTop,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001787 dirtyRect.width(), dirtyRect.height(), target->origin());
reed@google.comac10a2d2010-12-22 21:39:39 +00001788
bsalomon@google.coma3201942012-06-21 19:58:20 +00001789 GrAutoTRestore<ScissorState> asr;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001790 if (GrGLCaps::kAppleES_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001791 // Apple's extension uses the scissor as the blit bounds.
bsalomon@google.coma3201942012-06-21 19:58:20 +00001792 asr.reset(&fScissorState);
1793 fScissorState.fEnabled = true;
1794 fScissorState.fRect = dirtyRect;
1795 this->flushScissor();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001796 GL_CALL(ResolveMultisampleFramebuffer());
reed@google.comac10a2d2010-12-22 21:39:39 +00001797 } else {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001798 if (GrGLCaps::kDesktopARB_MSFBOType != this->glCaps().msFBOType()) {
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001799 // this respects the scissor during the blit, so disable it.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001800 GrAssert(GrGLCaps::kDesktopEXT_MSFBOType ==
1801 this->glCaps().msFBOType());
bsalomon@google.coma3201942012-06-21 19:58:20 +00001802 asr.reset(&fScissorState);
1803 fScissorState.fEnabled = false;
1804 this->flushScissor();
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001805 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001806 int right = r.fLeft + r.fWidth;
1807 int top = r.fBottom + r.fHeight;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001808 GL_CALL(BlitFramebuffer(r.fLeft, r.fBottom, right, top,
1809 r.fLeft, r.fBottom, right, top,
1810 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001811 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001812 rt->flagAsResolved();
reed@google.comac10a2d2010-12-22 21:39:39 +00001813 }
1814}
1815
bsalomon@google.com411dad02012-06-05 20:24:20 +00001816namespace {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001817
bsalomon@google.com411dad02012-06-05 20:24:20 +00001818GrGLenum gr_to_gl_stencil_func(GrStencilFunc basicFunc) {
1819 static const GrGLenum gTable[] = {
1820 GR_GL_ALWAYS, // kAlways_StencilFunc
1821 GR_GL_NEVER, // kNever_StencilFunc
1822 GR_GL_GREATER, // kGreater_StencilFunc
1823 GR_GL_GEQUAL, // kGEqual_StencilFunc
1824 GR_GL_LESS, // kLess_StencilFunc
1825 GR_GL_LEQUAL, // kLEqual_StencilFunc,
1826 GR_GL_EQUAL, // kEqual_StencilFunc,
1827 GR_GL_NOTEQUAL, // kNotEqual_StencilFunc,
1828 };
1829 GR_STATIC_ASSERT(GR_ARRAY_COUNT(gTable) == kBasicStencilFuncCount);
1830 GR_STATIC_ASSERT(0 == kAlways_StencilFunc);
1831 GR_STATIC_ASSERT(1 == kNever_StencilFunc);
1832 GR_STATIC_ASSERT(2 == kGreater_StencilFunc);
1833 GR_STATIC_ASSERT(3 == kGEqual_StencilFunc);
1834 GR_STATIC_ASSERT(4 == kLess_StencilFunc);
1835 GR_STATIC_ASSERT(5 == kLEqual_StencilFunc);
1836 GR_STATIC_ASSERT(6 == kEqual_StencilFunc);
1837 GR_STATIC_ASSERT(7 == kNotEqual_StencilFunc);
1838 GrAssert((unsigned) basicFunc < kBasicStencilFuncCount);
1839
1840 return gTable[basicFunc];
1841}
1842
1843GrGLenum gr_to_gl_stencil_op(GrStencilOp op) {
1844 static const GrGLenum gTable[] = {
1845 GR_GL_KEEP, // kKeep_StencilOp
1846 GR_GL_REPLACE, // kReplace_StencilOp
1847 GR_GL_INCR_WRAP, // kIncWrap_StencilOp
1848 GR_GL_INCR, // kIncClamp_StencilOp
1849 GR_GL_DECR_WRAP, // kDecWrap_StencilOp
1850 GR_GL_DECR, // kDecClamp_StencilOp
1851 GR_GL_ZERO, // kZero_StencilOp
1852 GR_GL_INVERT, // kInvert_StencilOp
1853 };
1854 GR_STATIC_ASSERT(GR_ARRAY_COUNT(gTable) == kStencilOpCount);
1855 GR_STATIC_ASSERT(0 == kKeep_StencilOp);
1856 GR_STATIC_ASSERT(1 == kReplace_StencilOp);
1857 GR_STATIC_ASSERT(2 == kIncWrap_StencilOp);
1858 GR_STATIC_ASSERT(3 == kIncClamp_StencilOp);
1859 GR_STATIC_ASSERT(4 == kDecWrap_StencilOp);
1860 GR_STATIC_ASSERT(5 == kDecClamp_StencilOp);
1861 GR_STATIC_ASSERT(6 == kZero_StencilOp);
1862 GR_STATIC_ASSERT(7 == kInvert_StencilOp);
1863 GrAssert((unsigned) op < kStencilOpCount);
1864 return gTable[op];
1865}
1866
1867void set_gl_stencil(const GrGLInterface* gl,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001868 const GrStencilSettings& settings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00001869 GrGLenum glFace,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001870 GrStencilSettings::Face grFace) {
1871 GrGLenum glFunc = gr_to_gl_stencil_func(settings.func(grFace));
1872 GrGLenum glFailOp = gr_to_gl_stencil_op(settings.failOp(grFace));
1873 GrGLenum glPassOp = gr_to_gl_stencil_op(settings.passOp(grFace));
1874
1875 GrGLint ref = settings.funcRef(grFace);
1876 GrGLint mask = settings.funcMask(grFace);
1877 GrGLint writeMask = settings.writeMask(grFace);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001878
1879 if (GR_GL_FRONT_AND_BACK == glFace) {
1880 // we call the combined func just in case separate stencil is not
1881 // supported.
1882 GR_GL_CALL(gl, StencilFunc(glFunc, ref, mask));
1883 GR_GL_CALL(gl, StencilMask(writeMask));
1884 GR_GL_CALL(gl, StencilOp(glFailOp, glPassOp, glPassOp));
1885 } else {
1886 GR_GL_CALL(gl, StencilFuncSeparate(glFace, glFunc, ref, mask));
1887 GR_GL_CALL(gl, StencilMaskSeparate(glFace, writeMask));
1888 GR_GL_CALL(gl, StencilOpSeparate(glFace, glFailOp, glPassOp, glPassOp));
1889 }
1890}
1891}
bsalomon@google.comd302f142011-03-03 13:54:13 +00001892
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001893void GrGpuGL::flushStencil(DrawType type) {
1894 if (kStencilPath_DrawType == type) {
1895 GrAssert(!fStencilSettings.isTwoSided());
1896 // Just the func, ref, and mask is set here. The op and write mask are params to the call
1897 // that draws the path to the SB (glStencilFillPath)
1898 GrGLenum func =
1899 gr_to_gl_stencil_func(fStencilSettings.func(GrStencilSettings::kFront_Face));
1900 GL_CALL(PathStencilFunc(func,
1901 fStencilSettings.funcRef(GrStencilSettings::kFront_Face),
1902 fStencilSettings.funcMask(GrStencilSettings::kFront_Face)));
1903 } else if (fHWStencilSettings != fStencilSettings) {
1904 if (fStencilSettings.isDisabled()) {
1905 if (kNo_TriState != fHWStencilTestEnabled) {
1906 GL_CALL(Disable(GR_GL_STENCIL_TEST));
1907 fHWStencilTestEnabled = kNo_TriState;
1908 }
1909 } else {
1910 if (kYes_TriState != fHWStencilTestEnabled) {
1911 GL_CALL(Enable(GR_GL_STENCIL_TEST));
1912 fHWStencilTestEnabled = kYes_TriState;
1913 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001914 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001915 if (!fStencilSettings.isDisabled()) {
bsalomon@google.comf6601872012-08-28 21:11:35 +00001916 if (this->getCaps().twoSidedStencilSupport()) {
bsalomon@google.com411dad02012-06-05 20:24:20 +00001917 set_gl_stencil(this->glInterface(),
bsalomon@google.coma3201942012-06-21 19:58:20 +00001918 fStencilSettings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00001919 GR_GL_FRONT,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001920 GrStencilSettings::kFront_Face);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001921 set_gl_stencil(this->glInterface(),
bsalomon@google.coma3201942012-06-21 19:58:20 +00001922 fStencilSettings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00001923 GR_GL_BACK,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001924 GrStencilSettings::kBack_Face);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001925 } else {
bsalomon@google.com411dad02012-06-05 20:24:20 +00001926 set_gl_stencil(this->glInterface(),
bsalomon@google.coma3201942012-06-21 19:58:20 +00001927 fStencilSettings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00001928 GR_GL_FRONT_AND_BACK,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001929 GrStencilSettings::kFront_Face);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001930 }
1931 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001932 fHWStencilSettings = fStencilSettings;
reed@google.comac10a2d2010-12-22 21:39:39 +00001933 }
1934}
1935
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001936void GrGpuGL::flushAAState(DrawType type) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001937 const GrRenderTarget* rt = this->getDrawState().getRenderTarget();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001938 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001939 // ES doesn't support toggling GL_MULTISAMPLE and doesn't have
1940 // smooth lines.
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001941 // we prefer smooth lines over multisampled lines
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00001942 bool smoothLines = false;
1943
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001944 if (kDrawLines_DrawType == type) {
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00001945 smoothLines = this->willUseHWAALines();
1946 if (smoothLines) {
1947 if (kYes_TriState != fHWAAState.fSmoothLineEnabled) {
1948 GL_CALL(Enable(GR_GL_LINE_SMOOTH));
1949 fHWAAState.fSmoothLineEnabled = kYes_TriState;
1950 // must disable msaa to use line smoothing
1951 if (rt->isMultisampled() &&
1952 kNo_TriState != fHWAAState.fMSAAEnabled) {
1953 GL_CALL(Disable(GR_GL_MULTISAMPLE));
1954 fHWAAState.fMSAAEnabled = kNo_TriState;
1955 }
1956 }
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001957 } else {
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00001958 if (kNo_TriState != fHWAAState.fSmoothLineEnabled) {
1959 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
1960 fHWAAState.fSmoothLineEnabled = kNo_TriState;
1961 }
1962 }
1963 }
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001964 if (!smoothLines && rt->isMultisampled()) {
1965 // FIXME: GL_NV_pr doesn't seem to like MSAA disabled. The paths
1966 // convex hulls of each segment appear to get filled.
1967 bool enableMSAA = kStencilPath_DrawType == type ||
1968 this->getDrawState().isHWAntialiasState();
1969 if (enableMSAA) {
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00001970 if (kYes_TriState != fHWAAState.fMSAAEnabled) {
1971 GL_CALL(Enable(GR_GL_MULTISAMPLE));
1972 fHWAAState.fMSAAEnabled = kYes_TriState;
1973 }
1974 } else {
1975 if (kNo_TriState != fHWAAState.fMSAAEnabled) {
1976 GL_CALL(Disable(GR_GL_MULTISAMPLE));
1977 fHWAAState.fMSAAEnabled = kNo_TriState;
1978 }
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001979 }
1980 }
1981 }
1982}
1983
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00001984void GrGpuGL::flushBlend(bool isLines,
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001985 GrBlendCoeff srcCoeff,
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001986 GrBlendCoeff dstCoeff) {
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00001987 if (isLines && this->willUseHWAALines()) {
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001988 if (kYes_TriState != fHWBlendState.fEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001989 GL_CALL(Enable(GR_GL_BLEND));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001990 fHWBlendState.fEnabled = kYes_TriState;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001991 }
bsalomon@google.com47059542012-06-06 20:51:20 +00001992 if (kSA_GrBlendCoeff != fHWBlendState.fSrcCoeff ||
1993 kISA_GrBlendCoeff != fHWBlendState.fDstCoeff) {
1994 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[kSA_GrBlendCoeff],
1995 gXfermodeCoeff2Blend[kISA_GrBlendCoeff]));
1996 fHWBlendState.fSrcCoeff = kSA_GrBlendCoeff;
1997 fHWBlendState.fDstCoeff = kISA_GrBlendCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001998 }
1999 } else {
bsalomon@google.com86c1f712011-10-12 14:54:26 +00002000 // any optimization to disable blending should
2001 // have already been applied and tweaked the coeffs
2002 // to (1, 0).
bsalomon@google.com47059542012-06-06 20:51:20 +00002003 bool blendOff = kOne_GrBlendCoeff == srcCoeff &&
2004 kZero_GrBlendCoeff == dstCoeff;
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002005 if (blendOff) {
2006 if (kNo_TriState != fHWBlendState.fEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002007 GL_CALL(Disable(GR_GL_BLEND));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002008 fHWBlendState.fEnabled = kNo_TriState;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002009 }
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002010 } else {
2011 if (kYes_TriState != fHWBlendState.fEnabled) {
2012 GL_CALL(Enable(GR_GL_BLEND));
2013 fHWBlendState.fEnabled = kYes_TriState;
2014 }
2015 if (fHWBlendState.fSrcCoeff != srcCoeff ||
2016 fHWBlendState.fDstCoeff != dstCoeff) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002017 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
2018 gXfermodeCoeff2Blend[dstCoeff]));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002019 fHWBlendState.fSrcCoeff = srcCoeff;
2020 fHWBlendState.fDstCoeff = dstCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002021 }
bsalomon@google.coma5d056a2012-03-27 15:59:58 +00002022 GrColor blendConst = this->getDrawState().getBlendConstant();
bsalomon@google.com271cffc2011-05-20 14:13:56 +00002023 if ((BlendCoeffReferencesConstant(srcCoeff) ||
2024 BlendCoeffReferencesConstant(dstCoeff)) &&
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002025 (!fHWBlendState.fConstColorValid ||
2026 fHWBlendState.fConstColor != blendConst)) {
bsalomon@google.com75347472012-09-17 17:23:21 +00002027 GrGLfloat c[4];
2028 GrColorToRGBAFloat(blendConst, c);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002029 GL_CALL(BlendColor(c[0], c[1], c[2], c[3]));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002030 fHWBlendState.fConstColor = blendConst;
2031 fHWBlendState.fConstColorValid = true;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002032 }
2033 }
2034 }
2035}
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002036namespace {
2037
bsalomon@google.com6d003d12012-09-11 15:45:20 +00002038inline void set_tex_swizzle(GrGLenum swizzle[4], const GrGLInterface* gl) {
bsalomon@google.com4d063de2012-05-31 17:59:23 +00002039 GR_GL_CALL(gl, TexParameteriv(GR_GL_TEXTURE_2D,
2040 GR_GL_TEXTURE_SWIZZLE_RGBA,
2041 reinterpret_cast<const GrGLint*>(swizzle)));
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002042}
bsalomon@google.comb8670992012-07-25 21:27:09 +00002043
bsalomon@google.com6c76d242012-09-07 16:52:24 +00002044inline GrGLenum tile_to_gl_wrap(SkShader::TileMode tm) {
bsalomon@google.comb8670992012-07-25 21:27:09 +00002045 static const GrGLenum gWrapModes[] = {
2046 GR_GL_CLAMP_TO_EDGE,
2047 GR_GL_REPEAT,
2048 GR_GL_MIRRORED_REPEAT
2049 };
2050 GrAssert((unsigned) tm <= SK_ARRAY_COUNT(gWrapModes));
2051 GR_STATIC_ASSERT(0 == SkShader::kClamp_TileMode);
2052 GR_STATIC_ASSERT(1 == SkShader::kRepeat_TileMode);
2053 GR_STATIC_ASSERT(2 == SkShader::kMirror_TileMode);
2054 return gWrapModes[tm];
2055}
2056
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002057}
2058
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002059void GrGpuGL::bindTexture(int unitIdx, const GrTextureParams& params, GrGLTexture* texture) {
2060 GrAssert(NULL != texture);
tomhudson@google.comd0c1a062012-07-12 17:23:52 +00002061
bsalomon@google.comb8670992012-07-25 21:27:09 +00002062 // If we created a rt/tex and rendered to it without using a texture and now we're texturing
2063 // 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 +00002064 // out of the "last != next" check.
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002065 GrGLRenderTarget* texRT = static_cast<GrGLRenderTarget*>(texture->asRenderTarget());
bsalomon@google.com4c883782012-06-04 19:05:11 +00002066 if (NULL != texRT) {
2067 this->onResolveRenderTarget(texRT);
2068 }
2069
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002070 if (fHWBoundTextures[unitIdx] != texture) {
2071 this->setTextureUnit(unitIdx);
2072 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, texture->textureID()));
2073 fHWBoundTextures[unitIdx] = texture;
bsalomon@google.com4c883782012-06-04 19:05:11 +00002074 }
2075
bsalomon@google.com4c883782012-06-04 19:05:11 +00002076 ResetTimestamp timestamp;
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002077 const GrGLTexture::TexParams& oldTexParams = texture->getCachedTexParams(&timestamp);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002078 bool setAll = timestamp < this->getResetTimestamp();
2079 GrGLTexture::TexParams newTexParams;
2080
bsalomon@google.comb8670992012-07-25 21:27:09 +00002081 newTexParams.fFilter = params.isBilerp() ? GR_GL_LINEAR : GR_GL_NEAREST;
bsalomon@google.com4c883782012-06-04 19:05:11 +00002082
bsalomon@google.comb8670992012-07-25 21:27:09 +00002083 newTexParams.fWrapS = tile_to_gl_wrap(params.getTileModeX());
2084 newTexParams.fWrapT = tile_to_gl_wrap(params.getTileModeY());
bsalomon@google.com4c883782012-06-04 19:05:11 +00002085 memcpy(newTexParams.fSwizzleRGBA,
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002086 GrGLShaderBuilder::GetTexParamSwizzle(texture->config(), this->glCaps()),
bsalomon@google.com4c883782012-06-04 19:05:11 +00002087 sizeof(newTexParams.fSwizzleRGBA));
2088 if (setAll || newTexParams.fFilter != oldTexParams.fFilter) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002089 this->setTextureUnit(unitIdx);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002090 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2091 GR_GL_TEXTURE_MAG_FILTER,
2092 newTexParams.fFilter));
2093 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2094 GR_GL_TEXTURE_MIN_FILTER,
2095 newTexParams.fFilter));
2096 }
2097 if (setAll || newTexParams.fWrapS != oldTexParams.fWrapS) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002098 this->setTextureUnit(unitIdx);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002099 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2100 GR_GL_TEXTURE_WRAP_S,
2101 newTexParams.fWrapS));
2102 }
2103 if (setAll || newTexParams.fWrapT != oldTexParams.fWrapT) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002104 this->setTextureUnit(unitIdx);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002105 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2106 GR_GL_TEXTURE_WRAP_T,
2107 newTexParams.fWrapT));
2108 }
2109 if (this->glCaps().textureSwizzleSupport() &&
2110 (setAll || memcmp(newTexParams.fSwizzleRGBA,
2111 oldTexParams.fSwizzleRGBA,
2112 sizeof(newTexParams.fSwizzleRGBA)))) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002113 this->setTextureUnit(unitIdx);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002114 set_tex_swizzle(newTexParams.fSwizzleRGBA,
2115 this->glInterface());
2116 }
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002117 texture->setCachedTexParams(newTexParams, this->getResetTimestamp());
bsalomon@google.com4c883782012-06-04 19:05:11 +00002118}
2119
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002120void GrGpuGL::flushMiscFixedFunctionState() {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002121
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002122 const GrDrawState& drawState = this->getDrawState();
reed@google.comac10a2d2010-12-22 21:39:39 +00002123
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002124 if (drawState.isDitherState()) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002125 if (kYes_TriState != fHWDitherEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002126 GL_CALL(Enable(GR_GL_DITHER));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002127 fHWDitherEnabled = kYes_TriState;
2128 }
2129 } else {
2130 if (kNo_TriState != fHWDitherEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002131 GL_CALL(Disable(GR_GL_DITHER));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002132 fHWDitherEnabled = kNo_TriState;
reed@google.comac10a2d2010-12-22 21:39:39 +00002133 }
2134 }
2135
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002136 if (drawState.isColorWriteDisabled()) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002137 if (kNo_TriState != fHWWriteToColor) {
2138 GL_CALL(ColorMask(GR_GL_FALSE, GR_GL_FALSE,
2139 GR_GL_FALSE, GR_GL_FALSE));
2140 fHWWriteToColor = kNo_TriState;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002141 }
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002142 } else {
2143 if (kYes_TriState != fHWWriteToColor) {
2144 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
2145 fHWWriteToColor = kYes_TriState;
2146 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00002147 }
2148
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002149 if (fHWDrawFace != drawState.getDrawFace()) {
bsalomon@google.coma5d056a2012-03-27 15:59:58 +00002150 switch (this->getDrawState().getDrawFace()) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002151 case GrDrawState::kCCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002152 GL_CALL(Enable(GR_GL_CULL_FACE));
2153 GL_CALL(CullFace(GR_GL_BACK));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002154 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002155 case GrDrawState::kCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002156 GL_CALL(Enable(GR_GL_CULL_FACE));
2157 GL_CALL(CullFace(GR_GL_FRONT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002158 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002159 case GrDrawState::kBoth_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002160 GL_CALL(Disable(GR_GL_CULL_FACE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002161 break;
2162 default:
2163 GrCrash("Unknown draw face.");
2164 }
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002165 fHWDrawFace = drawState.getDrawFace();
bsalomon@google.comd302f142011-03-03 13:54:13 +00002166 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002167}
2168
reed@google.comac10a2d2010-12-22 21:39:39 +00002169void GrGpuGL::notifyRenderTargetDelete(GrRenderTarget* renderTarget) {
2170 GrAssert(NULL != renderTarget);
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002171 if (fHWBoundRenderTarget == renderTarget) {
2172 fHWBoundRenderTarget = NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +00002173 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002174}
2175
2176void GrGpuGL::notifyTextureDelete(GrGLTexture* texture) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002177 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002178 if (fHWBoundTextures[s] == texture) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002179 // deleting bound texture does implied bind to 0
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002180 fHWBoundTextures[s] = NULL;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002181 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002182 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002183}
2184
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002185bool GrGpuGL::configToGLFormats(GrPixelConfig config,
2186 bool getSizedInternalFormat,
2187 GrGLenum* internalFormat,
2188 GrGLenum* externalFormat,
2189 GrGLenum* externalType) {
2190 GrGLenum dontCare;
2191 if (NULL == internalFormat) {
2192 internalFormat = &dontCare;
2193 }
2194 if (NULL == externalFormat) {
2195 externalFormat = &dontCare;
2196 }
2197 if (NULL == externalType) {
2198 externalType = &dontCare;
2199 }
2200
reed@google.comac10a2d2010-12-22 21:39:39 +00002201 switch (config) {
bsalomon@google.com0342a852012-08-20 19:22:38 +00002202 case kRGBA_8888_GrPixelConfig:
bsalomon@google.comc4364992011-11-07 15:54:49 +00002203 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002204 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002205 if (getSizedInternalFormat) {
2206 *internalFormat = GR_GL_RGBA8;
2207 } else {
2208 *internalFormat = GR_GL_RGBA;
2209 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002210 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.comc4364992011-11-07 15:54:49 +00002211 break;
bsalomon@google.com0342a852012-08-20 19:22:38 +00002212 case kBGRA_8888_GrPixelConfig:
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002213 if (!this->glCaps().bgraFormatSupport()) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002214 return false;
2215 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002216 if (this->glCaps().bgraIsInternalFormat()) {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002217 if (getSizedInternalFormat) {
2218 *internalFormat = GR_GL_BGRA8;
2219 } else {
2220 *internalFormat = GR_GL_BGRA;
2221 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002222 } else {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002223 if (getSizedInternalFormat) {
2224 *internalFormat = GR_GL_RGBA8;
2225 } else {
2226 *internalFormat = GR_GL_RGBA;
2227 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002228 }
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002229 *externalFormat = GR_GL_BGRA;
bsalomon@google.com6f379512011-11-16 20:36:03 +00002230 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002231 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002232 case kRGB_565_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002233 *internalFormat = GR_GL_RGB;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002234 *externalFormat = GR_GL_RGB;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002235 if (getSizedInternalFormat) {
2236 if (this->glBinding() == kDesktop_GrGLBinding) {
2237 return false;
2238 } else {
2239 *internalFormat = GR_GL_RGB565;
2240 }
2241 } else {
2242 *internalFormat = GR_GL_RGB;
2243 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002244 *externalType = GR_GL_UNSIGNED_SHORT_5_6_5;
reed@google.comac10a2d2010-12-22 21:39:39 +00002245 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002246 case kRGBA_4444_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002247 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002248 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002249 if (getSizedInternalFormat) {
2250 *internalFormat = GR_GL_RGBA4;
2251 } else {
2252 *internalFormat = GR_GL_RGBA;
2253 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002254 *externalType = GR_GL_UNSIGNED_SHORT_4_4_4_4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002255 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002256 case kIndex_8_GrPixelConfig:
bsalomon@google.comf6601872012-08-28 21:11:35 +00002257 if (this->getCaps().eightBitPaletteSupport()) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002258 *internalFormat = GR_GL_PALETTE8_RGBA8;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002259 // glCompressedTexImage doesn't take external params
2260 *externalFormat = GR_GL_PALETTE8_RGBA8;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002261 // no sized/unsized internal format distinction here
2262 *internalFormat = GR_GL_PALETTE8_RGBA8;
2263 // unused with CompressedTexImage
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002264 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002265 } else {
2266 return false;
2267 }
2268 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002269 case kAlpha_8_GrPixelConfig:
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002270 if (this->glCaps().textureRedSupport()) {
2271 *internalFormat = GR_GL_RED;
2272 *externalFormat = GR_GL_RED;
2273 if (getSizedInternalFormat) {
2274 *internalFormat = GR_GL_R8;
2275 } else {
2276 *internalFormat = GR_GL_RED;
2277 }
2278 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002279 } else {
2280 *internalFormat = GR_GL_ALPHA;
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002281 *externalFormat = GR_GL_ALPHA;
2282 if (getSizedInternalFormat) {
2283 *internalFormat = GR_GL_ALPHA8;
2284 } else {
2285 *internalFormat = GR_GL_ALPHA;
2286 }
2287 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002288 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002289 break;
2290 default:
2291 return false;
2292 }
2293 return true;
2294}
2295
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002296void GrGpuGL::setTextureUnit(int unit) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002297 GrAssert(unit >= 0 && unit < GrDrawState::kNumStages);
bsalomon@google.com49209392012-06-05 15:13:46 +00002298 if (fHWActiveTextureUnitIdx != unit) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002299 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com49209392012-06-05 15:13:46 +00002300 fHWActiveTextureUnitIdx = unit;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002301 }
2302}
bsalomon@google.com316f99232011-01-13 21:28:12 +00002303
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002304void GrGpuGL::setSpareTextureUnit() {
bsalomon@google.com49209392012-06-05 15:13:46 +00002305 if (fHWActiveTextureUnitIdx != (GR_GL_TEXTURE0 + SPARE_TEX_UNIT)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002306 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + SPARE_TEX_UNIT));
bsalomon@google.com49209392012-06-05 15:13:46 +00002307 fHWActiveTextureUnitIdx = SPARE_TEX_UNIT;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002308 }
2309}
2310
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00002311///////////////////////////////////////////////////////////////////////////////
2312
bsalomon@google.com6918d482013-03-07 19:09:11 +00002313GrGLAttribArrayState* GrGpuGL::HWGeometryState::bindArrayAndBuffersToDraw(
2314 GrGpuGL* gpu,
2315 const GrGLVertexBuffer* vbuffer,
2316 const GrGLIndexBuffer* ibuffer) {
2317 GrAssert(NULL != vbuffer);
2318 GrGLAttribArrayState* attribState = &fDefaultVertexArrayAttribState;
2319 // We use a vertex array if we're on a core profile and the verts are in a VBO.
2320 if (gpu->glCaps().isCoreProfile() && !vbuffer->isCPUBacked()) {
2321 if (NULL == fVBOVertexArray || !fVBOVertexArray->isValid()) {
2322 SkSafeUnref(fVBOVertexArray);
2323 GrGLuint arrayID;
2324 GR_GL_CALL(gpu->glInterface(), GenVertexArrays(1, &arrayID));
2325 int attrCount = gpu->glCaps().maxVertexAttributes();
2326 fVBOVertexArray = SkNEW_ARGS(GrGLVertexArray, (gpu, arrayID, attrCount));
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00002327 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00002328 attribState = fVBOVertexArray->bindWithIndexBuffer(ibuffer);
2329 } else {
2330 if (NULL != ibuffer) {
2331 this->setIndexBufferIDOnDefaultVertexArray(gpu, ibuffer->bufferID());
2332 } else {
2333 this->setVertexArrayID(gpu, 0);
2334 }
2335 int attrCount = gpu->glCaps().maxVertexAttributes();
2336 if (fDefaultVertexArrayAttribState.count() != attrCount) {
2337 fDefaultVertexArrayAttribState.resize(attrCount);
2338 }
2339 attribState = &fDefaultVertexArrayAttribState;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00002340 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00002341 return attribState;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002342}