blob: 28e41af5f040f0eeb6281bde4fb968865ea919aa [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.com8531c1c2011-01-13 19:52:45 +000023
reed@google.comac10a2d2010-12-22 21:39:39 +000024#define SKIP_CACHE_CHECK true
25
bsalomon@google.com4f3c2532012-01-19 16:16:52 +000026#if GR_GL_CHECK_ALLOC_WITH_GET_ERROR
27 #define CLEAR_ERROR_BEFORE_ALLOC(iface) GrGLClearErr(iface)
28 #define GL_ALLOC_CALL(iface, call) GR_GL_CALL_NOERRCHECK(iface, call)
29 #define CHECK_ALLOC_ERROR(iface) GR_GL_GET_ERROR(iface)
rmistry@google.comfbfcd562012-08-23 18:09:54 +000030#else
bsalomon@google.com4f3c2532012-01-19 16:16:52 +000031 #define CLEAR_ERROR_BEFORE_ALLOC(iface)
32 #define GL_ALLOC_CALL(iface, call) GR_GL_CALL(iface, call)
33 #define CHECK_ALLOC_ERROR(iface) GR_GL_NO_ERROR
34#endif
35
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +000036
37///////////////////////////////////////////////////////////////////////////////
38
twiz@google.com0f31ca72011-03-18 17:38:11 +000039static const GrGLenum gXfermodeCoeff2Blend[] = {
40 GR_GL_ZERO,
41 GR_GL_ONE,
42 GR_GL_SRC_COLOR,
43 GR_GL_ONE_MINUS_SRC_COLOR,
44 GR_GL_DST_COLOR,
45 GR_GL_ONE_MINUS_DST_COLOR,
46 GR_GL_SRC_ALPHA,
47 GR_GL_ONE_MINUS_SRC_ALPHA,
48 GR_GL_DST_ALPHA,
49 GR_GL_ONE_MINUS_DST_ALPHA,
50 GR_GL_CONSTANT_COLOR,
51 GR_GL_ONE_MINUS_CONSTANT_COLOR,
52 GR_GL_CONSTANT_ALPHA,
53 GR_GL_ONE_MINUS_CONSTANT_ALPHA,
bsalomon@google.com271cffc2011-05-20 14:13:56 +000054
55 // extended blend coeffs
56 GR_GL_SRC1_COLOR,
57 GR_GL_ONE_MINUS_SRC1_COLOR,
58 GR_GL_SRC1_ALPHA,
59 GR_GL_ONE_MINUS_SRC1_ALPHA,
reed@google.comac10a2d2010-12-22 21:39:39 +000060};
61
bsalomon@google.com271cffc2011-05-20 14:13:56 +000062bool GrGpuGL::BlendCoeffReferencesConstant(GrBlendCoeff coeff) {
bsalomon@google.com080773c2011-03-15 19:09:25 +000063 static const bool gCoeffReferencesBlendConst[] = {
64 false,
65 false,
66 false,
67 false,
68 false,
69 false,
70 false,
71 false,
72 false,
73 false,
74 true,
75 true,
76 true,
77 true,
bsalomon@google.com271cffc2011-05-20 14:13:56 +000078
79 // extended blend coeffs
80 false,
81 false,
82 false,
83 false,
bsalomon@google.com080773c2011-03-15 19:09:25 +000084 };
85 return gCoeffReferencesBlendConst[coeff];
bsalomon@google.com47059542012-06-06 20:51:20 +000086 GR_STATIC_ASSERT(kTotalGrBlendCoeffCount ==
87 GR_ARRAY_COUNT(gCoeffReferencesBlendConst));
bsalomon@google.com271cffc2011-05-20 14:13:56 +000088
bsalomon@google.com47059542012-06-06 20:51:20 +000089 GR_STATIC_ASSERT(0 == kZero_GrBlendCoeff);
90 GR_STATIC_ASSERT(1 == kOne_GrBlendCoeff);
91 GR_STATIC_ASSERT(2 == kSC_GrBlendCoeff);
92 GR_STATIC_ASSERT(3 == kISC_GrBlendCoeff);
93 GR_STATIC_ASSERT(4 == kDC_GrBlendCoeff);
94 GR_STATIC_ASSERT(5 == kIDC_GrBlendCoeff);
95 GR_STATIC_ASSERT(6 == kSA_GrBlendCoeff);
96 GR_STATIC_ASSERT(7 == kISA_GrBlendCoeff);
97 GR_STATIC_ASSERT(8 == kDA_GrBlendCoeff);
98 GR_STATIC_ASSERT(9 == kIDA_GrBlendCoeff);
99 GR_STATIC_ASSERT(10 == kConstC_GrBlendCoeff);
100 GR_STATIC_ASSERT(11 == kIConstC_GrBlendCoeff);
101 GR_STATIC_ASSERT(12 == kConstA_GrBlendCoeff);
102 GR_STATIC_ASSERT(13 == kIConstA_GrBlendCoeff);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000103
bsalomon@google.com47059542012-06-06 20:51:20 +0000104 GR_STATIC_ASSERT(14 == kS2C_GrBlendCoeff);
105 GR_STATIC_ASSERT(15 == kIS2C_GrBlendCoeff);
106 GR_STATIC_ASSERT(16 == kS2A_GrBlendCoeff);
107 GR_STATIC_ASSERT(17 == kIS2A_GrBlendCoeff);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000108
109 // assertion for gXfermodeCoeff2Blend have to be in GrGpu scope
bsalomon@google.com47059542012-06-06 20:51:20 +0000110 GR_STATIC_ASSERT(kTotalGrBlendCoeffCount ==
111 GR_ARRAY_COUNT(gXfermodeCoeff2Blend));
bsalomon@google.com080773c2011-03-15 19:09:25 +0000112}
113
reed@google.comac10a2d2010-12-22 21:39:39 +0000114///////////////////////////////////////////////////////////////////////////////
115
rileya@google.come38160c2012-07-03 18:03:04 +0000116static bool gPrintStartupSpew;
bsalomon@google.com42ab7ea2011-01-19 17:19:40 +0000117
robertphillips@google.com6177e692013-02-28 20:16:25 +0000118GrGpuGL::GrGpuGL(const GrGLContext& ctx, GrContext* context)
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000119 : GrGpu(context)
robertphillips@google.com6177e692013-02-28 20:16:25 +0000120 , fGLContext(ctx) {
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000121
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000122 SkASSERT(ctx.isInitialized());
tomhudson@google.com747bf292011-06-14 18:16:52 +0000123
bsalomon@google.combcce8922013-03-25 15:38:39 +0000124 fCaps.reset(SkRef(ctx.info().caps()));
125
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +0000126 fHWBoundTextures.reset(ctx.info().caps()->maxFragmentTextureUnits());
127
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000128 fillInConfigRenderableTable();
129
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000130
robertphillips@google.com6177e692013-02-28 20:16:25 +0000131 GrGLClearErr(fGLContext.interface());
reed@google.comac10a2d2010-12-22 21:39:39 +0000132
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000133 if (gPrintStartupSpew) {
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000134 const GrGLubyte* vendor;
135 const GrGLubyte* renderer;
136 const GrGLubyte* version;
137 GL_CALL_RET(vendor, GetString(GR_GL_VENDOR));
138 GL_CALL_RET(renderer, GetString(GR_GL_RENDERER));
139 GL_CALL_RET(version, GetString(GR_GL_VERSION));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000140 GrPrintf("------------------------- create GrGpuGL %p --------------\n",
141 this);
142 GrPrintf("------ VENDOR %s\n", vendor);
143 GrPrintf("------ RENDERER %s\n", renderer);
144 GrPrintf("------ VERSION %s\n", version);
bsalomon@google.com00142c42013-05-02 19:42:54 +0000145 GrPrintf("------ EXTENSIONS\n");
146 ctx.info().extensions().print();
147 GrPrintf("\n");
bsalomon@google.combcce8922013-03-25 15:38:39 +0000148 ctx.info().caps()->print();
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000149 }
150
robertphillips@google.com6177e692013-02-28 20:16:25 +0000151 fProgramCache = SkNEW_ARGS(ProgramCache, (this->glContext()));
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000152
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000153 SkASSERT(this->glCaps().maxVertexAttributes() >= GrDrawState::kMaxVertexAttribCnt);
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000154
bsalomon@google.comfe676522011-06-17 18:12:21 +0000155 fLastSuccessfulStencilFmtIdx = 0;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000156 fHWProgramID = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000157}
158
159GrGpuGL::~GrGpuGL() {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000160 if (0 != fHWProgramID) {
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000161 // detach the current program so there is no confusion on OpenGL's part
162 // that we want it to be deleted
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000163 SkASSERT(fHWProgramID == fCurrentProgram->programID());
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000164 GL_CALL(UseProgram(0));
165 }
166
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000167 delete fProgramCache;
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000168
bsalomon@google.com4a018bb2011-10-28 19:50:21 +0000169 // This must be called by before the GrDrawTarget destructor
170 this->releaseGeometry();
bsalomon@google.com15b11df2011-09-16 21:18:29 +0000171 // This subclass must do this before the base class destructor runs
172 // since we will unref the GrGLInterface.
173 this->releaseResources();
reed@google.comac10a2d2010-12-22 21:39:39 +0000174}
175
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000176///////////////////////////////////////////////////////////////////////////////
177
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000178void GrGpuGL::fillInConfigRenderableTable() {
179
180 // OpenGL < 3.0
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000181 // no support for render targets unless the GL_ARB_framebuffer_object
182 // extension is supported (in which case we get ALPHA, RED, RG, RGB,
183 // RGBA (ALPHA8, RGBA4, RGBA8) for OpenGL > 1.1). Note that we
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000184 // probably don't get R8 in this case.
185
186 // OpenGL 3.0
187 // base color renderable: ALPHA, RED, RG, RGB, and RGBA
188 // sized derivatives: ALPHA8, R8, RGBA4, RGBA8
189
190 // >= OpenGL 3.1
191 // base color renderable: RED, RG, RGB, and RGBA
192 // sized derivatives: R8, RGBA4, RGBA8
193 // if the GL_ARB_compatibility extension is supported then we get back
194 // support for GL_ALPHA and ALPHA8
195
196 // GL_EXT_bgra adds BGRA render targets to any version
197
198 // ES 2.0
199 // color renderable: RGBA4, RGB5_A1, RGB565
200 // GL_EXT_texture_rg adds support for R8 as a color render target
201 // GL_OES_rgb8_rgba8 and/or GL_ARM_rgba8 adds support for RGBA8
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000202 // GL_EXT_texture_format_BGRA8888 and/or GL_APPLE_texture_format_BGRA8888 added BGRA support
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000203
commit-bot@chromium.orgc5dffe42013-08-20 15:25:21 +0000204 // ES 3.0
205 // Same as ES 2.0 except R8 and RGBA8 are supported without extensions (the functions called
206 // below already account for this).
207
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000208 if (kDesktop_GrGLBinding == this->glBinding()) {
209 // Post 3.0 we will get R8
210 // Prior to 3.0 we will get ALPHA8 (with GL_ARB_framebuffer_object)
211 if (this->glVersion() >= GR_GL_VER(3,0) ||
212 this->hasExtension("GL_ARB_framebuffer_object")) {
213 fConfigRenderSupport[kAlpha_8_GrPixelConfig] = true;
214 }
215 } else {
216 // On ES we can only hope for R8
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000217 fConfigRenderSupport[kAlpha_8_GrPixelConfig] =
robertphillips@google.com443e5a52012-04-30 20:01:21 +0000218 this->glCaps().textureRedSupport();
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000219 }
220
221 if (kDesktop_GrGLBinding != this->glBinding()) {
222 // only available in ES
223 fConfigRenderSupport[kRGB_565_GrPixelConfig] = true;
224 }
225
reed@google.com6ba45722013-06-21 18:30:53 +0000226 // we no longer support 444 as a render target
227 fConfigRenderSupport[kRGBA_4444_GrPixelConfig] = false;
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000228
229 if (this->glCaps().rgba8RenderbufferSupport()) {
bsalomon@google.com0342a852012-08-20 19:22:38 +0000230 fConfigRenderSupport[kRGBA_8888_GrPixelConfig] = true;
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000231 }
232
233 if (this->glCaps().bgraFormatSupport()) {
bsalomon@google.com0342a852012-08-20 19:22:38 +0000234 fConfigRenderSupport[kBGRA_8888_GrPixelConfig] = true;
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000235 }
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000236}
237
commit-bot@chromium.org28621512013-08-07 19:43:45 +0000238GrPixelConfig GrGpuGL::preferredReadPixelsConfig(GrPixelConfig readConfig,
239 GrPixelConfig surfaceConfig) const {
240 if (GR_GL_RGBA_8888_PIXEL_OPS_SLOW && kRGBA_8888_GrPixelConfig == readConfig) {
bsalomon@google.com9c680582013-02-06 18:17:50 +0000241 return kBGRA_8888_GrPixelConfig;
commit-bot@chromium.org28621512013-08-07 19:43:45 +0000242 } else if (fGLContext.info().isMesa() &&
243 GrBytesPerPixel(readConfig) == 4 &&
244 GrPixelConfigSwapRAndB(readConfig) == surfaceConfig) {
commit-bot@chromium.org5d1d79a2013-05-24 18:52:52 +0000245 // Mesa 3D takes a slow path on when reading back BGRA from an RGBA surface and vice-versa.
246 // Perhaps this should be guarded by some compiletime or runtime check.
247 return surfaceConfig;
commit-bot@chromium.org28621512013-08-07 19:43:45 +0000248 } else if (readConfig == kBGRA_8888_GrPixelConfig &&
249 !this->glCaps().readPixelsSupported(this->glInterface(),
250 GR_GL_BGRA, GR_GL_UNSIGNED_BYTE)) {
251 return kRGBA_8888_GrPixelConfig;
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000252 } else {
commit-bot@chromium.org28621512013-08-07 19:43:45 +0000253 return readConfig;
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000254 }
255}
256
commit-bot@chromium.org5d1d79a2013-05-24 18:52:52 +0000257GrPixelConfig GrGpuGL::preferredWritePixelsConfig(GrPixelConfig writeConfig,
258 GrPixelConfig surfaceConfig) const {
commit-bot@chromium.org28621512013-08-07 19:43:45 +0000259 if (GR_GL_RGBA_8888_PIXEL_OPS_SLOW && kRGBA_8888_GrPixelConfig == writeConfig) {
260 return kBGRA_8888_GrPixelConfig;
261 } else {
262 return writeConfig;
263 }
bsalomon@google.com9c680582013-02-06 18:17:50 +0000264}
265
266bool GrGpuGL::canWriteTexturePixels(const GrTexture* texture, GrPixelConfig srcConfig) const {
267 if (kIndex_8_GrPixelConfig == srcConfig || kIndex_8_GrPixelConfig == texture->config()) {
268 return false;
269 }
bsalomon@google.com791816a2013-08-15 18:54:39 +0000270 if (srcConfig != texture->config() && kES_GrGLBinding == this->glBinding()) {
bsalomon@google.com9c680582013-02-06 18:17:50 +0000271 // In general ES2 requires the internal format of the texture and the format of the src
272 // pixels to match. However, It may or may not be possible to upload BGRA data to a RGBA
273 // texture. It depends upon which extension added BGRA. The Apple extension allows it
274 // (BGRA's internal format is RGBA) while the EXT extension does not (BGRA is its own
275 // internal format).
276 if (this->glCaps().bgraFormatSupport() &&
277 !this->glCaps().bgraIsInternalFormat() &&
278 kBGRA_8888_GrPixelConfig == srcConfig &&
279 kRGBA_8888_GrPixelConfig == texture->config()) {
280 return true;
281 } else {
282 return false;
283 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000284 } else {
bsalomon@google.com9c680582013-02-06 18:17:50 +0000285 return true;
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000286 }
287}
288
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000289bool GrGpuGL::fullReadPixelsIsFasterThanPartial() const {
290 return SkToBool(GR_GL_FULL_READPIXELS_FASTER_THAN_PARTIAL);
291}
292
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000293void GrGpuGL::onResetContext(uint32_t resetBits) {
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000294 // we don't use the zb at all
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000295 if (resetBits & kMisc_GrGLBackendState) {
296 GL_CALL(Disable(GR_GL_DEPTH_TEST));
297 GL_CALL(DepthMask(GR_GL_FALSE));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000298
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000299 fHWDrawFace = GrDrawState::kInvalid_DrawFace;
300 fHWDitherEnabled = kUnknown_TriState;
reed@google.comac10a2d2010-12-22 21:39:39 +0000301
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000302 if (kDesktop_GrGLBinding == this->glBinding()) {
303 // Desktop-only state that we never change
304 if (!this->glCaps().isCoreProfile()) {
305 GL_CALL(Disable(GR_GL_POINT_SMOOTH));
306 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
307 GL_CALL(Disable(GR_GL_POLYGON_SMOOTH));
308 GL_CALL(Disable(GR_GL_POLYGON_STIPPLE));
309 GL_CALL(Disable(GR_GL_COLOR_LOGIC_OP));
310 GL_CALL(Disable(GR_GL_INDEX_LOGIC_OP));
311 }
312 // The windows NVIDIA driver has GL_ARB_imaging in the extension string when using a
313 // core profile. This seems like a bug since the core spec removes any mention of
314 // GL_ARB_imaging.
315 if (this->glCaps().imagingSupport() && !this->glCaps().isCoreProfile()) {
316 GL_CALL(Disable(GR_GL_COLOR_TABLE));
317 }
318 GL_CALL(Disable(GR_GL_POLYGON_OFFSET_FILL));
319 // Since ES doesn't support glPointSize at all we always use the VS to
320 // set the point size
321 GL_CALL(Enable(GR_GL_VERTEX_PROGRAM_POINT_SIZE));
322
323 // We should set glPolygonMode(FRONT_AND_BACK,FILL) here, too. It isn't
324 // currently part of our gl interface. There are probably others as
325 // well.
bsalomon@google.com2b1b8c02013-02-28 22:06:02 +0000326 }
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000327 fHWWriteToColor = kUnknown_TriState;
328 // we only ever use lines in hairline mode
329 GL_CALL(LineWidth(1));
bsalomon@google.comcad107b2013-06-28 14:32:08 +0000330 }
edisonn@google.comba669992013-06-28 16:03:21 +0000331
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000332 if (resetBits & kAA_GrGLBackendState) {
333 fHWAAState.invalidate();
334 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000335
commit-bot@chromium.org46fbfe02013-08-30 15:52:12 +0000336 fHWActiveTextureUnitIdx = -1; // invalid
337
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000338 if (resetBits & kTextureBinding_GrGLBackendState) {
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000339 for (int s = 0; s < fHWBoundTextures.count(); ++s) {
340 fHWBoundTextures[s] = NULL;
341 }
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000342 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000343
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000344 if (resetBits & kBlend_GrGLBackendState) {
345 fHWBlendState.invalidate();
346 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000347
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000348 if (resetBits & kView_GrGLBackendState) {
349 fHWScissorSettings.invalidate();
350 fHWViewport.invalidate();
351 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000352
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000353 if (resetBits & kStencil_GrGLBackendState) {
354 fHWStencilSettings.invalidate();
355 fHWStencilTestEnabled = kUnknown_TriState;
356 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000357
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000358 // Vertex
359 if (resetBits & kVertex_GrGLBackendState) {
360 fHWGeometryState.invalidate();
361 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000362
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000363 if (resetBits & kRenderTarget_GrGLBackendState) {
364 fHWBoundRenderTarget = NULL;
365 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000366
commit-bot@chromium.org3628ad92013-08-30 19:43:47 +0000367 if (resetBits & kFixedFunction_GrGLBackendState && this->glCaps().fixedFunctionSupport()) {
commit-bot@chromium.org46fbfe02013-08-30 15:52:12 +0000368
369 fHWProjectionMatrixState.invalidate();
370 // we don't use the model view matrix.
371 GL_CALL(MatrixMode(GR_GL_MODELVIEW));
372 GL_CALL(LoadIdentity());
373
374 // When we use fixed function vertex processing we always use the vertex array
375 // and none of the other arrays.
376 GL_CALL(EnableClientState(GR_GL_VERTEX_ARRAY));
377 GL_CALL(DisableClientState(GR_GL_NORMAL_ARRAY));
378 GL_CALL(DisableClientState(GR_GL_COLOR_ARRAY));
379 GL_CALL(DisableClientState(GR_GL_INDEX_ARRAY));
380 GL_CALL(DisableClientState(GR_GL_EDGE_FLAG_ARRAY));
381 for (int i = 0; i < this->glCaps().maxFixedFunctionTextureCoords(); ++i) {
382 GL_CALL(ClientActiveTexture(GR_GL_TEXTURE0 + i));
383 GL_CALL(DisableClientState(GR_GL_TEXTURE_COORD_ARRAY));
384
385 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + i));
386 GL_CALL(Disable(GR_GL_TEXTURE_GEN_S));
387 GL_CALL(Disable(GR_GL_TEXTURE_GEN_T));
388 GL_CALL(Disable(GR_GL_TEXTURE_GEN_Q));
389 GL_CALL(Disable(GR_GL_TEXTURE_GEN_R));
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000390 }
bsalomon@google.com05a718c2012-06-29 14:01:53 +0000391 }
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000392
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000393 // we assume these values
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000394 if (resetBits & kPixelStore_GrGLBackendState) {
395 if (this->glCaps().unpackRowLengthSupport()) {
396 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
397 }
398 if (this->glCaps().packRowLengthSupport()) {
399 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
400 }
401 if (this->glCaps().unpackFlipYSupport()) {
402 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
403 }
404 if (this->glCaps().packFlipYSupport()) {
405 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, GR_GL_FALSE));
406 }
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000407 }
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000408
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000409 if (resetBits & kProgram_GrGLBackendState) {
410 fHWProgramID = 0;
411 fSharedGLProgramState.invalidate();
412 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000413}
414
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000415namespace {
416
417GrSurfaceOrigin resolve_origin(GrSurfaceOrigin origin, bool renderTarget) {
418 // By default, GrRenderTargets are GL's normal orientation so that they
419 // can be drawn to by the outside world without the client having
420 // to render upside down.
421 if (kDefault_GrSurfaceOrigin == origin) {
422 return renderTarget ? kBottomLeft_GrSurfaceOrigin : kTopLeft_GrSurfaceOrigin;
423 } else {
424 return origin;
425 }
426}
427
428}
429
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000430GrTexture* GrGpuGL::onWrapBackendTexture(const GrBackendTextureDesc& desc) {
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000431 if (!this->configToGLFormats(desc.fConfig, false, NULL, NULL, NULL)) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000432 return NULL;
433 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000434
robertphillips@google.comb72e5d32012-10-30 15:18:10 +0000435 if (0 == desc.fTextureHandle) {
436 return NULL;
437 }
438
bsalomon@google.combcce8922013-03-25 15:38:39 +0000439 int maxSize = this->caps()->maxTextureSize();
robertphillips@google.comb72e5d32012-10-30 15:18:10 +0000440 if (desc.fWidth > maxSize || desc.fHeight > maxSize) {
441 return NULL;
442 }
443
444 GrGLTexture::Desc glTexDesc;
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000445 // next line relies on GrBackendTextureDesc's flags matching GrTexture's
robertphillips@google.com32716282012-06-04 12:48:45 +0000446 glTexDesc.fFlags = (GrTextureFlags) desc.fFlags;
bsalomon@google.com99621082011-11-15 16:47:16 +0000447 glTexDesc.fWidth = desc.fWidth;
448 glTexDesc.fHeight = desc.fHeight;
bsalomon@google.come269f212011-11-07 13:29:52 +0000449 glTexDesc.fConfig = desc.fConfig;
robertphillips@google.com32716282012-06-04 12:48:45 +0000450 glTexDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.come269f212011-11-07 13:29:52 +0000451 glTexDesc.fTextureID = static_cast<GrGLuint>(desc.fTextureHandle);
bsalomon@google.com72830222013-01-23 20:25:22 +0000452 glTexDesc.fIsWrapped = true;
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000453 bool renderTarget = SkToBool(desc.fFlags & kRenderTarget_GrBackendTextureFlag);
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000454 // FIXME: this should be calling resolve_origin(), but Chrome code is currently
455 // assuming the old behaviour, which is that backend textures are always
456 // BottomLeft, even for non-RT's. Once Chrome is fixed, change this to:
457 // glTexDesc.fOrigin = resolve_origin(desc.fOrigin, renderTarget);
458 if (kDefault_GrSurfaceOrigin == desc.fOrigin) {
459 glTexDesc.fOrigin = kBottomLeft_GrSurfaceOrigin;
460 } else {
461 glTexDesc.fOrigin = desc.fOrigin;
462 }
bsalomon@google.come269f212011-11-07 13:29:52 +0000463
464 GrGLTexture* texture = NULL;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000465 if (renderTarget) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000466 GrGLRenderTarget::Desc glRTDesc;
467 glRTDesc.fRTFBOID = 0;
468 glRTDesc.fTexFBOID = 0;
469 glRTDesc.fMSColorRenderbufferID = 0;
bsalomon@google.come269f212011-11-07 13:29:52 +0000470 glRTDesc.fConfig = desc.fConfig;
471 glRTDesc.fSampleCnt = desc.fSampleCnt;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000472 glRTDesc.fOrigin = glTexDesc.fOrigin;
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000473 glRTDesc.fCheckAllocation = false;
bsalomon@google.com99621082011-11-15 16:47:16 +0000474 if (!this->createRenderTargetObjects(glTexDesc.fWidth,
475 glTexDesc.fHeight,
bsalomon@google.come269f212011-11-07 13:29:52 +0000476 glTexDesc.fTextureID,
477 &glRTDesc)) {
478 return NULL;
479 }
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000480 texture = SkNEW_ARGS(GrGLTexture, (this, glTexDesc, glRTDesc));
bsalomon@google.come269f212011-11-07 13:29:52 +0000481 } else {
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000482 texture = SkNEW_ARGS(GrGLTexture, (this, glTexDesc));
bsalomon@google.come269f212011-11-07 13:29:52 +0000483 }
484 if (NULL == texture) {
485 return NULL;
486 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000487
bsalomon@google.come269f212011-11-07 13:29:52 +0000488 return texture;
489}
490
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000491GrRenderTarget* GrGpuGL::onWrapBackendRenderTarget(const GrBackendRenderTargetDesc& desc) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000492 GrGLRenderTarget::Desc glDesc;
493 glDesc.fConfig = desc.fConfig;
494 glDesc.fRTFBOID = static_cast<GrGLuint>(desc.fRenderTargetHandle);
495 glDesc.fMSColorRenderbufferID = 0;
496 glDesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
497 glDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.com72830222013-01-23 20:25:22 +0000498 glDesc.fIsWrapped = true;
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000499 glDesc.fCheckAllocation = false;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000500
501 glDesc.fOrigin = resolve_origin(desc.fOrigin, true);
bsalomon@google.come269f212011-11-07 13:29:52 +0000502 GrGLIRect viewport;
503 viewport.fLeft = 0;
504 viewport.fBottom = 0;
505 viewport.fWidth = desc.fWidth;
506 viewport.fHeight = desc.fHeight;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000507
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000508 GrRenderTarget* tgt = SkNEW_ARGS(GrGLRenderTarget,
509 (this, glDesc, viewport));
bsalomon@google.come269f212011-11-07 13:29:52 +0000510 if (desc.fStencilBits) {
511 GrGLStencilBuffer::Format format;
512 format.fInternalFormat = GrGLStencilBuffer::kUnknownInternalFormat;
513 format.fPacked = false;
514 format.fStencilBits = desc.fStencilBits;
515 format.fTotalBits = desc.fStencilBits;
bsalomon@google.com1f0f1a32013-01-23 21:32:32 +0000516 static const bool kIsSBWrapped = false;
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000517 GrGLStencilBuffer* sb = SkNEW_ARGS(GrGLStencilBuffer,
518 (this,
bsalomon@google.com1f0f1a32013-01-23 21:32:32 +0000519 kIsSBWrapped,
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000520 0,
521 desc.fWidth,
522 desc.fHeight,
523 desc.fSampleCnt,
524 format));
bsalomon@google.come269f212011-11-07 13:29:52 +0000525 tgt->setStencilBuffer(sb);
526 sb->unref();
527 }
528 return tgt;
529}
530
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000531////////////////////////////////////////////////////////////////////////////////
532
bsalomon@google.com9c680582013-02-06 18:17:50 +0000533bool GrGpuGL::onWriteTexturePixels(GrTexture* texture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000534 int left, int top, int width, int height,
535 GrPixelConfig config, const void* buffer,
536 size_t rowBytes) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000537 if (NULL == buffer) {
bsalomon@google.com9c680582013-02-06 18:17:50 +0000538 return false;
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000539 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000540 GrGLTexture* glTex = static_cast<GrGLTexture*>(texture);
541
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +0000542 this->setScratchTextureUnit();
bsalomon@google.com6f379512011-11-16 20:36:03 +0000543 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTex->textureID()));
544 GrGLTexture::Desc desc;
robertphillips@google.com32716282012-06-04 12:48:45 +0000545 desc.fFlags = glTex->desc().fFlags;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000546 desc.fWidth = glTex->width();
547 desc.fHeight = glTex->height();
robertphillips@google.com32716282012-06-04 12:48:45 +0000548 desc.fConfig = glTex->config();
549 desc.fSampleCnt = glTex->desc().fSampleCnt;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000550 desc.fTextureID = glTex->textureID();
bsalomon@google.com2d0bade2012-10-26 19:01:17 +0000551 desc.fOrigin = glTex->origin();
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000552
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +0000553 if (this->uploadTexData(desc, false,
554 left, top, width, height,
555 config, buffer, rowBytes)) {
556 texture->dirtyMipMaps(true);
557 return true;
558 } else {
559 return false;
560 }
bsalomon@google.com6f379512011-11-16 20:36:03 +0000561}
562
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000563namespace {
564bool adjust_pixel_ops_params(int surfaceWidth,
565 int surfaceHeight,
566 size_t bpp,
567 int* left, int* top, int* width, int* height,
568 const void** data,
569 size_t* rowBytes) {
570 if (!*rowBytes) {
571 *rowBytes = *width * bpp;
572 }
573
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000574 SkIRect subRect = SkIRect::MakeXYWH(*left, *top, *width, *height);
575 SkIRect bounds = SkIRect::MakeWH(surfaceWidth, surfaceHeight);
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000576
577 if (!subRect.intersect(bounds)) {
578 return false;
579 }
580 *data = reinterpret_cast<const void*>(reinterpret_cast<intptr_t>(*data) +
581 (subRect.fTop - *top) * *rowBytes + (subRect.fLeft - *left) * bpp);
582
583 *left = subRect.fLeft;
584 *top = subRect.fTop;
585 *width = subRect.width();
586 *height = subRect.height();
587 return true;
588}
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000589
590GrGLenum check_alloc_error(const GrTextureDesc& desc, const GrGLInterface* interface) {
591 if (SkToBool(desc.fFlags & kCheckAllocation_GrTextureFlagBit)) {
592 return GR_GL_GET_ERROR(interface);
593 } else {
594 return CHECK_ALLOC_ERROR(interface);
595 }
596}
597
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000598}
599
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000600bool GrGpuGL::uploadTexData(const GrGLTexture::Desc& desc,
601 bool isNewTexture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000602 int left, int top, int width, int height,
603 GrPixelConfig dataConfig,
604 const void* data,
605 size_t rowBytes) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000606 SkASSERT(NULL != data || isNewTexture);
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000607
608 size_t bpp = GrBytesPerPixel(dataConfig);
609 if (!adjust_pixel_ops_params(desc.fWidth, desc.fHeight, bpp, &left, &top,
610 &width, &height, &data, &rowBytes)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000611 return false;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000612 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000613 size_t trimRowBytes = width * bpp;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000614
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000615 // in case we need a temporary, trimmed copy of the src pixels
616 SkAutoSMalloc<128 * 128> tempStorage;
617
bsalomon@google.com313f0192012-07-10 17:21:02 +0000618 // paletted textures cannot be partially updated
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000619 bool useTexStorage = isNewTexture &&
bsalomon@google.com313f0192012-07-10 17:21:02 +0000620 desc.fConfig != kIndex_8_GrPixelConfig &&
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000621 this->glCaps().texStorageSupport();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000622
bsalomon@google.com313f0192012-07-10 17:21:02 +0000623 if (useTexStorage && kDesktop_GrGLBinding == this->glBinding()) {
624 // 565 is not a sized internal format on desktop GL. So on desktop with
625 // 565 we always use an unsized internal format to let the system pick
626 // the best sized format to convert the 565 data to. Since TexStorage
627 // only allows sized internal formats we will instead use TexImage2D.
628 useTexStorage = desc.fConfig != kRGB_565_GrPixelConfig;
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000629 }
630
631 GrGLenum internalFormat;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000632 GrGLenum externalFormat;
633 GrGLenum externalType;
commit-bot@chromium.org87002952013-08-20 15:12:01 +0000634 // glTexStorage requires sized internal formats on both desktop and ES. ES2 requires an unsized
635 // format for glTexImage, unlike ES3 and desktop. However, we allow the driver to decide the
636 // size of the internal format whenever possible and so only use a sized internal format when
637 // using texture storage.
bsalomon@google.com3323c4d2013-08-20 13:48:33 +0000638 if (!this->configToGLFormats(dataConfig, useTexStorage, &internalFormat,
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000639 &externalFormat, &externalType)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000640 return false;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000641 }
642
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000643 if (!isNewTexture && GR_GL_PALETTE8_RGBA8 == internalFormat) {
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000644 // paletted textures cannot be updated
645 return false;
646 }
647
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000648 /*
bsalomon@google.com6f379512011-11-16 20:36:03 +0000649 * check whether to allocate a temporary buffer for flipping y or
650 * because our srcData has extra bytes past each row. If so, we need
651 * to trim those off here, since GL ES may not let us specify
652 * GL_UNPACK_ROW_LENGTH.
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000653 */
bsalomon@google.com6f379512011-11-16 20:36:03 +0000654 bool restoreGLRowLength = false;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000655 bool swFlipY = false;
656 bool glFlipY = false;
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000657 if (NULL != data) {
senorblanco@chromium.orgef5dbe12013-01-28 16:42:38 +0000658 if (kBottomLeft_GrSurfaceOrigin == desc.fOrigin) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000659 if (this->glCaps().unpackFlipYSupport()) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000660 glFlipY = true;
661 } else {
662 swFlipY = true;
663 }
664 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000665 if (this->glCaps().unpackRowLengthSupport() && !swFlipY) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000666 // can't use this for flipping, only non-neg values allowed. :(
667 if (rowBytes != trimRowBytes) {
668 GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
669 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength));
670 restoreGLRowLength = true;
671 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000672 } else {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000673 if (trimRowBytes != rowBytes || swFlipY) {
674 // copy data into our new storage, skipping the trailing bytes
675 size_t trimSize = height * trimRowBytes;
676 const char* src = (const char*)data;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000677 if (swFlipY) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000678 src += (height - 1) * rowBytes;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000679 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000680 char* dst = (char*)tempStorage.reset(trimSize);
681 for (int y = 0; y < height; y++) {
682 memcpy(dst, src, trimRowBytes);
683 if (swFlipY) {
684 src -= rowBytes;
685 } else {
686 src += rowBytes;
687 }
688 dst += trimRowBytes;
689 }
690 // now point data to our copied version
691 data = tempStorage.get();
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000692 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000693 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000694 if (glFlipY) {
695 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_TRUE));
696 }
697 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, static_cast<GrGLint>(bpp)));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000698 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000699 bool succeeded = true;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000700 if (isNewTexture &&
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000701 0 == left && 0 == top &&
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000702 desc.fWidth == width && desc.fHeight == height) {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000703 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000704 if (useTexStorage) {
705 // We never resize or change formats of textures. We don't use
706 // mipmaps currently.
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000707 GL_ALLOC_CALL(this->glInterface(),
708 TexStorage2D(GR_GL_TEXTURE_2D,
709 1, // levels
710 internalFormat,
711 desc.fWidth, desc.fHeight));
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000712 } else {
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000713 if (GR_GL_PALETTE8_RGBA8 == internalFormat) {
714 GrGLsizei imageSize = desc.fWidth * desc.fHeight +
715 kGrColorTableSize;
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000716 GL_ALLOC_CALL(this->glInterface(),
717 CompressedTexImage2D(GR_GL_TEXTURE_2D,
718 0, // level
719 internalFormat,
720 desc.fWidth, desc.fHeight,
721 0, // border
722 imageSize,
723 data));
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000724 } else {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000725 GL_ALLOC_CALL(this->glInterface(),
726 TexImage2D(GR_GL_TEXTURE_2D,
727 0, // level
728 internalFormat,
729 desc.fWidth, desc.fHeight,
730 0, // border
731 externalFormat, externalType,
732 data));
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000733 }
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000734 }
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000735 GrGLenum error = check_alloc_error(desc, this->glInterface());
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000736 if (error != GR_GL_NO_ERROR) {
737 succeeded = false;
738 } else {
739 // if we have data and we used TexStorage to create the texture, we
740 // now upload with TexSubImage.
741 if (NULL != data && useTexStorage) {
742 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D,
743 0, // level
744 left, top,
745 width, height,
746 externalFormat, externalType,
747 data));
748 }
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000749 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000750 } else {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000751 if (swFlipY || glFlipY) {
bsalomon@google.com6f379512011-11-16 20:36:03 +0000752 top = desc.fHeight - (top + height);
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000753 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000754 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D,
755 0, // level
756 left, top,
757 width, height,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000758 externalFormat, externalType, data));
759 }
760
761 if (restoreGLRowLength) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000762 SkASSERT(this->glCaps().unpackRowLengthSupport());
bsalomon@google.com6f379512011-11-16 20:36:03 +0000763 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000764 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000765 if (glFlipY) {
766 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
767 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000768 return succeeded;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000769}
770
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000771namespace {
robertphillips@google.com6177e692013-02-28 20:16:25 +0000772bool renderbuffer_storage_msaa(GrGLContext& ctx,
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000773 int sampleCount,
774 GrGLenum format,
775 int width, int height) {
robertphillips@google.com6177e692013-02-28 20:16:25 +0000776 CLEAR_ERROR_BEFORE_ALLOC(ctx.interface());
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000777 SkASSERT(GrGLCaps::kNone_MSFBOType != ctx.info().caps()->msFBOType());
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000778 bool created = false;
779 if (GrGLCaps::kNVDesktop_CoverageAAType ==
bsalomon@google.combcce8922013-03-25 15:38:39 +0000780 ctx.info().caps()->coverageAAType()) {
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000781 const GrGLCaps::MSAACoverageMode& mode =
bsalomon@google.combcce8922013-03-25 15:38:39 +0000782 ctx.info().caps()->getMSAACoverageMode(sampleCount);
robertphillips@google.com6177e692013-02-28 20:16:25 +0000783 GL_ALLOC_CALL(ctx.interface(),
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000784 RenderbufferStorageMultisampleCoverage(GR_GL_RENDERBUFFER,
785 mode.fCoverageSampleCnt,
786 mode.fColorSampleCnt,
787 format,
788 width, height));
robertphillips@google.com6177e692013-02-28 20:16:25 +0000789 created = (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctx.interface()));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000790 }
791 if (!created) {
robertphillips@google.com6177e692013-02-28 20:16:25 +0000792 GL_ALLOC_CALL(ctx.interface(),
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000793 RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
794 sampleCount,
795 format,
796 width, height));
robertphillips@google.com6177e692013-02-28 20:16:25 +0000797 created = (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctx.interface()));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000798 }
799 return created;
800}
801}
802
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000803bool GrGpuGL::createRenderTargetObjects(int width, int height,
804 GrGLuint texID,
805 GrGLRenderTarget::Desc* desc) {
806 desc->fMSColorRenderbufferID = 0;
807 desc->fRTFBOID = 0;
808 desc->fTexFBOID = 0;
bsalomon@google.com72830222013-01-23 20:25:22 +0000809 desc->fIsWrapped = false;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000810
811 GrGLenum status;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000812
bsalomon@google.comab15d612011-08-09 12:57:56 +0000813 GrGLenum msColorFormat = 0; // suppress warning
814
bsalomon@google.com347c3822013-05-01 20:10:01 +0000815 if (desc->fSampleCnt > 0 && GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType()) {
816 goto FAILED;
817 }
818
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000819 GL_CALL(GenFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000820 if (!desc->fTexFBOID) {
821 goto FAILED;
822 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000823
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000824
bsalomon@google.comf3a60c02013-03-19 19:06:09 +0000825 // If we are using multisampling we will create two FBOS. We render to one and then resolve to
826 // the texture bound to the other. The exception is the IMG multisample extension. With this
827 // extension the texture is multisampled when rendered to and then auto-resolves it when it is
828 // rendered from.
bsalomon@google.com347c3822013-05-01 20:10:01 +0000829 if (desc->fSampleCnt > 0 && this->glCaps().usesMSAARenderBuffers()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000830 GL_CALL(GenFramebuffers(1, &desc->fRTFBOID));
831 GL_CALL(GenRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000832 if (!desc->fRTFBOID ||
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000833 !desc->fMSColorRenderbufferID ||
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000834 !this->configToGLFormats(desc->fConfig,
commit-bot@chromium.org87002952013-08-20 15:12:01 +0000835 // ES2 and ES3 require sized internal formats for rb storage.
bsalomon@google.com791816a2013-08-15 18:54:39 +0000836 kES_GrGLBinding == this->glBinding(),
bsalomon@google.com347c3822013-05-01 20:10:01 +0000837 &msColorFormat,
838 NULL,
839 NULL)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000840 goto FAILED;
841 }
842 } else {
843 desc->fRTFBOID = desc->fTexFBOID;
844 }
845
bsalomon@google.com0e9b41a2012-01-04 22:11:43 +0000846 // below here we may bind the FBO
bsalomon@google.comc811ea32012-05-21 15:33:09 +0000847 fHWBoundRenderTarget = NULL;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000848 if (desc->fRTFBOID != desc->fTexFBOID) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000849 SkASSERT(desc->fSampleCnt > 0);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000850 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000851 desc->fMSColorRenderbufferID));
robertphillips@google.com6177e692013-02-28 20:16:25 +0000852 if (!renderbuffer_storage_msaa(fGLContext,
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000853 desc->fSampleCnt,
854 msColorFormat,
855 width, height)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000856 goto FAILED;
857 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000858 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fRTFBOID));
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000859 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000860 GR_GL_COLOR_ATTACHMENT0,
861 GR_GL_RENDERBUFFER,
862 desc->fMSColorRenderbufferID));
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000863 if (desc->fCheckAllocation ||
864 !this->glCaps().isConfigVerifiedColorAttachment(desc->fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000865 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
866 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
867 goto FAILED;
868 }
bsalomon@google.combcce8922013-03-25 15:38:39 +0000869 fGLContext.info().caps()->markConfigAsValidColorAttachment(desc->fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000870 }
871 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000872 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000873
bsalomon@google.com347c3822013-05-01 20:10:01 +0000874 if (this->glCaps().usesImplicitMSAAResolve() && desc->fSampleCnt > 0) {
bsalomon@google.comf3a60c02013-03-19 19:06:09 +0000875 GL_CALL(FramebufferTexture2DMultisample(GR_GL_FRAMEBUFFER,
876 GR_GL_COLOR_ATTACHMENT0,
877 GR_GL_TEXTURE_2D,
878 texID, 0, desc->fSampleCnt));
879 } else {
880 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
881 GR_GL_COLOR_ATTACHMENT0,
882 GR_GL_TEXTURE_2D,
883 texID, 0));
884 }
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000885 if (desc->fCheckAllocation ||
886 !this->glCaps().isConfigVerifiedColorAttachment(desc->fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000887 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
888 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
889 goto FAILED;
890 }
bsalomon@google.combcce8922013-03-25 15:38:39 +0000891 fGLContext.info().caps()->markConfigAsValidColorAttachment(desc->fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000892 }
893
894 return true;
895
896FAILED:
897 if (desc->fMSColorRenderbufferID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000898 GL_CALL(DeleteRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000899 }
900 if (desc->fRTFBOID != desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000901 GL_CALL(DeleteFramebuffers(1, &desc->fRTFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000902 }
903 if (desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000904 GL_CALL(DeleteFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000905 }
906 return false;
907}
908
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000909// good to set a break-point here to know when createTexture fails
910static GrTexture* return_null_texture() {
mtklein@google.com330313a2013-08-22 15:37:26 +0000911// SkDEBUGFAIL("null texture");
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000912 return NULL;
913}
914
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000915#if 0 && defined(SK_DEBUG)
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000916static size_t as_size_t(int x) {
917 return x;
918}
919#endif
920
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000921GrTexture* GrGpuGL::onCreateTexture(const GrTextureDesc& desc,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000922 const void* srcData,
923 size_t rowBytes) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000924
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000925 GrGLTexture::Desc glTexDesc;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000926 GrGLRenderTarget::Desc glRTDesc;
reed@google.comac10a2d2010-12-22 21:39:39 +0000927
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000928 // Attempt to catch un- or wrongly initialized sample counts;
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000929 SkASSERT(desc.fSampleCnt >= 0 && desc.fSampleCnt <= 64);
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000930 // We fail if the MSAA was requested and is not available.
931 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType() && desc.fSampleCnt) {
932 //GrPrintf("MSAA RT requested but not supported on this platform.");
933 return return_null_texture();
934 }
935 // If the sample count exceeds the max then we clamp it.
bsalomon@google.combcce8922013-03-25 15:38:39 +0000936 glTexDesc.fSampleCnt = GrMin(desc.fSampleCnt, this->caps()->maxSampleCount());
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000937
robertphillips@google.com32716282012-06-04 12:48:45 +0000938 glTexDesc.fFlags = desc.fFlags;
bsalomon@google.com99621082011-11-15 16:47:16 +0000939 glTexDesc.fWidth = desc.fWidth;
940 glTexDesc.fHeight = desc.fHeight;
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000941 glTexDesc.fConfig = desc.fConfig;
bsalomon@google.com72830222013-01-23 20:25:22 +0000942 glTexDesc.fIsWrapped = false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000943
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000944 glRTDesc.fMSColorRenderbufferID = 0;
945 glRTDesc.fRTFBOID = 0;
946 glRTDesc.fTexFBOID = 0;
bsalomon@google.com72830222013-01-23 20:25:22 +0000947 glRTDesc.fIsWrapped = false;
bsalomon@google.com64c4fe42011-11-05 14:51:01 +0000948 glRTDesc.fConfig = glTexDesc.fConfig;
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000949 glRTDesc.fCheckAllocation = SkToBool(desc.fFlags & kCheckAllocation_GrTextureFlagBit);
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000950
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000951 bool renderTarget = SkToBool(desc.fFlags & kRenderTarget_GrTextureFlagBit);
reed@google.comac10a2d2010-12-22 21:39:39 +0000952
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000953 glTexDesc.fOrigin = resolve_origin(desc.fOrigin, renderTarget);
954 glRTDesc.fOrigin = glTexDesc.fOrigin;
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000955
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000956 glRTDesc.fSampleCnt = glTexDesc.fSampleCnt;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000957 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType() &&
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000958 desc.fSampleCnt) {
bsalomon@google.com945bbe12012-06-15 14:30:34 +0000959 //GrPrintf("MSAA RT requested but not supported on this platform.");
960 return return_null_texture();
reed@google.comac10a2d2010-12-22 21:39:39 +0000961 }
962
reed@google.comac10a2d2010-12-22 21:39:39 +0000963 if (renderTarget) {
bsalomon@google.combcce8922013-03-25 15:38:39 +0000964 int maxRTSize = this->caps()->maxRenderTargetSize();
965 if (glTexDesc.fWidth > maxRTSize || glTexDesc.fHeight > maxRTSize) {
bsalomon@google.com91958362011-06-13 17:58:13 +0000966 return return_null_texture();
967 }
sugoi@google.com6ba0b0f2013-05-03 13:52:32 +0000968 } else {
969 int maxSize = this->caps()->maxTextureSize();
970 if (glTexDesc.fWidth > maxSize || glTexDesc.fHeight > maxSize) {
971 return return_null_texture();
972 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000973 }
974
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000975 GL_CALL(GenTextures(1, &glTexDesc.fTextureID));
junov@chromium.org8b6b1c92013-08-29 16:22:09 +0000976
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000977 if (!glTexDesc.fTextureID) {
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000978 return return_null_texture();
979 }
980
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +0000981 this->setScratchTextureUnit();
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000982 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTexDesc.fTextureID));
bsalomon@google.come269f212011-11-07 13:29:52 +0000983
junov@chromium.org8b6b1c92013-08-29 16:22:09 +0000984 if (renderTarget && this->glCaps().textureUsageSupport()) {
985 // provides a hint about how this texture will be used
986 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
987 GR_GL_TEXTURE_USAGE,
988 GR_GL_FRAMEBUFFER_ATTACHMENT));
989 }
990
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000991 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
992 // drivers have a bug where an FBO won't be complete if it includes a
993 // texture that is not mipmap complete (considering the filter in use).
994 GrGLTexture::TexParams initialTexParams;
995 // we only set a subset here so invalidate first
996 initialTexParams.invalidate();
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +0000997 initialTexParams.fMinFilter = GR_GL_NEAREST;
998 initialTexParams.fMagFilter = GR_GL_NEAREST;
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000999 initialTexParams.fWrapS = GR_GL_CLAMP_TO_EDGE;
1000 initialTexParams.fWrapT = GR_GL_CLAMP_TO_EDGE;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001001 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1002 GR_GL_TEXTURE_MAG_FILTER,
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00001003 initialTexParams.fMagFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001004 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1005 GR_GL_TEXTURE_MIN_FILTER,
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00001006 initialTexParams.fMinFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001007 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1008 GR_GL_TEXTURE_WRAP_S,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001009 initialTexParams.fWrapS));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001010 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1011 GR_GL_TEXTURE_WRAP_T,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001012 initialTexParams.fWrapT));
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001013 if (!this->uploadTexData(glTexDesc, true, 0, 0,
1014 glTexDesc.fWidth, glTexDesc.fHeight,
1015 desc.fConfig, srcData, rowBytes)) {
1016 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
1017 return return_null_texture();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001018 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001019
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001020 GrGLTexture* tex;
reed@google.comac10a2d2010-12-22 21:39:39 +00001021 if (renderTarget) {
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001022 // unbind the texture from the texture unit before binding it to the frame buffer
1023 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, 0));
1024
bsalomon@google.com99621082011-11-15 16:47:16 +00001025 if (!this->createRenderTargetObjects(glTexDesc.fWidth,
1026 glTexDesc.fHeight,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001027 glTexDesc.fTextureID,
1028 &glRTDesc)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001029 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001030 return return_null_texture();
1031 }
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001032 tex = SkNEW_ARGS(GrGLTexture, (this, glTexDesc, glRTDesc));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001033 } else {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001034 tex = SkNEW_ARGS(GrGLTexture, (this, glTexDesc));
reed@google.comac10a2d2010-12-22 21:39:39 +00001035 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001036 tex->setCachedTexParams(initialTexParams, this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00001037#ifdef TRACE_TEXTURE_CREATION
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001038 GrPrintf("--- new texture [%d] size=(%d %d) config=%d\n",
1039 glTexDesc.fTextureID, desc.fWidth, desc.fHeight, desc.fConfig);
reed@google.comac10a2d2010-12-22 21:39:39 +00001040#endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001041 return tex;
1042}
1043
1044namespace {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001045
1046const GrGLuint kUnknownBitCount = GrGLStencilBuffer::kUnknownBitCount;
1047
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001048void inline get_stencil_rb_sizes(const GrGLInterface* gl,
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001049 GrGLStencilBuffer::Format* format) {
sugoi@google.com6ba0b0f2013-05-03 13:52:32 +00001050
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001051 // we shouldn't ever know one size and not the other
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001052 SkASSERT((kUnknownBitCount == format->fStencilBits) ==
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001053 (kUnknownBitCount == format->fTotalBits));
1054 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001055 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001056 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1057 (GrGLint*)&format->fStencilBits);
1058 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001059 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001060 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1061 (GrGLint*)&format->fTotalBits);
1062 format->fTotalBits += format->fStencilBits;
1063 } else {
1064 format->fTotalBits = format->fStencilBits;
1065 }
1066 }
1067}
1068}
1069
1070bool GrGpuGL::createStencilBufferForRenderTarget(GrRenderTarget* rt,
1071 int width, int height) {
1072
1073 // All internally created RTs are also textures. We don't create
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +00001074 // SBs for a client's standalone RT (that is a RT that isn't also a texture).
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001075 SkASSERT(rt->asTexture());
1076 SkASSERT(width >= rt->width());
1077 SkASSERT(height >= rt->height());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001078
1079 int samples = rt->numSamples();
1080 GrGLuint sbID;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001081 GL_CALL(GenRenderbuffers(1, &sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001082 if (!sbID) {
1083 return false;
1084 }
1085
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001086 int stencilFmtCnt = this->glCaps().stencilFormats().count();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001087 for (int i = 0; i < stencilFmtCnt; ++i) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001088 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001089 // we start with the last stencil format that succeeded in hopes
1090 // that we won't go through this loop more than once after the
1091 // first (painful) stencil creation.
1092 int sIdx = (i + fLastSuccessfulStencilFmtIdx) % stencilFmtCnt;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001093 const GrGLCaps::StencilFormat& sFmt =
1094 this->glCaps().stencilFormats()[sIdx];
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001095 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001096 // we do this "if" so that we don't call the multisample
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001097 // version on a GL that doesn't have an MSAA extension.
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001098 bool created;
1099 if (samples > 0) {
robertphillips@google.com6177e692013-02-28 20:16:25 +00001100 created = renderbuffer_storage_msaa(fGLContext,
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001101 samples,
1102 sFmt.fInternalFormat,
1103 width, height);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001104 } else {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001105 GL_ALLOC_CALL(this->glInterface(),
1106 RenderbufferStorage(GR_GL_RENDERBUFFER,
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001107 sFmt.fInternalFormat,
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001108 width, height));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001109 created =
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +00001110 (GR_GL_NO_ERROR == check_alloc_error(rt->desc(), this->glInterface()));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001111 }
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001112 if (created) {
1113 // After sized formats we attempt an unsized format and take
1114 // whatever sizes GL gives us. In that case we query for the size.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001115 GrGLStencilBuffer::Format format = sFmt;
sugoi@google.com6ba0b0f2013-05-03 13:52:32 +00001116 get_stencil_rb_sizes(this->glInterface(), &format);
bsalomon@google.com72830222013-01-23 20:25:22 +00001117 static const bool kIsWrapped = false;
robertphillips@google.comf2e93fc2012-09-05 19:44:18 +00001118 SkAutoTUnref<GrStencilBuffer> sb(SkNEW_ARGS(GrGLStencilBuffer,
bsalomon@google.com72830222013-01-23 20:25:22 +00001119 (this, kIsWrapped, sbID, width, height,
robertphillips@google.comf2e93fc2012-09-05 19:44:18 +00001120 samples, format)));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001121 if (this->attachStencilBufferToRenderTarget(sb, rt)) {
1122 fLastSuccessfulStencilFmtIdx = sIdx;
robertphillips@google.com9fbcad02012-09-09 14:44:15 +00001123 sb->transferToCache();
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001124 rt->setStencilBuffer(sb);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001125 return true;
1126 }
1127 sb->abandon(); // otherwise we lose sbID
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001128 }
1129 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001130 GL_CALL(DeleteRenderbuffers(1, &sbID));
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001131 return false;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001132}
1133
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001134bool GrGpuGL::attachStencilBufferToRenderTarget(GrStencilBuffer* sb, GrRenderTarget* rt) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001135 GrGLRenderTarget* glrt = (GrGLRenderTarget*) rt;
1136
1137 GrGLuint fbo = glrt->renderFBOID();
1138
1139 if (NULL == sb) {
1140 if (NULL != rt->getStencilBuffer()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001141 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001142 GR_GL_STENCIL_ATTACHMENT,
1143 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001144 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001145 GR_GL_DEPTH_ATTACHMENT,
1146 GR_GL_RENDERBUFFER, 0));
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +00001147#ifdef SK_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001148 GrGLenum status;
1149 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001150 SkASSERT(GR_GL_FRAMEBUFFER_COMPLETE == status);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001151#endif
1152 }
1153 return true;
1154 } else {
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001155 GrGLStencilBuffer* glsb = static_cast<GrGLStencilBuffer*>(sb);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001156 GrGLuint rb = glsb->renderbufferID();
1157
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001158 fHWBoundRenderTarget = NULL;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001159 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, fbo));
1160 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001161 GR_GL_STENCIL_ATTACHMENT,
1162 GR_GL_RENDERBUFFER, rb));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001163 if (glsb->format().fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001164 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001165 GR_GL_DEPTH_ATTACHMENT,
1166 GR_GL_RENDERBUFFER, rb));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001167 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001168 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001169 GR_GL_DEPTH_ATTACHMENT,
1170 GR_GL_RENDERBUFFER, 0));
reed@google.comac10a2d2010-12-22 21:39:39 +00001171 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001172
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001173 GrGLenum status;
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001174 if (!this->glCaps().isColorConfigAndStencilFormatVerified(rt->config(), glsb->format())) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001175 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1176 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001177 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001178 GR_GL_STENCIL_ATTACHMENT,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001179 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001180 if (glsb->format().fPacked) {
1181 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1182 GR_GL_DEPTH_ATTACHMENT,
1183 GR_GL_RENDERBUFFER, 0));
1184 }
1185 return false;
1186 } else {
bsalomon@google.combcce8922013-03-25 15:38:39 +00001187 fGLContext.info().caps()->markColorConfigAndStencilFormatAsVerified(
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001188 rt->config(),
1189 glsb->format());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001190 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001191 }
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001192 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001193 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001194}
1195
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001196////////////////////////////////////////////////////////////////////////////////
1197
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001198GrVertexBuffer* GrGpuGL::onCreateVertexBuffer(uint32_t size, bool dynamic) {
bsalomon@google.come49ad452013-02-20 19:33:20 +00001199 GrGLVertexBuffer::Desc desc;
1200 desc.fDynamic = dynamic;
1201 desc.fSizeInBytes = size;
1202 desc.fIsWrapped = false;
1203
bsalomon@google.com96966a52013-02-21 16:34:21 +00001204 if (this->glCaps().useNonVBOVertexAndIndexDynamicData() && desc.fDynamic) {
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001205 desc.fID = 0;
bsalomon@google.come49ad452013-02-20 19:33:20 +00001206 GrGLVertexBuffer* vertexBuffer = SkNEW_ARGS(GrGLVertexBuffer, (this, desc));
reed@google.comac10a2d2010-12-22 21:39:39 +00001207 return vertexBuffer;
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001208 } else {
1209 GL_CALL(GenBuffers(1, &desc.fID));
1210 if (desc.fID) {
bsalomon@google.com6918d482013-03-07 19:09:11 +00001211 fHWGeometryState.setVertexBufferID(this, desc.fID);
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001212 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1213 // make sure driver can allocate memory for this buffer
1214 GL_ALLOC_CALL(this->glInterface(),
1215 BufferData(GR_GL_ARRAY_BUFFER,
1216 desc.fSizeInBytes,
1217 NULL, // data ptr
1218 desc.fDynamic ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW));
1219 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
1220 GL_CALL(DeleteBuffers(1, &desc.fID));
bsalomon@google.com6918d482013-03-07 19:09:11 +00001221 this->notifyVertexBufferDelete(desc.fID);
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001222 return NULL;
1223 }
1224 GrGLVertexBuffer* vertexBuffer = SkNEW_ARGS(GrGLVertexBuffer, (this, desc));
1225 return vertexBuffer;
1226 }
1227 return NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +00001228 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001229}
1230
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001231GrIndexBuffer* GrGpuGL::onCreateIndexBuffer(uint32_t size, bool dynamic) {
bsalomon@google.come49ad452013-02-20 19:33:20 +00001232 GrGLIndexBuffer::Desc desc;
1233 desc.fDynamic = dynamic;
1234 desc.fSizeInBytes = size;
1235 desc.fIsWrapped = false;
1236
bsalomon@google.com96966a52013-02-21 16:34:21 +00001237 if (this->glCaps().useNonVBOVertexAndIndexDynamicData() && desc.fDynamic) {
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001238 desc.fID = 0;
bsalomon@google.come49ad452013-02-20 19:33:20 +00001239 GrIndexBuffer* indexBuffer = SkNEW_ARGS(GrGLIndexBuffer, (this, desc));
reed@google.comac10a2d2010-12-22 21:39:39 +00001240 return indexBuffer;
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001241 } else {
1242 GL_CALL(GenBuffers(1, &desc.fID));
1243 if (desc.fID) {
bsalomon@google.com6918d482013-03-07 19:09:11 +00001244 fHWGeometryState.setIndexBufferIDOnDefaultVertexArray(this, desc.fID);
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001245 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1246 // make sure driver can allocate memory for this buffer
1247 GL_ALLOC_CALL(this->glInterface(),
1248 BufferData(GR_GL_ELEMENT_ARRAY_BUFFER,
1249 desc.fSizeInBytes,
1250 NULL, // data ptr
1251 desc.fDynamic ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW));
1252 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
1253 GL_CALL(DeleteBuffers(1, &desc.fID));
bsalomon@google.com6918d482013-03-07 19:09:11 +00001254 this->notifyIndexBufferDelete(desc.fID);
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001255 return NULL;
1256 }
1257 GrIndexBuffer* indexBuffer = SkNEW_ARGS(GrGLIndexBuffer, (this, desc));
1258 return indexBuffer;
1259 }
1260 return NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +00001261 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001262}
1263
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00001264GrPath* GrGpuGL::onCreatePath(const SkPath& inPath) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001265 SkASSERT(this->caps()->pathStencilingSupport());
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001266 return SkNEW_ARGS(GrGLPath, (this, inPath));
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00001267}
1268
bsalomon@google.coma3201942012-06-21 19:58:20 +00001269void GrGpuGL::flushScissor() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001270 const GrDrawState& drawState = this->getDrawState();
1271 const GrGLRenderTarget* rt =
1272 static_cast<const GrGLRenderTarget*>(drawState.getRenderTarget());
1273
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001274 SkASSERT(NULL != rt);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001275 const GrGLIRect& vp = rt->getViewport();
reed@google.comac10a2d2010-12-22 21:39:39 +00001276
bsalomon@google.coma3201942012-06-21 19:58:20 +00001277 if (fScissorState.fEnabled) {
1278 GrGLIRect scissor;
1279 scissor.setRelativeTo(vp,
1280 fScissorState.fRect.fLeft,
1281 fScissorState.fRect.fTop,
1282 fScissorState.fRect.width(),
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001283 fScissorState.fRect.height(),
1284 rt->origin());
bsalomon@google.coma3201942012-06-21 19:58:20 +00001285 // if the scissor fully contains the viewport then we fall through and
1286 // disable the scissor test.
1287 if (!scissor.contains(vp)) {
1288 if (fHWScissorSettings.fRect != scissor) {
1289 scissor.pushToGLScissor(this->glInterface());
1290 fHWScissorSettings.fRect = scissor;
1291 }
1292 if (kYes_TriState != fHWScissorSettings.fEnabled) {
1293 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1294 fHWScissorSettings.fEnabled = kYes_TriState;
1295 }
1296 return;
1297 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001298 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001299 if (kNo_TriState != fHWScissorSettings.fEnabled) {
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001300 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.coma3201942012-06-21 19:58:20 +00001301 fHWScissorSettings.fEnabled = kNo_TriState;
1302 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001303 }
1304}
1305
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001306void GrGpuGL::onClear(const SkIRect* rect, GrColor color) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001307 const GrDrawState& drawState = this->getDrawState();
1308 const GrRenderTarget* rt = drawState.getRenderTarget();
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001309 // parent class should never let us get here with no RT
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001310 SkASSERT(NULL != rt);
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001311
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001312 SkIRect clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001313 if (NULL != rect) {
1314 // flushScissor expects rect to be clipped to the target.
bsalomon@google.com74b98712011-11-11 19:46:16 +00001315 clippedRect = *rect;
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001316 SkIRect rtRect = SkIRect::MakeWH(rt->width(), rt->height());
bsalomon@google.com74b98712011-11-11 19:46:16 +00001317 if (clippedRect.intersect(rtRect)) {
1318 rect = &clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001319 } else {
1320 return;
1321 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001322 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001323 this->flushRenderTarget(rect);
bsalomon@google.coma3201942012-06-21 19:58:20 +00001324 GrAutoTRestore<ScissorState> asr(&fScissorState);
1325 fScissorState.fEnabled = (NULL != rect);
1326 if (fScissorState.fEnabled) {
1327 fScissorState.fRect = *rect;
1328 }
1329 this->flushScissor();
bsalomon@google.com74b98712011-11-11 19:46:16 +00001330
1331 GrGLfloat r, g, b, a;
1332 static const GrGLfloat scale255 = 1.f / 255.f;
1333 a = GrColorUnpackA(color) * scale255;
1334 GrGLfloat scaleRGB = scale255;
bsalomon@google.com74b98712011-11-11 19:46:16 +00001335 r = GrColorUnpackR(color) * scaleRGB;
1336 g = GrColorUnpackG(color) * scaleRGB;
1337 b = GrColorUnpackB(color) * scaleRGB;
1338
1339 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00001340 fHWWriteToColor = kYes_TriState;
bsalomon@google.com74b98712011-11-11 19:46:16 +00001341 GL_CALL(ClearColor(r, g, b, a));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001342 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001343}
1344
bsalomon@google.comedc177d2011-08-05 15:46:40 +00001345void GrGpuGL::clearStencil() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001346 if (NULL == this->getDrawState().getRenderTarget()) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001347 return;
1348 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001349
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001350 this->flushRenderTarget(&SkIRect::EmptyIRect());
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001351
bsalomon@google.coma3201942012-06-21 19:58:20 +00001352 GrAutoTRestore<ScissorState> asr(&fScissorState);
1353 fScissorState.fEnabled = false;
1354 this->flushScissor();
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001355
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001356 GL_CALL(StencilMask(0xffffffff));
1357 GL_CALL(ClearStencil(0));
1358 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001359 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001360}
1361
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001362void GrGpuGL::clearStencilClip(const SkIRect& rect, bool insideClip) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001363 const GrDrawState& drawState = this->getDrawState();
1364 const GrRenderTarget* rt = drawState.getRenderTarget();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001365 SkASSERT(NULL != rt);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001366
1367 // this should only be called internally when we know we have a
1368 // stencil buffer.
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001369 SkASSERT(NULL != rt->getStencilBuffer());
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001370 GrGLint stencilBitCount = rt->getStencilBuffer()->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001371#if 0
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001372 SkASSERT(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00001373 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001374#else
1375 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001376 // ANGLE a partial stencil mask will cause clears to be
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001377 // turned into draws. Our contract on GrDrawTarget says that
1378 // changing the clip between stencil passes may or may not
1379 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00001380 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001381#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001382 GrGLint value;
1383 if (insideClip) {
1384 value = (1 << (stencilBitCount - 1));
1385 } else {
1386 value = 0;
1387 }
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001388 this->flushRenderTarget(&SkIRect::EmptyIRect());
bsalomon@google.coma3201942012-06-21 19:58:20 +00001389
1390 GrAutoTRestore<ScissorState> asr(&fScissorState);
1391 fScissorState.fEnabled = true;
1392 fScissorState.fRect = rect;
1393 this->flushScissor();
1394
caryclark@google.comcf6285b2012-06-06 12:09:01 +00001395 GL_CALL(StencilMask((uint32_t) clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001396 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001397 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001398 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001399}
1400
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001401void GrGpuGL::onForceRenderTargetFlush() {
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001402 this->flushRenderTarget(&SkIRect::EmptyIRect());
reed@google.comac10a2d2010-12-22 21:39:39 +00001403}
1404
bsalomon@google.comc4364992011-11-07 15:54:49 +00001405bool GrGpuGL::readPixelsWillPayForYFlip(GrRenderTarget* renderTarget,
1406 int left, int top,
1407 int width, int height,
1408 GrPixelConfig config,
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001409 size_t rowBytes) const {
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001410 // If this rendertarget is aready TopLeft, we don't need to flip.
1411 if (kTopLeft_GrSurfaceOrigin == renderTarget->origin()) {
1412 return false;
1413 }
1414
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001415 // if GL can do the flip then we'll never pay for it.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001416 if (this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001417 return false;
1418 }
1419
1420 // If we have to do memcpy to handle non-trim rowBytes then we
bsalomon@google.com7107fa72011-11-10 14:54:14 +00001421 // get the flip for free. Otherwise it costs.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001422 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001423 return true;
1424 }
1425 // If we have to do memcpys to handle rowBytes then y-flip is free
1426 // Note the rowBytes might be tight to the passed in data, but if data
1427 // gets clipped in x to the target the rowBytes will no longer be tight.
1428 if (left >= 0 && (left + width) < renderTarget->width()) {
1429 return 0 == rowBytes ||
1430 GrBytesPerPixel(config) * width == rowBytes;
1431 } else {
1432 return false;
1433 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001434}
1435
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001436bool GrGpuGL::onReadPixels(GrRenderTarget* target,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001437 int left, int top,
1438 int width, int height,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001439 GrPixelConfig config,
1440 void* buffer,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001441 size_t rowBytes) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001442 GrGLenum format;
1443 GrGLenum type;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001444 bool flipY = kBottomLeft_GrSurfaceOrigin == target->origin();
bsalomon@google.com280e99f2012-01-05 16:17:38 +00001445 if (!this->configToGLFormats(config, false, NULL, &format, &type)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001446 return false;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001447 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001448 size_t bpp = GrBytesPerPixel(config);
1449 if (!adjust_pixel_ops_params(target->width(), target->height(), bpp,
1450 &left, &top, &width, &height,
1451 const_cast<const void**>(&buffer),
1452 &rowBytes)) {
1453 return false;
1454 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001455
bsalomon@google.comc6980972011-11-02 19:57:21 +00001456 // resolve the render target if necessary
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001457 GrGLRenderTarget* tgt = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001458 GrDrawState::AutoRenderTargetRestore artr;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001459 switch (tgt->getResolveType()) {
1460 case GrGLRenderTarget::kCantResolve_ResolveType:
1461 return false;
1462 case GrGLRenderTarget::kAutoResolves_ResolveType:
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001463 artr.set(this->drawState(), target);
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001464 this->flushRenderTarget(&SkIRect::EmptyIRect());
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001465 break;
1466 case GrGLRenderTarget::kCanResolve_ResolveType:
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001467 this->onResolveRenderTarget(tgt);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001468 // we don't track the state of the READ FBO ID.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001469 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1470 tgt->textureFBOID()));
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001471 break;
1472 default:
1473 GrCrash("Unknown resolve type");
reed@google.comac10a2d2010-12-22 21:39:39 +00001474 }
1475
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001476 const GrGLIRect& glvp = tgt->getViewport();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001477
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001478 // the read rect is viewport-relative
1479 GrGLIRect readRect;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001480 readRect.setRelativeTo(glvp, left, top, width, height, target->origin());
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001481
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001482 size_t tightRowBytes = bpp * width;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001483 if (0 == rowBytes) {
1484 rowBytes = tightRowBytes;
1485 }
1486 size_t readDstRowBytes = tightRowBytes;
1487 void* readDst = buffer;
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001488
bsalomon@google.comc6980972011-11-02 19:57:21 +00001489 // determine if GL can read using the passed rowBytes or if we need
1490 // a scratch buffer.
1491 SkAutoSMalloc<32 * sizeof(GrColor)> scratch;
1492 if (rowBytes != tightRowBytes) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001493 if (this->glCaps().packRowLengthSupport()) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001494 SkASSERT(!(rowBytes % sizeof(GrColor)));
bsalomon@google.comc6980972011-11-02 19:57:21 +00001495 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, rowBytes / sizeof(GrColor)));
1496 readDstRowBytes = rowBytes;
1497 } else {
1498 scratch.reset(tightRowBytes * height);
1499 readDst = scratch.get();
1500 }
1501 }
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001502 if (flipY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001503 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 1));
1504 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001505 GL_CALL(ReadPixels(readRect.fLeft, readRect.fBottom,
1506 readRect.fWidth, readRect.fHeight,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001507 format, type, readDst));
1508 if (readDstRowBytes != tightRowBytes) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001509 SkASSERT(this->glCaps().packRowLengthSupport());
bsalomon@google.comc6980972011-11-02 19:57:21 +00001510 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
1511 }
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001512 if (flipY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001513 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 0));
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001514 flipY = false;
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001515 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001516
1517 // now reverse the order of the rows, since GL's are bottom-to-top, but our
bsalomon@google.comc6980972011-11-02 19:57:21 +00001518 // API presents top-to-bottom. We must preserve the padding contents. Note
1519 // that the above readPixels did not overwrite the padding.
1520 if (readDst == buffer) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001521 SkASSERT(rowBytes == readDstRowBytes);
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001522 if (flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001523 scratch.reset(tightRowBytes);
1524 void* tmpRow = scratch.get();
1525 // flip y in-place by rows
1526 const int halfY = height >> 1;
1527 char* top = reinterpret_cast<char*>(buffer);
1528 char* bottom = top + (height - 1) * rowBytes;
1529 for (int y = 0; y < halfY; y++) {
1530 memcpy(tmpRow, top, tightRowBytes);
1531 memcpy(top, bottom, tightRowBytes);
1532 memcpy(bottom, tmpRow, tightRowBytes);
1533 top += rowBytes;
1534 bottom -= rowBytes;
1535 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001536 }
1537 } else {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001538 SkASSERT(readDst != buffer); SkASSERT(rowBytes != tightRowBytes);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001539 // copy from readDst to buffer while flipping y
caryclark@google.comcf6285b2012-06-06 12:09:01 +00001540 // const int halfY = height >> 1;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001541 const char* src = reinterpret_cast<const char*>(readDst);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001542 char* dst = reinterpret_cast<char*>(buffer);
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001543 if (flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001544 dst += (height-1) * rowBytes;
1545 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001546 for (int y = 0; y < height; y++) {
1547 memcpy(dst, src, tightRowBytes);
1548 src += readDstRowBytes;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001549 if (!flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001550 dst += rowBytes;
1551 } else {
1552 dst -= rowBytes;
1553 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001554 }
1555 }
1556 return true;
1557}
1558
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001559void GrGpuGL::flushRenderTarget(const SkIRect* bound) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001560
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001561 GrGLRenderTarget* rt =
1562 static_cast<GrGLRenderTarget*>(this->drawState()->getRenderTarget());
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001563 SkASSERT(NULL != rt);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001564
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001565 if (fHWBoundRenderTarget != rt) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001566 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, rt->renderFBOID()));
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +00001567#ifdef SK_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001568 GrGLenum status;
1569 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001570 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
reed@google.comb9255d52011-06-13 18:54:59 +00001571 GrPrintf("GrGpuGL::flushRenderTarget glCheckFramebufferStatus %x\n", status);
reed@google.comac10a2d2010-12-22 21:39:39 +00001572 }
robertphillips@google.coma6ffb582013-04-29 16:50:17 +00001573#endif
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001574 fHWBoundRenderTarget = rt;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001575 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.coma3201942012-06-21 19:58:20 +00001576 if (fHWViewport != vp) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001577 vp.pushToGLViewport(this->glInterface());
bsalomon@google.coma3201942012-06-21 19:58:20 +00001578 fHWViewport = vp;
reed@google.comac10a2d2010-12-22 21:39:39 +00001579 }
1580 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001581 if (NULL == bound || !bound->isEmpty()) {
1582 rt->flagAsNeedingResolve(bound);
1583 }
skia.committer@gmail.comaeefb2a2013-07-27 07:01:06 +00001584
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +00001585 GrTexture *texture = rt->asTexture();
1586 if (texture) {
1587 texture->dirtyMipMaps(true);
1588 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001589}
1590
twiz@google.com0f31ca72011-03-18 17:38:11 +00001591GrGLenum gPrimitiveType2GLMode[] = {
1592 GR_GL_TRIANGLES,
1593 GR_GL_TRIANGLE_STRIP,
1594 GR_GL_TRIANGLE_FAN,
1595 GR_GL_POINTS,
1596 GR_GL_LINES,
1597 GR_GL_LINE_STRIP
reed@google.comac10a2d2010-12-22 21:39:39 +00001598};
1599
bsalomon@google.comd302f142011-03-03 13:54:13 +00001600#define SWAP_PER_DRAW 0
1601
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001602#if SWAP_PER_DRAW
bsalomon@google.comd302f142011-03-03 13:54:13 +00001603 #if GR_MAC_BUILD
1604 #include <AGL/agl.h>
1605 #elif GR_WIN32_BUILD
bsalomon@google.comce7357d2012-06-25 17:49:25 +00001606 #include <gl/GL.h>
bsalomon@google.comd302f142011-03-03 13:54:13 +00001607 void SwapBuf() {
1608 DWORD procID = GetCurrentProcessId();
1609 HWND hwnd = GetTopWindow(GetDesktopWindow());
1610 while(hwnd) {
1611 DWORD wndProcID = 0;
1612 GetWindowThreadProcessId(hwnd, &wndProcID);
1613 if(wndProcID == procID) {
1614 SwapBuffers(GetDC(hwnd));
1615 }
1616 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
1617 }
1618 }
1619 #endif
1620#endif
1621
bsalomon@google.com74749cd2013-01-30 16:12:41 +00001622void GrGpuGL::onGpuDraw(const DrawInfo& info) {
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00001623 size_t indexOffsetInBytes;
1624 this->setupGeometry(info, &indexOffsetInBytes);
reed@google.comac10a2d2010-12-22 21:39:39 +00001625
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001626 SkASSERT((size_t)info.primitiveType() < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001627
bsalomon@google.com74749cd2013-01-30 16:12:41 +00001628 if (info.isIndexed()) {
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00001629 GrGLvoid* indices =
1630 reinterpret_cast<GrGLvoid*>(indexOffsetInBytes + sizeof(uint16_t) * info.startIndex());
bsalomon@google.com74749cd2013-01-30 16:12:41 +00001631 // info.startVertex() was accounted for by setupGeometry.
1632 GL_CALL(DrawElements(gPrimitiveType2GLMode[info.primitiveType()],
1633 info.indexCount(),
1634 GR_GL_UNSIGNED_SHORT,
1635 indices));
1636 } else {
1637 // Pass 0 for parameter first. We have to adjust glVertexAttribPointer() to account for
1638 // startVertex in the DrawElements case. So we always rely on setupGeometry to have
1639 // accounted for startVertex.
1640 GL_CALL(DrawArrays(gPrimitiveType2GLMode[info.primitiveType()], 0, info.vertexCount()));
1641 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00001642#if SWAP_PER_DRAW
1643 glFlush();
1644 #if GR_MAC_BUILD
1645 aglSwapBuffers(aglGetCurrentContext());
1646 int set_a_break_pt_here = 9;
1647 aglSwapBuffers(aglGetCurrentContext());
1648 #elif GR_WIN32_BUILD
1649 SwapBuf();
1650 int set_a_break_pt_here = 9;
1651 SwapBuf();
1652 #endif
1653#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001654}
1655
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001656namespace {
bsalomon@google.com100abf42012-09-05 17:40:04 +00001657
1658static const uint16_t kOnes16 = static_cast<uint16_t>(~0);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001659const GrStencilSettings& winding_nv_path_stencil_settings() {
1660 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
1661 kIncClamp_StencilOp,
1662 kIncClamp_StencilOp,
1663 kAlwaysIfInClip_StencilFunc,
bsalomon@google.com100abf42012-09-05 17:40:04 +00001664 kOnes16, kOnes16, kOnes16);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001665 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
1666}
1667const GrStencilSettings& even_odd_nv_path_stencil_settings() {
1668 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
1669 kInvert_StencilOp,
1670 kInvert_StencilOp,
1671 kAlwaysIfInClip_StencilFunc,
bsalomon@google.com100abf42012-09-05 17:40:04 +00001672 kOnes16, kOnes16, kOnes16);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001673 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
1674}
1675}
1676
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001677void GrGpuGL::setStencilPathSettings(const GrPath&,
sugoi@google.com12b4e272012-12-06 20:13:11 +00001678 SkPath::FillType fill,
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001679 GrStencilSettings* settings) {
1680 switch (fill) {
sugoi@google.com12b4e272012-12-06 20:13:11 +00001681 case SkPath::kEvenOdd_FillType:
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001682 *settings = even_odd_nv_path_stencil_settings();
1683 return;
sugoi@google.com12b4e272012-12-06 20:13:11 +00001684 case SkPath::kWinding_FillType:
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001685 *settings = winding_nv_path_stencil_settings();
1686 return;
1687 default:
1688 GrCrash("Unexpected path fill.");
1689 }
1690}
1691
sugoi@google.com12b4e272012-12-06 20:13:11 +00001692void GrGpuGL::onGpuStencilPath(const GrPath* path, SkPath::FillType fill) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001693 SkASSERT(this->caps()->pathStencilingSupport());
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001694
1695 GrGLuint id = static_cast<const GrGLPath*>(path)->pathID();
1696 GrDrawState* drawState = this->drawState();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001697 SkASSERT(NULL != drawState->getRenderTarget());
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001698 if (NULL == drawState->getRenderTarget()->getStencilBuffer()) {
1699 return;
1700 }
1701
1702 // Decide how to manipulate the stencil buffer based on the fill rule.
1703 // Also, assert that the stencil settings we set in setStencilPathSettings
1704 // are present.
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001705 SkASSERT(!fStencilSettings.isTwoSided());
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001706 GrGLenum fillMode;
1707 switch (fill) {
sugoi@google.com12b4e272012-12-06 20:13:11 +00001708 case SkPath::kWinding_FillType:
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001709 fillMode = GR_GL_COUNT_UP;
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001710 SkASSERT(kIncClamp_StencilOp ==
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001711 fStencilSettings.passOp(GrStencilSettings::kFront_Face));
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001712 SkASSERT(kIncClamp_StencilOp ==
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001713 fStencilSettings.failOp(GrStencilSettings::kFront_Face));
1714 break;
sugoi@google.com12b4e272012-12-06 20:13:11 +00001715 case SkPath::kEvenOdd_FillType:
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001716 fillMode = GR_GL_INVERT;
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001717 SkASSERT(kInvert_StencilOp ==
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001718 fStencilSettings.passOp(GrStencilSettings::kFront_Face));
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001719 SkASSERT(kInvert_StencilOp ==
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001720 fStencilSettings.failOp(GrStencilSettings::kFront_Face));
1721 break;
1722 default:
1723 // Only the above two fill rules are allowed.
1724 GrCrash("Unexpected path fill.");
bsalomon@google.com548a4332012-07-11 19:45:22 +00001725 return; // suppress unused var warning.
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001726 }
1727 GrGLint writeMask = fStencilSettings.writeMask(GrStencilSettings::kFront_Face);
1728 GL_CALL(StencilFillPath(id, fillMode, writeMask));
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00001729}
1730
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001731void GrGpuGL::onResolveRenderTarget(GrRenderTarget* target) {
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001732 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001733 if (rt->needsResolve()) {
bsalomon@google.com347c3822013-05-01 20:10:01 +00001734 // Some extensions automatically resolves the texture when it is read.
1735 if (this->glCaps().usesMSAARenderBuffers()) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001736 SkASSERT(rt->textureFBOID() != rt->renderFBOID());
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001737 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER, rt->renderFBOID()));
1738 GL_CALL(BindFramebuffer(GR_GL_DRAW_FRAMEBUFFER, rt->textureFBOID()));
1739 // make sure we go through flushRenderTarget() since we've modified
1740 // the bound DRAW FBO ID.
1741 fHWBoundRenderTarget = NULL;
1742 const GrGLIRect& vp = rt->getViewport();
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001743 const SkIRect dirtyRect = rt->getResolveRect();
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001744 GrGLIRect r;
1745 r.setRelativeTo(vp, dirtyRect.fLeft, dirtyRect.fTop,
1746 dirtyRect.width(), dirtyRect.height(), target->origin());
reed@google.comac10a2d2010-12-22 21:39:39 +00001747
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001748 GrAutoTRestore<ScissorState> asr;
bsalomon@google.com347c3822013-05-01 20:10:01 +00001749 if (GrGLCaps::kES_Apple_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001750 // Apple's extension uses the scissor as the blit bounds.
bsalomon@google.coma3201942012-06-21 19:58:20 +00001751 asr.reset(&fScissorState);
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001752 fScissorState.fEnabled = true;
1753 fScissorState.fRect = dirtyRect;
bsalomon@google.coma3201942012-06-21 19:58:20 +00001754 this->flushScissor();
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001755 GL_CALL(ResolveMultisampleFramebuffer());
1756 } else {
bsalomon@google.com347c3822013-05-01 20:10:01 +00001757 if (GrGLCaps::kDesktop_EXT_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001758 // this respects the scissor during the blit, so disable it.
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001759 asr.reset(&fScissorState);
1760 fScissorState.fEnabled = false;
1761 this->flushScissor();
1762 }
1763 int right = r.fLeft + r.fWidth;
1764 int top = r.fBottom + r.fHeight;
1765 GL_CALL(BlitFramebuffer(r.fLeft, r.fBottom, right, top,
1766 r.fLeft, r.fBottom, right, top,
1767 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001768 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001769 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001770 rt->flagAsResolved();
reed@google.comac10a2d2010-12-22 21:39:39 +00001771 }
1772}
1773
bsalomon@google.com411dad02012-06-05 20:24:20 +00001774namespace {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001775
bsalomon@google.com411dad02012-06-05 20:24:20 +00001776GrGLenum gr_to_gl_stencil_func(GrStencilFunc basicFunc) {
1777 static const GrGLenum gTable[] = {
1778 GR_GL_ALWAYS, // kAlways_StencilFunc
1779 GR_GL_NEVER, // kNever_StencilFunc
1780 GR_GL_GREATER, // kGreater_StencilFunc
1781 GR_GL_GEQUAL, // kGEqual_StencilFunc
1782 GR_GL_LESS, // kLess_StencilFunc
1783 GR_GL_LEQUAL, // kLEqual_StencilFunc,
1784 GR_GL_EQUAL, // kEqual_StencilFunc,
1785 GR_GL_NOTEQUAL, // kNotEqual_StencilFunc,
1786 };
1787 GR_STATIC_ASSERT(GR_ARRAY_COUNT(gTable) == kBasicStencilFuncCount);
1788 GR_STATIC_ASSERT(0 == kAlways_StencilFunc);
1789 GR_STATIC_ASSERT(1 == kNever_StencilFunc);
1790 GR_STATIC_ASSERT(2 == kGreater_StencilFunc);
1791 GR_STATIC_ASSERT(3 == kGEqual_StencilFunc);
1792 GR_STATIC_ASSERT(4 == kLess_StencilFunc);
1793 GR_STATIC_ASSERT(5 == kLEqual_StencilFunc);
1794 GR_STATIC_ASSERT(6 == kEqual_StencilFunc);
1795 GR_STATIC_ASSERT(7 == kNotEqual_StencilFunc);
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001796 SkASSERT((unsigned) basicFunc < kBasicStencilFuncCount);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001797
1798 return gTable[basicFunc];
1799}
1800
1801GrGLenum gr_to_gl_stencil_op(GrStencilOp op) {
1802 static const GrGLenum gTable[] = {
1803 GR_GL_KEEP, // kKeep_StencilOp
1804 GR_GL_REPLACE, // kReplace_StencilOp
1805 GR_GL_INCR_WRAP, // kIncWrap_StencilOp
1806 GR_GL_INCR, // kIncClamp_StencilOp
1807 GR_GL_DECR_WRAP, // kDecWrap_StencilOp
1808 GR_GL_DECR, // kDecClamp_StencilOp
1809 GR_GL_ZERO, // kZero_StencilOp
1810 GR_GL_INVERT, // kInvert_StencilOp
1811 };
1812 GR_STATIC_ASSERT(GR_ARRAY_COUNT(gTable) == kStencilOpCount);
1813 GR_STATIC_ASSERT(0 == kKeep_StencilOp);
1814 GR_STATIC_ASSERT(1 == kReplace_StencilOp);
1815 GR_STATIC_ASSERT(2 == kIncWrap_StencilOp);
1816 GR_STATIC_ASSERT(3 == kIncClamp_StencilOp);
1817 GR_STATIC_ASSERT(4 == kDecWrap_StencilOp);
1818 GR_STATIC_ASSERT(5 == kDecClamp_StencilOp);
1819 GR_STATIC_ASSERT(6 == kZero_StencilOp);
1820 GR_STATIC_ASSERT(7 == kInvert_StencilOp);
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001821 SkASSERT((unsigned) op < kStencilOpCount);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001822 return gTable[op];
1823}
1824
1825void set_gl_stencil(const GrGLInterface* gl,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001826 const GrStencilSettings& settings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00001827 GrGLenum glFace,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001828 GrStencilSettings::Face grFace) {
1829 GrGLenum glFunc = gr_to_gl_stencil_func(settings.func(grFace));
1830 GrGLenum glFailOp = gr_to_gl_stencil_op(settings.failOp(grFace));
1831 GrGLenum glPassOp = gr_to_gl_stencil_op(settings.passOp(grFace));
1832
1833 GrGLint ref = settings.funcRef(grFace);
1834 GrGLint mask = settings.funcMask(grFace);
1835 GrGLint writeMask = settings.writeMask(grFace);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001836
1837 if (GR_GL_FRONT_AND_BACK == glFace) {
1838 // we call the combined func just in case separate stencil is not
1839 // supported.
1840 GR_GL_CALL(gl, StencilFunc(glFunc, ref, mask));
1841 GR_GL_CALL(gl, StencilMask(writeMask));
1842 GR_GL_CALL(gl, StencilOp(glFailOp, glPassOp, glPassOp));
1843 } else {
1844 GR_GL_CALL(gl, StencilFuncSeparate(glFace, glFunc, ref, mask));
1845 GR_GL_CALL(gl, StencilMaskSeparate(glFace, writeMask));
1846 GR_GL_CALL(gl, StencilOpSeparate(glFace, glFailOp, glPassOp, glPassOp));
1847 }
1848}
1849}
bsalomon@google.comd302f142011-03-03 13:54:13 +00001850
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001851void GrGpuGL::flushStencil(DrawType type) {
1852 if (kStencilPath_DrawType == type) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001853 SkASSERT(!fStencilSettings.isTwoSided());
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001854 // Just the func, ref, and mask is set here. The op and write mask are params to the call
1855 // that draws the path to the SB (glStencilFillPath)
1856 GrGLenum func =
1857 gr_to_gl_stencil_func(fStencilSettings.func(GrStencilSettings::kFront_Face));
1858 GL_CALL(PathStencilFunc(func,
1859 fStencilSettings.funcRef(GrStencilSettings::kFront_Face),
1860 fStencilSettings.funcMask(GrStencilSettings::kFront_Face)));
1861 } else if (fHWStencilSettings != fStencilSettings) {
1862 if (fStencilSettings.isDisabled()) {
1863 if (kNo_TriState != fHWStencilTestEnabled) {
1864 GL_CALL(Disable(GR_GL_STENCIL_TEST));
1865 fHWStencilTestEnabled = kNo_TriState;
1866 }
1867 } else {
1868 if (kYes_TriState != fHWStencilTestEnabled) {
1869 GL_CALL(Enable(GR_GL_STENCIL_TEST));
1870 fHWStencilTestEnabled = kYes_TriState;
1871 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001872 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001873 if (!fStencilSettings.isDisabled()) {
bsalomon@google.combcce8922013-03-25 15:38:39 +00001874 if (this->caps()->twoSidedStencilSupport()) {
bsalomon@google.com411dad02012-06-05 20:24:20 +00001875 set_gl_stencil(this->glInterface(),
bsalomon@google.coma3201942012-06-21 19:58:20 +00001876 fStencilSettings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00001877 GR_GL_FRONT,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001878 GrStencilSettings::kFront_Face);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001879 set_gl_stencil(this->glInterface(),
bsalomon@google.coma3201942012-06-21 19:58:20 +00001880 fStencilSettings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00001881 GR_GL_BACK,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001882 GrStencilSettings::kBack_Face);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001883 } else {
bsalomon@google.com411dad02012-06-05 20:24:20 +00001884 set_gl_stencil(this->glInterface(),
bsalomon@google.coma3201942012-06-21 19:58:20 +00001885 fStencilSettings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00001886 GR_GL_FRONT_AND_BACK,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001887 GrStencilSettings::kFront_Face);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001888 }
1889 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001890 fHWStencilSettings = fStencilSettings;
reed@google.comac10a2d2010-12-22 21:39:39 +00001891 }
1892}
1893
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001894void GrGpuGL::flushAAState(DrawType type) {
bsalomon@google.com202d1392013-03-19 18:58:08 +00001895// At least some ATI linux drivers will render GL_LINES incorrectly when MSAA state is enabled but
1896// the target is not multisampled. Single pixel wide lines are rendered thicker than 1 pixel wide.
1897#if 0
1898 // Replace RT_HAS_MSAA with this definition once this driver bug is no longer a relevant concern
1899 #define RT_HAS_MSAA rt->isMultisampled()
1900#else
1901 #define RT_HAS_MSAA (rt->isMultisampled() || kDrawLines_DrawType == type)
1902#endif
1903
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001904 const GrRenderTarget* rt = this->getDrawState().getRenderTarget();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001905 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001906 // ES doesn't support toggling GL_MULTISAMPLE and doesn't have
1907 // smooth lines.
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001908 // we prefer smooth lines over multisampled lines
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00001909 bool smoothLines = false;
1910
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001911 if (kDrawLines_DrawType == type) {
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00001912 smoothLines = this->willUseHWAALines();
1913 if (smoothLines) {
1914 if (kYes_TriState != fHWAAState.fSmoothLineEnabled) {
1915 GL_CALL(Enable(GR_GL_LINE_SMOOTH));
1916 fHWAAState.fSmoothLineEnabled = kYes_TriState;
1917 // must disable msaa to use line smoothing
bsalomon@google.com202d1392013-03-19 18:58:08 +00001918 if (RT_HAS_MSAA &&
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00001919 kNo_TriState != fHWAAState.fMSAAEnabled) {
1920 GL_CALL(Disable(GR_GL_MULTISAMPLE));
1921 fHWAAState.fMSAAEnabled = kNo_TriState;
1922 }
1923 }
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001924 } else {
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00001925 if (kNo_TriState != fHWAAState.fSmoothLineEnabled) {
1926 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
1927 fHWAAState.fSmoothLineEnabled = kNo_TriState;
1928 }
1929 }
1930 }
bsalomon@google.com202d1392013-03-19 18:58:08 +00001931 if (!smoothLines && RT_HAS_MSAA) {
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001932 // FIXME: GL_NV_pr doesn't seem to like MSAA disabled. The paths
1933 // convex hulls of each segment appear to get filled.
1934 bool enableMSAA = kStencilPath_DrawType == type ||
1935 this->getDrawState().isHWAntialiasState();
1936 if (enableMSAA) {
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00001937 if (kYes_TriState != fHWAAState.fMSAAEnabled) {
1938 GL_CALL(Enable(GR_GL_MULTISAMPLE));
1939 fHWAAState.fMSAAEnabled = kYes_TriState;
1940 }
1941 } else {
1942 if (kNo_TriState != fHWAAState.fMSAAEnabled) {
1943 GL_CALL(Disable(GR_GL_MULTISAMPLE));
1944 fHWAAState.fMSAAEnabled = kNo_TriState;
1945 }
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001946 }
1947 }
1948 }
1949}
1950
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00001951void GrGpuGL::flushBlend(bool isLines,
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001952 GrBlendCoeff srcCoeff,
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001953 GrBlendCoeff dstCoeff) {
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00001954 if (isLines && this->willUseHWAALines()) {
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001955 if (kYes_TriState != fHWBlendState.fEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001956 GL_CALL(Enable(GR_GL_BLEND));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001957 fHWBlendState.fEnabled = kYes_TriState;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001958 }
bsalomon@google.com47059542012-06-06 20:51:20 +00001959 if (kSA_GrBlendCoeff != fHWBlendState.fSrcCoeff ||
1960 kISA_GrBlendCoeff != fHWBlendState.fDstCoeff) {
1961 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[kSA_GrBlendCoeff],
1962 gXfermodeCoeff2Blend[kISA_GrBlendCoeff]));
1963 fHWBlendState.fSrcCoeff = kSA_GrBlendCoeff;
1964 fHWBlendState.fDstCoeff = kISA_GrBlendCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001965 }
1966 } else {
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001967 // any optimization to disable blending should
1968 // have already been applied and tweaked the coeffs
1969 // to (1, 0).
bsalomon@google.com47059542012-06-06 20:51:20 +00001970 bool blendOff = kOne_GrBlendCoeff == srcCoeff &&
1971 kZero_GrBlendCoeff == dstCoeff;
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001972 if (blendOff) {
1973 if (kNo_TriState != fHWBlendState.fEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001974 GL_CALL(Disable(GR_GL_BLEND));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001975 fHWBlendState.fEnabled = kNo_TriState;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001976 }
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001977 } else {
1978 if (kYes_TriState != fHWBlendState.fEnabled) {
1979 GL_CALL(Enable(GR_GL_BLEND));
1980 fHWBlendState.fEnabled = kYes_TriState;
1981 }
1982 if (fHWBlendState.fSrcCoeff != srcCoeff ||
1983 fHWBlendState.fDstCoeff != dstCoeff) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001984 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
1985 gXfermodeCoeff2Blend[dstCoeff]));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001986 fHWBlendState.fSrcCoeff = srcCoeff;
1987 fHWBlendState.fDstCoeff = dstCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001988 }
bsalomon@google.coma5d056a2012-03-27 15:59:58 +00001989 GrColor blendConst = this->getDrawState().getBlendConstant();
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001990 if ((BlendCoeffReferencesConstant(srcCoeff) ||
1991 BlendCoeffReferencesConstant(dstCoeff)) &&
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001992 (!fHWBlendState.fConstColorValid ||
1993 fHWBlendState.fConstColor != blendConst)) {
bsalomon@google.com75347472012-09-17 17:23:21 +00001994 GrGLfloat c[4];
1995 GrColorToRGBAFloat(blendConst, c);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001996 GL_CALL(BlendColor(c[0], c[1], c[2], c[3]));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001997 fHWBlendState.fConstColor = blendConst;
1998 fHWBlendState.fConstColorValid = true;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001999 }
2000 }
2001 }
2002}
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002003
commit-bot@chromium.org6364b5e2013-08-20 20:22:52 +00002004static inline GrGLenum tile_to_gl_wrap(SkShader::TileMode tm) {
bsalomon@google.comb8670992012-07-25 21:27:09 +00002005 static const GrGLenum gWrapModes[] = {
2006 GR_GL_CLAMP_TO_EDGE,
2007 GR_GL_REPEAT,
2008 GR_GL_MIRRORED_REPEAT
2009 };
commit-bot@chromium.org5d7ca952013-04-22 20:26:44 +00002010 GR_STATIC_ASSERT(SkShader::kTileModeCount == SK_ARRAY_COUNT(gWrapModes));
bsalomon@google.comb8670992012-07-25 21:27:09 +00002011 GR_STATIC_ASSERT(0 == SkShader::kClamp_TileMode);
2012 GR_STATIC_ASSERT(1 == SkShader::kRepeat_TileMode);
2013 GR_STATIC_ASSERT(2 == SkShader::kMirror_TileMode);
2014 return gWrapModes[tm];
2015}
2016
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002017void GrGpuGL::bindTexture(int unitIdx, const GrTextureParams& params, GrGLTexture* texture) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002018 SkASSERT(NULL != texture);
tomhudson@google.comd0c1a062012-07-12 17:23:52 +00002019
bsalomon@google.comb8670992012-07-25 21:27:09 +00002020 // If we created a rt/tex and rendered to it without using a texture and now we're texturing
2021 // 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 +00002022 // out of the "last != next" check.
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002023 GrGLRenderTarget* texRT = static_cast<GrGLRenderTarget*>(texture->asRenderTarget());
bsalomon@google.com4c883782012-06-04 19:05:11 +00002024 if (NULL != texRT) {
2025 this->onResolveRenderTarget(texRT);
2026 }
2027
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002028 if (fHWBoundTextures[unitIdx] != texture) {
2029 this->setTextureUnit(unitIdx);
2030 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, texture->textureID()));
2031 fHWBoundTextures[unitIdx] = texture;
bsalomon@google.com4c883782012-06-04 19:05:11 +00002032 }
2033
bsalomon@google.com4c883782012-06-04 19:05:11 +00002034 ResetTimestamp timestamp;
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002035 const GrGLTexture::TexParams& oldTexParams = texture->getCachedTexParams(&timestamp);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002036 bool setAll = timestamp < this->getResetTimestamp();
2037 GrGLTexture::TexParams newTexParams;
2038
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00002039 static GrGLenum glMinFilterModes[] = {
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +00002040 GR_GL_NEAREST,
2041 GR_GL_LINEAR,
2042 GR_GL_LINEAR_MIPMAP_LINEAR
2043 };
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00002044 static GrGLenum glMagFilterModes[] = {
2045 GR_GL_NEAREST,
2046 GR_GL_LINEAR,
2047 GR_GL_LINEAR
2048 };
2049 newTexParams.fMinFilter = glMinFilterModes[params.filterMode()];
2050 newTexParams.fMagFilter = glMagFilterModes[params.filterMode()];
skia.committer@gmail.comaeefb2a2013-07-27 07:01:06 +00002051
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +00002052#ifndef SKIA_IGNORE_GPU_MIPMAPS
skia.committer@gmail.comaeefb2a2013-07-27 07:01:06 +00002053 if (params.filterMode() == GrTextureParams::kMipMap_FilterMode &&
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +00002054 texture->mipMapsAreDirty()) {
2055// GL_CALL(Hint(GR_GL_GENERATE_MIPMAP_HINT,GR_GL_NICEST));
2056 GL_CALL(GenerateMipmap(GR_GL_TEXTURE_2D));
2057 texture->dirtyMipMaps(false);
2058 }
2059#endif
bsalomon@google.com4c883782012-06-04 19:05:11 +00002060
bsalomon@google.comb8670992012-07-25 21:27:09 +00002061 newTexParams.fWrapS = tile_to_gl_wrap(params.getTileModeX());
2062 newTexParams.fWrapT = tile_to_gl_wrap(params.getTileModeY());
bsalomon@google.com4c883782012-06-04 19:05:11 +00002063 memcpy(newTexParams.fSwizzleRGBA,
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002064 GrGLShaderBuilder::GetTexParamSwizzle(texture->config(), this->glCaps()),
bsalomon@google.com4c883782012-06-04 19:05:11 +00002065 sizeof(newTexParams.fSwizzleRGBA));
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00002066 if (setAll || newTexParams.fMagFilter != oldTexParams.fMagFilter) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002067 this->setTextureUnit(unitIdx);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002068 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2069 GR_GL_TEXTURE_MAG_FILTER,
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00002070 newTexParams.fMagFilter));
2071 }
2072 if (setAll || newTexParams.fMinFilter != oldTexParams.fMinFilter) {
2073 this->setTextureUnit(unitIdx);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002074 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2075 GR_GL_TEXTURE_MIN_FILTER,
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00002076 newTexParams.fMinFilter));
bsalomon@google.com4c883782012-06-04 19:05:11 +00002077 }
2078 if (setAll || newTexParams.fWrapS != oldTexParams.fWrapS) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002079 this->setTextureUnit(unitIdx);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002080 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2081 GR_GL_TEXTURE_WRAP_S,
2082 newTexParams.fWrapS));
2083 }
2084 if (setAll || newTexParams.fWrapT != oldTexParams.fWrapT) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002085 this->setTextureUnit(unitIdx);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002086 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2087 GR_GL_TEXTURE_WRAP_T,
2088 newTexParams.fWrapT));
2089 }
2090 if (this->glCaps().textureSwizzleSupport() &&
2091 (setAll || memcmp(newTexParams.fSwizzleRGBA,
2092 oldTexParams.fSwizzleRGBA,
2093 sizeof(newTexParams.fSwizzleRGBA)))) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002094 this->setTextureUnit(unitIdx);
commit-bot@chromium.org6364b5e2013-08-20 20:22:52 +00002095 if (this->glBinding() == kES_GrGLBinding) {
2096 // ES3 added swizzle support but not GL_TEXTURE_SWIZZLE_RGBA.
2097 const GrGLenum* swizzle = newTexParams.fSwizzleRGBA;
2098 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_SWIZZLE_R, swizzle[0]));
2099 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_SWIZZLE_G, swizzle[1]));
2100 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_SWIZZLE_B, swizzle[2]));
2101 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_SWIZZLE_A, swizzle[3]));
2102 } else {
2103 GR_STATIC_ASSERT(sizeof(newTexParams.fSwizzleRGBA[0]) == sizeof(GrGLint));
2104 const GrGLint* swizzle = reinterpret_cast<const GrGLint*>(newTexParams.fSwizzleRGBA);
2105 GL_CALL(TexParameteriv(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_SWIZZLE_RGBA, swizzle));
2106 }
bsalomon@google.com4c883782012-06-04 19:05:11 +00002107 }
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002108 texture->setCachedTexParams(newTexParams, this->getResetTimestamp());
bsalomon@google.com4c883782012-06-04 19:05:11 +00002109}
2110
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002111void GrGpuGL::flushMiscFixedFunctionState() {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002112
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002113 const GrDrawState& drawState = this->getDrawState();
reed@google.comac10a2d2010-12-22 21:39:39 +00002114
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002115 if (drawState.isDitherState()) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002116 if (kYes_TriState != fHWDitherEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002117 GL_CALL(Enable(GR_GL_DITHER));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002118 fHWDitherEnabled = kYes_TriState;
2119 }
2120 } else {
2121 if (kNo_TriState != fHWDitherEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002122 GL_CALL(Disable(GR_GL_DITHER));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002123 fHWDitherEnabled = kNo_TriState;
reed@google.comac10a2d2010-12-22 21:39:39 +00002124 }
2125 }
2126
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002127 if (drawState.isColorWriteDisabled()) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002128 if (kNo_TriState != fHWWriteToColor) {
2129 GL_CALL(ColorMask(GR_GL_FALSE, GR_GL_FALSE,
2130 GR_GL_FALSE, GR_GL_FALSE));
2131 fHWWriteToColor = kNo_TriState;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002132 }
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002133 } else {
2134 if (kYes_TriState != fHWWriteToColor) {
2135 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
2136 fHWWriteToColor = kYes_TriState;
2137 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00002138 }
2139
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002140 if (fHWDrawFace != drawState.getDrawFace()) {
bsalomon@google.coma5d056a2012-03-27 15:59:58 +00002141 switch (this->getDrawState().getDrawFace()) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002142 case GrDrawState::kCCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002143 GL_CALL(Enable(GR_GL_CULL_FACE));
2144 GL_CALL(CullFace(GR_GL_BACK));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002145 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002146 case GrDrawState::kCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002147 GL_CALL(Enable(GR_GL_CULL_FACE));
2148 GL_CALL(CullFace(GR_GL_FRONT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002149 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002150 case GrDrawState::kBoth_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002151 GL_CALL(Disable(GR_GL_CULL_FACE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002152 break;
2153 default:
2154 GrCrash("Unknown draw face.");
2155 }
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002156 fHWDrawFace = drawState.getDrawFace();
bsalomon@google.comd302f142011-03-03 13:54:13 +00002157 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002158}
2159
reed@google.comac10a2d2010-12-22 21:39:39 +00002160void GrGpuGL::notifyRenderTargetDelete(GrRenderTarget* renderTarget) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002161 SkASSERT(NULL != renderTarget);
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002162 if (fHWBoundRenderTarget == renderTarget) {
2163 fHWBoundRenderTarget = NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +00002164 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002165}
2166
2167void GrGpuGL::notifyTextureDelete(GrGLTexture* texture) {
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002168 for (int s = 0; s < fHWBoundTextures.count(); ++s) {
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002169 if (fHWBoundTextures[s] == texture) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002170 // deleting bound texture does implied bind to 0
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002171 fHWBoundTextures[s] = NULL;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002172 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002173 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002174}
2175
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002176bool GrGpuGL::configToGLFormats(GrPixelConfig config,
2177 bool getSizedInternalFormat,
2178 GrGLenum* internalFormat,
2179 GrGLenum* externalFormat,
2180 GrGLenum* externalType) {
2181 GrGLenum dontCare;
2182 if (NULL == internalFormat) {
2183 internalFormat = &dontCare;
2184 }
2185 if (NULL == externalFormat) {
2186 externalFormat = &dontCare;
2187 }
2188 if (NULL == externalType) {
2189 externalType = &dontCare;
2190 }
2191
reed@google.comac10a2d2010-12-22 21:39:39 +00002192 switch (config) {
bsalomon@google.com0342a852012-08-20 19:22:38 +00002193 case kRGBA_8888_GrPixelConfig:
bsalomon@google.comc4364992011-11-07 15:54:49 +00002194 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002195 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002196 if (getSizedInternalFormat) {
2197 *internalFormat = GR_GL_RGBA8;
2198 } else {
2199 *internalFormat = GR_GL_RGBA;
2200 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002201 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.comc4364992011-11-07 15:54:49 +00002202 break;
bsalomon@google.com0342a852012-08-20 19:22:38 +00002203 case kBGRA_8888_GrPixelConfig:
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002204 if (!this->glCaps().bgraFormatSupport()) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002205 return false;
2206 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002207 if (this->glCaps().bgraIsInternalFormat()) {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002208 if (getSizedInternalFormat) {
2209 *internalFormat = GR_GL_BGRA8;
2210 } else {
2211 *internalFormat = GR_GL_BGRA;
2212 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002213 } else {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002214 if (getSizedInternalFormat) {
2215 *internalFormat = GR_GL_RGBA8;
2216 } else {
2217 *internalFormat = GR_GL_RGBA;
2218 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002219 }
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002220 *externalFormat = GR_GL_BGRA;
bsalomon@google.com6f379512011-11-16 20:36:03 +00002221 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002222 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002223 case kRGB_565_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002224 *internalFormat = GR_GL_RGB;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002225 *externalFormat = GR_GL_RGB;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002226 if (getSizedInternalFormat) {
2227 if (this->glBinding() == kDesktop_GrGLBinding) {
2228 return false;
2229 } else {
2230 *internalFormat = GR_GL_RGB565;
2231 }
2232 } else {
2233 *internalFormat = GR_GL_RGB;
2234 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002235 *externalType = GR_GL_UNSIGNED_SHORT_5_6_5;
reed@google.comac10a2d2010-12-22 21:39:39 +00002236 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002237 case kRGBA_4444_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002238 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002239 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002240 if (getSizedInternalFormat) {
2241 *internalFormat = GR_GL_RGBA4;
2242 } else {
2243 *internalFormat = GR_GL_RGBA;
2244 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002245 *externalType = GR_GL_UNSIGNED_SHORT_4_4_4_4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002246 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002247 case kIndex_8_GrPixelConfig:
bsalomon@google.combcce8922013-03-25 15:38:39 +00002248 if (this->caps()->eightBitPaletteSupport()) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002249 *internalFormat = GR_GL_PALETTE8_RGBA8;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002250 // glCompressedTexImage doesn't take external params
2251 *externalFormat = GR_GL_PALETTE8_RGBA8;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002252 // no sized/unsized internal format distinction here
2253 *internalFormat = GR_GL_PALETTE8_RGBA8;
2254 // unused with CompressedTexImage
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002255 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002256 } else {
2257 return false;
2258 }
2259 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002260 case kAlpha_8_GrPixelConfig:
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002261 if (this->glCaps().textureRedSupport()) {
2262 *internalFormat = GR_GL_RED;
2263 *externalFormat = GR_GL_RED;
2264 if (getSizedInternalFormat) {
2265 *internalFormat = GR_GL_R8;
2266 } else {
2267 *internalFormat = GR_GL_RED;
2268 }
2269 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002270 } else {
2271 *internalFormat = GR_GL_ALPHA;
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002272 *externalFormat = GR_GL_ALPHA;
2273 if (getSizedInternalFormat) {
2274 *internalFormat = GR_GL_ALPHA8;
2275 } else {
2276 *internalFormat = GR_GL_ALPHA;
2277 }
2278 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002279 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002280 break;
2281 default:
2282 return false;
2283 }
2284 return true;
2285}
2286
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002287void GrGpuGL::setTextureUnit(int unit) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002288 SkASSERT(unit >= 0 && unit < fHWBoundTextures.count());
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002289 if (unit != fHWActiveTextureUnitIdx) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002290 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com49209392012-06-05 15:13:46 +00002291 fHWActiveTextureUnitIdx = unit;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002292 }
2293}
bsalomon@google.com316f99232011-01-13 21:28:12 +00002294
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002295void GrGpuGL::setScratchTextureUnit() {
2296 // Bind the last texture unit since it is the least likely to be used by GrGLProgram.
2297 int lastUnitIdx = fHWBoundTextures.count() - 1;
2298 if (lastUnitIdx != fHWActiveTextureUnitIdx) {
2299 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + lastUnitIdx));
2300 fHWActiveTextureUnitIdx = lastUnitIdx;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002301 }
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002302 // clear out the this field so that if a program does use this unit it will rebind the correct
2303 // texture.
2304 fHWBoundTextures[lastUnitIdx] = NULL;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002305}
2306
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002307namespace {
2308// Determines whether glBlitFramebuffer could be used between src and dst.
bsalomon@google.comeb851172013-04-15 13:51:00 +00002309inline bool can_blit_framebuffer(const GrSurface* dst,
2310 const GrSurface* src,
2311 const GrGpuGL* gpu,
2312 bool* wouldNeedTempFBO = NULL) {
bsalomon@google.com347c3822013-05-01 20:10:01 +00002313 if (gpu->isConfigRenderable(dst->config()) &&
2314 gpu->isConfigRenderable(src->config()) &&
2315 gpu->glCaps().usesMSAARenderBuffers()) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002316 if (NULL != wouldNeedTempFBO) {
2317 *wouldNeedTempFBO = NULL == dst->asRenderTarget() || NULL == src->asRenderTarget();
2318 }
2319 return true;
2320 } else {
2321 return false;
2322 }
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002323}
bsalomon@google.comeb851172013-04-15 13:51:00 +00002324
2325inline bool can_copy_texsubimage(const GrSurface* dst,
2326 const GrSurface* src,
2327 const GrGpuGL* gpu,
2328 bool* wouldNeedTempFBO = NULL) {
2329 // Table 3.9 of the ES2 spec indicates the supported formats with CopyTexSubImage
2330 // and BGRA isn't in the spec. There doesn't appear to be any extension that adds it. Perhaps
2331 // many drivers would allow it to work, but ANGLE does not.
bsalomon@google.com791816a2013-08-15 18:54:39 +00002332 if (kES_GrGLBinding == gpu->glBinding() && gpu->glCaps().bgraIsInternalFormat() &&
bsalomon@google.comeb851172013-04-15 13:51:00 +00002333 (kBGRA_8888_GrPixelConfig == dst->config() || kBGRA_8888_GrPixelConfig == src->config())) {
2334 return false;
2335 }
2336 const GrGLRenderTarget* dstRT = static_cast<const GrGLRenderTarget*>(dst->asRenderTarget());
2337 // If dst is multisampled (and uses an extension where there is a separate MSAA renderbuffer)
2338 // then we don't want to copy to the texture but to the MSAA buffer.
2339 if (NULL != dstRT && dstRT->renderFBOID() != dstRT->textureFBOID()) {
2340 return false;
2341 }
bsalomon@google.coma2719852013-04-17 14:25:27 +00002342 const GrGLRenderTarget* srcRT = static_cast<const GrGLRenderTarget*>(src->asRenderTarget());
2343 // If the src is multisampled (and uses an extension where there is a separate MSAA
2344 // renderbuffer) then it is an invalid operation to call CopyTexSubImage
2345 if (NULL != srcRT && srcRT->renderFBOID() != srcRT->textureFBOID()) {
2346 return false;
2347 }
bsalomon@google.comeb851172013-04-15 13:51:00 +00002348 if (gpu->isConfigRenderable(src->config()) && NULL != dst->asTexture() &&
2349 dst->origin() == src->origin() && kIndex_8_GrPixelConfig != src->config()) {
2350 if (NULL != wouldNeedTempFBO) {
2351 *wouldNeedTempFBO = NULL == src->asRenderTarget();
2352 }
2353 return true;
2354 } else {
2355 return false;
2356 }
2357}
2358
2359// If a temporary FBO was created, its non-zero ID is returned. The viewport that the copy rect is
2360// relative to is output.
2361inline GrGLuint bind_surface_as_fbo(const GrGLInterface* gl,
2362 GrSurface* surface,
2363 GrGLenum fboTarget,
2364 GrGLIRect* viewport) {
2365 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(surface->asRenderTarget());
2366 GrGLuint tempFBOID;
2367 if (NULL == rt) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002368 SkASSERT(NULL != surface->asTexture());
bsalomon@google.comeb851172013-04-15 13:51:00 +00002369 GrGLuint texID = static_cast<GrGLTexture*>(surface->asTexture())->textureID();
2370 GR_GL_CALL(gl, GenFramebuffers(1, &tempFBOID));
2371 GR_GL_CALL(gl, BindFramebuffer(fboTarget, tempFBOID));
2372 GR_GL_CALL(gl, FramebufferTexture2D(fboTarget,
2373 GR_GL_COLOR_ATTACHMENT0,
2374 GR_GL_TEXTURE_2D,
2375 texID,
2376 0));
2377 viewport->fLeft = 0;
2378 viewport->fBottom = 0;
2379 viewport->fWidth = surface->width();
2380 viewport->fHeight = surface->height();
2381 } else {
2382 tempFBOID = 0;
2383 GR_GL_CALL(gl, BindFramebuffer(fboTarget, rt->renderFBOID()));
2384 *viewport = rt->getViewport();
2385 }
2386 return tempFBOID;
2387}
2388
2389}
2390
2391void GrGpuGL::initCopySurfaceDstDesc(const GrSurface* src, GrTextureDesc* desc) {
2392 // Check for format issues with glCopyTexSubImage2D
bsalomon@google.com791816a2013-08-15 18:54:39 +00002393 if (kES_GrGLBinding == this->glBinding() && this->glCaps().bgraIsInternalFormat() &&
bsalomon@google.comeb851172013-04-15 13:51:00 +00002394 kBGRA_8888_GrPixelConfig == src->config()) {
2395 // glCopyTexSubImage2D doesn't work with this config. We'll want to make it a render target
bsalomon@google.com31c4e892013-04-15 13:55:02 +00002396 // in order to call glBlitFramebuffer or to copy to it by rendering.
bsalomon@google.comeb851172013-04-15 13:51:00 +00002397 INHERITED::initCopySurfaceDstDesc(src, desc);
bsalomon@google.coma2719852013-04-17 14:25:27 +00002398 return;
bsalomon@google.comeb851172013-04-15 13:51:00 +00002399 } else if (NULL == src->asRenderTarget()) {
2400 // We don't want to have to create an FBO just to use glCopyTexSubImage2D. Let the base
2401 // class handle it by rendering.
2402 INHERITED::initCopySurfaceDstDesc(src, desc);
bsalomon@google.coma2719852013-04-17 14:25:27 +00002403 return;
2404 }
2405
2406 const GrGLRenderTarget* srcRT = static_cast<const GrGLRenderTarget*>(src->asRenderTarget());
2407 if (NULL != srcRT && srcRT->renderFBOID() != srcRT->textureFBOID()) {
2408 // It's illegal to call CopyTexSubImage2D on a MSAA renderbuffer.
2409 INHERITED::initCopySurfaceDstDesc(src, desc);
bsalomon@google.comeb851172013-04-15 13:51:00 +00002410 } else {
2411 desc->fConfig = src->config();
2412 desc->fOrigin = src->origin();
2413 desc->fFlags = kNone_GrTextureFlags;
2414 }
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002415}
2416
2417bool GrGpuGL::onCopySurface(GrSurface* dst,
2418 GrSurface* src,
2419 const SkIRect& srcRect,
2420 const SkIPoint& dstPoint) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002421 bool inheritedCouldCopy = INHERITED::onCanCopySurface(dst, src, srcRect, dstPoint);
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002422 bool copied = false;
bsalomon@google.comeb851172013-04-15 13:51:00 +00002423 bool wouldNeedTempFBO = false;
2424 if (can_copy_texsubimage(dst, src, this, &wouldNeedTempFBO) &&
2425 (!wouldNeedTempFBO || !inheritedCouldCopy)) {
2426 GrGLuint srcFBO;
2427 GrGLIRect srcVP;
2428 srcFBO = bind_surface_as_fbo(this->glInterface(), src, GR_GL_FRAMEBUFFER, &srcVP);
2429 GrGLTexture* dstTex = static_cast<GrGLTexture*>(dst->asTexture());
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002430 SkASSERT(NULL != dstTex);
bsalomon@google.comeb851172013-04-15 13:51:00 +00002431 // We modified the bound FBO
2432 fHWBoundRenderTarget = NULL;
2433 GrGLIRect srcGLRect;
2434 srcGLRect.setRelativeTo(srcVP,
2435 srcRect.fLeft,
2436 srcRect.fTop,
2437 srcRect.width(),
2438 srcRect.height(),
2439 src->origin());
2440
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002441 this->setScratchTextureUnit();
bsalomon@google.comeb851172013-04-15 13:51:00 +00002442 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, dstTex->textureID()));
2443 GrGLint dstY;
2444 if (kBottomLeft_GrSurfaceOrigin == dst->origin()) {
2445 dstY = dst->height() - (dstPoint.fY + srcGLRect.fHeight);
2446 } else {
2447 dstY = dstPoint.fY;
2448 }
2449 GL_CALL(CopyTexSubImage2D(GR_GL_TEXTURE_2D, 0,
2450 dstPoint.fX, dstY,
2451 srcGLRect.fLeft, srcGLRect.fBottom,
2452 srcGLRect.fWidth, srcGLRect.fHeight));
2453 copied = true;
2454 if (srcFBO) {
2455 GL_CALL(DeleteFramebuffers(1, &srcFBO));
2456 }
2457 } else if (can_blit_framebuffer(dst, src, this, &wouldNeedTempFBO) &&
2458 (!wouldNeedTempFBO || !inheritedCouldCopy)) {
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002459 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
2460 srcRect.width(), srcRect.height());
2461 bool selfOverlap = false;
2462 if (dst->isSameAs(src)) {
2463 selfOverlap = SkIRect::IntersectsNoEmptyCheck(dstRect, srcRect);
2464 }
2465
2466 if (!selfOverlap) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002467 GrGLuint dstFBO;
2468 GrGLuint srcFBO;
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002469 GrGLIRect dstVP;
2470 GrGLIRect srcVP;
bsalomon@google.comeb851172013-04-15 13:51:00 +00002471 dstFBO = bind_surface_as_fbo(this->glInterface(), dst, GR_GL_DRAW_FRAMEBUFFER, &dstVP);
2472 srcFBO = bind_surface_as_fbo(this->glInterface(), src, GR_GL_READ_FRAMEBUFFER, &srcVP);
2473 // We modified the bound FBO
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002474 fHWBoundRenderTarget = NULL;
2475 GrGLIRect srcGLRect;
2476 GrGLIRect dstGLRect;
2477 srcGLRect.setRelativeTo(srcVP,
2478 srcRect.fLeft,
2479 srcRect.fTop,
2480 srcRect.width(),
2481 srcRect.height(),
2482 src->origin());
2483 dstGLRect.setRelativeTo(dstVP,
2484 dstRect.fLeft,
2485 dstRect.fTop,
2486 dstRect.width(),
2487 dstRect.height(),
2488 dst->origin());
2489
2490 GrAutoTRestore<ScissorState> asr;
bsalomon@google.com347c3822013-05-01 20:10:01 +00002491 if (GrGLCaps::kDesktop_EXT_MSFBOType == this->glCaps().msFBOType()) {
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002492 // The EXT version applies the scissor during the blit, so disable it.
2493 asr.reset(&fScissorState);
2494 fScissorState.fEnabled = false;
2495 this->flushScissor();
2496 }
2497 GrGLint srcY0;
2498 GrGLint srcY1;
2499 // Does the blit need to y-mirror or not?
2500 if (src->origin() == dst->origin()) {
2501 srcY0 = srcGLRect.fBottom;
2502 srcY1 = srcGLRect.fBottom + srcGLRect.fHeight;
2503 } else {
2504 srcY0 = srcGLRect.fBottom + srcGLRect.fHeight;
2505 srcY1 = srcGLRect.fBottom;
2506 }
2507 GL_CALL(BlitFramebuffer(srcGLRect.fLeft,
2508 srcY0,
2509 srcGLRect.fLeft + srcGLRect.fWidth,
2510 srcY1,
2511 dstGLRect.fLeft,
2512 dstGLRect.fBottom,
2513 dstGLRect.fLeft + dstGLRect.fWidth,
2514 dstGLRect.fBottom + dstGLRect.fHeight,
2515 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
2516 if (dstFBO) {
2517 GL_CALL(DeleteFramebuffers(1, &dstFBO));
2518 }
2519 if (srcFBO) {
2520 GL_CALL(DeleteFramebuffers(1, &srcFBO));
2521 }
2522 copied = true;
2523 }
2524 }
bsalomon@google.comeb851172013-04-15 13:51:00 +00002525 if (!copied && inheritedCouldCopy) {
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002526 copied = INHERITED::onCopySurface(dst, src, srcRect, dstPoint);
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002527 SkASSERT(copied);
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002528 }
2529 return copied;
2530}
2531
2532bool GrGpuGL::onCanCopySurface(GrSurface* dst,
2533 GrSurface* src,
2534 const SkIRect& srcRect,
2535 const SkIPoint& dstPoint) {
2536 // This mirrors the logic in onCopySurface.
bsalomon@google.comeb851172013-04-15 13:51:00 +00002537 if (can_copy_texsubimage(dst, src, this)) {
2538 return true;
2539 }
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002540 if (can_blit_framebuffer(dst, src, this)) {
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002541 if (dst->isSameAs(src)) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002542 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
2543 srcRect.width(), srcRect.height());
2544 if(!SkIRect::IntersectsNoEmptyCheck(dstRect, srcRect)) {
2545 return true;
2546 }
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002547 } else {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002548 return true;
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002549 }
2550 }
bsalomon@google.comeb851172013-04-15 13:51:00 +00002551 return INHERITED::onCanCopySurface(dst, src, srcRect, dstPoint);
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002552}
2553
2554
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00002555///////////////////////////////////////////////////////////////////////////////
2556
bsalomon@google.com6918d482013-03-07 19:09:11 +00002557GrGLAttribArrayState* GrGpuGL::HWGeometryState::bindArrayAndBuffersToDraw(
2558 GrGpuGL* gpu,
2559 const GrGLVertexBuffer* vbuffer,
2560 const GrGLIndexBuffer* ibuffer) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002561 SkASSERT(NULL != vbuffer);
robertphillips@google.com4f65a272013-03-26 19:40:46 +00002562 GrGLAttribArrayState* attribState;
2563
bsalomon@google.com6918d482013-03-07 19:09:11 +00002564 // We use a vertex array if we're on a core profile and the verts are in a VBO.
2565 if (gpu->glCaps().isCoreProfile() && !vbuffer->isCPUBacked()) {
2566 if (NULL == fVBOVertexArray || !fVBOVertexArray->isValid()) {
2567 SkSafeUnref(fVBOVertexArray);
2568 GrGLuint arrayID;
2569 GR_GL_CALL(gpu->glInterface(), GenVertexArrays(1, &arrayID));
2570 int attrCount = gpu->glCaps().maxVertexAttributes();
2571 fVBOVertexArray = SkNEW_ARGS(GrGLVertexArray, (gpu, arrayID, attrCount));
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00002572 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00002573 attribState = fVBOVertexArray->bindWithIndexBuffer(ibuffer);
2574 } else {
2575 if (NULL != ibuffer) {
2576 this->setIndexBufferIDOnDefaultVertexArray(gpu, ibuffer->bufferID());
2577 } else {
2578 this->setVertexArrayID(gpu, 0);
2579 }
2580 int attrCount = gpu->glCaps().maxVertexAttributes();
2581 if (fDefaultVertexArrayAttribState.count() != attrCount) {
2582 fDefaultVertexArrayAttribState.resize(attrCount);
2583 }
2584 attribState = &fDefaultVertexArrayAttribState;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00002585 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00002586 return attribState;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002587}