blob: e7d3a946a4ee61e725a7a3cbb488373f17c2c3e7 [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.comf3a60c02013-03-19 19:06:09 +0000288 caps->fShaderDerivativeSupport = this->hasExtension("GL_OES_standard_derivatives");
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000289 }
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000290
bsalomon@google.comf3a60c02013-03-19 19:06:09 +0000291 if (GrGLCaps::kImaginationES_MSFBOType == this->glCaps().msFBOType()) {
292 GR_GL_GetIntegerv(this->glInterface(), GR_GL_MAX_SAMPLES_IMG, &caps->fMaxSampleCount);
293 } else if (GrGLCaps::kNone_MSFBOType != this->glCaps().msFBOType()) {
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000294 GR_GL_GetIntegerv(this->glInterface(), GR_GL_MAX_SAMPLES, &caps->fMaxSampleCount);
295 }
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000296}
297
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000298void GrGpuGL::fillInConfigRenderableTable() {
299
300 // OpenGL < 3.0
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000301 // no support for render targets unless the GL_ARB_framebuffer_object
302 // extension is supported (in which case we get ALPHA, RED, RG, RGB,
303 // RGBA (ALPHA8, RGBA4, RGBA8) for OpenGL > 1.1). Note that we
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000304 // probably don't get R8 in this case.
305
306 // OpenGL 3.0
307 // base color renderable: ALPHA, RED, RG, RGB, and RGBA
308 // sized derivatives: ALPHA8, R8, RGBA4, RGBA8
309
310 // >= OpenGL 3.1
311 // base color renderable: RED, RG, RGB, and RGBA
312 // sized derivatives: R8, RGBA4, RGBA8
313 // if the GL_ARB_compatibility extension is supported then we get back
314 // support for GL_ALPHA and ALPHA8
315
316 // GL_EXT_bgra adds BGRA render targets to any version
317
318 // ES 2.0
319 // color renderable: RGBA4, RGB5_A1, RGB565
320 // GL_EXT_texture_rg adds support for R8 as a color render target
321 // GL_OES_rgb8_rgba8 and/or GL_ARM_rgba8 adds support for RGBA8
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000322 // GL_EXT_texture_format_BGRA8888 and/or GL_APPLE_texture_format_BGRA8888 added BGRA support
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000323
324 if (kDesktop_GrGLBinding == this->glBinding()) {
325 // Post 3.0 we will get R8
326 // Prior to 3.0 we will get ALPHA8 (with GL_ARB_framebuffer_object)
327 if (this->glVersion() >= GR_GL_VER(3,0) ||
328 this->hasExtension("GL_ARB_framebuffer_object")) {
329 fConfigRenderSupport[kAlpha_8_GrPixelConfig] = true;
330 }
331 } else {
332 // On ES we can only hope for R8
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000333 fConfigRenderSupport[kAlpha_8_GrPixelConfig] =
robertphillips@google.com443e5a52012-04-30 20:01:21 +0000334 this->glCaps().textureRedSupport();
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000335 }
336
337 if (kDesktop_GrGLBinding != this->glBinding()) {
338 // only available in ES
339 fConfigRenderSupport[kRGB_565_GrPixelConfig] = true;
340 }
341
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000342 // Pre 3.0, Ganesh relies on either GL_ARB_framebuffer_object or
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000343 // GL_EXT_framebuffer_object for FBO support. Both of these
344 // allow RGBA4 render targets so this is always supported.
345 fConfigRenderSupport[kRGBA_4444_GrPixelConfig] = true;
346
347 if (this->glCaps().rgba8RenderbufferSupport()) {
bsalomon@google.com0342a852012-08-20 19:22:38 +0000348 fConfigRenderSupport[kRGBA_8888_GrPixelConfig] = true;
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000349 }
350
351 if (this->glCaps().bgraFormatSupport()) {
bsalomon@google.com0342a852012-08-20 19:22:38 +0000352 fConfigRenderSupport[kBGRA_8888_GrPixelConfig] = true;
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000353 }
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000354}
355
bsalomon@google.com9c680582013-02-06 18:17:50 +0000356namespace {
357GrPixelConfig preferred_pixel_ops_config(GrPixelConfig config) {
358 if (GR_GL_RGBA_8888_PIXEL_OPS_SLOW && kRGBA_8888_GrPixelConfig == config) {
359 return kBGRA_8888_GrPixelConfig;
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000360 } else {
361 return config;
362 }
363}
bsalomon@google.com9c680582013-02-06 18:17:50 +0000364}
365
366GrPixelConfig GrGpuGL::preferredReadPixelsConfig(GrPixelConfig config) const {
367 return preferred_pixel_ops_config(config);
368}
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000369
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000370GrPixelConfig GrGpuGL::preferredWritePixelsConfig(GrPixelConfig config) const {
bsalomon@google.com9c680582013-02-06 18:17:50 +0000371 return preferred_pixel_ops_config(config);
372}
373
374bool GrGpuGL::canWriteTexturePixels(const GrTexture* texture, GrPixelConfig srcConfig) const {
375 if (kIndex_8_GrPixelConfig == srcConfig || kIndex_8_GrPixelConfig == texture->config()) {
376 return false;
377 }
378 if (srcConfig != texture->config() && kES2_GrGLBinding == this->glBinding()) {
379 // In general ES2 requires the internal format of the texture and the format of the src
380 // pixels to match. However, It may or may not be possible to upload BGRA data to a RGBA
381 // texture. It depends upon which extension added BGRA. The Apple extension allows it
382 // (BGRA's internal format is RGBA) while the EXT extension does not (BGRA is its own
383 // internal format).
384 if (this->glCaps().bgraFormatSupport() &&
385 !this->glCaps().bgraIsInternalFormat() &&
386 kBGRA_8888_GrPixelConfig == srcConfig &&
387 kRGBA_8888_GrPixelConfig == texture->config()) {
388 return true;
389 } else {
390 return false;
391 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000392 } else {
bsalomon@google.com9c680582013-02-06 18:17:50 +0000393 return true;
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000394 }
395}
396
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000397bool GrGpuGL::fullReadPixelsIsFasterThanPartial() const {
398 return SkToBool(GR_GL_FULL_READPIXELS_FASTER_THAN_PARTIAL);
399}
400
bsalomon@google.com1bf1c212011-11-05 12:18:58 +0000401void GrGpuGL::onResetContext() {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000402 if (gPrintStartupSpew && !fPrintedCaps) {
403 fPrintedCaps = true;
404 this->getCaps().print();
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000405 this->glCaps().print();
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000406 }
407
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000408 // we don't use the zb at all
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000409 GL_CALL(Disable(GR_GL_DEPTH_TEST));
410 GL_CALL(DepthMask(GR_GL_FALSE));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000411
bsalomon@google.com978c8c62012-05-21 14:45:49 +0000412 fHWDrawFace = GrDrawState::kInvalid_DrawFace;
413 fHWDitherEnabled = kUnknown_TriState;
reed@google.comac10a2d2010-12-22 21:39:39 +0000414
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000415 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com469d0dd2012-05-21 20:14:29 +0000416 // Desktop-only state that we never change
bsalomon@google.com2b1b8c02013-02-28 22:06:02 +0000417 if (!this->glCaps().isCoreProfile()) {
418 GL_CALL(Disable(GR_GL_POINT_SMOOTH));
419 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
420 GL_CALL(Disable(GR_GL_POLYGON_SMOOTH));
421 GL_CALL(Disable(GR_GL_POLYGON_STIPPLE));
422 GL_CALL(Disable(GR_GL_COLOR_LOGIC_OP));
423 GL_CALL(Disable(GR_GL_INDEX_LOGIC_OP));
424 }
commit-bot@chromium.orgdd4400b2013-03-21 19:58:22 +0000425 // The windows NVIDIA driver has GL_ARB_imaging in the extension string when using a core
426 // profile. This seems like a bug since the core spec removes any mention of GL_ARB_imaging.
427 if (this->glCaps().imagingSupport() && !this->glCaps().isCoreProfile()) {
bsalomon@google.come76b7cc2012-06-18 12:47:06 +0000428 GL_CALL(Disable(GR_GL_COLOR_TABLE));
429 }
bsalomon@google.com469d0dd2012-05-21 20:14:29 +0000430 GL_CALL(Disable(GR_GL_POLYGON_OFFSET_FILL));
431 // Since ES doesn't support glPointSize at all we always use the VS to
432 // set the point size
433 GL_CALL(Enable(GR_GL_VERTEX_PROGRAM_POINT_SIZE));
434
435 // We should set glPolygonMode(FRONT_AND_BACK,FILL) here, too. It isn't
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000436 // currently part of our gl interface. There are probably others as
bsalomon@google.com469d0dd2012-05-21 20:14:29 +0000437 // well.
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000438 }
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +0000439 fHWAAState.invalidate();
bsalomon@google.com978c8c62012-05-21 14:45:49 +0000440 fHWWriteToColor = kUnknown_TriState;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000441
reed@google.comac10a2d2010-12-22 21:39:39 +0000442 // we only ever use lines in hairline mode
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000443 GL_CALL(LineWidth(1));
reed@google.comac10a2d2010-12-22 21:39:39 +0000444
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000445 // invalid
bsalomon@google.com49209392012-06-05 15:13:46 +0000446 fHWActiveTextureUnitIdx = -1;
reed@google.comac10a2d2010-12-22 21:39:39 +0000447
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +0000448 fHWBlendState.invalidate();
bsalomon@google.com080773c2011-03-15 19:09:25 +0000449
tomhudson@google.com93813632011-10-27 20:21:16 +0000450 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.comc811ea32012-05-21 15:33:09 +0000451 fHWBoundTextures[s] = NULL;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000452 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000453
bsalomon@google.coma3201942012-06-21 19:58:20 +0000454 fHWScissorSettings.invalidate();
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000455
bsalomon@google.coma3201942012-06-21 19:58:20 +0000456 fHWViewport.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000457
bsalomon@google.com457b8a32012-05-21 21:19:58 +0000458 fHWStencilSettings.invalidate();
bsalomon@google.coma3201942012-06-21 19:58:20 +0000459 fHWStencilTestEnabled = kUnknown_TriState;
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000460
bsalomon@google.com880b8fc2013-02-19 20:17:28 +0000461 fHWGeometryState.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000462
bsalomon@google.comc811ea32012-05-21 15:33:09 +0000463 fHWBoundRenderTarget = NULL;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000464
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +0000465 fHWPathStencilMatrixState.invalidate();
bsalomon@google.comf6601872012-08-28 21:11:35 +0000466 if (fCaps.pathStencilingSupport()) {
bsalomon@google.com05a718c2012-06-29 14:01:53 +0000467 // we don't use the model view matrix.
468 GL_CALL(MatrixMode(GR_GL_MODELVIEW));
469 GL_CALL(LoadIdentity());
470 }
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000471
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000472 // we assume these values
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000473 if (this->glCaps().unpackRowLengthSupport()) {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000474 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
475 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000476 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000477 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
478 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000479 if (this->glCaps().unpackFlipYSupport()) {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000480 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
481 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000482 if (this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000483 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, GR_GL_FALSE));
484 }
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000485
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000486 fHWProgramID = 0;
bsalomon@google.com91207482013-02-12 21:45:24 +0000487 fSharedGLProgramState.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000488}
489
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000490namespace {
491
492GrSurfaceOrigin resolve_origin(GrSurfaceOrigin origin, bool renderTarget) {
493 // By default, GrRenderTargets are GL's normal orientation so that they
494 // can be drawn to by the outside world without the client having
495 // to render upside down.
496 if (kDefault_GrSurfaceOrigin == origin) {
497 return renderTarget ? kBottomLeft_GrSurfaceOrigin : kTopLeft_GrSurfaceOrigin;
498 } else {
499 return origin;
500 }
501}
502
503}
504
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000505GrTexture* GrGpuGL::onWrapBackendTexture(const GrBackendTextureDesc& desc) {
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000506 if (!this->configToGLFormats(desc.fConfig, false, NULL, NULL, NULL)) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000507 return NULL;
508 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000509
robertphillips@google.comb72e5d32012-10-30 15:18:10 +0000510 if (0 == desc.fTextureHandle) {
511 return NULL;
512 }
513
514 int maxSize = this->getCaps().maxTextureSize();
515 if (desc.fWidth > maxSize || desc.fHeight > maxSize) {
516 return NULL;
517 }
518
519 GrGLTexture::Desc glTexDesc;
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000520 // next line relies on GrBackendTextureDesc's flags matching GrTexture's
robertphillips@google.com32716282012-06-04 12:48:45 +0000521 glTexDesc.fFlags = (GrTextureFlags) desc.fFlags;
bsalomon@google.com99621082011-11-15 16:47:16 +0000522 glTexDesc.fWidth = desc.fWidth;
523 glTexDesc.fHeight = desc.fHeight;
bsalomon@google.come269f212011-11-07 13:29:52 +0000524 glTexDesc.fConfig = desc.fConfig;
robertphillips@google.com32716282012-06-04 12:48:45 +0000525 glTexDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.come269f212011-11-07 13:29:52 +0000526 glTexDesc.fTextureID = static_cast<GrGLuint>(desc.fTextureHandle);
bsalomon@google.com72830222013-01-23 20:25:22 +0000527 glTexDesc.fIsWrapped = true;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000528 bool renderTarget = 0 != (desc.fFlags & kRenderTarget_GrBackendTextureFlag);
529 // FIXME: this should be calling resolve_origin(), but Chrome code is currently
530 // assuming the old behaviour, which is that backend textures are always
531 // BottomLeft, even for non-RT's. Once Chrome is fixed, change this to:
532 // glTexDesc.fOrigin = resolve_origin(desc.fOrigin, renderTarget);
533 if (kDefault_GrSurfaceOrigin == desc.fOrigin) {
534 glTexDesc.fOrigin = kBottomLeft_GrSurfaceOrigin;
535 } else {
536 glTexDesc.fOrigin = desc.fOrigin;
537 }
bsalomon@google.come269f212011-11-07 13:29:52 +0000538
539 GrGLTexture* texture = NULL;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000540 if (renderTarget) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000541 GrGLRenderTarget::Desc glRTDesc;
542 glRTDesc.fRTFBOID = 0;
543 glRTDesc.fTexFBOID = 0;
544 glRTDesc.fMSColorRenderbufferID = 0;
bsalomon@google.come269f212011-11-07 13:29:52 +0000545 glRTDesc.fConfig = desc.fConfig;
546 glRTDesc.fSampleCnt = desc.fSampleCnt;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000547 glRTDesc.fOrigin = glTexDesc.fOrigin;
bsalomon@google.com99621082011-11-15 16:47:16 +0000548 if (!this->createRenderTargetObjects(glTexDesc.fWidth,
549 glTexDesc.fHeight,
bsalomon@google.come269f212011-11-07 13:29:52 +0000550 glTexDesc.fTextureID,
551 &glRTDesc)) {
552 return NULL;
553 }
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000554 texture = SkNEW_ARGS(GrGLTexture, (this, glTexDesc, glRTDesc));
bsalomon@google.come269f212011-11-07 13:29:52 +0000555 } else {
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000556 texture = SkNEW_ARGS(GrGLTexture, (this, glTexDesc));
bsalomon@google.come269f212011-11-07 13:29:52 +0000557 }
558 if (NULL == texture) {
559 return NULL;
560 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000561
bsalomon@google.come269f212011-11-07 13:29:52 +0000562 this->setSpareTextureUnit();
563 return texture;
564}
565
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000566GrRenderTarget* GrGpuGL::onWrapBackendRenderTarget(const GrBackendRenderTargetDesc& desc) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000567 GrGLRenderTarget::Desc glDesc;
568 glDesc.fConfig = desc.fConfig;
569 glDesc.fRTFBOID = static_cast<GrGLuint>(desc.fRenderTargetHandle);
570 glDesc.fMSColorRenderbufferID = 0;
571 glDesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
572 glDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.com72830222013-01-23 20:25:22 +0000573 glDesc.fIsWrapped = true;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000574 glDesc.fOrigin = desc.fOrigin;
575 if (glDesc.fRTFBOID == 0) {
576 GrAssert(desc.fOrigin == kBottomLeft_GrSurfaceOrigin);
577 }
578
579 glDesc.fOrigin = resolve_origin(desc.fOrigin, true);
bsalomon@google.come269f212011-11-07 13:29:52 +0000580 GrGLIRect viewport;
581 viewport.fLeft = 0;
582 viewport.fBottom = 0;
583 viewport.fWidth = desc.fWidth;
584 viewport.fHeight = desc.fHeight;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000585
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000586 GrRenderTarget* tgt = SkNEW_ARGS(GrGLRenderTarget,
587 (this, glDesc, viewport));
bsalomon@google.come269f212011-11-07 13:29:52 +0000588 if (desc.fStencilBits) {
589 GrGLStencilBuffer::Format format;
590 format.fInternalFormat = GrGLStencilBuffer::kUnknownInternalFormat;
591 format.fPacked = false;
592 format.fStencilBits = desc.fStencilBits;
593 format.fTotalBits = desc.fStencilBits;
bsalomon@google.com1f0f1a32013-01-23 21:32:32 +0000594 static const bool kIsSBWrapped = false;
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000595 GrGLStencilBuffer* sb = SkNEW_ARGS(GrGLStencilBuffer,
596 (this,
bsalomon@google.com1f0f1a32013-01-23 21:32:32 +0000597 kIsSBWrapped,
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000598 0,
599 desc.fWidth,
600 desc.fHeight,
601 desc.fSampleCnt,
602 format));
bsalomon@google.come269f212011-11-07 13:29:52 +0000603 tgt->setStencilBuffer(sb);
604 sb->unref();
605 }
606 return tgt;
607}
608
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000609////////////////////////////////////////////////////////////////////////////////
610
bsalomon@google.com9c680582013-02-06 18:17:50 +0000611bool GrGpuGL::onWriteTexturePixels(GrTexture* texture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000612 int left, int top, int width, int height,
613 GrPixelConfig config, const void* buffer,
614 size_t rowBytes) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000615 if (NULL == buffer) {
bsalomon@google.com9c680582013-02-06 18:17:50 +0000616 return false;
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000617 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000618 GrGLTexture* glTex = static_cast<GrGLTexture*>(texture);
619
bsalomon@google.com6f379512011-11-16 20:36:03 +0000620 this->setSpareTextureUnit();
621 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTex->textureID()));
622 GrGLTexture::Desc desc;
robertphillips@google.com32716282012-06-04 12:48:45 +0000623 desc.fFlags = glTex->desc().fFlags;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000624 desc.fWidth = glTex->width();
625 desc.fHeight = glTex->height();
robertphillips@google.com32716282012-06-04 12:48:45 +0000626 desc.fConfig = glTex->config();
627 desc.fSampleCnt = glTex->desc().fSampleCnt;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000628 desc.fTextureID = glTex->textureID();
bsalomon@google.com2d0bade2012-10-26 19:01:17 +0000629 desc.fOrigin = glTex->origin();
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000630
bsalomon@google.com9c680582013-02-06 18:17:50 +0000631 return this->uploadTexData(desc, false,
632 left, top, width, height,
633 config, buffer, rowBytes);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000634}
635
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000636namespace {
637bool adjust_pixel_ops_params(int surfaceWidth,
638 int surfaceHeight,
639 size_t bpp,
640 int* left, int* top, int* width, int* height,
641 const void** data,
642 size_t* rowBytes) {
643 if (!*rowBytes) {
644 *rowBytes = *width * bpp;
645 }
646
647 GrIRect subRect = GrIRect::MakeXYWH(*left, *top, *width, *height);
648 GrIRect bounds = GrIRect::MakeWH(surfaceWidth, surfaceHeight);
649
650 if (!subRect.intersect(bounds)) {
651 return false;
652 }
653 *data = reinterpret_cast<const void*>(reinterpret_cast<intptr_t>(*data) +
654 (subRect.fTop - *top) * *rowBytes + (subRect.fLeft - *left) * bpp);
655
656 *left = subRect.fLeft;
657 *top = subRect.fTop;
658 *width = subRect.width();
659 *height = subRect.height();
660 return true;
661}
662}
663
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000664bool GrGpuGL::uploadTexData(const GrGLTexture::Desc& desc,
665 bool isNewTexture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000666 int left, int top, int width, int height,
667 GrPixelConfig dataConfig,
668 const void* data,
669 size_t rowBytes) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000670 GrAssert(NULL != data || isNewTexture);
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000671
672 size_t bpp = GrBytesPerPixel(dataConfig);
673 if (!adjust_pixel_ops_params(desc.fWidth, desc.fHeight, bpp, &left, &top,
674 &width, &height, &data, &rowBytes)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000675 return false;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000676 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000677 size_t trimRowBytes = width * bpp;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000678
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000679 // in case we need a temporary, trimmed copy of the src pixels
680 SkAutoSMalloc<128 * 128> tempStorage;
681
bsalomon@google.com313f0192012-07-10 17:21:02 +0000682 // paletted textures cannot be partially updated
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000683 bool useTexStorage = isNewTexture &&
bsalomon@google.com313f0192012-07-10 17:21:02 +0000684 desc.fConfig != kIndex_8_GrPixelConfig &&
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000685 this->glCaps().texStorageSupport();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000686
bsalomon@google.com313f0192012-07-10 17:21:02 +0000687 if (useTexStorage && kDesktop_GrGLBinding == this->glBinding()) {
688 // 565 is not a sized internal format on desktop GL. So on desktop with
689 // 565 we always use an unsized internal format to let the system pick
690 // the best sized format to convert the 565 data to. Since TexStorage
691 // only allows sized internal formats we will instead use TexImage2D.
692 useTexStorage = desc.fConfig != kRGB_565_GrPixelConfig;
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000693 }
694
695 GrGLenum internalFormat;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000696 GrGLenum externalFormat;
697 GrGLenum externalType;
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000698 // glTexStorage requires sized internal formats on both desktop and ES. ES
699 // glTexImage requires an unsized format.
700 if (!this->configToGLFormats(dataConfig, useTexStorage, &internalFormat,
701 &externalFormat, &externalType)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000702 return false;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000703 }
704
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000705 if (!isNewTexture && GR_GL_PALETTE8_RGBA8 == internalFormat) {
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000706 // paletted textures cannot be updated
707 return false;
708 }
709
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000710 /*
bsalomon@google.com6f379512011-11-16 20:36:03 +0000711 * check whether to allocate a temporary buffer for flipping y or
712 * because our srcData has extra bytes past each row. If so, we need
713 * to trim those off here, since GL ES may not let us specify
714 * GL_UNPACK_ROW_LENGTH.
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000715 */
bsalomon@google.com6f379512011-11-16 20:36:03 +0000716 bool restoreGLRowLength = false;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000717 bool swFlipY = false;
718 bool glFlipY = false;
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000719 if (NULL != data) {
senorblanco@chromium.orgef5dbe12013-01-28 16:42:38 +0000720 if (kBottomLeft_GrSurfaceOrigin == desc.fOrigin) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000721 if (this->glCaps().unpackFlipYSupport()) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000722 glFlipY = true;
723 } else {
724 swFlipY = true;
725 }
726 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000727 if (this->glCaps().unpackRowLengthSupport() && !swFlipY) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000728 // can't use this for flipping, only non-neg values allowed. :(
729 if (rowBytes != trimRowBytes) {
730 GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
731 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength));
732 restoreGLRowLength = true;
733 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000734 } else {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000735 if (trimRowBytes != rowBytes || swFlipY) {
736 // copy data into our new storage, skipping the trailing bytes
737 size_t trimSize = height * trimRowBytes;
738 const char* src = (const char*)data;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000739 if (swFlipY) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000740 src += (height - 1) * rowBytes;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000741 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000742 char* dst = (char*)tempStorage.reset(trimSize);
743 for (int y = 0; y < height; y++) {
744 memcpy(dst, src, trimRowBytes);
745 if (swFlipY) {
746 src -= rowBytes;
747 } else {
748 src += rowBytes;
749 }
750 dst += trimRowBytes;
751 }
752 // now point data to our copied version
753 data = tempStorage.get();
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000754 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000755 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000756 if (glFlipY) {
757 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_TRUE));
758 }
759 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, static_cast<GrGLint>(bpp)));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000760 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000761 bool succeeded = true;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000762 if (isNewTexture &&
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000763 0 == left && 0 == top &&
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000764 desc.fWidth == width && desc.fHeight == height) {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000765 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000766 if (useTexStorage) {
767 // We never resize or change formats of textures. We don't use
768 // mipmaps currently.
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000769 GL_ALLOC_CALL(this->glInterface(),
770 TexStorage2D(GR_GL_TEXTURE_2D,
771 1, // levels
772 internalFormat,
773 desc.fWidth, desc.fHeight));
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000774 } else {
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000775 if (GR_GL_PALETTE8_RGBA8 == internalFormat) {
776 GrGLsizei imageSize = desc.fWidth * desc.fHeight +
777 kGrColorTableSize;
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000778 GL_ALLOC_CALL(this->glInterface(),
779 CompressedTexImage2D(GR_GL_TEXTURE_2D,
780 0, // level
781 internalFormat,
782 desc.fWidth, desc.fHeight,
783 0, // border
784 imageSize,
785 data));
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000786 } else {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000787 GL_ALLOC_CALL(this->glInterface(),
788 TexImage2D(GR_GL_TEXTURE_2D,
789 0, // level
790 internalFormat,
791 desc.fWidth, desc.fHeight,
792 0, // border
793 externalFormat, externalType,
794 data));
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000795 }
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000796 }
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000797 GrGLenum error = CHECK_ALLOC_ERROR(this->glInterface());
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000798 if (error != GR_GL_NO_ERROR) {
799 succeeded = false;
800 } else {
801 // if we have data and we used TexStorage to create the texture, we
802 // now upload with TexSubImage.
803 if (NULL != data && useTexStorage) {
804 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D,
805 0, // level
806 left, top,
807 width, height,
808 externalFormat, externalType,
809 data));
810 }
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000811 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000812 } else {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000813 if (swFlipY || glFlipY) {
bsalomon@google.com6f379512011-11-16 20:36:03 +0000814 top = desc.fHeight - (top + height);
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000815 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000816 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D,
817 0, // level
818 left, top,
819 width, height,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000820 externalFormat, externalType, data));
821 }
822
823 if (restoreGLRowLength) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000824 GrAssert(this->glCaps().unpackRowLengthSupport());
bsalomon@google.com6f379512011-11-16 20:36:03 +0000825 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000826 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000827 if (glFlipY) {
828 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
829 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000830 return succeeded;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000831}
832
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000833namespace {
robertphillips@google.com6177e692013-02-28 20:16:25 +0000834bool renderbuffer_storage_msaa(GrGLContext& ctx,
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000835 int sampleCount,
836 GrGLenum format,
837 int width, int height) {
robertphillips@google.com6177e692013-02-28 20:16:25 +0000838 CLEAR_ERROR_BEFORE_ALLOC(ctx.interface());
839 GrAssert(GrGLCaps::kNone_MSFBOType != ctx.info().caps().msFBOType());
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000840 bool created = false;
841 if (GrGLCaps::kNVDesktop_CoverageAAType ==
robertphillips@google.com6177e692013-02-28 20:16:25 +0000842 ctx.info().caps().coverageAAType()) {
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000843 const GrGLCaps::MSAACoverageMode& mode =
robertphillips@google.com6177e692013-02-28 20:16:25 +0000844 ctx.info().caps().getMSAACoverageMode(sampleCount);
845 GL_ALLOC_CALL(ctx.interface(),
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000846 RenderbufferStorageMultisampleCoverage(GR_GL_RENDERBUFFER,
847 mode.fCoverageSampleCnt,
848 mode.fColorSampleCnt,
849 format,
850 width, height));
robertphillips@google.com6177e692013-02-28 20:16:25 +0000851 created = (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctx.interface()));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000852 }
853 if (!created) {
robertphillips@google.com6177e692013-02-28 20:16:25 +0000854 GL_ALLOC_CALL(ctx.interface(),
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000855 RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
856 sampleCount,
857 format,
858 width, height));
robertphillips@google.com6177e692013-02-28 20:16:25 +0000859 created = (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctx.interface()));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000860 }
861 return created;
862}
863}
864
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000865bool GrGpuGL::createRenderTargetObjects(int width, int height,
866 GrGLuint texID,
867 GrGLRenderTarget::Desc* desc) {
868 desc->fMSColorRenderbufferID = 0;
869 desc->fRTFBOID = 0;
870 desc->fTexFBOID = 0;
bsalomon@google.com72830222013-01-23 20:25:22 +0000871 desc->fIsWrapped = false;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000872
873 GrGLenum status;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000874
bsalomon@google.comab15d612011-08-09 12:57:56 +0000875 GrGLenum msColorFormat = 0; // suppress warning
876
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000877 GL_CALL(GenFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000878 if (!desc->fTexFBOID) {
879 goto FAILED;
880 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000881
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000882
bsalomon@google.comf3a60c02013-03-19 19:06:09 +0000883 // If we are using multisampling we will create two FBOS. We render to one and then resolve to
884 // the texture bound to the other. The exception is the IMG multisample extension. With this
885 // extension the texture is multisampled when rendered to and then auto-resolves it when it is
886 // rendered from.
887 if (desc->fSampleCnt > 0 && GrGLCaps::kImaginationES_MSFBOType != this->glCaps().msFBOType()) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000888 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000889 goto FAILED;
890 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000891 GL_CALL(GenFramebuffers(1, &desc->fRTFBOID));
892 GL_CALL(GenRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000893 if (!desc->fRTFBOID ||
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000894 !desc->fMSColorRenderbufferID ||
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000895 !this->configToGLFormats(desc->fConfig,
896 // GLES requires sized internal formats
897 kES2_GrGLBinding == this->glBinding(),
898 &msColorFormat, NULL, NULL)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000899 goto FAILED;
900 }
901 } else {
902 desc->fRTFBOID = desc->fTexFBOID;
903 }
904
bsalomon@google.com0e9b41a2012-01-04 22:11:43 +0000905 // below here we may bind the FBO
bsalomon@google.comc811ea32012-05-21 15:33:09 +0000906 fHWBoundRenderTarget = NULL;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000907 if (desc->fRTFBOID != desc->fTexFBOID) {
bsalomon@google.com13ac3a32013-01-09 23:29:39 +0000908 GrAssert(desc->fSampleCnt > 0);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000909 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000910 desc->fMSColorRenderbufferID));
robertphillips@google.com6177e692013-02-28 20:16:25 +0000911 if (!renderbuffer_storage_msaa(fGLContext,
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000912 desc->fSampleCnt,
913 msColorFormat,
914 width, height)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000915 goto FAILED;
916 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000917 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fRTFBOID));
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000918 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000919 GR_GL_COLOR_ATTACHMENT0,
920 GR_GL_RENDERBUFFER,
921 desc->fMSColorRenderbufferID));
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000922 if (!this->glCaps().isConfigVerifiedColorAttachment(desc->fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000923 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
924 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
925 goto FAILED;
926 }
bsalomon@google.comf3a60c02013-03-19 19:06:09 +0000927 fGLContext.info().caps().markConfigAsValidColorAttachment(desc->fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000928 }
929 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000930 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000931
bsalomon@google.comf3a60c02013-03-19 19:06:09 +0000932 if (GrGLCaps::kImaginationES_MSFBOType == this->glCaps().msFBOType() && desc->fSampleCnt > 0) {
933 GL_CALL(FramebufferTexture2DMultisample(GR_GL_FRAMEBUFFER,
934 GR_GL_COLOR_ATTACHMENT0,
935 GR_GL_TEXTURE_2D,
936 texID, 0, desc->fSampleCnt));
937 } else {
938 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
939 GR_GL_COLOR_ATTACHMENT0,
940 GR_GL_TEXTURE_2D,
941 texID, 0));
942 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000943 if (!this->glCaps().isConfigVerifiedColorAttachment(desc->fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000944 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
945 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
946 goto FAILED;
947 }
robertphillips@google.com6177e692013-02-28 20:16:25 +0000948 fGLContext.info().caps().markConfigAsValidColorAttachment(desc->fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000949 }
950
951 return true;
952
953FAILED:
954 if (desc->fMSColorRenderbufferID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000955 GL_CALL(DeleteRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000956 }
957 if (desc->fRTFBOID != desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000958 GL_CALL(DeleteFramebuffers(1, &desc->fRTFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000959 }
960 if (desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000961 GL_CALL(DeleteFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000962 }
963 return false;
964}
965
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000966// good to set a break-point here to know when createTexture fails
967static GrTexture* return_null_texture() {
968// GrAssert(!"null texture");
969 return NULL;
970}
971
caryclark@google.comcf6285b2012-06-06 12:09:01 +0000972#if 0 && GR_DEBUG
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000973static size_t as_size_t(int x) {
974 return x;
975}
976#endif
977
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000978GrTexture* GrGpuGL::onCreateTexture(const GrTextureDesc& desc,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000979 const void* srcData,
980 size_t rowBytes) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000981
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000982 GrGLTexture::Desc glTexDesc;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000983 GrGLRenderTarget::Desc glRTDesc;
reed@google.comac10a2d2010-12-22 21:39:39 +0000984
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000985 // Attempt to catch un- or wrongly initialized sample counts;
986 GrAssert(desc.fSampleCnt >= 0 && desc.fSampleCnt <= 64);
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000987 // We fail if the MSAA was requested and is not available.
988 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType() && desc.fSampleCnt) {
989 //GrPrintf("MSAA RT requested but not supported on this platform.");
990 return return_null_texture();
991 }
992 // If the sample count exceeds the max then we clamp it.
993 glTexDesc.fSampleCnt = GrMin(desc.fSampleCnt, this->getCaps().maxSampleCount());
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000994
robertphillips@google.com32716282012-06-04 12:48:45 +0000995 glTexDesc.fFlags = desc.fFlags;
bsalomon@google.com99621082011-11-15 16:47:16 +0000996 glTexDesc.fWidth = desc.fWidth;
997 glTexDesc.fHeight = desc.fHeight;
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000998 glTexDesc.fConfig = desc.fConfig;
bsalomon@google.com72830222013-01-23 20:25:22 +0000999 glTexDesc.fIsWrapped = false;
reed@google.comac10a2d2010-12-22 21:39:39 +00001000
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001001 glRTDesc.fMSColorRenderbufferID = 0;
1002 glRTDesc.fRTFBOID = 0;
1003 glRTDesc.fTexFBOID = 0;
bsalomon@google.com72830222013-01-23 20:25:22 +00001004 glRTDesc.fIsWrapped = false;
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001005 glRTDesc.fConfig = glTexDesc.fConfig;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001006
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001007 bool renderTarget = 0 != (desc.fFlags & kRenderTarget_GrTextureFlagBit);
reed@google.comac10a2d2010-12-22 21:39:39 +00001008
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001009 const Caps& caps = this->getCaps();
1010
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001011 glTexDesc.fOrigin = resolve_origin(desc.fOrigin, renderTarget);
1012 glRTDesc.fOrigin = glTexDesc.fOrigin;
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001013
bsalomon@google.com8a70eef2013-03-19 13:58:55 +00001014 glRTDesc.fSampleCnt = glTexDesc.fSampleCnt;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001015 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType() &&
bsalomon@google.com78d6cf92012-01-30 18:09:31 +00001016 desc.fSampleCnt) {
bsalomon@google.com945bbe12012-06-15 14:30:34 +00001017 //GrPrintf("MSAA RT requested but not supported on this platform.");
1018 return return_null_texture();
reed@google.comac10a2d2010-12-22 21:39:39 +00001019 }
1020
reed@google.comac10a2d2010-12-22 21:39:39 +00001021 if (renderTarget) {
bsalomon@google.comf6601872012-08-28 21:11:35 +00001022 if (glTexDesc.fWidth > caps.maxRenderTargetSize() ||
1023 glTexDesc.fHeight > caps.maxRenderTargetSize()) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001024 return return_null_texture();
1025 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001026 }
1027
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001028 GL_CALL(GenTextures(1, &glTexDesc.fTextureID));
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001029 if (renderTarget && this->glCaps().textureUsageSupport()) {
bsalomon@google.com07dd2bf2011-12-09 19:40:36 +00001030 // provides a hint about how this texture will be used
1031 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1032 GR_GL_TEXTURE_USAGE,
1033 GR_GL_FRAMEBUFFER_ATTACHMENT));
1034 }
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001035 if (!glTexDesc.fTextureID) {
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001036 return return_null_texture();
1037 }
1038
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001039 this->setSpareTextureUnit();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001040 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTexDesc.fTextureID));
bsalomon@google.come269f212011-11-07 13:29:52 +00001041
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001042 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
1043 // drivers have a bug where an FBO won't be complete if it includes a
1044 // texture that is not mipmap complete (considering the filter in use).
1045 GrGLTexture::TexParams initialTexParams;
1046 // we only set a subset here so invalidate first
1047 initialTexParams.invalidate();
1048 initialTexParams.fFilter = GR_GL_NEAREST;
1049 initialTexParams.fWrapS = GR_GL_CLAMP_TO_EDGE;
1050 initialTexParams.fWrapT = GR_GL_CLAMP_TO_EDGE;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001051 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1052 GR_GL_TEXTURE_MAG_FILTER,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001053 initialTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001054 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1055 GR_GL_TEXTURE_MIN_FILTER,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001056 initialTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001057 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1058 GR_GL_TEXTURE_WRAP_S,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001059 initialTexParams.fWrapS));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001060 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1061 GR_GL_TEXTURE_WRAP_T,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001062 initialTexParams.fWrapT));
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001063 if (!this->uploadTexData(glTexDesc, true, 0, 0,
1064 glTexDesc.fWidth, glTexDesc.fHeight,
1065 desc.fConfig, srcData, rowBytes)) {
1066 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
1067 return return_null_texture();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001068 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001069
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001070 GrGLTexture* tex;
reed@google.comac10a2d2010-12-22 21:39:39 +00001071 if (renderTarget) {
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001072 // unbind the texture from the texture unit before binding it to the frame buffer
1073 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, 0));
1074
bsalomon@google.com99621082011-11-15 16:47:16 +00001075 if (!this->createRenderTargetObjects(glTexDesc.fWidth,
1076 glTexDesc.fHeight,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001077 glTexDesc.fTextureID,
1078 &glRTDesc)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001079 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001080 return return_null_texture();
1081 }
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001082 tex = SkNEW_ARGS(GrGLTexture, (this, glTexDesc, glRTDesc));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001083 } else {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001084 tex = SkNEW_ARGS(GrGLTexture, (this, glTexDesc));
reed@google.comac10a2d2010-12-22 21:39:39 +00001085 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001086 tex->setCachedTexParams(initialTexParams, this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00001087#ifdef TRACE_TEXTURE_CREATION
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001088 GrPrintf("--- new texture [%d] size=(%d %d) config=%d\n",
1089 glTexDesc.fTextureID, desc.fWidth, desc.fHeight, desc.fConfig);
reed@google.comac10a2d2010-12-22 21:39:39 +00001090#endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001091 return tex;
1092}
1093
1094namespace {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001095
1096const GrGLuint kUnknownBitCount = GrGLStencilBuffer::kUnknownBitCount;
1097
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001098void inline get_stencil_rb_sizes(const GrGLInterface* gl,
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001099 GrGLuint rb,
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001100 GrGLStencilBuffer::Format* format) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001101 // we shouldn't ever know one size and not the other
1102 GrAssert((kUnknownBitCount == format->fStencilBits) ==
1103 (kUnknownBitCount == format->fTotalBits));
1104 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001105 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001106 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1107 (GrGLint*)&format->fStencilBits);
1108 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001109 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001110 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1111 (GrGLint*)&format->fTotalBits);
1112 format->fTotalBits += format->fStencilBits;
1113 } else {
1114 format->fTotalBits = format->fStencilBits;
1115 }
1116 }
1117}
1118}
1119
1120bool GrGpuGL::createStencilBufferForRenderTarget(GrRenderTarget* rt,
1121 int width, int height) {
1122
1123 // All internally created RTs are also textures. We don't create
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +00001124 // 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 +00001125 GrAssert(rt->asTexture());
bsalomon@google.com99621082011-11-15 16:47:16 +00001126 GrAssert(width >= rt->width());
1127 GrAssert(height >= rt->height());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001128
1129 int samples = rt->numSamples();
1130 GrGLuint sbID;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001131 GL_CALL(GenRenderbuffers(1, &sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001132 if (!sbID) {
1133 return false;
1134 }
1135
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001136 int stencilFmtCnt = this->glCaps().stencilFormats().count();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001137 for (int i = 0; i < stencilFmtCnt; ++i) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001138 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001139 // we start with the last stencil format that succeeded in hopes
1140 // that we won't go through this loop more than once after the
1141 // first (painful) stencil creation.
1142 int sIdx = (i + fLastSuccessfulStencilFmtIdx) % stencilFmtCnt;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001143 const GrGLCaps::StencilFormat& sFmt =
1144 this->glCaps().stencilFormats()[sIdx];
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001145 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001146 // we do this "if" so that we don't call the multisample
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001147 // version on a GL that doesn't have an MSAA extension.
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001148 bool created;
1149 if (samples > 0) {
robertphillips@google.com6177e692013-02-28 20:16:25 +00001150 created = renderbuffer_storage_msaa(fGLContext,
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001151 samples,
1152 sFmt.fInternalFormat,
1153 width, height);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001154 } else {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001155 GL_ALLOC_CALL(this->glInterface(),
1156 RenderbufferStorage(GR_GL_RENDERBUFFER,
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001157 sFmt.fInternalFormat,
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001158 width, height));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001159 created =
1160 (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(this->glInterface()));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001161 }
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001162 if (created) {
1163 // After sized formats we attempt an unsized format and take
1164 // whatever sizes GL gives us. In that case we query for the size.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001165 GrGLStencilBuffer::Format format = sFmt;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001166 get_stencil_rb_sizes(this->glInterface(), sbID, &format);
bsalomon@google.com72830222013-01-23 20:25:22 +00001167 static const bool kIsWrapped = false;
robertphillips@google.comf2e93fc2012-09-05 19:44:18 +00001168 SkAutoTUnref<GrStencilBuffer> sb(SkNEW_ARGS(GrGLStencilBuffer,
bsalomon@google.com72830222013-01-23 20:25:22 +00001169 (this, kIsWrapped, sbID, width, height,
robertphillips@google.comf2e93fc2012-09-05 19:44:18 +00001170 samples, format)));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001171 if (this->attachStencilBufferToRenderTarget(sb, rt)) {
1172 fLastSuccessfulStencilFmtIdx = sIdx;
robertphillips@google.com9fbcad02012-09-09 14:44:15 +00001173 sb->transferToCache();
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001174 rt->setStencilBuffer(sb);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001175 return true;
1176 }
1177 sb->abandon(); // otherwise we lose sbID
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001178 }
1179 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001180 GL_CALL(DeleteRenderbuffers(1, &sbID));
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001181 return false;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001182}
1183
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001184bool GrGpuGL::attachStencilBufferToRenderTarget(GrStencilBuffer* sb, GrRenderTarget* rt) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001185 GrGLRenderTarget* glrt = (GrGLRenderTarget*) rt;
1186
1187 GrGLuint fbo = glrt->renderFBOID();
1188
1189 if (NULL == sb) {
1190 if (NULL != rt->getStencilBuffer()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001191 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001192 GR_GL_STENCIL_ATTACHMENT,
1193 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001194 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001195 GR_GL_DEPTH_ATTACHMENT,
1196 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001197#if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001198 GrGLenum status;
1199 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001200 GrAssert(GR_GL_FRAMEBUFFER_COMPLETE == status);
1201#endif
1202 }
1203 return true;
1204 } else {
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001205 GrGLStencilBuffer* glsb = static_cast<GrGLStencilBuffer*>(sb);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001206 GrGLuint rb = glsb->renderbufferID();
1207
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001208 fHWBoundRenderTarget = NULL;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001209 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, fbo));
1210 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001211 GR_GL_STENCIL_ATTACHMENT,
1212 GR_GL_RENDERBUFFER, rb));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001213 if (glsb->format().fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001214 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001215 GR_GL_DEPTH_ATTACHMENT,
1216 GR_GL_RENDERBUFFER, rb));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001217 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001218 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001219 GR_GL_DEPTH_ATTACHMENT,
1220 GR_GL_RENDERBUFFER, 0));
reed@google.comac10a2d2010-12-22 21:39:39 +00001221 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001222
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001223 GrGLenum status;
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001224 if (!this->glCaps().isColorConfigAndStencilFormatVerified(rt->config(), glsb->format())) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001225 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1226 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001227 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001228 GR_GL_STENCIL_ATTACHMENT,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001229 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001230 if (glsb->format().fPacked) {
1231 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1232 GR_GL_DEPTH_ATTACHMENT,
1233 GR_GL_RENDERBUFFER, 0));
1234 }
1235 return false;
1236 } else {
robertphillips@google.com6177e692013-02-28 20:16:25 +00001237 fGLContext.info().caps().markColorConfigAndStencilFormatAsVerified(
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001238 rt->config(),
1239 glsb->format());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001240 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001241 }
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001242 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001243 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001244}
1245
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001246////////////////////////////////////////////////////////////////////////////////
1247
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001248GrVertexBuffer* GrGpuGL::onCreateVertexBuffer(uint32_t size, bool dynamic) {
bsalomon@google.come49ad452013-02-20 19:33:20 +00001249 GrGLVertexBuffer::Desc desc;
1250 desc.fDynamic = dynamic;
1251 desc.fSizeInBytes = size;
1252 desc.fIsWrapped = false;
1253
bsalomon@google.com96966a52013-02-21 16:34:21 +00001254 if (this->glCaps().useNonVBOVertexAndIndexDynamicData() && desc.fDynamic) {
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001255 desc.fID = 0;
bsalomon@google.come49ad452013-02-20 19:33:20 +00001256 GrGLVertexBuffer* vertexBuffer = SkNEW_ARGS(GrGLVertexBuffer, (this, desc));
reed@google.comac10a2d2010-12-22 21:39:39 +00001257 return vertexBuffer;
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001258 } else {
1259 GL_CALL(GenBuffers(1, &desc.fID));
1260 if (desc.fID) {
bsalomon@google.com6918d482013-03-07 19:09:11 +00001261 fHWGeometryState.setVertexBufferID(this, desc.fID);
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001262 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1263 // make sure driver can allocate memory for this buffer
1264 GL_ALLOC_CALL(this->glInterface(),
1265 BufferData(GR_GL_ARRAY_BUFFER,
1266 desc.fSizeInBytes,
1267 NULL, // data ptr
1268 desc.fDynamic ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW));
1269 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
1270 GL_CALL(DeleteBuffers(1, &desc.fID));
bsalomon@google.com6918d482013-03-07 19:09:11 +00001271 this->notifyVertexBufferDelete(desc.fID);
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001272 return NULL;
1273 }
1274 GrGLVertexBuffer* vertexBuffer = SkNEW_ARGS(GrGLVertexBuffer, (this, desc));
1275 return vertexBuffer;
1276 }
1277 return NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +00001278 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001279}
1280
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001281GrIndexBuffer* GrGpuGL::onCreateIndexBuffer(uint32_t size, bool dynamic) {
bsalomon@google.come49ad452013-02-20 19:33:20 +00001282 GrGLIndexBuffer::Desc desc;
1283 desc.fDynamic = dynamic;
1284 desc.fSizeInBytes = size;
1285 desc.fIsWrapped = false;
1286
bsalomon@google.com96966a52013-02-21 16:34:21 +00001287 if (this->glCaps().useNonVBOVertexAndIndexDynamicData() && desc.fDynamic) {
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001288 desc.fID = 0;
bsalomon@google.come49ad452013-02-20 19:33:20 +00001289 GrIndexBuffer* indexBuffer = SkNEW_ARGS(GrGLIndexBuffer, (this, desc));
reed@google.comac10a2d2010-12-22 21:39:39 +00001290 return indexBuffer;
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001291 } else {
1292 GL_CALL(GenBuffers(1, &desc.fID));
1293 if (desc.fID) {
bsalomon@google.com6918d482013-03-07 19:09:11 +00001294 fHWGeometryState.setIndexBufferIDOnDefaultVertexArray(this, desc.fID);
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001295 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1296 // make sure driver can allocate memory for this buffer
1297 GL_ALLOC_CALL(this->glInterface(),
1298 BufferData(GR_GL_ELEMENT_ARRAY_BUFFER,
1299 desc.fSizeInBytes,
1300 NULL, // data ptr
1301 desc.fDynamic ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW));
1302 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
1303 GL_CALL(DeleteBuffers(1, &desc.fID));
bsalomon@google.com6918d482013-03-07 19:09:11 +00001304 this->notifyIndexBufferDelete(desc.fID);
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001305 return NULL;
1306 }
1307 GrIndexBuffer* indexBuffer = SkNEW_ARGS(GrGLIndexBuffer, (this, desc));
1308 return indexBuffer;
1309 }
1310 return NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +00001311 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001312}
1313
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00001314GrPath* GrGpuGL::onCreatePath(const SkPath& inPath) {
bsalomon@google.comf6601872012-08-28 21:11:35 +00001315 GrAssert(fCaps.pathStencilingSupport());
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001316 return SkNEW_ARGS(GrGLPath, (this, inPath));
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00001317}
1318
bsalomon@google.coma3201942012-06-21 19:58:20 +00001319void GrGpuGL::flushScissor() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001320 const GrDrawState& drawState = this->getDrawState();
1321 const GrGLRenderTarget* rt =
1322 static_cast<const GrGLRenderTarget*>(drawState.getRenderTarget());
1323
1324 GrAssert(NULL != rt);
1325 const GrGLIRect& vp = rt->getViewport();
reed@google.comac10a2d2010-12-22 21:39:39 +00001326
bsalomon@google.coma3201942012-06-21 19:58:20 +00001327 if (fScissorState.fEnabled) {
1328 GrGLIRect scissor;
1329 scissor.setRelativeTo(vp,
1330 fScissorState.fRect.fLeft,
1331 fScissorState.fRect.fTop,
1332 fScissorState.fRect.width(),
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001333 fScissorState.fRect.height(),
1334 rt->origin());
bsalomon@google.coma3201942012-06-21 19:58:20 +00001335 // if the scissor fully contains the viewport then we fall through and
1336 // disable the scissor test.
1337 if (!scissor.contains(vp)) {
1338 if (fHWScissorSettings.fRect != scissor) {
1339 scissor.pushToGLScissor(this->glInterface());
1340 fHWScissorSettings.fRect = scissor;
1341 }
1342 if (kYes_TriState != fHWScissorSettings.fEnabled) {
1343 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1344 fHWScissorSettings.fEnabled = kYes_TriState;
1345 }
1346 return;
1347 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001348 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001349 if (kNo_TriState != fHWScissorSettings.fEnabled) {
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001350 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.coma3201942012-06-21 19:58:20 +00001351 fHWScissorSettings.fEnabled = kNo_TriState;
1352 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001353 }
1354}
1355
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001356void GrGpuGL::onClear(const GrIRect* rect, GrColor color) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001357 const GrDrawState& drawState = this->getDrawState();
1358 const GrRenderTarget* rt = drawState.getRenderTarget();
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001359 // parent class should never let us get here with no RT
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001360 GrAssert(NULL != rt);
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001361
bsalomon@google.com74b98712011-11-11 19:46:16 +00001362 GrIRect clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001363 if (NULL != rect) {
1364 // flushScissor expects rect to be clipped to the target.
bsalomon@google.com74b98712011-11-11 19:46:16 +00001365 clippedRect = *rect;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001366 GrIRect rtRect = SkIRect::MakeWH(rt->width(), rt->height());
bsalomon@google.com74b98712011-11-11 19:46:16 +00001367 if (clippedRect.intersect(rtRect)) {
1368 rect = &clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001369 } else {
1370 return;
1371 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001372 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001373 this->flushRenderTarget(rect);
bsalomon@google.coma3201942012-06-21 19:58:20 +00001374 GrAutoTRestore<ScissorState> asr(&fScissorState);
1375 fScissorState.fEnabled = (NULL != rect);
1376 if (fScissorState.fEnabled) {
1377 fScissorState.fRect = *rect;
1378 }
1379 this->flushScissor();
bsalomon@google.com74b98712011-11-11 19:46:16 +00001380
1381 GrGLfloat r, g, b, a;
1382 static const GrGLfloat scale255 = 1.f / 255.f;
1383 a = GrColorUnpackA(color) * scale255;
1384 GrGLfloat scaleRGB = scale255;
bsalomon@google.com74b98712011-11-11 19:46:16 +00001385 r = GrColorUnpackR(color) * scaleRGB;
1386 g = GrColorUnpackG(color) * scaleRGB;
1387 b = GrColorUnpackB(color) * scaleRGB;
1388
1389 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00001390 fHWWriteToColor = kYes_TriState;
bsalomon@google.com74b98712011-11-11 19:46:16 +00001391 GL_CALL(ClearColor(r, g, b, a));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001392 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001393}
1394
bsalomon@google.comedc177d2011-08-05 15:46:40 +00001395void GrGpuGL::clearStencil() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001396 if (NULL == this->getDrawState().getRenderTarget()) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001397 return;
1398 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001399
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001400 this->flushRenderTarget(&GrIRect::EmptyIRect());
1401
bsalomon@google.coma3201942012-06-21 19:58:20 +00001402 GrAutoTRestore<ScissorState> asr(&fScissorState);
1403 fScissorState.fEnabled = false;
1404 this->flushScissor();
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001405
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001406 GL_CALL(StencilMask(0xffffffff));
1407 GL_CALL(ClearStencil(0));
1408 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001409 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001410}
1411
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001412void GrGpuGL::clearStencilClip(const GrIRect& rect, bool insideClip) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001413 const GrDrawState& drawState = this->getDrawState();
1414 const GrRenderTarget* rt = drawState.getRenderTarget();
1415 GrAssert(NULL != rt);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001416
1417 // this should only be called internally when we know we have a
1418 // stencil buffer.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001419 GrAssert(NULL != rt->getStencilBuffer());
1420 GrGLint stencilBitCount = rt->getStencilBuffer()->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001421#if 0
reed@google.comac10a2d2010-12-22 21:39:39 +00001422 GrAssert(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00001423 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001424#else
1425 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001426 // ANGLE a partial stencil mask will cause clears to be
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001427 // turned into draws. Our contract on GrDrawTarget says that
1428 // changing the clip between stencil passes may or may not
1429 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00001430 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001431#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001432 GrGLint value;
1433 if (insideClip) {
1434 value = (1 << (stencilBitCount - 1));
1435 } else {
1436 value = 0;
1437 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001438 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.coma3201942012-06-21 19:58:20 +00001439
1440 GrAutoTRestore<ScissorState> asr(&fScissorState);
1441 fScissorState.fEnabled = true;
1442 fScissorState.fRect = rect;
1443 this->flushScissor();
1444
caryclark@google.comcf6285b2012-06-06 12:09:01 +00001445 GL_CALL(StencilMask((uint32_t) clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001446 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001447 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001448 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001449}
1450
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001451void GrGpuGL::onForceRenderTargetFlush() {
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001452 this->flushRenderTarget(&GrIRect::EmptyIRect());
reed@google.comac10a2d2010-12-22 21:39:39 +00001453}
1454
bsalomon@google.comc4364992011-11-07 15:54:49 +00001455bool GrGpuGL::readPixelsWillPayForYFlip(GrRenderTarget* renderTarget,
1456 int left, int top,
1457 int width, int height,
1458 GrPixelConfig config,
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001459 size_t rowBytes) const {
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001460 // If this rendertarget is aready TopLeft, we don't need to flip.
1461 if (kTopLeft_GrSurfaceOrigin == renderTarget->origin()) {
1462 return false;
1463 }
1464
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001465 // if GL can do the flip then we'll never pay for it.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001466 if (this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001467 return false;
1468 }
1469
1470 // If we have to do memcpy to handle non-trim rowBytes then we
bsalomon@google.com7107fa72011-11-10 14:54:14 +00001471 // get the flip for free. Otherwise it costs.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001472 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001473 return true;
1474 }
1475 // If we have to do memcpys to handle rowBytes then y-flip is free
1476 // Note the rowBytes might be tight to the passed in data, but if data
1477 // gets clipped in x to the target the rowBytes will no longer be tight.
1478 if (left >= 0 && (left + width) < renderTarget->width()) {
1479 return 0 == rowBytes ||
1480 GrBytesPerPixel(config) * width == rowBytes;
1481 } else {
1482 return false;
1483 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001484}
1485
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001486bool GrGpuGL::onReadPixels(GrRenderTarget* target,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001487 int left, int top,
1488 int width, int height,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001489 GrPixelConfig config,
1490 void* buffer,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001491 size_t rowBytes) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001492 GrGLenum format;
1493 GrGLenum type;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001494 bool flipY = kBottomLeft_GrSurfaceOrigin == target->origin();
bsalomon@google.com280e99f2012-01-05 16:17:38 +00001495 if (!this->configToGLFormats(config, false, NULL, &format, &type)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001496 return false;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001497 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001498 size_t bpp = GrBytesPerPixel(config);
1499 if (!adjust_pixel_ops_params(target->width(), target->height(), bpp,
1500 &left, &top, &width, &height,
1501 const_cast<const void**>(&buffer),
1502 &rowBytes)) {
1503 return false;
1504 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001505
bsalomon@google.comc6980972011-11-02 19:57:21 +00001506 // resolve the render target if necessary
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001507 GrGLRenderTarget* tgt = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001508 GrDrawState::AutoRenderTargetRestore artr;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001509 switch (tgt->getResolveType()) {
1510 case GrGLRenderTarget::kCantResolve_ResolveType:
1511 return false;
1512 case GrGLRenderTarget::kAutoResolves_ResolveType:
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001513 artr.set(this->drawState(), target);
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001514 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001515 break;
1516 case GrGLRenderTarget::kCanResolve_ResolveType:
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001517 this->onResolveRenderTarget(tgt);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001518 // we don't track the state of the READ FBO ID.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001519 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1520 tgt->textureFBOID()));
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001521 break;
1522 default:
1523 GrCrash("Unknown resolve type");
reed@google.comac10a2d2010-12-22 21:39:39 +00001524 }
1525
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001526 const GrGLIRect& glvp = tgt->getViewport();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001527
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001528 // the read rect is viewport-relative
1529 GrGLIRect readRect;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001530 readRect.setRelativeTo(glvp, left, top, width, height, target->origin());
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001531
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001532 size_t tightRowBytes = bpp * width;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001533 if (0 == rowBytes) {
1534 rowBytes = tightRowBytes;
1535 }
1536 size_t readDstRowBytes = tightRowBytes;
1537 void* readDst = buffer;
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001538
bsalomon@google.comc6980972011-11-02 19:57:21 +00001539 // determine if GL can read using the passed rowBytes or if we need
1540 // a scratch buffer.
1541 SkAutoSMalloc<32 * sizeof(GrColor)> scratch;
1542 if (rowBytes != tightRowBytes) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001543 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.comc6980972011-11-02 19:57:21 +00001544 GrAssert(!(rowBytes % sizeof(GrColor)));
1545 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, rowBytes / sizeof(GrColor)));
1546 readDstRowBytes = rowBytes;
1547 } else {
1548 scratch.reset(tightRowBytes * height);
1549 readDst = scratch.get();
1550 }
1551 }
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001552 if (flipY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001553 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 1));
1554 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001555 GL_CALL(ReadPixels(readRect.fLeft, readRect.fBottom,
1556 readRect.fWidth, readRect.fHeight,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001557 format, type, readDst));
1558 if (readDstRowBytes != tightRowBytes) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001559 GrAssert(this->glCaps().packRowLengthSupport());
bsalomon@google.comc6980972011-11-02 19:57:21 +00001560 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
1561 }
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001562 if (flipY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001563 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 0));
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001564 flipY = false;
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001565 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001566
1567 // now reverse the order of the rows, since GL's are bottom-to-top, but our
bsalomon@google.comc6980972011-11-02 19:57:21 +00001568 // API presents top-to-bottom. We must preserve the padding contents. Note
1569 // that the above readPixels did not overwrite the padding.
1570 if (readDst == buffer) {
1571 GrAssert(rowBytes == readDstRowBytes);
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001572 if (flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001573 scratch.reset(tightRowBytes);
1574 void* tmpRow = scratch.get();
1575 // flip y in-place by rows
1576 const int halfY = height >> 1;
1577 char* top = reinterpret_cast<char*>(buffer);
1578 char* bottom = top + (height - 1) * rowBytes;
1579 for (int y = 0; y < halfY; y++) {
1580 memcpy(tmpRow, top, tightRowBytes);
1581 memcpy(top, bottom, tightRowBytes);
1582 memcpy(bottom, tmpRow, tightRowBytes);
1583 top += rowBytes;
1584 bottom -= rowBytes;
1585 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001586 }
1587 } else {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001588 GrAssert(readDst != buffer); GrAssert(rowBytes != tightRowBytes);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001589 // copy from readDst to buffer while flipping y
caryclark@google.comcf6285b2012-06-06 12:09:01 +00001590 // const int halfY = height >> 1;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001591 const char* src = reinterpret_cast<const char*>(readDst);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001592 char* dst = reinterpret_cast<char*>(buffer);
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001593 if (flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001594 dst += (height-1) * rowBytes;
1595 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001596 for (int y = 0; y < height; y++) {
1597 memcpy(dst, src, tightRowBytes);
1598 src += readDstRowBytes;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001599 if (!flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001600 dst += rowBytes;
1601 } else {
1602 dst -= rowBytes;
1603 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001604 }
1605 }
1606 return true;
1607}
1608
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001609void GrGpuGL::flushRenderTarget(const GrIRect* bound) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001610
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001611 GrGLRenderTarget* rt =
1612 static_cast<GrGLRenderTarget*>(this->drawState()->getRenderTarget());
1613 GrAssert(NULL != rt);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001614
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001615 if (fHWBoundRenderTarget != rt) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001616 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, rt->renderFBOID()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001617 #if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001618 GrGLenum status;
1619 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001620 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
reed@google.comb9255d52011-06-13 18:54:59 +00001621 GrPrintf("GrGpuGL::flushRenderTarget glCheckFramebufferStatus %x\n", status);
reed@google.comac10a2d2010-12-22 21:39:39 +00001622 }
1623 #endif
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001624 fHWBoundRenderTarget = rt;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001625 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.coma3201942012-06-21 19:58:20 +00001626 if (fHWViewport != vp) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001627 vp.pushToGLViewport(this->glInterface());
bsalomon@google.coma3201942012-06-21 19:58:20 +00001628 fHWViewport = vp;
reed@google.comac10a2d2010-12-22 21:39:39 +00001629 }
1630 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001631 if (NULL == bound || !bound->isEmpty()) {
1632 rt->flagAsNeedingResolve(bound);
1633 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001634}
1635
twiz@google.com0f31ca72011-03-18 17:38:11 +00001636GrGLenum gPrimitiveType2GLMode[] = {
1637 GR_GL_TRIANGLES,
1638 GR_GL_TRIANGLE_STRIP,
1639 GR_GL_TRIANGLE_FAN,
1640 GR_GL_POINTS,
1641 GR_GL_LINES,
1642 GR_GL_LINE_STRIP
reed@google.comac10a2d2010-12-22 21:39:39 +00001643};
1644
bsalomon@google.comd302f142011-03-03 13:54:13 +00001645#define SWAP_PER_DRAW 0
1646
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001647#if SWAP_PER_DRAW
bsalomon@google.comd302f142011-03-03 13:54:13 +00001648 #if GR_MAC_BUILD
1649 #include <AGL/agl.h>
1650 #elif GR_WIN32_BUILD
bsalomon@google.comce7357d2012-06-25 17:49:25 +00001651 #include <gl/GL.h>
bsalomon@google.comd302f142011-03-03 13:54:13 +00001652 void SwapBuf() {
1653 DWORD procID = GetCurrentProcessId();
1654 HWND hwnd = GetTopWindow(GetDesktopWindow());
1655 while(hwnd) {
1656 DWORD wndProcID = 0;
1657 GetWindowThreadProcessId(hwnd, &wndProcID);
1658 if(wndProcID == procID) {
1659 SwapBuffers(GetDC(hwnd));
1660 }
1661 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
1662 }
1663 }
1664 #endif
1665#endif
1666
bsalomon@google.com74749cd2013-01-30 16:12:41 +00001667void GrGpuGL::onGpuDraw(const DrawInfo& info) {
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00001668 size_t indexOffsetInBytes;
1669 this->setupGeometry(info, &indexOffsetInBytes);
reed@google.comac10a2d2010-12-22 21:39:39 +00001670
bsalomon@google.com74749cd2013-01-30 16:12:41 +00001671 GrAssert((size_t)info.primitiveType() < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001672
bsalomon@google.com74749cd2013-01-30 16:12:41 +00001673 if (info.isIndexed()) {
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00001674 GrGLvoid* indices =
1675 reinterpret_cast<GrGLvoid*>(indexOffsetInBytes + sizeof(uint16_t) * info.startIndex());
bsalomon@google.com74749cd2013-01-30 16:12:41 +00001676 // info.startVertex() was accounted for by setupGeometry.
1677 GL_CALL(DrawElements(gPrimitiveType2GLMode[info.primitiveType()],
1678 info.indexCount(),
1679 GR_GL_UNSIGNED_SHORT,
1680 indices));
1681 } else {
1682 // Pass 0 for parameter first. We have to adjust glVertexAttribPointer() to account for
1683 // startVertex in the DrawElements case. So we always rely on setupGeometry to have
1684 // accounted for startVertex.
1685 GL_CALL(DrawArrays(gPrimitiveType2GLMode[info.primitiveType()], 0, info.vertexCount()));
1686 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00001687#if SWAP_PER_DRAW
1688 glFlush();
1689 #if GR_MAC_BUILD
1690 aglSwapBuffers(aglGetCurrentContext());
1691 int set_a_break_pt_here = 9;
1692 aglSwapBuffers(aglGetCurrentContext());
1693 #elif GR_WIN32_BUILD
1694 SwapBuf();
1695 int set_a_break_pt_here = 9;
1696 SwapBuf();
1697 #endif
1698#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001699}
1700
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001701namespace {
bsalomon@google.com100abf42012-09-05 17:40:04 +00001702
1703static const uint16_t kOnes16 = static_cast<uint16_t>(~0);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001704const GrStencilSettings& winding_nv_path_stencil_settings() {
1705 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
1706 kIncClamp_StencilOp,
1707 kIncClamp_StencilOp,
1708 kAlwaysIfInClip_StencilFunc,
bsalomon@google.com100abf42012-09-05 17:40:04 +00001709 kOnes16, kOnes16, kOnes16);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001710 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
1711}
1712const GrStencilSettings& even_odd_nv_path_stencil_settings() {
1713 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
1714 kInvert_StencilOp,
1715 kInvert_StencilOp,
1716 kAlwaysIfInClip_StencilFunc,
bsalomon@google.com100abf42012-09-05 17:40:04 +00001717 kOnes16, kOnes16, kOnes16);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001718 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
1719}
1720}
1721
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001722void GrGpuGL::setStencilPathSettings(const GrPath&,
sugoi@google.com12b4e272012-12-06 20:13:11 +00001723 SkPath::FillType fill,
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001724 GrStencilSettings* settings) {
1725 switch (fill) {
sugoi@google.com12b4e272012-12-06 20:13:11 +00001726 case SkPath::kEvenOdd_FillType:
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001727 *settings = even_odd_nv_path_stencil_settings();
1728 return;
sugoi@google.com12b4e272012-12-06 20:13:11 +00001729 case SkPath::kWinding_FillType:
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001730 *settings = winding_nv_path_stencil_settings();
1731 return;
1732 default:
1733 GrCrash("Unexpected path fill.");
1734 }
1735}
1736
sugoi@google.com12b4e272012-12-06 20:13:11 +00001737void GrGpuGL::onGpuStencilPath(const GrPath* path, SkPath::FillType fill) {
bsalomon@google.comf6601872012-08-28 21:11:35 +00001738 GrAssert(fCaps.pathStencilingSupport());
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001739
1740 GrGLuint id = static_cast<const GrGLPath*>(path)->pathID();
1741 GrDrawState* drawState = this->drawState();
1742 GrAssert(NULL != drawState->getRenderTarget());
1743 if (NULL == drawState->getRenderTarget()->getStencilBuffer()) {
1744 return;
1745 }
1746
1747 // Decide how to manipulate the stencil buffer based on the fill rule.
1748 // Also, assert that the stencil settings we set in setStencilPathSettings
1749 // are present.
1750 GrAssert(!fStencilSettings.isTwoSided());
1751 GrGLenum fillMode;
1752 switch (fill) {
sugoi@google.com12b4e272012-12-06 20:13:11 +00001753 case SkPath::kWinding_FillType:
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001754 fillMode = GR_GL_COUNT_UP;
1755 GrAssert(kIncClamp_StencilOp ==
1756 fStencilSettings.passOp(GrStencilSettings::kFront_Face));
1757 GrAssert(kIncClamp_StencilOp ==
1758 fStencilSettings.failOp(GrStencilSettings::kFront_Face));
1759 break;
sugoi@google.com12b4e272012-12-06 20:13:11 +00001760 case SkPath::kEvenOdd_FillType:
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001761 fillMode = GR_GL_INVERT;
1762 GrAssert(kInvert_StencilOp ==
1763 fStencilSettings.passOp(GrStencilSettings::kFront_Face));
1764 GrAssert(kInvert_StencilOp ==
1765 fStencilSettings.failOp(GrStencilSettings::kFront_Face));
1766 break;
1767 default:
1768 // Only the above two fill rules are allowed.
1769 GrCrash("Unexpected path fill.");
bsalomon@google.com548a4332012-07-11 19:45:22 +00001770 return; // suppress unused var warning.
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001771 }
1772 GrGLint writeMask = fStencilSettings.writeMask(GrStencilSettings::kFront_Face);
1773 GL_CALL(StencilFillPath(id, fillMode, writeMask));
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00001774}
1775
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001776void GrGpuGL::onResolveRenderTarget(GrRenderTarget* target) {
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001777 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001778 if (rt->needsResolve()) {
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001779 // The IMG extension automatically resolves the texture when it is read.
1780 if (GrGLCaps::kImaginationES_MSFBOType != this->glCaps().msFBOType()) {
1781 GrAssert(GrGLCaps::kNone_MSFBOType != this->glCaps().msFBOType());
1782 GrAssert(rt->textureFBOID() != rt->renderFBOID());
1783 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER, rt->renderFBOID()));
1784 GL_CALL(BindFramebuffer(GR_GL_DRAW_FRAMEBUFFER, rt->textureFBOID()));
1785 // make sure we go through flushRenderTarget() since we've modified
1786 // the bound DRAW FBO ID.
1787 fHWBoundRenderTarget = NULL;
1788 const GrGLIRect& vp = rt->getViewport();
1789 const GrIRect dirtyRect = rt->getResolveRect();
1790 GrGLIRect r;
1791 r.setRelativeTo(vp, dirtyRect.fLeft, dirtyRect.fTop,
1792 dirtyRect.width(), dirtyRect.height(), target->origin());
reed@google.comac10a2d2010-12-22 21:39:39 +00001793
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001794 GrAutoTRestore<ScissorState> asr;
1795 if (GrGLCaps::kAppleES_MSFBOType == this->glCaps().msFBOType()) {
1796 // Apple's extension uses the scissor as the blit bounds.
bsalomon@google.coma3201942012-06-21 19:58:20 +00001797 asr.reset(&fScissorState);
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001798 fScissorState.fEnabled = true;
1799 fScissorState.fRect = dirtyRect;
bsalomon@google.coma3201942012-06-21 19:58:20 +00001800 this->flushScissor();
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001801 GL_CALL(ResolveMultisampleFramebuffer());
1802 } else {
1803 if (GrGLCaps::kDesktopARB_MSFBOType != this->glCaps().msFBOType()) {
1804 // this respects the scissor during the blit, so disable it.
1805 GrAssert(GrGLCaps::kDesktopEXT_MSFBOType == this->glCaps().msFBOType());
1806 asr.reset(&fScissorState);
1807 fScissorState.fEnabled = false;
1808 this->flushScissor();
1809 }
1810 int right = r.fLeft + r.fWidth;
1811 int top = r.fBottom + r.fHeight;
1812 GL_CALL(BlitFramebuffer(r.fLeft, r.fBottom, right, top,
1813 r.fLeft, r.fBottom, right, top,
1814 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001815 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001816 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001817 rt->flagAsResolved();
reed@google.comac10a2d2010-12-22 21:39:39 +00001818 }
1819}
1820
bsalomon@google.com411dad02012-06-05 20:24:20 +00001821namespace {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001822
bsalomon@google.com411dad02012-06-05 20:24:20 +00001823GrGLenum gr_to_gl_stencil_func(GrStencilFunc basicFunc) {
1824 static const GrGLenum gTable[] = {
1825 GR_GL_ALWAYS, // kAlways_StencilFunc
1826 GR_GL_NEVER, // kNever_StencilFunc
1827 GR_GL_GREATER, // kGreater_StencilFunc
1828 GR_GL_GEQUAL, // kGEqual_StencilFunc
1829 GR_GL_LESS, // kLess_StencilFunc
1830 GR_GL_LEQUAL, // kLEqual_StencilFunc,
1831 GR_GL_EQUAL, // kEqual_StencilFunc,
1832 GR_GL_NOTEQUAL, // kNotEqual_StencilFunc,
1833 };
1834 GR_STATIC_ASSERT(GR_ARRAY_COUNT(gTable) == kBasicStencilFuncCount);
1835 GR_STATIC_ASSERT(0 == kAlways_StencilFunc);
1836 GR_STATIC_ASSERT(1 == kNever_StencilFunc);
1837 GR_STATIC_ASSERT(2 == kGreater_StencilFunc);
1838 GR_STATIC_ASSERT(3 == kGEqual_StencilFunc);
1839 GR_STATIC_ASSERT(4 == kLess_StencilFunc);
1840 GR_STATIC_ASSERT(5 == kLEqual_StencilFunc);
1841 GR_STATIC_ASSERT(6 == kEqual_StencilFunc);
1842 GR_STATIC_ASSERT(7 == kNotEqual_StencilFunc);
1843 GrAssert((unsigned) basicFunc < kBasicStencilFuncCount);
1844
1845 return gTable[basicFunc];
1846}
1847
1848GrGLenum gr_to_gl_stencil_op(GrStencilOp op) {
1849 static const GrGLenum gTable[] = {
1850 GR_GL_KEEP, // kKeep_StencilOp
1851 GR_GL_REPLACE, // kReplace_StencilOp
1852 GR_GL_INCR_WRAP, // kIncWrap_StencilOp
1853 GR_GL_INCR, // kIncClamp_StencilOp
1854 GR_GL_DECR_WRAP, // kDecWrap_StencilOp
1855 GR_GL_DECR, // kDecClamp_StencilOp
1856 GR_GL_ZERO, // kZero_StencilOp
1857 GR_GL_INVERT, // kInvert_StencilOp
1858 };
1859 GR_STATIC_ASSERT(GR_ARRAY_COUNT(gTable) == kStencilOpCount);
1860 GR_STATIC_ASSERT(0 == kKeep_StencilOp);
1861 GR_STATIC_ASSERT(1 == kReplace_StencilOp);
1862 GR_STATIC_ASSERT(2 == kIncWrap_StencilOp);
1863 GR_STATIC_ASSERT(3 == kIncClamp_StencilOp);
1864 GR_STATIC_ASSERT(4 == kDecWrap_StencilOp);
1865 GR_STATIC_ASSERT(5 == kDecClamp_StencilOp);
1866 GR_STATIC_ASSERT(6 == kZero_StencilOp);
1867 GR_STATIC_ASSERT(7 == kInvert_StencilOp);
1868 GrAssert((unsigned) op < kStencilOpCount);
1869 return gTable[op];
1870}
1871
1872void set_gl_stencil(const GrGLInterface* gl,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001873 const GrStencilSettings& settings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00001874 GrGLenum glFace,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001875 GrStencilSettings::Face grFace) {
1876 GrGLenum glFunc = gr_to_gl_stencil_func(settings.func(grFace));
1877 GrGLenum glFailOp = gr_to_gl_stencil_op(settings.failOp(grFace));
1878 GrGLenum glPassOp = gr_to_gl_stencil_op(settings.passOp(grFace));
1879
1880 GrGLint ref = settings.funcRef(grFace);
1881 GrGLint mask = settings.funcMask(grFace);
1882 GrGLint writeMask = settings.writeMask(grFace);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001883
1884 if (GR_GL_FRONT_AND_BACK == glFace) {
1885 // we call the combined func just in case separate stencil is not
1886 // supported.
1887 GR_GL_CALL(gl, StencilFunc(glFunc, ref, mask));
1888 GR_GL_CALL(gl, StencilMask(writeMask));
1889 GR_GL_CALL(gl, StencilOp(glFailOp, glPassOp, glPassOp));
1890 } else {
1891 GR_GL_CALL(gl, StencilFuncSeparate(glFace, glFunc, ref, mask));
1892 GR_GL_CALL(gl, StencilMaskSeparate(glFace, writeMask));
1893 GR_GL_CALL(gl, StencilOpSeparate(glFace, glFailOp, glPassOp, glPassOp));
1894 }
1895}
1896}
bsalomon@google.comd302f142011-03-03 13:54:13 +00001897
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001898void GrGpuGL::flushStencil(DrawType type) {
1899 if (kStencilPath_DrawType == type) {
1900 GrAssert(!fStencilSettings.isTwoSided());
1901 // Just the func, ref, and mask is set here. The op and write mask are params to the call
1902 // that draws the path to the SB (glStencilFillPath)
1903 GrGLenum func =
1904 gr_to_gl_stencil_func(fStencilSettings.func(GrStencilSettings::kFront_Face));
1905 GL_CALL(PathStencilFunc(func,
1906 fStencilSettings.funcRef(GrStencilSettings::kFront_Face),
1907 fStencilSettings.funcMask(GrStencilSettings::kFront_Face)));
1908 } else if (fHWStencilSettings != fStencilSettings) {
1909 if (fStencilSettings.isDisabled()) {
1910 if (kNo_TriState != fHWStencilTestEnabled) {
1911 GL_CALL(Disable(GR_GL_STENCIL_TEST));
1912 fHWStencilTestEnabled = kNo_TriState;
1913 }
1914 } else {
1915 if (kYes_TriState != fHWStencilTestEnabled) {
1916 GL_CALL(Enable(GR_GL_STENCIL_TEST));
1917 fHWStencilTestEnabled = kYes_TriState;
1918 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001919 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001920 if (!fStencilSettings.isDisabled()) {
bsalomon@google.comf6601872012-08-28 21:11:35 +00001921 if (this->getCaps().twoSidedStencilSupport()) {
bsalomon@google.com411dad02012-06-05 20:24:20 +00001922 set_gl_stencil(this->glInterface(),
bsalomon@google.coma3201942012-06-21 19:58:20 +00001923 fStencilSettings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00001924 GR_GL_FRONT,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001925 GrStencilSettings::kFront_Face);
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_BACK,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001929 GrStencilSettings::kBack_Face);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001930 } else {
bsalomon@google.com411dad02012-06-05 20:24:20 +00001931 set_gl_stencil(this->glInterface(),
bsalomon@google.coma3201942012-06-21 19:58:20 +00001932 fStencilSettings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00001933 GR_GL_FRONT_AND_BACK,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001934 GrStencilSettings::kFront_Face);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001935 }
1936 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001937 fHWStencilSettings = fStencilSettings;
reed@google.comac10a2d2010-12-22 21:39:39 +00001938 }
1939}
1940
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001941void GrGpuGL::flushAAState(DrawType type) {
bsalomon@google.com202d1392013-03-19 18:58:08 +00001942// At least some ATI linux drivers will render GL_LINES incorrectly when MSAA state is enabled but
1943// the target is not multisampled. Single pixel wide lines are rendered thicker than 1 pixel wide.
1944#if 0
1945 // Replace RT_HAS_MSAA with this definition once this driver bug is no longer a relevant concern
1946 #define RT_HAS_MSAA rt->isMultisampled()
1947#else
1948 #define RT_HAS_MSAA (rt->isMultisampled() || kDrawLines_DrawType == type)
1949#endif
1950
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001951 const GrRenderTarget* rt = this->getDrawState().getRenderTarget();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001952 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001953 // ES doesn't support toggling GL_MULTISAMPLE and doesn't have
1954 // smooth lines.
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001955 // we prefer smooth lines over multisampled lines
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00001956 bool smoothLines = false;
1957
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001958 if (kDrawLines_DrawType == type) {
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00001959 smoothLines = this->willUseHWAALines();
1960 if (smoothLines) {
1961 if (kYes_TriState != fHWAAState.fSmoothLineEnabled) {
1962 GL_CALL(Enable(GR_GL_LINE_SMOOTH));
1963 fHWAAState.fSmoothLineEnabled = kYes_TriState;
1964 // must disable msaa to use line smoothing
bsalomon@google.com202d1392013-03-19 18:58:08 +00001965 if (RT_HAS_MSAA &&
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00001966 kNo_TriState != fHWAAState.fMSAAEnabled) {
1967 GL_CALL(Disable(GR_GL_MULTISAMPLE));
1968 fHWAAState.fMSAAEnabled = kNo_TriState;
1969 }
1970 }
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001971 } else {
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00001972 if (kNo_TriState != fHWAAState.fSmoothLineEnabled) {
1973 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
1974 fHWAAState.fSmoothLineEnabled = kNo_TriState;
1975 }
1976 }
1977 }
bsalomon@google.com202d1392013-03-19 18:58:08 +00001978 if (!smoothLines && RT_HAS_MSAA) {
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001979 // FIXME: GL_NV_pr doesn't seem to like MSAA disabled. The paths
1980 // convex hulls of each segment appear to get filled.
1981 bool enableMSAA = kStencilPath_DrawType == type ||
1982 this->getDrawState().isHWAntialiasState();
1983 if (enableMSAA) {
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00001984 if (kYes_TriState != fHWAAState.fMSAAEnabled) {
1985 GL_CALL(Enable(GR_GL_MULTISAMPLE));
1986 fHWAAState.fMSAAEnabled = kYes_TriState;
1987 }
1988 } else {
1989 if (kNo_TriState != fHWAAState.fMSAAEnabled) {
1990 GL_CALL(Disable(GR_GL_MULTISAMPLE));
1991 fHWAAState.fMSAAEnabled = kNo_TriState;
1992 }
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001993 }
1994 }
1995 }
1996}
1997
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00001998void GrGpuGL::flushBlend(bool isLines,
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001999 GrBlendCoeff srcCoeff,
bsalomon@google.com271cffc2011-05-20 14:13:56 +00002000 GrBlendCoeff dstCoeff) {
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00002001 if (isLines && this->willUseHWAALines()) {
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002002 if (kYes_TriState != fHWBlendState.fEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002003 GL_CALL(Enable(GR_GL_BLEND));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002004 fHWBlendState.fEnabled = kYes_TriState;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002005 }
bsalomon@google.com47059542012-06-06 20:51:20 +00002006 if (kSA_GrBlendCoeff != fHWBlendState.fSrcCoeff ||
2007 kISA_GrBlendCoeff != fHWBlendState.fDstCoeff) {
2008 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[kSA_GrBlendCoeff],
2009 gXfermodeCoeff2Blend[kISA_GrBlendCoeff]));
2010 fHWBlendState.fSrcCoeff = kSA_GrBlendCoeff;
2011 fHWBlendState.fDstCoeff = kISA_GrBlendCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002012 }
2013 } else {
bsalomon@google.com86c1f712011-10-12 14:54:26 +00002014 // any optimization to disable blending should
2015 // have already been applied and tweaked the coeffs
2016 // to (1, 0).
bsalomon@google.com47059542012-06-06 20:51:20 +00002017 bool blendOff = kOne_GrBlendCoeff == srcCoeff &&
2018 kZero_GrBlendCoeff == dstCoeff;
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002019 if (blendOff) {
2020 if (kNo_TriState != fHWBlendState.fEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002021 GL_CALL(Disable(GR_GL_BLEND));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002022 fHWBlendState.fEnabled = kNo_TriState;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002023 }
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002024 } else {
2025 if (kYes_TriState != fHWBlendState.fEnabled) {
2026 GL_CALL(Enable(GR_GL_BLEND));
2027 fHWBlendState.fEnabled = kYes_TriState;
2028 }
2029 if (fHWBlendState.fSrcCoeff != srcCoeff ||
2030 fHWBlendState.fDstCoeff != dstCoeff) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002031 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
2032 gXfermodeCoeff2Blend[dstCoeff]));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002033 fHWBlendState.fSrcCoeff = srcCoeff;
2034 fHWBlendState.fDstCoeff = dstCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002035 }
bsalomon@google.coma5d056a2012-03-27 15:59:58 +00002036 GrColor blendConst = this->getDrawState().getBlendConstant();
bsalomon@google.com271cffc2011-05-20 14:13:56 +00002037 if ((BlendCoeffReferencesConstant(srcCoeff) ||
2038 BlendCoeffReferencesConstant(dstCoeff)) &&
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002039 (!fHWBlendState.fConstColorValid ||
2040 fHWBlendState.fConstColor != blendConst)) {
bsalomon@google.com75347472012-09-17 17:23:21 +00002041 GrGLfloat c[4];
2042 GrColorToRGBAFloat(blendConst, c);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002043 GL_CALL(BlendColor(c[0], c[1], c[2], c[3]));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002044 fHWBlendState.fConstColor = blendConst;
2045 fHWBlendState.fConstColorValid = true;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002046 }
2047 }
2048 }
2049}
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002050namespace {
2051
bsalomon@google.com6d003d12012-09-11 15:45:20 +00002052inline void set_tex_swizzle(GrGLenum swizzle[4], const GrGLInterface* gl) {
bsalomon@google.com4d063de2012-05-31 17:59:23 +00002053 GR_GL_CALL(gl, TexParameteriv(GR_GL_TEXTURE_2D,
2054 GR_GL_TEXTURE_SWIZZLE_RGBA,
2055 reinterpret_cast<const GrGLint*>(swizzle)));
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002056}
bsalomon@google.comb8670992012-07-25 21:27:09 +00002057
bsalomon@google.com6c76d242012-09-07 16:52:24 +00002058inline GrGLenum tile_to_gl_wrap(SkShader::TileMode tm) {
bsalomon@google.comb8670992012-07-25 21:27:09 +00002059 static const GrGLenum gWrapModes[] = {
2060 GR_GL_CLAMP_TO_EDGE,
2061 GR_GL_REPEAT,
2062 GR_GL_MIRRORED_REPEAT
2063 };
2064 GrAssert((unsigned) tm <= SK_ARRAY_COUNT(gWrapModes));
2065 GR_STATIC_ASSERT(0 == SkShader::kClamp_TileMode);
2066 GR_STATIC_ASSERT(1 == SkShader::kRepeat_TileMode);
2067 GR_STATIC_ASSERT(2 == SkShader::kMirror_TileMode);
2068 return gWrapModes[tm];
2069}
2070
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002071}
2072
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002073void GrGpuGL::bindTexture(int unitIdx, const GrTextureParams& params, GrGLTexture* texture) {
2074 GrAssert(NULL != texture);
tomhudson@google.comd0c1a062012-07-12 17:23:52 +00002075
bsalomon@google.comb8670992012-07-25 21:27:09 +00002076 // If we created a rt/tex and rendered to it without using a texture and now we're texturing
2077 // 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 +00002078 // out of the "last != next" check.
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002079 GrGLRenderTarget* texRT = static_cast<GrGLRenderTarget*>(texture->asRenderTarget());
bsalomon@google.com4c883782012-06-04 19:05:11 +00002080 if (NULL != texRT) {
2081 this->onResolveRenderTarget(texRT);
2082 }
2083
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002084 if (fHWBoundTextures[unitIdx] != texture) {
2085 this->setTextureUnit(unitIdx);
2086 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, texture->textureID()));
2087 fHWBoundTextures[unitIdx] = texture;
bsalomon@google.com4c883782012-06-04 19:05:11 +00002088 }
2089
bsalomon@google.com4c883782012-06-04 19:05:11 +00002090 ResetTimestamp timestamp;
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002091 const GrGLTexture::TexParams& oldTexParams = texture->getCachedTexParams(&timestamp);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002092 bool setAll = timestamp < this->getResetTimestamp();
2093 GrGLTexture::TexParams newTexParams;
2094
bsalomon@google.comb8670992012-07-25 21:27:09 +00002095 newTexParams.fFilter = params.isBilerp() ? GR_GL_LINEAR : GR_GL_NEAREST;
bsalomon@google.com4c883782012-06-04 19:05:11 +00002096
bsalomon@google.comb8670992012-07-25 21:27:09 +00002097 newTexParams.fWrapS = tile_to_gl_wrap(params.getTileModeX());
2098 newTexParams.fWrapT = tile_to_gl_wrap(params.getTileModeY());
bsalomon@google.com4c883782012-06-04 19:05:11 +00002099 memcpy(newTexParams.fSwizzleRGBA,
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002100 GrGLShaderBuilder::GetTexParamSwizzle(texture->config(), this->glCaps()),
bsalomon@google.com4c883782012-06-04 19:05:11 +00002101 sizeof(newTexParams.fSwizzleRGBA));
2102 if (setAll || newTexParams.fFilter != oldTexParams.fFilter) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002103 this->setTextureUnit(unitIdx);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002104 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2105 GR_GL_TEXTURE_MAG_FILTER,
2106 newTexParams.fFilter));
2107 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2108 GR_GL_TEXTURE_MIN_FILTER,
2109 newTexParams.fFilter));
2110 }
2111 if (setAll || newTexParams.fWrapS != oldTexParams.fWrapS) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002112 this->setTextureUnit(unitIdx);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002113 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2114 GR_GL_TEXTURE_WRAP_S,
2115 newTexParams.fWrapS));
2116 }
2117 if (setAll || newTexParams.fWrapT != oldTexParams.fWrapT) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002118 this->setTextureUnit(unitIdx);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002119 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2120 GR_GL_TEXTURE_WRAP_T,
2121 newTexParams.fWrapT));
2122 }
2123 if (this->glCaps().textureSwizzleSupport() &&
2124 (setAll || memcmp(newTexParams.fSwizzleRGBA,
2125 oldTexParams.fSwizzleRGBA,
2126 sizeof(newTexParams.fSwizzleRGBA)))) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002127 this->setTextureUnit(unitIdx);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002128 set_tex_swizzle(newTexParams.fSwizzleRGBA,
2129 this->glInterface());
2130 }
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002131 texture->setCachedTexParams(newTexParams, this->getResetTimestamp());
bsalomon@google.com4c883782012-06-04 19:05:11 +00002132}
2133
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002134void GrGpuGL::flushMiscFixedFunctionState() {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002135
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002136 const GrDrawState& drawState = this->getDrawState();
reed@google.comac10a2d2010-12-22 21:39:39 +00002137
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002138 if (drawState.isDitherState()) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002139 if (kYes_TriState != fHWDitherEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002140 GL_CALL(Enable(GR_GL_DITHER));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002141 fHWDitherEnabled = kYes_TriState;
2142 }
2143 } else {
2144 if (kNo_TriState != fHWDitherEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002145 GL_CALL(Disable(GR_GL_DITHER));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002146 fHWDitherEnabled = kNo_TriState;
reed@google.comac10a2d2010-12-22 21:39:39 +00002147 }
2148 }
2149
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002150 if (drawState.isColorWriteDisabled()) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002151 if (kNo_TriState != fHWWriteToColor) {
2152 GL_CALL(ColorMask(GR_GL_FALSE, GR_GL_FALSE,
2153 GR_GL_FALSE, GR_GL_FALSE));
2154 fHWWriteToColor = kNo_TriState;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002155 }
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002156 } else {
2157 if (kYes_TriState != fHWWriteToColor) {
2158 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
2159 fHWWriteToColor = kYes_TriState;
2160 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00002161 }
2162
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002163 if (fHWDrawFace != drawState.getDrawFace()) {
bsalomon@google.coma5d056a2012-03-27 15:59:58 +00002164 switch (this->getDrawState().getDrawFace()) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002165 case GrDrawState::kCCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002166 GL_CALL(Enable(GR_GL_CULL_FACE));
2167 GL_CALL(CullFace(GR_GL_BACK));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002168 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002169 case GrDrawState::kCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002170 GL_CALL(Enable(GR_GL_CULL_FACE));
2171 GL_CALL(CullFace(GR_GL_FRONT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002172 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002173 case GrDrawState::kBoth_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002174 GL_CALL(Disable(GR_GL_CULL_FACE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002175 break;
2176 default:
2177 GrCrash("Unknown draw face.");
2178 }
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002179 fHWDrawFace = drawState.getDrawFace();
bsalomon@google.comd302f142011-03-03 13:54:13 +00002180 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002181}
2182
reed@google.comac10a2d2010-12-22 21:39:39 +00002183void GrGpuGL::notifyRenderTargetDelete(GrRenderTarget* renderTarget) {
2184 GrAssert(NULL != renderTarget);
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002185 if (fHWBoundRenderTarget == renderTarget) {
2186 fHWBoundRenderTarget = NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +00002187 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002188}
2189
2190void GrGpuGL::notifyTextureDelete(GrGLTexture* texture) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002191 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002192 if (fHWBoundTextures[s] == texture) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002193 // deleting bound texture does implied bind to 0
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002194 fHWBoundTextures[s] = NULL;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002195 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002196 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002197}
2198
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002199bool GrGpuGL::configToGLFormats(GrPixelConfig config,
2200 bool getSizedInternalFormat,
2201 GrGLenum* internalFormat,
2202 GrGLenum* externalFormat,
2203 GrGLenum* externalType) {
2204 GrGLenum dontCare;
2205 if (NULL == internalFormat) {
2206 internalFormat = &dontCare;
2207 }
2208 if (NULL == externalFormat) {
2209 externalFormat = &dontCare;
2210 }
2211 if (NULL == externalType) {
2212 externalType = &dontCare;
2213 }
2214
reed@google.comac10a2d2010-12-22 21:39:39 +00002215 switch (config) {
bsalomon@google.com0342a852012-08-20 19:22:38 +00002216 case kRGBA_8888_GrPixelConfig:
bsalomon@google.comc4364992011-11-07 15:54:49 +00002217 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002218 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002219 if (getSizedInternalFormat) {
2220 *internalFormat = GR_GL_RGBA8;
2221 } else {
2222 *internalFormat = GR_GL_RGBA;
2223 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002224 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.comc4364992011-11-07 15:54:49 +00002225 break;
bsalomon@google.com0342a852012-08-20 19:22:38 +00002226 case kBGRA_8888_GrPixelConfig:
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002227 if (!this->glCaps().bgraFormatSupport()) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002228 return false;
2229 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002230 if (this->glCaps().bgraIsInternalFormat()) {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002231 if (getSizedInternalFormat) {
2232 *internalFormat = GR_GL_BGRA8;
2233 } else {
2234 *internalFormat = GR_GL_BGRA;
2235 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002236 } else {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002237 if (getSizedInternalFormat) {
2238 *internalFormat = GR_GL_RGBA8;
2239 } else {
2240 *internalFormat = GR_GL_RGBA;
2241 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002242 }
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002243 *externalFormat = GR_GL_BGRA;
bsalomon@google.com6f379512011-11-16 20:36:03 +00002244 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002245 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002246 case kRGB_565_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002247 *internalFormat = GR_GL_RGB;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002248 *externalFormat = GR_GL_RGB;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002249 if (getSizedInternalFormat) {
2250 if (this->glBinding() == kDesktop_GrGLBinding) {
2251 return false;
2252 } else {
2253 *internalFormat = GR_GL_RGB565;
2254 }
2255 } else {
2256 *internalFormat = GR_GL_RGB;
2257 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002258 *externalType = GR_GL_UNSIGNED_SHORT_5_6_5;
reed@google.comac10a2d2010-12-22 21:39:39 +00002259 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002260 case kRGBA_4444_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002261 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002262 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002263 if (getSizedInternalFormat) {
2264 *internalFormat = GR_GL_RGBA4;
2265 } else {
2266 *internalFormat = GR_GL_RGBA;
2267 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002268 *externalType = GR_GL_UNSIGNED_SHORT_4_4_4_4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002269 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002270 case kIndex_8_GrPixelConfig:
bsalomon@google.comf6601872012-08-28 21:11:35 +00002271 if (this->getCaps().eightBitPaletteSupport()) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002272 *internalFormat = GR_GL_PALETTE8_RGBA8;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002273 // glCompressedTexImage doesn't take external params
2274 *externalFormat = GR_GL_PALETTE8_RGBA8;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002275 // no sized/unsized internal format distinction here
2276 *internalFormat = GR_GL_PALETTE8_RGBA8;
2277 // unused with CompressedTexImage
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002278 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002279 } else {
2280 return false;
2281 }
2282 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002283 case kAlpha_8_GrPixelConfig:
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002284 if (this->glCaps().textureRedSupport()) {
2285 *internalFormat = GR_GL_RED;
2286 *externalFormat = GR_GL_RED;
2287 if (getSizedInternalFormat) {
2288 *internalFormat = GR_GL_R8;
2289 } else {
2290 *internalFormat = GR_GL_RED;
2291 }
2292 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002293 } else {
2294 *internalFormat = GR_GL_ALPHA;
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002295 *externalFormat = GR_GL_ALPHA;
2296 if (getSizedInternalFormat) {
2297 *internalFormat = GR_GL_ALPHA8;
2298 } else {
2299 *internalFormat = GR_GL_ALPHA;
2300 }
2301 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002302 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002303 break;
2304 default:
2305 return false;
2306 }
2307 return true;
2308}
2309
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002310void GrGpuGL::setTextureUnit(int unit) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002311 GrAssert(unit >= 0 && unit < GrDrawState::kNumStages);
bsalomon@google.com49209392012-06-05 15:13:46 +00002312 if (fHWActiveTextureUnitIdx != unit) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002313 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com49209392012-06-05 15:13:46 +00002314 fHWActiveTextureUnitIdx = unit;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002315 }
2316}
bsalomon@google.com316f99232011-01-13 21:28:12 +00002317
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002318void GrGpuGL::setSpareTextureUnit() {
bsalomon@google.com49209392012-06-05 15:13:46 +00002319 if (fHWActiveTextureUnitIdx != (GR_GL_TEXTURE0 + SPARE_TEX_UNIT)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002320 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + SPARE_TEX_UNIT));
bsalomon@google.com49209392012-06-05 15:13:46 +00002321 fHWActiveTextureUnitIdx = SPARE_TEX_UNIT;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002322 }
2323}
2324
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00002325///////////////////////////////////////////////////////////////////////////////
2326
bsalomon@google.com6918d482013-03-07 19:09:11 +00002327GrGLAttribArrayState* GrGpuGL::HWGeometryState::bindArrayAndBuffersToDraw(
2328 GrGpuGL* gpu,
2329 const GrGLVertexBuffer* vbuffer,
2330 const GrGLIndexBuffer* ibuffer) {
2331 GrAssert(NULL != vbuffer);
2332 GrGLAttribArrayState* attribState = &fDefaultVertexArrayAttribState;
2333 // We use a vertex array if we're on a core profile and the verts are in a VBO.
2334 if (gpu->glCaps().isCoreProfile() && !vbuffer->isCPUBacked()) {
2335 if (NULL == fVBOVertexArray || !fVBOVertexArray->isValid()) {
2336 SkSafeUnref(fVBOVertexArray);
2337 GrGLuint arrayID;
2338 GR_GL_CALL(gpu->glInterface(), GenVertexArrays(1, &arrayID));
2339 int attrCount = gpu->glCaps().maxVertexAttributes();
2340 fVBOVertexArray = SkNEW_ARGS(GrGLVertexArray, (gpu, arrayID, attrCount));
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00002341 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00002342 attribState = fVBOVertexArray->bindWithIndexBuffer(ibuffer);
2343 } else {
2344 if (NULL != ibuffer) {
2345 this->setIndexBufferIDOnDefaultVertexArray(gpu, ibuffer->bufferID());
2346 } else {
2347 this->setVertexArrayID(gpu, 0);
2348 }
2349 int attrCount = gpu->glCaps().maxVertexAttributes();
2350 if (fDefaultVertexArrayAttribState.count() != attrCount) {
2351 fDefaultVertexArrayAttribState.resize(attrCount);
2352 }
2353 attribState = &fDefaultVertexArrayAttribState;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00002354 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00002355 return attribState;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002356}