blob: 42e83b0435b1347da2ad2c2e1c974a678f5a1842 [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
bsalomon@google.com880b8fc2013-02-19 20:17:28 +0000183 fHWGeometryState.setMaxAttribArrays(this->glCaps().maxVertexAttributes());
184
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000185 GrAssert(this->glCaps().maxVertexAttributes() >= GrDrawState::kVertexAttribCnt);
186 GrAssert(this->glCaps().maxVertexAttributes() > GrDrawState::kColorOverrideAttribIndexValue);
187 GrAssert(this->glCaps().maxVertexAttributes() > GrDrawState::kCoverageOverrideAttribIndexValue);
188
bsalomon@google.comfe676522011-06-17 18:12:21 +0000189 fLastSuccessfulStencilFmtIdx = 0;
caryclark@google.comcf6285b2012-06-06 12:09:01 +0000190 if (false) { // avoid bit rot, suppress warning
191 fbo_test(this->glInterface(), 0, 0);
192 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000193}
194
195GrGpuGL::~GrGpuGL() {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000196 if (0 != fHWProgramID) {
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000197 // detach the current program so there is no confusion on OpenGL's part
198 // that we want it to be deleted
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +0000199 GrAssert(fHWProgramID == fCurrentProgram->programID());
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000200 GL_CALL(UseProgram(0));
201 }
202
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000203 delete fProgramCache;
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000204
bsalomon@google.com4a018bb2011-10-28 19:50:21 +0000205 // This must be called by before the GrDrawTarget destructor
206 this->releaseGeometry();
bsalomon@google.com15b11df2011-09-16 21:18:29 +0000207 // This subclass must do this before the base class destructor runs
208 // since we will unref the GrGLInterface.
209 this->releaseResources();
reed@google.comac10a2d2010-12-22 21:39:39 +0000210}
211
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000212///////////////////////////////////////////////////////////////////////////////
213
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000214void GrGpuGL::initCaps() {
215 GrGLint maxTextureUnits;
216 // check FS and fixed-function texture unit limits
217 // we only use textures in the fragment stage currently.
218 // checks are > to make sure we have a spare unit.
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000219 const GrGLInterface* gl = this->glInterface();
220 GR_GL_GetIntegerv(gl, GR_GL_MAX_TEXTURE_IMAGE_UNITS, &maxTextureUnits);
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000221 GrAssert(maxTextureUnits > GrDrawState::kNumStages);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000222
bsalomon@google.comf6601872012-08-28 21:11:35 +0000223 CapsInternals* caps = this->capsInternals();
224
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000225 GrGLint numFormats;
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000226 GR_GL_GetIntegerv(gl, GR_GL_NUM_COMPRESSED_TEXTURE_FORMATS, &numFormats);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000227 SkAutoSTMalloc<10, GrGLint> formats(numFormats);
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000228 GR_GL_GetIntegerv(gl, GR_GL_COMPRESSED_TEXTURE_FORMATS, formats);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000229 for (int i = 0; i < numFormats; ++i) {
230 if (formats[i] == GR_GL_PALETTE8_RGBA8) {
bsalomon@google.comf6601872012-08-28 21:11:35 +0000231 caps->f8BitPaletteSupport = true;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000232 break;
233 }
234 }
235
236 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000237 // we could also look for GL_ATI_separate_stencil extension or
238 // GL_EXT_stencil_two_side but they use different function signatures
239 // than GL2.0+ (and than each other).
bsalomon@google.comf6601872012-08-28 21:11:35 +0000240 caps->fTwoSidedStencilSupport = (this->glVersion() >= GR_GL_VER(2,0));
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000241 // supported on GL 1.4 and higher or by extension
bsalomon@google.comf6601872012-08-28 21:11:35 +0000242 caps->fStencilWrapOpsSupport = (this->glVersion() >= GR_GL_VER(1,4)) ||
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000243 this->hasExtension("GL_EXT_stencil_wrap");
244 } else {
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000245 // ES 2 has two sided stencil and stencil wrap
bsalomon@google.comf6601872012-08-28 21:11:35 +0000246 caps->fTwoSidedStencilSupport = true;
247 caps->fStencilWrapOpsSupport = true;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000248 }
249
250 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.comf6601872012-08-28 21:11:35 +0000251 caps->fBufferLockSupport = true; // we require VBO support and the desktop VBO
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000252 // extension includes glMapBuffer.
253 } else {
bsalomon@google.comf6601872012-08-28 21:11:35 +0000254 caps->fBufferLockSupport = this->hasExtension("GL_OES_mapbuffer");
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000255 }
256
257 if (kDesktop_GrGLBinding == this->glBinding()) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000258 if (this->glVersion() >= GR_GL_VER(2,0) ||
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000259 this->hasExtension("GL_ARB_texture_non_power_of_two")) {
bsalomon@google.comf6601872012-08-28 21:11:35 +0000260 caps->fNPOTTextureTileSupport = true;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000261 } else {
bsalomon@google.comf6601872012-08-28 21:11:35 +0000262 caps->fNPOTTextureTileSupport = false;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000263 }
264 } else {
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000265 // Unextended ES2 supports NPOT textures with clamp_to_edge and non-mip filters only
bsalomon@google.comf6601872012-08-28 21:11:35 +0000266 caps->fNPOTTextureTileSupport = this->hasExtension("GL_OES_texture_npot");
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000267 }
268
bsalomon@google.comf6601872012-08-28 21:11:35 +0000269 caps->fHWAALineSupport = (kDesktop_GrGLBinding == this->glBinding());
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000270
bsalomon@google.comf6601872012-08-28 21:11:35 +0000271 GR_GL_GetIntegerv(gl, GR_GL_MAX_TEXTURE_SIZE, &caps->fMaxTextureSize);
272 GR_GL_GetIntegerv(gl, GR_GL_MAX_RENDERBUFFER_SIZE, &caps->fMaxRenderTargetSize);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000273 // Our render targets are always created with textures as the color
274 // attachment, hence this min:
bsalomon@google.comf6601872012-08-28 21:11:35 +0000275 caps->fMaxRenderTargetSize = GrMin(caps->fMaxTextureSize, caps->fMaxRenderTargetSize);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000276
bsalomon@google.comf6601872012-08-28 21:11:35 +0000277 caps->fFSAASupport = GrGLCaps::kNone_MSFBOType != this->glCaps().msFBOType();
278 caps->fPathStencilingSupport = GR_GL_USE_NV_PATH_RENDERING &&
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000279 this->hasExtension("GL_NV_path_rendering");
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000280
281 // Enable supported shader-related caps
282 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.comf6601872012-08-28 21:11:35 +0000283 caps->fDualSourceBlendingSupport =
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000284 this->glVersion() >= GR_GL_VER(3,3) ||
285 this->hasExtension("GL_ARB_blend_func_extended");
bsalomon@google.comf6601872012-08-28 21:11:35 +0000286 caps->fShaderDerivativeSupport = true;
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000287 // we don't support GL_ARB_geometry_shader4, just GL 3.2+ GS
bsalomon@google.comf6601872012-08-28 21:11:35 +0000288 caps->fGeometryShaderSupport =
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000289 this->glVersion() >= GR_GL_VER(3,2) &&
290 this->glslGeneration() >= k150_GrGLSLGeneration;
291 } else {
bsalomon@google.comf6601872012-08-28 21:11:35 +0000292 caps->fShaderDerivativeSupport =
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000293 this->hasExtension("GL_OES_standard_derivatives");
294 }
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000295}
296
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000297void GrGpuGL::fillInConfigRenderableTable() {
298
299 // OpenGL < 3.0
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000300 // no support for render targets unless the GL_ARB_framebuffer_object
301 // extension is supported (in which case we get ALPHA, RED, RG, RGB,
302 // RGBA (ALPHA8, RGBA4, RGBA8) for OpenGL > 1.1). Note that we
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000303 // probably don't get R8 in this case.
304
305 // OpenGL 3.0
306 // base color renderable: ALPHA, RED, RG, RGB, and RGBA
307 // sized derivatives: ALPHA8, R8, RGBA4, RGBA8
308
309 // >= OpenGL 3.1
310 // base color renderable: RED, RG, RGB, and RGBA
311 // sized derivatives: R8, RGBA4, RGBA8
312 // if the GL_ARB_compatibility extension is supported then we get back
313 // support for GL_ALPHA and ALPHA8
314
315 // GL_EXT_bgra adds BGRA render targets to any version
316
317 // ES 2.0
318 // color renderable: RGBA4, RGB5_A1, RGB565
319 // GL_EXT_texture_rg adds support for R8 as a color render target
320 // GL_OES_rgb8_rgba8 and/or GL_ARM_rgba8 adds support for RGBA8
321 // GL_EXT_texture_format_BGRA8888 and/or GL_APPLE_texture_format_BGRA8888
322 // added BGRA support
323
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 }
bsalomon@google.come76b7cc2012-06-18 12:47:06 +0000425 if (this->glCaps().imagingSupport()) {
426 GL_CALL(Disable(GR_GL_COLOR_TABLE));
427 }
bsalomon@google.com469d0dd2012-05-21 20:14:29 +0000428 GL_CALL(Disable(GR_GL_POLYGON_OFFSET_FILL));
429 // Since ES doesn't support glPointSize at all we always use the VS to
430 // set the point size
431 GL_CALL(Enable(GR_GL_VERTEX_PROGRAM_POINT_SIZE));
432
433 // We should set glPolygonMode(FRONT_AND_BACK,FILL) here, too. It isn't
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000434 // currently part of our gl interface. There are probably others as
bsalomon@google.com469d0dd2012-05-21 20:14:29 +0000435 // well.
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000436 }
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +0000437 fHWAAState.invalidate();
bsalomon@google.com978c8c62012-05-21 14:45:49 +0000438 fHWWriteToColor = kUnknown_TriState;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000439
reed@google.comac10a2d2010-12-22 21:39:39 +0000440 // we only ever use lines in hairline mode
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000441 GL_CALL(LineWidth(1));
reed@google.comac10a2d2010-12-22 21:39:39 +0000442
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000443 // invalid
bsalomon@google.com49209392012-06-05 15:13:46 +0000444 fHWActiveTextureUnitIdx = -1;
reed@google.comac10a2d2010-12-22 21:39:39 +0000445
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +0000446 fHWBlendState.invalidate();
bsalomon@google.com080773c2011-03-15 19:09:25 +0000447
tomhudson@google.com93813632011-10-27 20:21:16 +0000448 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.comc811ea32012-05-21 15:33:09 +0000449 fHWBoundTextures[s] = NULL;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000450 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000451
bsalomon@google.coma3201942012-06-21 19:58:20 +0000452 fHWScissorSettings.invalidate();
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000453
bsalomon@google.coma3201942012-06-21 19:58:20 +0000454 fHWViewport.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000455
bsalomon@google.com457b8a32012-05-21 21:19:58 +0000456 fHWStencilSettings.invalidate();
bsalomon@google.coma3201942012-06-21 19:58:20 +0000457 fHWStencilTestEnabled = kUnknown_TriState;
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000458
bsalomon@google.com880b8fc2013-02-19 20:17:28 +0000459 fHWGeometryState.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000460
bsalomon@google.comc811ea32012-05-21 15:33:09 +0000461 fHWBoundRenderTarget = NULL;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000462
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +0000463 fHWPathStencilMatrixState.invalidate();
bsalomon@google.comf6601872012-08-28 21:11:35 +0000464 if (fCaps.pathStencilingSupport()) {
bsalomon@google.com05a718c2012-06-29 14:01:53 +0000465 // we don't use the model view matrix.
466 GL_CALL(MatrixMode(GR_GL_MODELVIEW));
467 GL_CALL(LoadIdentity());
468 }
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000469
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000470 // we assume these values
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000471 if (this->glCaps().unpackRowLengthSupport()) {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000472 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
473 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000474 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000475 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
476 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000477 if (this->glCaps().unpackFlipYSupport()) {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000478 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
479 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000480 if (this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000481 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, GR_GL_FALSE));
482 }
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000483
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000484 fHWProgramID = 0;
bsalomon@google.com91207482013-02-12 21:45:24 +0000485 fSharedGLProgramState.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000486}
487
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000488namespace {
489
490GrSurfaceOrigin resolve_origin(GrSurfaceOrigin origin, bool renderTarget) {
491 // By default, GrRenderTargets are GL's normal orientation so that they
492 // can be drawn to by the outside world without the client having
493 // to render upside down.
494 if (kDefault_GrSurfaceOrigin == origin) {
495 return renderTarget ? kBottomLeft_GrSurfaceOrigin : kTopLeft_GrSurfaceOrigin;
496 } else {
497 return origin;
498 }
499}
500
501}
502
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000503GrTexture* GrGpuGL::onWrapBackendTexture(const GrBackendTextureDesc& desc) {
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000504 if (!this->configToGLFormats(desc.fConfig, false, NULL, NULL, NULL)) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000505 return NULL;
506 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000507
robertphillips@google.comb72e5d32012-10-30 15:18:10 +0000508 if (0 == desc.fTextureHandle) {
509 return NULL;
510 }
511
512 int maxSize = this->getCaps().maxTextureSize();
513 if (desc.fWidth > maxSize || desc.fHeight > maxSize) {
514 return NULL;
515 }
516
517 GrGLTexture::Desc glTexDesc;
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000518 // next line relies on GrBackendTextureDesc's flags matching GrTexture's
robertphillips@google.com32716282012-06-04 12:48:45 +0000519 glTexDesc.fFlags = (GrTextureFlags) desc.fFlags;
bsalomon@google.com99621082011-11-15 16:47:16 +0000520 glTexDesc.fWidth = desc.fWidth;
521 glTexDesc.fHeight = desc.fHeight;
bsalomon@google.come269f212011-11-07 13:29:52 +0000522 glTexDesc.fConfig = desc.fConfig;
robertphillips@google.com32716282012-06-04 12:48:45 +0000523 glTexDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.come269f212011-11-07 13:29:52 +0000524 glTexDesc.fTextureID = static_cast<GrGLuint>(desc.fTextureHandle);
bsalomon@google.com72830222013-01-23 20:25:22 +0000525 glTexDesc.fIsWrapped = true;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000526 bool renderTarget = 0 != (desc.fFlags & kRenderTarget_GrBackendTextureFlag);
527 // FIXME: this should be calling resolve_origin(), but Chrome code is currently
528 // assuming the old behaviour, which is that backend textures are always
529 // BottomLeft, even for non-RT's. Once Chrome is fixed, change this to:
530 // glTexDesc.fOrigin = resolve_origin(desc.fOrigin, renderTarget);
531 if (kDefault_GrSurfaceOrigin == desc.fOrigin) {
532 glTexDesc.fOrigin = kBottomLeft_GrSurfaceOrigin;
533 } else {
534 glTexDesc.fOrigin = desc.fOrigin;
535 }
bsalomon@google.come269f212011-11-07 13:29:52 +0000536
537 GrGLTexture* texture = NULL;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000538 if (renderTarget) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000539 GrGLRenderTarget::Desc glRTDesc;
540 glRTDesc.fRTFBOID = 0;
541 glRTDesc.fTexFBOID = 0;
542 glRTDesc.fMSColorRenderbufferID = 0;
bsalomon@google.come269f212011-11-07 13:29:52 +0000543 glRTDesc.fConfig = desc.fConfig;
544 glRTDesc.fSampleCnt = desc.fSampleCnt;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000545 glRTDesc.fOrigin = glTexDesc.fOrigin;
bsalomon@google.com99621082011-11-15 16:47:16 +0000546 if (!this->createRenderTargetObjects(glTexDesc.fWidth,
547 glTexDesc.fHeight,
bsalomon@google.come269f212011-11-07 13:29:52 +0000548 glTexDesc.fTextureID,
549 &glRTDesc)) {
550 return NULL;
551 }
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000552 texture = SkNEW_ARGS(GrGLTexture, (this, glTexDesc, glRTDesc));
bsalomon@google.come269f212011-11-07 13:29:52 +0000553 } else {
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000554 texture = SkNEW_ARGS(GrGLTexture, (this, glTexDesc));
bsalomon@google.come269f212011-11-07 13:29:52 +0000555 }
556 if (NULL == texture) {
557 return NULL;
558 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000559
bsalomon@google.come269f212011-11-07 13:29:52 +0000560 this->setSpareTextureUnit();
561 return texture;
562}
563
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000564GrRenderTarget* GrGpuGL::onWrapBackendRenderTarget(const GrBackendRenderTargetDesc& desc) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000565 GrGLRenderTarget::Desc glDesc;
566 glDesc.fConfig = desc.fConfig;
567 glDesc.fRTFBOID = static_cast<GrGLuint>(desc.fRenderTargetHandle);
568 glDesc.fMSColorRenderbufferID = 0;
569 glDesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
570 glDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.com72830222013-01-23 20:25:22 +0000571 glDesc.fIsWrapped = true;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000572 glDesc.fOrigin = desc.fOrigin;
573 if (glDesc.fRTFBOID == 0) {
574 GrAssert(desc.fOrigin == kBottomLeft_GrSurfaceOrigin);
575 }
576
577 glDesc.fOrigin = resolve_origin(desc.fOrigin, true);
bsalomon@google.come269f212011-11-07 13:29:52 +0000578 GrGLIRect viewport;
579 viewport.fLeft = 0;
580 viewport.fBottom = 0;
581 viewport.fWidth = desc.fWidth;
582 viewport.fHeight = desc.fHeight;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000583
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000584 GrRenderTarget* tgt = SkNEW_ARGS(GrGLRenderTarget,
585 (this, glDesc, viewport));
bsalomon@google.come269f212011-11-07 13:29:52 +0000586 if (desc.fStencilBits) {
587 GrGLStencilBuffer::Format format;
588 format.fInternalFormat = GrGLStencilBuffer::kUnknownInternalFormat;
589 format.fPacked = false;
590 format.fStencilBits = desc.fStencilBits;
591 format.fTotalBits = desc.fStencilBits;
bsalomon@google.com1f0f1a32013-01-23 21:32:32 +0000592 static const bool kIsSBWrapped = false;
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000593 GrGLStencilBuffer* sb = SkNEW_ARGS(GrGLStencilBuffer,
594 (this,
bsalomon@google.com1f0f1a32013-01-23 21:32:32 +0000595 kIsSBWrapped,
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000596 0,
597 desc.fWidth,
598 desc.fHeight,
599 desc.fSampleCnt,
600 format));
bsalomon@google.come269f212011-11-07 13:29:52 +0000601 tgt->setStencilBuffer(sb);
602 sb->unref();
603 }
604 return tgt;
605}
606
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000607////////////////////////////////////////////////////////////////////////////////
608
bsalomon@google.com9c680582013-02-06 18:17:50 +0000609bool GrGpuGL::onWriteTexturePixels(GrTexture* texture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000610 int left, int top, int width, int height,
611 GrPixelConfig config, const void* buffer,
612 size_t rowBytes) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000613 if (NULL == buffer) {
bsalomon@google.com9c680582013-02-06 18:17:50 +0000614 return false;
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000615 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000616 GrGLTexture* glTex = static_cast<GrGLTexture*>(texture);
617
bsalomon@google.com6f379512011-11-16 20:36:03 +0000618 this->setSpareTextureUnit();
619 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTex->textureID()));
620 GrGLTexture::Desc desc;
robertphillips@google.com32716282012-06-04 12:48:45 +0000621 desc.fFlags = glTex->desc().fFlags;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000622 desc.fWidth = glTex->width();
623 desc.fHeight = glTex->height();
robertphillips@google.com32716282012-06-04 12:48:45 +0000624 desc.fConfig = glTex->config();
625 desc.fSampleCnt = glTex->desc().fSampleCnt;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000626 desc.fTextureID = glTex->textureID();
bsalomon@google.com2d0bade2012-10-26 19:01:17 +0000627 desc.fOrigin = glTex->origin();
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000628
bsalomon@google.com9c680582013-02-06 18:17:50 +0000629 return this->uploadTexData(desc, false,
630 left, top, width, height,
631 config, buffer, rowBytes);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000632}
633
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000634namespace {
635bool adjust_pixel_ops_params(int surfaceWidth,
636 int surfaceHeight,
637 size_t bpp,
638 int* left, int* top, int* width, int* height,
639 const void** data,
640 size_t* rowBytes) {
641 if (!*rowBytes) {
642 *rowBytes = *width * bpp;
643 }
644
645 GrIRect subRect = GrIRect::MakeXYWH(*left, *top, *width, *height);
646 GrIRect bounds = GrIRect::MakeWH(surfaceWidth, surfaceHeight);
647
648 if (!subRect.intersect(bounds)) {
649 return false;
650 }
651 *data = reinterpret_cast<const void*>(reinterpret_cast<intptr_t>(*data) +
652 (subRect.fTop - *top) * *rowBytes + (subRect.fLeft - *left) * bpp);
653
654 *left = subRect.fLeft;
655 *top = subRect.fTop;
656 *width = subRect.width();
657 *height = subRect.height();
658 return true;
659}
660}
661
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000662bool GrGpuGL::uploadTexData(const GrGLTexture::Desc& desc,
663 bool isNewTexture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000664 int left, int top, int width, int height,
665 GrPixelConfig dataConfig,
666 const void* data,
667 size_t rowBytes) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000668 GrAssert(NULL != data || isNewTexture);
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000669
670 size_t bpp = GrBytesPerPixel(dataConfig);
671 if (!adjust_pixel_ops_params(desc.fWidth, desc.fHeight, bpp, &left, &top,
672 &width, &height, &data, &rowBytes)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000673 return false;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000674 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000675 size_t trimRowBytes = width * bpp;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000676
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000677 // in case we need a temporary, trimmed copy of the src pixels
678 SkAutoSMalloc<128 * 128> tempStorage;
679
bsalomon@google.com313f0192012-07-10 17:21:02 +0000680 // paletted textures cannot be partially updated
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000681 bool useTexStorage = isNewTexture &&
bsalomon@google.com313f0192012-07-10 17:21:02 +0000682 desc.fConfig != kIndex_8_GrPixelConfig &&
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000683 this->glCaps().texStorageSupport();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000684
bsalomon@google.com313f0192012-07-10 17:21:02 +0000685 if (useTexStorage && kDesktop_GrGLBinding == this->glBinding()) {
686 // 565 is not a sized internal format on desktop GL. So on desktop with
687 // 565 we always use an unsized internal format to let the system pick
688 // the best sized format to convert the 565 data to. Since TexStorage
689 // only allows sized internal formats we will instead use TexImage2D.
690 useTexStorage = desc.fConfig != kRGB_565_GrPixelConfig;
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000691 }
692
693 GrGLenum internalFormat;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000694 GrGLenum externalFormat;
695 GrGLenum externalType;
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000696 // glTexStorage requires sized internal formats on both desktop and ES. ES
697 // glTexImage requires an unsized format.
698 if (!this->configToGLFormats(dataConfig, useTexStorage, &internalFormat,
699 &externalFormat, &externalType)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000700 return false;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000701 }
702
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000703 if (!isNewTexture && GR_GL_PALETTE8_RGBA8 == internalFormat) {
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000704 // paletted textures cannot be updated
705 return false;
706 }
707
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000708 /*
bsalomon@google.com6f379512011-11-16 20:36:03 +0000709 * check whether to allocate a temporary buffer for flipping y or
710 * because our srcData has extra bytes past each row. If so, we need
711 * to trim those off here, since GL ES may not let us specify
712 * GL_UNPACK_ROW_LENGTH.
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000713 */
bsalomon@google.com6f379512011-11-16 20:36:03 +0000714 bool restoreGLRowLength = false;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000715 bool swFlipY = false;
716 bool glFlipY = false;
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000717 if (NULL != data) {
senorblanco@chromium.orgef5dbe12013-01-28 16:42:38 +0000718 if (kBottomLeft_GrSurfaceOrigin == desc.fOrigin) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000719 if (this->glCaps().unpackFlipYSupport()) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000720 glFlipY = true;
721 } else {
722 swFlipY = true;
723 }
724 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000725 if (this->glCaps().unpackRowLengthSupport() && !swFlipY) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000726 // can't use this for flipping, only non-neg values allowed. :(
727 if (rowBytes != trimRowBytes) {
728 GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
729 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength));
730 restoreGLRowLength = true;
731 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000732 } else {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000733 if (trimRowBytes != rowBytes || swFlipY) {
734 // copy data into our new storage, skipping the trailing bytes
735 size_t trimSize = height * trimRowBytes;
736 const char* src = (const char*)data;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000737 if (swFlipY) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000738 src += (height - 1) * rowBytes;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000739 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000740 char* dst = (char*)tempStorage.reset(trimSize);
741 for (int y = 0; y < height; y++) {
742 memcpy(dst, src, trimRowBytes);
743 if (swFlipY) {
744 src -= rowBytes;
745 } else {
746 src += rowBytes;
747 }
748 dst += trimRowBytes;
749 }
750 // now point data to our copied version
751 data = tempStorage.get();
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000752 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000753 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000754 if (glFlipY) {
755 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_TRUE));
756 }
757 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, static_cast<GrGLint>(bpp)));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000758 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000759 bool succeeded = true;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000760 if (isNewTexture &&
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000761 0 == left && 0 == top &&
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000762 desc.fWidth == width && desc.fHeight == height) {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000763 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000764 if (useTexStorage) {
765 // We never resize or change formats of textures. We don't use
766 // mipmaps currently.
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000767 GL_ALLOC_CALL(this->glInterface(),
768 TexStorage2D(GR_GL_TEXTURE_2D,
769 1, // levels
770 internalFormat,
771 desc.fWidth, desc.fHeight));
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000772 } else {
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000773 if (GR_GL_PALETTE8_RGBA8 == internalFormat) {
774 GrGLsizei imageSize = desc.fWidth * desc.fHeight +
775 kGrColorTableSize;
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000776 GL_ALLOC_CALL(this->glInterface(),
777 CompressedTexImage2D(GR_GL_TEXTURE_2D,
778 0, // level
779 internalFormat,
780 desc.fWidth, desc.fHeight,
781 0, // border
782 imageSize,
783 data));
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000784 } else {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000785 GL_ALLOC_CALL(this->glInterface(),
786 TexImage2D(GR_GL_TEXTURE_2D,
787 0, // level
788 internalFormat,
789 desc.fWidth, desc.fHeight,
790 0, // border
791 externalFormat, externalType,
792 data));
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000793 }
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000794 }
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000795 GrGLenum error = CHECK_ALLOC_ERROR(this->glInterface());
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000796 if (error != GR_GL_NO_ERROR) {
797 succeeded = false;
798 } else {
799 // if we have data and we used TexStorage to create the texture, we
800 // now upload with TexSubImage.
801 if (NULL != data && useTexStorage) {
802 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D,
803 0, // level
804 left, top,
805 width, height,
806 externalFormat, externalType,
807 data));
808 }
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000809 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000810 } else {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000811 if (swFlipY || glFlipY) {
bsalomon@google.com6f379512011-11-16 20:36:03 +0000812 top = desc.fHeight - (top + height);
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000813 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000814 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D,
815 0, // level
816 left, top,
817 width, height,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000818 externalFormat, externalType, data));
819 }
820
821 if (restoreGLRowLength) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000822 GrAssert(this->glCaps().unpackRowLengthSupport());
bsalomon@google.com6f379512011-11-16 20:36:03 +0000823 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000824 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000825 if (glFlipY) {
826 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
827 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000828 return succeeded;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000829}
830
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000831namespace {
robertphillips@google.com6177e692013-02-28 20:16:25 +0000832bool renderbuffer_storage_msaa(GrGLContext& ctx,
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000833 int sampleCount,
834 GrGLenum format,
835 int width, int height) {
robertphillips@google.com6177e692013-02-28 20:16:25 +0000836 CLEAR_ERROR_BEFORE_ALLOC(ctx.interface());
837 GrAssert(GrGLCaps::kNone_MSFBOType != ctx.info().caps().msFBOType());
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000838 bool created = false;
839 if (GrGLCaps::kNVDesktop_CoverageAAType ==
robertphillips@google.com6177e692013-02-28 20:16:25 +0000840 ctx.info().caps().coverageAAType()) {
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000841 const GrGLCaps::MSAACoverageMode& mode =
robertphillips@google.com6177e692013-02-28 20:16:25 +0000842 ctx.info().caps().getMSAACoverageMode(sampleCount);
843 GL_ALLOC_CALL(ctx.interface(),
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000844 RenderbufferStorageMultisampleCoverage(GR_GL_RENDERBUFFER,
845 mode.fCoverageSampleCnt,
846 mode.fColorSampleCnt,
847 format,
848 width, height));
robertphillips@google.com6177e692013-02-28 20:16:25 +0000849 created = (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctx.interface()));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000850 }
851 if (!created) {
bsalomon@google.comf6b070d2012-04-27 14:25:44 +0000852 // glRBMS will fail if requested samples is > max samples.
robertphillips@google.com6177e692013-02-28 20:16:25 +0000853 sampleCount = GrMin(sampleCount, ctx.info().caps().maxSampleCount());
854 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
883 // If we are using multisampling we will create two FBOS. We render
884 // to one and then resolve to the texture bound to the other.
bsalomon@google.come269f212011-11-07 13:29:52 +0000885 if (desc->fSampleCnt > 0) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000886 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000887 goto FAILED;
888 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000889 GL_CALL(GenFramebuffers(1, &desc->fRTFBOID));
890 GL_CALL(GenRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000891 if (!desc->fRTFBOID ||
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000892 !desc->fMSColorRenderbufferID ||
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000893 !this->configToGLFormats(desc->fConfig,
894 // GLES requires sized internal formats
895 kES2_GrGLBinding == this->glBinding(),
896 &msColorFormat, NULL, NULL)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000897 goto FAILED;
898 }
899 } else {
900 desc->fRTFBOID = desc->fTexFBOID;
901 }
902
bsalomon@google.com0e9b41a2012-01-04 22:11:43 +0000903 // below here we may bind the FBO
bsalomon@google.comc811ea32012-05-21 15:33:09 +0000904 fHWBoundRenderTarget = NULL;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000905 if (desc->fRTFBOID != desc->fTexFBOID) {
bsalomon@google.com13ac3a32013-01-09 23:29:39 +0000906 GrAssert(desc->fSampleCnt > 0);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000907 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000908 desc->fMSColorRenderbufferID));
robertphillips@google.com6177e692013-02-28 20:16:25 +0000909 if (!renderbuffer_storage_msaa(fGLContext,
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000910 desc->fSampleCnt,
911 msColorFormat,
912 width, height)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000913 goto FAILED;
914 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000915 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fRTFBOID));
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000916 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000917 GR_GL_COLOR_ATTACHMENT0,
918 GR_GL_RENDERBUFFER,
919 desc->fMSColorRenderbufferID));
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000920 if (!this->glCaps().isConfigVerifiedColorAttachment(desc->fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000921 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
922 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
923 goto FAILED;
924 }
robertphillips@google.com6177e692013-02-28 20:16:25 +0000925 fGLContext.info().caps().markConfigAsValidColorAttachment(
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000926 desc->fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000927 }
928 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000929 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000930
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000931 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
932 GR_GL_COLOR_ATTACHMENT0,
933 GR_GL_TEXTURE_2D,
934 texID, 0));
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000935 if (!this->glCaps().isConfigVerifiedColorAttachment(desc->fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000936 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
937 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
938 goto FAILED;
939 }
robertphillips@google.com6177e692013-02-28 20:16:25 +0000940 fGLContext.info().caps().markConfigAsValidColorAttachment(desc->fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000941 }
942
943 return true;
944
945FAILED:
946 if (desc->fMSColorRenderbufferID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000947 GL_CALL(DeleteRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000948 }
949 if (desc->fRTFBOID != desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000950 GL_CALL(DeleteFramebuffers(1, &desc->fRTFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000951 }
952 if (desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000953 GL_CALL(DeleteFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000954 }
955 return false;
956}
957
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000958// good to set a break-point here to know when createTexture fails
959static GrTexture* return_null_texture() {
960// GrAssert(!"null texture");
961 return NULL;
962}
963
caryclark@google.comcf6285b2012-06-06 12:09:01 +0000964#if 0 && GR_DEBUG
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000965static size_t as_size_t(int x) {
966 return x;
967}
968#endif
969
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000970GrTexture* GrGpuGL::onCreateTexture(const GrTextureDesc& desc,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000971 const void* srcData,
972 size_t rowBytes) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000973
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000974 GrGLTexture::Desc glTexDesc;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000975 GrGLRenderTarget::Desc glRTDesc;
reed@google.comac10a2d2010-12-22 21:39:39 +0000976
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000977 // Attempt to catch un- or wrongly initialized sample counts;
978 GrAssert(desc.fSampleCnt >= 0 && desc.fSampleCnt <= 64);
979
robertphillips@google.com32716282012-06-04 12:48:45 +0000980 glTexDesc.fFlags = desc.fFlags;
bsalomon@google.com99621082011-11-15 16:47:16 +0000981 glTexDesc.fWidth = desc.fWidth;
982 glTexDesc.fHeight = desc.fHeight;
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000983 glTexDesc.fConfig = desc.fConfig;
robertphillips@google.com32716282012-06-04 12:48:45 +0000984 glTexDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.com72830222013-01-23 20:25:22 +0000985 glTexDesc.fIsWrapped = false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000986
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000987 glRTDesc.fMSColorRenderbufferID = 0;
988 glRTDesc.fRTFBOID = 0;
989 glRTDesc.fTexFBOID = 0;
bsalomon@google.com72830222013-01-23 20:25:22 +0000990 glRTDesc.fIsWrapped = false;
bsalomon@google.com64c4fe42011-11-05 14:51:01 +0000991 glRTDesc.fConfig = glTexDesc.fConfig;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000992
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000993 bool renderTarget = 0 != (desc.fFlags & kRenderTarget_GrTextureFlagBit);
reed@google.comac10a2d2010-12-22 21:39:39 +0000994
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000995 const Caps& caps = this->getCaps();
996
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000997 glTexDesc.fOrigin = resolve_origin(desc.fOrigin, renderTarget);
998 glRTDesc.fOrigin = glTexDesc.fOrigin;
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000999
bsalomon@google.com78d6cf92012-01-30 18:09:31 +00001000 glRTDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001001 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType() &&
bsalomon@google.com78d6cf92012-01-30 18:09:31 +00001002 desc.fSampleCnt) {
bsalomon@google.com945bbe12012-06-15 14:30:34 +00001003 //GrPrintf("MSAA RT requested but not supported on this platform.");
1004 return return_null_texture();
reed@google.comac10a2d2010-12-22 21:39:39 +00001005 }
1006
reed@google.comac10a2d2010-12-22 21:39:39 +00001007 if (renderTarget) {
bsalomon@google.comf6601872012-08-28 21:11:35 +00001008 if (glTexDesc.fWidth > caps.maxRenderTargetSize() ||
1009 glTexDesc.fHeight > caps.maxRenderTargetSize()) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001010 return return_null_texture();
1011 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001012 }
1013
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001014 GL_CALL(GenTextures(1, &glTexDesc.fTextureID));
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001015 if (renderTarget && this->glCaps().textureUsageSupport()) {
bsalomon@google.com07dd2bf2011-12-09 19:40:36 +00001016 // provides a hint about how this texture will be used
1017 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1018 GR_GL_TEXTURE_USAGE,
1019 GR_GL_FRAMEBUFFER_ATTACHMENT));
1020 }
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001021 if (!glTexDesc.fTextureID) {
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001022 return return_null_texture();
1023 }
1024
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001025 this->setSpareTextureUnit();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001026 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTexDesc.fTextureID));
bsalomon@google.come269f212011-11-07 13:29:52 +00001027
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001028 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
1029 // drivers have a bug where an FBO won't be complete if it includes a
1030 // texture that is not mipmap complete (considering the filter in use).
1031 GrGLTexture::TexParams initialTexParams;
1032 // we only set a subset here so invalidate first
1033 initialTexParams.invalidate();
1034 initialTexParams.fFilter = GR_GL_NEAREST;
1035 initialTexParams.fWrapS = GR_GL_CLAMP_TO_EDGE;
1036 initialTexParams.fWrapT = GR_GL_CLAMP_TO_EDGE;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001037 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1038 GR_GL_TEXTURE_MAG_FILTER,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001039 initialTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001040 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1041 GR_GL_TEXTURE_MIN_FILTER,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001042 initialTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001043 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1044 GR_GL_TEXTURE_WRAP_S,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001045 initialTexParams.fWrapS));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001046 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1047 GR_GL_TEXTURE_WRAP_T,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001048 initialTexParams.fWrapT));
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001049 if (!this->uploadTexData(glTexDesc, true, 0, 0,
1050 glTexDesc.fWidth, glTexDesc.fHeight,
1051 desc.fConfig, srcData, rowBytes)) {
1052 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
1053 return return_null_texture();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001054 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001055
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001056 GrGLTexture* tex;
reed@google.comac10a2d2010-12-22 21:39:39 +00001057 if (renderTarget) {
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001058 // unbind the texture from the texture unit before binding it to the frame buffer
1059 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, 0));
1060
bsalomon@google.com99621082011-11-15 16:47:16 +00001061 if (!this->createRenderTargetObjects(glTexDesc.fWidth,
1062 glTexDesc.fHeight,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001063 glTexDesc.fTextureID,
1064 &glRTDesc)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001065 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001066 return return_null_texture();
1067 }
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001068 tex = SkNEW_ARGS(GrGLTexture, (this, glTexDesc, glRTDesc));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001069 } else {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001070 tex = SkNEW_ARGS(GrGLTexture, (this, glTexDesc));
reed@google.comac10a2d2010-12-22 21:39:39 +00001071 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001072 tex->setCachedTexParams(initialTexParams, this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00001073#ifdef TRACE_TEXTURE_CREATION
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001074 GrPrintf("--- new texture [%d] size=(%d %d) config=%d\n",
1075 glTexDesc.fTextureID, desc.fWidth, desc.fHeight, desc.fConfig);
reed@google.comac10a2d2010-12-22 21:39:39 +00001076#endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001077 return tex;
1078}
1079
1080namespace {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001081
1082const GrGLuint kUnknownBitCount = GrGLStencilBuffer::kUnknownBitCount;
1083
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001084void inline get_stencil_rb_sizes(const GrGLInterface* gl,
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001085 GrGLuint rb,
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001086 GrGLStencilBuffer::Format* format) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001087 // we shouldn't ever know one size and not the other
1088 GrAssert((kUnknownBitCount == format->fStencilBits) ==
1089 (kUnknownBitCount == format->fTotalBits));
1090 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001091 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001092 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1093 (GrGLint*)&format->fStencilBits);
1094 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001095 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001096 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1097 (GrGLint*)&format->fTotalBits);
1098 format->fTotalBits += format->fStencilBits;
1099 } else {
1100 format->fTotalBits = format->fStencilBits;
1101 }
1102 }
1103}
1104}
1105
1106bool GrGpuGL::createStencilBufferForRenderTarget(GrRenderTarget* rt,
1107 int width, int height) {
1108
1109 // All internally created RTs are also textures. We don't create
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +00001110 // 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 +00001111 GrAssert(rt->asTexture());
bsalomon@google.com99621082011-11-15 16:47:16 +00001112 GrAssert(width >= rt->width());
1113 GrAssert(height >= rt->height());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001114
1115 int samples = rt->numSamples();
1116 GrGLuint sbID;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001117 GL_CALL(GenRenderbuffers(1, &sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001118 if (!sbID) {
1119 return false;
1120 }
1121
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001122 int stencilFmtCnt = this->glCaps().stencilFormats().count();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001123 for (int i = 0; i < stencilFmtCnt; ++i) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001124 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001125 // we start with the last stencil format that succeeded in hopes
1126 // that we won't go through this loop more than once after the
1127 // first (painful) stencil creation.
1128 int sIdx = (i + fLastSuccessfulStencilFmtIdx) % stencilFmtCnt;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001129 const GrGLCaps::StencilFormat& sFmt =
1130 this->glCaps().stencilFormats()[sIdx];
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001131 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001132 // we do this "if" so that we don't call the multisample
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001133 // version on a GL that doesn't have an MSAA extension.
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001134 bool created;
1135 if (samples > 0) {
robertphillips@google.com6177e692013-02-28 20:16:25 +00001136 created = renderbuffer_storage_msaa(fGLContext,
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001137 samples,
1138 sFmt.fInternalFormat,
1139 width, height);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001140 } else {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001141 GL_ALLOC_CALL(this->glInterface(),
1142 RenderbufferStorage(GR_GL_RENDERBUFFER,
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001143 sFmt.fInternalFormat,
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001144 width, height));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001145 created =
1146 (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(this->glInterface()));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001147 }
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001148 if (created) {
1149 // After sized formats we attempt an unsized format and take
1150 // whatever sizes GL gives us. In that case we query for the size.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001151 GrGLStencilBuffer::Format format = sFmt;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001152 get_stencil_rb_sizes(this->glInterface(), sbID, &format);
bsalomon@google.com72830222013-01-23 20:25:22 +00001153 static const bool kIsWrapped = false;
robertphillips@google.comf2e93fc2012-09-05 19:44:18 +00001154 SkAutoTUnref<GrStencilBuffer> sb(SkNEW_ARGS(GrGLStencilBuffer,
bsalomon@google.com72830222013-01-23 20:25:22 +00001155 (this, kIsWrapped, sbID, width, height,
robertphillips@google.comf2e93fc2012-09-05 19:44:18 +00001156 samples, format)));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001157 if (this->attachStencilBufferToRenderTarget(sb, rt)) {
1158 fLastSuccessfulStencilFmtIdx = sIdx;
robertphillips@google.com9fbcad02012-09-09 14:44:15 +00001159 sb->transferToCache();
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001160 rt->setStencilBuffer(sb);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001161 return true;
1162 }
1163 sb->abandon(); // otherwise we lose sbID
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001164 }
1165 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001166 GL_CALL(DeleteRenderbuffers(1, &sbID));
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001167 return false;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001168}
1169
1170bool GrGpuGL::attachStencilBufferToRenderTarget(GrStencilBuffer* sb,
1171 GrRenderTarget* rt) {
1172 GrGLRenderTarget* glrt = (GrGLRenderTarget*) rt;
1173
1174 GrGLuint fbo = glrt->renderFBOID();
1175
1176 if (NULL == sb) {
1177 if (NULL != rt->getStencilBuffer()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001178 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001179 GR_GL_STENCIL_ATTACHMENT,
1180 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001181 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001182 GR_GL_DEPTH_ATTACHMENT,
1183 GR_GL_RENDERBUFFER, 0));
1184#if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001185 GrGLenum status;
1186 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001187 GrAssert(GR_GL_FRAMEBUFFER_COMPLETE == status);
1188#endif
1189 }
1190 return true;
1191 } else {
1192 GrGLStencilBuffer* glsb = (GrGLStencilBuffer*) sb;
1193 GrGLuint rb = glsb->renderbufferID();
1194
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001195 fHWBoundRenderTarget = NULL;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001196 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, fbo));
1197 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001198 GR_GL_STENCIL_ATTACHMENT,
1199 GR_GL_RENDERBUFFER, rb));
1200 if (glsb->format().fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001201 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001202 GR_GL_DEPTH_ATTACHMENT,
1203 GR_GL_RENDERBUFFER, rb));
1204 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001205 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001206 GR_GL_DEPTH_ATTACHMENT,
1207 GR_GL_RENDERBUFFER, 0));
reed@google.comac10a2d2010-12-22 21:39:39 +00001208 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001209
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001210 GrGLenum status;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001211 if (!this->glCaps().isColorConfigAndStencilFormatVerified(rt->config(),
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001212 glsb->format())) {
1213 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1214 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001215 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001216 GR_GL_STENCIL_ATTACHMENT,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001217 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001218 if (glsb->format().fPacked) {
1219 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1220 GR_GL_DEPTH_ATTACHMENT,
1221 GR_GL_RENDERBUFFER, 0));
1222 }
1223 return false;
1224 } else {
robertphillips@google.com6177e692013-02-28 20:16:25 +00001225 fGLContext.info().caps().markColorConfigAndStencilFormatAsVerified(
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001226 rt->config(),
1227 glsb->format());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001228 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001229 }
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001230 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001231 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001232}
1233
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001234////////////////////////////////////////////////////////////////////////////////
1235
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001236GrVertexBuffer* GrGpuGL::onCreateVertexBuffer(uint32_t size, bool dynamic) {
bsalomon@google.come49ad452013-02-20 19:33:20 +00001237 GrGLVertexBuffer::Desc desc;
1238 desc.fDynamic = dynamic;
1239 desc.fSizeInBytes = size;
1240 desc.fIsWrapped = false;
1241
bsalomon@google.com96966a52013-02-21 16:34:21 +00001242 if (this->glCaps().useNonVBOVertexAndIndexDynamicData() && desc.fDynamic) {
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001243 desc.fID = 0;
bsalomon@google.come49ad452013-02-20 19:33:20 +00001244 GrGLVertexBuffer* vertexBuffer = SkNEW_ARGS(GrGLVertexBuffer, (this, desc));
reed@google.comac10a2d2010-12-22 21:39:39 +00001245 return vertexBuffer;
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001246 } else {
1247 GL_CALL(GenBuffers(1, &desc.fID));
1248 if (desc.fID) {
1249 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, desc.fID));
1250 fHWGeometryState.setVertexBufferID(desc.fID);
1251 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1252 // make sure driver can allocate memory for this buffer
1253 GL_ALLOC_CALL(this->glInterface(),
1254 BufferData(GR_GL_ARRAY_BUFFER,
1255 desc.fSizeInBytes,
1256 NULL, // data ptr
1257 desc.fDynamic ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW));
1258 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
1259 GL_CALL(DeleteBuffers(1, &desc.fID));
1260 // deleting bound buffer does implicit bind to 0
1261 fHWGeometryState.setVertexBufferID(0);
1262 return NULL;
1263 }
1264 GrGLVertexBuffer* vertexBuffer = SkNEW_ARGS(GrGLVertexBuffer, (this, desc));
1265 return vertexBuffer;
1266 }
1267 return NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +00001268 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001269}
1270
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001271GrIndexBuffer* GrGpuGL::onCreateIndexBuffer(uint32_t size, bool dynamic) {
bsalomon@google.come49ad452013-02-20 19:33:20 +00001272 GrGLIndexBuffer::Desc desc;
1273 desc.fDynamic = dynamic;
1274 desc.fSizeInBytes = size;
1275 desc.fIsWrapped = false;
1276
bsalomon@google.com96966a52013-02-21 16:34:21 +00001277 if (this->glCaps().useNonVBOVertexAndIndexDynamicData() && desc.fDynamic) {
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001278 desc.fID = 0;
bsalomon@google.come49ad452013-02-20 19:33:20 +00001279 GrIndexBuffer* indexBuffer = SkNEW_ARGS(GrGLIndexBuffer, (this, desc));
reed@google.comac10a2d2010-12-22 21:39:39 +00001280 return indexBuffer;
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001281 } else {
1282 GL_CALL(GenBuffers(1, &desc.fID));
1283 if (desc.fID) {
1284 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, desc.fID));
1285 fHWGeometryState.setIndexBufferID(desc.fID);
1286 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1287 // make sure driver can allocate memory for this buffer
1288 GL_ALLOC_CALL(this->glInterface(),
1289 BufferData(GR_GL_ELEMENT_ARRAY_BUFFER,
1290 desc.fSizeInBytes,
1291 NULL, // data ptr
1292 desc.fDynamic ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW));
1293 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
1294 GL_CALL(DeleteBuffers(1, &desc.fID));
1295 // deleting bound buffer does implicit bind to 0
1296 fHWGeometryState.setIndexBufferID(0);
1297 return NULL;
1298 }
1299 GrIndexBuffer* indexBuffer = SkNEW_ARGS(GrGLIndexBuffer, (this, desc));
1300 return indexBuffer;
1301 }
1302 return NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +00001303 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001304}
1305
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00001306GrPath* GrGpuGL::onCreatePath(const SkPath& inPath) {
bsalomon@google.comf6601872012-08-28 21:11:35 +00001307 GrAssert(fCaps.pathStencilingSupport());
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001308 return SkNEW_ARGS(GrGLPath, (this, inPath));
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00001309}
1310
bsalomon@google.coma3201942012-06-21 19:58:20 +00001311void GrGpuGL::flushScissor() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001312 const GrDrawState& drawState = this->getDrawState();
1313 const GrGLRenderTarget* rt =
1314 static_cast<const GrGLRenderTarget*>(drawState.getRenderTarget());
1315
1316 GrAssert(NULL != rt);
1317 const GrGLIRect& vp = rt->getViewport();
reed@google.comac10a2d2010-12-22 21:39:39 +00001318
bsalomon@google.coma3201942012-06-21 19:58:20 +00001319 if (fScissorState.fEnabled) {
1320 GrGLIRect scissor;
1321 scissor.setRelativeTo(vp,
1322 fScissorState.fRect.fLeft,
1323 fScissorState.fRect.fTop,
1324 fScissorState.fRect.width(),
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001325 fScissorState.fRect.height(),
1326 rt->origin());
bsalomon@google.coma3201942012-06-21 19:58:20 +00001327 // if the scissor fully contains the viewport then we fall through and
1328 // disable the scissor test.
1329 if (!scissor.contains(vp)) {
1330 if (fHWScissorSettings.fRect != scissor) {
1331 scissor.pushToGLScissor(this->glInterface());
1332 fHWScissorSettings.fRect = scissor;
1333 }
1334 if (kYes_TriState != fHWScissorSettings.fEnabled) {
1335 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1336 fHWScissorSettings.fEnabled = kYes_TriState;
1337 }
1338 return;
1339 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001340 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001341 if (kNo_TriState != fHWScissorSettings.fEnabled) {
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001342 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.coma3201942012-06-21 19:58:20 +00001343 fHWScissorSettings.fEnabled = kNo_TriState;
1344 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001345 }
1346}
1347
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001348void GrGpuGL::onClear(const GrIRect* rect, GrColor color) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001349 const GrDrawState& drawState = this->getDrawState();
1350 const GrRenderTarget* rt = drawState.getRenderTarget();
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001351 // parent class should never let us get here with no RT
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001352 GrAssert(NULL != rt);
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001353
bsalomon@google.com74b98712011-11-11 19:46:16 +00001354 GrIRect clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001355 if (NULL != rect) {
1356 // flushScissor expects rect to be clipped to the target.
bsalomon@google.com74b98712011-11-11 19:46:16 +00001357 clippedRect = *rect;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001358 GrIRect rtRect = SkIRect::MakeWH(rt->width(), rt->height());
bsalomon@google.com74b98712011-11-11 19:46:16 +00001359 if (clippedRect.intersect(rtRect)) {
1360 rect = &clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001361 } else {
1362 return;
1363 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001364 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001365 this->flushRenderTarget(rect);
bsalomon@google.coma3201942012-06-21 19:58:20 +00001366 GrAutoTRestore<ScissorState> asr(&fScissorState);
1367 fScissorState.fEnabled = (NULL != rect);
1368 if (fScissorState.fEnabled) {
1369 fScissorState.fRect = *rect;
1370 }
1371 this->flushScissor();
bsalomon@google.com74b98712011-11-11 19:46:16 +00001372
1373 GrGLfloat r, g, b, a;
1374 static const GrGLfloat scale255 = 1.f / 255.f;
1375 a = GrColorUnpackA(color) * scale255;
1376 GrGLfloat scaleRGB = scale255;
bsalomon@google.com74b98712011-11-11 19:46:16 +00001377 r = GrColorUnpackR(color) * scaleRGB;
1378 g = GrColorUnpackG(color) * scaleRGB;
1379 b = GrColorUnpackB(color) * scaleRGB;
1380
1381 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00001382 fHWWriteToColor = kYes_TriState;
bsalomon@google.com74b98712011-11-11 19:46:16 +00001383 GL_CALL(ClearColor(r, g, b, a));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001384 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001385}
1386
bsalomon@google.comedc177d2011-08-05 15:46:40 +00001387void GrGpuGL::clearStencil() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001388 if (NULL == this->getDrawState().getRenderTarget()) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001389 return;
1390 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001391
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001392 this->flushRenderTarget(&GrIRect::EmptyIRect());
1393
bsalomon@google.coma3201942012-06-21 19:58:20 +00001394 GrAutoTRestore<ScissorState> asr(&fScissorState);
1395 fScissorState.fEnabled = false;
1396 this->flushScissor();
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001397
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001398 GL_CALL(StencilMask(0xffffffff));
1399 GL_CALL(ClearStencil(0));
1400 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001401 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001402}
1403
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001404void GrGpuGL::clearStencilClip(const GrIRect& rect, bool insideClip) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001405 const GrDrawState& drawState = this->getDrawState();
1406 const GrRenderTarget* rt = drawState.getRenderTarget();
1407 GrAssert(NULL != rt);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001408
1409 // this should only be called internally when we know we have a
1410 // stencil buffer.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001411 GrAssert(NULL != rt->getStencilBuffer());
1412 GrGLint stencilBitCount = rt->getStencilBuffer()->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001413#if 0
reed@google.comac10a2d2010-12-22 21:39:39 +00001414 GrAssert(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00001415 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001416#else
1417 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001418 // ANGLE a partial stencil mask will cause clears to be
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001419 // turned into draws. Our contract on GrDrawTarget says that
1420 // changing the clip between stencil passes may or may not
1421 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00001422 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001423#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001424 GrGLint value;
1425 if (insideClip) {
1426 value = (1 << (stencilBitCount - 1));
1427 } else {
1428 value = 0;
1429 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001430 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.coma3201942012-06-21 19:58:20 +00001431
1432 GrAutoTRestore<ScissorState> asr(&fScissorState);
1433 fScissorState.fEnabled = true;
1434 fScissorState.fRect = rect;
1435 this->flushScissor();
1436
caryclark@google.comcf6285b2012-06-06 12:09:01 +00001437 GL_CALL(StencilMask((uint32_t) clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001438 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001439 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001440 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001441}
1442
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001443void GrGpuGL::onForceRenderTargetFlush() {
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001444 this->flushRenderTarget(&GrIRect::EmptyIRect());
reed@google.comac10a2d2010-12-22 21:39:39 +00001445}
1446
bsalomon@google.comc4364992011-11-07 15:54:49 +00001447bool GrGpuGL::readPixelsWillPayForYFlip(GrRenderTarget* renderTarget,
1448 int left, int top,
1449 int width, int height,
1450 GrPixelConfig config,
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001451 size_t rowBytes) const {
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001452 // If this rendertarget is aready TopLeft, we don't need to flip.
1453 if (kTopLeft_GrSurfaceOrigin == renderTarget->origin()) {
1454 return false;
1455 }
1456
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001457 // if GL can do the flip then we'll never pay for it.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001458 if (this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001459 return false;
1460 }
1461
1462 // If we have to do memcpy to handle non-trim rowBytes then we
bsalomon@google.com7107fa72011-11-10 14:54:14 +00001463 // get the flip for free. Otherwise it costs.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001464 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001465 return true;
1466 }
1467 // If we have to do memcpys to handle rowBytes then y-flip is free
1468 // Note the rowBytes might be tight to the passed in data, but if data
1469 // gets clipped in x to the target the rowBytes will no longer be tight.
1470 if (left >= 0 && (left + width) < renderTarget->width()) {
1471 return 0 == rowBytes ||
1472 GrBytesPerPixel(config) * width == rowBytes;
1473 } else {
1474 return false;
1475 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001476}
1477
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001478bool GrGpuGL::onReadPixels(GrRenderTarget* target,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001479 int left, int top,
1480 int width, int height,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001481 GrPixelConfig config,
1482 void* buffer,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001483 size_t rowBytes) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001484 GrGLenum format;
1485 GrGLenum type;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001486 bool flipY = kBottomLeft_GrSurfaceOrigin == target->origin();
bsalomon@google.com280e99f2012-01-05 16:17:38 +00001487 if (!this->configToGLFormats(config, false, NULL, &format, &type)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001488 return false;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001489 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001490 size_t bpp = GrBytesPerPixel(config);
1491 if (!adjust_pixel_ops_params(target->width(), target->height(), bpp,
1492 &left, &top, &width, &height,
1493 const_cast<const void**>(&buffer),
1494 &rowBytes)) {
1495 return false;
1496 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001497
bsalomon@google.comc6980972011-11-02 19:57:21 +00001498 // resolve the render target if necessary
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001499 GrGLRenderTarget* tgt = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001500 GrDrawState::AutoRenderTargetRestore artr;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001501 switch (tgt->getResolveType()) {
1502 case GrGLRenderTarget::kCantResolve_ResolveType:
1503 return false;
1504 case GrGLRenderTarget::kAutoResolves_ResolveType:
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001505 artr.set(this->drawState(), target);
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001506 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001507 break;
1508 case GrGLRenderTarget::kCanResolve_ResolveType:
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001509 this->onResolveRenderTarget(tgt);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001510 // we don't track the state of the READ FBO ID.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001511 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1512 tgt->textureFBOID()));
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001513 break;
1514 default:
1515 GrCrash("Unknown resolve type");
reed@google.comac10a2d2010-12-22 21:39:39 +00001516 }
1517
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001518 const GrGLIRect& glvp = tgt->getViewport();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001519
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001520 // the read rect is viewport-relative
1521 GrGLIRect readRect;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001522 readRect.setRelativeTo(glvp, left, top, width, height, target->origin());
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001523
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001524 size_t tightRowBytes = bpp * width;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001525 if (0 == rowBytes) {
1526 rowBytes = tightRowBytes;
1527 }
1528 size_t readDstRowBytes = tightRowBytes;
1529 void* readDst = buffer;
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001530
bsalomon@google.comc6980972011-11-02 19:57:21 +00001531 // determine if GL can read using the passed rowBytes or if we need
1532 // a scratch buffer.
1533 SkAutoSMalloc<32 * sizeof(GrColor)> scratch;
1534 if (rowBytes != tightRowBytes) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001535 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.comc6980972011-11-02 19:57:21 +00001536 GrAssert(!(rowBytes % sizeof(GrColor)));
1537 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, rowBytes / sizeof(GrColor)));
1538 readDstRowBytes = rowBytes;
1539 } else {
1540 scratch.reset(tightRowBytes * height);
1541 readDst = scratch.get();
1542 }
1543 }
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001544 if (flipY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001545 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 1));
1546 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001547 GL_CALL(ReadPixels(readRect.fLeft, readRect.fBottom,
1548 readRect.fWidth, readRect.fHeight,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001549 format, type, readDst));
1550 if (readDstRowBytes != tightRowBytes) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001551 GrAssert(this->glCaps().packRowLengthSupport());
bsalomon@google.comc6980972011-11-02 19:57:21 +00001552 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
1553 }
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001554 if (flipY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001555 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 0));
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001556 flipY = false;
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001557 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001558
1559 // now reverse the order of the rows, since GL's are bottom-to-top, but our
bsalomon@google.comc6980972011-11-02 19:57:21 +00001560 // API presents top-to-bottom. We must preserve the padding contents. Note
1561 // that the above readPixels did not overwrite the padding.
1562 if (readDst == buffer) {
1563 GrAssert(rowBytes == readDstRowBytes);
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001564 if (flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001565 scratch.reset(tightRowBytes);
1566 void* tmpRow = scratch.get();
1567 // flip y in-place by rows
1568 const int halfY = height >> 1;
1569 char* top = reinterpret_cast<char*>(buffer);
1570 char* bottom = top + (height - 1) * rowBytes;
1571 for (int y = 0; y < halfY; y++) {
1572 memcpy(tmpRow, top, tightRowBytes);
1573 memcpy(top, bottom, tightRowBytes);
1574 memcpy(bottom, tmpRow, tightRowBytes);
1575 top += rowBytes;
1576 bottom -= rowBytes;
1577 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001578 }
1579 } else {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001580 GrAssert(readDst != buffer); GrAssert(rowBytes != tightRowBytes);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001581 // copy from readDst to buffer while flipping y
caryclark@google.comcf6285b2012-06-06 12:09:01 +00001582 // const int halfY = height >> 1;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001583 const char* src = reinterpret_cast<const char*>(readDst);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001584 char* dst = reinterpret_cast<char*>(buffer);
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001585 if (flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001586 dst += (height-1) * rowBytes;
1587 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001588 for (int y = 0; y < height; y++) {
1589 memcpy(dst, src, tightRowBytes);
1590 src += readDstRowBytes;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001591 if (!flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001592 dst += rowBytes;
1593 } else {
1594 dst -= rowBytes;
1595 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001596 }
1597 }
1598 return true;
1599}
1600
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001601void GrGpuGL::flushRenderTarget(const GrIRect* bound) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001602
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001603 GrGLRenderTarget* rt =
1604 static_cast<GrGLRenderTarget*>(this->drawState()->getRenderTarget());
1605 GrAssert(NULL != rt);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001606
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001607 if (fHWBoundRenderTarget != rt) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001608 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, rt->renderFBOID()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001609 #if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001610 GrGLenum status;
1611 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001612 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
reed@google.comb9255d52011-06-13 18:54:59 +00001613 GrPrintf("GrGpuGL::flushRenderTarget glCheckFramebufferStatus %x\n", status);
reed@google.comac10a2d2010-12-22 21:39:39 +00001614 }
1615 #endif
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001616 fHWBoundRenderTarget = rt;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001617 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.coma3201942012-06-21 19:58:20 +00001618 if (fHWViewport != vp) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001619 vp.pushToGLViewport(this->glInterface());
bsalomon@google.coma3201942012-06-21 19:58:20 +00001620 fHWViewport = vp;
reed@google.comac10a2d2010-12-22 21:39:39 +00001621 }
1622 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001623 if (NULL == bound || !bound->isEmpty()) {
1624 rt->flagAsNeedingResolve(bound);
1625 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001626}
1627
twiz@google.com0f31ca72011-03-18 17:38:11 +00001628GrGLenum gPrimitiveType2GLMode[] = {
1629 GR_GL_TRIANGLES,
1630 GR_GL_TRIANGLE_STRIP,
1631 GR_GL_TRIANGLE_FAN,
1632 GR_GL_POINTS,
1633 GR_GL_LINES,
1634 GR_GL_LINE_STRIP
reed@google.comac10a2d2010-12-22 21:39:39 +00001635};
1636
bsalomon@google.comd302f142011-03-03 13:54:13 +00001637#define SWAP_PER_DRAW 0
1638
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001639#if SWAP_PER_DRAW
bsalomon@google.comd302f142011-03-03 13:54:13 +00001640 #if GR_MAC_BUILD
1641 #include <AGL/agl.h>
1642 #elif GR_WIN32_BUILD
bsalomon@google.comce7357d2012-06-25 17:49:25 +00001643 #include <gl/GL.h>
bsalomon@google.comd302f142011-03-03 13:54:13 +00001644 void SwapBuf() {
1645 DWORD procID = GetCurrentProcessId();
1646 HWND hwnd = GetTopWindow(GetDesktopWindow());
1647 while(hwnd) {
1648 DWORD wndProcID = 0;
1649 GetWindowThreadProcessId(hwnd, &wndProcID);
1650 if(wndProcID == procID) {
1651 SwapBuffers(GetDC(hwnd));
1652 }
1653 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
1654 }
1655 }
1656 #endif
1657#endif
1658
bsalomon@google.com74749cd2013-01-30 16:12:41 +00001659void GrGpuGL::onGpuDraw(const DrawInfo& info) {
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00001660 size_t indexOffsetInBytes;
1661 this->setupGeometry(info, &indexOffsetInBytes);
reed@google.comac10a2d2010-12-22 21:39:39 +00001662
bsalomon@google.com74749cd2013-01-30 16:12:41 +00001663 GrAssert((size_t)info.primitiveType() < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001664
bsalomon@google.com74749cd2013-01-30 16:12:41 +00001665 if (info.isIndexed()) {
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00001666 GrGLvoid* indices =
1667 reinterpret_cast<GrGLvoid*>(indexOffsetInBytes + sizeof(uint16_t) * info.startIndex());
bsalomon@google.com74749cd2013-01-30 16:12:41 +00001668 // info.startVertex() was accounted for by setupGeometry.
1669 GL_CALL(DrawElements(gPrimitiveType2GLMode[info.primitiveType()],
1670 info.indexCount(),
1671 GR_GL_UNSIGNED_SHORT,
1672 indices));
1673 } else {
1674 // Pass 0 for parameter first. We have to adjust glVertexAttribPointer() to account for
1675 // startVertex in the DrawElements case. So we always rely on setupGeometry to have
1676 // accounted for startVertex.
1677 GL_CALL(DrawArrays(gPrimitiveType2GLMode[info.primitiveType()], 0, info.vertexCount()));
1678 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00001679#if SWAP_PER_DRAW
1680 glFlush();
1681 #if GR_MAC_BUILD
1682 aglSwapBuffers(aglGetCurrentContext());
1683 int set_a_break_pt_here = 9;
1684 aglSwapBuffers(aglGetCurrentContext());
1685 #elif GR_WIN32_BUILD
1686 SwapBuf();
1687 int set_a_break_pt_here = 9;
1688 SwapBuf();
1689 #endif
1690#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001691}
1692
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001693namespace {
bsalomon@google.com100abf42012-09-05 17:40:04 +00001694
1695static const uint16_t kOnes16 = static_cast<uint16_t>(~0);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001696const GrStencilSettings& winding_nv_path_stencil_settings() {
1697 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
1698 kIncClamp_StencilOp,
1699 kIncClamp_StencilOp,
1700 kAlwaysIfInClip_StencilFunc,
bsalomon@google.com100abf42012-09-05 17:40:04 +00001701 kOnes16, kOnes16, kOnes16);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001702 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
1703}
1704const GrStencilSettings& even_odd_nv_path_stencil_settings() {
1705 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
1706 kInvert_StencilOp,
1707 kInvert_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}
1712}
1713
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001714void GrGpuGL::setStencilPathSettings(const GrPath&,
sugoi@google.com12b4e272012-12-06 20:13:11 +00001715 SkPath::FillType fill,
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001716 GrStencilSettings* settings) {
1717 switch (fill) {
sugoi@google.com12b4e272012-12-06 20:13:11 +00001718 case SkPath::kEvenOdd_FillType:
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001719 *settings = even_odd_nv_path_stencil_settings();
1720 return;
sugoi@google.com12b4e272012-12-06 20:13:11 +00001721 case SkPath::kWinding_FillType:
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001722 *settings = winding_nv_path_stencil_settings();
1723 return;
1724 default:
1725 GrCrash("Unexpected path fill.");
1726 }
1727}
1728
sugoi@google.com12b4e272012-12-06 20:13:11 +00001729void GrGpuGL::onGpuStencilPath(const GrPath* path, SkPath::FillType fill) {
bsalomon@google.comf6601872012-08-28 21:11:35 +00001730 GrAssert(fCaps.pathStencilingSupport());
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001731
1732 GrGLuint id = static_cast<const GrGLPath*>(path)->pathID();
1733 GrDrawState* drawState = this->drawState();
1734 GrAssert(NULL != drawState->getRenderTarget());
1735 if (NULL == drawState->getRenderTarget()->getStencilBuffer()) {
1736 return;
1737 }
1738
1739 // Decide how to manipulate the stencil buffer based on the fill rule.
1740 // Also, assert that the stencil settings we set in setStencilPathSettings
1741 // are present.
1742 GrAssert(!fStencilSettings.isTwoSided());
1743 GrGLenum fillMode;
1744 switch (fill) {
sugoi@google.com12b4e272012-12-06 20:13:11 +00001745 case SkPath::kWinding_FillType:
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001746 fillMode = GR_GL_COUNT_UP;
1747 GrAssert(kIncClamp_StencilOp ==
1748 fStencilSettings.passOp(GrStencilSettings::kFront_Face));
1749 GrAssert(kIncClamp_StencilOp ==
1750 fStencilSettings.failOp(GrStencilSettings::kFront_Face));
1751 break;
sugoi@google.com12b4e272012-12-06 20:13:11 +00001752 case SkPath::kEvenOdd_FillType:
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001753 fillMode = GR_GL_INVERT;
1754 GrAssert(kInvert_StencilOp ==
1755 fStencilSettings.passOp(GrStencilSettings::kFront_Face));
1756 GrAssert(kInvert_StencilOp ==
1757 fStencilSettings.failOp(GrStencilSettings::kFront_Face));
1758 break;
1759 default:
1760 // Only the above two fill rules are allowed.
1761 GrCrash("Unexpected path fill.");
bsalomon@google.com548a4332012-07-11 19:45:22 +00001762 return; // suppress unused var warning.
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001763 }
1764 GrGLint writeMask = fStencilSettings.writeMask(GrStencilSettings::kFront_Face);
1765 GL_CALL(StencilFillPath(id, fillMode, writeMask));
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00001766}
1767
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001768void GrGpuGL::onResolveRenderTarget(GrRenderTarget* target) {
1769
1770 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(target);
reed@google.comac10a2d2010-12-22 21:39:39 +00001771
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001772 if (rt->needsResolve()) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001773 GrAssert(GrGLCaps::kNone_MSFBOType != this->glCaps().msFBOType());
reed@google.comac10a2d2010-12-22 21:39:39 +00001774 GrAssert(rt->textureFBOID() != rt->renderFBOID());
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001775 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1776 rt->renderFBOID()));
1777 GL_CALL(BindFramebuffer(GR_GL_DRAW_FRAMEBUFFER,
1778 rt->textureFBOID()));
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001779 // make sure we go through flushRenderTarget() since we've modified
1780 // the bound DRAW FBO ID.
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001781 fHWBoundRenderTarget = NULL;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001782 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001783 const GrIRect dirtyRect = rt->getResolveRect();
1784 GrGLIRect r;
bsalomon@google.coma3201942012-06-21 19:58:20 +00001785 r.setRelativeTo(vp, dirtyRect.fLeft, dirtyRect.fTop,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001786 dirtyRect.width(), dirtyRect.height(), target->origin());
reed@google.comac10a2d2010-12-22 21:39:39 +00001787
bsalomon@google.coma3201942012-06-21 19:58:20 +00001788 GrAutoTRestore<ScissorState> asr;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001789 if (GrGLCaps::kAppleES_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001790 // Apple's extension uses the scissor as the blit bounds.
bsalomon@google.coma3201942012-06-21 19:58:20 +00001791 asr.reset(&fScissorState);
1792 fScissorState.fEnabled = true;
1793 fScissorState.fRect = dirtyRect;
1794 this->flushScissor();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001795 GL_CALL(ResolveMultisampleFramebuffer());
reed@google.comac10a2d2010-12-22 21:39:39 +00001796 } else {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001797 if (GrGLCaps::kDesktopARB_MSFBOType != this->glCaps().msFBOType()) {
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001798 // this respects the scissor during the blit, so disable it.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001799 GrAssert(GrGLCaps::kDesktopEXT_MSFBOType ==
1800 this->glCaps().msFBOType());
bsalomon@google.coma3201942012-06-21 19:58:20 +00001801 asr.reset(&fScissorState);
1802 fScissorState.fEnabled = false;
1803 this->flushScissor();
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001804 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001805 int right = r.fLeft + r.fWidth;
1806 int top = r.fBottom + r.fHeight;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001807 GL_CALL(BlitFramebuffer(r.fLeft, r.fBottom, right, top,
1808 r.fLeft, r.fBottom, right, top,
1809 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001810 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001811 rt->flagAsResolved();
reed@google.comac10a2d2010-12-22 21:39:39 +00001812 }
1813}
1814
bsalomon@google.com411dad02012-06-05 20:24:20 +00001815namespace {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001816
bsalomon@google.com411dad02012-06-05 20:24:20 +00001817GrGLenum gr_to_gl_stencil_func(GrStencilFunc basicFunc) {
1818 static const GrGLenum gTable[] = {
1819 GR_GL_ALWAYS, // kAlways_StencilFunc
1820 GR_GL_NEVER, // kNever_StencilFunc
1821 GR_GL_GREATER, // kGreater_StencilFunc
1822 GR_GL_GEQUAL, // kGEqual_StencilFunc
1823 GR_GL_LESS, // kLess_StencilFunc
1824 GR_GL_LEQUAL, // kLEqual_StencilFunc,
1825 GR_GL_EQUAL, // kEqual_StencilFunc,
1826 GR_GL_NOTEQUAL, // kNotEqual_StencilFunc,
1827 };
1828 GR_STATIC_ASSERT(GR_ARRAY_COUNT(gTable) == kBasicStencilFuncCount);
1829 GR_STATIC_ASSERT(0 == kAlways_StencilFunc);
1830 GR_STATIC_ASSERT(1 == kNever_StencilFunc);
1831 GR_STATIC_ASSERT(2 == kGreater_StencilFunc);
1832 GR_STATIC_ASSERT(3 == kGEqual_StencilFunc);
1833 GR_STATIC_ASSERT(4 == kLess_StencilFunc);
1834 GR_STATIC_ASSERT(5 == kLEqual_StencilFunc);
1835 GR_STATIC_ASSERT(6 == kEqual_StencilFunc);
1836 GR_STATIC_ASSERT(7 == kNotEqual_StencilFunc);
1837 GrAssert((unsigned) basicFunc < kBasicStencilFuncCount);
1838
1839 return gTable[basicFunc];
1840}
1841
1842GrGLenum gr_to_gl_stencil_op(GrStencilOp op) {
1843 static const GrGLenum gTable[] = {
1844 GR_GL_KEEP, // kKeep_StencilOp
1845 GR_GL_REPLACE, // kReplace_StencilOp
1846 GR_GL_INCR_WRAP, // kIncWrap_StencilOp
1847 GR_GL_INCR, // kIncClamp_StencilOp
1848 GR_GL_DECR_WRAP, // kDecWrap_StencilOp
1849 GR_GL_DECR, // kDecClamp_StencilOp
1850 GR_GL_ZERO, // kZero_StencilOp
1851 GR_GL_INVERT, // kInvert_StencilOp
1852 };
1853 GR_STATIC_ASSERT(GR_ARRAY_COUNT(gTable) == kStencilOpCount);
1854 GR_STATIC_ASSERT(0 == kKeep_StencilOp);
1855 GR_STATIC_ASSERT(1 == kReplace_StencilOp);
1856 GR_STATIC_ASSERT(2 == kIncWrap_StencilOp);
1857 GR_STATIC_ASSERT(3 == kIncClamp_StencilOp);
1858 GR_STATIC_ASSERT(4 == kDecWrap_StencilOp);
1859 GR_STATIC_ASSERT(5 == kDecClamp_StencilOp);
1860 GR_STATIC_ASSERT(6 == kZero_StencilOp);
1861 GR_STATIC_ASSERT(7 == kInvert_StencilOp);
1862 GrAssert((unsigned) op < kStencilOpCount);
1863 return gTable[op];
1864}
1865
1866void set_gl_stencil(const GrGLInterface* gl,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001867 const GrStencilSettings& settings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00001868 GrGLenum glFace,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001869 GrStencilSettings::Face grFace) {
1870 GrGLenum glFunc = gr_to_gl_stencil_func(settings.func(grFace));
1871 GrGLenum glFailOp = gr_to_gl_stencil_op(settings.failOp(grFace));
1872 GrGLenum glPassOp = gr_to_gl_stencil_op(settings.passOp(grFace));
1873
1874 GrGLint ref = settings.funcRef(grFace);
1875 GrGLint mask = settings.funcMask(grFace);
1876 GrGLint writeMask = settings.writeMask(grFace);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001877
1878 if (GR_GL_FRONT_AND_BACK == glFace) {
1879 // we call the combined func just in case separate stencil is not
1880 // supported.
1881 GR_GL_CALL(gl, StencilFunc(glFunc, ref, mask));
1882 GR_GL_CALL(gl, StencilMask(writeMask));
1883 GR_GL_CALL(gl, StencilOp(glFailOp, glPassOp, glPassOp));
1884 } else {
1885 GR_GL_CALL(gl, StencilFuncSeparate(glFace, glFunc, ref, mask));
1886 GR_GL_CALL(gl, StencilMaskSeparate(glFace, writeMask));
1887 GR_GL_CALL(gl, StencilOpSeparate(glFace, glFailOp, glPassOp, glPassOp));
1888 }
1889}
1890}
bsalomon@google.comd302f142011-03-03 13:54:13 +00001891
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001892void GrGpuGL::flushStencil(DrawType type) {
1893 if (kStencilPath_DrawType == type) {
1894 GrAssert(!fStencilSettings.isTwoSided());
1895 // Just the func, ref, and mask is set here. The op and write mask are params to the call
1896 // that draws the path to the SB (glStencilFillPath)
1897 GrGLenum func =
1898 gr_to_gl_stencil_func(fStencilSettings.func(GrStencilSettings::kFront_Face));
1899 GL_CALL(PathStencilFunc(func,
1900 fStencilSettings.funcRef(GrStencilSettings::kFront_Face),
1901 fStencilSettings.funcMask(GrStencilSettings::kFront_Face)));
1902 } else if (fHWStencilSettings != fStencilSettings) {
1903 if (fStencilSettings.isDisabled()) {
1904 if (kNo_TriState != fHWStencilTestEnabled) {
1905 GL_CALL(Disable(GR_GL_STENCIL_TEST));
1906 fHWStencilTestEnabled = kNo_TriState;
1907 }
1908 } else {
1909 if (kYes_TriState != fHWStencilTestEnabled) {
1910 GL_CALL(Enable(GR_GL_STENCIL_TEST));
1911 fHWStencilTestEnabled = kYes_TriState;
1912 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001913 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001914 if (!fStencilSettings.isDisabled()) {
bsalomon@google.comf6601872012-08-28 21:11:35 +00001915 if (this->getCaps().twoSidedStencilSupport()) {
bsalomon@google.com411dad02012-06-05 20:24:20 +00001916 set_gl_stencil(this->glInterface(),
bsalomon@google.coma3201942012-06-21 19:58:20 +00001917 fStencilSettings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00001918 GR_GL_FRONT,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001919 GrStencilSettings::kFront_Face);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001920 set_gl_stencil(this->glInterface(),
bsalomon@google.coma3201942012-06-21 19:58:20 +00001921 fStencilSettings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00001922 GR_GL_BACK,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001923 GrStencilSettings::kBack_Face);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001924 } else {
bsalomon@google.com411dad02012-06-05 20:24:20 +00001925 set_gl_stencil(this->glInterface(),
bsalomon@google.coma3201942012-06-21 19:58:20 +00001926 fStencilSettings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00001927 GR_GL_FRONT_AND_BACK,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001928 GrStencilSettings::kFront_Face);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001929 }
1930 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001931 fHWStencilSettings = fStencilSettings;
reed@google.comac10a2d2010-12-22 21:39:39 +00001932 }
1933}
1934
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001935void GrGpuGL::flushAAState(DrawType type) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001936 const GrRenderTarget* rt = this->getDrawState().getRenderTarget();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001937 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001938 // ES doesn't support toggling GL_MULTISAMPLE and doesn't have
1939 // smooth lines.
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001940 // we prefer smooth lines over multisampled lines
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00001941 bool smoothLines = false;
1942
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001943 if (kDrawLines_DrawType == type) {
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00001944 smoothLines = this->willUseHWAALines();
1945 if (smoothLines) {
1946 if (kYes_TriState != fHWAAState.fSmoothLineEnabled) {
1947 GL_CALL(Enable(GR_GL_LINE_SMOOTH));
1948 fHWAAState.fSmoothLineEnabled = kYes_TriState;
1949 // must disable msaa to use line smoothing
1950 if (rt->isMultisampled() &&
1951 kNo_TriState != fHWAAState.fMSAAEnabled) {
1952 GL_CALL(Disable(GR_GL_MULTISAMPLE));
1953 fHWAAState.fMSAAEnabled = kNo_TriState;
1954 }
1955 }
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001956 } else {
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00001957 if (kNo_TriState != fHWAAState.fSmoothLineEnabled) {
1958 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
1959 fHWAAState.fSmoothLineEnabled = kNo_TriState;
1960 }
1961 }
1962 }
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001963 if (!smoothLines && rt->isMultisampled()) {
1964 // FIXME: GL_NV_pr doesn't seem to like MSAA disabled. The paths
1965 // convex hulls of each segment appear to get filled.
1966 bool enableMSAA = kStencilPath_DrawType == type ||
1967 this->getDrawState().isHWAntialiasState();
1968 if (enableMSAA) {
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00001969 if (kYes_TriState != fHWAAState.fMSAAEnabled) {
1970 GL_CALL(Enable(GR_GL_MULTISAMPLE));
1971 fHWAAState.fMSAAEnabled = kYes_TriState;
1972 }
1973 } else {
1974 if (kNo_TriState != fHWAAState.fMSAAEnabled) {
1975 GL_CALL(Disable(GR_GL_MULTISAMPLE));
1976 fHWAAState.fMSAAEnabled = kNo_TriState;
1977 }
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001978 }
1979 }
1980 }
1981}
1982
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00001983void GrGpuGL::flushBlend(bool isLines,
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001984 GrBlendCoeff srcCoeff,
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001985 GrBlendCoeff dstCoeff) {
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00001986 if (isLines && this->willUseHWAALines()) {
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001987 if (kYes_TriState != fHWBlendState.fEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001988 GL_CALL(Enable(GR_GL_BLEND));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001989 fHWBlendState.fEnabled = kYes_TriState;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001990 }
bsalomon@google.com47059542012-06-06 20:51:20 +00001991 if (kSA_GrBlendCoeff != fHWBlendState.fSrcCoeff ||
1992 kISA_GrBlendCoeff != fHWBlendState.fDstCoeff) {
1993 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[kSA_GrBlendCoeff],
1994 gXfermodeCoeff2Blend[kISA_GrBlendCoeff]));
1995 fHWBlendState.fSrcCoeff = kSA_GrBlendCoeff;
1996 fHWBlendState.fDstCoeff = kISA_GrBlendCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001997 }
1998 } else {
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001999 // any optimization to disable blending should
2000 // have already been applied and tweaked the coeffs
2001 // to (1, 0).
bsalomon@google.com47059542012-06-06 20:51:20 +00002002 bool blendOff = kOne_GrBlendCoeff == srcCoeff &&
2003 kZero_GrBlendCoeff == dstCoeff;
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002004 if (blendOff) {
2005 if (kNo_TriState != fHWBlendState.fEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002006 GL_CALL(Disable(GR_GL_BLEND));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002007 fHWBlendState.fEnabled = kNo_TriState;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002008 }
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002009 } else {
2010 if (kYes_TriState != fHWBlendState.fEnabled) {
2011 GL_CALL(Enable(GR_GL_BLEND));
2012 fHWBlendState.fEnabled = kYes_TriState;
2013 }
2014 if (fHWBlendState.fSrcCoeff != srcCoeff ||
2015 fHWBlendState.fDstCoeff != dstCoeff) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002016 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
2017 gXfermodeCoeff2Blend[dstCoeff]));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002018 fHWBlendState.fSrcCoeff = srcCoeff;
2019 fHWBlendState.fDstCoeff = dstCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002020 }
bsalomon@google.coma5d056a2012-03-27 15:59:58 +00002021 GrColor blendConst = this->getDrawState().getBlendConstant();
bsalomon@google.com271cffc2011-05-20 14:13:56 +00002022 if ((BlendCoeffReferencesConstant(srcCoeff) ||
2023 BlendCoeffReferencesConstant(dstCoeff)) &&
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002024 (!fHWBlendState.fConstColorValid ||
2025 fHWBlendState.fConstColor != blendConst)) {
bsalomon@google.com75347472012-09-17 17:23:21 +00002026 GrGLfloat c[4];
2027 GrColorToRGBAFloat(blendConst, c);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002028 GL_CALL(BlendColor(c[0], c[1], c[2], c[3]));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002029 fHWBlendState.fConstColor = blendConst;
2030 fHWBlendState.fConstColorValid = true;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002031 }
2032 }
2033 }
2034}
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002035namespace {
2036
bsalomon@google.com6d003d12012-09-11 15:45:20 +00002037inline void set_tex_swizzle(GrGLenum swizzle[4], const GrGLInterface* gl) {
bsalomon@google.com4d063de2012-05-31 17:59:23 +00002038 GR_GL_CALL(gl, TexParameteriv(GR_GL_TEXTURE_2D,
2039 GR_GL_TEXTURE_SWIZZLE_RGBA,
2040 reinterpret_cast<const GrGLint*>(swizzle)));
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002041}
bsalomon@google.comb8670992012-07-25 21:27:09 +00002042
bsalomon@google.com6c76d242012-09-07 16:52:24 +00002043inline GrGLenum tile_to_gl_wrap(SkShader::TileMode tm) {
bsalomon@google.comb8670992012-07-25 21:27:09 +00002044 static const GrGLenum gWrapModes[] = {
2045 GR_GL_CLAMP_TO_EDGE,
2046 GR_GL_REPEAT,
2047 GR_GL_MIRRORED_REPEAT
2048 };
2049 GrAssert((unsigned) tm <= SK_ARRAY_COUNT(gWrapModes));
2050 GR_STATIC_ASSERT(0 == SkShader::kClamp_TileMode);
2051 GR_STATIC_ASSERT(1 == SkShader::kRepeat_TileMode);
2052 GR_STATIC_ASSERT(2 == SkShader::kMirror_TileMode);
2053 return gWrapModes[tm];
2054}
2055
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002056}
2057
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002058void GrGpuGL::bindTexture(int unitIdx, const GrTextureParams& params, GrGLTexture* texture) {
2059 GrAssert(NULL != texture);
tomhudson@google.comd0c1a062012-07-12 17:23:52 +00002060
bsalomon@google.comb8670992012-07-25 21:27:09 +00002061 // If we created a rt/tex and rendered to it without using a texture and now we're texturing
2062 // 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 +00002063 // out of the "last != next" check.
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002064 GrGLRenderTarget* texRT = static_cast<GrGLRenderTarget*>(texture->asRenderTarget());
bsalomon@google.com4c883782012-06-04 19:05:11 +00002065 if (NULL != texRT) {
2066 this->onResolveRenderTarget(texRT);
2067 }
2068
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002069 if (fHWBoundTextures[unitIdx] != texture) {
2070 this->setTextureUnit(unitIdx);
2071 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, texture->textureID()));
2072 fHWBoundTextures[unitIdx] = texture;
bsalomon@google.com4c883782012-06-04 19:05:11 +00002073 }
2074
bsalomon@google.com4c883782012-06-04 19:05:11 +00002075 ResetTimestamp timestamp;
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002076 const GrGLTexture::TexParams& oldTexParams = texture->getCachedTexParams(&timestamp);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002077 bool setAll = timestamp < this->getResetTimestamp();
2078 GrGLTexture::TexParams newTexParams;
2079
bsalomon@google.comb8670992012-07-25 21:27:09 +00002080 newTexParams.fFilter = params.isBilerp() ? GR_GL_LINEAR : GR_GL_NEAREST;
bsalomon@google.com4c883782012-06-04 19:05:11 +00002081
bsalomon@google.comb8670992012-07-25 21:27:09 +00002082 newTexParams.fWrapS = tile_to_gl_wrap(params.getTileModeX());
2083 newTexParams.fWrapT = tile_to_gl_wrap(params.getTileModeY());
bsalomon@google.com4c883782012-06-04 19:05:11 +00002084 memcpy(newTexParams.fSwizzleRGBA,
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002085 GrGLShaderBuilder::GetTexParamSwizzle(texture->config(), this->glCaps()),
bsalomon@google.com4c883782012-06-04 19:05:11 +00002086 sizeof(newTexParams.fSwizzleRGBA));
2087 if (setAll || newTexParams.fFilter != oldTexParams.fFilter) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002088 this->setTextureUnit(unitIdx);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002089 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2090 GR_GL_TEXTURE_MAG_FILTER,
2091 newTexParams.fFilter));
2092 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2093 GR_GL_TEXTURE_MIN_FILTER,
2094 newTexParams.fFilter));
2095 }
2096 if (setAll || newTexParams.fWrapS != oldTexParams.fWrapS) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002097 this->setTextureUnit(unitIdx);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002098 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2099 GR_GL_TEXTURE_WRAP_S,
2100 newTexParams.fWrapS));
2101 }
2102 if (setAll || newTexParams.fWrapT != oldTexParams.fWrapT) {
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_WRAP_T,
2106 newTexParams.fWrapT));
2107 }
2108 if (this->glCaps().textureSwizzleSupport() &&
2109 (setAll || memcmp(newTexParams.fSwizzleRGBA,
2110 oldTexParams.fSwizzleRGBA,
2111 sizeof(newTexParams.fSwizzleRGBA)))) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002112 this->setTextureUnit(unitIdx);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002113 set_tex_swizzle(newTexParams.fSwizzleRGBA,
2114 this->glInterface());
2115 }
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002116 texture->setCachedTexParams(newTexParams, this->getResetTimestamp());
bsalomon@google.com4c883782012-06-04 19:05:11 +00002117}
2118
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002119void GrGpuGL::flushMiscFixedFunctionState() {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002120
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002121 const GrDrawState& drawState = this->getDrawState();
reed@google.comac10a2d2010-12-22 21:39:39 +00002122
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002123 if (drawState.isDitherState()) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002124 if (kYes_TriState != fHWDitherEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002125 GL_CALL(Enable(GR_GL_DITHER));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002126 fHWDitherEnabled = kYes_TriState;
2127 }
2128 } else {
2129 if (kNo_TriState != fHWDitherEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002130 GL_CALL(Disable(GR_GL_DITHER));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002131 fHWDitherEnabled = kNo_TriState;
reed@google.comac10a2d2010-12-22 21:39:39 +00002132 }
2133 }
2134
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002135 if (drawState.isColorWriteDisabled()) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002136 if (kNo_TriState != fHWWriteToColor) {
2137 GL_CALL(ColorMask(GR_GL_FALSE, GR_GL_FALSE,
2138 GR_GL_FALSE, GR_GL_FALSE));
2139 fHWWriteToColor = kNo_TriState;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002140 }
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002141 } else {
2142 if (kYes_TriState != fHWWriteToColor) {
2143 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
2144 fHWWriteToColor = kYes_TriState;
2145 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00002146 }
2147
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002148 if (fHWDrawFace != drawState.getDrawFace()) {
bsalomon@google.coma5d056a2012-03-27 15:59:58 +00002149 switch (this->getDrawState().getDrawFace()) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002150 case GrDrawState::kCCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002151 GL_CALL(Enable(GR_GL_CULL_FACE));
2152 GL_CALL(CullFace(GR_GL_BACK));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002153 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002154 case GrDrawState::kCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002155 GL_CALL(Enable(GR_GL_CULL_FACE));
2156 GL_CALL(CullFace(GR_GL_FRONT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002157 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002158 case GrDrawState::kBoth_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002159 GL_CALL(Disable(GR_GL_CULL_FACE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002160 break;
2161 default:
2162 GrCrash("Unknown draw face.");
2163 }
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002164 fHWDrawFace = drawState.getDrawFace();
bsalomon@google.comd302f142011-03-03 13:54:13 +00002165 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002166}
2167
bsalomon@google.come49ad452013-02-20 19:33:20 +00002168void GrGpuGL::notifyVertexBufferBind(GrGLuint id) {
2169 fHWGeometryState.setVertexBufferID(id);
reed@google.comac10a2d2010-12-22 21:39:39 +00002170}
2171
bsalomon@google.come49ad452013-02-20 19:33:20 +00002172void GrGpuGL::notifyVertexBufferDelete(GrGLuint id) {
2173 fHWGeometryState.notifyVertexBufferDelete(id);
reed@google.comac10a2d2010-12-22 21:39:39 +00002174}
2175
bsalomon@google.come49ad452013-02-20 19:33:20 +00002176void GrGpuGL::notifyIndexBufferBind(GrGLuint id) {
2177 fHWGeometryState.setIndexBufferID(id);
reed@google.comac10a2d2010-12-22 21:39:39 +00002178}
2179
bsalomon@google.come49ad452013-02-20 19:33:20 +00002180void GrGpuGL::notifyIndexBufferDelete(GrGLuint id) {
2181 fHWGeometryState.notifyIndexBufferDelete(id);
reed@google.comac10a2d2010-12-22 21:39:39 +00002182}
2183
reed@google.comac10a2d2010-12-22 21:39:39 +00002184void GrGpuGL::notifyRenderTargetDelete(GrRenderTarget* renderTarget) {
2185 GrAssert(NULL != renderTarget);
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002186 if (fHWBoundRenderTarget == renderTarget) {
2187 fHWBoundRenderTarget = NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +00002188 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002189}
2190
2191void GrGpuGL::notifyTextureDelete(GrGLTexture* texture) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002192 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002193 if (fHWBoundTextures[s] == texture) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002194 // deleting bound texture does implied bind to 0
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002195 fHWBoundTextures[s] = NULL;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002196 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002197 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002198}
2199
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002200bool GrGpuGL::configToGLFormats(GrPixelConfig config,
2201 bool getSizedInternalFormat,
2202 GrGLenum* internalFormat,
2203 GrGLenum* externalFormat,
2204 GrGLenum* externalType) {
2205 GrGLenum dontCare;
2206 if (NULL == internalFormat) {
2207 internalFormat = &dontCare;
2208 }
2209 if (NULL == externalFormat) {
2210 externalFormat = &dontCare;
2211 }
2212 if (NULL == externalType) {
2213 externalType = &dontCare;
2214 }
2215
reed@google.comac10a2d2010-12-22 21:39:39 +00002216 switch (config) {
bsalomon@google.com0342a852012-08-20 19:22:38 +00002217 case kRGBA_8888_GrPixelConfig:
bsalomon@google.comc4364992011-11-07 15:54:49 +00002218 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002219 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002220 if (getSizedInternalFormat) {
2221 *internalFormat = GR_GL_RGBA8;
2222 } else {
2223 *internalFormat = GR_GL_RGBA;
2224 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002225 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.comc4364992011-11-07 15:54:49 +00002226 break;
bsalomon@google.com0342a852012-08-20 19:22:38 +00002227 case kBGRA_8888_GrPixelConfig:
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002228 if (!this->glCaps().bgraFormatSupport()) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002229 return false;
2230 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002231 if (this->glCaps().bgraIsInternalFormat()) {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002232 if (getSizedInternalFormat) {
2233 *internalFormat = GR_GL_BGRA8;
2234 } else {
2235 *internalFormat = GR_GL_BGRA;
2236 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002237 } else {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002238 if (getSizedInternalFormat) {
2239 *internalFormat = GR_GL_RGBA8;
2240 } else {
2241 *internalFormat = GR_GL_RGBA;
2242 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002243 }
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002244 *externalFormat = GR_GL_BGRA;
bsalomon@google.com6f379512011-11-16 20:36:03 +00002245 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002246 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002247 case kRGB_565_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002248 *internalFormat = GR_GL_RGB;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002249 *externalFormat = GR_GL_RGB;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002250 if (getSizedInternalFormat) {
2251 if (this->glBinding() == kDesktop_GrGLBinding) {
2252 return false;
2253 } else {
2254 *internalFormat = GR_GL_RGB565;
2255 }
2256 } else {
2257 *internalFormat = GR_GL_RGB;
2258 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002259 *externalType = GR_GL_UNSIGNED_SHORT_5_6_5;
reed@google.comac10a2d2010-12-22 21:39:39 +00002260 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002261 case kRGBA_4444_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002262 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002263 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002264 if (getSizedInternalFormat) {
2265 *internalFormat = GR_GL_RGBA4;
2266 } else {
2267 *internalFormat = GR_GL_RGBA;
2268 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002269 *externalType = GR_GL_UNSIGNED_SHORT_4_4_4_4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002270 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002271 case kIndex_8_GrPixelConfig:
bsalomon@google.comf6601872012-08-28 21:11:35 +00002272 if (this->getCaps().eightBitPaletteSupport()) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002273 *internalFormat = GR_GL_PALETTE8_RGBA8;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002274 // glCompressedTexImage doesn't take external params
2275 *externalFormat = GR_GL_PALETTE8_RGBA8;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002276 // no sized/unsized internal format distinction here
2277 *internalFormat = GR_GL_PALETTE8_RGBA8;
2278 // unused with CompressedTexImage
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002279 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002280 } else {
2281 return false;
2282 }
2283 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002284 case kAlpha_8_GrPixelConfig:
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002285 if (this->glCaps().textureRedSupport()) {
2286 *internalFormat = GR_GL_RED;
2287 *externalFormat = GR_GL_RED;
2288 if (getSizedInternalFormat) {
2289 *internalFormat = GR_GL_R8;
2290 } else {
2291 *internalFormat = GR_GL_RED;
2292 }
2293 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002294 } else {
2295 *internalFormat = GR_GL_ALPHA;
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002296 *externalFormat = GR_GL_ALPHA;
2297 if (getSizedInternalFormat) {
2298 *internalFormat = GR_GL_ALPHA8;
2299 } else {
2300 *internalFormat = GR_GL_ALPHA;
2301 }
2302 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002303 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002304 break;
2305 default:
2306 return false;
2307 }
2308 return true;
2309}
2310
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002311void GrGpuGL::setTextureUnit(int unit) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002312 GrAssert(unit >= 0 && unit < GrDrawState::kNumStages);
bsalomon@google.com49209392012-06-05 15:13:46 +00002313 if (fHWActiveTextureUnitIdx != unit) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002314 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com49209392012-06-05 15:13:46 +00002315 fHWActiveTextureUnitIdx = unit;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002316 }
2317}
bsalomon@google.com316f99232011-01-13 21:28:12 +00002318
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002319void GrGpuGL::setSpareTextureUnit() {
bsalomon@google.com49209392012-06-05 15:13:46 +00002320 if (fHWActiveTextureUnitIdx != (GR_GL_TEXTURE0 + SPARE_TEX_UNIT)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002321 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + SPARE_TEX_UNIT));
bsalomon@google.com49209392012-06-05 15:13:46 +00002322 fHWActiveTextureUnitIdx = SPARE_TEX_UNIT;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002323 }
2324}
2325
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00002326GrGLVertexBuffer* GrGpuGL::setBuffers(bool indexed,
2327 size_t* vertexOffsetInBytes,
2328 size_t* indexOffsetInBytes) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002329
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00002330 GrAssert(NULL != vertexOffsetInBytes);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002331
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002332 const GeometryPoolState& geoPoolState = this->getGeomPoolState();
2333
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002334 GrGLVertexBuffer* vbuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002335 switch (this->getGeomSrc().fVertexSrc) {
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00002336 case kBuffer_GeometrySrcType:
2337 *vertexOffsetInBytes = 0;
2338 vbuf = (GrGLVertexBuffer*) this->getGeomSrc().fVertexBuffer;
2339 break;
2340 case kArray_GeometrySrcType:
2341 case kReserved_GeometrySrcType:
2342 this->finalizeReservedVertices();
2343 *vertexOffsetInBytes = geoPoolState.fPoolStartVertex * this->getGeomSrc().fVertexSize;
2344 vbuf = (GrGLVertexBuffer*) geoPoolState.fPoolVertexBuffer;
2345 break;
2346 default:
2347 vbuf = NULL; // suppress warning
2348 GrCrash("Unknown geometry src type!");
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002349 }
2350
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002351 GrAssert(NULL != vbuf);
2352 GrAssert(!vbuf->isLocked());
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00002353 *vertexOffsetInBytes += vbuf->baseOffset();
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002354
2355 if (indexed) {
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00002356 GrAssert(NULL != indexOffsetInBytes);
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002357
2358 GrGLIndexBuffer* ibuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002359 switch (this->getGeomSrc().fIndexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002360 case kBuffer_GeometrySrcType:
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00002361 *indexOffsetInBytes = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002362 ibuf = (GrGLIndexBuffer*)this->getGeomSrc().fIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002363 break;
2364 case kArray_GeometrySrcType:
2365 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002366 this->finalizeReservedIndices();
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00002367 *indexOffsetInBytes = geoPoolState.fPoolStartIndex * sizeof(GrGLushort);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002368 ibuf = (GrGLIndexBuffer*) geoPoolState.fPoolIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002369 break;
2370 default:
2371 ibuf = NULL; // suppress warning
2372 GrCrash("Unknown geometry src type!");
2373 }
2374
2375 GrAssert(NULL != ibuf);
2376 GrAssert(!ibuf->isLocked());
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00002377 *indexOffsetInBytes += ibuf->baseOffset();
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00002378 if (!fHWGeometryState.isIndexBufferIDBound(ibuf->bufferID())) {
2379 ibuf->bind();
2380 fHWGeometryState.setIndexBufferID(ibuf->bufferID());
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002381 }
2382 }
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00002383 return vbuf;
2384}
2385
2386///////////////////////////////////////////////////////////////////////////////
2387
2388void GrGpuGL::HWGeometryState::AttribArray::set(const GrGpuGL* gpu,
2389 HWGeometryState* geoState,
2390 int index,
2391 GrGLVertexBuffer* buffer,
2392 GrGLint size,
2393 GrGLenum type,
2394 GrGLboolean normalized,
2395 GrGLsizei stride,
2396 GrGLvoid* offset) {
2397 if (!fEnableIsValid || !fEnabled) {
2398 GR_GL_CALL(gpu->glInterface(), EnableVertexAttribArray(index));
2399 fEnableIsValid = true;
2400 fEnabled = true;
2401 }
2402 if (!fAttribPointerIsValid ||
2403 fVertexBufferID != buffer->bufferID() ||
2404 fSize != size ||
2405 fNormalized != normalized ||
2406 fStride != stride ||
2407 offset != fOffset) {
2408
2409 GrGLuint bufferID = buffer->bufferID();
2410 if (!geoState->isVertexBufferIDBound(bufferID)) {
2411 buffer->bind();
2412 geoState->setVertexBufferID(bufferID);
2413 }
2414 GR_GL_CALL(gpu->glInterface(), VertexAttribPointer(index,
2415 size,
2416 type,
2417 normalized,
2418 stride,
2419 offset));
2420 fAttribPointerIsValid = true;
2421 fVertexBufferID = bufferID;
2422 fSize = size;
2423 fNormalized = normalized;
2424 fStride = stride;
2425 fOffset = offset;
2426 }
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002427}