blob: 91201009668b2772edce756570920b4c12018a0f [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"
egdaniel8dc7c3a2015-04-16 11:22:42 -070010#include "GrGLStencilAttachment.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;
egdanield803f272015-03-18 13:01:52 -0700154 fTempSrcFBOID = 0;
155 fTempDstFBOID = 0;
156 fStencilClearFBOID = 0;
cdaltonc7103a12014-08-11 14:05:05 -0700157
158 if (this->glCaps().pathRenderingSupport()) {
kkinnunen5b653572014-08-20 04:13:27 -0700159 fPathRendering.reset(new GrGLPathRendering(this));
cdaltonc7103a12014-08-11 14:05:05 -0700160 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000161}
162
bsalomon861e1032014-12-16 07:33:49 -0800163GrGLGpu::~GrGLGpu() {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000164 if (0 != fHWProgramID) {
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000165 // detach the current program so there is no confusion on OpenGL's part
166 // that we want it to be deleted
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000167 SkASSERT(fHWProgramID == fCurrentProgram->programID());
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000168 GL_CALL(UseProgram(0));
169 }
170
egdanield803f272015-03-18 13:01:52 -0700171 if (0 != fTempSrcFBOID) {
172 GL_CALL(DeleteFramebuffers(1, &fTempSrcFBOID));
egdaniel0f5f9672015-02-03 11:10:51 -0800173 }
egdanield803f272015-03-18 13:01:52 -0700174 if (0 != fTempDstFBOID) {
175 GL_CALL(DeleteFramebuffers(1, &fTempDstFBOID));
egdaniel0f5f9672015-02-03 11:10:51 -0800176 }
egdanield803f272015-03-18 13:01:52 -0700177 if (0 != fStencilClearFBOID) {
178 GL_CALL(DeleteFramebuffers(1, &fStencilClearFBOID));
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;
egdanield803f272015-03-18 13:01:52 -0700188 fTempSrcFBOID = 0;
189 fTempDstFBOID = 0;
190 fStencilClearFBOID = 0;
bsalomonc8dc1f72014-08-21 13:02:13 -0700191 if (this->glCaps().pathRenderingSupport()) {
192 this->glPathRendering()->abandonGpuResources();
193 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000194}
195
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000196///////////////////////////////////////////////////////////////////////////////
bsalomon861e1032014-12-16 07:33:49 -0800197GrPixelConfig GrGLGpu::preferredReadPixelsConfig(GrPixelConfig readConfig,
commit-bot@chromium.org28621512013-08-07 19:43:45 +0000198 GrPixelConfig surfaceConfig) const {
199 if (GR_GL_RGBA_8888_PIXEL_OPS_SLOW && kRGBA_8888_GrPixelConfig == readConfig) {
bsalomon@google.com9c680582013-02-06 18:17:50 +0000200 return kBGRA_8888_GrPixelConfig;
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +0000201 } else if (this->glContext().isMesa() &&
commit-bot@chromium.org28621512013-08-07 19:43:45 +0000202 GrBytesPerPixel(readConfig) == 4 &&
203 GrPixelConfigSwapRAndB(readConfig) == surfaceConfig) {
commit-bot@chromium.org5d1d79a2013-05-24 18:52:52 +0000204 // Mesa 3D takes a slow path on when reading back BGRA from an RGBA surface and vice-versa.
205 // Perhaps this should be guarded by some compiletime or runtime check.
206 return surfaceConfig;
piotaixre4b23142014-10-02 10:57:53 -0700207 } else if (readConfig == kBGRA_8888_GrPixelConfig
208 && !this->glCaps().readPixelsSupported(
209 this->glInterface(),
210 GR_GL_BGRA,
211 GR_GL_UNSIGNED_BYTE,
212 surfaceConfig
213 )) {
commit-bot@chromium.org28621512013-08-07 19:43:45 +0000214 return kRGBA_8888_GrPixelConfig;
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000215 } else {
commit-bot@chromium.org28621512013-08-07 19:43:45 +0000216 return readConfig;
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000217 }
218}
219
bsalomon861e1032014-12-16 07:33:49 -0800220GrPixelConfig GrGLGpu::preferredWritePixelsConfig(GrPixelConfig writeConfig,
commit-bot@chromium.org5d1d79a2013-05-24 18:52:52 +0000221 GrPixelConfig surfaceConfig) const {
commit-bot@chromium.org28621512013-08-07 19:43:45 +0000222 if (GR_GL_RGBA_8888_PIXEL_OPS_SLOW && kRGBA_8888_GrPixelConfig == writeConfig) {
223 return kBGRA_8888_GrPixelConfig;
224 } else {
225 return writeConfig;
226 }
bsalomon@google.com9c680582013-02-06 18:17:50 +0000227}
228
bsalomon861e1032014-12-16 07:33:49 -0800229bool GrGLGpu::canWriteTexturePixels(const GrTexture* texture, GrPixelConfig srcConfig) const {
krajcevski145d48c2014-06-11 16:07:50 -0700230 if (kIndex_8_GrPixelConfig == srcConfig || kIndex_8_GrPixelConfig == texture->config()) {
bsalomon@google.com9c680582013-02-06 18:17:50 +0000231 return false;
232 }
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +0000233 if (srcConfig != texture->config() && kGLES_GrGLStandard == this->glStandard()) {
bsalomon@google.com9c680582013-02-06 18:17:50 +0000234 // In general ES2 requires the internal format of the texture and the format of the src
235 // pixels to match. However, It may or may not be possible to upload BGRA data to a RGBA
236 // texture. It depends upon which extension added BGRA. The Apple extension allows it
237 // (BGRA's internal format is RGBA) while the EXT extension does not (BGRA is its own
238 // internal format).
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +0000239 if (this->glCaps().isConfigTexturable(kBGRA_8888_GrPixelConfig) &&
bsalomon@google.com9c680582013-02-06 18:17:50 +0000240 !this->glCaps().bgraIsInternalFormat() &&
241 kBGRA_8888_GrPixelConfig == srcConfig &&
242 kRGBA_8888_GrPixelConfig == texture->config()) {
243 return true;
244 } else {
245 return false;
246 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000247 } else {
bsalomon@google.com9c680582013-02-06 18:17:50 +0000248 return true;
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000249 }
250}
251
bsalomon861e1032014-12-16 07:33:49 -0800252bool GrGLGpu::fullReadPixelsIsFasterThanPartial() const {
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000253 return SkToBool(GR_GL_FULL_READPIXELS_FASTER_THAN_PARTIAL);
254}
255
bsalomon861e1032014-12-16 07:33:49 -0800256void GrGLGpu::onResetContext(uint32_t resetBits) {
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000257 // we don't use the zb at all
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000258 if (resetBits & kMisc_GrGLBackendState) {
259 GL_CALL(Disable(GR_GL_DEPTH_TEST));
260 GL_CALL(DepthMask(GR_GL_FALSE));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000261
egdaniel8dd688b2015-01-22 10:16:09 -0800262 fHWDrawFace = GrPipelineBuilder::kInvalid_DrawFace;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000263 fHWDitherEnabled = kUnknown_TriState;
reed@google.comac10a2d2010-12-22 21:39:39 +0000264
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +0000265 if (kGL_GrGLStandard == this->glStandard()) {
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000266 // Desktop-only state that we never change
267 if (!this->glCaps().isCoreProfile()) {
268 GL_CALL(Disable(GR_GL_POINT_SMOOTH));
269 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
270 GL_CALL(Disable(GR_GL_POLYGON_SMOOTH));
271 GL_CALL(Disable(GR_GL_POLYGON_STIPPLE));
272 GL_CALL(Disable(GR_GL_COLOR_LOGIC_OP));
273 GL_CALL(Disable(GR_GL_INDEX_LOGIC_OP));
274 }
275 // The windows NVIDIA driver has GL_ARB_imaging in the extension string when using a
276 // core profile. This seems like a bug since the core spec removes any mention of
277 // GL_ARB_imaging.
278 if (this->glCaps().imagingSupport() && !this->glCaps().isCoreProfile()) {
279 GL_CALL(Disable(GR_GL_COLOR_TABLE));
280 }
281 GL_CALL(Disable(GR_GL_POLYGON_OFFSET_FILL));
282 // Since ES doesn't support glPointSize at all we always use the VS to
283 // set the point size
284 GL_CALL(Enable(GR_GL_VERTEX_PROGRAM_POINT_SIZE));
285
286 // We should set glPolygonMode(FRONT_AND_BACK,FILL) here, too. It isn't
287 // currently part of our gl interface. There are probably others as
288 // well.
bsalomon@google.com2b1b8c02013-02-28 22:06:02 +0000289 }
joshualitt58162332014-08-01 06:44:53 -0700290
291 if (kGLES_GrGLStandard == this->glStandard() &&
292 fGLContext.hasExtension("GL_ARM_shader_framebuffer_fetch")) {
293 // The arm extension requires specifically enabling MSAA fetching per sample.
294 // On some devices this may have a perf hit. Also multiple render targets are disabled
295 GL_CALL(Enable(GR_GL_FETCH_PER_SAMPLE_ARM));
296 }
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000297 fHWWriteToColor = kUnknown_TriState;
298 // we only ever use lines in hairline mode
299 GL_CALL(LineWidth(1));
bsalomon@google.comcad107b2013-06-28 14:32:08 +0000300 }
edisonn@google.comba669992013-06-28 16:03:21 +0000301
egdanielb414f252014-07-29 13:15:47 -0700302 if (resetBits & kMSAAEnable_GrGLBackendState) {
303 fMSAAEnabled = kUnknown_TriState;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000304 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000305
commit-bot@chromium.org46fbfe02013-08-30 15:52:12 +0000306 fHWActiveTextureUnitIdx = -1; // invalid
307
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000308 if (resetBits & kTextureBinding_GrGLBackendState) {
bsalomon1c63bf62014-07-22 13:09:46 -0700309 for (int s = 0; s < fHWBoundTextureUniqueIDs.count(); ++s) {
310 fHWBoundTextureUniqueIDs[s] = SK_InvalidUniqueID;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000311 }
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000312 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000313
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000314 if (resetBits & kBlend_GrGLBackendState) {
315 fHWBlendState.invalidate();
316 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000317
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000318 if (resetBits & kView_GrGLBackendState) {
319 fHWScissorSettings.invalidate();
320 fHWViewport.invalidate();
321 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000322
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000323 if (resetBits & kStencil_GrGLBackendState) {
324 fHWStencilSettings.invalidate();
325 fHWStencilTestEnabled = kUnknown_TriState;
326 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000327
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000328 // Vertex
329 if (resetBits & kVertex_GrGLBackendState) {
330 fHWGeometryState.invalidate();
331 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000332
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000333 if (resetBits & kRenderTarget_GrGLBackendState) {
egdanield803f272015-03-18 13:01:52 -0700334 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000335 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000336
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000337 if (resetBits & kPathRendering_GrGLBackendState) {
338 if (this->caps()->pathRenderingSupport()) {
kkinnunenccdaa042014-08-20 01:36:23 -0700339 this->glPathRendering()->resetContext();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000340 }
bsalomon@google.com05a718c2012-06-29 14:01:53 +0000341 }
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000342
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000343 // we assume these values
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000344 if (resetBits & kPixelStore_GrGLBackendState) {
345 if (this->glCaps().unpackRowLengthSupport()) {
346 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
347 }
348 if (this->glCaps().packRowLengthSupport()) {
349 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
350 }
351 if (this->glCaps().unpackFlipYSupport()) {
352 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
353 }
354 if (this->glCaps().packFlipYSupport()) {
355 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, GR_GL_FALSE));
356 }
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000357 }
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000358
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000359 if (resetBits & kProgram_GrGLBackendState) {
360 fHWProgramID = 0;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000361 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000362}
363
egdanielcf614fd2015-04-22 13:58:58 -0700364static GrSurfaceOrigin resolve_origin(GrSurfaceOrigin origin, bool renderTarget) {
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000365 // By default, GrRenderTargets are GL's normal orientation so that they
366 // can be drawn to by the outside world without the client having
367 // to render upside down.
368 if (kDefault_GrSurfaceOrigin == origin) {
369 return renderTarget ? kBottomLeft_GrSurfaceOrigin : kTopLeft_GrSurfaceOrigin;
370 } else {
371 return origin;
372 }
373}
374
bsalomon861e1032014-12-16 07:33:49 -0800375GrTexture* GrGLGpu::onWrapBackendTexture(const GrBackendTextureDesc& desc) {
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000376 if (!this->configToGLFormats(desc.fConfig, false, NULL, NULL, NULL)) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000377 return NULL;
378 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000379
robertphillips@google.comb72e5d32012-10-30 15:18:10 +0000380 if (0 == desc.fTextureHandle) {
381 return NULL;
382 }
383
bsalomon@google.combcce8922013-03-25 15:38:39 +0000384 int maxSize = this->caps()->maxTextureSize();
robertphillips@google.comb72e5d32012-10-30 15:18:10 +0000385 if (desc.fWidth > maxSize || desc.fHeight > maxSize) {
386 return NULL;
387 }
388
bsalomonb15b4c12014-10-29 12:41:57 -0700389 GrGLTexture::IDDesc idDesc;
390 GrSurfaceDesc surfDesc;
391
392 idDesc.fTextureID = static_cast<GrGLuint>(desc.fTextureHandle);
bsalomon5236cf42015-01-14 10:42:08 -0800393 idDesc.fLifeCycle = GrGpuResource::kWrapped_LifeCycle;
bsalomonb15b4c12014-10-29 12:41:57 -0700394
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000395 // next line relies on GrBackendTextureDesc's flags matching GrTexture's
bsalomonb15b4c12014-10-29 12:41:57 -0700396 surfDesc.fFlags = (GrSurfaceFlags) desc.fFlags;
397 surfDesc.fWidth = desc.fWidth;
398 surfDesc.fHeight = desc.fHeight;
399 surfDesc.fConfig = desc.fConfig;
senorblanco94e50102015-04-06 09:42:57 -0700400 surfDesc.fSampleCnt = SkTMin(desc.fSampleCnt, this->caps()->maxSampleCount());
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000401 bool renderTarget = SkToBool(desc.fFlags & kRenderTarget_GrBackendTextureFlag);
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000402 // FIXME: this should be calling resolve_origin(), but Chrome code is currently
403 // assuming the old behaviour, which is that backend textures are always
404 // BottomLeft, even for non-RT's. Once Chrome is fixed, change this to:
405 // glTexDesc.fOrigin = resolve_origin(desc.fOrigin, renderTarget);
406 if (kDefault_GrSurfaceOrigin == desc.fOrigin) {
bsalomonb15b4c12014-10-29 12:41:57 -0700407 surfDesc.fOrigin = kBottomLeft_GrSurfaceOrigin;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000408 } else {
bsalomonb15b4c12014-10-29 12:41:57 -0700409 surfDesc.fOrigin = desc.fOrigin;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000410 }
bsalomon@google.come269f212011-11-07 13:29:52 +0000411
412 GrGLTexture* texture = NULL;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000413 if (renderTarget) {
bsalomonb15b4c12014-10-29 12:41:57 -0700414 GrGLRenderTarget::IDDesc rtIDDesc;
egdanielb0e1be22015-04-22 13:27:39 -0700415 if (!this->createRenderTargetObjects(surfDesc, GrGpuResource::kUncached_LifeCycle,
416 idDesc.fTextureID, &rtIDDesc)) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000417 return NULL;
418 }
bsalomon37dd3312014-11-03 08:47:23 -0800419 texture = SkNEW_ARGS(GrGLTextureRenderTarget, (this, surfDesc, idDesc, rtIDDesc));
bsalomon@google.come269f212011-11-07 13:29:52 +0000420 } else {
bsalomonb15b4c12014-10-29 12:41:57 -0700421 texture = SkNEW_ARGS(GrGLTexture, (this, surfDesc, idDesc));
bsalomon@google.come269f212011-11-07 13:29:52 +0000422 }
423 if (NULL == texture) {
424 return NULL;
425 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000426
bsalomon@google.come269f212011-11-07 13:29:52 +0000427 return texture;
428}
429
bsalomon861e1032014-12-16 07:33:49 -0800430GrRenderTarget* GrGLGpu::onWrapBackendRenderTarget(const GrBackendRenderTargetDesc& wrapDesc) {
bsalomonb15b4c12014-10-29 12:41:57 -0700431 GrGLRenderTarget::IDDesc idDesc;
egdanield803f272015-03-18 13:01:52 -0700432 idDesc.fRTFBOID = static_cast<GrGLuint>(wrapDesc.fRenderTargetHandle);
bsalomonb15b4c12014-10-29 12:41:57 -0700433 idDesc.fMSColorRenderbufferID = 0;
egdanield803f272015-03-18 13:01:52 -0700434 idDesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
bsalomon5236cf42015-01-14 10:42:08 -0800435 idDesc.fLifeCycle = GrGpuResource::kWrapped_LifeCycle;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000436
bsalomonb15b4c12014-10-29 12:41:57 -0700437 GrSurfaceDesc desc;
438 desc.fConfig = wrapDesc.fConfig;
439 desc.fFlags = kCheckAllocation_GrSurfaceFlag;
440 desc.fWidth = wrapDesc.fWidth;
441 desc.fHeight = wrapDesc.fHeight;
senorblanco94e50102015-04-06 09:42:57 -0700442 desc.fSampleCnt = SkTMin(wrapDesc.fSampleCnt, this->caps()->maxSampleCount());
bsalomonb15b4c12014-10-29 12:41:57 -0700443 desc.fOrigin = resolve_origin(wrapDesc.fOrigin, true);
444
bsalomon37dd3312014-11-03 08:47:23 -0800445 GrRenderTarget* tgt = SkNEW_ARGS(GrGLRenderTarget, (this, desc, idDesc));
bsalomonb15b4c12014-10-29 12:41:57 -0700446 if (wrapDesc.fStencilBits) {
egdaniel8dc7c3a2015-04-16 11:22:42 -0700447 GrGLStencilAttachment::IDDesc sbDesc;
448 GrGLStencilAttachment::Format format;
449 format.fInternalFormat = GrGLStencilAttachment::kUnknownInternalFormat;
bsalomon@google.come269f212011-11-07 13:29:52 +0000450 format.fPacked = false;
bsalomonb15b4c12014-10-29 12:41:57 -0700451 format.fStencilBits = wrapDesc.fStencilBits;
452 format.fTotalBits = wrapDesc.fStencilBits;
egdaniel8dc7c3a2015-04-16 11:22:42 -0700453 GrGLStencilAttachment* sb = SkNEW_ARGS(GrGLStencilAttachment,
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000454 (this,
kkinnunen36c57df2015-01-27 00:30:18 -0800455 sbDesc,
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000456 desc.fWidth,
457 desc.fHeight,
458 desc.fSampleCnt,
459 format));
egdaniel8dc7c3a2015-04-16 11:22:42 -0700460 tgt->renderTargetPriv().didAttachStencilAttachment(sb);
bsalomon@google.come269f212011-11-07 13:29:52 +0000461 sb->unref();
462 }
463 return tgt;
464}
465
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000466////////////////////////////////////////////////////////////////////////////////
467
bsalomon861e1032014-12-16 07:33:49 -0800468bool GrGLGpu::onWriteTexturePixels(GrTexture* texture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000469 int left, int top, int width, int height,
470 GrPixelConfig config, const void* buffer,
471 size_t rowBytes) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000472 if (NULL == buffer) {
bsalomon@google.com9c680582013-02-06 18:17:50 +0000473 return false;
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000474 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000475 GrGLTexture* glTex = static_cast<GrGLTexture*>(texture);
476
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +0000477 this->setScratchTextureUnit();
bsalomon@google.com6f379512011-11-16 20:36:03 +0000478 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTex->textureID()));
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000479
krajcevski145d48c2014-06-11 16:07:50 -0700480 bool success = false;
bsalomonb15b4c12014-10-29 12:41:57 -0700481 if (GrPixelConfigIsCompressed(glTex->desc().fConfig)) {
bsalomon861e1032014-12-16 07:33:49 -0800482 // We check that config == desc.fConfig in GrGLGpu::canWriteTexturePixels()
bsalomonb15b4c12014-10-29 12:41:57 -0700483 SkASSERT(config == glTex->desc().fConfig);
484 success = this->uploadCompressedTexData(glTex->desc(), buffer, false, left, top, width,
485 height);
krajcevski145d48c2014-06-11 16:07:50 -0700486 } else {
bsalomonb15b4c12014-10-29 12:41:57 -0700487 success = this->uploadTexData(glTex->desc(), false, left, top, width, height, config,
488 buffer, rowBytes);
krajcevski145d48c2014-06-11 16:07:50 -0700489 }
490
491 if (success) {
bsalomonafbf2d62014-09-30 12:18:44 -0700492 texture->texturePriv().dirtyMipMaps(true);
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +0000493 return true;
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +0000494 }
krajcevski145d48c2014-06-11 16:07:50 -0700495
496 return false;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000497}
498
bsalomonb15b4c12014-10-29 12:41:57 -0700499static bool adjust_pixel_ops_params(int surfaceWidth,
500 int surfaceHeight,
501 size_t bpp,
502 int* left, int* top, int* width, int* height,
503 const void** data,
504 size_t* rowBytes) {
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000505 if (!*rowBytes) {
506 *rowBytes = *width * bpp;
507 }
508
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000509 SkIRect subRect = SkIRect::MakeXYWH(*left, *top, *width, *height);
510 SkIRect bounds = SkIRect::MakeWH(surfaceWidth, surfaceHeight);
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000511
512 if (!subRect.intersect(bounds)) {
513 return false;
514 }
515 *data = reinterpret_cast<const void*>(reinterpret_cast<intptr_t>(*data) +
516 (subRect.fTop - *top) * *rowBytes + (subRect.fLeft - *left) * bpp);
517
518 *left = subRect.fLeft;
519 *top = subRect.fTop;
520 *width = subRect.width();
521 *height = subRect.height();
522 return true;
523}
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000524
bsalomonb15b4c12014-10-29 12:41:57 -0700525static inline GrGLenum check_alloc_error(const GrSurfaceDesc& desc,
526 const GrGLInterface* interface) {
bsalomonf2703d82014-10-28 14:33:06 -0700527 if (SkToBool(desc.fFlags & kCheckAllocation_GrSurfaceFlag)) {
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000528 return GR_GL_GET_ERROR(interface);
529 } else {
530 return CHECK_ALLOC_ERROR(interface);
531 }
532}
533
bsalomon861e1032014-12-16 07:33:49 -0800534bool GrGLGpu::uploadTexData(const GrSurfaceDesc& desc,
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000535 bool isNewTexture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000536 int left, int top, int width, int height,
537 GrPixelConfig dataConfig,
538 const void* data,
539 size_t rowBytes) {
bsalomon49f085d2014-09-05 13:34:00 -0700540 SkASSERT(data || isNewTexture);
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000541
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +0000542 // If we're uploading compressed data then we should be using uploadCompressedTexData
543 SkASSERT(!GrPixelConfigIsCompressed(dataConfig));
544
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000545 size_t bpp = GrBytesPerPixel(dataConfig);
546 if (!adjust_pixel_ops_params(desc.fWidth, desc.fHeight, bpp, &left, &top,
547 &width, &height, &data, &rowBytes)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000548 return false;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000549 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000550 size_t trimRowBytes = width * bpp;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000551
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000552 // in case we need a temporary, trimmed copy of the src pixels
georgeb62508b2014-08-12 18:00:47 -0700553 GrAutoMalloc<128 * 128> tempStorage;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000554
commit-bot@chromium.orgcea9abb2013-12-09 19:15:37 +0000555 // We currently lazily create MIPMAPs when the we see a draw with
556 // GrTextureParams::kMipMap_FilterMode. Using texture storage requires that the
557 // MIP levels are all created when the texture is created. So for now we don't use
558 // texture storage.
559 bool useTexStorage = false &&
560 isNewTexture &&
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000561 this->glCaps().texStorageSupport();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000562
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +0000563 if (useTexStorage && kGL_GrGLStandard == this->glStandard()) {
bsalomon@google.com313f0192012-07-10 17:21:02 +0000564 // 565 is not a sized internal format on desktop GL. So on desktop with
565 // 565 we always use an unsized internal format to let the system pick
566 // the best sized format to convert the 565 data to. Since TexStorage
567 // only allows sized internal formats we will instead use TexImage2D.
568 useTexStorage = desc.fConfig != kRGB_565_GrPixelConfig;
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000569 }
570
jvanverth3f801cb2014-12-16 09:49:38 -0800571 GrGLenum internalFormat = 0x0; // suppress warning
572 GrGLenum externalFormat = 0x0; // suppress warning
573 GrGLenum externalType = 0x0; // suppress warning
bsalomone904c092014-07-17 10:50:59 -0700574
jvanverthe1869ca2014-12-16 13:32:27 -0800575 // glTexStorage requires sized internal formats on both desktop and ES. ES2 requires an unsized
jvanverthf72b7522014-12-17 10:46:01 -0800576 // format for glTexImage, unlike ES3 and desktop.
jvanverthe1869ca2014-12-16 13:32:27 -0800577 bool useSizedFormat = useTexStorage;
jvanverthf72b7522014-12-17 10:46:01 -0800578 if (kGL_GrGLStandard == this->glStandard() ||
579 (this->glVersion() >= GR_GL_VER(3, 0) &&
580 // ES3 only works with sized BGRA8 format if "GL_APPLE_texture_format_BGRA8888" enabled
581 (kBGRA_8888_GrPixelConfig != dataConfig || !this->glCaps().bgraIsInternalFormat()))) {
jvanverthe1869ca2014-12-16 13:32:27 -0800582 useSizedFormat = true;
583 }
jvanverthf72b7522014-12-17 10:46:01 -0800584
bsalomone904c092014-07-17 10:50:59 -0700585 if (!this->configToGLFormats(dataConfig, useSizedFormat, &internalFormat,
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000586 &externalFormat, &externalType)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000587 return false;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000588 }
589
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000590 /*
bsalomon@google.com6f379512011-11-16 20:36:03 +0000591 * check whether to allocate a temporary buffer for flipping y or
592 * because our srcData has extra bytes past each row. If so, we need
593 * to trim those off here, since GL ES may not let us specify
594 * GL_UNPACK_ROW_LENGTH.
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000595 */
bsalomon@google.com6f379512011-11-16 20:36:03 +0000596 bool restoreGLRowLength = false;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000597 bool swFlipY = false;
598 bool glFlipY = false;
bsalomon49f085d2014-09-05 13:34:00 -0700599 if (data) {
senorblanco@chromium.orgef5dbe12013-01-28 16:42:38 +0000600 if (kBottomLeft_GrSurfaceOrigin == desc.fOrigin) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000601 if (this->glCaps().unpackFlipYSupport()) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000602 glFlipY = true;
603 } else {
604 swFlipY = true;
605 }
606 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000607 if (this->glCaps().unpackRowLengthSupport() && !swFlipY) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000608 // can't use this for flipping, only non-neg values allowed. :(
609 if (rowBytes != trimRowBytes) {
610 GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
611 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength));
612 restoreGLRowLength = true;
613 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000614 } else {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000615 if (trimRowBytes != rowBytes || swFlipY) {
616 // copy data into our new storage, skipping the trailing bytes
617 size_t trimSize = height * trimRowBytes;
618 const char* src = (const char*)data;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000619 if (swFlipY) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000620 src += (height - 1) * rowBytes;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000621 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000622 char* dst = (char*)tempStorage.reset(trimSize);
623 for (int y = 0; y < height; y++) {
624 memcpy(dst, src, trimRowBytes);
625 if (swFlipY) {
626 src -= rowBytes;
627 } else {
628 src += rowBytes;
629 }
630 dst += trimRowBytes;
631 }
632 // now point data to our copied version
633 data = tempStorage.get();
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000634 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000635 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000636 if (glFlipY) {
637 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_TRUE));
638 }
joshualittee5da552014-07-16 13:32:56 -0700639 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT,
640 static_cast<GrGLint>(GrUnpackAlignment(dataConfig))));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000641 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000642 bool succeeded = true;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000643 if (isNewTexture &&
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000644 0 == left && 0 == top &&
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000645 desc.fWidth == width && desc.fHeight == height) {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000646 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000647 if (useTexStorage) {
commit-bot@chromium.orgcea9abb2013-12-09 19:15:37 +0000648 // We never resize or change formats of textures.
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000649 GL_ALLOC_CALL(this->glInterface(),
650 TexStorage2D(GR_GL_TEXTURE_2D,
651 1, // levels
652 internalFormat,
653 desc.fWidth, desc.fHeight));
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000654 } else {
bsalomond4cb9222014-08-11 14:19:09 -0700655 GL_ALLOC_CALL(this->glInterface(),
656 TexImage2D(GR_GL_TEXTURE_2D,
657 0, // level
658 internalFormat,
659 desc.fWidth, desc.fHeight,
660 0, // border
661 externalFormat, externalType,
662 data));
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000663 }
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000664 GrGLenum error = check_alloc_error(desc, this->glInterface());
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000665 if (error != GR_GL_NO_ERROR) {
666 succeeded = false;
667 } else {
668 // if we have data and we used TexStorage to create the texture, we
669 // now upload with TexSubImage.
bsalomon49f085d2014-09-05 13:34:00 -0700670 if (data && useTexStorage) {
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000671 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D,
672 0, // level
673 left, top,
674 width, height,
675 externalFormat, externalType,
676 data));
677 }
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000678 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000679 } else {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000680 if (swFlipY || glFlipY) {
bsalomon@google.com6f379512011-11-16 20:36:03 +0000681 top = desc.fHeight - (top + height);
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000682 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000683 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D,
684 0, // level
685 left, top,
686 width, height,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000687 externalFormat, externalType, data));
688 }
689
690 if (restoreGLRowLength) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000691 SkASSERT(this->glCaps().unpackRowLengthSupport());
bsalomon@google.com6f379512011-11-16 20:36:03 +0000692 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000693 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000694 if (glFlipY) {
695 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
696 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000697 return succeeded;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000698}
699
krajcevski145d48c2014-06-11 16:07:50 -0700700// TODO: This function is using a lot of wonky semantics like, if width == -1
piotaixre4b23142014-10-02 10:57:53 -0700701// then set width = desc.fWdith ... blah. A better way to do it might be to
krajcevski145d48c2014-06-11 16:07:50 -0700702// create a CompressedTexData struct that takes a desc/ptr and figures out
703// the proper upload semantics. Then users can construct this function how they
704// see fit if they want to go against the "standard" way to do it.
bsalomon861e1032014-12-16 07:33:49 -0800705bool GrGLGpu::uploadCompressedTexData(const GrSurfaceDesc& desc,
krajcevski145d48c2014-06-11 16:07:50 -0700706 const void* data,
707 bool isNewTexture,
708 int left, int top, int width, int height) {
bsalomon49f085d2014-09-05 13:34:00 -0700709 SkASSERT(data || isNewTexture);
krajcevski9c0e6292014-06-02 07:38:14 -0700710
711 // No support for software flip y, yet...
712 SkASSERT(kBottomLeft_GrSurfaceOrigin != desc.fOrigin);
713
krajcevski145d48c2014-06-11 16:07:50 -0700714 if (-1 == width) {
715 width = desc.fWidth;
716 }
717#ifdef SK_DEBUG
718 else {
719 SkASSERT(width <= desc.fWidth);
720 }
721#endif
722
723 if (-1 == height) {
724 height = desc.fHeight;
725 }
726#ifdef SK_DEBUG
727 else {
728 SkASSERT(height <= desc.fHeight);
729 }
730#endif
731
krajcevski9c0e6292014-06-02 07:38:14 -0700732 // Make sure that the width and height that we pass to OpenGL
733 // is a multiple of the block size.
bsalomon98806072014-12-12 15:11:17 -0800734 size_t dataSize = GrCompressedFormatDataSize(desc.fConfig, width, height);
krajcevski9c0e6292014-06-02 07:38:14 -0700735
736 // We only need the internal format for compressed 2D textures.
737 GrGLenum internalFormat = 0;
738 if (!this->configToGLFormats(desc.fConfig, false, &internalFormat, NULL, NULL)) {
739 return false;
740 }
741
krajcevski145d48c2014-06-11 16:07:50 -0700742 if (isNewTexture) {
bsalomond4cb9222014-08-11 14:19:09 -0700743 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
krajcevski145d48c2014-06-11 16:07:50 -0700744 GL_ALLOC_CALL(this->glInterface(),
745 CompressedTexImage2D(GR_GL_TEXTURE_2D,
746 0, // level
747 internalFormat,
748 width, height,
749 0, // border
bsalomon98806072014-12-12 15:11:17 -0800750 SkToInt(dataSize),
krajcevski145d48c2014-06-11 16:07:50 -0700751 data));
bsalomond4cb9222014-08-11 14:19:09 -0700752 GrGLenum error = check_alloc_error(desc, this->glInterface());
753 if (error != GR_GL_NO_ERROR) {
754 return false;
755 }
krajcevski145d48c2014-06-11 16:07:50 -0700756 } else {
bsalomond4cb9222014-08-11 14:19:09 -0700757 // Paletted textures can't be updated.
758 if (GR_GL_PALETTE8_RGBA8 == internalFormat) {
759 return false;
760 }
761 GL_CALL(CompressedTexSubImage2D(GR_GL_TEXTURE_2D,
762 0, // level
763 left, top,
764 width, height,
765 internalFormat,
bsalomon98806072014-12-12 15:11:17 -0800766 SkToInt(dataSize),
bsalomond4cb9222014-08-11 14:19:09 -0700767 data));
krajcevski145d48c2014-06-11 16:07:50 -0700768 }
krajcevski9c0e6292014-06-02 07:38:14 -0700769
bsalomond4cb9222014-08-11 14:19:09 -0700770 return true;
krajcevski9c0e6292014-06-02 07:38:14 -0700771}
772
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +0000773static bool renderbuffer_storage_msaa(GrGLContext& ctx,
774 int sampleCount,
775 GrGLenum format,
776 int width, int height) {
robertphillips@google.com6177e692013-02-28 20:16:25 +0000777 CLEAR_ERROR_BEFORE_ALLOC(ctx.interface());
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +0000778 SkASSERT(GrGLCaps::kNone_MSFBOType != ctx.caps()->msFBOType());
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +0000779 switch (ctx.caps()->msFBOType()) {
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +0000780 case GrGLCaps::kDesktop_ARB_MSFBOType:
781 case GrGLCaps::kDesktop_EXT_MSFBOType:
782 case GrGLCaps::kES_3_0_MSFBOType:
783 GL_ALLOC_CALL(ctx.interface(),
784 RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
785 sampleCount,
786 format,
787 width, height));
788 break;
789 case GrGLCaps::kES_Apple_MSFBOType:
790 GL_ALLOC_CALL(ctx.interface(),
791 RenderbufferStorageMultisampleES2APPLE(GR_GL_RENDERBUFFER,
792 sampleCount,
793 format,
794 width, height));
795 break;
796 case GrGLCaps::kES_EXT_MsToTexture_MSFBOType:
797 case GrGLCaps::kES_IMG_MsToTexture_MSFBOType:
798 GL_ALLOC_CALL(ctx.interface(),
799 RenderbufferStorageMultisampleES2EXT(GR_GL_RENDERBUFFER,
800 sampleCount,
801 format,
802 width, height));
803 break;
804 case GrGLCaps::kNone_MSFBOType:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000805 SkFAIL("Shouldn't be here if we don't support multisampled renderbuffers.");
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +0000806 break;
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000807 }
Brian Salomon9251d4e2015-03-17 16:03:19 -0400808 return (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctx.interface()));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000809}
810
egdanielb0e1be22015-04-22 13:27:39 -0700811bool GrGLGpu::createRenderTargetObjects(const GrSurfaceDesc& desc,
812 GrGpuResource::LifeCycle lifeCycle,
813 GrGLuint texID,
bsalomonb15b4c12014-10-29 12:41:57 -0700814 GrGLRenderTarget::IDDesc* idDesc) {
815 idDesc->fMSColorRenderbufferID = 0;
egdanield803f272015-03-18 13:01:52 -0700816 idDesc->fRTFBOID = 0;
817 idDesc->fTexFBOID = 0;
egdanielb0e1be22015-04-22 13:27:39 -0700818 idDesc->fLifeCycle = lifeCycle;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000819
820 GrGLenum status;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000821
bsalomon@google.comab15d612011-08-09 12:57:56 +0000822 GrGLenum msColorFormat = 0; // suppress warning
823
bsalomonb15b4c12014-10-29 12:41:57 -0700824 if (desc.fSampleCnt > 0 && GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.com347c3822013-05-01 20:10:01 +0000825 goto FAILED;
826 }
827
egdanield803f272015-03-18 13:01:52 -0700828 GL_CALL(GenFramebuffers(1, &idDesc->fTexFBOID));
829 if (!idDesc->fTexFBOID) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000830 goto FAILED;
831 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000832
egdanield803f272015-03-18 13:01:52 -0700833
bsalomon@google.comf3a60c02013-03-19 19:06:09 +0000834 // If we are using multisampling we will create two FBOS. We render to one and then resolve to
835 // the texture bound to the other. The exception is the IMG multisample extension. With this
836 // extension the texture is multisampled when rendered to and then auto-resolves it when it is
837 // rendered from.
bsalomonb15b4c12014-10-29 12:41:57 -0700838 if (desc.fSampleCnt > 0 && this->glCaps().usesMSAARenderBuffers()) {
egdanield803f272015-03-18 13:01:52 -0700839 GL_CALL(GenFramebuffers(1, &idDesc->fRTFBOID));
bsalomonb15b4c12014-10-29 12:41:57 -0700840 GL_CALL(GenRenderbuffers(1, &idDesc->fMSColorRenderbufferID));
egdanield803f272015-03-18 13:01:52 -0700841 if (!idDesc->fRTFBOID ||
842 !idDesc->fMSColorRenderbufferID ||
bsalomonb15b4c12014-10-29 12:41:57 -0700843 !this->configToGLFormats(desc.fConfig,
commit-bot@chromium.org87002952013-08-20 15:12:01 +0000844 // ES2 and ES3 require sized internal formats for rb storage.
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +0000845 kGLES_GrGLStandard == this->glStandard(),
bsalomon@google.com347c3822013-05-01 20:10:01 +0000846 &msColorFormat,
847 NULL,
848 NULL)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000849 goto FAILED;
850 }
851 } else {
egdanield803f272015-03-18 13:01:52 -0700852 idDesc->fRTFBOID = idDesc->fTexFBOID;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000853 }
854
egdanield803f272015-03-18 13:01:52 -0700855 // below here we may bind the FBO
856 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
857 if (idDesc->fRTFBOID != idDesc->fTexFBOID) {
bsalomonb15b4c12014-10-29 12:41:57 -0700858 SkASSERT(desc.fSampleCnt > 0);
859 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, idDesc->fMSColorRenderbufferID));
robertphillips@google.com6177e692013-02-28 20:16:25 +0000860 if (!renderbuffer_storage_msaa(fGLContext,
bsalomonb15b4c12014-10-29 12:41:57 -0700861 desc.fSampleCnt,
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000862 msColorFormat,
bsalomonb15b4c12014-10-29 12:41:57 -0700863 desc.fWidth, desc.fHeight)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000864 goto FAILED;
865 }
egdanield803f272015-03-18 13:01:52 -0700866 fStats.incRenderTargetBinds();
867 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, idDesc->fRTFBOID));
868 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
869 GR_GL_COLOR_ATTACHMENT0,
870 GR_GL_RENDERBUFFER,
871 idDesc->fMSColorRenderbufferID));
bsalomonb15b4c12014-10-29 12:41:57 -0700872 if ((desc.fFlags & kCheckAllocation_GrSurfaceFlag) ||
873 !this->glCaps().isConfigVerifiedColorAttachment(desc.fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000874 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
875 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
876 goto FAILED;
877 }
bsalomonb15b4c12014-10-29 12:41:57 -0700878 fGLContext.caps()->markConfigAsValidColorAttachment(desc.fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000879 }
880 }
egdanield803f272015-03-18 13:01:52 -0700881 fStats.incRenderTargetBinds();
882 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, idDesc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000883
bsalomonb15b4c12014-10-29 12:41:57 -0700884 if (this->glCaps().usesImplicitMSAAResolve() && desc.fSampleCnt > 0) {
egdanield803f272015-03-18 13:01:52 -0700885 GL_CALL(FramebufferTexture2DMultisample(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +0000886 GR_GL_COLOR_ATTACHMENT0,
887 GR_GL_TEXTURE_2D,
bsalomonb15b4c12014-10-29 12:41:57 -0700888 texID, 0, desc.fSampleCnt));
bsalomon@google.comf3a60c02013-03-19 19:06:09 +0000889 } else {
egdanield803f272015-03-18 13:01:52 -0700890 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +0000891 GR_GL_COLOR_ATTACHMENT0,
892 GR_GL_TEXTURE_2D,
893 texID, 0));
894 }
bsalomonb15b4c12014-10-29 12:41:57 -0700895 if ((desc.fFlags & kCheckAllocation_GrSurfaceFlag) ||
896 !this->glCaps().isConfigVerifiedColorAttachment(desc.fConfig)) {
egdanield803f272015-03-18 13:01:52 -0700897 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000898 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
899 goto FAILED;
900 }
bsalomonb15b4c12014-10-29 12:41:57 -0700901 fGLContext.caps()->markConfigAsValidColorAttachment(desc.fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000902 }
903
904 return true;
905
906FAILED:
bsalomonb15b4c12014-10-29 12:41:57 -0700907 if (idDesc->fMSColorRenderbufferID) {
908 GL_CALL(DeleteRenderbuffers(1, &idDesc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000909 }
egdanield803f272015-03-18 13:01:52 -0700910 if (idDesc->fRTFBOID != idDesc->fTexFBOID) {
911 GL_CALL(DeleteFramebuffers(1, &idDesc->fRTFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000912 }
egdanield803f272015-03-18 13:01:52 -0700913 if (idDesc->fTexFBOID) {
914 GL_CALL(DeleteFramebuffers(1, &idDesc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000915 }
916 return false;
917}
918
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000919// good to set a break-point here to know when createTexture fails
920static GrTexture* return_null_texture() {
mtklein@google.com330313a2013-08-22 15:37:26 +0000921// SkDEBUGFAIL("null texture");
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000922 return NULL;
923}
924
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000925#if 0 && defined(SK_DEBUG)
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000926static size_t as_size_t(int x) {
927 return x;
928}
929#endif
930
egdanielb0e1be22015-04-22 13:27:39 -0700931GrTexture* GrGLGpu::onCreateTexture(const GrSurfaceDesc& desc,
932 GrGpuResource::LifeCycle lifeCycle,
bsalomon5236cf42015-01-14 10:42:08 -0800933 const void* srcData, size_t rowBytes) {
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000934 // We fail if the MSAA was requested and is not available.
935 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType() && desc.fSampleCnt) {
tfarina38406c82014-10-31 07:11:12 -0700936 //SkDebugf("MSAA RT requested but not supported on this platform.");
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000937 return return_null_texture();
938 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000939
bsalomonf2703d82014-10-28 14:33:06 -0700940 bool renderTarget = SkToBool(desc.fFlags & kRenderTarget_GrSurfaceFlag);
reed@google.comac10a2d2010-12-22 21:39:39 +0000941
bsalomonb15b4c12014-10-29 12:41:57 -0700942 GrGLTexture::IDDesc idDesc;
943 GL_CALL(GenTextures(1, &idDesc.fTextureID));
egdanielb0e1be22015-04-22 13:27:39 -0700944 idDesc.fLifeCycle = lifeCycle;
junov@chromium.org8b6b1c92013-08-29 16:22:09 +0000945
bsalomonb15b4c12014-10-29 12:41:57 -0700946 if (!idDesc.fTextureID) {
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000947 return return_null_texture();
948 }
949
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +0000950 this->setScratchTextureUnit();
bsalomonb15b4c12014-10-29 12:41:57 -0700951 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, idDesc.fTextureID));
bsalomon@google.come269f212011-11-07 13:29:52 +0000952
junov@chromium.org8b6b1c92013-08-29 16:22:09 +0000953 if (renderTarget && this->glCaps().textureUsageSupport()) {
954 // provides a hint about how this texture will be used
955 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
956 GR_GL_TEXTURE_USAGE,
957 GR_GL_FRAMEBUFFER_ATTACHMENT));
958 }
959
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000960 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
961 // drivers have a bug where an FBO won't be complete if it includes a
962 // texture that is not mipmap complete (considering the filter in use).
963 GrGLTexture::TexParams initialTexParams;
964 // we only set a subset here so invalidate first
965 initialTexParams.invalidate();
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +0000966 initialTexParams.fMinFilter = GR_GL_NEAREST;
967 initialTexParams.fMagFilter = GR_GL_NEAREST;
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000968 initialTexParams.fWrapS = GR_GL_CLAMP_TO_EDGE;
969 initialTexParams.fWrapT = GR_GL_CLAMP_TO_EDGE;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000970 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
971 GR_GL_TEXTURE_MAG_FILTER,
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +0000972 initialTexParams.fMagFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000973 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
974 GR_GL_TEXTURE_MIN_FILTER,
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +0000975 initialTexParams.fMinFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000976 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
977 GR_GL_TEXTURE_WRAP_S,
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000978 initialTexParams.fWrapS));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000979 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
980 GR_GL_TEXTURE_WRAP_T,
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000981 initialTexParams.fWrapT));
bsalomonb15b4c12014-10-29 12:41:57 -0700982 if (!this->uploadTexData(desc, true, 0, 0,
983 desc.fWidth, desc.fHeight,
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000984 desc.fConfig, srcData, rowBytes)) {
bsalomonb15b4c12014-10-29 12:41:57 -0700985 GL_CALL(DeleteTextures(1, &idDesc.fTextureID));
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000986 return return_null_texture();
bsalomon@google.com6f379512011-11-16 20:36:03 +0000987 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000988
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000989 GrGLTexture* tex;
reed@google.comac10a2d2010-12-22 21:39:39 +0000990 if (renderTarget) {
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +0000991 // unbind the texture from the texture unit before binding it to the frame buffer
992 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, 0));
bsalomon5236cf42015-01-14 10:42:08 -0800993 GrGLRenderTarget::IDDesc rtIDDesc;
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +0000994
egdanielb0e1be22015-04-22 13:27:39 -0700995 if (!this->createRenderTargetObjects(desc, lifeCycle, idDesc.fTextureID, &rtIDDesc)) {
bsalomonb15b4c12014-10-29 12:41:57 -0700996 GL_CALL(DeleteTextures(1, &idDesc.fTextureID));
reed@google.comac10a2d2010-12-22 21:39:39 +0000997 return return_null_texture();
998 }
bsalomon37dd3312014-11-03 08:47:23 -0800999 tex = SkNEW_ARGS(GrGLTextureRenderTarget, (this, desc, idDesc, rtIDDesc));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001000 } else {
bsalomonb15b4c12014-10-29 12:41:57 -07001001 tex = SkNEW_ARGS(GrGLTexture, (this, desc, idDesc));
reed@google.comac10a2d2010-12-22 21:39:39 +00001002 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001003 tex->setCachedTexParams(initialTexParams, this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00001004#ifdef TRACE_TEXTURE_CREATION
tfarina38406c82014-10-31 07:11:12 -07001005 SkDebugf("--- new texture [%d] size=(%d %d) config=%d\n",
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001006 glTexDesc.fTextureID, desc.fWidth, desc.fHeight, desc.fConfig);
reed@google.comac10a2d2010-12-22 21:39:39 +00001007#endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001008 return tex;
1009}
1010
egdanielb0e1be22015-04-22 13:27:39 -07001011GrTexture* GrGLGpu::onCreateCompressedTexture(const GrSurfaceDesc& desc,
1012 GrGpuResource::LifeCycle lifeCycle,
bsalomon5236cf42015-01-14 10:42:08 -08001013 const void* srcData) {
krajcevski9c0e6292014-06-02 07:38:14 -07001014 // Make sure that we're not flipping Y.
egdanielb0e1be22015-04-22 13:27:39 -07001015 if (kBottomLeft_GrSurfaceOrigin == desc.fOrigin) {
krajcevski9c0e6292014-06-02 07:38:14 -07001016 return return_null_texture();
1017 }
1018
bsalomonb15b4c12014-10-29 12:41:57 -07001019 GrGLTexture::IDDesc idDesc;
1020 GL_CALL(GenTextures(1, &idDesc.fTextureID));
egdanielb0e1be22015-04-22 13:27:39 -07001021 idDesc.fLifeCycle = lifeCycle;
krajcevski9c0e6292014-06-02 07:38:14 -07001022
bsalomonb15b4c12014-10-29 12:41:57 -07001023 if (!idDesc.fTextureID) {
krajcevski9c0e6292014-06-02 07:38:14 -07001024 return return_null_texture();
1025 }
1026
1027 this->setScratchTextureUnit();
bsalomonb15b4c12014-10-29 12:41:57 -07001028 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, idDesc.fTextureID));
krajcevski9c0e6292014-06-02 07:38:14 -07001029
1030 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
1031 // drivers have a bug where an FBO won't be complete if it includes a
1032 // texture that is not mipmap complete (considering the filter in use).
1033 GrGLTexture::TexParams initialTexParams;
1034 // we only set a subset here so invalidate first
1035 initialTexParams.invalidate();
1036 initialTexParams.fMinFilter = GR_GL_NEAREST;
1037 initialTexParams.fMagFilter = GR_GL_NEAREST;
1038 initialTexParams.fWrapS = GR_GL_CLAMP_TO_EDGE;
1039 initialTexParams.fWrapT = GR_GL_CLAMP_TO_EDGE;
1040 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1041 GR_GL_TEXTURE_MAG_FILTER,
1042 initialTexParams.fMagFilter));
1043 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1044 GR_GL_TEXTURE_MIN_FILTER,
1045 initialTexParams.fMinFilter));
1046 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1047 GR_GL_TEXTURE_WRAP_S,
1048 initialTexParams.fWrapS));
1049 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1050 GR_GL_TEXTURE_WRAP_T,
1051 initialTexParams.fWrapT));
1052
bsalomonb15b4c12014-10-29 12:41:57 -07001053 if (!this->uploadCompressedTexData(desc, srcData)) {
1054 GL_CALL(DeleteTextures(1, &idDesc.fTextureID));
krajcevski9c0e6292014-06-02 07:38:14 -07001055 return return_null_texture();
1056 }
1057
1058 GrGLTexture* tex;
bsalomonb15b4c12014-10-29 12:41:57 -07001059 tex = SkNEW_ARGS(GrGLTexture, (this, desc, idDesc));
krajcevski9c0e6292014-06-02 07:38:14 -07001060 tex->setCachedTexParams(initialTexParams, this->getResetTimestamp());
1061#ifdef TRACE_TEXTURE_CREATION
tfarina38406c82014-10-31 07:11:12 -07001062 SkDebugf("--- new compressed texture [%d] size=(%d %d) config=%d\n",
krajcevski9c0e6292014-06-02 07:38:14 -07001063 glTexDesc.fTextureID, desc.fWidth, desc.fHeight, desc.fConfig);
1064#endif
1065 return tex;
1066}
1067
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001068namespace {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001069
egdaniel8dc7c3a2015-04-16 11:22:42 -07001070const GrGLuint kUnknownBitCount = GrGLStencilAttachment::kUnknownBitCount;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001071
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001072void inline get_stencil_rb_sizes(const GrGLInterface* gl,
egdaniel8dc7c3a2015-04-16 11:22:42 -07001073 GrGLStencilAttachment::Format* format) {
sugoi@google.com6ba0b0f2013-05-03 13:52:32 +00001074
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001075 // we shouldn't ever know one size and not the other
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001076 SkASSERT((kUnknownBitCount == format->fStencilBits) ==
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001077 (kUnknownBitCount == format->fTotalBits));
1078 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001079 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001080 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1081 (GrGLint*)&format->fStencilBits);
1082 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001083 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001084 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1085 (GrGLint*)&format->fTotalBits);
1086 format->fTotalBits += format->fStencilBits;
1087 } else {
1088 format->fTotalBits = format->fStencilBits;
1089 }
1090 }
1091}
1092}
1093
egdaniel8dc7c3a2015-04-16 11:22:42 -07001094bool GrGLGpu::createStencilAttachmentForRenderTarget(GrRenderTarget* rt, int width, int height) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001095 // All internally created RTs are also textures. We don't create
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +00001096 // 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 +00001097 SkASSERT(rt->asTexture());
1098 SkASSERT(width >= rt->width());
1099 SkASSERT(height >= rt->height());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001100
1101 int samples = rt->numSamples();
egdaniel8dc7c3a2015-04-16 11:22:42 -07001102 GrGLStencilAttachment::IDDesc sbDesc;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001103
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001104 int stencilFmtCnt = this->glCaps().stencilFormats().count();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001105 for (int i = 0; i < stencilFmtCnt; ++i) {
kkinnunen36c57df2015-01-27 00:30:18 -08001106 if (!sbDesc.fRenderbufferID) {
1107 GL_CALL(GenRenderbuffers(1, &sbDesc.fRenderbufferID));
bsalomon12299ab2014-11-14 13:33:09 -08001108 }
kkinnunen36c57df2015-01-27 00:30:18 -08001109 if (!sbDesc.fRenderbufferID) {
bsalomon12299ab2014-11-14 13:33:09 -08001110 return false;
1111 }
kkinnunen36c57df2015-01-27 00:30:18 -08001112 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbDesc.fRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001113 // we start with the last stencil format that succeeded in hopes
1114 // that we won't go through this loop more than once after the
1115 // first (painful) stencil creation.
1116 int sIdx = (i + fLastSuccessfulStencilFmtIdx) % stencilFmtCnt;
bsalomon12299ab2014-11-14 13:33:09 -08001117 const GrGLCaps::StencilFormat& sFmt = this->glCaps().stencilFormats()[sIdx];
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001118 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001119 // we do this "if" so that we don't call the multisample
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001120 // version on a GL that doesn't have an MSAA extension.
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001121 bool created;
1122 if (samples > 0) {
robertphillips@google.com6177e692013-02-28 20:16:25 +00001123 created = renderbuffer_storage_msaa(fGLContext,
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001124 samples,
1125 sFmt.fInternalFormat,
1126 width, height);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001127 } else {
bsalomon12299ab2014-11-14 13:33:09 -08001128 GL_ALLOC_CALL(this->glInterface(), RenderbufferStorage(GR_GL_RENDERBUFFER,
1129 sFmt.fInternalFormat,
1130 width, height));
1131 created = (GR_GL_NO_ERROR == check_alloc_error(rt->desc(), this->glInterface()));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001132 }
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001133 if (created) {
egdaniel8dc7c3a2015-04-16 11:22:42 -07001134 fStats.incStencilAttachmentCreates();
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001135 // After sized formats we attempt an unsized format and take
1136 // whatever sizes GL gives us. In that case we query for the size.
egdaniel8dc7c3a2015-04-16 11:22:42 -07001137 GrGLStencilAttachment::Format format = sFmt;
sugoi@google.com6ba0b0f2013-05-03 13:52:32 +00001138 get_stencil_rb_sizes(this->glInterface(), &format);
egdaniel8dc7c3a2015-04-16 11:22:42 -07001139 SkAutoTUnref<GrGLStencilAttachment> sb(SkNEW_ARGS(GrGLStencilAttachment,
kkinnunen36c57df2015-01-27 00:30:18 -08001140 (this, sbDesc, width, height, samples, format)));
egdaniel8dc7c3a2015-04-16 11:22:42 -07001141 if (this->attachStencilAttachmentToRenderTarget(sb, rt)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001142 fLastSuccessfulStencilFmtIdx = sIdx;
egdaniel8dc7c3a2015-04-16 11:22:42 -07001143 rt->renderTargetPriv().didAttachStencilAttachment(sb);
egdanieldf603552015-03-18 13:26:11 -07001144// This work around is currently breaking on windows 7 hd2000 bot when we bind a color buffer
1145#if 0
bsalomondd3143b2015-02-23 09:27:45 -08001146 // Clear the stencil buffer. We use a special purpose FBO for this so that the
1147 // entire stencil buffer is cleared, even if it is attached to an FBO with a
1148 // smaller color target.
egdanield803f272015-03-18 13:01:52 -07001149 if (0 == fStencilClearFBOID) {
1150 GL_CALL(GenFramebuffers(1, &fStencilClearFBOID));
bsalomondd3143b2015-02-23 09:27:45 -08001151 }
robertphillips73165bd2015-03-01 07:38:15 -08001152
egdanield803f272015-03-18 13:01:52 -07001153 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, fStencilClearFBOID));
1154 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
1155 fStats.incRenderTargetBinds();
1156 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomondd3143b2015-02-23 09:27:45 -08001157 GR_GL_STENCIL_ATTACHMENT,
1158 GR_GL_RENDERBUFFER, sbDesc.fRenderbufferID));
1159 if (sFmt.fPacked) {
egdanield803f272015-03-18 13:01:52 -07001160 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomondd3143b2015-02-23 09:27:45 -08001161 GR_GL_DEPTH_ATTACHMENT,
1162 GR_GL_RENDERBUFFER, sbDesc.fRenderbufferID));
1163 }
1164
1165 GL_CALL(ClearStencil(0));
bsalomon9d2049d2015-03-17 12:06:19 -07001166 // Many GL implementations seem to have trouble with clearing an FBO with only
1167 // a stencil buffer.
1168 GrGLuint tempRB;
1169 GL_CALL(GenRenderbuffers(1, &tempRB));
1170 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, tempRB));
Brian Salomon9251d4e2015-03-17 16:03:19 -04001171 if (samples > 0) {
1172 renderbuffer_storage_msaa(fGLContext, samples, GR_GL_RGBA8, width, height);
1173 } else {
1174 GL_CALL(RenderbufferStorage(GR_GL_RENDERBUFFER, GR_GL_RGBA8, width, height));
1175 }
egdanield803f272015-03-18 13:01:52 -07001176 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon9d2049d2015-03-17 12:06:19 -07001177 GR_GL_COLOR_ATTACHMENT0,
1178 GR_GL_RENDERBUFFER, tempRB));
1179
bsalomondd3143b2015-02-23 09:27:45 -08001180 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
1181
egdanield803f272015-03-18 13:01:52 -07001182 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon9d2049d2015-03-17 12:06:19 -07001183 GR_GL_COLOR_ATTACHMENT0,
1184 GR_GL_RENDERBUFFER, 0));
1185 GL_CALL(DeleteRenderbuffers(1, &tempRB));
1186
bsalomondd3143b2015-02-23 09:27:45 -08001187 // Unbind the SB from the FBO so that we don't keep it alive.
egdanield803f272015-03-18 13:01:52 -07001188 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomondd3143b2015-02-23 09:27:45 -08001189 GR_GL_STENCIL_ATTACHMENT,
1190 GR_GL_RENDERBUFFER, 0));
1191 if (sFmt.fPacked) {
egdanield803f272015-03-18 13:01:52 -07001192 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomondd3143b2015-02-23 09:27:45 -08001193 GR_GL_DEPTH_ATTACHMENT,
1194 GR_GL_RENDERBUFFER, 0));
1195 }
egdanieldf603552015-03-18 13:26:11 -07001196#endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001197 return true;
bsalomon12299ab2014-11-14 13:33:09 -08001198 }
bsalomon10e23ca2014-11-25 05:52:06 -08001199 // Remove the scratch key from this resource so we don't grab it from the cache ever
1200 // again.
bsalomon3582d3e2015-02-13 14:20:05 -08001201 sb->resourcePriv().removeScratchKey();
bsalomon10e23ca2014-11-25 05:52:06 -08001202 // Set this to 0 since we handed the valid ID off to the failed stencil buffer resource.
kkinnunen36c57df2015-01-27 00:30:18 -08001203 sbDesc.fRenderbufferID = 0;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001204 }
1205 }
kkinnunen36c57df2015-01-27 00:30:18 -08001206 GL_CALL(DeleteRenderbuffers(1, &sbDesc.fRenderbufferID));
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001207 return false;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001208}
1209
egdaniel8dc7c3a2015-04-16 11:22:42 -07001210bool GrGLGpu::attachStencilAttachmentToRenderTarget(GrStencilAttachment* sb, GrRenderTarget* rt) {
bsalomon37dd3312014-11-03 08:47:23 -08001211 GrGLRenderTarget* glrt = static_cast<GrGLRenderTarget*>(rt);
egdanield803f272015-03-18 13:01:52 -07001212
1213 GrGLuint fbo = glrt->renderFBOID();
1214
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001215 if (NULL == sb) {
egdaniel8dc7c3a2015-04-16 11:22:42 -07001216 if (rt->renderTargetPriv().getStencilAttachment()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001217 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001218 GR_GL_STENCIL_ATTACHMENT,
1219 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001220 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001221 GR_GL_DEPTH_ATTACHMENT,
1222 GR_GL_RENDERBUFFER, 0));
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +00001223#ifdef SK_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001224 GrGLenum status;
1225 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001226 SkASSERT(GR_GL_FRAMEBUFFER_COMPLETE == status);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001227#endif
1228 }
1229 return true;
1230 } else {
egdaniel8dc7c3a2015-04-16 11:22:42 -07001231 GrGLStencilAttachment* glsb = static_cast<GrGLStencilAttachment*>(sb);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001232 GrGLuint rb = glsb->renderbufferID();
1233
egdanield803f272015-03-18 13:01:52 -07001234 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
1235 fStats.incRenderTargetBinds();
1236 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, fbo));
1237 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001238 GR_GL_STENCIL_ATTACHMENT,
1239 GR_GL_RENDERBUFFER, rb));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001240 if (glsb->format().fPacked) {
egdanield803f272015-03-18 13:01:52 -07001241 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001242 GR_GL_DEPTH_ATTACHMENT,
1243 GR_GL_RENDERBUFFER, rb));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001244 } else {
egdanield803f272015-03-18 13:01:52 -07001245 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001246 GR_GL_DEPTH_ATTACHMENT,
1247 GR_GL_RENDERBUFFER, 0));
reed@google.comac10a2d2010-12-22 21:39:39 +00001248 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001249
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001250 GrGLenum status;
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001251 if (!this->glCaps().isColorConfigAndStencilFormatVerified(rt->config(), glsb->format())) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001252 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1253 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
egdanield803f272015-03-18 13:01:52 -07001254 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1255 GR_GL_STENCIL_ATTACHMENT,
1256 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001257 if (glsb->format().fPacked) {
egdanield803f272015-03-18 13:01:52 -07001258 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1259 GR_GL_DEPTH_ATTACHMENT,
1260 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001261 }
1262 return false;
1263 } else {
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +00001264 fGLContext.caps()->markColorConfigAndStencilFormatAsVerified(
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001265 rt->config(),
1266 glsb->format());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001267 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001268 }
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001269 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001270 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001271}
1272
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001273////////////////////////////////////////////////////////////////////////////////
1274
bsalomon861e1032014-12-16 07:33:49 -08001275GrVertexBuffer* GrGLGpu::onCreateVertexBuffer(size_t size, bool dynamic) {
bsalomon@google.come49ad452013-02-20 19:33:20 +00001276 GrGLVertexBuffer::Desc desc;
1277 desc.fDynamic = dynamic;
1278 desc.fSizeInBytes = size;
bsalomon@google.come49ad452013-02-20 19:33:20 +00001279
bsalomon@google.com96966a52013-02-21 16:34:21 +00001280 if (this->glCaps().useNonVBOVertexAndIndexDynamicData() && desc.fDynamic) {
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001281 desc.fID = 0;
bsalomon@google.come49ad452013-02-20 19:33:20 +00001282 GrGLVertexBuffer* vertexBuffer = SkNEW_ARGS(GrGLVertexBuffer, (this, desc));
reed@google.comac10a2d2010-12-22 21:39:39 +00001283 return vertexBuffer;
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001284 } else {
1285 GL_CALL(GenBuffers(1, &desc.fID));
1286 if (desc.fID) {
bsalomon@google.com6918d482013-03-07 19:09:11 +00001287 fHWGeometryState.setVertexBufferID(this, desc.fID);
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001288 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1289 // make sure driver can allocate memory for this buffer
1290 GL_ALLOC_CALL(this->glInterface(),
1291 BufferData(GR_GL_ARRAY_BUFFER,
robertphillips@google.come9cd27d2013-10-16 17:48:11 +00001292 (GrGLsizeiptr) desc.fSizeInBytes,
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001293 NULL, // data ptr
1294 desc.fDynamic ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW));
1295 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
1296 GL_CALL(DeleteBuffers(1, &desc.fID));
bsalomon@google.com6918d482013-03-07 19:09:11 +00001297 this->notifyVertexBufferDelete(desc.fID);
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001298 return NULL;
1299 }
1300 GrGLVertexBuffer* vertexBuffer = SkNEW_ARGS(GrGLVertexBuffer, (this, desc));
1301 return vertexBuffer;
1302 }
1303 return NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +00001304 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001305}
1306
bsalomon861e1032014-12-16 07:33:49 -08001307GrIndexBuffer* GrGLGpu::onCreateIndexBuffer(size_t size, bool dynamic) {
bsalomon@google.come49ad452013-02-20 19:33:20 +00001308 GrGLIndexBuffer::Desc desc;
1309 desc.fDynamic = dynamic;
1310 desc.fSizeInBytes = size;
bsalomon@google.come49ad452013-02-20 19:33:20 +00001311
bsalomon@google.com96966a52013-02-21 16:34:21 +00001312 if (this->glCaps().useNonVBOVertexAndIndexDynamicData() && desc.fDynamic) {
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001313 desc.fID = 0;
bsalomon@google.come49ad452013-02-20 19:33:20 +00001314 GrIndexBuffer* indexBuffer = SkNEW_ARGS(GrGLIndexBuffer, (this, desc));
reed@google.comac10a2d2010-12-22 21:39:39 +00001315 return indexBuffer;
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001316 } else {
1317 GL_CALL(GenBuffers(1, &desc.fID));
1318 if (desc.fID) {
bsalomon@google.com6918d482013-03-07 19:09:11 +00001319 fHWGeometryState.setIndexBufferIDOnDefaultVertexArray(this, desc.fID);
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001320 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1321 // make sure driver can allocate memory for this buffer
1322 GL_ALLOC_CALL(this->glInterface(),
1323 BufferData(GR_GL_ELEMENT_ARRAY_BUFFER,
robertphillips@google.come9cd27d2013-10-16 17:48:11 +00001324 (GrGLsizeiptr) desc.fSizeInBytes,
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001325 NULL, // data ptr
1326 desc.fDynamic ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW));
1327 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
1328 GL_CALL(DeleteBuffers(1, &desc.fID));
bsalomon@google.com6918d482013-03-07 19:09:11 +00001329 this->notifyIndexBufferDelete(desc.fID);
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001330 return NULL;
1331 }
1332 GrIndexBuffer* indexBuffer = SkNEW_ARGS(GrGLIndexBuffer, (this, desc));
1333 return indexBuffer;
1334 }
1335 return NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +00001336 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001337}
1338
bsalomon3e791242014-12-17 13:43:13 -08001339void GrGLGpu::flushScissor(const GrScissorState& scissorState,
joshualitt77b13072014-10-27 14:51:01 -07001340 const GrGLIRect& rtViewport,
1341 GrSurfaceOrigin rtOrigin) {
robertphillipse85a32d2015-02-10 08:16:55 -08001342 if (scissorState.enabled()) {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001343 GrGLIRect scissor;
bsalomonb0bd4f62014-09-03 07:19:50 -07001344 scissor.setRelativeTo(rtViewport,
robertphillipse85a32d2015-02-10 08:16:55 -08001345 scissorState.rect().fLeft,
1346 scissorState.rect().fTop,
1347 scissorState.rect().width(),
1348 scissorState.rect().height(),
bsalomonb0bd4f62014-09-03 07:19:50 -07001349 rtOrigin);
bsalomon@google.coma3201942012-06-21 19:58:20 +00001350 // if the scissor fully contains the viewport then we fall through and
1351 // disable the scissor test.
bsalomonb0bd4f62014-09-03 07:19:50 -07001352 if (!scissor.contains(rtViewport)) {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001353 if (fHWScissorSettings.fRect != scissor) {
1354 scissor.pushToGLScissor(this->glInterface());
1355 fHWScissorSettings.fRect = scissor;
1356 }
1357 if (kYes_TriState != fHWScissorSettings.fEnabled) {
1358 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1359 fHWScissorSettings.fEnabled = kYes_TriState;
1360 }
1361 return;
1362 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001363 }
joshualitt77b13072014-10-27 14:51:01 -07001364
1365 // See fall through note above
1366 this->disableScissor();
1367}
1368
cdaltond0a840d2015-03-16 17:19:58 -07001369bool GrGLGpu::flushGLState(const DrawArgs& args) {
egdaniel080e6732014-12-22 07:35:52 -08001370 GrXferProcessor::BlendInfo blendInfo;
egdaniel8dd688b2015-01-22 10:16:09 -08001371 const GrPipeline& pipeline = *args.fPipeline;
1372 args.fPipeline->getXferProcessor()->getBlendInfo(&blendInfo);
egdaniel080e6732014-12-22 07:35:52 -08001373
egdaniel8dd688b2015-01-22 10:16:09 -08001374 this->flushDither(pipeline.isDitherState());
egdaniel080e6732014-12-22 07:35:52 -08001375 this->flushColorWrite(blendInfo.fWriteColor);
egdaniel8dd688b2015-01-22 10:16:09 -08001376 this->flushDrawFace(pipeline.getDrawFace());
bsalomonbc3d0de2014-12-15 13:45:03 -08001377
joshualitt873ad0e2015-01-20 09:08:51 -08001378 fCurrentProgram.reset(fProgramCache->getProgram(args));
bsalomon1f78c0a2014-12-17 09:43:13 -08001379 if (NULL == fCurrentProgram.get()) {
1380 SkDEBUGFAIL("Failed to create program!");
1381 return false;
bsalomonbc3d0de2014-12-15 13:45:03 -08001382 }
1383
bsalomon1f78c0a2014-12-17 09:43:13 -08001384 fCurrentProgram.get()->ref();
1385
1386 GrGLuint programID = fCurrentProgram->programID();
1387 if (fHWProgramID != programID) {
1388 GL_CALL(UseProgram(programID));
1389 fHWProgramID = programID;
1390 }
1391
egdanield803f272015-03-18 13:01:52 -07001392 if (blendInfo.fWriteColor) {
1393 this->flushBlend(blendInfo);
1394 }
bsalomon1f78c0a2014-12-17 09:43:13 -08001395
egdaniel8dd688b2015-01-22 10:16:09 -08001396 fCurrentProgram->setData(*args.fPrimitiveProcessor, pipeline, *args.fBatchTracker);
bsalomon1f78c0a2014-12-17 09:43:13 -08001397
egdaniel8dd688b2015-01-22 10:16:09 -08001398 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(pipeline.getRenderTarget());
1399 this->flushStencil(pipeline.getStencil());
1400 this->flushScissor(pipeline.getScissorState(), glRT->getViewport(), glRT->origin());
cdaltond0a840d2015-03-16 17:19:58 -07001401 this->flushHWAAState(glRT, pipeline.isHWAntialiasState());
bsalomonbc3d0de2014-12-15 13:45:03 -08001402
1403 // This must come after textures are flushed because a texture may need
egdanield803f272015-03-18 13:01:52 -07001404 // to be msaa-resolved (which will modify bound FBO state).
1405 this->flushRenderTarget(glRT, NULL);
bsalomonbc3d0de2014-12-15 13:45:03 -08001406
1407 return true;
1408}
1409
joshualitt873ad0e2015-01-20 09:08:51 -08001410void GrGLGpu::setupGeometry(const GrPrimitiveProcessor& primProc,
bsalomonbc3d0de2014-12-15 13:45:03 -08001411 const GrDrawTarget::DrawInfo& info,
1412 size_t* indexOffsetInBytes) {
1413 GrGLVertexBuffer* vbuf;
1414 vbuf = (GrGLVertexBuffer*) info.vertexBuffer();
1415
1416 SkASSERT(vbuf);
1417 SkASSERT(!vbuf->isMapped());
1418
1419 GrGLIndexBuffer* ibuf = NULL;
1420 if (info.isIndexed()) {
1421 SkASSERT(indexOffsetInBytes);
1422
1423 *indexOffsetInBytes = 0;
1424 ibuf = (GrGLIndexBuffer*)info.indexBuffer();
1425
1426 SkASSERT(ibuf);
1427 SkASSERT(!ibuf->isMapped());
1428 *indexOffsetInBytes += ibuf->baseOffset();
1429 }
1430 GrGLAttribArrayState* attribState =
1431 fHWGeometryState.bindArrayAndBuffersToDraw(this, vbuf, ibuf);
1432
joshualitt873ad0e2015-01-20 09:08:51 -08001433 int vaCount = primProc.numAttribs();
joshualitt71c92602015-01-14 08:12:47 -08001434 if (vaCount > 0) {
bsalomonbc3d0de2014-12-15 13:45:03 -08001435
joshualitt873ad0e2015-01-20 09:08:51 -08001436 GrGLsizei stride = static_cast<GrGLsizei>(primProc.getVertexStride());
bsalomonbc3d0de2014-12-15 13:45:03 -08001437
1438 size_t vertexOffsetInBytes = stride * info.startVertex();
1439
1440 vertexOffsetInBytes += vbuf->baseOffset();
1441
bsalomonbc3d0de2014-12-15 13:45:03 -08001442 uint32_t usedAttribArraysMask = 0;
1443 size_t offset = 0;
1444
1445 for (int attribIndex = 0; attribIndex < vaCount; attribIndex++) {
joshualitt873ad0e2015-01-20 09:08:51 -08001446 const GrGeometryProcessor::Attribute& attrib = primProc.getAttrib(attribIndex);
bsalomonbc3d0de2014-12-15 13:45:03 -08001447 usedAttribArraysMask |= (1 << attribIndex);
joshualitt71c92602015-01-14 08:12:47 -08001448 GrVertexAttribType attribType = attrib.fType;
bsalomonbc3d0de2014-12-15 13:45:03 -08001449 attribState->set(this,
1450 attribIndex,
1451 vbuf,
1452 GrGLAttribTypeToLayout(attribType).fCount,
1453 GrGLAttribTypeToLayout(attribType).fType,
1454 GrGLAttribTypeToLayout(attribType).fNormalized,
1455 stride,
1456 reinterpret_cast<GrGLvoid*>(vertexOffsetInBytes + offset));
joshualitt71c92602015-01-14 08:12:47 -08001457 offset += attrib.fOffset;
bsalomonbc3d0de2014-12-15 13:45:03 -08001458 }
1459 attribState->disableUnusedArrays(this, usedAttribArraysMask);
1460 }
1461}
1462
joshualitt873ad0e2015-01-20 09:08:51 -08001463void GrGLGpu::buildProgramDesc(GrProgramDesc* desc,
1464 const GrPrimitiveProcessor& primProc,
egdaniel8dd688b2015-01-22 10:16:09 -08001465 const GrPipeline& pipeline,
joshualitt873ad0e2015-01-20 09:08:51 -08001466 const GrBatchTracker& batchTracker) const {
bsalomon50785a32015-02-06 07:02:37 -08001467 if (!GrGLProgramDescBuilder::Build(desc, primProc, pipeline, this, batchTracker)) {
bsalomonbc3d0de2014-12-15 13:45:03 -08001468 SkDEBUGFAIL("Failed to generate GL program descriptor");
1469 }
1470}
1471
bsalomon861e1032014-12-16 07:33:49 -08001472void GrGLGpu::disableScissor() {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001473 if (kNo_TriState != fHWScissorSettings.fEnabled) {
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001474 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.coma3201942012-06-21 19:58:20 +00001475 fHWScissorSettings.fEnabled = kNo_TriState;
1476 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001477 }
1478}
1479
bsalomon861e1032014-12-16 07:33:49 -08001480void GrGLGpu::onClear(GrRenderTarget* target, const SkIRect* rect, GrColor color,
joshualitt4b68ec02014-11-07 14:11:45 -08001481 bool canIgnoreRect) {
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001482 // parent class should never let us get here with no RT
bsalomon49f085d2014-09-05 13:34:00 -07001483 SkASSERT(target);
bsalomonb0bd4f62014-09-03 07:19:50 -07001484 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001485
robertphillips@google.com56ce48a2013-10-31 21:44:25 +00001486 if (canIgnoreRect && this->glCaps().fullClearIsFree()) {
1487 rect = NULL;
1488 }
1489
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001490 SkIRect clippedRect;
bsalomon49f085d2014-09-05 13:34:00 -07001491 if (rect) {
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001492 // flushScissor expects rect to be clipped to the target.
bsalomon@google.com74b98712011-11-11 19:46:16 +00001493 clippedRect = *rect;
bsalomonb0bd4f62014-09-03 07:19:50 -07001494 SkIRect rtRect = SkIRect::MakeWH(target->width(), target->height());
bsalomon@google.com74b98712011-11-11 19:46:16 +00001495 if (clippedRect.intersect(rtRect)) {
1496 rect = &clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001497 } else {
1498 return;
1499 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001500 }
rmistry@google.comd6bab022013-12-02 13:50:38 +00001501
egdanield803f272015-03-18 13:01:52 -07001502 this->flushRenderTarget(glRT, rect);
bsalomon3e791242014-12-17 13:43:13 -08001503 GrScissorState scissorState;
robertphillipse85a32d2015-02-10 08:16:55 -08001504 if (rect) {
1505 scissorState.set(*rect);
bsalomon@google.coma3201942012-06-21 19:58:20 +00001506 }
joshualitt77b13072014-10-27 14:51:01 -07001507 this->flushScissor(scissorState, glRT->getViewport(), glRT->origin());
bsalomon@google.com74b98712011-11-11 19:46:16 +00001508
1509 GrGLfloat r, g, b, a;
1510 static const GrGLfloat scale255 = 1.f / 255.f;
1511 a = GrColorUnpackA(color) * scale255;
1512 GrGLfloat scaleRGB = scale255;
bsalomon@google.com74b98712011-11-11 19:46:16 +00001513 r = GrColorUnpackR(color) * scaleRGB;
1514 g = GrColorUnpackG(color) * scaleRGB;
1515 b = GrColorUnpackB(color) * scaleRGB;
1516
1517 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00001518 fHWWriteToColor = kYes_TriState;
bsalomon@google.com74b98712011-11-11 19:46:16 +00001519 GL_CALL(ClearColor(r, g, b, a));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001520 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001521}
1522
bsalomon861e1032014-12-16 07:33:49 -08001523void GrGLGpu::discard(GrRenderTarget* renderTarget) {
bsalomon89c62982014-11-03 12:08:42 -08001524 SkASSERT(renderTarget);
commit-bot@chromium.org52ffbf62014-04-02 16:19:33 +00001525 if (!this->caps()->discardRenderTargetSupport()) {
1526 return;
1527 }
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +00001528
1529 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(renderTarget);
egdanield803f272015-03-18 13:01:52 -07001530 if (renderTarget->getUniqueID() != fHWBoundRenderTargetUniqueID) {
1531 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
1532 fStats.incRenderTargetBinds();
1533 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, glRT->renderFBOID()));
1534 }
commit-bot@chromium.org52ffbf62014-04-02 16:19:33 +00001535 switch (this->glCaps().invalidateFBType()) {
joshualitt58162332014-08-01 06:44:53 -07001536 case GrGLCaps::kNone_InvalidateFBType:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +00001537 SkFAIL("Should never get here.");
commit-bot@chromium.org52ffbf62014-04-02 16:19:33 +00001538 break;
1539 case GrGLCaps::kInvalidate_InvalidateFBType:
egdanield803f272015-03-18 13:01:52 -07001540 if (0 == glRT->renderFBOID()) {
commit-bot@chromium.org52ffbf62014-04-02 16:19:33 +00001541 // When rendering to the default framebuffer the legal values for attachments
1542 // are GL_COLOR, GL_DEPTH, GL_STENCIL, ... rather than the various FBO attachment
1543 // types.
1544 static const GrGLenum attachments[] = { GR_GL_COLOR };
egdanield803f272015-03-18 13:01:52 -07001545 GL_CALL(InvalidateFramebuffer(GR_GL_FRAMEBUFFER, SK_ARRAY_COUNT(attachments),
commit-bot@chromium.org52ffbf62014-04-02 16:19:33 +00001546 attachments));
1547 } else {
1548 static const GrGLenum attachments[] = { GR_GL_COLOR_ATTACHMENT0 };
egdanield803f272015-03-18 13:01:52 -07001549 GL_CALL(InvalidateFramebuffer(GR_GL_FRAMEBUFFER, SK_ARRAY_COUNT(attachments),
commit-bot@chromium.org52ffbf62014-04-02 16:19:33 +00001550 attachments));
1551 }
1552 break;
1553 case GrGLCaps::kDiscard_InvalidateFBType: {
egdanield803f272015-03-18 13:01:52 -07001554 if (0 == glRT->renderFBOID()) {
commit-bot@chromium.org4453e8b2014-04-16 14:33:27 +00001555 // When rendering to the default framebuffer the legal values for attachments
1556 // are GL_COLOR, GL_DEPTH, GL_STENCIL, ... rather than the various FBO attachment
1557 // types. See glDiscardFramebuffer() spec.
1558 static const GrGLenum attachments[] = { GR_GL_COLOR };
egdanield803f272015-03-18 13:01:52 -07001559 GL_CALL(DiscardFramebuffer(GR_GL_FRAMEBUFFER, SK_ARRAY_COUNT(attachments),
commit-bot@chromium.org4453e8b2014-04-16 14:33:27 +00001560 attachments));
1561 } else {
1562 static const GrGLenum attachments[] = { GR_GL_COLOR_ATTACHMENT0 };
egdanield803f272015-03-18 13:01:52 -07001563 GL_CALL(DiscardFramebuffer(GR_GL_FRAMEBUFFER, SK_ARRAY_COUNT(attachments),
commit-bot@chromium.org4453e8b2014-04-16 14:33:27 +00001564 attachments));
1565 }
commit-bot@chromium.org52ffbf62014-04-02 16:19:33 +00001566 break;
1567 }
1568 }
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +00001569 renderTarget->flagAsResolved();
1570}
1571
1572
bsalomon861e1032014-12-16 07:33:49 -08001573void GrGLGpu::clearStencil(GrRenderTarget* target) {
bsalomonb0bd4f62014-09-03 07:19:50 -07001574 if (NULL == target) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001575 return;
1576 }
bsalomonb0bd4f62014-09-03 07:19:50 -07001577 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
egdanield803f272015-03-18 13:01:52 -07001578 this->flushRenderTarget(glRT, &SkIRect::EmptyIRect());
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001579
joshualitt77b13072014-10-27 14:51:01 -07001580 this->disableScissor();
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001581
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001582 GL_CALL(StencilMask(0xffffffff));
1583 GL_CALL(ClearStencil(0));
1584 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001585 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001586}
1587
bsalomon861e1032014-12-16 07:33:49 -08001588void GrGLGpu::onClearStencilClip(GrRenderTarget* target, const SkIRect& rect, bool insideClip) {
bsalomon49f085d2014-09-05 13:34:00 -07001589 SkASSERT(target);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001590
egdaniel8dc7c3a2015-04-16 11:22:42 -07001591 GrStencilAttachment* sb = target->renderTargetPriv().getStencilAttachment();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001592 // this should only be called internally when we know we have a
1593 // stencil buffer.
bsalomon6bc1b5f2015-02-23 09:06:38 -08001594 SkASSERT(sb);
1595 GrGLint stencilBitCount = sb->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001596#if 0
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001597 SkASSERT(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00001598 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001599#else
1600 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001601 // ANGLE a partial stencil mask will cause clears to be
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001602 // turned into draws. Our contract on GrDrawTarget says that
1603 // changing the clip between stencil passes may or may not
1604 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00001605 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001606#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001607 GrGLint value;
1608 if (insideClip) {
1609 value = (1 << (stencilBitCount - 1));
1610 } else {
1611 value = 0;
1612 }
bsalomonb0bd4f62014-09-03 07:19:50 -07001613 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
egdanield803f272015-03-18 13:01:52 -07001614 this->flushRenderTarget(glRT, &SkIRect::EmptyIRect());
bsalomon@google.coma3201942012-06-21 19:58:20 +00001615
bsalomon3e791242014-12-17 13:43:13 -08001616 GrScissorState scissorState;
robertphillipse85a32d2015-02-10 08:16:55 -08001617 scissorState.set(rect);
joshualitt77b13072014-10-27 14:51:01 -07001618 this->flushScissor(scissorState, glRT->getViewport(), glRT->origin());
bsalomon@google.coma3201942012-06-21 19:58:20 +00001619
caryclark@google.comcf6285b2012-06-06 12:09:01 +00001620 GL_CALL(StencilMask((uint32_t) clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001621 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001622 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001623 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001624}
1625
bsalomon861e1032014-12-16 07:33:49 -08001626bool GrGLGpu::readPixelsWillPayForYFlip(GrRenderTarget* renderTarget,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001627 int left, int top,
1628 int width, int height,
1629 GrPixelConfig config,
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001630 size_t rowBytes) const {
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001631 // If this rendertarget is aready TopLeft, we don't need to flip.
1632 if (kTopLeft_GrSurfaceOrigin == renderTarget->origin()) {
1633 return false;
1634 }
1635
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001636 // if GL can do the flip then we'll never pay for it.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001637 if (this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001638 return false;
1639 }
1640
1641 // If we have to do memcpy to handle non-trim rowBytes then we
bsalomon@google.com7107fa72011-11-10 14:54:14 +00001642 // get the flip for free. Otherwise it costs.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001643 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001644 return true;
1645 }
1646 // If we have to do memcpys to handle rowBytes then y-flip is free
1647 // Note the rowBytes might be tight to the passed in data, but if data
1648 // gets clipped in x to the target the rowBytes will no longer be tight.
1649 if (left >= 0 && (left + width) < renderTarget->width()) {
1650 return 0 == rowBytes ||
1651 GrBytesPerPixel(config) * width == rowBytes;
1652 } else {
1653 return false;
1654 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001655}
1656
bsalomon861e1032014-12-16 07:33:49 -08001657bool GrGLGpu::onReadPixels(GrRenderTarget* target,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001658 int left, int top,
1659 int width, int height,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001660 GrPixelConfig config,
1661 void* buffer,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001662 size_t rowBytes) {
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +00001663 // We cannot read pixels into a compressed buffer
1664 if (GrPixelConfigIsCompressed(config)) {
1665 return false;
1666 }
1667
1668 GrGLenum format = 0;
1669 GrGLenum type = 0;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001670 bool flipY = kBottomLeft_GrSurfaceOrigin == target->origin();
bsalomon@google.com280e99f2012-01-05 16:17:38 +00001671 if (!this->configToGLFormats(config, false, NULL, &format, &type)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001672 return false;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001673 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001674 size_t bpp = GrBytesPerPixel(config);
1675 if (!adjust_pixel_ops_params(target->width(), target->height(), bpp,
1676 &left, &top, &width, &height,
1677 const_cast<const void**>(&buffer),
1678 &rowBytes)) {
1679 return false;
1680 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001681
bsalomon@google.comc6980972011-11-02 19:57:21 +00001682 // resolve the render target if necessary
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001683 GrGLRenderTarget* tgt = static_cast<GrGLRenderTarget*>(target);
egdanield803f272015-03-18 13:01:52 -07001684 switch (tgt->getResolveType()) {
1685 case GrGLRenderTarget::kCantResolve_ResolveType:
1686 return false;
1687 case GrGLRenderTarget::kAutoResolves_ResolveType:
1688 this->flushRenderTarget(static_cast<GrGLRenderTarget*>(target), &SkIRect::EmptyIRect());
1689 break;
1690 case GrGLRenderTarget::kCanResolve_ResolveType:
1691 this->onResolveRenderTarget(tgt);
1692 // we don't track the state of the READ FBO ID.
1693 fStats.incRenderTargetBinds();
1694 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1695 tgt->textureFBOID()));
1696 break;
1697 default:
1698 SkFAIL("Unknown resolve type");
reed@google.comac10a2d2010-12-22 21:39:39 +00001699 }
1700
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001701 const GrGLIRect& glvp = tgt->getViewport();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001702
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001703 // the read rect is viewport-relative
1704 GrGLIRect readRect;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001705 readRect.setRelativeTo(glvp, left, top, width, height, target->origin());
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001706
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001707 size_t tightRowBytes = bpp * width;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001708 if (0 == rowBytes) {
1709 rowBytes = tightRowBytes;
1710 }
1711 size_t readDstRowBytes = tightRowBytes;
1712 void* readDst = buffer;
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001713
bsalomon@google.comc6980972011-11-02 19:57:21 +00001714 // determine if GL can read using the passed rowBytes or if we need
1715 // a scratch buffer.
georgeb62508b2014-08-12 18:00:47 -07001716 GrAutoMalloc<32 * sizeof(GrColor)> scratch;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001717 if (rowBytes != tightRowBytes) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001718 if (this->glCaps().packRowLengthSupport()) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001719 SkASSERT(!(rowBytes % sizeof(GrColor)));
skia.committer@gmail.com4677acc2013-10-17 07:02:33 +00001720 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH,
robertphillips@google.come9cd27d2013-10-16 17:48:11 +00001721 static_cast<GrGLint>(rowBytes / sizeof(GrColor))));
bsalomon@google.comc6980972011-11-02 19:57:21 +00001722 readDstRowBytes = rowBytes;
1723 } else {
1724 scratch.reset(tightRowBytes * height);
1725 readDst = scratch.get();
1726 }
1727 }
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001728 if (flipY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001729 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 1));
1730 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001731 GL_CALL(ReadPixels(readRect.fLeft, readRect.fBottom,
1732 readRect.fWidth, readRect.fHeight,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001733 format, type, readDst));
1734 if (readDstRowBytes != tightRowBytes) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001735 SkASSERT(this->glCaps().packRowLengthSupport());
bsalomon@google.comc6980972011-11-02 19:57:21 +00001736 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
1737 }
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001738 if (flipY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001739 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 0));
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001740 flipY = false;
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001741 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001742
1743 // now reverse the order of the rows, since GL's are bottom-to-top, but our
bsalomon@google.comc6980972011-11-02 19:57:21 +00001744 // API presents top-to-bottom. We must preserve the padding contents. Note
1745 // that the above readPixels did not overwrite the padding.
1746 if (readDst == buffer) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001747 SkASSERT(rowBytes == readDstRowBytes);
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001748 if (flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001749 scratch.reset(tightRowBytes);
1750 void* tmpRow = scratch.get();
1751 // flip y in-place by rows
1752 const int halfY = height >> 1;
1753 char* top = reinterpret_cast<char*>(buffer);
1754 char* bottom = top + (height - 1) * rowBytes;
1755 for (int y = 0; y < halfY; y++) {
1756 memcpy(tmpRow, top, tightRowBytes);
1757 memcpy(top, bottom, tightRowBytes);
1758 memcpy(bottom, tmpRow, tightRowBytes);
1759 top += rowBytes;
1760 bottom -= rowBytes;
1761 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001762 }
1763 } else {
egdanield803f272015-03-18 13:01:52 -07001764 SkASSERT(readDst != buffer); SkASSERT(rowBytes != tightRowBytes);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001765 // copy from readDst to buffer while flipping y
caryclark@google.comcf6285b2012-06-06 12:09:01 +00001766 // const int halfY = height >> 1;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001767 const char* src = reinterpret_cast<const char*>(readDst);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001768 char* dst = reinterpret_cast<char*>(buffer);
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001769 if (flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001770 dst += (height-1) * rowBytes;
1771 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001772 for (int y = 0; y < height; y++) {
1773 memcpy(dst, src, tightRowBytes);
1774 src += readDstRowBytes;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001775 if (!flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001776 dst += rowBytes;
1777 } else {
1778 dst -= rowBytes;
1779 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001780 }
1781 }
1782 return true;
1783}
1784
egdanield803f272015-03-18 13:01:52 -07001785void GrGLGpu::flushRenderTarget(GrGLRenderTarget* target, const SkIRect* bound) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001786
egdanield803f272015-03-18 13:01:52 -07001787 SkASSERT(target);
bsalomon6ba6fa12015-03-04 11:57:37 -08001788
egdanield803f272015-03-18 13:01:52 -07001789 uint32_t rtID = target->getUniqueID();
1790 if (fHWBoundRenderTargetUniqueID != rtID) {
bsalomon1e0bf7e2015-03-14 12:08:51 -07001791 fStats.incRenderTargetBinds();
egdanield803f272015-03-18 13:01:52 -07001792 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, target->renderFBOID()));
1793#ifdef SK_DEBUG
1794 // don't do this check in Chromium -- this is causing
1795 // lots of repeated command buffer flushes when the compositor is
1796 // rendering with Ganesh, which is really slow; even too slow for
1797 // Debug mode.
1798 if (!this->glContext().isChromium()) {
1799 GrGLenum status;
1800 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1801 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
1802 SkDebugf("GrGLGpu::flushRenderTarget glCheckFramebufferStatus %x\n", status);
1803 }
bsalomon160f24c2015-03-17 15:55:42 -07001804 }
egdanield803f272015-03-18 13:01:52 -07001805#endif
1806 fHWBoundRenderTargetUniqueID = rtID;
1807 const GrGLIRect& vp = target->getViewport();
1808 if (fHWViewport != vp) {
1809 vp.pushToGLViewport(this->glInterface());
1810 fHWViewport = vp;
bsalomon160f24c2015-03-17 15:55:42 -07001811 }
bsalomon5cd020f2015-03-17 12:46:56 -07001812 }
egdanield803f272015-03-18 13:01:52 -07001813 if (NULL == bound || !bound->isEmpty()) {
1814 target->flagAsNeedingResolve(bound);
1815 }
1816
1817 GrTexture *texture = target->asTexture();
1818 if (texture) {
1819 texture->texturePriv().dirtyMipMaps(true);
1820 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001821}
1822
twiz@google.com0f31ca72011-03-18 17:38:11 +00001823GrGLenum gPrimitiveType2GLMode[] = {
1824 GR_GL_TRIANGLES,
1825 GR_GL_TRIANGLE_STRIP,
1826 GR_GL_TRIANGLE_FAN,
1827 GR_GL_POINTS,
1828 GR_GL_LINES,
1829 GR_GL_LINE_STRIP
reed@google.comac10a2d2010-12-22 21:39:39 +00001830};
1831
bsalomon@google.comd302f142011-03-03 13:54:13 +00001832#define SWAP_PER_DRAW 0
1833
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001834#if SWAP_PER_DRAW
commit-bot@chromium.org43823302013-09-25 20:57:51 +00001835 #if defined(SK_BUILD_FOR_MAC)
bsalomon@google.comd302f142011-03-03 13:54:13 +00001836 #include <AGL/agl.h>
commit-bot@chromium.org43823302013-09-25 20:57:51 +00001837 #elif defined(SK_BUILD_FOR_WIN32)
bsalomon@google.comce7357d2012-06-25 17:49:25 +00001838 #include <gl/GL.h>
bsalomon@google.comd302f142011-03-03 13:54:13 +00001839 void SwapBuf() {
1840 DWORD procID = GetCurrentProcessId();
1841 HWND hwnd = GetTopWindow(GetDesktopWindow());
1842 while(hwnd) {
1843 DWORD wndProcID = 0;
1844 GetWindowThreadProcessId(hwnd, &wndProcID);
1845 if(wndProcID == procID) {
1846 SwapBuffers(GetDC(hwnd));
1847 }
1848 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
1849 }
1850 }
1851 #endif
1852#endif
1853
joshualitt873ad0e2015-01-20 09:08:51 -08001854void GrGLGpu::onDraw(const DrawArgs& args, const GrDrawTarget::DrawInfo& info) {
cdaltond0a840d2015-03-16 17:19:58 -07001855 if (!this->flushGLState(args)) {
bsalomond95263c2014-12-16 13:05:12 -08001856 return;
1857 }
1858
joshualitt873ad0e2015-01-20 09:08:51 -08001859 size_t indexOffsetInBytes = 0;
1860 this->setupGeometry(*args.fPrimitiveProcessor, info, &indexOffsetInBytes);
reed@google.comac10a2d2010-12-22 21:39:39 +00001861
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +00001862 SkASSERT((size_t)info.primitiveType() < SK_ARRAY_COUNT(gPrimitiveType2GLMode));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001863
bsalomon@google.com74749cd2013-01-30 16:12:41 +00001864 if (info.isIndexed()) {
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00001865 GrGLvoid* indices =
1866 reinterpret_cast<GrGLvoid*>(indexOffsetInBytes + sizeof(uint16_t) * info.startIndex());
bsalomon@google.com74749cd2013-01-30 16:12:41 +00001867 // info.startVertex() was accounted for by setupGeometry.
1868 GL_CALL(DrawElements(gPrimitiveType2GLMode[info.primitiveType()],
1869 info.indexCount(),
1870 GR_GL_UNSIGNED_SHORT,
1871 indices));
1872 } else {
1873 // Pass 0 for parameter first. We have to adjust glVertexAttribPointer() to account for
1874 // startVertex in the DrawElements case. So we always rely on setupGeometry to have
1875 // accounted for startVertex.
1876 GL_CALL(DrawArrays(gPrimitiveType2GLMode[info.primitiveType()], 0, info.vertexCount()));
1877 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00001878#if SWAP_PER_DRAW
1879 glFlush();
commit-bot@chromium.org43823302013-09-25 20:57:51 +00001880 #if defined(SK_BUILD_FOR_MAC)
bsalomon@google.comd302f142011-03-03 13:54:13 +00001881 aglSwapBuffers(aglGetCurrentContext());
1882 int set_a_break_pt_here = 9;
1883 aglSwapBuffers(aglGetCurrentContext());
commit-bot@chromium.org43823302013-09-25 20:57:51 +00001884 #elif defined(SK_BUILD_FOR_WIN32)
bsalomon@google.comd302f142011-03-03 13:54:13 +00001885 SwapBuf();
1886 int set_a_break_pt_here = 9;
1887 SwapBuf();
1888 #endif
1889#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001890}
1891
bsalomon3e791242014-12-17 13:43:13 -08001892void GrGLGpu::onStencilPath(const GrPath* path, const StencilPathState& state) {
egdaniel080e6732014-12-22 07:35:52 -08001893 this->flushColorWrite(false);
egdaniel8dd688b2015-01-22 10:16:09 -08001894 this->flushDrawFace(GrPipelineBuilder::kBoth_DrawFace);
bsalomon3e791242014-12-17 13:43:13 -08001895
1896 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(state.fRenderTarget);
bsalomon1f78c0a2014-12-17 09:43:13 -08001897 SkISize size = SkISize::Make(rt->width(), rt->height());
bsalomon3e791242014-12-17 13:43:13 -08001898 this->glPathRendering()->setProjectionMatrix(*state.fViewMatrix, size, rt->origin());
1899 this->flushScissor(*state.fScissor, rt->getViewport(), rt->origin());
cdaltond0a840d2015-03-16 17:19:58 -07001900 this->flushHWAAState(rt, state.fUseHWAA);
egdanield803f272015-03-18 13:01:52 -07001901 this->flushRenderTarget(rt, NULL);
1902
bsalomon3e791242014-12-17 13:43:13 -08001903 fPathRendering->stencilPath(path, *state.fStencil);
bsalomond95263c2014-12-16 13:05:12 -08001904}
1905
joshualitt873ad0e2015-01-20 09:08:51 -08001906void GrGLGpu::onDrawPath(const DrawArgs& args, const GrPath* path,
bsalomond95263c2014-12-16 13:05:12 -08001907 const GrStencilSettings& stencil) {
cdaltond0a840d2015-03-16 17:19:58 -07001908 if (!this->flushGLState(args)) {
bsalomond95263c2014-12-16 13:05:12 -08001909 return;
1910 }
1911 fPathRendering->drawPath(path, stencil);
1912}
1913
joshualitt873ad0e2015-01-20 09:08:51 -08001914void GrGLGpu::onDrawPaths(const DrawArgs& args,
bsalomond95263c2014-12-16 13:05:12 -08001915 const GrPathRange* pathRange,
1916 const void* indices,
1917 GrDrawTarget::PathIndexType indexType,
1918 const float transformValues[],
1919 GrDrawTarget::PathTransformType transformType,
1920 int count,
1921 const GrStencilSettings& stencil) {
cdaltond0a840d2015-03-16 17:19:58 -07001922 if (!this->flushGLState(args)) {
bsalomond95263c2014-12-16 13:05:12 -08001923 return;
1924 }
1925 fPathRendering->drawPaths(pathRange, indices, indexType, transformValues,
1926 transformType, count, stencil);
1927}
1928
bsalomon861e1032014-12-16 07:33:49 -08001929void GrGLGpu::onResolveRenderTarget(GrRenderTarget* target) {
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001930 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001931 if (rt->needsResolve()) {
bsalomon@google.com347c3822013-05-01 20:10:01 +00001932 // Some extensions automatically resolves the texture when it is read.
1933 if (this->glCaps().usesMSAARenderBuffers()) {
egdanield803f272015-03-18 13:01:52 -07001934 SkASSERT(rt->textureFBOID() != rt->renderFBOID());
1935 fStats.incRenderTargetBinds();
1936 fStats.incRenderTargetBinds();
1937 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER, rt->renderFBOID()));
1938 GL_CALL(BindFramebuffer(GR_GL_DRAW_FRAMEBUFFER, rt->textureFBOID()));
1939 // make sure we go through flushRenderTarget() since we've modified
1940 // the bound DRAW FBO ID.
1941 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001942 const GrGLIRect& vp = rt->getViewport();
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001943 const SkIRect dirtyRect = rt->getResolveRect();
reed@google.comac10a2d2010-12-22 21:39:39 +00001944
bsalomon@google.com347c3822013-05-01 20:10:01 +00001945 if (GrGLCaps::kES_Apple_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001946 // Apple's extension uses the scissor as the blit bounds.
bsalomon3e791242014-12-17 13:43:13 -08001947 GrScissorState scissorState;
robertphillipse85a32d2015-02-10 08:16:55 -08001948 scissorState.set(dirtyRect);
1949 this->flushScissor(scissorState, vp, rt->origin());
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001950 GL_CALL(ResolveMultisampleFramebuffer());
1951 } else {
robertphillipse85a32d2015-02-10 08:16:55 -08001952 GrGLIRect r;
1953 r.setRelativeTo(vp, dirtyRect.fLeft, dirtyRect.fTop,
1954 dirtyRect.width(), dirtyRect.height(), target->origin());
1955
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001956 int right = r.fLeft + r.fWidth;
1957 int top = r.fBottom + r.fHeight;
derekf8c8f71a2014-09-16 06:24:57 -07001958
1959 // BlitFrameBuffer respects the scissor, so disable it.
joshualitt77b13072014-10-27 14:51:01 -07001960 this->disableScissor();
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001961 GL_CALL(BlitFramebuffer(r.fLeft, r.fBottom, right, top,
1962 r.fLeft, r.fBottom, right, top,
1963 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001964 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001965 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001966 rt->flagAsResolved();
reed@google.comac10a2d2010-12-22 21:39:39 +00001967 }
1968}
1969
bsalomon@google.com411dad02012-06-05 20:24:20 +00001970namespace {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001971
bsalomon@google.com411dad02012-06-05 20:24:20 +00001972
1973GrGLenum gr_to_gl_stencil_op(GrStencilOp op) {
1974 static const GrGLenum gTable[] = {
1975 GR_GL_KEEP, // kKeep_StencilOp
1976 GR_GL_REPLACE, // kReplace_StencilOp
1977 GR_GL_INCR_WRAP, // kIncWrap_StencilOp
1978 GR_GL_INCR, // kIncClamp_StencilOp
1979 GR_GL_DECR_WRAP, // kDecWrap_StencilOp
1980 GR_GL_DECR, // kDecClamp_StencilOp
1981 GR_GL_ZERO, // kZero_StencilOp
1982 GR_GL_INVERT, // kInvert_StencilOp
1983 };
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +00001984 GR_STATIC_ASSERT(SK_ARRAY_COUNT(gTable) == kStencilOpCount);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001985 GR_STATIC_ASSERT(0 == kKeep_StencilOp);
1986 GR_STATIC_ASSERT(1 == kReplace_StencilOp);
1987 GR_STATIC_ASSERT(2 == kIncWrap_StencilOp);
1988 GR_STATIC_ASSERT(3 == kIncClamp_StencilOp);
1989 GR_STATIC_ASSERT(4 == kDecWrap_StencilOp);
1990 GR_STATIC_ASSERT(5 == kDecClamp_StencilOp);
1991 GR_STATIC_ASSERT(6 == kZero_StencilOp);
1992 GR_STATIC_ASSERT(7 == kInvert_StencilOp);
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001993 SkASSERT((unsigned) op < kStencilOpCount);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001994 return gTable[op];
1995}
1996
1997void set_gl_stencil(const GrGLInterface* gl,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001998 const GrStencilSettings& settings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00001999 GrGLenum glFace,
bsalomon@google.coma3201942012-06-21 19:58:20 +00002000 GrStencilSettings::Face grFace) {
kkinnunenccdaa042014-08-20 01:36:23 -07002001 GrGLenum glFunc = GrToGLStencilFunc(settings.func(grFace));
bsalomon@google.coma3201942012-06-21 19:58:20 +00002002 GrGLenum glFailOp = gr_to_gl_stencil_op(settings.failOp(grFace));
2003 GrGLenum glPassOp = gr_to_gl_stencil_op(settings.passOp(grFace));
2004
2005 GrGLint ref = settings.funcRef(grFace);
2006 GrGLint mask = settings.funcMask(grFace);
2007 GrGLint writeMask = settings.writeMask(grFace);
bsalomon@google.com411dad02012-06-05 20:24:20 +00002008
2009 if (GR_GL_FRONT_AND_BACK == glFace) {
2010 // we call the combined func just in case separate stencil is not
2011 // supported.
2012 GR_GL_CALL(gl, StencilFunc(glFunc, ref, mask));
2013 GR_GL_CALL(gl, StencilMask(writeMask));
2014 GR_GL_CALL(gl, StencilOp(glFailOp, glPassOp, glPassOp));
2015 } else {
2016 GR_GL_CALL(gl, StencilFuncSeparate(glFace, glFunc, ref, mask));
2017 GR_GL_CALL(gl, StencilMaskSeparate(glFace, writeMask));
2018 GR_GL_CALL(gl, StencilOpSeparate(glFace, glFailOp, glPassOp, glPassOp));
2019 }
2020}
2021}
bsalomon@google.comd302f142011-03-03 13:54:13 +00002022
bsalomon3e791242014-12-17 13:43:13 -08002023void GrGLGpu::flushStencil(const GrStencilSettings& stencilSettings) {
2024 if (fHWStencilSettings != stencilSettings) {
joshualitta58fe352014-10-27 08:39:00 -07002025 if (stencilSettings.isDisabled()) {
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00002026 if (kNo_TriState != fHWStencilTestEnabled) {
2027 GL_CALL(Disable(GR_GL_STENCIL_TEST));
2028 fHWStencilTestEnabled = kNo_TriState;
2029 }
2030 } else {
2031 if (kYes_TriState != fHWStencilTestEnabled) {
2032 GL_CALL(Enable(GR_GL_STENCIL_TEST));
2033 fHWStencilTestEnabled = kYes_TriState;
2034 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00002035 }
joshualitta58fe352014-10-27 08:39:00 -07002036 if (!stencilSettings.isDisabled()) {
bsalomon@google.combcce8922013-03-25 15:38:39 +00002037 if (this->caps()->twoSidedStencilSupport()) {
bsalomon@google.com411dad02012-06-05 20:24:20 +00002038 set_gl_stencil(this->glInterface(),
joshualitta58fe352014-10-27 08:39:00 -07002039 stencilSettings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00002040 GR_GL_FRONT,
bsalomon@google.coma3201942012-06-21 19:58:20 +00002041 GrStencilSettings::kFront_Face);
bsalomon@google.com411dad02012-06-05 20:24:20 +00002042 set_gl_stencil(this->glInterface(),
joshualitta58fe352014-10-27 08:39:00 -07002043 stencilSettings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00002044 GR_GL_BACK,
bsalomon@google.coma3201942012-06-21 19:58:20 +00002045 GrStencilSettings::kBack_Face);
bsalomon@google.comd302f142011-03-03 13:54:13 +00002046 } else {
bsalomon@google.com411dad02012-06-05 20:24:20 +00002047 set_gl_stencil(this->glInterface(),
joshualitta58fe352014-10-27 08:39:00 -07002048 stencilSettings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00002049 GR_GL_FRONT_AND_BACK,
bsalomon@google.coma3201942012-06-21 19:58:20 +00002050 GrStencilSettings::kFront_Face);
bsalomon@google.comd302f142011-03-03 13:54:13 +00002051 }
2052 }
joshualitta58fe352014-10-27 08:39:00 -07002053 fHWStencilSettings = stencilSettings;
reed@google.comac10a2d2010-12-22 21:39:39 +00002054 }
2055}
2056
cdaltond0a840d2015-03-16 17:19:58 -07002057void GrGLGpu::flushHWAAState(GrRenderTarget* rt, bool useHWAA) {
2058 SkASSERT(!useHWAA || rt->isMultisampled());
bsalomon@google.com202d1392013-03-19 18:58:08 +00002059
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +00002060 if (kGL_GrGLStandard == this->glStandard()) {
cdaltond0a840d2015-03-16 17:19:58 -07002061 if (useHWAA) {
2062 if (kYes_TriState != fMSAAEnabled) {
2063 GL_CALL(Enable(GR_GL_MULTISAMPLE));
2064 fMSAAEnabled = kYes_TriState;
2065 }
2066 } else {
2067 if (kNo_TriState != fMSAAEnabled) {
2068 GL_CALL(Disable(GR_GL_MULTISAMPLE));
2069 fMSAAEnabled = kNo_TriState;
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00002070 }
2071 }
2072 }
2073}
2074
egdaniel080e6732014-12-22 07:35:52 -08002075void GrGLGpu::flushBlend(const GrXferProcessor::BlendInfo& blendInfo) {
egdanielb414f252014-07-29 13:15:47 -07002076 // Any optimization to disable blending should have already been applied and
2077 // tweaked the coeffs to (1, 0).
egdanielc2304142014-12-11 13:15:13 -08002078
egdanielc2304142014-12-11 13:15:13 -08002079 GrBlendCoeff srcCoeff = blendInfo.fSrcBlend;
2080 GrBlendCoeff dstCoeff = blendInfo.fDstBlend;
egdanielb414f252014-07-29 13:15:47 -07002081 bool blendOff = kOne_GrBlendCoeff == srcCoeff && kZero_GrBlendCoeff == dstCoeff;
2082 if (blendOff) {
2083 if (kNo_TriState != fHWBlendState.fEnabled) {
2084 GL_CALL(Disable(GR_GL_BLEND));
2085 fHWBlendState.fEnabled = kNo_TriState;
2086 }
2087 } else {
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002088 if (kYes_TriState != fHWBlendState.fEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002089 GL_CALL(Enable(GR_GL_BLEND));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002090 fHWBlendState.fEnabled = kYes_TriState;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002091 }
egdanielb414f252014-07-29 13:15:47 -07002092 if (fHWBlendState.fSrcCoeff != srcCoeff ||
2093 fHWBlendState.fDstCoeff != dstCoeff) {
2094 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
2095 gXfermodeCoeff2Blend[dstCoeff]));
2096 fHWBlendState.fSrcCoeff = srcCoeff;
2097 fHWBlendState.fDstCoeff = dstCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002098 }
egdanielc2304142014-12-11 13:15:13 -08002099 GrColor blendConst = blendInfo.fBlendConstant;
egdanielb414f252014-07-29 13:15:47 -07002100 if ((BlendCoeffReferencesConstant(srcCoeff) ||
2101 BlendCoeffReferencesConstant(dstCoeff)) &&
2102 (!fHWBlendState.fConstColorValid ||
2103 fHWBlendState.fConstColor != blendConst)) {
2104 GrGLfloat c[4];
2105 GrColorToRGBAFloat(blendConst, c);
2106 GL_CALL(BlendColor(c[0], c[1], c[2], c[3]));
2107 fHWBlendState.fConstColor = blendConst;
2108 fHWBlendState.fConstColorValid = true;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002109 }
2110 }
2111}
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002112
commit-bot@chromium.org6364b5e2013-08-20 20:22:52 +00002113static inline GrGLenum tile_to_gl_wrap(SkShader::TileMode tm) {
bsalomon@google.comb8670992012-07-25 21:27:09 +00002114 static const GrGLenum gWrapModes[] = {
2115 GR_GL_CLAMP_TO_EDGE,
2116 GR_GL_REPEAT,
2117 GR_GL_MIRRORED_REPEAT
2118 };
commit-bot@chromium.org5d7ca952013-04-22 20:26:44 +00002119 GR_STATIC_ASSERT(SkShader::kTileModeCount == SK_ARRAY_COUNT(gWrapModes));
bsalomon@google.comb8670992012-07-25 21:27:09 +00002120 GR_STATIC_ASSERT(0 == SkShader::kClamp_TileMode);
2121 GR_STATIC_ASSERT(1 == SkShader::kRepeat_TileMode);
2122 GR_STATIC_ASSERT(2 == SkShader::kMirror_TileMode);
2123 return gWrapModes[tm];
2124}
2125
bsalomon861e1032014-12-16 07:33:49 -08002126void GrGLGpu::bindTexture(int unitIdx, const GrTextureParams& params, GrGLTexture* texture) {
bsalomon49f085d2014-09-05 13:34:00 -07002127 SkASSERT(texture);
tomhudson@google.comd0c1a062012-07-12 17:23:52 +00002128
bsalomon@google.comb8670992012-07-25 21:27:09 +00002129 // If we created a rt/tex and rendered to it without using a texture and now we're texturing
2130 // 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 +00002131 // out of the "last != next" check.
bsalomon37dd3312014-11-03 08:47:23 -08002132 GrGLRenderTarget* texRT = static_cast<GrGLRenderTarget*>(texture->asRenderTarget());
bsalomon49f085d2014-09-05 13:34:00 -07002133 if (texRT) {
bsalomon@google.com4c883782012-06-04 19:05:11 +00002134 this->onResolveRenderTarget(texRT);
2135 }
2136
bsalomon1c63bf62014-07-22 13:09:46 -07002137 uint32_t textureID = texture->getUniqueID();
2138 if (fHWBoundTextureUniqueIDs[unitIdx] != textureID) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002139 this->setTextureUnit(unitIdx);
2140 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, texture->textureID()));
bsalomon1c63bf62014-07-22 13:09:46 -07002141 fHWBoundTextureUniqueIDs[unitIdx] = textureID;
bsalomon@google.com4c883782012-06-04 19:05:11 +00002142 }
2143
bsalomon@google.com4c883782012-06-04 19:05:11 +00002144 ResetTimestamp timestamp;
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002145 const GrGLTexture::TexParams& oldTexParams = texture->getCachedTexParams(&timestamp);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002146 bool setAll = timestamp < this->getResetTimestamp();
2147 GrGLTexture::TexParams newTexParams;
2148
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00002149 static GrGLenum glMinFilterModes[] = {
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +00002150 GR_GL_NEAREST,
2151 GR_GL_LINEAR,
2152 GR_GL_LINEAR_MIPMAP_LINEAR
2153 };
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00002154 static GrGLenum glMagFilterModes[] = {
2155 GR_GL_NEAREST,
2156 GR_GL_LINEAR,
2157 GR_GL_LINEAR
2158 };
commit-bot@chromium.org47442312013-12-19 16:18:01 +00002159 GrTextureParams::FilterMode filterMode = params.filterMode();
bsalomonefd7d452014-10-23 14:17:46 -07002160
2161 if (GrTextureParams::kMipMap_FilterMode == filterMode) {
2162 if (!this->caps()->mipMapSupport() || GrPixelConfigIsCompressed(texture->config())) {
2163 filterMode = GrTextureParams::kBilerp_FilterMode;
2164 }
commit-bot@chromium.org47442312013-12-19 16:18:01 +00002165 }
bsalomonefd7d452014-10-23 14:17:46 -07002166
commit-bot@chromium.org47442312013-12-19 16:18:01 +00002167 newTexParams.fMinFilter = glMinFilterModes[filterMode];
2168 newTexParams.fMagFilter = glMagFilterModes[filterMode];
skia.committer@gmail.comaeefb2a2013-07-27 07:01:06 +00002169
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +00002170 if (GrTextureParams::kMipMap_FilterMode == filterMode &&
bsalomonefd7d452014-10-23 14:17:46 -07002171 texture->texturePriv().mipMapsAreDirty()) {
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +00002172 GL_CALL(GenerateMipmap(GR_GL_TEXTURE_2D));
bsalomonafbf2d62014-09-30 12:18:44 -07002173 texture->texturePriv().dirtyMipMaps(false);
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +00002174 }
bsalomon@google.com4c883782012-06-04 19:05:11 +00002175
bsalomon@google.comb8670992012-07-25 21:27:09 +00002176 newTexParams.fWrapS = tile_to_gl_wrap(params.getTileModeX());
2177 newTexParams.fWrapT = tile_to_gl_wrap(params.getTileModeY());
bsalomon@google.com4c883782012-06-04 19:05:11 +00002178 memcpy(newTexParams.fSwizzleRGBA,
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002179 GrGLShaderBuilder::GetTexParamSwizzle(texture->config(), this->glCaps()),
bsalomon@google.com4c883782012-06-04 19:05:11 +00002180 sizeof(newTexParams.fSwizzleRGBA));
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00002181 if (setAll || newTexParams.fMagFilter != oldTexParams.fMagFilter) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002182 this->setTextureUnit(unitIdx);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002183 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2184 GR_GL_TEXTURE_MAG_FILTER,
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00002185 newTexParams.fMagFilter));
2186 }
2187 if (setAll || newTexParams.fMinFilter != oldTexParams.fMinFilter) {
2188 this->setTextureUnit(unitIdx);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002189 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2190 GR_GL_TEXTURE_MIN_FILTER,
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00002191 newTexParams.fMinFilter));
bsalomon@google.com4c883782012-06-04 19:05:11 +00002192 }
2193 if (setAll || newTexParams.fWrapS != oldTexParams.fWrapS) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002194 this->setTextureUnit(unitIdx);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002195 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2196 GR_GL_TEXTURE_WRAP_S,
2197 newTexParams.fWrapS));
2198 }
2199 if (setAll || newTexParams.fWrapT != oldTexParams.fWrapT) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002200 this->setTextureUnit(unitIdx);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002201 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2202 GR_GL_TEXTURE_WRAP_T,
2203 newTexParams.fWrapT));
2204 }
2205 if (this->glCaps().textureSwizzleSupport() &&
2206 (setAll || memcmp(newTexParams.fSwizzleRGBA,
2207 oldTexParams.fSwizzleRGBA,
2208 sizeof(newTexParams.fSwizzleRGBA)))) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002209 this->setTextureUnit(unitIdx);
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +00002210 if (this->glStandard() == kGLES_GrGLStandard) {
commit-bot@chromium.org6364b5e2013-08-20 20:22:52 +00002211 // ES3 added swizzle support but not GL_TEXTURE_SWIZZLE_RGBA.
2212 const GrGLenum* swizzle = newTexParams.fSwizzleRGBA;
2213 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_SWIZZLE_R, swizzle[0]));
2214 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_SWIZZLE_G, swizzle[1]));
2215 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_SWIZZLE_B, swizzle[2]));
2216 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_SWIZZLE_A, swizzle[3]));
2217 } else {
2218 GR_STATIC_ASSERT(sizeof(newTexParams.fSwizzleRGBA[0]) == sizeof(GrGLint));
2219 const GrGLint* swizzle = reinterpret_cast<const GrGLint*>(newTexParams.fSwizzleRGBA);
2220 GL_CALL(TexParameteriv(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_SWIZZLE_RGBA, swizzle));
2221 }
bsalomon@google.com4c883782012-06-04 19:05:11 +00002222 }
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002223 texture->setCachedTexParams(newTexParams, this->getResetTimestamp());
bsalomon@google.com4c883782012-06-04 19:05:11 +00002224}
2225
bsalomon3e791242014-12-17 13:43:13 -08002226void GrGLGpu::flushDither(bool dither) {
2227 if (dither) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002228 if (kYes_TriState != fHWDitherEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002229 GL_CALL(Enable(GR_GL_DITHER));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002230 fHWDitherEnabled = kYes_TriState;
2231 }
2232 } else {
2233 if (kNo_TriState != fHWDitherEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002234 GL_CALL(Disable(GR_GL_DITHER));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002235 fHWDitherEnabled = kNo_TriState;
reed@google.comac10a2d2010-12-22 21:39:39 +00002236 }
2237 }
bsalomon3e791242014-12-17 13:43:13 -08002238}
reed@google.comac10a2d2010-12-22 21:39:39 +00002239
egdaniel080e6732014-12-22 07:35:52 -08002240void GrGLGpu::flushColorWrite(bool writeColor) {
2241 if (!writeColor) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002242 if (kNo_TriState != fHWWriteToColor) {
2243 GL_CALL(ColorMask(GR_GL_FALSE, GR_GL_FALSE,
2244 GR_GL_FALSE, GR_GL_FALSE));
2245 fHWWriteToColor = kNo_TriState;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002246 }
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002247 } else {
2248 if (kYes_TriState != fHWWriteToColor) {
2249 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
2250 fHWWriteToColor = kYes_TriState;
2251 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00002252 }
bsalomon3e791242014-12-17 13:43:13 -08002253}
bsalomon@google.comd302f142011-03-03 13:54:13 +00002254
egdaniel8dd688b2015-01-22 10:16:09 -08002255void GrGLGpu::flushDrawFace(GrPipelineBuilder::DrawFace face) {
bsalomon3e791242014-12-17 13:43:13 -08002256 if (fHWDrawFace != face) {
2257 switch (face) {
egdaniel8dd688b2015-01-22 10:16:09 -08002258 case GrPipelineBuilder::kCCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002259 GL_CALL(Enable(GR_GL_CULL_FACE));
2260 GL_CALL(CullFace(GR_GL_BACK));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002261 break;
egdaniel8dd688b2015-01-22 10:16:09 -08002262 case GrPipelineBuilder::kCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002263 GL_CALL(Enable(GR_GL_CULL_FACE));
2264 GL_CALL(CullFace(GR_GL_FRONT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002265 break;
egdaniel8dd688b2015-01-22 10:16:09 -08002266 case GrPipelineBuilder::kBoth_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002267 GL_CALL(Disable(GR_GL_CULL_FACE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002268 break;
2269 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +00002270 SkFAIL("Unknown draw face.");
bsalomon@google.comd302f142011-03-03 13:54:13 +00002271 }
bsalomon3e791242014-12-17 13:43:13 -08002272 fHWDrawFace = face;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002273 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002274}
2275
bsalomon861e1032014-12-16 07:33:49 -08002276bool GrGLGpu::configToGLFormats(GrPixelConfig config,
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002277 bool getSizedInternalFormat,
2278 GrGLenum* internalFormat,
2279 GrGLenum* externalFormat,
2280 GrGLenum* externalType) {
2281 GrGLenum dontCare;
2282 if (NULL == internalFormat) {
2283 internalFormat = &dontCare;
2284 }
2285 if (NULL == externalFormat) {
2286 externalFormat = &dontCare;
2287 }
2288 if (NULL == externalType) {
2289 externalType = &dontCare;
2290 }
2291
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +00002292 if(!this->glCaps().isConfigTexturable(config)) {
2293 return false;
2294 }
2295
reed@google.comac10a2d2010-12-22 21:39:39 +00002296 switch (config) {
bsalomon@google.com0342a852012-08-20 19:22:38 +00002297 case kRGBA_8888_GrPixelConfig:
bsalomon@google.comc4364992011-11-07 15:54:49 +00002298 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002299 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002300 if (getSizedInternalFormat) {
2301 *internalFormat = GR_GL_RGBA8;
2302 } else {
2303 *internalFormat = GR_GL_RGBA;
2304 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002305 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.comc4364992011-11-07 15:54:49 +00002306 break;
bsalomon@google.com0342a852012-08-20 19:22:38 +00002307 case kBGRA_8888_GrPixelConfig:
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002308 if (this->glCaps().bgraIsInternalFormat()) {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002309 if (getSizedInternalFormat) {
2310 *internalFormat = GR_GL_BGRA8;
2311 } else {
2312 *internalFormat = GR_GL_BGRA;
2313 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002314 } else {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002315 if (getSizedInternalFormat) {
2316 *internalFormat = GR_GL_RGBA8;
2317 } else {
2318 *internalFormat = GR_GL_RGBA;
2319 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002320 }
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002321 *externalFormat = GR_GL_BGRA;
bsalomon@google.com6f379512011-11-16 20:36:03 +00002322 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002323 break;
jvanverthfa1e8a72014-12-22 08:31:49 -08002324 case kSRGBA_8888_GrPixelConfig:
2325 *internalFormat = GR_GL_SRGB_ALPHA;
2326 *externalFormat = GR_GL_SRGB_ALPHA;
2327 if (getSizedInternalFormat) {
2328 *internalFormat = GR_GL_SRGB8_ALPHA8;
2329 } else {
2330 *internalFormat = GR_GL_SRGB_ALPHA;
2331 }
2332 *externalType = GR_GL_UNSIGNED_BYTE;
2333 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002334 case kRGB_565_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002335 *internalFormat = GR_GL_RGB;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002336 *externalFormat = GR_GL_RGB;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002337 if (getSizedInternalFormat) {
jvanverth3f801cb2014-12-16 09:49:38 -08002338 if (!this->glCaps().ES2CompatibilitySupport()) {
2339 *internalFormat = GR_GL_RGB5;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002340 } else {
2341 *internalFormat = GR_GL_RGB565;
2342 }
2343 } else {
2344 *internalFormat = GR_GL_RGB;
2345 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002346 *externalType = GR_GL_UNSIGNED_SHORT_5_6_5;
reed@google.comac10a2d2010-12-22 21:39:39 +00002347 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002348 case kRGBA_4444_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002349 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002350 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002351 if (getSizedInternalFormat) {
2352 *internalFormat = GR_GL_RGBA4;
2353 } else {
2354 *internalFormat = GR_GL_RGBA;
2355 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002356 *externalType = GR_GL_UNSIGNED_SHORT_4_4_4_4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002357 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002358 case kIndex_8_GrPixelConfig:
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +00002359 // no sized/unsized internal format distinction here
2360 *internalFormat = GR_GL_PALETTE8_RGBA8;
reed@google.comac10a2d2010-12-22 21:39:39 +00002361 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002362 case kAlpha_8_GrPixelConfig:
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002363 if (this->glCaps().textureRedSupport()) {
2364 *internalFormat = GR_GL_RED;
2365 *externalFormat = GR_GL_RED;
2366 if (getSizedInternalFormat) {
2367 *internalFormat = GR_GL_R8;
2368 } else {
2369 *internalFormat = GR_GL_RED;
2370 }
2371 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002372 } else {
2373 *internalFormat = GR_GL_ALPHA;
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002374 *externalFormat = GR_GL_ALPHA;
2375 if (getSizedInternalFormat) {
2376 *internalFormat = GR_GL_ALPHA8;
2377 } else {
2378 *internalFormat = GR_GL_ALPHA;
2379 }
2380 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002381 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002382 break;
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +00002383 case kETC1_GrPixelConfig:
egdanield588c012015-03-27 12:22:10 -07002384 *internalFormat = GR_GL_COMPRESSED_ETC1_RGB8;
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +00002385 break;
2386 case kLATC_GrPixelConfig:
2387 switch(this->glCaps().latcAlias()) {
2388 case GrGLCaps::kLATC_LATCAlias:
2389 *internalFormat = GR_GL_COMPRESSED_LUMINANCE_LATC1;
2390 break;
2391 case GrGLCaps::kRGTC_LATCAlias:
2392 *internalFormat = GR_GL_COMPRESSED_RED_RGTC1;
2393 break;
2394 case GrGLCaps::k3DC_LATCAlias:
2395 *internalFormat = GR_GL_COMPRESSED_3DC_X;
2396 break;
2397 }
2398 break;
krajcevski238b4562014-06-30 09:09:22 -07002399 case kR11_EAC_GrPixelConfig:
egdanield588c012015-03-27 12:22:10 -07002400 *internalFormat = GR_GL_COMPRESSED_R11_EAC;
krajcevski238b4562014-06-30 09:09:22 -07002401 break;
krajcevski7ef21622014-07-16 15:21:13 -07002402
2403 case kASTC_12x12_GrPixelConfig:
egdanield588c012015-03-27 12:22:10 -07002404 *internalFormat = GR_GL_COMPRESSED_RGBA_ASTC_12x12_KHR;
krajcevski7ef21622014-07-16 15:21:13 -07002405 break;
2406
joshualittee5da552014-07-16 13:32:56 -07002407 case kRGBA_float_GrPixelConfig:
2408 *internalFormat = GR_GL_RGBA32F;
2409 *externalFormat = GR_GL_RGBA;
2410 *externalType = GR_GL_FLOAT;
2411 break;
krajcevski7ef21622014-07-16 15:21:13 -07002412
jvanverth28f9c602014-12-05 13:06:35 -08002413 case kAlpha_half_GrPixelConfig:
jvanverth1334c212014-12-18 05:44:55 -08002414 if (this->glCaps().textureRedSupport()) {
2415 if (getSizedInternalFormat) {
2416 *internalFormat = GR_GL_R16F;
2417 } else {
2418 *internalFormat = GR_GL_RED;
2419 }
jvanverth28f9c602014-12-05 13:06:35 -08002420 *externalFormat = GR_GL_RED;
jvanverth1334c212014-12-18 05:44:55 -08002421 } else {
2422 if (getSizedInternalFormat) {
2423 *internalFormat = GR_GL_ALPHA16F;
2424 } else {
2425 *internalFormat = GR_GL_ALPHA;
2426 }
2427 *externalFormat = GR_GL_ALPHA;
2428 }
2429 if (kGL_GrGLStandard == this->glStandard() || this->glVersion() >= GR_GL_VER(3, 0)) {
jvanvertha60b2ea2014-12-12 05:58:06 -08002430 *externalType = GR_GL_HALF_FLOAT;
jvanverth28f9c602014-12-05 13:06:35 -08002431 } else {
jvanverth1334c212014-12-18 05:44:55 -08002432 *externalType = GR_GL_HALF_FLOAT_OES;
jvanverth28f9c602014-12-05 13:06:35 -08002433 }
2434 break;
2435
reed@google.comac10a2d2010-12-22 21:39:39 +00002436 default:
2437 return false;
2438 }
2439 return true;
2440}
2441
bsalomon861e1032014-12-16 07:33:49 -08002442void GrGLGpu::setTextureUnit(int unit) {
bsalomon1c63bf62014-07-22 13:09:46 -07002443 SkASSERT(unit >= 0 && unit < fHWBoundTextureUniqueIDs.count());
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002444 if (unit != fHWActiveTextureUnitIdx) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002445 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com49209392012-06-05 15:13:46 +00002446 fHWActiveTextureUnitIdx = unit;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002447 }
2448}
bsalomon@google.com316f99232011-01-13 21:28:12 +00002449
bsalomon861e1032014-12-16 07:33:49 -08002450void GrGLGpu::setScratchTextureUnit() {
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002451 // Bind the last texture unit since it is the least likely to be used by GrGLProgram.
bsalomon1c63bf62014-07-22 13:09:46 -07002452 int lastUnitIdx = fHWBoundTextureUniqueIDs.count() - 1;
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002453 if (lastUnitIdx != fHWActiveTextureUnitIdx) {
2454 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + lastUnitIdx));
2455 fHWActiveTextureUnitIdx = lastUnitIdx;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002456 }
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002457 // clear out the this field so that if a program does use this unit it will rebind the correct
2458 // texture.
bsalomon1c63bf62014-07-22 13:09:46 -07002459 fHWBoundTextureUniqueIDs[lastUnitIdx] = SK_InvalidUniqueID;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002460}
2461
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002462namespace {
2463// Determines whether glBlitFramebuffer could be used between src and dst.
bsalomon@google.comeb851172013-04-15 13:51:00 +00002464inline bool can_blit_framebuffer(const GrSurface* dst,
2465 const GrSurface* src,
egdaniel0f5f9672015-02-03 11:10:51 -08002466 const GrGLGpu* gpu) {
commit-bot@chromium.org6b7938f2013-10-15 14:18:16 +00002467 if (gpu->glCaps().isConfigRenderable(dst->config(), dst->desc().fSampleCnt > 0) &&
2468 gpu->glCaps().isConfigRenderable(src->config(), src->desc().fSampleCnt > 0) &&
bsalomon@google.com347c3822013-05-01 20:10:01 +00002469 gpu->glCaps().usesMSAARenderBuffers()) {
commit-bot@chromium.orga8e5a062013-09-05 23:44:09 +00002470 // ES3 doesn't allow framebuffer blits when the src has MSAA and the configs don't match
2471 // or the rects are not the same (not just the same size but have the same edges).
2472 if (GrGLCaps::kES_3_0_MSFBOType == gpu->glCaps().msFBOType() &&
2473 (src->desc().fSampleCnt > 0 || src->config() != dst->config())) {
2474 return false;
2475 }
bsalomon@google.comeb851172013-04-15 13:51:00 +00002476 return true;
2477 } else {
2478 return false;
2479 }
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002480}
bsalomon@google.comeb851172013-04-15 13:51:00 +00002481
2482inline bool can_copy_texsubimage(const GrSurface* dst,
2483 const GrSurface* src,
egdaniel0f5f9672015-02-03 11:10:51 -08002484 const GrGLGpu* gpu) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002485 // Table 3.9 of the ES2 spec indicates the supported formats with CopyTexSubImage
2486 // and BGRA isn't in the spec. There doesn't appear to be any extension that adds it. Perhaps
2487 // many drivers would allow it to work, but ANGLE does not.
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +00002488 if (kGLES_GrGLStandard == gpu->glStandard() && gpu->glCaps().bgraIsInternalFormat() &&
bsalomon@google.comeb851172013-04-15 13:51:00 +00002489 (kBGRA_8888_GrPixelConfig == dst->config() || kBGRA_8888_GrPixelConfig == src->config())) {
2490 return false;
2491 }
2492 const GrGLRenderTarget* dstRT = static_cast<const GrGLRenderTarget*>(dst->asRenderTarget());
2493 // If dst is multisampled (and uses an extension where there is a separate MSAA renderbuffer)
2494 // then we don't want to copy to the texture but to the MSAA buffer.
egdanield803f272015-03-18 13:01:52 -07002495 if (dstRT && dstRT->renderFBOID() != dstRT->textureFBOID()) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002496 return false;
2497 }
bsalomon@google.coma2719852013-04-17 14:25:27 +00002498 const GrGLRenderTarget* srcRT = static_cast<const GrGLRenderTarget*>(src->asRenderTarget());
2499 // If the src is multisampled (and uses an extension where there is a separate MSAA
2500 // renderbuffer) then it is an invalid operation to call CopyTexSubImage
egdanield803f272015-03-18 13:01:52 -07002501 if (srcRT && srcRT->renderFBOID() != srcRT->textureFBOID()) {
bsalomon@google.coma2719852013-04-17 14:25:27 +00002502 return false;
2503 }
commit-bot@chromium.org6b7938f2013-10-15 14:18:16 +00002504 if (gpu->glCaps().isConfigRenderable(src->config(), src->desc().fSampleCnt > 0) &&
bsalomon49f085d2014-09-05 13:34:00 -07002505 dst->asTexture() &&
commit-bot@chromium.org6b7938f2013-10-15 14:18:16 +00002506 dst->origin() == src->origin() &&
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +00002507 !GrPixelConfigIsCompressed(src->config())) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002508 return true;
2509 } else {
2510 return false;
2511 }
2512}
2513
robertphillips754f4e92014-09-18 13:52:08 -07002514}
2515
bsalomon@google.comeb851172013-04-15 13:51:00 +00002516// If a temporary FBO was created, its non-zero ID is returned. The viewport that the copy rect is
2517// relative to is output.
egdanield803f272015-03-18 13:01:52 -07002518GrGLuint GrGLGpu::bindSurfaceAsFBO(GrSurface* surface, GrGLenum fboTarget, GrGLIRect* viewport,
2519 TempFBOTarget tempFBOTarget) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002520 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(surface->asRenderTarget());
bsalomon@google.comeb851172013-04-15 13:51:00 +00002521 if (NULL == rt) {
bsalomon49f085d2014-09-05 13:34:00 -07002522 SkASSERT(surface->asTexture());
bsalomon@google.comeb851172013-04-15 13:51:00 +00002523 GrGLuint texID = static_cast<GrGLTexture*>(surface->asTexture())->textureID();
egdanield803f272015-03-18 13:01:52 -07002524 GrGLuint* tempFBOID;
2525 tempFBOID = kSrc_TempFBOTarget == tempFBOTarget ? &fTempSrcFBOID : &fTempDstFBOID;
egdaniel0f5f9672015-02-03 11:10:51 -08002526
egdanield803f272015-03-18 13:01:52 -07002527 if (0 == *tempFBOID) {
2528 GR_GL_CALL(this->glInterface(), GenFramebuffers(1, tempFBOID));
egdaniel0f5f9672015-02-03 11:10:51 -08002529 }
2530
egdanield803f272015-03-18 13:01:52 -07002531 fStats.incRenderTargetBinds();
2532 GR_GL_CALL(this->glInterface(), BindFramebuffer(fboTarget, *tempFBOID));
2533 GR_GL_CALL(this->glInterface(), FramebufferTexture2D(fboTarget,
robertphillips754f4e92014-09-18 13:52:08 -07002534 GR_GL_COLOR_ATTACHMENT0,
2535 GR_GL_TEXTURE_2D,
2536 texID,
2537 0));
bsalomon@google.comeb851172013-04-15 13:51:00 +00002538 viewport->fLeft = 0;
2539 viewport->fBottom = 0;
2540 viewport->fWidth = surface->width();
2541 viewport->fHeight = surface->height();
egdanield803f272015-03-18 13:01:52 -07002542 return *tempFBOID;
bsalomon@google.comeb851172013-04-15 13:51:00 +00002543 } else {
egdanield803f272015-03-18 13:01:52 -07002544 GrGLuint tempFBOID = 0;
2545 fStats.incRenderTargetBinds();
2546 GR_GL_CALL(this->glInterface(), BindFramebuffer(fboTarget, rt->renderFBOID()));
bsalomon@google.comeb851172013-04-15 13:51:00 +00002547 *viewport = rt->getViewport();
egdanield803f272015-03-18 13:01:52 -07002548 return tempFBOID;
bsalomon@google.comeb851172013-04-15 13:51:00 +00002549 }
egdaniel0f5f9672015-02-03 11:10:51 -08002550}
2551
egdanield803f272015-03-18 13:01:52 -07002552void GrGLGpu::unbindTextureFromFBO(GrGLenum fboTarget) {
2553 GR_GL_CALL(this->glInterface(), FramebufferTexture2D(fboTarget,
egdaniel0f5f9672015-02-03 11:10:51 -08002554 GR_GL_COLOR_ATTACHMENT0,
2555 GR_GL_TEXTURE_2D,
2556 0,
2557 0));
bsalomon@google.comeb851172013-04-15 13:51:00 +00002558}
2559
bsalomon861e1032014-12-16 07:33:49 -08002560bool GrGLGpu::initCopySurfaceDstDesc(const GrSurface* src, GrSurfaceDesc* desc) {
bsalomonf90a02b2014-11-26 12:28:00 -08002561 // In here we look for opportunities to use CopyTexSubImage, or fbo blit. If neither are
2562 // possible and we return false to fallback to creating a render target dst for render-to-
2563 // texture. This code prefers CopyTexSubImage to fbo blit and avoids triggering temporary fbo
2564 // creation. It isn't clear that avoiding temporary fbo creation is actually optimal.
2565
bsalomon@google.comeb851172013-04-15 13:51:00 +00002566 // Check for format issues with glCopyTexSubImage2D
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +00002567 if (kGLES_GrGLStandard == this->glStandard() && this->glCaps().bgraIsInternalFormat() &&
bsalomon@google.comeb851172013-04-15 13:51:00 +00002568 kBGRA_8888_GrPixelConfig == src->config()) {
bsalomonf90a02b2014-11-26 12:28:00 -08002569 // glCopyTexSubImage2D doesn't work with this config. If the bgra can be used with fbo blit
2570 // then we set up for that, otherwise fail.
2571 if (this->caps()->isConfigRenderable(kBGRA_8888_GrPixelConfig, false)) {
2572 desc->fOrigin = kDefault_GrSurfaceOrigin;
bsalomon6bc1b5f2015-02-23 09:06:38 -08002573 desc->fFlags = kRenderTarget_GrSurfaceFlag;
bsalomonf90a02b2014-11-26 12:28:00 -08002574 desc->fConfig = kBGRA_8888_GrPixelConfig;
2575 return true;
2576 }
2577 return false;
bsalomon@google.comeb851172013-04-15 13:51:00 +00002578 } else if (NULL == src->asRenderTarget()) {
bsalomonf90a02b2014-11-26 12:28:00 -08002579 // CopyTexSubImage2D or fbo blit would require creating a temp fbo for the src.
2580 return false;
bsalomon@google.coma2719852013-04-17 14:25:27 +00002581 }
2582
2583 const GrGLRenderTarget* srcRT = static_cast<const GrGLRenderTarget*>(src->asRenderTarget());
egdanield803f272015-03-18 13:01:52 -07002584 if (srcRT && srcRT->renderFBOID() != srcRT->textureFBOID()) {
bsalomonf90a02b2014-11-26 12:28:00 -08002585 // It's illegal to call CopyTexSubImage2D on a MSAA renderbuffer. Set up for FBO blit or
2586 // fail.
2587 if (this->caps()->isConfigRenderable(src->config(), false)) {
2588 desc->fOrigin = kDefault_GrSurfaceOrigin;
bsalomon6bc1b5f2015-02-23 09:06:38 -08002589 desc->fFlags = kRenderTarget_GrSurfaceFlag;
bsalomonf90a02b2014-11-26 12:28:00 -08002590 desc->fConfig = src->config();
2591 return true;
2592 }
2593 return false;
bsalomon@google.comeb851172013-04-15 13:51:00 +00002594 }
bsalomonf90a02b2014-11-26 12:28:00 -08002595
2596 // We'll do a CopyTexSubImage. Make the dst a plain old texture.
2597 desc->fConfig = src->config();
2598 desc->fOrigin = src->origin();
2599 desc->fFlags = kNone_GrSurfaceFlags;
2600 return true;
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002601}
2602
bsalomon861e1032014-12-16 07:33:49 -08002603bool GrGLGpu::copySurface(GrSurface* dst,
joshualitta7024152014-11-03 14:16:35 -08002604 GrSurface* src,
2605 const SkIRect& srcRect,
2606 const SkIPoint& dstPoint) {
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002607 bool copied = false;
joshualitta7024152014-11-03 14:16:35 -08002608 if (can_copy_texsubimage(dst, src, this)) {
egdanield803f272015-03-18 13:01:52 -07002609 GrGLuint srcFBO;
bsalomon@google.comeb851172013-04-15 13:51:00 +00002610 GrGLIRect srcVP;
egdanield803f272015-03-18 13:01:52 -07002611 srcFBO = this->bindSurfaceAsFBO(src, GR_GL_FRAMEBUFFER, &srcVP, kSrc_TempFBOTarget);
bsalomon@google.comeb851172013-04-15 13:51:00 +00002612 GrGLTexture* dstTex = static_cast<GrGLTexture*>(dst->asTexture());
bsalomon49f085d2014-09-05 13:34:00 -07002613 SkASSERT(dstTex);
egdanield803f272015-03-18 13:01:52 -07002614 // We modified the bound FBO
2615 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
bsalomon@google.comeb851172013-04-15 13:51:00 +00002616 GrGLIRect srcGLRect;
2617 srcGLRect.setRelativeTo(srcVP,
2618 srcRect.fLeft,
2619 srcRect.fTop,
2620 srcRect.width(),
2621 srcRect.height(),
2622 src->origin());
2623
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002624 this->setScratchTextureUnit();
bsalomon@google.comeb851172013-04-15 13:51:00 +00002625 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, dstTex->textureID()));
2626 GrGLint dstY;
2627 if (kBottomLeft_GrSurfaceOrigin == dst->origin()) {
2628 dstY = dst->height() - (dstPoint.fY + srcGLRect.fHeight);
2629 } else {
2630 dstY = dstPoint.fY;
2631 }
2632 GL_CALL(CopyTexSubImage2D(GR_GL_TEXTURE_2D, 0,
2633 dstPoint.fX, dstY,
2634 srcGLRect.fLeft, srcGLRect.fBottom,
2635 srcGLRect.fWidth, srcGLRect.fHeight));
2636 copied = true;
egdanield803f272015-03-18 13:01:52 -07002637 if (srcFBO) {
2638 this->unbindTextureFromFBO(GR_GL_FRAMEBUFFER);
2639 }
joshualitta7024152014-11-03 14:16:35 -08002640 } else if (can_blit_framebuffer(dst, src, this)) {
egdanield803f272015-03-18 13:01:52 -07002641 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
2642 srcRect.width(), srcRect.height());
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002643 bool selfOverlap = false;
bsalomona2c23232014-11-25 07:41:12 -08002644 if (dst == src) {
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002645 selfOverlap = SkIRect::IntersectsNoEmptyCheck(dstRect, srcRect);
2646 }
2647
2648 if (!selfOverlap) {
egdanield803f272015-03-18 13:01:52 -07002649 GrGLuint dstFBO;
2650 GrGLuint srcFBO;
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002651 GrGLIRect dstVP;
2652 GrGLIRect srcVP;
egdanield803f272015-03-18 13:01:52 -07002653 dstFBO = this->bindSurfaceAsFBO(dst, GR_GL_DRAW_FRAMEBUFFER, &dstVP,
2654 kDst_TempFBOTarget);
2655 srcFBO = this->bindSurfaceAsFBO(src, GR_GL_READ_FRAMEBUFFER, &srcVP,
2656 kSrc_TempFBOTarget);
2657 // We modified the bound FBO
2658 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002659 GrGLIRect srcGLRect;
2660 GrGLIRect dstGLRect;
2661 srcGLRect.setRelativeTo(srcVP,
2662 srcRect.fLeft,
2663 srcRect.fTop,
2664 srcRect.width(),
2665 srcRect.height(),
2666 src->origin());
2667 dstGLRect.setRelativeTo(dstVP,
2668 dstRect.fLeft,
2669 dstRect.fTop,
2670 dstRect.width(),
2671 dstRect.height(),
2672 dst->origin());
2673
derekf8c8f71a2014-09-16 06:24:57 -07002674 // BlitFrameBuffer respects the scissor, so disable it.
joshualitt77b13072014-10-27 14:51:01 -07002675 this->disableScissor();
derekf8c8f71a2014-09-16 06:24:57 -07002676
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002677 GrGLint srcY0;
2678 GrGLint srcY1;
2679 // Does the blit need to y-mirror or not?
2680 if (src->origin() == dst->origin()) {
2681 srcY0 = srcGLRect.fBottom;
2682 srcY1 = srcGLRect.fBottom + srcGLRect.fHeight;
2683 } else {
2684 srcY0 = srcGLRect.fBottom + srcGLRect.fHeight;
2685 srcY1 = srcGLRect.fBottom;
2686 }
2687 GL_CALL(BlitFramebuffer(srcGLRect.fLeft,
2688 srcY0,
2689 srcGLRect.fLeft + srcGLRect.fWidth,
2690 srcY1,
2691 dstGLRect.fLeft,
2692 dstGLRect.fBottom,
2693 dstGLRect.fLeft + dstGLRect.fWidth,
2694 dstGLRect.fBottom + dstGLRect.fHeight,
2695 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
egdanield803f272015-03-18 13:01:52 -07002696 if (dstFBO) {
2697 this->unbindTextureFromFBO(GR_GL_DRAW_FRAMEBUFFER);
2698 }
2699 if (srcFBO) {
2700 this->unbindTextureFromFBO(GR_GL_READ_FRAMEBUFFER);
2701 }
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002702 copied = true;
2703 }
2704 }
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002705 return copied;
2706}
2707
bsalomon861e1032014-12-16 07:33:49 -08002708bool GrGLGpu::canCopySurface(const GrSurface* dst,
joshualitt9853cce2014-11-17 14:22:48 -08002709 const GrSurface* src,
joshualitta7024152014-11-03 14:16:35 -08002710 const SkIRect& srcRect,
2711 const SkIPoint& dstPoint) {
egdaniel0f5f9672015-02-03 11:10:51 -08002712 // This mirrors the logic in onCopySurface.
2713 if (can_copy_texsubimage(dst, src, this)) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002714 return true;
2715 }
egdaniel0f5f9672015-02-03 11:10:51 -08002716 if (can_blit_framebuffer(dst, src, this)) {
bsalomona2c23232014-11-25 07:41:12 -08002717 if (dst == src) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002718 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
2719 srcRect.width(), srcRect.height());
2720 if(!SkIRect::IntersectsNoEmptyCheck(dstRect, srcRect)) {
2721 return true;
2722 }
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002723 } else {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002724 return true;
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002725 }
2726 }
joshualitta7024152014-11-03 14:16:35 -08002727 return false;
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002728}
2729
bsalomon861e1032014-12-16 07:33:49 -08002730void GrGLGpu::didAddGpuTraceMarker() {
commit-bot@chromium.orga3baf3b2014-02-21 18:45:30 +00002731 if (this->caps()->gpuTracingSupport()) {
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +00002732 const GrTraceMarkerSet& markerArray = this->getActiveTraceMarkers();
egdanield78a1682014-07-09 10:41:26 -07002733 SkString markerString = markerArray.toStringLast();
egdanielbdad9c32015-03-05 12:19:17 -08002734#if GR_FORCE_GPU_TRACE_DEBUGGING
2735 SkDebugf("%s\n", markerString.c_str());
2736#else
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +00002737 GL_CALL(PushGroupMarker(0, markerString.c_str()));
egdanielbdad9c32015-03-05 12:19:17 -08002738#endif
commit-bot@chromium.orga3baf3b2014-02-21 18:45:30 +00002739 }
2740}
2741
bsalomon861e1032014-12-16 07:33:49 -08002742void GrGLGpu::didRemoveGpuTraceMarker() {
commit-bot@chromium.orga3baf3b2014-02-21 18:45:30 +00002743 if (this->caps()->gpuTracingSupport()) {
egdanielbdad9c32015-03-05 12:19:17 -08002744#if GR_FORCE_GPU_TRACE_DEBUGGING
2745 SkDebugf("Pop trace marker.\n");
2746#else
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +00002747 GL_CALL(PopGroupMarker());
egdanielbdad9c32015-03-05 12:19:17 -08002748#endif
commit-bot@chromium.orga3baf3b2014-02-21 18:45:30 +00002749 }
2750}
joshualitt79f8fae2014-10-28 17:59:26 -07002751
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00002752///////////////////////////////////////////////////////////////////////////////
2753
bsalomon861e1032014-12-16 07:33:49 -08002754GrGLAttribArrayState* GrGLGpu::HWGeometryState::bindArrayAndBuffersToDraw(
2755 GrGLGpu* gpu,
bsalomon@google.com6918d482013-03-07 19:09:11 +00002756 const GrGLVertexBuffer* vbuffer,
2757 const GrGLIndexBuffer* ibuffer) {
bsalomon49f085d2014-09-05 13:34:00 -07002758 SkASSERT(vbuffer);
robertphillips@google.com4f65a272013-03-26 19:40:46 +00002759 GrGLAttribArrayState* attribState;
2760
bsalomon@google.com6918d482013-03-07 19:09:11 +00002761 // We use a vertex array if we're on a core profile and the verts are in a VBO.
2762 if (gpu->glCaps().isCoreProfile() && !vbuffer->isCPUBacked()) {
commit-bot@chromium.org089a7802014-05-02 21:38:22 +00002763 if (NULL == fVBOVertexArray || fVBOVertexArray->wasDestroyed()) {
bsalomon@google.com6918d482013-03-07 19:09:11 +00002764 SkSafeUnref(fVBOVertexArray);
2765 GrGLuint arrayID;
2766 GR_GL_CALL(gpu->glInterface(), GenVertexArrays(1, &arrayID));
2767 int attrCount = gpu->glCaps().maxVertexAttributes();
2768 fVBOVertexArray = SkNEW_ARGS(GrGLVertexArray, (gpu, arrayID, attrCount));
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00002769 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00002770 attribState = fVBOVertexArray->bindWithIndexBuffer(ibuffer);
2771 } else {
bsalomon49f085d2014-09-05 13:34:00 -07002772 if (ibuffer) {
bsalomon@google.com6918d482013-03-07 19:09:11 +00002773 this->setIndexBufferIDOnDefaultVertexArray(gpu, ibuffer->bufferID());
2774 } else {
2775 this->setVertexArrayID(gpu, 0);
2776 }
2777 int attrCount = gpu->glCaps().maxVertexAttributes();
2778 if (fDefaultVertexArrayAttribState.count() != attrCount) {
2779 fDefaultVertexArrayAttribState.resize(attrCount);
2780 }
2781 attribState = &fDefaultVertexArrayAttribState;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00002782 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00002783 return attribState;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002784}