blob: 7131cfa1ebbf6afd8e46b32a8f0651fa85d9407f [file] [log] [blame]
reed@google.comac10a2d2010-12-22 21:39:39 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
reed@google.comac10a2d2010-12-22 21:39:39 +00006 */
7
epoger@google.comec3ed6a2011-07-28 14:26:00 +00008
reed@google.comac10a2d2010-12-22 21:39:39 +00009#include "GrGpuGL.h"
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000010#include "GrGLStencilBuffer.h"
bsalomon@google.com64aef2b2012-06-11 15:36:13 +000011#include "GrGLPath.h"
cdaltonb85a0aa2014-07-21 15:32:44 -070012#include "GrGLPathRange.h"
cdaltonc7103a12014-08-11 14:05:05 -070013#include "GrGLPathRendering.h"
bsalomon@google.com6d003d12012-09-11 15:45:20 +000014#include "GrGLShaderBuilder.h"
bsalomon@google.coma3201942012-06-21 19:58:20 +000015#include "GrTemplates.h"
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +000016#include "GrTypes.h"
commit-bot@chromium.org32184d82013-10-09 15:14:18 +000017#include "SkStrokeRec.h"
bsalomon@google.com3582bf92011-06-30 21:32:31 +000018#include "SkTemplates.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000019
bsalomon@google.com0b77d682011-08-19 13:28:54 +000020#define GL_CALL(X) GR_GL_CALL(this->glInterface(), X)
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +000021#define GL_CALL_RET(RET, X) GR_GL_CALL_RET(this->glInterface(), RET, X)
bsalomon@google.com0b77d682011-08-19 13:28:54 +000022
reed@google.comac10a2d2010-12-22 21:39:39 +000023#define SKIP_CACHE_CHECK true
24
bsalomon@google.com4f3c2532012-01-19 16:16:52 +000025#if GR_GL_CHECK_ALLOC_WITH_GET_ERROR
26 #define CLEAR_ERROR_BEFORE_ALLOC(iface) GrGLClearErr(iface)
27 #define GL_ALLOC_CALL(iface, call) GR_GL_CALL_NOERRCHECK(iface, call)
28 #define CHECK_ALLOC_ERROR(iface) GR_GL_GET_ERROR(iface)
rmistry@google.comfbfcd562012-08-23 18:09:54 +000029#else
bsalomon@google.com4f3c2532012-01-19 16:16:52 +000030 #define CLEAR_ERROR_BEFORE_ALLOC(iface)
31 #define GL_ALLOC_CALL(iface, call) GR_GL_CALL(iface, call)
32 #define CHECK_ALLOC_ERROR(iface) GR_GL_NO_ERROR
33#endif
34
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +000035
36///////////////////////////////////////////////////////////////////////////////
37
cdaltonb85a0aa2014-07-21 15:32:44 -070038static const GrGLenum gXformType2GLType[] = {
39 GR_GL_NONE,
40 GR_GL_TRANSLATE_X,
41 GR_GL_TRANSLATE_Y,
42 GR_GL_TRANSLATE_2D,
43 GR_GL_TRANSPOSE_AFFINE_2D
44};
45
46GR_STATIC_ASSERT(0 == GrDrawTarget::kNone_PathTransformType);
47GR_STATIC_ASSERT(1 == GrDrawTarget::kTranslateX_PathTransformType);
48GR_STATIC_ASSERT(2 == GrDrawTarget::kTranslateY_PathTransformType);
49GR_STATIC_ASSERT(3 == GrDrawTarget::kTranslate_PathTransformType);
50GR_STATIC_ASSERT(4 == GrDrawTarget::kAffine_PathTransformType);
51GR_STATIC_ASSERT(GrDrawTarget::kAffine_PathTransformType == GrDrawTarget::kLast_PathTransformType);
52
twiz@google.com0f31ca72011-03-18 17:38:11 +000053static const GrGLenum gXfermodeCoeff2Blend[] = {
54 GR_GL_ZERO,
55 GR_GL_ONE,
56 GR_GL_SRC_COLOR,
57 GR_GL_ONE_MINUS_SRC_COLOR,
58 GR_GL_DST_COLOR,
59 GR_GL_ONE_MINUS_DST_COLOR,
60 GR_GL_SRC_ALPHA,
61 GR_GL_ONE_MINUS_SRC_ALPHA,
62 GR_GL_DST_ALPHA,
63 GR_GL_ONE_MINUS_DST_ALPHA,
64 GR_GL_CONSTANT_COLOR,
65 GR_GL_ONE_MINUS_CONSTANT_COLOR,
66 GR_GL_CONSTANT_ALPHA,
67 GR_GL_ONE_MINUS_CONSTANT_ALPHA,
bsalomon@google.com271cffc2011-05-20 14:13:56 +000068
69 // extended blend coeffs
70 GR_GL_SRC1_COLOR,
71 GR_GL_ONE_MINUS_SRC1_COLOR,
72 GR_GL_SRC1_ALPHA,
73 GR_GL_ONE_MINUS_SRC1_ALPHA,
reed@google.comac10a2d2010-12-22 21:39:39 +000074};
75
bsalomon@google.com271cffc2011-05-20 14:13:56 +000076bool GrGpuGL::BlendCoeffReferencesConstant(GrBlendCoeff coeff) {
bsalomon@google.com080773c2011-03-15 19:09:25 +000077 static const bool gCoeffReferencesBlendConst[] = {
78 false,
79 false,
80 false,
81 false,
82 false,
83 false,
84 false,
85 false,
86 false,
87 false,
88 true,
89 true,
90 true,
91 true,
bsalomon@google.com271cffc2011-05-20 14:13:56 +000092
93 // extended blend coeffs
94 false,
95 false,
96 false,
97 false,
bsalomon@google.com080773c2011-03-15 19:09:25 +000098 };
99 return gCoeffReferencesBlendConst[coeff];
bsalomon@google.com47059542012-06-06 20:51:20 +0000100 GR_STATIC_ASSERT(kTotalGrBlendCoeffCount ==
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000101 SK_ARRAY_COUNT(gCoeffReferencesBlendConst));
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000102
bsalomon@google.com47059542012-06-06 20:51:20 +0000103 GR_STATIC_ASSERT(0 == kZero_GrBlendCoeff);
104 GR_STATIC_ASSERT(1 == kOne_GrBlendCoeff);
105 GR_STATIC_ASSERT(2 == kSC_GrBlendCoeff);
106 GR_STATIC_ASSERT(3 == kISC_GrBlendCoeff);
107 GR_STATIC_ASSERT(4 == kDC_GrBlendCoeff);
108 GR_STATIC_ASSERT(5 == kIDC_GrBlendCoeff);
109 GR_STATIC_ASSERT(6 == kSA_GrBlendCoeff);
110 GR_STATIC_ASSERT(7 == kISA_GrBlendCoeff);
111 GR_STATIC_ASSERT(8 == kDA_GrBlendCoeff);
112 GR_STATIC_ASSERT(9 == kIDA_GrBlendCoeff);
113 GR_STATIC_ASSERT(10 == kConstC_GrBlendCoeff);
114 GR_STATIC_ASSERT(11 == kIConstC_GrBlendCoeff);
115 GR_STATIC_ASSERT(12 == kConstA_GrBlendCoeff);
116 GR_STATIC_ASSERT(13 == kIConstA_GrBlendCoeff);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000117
bsalomon@google.com47059542012-06-06 20:51:20 +0000118 GR_STATIC_ASSERT(14 == kS2C_GrBlendCoeff);
119 GR_STATIC_ASSERT(15 == kIS2C_GrBlendCoeff);
120 GR_STATIC_ASSERT(16 == kS2A_GrBlendCoeff);
121 GR_STATIC_ASSERT(17 == kIS2A_GrBlendCoeff);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000122
123 // assertion for gXfermodeCoeff2Blend have to be in GrGpu scope
bsalomon@google.com47059542012-06-06 20:51:20 +0000124 GR_STATIC_ASSERT(kTotalGrBlendCoeffCount ==
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000125 SK_ARRAY_COUNT(gXfermodeCoeff2Blend));
bsalomon@google.com080773c2011-03-15 19:09:25 +0000126}
127
reed@google.comac10a2d2010-12-22 21:39:39 +0000128///////////////////////////////////////////////////////////////////////////////
129
rileya@google.come38160c2012-07-03 18:03:04 +0000130static bool gPrintStartupSpew;
bsalomon@google.com42ab7ea2011-01-19 17:19:40 +0000131
robertphillips@google.com6177e692013-02-28 20:16:25 +0000132GrGpuGL::GrGpuGL(const GrGLContext& ctx, GrContext* context)
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000133 : GrGpu(context)
robertphillips@google.com6177e692013-02-28 20:16:25 +0000134 , fGLContext(ctx) {
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000135
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000136 SkASSERT(ctx.isInitialized());
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +0000137 fCaps.reset(SkRef(ctx.caps()));
bsalomon@google.combcce8922013-03-25 15:38:39 +0000138
bsalomon1c63bf62014-07-22 13:09:46 -0700139 fHWBoundTextureUniqueIDs.reset(this->glCaps().maxFragmentTextureUnits());
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000140 fHWPathTexGenSettings.reset(this->glCaps().maxFixedFunctionTextureCoords());
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +0000141
robertphillips@google.com6177e692013-02-28 20:16:25 +0000142 GrGLClearErr(fGLContext.interface());
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000143 if (gPrintStartupSpew) {
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000144 const GrGLubyte* vendor;
145 const GrGLubyte* renderer;
146 const GrGLubyte* version;
147 GL_CALL_RET(vendor, GetString(GR_GL_VENDOR));
148 GL_CALL_RET(renderer, GetString(GR_GL_RENDERER));
149 GL_CALL_RET(version, GetString(GR_GL_VERSION));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000150 GrPrintf("------------------------- create GrGpuGL %p --------------\n",
151 this);
152 GrPrintf("------ VENDOR %s\n", vendor);
153 GrPrintf("------ RENDERER %s\n", renderer);
154 GrPrintf("------ VERSION %s\n", version);
bsalomon@google.com00142c42013-05-02 19:42:54 +0000155 GrPrintf("------ EXTENSIONS\n");
bsalomone904c092014-07-17 10:50:59 -0700156 ctx.extensions().print();
bsalomon@google.com00142c42013-05-02 19:42:54 +0000157 GrPrintf("\n");
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +0000158 GrPrintf(this->glCaps().dump().c_str());
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000159 }
160
commit-bot@chromium.org9188a152013-09-05 18:28:24 +0000161 fProgramCache = SkNEW_ARGS(ProgramCache, (this));
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000162
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000163 SkASSERT(this->glCaps().maxVertexAttributes() >= GrDrawState::kMaxVertexAttribCnt);
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000164
bsalomon@google.comfe676522011-06-17 18:12:21 +0000165 fLastSuccessfulStencilFmtIdx = 0;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000166 fHWProgramID = 0;
cdaltonc7103a12014-08-11 14:05:05 -0700167
168 if (this->glCaps().pathRenderingSupport()) {
169 fPathRendering.reset(GrGLPathRendering::Create(glInterface()));
170 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000171}
172
173GrGpuGL::~GrGpuGL() {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000174 if (0 != fHWProgramID) {
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000175 // detach the current program so there is no confusion on OpenGL's part
176 // that we want it to be deleted
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000177 SkASSERT(fHWProgramID == fCurrentProgram->programID());
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000178 GL_CALL(UseProgram(0));
179 }
180
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000181 delete fProgramCache;
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000182
bsalomon@google.com4a018bb2011-10-28 19:50:21 +0000183 // This must be called by before the GrDrawTarget destructor
184 this->releaseGeometry();
bsalomon1d89ddc2014-08-19 14:20:58 -0700185 // This subclass must do this before the base class destructor runs
186 // since we will unref the GrGLInterface.
187 this->releaseResources();
reed@google.comac10a2d2010-12-22 21:39:39 +0000188}
189
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000190///////////////////////////////////////////////////////////////////////////////
bungemanc7af8122014-07-16 09:10:41 -0700191
192
commit-bot@chromium.org28621512013-08-07 19:43:45 +0000193GrPixelConfig GrGpuGL::preferredReadPixelsConfig(GrPixelConfig readConfig,
194 GrPixelConfig surfaceConfig) const {
195 if (GR_GL_RGBA_8888_PIXEL_OPS_SLOW && kRGBA_8888_GrPixelConfig == readConfig) {
bsalomon@google.com9c680582013-02-06 18:17:50 +0000196 return kBGRA_8888_GrPixelConfig;
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +0000197 } else if (this->glContext().isMesa() &&
commit-bot@chromium.org28621512013-08-07 19:43:45 +0000198 GrBytesPerPixel(readConfig) == 4 &&
199 GrPixelConfigSwapRAndB(readConfig) == surfaceConfig) {
commit-bot@chromium.org5d1d79a2013-05-24 18:52:52 +0000200 // Mesa 3D takes a slow path on when reading back BGRA from an RGBA surface and vice-versa.
201 // Perhaps this should be guarded by some compiletime or runtime check.
202 return surfaceConfig;
bungemanc7af8122014-07-16 09:10:41 -0700203 } else if (readConfig == kBGRA_8888_GrPixelConfig &&
204 !this->glCaps().readPixelsSupported(this->glInterface(),
205 GR_GL_BGRA, GR_GL_UNSIGNED_BYTE)) {
commit-bot@chromium.org28621512013-08-07 19:43:45 +0000206 return kRGBA_8888_GrPixelConfig;
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000207 } else {
commit-bot@chromium.org28621512013-08-07 19:43:45 +0000208 return readConfig;
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000209 }
210}
211
commit-bot@chromium.org5d1d79a2013-05-24 18:52:52 +0000212GrPixelConfig GrGpuGL::preferredWritePixelsConfig(GrPixelConfig writeConfig,
213 GrPixelConfig surfaceConfig) const {
commit-bot@chromium.org28621512013-08-07 19:43:45 +0000214 if (GR_GL_RGBA_8888_PIXEL_OPS_SLOW && kRGBA_8888_GrPixelConfig == writeConfig) {
215 return kBGRA_8888_GrPixelConfig;
216 } else {
217 return writeConfig;
218 }
bsalomon@google.com9c680582013-02-06 18:17:50 +0000219}
220
221bool GrGpuGL::canWriteTexturePixels(const GrTexture* texture, GrPixelConfig srcConfig) const {
krajcevski145d48c2014-06-11 16:07:50 -0700222 if (kIndex_8_GrPixelConfig == srcConfig || kIndex_8_GrPixelConfig == texture->config()) {
bsalomon@google.com9c680582013-02-06 18:17:50 +0000223 return false;
224 }
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +0000225 if (srcConfig != texture->config() && kGLES_GrGLStandard == this->glStandard()) {
bsalomon@google.com9c680582013-02-06 18:17:50 +0000226 // In general ES2 requires the internal format of the texture and the format of the src
227 // pixels to match. However, It may or may not be possible to upload BGRA data to a RGBA
228 // texture. It depends upon which extension added BGRA. The Apple extension allows it
229 // (BGRA's internal format is RGBA) while the EXT extension does not (BGRA is its own
230 // internal format).
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +0000231 if (this->glCaps().isConfigTexturable(kBGRA_8888_GrPixelConfig) &&
bsalomon@google.com9c680582013-02-06 18:17:50 +0000232 !this->glCaps().bgraIsInternalFormat() &&
233 kBGRA_8888_GrPixelConfig == srcConfig &&
234 kRGBA_8888_GrPixelConfig == texture->config()) {
235 return true;
236 } else {
237 return false;
238 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000239 } else {
bsalomon@google.com9c680582013-02-06 18:17:50 +0000240 return true;
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000241 }
242}
243
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000244bool GrGpuGL::fullReadPixelsIsFasterThanPartial() const {
245 return SkToBool(GR_GL_FULL_READPIXELS_FASTER_THAN_PARTIAL);
246}
247
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000248void GrGpuGL::onResetContext(uint32_t resetBits) {
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000249 // we don't use the zb at all
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000250 if (resetBits & kMisc_GrGLBackendState) {
251 GL_CALL(Disable(GR_GL_DEPTH_TEST));
252 GL_CALL(DepthMask(GR_GL_FALSE));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000253
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000254 fHWDrawFace = GrDrawState::kInvalid_DrawFace;
255 fHWDitherEnabled = kUnknown_TriState;
reed@google.comac10a2d2010-12-22 21:39:39 +0000256
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +0000257 if (kGL_GrGLStandard == this->glStandard()) {
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000258 // Desktop-only state that we never change
259 if (!this->glCaps().isCoreProfile()) {
260 GL_CALL(Disable(GR_GL_POINT_SMOOTH));
261 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
262 GL_CALL(Disable(GR_GL_POLYGON_SMOOTH));
263 GL_CALL(Disable(GR_GL_POLYGON_STIPPLE));
264 GL_CALL(Disable(GR_GL_COLOR_LOGIC_OP));
265 GL_CALL(Disable(GR_GL_INDEX_LOGIC_OP));
266 }
267 // The windows NVIDIA driver has GL_ARB_imaging in the extension string when using a
268 // core profile. This seems like a bug since the core spec removes any mention of
269 // GL_ARB_imaging.
270 if (this->glCaps().imagingSupport() && !this->glCaps().isCoreProfile()) {
271 GL_CALL(Disable(GR_GL_COLOR_TABLE));
272 }
273 GL_CALL(Disable(GR_GL_POLYGON_OFFSET_FILL));
274 // Since ES doesn't support glPointSize at all we always use the VS to
275 // set the point size
276 GL_CALL(Enable(GR_GL_VERTEX_PROGRAM_POINT_SIZE));
277
278 // We should set glPolygonMode(FRONT_AND_BACK,FILL) here, too. It isn't
279 // currently part of our gl interface. There are probably others as
280 // well.
bsalomon@google.com2b1b8c02013-02-28 22:06:02 +0000281 }
joshualitt58162332014-08-01 06:44:53 -0700282
283 if (kGLES_GrGLStandard == this->glStandard() &&
284 fGLContext.hasExtension("GL_ARM_shader_framebuffer_fetch")) {
285 // The arm extension requires specifically enabling MSAA fetching per sample.
286 // On some devices this may have a perf hit. Also multiple render targets are disabled
287 GL_CALL(Enable(GR_GL_FETCH_PER_SAMPLE_ARM));
288 }
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000289 fHWWriteToColor = kUnknown_TriState;
290 // we only ever use lines in hairline mode
291 GL_CALL(LineWidth(1));
bsalomon@google.comcad107b2013-06-28 14:32:08 +0000292 }
edisonn@google.comba669992013-06-28 16:03:21 +0000293
egdanielb414f252014-07-29 13:15:47 -0700294 if (resetBits & kMSAAEnable_GrGLBackendState) {
295 fMSAAEnabled = kUnknown_TriState;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000296 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000297
commit-bot@chromium.org46fbfe02013-08-30 15:52:12 +0000298 fHWActiveTextureUnitIdx = -1; // invalid
299
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000300 if (resetBits & kTextureBinding_GrGLBackendState) {
bsalomon1c63bf62014-07-22 13:09:46 -0700301 for (int s = 0; s < fHWBoundTextureUniqueIDs.count(); ++s) {
302 fHWBoundTextureUniqueIDs[s] = SK_InvalidUniqueID;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000303 }
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000304 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000305
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000306 if (resetBits & kBlend_GrGLBackendState) {
307 fHWBlendState.invalidate();
308 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000309
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000310 if (resetBits & kView_GrGLBackendState) {
311 fHWScissorSettings.invalidate();
312 fHWViewport.invalidate();
313 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000314
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000315 if (resetBits & kStencil_GrGLBackendState) {
316 fHWStencilSettings.invalidate();
317 fHWStencilTestEnabled = kUnknown_TriState;
318 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000319
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000320 // Vertex
321 if (resetBits & kVertex_GrGLBackendState) {
322 fHWGeometryState.invalidate();
323 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000324
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000325 if (resetBits & kRenderTarget_GrGLBackendState) {
bsalomon1c63bf62014-07-22 13:09:46 -0700326 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000327 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000328
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000329 if (resetBits & kPathRendering_GrGLBackendState) {
330 if (this->caps()->pathRenderingSupport()) {
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000331 fHWProjectionMatrixState.invalidate();
332 // we don't use the model view matrix.
commit-bot@chromium.orgf6696722014-04-25 06:21:30 +0000333 GL_CALL(MatrixLoadIdentity(GR_GL_MODELVIEW));
commit-bot@chromium.org46fbfe02013-08-30 15:52:12 +0000334
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000335 for (int i = 0; i < this->glCaps().maxFixedFunctionTextureCoords(); ++i) {
cdaltonc7103a12014-08-11 14:05:05 -0700336 fPathRendering->pathTexGen(GR_GL_TEXTURE0 + i, GR_GL_NONE, 0, NULL);
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000337 fHWPathTexGenSettings[i].fMode = GR_GL_NONE;
338 fHWPathTexGenSettings[i].fNumComponents = 0;
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000339 }
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000340 fHWActivePathTexGenSets = 0;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000341 }
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000342 fHWPathStencilSettings.invalidate();
bsalomon@google.com05a718c2012-06-29 14:01:53 +0000343 }
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000344
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000345 // we assume these values
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000346 if (resetBits & kPixelStore_GrGLBackendState) {
347 if (this->glCaps().unpackRowLengthSupport()) {
348 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
349 }
350 if (this->glCaps().packRowLengthSupport()) {
351 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
352 }
353 if (this->glCaps().unpackFlipYSupport()) {
354 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
355 }
356 if (this->glCaps().packFlipYSupport()) {
357 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, GR_GL_FALSE));
358 }
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000359 }
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000360
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000361 if (resetBits & kProgram_GrGLBackendState) {
362 fHWProgramID = 0;
363 fSharedGLProgramState.invalidate();
364 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000365}
366
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000367namespace {
368
369GrSurfaceOrigin resolve_origin(GrSurfaceOrigin origin, bool renderTarget) {
370 // By default, GrRenderTargets are GL's normal orientation so that they
371 // can be drawn to by the outside world without the client having
372 // to render upside down.
373 if (kDefault_GrSurfaceOrigin == origin) {
374 return renderTarget ? kBottomLeft_GrSurfaceOrigin : kTopLeft_GrSurfaceOrigin;
375 } else {
376 return origin;
377 }
378}
379
380}
381
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000382GrTexture* GrGpuGL::onWrapBackendTexture(const GrBackendTextureDesc& desc) {
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000383 if (!this->configToGLFormats(desc.fConfig, false, NULL, NULL, NULL)) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000384 return NULL;
385 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000386
robertphillips@google.comb72e5d32012-10-30 15:18:10 +0000387 if (0 == desc.fTextureHandle) {
388 return NULL;
389 }
390
bsalomon@google.combcce8922013-03-25 15:38:39 +0000391 int maxSize = this->caps()->maxTextureSize();
robertphillips@google.comb72e5d32012-10-30 15:18:10 +0000392 if (desc.fWidth > maxSize || desc.fHeight > maxSize) {
393 return NULL;
394 }
395
396 GrGLTexture::Desc glTexDesc;
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000397 // next line relies on GrBackendTextureDesc's flags matching GrTexture's
robertphillips@google.com32716282012-06-04 12:48:45 +0000398 glTexDesc.fFlags = (GrTextureFlags) desc.fFlags;
bsalomon@google.com99621082011-11-15 16:47:16 +0000399 glTexDesc.fWidth = desc.fWidth;
400 glTexDesc.fHeight = desc.fHeight;
bsalomon@google.come269f212011-11-07 13:29:52 +0000401 glTexDesc.fConfig = desc.fConfig;
robertphillips@google.com32716282012-06-04 12:48:45 +0000402 glTexDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.come269f212011-11-07 13:29:52 +0000403 glTexDesc.fTextureID = static_cast<GrGLuint>(desc.fTextureHandle);
bsalomon@google.com72830222013-01-23 20:25:22 +0000404 glTexDesc.fIsWrapped = true;
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000405 bool renderTarget = SkToBool(desc.fFlags & kRenderTarget_GrBackendTextureFlag);
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000406 // FIXME: this should be calling resolve_origin(), but Chrome code is currently
407 // assuming the old behaviour, which is that backend textures are always
408 // BottomLeft, even for non-RT's. Once Chrome is fixed, change this to:
409 // glTexDesc.fOrigin = resolve_origin(desc.fOrigin, renderTarget);
410 if (kDefault_GrSurfaceOrigin == desc.fOrigin) {
411 glTexDesc.fOrigin = kBottomLeft_GrSurfaceOrigin;
412 } else {
413 glTexDesc.fOrigin = desc.fOrigin;
414 }
bsalomon@google.come269f212011-11-07 13:29:52 +0000415
416 GrGLTexture* texture = NULL;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000417 if (renderTarget) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000418 GrGLRenderTarget::Desc glRTDesc;
419 glRTDesc.fRTFBOID = 0;
420 glRTDesc.fTexFBOID = 0;
421 glRTDesc.fMSColorRenderbufferID = 0;
bsalomon@google.come269f212011-11-07 13:29:52 +0000422 glRTDesc.fConfig = desc.fConfig;
423 glRTDesc.fSampleCnt = desc.fSampleCnt;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000424 glRTDesc.fOrigin = glTexDesc.fOrigin;
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000425 glRTDesc.fCheckAllocation = false;
bsalomon@google.com99621082011-11-15 16:47:16 +0000426 if (!this->createRenderTargetObjects(glTexDesc.fWidth,
427 glTexDesc.fHeight,
bsalomon@google.come269f212011-11-07 13:29:52 +0000428 glTexDesc.fTextureID,
429 &glRTDesc)) {
430 return NULL;
431 }
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000432 texture = SkNEW_ARGS(GrGLTexture, (this, glTexDesc, glRTDesc));
bsalomon@google.come269f212011-11-07 13:29:52 +0000433 } else {
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000434 texture = SkNEW_ARGS(GrGLTexture, (this, glTexDesc));
bsalomon@google.come269f212011-11-07 13:29:52 +0000435 }
436 if (NULL == texture) {
437 return NULL;
438 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000439
bsalomon@google.come269f212011-11-07 13:29:52 +0000440 return texture;
441}
442
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000443GrRenderTarget* GrGpuGL::onWrapBackendRenderTarget(const GrBackendRenderTargetDesc& desc) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000444 GrGLRenderTarget::Desc glDesc;
445 glDesc.fConfig = desc.fConfig;
446 glDesc.fRTFBOID = static_cast<GrGLuint>(desc.fRenderTargetHandle);
447 glDesc.fMSColorRenderbufferID = 0;
448 glDesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
449 glDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.com72830222013-01-23 20:25:22 +0000450 glDesc.fIsWrapped = true;
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000451 glDesc.fCheckAllocation = false;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000452
453 glDesc.fOrigin = resolve_origin(desc.fOrigin, true);
bsalomon@google.come269f212011-11-07 13:29:52 +0000454 GrGLIRect viewport;
455 viewport.fLeft = 0;
456 viewport.fBottom = 0;
457 viewport.fWidth = desc.fWidth;
458 viewport.fHeight = desc.fHeight;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000459
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000460 GrRenderTarget* tgt = SkNEW_ARGS(GrGLRenderTarget,
461 (this, glDesc, viewport));
bsalomon@google.come269f212011-11-07 13:29:52 +0000462 if (desc.fStencilBits) {
463 GrGLStencilBuffer::Format format;
464 format.fInternalFormat = GrGLStencilBuffer::kUnknownInternalFormat;
465 format.fPacked = false;
466 format.fStencilBits = desc.fStencilBits;
467 format.fTotalBits = desc.fStencilBits;
bsalomon@google.com1f0f1a32013-01-23 21:32:32 +0000468 static const bool kIsSBWrapped = false;
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000469 GrGLStencilBuffer* sb = SkNEW_ARGS(GrGLStencilBuffer,
470 (this,
bsalomon@google.com1f0f1a32013-01-23 21:32:32 +0000471 kIsSBWrapped,
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000472 0,
473 desc.fWidth,
474 desc.fHeight,
475 desc.fSampleCnt,
476 format));
bsalomon@google.come269f212011-11-07 13:29:52 +0000477 tgt->setStencilBuffer(sb);
478 sb->unref();
479 }
480 return tgt;
481}
482
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000483////////////////////////////////////////////////////////////////////////////////
484
bsalomon@google.com9c680582013-02-06 18:17:50 +0000485bool GrGpuGL::onWriteTexturePixels(GrTexture* texture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000486 int left, int top, int width, int height,
487 GrPixelConfig config, const void* buffer,
488 size_t rowBytes) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000489 if (NULL == buffer) {
bsalomon@google.com9c680582013-02-06 18:17:50 +0000490 return false;
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000491 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000492 GrGLTexture* glTex = static_cast<GrGLTexture*>(texture);
493
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +0000494 this->setScratchTextureUnit();
bsalomon@google.com6f379512011-11-16 20:36:03 +0000495 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTex->textureID()));
496 GrGLTexture::Desc desc;
robertphillips@google.com32716282012-06-04 12:48:45 +0000497 desc.fFlags = glTex->desc().fFlags;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000498 desc.fWidth = glTex->width();
499 desc.fHeight = glTex->height();
robertphillips@google.com32716282012-06-04 12:48:45 +0000500 desc.fConfig = glTex->config();
501 desc.fSampleCnt = glTex->desc().fSampleCnt;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000502 desc.fTextureID = glTex->textureID();
bsalomon@google.com2d0bade2012-10-26 19:01:17 +0000503 desc.fOrigin = glTex->origin();
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000504
krajcevski145d48c2014-06-11 16:07:50 -0700505 bool success = false;
506 if (GrPixelConfigIsCompressed(desc.fConfig)) {
507 // We check that config == desc.fConfig in GrGpuGL::canWriteTexturePixels()
508 SkASSERT(config == desc.fConfig);
509 success = this->uploadCompressedTexData(desc, buffer, false,
510 left, top, width, height);
511 } else {
512 success = this->uploadTexData(desc, false,
513 left, top, width, height,
514 config, buffer, rowBytes);
515 }
516
517 if (success) {
commit-bot@chromium.orge49157f2014-05-09 20:46:48 +0000518 texture->impl()->dirtyMipMaps(true);
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +0000519 return true;
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +0000520 }
krajcevski145d48c2014-06-11 16:07:50 -0700521
522 return false;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000523}
524
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000525namespace {
526bool adjust_pixel_ops_params(int surfaceWidth,
527 int surfaceHeight,
528 size_t bpp,
529 int* left, int* top, int* width, int* height,
530 const void** data,
531 size_t* rowBytes) {
532 if (!*rowBytes) {
533 *rowBytes = *width * bpp;
534 }
535
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000536 SkIRect subRect = SkIRect::MakeXYWH(*left, *top, *width, *height);
537 SkIRect bounds = SkIRect::MakeWH(surfaceWidth, surfaceHeight);
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000538
539 if (!subRect.intersect(bounds)) {
540 return false;
541 }
542 *data = reinterpret_cast<const void*>(reinterpret_cast<intptr_t>(*data) +
543 (subRect.fTop - *top) * *rowBytes + (subRect.fLeft - *left) * bpp);
544
545 *left = subRect.fLeft;
546 *top = subRect.fTop;
547 *width = subRect.width();
548 *height = subRect.height();
549 return true;
550}
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000551
552GrGLenum check_alloc_error(const GrTextureDesc& desc, const GrGLInterface* interface) {
553 if (SkToBool(desc.fFlags & kCheckAllocation_GrTextureFlagBit)) {
554 return GR_GL_GET_ERROR(interface);
555 } else {
556 return CHECK_ALLOC_ERROR(interface);
557 }
558}
559
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000560}
561
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000562bool GrGpuGL::uploadTexData(const GrGLTexture::Desc& desc,
563 bool isNewTexture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000564 int left, int top, int width, int height,
565 GrPixelConfig dataConfig,
566 const void* data,
567 size_t rowBytes) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000568 SkASSERT(NULL != data || isNewTexture);
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000569
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +0000570 // If we're uploading compressed data then we should be using uploadCompressedTexData
571 SkASSERT(!GrPixelConfigIsCompressed(dataConfig));
572
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000573 size_t bpp = GrBytesPerPixel(dataConfig);
574 if (!adjust_pixel_ops_params(desc.fWidth, desc.fHeight, bpp, &left, &top,
575 &width, &height, &data, &rowBytes)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000576 return false;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000577 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000578 size_t trimRowBytes = width * bpp;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000579
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000580 // in case we need a temporary, trimmed copy of the src pixels
georgeb62508b2014-08-12 18:00:47 -0700581 GrAutoMalloc<128 * 128> tempStorage;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000582
commit-bot@chromium.orgcea9abb2013-12-09 19:15:37 +0000583 // We currently lazily create MIPMAPs when the we see a draw with
584 // GrTextureParams::kMipMap_FilterMode. Using texture storage requires that the
585 // MIP levels are all created when the texture is created. So for now we don't use
586 // texture storage.
587 bool useTexStorage = false &&
588 isNewTexture &&
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000589 this->glCaps().texStorageSupport();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000590
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +0000591 if (useTexStorage && kGL_GrGLStandard == this->glStandard()) {
bsalomon@google.com313f0192012-07-10 17:21:02 +0000592 // 565 is not a sized internal format on desktop GL. So on desktop with
593 // 565 we always use an unsized internal format to let the system pick
594 // the best sized format to convert the 565 data to. Since TexStorage
595 // only allows sized internal formats we will instead use TexImage2D.
596 useTexStorage = desc.fConfig != kRGB_565_GrPixelConfig;
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000597 }
598
599 GrGLenum internalFormat;
bsalomone904c092014-07-17 10:50:59 -0700600 GrGLenum externalFormat = 0x0; // suprress warning
601 GrGLenum externalType = 0x0;// suprress warning
602
commit-bot@chromium.org87002952013-08-20 15:12:01 +0000603 // glTexStorage requires sized internal formats on both desktop and ES. ES2 requires an unsized
604 // format for glTexImage, unlike ES3 and desktop. However, we allow the driver to decide the
605 // size of the internal format whenever possible and so only use a sized internal format when
606 // using texture storage.
bsalomone904c092014-07-17 10:50:59 -0700607 bool useSizedFormat = useTexStorage;
608 // At least some versions of the desktop ES3 drivers for NVIDIA won't accept GL_RED in
609 // glTexImage2D for the internal format but will accept GL_R8.
610 if (!useSizedFormat && kNVIDIA_GrGLVendor == this->glContext().vendor() &&
611 kGLES_GrGLStandard == this->glStandard() && this->glVersion() >= GR_GL_VER(3, 0)) {
612 useSizedFormat = true;
613 }
614 if (!this->configToGLFormats(dataConfig, useSizedFormat, &internalFormat,
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000615 &externalFormat, &externalType)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000616 return false;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000617 }
618
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000619 /*
bsalomon@google.com6f379512011-11-16 20:36:03 +0000620 * check whether to allocate a temporary buffer for flipping y or
621 * because our srcData has extra bytes past each row. If so, we need
622 * to trim those off here, since GL ES may not let us specify
623 * GL_UNPACK_ROW_LENGTH.
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000624 */
bsalomon@google.com6f379512011-11-16 20:36:03 +0000625 bool restoreGLRowLength = false;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000626 bool swFlipY = false;
627 bool glFlipY = false;
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000628 if (NULL != data) {
senorblanco@chromium.orgef5dbe12013-01-28 16:42:38 +0000629 if (kBottomLeft_GrSurfaceOrigin == desc.fOrigin) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000630 if (this->glCaps().unpackFlipYSupport()) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000631 glFlipY = true;
632 } else {
633 swFlipY = true;
634 }
635 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000636 if (this->glCaps().unpackRowLengthSupport() && !swFlipY) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000637 // can't use this for flipping, only non-neg values allowed. :(
638 if (rowBytes != trimRowBytes) {
639 GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
640 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength));
641 restoreGLRowLength = true;
642 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000643 } else {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000644 if (trimRowBytes != rowBytes || swFlipY) {
645 // copy data into our new storage, skipping the trailing bytes
646 size_t trimSize = height * trimRowBytes;
647 const char* src = (const char*)data;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000648 if (swFlipY) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000649 src += (height - 1) * rowBytes;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000650 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000651 char* dst = (char*)tempStorage.reset(trimSize);
652 for (int y = 0; y < height; y++) {
653 memcpy(dst, src, trimRowBytes);
654 if (swFlipY) {
655 src -= rowBytes;
656 } else {
657 src += rowBytes;
658 }
659 dst += trimRowBytes;
660 }
661 // now point data to our copied version
662 data = tempStorage.get();
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000663 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000664 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000665 if (glFlipY) {
666 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_TRUE));
667 }
joshualittee5da552014-07-16 13:32:56 -0700668 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT,
669 static_cast<GrGLint>(GrUnpackAlignment(dataConfig))));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000670 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000671 bool succeeded = true;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000672 if (isNewTexture &&
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000673 0 == left && 0 == top &&
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000674 desc.fWidth == width && desc.fHeight == height) {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000675 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000676 if (useTexStorage) {
commit-bot@chromium.orgcea9abb2013-12-09 19:15:37 +0000677 // We never resize or change formats of textures.
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000678 GL_ALLOC_CALL(this->glInterface(),
679 TexStorage2D(GR_GL_TEXTURE_2D,
680 1, // levels
681 internalFormat,
682 desc.fWidth, desc.fHeight));
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000683 } else {
bsalomond4cb9222014-08-11 14:19:09 -0700684 GL_ALLOC_CALL(this->glInterface(),
685 TexImage2D(GR_GL_TEXTURE_2D,
686 0, // level
687 internalFormat,
688 desc.fWidth, desc.fHeight,
689 0, // border
690 externalFormat, externalType,
691 data));
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000692 }
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000693 GrGLenum error = check_alloc_error(desc, this->glInterface());
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000694 if (error != GR_GL_NO_ERROR) {
695 succeeded = false;
696 } else {
697 // if we have data and we used TexStorage to create the texture, we
698 // now upload with TexSubImage.
699 if (NULL != data && useTexStorage) {
700 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D,
701 0, // level
702 left, top,
703 width, height,
704 externalFormat, externalType,
705 data));
706 }
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000707 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000708 } else {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000709 if (swFlipY || glFlipY) {
bsalomon@google.com6f379512011-11-16 20:36:03 +0000710 top = desc.fHeight - (top + height);
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000711 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000712 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D,
713 0, // level
714 left, top,
715 width, height,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000716 externalFormat, externalType, data));
717 }
718
719 if (restoreGLRowLength) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000720 SkASSERT(this->glCaps().unpackRowLengthSupport());
bsalomon@google.com6f379512011-11-16 20:36:03 +0000721 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000722 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000723 if (glFlipY) {
724 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
725 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000726 return succeeded;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000727}
728
krajcevski145d48c2014-06-11 16:07:50 -0700729// TODO: This function is using a lot of wonky semantics like, if width == -1
bungemanc7af8122014-07-16 09:10:41 -0700730// then set width = desc.fWdith ... blah. A better way to do it might be to
krajcevski145d48c2014-06-11 16:07:50 -0700731// create a CompressedTexData struct that takes a desc/ptr and figures out
732// the proper upload semantics. Then users can construct this function how they
733// see fit if they want to go against the "standard" way to do it.
krajcevski9c0e6292014-06-02 07:38:14 -0700734bool GrGpuGL::uploadCompressedTexData(const GrGLTexture::Desc& desc,
krajcevski145d48c2014-06-11 16:07:50 -0700735 const void* data,
736 bool isNewTexture,
737 int left, int top, int width, int height) {
738 SkASSERT(NULL != data || isNewTexture);
krajcevski9c0e6292014-06-02 07:38:14 -0700739
740 // No support for software flip y, yet...
741 SkASSERT(kBottomLeft_GrSurfaceOrigin != desc.fOrigin);
742
krajcevski145d48c2014-06-11 16:07:50 -0700743 if (-1 == width) {
744 width = desc.fWidth;
745 }
746#ifdef SK_DEBUG
747 else {
748 SkASSERT(width <= desc.fWidth);
749 }
750#endif
751
752 if (-1 == height) {
753 height = desc.fHeight;
754 }
755#ifdef SK_DEBUG
756 else {
757 SkASSERT(height <= desc.fHeight);
758 }
759#endif
760
krajcevski9c0e6292014-06-02 07:38:14 -0700761 // Make sure that the width and height that we pass to OpenGL
762 // is a multiple of the block size.
krajcevski145d48c2014-06-11 16:07:50 -0700763 int dataSize = GrCompressedFormatDataSize(desc.fConfig, width, height);
krajcevski9c0e6292014-06-02 07:38:14 -0700764
765 // We only need the internal format for compressed 2D textures.
766 GrGLenum internalFormat = 0;
767 if (!this->configToGLFormats(desc.fConfig, false, &internalFormat, NULL, NULL)) {
768 return false;
769 }
770
krajcevski145d48c2014-06-11 16:07:50 -0700771 if (isNewTexture) {
bsalomond4cb9222014-08-11 14:19:09 -0700772 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
krajcevski145d48c2014-06-11 16:07:50 -0700773 GL_ALLOC_CALL(this->glInterface(),
774 CompressedTexImage2D(GR_GL_TEXTURE_2D,
775 0, // level
776 internalFormat,
777 width, height,
778 0, // border
779 dataSize,
780 data));
bsalomond4cb9222014-08-11 14:19:09 -0700781 GrGLenum error = check_alloc_error(desc, this->glInterface());
782 if (error != GR_GL_NO_ERROR) {
783 return false;
784 }
krajcevski145d48c2014-06-11 16:07:50 -0700785 } else {
bsalomond4cb9222014-08-11 14:19:09 -0700786 // Paletted textures can't be updated.
787 if (GR_GL_PALETTE8_RGBA8 == internalFormat) {
788 return false;
789 }
790 GL_CALL(CompressedTexSubImage2D(GR_GL_TEXTURE_2D,
791 0, // level
792 left, top,
793 width, height,
794 internalFormat,
795 dataSize,
796 data));
krajcevski145d48c2014-06-11 16:07:50 -0700797 }
krajcevski9c0e6292014-06-02 07:38:14 -0700798
bsalomond4cb9222014-08-11 14:19:09 -0700799 return true;
krajcevski9c0e6292014-06-02 07:38:14 -0700800}
801
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +0000802static bool renderbuffer_storage_msaa(GrGLContext& ctx,
803 int sampleCount,
804 GrGLenum format,
805 int width, int height) {
robertphillips@google.com6177e692013-02-28 20:16:25 +0000806 CLEAR_ERROR_BEFORE_ALLOC(ctx.interface());
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +0000807 SkASSERT(GrGLCaps::kNone_MSFBOType != ctx.caps()->msFBOType());
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +0000808 switch (ctx.caps()->msFBOType()) {
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +0000809 case GrGLCaps::kDesktop_ARB_MSFBOType:
810 case GrGLCaps::kDesktop_EXT_MSFBOType:
811 case GrGLCaps::kES_3_0_MSFBOType:
812 GL_ALLOC_CALL(ctx.interface(),
813 RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
814 sampleCount,
815 format,
816 width, height));
817 break;
818 case GrGLCaps::kES_Apple_MSFBOType:
819 GL_ALLOC_CALL(ctx.interface(),
820 RenderbufferStorageMultisampleES2APPLE(GR_GL_RENDERBUFFER,
821 sampleCount,
822 format,
823 width, height));
824 break;
825 case GrGLCaps::kES_EXT_MsToTexture_MSFBOType:
826 case GrGLCaps::kES_IMG_MsToTexture_MSFBOType:
827 GL_ALLOC_CALL(ctx.interface(),
828 RenderbufferStorageMultisampleES2EXT(GR_GL_RENDERBUFFER,
829 sampleCount,
830 format,
831 width, height));
832 break;
833 case GrGLCaps::kNone_MSFBOType:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000834 SkFAIL("Shouldn't be here if we don't support multisampled renderbuffers.");
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +0000835 break;
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000836 }
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +0000837 return (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctx.interface()));;
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000838}
839
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000840bool GrGpuGL::createRenderTargetObjects(int width, int height,
841 GrGLuint texID,
842 GrGLRenderTarget::Desc* desc) {
843 desc->fMSColorRenderbufferID = 0;
844 desc->fRTFBOID = 0;
845 desc->fTexFBOID = 0;
bsalomon@google.com72830222013-01-23 20:25:22 +0000846 desc->fIsWrapped = false;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000847
848 GrGLenum status;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000849
bsalomon@google.comab15d612011-08-09 12:57:56 +0000850 GrGLenum msColorFormat = 0; // suppress warning
851
bsalomon@google.com347c3822013-05-01 20:10:01 +0000852 if (desc->fSampleCnt > 0 && GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType()) {
853 goto FAILED;
854 }
855
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000856 GL_CALL(GenFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000857 if (!desc->fTexFBOID) {
858 goto FAILED;
859 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000860
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000861
bsalomon@google.comf3a60c02013-03-19 19:06:09 +0000862 // If we are using multisampling we will create two FBOS. We render to one and then resolve to
863 // the texture bound to the other. The exception is the IMG multisample extension. With this
864 // extension the texture is multisampled when rendered to and then auto-resolves it when it is
865 // rendered from.
bsalomon@google.com347c3822013-05-01 20:10:01 +0000866 if (desc->fSampleCnt > 0 && this->glCaps().usesMSAARenderBuffers()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000867 GL_CALL(GenFramebuffers(1, &desc->fRTFBOID));
868 GL_CALL(GenRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000869 if (!desc->fRTFBOID ||
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000870 !desc->fMSColorRenderbufferID ||
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000871 !this->configToGLFormats(desc->fConfig,
commit-bot@chromium.org87002952013-08-20 15:12:01 +0000872 // ES2 and ES3 require sized internal formats for rb storage.
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +0000873 kGLES_GrGLStandard == this->glStandard(),
bsalomon@google.com347c3822013-05-01 20:10:01 +0000874 &msColorFormat,
875 NULL,
876 NULL)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000877 goto FAILED;
878 }
879 } else {
880 desc->fRTFBOID = desc->fTexFBOID;
881 }
882
bsalomon@google.com0e9b41a2012-01-04 22:11:43 +0000883 // below here we may bind the FBO
bsalomon1c63bf62014-07-22 13:09:46 -0700884 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000885 if (desc->fRTFBOID != desc->fTexFBOID) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000886 SkASSERT(desc->fSampleCnt > 0);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000887 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000888 desc->fMSColorRenderbufferID));
robertphillips@google.com6177e692013-02-28 20:16:25 +0000889 if (!renderbuffer_storage_msaa(fGLContext,
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000890 desc->fSampleCnt,
891 msColorFormat,
892 width, height)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000893 goto FAILED;
894 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000895 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fRTFBOID));
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000896 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000897 GR_GL_COLOR_ATTACHMENT0,
898 GR_GL_RENDERBUFFER,
899 desc->fMSColorRenderbufferID));
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000900 if (desc->fCheckAllocation ||
901 !this->glCaps().isConfigVerifiedColorAttachment(desc->fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000902 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
903 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
904 goto FAILED;
905 }
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +0000906 fGLContext.caps()->markConfigAsValidColorAttachment(desc->fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000907 }
908 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000909 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000910
bsalomon@google.com347c3822013-05-01 20:10:01 +0000911 if (this->glCaps().usesImplicitMSAAResolve() && desc->fSampleCnt > 0) {
bsalomon@google.comf3a60c02013-03-19 19:06:09 +0000912 GL_CALL(FramebufferTexture2DMultisample(GR_GL_FRAMEBUFFER,
913 GR_GL_COLOR_ATTACHMENT0,
914 GR_GL_TEXTURE_2D,
915 texID, 0, desc->fSampleCnt));
916 } else {
917 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
918 GR_GL_COLOR_ATTACHMENT0,
919 GR_GL_TEXTURE_2D,
920 texID, 0));
921 }
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000922 if (desc->fCheckAllocation ||
923 !this->glCaps().isConfigVerifiedColorAttachment(desc->fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000924 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
925 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
926 goto FAILED;
927 }
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +0000928 fGLContext.caps()->markConfigAsValidColorAttachment(desc->fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000929 }
930
931 return true;
932
933FAILED:
934 if (desc->fMSColorRenderbufferID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000935 GL_CALL(DeleteRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000936 }
937 if (desc->fRTFBOID != desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000938 GL_CALL(DeleteFramebuffers(1, &desc->fRTFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000939 }
940 if (desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000941 GL_CALL(DeleteFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000942 }
943 return false;
944}
945
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000946// good to set a break-point here to know when createTexture fails
947static GrTexture* return_null_texture() {
mtklein@google.com330313a2013-08-22 15:37:26 +0000948// SkDEBUGFAIL("null texture");
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000949 return NULL;
950}
951
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000952#if 0 && defined(SK_DEBUG)
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000953static size_t as_size_t(int x) {
954 return x;
955}
956#endif
957
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000958GrTexture* GrGpuGL::onCreateTexture(const GrTextureDesc& desc,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000959 const void* srcData,
960 size_t rowBytes) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000961
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000962 GrGLTexture::Desc glTexDesc;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000963 GrGLRenderTarget::Desc glRTDesc;
reed@google.comac10a2d2010-12-22 21:39:39 +0000964
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000965 // Attempt to catch un- or wrongly initialized sample counts;
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000966 SkASSERT(desc.fSampleCnt >= 0 && desc.fSampleCnt <= 64);
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000967 // We fail if the MSAA was requested and is not available.
968 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType() && desc.fSampleCnt) {
969 //GrPrintf("MSAA RT requested but not supported on this platform.");
970 return return_null_texture();
971 }
972 // If the sample count exceeds the max then we clamp it.
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000973 glTexDesc.fSampleCnt = SkTMin(desc.fSampleCnt, this->caps()->maxSampleCount());
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000974
robertphillips@google.com32716282012-06-04 12:48:45 +0000975 glTexDesc.fFlags = desc.fFlags;
bsalomon@google.com99621082011-11-15 16:47:16 +0000976 glTexDesc.fWidth = desc.fWidth;
977 glTexDesc.fHeight = desc.fHeight;
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000978 glTexDesc.fConfig = desc.fConfig;
bsalomon@google.com72830222013-01-23 20:25:22 +0000979 glTexDesc.fIsWrapped = false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000980
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000981 glRTDesc.fMSColorRenderbufferID = 0;
982 glRTDesc.fRTFBOID = 0;
983 glRTDesc.fTexFBOID = 0;
bsalomon@google.com72830222013-01-23 20:25:22 +0000984 glRTDesc.fIsWrapped = false;
bsalomon@google.com64c4fe42011-11-05 14:51:01 +0000985 glRTDesc.fConfig = glTexDesc.fConfig;
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000986 glRTDesc.fCheckAllocation = SkToBool(desc.fFlags & kCheckAllocation_GrTextureFlagBit);
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000987
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000988 bool renderTarget = SkToBool(desc.fFlags & kRenderTarget_GrTextureFlagBit);
reed@google.comac10a2d2010-12-22 21:39:39 +0000989
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000990 glTexDesc.fOrigin = resolve_origin(desc.fOrigin, renderTarget);
991 glRTDesc.fOrigin = glTexDesc.fOrigin;
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000992
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000993 glRTDesc.fSampleCnt = glTexDesc.fSampleCnt;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000994 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType() &&
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000995 desc.fSampleCnt) {
bsalomon@google.com945bbe12012-06-15 14:30:34 +0000996 //GrPrintf("MSAA RT requested but not supported on this platform.");
997 return return_null_texture();
reed@google.comac10a2d2010-12-22 21:39:39 +0000998 }
999
reed@google.comac10a2d2010-12-22 21:39:39 +00001000 if (renderTarget) {
bsalomon@google.combcce8922013-03-25 15:38:39 +00001001 int maxRTSize = this->caps()->maxRenderTargetSize();
1002 if (glTexDesc.fWidth > maxRTSize || glTexDesc.fHeight > maxRTSize) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001003 return return_null_texture();
1004 }
sugoi@google.com6ba0b0f2013-05-03 13:52:32 +00001005 } else {
1006 int maxSize = this->caps()->maxTextureSize();
1007 if (glTexDesc.fWidth > maxSize || glTexDesc.fHeight > maxSize) {
1008 return return_null_texture();
1009 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001010 }
1011
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001012 GL_CALL(GenTextures(1, &glTexDesc.fTextureID));
junov@chromium.org8b6b1c92013-08-29 16:22:09 +00001013
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001014 if (!glTexDesc.fTextureID) {
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001015 return return_null_texture();
1016 }
1017
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00001018 this->setScratchTextureUnit();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001019 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTexDesc.fTextureID));
bsalomon@google.come269f212011-11-07 13:29:52 +00001020
junov@chromium.org8b6b1c92013-08-29 16:22:09 +00001021 if (renderTarget && this->glCaps().textureUsageSupport()) {
1022 // provides a hint about how this texture will be used
1023 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1024 GR_GL_TEXTURE_USAGE,
1025 GR_GL_FRAMEBUFFER_ATTACHMENT));
1026 }
1027
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001028 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
1029 // drivers have a bug where an FBO won't be complete if it includes a
1030 // texture that is not mipmap complete (considering the filter in use).
1031 GrGLTexture::TexParams initialTexParams;
1032 // we only set a subset here so invalidate first
1033 initialTexParams.invalidate();
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00001034 initialTexParams.fMinFilter = GR_GL_NEAREST;
1035 initialTexParams.fMagFilter = GR_GL_NEAREST;
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001036 initialTexParams.fWrapS = GR_GL_CLAMP_TO_EDGE;
1037 initialTexParams.fWrapT = GR_GL_CLAMP_TO_EDGE;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001038 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1039 GR_GL_TEXTURE_MAG_FILTER,
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00001040 initialTexParams.fMagFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001041 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1042 GR_GL_TEXTURE_MIN_FILTER,
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00001043 initialTexParams.fMinFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001044 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1045 GR_GL_TEXTURE_WRAP_S,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001046 initialTexParams.fWrapS));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001047 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1048 GR_GL_TEXTURE_WRAP_T,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001049 initialTexParams.fWrapT));
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001050 if (!this->uploadTexData(glTexDesc, true, 0, 0,
1051 glTexDesc.fWidth, glTexDesc.fHeight,
1052 desc.fConfig, srcData, rowBytes)) {
1053 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
1054 return return_null_texture();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001055 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001056
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001057 GrGLTexture* tex;
reed@google.comac10a2d2010-12-22 21:39:39 +00001058 if (renderTarget) {
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001059 // unbind the texture from the texture unit before binding it to the frame buffer
1060 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, 0));
1061
bsalomon@google.com99621082011-11-15 16:47:16 +00001062 if (!this->createRenderTargetObjects(glTexDesc.fWidth,
1063 glTexDesc.fHeight,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001064 glTexDesc.fTextureID,
1065 &glRTDesc)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001066 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001067 return return_null_texture();
1068 }
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001069 tex = SkNEW_ARGS(GrGLTexture, (this, glTexDesc, glRTDesc));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001070 } else {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001071 tex = SkNEW_ARGS(GrGLTexture, (this, glTexDesc));
reed@google.comac10a2d2010-12-22 21:39:39 +00001072 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001073 tex->setCachedTexParams(initialTexParams, this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00001074#ifdef TRACE_TEXTURE_CREATION
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001075 GrPrintf("--- new texture [%d] size=(%d %d) config=%d\n",
1076 glTexDesc.fTextureID, desc.fWidth, desc.fHeight, desc.fConfig);
reed@google.comac10a2d2010-12-22 21:39:39 +00001077#endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001078 return tex;
1079}
1080
krajcevski9c0e6292014-06-02 07:38:14 -07001081GrTexture* GrGpuGL::onCreateCompressedTexture(const GrTextureDesc& desc,
1082 const void* srcData) {
1083
1084 if(SkToBool(desc.fFlags & kRenderTarget_GrTextureFlagBit)) {
1085 return return_null_texture();
1086 }
1087
1088 // Make sure that we're not flipping Y.
1089 GrSurfaceOrigin texOrigin = resolve_origin(desc.fOrigin, false);
1090 if (kBottomLeft_GrSurfaceOrigin == texOrigin) {
1091 return return_null_texture();
1092 }
1093
1094 GrGLTexture::Desc glTexDesc;
1095
1096 glTexDesc.fFlags = desc.fFlags;
1097 glTexDesc.fWidth = desc.fWidth;
1098 glTexDesc.fHeight = desc.fHeight;
1099 glTexDesc.fConfig = desc.fConfig;
1100 glTexDesc.fIsWrapped = false;
1101 glTexDesc.fOrigin = texOrigin;
1102
1103 int maxSize = this->caps()->maxTextureSize();
1104 if (glTexDesc.fWidth > maxSize || glTexDesc.fHeight > maxSize) {
1105 return return_null_texture();
1106 }
1107
1108 GL_CALL(GenTextures(1, &glTexDesc.fTextureID));
1109
1110 if (!glTexDesc.fTextureID) {
1111 return return_null_texture();
1112 }
1113
1114 this->setScratchTextureUnit();
1115 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTexDesc.fTextureID));
1116
1117 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
1118 // drivers have a bug where an FBO won't be complete if it includes a
1119 // texture that is not mipmap complete (considering the filter in use).
1120 GrGLTexture::TexParams initialTexParams;
1121 // we only set a subset here so invalidate first
1122 initialTexParams.invalidate();
1123 initialTexParams.fMinFilter = GR_GL_NEAREST;
1124 initialTexParams.fMagFilter = GR_GL_NEAREST;
1125 initialTexParams.fWrapS = GR_GL_CLAMP_TO_EDGE;
1126 initialTexParams.fWrapT = GR_GL_CLAMP_TO_EDGE;
1127 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1128 GR_GL_TEXTURE_MAG_FILTER,
1129 initialTexParams.fMagFilter));
1130 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1131 GR_GL_TEXTURE_MIN_FILTER,
1132 initialTexParams.fMinFilter));
1133 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1134 GR_GL_TEXTURE_WRAP_S,
1135 initialTexParams.fWrapS));
1136 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1137 GR_GL_TEXTURE_WRAP_T,
1138 initialTexParams.fWrapT));
1139
1140 if (!this->uploadCompressedTexData(glTexDesc, srcData)) {
1141 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
1142 return return_null_texture();
1143 }
1144
1145 GrGLTexture* tex;
1146 tex = SkNEW_ARGS(GrGLTexture, (this, glTexDesc));
1147 tex->setCachedTexParams(initialTexParams, this->getResetTimestamp());
1148#ifdef TRACE_TEXTURE_CREATION
1149 GrPrintf("--- new compressed texture [%d] size=(%d %d) config=%d\n",
1150 glTexDesc.fTextureID, desc.fWidth, desc.fHeight, desc.fConfig);
1151#endif
1152 return tex;
1153}
1154
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001155namespace {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001156
1157const GrGLuint kUnknownBitCount = GrGLStencilBuffer::kUnknownBitCount;
1158
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001159void inline get_stencil_rb_sizes(const GrGLInterface* gl,
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001160 GrGLStencilBuffer::Format* format) {
sugoi@google.com6ba0b0f2013-05-03 13:52:32 +00001161
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001162 // we shouldn't ever know one size and not the other
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001163 SkASSERT((kUnknownBitCount == format->fStencilBits) ==
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001164 (kUnknownBitCount == format->fTotalBits));
1165 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001166 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001167 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1168 (GrGLint*)&format->fStencilBits);
1169 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001170 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001171 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1172 (GrGLint*)&format->fTotalBits);
1173 format->fTotalBits += format->fStencilBits;
1174 } else {
1175 format->fTotalBits = format->fStencilBits;
1176 }
1177 }
1178}
1179}
1180
1181bool GrGpuGL::createStencilBufferForRenderTarget(GrRenderTarget* rt,
1182 int width, int height) {
1183
1184 // All internally created RTs are also textures. We don't create
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +00001185 // 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 +00001186 SkASSERT(rt->asTexture());
1187 SkASSERT(width >= rt->width());
1188 SkASSERT(height >= rt->height());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001189
1190 int samples = rt->numSamples();
1191 GrGLuint sbID;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001192 GL_CALL(GenRenderbuffers(1, &sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001193 if (!sbID) {
1194 return false;
1195 }
1196
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001197 int stencilFmtCnt = this->glCaps().stencilFormats().count();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001198 for (int i = 0; i < stencilFmtCnt; ++i) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001199 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001200 // we start with the last stencil format that succeeded in hopes
1201 // that we won't go through this loop more than once after the
1202 // first (painful) stencil creation.
1203 int sIdx = (i + fLastSuccessfulStencilFmtIdx) % stencilFmtCnt;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001204 const GrGLCaps::StencilFormat& sFmt =
1205 this->glCaps().stencilFormats()[sIdx];
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001206 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001207 // we do this "if" so that we don't call the multisample
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001208 // version on a GL that doesn't have an MSAA extension.
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001209 bool created;
1210 if (samples > 0) {
robertphillips@google.com6177e692013-02-28 20:16:25 +00001211 created = renderbuffer_storage_msaa(fGLContext,
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001212 samples,
1213 sFmt.fInternalFormat,
1214 width, height);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001215 } else {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001216 GL_ALLOC_CALL(this->glInterface(),
1217 RenderbufferStorage(GR_GL_RENDERBUFFER,
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001218 sFmt.fInternalFormat,
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001219 width, height));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001220 created =
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +00001221 (GR_GL_NO_ERROR == check_alloc_error(rt->desc(), this->glInterface()));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001222 }
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001223 if (created) {
1224 // After sized formats we attempt an unsized format and take
1225 // whatever sizes GL gives us. In that case we query for the size.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001226 GrGLStencilBuffer::Format format = sFmt;
sugoi@google.com6ba0b0f2013-05-03 13:52:32 +00001227 get_stencil_rb_sizes(this->glInterface(), &format);
bsalomon@google.com72830222013-01-23 20:25:22 +00001228 static const bool kIsWrapped = false;
robertphillips@google.comf2e93fc2012-09-05 19:44:18 +00001229 SkAutoTUnref<GrStencilBuffer> sb(SkNEW_ARGS(GrGLStencilBuffer,
bsalomon@google.com72830222013-01-23 20:25:22 +00001230 (this, kIsWrapped, sbID, width, height,
robertphillips@google.comf2e93fc2012-09-05 19:44:18 +00001231 samples, format)));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001232 if (this->attachStencilBufferToRenderTarget(sb, rt)) {
1233 fLastSuccessfulStencilFmtIdx = sIdx;
robertphillips@google.com9fbcad02012-09-09 14:44:15 +00001234 sb->transferToCache();
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001235 rt->setStencilBuffer(sb);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001236 return true;
1237 }
1238 sb->abandon(); // otherwise we lose sbID
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001239 }
1240 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001241 GL_CALL(DeleteRenderbuffers(1, &sbID));
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001242 return false;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001243}
1244
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001245bool GrGpuGL::attachStencilBufferToRenderTarget(GrStencilBuffer* sb, GrRenderTarget* rt) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001246 GrGLRenderTarget* glrt = (GrGLRenderTarget*) rt;
1247
1248 GrGLuint fbo = glrt->renderFBOID();
1249
1250 if (NULL == sb) {
1251 if (NULL != rt->getStencilBuffer()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001252 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001253 GR_GL_STENCIL_ATTACHMENT,
1254 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001255 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001256 GR_GL_DEPTH_ATTACHMENT,
1257 GR_GL_RENDERBUFFER, 0));
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +00001258#ifdef SK_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001259 GrGLenum status;
1260 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001261 SkASSERT(GR_GL_FRAMEBUFFER_COMPLETE == status);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001262#endif
1263 }
1264 return true;
1265 } else {
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001266 GrGLStencilBuffer* glsb = static_cast<GrGLStencilBuffer*>(sb);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001267 GrGLuint rb = glsb->renderbufferID();
1268
bsalomon1c63bf62014-07-22 13:09:46 -07001269 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001270 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, fbo));
1271 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001272 GR_GL_STENCIL_ATTACHMENT,
1273 GR_GL_RENDERBUFFER, rb));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001274 if (glsb->format().fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001275 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001276 GR_GL_DEPTH_ATTACHMENT,
1277 GR_GL_RENDERBUFFER, rb));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001278 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001279 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001280 GR_GL_DEPTH_ATTACHMENT,
1281 GR_GL_RENDERBUFFER, 0));
reed@google.comac10a2d2010-12-22 21:39:39 +00001282 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001283
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001284 GrGLenum status;
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001285 if (!this->glCaps().isColorConfigAndStencilFormatVerified(rt->config(), glsb->format())) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001286 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1287 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001288 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001289 GR_GL_STENCIL_ATTACHMENT,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001290 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001291 if (glsb->format().fPacked) {
1292 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1293 GR_GL_DEPTH_ATTACHMENT,
1294 GR_GL_RENDERBUFFER, 0));
1295 }
1296 return false;
1297 } else {
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +00001298 fGLContext.caps()->markColorConfigAndStencilFormatAsVerified(
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001299 rt->config(),
1300 glsb->format());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001301 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001302 }
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001303 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001304 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001305}
1306
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001307////////////////////////////////////////////////////////////////////////////////
1308
robertphillips@google.comadacc702013-10-14 21:53:24 +00001309GrVertexBuffer* GrGpuGL::onCreateVertexBuffer(size_t size, bool dynamic) {
bsalomon@google.come49ad452013-02-20 19:33:20 +00001310 GrGLVertexBuffer::Desc desc;
1311 desc.fDynamic = dynamic;
1312 desc.fSizeInBytes = size;
1313 desc.fIsWrapped = false;
1314
bsalomon@google.com96966a52013-02-21 16:34:21 +00001315 if (this->glCaps().useNonVBOVertexAndIndexDynamicData() && desc.fDynamic) {
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001316 desc.fID = 0;
bsalomon@google.come49ad452013-02-20 19:33:20 +00001317 GrGLVertexBuffer* vertexBuffer = SkNEW_ARGS(GrGLVertexBuffer, (this, desc));
reed@google.comac10a2d2010-12-22 21:39:39 +00001318 return vertexBuffer;
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001319 } else {
1320 GL_CALL(GenBuffers(1, &desc.fID));
1321 if (desc.fID) {
bsalomon@google.com6918d482013-03-07 19:09:11 +00001322 fHWGeometryState.setVertexBufferID(this, desc.fID);
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001323 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1324 // make sure driver can allocate memory for this buffer
1325 GL_ALLOC_CALL(this->glInterface(),
1326 BufferData(GR_GL_ARRAY_BUFFER,
robertphillips@google.come9cd27d2013-10-16 17:48:11 +00001327 (GrGLsizeiptr) desc.fSizeInBytes,
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001328 NULL, // data ptr
1329 desc.fDynamic ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW));
1330 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
1331 GL_CALL(DeleteBuffers(1, &desc.fID));
bsalomon@google.com6918d482013-03-07 19:09:11 +00001332 this->notifyVertexBufferDelete(desc.fID);
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001333 return NULL;
1334 }
1335 GrGLVertexBuffer* vertexBuffer = SkNEW_ARGS(GrGLVertexBuffer, (this, desc));
1336 return vertexBuffer;
1337 }
1338 return NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +00001339 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001340}
1341
robertphillips@google.comadacc702013-10-14 21:53:24 +00001342GrIndexBuffer* GrGpuGL::onCreateIndexBuffer(size_t size, bool dynamic) {
bsalomon@google.come49ad452013-02-20 19:33:20 +00001343 GrGLIndexBuffer::Desc desc;
1344 desc.fDynamic = dynamic;
1345 desc.fSizeInBytes = size;
1346 desc.fIsWrapped = false;
1347
bsalomon@google.com96966a52013-02-21 16:34:21 +00001348 if (this->glCaps().useNonVBOVertexAndIndexDynamicData() && desc.fDynamic) {
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001349 desc.fID = 0;
bsalomon@google.come49ad452013-02-20 19:33:20 +00001350 GrIndexBuffer* indexBuffer = SkNEW_ARGS(GrGLIndexBuffer, (this, desc));
reed@google.comac10a2d2010-12-22 21:39:39 +00001351 return indexBuffer;
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001352 } else {
1353 GL_CALL(GenBuffers(1, &desc.fID));
1354 if (desc.fID) {
bsalomon@google.com6918d482013-03-07 19:09:11 +00001355 fHWGeometryState.setIndexBufferIDOnDefaultVertexArray(this, desc.fID);
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001356 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1357 // make sure driver can allocate memory for this buffer
1358 GL_ALLOC_CALL(this->glInterface(),
1359 BufferData(GR_GL_ELEMENT_ARRAY_BUFFER,
robertphillips@google.come9cd27d2013-10-16 17:48:11 +00001360 (GrGLsizeiptr) desc.fSizeInBytes,
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001361 NULL, // data ptr
1362 desc.fDynamic ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW));
1363 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
1364 GL_CALL(DeleteBuffers(1, &desc.fID));
bsalomon@google.com6918d482013-03-07 19:09:11 +00001365 this->notifyIndexBufferDelete(desc.fID);
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001366 return NULL;
1367 }
1368 GrIndexBuffer* indexBuffer = SkNEW_ARGS(GrGLIndexBuffer, (this, desc));
1369 return indexBuffer;
1370 }
1371 return NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +00001372 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001373}
1374
commit-bot@chromium.org32184d82013-10-09 15:14:18 +00001375GrPath* GrGpuGL::onCreatePath(const SkPath& inPath, const SkStrokeRec& stroke) {
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001376 SkASSERT(this->caps()->pathRenderingSupport());
commit-bot@chromium.org32184d82013-10-09 15:14:18 +00001377 return SkNEW_ARGS(GrGLPath, (this, inPath, stroke));
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00001378}
1379
cdaltonb85a0aa2014-07-21 15:32:44 -07001380GrPathRange* GrGpuGL::onCreatePathRange(size_t size, const SkStrokeRec& stroke) {
1381 SkASSERT(this->caps()->pathRenderingSupport());
1382 return SkNEW_ARGS(GrGLPathRange, (this, size, stroke));
1383}
1384
bsalomon@google.coma3201942012-06-21 19:58:20 +00001385void GrGpuGL::flushScissor() {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001386 if (fScissorState.fEnabled) {
commit-bot@chromium.org5c363642013-10-04 15:28:18 +00001387 // Only access the RT if scissoring is being enabled. We can call this before performing
1388 // a glBitframebuffer for a surface->surface copy, which requires no RT to be bound to the
1389 // GrDrawState.
1390 const GrDrawState& drawState = this->getDrawState();
1391 const GrGLRenderTarget* rt =
1392 static_cast<const GrGLRenderTarget*>(drawState.getRenderTarget());
1393
1394 SkASSERT(NULL != rt);
1395 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.coma3201942012-06-21 19:58:20 +00001396 GrGLIRect scissor;
1397 scissor.setRelativeTo(vp,
1398 fScissorState.fRect.fLeft,
1399 fScissorState.fRect.fTop,
1400 fScissorState.fRect.width(),
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001401 fScissorState.fRect.height(),
1402 rt->origin());
bsalomon@google.coma3201942012-06-21 19:58:20 +00001403 // if the scissor fully contains the viewport then we fall through and
1404 // disable the scissor test.
1405 if (!scissor.contains(vp)) {
1406 if (fHWScissorSettings.fRect != scissor) {
1407 scissor.pushToGLScissor(this->glInterface());
1408 fHWScissorSettings.fRect = scissor;
1409 }
1410 if (kYes_TriState != fHWScissorSettings.fEnabled) {
1411 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1412 fHWScissorSettings.fEnabled = kYes_TriState;
1413 }
1414 return;
1415 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001416 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001417 if (kNo_TriState != fHWScissorSettings.fEnabled) {
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001418 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.coma3201942012-06-21 19:58:20 +00001419 fHWScissorSettings.fEnabled = kNo_TriState;
1420 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001421 }
1422}
1423
robertphillips@google.com56ce48a2013-10-31 21:44:25 +00001424void GrGpuGL::onClear(const SkIRect* rect, GrColor color, bool canIgnoreRect) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001425 const GrDrawState& drawState = this->getDrawState();
1426 const GrRenderTarget* rt = drawState.getRenderTarget();
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001427 // parent class should never let us get here with no RT
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001428 SkASSERT(NULL != rt);
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001429
robertphillips@google.com56ce48a2013-10-31 21:44:25 +00001430 if (canIgnoreRect && this->glCaps().fullClearIsFree()) {
1431 rect = NULL;
1432 }
1433
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001434 SkIRect clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001435 if (NULL != rect) {
1436 // flushScissor expects rect to be clipped to the target.
bsalomon@google.com74b98712011-11-11 19:46:16 +00001437 clippedRect = *rect;
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001438 SkIRect rtRect = SkIRect::MakeWH(rt->width(), rt->height());
bsalomon@google.com74b98712011-11-11 19:46:16 +00001439 if (clippedRect.intersect(rtRect)) {
1440 rect = &clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001441 } else {
1442 return;
1443 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001444 }
rmistry@google.comd6bab022013-12-02 13:50:38 +00001445
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001446 this->flushRenderTarget(rect);
bsalomon@google.coma3201942012-06-21 19:58:20 +00001447 GrAutoTRestore<ScissorState> asr(&fScissorState);
1448 fScissorState.fEnabled = (NULL != rect);
1449 if (fScissorState.fEnabled) {
1450 fScissorState.fRect = *rect;
1451 }
1452 this->flushScissor();
bsalomon@google.com74b98712011-11-11 19:46:16 +00001453
1454 GrGLfloat r, g, b, a;
1455 static const GrGLfloat scale255 = 1.f / 255.f;
1456 a = GrColorUnpackA(color) * scale255;
1457 GrGLfloat scaleRGB = scale255;
bsalomon@google.com74b98712011-11-11 19:46:16 +00001458 r = GrColorUnpackR(color) * scaleRGB;
1459 g = GrColorUnpackG(color) * scaleRGB;
1460 b = GrColorUnpackB(color) * scaleRGB;
1461
1462 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00001463 fHWWriteToColor = kYes_TriState;
bsalomon@google.com74b98712011-11-11 19:46:16 +00001464 GL_CALL(ClearColor(r, g, b, a));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001465 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001466}
1467
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +00001468void GrGpuGL::discard(GrRenderTarget* renderTarget) {
commit-bot@chromium.org52ffbf62014-04-02 16:19:33 +00001469 if (!this->caps()->discardRenderTargetSupport()) {
1470 return;
1471 }
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +00001472 if (NULL == renderTarget) {
1473 renderTarget = this->drawState()->getRenderTarget();
1474 if (NULL == renderTarget) {
1475 return;
1476 }
1477 }
1478
1479 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(renderTarget);
bsalomon1c63bf62014-07-22 13:09:46 -07001480 if (renderTarget->getUniqueID() != fHWBoundRenderTargetUniqueID) {
1481 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +00001482 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, glRT->renderFBOID()));
1483 }
commit-bot@chromium.org52ffbf62014-04-02 16:19:33 +00001484 switch (this->glCaps().invalidateFBType()) {
joshualitt58162332014-08-01 06:44:53 -07001485 case GrGLCaps::kNone_InvalidateFBType:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +00001486 SkFAIL("Should never get here.");
commit-bot@chromium.org52ffbf62014-04-02 16:19:33 +00001487 break;
1488 case GrGLCaps::kInvalidate_InvalidateFBType:
1489 if (0 == glRT->renderFBOID()) {
1490 // When rendering to the default framebuffer the legal values for attachments
1491 // are GL_COLOR, GL_DEPTH, GL_STENCIL, ... rather than the various FBO attachment
1492 // types.
1493 static const GrGLenum attachments[] = { GR_GL_COLOR };
1494 GL_CALL(InvalidateFramebuffer(GR_GL_FRAMEBUFFER, SK_ARRAY_COUNT(attachments),
1495 attachments));
1496 } else {
1497 static const GrGLenum attachments[] = { GR_GL_COLOR_ATTACHMENT0 };
1498 GL_CALL(InvalidateFramebuffer(GR_GL_FRAMEBUFFER, SK_ARRAY_COUNT(attachments),
1499 attachments));
1500 }
1501 break;
1502 case GrGLCaps::kDiscard_InvalidateFBType: {
commit-bot@chromium.org4453e8b2014-04-16 14:33:27 +00001503 if (0 == glRT->renderFBOID()) {
1504 // When rendering to the default framebuffer the legal values for attachments
1505 // are GL_COLOR, GL_DEPTH, GL_STENCIL, ... rather than the various FBO attachment
1506 // types. See glDiscardFramebuffer() spec.
1507 static const GrGLenum attachments[] = { GR_GL_COLOR };
1508 GL_CALL(DiscardFramebuffer(GR_GL_FRAMEBUFFER, SK_ARRAY_COUNT(attachments),
1509 attachments));
1510 } else {
1511 static const GrGLenum attachments[] = { GR_GL_COLOR_ATTACHMENT0 };
1512 GL_CALL(DiscardFramebuffer(GR_GL_FRAMEBUFFER, SK_ARRAY_COUNT(attachments),
1513 attachments));
1514 }
commit-bot@chromium.org52ffbf62014-04-02 16:19:33 +00001515 break;
1516 }
1517 }
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +00001518 renderTarget->flagAsResolved();
1519}
1520
1521
bsalomon@google.comedc177d2011-08-05 15:46:40 +00001522void GrGpuGL::clearStencil() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001523 if (NULL == this->getDrawState().getRenderTarget()) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001524 return;
1525 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001526
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001527 this->flushRenderTarget(&SkIRect::EmptyIRect());
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001528
bsalomon@google.coma3201942012-06-21 19:58:20 +00001529 GrAutoTRestore<ScissorState> asr(&fScissorState);
1530 fScissorState.fEnabled = false;
1531 this->flushScissor();
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001532
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001533 GL_CALL(StencilMask(0xffffffff));
1534 GL_CALL(ClearStencil(0));
1535 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001536 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001537}
1538
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001539void GrGpuGL::clearStencilClip(const SkIRect& rect, bool insideClip) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001540 const GrDrawState& drawState = this->getDrawState();
1541 const GrRenderTarget* rt = drawState.getRenderTarget();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001542 SkASSERT(NULL != rt);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001543
1544 // this should only be called internally when we know we have a
1545 // stencil buffer.
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001546 SkASSERT(NULL != rt->getStencilBuffer());
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001547 GrGLint stencilBitCount = rt->getStencilBuffer()->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001548#if 0
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001549 SkASSERT(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00001550 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001551#else
1552 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001553 // ANGLE a partial stencil mask will cause clears to be
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001554 // turned into draws. Our contract on GrDrawTarget says that
1555 // changing the clip between stencil passes may or may not
1556 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00001557 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001558#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001559 GrGLint value;
1560 if (insideClip) {
1561 value = (1 << (stencilBitCount - 1));
1562 } else {
1563 value = 0;
1564 }
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001565 this->flushRenderTarget(&SkIRect::EmptyIRect());
bsalomon@google.coma3201942012-06-21 19:58:20 +00001566
1567 GrAutoTRestore<ScissorState> asr(&fScissorState);
1568 fScissorState.fEnabled = true;
1569 fScissorState.fRect = rect;
1570 this->flushScissor();
1571
caryclark@google.comcf6285b2012-06-06 12:09:01 +00001572 GL_CALL(StencilMask((uint32_t) clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001573 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001574 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001575 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001576}
1577
bsalomon@google.comc4364992011-11-07 15:54:49 +00001578bool GrGpuGL::readPixelsWillPayForYFlip(GrRenderTarget* renderTarget,
1579 int left, int top,
1580 int width, int height,
1581 GrPixelConfig config,
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001582 size_t rowBytes) const {
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001583 // If this rendertarget is aready TopLeft, we don't need to flip.
1584 if (kTopLeft_GrSurfaceOrigin == renderTarget->origin()) {
1585 return false;
1586 }
1587
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001588 // if GL can do the flip then we'll never pay for it.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001589 if (this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001590 return false;
1591 }
1592
1593 // If we have to do memcpy to handle non-trim rowBytes then we
bsalomon@google.com7107fa72011-11-10 14:54:14 +00001594 // get the flip for free. Otherwise it costs.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001595 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001596 return true;
1597 }
1598 // If we have to do memcpys to handle rowBytes then y-flip is free
1599 // Note the rowBytes might be tight to the passed in data, but if data
1600 // gets clipped in x to the target the rowBytes will no longer be tight.
1601 if (left >= 0 && (left + width) < renderTarget->width()) {
1602 return 0 == rowBytes ||
1603 GrBytesPerPixel(config) * width == rowBytes;
1604 } else {
1605 return false;
1606 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001607}
1608
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001609bool GrGpuGL::onReadPixels(GrRenderTarget* target,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001610 int left, int top,
1611 int width, int height,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001612 GrPixelConfig config,
1613 void* buffer,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001614 size_t rowBytes) {
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +00001615 // We cannot read pixels into a compressed buffer
1616 if (GrPixelConfigIsCompressed(config)) {
1617 return false;
1618 }
1619
1620 GrGLenum format = 0;
1621 GrGLenum type = 0;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001622 bool flipY = kBottomLeft_GrSurfaceOrigin == target->origin();
bsalomon@google.com280e99f2012-01-05 16:17:38 +00001623 if (!this->configToGLFormats(config, false, NULL, &format, &type)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001624 return false;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001625 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001626 size_t bpp = GrBytesPerPixel(config);
1627 if (!adjust_pixel_ops_params(target->width(), target->height(), bpp,
1628 &left, &top, &width, &height,
1629 const_cast<const void**>(&buffer),
1630 &rowBytes)) {
1631 return false;
1632 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001633
bsalomon@google.comc6980972011-11-02 19:57:21 +00001634 // resolve the render target if necessary
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001635 GrGLRenderTarget* tgt = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001636 GrDrawState::AutoRenderTargetRestore artr;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001637 switch (tgt->getResolveType()) {
1638 case GrGLRenderTarget::kCantResolve_ResolveType:
1639 return false;
1640 case GrGLRenderTarget::kAutoResolves_ResolveType:
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001641 artr.set(this->drawState(), target);
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001642 this->flushRenderTarget(&SkIRect::EmptyIRect());
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001643 break;
1644 case GrGLRenderTarget::kCanResolve_ResolveType:
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001645 this->onResolveRenderTarget(tgt);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001646 // we don't track the state of the READ FBO ID.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001647 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1648 tgt->textureFBOID()));
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001649 break;
1650 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +00001651 SkFAIL("Unknown resolve type");
reed@google.comac10a2d2010-12-22 21:39:39 +00001652 }
1653
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001654 const GrGLIRect& glvp = tgt->getViewport();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001655
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001656 // the read rect is viewport-relative
1657 GrGLIRect readRect;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001658 readRect.setRelativeTo(glvp, left, top, width, height, target->origin());
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001659
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001660 size_t tightRowBytes = bpp * width;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001661 if (0 == rowBytes) {
1662 rowBytes = tightRowBytes;
1663 }
1664 size_t readDstRowBytes = tightRowBytes;
1665 void* readDst = buffer;
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001666
bsalomon@google.comc6980972011-11-02 19:57:21 +00001667 // determine if GL can read using the passed rowBytes or if we need
1668 // a scratch buffer.
georgeb62508b2014-08-12 18:00:47 -07001669 GrAutoMalloc<32 * sizeof(GrColor)> scratch;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001670 if (rowBytes != tightRowBytes) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001671 if (this->glCaps().packRowLengthSupport()) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001672 SkASSERT(!(rowBytes % sizeof(GrColor)));
skia.committer@gmail.com4677acc2013-10-17 07:02:33 +00001673 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH,
robertphillips@google.come9cd27d2013-10-16 17:48:11 +00001674 static_cast<GrGLint>(rowBytes / sizeof(GrColor))));
bsalomon@google.comc6980972011-11-02 19:57:21 +00001675 readDstRowBytes = rowBytes;
1676 } else {
1677 scratch.reset(tightRowBytes * height);
1678 readDst = scratch.get();
1679 }
1680 }
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001681 if (flipY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001682 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 1));
1683 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001684 GL_CALL(ReadPixels(readRect.fLeft, readRect.fBottom,
1685 readRect.fWidth, readRect.fHeight,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001686 format, type, readDst));
1687 if (readDstRowBytes != tightRowBytes) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001688 SkASSERT(this->glCaps().packRowLengthSupport());
bsalomon@google.comc6980972011-11-02 19:57:21 +00001689 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
1690 }
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001691 if (flipY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001692 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 0));
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001693 flipY = false;
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001694 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001695
1696 // now reverse the order of the rows, since GL's are bottom-to-top, but our
bsalomon@google.comc6980972011-11-02 19:57:21 +00001697 // API presents top-to-bottom. We must preserve the padding contents. Note
1698 // that the above readPixels did not overwrite the padding.
1699 if (readDst == buffer) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001700 SkASSERT(rowBytes == readDstRowBytes);
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001701 if (flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001702 scratch.reset(tightRowBytes);
1703 void* tmpRow = scratch.get();
1704 // flip y in-place by rows
1705 const int halfY = height >> 1;
1706 char* top = reinterpret_cast<char*>(buffer);
1707 char* bottom = top + (height - 1) * rowBytes;
1708 for (int y = 0; y < halfY; y++) {
1709 memcpy(tmpRow, top, tightRowBytes);
1710 memcpy(top, bottom, tightRowBytes);
1711 memcpy(bottom, tmpRow, tightRowBytes);
1712 top += rowBytes;
1713 bottom -= rowBytes;
1714 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001715 }
1716 } else {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001717 SkASSERT(readDst != buffer); SkASSERT(rowBytes != tightRowBytes);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001718 // copy from readDst to buffer while flipping y
caryclark@google.comcf6285b2012-06-06 12:09:01 +00001719 // const int halfY = height >> 1;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001720 const char* src = reinterpret_cast<const char*>(readDst);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001721 char* dst = reinterpret_cast<char*>(buffer);
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001722 if (flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001723 dst += (height-1) * rowBytes;
1724 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001725 for (int y = 0; y < height; y++) {
1726 memcpy(dst, src, tightRowBytes);
1727 src += readDstRowBytes;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001728 if (!flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001729 dst += rowBytes;
1730 } else {
1731 dst -= rowBytes;
1732 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001733 }
1734 }
1735 return true;
1736}
1737
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001738void GrGpuGL::flushRenderTarget(const SkIRect* bound) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001739
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001740 GrGLRenderTarget* rt =
1741 static_cast<GrGLRenderTarget*>(this->drawState()->getRenderTarget());
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001742 SkASSERT(NULL != rt);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001743
bsalomon1c63bf62014-07-22 13:09:46 -07001744 uint32_t rtID = rt->getUniqueID();
1745 if (fHWBoundRenderTargetUniqueID != rtID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001746 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, rt->renderFBOID()));
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +00001747#ifdef SK_DEBUG
rmistry@google.comd6bab022013-12-02 13:50:38 +00001748 // don't do this check in Chromium -- this is causing
1749 // lots of repeated command buffer flushes when the compositor is
1750 // rendering with Ganesh, which is really slow; even too slow for
1751 // Debug mode.
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +00001752 if (!this->glContext().isChromium()) {
rmistry@google.comd6bab022013-12-02 13:50:38 +00001753 GrGLenum status;
1754 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1755 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
1756 GrPrintf("GrGpuGL::flushRenderTarget glCheckFramebufferStatus %x\n", status);
1757 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001758 }
robertphillips@google.coma6ffb582013-04-29 16:50:17 +00001759#endif
bsalomon1c63bf62014-07-22 13:09:46 -07001760 fHWBoundRenderTargetUniqueID = rtID;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001761 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.coma3201942012-06-21 19:58:20 +00001762 if (fHWViewport != vp) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001763 vp.pushToGLViewport(this->glInterface());
bsalomon@google.coma3201942012-06-21 19:58:20 +00001764 fHWViewport = vp;
reed@google.comac10a2d2010-12-22 21:39:39 +00001765 }
1766 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001767 if (NULL == bound || !bound->isEmpty()) {
1768 rt->flagAsNeedingResolve(bound);
1769 }
skia.committer@gmail.comaeefb2a2013-07-27 07:01:06 +00001770
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +00001771 GrTexture *texture = rt->asTexture();
commit-bot@chromium.orge49157f2014-05-09 20:46:48 +00001772 if (NULL != texture) {
1773 texture->impl()->dirtyMipMaps(true);
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +00001774 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001775}
1776
twiz@google.com0f31ca72011-03-18 17:38:11 +00001777GrGLenum gPrimitiveType2GLMode[] = {
1778 GR_GL_TRIANGLES,
1779 GR_GL_TRIANGLE_STRIP,
1780 GR_GL_TRIANGLE_FAN,
1781 GR_GL_POINTS,
1782 GR_GL_LINES,
1783 GR_GL_LINE_STRIP
reed@google.comac10a2d2010-12-22 21:39:39 +00001784};
1785
bsalomon@google.comd302f142011-03-03 13:54:13 +00001786#define SWAP_PER_DRAW 0
1787
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001788#if SWAP_PER_DRAW
commit-bot@chromium.org43823302013-09-25 20:57:51 +00001789 #if defined(SK_BUILD_FOR_MAC)
bsalomon@google.comd302f142011-03-03 13:54:13 +00001790 #include <AGL/agl.h>
commit-bot@chromium.org43823302013-09-25 20:57:51 +00001791 #elif defined(SK_BUILD_FOR_WIN32)
bsalomon@google.comce7357d2012-06-25 17:49:25 +00001792 #include <gl/GL.h>
bsalomon@google.comd302f142011-03-03 13:54:13 +00001793 void SwapBuf() {
1794 DWORD procID = GetCurrentProcessId();
1795 HWND hwnd = GetTopWindow(GetDesktopWindow());
1796 while(hwnd) {
1797 DWORD wndProcID = 0;
1798 GetWindowThreadProcessId(hwnd, &wndProcID);
1799 if(wndProcID == procID) {
1800 SwapBuffers(GetDC(hwnd));
1801 }
1802 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
1803 }
1804 }
1805 #endif
1806#endif
1807
bsalomon@google.com74749cd2013-01-30 16:12:41 +00001808void GrGpuGL::onGpuDraw(const DrawInfo& info) {
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00001809 size_t indexOffsetInBytes;
1810 this->setupGeometry(info, &indexOffsetInBytes);
reed@google.comac10a2d2010-12-22 21:39:39 +00001811
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +00001812 SkASSERT((size_t)info.primitiveType() < SK_ARRAY_COUNT(gPrimitiveType2GLMode));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001813
bsalomon@google.com74749cd2013-01-30 16:12:41 +00001814 if (info.isIndexed()) {
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00001815 GrGLvoid* indices =
1816 reinterpret_cast<GrGLvoid*>(indexOffsetInBytes + sizeof(uint16_t) * info.startIndex());
bsalomon@google.com74749cd2013-01-30 16:12:41 +00001817 // info.startVertex() was accounted for by setupGeometry.
1818 GL_CALL(DrawElements(gPrimitiveType2GLMode[info.primitiveType()],
1819 info.indexCount(),
1820 GR_GL_UNSIGNED_SHORT,
1821 indices));
1822 } else {
1823 // Pass 0 for parameter first. We have to adjust glVertexAttribPointer() to account for
1824 // startVertex in the DrawElements case. So we always rely on setupGeometry to have
1825 // accounted for startVertex.
1826 GL_CALL(DrawArrays(gPrimitiveType2GLMode[info.primitiveType()], 0, info.vertexCount()));
1827 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00001828#if SWAP_PER_DRAW
1829 glFlush();
commit-bot@chromium.org43823302013-09-25 20:57:51 +00001830 #if defined(SK_BUILD_FOR_MAC)
bsalomon@google.comd302f142011-03-03 13:54:13 +00001831 aglSwapBuffers(aglGetCurrentContext());
1832 int set_a_break_pt_here = 9;
1833 aglSwapBuffers(aglGetCurrentContext());
commit-bot@chromium.org43823302013-09-25 20:57:51 +00001834 #elif defined(SK_BUILD_FOR_WIN32)
bsalomon@google.comd302f142011-03-03 13:54:13 +00001835 SwapBuf();
1836 int set_a_break_pt_here = 9;
1837 SwapBuf();
1838 #endif
1839#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001840}
1841
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001842static GrGLenum gr_stencil_op_to_gl_path_rendering_fill_mode(GrStencilOp op) {
1843 switch (op) {
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001844 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +00001845 SkFAIL("Unexpected path fill.");
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001846 /* fallthrough */;
1847 case kIncClamp_StencilOp:
1848 return GR_GL_COUNT_UP;
1849 case kInvert_StencilOp:
1850 return GR_GL_INVERT;
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001851 }
1852}
1853
sugoi@google.com12b4e272012-12-06 20:13:11 +00001854void GrGpuGL::onGpuStencilPath(const GrPath* path, SkPath::FillType fill) {
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001855 SkASSERT(this->caps()->pathRenderingSupport());
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001856
1857 GrGLuint id = static_cast<const GrGLPath*>(path)->pathID();
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001858 SkASSERT(NULL != this->drawState()->getRenderTarget());
1859 SkASSERT(NULL != this->drawState()->getRenderTarget()->getStencilBuffer());
1860
1861 flushPathStencilSettings(fill);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001862
1863 // Decide how to manipulate the stencil buffer based on the fill rule.
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001864 SkASSERT(!fHWPathStencilSettings.isTwoSided());
1865
1866 GrGLenum fillMode =
1867 gr_stencil_op_to_gl_path_rendering_fill_mode(fHWPathStencilSettings.passOp(GrStencilSettings::kFront_Face));
1868 GrGLint writeMask = fHWPathStencilSettings.writeMask(GrStencilSettings::kFront_Face);
cdaltonc7103a12014-08-11 14:05:05 -07001869 fPathRendering->stencilFillPath(id, fillMode, writeMask);
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00001870}
1871
commit-bot@chromium.org32184d82013-10-09 15:14:18 +00001872void GrGpuGL::onGpuDrawPath(const GrPath* path, SkPath::FillType fill) {
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001873 SkASSERT(this->caps()->pathRenderingSupport());
1874
1875 GrGLuint id = static_cast<const GrGLPath*>(path)->pathID();
1876 SkASSERT(NULL != this->drawState()->getRenderTarget());
1877 SkASSERT(NULL != this->drawState()->getRenderTarget()->getStencilBuffer());
1878 SkASSERT(!fCurrentProgram->hasVertexShader());
1879
1880 flushPathStencilSettings(fill);
commit-bot@chromium.org32184d82013-10-09 15:14:18 +00001881 const SkStrokeRec& stroke = path->getStroke();
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001882
1883 SkPath::FillType nonInvertedFill = SkPath::ConvertToNonInverseFillType(fill);
1884 SkASSERT(!fHWPathStencilSettings.isTwoSided());
1885 GrGLenum fillMode =
1886 gr_stencil_op_to_gl_path_rendering_fill_mode(fHWPathStencilSettings.passOp(GrStencilSettings::kFront_Face));
1887 GrGLint writeMask = fHWPathStencilSettings.writeMask(GrStencilSettings::kFront_Face);
commit-bot@chromium.org32184d82013-10-09 15:14:18 +00001888
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001889 if (nonInvertedFill == fill) {
commit-bot@chromium.org32184d82013-10-09 15:14:18 +00001890 if (stroke.needToApply()) {
cdaltonc8f52042014-07-29 15:25:51 -07001891 if (SkStrokeRec::kStrokeAndFill_Style == stroke.getStyle()) {
cdaltonc7103a12014-08-11 14:05:05 -07001892 fPathRendering->stencilFillPath(id, fillMode, writeMask);
cdaltonc8f52042014-07-29 15:25:51 -07001893 }
cdaltonc7103a12014-08-11 14:05:05 -07001894 fPathRendering->stencilThenCoverStrokePath(id, 0xffff, writeMask, GR_GL_BOUNDING_BOX);
commit-bot@chromium.org32184d82013-10-09 15:14:18 +00001895 } else {
cdaltonc7103a12014-08-11 14:05:05 -07001896 fPathRendering->stencilThenCoverFillPath(id, fillMode, writeMask, GR_GL_BOUNDING_BOX);
commit-bot@chromium.org32184d82013-10-09 15:14:18 +00001897 }
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001898 } else {
cdaltonc8f52042014-07-29 15:25:51 -07001899 if (stroke.isFillStyle() || SkStrokeRec::kStrokeAndFill_Style == stroke.getStyle()) {
cdaltonc7103a12014-08-11 14:05:05 -07001900 fPathRendering->stencilFillPath(id, fillMode, writeMask);
cdaltonc8f52042014-07-29 15:25:51 -07001901 }
1902 if (stroke.needToApply()) {
cdaltonc7103a12014-08-11 14:05:05 -07001903 fPathRendering->stencilStrokePath(id, 0xffff, writeMask);
cdaltonc8f52042014-07-29 15:25:51 -07001904 }
1905
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001906 GrDrawState* drawState = this->drawState();
1907 GrDrawState::AutoViewMatrixRestore avmr;
1908 SkRect bounds = SkRect::MakeLTRB(0, 0,
1909 SkIntToScalar(drawState->getRenderTarget()->width()),
1910 SkIntToScalar(drawState->getRenderTarget()->height()));
1911 SkMatrix vmi;
1912 // mapRect through persp matrix may not be correct
1913 if (!drawState->getViewMatrix().hasPerspective() && drawState->getViewInverse(&vmi)) {
1914 vmi.mapRect(&bounds);
1915 // theoretically could set bloat = 0, instead leave it because of matrix inversion
1916 // precision.
commit-bot@chromium.org18786512014-05-20 14:53:45 +00001917 SkScalar bloat = drawState->getViewMatrix().getMaxScale() * SK_ScalarHalf;
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001918 bounds.outset(bloat, bloat);
1919 } else {
1920 avmr.setIdentity(drawState);
1921 }
1922
bsalomon01c8da12014-08-04 09:21:30 -07001923 this->drawSimpleRect(bounds);
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001924 }
1925}
1926
cdaltonb85a0aa2014-07-21 15:32:44 -07001927void GrGpuGL::onGpuDrawPaths(const GrPathRange* pathRange,
1928 const uint32_t indices[], int count,
1929 const float transforms[], PathTransformType transformsType,
1930 SkPath::FillType fill) {
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +00001931 SkASSERT(this->caps()->pathRenderingSupport());
1932 SkASSERT(NULL != this->drawState()->getRenderTarget());
1933 SkASSERT(NULL != this->drawState()->getRenderTarget()->getStencilBuffer());
1934 SkASSERT(!fCurrentProgram->hasVertexShader());
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +00001935
cdaltonb85a0aa2014-07-21 15:32:44 -07001936 GrGLuint baseID = static_cast<const GrGLPathRange*>(pathRange)->basePathID();
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +00001937
1938 flushPathStencilSettings(fill);
cdaltonb85a0aa2014-07-21 15:32:44 -07001939 const SkStrokeRec& stroke = pathRange->getStroke();
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +00001940
1941 SkPath::FillType nonInvertedFill =
1942 SkPath::ConvertToNonInverseFillType(fill);
1943
1944 SkASSERT(!fHWPathStencilSettings.isTwoSided());
1945 GrGLenum fillMode =
1946 gr_stencil_op_to_gl_path_rendering_fill_mode(
1947 fHWPathStencilSettings.passOp(GrStencilSettings::kFront_Face));
1948 GrGLint writeMask =
1949 fHWPathStencilSettings.writeMask(GrStencilSettings::kFront_Face);
1950
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +00001951 if (nonInvertedFill == fill) {
cdaltonb85a0aa2014-07-21 15:32:44 -07001952 if (stroke.needToApply()) {
cdaltonc8f52042014-07-29 15:25:51 -07001953 if (SkStrokeRec::kStrokeAndFill_Style == stroke.getStyle()) {
cdaltonc7103a12014-08-11 14:05:05 -07001954 fPathRendering->stencilFillPathInstanced(
1955 count, GR_GL_UNSIGNED_INT, indices, baseID, fillMode,
1956 writeMask, gXformType2GLType[transformsType],
1957 transforms);
cdaltonc8f52042014-07-29 15:25:51 -07001958 }
cdaltonc7103a12014-08-11 14:05:05 -07001959 fPathRendering->stencilThenCoverStrokePathInstanced(
1960 count, GR_GL_UNSIGNED_INT, indices, baseID, 0xffff, writeMask,
1961 GR_GL_BOUNDING_BOX_OF_BOUNDING_BOXES,
1962 gXformType2GLType[transformsType], transforms);
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +00001963 } else {
cdaltonc7103a12014-08-11 14:05:05 -07001964 fPathRendering->stencilThenCoverFillPathInstanced(
1965 count, GR_GL_UNSIGNED_INT, indices, baseID, fillMode, writeMask,
1966 GR_GL_BOUNDING_BOX_OF_BOUNDING_BOXES,
1967 gXformType2GLType[transformsType], transforms);
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +00001968 }
1969 } else {
cdaltonc8f52042014-07-29 15:25:51 -07001970 if (stroke.isFillStyle() || SkStrokeRec::kStrokeAndFill_Style == stroke.getStyle()) {
cdaltonc7103a12014-08-11 14:05:05 -07001971 fPathRendering->stencilFillPathInstanced(
1972 count, GR_GL_UNSIGNED_INT, indices, baseID, fillMode,
1973 writeMask, gXformType2GLType[transformsType],
1974 transforms);
cdaltonc8f52042014-07-29 15:25:51 -07001975 }
1976 if (stroke.needToApply()) {
cdaltonc7103a12014-08-11 14:05:05 -07001977 fPathRendering->stencilStrokePathInstanced(
1978 count, GR_GL_UNSIGNED_INT, indices, baseID, 0xffff,
1979 writeMask, gXformType2GLType[transformsType],
1980 transforms);
cdaltonc8f52042014-07-29 15:25:51 -07001981 }
1982
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +00001983 GrDrawState* drawState = this->drawState();
1984 GrDrawState::AutoViewMatrixRestore avmr;
1985 SkRect bounds = SkRect::MakeLTRB(0, 0,
1986 SkIntToScalar(drawState->getRenderTarget()->width()),
1987 SkIntToScalar(drawState->getRenderTarget()->height()));
1988 SkMatrix vmi;
1989 // mapRect through persp matrix may not be correct
1990 if (!drawState->getViewMatrix().hasPerspective() && drawState->getViewInverse(&vmi)) {
1991 vmi.mapRect(&bounds);
1992 // theoretically could set bloat = 0, instead leave it because of matrix inversion
1993 // precision.
commit-bot@chromium.org18786512014-05-20 14:53:45 +00001994 SkScalar bloat = drawState->getViewMatrix().getMaxScale() * SK_ScalarHalf;
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +00001995 bounds.outset(bloat, bloat);
1996 } else {
1997 avmr.setIdentity(drawState);
1998 }
1999
bsalomon01c8da12014-08-04 09:21:30 -07002000 this->drawSimpleRect(bounds);
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +00002001 }
2002}
2003
bsalomon@google.com75f9f252012-01-31 13:35:56 +00002004void GrGpuGL::onResolveRenderTarget(GrRenderTarget* target) {
bsalomon@google.com75f9f252012-01-31 13:35:56 +00002005 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00002006 if (rt->needsResolve()) {
bsalomon@google.com347c3822013-05-01 20:10:01 +00002007 // Some extensions automatically resolves the texture when it is read.
2008 if (this->glCaps().usesMSAARenderBuffers()) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002009 SkASSERT(rt->textureFBOID() != rt->renderFBOID());
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002010 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER, rt->renderFBOID()));
2011 GL_CALL(BindFramebuffer(GR_GL_DRAW_FRAMEBUFFER, rt->textureFBOID()));
2012 // make sure we go through flushRenderTarget() since we've modified
2013 // the bound DRAW FBO ID.
bsalomon1c63bf62014-07-22 13:09:46 -07002014 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002015 const GrGLIRect& vp = rt->getViewport();
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00002016 const SkIRect dirtyRect = rt->getResolveRect();
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002017 GrGLIRect r;
2018 r.setRelativeTo(vp, dirtyRect.fLeft, dirtyRect.fTop,
2019 dirtyRect.width(), dirtyRect.height(), target->origin());
reed@google.comac10a2d2010-12-22 21:39:39 +00002020
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002021 GrAutoTRestore<ScissorState> asr;
bsalomon@google.com347c3822013-05-01 20:10:01 +00002022 if (GrGLCaps::kES_Apple_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002023 // Apple's extension uses the scissor as the blit bounds.
bsalomon@google.coma3201942012-06-21 19:58:20 +00002024 asr.reset(&fScissorState);
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002025 fScissorState.fEnabled = true;
2026 fScissorState.fRect = dirtyRect;
bsalomon@google.coma3201942012-06-21 19:58:20 +00002027 this->flushScissor();
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002028 GL_CALL(ResolveMultisampleFramebuffer());
2029 } else {
bsalomon@google.com347c3822013-05-01 20:10:01 +00002030 if (GrGLCaps::kDesktop_EXT_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002031 // this respects the scissor during the blit, so disable it.
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002032 asr.reset(&fScissorState);
2033 fScissorState.fEnabled = false;
2034 this->flushScissor();
2035 }
2036 int right = r.fLeft + r.fWidth;
2037 int top = r.fBottom + r.fHeight;
2038 GL_CALL(BlitFramebuffer(r.fLeft, r.fBottom, right, top,
2039 r.fLeft, r.fBottom, right, top,
2040 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00002041 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002042 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00002043 rt->flagAsResolved();
reed@google.comac10a2d2010-12-22 21:39:39 +00002044 }
2045}
2046
bsalomon@google.com411dad02012-06-05 20:24:20 +00002047namespace {
bsalomon@google.comd302f142011-03-03 13:54:13 +00002048
bsalomon@google.com411dad02012-06-05 20:24:20 +00002049GrGLenum gr_to_gl_stencil_func(GrStencilFunc basicFunc) {
2050 static const GrGLenum gTable[] = {
2051 GR_GL_ALWAYS, // kAlways_StencilFunc
2052 GR_GL_NEVER, // kNever_StencilFunc
2053 GR_GL_GREATER, // kGreater_StencilFunc
2054 GR_GL_GEQUAL, // kGEqual_StencilFunc
2055 GR_GL_LESS, // kLess_StencilFunc
2056 GR_GL_LEQUAL, // kLEqual_StencilFunc,
2057 GR_GL_EQUAL, // kEqual_StencilFunc,
2058 GR_GL_NOTEQUAL, // kNotEqual_StencilFunc,
2059 };
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +00002060 GR_STATIC_ASSERT(SK_ARRAY_COUNT(gTable) == kBasicStencilFuncCount);
bsalomon@google.com411dad02012-06-05 20:24:20 +00002061 GR_STATIC_ASSERT(0 == kAlways_StencilFunc);
2062 GR_STATIC_ASSERT(1 == kNever_StencilFunc);
2063 GR_STATIC_ASSERT(2 == kGreater_StencilFunc);
2064 GR_STATIC_ASSERT(3 == kGEqual_StencilFunc);
2065 GR_STATIC_ASSERT(4 == kLess_StencilFunc);
2066 GR_STATIC_ASSERT(5 == kLEqual_StencilFunc);
2067 GR_STATIC_ASSERT(6 == kEqual_StencilFunc);
2068 GR_STATIC_ASSERT(7 == kNotEqual_StencilFunc);
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002069 SkASSERT((unsigned) basicFunc < kBasicStencilFuncCount);
bsalomon@google.com411dad02012-06-05 20:24:20 +00002070
2071 return gTable[basicFunc];
2072}
2073
2074GrGLenum gr_to_gl_stencil_op(GrStencilOp op) {
2075 static const GrGLenum gTable[] = {
2076 GR_GL_KEEP, // kKeep_StencilOp
2077 GR_GL_REPLACE, // kReplace_StencilOp
2078 GR_GL_INCR_WRAP, // kIncWrap_StencilOp
2079 GR_GL_INCR, // kIncClamp_StencilOp
2080 GR_GL_DECR_WRAP, // kDecWrap_StencilOp
2081 GR_GL_DECR, // kDecClamp_StencilOp
2082 GR_GL_ZERO, // kZero_StencilOp
2083 GR_GL_INVERT, // kInvert_StencilOp
2084 };
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +00002085 GR_STATIC_ASSERT(SK_ARRAY_COUNT(gTable) == kStencilOpCount);
bsalomon@google.com411dad02012-06-05 20:24:20 +00002086 GR_STATIC_ASSERT(0 == kKeep_StencilOp);
2087 GR_STATIC_ASSERT(1 == kReplace_StencilOp);
2088 GR_STATIC_ASSERT(2 == kIncWrap_StencilOp);
2089 GR_STATIC_ASSERT(3 == kIncClamp_StencilOp);
2090 GR_STATIC_ASSERT(4 == kDecWrap_StencilOp);
2091 GR_STATIC_ASSERT(5 == kDecClamp_StencilOp);
2092 GR_STATIC_ASSERT(6 == kZero_StencilOp);
2093 GR_STATIC_ASSERT(7 == kInvert_StencilOp);
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002094 SkASSERT((unsigned) op < kStencilOpCount);
bsalomon@google.com411dad02012-06-05 20:24:20 +00002095 return gTable[op];
2096}
2097
2098void set_gl_stencil(const GrGLInterface* gl,
bsalomon@google.coma3201942012-06-21 19:58:20 +00002099 const GrStencilSettings& settings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00002100 GrGLenum glFace,
bsalomon@google.coma3201942012-06-21 19:58:20 +00002101 GrStencilSettings::Face grFace) {
2102 GrGLenum glFunc = gr_to_gl_stencil_func(settings.func(grFace));
2103 GrGLenum glFailOp = gr_to_gl_stencil_op(settings.failOp(grFace));
2104 GrGLenum glPassOp = gr_to_gl_stencil_op(settings.passOp(grFace));
2105
2106 GrGLint ref = settings.funcRef(grFace);
2107 GrGLint mask = settings.funcMask(grFace);
2108 GrGLint writeMask = settings.writeMask(grFace);
bsalomon@google.com411dad02012-06-05 20:24:20 +00002109
2110 if (GR_GL_FRONT_AND_BACK == glFace) {
2111 // we call the combined func just in case separate stencil is not
2112 // supported.
2113 GR_GL_CALL(gl, StencilFunc(glFunc, ref, mask));
2114 GR_GL_CALL(gl, StencilMask(writeMask));
2115 GR_GL_CALL(gl, StencilOp(glFailOp, glPassOp, glPassOp));
2116 } else {
2117 GR_GL_CALL(gl, StencilFuncSeparate(glFace, glFunc, ref, mask));
2118 GR_GL_CALL(gl, StencilMaskSeparate(glFace, writeMask));
2119 GR_GL_CALL(gl, StencilOpSeparate(glFace, glFailOp, glPassOp, glPassOp));
2120 }
2121}
2122}
bsalomon@google.comd302f142011-03-03 13:54:13 +00002123
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00002124void GrGpuGL::flushStencil(DrawType type) {
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00002125 if (kStencilPath_DrawType != type && fHWStencilSettings != fStencilSettings) {
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00002126 if (fStencilSettings.isDisabled()) {
2127 if (kNo_TriState != fHWStencilTestEnabled) {
2128 GL_CALL(Disable(GR_GL_STENCIL_TEST));
2129 fHWStencilTestEnabled = kNo_TriState;
2130 }
2131 } else {
2132 if (kYes_TriState != fHWStencilTestEnabled) {
2133 GL_CALL(Enable(GR_GL_STENCIL_TEST));
2134 fHWStencilTestEnabled = kYes_TriState;
2135 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00002136 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00002137 if (!fStencilSettings.isDisabled()) {
bsalomon@google.combcce8922013-03-25 15:38:39 +00002138 if (this->caps()->twoSidedStencilSupport()) {
bsalomon@google.com411dad02012-06-05 20:24:20 +00002139 set_gl_stencil(this->glInterface(),
bsalomon@google.coma3201942012-06-21 19:58:20 +00002140 fStencilSettings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00002141 GR_GL_FRONT,
bsalomon@google.coma3201942012-06-21 19:58:20 +00002142 GrStencilSettings::kFront_Face);
bsalomon@google.com411dad02012-06-05 20:24:20 +00002143 set_gl_stencil(this->glInterface(),
bsalomon@google.coma3201942012-06-21 19:58:20 +00002144 fStencilSettings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00002145 GR_GL_BACK,
bsalomon@google.coma3201942012-06-21 19:58:20 +00002146 GrStencilSettings::kBack_Face);
bsalomon@google.comd302f142011-03-03 13:54:13 +00002147 } else {
bsalomon@google.com411dad02012-06-05 20:24:20 +00002148 set_gl_stencil(this->glInterface(),
bsalomon@google.coma3201942012-06-21 19:58:20 +00002149 fStencilSettings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00002150 GR_GL_FRONT_AND_BACK,
bsalomon@google.coma3201942012-06-21 19:58:20 +00002151 GrStencilSettings::kFront_Face);
bsalomon@google.comd302f142011-03-03 13:54:13 +00002152 }
2153 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00002154 fHWStencilSettings = fStencilSettings;
reed@google.comac10a2d2010-12-22 21:39:39 +00002155 }
2156}
2157
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00002158void GrGpuGL::flushAAState(DrawType type) {
bsalomon@google.com202d1392013-03-19 18:58:08 +00002159// At least some ATI linux drivers will render GL_LINES incorrectly when MSAA state is enabled but
2160// the target is not multisampled. Single pixel wide lines are rendered thicker than 1 pixel wide.
2161#if 0
2162 // Replace RT_HAS_MSAA with this definition once this driver bug is no longer a relevant concern
2163 #define RT_HAS_MSAA rt->isMultisampled()
2164#else
2165 #define RT_HAS_MSAA (rt->isMultisampled() || kDrawLines_DrawType == type)
2166#endif
2167
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002168 const GrRenderTarget* rt = this->getDrawState().getRenderTarget();
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +00002169 if (kGL_GrGLStandard == this->glStandard()) {
egdanielb414f252014-07-29 13:15:47 -07002170 if (RT_HAS_MSAA) {
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00002171 // FIXME: GL_NV_pr doesn't seem to like MSAA disabled. The paths
2172 // convex hulls of each segment appear to get filled.
2173 bool enableMSAA = kStencilPath_DrawType == type ||
2174 this->getDrawState().isHWAntialiasState();
2175 if (enableMSAA) {
egdanielb414f252014-07-29 13:15:47 -07002176 if (kYes_TriState != fMSAAEnabled) {
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00002177 GL_CALL(Enable(GR_GL_MULTISAMPLE));
egdanielb414f252014-07-29 13:15:47 -07002178 fMSAAEnabled = kYes_TriState;
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00002179 }
2180 } else {
egdanielb414f252014-07-29 13:15:47 -07002181 if (kNo_TriState != fMSAAEnabled) {
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00002182 GL_CALL(Disable(GR_GL_MULTISAMPLE));
egdanielb414f252014-07-29 13:15:47 -07002183 fMSAAEnabled = kNo_TriState;
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00002184 }
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00002185 }
2186 }
2187 }
2188}
2189
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00002190void GrGpuGL::flushPathStencilSettings(SkPath::FillType fill) {
2191 GrStencilSettings pathStencilSettings;
2192 this->getPathStencilSettingsForFillType(fill, &pathStencilSettings);
2193 if (fHWPathStencilSettings != pathStencilSettings) {
2194 // Just the func, ref, and mask is set here. The op and write mask are params to the call
2195 // that draws the path to the SB (glStencilFillPath)
2196 GrGLenum func =
2197 gr_to_gl_stencil_func(pathStencilSettings.func(GrStencilSettings::kFront_Face));
cdaltonc7103a12014-08-11 14:05:05 -07002198 fPathRendering->pathStencilFunc(
2199 func, pathStencilSettings.funcRef(GrStencilSettings::kFront_Face),
2200 pathStencilSettings.funcMask(GrStencilSettings::kFront_Face));
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00002201
2202 fHWPathStencilSettings = pathStencilSettings;
2203 }
2204}
2205
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00002206void GrGpuGL::flushBlend(bool isLines,
bsalomon@google.com86c1f712011-10-12 14:54:26 +00002207 GrBlendCoeff srcCoeff,
bsalomon@google.com271cffc2011-05-20 14:13:56 +00002208 GrBlendCoeff dstCoeff) {
egdanielb414f252014-07-29 13:15:47 -07002209 // Any optimization to disable blending should have already been applied and
2210 // tweaked the coeffs to (1, 0).
2211 bool blendOff = kOne_GrBlendCoeff == srcCoeff && kZero_GrBlendCoeff == dstCoeff;
2212 if (blendOff) {
2213 if (kNo_TriState != fHWBlendState.fEnabled) {
2214 GL_CALL(Disable(GR_GL_BLEND));
2215 fHWBlendState.fEnabled = kNo_TriState;
2216 }
2217 } else {
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002218 if (kYes_TriState != fHWBlendState.fEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002219 GL_CALL(Enable(GR_GL_BLEND));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002220 fHWBlendState.fEnabled = kYes_TriState;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002221 }
egdanielb414f252014-07-29 13:15:47 -07002222 if (fHWBlendState.fSrcCoeff != srcCoeff ||
2223 fHWBlendState.fDstCoeff != dstCoeff) {
2224 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
2225 gXfermodeCoeff2Blend[dstCoeff]));
2226 fHWBlendState.fSrcCoeff = srcCoeff;
2227 fHWBlendState.fDstCoeff = dstCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002228 }
egdanielb414f252014-07-29 13:15:47 -07002229 GrColor blendConst = this->getDrawState().getBlendConstant();
2230 if ((BlendCoeffReferencesConstant(srcCoeff) ||
2231 BlendCoeffReferencesConstant(dstCoeff)) &&
2232 (!fHWBlendState.fConstColorValid ||
2233 fHWBlendState.fConstColor != blendConst)) {
2234 GrGLfloat c[4];
2235 GrColorToRGBAFloat(blendConst, c);
2236 GL_CALL(BlendColor(c[0], c[1], c[2], c[3]));
2237 fHWBlendState.fConstColor = blendConst;
2238 fHWBlendState.fConstColorValid = true;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002239 }
2240 }
2241}
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002242
commit-bot@chromium.org6364b5e2013-08-20 20:22:52 +00002243static inline GrGLenum tile_to_gl_wrap(SkShader::TileMode tm) {
bsalomon@google.comb8670992012-07-25 21:27:09 +00002244 static const GrGLenum gWrapModes[] = {
2245 GR_GL_CLAMP_TO_EDGE,
2246 GR_GL_REPEAT,
2247 GR_GL_MIRRORED_REPEAT
2248 };
commit-bot@chromium.org5d7ca952013-04-22 20:26:44 +00002249 GR_STATIC_ASSERT(SkShader::kTileModeCount == SK_ARRAY_COUNT(gWrapModes));
bsalomon@google.comb8670992012-07-25 21:27:09 +00002250 GR_STATIC_ASSERT(0 == SkShader::kClamp_TileMode);
2251 GR_STATIC_ASSERT(1 == SkShader::kRepeat_TileMode);
2252 GR_STATIC_ASSERT(2 == SkShader::kMirror_TileMode);
2253 return gWrapModes[tm];
2254}
2255
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002256void GrGpuGL::bindTexture(int unitIdx, const GrTextureParams& params, GrGLTexture* texture) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002257 SkASSERT(NULL != texture);
tomhudson@google.comd0c1a062012-07-12 17:23:52 +00002258
bsalomon@google.comb8670992012-07-25 21:27:09 +00002259 // If we created a rt/tex and rendered to it without using a texture and now we're texturing
2260 // 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 +00002261 // out of the "last != next" check.
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002262 GrGLRenderTarget* texRT = static_cast<GrGLRenderTarget*>(texture->asRenderTarget());
bsalomon@google.com4c883782012-06-04 19:05:11 +00002263 if (NULL != texRT) {
2264 this->onResolveRenderTarget(texRT);
2265 }
2266
bsalomon1c63bf62014-07-22 13:09:46 -07002267 uint32_t textureID = texture->getUniqueID();
2268 if (fHWBoundTextureUniqueIDs[unitIdx] != textureID) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002269 this->setTextureUnit(unitIdx);
2270 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, texture->textureID()));
bsalomon1c63bf62014-07-22 13:09:46 -07002271 fHWBoundTextureUniqueIDs[unitIdx] = textureID;
bsalomon@google.com4c883782012-06-04 19:05:11 +00002272 }
2273
bsalomon@google.com4c883782012-06-04 19:05:11 +00002274 ResetTimestamp timestamp;
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002275 const GrGLTexture::TexParams& oldTexParams = texture->getCachedTexParams(&timestamp);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002276 bool setAll = timestamp < this->getResetTimestamp();
2277 GrGLTexture::TexParams newTexParams;
2278
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00002279 static GrGLenum glMinFilterModes[] = {
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +00002280 GR_GL_NEAREST,
2281 GR_GL_LINEAR,
2282 GR_GL_LINEAR_MIPMAP_LINEAR
2283 };
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00002284 static GrGLenum glMagFilterModes[] = {
2285 GR_GL_NEAREST,
2286 GR_GL_LINEAR,
2287 GR_GL_LINEAR
2288 };
commit-bot@chromium.org47442312013-12-19 16:18:01 +00002289 GrTextureParams::FilterMode filterMode = params.filterMode();
2290 if (!this->caps()->mipMapSupport() && GrTextureParams::kMipMap_FilterMode == filterMode) {
2291 filterMode = GrTextureParams::kBilerp_FilterMode;
2292 }
2293 newTexParams.fMinFilter = glMinFilterModes[filterMode];
2294 newTexParams.fMagFilter = glMagFilterModes[filterMode];
skia.committer@gmail.comaeefb2a2013-07-27 07:01:06 +00002295
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +00002296 if (GrTextureParams::kMipMap_FilterMode == filterMode &&
2297 texture->mipMapsAreDirty() && !GrPixelConfigIsCompressed(texture->config())) {
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +00002298 GL_CALL(GenerateMipmap(GR_GL_TEXTURE_2D));
2299 texture->dirtyMipMaps(false);
2300 }
bsalomon@google.com4c883782012-06-04 19:05:11 +00002301
bsalomon@google.comb8670992012-07-25 21:27:09 +00002302 newTexParams.fWrapS = tile_to_gl_wrap(params.getTileModeX());
2303 newTexParams.fWrapT = tile_to_gl_wrap(params.getTileModeY());
bsalomon@google.com4c883782012-06-04 19:05:11 +00002304 memcpy(newTexParams.fSwizzleRGBA,
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002305 GrGLShaderBuilder::GetTexParamSwizzle(texture->config(), this->glCaps()),
bsalomon@google.com4c883782012-06-04 19:05:11 +00002306 sizeof(newTexParams.fSwizzleRGBA));
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00002307 if (setAll || newTexParams.fMagFilter != oldTexParams.fMagFilter) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002308 this->setTextureUnit(unitIdx);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002309 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2310 GR_GL_TEXTURE_MAG_FILTER,
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00002311 newTexParams.fMagFilter));
2312 }
2313 if (setAll || newTexParams.fMinFilter != oldTexParams.fMinFilter) {
2314 this->setTextureUnit(unitIdx);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002315 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2316 GR_GL_TEXTURE_MIN_FILTER,
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00002317 newTexParams.fMinFilter));
bsalomon@google.com4c883782012-06-04 19:05:11 +00002318 }
2319 if (setAll || newTexParams.fWrapS != oldTexParams.fWrapS) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002320 this->setTextureUnit(unitIdx);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002321 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2322 GR_GL_TEXTURE_WRAP_S,
2323 newTexParams.fWrapS));
2324 }
2325 if (setAll || newTexParams.fWrapT != oldTexParams.fWrapT) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002326 this->setTextureUnit(unitIdx);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002327 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2328 GR_GL_TEXTURE_WRAP_T,
2329 newTexParams.fWrapT));
2330 }
2331 if (this->glCaps().textureSwizzleSupport() &&
2332 (setAll || memcmp(newTexParams.fSwizzleRGBA,
2333 oldTexParams.fSwizzleRGBA,
2334 sizeof(newTexParams.fSwizzleRGBA)))) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002335 this->setTextureUnit(unitIdx);
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +00002336 if (this->glStandard() == kGLES_GrGLStandard) {
commit-bot@chromium.org6364b5e2013-08-20 20:22:52 +00002337 // ES3 added swizzle support but not GL_TEXTURE_SWIZZLE_RGBA.
2338 const GrGLenum* swizzle = newTexParams.fSwizzleRGBA;
2339 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_SWIZZLE_R, swizzle[0]));
2340 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_SWIZZLE_G, swizzle[1]));
2341 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_SWIZZLE_B, swizzle[2]));
2342 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_SWIZZLE_A, swizzle[3]));
2343 } else {
2344 GR_STATIC_ASSERT(sizeof(newTexParams.fSwizzleRGBA[0]) == sizeof(GrGLint));
2345 const GrGLint* swizzle = reinterpret_cast<const GrGLint*>(newTexParams.fSwizzleRGBA);
2346 GL_CALL(TexParameteriv(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_SWIZZLE_RGBA, swizzle));
2347 }
bsalomon@google.com4c883782012-06-04 19:05:11 +00002348 }
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002349 texture->setCachedTexParams(newTexParams, this->getResetTimestamp());
bsalomon@google.com4c883782012-06-04 19:05:11 +00002350}
2351
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +00002352void GrGpuGL::setProjectionMatrix(const SkMatrix& matrix,
2353 const SkISize& renderTargetSize,
2354 GrSurfaceOrigin renderTargetOrigin) {
2355
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +00002356 SkASSERT(this->glCaps().pathRenderingSupport());
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +00002357
2358 if (renderTargetOrigin == fHWProjectionMatrixState.fRenderTargetOrigin &&
2359 renderTargetSize == fHWProjectionMatrixState.fRenderTargetSize &&
2360 matrix.cheapEqualTo(fHWProjectionMatrixState.fViewMatrix)) {
2361 return;
2362 }
2363
2364 fHWProjectionMatrixState.fViewMatrix = matrix;
2365 fHWProjectionMatrixState.fRenderTargetSize = renderTargetSize;
2366 fHWProjectionMatrixState.fRenderTargetOrigin = renderTargetOrigin;
2367
2368 GrGLfloat glMatrix[4 * 4];
commit-bot@chromium.org47c66dd2014-05-29 01:12:10 +00002369 fHWProjectionMatrixState.getRTAdjustedGLMatrix<4>(glMatrix);
commit-bot@chromium.orgf6696722014-04-25 06:21:30 +00002370 GL_CALL(MatrixLoadf(GR_GL_PROJECTION, glMatrix));
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +00002371}
2372
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +00002373void GrGpuGL::enablePathTexGen(int unitIdx,
2374 PathTexGenComponents components,
2375 const GrGLfloat* coefficients) {
2376 SkASSERT(this->glCaps().pathRenderingSupport());
2377 SkASSERT(components >= kS_PathTexGenComponents &&
2378 components <= kSTR_PathTexGenComponents);
commit-bot@chromium.org8e919ad2013-10-21 14:48:23 +00002379 SkASSERT(this->glCaps().maxFixedFunctionTextureCoords() >= unitIdx);
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +00002380
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +00002381 if (GR_GL_OBJECT_LINEAR == fHWPathTexGenSettings[unitIdx].fMode &&
2382 components == fHWPathTexGenSettings[unitIdx].fNumComponents &&
2383 !memcmp(coefficients, fHWPathTexGenSettings[unitIdx].fCoefficients,
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +00002384 3 * components * sizeof(GrGLfloat))) {
2385 return;
2386 }
2387
2388 this->setTextureUnit(unitIdx);
2389
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +00002390 fHWPathTexGenSettings[unitIdx].fNumComponents = components;
cdaltonc7103a12014-08-11 14:05:05 -07002391 fPathRendering->pathTexGen(GR_GL_TEXTURE0 + unitIdx,
2392 GR_GL_OBJECT_LINEAR,
2393 components,
2394 coefficients);
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +00002395
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +00002396 memcpy(fHWPathTexGenSettings[unitIdx].fCoefficients, coefficients,
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +00002397 3 * components * sizeof(GrGLfloat));
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +00002398}
2399
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +00002400void GrGpuGL::enablePathTexGen(int unitIdx, PathTexGenComponents components,
2401 const SkMatrix& matrix) {
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +00002402 GrGLfloat coefficients[3 * 3];
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +00002403 SkASSERT(this->glCaps().pathRenderingSupport());
2404 SkASSERT(components >= kS_PathTexGenComponents &&
2405 components <= kSTR_PathTexGenComponents);
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +00002406
2407 coefficients[0] = SkScalarToFloat(matrix[SkMatrix::kMScaleX]);
2408 coefficients[1] = SkScalarToFloat(matrix[SkMatrix::kMSkewX]);
2409 coefficients[2] = SkScalarToFloat(matrix[SkMatrix::kMTransX]);
2410
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +00002411 if (components >= kST_PathTexGenComponents) {
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +00002412 coefficients[3] = SkScalarToFloat(matrix[SkMatrix::kMSkewY]);
2413 coefficients[4] = SkScalarToFloat(matrix[SkMatrix::kMScaleY]);
2414 coefficients[5] = SkScalarToFloat(matrix[SkMatrix::kMTransY]);
2415 }
2416
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +00002417 if (components >= kSTR_PathTexGenComponents) {
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +00002418 coefficients[6] = SkScalarToFloat(matrix[SkMatrix::kMPersp0]);
2419 coefficients[7] = SkScalarToFloat(matrix[SkMatrix::kMPersp1]);
2420 coefficients[8] = SkScalarToFloat(matrix[SkMatrix::kMPersp2]);
2421 }
2422
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +00002423 enablePathTexGen(unitIdx, components, coefficients);
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +00002424}
2425
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +00002426void GrGpuGL::flushPathTexGenSettings(int numUsedTexCoordSets) {
2427 SkASSERT(this->glCaps().pathRenderingSupport());
commit-bot@chromium.org8e919ad2013-10-21 14:48:23 +00002428 SkASSERT(this->glCaps().maxFixedFunctionTextureCoords() >= numUsedTexCoordSets);
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +00002429
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +00002430 // Only write the inactive path tex gens, since active path tex gens were
2431 // written when they were enabled.
commit-bot@chromium.org20807222013-11-01 11:54:54 +00002432
2433 SkDEBUGCODE(
2434 for (int i = 0; i < numUsedTexCoordSets; i++) {
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +00002435 SkASSERT(0 != fHWPathTexGenSettings[i].fNumComponents);
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +00002436 }
commit-bot@chromium.org20807222013-11-01 11:54:54 +00002437 );
2438
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +00002439 for (int i = numUsedTexCoordSets; i < fHWActivePathTexGenSets; i++) {
2440 SkASSERT(0 != fHWPathTexGenSettings[i].fNumComponents);
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +00002441
2442 this->setTextureUnit(i);
cdaltonc7103a12014-08-11 14:05:05 -07002443 fPathRendering->pathTexGen(GR_GL_TEXTURE0 + i, GR_GL_NONE, 0, NULL);
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +00002444 fHWPathTexGenSettings[i].fNumComponents = 0;
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +00002445 }
2446
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +00002447 fHWActivePathTexGenSets = numUsedTexCoordSets;
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +00002448}
2449
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002450void GrGpuGL::flushMiscFixedFunctionState() {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002451
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002452 const GrDrawState& drawState = this->getDrawState();
reed@google.comac10a2d2010-12-22 21:39:39 +00002453
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002454 if (drawState.isDitherState()) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002455 if (kYes_TriState != fHWDitherEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002456 GL_CALL(Enable(GR_GL_DITHER));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002457 fHWDitherEnabled = kYes_TriState;
2458 }
2459 } else {
2460 if (kNo_TriState != fHWDitherEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002461 GL_CALL(Disable(GR_GL_DITHER));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002462 fHWDitherEnabled = kNo_TriState;
reed@google.comac10a2d2010-12-22 21:39:39 +00002463 }
2464 }
2465
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002466 if (drawState.isColorWriteDisabled()) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002467 if (kNo_TriState != fHWWriteToColor) {
2468 GL_CALL(ColorMask(GR_GL_FALSE, GR_GL_FALSE,
2469 GR_GL_FALSE, GR_GL_FALSE));
2470 fHWWriteToColor = kNo_TriState;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002471 }
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002472 } else {
2473 if (kYes_TriState != fHWWriteToColor) {
2474 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
2475 fHWWriteToColor = kYes_TriState;
2476 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00002477 }
2478
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002479 if (fHWDrawFace != drawState.getDrawFace()) {
bsalomon@google.coma5d056a2012-03-27 15:59:58 +00002480 switch (this->getDrawState().getDrawFace()) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002481 case GrDrawState::kCCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002482 GL_CALL(Enable(GR_GL_CULL_FACE));
2483 GL_CALL(CullFace(GR_GL_BACK));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002484 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002485 case GrDrawState::kCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002486 GL_CALL(Enable(GR_GL_CULL_FACE));
2487 GL_CALL(CullFace(GR_GL_FRONT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002488 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002489 case GrDrawState::kBoth_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002490 GL_CALL(Disable(GR_GL_CULL_FACE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002491 break;
2492 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +00002493 SkFAIL("Unknown draw face.");
bsalomon@google.comd302f142011-03-03 13:54:13 +00002494 }
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002495 fHWDrawFace = drawState.getDrawFace();
bsalomon@google.comd302f142011-03-03 13:54:13 +00002496 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002497}
2498
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002499bool GrGpuGL::configToGLFormats(GrPixelConfig config,
2500 bool getSizedInternalFormat,
2501 GrGLenum* internalFormat,
2502 GrGLenum* externalFormat,
2503 GrGLenum* externalType) {
2504 GrGLenum dontCare;
2505 if (NULL == internalFormat) {
2506 internalFormat = &dontCare;
2507 }
2508 if (NULL == externalFormat) {
2509 externalFormat = &dontCare;
2510 }
2511 if (NULL == externalType) {
2512 externalType = &dontCare;
2513 }
2514
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +00002515 if(!this->glCaps().isConfigTexturable(config)) {
2516 return false;
2517 }
2518
reed@google.comac10a2d2010-12-22 21:39:39 +00002519 switch (config) {
bsalomon@google.com0342a852012-08-20 19:22:38 +00002520 case kRGBA_8888_GrPixelConfig:
bsalomon@google.comc4364992011-11-07 15:54:49 +00002521 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002522 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002523 if (getSizedInternalFormat) {
2524 *internalFormat = GR_GL_RGBA8;
2525 } else {
2526 *internalFormat = GR_GL_RGBA;
2527 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002528 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.comc4364992011-11-07 15:54:49 +00002529 break;
bsalomon@google.com0342a852012-08-20 19:22:38 +00002530 case kBGRA_8888_GrPixelConfig:
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002531 if (this->glCaps().bgraIsInternalFormat()) {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002532 if (getSizedInternalFormat) {
2533 *internalFormat = GR_GL_BGRA8;
2534 } else {
2535 *internalFormat = GR_GL_BGRA;
2536 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002537 } else {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002538 if (getSizedInternalFormat) {
2539 *internalFormat = GR_GL_RGBA8;
2540 } else {
2541 *internalFormat = GR_GL_RGBA;
2542 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002543 }
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002544 *externalFormat = GR_GL_BGRA;
bsalomon@google.com6f379512011-11-16 20:36:03 +00002545 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002546 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002547 case kRGB_565_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002548 *internalFormat = GR_GL_RGB;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002549 *externalFormat = GR_GL_RGB;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002550 if (getSizedInternalFormat) {
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +00002551 if (this->glStandard() == kGL_GrGLStandard) {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002552 return false;
2553 } else {
2554 *internalFormat = GR_GL_RGB565;
2555 }
2556 } else {
2557 *internalFormat = GR_GL_RGB;
2558 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002559 *externalType = GR_GL_UNSIGNED_SHORT_5_6_5;
reed@google.comac10a2d2010-12-22 21:39:39 +00002560 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002561 case kRGBA_4444_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002562 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002563 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002564 if (getSizedInternalFormat) {
2565 *internalFormat = GR_GL_RGBA4;
2566 } else {
2567 *internalFormat = GR_GL_RGBA;
2568 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002569 *externalType = GR_GL_UNSIGNED_SHORT_4_4_4_4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002570 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002571 case kIndex_8_GrPixelConfig:
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +00002572 // no sized/unsized internal format distinction here
2573 *internalFormat = GR_GL_PALETTE8_RGBA8;
reed@google.comac10a2d2010-12-22 21:39:39 +00002574 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002575 case kAlpha_8_GrPixelConfig:
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002576 if (this->glCaps().textureRedSupport()) {
2577 *internalFormat = GR_GL_RED;
2578 *externalFormat = GR_GL_RED;
2579 if (getSizedInternalFormat) {
2580 *internalFormat = GR_GL_R8;
2581 } else {
2582 *internalFormat = GR_GL_RED;
2583 }
2584 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002585 } else {
2586 *internalFormat = GR_GL_ALPHA;
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002587 *externalFormat = GR_GL_ALPHA;
2588 if (getSizedInternalFormat) {
2589 *internalFormat = GR_GL_ALPHA8;
2590 } else {
2591 *internalFormat = GR_GL_ALPHA;
2592 }
2593 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002594 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002595 break;
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +00002596 case kETC1_GrPixelConfig:
2597 *internalFormat = GR_GL_COMPRESSED_RGB8_ETC1;
2598 break;
2599 case kLATC_GrPixelConfig:
2600 switch(this->glCaps().latcAlias()) {
2601 case GrGLCaps::kLATC_LATCAlias:
2602 *internalFormat = GR_GL_COMPRESSED_LUMINANCE_LATC1;
2603 break;
2604 case GrGLCaps::kRGTC_LATCAlias:
2605 *internalFormat = GR_GL_COMPRESSED_RED_RGTC1;
2606 break;
2607 case GrGLCaps::k3DC_LATCAlias:
2608 *internalFormat = GR_GL_COMPRESSED_3DC_X;
2609 break;
2610 }
2611 break;
krajcevski238b4562014-06-30 09:09:22 -07002612 case kR11_EAC_GrPixelConfig:
2613 *internalFormat = GR_GL_COMPRESSED_R11;
2614 break;
krajcevski7ef21622014-07-16 15:21:13 -07002615
2616 case kASTC_12x12_GrPixelConfig:
2617 *internalFormat = GR_GL_COMPRESSED_RGBA_ASTC_12x12;
2618 break;
2619
joshualittee5da552014-07-16 13:32:56 -07002620 case kRGBA_float_GrPixelConfig:
2621 *internalFormat = GR_GL_RGBA32F;
2622 *externalFormat = GR_GL_RGBA;
2623 *externalType = GR_GL_FLOAT;
2624 break;
krajcevski7ef21622014-07-16 15:21:13 -07002625
reed@google.comac10a2d2010-12-22 21:39:39 +00002626 default:
2627 return false;
2628 }
2629 return true;
2630}
2631
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002632void GrGpuGL::setTextureUnit(int unit) {
bsalomon1c63bf62014-07-22 13:09:46 -07002633 SkASSERT(unit >= 0 && unit < fHWBoundTextureUniqueIDs.count());
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002634 if (unit != fHWActiveTextureUnitIdx) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002635 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com49209392012-06-05 15:13:46 +00002636 fHWActiveTextureUnitIdx = unit;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002637 }
2638}
bsalomon@google.com316f99232011-01-13 21:28:12 +00002639
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002640void GrGpuGL::setScratchTextureUnit() {
2641 // Bind the last texture unit since it is the least likely to be used by GrGLProgram.
bsalomon1c63bf62014-07-22 13:09:46 -07002642 int lastUnitIdx = fHWBoundTextureUniqueIDs.count() - 1;
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002643 if (lastUnitIdx != fHWActiveTextureUnitIdx) {
2644 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + lastUnitIdx));
2645 fHWActiveTextureUnitIdx = lastUnitIdx;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002646 }
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002647 // clear out the this field so that if a program does use this unit it will rebind the correct
2648 // texture.
bsalomon1c63bf62014-07-22 13:09:46 -07002649 fHWBoundTextureUniqueIDs[lastUnitIdx] = SK_InvalidUniqueID;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002650}
2651
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002652namespace {
2653// Determines whether glBlitFramebuffer could be used between src and dst.
bsalomon@google.comeb851172013-04-15 13:51:00 +00002654inline bool can_blit_framebuffer(const GrSurface* dst,
2655 const GrSurface* src,
2656 const GrGpuGL* gpu,
2657 bool* wouldNeedTempFBO = NULL) {
commit-bot@chromium.org6b7938f2013-10-15 14:18:16 +00002658 if (gpu->glCaps().isConfigRenderable(dst->config(), dst->desc().fSampleCnt > 0) &&
2659 gpu->glCaps().isConfigRenderable(src->config(), src->desc().fSampleCnt > 0) &&
bsalomon@google.com347c3822013-05-01 20:10:01 +00002660 gpu->glCaps().usesMSAARenderBuffers()) {
commit-bot@chromium.orga8e5a062013-09-05 23:44:09 +00002661 // ES3 doesn't allow framebuffer blits when the src has MSAA and the configs don't match
2662 // or the rects are not the same (not just the same size but have the same edges).
2663 if (GrGLCaps::kES_3_0_MSFBOType == gpu->glCaps().msFBOType() &&
2664 (src->desc().fSampleCnt > 0 || src->config() != dst->config())) {
2665 return false;
2666 }
bsalomon@google.comeb851172013-04-15 13:51:00 +00002667 if (NULL != wouldNeedTempFBO) {
2668 *wouldNeedTempFBO = NULL == dst->asRenderTarget() || NULL == src->asRenderTarget();
2669 }
2670 return true;
2671 } else {
2672 return false;
2673 }
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002674}
bsalomon@google.comeb851172013-04-15 13:51:00 +00002675
2676inline bool can_copy_texsubimage(const GrSurface* dst,
2677 const GrSurface* src,
2678 const GrGpuGL* gpu,
2679 bool* wouldNeedTempFBO = NULL) {
2680 // Table 3.9 of the ES2 spec indicates the supported formats with CopyTexSubImage
2681 // and BGRA isn't in the spec. There doesn't appear to be any extension that adds it. Perhaps
2682 // many drivers would allow it to work, but ANGLE does not.
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +00002683 if (kGLES_GrGLStandard == gpu->glStandard() && gpu->glCaps().bgraIsInternalFormat() &&
bsalomon@google.comeb851172013-04-15 13:51:00 +00002684 (kBGRA_8888_GrPixelConfig == dst->config() || kBGRA_8888_GrPixelConfig == src->config())) {
2685 return false;
2686 }
2687 const GrGLRenderTarget* dstRT = static_cast<const GrGLRenderTarget*>(dst->asRenderTarget());
2688 // If dst is multisampled (and uses an extension where there is a separate MSAA renderbuffer)
2689 // then we don't want to copy to the texture but to the MSAA buffer.
2690 if (NULL != dstRT && dstRT->renderFBOID() != dstRT->textureFBOID()) {
2691 return false;
2692 }
bsalomon@google.coma2719852013-04-17 14:25:27 +00002693 const GrGLRenderTarget* srcRT = static_cast<const GrGLRenderTarget*>(src->asRenderTarget());
2694 // If the src is multisampled (and uses an extension where there is a separate MSAA
2695 // renderbuffer) then it is an invalid operation to call CopyTexSubImage
2696 if (NULL != srcRT && srcRT->renderFBOID() != srcRT->textureFBOID()) {
2697 return false;
2698 }
commit-bot@chromium.org6b7938f2013-10-15 14:18:16 +00002699 if (gpu->glCaps().isConfigRenderable(src->config(), src->desc().fSampleCnt > 0) &&
2700 NULL != dst->asTexture() &&
2701 dst->origin() == src->origin() &&
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +00002702 !GrPixelConfigIsCompressed(src->config())) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002703 if (NULL != wouldNeedTempFBO) {
2704 *wouldNeedTempFBO = NULL == src->asRenderTarget();
2705 }
2706 return true;
2707 } else {
2708 return false;
2709 }
2710}
2711
2712// If a temporary FBO was created, its non-zero ID is returned. The viewport that the copy rect is
2713// relative to is output.
2714inline GrGLuint bind_surface_as_fbo(const GrGLInterface* gl,
2715 GrSurface* surface,
2716 GrGLenum fboTarget,
2717 GrGLIRect* viewport) {
2718 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(surface->asRenderTarget());
2719 GrGLuint tempFBOID;
2720 if (NULL == rt) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002721 SkASSERT(NULL != surface->asTexture());
bsalomon@google.comeb851172013-04-15 13:51:00 +00002722 GrGLuint texID = static_cast<GrGLTexture*>(surface->asTexture())->textureID();
2723 GR_GL_CALL(gl, GenFramebuffers(1, &tempFBOID));
2724 GR_GL_CALL(gl, BindFramebuffer(fboTarget, tempFBOID));
2725 GR_GL_CALL(gl, FramebufferTexture2D(fboTarget,
2726 GR_GL_COLOR_ATTACHMENT0,
2727 GR_GL_TEXTURE_2D,
2728 texID,
2729 0));
2730 viewport->fLeft = 0;
2731 viewport->fBottom = 0;
2732 viewport->fWidth = surface->width();
2733 viewport->fHeight = surface->height();
2734 } else {
2735 tempFBOID = 0;
2736 GR_GL_CALL(gl, BindFramebuffer(fboTarget, rt->renderFBOID()));
2737 *viewport = rt->getViewport();
2738 }
2739 return tempFBOID;
2740}
2741
2742}
2743
2744void GrGpuGL::initCopySurfaceDstDesc(const GrSurface* src, GrTextureDesc* desc) {
2745 // Check for format issues with glCopyTexSubImage2D
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +00002746 if (kGLES_GrGLStandard == this->glStandard() && this->glCaps().bgraIsInternalFormat() &&
bsalomon@google.comeb851172013-04-15 13:51:00 +00002747 kBGRA_8888_GrPixelConfig == src->config()) {
2748 // glCopyTexSubImage2D doesn't work with this config. We'll want to make it a render target
bsalomon@google.com31c4e892013-04-15 13:55:02 +00002749 // in order to call glBlitFramebuffer or to copy to it by rendering.
bsalomon@google.comeb851172013-04-15 13:51:00 +00002750 INHERITED::initCopySurfaceDstDesc(src, desc);
bsalomon@google.coma2719852013-04-17 14:25:27 +00002751 return;
bsalomon@google.comeb851172013-04-15 13:51:00 +00002752 } else if (NULL == src->asRenderTarget()) {
2753 // We don't want to have to create an FBO just to use glCopyTexSubImage2D. Let the base
2754 // class handle it by rendering.
2755 INHERITED::initCopySurfaceDstDesc(src, desc);
bsalomon@google.coma2719852013-04-17 14:25:27 +00002756 return;
2757 }
2758
2759 const GrGLRenderTarget* srcRT = static_cast<const GrGLRenderTarget*>(src->asRenderTarget());
2760 if (NULL != srcRT && srcRT->renderFBOID() != srcRT->textureFBOID()) {
2761 // It's illegal to call CopyTexSubImage2D on a MSAA renderbuffer.
2762 INHERITED::initCopySurfaceDstDesc(src, desc);
bsalomon@google.comeb851172013-04-15 13:51:00 +00002763 } else {
2764 desc->fConfig = src->config();
2765 desc->fOrigin = src->origin();
2766 desc->fFlags = kNone_GrTextureFlags;
2767 }
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002768}
2769
2770bool GrGpuGL::onCopySurface(GrSurface* dst,
2771 GrSurface* src,
2772 const SkIRect& srcRect,
2773 const SkIPoint& dstPoint) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002774 bool inheritedCouldCopy = INHERITED::onCanCopySurface(dst, src, srcRect, dstPoint);
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002775 bool copied = false;
bsalomon@google.comeb851172013-04-15 13:51:00 +00002776 bool wouldNeedTempFBO = false;
2777 if (can_copy_texsubimage(dst, src, this, &wouldNeedTempFBO) &&
2778 (!wouldNeedTempFBO || !inheritedCouldCopy)) {
2779 GrGLuint srcFBO;
2780 GrGLIRect srcVP;
2781 srcFBO = bind_surface_as_fbo(this->glInterface(), src, GR_GL_FRAMEBUFFER, &srcVP);
2782 GrGLTexture* dstTex = static_cast<GrGLTexture*>(dst->asTexture());
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002783 SkASSERT(NULL != dstTex);
bsalomon@google.comeb851172013-04-15 13:51:00 +00002784 // We modified the bound FBO
bsalomon1c63bf62014-07-22 13:09:46 -07002785 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
bsalomon@google.comeb851172013-04-15 13:51:00 +00002786 GrGLIRect srcGLRect;
2787 srcGLRect.setRelativeTo(srcVP,
2788 srcRect.fLeft,
2789 srcRect.fTop,
2790 srcRect.width(),
2791 srcRect.height(),
2792 src->origin());
2793
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002794 this->setScratchTextureUnit();
bsalomon@google.comeb851172013-04-15 13:51:00 +00002795 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, dstTex->textureID()));
2796 GrGLint dstY;
2797 if (kBottomLeft_GrSurfaceOrigin == dst->origin()) {
2798 dstY = dst->height() - (dstPoint.fY + srcGLRect.fHeight);
2799 } else {
2800 dstY = dstPoint.fY;
2801 }
2802 GL_CALL(CopyTexSubImage2D(GR_GL_TEXTURE_2D, 0,
2803 dstPoint.fX, dstY,
2804 srcGLRect.fLeft, srcGLRect.fBottom,
2805 srcGLRect.fWidth, srcGLRect.fHeight));
2806 copied = true;
2807 if (srcFBO) {
2808 GL_CALL(DeleteFramebuffers(1, &srcFBO));
2809 }
2810 } else if (can_blit_framebuffer(dst, src, this, &wouldNeedTempFBO) &&
2811 (!wouldNeedTempFBO || !inheritedCouldCopy)) {
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002812 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
2813 srcRect.width(), srcRect.height());
2814 bool selfOverlap = false;
2815 if (dst->isSameAs(src)) {
2816 selfOverlap = SkIRect::IntersectsNoEmptyCheck(dstRect, srcRect);
2817 }
2818
2819 if (!selfOverlap) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002820 GrGLuint dstFBO;
2821 GrGLuint srcFBO;
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002822 GrGLIRect dstVP;
2823 GrGLIRect srcVP;
bsalomon@google.comeb851172013-04-15 13:51:00 +00002824 dstFBO = bind_surface_as_fbo(this->glInterface(), dst, GR_GL_DRAW_FRAMEBUFFER, &dstVP);
2825 srcFBO = bind_surface_as_fbo(this->glInterface(), src, GR_GL_READ_FRAMEBUFFER, &srcVP);
2826 // We modified the bound FBO
bsalomon1c63bf62014-07-22 13:09:46 -07002827 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002828 GrGLIRect srcGLRect;
2829 GrGLIRect dstGLRect;
2830 srcGLRect.setRelativeTo(srcVP,
2831 srcRect.fLeft,
2832 srcRect.fTop,
2833 srcRect.width(),
2834 srcRect.height(),
2835 src->origin());
2836 dstGLRect.setRelativeTo(dstVP,
2837 dstRect.fLeft,
2838 dstRect.fTop,
2839 dstRect.width(),
2840 dstRect.height(),
2841 dst->origin());
2842
2843 GrAutoTRestore<ScissorState> asr;
bsalomon@google.com347c3822013-05-01 20:10:01 +00002844 if (GrGLCaps::kDesktop_EXT_MSFBOType == this->glCaps().msFBOType()) {
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002845 // The EXT version applies the scissor during the blit, so disable it.
2846 asr.reset(&fScissorState);
2847 fScissorState.fEnabled = false;
2848 this->flushScissor();
2849 }
2850 GrGLint srcY0;
2851 GrGLint srcY1;
2852 // Does the blit need to y-mirror or not?
2853 if (src->origin() == dst->origin()) {
2854 srcY0 = srcGLRect.fBottom;
2855 srcY1 = srcGLRect.fBottom + srcGLRect.fHeight;
2856 } else {
2857 srcY0 = srcGLRect.fBottom + srcGLRect.fHeight;
2858 srcY1 = srcGLRect.fBottom;
2859 }
2860 GL_CALL(BlitFramebuffer(srcGLRect.fLeft,
2861 srcY0,
2862 srcGLRect.fLeft + srcGLRect.fWidth,
2863 srcY1,
2864 dstGLRect.fLeft,
2865 dstGLRect.fBottom,
2866 dstGLRect.fLeft + dstGLRect.fWidth,
2867 dstGLRect.fBottom + dstGLRect.fHeight,
2868 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
2869 if (dstFBO) {
2870 GL_CALL(DeleteFramebuffers(1, &dstFBO));
2871 }
2872 if (srcFBO) {
2873 GL_CALL(DeleteFramebuffers(1, &srcFBO));
2874 }
2875 copied = true;
2876 }
2877 }
bsalomon@google.comeb851172013-04-15 13:51:00 +00002878 if (!copied && inheritedCouldCopy) {
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002879 copied = INHERITED::onCopySurface(dst, src, srcRect, dstPoint);
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002880 SkASSERT(copied);
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002881 }
2882 return copied;
2883}
2884
2885bool GrGpuGL::onCanCopySurface(GrSurface* dst,
2886 GrSurface* src,
2887 const SkIRect& srcRect,
2888 const SkIPoint& dstPoint) {
2889 // This mirrors the logic in onCopySurface.
bsalomon@google.comeb851172013-04-15 13:51:00 +00002890 if (can_copy_texsubimage(dst, src, this)) {
2891 return true;
2892 }
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002893 if (can_blit_framebuffer(dst, src, this)) {
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002894 if (dst->isSameAs(src)) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002895 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
2896 srcRect.width(), srcRect.height());
2897 if(!SkIRect::IntersectsNoEmptyCheck(dstRect, srcRect)) {
2898 return true;
2899 }
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002900 } else {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002901 return true;
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002902 }
2903 }
bsalomon@google.comeb851172013-04-15 13:51:00 +00002904 return INHERITED::onCanCopySurface(dst, src, srcRect, dstPoint);
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002905}
2906
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +00002907void GrGpuGL::didAddGpuTraceMarker() {
commit-bot@chromium.orga3baf3b2014-02-21 18:45:30 +00002908 if (this->caps()->gpuTracingSupport()) {
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +00002909 const GrTraceMarkerSet& markerArray = this->getActiveTraceMarkers();
egdanield78a1682014-07-09 10:41:26 -07002910 SkString markerString = markerArray.toStringLast();
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +00002911 GL_CALL(PushGroupMarker(0, markerString.c_str()));
commit-bot@chromium.orga3baf3b2014-02-21 18:45:30 +00002912 }
2913}
2914
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +00002915void GrGpuGL::didRemoveGpuTraceMarker() {
commit-bot@chromium.orga3baf3b2014-02-21 18:45:30 +00002916 if (this->caps()->gpuTracingSupport()) {
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +00002917 GL_CALL(PopGroupMarker());
commit-bot@chromium.orga3baf3b2014-02-21 18:45:30 +00002918 }
2919}
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00002920///////////////////////////////////////////////////////////////////////////////
2921
bsalomon@google.com6918d482013-03-07 19:09:11 +00002922GrGLAttribArrayState* GrGpuGL::HWGeometryState::bindArrayAndBuffersToDraw(
2923 GrGpuGL* gpu,
2924 const GrGLVertexBuffer* vbuffer,
2925 const GrGLIndexBuffer* ibuffer) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002926 SkASSERT(NULL != vbuffer);
robertphillips@google.com4f65a272013-03-26 19:40:46 +00002927 GrGLAttribArrayState* attribState;
2928
bsalomon@google.com6918d482013-03-07 19:09:11 +00002929 // We use a vertex array if we're on a core profile and the verts are in a VBO.
2930 if (gpu->glCaps().isCoreProfile() && !vbuffer->isCPUBacked()) {
commit-bot@chromium.org089a7802014-05-02 21:38:22 +00002931 if (NULL == fVBOVertexArray || fVBOVertexArray->wasDestroyed()) {
bsalomon@google.com6918d482013-03-07 19:09:11 +00002932 SkSafeUnref(fVBOVertexArray);
2933 GrGLuint arrayID;
2934 GR_GL_CALL(gpu->glInterface(), GenVertexArrays(1, &arrayID));
2935 int attrCount = gpu->glCaps().maxVertexAttributes();
2936 fVBOVertexArray = SkNEW_ARGS(GrGLVertexArray, (gpu, arrayID, attrCount));
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00002937 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00002938 attribState = fVBOVertexArray->bindWithIndexBuffer(ibuffer);
2939 } else {
2940 if (NULL != ibuffer) {
2941 this->setIndexBufferIDOnDefaultVertexArray(gpu, ibuffer->bufferID());
2942 } else {
2943 this->setVertexArrayID(gpu, 0);
2944 }
2945 int attrCount = gpu->glCaps().maxVertexAttributes();
2946 if (fDefaultVertexArrayAttribState.count() != attrCount) {
2947 fDefaultVertexArrayAttribState.resize(attrCount);
2948 }
2949 attribState = &fDefaultVertexArrayAttribState;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00002950 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00002951 return attribState;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002952}