blob: 76a7a63361a84ae2cb33a0b04b177e6b54b25c09 [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.coma3201942012-06-21 19:58:20 +000012#include "GrTemplates.h"
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +000013#include "GrTypes.h"
bsalomon@google.com3582bf92011-06-30 21:32:31 +000014#include "SkTemplates.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000015
caryclark@google.comcf6285b2012-06-06 12:09:01 +000016static const GrGLuint GR_MAX_GLUINT = ~0U;
twiz@google.com0f31ca72011-03-18 17:38:11 +000017static const GrGLint GR_INVAL_GLINT = ~0;
reed@google.comac10a2d2010-12-22 21:39:39 +000018
bsalomon@google.com0b77d682011-08-19 13:28:54 +000019#define GL_CALL(X) GR_GL_CALL(this->glInterface(), X)
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +000020#define GL_CALL_RET(RET, X) GR_GL_CALL_RET(this->glInterface(), RET, X)
bsalomon@google.com0b77d682011-08-19 13:28:54 +000021
bsalomon@google.com316f99232011-01-13 21:28:12 +000022// we use a spare texture unit to avoid
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000023// mucking with the state of any of the stages.
tomhudson@google.com93813632011-10-27 20:21:16 +000024static const int SPARE_TEX_UNIT = GrDrawState::kNumStages;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000025
reed@google.comac10a2d2010-12-22 21:39:39 +000026#define SKIP_CACHE_CHECK true
27
bsalomon@google.com4f3c2532012-01-19 16:16:52 +000028#if GR_GL_CHECK_ALLOC_WITH_GET_ERROR
29 #define CLEAR_ERROR_BEFORE_ALLOC(iface) GrGLClearErr(iface)
30 #define GL_ALLOC_CALL(iface, call) GR_GL_CALL_NOERRCHECK(iface, call)
31 #define CHECK_ALLOC_ERROR(iface) GR_GL_GET_ERROR(iface)
32#else
33 #define CLEAR_ERROR_BEFORE_ALLOC(iface)
34 #define GL_ALLOC_CALL(iface, call) GR_GL_CALL(iface, call)
35 #define CHECK_ALLOC_ERROR(iface) GR_GL_NO_ERROR
36#endif
37
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +000038
39///////////////////////////////////////////////////////////////////////////////
40
twiz@google.com0f31ca72011-03-18 17:38:11 +000041static const GrGLenum gXfermodeCoeff2Blend[] = {
42 GR_GL_ZERO,
43 GR_GL_ONE,
44 GR_GL_SRC_COLOR,
45 GR_GL_ONE_MINUS_SRC_COLOR,
46 GR_GL_DST_COLOR,
47 GR_GL_ONE_MINUS_DST_COLOR,
48 GR_GL_SRC_ALPHA,
49 GR_GL_ONE_MINUS_SRC_ALPHA,
50 GR_GL_DST_ALPHA,
51 GR_GL_ONE_MINUS_DST_ALPHA,
52 GR_GL_CONSTANT_COLOR,
53 GR_GL_ONE_MINUS_CONSTANT_COLOR,
54 GR_GL_CONSTANT_ALPHA,
55 GR_GL_ONE_MINUS_CONSTANT_ALPHA,
bsalomon@google.com271cffc2011-05-20 14:13:56 +000056
57 // extended blend coeffs
58 GR_GL_SRC1_COLOR,
59 GR_GL_ONE_MINUS_SRC1_COLOR,
60 GR_GL_SRC1_ALPHA,
61 GR_GL_ONE_MINUS_SRC1_ALPHA,
reed@google.comac10a2d2010-12-22 21:39:39 +000062};
63
bsalomon@google.com271cffc2011-05-20 14:13:56 +000064bool GrGpuGL::BlendCoeffReferencesConstant(GrBlendCoeff coeff) {
bsalomon@google.com080773c2011-03-15 19:09:25 +000065 static const bool gCoeffReferencesBlendConst[] = {
66 false,
67 false,
68 false,
69 false,
70 false,
71 false,
72 false,
73 false,
74 false,
75 false,
76 true,
77 true,
78 true,
79 true,
bsalomon@google.com271cffc2011-05-20 14:13:56 +000080
81 // extended blend coeffs
82 false,
83 false,
84 false,
85 false,
bsalomon@google.com080773c2011-03-15 19:09:25 +000086 };
87 return gCoeffReferencesBlendConst[coeff];
bsalomon@google.com47059542012-06-06 20:51:20 +000088 GR_STATIC_ASSERT(kTotalGrBlendCoeffCount ==
89 GR_ARRAY_COUNT(gCoeffReferencesBlendConst));
bsalomon@google.com271cffc2011-05-20 14:13:56 +000090
bsalomon@google.com47059542012-06-06 20:51:20 +000091 GR_STATIC_ASSERT(0 == kZero_GrBlendCoeff);
92 GR_STATIC_ASSERT(1 == kOne_GrBlendCoeff);
93 GR_STATIC_ASSERT(2 == kSC_GrBlendCoeff);
94 GR_STATIC_ASSERT(3 == kISC_GrBlendCoeff);
95 GR_STATIC_ASSERT(4 == kDC_GrBlendCoeff);
96 GR_STATIC_ASSERT(5 == kIDC_GrBlendCoeff);
97 GR_STATIC_ASSERT(6 == kSA_GrBlendCoeff);
98 GR_STATIC_ASSERT(7 == kISA_GrBlendCoeff);
99 GR_STATIC_ASSERT(8 == kDA_GrBlendCoeff);
100 GR_STATIC_ASSERT(9 == kIDA_GrBlendCoeff);
101 GR_STATIC_ASSERT(10 == kConstC_GrBlendCoeff);
102 GR_STATIC_ASSERT(11 == kIConstC_GrBlendCoeff);
103 GR_STATIC_ASSERT(12 == kConstA_GrBlendCoeff);
104 GR_STATIC_ASSERT(13 == kIConstA_GrBlendCoeff);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000105
bsalomon@google.com47059542012-06-06 20:51:20 +0000106 GR_STATIC_ASSERT(14 == kS2C_GrBlendCoeff);
107 GR_STATIC_ASSERT(15 == kIS2C_GrBlendCoeff);
108 GR_STATIC_ASSERT(16 == kS2A_GrBlendCoeff);
109 GR_STATIC_ASSERT(17 == kIS2A_GrBlendCoeff);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000110
111 // assertion for gXfermodeCoeff2Blend have to be in GrGpu scope
bsalomon@google.com47059542012-06-06 20:51:20 +0000112 GR_STATIC_ASSERT(kTotalGrBlendCoeffCount ==
113 GR_ARRAY_COUNT(gXfermodeCoeff2Blend));
bsalomon@google.com080773c2011-03-15 19:09:25 +0000114}
115
reed@google.comac10a2d2010-12-22 21:39:39 +0000116///////////////////////////////////////////////////////////////////////////////
117
bsalomon@google.com42ab7ea2011-01-19 17:19:40 +0000118static bool gPrintStartupSpew;
119
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000120static bool fbo_test(const GrGLInterface* gl, int w, int h) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000121
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000122 GR_GL_CALL(gl, ActiveTexture(GR_GL_TEXTURE0 + SPARE_TEX_UNIT));
bsalomon@google.com316f99232011-01-13 21:28:12 +0000123
twiz@google.com0f31ca72011-03-18 17:38:11 +0000124 GrGLuint testFBO;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000125 GR_GL_CALL(gl, GenFramebuffers(1, &testFBO));
126 GR_GL_CALL(gl, BindFramebuffer(GR_GL_FRAMEBUFFER, testFBO));
twiz@google.com0f31ca72011-03-18 17:38:11 +0000127 GrGLuint testRTTex;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000128 GR_GL_CALL(gl, GenTextures(1, &testRTTex));
129 GR_GL_CALL(gl, BindTexture(GR_GL_TEXTURE_2D, testRTTex));
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +0000130 // some implementations require texture to be mip-map complete before
131 // FBO with level 0 bound as color attachment will be framebuffer complete.
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000132 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000133 GR_GL_TEXTURE_MIN_FILTER,
134 GR_GL_NEAREST));
135 GR_GL_CALL(gl, TexImage2D(GR_GL_TEXTURE_2D, 0, GR_GL_RGBA, w, h,
136 0, GR_GL_RGBA, GR_GL_UNSIGNED_BYTE, NULL));
137 GR_GL_CALL(gl, BindTexture(GR_GL_TEXTURE_2D, 0));
138 GR_GL_CALL(gl, FramebufferTexture2D(GR_GL_FRAMEBUFFER,
139 GR_GL_COLOR_ATTACHMENT0,
140 GR_GL_TEXTURE_2D, testRTTex, 0));
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000141 GrGLenum status;
142 GR_GL_CALL_RET(gl, status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000143 GR_GL_CALL(gl, DeleteFramebuffers(1, &testFBO));
144 GR_GL_CALL(gl, DeleteTextures(1, &testRTTex));
bsalomon@google.com316f99232011-01-13 21:28:12 +0000145
bsalomon@google.comc312bf92011-03-21 21:10:33 +0000146 return status == GR_GL_FRAMEBUFFER_COMPLETE;
reed@google.comac10a2d2010-12-22 21:39:39 +0000147}
148
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000149GrGpuGL::GrGpuGL(const GrGLContextInfo& ctxInfo) : fGLContextInfo(ctxInfo) {
150
151 GrAssert(ctxInfo.isInitialized());
tomhudson@google.com747bf292011-06-14 18:16:52 +0000152
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000153 fillInConfigRenderableTable();
154
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000155 fPrintedCaps = false;
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000156
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000157 GrGLClearErr(fGLContextInfo.interface());
reed@google.comac10a2d2010-12-22 21:39:39 +0000158
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000159 if (gPrintStartupSpew) {
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000160 const GrGLubyte* ext;
161 GL_CALL_RET(ext, GetString(GR_GL_EXTENSIONS));
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000162 const GrGLubyte* vendor;
163 const GrGLubyte* renderer;
164 const GrGLubyte* version;
165 GL_CALL_RET(vendor, GetString(GR_GL_VENDOR));
166 GL_CALL_RET(renderer, GetString(GR_GL_RENDERER));
167 GL_CALL_RET(version, GetString(GR_GL_VERSION));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000168 GrPrintf("------------------------- create GrGpuGL %p --------------\n",
169 this);
170 GrPrintf("------ VENDOR %s\n", vendor);
171 GrPrintf("------ RENDERER %s\n", renderer);
172 GrPrintf("------ VERSION %s\n", version);
173 GrPrintf("------ EXTENSIONS\n %s \n", ext);
174 }
175
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000176 this->initCaps();
tomhudson@google.com747bf292011-06-14 18:16:52 +0000177
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000178 fProgramData = NULL;
179 fProgramCache = new ProgramCache(this->glContextInfo());
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000180
bsalomon@google.comfe676522011-06-17 18:12:21 +0000181 fLastSuccessfulStencilFmtIdx = 0;
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000182 fCanPreserveUnpremulRoundtrip = kUnknown_CanPreserveUnpremulRoundtrip;
caryclark@google.comcf6285b2012-06-06 12:09:01 +0000183 if (false) { // avoid bit rot, suppress warning
184 fbo_test(this->glInterface(), 0, 0);
185 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000186}
187
188GrGpuGL::~GrGpuGL() {
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000189 if (fProgramData && 0 != fHWProgramID) {
190 // detach the current program so there is no confusion on OpenGL's part
191 // that we want it to be deleted
192 GrAssert(fHWProgramID == fProgramData->fProgramID);
193 GL_CALL(UseProgram(0));
194 }
195
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000196 delete fProgramCache;
197 fProgramCache = NULL;
198 fProgramData = NULL;
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000199
bsalomon@google.com4a018bb2011-10-28 19:50:21 +0000200 // This must be called by before the GrDrawTarget destructor
201 this->releaseGeometry();
bsalomon@google.com15b11df2011-09-16 21:18:29 +0000202 // This subclass must do this before the base class destructor runs
203 // since we will unref the GrGLInterface.
204 this->releaseResources();
reed@google.comac10a2d2010-12-22 21:39:39 +0000205}
206
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000207///////////////////////////////////////////////////////////////////////////////
208
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000209void GrGpuGL::initCaps() {
210 GrGLint maxTextureUnits;
211 // check FS and fixed-function texture unit limits
212 // we only use textures in the fragment stage currently.
213 // checks are > to make sure we have a spare unit.
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000214 const GrGLInterface* gl = this->glInterface();
215 GR_GL_GetIntegerv(gl, GR_GL_MAX_TEXTURE_IMAGE_UNITS, &maxTextureUnits);
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000216 GrAssert(maxTextureUnits > GrDrawState::kNumStages);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000217
218 GrGLint numFormats;
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000219 GR_GL_GetIntegerv(gl, GR_GL_NUM_COMPRESSED_TEXTURE_FORMATS, &numFormats);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000220 SkAutoSTMalloc<10, GrGLint> formats(numFormats);
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000221 GR_GL_GetIntegerv(gl, GR_GL_COMPRESSED_TEXTURE_FORMATS, formats);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000222 for (int i = 0; i < numFormats; ++i) {
223 if (formats[i] == GR_GL_PALETTE8_RGBA8) {
224 fCaps.f8BitPaletteSupport = true;
225 break;
226 }
227 }
228
229 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000230 // we could also look for GL_ATI_separate_stencil extension or
231 // GL_EXT_stencil_two_side but they use different function signatures
232 // than GL2.0+ (and than each other).
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000233 fCaps.fTwoSidedStencilSupport = (this->glVersion() >= GR_GL_VER(2,0));
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000234 // supported on GL 1.4 and higher or by extension
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000235 fCaps.fStencilWrapOpsSupport = (this->glVersion() >= GR_GL_VER(1,4)) ||
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000236 this->hasExtension("GL_EXT_stencil_wrap");
237 } else {
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000238 // ES 2 has two sided stencil and stencil wrap
239 fCaps.fTwoSidedStencilSupport = true;
240 fCaps.fStencilWrapOpsSupport = true;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000241 }
242
243 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000244 fCaps.fBufferLockSupport = true; // we require VBO support and the desktop VBO
245 // extension includes glMapBuffer.
246 } else {
247 fCaps.fBufferLockSupport = this->hasExtension("GL_OES_mapbuffer");
248 }
249
250 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000251 if (this->glVersion() >= GR_GL_VER(2,0) ||
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000252 this->hasExtension("GL_ARB_texture_non_power_of_two")) {
253 fCaps.fNPOTTextureTileSupport = true;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000254 } else {
255 fCaps.fNPOTTextureTileSupport = false;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000256 }
257 } else {
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000258 // Unextended ES2 supports NPOT textures with clamp_to_edge and non-mip filters only
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000259 fCaps.fNPOTTextureTileSupport = this->hasExtension("GL_OES_texture_npot");
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000260 }
261
262 fCaps.fHWAALineSupport = (kDesktop_GrGLBinding == this->glBinding());
263
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000264 GR_GL_GetIntegerv(gl, GR_GL_MAX_TEXTURE_SIZE, &fCaps.fMaxTextureSize);
265 GR_GL_GetIntegerv(gl, GR_GL_MAX_RENDERBUFFER_SIZE, &fCaps.fMaxRenderTargetSize);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000266 // Our render targets are always created with textures as the color
267 // attachment, hence this min:
268 fCaps.fMaxRenderTargetSize = GrMin(fCaps.fMaxTextureSize, fCaps.fMaxRenderTargetSize);
269
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000270 fCaps.fFSAASupport = GrGLCaps::kNone_MSFBOType != this->glCaps().msFBOType();
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000271
272 // Enable supported shader-related caps
273 if (kDesktop_GrGLBinding == this->glBinding()) {
274 fCaps.fDualSourceBlendingSupport =
275 this->glVersion() >= GR_GL_VER(3,3) ||
276 this->hasExtension("GL_ARB_blend_func_extended");
277 fCaps.fShaderDerivativeSupport = true;
278 // we don't support GL_ARB_geometry_shader4, just GL 3.2+ GS
279 fCaps.fGeometryShaderSupport =
280 this->glVersion() >= GR_GL_VER(3,2) &&
281 this->glslGeneration() >= k150_GrGLSLGeneration;
282 } else {
283 fCaps.fShaderDerivativeSupport =
284 this->hasExtension("GL_OES_standard_derivatives");
285 }
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000286}
287
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000288void GrGpuGL::fillInConfigRenderableTable() {
289
290 // OpenGL < 3.0
291 // no support for render targets unless the GL_ARB_framebuffer_object
292 // extension is supported (in which case we get ALPHA, RED, RG, RGB,
293 // RGBA (ALPHA8, RGBA4, RGBA8) for OpenGL > 1.1). Note that we
294 // probably don't get R8 in this case.
295
296 // OpenGL 3.0
297 // base color renderable: ALPHA, RED, RG, RGB, and RGBA
298 // sized derivatives: ALPHA8, R8, RGBA4, RGBA8
299
300 // >= OpenGL 3.1
301 // base color renderable: RED, RG, RGB, and RGBA
302 // sized derivatives: R8, RGBA4, RGBA8
303 // if the GL_ARB_compatibility extension is supported then we get back
304 // support for GL_ALPHA and ALPHA8
305
306 // GL_EXT_bgra adds BGRA render targets to any version
307
308 // ES 2.0
309 // color renderable: RGBA4, RGB5_A1, RGB565
310 // GL_EXT_texture_rg adds support for R8 as a color render target
311 // GL_OES_rgb8_rgba8 and/or GL_ARM_rgba8 adds support for RGBA8
312 // GL_EXT_texture_format_BGRA8888 and/or GL_APPLE_texture_format_BGRA8888
313 // added BGRA support
314
315 if (kDesktop_GrGLBinding == this->glBinding()) {
316 // Post 3.0 we will get R8
317 // Prior to 3.0 we will get ALPHA8 (with GL_ARB_framebuffer_object)
318 if (this->glVersion() >= GR_GL_VER(3,0) ||
319 this->hasExtension("GL_ARB_framebuffer_object")) {
320 fConfigRenderSupport[kAlpha_8_GrPixelConfig] = true;
321 }
322 } else {
323 // On ES we can only hope for R8
robertphillips@google.com443e5a52012-04-30 20:01:21 +0000324 fConfigRenderSupport[kAlpha_8_GrPixelConfig] =
325 this->glCaps().textureRedSupport();
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000326 }
327
328 if (kDesktop_GrGLBinding != this->glBinding()) {
329 // only available in ES
330 fConfigRenderSupport[kRGB_565_GrPixelConfig] = true;
331 }
332
333 // Pre 3.0, Ganesh relies on either GL_ARB_framebuffer_object or
334 // GL_EXT_framebuffer_object for FBO support. Both of these
335 // allow RGBA4 render targets so this is always supported.
336 fConfigRenderSupport[kRGBA_4444_GrPixelConfig] = true;
337
338 if (this->glCaps().rgba8RenderbufferSupport()) {
339 fConfigRenderSupport[kRGBA_8888_PM_GrPixelConfig] = true;
340 }
341
342 if (this->glCaps().bgraFormatSupport()) {
343 fConfigRenderSupport[kBGRA_8888_PM_GrPixelConfig] = true;
344 }
345
346 // the un-premultiplied formats just inherit the premultiplied setting
347 fConfigRenderSupport[kRGBA_8888_UPM_GrPixelConfig] =
348 fConfigRenderSupport[kRGBA_8888_PM_GrPixelConfig];
349 fConfigRenderSupport[kBGRA_8888_UPM_GrPixelConfig] =
350 fConfigRenderSupport[kBGRA_8888_PM_GrPixelConfig];
351}
352
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000353bool GrGpuGL::canPreserveReadWriteUnpremulPixels() {
354 if (kUnknown_CanPreserveUnpremulRoundtrip ==
355 fCanPreserveUnpremulRoundtrip) {
356
357 SkAutoTMalloc<uint32_t> data(256 * 256 * 3);
358 uint32_t* srcData = data.get();
359 uint32_t* firstRead = data.get() + 256 * 256;
360 uint32_t* secondRead = data.get() + 2 * 256 * 256;
361
362 for (int y = 0; y < 256; ++y) {
363 for (int x = 0; x < 256; ++x) {
364 uint8_t* color = reinterpret_cast<uint8_t*>(&srcData[256*y + x]);
365 color[3] = y;
366 color[2] = x;
367 color[1] = x;
368 color[0] = x;
369 }
370 }
371
372 // We have broader support for read/write pixels on render targets
373 // than on textures.
374 GrTextureDesc dstDesc;
375 dstDesc.fFlags = kRenderTarget_GrTextureFlagBit |
376 kNoStencil_GrTextureFlagBit;
377 dstDesc.fWidth = 256;
378 dstDesc.fHeight = 256;
bsalomon@google.comb9014f42012-03-30 14:22:41 +0000379 dstDesc.fConfig = kRGBA_8888_PM_GrPixelConfig;
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000380
381 SkAutoTUnref<GrTexture> dstTex(this->createTexture(dstDesc, NULL, 0));
382 if (!dstTex.get()) {
383 return false;
384 }
385 GrRenderTarget* rt = dstTex.get()->asRenderTarget();
386 GrAssert(NULL != rt);
387
388 bool failed = true;
389 static const UnpremulConversion gMethods[] = {
390 kUpOnWrite_DownOnRead_UnpremulConversion,
391 kDownOnWrite_UpOnRead_UnpremulConversion,
392 };
393
394 // pretend that we can do the roundtrip to avoid recursive calls to
395 // this function
396 fCanPreserveUnpremulRoundtrip = kYes_CanPreserveUnpremulRoundtrip;
397 for (size_t i = 0; i < GR_ARRAY_COUNT(gMethods) && failed; ++i) {
398 fUnpremulConversion = gMethods[i];
399 rt->writePixels(0, 0,
400 256, 256,
401 kRGBA_8888_UPM_GrPixelConfig, srcData, 0);
402 rt->readPixels(0, 0,
403 256, 256,
404 kRGBA_8888_UPM_GrPixelConfig, firstRead, 0);
405 rt->writePixels(0, 0,
406 256, 256,
407 kRGBA_8888_UPM_GrPixelConfig, firstRead, 0);
408 rt->readPixels(0, 0,
409 256, 256,
410 kRGBA_8888_UPM_GrPixelConfig, secondRead, 0);
411 failed = false;
412 for (int j = 0; j < 256 * 256; ++j) {
413 if (firstRead[j] != secondRead[j]) {
414 failed = true;
415 break;
416 }
417 }
418 }
419 fCanPreserveUnpremulRoundtrip = failed ?
420 kNo_CanPreserveUnpremulRoundtrip :
421 kYes_CanPreserveUnpremulRoundtrip;
422 }
423
424 if (kYes_CanPreserveUnpremulRoundtrip == fCanPreserveUnpremulRoundtrip) {
425 return true;
426 } else {
427 return false;
428 }
429}
430
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000431GrPixelConfig GrGpuGL::preferredReadPixelsConfig(GrPixelConfig config) const {
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000432 if (GR_GL_RGBA_8888_PIXEL_OPS_SLOW && GrPixelConfigIsRGBA8888(config)) {
433 return GrPixelConfigSwapRAndB(config);
434 } else {
435 return config;
436 }
437}
438
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000439GrPixelConfig GrGpuGL::preferredWritePixelsConfig(GrPixelConfig config) const {
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000440 if (GR_GL_RGBA_8888_PIXEL_OPS_SLOW && GrPixelConfigIsRGBA8888(config)) {
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000441 return GrPixelConfigSwapRAndB(config);
442 } else {
443 return config;
444 }
445}
446
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000447bool GrGpuGL::fullReadPixelsIsFasterThanPartial() const {
448 return SkToBool(GR_GL_FULL_READPIXELS_FASTER_THAN_PARTIAL);
449}
450
bsalomon@google.com1bf1c212011-11-05 12:18:58 +0000451void GrGpuGL::onResetContext() {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000452 if (gPrintStartupSpew && !fPrintedCaps) {
453 fPrintedCaps = true;
454 this->getCaps().print();
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000455 this->glCaps().print();
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000456 }
457
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000458 // we don't use the zb at all
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000459 GL_CALL(Disable(GR_GL_DEPTH_TEST));
460 GL_CALL(DepthMask(GR_GL_FALSE));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000461
bsalomon@google.com978c8c62012-05-21 14:45:49 +0000462 fHWDrawFace = GrDrawState::kInvalid_DrawFace;
463 fHWDitherEnabled = kUnknown_TriState;
reed@google.comac10a2d2010-12-22 21:39:39 +0000464
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000465 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com469d0dd2012-05-21 20:14:29 +0000466 // Desktop-only state that we never change
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000467 GL_CALL(Disable(GR_GL_POINT_SMOOTH));
bsalomon@google.com469d0dd2012-05-21 20:14:29 +0000468 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
469 GL_CALL(Disable(GR_GL_POLYGON_SMOOTH));
470 GL_CALL(Disable(GR_GL_POLYGON_STIPPLE));
471 GL_CALL(Disable(GR_GL_COLOR_LOGIC_OP));
bsalomon@google.come76b7cc2012-06-18 12:47:06 +0000472 if (this->glCaps().imagingSupport()) {
473 GL_CALL(Disable(GR_GL_COLOR_TABLE));
474 }
bsalomon@google.com469d0dd2012-05-21 20:14:29 +0000475 GL_CALL(Disable(GR_GL_INDEX_LOGIC_OP));
476 GL_CALL(Disable(GR_GL_POLYGON_OFFSET_FILL));
477 // Since ES doesn't support glPointSize at all we always use the VS to
478 // set the point size
479 GL_CALL(Enable(GR_GL_VERTEX_PROGRAM_POINT_SIZE));
480
481 // We should set glPolygonMode(FRONT_AND_BACK,FILL) here, too. It isn't
482 // currently part of our gl interface. There are probably others as
483 // well.
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000484 }
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +0000485 fHWAAState.invalidate();
bsalomon@google.com978c8c62012-05-21 14:45:49 +0000486 fHWWriteToColor = kUnknown_TriState;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000487
reed@google.comac10a2d2010-12-22 21:39:39 +0000488 // we only ever use lines in hairline mode
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000489 GL_CALL(LineWidth(1));
reed@google.comac10a2d2010-12-22 21:39:39 +0000490
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000491 // invalid
bsalomon@google.com49209392012-06-05 15:13:46 +0000492 fHWActiveTextureUnitIdx = -1;
reed@google.comac10a2d2010-12-22 21:39:39 +0000493
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +0000494 fHWBlendState.invalidate();
bsalomon@google.com080773c2011-03-15 19:09:25 +0000495
tomhudson@google.com93813632011-10-27 20:21:16 +0000496 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.comc811ea32012-05-21 15:33:09 +0000497 fHWBoundTextures[s] = NULL;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000498 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000499
bsalomon@google.coma3201942012-06-21 19:58:20 +0000500 fHWScissorSettings.invalidate();
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000501
bsalomon@google.coma3201942012-06-21 19:58:20 +0000502 fHWViewport.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000503
bsalomon@google.com457b8a32012-05-21 21:19:58 +0000504 fHWStencilSettings.invalidate();
bsalomon@google.coma3201942012-06-21 19:58:20 +0000505 fHWStencilTestEnabled = kUnknown_TriState;
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000506
reed@google.comac10a2d2010-12-22 21:39:39 +0000507 fHWGeometryState.fIndexBuffer = NULL;
508 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.coma3201942012-06-21 19:58:20 +0000509
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +0000510 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000511
bsalomon@google.comc811ea32012-05-21 15:33:09 +0000512 fHWBoundRenderTarget = NULL;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000513
514 // we assume these values
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000515 if (this->glCaps().unpackRowLengthSupport()) {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000516 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
517 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000518 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000519 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
520 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000521 if (this->glCaps().unpackFlipYSupport()) {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000522 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
523 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000524 if (this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000525 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, GR_GL_FALSE));
526 }
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000527
caryclark@google.comcf6285b2012-06-06 12:09:01 +0000528 fHWGeometryState.fVertexOffset = ~0U;
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000529
530 // Third party GL code may have left vertex attributes enabled. Some GL
531 // implementations (osmesa) may read vetex attributes that are not required
532 // by the current shader. Therefore, we have to ensure that only the
533 // attributes we require for the current draw are enabled or we may cause an
534 // invalid read.
535
536 // Disable all vertex layout bits so that next flush will assume all
537 // optional vertex attributes are disabled.
538 fHWGeometryState.fVertexLayout = 0;
539
540 // We always use the this attribute and assume it is always enabled.
541 int posAttrIdx = GrGLProgram::PositionAttributeIdx();
542 GL_CALL(EnableVertexAttribArray(posAttrIdx));
543 // Disable all other vertex attributes.
bsalomon@google.com60da4172012-06-01 19:25:00 +0000544 for (int va = 0; va < this->glCaps().maxVertexAttributes(); ++va) {
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000545 if (va != posAttrIdx) {
546 GL_CALL(DisableVertexAttribArray(va));
547 }
548 }
549
550 fHWProgramID = 0;
551 fHWConstAttribColor = GrColor_ILLEGAL;
552 fHWConstAttribCoverage = GrColor_ILLEGAL;
reed@google.comac10a2d2010-12-22 21:39:39 +0000553}
554
bsalomon@google.come269f212011-11-07 13:29:52 +0000555GrTexture* GrGpuGL::onCreatePlatformTexture(const GrPlatformTextureDesc& desc) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000556 GrGLTexture::Desc glTexDesc;
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000557 if (!configToGLFormats(desc.fConfig, false, NULL, NULL, NULL)) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000558 return NULL;
559 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000560
robertphillips@google.com32716282012-06-04 12:48:45 +0000561 // next line relies on PlatformTextureDesc's flags matching GrTexture's
562 glTexDesc.fFlags = (GrTextureFlags) desc.fFlags;
bsalomon@google.com99621082011-11-15 16:47:16 +0000563 glTexDesc.fWidth = desc.fWidth;
564 glTexDesc.fHeight = desc.fHeight;
bsalomon@google.come269f212011-11-07 13:29:52 +0000565 glTexDesc.fConfig = desc.fConfig;
robertphillips@google.com32716282012-06-04 12:48:45 +0000566 glTexDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.come269f212011-11-07 13:29:52 +0000567 glTexDesc.fTextureID = static_cast<GrGLuint>(desc.fTextureHandle);
568 glTexDesc.fOwnsID = false;
569 glTexDesc.fOrientation = GrGLTexture::kBottomUp_Orientation;
570
571 GrGLTexture* texture = NULL;
572 if (desc.fFlags & kRenderTarget_GrPlatformTextureFlag) {
573 GrGLRenderTarget::Desc glRTDesc;
574 glRTDesc.fRTFBOID = 0;
575 glRTDesc.fTexFBOID = 0;
576 glRTDesc.fMSColorRenderbufferID = 0;
577 glRTDesc.fOwnIDs = true;
578 glRTDesc.fConfig = desc.fConfig;
579 glRTDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.com99621082011-11-15 16:47:16 +0000580 if (!this->createRenderTargetObjects(glTexDesc.fWidth,
581 glTexDesc.fHeight,
bsalomon@google.come269f212011-11-07 13:29:52 +0000582 glTexDesc.fTextureID,
583 &glRTDesc)) {
584 return NULL;
585 }
586 texture = new GrGLTexture(this, glTexDesc, glRTDesc);
587 } else {
588 texture = new GrGLTexture(this, glTexDesc);
589 }
590 if (NULL == texture) {
591 return NULL;
592 }
593
594 this->setSpareTextureUnit();
595 return texture;
596}
597
598GrRenderTarget* GrGpuGL::onCreatePlatformRenderTarget(const GrPlatformRenderTargetDesc& desc) {
599 GrGLRenderTarget::Desc glDesc;
600 glDesc.fConfig = desc.fConfig;
601 glDesc.fRTFBOID = static_cast<GrGLuint>(desc.fRenderTargetHandle);
602 glDesc.fMSColorRenderbufferID = 0;
603 glDesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
604 glDesc.fSampleCnt = desc.fSampleCnt;
605 glDesc.fOwnIDs = false;
606 GrGLIRect viewport;
607 viewport.fLeft = 0;
608 viewport.fBottom = 0;
609 viewport.fWidth = desc.fWidth;
610 viewport.fHeight = desc.fHeight;
611
612 GrRenderTarget* tgt = new GrGLRenderTarget(this, glDesc, viewport);
613 if (desc.fStencilBits) {
614 GrGLStencilBuffer::Format format;
615 format.fInternalFormat = GrGLStencilBuffer::kUnknownInternalFormat;
616 format.fPacked = false;
617 format.fStencilBits = desc.fStencilBits;
618 format.fTotalBits = desc.fStencilBits;
619 GrGLStencilBuffer* sb = new GrGLStencilBuffer(this,
620 0,
621 desc.fWidth,
622 desc.fHeight,
623 desc.fSampleCnt,
624 format);
625 tgt->setStencilBuffer(sb);
626 sb->unref();
627 }
628 return tgt;
629}
630
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000631////////////////////////////////////////////////////////////////////////////////
632
bsalomon@google.com6f379512011-11-16 20:36:03 +0000633void GrGpuGL::onWriteTexturePixels(GrTexture* texture,
634 int left, int top, int width, int height,
635 GrPixelConfig config, const void* buffer,
636 size_t rowBytes) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000637 if (NULL == buffer) {
638 return;
639 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000640 GrGLTexture* glTex = static_cast<GrGLTexture*>(texture);
641
bsalomon@google.com6f379512011-11-16 20:36:03 +0000642 this->setSpareTextureUnit();
643 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTex->textureID()));
644 GrGLTexture::Desc desc;
robertphillips@google.com32716282012-06-04 12:48:45 +0000645 desc.fFlags = glTex->desc().fFlags;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000646 desc.fWidth = glTex->width();
647 desc.fHeight = glTex->height();
robertphillips@google.com32716282012-06-04 12:48:45 +0000648 desc.fConfig = glTex->config();
649 desc.fSampleCnt = glTex->desc().fSampleCnt;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000650 desc.fTextureID = glTex->textureID();
robertphillips@google.com32716282012-06-04 12:48:45 +0000651 desc.fOrientation = glTex->orientation();
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000652
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000653 this->uploadTexData(desc, false,
654 left, top, width, height,
655 config, buffer, rowBytes);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000656}
657
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000658namespace {
659bool adjust_pixel_ops_params(int surfaceWidth,
660 int surfaceHeight,
661 size_t bpp,
662 int* left, int* top, int* width, int* height,
663 const void** data,
664 size_t* rowBytes) {
665 if (!*rowBytes) {
666 *rowBytes = *width * bpp;
667 }
668
669 GrIRect subRect = GrIRect::MakeXYWH(*left, *top, *width, *height);
670 GrIRect bounds = GrIRect::MakeWH(surfaceWidth, surfaceHeight);
671
672 if (!subRect.intersect(bounds)) {
673 return false;
674 }
675 *data = reinterpret_cast<const void*>(reinterpret_cast<intptr_t>(*data) +
676 (subRect.fTop - *top) * *rowBytes + (subRect.fLeft - *left) * bpp);
677
678 *left = subRect.fLeft;
679 *top = subRect.fTop;
680 *width = subRect.width();
681 *height = subRect.height();
682 return true;
683}
684}
685
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000686bool GrGpuGL::uploadTexData(const GrGLTexture::Desc& desc,
687 bool isNewTexture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000688 int left, int top, int width, int height,
689 GrPixelConfig dataConfig,
690 const void* data,
691 size_t rowBytes) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000692 GrAssert(NULL != data || isNewTexture);
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000693
694 size_t bpp = GrBytesPerPixel(dataConfig);
695 if (!adjust_pixel_ops_params(desc.fWidth, desc.fHeight, bpp, &left, &top,
696 &width, &height, &data, &rowBytes)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000697 return false;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000698 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000699 size_t trimRowBytes = width * bpp;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000700
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000701 // in case we need a temporary, trimmed copy of the src pixels
702 SkAutoSMalloc<128 * 128> tempStorage;
703
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000704 bool useTexStorage = isNewTexture &&
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000705 this->glCaps().texStorageSupport();
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000706 if (useTexStorage) {
707 if (kDesktop_GrGLBinding == this->glBinding()) {
708 // 565 is not a sized internal format on desktop GL. So on desktop
709 // with 565 we always use an unsized internal format to let the
710 // system pick the best sized format to convert the 565 data to.
711 // Since glTexStorage only allows sized internal formats we will
712 // instead fallback to glTexImage2D.
713 useTexStorage = desc.fConfig != kRGB_565_GrPixelConfig;
714 } else {
715 // ES doesn't allow paletted textures to be used with tex storage
716 useTexStorage = desc.fConfig != kIndex_8_GrPixelConfig;
717 }
718 }
719
720 GrGLenum internalFormat;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000721 GrGLenum externalFormat;
722 GrGLenum externalType;
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000723 // glTexStorage requires sized internal formats on both desktop and ES. ES
724 // glTexImage requires an unsized format.
725 if (!this->configToGLFormats(dataConfig, useTexStorage, &internalFormat,
726 &externalFormat, &externalType)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000727 return false;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000728 }
729
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000730 if (!isNewTexture && GR_GL_PALETTE8_RGBA8 == internalFormat) {
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000731 // paletted textures cannot be updated
732 return false;
733 }
734
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000735 /*
bsalomon@google.com6f379512011-11-16 20:36:03 +0000736 * check whether to allocate a temporary buffer for flipping y or
737 * because our srcData has extra bytes past each row. If so, we need
738 * to trim those off here, since GL ES may not let us specify
739 * GL_UNPACK_ROW_LENGTH.
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000740 */
bsalomon@google.com6f379512011-11-16 20:36:03 +0000741 bool restoreGLRowLength = false;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000742 bool swFlipY = false;
743 bool glFlipY = false;
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000744 if (NULL != data) {
745 if (GrGLTexture::kBottomUp_Orientation == desc.fOrientation) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000746 if (this->glCaps().unpackFlipYSupport()) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000747 glFlipY = true;
748 } else {
749 swFlipY = true;
750 }
751 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000752 if (this->glCaps().unpackRowLengthSupport() && !swFlipY) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000753 // can't use this for flipping, only non-neg values allowed. :(
754 if (rowBytes != trimRowBytes) {
755 GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
756 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength));
757 restoreGLRowLength = true;
758 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000759 } else {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000760 if (trimRowBytes != rowBytes || swFlipY) {
761 // copy data into our new storage, skipping the trailing bytes
762 size_t trimSize = height * trimRowBytes;
763 const char* src = (const char*)data;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000764 if (swFlipY) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000765 src += (height - 1) * rowBytes;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000766 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000767 char* dst = (char*)tempStorage.reset(trimSize);
768 for (int y = 0; y < height; y++) {
769 memcpy(dst, src, trimRowBytes);
770 if (swFlipY) {
771 src -= rowBytes;
772 } else {
773 src += rowBytes;
774 }
775 dst += trimRowBytes;
776 }
777 // now point data to our copied version
778 data = tempStorage.get();
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000779 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000780 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000781 if (glFlipY) {
782 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_TRUE));
783 }
784 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, static_cast<GrGLint>(bpp)));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000785 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000786 bool succeeded = true;
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000787 if (isNewTexture &&
788 0 == left && 0 == top &&
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000789 desc.fWidth == width && desc.fHeight == height) {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000790 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000791 if (useTexStorage) {
792 // We never resize or change formats of textures. We don't use
793 // mipmaps currently.
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000794 GL_ALLOC_CALL(this->glInterface(),
795 TexStorage2D(GR_GL_TEXTURE_2D,
796 1, // levels
797 internalFormat,
798 desc.fWidth, desc.fHeight));
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000799 } else {
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000800 if (GR_GL_PALETTE8_RGBA8 == internalFormat) {
801 GrGLsizei imageSize = desc.fWidth * desc.fHeight +
802 kGrColorTableSize;
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000803 GL_ALLOC_CALL(this->glInterface(),
804 CompressedTexImage2D(GR_GL_TEXTURE_2D,
805 0, // level
806 internalFormat,
807 desc.fWidth, desc.fHeight,
808 0, // border
809 imageSize,
810 data));
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000811 } else {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000812 GL_ALLOC_CALL(this->glInterface(),
813 TexImage2D(GR_GL_TEXTURE_2D,
814 0, // level
815 internalFormat,
816 desc.fWidth, desc.fHeight,
817 0, // border
818 externalFormat, externalType,
819 data));
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000820 }
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000821 }
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000822 GrGLenum error = CHECK_ALLOC_ERROR(this->glInterface());
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000823 if (error != GR_GL_NO_ERROR) {
824 succeeded = false;
825 } else {
826 // if we have data and we used TexStorage to create the texture, we
827 // now upload with TexSubImage.
828 if (NULL != data && useTexStorage) {
829 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D,
830 0, // level
831 left, top,
832 width, height,
833 externalFormat, externalType,
834 data));
835 }
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000836 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000837 } else {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000838 if (swFlipY || glFlipY) {
bsalomon@google.com6f379512011-11-16 20:36:03 +0000839 top = desc.fHeight - (top + height);
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000840 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000841 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D,
842 0, // level
843 left, top,
844 width, height,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000845 externalFormat, externalType, data));
846 }
847
848 if (restoreGLRowLength) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000849 GrAssert(this->glCaps().unpackRowLengthSupport());
bsalomon@google.com6f379512011-11-16 20:36:03 +0000850 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000851 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000852 if (glFlipY) {
853 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
854 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000855 return succeeded;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000856}
857
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000858namespace {
859bool renderbuffer_storage_msaa(GrGLContextInfo& ctxInfo,
860 int sampleCount,
861 GrGLenum format,
862 int width, int height) {
863 CLEAR_ERROR_BEFORE_ALLOC(ctxInfo.interface());
864 GrAssert(GrGLCaps::kNone_MSFBOType != ctxInfo.caps().msFBOType());
865 bool created = false;
866 if (GrGLCaps::kNVDesktop_CoverageAAType ==
867 ctxInfo.caps().coverageAAType()) {
868 const GrGLCaps::MSAACoverageMode& mode =
869 ctxInfo.caps().getMSAACoverageMode(sampleCount);
870 GL_ALLOC_CALL(ctxInfo.interface(),
871 RenderbufferStorageMultisampleCoverage(GR_GL_RENDERBUFFER,
872 mode.fCoverageSampleCnt,
873 mode.fColorSampleCnt,
874 format,
875 width, height));
876 created = (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctxInfo.interface()));
877 }
878 if (!created) {
bsalomon@google.comf6b070d2012-04-27 14:25:44 +0000879 // glRBMS will fail if requested samples is > max samples.
880 sampleCount = GrMin(sampleCount, ctxInfo.caps().maxSampleCount());
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000881 GL_ALLOC_CALL(ctxInfo.interface(),
882 RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
883 sampleCount,
884 format,
885 width, height));
886 created = (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctxInfo.interface()));
887 }
888 return created;
889}
890}
891
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000892bool GrGpuGL::createRenderTargetObjects(int width, int height,
893 GrGLuint texID,
894 GrGLRenderTarget::Desc* desc) {
895 desc->fMSColorRenderbufferID = 0;
896 desc->fRTFBOID = 0;
897 desc->fTexFBOID = 0;
898 desc->fOwnIDs = true;
899
900 GrGLenum status;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000901
bsalomon@google.comab15d612011-08-09 12:57:56 +0000902 GrGLenum msColorFormat = 0; // suppress warning
903
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000904 GL_CALL(GenFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000905 if (!desc->fTexFBOID) {
906 goto FAILED;
907 }
908
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000909
910 // If we are using multisampling we will create two FBOS. We render
911 // to one and then resolve to the texture bound to the other.
bsalomon@google.come269f212011-11-07 13:29:52 +0000912 if (desc->fSampleCnt > 0) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000913 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000914 goto FAILED;
915 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000916 GL_CALL(GenFramebuffers(1, &desc->fRTFBOID));
917 GL_CALL(GenRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000918 if (!desc->fRTFBOID ||
919 !desc->fMSColorRenderbufferID ||
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000920 !this->configToGLFormats(desc->fConfig,
921 // GLES requires sized internal formats
922 kES2_GrGLBinding == this->glBinding(),
923 &msColorFormat, NULL, NULL)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000924 goto FAILED;
925 }
926 } else {
927 desc->fRTFBOID = desc->fTexFBOID;
928 }
929
bsalomon@google.com0e9b41a2012-01-04 22:11:43 +0000930 // below here we may bind the FBO
bsalomon@google.comc811ea32012-05-21 15:33:09 +0000931 fHWBoundRenderTarget = NULL;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000932 if (desc->fRTFBOID != desc->fTexFBOID) {
933 GrAssert(desc->fSampleCnt > 1);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000934 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000935 desc->fMSColorRenderbufferID));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000936 if (!renderbuffer_storage_msaa(fGLContextInfo,
937 desc->fSampleCnt,
938 msColorFormat,
939 width, height)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000940 goto FAILED;
941 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000942 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fRTFBOID));
943 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000944 GR_GL_COLOR_ATTACHMENT0,
945 GR_GL_RENDERBUFFER,
946 desc->fMSColorRenderbufferID));
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000947 if (!this->glCaps().isConfigVerifiedColorAttachment(desc->fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000948 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
949 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
950 goto FAILED;
951 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000952 fGLContextInfo.caps().markConfigAsValidColorAttachment(
953 desc->fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000954 }
955 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000956 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000957
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000958 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
959 GR_GL_COLOR_ATTACHMENT0,
960 GR_GL_TEXTURE_2D,
961 texID, 0));
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000962 if (!this->glCaps().isConfigVerifiedColorAttachment(desc->fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000963 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
964 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
965 goto FAILED;
966 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000967 fGLContextInfo.caps().markConfigAsValidColorAttachment(desc->fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000968 }
969
970 return true;
971
972FAILED:
973 if (desc->fMSColorRenderbufferID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000974 GL_CALL(DeleteRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000975 }
976 if (desc->fRTFBOID != desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000977 GL_CALL(DeleteFramebuffers(1, &desc->fRTFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000978 }
979 if (desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000980 GL_CALL(DeleteFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000981 }
982 return false;
983}
984
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000985// good to set a break-point here to know when createTexture fails
986static GrTexture* return_null_texture() {
987// GrAssert(!"null texture");
988 return NULL;
989}
990
caryclark@google.comcf6285b2012-06-06 12:09:01 +0000991#if 0 && GR_DEBUG
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000992static size_t as_size_t(int x) {
993 return x;
994}
995#endif
996
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000997GrTexture* GrGpuGL::onCreateTexture(const GrTextureDesc& desc,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000998 const void* srcData,
999 size_t rowBytes) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001000
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001001 GrGLTexture::Desc glTexDesc;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001002 GrGLRenderTarget::Desc glRTDesc;
reed@google.comac10a2d2010-12-22 21:39:39 +00001003
bsalomon@google.com78d6cf92012-01-30 18:09:31 +00001004 // Attempt to catch un- or wrongly initialized sample counts;
1005 GrAssert(desc.fSampleCnt >= 0 && desc.fSampleCnt <= 64);
1006
robertphillips@google.com32716282012-06-04 12:48:45 +00001007 glTexDesc.fFlags = desc.fFlags;
bsalomon@google.com99621082011-11-15 16:47:16 +00001008 glTexDesc.fWidth = desc.fWidth;
1009 glTexDesc.fHeight = desc.fHeight;
bsalomon@google.com78d6cf92012-01-30 18:09:31 +00001010 glTexDesc.fConfig = desc.fConfig;
robertphillips@google.com32716282012-06-04 12:48:45 +00001011 glTexDesc.fSampleCnt = desc.fSampleCnt;
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001012 glTexDesc.fClientCacheID = desc.fClientCacheID;
robertphillips@google.com32716282012-06-04 12:48:45 +00001013
bsalomon@google.com78d6cf92012-01-30 18:09:31 +00001014 glTexDesc.fOwnsID = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001015
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001016 glRTDesc.fMSColorRenderbufferID = 0;
1017 glRTDesc.fRTFBOID = 0;
1018 glRTDesc.fTexFBOID = 0;
1019 glRTDesc.fOwnIDs = true;
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001020 glRTDesc.fConfig = glTexDesc.fConfig;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001021
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001022 bool renderTarget = 0 != (desc.fFlags & kRenderTarget_GrTextureFlagBit);
reed@google.comac10a2d2010-12-22 21:39:39 +00001023
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001024 const Caps& caps = this->getCaps();
1025
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001026 // We keep GrRenderTargets in GL's normal orientation so that they
1027 // can be drawn to by the outside world without the client having
1028 // to render upside down.
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001029 glTexDesc.fOrientation = renderTarget ? GrGLTexture::kBottomUp_Orientation :
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001030 GrGLTexture::kTopDown_Orientation;
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001031
bsalomon@google.com78d6cf92012-01-30 18:09:31 +00001032 glRTDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001033 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType() &&
bsalomon@google.com78d6cf92012-01-30 18:09:31 +00001034 desc.fSampleCnt) {
bsalomon@google.com945bbe12012-06-15 14:30:34 +00001035 //GrPrintf("MSAA RT requested but not supported on this platform.");
1036 return return_null_texture();
reed@google.comac10a2d2010-12-22 21:39:39 +00001037 }
1038
reed@google.comac10a2d2010-12-22 21:39:39 +00001039 if (renderTarget) {
bsalomon@google.com99621082011-11-15 16:47:16 +00001040 if (glTexDesc.fWidth > caps.fMaxRenderTargetSize ||
1041 glTexDesc.fHeight > caps.fMaxRenderTargetSize) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001042 return return_null_texture();
1043 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001044 }
1045
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001046 GL_CALL(GenTextures(1, &glTexDesc.fTextureID));
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001047 if (renderTarget && this->glCaps().textureUsageSupport()) {
bsalomon@google.com07dd2bf2011-12-09 19:40:36 +00001048 // provides a hint about how this texture will be used
1049 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1050 GR_GL_TEXTURE_USAGE,
1051 GR_GL_FRAMEBUFFER_ATTACHMENT));
1052 }
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001053 if (!glTexDesc.fTextureID) {
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001054 return return_null_texture();
1055 }
1056
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001057 this->setSpareTextureUnit();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001058 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTexDesc.fTextureID));
bsalomon@google.come269f212011-11-07 13:29:52 +00001059
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001060 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
1061 // drivers have a bug where an FBO won't be complete if it includes a
1062 // texture that is not mipmap complete (considering the filter in use).
1063 GrGLTexture::TexParams initialTexParams;
1064 // we only set a subset here so invalidate first
1065 initialTexParams.invalidate();
1066 initialTexParams.fFilter = GR_GL_NEAREST;
1067 initialTexParams.fWrapS = GR_GL_CLAMP_TO_EDGE;
1068 initialTexParams.fWrapT = GR_GL_CLAMP_TO_EDGE;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001069 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1070 GR_GL_TEXTURE_MAG_FILTER,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001071 initialTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001072 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1073 GR_GL_TEXTURE_MIN_FILTER,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001074 initialTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001075 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1076 GR_GL_TEXTURE_WRAP_S,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001077 initialTexParams.fWrapS));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001078 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1079 GR_GL_TEXTURE_WRAP_T,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001080 initialTexParams.fWrapT));
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001081 if (!this->uploadTexData(glTexDesc, true, 0, 0,
1082 glTexDesc.fWidth, glTexDesc.fHeight,
1083 desc.fConfig, srcData, rowBytes)) {
1084 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
1085 return return_null_texture();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001086 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001087
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001088 GrGLTexture* tex;
reed@google.comac10a2d2010-12-22 21:39:39 +00001089 if (renderTarget) {
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001090 // unbind the texture from the texture unit before binding it to the frame buffer
1091 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, 0));
1092
bsalomon@google.com99621082011-11-15 16:47:16 +00001093 if (!this->createRenderTargetObjects(glTexDesc.fWidth,
1094 glTexDesc.fHeight,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001095 glTexDesc.fTextureID,
1096 &glRTDesc)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001097 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001098 return return_null_texture();
1099 }
bsalomon@google.com80d09b92011-11-05 21:21:13 +00001100 tex = new GrGLTexture(this, glTexDesc, glRTDesc);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001101 } else {
bsalomon@google.com80d09b92011-11-05 21:21:13 +00001102 tex = new GrGLTexture(this, glTexDesc);
reed@google.comac10a2d2010-12-22 21:39:39 +00001103 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001104 tex->setCachedTexParams(initialTexParams, this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00001105#ifdef TRACE_TEXTURE_CREATION
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001106 GrPrintf("--- new texture [%d] size=(%d %d) config=%d\n",
1107 glTexDesc.fTextureID, desc.fWidth, desc.fHeight, desc.fConfig);
reed@google.comac10a2d2010-12-22 21:39:39 +00001108#endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001109 return tex;
1110}
1111
1112namespace {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001113
1114const GrGLuint kUnknownBitCount = GrGLStencilBuffer::kUnknownBitCount;
1115
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001116void inline get_stencil_rb_sizes(const GrGLInterface* gl,
1117 GrGLuint rb,
1118 GrGLStencilBuffer::Format* format) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001119 // we shouldn't ever know one size and not the other
1120 GrAssert((kUnknownBitCount == format->fStencilBits) ==
1121 (kUnknownBitCount == format->fTotalBits));
1122 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001123 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001124 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1125 (GrGLint*)&format->fStencilBits);
1126 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001127 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001128 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1129 (GrGLint*)&format->fTotalBits);
1130 format->fTotalBits += format->fStencilBits;
1131 } else {
1132 format->fTotalBits = format->fStencilBits;
1133 }
1134 }
1135}
1136}
1137
1138bool GrGpuGL::createStencilBufferForRenderTarget(GrRenderTarget* rt,
1139 int width, int height) {
1140
1141 // All internally created RTs are also textures. We don't create
1142 // SBs for a client's standalone RT (that is RT that isnt also a texture).
1143 GrAssert(rt->asTexture());
bsalomon@google.com99621082011-11-15 16:47:16 +00001144 GrAssert(width >= rt->width());
1145 GrAssert(height >= rt->height());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001146
1147 int samples = rt->numSamples();
1148 GrGLuint sbID;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001149 GL_CALL(GenRenderbuffers(1, &sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001150 if (!sbID) {
1151 return false;
1152 }
1153
1154 GrGLStencilBuffer* sb = NULL;
1155
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001156 int stencilFmtCnt = this->glCaps().stencilFormats().count();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001157 for (int i = 0; i < stencilFmtCnt; ++i) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001158 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001159 // we start with the last stencil format that succeeded in hopes
1160 // that we won't go through this loop more than once after the
1161 // first (painful) stencil creation.
1162 int sIdx = (i + fLastSuccessfulStencilFmtIdx) % stencilFmtCnt;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001163 const GrGLCaps::StencilFormat& sFmt =
1164 this->glCaps().stencilFormats()[sIdx];
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001165 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001166 // we do this "if" so that we don't call the multisample
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001167 // version on a GL that doesn't have an MSAA extension.
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001168 bool created;
1169 if (samples > 0) {
1170 created = renderbuffer_storage_msaa(fGLContextInfo,
1171 samples,
1172 sFmt.fInternalFormat,
1173 width, height);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001174 } else {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001175 GL_ALLOC_CALL(this->glInterface(),
1176 RenderbufferStorage(GR_GL_RENDERBUFFER,
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001177 sFmt.fInternalFormat,
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001178 width, height));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001179 created =
1180 (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(this->glInterface()));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001181 }
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001182 if (created) {
1183 // After sized formats we attempt an unsized format and take
1184 // whatever sizes GL gives us. In that case we query for the size.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001185 GrGLStencilBuffer::Format format = sFmt;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001186 get_stencil_rb_sizes(this->glInterface(), sbID, &format);
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001187 sb = new GrGLStencilBuffer(this, sbID, width, height,
1188 samples, format);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001189 if (this->attachStencilBufferToRenderTarget(sb, rt)) {
1190 fLastSuccessfulStencilFmtIdx = sIdx;
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001191 rt->setStencilBuffer(sb);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001192 sb->unref();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001193 return true;
1194 }
1195 sb->abandon(); // otherwise we lose sbID
1196 sb->unref();
1197 }
1198 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001199 GL_CALL(DeleteRenderbuffers(1, &sbID));
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001200 return false;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001201}
1202
1203bool GrGpuGL::attachStencilBufferToRenderTarget(GrStencilBuffer* sb,
1204 GrRenderTarget* rt) {
1205 GrGLRenderTarget* glrt = (GrGLRenderTarget*) rt;
1206
1207 GrGLuint fbo = glrt->renderFBOID();
1208
1209 if (NULL == sb) {
1210 if (NULL != rt->getStencilBuffer()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001211 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001212 GR_GL_STENCIL_ATTACHMENT,
1213 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001214 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001215 GR_GL_DEPTH_ATTACHMENT,
1216 GR_GL_RENDERBUFFER, 0));
1217#if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001218 GrGLenum status;
1219 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001220 GrAssert(GR_GL_FRAMEBUFFER_COMPLETE == status);
1221#endif
1222 }
1223 return true;
1224 } else {
1225 GrGLStencilBuffer* glsb = (GrGLStencilBuffer*) sb;
1226 GrGLuint rb = glsb->renderbufferID();
1227
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001228 fHWBoundRenderTarget = NULL;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001229 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, fbo));
1230 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001231 GR_GL_STENCIL_ATTACHMENT,
1232 GR_GL_RENDERBUFFER, rb));
1233 if (glsb->format().fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001234 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001235 GR_GL_DEPTH_ATTACHMENT,
1236 GR_GL_RENDERBUFFER, rb));
1237 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001238 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001239 GR_GL_DEPTH_ATTACHMENT,
1240 GR_GL_RENDERBUFFER, 0));
reed@google.comac10a2d2010-12-22 21:39:39 +00001241 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001242
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001243 GrGLenum status;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001244 if (!this->glCaps().isColorConfigAndStencilFormatVerified(rt->config(),
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001245 glsb->format())) {
1246 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1247 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001248 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001249 GR_GL_STENCIL_ATTACHMENT,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001250 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001251 if (glsb->format().fPacked) {
1252 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1253 GR_GL_DEPTH_ATTACHMENT,
1254 GR_GL_RENDERBUFFER, 0));
1255 }
1256 return false;
1257 } else {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001258 fGLContextInfo.caps().markColorConfigAndStencilFormatAsVerified(
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001259 rt->config(),
1260 glsb->format());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001261 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001262 }
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001263 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001264 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001265}
1266
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001267////////////////////////////////////////////////////////////////////////////////
1268
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001269GrVertexBuffer* GrGpuGL::onCreateVertexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001270 GrGLuint id;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001271 GL_CALL(GenBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001272 if (id) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001273 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, id));
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001274 fHWGeometryState.fArrayPtrsDirty = true;
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001275 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001276 // make sure driver can allocate memory for this buffer
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001277 GL_ALLOC_CALL(this->glInterface(),
1278 BufferData(GR_GL_ARRAY_BUFFER,
1279 size,
1280 NULL, // data ptr
1281 dynamic ? GR_GL_DYNAMIC_DRAW :
1282 GR_GL_STATIC_DRAW));
1283 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001284 GL_CALL(DeleteBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001285 // deleting bound buffer does implicit bind to 0
1286 fHWGeometryState.fVertexBuffer = NULL;
1287 return NULL;
1288 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001289 GrGLVertexBuffer* vertexBuffer = new GrGLVertexBuffer(this, id,
reed@google.comac10a2d2010-12-22 21:39:39 +00001290 size, dynamic);
1291 fHWGeometryState.fVertexBuffer = vertexBuffer;
1292 return vertexBuffer;
1293 }
1294 return NULL;
1295}
1296
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001297GrIndexBuffer* GrGpuGL::onCreateIndexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001298 GrGLuint id;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001299 GL_CALL(GenBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001300 if (id) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001301 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, id));
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001302 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001303 // make sure driver can allocate memory for this buffer
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001304 GL_ALLOC_CALL(this->glInterface(),
1305 BufferData(GR_GL_ELEMENT_ARRAY_BUFFER,
1306 size,
1307 NULL, // data ptr
1308 dynamic ? GR_GL_DYNAMIC_DRAW :
1309 GR_GL_STATIC_DRAW));
1310 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001311 GL_CALL(DeleteBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001312 // deleting bound buffer does implicit bind to 0
1313 fHWGeometryState.fIndexBuffer = NULL;
1314 return NULL;
1315 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001316 GrIndexBuffer* indexBuffer = new GrGLIndexBuffer(this, id,
reed@google.comac10a2d2010-12-22 21:39:39 +00001317 size, dynamic);
1318 fHWGeometryState.fIndexBuffer = indexBuffer;
1319 return indexBuffer;
1320 }
1321 return NULL;
1322}
1323
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00001324GrPath* GrGpuGL::onCreatePath(const SkPath& inPath) {
1325 GrPath* path = NULL;
1326 if (fCaps.fPathStencilingSupport) {
1327 path = new GrGLPath(this, inPath);
1328 }
1329 return path;
1330}
1331
bsalomon@google.coma3201942012-06-21 19:58:20 +00001332void GrGpuGL::flushScissor() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001333 const GrDrawState& drawState = this->getDrawState();
1334 const GrGLRenderTarget* rt =
1335 static_cast<const GrGLRenderTarget*>(drawState.getRenderTarget());
1336
1337 GrAssert(NULL != rt);
1338 const GrGLIRect& vp = rt->getViewport();
reed@google.comac10a2d2010-12-22 21:39:39 +00001339
bsalomon@google.coma3201942012-06-21 19:58:20 +00001340 if (fScissorState.fEnabled) {
1341 GrGLIRect scissor;
1342 scissor.setRelativeTo(vp,
1343 fScissorState.fRect.fLeft,
1344 fScissorState.fRect.fTop,
1345 fScissorState.fRect.width(),
1346 fScissorState.fRect.height());
1347 // if the scissor fully contains the viewport then we fall through and
1348 // disable the scissor test.
1349 if (!scissor.contains(vp)) {
1350 if (fHWScissorSettings.fRect != scissor) {
1351 scissor.pushToGLScissor(this->glInterface());
1352 fHWScissorSettings.fRect = scissor;
1353 }
1354 if (kYes_TriState != fHWScissorSettings.fEnabled) {
1355 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1356 fHWScissorSettings.fEnabled = kYes_TriState;
1357 }
1358 return;
1359 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001360 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001361 if (kNo_TriState != fHWScissorSettings.fEnabled) {
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001362 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.coma3201942012-06-21 19:58:20 +00001363 fHWScissorSettings.fEnabled = kNo_TriState;
1364 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001365 }
1366}
1367
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001368void GrGpuGL::onClear(const GrIRect* rect, GrColor color) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001369 const GrDrawState& drawState = this->getDrawState();
1370 const GrRenderTarget* rt = drawState.getRenderTarget();
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001371 // parent class should never let us get here with no RT
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001372 GrAssert(NULL != rt);
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001373
bsalomon@google.com74b98712011-11-11 19:46:16 +00001374 GrIRect clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001375 if (NULL != rect) {
1376 // flushScissor expects rect to be clipped to the target.
bsalomon@google.com74b98712011-11-11 19:46:16 +00001377 clippedRect = *rect;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001378 GrIRect rtRect = SkIRect::MakeWH(rt->width(), rt->height());
bsalomon@google.com74b98712011-11-11 19:46:16 +00001379 if (clippedRect.intersect(rtRect)) {
1380 rect = &clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001381 } else {
1382 return;
1383 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001384 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001385 this->flushRenderTarget(rect);
bsalomon@google.coma3201942012-06-21 19:58:20 +00001386 GrAutoTRestore<ScissorState> asr(&fScissorState);
1387 fScissorState.fEnabled = (NULL != rect);
1388 if (fScissorState.fEnabled) {
1389 fScissorState.fRect = *rect;
1390 }
1391 this->flushScissor();
bsalomon@google.com74b98712011-11-11 19:46:16 +00001392
1393 GrGLfloat r, g, b, a;
1394 static const GrGLfloat scale255 = 1.f / 255.f;
1395 a = GrColorUnpackA(color) * scale255;
1396 GrGLfloat scaleRGB = scale255;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001397 if (GrPixelConfigIsUnpremultiplied(rt->config())) {
bsalomon@google.com74b98712011-11-11 19:46:16 +00001398 scaleRGB *= a;
1399 }
1400 r = GrColorUnpackR(color) * scaleRGB;
1401 g = GrColorUnpackG(color) * scaleRGB;
1402 b = GrColorUnpackB(color) * scaleRGB;
1403
1404 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00001405 fHWWriteToColor = kYes_TriState;
bsalomon@google.com74b98712011-11-11 19:46:16 +00001406 GL_CALL(ClearColor(r, g, b, a));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001407 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001408}
1409
bsalomon@google.comedc177d2011-08-05 15:46:40 +00001410void GrGpuGL::clearStencil() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001411 if (NULL == this->getDrawState().getRenderTarget()) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001412 return;
1413 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001414
1415 this->flushRenderTarget(&GrIRect::EmptyIRect());
1416
bsalomon@google.coma3201942012-06-21 19:58:20 +00001417 GrAutoTRestore<ScissorState> asr(&fScissorState);
1418 fScissorState.fEnabled = false;
1419 this->flushScissor();
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001420
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001421 GL_CALL(StencilMask(0xffffffff));
1422 GL_CALL(ClearStencil(0));
1423 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001424 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001425}
1426
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001427void GrGpuGL::clearStencilClip(const GrIRect& rect, bool insideClip) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001428 const GrDrawState& drawState = this->getDrawState();
1429 const GrRenderTarget* rt = drawState.getRenderTarget();
1430 GrAssert(NULL != rt);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001431
1432 // this should only be called internally when we know we have a
1433 // stencil buffer.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001434 GrAssert(NULL != rt->getStencilBuffer());
1435 GrGLint stencilBitCount = rt->getStencilBuffer()->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001436#if 0
reed@google.comac10a2d2010-12-22 21:39:39 +00001437 GrAssert(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00001438 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001439#else
1440 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001441 // ANGLE a partial stencil mask will cause clears to be
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001442 // turned into draws. Our contract on GrDrawTarget says that
1443 // changing the clip between stencil passes may or may not
1444 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00001445 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001446#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001447 GrGLint value;
1448 if (insideClip) {
1449 value = (1 << (stencilBitCount - 1));
1450 } else {
1451 value = 0;
1452 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001453 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.coma3201942012-06-21 19:58:20 +00001454
1455 GrAutoTRestore<ScissorState> asr(&fScissorState);
1456 fScissorState.fEnabled = true;
1457 fScissorState.fRect = rect;
1458 this->flushScissor();
1459
caryclark@google.comcf6285b2012-06-06 12:09:01 +00001460 GL_CALL(StencilMask((uint32_t) clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001461 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001462 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001463 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001464}
1465
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001466void GrGpuGL::onForceRenderTargetFlush() {
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001467 this->flushRenderTarget(&GrIRect::EmptyIRect());
reed@google.comac10a2d2010-12-22 21:39:39 +00001468}
1469
bsalomon@google.comc4364992011-11-07 15:54:49 +00001470bool GrGpuGL::readPixelsWillPayForYFlip(GrRenderTarget* renderTarget,
1471 int left, int top,
1472 int width, int height,
1473 GrPixelConfig config,
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001474 size_t rowBytes) const {
1475 // if GL can do the flip then we'll never pay for it.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001476 if (this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001477 return false;
1478 }
1479
1480 // If we have to do memcpy to handle non-trim rowBytes then we
bsalomon@google.com7107fa72011-11-10 14:54:14 +00001481 // get the flip for free. Otherwise it costs.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001482 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001483 return true;
1484 }
1485 // If we have to do memcpys to handle rowBytes then y-flip is free
1486 // Note the rowBytes might be tight to the passed in data, but if data
1487 // gets clipped in x to the target the rowBytes will no longer be tight.
1488 if (left >= 0 && (left + width) < renderTarget->width()) {
1489 return 0 == rowBytes ||
1490 GrBytesPerPixel(config) * width == rowBytes;
1491 } else {
1492 return false;
1493 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001494}
1495
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001496bool GrGpuGL::onReadPixels(GrRenderTarget* target,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001497 int left, int top,
1498 int width, int height,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001499 GrPixelConfig config,
1500 void* buffer,
1501 size_t rowBytes,
1502 bool invertY) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001503 GrGLenum format;
1504 GrGLenum type;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00001505 if (!this->configToGLFormats(config, false, NULL, &format, &type)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001506 return false;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001507 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001508 size_t bpp = GrBytesPerPixel(config);
1509 if (!adjust_pixel_ops_params(target->width(), target->height(), bpp,
1510 &left, &top, &width, &height,
1511 const_cast<const void**>(&buffer),
1512 &rowBytes)) {
1513 return false;
1514 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001515
bsalomon@google.comc6980972011-11-02 19:57:21 +00001516 // resolve the render target if necessary
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001517 GrGLRenderTarget* tgt = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001518 GrDrawState::AutoRenderTargetRestore artr;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001519 switch (tgt->getResolveType()) {
1520 case GrGLRenderTarget::kCantResolve_ResolveType:
1521 return false;
1522 case GrGLRenderTarget::kAutoResolves_ResolveType:
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001523 artr.set(this->drawState(), target);
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001524 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001525 break;
1526 case GrGLRenderTarget::kCanResolve_ResolveType:
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001527 this->onResolveRenderTarget(tgt);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001528 // we don't track the state of the READ FBO ID.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001529 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1530 tgt->textureFBOID()));
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001531 break;
1532 default:
1533 GrCrash("Unknown resolve type");
reed@google.comac10a2d2010-12-22 21:39:39 +00001534 }
1535
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001536 const GrGLIRect& glvp = tgt->getViewport();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001537
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001538 // the read rect is viewport-relative
1539 GrGLIRect readRect;
1540 readRect.setRelativeTo(glvp, left, top, width, height);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001541
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001542 size_t tightRowBytes = bpp * width;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001543 if (0 == rowBytes) {
1544 rowBytes = tightRowBytes;
1545 }
1546 size_t readDstRowBytes = tightRowBytes;
1547 void* readDst = buffer;
1548
1549 // determine if GL can read using the passed rowBytes or if we need
1550 // a scratch buffer.
1551 SkAutoSMalloc<32 * sizeof(GrColor)> scratch;
1552 if (rowBytes != tightRowBytes) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001553 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.comc6980972011-11-02 19:57:21 +00001554 GrAssert(!(rowBytes % sizeof(GrColor)));
1555 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, rowBytes / sizeof(GrColor)));
1556 readDstRowBytes = rowBytes;
1557 } else {
1558 scratch.reset(tightRowBytes * height);
1559 readDst = scratch.get();
1560 }
1561 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001562 if (!invertY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001563 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 1));
1564 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001565 GL_CALL(ReadPixels(readRect.fLeft, readRect.fBottom,
1566 readRect.fWidth, readRect.fHeight,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001567 format, type, readDst));
1568 if (readDstRowBytes != tightRowBytes) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001569 GrAssert(this->glCaps().packRowLengthSupport());
bsalomon@google.comc6980972011-11-02 19:57:21 +00001570 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
1571 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001572 if (!invertY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001573 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 0));
1574 invertY = true;
1575 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001576
1577 // now reverse the order of the rows, since GL's are bottom-to-top, but our
bsalomon@google.comc6980972011-11-02 19:57:21 +00001578 // API presents top-to-bottom. We must preserve the padding contents. Note
1579 // that the above readPixels did not overwrite the padding.
1580 if (readDst == buffer) {
1581 GrAssert(rowBytes == readDstRowBytes);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001582 if (!invertY) {
1583 scratch.reset(tightRowBytes);
1584 void* tmpRow = scratch.get();
1585 // flip y in-place by rows
1586 const int halfY = height >> 1;
1587 char* top = reinterpret_cast<char*>(buffer);
1588 char* bottom = top + (height - 1) * rowBytes;
1589 for (int y = 0; y < halfY; y++) {
1590 memcpy(tmpRow, top, tightRowBytes);
1591 memcpy(top, bottom, tightRowBytes);
1592 memcpy(bottom, tmpRow, tightRowBytes);
1593 top += rowBytes;
1594 bottom -= rowBytes;
1595 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001596 }
1597 } else {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001598 GrAssert(readDst != buffer); GrAssert(rowBytes != tightRowBytes);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001599 // copy from readDst to buffer while flipping y
caryclark@google.comcf6285b2012-06-06 12:09:01 +00001600 // const int halfY = height >> 1;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001601 const char* src = reinterpret_cast<const char*>(readDst);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001602 char* dst = reinterpret_cast<char*>(buffer);
1603 if (!invertY) {
1604 dst += (height-1) * rowBytes;
1605 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001606 for (int y = 0; y < height; y++) {
1607 memcpy(dst, src, tightRowBytes);
1608 src += readDstRowBytes;
bsalomon@google.comc4364992011-11-07 15:54:49 +00001609 if (invertY) {
1610 dst += rowBytes;
1611 } else {
1612 dst -= rowBytes;
1613 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001614 }
1615 }
1616 return true;
1617}
1618
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001619void GrGpuGL::flushRenderTarget(const GrIRect* bound) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001620
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001621 GrGLRenderTarget* rt =
1622 static_cast<GrGLRenderTarget*>(this->drawState()->getRenderTarget());
1623 GrAssert(NULL != rt);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001624
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001625 if (fHWBoundRenderTarget != rt) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001626 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, rt->renderFBOID()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001627 #if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001628 GrGLenum status;
1629 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001630 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
reed@google.comb9255d52011-06-13 18:54:59 +00001631 GrPrintf("GrGpuGL::flushRenderTarget glCheckFramebufferStatus %x\n", status);
reed@google.comac10a2d2010-12-22 21:39:39 +00001632 }
1633 #endif
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001634 fHWBoundRenderTarget = rt;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001635 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.coma3201942012-06-21 19:58:20 +00001636 if (fHWViewport != vp) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001637 vp.pushToGLViewport(this->glInterface());
bsalomon@google.coma3201942012-06-21 19:58:20 +00001638 fHWViewport = vp;
reed@google.comac10a2d2010-12-22 21:39:39 +00001639 }
1640 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001641 if (NULL == bound || !bound->isEmpty()) {
1642 rt->flagAsNeedingResolve(bound);
1643 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001644}
1645
twiz@google.com0f31ca72011-03-18 17:38:11 +00001646GrGLenum gPrimitiveType2GLMode[] = {
1647 GR_GL_TRIANGLES,
1648 GR_GL_TRIANGLE_STRIP,
1649 GR_GL_TRIANGLE_FAN,
1650 GR_GL_POINTS,
1651 GR_GL_LINES,
1652 GR_GL_LINE_STRIP
reed@google.comac10a2d2010-12-22 21:39:39 +00001653};
1654
bsalomon@google.comd302f142011-03-03 13:54:13 +00001655#define SWAP_PER_DRAW 0
1656
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001657#if SWAP_PER_DRAW
bsalomon@google.comd302f142011-03-03 13:54:13 +00001658 #if GR_MAC_BUILD
1659 #include <AGL/agl.h>
1660 #elif GR_WIN32_BUILD
1661 void SwapBuf() {
1662 DWORD procID = GetCurrentProcessId();
1663 HWND hwnd = GetTopWindow(GetDesktopWindow());
1664 while(hwnd) {
1665 DWORD wndProcID = 0;
1666 GetWindowThreadProcessId(hwnd, &wndProcID);
1667 if(wndProcID == procID) {
1668 SwapBuffers(GetDC(hwnd));
1669 }
1670 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
1671 }
1672 }
1673 #endif
1674#endif
1675
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001676void GrGpuGL::onGpuDrawIndexed(GrPrimitiveType type,
1677 uint32_t startVertex,
1678 uint32_t startIndex,
1679 uint32_t vertexCount,
1680 uint32_t indexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001681 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1682
twiz@google.com0f31ca72011-03-18 17:38:11 +00001683 GrGLvoid* indices = (GrGLvoid*)(sizeof(uint16_t) * startIndex);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001684
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001685 GrAssert(NULL != fHWGeometryState.fIndexBuffer);
1686 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1687
1688 // our setupGeometry better have adjusted this to zero since
1689 // DrawElements always draws from the begining of the arrays for idx 0.
1690 GrAssert(0 == startVertex);
reed@google.comac10a2d2010-12-22 21:39:39 +00001691
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001692 GL_CALL(DrawElements(gPrimitiveType2GLMode[type], indexCount,
1693 GR_GL_UNSIGNED_SHORT, indices));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001694#if SWAP_PER_DRAW
1695 glFlush();
1696 #if GR_MAC_BUILD
1697 aglSwapBuffers(aglGetCurrentContext());
1698 int set_a_break_pt_here = 9;
1699 aglSwapBuffers(aglGetCurrentContext());
1700 #elif GR_WIN32_BUILD
1701 SwapBuf();
1702 int set_a_break_pt_here = 9;
1703 SwapBuf();
1704 #endif
1705#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001706}
1707
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001708void GrGpuGL::onGpuDrawNonIndexed(GrPrimitiveType type,
1709 uint32_t startVertex,
1710 uint32_t vertexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001711 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1712
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001713 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1714
1715 // our setupGeometry better have adjusted this to zero.
1716 // DrawElements doesn't take an offset so we always adjus the startVertex.
1717 GrAssert(0 == startVertex);
1718
1719 // pass 0 for parameter first. We have to adjust gl*Pointer() to
1720 // account for startVertex in the DrawElements case. So we always
1721 // rely on setupGeometry to have accounted for startVertex.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001722 GL_CALL(DrawArrays(gPrimitiveType2GLMode[type], 0, vertexCount));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001723#if SWAP_PER_DRAW
1724 glFlush();
1725 #if GR_MAC_BUILD
1726 aglSwapBuffers(aglGetCurrentContext());
1727 int set_a_break_pt_here = 9;
1728 aglSwapBuffers(aglGetCurrentContext());
1729 #elif GR_WIN32_BUILD
1730 SwapBuf();
1731 int set_a_break_pt_here = 9;
1732 SwapBuf();
1733 #endif
1734#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001735}
1736
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00001737void GrGpuGL::onGpuStencilPath(const GrPath&, GrPathFill) {
1738 GrCrash("Not implemented yet. Should not get here.");
1739}
1740
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001741void GrGpuGL::onResolveRenderTarget(GrRenderTarget* target) {
1742
1743 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(target);
reed@google.comac10a2d2010-12-22 21:39:39 +00001744
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001745 if (rt->needsResolve()) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001746 GrAssert(GrGLCaps::kNone_MSFBOType != this->glCaps().msFBOType());
reed@google.comac10a2d2010-12-22 21:39:39 +00001747 GrAssert(rt->textureFBOID() != rt->renderFBOID());
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001748 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1749 rt->renderFBOID()));
1750 GL_CALL(BindFramebuffer(GR_GL_DRAW_FRAMEBUFFER,
1751 rt->textureFBOID()));
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001752 // make sure we go through flushRenderTarget() since we've modified
1753 // the bound DRAW FBO ID.
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001754 fHWBoundRenderTarget = NULL;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001755 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001756 const GrIRect dirtyRect = rt->getResolveRect();
1757 GrGLIRect r;
bsalomon@google.coma3201942012-06-21 19:58:20 +00001758 r.setRelativeTo(vp, dirtyRect.fLeft, dirtyRect.fTop,
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001759 dirtyRect.width(), dirtyRect.height());
reed@google.comac10a2d2010-12-22 21:39:39 +00001760
bsalomon@google.coma3201942012-06-21 19:58:20 +00001761 GrAutoTRestore<ScissorState> asr;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001762 if (GrGLCaps::kAppleES_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001763 // Apple's extension uses the scissor as the blit bounds.
bsalomon@google.coma3201942012-06-21 19:58:20 +00001764 asr.reset(&fScissorState);
1765 fScissorState.fEnabled = true;
1766 fScissorState.fRect = dirtyRect;
1767 this->flushScissor();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001768 GL_CALL(ResolveMultisampleFramebuffer());
reed@google.comac10a2d2010-12-22 21:39:39 +00001769 } else {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001770 if (GrGLCaps::kDesktopARB_MSFBOType != this->glCaps().msFBOType()) {
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001771 // this respects the scissor during the blit, so disable it.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001772 GrAssert(GrGLCaps::kDesktopEXT_MSFBOType ==
1773 this->glCaps().msFBOType());
bsalomon@google.coma3201942012-06-21 19:58:20 +00001774 asr.reset(&fScissorState);
1775 fScissorState.fEnabled = false;
1776 this->flushScissor();
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001777 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001778 int right = r.fLeft + r.fWidth;
1779 int top = r.fBottom + r.fHeight;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001780 GL_CALL(BlitFramebuffer(r.fLeft, r.fBottom, right, top,
1781 r.fLeft, r.fBottom, right, top,
1782 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001783 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001784 rt->flagAsResolved();
reed@google.comac10a2d2010-12-22 21:39:39 +00001785 }
1786}
1787
bsalomon@google.com411dad02012-06-05 20:24:20 +00001788namespace {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001789
bsalomon@google.com411dad02012-06-05 20:24:20 +00001790GrGLenum gr_to_gl_stencil_func(GrStencilFunc basicFunc) {
1791 static const GrGLenum gTable[] = {
1792 GR_GL_ALWAYS, // kAlways_StencilFunc
1793 GR_GL_NEVER, // kNever_StencilFunc
1794 GR_GL_GREATER, // kGreater_StencilFunc
1795 GR_GL_GEQUAL, // kGEqual_StencilFunc
1796 GR_GL_LESS, // kLess_StencilFunc
1797 GR_GL_LEQUAL, // kLEqual_StencilFunc,
1798 GR_GL_EQUAL, // kEqual_StencilFunc,
1799 GR_GL_NOTEQUAL, // kNotEqual_StencilFunc,
1800 };
1801 GR_STATIC_ASSERT(GR_ARRAY_COUNT(gTable) == kBasicStencilFuncCount);
1802 GR_STATIC_ASSERT(0 == kAlways_StencilFunc);
1803 GR_STATIC_ASSERT(1 == kNever_StencilFunc);
1804 GR_STATIC_ASSERT(2 == kGreater_StencilFunc);
1805 GR_STATIC_ASSERT(3 == kGEqual_StencilFunc);
1806 GR_STATIC_ASSERT(4 == kLess_StencilFunc);
1807 GR_STATIC_ASSERT(5 == kLEqual_StencilFunc);
1808 GR_STATIC_ASSERT(6 == kEqual_StencilFunc);
1809 GR_STATIC_ASSERT(7 == kNotEqual_StencilFunc);
1810 GrAssert((unsigned) basicFunc < kBasicStencilFuncCount);
1811
1812 return gTable[basicFunc];
1813}
1814
1815GrGLenum gr_to_gl_stencil_op(GrStencilOp op) {
1816 static const GrGLenum gTable[] = {
1817 GR_GL_KEEP, // kKeep_StencilOp
1818 GR_GL_REPLACE, // kReplace_StencilOp
1819 GR_GL_INCR_WRAP, // kIncWrap_StencilOp
1820 GR_GL_INCR, // kIncClamp_StencilOp
1821 GR_GL_DECR_WRAP, // kDecWrap_StencilOp
1822 GR_GL_DECR, // kDecClamp_StencilOp
1823 GR_GL_ZERO, // kZero_StencilOp
1824 GR_GL_INVERT, // kInvert_StencilOp
1825 };
1826 GR_STATIC_ASSERT(GR_ARRAY_COUNT(gTable) == kStencilOpCount);
1827 GR_STATIC_ASSERT(0 == kKeep_StencilOp);
1828 GR_STATIC_ASSERT(1 == kReplace_StencilOp);
1829 GR_STATIC_ASSERT(2 == kIncWrap_StencilOp);
1830 GR_STATIC_ASSERT(3 == kIncClamp_StencilOp);
1831 GR_STATIC_ASSERT(4 == kDecWrap_StencilOp);
1832 GR_STATIC_ASSERT(5 == kDecClamp_StencilOp);
1833 GR_STATIC_ASSERT(6 == kZero_StencilOp);
1834 GR_STATIC_ASSERT(7 == kInvert_StencilOp);
1835 GrAssert((unsigned) op < kStencilOpCount);
1836 return gTable[op];
1837}
1838
1839void set_gl_stencil(const GrGLInterface* gl,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001840 const GrStencilSettings& settings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00001841 GrGLenum glFace,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001842 GrStencilSettings::Face grFace) {
1843 GrGLenum glFunc = gr_to_gl_stencil_func(settings.func(grFace));
1844 GrGLenum glFailOp = gr_to_gl_stencil_op(settings.failOp(grFace));
1845 GrGLenum glPassOp = gr_to_gl_stencil_op(settings.passOp(grFace));
1846
1847 GrGLint ref = settings.funcRef(grFace);
1848 GrGLint mask = settings.funcMask(grFace);
1849 GrGLint writeMask = settings.writeMask(grFace);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001850
1851 if (GR_GL_FRONT_AND_BACK == glFace) {
1852 // we call the combined func just in case separate stencil is not
1853 // supported.
1854 GR_GL_CALL(gl, StencilFunc(glFunc, ref, mask));
1855 GR_GL_CALL(gl, StencilMask(writeMask));
1856 GR_GL_CALL(gl, StencilOp(glFailOp, glPassOp, glPassOp));
1857 } else {
1858 GR_GL_CALL(gl, StencilFuncSeparate(glFace, glFunc, ref, mask));
1859 GR_GL_CALL(gl, StencilMaskSeparate(glFace, writeMask));
1860 GR_GL_CALL(gl, StencilOpSeparate(glFace, glFailOp, glPassOp, glPassOp));
1861 }
1862}
1863}
bsalomon@google.comd302f142011-03-03 13:54:13 +00001864
reed@google.comac10a2d2010-12-22 21:39:39 +00001865void GrGpuGL::flushStencil() {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001866 if (fStencilSettings.isDisabled()) {
1867 if (kNo_TriState != fHWStencilTestEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001868 GL_CALL(Disable(GR_GL_STENCIL_TEST));
bsalomon@google.coma3201942012-06-21 19:58:20 +00001869 fHWStencilTestEnabled = kNo_TriState;
1870 }
1871 } else {
1872 if (kYes_TriState != fHWStencilTestEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001873 GL_CALL(Enable(GR_GL_STENCIL_TEST));
bsalomon@google.coma3201942012-06-21 19:58:20 +00001874 fHWStencilTestEnabled = kYes_TriState;
1875 }
1876 }
1877 if (fHWStencilSettings != fStencilSettings) {
1878 if (!fStencilSettings.isDisabled()) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001879 if (this->getCaps().fTwoSidedStencilSupport) {
bsalomon@google.com411dad02012-06-05 20:24:20 +00001880 set_gl_stencil(this->glInterface(),
bsalomon@google.coma3201942012-06-21 19:58:20 +00001881 fStencilSettings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00001882 GR_GL_FRONT,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001883 GrStencilSettings::kFront_Face);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001884 set_gl_stencil(this->glInterface(),
bsalomon@google.coma3201942012-06-21 19:58:20 +00001885 fStencilSettings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00001886 GR_GL_BACK,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001887 GrStencilSettings::kBack_Face);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001888 } else {
bsalomon@google.com411dad02012-06-05 20:24:20 +00001889 set_gl_stencil(this->glInterface(),
bsalomon@google.coma3201942012-06-21 19:58:20 +00001890 fStencilSettings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00001891 GR_GL_FRONT_AND_BACK,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001892 GrStencilSettings::kFront_Face);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001893 }
1894 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001895 fHWStencilSettings = fStencilSettings;
reed@google.comac10a2d2010-12-22 21:39:39 +00001896 }
1897}
1898
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00001899void GrGpuGL::flushAAState(bool isLines) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001900 const GrRenderTarget* rt = this->getDrawState().getRenderTarget();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001901 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001902 // ES doesn't support toggling GL_MULTISAMPLE and doesn't have
1903 // smooth lines.
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001904 // we prefer smooth lines over multisampled lines
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00001905 bool smoothLines = false;
1906
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00001907 if (isLines) {
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00001908 smoothLines = this->willUseHWAALines();
1909 if (smoothLines) {
1910 if (kYes_TriState != fHWAAState.fSmoothLineEnabled) {
1911 GL_CALL(Enable(GR_GL_LINE_SMOOTH));
1912 fHWAAState.fSmoothLineEnabled = kYes_TriState;
1913 // must disable msaa to use line smoothing
1914 if (rt->isMultisampled() &&
1915 kNo_TriState != fHWAAState.fMSAAEnabled) {
1916 GL_CALL(Disable(GR_GL_MULTISAMPLE));
1917 fHWAAState.fMSAAEnabled = kNo_TriState;
1918 }
1919 }
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001920 } else {
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00001921 if (kNo_TriState != fHWAAState.fSmoothLineEnabled) {
1922 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
1923 fHWAAState.fSmoothLineEnabled = kNo_TriState;
1924 }
1925 }
1926 }
1927 if (!smoothLines && rt->isMultisampled()) {
1928 if (this->getDrawState().isHWAntialiasState()) {
1929 if (kYes_TriState != fHWAAState.fMSAAEnabled) {
1930 GL_CALL(Enable(GR_GL_MULTISAMPLE));
1931 fHWAAState.fMSAAEnabled = kYes_TriState;
1932 }
1933 } else {
1934 if (kNo_TriState != fHWAAState.fMSAAEnabled) {
1935 GL_CALL(Disable(GR_GL_MULTISAMPLE));
1936 fHWAAState.fMSAAEnabled = kNo_TriState;
1937 }
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001938 }
1939 }
1940 }
1941}
1942
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00001943void GrGpuGL::flushBlend(bool isLines,
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001944 GrBlendCoeff srcCoeff,
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001945 GrBlendCoeff dstCoeff) {
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00001946 if (isLines && this->willUseHWAALines()) {
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001947 if (kYes_TriState != fHWBlendState.fEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001948 GL_CALL(Enable(GR_GL_BLEND));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001949 fHWBlendState.fEnabled = kYes_TriState;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001950 }
bsalomon@google.com47059542012-06-06 20:51:20 +00001951 if (kSA_GrBlendCoeff != fHWBlendState.fSrcCoeff ||
1952 kISA_GrBlendCoeff != fHWBlendState.fDstCoeff) {
1953 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[kSA_GrBlendCoeff],
1954 gXfermodeCoeff2Blend[kISA_GrBlendCoeff]));
1955 fHWBlendState.fSrcCoeff = kSA_GrBlendCoeff;
1956 fHWBlendState.fDstCoeff = kISA_GrBlendCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001957 }
1958 } else {
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001959 // any optimization to disable blending should
1960 // have already been applied and tweaked the coeffs
1961 // to (1, 0).
bsalomon@google.com47059542012-06-06 20:51:20 +00001962 bool blendOff = kOne_GrBlendCoeff == srcCoeff &&
1963 kZero_GrBlendCoeff == dstCoeff;
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001964 if (blendOff) {
1965 if (kNo_TriState != fHWBlendState.fEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001966 GL_CALL(Disable(GR_GL_BLEND));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001967 fHWBlendState.fEnabled = kNo_TriState;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001968 }
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001969 } else {
1970 if (kYes_TriState != fHWBlendState.fEnabled) {
1971 GL_CALL(Enable(GR_GL_BLEND));
1972 fHWBlendState.fEnabled = kYes_TriState;
1973 }
1974 if (fHWBlendState.fSrcCoeff != srcCoeff ||
1975 fHWBlendState.fDstCoeff != dstCoeff) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001976 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
1977 gXfermodeCoeff2Blend[dstCoeff]));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001978 fHWBlendState.fSrcCoeff = srcCoeff;
1979 fHWBlendState.fDstCoeff = dstCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001980 }
bsalomon@google.coma5d056a2012-03-27 15:59:58 +00001981 GrColor blendConst = this->getDrawState().getBlendConstant();
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001982 if ((BlendCoeffReferencesConstant(srcCoeff) ||
1983 BlendCoeffReferencesConstant(dstCoeff)) &&
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001984 (!fHWBlendState.fConstColorValid ||
1985 fHWBlendState.fConstColor != blendConst)) {
bsalomon@google.com0650e812011-04-08 18:07:53 +00001986
1987 float c[] = {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001988 GrColorUnpackR(blendConst) / 255.f,
1989 GrColorUnpackG(blendConst) / 255.f,
1990 GrColorUnpackB(blendConst) / 255.f,
1991 GrColorUnpackA(blendConst) / 255.f
bsalomon@google.com0650e812011-04-08 18:07:53 +00001992 };
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001993 GL_CALL(BlendColor(c[0], c[1], c[2], c[3]));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001994 fHWBlendState.fConstColor = blendConst;
1995 fHWBlendState.fConstColorValid = true;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001996 }
1997 }
1998 }
1999}
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002000namespace {
2001
2002unsigned gr_to_gl_filter(GrSamplerState::Filter filter) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00002003 switch (filter) {
2004 case GrSamplerState::kBilinear_Filter:
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00002005 return GR_GL_LINEAR;
2006 case GrSamplerState::kNearest_Filter:
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00002007 return GR_GL_NEAREST;
2008 default:
2009 GrAssert(!"Unknown filter type");
2010 return GR_GL_LINEAR;
2011 }
2012}
2013
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002014// get_swizzle is only called from this .cpp so it is OK to inline it here
2015inline const GrGLenum* get_swizzle(GrPixelConfig config,
2016 const GrSamplerState& sampler,
2017 const GrGLCaps& glCaps) {
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002018 if (GrPixelConfigIsAlphaOnly(config)) {
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002019 if (glCaps.textureRedSupport()) {
2020 static const GrGLenum gRedSmear[] = { GR_GL_RED, GR_GL_RED,
2021 GR_GL_RED, GR_GL_RED };
2022 return gRedSmear;
2023 } else {
2024 static const GrGLenum gAlphaSmear[] = { GR_GL_ALPHA, GR_GL_ALPHA,
2025 GR_GL_ALPHA, GR_GL_ALPHA };
2026 return gAlphaSmear;
2027 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002028 } else if (sampler.swapsRAndB()) {
2029 static const GrGLenum gRedBlueSwap[] = { GR_GL_BLUE, GR_GL_GREEN,
2030 GR_GL_RED, GR_GL_ALPHA };
2031 return gRedBlueSwap;
2032 } else {
2033 static const GrGLenum gStraight[] = { GR_GL_RED, GR_GL_GREEN,
2034 GR_GL_BLUE, GR_GL_ALPHA };
2035 return gStraight;
2036 }
2037}
2038
2039void set_tex_swizzle(GrGLenum swizzle[4], const GrGLInterface* gl) {
bsalomon@google.com4d063de2012-05-31 17:59:23 +00002040 GR_GL_CALL(gl, TexParameteriv(GR_GL_TEXTURE_2D,
2041 GR_GL_TEXTURE_SWIZZLE_RGBA,
2042 reinterpret_cast<const GrGLint*>(swizzle)));
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002043}
2044}
2045
bsalomon@google.com4c883782012-06-04 19:05:11 +00002046void GrGpuGL::flushBoundTextureAndParams(int stage) {
2047 GrDrawState* drawState = this->drawState();
2048
2049 GrGLTexture* nextTexture =
2050 static_cast<GrGLTexture*>(drawState->getTexture(stage));
2051
2052 // true for now, but maybe not with GrEffect.
2053 GrAssert(NULL != nextTexture);
2054 // if we created a rt/tex and rendered to it without using a
2055 // texture and now we're texturing from the rt it will still be
2056 // the last bound texture, but it needs resolving. So keep this
2057 // out of the "last != next" check.
2058 GrGLRenderTarget* texRT =
2059 static_cast<GrGLRenderTarget*>(nextTexture->asRenderTarget());
2060 if (NULL != texRT) {
2061 this->onResolveRenderTarget(texRT);
2062 }
2063
2064 if (fHWBoundTextures[stage] != nextTexture) {
2065 this->setTextureUnit(stage);
2066 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, nextTexture->textureID()));
bsalomon@google.com4c883782012-06-04 19:05:11 +00002067 //GrPrintf("---- bindtexture %d\n", nextTexture->textureID());
2068 fHWBoundTextures[stage] = nextTexture;
2069 }
2070
2071 const GrSamplerState& sampler = drawState->getSampler(stage);
2072 ResetTimestamp timestamp;
2073 const GrGLTexture::TexParams& oldTexParams =
2074 nextTexture->getCachedTexParams(&timestamp);
2075 bool setAll = timestamp < this->getResetTimestamp();
2076 GrGLTexture::TexParams newTexParams;
2077
2078 newTexParams.fFilter = gr_to_gl_filter(sampler.getFilter());
2079
2080 const GrGLenum* wraps = GrGLTexture::WrapMode2GLWrap();
2081 newTexParams.fWrapS = wraps[sampler.getWrapX()];
2082 newTexParams.fWrapT = wraps[sampler.getWrapY()];
2083 memcpy(newTexParams.fSwizzleRGBA,
2084 get_swizzle(nextTexture->config(), sampler, this->glCaps()),
2085 sizeof(newTexParams.fSwizzleRGBA));
2086 if (setAll || newTexParams.fFilter != oldTexParams.fFilter) {
2087 this->setTextureUnit(stage);
2088 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2089 GR_GL_TEXTURE_MAG_FILTER,
2090 newTexParams.fFilter));
2091 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2092 GR_GL_TEXTURE_MIN_FILTER,
2093 newTexParams.fFilter));
2094 }
2095 if (setAll || newTexParams.fWrapS != oldTexParams.fWrapS) {
2096 this->setTextureUnit(stage);
2097 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2098 GR_GL_TEXTURE_WRAP_S,
2099 newTexParams.fWrapS));
2100 }
2101 if (setAll || newTexParams.fWrapT != oldTexParams.fWrapT) {
2102 this->setTextureUnit(stage);
2103 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2104 GR_GL_TEXTURE_WRAP_T,
2105 newTexParams.fWrapT));
2106 }
2107 if (this->glCaps().textureSwizzleSupport() &&
2108 (setAll || memcmp(newTexParams.fSwizzleRGBA,
2109 oldTexParams.fSwizzleRGBA,
2110 sizeof(newTexParams.fSwizzleRGBA)))) {
2111 this->setTextureUnit(stage);
2112 set_tex_swizzle(newTexParams.fSwizzleRGBA,
2113 this->glInterface());
2114 }
2115 nextTexture->setCachedTexParams(newTexParams,
2116 this->getResetTimestamp());
2117}
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
2168void GrGpuGL::notifyVertexBufferBind(const GrGLVertexBuffer* buffer) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002169 if (fHWGeometryState.fVertexBuffer != buffer) {
2170 fHWGeometryState.fArrayPtrsDirty = true;
2171 fHWGeometryState.fVertexBuffer = buffer;
2172 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002173}
2174
2175void GrGpuGL::notifyVertexBufferDelete(const GrGLVertexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002176 if (fHWGeometryState.fVertexBuffer == buffer) {
2177 // deleting bound buffer does implied bind to 0
2178 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002179 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00002180 }
2181}
2182
2183void GrGpuGL::notifyIndexBufferBind(const GrGLIndexBuffer* buffer) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002184 fHWGeometryState.fIndexBuffer = buffer;
reed@google.comac10a2d2010-12-22 21:39:39 +00002185}
2186
2187void GrGpuGL::notifyIndexBufferDelete(const GrGLIndexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002188 if (fHWGeometryState.fIndexBuffer == buffer) {
2189 // deleting bound buffer does implied bind to 0
2190 fHWGeometryState.fIndexBuffer = NULL;
2191 }
2192}
2193
reed@google.comac10a2d2010-12-22 21:39:39 +00002194void GrGpuGL::notifyRenderTargetDelete(GrRenderTarget* renderTarget) {
2195 GrAssert(NULL != renderTarget);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002196 GrDrawState* drawState = this->drawState();
2197 if (drawState->getRenderTarget() == renderTarget) {
2198 drawState->setRenderTarget(NULL);
reed@google.comac10a2d2010-12-22 21:39:39 +00002199 }
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002200 if (fHWBoundRenderTarget == renderTarget) {
2201 fHWBoundRenderTarget = NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +00002202 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002203}
2204
2205void GrGpuGL::notifyTextureDelete(GrGLTexture* texture) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002206 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002207 GrDrawState* drawState = this->drawState();
2208 if (drawState->getTexture(s) == texture) {
bsalomon@google.coma5d056a2012-03-27 15:59:58 +00002209 this->drawState()->setTexture(s, NULL);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002210 }
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002211 if (fHWBoundTextures[s] == texture) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002212 // deleting bound texture does implied bind to 0
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002213 fHWBoundTextures[s] = NULL;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002214 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002215 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002216}
2217
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002218bool GrGpuGL::configToGLFormats(GrPixelConfig config,
2219 bool getSizedInternalFormat,
2220 GrGLenum* internalFormat,
2221 GrGLenum* externalFormat,
2222 GrGLenum* externalType) {
2223 GrGLenum dontCare;
2224 if (NULL == internalFormat) {
2225 internalFormat = &dontCare;
2226 }
2227 if (NULL == externalFormat) {
2228 externalFormat = &dontCare;
2229 }
2230 if (NULL == externalType) {
2231 externalType = &dontCare;
2232 }
2233
reed@google.comac10a2d2010-12-22 21:39:39 +00002234 switch (config) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002235 case kRGBA_8888_PM_GrPixelConfig:
2236 case kRGBA_8888_UPM_GrPixelConfig:
bsalomon@google.comc4364992011-11-07 15:54:49 +00002237 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002238 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002239 if (getSizedInternalFormat) {
2240 *internalFormat = GR_GL_RGBA8;
2241 } else {
2242 *internalFormat = GR_GL_RGBA;
2243 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002244 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.comc4364992011-11-07 15:54:49 +00002245 break;
2246 case kBGRA_8888_PM_GrPixelConfig:
2247 case kBGRA_8888_UPM_GrPixelConfig:
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002248 if (!this->glCaps().bgraFormatSupport()) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002249 return false;
2250 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002251 if (this->glCaps().bgraIsInternalFormat()) {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002252 if (getSizedInternalFormat) {
2253 *internalFormat = GR_GL_BGRA8;
2254 } else {
2255 *internalFormat = GR_GL_BGRA;
2256 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002257 } else {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002258 if (getSizedInternalFormat) {
2259 *internalFormat = GR_GL_RGBA8;
2260 } else {
2261 *internalFormat = GR_GL_RGBA;
2262 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002263 }
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002264 *externalFormat = GR_GL_BGRA;
bsalomon@google.com6f379512011-11-16 20:36:03 +00002265 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002266 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002267 case kRGB_565_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002268 *internalFormat = GR_GL_RGB;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002269 *externalFormat = GR_GL_RGB;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002270 if (getSizedInternalFormat) {
2271 if (this->glBinding() == kDesktop_GrGLBinding) {
2272 return false;
2273 } else {
2274 *internalFormat = GR_GL_RGB565;
2275 }
2276 } else {
2277 *internalFormat = GR_GL_RGB;
2278 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002279 *externalType = GR_GL_UNSIGNED_SHORT_5_6_5;
reed@google.comac10a2d2010-12-22 21:39:39 +00002280 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002281 case kRGBA_4444_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002282 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002283 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002284 if (getSizedInternalFormat) {
2285 *internalFormat = GR_GL_RGBA4;
2286 } else {
2287 *internalFormat = GR_GL_RGBA;
2288 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002289 *externalType = GR_GL_UNSIGNED_SHORT_4_4_4_4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002290 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002291 case kIndex_8_GrPixelConfig:
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002292 if (this->getCaps().f8BitPaletteSupport) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002293 *internalFormat = GR_GL_PALETTE8_RGBA8;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002294 // glCompressedTexImage doesn't take external params
2295 *externalFormat = GR_GL_PALETTE8_RGBA8;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002296 // no sized/unsized internal format distinction here
2297 *internalFormat = GR_GL_PALETTE8_RGBA8;
2298 // unused with CompressedTexImage
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002299 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002300 } else {
2301 return false;
2302 }
2303 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002304 case kAlpha_8_GrPixelConfig:
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002305 if (this->glCaps().textureRedSupport()) {
2306 *internalFormat = GR_GL_RED;
2307 *externalFormat = GR_GL_RED;
2308 if (getSizedInternalFormat) {
2309 *internalFormat = GR_GL_R8;
2310 } else {
2311 *internalFormat = GR_GL_RED;
2312 }
2313 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002314 } else {
2315 *internalFormat = GR_GL_ALPHA;
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002316 *externalFormat = GR_GL_ALPHA;
2317 if (getSizedInternalFormat) {
2318 *internalFormat = GR_GL_ALPHA8;
2319 } else {
2320 *internalFormat = GR_GL_ALPHA;
2321 }
2322 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002323 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002324 break;
2325 default:
2326 return false;
2327 }
2328 return true;
2329}
2330
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002331void GrGpuGL::setTextureUnit(int unit) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002332 GrAssert(unit >= 0 && unit < GrDrawState::kNumStages);
bsalomon@google.com49209392012-06-05 15:13:46 +00002333 if (fHWActiveTextureUnitIdx != unit) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002334 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com49209392012-06-05 15:13:46 +00002335 fHWActiveTextureUnitIdx = unit;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002336 }
2337}
bsalomon@google.com316f99232011-01-13 21:28:12 +00002338
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002339void GrGpuGL::setSpareTextureUnit() {
bsalomon@google.com49209392012-06-05 15:13:46 +00002340 if (fHWActiveTextureUnitIdx != (GR_GL_TEXTURE0 + SPARE_TEX_UNIT)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002341 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + SPARE_TEX_UNIT));
bsalomon@google.com49209392012-06-05 15:13:46 +00002342 fHWActiveTextureUnitIdx = SPARE_TEX_UNIT;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002343 }
2344}
2345
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002346void GrGpuGL::setBuffers(bool indexed,
2347 int* extraVertexOffset,
2348 int* extraIndexOffset) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002349
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002350 GrAssert(NULL != extraVertexOffset);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002351
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002352 const GeometryPoolState& geoPoolState = this->getGeomPoolState();
2353
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002354 GrGLVertexBuffer* vbuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002355 switch (this->getGeomSrc().fVertexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002356 case kBuffer_GeometrySrcType:
2357 *extraVertexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002358 vbuf = (GrGLVertexBuffer*) this->getGeomSrc().fVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002359 break;
2360 case kArray_GeometrySrcType:
2361 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002362 this->finalizeReservedVertices();
2363 *extraVertexOffset = geoPoolState.fPoolStartVertex;
2364 vbuf = (GrGLVertexBuffer*) geoPoolState.fPoolVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002365 break;
2366 default:
2367 vbuf = NULL; // suppress warning
2368 GrCrash("Unknown geometry src type!");
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002369 }
2370
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002371 GrAssert(NULL != vbuf);
2372 GrAssert(!vbuf->isLocked());
2373 if (fHWGeometryState.fVertexBuffer != vbuf) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002374 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, vbuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002375 fHWGeometryState.fArrayPtrsDirty = true;
2376 fHWGeometryState.fVertexBuffer = vbuf;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002377 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002378
2379 if (indexed) {
2380 GrAssert(NULL != extraIndexOffset);
2381
2382 GrGLIndexBuffer* ibuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002383 switch (this->getGeomSrc().fIndexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002384 case kBuffer_GeometrySrcType:
2385 *extraIndexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002386 ibuf = (GrGLIndexBuffer*)this->getGeomSrc().fIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002387 break;
2388 case kArray_GeometrySrcType:
2389 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002390 this->finalizeReservedIndices();
2391 *extraIndexOffset = geoPoolState.fPoolStartIndex;
2392 ibuf = (GrGLIndexBuffer*) geoPoolState.fPoolIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002393 break;
2394 default:
2395 ibuf = NULL; // suppress warning
2396 GrCrash("Unknown geometry src type!");
2397 }
2398
2399 GrAssert(NULL != ibuf);
2400 GrAssert(!ibuf->isLocked());
2401 if (fHWGeometryState.fIndexBuffer != ibuf) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002402 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, ibuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002403 fHWGeometryState.fIndexBuffer = ibuf;
2404 }
2405 }
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002406}
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +00002407