blob: ce02fe8ea982128d8a9d2ce80dafdd6f874c8d39 [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"
jvanverthcba99b82015-06-24 06:59:57 -070010#include "GrGLGLSL.h"
egdaniel8dc7c3a2015-04-16 11:22:42 -070011#include "GrGLStencilAttachment.h"
bsalomon37dd3312014-11-03 08:47:23 -080012#include "GrGLTextureRenderTarget.h"
bsalomon3582d3e2015-02-13 14:20:05 -080013#include "GrGpuResourcePriv.h"
egdaniel8dd688b2015-01-22 10:16:09 -080014#include "GrPipeline.h"
bsalomon6bc1b5f2015-02-23 09:06:38 -080015#include "GrRenderTargetPriv.h"
bsalomonafbf2d62014-09-30 12:18:44 -070016#include "GrSurfacePriv.h"
bsalomonafbf2d62014-09-30 12:18:44 -070017#include "GrTexturePriv.h"
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +000018#include "GrTypes.h"
bsalomoncb8979d2015-05-05 09:51:38 -070019#include "GrVertices.h"
bsalomon6df86402015-06-01 10:41:49 -070020#include "builders/GrGLShaderStringBuilder.h"
jvanverthcba99b82015-06-24 06:59:57 -070021#include "glsl/GrGLSLCaps.h"
commit-bot@chromium.org32184d82013-10-09 15:14:18 +000022#include "SkStrokeRec.h"
bsalomon@google.com3582bf92011-06-30 21:32:31 +000023#include "SkTemplates.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000024
bsalomon@google.com0b77d682011-08-19 13:28:54 +000025#define GL_CALL(X) GR_GL_CALL(this->glInterface(), X)
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +000026#define GL_CALL_RET(RET, X) GR_GL_CALL_RET(this->glInterface(), RET, X)
bsalomon@google.com0b77d682011-08-19 13:28:54 +000027
reed@google.comac10a2d2010-12-22 21:39:39 +000028#define SKIP_CACHE_CHECK true
29
bsalomon@google.com4f3c2532012-01-19 16:16:52 +000030#if GR_GL_CHECK_ALLOC_WITH_GET_ERROR
31 #define CLEAR_ERROR_BEFORE_ALLOC(iface) GrGLClearErr(iface)
32 #define GL_ALLOC_CALL(iface, call) GR_GL_CALL_NOERRCHECK(iface, call)
33 #define CHECK_ALLOC_ERROR(iface) GR_GL_GET_ERROR(iface)
rmistry@google.comfbfcd562012-08-23 18:09:54 +000034#else
bsalomon@google.com4f3c2532012-01-19 16:16:52 +000035 #define CLEAR_ERROR_BEFORE_ALLOC(iface)
36 #define GL_ALLOC_CALL(iface, call) GR_GL_CALL(iface, call)
37 #define CHECK_ALLOC_ERROR(iface) GR_GL_NO_ERROR
38#endif
39
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +000040
41///////////////////////////////////////////////////////////////////////////////
42
cdaltonb85a0aa2014-07-21 15:32:44 -070043
cdalton8917d622015-05-06 13:40:21 -070044static const GrGLenum gXfermodeEquation2Blend[] = {
45 // Basic OpenGL blend equations.
46 GR_GL_FUNC_ADD,
47 GR_GL_FUNC_SUBTRACT,
48 GR_GL_FUNC_REVERSE_SUBTRACT,
49
50 // GL_KHR_blend_equation_advanced.
51 GR_GL_SCREEN,
52 GR_GL_OVERLAY,
53 GR_GL_DARKEN,
54 GR_GL_LIGHTEN,
55 GR_GL_COLORDODGE,
56 GR_GL_COLORBURN,
57 GR_GL_HARDLIGHT,
58 GR_GL_SOFTLIGHT,
59 GR_GL_DIFFERENCE,
60 GR_GL_EXCLUSION,
61 GR_GL_MULTIPLY,
62 GR_GL_HSL_HUE,
63 GR_GL_HSL_SATURATION,
64 GR_GL_HSL_COLOR,
65 GR_GL_HSL_LUMINOSITY
66};
67GR_STATIC_ASSERT(0 == kAdd_GrBlendEquation);
68GR_STATIC_ASSERT(1 == kSubtract_GrBlendEquation);
69GR_STATIC_ASSERT(2 == kReverseSubtract_GrBlendEquation);
70GR_STATIC_ASSERT(3 == kScreen_GrBlendEquation);
71GR_STATIC_ASSERT(4 == kOverlay_GrBlendEquation);
72GR_STATIC_ASSERT(5 == kDarken_GrBlendEquation);
73GR_STATIC_ASSERT(6 == kLighten_GrBlendEquation);
74GR_STATIC_ASSERT(7 == kColorDodge_GrBlendEquation);
75GR_STATIC_ASSERT(8 == kColorBurn_GrBlendEquation);
76GR_STATIC_ASSERT(9 == kHardLight_GrBlendEquation);
77GR_STATIC_ASSERT(10 == kSoftLight_GrBlendEquation);
78GR_STATIC_ASSERT(11 == kDifference_GrBlendEquation);
79GR_STATIC_ASSERT(12 == kExclusion_GrBlendEquation);
80GR_STATIC_ASSERT(13 == kMultiply_GrBlendEquation);
81GR_STATIC_ASSERT(14 == kHSLHue_GrBlendEquation);
82GR_STATIC_ASSERT(15 == kHSLSaturation_GrBlendEquation);
83GR_STATIC_ASSERT(16 == kHSLColor_GrBlendEquation);
84GR_STATIC_ASSERT(17 == kHSLLuminosity_GrBlendEquation);
bsalomonf7cc8772015-05-11 11:21:14 -070085GR_STATIC_ASSERT(SK_ARRAY_COUNT(gXfermodeEquation2Blend) == kGrBlendEquationCnt);
cdalton8917d622015-05-06 13:40:21 -070086
twiz@google.com0f31ca72011-03-18 17:38:11 +000087static const GrGLenum gXfermodeCoeff2Blend[] = {
88 GR_GL_ZERO,
89 GR_GL_ONE,
90 GR_GL_SRC_COLOR,
91 GR_GL_ONE_MINUS_SRC_COLOR,
92 GR_GL_DST_COLOR,
93 GR_GL_ONE_MINUS_DST_COLOR,
94 GR_GL_SRC_ALPHA,
95 GR_GL_ONE_MINUS_SRC_ALPHA,
96 GR_GL_DST_ALPHA,
97 GR_GL_ONE_MINUS_DST_ALPHA,
98 GR_GL_CONSTANT_COLOR,
99 GR_GL_ONE_MINUS_CONSTANT_COLOR,
100 GR_GL_CONSTANT_ALPHA,
101 GR_GL_ONE_MINUS_CONSTANT_ALPHA,
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000102
103 // extended blend coeffs
104 GR_GL_SRC1_COLOR,
105 GR_GL_ONE_MINUS_SRC1_COLOR,
106 GR_GL_SRC1_ALPHA,
107 GR_GL_ONE_MINUS_SRC1_ALPHA,
reed@google.comac10a2d2010-12-22 21:39:39 +0000108};
109
bsalomon861e1032014-12-16 07:33:49 -0800110bool GrGLGpu::BlendCoeffReferencesConstant(GrBlendCoeff coeff) {
bsalomon@google.com080773c2011-03-15 19:09:25 +0000111 static const bool gCoeffReferencesBlendConst[] = {
112 false,
113 false,
114 false,
115 false,
116 false,
117 false,
118 false,
119 false,
120 false,
121 false,
122 true,
123 true,
124 true,
125 true,
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000126
127 // extended blend coeffs
128 false,
129 false,
130 false,
131 false,
bsalomon@google.com080773c2011-03-15 19:09:25 +0000132 };
133 return gCoeffReferencesBlendConst[coeff];
bsalomonf7cc8772015-05-11 11:21:14 -0700134 GR_STATIC_ASSERT(kGrBlendCoeffCnt == SK_ARRAY_COUNT(gCoeffReferencesBlendConst));
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000135
bsalomon@google.com47059542012-06-06 20:51:20 +0000136 GR_STATIC_ASSERT(0 == kZero_GrBlendCoeff);
137 GR_STATIC_ASSERT(1 == kOne_GrBlendCoeff);
138 GR_STATIC_ASSERT(2 == kSC_GrBlendCoeff);
139 GR_STATIC_ASSERT(3 == kISC_GrBlendCoeff);
140 GR_STATIC_ASSERT(4 == kDC_GrBlendCoeff);
141 GR_STATIC_ASSERT(5 == kIDC_GrBlendCoeff);
142 GR_STATIC_ASSERT(6 == kSA_GrBlendCoeff);
143 GR_STATIC_ASSERT(7 == kISA_GrBlendCoeff);
144 GR_STATIC_ASSERT(8 == kDA_GrBlendCoeff);
145 GR_STATIC_ASSERT(9 == kIDA_GrBlendCoeff);
146 GR_STATIC_ASSERT(10 == kConstC_GrBlendCoeff);
147 GR_STATIC_ASSERT(11 == kIConstC_GrBlendCoeff);
148 GR_STATIC_ASSERT(12 == kConstA_GrBlendCoeff);
149 GR_STATIC_ASSERT(13 == kIConstA_GrBlendCoeff);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000150
bsalomon@google.com47059542012-06-06 20:51:20 +0000151 GR_STATIC_ASSERT(14 == kS2C_GrBlendCoeff);
152 GR_STATIC_ASSERT(15 == kIS2C_GrBlendCoeff);
153 GR_STATIC_ASSERT(16 == kS2A_GrBlendCoeff);
154 GR_STATIC_ASSERT(17 == kIS2A_GrBlendCoeff);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000155
156 // assertion for gXfermodeCoeff2Blend have to be in GrGpu scope
bsalomonf7cc8772015-05-11 11:21:14 -0700157 GR_STATIC_ASSERT(kGrBlendCoeffCnt == SK_ARRAY_COUNT(gXfermodeCoeff2Blend));
bsalomon@google.com080773c2011-03-15 19:09:25 +0000158}
159
reed@google.comac10a2d2010-12-22 21:39:39 +0000160///////////////////////////////////////////////////////////////////////////////
161
egdanielff1d5472015-09-10 08:37:20 -0700162// Used in the map of pixel configs to stencil format indices. This value is used to
163// indicate that a stencil format has not yet been set for the given config.
164static const int kUnknownStencilIndex = -1;
165// This value is used as the stencil index when no stencil configs are supported with the
166// given pixel config.
167static const int kUnsupportedStencilIndex = -2;
168
169///////////////////////////////////////////////////////////////////////////////
170
bsalomon682c2692015-05-22 14:01:46 -0700171GrGpu* GrGLGpu::Create(GrBackendContext backendContext, const GrContextOptions& options,
172 GrContext* context) {
bsalomon424cc262015-05-22 10:37:30 -0700173 SkAutoTUnref<const GrGLInterface> glInterface(
174 reinterpret_cast<const GrGLInterface*>(backendContext));
175 if (!glInterface) {
176 glInterface.reset(GrGLDefaultInterface());
177 } else {
178 glInterface->ref();
179 }
180 if (!glInterface) {
halcanary96fcdcc2015-08-27 07:41:13 -0700181 return nullptr;
bsalomon424cc262015-05-22 10:37:30 -0700182 }
bsalomon682c2692015-05-22 14:01:46 -0700183 GrGLContext* glContext = GrGLContext::Create(glInterface, options);
bsalomon424cc262015-05-22 10:37:30 -0700184 if (glContext) {
halcanary385fe4d2015-08-26 13:07:48 -0700185 return new GrGLGpu(glContext, context);
bsalomon424cc262015-05-22 10:37:30 -0700186 }
halcanary96fcdcc2015-08-27 07:41:13 -0700187 return nullptr;
bsalomon424cc262015-05-22 10:37:30 -0700188}
189
rileya@google.come38160c2012-07-03 18:03:04 +0000190static bool gPrintStartupSpew;
bsalomon@google.com42ab7ea2011-01-19 17:19:40 +0000191
bsalomon424cc262015-05-22 10:37:30 -0700192GrGLGpu::GrGLGpu(GrGLContext* ctx, GrContext* context)
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000193 : GrGpu(context)
robertphillips@google.com6177e692013-02-28 20:16:25 +0000194 , fGLContext(ctx) {
bsalomon424cc262015-05-22 10:37:30 -0700195 SkASSERT(ctx);
196 fCaps.reset(SkRef(ctx->caps()));
bsalomon@google.combcce8922013-03-25 15:38:39 +0000197
bsalomon1c63bf62014-07-22 13:09:46 -0700198 fHWBoundTextureUniqueIDs.reset(this->glCaps().maxFragmentTextureUnits());
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +0000199
bsalomon424cc262015-05-22 10:37:30 -0700200 GrGLClearErr(this->glInterface());
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000201 if (gPrintStartupSpew) {
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000202 const GrGLubyte* vendor;
203 const GrGLubyte* renderer;
204 const GrGLubyte* version;
205 GL_CALL_RET(vendor, GetString(GR_GL_VENDOR));
206 GL_CALL_RET(renderer, GetString(GR_GL_RENDERER));
207 GL_CALL_RET(version, GetString(GR_GL_VERSION));
bsalomon861e1032014-12-16 07:33:49 -0800208 SkDebugf("------------------------- create GrGLGpu %p --------------\n",
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000209 this);
tfarina38406c82014-10-31 07:11:12 -0700210 SkDebugf("------ VENDOR %s\n", vendor);
211 SkDebugf("------ RENDERER %s\n", renderer);
212 SkDebugf("------ VERSION %s\n", version);
213 SkDebugf("------ EXTENSIONS\n");
bsalomon424cc262015-05-22 10:37:30 -0700214 this->glContext().extensions().print();
tfarina38406c82014-10-31 07:11:12 -0700215 SkDebugf("\n");
kkinnunen297aaf92015-02-19 06:32:12 -0800216 SkDebugf("%s", this->glCaps().dump().c_str());
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000217 }
218
halcanary385fe4d2015-08-26 13:07:48 -0700219 fProgramCache = new ProgramCache(this);
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000220
joshualitt2dd1ae02014-12-03 06:24:10 -0800221 SkASSERT(this->glCaps().maxVertexAttributes() >= GrGeometryProcessor::kMaxVertexAttribs);
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000222
egdanielff1d5472015-09-10 08:37:20 -0700223 for (int i = 0; i < kGrPixelConfigCnt; ++i) {
224 fPixelConfigToStencilIndex[i] = kUnknownStencilIndex;
225 }
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000226 fHWProgramID = 0;
egdanield803f272015-03-18 13:01:52 -0700227 fTempSrcFBOID = 0;
228 fTempDstFBOID = 0;
229 fStencilClearFBOID = 0;
cdaltonc7103a12014-08-11 14:05:05 -0700230
jvanverthe9c0fc62015-04-29 11:18:05 -0700231 if (this->glCaps().shaderCaps()->pathRenderingSupport()) {
kkinnunen5b653572014-08-20 04:13:27 -0700232 fPathRendering.reset(new GrGLPathRendering(this));
cdaltonc7103a12014-08-11 14:05:05 -0700233 }
bsalomon6df86402015-06-01 10:41:49 -0700234
235 this->createCopyProgram();
reed@google.comac10a2d2010-12-22 21:39:39 +0000236}
237
bsalomon861e1032014-12-16 07:33:49 -0800238GrGLGpu::~GrGLGpu() {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000239 if (0 != fHWProgramID) {
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000240 // detach the current program so there is no confusion on OpenGL's part
241 // that we want it to be deleted
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000242 GL_CALL(UseProgram(0));
243 }
244
egdanield803f272015-03-18 13:01:52 -0700245 if (0 != fTempSrcFBOID) {
246 GL_CALL(DeleteFramebuffers(1, &fTempSrcFBOID));
egdaniel0f5f9672015-02-03 11:10:51 -0800247 }
egdanield803f272015-03-18 13:01:52 -0700248 if (0 != fTempDstFBOID) {
249 GL_CALL(DeleteFramebuffers(1, &fTempDstFBOID));
egdaniel0f5f9672015-02-03 11:10:51 -0800250 }
egdanield803f272015-03-18 13:01:52 -0700251 if (0 != fStencilClearFBOID) {
252 GL_CALL(DeleteFramebuffers(1, &fStencilClearFBOID));
bsalomondd3143b2015-02-23 09:27:45 -0800253 }
egdaniel0f5f9672015-02-03 11:10:51 -0800254
bsalomon6df86402015-06-01 10:41:49 -0700255 if (0 != fCopyProgram.fArrayBuffer) {
256 GL_CALL(DeleteBuffers(1, &fCopyProgram.fArrayBuffer));
257 }
258
259 if (0 != fCopyProgram.fProgram) {
260 GL_CALL(DeleteProgram(fCopyProgram.fProgram));
261 }
262
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000263 delete fProgramCache;
bsalomonc8dc1f72014-08-21 13:02:13 -0700264}
265
bsalomon861e1032014-12-16 07:33:49 -0800266void GrGLGpu::contextAbandoned() {
robertphillipse3371302014-09-17 06:01:06 -0700267 INHERITED::contextAbandoned();
bsalomonc8dc1f72014-08-21 13:02:13 -0700268 fProgramCache->abandon();
269 fHWProgramID = 0;
egdanield803f272015-03-18 13:01:52 -0700270 fTempSrcFBOID = 0;
271 fTempDstFBOID = 0;
272 fStencilClearFBOID = 0;
bsalomon6df86402015-06-01 10:41:49 -0700273 fCopyProgram.fArrayBuffer = 0;
274 fCopyProgram.fProgram = 0;
jvanverthe9c0fc62015-04-29 11:18:05 -0700275 if (this->glCaps().shaderCaps()->pathRenderingSupport()) {
bsalomonc8dc1f72014-08-21 13:02:13 -0700276 this->glPathRendering()->abandonGpuResources();
277 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000278}
279
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000280///////////////////////////////////////////////////////////////////////////////
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000281
bsalomon861e1032014-12-16 07:33:49 -0800282void GrGLGpu::onResetContext(uint32_t resetBits) {
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000283 // we don't use the zb at all
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000284 if (resetBits & kMisc_GrGLBackendState) {
285 GL_CALL(Disable(GR_GL_DEPTH_TEST));
286 GL_CALL(DepthMask(GR_GL_FALSE));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000287
egdaniel8dd688b2015-01-22 10:16:09 -0800288 fHWDrawFace = GrPipelineBuilder::kInvalid_DrawFace;
reed@google.comac10a2d2010-12-22 21:39:39 +0000289
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +0000290 if (kGL_GrGLStandard == this->glStandard()) {
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000291 // Desktop-only state that we never change
292 if (!this->glCaps().isCoreProfile()) {
293 GL_CALL(Disable(GR_GL_POINT_SMOOTH));
294 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
295 GL_CALL(Disable(GR_GL_POLYGON_SMOOTH));
296 GL_CALL(Disable(GR_GL_POLYGON_STIPPLE));
297 GL_CALL(Disable(GR_GL_COLOR_LOGIC_OP));
298 GL_CALL(Disable(GR_GL_INDEX_LOGIC_OP));
299 }
300 // The windows NVIDIA driver has GL_ARB_imaging in the extension string when using a
301 // core profile. This seems like a bug since the core spec removes any mention of
302 // GL_ARB_imaging.
303 if (this->glCaps().imagingSupport() && !this->glCaps().isCoreProfile()) {
304 GL_CALL(Disable(GR_GL_COLOR_TABLE));
305 }
306 GL_CALL(Disable(GR_GL_POLYGON_OFFSET_FILL));
307 // Since ES doesn't support glPointSize at all we always use the VS to
308 // set the point size
309 GL_CALL(Enable(GR_GL_VERTEX_PROGRAM_POINT_SIZE));
310
311 // We should set glPolygonMode(FRONT_AND_BACK,FILL) here, too. It isn't
312 // currently part of our gl interface. There are probably others as
313 // well.
bsalomon@google.com2b1b8c02013-02-28 22:06:02 +0000314 }
joshualitt58162332014-08-01 06:44:53 -0700315
316 if (kGLES_GrGLStandard == this->glStandard() &&
bsalomon424cc262015-05-22 10:37:30 -0700317 this->hasExtension("GL_ARM_shader_framebuffer_fetch")) {
joshualitt58162332014-08-01 06:44:53 -0700318 // The arm extension requires specifically enabling MSAA fetching per sample.
319 // On some devices this may have a perf hit. Also multiple render targets are disabled
320 GL_CALL(Enable(GR_GL_FETCH_PER_SAMPLE_ARM));
321 }
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000322 fHWWriteToColor = kUnknown_TriState;
323 // we only ever use lines in hairline mode
324 GL_CALL(LineWidth(1));
bsalomonaca31fe2015-09-22 11:38:46 -0700325 GL_CALL(Disable(GR_GL_DITHER));
bsalomon@google.comcad107b2013-06-28 14:32:08 +0000326 }
edisonn@google.comba669992013-06-28 16:03:21 +0000327
egdanielb414f252014-07-29 13:15:47 -0700328 if (resetBits & kMSAAEnable_GrGLBackendState) {
329 fMSAAEnabled = kUnknown_TriState;
vbuzinovdded6962015-06-12 08:59:45 -0700330
331 // In mixed samples mode coverage modulation allows the coverage to be converted to
332 // "opacity", which can then be blended into the color buffer to accomplish antialiasing.
333 // Enable coverage modulation suitable for premultiplied alpha colors.
334 // This state has no effect when not rendering to a mixed sampled target.
335 if (this->glCaps().shaderCaps()->mixedSamplesSupport()) {
336 GL_CALL(CoverageModulation(GR_GL_RGBA));
337 }
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000338 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000339
commit-bot@chromium.org46fbfe02013-08-30 15:52:12 +0000340 fHWActiveTextureUnitIdx = -1; // invalid
341
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000342 if (resetBits & kTextureBinding_GrGLBackendState) {
bsalomon1c63bf62014-07-22 13:09:46 -0700343 for (int s = 0; s < fHWBoundTextureUniqueIDs.count(); ++s) {
344 fHWBoundTextureUniqueIDs[s] = SK_InvalidUniqueID;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000345 }
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000346 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000347
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000348 if (resetBits & kBlend_GrGLBackendState) {
349 fHWBlendState.invalidate();
350 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000351
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000352 if (resetBits & kView_GrGLBackendState) {
353 fHWScissorSettings.invalidate();
354 fHWViewport.invalidate();
355 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000356
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000357 if (resetBits & kStencil_GrGLBackendState) {
358 fHWStencilSettings.invalidate();
359 fHWStencilTestEnabled = kUnknown_TriState;
360 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000361
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000362 // Vertex
363 if (resetBits & kVertex_GrGLBackendState) {
364 fHWGeometryState.invalidate();
365 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000366
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000367 if (resetBits & kRenderTarget_GrGLBackendState) {
egdanield803f272015-03-18 13:01:52 -0700368 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
bsalomon16921ec2015-07-30 15:34:56 -0700369 fHWSRGBFramebuffer = kUnknown_TriState;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000370 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000371
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000372 if (resetBits & kPathRendering_GrGLBackendState) {
jvanverthe9c0fc62015-04-29 11:18:05 -0700373 if (this->caps()->shaderCaps()->pathRenderingSupport()) {
kkinnunenccdaa042014-08-20 01:36:23 -0700374 this->glPathRendering()->resetContext();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000375 }
bsalomon@google.com05a718c2012-06-29 14:01:53 +0000376 }
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000377
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000378 // we assume these values
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000379 if (resetBits & kPixelStore_GrGLBackendState) {
380 if (this->glCaps().unpackRowLengthSupport()) {
381 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
382 }
383 if (this->glCaps().packRowLengthSupport()) {
384 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
385 }
386 if (this->glCaps().unpackFlipYSupport()) {
387 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
388 }
389 if (this->glCaps().packFlipYSupport()) {
390 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, GR_GL_FALSE));
391 }
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000392 }
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000393
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000394 if (resetBits & kProgram_GrGLBackendState) {
395 fHWProgramID = 0;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000396 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000397}
398
egdanielcf614fd2015-04-22 13:58:58 -0700399static GrSurfaceOrigin resolve_origin(GrSurfaceOrigin origin, bool renderTarget) {
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000400 // By default, GrRenderTargets are GL's normal orientation so that they
401 // can be drawn to by the outside world without the client having
402 // to render upside down.
403 if (kDefault_GrSurfaceOrigin == origin) {
404 return renderTarget ? kBottomLeft_GrSurfaceOrigin : kTopLeft_GrSurfaceOrigin;
405 } else {
406 return origin;
407 }
408}
409
bsalomon6dc6f5f2015-06-18 09:12:16 -0700410GrTexture* GrGLGpu::onWrapBackendTexture(const GrBackendTextureDesc& desc,
411 GrWrapOwnership ownership) {
halcanary96fcdcc2015-08-27 07:41:13 -0700412 if (!this->configToGLFormats(desc.fConfig, false, nullptr, nullptr, nullptr)) {
413 return nullptr;
bsalomon@google.come269f212011-11-07 13:29:52 +0000414 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000415
robertphillips@google.comb72e5d32012-10-30 15:18:10 +0000416 if (0 == desc.fTextureHandle) {
halcanary96fcdcc2015-08-27 07:41:13 -0700417 return nullptr;
robertphillips@google.comb72e5d32012-10-30 15:18:10 +0000418 }
419
bsalomon@google.combcce8922013-03-25 15:38:39 +0000420 int maxSize = this->caps()->maxTextureSize();
robertphillips@google.comb72e5d32012-10-30 15:18:10 +0000421 if (desc.fWidth > maxSize || desc.fHeight > maxSize) {
halcanary96fcdcc2015-08-27 07:41:13 -0700422 return nullptr;
robertphillips@google.comb72e5d32012-10-30 15:18:10 +0000423 }
424
bsalomonb15b4c12014-10-29 12:41:57 -0700425 GrGLTexture::IDDesc idDesc;
426 GrSurfaceDesc surfDesc;
427
428 idDesc.fTextureID = static_cast<GrGLuint>(desc.fTextureHandle);
bsalomon10528f12015-10-14 12:54:52 -0700429 // We only support GL_TEXTURE_2D at the moment.
430 idDesc.fTarget = GR_GL_TEXTURE_2D;
431
bsalomon6dc6f5f2015-06-18 09:12:16 -0700432 switch (ownership) {
433 case kAdopt_GrWrapOwnership:
434 idDesc.fLifeCycle = GrGpuResource::kAdopted_LifeCycle;
435 break;
436 case kBorrow_GrWrapOwnership:
437 idDesc.fLifeCycle = GrGpuResource::kBorrowed_LifeCycle;
438 break;
439 }
bsalomonb15b4c12014-10-29 12:41:57 -0700440
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000441 // next line relies on GrBackendTextureDesc's flags matching GrTexture's
bsalomonb15b4c12014-10-29 12:41:57 -0700442 surfDesc.fFlags = (GrSurfaceFlags) desc.fFlags;
443 surfDesc.fWidth = desc.fWidth;
444 surfDesc.fHeight = desc.fHeight;
445 surfDesc.fConfig = desc.fConfig;
senorblanco94e50102015-04-06 09:42:57 -0700446 surfDesc.fSampleCnt = SkTMin(desc.fSampleCnt, this->caps()->maxSampleCount());
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000447 bool renderTarget = SkToBool(desc.fFlags & kRenderTarget_GrBackendTextureFlag);
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000448 // FIXME: this should be calling resolve_origin(), but Chrome code is currently
449 // assuming the old behaviour, which is that backend textures are always
450 // BottomLeft, even for non-RT's. Once Chrome is fixed, change this to:
451 // glTexDesc.fOrigin = resolve_origin(desc.fOrigin, renderTarget);
452 if (kDefault_GrSurfaceOrigin == desc.fOrigin) {
bsalomonb15b4c12014-10-29 12:41:57 -0700453 surfDesc.fOrigin = kBottomLeft_GrSurfaceOrigin;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000454 } else {
bsalomonb15b4c12014-10-29 12:41:57 -0700455 surfDesc.fOrigin = desc.fOrigin;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000456 }
bsalomon@google.come269f212011-11-07 13:29:52 +0000457
halcanary96fcdcc2015-08-27 07:41:13 -0700458 GrGLTexture* texture = nullptr;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000459 if (renderTarget) {
bsalomonb15b4c12014-10-29 12:41:57 -0700460 GrGLRenderTarget::IDDesc rtIDDesc;
egdanielb0e1be22015-04-22 13:27:39 -0700461 if (!this->createRenderTargetObjects(surfDesc, GrGpuResource::kUncached_LifeCycle,
bsalomon10528f12015-10-14 12:54:52 -0700462 idDesc.fTextureID, idDesc.fTarget, &rtIDDesc)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700463 return nullptr;
bsalomon@google.come269f212011-11-07 13:29:52 +0000464 }
halcanary385fe4d2015-08-26 13:07:48 -0700465 texture = new GrGLTextureRenderTarget(this, surfDesc, idDesc, rtIDDesc);
bsalomon@google.come269f212011-11-07 13:29:52 +0000466 } else {
halcanary385fe4d2015-08-26 13:07:48 -0700467 texture = new GrGLTexture(this, surfDesc, idDesc);
bsalomon@google.come269f212011-11-07 13:29:52 +0000468 }
halcanary96fcdcc2015-08-27 07:41:13 -0700469 if (nullptr == texture) {
470 return nullptr;
bsalomon@google.come269f212011-11-07 13:29:52 +0000471 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000472
bsalomon@google.come269f212011-11-07 13:29:52 +0000473 return texture;
474}
475
bsalomon6dc6f5f2015-06-18 09:12:16 -0700476GrRenderTarget* GrGLGpu::onWrapBackendRenderTarget(const GrBackendRenderTargetDesc& wrapDesc,
477 GrWrapOwnership ownership) {
bsalomonb15b4c12014-10-29 12:41:57 -0700478 GrGLRenderTarget::IDDesc idDesc;
egdanield803f272015-03-18 13:01:52 -0700479 idDesc.fRTFBOID = static_cast<GrGLuint>(wrapDesc.fRenderTargetHandle);
bsalomonb15b4c12014-10-29 12:41:57 -0700480 idDesc.fMSColorRenderbufferID = 0;
egdanield803f272015-03-18 13:01:52 -0700481 idDesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
bsalomon6dc6f5f2015-06-18 09:12:16 -0700482 switch (ownership) {
483 case kAdopt_GrWrapOwnership:
484 idDesc.fLifeCycle = GrGpuResource::kAdopted_LifeCycle;
485 break;
486 case kBorrow_GrWrapOwnership:
487 idDesc.fLifeCycle = GrGpuResource::kBorrowed_LifeCycle;
488 break;
489 }
senorblancod7395d82015-06-18 13:26:52 -0700490 idDesc.fSampleConfig = GrRenderTarget::kUnified_SampleConfig;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000491
bsalomonb15b4c12014-10-29 12:41:57 -0700492 GrSurfaceDesc desc;
493 desc.fConfig = wrapDesc.fConfig;
senorblanco4b013292015-08-19 09:10:28 -0700494 desc.fFlags = kCheckAllocation_GrSurfaceFlag | kRenderTarget_GrSurfaceFlag;
bsalomonb15b4c12014-10-29 12:41:57 -0700495 desc.fWidth = wrapDesc.fWidth;
496 desc.fHeight = wrapDesc.fHeight;
senorblanco94e50102015-04-06 09:42:57 -0700497 desc.fSampleCnt = SkTMin(wrapDesc.fSampleCnt, this->caps()->maxSampleCount());
bsalomonb15b4c12014-10-29 12:41:57 -0700498 desc.fOrigin = resolve_origin(wrapDesc.fOrigin, true);
499
egdanielec00d942015-09-14 12:56:10 -0700500 return GrGLRenderTarget::CreateWrapped(this, desc, idDesc, wrapDesc.fStencilBits);
bsalomon@google.come269f212011-11-07 13:29:52 +0000501}
502
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000503////////////////////////////////////////////////////////////////////////////////
bsalomonf0674512015-07-28 13:26:15 -0700504bool GrGLGpu::onGetWritePixelsInfo(GrSurface* dstSurface, int width, int height,
505 size_t rowBytes, GrPixelConfig srcConfig,
506 DrawPreference* drawPreference,
507 WritePixelTempDrawInfo* tempDrawInfo) {
508 if (kIndex_8_GrPixelConfig == srcConfig || GrPixelConfigIsCompressed(dstSurface->config())) {
509 return false;
510 }
511
bsalomon6cb3cbe2015-07-30 07:34:27 -0700512 // This subclass only allows writes to textures. If the dst is not a texture we have to draw
513 // into it. We could use glDrawPixels on GLs that have it, but we don't today.
514 if (!dstSurface->asTexture()) {
515 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
516 }
517
bsalomon16921ec2015-07-30 15:34:56 -0700518 if (GrPixelConfigIsSRGB(dstSurface->config()) != GrPixelConfigIsSRGB(srcConfig)) {
519 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
520 }
521
bsalomonf0674512015-07-28 13:26:15 -0700522 tempDrawInfo->fSwapRAndB = false;
523
524 // These settings we will always want if a temp draw is performed. Initially set the config
525 // to srcConfig, though that may be modified if we decide to do a R/G swap.
526 tempDrawInfo->fTempSurfaceDesc.fFlags = kNone_GrSurfaceFlags;
527 tempDrawInfo->fTempSurfaceDesc.fConfig = srcConfig;
528 tempDrawInfo->fTempSurfaceDesc.fWidth = width;
529 tempDrawInfo->fTempSurfaceDesc.fHeight = height;
530 tempDrawInfo->fTempSurfaceDesc.fSampleCnt = 0;
531 tempDrawInfo->fTempSurfaceDesc.fOrigin = kTopLeft_GrSurfaceOrigin; // no CPU y-flip for TL.
532
533 bool configsAreRBSwaps = GrPixelConfigSwapRAndB(srcConfig) == dstSurface->config();
534
535 if (configsAreRBSwaps) {
536 if (!this->caps()->isConfigTexturable(srcConfig)) {
537 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
538 tempDrawInfo->fTempSurfaceDesc.fConfig = dstSurface->config();
539 tempDrawInfo->fSwapRAndB = true;
bsalomon88c7b982015-07-31 11:20:16 -0700540 } else if (this->glCaps().rgba8888PixelsOpsAreSlow() &&
541 kRGBA_8888_GrPixelConfig == srcConfig) {
bsalomonf0674512015-07-28 13:26:15 -0700542 ElevateDrawPreference(drawPreference, kGpuPrefersDraw_DrawPreference);
543 tempDrawInfo->fTempSurfaceDesc.fConfig = dstSurface->config();
544 tempDrawInfo->fSwapRAndB = true;
545 } else if (kGLES_GrGLStandard == this->glStandard() &&
546 this->glCaps().bgraIsInternalFormat()) {
547 // The internal format and external formats must match texture uploads so we can't
548 // swizzle while uploading when BGRA is a distinct internal format.
549 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
550 tempDrawInfo->fTempSurfaceDesc.fConfig = dstSurface->config();
551 tempDrawInfo->fSwapRAndB = true;
552 }
553 }
554
555 if (!this->glCaps().unpackFlipYSupport() &&
556 kBottomLeft_GrSurfaceOrigin == dstSurface->origin()) {
557 ElevateDrawPreference(drawPreference, kGpuPrefersDraw_DrawPreference);
558 }
559
560 return true;
561}
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000562
bsalomon6cb3cbe2015-07-30 07:34:27 -0700563bool GrGLGpu::onWritePixels(GrSurface* surface,
564 int left, int top, int width, int height,
565 GrPixelConfig config, const void* buffer,
566 size_t rowBytes) {
bsalomon6cb3cbe2015-07-30 07:34:27 -0700567 GrGLTexture* glTex = static_cast<GrGLTexture*>(surface->asTexture());
568 if (!glTex) {
569 return false;
570 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000571
bsalomon16921ec2015-07-30 15:34:56 -0700572 // OpenGL doesn't do sRGB <-> linear conversions when reading and writing pixels.
573 if (GrPixelConfigIsSRGB(surface->config()) != GrPixelConfigIsSRGB(config)) {
574 return false;
575 }
576
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +0000577 this->setScratchTextureUnit();
bsalomon10528f12015-10-14 12:54:52 -0700578 GL_CALL(BindTexture(glTex->target(), glTex->textureID()));
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000579
krajcevski145d48c2014-06-11 16:07:50 -0700580 bool success = false;
bsalomonb15b4c12014-10-29 12:41:57 -0700581 if (GrPixelConfigIsCompressed(glTex->desc().fConfig)) {
bsalomon861e1032014-12-16 07:33:49 -0800582 // We check that config == desc.fConfig in GrGLGpu::canWriteTexturePixels()
bsalomonb15b4c12014-10-29 12:41:57 -0700583 SkASSERT(config == glTex->desc().fConfig);
bsalomon10528f12015-10-14 12:54:52 -0700584 success = this->uploadCompressedTexData(glTex->desc(), glTex->target(), buffer, false, left,
585 top, width, height);
krajcevski145d48c2014-06-11 16:07:50 -0700586 } else {
bsalomon10528f12015-10-14 12:54:52 -0700587 success = this->uploadTexData(glTex->desc(), glTex->target(), false, left, top, width,
588 height, config, buffer, rowBytes);
krajcevski145d48c2014-06-11 16:07:50 -0700589 }
590
591 if (success) {
bsalomon6cb3cbe2015-07-30 07:34:27 -0700592 glTex->texturePriv().dirtyMipMaps(true);
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +0000593 return true;
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +0000594 }
krajcevski145d48c2014-06-11 16:07:50 -0700595
596 return false;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000597}
598
bsalomonb15b4c12014-10-29 12:41:57 -0700599static inline GrGLenum check_alloc_error(const GrSurfaceDesc& desc,
600 const GrGLInterface* interface) {
bsalomonf2703d82014-10-28 14:33:06 -0700601 if (SkToBool(desc.fFlags & kCheckAllocation_GrSurfaceFlag)) {
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000602 return GR_GL_GET_ERROR(interface);
603 } else {
604 return CHECK_ALLOC_ERROR(interface);
605 }
606}
607
bsalomon861e1032014-12-16 07:33:49 -0800608bool GrGLGpu::uploadTexData(const GrSurfaceDesc& desc,
bsalomon10528f12015-10-14 12:54:52 -0700609 GrGLenum target,
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000610 bool isNewTexture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000611 int left, int top, int width, int height,
612 GrPixelConfig dataConfig,
613 const void* data,
614 size_t rowBytes) {
bsalomon49f085d2014-09-05 13:34:00 -0700615 SkASSERT(data || isNewTexture);
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000616
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +0000617 // If we're uploading compressed data then we should be using uploadCompressedTexData
618 SkASSERT(!GrPixelConfigIsCompressed(dataConfig));
619
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000620 size_t bpp = GrBytesPerPixel(dataConfig);
bsalomone8d21e82015-07-16 08:23:13 -0700621 if (!GrSurfacePriv::AdjustWritePixelParams(desc.fWidth, desc.fHeight, bpp, &left, &top,
622 &width, &height, &data, &rowBytes)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000623 return false;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000624 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000625 size_t trimRowBytes = width * bpp;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000626
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000627 // in case we need a temporary, trimmed copy of the src pixels
benjaminwagner7d974f52015-10-19 13:55:55 -0700628#if defined(GOOGLE3)
629 // Stack frame size is limited in GOOGLE3.
630 SkAutoSMalloc<64 * 128> tempStorage;
631#else
joshualitt29f86792015-05-29 08:06:48 -0700632 SkAutoSMalloc<128 * 128> tempStorage;
benjaminwagner7d974f52015-10-19 13:55:55 -0700633#endif
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000634
commit-bot@chromium.orgcea9abb2013-12-09 19:15:37 +0000635 // We currently lazily create MIPMAPs when the we see a draw with
636 // GrTextureParams::kMipMap_FilterMode. Using texture storage requires that the
637 // MIP levels are all created when the texture is created. So for now we don't use
638 // texture storage.
639 bool useTexStorage = false &&
640 isNewTexture &&
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000641 this->glCaps().texStorageSupport();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000642
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +0000643 if (useTexStorage && kGL_GrGLStandard == this->glStandard()) {
bsalomon@google.com313f0192012-07-10 17:21:02 +0000644 // 565 is not a sized internal format on desktop GL. So on desktop with
645 // 565 we always use an unsized internal format to let the system pick
646 // the best sized format to convert the 565 data to. Since TexStorage
647 // only allows sized internal formats we will instead use TexImage2D.
648 useTexStorage = desc.fConfig != kRGB_565_GrPixelConfig;
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000649 }
650
jvanverth3f801cb2014-12-16 09:49:38 -0800651 GrGLenum internalFormat = 0x0; // suppress warning
652 GrGLenum externalFormat = 0x0; // suppress warning
653 GrGLenum externalType = 0x0; // suppress warning
bsalomone904c092014-07-17 10:50:59 -0700654
jvanverthe1869ca2014-12-16 13:32:27 -0800655 // glTexStorage requires sized internal formats on both desktop and ES. ES2 requires an unsized
jvanverthf72b7522014-12-17 10:46:01 -0800656 // format for glTexImage, unlike ES3 and desktop.
jvanverthe1869ca2014-12-16 13:32:27 -0800657 bool useSizedFormat = useTexStorage;
jvanverthf72b7522014-12-17 10:46:01 -0800658 if (kGL_GrGLStandard == this->glStandard() ||
659 (this->glVersion() >= GR_GL_VER(3, 0) &&
660 // ES3 only works with sized BGRA8 format if "GL_APPLE_texture_format_BGRA8888" enabled
661 (kBGRA_8888_GrPixelConfig != dataConfig || !this->glCaps().bgraIsInternalFormat()))) {
jvanverthe1869ca2014-12-16 13:32:27 -0800662 useSizedFormat = true;
663 }
jvanverthf72b7522014-12-17 10:46:01 -0800664
bsalomone904c092014-07-17 10:50:59 -0700665 if (!this->configToGLFormats(dataConfig, useSizedFormat, &internalFormat,
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000666 &externalFormat, &externalType)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000667 return false;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000668 }
669
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000670 /*
bsalomon@google.com6f379512011-11-16 20:36:03 +0000671 * check whether to allocate a temporary buffer for flipping y or
672 * because our srcData has extra bytes past each row. If so, we need
673 * to trim those off here, since GL ES may not let us specify
674 * GL_UNPACK_ROW_LENGTH.
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000675 */
bsalomon@google.com6f379512011-11-16 20:36:03 +0000676 bool restoreGLRowLength = false;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000677 bool swFlipY = false;
678 bool glFlipY = false;
bsalomon49f085d2014-09-05 13:34:00 -0700679 if (data) {
senorblanco@chromium.orgef5dbe12013-01-28 16:42:38 +0000680 if (kBottomLeft_GrSurfaceOrigin == desc.fOrigin) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000681 if (this->glCaps().unpackFlipYSupport()) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000682 glFlipY = true;
683 } else {
684 swFlipY = true;
685 }
686 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000687 if (this->glCaps().unpackRowLengthSupport() && !swFlipY) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000688 // can't use this for flipping, only non-neg values allowed. :(
689 if (rowBytes != trimRowBytes) {
690 GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
691 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength));
692 restoreGLRowLength = true;
693 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000694 } else {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000695 if (trimRowBytes != rowBytes || swFlipY) {
696 // copy data into our new storage, skipping the trailing bytes
697 size_t trimSize = height * trimRowBytes;
698 const char* src = (const char*)data;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000699 if (swFlipY) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000700 src += (height - 1) * rowBytes;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000701 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000702 char* dst = (char*)tempStorage.reset(trimSize);
703 for (int y = 0; y < height; y++) {
704 memcpy(dst, src, trimRowBytes);
705 if (swFlipY) {
706 src -= rowBytes;
707 } else {
708 src += rowBytes;
709 }
710 dst += trimRowBytes;
711 }
712 // now point data to our copied version
713 data = tempStorage.get();
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000714 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000715 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000716 if (glFlipY) {
717 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_TRUE));
718 }
joshualittee5da552014-07-16 13:32:56 -0700719 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT,
720 static_cast<GrGLint>(GrUnpackAlignment(dataConfig))));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000721 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000722 bool succeeded = true;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000723 if (isNewTexture &&
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000724 0 == left && 0 == top &&
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000725 desc.fWidth == width && desc.fHeight == height) {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000726 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000727 if (useTexStorage) {
commit-bot@chromium.orgcea9abb2013-12-09 19:15:37 +0000728 // We never resize or change formats of textures.
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000729 GL_ALLOC_CALL(this->glInterface(),
bsalomon10528f12015-10-14 12:54:52 -0700730 TexStorage2D(target,
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000731 1, // levels
732 internalFormat,
733 desc.fWidth, desc.fHeight));
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000734 } else {
bsalomond4cb9222014-08-11 14:19:09 -0700735 GL_ALLOC_CALL(this->glInterface(),
bsalomon10528f12015-10-14 12:54:52 -0700736 TexImage2D(target,
bsalomond4cb9222014-08-11 14:19:09 -0700737 0, // level
738 internalFormat,
739 desc.fWidth, desc.fHeight,
740 0, // border
741 externalFormat, externalType,
742 data));
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000743 }
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000744 GrGLenum error = check_alloc_error(desc, this->glInterface());
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000745 if (error != GR_GL_NO_ERROR) {
746 succeeded = false;
747 } else {
748 // if we have data and we used TexStorage to create the texture, we
749 // now upload with TexSubImage.
bsalomon49f085d2014-09-05 13:34:00 -0700750 if (data && useTexStorage) {
bsalomon10528f12015-10-14 12:54:52 -0700751 GL_CALL(TexSubImage2D(target,
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000752 0, // level
753 left, top,
754 width, height,
755 externalFormat, externalType,
756 data));
757 }
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000758 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000759 } else {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000760 if (swFlipY || glFlipY) {
bsalomon@google.com6f379512011-11-16 20:36:03 +0000761 top = desc.fHeight - (top + height);
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000762 }
bsalomon10528f12015-10-14 12:54:52 -0700763 GL_CALL(TexSubImage2D(target,
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000764 0, // level
765 left, top,
766 width, height,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000767 externalFormat, externalType, data));
768 }
769
770 if (restoreGLRowLength) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000771 SkASSERT(this->glCaps().unpackRowLengthSupport());
bsalomon@google.com6f379512011-11-16 20:36:03 +0000772 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000773 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000774 if (glFlipY) {
775 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
776 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000777 return succeeded;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000778}
779
krajcevski145d48c2014-06-11 16:07:50 -0700780// TODO: This function is using a lot of wonky semantics like, if width == -1
piotaixre4b23142014-10-02 10:57:53 -0700781// then set width = desc.fWdith ... blah. A better way to do it might be to
krajcevski145d48c2014-06-11 16:07:50 -0700782// create a CompressedTexData struct that takes a desc/ptr and figures out
783// the proper upload semantics. Then users can construct this function how they
784// see fit if they want to go against the "standard" way to do it.
bsalomon861e1032014-12-16 07:33:49 -0800785bool GrGLGpu::uploadCompressedTexData(const GrSurfaceDesc& desc,
bsalomon10528f12015-10-14 12:54:52 -0700786 GrGLenum target,
krajcevski145d48c2014-06-11 16:07:50 -0700787 const void* data,
788 bool isNewTexture,
789 int left, int top, int width, int height) {
bsalomon49f085d2014-09-05 13:34:00 -0700790 SkASSERT(data || isNewTexture);
krajcevski9c0e6292014-06-02 07:38:14 -0700791
792 // No support for software flip y, yet...
793 SkASSERT(kBottomLeft_GrSurfaceOrigin != desc.fOrigin);
794
krajcevski145d48c2014-06-11 16:07:50 -0700795 if (-1 == width) {
796 width = desc.fWidth;
797 }
798#ifdef SK_DEBUG
799 else {
800 SkASSERT(width <= desc.fWidth);
801 }
802#endif
803
804 if (-1 == height) {
805 height = desc.fHeight;
806 }
807#ifdef SK_DEBUG
808 else {
809 SkASSERT(height <= desc.fHeight);
810 }
811#endif
812
krajcevski9c0e6292014-06-02 07:38:14 -0700813 // Make sure that the width and height that we pass to OpenGL
814 // is a multiple of the block size.
bsalomon98806072014-12-12 15:11:17 -0800815 size_t dataSize = GrCompressedFormatDataSize(desc.fConfig, width, height);
krajcevski9c0e6292014-06-02 07:38:14 -0700816
817 // We only need the internal format for compressed 2D textures.
818 GrGLenum internalFormat = 0;
halcanary96fcdcc2015-08-27 07:41:13 -0700819 if (!this->configToGLFormats(desc.fConfig, false, &internalFormat, nullptr, nullptr)) {
krajcevski9c0e6292014-06-02 07:38:14 -0700820 return false;
821 }
822
krajcevski145d48c2014-06-11 16:07:50 -0700823 if (isNewTexture) {
bsalomond4cb9222014-08-11 14:19:09 -0700824 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
krajcevski145d48c2014-06-11 16:07:50 -0700825 GL_ALLOC_CALL(this->glInterface(),
bsalomon10528f12015-10-14 12:54:52 -0700826 CompressedTexImage2D(target,
krajcevski145d48c2014-06-11 16:07:50 -0700827 0, // level
828 internalFormat,
829 width, height,
830 0, // border
bsalomon98806072014-12-12 15:11:17 -0800831 SkToInt(dataSize),
krajcevski145d48c2014-06-11 16:07:50 -0700832 data));
bsalomond4cb9222014-08-11 14:19:09 -0700833 GrGLenum error = check_alloc_error(desc, this->glInterface());
834 if (error != GR_GL_NO_ERROR) {
835 return false;
836 }
krajcevski145d48c2014-06-11 16:07:50 -0700837 } else {
bsalomond4cb9222014-08-11 14:19:09 -0700838 // Paletted textures can't be updated.
839 if (GR_GL_PALETTE8_RGBA8 == internalFormat) {
840 return false;
841 }
bsalomon10528f12015-10-14 12:54:52 -0700842 GL_CALL(CompressedTexSubImage2D(target,
bsalomond4cb9222014-08-11 14:19:09 -0700843 0, // level
844 left, top,
845 width, height,
846 internalFormat,
bsalomon98806072014-12-12 15:11:17 -0800847 SkToInt(dataSize),
bsalomond4cb9222014-08-11 14:19:09 -0700848 data));
krajcevski145d48c2014-06-11 16:07:50 -0700849 }
krajcevski9c0e6292014-06-02 07:38:14 -0700850
bsalomond4cb9222014-08-11 14:19:09 -0700851 return true;
krajcevski9c0e6292014-06-02 07:38:14 -0700852}
853
bsalomon424cc262015-05-22 10:37:30 -0700854static bool renderbuffer_storage_msaa(const GrGLContext& ctx,
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +0000855 int sampleCount,
856 GrGLenum format,
857 int width, int height) {
robertphillips@google.com6177e692013-02-28 20:16:25 +0000858 CLEAR_ERROR_BEFORE_ALLOC(ctx.interface());
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +0000859 SkASSERT(GrGLCaps::kNone_MSFBOType != ctx.caps()->msFBOType());
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +0000860 switch (ctx.caps()->msFBOType()) {
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +0000861 case GrGLCaps::kDesktop_ARB_MSFBOType:
862 case GrGLCaps::kDesktop_EXT_MSFBOType:
vbuzinovdded6962015-06-12 08:59:45 -0700863 case GrGLCaps::kMixedSamples_MSFBOType:
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +0000864 case GrGLCaps::kES_3_0_MSFBOType:
865 GL_ALLOC_CALL(ctx.interface(),
866 RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
867 sampleCount,
868 format,
869 width, height));
870 break;
871 case GrGLCaps::kES_Apple_MSFBOType:
872 GL_ALLOC_CALL(ctx.interface(),
873 RenderbufferStorageMultisampleES2APPLE(GR_GL_RENDERBUFFER,
874 sampleCount,
875 format,
876 width, height));
877 break;
878 case GrGLCaps::kES_EXT_MsToTexture_MSFBOType:
879 case GrGLCaps::kES_IMG_MsToTexture_MSFBOType:
880 GL_ALLOC_CALL(ctx.interface(),
881 RenderbufferStorageMultisampleES2EXT(GR_GL_RENDERBUFFER,
882 sampleCount,
883 format,
884 width, height));
885 break;
886 case GrGLCaps::kNone_MSFBOType:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000887 SkFAIL("Shouldn't be here if we don't support multisampled renderbuffers.");
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +0000888 break;
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000889 }
Brian Salomon9251d4e2015-03-17 16:03:19 -0400890 return (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctx.interface()));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000891}
892
egdanielb0e1be22015-04-22 13:27:39 -0700893bool GrGLGpu::createRenderTargetObjects(const GrSurfaceDesc& desc,
894 GrGpuResource::LifeCycle lifeCycle,
895 GrGLuint texID,
bsalomon10528f12015-10-14 12:54:52 -0700896 GrGLenum textureTarget,
bsalomonb15b4c12014-10-29 12:41:57 -0700897 GrGLRenderTarget::IDDesc* idDesc) {
898 idDesc->fMSColorRenderbufferID = 0;
egdanield803f272015-03-18 13:01:52 -0700899 idDesc->fRTFBOID = 0;
900 idDesc->fTexFBOID = 0;
egdanielb0e1be22015-04-22 13:27:39 -0700901 idDesc->fLifeCycle = lifeCycle;
vbuzinovdded6962015-06-12 08:59:45 -0700902 idDesc->fSampleConfig = (GrGLCaps::kMixedSamples_MSFBOType == this->glCaps().msFBOType() &&
903 desc.fSampleCnt > 0) ? GrRenderTarget::kStencil_SampleConfig :
904 GrRenderTarget::kUnified_SampleConfig;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000905
906 GrGLenum status;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000907
bsalomon@google.comab15d612011-08-09 12:57:56 +0000908 GrGLenum msColorFormat = 0; // suppress warning
909
bsalomonb15b4c12014-10-29 12:41:57 -0700910 if (desc.fSampleCnt > 0 && GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.com347c3822013-05-01 20:10:01 +0000911 goto FAILED;
912 }
913
egdanield803f272015-03-18 13:01:52 -0700914 GL_CALL(GenFramebuffers(1, &idDesc->fTexFBOID));
915 if (!idDesc->fTexFBOID) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000916 goto FAILED;
917 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000918
egdanield803f272015-03-18 13:01:52 -0700919
bsalomon@google.comf3a60c02013-03-19 19:06:09 +0000920 // If we are using multisampling we will create two FBOS. We render to one and then resolve to
921 // the texture bound to the other. The exception is the IMG multisample extension. With this
922 // extension the texture is multisampled when rendered to and then auto-resolves it when it is
923 // rendered from.
bsalomonb15b4c12014-10-29 12:41:57 -0700924 if (desc.fSampleCnt > 0 && this->glCaps().usesMSAARenderBuffers()) {
egdanield803f272015-03-18 13:01:52 -0700925 GL_CALL(GenFramebuffers(1, &idDesc->fRTFBOID));
bsalomonb15b4c12014-10-29 12:41:57 -0700926 GL_CALL(GenRenderbuffers(1, &idDesc->fMSColorRenderbufferID));
egdanield803f272015-03-18 13:01:52 -0700927 if (!idDesc->fRTFBOID ||
928 !idDesc->fMSColorRenderbufferID ||
bsalomonb15b4c12014-10-29 12:41:57 -0700929 !this->configToGLFormats(desc.fConfig,
commit-bot@chromium.org87002952013-08-20 15:12:01 +0000930 // ES2 and ES3 require sized internal formats for rb storage.
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +0000931 kGLES_GrGLStandard == this->glStandard(),
bsalomon@google.com347c3822013-05-01 20:10:01 +0000932 &msColorFormat,
halcanary96fcdcc2015-08-27 07:41:13 -0700933 nullptr,
934 nullptr)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000935 goto FAILED;
936 }
937 } else {
egdanield803f272015-03-18 13:01:52 -0700938 idDesc->fRTFBOID = idDesc->fTexFBOID;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000939 }
940
egdanield803f272015-03-18 13:01:52 -0700941 // below here we may bind the FBO
942 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
943 if (idDesc->fRTFBOID != idDesc->fTexFBOID) {
bsalomonb15b4c12014-10-29 12:41:57 -0700944 SkASSERT(desc.fSampleCnt > 0);
945 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, idDesc->fMSColorRenderbufferID));
bsalomon424cc262015-05-22 10:37:30 -0700946 if (!renderbuffer_storage_msaa(*fGLContext,
bsalomonb15b4c12014-10-29 12:41:57 -0700947 desc.fSampleCnt,
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000948 msColorFormat,
bsalomonb15b4c12014-10-29 12:41:57 -0700949 desc.fWidth, desc.fHeight)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000950 goto FAILED;
951 }
egdanield803f272015-03-18 13:01:52 -0700952 fStats.incRenderTargetBinds();
953 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, idDesc->fRTFBOID));
954 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon10528f12015-10-14 12:54:52 -0700955 GR_GL_COLOR_ATTACHMENT0,
956 GR_GL_RENDERBUFFER,
957 idDesc->fMSColorRenderbufferID));
bsalomonb15b4c12014-10-29 12:41:57 -0700958 if ((desc.fFlags & kCheckAllocation_GrSurfaceFlag) ||
959 !this->glCaps().isConfigVerifiedColorAttachment(desc.fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000960 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
961 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
962 goto FAILED;
963 }
bsalomon424cc262015-05-22 10:37:30 -0700964 fGLContext->caps()->markConfigAsValidColorAttachment(desc.fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000965 }
966 }
egdanield803f272015-03-18 13:01:52 -0700967 fStats.incRenderTargetBinds();
968 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, idDesc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000969
bsalomonb15b4c12014-10-29 12:41:57 -0700970 if (this->glCaps().usesImplicitMSAAResolve() && desc.fSampleCnt > 0) {
egdanield803f272015-03-18 13:01:52 -0700971 GL_CALL(FramebufferTexture2DMultisample(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +0000972 GR_GL_COLOR_ATTACHMENT0,
bsalomon10528f12015-10-14 12:54:52 -0700973 textureTarget,
bsalomonb15b4c12014-10-29 12:41:57 -0700974 texID, 0, desc.fSampleCnt));
bsalomon@google.comf3a60c02013-03-19 19:06:09 +0000975 } else {
egdanield803f272015-03-18 13:01:52 -0700976 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +0000977 GR_GL_COLOR_ATTACHMENT0,
bsalomon10528f12015-10-14 12:54:52 -0700978 textureTarget,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +0000979 texID, 0));
980 }
bsalomonb15b4c12014-10-29 12:41:57 -0700981 if ((desc.fFlags & kCheckAllocation_GrSurfaceFlag) ||
982 !this->glCaps().isConfigVerifiedColorAttachment(desc.fConfig)) {
egdanield803f272015-03-18 13:01:52 -0700983 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000984 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
985 goto FAILED;
986 }
bsalomon424cc262015-05-22 10:37:30 -0700987 fGLContext->caps()->markConfigAsValidColorAttachment(desc.fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000988 }
989
990 return true;
991
992FAILED:
bsalomonb15b4c12014-10-29 12:41:57 -0700993 if (idDesc->fMSColorRenderbufferID) {
994 GL_CALL(DeleteRenderbuffers(1, &idDesc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000995 }
egdanield803f272015-03-18 13:01:52 -0700996 if (idDesc->fRTFBOID != idDesc->fTexFBOID) {
997 GL_CALL(DeleteFramebuffers(1, &idDesc->fRTFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000998 }
egdanield803f272015-03-18 13:01:52 -0700999 if (idDesc->fTexFBOID) {
1000 GL_CALL(DeleteFramebuffers(1, &idDesc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001001 }
1002 return false;
1003}
1004
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001005// good to set a break-point here to know when createTexture fails
1006static GrTexture* return_null_texture() {
mtklein@google.com330313a2013-08-22 15:37:26 +00001007// SkDEBUGFAIL("null texture");
halcanary96fcdcc2015-08-27 07:41:13 -07001008 return nullptr;
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001009}
1010
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +00001011#if 0 && defined(SK_DEBUG)
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001012static size_t as_size_t(int x) {
1013 return x;
1014}
1015#endif
1016
egdanielb0e1be22015-04-22 13:27:39 -07001017GrTexture* GrGLGpu::onCreateTexture(const GrSurfaceDesc& desc,
1018 GrGpuResource::LifeCycle lifeCycle,
bsalomon5236cf42015-01-14 10:42:08 -08001019 const void* srcData, size_t rowBytes) {
bsalomon@google.com8a70eef2013-03-19 13:58:55 +00001020 // We fail if the MSAA was requested and is not available.
1021 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType() && desc.fSampleCnt) {
tfarina38406c82014-10-31 07:11:12 -07001022 //SkDebugf("MSAA RT requested but not supported on this platform.");
bsalomon@google.com8a70eef2013-03-19 13:58:55 +00001023 return return_null_texture();
1024 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001025
bsalomonf2703d82014-10-28 14:33:06 -07001026 bool renderTarget = SkToBool(desc.fFlags & kRenderTarget_GrSurfaceFlag);
reed@google.comac10a2d2010-12-22 21:39:39 +00001027
bsalomonb15b4c12014-10-29 12:41:57 -07001028 GrGLTexture::IDDesc idDesc;
1029 GL_CALL(GenTextures(1, &idDesc.fTextureID));
egdanielb0e1be22015-04-22 13:27:39 -07001030 idDesc.fLifeCycle = lifeCycle;
bsalomon10528f12015-10-14 12:54:52 -07001031 // We only support GL_TEXTURE_2D at the moment.
1032 idDesc.fTarget = GR_GL_TEXTURE_2D;
junov@chromium.org8b6b1c92013-08-29 16:22:09 +00001033
bsalomonb15b4c12014-10-29 12:41:57 -07001034 if (!idDesc.fTextureID) {
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001035 return return_null_texture();
1036 }
1037
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00001038 this->setScratchTextureUnit();
bsalomon10528f12015-10-14 12:54:52 -07001039 GL_CALL(BindTexture(idDesc.fTarget, idDesc.fTextureID));
bsalomon@google.come269f212011-11-07 13:29:52 +00001040
junov@chromium.org8b6b1c92013-08-29 16:22:09 +00001041 if (renderTarget && this->glCaps().textureUsageSupport()) {
1042 // provides a hint about how this texture will be used
bsalomon10528f12015-10-14 12:54:52 -07001043 GL_CALL(TexParameteri(idDesc.fTarget,
junov@chromium.org8b6b1c92013-08-29 16:22:09 +00001044 GR_GL_TEXTURE_USAGE,
1045 GR_GL_FRAMEBUFFER_ATTACHMENT));
1046 }
1047
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001048 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
1049 // drivers have a bug where an FBO won't be complete if it includes a
1050 // texture that is not mipmap complete (considering the filter in use).
1051 GrGLTexture::TexParams initialTexParams;
1052 // we only set a subset here so invalidate first
1053 initialTexParams.invalidate();
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00001054 initialTexParams.fMinFilter = GR_GL_NEAREST;
1055 initialTexParams.fMagFilter = GR_GL_NEAREST;
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001056 initialTexParams.fWrapS = GR_GL_CLAMP_TO_EDGE;
1057 initialTexParams.fWrapT = GR_GL_CLAMP_TO_EDGE;
bsalomon10528f12015-10-14 12:54:52 -07001058 GL_CALL(TexParameteri(idDesc.fTarget,
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001059 GR_GL_TEXTURE_MAG_FILTER,
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00001060 initialTexParams.fMagFilter));
bsalomon10528f12015-10-14 12:54:52 -07001061 GL_CALL(TexParameteri(idDesc.fTarget,
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001062 GR_GL_TEXTURE_MIN_FILTER,
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00001063 initialTexParams.fMinFilter));
bsalomon10528f12015-10-14 12:54:52 -07001064 GL_CALL(TexParameteri(idDesc.fTarget,
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001065 GR_GL_TEXTURE_WRAP_S,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001066 initialTexParams.fWrapS));
bsalomon10528f12015-10-14 12:54:52 -07001067 GL_CALL(TexParameteri(idDesc.fTarget,
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001068 GR_GL_TEXTURE_WRAP_T,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001069 initialTexParams.fWrapT));
bsalomon10528f12015-10-14 12:54:52 -07001070 if (!this->uploadTexData(desc, idDesc.fTarget, true, 0, 0,
bsalomonb15b4c12014-10-29 12:41:57 -07001071 desc.fWidth, desc.fHeight,
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001072 desc.fConfig, srcData, rowBytes)) {
bsalomonb15b4c12014-10-29 12:41:57 -07001073 GL_CALL(DeleteTextures(1, &idDesc.fTextureID));
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001074 return return_null_texture();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001075 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001076
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001077 GrGLTexture* tex;
reed@google.comac10a2d2010-12-22 21:39:39 +00001078 if (renderTarget) {
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001079 // unbind the texture from the texture unit before binding it to the frame buffer
bsalomon10528f12015-10-14 12:54:52 -07001080 GL_CALL(BindTexture(idDesc.fTarget, 0));
bsalomon5236cf42015-01-14 10:42:08 -08001081 GrGLRenderTarget::IDDesc rtIDDesc;
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001082
bsalomon10528f12015-10-14 12:54:52 -07001083 if (!this->createRenderTargetObjects(desc, lifeCycle, idDesc.fTextureID, idDesc.fTarget,
1084 &rtIDDesc)) {
bsalomonb15b4c12014-10-29 12:41:57 -07001085 GL_CALL(DeleteTextures(1, &idDesc.fTextureID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001086 return return_null_texture();
1087 }
halcanary385fe4d2015-08-26 13:07:48 -07001088 tex = new GrGLTextureRenderTarget(this, desc, idDesc, rtIDDesc);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001089 } else {
halcanary385fe4d2015-08-26 13:07:48 -07001090 tex = new GrGLTexture(this, desc, idDesc);
reed@google.comac10a2d2010-12-22 21:39:39 +00001091 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001092 tex->setCachedTexParams(initialTexParams, this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00001093#ifdef TRACE_TEXTURE_CREATION
tfarina38406c82014-10-31 07:11:12 -07001094 SkDebugf("--- new texture [%d] size=(%d %d) config=%d\n",
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001095 glTexDesc.fTextureID, desc.fWidth, desc.fHeight, desc.fConfig);
reed@google.comac10a2d2010-12-22 21:39:39 +00001096#endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001097 return tex;
1098}
1099
egdanielb0e1be22015-04-22 13:27:39 -07001100GrTexture* GrGLGpu::onCreateCompressedTexture(const GrSurfaceDesc& desc,
1101 GrGpuResource::LifeCycle lifeCycle,
bsalomon5236cf42015-01-14 10:42:08 -08001102 const void* srcData) {
krajcevski9c0e6292014-06-02 07:38:14 -07001103 // Make sure that we're not flipping Y.
egdanielb0e1be22015-04-22 13:27:39 -07001104 if (kBottomLeft_GrSurfaceOrigin == desc.fOrigin) {
krajcevski9c0e6292014-06-02 07:38:14 -07001105 return return_null_texture();
1106 }
1107
bsalomonb15b4c12014-10-29 12:41:57 -07001108 GrGLTexture::IDDesc idDesc;
1109 GL_CALL(GenTextures(1, &idDesc.fTextureID));
egdanielb0e1be22015-04-22 13:27:39 -07001110 idDesc.fLifeCycle = lifeCycle;
bsalomon10528f12015-10-14 12:54:52 -07001111 // We only support GL_TEXTURE_2D at the moment.
1112 idDesc.fTarget = GR_GL_TEXTURE_2D;
krajcevski9c0e6292014-06-02 07:38:14 -07001113
bsalomonb15b4c12014-10-29 12:41:57 -07001114 if (!idDesc.fTextureID) {
krajcevski9c0e6292014-06-02 07:38:14 -07001115 return return_null_texture();
1116 }
1117
1118 this->setScratchTextureUnit();
bsalomon10528f12015-10-14 12:54:52 -07001119 GL_CALL(BindTexture(idDesc.fTarget, idDesc.fTextureID));
krajcevski9c0e6292014-06-02 07:38:14 -07001120
1121 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
1122 // drivers have a bug where an FBO won't be complete if it includes a
1123 // texture that is not mipmap complete (considering the filter in use).
1124 GrGLTexture::TexParams initialTexParams;
1125 // we only set a subset here so invalidate first
1126 initialTexParams.invalidate();
1127 initialTexParams.fMinFilter = GR_GL_NEAREST;
1128 initialTexParams.fMagFilter = GR_GL_NEAREST;
1129 initialTexParams.fWrapS = GR_GL_CLAMP_TO_EDGE;
1130 initialTexParams.fWrapT = GR_GL_CLAMP_TO_EDGE;
bsalomon10528f12015-10-14 12:54:52 -07001131 GL_CALL(TexParameteri(idDesc.fTarget,
krajcevski9c0e6292014-06-02 07:38:14 -07001132 GR_GL_TEXTURE_MAG_FILTER,
1133 initialTexParams.fMagFilter));
bsalomon10528f12015-10-14 12:54:52 -07001134 GL_CALL(TexParameteri(idDesc.fTarget,
krajcevski9c0e6292014-06-02 07:38:14 -07001135 GR_GL_TEXTURE_MIN_FILTER,
1136 initialTexParams.fMinFilter));
bsalomon10528f12015-10-14 12:54:52 -07001137 GL_CALL(TexParameteri(idDesc.fTarget,
krajcevski9c0e6292014-06-02 07:38:14 -07001138 GR_GL_TEXTURE_WRAP_S,
1139 initialTexParams.fWrapS));
bsalomon10528f12015-10-14 12:54:52 -07001140 GL_CALL(TexParameteri(idDesc.fTarget,
krajcevski9c0e6292014-06-02 07:38:14 -07001141 GR_GL_TEXTURE_WRAP_T,
1142 initialTexParams.fWrapT));
1143
bsalomon10528f12015-10-14 12:54:52 -07001144 if (!this->uploadCompressedTexData(desc, idDesc.fTarget, srcData)) {
bsalomonb15b4c12014-10-29 12:41:57 -07001145 GL_CALL(DeleteTextures(1, &idDesc.fTextureID));
krajcevski9c0e6292014-06-02 07:38:14 -07001146 return return_null_texture();
1147 }
1148
1149 GrGLTexture* tex;
halcanary385fe4d2015-08-26 13:07:48 -07001150 tex = new GrGLTexture(this, desc, idDesc);
krajcevski9c0e6292014-06-02 07:38:14 -07001151 tex->setCachedTexParams(initialTexParams, this->getResetTimestamp());
1152#ifdef TRACE_TEXTURE_CREATION
tfarina38406c82014-10-31 07:11:12 -07001153 SkDebugf("--- new compressed texture [%d] size=(%d %d) config=%d\n",
krajcevski9c0e6292014-06-02 07:38:14 -07001154 glTexDesc.fTextureID, desc.fWidth, desc.fHeight, desc.fConfig);
1155#endif
1156 return tex;
1157}
1158
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001159namespace {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001160
egdaniel8dc7c3a2015-04-16 11:22:42 -07001161const GrGLuint kUnknownBitCount = GrGLStencilAttachment::kUnknownBitCount;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001162
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001163void inline get_stencil_rb_sizes(const GrGLInterface* gl,
egdaniel8dc7c3a2015-04-16 11:22:42 -07001164 GrGLStencilAttachment::Format* format) {
sugoi@google.com6ba0b0f2013-05-03 13:52:32 +00001165
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001166 // we shouldn't ever know one size and not the other
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001167 SkASSERT((kUnknownBitCount == format->fStencilBits) ==
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001168 (kUnknownBitCount == format->fTotalBits));
1169 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001170 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001171 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1172 (GrGLint*)&format->fStencilBits);
1173 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001174 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001175 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1176 (GrGLint*)&format->fTotalBits);
1177 format->fTotalBits += format->fStencilBits;
1178 } else {
1179 format->fTotalBits = format->fStencilBits;
1180 }
1181 }
1182}
1183}
1184
egdanielff1d5472015-09-10 08:37:20 -07001185int GrGLGpu::getCompatibleStencilIndex(GrPixelConfig config) {
1186 int size = this->caps()->minTextureSize();
1187 if (kUnknownStencilIndex == fPixelConfigToStencilIndex[config]) {
1188 // Default to unsupported
1189 fPixelConfigToStencilIndex[config] = kUnsupportedStencilIndex;
1190 // Create color texture
1191 GrGLuint colorID;
1192 GL_CALL(GenTextures(1, &colorID));
1193 this->setScratchTextureUnit();
1194 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, colorID));
1195 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1196 GR_GL_TEXTURE_MAG_FILTER,
1197 GR_GL_NEAREST));
1198 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1199 GR_GL_TEXTURE_MIN_FILTER,
1200 GR_GL_NEAREST));
1201 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1202 GR_GL_TEXTURE_WRAP_S,
1203 GR_GL_CLAMP_TO_EDGE));
1204 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1205 GR_GL_TEXTURE_WRAP_T,
1206 GR_GL_CLAMP_TO_EDGE));
1207
1208 GrGLenum internalFormat = 0x0; // suppress warning
1209 GrGLenum externalFormat = 0x0; // suppress warning
1210 GrGLenum externalType = 0x0; // suppress warning
egdanieleef3c5b2015-09-10 11:28:22 -07001211 bool useSizedFormat = false;
1212 if (kGL_GrGLStandard == this->glStandard() ||
1213 (this->glVersion() >= GR_GL_VER(3, 0) &&
1214 // ES3 only works with sized BGRA8 format if "GL_APPLE_texture_format_BGRA8888" enabled
1215 (kBGRA_8888_GrPixelConfig != config || !this->glCaps().bgraIsInternalFormat()))) {
1216 useSizedFormat = true;
1217 }
1218 if (!this->configToGLFormats(config, useSizedFormat, &internalFormat,
egdanielff1d5472015-09-10 08:37:20 -07001219 &externalFormat, &externalType)) {
1220 GL_CALL(DeleteTextures(1, &colorID));
1221 fPixelConfigToStencilIndex[config] = kUnsupportedStencilIndex;
1222 return kUnsupportedStencilIndex;
1223 }
1224
1225 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1226 GL_ALLOC_CALL(this->glInterface(), TexImage2D(GR_GL_TEXTURE_2D,
1227 0, internalFormat,
1228 size,
1229 size,
1230 0,
1231 externalFormat,
1232 externalType,
1233 NULL));
1234 if (GR_GL_NO_ERROR != GR_GL_GET_ERROR(this->glInterface())) {
1235 GL_CALL(DeleteTextures(1, &colorID));
1236 fPixelConfigToStencilIndex[config] = kUnsupportedStencilIndex;
1237 return kUnsupportedStencilIndex;
1238 }
1239
1240 // unbind the texture from the texture unit before binding it to the frame buffer
1241 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, 0));
1242
1243 // Create Framebuffer
1244 GrGLuint fb;
1245 GL_CALL(GenFramebuffers(1, &fb));
egdanielec00d942015-09-14 12:56:10 -07001246 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, fb));
egdanielff1d5472015-09-10 08:37:20 -07001247 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
1248 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
1249 GR_GL_COLOR_ATTACHMENT0,
1250 GR_GL_TEXTURE_2D,
1251 colorID,
1252 0));
1253
1254 // look over formats till I find a compatible one
1255 int stencilFmtCnt = this->glCaps().stencilFormats().count();
1256 GrGLuint sbRBID = 0;
1257 for (int i = 0; i < stencilFmtCnt; ++i) {
1258 const GrGLCaps::StencilFormat& sFmt = this->glCaps().stencilFormats()[i];
1259
1260 GL_CALL(GenRenderbuffers(1, &sbRBID));
1261 if (!sbRBID) {
1262 break;
1263 }
1264 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbRBID));
1265 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1266 GL_ALLOC_CALL(this->glInterface(), RenderbufferStorage(GR_GL_RENDERBUFFER,
1267 sFmt.fInternalFormat,
1268 size, size));
1269 if (GR_GL_NO_ERROR == GR_GL_GET_ERROR(this->glInterface())) {
1270 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1271 GR_GL_STENCIL_ATTACHMENT,
1272 GR_GL_RENDERBUFFER, sbRBID));
1273 if (sFmt.fPacked) {
1274 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1275 GR_GL_DEPTH_ATTACHMENT,
1276 GR_GL_RENDERBUFFER, sbRBID));
1277 } else {
1278 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1279 GR_GL_DEPTH_ATTACHMENT,
1280 GR_GL_RENDERBUFFER, 0));
1281 }
1282 GrGLenum status;
1283 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1284 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
1285 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1286 GR_GL_STENCIL_ATTACHMENT,
1287 GR_GL_RENDERBUFFER, 0));
1288 if (sFmt.fPacked) {
1289 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1290 GR_GL_DEPTH_ATTACHMENT,
1291 GR_GL_RENDERBUFFER, 0));
1292 }
1293 } else {
1294 fPixelConfigToStencilIndex[config] = i;
1295 break;
1296 }
1297 }
1298 sbRBID = 0;
1299 }
1300 GL_CALL(DeleteTextures(1, &colorID));
1301 GL_CALL(DeleteRenderbuffers(1, &sbRBID));
1302 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, 0));
1303 GL_CALL(DeleteFramebuffers(1, &fb));
1304 }
1305 SkASSERT(kUnknownStencilIndex != fPixelConfigToStencilIndex[config]);
1306 return fPixelConfigToStencilIndex[config];
1307}
1308
egdanielec00d942015-09-14 12:56:10 -07001309GrStencilAttachment* GrGLGpu::createStencilAttachmentForRenderTarget(const GrRenderTarget* rt,
1310 int width,
1311 int height) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001312 // All internally created RTs are also textures. We don't create
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +00001313 // 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 +00001314 SkASSERT(rt->asTexture());
1315 SkASSERT(width >= rt->width());
1316 SkASSERT(height >= rt->height());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001317
vbuzinovdded6962015-06-12 08:59:45 -07001318 int samples = rt->numStencilSamples();
egdaniel8dc7c3a2015-04-16 11:22:42 -07001319 GrGLStencilAttachment::IDDesc sbDesc;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001320
egdanielff1d5472015-09-10 08:37:20 -07001321 int sIdx = this->getCompatibleStencilIndex(rt->config());
1322 if (sIdx == kUnsupportedStencilIndex) {
egdanielec00d942015-09-14 12:56:10 -07001323 return nullptr;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001324 }
egdanielff1d5472015-09-10 08:37:20 -07001325
1326 if (!sbDesc.fRenderbufferID) {
1327 GL_CALL(GenRenderbuffers(1, &sbDesc.fRenderbufferID));
1328 }
1329 if (!sbDesc.fRenderbufferID) {
egdanielec00d942015-09-14 12:56:10 -07001330 return nullptr;
egdanielff1d5472015-09-10 08:37:20 -07001331 }
1332 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbDesc.fRenderbufferID));
1333 const GrGLCaps::StencilFormat& sFmt = this->glCaps().stencilFormats()[sIdx];
1334 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1335 // we do this "if" so that we don't call the multisample
1336 // version on a GL that doesn't have an MSAA extension.
1337 if (samples > 0) {
1338 SkAssertResult(renderbuffer_storage_msaa(*fGLContext,
1339 samples,
1340 sFmt.fInternalFormat,
1341 width, height));
1342 } else {
1343 GL_ALLOC_CALL(this->glInterface(), RenderbufferStorage(GR_GL_RENDERBUFFER,
1344 sFmt.fInternalFormat,
1345 width, height));
1346 SkASSERT(GR_GL_NO_ERROR == check_alloc_error(rt->desc(), this->glInterface()));
1347 }
1348 fStats.incStencilAttachmentCreates();
1349 // After sized formats we attempt an unsized format and take
1350 // whatever sizes GL gives us. In that case we query for the size.
1351 GrGLStencilAttachment::Format format = sFmt;
1352 get_stencil_rb_sizes(this->glInterface(), &format);
egdanielec00d942015-09-14 12:56:10 -07001353 GrGLStencilAttachment* stencil = new GrGLStencilAttachment(this,
1354 sbDesc,
1355 width,
1356 height,
1357 samples,
1358 format);
1359 return stencil;
reed@google.comac10a2d2010-12-22 21:39:39 +00001360}
1361
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001362////////////////////////////////////////////////////////////////////////////////
1363
bsalomon861e1032014-12-16 07:33:49 -08001364GrVertexBuffer* GrGLGpu::onCreateVertexBuffer(size_t size, bool dynamic) {
bsalomon@google.come49ad452013-02-20 19:33:20 +00001365 GrGLVertexBuffer::Desc desc;
1366 desc.fDynamic = dynamic;
1367 desc.fSizeInBytes = size;
bsalomon@google.come49ad452013-02-20 19:33:20 +00001368
bsalomon@google.com96966a52013-02-21 16:34:21 +00001369 if (this->glCaps().useNonVBOVertexAndIndexDynamicData() && desc.fDynamic) {
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001370 desc.fID = 0;
halcanary385fe4d2015-08-26 13:07:48 -07001371 GrGLVertexBuffer* vertexBuffer = new GrGLVertexBuffer(this, desc);
reed@google.comac10a2d2010-12-22 21:39:39 +00001372 return vertexBuffer;
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001373 } else {
1374 GL_CALL(GenBuffers(1, &desc.fID));
1375 if (desc.fID) {
bsalomon@google.com6918d482013-03-07 19:09:11 +00001376 fHWGeometryState.setVertexBufferID(this, desc.fID);
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001377 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1378 // make sure driver can allocate memory for this buffer
1379 GL_ALLOC_CALL(this->glInterface(),
1380 BufferData(GR_GL_ARRAY_BUFFER,
robertphillips@google.come9cd27d2013-10-16 17:48:11 +00001381 (GrGLsizeiptr) desc.fSizeInBytes,
halcanary96fcdcc2015-08-27 07:41:13 -07001382 nullptr, // data ptr
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001383 desc.fDynamic ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW));
1384 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
1385 GL_CALL(DeleteBuffers(1, &desc.fID));
bsalomon@google.com6918d482013-03-07 19:09:11 +00001386 this->notifyVertexBufferDelete(desc.fID);
halcanary96fcdcc2015-08-27 07:41:13 -07001387 return nullptr;
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001388 }
halcanary385fe4d2015-08-26 13:07:48 -07001389 GrGLVertexBuffer* vertexBuffer = new GrGLVertexBuffer(this, desc);
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001390 return vertexBuffer;
1391 }
halcanary96fcdcc2015-08-27 07:41:13 -07001392 return nullptr;
reed@google.comac10a2d2010-12-22 21:39:39 +00001393 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001394}
1395
bsalomon861e1032014-12-16 07:33:49 -08001396GrIndexBuffer* GrGLGpu::onCreateIndexBuffer(size_t size, bool dynamic) {
bsalomon@google.come49ad452013-02-20 19:33:20 +00001397 GrGLIndexBuffer::Desc desc;
1398 desc.fDynamic = dynamic;
1399 desc.fSizeInBytes = size;
bsalomon@google.come49ad452013-02-20 19:33:20 +00001400
bsalomon@google.com96966a52013-02-21 16:34:21 +00001401 if (this->glCaps().useNonVBOVertexAndIndexDynamicData() && desc.fDynamic) {
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001402 desc.fID = 0;
halcanary385fe4d2015-08-26 13:07:48 -07001403 GrIndexBuffer* indexBuffer = new GrGLIndexBuffer(this, desc);
reed@google.comac10a2d2010-12-22 21:39:39 +00001404 return indexBuffer;
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001405 } else {
1406 GL_CALL(GenBuffers(1, &desc.fID));
1407 if (desc.fID) {
bsalomon@google.com6918d482013-03-07 19:09:11 +00001408 fHWGeometryState.setIndexBufferIDOnDefaultVertexArray(this, desc.fID);
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001409 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1410 // make sure driver can allocate memory for this buffer
1411 GL_ALLOC_CALL(this->glInterface(),
1412 BufferData(GR_GL_ELEMENT_ARRAY_BUFFER,
robertphillips@google.come9cd27d2013-10-16 17:48:11 +00001413 (GrGLsizeiptr) desc.fSizeInBytes,
halcanary96fcdcc2015-08-27 07:41:13 -07001414 nullptr, // data ptr
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001415 desc.fDynamic ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW));
1416 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
1417 GL_CALL(DeleteBuffers(1, &desc.fID));
bsalomon@google.com6918d482013-03-07 19:09:11 +00001418 this->notifyIndexBufferDelete(desc.fID);
halcanary96fcdcc2015-08-27 07:41:13 -07001419 return nullptr;
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001420 }
halcanary385fe4d2015-08-26 13:07:48 -07001421 GrIndexBuffer* indexBuffer = new GrGLIndexBuffer(this, desc);
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001422 return indexBuffer;
1423 }
halcanary96fcdcc2015-08-27 07:41:13 -07001424 return nullptr;
reed@google.comac10a2d2010-12-22 21:39:39 +00001425 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001426}
1427
bsalomon3e791242014-12-17 13:43:13 -08001428void GrGLGpu::flushScissor(const GrScissorState& scissorState,
joshualitt77b13072014-10-27 14:51:01 -07001429 const GrGLIRect& rtViewport,
1430 GrSurfaceOrigin rtOrigin) {
robertphillipse85a32d2015-02-10 08:16:55 -08001431 if (scissorState.enabled()) {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001432 GrGLIRect scissor;
bsalomonb0bd4f62014-09-03 07:19:50 -07001433 scissor.setRelativeTo(rtViewport,
robertphillipse85a32d2015-02-10 08:16:55 -08001434 scissorState.rect().fLeft,
1435 scissorState.rect().fTop,
1436 scissorState.rect().width(),
1437 scissorState.rect().height(),
bsalomonb0bd4f62014-09-03 07:19:50 -07001438 rtOrigin);
bsalomon@google.coma3201942012-06-21 19:58:20 +00001439 // if the scissor fully contains the viewport then we fall through and
1440 // disable the scissor test.
bsalomonb0bd4f62014-09-03 07:19:50 -07001441 if (!scissor.contains(rtViewport)) {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001442 if (fHWScissorSettings.fRect != scissor) {
1443 scissor.pushToGLScissor(this->glInterface());
1444 fHWScissorSettings.fRect = scissor;
1445 }
1446 if (kYes_TriState != fHWScissorSettings.fEnabled) {
1447 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1448 fHWScissorSettings.fEnabled = kYes_TriState;
1449 }
1450 return;
1451 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001452 }
joshualitt77b13072014-10-27 14:51:01 -07001453
1454 // See fall through note above
1455 this->disableScissor();
1456}
1457
cdaltond0a840d2015-03-16 17:19:58 -07001458bool GrGLGpu::flushGLState(const DrawArgs& args) {
egdaniel080e6732014-12-22 07:35:52 -08001459 GrXferProcessor::BlendInfo blendInfo;
egdaniel8dd688b2015-01-22 10:16:09 -08001460 const GrPipeline& pipeline = *args.fPipeline;
1461 args.fPipeline->getXferProcessor()->getBlendInfo(&blendInfo);
egdaniel080e6732014-12-22 07:35:52 -08001462
egdaniel080e6732014-12-22 07:35:52 -08001463 this->flushColorWrite(blendInfo.fWriteColor);
egdaniel8dd688b2015-01-22 10:16:09 -08001464 this->flushDrawFace(pipeline.getDrawFace());
bsalomonbc3d0de2014-12-15 13:45:03 -08001465
bsalomon6df86402015-06-01 10:41:49 -07001466 SkAutoTUnref<GrGLProgram> program(fProgramCache->refProgram(args));
1467 if (!program) {
bsalomon682c2692015-05-22 14:01:46 -07001468 GrCapsDebugf(this->caps(), "Failed to create program!\n");
bsalomon1f78c0a2014-12-17 09:43:13 -08001469 return false;
bsalomonbc3d0de2014-12-15 13:45:03 -08001470 }
1471
bsalomon6df86402015-06-01 10:41:49 -07001472 GrGLuint programID = program->programID();
bsalomon1f78c0a2014-12-17 09:43:13 -08001473 if (fHWProgramID != programID) {
1474 GL_CALL(UseProgram(programID));
1475 fHWProgramID = programID;
1476 }
1477
egdanield803f272015-03-18 13:01:52 -07001478 if (blendInfo.fWriteColor) {
1479 this->flushBlend(blendInfo);
1480 }
bsalomon1f78c0a2014-12-17 09:43:13 -08001481
cdalton42717652015-06-18 11:54:30 -07001482 SkSTArray<8, const GrTextureAccess*> textureAccesses;
joshualitt465283c2015-09-11 08:19:35 -07001483 program->setData(*args.fPrimitiveProcessor, pipeline, &textureAccesses);
cdalton42717652015-06-18 11:54:30 -07001484
1485 int numTextureAccesses = textureAccesses.count();
1486 for (int i = 0; i < numTextureAccesses; i++) {
1487 this->bindTexture(i, textureAccesses[i]->getParams(),
1488 static_cast<GrGLTexture*>(textureAccesses[i]->getTexture()));
1489 }
bsalomon1f78c0a2014-12-17 09:43:13 -08001490
egdaniel8dd688b2015-01-22 10:16:09 -08001491 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(pipeline.getRenderTarget());
1492 this->flushStencil(pipeline.getStencil());
1493 this->flushScissor(pipeline.getScissorState(), glRT->getViewport(), glRT->origin());
vbuzinov3e77ba92015-09-30 23:02:06 -07001494 this->flushHWAAState(glRT, pipeline.isHWAntialiasState(), !pipeline.getStencil().isDisabled());
bsalomonbc3d0de2014-12-15 13:45:03 -08001495
1496 // This must come after textures are flushed because a texture may need
egdanield803f272015-03-18 13:01:52 -07001497 // to be msaa-resolved (which will modify bound FBO state).
halcanary96fcdcc2015-08-27 07:41:13 -07001498 this->flushRenderTarget(glRT, nullptr);
bsalomonbc3d0de2014-12-15 13:45:03 -08001499
1500 return true;
1501}
1502
joshualitt873ad0e2015-01-20 09:08:51 -08001503void GrGLGpu::setupGeometry(const GrPrimitiveProcessor& primProc,
bsalomone64eb572015-05-07 11:35:55 -07001504 const GrNonInstancedVertices& vertices,
bsalomonbc3d0de2014-12-15 13:45:03 -08001505 size_t* indexOffsetInBytes) {
1506 GrGLVertexBuffer* vbuf;
bsalomone64eb572015-05-07 11:35:55 -07001507 vbuf = (GrGLVertexBuffer*) vertices.vertexBuffer();
bsalomonbc3d0de2014-12-15 13:45:03 -08001508
1509 SkASSERT(vbuf);
1510 SkASSERT(!vbuf->isMapped());
1511
halcanary96fcdcc2015-08-27 07:41:13 -07001512 GrGLIndexBuffer* ibuf = nullptr;
bsalomone64eb572015-05-07 11:35:55 -07001513 if (vertices.isIndexed()) {
bsalomonbc3d0de2014-12-15 13:45:03 -08001514 SkASSERT(indexOffsetInBytes);
1515
1516 *indexOffsetInBytes = 0;
bsalomone64eb572015-05-07 11:35:55 -07001517 ibuf = (GrGLIndexBuffer*)vertices.indexBuffer();
bsalomonbc3d0de2014-12-15 13:45:03 -08001518
1519 SkASSERT(ibuf);
1520 SkASSERT(!ibuf->isMapped());
1521 *indexOffsetInBytes += ibuf->baseOffset();
1522 }
1523 GrGLAttribArrayState* attribState =
1524 fHWGeometryState.bindArrayAndBuffersToDraw(this, vbuf, ibuf);
1525
joshualitt873ad0e2015-01-20 09:08:51 -08001526 int vaCount = primProc.numAttribs();
joshualitt71c92602015-01-14 08:12:47 -08001527 if (vaCount > 0) {
bsalomonbc3d0de2014-12-15 13:45:03 -08001528
joshualitt873ad0e2015-01-20 09:08:51 -08001529 GrGLsizei stride = static_cast<GrGLsizei>(primProc.getVertexStride());
bsalomonbc3d0de2014-12-15 13:45:03 -08001530
bsalomone64eb572015-05-07 11:35:55 -07001531 size_t vertexOffsetInBytes = stride * vertices.startVertex();
bsalomonbc3d0de2014-12-15 13:45:03 -08001532
1533 vertexOffsetInBytes += vbuf->baseOffset();
1534
bsalomonbc3d0de2014-12-15 13:45:03 -08001535 uint32_t usedAttribArraysMask = 0;
1536 size_t offset = 0;
1537
1538 for (int attribIndex = 0; attribIndex < vaCount; attribIndex++) {
joshualitt873ad0e2015-01-20 09:08:51 -08001539 const GrGeometryProcessor::Attribute& attrib = primProc.getAttrib(attribIndex);
bsalomonbc3d0de2014-12-15 13:45:03 -08001540 usedAttribArraysMask |= (1 << attribIndex);
joshualitt71c92602015-01-14 08:12:47 -08001541 GrVertexAttribType attribType = attrib.fType;
bsalomonbc3d0de2014-12-15 13:45:03 -08001542 attribState->set(this,
1543 attribIndex,
bsalomon6df86402015-06-01 10:41:49 -07001544 vbuf->bufferID(),
bsalomonbc3d0de2014-12-15 13:45:03 -08001545 GrGLAttribTypeToLayout(attribType).fCount,
1546 GrGLAttribTypeToLayout(attribType).fType,
1547 GrGLAttribTypeToLayout(attribType).fNormalized,
1548 stride,
1549 reinterpret_cast<GrGLvoid*>(vertexOffsetInBytes + offset));
joshualitt71c92602015-01-14 08:12:47 -08001550 offset += attrib.fOffset;
bsalomonbc3d0de2014-12-15 13:45:03 -08001551 }
1552 attribState->disableUnusedArrays(this, usedAttribArraysMask);
1553 }
1554}
1555
joshualitt873ad0e2015-01-20 09:08:51 -08001556void GrGLGpu::buildProgramDesc(GrProgramDesc* desc,
1557 const GrPrimitiveProcessor& primProc,
joshualitt465283c2015-09-11 08:19:35 -07001558 const GrPipeline& pipeline) const {
1559 if (!GrGLProgramDescBuilder::Build(desc, primProc, pipeline, this)) {
bsalomonbc3d0de2014-12-15 13:45:03 -08001560 SkDEBUGFAIL("Failed to generate GL program descriptor");
1561 }
1562}
1563
joshualitt93316b92015-10-23 09:08:08 -07001564void GrGLGpu::bindBuffer(GrGLuint id, GrGLenum type) {
1565 this->handleDirtyContext();
1566 if (GR_GL_ARRAY_BUFFER == type) {
1567 this->bindVertexBuffer(id);
1568 } else {
1569 SkASSERT(GR_GL_ELEMENT_ARRAY_BUFFER == type);
1570 this->bindIndexBufferAndDefaultVertexArray(id);
1571 }
1572}
1573
1574void GrGLGpu::releaseBuffer(GrGLuint id, GrGLenum type) {
1575 this->handleDirtyContext();
1576 GL_CALL(DeleteBuffers(1, &id));
1577 if (GR_GL_ARRAY_BUFFER == type) {
1578 this->notifyVertexBufferDelete(id);
1579 } else {
1580 SkASSERT(GR_GL_ELEMENT_ARRAY_BUFFER == type);
1581 this->notifyIndexBufferDelete(id);
1582 }
1583}
1584
1585// GL_STREAM_DRAW triggers an optimization in Chromium's GPU process where a client's vertex buffer
1586// objects are implemented as client-side-arrays on tile-deferred architectures.
1587#define DYNAMIC_USAGE_PARAM GR_GL_STREAM_DRAW
1588
1589void* GrGLGpu::mapBuffer(GrGLuint id, GrGLenum type, bool dynamic, size_t currentSize,
1590 size_t requestedSize) {
1591 void* mapPtr = nullptr;
1592 // Handling dirty context is done in the bindBuffer call
1593 switch (this->glCaps().mapBufferType()) {
1594 case GrGLCaps::kNone_MapBufferType:
1595 break;
1596 case GrGLCaps::kMapBuffer_MapBufferType:
1597 this->bindBuffer(id, type);
1598 // Let driver know it can discard the old data
1599 if (GR_GL_USE_BUFFER_DATA_NULL_HINT || currentSize != requestedSize) {
1600 GL_CALL(BufferData(type, requestedSize, nullptr,
1601 dynamic ? DYNAMIC_USAGE_PARAM : GR_GL_STATIC_DRAW));
1602 }
1603 GL_CALL_RET(mapPtr, MapBuffer(type, GR_GL_WRITE_ONLY));
1604 break;
1605 case GrGLCaps::kMapBufferRange_MapBufferType: {
1606 this->bindBuffer(id, type);
1607 // Make sure the GL buffer size agrees with fDesc before mapping.
1608 if (currentSize != requestedSize) {
1609 GL_CALL(BufferData(type, requestedSize, nullptr,
1610 dynamic ? DYNAMIC_USAGE_PARAM : GR_GL_STATIC_DRAW));
1611 }
1612 static const GrGLbitfield kAccess = GR_GL_MAP_INVALIDATE_BUFFER_BIT |
1613 GR_GL_MAP_WRITE_BIT;
1614 GL_CALL_RET(mapPtr, MapBufferRange(type, 0, requestedSize, kAccess));
1615 break;
1616 }
1617 case GrGLCaps::kChromium_MapBufferType:
1618 this->bindBuffer(id, type);
1619 // Make sure the GL buffer size agrees with fDesc before mapping.
1620 if (currentSize != requestedSize) {
1621 GL_CALL(BufferData(type, requestedSize, nullptr,
1622 dynamic ? DYNAMIC_USAGE_PARAM : GR_GL_STATIC_DRAW));
1623 }
1624 GL_CALL_RET(mapPtr, MapBufferSubData(type, 0, requestedSize, GR_GL_WRITE_ONLY));
1625 break;
1626 }
1627 return mapPtr;
1628}
1629
1630void GrGLGpu::bufferData(GrGLuint id, GrGLenum type, bool dynamic, size_t currentSize,
1631 const void* src, size_t srcSizeInBytes) {
1632 SkASSERT(srcSizeInBytes <= currentSize);
1633 // bindbuffer handles dirty context
1634 this->bindBuffer(id, type);
1635 GrGLenum usage = dynamic ? DYNAMIC_USAGE_PARAM : GR_GL_STATIC_DRAW;
1636
1637#if GR_GL_USE_BUFFER_DATA_NULL_HINT
1638 if (currentSize == srcSizeInBytes) {
1639 GL_CALL(BufferData(type, (GrGLsizeiptr) srcSizeInBytes, src, usage));
1640 } else {
1641 // Before we call glBufferSubData we give the driver a hint using
1642 // glBufferData with nullptr. This makes the old buffer contents
1643 // inaccessible to future draws. The GPU may still be processing
1644 // draws that reference the old contents. With this hint it can
1645 // assign a different allocation for the new contents to avoid
1646 // flushing the gpu past draws consuming the old contents.
1647 // TODO I think we actually want to try calling bufferData here
1648 GL_CALL(BufferData(type, currentSize, nullptr, usage));
1649 GL_CALL(BufferSubData(type, 0, (GrGLsizeiptr) srcSizeInBytes, src));
1650 }
1651#else
1652 // Note that we're cheating on the size here. Currently no methods
1653 // allow a partial update that preserves contents of non-updated
1654 // portions of the buffer (map() does a glBufferData(..size, nullptr..))
1655 GL_CALL(BufferData(type, srcSizeInBytes, src, usage));
1656#endif
1657}
1658
1659void GrGLGpu::unmapBuffer(GrGLuint id, GrGLenum type, void* mapPtr) {
1660 // bind buffer handles the dirty context
1661 switch (this->glCaps().mapBufferType()) {
1662 case GrGLCaps::kNone_MapBufferType:
1663 SkDEBUGFAIL("Shouldn't get here.");
1664 return;
1665 case GrGLCaps::kMapBuffer_MapBufferType: // fall through
1666 case GrGLCaps::kMapBufferRange_MapBufferType:
1667 this->bindBuffer(id, type);
1668 GL_CALL(UnmapBuffer(type));
1669 break;
1670 case GrGLCaps::kChromium_MapBufferType:
1671 this->bindBuffer(id, type);
1672 GL_CALL(UnmapBufferSubData(mapPtr));
1673 break;
1674 }
1675}
1676
bsalomon861e1032014-12-16 07:33:49 -08001677void GrGLGpu::disableScissor() {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001678 if (kNo_TriState != fHWScissorSettings.fEnabled) {
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001679 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.coma3201942012-06-21 19:58:20 +00001680 fHWScissorSettings.fEnabled = kNo_TriState;
1681 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001682 }
1683}
1684
egdaniel51c8d402015-08-06 10:54:13 -07001685void GrGLGpu::onClear(GrRenderTarget* target, const SkIRect& rect, GrColor color) {
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001686 // parent class should never let us get here with no RT
bsalomon49f085d2014-09-05 13:34:00 -07001687 SkASSERT(target);
bsalomonb0bd4f62014-09-03 07:19:50 -07001688 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001689
egdaniel51c8d402015-08-06 10:54:13 -07001690 this->flushRenderTarget(glRT, &rect);
bsalomon3e791242014-12-17 13:43:13 -08001691 GrScissorState scissorState;
egdaniel51c8d402015-08-06 10:54:13 -07001692 scissorState.set(rect);
joshualitt77b13072014-10-27 14:51:01 -07001693 this->flushScissor(scissorState, glRT->getViewport(), glRT->origin());
bsalomon@google.com74b98712011-11-11 19:46:16 +00001694
1695 GrGLfloat r, g, b, a;
1696 static const GrGLfloat scale255 = 1.f / 255.f;
1697 a = GrColorUnpackA(color) * scale255;
1698 GrGLfloat scaleRGB = scale255;
bsalomon@google.com74b98712011-11-11 19:46:16 +00001699 r = GrColorUnpackR(color) * scaleRGB;
1700 g = GrColorUnpackG(color) * scaleRGB;
1701 b = GrColorUnpackB(color) * scaleRGB;
1702
1703 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00001704 fHWWriteToColor = kYes_TriState;
bsalomon@google.com74b98712011-11-11 19:46:16 +00001705 GL_CALL(ClearColor(r, g, b, a));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001706 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001707}
1708
bsalomon861e1032014-12-16 07:33:49 -08001709void GrGLGpu::discard(GrRenderTarget* renderTarget) {
bsalomon89c62982014-11-03 12:08:42 -08001710 SkASSERT(renderTarget);
commit-bot@chromium.org52ffbf62014-04-02 16:19:33 +00001711 if (!this->caps()->discardRenderTargetSupport()) {
1712 return;
1713 }
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +00001714
1715 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(renderTarget);
egdanield803f272015-03-18 13:01:52 -07001716 if (renderTarget->getUniqueID() != fHWBoundRenderTargetUniqueID) {
1717 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
1718 fStats.incRenderTargetBinds();
1719 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, glRT->renderFBOID()));
1720 }
commit-bot@chromium.org52ffbf62014-04-02 16:19:33 +00001721 switch (this->glCaps().invalidateFBType()) {
joshualitt58162332014-08-01 06:44:53 -07001722 case GrGLCaps::kNone_InvalidateFBType:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +00001723 SkFAIL("Should never get here.");
commit-bot@chromium.org52ffbf62014-04-02 16:19:33 +00001724 break;
1725 case GrGLCaps::kInvalidate_InvalidateFBType:
egdanield803f272015-03-18 13:01:52 -07001726 if (0 == glRT->renderFBOID()) {
commit-bot@chromium.org52ffbf62014-04-02 16:19:33 +00001727 // When rendering to the default framebuffer the legal values for attachments
1728 // are GL_COLOR, GL_DEPTH, GL_STENCIL, ... rather than the various FBO attachment
1729 // types.
1730 static const GrGLenum attachments[] = { GR_GL_COLOR };
egdanield803f272015-03-18 13:01:52 -07001731 GL_CALL(InvalidateFramebuffer(GR_GL_FRAMEBUFFER, SK_ARRAY_COUNT(attachments),
commit-bot@chromium.org52ffbf62014-04-02 16:19:33 +00001732 attachments));
1733 } else {
1734 static const GrGLenum attachments[] = { GR_GL_COLOR_ATTACHMENT0 };
egdanield803f272015-03-18 13:01:52 -07001735 GL_CALL(InvalidateFramebuffer(GR_GL_FRAMEBUFFER, SK_ARRAY_COUNT(attachments),
commit-bot@chromium.org52ffbf62014-04-02 16:19:33 +00001736 attachments));
1737 }
1738 break;
1739 case GrGLCaps::kDiscard_InvalidateFBType: {
egdanield803f272015-03-18 13:01:52 -07001740 if (0 == glRT->renderFBOID()) {
commit-bot@chromium.org4453e8b2014-04-16 14:33:27 +00001741 // When rendering to the default framebuffer the legal values for attachments
1742 // are GL_COLOR, GL_DEPTH, GL_STENCIL, ... rather than the various FBO attachment
1743 // types. See glDiscardFramebuffer() spec.
1744 static const GrGLenum attachments[] = { GR_GL_COLOR };
egdanield803f272015-03-18 13:01:52 -07001745 GL_CALL(DiscardFramebuffer(GR_GL_FRAMEBUFFER, SK_ARRAY_COUNT(attachments),
commit-bot@chromium.org4453e8b2014-04-16 14:33:27 +00001746 attachments));
1747 } else {
1748 static const GrGLenum attachments[] = { GR_GL_COLOR_ATTACHMENT0 };
egdanield803f272015-03-18 13:01:52 -07001749 GL_CALL(DiscardFramebuffer(GR_GL_FRAMEBUFFER, SK_ARRAY_COUNT(attachments),
commit-bot@chromium.org4453e8b2014-04-16 14:33:27 +00001750 attachments));
1751 }
commit-bot@chromium.org52ffbf62014-04-02 16:19:33 +00001752 break;
1753 }
1754 }
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +00001755 renderTarget->flagAsResolved();
1756}
1757
bsalomon861e1032014-12-16 07:33:49 -08001758void GrGLGpu::clearStencil(GrRenderTarget* target) {
halcanary96fcdcc2015-08-27 07:41:13 -07001759 if (nullptr == target) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001760 return;
1761 }
bsalomonb0bd4f62014-09-03 07:19:50 -07001762 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
egdanield803f272015-03-18 13:01:52 -07001763 this->flushRenderTarget(glRT, &SkIRect::EmptyIRect());
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001764
joshualitt77b13072014-10-27 14:51:01 -07001765 this->disableScissor();
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001766
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001767 GL_CALL(StencilMask(0xffffffff));
1768 GL_CALL(ClearStencil(0));
1769 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001770 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001771}
1772
bsalomon861e1032014-12-16 07:33:49 -08001773void GrGLGpu::onClearStencilClip(GrRenderTarget* target, const SkIRect& rect, bool insideClip) {
bsalomon49f085d2014-09-05 13:34:00 -07001774 SkASSERT(target);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001775
egdaniel8dc7c3a2015-04-16 11:22:42 -07001776 GrStencilAttachment* sb = target->renderTargetPriv().getStencilAttachment();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001777 // this should only be called internally when we know we have a
1778 // stencil buffer.
bsalomon6bc1b5f2015-02-23 09:06:38 -08001779 SkASSERT(sb);
1780 GrGLint stencilBitCount = sb->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001781#if 0
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001782 SkASSERT(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00001783 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001784#else
1785 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001786 // ANGLE a partial stencil mask will cause clears to be
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001787 // turned into draws. Our contract on GrDrawTarget says that
1788 // changing the clip between stencil passes may or may not
1789 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00001790 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001791#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001792 GrGLint value;
1793 if (insideClip) {
1794 value = (1 << (stencilBitCount - 1));
1795 } else {
1796 value = 0;
1797 }
bsalomonb0bd4f62014-09-03 07:19:50 -07001798 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
egdanield803f272015-03-18 13:01:52 -07001799 this->flushRenderTarget(glRT, &SkIRect::EmptyIRect());
bsalomon@google.coma3201942012-06-21 19:58:20 +00001800
bsalomon3e791242014-12-17 13:43:13 -08001801 GrScissorState scissorState;
robertphillipse85a32d2015-02-10 08:16:55 -08001802 scissorState.set(rect);
joshualitt77b13072014-10-27 14:51:01 -07001803 this->flushScissor(scissorState, glRT->getViewport(), glRT->origin());
bsalomon@google.coma3201942012-06-21 19:58:20 +00001804
caryclark@google.comcf6285b2012-06-06 12:09:01 +00001805 GL_CALL(StencilMask((uint32_t) clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001806 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001807 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001808 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001809}
1810
bsalomon39826022015-07-23 08:07:21 -07001811static bool read_pixels_pays_for_y_flip(GrRenderTarget* renderTarget, const GrGLCaps& caps,
1812 int width, int height, GrPixelConfig config,
1813 size_t rowBytes) {
1814 // If this render target is already TopLeft, we don't need to flip.
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001815 if (kTopLeft_GrSurfaceOrigin == renderTarget->origin()) {
1816 return false;
1817 }
1818
bsalomon494aa592015-07-23 11:45:02 -07001819 // If the read is really small or smaller than the min texture size, don't force a draw.
1820 int minSize = SkTMax(32, caps.minTextureSize());
1821 if (width < minSize || height < minSize) {
1822 return false;
1823 }
1824
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001825 // if GL can do the flip then we'll never pay for it.
bsalomon39826022015-07-23 08:07:21 -07001826 if (caps.packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001827 return false;
1828 }
1829
1830 // If we have to do memcpy to handle non-trim rowBytes then we
bsalomon@google.com7107fa72011-11-10 14:54:14 +00001831 // get the flip for free. Otherwise it costs.
bsalomon39826022015-07-23 08:07:21 -07001832 // Note that we're assuming that 0 rowBytes has already been handled and that the width has been
1833 // clipped.
1834 return caps.packRowLengthSupport() || GrBytesPerPixel(config) * width == rowBytes;
1835}
1836
bsalomonf0674512015-07-28 13:26:15 -07001837bool GrGLGpu::onGetReadPixelsInfo(GrSurface* srcSurface, int width, int height, size_t rowBytes,
1838 GrPixelConfig readConfig, DrawPreference* drawPreference,
1839 ReadPixelTempDrawInfo* tempDrawInfo) {
bsalomon6cb3cbe2015-07-30 07:34:27 -07001840 // This subclass can only read pixels from a render target. We could use glTexSubImage2D on
1841 // GL versions that support it but we don't today.
1842 if (!srcSurface->asRenderTarget()) {
1843 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
1844 }
1845
bsalomon16921ec2015-07-30 15:34:56 -07001846 if (GrPixelConfigIsSRGB(srcSurface->config()) != GrPixelConfigIsSRGB(readConfig)) {
1847 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
1848 }
1849
bsalomon39826022015-07-23 08:07:21 -07001850 tempDrawInfo->fSwapRAndB = false;
1851
1852 // These settings we will always want if a temp draw is performed. The config is set below
1853 // depending on whether we want to do a R/B swap or not.
1854 tempDrawInfo->fTempSurfaceDesc.fFlags = kRenderTarget_GrSurfaceFlag;
1855 tempDrawInfo->fTempSurfaceDesc.fWidth = width;
1856 tempDrawInfo->fTempSurfaceDesc.fHeight = height;
1857 tempDrawInfo->fTempSurfaceDesc.fSampleCnt = 0;
1858 tempDrawInfo->fTempSurfaceDesc.fOrigin = kTopLeft_GrSurfaceOrigin; // no CPU y-flip for TL.
bsalomon88c7b982015-07-31 11:20:16 -07001859 tempDrawInfo->fUseExactScratch = this->glCaps().partialFBOReadIsSlow() &&
bsalomon494aa592015-07-23 11:45:02 -07001860 width >= this->caps()->minTextureSize() &&
1861 height >= this->caps()->minTextureSize();
bsalomon39826022015-07-23 08:07:21 -07001862
1863 // Start off assuming that any temp draw should be to the readConfig, then check if that will
1864 // be inefficient.
1865 GrPixelConfig srcConfig = srcSurface->config();
1866 tempDrawInfo->fTempSurfaceDesc.fConfig = readConfig;
1867
bsalomon88c7b982015-07-31 11:20:16 -07001868 if (this->glCaps().rgba8888PixelsOpsAreSlow() && kRGBA_8888_GrPixelConfig == readConfig) {
bsalomon39826022015-07-23 08:07:21 -07001869 tempDrawInfo->fTempSurfaceDesc.fConfig = kBGRA_8888_GrPixelConfig;
bsalomonb411b3b2015-07-31 09:34:24 -07001870 tempDrawInfo->fSwapRAndB = true;
1871 ElevateDrawPreference(drawPreference, kGpuPrefersDraw_DrawPreference);
bsalomon39826022015-07-23 08:07:21 -07001872 } else if (kMesa_GrGLDriver == this->glContext().driver() &&
1873 GrBytesPerPixel(readConfig) == 4 &&
1874 GrPixelConfigSwapRAndB(readConfig) == srcConfig) {
1875 // Mesa 3D takes a slow path on when reading back BGRA from an RGBA surface and vice-versa.
1876 // Better to do a draw with a R/B swap and then read as the original config.
1877 tempDrawInfo->fTempSurfaceDesc.fConfig = srcConfig;
1878 tempDrawInfo->fSwapRAndB = true;
bsalomonf0674512015-07-28 13:26:15 -07001879 ElevateDrawPreference(drawPreference, kGpuPrefersDraw_DrawPreference);
bsalomon39826022015-07-23 08:07:21 -07001880 } else if (readConfig == kBGRA_8888_GrPixelConfig &&
1881 !this->glCaps().readPixelsSupported(this->glInterface(), GR_GL_BGRA,
1882 GR_GL_UNSIGNED_BYTE, srcConfig)) {
1883 tempDrawInfo->fTempSurfaceDesc.fConfig = kRGBA_8888_GrPixelConfig;
1884 tempDrawInfo->fSwapRAndB = true;
bsalomonf0674512015-07-28 13:26:15 -07001885 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
bsalomon39826022015-07-23 08:07:21 -07001886 }
1887
1888 GrRenderTarget* srcAsRT = srcSurface->asRenderTarget();
1889 if (!srcAsRT) {
bsalomonf0674512015-07-28 13:26:15 -07001890 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
bsalomon39826022015-07-23 08:07:21 -07001891 } else if (read_pixels_pays_for_y_flip(srcAsRT, this->glCaps(), width, height, readConfig,
1892 rowBytes)) {
bsalomonf0674512015-07-28 13:26:15 -07001893 ElevateDrawPreference(drawPreference, kGpuPrefersDraw_DrawPreference);
bsalomon39826022015-07-23 08:07:21 -07001894 }
1895
bsalomon39826022015-07-23 08:07:21 -07001896 return true;
bsalomon@google.comc4364992011-11-07 15:54:49 +00001897}
1898
bsalomon6cb3cbe2015-07-30 07:34:27 -07001899bool GrGLGpu::onReadPixels(GrSurface* surface,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001900 int left, int top,
1901 int width, int height,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001902 GrPixelConfig config,
1903 void* buffer,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001904 size_t rowBytes) {
bsalomon6cb3cbe2015-07-30 07:34:27 -07001905 SkASSERT(surface);
bsalomon39826022015-07-23 08:07:21 -07001906
bsalomon6cb3cbe2015-07-30 07:34:27 -07001907 GrGLRenderTarget* tgt = static_cast<GrGLRenderTarget*>(surface->asRenderTarget());
1908 if (!tgt) {
1909 return false;
1910 }
1911
bsalomon16921ec2015-07-30 15:34:56 -07001912 // OpenGL doesn't do sRGB <-> linear conversions when reading and writing pixels.
1913 if (GrPixelConfigIsSRGB(surface->config()) != GrPixelConfigIsSRGB(config)) {
1914 return false;
1915 }
1916
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +00001917 GrGLenum format = 0;
1918 GrGLenum type = 0;
bsalomon6cb3cbe2015-07-30 07:34:27 -07001919 bool flipY = kBottomLeft_GrSurfaceOrigin == surface->origin();
halcanary96fcdcc2015-08-27 07:41:13 -07001920 if (!this->configToGLFormats(config, false, nullptr, &format, &type)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001921 return false;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001922 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001923
bsalomon16921ec2015-07-30 15:34:56 -07001924 // glReadPixels does not allow GL_SRGB_ALPHA. Instead use GL_RGBA. This will not trigger a
1925 // conversion when the src is srgb.
1926 if (GR_GL_SRGB_ALPHA == format) {
1927 format = GR_GL_RGBA;
1928 }
1929
bsalomon@google.comc6980972011-11-02 19:57:21 +00001930 // resolve the render target if necessary
egdanield803f272015-03-18 13:01:52 -07001931 switch (tgt->getResolveType()) {
1932 case GrGLRenderTarget::kCantResolve_ResolveType:
1933 return false;
1934 case GrGLRenderTarget::kAutoResolves_ResolveType:
bsalomon6cb3cbe2015-07-30 07:34:27 -07001935 this->flushRenderTarget(tgt, &SkIRect::EmptyIRect());
egdanield803f272015-03-18 13:01:52 -07001936 break;
1937 case GrGLRenderTarget::kCanResolve_ResolveType:
1938 this->onResolveRenderTarget(tgt);
1939 // we don't track the state of the READ FBO ID.
1940 fStats.incRenderTargetBinds();
1941 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1942 tgt->textureFBOID()));
1943 break;
1944 default:
1945 SkFAIL("Unknown resolve type");
reed@google.comac10a2d2010-12-22 21:39:39 +00001946 }
1947
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001948 const GrGLIRect& glvp = tgt->getViewport();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001949
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001950 // the read rect is viewport-relative
1951 GrGLIRect readRect;
bsalomon6cb3cbe2015-07-30 07:34:27 -07001952 readRect.setRelativeTo(glvp, left, top, width, height, tgt->origin());
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001953
egdaniel6d901da2015-07-30 12:02:15 -07001954 size_t tightRowBytes = GrBytesPerPixel(config) * width;
1955
bsalomon@google.comc6980972011-11-02 19:57:21 +00001956 size_t readDstRowBytes = tightRowBytes;
1957 void* readDst = buffer;
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001958
bsalomon@google.comc6980972011-11-02 19:57:21 +00001959 // determine if GL can read using the passed rowBytes or if we need
1960 // a scratch buffer.
joshualitt29f86792015-05-29 08:06:48 -07001961 SkAutoSMalloc<32 * sizeof(GrColor)> scratch;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001962 if (rowBytes != tightRowBytes) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001963 if (this->glCaps().packRowLengthSupport()) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001964 SkASSERT(!(rowBytes % sizeof(GrColor)));
skia.committer@gmail.com4677acc2013-10-17 07:02:33 +00001965 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH,
robertphillips@google.come9cd27d2013-10-16 17:48:11 +00001966 static_cast<GrGLint>(rowBytes / sizeof(GrColor))));
bsalomon@google.comc6980972011-11-02 19:57:21 +00001967 readDstRowBytes = rowBytes;
1968 } else {
1969 scratch.reset(tightRowBytes * height);
1970 readDst = scratch.get();
1971 }
1972 }
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001973 if (flipY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001974 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 1));
1975 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001976 GL_CALL(ReadPixels(readRect.fLeft, readRect.fBottom,
1977 readRect.fWidth, readRect.fHeight,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001978 format, type, readDst));
1979 if (readDstRowBytes != tightRowBytes) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001980 SkASSERT(this->glCaps().packRowLengthSupport());
bsalomon@google.comc6980972011-11-02 19:57:21 +00001981 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
1982 }
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001983 if (flipY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001984 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 0));
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001985 flipY = false;
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001986 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001987
1988 // now reverse the order of the rows, since GL's are bottom-to-top, but our
bsalomon@google.comc6980972011-11-02 19:57:21 +00001989 // API presents top-to-bottom. We must preserve the padding contents. Note
1990 // that the above readPixels did not overwrite the padding.
1991 if (readDst == buffer) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001992 SkASSERT(rowBytes == readDstRowBytes);
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001993 if (flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001994 scratch.reset(tightRowBytes);
1995 void* tmpRow = scratch.get();
1996 // flip y in-place by rows
1997 const int halfY = height >> 1;
1998 char* top = reinterpret_cast<char*>(buffer);
1999 char* bottom = top + (height - 1) * rowBytes;
2000 for (int y = 0; y < halfY; y++) {
2001 memcpy(tmpRow, top, tightRowBytes);
2002 memcpy(top, bottom, tightRowBytes);
2003 memcpy(bottom, tmpRow, tightRowBytes);
2004 top += rowBytes;
2005 bottom -= rowBytes;
2006 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00002007 }
2008 } else {
egdanield803f272015-03-18 13:01:52 -07002009 SkASSERT(readDst != buffer); SkASSERT(rowBytes != tightRowBytes);
bsalomon@google.comc6980972011-11-02 19:57:21 +00002010 // copy from readDst to buffer while flipping y
caryclark@google.comcf6285b2012-06-06 12:09:01 +00002011 // const int halfY = height >> 1;
bsalomon@google.comc6980972011-11-02 19:57:21 +00002012 const char* src = reinterpret_cast<const char*>(readDst);
bsalomon@google.comc4364992011-11-07 15:54:49 +00002013 char* dst = reinterpret_cast<char*>(buffer);
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002014 if (flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002015 dst += (height-1) * rowBytes;
2016 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00002017 for (int y = 0; y < height; y++) {
2018 memcpy(dst, src, tightRowBytes);
2019 src += readDstRowBytes;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002020 if (!flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002021 dst += rowBytes;
2022 } else {
2023 dst -= rowBytes;
2024 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002025 }
2026 }
2027 return true;
2028}
2029
vbuzinov3e77ba92015-09-30 23:02:06 -07002030void GrGLGpu::setColocatedSampleLocations(GrRenderTarget* rt, bool useColocatedSampleLocations) {
2031 GrGLRenderTarget* target = static_cast<GrGLRenderTarget*>(rt->asRenderTarget());
2032 SkASSERT(0 != target->renderFBOID());
2033
2034 if (!rt->isStencilBufferMultisampled() ||
2035 useColocatedSampleLocations == target->usesColocatedSampleLocations()) {
2036 return;
2037 }
2038
2039 GL_CALL(NamedFramebufferParameteri(target->renderFBOID(),
2040 GR_GL_FRAMEBUFFER_PROGRAMMABLE_SAMPLE_LOCATIONS,
2041 useColocatedSampleLocations));
2042
2043 target->flagAsUsingColocatedSampleLocations(useColocatedSampleLocations);
2044}
2045
egdanield803f272015-03-18 13:01:52 -07002046void GrGLGpu::flushRenderTarget(GrGLRenderTarget* target, const SkIRect* bound) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002047
egdanield803f272015-03-18 13:01:52 -07002048 SkASSERT(target);
bsalomon6ba6fa12015-03-04 11:57:37 -08002049
egdanield803f272015-03-18 13:01:52 -07002050 uint32_t rtID = target->getUniqueID();
2051 if (fHWBoundRenderTargetUniqueID != rtID) {
bsalomon1e0bf7e2015-03-14 12:08:51 -07002052 fStats.incRenderTargetBinds();
egdanield803f272015-03-18 13:01:52 -07002053 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, target->renderFBOID()));
2054#ifdef SK_DEBUG
2055 // don't do this check in Chromium -- this is causing
2056 // lots of repeated command buffer flushes when the compositor is
2057 // rendering with Ganesh, which is really slow; even too slow for
2058 // Debug mode.
cdalton1acea862015-06-02 13:05:52 -07002059 if (kChromium_GrGLDriver != this->glContext().driver()) {
egdanield803f272015-03-18 13:01:52 -07002060 GrGLenum status;
2061 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
2062 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
2063 SkDebugf("GrGLGpu::flushRenderTarget glCheckFramebufferStatus %x\n", status);
2064 }
bsalomon160f24c2015-03-17 15:55:42 -07002065 }
egdanield803f272015-03-18 13:01:52 -07002066#endif
2067 fHWBoundRenderTargetUniqueID = rtID;
2068 const GrGLIRect& vp = target->getViewport();
2069 if (fHWViewport != vp) {
2070 vp.pushToGLViewport(this->glInterface());
2071 fHWViewport = vp;
bsalomon160f24c2015-03-17 15:55:42 -07002072 }
bsalomon16921ec2015-07-30 15:34:56 -07002073 if (this->glCaps().srgbWriteControl()) {
2074 bool enableSRGBWrite = GrPixelConfigIsSRGB(target->config());
2075 if (enableSRGBWrite && kYes_TriState != fHWSRGBFramebuffer) {
2076 GL_CALL(Enable(GR_GL_FRAMEBUFFER_SRGB));
2077 fHWSRGBFramebuffer = kYes_TriState;
2078 } else if (!enableSRGBWrite && kNo_TriState != fHWSRGBFramebuffer) {
2079 GL_CALL(Disable(GR_GL_FRAMEBUFFER_SRGB));
2080 fHWSRGBFramebuffer = kNo_TriState;
2081 }
2082 }
bsalomon5cd020f2015-03-17 12:46:56 -07002083 }
halcanary96fcdcc2015-08-27 07:41:13 -07002084 if (nullptr == bound || !bound->isEmpty()) {
egdanield803f272015-03-18 13:01:52 -07002085 target->flagAsNeedingResolve(bound);
2086 }
2087
2088 GrTexture *texture = target->asTexture();
2089 if (texture) {
2090 texture->texturePriv().dirtyMipMaps(true);
2091 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002092}
2093
twiz@google.com0f31ca72011-03-18 17:38:11 +00002094GrGLenum gPrimitiveType2GLMode[] = {
2095 GR_GL_TRIANGLES,
2096 GR_GL_TRIANGLE_STRIP,
2097 GR_GL_TRIANGLE_FAN,
2098 GR_GL_POINTS,
2099 GR_GL_LINES,
2100 GR_GL_LINE_STRIP
reed@google.comac10a2d2010-12-22 21:39:39 +00002101};
2102
bsalomon@google.comd302f142011-03-03 13:54:13 +00002103#define SWAP_PER_DRAW 0
2104
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00002105#if SWAP_PER_DRAW
commit-bot@chromium.org43823302013-09-25 20:57:51 +00002106 #if defined(SK_BUILD_FOR_MAC)
bsalomon@google.comd302f142011-03-03 13:54:13 +00002107 #include <AGL/agl.h>
commit-bot@chromium.org43823302013-09-25 20:57:51 +00002108 #elif defined(SK_BUILD_FOR_WIN32)
bsalomon@google.comce7357d2012-06-25 17:49:25 +00002109 #include <gl/GL.h>
bsalomon@google.comd302f142011-03-03 13:54:13 +00002110 void SwapBuf() {
2111 DWORD procID = GetCurrentProcessId();
2112 HWND hwnd = GetTopWindow(GetDesktopWindow());
2113 while(hwnd) {
2114 DWORD wndProcID = 0;
2115 GetWindowThreadProcessId(hwnd, &wndProcID);
2116 if(wndProcID == procID) {
2117 SwapBuffers(GetDC(hwnd));
2118 }
2119 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
2120 }
2121 }
2122 #endif
2123#endif
2124
bsalomone64eb572015-05-07 11:35:55 -07002125void GrGLGpu::onDraw(const DrawArgs& args, const GrNonInstancedVertices& vertices) {
cdaltond0a840d2015-03-16 17:19:58 -07002126 if (!this->flushGLState(args)) {
bsalomond95263c2014-12-16 13:05:12 -08002127 return;
2128 }
2129
joshualitt873ad0e2015-01-20 09:08:51 -08002130 size_t indexOffsetInBytes = 0;
bsalomoncb8979d2015-05-05 09:51:38 -07002131 this->setupGeometry(*args.fPrimitiveProcessor, vertices, &indexOffsetInBytes);
reed@google.comac10a2d2010-12-22 21:39:39 +00002132
bsalomoncb8979d2015-05-05 09:51:38 -07002133 SkASSERT((size_t)vertices.primitiveType() < SK_ARRAY_COUNT(gPrimitiveType2GLMode));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002134
bsalomoncb8979d2015-05-05 09:51:38 -07002135 if (vertices.isIndexed()) {
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00002136 GrGLvoid* indices =
bsalomoncb8979d2015-05-05 09:51:38 -07002137 reinterpret_cast<GrGLvoid*>(indexOffsetInBytes + sizeof(uint16_t) *
2138 vertices.startIndex());
bsalomon@google.com74749cd2013-01-30 16:12:41 +00002139 // info.startVertex() was accounted for by setupGeometry.
bsalomoncb8979d2015-05-05 09:51:38 -07002140 GL_CALL(DrawElements(gPrimitiveType2GLMode[vertices.primitiveType()],
2141 vertices.indexCount(),
bsalomon@google.com74749cd2013-01-30 16:12:41 +00002142 GR_GL_UNSIGNED_SHORT,
2143 indices));
2144 } else {
2145 // Pass 0 for parameter first. We have to adjust glVertexAttribPointer() to account for
2146 // startVertex in the DrawElements case. So we always rely on setupGeometry to have
2147 // accounted for startVertex.
bsalomoncb8979d2015-05-05 09:51:38 -07002148 GL_CALL(DrawArrays(gPrimitiveType2GLMode[vertices.primitiveType()], 0,
2149 vertices.vertexCount()));
bsalomon@google.com74749cd2013-01-30 16:12:41 +00002150 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00002151#if SWAP_PER_DRAW
2152 glFlush();
commit-bot@chromium.org43823302013-09-25 20:57:51 +00002153 #if defined(SK_BUILD_FOR_MAC)
bsalomon@google.comd302f142011-03-03 13:54:13 +00002154 aglSwapBuffers(aglGetCurrentContext());
2155 int set_a_break_pt_here = 9;
2156 aglSwapBuffers(aglGetCurrentContext());
commit-bot@chromium.org43823302013-09-25 20:57:51 +00002157 #elif defined(SK_BUILD_FOR_WIN32)
bsalomon@google.comd302f142011-03-03 13:54:13 +00002158 SwapBuf();
2159 int set_a_break_pt_here = 9;
2160 SwapBuf();
2161 #endif
2162#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00002163}
2164
bsalomon861e1032014-12-16 07:33:49 -08002165void GrGLGpu::onResolveRenderTarget(GrRenderTarget* target) {
bsalomon@google.com75f9f252012-01-31 13:35:56 +00002166 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00002167 if (rt->needsResolve()) {
bsalomon@google.com347c3822013-05-01 20:10:01 +00002168 // Some extensions automatically resolves the texture when it is read.
2169 if (this->glCaps().usesMSAARenderBuffers()) {
egdanield803f272015-03-18 13:01:52 -07002170 SkASSERT(rt->textureFBOID() != rt->renderFBOID());
2171 fStats.incRenderTargetBinds();
2172 fStats.incRenderTargetBinds();
2173 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER, rt->renderFBOID()));
2174 GL_CALL(BindFramebuffer(GR_GL_DRAW_FRAMEBUFFER, rt->textureFBOID()));
2175 // make sure we go through flushRenderTarget() since we've modified
2176 // the bound DRAW FBO ID.
2177 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002178 const GrGLIRect& vp = rt->getViewport();
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00002179 const SkIRect dirtyRect = rt->getResolveRect();
reed@google.comac10a2d2010-12-22 21:39:39 +00002180
bsalomon@google.com347c3822013-05-01 20:10:01 +00002181 if (GrGLCaps::kES_Apple_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002182 // Apple's extension uses the scissor as the blit bounds.
bsalomon3e791242014-12-17 13:43:13 -08002183 GrScissorState scissorState;
robertphillipse85a32d2015-02-10 08:16:55 -08002184 scissorState.set(dirtyRect);
2185 this->flushScissor(scissorState, vp, rt->origin());
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002186 GL_CALL(ResolveMultisampleFramebuffer());
2187 } else {
robertphillipse85a32d2015-02-10 08:16:55 -08002188 GrGLIRect r;
2189 r.setRelativeTo(vp, dirtyRect.fLeft, dirtyRect.fTop,
2190 dirtyRect.width(), dirtyRect.height(), target->origin());
2191
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002192 int right = r.fLeft + r.fWidth;
2193 int top = r.fBottom + r.fHeight;
derekf8c8f71a2014-09-16 06:24:57 -07002194
2195 // BlitFrameBuffer respects the scissor, so disable it.
joshualitt77b13072014-10-27 14:51:01 -07002196 this->disableScissor();
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002197 GL_CALL(BlitFramebuffer(r.fLeft, r.fBottom, right, top,
2198 r.fLeft, r.fBottom, right, top,
2199 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00002200 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002201 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00002202 rt->flagAsResolved();
reed@google.comac10a2d2010-12-22 21:39:39 +00002203 }
2204}
2205
bsalomon@google.com411dad02012-06-05 20:24:20 +00002206namespace {
bsalomon@google.comd302f142011-03-03 13:54:13 +00002207
bsalomon@google.com411dad02012-06-05 20:24:20 +00002208
2209GrGLenum gr_to_gl_stencil_op(GrStencilOp op) {
2210 static const GrGLenum gTable[] = {
2211 GR_GL_KEEP, // kKeep_StencilOp
2212 GR_GL_REPLACE, // kReplace_StencilOp
2213 GR_GL_INCR_WRAP, // kIncWrap_StencilOp
2214 GR_GL_INCR, // kIncClamp_StencilOp
2215 GR_GL_DECR_WRAP, // kDecWrap_StencilOp
2216 GR_GL_DECR, // kDecClamp_StencilOp
2217 GR_GL_ZERO, // kZero_StencilOp
2218 GR_GL_INVERT, // kInvert_StencilOp
2219 };
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +00002220 GR_STATIC_ASSERT(SK_ARRAY_COUNT(gTable) == kStencilOpCount);
bsalomon@google.com411dad02012-06-05 20:24:20 +00002221 GR_STATIC_ASSERT(0 == kKeep_StencilOp);
2222 GR_STATIC_ASSERT(1 == kReplace_StencilOp);
2223 GR_STATIC_ASSERT(2 == kIncWrap_StencilOp);
2224 GR_STATIC_ASSERT(3 == kIncClamp_StencilOp);
2225 GR_STATIC_ASSERT(4 == kDecWrap_StencilOp);
2226 GR_STATIC_ASSERT(5 == kDecClamp_StencilOp);
2227 GR_STATIC_ASSERT(6 == kZero_StencilOp);
2228 GR_STATIC_ASSERT(7 == kInvert_StencilOp);
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002229 SkASSERT((unsigned) op < kStencilOpCount);
bsalomon@google.com411dad02012-06-05 20:24:20 +00002230 return gTable[op];
2231}
2232
2233void set_gl_stencil(const GrGLInterface* gl,
bsalomon@google.coma3201942012-06-21 19:58:20 +00002234 const GrStencilSettings& settings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00002235 GrGLenum glFace,
bsalomon@google.coma3201942012-06-21 19:58:20 +00002236 GrStencilSettings::Face grFace) {
kkinnunenccdaa042014-08-20 01:36:23 -07002237 GrGLenum glFunc = GrToGLStencilFunc(settings.func(grFace));
bsalomon@google.coma3201942012-06-21 19:58:20 +00002238 GrGLenum glFailOp = gr_to_gl_stencil_op(settings.failOp(grFace));
2239 GrGLenum glPassOp = gr_to_gl_stencil_op(settings.passOp(grFace));
2240
2241 GrGLint ref = settings.funcRef(grFace);
2242 GrGLint mask = settings.funcMask(grFace);
2243 GrGLint writeMask = settings.writeMask(grFace);
bsalomon@google.com411dad02012-06-05 20:24:20 +00002244
2245 if (GR_GL_FRONT_AND_BACK == glFace) {
2246 // we call the combined func just in case separate stencil is not
2247 // supported.
2248 GR_GL_CALL(gl, StencilFunc(glFunc, ref, mask));
2249 GR_GL_CALL(gl, StencilMask(writeMask));
vbuzinovc5d58f02015-08-21 05:24:24 -07002250 GR_GL_CALL(gl, StencilOp(glFailOp, GR_GL_KEEP, glPassOp));
bsalomon@google.com411dad02012-06-05 20:24:20 +00002251 } else {
2252 GR_GL_CALL(gl, StencilFuncSeparate(glFace, glFunc, ref, mask));
2253 GR_GL_CALL(gl, StencilMaskSeparate(glFace, writeMask));
vbuzinovc5d58f02015-08-21 05:24:24 -07002254 GR_GL_CALL(gl, StencilOpSeparate(glFace, glFailOp, GR_GL_KEEP, glPassOp));
bsalomon@google.com411dad02012-06-05 20:24:20 +00002255 }
2256}
2257}
bsalomon@google.comd302f142011-03-03 13:54:13 +00002258
bsalomon3e791242014-12-17 13:43:13 -08002259void GrGLGpu::flushStencil(const GrStencilSettings& stencilSettings) {
2260 if (fHWStencilSettings != stencilSettings) {
joshualitta58fe352014-10-27 08:39:00 -07002261 if (stencilSettings.isDisabled()) {
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00002262 if (kNo_TriState != fHWStencilTestEnabled) {
2263 GL_CALL(Disable(GR_GL_STENCIL_TEST));
2264 fHWStencilTestEnabled = kNo_TriState;
2265 }
2266 } else {
2267 if (kYes_TriState != fHWStencilTestEnabled) {
2268 GL_CALL(Enable(GR_GL_STENCIL_TEST));
2269 fHWStencilTestEnabled = kYes_TriState;
2270 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00002271 }
joshualitta58fe352014-10-27 08:39:00 -07002272 if (!stencilSettings.isDisabled()) {
bsalomon@google.combcce8922013-03-25 15:38:39 +00002273 if (this->caps()->twoSidedStencilSupport()) {
bsalomon@google.com411dad02012-06-05 20:24:20 +00002274 set_gl_stencil(this->glInterface(),
joshualitta58fe352014-10-27 08:39:00 -07002275 stencilSettings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00002276 GR_GL_FRONT,
bsalomon@google.coma3201942012-06-21 19:58:20 +00002277 GrStencilSettings::kFront_Face);
bsalomon@google.com411dad02012-06-05 20:24:20 +00002278 set_gl_stencil(this->glInterface(),
joshualitta58fe352014-10-27 08:39:00 -07002279 stencilSettings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00002280 GR_GL_BACK,
bsalomon@google.coma3201942012-06-21 19:58:20 +00002281 GrStencilSettings::kBack_Face);
bsalomon@google.comd302f142011-03-03 13:54:13 +00002282 } else {
bsalomon@google.com411dad02012-06-05 20:24:20 +00002283 set_gl_stencil(this->glInterface(),
joshualitta58fe352014-10-27 08:39:00 -07002284 stencilSettings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00002285 GR_GL_FRONT_AND_BACK,
bsalomon@google.coma3201942012-06-21 19:58:20 +00002286 GrStencilSettings::kFront_Face);
bsalomon@google.comd302f142011-03-03 13:54:13 +00002287 }
2288 }
joshualitta58fe352014-10-27 08:39:00 -07002289 fHWStencilSettings = stencilSettings;
reed@google.comac10a2d2010-12-22 21:39:39 +00002290 }
2291}
2292
vbuzinov3e77ba92015-09-30 23:02:06 -07002293void GrGLGpu::flushHWAAState(GrRenderTarget* rt, bool useHWAA, bool stencilEnabled) {
vbuzinovdded6962015-06-12 08:59:45 -07002294 SkASSERT(!useHWAA || rt->isStencilBufferMultisampled());
bsalomon@google.com202d1392013-03-19 18:58:08 +00002295
vbuzinov3e77ba92015-09-30 23:02:06 -07002296 if (rt->hasMixedSamples() && stencilEnabled &&
2297 this->glCaps().glslCaps()->programmableSampleLocationsSupport()) {
2298 if (useHWAA) {
2299 this->setColocatedSampleLocations(rt, false);
2300 } else {
2301 this->setColocatedSampleLocations(rt, true);
2302 }
2303 useHWAA = true;
2304 }
2305
cdalton0edea2c2015-05-21 08:27:44 -07002306 if (this->glCaps().multisampleDisableSupport()) {
cdaltond0a840d2015-03-16 17:19:58 -07002307 if (useHWAA) {
2308 if (kYes_TriState != fMSAAEnabled) {
2309 GL_CALL(Enable(GR_GL_MULTISAMPLE));
2310 fMSAAEnabled = kYes_TriState;
2311 }
2312 } else {
2313 if (kNo_TriState != fMSAAEnabled) {
2314 GL_CALL(Disable(GR_GL_MULTISAMPLE));
2315 fMSAAEnabled = kNo_TriState;
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00002316 }
2317 }
2318 }
2319}
2320
egdaniel080e6732014-12-22 07:35:52 -08002321void GrGLGpu::flushBlend(const GrXferProcessor::BlendInfo& blendInfo) {
egdanielb414f252014-07-29 13:15:47 -07002322 // Any optimization to disable blending should have already been applied and
cdalton8917d622015-05-06 13:40:21 -07002323 // tweaked the equation to "add" or "subtract", and the coeffs to (1, 0).
bsalomonf7cc8772015-05-11 11:21:14 -07002324
cdalton8917d622015-05-06 13:40:21 -07002325 GrBlendEquation equation = blendInfo.fEquation;
egdanielc2304142014-12-11 13:15:13 -08002326 GrBlendCoeff srcCoeff = blendInfo.fSrcBlend;
2327 GrBlendCoeff dstCoeff = blendInfo.fDstBlend;
cdalton8917d622015-05-06 13:40:21 -07002328 bool blendOff = (kAdd_GrBlendEquation == equation || kSubtract_GrBlendEquation == equation) &&
2329 kOne_GrBlendCoeff == srcCoeff && kZero_GrBlendCoeff == dstCoeff;
egdanielb414f252014-07-29 13:15:47 -07002330 if (blendOff) {
2331 if (kNo_TriState != fHWBlendState.fEnabled) {
2332 GL_CALL(Disable(GR_GL_BLEND));
joel.liang9764c402015-07-09 19:46:18 -07002333
2334 // Workaround for the ARM KHR_blend_equation_advanced blacklist issue
2335 // https://code.google.com/p/skia/issues/detail?id=3943
2336 if (kARM_GrGLVendor == this->ctxInfo().vendor() &&
2337 GrBlendEquationIsAdvanced(fHWBlendState.fEquation)) {
2338 SkASSERT(this->caps()->advancedBlendEquationSupport());
2339 // Set to any basic blending equation.
2340 GrBlendEquation blend_equation = kAdd_GrBlendEquation;
2341 GL_CALL(BlendEquation(gXfermodeEquation2Blend[blend_equation]));
2342 fHWBlendState.fEquation = blend_equation;
2343 }
2344
egdanielb414f252014-07-29 13:15:47 -07002345 fHWBlendState.fEnabled = kNo_TriState;
2346 }
cdalton8917d622015-05-06 13:40:21 -07002347 return;
2348 }
2349
2350 if (kYes_TriState != fHWBlendState.fEnabled) {
2351 GL_CALL(Enable(GR_GL_BLEND));
2352 fHWBlendState.fEnabled = kYes_TriState;
2353 }
2354
2355 if (fHWBlendState.fEquation != equation) {
2356 GL_CALL(BlendEquation(gXfermodeEquation2Blend[equation]));
2357 fHWBlendState.fEquation = equation;
2358 }
2359
2360 if (GrBlendEquationIsAdvanced(equation)) {
2361 SkASSERT(this->caps()->advancedBlendEquationSupport());
2362 // Advanced equations have no other blend state.
2363 return;
2364 }
2365
2366 if (fHWBlendState.fSrcCoeff != srcCoeff ||
2367 fHWBlendState.fDstCoeff != dstCoeff) {
2368 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
2369 gXfermodeCoeff2Blend[dstCoeff]));
2370 fHWBlendState.fSrcCoeff = srcCoeff;
2371 fHWBlendState.fDstCoeff = dstCoeff;
2372 }
2373
2374 GrColor blendConst = blendInfo.fBlendConstant;
2375 if ((BlendCoeffReferencesConstant(srcCoeff) ||
2376 BlendCoeffReferencesConstant(dstCoeff)) &&
2377 (!fHWBlendState.fConstColorValid ||
2378 fHWBlendState.fConstColor != blendConst)) {
2379 GrGLfloat c[4];
2380 GrColorToRGBAFloat(blendConst, c);
2381 GL_CALL(BlendColor(c[0], c[1], c[2], c[3]));
2382 fHWBlendState.fConstColor = blendConst;
2383 fHWBlendState.fConstColorValid = true;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002384 }
2385}
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002386
commit-bot@chromium.org6364b5e2013-08-20 20:22:52 +00002387static inline GrGLenum tile_to_gl_wrap(SkShader::TileMode tm) {
bsalomon@google.comb8670992012-07-25 21:27:09 +00002388 static const GrGLenum gWrapModes[] = {
2389 GR_GL_CLAMP_TO_EDGE,
2390 GR_GL_REPEAT,
2391 GR_GL_MIRRORED_REPEAT
2392 };
commit-bot@chromium.org5d7ca952013-04-22 20:26:44 +00002393 GR_STATIC_ASSERT(SkShader::kTileModeCount == SK_ARRAY_COUNT(gWrapModes));
bsalomon@google.comb8670992012-07-25 21:27:09 +00002394 GR_STATIC_ASSERT(0 == SkShader::kClamp_TileMode);
2395 GR_STATIC_ASSERT(1 == SkShader::kRepeat_TileMode);
2396 GR_STATIC_ASSERT(2 == SkShader::kMirror_TileMode);
2397 return gWrapModes[tm];
2398}
2399
bsalomon861e1032014-12-16 07:33:49 -08002400void GrGLGpu::bindTexture(int unitIdx, const GrTextureParams& params, GrGLTexture* texture) {
bsalomon49f085d2014-09-05 13:34:00 -07002401 SkASSERT(texture);
tomhudson@google.comd0c1a062012-07-12 17:23:52 +00002402
reed856e9d92015-09-30 12:21:45 -07002403#ifdef SK_DEBUG
2404 if (!this->caps()->npotTextureTileSupport()) {
2405 const bool tileX = SkShader::kClamp_TileMode != params.getTileModeX();
2406 const bool tileY = SkShader::kClamp_TileMode != params.getTileModeY();
2407 if (tileX || tileY) {
2408 const int w = texture->width();
2409 const int h = texture->height();
2410 SkASSERT(SkIsPow2(w) && SkIsPow2(h));
2411 }
2412 }
2413#endif
2414
bsalomon@google.comb8670992012-07-25 21:27:09 +00002415 // If we created a rt/tex and rendered to it without using a texture and now we're texturing
2416 // 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 +00002417 // out of the "last != next" check.
bsalomon37dd3312014-11-03 08:47:23 -08002418 GrGLRenderTarget* texRT = static_cast<GrGLRenderTarget*>(texture->asRenderTarget());
bsalomon49f085d2014-09-05 13:34:00 -07002419 if (texRT) {
bsalomon@google.com4c883782012-06-04 19:05:11 +00002420 this->onResolveRenderTarget(texRT);
2421 }
2422
bsalomon1c63bf62014-07-22 13:09:46 -07002423 uint32_t textureID = texture->getUniqueID();
bsalomon10528f12015-10-14 12:54:52 -07002424 GrGLenum target = texture->target();
bsalomon1c63bf62014-07-22 13:09:46 -07002425 if (fHWBoundTextureUniqueIDs[unitIdx] != textureID) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002426 this->setTextureUnit(unitIdx);
bsalomon10528f12015-10-14 12:54:52 -07002427 GL_CALL(BindTexture(target, texture->textureID()));
bsalomon1c63bf62014-07-22 13:09:46 -07002428 fHWBoundTextureUniqueIDs[unitIdx] = textureID;
bsalomon@google.com4c883782012-06-04 19:05:11 +00002429 }
2430
bsalomon@google.com4c883782012-06-04 19:05:11 +00002431 ResetTimestamp timestamp;
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002432 const GrGLTexture::TexParams& oldTexParams = texture->getCachedTexParams(&timestamp);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002433 bool setAll = timestamp < this->getResetTimestamp();
2434 GrGLTexture::TexParams newTexParams;
2435
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00002436 static GrGLenum glMinFilterModes[] = {
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +00002437 GR_GL_NEAREST,
2438 GR_GL_LINEAR,
2439 GR_GL_LINEAR_MIPMAP_LINEAR
2440 };
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00002441 static GrGLenum glMagFilterModes[] = {
2442 GR_GL_NEAREST,
2443 GR_GL_LINEAR,
2444 GR_GL_LINEAR
2445 };
commit-bot@chromium.org47442312013-12-19 16:18:01 +00002446 GrTextureParams::FilterMode filterMode = params.filterMode();
bsalomonefd7d452014-10-23 14:17:46 -07002447
2448 if (GrTextureParams::kMipMap_FilterMode == filterMode) {
2449 if (!this->caps()->mipMapSupport() || GrPixelConfigIsCompressed(texture->config())) {
2450 filterMode = GrTextureParams::kBilerp_FilterMode;
2451 }
commit-bot@chromium.org47442312013-12-19 16:18:01 +00002452 }
bsalomonefd7d452014-10-23 14:17:46 -07002453
commit-bot@chromium.org47442312013-12-19 16:18:01 +00002454 newTexParams.fMinFilter = glMinFilterModes[filterMode];
2455 newTexParams.fMagFilter = glMagFilterModes[filterMode];
skia.committer@gmail.comaeefb2a2013-07-27 07:01:06 +00002456
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +00002457 if (GrTextureParams::kMipMap_FilterMode == filterMode &&
bsalomonefd7d452014-10-23 14:17:46 -07002458 texture->texturePriv().mipMapsAreDirty()) {
bsalomon10528f12015-10-14 12:54:52 -07002459 GL_CALL(GenerateMipmap(target));
bsalomonafbf2d62014-09-30 12:18:44 -07002460 texture->texturePriv().dirtyMipMaps(false);
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +00002461 }
bsalomon@google.com4c883782012-06-04 19:05:11 +00002462
bsalomon@google.comb8670992012-07-25 21:27:09 +00002463 newTexParams.fWrapS = tile_to_gl_wrap(params.getTileModeX());
2464 newTexParams.fWrapT = tile_to_gl_wrap(params.getTileModeY());
bsalomon@google.com4c883782012-06-04 19:05:11 +00002465 memcpy(newTexParams.fSwizzleRGBA,
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002466 GrGLShaderBuilder::GetTexParamSwizzle(texture->config(), this->glCaps()),
bsalomon@google.com4c883782012-06-04 19:05:11 +00002467 sizeof(newTexParams.fSwizzleRGBA));
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00002468 if (setAll || newTexParams.fMagFilter != oldTexParams.fMagFilter) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002469 this->setTextureUnit(unitIdx);
bsalomon10528f12015-10-14 12:54:52 -07002470 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MAG_FILTER, newTexParams.fMagFilter));
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00002471 }
2472 if (setAll || newTexParams.fMinFilter != oldTexParams.fMinFilter) {
2473 this->setTextureUnit(unitIdx);
bsalomon10528f12015-10-14 12:54:52 -07002474 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MIN_FILTER, newTexParams.fMinFilter));
bsalomon@google.com4c883782012-06-04 19:05:11 +00002475 }
2476 if (setAll || newTexParams.fWrapS != oldTexParams.fWrapS) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002477 this->setTextureUnit(unitIdx);
bsalomon10528f12015-10-14 12:54:52 -07002478 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_WRAP_S, newTexParams.fWrapS));
bsalomon@google.com4c883782012-06-04 19:05:11 +00002479 }
2480 if (setAll || newTexParams.fWrapT != oldTexParams.fWrapT) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002481 this->setTextureUnit(unitIdx);
bsalomon10528f12015-10-14 12:54:52 -07002482 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_WRAP_T, newTexParams.fWrapT));
bsalomon@google.com4c883782012-06-04 19:05:11 +00002483 }
2484 if (this->glCaps().textureSwizzleSupport() &&
2485 (setAll || memcmp(newTexParams.fSwizzleRGBA,
2486 oldTexParams.fSwizzleRGBA,
2487 sizeof(newTexParams.fSwizzleRGBA)))) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002488 this->setTextureUnit(unitIdx);
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +00002489 if (this->glStandard() == kGLES_GrGLStandard) {
commit-bot@chromium.org6364b5e2013-08-20 20:22:52 +00002490 // ES3 added swizzle support but not GL_TEXTURE_SWIZZLE_RGBA.
2491 const GrGLenum* swizzle = newTexParams.fSwizzleRGBA;
bsalomon10528f12015-10-14 12:54:52 -07002492 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_R, swizzle[0]));
2493 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_G, swizzle[1]));
2494 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_B, swizzle[2]));
2495 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_A, swizzle[3]));
commit-bot@chromium.org6364b5e2013-08-20 20:22:52 +00002496 } else {
2497 GR_STATIC_ASSERT(sizeof(newTexParams.fSwizzleRGBA[0]) == sizeof(GrGLint));
2498 const GrGLint* swizzle = reinterpret_cast<const GrGLint*>(newTexParams.fSwizzleRGBA);
bsalomon10528f12015-10-14 12:54:52 -07002499 GL_CALL(TexParameteriv(target, GR_GL_TEXTURE_SWIZZLE_RGBA, swizzle));
commit-bot@chromium.org6364b5e2013-08-20 20:22:52 +00002500 }
bsalomon@google.com4c883782012-06-04 19:05:11 +00002501 }
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002502 texture->setCachedTexParams(newTexParams, this->getResetTimestamp());
bsalomon@google.com4c883782012-06-04 19:05:11 +00002503}
2504
egdaniel080e6732014-12-22 07:35:52 -08002505void GrGLGpu::flushColorWrite(bool writeColor) {
2506 if (!writeColor) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002507 if (kNo_TriState != fHWWriteToColor) {
2508 GL_CALL(ColorMask(GR_GL_FALSE, GR_GL_FALSE,
2509 GR_GL_FALSE, GR_GL_FALSE));
2510 fHWWriteToColor = kNo_TriState;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002511 }
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002512 } else {
2513 if (kYes_TriState != fHWWriteToColor) {
2514 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
2515 fHWWriteToColor = kYes_TriState;
2516 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00002517 }
bsalomon3e791242014-12-17 13:43:13 -08002518}
bsalomon@google.comd302f142011-03-03 13:54:13 +00002519
egdaniel8dd688b2015-01-22 10:16:09 -08002520void GrGLGpu::flushDrawFace(GrPipelineBuilder::DrawFace face) {
bsalomon3e791242014-12-17 13:43:13 -08002521 if (fHWDrawFace != face) {
2522 switch (face) {
egdaniel8dd688b2015-01-22 10:16:09 -08002523 case GrPipelineBuilder::kCCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002524 GL_CALL(Enable(GR_GL_CULL_FACE));
2525 GL_CALL(CullFace(GR_GL_BACK));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002526 break;
egdaniel8dd688b2015-01-22 10:16:09 -08002527 case GrPipelineBuilder::kCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002528 GL_CALL(Enable(GR_GL_CULL_FACE));
2529 GL_CALL(CullFace(GR_GL_FRONT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002530 break;
egdaniel8dd688b2015-01-22 10:16:09 -08002531 case GrPipelineBuilder::kBoth_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002532 GL_CALL(Disable(GR_GL_CULL_FACE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002533 break;
2534 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +00002535 SkFAIL("Unknown draw face.");
bsalomon@google.comd302f142011-03-03 13:54:13 +00002536 }
bsalomon3e791242014-12-17 13:43:13 -08002537 fHWDrawFace = face;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002538 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002539}
2540
bsalomon861e1032014-12-16 07:33:49 -08002541bool GrGLGpu::configToGLFormats(GrPixelConfig config,
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002542 bool getSizedInternalFormat,
2543 GrGLenum* internalFormat,
2544 GrGLenum* externalFormat,
jvanverth672bb7f2015-07-13 07:19:57 -07002545 GrGLenum* externalType) const {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002546 GrGLenum dontCare;
halcanary96fcdcc2015-08-27 07:41:13 -07002547 if (nullptr == internalFormat) {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002548 internalFormat = &dontCare;
2549 }
halcanary96fcdcc2015-08-27 07:41:13 -07002550 if (nullptr == externalFormat) {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002551 externalFormat = &dontCare;
2552 }
halcanary96fcdcc2015-08-27 07:41:13 -07002553 if (nullptr == externalType) {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002554 externalType = &dontCare;
2555 }
2556
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +00002557 if(!this->glCaps().isConfigTexturable(config)) {
2558 return false;
2559 }
2560
reed@google.comac10a2d2010-12-22 21:39:39 +00002561 switch (config) {
bsalomon@google.com0342a852012-08-20 19:22:38 +00002562 case kRGBA_8888_GrPixelConfig:
bsalomon@google.comc4364992011-11-07 15:54:49 +00002563 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002564 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002565 if (getSizedInternalFormat) {
2566 *internalFormat = GR_GL_RGBA8;
2567 } else {
2568 *internalFormat = GR_GL_RGBA;
2569 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002570 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.comc4364992011-11-07 15:54:49 +00002571 break;
bsalomon@google.com0342a852012-08-20 19:22:38 +00002572 case kBGRA_8888_GrPixelConfig:
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002573 if (this->glCaps().bgraIsInternalFormat()) {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002574 if (getSizedInternalFormat) {
2575 *internalFormat = GR_GL_BGRA8;
2576 } else {
2577 *internalFormat = GR_GL_BGRA;
2578 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002579 } else {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002580 if (getSizedInternalFormat) {
2581 *internalFormat = GR_GL_RGBA8;
2582 } else {
2583 *internalFormat = GR_GL_RGBA;
2584 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002585 }
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002586 *externalFormat = GR_GL_BGRA;
bsalomon@google.com6f379512011-11-16 20:36:03 +00002587 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002588 break;
jvanverthfa1e8a72014-12-22 08:31:49 -08002589 case kSRGBA_8888_GrPixelConfig:
bsalomon16921ec2015-07-30 15:34:56 -07002590 if (getSizedInternalFormat) {
jvanverthfa1e8a72014-12-22 08:31:49 -08002591 *internalFormat = GR_GL_SRGB8_ALPHA8;
2592 } else {
2593 *internalFormat = GR_GL_SRGB_ALPHA;
bsalomon16921ec2015-07-30 15:34:56 -07002594 }
2595 // OpenGL ES 2.0 + GL_EXT_sRGB allows GL_SRGB_ALPHA to be specified as the <format>
2596 // param to Tex(Sub)Image2D. ES 2.0 requires the internalFormat and format to match.
2597 // Thus, on ES 2.0 we will use GL_SRGB_ALPHA as the externalFormat. However,
2598 // onReadPixels needs code to override that because GL_SRGB_ALPHA is not allowed as a
2599 // glReadPixels format.
2600 // On OpenGL and ES 3.0 GL_SRGB_ALPHA does not work for the <format> param to
2601 // glReadPixels nor does it work with Tex(Sub)Image2D So we always set the externalFormat
2602 // return to GL_RGBA.
2603 if (this->glStandard() == kGLES_GrGLStandard &&
2604 this->glVersion() == GR_GL_VER(2,0)) {
jvanverth99babf22015-05-22 06:06:40 -07002605 *externalFormat = GR_GL_SRGB_ALPHA;
bsalomon16921ec2015-07-30 15:34:56 -07002606 } else {
2607 *externalFormat = GR_GL_RGBA;
jvanverthfa1e8a72014-12-22 08:31:49 -08002608 }
2609 *externalType = GR_GL_UNSIGNED_BYTE;
2610 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002611 case kRGB_565_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002612 *internalFormat = GR_GL_RGB;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002613 *externalFormat = GR_GL_RGB;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002614 if (getSizedInternalFormat) {
jvanverth3f801cb2014-12-16 09:49:38 -08002615 if (!this->glCaps().ES2CompatibilitySupport()) {
2616 *internalFormat = GR_GL_RGB5;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002617 } else {
2618 *internalFormat = GR_GL_RGB565;
2619 }
2620 } else {
2621 *internalFormat = GR_GL_RGB;
2622 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002623 *externalType = GR_GL_UNSIGNED_SHORT_5_6_5;
reed@google.comac10a2d2010-12-22 21:39:39 +00002624 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002625 case kRGBA_4444_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002626 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002627 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002628 if (getSizedInternalFormat) {
2629 *internalFormat = GR_GL_RGBA4;
2630 } else {
2631 *internalFormat = GR_GL_RGBA;
2632 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002633 *externalType = GR_GL_UNSIGNED_SHORT_4_4_4_4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002634 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002635 case kIndex_8_GrPixelConfig:
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +00002636 // no sized/unsized internal format distinction here
2637 *internalFormat = GR_GL_PALETTE8_RGBA8;
reed@google.comac10a2d2010-12-22 21:39:39 +00002638 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002639 case kAlpha_8_GrPixelConfig:
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002640 if (this->glCaps().textureRedSupport()) {
2641 *internalFormat = GR_GL_RED;
2642 *externalFormat = GR_GL_RED;
2643 if (getSizedInternalFormat) {
2644 *internalFormat = GR_GL_R8;
2645 } else {
2646 *internalFormat = GR_GL_RED;
2647 }
2648 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002649 } else {
2650 *internalFormat = GR_GL_ALPHA;
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002651 *externalFormat = GR_GL_ALPHA;
2652 if (getSizedInternalFormat) {
2653 *internalFormat = GR_GL_ALPHA8;
2654 } else {
2655 *internalFormat = GR_GL_ALPHA;
2656 }
2657 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002658 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002659 break;
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +00002660 case kETC1_GrPixelConfig:
egdanield588c012015-03-27 12:22:10 -07002661 *internalFormat = GR_GL_COMPRESSED_ETC1_RGB8;
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +00002662 break;
2663 case kLATC_GrPixelConfig:
2664 switch(this->glCaps().latcAlias()) {
2665 case GrGLCaps::kLATC_LATCAlias:
2666 *internalFormat = GR_GL_COMPRESSED_LUMINANCE_LATC1;
2667 break;
2668 case GrGLCaps::kRGTC_LATCAlias:
2669 *internalFormat = GR_GL_COMPRESSED_RED_RGTC1;
2670 break;
2671 case GrGLCaps::k3DC_LATCAlias:
2672 *internalFormat = GR_GL_COMPRESSED_3DC_X;
2673 break;
2674 }
2675 break;
krajcevski238b4562014-06-30 09:09:22 -07002676 case kR11_EAC_GrPixelConfig:
egdanield588c012015-03-27 12:22:10 -07002677 *internalFormat = GR_GL_COMPRESSED_R11_EAC;
krajcevski238b4562014-06-30 09:09:22 -07002678 break;
krajcevski7ef21622014-07-16 15:21:13 -07002679
2680 case kASTC_12x12_GrPixelConfig:
egdanield588c012015-03-27 12:22:10 -07002681 *internalFormat = GR_GL_COMPRESSED_RGBA_ASTC_12x12_KHR;
krajcevski7ef21622014-07-16 15:21:13 -07002682 break;
2683
joshualittee5da552014-07-16 13:32:56 -07002684 case kRGBA_float_GrPixelConfig:
2685 *internalFormat = GR_GL_RGBA32F;
2686 *externalFormat = GR_GL_RGBA;
2687 *externalType = GR_GL_FLOAT;
2688 break;
krajcevski7ef21622014-07-16 15:21:13 -07002689
jvanverth28f9c602014-12-05 13:06:35 -08002690 case kAlpha_half_GrPixelConfig:
jvanverth1334c212014-12-18 05:44:55 -08002691 if (this->glCaps().textureRedSupport()) {
2692 if (getSizedInternalFormat) {
2693 *internalFormat = GR_GL_R16F;
2694 } else {
2695 *internalFormat = GR_GL_RED;
2696 }
jvanverth28f9c602014-12-05 13:06:35 -08002697 *externalFormat = GR_GL_RED;
jvanverth1334c212014-12-18 05:44:55 -08002698 } else {
2699 if (getSizedInternalFormat) {
2700 *internalFormat = GR_GL_ALPHA16F;
2701 } else {
2702 *internalFormat = GR_GL_ALPHA;
2703 }
2704 *externalFormat = GR_GL_ALPHA;
2705 }
2706 if (kGL_GrGLStandard == this->glStandard() || this->glVersion() >= GR_GL_VER(3, 0)) {
jvanvertha60b2ea2014-12-12 05:58:06 -08002707 *externalType = GR_GL_HALF_FLOAT;
jvanverth28f9c602014-12-05 13:06:35 -08002708 } else {
jvanverth1334c212014-12-18 05:44:55 -08002709 *externalType = GR_GL_HALF_FLOAT_OES;
jvanverth28f9c602014-12-05 13:06:35 -08002710 }
2711 break;
2712
jvanverthfb5df432015-05-21 08:12:27 -07002713 case kRGBA_half_GrPixelConfig:
2714 *internalFormat = GR_GL_RGBA16F;
2715 *externalFormat = GR_GL_RGBA;
2716 if (kGL_GrGLStandard == this->glStandard() || this->glVersion() >= GR_GL_VER(3, 0)) {
2717 *externalType = GR_GL_HALF_FLOAT;
2718 } else {
2719 *externalType = GR_GL_HALF_FLOAT_OES;
2720 }
2721 break;
2722
reed@google.comac10a2d2010-12-22 21:39:39 +00002723 default:
2724 return false;
2725 }
2726 return true;
2727}
2728
bsalomon861e1032014-12-16 07:33:49 -08002729void GrGLGpu::setTextureUnit(int unit) {
bsalomon1c63bf62014-07-22 13:09:46 -07002730 SkASSERT(unit >= 0 && unit < fHWBoundTextureUniqueIDs.count());
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002731 if (unit != fHWActiveTextureUnitIdx) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002732 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com49209392012-06-05 15:13:46 +00002733 fHWActiveTextureUnitIdx = unit;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002734 }
2735}
bsalomon@google.com316f99232011-01-13 21:28:12 +00002736
bsalomon861e1032014-12-16 07:33:49 -08002737void GrGLGpu::setScratchTextureUnit() {
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002738 // Bind the last texture unit since it is the least likely to be used by GrGLProgram.
bsalomon1c63bf62014-07-22 13:09:46 -07002739 int lastUnitIdx = fHWBoundTextureUniqueIDs.count() - 1;
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002740 if (lastUnitIdx != fHWActiveTextureUnitIdx) {
2741 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + lastUnitIdx));
2742 fHWActiveTextureUnitIdx = lastUnitIdx;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002743 }
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002744 // clear out the this field so that if a program does use this unit it will rebind the correct
2745 // texture.
bsalomon1c63bf62014-07-22 13:09:46 -07002746 fHWBoundTextureUniqueIDs[lastUnitIdx] = SK_InvalidUniqueID;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002747}
2748
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002749namespace {
2750// Determines whether glBlitFramebuffer could be used between src and dst.
bsalomon@google.comeb851172013-04-15 13:51:00 +00002751inline bool can_blit_framebuffer(const GrSurface* dst,
2752 const GrSurface* src,
egdaniel0f5f9672015-02-03 11:10:51 -08002753 const GrGLGpu* gpu) {
commit-bot@chromium.org6b7938f2013-10-15 14:18:16 +00002754 if (gpu->glCaps().isConfigRenderable(dst->config(), dst->desc().fSampleCnt > 0) &&
2755 gpu->glCaps().isConfigRenderable(src->config(), src->desc().fSampleCnt > 0) &&
bsalomon@google.com347c3822013-05-01 20:10:01 +00002756 gpu->glCaps().usesMSAARenderBuffers()) {
commit-bot@chromium.orga8e5a062013-09-05 23:44:09 +00002757 // ES3 doesn't allow framebuffer blits when the src has MSAA and the configs don't match
2758 // or the rects are not the same (not just the same size but have the same edges).
2759 if (GrGLCaps::kES_3_0_MSFBOType == gpu->glCaps().msFBOType() &&
2760 (src->desc().fSampleCnt > 0 || src->config() != dst->config())) {
2761 return false;
2762 }
bsalomon@google.comeb851172013-04-15 13:51:00 +00002763 return true;
2764 } else {
2765 return false;
2766 }
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002767}
bsalomon@google.comeb851172013-04-15 13:51:00 +00002768
2769inline bool can_copy_texsubimage(const GrSurface* dst,
2770 const GrSurface* src,
egdaniel0f5f9672015-02-03 11:10:51 -08002771 const GrGLGpu* gpu) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002772 // Table 3.9 of the ES2 spec indicates the supported formats with CopyTexSubImage
2773 // and BGRA isn't in the spec. There doesn't appear to be any extension that adds it. Perhaps
2774 // many drivers would allow it to work, but ANGLE does not.
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +00002775 if (kGLES_GrGLStandard == gpu->glStandard() && gpu->glCaps().bgraIsInternalFormat() &&
bsalomon@google.comeb851172013-04-15 13:51:00 +00002776 (kBGRA_8888_GrPixelConfig == dst->config() || kBGRA_8888_GrPixelConfig == src->config())) {
2777 return false;
2778 }
2779 const GrGLRenderTarget* dstRT = static_cast<const GrGLRenderTarget*>(dst->asRenderTarget());
2780 // If dst is multisampled (and uses an extension where there is a separate MSAA renderbuffer)
2781 // then we don't want to copy to the texture but to the MSAA buffer.
egdanield803f272015-03-18 13:01:52 -07002782 if (dstRT && dstRT->renderFBOID() != dstRT->textureFBOID()) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002783 return false;
2784 }
bsalomon@google.coma2719852013-04-17 14:25:27 +00002785 const GrGLRenderTarget* srcRT = static_cast<const GrGLRenderTarget*>(src->asRenderTarget());
2786 // If the src is multisampled (and uses an extension where there is a separate MSAA
2787 // renderbuffer) then it is an invalid operation to call CopyTexSubImage
egdanield803f272015-03-18 13:01:52 -07002788 if (srcRT && srcRT->renderFBOID() != srcRT->textureFBOID()) {
bsalomon@google.coma2719852013-04-17 14:25:27 +00002789 return false;
2790 }
commit-bot@chromium.org6b7938f2013-10-15 14:18:16 +00002791 if (gpu->glCaps().isConfigRenderable(src->config(), src->desc().fSampleCnt > 0) &&
bsalomon49f085d2014-09-05 13:34:00 -07002792 dst->asTexture() &&
commit-bot@chromium.org6b7938f2013-10-15 14:18:16 +00002793 dst->origin() == src->origin() &&
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +00002794 !GrPixelConfigIsCompressed(src->config())) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002795 return true;
2796 } else {
2797 return false;
2798 }
2799}
2800
robertphillips754f4e92014-09-18 13:52:08 -07002801}
2802
bsalomon@google.comeb851172013-04-15 13:51:00 +00002803// If a temporary FBO was created, its non-zero ID is returned. The viewport that the copy rect is
2804// relative to is output.
bsalomon10528f12015-10-14 12:54:52 -07002805void GrGLGpu::bindSurfaceFBOForCopy(GrSurface* surface, GrGLenum fboTarget, GrGLIRect* viewport,
2806 TempFBOTarget tempFBOTarget) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002807 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(surface->asRenderTarget());
halcanary96fcdcc2015-08-27 07:41:13 -07002808 if (nullptr == rt) {
bsalomon49f085d2014-09-05 13:34:00 -07002809 SkASSERT(surface->asTexture());
bsalomon@google.comeb851172013-04-15 13:51:00 +00002810 GrGLuint texID = static_cast<GrGLTexture*>(surface->asTexture())->textureID();
bsalomon10528f12015-10-14 12:54:52 -07002811 GrGLenum target = static_cast<GrGLTexture*>(surface->asTexture())->target();
egdanield803f272015-03-18 13:01:52 -07002812 GrGLuint* tempFBOID;
2813 tempFBOID = kSrc_TempFBOTarget == tempFBOTarget ? &fTempSrcFBOID : &fTempDstFBOID;
egdaniel0f5f9672015-02-03 11:10:51 -08002814
egdanield803f272015-03-18 13:01:52 -07002815 if (0 == *tempFBOID) {
2816 GR_GL_CALL(this->glInterface(), GenFramebuffers(1, tempFBOID));
egdaniel0f5f9672015-02-03 11:10:51 -08002817 }
2818
egdanield803f272015-03-18 13:01:52 -07002819 fStats.incRenderTargetBinds();
2820 GR_GL_CALL(this->glInterface(), BindFramebuffer(fboTarget, *tempFBOID));
2821 GR_GL_CALL(this->glInterface(), FramebufferTexture2D(fboTarget,
robertphillips754f4e92014-09-18 13:52:08 -07002822 GR_GL_COLOR_ATTACHMENT0,
bsalomon10528f12015-10-14 12:54:52 -07002823 target,
robertphillips754f4e92014-09-18 13:52:08 -07002824 texID,
2825 0));
bsalomon@google.comeb851172013-04-15 13:51:00 +00002826 viewport->fLeft = 0;
2827 viewport->fBottom = 0;
2828 viewport->fWidth = surface->width();
2829 viewport->fHeight = surface->height();
2830 } else {
egdanield803f272015-03-18 13:01:52 -07002831 fStats.incRenderTargetBinds();
2832 GR_GL_CALL(this->glInterface(), BindFramebuffer(fboTarget, rt->renderFBOID()));
bsalomon@google.comeb851172013-04-15 13:51:00 +00002833 *viewport = rt->getViewport();
2834 }
egdaniel0f5f9672015-02-03 11:10:51 -08002835}
2836
bsalomon10528f12015-10-14 12:54:52 -07002837void GrGLGpu::unbindTextureFBOForCopy(GrGLenum fboTarget, GrSurface* surface) {
2838 // bindSurfaceFBOForCopy temporarily binds textures that are not render targets to
2839 if (!surface->asRenderTarget()) {
2840 SkASSERT(surface->asTexture());
2841 GrGLenum textureTarget = static_cast<GrGLTexture*>(surface->asTexture())->target();
2842 GR_GL_CALL(this->glInterface(), FramebufferTexture2D(fboTarget,
2843 GR_GL_COLOR_ATTACHMENT0,
2844 textureTarget,
2845 0,
2846 0));
2847 }
bsalomon@google.comeb851172013-04-15 13:51:00 +00002848}
2849
joshualitt1c735482015-07-13 08:08:25 -07002850bool GrGLGpu::initCopySurfaceDstDesc(const GrSurface* src, GrSurfaceDesc* desc) const {
bsalomon6df86402015-06-01 10:41:49 -07002851 // If the src is a texture, we can implement the blit as a draw assuming the config is
2852 // renderable.
2853 if (src->asTexture() && this->caps()->isConfigRenderable(src->config(), false)) {
2854 desc->fOrigin = kDefault_GrSurfaceOrigin;
2855 desc->fFlags = kRenderTarget_GrSurfaceFlag;
2856 desc->fConfig = src->config();
2857 return true;
2858 }
2859
2860 // We look for opportunities to use CopyTexSubImage, or fbo blit. If neither are
bsalomonf90a02b2014-11-26 12:28:00 -08002861 // possible and we return false to fallback to creating a render target dst for render-to-
2862 // texture. This code prefers CopyTexSubImage to fbo blit and avoids triggering temporary fbo
2863 // creation. It isn't clear that avoiding temporary fbo creation is actually optimal.
2864
bsalomon@google.comeb851172013-04-15 13:51:00 +00002865 // Check for format issues with glCopyTexSubImage2D
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +00002866 if (kGLES_GrGLStandard == this->glStandard() && this->glCaps().bgraIsInternalFormat() &&
bsalomon@google.comeb851172013-04-15 13:51:00 +00002867 kBGRA_8888_GrPixelConfig == src->config()) {
bsalomonf90a02b2014-11-26 12:28:00 -08002868 // glCopyTexSubImage2D doesn't work with this config. If the bgra can be used with fbo blit
2869 // then we set up for that, otherwise fail.
2870 if (this->caps()->isConfigRenderable(kBGRA_8888_GrPixelConfig, false)) {
2871 desc->fOrigin = kDefault_GrSurfaceOrigin;
bsalomon6bc1b5f2015-02-23 09:06:38 -08002872 desc->fFlags = kRenderTarget_GrSurfaceFlag;
bsalomonf90a02b2014-11-26 12:28:00 -08002873 desc->fConfig = kBGRA_8888_GrPixelConfig;
2874 return true;
2875 }
2876 return false;
halcanary96fcdcc2015-08-27 07:41:13 -07002877 } else if (nullptr == src->asRenderTarget()) {
bsalomonf90a02b2014-11-26 12:28:00 -08002878 // CopyTexSubImage2D or fbo blit would require creating a temp fbo for the src.
2879 return false;
bsalomon@google.coma2719852013-04-17 14:25:27 +00002880 }
2881
2882 const GrGLRenderTarget* srcRT = static_cast<const GrGLRenderTarget*>(src->asRenderTarget());
egdanield803f272015-03-18 13:01:52 -07002883 if (srcRT && srcRT->renderFBOID() != srcRT->textureFBOID()) {
bsalomonf90a02b2014-11-26 12:28:00 -08002884 // It's illegal to call CopyTexSubImage2D on a MSAA renderbuffer. Set up for FBO blit or
2885 // fail.
2886 if (this->caps()->isConfigRenderable(src->config(), false)) {
2887 desc->fOrigin = kDefault_GrSurfaceOrigin;
bsalomon6bc1b5f2015-02-23 09:06:38 -08002888 desc->fFlags = kRenderTarget_GrSurfaceFlag;
bsalomonf90a02b2014-11-26 12:28:00 -08002889 desc->fConfig = src->config();
2890 return true;
2891 }
2892 return false;
bsalomon@google.comeb851172013-04-15 13:51:00 +00002893 }
bsalomonf90a02b2014-11-26 12:28:00 -08002894
2895 // We'll do a CopyTexSubImage. Make the dst a plain old texture.
2896 desc->fConfig = src->config();
2897 desc->fOrigin = src->origin();
2898 desc->fFlags = kNone_GrSurfaceFlags;
2899 return true;
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002900}
2901
joshualitt1cbdcde2015-08-21 11:53:29 -07002902bool GrGLGpu::onCopySurface(GrSurface* dst,
2903 GrSurface* src,
2904 const SkIRect& srcRect,
2905 const SkIPoint& dstPoint) {
bsalomon6df86402015-06-01 10:41:49 -07002906 if (src->asTexture() && dst->asRenderTarget()) {
2907 this->copySurfaceAsDraw(dst, src, srcRect, dstPoint);
mtklein404b3b22015-05-18 09:29:10 -07002908 return true;
bsalomon5df6fee2015-05-18 06:26:15 -07002909 }
bsalomon6df86402015-06-01 10:41:49 -07002910
2911 if (can_copy_texsubimage(dst, src, this)) {
2912 this->copySurfaceAsCopyTexSubImage(dst, src, srcRect, dstPoint);
2913 return true;
2914 }
2915
mtklein404b3b22015-05-18 09:29:10 -07002916 if (can_blit_framebuffer(dst, src, this)) {
bsalomon6df86402015-06-01 10:41:49 -07002917 return this->copySurfaceAsBlitFramebuffer(dst, src, srcRect, dstPoint);
2918 }
2919
2920 return false;
2921}
2922
2923
2924void GrGLGpu::createCopyProgram() {
egdaniel472d44e2015-10-22 08:20:00 -07002925 const char* version = this->glCaps().glslCaps()->versionDeclString();
bsalomon6df86402015-06-01 10:41:49 -07002926
egdaniel0d3f0612015-10-21 10:45:48 -07002927 GrGLSLShaderVar aVertex("a_vertex", kVec2f_GrSLType, GrShaderVar::kAttribute_TypeModifier);
2928 GrGLSLShaderVar uTexCoordXform("u_texCoordXform", kVec4f_GrSLType,
bsalomon6df86402015-06-01 10:41:49 -07002929 GrShaderVar::kUniform_TypeModifier);
egdaniel0d3f0612015-10-21 10:45:48 -07002930 GrGLSLShaderVar uPosXform("u_posXform", kVec4f_GrSLType, GrShaderVar::kUniform_TypeModifier);
2931 GrGLSLShaderVar uTexture("u_texture", kSampler2D_GrSLType, GrShaderVar::kUniform_TypeModifier);
2932 GrGLSLShaderVar vTexCoord("v_texCoord", kVec2f_GrSLType, GrShaderVar::kVaryingOut_TypeModifier);
2933 GrGLSLShaderVar oFragColor("o_FragColor", kVec4f_GrSLType, GrShaderVar::kOut_TypeModifier);
bsalomon6df86402015-06-01 10:41:49 -07002934
2935 SkString vshaderTxt(version);
egdanielf5294392015-10-21 07:14:17 -07002936 aVertex.appendDecl(this->glCaps().glslCaps(), &vshaderTxt);
bsalomon6df86402015-06-01 10:41:49 -07002937 vshaderTxt.append(";");
egdanielf5294392015-10-21 07:14:17 -07002938 uTexCoordXform.appendDecl(this->glCaps().glslCaps(), &vshaderTxt);
bsalomon6df86402015-06-01 10:41:49 -07002939 vshaderTxt.append(";");
egdanielf5294392015-10-21 07:14:17 -07002940 uPosXform.appendDecl(this->glCaps().glslCaps(), &vshaderTxt);
bsalomon6df86402015-06-01 10:41:49 -07002941 vshaderTxt.append(";");
egdanielf5294392015-10-21 07:14:17 -07002942 vTexCoord.appendDecl(this->glCaps().glslCaps(), &vshaderTxt);
bsalomon6df86402015-06-01 10:41:49 -07002943 vshaderTxt.append(";");
2944
2945 vshaderTxt.append(
2946 "// Copy Program VS\n"
2947 "void main() {"
2948 " v_texCoord = a_vertex.xy * u_texCoordXform.xy + u_texCoordXform.zw;"
2949 " gl_Position.xy = a_vertex * u_posXform.xy + u_posXform.zw;"
2950 " gl_Position.zw = vec2(0, 1);"
2951 "}"
2952 );
2953
2954 SkString fshaderTxt(version);
jvanverthcba99b82015-06-24 06:59:57 -07002955 GrGLAppendGLSLDefaultFloatPrecisionDeclaration(kDefault_GrSLPrecision, this->glStandard(),
2956 &fshaderTxt);
bsalomon6df86402015-06-01 10:41:49 -07002957 vTexCoord.setTypeModifier(GrShaderVar::kVaryingIn_TypeModifier);
egdanielf5294392015-10-21 07:14:17 -07002958 vTexCoord.appendDecl(this->glCaps().glslCaps(), &fshaderTxt);
bsalomon6df86402015-06-01 10:41:49 -07002959 fshaderTxt.append(";");
egdanielf5294392015-10-21 07:14:17 -07002960 uTexture.appendDecl(this->glCaps().glslCaps(), &fshaderTxt);
bsalomon6df86402015-06-01 10:41:49 -07002961 fshaderTxt.append(";");
2962 const char* fsOutName;
2963 if (this->glCaps().glslCaps()->mustDeclareFragmentShaderOutput()) {
egdanielf5294392015-10-21 07:14:17 -07002964 oFragColor.appendDecl(this->glCaps().glslCaps(), &fshaderTxt);
bsalomon6df86402015-06-01 10:41:49 -07002965 fshaderTxt.append(";");
2966 fsOutName = oFragColor.c_str();
2967 } else {
2968 fsOutName = "gl_FragColor";
2969 }
2970 fshaderTxt.appendf(
2971 "// Copy Program FS\n"
2972 "void main() {"
2973 " %s = %s(u_texture, v_texCoord);"
2974 "}",
2975 fsOutName,
2976 GrGLSLTexture2DFunctionName(kVec2f_GrSLType, this->glslGeneration())
2977 );
2978
2979 GL_CALL_RET(fCopyProgram.fProgram, CreateProgram());
2980 const char* str;
2981 GrGLint length;
2982
2983 str = vshaderTxt.c_str();
2984 length = SkToInt(vshaderTxt.size());
2985 GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fCopyProgram.fProgram,
2986 GR_GL_VERTEX_SHADER, &str, &length, 1, &fStats);
2987
2988 str = fshaderTxt.c_str();
2989 length = SkToInt(fshaderTxt.size());
2990 GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fCopyProgram.fProgram,
2991 GR_GL_FRAGMENT_SHADER, &str, &length, 1, &fStats);
2992
2993 GL_CALL(LinkProgram(fCopyProgram.fProgram));
2994
2995 GL_CALL_RET(fCopyProgram.fTextureUniform, GetUniformLocation(fCopyProgram.fProgram,
2996 "u_texture"));
2997 GL_CALL_RET(fCopyProgram.fPosXformUniform, GetUniformLocation(fCopyProgram.fProgram,
2998 "u_posXform"));
2999 GL_CALL_RET(fCopyProgram.fTexCoordXformUniform, GetUniformLocation(fCopyProgram.fProgram,
3000 "u_texCoordXform"));
3001
3002 GL_CALL(BindAttribLocation(fCopyProgram.fProgram, 0, "a_vertex"));
3003
3004 GL_CALL(DeleteShader(vshader));
3005 GL_CALL(DeleteShader(fshader));
3006
3007 GL_CALL(GenBuffers(1, &fCopyProgram.fArrayBuffer));
3008 fHWGeometryState.setVertexBufferID(this, fCopyProgram.fArrayBuffer);
3009 static const GrGLfloat vdata[] = {
3010 0, 0,
3011 0, 1,
3012 1, 0,
3013 1, 1
3014 };
3015 GL_ALLOC_CALL(this->glInterface(),
3016 BufferData(GR_GL_ARRAY_BUFFER,
3017 (GrGLsizeiptr) sizeof(vdata),
3018 vdata, // data ptr
3019 GR_GL_STATIC_DRAW));
3020}
3021
3022void GrGLGpu::copySurfaceAsDraw(GrSurface* dst,
3023 GrSurface* src,
3024 const SkIRect& srcRect,
3025 const SkIPoint& dstPoint) {
3026 int w = srcRect.width();
3027 int h = srcRect.height();
3028
3029 GrGLTexture* srcTex = static_cast<GrGLTexture*>(src->asTexture());
3030 GrTextureParams params(SkShader::kClamp_TileMode, GrTextureParams::kNone_FilterMode);
3031 this->bindTexture(0, params, srcTex);
3032
3033 GrGLRenderTarget* dstRT = static_cast<GrGLRenderTarget*>(dst->asRenderTarget());
3034 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY, w, h);
3035 this->flushRenderTarget(dstRT, &dstRect);
3036
3037 GL_CALL(UseProgram(fCopyProgram.fProgram));
3038 fHWProgramID = fCopyProgram.fProgram;
3039
3040 fHWGeometryState.setVertexArrayID(this, 0);
3041
3042 GrGLAttribArrayState* attribs =
3043 fHWGeometryState.bindArrayAndBufferToDraw(this, fCopyProgram.fArrayBuffer);
3044 attribs->set(this, 0, fCopyProgram.fArrayBuffer, 2, GR_GL_FLOAT, false,
3045 2 * sizeof(GrGLfloat), 0);
bsalomond6246342015-06-04 13:57:00 -07003046 attribs->disableUnusedArrays(this, 0x1);
bsalomon6df86402015-06-01 10:41:49 -07003047
3048 // dst rect edges in NDC (-1 to 1)
3049 int dw = dst->width();
3050 int dh = dst->height();
3051 GrGLfloat dx0 = 2.f * dstPoint.fX / dw - 1.f;
3052 GrGLfloat dx1 = 2.f * (dstPoint.fX + w) / dw - 1.f;
3053 GrGLfloat dy0 = 2.f * dstPoint.fY / dh - 1.f;
3054 GrGLfloat dy1 = 2.f * (dstPoint.fY + h) / dh - 1.f;
3055 if (kBottomLeft_GrSurfaceOrigin == dst->origin()) {
3056 dy0 = -dy0;
3057 dy1 = -dy1;
3058 }
3059
3060 // src rect edges in normalized texture space (0 to 1)
3061 int sw = src->width();
3062 int sh = src->height();
3063 GrGLfloat sx0 = (GrGLfloat)srcRect.fLeft / sw;
3064 GrGLfloat sx1 = (GrGLfloat)(srcRect.fLeft + w) / sw;
3065 GrGLfloat sy0 = (GrGLfloat)srcRect.fTop / sh;
3066 GrGLfloat sy1 = (GrGLfloat)(srcRect.fTop + h) / sh;
3067 if (kBottomLeft_GrSurfaceOrigin == src->origin()) {
3068 sy0 = 1.f - sy0;
3069 sy1 = 1.f - sy1;
3070 }
3071
3072 GL_CALL(Uniform4f(fCopyProgram.fPosXformUniform, dx1 - dx0, dy1 - dy0, dx0, dy0));
3073 GL_CALL(Uniform4f(fCopyProgram.fTexCoordXformUniform, sx1 - sx0, sy1 - sy0, sx0, sy0));
3074 GL_CALL(Uniform1i(fCopyProgram.fTextureUniform, 0));
3075
3076 GrXferProcessor::BlendInfo blendInfo;
3077 blendInfo.reset();
3078 this->flushBlend(blendInfo);
3079 this->flushColorWrite(true);
bsalomon6df86402015-06-01 10:41:49 -07003080 this->flushDrawFace(GrPipelineBuilder::kBoth_DrawFace);
vbuzinov3e77ba92015-09-30 23:02:06 -07003081 this->flushHWAAState(dstRT, false, false);
bsalomon6df86402015-06-01 10:41:49 -07003082 this->disableScissor();
3083 GrStencilSettings stencil;
3084 stencil.setDisabled();
3085 this->flushStencil(stencil);
3086
3087 GL_CALL(DrawArrays(GR_GL_TRIANGLE_STRIP, 0, 4));
3088}
3089
3090void GrGLGpu::copySurfaceAsCopyTexSubImage(GrSurface* dst,
3091 GrSurface* src,
3092 const SkIRect& srcRect,
3093 const SkIPoint& dstPoint) {
3094 SkASSERT(can_copy_texsubimage(dst, src, this));
bsalomon6df86402015-06-01 10:41:49 -07003095 GrGLIRect srcVP;
bsalomon10528f12015-10-14 12:54:52 -07003096 this->bindSurfaceFBOForCopy(src, GR_GL_FRAMEBUFFER, &srcVP, kSrc_TempFBOTarget);
bsalomon6df86402015-06-01 10:41:49 -07003097 GrGLTexture* dstTex = static_cast<GrGLTexture*>(dst->asTexture());
3098 SkASSERT(dstTex);
3099 // We modified the bound FBO
3100 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
3101 GrGLIRect srcGLRect;
3102 srcGLRect.setRelativeTo(srcVP,
3103 srcRect.fLeft,
3104 srcRect.fTop,
3105 srcRect.width(),
3106 srcRect.height(),
3107 src->origin());
3108
3109 this->setScratchTextureUnit();
bsalomon10528f12015-10-14 12:54:52 -07003110 GL_CALL(BindTexture(dstTex->target(), dstTex->textureID()));
bsalomon6df86402015-06-01 10:41:49 -07003111 GrGLint dstY;
3112 if (kBottomLeft_GrSurfaceOrigin == dst->origin()) {
3113 dstY = dst->height() - (dstPoint.fY + srcGLRect.fHeight);
3114 } else {
3115 dstY = dstPoint.fY;
3116 }
bsalomon10528f12015-10-14 12:54:52 -07003117 GL_CALL(CopyTexSubImage2D(dstTex->target(), 0,
bsalomon6df86402015-06-01 10:41:49 -07003118 dstPoint.fX, dstY,
3119 srcGLRect.fLeft, srcGLRect.fBottom,
3120 srcGLRect.fWidth, srcGLRect.fHeight));
bsalomon10528f12015-10-14 12:54:52 -07003121 this->unbindTextureFBOForCopy(GR_GL_FRAMEBUFFER, src);
bsalomon6df86402015-06-01 10:41:49 -07003122}
3123
3124bool GrGLGpu::copySurfaceAsBlitFramebuffer(GrSurface* dst,
3125 GrSurface* src,
3126 const SkIRect& srcRect,
3127 const SkIPoint& dstPoint) {
3128 SkASSERT(can_blit_framebuffer(dst, src, this));
3129 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
3130 srcRect.width(), srcRect.height());
3131 if (dst == src) {
3132 if (SkIRect::IntersectsNoEmptyCheck(dstRect, srcRect)) {
3133 return false;
mtklein404b3b22015-05-18 09:29:10 -07003134 }
bsalomon5df6fee2015-05-18 06:26:15 -07003135 }
bsalomon6df86402015-06-01 10:41:49 -07003136
bsalomon6df86402015-06-01 10:41:49 -07003137 GrGLIRect dstVP;
3138 GrGLIRect srcVP;
bsalomon10528f12015-10-14 12:54:52 -07003139 this->bindSurfaceFBOForCopy(dst, GR_GL_DRAW_FRAMEBUFFER, &dstVP, kDst_TempFBOTarget);
3140 this->bindSurfaceFBOForCopy(src, GR_GL_READ_FRAMEBUFFER, &srcVP, kSrc_TempFBOTarget);
bsalomon6df86402015-06-01 10:41:49 -07003141 // We modified the bound FBO
3142 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
3143 GrGLIRect srcGLRect;
3144 GrGLIRect dstGLRect;
3145 srcGLRect.setRelativeTo(srcVP,
3146 srcRect.fLeft,
3147 srcRect.fTop,
3148 srcRect.width(),
3149 srcRect.height(),
3150 src->origin());
3151 dstGLRect.setRelativeTo(dstVP,
3152 dstRect.fLeft,
3153 dstRect.fTop,
3154 dstRect.width(),
3155 dstRect.height(),
3156 dst->origin());
3157
3158 // BlitFrameBuffer respects the scissor, so disable it.
3159 this->disableScissor();
3160
3161 GrGLint srcY0;
3162 GrGLint srcY1;
3163 // Does the blit need to y-mirror or not?
3164 if (src->origin() == dst->origin()) {
3165 srcY0 = srcGLRect.fBottom;
3166 srcY1 = srcGLRect.fBottom + srcGLRect.fHeight;
3167 } else {
3168 srcY0 = srcGLRect.fBottom + srcGLRect.fHeight;
3169 srcY1 = srcGLRect.fBottom;
3170 }
3171 GL_CALL(BlitFramebuffer(srcGLRect.fLeft,
3172 srcY0,
3173 srcGLRect.fLeft + srcGLRect.fWidth,
3174 srcY1,
3175 dstGLRect.fLeft,
3176 dstGLRect.fBottom,
3177 dstGLRect.fLeft + dstGLRect.fWidth,
3178 dstGLRect.fBottom + dstGLRect.fHeight,
3179 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
bsalomon10528f12015-10-14 12:54:52 -07003180 this->unbindTextureFBOForCopy(GR_GL_DRAW_FRAMEBUFFER, dst);
3181 this->unbindTextureFBOForCopy(GR_GL_READ_FRAMEBUFFER, src);
bsalomon6df86402015-06-01 10:41:49 -07003182 return true;
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00003183}
3184
cdalton231c5fd2015-05-13 12:35:36 -07003185void GrGLGpu::xferBarrier(GrRenderTarget* rt, GrXferBarrierType type) {
bsalomoncb02b382015-08-12 11:14:50 -07003186 SkASSERT(type);
cdalton9954bc32015-04-29 14:17:00 -07003187 switch (type) {
cdalton231c5fd2015-05-13 12:35:36 -07003188 case kTexture_GrXferBarrierType: {
3189 GrGLRenderTarget* glrt = static_cast<GrGLRenderTarget*>(rt);
3190 if (glrt->textureFBOID() != glrt->renderFBOID()) {
3191 // The render target uses separate storage so no need for glTextureBarrier.
3192 // FIXME: The render target will resolve automatically when its texture is bound,
3193 // but we could resolve only the bounds that will be read if we do it here instead.
3194 return;
3195 }
cdalton9954bc32015-04-29 14:17:00 -07003196 SkASSERT(this->caps()->textureBarrierSupport());
3197 GL_CALL(TextureBarrier());
3198 return;
cdalton231c5fd2015-05-13 12:35:36 -07003199 }
cdalton8917d622015-05-06 13:40:21 -07003200 case kBlend_GrXferBarrierType:
bsalomon4b91f762015-05-19 09:29:46 -07003201 SkASSERT(GrCaps::kAdvanced_BlendEquationSupport ==
cdalton8917d622015-05-06 13:40:21 -07003202 this->caps()->blendEquationSupport());
3203 GL_CALL(BlendBarrier());
3204 return;
bsalomoncb02b382015-08-12 11:14:50 -07003205 default: break; // placate compiler warnings that kNone not handled
cdalton9954bc32015-04-29 14:17:00 -07003206 }
3207}
3208
jvanverth88957922015-07-14 11:02:52 -07003209GrBackendObject GrGLGpu::createTestingOnlyBackendTexture(void* pixels, int w, int h,
jvanverth672bb7f2015-07-13 07:19:57 -07003210 GrPixelConfig config) const {
3211 GrGLuint texID;
3212 GL_CALL(GenTextures(1, &texID));
3213 GL_CALL(ActiveTexture(GR_GL_TEXTURE0));
3214 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, 1));
3215 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, texID));
3216 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_MAG_FILTER, GR_GL_NEAREST));
3217 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_MIN_FILTER, GR_GL_NEAREST));
3218 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_WRAP_S, GR_GL_CLAMP_TO_EDGE));
3219 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_WRAP_T, GR_GL_CLAMP_TO_EDGE));
3220
3221 GrGLenum internalFormat = 0x0; // suppress warning
3222 GrGLenum externalFormat = 0x0; // suppress warning
3223 GrGLenum externalType = 0x0; // suppress warning
3224
3225 this->configToGLFormats(config, false, &internalFormat, &externalFormat, &externalType);
3226
3227 GL_CALL(TexImage2D(GR_GL_TEXTURE_2D, 0, internalFormat, w, h, 0, externalFormat,
3228 externalType, pixels));
3229
3230 return texID;
3231}
3232
jvanverth88957922015-07-14 11:02:52 -07003233bool GrGLGpu::isTestingOnlyBackendTexture(GrBackendObject id) const {
jvanverth672bb7f2015-07-13 07:19:57 -07003234 GrGLuint texID = (GrGLuint)id;
3235
3236 GrGLboolean result;
3237 GL_CALL_RET(result, IsTexture(texID));
3238
3239 return (GR_GL_TRUE == result);
3240}
3241
jvanverth88957922015-07-14 11:02:52 -07003242void GrGLGpu::deleteTestingOnlyBackendTexture(GrBackendObject id) const {
jvanverth672bb7f2015-07-13 07:19:57 -07003243 GrGLuint texID = (GrGLuint)id;
3244 GL_CALL(DeleteTextures(1, &texID));
3245}
3246
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00003247///////////////////////////////////////////////////////////////////////////////
bsalomon861e1032014-12-16 07:33:49 -08003248GrGLAttribArrayState* GrGLGpu::HWGeometryState::bindArrayAndBuffersToDraw(
3249 GrGLGpu* gpu,
bsalomon@google.com6918d482013-03-07 19:09:11 +00003250 const GrGLVertexBuffer* vbuffer,
3251 const GrGLIndexBuffer* ibuffer) {
bsalomon49f085d2014-09-05 13:34:00 -07003252 SkASSERT(vbuffer);
bsalomon6df86402015-06-01 10:41:49 -07003253 GrGLuint vbufferID = vbuffer->bufferID();
halcanary96fcdcc2015-08-27 07:41:13 -07003254 GrGLuint* ibufferIDPtr = nullptr;
bsalomon6df86402015-06-01 10:41:49 -07003255 GrGLuint ibufferID;
3256 if (ibuffer) {
3257 ibufferID = ibuffer->bufferID();
3258 ibufferIDPtr = &ibufferID;
3259 }
3260 return this->internalBind(gpu, vbufferID, ibufferIDPtr);
3261}
3262
3263GrGLAttribArrayState* GrGLGpu::HWGeometryState::bindArrayAndBufferToDraw(GrGLGpu* gpu,
3264 GrGLuint vbufferID) {
halcanary96fcdcc2015-08-27 07:41:13 -07003265 return this->internalBind(gpu, vbufferID, nullptr);
bsalomon6df86402015-06-01 10:41:49 -07003266}
3267
3268GrGLAttribArrayState* GrGLGpu::HWGeometryState::bindArrayAndBuffersToDraw(GrGLGpu* gpu,
3269 GrGLuint vbufferID,
3270 GrGLuint ibufferID) {
3271 return this->internalBind(gpu, vbufferID, &ibufferID);
3272}
3273
3274GrGLAttribArrayState* GrGLGpu::HWGeometryState::internalBind(GrGLGpu* gpu,
3275 GrGLuint vbufferID,
3276 GrGLuint* ibufferID) {
robertphillips@google.com4f65a272013-03-26 19:40:46 +00003277 GrGLAttribArrayState* attribState;
3278
bsalomon6df86402015-06-01 10:41:49 -07003279 if (gpu->glCaps().isCoreProfile() && 0 != vbufferID) {
bsalomon8780bc62015-05-13 09:56:37 -07003280 if (!fVBOVertexArray) {
bsalomon@google.com6918d482013-03-07 19:09:11 +00003281 GrGLuint arrayID;
3282 GR_GL_CALL(gpu->glInterface(), GenVertexArrays(1, &arrayID));
3283 int attrCount = gpu->glCaps().maxVertexAttributes();
halcanary385fe4d2015-08-26 13:07:48 -07003284 fVBOVertexArray = new GrGLVertexArray(arrayID, attrCount);
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00003285 }
bsalomon6df86402015-06-01 10:41:49 -07003286 if (ibufferID) {
3287 attribState = fVBOVertexArray->bindWithIndexBuffer(gpu, *ibufferID);
3288 } else {
3289 attribState = fVBOVertexArray->bind(gpu);
3290 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00003291 } else {
bsalomon6df86402015-06-01 10:41:49 -07003292 if (ibufferID) {
3293 this->setIndexBufferIDOnDefaultVertexArray(gpu, *ibufferID);
bsalomon@google.com6918d482013-03-07 19:09:11 +00003294 } else {
3295 this->setVertexArrayID(gpu, 0);
3296 }
3297 int attrCount = gpu->glCaps().maxVertexAttributes();
3298 if (fDefaultVertexArrayAttribState.count() != attrCount) {
3299 fDefaultVertexArrayAttribState.resize(attrCount);
3300 }
3301 attribState = &fDefaultVertexArrayAttribState;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00003302 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00003303 return attribState;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00003304}