blob: 2eb76a5aff43137e69a0d2461efa0f2a1ec0ffb9 [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"
cdalton51192342014-06-09 11:16:58 -070010#include "GrGLNameAllocator.h"
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000011#include "GrGLStencilBuffer.h"
bsalomon@google.com64aef2b2012-06-11 15:36:13 +000012#include "GrGLPath.h"
bsalomon@google.com6d003d12012-09-11 15:45:20 +000013#include "GrGLShaderBuilder.h"
bsalomon@google.coma3201942012-06-21 19:58:20 +000014#include "GrTemplates.h"
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +000015#include "GrTypes.h"
commit-bot@chromium.org32184d82013-10-09 15:14:18 +000016#include "SkStrokeRec.h"
bsalomon@google.com3582bf92011-06-30 21:32:31 +000017#include "SkTemplates.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000018
bsalomon@google.com0b77d682011-08-19 13:28:54 +000019#define GL_CALL(X) GR_GL_CALL(this->glInterface(), X)
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +000020#define GL_CALL_RET(RET, X) GR_GL_CALL_RET(this->glInterface(), RET, X)
bsalomon@google.com0b77d682011-08-19 13:28:54 +000021
reed@google.comac10a2d2010-12-22 21:39:39 +000022#define SKIP_CACHE_CHECK true
23
bsalomon@google.com4f3c2532012-01-19 16:16:52 +000024#if GR_GL_CHECK_ALLOC_WITH_GET_ERROR
25 #define CLEAR_ERROR_BEFORE_ALLOC(iface) GrGLClearErr(iface)
26 #define GL_ALLOC_CALL(iface, call) GR_GL_CALL_NOERRCHECK(iface, call)
27 #define CHECK_ALLOC_ERROR(iface) GR_GL_GET_ERROR(iface)
rmistry@google.comfbfcd562012-08-23 18:09:54 +000028#else
bsalomon@google.com4f3c2532012-01-19 16:16:52 +000029 #define CLEAR_ERROR_BEFORE_ALLOC(iface)
30 #define GL_ALLOC_CALL(iface, call) GR_GL_CALL(iface, call)
31 #define CHECK_ALLOC_ERROR(iface) GR_GL_NO_ERROR
32#endif
33
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +000034
35///////////////////////////////////////////////////////////////////////////////
36
twiz@google.com0f31ca72011-03-18 17:38:11 +000037static const GrGLenum gXfermodeCoeff2Blend[] = {
38 GR_GL_ZERO,
39 GR_GL_ONE,
40 GR_GL_SRC_COLOR,
41 GR_GL_ONE_MINUS_SRC_COLOR,
42 GR_GL_DST_COLOR,
43 GR_GL_ONE_MINUS_DST_COLOR,
44 GR_GL_SRC_ALPHA,
45 GR_GL_ONE_MINUS_SRC_ALPHA,
46 GR_GL_DST_ALPHA,
47 GR_GL_ONE_MINUS_DST_ALPHA,
48 GR_GL_CONSTANT_COLOR,
49 GR_GL_ONE_MINUS_CONSTANT_COLOR,
50 GR_GL_CONSTANT_ALPHA,
51 GR_GL_ONE_MINUS_CONSTANT_ALPHA,
bsalomon@google.com271cffc2011-05-20 14:13:56 +000052
53 // extended blend coeffs
54 GR_GL_SRC1_COLOR,
55 GR_GL_ONE_MINUS_SRC1_COLOR,
56 GR_GL_SRC1_ALPHA,
57 GR_GL_ONE_MINUS_SRC1_ALPHA,
reed@google.comac10a2d2010-12-22 21:39:39 +000058};
59
bsalomon@google.com271cffc2011-05-20 14:13:56 +000060bool GrGpuGL::BlendCoeffReferencesConstant(GrBlendCoeff coeff) {
bsalomon@google.com080773c2011-03-15 19:09:25 +000061 static const bool gCoeffReferencesBlendConst[] = {
62 false,
63 false,
64 false,
65 false,
66 false,
67 false,
68 false,
69 false,
70 false,
71 false,
72 true,
73 true,
74 true,
75 true,
bsalomon@google.com271cffc2011-05-20 14:13:56 +000076
77 // extended blend coeffs
78 false,
79 false,
80 false,
81 false,
bsalomon@google.com080773c2011-03-15 19:09:25 +000082 };
83 return gCoeffReferencesBlendConst[coeff];
bsalomon@google.com47059542012-06-06 20:51:20 +000084 GR_STATIC_ASSERT(kTotalGrBlendCoeffCount ==
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +000085 SK_ARRAY_COUNT(gCoeffReferencesBlendConst));
bsalomon@google.com271cffc2011-05-20 14:13:56 +000086
bsalomon@google.com47059542012-06-06 20:51:20 +000087 GR_STATIC_ASSERT(0 == kZero_GrBlendCoeff);
88 GR_STATIC_ASSERT(1 == kOne_GrBlendCoeff);
89 GR_STATIC_ASSERT(2 == kSC_GrBlendCoeff);
90 GR_STATIC_ASSERT(3 == kISC_GrBlendCoeff);
91 GR_STATIC_ASSERT(4 == kDC_GrBlendCoeff);
92 GR_STATIC_ASSERT(5 == kIDC_GrBlendCoeff);
93 GR_STATIC_ASSERT(6 == kSA_GrBlendCoeff);
94 GR_STATIC_ASSERT(7 == kISA_GrBlendCoeff);
95 GR_STATIC_ASSERT(8 == kDA_GrBlendCoeff);
96 GR_STATIC_ASSERT(9 == kIDA_GrBlendCoeff);
97 GR_STATIC_ASSERT(10 == kConstC_GrBlendCoeff);
98 GR_STATIC_ASSERT(11 == kIConstC_GrBlendCoeff);
99 GR_STATIC_ASSERT(12 == kConstA_GrBlendCoeff);
100 GR_STATIC_ASSERT(13 == kIConstA_GrBlendCoeff);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000101
bsalomon@google.com47059542012-06-06 20:51:20 +0000102 GR_STATIC_ASSERT(14 == kS2C_GrBlendCoeff);
103 GR_STATIC_ASSERT(15 == kIS2C_GrBlendCoeff);
104 GR_STATIC_ASSERT(16 == kS2A_GrBlendCoeff);
105 GR_STATIC_ASSERT(17 == kIS2A_GrBlendCoeff);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000106
107 // assertion for gXfermodeCoeff2Blend have to be in GrGpu scope
bsalomon@google.com47059542012-06-06 20:51:20 +0000108 GR_STATIC_ASSERT(kTotalGrBlendCoeffCount ==
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000109 SK_ARRAY_COUNT(gXfermodeCoeff2Blend));
bsalomon@google.com080773c2011-03-15 19:09:25 +0000110}
111
reed@google.comac10a2d2010-12-22 21:39:39 +0000112///////////////////////////////////////////////////////////////////////////////
113
rileya@google.come38160c2012-07-03 18:03:04 +0000114static bool gPrintStartupSpew;
bsalomon@google.com42ab7ea2011-01-19 17:19:40 +0000115
robertphillips@google.com6177e692013-02-28 20:16:25 +0000116GrGpuGL::GrGpuGL(const GrGLContext& ctx, GrContext* context)
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000117 : GrGpu(context)
robertphillips@google.com6177e692013-02-28 20:16:25 +0000118 , fGLContext(ctx) {
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000119
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000120 SkASSERT(ctx.isInitialized());
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +0000121 fCaps.reset(SkRef(ctx.caps()));
bsalomon@google.combcce8922013-03-25 15:38:39 +0000122
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +0000123 fHWBoundTextures.reset(this->glCaps().maxFragmentTextureUnits());
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000124 fHWPathTexGenSettings.reset(this->glCaps().maxFixedFunctionTextureCoords());
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +0000125
robertphillips@google.com6177e692013-02-28 20:16:25 +0000126 GrGLClearErr(fGLContext.interface());
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000127 if (gPrintStartupSpew) {
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000128 const GrGLubyte* vendor;
129 const GrGLubyte* renderer;
130 const GrGLubyte* version;
131 GL_CALL_RET(vendor, GetString(GR_GL_VENDOR));
132 GL_CALL_RET(renderer, GetString(GR_GL_RENDERER));
133 GL_CALL_RET(version, GetString(GR_GL_VERSION));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000134 GrPrintf("------------------------- create GrGpuGL %p --------------\n",
135 this);
136 GrPrintf("------ VENDOR %s\n", vendor);
137 GrPrintf("------ RENDERER %s\n", renderer);
138 GrPrintf("------ VERSION %s\n", version);
bsalomon@google.com00142c42013-05-02 19:42:54 +0000139 GrPrintf("------ EXTENSIONS\n");
commit-bot@chromium.org90313cc2014-01-17 15:05:38 +0000140#if 0 // TODO: Reenable this after GrGLInterface's extensions can be accessed safely.
141 ctx.extensions().print();
142#endif
bsalomon@google.com00142c42013-05-02 19:42:54 +0000143 GrPrintf("\n");
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +0000144 GrPrintf(this->glCaps().dump().c_str());
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000145 }
146
commit-bot@chromium.org9188a152013-09-05 18:28:24 +0000147 fProgramCache = SkNEW_ARGS(ProgramCache, (this));
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000148
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000149 SkASSERT(this->glCaps().maxVertexAttributes() >= GrDrawState::kMaxVertexAttribCnt);
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000150
bsalomon@google.comfe676522011-06-17 18:12:21 +0000151 fLastSuccessfulStencilFmtIdx = 0;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000152 fHWProgramID = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000153}
154
155GrGpuGL::~GrGpuGL() {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000156 if (0 != fHWProgramID) {
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000157 // detach the current program so there is no confusion on OpenGL's part
158 // that we want it to be deleted
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000159 SkASSERT(fHWProgramID == fCurrentProgram->programID());
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000160 GL_CALL(UseProgram(0));
161 }
162
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000163 delete fProgramCache;
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000164
bsalomon@google.com4a018bb2011-10-28 19:50:21 +0000165 // This must be called by before the GrDrawTarget destructor
166 this->releaseGeometry();
bsalomon@google.com15b11df2011-09-16 21:18:29 +0000167 // This subclass must do this before the base class destructor runs
168 // since we will unref the GrGLInterface.
169 this->releaseResources();
reed@google.comac10a2d2010-12-22 21:39:39 +0000170}
171
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000172///////////////////////////////////////////////////////////////////////////////
173
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000174
commit-bot@chromium.org28621512013-08-07 19:43:45 +0000175GrPixelConfig GrGpuGL::preferredReadPixelsConfig(GrPixelConfig readConfig,
176 GrPixelConfig surfaceConfig) const {
177 if (GR_GL_RGBA_8888_PIXEL_OPS_SLOW && kRGBA_8888_GrPixelConfig == readConfig) {
bsalomon@google.com9c680582013-02-06 18:17:50 +0000178 return kBGRA_8888_GrPixelConfig;
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +0000179 } else if (this->glContext().isMesa() &&
commit-bot@chromium.org28621512013-08-07 19:43:45 +0000180 GrBytesPerPixel(readConfig) == 4 &&
181 GrPixelConfigSwapRAndB(readConfig) == surfaceConfig) {
commit-bot@chromium.org5d1d79a2013-05-24 18:52:52 +0000182 // Mesa 3D takes a slow path on when reading back BGRA from an RGBA surface and vice-versa.
183 // Perhaps this should be guarded by some compiletime or runtime check.
184 return surfaceConfig;
commit-bot@chromium.org28621512013-08-07 19:43:45 +0000185 } else if (readConfig == kBGRA_8888_GrPixelConfig &&
186 !this->glCaps().readPixelsSupported(this->glInterface(),
187 GR_GL_BGRA, GR_GL_UNSIGNED_BYTE)) {
188 return kRGBA_8888_GrPixelConfig;
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000189 } else {
commit-bot@chromium.org28621512013-08-07 19:43:45 +0000190 return readConfig;
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000191 }
192}
193
commit-bot@chromium.org5d1d79a2013-05-24 18:52:52 +0000194GrPixelConfig GrGpuGL::preferredWritePixelsConfig(GrPixelConfig writeConfig,
195 GrPixelConfig surfaceConfig) const {
commit-bot@chromium.org28621512013-08-07 19:43:45 +0000196 if (GR_GL_RGBA_8888_PIXEL_OPS_SLOW && kRGBA_8888_GrPixelConfig == writeConfig) {
197 return kBGRA_8888_GrPixelConfig;
198 } else {
199 return writeConfig;
200 }
bsalomon@google.com9c680582013-02-06 18:17:50 +0000201}
202
203bool GrGpuGL::canWriteTexturePixels(const GrTexture* texture, GrPixelConfig srcConfig) const {
krajcevski145d48c2014-06-11 16:07:50 -0700204 if (kIndex_8_GrPixelConfig == srcConfig || kIndex_8_GrPixelConfig == texture->config()) {
bsalomon@google.com9c680582013-02-06 18:17:50 +0000205 return false;
206 }
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +0000207 if (srcConfig != texture->config() && kGLES_GrGLStandard == this->glStandard()) {
bsalomon@google.com9c680582013-02-06 18:17:50 +0000208 // In general ES2 requires the internal format of the texture and the format of the src
209 // pixels to match. However, It may or may not be possible to upload BGRA data to a RGBA
210 // texture. It depends upon which extension added BGRA. The Apple extension allows it
211 // (BGRA's internal format is RGBA) while the EXT extension does not (BGRA is its own
212 // internal format).
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +0000213 if (this->glCaps().isConfigTexturable(kBGRA_8888_GrPixelConfig) &&
bsalomon@google.com9c680582013-02-06 18:17:50 +0000214 !this->glCaps().bgraIsInternalFormat() &&
215 kBGRA_8888_GrPixelConfig == srcConfig &&
216 kRGBA_8888_GrPixelConfig == texture->config()) {
217 return true;
218 } else {
219 return false;
220 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000221 } else {
bsalomon@google.com9c680582013-02-06 18:17:50 +0000222 return true;
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000223 }
224}
225
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000226bool GrGpuGL::fullReadPixelsIsFasterThanPartial() const {
227 return SkToBool(GR_GL_FULL_READPIXELS_FASTER_THAN_PARTIAL);
228}
229
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000230void GrGpuGL::onResetContext(uint32_t resetBits) {
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000231 // we don't use the zb at all
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000232 if (resetBits & kMisc_GrGLBackendState) {
233 GL_CALL(Disable(GR_GL_DEPTH_TEST));
234 GL_CALL(DepthMask(GR_GL_FALSE));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000235
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000236 fHWDrawFace = GrDrawState::kInvalid_DrawFace;
237 fHWDitherEnabled = kUnknown_TriState;
reed@google.comac10a2d2010-12-22 21:39:39 +0000238
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +0000239 if (kGL_GrGLStandard == this->glStandard()) {
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000240 // Desktop-only state that we never change
241 if (!this->glCaps().isCoreProfile()) {
242 GL_CALL(Disable(GR_GL_POINT_SMOOTH));
243 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
244 GL_CALL(Disable(GR_GL_POLYGON_SMOOTH));
245 GL_CALL(Disable(GR_GL_POLYGON_STIPPLE));
246 GL_CALL(Disable(GR_GL_COLOR_LOGIC_OP));
247 GL_CALL(Disable(GR_GL_INDEX_LOGIC_OP));
248 }
249 // The windows NVIDIA driver has GL_ARB_imaging in the extension string when using a
250 // core profile. This seems like a bug since the core spec removes any mention of
251 // GL_ARB_imaging.
252 if (this->glCaps().imagingSupport() && !this->glCaps().isCoreProfile()) {
253 GL_CALL(Disable(GR_GL_COLOR_TABLE));
254 }
255 GL_CALL(Disable(GR_GL_POLYGON_OFFSET_FILL));
256 // Since ES doesn't support glPointSize at all we always use the VS to
257 // set the point size
258 GL_CALL(Enable(GR_GL_VERTEX_PROGRAM_POINT_SIZE));
259
260 // We should set glPolygonMode(FRONT_AND_BACK,FILL) here, too. It isn't
261 // currently part of our gl interface. There are probably others as
262 // well.
bsalomon@google.com2b1b8c02013-02-28 22:06:02 +0000263 }
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000264 fHWWriteToColor = kUnknown_TriState;
265 // we only ever use lines in hairline mode
266 GL_CALL(LineWidth(1));
bsalomon@google.comcad107b2013-06-28 14:32:08 +0000267 }
edisonn@google.comba669992013-06-28 16:03:21 +0000268
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000269 if (resetBits & kAA_GrGLBackendState) {
270 fHWAAState.invalidate();
271 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000272
commit-bot@chromium.org46fbfe02013-08-30 15:52:12 +0000273 fHWActiveTextureUnitIdx = -1; // invalid
274
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000275 if (resetBits & kTextureBinding_GrGLBackendState) {
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000276 for (int s = 0; s < fHWBoundTextures.count(); ++s) {
277 fHWBoundTextures[s] = NULL;
278 }
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000279 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000280
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000281 if (resetBits & kBlend_GrGLBackendState) {
282 fHWBlendState.invalidate();
283 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000284
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000285 if (resetBits & kView_GrGLBackendState) {
286 fHWScissorSettings.invalidate();
287 fHWViewport.invalidate();
288 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000289
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000290 if (resetBits & kStencil_GrGLBackendState) {
291 fHWStencilSettings.invalidate();
292 fHWStencilTestEnabled = kUnknown_TriState;
293 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000294
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000295 // Vertex
296 if (resetBits & kVertex_GrGLBackendState) {
297 fHWGeometryState.invalidate();
298 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000299
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000300 if (resetBits & kRenderTarget_GrGLBackendState) {
301 fHWBoundRenderTarget = NULL;
302 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000303
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000304 if (resetBits & kPathRendering_GrGLBackendState) {
305 if (this->caps()->pathRenderingSupport()) {
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000306 fHWProjectionMatrixState.invalidate();
307 // we don't use the model view matrix.
commit-bot@chromium.orgf6696722014-04-25 06:21:30 +0000308 GL_CALL(MatrixLoadIdentity(GR_GL_MODELVIEW));
commit-bot@chromium.org46fbfe02013-08-30 15:52:12 +0000309
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000310 for (int i = 0; i < this->glCaps().maxFixedFunctionTextureCoords(); ++i) {
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000311 GL_CALL(PathTexGen(GR_GL_TEXTURE0 + i, GR_GL_NONE, 0, NULL));
312 fHWPathTexGenSettings[i].fMode = GR_GL_NONE;
313 fHWPathTexGenSettings[i].fNumComponents = 0;
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000314 }
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000315 fHWActivePathTexGenSets = 0;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000316 }
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000317 fHWPathStencilSettings.invalidate();
bsalomon@google.com05a718c2012-06-29 14:01:53 +0000318 }
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000319
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000320 // we assume these values
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000321 if (resetBits & kPixelStore_GrGLBackendState) {
322 if (this->glCaps().unpackRowLengthSupport()) {
323 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
324 }
325 if (this->glCaps().packRowLengthSupport()) {
326 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
327 }
328 if (this->glCaps().unpackFlipYSupport()) {
329 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
330 }
331 if (this->glCaps().packFlipYSupport()) {
332 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, GR_GL_FALSE));
333 }
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000334 }
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000335
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000336 if (resetBits & kProgram_GrGLBackendState) {
337 fHWProgramID = 0;
338 fSharedGLProgramState.invalidate();
339 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000340}
341
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000342namespace {
343
344GrSurfaceOrigin resolve_origin(GrSurfaceOrigin origin, bool renderTarget) {
345 // By default, GrRenderTargets are GL's normal orientation so that they
346 // can be drawn to by the outside world without the client having
347 // to render upside down.
348 if (kDefault_GrSurfaceOrigin == origin) {
349 return renderTarget ? kBottomLeft_GrSurfaceOrigin : kTopLeft_GrSurfaceOrigin;
350 } else {
351 return origin;
352 }
353}
354
355}
356
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000357GrTexture* GrGpuGL::onWrapBackendTexture(const GrBackendTextureDesc& desc) {
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000358 if (!this->configToGLFormats(desc.fConfig, false, NULL, NULL, NULL)) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000359 return NULL;
360 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000361
robertphillips@google.comb72e5d32012-10-30 15:18:10 +0000362 if (0 == desc.fTextureHandle) {
363 return NULL;
364 }
365
bsalomon@google.combcce8922013-03-25 15:38:39 +0000366 int maxSize = this->caps()->maxTextureSize();
robertphillips@google.comb72e5d32012-10-30 15:18:10 +0000367 if (desc.fWidth > maxSize || desc.fHeight > maxSize) {
368 return NULL;
369 }
370
371 GrGLTexture::Desc glTexDesc;
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000372 // next line relies on GrBackendTextureDesc's flags matching GrTexture's
robertphillips@google.com32716282012-06-04 12:48:45 +0000373 glTexDesc.fFlags = (GrTextureFlags) desc.fFlags;
bsalomon@google.com99621082011-11-15 16:47:16 +0000374 glTexDesc.fWidth = desc.fWidth;
375 glTexDesc.fHeight = desc.fHeight;
bsalomon@google.come269f212011-11-07 13:29:52 +0000376 glTexDesc.fConfig = desc.fConfig;
robertphillips@google.com32716282012-06-04 12:48:45 +0000377 glTexDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.come269f212011-11-07 13:29:52 +0000378 glTexDesc.fTextureID = static_cast<GrGLuint>(desc.fTextureHandle);
bsalomon@google.com72830222013-01-23 20:25:22 +0000379 glTexDesc.fIsWrapped = true;
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000380 bool renderTarget = SkToBool(desc.fFlags & kRenderTarget_GrBackendTextureFlag);
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000381 // FIXME: this should be calling resolve_origin(), but Chrome code is currently
382 // assuming the old behaviour, which is that backend textures are always
383 // BottomLeft, even for non-RT's. Once Chrome is fixed, change this to:
384 // glTexDesc.fOrigin = resolve_origin(desc.fOrigin, renderTarget);
385 if (kDefault_GrSurfaceOrigin == desc.fOrigin) {
386 glTexDesc.fOrigin = kBottomLeft_GrSurfaceOrigin;
387 } else {
388 glTexDesc.fOrigin = desc.fOrigin;
389 }
bsalomon@google.come269f212011-11-07 13:29:52 +0000390
391 GrGLTexture* texture = NULL;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000392 if (renderTarget) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000393 GrGLRenderTarget::Desc glRTDesc;
394 glRTDesc.fRTFBOID = 0;
395 glRTDesc.fTexFBOID = 0;
396 glRTDesc.fMSColorRenderbufferID = 0;
bsalomon@google.come269f212011-11-07 13:29:52 +0000397 glRTDesc.fConfig = desc.fConfig;
398 glRTDesc.fSampleCnt = desc.fSampleCnt;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000399 glRTDesc.fOrigin = glTexDesc.fOrigin;
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000400 glRTDesc.fCheckAllocation = false;
bsalomon@google.com99621082011-11-15 16:47:16 +0000401 if (!this->createRenderTargetObjects(glTexDesc.fWidth,
402 glTexDesc.fHeight,
bsalomon@google.come269f212011-11-07 13:29:52 +0000403 glTexDesc.fTextureID,
404 &glRTDesc)) {
405 return NULL;
406 }
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000407 texture = SkNEW_ARGS(GrGLTexture, (this, glTexDesc, glRTDesc));
bsalomon@google.come269f212011-11-07 13:29:52 +0000408 } else {
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000409 texture = SkNEW_ARGS(GrGLTexture, (this, glTexDesc));
bsalomon@google.come269f212011-11-07 13:29:52 +0000410 }
411 if (NULL == texture) {
412 return NULL;
413 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000414
bsalomon@google.come269f212011-11-07 13:29:52 +0000415 return texture;
416}
417
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000418GrRenderTarget* GrGpuGL::onWrapBackendRenderTarget(const GrBackendRenderTargetDesc& desc) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000419 GrGLRenderTarget::Desc glDesc;
420 glDesc.fConfig = desc.fConfig;
421 glDesc.fRTFBOID = static_cast<GrGLuint>(desc.fRenderTargetHandle);
422 glDesc.fMSColorRenderbufferID = 0;
423 glDesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
424 glDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.com72830222013-01-23 20:25:22 +0000425 glDesc.fIsWrapped = true;
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000426 glDesc.fCheckAllocation = false;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000427
428 glDesc.fOrigin = resolve_origin(desc.fOrigin, true);
bsalomon@google.come269f212011-11-07 13:29:52 +0000429 GrGLIRect viewport;
430 viewport.fLeft = 0;
431 viewport.fBottom = 0;
432 viewport.fWidth = desc.fWidth;
433 viewport.fHeight = desc.fHeight;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000434
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000435 GrRenderTarget* tgt = SkNEW_ARGS(GrGLRenderTarget,
436 (this, glDesc, viewport));
bsalomon@google.come269f212011-11-07 13:29:52 +0000437 if (desc.fStencilBits) {
438 GrGLStencilBuffer::Format format;
439 format.fInternalFormat = GrGLStencilBuffer::kUnknownInternalFormat;
440 format.fPacked = false;
441 format.fStencilBits = desc.fStencilBits;
442 format.fTotalBits = desc.fStencilBits;
bsalomon@google.com1f0f1a32013-01-23 21:32:32 +0000443 static const bool kIsSBWrapped = false;
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000444 GrGLStencilBuffer* sb = SkNEW_ARGS(GrGLStencilBuffer,
445 (this,
bsalomon@google.com1f0f1a32013-01-23 21:32:32 +0000446 kIsSBWrapped,
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000447 0,
448 desc.fWidth,
449 desc.fHeight,
450 desc.fSampleCnt,
451 format));
bsalomon@google.come269f212011-11-07 13:29:52 +0000452 tgt->setStencilBuffer(sb);
453 sb->unref();
454 }
455 return tgt;
456}
457
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000458////////////////////////////////////////////////////////////////////////////////
459
bsalomon@google.com9c680582013-02-06 18:17:50 +0000460bool GrGpuGL::onWriteTexturePixels(GrTexture* texture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000461 int left, int top, int width, int height,
462 GrPixelConfig config, const void* buffer,
463 size_t rowBytes) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000464 if (NULL == buffer) {
bsalomon@google.com9c680582013-02-06 18:17:50 +0000465 return false;
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000466 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000467 GrGLTexture* glTex = static_cast<GrGLTexture*>(texture);
468
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +0000469 this->setScratchTextureUnit();
bsalomon@google.com6f379512011-11-16 20:36:03 +0000470 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTex->textureID()));
471 GrGLTexture::Desc desc;
robertphillips@google.com32716282012-06-04 12:48:45 +0000472 desc.fFlags = glTex->desc().fFlags;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000473 desc.fWidth = glTex->width();
474 desc.fHeight = glTex->height();
robertphillips@google.com32716282012-06-04 12:48:45 +0000475 desc.fConfig = glTex->config();
476 desc.fSampleCnt = glTex->desc().fSampleCnt;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000477 desc.fTextureID = glTex->textureID();
bsalomon@google.com2d0bade2012-10-26 19:01:17 +0000478 desc.fOrigin = glTex->origin();
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000479
krajcevski145d48c2014-06-11 16:07:50 -0700480 bool success = false;
481 if (GrPixelConfigIsCompressed(desc.fConfig)) {
482 // We check that config == desc.fConfig in GrGpuGL::canWriteTexturePixels()
483 SkASSERT(config == desc.fConfig);
484 success = this->uploadCompressedTexData(desc, buffer, false,
485 left, top, width, height);
486 } else {
487 success = this->uploadTexData(desc, false,
488 left, top, width, height,
489 config, buffer, rowBytes);
490 }
491
492 if (success) {
commit-bot@chromium.orge49157f2014-05-09 20:46:48 +0000493 texture->impl()->dirtyMipMaps(true);
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +0000494 return true;
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +0000495 }
krajcevski145d48c2014-06-11 16:07:50 -0700496
497 return false;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000498}
499
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000500namespace {
501bool adjust_pixel_ops_params(int surfaceWidth,
502 int surfaceHeight,
503 size_t bpp,
504 int* left, int* top, int* width, int* height,
505 const void** data,
506 size_t* rowBytes) {
507 if (!*rowBytes) {
508 *rowBytes = *width * bpp;
509 }
510
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000511 SkIRect subRect = SkIRect::MakeXYWH(*left, *top, *width, *height);
512 SkIRect bounds = SkIRect::MakeWH(surfaceWidth, surfaceHeight);
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000513
514 if (!subRect.intersect(bounds)) {
515 return false;
516 }
517 *data = reinterpret_cast<const void*>(reinterpret_cast<intptr_t>(*data) +
518 (subRect.fTop - *top) * *rowBytes + (subRect.fLeft - *left) * bpp);
519
520 *left = subRect.fLeft;
521 *top = subRect.fTop;
522 *width = subRect.width();
523 *height = subRect.height();
524 return true;
525}
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000526
527GrGLenum check_alloc_error(const GrTextureDesc& desc, const GrGLInterface* interface) {
528 if (SkToBool(desc.fFlags & kCheckAllocation_GrTextureFlagBit)) {
529 return GR_GL_GET_ERROR(interface);
530 } else {
531 return CHECK_ALLOC_ERROR(interface);
532 }
533}
534
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000535}
536
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000537bool GrGpuGL::uploadTexData(const GrGLTexture::Desc& desc,
538 bool isNewTexture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000539 int left, int top, int width, int height,
540 GrPixelConfig dataConfig,
541 const void* data,
542 size_t rowBytes) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000543 SkASSERT(NULL != data || isNewTexture);
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000544
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +0000545 // If we're uploading compressed data then we should be using uploadCompressedTexData
546 SkASSERT(!GrPixelConfigIsCompressed(dataConfig));
547
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000548 size_t bpp = GrBytesPerPixel(dataConfig);
549 if (!adjust_pixel_ops_params(desc.fWidth, desc.fHeight, bpp, &left, &top,
550 &width, &height, &data, &rowBytes)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000551 return false;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000552 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000553 size_t trimRowBytes = width * bpp;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000554
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000555 // in case we need a temporary, trimmed copy of the src pixels
556 SkAutoSMalloc<128 * 128> tempStorage;
557
bsalomon@google.com313f0192012-07-10 17:21:02 +0000558 // paletted textures cannot be partially updated
commit-bot@chromium.orgcea9abb2013-12-09 19:15:37 +0000559 // We currently lazily create MIPMAPs when the we see a draw with
560 // GrTextureParams::kMipMap_FilterMode. Using texture storage requires that the
561 // MIP levels are all created when the texture is created. So for now we don't use
562 // texture storage.
563 bool useTexStorage = false &&
564 isNewTexture &&
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +0000565 kIndex_8_GrPixelConfig != desc.fConfig &&
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000566 this->glCaps().texStorageSupport();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000567
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +0000568 if (useTexStorage && kGL_GrGLStandard == this->glStandard()) {
bsalomon@google.com313f0192012-07-10 17:21:02 +0000569 // 565 is not a sized internal format on desktop GL. So on desktop with
570 // 565 we always use an unsized internal format to let the system pick
571 // the best sized format to convert the 565 data to. Since TexStorage
572 // only allows sized internal formats we will instead use TexImage2D.
573 useTexStorage = desc.fConfig != kRGB_565_GrPixelConfig;
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000574 }
575
576 GrGLenum internalFormat;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000577 GrGLenum externalFormat;
578 GrGLenum externalType;
commit-bot@chromium.org87002952013-08-20 15:12:01 +0000579 // glTexStorage requires sized internal formats on both desktop and ES. ES2 requires an unsized
580 // format for glTexImage, unlike ES3 and desktop. However, we allow the driver to decide the
581 // size of the internal format whenever possible and so only use a sized internal format when
582 // using texture storage.
bsalomon@google.com3323c4d2013-08-20 13:48:33 +0000583 if (!this->configToGLFormats(dataConfig, useTexStorage, &internalFormat,
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000584 &externalFormat, &externalType)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000585 return false;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000586 }
587
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000588 if (!isNewTexture && GR_GL_PALETTE8_RGBA8 == internalFormat) {
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000589 // paletted textures cannot be updated
590 return false;
591 }
592
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000593 /*
bsalomon@google.com6f379512011-11-16 20:36:03 +0000594 * check whether to allocate a temporary buffer for flipping y or
595 * because our srcData has extra bytes past each row. If so, we need
596 * to trim those off here, since GL ES may not let us specify
597 * GL_UNPACK_ROW_LENGTH.
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000598 */
bsalomon@google.com6f379512011-11-16 20:36:03 +0000599 bool restoreGLRowLength = false;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000600 bool swFlipY = false;
601 bool glFlipY = false;
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000602 if (NULL != data) {
senorblanco@chromium.orgef5dbe12013-01-28 16:42:38 +0000603 if (kBottomLeft_GrSurfaceOrigin == desc.fOrigin) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000604 if (this->glCaps().unpackFlipYSupport()) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000605 glFlipY = true;
606 } else {
607 swFlipY = true;
608 }
609 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000610 if (this->glCaps().unpackRowLengthSupport() && !swFlipY) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000611 // can't use this for flipping, only non-neg values allowed. :(
612 if (rowBytes != trimRowBytes) {
613 GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
614 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength));
615 restoreGLRowLength = true;
616 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000617 } else {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000618 if (trimRowBytes != rowBytes || swFlipY) {
619 // copy data into our new storage, skipping the trailing bytes
620 size_t trimSize = height * trimRowBytes;
621 const char* src = (const char*)data;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000622 if (swFlipY) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000623 src += (height - 1) * rowBytes;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000624 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000625 char* dst = (char*)tempStorage.reset(trimSize);
626 for (int y = 0; y < height; y++) {
627 memcpy(dst, src, trimRowBytes);
628 if (swFlipY) {
629 src -= rowBytes;
630 } else {
631 src += rowBytes;
632 }
633 dst += trimRowBytes;
634 }
635 // now point data to our copied version
636 data = tempStorage.get();
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000637 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000638 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000639 if (glFlipY) {
640 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_TRUE));
641 }
642 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, static_cast<GrGLint>(bpp)));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000643 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000644 bool succeeded = true;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000645 if (isNewTexture &&
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000646 0 == left && 0 == top &&
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000647 desc.fWidth == width && desc.fHeight == height) {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000648 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000649 if (useTexStorage) {
commit-bot@chromium.orgcea9abb2013-12-09 19:15:37 +0000650 // We never resize or change formats of textures.
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000651 GL_ALLOC_CALL(this->glInterface(),
652 TexStorage2D(GR_GL_TEXTURE_2D,
653 1, // levels
654 internalFormat,
655 desc.fWidth, desc.fHeight));
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000656 } else {
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000657 if (GR_GL_PALETTE8_RGBA8 == internalFormat) {
658 GrGLsizei imageSize = desc.fWidth * desc.fHeight +
659 kGrColorTableSize;
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000660 GL_ALLOC_CALL(this->glInterface(),
661 CompressedTexImage2D(GR_GL_TEXTURE_2D,
662 0, // level
663 internalFormat,
664 desc.fWidth, desc.fHeight,
665 0, // border
666 imageSize,
667 data));
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000668 } else {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000669 GL_ALLOC_CALL(this->glInterface(),
670 TexImage2D(GR_GL_TEXTURE_2D,
671 0, // level
672 internalFormat,
673 desc.fWidth, desc.fHeight,
674 0, // border
675 externalFormat, externalType,
676 data));
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000677 }
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000678 }
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000679 GrGLenum error = check_alloc_error(desc, this->glInterface());
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000680 if (error != GR_GL_NO_ERROR) {
681 succeeded = false;
682 } else {
683 // if we have data and we used TexStorage to create the texture, we
684 // now upload with TexSubImage.
685 if (NULL != data && useTexStorage) {
686 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D,
687 0, // level
688 left, top,
689 width, height,
690 externalFormat, externalType,
691 data));
692 }
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000693 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000694 } else {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000695 if (swFlipY || glFlipY) {
bsalomon@google.com6f379512011-11-16 20:36:03 +0000696 top = desc.fHeight - (top + height);
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000697 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000698 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D,
699 0, // level
700 left, top,
701 width, height,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000702 externalFormat, externalType, data));
703 }
704
705 if (restoreGLRowLength) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000706 SkASSERT(this->glCaps().unpackRowLengthSupport());
bsalomon@google.com6f379512011-11-16 20:36:03 +0000707 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000708 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000709 if (glFlipY) {
710 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
711 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000712 return succeeded;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000713}
714
krajcevski145d48c2014-06-11 16:07:50 -0700715// TODO: This function is using a lot of wonky semantics like, if width == -1
716// then set width = desc.fWdith ... blah. A better way to do it might be to
717// create a CompressedTexData struct that takes a desc/ptr and figures out
718// the proper upload semantics. Then users can construct this function how they
719// see fit if they want to go against the "standard" way to do it.
krajcevski9c0e6292014-06-02 07:38:14 -0700720bool GrGpuGL::uploadCompressedTexData(const GrGLTexture::Desc& desc,
krajcevski145d48c2014-06-11 16:07:50 -0700721 const void* data,
722 bool isNewTexture,
723 int left, int top, int width, int height) {
724 SkASSERT(NULL != data || isNewTexture);
krajcevski9c0e6292014-06-02 07:38:14 -0700725
726 // No support for software flip y, yet...
727 SkASSERT(kBottomLeft_GrSurfaceOrigin != desc.fOrigin);
728
krajcevski145d48c2014-06-11 16:07:50 -0700729 if (-1 == width) {
730 width = desc.fWidth;
731 }
732#ifdef SK_DEBUG
733 else {
734 SkASSERT(width <= desc.fWidth);
735 }
736#endif
737
738 if (-1 == height) {
739 height = desc.fHeight;
740 }
741#ifdef SK_DEBUG
742 else {
743 SkASSERT(height <= desc.fHeight);
744 }
745#endif
746
krajcevski9c0e6292014-06-02 07:38:14 -0700747 // Make sure that the width and height that we pass to OpenGL
748 // is a multiple of the block size.
krajcevski145d48c2014-06-11 16:07:50 -0700749 int dataSize = GrCompressedFormatDataSize(desc.fConfig, width, height);
krajcevski9c0e6292014-06-02 07:38:14 -0700750
751 // We only need the internal format for compressed 2D textures.
752 GrGLenum internalFormat = 0;
753 if (!this->configToGLFormats(desc.fConfig, false, &internalFormat, NULL, NULL)) {
754 return false;
755 }
756
757 bool succeeded = true;
758 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
krajcevski145d48c2014-06-11 16:07:50 -0700759
760 if (isNewTexture) {
761 GL_ALLOC_CALL(this->glInterface(),
762 CompressedTexImage2D(GR_GL_TEXTURE_2D,
763 0, // level
764 internalFormat,
765 width, height,
766 0, // border
767 dataSize,
768 data));
769 } else {
770 GL_ALLOC_CALL(this->glInterface(),
771 CompressedTexSubImage2D(GR_GL_TEXTURE_2D,
772 0, // level
773 left, top,
774 width, height,
775 internalFormat,
776 dataSize,
777 data));
778 }
krajcevski9c0e6292014-06-02 07:38:14 -0700779
780 GrGLenum error = check_alloc_error(desc, this->glInterface());
781 if (error != GR_GL_NO_ERROR) {
782 succeeded = false;
783 }
784 return succeeded;
785}
786
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +0000787static bool renderbuffer_storage_msaa(GrGLContext& ctx,
788 int sampleCount,
789 GrGLenum format,
790 int width, int height) {
robertphillips@google.com6177e692013-02-28 20:16:25 +0000791 CLEAR_ERROR_BEFORE_ALLOC(ctx.interface());
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +0000792 SkASSERT(GrGLCaps::kNone_MSFBOType != ctx.caps()->msFBOType());
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +0000793 switch (ctx.caps()->msFBOType()) {
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +0000794 case GrGLCaps::kDesktop_ARB_MSFBOType:
795 case GrGLCaps::kDesktop_EXT_MSFBOType:
796 case GrGLCaps::kES_3_0_MSFBOType:
797 GL_ALLOC_CALL(ctx.interface(),
798 RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
799 sampleCount,
800 format,
801 width, height));
802 break;
803 case GrGLCaps::kES_Apple_MSFBOType:
804 GL_ALLOC_CALL(ctx.interface(),
805 RenderbufferStorageMultisampleES2APPLE(GR_GL_RENDERBUFFER,
806 sampleCount,
807 format,
808 width, height));
809 break;
810 case GrGLCaps::kES_EXT_MsToTexture_MSFBOType:
811 case GrGLCaps::kES_IMG_MsToTexture_MSFBOType:
812 GL_ALLOC_CALL(ctx.interface(),
813 RenderbufferStorageMultisampleES2EXT(GR_GL_RENDERBUFFER,
814 sampleCount,
815 format,
816 width, height));
817 break;
818 case GrGLCaps::kNone_MSFBOType:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000819 SkFAIL("Shouldn't be here if we don't support multisampled renderbuffers.");
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +0000820 break;
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000821 }
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +0000822 return (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctx.interface()));;
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000823}
824
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000825bool GrGpuGL::createRenderTargetObjects(int width, int height,
826 GrGLuint texID,
827 GrGLRenderTarget::Desc* desc) {
828 desc->fMSColorRenderbufferID = 0;
829 desc->fRTFBOID = 0;
830 desc->fTexFBOID = 0;
bsalomon@google.com72830222013-01-23 20:25:22 +0000831 desc->fIsWrapped = false;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000832
833 GrGLenum status;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000834
bsalomon@google.comab15d612011-08-09 12:57:56 +0000835 GrGLenum msColorFormat = 0; // suppress warning
836
bsalomon@google.com347c3822013-05-01 20:10:01 +0000837 if (desc->fSampleCnt > 0 && GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType()) {
838 goto FAILED;
839 }
840
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000841 GL_CALL(GenFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000842 if (!desc->fTexFBOID) {
843 goto FAILED;
844 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000845
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000846
bsalomon@google.comf3a60c02013-03-19 19:06:09 +0000847 // If we are using multisampling we will create two FBOS. We render to one and then resolve to
848 // the texture bound to the other. The exception is the IMG multisample extension. With this
849 // extension the texture is multisampled when rendered to and then auto-resolves it when it is
850 // rendered from.
bsalomon@google.com347c3822013-05-01 20:10:01 +0000851 if (desc->fSampleCnt > 0 && this->glCaps().usesMSAARenderBuffers()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000852 GL_CALL(GenFramebuffers(1, &desc->fRTFBOID));
853 GL_CALL(GenRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000854 if (!desc->fRTFBOID ||
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000855 !desc->fMSColorRenderbufferID ||
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000856 !this->configToGLFormats(desc->fConfig,
commit-bot@chromium.org87002952013-08-20 15:12:01 +0000857 // ES2 and ES3 require sized internal formats for rb storage.
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +0000858 kGLES_GrGLStandard == this->glStandard(),
bsalomon@google.com347c3822013-05-01 20:10:01 +0000859 &msColorFormat,
860 NULL,
861 NULL)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000862 goto FAILED;
863 }
864 } else {
865 desc->fRTFBOID = desc->fTexFBOID;
866 }
867
bsalomon@google.com0e9b41a2012-01-04 22:11:43 +0000868 // below here we may bind the FBO
bsalomon@google.comc811ea32012-05-21 15:33:09 +0000869 fHWBoundRenderTarget = NULL;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000870 if (desc->fRTFBOID != desc->fTexFBOID) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000871 SkASSERT(desc->fSampleCnt > 0);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000872 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000873 desc->fMSColorRenderbufferID));
robertphillips@google.com6177e692013-02-28 20:16:25 +0000874 if (!renderbuffer_storage_msaa(fGLContext,
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000875 desc->fSampleCnt,
876 msColorFormat,
877 width, height)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000878 goto FAILED;
879 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000880 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fRTFBOID));
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000881 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000882 GR_GL_COLOR_ATTACHMENT0,
883 GR_GL_RENDERBUFFER,
884 desc->fMSColorRenderbufferID));
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000885 if (desc->fCheckAllocation ||
886 !this->glCaps().isConfigVerifiedColorAttachment(desc->fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000887 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
888 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
889 goto FAILED;
890 }
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +0000891 fGLContext.caps()->markConfigAsValidColorAttachment(desc->fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000892 }
893 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000894 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000895
bsalomon@google.com347c3822013-05-01 20:10:01 +0000896 if (this->glCaps().usesImplicitMSAAResolve() && desc->fSampleCnt > 0) {
bsalomon@google.comf3a60c02013-03-19 19:06:09 +0000897 GL_CALL(FramebufferTexture2DMultisample(GR_GL_FRAMEBUFFER,
898 GR_GL_COLOR_ATTACHMENT0,
899 GR_GL_TEXTURE_2D,
900 texID, 0, desc->fSampleCnt));
901 } else {
902 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
903 GR_GL_COLOR_ATTACHMENT0,
904 GR_GL_TEXTURE_2D,
905 texID, 0));
906 }
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000907 if (desc->fCheckAllocation ||
908 !this->glCaps().isConfigVerifiedColorAttachment(desc->fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000909 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
910 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
911 goto FAILED;
912 }
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +0000913 fGLContext.caps()->markConfigAsValidColorAttachment(desc->fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000914 }
915
916 return true;
917
918FAILED:
919 if (desc->fMSColorRenderbufferID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000920 GL_CALL(DeleteRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000921 }
922 if (desc->fRTFBOID != desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000923 GL_CALL(DeleteFramebuffers(1, &desc->fRTFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000924 }
925 if (desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000926 GL_CALL(DeleteFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000927 }
928 return false;
929}
930
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000931// good to set a break-point here to know when createTexture fails
932static GrTexture* return_null_texture() {
mtklein@google.com330313a2013-08-22 15:37:26 +0000933// SkDEBUGFAIL("null texture");
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000934 return NULL;
935}
936
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000937#if 0 && defined(SK_DEBUG)
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000938static size_t as_size_t(int x) {
939 return x;
940}
941#endif
942
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000943GrTexture* GrGpuGL::onCreateTexture(const GrTextureDesc& desc,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000944 const void* srcData,
945 size_t rowBytes) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000946
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000947 GrGLTexture::Desc glTexDesc;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000948 GrGLRenderTarget::Desc glRTDesc;
reed@google.comac10a2d2010-12-22 21:39:39 +0000949
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000950 // Attempt to catch un- or wrongly initialized sample counts;
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000951 SkASSERT(desc.fSampleCnt >= 0 && desc.fSampleCnt <= 64);
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000952 // We fail if the MSAA was requested and is not available.
953 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType() && desc.fSampleCnt) {
954 //GrPrintf("MSAA RT requested but not supported on this platform.");
955 return return_null_texture();
956 }
957 // If the sample count exceeds the max then we clamp it.
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000958 glTexDesc.fSampleCnt = SkTMin(desc.fSampleCnt, this->caps()->maxSampleCount());
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000959
robertphillips@google.com32716282012-06-04 12:48:45 +0000960 glTexDesc.fFlags = desc.fFlags;
bsalomon@google.com99621082011-11-15 16:47:16 +0000961 glTexDesc.fWidth = desc.fWidth;
962 glTexDesc.fHeight = desc.fHeight;
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000963 glTexDesc.fConfig = desc.fConfig;
bsalomon@google.com72830222013-01-23 20:25:22 +0000964 glTexDesc.fIsWrapped = false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000965
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000966 glRTDesc.fMSColorRenderbufferID = 0;
967 glRTDesc.fRTFBOID = 0;
968 glRTDesc.fTexFBOID = 0;
bsalomon@google.com72830222013-01-23 20:25:22 +0000969 glRTDesc.fIsWrapped = false;
bsalomon@google.com64c4fe42011-11-05 14:51:01 +0000970 glRTDesc.fConfig = glTexDesc.fConfig;
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000971 glRTDesc.fCheckAllocation = SkToBool(desc.fFlags & kCheckAllocation_GrTextureFlagBit);
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000972
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000973 bool renderTarget = SkToBool(desc.fFlags & kRenderTarget_GrTextureFlagBit);
reed@google.comac10a2d2010-12-22 21:39:39 +0000974
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000975 glTexDesc.fOrigin = resolve_origin(desc.fOrigin, renderTarget);
976 glRTDesc.fOrigin = glTexDesc.fOrigin;
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000977
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000978 glRTDesc.fSampleCnt = glTexDesc.fSampleCnt;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000979 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType() &&
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000980 desc.fSampleCnt) {
bsalomon@google.com945bbe12012-06-15 14:30:34 +0000981 //GrPrintf("MSAA RT requested but not supported on this platform.");
982 return return_null_texture();
reed@google.comac10a2d2010-12-22 21:39:39 +0000983 }
984
reed@google.comac10a2d2010-12-22 21:39:39 +0000985 if (renderTarget) {
bsalomon@google.combcce8922013-03-25 15:38:39 +0000986 int maxRTSize = this->caps()->maxRenderTargetSize();
987 if (glTexDesc.fWidth > maxRTSize || glTexDesc.fHeight > maxRTSize) {
bsalomon@google.com91958362011-06-13 17:58:13 +0000988 return return_null_texture();
989 }
sugoi@google.com6ba0b0f2013-05-03 13:52:32 +0000990 } else {
991 int maxSize = this->caps()->maxTextureSize();
992 if (glTexDesc.fWidth > maxSize || glTexDesc.fHeight > maxSize) {
993 return return_null_texture();
994 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000995 }
996
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000997 GL_CALL(GenTextures(1, &glTexDesc.fTextureID));
junov@chromium.org8b6b1c92013-08-29 16:22:09 +0000998
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000999 if (!glTexDesc.fTextureID) {
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001000 return return_null_texture();
1001 }
1002
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00001003 this->setScratchTextureUnit();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001004 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTexDesc.fTextureID));
bsalomon@google.come269f212011-11-07 13:29:52 +00001005
junov@chromium.org8b6b1c92013-08-29 16:22:09 +00001006 if (renderTarget && this->glCaps().textureUsageSupport()) {
1007 // provides a hint about how this texture will be used
1008 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1009 GR_GL_TEXTURE_USAGE,
1010 GR_GL_FRAMEBUFFER_ATTACHMENT));
1011 }
1012
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001013 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
1014 // drivers have a bug where an FBO won't be complete if it includes a
1015 // texture that is not mipmap complete (considering the filter in use).
1016 GrGLTexture::TexParams initialTexParams;
1017 // we only set a subset here so invalidate first
1018 initialTexParams.invalidate();
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00001019 initialTexParams.fMinFilter = GR_GL_NEAREST;
1020 initialTexParams.fMagFilter = GR_GL_NEAREST;
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001021 initialTexParams.fWrapS = GR_GL_CLAMP_TO_EDGE;
1022 initialTexParams.fWrapT = GR_GL_CLAMP_TO_EDGE;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001023 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1024 GR_GL_TEXTURE_MAG_FILTER,
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00001025 initialTexParams.fMagFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001026 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1027 GR_GL_TEXTURE_MIN_FILTER,
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00001028 initialTexParams.fMinFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001029 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1030 GR_GL_TEXTURE_WRAP_S,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001031 initialTexParams.fWrapS));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001032 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1033 GR_GL_TEXTURE_WRAP_T,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001034 initialTexParams.fWrapT));
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001035 if (!this->uploadTexData(glTexDesc, true, 0, 0,
1036 glTexDesc.fWidth, glTexDesc.fHeight,
1037 desc.fConfig, srcData, rowBytes)) {
1038 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
1039 return return_null_texture();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001040 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001041
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001042 GrGLTexture* tex;
reed@google.comac10a2d2010-12-22 21:39:39 +00001043 if (renderTarget) {
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001044 // unbind the texture from the texture unit before binding it to the frame buffer
1045 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, 0));
1046
bsalomon@google.com99621082011-11-15 16:47:16 +00001047 if (!this->createRenderTargetObjects(glTexDesc.fWidth,
1048 glTexDesc.fHeight,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001049 glTexDesc.fTextureID,
1050 &glRTDesc)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001051 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001052 return return_null_texture();
1053 }
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001054 tex = SkNEW_ARGS(GrGLTexture, (this, glTexDesc, glRTDesc));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001055 } else {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001056 tex = SkNEW_ARGS(GrGLTexture, (this, glTexDesc));
reed@google.comac10a2d2010-12-22 21:39:39 +00001057 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001058 tex->setCachedTexParams(initialTexParams, this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00001059#ifdef TRACE_TEXTURE_CREATION
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001060 GrPrintf("--- new texture [%d] size=(%d %d) config=%d\n",
1061 glTexDesc.fTextureID, desc.fWidth, desc.fHeight, desc.fConfig);
reed@google.comac10a2d2010-12-22 21:39:39 +00001062#endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001063 return tex;
1064}
1065
krajcevski9c0e6292014-06-02 07:38:14 -07001066GrTexture* GrGpuGL::onCreateCompressedTexture(const GrTextureDesc& desc,
1067 const void* srcData) {
1068
1069 if(SkToBool(desc.fFlags & kRenderTarget_GrTextureFlagBit)) {
1070 return return_null_texture();
1071 }
1072
1073 // Make sure that we're not flipping Y.
1074 GrSurfaceOrigin texOrigin = resolve_origin(desc.fOrigin, false);
1075 if (kBottomLeft_GrSurfaceOrigin == texOrigin) {
1076 return return_null_texture();
1077 }
1078
1079 GrGLTexture::Desc glTexDesc;
1080
1081 glTexDesc.fFlags = desc.fFlags;
1082 glTexDesc.fWidth = desc.fWidth;
1083 glTexDesc.fHeight = desc.fHeight;
1084 glTexDesc.fConfig = desc.fConfig;
1085 glTexDesc.fIsWrapped = false;
1086 glTexDesc.fOrigin = texOrigin;
1087
1088 int maxSize = this->caps()->maxTextureSize();
1089 if (glTexDesc.fWidth > maxSize || glTexDesc.fHeight > maxSize) {
1090 return return_null_texture();
1091 }
1092
1093 GL_CALL(GenTextures(1, &glTexDesc.fTextureID));
1094
1095 if (!glTexDesc.fTextureID) {
1096 return return_null_texture();
1097 }
1098
1099 this->setScratchTextureUnit();
1100 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTexDesc.fTextureID));
1101
1102 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
1103 // drivers have a bug where an FBO won't be complete if it includes a
1104 // texture that is not mipmap complete (considering the filter in use).
1105 GrGLTexture::TexParams initialTexParams;
1106 // we only set a subset here so invalidate first
1107 initialTexParams.invalidate();
1108 initialTexParams.fMinFilter = GR_GL_NEAREST;
1109 initialTexParams.fMagFilter = GR_GL_NEAREST;
1110 initialTexParams.fWrapS = GR_GL_CLAMP_TO_EDGE;
1111 initialTexParams.fWrapT = GR_GL_CLAMP_TO_EDGE;
1112 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1113 GR_GL_TEXTURE_MAG_FILTER,
1114 initialTexParams.fMagFilter));
1115 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1116 GR_GL_TEXTURE_MIN_FILTER,
1117 initialTexParams.fMinFilter));
1118 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1119 GR_GL_TEXTURE_WRAP_S,
1120 initialTexParams.fWrapS));
1121 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1122 GR_GL_TEXTURE_WRAP_T,
1123 initialTexParams.fWrapT));
1124
1125 if (!this->uploadCompressedTexData(glTexDesc, srcData)) {
1126 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
1127 return return_null_texture();
1128 }
1129
1130 GrGLTexture* tex;
1131 tex = SkNEW_ARGS(GrGLTexture, (this, glTexDesc));
1132 tex->setCachedTexParams(initialTexParams, this->getResetTimestamp());
1133#ifdef TRACE_TEXTURE_CREATION
1134 GrPrintf("--- new compressed texture [%d] size=(%d %d) config=%d\n",
1135 glTexDesc.fTextureID, desc.fWidth, desc.fHeight, desc.fConfig);
1136#endif
1137 return tex;
1138}
1139
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001140namespace {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001141
1142const GrGLuint kUnknownBitCount = GrGLStencilBuffer::kUnknownBitCount;
1143
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001144void inline get_stencil_rb_sizes(const GrGLInterface* gl,
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001145 GrGLStencilBuffer::Format* format) {
sugoi@google.com6ba0b0f2013-05-03 13:52:32 +00001146
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001147 // we shouldn't ever know one size and not the other
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001148 SkASSERT((kUnknownBitCount == format->fStencilBits) ==
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001149 (kUnknownBitCount == format->fTotalBits));
1150 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001151 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001152 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1153 (GrGLint*)&format->fStencilBits);
1154 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001155 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001156 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1157 (GrGLint*)&format->fTotalBits);
1158 format->fTotalBits += format->fStencilBits;
1159 } else {
1160 format->fTotalBits = format->fStencilBits;
1161 }
1162 }
1163}
1164}
1165
1166bool GrGpuGL::createStencilBufferForRenderTarget(GrRenderTarget* rt,
1167 int width, int height) {
1168
1169 // All internally created RTs are also textures. We don't create
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +00001170 // 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 +00001171 SkASSERT(rt->asTexture());
1172 SkASSERT(width >= rt->width());
1173 SkASSERT(height >= rt->height());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001174
1175 int samples = rt->numSamples();
1176 GrGLuint sbID;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001177 GL_CALL(GenRenderbuffers(1, &sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001178 if (!sbID) {
1179 return false;
1180 }
1181
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001182 int stencilFmtCnt = this->glCaps().stencilFormats().count();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001183 for (int i = 0; i < stencilFmtCnt; ++i) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001184 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001185 // we start with the last stencil format that succeeded in hopes
1186 // that we won't go through this loop more than once after the
1187 // first (painful) stencil creation.
1188 int sIdx = (i + fLastSuccessfulStencilFmtIdx) % stencilFmtCnt;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001189 const GrGLCaps::StencilFormat& sFmt =
1190 this->glCaps().stencilFormats()[sIdx];
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001191 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001192 // we do this "if" so that we don't call the multisample
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001193 // version on a GL that doesn't have an MSAA extension.
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001194 bool created;
1195 if (samples > 0) {
robertphillips@google.com6177e692013-02-28 20:16:25 +00001196 created = renderbuffer_storage_msaa(fGLContext,
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001197 samples,
1198 sFmt.fInternalFormat,
1199 width, height);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001200 } else {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001201 GL_ALLOC_CALL(this->glInterface(),
1202 RenderbufferStorage(GR_GL_RENDERBUFFER,
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001203 sFmt.fInternalFormat,
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001204 width, height));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001205 created =
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +00001206 (GR_GL_NO_ERROR == check_alloc_error(rt->desc(), this->glInterface()));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001207 }
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001208 if (created) {
1209 // After sized formats we attempt an unsized format and take
1210 // whatever sizes GL gives us. In that case we query for the size.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001211 GrGLStencilBuffer::Format format = sFmt;
sugoi@google.com6ba0b0f2013-05-03 13:52:32 +00001212 get_stencil_rb_sizes(this->glInterface(), &format);
bsalomon@google.com72830222013-01-23 20:25:22 +00001213 static const bool kIsWrapped = false;
robertphillips@google.comf2e93fc2012-09-05 19:44:18 +00001214 SkAutoTUnref<GrStencilBuffer> sb(SkNEW_ARGS(GrGLStencilBuffer,
bsalomon@google.com72830222013-01-23 20:25:22 +00001215 (this, kIsWrapped, sbID, width, height,
robertphillips@google.comf2e93fc2012-09-05 19:44:18 +00001216 samples, format)));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001217 if (this->attachStencilBufferToRenderTarget(sb, rt)) {
1218 fLastSuccessfulStencilFmtIdx = sIdx;
robertphillips@google.com9fbcad02012-09-09 14:44:15 +00001219 sb->transferToCache();
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001220 rt->setStencilBuffer(sb);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001221 return true;
1222 }
1223 sb->abandon(); // otherwise we lose sbID
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001224 }
1225 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001226 GL_CALL(DeleteRenderbuffers(1, &sbID));
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001227 return false;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001228}
1229
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001230bool GrGpuGL::attachStencilBufferToRenderTarget(GrStencilBuffer* sb, GrRenderTarget* rt) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001231 GrGLRenderTarget* glrt = (GrGLRenderTarget*) rt;
1232
1233 GrGLuint fbo = glrt->renderFBOID();
1234
1235 if (NULL == sb) {
1236 if (NULL != rt->getStencilBuffer()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001237 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001238 GR_GL_STENCIL_ATTACHMENT,
1239 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001240 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001241 GR_GL_DEPTH_ATTACHMENT,
1242 GR_GL_RENDERBUFFER, 0));
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +00001243#ifdef SK_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001244 GrGLenum status;
1245 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001246 SkASSERT(GR_GL_FRAMEBUFFER_COMPLETE == status);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001247#endif
1248 }
1249 return true;
1250 } else {
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001251 GrGLStencilBuffer* glsb = static_cast<GrGLStencilBuffer*>(sb);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001252 GrGLuint rb = glsb->renderbufferID();
1253
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001254 fHWBoundRenderTarget = NULL;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001255 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, fbo));
1256 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001257 GR_GL_STENCIL_ATTACHMENT,
1258 GR_GL_RENDERBUFFER, rb));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001259 if (glsb->format().fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001260 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001261 GR_GL_DEPTH_ATTACHMENT,
1262 GR_GL_RENDERBUFFER, rb));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001263 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001264 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001265 GR_GL_DEPTH_ATTACHMENT,
1266 GR_GL_RENDERBUFFER, 0));
reed@google.comac10a2d2010-12-22 21:39:39 +00001267 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001268
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001269 GrGLenum status;
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001270 if (!this->glCaps().isColorConfigAndStencilFormatVerified(rt->config(), glsb->format())) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001271 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1272 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001273 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001274 GR_GL_STENCIL_ATTACHMENT,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001275 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001276 if (glsb->format().fPacked) {
1277 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1278 GR_GL_DEPTH_ATTACHMENT,
1279 GR_GL_RENDERBUFFER, 0));
1280 }
1281 return false;
1282 } else {
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +00001283 fGLContext.caps()->markColorConfigAndStencilFormatAsVerified(
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001284 rt->config(),
1285 glsb->format());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001286 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001287 }
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001288 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001289 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001290}
1291
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001292////////////////////////////////////////////////////////////////////////////////
1293
robertphillips@google.comadacc702013-10-14 21:53:24 +00001294GrVertexBuffer* GrGpuGL::onCreateVertexBuffer(size_t size, bool dynamic) {
bsalomon@google.come49ad452013-02-20 19:33:20 +00001295 GrGLVertexBuffer::Desc desc;
1296 desc.fDynamic = dynamic;
1297 desc.fSizeInBytes = size;
1298 desc.fIsWrapped = false;
1299
bsalomon@google.com96966a52013-02-21 16:34:21 +00001300 if (this->glCaps().useNonVBOVertexAndIndexDynamicData() && desc.fDynamic) {
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001301 desc.fID = 0;
bsalomon@google.come49ad452013-02-20 19:33:20 +00001302 GrGLVertexBuffer* vertexBuffer = SkNEW_ARGS(GrGLVertexBuffer, (this, desc));
reed@google.comac10a2d2010-12-22 21:39:39 +00001303 return vertexBuffer;
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001304 } else {
1305 GL_CALL(GenBuffers(1, &desc.fID));
1306 if (desc.fID) {
bsalomon@google.com6918d482013-03-07 19:09:11 +00001307 fHWGeometryState.setVertexBufferID(this, desc.fID);
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001308 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1309 // make sure driver can allocate memory for this buffer
1310 GL_ALLOC_CALL(this->glInterface(),
1311 BufferData(GR_GL_ARRAY_BUFFER,
robertphillips@google.come9cd27d2013-10-16 17:48:11 +00001312 (GrGLsizeiptr) desc.fSizeInBytes,
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001313 NULL, // data ptr
1314 desc.fDynamic ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW));
1315 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
1316 GL_CALL(DeleteBuffers(1, &desc.fID));
bsalomon@google.com6918d482013-03-07 19:09:11 +00001317 this->notifyVertexBufferDelete(desc.fID);
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001318 return NULL;
1319 }
1320 GrGLVertexBuffer* vertexBuffer = SkNEW_ARGS(GrGLVertexBuffer, (this, desc));
1321 return vertexBuffer;
1322 }
1323 return NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +00001324 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001325}
1326
robertphillips@google.comadacc702013-10-14 21:53:24 +00001327GrIndexBuffer* GrGpuGL::onCreateIndexBuffer(size_t size, bool dynamic) {
bsalomon@google.come49ad452013-02-20 19:33:20 +00001328 GrGLIndexBuffer::Desc desc;
1329 desc.fDynamic = dynamic;
1330 desc.fSizeInBytes = size;
1331 desc.fIsWrapped = false;
1332
bsalomon@google.com96966a52013-02-21 16:34:21 +00001333 if (this->glCaps().useNonVBOVertexAndIndexDynamicData() && desc.fDynamic) {
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001334 desc.fID = 0;
bsalomon@google.come49ad452013-02-20 19:33:20 +00001335 GrIndexBuffer* indexBuffer = SkNEW_ARGS(GrGLIndexBuffer, (this, desc));
reed@google.comac10a2d2010-12-22 21:39:39 +00001336 return indexBuffer;
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001337 } else {
1338 GL_CALL(GenBuffers(1, &desc.fID));
1339 if (desc.fID) {
bsalomon@google.com6918d482013-03-07 19:09:11 +00001340 fHWGeometryState.setIndexBufferIDOnDefaultVertexArray(this, desc.fID);
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001341 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1342 // make sure driver can allocate memory for this buffer
1343 GL_ALLOC_CALL(this->glInterface(),
1344 BufferData(GR_GL_ELEMENT_ARRAY_BUFFER,
robertphillips@google.come9cd27d2013-10-16 17:48:11 +00001345 (GrGLsizeiptr) desc.fSizeInBytes,
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001346 NULL, // data ptr
1347 desc.fDynamic ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW));
1348 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
1349 GL_CALL(DeleteBuffers(1, &desc.fID));
bsalomon@google.com6918d482013-03-07 19:09:11 +00001350 this->notifyIndexBufferDelete(desc.fID);
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001351 return NULL;
1352 }
1353 GrIndexBuffer* indexBuffer = SkNEW_ARGS(GrGLIndexBuffer, (this, desc));
1354 return indexBuffer;
1355 }
1356 return NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +00001357 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001358}
1359
commit-bot@chromium.org32184d82013-10-09 15:14:18 +00001360GrPath* GrGpuGL::onCreatePath(const SkPath& inPath, const SkStrokeRec& stroke) {
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001361 SkASSERT(this->caps()->pathRenderingSupport());
commit-bot@chromium.org32184d82013-10-09 15:14:18 +00001362 return SkNEW_ARGS(GrGLPath, (this, inPath, stroke));
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00001363}
1364
bsalomon@google.coma3201942012-06-21 19:58:20 +00001365void GrGpuGL::flushScissor() {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001366 if (fScissorState.fEnabled) {
commit-bot@chromium.org5c363642013-10-04 15:28:18 +00001367 // Only access the RT if scissoring is being enabled. We can call this before performing
1368 // a glBitframebuffer for a surface->surface copy, which requires no RT to be bound to the
1369 // GrDrawState.
1370 const GrDrawState& drawState = this->getDrawState();
1371 const GrGLRenderTarget* rt =
1372 static_cast<const GrGLRenderTarget*>(drawState.getRenderTarget());
1373
1374 SkASSERT(NULL != rt);
1375 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.coma3201942012-06-21 19:58:20 +00001376 GrGLIRect scissor;
1377 scissor.setRelativeTo(vp,
1378 fScissorState.fRect.fLeft,
1379 fScissorState.fRect.fTop,
1380 fScissorState.fRect.width(),
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001381 fScissorState.fRect.height(),
1382 rt->origin());
bsalomon@google.coma3201942012-06-21 19:58:20 +00001383 // if the scissor fully contains the viewport then we fall through and
1384 // disable the scissor test.
1385 if (!scissor.contains(vp)) {
1386 if (fHWScissorSettings.fRect != scissor) {
1387 scissor.pushToGLScissor(this->glInterface());
1388 fHWScissorSettings.fRect = scissor;
1389 }
1390 if (kYes_TriState != fHWScissorSettings.fEnabled) {
1391 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1392 fHWScissorSettings.fEnabled = kYes_TriState;
1393 }
1394 return;
1395 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001396 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001397 if (kNo_TriState != fHWScissorSettings.fEnabled) {
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001398 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.coma3201942012-06-21 19:58:20 +00001399 fHWScissorSettings.fEnabled = kNo_TriState;
1400 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001401 }
1402}
1403
robertphillips@google.com56ce48a2013-10-31 21:44:25 +00001404void GrGpuGL::onClear(const SkIRect* rect, GrColor color, bool canIgnoreRect) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001405 const GrDrawState& drawState = this->getDrawState();
1406 const GrRenderTarget* rt = drawState.getRenderTarget();
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001407 // parent class should never let us get here with no RT
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001408 SkASSERT(NULL != rt);
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001409
robertphillips@google.com56ce48a2013-10-31 21:44:25 +00001410 if (canIgnoreRect && this->glCaps().fullClearIsFree()) {
1411 rect = NULL;
1412 }
1413
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001414 SkIRect clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001415 if (NULL != rect) {
1416 // flushScissor expects rect to be clipped to the target.
bsalomon@google.com74b98712011-11-11 19:46:16 +00001417 clippedRect = *rect;
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001418 SkIRect rtRect = SkIRect::MakeWH(rt->width(), rt->height());
bsalomon@google.com74b98712011-11-11 19:46:16 +00001419 if (clippedRect.intersect(rtRect)) {
1420 rect = &clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001421 } else {
1422 return;
1423 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001424 }
rmistry@google.comd6bab022013-12-02 13:50:38 +00001425
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001426 this->flushRenderTarget(rect);
bsalomon@google.coma3201942012-06-21 19:58:20 +00001427 GrAutoTRestore<ScissorState> asr(&fScissorState);
1428 fScissorState.fEnabled = (NULL != rect);
1429 if (fScissorState.fEnabled) {
1430 fScissorState.fRect = *rect;
1431 }
1432 this->flushScissor();
bsalomon@google.com74b98712011-11-11 19:46:16 +00001433
1434 GrGLfloat r, g, b, a;
1435 static const GrGLfloat scale255 = 1.f / 255.f;
1436 a = GrColorUnpackA(color) * scale255;
1437 GrGLfloat scaleRGB = scale255;
bsalomon@google.com74b98712011-11-11 19:46:16 +00001438 r = GrColorUnpackR(color) * scaleRGB;
1439 g = GrColorUnpackG(color) * scaleRGB;
1440 b = GrColorUnpackB(color) * scaleRGB;
1441
1442 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00001443 fHWWriteToColor = kYes_TriState;
bsalomon@google.com74b98712011-11-11 19:46:16 +00001444 GL_CALL(ClearColor(r, g, b, a));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001445 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001446}
1447
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +00001448void GrGpuGL::discard(GrRenderTarget* renderTarget) {
commit-bot@chromium.org52ffbf62014-04-02 16:19:33 +00001449 if (!this->caps()->discardRenderTargetSupport()) {
1450 return;
1451 }
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +00001452 if (NULL == renderTarget) {
1453 renderTarget = this->drawState()->getRenderTarget();
1454 if (NULL == renderTarget) {
1455 return;
1456 }
1457 }
1458
1459 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(renderTarget);
1460 if (renderTarget != fHWBoundRenderTarget) {
1461 fHWBoundRenderTarget = NULL;
1462 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, glRT->renderFBOID()));
1463 }
commit-bot@chromium.org52ffbf62014-04-02 16:19:33 +00001464 switch (this->glCaps().invalidateFBType()) {
1465 case GrGLCaps::kNone_FBFetchType:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +00001466 SkFAIL("Should never get here.");
commit-bot@chromium.org52ffbf62014-04-02 16:19:33 +00001467 break;
1468 case GrGLCaps::kInvalidate_InvalidateFBType:
1469 if (0 == glRT->renderFBOID()) {
1470 // When rendering to the default framebuffer the legal values for attachments
1471 // are GL_COLOR, GL_DEPTH, GL_STENCIL, ... rather than the various FBO attachment
1472 // types.
1473 static const GrGLenum attachments[] = { GR_GL_COLOR };
1474 GL_CALL(InvalidateFramebuffer(GR_GL_FRAMEBUFFER, SK_ARRAY_COUNT(attachments),
1475 attachments));
1476 } else {
1477 static const GrGLenum attachments[] = { GR_GL_COLOR_ATTACHMENT0 };
1478 GL_CALL(InvalidateFramebuffer(GR_GL_FRAMEBUFFER, SK_ARRAY_COUNT(attachments),
1479 attachments));
1480 }
1481 break;
1482 case GrGLCaps::kDiscard_InvalidateFBType: {
commit-bot@chromium.org4453e8b2014-04-16 14:33:27 +00001483 if (0 == glRT->renderFBOID()) {
1484 // When rendering to the default framebuffer the legal values for attachments
1485 // are GL_COLOR, GL_DEPTH, GL_STENCIL, ... rather than the various FBO attachment
1486 // types. See glDiscardFramebuffer() spec.
1487 static const GrGLenum attachments[] = { GR_GL_COLOR };
1488 GL_CALL(DiscardFramebuffer(GR_GL_FRAMEBUFFER, SK_ARRAY_COUNT(attachments),
1489 attachments));
1490 } else {
1491 static const GrGLenum attachments[] = { GR_GL_COLOR_ATTACHMENT0 };
1492 GL_CALL(DiscardFramebuffer(GR_GL_FRAMEBUFFER, SK_ARRAY_COUNT(attachments),
1493 attachments));
1494 }
commit-bot@chromium.org52ffbf62014-04-02 16:19:33 +00001495 break;
1496 }
1497 }
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +00001498 renderTarget->flagAsResolved();
1499}
1500
1501
bsalomon@google.comedc177d2011-08-05 15:46:40 +00001502void GrGpuGL::clearStencil() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001503 if (NULL == this->getDrawState().getRenderTarget()) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001504 return;
1505 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001506
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001507 this->flushRenderTarget(&SkIRect::EmptyIRect());
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001508
bsalomon@google.coma3201942012-06-21 19:58:20 +00001509 GrAutoTRestore<ScissorState> asr(&fScissorState);
1510 fScissorState.fEnabled = false;
1511 this->flushScissor();
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001512
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001513 GL_CALL(StencilMask(0xffffffff));
1514 GL_CALL(ClearStencil(0));
1515 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001516 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001517}
1518
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001519void GrGpuGL::clearStencilClip(const SkIRect& rect, bool insideClip) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001520 const GrDrawState& drawState = this->getDrawState();
1521 const GrRenderTarget* rt = drawState.getRenderTarget();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001522 SkASSERT(NULL != rt);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001523
1524 // this should only be called internally when we know we have a
1525 // stencil buffer.
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001526 SkASSERT(NULL != rt->getStencilBuffer());
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001527 GrGLint stencilBitCount = rt->getStencilBuffer()->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001528#if 0
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001529 SkASSERT(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00001530 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001531#else
1532 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001533 // ANGLE a partial stencil mask will cause clears to be
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001534 // turned into draws. Our contract on GrDrawTarget says that
1535 // changing the clip between stencil passes may or may not
1536 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00001537 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001538#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001539 GrGLint value;
1540 if (insideClip) {
1541 value = (1 << (stencilBitCount - 1));
1542 } else {
1543 value = 0;
1544 }
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001545 this->flushRenderTarget(&SkIRect::EmptyIRect());
bsalomon@google.coma3201942012-06-21 19:58:20 +00001546
1547 GrAutoTRestore<ScissorState> asr(&fScissorState);
1548 fScissorState.fEnabled = true;
1549 fScissorState.fRect = rect;
1550 this->flushScissor();
1551
caryclark@google.comcf6285b2012-06-06 12:09:01 +00001552 GL_CALL(StencilMask((uint32_t) clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001553 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001554 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001555 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001556}
1557
bsalomon@google.comc4364992011-11-07 15:54:49 +00001558bool GrGpuGL::readPixelsWillPayForYFlip(GrRenderTarget* renderTarget,
1559 int left, int top,
1560 int width, int height,
1561 GrPixelConfig config,
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001562 size_t rowBytes) const {
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001563 // If this rendertarget is aready TopLeft, we don't need to flip.
1564 if (kTopLeft_GrSurfaceOrigin == renderTarget->origin()) {
1565 return false;
1566 }
1567
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001568 // if GL can do the flip then we'll never pay for it.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001569 if (this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001570 return false;
1571 }
1572
1573 // If we have to do memcpy to handle non-trim rowBytes then we
bsalomon@google.com7107fa72011-11-10 14:54:14 +00001574 // get the flip for free. Otherwise it costs.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001575 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001576 return true;
1577 }
1578 // If we have to do memcpys to handle rowBytes then y-flip is free
1579 // Note the rowBytes might be tight to the passed in data, but if data
1580 // gets clipped in x to the target the rowBytes will no longer be tight.
1581 if (left >= 0 && (left + width) < renderTarget->width()) {
1582 return 0 == rowBytes ||
1583 GrBytesPerPixel(config) * width == rowBytes;
1584 } else {
1585 return false;
1586 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001587}
1588
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001589bool GrGpuGL::onReadPixels(GrRenderTarget* target,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001590 int left, int top,
1591 int width, int height,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001592 GrPixelConfig config,
1593 void* buffer,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001594 size_t rowBytes) {
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +00001595 // We cannot read pixels into a compressed buffer
1596 if (GrPixelConfigIsCompressed(config)) {
1597 return false;
1598 }
1599
1600 GrGLenum format = 0;
1601 GrGLenum type = 0;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001602 bool flipY = kBottomLeft_GrSurfaceOrigin == target->origin();
bsalomon@google.com280e99f2012-01-05 16:17:38 +00001603 if (!this->configToGLFormats(config, false, NULL, &format, &type)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001604 return false;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001605 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001606 size_t bpp = GrBytesPerPixel(config);
1607 if (!adjust_pixel_ops_params(target->width(), target->height(), bpp,
1608 &left, &top, &width, &height,
1609 const_cast<const void**>(&buffer),
1610 &rowBytes)) {
1611 return false;
1612 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001613
bsalomon@google.comc6980972011-11-02 19:57:21 +00001614 // resolve the render target if necessary
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001615 GrGLRenderTarget* tgt = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001616 GrDrawState::AutoRenderTargetRestore artr;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001617 switch (tgt->getResolveType()) {
1618 case GrGLRenderTarget::kCantResolve_ResolveType:
1619 return false;
1620 case GrGLRenderTarget::kAutoResolves_ResolveType:
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001621 artr.set(this->drawState(), target);
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001622 this->flushRenderTarget(&SkIRect::EmptyIRect());
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001623 break;
1624 case GrGLRenderTarget::kCanResolve_ResolveType:
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001625 this->onResolveRenderTarget(tgt);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001626 // we don't track the state of the READ FBO ID.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001627 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1628 tgt->textureFBOID()));
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001629 break;
1630 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +00001631 SkFAIL("Unknown resolve type");
reed@google.comac10a2d2010-12-22 21:39:39 +00001632 }
1633
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001634 const GrGLIRect& glvp = tgt->getViewport();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001635
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001636 // the read rect is viewport-relative
1637 GrGLIRect readRect;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001638 readRect.setRelativeTo(glvp, left, top, width, height, target->origin());
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001639
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001640 size_t tightRowBytes = bpp * width;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001641 if (0 == rowBytes) {
1642 rowBytes = tightRowBytes;
1643 }
1644 size_t readDstRowBytes = tightRowBytes;
1645 void* readDst = buffer;
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001646
bsalomon@google.comc6980972011-11-02 19:57:21 +00001647 // determine if GL can read using the passed rowBytes or if we need
1648 // a scratch buffer.
1649 SkAutoSMalloc<32 * sizeof(GrColor)> scratch;
1650 if (rowBytes != tightRowBytes) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001651 if (this->glCaps().packRowLengthSupport()) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001652 SkASSERT(!(rowBytes % sizeof(GrColor)));
skia.committer@gmail.com4677acc2013-10-17 07:02:33 +00001653 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH,
robertphillips@google.come9cd27d2013-10-16 17:48:11 +00001654 static_cast<GrGLint>(rowBytes / sizeof(GrColor))));
bsalomon@google.comc6980972011-11-02 19:57:21 +00001655 readDstRowBytes = rowBytes;
1656 } else {
1657 scratch.reset(tightRowBytes * height);
1658 readDst = scratch.get();
1659 }
1660 }
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001661 if (flipY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001662 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 1));
1663 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001664 GL_CALL(ReadPixels(readRect.fLeft, readRect.fBottom,
1665 readRect.fWidth, readRect.fHeight,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001666 format, type, readDst));
1667 if (readDstRowBytes != tightRowBytes) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001668 SkASSERT(this->glCaps().packRowLengthSupport());
bsalomon@google.comc6980972011-11-02 19:57:21 +00001669 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
1670 }
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001671 if (flipY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001672 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 0));
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001673 flipY = false;
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001674 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001675
1676 // now reverse the order of the rows, since GL's are bottom-to-top, but our
bsalomon@google.comc6980972011-11-02 19:57:21 +00001677 // API presents top-to-bottom. We must preserve the padding contents. Note
1678 // that the above readPixels did not overwrite the padding.
1679 if (readDst == buffer) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001680 SkASSERT(rowBytes == readDstRowBytes);
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001681 if (flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001682 scratch.reset(tightRowBytes);
1683 void* tmpRow = scratch.get();
1684 // flip y in-place by rows
1685 const int halfY = height >> 1;
1686 char* top = reinterpret_cast<char*>(buffer);
1687 char* bottom = top + (height - 1) * rowBytes;
1688 for (int y = 0; y < halfY; y++) {
1689 memcpy(tmpRow, top, tightRowBytes);
1690 memcpy(top, bottom, tightRowBytes);
1691 memcpy(bottom, tmpRow, tightRowBytes);
1692 top += rowBytes;
1693 bottom -= rowBytes;
1694 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001695 }
1696 } else {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001697 SkASSERT(readDst != buffer); SkASSERT(rowBytes != tightRowBytes);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001698 // copy from readDst to buffer while flipping y
caryclark@google.comcf6285b2012-06-06 12:09:01 +00001699 // const int halfY = height >> 1;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001700 const char* src = reinterpret_cast<const char*>(readDst);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001701 char* dst = reinterpret_cast<char*>(buffer);
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001702 if (flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001703 dst += (height-1) * rowBytes;
1704 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001705 for (int y = 0; y < height; y++) {
1706 memcpy(dst, src, tightRowBytes);
1707 src += readDstRowBytes;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001708 if (!flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001709 dst += rowBytes;
1710 } else {
1711 dst -= rowBytes;
1712 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001713 }
1714 }
1715 return true;
1716}
1717
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001718void GrGpuGL::flushRenderTarget(const SkIRect* bound) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001719
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001720 GrGLRenderTarget* rt =
1721 static_cast<GrGLRenderTarget*>(this->drawState()->getRenderTarget());
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001722 SkASSERT(NULL != rt);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001723
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001724 if (fHWBoundRenderTarget != rt) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001725 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, rt->renderFBOID()));
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +00001726#ifdef SK_DEBUG
rmistry@google.comd6bab022013-12-02 13:50:38 +00001727 // don't do this check in Chromium -- this is causing
1728 // lots of repeated command buffer flushes when the compositor is
1729 // rendering with Ganesh, which is really slow; even too slow for
1730 // Debug mode.
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +00001731 if (!this->glContext().isChromium()) {
rmistry@google.comd6bab022013-12-02 13:50:38 +00001732 GrGLenum status;
1733 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1734 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
1735 GrPrintf("GrGpuGL::flushRenderTarget glCheckFramebufferStatus %x\n", status);
1736 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001737 }
robertphillips@google.coma6ffb582013-04-29 16:50:17 +00001738#endif
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001739 fHWBoundRenderTarget = rt;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001740 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.coma3201942012-06-21 19:58:20 +00001741 if (fHWViewport != vp) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001742 vp.pushToGLViewport(this->glInterface());
bsalomon@google.coma3201942012-06-21 19:58:20 +00001743 fHWViewport = vp;
reed@google.comac10a2d2010-12-22 21:39:39 +00001744 }
1745 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001746 if (NULL == bound || !bound->isEmpty()) {
1747 rt->flagAsNeedingResolve(bound);
1748 }
skia.committer@gmail.comaeefb2a2013-07-27 07:01:06 +00001749
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +00001750 GrTexture *texture = rt->asTexture();
commit-bot@chromium.orge49157f2014-05-09 20:46:48 +00001751 if (NULL != texture) {
1752 texture->impl()->dirtyMipMaps(true);
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +00001753 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001754}
1755
twiz@google.com0f31ca72011-03-18 17:38:11 +00001756GrGLenum gPrimitiveType2GLMode[] = {
1757 GR_GL_TRIANGLES,
1758 GR_GL_TRIANGLE_STRIP,
1759 GR_GL_TRIANGLE_FAN,
1760 GR_GL_POINTS,
1761 GR_GL_LINES,
1762 GR_GL_LINE_STRIP
reed@google.comac10a2d2010-12-22 21:39:39 +00001763};
1764
bsalomon@google.comd302f142011-03-03 13:54:13 +00001765#define SWAP_PER_DRAW 0
1766
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001767#if SWAP_PER_DRAW
commit-bot@chromium.org43823302013-09-25 20:57:51 +00001768 #if defined(SK_BUILD_FOR_MAC)
bsalomon@google.comd302f142011-03-03 13:54:13 +00001769 #include <AGL/agl.h>
commit-bot@chromium.org43823302013-09-25 20:57:51 +00001770 #elif defined(SK_BUILD_FOR_WIN32)
bsalomon@google.comce7357d2012-06-25 17:49:25 +00001771 #include <gl/GL.h>
bsalomon@google.comd302f142011-03-03 13:54:13 +00001772 void SwapBuf() {
1773 DWORD procID = GetCurrentProcessId();
1774 HWND hwnd = GetTopWindow(GetDesktopWindow());
1775 while(hwnd) {
1776 DWORD wndProcID = 0;
1777 GetWindowThreadProcessId(hwnd, &wndProcID);
1778 if(wndProcID == procID) {
1779 SwapBuffers(GetDC(hwnd));
1780 }
1781 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
1782 }
1783 }
1784 #endif
1785#endif
1786
bsalomon@google.com74749cd2013-01-30 16:12:41 +00001787void GrGpuGL::onGpuDraw(const DrawInfo& info) {
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00001788 size_t indexOffsetInBytes;
1789 this->setupGeometry(info, &indexOffsetInBytes);
reed@google.comac10a2d2010-12-22 21:39:39 +00001790
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +00001791 SkASSERT((size_t)info.primitiveType() < SK_ARRAY_COUNT(gPrimitiveType2GLMode));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001792
bsalomon@google.com74749cd2013-01-30 16:12:41 +00001793 if (info.isIndexed()) {
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00001794 GrGLvoid* indices =
1795 reinterpret_cast<GrGLvoid*>(indexOffsetInBytes + sizeof(uint16_t) * info.startIndex());
bsalomon@google.com74749cd2013-01-30 16:12:41 +00001796 // info.startVertex() was accounted for by setupGeometry.
1797 GL_CALL(DrawElements(gPrimitiveType2GLMode[info.primitiveType()],
1798 info.indexCount(),
1799 GR_GL_UNSIGNED_SHORT,
1800 indices));
1801 } else {
1802 // Pass 0 for parameter first. We have to adjust glVertexAttribPointer() to account for
1803 // startVertex in the DrawElements case. So we always rely on setupGeometry to have
1804 // accounted for startVertex.
1805 GL_CALL(DrawArrays(gPrimitiveType2GLMode[info.primitiveType()], 0, info.vertexCount()));
1806 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00001807#if SWAP_PER_DRAW
1808 glFlush();
commit-bot@chromium.org43823302013-09-25 20:57:51 +00001809 #if defined(SK_BUILD_FOR_MAC)
bsalomon@google.comd302f142011-03-03 13:54:13 +00001810 aglSwapBuffers(aglGetCurrentContext());
1811 int set_a_break_pt_here = 9;
1812 aglSwapBuffers(aglGetCurrentContext());
commit-bot@chromium.org43823302013-09-25 20:57:51 +00001813 #elif defined(SK_BUILD_FOR_WIN32)
bsalomon@google.comd302f142011-03-03 13:54:13 +00001814 SwapBuf();
1815 int set_a_break_pt_here = 9;
1816 SwapBuf();
1817 #endif
1818#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001819}
1820
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001821static GrGLenum gr_stencil_op_to_gl_path_rendering_fill_mode(GrStencilOp op) {
1822 switch (op) {
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001823 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +00001824 SkFAIL("Unexpected path fill.");
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001825 /* fallthrough */;
1826 case kIncClamp_StencilOp:
1827 return GR_GL_COUNT_UP;
1828 case kInvert_StencilOp:
1829 return GR_GL_INVERT;
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001830 }
1831}
1832
sugoi@google.com12b4e272012-12-06 20:13:11 +00001833void GrGpuGL::onGpuStencilPath(const GrPath* path, SkPath::FillType fill) {
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001834 SkASSERT(this->caps()->pathRenderingSupport());
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001835
1836 GrGLuint id = static_cast<const GrGLPath*>(path)->pathID();
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001837 SkASSERT(NULL != this->drawState()->getRenderTarget());
1838 SkASSERT(NULL != this->drawState()->getRenderTarget()->getStencilBuffer());
1839
1840 flushPathStencilSettings(fill);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001841
1842 // Decide how to manipulate the stencil buffer based on the fill rule.
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001843 SkASSERT(!fHWPathStencilSettings.isTwoSided());
1844
1845 GrGLenum fillMode =
1846 gr_stencil_op_to_gl_path_rendering_fill_mode(fHWPathStencilSettings.passOp(GrStencilSettings::kFront_Face));
1847 GrGLint writeMask = fHWPathStencilSettings.writeMask(GrStencilSettings::kFront_Face);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001848 GL_CALL(StencilFillPath(id, fillMode, writeMask));
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00001849}
1850
commit-bot@chromium.org32184d82013-10-09 15:14:18 +00001851void GrGpuGL::onGpuDrawPath(const GrPath* path, SkPath::FillType fill) {
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001852 SkASSERT(this->caps()->pathRenderingSupport());
1853
1854 GrGLuint id = static_cast<const GrGLPath*>(path)->pathID();
1855 SkASSERT(NULL != this->drawState()->getRenderTarget());
1856 SkASSERT(NULL != this->drawState()->getRenderTarget()->getStencilBuffer());
1857 SkASSERT(!fCurrentProgram->hasVertexShader());
1858
1859 flushPathStencilSettings(fill);
commit-bot@chromium.org32184d82013-10-09 15:14:18 +00001860 const SkStrokeRec& stroke = path->getStroke();
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001861
1862 SkPath::FillType nonInvertedFill = SkPath::ConvertToNonInverseFillType(fill);
1863 SkASSERT(!fHWPathStencilSettings.isTwoSided());
1864 GrGLenum fillMode =
1865 gr_stencil_op_to_gl_path_rendering_fill_mode(fHWPathStencilSettings.passOp(GrStencilSettings::kFront_Face));
1866 GrGLint writeMask = fHWPathStencilSettings.writeMask(GrStencilSettings::kFront_Face);
commit-bot@chromium.org32184d82013-10-09 15:14:18 +00001867
1868 if (stroke.isFillStyle() || SkStrokeRec::kStrokeAndFill_Style == stroke.getStyle()) {
1869 GL_CALL(StencilFillPath(id, fillMode, writeMask));
1870 }
1871 if (stroke.needToApply()) {
1872 GL_CALL(StencilStrokePath(id, 0xffff, writeMask));
1873 }
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001874
1875 if (nonInvertedFill == fill) {
commit-bot@chromium.org32184d82013-10-09 15:14:18 +00001876 if (stroke.needToApply()) {
1877 GL_CALL(CoverStrokePath(id, GR_GL_BOUNDING_BOX));
1878 } else {
1879 GL_CALL(CoverFillPath(id, GR_GL_BOUNDING_BOX));
1880 }
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001881 } else {
1882 GrDrawState* drawState = this->drawState();
1883 GrDrawState::AutoViewMatrixRestore avmr;
1884 SkRect bounds = SkRect::MakeLTRB(0, 0,
1885 SkIntToScalar(drawState->getRenderTarget()->width()),
1886 SkIntToScalar(drawState->getRenderTarget()->height()));
1887 SkMatrix vmi;
1888 // mapRect through persp matrix may not be correct
1889 if (!drawState->getViewMatrix().hasPerspective() && drawState->getViewInverse(&vmi)) {
1890 vmi.mapRect(&bounds);
1891 // theoretically could set bloat = 0, instead leave it because of matrix inversion
1892 // precision.
commit-bot@chromium.org18786512014-05-20 14:53:45 +00001893 SkScalar bloat = drawState->getViewMatrix().getMaxScale() * SK_ScalarHalf;
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001894 bounds.outset(bloat, bloat);
1895 } else {
1896 avmr.setIdentity(drawState);
1897 }
1898
1899 this->drawSimpleRect(bounds, NULL);
1900 }
1901}
1902
commit-bot@chromium.orgecc45362014-03-28 21:31:34 +00001903void GrGpuGL::onGpuDrawPaths(int pathCount, const GrPath** paths,
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +00001904 const SkMatrix* transforms,
1905 SkPath::FillType fill,
1906 SkStrokeRec::Style stroke) {
1907 SkASSERT(this->caps()->pathRenderingSupport());
1908 SkASSERT(NULL != this->drawState()->getRenderTarget());
1909 SkASSERT(NULL != this->drawState()->getRenderTarget()->getStencilBuffer());
1910 SkASSERT(!fCurrentProgram->hasVertexShader());
1911 SkASSERT(stroke != SkStrokeRec::kHairline_Style);
1912
1913 SkAutoMalloc pathData(pathCount * sizeof(GrGLuint));
1914 SkAutoMalloc transformData(pathCount * sizeof(GrGLfloat) * 6);
1915 GrGLfloat* transformValues =
1916 reinterpret_cast<GrGLfloat*>(transformData.get());
1917 GrGLuint* pathIDs = reinterpret_cast<GrGLuint*>(pathData.get());
1918
commit-bot@chromium.orgecc45362014-03-28 21:31:34 +00001919 for (int i = 0; i < pathCount; ++i) {
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +00001920 SkASSERT(transforms[i].asAffine(NULL));
1921 const SkMatrix& m = transforms[i];
1922 transformValues[i * 6] = m.getScaleX();
1923 transformValues[i * 6 + 1] = m.getSkewY();
1924 transformValues[i * 6 + 2] = m.getSkewX();
1925 transformValues[i * 6 + 3] = m.getScaleY();
1926 transformValues[i * 6 + 4] = m.getTranslateX();
1927 transformValues[i * 6 + 5] = m.getTranslateY();
1928 pathIDs[i] = static_cast<const GrGLPath*>(paths[i])->pathID();
1929 }
1930
1931 flushPathStencilSettings(fill);
1932
1933 SkPath::FillType nonInvertedFill =
1934 SkPath::ConvertToNonInverseFillType(fill);
1935
1936 SkASSERT(!fHWPathStencilSettings.isTwoSided());
1937 GrGLenum fillMode =
1938 gr_stencil_op_to_gl_path_rendering_fill_mode(
1939 fHWPathStencilSettings.passOp(GrStencilSettings::kFront_Face));
1940 GrGLint writeMask =
1941 fHWPathStencilSettings.writeMask(GrStencilSettings::kFront_Face);
1942
1943 bool doFill = stroke == SkStrokeRec::kFill_Style
1944 || stroke == SkStrokeRec::kStrokeAndFill_Style;
1945 bool doStroke = stroke == SkStrokeRec::kStroke_Style
1946 || stroke == SkStrokeRec::kStrokeAndFill_Style;
1947
1948 if (doFill) {
1949 GL_CALL(StencilFillPathInstanced(pathCount, GR_GL_UNSIGNED_INT,
1950 pathIDs, 0,
1951 fillMode, writeMask,
1952 GR_GL_AFFINE_2D, transformValues));
1953 }
1954 if (doStroke) {
1955 GL_CALL(StencilStrokePathInstanced(pathCount, GR_GL_UNSIGNED_INT,
1956 pathIDs, 0,
1957 0xffff, writeMask,
1958 GR_GL_AFFINE_2D, transformValues));
1959 }
1960
1961 if (nonInvertedFill == fill) {
1962 if (doStroke) {
1963 GL_CALL(CoverStrokePathInstanced(
1964 pathCount, GR_GL_UNSIGNED_INT, pathIDs, 0,
1965 GR_GL_BOUNDING_BOX_OF_BOUNDING_BOXES,
1966 GR_GL_AFFINE_2D, transformValues));
1967 } else {
1968 GL_CALL(CoverFillPathInstanced(
1969 pathCount, GR_GL_UNSIGNED_INT, pathIDs, 0,
1970 GR_GL_BOUNDING_BOX_OF_BOUNDING_BOXES,
1971 GR_GL_AFFINE_2D, transformValues));
1972
1973 }
1974 } else {
1975 GrDrawState* drawState = this->drawState();
1976 GrDrawState::AutoViewMatrixRestore avmr;
1977 SkRect bounds = SkRect::MakeLTRB(0, 0,
1978 SkIntToScalar(drawState->getRenderTarget()->width()),
1979 SkIntToScalar(drawState->getRenderTarget()->height()));
1980 SkMatrix vmi;
1981 // mapRect through persp matrix may not be correct
1982 if (!drawState->getViewMatrix().hasPerspective() && drawState->getViewInverse(&vmi)) {
1983 vmi.mapRect(&bounds);
1984 // theoretically could set bloat = 0, instead leave it because of matrix inversion
1985 // precision.
commit-bot@chromium.org18786512014-05-20 14:53:45 +00001986 SkScalar bloat = drawState->getViewMatrix().getMaxScale() * SK_ScalarHalf;
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +00001987 bounds.outset(bloat, bloat);
1988 } else {
1989 avmr.setIdentity(drawState);
1990 }
1991
1992 this->drawSimpleRect(bounds, NULL);
1993 }
1994}
1995
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001996void GrGpuGL::onResolveRenderTarget(GrRenderTarget* target) {
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001997 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001998 if (rt->needsResolve()) {
bsalomon@google.com347c3822013-05-01 20:10:01 +00001999 // Some extensions automatically resolves the texture when it is read.
2000 if (this->glCaps().usesMSAARenderBuffers()) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002001 SkASSERT(rt->textureFBOID() != rt->renderFBOID());
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002002 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER, rt->renderFBOID()));
2003 GL_CALL(BindFramebuffer(GR_GL_DRAW_FRAMEBUFFER, rt->textureFBOID()));
2004 // make sure we go through flushRenderTarget() since we've modified
2005 // the bound DRAW FBO ID.
2006 fHWBoundRenderTarget = NULL;
2007 const GrGLIRect& vp = rt->getViewport();
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00002008 const SkIRect dirtyRect = rt->getResolveRect();
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002009 GrGLIRect r;
2010 r.setRelativeTo(vp, dirtyRect.fLeft, dirtyRect.fTop,
2011 dirtyRect.width(), dirtyRect.height(), target->origin());
reed@google.comac10a2d2010-12-22 21:39:39 +00002012
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002013 GrAutoTRestore<ScissorState> asr;
bsalomon@google.com347c3822013-05-01 20:10:01 +00002014 if (GrGLCaps::kES_Apple_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002015 // Apple's extension uses the scissor as the blit bounds.
bsalomon@google.coma3201942012-06-21 19:58:20 +00002016 asr.reset(&fScissorState);
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002017 fScissorState.fEnabled = true;
2018 fScissorState.fRect = dirtyRect;
bsalomon@google.coma3201942012-06-21 19:58:20 +00002019 this->flushScissor();
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002020 GL_CALL(ResolveMultisampleFramebuffer());
2021 } else {
bsalomon@google.com347c3822013-05-01 20:10:01 +00002022 if (GrGLCaps::kDesktop_EXT_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002023 // this respects the scissor during the blit, so disable it.
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002024 asr.reset(&fScissorState);
2025 fScissorState.fEnabled = false;
2026 this->flushScissor();
2027 }
2028 int right = r.fLeft + r.fWidth;
2029 int top = r.fBottom + r.fHeight;
2030 GL_CALL(BlitFramebuffer(r.fLeft, r.fBottom, right, top,
2031 r.fLeft, r.fBottom, right, top,
2032 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00002033 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002034 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00002035 rt->flagAsResolved();
reed@google.comac10a2d2010-12-22 21:39:39 +00002036 }
2037}
2038
bsalomon@google.com411dad02012-06-05 20:24:20 +00002039namespace {
bsalomon@google.comd302f142011-03-03 13:54:13 +00002040
bsalomon@google.com411dad02012-06-05 20:24:20 +00002041GrGLenum gr_to_gl_stencil_func(GrStencilFunc basicFunc) {
2042 static const GrGLenum gTable[] = {
2043 GR_GL_ALWAYS, // kAlways_StencilFunc
2044 GR_GL_NEVER, // kNever_StencilFunc
2045 GR_GL_GREATER, // kGreater_StencilFunc
2046 GR_GL_GEQUAL, // kGEqual_StencilFunc
2047 GR_GL_LESS, // kLess_StencilFunc
2048 GR_GL_LEQUAL, // kLEqual_StencilFunc,
2049 GR_GL_EQUAL, // kEqual_StencilFunc,
2050 GR_GL_NOTEQUAL, // kNotEqual_StencilFunc,
2051 };
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +00002052 GR_STATIC_ASSERT(SK_ARRAY_COUNT(gTable) == kBasicStencilFuncCount);
bsalomon@google.com411dad02012-06-05 20:24:20 +00002053 GR_STATIC_ASSERT(0 == kAlways_StencilFunc);
2054 GR_STATIC_ASSERT(1 == kNever_StencilFunc);
2055 GR_STATIC_ASSERT(2 == kGreater_StencilFunc);
2056 GR_STATIC_ASSERT(3 == kGEqual_StencilFunc);
2057 GR_STATIC_ASSERT(4 == kLess_StencilFunc);
2058 GR_STATIC_ASSERT(5 == kLEqual_StencilFunc);
2059 GR_STATIC_ASSERT(6 == kEqual_StencilFunc);
2060 GR_STATIC_ASSERT(7 == kNotEqual_StencilFunc);
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002061 SkASSERT((unsigned) basicFunc < kBasicStencilFuncCount);
bsalomon@google.com411dad02012-06-05 20:24:20 +00002062
2063 return gTable[basicFunc];
2064}
2065
2066GrGLenum gr_to_gl_stencil_op(GrStencilOp op) {
2067 static const GrGLenum gTable[] = {
2068 GR_GL_KEEP, // kKeep_StencilOp
2069 GR_GL_REPLACE, // kReplace_StencilOp
2070 GR_GL_INCR_WRAP, // kIncWrap_StencilOp
2071 GR_GL_INCR, // kIncClamp_StencilOp
2072 GR_GL_DECR_WRAP, // kDecWrap_StencilOp
2073 GR_GL_DECR, // kDecClamp_StencilOp
2074 GR_GL_ZERO, // kZero_StencilOp
2075 GR_GL_INVERT, // kInvert_StencilOp
2076 };
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +00002077 GR_STATIC_ASSERT(SK_ARRAY_COUNT(gTable) == kStencilOpCount);
bsalomon@google.com411dad02012-06-05 20:24:20 +00002078 GR_STATIC_ASSERT(0 == kKeep_StencilOp);
2079 GR_STATIC_ASSERT(1 == kReplace_StencilOp);
2080 GR_STATIC_ASSERT(2 == kIncWrap_StencilOp);
2081 GR_STATIC_ASSERT(3 == kIncClamp_StencilOp);
2082 GR_STATIC_ASSERT(4 == kDecWrap_StencilOp);
2083 GR_STATIC_ASSERT(5 == kDecClamp_StencilOp);
2084 GR_STATIC_ASSERT(6 == kZero_StencilOp);
2085 GR_STATIC_ASSERT(7 == kInvert_StencilOp);
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002086 SkASSERT((unsigned) op < kStencilOpCount);
bsalomon@google.com411dad02012-06-05 20:24:20 +00002087 return gTable[op];
2088}
2089
2090void set_gl_stencil(const GrGLInterface* gl,
bsalomon@google.coma3201942012-06-21 19:58:20 +00002091 const GrStencilSettings& settings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00002092 GrGLenum glFace,
bsalomon@google.coma3201942012-06-21 19:58:20 +00002093 GrStencilSettings::Face grFace) {
2094 GrGLenum glFunc = gr_to_gl_stencil_func(settings.func(grFace));
2095 GrGLenum glFailOp = gr_to_gl_stencil_op(settings.failOp(grFace));
2096 GrGLenum glPassOp = gr_to_gl_stencil_op(settings.passOp(grFace));
2097
2098 GrGLint ref = settings.funcRef(grFace);
2099 GrGLint mask = settings.funcMask(grFace);
2100 GrGLint writeMask = settings.writeMask(grFace);
bsalomon@google.com411dad02012-06-05 20:24:20 +00002101
2102 if (GR_GL_FRONT_AND_BACK == glFace) {
2103 // we call the combined func just in case separate stencil is not
2104 // supported.
2105 GR_GL_CALL(gl, StencilFunc(glFunc, ref, mask));
2106 GR_GL_CALL(gl, StencilMask(writeMask));
2107 GR_GL_CALL(gl, StencilOp(glFailOp, glPassOp, glPassOp));
2108 } else {
2109 GR_GL_CALL(gl, StencilFuncSeparate(glFace, glFunc, ref, mask));
2110 GR_GL_CALL(gl, StencilMaskSeparate(glFace, writeMask));
2111 GR_GL_CALL(gl, StencilOpSeparate(glFace, glFailOp, glPassOp, glPassOp));
2112 }
2113}
2114}
bsalomon@google.comd302f142011-03-03 13:54:13 +00002115
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00002116void GrGpuGL::flushStencil(DrawType type) {
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00002117 if (kStencilPath_DrawType != type && fHWStencilSettings != fStencilSettings) {
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00002118 if (fStencilSettings.isDisabled()) {
2119 if (kNo_TriState != fHWStencilTestEnabled) {
2120 GL_CALL(Disable(GR_GL_STENCIL_TEST));
2121 fHWStencilTestEnabled = kNo_TriState;
2122 }
2123 } else {
2124 if (kYes_TriState != fHWStencilTestEnabled) {
2125 GL_CALL(Enable(GR_GL_STENCIL_TEST));
2126 fHWStencilTestEnabled = kYes_TriState;
2127 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00002128 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00002129 if (!fStencilSettings.isDisabled()) {
bsalomon@google.combcce8922013-03-25 15:38:39 +00002130 if (this->caps()->twoSidedStencilSupport()) {
bsalomon@google.com411dad02012-06-05 20:24:20 +00002131 set_gl_stencil(this->glInterface(),
bsalomon@google.coma3201942012-06-21 19:58:20 +00002132 fStencilSettings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00002133 GR_GL_FRONT,
bsalomon@google.coma3201942012-06-21 19:58:20 +00002134 GrStencilSettings::kFront_Face);
bsalomon@google.com411dad02012-06-05 20:24:20 +00002135 set_gl_stencil(this->glInterface(),
bsalomon@google.coma3201942012-06-21 19:58:20 +00002136 fStencilSettings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00002137 GR_GL_BACK,
bsalomon@google.coma3201942012-06-21 19:58:20 +00002138 GrStencilSettings::kBack_Face);
bsalomon@google.comd302f142011-03-03 13:54:13 +00002139 } else {
bsalomon@google.com411dad02012-06-05 20:24:20 +00002140 set_gl_stencil(this->glInterface(),
bsalomon@google.coma3201942012-06-21 19:58:20 +00002141 fStencilSettings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00002142 GR_GL_FRONT_AND_BACK,
bsalomon@google.coma3201942012-06-21 19:58:20 +00002143 GrStencilSettings::kFront_Face);
bsalomon@google.comd302f142011-03-03 13:54:13 +00002144 }
2145 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00002146 fHWStencilSettings = fStencilSettings;
reed@google.comac10a2d2010-12-22 21:39:39 +00002147 }
2148}
2149
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00002150void GrGpuGL::flushAAState(DrawType type) {
bsalomon@google.com202d1392013-03-19 18:58:08 +00002151// At least some ATI linux drivers will render GL_LINES incorrectly when MSAA state is enabled but
2152// the target is not multisampled. Single pixel wide lines are rendered thicker than 1 pixel wide.
2153#if 0
2154 // Replace RT_HAS_MSAA with this definition once this driver bug is no longer a relevant concern
2155 #define RT_HAS_MSAA rt->isMultisampled()
2156#else
2157 #define RT_HAS_MSAA (rt->isMultisampled() || kDrawLines_DrawType == type)
2158#endif
2159
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002160 const GrRenderTarget* rt = this->getDrawState().getRenderTarget();
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +00002161 if (kGL_GrGLStandard == this->glStandard()) {
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00002162 // ES doesn't support toggling GL_MULTISAMPLE and doesn't have
2163 // smooth lines.
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00002164 // we prefer smooth lines over multisampled lines
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00002165 bool smoothLines = false;
2166
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00002167 if (kDrawLines_DrawType == type) {
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00002168 smoothLines = this->willUseHWAALines();
2169 if (smoothLines) {
2170 if (kYes_TriState != fHWAAState.fSmoothLineEnabled) {
2171 GL_CALL(Enable(GR_GL_LINE_SMOOTH));
2172 fHWAAState.fSmoothLineEnabled = kYes_TriState;
2173 // must disable msaa to use line smoothing
bsalomon@google.com202d1392013-03-19 18:58:08 +00002174 if (RT_HAS_MSAA &&
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00002175 kNo_TriState != fHWAAState.fMSAAEnabled) {
2176 GL_CALL(Disable(GR_GL_MULTISAMPLE));
2177 fHWAAState.fMSAAEnabled = kNo_TriState;
2178 }
2179 }
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00002180 } else {
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00002181 if (kNo_TriState != fHWAAState.fSmoothLineEnabled) {
2182 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
2183 fHWAAState.fSmoothLineEnabled = kNo_TriState;
2184 }
2185 }
2186 }
bsalomon@google.com202d1392013-03-19 18:58:08 +00002187 if (!smoothLines && RT_HAS_MSAA) {
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00002188 // FIXME: GL_NV_pr doesn't seem to like MSAA disabled. The paths
2189 // convex hulls of each segment appear to get filled.
2190 bool enableMSAA = kStencilPath_DrawType == type ||
2191 this->getDrawState().isHWAntialiasState();
2192 if (enableMSAA) {
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00002193 if (kYes_TriState != fHWAAState.fMSAAEnabled) {
2194 GL_CALL(Enable(GR_GL_MULTISAMPLE));
2195 fHWAAState.fMSAAEnabled = kYes_TriState;
2196 }
2197 } else {
2198 if (kNo_TriState != fHWAAState.fMSAAEnabled) {
2199 GL_CALL(Disable(GR_GL_MULTISAMPLE));
2200 fHWAAState.fMSAAEnabled = kNo_TriState;
2201 }
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00002202 }
2203 }
2204 }
2205}
2206
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00002207void GrGpuGL::flushPathStencilSettings(SkPath::FillType fill) {
2208 GrStencilSettings pathStencilSettings;
2209 this->getPathStencilSettingsForFillType(fill, &pathStencilSettings);
2210 if (fHWPathStencilSettings != pathStencilSettings) {
2211 // Just the func, ref, and mask is set here. The op and write mask are params to the call
2212 // that draws the path to the SB (glStencilFillPath)
2213 GrGLenum func =
2214 gr_to_gl_stencil_func(pathStencilSettings.func(GrStencilSettings::kFront_Face));
2215 GL_CALL(PathStencilFunc(func,
2216 pathStencilSettings.funcRef(GrStencilSettings::kFront_Face),
2217 pathStencilSettings.funcMask(GrStencilSettings::kFront_Face)));
2218
2219 fHWPathStencilSettings = pathStencilSettings;
2220 }
2221}
2222
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00002223void GrGpuGL::flushBlend(bool isLines,
bsalomon@google.com86c1f712011-10-12 14:54:26 +00002224 GrBlendCoeff srcCoeff,
bsalomon@google.com271cffc2011-05-20 14:13:56 +00002225 GrBlendCoeff dstCoeff) {
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00002226 if (isLines && this->willUseHWAALines()) {
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002227 if (kYes_TriState != fHWBlendState.fEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002228 GL_CALL(Enable(GR_GL_BLEND));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002229 fHWBlendState.fEnabled = kYes_TriState;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002230 }
bsalomon@google.com47059542012-06-06 20:51:20 +00002231 if (kSA_GrBlendCoeff != fHWBlendState.fSrcCoeff ||
2232 kISA_GrBlendCoeff != fHWBlendState.fDstCoeff) {
2233 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[kSA_GrBlendCoeff],
2234 gXfermodeCoeff2Blend[kISA_GrBlendCoeff]));
2235 fHWBlendState.fSrcCoeff = kSA_GrBlendCoeff;
2236 fHWBlendState.fDstCoeff = kISA_GrBlendCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002237 }
2238 } else {
bsalomon@google.com86c1f712011-10-12 14:54:26 +00002239 // any optimization to disable blending should
2240 // have already been applied and tweaked the coeffs
2241 // to (1, 0).
bsalomon@google.com47059542012-06-06 20:51:20 +00002242 bool blendOff = kOne_GrBlendCoeff == srcCoeff &&
2243 kZero_GrBlendCoeff == dstCoeff;
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002244 if (blendOff) {
2245 if (kNo_TriState != fHWBlendState.fEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002246 GL_CALL(Disable(GR_GL_BLEND));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002247 fHWBlendState.fEnabled = kNo_TriState;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002248 }
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002249 } else {
2250 if (kYes_TriState != fHWBlendState.fEnabled) {
2251 GL_CALL(Enable(GR_GL_BLEND));
2252 fHWBlendState.fEnabled = kYes_TriState;
2253 }
2254 if (fHWBlendState.fSrcCoeff != srcCoeff ||
2255 fHWBlendState.fDstCoeff != dstCoeff) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002256 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
2257 gXfermodeCoeff2Blend[dstCoeff]));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002258 fHWBlendState.fSrcCoeff = srcCoeff;
2259 fHWBlendState.fDstCoeff = dstCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002260 }
bsalomon@google.coma5d056a2012-03-27 15:59:58 +00002261 GrColor blendConst = this->getDrawState().getBlendConstant();
bsalomon@google.com271cffc2011-05-20 14:13:56 +00002262 if ((BlendCoeffReferencesConstant(srcCoeff) ||
2263 BlendCoeffReferencesConstant(dstCoeff)) &&
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002264 (!fHWBlendState.fConstColorValid ||
2265 fHWBlendState.fConstColor != blendConst)) {
bsalomon@google.com75347472012-09-17 17:23:21 +00002266 GrGLfloat c[4];
2267 GrColorToRGBAFloat(blendConst, c);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002268 GL_CALL(BlendColor(c[0], c[1], c[2], c[3]));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002269 fHWBlendState.fConstColor = blendConst;
2270 fHWBlendState.fConstColorValid = true;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002271 }
2272 }
2273 }
2274}
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002275
commit-bot@chromium.org6364b5e2013-08-20 20:22:52 +00002276static inline GrGLenum tile_to_gl_wrap(SkShader::TileMode tm) {
bsalomon@google.comb8670992012-07-25 21:27:09 +00002277 static const GrGLenum gWrapModes[] = {
2278 GR_GL_CLAMP_TO_EDGE,
2279 GR_GL_REPEAT,
2280 GR_GL_MIRRORED_REPEAT
2281 };
commit-bot@chromium.org5d7ca952013-04-22 20:26:44 +00002282 GR_STATIC_ASSERT(SkShader::kTileModeCount == SK_ARRAY_COUNT(gWrapModes));
bsalomon@google.comb8670992012-07-25 21:27:09 +00002283 GR_STATIC_ASSERT(0 == SkShader::kClamp_TileMode);
2284 GR_STATIC_ASSERT(1 == SkShader::kRepeat_TileMode);
2285 GR_STATIC_ASSERT(2 == SkShader::kMirror_TileMode);
2286 return gWrapModes[tm];
2287}
2288
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002289void GrGpuGL::bindTexture(int unitIdx, const GrTextureParams& params, GrGLTexture* texture) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002290 SkASSERT(NULL != texture);
tomhudson@google.comd0c1a062012-07-12 17:23:52 +00002291
bsalomon@google.comb8670992012-07-25 21:27:09 +00002292 // If we created a rt/tex and rendered to it without using a texture and now we're texturing
2293 // 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 +00002294 // out of the "last != next" check.
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002295 GrGLRenderTarget* texRT = static_cast<GrGLRenderTarget*>(texture->asRenderTarget());
bsalomon@google.com4c883782012-06-04 19:05:11 +00002296 if (NULL != texRT) {
2297 this->onResolveRenderTarget(texRT);
2298 }
2299
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002300 if (fHWBoundTextures[unitIdx] != texture) {
2301 this->setTextureUnit(unitIdx);
2302 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, texture->textureID()));
2303 fHWBoundTextures[unitIdx] = texture;
bsalomon@google.com4c883782012-06-04 19:05:11 +00002304 }
2305
bsalomon@google.com4c883782012-06-04 19:05:11 +00002306 ResetTimestamp timestamp;
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002307 const GrGLTexture::TexParams& oldTexParams = texture->getCachedTexParams(&timestamp);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002308 bool setAll = timestamp < this->getResetTimestamp();
2309 GrGLTexture::TexParams newTexParams;
2310
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00002311 static GrGLenum glMinFilterModes[] = {
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +00002312 GR_GL_NEAREST,
2313 GR_GL_LINEAR,
2314 GR_GL_LINEAR_MIPMAP_LINEAR
2315 };
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00002316 static GrGLenum glMagFilterModes[] = {
2317 GR_GL_NEAREST,
2318 GR_GL_LINEAR,
2319 GR_GL_LINEAR
2320 };
commit-bot@chromium.org47442312013-12-19 16:18:01 +00002321 GrTextureParams::FilterMode filterMode = params.filterMode();
2322 if (!this->caps()->mipMapSupport() && GrTextureParams::kMipMap_FilterMode == filterMode) {
2323 filterMode = GrTextureParams::kBilerp_FilterMode;
2324 }
2325 newTexParams.fMinFilter = glMinFilterModes[filterMode];
2326 newTexParams.fMagFilter = glMagFilterModes[filterMode];
skia.committer@gmail.comaeefb2a2013-07-27 07:01:06 +00002327
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +00002328 if (GrTextureParams::kMipMap_FilterMode == filterMode &&
2329 texture->mipMapsAreDirty() && !GrPixelConfigIsCompressed(texture->config())) {
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +00002330 GL_CALL(GenerateMipmap(GR_GL_TEXTURE_2D));
2331 texture->dirtyMipMaps(false);
2332 }
bsalomon@google.com4c883782012-06-04 19:05:11 +00002333
bsalomon@google.comb8670992012-07-25 21:27:09 +00002334 newTexParams.fWrapS = tile_to_gl_wrap(params.getTileModeX());
2335 newTexParams.fWrapT = tile_to_gl_wrap(params.getTileModeY());
bsalomon@google.com4c883782012-06-04 19:05:11 +00002336 memcpy(newTexParams.fSwizzleRGBA,
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002337 GrGLShaderBuilder::GetTexParamSwizzle(texture->config(), this->glCaps()),
bsalomon@google.com4c883782012-06-04 19:05:11 +00002338 sizeof(newTexParams.fSwizzleRGBA));
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00002339 if (setAll || newTexParams.fMagFilter != oldTexParams.fMagFilter) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002340 this->setTextureUnit(unitIdx);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002341 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2342 GR_GL_TEXTURE_MAG_FILTER,
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00002343 newTexParams.fMagFilter));
2344 }
2345 if (setAll || newTexParams.fMinFilter != oldTexParams.fMinFilter) {
2346 this->setTextureUnit(unitIdx);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002347 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2348 GR_GL_TEXTURE_MIN_FILTER,
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00002349 newTexParams.fMinFilter));
bsalomon@google.com4c883782012-06-04 19:05:11 +00002350 }
2351 if (setAll || newTexParams.fWrapS != oldTexParams.fWrapS) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002352 this->setTextureUnit(unitIdx);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002353 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2354 GR_GL_TEXTURE_WRAP_S,
2355 newTexParams.fWrapS));
2356 }
2357 if (setAll || newTexParams.fWrapT != oldTexParams.fWrapT) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002358 this->setTextureUnit(unitIdx);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002359 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2360 GR_GL_TEXTURE_WRAP_T,
2361 newTexParams.fWrapT));
2362 }
2363 if (this->glCaps().textureSwizzleSupport() &&
2364 (setAll || memcmp(newTexParams.fSwizzleRGBA,
2365 oldTexParams.fSwizzleRGBA,
2366 sizeof(newTexParams.fSwizzleRGBA)))) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002367 this->setTextureUnit(unitIdx);
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +00002368 if (this->glStandard() == kGLES_GrGLStandard) {
commit-bot@chromium.org6364b5e2013-08-20 20:22:52 +00002369 // ES3 added swizzle support but not GL_TEXTURE_SWIZZLE_RGBA.
2370 const GrGLenum* swizzle = newTexParams.fSwizzleRGBA;
2371 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_SWIZZLE_R, swizzle[0]));
2372 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_SWIZZLE_G, swizzle[1]));
2373 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_SWIZZLE_B, swizzle[2]));
2374 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_SWIZZLE_A, swizzle[3]));
2375 } else {
2376 GR_STATIC_ASSERT(sizeof(newTexParams.fSwizzleRGBA[0]) == sizeof(GrGLint));
2377 const GrGLint* swizzle = reinterpret_cast<const GrGLint*>(newTexParams.fSwizzleRGBA);
2378 GL_CALL(TexParameteriv(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_SWIZZLE_RGBA, swizzle));
2379 }
bsalomon@google.com4c883782012-06-04 19:05:11 +00002380 }
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002381 texture->setCachedTexParams(newTexParams, this->getResetTimestamp());
bsalomon@google.com4c883782012-06-04 19:05:11 +00002382}
2383
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +00002384void GrGpuGL::setProjectionMatrix(const SkMatrix& matrix,
2385 const SkISize& renderTargetSize,
2386 GrSurfaceOrigin renderTargetOrigin) {
2387
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +00002388 SkASSERT(this->glCaps().pathRenderingSupport());
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +00002389
2390 if (renderTargetOrigin == fHWProjectionMatrixState.fRenderTargetOrigin &&
2391 renderTargetSize == fHWProjectionMatrixState.fRenderTargetSize &&
2392 matrix.cheapEqualTo(fHWProjectionMatrixState.fViewMatrix)) {
2393 return;
2394 }
2395
2396 fHWProjectionMatrixState.fViewMatrix = matrix;
2397 fHWProjectionMatrixState.fRenderTargetSize = renderTargetSize;
2398 fHWProjectionMatrixState.fRenderTargetOrigin = renderTargetOrigin;
2399
2400 GrGLfloat glMatrix[4 * 4];
commit-bot@chromium.org47c66dd2014-05-29 01:12:10 +00002401 fHWProjectionMatrixState.getRTAdjustedGLMatrix<4>(glMatrix);
commit-bot@chromium.orgf6696722014-04-25 06:21:30 +00002402 GL_CALL(MatrixLoadf(GR_GL_PROJECTION, glMatrix));
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +00002403}
2404
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +00002405void GrGpuGL::enablePathTexGen(int unitIdx,
2406 PathTexGenComponents components,
2407 const GrGLfloat* coefficients) {
2408 SkASSERT(this->glCaps().pathRenderingSupport());
2409 SkASSERT(components >= kS_PathTexGenComponents &&
2410 components <= kSTR_PathTexGenComponents);
commit-bot@chromium.org8e919ad2013-10-21 14:48:23 +00002411 SkASSERT(this->glCaps().maxFixedFunctionTextureCoords() >= unitIdx);
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +00002412
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +00002413 if (GR_GL_OBJECT_LINEAR == fHWPathTexGenSettings[unitIdx].fMode &&
2414 components == fHWPathTexGenSettings[unitIdx].fNumComponents &&
2415 !memcmp(coefficients, fHWPathTexGenSettings[unitIdx].fCoefficients,
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +00002416 3 * components * sizeof(GrGLfloat))) {
2417 return;
2418 }
2419
2420 this->setTextureUnit(unitIdx);
2421
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +00002422 fHWPathTexGenSettings[unitIdx].fNumComponents = components;
2423 GL_CALL(PathTexGen(GR_GL_TEXTURE0 + unitIdx,
2424 GR_GL_OBJECT_LINEAR,
2425 components,
2426 coefficients));
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +00002427
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +00002428 memcpy(fHWPathTexGenSettings[unitIdx].fCoefficients, coefficients,
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +00002429 3 * components * sizeof(GrGLfloat));
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +00002430}
2431
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +00002432void GrGpuGL::enablePathTexGen(int unitIdx, PathTexGenComponents components,
2433 const SkMatrix& matrix) {
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +00002434 GrGLfloat coefficients[3 * 3];
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +00002435 SkASSERT(this->glCaps().pathRenderingSupport());
2436 SkASSERT(components >= kS_PathTexGenComponents &&
2437 components <= kSTR_PathTexGenComponents);
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +00002438
2439 coefficients[0] = SkScalarToFloat(matrix[SkMatrix::kMScaleX]);
2440 coefficients[1] = SkScalarToFloat(matrix[SkMatrix::kMSkewX]);
2441 coefficients[2] = SkScalarToFloat(matrix[SkMatrix::kMTransX]);
2442
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +00002443 if (components >= kST_PathTexGenComponents) {
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +00002444 coefficients[3] = SkScalarToFloat(matrix[SkMatrix::kMSkewY]);
2445 coefficients[4] = SkScalarToFloat(matrix[SkMatrix::kMScaleY]);
2446 coefficients[5] = SkScalarToFloat(matrix[SkMatrix::kMTransY]);
2447 }
2448
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +00002449 if (components >= kSTR_PathTexGenComponents) {
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +00002450 coefficients[6] = SkScalarToFloat(matrix[SkMatrix::kMPersp0]);
2451 coefficients[7] = SkScalarToFloat(matrix[SkMatrix::kMPersp1]);
2452 coefficients[8] = SkScalarToFloat(matrix[SkMatrix::kMPersp2]);
2453 }
2454
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +00002455 enablePathTexGen(unitIdx, components, coefficients);
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +00002456}
2457
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +00002458void GrGpuGL::flushPathTexGenSettings(int numUsedTexCoordSets) {
2459 SkASSERT(this->glCaps().pathRenderingSupport());
commit-bot@chromium.org8e919ad2013-10-21 14:48:23 +00002460 SkASSERT(this->glCaps().maxFixedFunctionTextureCoords() >= numUsedTexCoordSets);
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +00002461
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +00002462 // Only write the inactive path tex gens, since active path tex gens were
2463 // written when they were enabled.
commit-bot@chromium.org20807222013-11-01 11:54:54 +00002464
2465 SkDEBUGCODE(
2466 for (int i = 0; i < numUsedTexCoordSets; i++) {
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +00002467 SkASSERT(0 != fHWPathTexGenSettings[i].fNumComponents);
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +00002468 }
commit-bot@chromium.org20807222013-11-01 11:54:54 +00002469 );
2470
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +00002471 for (int i = numUsedTexCoordSets; i < fHWActivePathTexGenSets; i++) {
2472 SkASSERT(0 != fHWPathTexGenSettings[i].fNumComponents);
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +00002473
2474 this->setTextureUnit(i);
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +00002475 GL_CALL(PathTexGen(GR_GL_TEXTURE0 + i, GR_GL_NONE, 0, NULL));
2476 fHWPathTexGenSettings[i].fNumComponents = 0;
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +00002477 }
2478
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +00002479 fHWActivePathTexGenSets = numUsedTexCoordSets;
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +00002480}
2481
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002482void GrGpuGL::flushMiscFixedFunctionState() {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002483
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002484 const GrDrawState& drawState = this->getDrawState();
reed@google.comac10a2d2010-12-22 21:39:39 +00002485
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002486 if (drawState.isDitherState()) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002487 if (kYes_TriState != fHWDitherEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002488 GL_CALL(Enable(GR_GL_DITHER));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002489 fHWDitherEnabled = kYes_TriState;
2490 }
2491 } else {
2492 if (kNo_TriState != fHWDitherEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002493 GL_CALL(Disable(GR_GL_DITHER));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002494 fHWDitherEnabled = kNo_TriState;
reed@google.comac10a2d2010-12-22 21:39:39 +00002495 }
2496 }
2497
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002498 if (drawState.isColorWriteDisabled()) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002499 if (kNo_TriState != fHWWriteToColor) {
2500 GL_CALL(ColorMask(GR_GL_FALSE, GR_GL_FALSE,
2501 GR_GL_FALSE, GR_GL_FALSE));
2502 fHWWriteToColor = kNo_TriState;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002503 }
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002504 } else {
2505 if (kYes_TriState != fHWWriteToColor) {
2506 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
2507 fHWWriteToColor = kYes_TriState;
2508 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00002509 }
2510
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002511 if (fHWDrawFace != drawState.getDrawFace()) {
bsalomon@google.coma5d056a2012-03-27 15:59:58 +00002512 switch (this->getDrawState().getDrawFace()) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002513 case GrDrawState::kCCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002514 GL_CALL(Enable(GR_GL_CULL_FACE));
2515 GL_CALL(CullFace(GR_GL_BACK));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002516 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002517 case GrDrawState::kCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002518 GL_CALL(Enable(GR_GL_CULL_FACE));
2519 GL_CALL(CullFace(GR_GL_FRONT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002520 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002521 case GrDrawState::kBoth_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002522 GL_CALL(Disable(GR_GL_CULL_FACE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002523 break;
2524 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +00002525 SkFAIL("Unknown draw face.");
bsalomon@google.comd302f142011-03-03 13:54:13 +00002526 }
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002527 fHWDrawFace = drawState.getDrawFace();
bsalomon@google.comd302f142011-03-03 13:54:13 +00002528 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002529}
2530
reed@google.comac10a2d2010-12-22 21:39:39 +00002531void GrGpuGL::notifyRenderTargetDelete(GrRenderTarget* renderTarget) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002532 SkASSERT(NULL != renderTarget);
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002533 if (fHWBoundRenderTarget == renderTarget) {
2534 fHWBoundRenderTarget = NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +00002535 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002536}
2537
2538void GrGpuGL::notifyTextureDelete(GrGLTexture* texture) {
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002539 for (int s = 0; s < fHWBoundTextures.count(); ++s) {
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002540 if (fHWBoundTextures[s] == texture) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002541 // deleting bound texture does implied bind to 0
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002542 fHWBoundTextures[s] = NULL;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002543 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002544 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002545}
2546
cdalton51192342014-06-09 11:16:58 -07002547
2548GrGLuint GrGpuGL::createGLPathObject() {
2549 if (NULL == fPathNameAllocator.get()) {
2550 static const int range = 65536;
2551 GrGLuint firstName;
2552 GL_CALL_RET(firstName, GenPaths(range));
2553 fPathNameAllocator.reset(SkNEW_ARGS(GrGLNameAllocator, (firstName, firstName + range)));
2554 }
2555
2556 GrGLuint name = fPathNameAllocator->allocateName();
2557 if (0 == name) {
2558 // Our reserved path names are all in use. Fall back on GenPaths.
2559 GL_CALL_RET(name, GenPaths(1));
2560 }
2561
2562 return name;
2563}
2564
2565void GrGpuGL::deleteGLPathObject(GrGLuint name) {
2566 if (NULL == fPathNameAllocator.get() ||
2567 name < fPathNameAllocator->firstName() ||
2568 name >= fPathNameAllocator->endName()) {
2569 // If we aren't inside fPathNameAllocator's range then this name was
2570 // generated by the GenPaths fallback (or else the name is unallocated).
2571 GL_CALL(DeletePaths(name, 1));
2572 return;
2573 }
2574
2575 // Make the path empty to save memory, but don't free the name in the driver.
2576 GL_CALL(PathCommands(name, 0, NULL, 0, GR_GL_FLOAT, NULL));
2577 fPathNameAllocator->free(name);
2578}
2579
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002580bool GrGpuGL::configToGLFormats(GrPixelConfig config,
2581 bool getSizedInternalFormat,
2582 GrGLenum* internalFormat,
2583 GrGLenum* externalFormat,
2584 GrGLenum* externalType) {
2585 GrGLenum dontCare;
2586 if (NULL == internalFormat) {
2587 internalFormat = &dontCare;
2588 }
2589 if (NULL == externalFormat) {
2590 externalFormat = &dontCare;
2591 }
2592 if (NULL == externalType) {
2593 externalType = &dontCare;
2594 }
2595
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +00002596 if(!this->glCaps().isConfigTexturable(config)) {
2597 return false;
2598 }
2599
reed@google.comac10a2d2010-12-22 21:39:39 +00002600 switch (config) {
bsalomon@google.com0342a852012-08-20 19:22:38 +00002601 case kRGBA_8888_GrPixelConfig:
bsalomon@google.comc4364992011-11-07 15:54:49 +00002602 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002603 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002604 if (getSizedInternalFormat) {
2605 *internalFormat = GR_GL_RGBA8;
2606 } else {
2607 *internalFormat = GR_GL_RGBA;
2608 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002609 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.comc4364992011-11-07 15:54:49 +00002610 break;
bsalomon@google.com0342a852012-08-20 19:22:38 +00002611 case kBGRA_8888_GrPixelConfig:
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002612 if (this->glCaps().bgraIsInternalFormat()) {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002613 if (getSizedInternalFormat) {
2614 *internalFormat = GR_GL_BGRA8;
2615 } else {
2616 *internalFormat = GR_GL_BGRA;
2617 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002618 } else {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002619 if (getSizedInternalFormat) {
2620 *internalFormat = GR_GL_RGBA8;
2621 } else {
2622 *internalFormat = GR_GL_RGBA;
2623 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002624 }
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002625 *externalFormat = GR_GL_BGRA;
bsalomon@google.com6f379512011-11-16 20:36:03 +00002626 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002627 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002628 case kRGB_565_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002629 *internalFormat = GR_GL_RGB;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002630 *externalFormat = GR_GL_RGB;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002631 if (getSizedInternalFormat) {
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +00002632 if (this->glStandard() == kGL_GrGLStandard) {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002633 return false;
2634 } else {
2635 *internalFormat = GR_GL_RGB565;
2636 }
2637 } else {
2638 *internalFormat = GR_GL_RGB;
2639 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002640 *externalType = GR_GL_UNSIGNED_SHORT_5_6_5;
reed@google.comac10a2d2010-12-22 21:39:39 +00002641 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002642 case kRGBA_4444_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002643 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002644 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002645 if (getSizedInternalFormat) {
2646 *internalFormat = GR_GL_RGBA4;
2647 } else {
2648 *internalFormat = GR_GL_RGBA;
2649 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002650 *externalType = GR_GL_UNSIGNED_SHORT_4_4_4_4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002651 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002652 case kIndex_8_GrPixelConfig:
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +00002653 // glCompressedTexImage doesn't take external params
2654 *externalFormat = GR_GL_PALETTE8_RGBA8;
2655 // no sized/unsized internal format distinction here
2656 *internalFormat = GR_GL_PALETTE8_RGBA8;
2657 // unused with CompressedTexImage
2658 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002659 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002660 case kAlpha_8_GrPixelConfig:
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002661 if (this->glCaps().textureRedSupport()) {
2662 *internalFormat = GR_GL_RED;
2663 *externalFormat = GR_GL_RED;
2664 if (getSizedInternalFormat) {
2665 *internalFormat = GR_GL_R8;
2666 } else {
2667 *internalFormat = GR_GL_RED;
2668 }
2669 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002670 } else {
2671 *internalFormat = GR_GL_ALPHA;
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002672 *externalFormat = GR_GL_ALPHA;
2673 if (getSizedInternalFormat) {
2674 *internalFormat = GR_GL_ALPHA8;
2675 } else {
2676 *internalFormat = GR_GL_ALPHA;
2677 }
2678 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002679 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002680 break;
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +00002681 case kETC1_GrPixelConfig:
2682 *internalFormat = GR_GL_COMPRESSED_RGB8_ETC1;
2683 break;
2684 case kLATC_GrPixelConfig:
2685 switch(this->glCaps().latcAlias()) {
2686 case GrGLCaps::kLATC_LATCAlias:
2687 *internalFormat = GR_GL_COMPRESSED_LUMINANCE_LATC1;
2688 break;
2689 case GrGLCaps::kRGTC_LATCAlias:
2690 *internalFormat = GR_GL_COMPRESSED_RED_RGTC1;
2691 break;
2692 case GrGLCaps::k3DC_LATCAlias:
2693 *internalFormat = GR_GL_COMPRESSED_3DC_X;
2694 break;
2695 }
2696 break;
krajcevski238b4562014-06-30 09:09:22 -07002697 case kR11_EAC_GrPixelConfig:
2698 *internalFormat = GR_GL_COMPRESSED_R11;
2699 break;
reed@google.comac10a2d2010-12-22 21:39:39 +00002700 default:
2701 return false;
2702 }
2703 return true;
2704}
2705
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002706void GrGpuGL::setTextureUnit(int unit) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002707 SkASSERT(unit >= 0 && unit < fHWBoundTextures.count());
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002708 if (unit != fHWActiveTextureUnitIdx) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002709 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com49209392012-06-05 15:13:46 +00002710 fHWActiveTextureUnitIdx = unit;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002711 }
2712}
bsalomon@google.com316f99232011-01-13 21:28:12 +00002713
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002714void GrGpuGL::setScratchTextureUnit() {
2715 // Bind the last texture unit since it is the least likely to be used by GrGLProgram.
2716 int lastUnitIdx = fHWBoundTextures.count() - 1;
2717 if (lastUnitIdx != fHWActiveTextureUnitIdx) {
2718 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + lastUnitIdx));
2719 fHWActiveTextureUnitIdx = lastUnitIdx;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002720 }
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002721 // clear out the this field so that if a program does use this unit it will rebind the correct
2722 // texture.
2723 fHWBoundTextures[lastUnitIdx] = NULL;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002724}
2725
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002726namespace {
2727// Determines whether glBlitFramebuffer could be used between src and dst.
bsalomon@google.comeb851172013-04-15 13:51:00 +00002728inline bool can_blit_framebuffer(const GrSurface* dst,
2729 const GrSurface* src,
2730 const GrGpuGL* gpu,
2731 bool* wouldNeedTempFBO = NULL) {
commit-bot@chromium.org6b7938f2013-10-15 14:18:16 +00002732 if (gpu->glCaps().isConfigRenderable(dst->config(), dst->desc().fSampleCnt > 0) &&
2733 gpu->glCaps().isConfigRenderable(src->config(), src->desc().fSampleCnt > 0) &&
bsalomon@google.com347c3822013-05-01 20:10:01 +00002734 gpu->glCaps().usesMSAARenderBuffers()) {
commit-bot@chromium.orga8e5a062013-09-05 23:44:09 +00002735 // ES3 doesn't allow framebuffer blits when the src has MSAA and the configs don't match
2736 // or the rects are not the same (not just the same size but have the same edges).
2737 if (GrGLCaps::kES_3_0_MSFBOType == gpu->glCaps().msFBOType() &&
2738 (src->desc().fSampleCnt > 0 || src->config() != dst->config())) {
2739 return false;
2740 }
bsalomon@google.comeb851172013-04-15 13:51:00 +00002741 if (NULL != wouldNeedTempFBO) {
2742 *wouldNeedTempFBO = NULL == dst->asRenderTarget() || NULL == src->asRenderTarget();
2743 }
2744 return true;
2745 } else {
2746 return false;
2747 }
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002748}
bsalomon@google.comeb851172013-04-15 13:51:00 +00002749
2750inline bool can_copy_texsubimage(const GrSurface* dst,
2751 const GrSurface* src,
2752 const GrGpuGL* gpu,
2753 bool* wouldNeedTempFBO = NULL) {
2754 // Table 3.9 of the ES2 spec indicates the supported formats with CopyTexSubImage
2755 // and BGRA isn't in the spec. There doesn't appear to be any extension that adds it. Perhaps
2756 // many drivers would allow it to work, but ANGLE does not.
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +00002757 if (kGLES_GrGLStandard == gpu->glStandard() && gpu->glCaps().bgraIsInternalFormat() &&
bsalomon@google.comeb851172013-04-15 13:51:00 +00002758 (kBGRA_8888_GrPixelConfig == dst->config() || kBGRA_8888_GrPixelConfig == src->config())) {
2759 return false;
2760 }
2761 const GrGLRenderTarget* dstRT = static_cast<const GrGLRenderTarget*>(dst->asRenderTarget());
2762 // If dst is multisampled (and uses an extension where there is a separate MSAA renderbuffer)
2763 // then we don't want to copy to the texture but to the MSAA buffer.
2764 if (NULL != dstRT && dstRT->renderFBOID() != dstRT->textureFBOID()) {
2765 return false;
2766 }
bsalomon@google.coma2719852013-04-17 14:25:27 +00002767 const GrGLRenderTarget* srcRT = static_cast<const GrGLRenderTarget*>(src->asRenderTarget());
2768 // If the src is multisampled (and uses an extension where there is a separate MSAA
2769 // renderbuffer) then it is an invalid operation to call CopyTexSubImage
2770 if (NULL != srcRT && srcRT->renderFBOID() != srcRT->textureFBOID()) {
2771 return false;
2772 }
commit-bot@chromium.org6b7938f2013-10-15 14:18:16 +00002773 if (gpu->glCaps().isConfigRenderable(src->config(), src->desc().fSampleCnt > 0) &&
2774 NULL != dst->asTexture() &&
2775 dst->origin() == src->origin() &&
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +00002776 kIndex_8_GrPixelConfig != src->config() &&
2777 !GrPixelConfigIsCompressed(src->config())) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002778 if (NULL != wouldNeedTempFBO) {
2779 *wouldNeedTempFBO = NULL == src->asRenderTarget();
2780 }
2781 return true;
2782 } else {
2783 return false;
2784 }
2785}
2786
2787// If a temporary FBO was created, its non-zero ID is returned. The viewport that the copy rect is
2788// relative to is output.
2789inline GrGLuint bind_surface_as_fbo(const GrGLInterface* gl,
2790 GrSurface* surface,
2791 GrGLenum fboTarget,
2792 GrGLIRect* viewport) {
2793 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(surface->asRenderTarget());
2794 GrGLuint tempFBOID;
2795 if (NULL == rt) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002796 SkASSERT(NULL != surface->asTexture());
bsalomon@google.comeb851172013-04-15 13:51:00 +00002797 GrGLuint texID = static_cast<GrGLTexture*>(surface->asTexture())->textureID();
2798 GR_GL_CALL(gl, GenFramebuffers(1, &tempFBOID));
2799 GR_GL_CALL(gl, BindFramebuffer(fboTarget, tempFBOID));
2800 GR_GL_CALL(gl, FramebufferTexture2D(fboTarget,
2801 GR_GL_COLOR_ATTACHMENT0,
2802 GR_GL_TEXTURE_2D,
2803 texID,
2804 0));
2805 viewport->fLeft = 0;
2806 viewport->fBottom = 0;
2807 viewport->fWidth = surface->width();
2808 viewport->fHeight = surface->height();
2809 } else {
2810 tempFBOID = 0;
2811 GR_GL_CALL(gl, BindFramebuffer(fboTarget, rt->renderFBOID()));
2812 *viewport = rt->getViewport();
2813 }
2814 return tempFBOID;
2815}
2816
2817}
2818
2819void GrGpuGL::initCopySurfaceDstDesc(const GrSurface* src, GrTextureDesc* desc) {
2820 // Check for format issues with glCopyTexSubImage2D
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +00002821 if (kGLES_GrGLStandard == this->glStandard() && this->glCaps().bgraIsInternalFormat() &&
bsalomon@google.comeb851172013-04-15 13:51:00 +00002822 kBGRA_8888_GrPixelConfig == src->config()) {
2823 // glCopyTexSubImage2D doesn't work with this config. We'll want to make it a render target
bsalomon@google.com31c4e892013-04-15 13:55:02 +00002824 // in order to call glBlitFramebuffer or to copy to it by rendering.
bsalomon@google.comeb851172013-04-15 13:51:00 +00002825 INHERITED::initCopySurfaceDstDesc(src, desc);
bsalomon@google.coma2719852013-04-17 14:25:27 +00002826 return;
bsalomon@google.comeb851172013-04-15 13:51:00 +00002827 } else if (NULL == src->asRenderTarget()) {
2828 // We don't want to have to create an FBO just to use glCopyTexSubImage2D. Let the base
2829 // class handle it by rendering.
2830 INHERITED::initCopySurfaceDstDesc(src, desc);
bsalomon@google.coma2719852013-04-17 14:25:27 +00002831 return;
2832 }
2833
2834 const GrGLRenderTarget* srcRT = static_cast<const GrGLRenderTarget*>(src->asRenderTarget());
2835 if (NULL != srcRT && srcRT->renderFBOID() != srcRT->textureFBOID()) {
2836 // It's illegal to call CopyTexSubImage2D on a MSAA renderbuffer.
2837 INHERITED::initCopySurfaceDstDesc(src, desc);
bsalomon@google.comeb851172013-04-15 13:51:00 +00002838 } else {
2839 desc->fConfig = src->config();
2840 desc->fOrigin = src->origin();
2841 desc->fFlags = kNone_GrTextureFlags;
2842 }
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002843}
2844
2845bool GrGpuGL::onCopySurface(GrSurface* dst,
2846 GrSurface* src,
2847 const SkIRect& srcRect,
2848 const SkIPoint& dstPoint) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002849 bool inheritedCouldCopy = INHERITED::onCanCopySurface(dst, src, srcRect, dstPoint);
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002850 bool copied = false;
bsalomon@google.comeb851172013-04-15 13:51:00 +00002851 bool wouldNeedTempFBO = false;
2852 if (can_copy_texsubimage(dst, src, this, &wouldNeedTempFBO) &&
2853 (!wouldNeedTempFBO || !inheritedCouldCopy)) {
2854 GrGLuint srcFBO;
2855 GrGLIRect srcVP;
2856 srcFBO = bind_surface_as_fbo(this->glInterface(), src, GR_GL_FRAMEBUFFER, &srcVP);
2857 GrGLTexture* dstTex = static_cast<GrGLTexture*>(dst->asTexture());
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002858 SkASSERT(NULL != dstTex);
bsalomon@google.comeb851172013-04-15 13:51:00 +00002859 // We modified the bound FBO
2860 fHWBoundRenderTarget = NULL;
2861 GrGLIRect srcGLRect;
2862 srcGLRect.setRelativeTo(srcVP,
2863 srcRect.fLeft,
2864 srcRect.fTop,
2865 srcRect.width(),
2866 srcRect.height(),
2867 src->origin());
2868
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002869 this->setScratchTextureUnit();
bsalomon@google.comeb851172013-04-15 13:51:00 +00002870 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, dstTex->textureID()));
2871 GrGLint dstY;
2872 if (kBottomLeft_GrSurfaceOrigin == dst->origin()) {
2873 dstY = dst->height() - (dstPoint.fY + srcGLRect.fHeight);
2874 } else {
2875 dstY = dstPoint.fY;
2876 }
2877 GL_CALL(CopyTexSubImage2D(GR_GL_TEXTURE_2D, 0,
2878 dstPoint.fX, dstY,
2879 srcGLRect.fLeft, srcGLRect.fBottom,
2880 srcGLRect.fWidth, srcGLRect.fHeight));
2881 copied = true;
2882 if (srcFBO) {
2883 GL_CALL(DeleteFramebuffers(1, &srcFBO));
2884 }
2885 } else if (can_blit_framebuffer(dst, src, this, &wouldNeedTempFBO) &&
2886 (!wouldNeedTempFBO || !inheritedCouldCopy)) {
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002887 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
2888 srcRect.width(), srcRect.height());
2889 bool selfOverlap = false;
2890 if (dst->isSameAs(src)) {
2891 selfOverlap = SkIRect::IntersectsNoEmptyCheck(dstRect, srcRect);
2892 }
2893
2894 if (!selfOverlap) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002895 GrGLuint dstFBO;
2896 GrGLuint srcFBO;
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002897 GrGLIRect dstVP;
2898 GrGLIRect srcVP;
bsalomon@google.comeb851172013-04-15 13:51:00 +00002899 dstFBO = bind_surface_as_fbo(this->glInterface(), dst, GR_GL_DRAW_FRAMEBUFFER, &dstVP);
2900 srcFBO = bind_surface_as_fbo(this->glInterface(), src, GR_GL_READ_FRAMEBUFFER, &srcVP);
2901 // We modified the bound FBO
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002902 fHWBoundRenderTarget = NULL;
2903 GrGLIRect srcGLRect;
2904 GrGLIRect dstGLRect;
2905 srcGLRect.setRelativeTo(srcVP,
2906 srcRect.fLeft,
2907 srcRect.fTop,
2908 srcRect.width(),
2909 srcRect.height(),
2910 src->origin());
2911 dstGLRect.setRelativeTo(dstVP,
2912 dstRect.fLeft,
2913 dstRect.fTop,
2914 dstRect.width(),
2915 dstRect.height(),
2916 dst->origin());
2917
2918 GrAutoTRestore<ScissorState> asr;
bsalomon@google.com347c3822013-05-01 20:10:01 +00002919 if (GrGLCaps::kDesktop_EXT_MSFBOType == this->glCaps().msFBOType()) {
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002920 // The EXT version applies the scissor during the blit, so disable it.
2921 asr.reset(&fScissorState);
2922 fScissorState.fEnabled = false;
2923 this->flushScissor();
2924 }
2925 GrGLint srcY0;
2926 GrGLint srcY1;
2927 // Does the blit need to y-mirror or not?
2928 if (src->origin() == dst->origin()) {
2929 srcY0 = srcGLRect.fBottom;
2930 srcY1 = srcGLRect.fBottom + srcGLRect.fHeight;
2931 } else {
2932 srcY0 = srcGLRect.fBottom + srcGLRect.fHeight;
2933 srcY1 = srcGLRect.fBottom;
2934 }
2935 GL_CALL(BlitFramebuffer(srcGLRect.fLeft,
2936 srcY0,
2937 srcGLRect.fLeft + srcGLRect.fWidth,
2938 srcY1,
2939 dstGLRect.fLeft,
2940 dstGLRect.fBottom,
2941 dstGLRect.fLeft + dstGLRect.fWidth,
2942 dstGLRect.fBottom + dstGLRect.fHeight,
2943 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
2944 if (dstFBO) {
2945 GL_CALL(DeleteFramebuffers(1, &dstFBO));
2946 }
2947 if (srcFBO) {
2948 GL_CALL(DeleteFramebuffers(1, &srcFBO));
2949 }
2950 copied = true;
2951 }
2952 }
bsalomon@google.comeb851172013-04-15 13:51:00 +00002953 if (!copied && inheritedCouldCopy) {
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002954 copied = INHERITED::onCopySurface(dst, src, srcRect, dstPoint);
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002955 SkASSERT(copied);
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002956 }
2957 return copied;
2958}
2959
2960bool GrGpuGL::onCanCopySurface(GrSurface* dst,
2961 GrSurface* src,
2962 const SkIRect& srcRect,
2963 const SkIPoint& dstPoint) {
2964 // This mirrors the logic in onCopySurface.
bsalomon@google.comeb851172013-04-15 13:51:00 +00002965 if (can_copy_texsubimage(dst, src, this)) {
2966 return true;
2967 }
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002968 if (can_blit_framebuffer(dst, src, this)) {
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002969 if (dst->isSameAs(src)) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002970 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
2971 srcRect.width(), srcRect.height());
2972 if(!SkIRect::IntersectsNoEmptyCheck(dstRect, srcRect)) {
2973 return true;
2974 }
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002975 } else {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002976 return true;
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002977 }
2978 }
bsalomon@google.comeb851172013-04-15 13:51:00 +00002979 return INHERITED::onCanCopySurface(dst, src, srcRect, dstPoint);
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002980}
2981
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +00002982void GrGpuGL::didAddGpuTraceMarker() {
commit-bot@chromium.orga3baf3b2014-02-21 18:45:30 +00002983 if (this->caps()->gpuTracingSupport()) {
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +00002984 const GrTraceMarkerSet& markerArray = this->getActiveTraceMarkers();
2985 SkString markerString = markerArray.toString();
2986 GL_CALL(PushGroupMarker(0, markerString.c_str()));
commit-bot@chromium.orga3baf3b2014-02-21 18:45:30 +00002987 }
2988}
2989
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +00002990void GrGpuGL::didRemoveGpuTraceMarker() {
commit-bot@chromium.orga3baf3b2014-02-21 18:45:30 +00002991 if (this->caps()->gpuTracingSupport()) {
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +00002992 GL_CALL(PopGroupMarker());
commit-bot@chromium.orga3baf3b2014-02-21 18:45:30 +00002993 }
2994}
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00002995///////////////////////////////////////////////////////////////////////////////
2996
bsalomon@google.com6918d482013-03-07 19:09:11 +00002997GrGLAttribArrayState* GrGpuGL::HWGeometryState::bindArrayAndBuffersToDraw(
2998 GrGpuGL* gpu,
2999 const GrGLVertexBuffer* vbuffer,
3000 const GrGLIndexBuffer* ibuffer) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00003001 SkASSERT(NULL != vbuffer);
robertphillips@google.com4f65a272013-03-26 19:40:46 +00003002 GrGLAttribArrayState* attribState;
3003
bsalomon@google.com6918d482013-03-07 19:09:11 +00003004 // We use a vertex array if we're on a core profile and the verts are in a VBO.
3005 if (gpu->glCaps().isCoreProfile() && !vbuffer->isCPUBacked()) {
commit-bot@chromium.org089a7802014-05-02 21:38:22 +00003006 if (NULL == fVBOVertexArray || fVBOVertexArray->wasDestroyed()) {
bsalomon@google.com6918d482013-03-07 19:09:11 +00003007 SkSafeUnref(fVBOVertexArray);
3008 GrGLuint arrayID;
3009 GR_GL_CALL(gpu->glInterface(), GenVertexArrays(1, &arrayID));
3010 int attrCount = gpu->glCaps().maxVertexAttributes();
3011 fVBOVertexArray = SkNEW_ARGS(GrGLVertexArray, (gpu, arrayID, attrCount));
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00003012 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00003013 attribState = fVBOVertexArray->bindWithIndexBuffer(ibuffer);
3014 } else {
3015 if (NULL != ibuffer) {
3016 this->setIndexBufferIDOnDefaultVertexArray(gpu, ibuffer->bufferID());
3017 } else {
3018 this->setVertexArrayID(gpu, 0);
3019 }
3020 int attrCount = gpu->glCaps().maxVertexAttributes();
3021 if (fDefaultVertexArrayAttribState.count() != attrCount) {
3022 fDefaultVertexArrayAttribState.resize(attrCount);
3023 }
3024 attribState = &fDefaultVertexArrayAttribState;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00003025 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00003026 return attribState;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00003027}