blob: 0f7a0c152bd8dddc6ce6f548a65b409d6fe4053d [file] [log] [blame]
reed@google.comac10a2d2010-12-22 21:39:39 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
reed@google.comac10a2d2010-12-22 21:39:39 +00006 */
7
epoger@google.comec3ed6a2011-07-28 14:26:00 +00008
jvanverth39edf762014-12-22 11:44:19 -08009#include "GrGLGpu.h"
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000010#include "GrGLStencilBuffer.h"
bsalomon37dd3312014-11-03 08:47:23 -080011#include "GrGLTextureRenderTarget.h"
bsalomon3582d3e2015-02-13 14:20:05 -080012#include "GrGpuResourcePriv.h"
egdaniel8dd688b2015-01-22 10:16:09 -080013#include "GrPipeline.h"
bsalomon6bc1b5f2015-02-23 09:06:38 -080014#include "GrRenderTargetPriv.h"
bsalomonafbf2d62014-09-30 12:18:44 -070015#include "GrSurfacePriv.h"
bsalomon@google.coma3201942012-06-21 19:58:20 +000016#include "GrTemplates.h"
bsalomonafbf2d62014-09-30 12:18:44 -070017#include "GrTexturePriv.h"
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +000018#include "GrTypes.h"
commit-bot@chromium.org32184d82013-10-09 15:14:18 +000019#include "SkStrokeRec.h"
bsalomon@google.com3582bf92011-06-30 21:32:31 +000020#include "SkTemplates.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000021
bsalomon@google.com0b77d682011-08-19 13:28:54 +000022#define GL_CALL(X) GR_GL_CALL(this->glInterface(), X)
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +000023#define GL_CALL_RET(RET, X) GR_GL_CALL_RET(this->glInterface(), RET, X)
bsalomon@google.com0b77d682011-08-19 13:28:54 +000024
reed@google.comac10a2d2010-12-22 21:39:39 +000025#define SKIP_CACHE_CHECK true
26
bsalomon@google.com4f3c2532012-01-19 16:16:52 +000027#if GR_GL_CHECK_ALLOC_WITH_GET_ERROR
28 #define CLEAR_ERROR_BEFORE_ALLOC(iface) GrGLClearErr(iface)
29 #define GL_ALLOC_CALL(iface, call) GR_GL_CALL_NOERRCHECK(iface, call)
30 #define CHECK_ALLOC_ERROR(iface) GR_GL_GET_ERROR(iface)
rmistry@google.comfbfcd562012-08-23 18:09:54 +000031#else
bsalomon@google.com4f3c2532012-01-19 16:16:52 +000032 #define CLEAR_ERROR_BEFORE_ALLOC(iface)
33 #define GL_ALLOC_CALL(iface, call) GR_GL_CALL(iface, call)
34 #define CHECK_ALLOC_ERROR(iface) GR_GL_NO_ERROR
35#endif
36
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +000037
38///////////////////////////////////////////////////////////////////////////////
39
cdaltonb85a0aa2014-07-21 15:32:44 -070040
twiz@google.com0f31ca72011-03-18 17:38:11 +000041static const GrGLenum gXfermodeCoeff2Blend[] = {
42 GR_GL_ZERO,
43 GR_GL_ONE,
44 GR_GL_SRC_COLOR,
45 GR_GL_ONE_MINUS_SRC_COLOR,
46 GR_GL_DST_COLOR,
47 GR_GL_ONE_MINUS_DST_COLOR,
48 GR_GL_SRC_ALPHA,
49 GR_GL_ONE_MINUS_SRC_ALPHA,
50 GR_GL_DST_ALPHA,
51 GR_GL_ONE_MINUS_DST_ALPHA,
52 GR_GL_CONSTANT_COLOR,
53 GR_GL_ONE_MINUS_CONSTANT_COLOR,
54 GR_GL_CONSTANT_ALPHA,
55 GR_GL_ONE_MINUS_CONSTANT_ALPHA,
bsalomon@google.com271cffc2011-05-20 14:13:56 +000056
57 // extended blend coeffs
58 GR_GL_SRC1_COLOR,
59 GR_GL_ONE_MINUS_SRC1_COLOR,
60 GR_GL_SRC1_ALPHA,
61 GR_GL_ONE_MINUS_SRC1_ALPHA,
reed@google.comac10a2d2010-12-22 21:39:39 +000062};
63
bsalomon861e1032014-12-16 07:33:49 -080064bool GrGLGpu::BlendCoeffReferencesConstant(GrBlendCoeff coeff) {
bsalomon@google.com080773c2011-03-15 19:09:25 +000065 static const bool gCoeffReferencesBlendConst[] = {
66 false,
67 false,
68 false,
69 false,
70 false,
71 false,
72 false,
73 false,
74 false,
75 false,
76 true,
77 true,
78 true,
79 true,
bsalomon@google.com271cffc2011-05-20 14:13:56 +000080
81 // extended blend coeffs
82 false,
83 false,
84 false,
85 false,
bsalomon@google.com080773c2011-03-15 19:09:25 +000086 };
87 return gCoeffReferencesBlendConst[coeff];
bsalomon@google.com47059542012-06-06 20:51:20 +000088 GR_STATIC_ASSERT(kTotalGrBlendCoeffCount ==
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +000089 SK_ARRAY_COUNT(gCoeffReferencesBlendConst));
bsalomon@google.com271cffc2011-05-20 14:13:56 +000090
bsalomon@google.com47059542012-06-06 20:51:20 +000091 GR_STATIC_ASSERT(0 == kZero_GrBlendCoeff);
92 GR_STATIC_ASSERT(1 == kOne_GrBlendCoeff);
93 GR_STATIC_ASSERT(2 == kSC_GrBlendCoeff);
94 GR_STATIC_ASSERT(3 == kISC_GrBlendCoeff);
95 GR_STATIC_ASSERT(4 == kDC_GrBlendCoeff);
96 GR_STATIC_ASSERT(5 == kIDC_GrBlendCoeff);
97 GR_STATIC_ASSERT(6 == kSA_GrBlendCoeff);
98 GR_STATIC_ASSERT(7 == kISA_GrBlendCoeff);
99 GR_STATIC_ASSERT(8 == kDA_GrBlendCoeff);
100 GR_STATIC_ASSERT(9 == kIDA_GrBlendCoeff);
101 GR_STATIC_ASSERT(10 == kConstC_GrBlendCoeff);
102 GR_STATIC_ASSERT(11 == kIConstC_GrBlendCoeff);
103 GR_STATIC_ASSERT(12 == kConstA_GrBlendCoeff);
104 GR_STATIC_ASSERT(13 == kIConstA_GrBlendCoeff);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000105
bsalomon@google.com47059542012-06-06 20:51:20 +0000106 GR_STATIC_ASSERT(14 == kS2C_GrBlendCoeff);
107 GR_STATIC_ASSERT(15 == kIS2C_GrBlendCoeff);
108 GR_STATIC_ASSERT(16 == kS2A_GrBlendCoeff);
109 GR_STATIC_ASSERT(17 == kIS2A_GrBlendCoeff);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000110
111 // assertion for gXfermodeCoeff2Blend have to be in GrGpu scope
bsalomon@google.com47059542012-06-06 20:51:20 +0000112 GR_STATIC_ASSERT(kTotalGrBlendCoeffCount ==
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000113 SK_ARRAY_COUNT(gXfermodeCoeff2Blend));
bsalomon@google.com080773c2011-03-15 19:09:25 +0000114}
115
reed@google.comac10a2d2010-12-22 21:39:39 +0000116///////////////////////////////////////////////////////////////////////////////
117
rileya@google.come38160c2012-07-03 18:03:04 +0000118static bool gPrintStartupSpew;
bsalomon@google.com42ab7ea2011-01-19 17:19:40 +0000119
bsalomon861e1032014-12-16 07:33:49 -0800120GrGLGpu::GrGLGpu(const GrGLContext& ctx, GrContext* context)
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000121 : GrGpu(context)
robertphillips@google.com6177e692013-02-28 20:16:25 +0000122 , fGLContext(ctx) {
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000123
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000124 SkASSERT(ctx.isInitialized());
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +0000125 fCaps.reset(SkRef(ctx.caps()));
bsalomon@google.combcce8922013-03-25 15:38:39 +0000126
bsalomon1c63bf62014-07-22 13:09:46 -0700127 fHWBoundTextureUniqueIDs.reset(this->glCaps().maxFragmentTextureUnits());
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +0000128
robertphillips@google.com6177e692013-02-28 20:16:25 +0000129 GrGLClearErr(fGLContext.interface());
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000130 if (gPrintStartupSpew) {
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000131 const GrGLubyte* vendor;
132 const GrGLubyte* renderer;
133 const GrGLubyte* version;
134 GL_CALL_RET(vendor, GetString(GR_GL_VENDOR));
135 GL_CALL_RET(renderer, GetString(GR_GL_RENDERER));
136 GL_CALL_RET(version, GetString(GR_GL_VERSION));
bsalomon861e1032014-12-16 07:33:49 -0800137 SkDebugf("------------------------- create GrGLGpu %p --------------\n",
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000138 this);
tfarina38406c82014-10-31 07:11:12 -0700139 SkDebugf("------ VENDOR %s\n", vendor);
140 SkDebugf("------ RENDERER %s\n", renderer);
141 SkDebugf("------ VERSION %s\n", version);
142 SkDebugf("------ EXTENSIONS\n");
bsalomone904c092014-07-17 10:50:59 -0700143 ctx.extensions().print();
tfarina38406c82014-10-31 07:11:12 -0700144 SkDebugf("\n");
kkinnunen297aaf92015-02-19 06:32:12 -0800145 SkDebugf("%s", this->glCaps().dump().c_str());
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000146 }
147
commit-bot@chromium.org9188a152013-09-05 18:28:24 +0000148 fProgramCache = SkNEW_ARGS(ProgramCache, (this));
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000149
joshualitt2dd1ae02014-12-03 06:24:10 -0800150 SkASSERT(this->glCaps().maxVertexAttributes() >= GrGeometryProcessor::kMaxVertexAttribs);
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000151
bsalomon@google.comfe676522011-06-17 18:12:21 +0000152 fLastSuccessfulStencilFmtIdx = 0;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000153 fHWProgramID = 0;
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
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000364namespace {
365
366GrSurfaceOrigin resolve_origin(GrSurfaceOrigin origin, bool renderTarget) {
367 // By default, GrRenderTargets are GL's normal orientation so that they
368 // can be drawn to by the outside world without the client having
369 // to render upside down.
370 if (kDefault_GrSurfaceOrigin == origin) {
371 return renderTarget ? kBottomLeft_GrSurfaceOrigin : kTopLeft_GrSurfaceOrigin;
372 } else {
373 return origin;
374 }
375}
376
377}
378
bsalomon861e1032014-12-16 07:33:49 -0800379GrTexture* GrGLGpu::onWrapBackendTexture(const GrBackendTextureDesc& desc) {
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000380 if (!this->configToGLFormats(desc.fConfig, false, NULL, NULL, NULL)) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000381 return NULL;
382 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000383
robertphillips@google.comb72e5d32012-10-30 15:18:10 +0000384 if (0 == desc.fTextureHandle) {
385 return NULL;
386 }
387
bsalomon@google.combcce8922013-03-25 15:38:39 +0000388 int maxSize = this->caps()->maxTextureSize();
robertphillips@google.comb72e5d32012-10-30 15:18:10 +0000389 if (desc.fWidth > maxSize || desc.fHeight > maxSize) {
390 return NULL;
391 }
392
bsalomonb15b4c12014-10-29 12:41:57 -0700393 GrGLTexture::IDDesc idDesc;
394 GrSurfaceDesc surfDesc;
395
396 idDesc.fTextureID = static_cast<GrGLuint>(desc.fTextureHandle);
bsalomon5236cf42015-01-14 10:42:08 -0800397 idDesc.fLifeCycle = GrGpuResource::kWrapped_LifeCycle;
bsalomonb15b4c12014-10-29 12:41:57 -0700398
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000399 // next line relies on GrBackendTextureDesc's flags matching GrTexture's
bsalomonb15b4c12014-10-29 12:41:57 -0700400 surfDesc.fFlags = (GrSurfaceFlags) desc.fFlags;
401 surfDesc.fWidth = desc.fWidth;
402 surfDesc.fHeight = desc.fHeight;
403 surfDesc.fConfig = desc.fConfig;
404 surfDesc.fSampleCnt = desc.fSampleCnt;
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000405 bool renderTarget = SkToBool(desc.fFlags & kRenderTarget_GrBackendTextureFlag);
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000406 // FIXME: this should be calling resolve_origin(), but Chrome code is currently
407 // assuming the old behaviour, which is that backend textures are always
408 // BottomLeft, even for non-RT's. Once Chrome is fixed, change this to:
409 // glTexDesc.fOrigin = resolve_origin(desc.fOrigin, renderTarget);
410 if (kDefault_GrSurfaceOrigin == desc.fOrigin) {
bsalomonb15b4c12014-10-29 12:41:57 -0700411 surfDesc.fOrigin = kBottomLeft_GrSurfaceOrigin;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000412 } else {
bsalomonb15b4c12014-10-29 12:41:57 -0700413 surfDesc.fOrigin = desc.fOrigin;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000414 }
bsalomon@google.come269f212011-11-07 13:29:52 +0000415
416 GrGLTexture* texture = NULL;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000417 if (renderTarget) {
bsalomonb15b4c12014-10-29 12:41:57 -0700418 GrGLRenderTarget::IDDesc rtIDDesc;
bsalomon5236cf42015-01-14 10:42:08 -0800419 if (!this->createRenderTargetObjects(surfDesc, false, idDesc.fTextureID, &rtIDDesc)) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000420 return NULL;
421 }
bsalomon37dd3312014-11-03 08:47:23 -0800422 texture = SkNEW_ARGS(GrGLTextureRenderTarget, (this, surfDesc, idDesc, rtIDDesc));
bsalomon@google.come269f212011-11-07 13:29:52 +0000423 } else {
bsalomonb15b4c12014-10-29 12:41:57 -0700424 texture = SkNEW_ARGS(GrGLTexture, (this, surfDesc, idDesc));
bsalomon@google.come269f212011-11-07 13:29:52 +0000425 }
426 if (NULL == texture) {
427 return NULL;
428 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000429
bsalomon@google.come269f212011-11-07 13:29:52 +0000430 return texture;
431}
432
bsalomon861e1032014-12-16 07:33:49 -0800433GrRenderTarget* GrGLGpu::onWrapBackendRenderTarget(const GrBackendRenderTargetDesc& wrapDesc) {
bsalomonb15b4c12014-10-29 12:41:57 -0700434 GrGLRenderTarget::IDDesc idDesc;
egdanield803f272015-03-18 13:01:52 -0700435 idDesc.fRTFBOID = static_cast<GrGLuint>(wrapDesc.fRenderTargetHandle);
bsalomonb15b4c12014-10-29 12:41:57 -0700436 idDesc.fMSColorRenderbufferID = 0;
egdanield803f272015-03-18 13:01:52 -0700437 idDesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
bsalomon5236cf42015-01-14 10:42:08 -0800438 idDesc.fLifeCycle = GrGpuResource::kWrapped_LifeCycle;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000439
bsalomonb15b4c12014-10-29 12:41:57 -0700440 GrSurfaceDesc desc;
441 desc.fConfig = wrapDesc.fConfig;
442 desc.fFlags = kCheckAllocation_GrSurfaceFlag;
443 desc.fWidth = wrapDesc.fWidth;
444 desc.fHeight = wrapDesc.fHeight;
445 desc.fSampleCnt = wrapDesc.fSampleCnt;
446 desc.fOrigin = resolve_origin(wrapDesc.fOrigin, true);
447
bsalomon37dd3312014-11-03 08:47:23 -0800448 GrRenderTarget* tgt = SkNEW_ARGS(GrGLRenderTarget, (this, desc, idDesc));
bsalomonb15b4c12014-10-29 12:41:57 -0700449 if (wrapDesc.fStencilBits) {
kkinnunen36c57df2015-01-27 00:30:18 -0800450 GrGLStencilBuffer::IDDesc sbDesc;
bsalomon@google.come269f212011-11-07 13:29:52 +0000451 GrGLStencilBuffer::Format format;
452 format.fInternalFormat = GrGLStencilBuffer::kUnknownInternalFormat;
453 format.fPacked = false;
bsalomonb15b4c12014-10-29 12:41:57 -0700454 format.fStencilBits = wrapDesc.fStencilBits;
455 format.fTotalBits = wrapDesc.fStencilBits;
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000456 GrGLStencilBuffer* sb = SkNEW_ARGS(GrGLStencilBuffer,
457 (this,
kkinnunen36c57df2015-01-27 00:30:18 -0800458 sbDesc,
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000459 desc.fWidth,
460 desc.fHeight,
461 desc.fSampleCnt,
462 format));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800463 tgt->renderTargetPriv().didAttachStencilBuffer(sb);
bsalomon@google.come269f212011-11-07 13:29:52 +0000464 sb->unref();
465 }
466 return tgt;
467}
468
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000469////////////////////////////////////////////////////////////////////////////////
470
bsalomon861e1032014-12-16 07:33:49 -0800471bool GrGLGpu::onWriteTexturePixels(GrTexture* texture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000472 int left, int top, int width, int height,
473 GrPixelConfig config, const void* buffer,
474 size_t rowBytes) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000475 if (NULL == buffer) {
bsalomon@google.com9c680582013-02-06 18:17:50 +0000476 return false;
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000477 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000478 GrGLTexture* glTex = static_cast<GrGLTexture*>(texture);
479
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +0000480 this->setScratchTextureUnit();
bsalomon@google.com6f379512011-11-16 20:36:03 +0000481 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTex->textureID()));
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000482
krajcevski145d48c2014-06-11 16:07:50 -0700483 bool success = false;
bsalomonb15b4c12014-10-29 12:41:57 -0700484 if (GrPixelConfigIsCompressed(glTex->desc().fConfig)) {
bsalomon861e1032014-12-16 07:33:49 -0800485 // We check that config == desc.fConfig in GrGLGpu::canWriteTexturePixels()
bsalomonb15b4c12014-10-29 12:41:57 -0700486 SkASSERT(config == glTex->desc().fConfig);
487 success = this->uploadCompressedTexData(glTex->desc(), buffer, false, left, top, width,
488 height);
krajcevski145d48c2014-06-11 16:07:50 -0700489 } else {
bsalomonb15b4c12014-10-29 12:41:57 -0700490 success = this->uploadTexData(glTex->desc(), false, left, top, width, height, config,
491 buffer, rowBytes);
krajcevski145d48c2014-06-11 16:07:50 -0700492 }
493
494 if (success) {
bsalomonafbf2d62014-09-30 12:18:44 -0700495 texture->texturePriv().dirtyMipMaps(true);
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +0000496 return true;
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +0000497 }
krajcevski145d48c2014-06-11 16:07:50 -0700498
499 return false;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000500}
501
bsalomonb15b4c12014-10-29 12:41:57 -0700502static bool adjust_pixel_ops_params(int surfaceWidth,
503 int surfaceHeight,
504 size_t bpp,
505 int* left, int* top, int* width, int* height,
506 const void** data,
507 size_t* rowBytes) {
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000508 if (!*rowBytes) {
509 *rowBytes = *width * bpp;
510 }
511
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000512 SkIRect subRect = SkIRect::MakeXYWH(*left, *top, *width, *height);
513 SkIRect bounds = SkIRect::MakeWH(surfaceWidth, surfaceHeight);
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000514
515 if (!subRect.intersect(bounds)) {
516 return false;
517 }
518 *data = reinterpret_cast<const void*>(reinterpret_cast<intptr_t>(*data) +
519 (subRect.fTop - *top) * *rowBytes + (subRect.fLeft - *left) * bpp);
520
521 *left = subRect.fLeft;
522 *top = subRect.fTop;
523 *width = subRect.width();
524 *height = subRect.height();
525 return true;
526}
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000527
bsalomonb15b4c12014-10-29 12:41:57 -0700528static inline GrGLenum check_alloc_error(const GrSurfaceDesc& desc,
529 const GrGLInterface* interface) {
bsalomonf2703d82014-10-28 14:33:06 -0700530 if (SkToBool(desc.fFlags & kCheckAllocation_GrSurfaceFlag)) {
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000531 return GR_GL_GET_ERROR(interface);
532 } else {
533 return CHECK_ALLOC_ERROR(interface);
534 }
535}
536
bsalomon861e1032014-12-16 07:33:49 -0800537bool GrGLGpu::uploadTexData(const GrSurfaceDesc& desc,
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000538 bool isNewTexture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000539 int left, int top, int width, int height,
540 GrPixelConfig dataConfig,
541 const void* data,
542 size_t rowBytes) {
bsalomon49f085d2014-09-05 13:34:00 -0700543 SkASSERT(data || isNewTexture);
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000544
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +0000545 // If we're uploading compressed data then we should be using uploadCompressedTexData
546 SkASSERT(!GrPixelConfigIsCompressed(dataConfig));
547
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000548 size_t bpp = GrBytesPerPixel(dataConfig);
549 if (!adjust_pixel_ops_params(desc.fWidth, desc.fHeight, bpp, &left, &top,
550 &width, &height, &data, &rowBytes)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000551 return false;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000552 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000553 size_t trimRowBytes = width * bpp;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000554
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000555 // in case we need a temporary, trimmed copy of the src pixels
georgeb62508b2014-08-12 18:00:47 -0700556 GrAutoMalloc<128 * 128> tempStorage;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000557
commit-bot@chromium.orgcea9abb2013-12-09 19:15:37 +0000558 // We currently lazily create MIPMAPs when the we see a draw with
559 // GrTextureParams::kMipMap_FilterMode. Using texture storage requires that the
560 // MIP levels are all created when the texture is created. So for now we don't use
561 // texture storage.
562 bool useTexStorage = false &&
563 isNewTexture &&
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000564 this->glCaps().texStorageSupport();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000565
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +0000566 if (useTexStorage && kGL_GrGLStandard == this->glStandard()) {
bsalomon@google.com313f0192012-07-10 17:21:02 +0000567 // 565 is not a sized internal format on desktop GL. So on desktop with
568 // 565 we always use an unsized internal format to let the system pick
569 // the best sized format to convert the 565 data to. Since TexStorage
570 // only allows sized internal formats we will instead use TexImage2D.
571 useTexStorage = desc.fConfig != kRGB_565_GrPixelConfig;
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000572 }
573
jvanverth3f801cb2014-12-16 09:49:38 -0800574 GrGLenum internalFormat = 0x0; // suppress warning
575 GrGLenum externalFormat = 0x0; // suppress warning
576 GrGLenum externalType = 0x0; // suppress warning
bsalomone904c092014-07-17 10:50:59 -0700577
jvanverthe1869ca2014-12-16 13:32:27 -0800578 // glTexStorage requires sized internal formats on both desktop and ES. ES2 requires an unsized
jvanverthf72b7522014-12-17 10:46:01 -0800579 // format for glTexImage, unlike ES3 and desktop.
jvanverthe1869ca2014-12-16 13:32:27 -0800580 bool useSizedFormat = useTexStorage;
jvanverthf72b7522014-12-17 10:46:01 -0800581 if (kGL_GrGLStandard == this->glStandard() ||
582 (this->glVersion() >= GR_GL_VER(3, 0) &&
583 // ES3 only works with sized BGRA8 format if "GL_APPLE_texture_format_BGRA8888" enabled
584 (kBGRA_8888_GrPixelConfig != dataConfig || !this->glCaps().bgraIsInternalFormat()))) {
jvanverthe1869ca2014-12-16 13:32:27 -0800585 useSizedFormat = true;
586 }
jvanverthf72b7522014-12-17 10:46:01 -0800587
bsalomone904c092014-07-17 10:50:59 -0700588 if (!this->configToGLFormats(dataConfig, useSizedFormat, &internalFormat,
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000589 &externalFormat, &externalType)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000590 return false;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000591 }
592
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000593 /*
bsalomon@google.com6f379512011-11-16 20:36:03 +0000594 * check whether to allocate a temporary buffer for flipping y or
595 * because our srcData has extra bytes past each row. If so, we need
596 * to trim those off here, since GL ES may not let us specify
597 * GL_UNPACK_ROW_LENGTH.
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000598 */
bsalomon@google.com6f379512011-11-16 20:36:03 +0000599 bool restoreGLRowLength = false;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000600 bool swFlipY = false;
601 bool glFlipY = false;
bsalomon49f085d2014-09-05 13:34:00 -0700602 if (data) {
senorblanco@chromium.orgef5dbe12013-01-28 16:42:38 +0000603 if (kBottomLeft_GrSurfaceOrigin == desc.fOrigin) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000604 if (this->glCaps().unpackFlipYSupport()) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000605 glFlipY = true;
606 } else {
607 swFlipY = true;
608 }
609 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000610 if (this->glCaps().unpackRowLengthSupport() && !swFlipY) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000611 // can't use this for flipping, only non-neg values allowed. :(
612 if (rowBytes != trimRowBytes) {
613 GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
614 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength));
615 restoreGLRowLength = true;
616 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000617 } else {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000618 if (trimRowBytes != rowBytes || swFlipY) {
619 // copy data into our new storage, skipping the trailing bytes
620 size_t trimSize = height * trimRowBytes;
621 const char* src = (const char*)data;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000622 if (swFlipY) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000623 src += (height - 1) * rowBytes;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000624 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000625 char* dst = (char*)tempStorage.reset(trimSize);
626 for (int y = 0; y < height; y++) {
627 memcpy(dst, src, trimRowBytes);
628 if (swFlipY) {
629 src -= rowBytes;
630 } else {
631 src += rowBytes;
632 }
633 dst += trimRowBytes;
634 }
635 // now point data to our copied version
636 data = tempStorage.get();
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000637 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000638 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000639 if (glFlipY) {
640 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_TRUE));
641 }
joshualittee5da552014-07-16 13:32:56 -0700642 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT,
643 static_cast<GrGLint>(GrUnpackAlignment(dataConfig))));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000644 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000645 bool succeeded = true;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000646 if (isNewTexture &&
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000647 0 == left && 0 == top &&
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000648 desc.fWidth == width && desc.fHeight == height) {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000649 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000650 if (useTexStorage) {
commit-bot@chromium.orgcea9abb2013-12-09 19:15:37 +0000651 // We never resize or change formats of textures.
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000652 GL_ALLOC_CALL(this->glInterface(),
653 TexStorage2D(GR_GL_TEXTURE_2D,
654 1, // levels
655 internalFormat,
656 desc.fWidth, desc.fHeight));
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000657 } else {
bsalomond4cb9222014-08-11 14:19:09 -0700658 GL_ALLOC_CALL(this->glInterface(),
659 TexImage2D(GR_GL_TEXTURE_2D,
660 0, // level
661 internalFormat,
662 desc.fWidth, desc.fHeight,
663 0, // border
664 externalFormat, externalType,
665 data));
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000666 }
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000667 GrGLenum error = check_alloc_error(desc, this->glInterface());
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000668 if (error != GR_GL_NO_ERROR) {
669 succeeded = false;
670 } else {
671 // if we have data and we used TexStorage to create the texture, we
672 // now upload with TexSubImage.
bsalomon49f085d2014-09-05 13:34:00 -0700673 if (data && useTexStorage) {
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000674 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D,
675 0, // level
676 left, top,
677 width, height,
678 externalFormat, externalType,
679 data));
680 }
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000681 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000682 } else {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000683 if (swFlipY || glFlipY) {
bsalomon@google.com6f379512011-11-16 20:36:03 +0000684 top = desc.fHeight - (top + height);
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000685 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000686 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D,
687 0, // level
688 left, top,
689 width, height,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000690 externalFormat, externalType, data));
691 }
692
693 if (restoreGLRowLength) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000694 SkASSERT(this->glCaps().unpackRowLengthSupport());
bsalomon@google.com6f379512011-11-16 20:36:03 +0000695 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000696 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000697 if (glFlipY) {
698 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
699 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000700 return succeeded;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000701}
702
krajcevski145d48c2014-06-11 16:07:50 -0700703// TODO: This function is using a lot of wonky semantics like, if width == -1
piotaixre4b23142014-10-02 10:57:53 -0700704// then set width = desc.fWdith ... blah. A better way to do it might be to
krajcevski145d48c2014-06-11 16:07:50 -0700705// create a CompressedTexData struct that takes a desc/ptr and figures out
706// the proper upload semantics. Then users can construct this function how they
707// see fit if they want to go against the "standard" way to do it.
bsalomon861e1032014-12-16 07:33:49 -0800708bool GrGLGpu::uploadCompressedTexData(const GrSurfaceDesc& desc,
krajcevski145d48c2014-06-11 16:07:50 -0700709 const void* data,
710 bool isNewTexture,
711 int left, int top, int width, int height) {
bsalomon49f085d2014-09-05 13:34:00 -0700712 SkASSERT(data || isNewTexture);
krajcevski9c0e6292014-06-02 07:38:14 -0700713
714 // No support for software flip y, yet...
715 SkASSERT(kBottomLeft_GrSurfaceOrigin != desc.fOrigin);
716
krajcevski145d48c2014-06-11 16:07:50 -0700717 if (-1 == width) {
718 width = desc.fWidth;
719 }
720#ifdef SK_DEBUG
721 else {
722 SkASSERT(width <= desc.fWidth);
723 }
724#endif
725
726 if (-1 == height) {
727 height = desc.fHeight;
728 }
729#ifdef SK_DEBUG
730 else {
731 SkASSERT(height <= desc.fHeight);
732 }
733#endif
734
krajcevski9c0e6292014-06-02 07:38:14 -0700735 // Make sure that the width and height that we pass to OpenGL
736 // is a multiple of the block size.
bsalomon98806072014-12-12 15:11:17 -0800737 size_t dataSize = GrCompressedFormatDataSize(desc.fConfig, width, height);
krajcevski9c0e6292014-06-02 07:38:14 -0700738
739 // We only need the internal format for compressed 2D textures.
740 GrGLenum internalFormat = 0;
741 if (!this->configToGLFormats(desc.fConfig, false, &internalFormat, NULL, NULL)) {
742 return false;
743 }
744
krajcevski145d48c2014-06-11 16:07:50 -0700745 if (isNewTexture) {
bsalomond4cb9222014-08-11 14:19:09 -0700746 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
krajcevski145d48c2014-06-11 16:07:50 -0700747 GL_ALLOC_CALL(this->glInterface(),
748 CompressedTexImage2D(GR_GL_TEXTURE_2D,
749 0, // level
750 internalFormat,
751 width, height,
752 0, // border
bsalomon98806072014-12-12 15:11:17 -0800753 SkToInt(dataSize),
krajcevski145d48c2014-06-11 16:07:50 -0700754 data));
bsalomond4cb9222014-08-11 14:19:09 -0700755 GrGLenum error = check_alloc_error(desc, this->glInterface());
756 if (error != GR_GL_NO_ERROR) {
757 return false;
758 }
krajcevski145d48c2014-06-11 16:07:50 -0700759 } else {
bsalomond4cb9222014-08-11 14:19:09 -0700760 // Paletted textures can't be updated.
761 if (GR_GL_PALETTE8_RGBA8 == internalFormat) {
762 return false;
763 }
764 GL_CALL(CompressedTexSubImage2D(GR_GL_TEXTURE_2D,
765 0, // level
766 left, top,
767 width, height,
768 internalFormat,
bsalomon98806072014-12-12 15:11:17 -0800769 SkToInt(dataSize),
bsalomond4cb9222014-08-11 14:19:09 -0700770 data));
krajcevski145d48c2014-06-11 16:07:50 -0700771 }
krajcevski9c0e6292014-06-02 07:38:14 -0700772
bsalomond4cb9222014-08-11 14:19:09 -0700773 return true;
krajcevski9c0e6292014-06-02 07:38:14 -0700774}
775
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +0000776static bool renderbuffer_storage_msaa(GrGLContext& ctx,
777 int sampleCount,
778 GrGLenum format,
779 int width, int height) {
robertphillips@google.com6177e692013-02-28 20:16:25 +0000780 CLEAR_ERROR_BEFORE_ALLOC(ctx.interface());
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +0000781 SkASSERT(GrGLCaps::kNone_MSFBOType != ctx.caps()->msFBOType());
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +0000782 switch (ctx.caps()->msFBOType()) {
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +0000783 case GrGLCaps::kDesktop_ARB_MSFBOType:
784 case GrGLCaps::kDesktop_EXT_MSFBOType:
785 case GrGLCaps::kES_3_0_MSFBOType:
786 GL_ALLOC_CALL(ctx.interface(),
787 RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
788 sampleCount,
789 format,
790 width, height));
791 break;
792 case GrGLCaps::kES_Apple_MSFBOType:
793 GL_ALLOC_CALL(ctx.interface(),
794 RenderbufferStorageMultisampleES2APPLE(GR_GL_RENDERBUFFER,
795 sampleCount,
796 format,
797 width, height));
798 break;
799 case GrGLCaps::kES_EXT_MsToTexture_MSFBOType:
800 case GrGLCaps::kES_IMG_MsToTexture_MSFBOType:
801 GL_ALLOC_CALL(ctx.interface(),
802 RenderbufferStorageMultisampleES2EXT(GR_GL_RENDERBUFFER,
803 sampleCount,
804 format,
805 width, height));
806 break;
807 case GrGLCaps::kNone_MSFBOType:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000808 SkFAIL("Shouldn't be here if we don't support multisampled renderbuffers.");
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +0000809 break;
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000810 }
Brian Salomon9251d4e2015-03-17 16:03:19 -0400811 return (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctx.interface()));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000812}
813
bsalomon5236cf42015-01-14 10:42:08 -0800814bool GrGLGpu::createRenderTargetObjects(const GrSurfaceDesc& desc, bool budgeted, GrGLuint texID,
bsalomonb15b4c12014-10-29 12:41:57 -0700815 GrGLRenderTarget::IDDesc* idDesc) {
816 idDesc->fMSColorRenderbufferID = 0;
egdanield803f272015-03-18 13:01:52 -0700817 idDesc->fRTFBOID = 0;
818 idDesc->fTexFBOID = 0;
bsalomon5236cf42015-01-14 10:42:08 -0800819 idDesc->fLifeCycle = budgeted ? GrGpuResource::kCached_LifeCycle :
820 GrGpuResource::kUncached_LifeCycle;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000821
822 GrGLenum status;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000823
bsalomon@google.comab15d612011-08-09 12:57:56 +0000824 GrGLenum msColorFormat = 0; // suppress warning
825
bsalomonb15b4c12014-10-29 12:41:57 -0700826 if (desc.fSampleCnt > 0 && GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.com347c3822013-05-01 20:10:01 +0000827 goto FAILED;
828 }
829
egdanield803f272015-03-18 13:01:52 -0700830 GL_CALL(GenFramebuffers(1, &idDesc->fTexFBOID));
831 if (!idDesc->fTexFBOID) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000832 goto FAILED;
833 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000834
egdanield803f272015-03-18 13:01:52 -0700835
bsalomon@google.comf3a60c02013-03-19 19:06:09 +0000836 // If we are using multisampling we will create two FBOS. We render to one and then resolve to
837 // the texture bound to the other. The exception is the IMG multisample extension. With this
838 // extension the texture is multisampled when rendered to and then auto-resolves it when it is
839 // rendered from.
bsalomonb15b4c12014-10-29 12:41:57 -0700840 if (desc.fSampleCnt > 0 && this->glCaps().usesMSAARenderBuffers()) {
egdanield803f272015-03-18 13:01:52 -0700841 GL_CALL(GenFramebuffers(1, &idDesc->fRTFBOID));
bsalomonb15b4c12014-10-29 12:41:57 -0700842 GL_CALL(GenRenderbuffers(1, &idDesc->fMSColorRenderbufferID));
egdanield803f272015-03-18 13:01:52 -0700843 if (!idDesc->fRTFBOID ||
844 !idDesc->fMSColorRenderbufferID ||
bsalomonb15b4c12014-10-29 12:41:57 -0700845 !this->configToGLFormats(desc.fConfig,
commit-bot@chromium.org87002952013-08-20 15:12:01 +0000846 // ES2 and ES3 require sized internal formats for rb storage.
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +0000847 kGLES_GrGLStandard == this->glStandard(),
bsalomon@google.com347c3822013-05-01 20:10:01 +0000848 &msColorFormat,
849 NULL,
850 NULL)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000851 goto FAILED;
852 }
853 } else {
egdanield803f272015-03-18 13:01:52 -0700854 idDesc->fRTFBOID = idDesc->fTexFBOID;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000855 }
856
egdanield803f272015-03-18 13:01:52 -0700857 // below here we may bind the FBO
858 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
859 if (idDesc->fRTFBOID != idDesc->fTexFBOID) {
bsalomonb15b4c12014-10-29 12:41:57 -0700860 SkASSERT(desc.fSampleCnt > 0);
861 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, idDesc->fMSColorRenderbufferID));
robertphillips@google.com6177e692013-02-28 20:16:25 +0000862 if (!renderbuffer_storage_msaa(fGLContext,
bsalomonb15b4c12014-10-29 12:41:57 -0700863 desc.fSampleCnt,
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000864 msColorFormat,
bsalomonb15b4c12014-10-29 12:41:57 -0700865 desc.fWidth, desc.fHeight)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000866 goto FAILED;
867 }
egdanield803f272015-03-18 13:01:52 -0700868 fStats.incRenderTargetBinds();
869 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, idDesc->fRTFBOID));
870 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
871 GR_GL_COLOR_ATTACHMENT0,
872 GR_GL_RENDERBUFFER,
873 idDesc->fMSColorRenderbufferID));
bsalomonb15b4c12014-10-29 12:41:57 -0700874 if ((desc.fFlags & kCheckAllocation_GrSurfaceFlag) ||
875 !this->glCaps().isConfigVerifiedColorAttachment(desc.fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000876 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
877 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
878 goto FAILED;
879 }
bsalomonb15b4c12014-10-29 12:41:57 -0700880 fGLContext.caps()->markConfigAsValidColorAttachment(desc.fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000881 }
882 }
egdanield803f272015-03-18 13:01:52 -0700883 fStats.incRenderTargetBinds();
884 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, idDesc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000885
bsalomonb15b4c12014-10-29 12:41:57 -0700886 if (this->glCaps().usesImplicitMSAAResolve() && desc.fSampleCnt > 0) {
egdanield803f272015-03-18 13:01:52 -0700887 GL_CALL(FramebufferTexture2DMultisample(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +0000888 GR_GL_COLOR_ATTACHMENT0,
889 GR_GL_TEXTURE_2D,
bsalomonb15b4c12014-10-29 12:41:57 -0700890 texID, 0, desc.fSampleCnt));
bsalomon@google.comf3a60c02013-03-19 19:06:09 +0000891 } else {
egdanield803f272015-03-18 13:01:52 -0700892 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +0000893 GR_GL_COLOR_ATTACHMENT0,
894 GR_GL_TEXTURE_2D,
895 texID, 0));
896 }
bsalomonb15b4c12014-10-29 12:41:57 -0700897 if ((desc.fFlags & kCheckAllocation_GrSurfaceFlag) ||
898 !this->glCaps().isConfigVerifiedColorAttachment(desc.fConfig)) {
egdanield803f272015-03-18 13:01:52 -0700899 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000900 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
901 goto FAILED;
902 }
bsalomonb15b4c12014-10-29 12:41:57 -0700903 fGLContext.caps()->markConfigAsValidColorAttachment(desc.fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000904 }
905
906 return true;
907
908FAILED:
bsalomonb15b4c12014-10-29 12:41:57 -0700909 if (idDesc->fMSColorRenderbufferID) {
910 GL_CALL(DeleteRenderbuffers(1, &idDesc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000911 }
egdanield803f272015-03-18 13:01:52 -0700912 if (idDesc->fRTFBOID != idDesc->fTexFBOID) {
913 GL_CALL(DeleteFramebuffers(1, &idDesc->fRTFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000914 }
egdanield803f272015-03-18 13:01:52 -0700915 if (idDesc->fTexFBOID) {
916 GL_CALL(DeleteFramebuffers(1, &idDesc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000917 }
918 return false;
919}
920
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000921// good to set a break-point here to know when createTexture fails
922static GrTexture* return_null_texture() {
mtklein@google.com330313a2013-08-22 15:37:26 +0000923// SkDEBUGFAIL("null texture");
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000924 return NULL;
925}
926
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000927#if 0 && defined(SK_DEBUG)
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000928static size_t as_size_t(int x) {
929 return x;
930}
931#endif
932
bsalomon5236cf42015-01-14 10:42:08 -0800933GrTexture* GrGLGpu::onCreateTexture(const GrSurfaceDesc& origDesc, bool budgeted,
934 const void* srcData, size_t rowBytes) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000935
bsalomonb15b4c12014-10-29 12:41:57 -0700936 GrSurfaceDesc desc = origDesc;
reed@google.comac10a2d2010-12-22 21:39:39 +0000937
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000938 // Attempt to catch un- or wrongly initialized sample counts;
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000939 SkASSERT(desc.fSampleCnt >= 0 && desc.fSampleCnt <= 64);
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000940 // We fail if the MSAA was requested and is not available.
941 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType() && desc.fSampleCnt) {
tfarina38406c82014-10-31 07:11:12 -0700942 //SkDebugf("MSAA RT requested but not supported on this platform.");
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000943 return return_null_texture();
944 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000945
bsalomonf2703d82014-10-28 14:33:06 -0700946 bool renderTarget = SkToBool(desc.fFlags & kRenderTarget_GrSurfaceFlag);
reed@google.comac10a2d2010-12-22 21:39:39 +0000947
bsalomonb15b4c12014-10-29 12:41:57 -0700948 // If the sample count exceeds the max then we clamp it.
949 desc.fSampleCnt = SkTMin(desc.fSampleCnt, this->caps()->maxSampleCount());
950 desc.fOrigin = resolve_origin(desc.fOrigin, renderTarget);
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000951
bsalomonb15b4c12014-10-29 12:41:57 -0700952 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType() && desc.fSampleCnt) {
tfarina38406c82014-10-31 07:11:12 -0700953 //SkDebugf("MSAA RT requested but not supported on this platform.");
bsalomon@google.com945bbe12012-06-15 14:30:34 +0000954 return return_null_texture();
reed@google.comac10a2d2010-12-22 21:39:39 +0000955 }
956
reed@google.comac10a2d2010-12-22 21:39:39 +0000957 if (renderTarget) {
bsalomon@google.combcce8922013-03-25 15:38:39 +0000958 int maxRTSize = this->caps()->maxRenderTargetSize();
bsalomonb15b4c12014-10-29 12:41:57 -0700959 if (desc.fWidth > maxRTSize || desc.fHeight > maxRTSize) {
bsalomon@google.com91958362011-06-13 17:58:13 +0000960 return return_null_texture();
961 }
sugoi@google.com6ba0b0f2013-05-03 13:52:32 +0000962 } else {
963 int maxSize = this->caps()->maxTextureSize();
bsalomonb15b4c12014-10-29 12:41:57 -0700964 if (desc.fWidth > maxSize || desc.fHeight > maxSize) {
sugoi@google.com6ba0b0f2013-05-03 13:52:32 +0000965 return return_null_texture();
966 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000967 }
968
bsalomonb15b4c12014-10-29 12:41:57 -0700969 GrGLTexture::IDDesc idDesc;
970 GL_CALL(GenTextures(1, &idDesc.fTextureID));
bsalomon5236cf42015-01-14 10:42:08 -0800971 idDesc.fLifeCycle = budgeted ? GrGpuResource::kCached_LifeCycle :
972 GrGpuResource::kUncached_LifeCycle;
junov@chromium.org8b6b1c92013-08-29 16:22:09 +0000973
bsalomonb15b4c12014-10-29 12:41:57 -0700974 if (!idDesc.fTextureID) {
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000975 return return_null_texture();
976 }
977
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +0000978 this->setScratchTextureUnit();
bsalomonb15b4c12014-10-29 12:41:57 -0700979 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, idDesc.fTextureID));
bsalomon@google.come269f212011-11-07 13:29:52 +0000980
junov@chromium.org8b6b1c92013-08-29 16:22:09 +0000981 if (renderTarget && this->glCaps().textureUsageSupport()) {
982 // provides a hint about how this texture will be used
983 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
984 GR_GL_TEXTURE_USAGE,
985 GR_GL_FRAMEBUFFER_ATTACHMENT));
986 }
987
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000988 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
989 // drivers have a bug where an FBO won't be complete if it includes a
990 // texture that is not mipmap complete (considering the filter in use).
991 GrGLTexture::TexParams initialTexParams;
992 // we only set a subset here so invalidate first
993 initialTexParams.invalidate();
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +0000994 initialTexParams.fMinFilter = GR_GL_NEAREST;
995 initialTexParams.fMagFilter = GR_GL_NEAREST;
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000996 initialTexParams.fWrapS = GR_GL_CLAMP_TO_EDGE;
997 initialTexParams.fWrapT = GR_GL_CLAMP_TO_EDGE;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000998 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
999 GR_GL_TEXTURE_MAG_FILTER,
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00001000 initialTexParams.fMagFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001001 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1002 GR_GL_TEXTURE_MIN_FILTER,
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00001003 initialTexParams.fMinFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001004 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1005 GR_GL_TEXTURE_WRAP_S,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001006 initialTexParams.fWrapS));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001007 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1008 GR_GL_TEXTURE_WRAP_T,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001009 initialTexParams.fWrapT));
bsalomonb15b4c12014-10-29 12:41:57 -07001010 if (!this->uploadTexData(desc, true, 0, 0,
1011 desc.fWidth, desc.fHeight,
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001012 desc.fConfig, srcData, rowBytes)) {
bsalomonb15b4c12014-10-29 12:41:57 -07001013 GL_CALL(DeleteTextures(1, &idDesc.fTextureID));
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001014 return return_null_texture();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001015 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001016
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001017 GrGLTexture* tex;
reed@google.comac10a2d2010-12-22 21:39:39 +00001018 if (renderTarget) {
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001019 // unbind the texture from the texture unit before binding it to the frame buffer
1020 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, 0));
bsalomon5236cf42015-01-14 10:42:08 -08001021 GrGLRenderTarget::IDDesc rtIDDesc;
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001022
bsalomon5236cf42015-01-14 10:42:08 -08001023 if (!this->createRenderTargetObjects(desc, budgeted, idDesc.fTextureID, &rtIDDesc)) {
bsalomonb15b4c12014-10-29 12:41:57 -07001024 GL_CALL(DeleteTextures(1, &idDesc.fTextureID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001025 return return_null_texture();
1026 }
bsalomon37dd3312014-11-03 08:47:23 -08001027 tex = SkNEW_ARGS(GrGLTextureRenderTarget, (this, desc, idDesc, rtIDDesc));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001028 } else {
bsalomonb15b4c12014-10-29 12:41:57 -07001029 tex = SkNEW_ARGS(GrGLTexture, (this, desc, idDesc));
reed@google.comac10a2d2010-12-22 21:39:39 +00001030 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001031 tex->setCachedTexParams(initialTexParams, this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00001032#ifdef TRACE_TEXTURE_CREATION
tfarina38406c82014-10-31 07:11:12 -07001033 SkDebugf("--- new texture [%d] size=(%d %d) config=%d\n",
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001034 glTexDesc.fTextureID, desc.fWidth, desc.fHeight, desc.fConfig);
reed@google.comac10a2d2010-12-22 21:39:39 +00001035#endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001036 return tex;
1037}
1038
bsalomon5236cf42015-01-14 10:42:08 -08001039GrTexture* GrGLGpu::onCreateCompressedTexture(const GrSurfaceDesc& origDesc, bool budgeted,
1040 const void* srcData) {
krajcevski9c0e6292014-06-02 07:38:14 -07001041
bsalomonb15b4c12014-10-29 12:41:57 -07001042 if(SkToBool(origDesc.fFlags & kRenderTarget_GrSurfaceFlag) || origDesc.fSampleCnt > 0) {
krajcevski9c0e6292014-06-02 07:38:14 -07001043 return return_null_texture();
1044 }
1045
1046 // Make sure that we're not flipping Y.
bsalomonb15b4c12014-10-29 12:41:57 -07001047 GrSurfaceOrigin texOrigin = resolve_origin(origDesc.fOrigin, false);
krajcevski9c0e6292014-06-02 07:38:14 -07001048 if (kBottomLeft_GrSurfaceOrigin == texOrigin) {
1049 return return_null_texture();
1050 }
bsalomonb15b4c12014-10-29 12:41:57 -07001051 GrSurfaceDesc desc = origDesc;
1052 desc.fOrigin = texOrigin;
krajcevski9c0e6292014-06-02 07:38:14 -07001053
1054 int maxSize = this->caps()->maxTextureSize();
bsalomonb15b4c12014-10-29 12:41:57 -07001055 if (desc.fWidth > maxSize || desc.fHeight > maxSize) {
krajcevski9c0e6292014-06-02 07:38:14 -07001056 return return_null_texture();
1057 }
1058
bsalomonb15b4c12014-10-29 12:41:57 -07001059 GrGLTexture::IDDesc idDesc;
1060 GL_CALL(GenTextures(1, &idDesc.fTextureID));
bsalomon5236cf42015-01-14 10:42:08 -08001061 idDesc.fLifeCycle = budgeted ? GrGpuResource::kCached_LifeCycle :
1062 GrGpuResource::kUncached_LifeCycle;
krajcevski9c0e6292014-06-02 07:38:14 -07001063
bsalomonb15b4c12014-10-29 12:41:57 -07001064 if (!idDesc.fTextureID) {
krajcevski9c0e6292014-06-02 07:38:14 -07001065 return return_null_texture();
1066 }
1067
1068 this->setScratchTextureUnit();
bsalomonb15b4c12014-10-29 12:41:57 -07001069 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, idDesc.fTextureID));
krajcevski9c0e6292014-06-02 07:38:14 -07001070
1071 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
1072 // drivers have a bug where an FBO won't be complete if it includes a
1073 // texture that is not mipmap complete (considering the filter in use).
1074 GrGLTexture::TexParams initialTexParams;
1075 // we only set a subset here so invalidate first
1076 initialTexParams.invalidate();
1077 initialTexParams.fMinFilter = GR_GL_NEAREST;
1078 initialTexParams.fMagFilter = GR_GL_NEAREST;
1079 initialTexParams.fWrapS = GR_GL_CLAMP_TO_EDGE;
1080 initialTexParams.fWrapT = GR_GL_CLAMP_TO_EDGE;
1081 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1082 GR_GL_TEXTURE_MAG_FILTER,
1083 initialTexParams.fMagFilter));
1084 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1085 GR_GL_TEXTURE_MIN_FILTER,
1086 initialTexParams.fMinFilter));
1087 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1088 GR_GL_TEXTURE_WRAP_S,
1089 initialTexParams.fWrapS));
1090 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1091 GR_GL_TEXTURE_WRAP_T,
1092 initialTexParams.fWrapT));
1093
bsalomonb15b4c12014-10-29 12:41:57 -07001094 if (!this->uploadCompressedTexData(desc, srcData)) {
1095 GL_CALL(DeleteTextures(1, &idDesc.fTextureID));
krajcevski9c0e6292014-06-02 07:38:14 -07001096 return return_null_texture();
1097 }
1098
1099 GrGLTexture* tex;
bsalomonb15b4c12014-10-29 12:41:57 -07001100 tex = SkNEW_ARGS(GrGLTexture, (this, desc, idDesc));
krajcevski9c0e6292014-06-02 07:38:14 -07001101 tex->setCachedTexParams(initialTexParams, this->getResetTimestamp());
1102#ifdef TRACE_TEXTURE_CREATION
tfarina38406c82014-10-31 07:11:12 -07001103 SkDebugf("--- new compressed texture [%d] size=(%d %d) config=%d\n",
krajcevski9c0e6292014-06-02 07:38:14 -07001104 glTexDesc.fTextureID, desc.fWidth, desc.fHeight, desc.fConfig);
1105#endif
1106 return tex;
1107}
1108
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001109namespace {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001110
1111const GrGLuint kUnknownBitCount = GrGLStencilBuffer::kUnknownBitCount;
1112
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001113void inline get_stencil_rb_sizes(const GrGLInterface* gl,
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001114 GrGLStencilBuffer::Format* format) {
sugoi@google.com6ba0b0f2013-05-03 13:52:32 +00001115
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001116 // we shouldn't ever know one size and not the other
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001117 SkASSERT((kUnknownBitCount == format->fStencilBits) ==
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001118 (kUnknownBitCount == format->fTotalBits));
1119 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001120 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001121 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1122 (GrGLint*)&format->fStencilBits);
1123 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001124 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001125 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1126 (GrGLint*)&format->fTotalBits);
1127 format->fTotalBits += format->fStencilBits;
1128 } else {
1129 format->fTotalBits = format->fStencilBits;
1130 }
1131 }
1132}
1133}
1134
bsalomon02a44a42015-02-19 09:09:00 -08001135bool GrGLGpu::createStencilBufferForRenderTarget(GrRenderTarget* rt, int width, int height) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001136 // All internally created RTs are also textures. We don't create
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +00001137 // 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 +00001138 SkASSERT(rt->asTexture());
1139 SkASSERT(width >= rt->width());
1140 SkASSERT(height >= rt->height());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001141
1142 int samples = rt->numSamples();
kkinnunen36c57df2015-01-27 00:30:18 -08001143 GrGLStencilBuffer::IDDesc sbDesc;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001144
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001145 int stencilFmtCnt = this->glCaps().stencilFormats().count();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001146 for (int i = 0; i < stencilFmtCnt; ++i) {
kkinnunen36c57df2015-01-27 00:30:18 -08001147 if (!sbDesc.fRenderbufferID) {
1148 GL_CALL(GenRenderbuffers(1, &sbDesc.fRenderbufferID));
bsalomon12299ab2014-11-14 13:33:09 -08001149 }
kkinnunen36c57df2015-01-27 00:30:18 -08001150 if (!sbDesc.fRenderbufferID) {
bsalomon12299ab2014-11-14 13:33:09 -08001151 return false;
1152 }
kkinnunen36c57df2015-01-27 00:30:18 -08001153 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbDesc.fRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001154 // we start with the last stencil format that succeeded in hopes
1155 // that we won't go through this loop more than once after the
1156 // first (painful) stencil creation.
1157 int sIdx = (i + fLastSuccessfulStencilFmtIdx) % stencilFmtCnt;
bsalomon12299ab2014-11-14 13:33:09 -08001158 const GrGLCaps::StencilFormat& sFmt = this->glCaps().stencilFormats()[sIdx];
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001159 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001160 // we do this "if" so that we don't call the multisample
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001161 // version on a GL that doesn't have an MSAA extension.
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001162 bool created;
1163 if (samples > 0) {
robertphillips@google.com6177e692013-02-28 20:16:25 +00001164 created = renderbuffer_storage_msaa(fGLContext,
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001165 samples,
1166 sFmt.fInternalFormat,
1167 width, height);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001168 } else {
bsalomon12299ab2014-11-14 13:33:09 -08001169 GL_ALLOC_CALL(this->glInterface(), RenderbufferStorage(GR_GL_RENDERBUFFER,
1170 sFmt.fInternalFormat,
1171 width, height));
1172 created = (GR_GL_NO_ERROR == check_alloc_error(rt->desc(), this->glInterface()));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001173 }
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001174 if (created) {
bsalomon9e5fc722015-02-23 10:01:36 -08001175 fStats.incStencilBufferCreates();
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001176 // After sized formats we attempt an unsized format and take
1177 // whatever sizes GL gives us. In that case we query for the size.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001178 GrGLStencilBuffer::Format format = sFmt;
sugoi@google.com6ba0b0f2013-05-03 13:52:32 +00001179 get_stencil_rb_sizes(this->glInterface(), &format);
bsalomondd3143b2015-02-23 09:27:45 -08001180 SkAutoTUnref<GrGLStencilBuffer> sb(SkNEW_ARGS(GrGLStencilBuffer,
kkinnunen36c57df2015-01-27 00:30:18 -08001181 (this, sbDesc, width, height, samples, format)));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001182 if (this->attachStencilBufferToRenderTarget(sb, rt)) {
1183 fLastSuccessfulStencilFmtIdx = sIdx;
bsalomon6bc1b5f2015-02-23 09:06:38 -08001184 rt->renderTargetPriv().didAttachStencilBuffer(sb);
egdanieldf603552015-03-18 13:26:11 -07001185// This work around is currently breaking on windows 7 hd2000 bot when we bind a color buffer
1186#if 0
bsalomondd3143b2015-02-23 09:27:45 -08001187 // Clear the stencil buffer. We use a special purpose FBO for this so that the
1188 // entire stencil buffer is cleared, even if it is attached to an FBO with a
1189 // smaller color target.
egdanield803f272015-03-18 13:01:52 -07001190 if (0 == fStencilClearFBOID) {
1191 GL_CALL(GenFramebuffers(1, &fStencilClearFBOID));
bsalomondd3143b2015-02-23 09:27:45 -08001192 }
robertphillips73165bd2015-03-01 07:38:15 -08001193
egdanield803f272015-03-18 13:01:52 -07001194 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, fStencilClearFBOID));
1195 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
1196 fStats.incRenderTargetBinds();
1197 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomondd3143b2015-02-23 09:27:45 -08001198 GR_GL_STENCIL_ATTACHMENT,
1199 GR_GL_RENDERBUFFER, sbDesc.fRenderbufferID));
1200 if (sFmt.fPacked) {
egdanield803f272015-03-18 13:01:52 -07001201 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomondd3143b2015-02-23 09:27:45 -08001202 GR_GL_DEPTH_ATTACHMENT,
1203 GR_GL_RENDERBUFFER, sbDesc.fRenderbufferID));
1204 }
1205
1206 GL_CALL(ClearStencil(0));
bsalomon9d2049d2015-03-17 12:06:19 -07001207 // Many GL implementations seem to have trouble with clearing an FBO with only
1208 // a stencil buffer.
1209 GrGLuint tempRB;
1210 GL_CALL(GenRenderbuffers(1, &tempRB));
1211 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, tempRB));
Brian Salomon9251d4e2015-03-17 16:03:19 -04001212 if (samples > 0) {
1213 renderbuffer_storage_msaa(fGLContext, samples, GR_GL_RGBA8, width, height);
1214 } else {
1215 GL_CALL(RenderbufferStorage(GR_GL_RENDERBUFFER, GR_GL_RGBA8, width, height));
1216 }
egdanield803f272015-03-18 13:01:52 -07001217 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon9d2049d2015-03-17 12:06:19 -07001218 GR_GL_COLOR_ATTACHMENT0,
1219 GR_GL_RENDERBUFFER, tempRB));
1220
bsalomondd3143b2015-02-23 09:27:45 -08001221 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
1222
egdanield803f272015-03-18 13:01:52 -07001223 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon9d2049d2015-03-17 12:06:19 -07001224 GR_GL_COLOR_ATTACHMENT0,
1225 GR_GL_RENDERBUFFER, 0));
1226 GL_CALL(DeleteRenderbuffers(1, &tempRB));
1227
bsalomondd3143b2015-02-23 09:27:45 -08001228 // Unbind the SB from the FBO so that we don't keep it alive.
egdanield803f272015-03-18 13:01:52 -07001229 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomondd3143b2015-02-23 09:27:45 -08001230 GR_GL_STENCIL_ATTACHMENT,
1231 GR_GL_RENDERBUFFER, 0));
1232 if (sFmt.fPacked) {
egdanield803f272015-03-18 13:01:52 -07001233 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomondd3143b2015-02-23 09:27:45 -08001234 GR_GL_DEPTH_ATTACHMENT,
1235 GR_GL_RENDERBUFFER, 0));
1236 }
egdanieldf603552015-03-18 13:26:11 -07001237#endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001238 return true;
bsalomon12299ab2014-11-14 13:33:09 -08001239 }
bsalomon10e23ca2014-11-25 05:52:06 -08001240 // Remove the scratch key from this resource so we don't grab it from the cache ever
1241 // again.
bsalomon3582d3e2015-02-13 14:20:05 -08001242 sb->resourcePriv().removeScratchKey();
bsalomon10e23ca2014-11-25 05:52:06 -08001243 // Set this to 0 since we handed the valid ID off to the failed stencil buffer resource.
kkinnunen36c57df2015-01-27 00:30:18 -08001244 sbDesc.fRenderbufferID = 0;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001245 }
1246 }
kkinnunen36c57df2015-01-27 00:30:18 -08001247 GL_CALL(DeleteRenderbuffers(1, &sbDesc.fRenderbufferID));
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001248 return false;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001249}
1250
bsalomon861e1032014-12-16 07:33:49 -08001251bool GrGLGpu::attachStencilBufferToRenderTarget(GrStencilBuffer* sb, GrRenderTarget* rt) {
bsalomon37dd3312014-11-03 08:47:23 -08001252 GrGLRenderTarget* glrt = static_cast<GrGLRenderTarget*>(rt);
egdanield803f272015-03-18 13:01:52 -07001253
1254 GrGLuint fbo = glrt->renderFBOID();
1255
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001256 if (NULL == sb) {
bsalomon6bc1b5f2015-02-23 09:06:38 -08001257 if (rt->renderTargetPriv().getStencilBuffer()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001258 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001259 GR_GL_STENCIL_ATTACHMENT,
1260 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001261 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001262 GR_GL_DEPTH_ATTACHMENT,
1263 GR_GL_RENDERBUFFER, 0));
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +00001264#ifdef SK_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001265 GrGLenum status;
1266 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001267 SkASSERT(GR_GL_FRAMEBUFFER_COMPLETE == status);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001268#endif
1269 }
1270 return true;
1271 } else {
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001272 GrGLStencilBuffer* glsb = static_cast<GrGLStencilBuffer*>(sb);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001273 GrGLuint rb = glsb->renderbufferID();
1274
egdanield803f272015-03-18 13:01:52 -07001275 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
1276 fStats.incRenderTargetBinds();
1277 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, fbo));
1278 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001279 GR_GL_STENCIL_ATTACHMENT,
1280 GR_GL_RENDERBUFFER, rb));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001281 if (glsb->format().fPacked) {
egdanield803f272015-03-18 13:01:52 -07001282 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001283 GR_GL_DEPTH_ATTACHMENT,
1284 GR_GL_RENDERBUFFER, rb));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001285 } else {
egdanield803f272015-03-18 13:01:52 -07001286 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001287 GR_GL_DEPTH_ATTACHMENT,
1288 GR_GL_RENDERBUFFER, 0));
reed@google.comac10a2d2010-12-22 21:39:39 +00001289 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001290
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001291 GrGLenum status;
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001292 if (!this->glCaps().isColorConfigAndStencilFormatVerified(rt->config(), glsb->format())) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001293 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1294 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
egdanield803f272015-03-18 13:01:52 -07001295 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1296 GR_GL_STENCIL_ATTACHMENT,
1297 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001298 if (glsb->format().fPacked) {
egdanield803f272015-03-18 13:01:52 -07001299 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1300 GR_GL_DEPTH_ATTACHMENT,
1301 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001302 }
1303 return false;
1304 } else {
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +00001305 fGLContext.caps()->markColorConfigAndStencilFormatAsVerified(
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001306 rt->config(),
1307 glsb->format());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001308 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001309 }
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001310 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001311 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001312}
1313
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001314////////////////////////////////////////////////////////////////////////////////
1315
bsalomon861e1032014-12-16 07:33:49 -08001316GrVertexBuffer* GrGLGpu::onCreateVertexBuffer(size_t size, bool dynamic) {
bsalomon@google.come49ad452013-02-20 19:33:20 +00001317 GrGLVertexBuffer::Desc desc;
1318 desc.fDynamic = dynamic;
1319 desc.fSizeInBytes = size;
bsalomon@google.come49ad452013-02-20 19:33:20 +00001320
bsalomon@google.com96966a52013-02-21 16:34:21 +00001321 if (this->glCaps().useNonVBOVertexAndIndexDynamicData() && desc.fDynamic) {
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001322 desc.fID = 0;
bsalomon@google.come49ad452013-02-20 19:33:20 +00001323 GrGLVertexBuffer* vertexBuffer = SkNEW_ARGS(GrGLVertexBuffer, (this, desc));
reed@google.comac10a2d2010-12-22 21:39:39 +00001324 return vertexBuffer;
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001325 } else {
1326 GL_CALL(GenBuffers(1, &desc.fID));
1327 if (desc.fID) {
bsalomon@google.com6918d482013-03-07 19:09:11 +00001328 fHWGeometryState.setVertexBufferID(this, desc.fID);
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001329 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1330 // make sure driver can allocate memory for this buffer
1331 GL_ALLOC_CALL(this->glInterface(),
1332 BufferData(GR_GL_ARRAY_BUFFER,
robertphillips@google.come9cd27d2013-10-16 17:48:11 +00001333 (GrGLsizeiptr) desc.fSizeInBytes,
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001334 NULL, // data ptr
1335 desc.fDynamic ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW));
1336 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
1337 GL_CALL(DeleteBuffers(1, &desc.fID));
bsalomon@google.com6918d482013-03-07 19:09:11 +00001338 this->notifyVertexBufferDelete(desc.fID);
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001339 return NULL;
1340 }
1341 GrGLVertexBuffer* vertexBuffer = SkNEW_ARGS(GrGLVertexBuffer, (this, desc));
1342 return vertexBuffer;
1343 }
1344 return NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +00001345 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001346}
1347
bsalomon861e1032014-12-16 07:33:49 -08001348GrIndexBuffer* GrGLGpu::onCreateIndexBuffer(size_t size, bool dynamic) {
bsalomon@google.come49ad452013-02-20 19:33:20 +00001349 GrGLIndexBuffer::Desc desc;
1350 desc.fDynamic = dynamic;
1351 desc.fSizeInBytes = size;
bsalomon@google.come49ad452013-02-20 19:33:20 +00001352
bsalomon@google.com96966a52013-02-21 16:34:21 +00001353 if (this->glCaps().useNonVBOVertexAndIndexDynamicData() && desc.fDynamic) {
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001354 desc.fID = 0;
bsalomon@google.come49ad452013-02-20 19:33:20 +00001355 GrIndexBuffer* indexBuffer = SkNEW_ARGS(GrGLIndexBuffer, (this, desc));
reed@google.comac10a2d2010-12-22 21:39:39 +00001356 return indexBuffer;
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001357 } else {
1358 GL_CALL(GenBuffers(1, &desc.fID));
1359 if (desc.fID) {
bsalomon@google.com6918d482013-03-07 19:09:11 +00001360 fHWGeometryState.setIndexBufferIDOnDefaultVertexArray(this, desc.fID);
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001361 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1362 // make sure driver can allocate memory for this buffer
1363 GL_ALLOC_CALL(this->glInterface(),
1364 BufferData(GR_GL_ELEMENT_ARRAY_BUFFER,
robertphillips@google.come9cd27d2013-10-16 17:48:11 +00001365 (GrGLsizeiptr) desc.fSizeInBytes,
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001366 NULL, // data ptr
1367 desc.fDynamic ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW));
1368 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
1369 GL_CALL(DeleteBuffers(1, &desc.fID));
bsalomon@google.com6918d482013-03-07 19:09:11 +00001370 this->notifyIndexBufferDelete(desc.fID);
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001371 return NULL;
1372 }
1373 GrIndexBuffer* indexBuffer = SkNEW_ARGS(GrGLIndexBuffer, (this, desc));
1374 return indexBuffer;
1375 }
1376 return NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +00001377 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001378}
1379
bsalomon3e791242014-12-17 13:43:13 -08001380void GrGLGpu::flushScissor(const GrScissorState& scissorState,
joshualitt77b13072014-10-27 14:51:01 -07001381 const GrGLIRect& rtViewport,
1382 GrSurfaceOrigin rtOrigin) {
robertphillipse85a32d2015-02-10 08:16:55 -08001383 if (scissorState.enabled()) {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001384 GrGLIRect scissor;
bsalomonb0bd4f62014-09-03 07:19:50 -07001385 scissor.setRelativeTo(rtViewport,
robertphillipse85a32d2015-02-10 08:16:55 -08001386 scissorState.rect().fLeft,
1387 scissorState.rect().fTop,
1388 scissorState.rect().width(),
1389 scissorState.rect().height(),
bsalomonb0bd4f62014-09-03 07:19:50 -07001390 rtOrigin);
bsalomon@google.coma3201942012-06-21 19:58:20 +00001391 // if the scissor fully contains the viewport then we fall through and
1392 // disable the scissor test.
bsalomonb0bd4f62014-09-03 07:19:50 -07001393 if (!scissor.contains(rtViewport)) {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001394 if (fHWScissorSettings.fRect != scissor) {
1395 scissor.pushToGLScissor(this->glInterface());
1396 fHWScissorSettings.fRect = scissor;
1397 }
1398 if (kYes_TriState != fHWScissorSettings.fEnabled) {
1399 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1400 fHWScissorSettings.fEnabled = kYes_TriState;
1401 }
1402 return;
1403 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001404 }
joshualitt77b13072014-10-27 14:51:01 -07001405
1406 // See fall through note above
1407 this->disableScissor();
1408}
1409
cdaltond0a840d2015-03-16 17:19:58 -07001410bool GrGLGpu::flushGLState(const DrawArgs& args) {
egdaniel080e6732014-12-22 07:35:52 -08001411 GrXferProcessor::BlendInfo blendInfo;
egdaniel8dd688b2015-01-22 10:16:09 -08001412 const GrPipeline& pipeline = *args.fPipeline;
1413 args.fPipeline->getXferProcessor()->getBlendInfo(&blendInfo);
egdaniel080e6732014-12-22 07:35:52 -08001414
egdaniel8dd688b2015-01-22 10:16:09 -08001415 this->flushDither(pipeline.isDitherState());
egdaniel080e6732014-12-22 07:35:52 -08001416 this->flushColorWrite(blendInfo.fWriteColor);
egdaniel8dd688b2015-01-22 10:16:09 -08001417 this->flushDrawFace(pipeline.getDrawFace());
bsalomonbc3d0de2014-12-15 13:45:03 -08001418
joshualitt873ad0e2015-01-20 09:08:51 -08001419 fCurrentProgram.reset(fProgramCache->getProgram(args));
bsalomon1f78c0a2014-12-17 09:43:13 -08001420 if (NULL == fCurrentProgram.get()) {
1421 SkDEBUGFAIL("Failed to create program!");
1422 return false;
bsalomonbc3d0de2014-12-15 13:45:03 -08001423 }
1424
bsalomon1f78c0a2014-12-17 09:43:13 -08001425 fCurrentProgram.get()->ref();
1426
1427 GrGLuint programID = fCurrentProgram->programID();
1428 if (fHWProgramID != programID) {
1429 GL_CALL(UseProgram(programID));
1430 fHWProgramID = programID;
1431 }
1432
egdanield803f272015-03-18 13:01:52 -07001433 if (blendInfo.fWriteColor) {
1434 this->flushBlend(blendInfo);
1435 }
bsalomon1f78c0a2014-12-17 09:43:13 -08001436
egdaniel8dd688b2015-01-22 10:16:09 -08001437 fCurrentProgram->setData(*args.fPrimitiveProcessor, pipeline, *args.fBatchTracker);
bsalomon1f78c0a2014-12-17 09:43:13 -08001438
egdaniel8dd688b2015-01-22 10:16:09 -08001439 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(pipeline.getRenderTarget());
1440 this->flushStencil(pipeline.getStencil());
1441 this->flushScissor(pipeline.getScissorState(), glRT->getViewport(), glRT->origin());
cdaltond0a840d2015-03-16 17:19:58 -07001442 this->flushHWAAState(glRT, pipeline.isHWAntialiasState());
bsalomonbc3d0de2014-12-15 13:45:03 -08001443
1444 // This must come after textures are flushed because a texture may need
egdanield803f272015-03-18 13:01:52 -07001445 // to be msaa-resolved (which will modify bound FBO state).
1446 this->flushRenderTarget(glRT, NULL);
bsalomonbc3d0de2014-12-15 13:45:03 -08001447
1448 return true;
1449}
1450
joshualitt873ad0e2015-01-20 09:08:51 -08001451void GrGLGpu::setupGeometry(const GrPrimitiveProcessor& primProc,
bsalomonbc3d0de2014-12-15 13:45:03 -08001452 const GrDrawTarget::DrawInfo& info,
1453 size_t* indexOffsetInBytes) {
1454 GrGLVertexBuffer* vbuf;
1455 vbuf = (GrGLVertexBuffer*) info.vertexBuffer();
1456
1457 SkASSERT(vbuf);
1458 SkASSERT(!vbuf->isMapped());
1459
1460 GrGLIndexBuffer* ibuf = NULL;
1461 if (info.isIndexed()) {
1462 SkASSERT(indexOffsetInBytes);
1463
1464 *indexOffsetInBytes = 0;
1465 ibuf = (GrGLIndexBuffer*)info.indexBuffer();
1466
1467 SkASSERT(ibuf);
1468 SkASSERT(!ibuf->isMapped());
1469 *indexOffsetInBytes += ibuf->baseOffset();
1470 }
1471 GrGLAttribArrayState* attribState =
1472 fHWGeometryState.bindArrayAndBuffersToDraw(this, vbuf, ibuf);
1473
joshualitt873ad0e2015-01-20 09:08:51 -08001474 int vaCount = primProc.numAttribs();
joshualitt71c92602015-01-14 08:12:47 -08001475 if (vaCount > 0) {
bsalomonbc3d0de2014-12-15 13:45:03 -08001476
joshualitt873ad0e2015-01-20 09:08:51 -08001477 GrGLsizei stride = static_cast<GrGLsizei>(primProc.getVertexStride());
bsalomonbc3d0de2014-12-15 13:45:03 -08001478
1479 size_t vertexOffsetInBytes = stride * info.startVertex();
1480
1481 vertexOffsetInBytes += vbuf->baseOffset();
1482
bsalomonbc3d0de2014-12-15 13:45:03 -08001483 uint32_t usedAttribArraysMask = 0;
1484 size_t offset = 0;
1485
1486 for (int attribIndex = 0; attribIndex < vaCount; attribIndex++) {
joshualitt873ad0e2015-01-20 09:08:51 -08001487 const GrGeometryProcessor::Attribute& attrib = primProc.getAttrib(attribIndex);
bsalomonbc3d0de2014-12-15 13:45:03 -08001488 usedAttribArraysMask |= (1 << attribIndex);
joshualitt71c92602015-01-14 08:12:47 -08001489 GrVertexAttribType attribType = attrib.fType;
bsalomonbc3d0de2014-12-15 13:45:03 -08001490 attribState->set(this,
1491 attribIndex,
1492 vbuf,
1493 GrGLAttribTypeToLayout(attribType).fCount,
1494 GrGLAttribTypeToLayout(attribType).fType,
1495 GrGLAttribTypeToLayout(attribType).fNormalized,
1496 stride,
1497 reinterpret_cast<GrGLvoid*>(vertexOffsetInBytes + offset));
joshualitt71c92602015-01-14 08:12:47 -08001498 offset += attrib.fOffset;
bsalomonbc3d0de2014-12-15 13:45:03 -08001499 }
1500 attribState->disableUnusedArrays(this, usedAttribArraysMask);
1501 }
1502}
1503
joshualitt873ad0e2015-01-20 09:08:51 -08001504void GrGLGpu::buildProgramDesc(GrProgramDesc* desc,
1505 const GrPrimitiveProcessor& primProc,
egdaniel8dd688b2015-01-22 10:16:09 -08001506 const GrPipeline& pipeline,
joshualitt873ad0e2015-01-20 09:08:51 -08001507 const GrBatchTracker& batchTracker) const {
bsalomon50785a32015-02-06 07:02:37 -08001508 if (!GrGLProgramDescBuilder::Build(desc, primProc, pipeline, this, batchTracker)) {
bsalomonbc3d0de2014-12-15 13:45:03 -08001509 SkDEBUGFAIL("Failed to generate GL program descriptor");
1510 }
1511}
1512
bsalomon861e1032014-12-16 07:33:49 -08001513void GrGLGpu::disableScissor() {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001514 if (kNo_TriState != fHWScissorSettings.fEnabled) {
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001515 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.coma3201942012-06-21 19:58:20 +00001516 fHWScissorSettings.fEnabled = kNo_TriState;
1517 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001518 }
1519}
1520
bsalomon861e1032014-12-16 07:33:49 -08001521void GrGLGpu::onClear(GrRenderTarget* target, const SkIRect* rect, GrColor color,
joshualitt4b68ec02014-11-07 14:11:45 -08001522 bool canIgnoreRect) {
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001523 // parent class should never let us get here with no RT
bsalomon49f085d2014-09-05 13:34:00 -07001524 SkASSERT(target);
bsalomonb0bd4f62014-09-03 07:19:50 -07001525 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001526
robertphillips@google.com56ce48a2013-10-31 21:44:25 +00001527 if (canIgnoreRect && this->glCaps().fullClearIsFree()) {
1528 rect = NULL;
1529 }
1530
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001531 SkIRect clippedRect;
bsalomon49f085d2014-09-05 13:34:00 -07001532 if (rect) {
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001533 // flushScissor expects rect to be clipped to the target.
bsalomon@google.com74b98712011-11-11 19:46:16 +00001534 clippedRect = *rect;
bsalomonb0bd4f62014-09-03 07:19:50 -07001535 SkIRect rtRect = SkIRect::MakeWH(target->width(), target->height());
bsalomon@google.com74b98712011-11-11 19:46:16 +00001536 if (clippedRect.intersect(rtRect)) {
1537 rect = &clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001538 } else {
1539 return;
1540 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001541 }
rmistry@google.comd6bab022013-12-02 13:50:38 +00001542
egdanield803f272015-03-18 13:01:52 -07001543 this->flushRenderTarget(glRT, rect);
bsalomon3e791242014-12-17 13:43:13 -08001544 GrScissorState scissorState;
robertphillipse85a32d2015-02-10 08:16:55 -08001545 if (rect) {
1546 scissorState.set(*rect);
bsalomon@google.coma3201942012-06-21 19:58:20 +00001547 }
joshualitt77b13072014-10-27 14:51:01 -07001548 this->flushScissor(scissorState, glRT->getViewport(), glRT->origin());
bsalomon@google.com74b98712011-11-11 19:46:16 +00001549
1550 GrGLfloat r, g, b, a;
1551 static const GrGLfloat scale255 = 1.f / 255.f;
1552 a = GrColorUnpackA(color) * scale255;
1553 GrGLfloat scaleRGB = scale255;
bsalomon@google.com74b98712011-11-11 19:46:16 +00001554 r = GrColorUnpackR(color) * scaleRGB;
1555 g = GrColorUnpackG(color) * scaleRGB;
1556 b = GrColorUnpackB(color) * scaleRGB;
1557
1558 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00001559 fHWWriteToColor = kYes_TriState;
bsalomon@google.com74b98712011-11-11 19:46:16 +00001560 GL_CALL(ClearColor(r, g, b, a));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001561 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001562}
1563
bsalomon861e1032014-12-16 07:33:49 -08001564void GrGLGpu::discard(GrRenderTarget* renderTarget) {
bsalomon89c62982014-11-03 12:08:42 -08001565 SkASSERT(renderTarget);
commit-bot@chromium.org52ffbf62014-04-02 16:19:33 +00001566 if (!this->caps()->discardRenderTargetSupport()) {
1567 return;
1568 }
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +00001569
1570 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(renderTarget);
egdanield803f272015-03-18 13:01:52 -07001571 if (renderTarget->getUniqueID() != fHWBoundRenderTargetUniqueID) {
1572 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
1573 fStats.incRenderTargetBinds();
1574 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, glRT->renderFBOID()));
1575 }
commit-bot@chromium.org52ffbf62014-04-02 16:19:33 +00001576 switch (this->glCaps().invalidateFBType()) {
joshualitt58162332014-08-01 06:44:53 -07001577 case GrGLCaps::kNone_InvalidateFBType:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +00001578 SkFAIL("Should never get here.");
commit-bot@chromium.org52ffbf62014-04-02 16:19:33 +00001579 break;
1580 case GrGLCaps::kInvalidate_InvalidateFBType:
egdanield803f272015-03-18 13:01:52 -07001581 if (0 == glRT->renderFBOID()) {
commit-bot@chromium.org52ffbf62014-04-02 16:19:33 +00001582 // When rendering to the default framebuffer the legal values for attachments
1583 // are GL_COLOR, GL_DEPTH, GL_STENCIL, ... rather than the various FBO attachment
1584 // types.
1585 static const GrGLenum attachments[] = { GR_GL_COLOR };
egdanield803f272015-03-18 13:01:52 -07001586 GL_CALL(InvalidateFramebuffer(GR_GL_FRAMEBUFFER, SK_ARRAY_COUNT(attachments),
commit-bot@chromium.org52ffbf62014-04-02 16:19:33 +00001587 attachments));
1588 } else {
1589 static const GrGLenum attachments[] = { GR_GL_COLOR_ATTACHMENT0 };
egdanield803f272015-03-18 13:01:52 -07001590 GL_CALL(InvalidateFramebuffer(GR_GL_FRAMEBUFFER, SK_ARRAY_COUNT(attachments),
commit-bot@chromium.org52ffbf62014-04-02 16:19:33 +00001591 attachments));
1592 }
1593 break;
1594 case GrGLCaps::kDiscard_InvalidateFBType: {
egdanield803f272015-03-18 13:01:52 -07001595 if (0 == glRT->renderFBOID()) {
commit-bot@chromium.org4453e8b2014-04-16 14:33:27 +00001596 // When rendering to the default framebuffer the legal values for attachments
1597 // are GL_COLOR, GL_DEPTH, GL_STENCIL, ... rather than the various FBO attachment
1598 // types. See glDiscardFramebuffer() spec.
1599 static const GrGLenum attachments[] = { GR_GL_COLOR };
egdanield803f272015-03-18 13:01:52 -07001600 GL_CALL(DiscardFramebuffer(GR_GL_FRAMEBUFFER, SK_ARRAY_COUNT(attachments),
commit-bot@chromium.org4453e8b2014-04-16 14:33:27 +00001601 attachments));
1602 } else {
1603 static const GrGLenum attachments[] = { GR_GL_COLOR_ATTACHMENT0 };
egdanield803f272015-03-18 13:01:52 -07001604 GL_CALL(DiscardFramebuffer(GR_GL_FRAMEBUFFER, SK_ARRAY_COUNT(attachments),
commit-bot@chromium.org4453e8b2014-04-16 14:33:27 +00001605 attachments));
1606 }
commit-bot@chromium.org52ffbf62014-04-02 16:19:33 +00001607 break;
1608 }
1609 }
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +00001610 renderTarget->flagAsResolved();
1611}
1612
1613
bsalomon861e1032014-12-16 07:33:49 -08001614void GrGLGpu::clearStencil(GrRenderTarget* target) {
bsalomonb0bd4f62014-09-03 07:19:50 -07001615 if (NULL == target) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001616 return;
1617 }
bsalomonb0bd4f62014-09-03 07:19:50 -07001618 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
egdanield803f272015-03-18 13:01:52 -07001619 this->flushRenderTarget(glRT, &SkIRect::EmptyIRect());
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001620
joshualitt77b13072014-10-27 14:51:01 -07001621 this->disableScissor();
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001622
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001623 GL_CALL(StencilMask(0xffffffff));
1624 GL_CALL(ClearStencil(0));
1625 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001626 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001627}
1628
bsalomon861e1032014-12-16 07:33:49 -08001629void GrGLGpu::onClearStencilClip(GrRenderTarget* target, const SkIRect& rect, bool insideClip) {
bsalomon49f085d2014-09-05 13:34:00 -07001630 SkASSERT(target);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001631
bsalomon6bc1b5f2015-02-23 09:06:38 -08001632 GrStencilBuffer* sb = target->renderTargetPriv().getStencilBuffer();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001633 // this should only be called internally when we know we have a
1634 // stencil buffer.
bsalomon6bc1b5f2015-02-23 09:06:38 -08001635 SkASSERT(sb);
1636 GrGLint stencilBitCount = sb->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001637#if 0
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001638 SkASSERT(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00001639 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001640#else
1641 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001642 // ANGLE a partial stencil mask will cause clears to be
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001643 // turned into draws. Our contract on GrDrawTarget says that
1644 // changing the clip between stencil passes may or may not
1645 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00001646 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001647#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001648 GrGLint value;
1649 if (insideClip) {
1650 value = (1 << (stencilBitCount - 1));
1651 } else {
1652 value = 0;
1653 }
bsalomonb0bd4f62014-09-03 07:19:50 -07001654 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
egdanield803f272015-03-18 13:01:52 -07001655 this->flushRenderTarget(glRT, &SkIRect::EmptyIRect());
bsalomon@google.coma3201942012-06-21 19:58:20 +00001656
bsalomon3e791242014-12-17 13:43:13 -08001657 GrScissorState scissorState;
robertphillipse85a32d2015-02-10 08:16:55 -08001658 scissorState.set(rect);
joshualitt77b13072014-10-27 14:51:01 -07001659 this->flushScissor(scissorState, glRT->getViewport(), glRT->origin());
bsalomon@google.coma3201942012-06-21 19:58:20 +00001660
caryclark@google.comcf6285b2012-06-06 12:09:01 +00001661 GL_CALL(StencilMask((uint32_t) clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001662 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001663 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001664 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001665}
1666
bsalomon861e1032014-12-16 07:33:49 -08001667bool GrGLGpu::readPixelsWillPayForYFlip(GrRenderTarget* renderTarget,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001668 int left, int top,
1669 int width, int height,
1670 GrPixelConfig config,
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001671 size_t rowBytes) const {
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001672 // If this rendertarget is aready TopLeft, we don't need to flip.
1673 if (kTopLeft_GrSurfaceOrigin == renderTarget->origin()) {
1674 return false;
1675 }
1676
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001677 // if GL can do the flip then we'll never pay for it.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001678 if (this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001679 return false;
1680 }
1681
1682 // If we have to do memcpy to handle non-trim rowBytes then we
bsalomon@google.com7107fa72011-11-10 14:54:14 +00001683 // get the flip for free. Otherwise it costs.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001684 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001685 return true;
1686 }
1687 // If we have to do memcpys to handle rowBytes then y-flip is free
1688 // Note the rowBytes might be tight to the passed in data, but if data
1689 // gets clipped in x to the target the rowBytes will no longer be tight.
1690 if (left >= 0 && (left + width) < renderTarget->width()) {
1691 return 0 == rowBytes ||
1692 GrBytesPerPixel(config) * width == rowBytes;
1693 } else {
1694 return false;
1695 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001696}
1697
bsalomon861e1032014-12-16 07:33:49 -08001698bool GrGLGpu::onReadPixels(GrRenderTarget* target,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001699 int left, int top,
1700 int width, int height,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001701 GrPixelConfig config,
1702 void* buffer,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001703 size_t rowBytes) {
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +00001704 // We cannot read pixels into a compressed buffer
1705 if (GrPixelConfigIsCompressed(config)) {
1706 return false;
1707 }
1708
1709 GrGLenum format = 0;
1710 GrGLenum type = 0;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001711 bool flipY = kBottomLeft_GrSurfaceOrigin == target->origin();
bsalomon@google.com280e99f2012-01-05 16:17:38 +00001712 if (!this->configToGLFormats(config, false, NULL, &format, &type)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001713 return false;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001714 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001715 size_t bpp = GrBytesPerPixel(config);
1716 if (!adjust_pixel_ops_params(target->width(), target->height(), bpp,
1717 &left, &top, &width, &height,
1718 const_cast<const void**>(&buffer),
1719 &rowBytes)) {
1720 return false;
1721 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001722
bsalomon@google.comc6980972011-11-02 19:57:21 +00001723 // resolve the render target if necessary
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001724 GrGLRenderTarget* tgt = static_cast<GrGLRenderTarget*>(target);
egdanield803f272015-03-18 13:01:52 -07001725 switch (tgt->getResolveType()) {
1726 case GrGLRenderTarget::kCantResolve_ResolveType:
1727 return false;
1728 case GrGLRenderTarget::kAutoResolves_ResolveType:
1729 this->flushRenderTarget(static_cast<GrGLRenderTarget*>(target), &SkIRect::EmptyIRect());
1730 break;
1731 case GrGLRenderTarget::kCanResolve_ResolveType:
1732 this->onResolveRenderTarget(tgt);
1733 // we don't track the state of the READ FBO ID.
1734 fStats.incRenderTargetBinds();
1735 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1736 tgt->textureFBOID()));
1737 break;
1738 default:
1739 SkFAIL("Unknown resolve type");
reed@google.comac10a2d2010-12-22 21:39:39 +00001740 }
1741
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001742 const GrGLIRect& glvp = tgt->getViewport();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001743
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001744 // the read rect is viewport-relative
1745 GrGLIRect readRect;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001746 readRect.setRelativeTo(glvp, left, top, width, height, target->origin());
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001747
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001748 size_t tightRowBytes = bpp * width;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001749 if (0 == rowBytes) {
1750 rowBytes = tightRowBytes;
1751 }
1752 size_t readDstRowBytes = tightRowBytes;
1753 void* readDst = buffer;
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001754
bsalomon@google.comc6980972011-11-02 19:57:21 +00001755 // determine if GL can read using the passed rowBytes or if we need
1756 // a scratch buffer.
georgeb62508b2014-08-12 18:00:47 -07001757 GrAutoMalloc<32 * sizeof(GrColor)> scratch;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001758 if (rowBytes != tightRowBytes) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001759 if (this->glCaps().packRowLengthSupport()) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001760 SkASSERT(!(rowBytes % sizeof(GrColor)));
skia.committer@gmail.com4677acc2013-10-17 07:02:33 +00001761 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH,
robertphillips@google.come9cd27d2013-10-16 17:48:11 +00001762 static_cast<GrGLint>(rowBytes / sizeof(GrColor))));
bsalomon@google.comc6980972011-11-02 19:57:21 +00001763 readDstRowBytes = rowBytes;
1764 } else {
1765 scratch.reset(tightRowBytes * height);
1766 readDst = scratch.get();
1767 }
1768 }
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001769 if (flipY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001770 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 1));
1771 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001772 GL_CALL(ReadPixels(readRect.fLeft, readRect.fBottom,
1773 readRect.fWidth, readRect.fHeight,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001774 format, type, readDst));
1775 if (readDstRowBytes != tightRowBytes) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001776 SkASSERT(this->glCaps().packRowLengthSupport());
bsalomon@google.comc6980972011-11-02 19:57:21 +00001777 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
1778 }
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001779 if (flipY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001780 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 0));
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001781 flipY = false;
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001782 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001783
1784 // now reverse the order of the rows, since GL's are bottom-to-top, but our
bsalomon@google.comc6980972011-11-02 19:57:21 +00001785 // API presents top-to-bottom. We must preserve the padding contents. Note
1786 // that the above readPixels did not overwrite the padding.
1787 if (readDst == buffer) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001788 SkASSERT(rowBytes == readDstRowBytes);
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001789 if (flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001790 scratch.reset(tightRowBytes);
1791 void* tmpRow = scratch.get();
1792 // flip y in-place by rows
1793 const int halfY = height >> 1;
1794 char* top = reinterpret_cast<char*>(buffer);
1795 char* bottom = top + (height - 1) * rowBytes;
1796 for (int y = 0; y < halfY; y++) {
1797 memcpy(tmpRow, top, tightRowBytes);
1798 memcpy(top, bottom, tightRowBytes);
1799 memcpy(bottom, tmpRow, tightRowBytes);
1800 top += rowBytes;
1801 bottom -= rowBytes;
1802 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001803 }
1804 } else {
egdanield803f272015-03-18 13:01:52 -07001805 SkASSERT(readDst != buffer); SkASSERT(rowBytes != tightRowBytes);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001806 // copy from readDst to buffer while flipping y
caryclark@google.comcf6285b2012-06-06 12:09:01 +00001807 // const int halfY = height >> 1;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001808 const char* src = reinterpret_cast<const char*>(readDst);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001809 char* dst = reinterpret_cast<char*>(buffer);
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001810 if (flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001811 dst += (height-1) * rowBytes;
1812 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001813 for (int y = 0; y < height; y++) {
1814 memcpy(dst, src, tightRowBytes);
1815 src += readDstRowBytes;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001816 if (!flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001817 dst += rowBytes;
1818 } else {
1819 dst -= rowBytes;
1820 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001821 }
1822 }
1823 return true;
1824}
1825
egdanield803f272015-03-18 13:01:52 -07001826void GrGLGpu::flushRenderTarget(GrGLRenderTarget* target, const SkIRect* bound) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001827
egdanield803f272015-03-18 13:01:52 -07001828 SkASSERT(target);
bsalomon6ba6fa12015-03-04 11:57:37 -08001829
egdanield803f272015-03-18 13:01:52 -07001830 uint32_t rtID = target->getUniqueID();
1831 if (fHWBoundRenderTargetUniqueID != rtID) {
bsalomon1e0bf7e2015-03-14 12:08:51 -07001832 fStats.incRenderTargetBinds();
egdanield803f272015-03-18 13:01:52 -07001833 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, target->renderFBOID()));
1834#ifdef SK_DEBUG
1835 // don't do this check in Chromium -- this is causing
1836 // lots of repeated command buffer flushes when the compositor is
1837 // rendering with Ganesh, which is really slow; even too slow for
1838 // Debug mode.
1839 if (!this->glContext().isChromium()) {
1840 GrGLenum status;
1841 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1842 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
1843 SkDebugf("GrGLGpu::flushRenderTarget glCheckFramebufferStatus %x\n", status);
1844 }
bsalomon160f24c2015-03-17 15:55:42 -07001845 }
egdanield803f272015-03-18 13:01:52 -07001846#endif
1847 fHWBoundRenderTargetUniqueID = rtID;
1848 const GrGLIRect& vp = target->getViewport();
1849 if (fHWViewport != vp) {
1850 vp.pushToGLViewport(this->glInterface());
1851 fHWViewport = vp;
bsalomon160f24c2015-03-17 15:55:42 -07001852 }
bsalomon5cd020f2015-03-17 12:46:56 -07001853 }
egdanield803f272015-03-18 13:01:52 -07001854 if (NULL == bound || !bound->isEmpty()) {
1855 target->flagAsNeedingResolve(bound);
1856 }
1857
1858 GrTexture *texture = target->asTexture();
1859 if (texture) {
1860 texture->texturePriv().dirtyMipMaps(true);
1861 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001862}
1863
twiz@google.com0f31ca72011-03-18 17:38:11 +00001864GrGLenum gPrimitiveType2GLMode[] = {
1865 GR_GL_TRIANGLES,
1866 GR_GL_TRIANGLE_STRIP,
1867 GR_GL_TRIANGLE_FAN,
1868 GR_GL_POINTS,
1869 GR_GL_LINES,
1870 GR_GL_LINE_STRIP
reed@google.comac10a2d2010-12-22 21:39:39 +00001871};
1872
bsalomon@google.comd302f142011-03-03 13:54:13 +00001873#define SWAP_PER_DRAW 0
1874
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001875#if SWAP_PER_DRAW
commit-bot@chromium.org43823302013-09-25 20:57:51 +00001876 #if defined(SK_BUILD_FOR_MAC)
bsalomon@google.comd302f142011-03-03 13:54:13 +00001877 #include <AGL/agl.h>
commit-bot@chromium.org43823302013-09-25 20:57:51 +00001878 #elif defined(SK_BUILD_FOR_WIN32)
bsalomon@google.comce7357d2012-06-25 17:49:25 +00001879 #include <gl/GL.h>
bsalomon@google.comd302f142011-03-03 13:54:13 +00001880 void SwapBuf() {
1881 DWORD procID = GetCurrentProcessId();
1882 HWND hwnd = GetTopWindow(GetDesktopWindow());
1883 while(hwnd) {
1884 DWORD wndProcID = 0;
1885 GetWindowThreadProcessId(hwnd, &wndProcID);
1886 if(wndProcID == procID) {
1887 SwapBuffers(GetDC(hwnd));
1888 }
1889 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
1890 }
1891 }
1892 #endif
1893#endif
1894
joshualitt873ad0e2015-01-20 09:08:51 -08001895void GrGLGpu::onDraw(const DrawArgs& args, const GrDrawTarget::DrawInfo& info) {
cdaltond0a840d2015-03-16 17:19:58 -07001896 if (!this->flushGLState(args)) {
bsalomond95263c2014-12-16 13:05:12 -08001897 return;
1898 }
1899
joshualitt873ad0e2015-01-20 09:08:51 -08001900 size_t indexOffsetInBytes = 0;
1901 this->setupGeometry(*args.fPrimitiveProcessor, info, &indexOffsetInBytes);
reed@google.comac10a2d2010-12-22 21:39:39 +00001902
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +00001903 SkASSERT((size_t)info.primitiveType() < SK_ARRAY_COUNT(gPrimitiveType2GLMode));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001904
bsalomon@google.com74749cd2013-01-30 16:12:41 +00001905 if (info.isIndexed()) {
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00001906 GrGLvoid* indices =
1907 reinterpret_cast<GrGLvoid*>(indexOffsetInBytes + sizeof(uint16_t) * info.startIndex());
bsalomon@google.com74749cd2013-01-30 16:12:41 +00001908 // info.startVertex() was accounted for by setupGeometry.
1909 GL_CALL(DrawElements(gPrimitiveType2GLMode[info.primitiveType()],
1910 info.indexCount(),
1911 GR_GL_UNSIGNED_SHORT,
1912 indices));
1913 } else {
1914 // Pass 0 for parameter first. We have to adjust glVertexAttribPointer() to account for
1915 // startVertex in the DrawElements case. So we always rely on setupGeometry to have
1916 // accounted for startVertex.
1917 GL_CALL(DrawArrays(gPrimitiveType2GLMode[info.primitiveType()], 0, info.vertexCount()));
1918 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00001919#if SWAP_PER_DRAW
1920 glFlush();
commit-bot@chromium.org43823302013-09-25 20:57:51 +00001921 #if defined(SK_BUILD_FOR_MAC)
bsalomon@google.comd302f142011-03-03 13:54:13 +00001922 aglSwapBuffers(aglGetCurrentContext());
1923 int set_a_break_pt_here = 9;
1924 aglSwapBuffers(aglGetCurrentContext());
commit-bot@chromium.org43823302013-09-25 20:57:51 +00001925 #elif defined(SK_BUILD_FOR_WIN32)
bsalomon@google.comd302f142011-03-03 13:54:13 +00001926 SwapBuf();
1927 int set_a_break_pt_here = 9;
1928 SwapBuf();
1929 #endif
1930#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001931}
1932
bsalomon3e791242014-12-17 13:43:13 -08001933void GrGLGpu::onStencilPath(const GrPath* path, const StencilPathState& state) {
egdaniel080e6732014-12-22 07:35:52 -08001934 this->flushColorWrite(false);
egdaniel8dd688b2015-01-22 10:16:09 -08001935 this->flushDrawFace(GrPipelineBuilder::kBoth_DrawFace);
bsalomon3e791242014-12-17 13:43:13 -08001936
1937 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(state.fRenderTarget);
bsalomon1f78c0a2014-12-17 09:43:13 -08001938 SkISize size = SkISize::Make(rt->width(), rt->height());
bsalomon3e791242014-12-17 13:43:13 -08001939 this->glPathRendering()->setProjectionMatrix(*state.fViewMatrix, size, rt->origin());
1940 this->flushScissor(*state.fScissor, rt->getViewport(), rt->origin());
cdaltond0a840d2015-03-16 17:19:58 -07001941 this->flushHWAAState(rt, state.fUseHWAA);
egdanield803f272015-03-18 13:01:52 -07001942 this->flushRenderTarget(rt, NULL);
1943
bsalomon3e791242014-12-17 13:43:13 -08001944 fPathRendering->stencilPath(path, *state.fStencil);
bsalomond95263c2014-12-16 13:05:12 -08001945}
1946
joshualitt873ad0e2015-01-20 09:08:51 -08001947void GrGLGpu::onDrawPath(const DrawArgs& args, const GrPath* path,
bsalomond95263c2014-12-16 13:05:12 -08001948 const GrStencilSettings& stencil) {
cdaltond0a840d2015-03-16 17:19:58 -07001949 if (!this->flushGLState(args)) {
bsalomond95263c2014-12-16 13:05:12 -08001950 return;
1951 }
1952 fPathRendering->drawPath(path, stencil);
1953}
1954
joshualitt873ad0e2015-01-20 09:08:51 -08001955void GrGLGpu::onDrawPaths(const DrawArgs& args,
bsalomond95263c2014-12-16 13:05:12 -08001956 const GrPathRange* pathRange,
1957 const void* indices,
1958 GrDrawTarget::PathIndexType indexType,
1959 const float transformValues[],
1960 GrDrawTarget::PathTransformType transformType,
1961 int count,
1962 const GrStencilSettings& stencil) {
cdaltond0a840d2015-03-16 17:19:58 -07001963 if (!this->flushGLState(args)) {
bsalomond95263c2014-12-16 13:05:12 -08001964 return;
1965 }
1966 fPathRendering->drawPaths(pathRange, indices, indexType, transformValues,
1967 transformType, count, stencil);
1968}
1969
bsalomon861e1032014-12-16 07:33:49 -08001970void GrGLGpu::onResolveRenderTarget(GrRenderTarget* target) {
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001971 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001972 if (rt->needsResolve()) {
bsalomon@google.com347c3822013-05-01 20:10:01 +00001973 // Some extensions automatically resolves the texture when it is read.
1974 if (this->glCaps().usesMSAARenderBuffers()) {
egdanield803f272015-03-18 13:01:52 -07001975 SkASSERT(rt->textureFBOID() != rt->renderFBOID());
1976 fStats.incRenderTargetBinds();
1977 fStats.incRenderTargetBinds();
1978 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER, rt->renderFBOID()));
1979 GL_CALL(BindFramebuffer(GR_GL_DRAW_FRAMEBUFFER, rt->textureFBOID()));
1980 // make sure we go through flushRenderTarget() since we've modified
1981 // the bound DRAW FBO ID.
1982 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001983 const GrGLIRect& vp = rt->getViewport();
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001984 const SkIRect dirtyRect = rt->getResolveRect();
reed@google.comac10a2d2010-12-22 21:39:39 +00001985
bsalomon@google.com347c3822013-05-01 20:10:01 +00001986 if (GrGLCaps::kES_Apple_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001987 // Apple's extension uses the scissor as the blit bounds.
bsalomon3e791242014-12-17 13:43:13 -08001988 GrScissorState scissorState;
robertphillipse85a32d2015-02-10 08:16:55 -08001989 scissorState.set(dirtyRect);
1990 this->flushScissor(scissorState, vp, rt->origin());
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001991 GL_CALL(ResolveMultisampleFramebuffer());
1992 } else {
robertphillipse85a32d2015-02-10 08:16:55 -08001993 GrGLIRect r;
1994 r.setRelativeTo(vp, dirtyRect.fLeft, dirtyRect.fTop,
1995 dirtyRect.width(), dirtyRect.height(), target->origin());
1996
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001997 int right = r.fLeft + r.fWidth;
1998 int top = r.fBottom + r.fHeight;
derekf8c8f71a2014-09-16 06:24:57 -07001999
2000 // BlitFrameBuffer respects the scissor, so disable it.
joshualitt77b13072014-10-27 14:51:01 -07002001 this->disableScissor();
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002002 GL_CALL(BlitFramebuffer(r.fLeft, r.fBottom, right, top,
2003 r.fLeft, r.fBottom, right, top,
2004 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00002005 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002006 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00002007 rt->flagAsResolved();
reed@google.comac10a2d2010-12-22 21:39:39 +00002008 }
2009}
2010
bsalomon@google.com411dad02012-06-05 20:24:20 +00002011namespace {
bsalomon@google.comd302f142011-03-03 13:54:13 +00002012
bsalomon@google.com411dad02012-06-05 20:24:20 +00002013
2014GrGLenum gr_to_gl_stencil_op(GrStencilOp op) {
2015 static const GrGLenum gTable[] = {
2016 GR_GL_KEEP, // kKeep_StencilOp
2017 GR_GL_REPLACE, // kReplace_StencilOp
2018 GR_GL_INCR_WRAP, // kIncWrap_StencilOp
2019 GR_GL_INCR, // kIncClamp_StencilOp
2020 GR_GL_DECR_WRAP, // kDecWrap_StencilOp
2021 GR_GL_DECR, // kDecClamp_StencilOp
2022 GR_GL_ZERO, // kZero_StencilOp
2023 GR_GL_INVERT, // kInvert_StencilOp
2024 };
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +00002025 GR_STATIC_ASSERT(SK_ARRAY_COUNT(gTable) == kStencilOpCount);
bsalomon@google.com411dad02012-06-05 20:24:20 +00002026 GR_STATIC_ASSERT(0 == kKeep_StencilOp);
2027 GR_STATIC_ASSERT(1 == kReplace_StencilOp);
2028 GR_STATIC_ASSERT(2 == kIncWrap_StencilOp);
2029 GR_STATIC_ASSERT(3 == kIncClamp_StencilOp);
2030 GR_STATIC_ASSERT(4 == kDecWrap_StencilOp);
2031 GR_STATIC_ASSERT(5 == kDecClamp_StencilOp);
2032 GR_STATIC_ASSERT(6 == kZero_StencilOp);
2033 GR_STATIC_ASSERT(7 == kInvert_StencilOp);
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002034 SkASSERT((unsigned) op < kStencilOpCount);
bsalomon@google.com411dad02012-06-05 20:24:20 +00002035 return gTable[op];
2036}
2037
2038void set_gl_stencil(const GrGLInterface* gl,
bsalomon@google.coma3201942012-06-21 19:58:20 +00002039 const GrStencilSettings& settings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00002040 GrGLenum glFace,
bsalomon@google.coma3201942012-06-21 19:58:20 +00002041 GrStencilSettings::Face grFace) {
kkinnunenccdaa042014-08-20 01:36:23 -07002042 GrGLenum glFunc = GrToGLStencilFunc(settings.func(grFace));
bsalomon@google.coma3201942012-06-21 19:58:20 +00002043 GrGLenum glFailOp = gr_to_gl_stencil_op(settings.failOp(grFace));
2044 GrGLenum glPassOp = gr_to_gl_stencil_op(settings.passOp(grFace));
2045
2046 GrGLint ref = settings.funcRef(grFace);
2047 GrGLint mask = settings.funcMask(grFace);
2048 GrGLint writeMask = settings.writeMask(grFace);
bsalomon@google.com411dad02012-06-05 20:24:20 +00002049
2050 if (GR_GL_FRONT_AND_BACK == glFace) {
2051 // we call the combined func just in case separate stencil is not
2052 // supported.
2053 GR_GL_CALL(gl, StencilFunc(glFunc, ref, mask));
2054 GR_GL_CALL(gl, StencilMask(writeMask));
2055 GR_GL_CALL(gl, StencilOp(glFailOp, glPassOp, glPassOp));
2056 } else {
2057 GR_GL_CALL(gl, StencilFuncSeparate(glFace, glFunc, ref, mask));
2058 GR_GL_CALL(gl, StencilMaskSeparate(glFace, writeMask));
2059 GR_GL_CALL(gl, StencilOpSeparate(glFace, glFailOp, glPassOp, glPassOp));
2060 }
2061}
2062}
bsalomon@google.comd302f142011-03-03 13:54:13 +00002063
bsalomon3e791242014-12-17 13:43:13 -08002064void GrGLGpu::flushStencil(const GrStencilSettings& stencilSettings) {
2065 if (fHWStencilSettings != stencilSettings) {
joshualitta58fe352014-10-27 08:39:00 -07002066 if (stencilSettings.isDisabled()) {
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00002067 if (kNo_TriState != fHWStencilTestEnabled) {
2068 GL_CALL(Disable(GR_GL_STENCIL_TEST));
2069 fHWStencilTestEnabled = kNo_TriState;
2070 }
2071 } else {
2072 if (kYes_TriState != fHWStencilTestEnabled) {
2073 GL_CALL(Enable(GR_GL_STENCIL_TEST));
2074 fHWStencilTestEnabled = kYes_TriState;
2075 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00002076 }
joshualitta58fe352014-10-27 08:39:00 -07002077 if (!stencilSettings.isDisabled()) {
bsalomon@google.combcce8922013-03-25 15:38:39 +00002078 if (this->caps()->twoSidedStencilSupport()) {
bsalomon@google.com411dad02012-06-05 20:24:20 +00002079 set_gl_stencil(this->glInterface(),
joshualitta58fe352014-10-27 08:39:00 -07002080 stencilSettings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00002081 GR_GL_FRONT,
bsalomon@google.coma3201942012-06-21 19:58:20 +00002082 GrStencilSettings::kFront_Face);
bsalomon@google.com411dad02012-06-05 20:24:20 +00002083 set_gl_stencil(this->glInterface(),
joshualitta58fe352014-10-27 08:39:00 -07002084 stencilSettings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00002085 GR_GL_BACK,
bsalomon@google.coma3201942012-06-21 19:58:20 +00002086 GrStencilSettings::kBack_Face);
bsalomon@google.comd302f142011-03-03 13:54:13 +00002087 } else {
bsalomon@google.com411dad02012-06-05 20:24:20 +00002088 set_gl_stencil(this->glInterface(),
joshualitta58fe352014-10-27 08:39:00 -07002089 stencilSettings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00002090 GR_GL_FRONT_AND_BACK,
bsalomon@google.coma3201942012-06-21 19:58:20 +00002091 GrStencilSettings::kFront_Face);
bsalomon@google.comd302f142011-03-03 13:54:13 +00002092 }
2093 }
joshualitta58fe352014-10-27 08:39:00 -07002094 fHWStencilSettings = stencilSettings;
reed@google.comac10a2d2010-12-22 21:39:39 +00002095 }
2096}
2097
cdaltond0a840d2015-03-16 17:19:58 -07002098void GrGLGpu::flushHWAAState(GrRenderTarget* rt, bool useHWAA) {
2099 SkASSERT(!useHWAA || rt->isMultisampled());
bsalomon@google.com202d1392013-03-19 18:58:08 +00002100
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +00002101 if (kGL_GrGLStandard == this->glStandard()) {
cdaltond0a840d2015-03-16 17:19:58 -07002102 if (useHWAA) {
2103 if (kYes_TriState != fMSAAEnabled) {
2104 GL_CALL(Enable(GR_GL_MULTISAMPLE));
2105 fMSAAEnabled = kYes_TriState;
2106 }
2107 } else {
2108 if (kNo_TriState != fMSAAEnabled) {
2109 GL_CALL(Disable(GR_GL_MULTISAMPLE));
2110 fMSAAEnabled = kNo_TriState;
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00002111 }
2112 }
2113 }
2114}
2115
egdaniel080e6732014-12-22 07:35:52 -08002116void GrGLGpu::flushBlend(const GrXferProcessor::BlendInfo& blendInfo) {
egdanielb414f252014-07-29 13:15:47 -07002117 // Any optimization to disable blending should have already been applied and
2118 // tweaked the coeffs to (1, 0).
egdanielc2304142014-12-11 13:15:13 -08002119
egdanielc2304142014-12-11 13:15:13 -08002120 GrBlendCoeff srcCoeff = blendInfo.fSrcBlend;
2121 GrBlendCoeff dstCoeff = blendInfo.fDstBlend;
egdanielb414f252014-07-29 13:15:47 -07002122 bool blendOff = kOne_GrBlendCoeff == srcCoeff && kZero_GrBlendCoeff == dstCoeff;
2123 if (blendOff) {
2124 if (kNo_TriState != fHWBlendState.fEnabled) {
2125 GL_CALL(Disable(GR_GL_BLEND));
2126 fHWBlendState.fEnabled = kNo_TriState;
2127 }
2128 } else {
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002129 if (kYes_TriState != fHWBlendState.fEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002130 GL_CALL(Enable(GR_GL_BLEND));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002131 fHWBlendState.fEnabled = kYes_TriState;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002132 }
egdanielb414f252014-07-29 13:15:47 -07002133 if (fHWBlendState.fSrcCoeff != srcCoeff ||
2134 fHWBlendState.fDstCoeff != dstCoeff) {
2135 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
2136 gXfermodeCoeff2Blend[dstCoeff]));
2137 fHWBlendState.fSrcCoeff = srcCoeff;
2138 fHWBlendState.fDstCoeff = dstCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002139 }
egdanielc2304142014-12-11 13:15:13 -08002140 GrColor blendConst = blendInfo.fBlendConstant;
egdanielb414f252014-07-29 13:15:47 -07002141 if ((BlendCoeffReferencesConstant(srcCoeff) ||
2142 BlendCoeffReferencesConstant(dstCoeff)) &&
2143 (!fHWBlendState.fConstColorValid ||
2144 fHWBlendState.fConstColor != blendConst)) {
2145 GrGLfloat c[4];
2146 GrColorToRGBAFloat(blendConst, c);
2147 GL_CALL(BlendColor(c[0], c[1], c[2], c[3]));
2148 fHWBlendState.fConstColor = blendConst;
2149 fHWBlendState.fConstColorValid = true;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002150 }
2151 }
2152}
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002153
commit-bot@chromium.org6364b5e2013-08-20 20:22:52 +00002154static inline GrGLenum tile_to_gl_wrap(SkShader::TileMode tm) {
bsalomon@google.comb8670992012-07-25 21:27:09 +00002155 static const GrGLenum gWrapModes[] = {
2156 GR_GL_CLAMP_TO_EDGE,
2157 GR_GL_REPEAT,
2158 GR_GL_MIRRORED_REPEAT
2159 };
commit-bot@chromium.org5d7ca952013-04-22 20:26:44 +00002160 GR_STATIC_ASSERT(SkShader::kTileModeCount == SK_ARRAY_COUNT(gWrapModes));
bsalomon@google.comb8670992012-07-25 21:27:09 +00002161 GR_STATIC_ASSERT(0 == SkShader::kClamp_TileMode);
2162 GR_STATIC_ASSERT(1 == SkShader::kRepeat_TileMode);
2163 GR_STATIC_ASSERT(2 == SkShader::kMirror_TileMode);
2164 return gWrapModes[tm];
2165}
2166
bsalomon861e1032014-12-16 07:33:49 -08002167void GrGLGpu::bindTexture(int unitIdx, const GrTextureParams& params, GrGLTexture* texture) {
bsalomon49f085d2014-09-05 13:34:00 -07002168 SkASSERT(texture);
tomhudson@google.comd0c1a062012-07-12 17:23:52 +00002169
bsalomon@google.comb8670992012-07-25 21:27:09 +00002170 // If we created a rt/tex and rendered to it without using a texture and now we're texturing
2171 // 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 +00002172 // out of the "last != next" check.
bsalomon37dd3312014-11-03 08:47:23 -08002173 GrGLRenderTarget* texRT = static_cast<GrGLRenderTarget*>(texture->asRenderTarget());
bsalomon49f085d2014-09-05 13:34:00 -07002174 if (texRT) {
bsalomon@google.com4c883782012-06-04 19:05:11 +00002175 this->onResolveRenderTarget(texRT);
2176 }
2177
bsalomon1c63bf62014-07-22 13:09:46 -07002178 uint32_t textureID = texture->getUniqueID();
2179 if (fHWBoundTextureUniqueIDs[unitIdx] != textureID) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002180 this->setTextureUnit(unitIdx);
2181 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, texture->textureID()));
bsalomon1c63bf62014-07-22 13:09:46 -07002182 fHWBoundTextureUniqueIDs[unitIdx] = textureID;
bsalomon@google.com4c883782012-06-04 19:05:11 +00002183 }
2184
bsalomon@google.com4c883782012-06-04 19:05:11 +00002185 ResetTimestamp timestamp;
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002186 const GrGLTexture::TexParams& oldTexParams = texture->getCachedTexParams(&timestamp);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002187 bool setAll = timestamp < this->getResetTimestamp();
2188 GrGLTexture::TexParams newTexParams;
2189
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00002190 static GrGLenum glMinFilterModes[] = {
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +00002191 GR_GL_NEAREST,
2192 GR_GL_LINEAR,
2193 GR_GL_LINEAR_MIPMAP_LINEAR
2194 };
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00002195 static GrGLenum glMagFilterModes[] = {
2196 GR_GL_NEAREST,
2197 GR_GL_LINEAR,
2198 GR_GL_LINEAR
2199 };
commit-bot@chromium.org47442312013-12-19 16:18:01 +00002200 GrTextureParams::FilterMode filterMode = params.filterMode();
bsalomonefd7d452014-10-23 14:17:46 -07002201
2202 if (GrTextureParams::kMipMap_FilterMode == filterMode) {
2203 if (!this->caps()->mipMapSupport() || GrPixelConfigIsCompressed(texture->config())) {
2204 filterMode = GrTextureParams::kBilerp_FilterMode;
2205 }
commit-bot@chromium.org47442312013-12-19 16:18:01 +00002206 }
bsalomonefd7d452014-10-23 14:17:46 -07002207
commit-bot@chromium.org47442312013-12-19 16:18:01 +00002208 newTexParams.fMinFilter = glMinFilterModes[filterMode];
2209 newTexParams.fMagFilter = glMagFilterModes[filterMode];
skia.committer@gmail.comaeefb2a2013-07-27 07:01:06 +00002210
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +00002211 if (GrTextureParams::kMipMap_FilterMode == filterMode &&
bsalomonefd7d452014-10-23 14:17:46 -07002212 texture->texturePriv().mipMapsAreDirty()) {
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +00002213 GL_CALL(GenerateMipmap(GR_GL_TEXTURE_2D));
bsalomonafbf2d62014-09-30 12:18:44 -07002214 texture->texturePriv().dirtyMipMaps(false);
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +00002215 }
bsalomon@google.com4c883782012-06-04 19:05:11 +00002216
bsalomon@google.comb8670992012-07-25 21:27:09 +00002217 newTexParams.fWrapS = tile_to_gl_wrap(params.getTileModeX());
2218 newTexParams.fWrapT = tile_to_gl_wrap(params.getTileModeY());
bsalomon@google.com4c883782012-06-04 19:05:11 +00002219 memcpy(newTexParams.fSwizzleRGBA,
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002220 GrGLShaderBuilder::GetTexParamSwizzle(texture->config(), this->glCaps()),
bsalomon@google.com4c883782012-06-04 19:05:11 +00002221 sizeof(newTexParams.fSwizzleRGBA));
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00002222 if (setAll || newTexParams.fMagFilter != oldTexParams.fMagFilter) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002223 this->setTextureUnit(unitIdx);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002224 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2225 GR_GL_TEXTURE_MAG_FILTER,
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00002226 newTexParams.fMagFilter));
2227 }
2228 if (setAll || newTexParams.fMinFilter != oldTexParams.fMinFilter) {
2229 this->setTextureUnit(unitIdx);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002230 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2231 GR_GL_TEXTURE_MIN_FILTER,
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00002232 newTexParams.fMinFilter));
bsalomon@google.com4c883782012-06-04 19:05:11 +00002233 }
2234 if (setAll || newTexParams.fWrapS != oldTexParams.fWrapS) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002235 this->setTextureUnit(unitIdx);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002236 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2237 GR_GL_TEXTURE_WRAP_S,
2238 newTexParams.fWrapS));
2239 }
2240 if (setAll || newTexParams.fWrapT != oldTexParams.fWrapT) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002241 this->setTextureUnit(unitIdx);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002242 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2243 GR_GL_TEXTURE_WRAP_T,
2244 newTexParams.fWrapT));
2245 }
2246 if (this->glCaps().textureSwizzleSupport() &&
2247 (setAll || memcmp(newTexParams.fSwizzleRGBA,
2248 oldTexParams.fSwizzleRGBA,
2249 sizeof(newTexParams.fSwizzleRGBA)))) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002250 this->setTextureUnit(unitIdx);
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +00002251 if (this->glStandard() == kGLES_GrGLStandard) {
commit-bot@chromium.org6364b5e2013-08-20 20:22:52 +00002252 // ES3 added swizzle support but not GL_TEXTURE_SWIZZLE_RGBA.
2253 const GrGLenum* swizzle = newTexParams.fSwizzleRGBA;
2254 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_SWIZZLE_R, swizzle[0]));
2255 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_SWIZZLE_G, swizzle[1]));
2256 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_SWIZZLE_B, swizzle[2]));
2257 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_SWIZZLE_A, swizzle[3]));
2258 } else {
2259 GR_STATIC_ASSERT(sizeof(newTexParams.fSwizzleRGBA[0]) == sizeof(GrGLint));
2260 const GrGLint* swizzle = reinterpret_cast<const GrGLint*>(newTexParams.fSwizzleRGBA);
2261 GL_CALL(TexParameteriv(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_SWIZZLE_RGBA, swizzle));
2262 }
bsalomon@google.com4c883782012-06-04 19:05:11 +00002263 }
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002264 texture->setCachedTexParams(newTexParams, this->getResetTimestamp());
bsalomon@google.com4c883782012-06-04 19:05:11 +00002265}
2266
bsalomon3e791242014-12-17 13:43:13 -08002267void GrGLGpu::flushDither(bool dither) {
2268 if (dither) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002269 if (kYes_TriState != fHWDitherEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002270 GL_CALL(Enable(GR_GL_DITHER));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002271 fHWDitherEnabled = kYes_TriState;
2272 }
2273 } else {
2274 if (kNo_TriState != fHWDitherEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002275 GL_CALL(Disable(GR_GL_DITHER));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002276 fHWDitherEnabled = kNo_TriState;
reed@google.comac10a2d2010-12-22 21:39:39 +00002277 }
2278 }
bsalomon3e791242014-12-17 13:43:13 -08002279}
reed@google.comac10a2d2010-12-22 21:39:39 +00002280
egdaniel080e6732014-12-22 07:35:52 -08002281void GrGLGpu::flushColorWrite(bool writeColor) {
2282 if (!writeColor) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002283 if (kNo_TriState != fHWWriteToColor) {
2284 GL_CALL(ColorMask(GR_GL_FALSE, GR_GL_FALSE,
2285 GR_GL_FALSE, GR_GL_FALSE));
2286 fHWWriteToColor = kNo_TriState;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002287 }
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002288 } else {
2289 if (kYes_TriState != fHWWriteToColor) {
2290 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
2291 fHWWriteToColor = kYes_TriState;
2292 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00002293 }
bsalomon3e791242014-12-17 13:43:13 -08002294}
bsalomon@google.comd302f142011-03-03 13:54:13 +00002295
egdaniel8dd688b2015-01-22 10:16:09 -08002296void GrGLGpu::flushDrawFace(GrPipelineBuilder::DrawFace face) {
bsalomon3e791242014-12-17 13:43:13 -08002297 if (fHWDrawFace != face) {
2298 switch (face) {
egdaniel8dd688b2015-01-22 10:16:09 -08002299 case GrPipelineBuilder::kCCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002300 GL_CALL(Enable(GR_GL_CULL_FACE));
2301 GL_CALL(CullFace(GR_GL_BACK));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002302 break;
egdaniel8dd688b2015-01-22 10:16:09 -08002303 case GrPipelineBuilder::kCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002304 GL_CALL(Enable(GR_GL_CULL_FACE));
2305 GL_CALL(CullFace(GR_GL_FRONT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002306 break;
egdaniel8dd688b2015-01-22 10:16:09 -08002307 case GrPipelineBuilder::kBoth_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002308 GL_CALL(Disable(GR_GL_CULL_FACE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002309 break;
2310 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +00002311 SkFAIL("Unknown draw face.");
bsalomon@google.comd302f142011-03-03 13:54:13 +00002312 }
bsalomon3e791242014-12-17 13:43:13 -08002313 fHWDrawFace = face;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002314 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002315}
2316
bsalomon861e1032014-12-16 07:33:49 -08002317bool GrGLGpu::configToGLFormats(GrPixelConfig config,
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002318 bool getSizedInternalFormat,
2319 GrGLenum* internalFormat,
2320 GrGLenum* externalFormat,
2321 GrGLenum* externalType) {
2322 GrGLenum dontCare;
2323 if (NULL == internalFormat) {
2324 internalFormat = &dontCare;
2325 }
2326 if (NULL == externalFormat) {
2327 externalFormat = &dontCare;
2328 }
2329 if (NULL == externalType) {
2330 externalType = &dontCare;
2331 }
2332
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +00002333 if(!this->glCaps().isConfigTexturable(config)) {
2334 return false;
2335 }
2336
reed@google.comac10a2d2010-12-22 21:39:39 +00002337 switch (config) {
bsalomon@google.com0342a852012-08-20 19:22:38 +00002338 case kRGBA_8888_GrPixelConfig:
bsalomon@google.comc4364992011-11-07 15:54:49 +00002339 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002340 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002341 if (getSizedInternalFormat) {
2342 *internalFormat = GR_GL_RGBA8;
2343 } else {
2344 *internalFormat = GR_GL_RGBA;
2345 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002346 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.comc4364992011-11-07 15:54:49 +00002347 break;
bsalomon@google.com0342a852012-08-20 19:22:38 +00002348 case kBGRA_8888_GrPixelConfig:
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002349 if (this->glCaps().bgraIsInternalFormat()) {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002350 if (getSizedInternalFormat) {
2351 *internalFormat = GR_GL_BGRA8;
2352 } else {
2353 *internalFormat = GR_GL_BGRA;
2354 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002355 } else {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002356 if (getSizedInternalFormat) {
2357 *internalFormat = GR_GL_RGBA8;
2358 } else {
2359 *internalFormat = GR_GL_RGBA;
2360 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002361 }
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002362 *externalFormat = GR_GL_BGRA;
bsalomon@google.com6f379512011-11-16 20:36:03 +00002363 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002364 break;
jvanverthfa1e8a72014-12-22 08:31:49 -08002365 case kSRGBA_8888_GrPixelConfig:
2366 *internalFormat = GR_GL_SRGB_ALPHA;
2367 *externalFormat = GR_GL_SRGB_ALPHA;
2368 if (getSizedInternalFormat) {
2369 *internalFormat = GR_GL_SRGB8_ALPHA8;
2370 } else {
2371 *internalFormat = GR_GL_SRGB_ALPHA;
2372 }
2373 *externalType = GR_GL_UNSIGNED_BYTE;
2374 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002375 case kRGB_565_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002376 *internalFormat = GR_GL_RGB;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002377 *externalFormat = GR_GL_RGB;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002378 if (getSizedInternalFormat) {
jvanverth3f801cb2014-12-16 09:49:38 -08002379 if (!this->glCaps().ES2CompatibilitySupport()) {
2380 *internalFormat = GR_GL_RGB5;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002381 } else {
2382 *internalFormat = GR_GL_RGB565;
2383 }
2384 } else {
2385 *internalFormat = GR_GL_RGB;
2386 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002387 *externalType = GR_GL_UNSIGNED_SHORT_5_6_5;
reed@google.comac10a2d2010-12-22 21:39:39 +00002388 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002389 case kRGBA_4444_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002390 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002391 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002392 if (getSizedInternalFormat) {
2393 *internalFormat = GR_GL_RGBA4;
2394 } else {
2395 *internalFormat = GR_GL_RGBA;
2396 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002397 *externalType = GR_GL_UNSIGNED_SHORT_4_4_4_4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002398 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002399 case kIndex_8_GrPixelConfig:
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +00002400 // no sized/unsized internal format distinction here
2401 *internalFormat = GR_GL_PALETTE8_RGBA8;
reed@google.comac10a2d2010-12-22 21:39:39 +00002402 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002403 case kAlpha_8_GrPixelConfig:
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002404 if (this->glCaps().textureRedSupport()) {
2405 *internalFormat = GR_GL_RED;
2406 *externalFormat = GR_GL_RED;
2407 if (getSizedInternalFormat) {
2408 *internalFormat = GR_GL_R8;
2409 } else {
2410 *internalFormat = GR_GL_RED;
2411 }
2412 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002413 } else {
2414 *internalFormat = GR_GL_ALPHA;
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002415 *externalFormat = GR_GL_ALPHA;
2416 if (getSizedInternalFormat) {
2417 *internalFormat = GR_GL_ALPHA8;
2418 } else {
2419 *internalFormat = GR_GL_ALPHA;
2420 }
2421 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002422 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002423 break;
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +00002424 case kETC1_GrPixelConfig:
2425 *internalFormat = GR_GL_COMPRESSED_RGB8_ETC1;
2426 break;
2427 case kLATC_GrPixelConfig:
2428 switch(this->glCaps().latcAlias()) {
2429 case GrGLCaps::kLATC_LATCAlias:
2430 *internalFormat = GR_GL_COMPRESSED_LUMINANCE_LATC1;
2431 break;
2432 case GrGLCaps::kRGTC_LATCAlias:
2433 *internalFormat = GR_GL_COMPRESSED_RED_RGTC1;
2434 break;
2435 case GrGLCaps::k3DC_LATCAlias:
2436 *internalFormat = GR_GL_COMPRESSED_3DC_X;
2437 break;
2438 }
2439 break;
krajcevski238b4562014-06-30 09:09:22 -07002440 case kR11_EAC_GrPixelConfig:
2441 *internalFormat = GR_GL_COMPRESSED_R11;
2442 break;
krajcevski7ef21622014-07-16 15:21:13 -07002443
2444 case kASTC_12x12_GrPixelConfig:
2445 *internalFormat = GR_GL_COMPRESSED_RGBA_ASTC_12x12;
2446 break;
2447
joshualittee5da552014-07-16 13:32:56 -07002448 case kRGBA_float_GrPixelConfig:
2449 *internalFormat = GR_GL_RGBA32F;
2450 *externalFormat = GR_GL_RGBA;
2451 *externalType = GR_GL_FLOAT;
2452 break;
krajcevski7ef21622014-07-16 15:21:13 -07002453
jvanverth28f9c602014-12-05 13:06:35 -08002454 case kAlpha_half_GrPixelConfig:
jvanverth1334c212014-12-18 05:44:55 -08002455 if (this->glCaps().textureRedSupport()) {
2456 if (getSizedInternalFormat) {
2457 *internalFormat = GR_GL_R16F;
2458 } else {
2459 *internalFormat = GR_GL_RED;
2460 }
jvanverth28f9c602014-12-05 13:06:35 -08002461 *externalFormat = GR_GL_RED;
jvanverth1334c212014-12-18 05:44:55 -08002462 } else {
2463 if (getSizedInternalFormat) {
2464 *internalFormat = GR_GL_ALPHA16F;
2465 } else {
2466 *internalFormat = GR_GL_ALPHA;
2467 }
2468 *externalFormat = GR_GL_ALPHA;
2469 }
2470 if (kGL_GrGLStandard == this->glStandard() || this->glVersion() >= GR_GL_VER(3, 0)) {
jvanvertha60b2ea2014-12-12 05:58:06 -08002471 *externalType = GR_GL_HALF_FLOAT;
jvanverth28f9c602014-12-05 13:06:35 -08002472 } else {
jvanverth1334c212014-12-18 05:44:55 -08002473 *externalType = GR_GL_HALF_FLOAT_OES;
jvanverth28f9c602014-12-05 13:06:35 -08002474 }
2475 break;
2476
reed@google.comac10a2d2010-12-22 21:39:39 +00002477 default:
2478 return false;
2479 }
2480 return true;
2481}
2482
bsalomon861e1032014-12-16 07:33:49 -08002483void GrGLGpu::setTextureUnit(int unit) {
bsalomon1c63bf62014-07-22 13:09:46 -07002484 SkASSERT(unit >= 0 && unit < fHWBoundTextureUniqueIDs.count());
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002485 if (unit != fHWActiveTextureUnitIdx) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002486 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com49209392012-06-05 15:13:46 +00002487 fHWActiveTextureUnitIdx = unit;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002488 }
2489}
bsalomon@google.com316f99232011-01-13 21:28:12 +00002490
bsalomon861e1032014-12-16 07:33:49 -08002491void GrGLGpu::setScratchTextureUnit() {
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002492 // Bind the last texture unit since it is the least likely to be used by GrGLProgram.
bsalomon1c63bf62014-07-22 13:09:46 -07002493 int lastUnitIdx = fHWBoundTextureUniqueIDs.count() - 1;
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002494 if (lastUnitIdx != fHWActiveTextureUnitIdx) {
2495 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + lastUnitIdx));
2496 fHWActiveTextureUnitIdx = lastUnitIdx;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002497 }
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002498 // clear out the this field so that if a program does use this unit it will rebind the correct
2499 // texture.
bsalomon1c63bf62014-07-22 13:09:46 -07002500 fHWBoundTextureUniqueIDs[lastUnitIdx] = SK_InvalidUniqueID;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002501}
2502
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002503namespace {
2504// Determines whether glBlitFramebuffer could be used between src and dst.
bsalomon@google.comeb851172013-04-15 13:51:00 +00002505inline bool can_blit_framebuffer(const GrSurface* dst,
2506 const GrSurface* src,
egdaniel0f5f9672015-02-03 11:10:51 -08002507 const GrGLGpu* gpu) {
commit-bot@chromium.org6b7938f2013-10-15 14:18:16 +00002508 if (gpu->glCaps().isConfigRenderable(dst->config(), dst->desc().fSampleCnt > 0) &&
2509 gpu->glCaps().isConfigRenderable(src->config(), src->desc().fSampleCnt > 0) &&
bsalomon@google.com347c3822013-05-01 20:10:01 +00002510 gpu->glCaps().usesMSAARenderBuffers()) {
commit-bot@chromium.orga8e5a062013-09-05 23:44:09 +00002511 // ES3 doesn't allow framebuffer blits when the src has MSAA and the configs don't match
2512 // or the rects are not the same (not just the same size but have the same edges).
2513 if (GrGLCaps::kES_3_0_MSFBOType == gpu->glCaps().msFBOType() &&
2514 (src->desc().fSampleCnt > 0 || src->config() != dst->config())) {
2515 return false;
2516 }
bsalomon@google.comeb851172013-04-15 13:51:00 +00002517 return true;
2518 } else {
2519 return false;
2520 }
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002521}
bsalomon@google.comeb851172013-04-15 13:51:00 +00002522
2523inline bool can_copy_texsubimage(const GrSurface* dst,
2524 const GrSurface* src,
egdaniel0f5f9672015-02-03 11:10:51 -08002525 const GrGLGpu* gpu) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002526 // Table 3.9 of the ES2 spec indicates the supported formats with CopyTexSubImage
2527 // and BGRA isn't in the spec. There doesn't appear to be any extension that adds it. Perhaps
2528 // many drivers would allow it to work, but ANGLE does not.
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +00002529 if (kGLES_GrGLStandard == gpu->glStandard() && gpu->glCaps().bgraIsInternalFormat() &&
bsalomon@google.comeb851172013-04-15 13:51:00 +00002530 (kBGRA_8888_GrPixelConfig == dst->config() || kBGRA_8888_GrPixelConfig == src->config())) {
2531 return false;
2532 }
2533 const GrGLRenderTarget* dstRT = static_cast<const GrGLRenderTarget*>(dst->asRenderTarget());
2534 // If dst is multisampled (and uses an extension where there is a separate MSAA renderbuffer)
2535 // then we don't want to copy to the texture but to the MSAA buffer.
egdanield803f272015-03-18 13:01:52 -07002536 if (dstRT && dstRT->renderFBOID() != dstRT->textureFBOID()) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002537 return false;
2538 }
bsalomon@google.coma2719852013-04-17 14:25:27 +00002539 const GrGLRenderTarget* srcRT = static_cast<const GrGLRenderTarget*>(src->asRenderTarget());
2540 // If the src is multisampled (and uses an extension where there is a separate MSAA
2541 // renderbuffer) then it is an invalid operation to call CopyTexSubImage
egdanield803f272015-03-18 13:01:52 -07002542 if (srcRT && srcRT->renderFBOID() != srcRT->textureFBOID()) {
bsalomon@google.coma2719852013-04-17 14:25:27 +00002543 return false;
2544 }
commit-bot@chromium.org6b7938f2013-10-15 14:18:16 +00002545 if (gpu->glCaps().isConfigRenderable(src->config(), src->desc().fSampleCnt > 0) &&
bsalomon49f085d2014-09-05 13:34:00 -07002546 dst->asTexture() &&
commit-bot@chromium.org6b7938f2013-10-15 14:18:16 +00002547 dst->origin() == src->origin() &&
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +00002548 !GrPixelConfigIsCompressed(src->config())) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002549 return true;
2550 } else {
2551 return false;
2552 }
2553}
2554
robertphillips754f4e92014-09-18 13:52:08 -07002555}
2556
bsalomon@google.comeb851172013-04-15 13:51:00 +00002557// If a temporary FBO was created, its non-zero ID is returned. The viewport that the copy rect is
2558// relative to is output.
egdanield803f272015-03-18 13:01:52 -07002559GrGLuint GrGLGpu::bindSurfaceAsFBO(GrSurface* surface, GrGLenum fboTarget, GrGLIRect* viewport,
2560 TempFBOTarget tempFBOTarget) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002561 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(surface->asRenderTarget());
bsalomon@google.comeb851172013-04-15 13:51:00 +00002562 if (NULL == rt) {
bsalomon49f085d2014-09-05 13:34:00 -07002563 SkASSERT(surface->asTexture());
bsalomon@google.comeb851172013-04-15 13:51:00 +00002564 GrGLuint texID = static_cast<GrGLTexture*>(surface->asTexture())->textureID();
egdanield803f272015-03-18 13:01:52 -07002565 GrGLuint* tempFBOID;
2566 tempFBOID = kSrc_TempFBOTarget == tempFBOTarget ? &fTempSrcFBOID : &fTempDstFBOID;
egdaniel0f5f9672015-02-03 11:10:51 -08002567
egdanield803f272015-03-18 13:01:52 -07002568 if (0 == *tempFBOID) {
2569 GR_GL_CALL(this->glInterface(), GenFramebuffers(1, tempFBOID));
egdaniel0f5f9672015-02-03 11:10:51 -08002570 }
2571
egdanield803f272015-03-18 13:01:52 -07002572 fStats.incRenderTargetBinds();
2573 GR_GL_CALL(this->glInterface(), BindFramebuffer(fboTarget, *tempFBOID));
2574 GR_GL_CALL(this->glInterface(), FramebufferTexture2D(fboTarget,
robertphillips754f4e92014-09-18 13:52:08 -07002575 GR_GL_COLOR_ATTACHMENT0,
2576 GR_GL_TEXTURE_2D,
2577 texID,
2578 0));
bsalomon@google.comeb851172013-04-15 13:51:00 +00002579 viewport->fLeft = 0;
2580 viewport->fBottom = 0;
2581 viewport->fWidth = surface->width();
2582 viewport->fHeight = surface->height();
egdanield803f272015-03-18 13:01:52 -07002583 return *tempFBOID;
bsalomon@google.comeb851172013-04-15 13:51:00 +00002584 } else {
egdanield803f272015-03-18 13:01:52 -07002585 GrGLuint tempFBOID = 0;
2586 fStats.incRenderTargetBinds();
2587 GR_GL_CALL(this->glInterface(), BindFramebuffer(fboTarget, rt->renderFBOID()));
bsalomon@google.comeb851172013-04-15 13:51:00 +00002588 *viewport = rt->getViewport();
egdanield803f272015-03-18 13:01:52 -07002589 return tempFBOID;
bsalomon@google.comeb851172013-04-15 13:51:00 +00002590 }
egdaniel0f5f9672015-02-03 11:10:51 -08002591}
2592
egdanield803f272015-03-18 13:01:52 -07002593void GrGLGpu::unbindTextureFromFBO(GrGLenum fboTarget) {
2594 GR_GL_CALL(this->glInterface(), FramebufferTexture2D(fboTarget,
egdaniel0f5f9672015-02-03 11:10:51 -08002595 GR_GL_COLOR_ATTACHMENT0,
2596 GR_GL_TEXTURE_2D,
2597 0,
2598 0));
bsalomon@google.comeb851172013-04-15 13:51:00 +00002599}
2600
bsalomon861e1032014-12-16 07:33:49 -08002601bool GrGLGpu::initCopySurfaceDstDesc(const GrSurface* src, GrSurfaceDesc* desc) {
bsalomonf90a02b2014-11-26 12:28:00 -08002602 // In here we look for opportunities to use CopyTexSubImage, or fbo blit. If neither are
2603 // possible and we return false to fallback to creating a render target dst for render-to-
2604 // texture. This code prefers CopyTexSubImage to fbo blit and avoids triggering temporary fbo
2605 // creation. It isn't clear that avoiding temporary fbo creation is actually optimal.
2606
bsalomon@google.comeb851172013-04-15 13:51:00 +00002607 // Check for format issues with glCopyTexSubImage2D
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +00002608 if (kGLES_GrGLStandard == this->glStandard() && this->glCaps().bgraIsInternalFormat() &&
bsalomon@google.comeb851172013-04-15 13:51:00 +00002609 kBGRA_8888_GrPixelConfig == src->config()) {
bsalomonf90a02b2014-11-26 12:28:00 -08002610 // glCopyTexSubImage2D doesn't work with this config. If the bgra can be used with fbo blit
2611 // then we set up for that, otherwise fail.
2612 if (this->caps()->isConfigRenderable(kBGRA_8888_GrPixelConfig, false)) {
2613 desc->fOrigin = kDefault_GrSurfaceOrigin;
bsalomon6bc1b5f2015-02-23 09:06:38 -08002614 desc->fFlags = kRenderTarget_GrSurfaceFlag;
bsalomonf90a02b2014-11-26 12:28:00 -08002615 desc->fConfig = kBGRA_8888_GrPixelConfig;
2616 return true;
2617 }
2618 return false;
bsalomon@google.comeb851172013-04-15 13:51:00 +00002619 } else if (NULL == src->asRenderTarget()) {
bsalomonf90a02b2014-11-26 12:28:00 -08002620 // CopyTexSubImage2D or fbo blit would require creating a temp fbo for the src.
2621 return false;
bsalomon@google.coma2719852013-04-17 14:25:27 +00002622 }
2623
2624 const GrGLRenderTarget* srcRT = static_cast<const GrGLRenderTarget*>(src->asRenderTarget());
egdanield803f272015-03-18 13:01:52 -07002625 if (srcRT && srcRT->renderFBOID() != srcRT->textureFBOID()) {
bsalomonf90a02b2014-11-26 12:28:00 -08002626 // It's illegal to call CopyTexSubImage2D on a MSAA renderbuffer. Set up for FBO blit or
2627 // fail.
2628 if (this->caps()->isConfigRenderable(src->config(), false)) {
2629 desc->fOrigin = kDefault_GrSurfaceOrigin;
bsalomon6bc1b5f2015-02-23 09:06:38 -08002630 desc->fFlags = kRenderTarget_GrSurfaceFlag;
bsalomonf90a02b2014-11-26 12:28:00 -08002631 desc->fConfig = src->config();
2632 return true;
2633 }
2634 return false;
bsalomon@google.comeb851172013-04-15 13:51:00 +00002635 }
bsalomonf90a02b2014-11-26 12:28:00 -08002636
2637 // We'll do a CopyTexSubImage. Make the dst a plain old texture.
2638 desc->fConfig = src->config();
2639 desc->fOrigin = src->origin();
2640 desc->fFlags = kNone_GrSurfaceFlags;
2641 return true;
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002642}
2643
bsalomon861e1032014-12-16 07:33:49 -08002644bool GrGLGpu::copySurface(GrSurface* dst,
joshualitta7024152014-11-03 14:16:35 -08002645 GrSurface* src,
2646 const SkIRect& srcRect,
2647 const SkIPoint& dstPoint) {
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002648 bool copied = false;
joshualitta7024152014-11-03 14:16:35 -08002649 if (can_copy_texsubimage(dst, src, this)) {
egdanield803f272015-03-18 13:01:52 -07002650 GrGLuint srcFBO;
bsalomon@google.comeb851172013-04-15 13:51:00 +00002651 GrGLIRect srcVP;
egdanield803f272015-03-18 13:01:52 -07002652 srcFBO = this->bindSurfaceAsFBO(src, GR_GL_FRAMEBUFFER, &srcVP, kSrc_TempFBOTarget);
bsalomon@google.comeb851172013-04-15 13:51:00 +00002653 GrGLTexture* dstTex = static_cast<GrGLTexture*>(dst->asTexture());
bsalomon49f085d2014-09-05 13:34:00 -07002654 SkASSERT(dstTex);
egdanield803f272015-03-18 13:01:52 -07002655 // We modified the bound FBO
2656 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
bsalomon@google.comeb851172013-04-15 13:51:00 +00002657 GrGLIRect srcGLRect;
2658 srcGLRect.setRelativeTo(srcVP,
2659 srcRect.fLeft,
2660 srcRect.fTop,
2661 srcRect.width(),
2662 srcRect.height(),
2663 src->origin());
2664
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002665 this->setScratchTextureUnit();
bsalomon@google.comeb851172013-04-15 13:51:00 +00002666 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, dstTex->textureID()));
2667 GrGLint dstY;
2668 if (kBottomLeft_GrSurfaceOrigin == dst->origin()) {
2669 dstY = dst->height() - (dstPoint.fY + srcGLRect.fHeight);
2670 } else {
2671 dstY = dstPoint.fY;
2672 }
2673 GL_CALL(CopyTexSubImage2D(GR_GL_TEXTURE_2D, 0,
2674 dstPoint.fX, dstY,
2675 srcGLRect.fLeft, srcGLRect.fBottom,
2676 srcGLRect.fWidth, srcGLRect.fHeight));
2677 copied = true;
egdanield803f272015-03-18 13:01:52 -07002678 if (srcFBO) {
2679 this->unbindTextureFromFBO(GR_GL_FRAMEBUFFER);
2680 }
joshualitta7024152014-11-03 14:16:35 -08002681 } else if (can_blit_framebuffer(dst, src, this)) {
egdanield803f272015-03-18 13:01:52 -07002682 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
2683 srcRect.width(), srcRect.height());
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002684 bool selfOverlap = false;
bsalomona2c23232014-11-25 07:41:12 -08002685 if (dst == src) {
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002686 selfOverlap = SkIRect::IntersectsNoEmptyCheck(dstRect, srcRect);
2687 }
2688
2689 if (!selfOverlap) {
egdanield803f272015-03-18 13:01:52 -07002690 GrGLuint dstFBO;
2691 GrGLuint srcFBO;
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002692 GrGLIRect dstVP;
2693 GrGLIRect srcVP;
egdanield803f272015-03-18 13:01:52 -07002694 dstFBO = this->bindSurfaceAsFBO(dst, GR_GL_DRAW_FRAMEBUFFER, &dstVP,
2695 kDst_TempFBOTarget);
2696 srcFBO = this->bindSurfaceAsFBO(src, GR_GL_READ_FRAMEBUFFER, &srcVP,
2697 kSrc_TempFBOTarget);
2698 // We modified the bound FBO
2699 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002700 GrGLIRect srcGLRect;
2701 GrGLIRect dstGLRect;
2702 srcGLRect.setRelativeTo(srcVP,
2703 srcRect.fLeft,
2704 srcRect.fTop,
2705 srcRect.width(),
2706 srcRect.height(),
2707 src->origin());
2708 dstGLRect.setRelativeTo(dstVP,
2709 dstRect.fLeft,
2710 dstRect.fTop,
2711 dstRect.width(),
2712 dstRect.height(),
2713 dst->origin());
2714
derekf8c8f71a2014-09-16 06:24:57 -07002715 // BlitFrameBuffer respects the scissor, so disable it.
joshualitt77b13072014-10-27 14:51:01 -07002716 this->disableScissor();
derekf8c8f71a2014-09-16 06:24:57 -07002717
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002718 GrGLint srcY0;
2719 GrGLint srcY1;
2720 // Does the blit need to y-mirror or not?
2721 if (src->origin() == dst->origin()) {
2722 srcY0 = srcGLRect.fBottom;
2723 srcY1 = srcGLRect.fBottom + srcGLRect.fHeight;
2724 } else {
2725 srcY0 = srcGLRect.fBottom + srcGLRect.fHeight;
2726 srcY1 = srcGLRect.fBottom;
2727 }
2728 GL_CALL(BlitFramebuffer(srcGLRect.fLeft,
2729 srcY0,
2730 srcGLRect.fLeft + srcGLRect.fWidth,
2731 srcY1,
2732 dstGLRect.fLeft,
2733 dstGLRect.fBottom,
2734 dstGLRect.fLeft + dstGLRect.fWidth,
2735 dstGLRect.fBottom + dstGLRect.fHeight,
2736 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
egdanield803f272015-03-18 13:01:52 -07002737 if (dstFBO) {
2738 this->unbindTextureFromFBO(GR_GL_DRAW_FRAMEBUFFER);
2739 }
2740 if (srcFBO) {
2741 this->unbindTextureFromFBO(GR_GL_READ_FRAMEBUFFER);
2742 }
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002743 copied = true;
2744 }
2745 }
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002746 return copied;
2747}
2748
bsalomon861e1032014-12-16 07:33:49 -08002749bool GrGLGpu::canCopySurface(const GrSurface* dst,
joshualitt9853cce2014-11-17 14:22:48 -08002750 const GrSurface* src,
joshualitta7024152014-11-03 14:16:35 -08002751 const SkIRect& srcRect,
2752 const SkIPoint& dstPoint) {
egdaniel0f5f9672015-02-03 11:10:51 -08002753 // This mirrors the logic in onCopySurface.
2754 if (can_copy_texsubimage(dst, src, this)) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002755 return true;
2756 }
egdaniel0f5f9672015-02-03 11:10:51 -08002757 if (can_blit_framebuffer(dst, src, this)) {
bsalomona2c23232014-11-25 07:41:12 -08002758 if (dst == src) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002759 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
2760 srcRect.width(), srcRect.height());
2761 if(!SkIRect::IntersectsNoEmptyCheck(dstRect, srcRect)) {
2762 return true;
2763 }
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002764 } else {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002765 return true;
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002766 }
2767 }
joshualitta7024152014-11-03 14:16:35 -08002768 return false;
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002769}
2770
bsalomon861e1032014-12-16 07:33:49 -08002771void GrGLGpu::didAddGpuTraceMarker() {
commit-bot@chromium.orga3baf3b2014-02-21 18:45:30 +00002772 if (this->caps()->gpuTracingSupport()) {
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +00002773 const GrTraceMarkerSet& markerArray = this->getActiveTraceMarkers();
egdanield78a1682014-07-09 10:41:26 -07002774 SkString markerString = markerArray.toStringLast();
egdanielbdad9c32015-03-05 12:19:17 -08002775#if GR_FORCE_GPU_TRACE_DEBUGGING
2776 SkDebugf("%s\n", markerString.c_str());
2777#else
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +00002778 GL_CALL(PushGroupMarker(0, markerString.c_str()));
egdanielbdad9c32015-03-05 12:19:17 -08002779#endif
commit-bot@chromium.orga3baf3b2014-02-21 18:45:30 +00002780 }
2781}
2782
bsalomon861e1032014-12-16 07:33:49 -08002783void GrGLGpu::didRemoveGpuTraceMarker() {
commit-bot@chromium.orga3baf3b2014-02-21 18:45:30 +00002784 if (this->caps()->gpuTracingSupport()) {
egdanielbdad9c32015-03-05 12:19:17 -08002785#if GR_FORCE_GPU_TRACE_DEBUGGING
2786 SkDebugf("Pop trace marker.\n");
2787#else
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +00002788 GL_CALL(PopGroupMarker());
egdanielbdad9c32015-03-05 12:19:17 -08002789#endif
commit-bot@chromium.orga3baf3b2014-02-21 18:45:30 +00002790 }
2791}
joshualitt79f8fae2014-10-28 17:59:26 -07002792
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00002793///////////////////////////////////////////////////////////////////////////////
2794
bsalomon861e1032014-12-16 07:33:49 -08002795GrGLAttribArrayState* GrGLGpu::HWGeometryState::bindArrayAndBuffersToDraw(
2796 GrGLGpu* gpu,
bsalomon@google.com6918d482013-03-07 19:09:11 +00002797 const GrGLVertexBuffer* vbuffer,
2798 const GrGLIndexBuffer* ibuffer) {
bsalomon49f085d2014-09-05 13:34:00 -07002799 SkASSERT(vbuffer);
robertphillips@google.com4f65a272013-03-26 19:40:46 +00002800 GrGLAttribArrayState* attribState;
2801
bsalomon@google.com6918d482013-03-07 19:09:11 +00002802 // We use a vertex array if we're on a core profile and the verts are in a VBO.
2803 if (gpu->glCaps().isCoreProfile() && !vbuffer->isCPUBacked()) {
commit-bot@chromium.org089a7802014-05-02 21:38:22 +00002804 if (NULL == fVBOVertexArray || fVBOVertexArray->wasDestroyed()) {
bsalomon@google.com6918d482013-03-07 19:09:11 +00002805 SkSafeUnref(fVBOVertexArray);
2806 GrGLuint arrayID;
2807 GR_GL_CALL(gpu->glInterface(), GenVertexArrays(1, &arrayID));
2808 int attrCount = gpu->glCaps().maxVertexAttributes();
2809 fVBOVertexArray = SkNEW_ARGS(GrGLVertexArray, (gpu, arrayID, attrCount));
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00002810 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00002811 attribState = fVBOVertexArray->bindWithIndexBuffer(ibuffer);
2812 } else {
bsalomon49f085d2014-09-05 13:34:00 -07002813 if (ibuffer) {
bsalomon@google.com6918d482013-03-07 19:09:11 +00002814 this->setIndexBufferIDOnDefaultVertexArray(gpu, ibuffer->bufferID());
2815 } else {
2816 this->setVertexArrayID(gpu, 0);
2817 }
2818 int attrCount = gpu->glCaps().maxVertexAttributes();
2819 if (fDefaultVertexArrayAttribState.count() != attrCount) {
2820 fDefaultVertexArrayAttribState.resize(attrCount);
2821 }
2822 attribState = &fDefaultVertexArrayAttribState;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00002823 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00002824 return attribState;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002825}