blob: 5ebbe1f3b46845d610f6818e2493c4684a64413d [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
jvanverth39edf762014-12-22 11:44:19 -08009#include "GrGLGpu.h"
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000010#include "GrGLStencilBuffer.h"
bsalomon37dd3312014-11-03 08:47:23 -080011#include "GrGLTextureRenderTarget.h"
bsalomon3582d3e2015-02-13 14:20:05 -080012#include "GrGpuResourcePriv.h"
egdaniel8dd688b2015-01-22 10:16:09 -080013#include "GrPipeline.h"
bsalomon6bc1b5f2015-02-23 09:06:38 -080014#include "GrRenderTargetPriv.h"
bsalomonafbf2d62014-09-30 12:18:44 -070015#include "GrSurfacePriv.h"
bsalomon@google.coma3201942012-06-21 19:58:20 +000016#include "GrTemplates.h"
bsalomonafbf2d62014-09-30 12:18:44 -070017#include "GrTexturePriv.h"
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +000018#include "GrTypes.h"
commit-bot@chromium.org32184d82013-10-09 15:14:18 +000019#include "SkStrokeRec.h"
bsalomon@google.com3582bf92011-06-30 21:32:31 +000020#include "SkTemplates.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000021
bsalomon@google.com0b77d682011-08-19 13:28:54 +000022#define GL_CALL(X) GR_GL_CALL(this->glInterface(), X)
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +000023#define GL_CALL_RET(RET, X) GR_GL_CALL_RET(this->glInterface(), RET, X)
bsalomon@google.com0b77d682011-08-19 13:28:54 +000024
reed@google.comac10a2d2010-12-22 21:39:39 +000025#define SKIP_CACHE_CHECK true
26
bsalomon@google.com4f3c2532012-01-19 16:16:52 +000027#if GR_GL_CHECK_ALLOC_WITH_GET_ERROR
28 #define CLEAR_ERROR_BEFORE_ALLOC(iface) GrGLClearErr(iface)
29 #define GL_ALLOC_CALL(iface, call) GR_GL_CALL_NOERRCHECK(iface, call)
30 #define CHECK_ALLOC_ERROR(iface) GR_GL_GET_ERROR(iface)
rmistry@google.comfbfcd562012-08-23 18:09:54 +000031#else
bsalomon@google.com4f3c2532012-01-19 16:16:52 +000032 #define CLEAR_ERROR_BEFORE_ALLOC(iface)
33 #define GL_ALLOC_CALL(iface, call) GR_GL_CALL(iface, call)
34 #define CHECK_ALLOC_ERROR(iface) GR_GL_NO_ERROR
35#endif
36
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +000037
38///////////////////////////////////////////////////////////////////////////////
39
cdaltonb85a0aa2014-07-21 15:32:44 -070040
twiz@google.com0f31ca72011-03-18 17:38:11 +000041static const GrGLenum gXfermodeCoeff2Blend[] = {
42 GR_GL_ZERO,
43 GR_GL_ONE,
44 GR_GL_SRC_COLOR,
45 GR_GL_ONE_MINUS_SRC_COLOR,
46 GR_GL_DST_COLOR,
47 GR_GL_ONE_MINUS_DST_COLOR,
48 GR_GL_SRC_ALPHA,
49 GR_GL_ONE_MINUS_SRC_ALPHA,
50 GR_GL_DST_ALPHA,
51 GR_GL_ONE_MINUS_DST_ALPHA,
52 GR_GL_CONSTANT_COLOR,
53 GR_GL_ONE_MINUS_CONSTANT_COLOR,
54 GR_GL_CONSTANT_ALPHA,
55 GR_GL_ONE_MINUS_CONSTANT_ALPHA,
bsalomon@google.com271cffc2011-05-20 14:13:56 +000056
57 // extended blend coeffs
58 GR_GL_SRC1_COLOR,
59 GR_GL_ONE_MINUS_SRC1_COLOR,
60 GR_GL_SRC1_ALPHA,
61 GR_GL_ONE_MINUS_SRC1_ALPHA,
reed@google.comac10a2d2010-12-22 21:39:39 +000062};
63
bsalomon861e1032014-12-16 07:33:49 -080064bool GrGLGpu::BlendCoeffReferencesConstant(GrBlendCoeff coeff) {
bsalomon@google.com080773c2011-03-15 19:09:25 +000065 static const bool gCoeffReferencesBlendConst[] = {
66 false,
67 false,
68 false,
69 false,
70 false,
71 false,
72 false,
73 false,
74 false,
75 false,
76 true,
77 true,
78 true,
79 true,
bsalomon@google.com271cffc2011-05-20 14:13:56 +000080
81 // extended blend coeffs
82 false,
83 false,
84 false,
85 false,
bsalomon@google.com080773c2011-03-15 19:09:25 +000086 };
87 return gCoeffReferencesBlendConst[coeff];
bsalomon@google.com47059542012-06-06 20:51:20 +000088 GR_STATIC_ASSERT(kTotalGrBlendCoeffCount ==
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +000089 SK_ARRAY_COUNT(gCoeffReferencesBlendConst));
bsalomon@google.com271cffc2011-05-20 14:13:56 +000090
bsalomon@google.com47059542012-06-06 20:51:20 +000091 GR_STATIC_ASSERT(0 == kZero_GrBlendCoeff);
92 GR_STATIC_ASSERT(1 == kOne_GrBlendCoeff);
93 GR_STATIC_ASSERT(2 == kSC_GrBlendCoeff);
94 GR_STATIC_ASSERT(3 == kISC_GrBlendCoeff);
95 GR_STATIC_ASSERT(4 == kDC_GrBlendCoeff);
96 GR_STATIC_ASSERT(5 == kIDC_GrBlendCoeff);
97 GR_STATIC_ASSERT(6 == kSA_GrBlendCoeff);
98 GR_STATIC_ASSERT(7 == kISA_GrBlendCoeff);
99 GR_STATIC_ASSERT(8 == kDA_GrBlendCoeff);
100 GR_STATIC_ASSERT(9 == kIDA_GrBlendCoeff);
101 GR_STATIC_ASSERT(10 == kConstC_GrBlendCoeff);
102 GR_STATIC_ASSERT(11 == kIConstC_GrBlendCoeff);
103 GR_STATIC_ASSERT(12 == kConstA_GrBlendCoeff);
104 GR_STATIC_ASSERT(13 == kIConstA_GrBlendCoeff);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000105
bsalomon@google.com47059542012-06-06 20:51:20 +0000106 GR_STATIC_ASSERT(14 == kS2C_GrBlendCoeff);
107 GR_STATIC_ASSERT(15 == kIS2C_GrBlendCoeff);
108 GR_STATIC_ASSERT(16 == kS2A_GrBlendCoeff);
109 GR_STATIC_ASSERT(17 == kIS2A_GrBlendCoeff);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000110
111 // assertion for gXfermodeCoeff2Blend have to be in GrGpu scope
bsalomon@google.com47059542012-06-06 20:51:20 +0000112 GR_STATIC_ASSERT(kTotalGrBlendCoeffCount ==
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000113 SK_ARRAY_COUNT(gXfermodeCoeff2Blend));
bsalomon@google.com080773c2011-03-15 19:09:25 +0000114}
115
reed@google.comac10a2d2010-12-22 21:39:39 +0000116///////////////////////////////////////////////////////////////////////////////
117
rileya@google.come38160c2012-07-03 18:03:04 +0000118static bool gPrintStartupSpew;
bsalomon@google.com42ab7ea2011-01-19 17:19:40 +0000119
bsalomon861e1032014-12-16 07:33:49 -0800120GrGLGpu::GrGLGpu(const GrGLContext& ctx, GrContext* context)
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000121 : GrGpu(context)
robertphillips@google.com6177e692013-02-28 20:16:25 +0000122 , fGLContext(ctx) {
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000123
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000124 SkASSERT(ctx.isInitialized());
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +0000125 fCaps.reset(SkRef(ctx.caps()));
bsalomon@google.combcce8922013-03-25 15:38:39 +0000126
bsalomon1c63bf62014-07-22 13:09:46 -0700127 fHWBoundTextureUniqueIDs.reset(this->glCaps().maxFragmentTextureUnits());
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +0000128
robertphillips@google.com6177e692013-02-28 20:16:25 +0000129 GrGLClearErr(fGLContext.interface());
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000130 if (gPrintStartupSpew) {
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000131 const GrGLubyte* vendor;
132 const GrGLubyte* renderer;
133 const GrGLubyte* version;
134 GL_CALL_RET(vendor, GetString(GR_GL_VENDOR));
135 GL_CALL_RET(renderer, GetString(GR_GL_RENDERER));
136 GL_CALL_RET(version, GetString(GR_GL_VERSION));
bsalomon861e1032014-12-16 07:33:49 -0800137 SkDebugf("------------------------- create GrGLGpu %p --------------\n",
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000138 this);
tfarina38406c82014-10-31 07:11:12 -0700139 SkDebugf("------ VENDOR %s\n", vendor);
140 SkDebugf("------ RENDERER %s\n", renderer);
141 SkDebugf("------ VERSION %s\n", version);
142 SkDebugf("------ EXTENSIONS\n");
bsalomone904c092014-07-17 10:50:59 -0700143 ctx.extensions().print();
tfarina38406c82014-10-31 07:11:12 -0700144 SkDebugf("\n");
kkinnunen297aaf92015-02-19 06:32:12 -0800145 SkDebugf("%s", this->glCaps().dump().c_str());
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000146 }
147
commit-bot@chromium.org9188a152013-09-05 18:28:24 +0000148 fProgramCache = SkNEW_ARGS(ProgramCache, (this));
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000149
joshualitt2dd1ae02014-12-03 06:24:10 -0800150 SkASSERT(this->glCaps().maxVertexAttributes() >= GrGeometryProcessor::kMaxVertexAttribs);
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000151
bsalomon@google.comfe676522011-06-17 18:12:21 +0000152 fLastSuccessfulStencilFmtIdx = 0;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000153 fHWProgramID = 0;
cdaltonc7103a12014-08-11 14:05:05 -0700154
155 if (this->glCaps().pathRenderingSupport()) {
kkinnunen5b653572014-08-20 04:13:27 -0700156 fPathRendering.reset(new GrGLPathRendering(this));
cdaltonc7103a12014-08-11 14:05:05 -0700157 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000158}
159
bsalomon861e1032014-12-16 07:33:49 -0800160GrGLGpu::~GrGLGpu() {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000161 if (0 != fHWProgramID) {
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000162 // detach the current program so there is no confusion on OpenGL's part
163 // that we want it to be deleted
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000164 SkASSERT(fHWProgramID == fCurrentProgram->programID());
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000165 GL_CALL(UseProgram(0));
166 }
167
bsalomon160f24c2015-03-17 15:55:42 -0700168 if (fTempSrcFBO) {
169 fTempSrcFBO->release(this->glInterface());
170 fTempSrcFBO.reset(NULL);
egdaniel0f5f9672015-02-03 11:10:51 -0800171 }
bsalomon160f24c2015-03-17 15:55:42 -0700172 if (fTempDstFBO) {
173 fTempDstFBO->release(this->glInterface());
174 fTempDstFBO.reset(NULL);
egdaniel0f5f9672015-02-03 11:10:51 -0800175 }
bsalomon160f24c2015-03-17 15:55:42 -0700176 if (fStencilClearFBO) {
177 fStencilClearFBO->release(this->glInterface());
178 fStencilClearFBO.reset(NULL);
bsalomondd3143b2015-02-23 09:27:45 -0800179 }
egdaniel0f5f9672015-02-03 11:10:51 -0800180
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000181 delete fProgramCache;
bsalomonc8dc1f72014-08-21 13:02:13 -0700182}
183
bsalomon861e1032014-12-16 07:33:49 -0800184void GrGLGpu::contextAbandoned() {
robertphillipse3371302014-09-17 06:01:06 -0700185 INHERITED::contextAbandoned();
bsalomonc8dc1f72014-08-21 13:02:13 -0700186 fProgramCache->abandon();
187 fHWProgramID = 0;
bsalomon160f24c2015-03-17 15:55:42 -0700188 if (fTempSrcFBO) {
189 fTempSrcFBO->abandon();
190 fTempSrcFBO.reset(NULL);
191 }
192 if (fTempDstFBO) {
193 fTempDstFBO->abandon();
194 fTempDstFBO.reset(NULL);
195 }
196 if (fStencilClearFBO) {
197 fStencilClearFBO->abandon();
198 fStencilClearFBO.reset(NULL);
199 }
200
bsalomonc8dc1f72014-08-21 13:02:13 -0700201 if (this->glCaps().pathRenderingSupport()) {
202 this->glPathRendering()->abandonGpuResources();
203 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000204}
205
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000206///////////////////////////////////////////////////////////////////////////////
bsalomon861e1032014-12-16 07:33:49 -0800207GrPixelConfig GrGLGpu::preferredReadPixelsConfig(GrPixelConfig readConfig,
commit-bot@chromium.org28621512013-08-07 19:43:45 +0000208 GrPixelConfig surfaceConfig) const {
209 if (GR_GL_RGBA_8888_PIXEL_OPS_SLOW && kRGBA_8888_GrPixelConfig == readConfig) {
bsalomon@google.com9c680582013-02-06 18:17:50 +0000210 return kBGRA_8888_GrPixelConfig;
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +0000211 } else if (this->glContext().isMesa() &&
commit-bot@chromium.org28621512013-08-07 19:43:45 +0000212 GrBytesPerPixel(readConfig) == 4 &&
213 GrPixelConfigSwapRAndB(readConfig) == surfaceConfig) {
commit-bot@chromium.org5d1d79a2013-05-24 18:52:52 +0000214 // Mesa 3D takes a slow path on when reading back BGRA from an RGBA surface and vice-versa.
215 // Perhaps this should be guarded by some compiletime or runtime check.
216 return surfaceConfig;
piotaixre4b23142014-10-02 10:57:53 -0700217 } else if (readConfig == kBGRA_8888_GrPixelConfig
218 && !this->glCaps().readPixelsSupported(
219 this->glInterface(),
220 GR_GL_BGRA,
221 GR_GL_UNSIGNED_BYTE,
222 surfaceConfig
223 )) {
commit-bot@chromium.org28621512013-08-07 19:43:45 +0000224 return kRGBA_8888_GrPixelConfig;
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000225 } else {
commit-bot@chromium.org28621512013-08-07 19:43:45 +0000226 return readConfig;
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000227 }
228}
229
bsalomon861e1032014-12-16 07:33:49 -0800230GrPixelConfig GrGLGpu::preferredWritePixelsConfig(GrPixelConfig writeConfig,
commit-bot@chromium.org5d1d79a2013-05-24 18:52:52 +0000231 GrPixelConfig surfaceConfig) const {
commit-bot@chromium.org28621512013-08-07 19:43:45 +0000232 if (GR_GL_RGBA_8888_PIXEL_OPS_SLOW && kRGBA_8888_GrPixelConfig == writeConfig) {
233 return kBGRA_8888_GrPixelConfig;
234 } else {
235 return writeConfig;
236 }
bsalomon@google.com9c680582013-02-06 18:17:50 +0000237}
238
bsalomon861e1032014-12-16 07:33:49 -0800239bool GrGLGpu::canWriteTexturePixels(const GrTexture* texture, GrPixelConfig srcConfig) const {
krajcevski145d48c2014-06-11 16:07:50 -0700240 if (kIndex_8_GrPixelConfig == srcConfig || kIndex_8_GrPixelConfig == texture->config()) {
bsalomon@google.com9c680582013-02-06 18:17:50 +0000241 return false;
242 }
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +0000243 if (srcConfig != texture->config() && kGLES_GrGLStandard == this->glStandard()) {
bsalomon@google.com9c680582013-02-06 18:17:50 +0000244 // In general ES2 requires the internal format of the texture and the format of the src
245 // pixels to match. However, It may or may not be possible to upload BGRA data to a RGBA
246 // texture. It depends upon which extension added BGRA. The Apple extension allows it
247 // (BGRA's internal format is RGBA) while the EXT extension does not (BGRA is its own
248 // internal format).
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +0000249 if (this->glCaps().isConfigTexturable(kBGRA_8888_GrPixelConfig) &&
bsalomon@google.com9c680582013-02-06 18:17:50 +0000250 !this->glCaps().bgraIsInternalFormat() &&
251 kBGRA_8888_GrPixelConfig == srcConfig &&
252 kRGBA_8888_GrPixelConfig == texture->config()) {
253 return true;
254 } else {
255 return false;
256 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000257 } else {
bsalomon@google.com9c680582013-02-06 18:17:50 +0000258 return true;
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000259 }
260}
261
bsalomon861e1032014-12-16 07:33:49 -0800262bool GrGLGpu::fullReadPixelsIsFasterThanPartial() const {
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000263 return SkToBool(GR_GL_FULL_READPIXELS_FASTER_THAN_PARTIAL);
264}
265
bsalomon861e1032014-12-16 07:33:49 -0800266void GrGLGpu::onResetContext(uint32_t resetBits) {
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000267 // we don't use the zb at all
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000268 if (resetBits & kMisc_GrGLBackendState) {
269 GL_CALL(Disable(GR_GL_DEPTH_TEST));
270 GL_CALL(DepthMask(GR_GL_FALSE));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000271
egdaniel8dd688b2015-01-22 10:16:09 -0800272 fHWDrawFace = GrPipelineBuilder::kInvalid_DrawFace;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000273 fHWDitherEnabled = kUnknown_TriState;
reed@google.comac10a2d2010-12-22 21:39:39 +0000274
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +0000275 if (kGL_GrGLStandard == this->glStandard()) {
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000276 // Desktop-only state that we never change
277 if (!this->glCaps().isCoreProfile()) {
278 GL_CALL(Disable(GR_GL_POINT_SMOOTH));
279 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
280 GL_CALL(Disable(GR_GL_POLYGON_SMOOTH));
281 GL_CALL(Disable(GR_GL_POLYGON_STIPPLE));
282 GL_CALL(Disable(GR_GL_COLOR_LOGIC_OP));
283 GL_CALL(Disable(GR_GL_INDEX_LOGIC_OP));
284 }
285 // The windows NVIDIA driver has GL_ARB_imaging in the extension string when using a
286 // core profile. This seems like a bug since the core spec removes any mention of
287 // GL_ARB_imaging.
288 if (this->glCaps().imagingSupport() && !this->glCaps().isCoreProfile()) {
289 GL_CALL(Disable(GR_GL_COLOR_TABLE));
290 }
291 GL_CALL(Disable(GR_GL_POLYGON_OFFSET_FILL));
292 // Since ES doesn't support glPointSize at all we always use the VS to
293 // set the point size
294 GL_CALL(Enable(GR_GL_VERTEX_PROGRAM_POINT_SIZE));
295
296 // We should set glPolygonMode(FRONT_AND_BACK,FILL) here, too. It isn't
297 // currently part of our gl interface. There are probably others as
298 // well.
bsalomon@google.com2b1b8c02013-02-28 22:06:02 +0000299 }
joshualitt58162332014-08-01 06:44:53 -0700300
301 if (kGLES_GrGLStandard == this->glStandard() &&
302 fGLContext.hasExtension("GL_ARM_shader_framebuffer_fetch")) {
303 // The arm extension requires specifically enabling MSAA fetching per sample.
304 // On some devices this may have a perf hit. Also multiple render targets are disabled
305 GL_CALL(Enable(GR_GL_FETCH_PER_SAMPLE_ARM));
306 }
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000307 fHWWriteToColor = kUnknown_TriState;
308 // we only ever use lines in hairline mode
309 GL_CALL(LineWidth(1));
bsalomon@google.comcad107b2013-06-28 14:32:08 +0000310 }
edisonn@google.comba669992013-06-28 16:03:21 +0000311
egdanielb414f252014-07-29 13:15:47 -0700312 if (resetBits & kMSAAEnable_GrGLBackendState) {
313 fMSAAEnabled = kUnknown_TriState;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000314 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000315
commit-bot@chromium.org46fbfe02013-08-30 15:52:12 +0000316 fHWActiveTextureUnitIdx = -1; // invalid
317
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000318 if (resetBits & kTextureBinding_GrGLBackendState) {
bsalomon1c63bf62014-07-22 13:09:46 -0700319 for (int s = 0; s < fHWBoundTextureUniqueIDs.count(); ++s) {
320 fHWBoundTextureUniqueIDs[s] = SK_InvalidUniqueID;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000321 }
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000322 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000323
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000324 if (resetBits & kBlend_GrGLBackendState) {
325 fHWBlendState.invalidate();
326 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000327
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000328 if (resetBits & kView_GrGLBackendState) {
329 fHWScissorSettings.invalidate();
330 fHWViewport.invalidate();
331 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000332
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000333 if (resetBits & kStencil_GrGLBackendState) {
334 fHWStencilSettings.invalidate();
335 fHWStencilTestEnabled = kUnknown_TriState;
336 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000337
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000338 // Vertex
339 if (resetBits & kVertex_GrGLBackendState) {
340 fHWGeometryState.invalidate();
341 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000342
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000343 if (resetBits & kRenderTarget_GrGLBackendState) {
bsalomon160f24c2015-03-17 15:55:42 -0700344 for (size_t i = 0; i < SK_ARRAY_COUNT(fHWFBOBinding); ++i) {
345 fHWFBOBinding[i].invalidate();
346 }
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000347 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000348
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000349 if (resetBits & kPathRendering_GrGLBackendState) {
350 if (this->caps()->pathRenderingSupport()) {
kkinnunenccdaa042014-08-20 01:36:23 -0700351 this->glPathRendering()->resetContext();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000352 }
bsalomon@google.com05a718c2012-06-29 14:01:53 +0000353 }
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000354
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000355 // we assume these values
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000356 if (resetBits & kPixelStore_GrGLBackendState) {
357 if (this->glCaps().unpackRowLengthSupport()) {
358 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
359 }
360 if (this->glCaps().packRowLengthSupport()) {
361 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
362 }
363 if (this->glCaps().unpackFlipYSupport()) {
364 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
365 }
366 if (this->glCaps().packFlipYSupport()) {
367 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, GR_GL_FALSE));
368 }
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000369 }
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000370
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000371 if (resetBits & kProgram_GrGLBackendState) {
372 fHWProgramID = 0;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000373 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000374}
375
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000376namespace {
377
378GrSurfaceOrigin resolve_origin(GrSurfaceOrigin origin, bool renderTarget) {
379 // By default, GrRenderTargets are GL's normal orientation so that they
380 // can be drawn to by the outside world without the client having
381 // to render upside down.
382 if (kDefault_GrSurfaceOrigin == origin) {
383 return renderTarget ? kBottomLeft_GrSurfaceOrigin : kTopLeft_GrSurfaceOrigin;
384 } else {
385 return origin;
386 }
387}
388
389}
390
bsalomon861e1032014-12-16 07:33:49 -0800391GrTexture* GrGLGpu::onWrapBackendTexture(const GrBackendTextureDesc& desc) {
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000392 if (!this->configToGLFormats(desc.fConfig, false, NULL, NULL, NULL)) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000393 return NULL;
394 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000395
robertphillips@google.comb72e5d32012-10-30 15:18:10 +0000396 if (0 == desc.fTextureHandle) {
397 return NULL;
398 }
399
bsalomon@google.combcce8922013-03-25 15:38:39 +0000400 int maxSize = this->caps()->maxTextureSize();
robertphillips@google.comb72e5d32012-10-30 15:18:10 +0000401 if (desc.fWidth > maxSize || desc.fHeight > maxSize) {
402 return NULL;
403 }
404
bsalomonb15b4c12014-10-29 12:41:57 -0700405 GrGLTexture::IDDesc idDesc;
406 GrSurfaceDesc surfDesc;
407
408 idDesc.fTextureID = static_cast<GrGLuint>(desc.fTextureHandle);
bsalomon5236cf42015-01-14 10:42:08 -0800409 idDesc.fLifeCycle = GrGpuResource::kWrapped_LifeCycle;
bsalomonb15b4c12014-10-29 12:41:57 -0700410
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000411 // next line relies on GrBackendTextureDesc's flags matching GrTexture's
bsalomonb15b4c12014-10-29 12:41:57 -0700412 surfDesc.fFlags = (GrSurfaceFlags) desc.fFlags;
413 surfDesc.fWidth = desc.fWidth;
414 surfDesc.fHeight = desc.fHeight;
415 surfDesc.fConfig = desc.fConfig;
416 surfDesc.fSampleCnt = desc.fSampleCnt;
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000417 bool renderTarget = SkToBool(desc.fFlags & kRenderTarget_GrBackendTextureFlag);
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000418 // FIXME: this should be calling resolve_origin(), but Chrome code is currently
419 // assuming the old behaviour, which is that backend textures are always
420 // BottomLeft, even for non-RT's. Once Chrome is fixed, change this to:
421 // glTexDesc.fOrigin = resolve_origin(desc.fOrigin, renderTarget);
422 if (kDefault_GrSurfaceOrigin == desc.fOrigin) {
bsalomonb15b4c12014-10-29 12:41:57 -0700423 surfDesc.fOrigin = kBottomLeft_GrSurfaceOrigin;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000424 } else {
bsalomonb15b4c12014-10-29 12:41:57 -0700425 surfDesc.fOrigin = desc.fOrigin;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000426 }
bsalomon@google.come269f212011-11-07 13:29:52 +0000427
428 GrGLTexture* texture = NULL;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000429 if (renderTarget) {
bsalomonb15b4c12014-10-29 12:41:57 -0700430 GrGLRenderTarget::IDDesc rtIDDesc;
bsalomon5236cf42015-01-14 10:42:08 -0800431 if (!this->createRenderTargetObjects(surfDesc, false, idDesc.fTextureID, &rtIDDesc)) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000432 return NULL;
433 }
bsalomon37dd3312014-11-03 08:47:23 -0800434 texture = SkNEW_ARGS(GrGLTextureRenderTarget, (this, surfDesc, idDesc, rtIDDesc));
bsalomon@google.come269f212011-11-07 13:29:52 +0000435 } else {
bsalomonb15b4c12014-10-29 12:41:57 -0700436 texture = SkNEW_ARGS(GrGLTexture, (this, surfDesc, idDesc));
bsalomon@google.come269f212011-11-07 13:29:52 +0000437 }
438 if (NULL == texture) {
439 return NULL;
440 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000441
bsalomon@google.come269f212011-11-07 13:29:52 +0000442 return texture;
443}
444
bsalomon861e1032014-12-16 07:33:49 -0800445GrRenderTarget* GrGLGpu::onWrapBackendRenderTarget(const GrBackendRenderTargetDesc& wrapDesc) {
bsalomonb15b4c12014-10-29 12:41:57 -0700446 GrGLRenderTarget::IDDesc idDesc;
bsalomon160f24c2015-03-17 15:55:42 -0700447 GrGLuint fboID = static_cast<GrGLuint>(wrapDesc.fRenderTargetHandle);
448 idDesc.fRenderFBO.reset(SkNEW_ARGS(GrGLFBO, (fboID)));
bsalomonb15b4c12014-10-29 12:41:57 -0700449 idDesc.fMSColorRenderbufferID = 0;
bsalomon5236cf42015-01-14 10:42:08 -0800450 idDesc.fLifeCycle = GrGpuResource::kWrapped_LifeCycle;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000451
bsalomonb15b4c12014-10-29 12:41:57 -0700452 GrSurfaceDesc desc;
453 desc.fConfig = wrapDesc.fConfig;
454 desc.fFlags = kCheckAllocation_GrSurfaceFlag;
455 desc.fWidth = wrapDesc.fWidth;
456 desc.fHeight = wrapDesc.fHeight;
457 desc.fSampleCnt = wrapDesc.fSampleCnt;
458 desc.fOrigin = resolve_origin(wrapDesc.fOrigin, true);
459
bsalomon37dd3312014-11-03 08:47:23 -0800460 GrRenderTarget* tgt = SkNEW_ARGS(GrGLRenderTarget, (this, desc, idDesc));
bsalomonb15b4c12014-10-29 12:41:57 -0700461 if (wrapDesc.fStencilBits) {
kkinnunen36c57df2015-01-27 00:30:18 -0800462 GrGLStencilBuffer::IDDesc sbDesc;
bsalomon@google.come269f212011-11-07 13:29:52 +0000463 GrGLStencilBuffer::Format format;
464 format.fInternalFormat = GrGLStencilBuffer::kUnknownInternalFormat;
465 format.fPacked = false;
bsalomonb15b4c12014-10-29 12:41:57 -0700466 format.fStencilBits = wrapDesc.fStencilBits;
467 format.fTotalBits = wrapDesc.fStencilBits;
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000468 GrGLStencilBuffer* sb = SkNEW_ARGS(GrGLStencilBuffer,
469 (this,
kkinnunen36c57df2015-01-27 00:30:18 -0800470 sbDesc,
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000471 desc.fWidth,
472 desc.fHeight,
473 desc.fSampleCnt,
474 format));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800475 tgt->renderTargetPriv().didAttachStencilBuffer(sb);
bsalomon@google.come269f212011-11-07 13:29:52 +0000476 sb->unref();
477 }
478 return tgt;
479}
480
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000481////////////////////////////////////////////////////////////////////////////////
482
bsalomon861e1032014-12-16 07:33:49 -0800483bool GrGLGpu::onWriteTexturePixels(GrTexture* texture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000484 int left, int top, int width, int height,
485 GrPixelConfig config, const void* buffer,
486 size_t rowBytes) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000487 if (NULL == buffer) {
bsalomon@google.com9c680582013-02-06 18:17:50 +0000488 return false;
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000489 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000490 GrGLTexture* glTex = static_cast<GrGLTexture*>(texture);
491
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +0000492 this->setScratchTextureUnit();
bsalomon@google.com6f379512011-11-16 20:36:03 +0000493 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTex->textureID()));
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000494
krajcevski145d48c2014-06-11 16:07:50 -0700495 bool success = false;
bsalomonb15b4c12014-10-29 12:41:57 -0700496 if (GrPixelConfigIsCompressed(glTex->desc().fConfig)) {
bsalomon861e1032014-12-16 07:33:49 -0800497 // We check that config == desc.fConfig in GrGLGpu::canWriteTexturePixels()
bsalomonb15b4c12014-10-29 12:41:57 -0700498 SkASSERT(config == glTex->desc().fConfig);
499 success = this->uploadCompressedTexData(glTex->desc(), buffer, false, left, top, width,
500 height);
krajcevski145d48c2014-06-11 16:07:50 -0700501 } else {
bsalomonb15b4c12014-10-29 12:41:57 -0700502 success = this->uploadTexData(glTex->desc(), false, left, top, width, height, config,
503 buffer, rowBytes);
krajcevski145d48c2014-06-11 16:07:50 -0700504 }
505
506 if (success) {
bsalomonafbf2d62014-09-30 12:18:44 -0700507 texture->texturePriv().dirtyMipMaps(true);
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +0000508 return true;
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +0000509 }
krajcevski145d48c2014-06-11 16:07:50 -0700510
511 return false;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000512}
513
bsalomonb15b4c12014-10-29 12:41:57 -0700514static bool adjust_pixel_ops_params(int surfaceWidth,
515 int surfaceHeight,
516 size_t bpp,
517 int* left, int* top, int* width, int* height,
518 const void** data,
519 size_t* rowBytes) {
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000520 if (!*rowBytes) {
521 *rowBytes = *width * bpp;
522 }
523
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000524 SkIRect subRect = SkIRect::MakeXYWH(*left, *top, *width, *height);
525 SkIRect bounds = SkIRect::MakeWH(surfaceWidth, surfaceHeight);
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000526
527 if (!subRect.intersect(bounds)) {
528 return false;
529 }
530 *data = reinterpret_cast<const void*>(reinterpret_cast<intptr_t>(*data) +
531 (subRect.fTop - *top) * *rowBytes + (subRect.fLeft - *left) * bpp);
532
533 *left = subRect.fLeft;
534 *top = subRect.fTop;
535 *width = subRect.width();
536 *height = subRect.height();
537 return true;
538}
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000539
bsalomonb15b4c12014-10-29 12:41:57 -0700540static inline GrGLenum check_alloc_error(const GrSurfaceDesc& desc,
541 const GrGLInterface* interface) {
bsalomonf2703d82014-10-28 14:33:06 -0700542 if (SkToBool(desc.fFlags & kCheckAllocation_GrSurfaceFlag)) {
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000543 return GR_GL_GET_ERROR(interface);
544 } else {
545 return CHECK_ALLOC_ERROR(interface);
546 }
547}
548
bsalomon861e1032014-12-16 07:33:49 -0800549bool GrGLGpu::uploadTexData(const GrSurfaceDesc& desc,
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000550 bool isNewTexture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000551 int left, int top, int width, int height,
552 GrPixelConfig dataConfig,
553 const void* data,
554 size_t rowBytes) {
bsalomon49f085d2014-09-05 13:34:00 -0700555 SkASSERT(data || isNewTexture);
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000556
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +0000557 // If we're uploading compressed data then we should be using uploadCompressedTexData
558 SkASSERT(!GrPixelConfigIsCompressed(dataConfig));
559
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000560 size_t bpp = GrBytesPerPixel(dataConfig);
561 if (!adjust_pixel_ops_params(desc.fWidth, desc.fHeight, bpp, &left, &top,
562 &width, &height, &data, &rowBytes)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000563 return false;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000564 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000565 size_t trimRowBytes = width * bpp;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000566
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000567 // in case we need a temporary, trimmed copy of the src pixels
georgeb62508b2014-08-12 18:00:47 -0700568 GrAutoMalloc<128 * 128> tempStorage;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000569
commit-bot@chromium.orgcea9abb2013-12-09 19:15:37 +0000570 // We currently lazily create MIPMAPs when the we see a draw with
571 // GrTextureParams::kMipMap_FilterMode. Using texture storage requires that the
572 // MIP levels are all created when the texture is created. So for now we don't use
573 // texture storage.
574 bool useTexStorage = false &&
575 isNewTexture &&
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000576 this->glCaps().texStorageSupport();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000577
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +0000578 if (useTexStorage && kGL_GrGLStandard == this->glStandard()) {
bsalomon@google.com313f0192012-07-10 17:21:02 +0000579 // 565 is not a sized internal format on desktop GL. So on desktop with
580 // 565 we always use an unsized internal format to let the system pick
581 // the best sized format to convert the 565 data to. Since TexStorage
582 // only allows sized internal formats we will instead use TexImage2D.
583 useTexStorage = desc.fConfig != kRGB_565_GrPixelConfig;
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000584 }
585
jvanverth3f801cb2014-12-16 09:49:38 -0800586 GrGLenum internalFormat = 0x0; // suppress warning
587 GrGLenum externalFormat = 0x0; // suppress warning
588 GrGLenum externalType = 0x0; // suppress warning
bsalomone904c092014-07-17 10:50:59 -0700589
jvanverthe1869ca2014-12-16 13:32:27 -0800590 // glTexStorage requires sized internal formats on both desktop and ES. ES2 requires an unsized
jvanverthf72b7522014-12-17 10:46:01 -0800591 // format for glTexImage, unlike ES3 and desktop.
jvanverthe1869ca2014-12-16 13:32:27 -0800592 bool useSizedFormat = useTexStorage;
jvanverthf72b7522014-12-17 10:46:01 -0800593 if (kGL_GrGLStandard == this->glStandard() ||
594 (this->glVersion() >= GR_GL_VER(3, 0) &&
595 // ES3 only works with sized BGRA8 format if "GL_APPLE_texture_format_BGRA8888" enabled
596 (kBGRA_8888_GrPixelConfig != dataConfig || !this->glCaps().bgraIsInternalFormat()))) {
jvanverthe1869ca2014-12-16 13:32:27 -0800597 useSizedFormat = true;
598 }
jvanverthf72b7522014-12-17 10:46:01 -0800599
bsalomone904c092014-07-17 10:50:59 -0700600 if (!this->configToGLFormats(dataConfig, useSizedFormat, &internalFormat,
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000601 &externalFormat, &externalType)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000602 return false;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000603 }
604
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000605 /*
bsalomon@google.com6f379512011-11-16 20:36:03 +0000606 * check whether to allocate a temporary buffer for flipping y or
607 * because our srcData has extra bytes past each row. If so, we need
608 * to trim those off here, since GL ES may not let us specify
609 * GL_UNPACK_ROW_LENGTH.
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000610 */
bsalomon@google.com6f379512011-11-16 20:36:03 +0000611 bool restoreGLRowLength = false;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000612 bool swFlipY = false;
613 bool glFlipY = false;
bsalomon49f085d2014-09-05 13:34:00 -0700614 if (data) {
senorblanco@chromium.orgef5dbe12013-01-28 16:42:38 +0000615 if (kBottomLeft_GrSurfaceOrigin == desc.fOrigin) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000616 if (this->glCaps().unpackFlipYSupport()) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000617 glFlipY = true;
618 } else {
619 swFlipY = true;
620 }
621 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000622 if (this->glCaps().unpackRowLengthSupport() && !swFlipY) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000623 // can't use this for flipping, only non-neg values allowed. :(
624 if (rowBytes != trimRowBytes) {
625 GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
626 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength));
627 restoreGLRowLength = true;
628 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000629 } else {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000630 if (trimRowBytes != rowBytes || swFlipY) {
631 // copy data into our new storage, skipping the trailing bytes
632 size_t trimSize = height * trimRowBytes;
633 const char* src = (const char*)data;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000634 if (swFlipY) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000635 src += (height - 1) * rowBytes;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000636 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000637 char* dst = (char*)tempStorage.reset(trimSize);
638 for (int y = 0; y < height; y++) {
639 memcpy(dst, src, trimRowBytes);
640 if (swFlipY) {
641 src -= rowBytes;
642 } else {
643 src += rowBytes;
644 }
645 dst += trimRowBytes;
646 }
647 // now point data to our copied version
648 data = tempStorage.get();
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000649 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000650 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000651 if (glFlipY) {
652 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_TRUE));
653 }
joshualittee5da552014-07-16 13:32:56 -0700654 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT,
655 static_cast<GrGLint>(GrUnpackAlignment(dataConfig))));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000656 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000657 bool succeeded = true;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000658 if (isNewTexture &&
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000659 0 == left && 0 == top &&
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000660 desc.fWidth == width && desc.fHeight == height) {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000661 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000662 if (useTexStorage) {
commit-bot@chromium.orgcea9abb2013-12-09 19:15:37 +0000663 // We never resize or change formats of textures.
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000664 GL_ALLOC_CALL(this->glInterface(),
665 TexStorage2D(GR_GL_TEXTURE_2D,
666 1, // levels
667 internalFormat,
668 desc.fWidth, desc.fHeight));
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000669 } else {
bsalomond4cb9222014-08-11 14:19:09 -0700670 GL_ALLOC_CALL(this->glInterface(),
671 TexImage2D(GR_GL_TEXTURE_2D,
672 0, // level
673 internalFormat,
674 desc.fWidth, desc.fHeight,
675 0, // border
676 externalFormat, externalType,
677 data));
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000678 }
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000679 GrGLenum error = check_alloc_error(desc, this->glInterface());
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000680 if (error != GR_GL_NO_ERROR) {
681 succeeded = false;
682 } else {
683 // if we have data and we used TexStorage to create the texture, we
684 // now upload with TexSubImage.
bsalomon49f085d2014-09-05 13:34:00 -0700685 if (data && useTexStorage) {
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000686 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D,
687 0, // level
688 left, top,
689 width, height,
690 externalFormat, externalType,
691 data));
692 }
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000693 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000694 } else {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000695 if (swFlipY || glFlipY) {
bsalomon@google.com6f379512011-11-16 20:36:03 +0000696 top = desc.fHeight - (top + height);
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000697 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000698 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D,
699 0, // level
700 left, top,
701 width, height,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000702 externalFormat, externalType, data));
703 }
704
705 if (restoreGLRowLength) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000706 SkASSERT(this->glCaps().unpackRowLengthSupport());
bsalomon@google.com6f379512011-11-16 20:36:03 +0000707 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000708 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000709 if (glFlipY) {
710 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
711 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000712 return succeeded;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000713}
714
krajcevski145d48c2014-06-11 16:07:50 -0700715// TODO: This function is using a lot of wonky semantics like, if width == -1
piotaixre4b23142014-10-02 10:57:53 -0700716// then set width = desc.fWdith ... blah. A better way to do it might be to
krajcevski145d48c2014-06-11 16:07:50 -0700717// create a CompressedTexData struct that takes a desc/ptr and figures out
718// the proper upload semantics. Then users can construct this function how they
719// see fit if they want to go against the "standard" way to do it.
bsalomon861e1032014-12-16 07:33:49 -0800720bool GrGLGpu::uploadCompressedTexData(const GrSurfaceDesc& desc,
krajcevski145d48c2014-06-11 16:07:50 -0700721 const void* data,
722 bool isNewTexture,
723 int left, int top, int width, int height) {
bsalomon49f085d2014-09-05 13:34:00 -0700724 SkASSERT(data || isNewTexture);
krajcevski9c0e6292014-06-02 07:38:14 -0700725
726 // No support for software flip y, yet...
727 SkASSERT(kBottomLeft_GrSurfaceOrigin != desc.fOrigin);
728
krajcevski145d48c2014-06-11 16:07:50 -0700729 if (-1 == width) {
730 width = desc.fWidth;
731 }
732#ifdef SK_DEBUG
733 else {
734 SkASSERT(width <= desc.fWidth);
735 }
736#endif
737
738 if (-1 == height) {
739 height = desc.fHeight;
740 }
741#ifdef SK_DEBUG
742 else {
743 SkASSERT(height <= desc.fHeight);
744 }
745#endif
746
krajcevski9c0e6292014-06-02 07:38:14 -0700747 // Make sure that the width and height that we pass to OpenGL
748 // is a multiple of the block size.
bsalomon98806072014-12-12 15:11:17 -0800749 size_t dataSize = GrCompressedFormatDataSize(desc.fConfig, width, height);
krajcevski9c0e6292014-06-02 07:38:14 -0700750
751 // We only need the internal format for compressed 2D textures.
752 GrGLenum internalFormat = 0;
753 if (!this->configToGLFormats(desc.fConfig, false, &internalFormat, NULL, NULL)) {
754 return false;
755 }
756
krajcevski145d48c2014-06-11 16:07:50 -0700757 if (isNewTexture) {
bsalomond4cb9222014-08-11 14:19:09 -0700758 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
krajcevski145d48c2014-06-11 16:07:50 -0700759 GL_ALLOC_CALL(this->glInterface(),
760 CompressedTexImage2D(GR_GL_TEXTURE_2D,
761 0, // level
762 internalFormat,
763 width, height,
764 0, // border
bsalomon98806072014-12-12 15:11:17 -0800765 SkToInt(dataSize),
krajcevski145d48c2014-06-11 16:07:50 -0700766 data));
bsalomond4cb9222014-08-11 14:19:09 -0700767 GrGLenum error = check_alloc_error(desc, this->glInterface());
768 if (error != GR_GL_NO_ERROR) {
769 return false;
770 }
krajcevski145d48c2014-06-11 16:07:50 -0700771 } else {
bsalomond4cb9222014-08-11 14:19:09 -0700772 // Paletted textures can't be updated.
773 if (GR_GL_PALETTE8_RGBA8 == internalFormat) {
774 return false;
775 }
776 GL_CALL(CompressedTexSubImage2D(GR_GL_TEXTURE_2D,
777 0, // level
778 left, top,
779 width, height,
780 internalFormat,
bsalomon98806072014-12-12 15:11:17 -0800781 SkToInt(dataSize),
bsalomond4cb9222014-08-11 14:19:09 -0700782 data));
krajcevski145d48c2014-06-11 16:07:50 -0700783 }
krajcevski9c0e6292014-06-02 07:38:14 -0700784
bsalomond4cb9222014-08-11 14:19:09 -0700785 return true;
krajcevski9c0e6292014-06-02 07:38:14 -0700786}
787
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +0000788static bool renderbuffer_storage_msaa(GrGLContext& ctx,
789 int sampleCount,
790 GrGLenum format,
791 int width, int height) {
robertphillips@google.com6177e692013-02-28 20:16:25 +0000792 CLEAR_ERROR_BEFORE_ALLOC(ctx.interface());
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +0000793 SkASSERT(GrGLCaps::kNone_MSFBOType != ctx.caps()->msFBOType());
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +0000794 switch (ctx.caps()->msFBOType()) {
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +0000795 case GrGLCaps::kDesktop_ARB_MSFBOType:
796 case GrGLCaps::kDesktop_EXT_MSFBOType:
797 case GrGLCaps::kES_3_0_MSFBOType:
798 GL_ALLOC_CALL(ctx.interface(),
799 RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
800 sampleCount,
801 format,
802 width, height));
803 break;
804 case GrGLCaps::kES_Apple_MSFBOType:
805 GL_ALLOC_CALL(ctx.interface(),
806 RenderbufferStorageMultisampleES2APPLE(GR_GL_RENDERBUFFER,
807 sampleCount,
808 format,
809 width, height));
810 break;
811 case GrGLCaps::kES_EXT_MsToTexture_MSFBOType:
812 case GrGLCaps::kES_IMG_MsToTexture_MSFBOType:
813 GL_ALLOC_CALL(ctx.interface(),
814 RenderbufferStorageMultisampleES2EXT(GR_GL_RENDERBUFFER,
815 sampleCount,
816 format,
817 width, height));
818 break;
819 case GrGLCaps::kNone_MSFBOType:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000820 SkFAIL("Shouldn't be here if we don't support multisampled renderbuffers.");
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +0000821 break;
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000822 }
Brian Salomon9251d4e2015-03-17 16:03:19 -0400823 return (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctx.interface()));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000824}
825
bsalomon5236cf42015-01-14 10:42:08 -0800826bool GrGLGpu::createRenderTargetObjects(const GrSurfaceDesc& desc, bool budgeted, GrGLuint texID,
bsalomonb15b4c12014-10-29 12:41:57 -0700827 GrGLRenderTarget::IDDesc* idDesc) {
828 idDesc->fMSColorRenderbufferID = 0;
bsalomon5236cf42015-01-14 10:42:08 -0800829 idDesc->fLifeCycle = budgeted ? GrGpuResource::kCached_LifeCycle :
830 GrGpuResource::kUncached_LifeCycle;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000831
832 GrGLenum status;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000833
bsalomon@google.comab15d612011-08-09 12:57:56 +0000834 GrGLenum msColorFormat = 0; // suppress warning
bsalomon160f24c2015-03-17 15:55:42 -0700835 GrGLenum fboTarget = 0; // suppress warning
bsalomon@google.comab15d612011-08-09 12:57:56 +0000836
bsalomonb15b4c12014-10-29 12:41:57 -0700837 if (desc.fSampleCnt > 0 && GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.com347c3822013-05-01 20:10:01 +0000838 goto FAILED;
839 }
840
bsalomon160f24c2015-03-17 15:55:42 -0700841 idDesc->fTextureFBO.reset(SkNEW_ARGS(GrGLFBO, (this->glInterface())));
842 if (!idDesc->fTextureFBO->isValid()) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000843 goto FAILED;
844 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000845
bsalomon@google.comf3a60c02013-03-19 19:06:09 +0000846 // If we are using multisampling we will create two FBOS. We render to one and then resolve to
847 // the texture bound to the other. The exception is the IMG multisample extension. With this
848 // extension the texture is multisampled when rendered to and then auto-resolves it when it is
849 // rendered from.
bsalomonb15b4c12014-10-29 12:41:57 -0700850 if (desc.fSampleCnt > 0 && this->glCaps().usesMSAARenderBuffers()) {
bsalomon160f24c2015-03-17 15:55:42 -0700851 idDesc->fRenderFBO.reset(SkNEW_ARGS(GrGLFBO, (this->glInterface())));
852 if (!idDesc->fRenderFBO->isValid()) {
853 goto FAILED;
854 }
bsalomonb15b4c12014-10-29 12:41:57 -0700855 GL_CALL(GenRenderbuffers(1, &idDesc->fMSColorRenderbufferID));
bsalomon160f24c2015-03-17 15:55:42 -0700856 if (!idDesc->fMSColorRenderbufferID ||
bsalomonb15b4c12014-10-29 12:41:57 -0700857 !this->configToGLFormats(desc.fConfig,
commit-bot@chromium.org87002952013-08-20 15:12:01 +0000858 // ES2 and ES3 require sized internal formats for rb storage.
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +0000859 kGLES_GrGLStandard == this->glStandard(),
bsalomon@google.com347c3822013-05-01 20:10:01 +0000860 &msColorFormat,
861 NULL,
862 NULL)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000863 goto FAILED;
864 }
865 } else {
bsalomon160f24c2015-03-17 15:55:42 -0700866 idDesc->fRenderFBO.reset(SkRef(idDesc->fTextureFBO.get()));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000867 }
868
bsalomon160f24c2015-03-17 15:55:42 -0700869 if (idDesc->fRenderFBO != idDesc->fTextureFBO) {
bsalomonb15b4c12014-10-29 12:41:57 -0700870 SkASSERT(desc.fSampleCnt > 0);
871 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, idDesc->fMSColorRenderbufferID));
robertphillips@google.com6177e692013-02-28 20:16:25 +0000872 if (!renderbuffer_storage_msaa(fGLContext,
bsalomonb15b4c12014-10-29 12:41:57 -0700873 desc.fSampleCnt,
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000874 msColorFormat,
bsalomonb15b4c12014-10-29 12:41:57 -0700875 desc.fWidth, desc.fHeight)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000876 goto FAILED;
877 }
bsalomon160f24c2015-03-17 15:55:42 -0700878 fboTarget = this->bindFBO(kChangeAttachments_FBOBinding, idDesc->fRenderFBO);
879 GL_CALL(FramebufferRenderbuffer(fboTarget,
880 GR_GL_COLOR_ATTACHMENT0,
881 GR_GL_RENDERBUFFER,
882 idDesc->fMSColorRenderbufferID));
bsalomonb15b4c12014-10-29 12:41:57 -0700883 if ((desc.fFlags & kCheckAllocation_GrSurfaceFlag) ||
884 !this->glCaps().isConfigVerifiedColorAttachment(desc.fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000885 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
886 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
887 goto FAILED;
888 }
bsalomonb15b4c12014-10-29 12:41:57 -0700889 fGLContext.caps()->markConfigAsValidColorAttachment(desc.fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000890 }
891 }
bsalomon160f24c2015-03-17 15:55:42 -0700892 fboTarget = this->bindFBO(kChangeAttachments_FBOBinding, idDesc->fTextureFBO);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000893
bsalomonb15b4c12014-10-29 12:41:57 -0700894 if (this->glCaps().usesImplicitMSAAResolve() && desc.fSampleCnt > 0) {
bsalomon160f24c2015-03-17 15:55:42 -0700895 GL_CALL(FramebufferTexture2DMultisample(fboTarget,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +0000896 GR_GL_COLOR_ATTACHMENT0,
897 GR_GL_TEXTURE_2D,
bsalomonb15b4c12014-10-29 12:41:57 -0700898 texID, 0, desc.fSampleCnt));
bsalomon@google.comf3a60c02013-03-19 19:06:09 +0000899 } else {
bsalomon160f24c2015-03-17 15:55:42 -0700900 GL_CALL(FramebufferTexture2D(fboTarget,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +0000901 GR_GL_COLOR_ATTACHMENT0,
902 GR_GL_TEXTURE_2D,
903 texID, 0));
904 }
bsalomonb15b4c12014-10-29 12:41:57 -0700905 if ((desc.fFlags & kCheckAllocation_GrSurfaceFlag) ||
906 !this->glCaps().isConfigVerifiedColorAttachment(desc.fConfig)) {
bsalomon160f24c2015-03-17 15:55:42 -0700907 GL_CALL_RET(status, CheckFramebufferStatus(fboTarget));
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000908 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
909 goto FAILED;
910 }
bsalomonb15b4c12014-10-29 12:41:57 -0700911 fGLContext.caps()->markConfigAsValidColorAttachment(desc.fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000912 }
913
914 return true;
915
916FAILED:
bsalomonb15b4c12014-10-29 12:41:57 -0700917 if (idDesc->fMSColorRenderbufferID) {
918 GL_CALL(DeleteRenderbuffers(1, &idDesc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000919 }
bsalomon160f24c2015-03-17 15:55:42 -0700920 if (idDesc->fRenderFBO) {
921 idDesc->fRenderFBO->release(this->glInterface());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000922 }
bsalomon160f24c2015-03-17 15:55:42 -0700923 if (idDesc->fTextureFBO) {
924 idDesc->fTextureFBO->release(this->glInterface());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000925 }
926 return false;
927}
928
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000929// good to set a break-point here to know when createTexture fails
930static GrTexture* return_null_texture() {
mtklein@google.com330313a2013-08-22 15:37:26 +0000931// SkDEBUGFAIL("null texture");
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000932 return NULL;
933}
934
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000935#if 0 && defined(SK_DEBUG)
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000936static size_t as_size_t(int x) {
937 return x;
938}
939#endif
940
bsalomon5236cf42015-01-14 10:42:08 -0800941GrTexture* GrGLGpu::onCreateTexture(const GrSurfaceDesc& origDesc, bool budgeted,
942 const void* srcData, size_t rowBytes) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000943
bsalomonb15b4c12014-10-29 12:41:57 -0700944 GrSurfaceDesc desc = origDesc;
reed@google.comac10a2d2010-12-22 21:39:39 +0000945
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000946 // Attempt to catch un- or wrongly initialized sample counts;
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000947 SkASSERT(desc.fSampleCnt >= 0 && desc.fSampleCnt <= 64);
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000948 // We fail if the MSAA was requested and is not available.
949 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType() && desc.fSampleCnt) {
tfarina38406c82014-10-31 07:11:12 -0700950 //SkDebugf("MSAA RT requested but not supported on this platform.");
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000951 return return_null_texture();
952 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000953
bsalomonf2703d82014-10-28 14:33:06 -0700954 bool renderTarget = SkToBool(desc.fFlags & kRenderTarget_GrSurfaceFlag);
reed@google.comac10a2d2010-12-22 21:39:39 +0000955
bsalomonb15b4c12014-10-29 12:41:57 -0700956 // If the sample count exceeds the max then we clamp it.
957 desc.fSampleCnt = SkTMin(desc.fSampleCnt, this->caps()->maxSampleCount());
958 desc.fOrigin = resolve_origin(desc.fOrigin, renderTarget);
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000959
bsalomonb15b4c12014-10-29 12:41:57 -0700960 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType() && desc.fSampleCnt) {
tfarina38406c82014-10-31 07:11:12 -0700961 //SkDebugf("MSAA RT requested but not supported on this platform.");
bsalomon@google.com945bbe12012-06-15 14:30:34 +0000962 return return_null_texture();
reed@google.comac10a2d2010-12-22 21:39:39 +0000963 }
964
reed@google.comac10a2d2010-12-22 21:39:39 +0000965 if (renderTarget) {
bsalomon@google.combcce8922013-03-25 15:38:39 +0000966 int maxRTSize = this->caps()->maxRenderTargetSize();
bsalomonb15b4c12014-10-29 12:41:57 -0700967 if (desc.fWidth > maxRTSize || desc.fHeight > maxRTSize) {
bsalomon@google.com91958362011-06-13 17:58:13 +0000968 return return_null_texture();
969 }
sugoi@google.com6ba0b0f2013-05-03 13:52:32 +0000970 } else {
971 int maxSize = this->caps()->maxTextureSize();
bsalomonb15b4c12014-10-29 12:41:57 -0700972 if (desc.fWidth > maxSize || desc.fHeight > maxSize) {
sugoi@google.com6ba0b0f2013-05-03 13:52:32 +0000973 return return_null_texture();
974 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000975 }
976
bsalomonb15b4c12014-10-29 12:41:57 -0700977 GrGLTexture::IDDesc idDesc;
978 GL_CALL(GenTextures(1, &idDesc.fTextureID));
bsalomon5236cf42015-01-14 10:42:08 -0800979 idDesc.fLifeCycle = budgeted ? GrGpuResource::kCached_LifeCycle :
980 GrGpuResource::kUncached_LifeCycle;
junov@chromium.org8b6b1c92013-08-29 16:22:09 +0000981
bsalomonb15b4c12014-10-29 12:41:57 -0700982 if (!idDesc.fTextureID) {
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000983 return return_null_texture();
984 }
985
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +0000986 this->setScratchTextureUnit();
bsalomonb15b4c12014-10-29 12:41:57 -0700987 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, idDesc.fTextureID));
bsalomon@google.come269f212011-11-07 13:29:52 +0000988
junov@chromium.org8b6b1c92013-08-29 16:22:09 +0000989 if (renderTarget && this->glCaps().textureUsageSupport()) {
990 // provides a hint about how this texture will be used
991 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
992 GR_GL_TEXTURE_USAGE,
993 GR_GL_FRAMEBUFFER_ATTACHMENT));
994 }
995
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000996 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
997 // drivers have a bug where an FBO won't be complete if it includes a
998 // texture that is not mipmap complete (considering the filter in use).
999 GrGLTexture::TexParams initialTexParams;
1000 // we only set a subset here so invalidate first
1001 initialTexParams.invalidate();
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00001002 initialTexParams.fMinFilter = GR_GL_NEAREST;
1003 initialTexParams.fMagFilter = GR_GL_NEAREST;
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001004 initialTexParams.fWrapS = GR_GL_CLAMP_TO_EDGE;
1005 initialTexParams.fWrapT = GR_GL_CLAMP_TO_EDGE;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001006 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1007 GR_GL_TEXTURE_MAG_FILTER,
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00001008 initialTexParams.fMagFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001009 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1010 GR_GL_TEXTURE_MIN_FILTER,
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00001011 initialTexParams.fMinFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001012 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1013 GR_GL_TEXTURE_WRAP_S,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001014 initialTexParams.fWrapS));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001015 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1016 GR_GL_TEXTURE_WRAP_T,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001017 initialTexParams.fWrapT));
bsalomonb15b4c12014-10-29 12:41:57 -07001018 if (!this->uploadTexData(desc, true, 0, 0,
1019 desc.fWidth, desc.fHeight,
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001020 desc.fConfig, srcData, rowBytes)) {
bsalomonb15b4c12014-10-29 12:41:57 -07001021 GL_CALL(DeleteTextures(1, &idDesc.fTextureID));
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001022 return return_null_texture();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001023 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001024
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001025 GrGLTexture* tex;
reed@google.comac10a2d2010-12-22 21:39:39 +00001026 if (renderTarget) {
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001027 // unbind the texture from the texture unit before binding it to the frame buffer
1028 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, 0));
bsalomon5236cf42015-01-14 10:42:08 -08001029 GrGLRenderTarget::IDDesc rtIDDesc;
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001030
bsalomon5236cf42015-01-14 10:42:08 -08001031 if (!this->createRenderTargetObjects(desc, budgeted, idDesc.fTextureID, &rtIDDesc)) {
bsalomonb15b4c12014-10-29 12:41:57 -07001032 GL_CALL(DeleteTextures(1, &idDesc.fTextureID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001033 return return_null_texture();
1034 }
bsalomon37dd3312014-11-03 08:47:23 -08001035 tex = SkNEW_ARGS(GrGLTextureRenderTarget, (this, desc, idDesc, rtIDDesc));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001036 } else {
bsalomonb15b4c12014-10-29 12:41:57 -07001037 tex = SkNEW_ARGS(GrGLTexture, (this, desc, idDesc));
reed@google.comac10a2d2010-12-22 21:39:39 +00001038 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001039 tex->setCachedTexParams(initialTexParams, this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00001040#ifdef TRACE_TEXTURE_CREATION
tfarina38406c82014-10-31 07:11:12 -07001041 SkDebugf("--- new texture [%d] size=(%d %d) config=%d\n",
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001042 glTexDesc.fTextureID, desc.fWidth, desc.fHeight, desc.fConfig);
reed@google.comac10a2d2010-12-22 21:39:39 +00001043#endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001044 return tex;
1045}
1046
bsalomon5236cf42015-01-14 10:42:08 -08001047GrTexture* GrGLGpu::onCreateCompressedTexture(const GrSurfaceDesc& origDesc, bool budgeted,
1048 const void* srcData) {
krajcevski9c0e6292014-06-02 07:38:14 -07001049
bsalomonb15b4c12014-10-29 12:41:57 -07001050 if(SkToBool(origDesc.fFlags & kRenderTarget_GrSurfaceFlag) || origDesc.fSampleCnt > 0) {
krajcevski9c0e6292014-06-02 07:38:14 -07001051 return return_null_texture();
1052 }
1053
1054 // Make sure that we're not flipping Y.
bsalomonb15b4c12014-10-29 12:41:57 -07001055 GrSurfaceOrigin texOrigin = resolve_origin(origDesc.fOrigin, false);
krajcevski9c0e6292014-06-02 07:38:14 -07001056 if (kBottomLeft_GrSurfaceOrigin == texOrigin) {
1057 return return_null_texture();
1058 }
bsalomonb15b4c12014-10-29 12:41:57 -07001059 GrSurfaceDesc desc = origDesc;
1060 desc.fOrigin = texOrigin;
krajcevski9c0e6292014-06-02 07:38:14 -07001061
1062 int maxSize = this->caps()->maxTextureSize();
bsalomonb15b4c12014-10-29 12:41:57 -07001063 if (desc.fWidth > maxSize || desc.fHeight > maxSize) {
krajcevski9c0e6292014-06-02 07:38:14 -07001064 return return_null_texture();
1065 }
1066
bsalomonb15b4c12014-10-29 12:41:57 -07001067 GrGLTexture::IDDesc idDesc;
1068 GL_CALL(GenTextures(1, &idDesc.fTextureID));
bsalomon5236cf42015-01-14 10:42:08 -08001069 idDesc.fLifeCycle = budgeted ? GrGpuResource::kCached_LifeCycle :
1070 GrGpuResource::kUncached_LifeCycle;
krajcevski9c0e6292014-06-02 07:38:14 -07001071
bsalomonb15b4c12014-10-29 12:41:57 -07001072 if (!idDesc.fTextureID) {
krajcevski9c0e6292014-06-02 07:38:14 -07001073 return return_null_texture();
1074 }
1075
1076 this->setScratchTextureUnit();
bsalomonb15b4c12014-10-29 12:41:57 -07001077 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, idDesc.fTextureID));
krajcevski9c0e6292014-06-02 07:38:14 -07001078
1079 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
1080 // drivers have a bug where an FBO won't be complete if it includes a
1081 // texture that is not mipmap complete (considering the filter in use).
1082 GrGLTexture::TexParams initialTexParams;
1083 // we only set a subset here so invalidate first
1084 initialTexParams.invalidate();
1085 initialTexParams.fMinFilter = GR_GL_NEAREST;
1086 initialTexParams.fMagFilter = GR_GL_NEAREST;
1087 initialTexParams.fWrapS = GR_GL_CLAMP_TO_EDGE;
1088 initialTexParams.fWrapT = GR_GL_CLAMP_TO_EDGE;
1089 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1090 GR_GL_TEXTURE_MAG_FILTER,
1091 initialTexParams.fMagFilter));
1092 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1093 GR_GL_TEXTURE_MIN_FILTER,
1094 initialTexParams.fMinFilter));
1095 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1096 GR_GL_TEXTURE_WRAP_S,
1097 initialTexParams.fWrapS));
1098 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1099 GR_GL_TEXTURE_WRAP_T,
1100 initialTexParams.fWrapT));
1101
bsalomonb15b4c12014-10-29 12:41:57 -07001102 if (!this->uploadCompressedTexData(desc, srcData)) {
1103 GL_CALL(DeleteTextures(1, &idDesc.fTextureID));
krajcevski9c0e6292014-06-02 07:38:14 -07001104 return return_null_texture();
1105 }
1106
1107 GrGLTexture* tex;
bsalomonb15b4c12014-10-29 12:41:57 -07001108 tex = SkNEW_ARGS(GrGLTexture, (this, desc, idDesc));
krajcevski9c0e6292014-06-02 07:38:14 -07001109 tex->setCachedTexParams(initialTexParams, this->getResetTimestamp());
1110#ifdef TRACE_TEXTURE_CREATION
tfarina38406c82014-10-31 07:11:12 -07001111 SkDebugf("--- new compressed texture [%d] size=(%d %d) config=%d\n",
krajcevski9c0e6292014-06-02 07:38:14 -07001112 glTexDesc.fTextureID, desc.fWidth, desc.fHeight, desc.fConfig);
1113#endif
1114 return tex;
1115}
1116
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001117namespace {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001118
1119const GrGLuint kUnknownBitCount = GrGLStencilBuffer::kUnknownBitCount;
1120
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001121void inline get_stencil_rb_sizes(const GrGLInterface* gl,
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001122 GrGLStencilBuffer::Format* format) {
sugoi@google.com6ba0b0f2013-05-03 13:52:32 +00001123
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001124 // we shouldn't ever know one size and not the other
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001125 SkASSERT((kUnknownBitCount == format->fStencilBits) ==
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001126 (kUnknownBitCount == format->fTotalBits));
1127 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001128 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001129 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1130 (GrGLint*)&format->fStencilBits);
1131 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001132 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001133 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1134 (GrGLint*)&format->fTotalBits);
1135 format->fTotalBits += format->fStencilBits;
1136 } else {
1137 format->fTotalBits = format->fStencilBits;
1138 }
1139 }
1140}
1141}
1142
bsalomon02a44a42015-02-19 09:09:00 -08001143bool GrGLGpu::createStencilBufferForRenderTarget(GrRenderTarget* rt, int width, int height) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001144 // All internally created RTs are also textures. We don't create
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +00001145 // 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 +00001146 SkASSERT(rt->asTexture());
1147 SkASSERT(width >= rt->width());
1148 SkASSERT(height >= rt->height());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001149
1150 int samples = rt->numSamples();
kkinnunen36c57df2015-01-27 00:30:18 -08001151 GrGLStencilBuffer::IDDesc sbDesc;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001152
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001153 int stencilFmtCnt = this->glCaps().stencilFormats().count();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001154 for (int i = 0; i < stencilFmtCnt; ++i) {
kkinnunen36c57df2015-01-27 00:30:18 -08001155 if (!sbDesc.fRenderbufferID) {
1156 GL_CALL(GenRenderbuffers(1, &sbDesc.fRenderbufferID));
bsalomon12299ab2014-11-14 13:33:09 -08001157 }
kkinnunen36c57df2015-01-27 00:30:18 -08001158 if (!sbDesc.fRenderbufferID) {
bsalomon12299ab2014-11-14 13:33:09 -08001159 return false;
1160 }
kkinnunen36c57df2015-01-27 00:30:18 -08001161 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbDesc.fRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001162 // we start with the last stencil format that succeeded in hopes
1163 // that we won't go through this loop more than once after the
1164 // first (painful) stencil creation.
1165 int sIdx = (i + fLastSuccessfulStencilFmtIdx) % stencilFmtCnt;
bsalomon12299ab2014-11-14 13:33:09 -08001166 const GrGLCaps::StencilFormat& sFmt = this->glCaps().stencilFormats()[sIdx];
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001167 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001168 // we do this "if" so that we don't call the multisample
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001169 // version on a GL that doesn't have an MSAA extension.
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001170 bool created;
1171 if (samples > 0) {
robertphillips@google.com6177e692013-02-28 20:16:25 +00001172 created = renderbuffer_storage_msaa(fGLContext,
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001173 samples,
1174 sFmt.fInternalFormat,
1175 width, height);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001176 } else {
bsalomon12299ab2014-11-14 13:33:09 -08001177 GL_ALLOC_CALL(this->glInterface(), RenderbufferStorage(GR_GL_RENDERBUFFER,
1178 sFmt.fInternalFormat,
1179 width, height));
1180 created = (GR_GL_NO_ERROR == check_alloc_error(rt->desc(), this->glInterface()));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001181 }
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001182 if (created) {
bsalomon9e5fc722015-02-23 10:01:36 -08001183 fStats.incStencilBufferCreates();
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001184 // After sized formats we attempt an unsized format and take
1185 // whatever sizes GL gives us. In that case we query for the size.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001186 GrGLStencilBuffer::Format format = sFmt;
sugoi@google.com6ba0b0f2013-05-03 13:52:32 +00001187 get_stencil_rb_sizes(this->glInterface(), &format);
bsalomondd3143b2015-02-23 09:27:45 -08001188 SkAutoTUnref<GrGLStencilBuffer> sb(SkNEW_ARGS(GrGLStencilBuffer,
kkinnunen36c57df2015-01-27 00:30:18 -08001189 (this, sbDesc, width, height, samples, format)));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001190 if (this->attachStencilBufferToRenderTarget(sb, rt)) {
1191 fLastSuccessfulStencilFmtIdx = sIdx;
bsalomon6bc1b5f2015-02-23 09:06:38 -08001192 rt->renderTargetPriv().didAttachStencilBuffer(sb);
bsalomondd3143b2015-02-23 09:27:45 -08001193
1194 // Clear the stencil buffer. We use a special purpose FBO for this so that the
1195 // entire stencil buffer is cleared, even if it is attached to an FBO with a
1196 // smaller color target.
bsalomon160f24c2015-03-17 15:55:42 -07001197 if (!fStencilClearFBO) {
1198 fStencilClearFBO.reset(SkNEW_ARGS(GrGLFBO, (this->glInterface())));
bsalomondd3143b2015-02-23 09:27:45 -08001199 }
bsalomon160f24c2015-03-17 15:55:42 -07001200 SkASSERT(fStencilClearFBO->isValid());
1201 GrGLenum fboTarget = this->bindFBO(kClear_FBOBinding, fStencilClearFBO);
robertphillips73165bd2015-03-01 07:38:15 -08001202
bsalomon160f24c2015-03-17 15:55:42 -07001203 GL_CALL(FramebufferRenderbuffer(fboTarget,
bsalomondd3143b2015-02-23 09:27:45 -08001204 GR_GL_STENCIL_ATTACHMENT,
1205 GR_GL_RENDERBUFFER, sbDesc.fRenderbufferID));
1206 if (sFmt.fPacked) {
bsalomon160f24c2015-03-17 15:55:42 -07001207 GL_CALL(FramebufferRenderbuffer(fboTarget,
bsalomondd3143b2015-02-23 09:27:45 -08001208 GR_GL_DEPTH_ATTACHMENT,
1209 GR_GL_RENDERBUFFER, sbDesc.fRenderbufferID));
1210 }
1211
1212 GL_CALL(ClearStencil(0));
bsalomon9d2049d2015-03-17 12:06:19 -07001213 // Many GL implementations seem to have trouble with clearing an FBO with only
1214 // a stencil buffer.
1215 GrGLuint tempRB;
1216 GL_CALL(GenRenderbuffers(1, &tempRB));
1217 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, tempRB));
bsalomon160f24c2015-03-17 15:55:42 -07001218
Brian Salomon9251d4e2015-03-17 16:03:19 -04001219 if (samples > 0) {
1220 renderbuffer_storage_msaa(fGLContext, samples, GR_GL_RGBA8, width, height);
1221 } else {
1222 GL_CALL(RenderbufferStorage(GR_GL_RENDERBUFFER, GR_GL_RGBA8, width, height));
1223 }
bsalomon160f24c2015-03-17 15:55:42 -07001224 GL_CALL(FramebufferRenderbuffer(fboTarget,
bsalomon9d2049d2015-03-17 12:06:19 -07001225 GR_GL_COLOR_ATTACHMENT0,
1226 GR_GL_RENDERBUFFER, tempRB));
1227
bsalomondd3143b2015-02-23 09:27:45 -08001228 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
1229
bsalomon160f24c2015-03-17 15:55:42 -07001230 GL_CALL(FramebufferRenderbuffer(fboTarget,
bsalomon9d2049d2015-03-17 12:06:19 -07001231 GR_GL_COLOR_ATTACHMENT0,
1232 GR_GL_RENDERBUFFER, 0));
1233 GL_CALL(DeleteRenderbuffers(1, &tempRB));
1234
bsalomondd3143b2015-02-23 09:27:45 -08001235 // Unbind the SB from the FBO so that we don't keep it alive.
bsalomon160f24c2015-03-17 15:55:42 -07001236 GL_CALL(FramebufferRenderbuffer(fboTarget,
bsalomondd3143b2015-02-23 09:27:45 -08001237 GR_GL_STENCIL_ATTACHMENT,
1238 GR_GL_RENDERBUFFER, 0));
1239 if (sFmt.fPacked) {
bsalomon160f24c2015-03-17 15:55:42 -07001240 GL_CALL(FramebufferRenderbuffer(fboTarget,
bsalomondd3143b2015-02-23 09:27:45 -08001241 GR_GL_DEPTH_ATTACHMENT,
1242 GR_GL_RENDERBUFFER, 0));
1243 }
1244
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001245 return true;
bsalomon12299ab2014-11-14 13:33:09 -08001246 }
bsalomon10e23ca2014-11-25 05:52:06 -08001247 // Remove the scratch key from this resource so we don't grab it from the cache ever
1248 // again.
bsalomon3582d3e2015-02-13 14:20:05 -08001249 sb->resourcePriv().removeScratchKey();
bsalomon10e23ca2014-11-25 05:52:06 -08001250 // Set this to 0 since we handed the valid ID off to the failed stencil buffer resource.
kkinnunen36c57df2015-01-27 00:30:18 -08001251 sbDesc.fRenderbufferID = 0;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001252 }
1253 }
kkinnunen36c57df2015-01-27 00:30:18 -08001254 GL_CALL(DeleteRenderbuffers(1, &sbDesc.fRenderbufferID));
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001255 return false;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001256}
1257
bsalomon861e1032014-12-16 07:33:49 -08001258bool GrGLGpu::attachStencilBufferToRenderTarget(GrStencilBuffer* sb, GrRenderTarget* rt) {
bsalomon37dd3312014-11-03 08:47:23 -08001259 GrGLRenderTarget* glrt = static_cast<GrGLRenderTarget*>(rt);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001260 if (NULL == sb) {
bsalomon6bc1b5f2015-02-23 09:06:38 -08001261 if (rt->renderTargetPriv().getStencilBuffer()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001262 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001263 GR_GL_STENCIL_ATTACHMENT,
1264 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001265 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001266 GR_GL_DEPTH_ATTACHMENT,
1267 GR_GL_RENDERBUFFER, 0));
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +00001268#ifdef SK_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001269 GrGLenum status;
1270 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001271 SkASSERT(GR_GL_FRAMEBUFFER_COMPLETE == status);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001272#endif
1273 }
1274 return true;
1275 } else {
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001276 GrGLStencilBuffer* glsb = static_cast<GrGLStencilBuffer*>(sb);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001277 GrGLuint rb = glsb->renderbufferID();
bsalomon160f24c2015-03-17 15:55:42 -07001278 GrGLenum fboTarget = this->bindFBO(kChangeAttachments_FBOBinding, glrt->renderFBO());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001279
bsalomon160f24c2015-03-17 15:55:42 -07001280 GL_CALL(FramebufferRenderbuffer(fboTarget,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001281 GR_GL_STENCIL_ATTACHMENT,
1282 GR_GL_RENDERBUFFER, rb));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001283 if (glsb->format().fPacked) {
bsalomon160f24c2015-03-17 15:55:42 -07001284 GL_CALL(FramebufferRenderbuffer(fboTarget,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001285 GR_GL_DEPTH_ATTACHMENT,
1286 GR_GL_RENDERBUFFER, rb));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001287 } else {
bsalomon160f24c2015-03-17 15:55:42 -07001288 GL_CALL(FramebufferRenderbuffer(fboTarget,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001289 GR_GL_DEPTH_ATTACHMENT,
1290 GR_GL_RENDERBUFFER, 0));
reed@google.comac10a2d2010-12-22 21:39:39 +00001291 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001292
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001293 GrGLenum status;
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001294 if (!this->glCaps().isColorConfigAndStencilFormatVerified(rt->config(), glsb->format())) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001295 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1296 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
bsalomon160f24c2015-03-17 15:55:42 -07001297 GL_CALL(FramebufferRenderbuffer(fboTarget,
1298 GR_GL_STENCIL_ATTACHMENT,
1299 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001300 if (glsb->format().fPacked) {
bsalomon160f24c2015-03-17 15:55:42 -07001301 GL_CALL(FramebufferRenderbuffer(fboTarget,
1302 GR_GL_DEPTH_ATTACHMENT,
1303 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001304 }
1305 return false;
1306 } else {
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +00001307 fGLContext.caps()->markColorConfigAndStencilFormatAsVerified(
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001308 rt->config(),
1309 glsb->format());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001310 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001311 }
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001312 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001313 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001314}
1315
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001316////////////////////////////////////////////////////////////////////////////////
1317
bsalomon861e1032014-12-16 07:33:49 -08001318GrVertexBuffer* GrGLGpu::onCreateVertexBuffer(size_t size, bool dynamic) {
bsalomon@google.come49ad452013-02-20 19:33:20 +00001319 GrGLVertexBuffer::Desc desc;
1320 desc.fDynamic = dynamic;
1321 desc.fSizeInBytes = size;
bsalomon@google.come49ad452013-02-20 19:33:20 +00001322
bsalomon@google.com96966a52013-02-21 16:34:21 +00001323 if (this->glCaps().useNonVBOVertexAndIndexDynamicData() && desc.fDynamic) {
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001324 desc.fID = 0;
bsalomon@google.come49ad452013-02-20 19:33:20 +00001325 GrGLVertexBuffer* vertexBuffer = SkNEW_ARGS(GrGLVertexBuffer, (this, desc));
reed@google.comac10a2d2010-12-22 21:39:39 +00001326 return vertexBuffer;
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001327 } else {
1328 GL_CALL(GenBuffers(1, &desc.fID));
1329 if (desc.fID) {
bsalomon@google.com6918d482013-03-07 19:09:11 +00001330 fHWGeometryState.setVertexBufferID(this, desc.fID);
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001331 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1332 // make sure driver can allocate memory for this buffer
1333 GL_ALLOC_CALL(this->glInterface(),
1334 BufferData(GR_GL_ARRAY_BUFFER,
robertphillips@google.come9cd27d2013-10-16 17:48:11 +00001335 (GrGLsizeiptr) desc.fSizeInBytes,
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001336 NULL, // data ptr
1337 desc.fDynamic ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW));
1338 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
1339 GL_CALL(DeleteBuffers(1, &desc.fID));
bsalomon@google.com6918d482013-03-07 19:09:11 +00001340 this->notifyVertexBufferDelete(desc.fID);
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001341 return NULL;
1342 }
1343 GrGLVertexBuffer* vertexBuffer = SkNEW_ARGS(GrGLVertexBuffer, (this, desc));
1344 return vertexBuffer;
1345 }
1346 return NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +00001347 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001348}
1349
bsalomon861e1032014-12-16 07:33:49 -08001350GrIndexBuffer* GrGLGpu::onCreateIndexBuffer(size_t size, bool dynamic) {
bsalomon@google.come49ad452013-02-20 19:33:20 +00001351 GrGLIndexBuffer::Desc desc;
1352 desc.fDynamic = dynamic;
1353 desc.fSizeInBytes = size;
bsalomon@google.come49ad452013-02-20 19:33:20 +00001354
bsalomon@google.com96966a52013-02-21 16:34:21 +00001355 if (this->glCaps().useNonVBOVertexAndIndexDynamicData() && desc.fDynamic) {
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001356 desc.fID = 0;
bsalomon@google.come49ad452013-02-20 19:33:20 +00001357 GrIndexBuffer* indexBuffer = SkNEW_ARGS(GrGLIndexBuffer, (this, desc));
reed@google.comac10a2d2010-12-22 21:39:39 +00001358 return indexBuffer;
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001359 } else {
1360 GL_CALL(GenBuffers(1, &desc.fID));
1361 if (desc.fID) {
bsalomon@google.com6918d482013-03-07 19:09:11 +00001362 fHWGeometryState.setIndexBufferIDOnDefaultVertexArray(this, desc.fID);
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001363 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1364 // make sure driver can allocate memory for this buffer
1365 GL_ALLOC_CALL(this->glInterface(),
1366 BufferData(GR_GL_ELEMENT_ARRAY_BUFFER,
robertphillips@google.come9cd27d2013-10-16 17:48:11 +00001367 (GrGLsizeiptr) desc.fSizeInBytes,
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001368 NULL, // data ptr
1369 desc.fDynamic ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW));
1370 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
1371 GL_CALL(DeleteBuffers(1, &desc.fID));
bsalomon@google.com6918d482013-03-07 19:09:11 +00001372 this->notifyIndexBufferDelete(desc.fID);
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001373 return NULL;
1374 }
1375 GrIndexBuffer* indexBuffer = SkNEW_ARGS(GrGLIndexBuffer, (this, desc));
1376 return indexBuffer;
1377 }
1378 return NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +00001379 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001380}
1381
bsalomon3e791242014-12-17 13:43:13 -08001382void GrGLGpu::flushScissor(const GrScissorState& scissorState,
joshualitt77b13072014-10-27 14:51:01 -07001383 const GrGLIRect& rtViewport,
1384 GrSurfaceOrigin rtOrigin) {
robertphillipse85a32d2015-02-10 08:16:55 -08001385 if (scissorState.enabled()) {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001386 GrGLIRect scissor;
bsalomonb0bd4f62014-09-03 07:19:50 -07001387 scissor.setRelativeTo(rtViewport,
robertphillipse85a32d2015-02-10 08:16:55 -08001388 scissorState.rect().fLeft,
1389 scissorState.rect().fTop,
1390 scissorState.rect().width(),
1391 scissorState.rect().height(),
bsalomonb0bd4f62014-09-03 07:19:50 -07001392 rtOrigin);
bsalomon@google.coma3201942012-06-21 19:58:20 +00001393 // if the scissor fully contains the viewport then we fall through and
1394 // disable the scissor test.
bsalomonb0bd4f62014-09-03 07:19:50 -07001395 if (!scissor.contains(rtViewport)) {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001396 if (fHWScissorSettings.fRect != scissor) {
1397 scissor.pushToGLScissor(this->glInterface());
1398 fHWScissorSettings.fRect = scissor;
1399 }
1400 if (kYes_TriState != fHWScissorSettings.fEnabled) {
1401 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1402 fHWScissorSettings.fEnabled = kYes_TriState;
1403 }
1404 return;
1405 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001406 }
joshualitt77b13072014-10-27 14:51:01 -07001407
1408 // See fall through note above
1409 this->disableScissor();
1410}
1411
cdaltond0a840d2015-03-16 17:19:58 -07001412bool GrGLGpu::flushGLState(const DrawArgs& args) {
egdaniel080e6732014-12-22 07:35:52 -08001413 GrXferProcessor::BlendInfo blendInfo;
egdaniel8dd688b2015-01-22 10:16:09 -08001414 const GrPipeline& pipeline = *args.fPipeline;
1415 args.fPipeline->getXferProcessor()->getBlendInfo(&blendInfo);
egdaniel080e6732014-12-22 07:35:52 -08001416
egdaniel8dd688b2015-01-22 10:16:09 -08001417 this->flushDither(pipeline.isDitherState());
egdaniel080e6732014-12-22 07:35:52 -08001418 this->flushColorWrite(blendInfo.fWriteColor);
egdaniel8dd688b2015-01-22 10:16:09 -08001419 this->flushDrawFace(pipeline.getDrawFace());
bsalomonbc3d0de2014-12-15 13:45:03 -08001420
joshualitt873ad0e2015-01-20 09:08:51 -08001421 fCurrentProgram.reset(fProgramCache->getProgram(args));
bsalomon1f78c0a2014-12-17 09:43:13 -08001422 if (NULL == fCurrentProgram.get()) {
1423 SkDEBUGFAIL("Failed to create program!");
1424 return false;
bsalomonbc3d0de2014-12-15 13:45:03 -08001425 }
1426
bsalomon1f78c0a2014-12-17 09:43:13 -08001427 fCurrentProgram.get()->ref();
1428
1429 GrGLuint programID = fCurrentProgram->programID();
1430 if (fHWProgramID != programID) {
1431 GL_CALL(UseProgram(programID));
1432 fHWProgramID = programID;
1433 }
1434
bsalomon1f78c0a2014-12-17 09:43:13 -08001435
egdaniel8dd688b2015-01-22 10:16:09 -08001436 fCurrentProgram->setData(*args.fPrimitiveProcessor, pipeline, *args.fBatchTracker);
bsalomon1f78c0a2014-12-17 09:43:13 -08001437
egdaniel8dd688b2015-01-22 10:16:09 -08001438 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(pipeline.getRenderTarget());
bsalomon160f24c2015-03-17 15:55:42 -07001439
egdaniel8dd688b2015-01-22 10:16:09 -08001440 this->flushStencil(pipeline.getStencil());
1441 this->flushScissor(pipeline.getScissorState(), glRT->getViewport(), glRT->origin());
cdaltond0a840d2015-03-16 17:19:58 -07001442 this->flushHWAAState(glRT, pipeline.isHWAntialiasState());
bsalomonbc3d0de2014-12-15 13:45:03 -08001443
1444 // This must come after textures are flushed because a texture may need
bsalomon160f24c2015-03-17 15:55:42 -07001445 // to be msaa-resolved (which will modify bound FBO and scissor state).
1446 this->bindFBO(kDraw_FBOBinding, glRT->renderFBO());
1447 this->setViewport(glRT->getViewport());
1448 if (blendInfo.fWriteColor) {
1449 this->flushBlend(blendInfo);
1450 this->markSurfaceContentsDirty(glRT, NULL);
1451 }
bsalomonbc3d0de2014-12-15 13:45:03 -08001452
1453 return true;
1454}
1455
joshualitt873ad0e2015-01-20 09:08:51 -08001456void GrGLGpu::setupGeometry(const GrPrimitiveProcessor& primProc,
bsalomonbc3d0de2014-12-15 13:45:03 -08001457 const GrDrawTarget::DrawInfo& info,
1458 size_t* indexOffsetInBytes) {
1459 GrGLVertexBuffer* vbuf;
1460 vbuf = (GrGLVertexBuffer*) info.vertexBuffer();
1461
1462 SkASSERT(vbuf);
1463 SkASSERT(!vbuf->isMapped());
1464
1465 GrGLIndexBuffer* ibuf = NULL;
1466 if (info.isIndexed()) {
1467 SkASSERT(indexOffsetInBytes);
1468
1469 *indexOffsetInBytes = 0;
1470 ibuf = (GrGLIndexBuffer*)info.indexBuffer();
1471
1472 SkASSERT(ibuf);
1473 SkASSERT(!ibuf->isMapped());
1474 *indexOffsetInBytes += ibuf->baseOffset();
1475 }
1476 GrGLAttribArrayState* attribState =
1477 fHWGeometryState.bindArrayAndBuffersToDraw(this, vbuf, ibuf);
1478
joshualitt873ad0e2015-01-20 09:08:51 -08001479 int vaCount = primProc.numAttribs();
joshualitt71c92602015-01-14 08:12:47 -08001480 if (vaCount > 0) {
bsalomonbc3d0de2014-12-15 13:45:03 -08001481
joshualitt873ad0e2015-01-20 09:08:51 -08001482 GrGLsizei stride = static_cast<GrGLsizei>(primProc.getVertexStride());
bsalomonbc3d0de2014-12-15 13:45:03 -08001483
1484 size_t vertexOffsetInBytes = stride * info.startVertex();
1485
1486 vertexOffsetInBytes += vbuf->baseOffset();
1487
bsalomonbc3d0de2014-12-15 13:45:03 -08001488 uint32_t usedAttribArraysMask = 0;
1489 size_t offset = 0;
1490
1491 for (int attribIndex = 0; attribIndex < vaCount; attribIndex++) {
joshualitt873ad0e2015-01-20 09:08:51 -08001492 const GrGeometryProcessor::Attribute& attrib = primProc.getAttrib(attribIndex);
bsalomonbc3d0de2014-12-15 13:45:03 -08001493 usedAttribArraysMask |= (1 << attribIndex);
joshualitt71c92602015-01-14 08:12:47 -08001494 GrVertexAttribType attribType = attrib.fType;
bsalomonbc3d0de2014-12-15 13:45:03 -08001495 attribState->set(this,
1496 attribIndex,
1497 vbuf,
1498 GrGLAttribTypeToLayout(attribType).fCount,
1499 GrGLAttribTypeToLayout(attribType).fType,
1500 GrGLAttribTypeToLayout(attribType).fNormalized,
1501 stride,
1502 reinterpret_cast<GrGLvoid*>(vertexOffsetInBytes + offset));
joshualitt71c92602015-01-14 08:12:47 -08001503 offset += attrib.fOffset;
bsalomonbc3d0de2014-12-15 13:45:03 -08001504 }
1505 attribState->disableUnusedArrays(this, usedAttribArraysMask);
1506 }
1507}
1508
joshualitt873ad0e2015-01-20 09:08:51 -08001509void GrGLGpu::buildProgramDesc(GrProgramDesc* desc,
1510 const GrPrimitiveProcessor& primProc,
egdaniel8dd688b2015-01-22 10:16:09 -08001511 const GrPipeline& pipeline,
joshualitt873ad0e2015-01-20 09:08:51 -08001512 const GrBatchTracker& batchTracker) const {
bsalomon50785a32015-02-06 07:02:37 -08001513 if (!GrGLProgramDescBuilder::Build(desc, primProc, pipeline, this, batchTracker)) {
bsalomonbc3d0de2014-12-15 13:45:03 -08001514 SkDEBUGFAIL("Failed to generate GL program descriptor");
1515 }
1516}
1517
bsalomon861e1032014-12-16 07:33:49 -08001518void GrGLGpu::disableScissor() {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001519 if (kNo_TriState != fHWScissorSettings.fEnabled) {
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001520 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.coma3201942012-06-21 19:58:20 +00001521 fHWScissorSettings.fEnabled = kNo_TriState;
1522 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001523 }
1524}
1525
bsalomon861e1032014-12-16 07:33:49 -08001526void GrGLGpu::onClear(GrRenderTarget* target, const SkIRect* rect, GrColor color,
joshualitt4b68ec02014-11-07 14:11:45 -08001527 bool canIgnoreRect) {
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001528 // parent class should never let us get here with no RT
bsalomon49f085d2014-09-05 13:34:00 -07001529 SkASSERT(target);
bsalomonb0bd4f62014-09-03 07:19:50 -07001530 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001531
robertphillips@google.com56ce48a2013-10-31 21:44:25 +00001532 if (canIgnoreRect && this->glCaps().fullClearIsFree()) {
1533 rect = NULL;
1534 }
1535
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001536 SkIRect clippedRect;
bsalomon49f085d2014-09-05 13:34:00 -07001537 if (rect) {
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001538 // flushScissor expects rect to be clipped to the target.
bsalomon@google.com74b98712011-11-11 19:46:16 +00001539 clippedRect = *rect;
bsalomonb0bd4f62014-09-03 07:19:50 -07001540 SkIRect rtRect = SkIRect::MakeWH(target->width(), target->height());
bsalomon@google.com74b98712011-11-11 19:46:16 +00001541 if (clippedRect.intersect(rtRect)) {
1542 rect = &clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001543 } else {
1544 return;
1545 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001546 }
rmistry@google.comd6bab022013-12-02 13:50:38 +00001547
bsalomon160f24c2015-03-17 15:55:42 -07001548 this->bindFBO(kClear_FBOBinding, glRT->renderFBO());
1549 this->markSurfaceContentsDirty(glRT, rect);
bsalomon3e791242014-12-17 13:43:13 -08001550 GrScissorState scissorState;
robertphillipse85a32d2015-02-10 08:16:55 -08001551 if (rect) {
1552 scissorState.set(*rect);
bsalomon@google.coma3201942012-06-21 19:58:20 +00001553 }
joshualitt77b13072014-10-27 14:51:01 -07001554 this->flushScissor(scissorState, glRT->getViewport(), glRT->origin());
bsalomon@google.com74b98712011-11-11 19:46:16 +00001555
1556 GrGLfloat r, g, b, a;
1557 static const GrGLfloat scale255 = 1.f / 255.f;
1558 a = GrColorUnpackA(color) * scale255;
1559 GrGLfloat scaleRGB = scale255;
bsalomon@google.com74b98712011-11-11 19:46:16 +00001560 r = GrColorUnpackR(color) * scaleRGB;
1561 g = GrColorUnpackG(color) * scaleRGB;
1562 b = GrColorUnpackB(color) * scaleRGB;
1563
1564 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00001565 fHWWriteToColor = kYes_TriState;
bsalomon@google.com74b98712011-11-11 19:46:16 +00001566 GL_CALL(ClearColor(r, g, b, a));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001567 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001568}
1569
bsalomon861e1032014-12-16 07:33:49 -08001570void GrGLGpu::discard(GrRenderTarget* renderTarget) {
bsalomon89c62982014-11-03 12:08:42 -08001571 SkASSERT(renderTarget);
commit-bot@chromium.org52ffbf62014-04-02 16:19:33 +00001572 if (!this->caps()->discardRenderTargetSupport()) {
1573 return;
1574 }
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +00001575
1576 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(renderTarget);
bsalomon160f24c2015-03-17 15:55:42 -07001577 GrGLenum fboTarget = this->bindFBO(kDiscard_FBOBinding, glRT->renderFBO());
commit-bot@chromium.org52ffbf62014-04-02 16:19:33 +00001578 switch (this->glCaps().invalidateFBType()) {
joshualitt58162332014-08-01 06:44:53 -07001579 case GrGLCaps::kNone_InvalidateFBType:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +00001580 SkFAIL("Should never get here.");
commit-bot@chromium.org52ffbf62014-04-02 16:19:33 +00001581 break;
1582 case GrGLCaps::kInvalidate_InvalidateFBType:
bsalomon160f24c2015-03-17 15:55:42 -07001583 if (glRT->renderFBO()->isDefaultFramebuffer()) {
commit-bot@chromium.org52ffbf62014-04-02 16:19:33 +00001584 // When rendering to the default framebuffer the legal values for attachments
1585 // are GL_COLOR, GL_DEPTH, GL_STENCIL, ... rather than the various FBO attachment
1586 // types.
1587 static const GrGLenum attachments[] = { GR_GL_COLOR };
bsalomon160f24c2015-03-17 15:55:42 -07001588 GL_CALL(InvalidateFramebuffer(fboTarget, SK_ARRAY_COUNT(attachments),
commit-bot@chromium.org52ffbf62014-04-02 16:19:33 +00001589 attachments));
1590 } else {
1591 static const GrGLenum attachments[] = { GR_GL_COLOR_ATTACHMENT0 };
bsalomon160f24c2015-03-17 15:55:42 -07001592 GL_CALL(InvalidateFramebuffer(fboTarget, SK_ARRAY_COUNT(attachments),
commit-bot@chromium.org52ffbf62014-04-02 16:19:33 +00001593 attachments));
1594 }
1595 break;
1596 case GrGLCaps::kDiscard_InvalidateFBType: {
bsalomon160f24c2015-03-17 15:55:42 -07001597 if (glRT->renderFBO()->isDefaultFramebuffer()) {
commit-bot@chromium.org4453e8b2014-04-16 14:33:27 +00001598 // When rendering to the default framebuffer the legal values for attachments
1599 // are GL_COLOR, GL_DEPTH, GL_STENCIL, ... rather than the various FBO attachment
1600 // types. See glDiscardFramebuffer() spec.
1601 static const GrGLenum attachments[] = { GR_GL_COLOR };
bsalomon160f24c2015-03-17 15:55:42 -07001602 GL_CALL(DiscardFramebuffer(fboTarget, SK_ARRAY_COUNT(attachments),
commit-bot@chromium.org4453e8b2014-04-16 14:33:27 +00001603 attachments));
1604 } else {
1605 static const GrGLenum attachments[] = { GR_GL_COLOR_ATTACHMENT0 };
bsalomon160f24c2015-03-17 15:55:42 -07001606 GL_CALL(DiscardFramebuffer(fboTarget, SK_ARRAY_COUNT(attachments),
commit-bot@chromium.org4453e8b2014-04-16 14:33:27 +00001607 attachments));
1608 }
commit-bot@chromium.org52ffbf62014-04-02 16:19:33 +00001609 break;
1610 }
1611 }
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +00001612 renderTarget->flagAsResolved();
1613}
1614
1615
bsalomon861e1032014-12-16 07:33:49 -08001616void GrGLGpu::clearStencil(GrRenderTarget* target) {
bsalomonb0bd4f62014-09-03 07:19:50 -07001617 if (NULL == target) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001618 return;
1619 }
bsalomonb0bd4f62014-09-03 07:19:50 -07001620 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
bsalomon160f24c2015-03-17 15:55:42 -07001621 this->bindFBO(kClear_FBOBinding, glRT->renderFBO());
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001622
joshualitt77b13072014-10-27 14:51:01 -07001623 this->disableScissor();
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001624
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001625 GL_CALL(StencilMask(0xffffffff));
1626 GL_CALL(ClearStencil(0));
1627 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001628 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001629}
1630
bsalomon861e1032014-12-16 07:33:49 -08001631void GrGLGpu::onClearStencilClip(GrRenderTarget* target, const SkIRect& rect, bool insideClip) {
bsalomon49f085d2014-09-05 13:34:00 -07001632 SkASSERT(target);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001633
bsalomon6bc1b5f2015-02-23 09:06:38 -08001634 GrStencilBuffer* sb = target->renderTargetPriv().getStencilBuffer();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001635 // this should only be called internally when we know we have a
1636 // stencil buffer.
bsalomon6bc1b5f2015-02-23 09:06:38 -08001637 SkASSERT(sb);
1638 GrGLint stencilBitCount = sb->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001639#if 0
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001640 SkASSERT(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00001641 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001642#else
1643 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001644 // ANGLE a partial stencil mask will cause clears to be
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001645 // turned into draws. Our contract on GrDrawTarget says that
1646 // changing the clip between stencil passes may or may not
1647 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00001648 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001649#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001650 GrGLint value;
1651 if (insideClip) {
1652 value = (1 << (stencilBitCount - 1));
1653 } else {
1654 value = 0;
1655 }
bsalomonb0bd4f62014-09-03 07:19:50 -07001656 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
bsalomon160f24c2015-03-17 15:55:42 -07001657 this->bindFBO(kClear_FBOBinding, glRT->renderFBO());
bsalomon@google.coma3201942012-06-21 19:58:20 +00001658
bsalomon3e791242014-12-17 13:43:13 -08001659 GrScissorState scissorState;
robertphillipse85a32d2015-02-10 08:16:55 -08001660 scissorState.set(rect);
joshualitt77b13072014-10-27 14:51:01 -07001661 this->flushScissor(scissorState, glRT->getViewport(), glRT->origin());
bsalomon@google.coma3201942012-06-21 19:58:20 +00001662
caryclark@google.comcf6285b2012-06-06 12:09:01 +00001663 GL_CALL(StencilMask((uint32_t) clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001664 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001665 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001666 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001667}
1668
bsalomon861e1032014-12-16 07:33:49 -08001669bool GrGLGpu::readPixelsWillPayForYFlip(GrRenderTarget* renderTarget,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001670 int left, int top,
1671 int width, int height,
1672 GrPixelConfig config,
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001673 size_t rowBytes) const {
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001674 // If this rendertarget is aready TopLeft, we don't need to flip.
1675 if (kTopLeft_GrSurfaceOrigin == renderTarget->origin()) {
1676 return false;
1677 }
1678
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001679 // if GL can do the flip then we'll never pay for it.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001680 if (this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001681 return false;
1682 }
1683
1684 // If we have to do memcpy to handle non-trim rowBytes then we
bsalomon@google.com7107fa72011-11-10 14:54:14 +00001685 // get the flip for free. Otherwise it costs.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001686 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001687 return true;
1688 }
1689 // If we have to do memcpys to handle rowBytes then y-flip is free
1690 // Note the rowBytes might be tight to the passed in data, but if data
1691 // gets clipped in x to the target the rowBytes will no longer be tight.
1692 if (left >= 0 && (left + width) < renderTarget->width()) {
1693 return 0 == rowBytes ||
1694 GrBytesPerPixel(config) * width == rowBytes;
1695 } else {
1696 return false;
1697 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001698}
1699
bsalomon861e1032014-12-16 07:33:49 -08001700bool GrGLGpu::onReadPixels(GrRenderTarget* target,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001701 int left, int top,
1702 int width, int height,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001703 GrPixelConfig config,
1704 void* buffer,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001705 size_t rowBytes) {
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +00001706 // We cannot read pixels into a compressed buffer
1707 if (GrPixelConfigIsCompressed(config)) {
1708 return false;
1709 }
1710
1711 GrGLenum format = 0;
1712 GrGLenum type = 0;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001713 bool flipY = kBottomLeft_GrSurfaceOrigin == target->origin();
bsalomon@google.com280e99f2012-01-05 16:17:38 +00001714 if (!this->configToGLFormats(config, false, NULL, &format, &type)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001715 return false;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001716 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001717 size_t bpp = GrBytesPerPixel(config);
1718 if (!adjust_pixel_ops_params(target->width(), target->height(), bpp,
1719 &left, &top, &width, &height,
1720 const_cast<const void**>(&buffer),
1721 &rowBytes)) {
1722 return false;
1723 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001724
bsalomon@google.comc6980972011-11-02 19:57:21 +00001725 // resolve the render target if necessary
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001726 GrGLRenderTarget* tgt = static_cast<GrGLRenderTarget*>(target);
bsalomon160f24c2015-03-17 15:55:42 -07001727 if (tgt->getResolveType() == GrGLRenderTarget::kCantResolve_ResolveType) {
1728 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +00001729 }
bsalomon160f24c2015-03-17 15:55:42 -07001730 if (tgt->getResolveType() == GrGLRenderTarget::kCanResolve_ResolveType) {
1731 this->onResolveRenderTarget(tgt);
1732 }
1733 this->bindFBO(kReadPixels_FBOBinding, tgt->textureFBO());
reed@google.comac10a2d2010-12-22 21:39:39 +00001734
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001735 const GrGLIRect& glvp = tgt->getViewport();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001736
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001737 // the read rect is viewport-relative
1738 GrGLIRect readRect;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001739 readRect.setRelativeTo(glvp, left, top, width, height, target->origin());
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001740
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001741 size_t tightRowBytes = bpp * width;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001742 if (0 == rowBytes) {
1743 rowBytes = tightRowBytes;
1744 }
1745 size_t readDstRowBytes = tightRowBytes;
1746 void* readDst = buffer;
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001747
bsalomon@google.comc6980972011-11-02 19:57:21 +00001748 // determine if GL can read using the passed rowBytes or if we need
1749 // a scratch buffer.
georgeb62508b2014-08-12 18:00:47 -07001750 GrAutoMalloc<32 * sizeof(GrColor)> scratch;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001751 if (rowBytes != tightRowBytes) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001752 if (this->glCaps().packRowLengthSupport()) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001753 SkASSERT(!(rowBytes % sizeof(GrColor)));
skia.committer@gmail.com4677acc2013-10-17 07:02:33 +00001754 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH,
robertphillips@google.come9cd27d2013-10-16 17:48:11 +00001755 static_cast<GrGLint>(rowBytes / sizeof(GrColor))));
bsalomon@google.comc6980972011-11-02 19:57:21 +00001756 readDstRowBytes = rowBytes;
1757 } else {
1758 scratch.reset(tightRowBytes * height);
1759 readDst = scratch.get();
1760 }
1761 }
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001762 if (flipY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001763 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 1));
1764 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001765 GL_CALL(ReadPixels(readRect.fLeft, readRect.fBottom,
1766 readRect.fWidth, readRect.fHeight,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001767 format, type, readDst));
1768 if (readDstRowBytes != tightRowBytes) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001769 SkASSERT(this->glCaps().packRowLengthSupport());
bsalomon@google.comc6980972011-11-02 19:57:21 +00001770 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
1771 }
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001772 if (flipY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001773 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 0));
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001774 flipY = false;
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001775 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001776
1777 // now reverse the order of the rows, since GL's are bottom-to-top, but our
bsalomon@google.comc6980972011-11-02 19:57:21 +00001778 // API presents top-to-bottom. We must preserve the padding contents. Note
1779 // that the above readPixels did not overwrite the padding.
1780 if (readDst == buffer) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001781 SkASSERT(rowBytes == readDstRowBytes);
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001782 if (flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001783 scratch.reset(tightRowBytes);
1784 void* tmpRow = scratch.get();
1785 // flip y in-place by rows
1786 const int halfY = height >> 1;
1787 char* top = reinterpret_cast<char*>(buffer);
1788 char* bottom = top + (height - 1) * rowBytes;
1789 for (int y = 0; y < halfY; y++) {
1790 memcpy(tmpRow, top, tightRowBytes);
1791 memcpy(top, bottom, tightRowBytes);
1792 memcpy(bottom, tmpRow, tightRowBytes);
1793 top += rowBytes;
1794 bottom -= rowBytes;
1795 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001796 }
1797 } else {
bsalomon160f24c2015-03-17 15:55:42 -07001798 SkASSERT(readDst != buffer);
1799 SkASSERT(rowBytes != tightRowBytes);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001800 // copy from readDst to buffer while flipping y
caryclark@google.comcf6285b2012-06-06 12:09:01 +00001801 // const int halfY = height >> 1;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001802 const char* src = reinterpret_cast<const char*>(readDst);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001803 char* dst = reinterpret_cast<char*>(buffer);
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001804 if (flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001805 dst += (height-1) * rowBytes;
1806 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001807 for (int y = 0; y < height; y++) {
1808 memcpy(dst, src, tightRowBytes);
1809 src += readDstRowBytes;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001810 if (!flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001811 dst += rowBytes;
1812 } else {
1813 dst -= rowBytes;
1814 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001815 }
1816 }
1817 return true;
1818}
1819
bsalomon160f24c2015-03-17 15:55:42 -07001820GrGLenum GrGLGpu::bindFBO(FBOBinding binding, const GrGLFBO* fbo) {
1821 SkASSERT(fbo);
1822 SkASSERT(fbo->isValid());
1823
1824 enum {
1825 kDraw = 0,
1826 kRead = 1
1827 };
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001828
bsalomon160f24c2015-03-17 15:55:42 -07001829 bool useGLFramebuffer = !this->glCaps().usesMSAARenderBuffers() ||
1830 (this->glCaps().preferBindingToReadAndDrawFramebuffer() &&
1831 kBlitSrc_FBOBinding != binding && kBlitDst_FBOBinding != binding);
bsalomon6ba6fa12015-03-04 11:57:37 -08001832
bsalomon160f24c2015-03-17 15:55:42 -07001833 if (useGLFramebuffer) {
1834 SkASSERT(kBlitSrc_FBOBinding != binding);
bsalomon1e0bf7e2015-03-14 12:08:51 -07001835 fStats.incRenderTargetBinds();
bsalomon160f24c2015-03-17 15:55:42 -07001836 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, fbo->fboID()));
1837 fHWFBOBinding[kDraw].fFBO.reset(SkRef(fbo));
1838 fHWFBOBinding[kRead].fFBO.reset(SkRef(fbo));
1839 return GR_GL_FRAMEBUFFER;
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +00001840 }
bsalomon160f24c2015-03-17 15:55:42 -07001841 GrGLenum target = 0;
1842 HWFBOBinding* hwFBOBinding = NULL;
1843 switch (binding) {
1844 case kDraw_FBOBinding:
1845 case kClear_FBOBinding:
1846 case kDiscard_FBOBinding:
1847 case kChangeAttachments_FBOBinding:
1848 case kBlitDst_FBOBinding:
1849 target = GR_GL_DRAW_FRAMEBUFFER;
1850 hwFBOBinding = &fHWFBOBinding[kDraw];
1851 break;
bsalomon5cd020f2015-03-17 12:46:56 -07001852
bsalomon160f24c2015-03-17 15:55:42 -07001853 case kReadPixels_FBOBinding:
1854 case kBlitSrc_FBOBinding:
1855 case kCopyTexSrc_FBOBinding:
1856 target = GR_GL_READ_FRAMEBUFFER;
1857 hwFBOBinding = &fHWFBOBinding[kRead];
1858 break;
1859 }
1860 fStats.incRenderTargetBinds();
1861 GL_CALL(BindFramebuffer(target, fbo->fboID()));
1862 hwFBOBinding->fFBO.reset(SkRef(fbo));
1863 return target;
1864}
1865
1866void GrGLGpu::setViewport(const GrGLIRect& viewport) {
1867 if (viewport != fHWViewport) {
1868 viewport.pushToGLViewport(this->glInterface());
1869 fHWViewport = viewport;
1870 }
1871}
1872
1873void GrGLGpu::markSurfaceContentsDirty(GrSurface* surface, const SkIRect* bounds) {
1874 if (NULL == bounds || !bounds->isEmpty()) {
1875 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(surface->asRenderTarget());
1876 if (rt) {
1877 rt->flagAsNeedingResolve(bounds);
1878 }
1879 GrGLTexture* texture = static_cast<GrGLTexture*>(surface->asTexture());
1880 if (texture) {
1881 texture->texturePriv().dirtyMipMaps(true);
1882 }
bsalomon5cd020f2015-03-17 12:46:56 -07001883 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001884}
1885
twiz@google.com0f31ca72011-03-18 17:38:11 +00001886GrGLenum gPrimitiveType2GLMode[] = {
1887 GR_GL_TRIANGLES,
1888 GR_GL_TRIANGLE_STRIP,
1889 GR_GL_TRIANGLE_FAN,
1890 GR_GL_POINTS,
1891 GR_GL_LINES,
1892 GR_GL_LINE_STRIP
reed@google.comac10a2d2010-12-22 21:39:39 +00001893};
1894
bsalomon@google.comd302f142011-03-03 13:54:13 +00001895#define SWAP_PER_DRAW 0
1896
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001897#if SWAP_PER_DRAW
commit-bot@chromium.org43823302013-09-25 20:57:51 +00001898 #if defined(SK_BUILD_FOR_MAC)
bsalomon@google.comd302f142011-03-03 13:54:13 +00001899 #include <AGL/agl.h>
commit-bot@chromium.org43823302013-09-25 20:57:51 +00001900 #elif defined(SK_BUILD_FOR_WIN32)
bsalomon@google.comce7357d2012-06-25 17:49:25 +00001901 #include <gl/GL.h>
bsalomon@google.comd302f142011-03-03 13:54:13 +00001902 void SwapBuf() {
1903 DWORD procID = GetCurrentProcessId();
1904 HWND hwnd = GetTopWindow(GetDesktopWindow());
1905 while(hwnd) {
1906 DWORD wndProcID = 0;
1907 GetWindowThreadProcessId(hwnd, &wndProcID);
1908 if(wndProcID == procID) {
1909 SwapBuffers(GetDC(hwnd));
1910 }
1911 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
1912 }
1913 }
1914 #endif
1915#endif
1916
joshualitt873ad0e2015-01-20 09:08:51 -08001917void GrGLGpu::onDraw(const DrawArgs& args, const GrDrawTarget::DrawInfo& info) {
cdaltond0a840d2015-03-16 17:19:58 -07001918 if (!this->flushGLState(args)) {
bsalomond95263c2014-12-16 13:05:12 -08001919 return;
1920 }
1921
joshualitt873ad0e2015-01-20 09:08:51 -08001922 size_t indexOffsetInBytes = 0;
1923 this->setupGeometry(*args.fPrimitiveProcessor, info, &indexOffsetInBytes);
reed@google.comac10a2d2010-12-22 21:39:39 +00001924
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +00001925 SkASSERT((size_t)info.primitiveType() < SK_ARRAY_COUNT(gPrimitiveType2GLMode));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001926
bsalomon@google.com74749cd2013-01-30 16:12:41 +00001927 if (info.isIndexed()) {
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00001928 GrGLvoid* indices =
1929 reinterpret_cast<GrGLvoid*>(indexOffsetInBytes + sizeof(uint16_t) * info.startIndex());
bsalomon@google.com74749cd2013-01-30 16:12:41 +00001930 // info.startVertex() was accounted for by setupGeometry.
1931 GL_CALL(DrawElements(gPrimitiveType2GLMode[info.primitiveType()],
1932 info.indexCount(),
1933 GR_GL_UNSIGNED_SHORT,
1934 indices));
1935 } else {
1936 // Pass 0 for parameter first. We have to adjust glVertexAttribPointer() to account for
1937 // startVertex in the DrawElements case. So we always rely on setupGeometry to have
1938 // accounted for startVertex.
1939 GL_CALL(DrawArrays(gPrimitiveType2GLMode[info.primitiveType()], 0, info.vertexCount()));
1940 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00001941#if SWAP_PER_DRAW
1942 glFlush();
commit-bot@chromium.org43823302013-09-25 20:57:51 +00001943 #if defined(SK_BUILD_FOR_MAC)
bsalomon@google.comd302f142011-03-03 13:54:13 +00001944 aglSwapBuffers(aglGetCurrentContext());
1945 int set_a_break_pt_here = 9;
1946 aglSwapBuffers(aglGetCurrentContext());
commit-bot@chromium.org43823302013-09-25 20:57:51 +00001947 #elif defined(SK_BUILD_FOR_WIN32)
bsalomon@google.comd302f142011-03-03 13:54:13 +00001948 SwapBuf();
1949 int set_a_break_pt_here = 9;
1950 SwapBuf();
1951 #endif
1952#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001953}
1954
bsalomon3e791242014-12-17 13:43:13 -08001955void GrGLGpu::onStencilPath(const GrPath* path, const StencilPathState& state) {
egdaniel080e6732014-12-22 07:35:52 -08001956 this->flushColorWrite(false);
egdaniel8dd688b2015-01-22 10:16:09 -08001957 this->flushDrawFace(GrPipelineBuilder::kBoth_DrawFace);
bsalomon3e791242014-12-17 13:43:13 -08001958
1959 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(state.fRenderTarget);
bsalomon1f78c0a2014-12-17 09:43:13 -08001960 SkISize size = SkISize::Make(rt->width(), rt->height());
bsalomon3e791242014-12-17 13:43:13 -08001961 this->glPathRendering()->setProjectionMatrix(*state.fViewMatrix, size, rt->origin());
1962 this->flushScissor(*state.fScissor, rt->getViewport(), rt->origin());
cdaltond0a840d2015-03-16 17:19:58 -07001963 this->flushHWAAState(rt, state.fUseHWAA);
bsalomon160f24c2015-03-17 15:55:42 -07001964 this->bindFBO(kDraw_FBOBinding, rt->renderFBO());
1965 this->setViewport(rt->getViewport());
bsalomon3e791242014-12-17 13:43:13 -08001966 fPathRendering->stencilPath(path, *state.fStencil);
bsalomond95263c2014-12-16 13:05:12 -08001967}
1968
joshualitt873ad0e2015-01-20 09:08:51 -08001969void GrGLGpu::onDrawPath(const DrawArgs& args, const GrPath* path,
bsalomond95263c2014-12-16 13:05:12 -08001970 const GrStencilSettings& stencil) {
cdaltond0a840d2015-03-16 17:19:58 -07001971 if (!this->flushGLState(args)) {
bsalomond95263c2014-12-16 13:05:12 -08001972 return;
1973 }
1974 fPathRendering->drawPath(path, stencil);
1975}
1976
joshualitt873ad0e2015-01-20 09:08:51 -08001977void GrGLGpu::onDrawPaths(const DrawArgs& args,
bsalomond95263c2014-12-16 13:05:12 -08001978 const GrPathRange* pathRange,
1979 const void* indices,
1980 GrDrawTarget::PathIndexType indexType,
1981 const float transformValues[],
1982 GrDrawTarget::PathTransformType transformType,
1983 int count,
1984 const GrStencilSettings& stencil) {
cdaltond0a840d2015-03-16 17:19:58 -07001985 if (!this->flushGLState(args)) {
bsalomond95263c2014-12-16 13:05:12 -08001986 return;
1987 }
1988 fPathRendering->drawPaths(pathRange, indices, indexType, transformValues,
1989 transformType, count, stencil);
1990}
1991
bsalomon861e1032014-12-16 07:33:49 -08001992void GrGLGpu::onResolveRenderTarget(GrRenderTarget* target) {
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001993 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001994 if (rt->needsResolve()) {
bsalomon@google.com347c3822013-05-01 20:10:01 +00001995 // Some extensions automatically resolves the texture when it is read.
1996 if (this->glCaps().usesMSAARenderBuffers()) {
bsalomon160f24c2015-03-17 15:55:42 -07001997 SkASSERT(rt->textureFBO() != rt->renderFBO());
1998 this->bindFBO(kBlitSrc_FBOBinding, rt->renderFBO());
1999 this->bindFBO(kBlitDst_FBOBinding, rt->textureFBO());
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002000 const GrGLIRect& vp = rt->getViewport();
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00002001 const SkIRect dirtyRect = rt->getResolveRect();
reed@google.comac10a2d2010-12-22 21:39:39 +00002002
bsalomon@google.com347c3822013-05-01 20:10:01 +00002003 if (GrGLCaps::kES_Apple_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002004 // Apple's extension uses the scissor as the blit bounds.
bsalomon3e791242014-12-17 13:43:13 -08002005 GrScissorState scissorState;
robertphillipse85a32d2015-02-10 08:16:55 -08002006 scissorState.set(dirtyRect);
2007 this->flushScissor(scissorState, vp, rt->origin());
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002008 GL_CALL(ResolveMultisampleFramebuffer());
2009 } else {
robertphillipse85a32d2015-02-10 08:16:55 -08002010 GrGLIRect r;
2011 r.setRelativeTo(vp, dirtyRect.fLeft, dirtyRect.fTop,
2012 dirtyRect.width(), dirtyRect.height(), target->origin());
2013
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002014 int right = r.fLeft + r.fWidth;
2015 int top = r.fBottom + r.fHeight;
derekf8c8f71a2014-09-16 06:24:57 -07002016
2017 // BlitFrameBuffer respects the scissor, so disable it.
joshualitt77b13072014-10-27 14:51:01 -07002018 this->disableScissor();
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002019 GL_CALL(BlitFramebuffer(r.fLeft, r.fBottom, right, top,
2020 r.fLeft, r.fBottom, right, top,
2021 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00002022 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002023 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00002024 rt->flagAsResolved();
reed@google.comac10a2d2010-12-22 21:39:39 +00002025 }
2026}
2027
bsalomon@google.com411dad02012-06-05 20:24:20 +00002028namespace {
bsalomon@google.comd302f142011-03-03 13:54:13 +00002029
bsalomon@google.com411dad02012-06-05 20:24:20 +00002030
2031GrGLenum gr_to_gl_stencil_op(GrStencilOp op) {
2032 static const GrGLenum gTable[] = {
2033 GR_GL_KEEP, // kKeep_StencilOp
2034 GR_GL_REPLACE, // kReplace_StencilOp
2035 GR_GL_INCR_WRAP, // kIncWrap_StencilOp
2036 GR_GL_INCR, // kIncClamp_StencilOp
2037 GR_GL_DECR_WRAP, // kDecWrap_StencilOp
2038 GR_GL_DECR, // kDecClamp_StencilOp
2039 GR_GL_ZERO, // kZero_StencilOp
2040 GR_GL_INVERT, // kInvert_StencilOp
2041 };
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +00002042 GR_STATIC_ASSERT(SK_ARRAY_COUNT(gTable) == kStencilOpCount);
bsalomon@google.com411dad02012-06-05 20:24:20 +00002043 GR_STATIC_ASSERT(0 == kKeep_StencilOp);
2044 GR_STATIC_ASSERT(1 == kReplace_StencilOp);
2045 GR_STATIC_ASSERT(2 == kIncWrap_StencilOp);
2046 GR_STATIC_ASSERT(3 == kIncClamp_StencilOp);
2047 GR_STATIC_ASSERT(4 == kDecWrap_StencilOp);
2048 GR_STATIC_ASSERT(5 == kDecClamp_StencilOp);
2049 GR_STATIC_ASSERT(6 == kZero_StencilOp);
2050 GR_STATIC_ASSERT(7 == kInvert_StencilOp);
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002051 SkASSERT((unsigned) op < kStencilOpCount);
bsalomon@google.com411dad02012-06-05 20:24:20 +00002052 return gTable[op];
2053}
2054
2055void set_gl_stencil(const GrGLInterface* gl,
bsalomon@google.coma3201942012-06-21 19:58:20 +00002056 const GrStencilSettings& settings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00002057 GrGLenum glFace,
bsalomon@google.coma3201942012-06-21 19:58:20 +00002058 GrStencilSettings::Face grFace) {
kkinnunenccdaa042014-08-20 01:36:23 -07002059 GrGLenum glFunc = GrToGLStencilFunc(settings.func(grFace));
bsalomon@google.coma3201942012-06-21 19:58:20 +00002060 GrGLenum glFailOp = gr_to_gl_stencil_op(settings.failOp(grFace));
2061 GrGLenum glPassOp = gr_to_gl_stencil_op(settings.passOp(grFace));
2062
2063 GrGLint ref = settings.funcRef(grFace);
2064 GrGLint mask = settings.funcMask(grFace);
2065 GrGLint writeMask = settings.writeMask(grFace);
bsalomon@google.com411dad02012-06-05 20:24:20 +00002066
2067 if (GR_GL_FRONT_AND_BACK == glFace) {
2068 // we call the combined func just in case separate stencil is not
2069 // supported.
2070 GR_GL_CALL(gl, StencilFunc(glFunc, ref, mask));
2071 GR_GL_CALL(gl, StencilMask(writeMask));
2072 GR_GL_CALL(gl, StencilOp(glFailOp, glPassOp, glPassOp));
2073 } else {
2074 GR_GL_CALL(gl, StencilFuncSeparate(glFace, glFunc, ref, mask));
2075 GR_GL_CALL(gl, StencilMaskSeparate(glFace, writeMask));
2076 GR_GL_CALL(gl, StencilOpSeparate(glFace, glFailOp, glPassOp, glPassOp));
2077 }
2078}
2079}
bsalomon@google.comd302f142011-03-03 13:54:13 +00002080
bsalomon3e791242014-12-17 13:43:13 -08002081void GrGLGpu::flushStencil(const GrStencilSettings& stencilSettings) {
2082 if (fHWStencilSettings != stencilSettings) {
joshualitta58fe352014-10-27 08:39:00 -07002083 if (stencilSettings.isDisabled()) {
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00002084 if (kNo_TriState != fHWStencilTestEnabled) {
2085 GL_CALL(Disable(GR_GL_STENCIL_TEST));
2086 fHWStencilTestEnabled = kNo_TriState;
2087 }
2088 } else {
2089 if (kYes_TriState != fHWStencilTestEnabled) {
2090 GL_CALL(Enable(GR_GL_STENCIL_TEST));
2091 fHWStencilTestEnabled = kYes_TriState;
2092 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00002093 }
joshualitta58fe352014-10-27 08:39:00 -07002094 if (!stencilSettings.isDisabled()) {
bsalomon@google.combcce8922013-03-25 15:38:39 +00002095 if (this->caps()->twoSidedStencilSupport()) {
bsalomon@google.com411dad02012-06-05 20:24:20 +00002096 set_gl_stencil(this->glInterface(),
joshualitta58fe352014-10-27 08:39:00 -07002097 stencilSettings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00002098 GR_GL_FRONT,
bsalomon@google.coma3201942012-06-21 19:58:20 +00002099 GrStencilSettings::kFront_Face);
bsalomon@google.com411dad02012-06-05 20:24:20 +00002100 set_gl_stencil(this->glInterface(),
joshualitta58fe352014-10-27 08:39:00 -07002101 stencilSettings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00002102 GR_GL_BACK,
bsalomon@google.coma3201942012-06-21 19:58:20 +00002103 GrStencilSettings::kBack_Face);
bsalomon@google.comd302f142011-03-03 13:54:13 +00002104 } else {
bsalomon@google.com411dad02012-06-05 20:24:20 +00002105 set_gl_stencil(this->glInterface(),
joshualitta58fe352014-10-27 08:39:00 -07002106 stencilSettings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00002107 GR_GL_FRONT_AND_BACK,
bsalomon@google.coma3201942012-06-21 19:58:20 +00002108 GrStencilSettings::kFront_Face);
bsalomon@google.comd302f142011-03-03 13:54:13 +00002109 }
2110 }
joshualitta58fe352014-10-27 08:39:00 -07002111 fHWStencilSettings = stencilSettings;
reed@google.comac10a2d2010-12-22 21:39:39 +00002112 }
2113}
2114
cdaltond0a840d2015-03-16 17:19:58 -07002115void GrGLGpu::flushHWAAState(GrRenderTarget* rt, bool useHWAA) {
2116 SkASSERT(!useHWAA || rt->isMultisampled());
bsalomon@google.com202d1392013-03-19 18:58:08 +00002117
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +00002118 if (kGL_GrGLStandard == this->glStandard()) {
cdaltond0a840d2015-03-16 17:19:58 -07002119 if (useHWAA) {
2120 if (kYes_TriState != fMSAAEnabled) {
2121 GL_CALL(Enable(GR_GL_MULTISAMPLE));
2122 fMSAAEnabled = kYes_TriState;
2123 }
2124 } else {
2125 if (kNo_TriState != fMSAAEnabled) {
2126 GL_CALL(Disable(GR_GL_MULTISAMPLE));
2127 fMSAAEnabled = kNo_TriState;
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00002128 }
2129 }
2130 }
2131}
2132
egdaniel080e6732014-12-22 07:35:52 -08002133void GrGLGpu::flushBlend(const GrXferProcessor::BlendInfo& blendInfo) {
egdanielb414f252014-07-29 13:15:47 -07002134 // Any optimization to disable blending should have already been applied and
2135 // tweaked the coeffs to (1, 0).
egdanielc2304142014-12-11 13:15:13 -08002136
egdanielc2304142014-12-11 13:15:13 -08002137 GrBlendCoeff srcCoeff = blendInfo.fSrcBlend;
2138 GrBlendCoeff dstCoeff = blendInfo.fDstBlend;
egdanielb414f252014-07-29 13:15:47 -07002139 bool blendOff = kOne_GrBlendCoeff == srcCoeff && kZero_GrBlendCoeff == dstCoeff;
2140 if (blendOff) {
2141 if (kNo_TriState != fHWBlendState.fEnabled) {
2142 GL_CALL(Disable(GR_GL_BLEND));
2143 fHWBlendState.fEnabled = kNo_TriState;
2144 }
2145 } else {
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002146 if (kYes_TriState != fHWBlendState.fEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002147 GL_CALL(Enable(GR_GL_BLEND));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002148 fHWBlendState.fEnabled = kYes_TriState;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002149 }
egdanielb414f252014-07-29 13:15:47 -07002150 if (fHWBlendState.fSrcCoeff != srcCoeff ||
2151 fHWBlendState.fDstCoeff != dstCoeff) {
2152 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
2153 gXfermodeCoeff2Blend[dstCoeff]));
2154 fHWBlendState.fSrcCoeff = srcCoeff;
2155 fHWBlendState.fDstCoeff = dstCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002156 }
egdanielc2304142014-12-11 13:15:13 -08002157 GrColor blendConst = blendInfo.fBlendConstant;
egdanielb414f252014-07-29 13:15:47 -07002158 if ((BlendCoeffReferencesConstant(srcCoeff) ||
2159 BlendCoeffReferencesConstant(dstCoeff)) &&
2160 (!fHWBlendState.fConstColorValid ||
2161 fHWBlendState.fConstColor != blendConst)) {
2162 GrGLfloat c[4];
2163 GrColorToRGBAFloat(blendConst, c);
2164 GL_CALL(BlendColor(c[0], c[1], c[2], c[3]));
2165 fHWBlendState.fConstColor = blendConst;
2166 fHWBlendState.fConstColorValid = true;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002167 }
2168 }
2169}
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002170
commit-bot@chromium.org6364b5e2013-08-20 20:22:52 +00002171static inline GrGLenum tile_to_gl_wrap(SkShader::TileMode tm) {
bsalomon@google.comb8670992012-07-25 21:27:09 +00002172 static const GrGLenum gWrapModes[] = {
2173 GR_GL_CLAMP_TO_EDGE,
2174 GR_GL_REPEAT,
2175 GR_GL_MIRRORED_REPEAT
2176 };
commit-bot@chromium.org5d7ca952013-04-22 20:26:44 +00002177 GR_STATIC_ASSERT(SkShader::kTileModeCount == SK_ARRAY_COUNT(gWrapModes));
bsalomon@google.comb8670992012-07-25 21:27:09 +00002178 GR_STATIC_ASSERT(0 == SkShader::kClamp_TileMode);
2179 GR_STATIC_ASSERT(1 == SkShader::kRepeat_TileMode);
2180 GR_STATIC_ASSERT(2 == SkShader::kMirror_TileMode);
2181 return gWrapModes[tm];
2182}
2183
bsalomon861e1032014-12-16 07:33:49 -08002184void GrGLGpu::bindTexture(int unitIdx, const GrTextureParams& params, GrGLTexture* texture) {
bsalomon49f085d2014-09-05 13:34:00 -07002185 SkASSERT(texture);
tomhudson@google.comd0c1a062012-07-12 17:23:52 +00002186
bsalomon@google.comb8670992012-07-25 21:27:09 +00002187 // If we created a rt/tex and rendered to it without using a texture and now we're texturing
2188 // 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 +00002189 // out of the "last != next" check.
bsalomon37dd3312014-11-03 08:47:23 -08002190 GrGLRenderTarget* texRT = static_cast<GrGLRenderTarget*>(texture->asRenderTarget());
bsalomon49f085d2014-09-05 13:34:00 -07002191 if (texRT) {
bsalomon@google.com4c883782012-06-04 19:05:11 +00002192 this->onResolveRenderTarget(texRT);
2193 }
2194
bsalomon1c63bf62014-07-22 13:09:46 -07002195 uint32_t textureID = texture->getUniqueID();
2196 if (fHWBoundTextureUniqueIDs[unitIdx] != textureID) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002197 this->setTextureUnit(unitIdx);
2198 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, texture->textureID()));
bsalomon1c63bf62014-07-22 13:09:46 -07002199 fHWBoundTextureUniqueIDs[unitIdx] = textureID;
bsalomon@google.com4c883782012-06-04 19:05:11 +00002200 }
2201
bsalomon@google.com4c883782012-06-04 19:05:11 +00002202 ResetTimestamp timestamp;
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002203 const GrGLTexture::TexParams& oldTexParams = texture->getCachedTexParams(&timestamp);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002204 bool setAll = timestamp < this->getResetTimestamp();
2205 GrGLTexture::TexParams newTexParams;
2206
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00002207 static GrGLenum glMinFilterModes[] = {
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +00002208 GR_GL_NEAREST,
2209 GR_GL_LINEAR,
2210 GR_GL_LINEAR_MIPMAP_LINEAR
2211 };
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00002212 static GrGLenum glMagFilterModes[] = {
2213 GR_GL_NEAREST,
2214 GR_GL_LINEAR,
2215 GR_GL_LINEAR
2216 };
commit-bot@chromium.org47442312013-12-19 16:18:01 +00002217 GrTextureParams::FilterMode filterMode = params.filterMode();
bsalomonefd7d452014-10-23 14:17:46 -07002218
2219 if (GrTextureParams::kMipMap_FilterMode == filterMode) {
2220 if (!this->caps()->mipMapSupport() || GrPixelConfigIsCompressed(texture->config())) {
2221 filterMode = GrTextureParams::kBilerp_FilterMode;
2222 }
commit-bot@chromium.org47442312013-12-19 16:18:01 +00002223 }
bsalomonefd7d452014-10-23 14:17:46 -07002224
commit-bot@chromium.org47442312013-12-19 16:18:01 +00002225 newTexParams.fMinFilter = glMinFilterModes[filterMode];
2226 newTexParams.fMagFilter = glMagFilterModes[filterMode];
skia.committer@gmail.comaeefb2a2013-07-27 07:01:06 +00002227
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +00002228 if (GrTextureParams::kMipMap_FilterMode == filterMode &&
bsalomonefd7d452014-10-23 14:17:46 -07002229 texture->texturePriv().mipMapsAreDirty()) {
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +00002230 GL_CALL(GenerateMipmap(GR_GL_TEXTURE_2D));
bsalomonafbf2d62014-09-30 12:18:44 -07002231 texture->texturePriv().dirtyMipMaps(false);
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +00002232 }
bsalomon@google.com4c883782012-06-04 19:05:11 +00002233
bsalomon@google.comb8670992012-07-25 21:27:09 +00002234 newTexParams.fWrapS = tile_to_gl_wrap(params.getTileModeX());
2235 newTexParams.fWrapT = tile_to_gl_wrap(params.getTileModeY());
bsalomon@google.com4c883782012-06-04 19:05:11 +00002236 memcpy(newTexParams.fSwizzleRGBA,
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002237 GrGLShaderBuilder::GetTexParamSwizzle(texture->config(), this->glCaps()),
bsalomon@google.com4c883782012-06-04 19:05:11 +00002238 sizeof(newTexParams.fSwizzleRGBA));
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00002239 if (setAll || newTexParams.fMagFilter != oldTexParams.fMagFilter) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002240 this->setTextureUnit(unitIdx);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002241 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2242 GR_GL_TEXTURE_MAG_FILTER,
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00002243 newTexParams.fMagFilter));
2244 }
2245 if (setAll || newTexParams.fMinFilter != oldTexParams.fMinFilter) {
2246 this->setTextureUnit(unitIdx);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002247 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2248 GR_GL_TEXTURE_MIN_FILTER,
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00002249 newTexParams.fMinFilter));
bsalomon@google.com4c883782012-06-04 19:05:11 +00002250 }
2251 if (setAll || newTexParams.fWrapS != oldTexParams.fWrapS) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002252 this->setTextureUnit(unitIdx);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002253 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2254 GR_GL_TEXTURE_WRAP_S,
2255 newTexParams.fWrapS));
2256 }
2257 if (setAll || newTexParams.fWrapT != oldTexParams.fWrapT) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002258 this->setTextureUnit(unitIdx);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002259 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2260 GR_GL_TEXTURE_WRAP_T,
2261 newTexParams.fWrapT));
2262 }
2263 if (this->glCaps().textureSwizzleSupport() &&
2264 (setAll || memcmp(newTexParams.fSwizzleRGBA,
2265 oldTexParams.fSwizzleRGBA,
2266 sizeof(newTexParams.fSwizzleRGBA)))) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002267 this->setTextureUnit(unitIdx);
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +00002268 if (this->glStandard() == kGLES_GrGLStandard) {
commit-bot@chromium.org6364b5e2013-08-20 20:22:52 +00002269 // ES3 added swizzle support but not GL_TEXTURE_SWIZZLE_RGBA.
2270 const GrGLenum* swizzle = newTexParams.fSwizzleRGBA;
2271 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_SWIZZLE_R, swizzle[0]));
2272 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_SWIZZLE_G, swizzle[1]));
2273 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_SWIZZLE_B, swizzle[2]));
2274 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_SWIZZLE_A, swizzle[3]));
2275 } else {
2276 GR_STATIC_ASSERT(sizeof(newTexParams.fSwizzleRGBA[0]) == sizeof(GrGLint));
2277 const GrGLint* swizzle = reinterpret_cast<const GrGLint*>(newTexParams.fSwizzleRGBA);
2278 GL_CALL(TexParameteriv(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_SWIZZLE_RGBA, swizzle));
2279 }
bsalomon@google.com4c883782012-06-04 19:05:11 +00002280 }
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002281 texture->setCachedTexParams(newTexParams, this->getResetTimestamp());
bsalomon@google.com4c883782012-06-04 19:05:11 +00002282}
2283
bsalomon3e791242014-12-17 13:43:13 -08002284void GrGLGpu::flushDither(bool dither) {
2285 if (dither) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002286 if (kYes_TriState != fHWDitherEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002287 GL_CALL(Enable(GR_GL_DITHER));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002288 fHWDitherEnabled = kYes_TriState;
2289 }
2290 } else {
2291 if (kNo_TriState != fHWDitherEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002292 GL_CALL(Disable(GR_GL_DITHER));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002293 fHWDitherEnabled = kNo_TriState;
reed@google.comac10a2d2010-12-22 21:39:39 +00002294 }
2295 }
bsalomon3e791242014-12-17 13:43:13 -08002296}
reed@google.comac10a2d2010-12-22 21:39:39 +00002297
egdaniel080e6732014-12-22 07:35:52 -08002298void GrGLGpu::flushColorWrite(bool writeColor) {
2299 if (!writeColor) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002300 if (kNo_TriState != fHWWriteToColor) {
2301 GL_CALL(ColorMask(GR_GL_FALSE, GR_GL_FALSE,
2302 GR_GL_FALSE, GR_GL_FALSE));
2303 fHWWriteToColor = kNo_TriState;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002304 }
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002305 } else {
2306 if (kYes_TriState != fHWWriteToColor) {
2307 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
2308 fHWWriteToColor = kYes_TriState;
2309 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00002310 }
bsalomon3e791242014-12-17 13:43:13 -08002311}
bsalomon@google.comd302f142011-03-03 13:54:13 +00002312
egdaniel8dd688b2015-01-22 10:16:09 -08002313void GrGLGpu::flushDrawFace(GrPipelineBuilder::DrawFace face) {
bsalomon3e791242014-12-17 13:43:13 -08002314 if (fHWDrawFace != face) {
2315 switch (face) {
egdaniel8dd688b2015-01-22 10:16:09 -08002316 case GrPipelineBuilder::kCCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002317 GL_CALL(Enable(GR_GL_CULL_FACE));
2318 GL_CALL(CullFace(GR_GL_BACK));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002319 break;
egdaniel8dd688b2015-01-22 10:16:09 -08002320 case GrPipelineBuilder::kCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002321 GL_CALL(Enable(GR_GL_CULL_FACE));
2322 GL_CALL(CullFace(GR_GL_FRONT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002323 break;
egdaniel8dd688b2015-01-22 10:16:09 -08002324 case GrPipelineBuilder::kBoth_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002325 GL_CALL(Disable(GR_GL_CULL_FACE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002326 break;
2327 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +00002328 SkFAIL("Unknown draw face.");
bsalomon@google.comd302f142011-03-03 13:54:13 +00002329 }
bsalomon3e791242014-12-17 13:43:13 -08002330 fHWDrawFace = face;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002331 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002332}
2333
bsalomon861e1032014-12-16 07:33:49 -08002334bool GrGLGpu::configToGLFormats(GrPixelConfig config,
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002335 bool getSizedInternalFormat,
2336 GrGLenum* internalFormat,
2337 GrGLenum* externalFormat,
2338 GrGLenum* externalType) {
2339 GrGLenum dontCare;
2340 if (NULL == internalFormat) {
2341 internalFormat = &dontCare;
2342 }
2343 if (NULL == externalFormat) {
2344 externalFormat = &dontCare;
2345 }
2346 if (NULL == externalType) {
2347 externalType = &dontCare;
2348 }
2349
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +00002350 if(!this->glCaps().isConfigTexturable(config)) {
2351 return false;
2352 }
2353
reed@google.comac10a2d2010-12-22 21:39:39 +00002354 switch (config) {
bsalomon@google.com0342a852012-08-20 19:22:38 +00002355 case kRGBA_8888_GrPixelConfig:
bsalomon@google.comc4364992011-11-07 15:54:49 +00002356 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002357 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002358 if (getSizedInternalFormat) {
2359 *internalFormat = GR_GL_RGBA8;
2360 } else {
2361 *internalFormat = GR_GL_RGBA;
2362 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002363 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.comc4364992011-11-07 15:54:49 +00002364 break;
bsalomon@google.com0342a852012-08-20 19:22:38 +00002365 case kBGRA_8888_GrPixelConfig:
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002366 if (this->glCaps().bgraIsInternalFormat()) {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002367 if (getSizedInternalFormat) {
2368 *internalFormat = GR_GL_BGRA8;
2369 } else {
2370 *internalFormat = GR_GL_BGRA;
2371 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002372 } else {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002373 if (getSizedInternalFormat) {
2374 *internalFormat = GR_GL_RGBA8;
2375 } else {
2376 *internalFormat = GR_GL_RGBA;
2377 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002378 }
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002379 *externalFormat = GR_GL_BGRA;
bsalomon@google.com6f379512011-11-16 20:36:03 +00002380 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002381 break;
jvanverthfa1e8a72014-12-22 08:31:49 -08002382 case kSRGBA_8888_GrPixelConfig:
2383 *internalFormat = GR_GL_SRGB_ALPHA;
2384 *externalFormat = GR_GL_SRGB_ALPHA;
2385 if (getSizedInternalFormat) {
2386 *internalFormat = GR_GL_SRGB8_ALPHA8;
2387 } else {
2388 *internalFormat = GR_GL_SRGB_ALPHA;
2389 }
2390 *externalType = GR_GL_UNSIGNED_BYTE;
2391 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002392 case kRGB_565_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002393 *internalFormat = GR_GL_RGB;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002394 *externalFormat = GR_GL_RGB;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002395 if (getSizedInternalFormat) {
jvanverth3f801cb2014-12-16 09:49:38 -08002396 if (!this->glCaps().ES2CompatibilitySupport()) {
2397 *internalFormat = GR_GL_RGB5;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002398 } else {
2399 *internalFormat = GR_GL_RGB565;
2400 }
2401 } else {
2402 *internalFormat = GR_GL_RGB;
2403 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002404 *externalType = GR_GL_UNSIGNED_SHORT_5_6_5;
reed@google.comac10a2d2010-12-22 21:39:39 +00002405 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002406 case kRGBA_4444_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002407 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002408 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002409 if (getSizedInternalFormat) {
2410 *internalFormat = GR_GL_RGBA4;
2411 } else {
2412 *internalFormat = GR_GL_RGBA;
2413 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002414 *externalType = GR_GL_UNSIGNED_SHORT_4_4_4_4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002415 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002416 case kIndex_8_GrPixelConfig:
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +00002417 // no sized/unsized internal format distinction here
2418 *internalFormat = GR_GL_PALETTE8_RGBA8;
reed@google.comac10a2d2010-12-22 21:39:39 +00002419 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002420 case kAlpha_8_GrPixelConfig:
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002421 if (this->glCaps().textureRedSupport()) {
2422 *internalFormat = GR_GL_RED;
2423 *externalFormat = GR_GL_RED;
2424 if (getSizedInternalFormat) {
2425 *internalFormat = GR_GL_R8;
2426 } else {
2427 *internalFormat = GR_GL_RED;
2428 }
2429 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002430 } else {
2431 *internalFormat = GR_GL_ALPHA;
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002432 *externalFormat = GR_GL_ALPHA;
2433 if (getSizedInternalFormat) {
2434 *internalFormat = GR_GL_ALPHA8;
2435 } else {
2436 *internalFormat = GR_GL_ALPHA;
2437 }
2438 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002439 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002440 break;
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +00002441 case kETC1_GrPixelConfig:
2442 *internalFormat = GR_GL_COMPRESSED_RGB8_ETC1;
2443 break;
2444 case kLATC_GrPixelConfig:
2445 switch(this->glCaps().latcAlias()) {
2446 case GrGLCaps::kLATC_LATCAlias:
2447 *internalFormat = GR_GL_COMPRESSED_LUMINANCE_LATC1;
2448 break;
2449 case GrGLCaps::kRGTC_LATCAlias:
2450 *internalFormat = GR_GL_COMPRESSED_RED_RGTC1;
2451 break;
2452 case GrGLCaps::k3DC_LATCAlias:
2453 *internalFormat = GR_GL_COMPRESSED_3DC_X;
2454 break;
2455 }
2456 break;
krajcevski238b4562014-06-30 09:09:22 -07002457 case kR11_EAC_GrPixelConfig:
2458 *internalFormat = GR_GL_COMPRESSED_R11;
2459 break;
krajcevski7ef21622014-07-16 15:21:13 -07002460
2461 case kASTC_12x12_GrPixelConfig:
2462 *internalFormat = GR_GL_COMPRESSED_RGBA_ASTC_12x12;
2463 break;
2464
joshualittee5da552014-07-16 13:32:56 -07002465 case kRGBA_float_GrPixelConfig:
2466 *internalFormat = GR_GL_RGBA32F;
2467 *externalFormat = GR_GL_RGBA;
2468 *externalType = GR_GL_FLOAT;
2469 break;
krajcevski7ef21622014-07-16 15:21:13 -07002470
jvanverth28f9c602014-12-05 13:06:35 -08002471 case kAlpha_half_GrPixelConfig:
jvanverth1334c212014-12-18 05:44:55 -08002472 if (this->glCaps().textureRedSupport()) {
2473 if (getSizedInternalFormat) {
2474 *internalFormat = GR_GL_R16F;
2475 } else {
2476 *internalFormat = GR_GL_RED;
2477 }
jvanverth28f9c602014-12-05 13:06:35 -08002478 *externalFormat = GR_GL_RED;
jvanverth1334c212014-12-18 05:44:55 -08002479 } else {
2480 if (getSizedInternalFormat) {
2481 *internalFormat = GR_GL_ALPHA16F;
2482 } else {
2483 *internalFormat = GR_GL_ALPHA;
2484 }
2485 *externalFormat = GR_GL_ALPHA;
2486 }
2487 if (kGL_GrGLStandard == this->glStandard() || this->glVersion() >= GR_GL_VER(3, 0)) {
jvanvertha60b2ea2014-12-12 05:58:06 -08002488 *externalType = GR_GL_HALF_FLOAT;
jvanverth28f9c602014-12-05 13:06:35 -08002489 } else {
jvanverth1334c212014-12-18 05:44:55 -08002490 *externalType = GR_GL_HALF_FLOAT_OES;
jvanverth28f9c602014-12-05 13:06:35 -08002491 }
2492 break;
2493
reed@google.comac10a2d2010-12-22 21:39:39 +00002494 default:
2495 return false;
2496 }
2497 return true;
2498}
2499
bsalomon861e1032014-12-16 07:33:49 -08002500void GrGLGpu::setTextureUnit(int unit) {
bsalomon1c63bf62014-07-22 13:09:46 -07002501 SkASSERT(unit >= 0 && unit < fHWBoundTextureUniqueIDs.count());
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002502 if (unit != fHWActiveTextureUnitIdx) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002503 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com49209392012-06-05 15:13:46 +00002504 fHWActiveTextureUnitIdx = unit;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002505 }
2506}
bsalomon@google.com316f99232011-01-13 21:28:12 +00002507
bsalomon861e1032014-12-16 07:33:49 -08002508void GrGLGpu::setScratchTextureUnit() {
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002509 // Bind the last texture unit since it is the least likely to be used by GrGLProgram.
bsalomon1c63bf62014-07-22 13:09:46 -07002510 int lastUnitIdx = fHWBoundTextureUniqueIDs.count() - 1;
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002511 if (lastUnitIdx != fHWActiveTextureUnitIdx) {
2512 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + lastUnitIdx));
2513 fHWActiveTextureUnitIdx = lastUnitIdx;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002514 }
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002515 // clear out the this field so that if a program does use this unit it will rebind the correct
2516 // texture.
bsalomon1c63bf62014-07-22 13:09:46 -07002517 fHWBoundTextureUniqueIDs[lastUnitIdx] = SK_InvalidUniqueID;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002518}
2519
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002520namespace {
2521// Determines whether glBlitFramebuffer could be used between src and dst.
bsalomon@google.comeb851172013-04-15 13:51:00 +00002522inline bool can_blit_framebuffer(const GrSurface* dst,
2523 const GrSurface* src,
egdaniel0f5f9672015-02-03 11:10:51 -08002524 const GrGLGpu* gpu) {
commit-bot@chromium.org6b7938f2013-10-15 14:18:16 +00002525 if (gpu->glCaps().isConfigRenderable(dst->config(), dst->desc().fSampleCnt > 0) &&
2526 gpu->glCaps().isConfigRenderable(src->config(), src->desc().fSampleCnt > 0) &&
bsalomon@google.com347c3822013-05-01 20:10:01 +00002527 gpu->glCaps().usesMSAARenderBuffers()) {
commit-bot@chromium.orga8e5a062013-09-05 23:44:09 +00002528 // ES3 doesn't allow framebuffer blits when the src has MSAA and the configs don't match
2529 // or the rects are not the same (not just the same size but have the same edges).
2530 if (GrGLCaps::kES_3_0_MSFBOType == gpu->glCaps().msFBOType() &&
2531 (src->desc().fSampleCnt > 0 || src->config() != dst->config())) {
2532 return false;
2533 }
bsalomon@google.comeb851172013-04-15 13:51:00 +00002534 return true;
2535 } else {
2536 return false;
2537 }
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002538}
bsalomon@google.comeb851172013-04-15 13:51:00 +00002539
2540inline bool can_copy_texsubimage(const GrSurface* dst,
2541 const GrSurface* src,
egdaniel0f5f9672015-02-03 11:10:51 -08002542 const GrGLGpu* gpu) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002543 // Table 3.9 of the ES2 spec indicates the supported formats with CopyTexSubImage
2544 // and BGRA isn't in the spec. There doesn't appear to be any extension that adds it. Perhaps
2545 // many drivers would allow it to work, but ANGLE does not.
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +00002546 if (kGLES_GrGLStandard == gpu->glStandard() && gpu->glCaps().bgraIsInternalFormat() &&
bsalomon@google.comeb851172013-04-15 13:51:00 +00002547 (kBGRA_8888_GrPixelConfig == dst->config() || kBGRA_8888_GrPixelConfig == src->config())) {
2548 return false;
2549 }
2550 const GrGLRenderTarget* dstRT = static_cast<const GrGLRenderTarget*>(dst->asRenderTarget());
2551 // If dst is multisampled (and uses an extension where there is a separate MSAA renderbuffer)
2552 // then we don't want to copy to the texture but to the MSAA buffer.
bsalomon160f24c2015-03-17 15:55:42 -07002553 if (dstRT && dstRT->renderFBO() != dstRT->textureFBO()) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002554 return false;
2555 }
bsalomon@google.coma2719852013-04-17 14:25:27 +00002556 const GrGLRenderTarget* srcRT = static_cast<const GrGLRenderTarget*>(src->asRenderTarget());
2557 // If the src is multisampled (and uses an extension where there is a separate MSAA
2558 // renderbuffer) then it is an invalid operation to call CopyTexSubImage
bsalomon160f24c2015-03-17 15:55:42 -07002559 if (srcRT && srcRT->renderFBO() != srcRT->textureFBO()) {
bsalomon@google.coma2719852013-04-17 14:25:27 +00002560 return false;
2561 }
commit-bot@chromium.org6b7938f2013-10-15 14:18:16 +00002562 if (gpu->glCaps().isConfigRenderable(src->config(), src->desc().fSampleCnt > 0) &&
bsalomon49f085d2014-09-05 13:34:00 -07002563 dst->asTexture() &&
commit-bot@chromium.org6b7938f2013-10-15 14:18:16 +00002564 dst->origin() == src->origin() &&
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +00002565 !GrPixelConfigIsCompressed(src->config())) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002566 return true;
2567 } else {
2568 return false;
2569 }
2570}
2571
robertphillips754f4e92014-09-18 13:52:08 -07002572}
2573
bsalomon@google.comeb851172013-04-15 13:51:00 +00002574// If a temporary FBO was created, its non-zero ID is returned. The viewport that the copy rect is
2575// relative to is output.
bsalomon160f24c2015-03-17 15:55:42 -07002576GrGLGpu::FBOBinding GrGLGpu::bindSurfaceAsFBOForCopy(GrSurface* surface, FBOBinding binding,
2577 GrGLIRect* viewport) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002578 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(surface->asRenderTarget());
bsalomon@google.comeb851172013-04-15 13:51:00 +00002579 if (NULL == rt) {
bsalomon49f085d2014-09-05 13:34:00 -07002580 SkASSERT(surface->asTexture());
bsalomon@google.comeb851172013-04-15 13:51:00 +00002581 GrGLuint texID = static_cast<GrGLTexture*>(surface->asTexture())->textureID();
bsalomon160f24c2015-03-17 15:55:42 -07002582 GrGLFBO* tempFBO;
egdaniel0f5f9672015-02-03 11:10:51 -08002583
bsalomon160f24c2015-03-17 15:55:42 -07002584 if (kBlitSrc_FBOBinding == binding || kCopyTexSrc_FBOBinding == binding) {
2585 if (!fTempSrcFBO) {
2586 fTempSrcFBO.reset(SkNEW_ARGS(GrGLFBO, (this->glInterface())));
2587 SkASSERT(fTempSrcFBO->isValid());
2588 }
2589 tempFBO = fTempSrcFBO;
2590 } else {
2591 SkASSERT(kBlitDst_FBOBinding == binding);
2592 if (!fTempDstFBO) {
2593 fTempDstFBO.reset(SkNEW_ARGS(GrGLFBO, (this->glInterface())));
2594 SkASSERT(fTempDstFBO->isValid());
2595 }
2596 tempFBO = fTempDstFBO;
egdaniel0f5f9672015-02-03 11:10:51 -08002597 }
2598
bsalomon160f24c2015-03-17 15:55:42 -07002599 GrGLenum target = this->bindFBO(binding, tempFBO);
2600 GR_GL_CALL(this->glInterface(), FramebufferTexture2D(target,
robertphillips754f4e92014-09-18 13:52:08 -07002601 GR_GL_COLOR_ATTACHMENT0,
2602 GR_GL_TEXTURE_2D,
2603 texID,
2604 0));
bsalomon@google.comeb851172013-04-15 13:51:00 +00002605 viewport->fLeft = 0;
2606 viewport->fBottom = 0;
2607 viewport->fWidth = surface->width();
2608 viewport->fHeight = surface->height();
bsalomon160f24c2015-03-17 15:55:42 -07002609 return binding;
bsalomon@google.comeb851172013-04-15 13:51:00 +00002610 } else {
bsalomon160f24c2015-03-17 15:55:42 -07002611 this->bindFBO(binding, rt->renderFBO());
bsalomon@google.comeb851172013-04-15 13:51:00 +00002612 *viewport = rt->getViewport();
bsalomon160f24c2015-03-17 15:55:42 -07002613 return kInvalidFBOBinding;
bsalomon@google.comeb851172013-04-15 13:51:00 +00002614 }
egdaniel0f5f9672015-02-03 11:10:51 -08002615}
2616
bsalomon160f24c2015-03-17 15:55:42 -07002617void GrGLGpu::unbindSurfaceAsFBOForCopy(FBOBinding binding) {
2618 if (kInvalidFBOBinding == binding) {
2619 return;
2620 }
2621 GrGLFBO* tempFBO = kBlitDst_FBOBinding == binding ? fTempSrcFBO : fTempDstFBO;
2622 GrGLenum target = this->bindFBO(binding, tempFBO);
2623 GR_GL_CALL(this->glInterface(), FramebufferTexture2D(target,
egdaniel0f5f9672015-02-03 11:10:51 -08002624 GR_GL_COLOR_ATTACHMENT0,
2625 GR_GL_TEXTURE_2D,
2626 0,
2627 0));
bsalomon@google.comeb851172013-04-15 13:51:00 +00002628}
2629
bsalomon861e1032014-12-16 07:33:49 -08002630bool GrGLGpu::initCopySurfaceDstDesc(const GrSurface* src, GrSurfaceDesc* desc) {
bsalomonf90a02b2014-11-26 12:28:00 -08002631 // In here we look for opportunities to use CopyTexSubImage, or fbo blit. If neither are
2632 // possible and we return false to fallback to creating a render target dst for render-to-
2633 // texture. This code prefers CopyTexSubImage to fbo blit and avoids triggering temporary fbo
2634 // creation. It isn't clear that avoiding temporary fbo creation is actually optimal.
2635
bsalomon@google.comeb851172013-04-15 13:51:00 +00002636 // Check for format issues with glCopyTexSubImage2D
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +00002637 if (kGLES_GrGLStandard == this->glStandard() && this->glCaps().bgraIsInternalFormat() &&
bsalomon@google.comeb851172013-04-15 13:51:00 +00002638 kBGRA_8888_GrPixelConfig == src->config()) {
bsalomonf90a02b2014-11-26 12:28:00 -08002639 // glCopyTexSubImage2D doesn't work with this config. If the bgra can be used with fbo blit
2640 // then we set up for that, otherwise fail.
2641 if (this->caps()->isConfigRenderable(kBGRA_8888_GrPixelConfig, false)) {
2642 desc->fOrigin = kDefault_GrSurfaceOrigin;
bsalomon6bc1b5f2015-02-23 09:06:38 -08002643 desc->fFlags = kRenderTarget_GrSurfaceFlag;
bsalomonf90a02b2014-11-26 12:28:00 -08002644 desc->fConfig = kBGRA_8888_GrPixelConfig;
2645 return true;
2646 }
2647 return false;
bsalomon@google.comeb851172013-04-15 13:51:00 +00002648 } else if (NULL == src->asRenderTarget()) {
bsalomonf90a02b2014-11-26 12:28:00 -08002649 // CopyTexSubImage2D or fbo blit would require creating a temp fbo for the src.
2650 return false;
bsalomon@google.coma2719852013-04-17 14:25:27 +00002651 }
2652
2653 const GrGLRenderTarget* srcRT = static_cast<const GrGLRenderTarget*>(src->asRenderTarget());
bsalomon160f24c2015-03-17 15:55:42 -07002654 if (srcRT && srcRT->renderFBO() != srcRT->textureFBO()) {
bsalomonf90a02b2014-11-26 12:28:00 -08002655 // It's illegal to call CopyTexSubImage2D on a MSAA renderbuffer. Set up for FBO blit or
2656 // fail.
2657 if (this->caps()->isConfigRenderable(src->config(), false)) {
2658 desc->fOrigin = kDefault_GrSurfaceOrigin;
bsalomon6bc1b5f2015-02-23 09:06:38 -08002659 desc->fFlags = kRenderTarget_GrSurfaceFlag;
bsalomonf90a02b2014-11-26 12:28:00 -08002660 desc->fConfig = src->config();
2661 return true;
2662 }
2663 return false;
bsalomon@google.comeb851172013-04-15 13:51:00 +00002664 }
bsalomonf90a02b2014-11-26 12:28:00 -08002665
2666 // We'll do a CopyTexSubImage. Make the dst a plain old texture.
2667 desc->fConfig = src->config();
2668 desc->fOrigin = src->origin();
2669 desc->fFlags = kNone_GrSurfaceFlags;
2670 return true;
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002671}
2672
bsalomon861e1032014-12-16 07:33:49 -08002673bool GrGLGpu::copySurface(GrSurface* dst,
joshualitta7024152014-11-03 14:16:35 -08002674 GrSurface* src,
2675 const SkIRect& srcRect,
2676 const SkIPoint& dstPoint) {
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002677 bool copied = false;
bsalomon160f24c2015-03-17 15:55:42 -07002678 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
2679 srcRect.width(), srcRect.height());
joshualitta7024152014-11-03 14:16:35 -08002680 if (can_copy_texsubimage(dst, src, this)) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002681 GrGLIRect srcVP;
bsalomon160f24c2015-03-17 15:55:42 -07002682 FBOBinding srcFBOBinding = this->bindSurfaceAsFBOForCopy(src, kCopyTexSrc_FBOBinding,
2683 &srcVP);
bsalomon@google.comeb851172013-04-15 13:51:00 +00002684 GrGLTexture* dstTex = static_cast<GrGLTexture*>(dst->asTexture());
bsalomon49f085d2014-09-05 13:34:00 -07002685 SkASSERT(dstTex);
bsalomon@google.comeb851172013-04-15 13:51:00 +00002686 GrGLIRect srcGLRect;
2687 srcGLRect.setRelativeTo(srcVP,
2688 srcRect.fLeft,
2689 srcRect.fTop,
2690 srcRect.width(),
2691 srcRect.height(),
2692 src->origin());
2693
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002694 this->setScratchTextureUnit();
bsalomon@google.comeb851172013-04-15 13:51:00 +00002695 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, dstTex->textureID()));
2696 GrGLint dstY;
2697 if (kBottomLeft_GrSurfaceOrigin == dst->origin()) {
2698 dstY = dst->height() - (dstPoint.fY + srcGLRect.fHeight);
2699 } else {
2700 dstY = dstPoint.fY;
2701 }
2702 GL_CALL(CopyTexSubImage2D(GR_GL_TEXTURE_2D, 0,
2703 dstPoint.fX, dstY,
2704 srcGLRect.fLeft, srcGLRect.fBottom,
2705 srcGLRect.fWidth, srcGLRect.fHeight));
2706 copied = true;
bsalomon160f24c2015-03-17 15:55:42 -07002707 this->unbindSurfaceAsFBOForCopy(srcFBOBinding);
joshualitta7024152014-11-03 14:16:35 -08002708 } else if (can_blit_framebuffer(dst, src, this)) {
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002709 bool selfOverlap = false;
bsalomona2c23232014-11-25 07:41:12 -08002710 if (dst == src) {
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002711 selfOverlap = SkIRect::IntersectsNoEmptyCheck(dstRect, srcRect);
2712 }
2713
2714 if (!selfOverlap) {
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002715 GrGLIRect dstVP;
2716 GrGLIRect srcVP;
bsalomon160f24c2015-03-17 15:55:42 -07002717 FBOBinding dstFBOBinding = this->bindSurfaceAsFBOForCopy(dst, kBlitDst_FBOBinding,
2718 &dstVP);
2719 FBOBinding srcFBOBinding = this->bindSurfaceAsFBOForCopy(src, kBlitSrc_FBOBinding,
2720 &srcVP);
2721
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002722 GrGLIRect srcGLRect;
2723 GrGLIRect dstGLRect;
2724 srcGLRect.setRelativeTo(srcVP,
2725 srcRect.fLeft,
2726 srcRect.fTop,
2727 srcRect.width(),
2728 srcRect.height(),
2729 src->origin());
2730 dstGLRect.setRelativeTo(dstVP,
2731 dstRect.fLeft,
2732 dstRect.fTop,
2733 dstRect.width(),
2734 dstRect.height(),
2735 dst->origin());
2736
derekf8c8f71a2014-09-16 06:24:57 -07002737 // BlitFrameBuffer respects the scissor, so disable it.
joshualitt77b13072014-10-27 14:51:01 -07002738 this->disableScissor();
derekf8c8f71a2014-09-16 06:24:57 -07002739
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002740 GrGLint srcY0;
2741 GrGLint srcY1;
2742 // Does the blit need to y-mirror or not?
2743 if (src->origin() == dst->origin()) {
2744 srcY0 = srcGLRect.fBottom;
2745 srcY1 = srcGLRect.fBottom + srcGLRect.fHeight;
2746 } else {
2747 srcY0 = srcGLRect.fBottom + srcGLRect.fHeight;
2748 srcY1 = srcGLRect.fBottom;
2749 }
2750 GL_CALL(BlitFramebuffer(srcGLRect.fLeft,
2751 srcY0,
2752 srcGLRect.fLeft + srcGLRect.fWidth,
2753 srcY1,
2754 dstGLRect.fLeft,
2755 dstGLRect.fBottom,
2756 dstGLRect.fLeft + dstGLRect.fWidth,
2757 dstGLRect.fBottom + dstGLRect.fHeight,
2758 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
bsalomon160f24c2015-03-17 15:55:42 -07002759 this->unbindSurfaceAsFBOForCopy(dstFBOBinding);
2760 this->unbindSurfaceAsFBOForCopy(srcFBOBinding);
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002761 copied = true;
2762 }
2763 }
bsalomon160f24c2015-03-17 15:55:42 -07002764 if (copied) {
2765 this->markSurfaceContentsDirty(dst, &dstRect);
2766 }
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002767 return copied;
2768}
2769
bsalomon861e1032014-12-16 07:33:49 -08002770bool GrGLGpu::canCopySurface(const GrSurface* dst,
joshualitt9853cce2014-11-17 14:22:48 -08002771 const GrSurface* src,
joshualitta7024152014-11-03 14:16:35 -08002772 const SkIRect& srcRect,
2773 const SkIPoint& dstPoint) {
egdaniel0f5f9672015-02-03 11:10:51 -08002774 // This mirrors the logic in onCopySurface.
2775 if (can_copy_texsubimage(dst, src, this)) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002776 return true;
2777 }
egdaniel0f5f9672015-02-03 11:10:51 -08002778 if (can_blit_framebuffer(dst, src, this)) {
bsalomona2c23232014-11-25 07:41:12 -08002779 if (dst == src) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002780 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
2781 srcRect.width(), srcRect.height());
2782 if(!SkIRect::IntersectsNoEmptyCheck(dstRect, srcRect)) {
2783 return true;
2784 }
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002785 } else {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002786 return true;
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002787 }
2788 }
joshualitta7024152014-11-03 14:16:35 -08002789 return false;
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002790}
2791
bsalomon861e1032014-12-16 07:33:49 -08002792void GrGLGpu::didAddGpuTraceMarker() {
commit-bot@chromium.orga3baf3b2014-02-21 18:45:30 +00002793 if (this->caps()->gpuTracingSupport()) {
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +00002794 const GrTraceMarkerSet& markerArray = this->getActiveTraceMarkers();
egdanield78a1682014-07-09 10:41:26 -07002795 SkString markerString = markerArray.toStringLast();
egdanielbdad9c32015-03-05 12:19:17 -08002796#if GR_FORCE_GPU_TRACE_DEBUGGING
2797 SkDebugf("%s\n", markerString.c_str());
2798#else
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +00002799 GL_CALL(PushGroupMarker(0, markerString.c_str()));
egdanielbdad9c32015-03-05 12:19:17 -08002800#endif
commit-bot@chromium.orga3baf3b2014-02-21 18:45:30 +00002801 }
2802}
2803
bsalomon861e1032014-12-16 07:33:49 -08002804void GrGLGpu::didRemoveGpuTraceMarker() {
commit-bot@chromium.orga3baf3b2014-02-21 18:45:30 +00002805 if (this->caps()->gpuTracingSupport()) {
egdanielbdad9c32015-03-05 12:19:17 -08002806#if GR_FORCE_GPU_TRACE_DEBUGGING
2807 SkDebugf("Pop trace marker.\n");
2808#else
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +00002809 GL_CALL(PopGroupMarker());
egdanielbdad9c32015-03-05 12:19:17 -08002810#endif
commit-bot@chromium.orga3baf3b2014-02-21 18:45:30 +00002811 }
2812}
joshualitt79f8fae2014-10-28 17:59:26 -07002813
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00002814///////////////////////////////////////////////////////////////////////////////
2815
bsalomon861e1032014-12-16 07:33:49 -08002816GrGLAttribArrayState* GrGLGpu::HWGeometryState::bindArrayAndBuffersToDraw(
2817 GrGLGpu* gpu,
bsalomon@google.com6918d482013-03-07 19:09:11 +00002818 const GrGLVertexBuffer* vbuffer,
2819 const GrGLIndexBuffer* ibuffer) {
bsalomon49f085d2014-09-05 13:34:00 -07002820 SkASSERT(vbuffer);
robertphillips@google.com4f65a272013-03-26 19:40:46 +00002821 GrGLAttribArrayState* attribState;
2822
bsalomon@google.com6918d482013-03-07 19:09:11 +00002823 // We use a vertex array if we're on a core profile and the verts are in a VBO.
2824 if (gpu->glCaps().isCoreProfile() && !vbuffer->isCPUBacked()) {
commit-bot@chromium.org089a7802014-05-02 21:38:22 +00002825 if (NULL == fVBOVertexArray || fVBOVertexArray->wasDestroyed()) {
bsalomon@google.com6918d482013-03-07 19:09:11 +00002826 SkSafeUnref(fVBOVertexArray);
2827 GrGLuint arrayID;
2828 GR_GL_CALL(gpu->glInterface(), GenVertexArrays(1, &arrayID));
2829 int attrCount = gpu->glCaps().maxVertexAttributes();
2830 fVBOVertexArray = SkNEW_ARGS(GrGLVertexArray, (gpu, arrayID, attrCount));
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00002831 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00002832 attribState = fVBOVertexArray->bindWithIndexBuffer(ibuffer);
2833 } else {
bsalomon49f085d2014-09-05 13:34:00 -07002834 if (ibuffer) {
bsalomon@google.com6918d482013-03-07 19:09:11 +00002835 this->setIndexBufferIDOnDefaultVertexArray(gpu, ibuffer->bufferID());
2836 } else {
2837 this->setVertexArrayID(gpu, 0);
2838 }
2839 int attrCount = gpu->glCaps().maxVertexAttributes();
2840 if (fDefaultVertexArrayAttribState.count() != attrCount) {
2841 fDefaultVertexArrayAttribState.resize(attrCount);
2842 }
2843 attribState = &fDefaultVertexArrayAttribState;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00002844 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00002845 return attribState;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002846}