blob: a0121d7d82bbeddc24b32e843de77471fb0705f2 [file] [log] [blame]
reed@google.comac10a2d2010-12-22 21:39:39 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
reed@google.comac10a2d2010-12-22 21:39:39 +00006 */
7
epoger@google.comec3ed6a2011-07-28 14:26:00 +00008
reed@google.comac10a2d2010-12-22 21:39:39 +00009#include "GrGpuGL.h"
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000010#include "GrGLStencilBuffer.h"
bsalomon@google.com64aef2b2012-06-11 15:36:13 +000011#include "GrGLPath.h"
bsalomon@google.com6d003d12012-09-11 15:45:20 +000012#include "GrGLShaderBuilder.h"
bsalomon@google.coma3201942012-06-21 19:58:20 +000013#include "GrTemplates.h"
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +000014#include "GrTypes.h"
bsalomon@google.com3582bf92011-06-30 21:32:31 +000015#include "SkTemplates.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000016
caryclark@google.comcf6285b2012-06-06 12:09:01 +000017static const GrGLuint GR_MAX_GLUINT = ~0U;
twiz@google.com0f31ca72011-03-18 17:38:11 +000018static const GrGLint GR_INVAL_GLINT = ~0;
reed@google.comac10a2d2010-12-22 21:39:39 +000019
bsalomon@google.com0b77d682011-08-19 13:28:54 +000020#define GL_CALL(X) GR_GL_CALL(this->glInterface(), X)
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +000021#define GL_CALL_RET(RET, X) GR_GL_CALL_RET(this->glInterface(), RET, X)
bsalomon@google.com0b77d682011-08-19 13:28:54 +000022
bsalomon@google.com316f99232011-01-13 21:28:12 +000023// we use a spare texture unit to avoid
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000024// mucking with the state of any of the stages.
tomhudson@google.com93813632011-10-27 20:21:16 +000025static const int SPARE_TEX_UNIT = GrDrawState::kNumStages;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000026
reed@google.comac10a2d2010-12-22 21:39:39 +000027#define SKIP_CACHE_CHECK true
28
bsalomon@google.com4f3c2532012-01-19 16:16:52 +000029#if GR_GL_CHECK_ALLOC_WITH_GET_ERROR
30 #define CLEAR_ERROR_BEFORE_ALLOC(iface) GrGLClearErr(iface)
31 #define GL_ALLOC_CALL(iface, call) GR_GL_CALL_NOERRCHECK(iface, call)
32 #define CHECK_ALLOC_ERROR(iface) GR_GL_GET_ERROR(iface)
rmistry@google.comfbfcd562012-08-23 18:09:54 +000033#else
bsalomon@google.com4f3c2532012-01-19 16:16:52 +000034 #define CLEAR_ERROR_BEFORE_ALLOC(iface)
35 #define GL_ALLOC_CALL(iface, call) GR_GL_CALL(iface, call)
36 #define CHECK_ALLOC_ERROR(iface) GR_GL_NO_ERROR
37#endif
38
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +000039
40///////////////////////////////////////////////////////////////////////////////
41
twiz@google.com0f31ca72011-03-18 17:38:11 +000042static const GrGLenum gXfermodeCoeff2Blend[] = {
43 GR_GL_ZERO,
44 GR_GL_ONE,
45 GR_GL_SRC_COLOR,
46 GR_GL_ONE_MINUS_SRC_COLOR,
47 GR_GL_DST_COLOR,
48 GR_GL_ONE_MINUS_DST_COLOR,
49 GR_GL_SRC_ALPHA,
50 GR_GL_ONE_MINUS_SRC_ALPHA,
51 GR_GL_DST_ALPHA,
52 GR_GL_ONE_MINUS_DST_ALPHA,
53 GR_GL_CONSTANT_COLOR,
54 GR_GL_ONE_MINUS_CONSTANT_COLOR,
55 GR_GL_CONSTANT_ALPHA,
56 GR_GL_ONE_MINUS_CONSTANT_ALPHA,
bsalomon@google.com271cffc2011-05-20 14:13:56 +000057
58 // extended blend coeffs
59 GR_GL_SRC1_COLOR,
60 GR_GL_ONE_MINUS_SRC1_COLOR,
61 GR_GL_SRC1_ALPHA,
62 GR_GL_ONE_MINUS_SRC1_ALPHA,
reed@google.comac10a2d2010-12-22 21:39:39 +000063};
64
bsalomon@google.com271cffc2011-05-20 14:13:56 +000065bool GrGpuGL::BlendCoeffReferencesConstant(GrBlendCoeff coeff) {
bsalomon@google.com080773c2011-03-15 19:09:25 +000066 static const bool gCoeffReferencesBlendConst[] = {
67 false,
68 false,
69 false,
70 false,
71 false,
72 false,
73 false,
74 false,
75 false,
76 false,
77 true,
78 true,
79 true,
80 true,
bsalomon@google.com271cffc2011-05-20 14:13:56 +000081
82 // extended blend coeffs
83 false,
84 false,
85 false,
86 false,
bsalomon@google.com080773c2011-03-15 19:09:25 +000087 };
88 return gCoeffReferencesBlendConst[coeff];
bsalomon@google.com47059542012-06-06 20:51:20 +000089 GR_STATIC_ASSERT(kTotalGrBlendCoeffCount ==
90 GR_ARRAY_COUNT(gCoeffReferencesBlendConst));
bsalomon@google.com271cffc2011-05-20 14:13:56 +000091
bsalomon@google.com47059542012-06-06 20:51:20 +000092 GR_STATIC_ASSERT(0 == kZero_GrBlendCoeff);
93 GR_STATIC_ASSERT(1 == kOne_GrBlendCoeff);
94 GR_STATIC_ASSERT(2 == kSC_GrBlendCoeff);
95 GR_STATIC_ASSERT(3 == kISC_GrBlendCoeff);
96 GR_STATIC_ASSERT(4 == kDC_GrBlendCoeff);
97 GR_STATIC_ASSERT(5 == kIDC_GrBlendCoeff);
98 GR_STATIC_ASSERT(6 == kSA_GrBlendCoeff);
99 GR_STATIC_ASSERT(7 == kISA_GrBlendCoeff);
100 GR_STATIC_ASSERT(8 == kDA_GrBlendCoeff);
101 GR_STATIC_ASSERT(9 == kIDA_GrBlendCoeff);
102 GR_STATIC_ASSERT(10 == kConstC_GrBlendCoeff);
103 GR_STATIC_ASSERT(11 == kIConstC_GrBlendCoeff);
104 GR_STATIC_ASSERT(12 == kConstA_GrBlendCoeff);
105 GR_STATIC_ASSERT(13 == kIConstA_GrBlendCoeff);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000106
bsalomon@google.com47059542012-06-06 20:51:20 +0000107 GR_STATIC_ASSERT(14 == kS2C_GrBlendCoeff);
108 GR_STATIC_ASSERT(15 == kIS2C_GrBlendCoeff);
109 GR_STATIC_ASSERT(16 == kS2A_GrBlendCoeff);
110 GR_STATIC_ASSERT(17 == kIS2A_GrBlendCoeff);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000111
112 // assertion for gXfermodeCoeff2Blend have to be in GrGpu scope
bsalomon@google.com47059542012-06-06 20:51:20 +0000113 GR_STATIC_ASSERT(kTotalGrBlendCoeffCount ==
114 GR_ARRAY_COUNT(gXfermodeCoeff2Blend));
bsalomon@google.com080773c2011-03-15 19:09:25 +0000115}
116
reed@google.comac10a2d2010-12-22 21:39:39 +0000117///////////////////////////////////////////////////////////////////////////////
118
rileya@google.come38160c2012-07-03 18:03:04 +0000119static bool gPrintStartupSpew;
bsalomon@google.com42ab7ea2011-01-19 17:19:40 +0000120
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000121static bool fbo_test(const GrGLInterface* gl, int w, int h) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000122
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000123 GR_GL_CALL(gl, ActiveTexture(GR_GL_TEXTURE0 + SPARE_TEX_UNIT));
bsalomon@google.com316f99232011-01-13 21:28:12 +0000124
twiz@google.com0f31ca72011-03-18 17:38:11 +0000125 GrGLuint testFBO;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000126 GR_GL_CALL(gl, GenFramebuffers(1, &testFBO));
127 GR_GL_CALL(gl, BindFramebuffer(GR_GL_FRAMEBUFFER, testFBO));
twiz@google.com0f31ca72011-03-18 17:38:11 +0000128 GrGLuint testRTTex;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000129 GR_GL_CALL(gl, GenTextures(1, &testRTTex));
130 GR_GL_CALL(gl, BindTexture(GR_GL_TEXTURE_2D, testRTTex));
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +0000131 // some implementations require texture to be mip-map complete before
132 // FBO with level 0 bound as color attachment will be framebuffer complete.
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000133 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000134 GR_GL_TEXTURE_MIN_FILTER,
135 GR_GL_NEAREST));
136 GR_GL_CALL(gl, TexImage2D(GR_GL_TEXTURE_2D, 0, GR_GL_RGBA, w, h,
137 0, GR_GL_RGBA, GR_GL_UNSIGNED_BYTE, NULL));
138 GR_GL_CALL(gl, BindTexture(GR_GL_TEXTURE_2D, 0));
139 GR_GL_CALL(gl, FramebufferTexture2D(GR_GL_FRAMEBUFFER,
140 GR_GL_COLOR_ATTACHMENT0,
141 GR_GL_TEXTURE_2D, testRTTex, 0));
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000142 GrGLenum status;
143 GR_GL_CALL_RET(gl, status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000144 GR_GL_CALL(gl, DeleteFramebuffers(1, &testFBO));
145 GR_GL_CALL(gl, DeleteTextures(1, &testRTTex));
bsalomon@google.com316f99232011-01-13 21:28:12 +0000146
bsalomon@google.comc312bf92011-03-21 21:10:33 +0000147 return status == GR_GL_FRAMEBUFFER_COMPLETE;
reed@google.comac10a2d2010-12-22 21:39:39 +0000148}
149
robertphillips@google.com6177e692013-02-28 20:16:25 +0000150GrGpuGL::GrGpuGL(const GrGLContext& ctx, GrContext* context)
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000151 : GrGpu(context)
robertphillips@google.com6177e692013-02-28 20:16:25 +0000152 , fGLContext(ctx) {
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000153
robertphillips@google.com6177e692013-02-28 20:16:25 +0000154 GrAssert(ctx.isInitialized());
tomhudson@google.com747bf292011-06-14 18:16:52 +0000155
bsalomon@google.combcce8922013-03-25 15:38:39 +0000156 fCaps.reset(SkRef(ctx.info().caps()));
157
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000158 fillInConfigRenderableTable();
159
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000160
robertphillips@google.com6177e692013-02-28 20:16:25 +0000161 GrGLClearErr(fGLContext.interface());
reed@google.comac10a2d2010-12-22 21:39:39 +0000162
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000163 if (gPrintStartupSpew) {
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000164 const GrGLubyte* ext;
165 GL_CALL_RET(ext, GetString(GR_GL_EXTENSIONS));
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000166 const GrGLubyte* vendor;
167 const GrGLubyte* renderer;
168 const GrGLubyte* version;
169 GL_CALL_RET(vendor, GetString(GR_GL_VENDOR));
170 GL_CALL_RET(renderer, GetString(GR_GL_RENDERER));
171 GL_CALL_RET(version, GetString(GR_GL_VERSION));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000172 GrPrintf("------------------------- create GrGpuGL %p --------------\n",
173 this);
174 GrPrintf("------ VENDOR %s\n", vendor);
175 GrPrintf("------ RENDERER %s\n", renderer);
176 GrPrintf("------ VERSION %s\n", version);
177 GrPrintf("------ EXTENSIONS\n %s \n", ext);
bsalomon@google.combcce8922013-03-25 15:38:39 +0000178 ctx.info().caps()->print();
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000179 }
180
robertphillips@google.com6177e692013-02-28 20:16:25 +0000181 fProgramCache = SkNEW_ARGS(ProgramCache, (this->glContext()));
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000182
jvanverth@google.com054ae992013-04-01 20:06:51 +0000183 GrAssert(this->glCaps().maxVertexAttributes() >= GrDrawState::kMaxVertexAttribCnt);
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000184
bsalomon@google.comfe676522011-06-17 18:12:21 +0000185 fLastSuccessfulStencilFmtIdx = 0;
caryclark@google.comcf6285b2012-06-06 12:09:01 +0000186 if (false) { // avoid bit rot, suppress warning
187 fbo_test(this->glInterface(), 0, 0);
188 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000189}
190
191GrGpuGL::~GrGpuGL() {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000192 if (0 != fHWProgramID) {
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000193 // detach the current program so there is no confusion on OpenGL's part
194 // that we want it to be deleted
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +0000195 GrAssert(fHWProgramID == fCurrentProgram->programID());
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000196 GL_CALL(UseProgram(0));
197 }
198
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000199 delete fProgramCache;
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000200
bsalomon@google.com4a018bb2011-10-28 19:50:21 +0000201 // This must be called by before the GrDrawTarget destructor
202 this->releaseGeometry();
bsalomon@google.com15b11df2011-09-16 21:18:29 +0000203 // This subclass must do this before the base class destructor runs
204 // since we will unref the GrGLInterface.
205 this->releaseResources();
reed@google.comac10a2d2010-12-22 21:39:39 +0000206}
207
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000208///////////////////////////////////////////////////////////////////////////////
209
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000210void GrGpuGL::fillInConfigRenderableTable() {
211
212 // OpenGL < 3.0
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000213 // no support for render targets unless the GL_ARB_framebuffer_object
214 // extension is supported (in which case we get ALPHA, RED, RG, RGB,
215 // RGBA (ALPHA8, RGBA4, RGBA8) for OpenGL > 1.1). Note that we
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000216 // probably don't get R8 in this case.
217
218 // OpenGL 3.0
219 // base color renderable: ALPHA, RED, RG, RGB, and RGBA
220 // sized derivatives: ALPHA8, R8, RGBA4, RGBA8
221
222 // >= OpenGL 3.1
223 // base color renderable: RED, RG, RGB, and RGBA
224 // sized derivatives: R8, RGBA4, RGBA8
225 // if the GL_ARB_compatibility extension is supported then we get back
226 // support for GL_ALPHA and ALPHA8
227
228 // GL_EXT_bgra adds BGRA render targets to any version
229
230 // ES 2.0
231 // color renderable: RGBA4, RGB5_A1, RGB565
232 // GL_EXT_texture_rg adds support for R8 as a color render target
233 // GL_OES_rgb8_rgba8 and/or GL_ARM_rgba8 adds support for RGBA8
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000234 // GL_EXT_texture_format_BGRA8888 and/or GL_APPLE_texture_format_BGRA8888 added BGRA support
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000235
236 if (kDesktop_GrGLBinding == this->glBinding()) {
237 // Post 3.0 we will get R8
238 // Prior to 3.0 we will get ALPHA8 (with GL_ARB_framebuffer_object)
239 if (this->glVersion() >= GR_GL_VER(3,0) ||
240 this->hasExtension("GL_ARB_framebuffer_object")) {
241 fConfigRenderSupport[kAlpha_8_GrPixelConfig] = true;
242 }
243 } else {
244 // On ES we can only hope for R8
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000245 fConfigRenderSupport[kAlpha_8_GrPixelConfig] =
robertphillips@google.com443e5a52012-04-30 20:01:21 +0000246 this->glCaps().textureRedSupport();
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000247 }
248
249 if (kDesktop_GrGLBinding != this->glBinding()) {
250 // only available in ES
251 fConfigRenderSupport[kRGB_565_GrPixelConfig] = true;
252 }
253
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000254 // Pre 3.0, Ganesh relies on either GL_ARB_framebuffer_object or
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000255 // GL_EXT_framebuffer_object for FBO support. Both of these
256 // allow RGBA4 render targets so this is always supported.
257 fConfigRenderSupport[kRGBA_4444_GrPixelConfig] = true;
258
259 if (this->glCaps().rgba8RenderbufferSupport()) {
bsalomon@google.com0342a852012-08-20 19:22:38 +0000260 fConfigRenderSupport[kRGBA_8888_GrPixelConfig] = true;
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000261 }
262
263 if (this->glCaps().bgraFormatSupport()) {
bsalomon@google.com0342a852012-08-20 19:22:38 +0000264 fConfigRenderSupport[kBGRA_8888_GrPixelConfig] = true;
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000265 }
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000266}
267
bsalomon@google.com9c680582013-02-06 18:17:50 +0000268namespace {
269GrPixelConfig preferred_pixel_ops_config(GrPixelConfig config) {
270 if (GR_GL_RGBA_8888_PIXEL_OPS_SLOW && kRGBA_8888_GrPixelConfig == config) {
271 return kBGRA_8888_GrPixelConfig;
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000272 } else {
273 return config;
274 }
275}
bsalomon@google.com9c680582013-02-06 18:17:50 +0000276}
277
278GrPixelConfig GrGpuGL::preferredReadPixelsConfig(GrPixelConfig config) const {
279 return preferred_pixel_ops_config(config);
280}
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000281
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000282GrPixelConfig GrGpuGL::preferredWritePixelsConfig(GrPixelConfig config) const {
bsalomon@google.com9c680582013-02-06 18:17:50 +0000283 return preferred_pixel_ops_config(config);
284}
285
286bool GrGpuGL::canWriteTexturePixels(const GrTexture* texture, GrPixelConfig srcConfig) const {
287 if (kIndex_8_GrPixelConfig == srcConfig || kIndex_8_GrPixelConfig == texture->config()) {
288 return false;
289 }
290 if (srcConfig != texture->config() && kES2_GrGLBinding == this->glBinding()) {
291 // In general ES2 requires the internal format of the texture and the format of the src
292 // pixels to match. However, It may or may not be possible to upload BGRA data to a RGBA
293 // texture. It depends upon which extension added BGRA. The Apple extension allows it
294 // (BGRA's internal format is RGBA) while the EXT extension does not (BGRA is its own
295 // internal format).
296 if (this->glCaps().bgraFormatSupport() &&
297 !this->glCaps().bgraIsInternalFormat() &&
298 kBGRA_8888_GrPixelConfig == srcConfig &&
299 kRGBA_8888_GrPixelConfig == texture->config()) {
300 return true;
301 } else {
302 return false;
303 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000304 } else {
bsalomon@google.com9c680582013-02-06 18:17:50 +0000305 return true;
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000306 }
307}
308
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000309bool GrGpuGL::fullReadPixelsIsFasterThanPartial() const {
310 return SkToBool(GR_GL_FULL_READPIXELS_FASTER_THAN_PARTIAL);
311}
312
bsalomon@google.com1bf1c212011-11-05 12:18:58 +0000313void GrGpuGL::onResetContext() {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000314
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000315 // we don't use the zb at all
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000316 GL_CALL(Disable(GR_GL_DEPTH_TEST));
317 GL_CALL(DepthMask(GR_GL_FALSE));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000318
bsalomon@google.com978c8c62012-05-21 14:45:49 +0000319 fHWDrawFace = GrDrawState::kInvalid_DrawFace;
320 fHWDitherEnabled = kUnknown_TriState;
reed@google.comac10a2d2010-12-22 21:39:39 +0000321
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000322 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com469d0dd2012-05-21 20:14:29 +0000323 // Desktop-only state that we never change
bsalomon@google.com2b1b8c02013-02-28 22:06:02 +0000324 if (!this->glCaps().isCoreProfile()) {
325 GL_CALL(Disable(GR_GL_POINT_SMOOTH));
326 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
327 GL_CALL(Disable(GR_GL_POLYGON_SMOOTH));
328 GL_CALL(Disable(GR_GL_POLYGON_STIPPLE));
329 GL_CALL(Disable(GR_GL_COLOR_LOGIC_OP));
330 GL_CALL(Disable(GR_GL_INDEX_LOGIC_OP));
331 }
commit-bot@chromium.orgdd4400b2013-03-21 19:58:22 +0000332 // The windows NVIDIA driver has GL_ARB_imaging in the extension string when using a core
333 // profile. This seems like a bug since the core spec removes any mention of GL_ARB_imaging.
334 if (this->glCaps().imagingSupport() && !this->glCaps().isCoreProfile()) {
bsalomon@google.come76b7cc2012-06-18 12:47:06 +0000335 GL_CALL(Disable(GR_GL_COLOR_TABLE));
336 }
bsalomon@google.com469d0dd2012-05-21 20:14:29 +0000337 GL_CALL(Disable(GR_GL_POLYGON_OFFSET_FILL));
338 // Since ES doesn't support glPointSize at all we always use the VS to
339 // set the point size
340 GL_CALL(Enable(GR_GL_VERTEX_PROGRAM_POINT_SIZE));
341
342 // We should set glPolygonMode(FRONT_AND_BACK,FILL) here, too. It isn't
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000343 // currently part of our gl interface. There are probably others as
bsalomon@google.com469d0dd2012-05-21 20:14:29 +0000344 // well.
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000345 }
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +0000346 fHWAAState.invalidate();
bsalomon@google.com978c8c62012-05-21 14:45:49 +0000347 fHWWriteToColor = kUnknown_TriState;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000348
reed@google.comac10a2d2010-12-22 21:39:39 +0000349 // we only ever use lines in hairline mode
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000350 GL_CALL(LineWidth(1));
reed@google.comac10a2d2010-12-22 21:39:39 +0000351
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000352 // invalid
bsalomon@google.com49209392012-06-05 15:13:46 +0000353 fHWActiveTextureUnitIdx = -1;
reed@google.comac10a2d2010-12-22 21:39:39 +0000354
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +0000355 fHWBlendState.invalidate();
bsalomon@google.com080773c2011-03-15 19:09:25 +0000356
tomhudson@google.com93813632011-10-27 20:21:16 +0000357 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.comc811ea32012-05-21 15:33:09 +0000358 fHWBoundTextures[s] = NULL;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000359 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000360
bsalomon@google.coma3201942012-06-21 19:58:20 +0000361 fHWScissorSettings.invalidate();
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000362
bsalomon@google.coma3201942012-06-21 19:58:20 +0000363 fHWViewport.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000364
bsalomon@google.com457b8a32012-05-21 21:19:58 +0000365 fHWStencilSettings.invalidate();
bsalomon@google.coma3201942012-06-21 19:58:20 +0000366 fHWStencilTestEnabled = kUnknown_TriState;
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000367
bsalomon@google.com880b8fc2013-02-19 20:17:28 +0000368 fHWGeometryState.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000369
bsalomon@google.comc811ea32012-05-21 15:33:09 +0000370 fHWBoundRenderTarget = NULL;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000371
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +0000372 fHWPathStencilMatrixState.invalidate();
bsalomon@google.combcce8922013-03-25 15:38:39 +0000373 if (this->caps()->pathStencilingSupport()) {
bsalomon@google.com05a718c2012-06-29 14:01:53 +0000374 // we don't use the model view matrix.
375 GL_CALL(MatrixMode(GR_GL_MODELVIEW));
376 GL_CALL(LoadIdentity());
377 }
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000378
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000379 // we assume these values
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000380 if (this->glCaps().unpackRowLengthSupport()) {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000381 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
382 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000383 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000384 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
385 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000386 if (this->glCaps().unpackFlipYSupport()) {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000387 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
388 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000389 if (this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000390 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, GR_GL_FALSE));
391 }
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000392
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000393 fHWProgramID = 0;
bsalomon@google.com91207482013-02-12 21:45:24 +0000394 fSharedGLProgramState.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000395}
396
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000397namespace {
398
399GrSurfaceOrigin resolve_origin(GrSurfaceOrigin origin, bool renderTarget) {
400 // By default, GrRenderTargets are GL's normal orientation so that they
401 // can be drawn to by the outside world without the client having
402 // to render upside down.
403 if (kDefault_GrSurfaceOrigin == origin) {
404 return renderTarget ? kBottomLeft_GrSurfaceOrigin : kTopLeft_GrSurfaceOrigin;
405 } else {
406 return origin;
407 }
408}
409
410}
411
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000412GrTexture* GrGpuGL::onWrapBackendTexture(const GrBackendTextureDesc& desc) {
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000413 if (!this->configToGLFormats(desc.fConfig, false, NULL, NULL, NULL)) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000414 return NULL;
415 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000416
robertphillips@google.comb72e5d32012-10-30 15:18:10 +0000417 if (0 == desc.fTextureHandle) {
418 return NULL;
419 }
420
bsalomon@google.combcce8922013-03-25 15:38:39 +0000421 int maxSize = this->caps()->maxTextureSize();
robertphillips@google.comb72e5d32012-10-30 15:18:10 +0000422 if (desc.fWidth > maxSize || desc.fHeight > maxSize) {
423 return NULL;
424 }
425
426 GrGLTexture::Desc glTexDesc;
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000427 // next line relies on GrBackendTextureDesc's flags matching GrTexture's
robertphillips@google.com32716282012-06-04 12:48:45 +0000428 glTexDesc.fFlags = (GrTextureFlags) desc.fFlags;
bsalomon@google.com99621082011-11-15 16:47:16 +0000429 glTexDesc.fWidth = desc.fWidth;
430 glTexDesc.fHeight = desc.fHeight;
bsalomon@google.come269f212011-11-07 13:29:52 +0000431 glTexDesc.fConfig = desc.fConfig;
robertphillips@google.com32716282012-06-04 12:48:45 +0000432 glTexDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.come269f212011-11-07 13:29:52 +0000433 glTexDesc.fTextureID = static_cast<GrGLuint>(desc.fTextureHandle);
bsalomon@google.com72830222013-01-23 20:25:22 +0000434 glTexDesc.fIsWrapped = true;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000435 bool renderTarget = 0 != (desc.fFlags & kRenderTarget_GrBackendTextureFlag);
436 // FIXME: this should be calling resolve_origin(), but Chrome code is currently
437 // assuming the old behaviour, which is that backend textures are always
438 // BottomLeft, even for non-RT's. Once Chrome is fixed, change this to:
439 // glTexDesc.fOrigin = resolve_origin(desc.fOrigin, renderTarget);
440 if (kDefault_GrSurfaceOrigin == desc.fOrigin) {
441 glTexDesc.fOrigin = kBottomLeft_GrSurfaceOrigin;
442 } else {
443 glTexDesc.fOrigin = desc.fOrigin;
444 }
bsalomon@google.come269f212011-11-07 13:29:52 +0000445
446 GrGLTexture* texture = NULL;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000447 if (renderTarget) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000448 GrGLRenderTarget::Desc glRTDesc;
449 glRTDesc.fRTFBOID = 0;
450 glRTDesc.fTexFBOID = 0;
451 glRTDesc.fMSColorRenderbufferID = 0;
bsalomon@google.come269f212011-11-07 13:29:52 +0000452 glRTDesc.fConfig = desc.fConfig;
453 glRTDesc.fSampleCnt = desc.fSampleCnt;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000454 glRTDesc.fOrigin = glTexDesc.fOrigin;
bsalomon@google.com99621082011-11-15 16:47:16 +0000455 if (!this->createRenderTargetObjects(glTexDesc.fWidth,
456 glTexDesc.fHeight,
bsalomon@google.come269f212011-11-07 13:29:52 +0000457 glTexDesc.fTextureID,
458 &glRTDesc)) {
459 return NULL;
460 }
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000461 texture = SkNEW_ARGS(GrGLTexture, (this, glTexDesc, glRTDesc));
bsalomon@google.come269f212011-11-07 13:29:52 +0000462 } else {
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000463 texture = SkNEW_ARGS(GrGLTexture, (this, glTexDesc));
bsalomon@google.come269f212011-11-07 13:29:52 +0000464 }
465 if (NULL == texture) {
466 return NULL;
467 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000468
bsalomon@google.come269f212011-11-07 13:29:52 +0000469 this->setSpareTextureUnit();
470 return texture;
471}
472
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000473GrRenderTarget* GrGpuGL::onWrapBackendRenderTarget(const GrBackendRenderTargetDesc& desc) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000474 GrGLRenderTarget::Desc glDesc;
475 glDesc.fConfig = desc.fConfig;
476 glDesc.fRTFBOID = static_cast<GrGLuint>(desc.fRenderTargetHandle);
477 glDesc.fMSColorRenderbufferID = 0;
478 glDesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
479 glDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.com72830222013-01-23 20:25:22 +0000480 glDesc.fIsWrapped = true;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000481 glDesc.fOrigin = desc.fOrigin;
482 if (glDesc.fRTFBOID == 0) {
483 GrAssert(desc.fOrigin == kBottomLeft_GrSurfaceOrigin);
484 }
485
486 glDesc.fOrigin = resolve_origin(desc.fOrigin, true);
bsalomon@google.come269f212011-11-07 13:29:52 +0000487 GrGLIRect viewport;
488 viewport.fLeft = 0;
489 viewport.fBottom = 0;
490 viewport.fWidth = desc.fWidth;
491 viewport.fHeight = desc.fHeight;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000492
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000493 GrRenderTarget* tgt = SkNEW_ARGS(GrGLRenderTarget,
494 (this, glDesc, viewport));
bsalomon@google.come269f212011-11-07 13:29:52 +0000495 if (desc.fStencilBits) {
496 GrGLStencilBuffer::Format format;
497 format.fInternalFormat = GrGLStencilBuffer::kUnknownInternalFormat;
498 format.fPacked = false;
499 format.fStencilBits = desc.fStencilBits;
500 format.fTotalBits = desc.fStencilBits;
bsalomon@google.com1f0f1a32013-01-23 21:32:32 +0000501 static const bool kIsSBWrapped = false;
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000502 GrGLStencilBuffer* sb = SkNEW_ARGS(GrGLStencilBuffer,
503 (this,
bsalomon@google.com1f0f1a32013-01-23 21:32:32 +0000504 kIsSBWrapped,
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000505 0,
506 desc.fWidth,
507 desc.fHeight,
508 desc.fSampleCnt,
509 format));
bsalomon@google.come269f212011-11-07 13:29:52 +0000510 tgt->setStencilBuffer(sb);
511 sb->unref();
512 }
513 return tgt;
514}
515
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000516////////////////////////////////////////////////////////////////////////////////
517
bsalomon@google.com9c680582013-02-06 18:17:50 +0000518bool GrGpuGL::onWriteTexturePixels(GrTexture* texture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000519 int left, int top, int width, int height,
520 GrPixelConfig config, const void* buffer,
521 size_t rowBytes) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000522 if (NULL == buffer) {
bsalomon@google.com9c680582013-02-06 18:17:50 +0000523 return false;
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000524 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000525 GrGLTexture* glTex = static_cast<GrGLTexture*>(texture);
526
bsalomon@google.com6f379512011-11-16 20:36:03 +0000527 this->setSpareTextureUnit();
528 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTex->textureID()));
529 GrGLTexture::Desc desc;
robertphillips@google.com32716282012-06-04 12:48:45 +0000530 desc.fFlags = glTex->desc().fFlags;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000531 desc.fWidth = glTex->width();
532 desc.fHeight = glTex->height();
robertphillips@google.com32716282012-06-04 12:48:45 +0000533 desc.fConfig = glTex->config();
534 desc.fSampleCnt = glTex->desc().fSampleCnt;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000535 desc.fTextureID = glTex->textureID();
bsalomon@google.com2d0bade2012-10-26 19:01:17 +0000536 desc.fOrigin = glTex->origin();
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000537
bsalomon@google.com9c680582013-02-06 18:17:50 +0000538 return this->uploadTexData(desc, false,
539 left, top, width, height,
540 config, buffer, rowBytes);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000541}
542
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000543namespace {
544bool adjust_pixel_ops_params(int surfaceWidth,
545 int surfaceHeight,
546 size_t bpp,
547 int* left, int* top, int* width, int* height,
548 const void** data,
549 size_t* rowBytes) {
550 if (!*rowBytes) {
551 *rowBytes = *width * bpp;
552 }
553
554 GrIRect subRect = GrIRect::MakeXYWH(*left, *top, *width, *height);
555 GrIRect bounds = GrIRect::MakeWH(surfaceWidth, surfaceHeight);
556
557 if (!subRect.intersect(bounds)) {
558 return false;
559 }
560 *data = reinterpret_cast<const void*>(reinterpret_cast<intptr_t>(*data) +
561 (subRect.fTop - *top) * *rowBytes + (subRect.fLeft - *left) * bpp);
562
563 *left = subRect.fLeft;
564 *top = subRect.fTop;
565 *width = subRect.width();
566 *height = subRect.height();
567 return true;
568}
569}
570
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000571bool GrGpuGL::uploadTexData(const GrGLTexture::Desc& desc,
572 bool isNewTexture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000573 int left, int top, int width, int height,
574 GrPixelConfig dataConfig,
575 const void* data,
576 size_t rowBytes) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000577 GrAssert(NULL != data || isNewTexture);
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000578
579 size_t bpp = GrBytesPerPixel(dataConfig);
580 if (!adjust_pixel_ops_params(desc.fWidth, desc.fHeight, bpp, &left, &top,
581 &width, &height, &data, &rowBytes)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000582 return false;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000583 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000584 size_t trimRowBytes = width * bpp;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000585
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000586 // in case we need a temporary, trimmed copy of the src pixels
587 SkAutoSMalloc<128 * 128> tempStorage;
588
bsalomon@google.com313f0192012-07-10 17:21:02 +0000589 // paletted textures cannot be partially updated
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000590 bool useTexStorage = isNewTexture &&
bsalomon@google.com313f0192012-07-10 17:21:02 +0000591 desc.fConfig != kIndex_8_GrPixelConfig &&
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000592 this->glCaps().texStorageSupport();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000593
bsalomon@google.com313f0192012-07-10 17:21:02 +0000594 if (useTexStorage && kDesktop_GrGLBinding == this->glBinding()) {
595 // 565 is not a sized internal format on desktop GL. So on desktop with
596 // 565 we always use an unsized internal format to let the system pick
597 // the best sized format to convert the 565 data to. Since TexStorage
598 // only allows sized internal formats we will instead use TexImage2D.
599 useTexStorage = desc.fConfig != kRGB_565_GrPixelConfig;
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000600 }
601
602 GrGLenum internalFormat;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000603 GrGLenum externalFormat;
604 GrGLenum externalType;
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000605 // glTexStorage requires sized internal formats on both desktop and ES. ES
606 // glTexImage requires an unsized format.
607 if (!this->configToGLFormats(dataConfig, useTexStorage, &internalFormat,
608 &externalFormat, &externalType)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000609 return false;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000610 }
611
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000612 if (!isNewTexture && GR_GL_PALETTE8_RGBA8 == internalFormat) {
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000613 // paletted textures cannot be updated
614 return false;
615 }
616
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000617 /*
bsalomon@google.com6f379512011-11-16 20:36:03 +0000618 * check whether to allocate a temporary buffer for flipping y or
619 * because our srcData has extra bytes past each row. If so, we need
620 * to trim those off here, since GL ES may not let us specify
621 * GL_UNPACK_ROW_LENGTH.
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000622 */
bsalomon@google.com6f379512011-11-16 20:36:03 +0000623 bool restoreGLRowLength = false;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000624 bool swFlipY = false;
625 bool glFlipY = false;
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000626 if (NULL != data) {
senorblanco@chromium.orgef5dbe12013-01-28 16:42:38 +0000627 if (kBottomLeft_GrSurfaceOrigin == desc.fOrigin) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000628 if (this->glCaps().unpackFlipYSupport()) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000629 glFlipY = true;
630 } else {
631 swFlipY = true;
632 }
633 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000634 if (this->glCaps().unpackRowLengthSupport() && !swFlipY) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000635 // can't use this for flipping, only non-neg values allowed. :(
636 if (rowBytes != trimRowBytes) {
637 GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
638 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength));
639 restoreGLRowLength = true;
640 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000641 } else {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000642 if (trimRowBytes != rowBytes || swFlipY) {
643 // copy data into our new storage, skipping the trailing bytes
644 size_t trimSize = height * trimRowBytes;
645 const char* src = (const char*)data;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000646 if (swFlipY) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000647 src += (height - 1) * rowBytes;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000648 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000649 char* dst = (char*)tempStorage.reset(trimSize);
650 for (int y = 0; y < height; y++) {
651 memcpy(dst, src, trimRowBytes);
652 if (swFlipY) {
653 src -= rowBytes;
654 } else {
655 src += rowBytes;
656 }
657 dst += trimRowBytes;
658 }
659 // now point data to our copied version
660 data = tempStorage.get();
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000661 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000662 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000663 if (glFlipY) {
664 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_TRUE));
665 }
666 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, static_cast<GrGLint>(bpp)));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000667 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000668 bool succeeded = true;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000669 if (isNewTexture &&
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000670 0 == left && 0 == top &&
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000671 desc.fWidth == width && desc.fHeight == height) {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000672 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000673 if (useTexStorage) {
674 // We never resize or change formats of textures. We don't use
675 // mipmaps currently.
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000676 GL_ALLOC_CALL(this->glInterface(),
677 TexStorage2D(GR_GL_TEXTURE_2D,
678 1, // levels
679 internalFormat,
680 desc.fWidth, desc.fHeight));
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000681 } else {
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000682 if (GR_GL_PALETTE8_RGBA8 == internalFormat) {
683 GrGLsizei imageSize = desc.fWidth * desc.fHeight +
684 kGrColorTableSize;
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000685 GL_ALLOC_CALL(this->glInterface(),
686 CompressedTexImage2D(GR_GL_TEXTURE_2D,
687 0, // level
688 internalFormat,
689 desc.fWidth, desc.fHeight,
690 0, // border
691 imageSize,
692 data));
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000693 } else {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000694 GL_ALLOC_CALL(this->glInterface(),
695 TexImage2D(GR_GL_TEXTURE_2D,
696 0, // level
697 internalFormat,
698 desc.fWidth, desc.fHeight,
699 0, // border
700 externalFormat, externalType,
701 data));
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000702 }
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000703 }
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000704 GrGLenum error = CHECK_ALLOC_ERROR(this->glInterface());
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000705 if (error != GR_GL_NO_ERROR) {
706 succeeded = false;
707 } else {
708 // if we have data and we used TexStorage to create the texture, we
709 // now upload with TexSubImage.
710 if (NULL != data && useTexStorage) {
711 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D,
712 0, // level
713 left, top,
714 width, height,
715 externalFormat, externalType,
716 data));
717 }
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000718 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000719 } else {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000720 if (swFlipY || glFlipY) {
bsalomon@google.com6f379512011-11-16 20:36:03 +0000721 top = desc.fHeight - (top + height);
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000722 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000723 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D,
724 0, // level
725 left, top,
726 width, height,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000727 externalFormat, externalType, data));
728 }
729
730 if (restoreGLRowLength) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000731 GrAssert(this->glCaps().unpackRowLengthSupport());
bsalomon@google.com6f379512011-11-16 20:36:03 +0000732 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000733 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000734 if (glFlipY) {
735 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
736 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000737 return succeeded;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000738}
739
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000740namespace {
robertphillips@google.com6177e692013-02-28 20:16:25 +0000741bool renderbuffer_storage_msaa(GrGLContext& ctx,
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000742 int sampleCount,
743 GrGLenum format,
744 int width, int height) {
robertphillips@google.com6177e692013-02-28 20:16:25 +0000745 CLEAR_ERROR_BEFORE_ALLOC(ctx.interface());
bsalomon@google.combcce8922013-03-25 15:38:39 +0000746 GrAssert(GrGLCaps::kNone_MSFBOType != ctx.info().caps()->msFBOType());
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000747 bool created = false;
748 if (GrGLCaps::kNVDesktop_CoverageAAType ==
bsalomon@google.combcce8922013-03-25 15:38:39 +0000749 ctx.info().caps()->coverageAAType()) {
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000750 const GrGLCaps::MSAACoverageMode& mode =
bsalomon@google.combcce8922013-03-25 15:38:39 +0000751 ctx.info().caps()->getMSAACoverageMode(sampleCount);
robertphillips@google.com6177e692013-02-28 20:16:25 +0000752 GL_ALLOC_CALL(ctx.interface(),
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000753 RenderbufferStorageMultisampleCoverage(GR_GL_RENDERBUFFER,
754 mode.fCoverageSampleCnt,
755 mode.fColorSampleCnt,
756 format,
757 width, height));
robertphillips@google.com6177e692013-02-28 20:16:25 +0000758 created = (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctx.interface()));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000759 }
760 if (!created) {
robertphillips@google.com6177e692013-02-28 20:16:25 +0000761 GL_ALLOC_CALL(ctx.interface(),
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000762 RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
763 sampleCount,
764 format,
765 width, height));
robertphillips@google.com6177e692013-02-28 20:16:25 +0000766 created = (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctx.interface()));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000767 }
768 return created;
769}
770}
771
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000772bool GrGpuGL::createRenderTargetObjects(int width, int height,
773 GrGLuint texID,
774 GrGLRenderTarget::Desc* desc) {
775 desc->fMSColorRenderbufferID = 0;
776 desc->fRTFBOID = 0;
777 desc->fTexFBOID = 0;
bsalomon@google.com72830222013-01-23 20:25:22 +0000778 desc->fIsWrapped = false;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000779
780 GrGLenum status;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000781
bsalomon@google.comab15d612011-08-09 12:57:56 +0000782 GrGLenum msColorFormat = 0; // suppress warning
783
bsalomon@google.com347c3822013-05-01 20:10:01 +0000784 if (desc->fSampleCnt > 0 && GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType()) {
785 goto FAILED;
786 }
787
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000788 GL_CALL(GenFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000789 if (!desc->fTexFBOID) {
790 goto FAILED;
791 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000792
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000793
bsalomon@google.comf3a60c02013-03-19 19:06:09 +0000794 // If we are using multisampling we will create two FBOS. We render to one and then resolve to
795 // the texture bound to the other. The exception is the IMG multisample extension. With this
796 // extension the texture is multisampled when rendered to and then auto-resolves it when it is
797 // rendered from.
bsalomon@google.com347c3822013-05-01 20:10:01 +0000798 if (desc->fSampleCnt > 0 && this->glCaps().usesMSAARenderBuffers()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000799 GL_CALL(GenFramebuffers(1, &desc->fRTFBOID));
800 GL_CALL(GenRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000801 if (!desc->fRTFBOID ||
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000802 !desc->fMSColorRenderbufferID ||
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000803 !this->configToGLFormats(desc->fConfig,
804 // GLES requires sized internal formats
805 kES2_GrGLBinding == this->glBinding(),
bsalomon@google.com347c3822013-05-01 20:10:01 +0000806 &msColorFormat,
807 NULL,
808 NULL)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000809 goto FAILED;
810 }
811 } else {
812 desc->fRTFBOID = desc->fTexFBOID;
813 }
814
bsalomon@google.com0e9b41a2012-01-04 22:11:43 +0000815 // below here we may bind the FBO
bsalomon@google.comc811ea32012-05-21 15:33:09 +0000816 fHWBoundRenderTarget = NULL;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000817 if (desc->fRTFBOID != desc->fTexFBOID) {
bsalomon@google.com13ac3a32013-01-09 23:29:39 +0000818 GrAssert(desc->fSampleCnt > 0);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000819 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000820 desc->fMSColorRenderbufferID));
robertphillips@google.com6177e692013-02-28 20:16:25 +0000821 if (!renderbuffer_storage_msaa(fGLContext,
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000822 desc->fSampleCnt,
823 msColorFormat,
824 width, height)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000825 goto FAILED;
826 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000827 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fRTFBOID));
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000828 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000829 GR_GL_COLOR_ATTACHMENT0,
830 GR_GL_RENDERBUFFER,
831 desc->fMSColorRenderbufferID));
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000832 if (!this->glCaps().isConfigVerifiedColorAttachment(desc->fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000833 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
834 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
835 goto FAILED;
836 }
bsalomon@google.combcce8922013-03-25 15:38:39 +0000837 fGLContext.info().caps()->markConfigAsValidColorAttachment(desc->fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000838 }
839 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000840 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000841
bsalomon@google.com347c3822013-05-01 20:10:01 +0000842 if (this->glCaps().usesImplicitMSAAResolve() && desc->fSampleCnt > 0) {
bsalomon@google.comf3a60c02013-03-19 19:06:09 +0000843 GL_CALL(FramebufferTexture2DMultisample(GR_GL_FRAMEBUFFER,
844 GR_GL_COLOR_ATTACHMENT0,
845 GR_GL_TEXTURE_2D,
846 texID, 0, desc->fSampleCnt));
847 } else {
848 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
849 GR_GL_COLOR_ATTACHMENT0,
850 GR_GL_TEXTURE_2D,
851 texID, 0));
852 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000853 if (!this->glCaps().isConfigVerifiedColorAttachment(desc->fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000854 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
855 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
856 goto FAILED;
857 }
bsalomon@google.combcce8922013-03-25 15:38:39 +0000858 fGLContext.info().caps()->markConfigAsValidColorAttachment(desc->fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000859 }
860
861 return true;
862
863FAILED:
864 if (desc->fMSColorRenderbufferID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000865 GL_CALL(DeleteRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000866 }
867 if (desc->fRTFBOID != desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000868 GL_CALL(DeleteFramebuffers(1, &desc->fRTFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000869 }
870 if (desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000871 GL_CALL(DeleteFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000872 }
873 return false;
874}
875
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000876// good to set a break-point here to know when createTexture fails
877static GrTexture* return_null_texture() {
878// GrAssert(!"null texture");
879 return NULL;
880}
881
caryclark@google.comcf6285b2012-06-06 12:09:01 +0000882#if 0 && GR_DEBUG
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000883static size_t as_size_t(int x) {
884 return x;
885}
886#endif
887
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000888GrTexture* GrGpuGL::onCreateTexture(const GrTextureDesc& desc,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000889 const void* srcData,
890 size_t rowBytes) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000891
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000892 GrGLTexture::Desc glTexDesc;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000893 GrGLRenderTarget::Desc glRTDesc;
reed@google.comac10a2d2010-12-22 21:39:39 +0000894
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000895 // Attempt to catch un- or wrongly initialized sample counts;
896 GrAssert(desc.fSampleCnt >= 0 && desc.fSampleCnt <= 64);
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000897 // We fail if the MSAA was requested and is not available.
898 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType() && desc.fSampleCnt) {
899 //GrPrintf("MSAA RT requested but not supported on this platform.");
900 return return_null_texture();
901 }
902 // If the sample count exceeds the max then we clamp it.
bsalomon@google.combcce8922013-03-25 15:38:39 +0000903 glTexDesc.fSampleCnt = GrMin(desc.fSampleCnt, this->caps()->maxSampleCount());
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000904
robertphillips@google.com32716282012-06-04 12:48:45 +0000905 glTexDesc.fFlags = desc.fFlags;
bsalomon@google.com99621082011-11-15 16:47:16 +0000906 glTexDesc.fWidth = desc.fWidth;
907 glTexDesc.fHeight = desc.fHeight;
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000908 glTexDesc.fConfig = desc.fConfig;
bsalomon@google.com72830222013-01-23 20:25:22 +0000909 glTexDesc.fIsWrapped = false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000910
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000911 glRTDesc.fMSColorRenderbufferID = 0;
912 glRTDesc.fRTFBOID = 0;
913 glRTDesc.fTexFBOID = 0;
bsalomon@google.com72830222013-01-23 20:25:22 +0000914 glRTDesc.fIsWrapped = false;
bsalomon@google.com64c4fe42011-11-05 14:51:01 +0000915 glRTDesc.fConfig = glTexDesc.fConfig;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000916
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000917 bool renderTarget = 0 != (desc.fFlags & kRenderTarget_GrTextureFlagBit);
reed@google.comac10a2d2010-12-22 21:39:39 +0000918
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000919 glTexDesc.fOrigin = resolve_origin(desc.fOrigin, renderTarget);
920 glRTDesc.fOrigin = glTexDesc.fOrigin;
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000921
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000922 glRTDesc.fSampleCnt = glTexDesc.fSampleCnt;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000923 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType() &&
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000924 desc.fSampleCnt) {
bsalomon@google.com945bbe12012-06-15 14:30:34 +0000925 //GrPrintf("MSAA RT requested but not supported on this platform.");
926 return return_null_texture();
reed@google.comac10a2d2010-12-22 21:39:39 +0000927 }
928
reed@google.comac10a2d2010-12-22 21:39:39 +0000929 if (renderTarget) {
bsalomon@google.combcce8922013-03-25 15:38:39 +0000930 int maxRTSize = this->caps()->maxRenderTargetSize();
931 if (glTexDesc.fWidth > maxRTSize || glTexDesc.fHeight > maxRTSize) {
bsalomon@google.com91958362011-06-13 17:58:13 +0000932 return return_null_texture();
933 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000934 }
935
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000936 GL_CALL(GenTextures(1, &glTexDesc.fTextureID));
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000937 if (renderTarget && this->glCaps().textureUsageSupport()) {
bsalomon@google.com07dd2bf2011-12-09 19:40:36 +0000938 // provides a hint about how this texture will be used
939 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
940 GR_GL_TEXTURE_USAGE,
941 GR_GL_FRAMEBUFFER_ATTACHMENT));
942 }
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000943 if (!glTexDesc.fTextureID) {
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000944 return return_null_texture();
945 }
946
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000947 this->setSpareTextureUnit();
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000948 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTexDesc.fTextureID));
bsalomon@google.come269f212011-11-07 13:29:52 +0000949
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000950 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
951 // drivers have a bug where an FBO won't be complete if it includes a
952 // texture that is not mipmap complete (considering the filter in use).
953 GrGLTexture::TexParams initialTexParams;
954 // we only set a subset here so invalidate first
955 initialTexParams.invalidate();
956 initialTexParams.fFilter = GR_GL_NEAREST;
957 initialTexParams.fWrapS = GR_GL_CLAMP_TO_EDGE;
958 initialTexParams.fWrapT = GR_GL_CLAMP_TO_EDGE;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000959 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
960 GR_GL_TEXTURE_MAG_FILTER,
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000961 initialTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000962 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
963 GR_GL_TEXTURE_MIN_FILTER,
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000964 initialTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000965 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
966 GR_GL_TEXTURE_WRAP_S,
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000967 initialTexParams.fWrapS));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000968 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
969 GR_GL_TEXTURE_WRAP_T,
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000970 initialTexParams.fWrapT));
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000971 if (!this->uploadTexData(glTexDesc, true, 0, 0,
972 glTexDesc.fWidth, glTexDesc.fHeight,
973 desc.fConfig, srcData, rowBytes)) {
974 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
975 return return_null_texture();
bsalomon@google.com6f379512011-11-16 20:36:03 +0000976 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000977
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000978 GrGLTexture* tex;
reed@google.comac10a2d2010-12-22 21:39:39 +0000979 if (renderTarget) {
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +0000980 // unbind the texture from the texture unit before binding it to the frame buffer
981 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, 0));
982
bsalomon@google.com99621082011-11-15 16:47:16 +0000983 if (!this->createRenderTargetObjects(glTexDesc.fWidth,
984 glTexDesc.fHeight,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000985 glTexDesc.fTextureID,
986 &glRTDesc)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000987 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
reed@google.comac10a2d2010-12-22 21:39:39 +0000988 return return_null_texture();
989 }
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000990 tex = SkNEW_ARGS(GrGLTexture, (this, glTexDesc, glRTDesc));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000991 } else {
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000992 tex = SkNEW_ARGS(GrGLTexture, (this, glTexDesc));
reed@google.comac10a2d2010-12-22 21:39:39 +0000993 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000994 tex->setCachedTexParams(initialTexParams, this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +0000995#ifdef TRACE_TEXTURE_CREATION
bsalomon@google.com64c4fe42011-11-05 14:51:01 +0000996 GrPrintf("--- new texture [%d] size=(%d %d) config=%d\n",
997 glTexDesc.fTextureID, desc.fWidth, desc.fHeight, desc.fConfig);
reed@google.comac10a2d2010-12-22 21:39:39 +0000998#endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000999 return tex;
1000}
1001
1002namespace {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001003
1004const GrGLuint kUnknownBitCount = GrGLStencilBuffer::kUnknownBitCount;
1005
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001006void inline get_stencil_rb_sizes(const GrGLInterface* gl,
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001007 GrGLuint rb,
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001008 GrGLStencilBuffer::Format* format) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001009 // we shouldn't ever know one size and not the other
1010 GrAssert((kUnknownBitCount == format->fStencilBits) ==
1011 (kUnknownBitCount == format->fTotalBits));
1012 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001013 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001014 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1015 (GrGLint*)&format->fStencilBits);
1016 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001017 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001018 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1019 (GrGLint*)&format->fTotalBits);
1020 format->fTotalBits += format->fStencilBits;
1021 } else {
1022 format->fTotalBits = format->fStencilBits;
1023 }
1024 }
1025}
1026}
1027
1028bool GrGpuGL::createStencilBufferForRenderTarget(GrRenderTarget* rt,
1029 int width, int height) {
1030
1031 // All internally created RTs are also textures. We don't create
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +00001032 // SBs for a client's standalone RT (that is a RT that isn't also a texture).
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001033 GrAssert(rt->asTexture());
bsalomon@google.com99621082011-11-15 16:47:16 +00001034 GrAssert(width >= rt->width());
1035 GrAssert(height >= rt->height());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001036
1037 int samples = rt->numSamples();
1038 GrGLuint sbID;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001039 GL_CALL(GenRenderbuffers(1, &sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001040 if (!sbID) {
1041 return false;
1042 }
1043
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001044 int stencilFmtCnt = this->glCaps().stencilFormats().count();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001045 for (int i = 0; i < stencilFmtCnt; ++i) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001046 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001047 // we start with the last stencil format that succeeded in hopes
1048 // that we won't go through this loop more than once after the
1049 // first (painful) stencil creation.
1050 int sIdx = (i + fLastSuccessfulStencilFmtIdx) % stencilFmtCnt;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001051 const GrGLCaps::StencilFormat& sFmt =
1052 this->glCaps().stencilFormats()[sIdx];
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001053 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001054 // we do this "if" so that we don't call the multisample
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001055 // version on a GL that doesn't have an MSAA extension.
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001056 bool created;
1057 if (samples > 0) {
robertphillips@google.com6177e692013-02-28 20:16:25 +00001058 created = renderbuffer_storage_msaa(fGLContext,
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001059 samples,
1060 sFmt.fInternalFormat,
1061 width, height);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001062 } else {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001063 GL_ALLOC_CALL(this->glInterface(),
1064 RenderbufferStorage(GR_GL_RENDERBUFFER,
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001065 sFmt.fInternalFormat,
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001066 width, height));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001067 created =
1068 (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(this->glInterface()));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001069 }
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001070 if (created) {
1071 // After sized formats we attempt an unsized format and take
1072 // whatever sizes GL gives us. In that case we query for the size.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001073 GrGLStencilBuffer::Format format = sFmt;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001074 get_stencil_rb_sizes(this->glInterface(), sbID, &format);
bsalomon@google.com72830222013-01-23 20:25:22 +00001075 static const bool kIsWrapped = false;
robertphillips@google.comf2e93fc2012-09-05 19:44:18 +00001076 SkAutoTUnref<GrStencilBuffer> sb(SkNEW_ARGS(GrGLStencilBuffer,
bsalomon@google.com72830222013-01-23 20:25:22 +00001077 (this, kIsWrapped, sbID, width, height,
robertphillips@google.comf2e93fc2012-09-05 19:44:18 +00001078 samples, format)));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001079 if (this->attachStencilBufferToRenderTarget(sb, rt)) {
1080 fLastSuccessfulStencilFmtIdx = sIdx;
robertphillips@google.com9fbcad02012-09-09 14:44:15 +00001081 sb->transferToCache();
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001082 rt->setStencilBuffer(sb);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001083 return true;
1084 }
1085 sb->abandon(); // otherwise we lose sbID
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001086 }
1087 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001088 GL_CALL(DeleteRenderbuffers(1, &sbID));
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001089 return false;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001090}
1091
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001092bool GrGpuGL::attachStencilBufferToRenderTarget(GrStencilBuffer* sb, GrRenderTarget* rt) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001093 GrGLRenderTarget* glrt = (GrGLRenderTarget*) rt;
1094
1095 GrGLuint fbo = glrt->renderFBOID();
1096
1097 if (NULL == sb) {
1098 if (NULL != rt->getStencilBuffer()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001099 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001100 GR_GL_STENCIL_ATTACHMENT,
1101 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001102 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001103 GR_GL_DEPTH_ATTACHMENT,
1104 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001105#if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001106 GrGLenum status;
1107 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001108 GrAssert(GR_GL_FRAMEBUFFER_COMPLETE == status);
1109#endif
1110 }
1111 return true;
1112 } else {
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001113 GrGLStencilBuffer* glsb = static_cast<GrGLStencilBuffer*>(sb);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001114 GrGLuint rb = glsb->renderbufferID();
1115
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001116 fHWBoundRenderTarget = NULL;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001117 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, fbo));
1118 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001119 GR_GL_STENCIL_ATTACHMENT,
1120 GR_GL_RENDERBUFFER, rb));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001121 if (glsb->format().fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001122 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001123 GR_GL_DEPTH_ATTACHMENT,
1124 GR_GL_RENDERBUFFER, rb));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001125 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001126 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001127 GR_GL_DEPTH_ATTACHMENT,
1128 GR_GL_RENDERBUFFER, 0));
reed@google.comac10a2d2010-12-22 21:39:39 +00001129 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001130
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001131 GrGLenum status;
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001132 if (!this->glCaps().isColorConfigAndStencilFormatVerified(rt->config(), glsb->format())) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001133 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1134 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001135 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001136 GR_GL_STENCIL_ATTACHMENT,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001137 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001138 if (glsb->format().fPacked) {
1139 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1140 GR_GL_DEPTH_ATTACHMENT,
1141 GR_GL_RENDERBUFFER, 0));
1142 }
1143 return false;
1144 } else {
bsalomon@google.combcce8922013-03-25 15:38:39 +00001145 fGLContext.info().caps()->markColorConfigAndStencilFormatAsVerified(
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001146 rt->config(),
1147 glsb->format());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001148 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001149 }
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001150 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001151 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001152}
1153
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001154////////////////////////////////////////////////////////////////////////////////
1155
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001156GrVertexBuffer* GrGpuGL::onCreateVertexBuffer(uint32_t size, bool dynamic) {
bsalomon@google.come49ad452013-02-20 19:33:20 +00001157 GrGLVertexBuffer::Desc desc;
1158 desc.fDynamic = dynamic;
1159 desc.fSizeInBytes = size;
1160 desc.fIsWrapped = false;
1161
bsalomon@google.com96966a52013-02-21 16:34:21 +00001162 if (this->glCaps().useNonVBOVertexAndIndexDynamicData() && desc.fDynamic) {
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001163 desc.fID = 0;
bsalomon@google.come49ad452013-02-20 19:33:20 +00001164 GrGLVertexBuffer* vertexBuffer = SkNEW_ARGS(GrGLVertexBuffer, (this, desc));
reed@google.comac10a2d2010-12-22 21:39:39 +00001165 return vertexBuffer;
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001166 } else {
1167 GL_CALL(GenBuffers(1, &desc.fID));
1168 if (desc.fID) {
bsalomon@google.com6918d482013-03-07 19:09:11 +00001169 fHWGeometryState.setVertexBufferID(this, desc.fID);
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001170 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1171 // make sure driver can allocate memory for this buffer
1172 GL_ALLOC_CALL(this->glInterface(),
1173 BufferData(GR_GL_ARRAY_BUFFER,
1174 desc.fSizeInBytes,
1175 NULL, // data ptr
1176 desc.fDynamic ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW));
1177 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
1178 GL_CALL(DeleteBuffers(1, &desc.fID));
bsalomon@google.com6918d482013-03-07 19:09:11 +00001179 this->notifyVertexBufferDelete(desc.fID);
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001180 return NULL;
1181 }
1182 GrGLVertexBuffer* vertexBuffer = SkNEW_ARGS(GrGLVertexBuffer, (this, desc));
1183 return vertexBuffer;
1184 }
1185 return NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +00001186 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001187}
1188
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001189GrIndexBuffer* GrGpuGL::onCreateIndexBuffer(uint32_t size, bool dynamic) {
bsalomon@google.come49ad452013-02-20 19:33:20 +00001190 GrGLIndexBuffer::Desc desc;
1191 desc.fDynamic = dynamic;
1192 desc.fSizeInBytes = size;
1193 desc.fIsWrapped = false;
1194
bsalomon@google.com96966a52013-02-21 16:34:21 +00001195 if (this->glCaps().useNonVBOVertexAndIndexDynamicData() && desc.fDynamic) {
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001196 desc.fID = 0;
bsalomon@google.come49ad452013-02-20 19:33:20 +00001197 GrIndexBuffer* indexBuffer = SkNEW_ARGS(GrGLIndexBuffer, (this, desc));
reed@google.comac10a2d2010-12-22 21:39:39 +00001198 return indexBuffer;
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001199 } else {
1200 GL_CALL(GenBuffers(1, &desc.fID));
1201 if (desc.fID) {
bsalomon@google.com6918d482013-03-07 19:09:11 +00001202 fHWGeometryState.setIndexBufferIDOnDefaultVertexArray(this, desc.fID);
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001203 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1204 // make sure driver can allocate memory for this buffer
1205 GL_ALLOC_CALL(this->glInterface(),
1206 BufferData(GR_GL_ELEMENT_ARRAY_BUFFER,
1207 desc.fSizeInBytes,
1208 NULL, // data ptr
1209 desc.fDynamic ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW));
1210 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
1211 GL_CALL(DeleteBuffers(1, &desc.fID));
bsalomon@google.com6918d482013-03-07 19:09:11 +00001212 this->notifyIndexBufferDelete(desc.fID);
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001213 return NULL;
1214 }
1215 GrIndexBuffer* indexBuffer = SkNEW_ARGS(GrGLIndexBuffer, (this, desc));
1216 return indexBuffer;
1217 }
1218 return NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +00001219 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001220}
1221
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00001222GrPath* GrGpuGL::onCreatePath(const SkPath& inPath) {
bsalomon@google.combcce8922013-03-25 15:38:39 +00001223 GrAssert(this->caps()->pathStencilingSupport());
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001224 return SkNEW_ARGS(GrGLPath, (this, inPath));
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00001225}
1226
bsalomon@google.coma3201942012-06-21 19:58:20 +00001227void GrGpuGL::flushScissor() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001228 const GrDrawState& drawState = this->getDrawState();
1229 const GrGLRenderTarget* rt =
1230 static_cast<const GrGLRenderTarget*>(drawState.getRenderTarget());
1231
1232 GrAssert(NULL != rt);
1233 const GrGLIRect& vp = rt->getViewport();
reed@google.comac10a2d2010-12-22 21:39:39 +00001234
bsalomon@google.coma3201942012-06-21 19:58:20 +00001235 if (fScissorState.fEnabled) {
1236 GrGLIRect scissor;
1237 scissor.setRelativeTo(vp,
1238 fScissorState.fRect.fLeft,
1239 fScissorState.fRect.fTop,
1240 fScissorState.fRect.width(),
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001241 fScissorState.fRect.height(),
1242 rt->origin());
bsalomon@google.coma3201942012-06-21 19:58:20 +00001243 // if the scissor fully contains the viewport then we fall through and
1244 // disable the scissor test.
1245 if (!scissor.contains(vp)) {
1246 if (fHWScissorSettings.fRect != scissor) {
1247 scissor.pushToGLScissor(this->glInterface());
1248 fHWScissorSettings.fRect = scissor;
1249 }
1250 if (kYes_TriState != fHWScissorSettings.fEnabled) {
1251 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1252 fHWScissorSettings.fEnabled = kYes_TriState;
1253 }
1254 return;
1255 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001256 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001257 if (kNo_TriState != fHWScissorSettings.fEnabled) {
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001258 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.coma3201942012-06-21 19:58:20 +00001259 fHWScissorSettings.fEnabled = kNo_TriState;
1260 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001261 }
1262}
1263
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001264void GrGpuGL::onClear(const GrIRect* rect, GrColor color) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001265 const GrDrawState& drawState = this->getDrawState();
1266 const GrRenderTarget* rt = drawState.getRenderTarget();
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001267 // parent class should never let us get here with no RT
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001268 GrAssert(NULL != rt);
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001269
bsalomon@google.com74b98712011-11-11 19:46:16 +00001270 GrIRect clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001271 if (NULL != rect) {
1272 // flushScissor expects rect to be clipped to the target.
bsalomon@google.com74b98712011-11-11 19:46:16 +00001273 clippedRect = *rect;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001274 GrIRect rtRect = SkIRect::MakeWH(rt->width(), rt->height());
bsalomon@google.com74b98712011-11-11 19:46:16 +00001275 if (clippedRect.intersect(rtRect)) {
1276 rect = &clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001277 } else {
1278 return;
1279 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001280 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001281 this->flushRenderTarget(rect);
bsalomon@google.coma3201942012-06-21 19:58:20 +00001282 GrAutoTRestore<ScissorState> asr(&fScissorState);
1283 fScissorState.fEnabled = (NULL != rect);
1284 if (fScissorState.fEnabled) {
1285 fScissorState.fRect = *rect;
1286 }
1287 this->flushScissor();
bsalomon@google.com74b98712011-11-11 19:46:16 +00001288
1289 GrGLfloat r, g, b, a;
1290 static const GrGLfloat scale255 = 1.f / 255.f;
1291 a = GrColorUnpackA(color) * scale255;
1292 GrGLfloat scaleRGB = scale255;
bsalomon@google.com74b98712011-11-11 19:46:16 +00001293 r = GrColorUnpackR(color) * scaleRGB;
1294 g = GrColorUnpackG(color) * scaleRGB;
1295 b = GrColorUnpackB(color) * scaleRGB;
1296
1297 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00001298 fHWWriteToColor = kYes_TriState;
bsalomon@google.com74b98712011-11-11 19:46:16 +00001299 GL_CALL(ClearColor(r, g, b, a));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001300 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001301}
1302
bsalomon@google.comedc177d2011-08-05 15:46:40 +00001303void GrGpuGL::clearStencil() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001304 if (NULL == this->getDrawState().getRenderTarget()) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001305 return;
1306 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001307
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001308 this->flushRenderTarget(&GrIRect::EmptyIRect());
1309
bsalomon@google.coma3201942012-06-21 19:58:20 +00001310 GrAutoTRestore<ScissorState> asr(&fScissorState);
1311 fScissorState.fEnabled = false;
1312 this->flushScissor();
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001313
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001314 GL_CALL(StencilMask(0xffffffff));
1315 GL_CALL(ClearStencil(0));
1316 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001317 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001318}
1319
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001320void GrGpuGL::clearStencilClip(const GrIRect& rect, bool insideClip) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001321 const GrDrawState& drawState = this->getDrawState();
1322 const GrRenderTarget* rt = drawState.getRenderTarget();
1323 GrAssert(NULL != rt);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001324
1325 // this should only be called internally when we know we have a
1326 // stencil buffer.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001327 GrAssert(NULL != rt->getStencilBuffer());
1328 GrGLint stencilBitCount = rt->getStencilBuffer()->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001329#if 0
reed@google.comac10a2d2010-12-22 21:39:39 +00001330 GrAssert(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00001331 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001332#else
1333 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001334 // ANGLE a partial stencil mask will cause clears to be
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001335 // turned into draws. Our contract on GrDrawTarget says that
1336 // changing the clip between stencil passes may or may not
1337 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00001338 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001339#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001340 GrGLint value;
1341 if (insideClip) {
1342 value = (1 << (stencilBitCount - 1));
1343 } else {
1344 value = 0;
1345 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001346 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.coma3201942012-06-21 19:58:20 +00001347
1348 GrAutoTRestore<ScissorState> asr(&fScissorState);
1349 fScissorState.fEnabled = true;
1350 fScissorState.fRect = rect;
1351 this->flushScissor();
1352
caryclark@google.comcf6285b2012-06-06 12:09:01 +00001353 GL_CALL(StencilMask((uint32_t) clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001354 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001355 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001356 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001357}
1358
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001359void GrGpuGL::onForceRenderTargetFlush() {
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001360 this->flushRenderTarget(&GrIRect::EmptyIRect());
reed@google.comac10a2d2010-12-22 21:39:39 +00001361}
1362
bsalomon@google.comc4364992011-11-07 15:54:49 +00001363bool GrGpuGL::readPixelsWillPayForYFlip(GrRenderTarget* renderTarget,
1364 int left, int top,
1365 int width, int height,
1366 GrPixelConfig config,
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001367 size_t rowBytes) const {
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001368 // If this rendertarget is aready TopLeft, we don't need to flip.
1369 if (kTopLeft_GrSurfaceOrigin == renderTarget->origin()) {
1370 return false;
1371 }
1372
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001373 // if GL can do the flip then we'll never pay for it.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001374 if (this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001375 return false;
1376 }
1377
1378 // If we have to do memcpy to handle non-trim rowBytes then we
bsalomon@google.com7107fa72011-11-10 14:54:14 +00001379 // get the flip for free. Otherwise it costs.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001380 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001381 return true;
1382 }
1383 // If we have to do memcpys to handle rowBytes then y-flip is free
1384 // Note the rowBytes might be tight to the passed in data, but if data
1385 // gets clipped in x to the target the rowBytes will no longer be tight.
1386 if (left >= 0 && (left + width) < renderTarget->width()) {
1387 return 0 == rowBytes ||
1388 GrBytesPerPixel(config) * width == rowBytes;
1389 } else {
1390 return false;
1391 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001392}
1393
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001394bool GrGpuGL::onReadPixels(GrRenderTarget* target,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001395 int left, int top,
1396 int width, int height,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001397 GrPixelConfig config,
1398 void* buffer,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001399 size_t rowBytes) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001400 GrGLenum format;
1401 GrGLenum type;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001402 bool flipY = kBottomLeft_GrSurfaceOrigin == target->origin();
bsalomon@google.com280e99f2012-01-05 16:17:38 +00001403 if (!this->configToGLFormats(config, false, NULL, &format, &type)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001404 return false;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001405 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001406 size_t bpp = GrBytesPerPixel(config);
1407 if (!adjust_pixel_ops_params(target->width(), target->height(), bpp,
1408 &left, &top, &width, &height,
1409 const_cast<const void**>(&buffer),
1410 &rowBytes)) {
1411 return false;
1412 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001413
bsalomon@google.comc6980972011-11-02 19:57:21 +00001414 // resolve the render target if necessary
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001415 GrGLRenderTarget* tgt = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001416 GrDrawState::AutoRenderTargetRestore artr;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001417 switch (tgt->getResolveType()) {
1418 case GrGLRenderTarget::kCantResolve_ResolveType:
1419 return false;
1420 case GrGLRenderTarget::kAutoResolves_ResolveType:
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001421 artr.set(this->drawState(), target);
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001422 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001423 break;
1424 case GrGLRenderTarget::kCanResolve_ResolveType:
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001425 this->onResolveRenderTarget(tgt);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001426 // we don't track the state of the READ FBO ID.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001427 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1428 tgt->textureFBOID()));
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001429 break;
1430 default:
1431 GrCrash("Unknown resolve type");
reed@google.comac10a2d2010-12-22 21:39:39 +00001432 }
1433
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001434 const GrGLIRect& glvp = tgt->getViewport();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001435
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001436 // the read rect is viewport-relative
1437 GrGLIRect readRect;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001438 readRect.setRelativeTo(glvp, left, top, width, height, target->origin());
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001439
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001440 size_t tightRowBytes = bpp * width;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001441 if (0 == rowBytes) {
1442 rowBytes = tightRowBytes;
1443 }
1444 size_t readDstRowBytes = tightRowBytes;
1445 void* readDst = buffer;
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001446
bsalomon@google.comc6980972011-11-02 19:57:21 +00001447 // determine if GL can read using the passed rowBytes or if we need
1448 // a scratch buffer.
1449 SkAutoSMalloc<32 * sizeof(GrColor)> scratch;
1450 if (rowBytes != tightRowBytes) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001451 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.comc6980972011-11-02 19:57:21 +00001452 GrAssert(!(rowBytes % sizeof(GrColor)));
1453 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, rowBytes / sizeof(GrColor)));
1454 readDstRowBytes = rowBytes;
1455 } else {
1456 scratch.reset(tightRowBytes * height);
1457 readDst = scratch.get();
1458 }
1459 }
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001460 if (flipY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001461 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 1));
1462 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001463 GL_CALL(ReadPixels(readRect.fLeft, readRect.fBottom,
1464 readRect.fWidth, readRect.fHeight,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001465 format, type, readDst));
1466 if (readDstRowBytes != tightRowBytes) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001467 GrAssert(this->glCaps().packRowLengthSupport());
bsalomon@google.comc6980972011-11-02 19:57:21 +00001468 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
1469 }
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001470 if (flipY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001471 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 0));
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001472 flipY = false;
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001473 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001474
1475 // now reverse the order of the rows, since GL's are bottom-to-top, but our
bsalomon@google.comc6980972011-11-02 19:57:21 +00001476 // API presents top-to-bottom. We must preserve the padding contents. Note
1477 // that the above readPixels did not overwrite the padding.
1478 if (readDst == buffer) {
1479 GrAssert(rowBytes == readDstRowBytes);
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001480 if (flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001481 scratch.reset(tightRowBytes);
1482 void* tmpRow = scratch.get();
1483 // flip y in-place by rows
1484 const int halfY = height >> 1;
1485 char* top = reinterpret_cast<char*>(buffer);
1486 char* bottom = top + (height - 1) * rowBytes;
1487 for (int y = 0; y < halfY; y++) {
1488 memcpy(tmpRow, top, tightRowBytes);
1489 memcpy(top, bottom, tightRowBytes);
1490 memcpy(bottom, tmpRow, tightRowBytes);
1491 top += rowBytes;
1492 bottom -= rowBytes;
1493 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001494 }
1495 } else {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001496 GrAssert(readDst != buffer); GrAssert(rowBytes != tightRowBytes);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001497 // copy from readDst to buffer while flipping y
caryclark@google.comcf6285b2012-06-06 12:09:01 +00001498 // const int halfY = height >> 1;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001499 const char* src = reinterpret_cast<const char*>(readDst);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001500 char* dst = reinterpret_cast<char*>(buffer);
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001501 if (flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001502 dst += (height-1) * rowBytes;
1503 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001504 for (int y = 0; y < height; y++) {
1505 memcpy(dst, src, tightRowBytes);
1506 src += readDstRowBytes;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001507 if (!flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001508 dst += rowBytes;
1509 } else {
1510 dst -= rowBytes;
1511 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001512 }
1513 }
1514 return true;
1515}
1516
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001517void GrGpuGL::flushRenderTarget(const GrIRect* bound) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001518
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001519 GrGLRenderTarget* rt =
1520 static_cast<GrGLRenderTarget*>(this->drawState()->getRenderTarget());
1521 GrAssert(NULL != rt);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001522
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001523 if (fHWBoundRenderTarget != rt) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001524 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, rt->renderFBOID()));
robertphillips@google.coma6ffb582013-04-29 16:50:17 +00001525#if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001526 GrGLenum status;
1527 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001528 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
reed@google.comb9255d52011-06-13 18:54:59 +00001529 GrPrintf("GrGpuGL::flushRenderTarget glCheckFramebufferStatus %x\n", status);
reed@google.comac10a2d2010-12-22 21:39:39 +00001530 }
robertphillips@google.coma6ffb582013-04-29 16:50:17 +00001531#endif
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001532 fHWBoundRenderTarget = rt;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001533 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.coma3201942012-06-21 19:58:20 +00001534 if (fHWViewport != vp) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001535 vp.pushToGLViewport(this->glInterface());
bsalomon@google.coma3201942012-06-21 19:58:20 +00001536 fHWViewport = vp;
reed@google.comac10a2d2010-12-22 21:39:39 +00001537 }
1538 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001539 if (NULL == bound || !bound->isEmpty()) {
1540 rt->flagAsNeedingResolve(bound);
1541 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001542}
1543
twiz@google.com0f31ca72011-03-18 17:38:11 +00001544GrGLenum gPrimitiveType2GLMode[] = {
1545 GR_GL_TRIANGLES,
1546 GR_GL_TRIANGLE_STRIP,
1547 GR_GL_TRIANGLE_FAN,
1548 GR_GL_POINTS,
1549 GR_GL_LINES,
1550 GR_GL_LINE_STRIP
reed@google.comac10a2d2010-12-22 21:39:39 +00001551};
1552
bsalomon@google.comd302f142011-03-03 13:54:13 +00001553#define SWAP_PER_DRAW 0
1554
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001555#if SWAP_PER_DRAW
bsalomon@google.comd302f142011-03-03 13:54:13 +00001556 #if GR_MAC_BUILD
1557 #include <AGL/agl.h>
1558 #elif GR_WIN32_BUILD
bsalomon@google.comce7357d2012-06-25 17:49:25 +00001559 #include <gl/GL.h>
bsalomon@google.comd302f142011-03-03 13:54:13 +00001560 void SwapBuf() {
1561 DWORD procID = GetCurrentProcessId();
1562 HWND hwnd = GetTopWindow(GetDesktopWindow());
1563 while(hwnd) {
1564 DWORD wndProcID = 0;
1565 GetWindowThreadProcessId(hwnd, &wndProcID);
1566 if(wndProcID == procID) {
1567 SwapBuffers(GetDC(hwnd));
1568 }
1569 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
1570 }
1571 }
1572 #endif
1573#endif
1574
bsalomon@google.com74749cd2013-01-30 16:12:41 +00001575void GrGpuGL::onGpuDraw(const DrawInfo& info) {
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00001576 size_t indexOffsetInBytes;
1577 this->setupGeometry(info, &indexOffsetInBytes);
reed@google.comac10a2d2010-12-22 21:39:39 +00001578
bsalomon@google.com74749cd2013-01-30 16:12:41 +00001579 GrAssert((size_t)info.primitiveType() < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001580
bsalomon@google.com74749cd2013-01-30 16:12:41 +00001581 if (info.isIndexed()) {
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00001582 GrGLvoid* indices =
1583 reinterpret_cast<GrGLvoid*>(indexOffsetInBytes + sizeof(uint16_t) * info.startIndex());
bsalomon@google.com74749cd2013-01-30 16:12:41 +00001584 // info.startVertex() was accounted for by setupGeometry.
1585 GL_CALL(DrawElements(gPrimitiveType2GLMode[info.primitiveType()],
1586 info.indexCount(),
1587 GR_GL_UNSIGNED_SHORT,
1588 indices));
1589 } else {
1590 // Pass 0 for parameter first. We have to adjust glVertexAttribPointer() to account for
1591 // startVertex in the DrawElements case. So we always rely on setupGeometry to have
1592 // accounted for startVertex.
1593 GL_CALL(DrawArrays(gPrimitiveType2GLMode[info.primitiveType()], 0, info.vertexCount()));
1594 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00001595#if SWAP_PER_DRAW
1596 glFlush();
1597 #if GR_MAC_BUILD
1598 aglSwapBuffers(aglGetCurrentContext());
1599 int set_a_break_pt_here = 9;
1600 aglSwapBuffers(aglGetCurrentContext());
1601 #elif GR_WIN32_BUILD
1602 SwapBuf();
1603 int set_a_break_pt_here = 9;
1604 SwapBuf();
1605 #endif
1606#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001607}
1608
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001609namespace {
bsalomon@google.com100abf42012-09-05 17:40:04 +00001610
1611static const uint16_t kOnes16 = static_cast<uint16_t>(~0);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001612const GrStencilSettings& winding_nv_path_stencil_settings() {
1613 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
1614 kIncClamp_StencilOp,
1615 kIncClamp_StencilOp,
1616 kAlwaysIfInClip_StencilFunc,
bsalomon@google.com100abf42012-09-05 17:40:04 +00001617 kOnes16, kOnes16, kOnes16);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001618 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
1619}
1620const GrStencilSettings& even_odd_nv_path_stencil_settings() {
1621 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
1622 kInvert_StencilOp,
1623 kInvert_StencilOp,
1624 kAlwaysIfInClip_StencilFunc,
bsalomon@google.com100abf42012-09-05 17:40:04 +00001625 kOnes16, kOnes16, kOnes16);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001626 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
1627}
1628}
1629
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001630void GrGpuGL::setStencilPathSettings(const GrPath&,
sugoi@google.com12b4e272012-12-06 20:13:11 +00001631 SkPath::FillType fill,
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001632 GrStencilSettings* settings) {
1633 switch (fill) {
sugoi@google.com12b4e272012-12-06 20:13:11 +00001634 case SkPath::kEvenOdd_FillType:
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001635 *settings = even_odd_nv_path_stencil_settings();
1636 return;
sugoi@google.com12b4e272012-12-06 20:13:11 +00001637 case SkPath::kWinding_FillType:
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001638 *settings = winding_nv_path_stencil_settings();
1639 return;
1640 default:
1641 GrCrash("Unexpected path fill.");
1642 }
1643}
1644
sugoi@google.com12b4e272012-12-06 20:13:11 +00001645void GrGpuGL::onGpuStencilPath(const GrPath* path, SkPath::FillType fill) {
bsalomon@google.combcce8922013-03-25 15:38:39 +00001646 GrAssert(this->caps()->pathStencilingSupport());
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001647
1648 GrGLuint id = static_cast<const GrGLPath*>(path)->pathID();
1649 GrDrawState* drawState = this->drawState();
1650 GrAssert(NULL != drawState->getRenderTarget());
1651 if (NULL == drawState->getRenderTarget()->getStencilBuffer()) {
1652 return;
1653 }
1654
1655 // Decide how to manipulate the stencil buffer based on the fill rule.
1656 // Also, assert that the stencil settings we set in setStencilPathSettings
1657 // are present.
1658 GrAssert(!fStencilSettings.isTwoSided());
1659 GrGLenum fillMode;
1660 switch (fill) {
sugoi@google.com12b4e272012-12-06 20:13:11 +00001661 case SkPath::kWinding_FillType:
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001662 fillMode = GR_GL_COUNT_UP;
1663 GrAssert(kIncClamp_StencilOp ==
1664 fStencilSettings.passOp(GrStencilSettings::kFront_Face));
1665 GrAssert(kIncClamp_StencilOp ==
1666 fStencilSettings.failOp(GrStencilSettings::kFront_Face));
1667 break;
sugoi@google.com12b4e272012-12-06 20:13:11 +00001668 case SkPath::kEvenOdd_FillType:
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001669 fillMode = GR_GL_INVERT;
1670 GrAssert(kInvert_StencilOp ==
1671 fStencilSettings.passOp(GrStencilSettings::kFront_Face));
1672 GrAssert(kInvert_StencilOp ==
1673 fStencilSettings.failOp(GrStencilSettings::kFront_Face));
1674 break;
1675 default:
1676 // Only the above two fill rules are allowed.
1677 GrCrash("Unexpected path fill.");
bsalomon@google.com548a4332012-07-11 19:45:22 +00001678 return; // suppress unused var warning.
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001679 }
1680 GrGLint writeMask = fStencilSettings.writeMask(GrStencilSettings::kFront_Face);
1681 GL_CALL(StencilFillPath(id, fillMode, writeMask));
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00001682}
1683
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001684void GrGpuGL::onResolveRenderTarget(GrRenderTarget* target) {
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001685 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001686 if (rt->needsResolve()) {
bsalomon@google.com347c3822013-05-01 20:10:01 +00001687 // Some extensions automatically resolves the texture when it is read.
1688 if (this->glCaps().usesMSAARenderBuffers()) {
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001689 GrAssert(rt->textureFBOID() != rt->renderFBOID());
1690 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER, rt->renderFBOID()));
1691 GL_CALL(BindFramebuffer(GR_GL_DRAW_FRAMEBUFFER, rt->textureFBOID()));
1692 // make sure we go through flushRenderTarget() since we've modified
1693 // the bound DRAW FBO ID.
1694 fHWBoundRenderTarget = NULL;
1695 const GrGLIRect& vp = rt->getViewport();
1696 const GrIRect dirtyRect = rt->getResolveRect();
1697 GrGLIRect r;
1698 r.setRelativeTo(vp, dirtyRect.fLeft, dirtyRect.fTop,
1699 dirtyRect.width(), dirtyRect.height(), target->origin());
reed@google.comac10a2d2010-12-22 21:39:39 +00001700
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001701 GrAutoTRestore<ScissorState> asr;
bsalomon@google.com347c3822013-05-01 20:10:01 +00001702 if (GrGLCaps::kES_Apple_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001703 // Apple's extension uses the scissor as the blit bounds.
bsalomon@google.coma3201942012-06-21 19:58:20 +00001704 asr.reset(&fScissorState);
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001705 fScissorState.fEnabled = true;
1706 fScissorState.fRect = dirtyRect;
bsalomon@google.coma3201942012-06-21 19:58:20 +00001707 this->flushScissor();
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001708 GL_CALL(ResolveMultisampleFramebuffer());
1709 } else {
bsalomon@google.com347c3822013-05-01 20:10:01 +00001710 if (GrGLCaps::kDesktop_EXT_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001711 // this respects the scissor during the blit, so disable it.
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001712 asr.reset(&fScissorState);
1713 fScissorState.fEnabled = false;
1714 this->flushScissor();
1715 }
1716 int right = r.fLeft + r.fWidth;
1717 int top = r.fBottom + r.fHeight;
1718 GL_CALL(BlitFramebuffer(r.fLeft, r.fBottom, right, top,
1719 r.fLeft, r.fBottom, right, top,
1720 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001721 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001722 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001723 rt->flagAsResolved();
reed@google.comac10a2d2010-12-22 21:39:39 +00001724 }
1725}
1726
bsalomon@google.com411dad02012-06-05 20:24:20 +00001727namespace {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001728
bsalomon@google.com411dad02012-06-05 20:24:20 +00001729GrGLenum gr_to_gl_stencil_func(GrStencilFunc basicFunc) {
1730 static const GrGLenum gTable[] = {
1731 GR_GL_ALWAYS, // kAlways_StencilFunc
1732 GR_GL_NEVER, // kNever_StencilFunc
1733 GR_GL_GREATER, // kGreater_StencilFunc
1734 GR_GL_GEQUAL, // kGEqual_StencilFunc
1735 GR_GL_LESS, // kLess_StencilFunc
1736 GR_GL_LEQUAL, // kLEqual_StencilFunc,
1737 GR_GL_EQUAL, // kEqual_StencilFunc,
1738 GR_GL_NOTEQUAL, // kNotEqual_StencilFunc,
1739 };
1740 GR_STATIC_ASSERT(GR_ARRAY_COUNT(gTable) == kBasicStencilFuncCount);
1741 GR_STATIC_ASSERT(0 == kAlways_StencilFunc);
1742 GR_STATIC_ASSERT(1 == kNever_StencilFunc);
1743 GR_STATIC_ASSERT(2 == kGreater_StencilFunc);
1744 GR_STATIC_ASSERT(3 == kGEqual_StencilFunc);
1745 GR_STATIC_ASSERT(4 == kLess_StencilFunc);
1746 GR_STATIC_ASSERT(5 == kLEqual_StencilFunc);
1747 GR_STATIC_ASSERT(6 == kEqual_StencilFunc);
1748 GR_STATIC_ASSERT(7 == kNotEqual_StencilFunc);
1749 GrAssert((unsigned) basicFunc < kBasicStencilFuncCount);
1750
1751 return gTable[basicFunc];
1752}
1753
1754GrGLenum gr_to_gl_stencil_op(GrStencilOp op) {
1755 static const GrGLenum gTable[] = {
1756 GR_GL_KEEP, // kKeep_StencilOp
1757 GR_GL_REPLACE, // kReplace_StencilOp
1758 GR_GL_INCR_WRAP, // kIncWrap_StencilOp
1759 GR_GL_INCR, // kIncClamp_StencilOp
1760 GR_GL_DECR_WRAP, // kDecWrap_StencilOp
1761 GR_GL_DECR, // kDecClamp_StencilOp
1762 GR_GL_ZERO, // kZero_StencilOp
1763 GR_GL_INVERT, // kInvert_StencilOp
1764 };
1765 GR_STATIC_ASSERT(GR_ARRAY_COUNT(gTable) == kStencilOpCount);
1766 GR_STATIC_ASSERT(0 == kKeep_StencilOp);
1767 GR_STATIC_ASSERT(1 == kReplace_StencilOp);
1768 GR_STATIC_ASSERT(2 == kIncWrap_StencilOp);
1769 GR_STATIC_ASSERT(3 == kIncClamp_StencilOp);
1770 GR_STATIC_ASSERT(4 == kDecWrap_StencilOp);
1771 GR_STATIC_ASSERT(5 == kDecClamp_StencilOp);
1772 GR_STATIC_ASSERT(6 == kZero_StencilOp);
1773 GR_STATIC_ASSERT(7 == kInvert_StencilOp);
1774 GrAssert((unsigned) op < kStencilOpCount);
1775 return gTable[op];
1776}
1777
1778void set_gl_stencil(const GrGLInterface* gl,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001779 const GrStencilSettings& settings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00001780 GrGLenum glFace,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001781 GrStencilSettings::Face grFace) {
1782 GrGLenum glFunc = gr_to_gl_stencil_func(settings.func(grFace));
1783 GrGLenum glFailOp = gr_to_gl_stencil_op(settings.failOp(grFace));
1784 GrGLenum glPassOp = gr_to_gl_stencil_op(settings.passOp(grFace));
1785
1786 GrGLint ref = settings.funcRef(grFace);
1787 GrGLint mask = settings.funcMask(grFace);
1788 GrGLint writeMask = settings.writeMask(grFace);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001789
1790 if (GR_GL_FRONT_AND_BACK == glFace) {
1791 // we call the combined func just in case separate stencil is not
1792 // supported.
1793 GR_GL_CALL(gl, StencilFunc(glFunc, ref, mask));
1794 GR_GL_CALL(gl, StencilMask(writeMask));
1795 GR_GL_CALL(gl, StencilOp(glFailOp, glPassOp, glPassOp));
1796 } else {
1797 GR_GL_CALL(gl, StencilFuncSeparate(glFace, glFunc, ref, mask));
1798 GR_GL_CALL(gl, StencilMaskSeparate(glFace, writeMask));
1799 GR_GL_CALL(gl, StencilOpSeparate(glFace, glFailOp, glPassOp, glPassOp));
1800 }
1801}
1802}
bsalomon@google.comd302f142011-03-03 13:54:13 +00001803
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001804void GrGpuGL::flushStencil(DrawType type) {
1805 if (kStencilPath_DrawType == type) {
1806 GrAssert(!fStencilSettings.isTwoSided());
1807 // Just the func, ref, and mask is set here. The op and write mask are params to the call
1808 // that draws the path to the SB (glStencilFillPath)
1809 GrGLenum func =
1810 gr_to_gl_stencil_func(fStencilSettings.func(GrStencilSettings::kFront_Face));
1811 GL_CALL(PathStencilFunc(func,
1812 fStencilSettings.funcRef(GrStencilSettings::kFront_Face),
1813 fStencilSettings.funcMask(GrStencilSettings::kFront_Face)));
1814 } else if (fHWStencilSettings != fStencilSettings) {
1815 if (fStencilSettings.isDisabled()) {
1816 if (kNo_TriState != fHWStencilTestEnabled) {
1817 GL_CALL(Disable(GR_GL_STENCIL_TEST));
1818 fHWStencilTestEnabled = kNo_TriState;
1819 }
1820 } else {
1821 if (kYes_TriState != fHWStencilTestEnabled) {
1822 GL_CALL(Enable(GR_GL_STENCIL_TEST));
1823 fHWStencilTestEnabled = kYes_TriState;
1824 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001825 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001826 if (!fStencilSettings.isDisabled()) {
bsalomon@google.combcce8922013-03-25 15:38:39 +00001827 if (this->caps()->twoSidedStencilSupport()) {
bsalomon@google.com411dad02012-06-05 20:24:20 +00001828 set_gl_stencil(this->glInterface(),
bsalomon@google.coma3201942012-06-21 19:58:20 +00001829 fStencilSettings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00001830 GR_GL_FRONT,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001831 GrStencilSettings::kFront_Face);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001832 set_gl_stencil(this->glInterface(),
bsalomon@google.coma3201942012-06-21 19:58:20 +00001833 fStencilSettings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00001834 GR_GL_BACK,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001835 GrStencilSettings::kBack_Face);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001836 } else {
bsalomon@google.com411dad02012-06-05 20:24:20 +00001837 set_gl_stencil(this->glInterface(),
bsalomon@google.coma3201942012-06-21 19:58:20 +00001838 fStencilSettings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00001839 GR_GL_FRONT_AND_BACK,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001840 GrStencilSettings::kFront_Face);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001841 }
1842 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001843 fHWStencilSettings = fStencilSettings;
reed@google.comac10a2d2010-12-22 21:39:39 +00001844 }
1845}
1846
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001847void GrGpuGL::flushAAState(DrawType type) {
bsalomon@google.com202d1392013-03-19 18:58:08 +00001848// At least some ATI linux drivers will render GL_LINES incorrectly when MSAA state is enabled but
1849// the target is not multisampled. Single pixel wide lines are rendered thicker than 1 pixel wide.
1850#if 0
1851 // Replace RT_HAS_MSAA with this definition once this driver bug is no longer a relevant concern
1852 #define RT_HAS_MSAA rt->isMultisampled()
1853#else
1854 #define RT_HAS_MSAA (rt->isMultisampled() || kDrawLines_DrawType == type)
1855#endif
1856
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001857 const GrRenderTarget* rt = this->getDrawState().getRenderTarget();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001858 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001859 // ES doesn't support toggling GL_MULTISAMPLE and doesn't have
1860 // smooth lines.
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001861 // we prefer smooth lines over multisampled lines
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00001862 bool smoothLines = false;
1863
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001864 if (kDrawLines_DrawType == type) {
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00001865 smoothLines = this->willUseHWAALines();
1866 if (smoothLines) {
1867 if (kYes_TriState != fHWAAState.fSmoothLineEnabled) {
1868 GL_CALL(Enable(GR_GL_LINE_SMOOTH));
1869 fHWAAState.fSmoothLineEnabled = kYes_TriState;
1870 // must disable msaa to use line smoothing
bsalomon@google.com202d1392013-03-19 18:58:08 +00001871 if (RT_HAS_MSAA &&
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00001872 kNo_TriState != fHWAAState.fMSAAEnabled) {
1873 GL_CALL(Disable(GR_GL_MULTISAMPLE));
1874 fHWAAState.fMSAAEnabled = kNo_TriState;
1875 }
1876 }
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001877 } else {
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00001878 if (kNo_TriState != fHWAAState.fSmoothLineEnabled) {
1879 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
1880 fHWAAState.fSmoothLineEnabled = kNo_TriState;
1881 }
1882 }
1883 }
bsalomon@google.com202d1392013-03-19 18:58:08 +00001884 if (!smoothLines && RT_HAS_MSAA) {
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001885 // FIXME: GL_NV_pr doesn't seem to like MSAA disabled. The paths
1886 // convex hulls of each segment appear to get filled.
1887 bool enableMSAA = kStencilPath_DrawType == type ||
1888 this->getDrawState().isHWAntialiasState();
1889 if (enableMSAA) {
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00001890 if (kYes_TriState != fHWAAState.fMSAAEnabled) {
1891 GL_CALL(Enable(GR_GL_MULTISAMPLE));
1892 fHWAAState.fMSAAEnabled = kYes_TriState;
1893 }
1894 } else {
1895 if (kNo_TriState != fHWAAState.fMSAAEnabled) {
1896 GL_CALL(Disable(GR_GL_MULTISAMPLE));
1897 fHWAAState.fMSAAEnabled = kNo_TriState;
1898 }
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001899 }
1900 }
1901 }
1902}
1903
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00001904void GrGpuGL::flushBlend(bool isLines,
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001905 GrBlendCoeff srcCoeff,
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001906 GrBlendCoeff dstCoeff) {
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00001907 if (isLines && this->willUseHWAALines()) {
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001908 if (kYes_TriState != fHWBlendState.fEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001909 GL_CALL(Enable(GR_GL_BLEND));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001910 fHWBlendState.fEnabled = kYes_TriState;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001911 }
bsalomon@google.com47059542012-06-06 20:51:20 +00001912 if (kSA_GrBlendCoeff != fHWBlendState.fSrcCoeff ||
1913 kISA_GrBlendCoeff != fHWBlendState.fDstCoeff) {
1914 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[kSA_GrBlendCoeff],
1915 gXfermodeCoeff2Blend[kISA_GrBlendCoeff]));
1916 fHWBlendState.fSrcCoeff = kSA_GrBlendCoeff;
1917 fHWBlendState.fDstCoeff = kISA_GrBlendCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001918 }
1919 } else {
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001920 // any optimization to disable blending should
1921 // have already been applied and tweaked the coeffs
1922 // to (1, 0).
bsalomon@google.com47059542012-06-06 20:51:20 +00001923 bool blendOff = kOne_GrBlendCoeff == srcCoeff &&
1924 kZero_GrBlendCoeff == dstCoeff;
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001925 if (blendOff) {
1926 if (kNo_TriState != fHWBlendState.fEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001927 GL_CALL(Disable(GR_GL_BLEND));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001928 fHWBlendState.fEnabled = kNo_TriState;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001929 }
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001930 } else {
1931 if (kYes_TriState != fHWBlendState.fEnabled) {
1932 GL_CALL(Enable(GR_GL_BLEND));
1933 fHWBlendState.fEnabled = kYes_TriState;
1934 }
1935 if (fHWBlendState.fSrcCoeff != srcCoeff ||
1936 fHWBlendState.fDstCoeff != dstCoeff) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001937 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
1938 gXfermodeCoeff2Blend[dstCoeff]));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001939 fHWBlendState.fSrcCoeff = srcCoeff;
1940 fHWBlendState.fDstCoeff = dstCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001941 }
bsalomon@google.coma5d056a2012-03-27 15:59:58 +00001942 GrColor blendConst = this->getDrawState().getBlendConstant();
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001943 if ((BlendCoeffReferencesConstant(srcCoeff) ||
1944 BlendCoeffReferencesConstant(dstCoeff)) &&
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001945 (!fHWBlendState.fConstColorValid ||
1946 fHWBlendState.fConstColor != blendConst)) {
bsalomon@google.com75347472012-09-17 17:23:21 +00001947 GrGLfloat c[4];
1948 GrColorToRGBAFloat(blendConst, c);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001949 GL_CALL(BlendColor(c[0], c[1], c[2], c[3]));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001950 fHWBlendState.fConstColor = blendConst;
1951 fHWBlendState.fConstColorValid = true;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001952 }
1953 }
1954 }
1955}
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001956namespace {
1957
bsalomon@google.com6d003d12012-09-11 15:45:20 +00001958inline void set_tex_swizzle(GrGLenum swizzle[4], const GrGLInterface* gl) {
bsalomon@google.com4d063de2012-05-31 17:59:23 +00001959 GR_GL_CALL(gl, TexParameteriv(GR_GL_TEXTURE_2D,
1960 GR_GL_TEXTURE_SWIZZLE_RGBA,
1961 reinterpret_cast<const GrGLint*>(swizzle)));
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001962}
bsalomon@google.comb8670992012-07-25 21:27:09 +00001963
bsalomon@google.com6c76d242012-09-07 16:52:24 +00001964inline GrGLenum tile_to_gl_wrap(SkShader::TileMode tm) {
bsalomon@google.comb8670992012-07-25 21:27:09 +00001965 static const GrGLenum gWrapModes[] = {
1966 GR_GL_CLAMP_TO_EDGE,
1967 GR_GL_REPEAT,
1968 GR_GL_MIRRORED_REPEAT
1969 };
commit-bot@chromium.org5d7ca952013-04-22 20:26:44 +00001970 GR_STATIC_ASSERT(SkShader::kTileModeCount == SK_ARRAY_COUNT(gWrapModes));
bsalomon@google.comb8670992012-07-25 21:27:09 +00001971 GR_STATIC_ASSERT(0 == SkShader::kClamp_TileMode);
1972 GR_STATIC_ASSERT(1 == SkShader::kRepeat_TileMode);
1973 GR_STATIC_ASSERT(2 == SkShader::kMirror_TileMode);
1974 return gWrapModes[tm];
1975}
1976
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001977}
1978
bsalomon@google.com34cccde2013-01-04 18:34:30 +00001979void GrGpuGL::bindTexture(int unitIdx, const GrTextureParams& params, GrGLTexture* texture) {
1980 GrAssert(NULL != texture);
tomhudson@google.comd0c1a062012-07-12 17:23:52 +00001981
bsalomon@google.comb8670992012-07-25 21:27:09 +00001982 // If we created a rt/tex and rendered to it without using a texture and now we're texturing
1983 // from the rt it will still be the last bound texture, but it needs resolving. So keep this
bsalomon@google.com4c883782012-06-04 19:05:11 +00001984 // out of the "last != next" check.
bsalomon@google.com34cccde2013-01-04 18:34:30 +00001985 GrGLRenderTarget* texRT = static_cast<GrGLRenderTarget*>(texture->asRenderTarget());
bsalomon@google.com4c883782012-06-04 19:05:11 +00001986 if (NULL != texRT) {
1987 this->onResolveRenderTarget(texRT);
1988 }
1989
bsalomon@google.com34cccde2013-01-04 18:34:30 +00001990 if (fHWBoundTextures[unitIdx] != texture) {
1991 this->setTextureUnit(unitIdx);
1992 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, texture->textureID()));
1993 fHWBoundTextures[unitIdx] = texture;
bsalomon@google.com4c883782012-06-04 19:05:11 +00001994 }
1995
bsalomon@google.com4c883782012-06-04 19:05:11 +00001996 ResetTimestamp timestamp;
bsalomon@google.com34cccde2013-01-04 18:34:30 +00001997 const GrGLTexture::TexParams& oldTexParams = texture->getCachedTexParams(&timestamp);
bsalomon@google.com4c883782012-06-04 19:05:11 +00001998 bool setAll = timestamp < this->getResetTimestamp();
1999 GrGLTexture::TexParams newTexParams;
2000
bsalomon@google.comb8670992012-07-25 21:27:09 +00002001 newTexParams.fFilter = params.isBilerp() ? GR_GL_LINEAR : GR_GL_NEAREST;
bsalomon@google.com4c883782012-06-04 19:05:11 +00002002
bsalomon@google.comb8670992012-07-25 21:27:09 +00002003 newTexParams.fWrapS = tile_to_gl_wrap(params.getTileModeX());
2004 newTexParams.fWrapT = tile_to_gl_wrap(params.getTileModeY());
bsalomon@google.com4c883782012-06-04 19:05:11 +00002005 memcpy(newTexParams.fSwizzleRGBA,
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002006 GrGLShaderBuilder::GetTexParamSwizzle(texture->config(), this->glCaps()),
bsalomon@google.com4c883782012-06-04 19:05:11 +00002007 sizeof(newTexParams.fSwizzleRGBA));
2008 if (setAll || newTexParams.fFilter != oldTexParams.fFilter) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002009 this->setTextureUnit(unitIdx);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002010 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2011 GR_GL_TEXTURE_MAG_FILTER,
2012 newTexParams.fFilter));
2013 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2014 GR_GL_TEXTURE_MIN_FILTER,
2015 newTexParams.fFilter));
2016 }
2017 if (setAll || newTexParams.fWrapS != oldTexParams.fWrapS) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002018 this->setTextureUnit(unitIdx);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002019 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2020 GR_GL_TEXTURE_WRAP_S,
2021 newTexParams.fWrapS));
2022 }
2023 if (setAll || newTexParams.fWrapT != oldTexParams.fWrapT) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002024 this->setTextureUnit(unitIdx);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002025 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2026 GR_GL_TEXTURE_WRAP_T,
2027 newTexParams.fWrapT));
2028 }
2029 if (this->glCaps().textureSwizzleSupport() &&
2030 (setAll || memcmp(newTexParams.fSwizzleRGBA,
2031 oldTexParams.fSwizzleRGBA,
2032 sizeof(newTexParams.fSwizzleRGBA)))) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002033 this->setTextureUnit(unitIdx);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002034 set_tex_swizzle(newTexParams.fSwizzleRGBA,
2035 this->glInterface());
2036 }
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002037 texture->setCachedTexParams(newTexParams, this->getResetTimestamp());
bsalomon@google.com4c883782012-06-04 19:05:11 +00002038}
2039
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002040void GrGpuGL::flushMiscFixedFunctionState() {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002041
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002042 const GrDrawState& drawState = this->getDrawState();
reed@google.comac10a2d2010-12-22 21:39:39 +00002043
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002044 if (drawState.isDitherState()) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002045 if (kYes_TriState != fHWDitherEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002046 GL_CALL(Enable(GR_GL_DITHER));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002047 fHWDitherEnabled = kYes_TriState;
2048 }
2049 } else {
2050 if (kNo_TriState != fHWDitherEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002051 GL_CALL(Disable(GR_GL_DITHER));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002052 fHWDitherEnabled = kNo_TriState;
reed@google.comac10a2d2010-12-22 21:39:39 +00002053 }
2054 }
2055
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002056 if (drawState.isColorWriteDisabled()) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002057 if (kNo_TriState != fHWWriteToColor) {
2058 GL_CALL(ColorMask(GR_GL_FALSE, GR_GL_FALSE,
2059 GR_GL_FALSE, GR_GL_FALSE));
2060 fHWWriteToColor = kNo_TriState;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002061 }
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002062 } else {
2063 if (kYes_TriState != fHWWriteToColor) {
2064 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
2065 fHWWriteToColor = kYes_TriState;
2066 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00002067 }
2068
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002069 if (fHWDrawFace != drawState.getDrawFace()) {
bsalomon@google.coma5d056a2012-03-27 15:59:58 +00002070 switch (this->getDrawState().getDrawFace()) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002071 case GrDrawState::kCCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002072 GL_CALL(Enable(GR_GL_CULL_FACE));
2073 GL_CALL(CullFace(GR_GL_BACK));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002074 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002075 case GrDrawState::kCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002076 GL_CALL(Enable(GR_GL_CULL_FACE));
2077 GL_CALL(CullFace(GR_GL_FRONT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002078 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002079 case GrDrawState::kBoth_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002080 GL_CALL(Disable(GR_GL_CULL_FACE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002081 break;
2082 default:
2083 GrCrash("Unknown draw face.");
2084 }
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002085 fHWDrawFace = drawState.getDrawFace();
bsalomon@google.comd302f142011-03-03 13:54:13 +00002086 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002087}
2088
reed@google.comac10a2d2010-12-22 21:39:39 +00002089void GrGpuGL::notifyRenderTargetDelete(GrRenderTarget* renderTarget) {
2090 GrAssert(NULL != renderTarget);
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002091 if (fHWBoundRenderTarget == renderTarget) {
2092 fHWBoundRenderTarget = NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +00002093 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002094}
2095
2096void GrGpuGL::notifyTextureDelete(GrGLTexture* texture) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002097 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002098 if (fHWBoundTextures[s] == texture) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002099 // deleting bound texture does implied bind to 0
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002100 fHWBoundTextures[s] = NULL;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002101 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002102 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002103}
2104
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002105bool GrGpuGL::configToGLFormats(GrPixelConfig config,
2106 bool getSizedInternalFormat,
2107 GrGLenum* internalFormat,
2108 GrGLenum* externalFormat,
2109 GrGLenum* externalType) {
2110 GrGLenum dontCare;
2111 if (NULL == internalFormat) {
2112 internalFormat = &dontCare;
2113 }
2114 if (NULL == externalFormat) {
2115 externalFormat = &dontCare;
2116 }
2117 if (NULL == externalType) {
2118 externalType = &dontCare;
2119 }
2120
reed@google.comac10a2d2010-12-22 21:39:39 +00002121 switch (config) {
bsalomon@google.com0342a852012-08-20 19:22:38 +00002122 case kRGBA_8888_GrPixelConfig:
bsalomon@google.comc4364992011-11-07 15:54:49 +00002123 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002124 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002125 if (getSizedInternalFormat) {
2126 *internalFormat = GR_GL_RGBA8;
2127 } else {
2128 *internalFormat = GR_GL_RGBA;
2129 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002130 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.comc4364992011-11-07 15:54:49 +00002131 break;
bsalomon@google.com0342a852012-08-20 19:22:38 +00002132 case kBGRA_8888_GrPixelConfig:
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002133 if (!this->glCaps().bgraFormatSupport()) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002134 return false;
2135 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002136 if (this->glCaps().bgraIsInternalFormat()) {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002137 if (getSizedInternalFormat) {
2138 *internalFormat = GR_GL_BGRA8;
2139 } else {
2140 *internalFormat = GR_GL_BGRA;
2141 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002142 } else {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002143 if (getSizedInternalFormat) {
2144 *internalFormat = GR_GL_RGBA8;
2145 } else {
2146 *internalFormat = GR_GL_RGBA;
2147 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002148 }
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002149 *externalFormat = GR_GL_BGRA;
bsalomon@google.com6f379512011-11-16 20:36:03 +00002150 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002151 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002152 case kRGB_565_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002153 *internalFormat = GR_GL_RGB;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002154 *externalFormat = GR_GL_RGB;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002155 if (getSizedInternalFormat) {
2156 if (this->glBinding() == kDesktop_GrGLBinding) {
2157 return false;
2158 } else {
2159 *internalFormat = GR_GL_RGB565;
2160 }
2161 } else {
2162 *internalFormat = GR_GL_RGB;
2163 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002164 *externalType = GR_GL_UNSIGNED_SHORT_5_6_5;
reed@google.comac10a2d2010-12-22 21:39:39 +00002165 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002166 case kRGBA_4444_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002167 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002168 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002169 if (getSizedInternalFormat) {
2170 *internalFormat = GR_GL_RGBA4;
2171 } else {
2172 *internalFormat = GR_GL_RGBA;
2173 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002174 *externalType = GR_GL_UNSIGNED_SHORT_4_4_4_4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002175 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002176 case kIndex_8_GrPixelConfig:
bsalomon@google.combcce8922013-03-25 15:38:39 +00002177 if (this->caps()->eightBitPaletteSupport()) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002178 *internalFormat = GR_GL_PALETTE8_RGBA8;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002179 // glCompressedTexImage doesn't take external params
2180 *externalFormat = GR_GL_PALETTE8_RGBA8;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002181 // no sized/unsized internal format distinction here
2182 *internalFormat = GR_GL_PALETTE8_RGBA8;
2183 // unused with CompressedTexImage
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002184 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002185 } else {
2186 return false;
2187 }
2188 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002189 case kAlpha_8_GrPixelConfig:
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002190 if (this->glCaps().textureRedSupport()) {
2191 *internalFormat = GR_GL_RED;
2192 *externalFormat = GR_GL_RED;
2193 if (getSizedInternalFormat) {
2194 *internalFormat = GR_GL_R8;
2195 } else {
2196 *internalFormat = GR_GL_RED;
2197 }
2198 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002199 } else {
2200 *internalFormat = GR_GL_ALPHA;
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002201 *externalFormat = GR_GL_ALPHA;
2202 if (getSizedInternalFormat) {
2203 *internalFormat = GR_GL_ALPHA8;
2204 } else {
2205 *internalFormat = GR_GL_ALPHA;
2206 }
2207 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002208 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002209 break;
2210 default:
2211 return false;
2212 }
2213 return true;
2214}
2215
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002216void GrGpuGL::setTextureUnit(int unit) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002217 GrAssert(unit >= 0 && unit < GrDrawState::kNumStages);
bsalomon@google.com49209392012-06-05 15:13:46 +00002218 if (fHWActiveTextureUnitIdx != unit) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002219 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com49209392012-06-05 15:13:46 +00002220 fHWActiveTextureUnitIdx = unit;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002221 }
2222}
bsalomon@google.com316f99232011-01-13 21:28:12 +00002223
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002224void GrGpuGL::setSpareTextureUnit() {
bsalomon@google.com49209392012-06-05 15:13:46 +00002225 if (fHWActiveTextureUnitIdx != (GR_GL_TEXTURE0 + SPARE_TEX_UNIT)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002226 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + SPARE_TEX_UNIT));
bsalomon@google.com49209392012-06-05 15:13:46 +00002227 fHWActiveTextureUnitIdx = SPARE_TEX_UNIT;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002228 }
2229}
2230
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002231namespace {
2232// Determines whether glBlitFramebuffer could be used between src and dst.
bsalomon@google.comeb851172013-04-15 13:51:00 +00002233inline bool can_blit_framebuffer(const GrSurface* dst,
2234 const GrSurface* src,
2235 const GrGpuGL* gpu,
2236 bool* wouldNeedTempFBO = NULL) {
bsalomon@google.com347c3822013-05-01 20:10:01 +00002237 if (gpu->isConfigRenderable(dst->config()) &&
2238 gpu->isConfigRenderable(src->config()) &&
2239 gpu->glCaps().usesMSAARenderBuffers()) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002240 if (NULL != wouldNeedTempFBO) {
2241 *wouldNeedTempFBO = NULL == dst->asRenderTarget() || NULL == src->asRenderTarget();
2242 }
2243 return true;
2244 } else {
2245 return false;
2246 }
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002247}
bsalomon@google.comeb851172013-04-15 13:51:00 +00002248
2249inline bool can_copy_texsubimage(const GrSurface* dst,
2250 const GrSurface* src,
2251 const GrGpuGL* gpu,
2252 bool* wouldNeedTempFBO = NULL) {
2253 // Table 3.9 of the ES2 spec indicates the supported formats with CopyTexSubImage
2254 // and BGRA isn't in the spec. There doesn't appear to be any extension that adds it. Perhaps
2255 // many drivers would allow it to work, but ANGLE does not.
2256 if (kES2_GrGLBinding == gpu->glBinding() && gpu->glCaps().bgraIsInternalFormat() &&
2257 (kBGRA_8888_GrPixelConfig == dst->config() || kBGRA_8888_GrPixelConfig == src->config())) {
2258 return false;
2259 }
2260 const GrGLRenderTarget* dstRT = static_cast<const GrGLRenderTarget*>(dst->asRenderTarget());
2261 // If dst is multisampled (and uses an extension where there is a separate MSAA renderbuffer)
2262 // then we don't want to copy to the texture but to the MSAA buffer.
2263 if (NULL != dstRT && dstRT->renderFBOID() != dstRT->textureFBOID()) {
2264 return false;
2265 }
bsalomon@google.coma2719852013-04-17 14:25:27 +00002266 const GrGLRenderTarget* srcRT = static_cast<const GrGLRenderTarget*>(src->asRenderTarget());
2267 // If the src is multisampled (and uses an extension where there is a separate MSAA
2268 // renderbuffer) then it is an invalid operation to call CopyTexSubImage
2269 if (NULL != srcRT && srcRT->renderFBOID() != srcRT->textureFBOID()) {
2270 return false;
2271 }
bsalomon@google.comeb851172013-04-15 13:51:00 +00002272 if (gpu->isConfigRenderable(src->config()) && NULL != dst->asTexture() &&
2273 dst->origin() == src->origin() && kIndex_8_GrPixelConfig != src->config()) {
2274 if (NULL != wouldNeedTempFBO) {
2275 *wouldNeedTempFBO = NULL == src->asRenderTarget();
2276 }
2277 return true;
2278 } else {
2279 return false;
2280 }
2281}
2282
2283// If a temporary FBO was created, its non-zero ID is returned. The viewport that the copy rect is
2284// relative to is output.
2285inline GrGLuint bind_surface_as_fbo(const GrGLInterface* gl,
2286 GrSurface* surface,
2287 GrGLenum fboTarget,
2288 GrGLIRect* viewport) {
2289 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(surface->asRenderTarget());
2290 GrGLuint tempFBOID;
2291 if (NULL == rt) {
2292 GrAssert(NULL != surface->asTexture());
2293 GrGLuint texID = static_cast<GrGLTexture*>(surface->asTexture())->textureID();
2294 GR_GL_CALL(gl, GenFramebuffers(1, &tempFBOID));
2295 GR_GL_CALL(gl, BindFramebuffer(fboTarget, tempFBOID));
2296 GR_GL_CALL(gl, FramebufferTexture2D(fboTarget,
2297 GR_GL_COLOR_ATTACHMENT0,
2298 GR_GL_TEXTURE_2D,
2299 texID,
2300 0));
2301 viewport->fLeft = 0;
2302 viewport->fBottom = 0;
2303 viewport->fWidth = surface->width();
2304 viewport->fHeight = surface->height();
2305 } else {
2306 tempFBOID = 0;
2307 GR_GL_CALL(gl, BindFramebuffer(fboTarget, rt->renderFBOID()));
2308 *viewport = rt->getViewport();
2309 }
2310 return tempFBOID;
2311}
2312
2313}
2314
2315void GrGpuGL::initCopySurfaceDstDesc(const GrSurface* src, GrTextureDesc* desc) {
2316 // Check for format issues with glCopyTexSubImage2D
2317 if (kES2_GrGLBinding == this->glBinding() && this->glCaps().bgraIsInternalFormat() &&
2318 kBGRA_8888_GrPixelConfig == src->config()) {
2319 // glCopyTexSubImage2D doesn't work with this config. We'll want to make it a render target
bsalomon@google.com31c4e892013-04-15 13:55:02 +00002320 // in order to call glBlitFramebuffer or to copy to it by rendering.
bsalomon@google.comeb851172013-04-15 13:51:00 +00002321 INHERITED::initCopySurfaceDstDesc(src, desc);
bsalomon@google.coma2719852013-04-17 14:25:27 +00002322 return;
bsalomon@google.comeb851172013-04-15 13:51:00 +00002323 } else if (NULL == src->asRenderTarget()) {
2324 // We don't want to have to create an FBO just to use glCopyTexSubImage2D. Let the base
2325 // class handle it by rendering.
2326 INHERITED::initCopySurfaceDstDesc(src, desc);
bsalomon@google.coma2719852013-04-17 14:25:27 +00002327 return;
2328 }
2329
2330 const GrGLRenderTarget* srcRT = static_cast<const GrGLRenderTarget*>(src->asRenderTarget());
2331 if (NULL != srcRT && srcRT->renderFBOID() != srcRT->textureFBOID()) {
2332 // It's illegal to call CopyTexSubImage2D on a MSAA renderbuffer.
2333 INHERITED::initCopySurfaceDstDesc(src, desc);
bsalomon@google.comeb851172013-04-15 13:51:00 +00002334 } else {
2335 desc->fConfig = src->config();
2336 desc->fOrigin = src->origin();
2337 desc->fFlags = kNone_GrTextureFlags;
2338 }
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002339}
2340
2341bool GrGpuGL::onCopySurface(GrSurface* dst,
2342 GrSurface* src,
2343 const SkIRect& srcRect,
2344 const SkIPoint& dstPoint) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002345 bool inheritedCouldCopy = INHERITED::onCanCopySurface(dst, src, srcRect, dstPoint);
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002346 bool copied = false;
bsalomon@google.comeb851172013-04-15 13:51:00 +00002347 bool wouldNeedTempFBO = false;
2348 if (can_copy_texsubimage(dst, src, this, &wouldNeedTempFBO) &&
2349 (!wouldNeedTempFBO || !inheritedCouldCopy)) {
2350 GrGLuint srcFBO;
2351 GrGLIRect srcVP;
2352 srcFBO = bind_surface_as_fbo(this->glInterface(), src, GR_GL_FRAMEBUFFER, &srcVP);
2353 GrGLTexture* dstTex = static_cast<GrGLTexture*>(dst->asTexture());
2354 GrAssert(NULL != dstTex);
2355 // We modified the bound FBO
2356 fHWBoundRenderTarget = NULL;
2357 GrGLIRect srcGLRect;
2358 srcGLRect.setRelativeTo(srcVP,
2359 srcRect.fLeft,
2360 srcRect.fTop,
2361 srcRect.width(),
2362 srcRect.height(),
2363 src->origin());
2364
2365 this->setSpareTextureUnit();
2366 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, dstTex->textureID()));
2367 GrGLint dstY;
2368 if (kBottomLeft_GrSurfaceOrigin == dst->origin()) {
2369 dstY = dst->height() - (dstPoint.fY + srcGLRect.fHeight);
2370 } else {
2371 dstY = dstPoint.fY;
2372 }
2373 GL_CALL(CopyTexSubImage2D(GR_GL_TEXTURE_2D, 0,
2374 dstPoint.fX, dstY,
2375 srcGLRect.fLeft, srcGLRect.fBottom,
2376 srcGLRect.fWidth, srcGLRect.fHeight));
2377 copied = true;
2378 if (srcFBO) {
2379 GL_CALL(DeleteFramebuffers(1, &srcFBO));
2380 }
2381 } else if (can_blit_framebuffer(dst, src, this, &wouldNeedTempFBO) &&
2382 (!wouldNeedTempFBO || !inheritedCouldCopy)) {
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002383 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
2384 srcRect.width(), srcRect.height());
2385 bool selfOverlap = false;
2386 if (dst->isSameAs(src)) {
2387 selfOverlap = SkIRect::IntersectsNoEmptyCheck(dstRect, srcRect);
2388 }
2389
2390 if (!selfOverlap) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002391 GrGLuint dstFBO;
2392 GrGLuint srcFBO;
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002393 GrGLIRect dstVP;
2394 GrGLIRect srcVP;
bsalomon@google.comeb851172013-04-15 13:51:00 +00002395 dstFBO = bind_surface_as_fbo(this->glInterface(), dst, GR_GL_DRAW_FRAMEBUFFER, &dstVP);
2396 srcFBO = bind_surface_as_fbo(this->glInterface(), src, GR_GL_READ_FRAMEBUFFER, &srcVP);
2397 // We modified the bound FBO
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002398 fHWBoundRenderTarget = NULL;
2399 GrGLIRect srcGLRect;
2400 GrGLIRect dstGLRect;
2401 srcGLRect.setRelativeTo(srcVP,
2402 srcRect.fLeft,
2403 srcRect.fTop,
2404 srcRect.width(),
2405 srcRect.height(),
2406 src->origin());
2407 dstGLRect.setRelativeTo(dstVP,
2408 dstRect.fLeft,
2409 dstRect.fTop,
2410 dstRect.width(),
2411 dstRect.height(),
2412 dst->origin());
2413
2414 GrAutoTRestore<ScissorState> asr;
bsalomon@google.com347c3822013-05-01 20:10:01 +00002415 if (GrGLCaps::kDesktop_EXT_MSFBOType == this->glCaps().msFBOType()) {
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002416 // The EXT version applies the scissor during the blit, so disable it.
2417 asr.reset(&fScissorState);
2418 fScissorState.fEnabled = false;
2419 this->flushScissor();
2420 }
2421 GrGLint srcY0;
2422 GrGLint srcY1;
2423 // Does the blit need to y-mirror or not?
2424 if (src->origin() == dst->origin()) {
2425 srcY0 = srcGLRect.fBottom;
2426 srcY1 = srcGLRect.fBottom + srcGLRect.fHeight;
2427 } else {
2428 srcY0 = srcGLRect.fBottom + srcGLRect.fHeight;
2429 srcY1 = srcGLRect.fBottom;
2430 }
2431 GL_CALL(BlitFramebuffer(srcGLRect.fLeft,
2432 srcY0,
2433 srcGLRect.fLeft + srcGLRect.fWidth,
2434 srcY1,
2435 dstGLRect.fLeft,
2436 dstGLRect.fBottom,
2437 dstGLRect.fLeft + dstGLRect.fWidth,
2438 dstGLRect.fBottom + dstGLRect.fHeight,
2439 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
2440 if (dstFBO) {
2441 GL_CALL(DeleteFramebuffers(1, &dstFBO));
2442 }
2443 if (srcFBO) {
2444 GL_CALL(DeleteFramebuffers(1, &srcFBO));
2445 }
2446 copied = true;
2447 }
2448 }
bsalomon@google.comeb851172013-04-15 13:51:00 +00002449 if (!copied && inheritedCouldCopy) {
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002450 copied = INHERITED::onCopySurface(dst, src, srcRect, dstPoint);
bsalomon@google.comeb851172013-04-15 13:51:00 +00002451 GrAssert(copied);
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002452 }
2453 return copied;
2454}
2455
2456bool GrGpuGL::onCanCopySurface(GrSurface* dst,
2457 GrSurface* src,
2458 const SkIRect& srcRect,
2459 const SkIPoint& dstPoint) {
2460 // This mirrors the logic in onCopySurface.
bsalomon@google.comeb851172013-04-15 13:51:00 +00002461 if (can_copy_texsubimage(dst, src, this)) {
2462 return true;
2463 }
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002464 if (can_blit_framebuffer(dst, src, this)) {
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002465 if (dst->isSameAs(src)) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002466 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
2467 srcRect.width(), srcRect.height());
2468 if(!SkIRect::IntersectsNoEmptyCheck(dstRect, srcRect)) {
2469 return true;
2470 }
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002471 } else {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002472 return true;
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002473 }
2474 }
bsalomon@google.comeb851172013-04-15 13:51:00 +00002475 return INHERITED::onCanCopySurface(dst, src, srcRect, dstPoint);
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002476}
2477
2478
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00002479///////////////////////////////////////////////////////////////////////////////
2480
bsalomon@google.com6918d482013-03-07 19:09:11 +00002481GrGLAttribArrayState* GrGpuGL::HWGeometryState::bindArrayAndBuffersToDraw(
2482 GrGpuGL* gpu,
2483 const GrGLVertexBuffer* vbuffer,
2484 const GrGLIndexBuffer* ibuffer) {
2485 GrAssert(NULL != vbuffer);
robertphillips@google.com4f65a272013-03-26 19:40:46 +00002486 GrGLAttribArrayState* attribState;
2487
bsalomon@google.com6918d482013-03-07 19:09:11 +00002488 // We use a vertex array if we're on a core profile and the verts are in a VBO.
2489 if (gpu->glCaps().isCoreProfile() && !vbuffer->isCPUBacked()) {
2490 if (NULL == fVBOVertexArray || !fVBOVertexArray->isValid()) {
2491 SkSafeUnref(fVBOVertexArray);
2492 GrGLuint arrayID;
2493 GR_GL_CALL(gpu->glInterface(), GenVertexArrays(1, &arrayID));
2494 int attrCount = gpu->glCaps().maxVertexAttributes();
2495 fVBOVertexArray = SkNEW_ARGS(GrGLVertexArray, (gpu, arrayID, attrCount));
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00002496 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00002497 attribState = fVBOVertexArray->bindWithIndexBuffer(ibuffer);
2498 } else {
2499 if (NULL != ibuffer) {
2500 this->setIndexBufferIDOnDefaultVertexArray(gpu, ibuffer->bufferID());
2501 } else {
2502 this->setVertexArrayID(gpu, 0);
2503 }
2504 int attrCount = gpu->glCaps().maxVertexAttributes();
2505 if (fDefaultVertexArrayAttribState.count() != attrCount) {
2506 fDefaultVertexArrayAttribState.resize(attrCount);
2507 }
2508 attribState = &fDefaultVertexArrayAttribState;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00002509 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00002510 return attribState;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002511}