blob: 18165d9ca31629e8bf3becd6a76a8f674ee2c89c [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());
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000127 fHWTexGenSettings.reset(ctx.info().caps()->maxFixedFunctionTextureCoords());
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +0000128
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000129 fillInConfigRenderableTable();
130
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000131
robertphillips@google.com6177e692013-02-28 20:16:25 +0000132 GrGLClearErr(fGLContext.interface());
reed@google.comac10a2d2010-12-22 21:39:39 +0000133
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000134 if (gPrintStartupSpew) {
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000135 const GrGLubyte* vendor;
136 const GrGLubyte* renderer;
137 const GrGLubyte* version;
138 GL_CALL_RET(vendor, GetString(GR_GL_VENDOR));
139 GL_CALL_RET(renderer, GetString(GR_GL_RENDERER));
140 GL_CALL_RET(version, GetString(GR_GL_VERSION));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000141 GrPrintf("------------------------- create GrGpuGL %p --------------\n",
142 this);
143 GrPrintf("------ VENDOR %s\n", vendor);
144 GrPrintf("------ RENDERER %s\n", renderer);
145 GrPrintf("------ VERSION %s\n", version);
bsalomon@google.com00142c42013-05-02 19:42:54 +0000146 GrPrintf("------ EXTENSIONS\n");
147 ctx.info().extensions().print();
148 GrPrintf("\n");
bsalomon@google.combcce8922013-03-25 15:38:39 +0000149 ctx.info().caps()->print();
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000150 }
151
commit-bot@chromium.org9188a152013-09-05 18:28:24 +0000152 fProgramCache = SkNEW_ARGS(ProgramCache, (this));
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000153
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000154 SkASSERT(this->glCaps().maxVertexAttributes() >= GrDrawState::kMaxVertexAttribCnt);
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000155
bsalomon@google.comfe676522011-06-17 18:12:21 +0000156 fLastSuccessfulStencilFmtIdx = 0;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000157 fHWProgramID = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000158}
159
160GrGpuGL::~GrGpuGL() {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000161 if (0 != fHWProgramID) {
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000162 // detach the current program so there is no confusion on OpenGL's part
163 // that we want it to be deleted
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000164 SkASSERT(fHWProgramID == fCurrentProgram->programID());
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000165 GL_CALL(UseProgram(0));
166 }
167
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000168 delete fProgramCache;
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000169
bsalomon@google.com4a018bb2011-10-28 19:50:21 +0000170 // This must be called by before the GrDrawTarget destructor
171 this->releaseGeometry();
bsalomon@google.com15b11df2011-09-16 21:18:29 +0000172 // This subclass must do this before the base class destructor runs
173 // since we will unref the GrGLInterface.
174 this->releaseResources();
reed@google.comac10a2d2010-12-22 21:39:39 +0000175}
176
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000177///////////////////////////////////////////////////////////////////////////////
178
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000179void GrGpuGL::fillInConfigRenderableTable() {
180
181 // OpenGL < 3.0
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000182 // no support for render targets unless the GL_ARB_framebuffer_object
183 // extension is supported (in which case we get ALPHA, RED, RG, RGB,
184 // RGBA (ALPHA8, RGBA4, RGBA8) for OpenGL > 1.1). Note that we
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000185 // probably don't get R8 in this case.
186
187 // OpenGL 3.0
188 // base color renderable: ALPHA, RED, RG, RGB, and RGBA
189 // sized derivatives: ALPHA8, R8, RGBA4, RGBA8
190
191 // >= OpenGL 3.1
192 // base color renderable: RED, RG, RGB, and RGBA
193 // sized derivatives: R8, RGBA4, RGBA8
194 // if the GL_ARB_compatibility extension is supported then we get back
195 // support for GL_ALPHA and ALPHA8
196
197 // GL_EXT_bgra adds BGRA render targets to any version
198
199 // ES 2.0
200 // color renderable: RGBA4, RGB5_A1, RGB565
201 // GL_EXT_texture_rg adds support for R8 as a color render target
202 // GL_OES_rgb8_rgba8 and/or GL_ARM_rgba8 adds support for RGBA8
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000203 // GL_EXT_texture_format_BGRA8888 and/or GL_APPLE_texture_format_BGRA8888 added BGRA support
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000204
commit-bot@chromium.orgc5dffe42013-08-20 15:25:21 +0000205 // ES 3.0
206 // Same as ES 2.0 except R8 and RGBA8 are supported without extensions (the functions called
207 // below already account for this).
208
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000209 if (kDesktop_GrGLBinding == this->glBinding()) {
210 // Post 3.0 we will get R8
211 // Prior to 3.0 we will get ALPHA8 (with GL_ARB_framebuffer_object)
212 if (this->glVersion() >= GR_GL_VER(3,0) ||
213 this->hasExtension("GL_ARB_framebuffer_object")) {
214 fConfigRenderSupport[kAlpha_8_GrPixelConfig] = true;
215 }
216 } else {
217 // On ES we can only hope for R8
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000218 fConfigRenderSupport[kAlpha_8_GrPixelConfig] =
robertphillips@google.com443e5a52012-04-30 20:01:21 +0000219 this->glCaps().textureRedSupport();
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000220 }
221
222 if (kDesktop_GrGLBinding != this->glBinding()) {
223 // only available in ES
224 fConfigRenderSupport[kRGB_565_GrPixelConfig] = true;
225 }
226
reed@google.com6ba45722013-06-21 18:30:53 +0000227 // we no longer support 444 as a render target
228 fConfigRenderSupport[kRGBA_4444_GrPixelConfig] = false;
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000229
230 if (this->glCaps().rgba8RenderbufferSupport()) {
bsalomon@google.com0342a852012-08-20 19:22:38 +0000231 fConfigRenderSupport[kRGBA_8888_GrPixelConfig] = true;
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000232 }
233
234 if (this->glCaps().bgraFormatSupport()) {
bsalomon@google.com0342a852012-08-20 19:22:38 +0000235 fConfigRenderSupport[kBGRA_8888_GrPixelConfig] = true;
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000236 }
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000237}
238
commit-bot@chromium.org28621512013-08-07 19:43:45 +0000239GrPixelConfig GrGpuGL::preferredReadPixelsConfig(GrPixelConfig readConfig,
240 GrPixelConfig surfaceConfig) const {
241 if (GR_GL_RGBA_8888_PIXEL_OPS_SLOW && kRGBA_8888_GrPixelConfig == readConfig) {
bsalomon@google.com9c680582013-02-06 18:17:50 +0000242 return kBGRA_8888_GrPixelConfig;
commit-bot@chromium.org28621512013-08-07 19:43:45 +0000243 } else if (fGLContext.info().isMesa() &&
244 GrBytesPerPixel(readConfig) == 4 &&
245 GrPixelConfigSwapRAndB(readConfig) == surfaceConfig) {
commit-bot@chromium.org5d1d79a2013-05-24 18:52:52 +0000246 // Mesa 3D takes a slow path on when reading back BGRA from an RGBA surface and vice-versa.
247 // Perhaps this should be guarded by some compiletime or runtime check.
248 return surfaceConfig;
commit-bot@chromium.org28621512013-08-07 19:43:45 +0000249 } else if (readConfig == kBGRA_8888_GrPixelConfig &&
250 !this->glCaps().readPixelsSupported(this->glInterface(),
251 GR_GL_BGRA, GR_GL_UNSIGNED_BYTE)) {
252 return kRGBA_8888_GrPixelConfig;
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000253 } else {
commit-bot@chromium.org28621512013-08-07 19:43:45 +0000254 return readConfig;
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000255 }
256}
257
commit-bot@chromium.org5d1d79a2013-05-24 18:52:52 +0000258GrPixelConfig GrGpuGL::preferredWritePixelsConfig(GrPixelConfig writeConfig,
259 GrPixelConfig surfaceConfig) const {
commit-bot@chromium.org28621512013-08-07 19:43:45 +0000260 if (GR_GL_RGBA_8888_PIXEL_OPS_SLOW && kRGBA_8888_GrPixelConfig == writeConfig) {
261 return kBGRA_8888_GrPixelConfig;
262 } else {
263 return writeConfig;
264 }
bsalomon@google.com9c680582013-02-06 18:17:50 +0000265}
266
267bool GrGpuGL::canWriteTexturePixels(const GrTexture* texture, GrPixelConfig srcConfig) const {
268 if (kIndex_8_GrPixelConfig == srcConfig || kIndex_8_GrPixelConfig == texture->config()) {
269 return false;
270 }
bsalomon@google.com791816a2013-08-15 18:54:39 +0000271 if (srcConfig != texture->config() && kES_GrGLBinding == this->glBinding()) {
bsalomon@google.com9c680582013-02-06 18:17:50 +0000272 // In general ES2 requires the internal format of the texture and the format of the src
273 // pixels to match. However, It may or may not be possible to upload BGRA data to a RGBA
274 // texture. It depends upon which extension added BGRA. The Apple extension allows it
275 // (BGRA's internal format is RGBA) while the EXT extension does not (BGRA is its own
276 // internal format).
277 if (this->glCaps().bgraFormatSupport() &&
278 !this->glCaps().bgraIsInternalFormat() &&
279 kBGRA_8888_GrPixelConfig == srcConfig &&
280 kRGBA_8888_GrPixelConfig == texture->config()) {
281 return true;
282 } else {
283 return false;
284 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000285 } else {
bsalomon@google.com9c680582013-02-06 18:17:50 +0000286 return true;
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000287 }
288}
289
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000290bool GrGpuGL::fullReadPixelsIsFasterThanPartial() const {
291 return SkToBool(GR_GL_FULL_READPIXELS_FASTER_THAN_PARTIAL);
292}
293
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000294void GrGpuGL::onResetContext(uint32_t resetBits) {
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000295 // we don't use the zb at all
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000296 if (resetBits & kMisc_GrGLBackendState) {
297 GL_CALL(Disable(GR_GL_DEPTH_TEST));
298 GL_CALL(DepthMask(GR_GL_FALSE));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000299
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000300 fHWDrawFace = GrDrawState::kInvalid_DrawFace;
301 fHWDitherEnabled = kUnknown_TriState;
reed@google.comac10a2d2010-12-22 21:39:39 +0000302
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000303 if (kDesktop_GrGLBinding == this->glBinding()) {
304 // Desktop-only state that we never change
305 if (!this->glCaps().isCoreProfile()) {
306 GL_CALL(Disable(GR_GL_POINT_SMOOTH));
307 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
308 GL_CALL(Disable(GR_GL_POLYGON_SMOOTH));
309 GL_CALL(Disable(GR_GL_POLYGON_STIPPLE));
310 GL_CALL(Disable(GR_GL_COLOR_LOGIC_OP));
311 GL_CALL(Disable(GR_GL_INDEX_LOGIC_OP));
312 }
313 // The windows NVIDIA driver has GL_ARB_imaging in the extension string when using a
314 // core profile. This seems like a bug since the core spec removes any mention of
315 // GL_ARB_imaging.
316 if (this->glCaps().imagingSupport() && !this->glCaps().isCoreProfile()) {
317 GL_CALL(Disable(GR_GL_COLOR_TABLE));
318 }
319 GL_CALL(Disable(GR_GL_POLYGON_OFFSET_FILL));
320 // Since ES doesn't support glPointSize at all we always use the VS to
321 // set the point size
322 GL_CALL(Enable(GR_GL_VERTEX_PROGRAM_POINT_SIZE));
323
324 // We should set glPolygonMode(FRONT_AND_BACK,FILL) here, too. It isn't
325 // currently part of our gl interface. There are probably others as
326 // well.
bsalomon@google.com2b1b8c02013-02-28 22:06:02 +0000327 }
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000328 fHWWriteToColor = kUnknown_TriState;
329 // we only ever use lines in hairline mode
330 GL_CALL(LineWidth(1));
bsalomon@google.comcad107b2013-06-28 14:32:08 +0000331 }
edisonn@google.comba669992013-06-28 16:03:21 +0000332
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000333 if (resetBits & kAA_GrGLBackendState) {
334 fHWAAState.invalidate();
335 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000336
commit-bot@chromium.org46fbfe02013-08-30 15:52:12 +0000337 fHWActiveTextureUnitIdx = -1; // invalid
338
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000339 if (resetBits & kTextureBinding_GrGLBackendState) {
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000340 for (int s = 0; s < fHWBoundTextures.count(); ++s) {
341 fHWBoundTextures[s] = NULL;
342 }
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000343 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000344
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000345 if (resetBits & kBlend_GrGLBackendState) {
346 fHWBlendState.invalidate();
347 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000348
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000349 if (resetBits & kView_GrGLBackendState) {
350 fHWScissorSettings.invalidate();
351 fHWViewport.invalidate();
352 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000353
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000354 if (resetBits & kStencil_GrGLBackendState) {
355 fHWStencilSettings.invalidate();
356 fHWStencilTestEnabled = kUnknown_TriState;
357 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000358
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000359 // Vertex
360 if (resetBits & kVertex_GrGLBackendState) {
361 fHWGeometryState.invalidate();
362 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000363
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000364 if (resetBits & kRenderTarget_GrGLBackendState) {
365 fHWBoundRenderTarget = NULL;
366 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000367
commit-bot@chromium.org3628ad92013-08-30 19:43:47 +0000368 if (resetBits & kFixedFunction_GrGLBackendState && this->glCaps().fixedFunctionSupport()) {
commit-bot@chromium.org46fbfe02013-08-30 15:52:12 +0000369
370 fHWProjectionMatrixState.invalidate();
371 // we don't use the model view matrix.
372 GL_CALL(MatrixMode(GR_GL_MODELVIEW));
373 GL_CALL(LoadIdentity());
374
commit-bot@chromium.org46fbfe02013-08-30 15:52:12 +0000375 for (int i = 0; i < this->glCaps().maxFixedFunctionTextureCoords(); ++i) {
commit-bot@chromium.org46fbfe02013-08-30 15:52:12 +0000376 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + i));
377 GL_CALL(Disable(GR_GL_TEXTURE_GEN_S));
378 GL_CALL(Disable(GR_GL_TEXTURE_GEN_T));
379 GL_CALL(Disable(GR_GL_TEXTURE_GEN_Q));
380 GL_CALL(Disable(GR_GL_TEXTURE_GEN_R));
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000381 if (this->caps()->pathStencilingSupport()) {
382 GL_CALL(PathTexGen(GR_GL_TEXTURE0 + i, GR_GL_NONE, 0, NULL));
383 }
384 fHWTexGenSettings[i].fMode = GR_GL_NONE;
385 fHWTexGenSettings[i].fNumComponents = 0;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000386 }
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000387 fHWActiveTexGenSets = 0;
bsalomon@google.com05a718c2012-06-29 14:01:53 +0000388 }
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000389
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000390 // we assume these values
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000391 if (resetBits & kPixelStore_GrGLBackendState) {
392 if (this->glCaps().unpackRowLengthSupport()) {
393 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
394 }
395 if (this->glCaps().packRowLengthSupport()) {
396 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
397 }
398 if (this->glCaps().unpackFlipYSupport()) {
399 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
400 }
401 if (this->glCaps().packFlipYSupport()) {
402 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, GR_GL_FALSE));
403 }
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000404 }
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000405
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000406 if (resetBits & kProgram_GrGLBackendState) {
407 fHWProgramID = 0;
408 fSharedGLProgramState.invalidate();
409 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000410}
411
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000412namespace {
413
414GrSurfaceOrigin resolve_origin(GrSurfaceOrigin origin, bool renderTarget) {
415 // By default, GrRenderTargets are GL's normal orientation so that they
416 // can be drawn to by the outside world without the client having
417 // to render upside down.
418 if (kDefault_GrSurfaceOrigin == origin) {
419 return renderTarget ? kBottomLeft_GrSurfaceOrigin : kTopLeft_GrSurfaceOrigin;
420 } else {
421 return origin;
422 }
423}
424
425}
426
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000427GrTexture* GrGpuGL::onWrapBackendTexture(const GrBackendTextureDesc& desc) {
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000428 if (!this->configToGLFormats(desc.fConfig, false, NULL, NULL, NULL)) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000429 return NULL;
430 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000431
robertphillips@google.comb72e5d32012-10-30 15:18:10 +0000432 if (0 == desc.fTextureHandle) {
433 return NULL;
434 }
435
bsalomon@google.combcce8922013-03-25 15:38:39 +0000436 int maxSize = this->caps()->maxTextureSize();
robertphillips@google.comb72e5d32012-10-30 15:18:10 +0000437 if (desc.fWidth > maxSize || desc.fHeight > maxSize) {
438 return NULL;
439 }
440
441 GrGLTexture::Desc glTexDesc;
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000442 // next line relies on GrBackendTextureDesc's flags matching GrTexture's
robertphillips@google.com32716282012-06-04 12:48:45 +0000443 glTexDesc.fFlags = (GrTextureFlags) desc.fFlags;
bsalomon@google.com99621082011-11-15 16:47:16 +0000444 glTexDesc.fWidth = desc.fWidth;
445 glTexDesc.fHeight = desc.fHeight;
bsalomon@google.come269f212011-11-07 13:29:52 +0000446 glTexDesc.fConfig = desc.fConfig;
robertphillips@google.com32716282012-06-04 12:48:45 +0000447 glTexDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.come269f212011-11-07 13:29:52 +0000448 glTexDesc.fTextureID = static_cast<GrGLuint>(desc.fTextureHandle);
bsalomon@google.com72830222013-01-23 20:25:22 +0000449 glTexDesc.fIsWrapped = true;
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000450 bool renderTarget = SkToBool(desc.fFlags & kRenderTarget_GrBackendTextureFlag);
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000451 // FIXME: this should be calling resolve_origin(), but Chrome code is currently
452 // assuming the old behaviour, which is that backend textures are always
453 // BottomLeft, even for non-RT's. Once Chrome is fixed, change this to:
454 // glTexDesc.fOrigin = resolve_origin(desc.fOrigin, renderTarget);
455 if (kDefault_GrSurfaceOrigin == desc.fOrigin) {
456 glTexDesc.fOrigin = kBottomLeft_GrSurfaceOrigin;
457 } else {
458 glTexDesc.fOrigin = desc.fOrigin;
459 }
bsalomon@google.come269f212011-11-07 13:29:52 +0000460
461 GrGLTexture* texture = NULL;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000462 if (renderTarget) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000463 GrGLRenderTarget::Desc glRTDesc;
464 glRTDesc.fRTFBOID = 0;
465 glRTDesc.fTexFBOID = 0;
466 glRTDesc.fMSColorRenderbufferID = 0;
bsalomon@google.come269f212011-11-07 13:29:52 +0000467 glRTDesc.fConfig = desc.fConfig;
468 glRTDesc.fSampleCnt = desc.fSampleCnt;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000469 glRTDesc.fOrigin = glTexDesc.fOrigin;
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000470 glRTDesc.fCheckAllocation = false;
bsalomon@google.com99621082011-11-15 16:47:16 +0000471 if (!this->createRenderTargetObjects(glTexDesc.fWidth,
472 glTexDesc.fHeight,
bsalomon@google.come269f212011-11-07 13:29:52 +0000473 glTexDesc.fTextureID,
474 &glRTDesc)) {
475 return NULL;
476 }
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000477 texture = SkNEW_ARGS(GrGLTexture, (this, glTexDesc, glRTDesc));
bsalomon@google.come269f212011-11-07 13:29:52 +0000478 } else {
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000479 texture = SkNEW_ARGS(GrGLTexture, (this, glTexDesc));
bsalomon@google.come269f212011-11-07 13:29:52 +0000480 }
481 if (NULL == texture) {
482 return NULL;
483 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000484
bsalomon@google.come269f212011-11-07 13:29:52 +0000485 return texture;
486}
487
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000488GrRenderTarget* GrGpuGL::onWrapBackendRenderTarget(const GrBackendRenderTargetDesc& desc) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000489 GrGLRenderTarget::Desc glDesc;
490 glDesc.fConfig = desc.fConfig;
491 glDesc.fRTFBOID = static_cast<GrGLuint>(desc.fRenderTargetHandle);
492 glDesc.fMSColorRenderbufferID = 0;
493 glDesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
494 glDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.com72830222013-01-23 20:25:22 +0000495 glDesc.fIsWrapped = true;
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000496 glDesc.fCheckAllocation = false;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000497
498 glDesc.fOrigin = resolve_origin(desc.fOrigin, true);
bsalomon@google.come269f212011-11-07 13:29:52 +0000499 GrGLIRect viewport;
500 viewport.fLeft = 0;
501 viewport.fBottom = 0;
502 viewport.fWidth = desc.fWidth;
503 viewport.fHeight = desc.fHeight;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000504
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000505 GrRenderTarget* tgt = SkNEW_ARGS(GrGLRenderTarget,
506 (this, glDesc, viewport));
bsalomon@google.come269f212011-11-07 13:29:52 +0000507 if (desc.fStencilBits) {
508 GrGLStencilBuffer::Format format;
509 format.fInternalFormat = GrGLStencilBuffer::kUnknownInternalFormat;
510 format.fPacked = false;
511 format.fStencilBits = desc.fStencilBits;
512 format.fTotalBits = desc.fStencilBits;
bsalomon@google.com1f0f1a32013-01-23 21:32:32 +0000513 static const bool kIsSBWrapped = false;
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000514 GrGLStencilBuffer* sb = SkNEW_ARGS(GrGLStencilBuffer,
515 (this,
bsalomon@google.com1f0f1a32013-01-23 21:32:32 +0000516 kIsSBWrapped,
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000517 0,
518 desc.fWidth,
519 desc.fHeight,
520 desc.fSampleCnt,
521 format));
bsalomon@google.come269f212011-11-07 13:29:52 +0000522 tgt->setStencilBuffer(sb);
523 sb->unref();
524 }
525 return tgt;
526}
527
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000528////////////////////////////////////////////////////////////////////////////////
529
bsalomon@google.com9c680582013-02-06 18:17:50 +0000530bool GrGpuGL::onWriteTexturePixels(GrTexture* texture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000531 int left, int top, int width, int height,
532 GrPixelConfig config, const void* buffer,
533 size_t rowBytes) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000534 if (NULL == buffer) {
bsalomon@google.com9c680582013-02-06 18:17:50 +0000535 return false;
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000536 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000537 GrGLTexture* glTex = static_cast<GrGLTexture*>(texture);
538
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +0000539 this->setScratchTextureUnit();
bsalomon@google.com6f379512011-11-16 20:36:03 +0000540 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTex->textureID()));
541 GrGLTexture::Desc desc;
robertphillips@google.com32716282012-06-04 12:48:45 +0000542 desc.fFlags = glTex->desc().fFlags;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000543 desc.fWidth = glTex->width();
544 desc.fHeight = glTex->height();
robertphillips@google.com32716282012-06-04 12:48:45 +0000545 desc.fConfig = glTex->config();
546 desc.fSampleCnt = glTex->desc().fSampleCnt;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000547 desc.fTextureID = glTex->textureID();
bsalomon@google.com2d0bade2012-10-26 19:01:17 +0000548 desc.fOrigin = glTex->origin();
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000549
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +0000550 if (this->uploadTexData(desc, false,
551 left, top, width, height,
552 config, buffer, rowBytes)) {
553 texture->dirtyMipMaps(true);
554 return true;
555 } else {
556 return false;
557 }
bsalomon@google.com6f379512011-11-16 20:36:03 +0000558}
559
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000560namespace {
561bool adjust_pixel_ops_params(int surfaceWidth,
562 int surfaceHeight,
563 size_t bpp,
564 int* left, int* top, int* width, int* height,
565 const void** data,
566 size_t* rowBytes) {
567 if (!*rowBytes) {
568 *rowBytes = *width * bpp;
569 }
570
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000571 SkIRect subRect = SkIRect::MakeXYWH(*left, *top, *width, *height);
572 SkIRect bounds = SkIRect::MakeWH(surfaceWidth, surfaceHeight);
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000573
574 if (!subRect.intersect(bounds)) {
575 return false;
576 }
577 *data = reinterpret_cast<const void*>(reinterpret_cast<intptr_t>(*data) +
578 (subRect.fTop - *top) * *rowBytes + (subRect.fLeft - *left) * bpp);
579
580 *left = subRect.fLeft;
581 *top = subRect.fTop;
582 *width = subRect.width();
583 *height = subRect.height();
584 return true;
585}
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000586
587GrGLenum check_alloc_error(const GrTextureDesc& desc, const GrGLInterface* interface) {
588 if (SkToBool(desc.fFlags & kCheckAllocation_GrTextureFlagBit)) {
589 return GR_GL_GET_ERROR(interface);
590 } else {
591 return CHECK_ALLOC_ERROR(interface);
592 }
593}
594
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000595}
596
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000597bool GrGpuGL::uploadTexData(const GrGLTexture::Desc& desc,
598 bool isNewTexture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000599 int left, int top, int width, int height,
600 GrPixelConfig dataConfig,
601 const void* data,
602 size_t rowBytes) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000603 SkASSERT(NULL != data || isNewTexture);
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000604
605 size_t bpp = GrBytesPerPixel(dataConfig);
606 if (!adjust_pixel_ops_params(desc.fWidth, desc.fHeight, bpp, &left, &top,
607 &width, &height, &data, &rowBytes)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000608 return false;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000609 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000610 size_t trimRowBytes = width * bpp;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000611
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000612 // in case we need a temporary, trimmed copy of the src pixels
613 SkAutoSMalloc<128 * 128> tempStorage;
614
bsalomon@google.com313f0192012-07-10 17:21:02 +0000615 // paletted textures cannot be partially updated
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000616 bool useTexStorage = isNewTexture &&
bsalomon@google.com313f0192012-07-10 17:21:02 +0000617 desc.fConfig != kIndex_8_GrPixelConfig &&
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000618 this->glCaps().texStorageSupport();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000619
bsalomon@google.com313f0192012-07-10 17:21:02 +0000620 if (useTexStorage && kDesktop_GrGLBinding == this->glBinding()) {
621 // 565 is not a sized internal format on desktop GL. So on desktop with
622 // 565 we always use an unsized internal format to let the system pick
623 // the best sized format to convert the 565 data to. Since TexStorage
624 // only allows sized internal formats we will instead use TexImage2D.
625 useTexStorage = desc.fConfig != kRGB_565_GrPixelConfig;
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000626 }
627
628 GrGLenum internalFormat;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000629 GrGLenum externalFormat;
630 GrGLenum externalType;
commit-bot@chromium.org87002952013-08-20 15:12:01 +0000631 // glTexStorage requires sized internal formats on both desktop and ES. ES2 requires an unsized
632 // format for glTexImage, unlike ES3 and desktop. However, we allow the driver to decide the
633 // size of the internal format whenever possible and so only use a sized internal format when
634 // using texture storage.
bsalomon@google.com3323c4d2013-08-20 13:48:33 +0000635 if (!this->configToGLFormats(dataConfig, useTexStorage, &internalFormat,
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000636 &externalFormat, &externalType)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000637 return false;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000638 }
639
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000640 if (!isNewTexture && GR_GL_PALETTE8_RGBA8 == internalFormat) {
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000641 // paletted textures cannot be updated
642 return false;
643 }
644
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000645 /*
bsalomon@google.com6f379512011-11-16 20:36:03 +0000646 * check whether to allocate a temporary buffer for flipping y or
647 * because our srcData has extra bytes past each row. If so, we need
648 * to trim those off here, since GL ES may not let us specify
649 * GL_UNPACK_ROW_LENGTH.
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000650 */
bsalomon@google.com6f379512011-11-16 20:36:03 +0000651 bool restoreGLRowLength = false;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000652 bool swFlipY = false;
653 bool glFlipY = false;
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000654 if (NULL != data) {
senorblanco@chromium.orgef5dbe12013-01-28 16:42:38 +0000655 if (kBottomLeft_GrSurfaceOrigin == desc.fOrigin) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000656 if (this->glCaps().unpackFlipYSupport()) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000657 glFlipY = true;
658 } else {
659 swFlipY = true;
660 }
661 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000662 if (this->glCaps().unpackRowLengthSupport() && !swFlipY) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000663 // can't use this for flipping, only non-neg values allowed. :(
664 if (rowBytes != trimRowBytes) {
665 GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
666 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength));
667 restoreGLRowLength = true;
668 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000669 } else {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000670 if (trimRowBytes != rowBytes || swFlipY) {
671 // copy data into our new storage, skipping the trailing bytes
672 size_t trimSize = height * trimRowBytes;
673 const char* src = (const char*)data;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000674 if (swFlipY) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000675 src += (height - 1) * rowBytes;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000676 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000677 char* dst = (char*)tempStorage.reset(trimSize);
678 for (int y = 0; y < height; y++) {
679 memcpy(dst, src, trimRowBytes);
680 if (swFlipY) {
681 src -= rowBytes;
682 } else {
683 src += rowBytes;
684 }
685 dst += trimRowBytes;
686 }
687 // now point data to our copied version
688 data = tempStorage.get();
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000689 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000690 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000691 if (glFlipY) {
692 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_TRUE));
693 }
694 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, static_cast<GrGLint>(bpp)));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000695 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000696 bool succeeded = true;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000697 if (isNewTexture &&
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000698 0 == left && 0 == top &&
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000699 desc.fWidth == width && desc.fHeight == height) {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000700 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000701 if (useTexStorage) {
702 // We never resize or change formats of textures. We don't use
703 // mipmaps currently.
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000704 GL_ALLOC_CALL(this->glInterface(),
705 TexStorage2D(GR_GL_TEXTURE_2D,
706 1, // levels
707 internalFormat,
708 desc.fWidth, desc.fHeight));
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000709 } else {
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000710 if (GR_GL_PALETTE8_RGBA8 == internalFormat) {
711 GrGLsizei imageSize = desc.fWidth * desc.fHeight +
712 kGrColorTableSize;
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000713 GL_ALLOC_CALL(this->glInterface(),
714 CompressedTexImage2D(GR_GL_TEXTURE_2D,
715 0, // level
716 internalFormat,
717 desc.fWidth, desc.fHeight,
718 0, // border
719 imageSize,
720 data));
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000721 } else {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000722 GL_ALLOC_CALL(this->glInterface(),
723 TexImage2D(GR_GL_TEXTURE_2D,
724 0, // level
725 internalFormat,
726 desc.fWidth, desc.fHeight,
727 0, // border
728 externalFormat, externalType,
729 data));
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000730 }
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000731 }
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000732 GrGLenum error = check_alloc_error(desc, this->glInterface());
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000733 if (error != GR_GL_NO_ERROR) {
734 succeeded = false;
735 } else {
736 // if we have data and we used TexStorage to create the texture, we
737 // now upload with TexSubImage.
738 if (NULL != data && useTexStorage) {
739 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D,
740 0, // level
741 left, top,
742 width, height,
743 externalFormat, externalType,
744 data));
745 }
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000746 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000747 } else {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000748 if (swFlipY || glFlipY) {
bsalomon@google.com6f379512011-11-16 20:36:03 +0000749 top = desc.fHeight - (top + height);
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000750 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000751 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D,
752 0, // level
753 left, top,
754 width, height,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000755 externalFormat, externalType, data));
756 }
757
758 if (restoreGLRowLength) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000759 SkASSERT(this->glCaps().unpackRowLengthSupport());
bsalomon@google.com6f379512011-11-16 20:36:03 +0000760 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000761 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000762 if (glFlipY) {
763 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
764 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000765 return succeeded;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000766}
767
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +0000768static bool renderbuffer_storage_msaa(GrGLContext& ctx,
769 int sampleCount,
770 GrGLenum format,
771 int width, int height) {
robertphillips@google.com6177e692013-02-28 20:16:25 +0000772 CLEAR_ERROR_BEFORE_ALLOC(ctx.interface());
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000773 SkASSERT(GrGLCaps::kNone_MSFBOType != ctx.info().caps()->msFBOType());
commit-bot@chromium.orga8e5a062013-09-05 23:44:09 +0000774#if GR_GL_IGNORE_ES3_MSAA
robertphillips@google.com6177e692013-02-28 20:16:25 +0000775 GL_ALLOC_CALL(ctx.interface(),
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000776 RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
777 sampleCount,
778 format,
779 width, height));
commit-bot@chromium.orga8e5a062013-09-05 23:44:09 +0000780#else
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +0000781 switch (ctx.info().caps()->msFBOType()) {
782 case GrGLCaps::kDesktop_ARB_MSFBOType:
783 case GrGLCaps::kDesktop_EXT_MSFBOType:
784 case GrGLCaps::kES_3_0_MSFBOType:
785 GL_ALLOC_CALL(ctx.interface(),
786 RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
787 sampleCount,
788 format,
789 width, height));
790 break;
791 case GrGLCaps::kES_Apple_MSFBOType:
792 GL_ALLOC_CALL(ctx.interface(),
793 RenderbufferStorageMultisampleES2APPLE(GR_GL_RENDERBUFFER,
794 sampleCount,
795 format,
796 width, height));
797 break;
798 case GrGLCaps::kES_EXT_MsToTexture_MSFBOType:
799 case GrGLCaps::kES_IMG_MsToTexture_MSFBOType:
800 GL_ALLOC_CALL(ctx.interface(),
801 RenderbufferStorageMultisampleES2EXT(GR_GL_RENDERBUFFER,
802 sampleCount,
803 format,
804 width, height));
805 break;
806 case GrGLCaps::kNone_MSFBOType:
807 GrCrash("Shouldn't be here if we don't support multisampled renderbuffers.");
808 break;
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000809 }
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +0000810#endif
811 return (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctx.interface()));;
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000812}
813
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000814bool GrGpuGL::createRenderTargetObjects(int width, int height,
815 GrGLuint texID,
816 GrGLRenderTarget::Desc* desc) {
817 desc->fMSColorRenderbufferID = 0;
818 desc->fRTFBOID = 0;
819 desc->fTexFBOID = 0;
bsalomon@google.com72830222013-01-23 20:25:22 +0000820 desc->fIsWrapped = false;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000821
822 GrGLenum status;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000823
bsalomon@google.comab15d612011-08-09 12:57:56 +0000824 GrGLenum msColorFormat = 0; // suppress warning
825
bsalomon@google.com347c3822013-05-01 20:10:01 +0000826 if (desc->fSampleCnt > 0 && GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType()) {
827 goto FAILED;
828 }
829
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000830 GL_CALL(GenFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000831 if (!desc->fTexFBOID) {
832 goto FAILED;
833 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000834
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000835
bsalomon@google.comf3a60c02013-03-19 19:06:09 +0000836 // If we are using multisampling we will create two FBOS. We render to one and then resolve to
837 // the texture bound to the other. The exception is the IMG multisample extension. With this
838 // extension the texture is multisampled when rendered to and then auto-resolves it when it is
839 // rendered from.
bsalomon@google.com347c3822013-05-01 20:10:01 +0000840 if (desc->fSampleCnt > 0 && this->glCaps().usesMSAARenderBuffers()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000841 GL_CALL(GenFramebuffers(1, &desc->fRTFBOID));
842 GL_CALL(GenRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000843 if (!desc->fRTFBOID ||
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000844 !desc->fMSColorRenderbufferID ||
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000845 !this->configToGLFormats(desc->fConfig,
commit-bot@chromium.org87002952013-08-20 15:12:01 +0000846 // ES2 and ES3 require sized internal formats for rb storage.
bsalomon@google.com791816a2013-08-15 18:54:39 +0000847 kES_GrGLBinding == this->glBinding(),
bsalomon@google.com347c3822013-05-01 20:10:01 +0000848 &msColorFormat,
849 NULL,
850 NULL)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000851 goto FAILED;
852 }
853 } else {
854 desc->fRTFBOID = desc->fTexFBOID;
855 }
856
bsalomon@google.com0e9b41a2012-01-04 22:11:43 +0000857 // below here we may bind the FBO
bsalomon@google.comc811ea32012-05-21 15:33:09 +0000858 fHWBoundRenderTarget = NULL;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000859 if (desc->fRTFBOID != desc->fTexFBOID) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000860 SkASSERT(desc->fSampleCnt > 0);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000861 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000862 desc->fMSColorRenderbufferID));
robertphillips@google.com6177e692013-02-28 20:16:25 +0000863 if (!renderbuffer_storage_msaa(fGLContext,
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000864 desc->fSampleCnt,
865 msColorFormat,
866 width, height)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000867 goto FAILED;
868 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000869 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fRTFBOID));
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000870 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000871 GR_GL_COLOR_ATTACHMENT0,
872 GR_GL_RENDERBUFFER,
873 desc->fMSColorRenderbufferID));
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000874 if (desc->fCheckAllocation ||
875 !this->glCaps().isConfigVerifiedColorAttachment(desc->fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000876 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
877 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
878 goto FAILED;
879 }
bsalomon@google.combcce8922013-03-25 15:38:39 +0000880 fGLContext.info().caps()->markConfigAsValidColorAttachment(desc->fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000881 }
882 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000883 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000884
bsalomon@google.com347c3822013-05-01 20:10:01 +0000885 if (this->glCaps().usesImplicitMSAAResolve() && desc->fSampleCnt > 0) {
bsalomon@google.comf3a60c02013-03-19 19:06:09 +0000886 GL_CALL(FramebufferTexture2DMultisample(GR_GL_FRAMEBUFFER,
887 GR_GL_COLOR_ATTACHMENT0,
888 GR_GL_TEXTURE_2D,
889 texID, 0, desc->fSampleCnt));
890 } else {
891 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
892 GR_GL_COLOR_ATTACHMENT0,
893 GR_GL_TEXTURE_2D,
894 texID, 0));
895 }
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000896 if (desc->fCheckAllocation ||
897 !this->glCaps().isConfigVerifiedColorAttachment(desc->fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000898 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
899 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
900 goto FAILED;
901 }
bsalomon@google.combcce8922013-03-25 15:38:39 +0000902 fGLContext.info().caps()->markConfigAsValidColorAttachment(desc->fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000903 }
904
905 return true;
906
907FAILED:
908 if (desc->fMSColorRenderbufferID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000909 GL_CALL(DeleteRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000910 }
911 if (desc->fRTFBOID != desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000912 GL_CALL(DeleteFramebuffers(1, &desc->fRTFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000913 }
914 if (desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000915 GL_CALL(DeleteFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000916 }
917 return false;
918}
919
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000920// good to set a break-point here to know when createTexture fails
921static GrTexture* return_null_texture() {
mtklein@google.com330313a2013-08-22 15:37:26 +0000922// SkDEBUGFAIL("null texture");
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000923 return NULL;
924}
925
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000926#if 0 && defined(SK_DEBUG)
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000927static size_t as_size_t(int x) {
928 return x;
929}
930#endif
931
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000932GrTexture* GrGpuGL::onCreateTexture(const GrTextureDesc& desc,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000933 const void* srcData,
934 size_t rowBytes) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000935
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000936 GrGLTexture::Desc glTexDesc;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000937 GrGLRenderTarget::Desc glRTDesc;
reed@google.comac10a2d2010-12-22 21:39:39 +0000938
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000939 // Attempt to catch un- or wrongly initialized sample counts;
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000940 SkASSERT(desc.fSampleCnt >= 0 && desc.fSampleCnt <= 64);
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000941 // We fail if the MSAA was requested and is not available.
942 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType() && desc.fSampleCnt) {
943 //GrPrintf("MSAA RT requested but not supported on this platform.");
944 return return_null_texture();
945 }
946 // If the sample count exceeds the max then we clamp it.
bsalomon@google.combcce8922013-03-25 15:38:39 +0000947 glTexDesc.fSampleCnt = GrMin(desc.fSampleCnt, this->caps()->maxSampleCount());
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000948
robertphillips@google.com32716282012-06-04 12:48:45 +0000949 glTexDesc.fFlags = desc.fFlags;
bsalomon@google.com99621082011-11-15 16:47:16 +0000950 glTexDesc.fWidth = desc.fWidth;
951 glTexDesc.fHeight = desc.fHeight;
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000952 glTexDesc.fConfig = desc.fConfig;
bsalomon@google.com72830222013-01-23 20:25:22 +0000953 glTexDesc.fIsWrapped = false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000954
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000955 glRTDesc.fMSColorRenderbufferID = 0;
956 glRTDesc.fRTFBOID = 0;
957 glRTDesc.fTexFBOID = 0;
bsalomon@google.com72830222013-01-23 20:25:22 +0000958 glRTDesc.fIsWrapped = false;
bsalomon@google.com64c4fe42011-11-05 14:51:01 +0000959 glRTDesc.fConfig = glTexDesc.fConfig;
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000960 glRTDesc.fCheckAllocation = SkToBool(desc.fFlags & kCheckAllocation_GrTextureFlagBit);
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000961
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000962 bool renderTarget = SkToBool(desc.fFlags & kRenderTarget_GrTextureFlagBit);
reed@google.comac10a2d2010-12-22 21:39:39 +0000963
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000964 glTexDesc.fOrigin = resolve_origin(desc.fOrigin, renderTarget);
965 glRTDesc.fOrigin = glTexDesc.fOrigin;
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000966
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000967 glRTDesc.fSampleCnt = glTexDesc.fSampleCnt;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000968 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType() &&
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000969 desc.fSampleCnt) {
bsalomon@google.com945bbe12012-06-15 14:30:34 +0000970 //GrPrintf("MSAA RT requested but not supported on this platform.");
971 return return_null_texture();
reed@google.comac10a2d2010-12-22 21:39:39 +0000972 }
973
reed@google.comac10a2d2010-12-22 21:39:39 +0000974 if (renderTarget) {
bsalomon@google.combcce8922013-03-25 15:38:39 +0000975 int maxRTSize = this->caps()->maxRenderTargetSize();
976 if (glTexDesc.fWidth > maxRTSize || glTexDesc.fHeight > maxRTSize) {
bsalomon@google.com91958362011-06-13 17:58:13 +0000977 return return_null_texture();
978 }
sugoi@google.com6ba0b0f2013-05-03 13:52:32 +0000979 } else {
980 int maxSize = this->caps()->maxTextureSize();
981 if (glTexDesc.fWidth > maxSize || glTexDesc.fHeight > maxSize) {
982 return return_null_texture();
983 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000984 }
985
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000986 GL_CALL(GenTextures(1, &glTexDesc.fTextureID));
junov@chromium.org8b6b1c92013-08-29 16:22:09 +0000987
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000988 if (!glTexDesc.fTextureID) {
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000989 return return_null_texture();
990 }
991
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +0000992 this->setScratchTextureUnit();
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000993 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTexDesc.fTextureID));
bsalomon@google.come269f212011-11-07 13:29:52 +0000994
junov@chromium.org8b6b1c92013-08-29 16:22:09 +0000995 if (renderTarget && this->glCaps().textureUsageSupport()) {
996 // provides a hint about how this texture will be used
997 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
998 GR_GL_TEXTURE_USAGE,
999 GR_GL_FRAMEBUFFER_ATTACHMENT));
1000 }
1001
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001002 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
1003 // drivers have a bug where an FBO won't be complete if it includes a
1004 // texture that is not mipmap complete (considering the filter in use).
1005 GrGLTexture::TexParams initialTexParams;
1006 // we only set a subset here so invalidate first
1007 initialTexParams.invalidate();
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00001008 initialTexParams.fMinFilter = GR_GL_NEAREST;
1009 initialTexParams.fMagFilter = GR_GL_NEAREST;
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001010 initialTexParams.fWrapS = GR_GL_CLAMP_TO_EDGE;
1011 initialTexParams.fWrapT = GR_GL_CLAMP_TO_EDGE;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001012 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1013 GR_GL_TEXTURE_MAG_FILTER,
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00001014 initialTexParams.fMagFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001015 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1016 GR_GL_TEXTURE_MIN_FILTER,
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00001017 initialTexParams.fMinFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001018 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1019 GR_GL_TEXTURE_WRAP_S,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001020 initialTexParams.fWrapS));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001021 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1022 GR_GL_TEXTURE_WRAP_T,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001023 initialTexParams.fWrapT));
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001024 if (!this->uploadTexData(glTexDesc, true, 0, 0,
1025 glTexDesc.fWidth, glTexDesc.fHeight,
1026 desc.fConfig, srcData, rowBytes)) {
1027 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
1028 return return_null_texture();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001029 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001030
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001031 GrGLTexture* tex;
reed@google.comac10a2d2010-12-22 21:39:39 +00001032 if (renderTarget) {
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001033 // unbind the texture from the texture unit before binding it to the frame buffer
1034 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, 0));
1035
bsalomon@google.com99621082011-11-15 16:47:16 +00001036 if (!this->createRenderTargetObjects(glTexDesc.fWidth,
1037 glTexDesc.fHeight,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001038 glTexDesc.fTextureID,
1039 &glRTDesc)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001040 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001041 return return_null_texture();
1042 }
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001043 tex = SkNEW_ARGS(GrGLTexture, (this, glTexDesc, glRTDesc));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001044 } else {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001045 tex = SkNEW_ARGS(GrGLTexture, (this, glTexDesc));
reed@google.comac10a2d2010-12-22 21:39:39 +00001046 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001047 tex->setCachedTexParams(initialTexParams, this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00001048#ifdef TRACE_TEXTURE_CREATION
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001049 GrPrintf("--- new texture [%d] size=(%d %d) config=%d\n",
1050 glTexDesc.fTextureID, desc.fWidth, desc.fHeight, desc.fConfig);
reed@google.comac10a2d2010-12-22 21:39:39 +00001051#endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001052 return tex;
1053}
1054
1055namespace {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001056
1057const GrGLuint kUnknownBitCount = GrGLStencilBuffer::kUnknownBitCount;
1058
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001059void inline get_stencil_rb_sizes(const GrGLInterface* gl,
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001060 GrGLStencilBuffer::Format* format) {
sugoi@google.com6ba0b0f2013-05-03 13:52:32 +00001061
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001062 // we shouldn't ever know one size and not the other
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001063 SkASSERT((kUnknownBitCount == format->fStencilBits) ==
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001064 (kUnknownBitCount == format->fTotalBits));
1065 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001066 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001067 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1068 (GrGLint*)&format->fStencilBits);
1069 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001070 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001071 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1072 (GrGLint*)&format->fTotalBits);
1073 format->fTotalBits += format->fStencilBits;
1074 } else {
1075 format->fTotalBits = format->fStencilBits;
1076 }
1077 }
1078}
1079}
1080
1081bool GrGpuGL::createStencilBufferForRenderTarget(GrRenderTarget* rt,
1082 int width, int height) {
1083
1084 // All internally created RTs are also textures. We don't create
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +00001085 // 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 +00001086 SkASSERT(rt->asTexture());
1087 SkASSERT(width >= rt->width());
1088 SkASSERT(height >= rt->height());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001089
1090 int samples = rt->numSamples();
1091 GrGLuint sbID;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001092 GL_CALL(GenRenderbuffers(1, &sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001093 if (!sbID) {
1094 return false;
1095 }
1096
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001097 int stencilFmtCnt = this->glCaps().stencilFormats().count();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001098 for (int i = 0; i < stencilFmtCnt; ++i) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001099 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001100 // we start with the last stencil format that succeeded in hopes
1101 // that we won't go through this loop more than once after the
1102 // first (painful) stencil creation.
1103 int sIdx = (i + fLastSuccessfulStencilFmtIdx) % stencilFmtCnt;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001104 const GrGLCaps::StencilFormat& sFmt =
1105 this->glCaps().stencilFormats()[sIdx];
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001106 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001107 // we do this "if" so that we don't call the multisample
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001108 // version on a GL that doesn't have an MSAA extension.
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001109 bool created;
1110 if (samples > 0) {
robertphillips@google.com6177e692013-02-28 20:16:25 +00001111 created = renderbuffer_storage_msaa(fGLContext,
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001112 samples,
1113 sFmt.fInternalFormat,
1114 width, height);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001115 } else {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001116 GL_ALLOC_CALL(this->glInterface(),
1117 RenderbufferStorage(GR_GL_RENDERBUFFER,
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001118 sFmt.fInternalFormat,
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001119 width, height));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001120 created =
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +00001121 (GR_GL_NO_ERROR == check_alloc_error(rt->desc(), this->glInterface()));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001122 }
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001123 if (created) {
1124 // After sized formats we attempt an unsized format and take
1125 // whatever sizes GL gives us. In that case we query for the size.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001126 GrGLStencilBuffer::Format format = sFmt;
sugoi@google.com6ba0b0f2013-05-03 13:52:32 +00001127 get_stencil_rb_sizes(this->glInterface(), &format);
bsalomon@google.com72830222013-01-23 20:25:22 +00001128 static const bool kIsWrapped = false;
robertphillips@google.comf2e93fc2012-09-05 19:44:18 +00001129 SkAutoTUnref<GrStencilBuffer> sb(SkNEW_ARGS(GrGLStencilBuffer,
bsalomon@google.com72830222013-01-23 20:25:22 +00001130 (this, kIsWrapped, sbID, width, height,
robertphillips@google.comf2e93fc2012-09-05 19:44:18 +00001131 samples, format)));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001132 if (this->attachStencilBufferToRenderTarget(sb, rt)) {
1133 fLastSuccessfulStencilFmtIdx = sIdx;
robertphillips@google.com9fbcad02012-09-09 14:44:15 +00001134 sb->transferToCache();
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001135 rt->setStencilBuffer(sb);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001136 return true;
1137 }
1138 sb->abandon(); // otherwise we lose sbID
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001139 }
1140 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001141 GL_CALL(DeleteRenderbuffers(1, &sbID));
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001142 return false;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001143}
1144
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001145bool GrGpuGL::attachStencilBufferToRenderTarget(GrStencilBuffer* sb, GrRenderTarget* rt) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001146 GrGLRenderTarget* glrt = (GrGLRenderTarget*) rt;
1147
1148 GrGLuint fbo = glrt->renderFBOID();
1149
1150 if (NULL == sb) {
1151 if (NULL != rt->getStencilBuffer()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001152 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001153 GR_GL_STENCIL_ATTACHMENT,
1154 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001155 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001156 GR_GL_DEPTH_ATTACHMENT,
1157 GR_GL_RENDERBUFFER, 0));
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +00001158#ifdef SK_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001159 GrGLenum status;
1160 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001161 SkASSERT(GR_GL_FRAMEBUFFER_COMPLETE == status);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001162#endif
1163 }
1164 return true;
1165 } else {
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001166 GrGLStencilBuffer* glsb = static_cast<GrGLStencilBuffer*>(sb);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001167 GrGLuint rb = glsb->renderbufferID();
1168
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001169 fHWBoundRenderTarget = NULL;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001170 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, fbo));
1171 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001172 GR_GL_STENCIL_ATTACHMENT,
1173 GR_GL_RENDERBUFFER, rb));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001174 if (glsb->format().fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001175 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001176 GR_GL_DEPTH_ATTACHMENT,
1177 GR_GL_RENDERBUFFER, rb));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001178 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001179 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001180 GR_GL_DEPTH_ATTACHMENT,
1181 GR_GL_RENDERBUFFER, 0));
reed@google.comac10a2d2010-12-22 21:39:39 +00001182 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001183
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001184 GrGLenum status;
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001185 if (!this->glCaps().isColorConfigAndStencilFormatVerified(rt->config(), glsb->format())) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001186 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1187 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001188 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001189 GR_GL_STENCIL_ATTACHMENT,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001190 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001191 if (glsb->format().fPacked) {
1192 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1193 GR_GL_DEPTH_ATTACHMENT,
1194 GR_GL_RENDERBUFFER, 0));
1195 }
1196 return false;
1197 } else {
bsalomon@google.combcce8922013-03-25 15:38:39 +00001198 fGLContext.info().caps()->markColorConfigAndStencilFormatAsVerified(
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001199 rt->config(),
1200 glsb->format());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001201 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001202 }
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001203 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001204 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001205}
1206
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001207////////////////////////////////////////////////////////////////////////////////
1208
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001209GrVertexBuffer* GrGpuGL::onCreateVertexBuffer(uint32_t size, bool dynamic) {
bsalomon@google.come49ad452013-02-20 19:33:20 +00001210 GrGLVertexBuffer::Desc desc;
1211 desc.fDynamic = dynamic;
1212 desc.fSizeInBytes = size;
1213 desc.fIsWrapped = false;
1214
bsalomon@google.com96966a52013-02-21 16:34:21 +00001215 if (this->glCaps().useNonVBOVertexAndIndexDynamicData() && desc.fDynamic) {
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001216 desc.fID = 0;
bsalomon@google.come49ad452013-02-20 19:33:20 +00001217 GrGLVertexBuffer* vertexBuffer = SkNEW_ARGS(GrGLVertexBuffer, (this, desc));
reed@google.comac10a2d2010-12-22 21:39:39 +00001218 return vertexBuffer;
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001219 } else {
1220 GL_CALL(GenBuffers(1, &desc.fID));
1221 if (desc.fID) {
bsalomon@google.com6918d482013-03-07 19:09:11 +00001222 fHWGeometryState.setVertexBufferID(this, desc.fID);
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001223 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1224 // make sure driver can allocate memory for this buffer
1225 GL_ALLOC_CALL(this->glInterface(),
1226 BufferData(GR_GL_ARRAY_BUFFER,
1227 desc.fSizeInBytes,
1228 NULL, // data ptr
1229 desc.fDynamic ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW));
1230 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
1231 GL_CALL(DeleteBuffers(1, &desc.fID));
bsalomon@google.com6918d482013-03-07 19:09:11 +00001232 this->notifyVertexBufferDelete(desc.fID);
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001233 return NULL;
1234 }
1235 GrGLVertexBuffer* vertexBuffer = SkNEW_ARGS(GrGLVertexBuffer, (this, desc));
1236 return vertexBuffer;
1237 }
1238 return NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +00001239 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001240}
1241
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001242GrIndexBuffer* GrGpuGL::onCreateIndexBuffer(uint32_t size, bool dynamic) {
bsalomon@google.come49ad452013-02-20 19:33:20 +00001243 GrGLIndexBuffer::Desc desc;
1244 desc.fDynamic = dynamic;
1245 desc.fSizeInBytes = size;
1246 desc.fIsWrapped = false;
1247
bsalomon@google.com96966a52013-02-21 16:34:21 +00001248 if (this->glCaps().useNonVBOVertexAndIndexDynamicData() && desc.fDynamic) {
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001249 desc.fID = 0;
bsalomon@google.come49ad452013-02-20 19:33:20 +00001250 GrIndexBuffer* indexBuffer = SkNEW_ARGS(GrGLIndexBuffer, (this, desc));
reed@google.comac10a2d2010-12-22 21:39:39 +00001251 return indexBuffer;
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001252 } else {
1253 GL_CALL(GenBuffers(1, &desc.fID));
1254 if (desc.fID) {
bsalomon@google.com6918d482013-03-07 19:09:11 +00001255 fHWGeometryState.setIndexBufferIDOnDefaultVertexArray(this, desc.fID);
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001256 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1257 // make sure driver can allocate memory for this buffer
1258 GL_ALLOC_CALL(this->glInterface(),
1259 BufferData(GR_GL_ELEMENT_ARRAY_BUFFER,
1260 desc.fSizeInBytes,
1261 NULL, // data ptr
1262 desc.fDynamic ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW));
1263 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
1264 GL_CALL(DeleteBuffers(1, &desc.fID));
bsalomon@google.com6918d482013-03-07 19:09:11 +00001265 this->notifyIndexBufferDelete(desc.fID);
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001266 return NULL;
1267 }
1268 GrIndexBuffer* indexBuffer = SkNEW_ARGS(GrGLIndexBuffer, (this, desc));
1269 return indexBuffer;
1270 }
1271 return NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +00001272 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001273}
1274
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00001275GrPath* GrGpuGL::onCreatePath(const SkPath& inPath) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001276 SkASSERT(this->caps()->pathStencilingSupport());
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001277 return SkNEW_ARGS(GrGLPath, (this, inPath));
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00001278}
1279
bsalomon@google.coma3201942012-06-21 19:58:20 +00001280void GrGpuGL::flushScissor() {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001281 if (fScissorState.fEnabled) {
commit-bot@chromium.org5c363642013-10-04 15:28:18 +00001282 // Only access the RT if scissoring is being enabled. We can call this before performing
1283 // a glBitframebuffer for a surface->surface copy, which requires no RT to be bound to the
1284 // GrDrawState.
1285 const GrDrawState& drawState = this->getDrawState();
1286 const GrGLRenderTarget* rt =
1287 static_cast<const GrGLRenderTarget*>(drawState.getRenderTarget());
1288
1289 SkASSERT(NULL != rt);
1290 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.coma3201942012-06-21 19:58:20 +00001291 GrGLIRect scissor;
1292 scissor.setRelativeTo(vp,
1293 fScissorState.fRect.fLeft,
1294 fScissorState.fRect.fTop,
1295 fScissorState.fRect.width(),
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001296 fScissorState.fRect.height(),
1297 rt->origin());
bsalomon@google.coma3201942012-06-21 19:58:20 +00001298 // if the scissor fully contains the viewport then we fall through and
1299 // disable the scissor test.
1300 if (!scissor.contains(vp)) {
1301 if (fHWScissorSettings.fRect != scissor) {
1302 scissor.pushToGLScissor(this->glInterface());
1303 fHWScissorSettings.fRect = scissor;
1304 }
1305 if (kYes_TriState != fHWScissorSettings.fEnabled) {
1306 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1307 fHWScissorSettings.fEnabled = kYes_TriState;
1308 }
1309 return;
1310 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001311 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001312 if (kNo_TriState != fHWScissorSettings.fEnabled) {
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001313 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.coma3201942012-06-21 19:58:20 +00001314 fHWScissorSettings.fEnabled = kNo_TriState;
1315 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001316 }
1317}
1318
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001319void GrGpuGL::onClear(const SkIRect* rect, GrColor color) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001320 const GrDrawState& drawState = this->getDrawState();
1321 const GrRenderTarget* rt = drawState.getRenderTarget();
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001322 // parent class should never let us get here with no RT
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001323 SkASSERT(NULL != rt);
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001324
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001325 SkIRect clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001326 if (NULL != rect) {
1327 // flushScissor expects rect to be clipped to the target.
bsalomon@google.com74b98712011-11-11 19:46:16 +00001328 clippedRect = *rect;
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001329 SkIRect rtRect = SkIRect::MakeWH(rt->width(), rt->height());
bsalomon@google.com74b98712011-11-11 19:46:16 +00001330 if (clippedRect.intersect(rtRect)) {
1331 rect = &clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001332 } else {
1333 return;
1334 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001335 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001336 this->flushRenderTarget(rect);
bsalomon@google.coma3201942012-06-21 19:58:20 +00001337 GrAutoTRestore<ScissorState> asr(&fScissorState);
1338 fScissorState.fEnabled = (NULL != rect);
1339 if (fScissorState.fEnabled) {
1340 fScissorState.fRect = *rect;
1341 }
1342 this->flushScissor();
bsalomon@google.com74b98712011-11-11 19:46:16 +00001343
1344 GrGLfloat r, g, b, a;
1345 static const GrGLfloat scale255 = 1.f / 255.f;
1346 a = GrColorUnpackA(color) * scale255;
1347 GrGLfloat scaleRGB = scale255;
bsalomon@google.com74b98712011-11-11 19:46:16 +00001348 r = GrColorUnpackR(color) * scaleRGB;
1349 g = GrColorUnpackG(color) * scaleRGB;
1350 b = GrColorUnpackB(color) * scaleRGB;
1351
1352 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00001353 fHWWriteToColor = kYes_TriState;
bsalomon@google.com74b98712011-11-11 19:46:16 +00001354 GL_CALL(ClearColor(r, g, b, a));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001355 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001356}
1357
bsalomon@google.comedc177d2011-08-05 15:46:40 +00001358void GrGpuGL::clearStencil() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001359 if (NULL == this->getDrawState().getRenderTarget()) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001360 return;
1361 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001362
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001363 this->flushRenderTarget(&SkIRect::EmptyIRect());
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001364
bsalomon@google.coma3201942012-06-21 19:58:20 +00001365 GrAutoTRestore<ScissorState> asr(&fScissorState);
1366 fScissorState.fEnabled = false;
1367 this->flushScissor();
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001368
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001369 GL_CALL(StencilMask(0xffffffff));
1370 GL_CALL(ClearStencil(0));
1371 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001372 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001373}
1374
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001375void GrGpuGL::clearStencilClip(const SkIRect& rect, bool insideClip) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001376 const GrDrawState& drawState = this->getDrawState();
1377 const GrRenderTarget* rt = drawState.getRenderTarget();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001378 SkASSERT(NULL != rt);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001379
1380 // this should only be called internally when we know we have a
1381 // stencil buffer.
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001382 SkASSERT(NULL != rt->getStencilBuffer());
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001383 GrGLint stencilBitCount = rt->getStencilBuffer()->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001384#if 0
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001385 SkASSERT(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00001386 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001387#else
1388 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001389 // ANGLE a partial stencil mask will cause clears to be
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001390 // turned into draws. Our contract on GrDrawTarget says that
1391 // changing the clip between stencil passes may or may not
1392 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00001393 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001394#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001395 GrGLint value;
1396 if (insideClip) {
1397 value = (1 << (stencilBitCount - 1));
1398 } else {
1399 value = 0;
1400 }
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001401 this->flushRenderTarget(&SkIRect::EmptyIRect());
bsalomon@google.coma3201942012-06-21 19:58:20 +00001402
1403 GrAutoTRestore<ScissorState> asr(&fScissorState);
1404 fScissorState.fEnabled = true;
1405 fScissorState.fRect = rect;
1406 this->flushScissor();
1407
caryclark@google.comcf6285b2012-06-06 12:09:01 +00001408 GL_CALL(StencilMask((uint32_t) clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001409 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001410 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001411 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001412}
1413
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001414void GrGpuGL::onForceRenderTargetFlush() {
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001415 this->flushRenderTarget(&SkIRect::EmptyIRect());
reed@google.comac10a2d2010-12-22 21:39:39 +00001416}
1417
bsalomon@google.comc4364992011-11-07 15:54:49 +00001418bool GrGpuGL::readPixelsWillPayForYFlip(GrRenderTarget* renderTarget,
1419 int left, int top,
1420 int width, int height,
1421 GrPixelConfig config,
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001422 size_t rowBytes) const {
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001423 // If this rendertarget is aready TopLeft, we don't need to flip.
1424 if (kTopLeft_GrSurfaceOrigin == renderTarget->origin()) {
1425 return false;
1426 }
1427
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001428 // if GL can do the flip then we'll never pay for it.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001429 if (this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001430 return false;
1431 }
1432
1433 // If we have to do memcpy to handle non-trim rowBytes then we
bsalomon@google.com7107fa72011-11-10 14:54:14 +00001434 // get the flip for free. Otherwise it costs.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001435 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001436 return true;
1437 }
1438 // If we have to do memcpys to handle rowBytes then y-flip is free
1439 // Note the rowBytes might be tight to the passed in data, but if data
1440 // gets clipped in x to the target the rowBytes will no longer be tight.
1441 if (left >= 0 && (left + width) < renderTarget->width()) {
1442 return 0 == rowBytes ||
1443 GrBytesPerPixel(config) * width == rowBytes;
1444 } else {
1445 return false;
1446 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001447}
1448
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001449bool GrGpuGL::onReadPixels(GrRenderTarget* target,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001450 int left, int top,
1451 int width, int height,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001452 GrPixelConfig config,
1453 void* buffer,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001454 size_t rowBytes) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001455 GrGLenum format;
1456 GrGLenum type;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001457 bool flipY = kBottomLeft_GrSurfaceOrigin == target->origin();
bsalomon@google.com280e99f2012-01-05 16:17:38 +00001458 if (!this->configToGLFormats(config, false, NULL, &format, &type)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001459 return false;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001460 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001461 size_t bpp = GrBytesPerPixel(config);
1462 if (!adjust_pixel_ops_params(target->width(), target->height(), bpp,
1463 &left, &top, &width, &height,
1464 const_cast<const void**>(&buffer),
1465 &rowBytes)) {
1466 return false;
1467 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001468
bsalomon@google.comc6980972011-11-02 19:57:21 +00001469 // resolve the render target if necessary
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001470 GrGLRenderTarget* tgt = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001471 GrDrawState::AutoRenderTargetRestore artr;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001472 switch (tgt->getResolveType()) {
1473 case GrGLRenderTarget::kCantResolve_ResolveType:
1474 return false;
1475 case GrGLRenderTarget::kAutoResolves_ResolveType:
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001476 artr.set(this->drawState(), target);
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001477 this->flushRenderTarget(&SkIRect::EmptyIRect());
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001478 break;
1479 case GrGLRenderTarget::kCanResolve_ResolveType:
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001480 this->onResolveRenderTarget(tgt);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001481 // we don't track the state of the READ FBO ID.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001482 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1483 tgt->textureFBOID()));
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001484 break;
1485 default:
1486 GrCrash("Unknown resolve type");
reed@google.comac10a2d2010-12-22 21:39:39 +00001487 }
1488
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001489 const GrGLIRect& glvp = tgt->getViewport();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001490
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001491 // the read rect is viewport-relative
1492 GrGLIRect readRect;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001493 readRect.setRelativeTo(glvp, left, top, width, height, target->origin());
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001494
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001495 size_t tightRowBytes = bpp * width;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001496 if (0 == rowBytes) {
1497 rowBytes = tightRowBytes;
1498 }
1499 size_t readDstRowBytes = tightRowBytes;
1500 void* readDst = buffer;
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001501
bsalomon@google.comc6980972011-11-02 19:57:21 +00001502 // determine if GL can read using the passed rowBytes or if we need
1503 // a scratch buffer.
1504 SkAutoSMalloc<32 * sizeof(GrColor)> scratch;
1505 if (rowBytes != tightRowBytes) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001506 if (this->glCaps().packRowLengthSupport()) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001507 SkASSERT(!(rowBytes % sizeof(GrColor)));
bsalomon@google.comc6980972011-11-02 19:57:21 +00001508 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, rowBytes / sizeof(GrColor)));
1509 readDstRowBytes = rowBytes;
1510 } else {
1511 scratch.reset(tightRowBytes * height);
1512 readDst = scratch.get();
1513 }
1514 }
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001515 if (flipY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001516 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 1));
1517 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001518 GL_CALL(ReadPixels(readRect.fLeft, readRect.fBottom,
1519 readRect.fWidth, readRect.fHeight,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001520 format, type, readDst));
1521 if (readDstRowBytes != tightRowBytes) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001522 SkASSERT(this->glCaps().packRowLengthSupport());
bsalomon@google.comc6980972011-11-02 19:57:21 +00001523 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
1524 }
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001525 if (flipY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001526 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 0));
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001527 flipY = false;
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001528 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001529
1530 // now reverse the order of the rows, since GL's are bottom-to-top, but our
bsalomon@google.comc6980972011-11-02 19:57:21 +00001531 // API presents top-to-bottom. We must preserve the padding contents. Note
1532 // that the above readPixels did not overwrite the padding.
1533 if (readDst == buffer) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001534 SkASSERT(rowBytes == readDstRowBytes);
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001535 if (flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001536 scratch.reset(tightRowBytes);
1537 void* tmpRow = scratch.get();
1538 // flip y in-place by rows
1539 const int halfY = height >> 1;
1540 char* top = reinterpret_cast<char*>(buffer);
1541 char* bottom = top + (height - 1) * rowBytes;
1542 for (int y = 0; y < halfY; y++) {
1543 memcpy(tmpRow, top, tightRowBytes);
1544 memcpy(top, bottom, tightRowBytes);
1545 memcpy(bottom, tmpRow, tightRowBytes);
1546 top += rowBytes;
1547 bottom -= rowBytes;
1548 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001549 }
1550 } else {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001551 SkASSERT(readDst != buffer); SkASSERT(rowBytes != tightRowBytes);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001552 // copy from readDst to buffer while flipping y
caryclark@google.comcf6285b2012-06-06 12:09:01 +00001553 // const int halfY = height >> 1;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001554 const char* src = reinterpret_cast<const char*>(readDst);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001555 char* dst = reinterpret_cast<char*>(buffer);
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001556 if (flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001557 dst += (height-1) * rowBytes;
1558 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001559 for (int y = 0; y < height; y++) {
1560 memcpy(dst, src, tightRowBytes);
1561 src += readDstRowBytes;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001562 if (!flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001563 dst += rowBytes;
1564 } else {
1565 dst -= rowBytes;
1566 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001567 }
1568 }
1569 return true;
1570}
1571
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001572void GrGpuGL::flushRenderTarget(const SkIRect* bound) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001573
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001574 GrGLRenderTarget* rt =
1575 static_cast<GrGLRenderTarget*>(this->drawState()->getRenderTarget());
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001576 SkASSERT(NULL != rt);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001577
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001578 if (fHWBoundRenderTarget != rt) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001579 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, rt->renderFBOID()));
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +00001580#ifdef SK_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001581 GrGLenum status;
1582 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001583 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
reed@google.comb9255d52011-06-13 18:54:59 +00001584 GrPrintf("GrGpuGL::flushRenderTarget glCheckFramebufferStatus %x\n", status);
reed@google.comac10a2d2010-12-22 21:39:39 +00001585 }
robertphillips@google.coma6ffb582013-04-29 16:50:17 +00001586#endif
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001587 fHWBoundRenderTarget = rt;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001588 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.coma3201942012-06-21 19:58:20 +00001589 if (fHWViewport != vp) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001590 vp.pushToGLViewport(this->glInterface());
bsalomon@google.coma3201942012-06-21 19:58:20 +00001591 fHWViewport = vp;
reed@google.comac10a2d2010-12-22 21:39:39 +00001592 }
1593 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001594 if (NULL == bound || !bound->isEmpty()) {
1595 rt->flagAsNeedingResolve(bound);
1596 }
skia.committer@gmail.comaeefb2a2013-07-27 07:01:06 +00001597
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +00001598 GrTexture *texture = rt->asTexture();
1599 if (texture) {
1600 texture->dirtyMipMaps(true);
1601 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001602}
1603
twiz@google.com0f31ca72011-03-18 17:38:11 +00001604GrGLenum gPrimitiveType2GLMode[] = {
1605 GR_GL_TRIANGLES,
1606 GR_GL_TRIANGLE_STRIP,
1607 GR_GL_TRIANGLE_FAN,
1608 GR_GL_POINTS,
1609 GR_GL_LINES,
1610 GR_GL_LINE_STRIP
reed@google.comac10a2d2010-12-22 21:39:39 +00001611};
1612
bsalomon@google.comd302f142011-03-03 13:54:13 +00001613#define SWAP_PER_DRAW 0
1614
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001615#if SWAP_PER_DRAW
commit-bot@chromium.org43823302013-09-25 20:57:51 +00001616 #if defined(SK_BUILD_FOR_MAC)
bsalomon@google.comd302f142011-03-03 13:54:13 +00001617 #include <AGL/agl.h>
commit-bot@chromium.org43823302013-09-25 20:57:51 +00001618 #elif defined(SK_BUILD_FOR_WIN32)
bsalomon@google.comce7357d2012-06-25 17:49:25 +00001619 #include <gl/GL.h>
bsalomon@google.comd302f142011-03-03 13:54:13 +00001620 void SwapBuf() {
1621 DWORD procID = GetCurrentProcessId();
1622 HWND hwnd = GetTopWindow(GetDesktopWindow());
1623 while(hwnd) {
1624 DWORD wndProcID = 0;
1625 GetWindowThreadProcessId(hwnd, &wndProcID);
1626 if(wndProcID == procID) {
1627 SwapBuffers(GetDC(hwnd));
1628 }
1629 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
1630 }
1631 }
1632 #endif
1633#endif
1634
bsalomon@google.com74749cd2013-01-30 16:12:41 +00001635void GrGpuGL::onGpuDraw(const DrawInfo& info) {
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00001636 size_t indexOffsetInBytes;
1637 this->setupGeometry(info, &indexOffsetInBytes);
reed@google.comac10a2d2010-12-22 21:39:39 +00001638
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001639 SkASSERT((size_t)info.primitiveType() < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001640
bsalomon@google.com74749cd2013-01-30 16:12:41 +00001641 if (info.isIndexed()) {
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00001642 GrGLvoid* indices =
1643 reinterpret_cast<GrGLvoid*>(indexOffsetInBytes + sizeof(uint16_t) * info.startIndex());
bsalomon@google.com74749cd2013-01-30 16:12:41 +00001644 // info.startVertex() was accounted for by setupGeometry.
1645 GL_CALL(DrawElements(gPrimitiveType2GLMode[info.primitiveType()],
1646 info.indexCount(),
1647 GR_GL_UNSIGNED_SHORT,
1648 indices));
1649 } else {
1650 // Pass 0 for parameter first. We have to adjust glVertexAttribPointer() to account for
1651 // startVertex in the DrawElements case. So we always rely on setupGeometry to have
1652 // accounted for startVertex.
1653 GL_CALL(DrawArrays(gPrimitiveType2GLMode[info.primitiveType()], 0, info.vertexCount()));
1654 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00001655#if SWAP_PER_DRAW
1656 glFlush();
commit-bot@chromium.org43823302013-09-25 20:57:51 +00001657 #if defined(SK_BUILD_FOR_MAC)
bsalomon@google.comd302f142011-03-03 13:54:13 +00001658 aglSwapBuffers(aglGetCurrentContext());
1659 int set_a_break_pt_here = 9;
1660 aglSwapBuffers(aglGetCurrentContext());
commit-bot@chromium.org43823302013-09-25 20:57:51 +00001661 #elif defined(SK_BUILD_FOR_WIN32)
bsalomon@google.comd302f142011-03-03 13:54:13 +00001662 SwapBuf();
1663 int set_a_break_pt_here = 9;
1664 SwapBuf();
1665 #endif
1666#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001667}
1668
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001669namespace {
bsalomon@google.com100abf42012-09-05 17:40:04 +00001670
1671static const uint16_t kOnes16 = static_cast<uint16_t>(~0);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001672const GrStencilSettings& winding_nv_path_stencil_settings() {
1673 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
1674 kIncClamp_StencilOp,
1675 kIncClamp_StencilOp,
1676 kAlwaysIfInClip_StencilFunc,
bsalomon@google.com100abf42012-09-05 17:40:04 +00001677 kOnes16, kOnes16, kOnes16);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001678 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
1679}
1680const GrStencilSettings& even_odd_nv_path_stencil_settings() {
1681 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
1682 kInvert_StencilOp,
1683 kInvert_StencilOp,
1684 kAlwaysIfInClip_StencilFunc,
bsalomon@google.com100abf42012-09-05 17:40:04 +00001685 kOnes16, kOnes16, kOnes16);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001686 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
1687}
1688}
1689
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001690void GrGpuGL::setStencilPathSettings(const GrPath&,
sugoi@google.com12b4e272012-12-06 20:13:11 +00001691 SkPath::FillType fill,
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001692 GrStencilSettings* settings) {
1693 switch (fill) {
sugoi@google.com12b4e272012-12-06 20:13:11 +00001694 case SkPath::kEvenOdd_FillType:
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001695 *settings = even_odd_nv_path_stencil_settings();
1696 return;
sugoi@google.com12b4e272012-12-06 20:13:11 +00001697 case SkPath::kWinding_FillType:
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001698 *settings = winding_nv_path_stencil_settings();
1699 return;
1700 default:
1701 GrCrash("Unexpected path fill.");
1702 }
1703}
1704
sugoi@google.com12b4e272012-12-06 20:13:11 +00001705void GrGpuGL::onGpuStencilPath(const GrPath* path, SkPath::FillType fill) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001706 SkASSERT(this->caps()->pathStencilingSupport());
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001707
1708 GrGLuint id = static_cast<const GrGLPath*>(path)->pathID();
1709 GrDrawState* drawState = this->drawState();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001710 SkASSERT(NULL != drawState->getRenderTarget());
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001711 if (NULL == drawState->getRenderTarget()->getStencilBuffer()) {
1712 return;
1713 }
1714
1715 // Decide how to manipulate the stencil buffer based on the fill rule.
1716 // Also, assert that the stencil settings we set in setStencilPathSettings
1717 // are present.
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001718 SkASSERT(!fStencilSettings.isTwoSided());
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001719 GrGLenum fillMode;
1720 switch (fill) {
sugoi@google.com12b4e272012-12-06 20:13:11 +00001721 case SkPath::kWinding_FillType:
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001722 fillMode = GR_GL_COUNT_UP;
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001723 SkASSERT(kIncClamp_StencilOp ==
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001724 fStencilSettings.passOp(GrStencilSettings::kFront_Face));
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001725 SkASSERT(kIncClamp_StencilOp ==
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001726 fStencilSettings.failOp(GrStencilSettings::kFront_Face));
1727 break;
sugoi@google.com12b4e272012-12-06 20:13:11 +00001728 case SkPath::kEvenOdd_FillType:
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001729 fillMode = GR_GL_INVERT;
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001730 SkASSERT(kInvert_StencilOp ==
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001731 fStencilSettings.passOp(GrStencilSettings::kFront_Face));
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001732 SkASSERT(kInvert_StencilOp ==
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001733 fStencilSettings.failOp(GrStencilSettings::kFront_Face));
1734 break;
1735 default:
1736 // Only the above two fill rules are allowed.
1737 GrCrash("Unexpected path fill.");
bsalomon@google.com548a4332012-07-11 19:45:22 +00001738 return; // suppress unused var warning.
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001739 }
1740 GrGLint writeMask = fStencilSettings.writeMask(GrStencilSettings::kFront_Face);
1741 GL_CALL(StencilFillPath(id, fillMode, writeMask));
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00001742}
1743
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001744void GrGpuGL::onResolveRenderTarget(GrRenderTarget* target) {
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001745 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001746 if (rt->needsResolve()) {
bsalomon@google.com347c3822013-05-01 20:10:01 +00001747 // Some extensions automatically resolves the texture when it is read.
1748 if (this->glCaps().usesMSAARenderBuffers()) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001749 SkASSERT(rt->textureFBOID() != rt->renderFBOID());
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001750 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER, rt->renderFBOID()));
1751 GL_CALL(BindFramebuffer(GR_GL_DRAW_FRAMEBUFFER, rt->textureFBOID()));
1752 // make sure we go through flushRenderTarget() since we've modified
1753 // the bound DRAW FBO ID.
1754 fHWBoundRenderTarget = NULL;
1755 const GrGLIRect& vp = rt->getViewport();
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001756 const SkIRect dirtyRect = rt->getResolveRect();
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001757 GrGLIRect r;
1758 r.setRelativeTo(vp, dirtyRect.fLeft, dirtyRect.fTop,
1759 dirtyRect.width(), dirtyRect.height(), target->origin());
reed@google.comac10a2d2010-12-22 21:39:39 +00001760
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001761 GrAutoTRestore<ScissorState> asr;
bsalomon@google.com347c3822013-05-01 20:10:01 +00001762 if (GrGLCaps::kES_Apple_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001763 // Apple's extension uses the scissor as the blit bounds.
bsalomon@google.coma3201942012-06-21 19:58:20 +00001764 asr.reset(&fScissorState);
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001765 fScissorState.fEnabled = true;
1766 fScissorState.fRect = dirtyRect;
bsalomon@google.coma3201942012-06-21 19:58:20 +00001767 this->flushScissor();
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001768 GL_CALL(ResolveMultisampleFramebuffer());
1769 } else {
bsalomon@google.com347c3822013-05-01 20:10:01 +00001770 if (GrGLCaps::kDesktop_EXT_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001771 // this respects the scissor during the blit, so disable it.
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001772 asr.reset(&fScissorState);
1773 fScissorState.fEnabled = false;
1774 this->flushScissor();
1775 }
1776 int right = r.fLeft + r.fWidth;
1777 int top = r.fBottom + r.fHeight;
1778 GL_CALL(BlitFramebuffer(r.fLeft, r.fBottom, right, top,
1779 r.fLeft, r.fBottom, right, top,
1780 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001781 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001782 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001783 rt->flagAsResolved();
reed@google.comac10a2d2010-12-22 21:39:39 +00001784 }
1785}
1786
bsalomon@google.com411dad02012-06-05 20:24:20 +00001787namespace {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001788
bsalomon@google.com411dad02012-06-05 20:24:20 +00001789GrGLenum gr_to_gl_stencil_func(GrStencilFunc basicFunc) {
1790 static const GrGLenum gTable[] = {
1791 GR_GL_ALWAYS, // kAlways_StencilFunc
1792 GR_GL_NEVER, // kNever_StencilFunc
1793 GR_GL_GREATER, // kGreater_StencilFunc
1794 GR_GL_GEQUAL, // kGEqual_StencilFunc
1795 GR_GL_LESS, // kLess_StencilFunc
1796 GR_GL_LEQUAL, // kLEqual_StencilFunc,
1797 GR_GL_EQUAL, // kEqual_StencilFunc,
1798 GR_GL_NOTEQUAL, // kNotEqual_StencilFunc,
1799 };
1800 GR_STATIC_ASSERT(GR_ARRAY_COUNT(gTable) == kBasicStencilFuncCount);
1801 GR_STATIC_ASSERT(0 == kAlways_StencilFunc);
1802 GR_STATIC_ASSERT(1 == kNever_StencilFunc);
1803 GR_STATIC_ASSERT(2 == kGreater_StencilFunc);
1804 GR_STATIC_ASSERT(3 == kGEqual_StencilFunc);
1805 GR_STATIC_ASSERT(4 == kLess_StencilFunc);
1806 GR_STATIC_ASSERT(5 == kLEqual_StencilFunc);
1807 GR_STATIC_ASSERT(6 == kEqual_StencilFunc);
1808 GR_STATIC_ASSERT(7 == kNotEqual_StencilFunc);
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001809 SkASSERT((unsigned) basicFunc < kBasicStencilFuncCount);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001810
1811 return gTable[basicFunc];
1812}
1813
1814GrGLenum gr_to_gl_stencil_op(GrStencilOp op) {
1815 static const GrGLenum gTable[] = {
1816 GR_GL_KEEP, // kKeep_StencilOp
1817 GR_GL_REPLACE, // kReplace_StencilOp
1818 GR_GL_INCR_WRAP, // kIncWrap_StencilOp
1819 GR_GL_INCR, // kIncClamp_StencilOp
1820 GR_GL_DECR_WRAP, // kDecWrap_StencilOp
1821 GR_GL_DECR, // kDecClamp_StencilOp
1822 GR_GL_ZERO, // kZero_StencilOp
1823 GR_GL_INVERT, // kInvert_StencilOp
1824 };
1825 GR_STATIC_ASSERT(GR_ARRAY_COUNT(gTable) == kStencilOpCount);
1826 GR_STATIC_ASSERT(0 == kKeep_StencilOp);
1827 GR_STATIC_ASSERT(1 == kReplace_StencilOp);
1828 GR_STATIC_ASSERT(2 == kIncWrap_StencilOp);
1829 GR_STATIC_ASSERT(3 == kIncClamp_StencilOp);
1830 GR_STATIC_ASSERT(4 == kDecWrap_StencilOp);
1831 GR_STATIC_ASSERT(5 == kDecClamp_StencilOp);
1832 GR_STATIC_ASSERT(6 == kZero_StencilOp);
1833 GR_STATIC_ASSERT(7 == kInvert_StencilOp);
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001834 SkASSERT((unsigned) op < kStencilOpCount);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001835 return gTable[op];
1836}
1837
1838void set_gl_stencil(const GrGLInterface* gl,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001839 const GrStencilSettings& settings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00001840 GrGLenum glFace,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001841 GrStencilSettings::Face grFace) {
1842 GrGLenum glFunc = gr_to_gl_stencil_func(settings.func(grFace));
1843 GrGLenum glFailOp = gr_to_gl_stencil_op(settings.failOp(grFace));
1844 GrGLenum glPassOp = gr_to_gl_stencil_op(settings.passOp(grFace));
1845
1846 GrGLint ref = settings.funcRef(grFace);
1847 GrGLint mask = settings.funcMask(grFace);
1848 GrGLint writeMask = settings.writeMask(grFace);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001849
1850 if (GR_GL_FRONT_AND_BACK == glFace) {
1851 // we call the combined func just in case separate stencil is not
1852 // supported.
1853 GR_GL_CALL(gl, StencilFunc(glFunc, ref, mask));
1854 GR_GL_CALL(gl, StencilMask(writeMask));
1855 GR_GL_CALL(gl, StencilOp(glFailOp, glPassOp, glPassOp));
1856 } else {
1857 GR_GL_CALL(gl, StencilFuncSeparate(glFace, glFunc, ref, mask));
1858 GR_GL_CALL(gl, StencilMaskSeparate(glFace, writeMask));
1859 GR_GL_CALL(gl, StencilOpSeparate(glFace, glFailOp, glPassOp, glPassOp));
1860 }
1861}
1862}
bsalomon@google.comd302f142011-03-03 13:54:13 +00001863
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001864void GrGpuGL::flushStencil(DrawType type) {
1865 if (kStencilPath_DrawType == type) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001866 SkASSERT(!fStencilSettings.isTwoSided());
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001867 // Just the func, ref, and mask is set here. The op and write mask are params to the call
1868 // that draws the path to the SB (glStencilFillPath)
1869 GrGLenum func =
1870 gr_to_gl_stencil_func(fStencilSettings.func(GrStencilSettings::kFront_Face));
1871 GL_CALL(PathStencilFunc(func,
1872 fStencilSettings.funcRef(GrStencilSettings::kFront_Face),
1873 fStencilSettings.funcMask(GrStencilSettings::kFront_Face)));
1874 } else if (fHWStencilSettings != fStencilSettings) {
1875 if (fStencilSettings.isDisabled()) {
1876 if (kNo_TriState != fHWStencilTestEnabled) {
1877 GL_CALL(Disable(GR_GL_STENCIL_TEST));
1878 fHWStencilTestEnabled = kNo_TriState;
1879 }
1880 } else {
1881 if (kYes_TriState != fHWStencilTestEnabled) {
1882 GL_CALL(Enable(GR_GL_STENCIL_TEST));
1883 fHWStencilTestEnabled = kYes_TriState;
1884 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001885 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001886 if (!fStencilSettings.isDisabled()) {
bsalomon@google.combcce8922013-03-25 15:38:39 +00001887 if (this->caps()->twoSidedStencilSupport()) {
bsalomon@google.com411dad02012-06-05 20:24:20 +00001888 set_gl_stencil(this->glInterface(),
bsalomon@google.coma3201942012-06-21 19:58:20 +00001889 fStencilSettings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00001890 GR_GL_FRONT,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001891 GrStencilSettings::kFront_Face);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001892 set_gl_stencil(this->glInterface(),
bsalomon@google.coma3201942012-06-21 19:58:20 +00001893 fStencilSettings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00001894 GR_GL_BACK,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001895 GrStencilSettings::kBack_Face);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001896 } else {
bsalomon@google.com411dad02012-06-05 20:24:20 +00001897 set_gl_stencil(this->glInterface(),
bsalomon@google.coma3201942012-06-21 19:58:20 +00001898 fStencilSettings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00001899 GR_GL_FRONT_AND_BACK,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001900 GrStencilSettings::kFront_Face);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001901 }
1902 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001903 fHWStencilSettings = fStencilSettings;
reed@google.comac10a2d2010-12-22 21:39:39 +00001904 }
1905}
1906
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001907void GrGpuGL::flushAAState(DrawType type) {
bsalomon@google.com202d1392013-03-19 18:58:08 +00001908// At least some ATI linux drivers will render GL_LINES incorrectly when MSAA state is enabled but
1909// the target is not multisampled. Single pixel wide lines are rendered thicker than 1 pixel wide.
1910#if 0
1911 // Replace RT_HAS_MSAA with this definition once this driver bug is no longer a relevant concern
1912 #define RT_HAS_MSAA rt->isMultisampled()
1913#else
1914 #define RT_HAS_MSAA (rt->isMultisampled() || kDrawLines_DrawType == type)
1915#endif
1916
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001917 const GrRenderTarget* rt = this->getDrawState().getRenderTarget();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001918 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001919 // ES doesn't support toggling GL_MULTISAMPLE and doesn't have
1920 // smooth lines.
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001921 // we prefer smooth lines over multisampled lines
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00001922 bool smoothLines = false;
1923
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001924 if (kDrawLines_DrawType == type) {
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00001925 smoothLines = this->willUseHWAALines();
1926 if (smoothLines) {
1927 if (kYes_TriState != fHWAAState.fSmoothLineEnabled) {
1928 GL_CALL(Enable(GR_GL_LINE_SMOOTH));
1929 fHWAAState.fSmoothLineEnabled = kYes_TriState;
1930 // must disable msaa to use line smoothing
bsalomon@google.com202d1392013-03-19 18:58:08 +00001931 if (RT_HAS_MSAA &&
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00001932 kNo_TriState != fHWAAState.fMSAAEnabled) {
1933 GL_CALL(Disable(GR_GL_MULTISAMPLE));
1934 fHWAAState.fMSAAEnabled = kNo_TriState;
1935 }
1936 }
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001937 } else {
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00001938 if (kNo_TriState != fHWAAState.fSmoothLineEnabled) {
1939 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
1940 fHWAAState.fSmoothLineEnabled = kNo_TriState;
1941 }
1942 }
1943 }
bsalomon@google.com202d1392013-03-19 18:58:08 +00001944 if (!smoothLines && RT_HAS_MSAA) {
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001945 // FIXME: GL_NV_pr doesn't seem to like MSAA disabled. The paths
1946 // convex hulls of each segment appear to get filled.
1947 bool enableMSAA = kStencilPath_DrawType == type ||
1948 this->getDrawState().isHWAntialiasState();
1949 if (enableMSAA) {
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00001950 if (kYes_TriState != fHWAAState.fMSAAEnabled) {
1951 GL_CALL(Enable(GR_GL_MULTISAMPLE));
1952 fHWAAState.fMSAAEnabled = kYes_TriState;
1953 }
1954 } else {
1955 if (kNo_TriState != fHWAAState.fMSAAEnabled) {
1956 GL_CALL(Disable(GR_GL_MULTISAMPLE));
1957 fHWAAState.fMSAAEnabled = kNo_TriState;
1958 }
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001959 }
1960 }
1961 }
1962}
1963
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00001964void GrGpuGL::flushBlend(bool isLines,
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001965 GrBlendCoeff srcCoeff,
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001966 GrBlendCoeff dstCoeff) {
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00001967 if (isLines && this->willUseHWAALines()) {
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001968 if (kYes_TriState != fHWBlendState.fEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001969 GL_CALL(Enable(GR_GL_BLEND));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001970 fHWBlendState.fEnabled = kYes_TriState;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001971 }
bsalomon@google.com47059542012-06-06 20:51:20 +00001972 if (kSA_GrBlendCoeff != fHWBlendState.fSrcCoeff ||
1973 kISA_GrBlendCoeff != fHWBlendState.fDstCoeff) {
1974 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[kSA_GrBlendCoeff],
1975 gXfermodeCoeff2Blend[kISA_GrBlendCoeff]));
1976 fHWBlendState.fSrcCoeff = kSA_GrBlendCoeff;
1977 fHWBlendState.fDstCoeff = kISA_GrBlendCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001978 }
1979 } else {
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001980 // any optimization to disable blending should
1981 // have already been applied and tweaked the coeffs
1982 // to (1, 0).
bsalomon@google.com47059542012-06-06 20:51:20 +00001983 bool blendOff = kOne_GrBlendCoeff == srcCoeff &&
1984 kZero_GrBlendCoeff == dstCoeff;
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001985 if (blendOff) {
1986 if (kNo_TriState != fHWBlendState.fEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001987 GL_CALL(Disable(GR_GL_BLEND));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001988 fHWBlendState.fEnabled = kNo_TriState;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001989 }
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001990 } else {
1991 if (kYes_TriState != fHWBlendState.fEnabled) {
1992 GL_CALL(Enable(GR_GL_BLEND));
1993 fHWBlendState.fEnabled = kYes_TriState;
1994 }
1995 if (fHWBlendState.fSrcCoeff != srcCoeff ||
1996 fHWBlendState.fDstCoeff != dstCoeff) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001997 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
1998 gXfermodeCoeff2Blend[dstCoeff]));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001999 fHWBlendState.fSrcCoeff = srcCoeff;
2000 fHWBlendState.fDstCoeff = dstCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002001 }
bsalomon@google.coma5d056a2012-03-27 15:59:58 +00002002 GrColor blendConst = this->getDrawState().getBlendConstant();
bsalomon@google.com271cffc2011-05-20 14:13:56 +00002003 if ((BlendCoeffReferencesConstant(srcCoeff) ||
2004 BlendCoeffReferencesConstant(dstCoeff)) &&
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002005 (!fHWBlendState.fConstColorValid ||
2006 fHWBlendState.fConstColor != blendConst)) {
bsalomon@google.com75347472012-09-17 17:23:21 +00002007 GrGLfloat c[4];
2008 GrColorToRGBAFloat(blendConst, c);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002009 GL_CALL(BlendColor(c[0], c[1], c[2], c[3]));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002010 fHWBlendState.fConstColor = blendConst;
2011 fHWBlendState.fConstColorValid = true;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002012 }
2013 }
2014 }
2015}
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002016
commit-bot@chromium.org6364b5e2013-08-20 20:22:52 +00002017static inline GrGLenum tile_to_gl_wrap(SkShader::TileMode tm) {
bsalomon@google.comb8670992012-07-25 21:27:09 +00002018 static const GrGLenum gWrapModes[] = {
2019 GR_GL_CLAMP_TO_EDGE,
2020 GR_GL_REPEAT,
2021 GR_GL_MIRRORED_REPEAT
2022 };
commit-bot@chromium.org5d7ca952013-04-22 20:26:44 +00002023 GR_STATIC_ASSERT(SkShader::kTileModeCount == SK_ARRAY_COUNT(gWrapModes));
bsalomon@google.comb8670992012-07-25 21:27:09 +00002024 GR_STATIC_ASSERT(0 == SkShader::kClamp_TileMode);
2025 GR_STATIC_ASSERT(1 == SkShader::kRepeat_TileMode);
2026 GR_STATIC_ASSERT(2 == SkShader::kMirror_TileMode);
2027 return gWrapModes[tm];
2028}
2029
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002030void GrGpuGL::bindTexture(int unitIdx, const GrTextureParams& params, GrGLTexture* texture) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002031 SkASSERT(NULL != texture);
tomhudson@google.comd0c1a062012-07-12 17:23:52 +00002032
bsalomon@google.comb8670992012-07-25 21:27:09 +00002033 // If we created a rt/tex and rendered to it without using a texture and now we're texturing
2034 // 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 +00002035 // out of the "last != next" check.
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002036 GrGLRenderTarget* texRT = static_cast<GrGLRenderTarget*>(texture->asRenderTarget());
bsalomon@google.com4c883782012-06-04 19:05:11 +00002037 if (NULL != texRT) {
2038 this->onResolveRenderTarget(texRT);
2039 }
2040
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002041 if (fHWBoundTextures[unitIdx] != texture) {
2042 this->setTextureUnit(unitIdx);
2043 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, texture->textureID()));
2044 fHWBoundTextures[unitIdx] = texture;
bsalomon@google.com4c883782012-06-04 19:05:11 +00002045 }
2046
bsalomon@google.com4c883782012-06-04 19:05:11 +00002047 ResetTimestamp timestamp;
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002048 const GrGLTexture::TexParams& oldTexParams = texture->getCachedTexParams(&timestamp);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002049 bool setAll = timestamp < this->getResetTimestamp();
2050 GrGLTexture::TexParams newTexParams;
2051
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00002052 static GrGLenum glMinFilterModes[] = {
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +00002053 GR_GL_NEAREST,
2054 GR_GL_LINEAR,
2055 GR_GL_LINEAR_MIPMAP_LINEAR
2056 };
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00002057 static GrGLenum glMagFilterModes[] = {
2058 GR_GL_NEAREST,
2059 GR_GL_LINEAR,
2060 GR_GL_LINEAR
2061 };
2062 newTexParams.fMinFilter = glMinFilterModes[params.filterMode()];
2063 newTexParams.fMagFilter = glMagFilterModes[params.filterMode()];
skia.committer@gmail.comaeefb2a2013-07-27 07:01:06 +00002064
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +00002065#ifndef SKIA_IGNORE_GPU_MIPMAPS
skia.committer@gmail.comaeefb2a2013-07-27 07:01:06 +00002066 if (params.filterMode() == GrTextureParams::kMipMap_FilterMode &&
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +00002067 texture->mipMapsAreDirty()) {
2068// GL_CALL(Hint(GR_GL_GENERATE_MIPMAP_HINT,GR_GL_NICEST));
2069 GL_CALL(GenerateMipmap(GR_GL_TEXTURE_2D));
2070 texture->dirtyMipMaps(false);
2071 }
2072#endif
bsalomon@google.com4c883782012-06-04 19:05:11 +00002073
bsalomon@google.comb8670992012-07-25 21:27:09 +00002074 newTexParams.fWrapS = tile_to_gl_wrap(params.getTileModeX());
2075 newTexParams.fWrapT = tile_to_gl_wrap(params.getTileModeY());
bsalomon@google.com4c883782012-06-04 19:05:11 +00002076 memcpy(newTexParams.fSwizzleRGBA,
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002077 GrGLShaderBuilder::GetTexParamSwizzle(texture->config(), this->glCaps()),
bsalomon@google.com4c883782012-06-04 19:05:11 +00002078 sizeof(newTexParams.fSwizzleRGBA));
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00002079 if (setAll || newTexParams.fMagFilter != oldTexParams.fMagFilter) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002080 this->setTextureUnit(unitIdx);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002081 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2082 GR_GL_TEXTURE_MAG_FILTER,
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00002083 newTexParams.fMagFilter));
2084 }
2085 if (setAll || newTexParams.fMinFilter != oldTexParams.fMinFilter) {
2086 this->setTextureUnit(unitIdx);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002087 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2088 GR_GL_TEXTURE_MIN_FILTER,
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00002089 newTexParams.fMinFilter));
bsalomon@google.com4c883782012-06-04 19:05:11 +00002090 }
2091 if (setAll || newTexParams.fWrapS != oldTexParams.fWrapS) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002092 this->setTextureUnit(unitIdx);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002093 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2094 GR_GL_TEXTURE_WRAP_S,
2095 newTexParams.fWrapS));
2096 }
2097 if (setAll || newTexParams.fWrapT != oldTexParams.fWrapT) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002098 this->setTextureUnit(unitIdx);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002099 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2100 GR_GL_TEXTURE_WRAP_T,
2101 newTexParams.fWrapT));
2102 }
2103 if (this->glCaps().textureSwizzleSupport() &&
2104 (setAll || memcmp(newTexParams.fSwizzleRGBA,
2105 oldTexParams.fSwizzleRGBA,
2106 sizeof(newTexParams.fSwizzleRGBA)))) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002107 this->setTextureUnit(unitIdx);
commit-bot@chromium.org6364b5e2013-08-20 20:22:52 +00002108 if (this->glBinding() == kES_GrGLBinding) {
2109 // ES3 added swizzle support but not GL_TEXTURE_SWIZZLE_RGBA.
2110 const GrGLenum* swizzle = newTexParams.fSwizzleRGBA;
2111 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_SWIZZLE_R, swizzle[0]));
2112 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_SWIZZLE_G, swizzle[1]));
2113 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_SWIZZLE_B, swizzle[2]));
2114 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_SWIZZLE_A, swizzle[3]));
2115 } else {
2116 GR_STATIC_ASSERT(sizeof(newTexParams.fSwizzleRGBA[0]) == sizeof(GrGLint));
2117 const GrGLint* swizzle = reinterpret_cast<const GrGLint*>(newTexParams.fSwizzleRGBA);
2118 GL_CALL(TexParameteriv(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_SWIZZLE_RGBA, swizzle));
2119 }
bsalomon@google.com4c883782012-06-04 19:05:11 +00002120 }
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002121 texture->setCachedTexParams(newTexParams, this->getResetTimestamp());
bsalomon@google.com4c883782012-06-04 19:05:11 +00002122}
2123
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +00002124void GrGpuGL::setProjectionMatrix(const SkMatrix& matrix,
2125 const SkISize& renderTargetSize,
2126 GrSurfaceOrigin renderTargetOrigin) {
2127
2128 SkASSERT(this->glCaps().fixedFunctionSupport());
2129
2130 if (renderTargetOrigin == fHWProjectionMatrixState.fRenderTargetOrigin &&
2131 renderTargetSize == fHWProjectionMatrixState.fRenderTargetSize &&
2132 matrix.cheapEqualTo(fHWProjectionMatrixState.fViewMatrix)) {
2133 return;
2134 }
2135
2136 fHWProjectionMatrixState.fViewMatrix = matrix;
2137 fHWProjectionMatrixState.fRenderTargetSize = renderTargetSize;
2138 fHWProjectionMatrixState.fRenderTargetOrigin = renderTargetOrigin;
2139
2140 GrGLfloat glMatrix[4 * 4];
2141 fHWProjectionMatrixState.getGLMatrix<4>(glMatrix);
2142 GL_CALL(MatrixMode(GR_GL_PROJECTION));
2143 GL_CALL(LoadMatrixf(glMatrix));
2144}
2145
2146void GrGpuGL::enableTexGen(int unitIdx,
2147 TexGenComponents components,
2148 const GrGLfloat* coefficients) {
2149
2150 SkASSERT(this->glCaps().fixedFunctionSupport());
2151 SkASSERT(this->caps()->pathStencilingSupport());
2152 SkASSERT(components >= kS_TexGenComponents && components <= kSTR_TexGenComponents);
2153
2154 if (GR_GL_OBJECT_LINEAR == fHWTexGenSettings[unitIdx].fMode &&
2155 components == fHWTexGenSettings[unitIdx].fNumComponents &&
2156 !memcmp(coefficients, fHWTexGenSettings[unitIdx].fCoefficients,
2157 3 * components * sizeof(GrGLfloat))) {
2158 return;
2159 }
2160
2161 this->setTextureUnit(unitIdx);
2162
2163 if (GR_GL_OBJECT_LINEAR != fHWTexGenSettings[unitIdx].fMode) {
2164 for (int i = 0; i < 4; i++) {
2165 GL_CALL(TexGeni(GR_GL_S + i, GR_GL_TEXTURE_GEN_MODE, GR_GL_OBJECT_LINEAR));
2166 }
2167 fHWTexGenSettings[unitIdx].fMode = GR_GL_OBJECT_LINEAR;
2168 }
2169
2170 for (int i = fHWTexGenSettings[unitIdx].fNumComponents; i < components; i++) {
2171 GL_CALL(Enable(GR_GL_TEXTURE_GEN_S + i));
2172 }
2173 for (int i = components; i < fHWTexGenSettings[unitIdx].fNumComponents; i++) {
2174 GL_CALL(Disable(GR_GL_TEXTURE_GEN_S + i));
2175 }
2176 fHWTexGenSettings[unitIdx].fNumComponents = components;
2177
2178 for (int i = 0; i < components; i++) {
2179 GrGLfloat plane[] = {coefficients[0 + 3 * i],
2180 coefficients[1 + 3 * i],
2181 0,
2182 coefficients[2 + 3 * i]};
2183 GL_CALL(TexGenfv(GR_GL_S + i, GR_GL_OBJECT_PLANE, plane));
2184 }
2185
2186 GL_CALL(PathTexGen(GR_GL_TEXTURE0 + unitIdx,
2187 GR_GL_OBJECT_LINEAR,
2188 components,
2189 coefficients));
2190
2191 memcpy(fHWTexGenSettings[unitIdx].fCoefficients, coefficients,
2192 3 * components * sizeof(GrGLfloat));
2193
2194 fHWActiveTexGenSets = SkTMax(fHWActiveTexGenSets, unitIdx);
2195}
2196
2197void GrGpuGL::enableTexGen(int unitIdx, TexGenComponents components, const SkMatrix& matrix) {
2198
2199 GrGLfloat coefficients[3 * 3];
2200 SkASSERT(this->glCaps().fixedFunctionSupport());
2201 SkASSERT(components >= kS_TexGenComponents && components <= kSTR_TexGenComponents);
2202
2203 coefficients[0] = SkScalarToFloat(matrix[SkMatrix::kMScaleX]);
2204 coefficients[1] = SkScalarToFloat(matrix[SkMatrix::kMSkewX]);
2205 coefficients[2] = SkScalarToFloat(matrix[SkMatrix::kMTransX]);
2206
2207 if (components >= kST_TexGenComponents) {
2208 coefficients[3] = SkScalarToFloat(matrix[SkMatrix::kMSkewY]);
2209 coefficients[4] = SkScalarToFloat(matrix[SkMatrix::kMScaleY]);
2210 coefficients[5] = SkScalarToFloat(matrix[SkMatrix::kMTransY]);
2211 }
2212
2213 if (components >= kSTR_TexGenComponents) {
2214 coefficients[6] = SkScalarToFloat(matrix[SkMatrix::kMPersp0]);
2215 coefficients[7] = SkScalarToFloat(matrix[SkMatrix::kMPersp1]);
2216 coefficients[8] = SkScalarToFloat(matrix[SkMatrix::kMPersp2]);
2217 }
2218
2219 enableTexGen(unitIdx, components, coefficients);
2220}
2221
2222void GrGpuGL::disableUnusedTexGen(int numUsedTexCoordSets) {
2223
2224 SkASSERT(this->glCaps().fixedFunctionSupport());
2225
2226 for (int i = numUsedTexCoordSets; i < fHWActiveTexGenSets; i++) {
2227 if (0 == fHWTexGenSettings[i].fNumComponents) {
2228 continue;
2229 }
2230
2231 this->setTextureUnit(i);
2232 for (int j = 0; j < fHWTexGenSettings[i].fNumComponents; j++) {
2233 GL_CALL(Disable(GR_GL_TEXTURE_GEN_S + j));
2234 }
2235 GL_CALL(PathTexGen(GR_GL_TEXTURE0 + i, GR_GL_NONE, 0, NULL));
2236 fHWTexGenSettings[i].fNumComponents = 0;
2237 }
2238
2239 fHWActiveTexGenSets = SkTMin(fHWActiveTexGenSets, numUsedTexCoordSets);
2240}
2241
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002242void GrGpuGL::flushMiscFixedFunctionState() {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002243
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002244 const GrDrawState& drawState = this->getDrawState();
reed@google.comac10a2d2010-12-22 21:39:39 +00002245
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002246 if (drawState.isDitherState()) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002247 if (kYes_TriState != fHWDitherEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002248 GL_CALL(Enable(GR_GL_DITHER));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002249 fHWDitherEnabled = kYes_TriState;
2250 }
2251 } else {
2252 if (kNo_TriState != fHWDitherEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002253 GL_CALL(Disable(GR_GL_DITHER));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002254 fHWDitherEnabled = kNo_TriState;
reed@google.comac10a2d2010-12-22 21:39:39 +00002255 }
2256 }
2257
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002258 if (drawState.isColorWriteDisabled()) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002259 if (kNo_TriState != fHWWriteToColor) {
2260 GL_CALL(ColorMask(GR_GL_FALSE, GR_GL_FALSE,
2261 GR_GL_FALSE, GR_GL_FALSE));
2262 fHWWriteToColor = kNo_TriState;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002263 }
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002264 } else {
2265 if (kYes_TriState != fHWWriteToColor) {
2266 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
2267 fHWWriteToColor = kYes_TriState;
2268 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00002269 }
2270
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002271 if (fHWDrawFace != drawState.getDrawFace()) {
bsalomon@google.coma5d056a2012-03-27 15:59:58 +00002272 switch (this->getDrawState().getDrawFace()) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002273 case GrDrawState::kCCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002274 GL_CALL(Enable(GR_GL_CULL_FACE));
2275 GL_CALL(CullFace(GR_GL_BACK));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002276 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002277 case GrDrawState::kCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002278 GL_CALL(Enable(GR_GL_CULL_FACE));
2279 GL_CALL(CullFace(GR_GL_FRONT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002280 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002281 case GrDrawState::kBoth_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002282 GL_CALL(Disable(GR_GL_CULL_FACE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002283 break;
2284 default:
2285 GrCrash("Unknown draw face.");
2286 }
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002287 fHWDrawFace = drawState.getDrawFace();
bsalomon@google.comd302f142011-03-03 13:54:13 +00002288 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002289}
2290
reed@google.comac10a2d2010-12-22 21:39:39 +00002291void GrGpuGL::notifyRenderTargetDelete(GrRenderTarget* renderTarget) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002292 SkASSERT(NULL != renderTarget);
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002293 if (fHWBoundRenderTarget == renderTarget) {
2294 fHWBoundRenderTarget = NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +00002295 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002296}
2297
2298void GrGpuGL::notifyTextureDelete(GrGLTexture* texture) {
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002299 for (int s = 0; s < fHWBoundTextures.count(); ++s) {
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002300 if (fHWBoundTextures[s] == texture) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002301 // deleting bound texture does implied bind to 0
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002302 fHWBoundTextures[s] = NULL;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002303 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002304 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002305}
2306
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002307bool GrGpuGL::configToGLFormats(GrPixelConfig config,
2308 bool getSizedInternalFormat,
2309 GrGLenum* internalFormat,
2310 GrGLenum* externalFormat,
2311 GrGLenum* externalType) {
2312 GrGLenum dontCare;
2313 if (NULL == internalFormat) {
2314 internalFormat = &dontCare;
2315 }
2316 if (NULL == externalFormat) {
2317 externalFormat = &dontCare;
2318 }
2319 if (NULL == externalType) {
2320 externalType = &dontCare;
2321 }
2322
reed@google.comac10a2d2010-12-22 21:39:39 +00002323 switch (config) {
bsalomon@google.com0342a852012-08-20 19:22:38 +00002324 case kRGBA_8888_GrPixelConfig:
bsalomon@google.comc4364992011-11-07 15:54:49 +00002325 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002326 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002327 if (getSizedInternalFormat) {
2328 *internalFormat = GR_GL_RGBA8;
2329 } else {
2330 *internalFormat = GR_GL_RGBA;
2331 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002332 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.comc4364992011-11-07 15:54:49 +00002333 break;
bsalomon@google.com0342a852012-08-20 19:22:38 +00002334 case kBGRA_8888_GrPixelConfig:
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002335 if (!this->glCaps().bgraFormatSupport()) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002336 return false;
2337 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002338 if (this->glCaps().bgraIsInternalFormat()) {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002339 if (getSizedInternalFormat) {
2340 *internalFormat = GR_GL_BGRA8;
2341 } else {
2342 *internalFormat = GR_GL_BGRA;
2343 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002344 } else {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002345 if (getSizedInternalFormat) {
2346 *internalFormat = GR_GL_RGBA8;
2347 } else {
2348 *internalFormat = GR_GL_RGBA;
2349 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002350 }
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002351 *externalFormat = GR_GL_BGRA;
bsalomon@google.com6f379512011-11-16 20:36:03 +00002352 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002353 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002354 case kRGB_565_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002355 *internalFormat = GR_GL_RGB;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002356 *externalFormat = GR_GL_RGB;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002357 if (getSizedInternalFormat) {
2358 if (this->glBinding() == kDesktop_GrGLBinding) {
2359 return false;
2360 } else {
2361 *internalFormat = GR_GL_RGB565;
2362 }
2363 } else {
2364 *internalFormat = GR_GL_RGB;
2365 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002366 *externalType = GR_GL_UNSIGNED_SHORT_5_6_5;
reed@google.comac10a2d2010-12-22 21:39:39 +00002367 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002368 case kRGBA_4444_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002369 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002370 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002371 if (getSizedInternalFormat) {
2372 *internalFormat = GR_GL_RGBA4;
2373 } else {
2374 *internalFormat = GR_GL_RGBA;
2375 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002376 *externalType = GR_GL_UNSIGNED_SHORT_4_4_4_4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002377 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002378 case kIndex_8_GrPixelConfig:
bsalomon@google.combcce8922013-03-25 15:38:39 +00002379 if (this->caps()->eightBitPaletteSupport()) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002380 *internalFormat = GR_GL_PALETTE8_RGBA8;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002381 // glCompressedTexImage doesn't take external params
2382 *externalFormat = GR_GL_PALETTE8_RGBA8;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002383 // no sized/unsized internal format distinction here
2384 *internalFormat = GR_GL_PALETTE8_RGBA8;
2385 // unused with CompressedTexImage
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002386 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002387 } else {
2388 return false;
2389 }
2390 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002391 case kAlpha_8_GrPixelConfig:
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002392 if (this->glCaps().textureRedSupport()) {
2393 *internalFormat = GR_GL_RED;
2394 *externalFormat = GR_GL_RED;
2395 if (getSizedInternalFormat) {
2396 *internalFormat = GR_GL_R8;
2397 } else {
2398 *internalFormat = GR_GL_RED;
2399 }
2400 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002401 } else {
2402 *internalFormat = GR_GL_ALPHA;
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002403 *externalFormat = GR_GL_ALPHA;
2404 if (getSizedInternalFormat) {
2405 *internalFormat = GR_GL_ALPHA8;
2406 } else {
2407 *internalFormat = GR_GL_ALPHA;
2408 }
2409 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002410 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002411 break;
2412 default:
2413 return false;
2414 }
2415 return true;
2416}
2417
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002418void GrGpuGL::setTextureUnit(int unit) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002419 SkASSERT(unit >= 0 && unit < fHWBoundTextures.count());
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002420 if (unit != fHWActiveTextureUnitIdx) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002421 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com49209392012-06-05 15:13:46 +00002422 fHWActiveTextureUnitIdx = unit;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002423 }
2424}
bsalomon@google.com316f99232011-01-13 21:28:12 +00002425
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002426void GrGpuGL::setScratchTextureUnit() {
2427 // Bind the last texture unit since it is the least likely to be used by GrGLProgram.
2428 int lastUnitIdx = fHWBoundTextures.count() - 1;
2429 if (lastUnitIdx != fHWActiveTextureUnitIdx) {
2430 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + lastUnitIdx));
2431 fHWActiveTextureUnitIdx = lastUnitIdx;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002432 }
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002433 // clear out the this field so that if a program does use this unit it will rebind the correct
2434 // texture.
2435 fHWBoundTextures[lastUnitIdx] = NULL;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002436}
2437
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002438namespace {
2439// Determines whether glBlitFramebuffer could be used between src and dst.
bsalomon@google.comeb851172013-04-15 13:51:00 +00002440inline bool can_blit_framebuffer(const GrSurface* dst,
2441 const GrSurface* src,
2442 const GrGpuGL* gpu,
2443 bool* wouldNeedTempFBO = NULL) {
bsalomon@google.com347c3822013-05-01 20:10:01 +00002444 if (gpu->isConfigRenderable(dst->config()) &&
2445 gpu->isConfigRenderable(src->config()) &&
2446 gpu->glCaps().usesMSAARenderBuffers()) {
commit-bot@chromium.orga8e5a062013-09-05 23:44:09 +00002447 // ES3 doesn't allow framebuffer blits when the src has MSAA and the configs don't match
2448 // or the rects are not the same (not just the same size but have the same edges).
2449 if (GrGLCaps::kES_3_0_MSFBOType == gpu->glCaps().msFBOType() &&
2450 (src->desc().fSampleCnt > 0 || src->config() != dst->config())) {
2451 return false;
2452 }
bsalomon@google.comeb851172013-04-15 13:51:00 +00002453 if (NULL != wouldNeedTempFBO) {
2454 *wouldNeedTempFBO = NULL == dst->asRenderTarget() || NULL == src->asRenderTarget();
2455 }
2456 return true;
2457 } else {
2458 return false;
2459 }
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002460}
bsalomon@google.comeb851172013-04-15 13:51:00 +00002461
2462inline bool can_copy_texsubimage(const GrSurface* dst,
2463 const GrSurface* src,
2464 const GrGpuGL* gpu,
2465 bool* wouldNeedTempFBO = NULL) {
2466 // Table 3.9 of the ES2 spec indicates the supported formats with CopyTexSubImage
2467 // and BGRA isn't in the spec. There doesn't appear to be any extension that adds it. Perhaps
2468 // many drivers would allow it to work, but ANGLE does not.
bsalomon@google.com791816a2013-08-15 18:54:39 +00002469 if (kES_GrGLBinding == gpu->glBinding() && gpu->glCaps().bgraIsInternalFormat() &&
bsalomon@google.comeb851172013-04-15 13:51:00 +00002470 (kBGRA_8888_GrPixelConfig == dst->config() || kBGRA_8888_GrPixelConfig == src->config())) {
2471 return false;
2472 }
2473 const GrGLRenderTarget* dstRT = static_cast<const GrGLRenderTarget*>(dst->asRenderTarget());
2474 // If dst is multisampled (and uses an extension where there is a separate MSAA renderbuffer)
2475 // then we don't want to copy to the texture but to the MSAA buffer.
2476 if (NULL != dstRT && dstRT->renderFBOID() != dstRT->textureFBOID()) {
2477 return false;
2478 }
bsalomon@google.coma2719852013-04-17 14:25:27 +00002479 const GrGLRenderTarget* srcRT = static_cast<const GrGLRenderTarget*>(src->asRenderTarget());
2480 // If the src is multisampled (and uses an extension where there is a separate MSAA
2481 // renderbuffer) then it is an invalid operation to call CopyTexSubImage
2482 if (NULL != srcRT && srcRT->renderFBOID() != srcRT->textureFBOID()) {
2483 return false;
2484 }
bsalomon@google.comeb851172013-04-15 13:51:00 +00002485 if (gpu->isConfigRenderable(src->config()) && NULL != dst->asTexture() &&
2486 dst->origin() == src->origin() && kIndex_8_GrPixelConfig != src->config()) {
2487 if (NULL != wouldNeedTempFBO) {
2488 *wouldNeedTempFBO = NULL == src->asRenderTarget();
2489 }
2490 return true;
2491 } else {
2492 return false;
2493 }
2494}
2495
2496// If a temporary FBO was created, its non-zero ID is returned. The viewport that the copy rect is
2497// relative to is output.
2498inline GrGLuint bind_surface_as_fbo(const GrGLInterface* gl,
2499 GrSurface* surface,
2500 GrGLenum fboTarget,
2501 GrGLIRect* viewport) {
2502 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(surface->asRenderTarget());
2503 GrGLuint tempFBOID;
2504 if (NULL == rt) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002505 SkASSERT(NULL != surface->asTexture());
bsalomon@google.comeb851172013-04-15 13:51:00 +00002506 GrGLuint texID = static_cast<GrGLTexture*>(surface->asTexture())->textureID();
2507 GR_GL_CALL(gl, GenFramebuffers(1, &tempFBOID));
2508 GR_GL_CALL(gl, BindFramebuffer(fboTarget, tempFBOID));
2509 GR_GL_CALL(gl, FramebufferTexture2D(fboTarget,
2510 GR_GL_COLOR_ATTACHMENT0,
2511 GR_GL_TEXTURE_2D,
2512 texID,
2513 0));
2514 viewport->fLeft = 0;
2515 viewport->fBottom = 0;
2516 viewport->fWidth = surface->width();
2517 viewport->fHeight = surface->height();
2518 } else {
2519 tempFBOID = 0;
2520 GR_GL_CALL(gl, BindFramebuffer(fboTarget, rt->renderFBOID()));
2521 *viewport = rt->getViewport();
2522 }
2523 return tempFBOID;
2524}
2525
2526}
2527
2528void GrGpuGL::initCopySurfaceDstDesc(const GrSurface* src, GrTextureDesc* desc) {
2529 // Check for format issues with glCopyTexSubImage2D
bsalomon@google.com791816a2013-08-15 18:54:39 +00002530 if (kES_GrGLBinding == this->glBinding() && this->glCaps().bgraIsInternalFormat() &&
bsalomon@google.comeb851172013-04-15 13:51:00 +00002531 kBGRA_8888_GrPixelConfig == src->config()) {
2532 // glCopyTexSubImage2D doesn't work with this config. We'll want to make it a render target
bsalomon@google.com31c4e892013-04-15 13:55:02 +00002533 // in order to call glBlitFramebuffer or to copy to it by rendering.
bsalomon@google.comeb851172013-04-15 13:51:00 +00002534 INHERITED::initCopySurfaceDstDesc(src, desc);
bsalomon@google.coma2719852013-04-17 14:25:27 +00002535 return;
bsalomon@google.comeb851172013-04-15 13:51:00 +00002536 } else if (NULL == src->asRenderTarget()) {
2537 // We don't want to have to create an FBO just to use glCopyTexSubImage2D. Let the base
2538 // class handle it by rendering.
2539 INHERITED::initCopySurfaceDstDesc(src, desc);
bsalomon@google.coma2719852013-04-17 14:25:27 +00002540 return;
2541 }
2542
2543 const GrGLRenderTarget* srcRT = static_cast<const GrGLRenderTarget*>(src->asRenderTarget());
2544 if (NULL != srcRT && srcRT->renderFBOID() != srcRT->textureFBOID()) {
2545 // It's illegal to call CopyTexSubImage2D on a MSAA renderbuffer.
2546 INHERITED::initCopySurfaceDstDesc(src, desc);
bsalomon@google.comeb851172013-04-15 13:51:00 +00002547 } else {
2548 desc->fConfig = src->config();
2549 desc->fOrigin = src->origin();
2550 desc->fFlags = kNone_GrTextureFlags;
2551 }
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002552}
2553
2554bool GrGpuGL::onCopySurface(GrSurface* dst,
2555 GrSurface* src,
2556 const SkIRect& srcRect,
2557 const SkIPoint& dstPoint) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002558 bool inheritedCouldCopy = INHERITED::onCanCopySurface(dst, src, srcRect, dstPoint);
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002559 bool copied = false;
bsalomon@google.comeb851172013-04-15 13:51:00 +00002560 bool wouldNeedTempFBO = false;
2561 if (can_copy_texsubimage(dst, src, this, &wouldNeedTempFBO) &&
2562 (!wouldNeedTempFBO || !inheritedCouldCopy)) {
2563 GrGLuint srcFBO;
2564 GrGLIRect srcVP;
2565 srcFBO = bind_surface_as_fbo(this->glInterface(), src, GR_GL_FRAMEBUFFER, &srcVP);
2566 GrGLTexture* dstTex = static_cast<GrGLTexture*>(dst->asTexture());
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002567 SkASSERT(NULL != dstTex);
bsalomon@google.comeb851172013-04-15 13:51:00 +00002568 // We modified the bound FBO
2569 fHWBoundRenderTarget = NULL;
2570 GrGLIRect srcGLRect;
2571 srcGLRect.setRelativeTo(srcVP,
2572 srcRect.fLeft,
2573 srcRect.fTop,
2574 srcRect.width(),
2575 srcRect.height(),
2576 src->origin());
2577
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002578 this->setScratchTextureUnit();
bsalomon@google.comeb851172013-04-15 13:51:00 +00002579 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, dstTex->textureID()));
2580 GrGLint dstY;
2581 if (kBottomLeft_GrSurfaceOrigin == dst->origin()) {
2582 dstY = dst->height() - (dstPoint.fY + srcGLRect.fHeight);
2583 } else {
2584 dstY = dstPoint.fY;
2585 }
2586 GL_CALL(CopyTexSubImage2D(GR_GL_TEXTURE_2D, 0,
2587 dstPoint.fX, dstY,
2588 srcGLRect.fLeft, srcGLRect.fBottom,
2589 srcGLRect.fWidth, srcGLRect.fHeight));
2590 copied = true;
2591 if (srcFBO) {
2592 GL_CALL(DeleteFramebuffers(1, &srcFBO));
2593 }
2594 } else if (can_blit_framebuffer(dst, src, this, &wouldNeedTempFBO) &&
2595 (!wouldNeedTempFBO || !inheritedCouldCopy)) {
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002596 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
2597 srcRect.width(), srcRect.height());
2598 bool selfOverlap = false;
2599 if (dst->isSameAs(src)) {
2600 selfOverlap = SkIRect::IntersectsNoEmptyCheck(dstRect, srcRect);
2601 }
2602
2603 if (!selfOverlap) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002604 GrGLuint dstFBO;
2605 GrGLuint srcFBO;
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002606 GrGLIRect dstVP;
2607 GrGLIRect srcVP;
bsalomon@google.comeb851172013-04-15 13:51:00 +00002608 dstFBO = bind_surface_as_fbo(this->glInterface(), dst, GR_GL_DRAW_FRAMEBUFFER, &dstVP);
2609 srcFBO = bind_surface_as_fbo(this->glInterface(), src, GR_GL_READ_FRAMEBUFFER, &srcVP);
2610 // We modified the bound FBO
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002611 fHWBoundRenderTarget = NULL;
2612 GrGLIRect srcGLRect;
2613 GrGLIRect dstGLRect;
2614 srcGLRect.setRelativeTo(srcVP,
2615 srcRect.fLeft,
2616 srcRect.fTop,
2617 srcRect.width(),
2618 srcRect.height(),
2619 src->origin());
2620 dstGLRect.setRelativeTo(dstVP,
2621 dstRect.fLeft,
2622 dstRect.fTop,
2623 dstRect.width(),
2624 dstRect.height(),
2625 dst->origin());
2626
2627 GrAutoTRestore<ScissorState> asr;
bsalomon@google.com347c3822013-05-01 20:10:01 +00002628 if (GrGLCaps::kDesktop_EXT_MSFBOType == this->glCaps().msFBOType()) {
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002629 // The EXT version applies the scissor during the blit, so disable it.
2630 asr.reset(&fScissorState);
2631 fScissorState.fEnabled = false;
2632 this->flushScissor();
2633 }
2634 GrGLint srcY0;
2635 GrGLint srcY1;
2636 // Does the blit need to y-mirror or not?
2637 if (src->origin() == dst->origin()) {
2638 srcY0 = srcGLRect.fBottom;
2639 srcY1 = srcGLRect.fBottom + srcGLRect.fHeight;
2640 } else {
2641 srcY0 = srcGLRect.fBottom + srcGLRect.fHeight;
2642 srcY1 = srcGLRect.fBottom;
2643 }
2644 GL_CALL(BlitFramebuffer(srcGLRect.fLeft,
2645 srcY0,
2646 srcGLRect.fLeft + srcGLRect.fWidth,
2647 srcY1,
2648 dstGLRect.fLeft,
2649 dstGLRect.fBottom,
2650 dstGLRect.fLeft + dstGLRect.fWidth,
2651 dstGLRect.fBottom + dstGLRect.fHeight,
2652 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
2653 if (dstFBO) {
2654 GL_CALL(DeleteFramebuffers(1, &dstFBO));
2655 }
2656 if (srcFBO) {
2657 GL_CALL(DeleteFramebuffers(1, &srcFBO));
2658 }
2659 copied = true;
2660 }
2661 }
bsalomon@google.comeb851172013-04-15 13:51:00 +00002662 if (!copied && inheritedCouldCopy) {
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002663 copied = INHERITED::onCopySurface(dst, src, srcRect, dstPoint);
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002664 SkASSERT(copied);
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002665 }
2666 return copied;
2667}
2668
2669bool GrGpuGL::onCanCopySurface(GrSurface* dst,
2670 GrSurface* src,
2671 const SkIRect& srcRect,
2672 const SkIPoint& dstPoint) {
2673 // This mirrors the logic in onCopySurface.
bsalomon@google.comeb851172013-04-15 13:51:00 +00002674 if (can_copy_texsubimage(dst, src, this)) {
2675 return true;
2676 }
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002677 if (can_blit_framebuffer(dst, src, this)) {
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002678 if (dst->isSameAs(src)) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002679 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
2680 srcRect.width(), srcRect.height());
2681 if(!SkIRect::IntersectsNoEmptyCheck(dstRect, srcRect)) {
2682 return true;
2683 }
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002684 } else {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002685 return true;
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002686 }
2687 }
bsalomon@google.comeb851172013-04-15 13:51:00 +00002688 return INHERITED::onCanCopySurface(dst, src, srcRect, dstPoint);
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002689}
2690
2691
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00002692///////////////////////////////////////////////////////////////////////////////
2693
bsalomon@google.com6918d482013-03-07 19:09:11 +00002694GrGLAttribArrayState* GrGpuGL::HWGeometryState::bindArrayAndBuffersToDraw(
2695 GrGpuGL* gpu,
2696 const GrGLVertexBuffer* vbuffer,
2697 const GrGLIndexBuffer* ibuffer) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002698 SkASSERT(NULL != vbuffer);
robertphillips@google.com4f65a272013-03-26 19:40:46 +00002699 GrGLAttribArrayState* attribState;
2700
bsalomon@google.com6918d482013-03-07 19:09:11 +00002701 // We use a vertex array if we're on a core profile and the verts are in a VBO.
2702 if (gpu->glCaps().isCoreProfile() && !vbuffer->isCPUBacked()) {
2703 if (NULL == fVBOVertexArray || !fVBOVertexArray->isValid()) {
2704 SkSafeUnref(fVBOVertexArray);
2705 GrGLuint arrayID;
2706 GR_GL_CALL(gpu->glInterface(), GenVertexArrays(1, &arrayID));
2707 int attrCount = gpu->glCaps().maxVertexAttributes();
2708 fVBOVertexArray = SkNEW_ARGS(GrGLVertexArray, (gpu, arrayID, attrCount));
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00002709 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00002710 attribState = fVBOVertexArray->bindWithIndexBuffer(ibuffer);
2711 } else {
2712 if (NULL != ibuffer) {
2713 this->setIndexBufferIDOnDefaultVertexArray(gpu, ibuffer->bufferID());
2714 } else {
2715 this->setVertexArrayID(gpu, 0);
2716 }
2717 int attrCount = gpu->glCaps().maxVertexAttributes();
2718 if (fDefaultVertexArrayAttribState.count() != attrCount) {
2719 fDefaultVertexArrayAttribState.resize(attrCount);
2720 }
2721 attribState = &fDefaultVertexArrayAttribState;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00002722 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00002723 return attribState;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002724}