blob: 2bd36b640477eaf1b14a60003436553b9a344b0f [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"
egdanielcb7ba1e2015-10-26 08:38:25 -070021#include "glsl/GrGLSL.h"
jvanverthcba99b82015-06-24 06:59:57 -070022#include "glsl/GrGLSLCaps.h"
commit-bot@chromium.org32184d82013-10-09 15:14:18 +000023#include "SkStrokeRec.h"
bsalomon@google.com3582bf92011-06-30 21:32:31 +000024#include "SkTemplates.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000025
bsalomon@google.com0b77d682011-08-19 13:28:54 +000026#define GL_CALL(X) GR_GL_CALL(this->glInterface(), X)
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +000027#define GL_CALL_RET(RET, X) GR_GL_CALL_RET(this->glInterface(), RET, X)
bsalomon@google.com0b77d682011-08-19 13:28:54 +000028
reed@google.comac10a2d2010-12-22 21:39:39 +000029#define SKIP_CACHE_CHECK true
30
bsalomon@google.com4f3c2532012-01-19 16:16:52 +000031#if GR_GL_CHECK_ALLOC_WITH_GET_ERROR
32 #define CLEAR_ERROR_BEFORE_ALLOC(iface) GrGLClearErr(iface)
33 #define GL_ALLOC_CALL(iface, call) GR_GL_CALL_NOERRCHECK(iface, call)
34 #define CHECK_ALLOC_ERROR(iface) GR_GL_GET_ERROR(iface)
rmistry@google.comfbfcd562012-08-23 18:09:54 +000035#else
bsalomon@google.com4f3c2532012-01-19 16:16:52 +000036 #define CLEAR_ERROR_BEFORE_ALLOC(iface)
37 #define GL_ALLOC_CALL(iface, call) GR_GL_CALL(iface, call)
38 #define CHECK_ALLOC_ERROR(iface) GR_GL_NO_ERROR
39#endif
40
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +000041
42///////////////////////////////////////////////////////////////////////////////
43
cdaltonb85a0aa2014-07-21 15:32:44 -070044
cdalton8917d622015-05-06 13:40:21 -070045static const GrGLenum gXfermodeEquation2Blend[] = {
46 // Basic OpenGL blend equations.
47 GR_GL_FUNC_ADD,
48 GR_GL_FUNC_SUBTRACT,
49 GR_GL_FUNC_REVERSE_SUBTRACT,
50
51 // GL_KHR_blend_equation_advanced.
52 GR_GL_SCREEN,
53 GR_GL_OVERLAY,
54 GR_GL_DARKEN,
55 GR_GL_LIGHTEN,
56 GR_GL_COLORDODGE,
57 GR_GL_COLORBURN,
58 GR_GL_HARDLIGHT,
59 GR_GL_SOFTLIGHT,
60 GR_GL_DIFFERENCE,
61 GR_GL_EXCLUSION,
62 GR_GL_MULTIPLY,
63 GR_GL_HSL_HUE,
64 GR_GL_HSL_SATURATION,
65 GR_GL_HSL_COLOR,
66 GR_GL_HSL_LUMINOSITY
67};
68GR_STATIC_ASSERT(0 == kAdd_GrBlendEquation);
69GR_STATIC_ASSERT(1 == kSubtract_GrBlendEquation);
70GR_STATIC_ASSERT(2 == kReverseSubtract_GrBlendEquation);
71GR_STATIC_ASSERT(3 == kScreen_GrBlendEquation);
72GR_STATIC_ASSERT(4 == kOverlay_GrBlendEquation);
73GR_STATIC_ASSERT(5 == kDarken_GrBlendEquation);
74GR_STATIC_ASSERT(6 == kLighten_GrBlendEquation);
75GR_STATIC_ASSERT(7 == kColorDodge_GrBlendEquation);
76GR_STATIC_ASSERT(8 == kColorBurn_GrBlendEquation);
77GR_STATIC_ASSERT(9 == kHardLight_GrBlendEquation);
78GR_STATIC_ASSERT(10 == kSoftLight_GrBlendEquation);
79GR_STATIC_ASSERT(11 == kDifference_GrBlendEquation);
80GR_STATIC_ASSERT(12 == kExclusion_GrBlendEquation);
81GR_STATIC_ASSERT(13 == kMultiply_GrBlendEquation);
82GR_STATIC_ASSERT(14 == kHSLHue_GrBlendEquation);
83GR_STATIC_ASSERT(15 == kHSLSaturation_GrBlendEquation);
84GR_STATIC_ASSERT(16 == kHSLColor_GrBlendEquation);
85GR_STATIC_ASSERT(17 == kHSLLuminosity_GrBlendEquation);
bsalomonf7cc8772015-05-11 11:21:14 -070086GR_STATIC_ASSERT(SK_ARRAY_COUNT(gXfermodeEquation2Blend) == kGrBlendEquationCnt);
cdalton8917d622015-05-06 13:40:21 -070087
twiz@google.com0f31ca72011-03-18 17:38:11 +000088static const GrGLenum gXfermodeCoeff2Blend[] = {
89 GR_GL_ZERO,
90 GR_GL_ONE,
91 GR_GL_SRC_COLOR,
92 GR_GL_ONE_MINUS_SRC_COLOR,
93 GR_GL_DST_COLOR,
94 GR_GL_ONE_MINUS_DST_COLOR,
95 GR_GL_SRC_ALPHA,
96 GR_GL_ONE_MINUS_SRC_ALPHA,
97 GR_GL_DST_ALPHA,
98 GR_GL_ONE_MINUS_DST_ALPHA,
99 GR_GL_CONSTANT_COLOR,
100 GR_GL_ONE_MINUS_CONSTANT_COLOR,
101 GR_GL_CONSTANT_ALPHA,
102 GR_GL_ONE_MINUS_CONSTANT_ALPHA,
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000103
104 // extended blend coeffs
105 GR_GL_SRC1_COLOR,
106 GR_GL_ONE_MINUS_SRC1_COLOR,
107 GR_GL_SRC1_ALPHA,
108 GR_GL_ONE_MINUS_SRC1_ALPHA,
reed@google.comac10a2d2010-12-22 21:39:39 +0000109};
110
bsalomon861e1032014-12-16 07:33:49 -0800111bool GrGLGpu::BlendCoeffReferencesConstant(GrBlendCoeff coeff) {
bsalomon@google.com080773c2011-03-15 19:09:25 +0000112 static const bool gCoeffReferencesBlendConst[] = {
113 false,
114 false,
115 false,
116 false,
117 false,
118 false,
119 false,
120 false,
121 false,
122 false,
123 true,
124 true,
125 true,
126 true,
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000127
128 // extended blend coeffs
129 false,
130 false,
131 false,
132 false,
bsalomon@google.com080773c2011-03-15 19:09:25 +0000133 };
134 return gCoeffReferencesBlendConst[coeff];
bsalomonf7cc8772015-05-11 11:21:14 -0700135 GR_STATIC_ASSERT(kGrBlendCoeffCnt == SK_ARRAY_COUNT(gCoeffReferencesBlendConst));
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000136
bsalomon@google.com47059542012-06-06 20:51:20 +0000137 GR_STATIC_ASSERT(0 == kZero_GrBlendCoeff);
138 GR_STATIC_ASSERT(1 == kOne_GrBlendCoeff);
139 GR_STATIC_ASSERT(2 == kSC_GrBlendCoeff);
140 GR_STATIC_ASSERT(3 == kISC_GrBlendCoeff);
141 GR_STATIC_ASSERT(4 == kDC_GrBlendCoeff);
142 GR_STATIC_ASSERT(5 == kIDC_GrBlendCoeff);
143 GR_STATIC_ASSERT(6 == kSA_GrBlendCoeff);
144 GR_STATIC_ASSERT(7 == kISA_GrBlendCoeff);
145 GR_STATIC_ASSERT(8 == kDA_GrBlendCoeff);
146 GR_STATIC_ASSERT(9 == kIDA_GrBlendCoeff);
147 GR_STATIC_ASSERT(10 == kConstC_GrBlendCoeff);
148 GR_STATIC_ASSERT(11 == kIConstC_GrBlendCoeff);
149 GR_STATIC_ASSERT(12 == kConstA_GrBlendCoeff);
150 GR_STATIC_ASSERT(13 == kIConstA_GrBlendCoeff);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000151
bsalomon@google.com47059542012-06-06 20:51:20 +0000152 GR_STATIC_ASSERT(14 == kS2C_GrBlendCoeff);
153 GR_STATIC_ASSERT(15 == kIS2C_GrBlendCoeff);
154 GR_STATIC_ASSERT(16 == kS2A_GrBlendCoeff);
155 GR_STATIC_ASSERT(17 == kIS2A_GrBlendCoeff);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000156
157 // assertion for gXfermodeCoeff2Blend have to be in GrGpu scope
bsalomonf7cc8772015-05-11 11:21:14 -0700158 GR_STATIC_ASSERT(kGrBlendCoeffCnt == SK_ARRAY_COUNT(gXfermodeCoeff2Blend));
bsalomon@google.com080773c2011-03-15 19:09:25 +0000159}
160
reed@google.comac10a2d2010-12-22 21:39:39 +0000161///////////////////////////////////////////////////////////////////////////////
162
egdanielff1d5472015-09-10 08:37:20 -0700163// Used in the map of pixel configs to stencil format indices. This value is used to
164// indicate that a stencil format has not yet been set for the given config.
165static const int kUnknownStencilIndex = -1;
166// This value is used as the stencil index when no stencil configs are supported with the
167// given pixel config.
168static const int kUnsupportedStencilIndex = -2;
169
170///////////////////////////////////////////////////////////////////////////////
171
bsalomon682c2692015-05-22 14:01:46 -0700172GrGpu* GrGLGpu::Create(GrBackendContext backendContext, const GrContextOptions& options,
173 GrContext* context) {
bsalomon424cc262015-05-22 10:37:30 -0700174 SkAutoTUnref<const GrGLInterface> glInterface(
175 reinterpret_cast<const GrGLInterface*>(backendContext));
176 if (!glInterface) {
177 glInterface.reset(GrGLDefaultInterface());
178 } else {
179 glInterface->ref();
180 }
181 if (!glInterface) {
halcanary96fcdcc2015-08-27 07:41:13 -0700182 return nullptr;
bsalomon424cc262015-05-22 10:37:30 -0700183 }
bsalomon682c2692015-05-22 14:01:46 -0700184 GrGLContext* glContext = GrGLContext::Create(glInterface, options);
bsalomon424cc262015-05-22 10:37:30 -0700185 if (glContext) {
halcanary385fe4d2015-08-26 13:07:48 -0700186 return new GrGLGpu(glContext, context);
bsalomon424cc262015-05-22 10:37:30 -0700187 }
halcanary96fcdcc2015-08-27 07:41:13 -0700188 return nullptr;
bsalomon424cc262015-05-22 10:37:30 -0700189}
190
rileya@google.come38160c2012-07-03 18:03:04 +0000191static bool gPrintStartupSpew;
bsalomon@google.com42ab7ea2011-01-19 17:19:40 +0000192
bsalomon424cc262015-05-22 10:37:30 -0700193GrGLGpu::GrGLGpu(GrGLContext* ctx, GrContext* context)
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000194 : GrGpu(context)
robertphillips@google.com6177e692013-02-28 20:16:25 +0000195 , fGLContext(ctx) {
bsalomon424cc262015-05-22 10:37:30 -0700196 SkASSERT(ctx);
197 fCaps.reset(SkRef(ctx->caps()));
bsalomon@google.combcce8922013-03-25 15:38:39 +0000198
bsalomon1c63bf62014-07-22 13:09:46 -0700199 fHWBoundTextureUniqueIDs.reset(this->glCaps().maxFragmentTextureUnits());
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +0000200
bsalomon424cc262015-05-22 10:37:30 -0700201 GrGLClearErr(this->glInterface());
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000202 if (gPrintStartupSpew) {
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000203 const GrGLubyte* vendor;
204 const GrGLubyte* renderer;
205 const GrGLubyte* version;
206 GL_CALL_RET(vendor, GetString(GR_GL_VENDOR));
207 GL_CALL_RET(renderer, GetString(GR_GL_RENDERER));
208 GL_CALL_RET(version, GetString(GR_GL_VERSION));
bsalomon861e1032014-12-16 07:33:49 -0800209 SkDebugf("------------------------- create GrGLGpu %p --------------\n",
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000210 this);
tfarina38406c82014-10-31 07:11:12 -0700211 SkDebugf("------ VENDOR %s\n", vendor);
212 SkDebugf("------ RENDERER %s\n", renderer);
213 SkDebugf("------ VERSION %s\n", version);
214 SkDebugf("------ EXTENSIONS\n");
bsalomon424cc262015-05-22 10:37:30 -0700215 this->glContext().extensions().print();
tfarina38406c82014-10-31 07:11:12 -0700216 SkDebugf("\n");
kkinnunen297aaf92015-02-19 06:32:12 -0800217 SkDebugf("%s", this->glCaps().dump().c_str());
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000218 }
219
halcanary385fe4d2015-08-26 13:07:48 -0700220 fProgramCache = new ProgramCache(this);
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000221
joshualitt2dd1ae02014-12-03 06:24:10 -0800222 SkASSERT(this->glCaps().maxVertexAttributes() >= GrGeometryProcessor::kMaxVertexAttribs);
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000223
egdanielff1d5472015-09-10 08:37:20 -0700224 for (int i = 0; i < kGrPixelConfigCnt; ++i) {
225 fPixelConfigToStencilIndex[i] = kUnknownStencilIndex;
226 }
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000227 fHWProgramID = 0;
egdanield803f272015-03-18 13:01:52 -0700228 fTempSrcFBOID = 0;
229 fTempDstFBOID = 0;
230 fStencilClearFBOID = 0;
cdaltonc7103a12014-08-11 14:05:05 -0700231
jvanverthe9c0fc62015-04-29 11:18:05 -0700232 if (this->glCaps().shaderCaps()->pathRenderingSupport()) {
kkinnunen5b653572014-08-20 04:13:27 -0700233 fPathRendering.reset(new GrGLPathRendering(this));
cdaltonc7103a12014-08-11 14:05:05 -0700234 }
bsalomon6df86402015-06-01 10:41:49 -0700235
bsalomon0315dbc2015-11-20 20:24:31 -0800236 this->createCopyProgram();
reed@google.comac10a2d2010-12-22 21:39:39 +0000237}
238
bsalomon861e1032014-12-16 07:33:49 -0800239GrGLGpu::~GrGLGpu() {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000240 if (0 != fHWProgramID) {
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000241 // detach the current program so there is no confusion on OpenGL's part
242 // that we want it to be deleted
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000243 GL_CALL(UseProgram(0));
244 }
245
egdanield803f272015-03-18 13:01:52 -0700246 if (0 != fTempSrcFBOID) {
247 GL_CALL(DeleteFramebuffers(1, &fTempSrcFBOID));
egdaniel0f5f9672015-02-03 11:10:51 -0800248 }
egdanield803f272015-03-18 13:01:52 -0700249 if (0 != fTempDstFBOID) {
250 GL_CALL(DeleteFramebuffers(1, &fTempDstFBOID));
egdaniel0f5f9672015-02-03 11:10:51 -0800251 }
egdanield803f272015-03-18 13:01:52 -0700252 if (0 != fStencilClearFBOID) {
253 GL_CALL(DeleteFramebuffers(1, &fStencilClearFBOID));
bsalomondd3143b2015-02-23 09:27:45 -0800254 }
egdaniel0f5f9672015-02-03 11:10:51 -0800255
bsalomon0315dbc2015-11-20 20:24:31 -0800256 if (0 != fCopyProgram.fArrayBuffer) {
257 GL_CALL(DeleteBuffers(1, &fCopyProgram.fArrayBuffer));
bsalomon6df86402015-06-01 10:41:49 -0700258 }
bsalomon0315dbc2015-11-20 20:24:31 -0800259
260 if (0 != fCopyProgram.fProgram) {
261 GL_CALL(DeleteProgram(fCopyProgram.fProgram));
bsalomon6df86402015-06-01 10:41:49 -0700262 }
263
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000264 delete fProgramCache;
bsalomonc8dc1f72014-08-21 13:02:13 -0700265}
266
bsalomon861e1032014-12-16 07:33:49 -0800267void GrGLGpu::contextAbandoned() {
robertphillipse3371302014-09-17 06:01:06 -0700268 INHERITED::contextAbandoned();
bsalomonc8dc1f72014-08-21 13:02:13 -0700269 fProgramCache->abandon();
270 fHWProgramID = 0;
egdanield803f272015-03-18 13:01:52 -0700271 fTempSrcFBOID = 0;
272 fTempDstFBOID = 0;
273 fStencilClearFBOID = 0;
bsalomon0315dbc2015-11-20 20:24:31 -0800274 fCopyProgram.fArrayBuffer = 0;
275 fCopyProgram.fProgram = 0;
jvanverthe9c0fc62015-04-29 11:18:05 -0700276 if (this->glCaps().shaderCaps()->pathRenderingSupport()) {
bsalomonc8dc1f72014-08-21 13:02:13 -0700277 this->glPathRendering()->abandonGpuResources();
278 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000279}
280
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000281///////////////////////////////////////////////////////////////////////////////
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000282
bsalomon861e1032014-12-16 07:33:49 -0800283void GrGLGpu::onResetContext(uint32_t resetBits) {
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000284 // we don't use the zb at all
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000285 if (resetBits & kMisc_GrGLBackendState) {
286 GL_CALL(Disable(GR_GL_DEPTH_TEST));
287 GL_CALL(DepthMask(GR_GL_FALSE));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000288
egdaniel8dd688b2015-01-22 10:16:09 -0800289 fHWDrawFace = GrPipelineBuilder::kInvalid_DrawFace;
reed@google.comac10a2d2010-12-22 21:39:39 +0000290
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +0000291 if (kGL_GrGLStandard == this->glStandard()) {
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000292 // Desktop-only state that we never change
293 if (!this->glCaps().isCoreProfile()) {
294 GL_CALL(Disable(GR_GL_POINT_SMOOTH));
295 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
296 GL_CALL(Disable(GR_GL_POLYGON_SMOOTH));
297 GL_CALL(Disable(GR_GL_POLYGON_STIPPLE));
298 GL_CALL(Disable(GR_GL_COLOR_LOGIC_OP));
299 GL_CALL(Disable(GR_GL_INDEX_LOGIC_OP));
300 }
301 // The windows NVIDIA driver has GL_ARB_imaging in the extension string when using a
302 // core profile. This seems like a bug since the core spec removes any mention of
303 // GL_ARB_imaging.
304 if (this->glCaps().imagingSupport() && !this->glCaps().isCoreProfile()) {
305 GL_CALL(Disable(GR_GL_COLOR_TABLE));
306 }
307 GL_CALL(Disable(GR_GL_POLYGON_OFFSET_FILL));
308 // Since ES doesn't support glPointSize at all we always use the VS to
309 // set the point size
310 GL_CALL(Enable(GR_GL_VERTEX_PROGRAM_POINT_SIZE));
311
312 // We should set glPolygonMode(FRONT_AND_BACK,FILL) here, too. It isn't
313 // currently part of our gl interface. There are probably others as
314 // well.
bsalomon@google.com2b1b8c02013-02-28 22:06:02 +0000315 }
joshualitt58162332014-08-01 06:44:53 -0700316
317 if (kGLES_GrGLStandard == this->glStandard() &&
bsalomon424cc262015-05-22 10:37:30 -0700318 this->hasExtension("GL_ARM_shader_framebuffer_fetch")) {
joshualitt58162332014-08-01 06:44:53 -0700319 // The arm extension requires specifically enabling MSAA fetching per sample.
320 // On some devices this may have a perf hit. Also multiple render targets are disabled
321 GL_CALL(Enable(GR_GL_FETCH_PER_SAMPLE_ARM));
322 }
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000323 fHWWriteToColor = kUnknown_TriState;
324 // we only ever use lines in hairline mode
325 GL_CALL(LineWidth(1));
bsalomonaca31fe2015-09-22 11:38:46 -0700326 GL_CALL(Disable(GR_GL_DITHER));
bsalomon@google.comcad107b2013-06-28 14:32:08 +0000327 }
edisonn@google.comba669992013-06-28 16:03:21 +0000328
egdanielb414f252014-07-29 13:15:47 -0700329 if (resetBits & kMSAAEnable_GrGLBackendState) {
330 fMSAAEnabled = kUnknown_TriState;
vbuzinovdded6962015-06-12 08:59:45 -0700331
332 // In mixed samples mode coverage modulation allows the coverage to be converted to
333 // "opacity", which can then be blended into the color buffer to accomplish antialiasing.
334 // Enable coverage modulation suitable for premultiplied alpha colors.
335 // This state has no effect when not rendering to a mixed sampled target.
cdalton63f6c1f2015-11-06 07:09:43 -0800336 if (this->caps()->mixedSamplesSupport()) {
vbuzinovdded6962015-06-12 08:59:45 -0700337 GL_CALL(CoverageModulation(GR_GL_RGBA));
338 }
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000339 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000340
commit-bot@chromium.org46fbfe02013-08-30 15:52:12 +0000341 fHWActiveTextureUnitIdx = -1; // invalid
342
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000343 if (resetBits & kTextureBinding_GrGLBackendState) {
bsalomon1c63bf62014-07-22 13:09:46 -0700344 for (int s = 0; s < fHWBoundTextureUniqueIDs.count(); ++s) {
345 fHWBoundTextureUniqueIDs[s] = SK_InvalidUniqueID;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000346 }
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000347 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000348
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000349 if (resetBits & kBlend_GrGLBackendState) {
350 fHWBlendState.invalidate();
351 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000352
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000353 if (resetBits & kView_GrGLBackendState) {
354 fHWScissorSettings.invalidate();
355 fHWViewport.invalidate();
356 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000357
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000358 if (resetBits & kStencil_GrGLBackendState) {
359 fHWStencilSettings.invalidate();
360 fHWStencilTestEnabled = kUnknown_TriState;
361 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000362
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000363 // Vertex
364 if (resetBits & kVertex_GrGLBackendState) {
365 fHWGeometryState.invalidate();
366 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000367
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000368 if (resetBits & kRenderTarget_GrGLBackendState) {
egdanield803f272015-03-18 13:01:52 -0700369 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
bsalomon16921ec2015-07-30 15:34:56 -0700370 fHWSRGBFramebuffer = kUnknown_TriState;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000371 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000372
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000373 if (resetBits & kPathRendering_GrGLBackendState) {
jvanverthe9c0fc62015-04-29 11:18:05 -0700374 if (this->caps()->shaderCaps()->pathRenderingSupport()) {
kkinnunenccdaa042014-08-20 01:36:23 -0700375 this->glPathRendering()->resetContext();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000376 }
bsalomon@google.com05a718c2012-06-29 14:01:53 +0000377 }
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000378
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000379 // we assume these values
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000380 if (resetBits & kPixelStore_GrGLBackendState) {
381 if (this->glCaps().unpackRowLengthSupport()) {
382 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
383 }
384 if (this->glCaps().packRowLengthSupport()) {
385 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
386 }
387 if (this->glCaps().unpackFlipYSupport()) {
388 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
389 }
390 if (this->glCaps().packFlipYSupport()) {
391 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, GR_GL_FALSE));
392 }
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000393 }
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000394
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000395 if (resetBits & kProgram_GrGLBackendState) {
396 fHWProgramID = 0;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000397 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000398}
399
egdanielcf614fd2015-04-22 13:58:58 -0700400static GrSurfaceOrigin resolve_origin(GrSurfaceOrigin origin, bool renderTarget) {
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000401 // By default, GrRenderTargets are GL's normal orientation so that they
402 // can be drawn to by the outside world without the client having
403 // to render upside down.
404 if (kDefault_GrSurfaceOrigin == origin) {
405 return renderTarget ? kBottomLeft_GrSurfaceOrigin : kTopLeft_GrSurfaceOrigin;
406 } else {
407 return origin;
408 }
409}
410
bsalomon6dc6f5f2015-06-18 09:12:16 -0700411GrTexture* GrGLGpu::onWrapBackendTexture(const GrBackendTextureDesc& desc,
412 GrWrapOwnership ownership) {
halcanary96fcdcc2015-08-27 07:41:13 -0700413 if (!this->configToGLFormats(desc.fConfig, false, nullptr, nullptr, nullptr)) {
414 return nullptr;
bsalomon@google.come269f212011-11-07 13:29:52 +0000415 }
bsalomon091f60c2015-11-10 11:54:56 -0800416#ifdef SK_IGNORE_GL_TEXTURE_TARGET
417 if (!desc.fTextureHandle) {
halcanary96fcdcc2015-08-27 07:41:13 -0700418 return nullptr;
robertphillips@google.comb72e5d32012-10-30 15:18:10 +0000419 }
bsalomon091f60c2015-11-10 11:54:56 -0800420#else
421 const GrGLTextureInfo* info = reinterpret_cast<const GrGLTextureInfo*>(desc.fTextureHandle);
422 if (!info || !info->fID) {
423 return nullptr;
424 }
425#endif
robertphillips@google.comb72e5d32012-10-30 15:18:10 +0000426
bsalomon@google.combcce8922013-03-25 15:38:39 +0000427 int maxSize = this->caps()->maxTextureSize();
robertphillips@google.comb72e5d32012-10-30 15:18:10 +0000428 if (desc.fWidth > maxSize || desc.fHeight > maxSize) {
halcanary96fcdcc2015-08-27 07:41:13 -0700429 return nullptr;
robertphillips@google.comb72e5d32012-10-30 15:18:10 +0000430 }
431
bsalomonb15b4c12014-10-29 12:41:57 -0700432 GrGLTexture::IDDesc idDesc;
433 GrSurfaceDesc surfDesc;
434
bsalomon091f60c2015-11-10 11:54:56 -0800435#ifdef SK_IGNORE_GL_TEXTURE_TARGET
436 idDesc.fInfo.fID = static_cast<GrGLuint>(desc.fTextureHandle);
bsalomon10528f12015-10-14 12:54:52 -0700437 // We only support GL_TEXTURE_2D at the moment.
bsalomon091f60c2015-11-10 11:54:56 -0800438 idDesc.fInfo.fTarget = GR_GL_TEXTURE_2D;
439#else
440 idDesc.fInfo = *info;
441#endif
bsalomon10528f12015-10-14 12:54:52 -0700442
bsalomon6dc6f5f2015-06-18 09:12:16 -0700443 switch (ownership) {
444 case kAdopt_GrWrapOwnership:
445 idDesc.fLifeCycle = GrGpuResource::kAdopted_LifeCycle;
446 break;
447 case kBorrow_GrWrapOwnership:
448 idDesc.fLifeCycle = GrGpuResource::kBorrowed_LifeCycle;
449 break;
450 }
bsalomonb15b4c12014-10-29 12:41:57 -0700451
bsalomon0315dbc2015-11-20 20:24:31 -0800452 // next line relies on GrBackendTextureDesc's flags matching GrTexture's
bsalomonb15b4c12014-10-29 12:41:57 -0700453 surfDesc.fFlags = (GrSurfaceFlags) desc.fFlags;
454 surfDesc.fWidth = desc.fWidth;
455 surfDesc.fHeight = desc.fHeight;
456 surfDesc.fConfig = desc.fConfig;
senorblanco94e50102015-04-06 09:42:57 -0700457 surfDesc.fSampleCnt = SkTMin(desc.fSampleCnt, this->caps()->maxSampleCount());
bsalomon0315dbc2015-11-20 20:24:31 -0800458 bool renderTarget = SkToBool(desc.fFlags & kRenderTarget_GrBackendTextureFlag);
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000459 // FIXME: this should be calling resolve_origin(), but Chrome code is currently
460 // assuming the old behaviour, which is that backend textures are always
461 // BottomLeft, even for non-RT's. Once Chrome is fixed, change this to:
462 // glTexDesc.fOrigin = resolve_origin(desc.fOrigin, renderTarget);
463 if (kDefault_GrSurfaceOrigin == desc.fOrigin) {
bsalomonb15b4c12014-10-29 12:41:57 -0700464 surfDesc.fOrigin = kBottomLeft_GrSurfaceOrigin;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000465 } else {
bsalomonb15b4c12014-10-29 12:41:57 -0700466 surfDesc.fOrigin = desc.fOrigin;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000467 }
bsalomon@google.come269f212011-11-07 13:29:52 +0000468
halcanary96fcdcc2015-08-27 07:41:13 -0700469 GrGLTexture* texture = nullptr;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000470 if (renderTarget) {
bsalomonb15b4c12014-10-29 12:41:57 -0700471 GrGLRenderTarget::IDDesc rtIDDesc;
egdanielb0e1be22015-04-22 13:27:39 -0700472 if (!this->createRenderTargetObjects(surfDesc, GrGpuResource::kUncached_LifeCycle,
bsalomon091f60c2015-11-10 11:54:56 -0800473 idDesc.fInfo, &rtIDDesc)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700474 return nullptr;
bsalomon@google.come269f212011-11-07 13:29:52 +0000475 }
halcanary385fe4d2015-08-26 13:07:48 -0700476 texture = new GrGLTextureRenderTarget(this, surfDesc, idDesc, rtIDDesc);
bsalomon@google.come269f212011-11-07 13:29:52 +0000477 } else {
halcanary385fe4d2015-08-26 13:07:48 -0700478 texture = new GrGLTexture(this, surfDesc, idDesc);
bsalomon@google.come269f212011-11-07 13:29:52 +0000479 }
halcanary96fcdcc2015-08-27 07:41:13 -0700480 if (nullptr == texture) {
481 return nullptr;
bsalomon@google.come269f212011-11-07 13:29:52 +0000482 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000483
bsalomon@google.come269f212011-11-07 13:29:52 +0000484 return texture;
485}
486
bsalomon6dc6f5f2015-06-18 09:12:16 -0700487GrRenderTarget* GrGLGpu::onWrapBackendRenderTarget(const GrBackendRenderTargetDesc& wrapDesc,
488 GrWrapOwnership ownership) {
bsalomonb15b4c12014-10-29 12:41:57 -0700489 GrGLRenderTarget::IDDesc idDesc;
egdanield803f272015-03-18 13:01:52 -0700490 idDesc.fRTFBOID = static_cast<GrGLuint>(wrapDesc.fRenderTargetHandle);
bsalomonb15b4c12014-10-29 12:41:57 -0700491 idDesc.fMSColorRenderbufferID = 0;
egdanield803f272015-03-18 13:01:52 -0700492 idDesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
bsalomon6dc6f5f2015-06-18 09:12:16 -0700493 switch (ownership) {
494 case kAdopt_GrWrapOwnership:
495 idDesc.fLifeCycle = GrGpuResource::kAdopted_LifeCycle;
496 break;
497 case kBorrow_GrWrapOwnership:
498 idDesc.fLifeCycle = GrGpuResource::kBorrowed_LifeCycle;
499 break;
500 }
senorblancod7395d82015-06-18 13:26:52 -0700501 idDesc.fSampleConfig = GrRenderTarget::kUnified_SampleConfig;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000502
bsalomonb15b4c12014-10-29 12:41:57 -0700503 GrSurfaceDesc desc;
504 desc.fConfig = wrapDesc.fConfig;
senorblanco4b013292015-08-19 09:10:28 -0700505 desc.fFlags = kCheckAllocation_GrSurfaceFlag | kRenderTarget_GrSurfaceFlag;
bsalomonb15b4c12014-10-29 12:41:57 -0700506 desc.fWidth = wrapDesc.fWidth;
507 desc.fHeight = wrapDesc.fHeight;
senorblanco94e50102015-04-06 09:42:57 -0700508 desc.fSampleCnt = SkTMin(wrapDesc.fSampleCnt, this->caps()->maxSampleCount());
bsalomonb15b4c12014-10-29 12:41:57 -0700509 desc.fOrigin = resolve_origin(wrapDesc.fOrigin, true);
510
egdanielec00d942015-09-14 12:56:10 -0700511 return GrGLRenderTarget::CreateWrapped(this, desc, idDesc, wrapDesc.fStencilBits);
bsalomon@google.come269f212011-11-07 13:29:52 +0000512}
513
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000514////////////////////////////////////////////////////////////////////////////////
bsalomonf0674512015-07-28 13:26:15 -0700515bool GrGLGpu::onGetWritePixelsInfo(GrSurface* dstSurface, int width, int height,
516 size_t rowBytes, GrPixelConfig srcConfig,
517 DrawPreference* drawPreference,
518 WritePixelTempDrawInfo* tempDrawInfo) {
519 if (kIndex_8_GrPixelConfig == srcConfig || GrPixelConfigIsCompressed(dstSurface->config())) {
520 return false;
521 }
522
bsalomon6cb3cbe2015-07-30 07:34:27 -0700523 // This subclass only allows writes to textures. If the dst is not a texture we have to draw
524 // into it. We could use glDrawPixels on GLs that have it, but we don't today.
525 if (!dstSurface->asTexture()) {
526 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
527 }
528
bsalomon16921ec2015-07-30 15:34:56 -0700529 if (GrPixelConfigIsSRGB(dstSurface->config()) != GrPixelConfigIsSRGB(srcConfig)) {
530 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
531 }
532
bsalomonf0674512015-07-28 13:26:15 -0700533 tempDrawInfo->fSwapRAndB = false;
534
535 // These settings we will always want if a temp draw is performed. Initially set the config
536 // to srcConfig, though that may be modified if we decide to do a R/G swap.
537 tempDrawInfo->fTempSurfaceDesc.fFlags = kNone_GrSurfaceFlags;
538 tempDrawInfo->fTempSurfaceDesc.fConfig = srcConfig;
539 tempDrawInfo->fTempSurfaceDesc.fWidth = width;
540 tempDrawInfo->fTempSurfaceDesc.fHeight = height;
541 tempDrawInfo->fTempSurfaceDesc.fSampleCnt = 0;
542 tempDrawInfo->fTempSurfaceDesc.fOrigin = kTopLeft_GrSurfaceOrigin; // no CPU y-flip for TL.
543
544 bool configsAreRBSwaps = GrPixelConfigSwapRAndB(srcConfig) == dstSurface->config();
545
546 if (configsAreRBSwaps) {
547 if (!this->caps()->isConfigTexturable(srcConfig)) {
548 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
549 tempDrawInfo->fTempSurfaceDesc.fConfig = dstSurface->config();
550 tempDrawInfo->fSwapRAndB = true;
bsalomon88c7b982015-07-31 11:20:16 -0700551 } else if (this->glCaps().rgba8888PixelsOpsAreSlow() &&
552 kRGBA_8888_GrPixelConfig == srcConfig) {
bsalomonf0674512015-07-28 13:26:15 -0700553 ElevateDrawPreference(drawPreference, kGpuPrefersDraw_DrawPreference);
554 tempDrawInfo->fTempSurfaceDesc.fConfig = dstSurface->config();
555 tempDrawInfo->fSwapRAndB = true;
556 } else if (kGLES_GrGLStandard == this->glStandard() &&
557 this->glCaps().bgraIsInternalFormat()) {
558 // The internal format and external formats must match texture uploads so we can't
559 // swizzle while uploading when BGRA is a distinct internal format.
560 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
561 tempDrawInfo->fTempSurfaceDesc.fConfig = dstSurface->config();
562 tempDrawInfo->fSwapRAndB = true;
563 }
564 }
565
566 if (!this->glCaps().unpackFlipYSupport() &&
567 kBottomLeft_GrSurfaceOrigin == dstSurface->origin()) {
568 ElevateDrawPreference(drawPreference, kGpuPrefersDraw_DrawPreference);
569 }
570
571 return true;
572}
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000573
bsalomon6cb3cbe2015-07-30 07:34:27 -0700574bool GrGLGpu::onWritePixels(GrSurface* surface,
575 int left, int top, int width, int height,
576 GrPixelConfig config, const void* buffer,
577 size_t rowBytes) {
bsalomon6cb3cbe2015-07-30 07:34:27 -0700578 GrGLTexture* glTex = static_cast<GrGLTexture*>(surface->asTexture());
579 if (!glTex) {
580 return false;
581 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000582
bsalomon16921ec2015-07-30 15:34:56 -0700583 // OpenGL doesn't do sRGB <-> linear conversions when reading and writing pixels.
584 if (GrPixelConfigIsSRGB(surface->config()) != GrPixelConfigIsSRGB(config)) {
585 return false;
586 }
587
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +0000588 this->setScratchTextureUnit();
bsalomon10528f12015-10-14 12:54:52 -0700589 GL_CALL(BindTexture(glTex->target(), glTex->textureID()));
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000590
krajcevski145d48c2014-06-11 16:07:50 -0700591 bool success = false;
bsalomonb15b4c12014-10-29 12:41:57 -0700592 if (GrPixelConfigIsCompressed(glTex->desc().fConfig)) {
bsalomon861e1032014-12-16 07:33:49 -0800593 // We check that config == desc.fConfig in GrGLGpu::canWriteTexturePixels()
bsalomonb15b4c12014-10-29 12:41:57 -0700594 SkASSERT(config == glTex->desc().fConfig);
bsalomon10528f12015-10-14 12:54:52 -0700595 success = this->uploadCompressedTexData(glTex->desc(), glTex->target(), buffer, false, left,
596 top, width, height);
krajcevski145d48c2014-06-11 16:07:50 -0700597 } else {
bsalomon10528f12015-10-14 12:54:52 -0700598 success = this->uploadTexData(glTex->desc(), glTex->target(), false, left, top, width,
599 height, config, buffer, rowBytes);
krajcevski145d48c2014-06-11 16:07:50 -0700600 }
601
602 if (success) {
bsalomon6cb3cbe2015-07-30 07:34:27 -0700603 glTex->texturePriv().dirtyMipMaps(true);
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +0000604 return true;
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +0000605 }
krajcevski145d48c2014-06-11 16:07:50 -0700606
607 return false;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000608}
609
bsalomonb15b4c12014-10-29 12:41:57 -0700610static inline GrGLenum check_alloc_error(const GrSurfaceDesc& desc,
611 const GrGLInterface* interface) {
bsalomonf2703d82014-10-28 14:33:06 -0700612 if (SkToBool(desc.fFlags & kCheckAllocation_GrSurfaceFlag)) {
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000613 return GR_GL_GET_ERROR(interface);
614 } else {
615 return CHECK_ALLOC_ERROR(interface);
616 }
617}
618
bsalomon861e1032014-12-16 07:33:49 -0800619bool GrGLGpu::uploadTexData(const GrSurfaceDesc& desc,
bsalomon10528f12015-10-14 12:54:52 -0700620 GrGLenum target,
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000621 bool isNewTexture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000622 int left, int top, int width, int height,
623 GrPixelConfig dataConfig,
624 const void* data,
625 size_t rowBytes) {
bsalomon49f085d2014-09-05 13:34:00 -0700626 SkASSERT(data || isNewTexture);
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000627
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +0000628 // If we're uploading compressed data then we should be using uploadCompressedTexData
629 SkASSERT(!GrPixelConfigIsCompressed(dataConfig));
630
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000631 size_t bpp = GrBytesPerPixel(dataConfig);
bsalomone8d21e82015-07-16 08:23:13 -0700632 if (!GrSurfacePriv::AdjustWritePixelParams(desc.fWidth, desc.fHeight, bpp, &left, &top,
633 &width, &height, &data, &rowBytes)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000634 return false;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000635 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000636 size_t trimRowBytes = width * bpp;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000637
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000638 // in case we need a temporary, trimmed copy of the src pixels
benjaminwagner7d974f52015-10-19 13:55:55 -0700639#if defined(GOOGLE3)
640 // Stack frame size is limited in GOOGLE3.
641 SkAutoSMalloc<64 * 128> tempStorage;
642#else
joshualitt29f86792015-05-29 08:06:48 -0700643 SkAutoSMalloc<128 * 128> tempStorage;
benjaminwagner7d974f52015-10-19 13:55:55 -0700644#endif
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000645
commit-bot@chromium.orgcea9abb2013-12-09 19:15:37 +0000646 // We currently lazily create MIPMAPs when the we see a draw with
647 // GrTextureParams::kMipMap_FilterMode. Using texture storage requires that the
648 // MIP levels are all created when the texture is created. So for now we don't use
649 // texture storage.
650 bool useTexStorage = false &&
651 isNewTexture &&
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000652 this->glCaps().texStorageSupport();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000653
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +0000654 if (useTexStorage && kGL_GrGLStandard == this->glStandard()) {
bsalomon@google.com313f0192012-07-10 17:21:02 +0000655 // 565 is not a sized internal format on desktop GL. So on desktop with
656 // 565 we always use an unsized internal format to let the system pick
657 // the best sized format to convert the 565 data to. Since TexStorage
658 // only allows sized internal formats we will instead use TexImage2D.
659 useTexStorage = desc.fConfig != kRGB_565_GrPixelConfig;
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000660 }
661
jvanverth3f801cb2014-12-16 09:49:38 -0800662 GrGLenum internalFormat = 0x0; // suppress warning
663 GrGLenum externalFormat = 0x0; // suppress warning
664 GrGLenum externalType = 0x0; // suppress warning
bsalomone904c092014-07-17 10:50:59 -0700665
jvanverthe1869ca2014-12-16 13:32:27 -0800666 // glTexStorage requires sized internal formats on both desktop and ES. ES2 requires an unsized
jvanverthf72b7522014-12-17 10:46:01 -0800667 // format for glTexImage, unlike ES3 and desktop.
jvanverthe1869ca2014-12-16 13:32:27 -0800668 bool useSizedFormat = useTexStorage;
jvanverthf72b7522014-12-17 10:46:01 -0800669 if (kGL_GrGLStandard == this->glStandard() ||
670 (this->glVersion() >= GR_GL_VER(3, 0) &&
671 // ES3 only works with sized BGRA8 format if "GL_APPLE_texture_format_BGRA8888" enabled
672 (kBGRA_8888_GrPixelConfig != dataConfig || !this->glCaps().bgraIsInternalFormat()))) {
jvanverthe1869ca2014-12-16 13:32:27 -0800673 useSizedFormat = true;
674 }
jvanverthf72b7522014-12-17 10:46:01 -0800675
bsalomone904c092014-07-17 10:50:59 -0700676 if (!this->configToGLFormats(dataConfig, useSizedFormat, &internalFormat,
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000677 &externalFormat, &externalType)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000678 return false;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000679 }
680
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000681 /*
bsalomon@google.com6f379512011-11-16 20:36:03 +0000682 * check whether to allocate a temporary buffer for flipping y or
683 * because our srcData has extra bytes past each row. If so, we need
684 * to trim those off here, since GL ES may not let us specify
685 * GL_UNPACK_ROW_LENGTH.
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000686 */
bsalomon@google.com6f379512011-11-16 20:36:03 +0000687 bool restoreGLRowLength = false;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000688 bool swFlipY = false;
689 bool glFlipY = false;
bsalomon49f085d2014-09-05 13:34:00 -0700690 if (data) {
senorblanco@chromium.orgef5dbe12013-01-28 16:42:38 +0000691 if (kBottomLeft_GrSurfaceOrigin == desc.fOrigin) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000692 if (this->glCaps().unpackFlipYSupport()) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000693 glFlipY = true;
694 } else {
695 swFlipY = true;
696 }
697 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000698 if (this->glCaps().unpackRowLengthSupport() && !swFlipY) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000699 // can't use this for flipping, only non-neg values allowed. :(
700 if (rowBytes != trimRowBytes) {
701 GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
702 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength));
703 restoreGLRowLength = true;
704 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000705 } else {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000706 if (trimRowBytes != rowBytes || swFlipY) {
707 // copy data into our new storage, skipping the trailing bytes
708 size_t trimSize = height * trimRowBytes;
709 const char* src = (const char*)data;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000710 if (swFlipY) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000711 src += (height - 1) * rowBytes;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000712 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000713 char* dst = (char*)tempStorage.reset(trimSize);
714 for (int y = 0; y < height; y++) {
715 memcpy(dst, src, trimRowBytes);
716 if (swFlipY) {
717 src -= rowBytes;
718 } else {
719 src += rowBytes;
720 }
721 dst += trimRowBytes;
722 }
723 // now point data to our copied version
724 data = tempStorage.get();
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000725 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000726 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000727 if (glFlipY) {
728 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_TRUE));
729 }
joshualittee5da552014-07-16 13:32:56 -0700730 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT,
731 static_cast<GrGLint>(GrUnpackAlignment(dataConfig))));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000732 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000733 bool succeeded = true;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000734 if (isNewTexture &&
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000735 0 == left && 0 == top &&
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000736 desc.fWidth == width && desc.fHeight == height) {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000737 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000738 if (useTexStorage) {
commit-bot@chromium.orgcea9abb2013-12-09 19:15:37 +0000739 // We never resize or change formats of textures.
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000740 GL_ALLOC_CALL(this->glInterface(),
bsalomon10528f12015-10-14 12:54:52 -0700741 TexStorage2D(target,
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000742 1, // levels
743 internalFormat,
744 desc.fWidth, desc.fHeight));
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000745 } else {
bsalomond4cb9222014-08-11 14:19:09 -0700746 GL_ALLOC_CALL(this->glInterface(),
bsalomon10528f12015-10-14 12:54:52 -0700747 TexImage2D(target,
bsalomond4cb9222014-08-11 14:19:09 -0700748 0, // level
749 internalFormat,
750 desc.fWidth, desc.fHeight,
751 0, // border
752 externalFormat, externalType,
753 data));
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000754 }
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000755 GrGLenum error = check_alloc_error(desc, this->glInterface());
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000756 if (error != GR_GL_NO_ERROR) {
757 succeeded = false;
758 } else {
759 // if we have data and we used TexStorage to create the texture, we
760 // now upload with TexSubImage.
bsalomon49f085d2014-09-05 13:34:00 -0700761 if (data && useTexStorage) {
bsalomon10528f12015-10-14 12:54:52 -0700762 GL_CALL(TexSubImage2D(target,
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000763 0, // level
764 left, top,
765 width, height,
766 externalFormat, externalType,
767 data));
768 }
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000769 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000770 } else {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000771 if (swFlipY || glFlipY) {
bsalomon@google.com6f379512011-11-16 20:36:03 +0000772 top = desc.fHeight - (top + height);
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000773 }
bsalomon10528f12015-10-14 12:54:52 -0700774 GL_CALL(TexSubImage2D(target,
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000775 0, // level
776 left, top,
777 width, height,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000778 externalFormat, externalType, data));
779 }
780
781 if (restoreGLRowLength) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000782 SkASSERT(this->glCaps().unpackRowLengthSupport());
bsalomon@google.com6f379512011-11-16 20:36:03 +0000783 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000784 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000785 if (glFlipY) {
786 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
787 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000788 return succeeded;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000789}
790
krajcevski145d48c2014-06-11 16:07:50 -0700791// TODO: This function is using a lot of wonky semantics like, if width == -1
piotaixre4b23142014-10-02 10:57:53 -0700792// then set width = desc.fWdith ... blah. A better way to do it might be to
krajcevski145d48c2014-06-11 16:07:50 -0700793// create a CompressedTexData struct that takes a desc/ptr and figures out
794// the proper upload semantics. Then users can construct this function how they
795// see fit if they want to go against the "standard" way to do it.
bsalomon861e1032014-12-16 07:33:49 -0800796bool GrGLGpu::uploadCompressedTexData(const GrSurfaceDesc& desc,
bsalomon10528f12015-10-14 12:54:52 -0700797 GrGLenum target,
krajcevski145d48c2014-06-11 16:07:50 -0700798 const void* data,
799 bool isNewTexture,
800 int left, int top, int width, int height) {
bsalomon49f085d2014-09-05 13:34:00 -0700801 SkASSERT(data || isNewTexture);
krajcevski9c0e6292014-06-02 07:38:14 -0700802
803 // No support for software flip y, yet...
804 SkASSERT(kBottomLeft_GrSurfaceOrigin != desc.fOrigin);
805
krajcevski145d48c2014-06-11 16:07:50 -0700806 if (-1 == width) {
807 width = desc.fWidth;
808 }
809#ifdef SK_DEBUG
810 else {
811 SkASSERT(width <= desc.fWidth);
812 }
813#endif
814
815 if (-1 == height) {
816 height = desc.fHeight;
817 }
818#ifdef SK_DEBUG
819 else {
820 SkASSERT(height <= desc.fHeight);
821 }
822#endif
823
krajcevski9c0e6292014-06-02 07:38:14 -0700824 // Make sure that the width and height that we pass to OpenGL
825 // is a multiple of the block size.
bsalomon98806072014-12-12 15:11:17 -0800826 size_t dataSize = GrCompressedFormatDataSize(desc.fConfig, width, height);
krajcevski9c0e6292014-06-02 07:38:14 -0700827
828 // We only need the internal format for compressed 2D textures.
829 GrGLenum internalFormat = 0;
halcanary96fcdcc2015-08-27 07:41:13 -0700830 if (!this->configToGLFormats(desc.fConfig, false, &internalFormat, nullptr, nullptr)) {
krajcevski9c0e6292014-06-02 07:38:14 -0700831 return false;
832 }
833
krajcevski145d48c2014-06-11 16:07:50 -0700834 if (isNewTexture) {
bsalomond4cb9222014-08-11 14:19:09 -0700835 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
krajcevski145d48c2014-06-11 16:07:50 -0700836 GL_ALLOC_CALL(this->glInterface(),
bsalomon10528f12015-10-14 12:54:52 -0700837 CompressedTexImage2D(target,
krajcevski145d48c2014-06-11 16:07:50 -0700838 0, // level
839 internalFormat,
840 width, height,
841 0, // border
bsalomon98806072014-12-12 15:11:17 -0800842 SkToInt(dataSize),
krajcevski145d48c2014-06-11 16:07:50 -0700843 data));
bsalomond4cb9222014-08-11 14:19:09 -0700844 GrGLenum error = check_alloc_error(desc, this->glInterface());
845 if (error != GR_GL_NO_ERROR) {
846 return false;
847 }
krajcevski145d48c2014-06-11 16:07:50 -0700848 } else {
bsalomond4cb9222014-08-11 14:19:09 -0700849 // Paletted textures can't be updated.
850 if (GR_GL_PALETTE8_RGBA8 == internalFormat) {
851 return false;
852 }
bsalomon10528f12015-10-14 12:54:52 -0700853 GL_CALL(CompressedTexSubImage2D(target,
bsalomond4cb9222014-08-11 14:19:09 -0700854 0, // level
855 left, top,
856 width, height,
857 internalFormat,
bsalomon98806072014-12-12 15:11:17 -0800858 SkToInt(dataSize),
bsalomond4cb9222014-08-11 14:19:09 -0700859 data));
krajcevski145d48c2014-06-11 16:07:50 -0700860 }
krajcevski9c0e6292014-06-02 07:38:14 -0700861
bsalomond4cb9222014-08-11 14:19:09 -0700862 return true;
krajcevski9c0e6292014-06-02 07:38:14 -0700863}
864
bsalomon424cc262015-05-22 10:37:30 -0700865static bool renderbuffer_storage_msaa(const GrGLContext& ctx,
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +0000866 int sampleCount,
867 GrGLenum format,
868 int width, int height) {
robertphillips@google.com6177e692013-02-28 20:16:25 +0000869 CLEAR_ERROR_BEFORE_ALLOC(ctx.interface());
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +0000870 SkASSERT(GrGLCaps::kNone_MSFBOType != ctx.caps()->msFBOType());
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +0000871 switch (ctx.caps()->msFBOType()) {
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +0000872 case GrGLCaps::kDesktop_ARB_MSFBOType:
873 case GrGLCaps::kDesktop_EXT_MSFBOType:
vbuzinovdded6962015-06-12 08:59:45 -0700874 case GrGLCaps::kMixedSamples_MSFBOType:
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +0000875 case GrGLCaps::kES_3_0_MSFBOType:
876 GL_ALLOC_CALL(ctx.interface(),
877 RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
878 sampleCount,
879 format,
880 width, height));
881 break;
882 case GrGLCaps::kES_Apple_MSFBOType:
883 GL_ALLOC_CALL(ctx.interface(),
884 RenderbufferStorageMultisampleES2APPLE(GR_GL_RENDERBUFFER,
885 sampleCount,
886 format,
887 width, height));
888 break;
889 case GrGLCaps::kES_EXT_MsToTexture_MSFBOType:
890 case GrGLCaps::kES_IMG_MsToTexture_MSFBOType:
891 GL_ALLOC_CALL(ctx.interface(),
892 RenderbufferStorageMultisampleES2EXT(GR_GL_RENDERBUFFER,
893 sampleCount,
894 format,
895 width, height));
896 break;
897 case GrGLCaps::kNone_MSFBOType:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000898 SkFAIL("Shouldn't be here if we don't support multisampled renderbuffers.");
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +0000899 break;
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000900 }
Brian Salomon9251d4e2015-03-17 16:03:19 -0400901 return (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctx.interface()));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000902}
903
egdanielb0e1be22015-04-22 13:27:39 -0700904bool GrGLGpu::createRenderTargetObjects(const GrSurfaceDesc& desc,
905 GrGpuResource::LifeCycle lifeCycle,
bsalomon091f60c2015-11-10 11:54:56 -0800906 const GrGLTextureInfo& texInfo,
bsalomonb15b4c12014-10-29 12:41:57 -0700907 GrGLRenderTarget::IDDesc* idDesc) {
908 idDesc->fMSColorRenderbufferID = 0;
egdanield803f272015-03-18 13:01:52 -0700909 idDesc->fRTFBOID = 0;
910 idDesc->fTexFBOID = 0;
egdanielb0e1be22015-04-22 13:27:39 -0700911 idDesc->fLifeCycle = lifeCycle;
vbuzinovdded6962015-06-12 08:59:45 -0700912 idDesc->fSampleConfig = (GrGLCaps::kMixedSamples_MSFBOType == this->glCaps().msFBOType() &&
913 desc.fSampleCnt > 0) ? GrRenderTarget::kStencil_SampleConfig :
914 GrRenderTarget::kUnified_SampleConfig;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000915
916 GrGLenum status;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000917
bsalomon@google.comab15d612011-08-09 12:57:56 +0000918 GrGLenum msColorFormat = 0; // suppress warning
919
bsalomonb15b4c12014-10-29 12:41:57 -0700920 if (desc.fSampleCnt > 0 && GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.com347c3822013-05-01 20:10:01 +0000921 goto FAILED;
922 }
923
egdanield803f272015-03-18 13:01:52 -0700924 GL_CALL(GenFramebuffers(1, &idDesc->fTexFBOID));
925 if (!idDesc->fTexFBOID) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000926 goto FAILED;
927 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000928
egdanield803f272015-03-18 13:01:52 -0700929
bsalomon@google.comf3a60c02013-03-19 19:06:09 +0000930 // If we are using multisampling we will create two FBOS. We render to one and then resolve to
931 // the texture bound to the other. The exception is the IMG multisample extension. With this
932 // extension the texture is multisampled when rendered to and then auto-resolves it when it is
933 // rendered from.
bsalomonb15b4c12014-10-29 12:41:57 -0700934 if (desc.fSampleCnt > 0 && this->glCaps().usesMSAARenderBuffers()) {
egdanield803f272015-03-18 13:01:52 -0700935 GL_CALL(GenFramebuffers(1, &idDesc->fRTFBOID));
bsalomonb15b4c12014-10-29 12:41:57 -0700936 GL_CALL(GenRenderbuffers(1, &idDesc->fMSColorRenderbufferID));
egdanield803f272015-03-18 13:01:52 -0700937 if (!idDesc->fRTFBOID ||
938 !idDesc->fMSColorRenderbufferID ||
bsalomonb15b4c12014-10-29 12:41:57 -0700939 !this->configToGLFormats(desc.fConfig,
commit-bot@chromium.org87002952013-08-20 15:12:01 +0000940 // ES2 and ES3 require sized internal formats for rb storage.
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +0000941 kGLES_GrGLStandard == this->glStandard(),
bsalomon@google.com347c3822013-05-01 20:10:01 +0000942 &msColorFormat,
halcanary96fcdcc2015-08-27 07:41:13 -0700943 nullptr,
944 nullptr)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000945 goto FAILED;
946 }
947 } else {
egdanield803f272015-03-18 13:01:52 -0700948 idDesc->fRTFBOID = idDesc->fTexFBOID;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000949 }
950
egdanield803f272015-03-18 13:01:52 -0700951 // below here we may bind the FBO
952 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
953 if (idDesc->fRTFBOID != idDesc->fTexFBOID) {
bsalomonb15b4c12014-10-29 12:41:57 -0700954 SkASSERT(desc.fSampleCnt > 0);
955 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, idDesc->fMSColorRenderbufferID));
bsalomon424cc262015-05-22 10:37:30 -0700956 if (!renderbuffer_storage_msaa(*fGLContext,
bsalomonb15b4c12014-10-29 12:41:57 -0700957 desc.fSampleCnt,
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000958 msColorFormat,
bsalomonb15b4c12014-10-29 12:41:57 -0700959 desc.fWidth, desc.fHeight)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000960 goto FAILED;
961 }
egdanield803f272015-03-18 13:01:52 -0700962 fStats.incRenderTargetBinds();
963 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, idDesc->fRTFBOID));
964 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon10528f12015-10-14 12:54:52 -0700965 GR_GL_COLOR_ATTACHMENT0,
966 GR_GL_RENDERBUFFER,
967 idDesc->fMSColorRenderbufferID));
bsalomonb15b4c12014-10-29 12:41:57 -0700968 if ((desc.fFlags & kCheckAllocation_GrSurfaceFlag) ||
969 !this->glCaps().isConfigVerifiedColorAttachment(desc.fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000970 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
971 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
972 goto FAILED;
973 }
bsalomon424cc262015-05-22 10:37:30 -0700974 fGLContext->caps()->markConfigAsValidColorAttachment(desc.fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000975 }
976 }
egdanield803f272015-03-18 13:01:52 -0700977 fStats.incRenderTargetBinds();
978 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, idDesc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000979
bsalomonb15b4c12014-10-29 12:41:57 -0700980 if (this->glCaps().usesImplicitMSAAResolve() && desc.fSampleCnt > 0) {
egdanield803f272015-03-18 13:01:52 -0700981 GL_CALL(FramebufferTexture2DMultisample(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +0000982 GR_GL_COLOR_ATTACHMENT0,
bsalomon091f60c2015-11-10 11:54:56 -0800983 texInfo.fTarget,
984 texInfo.fID, 0, desc.fSampleCnt));
bsalomon@google.comf3a60c02013-03-19 19:06:09 +0000985 } else {
egdanield803f272015-03-18 13:01:52 -0700986 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +0000987 GR_GL_COLOR_ATTACHMENT0,
bsalomon091f60c2015-11-10 11:54:56 -0800988 texInfo.fTarget,
989 texInfo.fID, 0));
bsalomon@google.comf3a60c02013-03-19 19:06:09 +0000990 }
bsalomonb15b4c12014-10-29 12:41:57 -0700991 if ((desc.fFlags & kCheckAllocation_GrSurfaceFlag) ||
992 !this->glCaps().isConfigVerifiedColorAttachment(desc.fConfig)) {
egdanield803f272015-03-18 13:01:52 -0700993 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000994 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
995 goto FAILED;
996 }
bsalomon424cc262015-05-22 10:37:30 -0700997 fGLContext->caps()->markConfigAsValidColorAttachment(desc.fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000998 }
999
1000 return true;
1001
1002FAILED:
bsalomonb15b4c12014-10-29 12:41:57 -07001003 if (idDesc->fMSColorRenderbufferID) {
1004 GL_CALL(DeleteRenderbuffers(1, &idDesc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001005 }
egdanield803f272015-03-18 13:01:52 -07001006 if (idDesc->fRTFBOID != idDesc->fTexFBOID) {
1007 GL_CALL(DeleteFramebuffers(1, &idDesc->fRTFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001008 }
egdanield803f272015-03-18 13:01:52 -07001009 if (idDesc->fTexFBOID) {
1010 GL_CALL(DeleteFramebuffers(1, &idDesc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001011 }
1012 return false;
1013}
1014
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001015// good to set a break-point here to know when createTexture fails
1016static GrTexture* return_null_texture() {
mtklein@google.com330313a2013-08-22 15:37:26 +00001017// SkDEBUGFAIL("null texture");
halcanary96fcdcc2015-08-27 07:41:13 -07001018 return nullptr;
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001019}
1020
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +00001021#if 0 && defined(SK_DEBUG)
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001022static size_t as_size_t(int x) {
1023 return x;
1024}
1025#endif
1026
egdanielb0e1be22015-04-22 13:27:39 -07001027GrTexture* GrGLGpu::onCreateTexture(const GrSurfaceDesc& desc,
1028 GrGpuResource::LifeCycle lifeCycle,
bsalomon5236cf42015-01-14 10:42:08 -08001029 const void* srcData, size_t rowBytes) {
bsalomon@google.com8a70eef2013-03-19 13:58:55 +00001030 // We fail if the MSAA was requested and is not available.
1031 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType() && desc.fSampleCnt) {
tfarina38406c82014-10-31 07:11:12 -07001032 //SkDebugf("MSAA RT requested but not supported on this platform.");
bsalomon@google.com8a70eef2013-03-19 13:58:55 +00001033 return return_null_texture();
1034 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001035
bsalomonf2703d82014-10-28 14:33:06 -07001036 bool renderTarget = SkToBool(desc.fFlags & kRenderTarget_GrSurfaceFlag);
reed@google.comac10a2d2010-12-22 21:39:39 +00001037
bsalomonb15b4c12014-10-29 12:41:57 -07001038 GrGLTexture::IDDesc idDesc;
bsalomon091f60c2015-11-10 11:54:56 -08001039 GL_CALL(GenTextures(1, &idDesc.fInfo.fID));
egdanielb0e1be22015-04-22 13:27:39 -07001040 idDesc.fLifeCycle = lifeCycle;
bsalomon10528f12015-10-14 12:54:52 -07001041 // We only support GL_TEXTURE_2D at the moment.
bsalomon091f60c2015-11-10 11:54:56 -08001042 idDesc.fInfo.fTarget = GR_GL_TEXTURE_2D;
junov@chromium.org8b6b1c92013-08-29 16:22:09 +00001043
bsalomon091f60c2015-11-10 11:54:56 -08001044 if (!idDesc.fInfo.fID) {
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001045 return return_null_texture();
1046 }
1047
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00001048 this->setScratchTextureUnit();
bsalomon091f60c2015-11-10 11:54:56 -08001049 GL_CALL(BindTexture(idDesc.fInfo.fTarget, idDesc.fInfo.fID));
bsalomon@google.come269f212011-11-07 13:29:52 +00001050
junov@chromium.org8b6b1c92013-08-29 16:22:09 +00001051 if (renderTarget && this->glCaps().textureUsageSupport()) {
1052 // provides a hint about how this texture will be used
bsalomon091f60c2015-11-10 11:54:56 -08001053 GL_CALL(TexParameteri(idDesc.fInfo.fTarget,
junov@chromium.org8b6b1c92013-08-29 16:22:09 +00001054 GR_GL_TEXTURE_USAGE,
1055 GR_GL_FRAMEBUFFER_ATTACHMENT));
1056 }
1057
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001058 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
1059 // drivers have a bug where an FBO won't be complete if it includes a
1060 // texture that is not mipmap complete (considering the filter in use).
1061 GrGLTexture::TexParams initialTexParams;
1062 // we only set a subset here so invalidate first
1063 initialTexParams.invalidate();
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00001064 initialTexParams.fMinFilter = GR_GL_NEAREST;
1065 initialTexParams.fMagFilter = GR_GL_NEAREST;
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001066 initialTexParams.fWrapS = GR_GL_CLAMP_TO_EDGE;
1067 initialTexParams.fWrapT = GR_GL_CLAMP_TO_EDGE;
bsalomon091f60c2015-11-10 11:54:56 -08001068 GL_CALL(TexParameteri(idDesc.fInfo.fTarget,
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001069 GR_GL_TEXTURE_MAG_FILTER,
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00001070 initialTexParams.fMagFilter));
bsalomon091f60c2015-11-10 11:54:56 -08001071 GL_CALL(TexParameteri(idDesc.fInfo.fTarget,
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001072 GR_GL_TEXTURE_MIN_FILTER,
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00001073 initialTexParams.fMinFilter));
bsalomon091f60c2015-11-10 11:54:56 -08001074 GL_CALL(TexParameteri(idDesc.fInfo.fTarget,
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001075 GR_GL_TEXTURE_WRAP_S,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001076 initialTexParams.fWrapS));
bsalomon091f60c2015-11-10 11:54:56 -08001077 GL_CALL(TexParameteri(idDesc.fInfo.fTarget,
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001078 GR_GL_TEXTURE_WRAP_T,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001079 initialTexParams.fWrapT));
bsalomon091f60c2015-11-10 11:54:56 -08001080 if (!this->uploadTexData(desc, idDesc.fInfo.fTarget, true, 0, 0,
bsalomonb15b4c12014-10-29 12:41:57 -07001081 desc.fWidth, desc.fHeight,
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001082 desc.fConfig, srcData, rowBytes)) {
bsalomon091f60c2015-11-10 11:54:56 -08001083 GL_CALL(DeleteTextures(1, &idDesc.fInfo.fID));
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001084 return return_null_texture();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001085 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001086
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001087 GrGLTexture* tex;
reed@google.comac10a2d2010-12-22 21:39:39 +00001088 if (renderTarget) {
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001089 // unbind the texture from the texture unit before binding it to the frame buffer
bsalomon091f60c2015-11-10 11:54:56 -08001090 GL_CALL(BindTexture(idDesc.fInfo.fTarget, 0));
bsalomon5236cf42015-01-14 10:42:08 -08001091 GrGLRenderTarget::IDDesc rtIDDesc;
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001092
bsalomon091f60c2015-11-10 11:54:56 -08001093 if (!this->createRenderTargetObjects(desc, lifeCycle, idDesc.fInfo, &rtIDDesc)) {
1094 GL_CALL(DeleteTextures(1, &idDesc.fInfo.fID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001095 return return_null_texture();
1096 }
halcanary385fe4d2015-08-26 13:07:48 -07001097 tex = new GrGLTextureRenderTarget(this, desc, idDesc, rtIDDesc);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001098 } else {
halcanary385fe4d2015-08-26 13:07:48 -07001099 tex = new GrGLTexture(this, desc, idDesc);
reed@google.comac10a2d2010-12-22 21:39:39 +00001100 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001101 tex->setCachedTexParams(initialTexParams, this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00001102#ifdef TRACE_TEXTURE_CREATION
tfarina38406c82014-10-31 07:11:12 -07001103 SkDebugf("--- new texture [%d] size=(%d %d) config=%d\n",
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001104 glTexDesc.fTextureID, desc.fWidth, desc.fHeight, desc.fConfig);
reed@google.comac10a2d2010-12-22 21:39:39 +00001105#endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001106 return tex;
1107}
1108
egdanielb0e1be22015-04-22 13:27:39 -07001109GrTexture* GrGLGpu::onCreateCompressedTexture(const GrSurfaceDesc& desc,
1110 GrGpuResource::LifeCycle lifeCycle,
bsalomon5236cf42015-01-14 10:42:08 -08001111 const void* srcData) {
krajcevski9c0e6292014-06-02 07:38:14 -07001112 // Make sure that we're not flipping Y.
egdanielb0e1be22015-04-22 13:27:39 -07001113 if (kBottomLeft_GrSurfaceOrigin == desc.fOrigin) {
krajcevski9c0e6292014-06-02 07:38:14 -07001114 return return_null_texture();
1115 }
1116
bsalomonb15b4c12014-10-29 12:41:57 -07001117 GrGLTexture::IDDesc idDesc;
bsalomon091f60c2015-11-10 11:54:56 -08001118 GL_CALL(GenTextures(1, &idDesc.fInfo.fID));
egdanielb0e1be22015-04-22 13:27:39 -07001119 idDesc.fLifeCycle = lifeCycle;
bsalomon10528f12015-10-14 12:54:52 -07001120 // We only support GL_TEXTURE_2D at the moment.
bsalomon091f60c2015-11-10 11:54:56 -08001121 idDesc.fInfo.fTarget = GR_GL_TEXTURE_2D;
krajcevski9c0e6292014-06-02 07:38:14 -07001122
bsalomon091f60c2015-11-10 11:54:56 -08001123 if (!idDesc.fInfo.fID) {
krajcevski9c0e6292014-06-02 07:38:14 -07001124 return return_null_texture();
1125 }
1126
1127 this->setScratchTextureUnit();
bsalomon091f60c2015-11-10 11:54:56 -08001128 GL_CALL(BindTexture(idDesc.fInfo.fTarget, idDesc.fInfo.fID));
krajcevski9c0e6292014-06-02 07:38:14 -07001129
1130 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
1131 // drivers have a bug where an FBO won't be complete if it includes a
1132 // texture that is not mipmap complete (considering the filter in use).
1133 GrGLTexture::TexParams initialTexParams;
1134 // we only set a subset here so invalidate first
1135 initialTexParams.invalidate();
1136 initialTexParams.fMinFilter = GR_GL_NEAREST;
1137 initialTexParams.fMagFilter = GR_GL_NEAREST;
1138 initialTexParams.fWrapS = GR_GL_CLAMP_TO_EDGE;
1139 initialTexParams.fWrapT = GR_GL_CLAMP_TO_EDGE;
bsalomon091f60c2015-11-10 11:54:56 -08001140 GL_CALL(TexParameteri(idDesc.fInfo.fTarget,
krajcevski9c0e6292014-06-02 07:38:14 -07001141 GR_GL_TEXTURE_MAG_FILTER,
1142 initialTexParams.fMagFilter));
bsalomon091f60c2015-11-10 11:54:56 -08001143 GL_CALL(TexParameteri(idDesc.fInfo.fTarget,
krajcevski9c0e6292014-06-02 07:38:14 -07001144 GR_GL_TEXTURE_MIN_FILTER,
1145 initialTexParams.fMinFilter));
bsalomon091f60c2015-11-10 11:54:56 -08001146 GL_CALL(TexParameteri(idDesc.fInfo.fTarget,
krajcevski9c0e6292014-06-02 07:38:14 -07001147 GR_GL_TEXTURE_WRAP_S,
1148 initialTexParams.fWrapS));
bsalomon091f60c2015-11-10 11:54:56 -08001149 GL_CALL(TexParameteri(idDesc.fInfo.fTarget,
krajcevski9c0e6292014-06-02 07:38:14 -07001150 GR_GL_TEXTURE_WRAP_T,
1151 initialTexParams.fWrapT));
1152
bsalomon091f60c2015-11-10 11:54:56 -08001153 if (!this->uploadCompressedTexData(desc, idDesc.fInfo.fTarget, srcData)) {
1154 GL_CALL(DeleteTextures(1, &idDesc.fInfo.fID));
krajcevski9c0e6292014-06-02 07:38:14 -07001155 return return_null_texture();
1156 }
1157
1158 GrGLTexture* tex;
halcanary385fe4d2015-08-26 13:07:48 -07001159 tex = new GrGLTexture(this, desc, idDesc);
krajcevski9c0e6292014-06-02 07:38:14 -07001160 tex->setCachedTexParams(initialTexParams, this->getResetTimestamp());
1161#ifdef TRACE_TEXTURE_CREATION
tfarina38406c82014-10-31 07:11:12 -07001162 SkDebugf("--- new compressed texture [%d] size=(%d %d) config=%d\n",
krajcevski9c0e6292014-06-02 07:38:14 -07001163 glTexDesc.fTextureID, desc.fWidth, desc.fHeight, desc.fConfig);
1164#endif
1165 return tex;
1166}
1167
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001168namespace {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001169
egdaniel8dc7c3a2015-04-16 11:22:42 -07001170const GrGLuint kUnknownBitCount = GrGLStencilAttachment::kUnknownBitCount;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001171
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001172void inline get_stencil_rb_sizes(const GrGLInterface* gl,
egdaniel8dc7c3a2015-04-16 11:22:42 -07001173 GrGLStencilAttachment::Format* format) {
sugoi@google.com6ba0b0f2013-05-03 13:52:32 +00001174
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001175 // we shouldn't ever know one size and not the other
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001176 SkASSERT((kUnknownBitCount == format->fStencilBits) ==
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001177 (kUnknownBitCount == format->fTotalBits));
1178 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001179 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001180 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1181 (GrGLint*)&format->fStencilBits);
1182 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001183 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001184 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1185 (GrGLint*)&format->fTotalBits);
1186 format->fTotalBits += format->fStencilBits;
1187 } else {
1188 format->fTotalBits = format->fStencilBits;
1189 }
1190 }
1191}
1192}
1193
egdanielff1d5472015-09-10 08:37:20 -07001194int GrGLGpu::getCompatibleStencilIndex(GrPixelConfig config) {
bsalomon100b8f82015-10-28 08:37:44 -07001195 static const int kSize = 16;
egdanielff1d5472015-09-10 08:37:20 -07001196 if (kUnknownStencilIndex == fPixelConfigToStencilIndex[config]) {
1197 // Default to unsupported
1198 fPixelConfigToStencilIndex[config] = kUnsupportedStencilIndex;
1199 // Create color texture
1200 GrGLuint colorID;
1201 GL_CALL(GenTextures(1, &colorID));
1202 this->setScratchTextureUnit();
1203 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, colorID));
1204 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1205 GR_GL_TEXTURE_MAG_FILTER,
1206 GR_GL_NEAREST));
1207 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1208 GR_GL_TEXTURE_MIN_FILTER,
1209 GR_GL_NEAREST));
1210 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1211 GR_GL_TEXTURE_WRAP_S,
1212 GR_GL_CLAMP_TO_EDGE));
1213 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1214 GR_GL_TEXTURE_WRAP_T,
1215 GR_GL_CLAMP_TO_EDGE));
1216
1217 GrGLenum internalFormat = 0x0; // suppress warning
1218 GrGLenum externalFormat = 0x0; // suppress warning
1219 GrGLenum externalType = 0x0; // suppress warning
egdanieleef3c5b2015-09-10 11:28:22 -07001220 bool useSizedFormat = false;
1221 if (kGL_GrGLStandard == this->glStandard() ||
1222 (this->glVersion() >= GR_GL_VER(3, 0) &&
1223 // ES3 only works with sized BGRA8 format if "GL_APPLE_texture_format_BGRA8888" enabled
1224 (kBGRA_8888_GrPixelConfig != config || !this->glCaps().bgraIsInternalFormat()))) {
1225 useSizedFormat = true;
1226 }
1227 if (!this->configToGLFormats(config, useSizedFormat, &internalFormat,
egdanielff1d5472015-09-10 08:37:20 -07001228 &externalFormat, &externalType)) {
1229 GL_CALL(DeleteTextures(1, &colorID));
1230 fPixelConfigToStencilIndex[config] = kUnsupportedStencilIndex;
1231 return kUnsupportedStencilIndex;
1232 }
1233
1234 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1235 GL_ALLOC_CALL(this->glInterface(), TexImage2D(GR_GL_TEXTURE_2D,
1236 0, internalFormat,
bsalomon100b8f82015-10-28 08:37:44 -07001237 kSize,
1238 kSize,
egdanielff1d5472015-09-10 08:37:20 -07001239 0,
1240 externalFormat,
1241 externalType,
1242 NULL));
1243 if (GR_GL_NO_ERROR != GR_GL_GET_ERROR(this->glInterface())) {
1244 GL_CALL(DeleteTextures(1, &colorID));
1245 fPixelConfigToStencilIndex[config] = kUnsupportedStencilIndex;
1246 return kUnsupportedStencilIndex;
1247 }
1248
1249 // unbind the texture from the texture unit before binding it to the frame buffer
1250 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, 0));
1251
1252 // Create Framebuffer
1253 GrGLuint fb;
1254 GL_CALL(GenFramebuffers(1, &fb));
egdanielec00d942015-09-14 12:56:10 -07001255 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, fb));
egdanielff1d5472015-09-10 08:37:20 -07001256 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
1257 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
1258 GR_GL_COLOR_ATTACHMENT0,
1259 GR_GL_TEXTURE_2D,
1260 colorID,
1261 0));
1262
1263 // look over formats till I find a compatible one
1264 int stencilFmtCnt = this->glCaps().stencilFormats().count();
1265 GrGLuint sbRBID = 0;
1266 for (int i = 0; i < stencilFmtCnt; ++i) {
1267 const GrGLCaps::StencilFormat& sFmt = this->glCaps().stencilFormats()[i];
1268
1269 GL_CALL(GenRenderbuffers(1, &sbRBID));
1270 if (!sbRBID) {
1271 break;
1272 }
1273 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbRBID));
1274 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1275 GL_ALLOC_CALL(this->glInterface(), RenderbufferStorage(GR_GL_RENDERBUFFER,
1276 sFmt.fInternalFormat,
bsalomon100b8f82015-10-28 08:37:44 -07001277 kSize, kSize));
egdanielff1d5472015-09-10 08:37:20 -07001278 if (GR_GL_NO_ERROR == GR_GL_GET_ERROR(this->glInterface())) {
1279 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1280 GR_GL_STENCIL_ATTACHMENT,
1281 GR_GL_RENDERBUFFER, sbRBID));
1282 if (sFmt.fPacked) {
1283 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1284 GR_GL_DEPTH_ATTACHMENT,
1285 GR_GL_RENDERBUFFER, sbRBID));
1286 } else {
1287 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1288 GR_GL_DEPTH_ATTACHMENT,
1289 GR_GL_RENDERBUFFER, 0));
1290 }
1291 GrGLenum status;
1292 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1293 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
1294 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1295 GR_GL_STENCIL_ATTACHMENT,
1296 GR_GL_RENDERBUFFER, 0));
1297 if (sFmt.fPacked) {
1298 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1299 GR_GL_DEPTH_ATTACHMENT,
1300 GR_GL_RENDERBUFFER, 0));
1301 }
1302 } else {
1303 fPixelConfigToStencilIndex[config] = i;
1304 break;
1305 }
1306 }
1307 sbRBID = 0;
1308 }
1309 GL_CALL(DeleteTextures(1, &colorID));
1310 GL_CALL(DeleteRenderbuffers(1, &sbRBID));
1311 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, 0));
1312 GL_CALL(DeleteFramebuffers(1, &fb));
1313 }
1314 SkASSERT(kUnknownStencilIndex != fPixelConfigToStencilIndex[config]);
1315 return fPixelConfigToStencilIndex[config];
1316}
1317
egdanielec00d942015-09-14 12:56:10 -07001318GrStencilAttachment* GrGLGpu::createStencilAttachmentForRenderTarget(const GrRenderTarget* rt,
1319 int width,
1320 int height) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001321 // All internally created RTs are also textures. We don't create
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +00001322 // 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 +00001323 SkASSERT(rt->asTexture());
1324 SkASSERT(width >= rt->width());
1325 SkASSERT(height >= rt->height());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001326
vbuzinovdded6962015-06-12 08:59:45 -07001327 int samples = rt->numStencilSamples();
egdaniel8dc7c3a2015-04-16 11:22:42 -07001328 GrGLStencilAttachment::IDDesc sbDesc;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001329
egdanielff1d5472015-09-10 08:37:20 -07001330 int sIdx = this->getCompatibleStencilIndex(rt->config());
1331 if (sIdx == kUnsupportedStencilIndex) {
egdanielec00d942015-09-14 12:56:10 -07001332 return nullptr;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001333 }
egdanielff1d5472015-09-10 08:37:20 -07001334
1335 if (!sbDesc.fRenderbufferID) {
1336 GL_CALL(GenRenderbuffers(1, &sbDesc.fRenderbufferID));
1337 }
1338 if (!sbDesc.fRenderbufferID) {
egdanielec00d942015-09-14 12:56:10 -07001339 return nullptr;
egdanielff1d5472015-09-10 08:37:20 -07001340 }
1341 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbDesc.fRenderbufferID));
1342 const GrGLCaps::StencilFormat& sFmt = this->glCaps().stencilFormats()[sIdx];
1343 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1344 // we do this "if" so that we don't call the multisample
1345 // version on a GL that doesn't have an MSAA extension.
1346 if (samples > 0) {
1347 SkAssertResult(renderbuffer_storage_msaa(*fGLContext,
1348 samples,
1349 sFmt.fInternalFormat,
1350 width, height));
1351 } else {
1352 GL_ALLOC_CALL(this->glInterface(), RenderbufferStorage(GR_GL_RENDERBUFFER,
1353 sFmt.fInternalFormat,
1354 width, height));
1355 SkASSERT(GR_GL_NO_ERROR == check_alloc_error(rt->desc(), this->glInterface()));
1356 }
1357 fStats.incStencilAttachmentCreates();
1358 // After sized formats we attempt an unsized format and take
1359 // whatever sizes GL gives us. In that case we query for the size.
1360 GrGLStencilAttachment::Format format = sFmt;
1361 get_stencil_rb_sizes(this->glInterface(), &format);
egdanielec00d942015-09-14 12:56:10 -07001362 GrGLStencilAttachment* stencil = new GrGLStencilAttachment(this,
1363 sbDesc,
1364 width,
1365 height,
1366 samples,
1367 format);
1368 return stencil;
reed@google.comac10a2d2010-12-22 21:39:39 +00001369}
1370
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001371////////////////////////////////////////////////////////////////////////////////
1372
bsalomon861e1032014-12-16 07:33:49 -08001373GrVertexBuffer* GrGLGpu::onCreateVertexBuffer(size_t size, bool dynamic) {
bsalomon@google.come49ad452013-02-20 19:33:20 +00001374 GrGLVertexBuffer::Desc desc;
1375 desc.fDynamic = dynamic;
1376 desc.fSizeInBytes = size;
bsalomon@google.come49ad452013-02-20 19:33:20 +00001377
bsalomon@google.com96966a52013-02-21 16:34:21 +00001378 if (this->glCaps().useNonVBOVertexAndIndexDynamicData() && desc.fDynamic) {
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001379 desc.fID = 0;
halcanary385fe4d2015-08-26 13:07:48 -07001380 GrGLVertexBuffer* vertexBuffer = new GrGLVertexBuffer(this, desc);
reed@google.comac10a2d2010-12-22 21:39:39 +00001381 return vertexBuffer;
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001382 } else {
1383 GL_CALL(GenBuffers(1, &desc.fID));
1384 if (desc.fID) {
bsalomon@google.com6918d482013-03-07 19:09:11 +00001385 fHWGeometryState.setVertexBufferID(this, desc.fID);
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001386 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1387 // make sure driver can allocate memory for this buffer
1388 GL_ALLOC_CALL(this->glInterface(),
1389 BufferData(GR_GL_ARRAY_BUFFER,
robertphillips@google.come9cd27d2013-10-16 17:48:11 +00001390 (GrGLsizeiptr) desc.fSizeInBytes,
halcanary96fcdcc2015-08-27 07:41:13 -07001391 nullptr, // data ptr
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001392 desc.fDynamic ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW));
1393 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
1394 GL_CALL(DeleteBuffers(1, &desc.fID));
bsalomon@google.com6918d482013-03-07 19:09:11 +00001395 this->notifyVertexBufferDelete(desc.fID);
halcanary96fcdcc2015-08-27 07:41:13 -07001396 return nullptr;
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001397 }
halcanary385fe4d2015-08-26 13:07:48 -07001398 GrGLVertexBuffer* vertexBuffer = new GrGLVertexBuffer(this, desc);
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001399 return vertexBuffer;
1400 }
halcanary96fcdcc2015-08-27 07:41:13 -07001401 return nullptr;
reed@google.comac10a2d2010-12-22 21:39:39 +00001402 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001403}
1404
bsalomon861e1032014-12-16 07:33:49 -08001405GrIndexBuffer* GrGLGpu::onCreateIndexBuffer(size_t size, bool dynamic) {
bsalomon@google.come49ad452013-02-20 19:33:20 +00001406 GrGLIndexBuffer::Desc desc;
1407 desc.fDynamic = dynamic;
1408 desc.fSizeInBytes = size;
bsalomon@google.come49ad452013-02-20 19:33:20 +00001409
bsalomon@google.com96966a52013-02-21 16:34:21 +00001410 if (this->glCaps().useNonVBOVertexAndIndexDynamicData() && desc.fDynamic) {
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001411 desc.fID = 0;
halcanary385fe4d2015-08-26 13:07:48 -07001412 GrIndexBuffer* indexBuffer = new GrGLIndexBuffer(this, desc);
reed@google.comac10a2d2010-12-22 21:39:39 +00001413 return indexBuffer;
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001414 } else {
1415 GL_CALL(GenBuffers(1, &desc.fID));
1416 if (desc.fID) {
bsalomon@google.com6918d482013-03-07 19:09:11 +00001417 fHWGeometryState.setIndexBufferIDOnDefaultVertexArray(this, desc.fID);
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001418 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1419 // make sure driver can allocate memory for this buffer
1420 GL_ALLOC_CALL(this->glInterface(),
1421 BufferData(GR_GL_ELEMENT_ARRAY_BUFFER,
robertphillips@google.come9cd27d2013-10-16 17:48:11 +00001422 (GrGLsizeiptr) desc.fSizeInBytes,
halcanary96fcdcc2015-08-27 07:41:13 -07001423 nullptr, // data ptr
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001424 desc.fDynamic ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW));
1425 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
1426 GL_CALL(DeleteBuffers(1, &desc.fID));
bsalomon@google.com6918d482013-03-07 19:09:11 +00001427 this->notifyIndexBufferDelete(desc.fID);
halcanary96fcdcc2015-08-27 07:41:13 -07001428 return nullptr;
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001429 }
halcanary385fe4d2015-08-26 13:07:48 -07001430 GrIndexBuffer* indexBuffer = new GrGLIndexBuffer(this, desc);
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001431 return indexBuffer;
1432 }
halcanary96fcdcc2015-08-27 07:41:13 -07001433 return nullptr;
reed@google.comac10a2d2010-12-22 21:39:39 +00001434 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001435}
1436
bsalomon3e791242014-12-17 13:43:13 -08001437void GrGLGpu::flushScissor(const GrScissorState& scissorState,
joshualitt77b13072014-10-27 14:51:01 -07001438 const GrGLIRect& rtViewport,
1439 GrSurfaceOrigin rtOrigin) {
robertphillipse85a32d2015-02-10 08:16:55 -08001440 if (scissorState.enabled()) {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001441 GrGLIRect scissor;
bsalomonb0bd4f62014-09-03 07:19:50 -07001442 scissor.setRelativeTo(rtViewport,
robertphillipse85a32d2015-02-10 08:16:55 -08001443 scissorState.rect().fLeft,
1444 scissorState.rect().fTop,
1445 scissorState.rect().width(),
1446 scissorState.rect().height(),
bsalomonb0bd4f62014-09-03 07:19:50 -07001447 rtOrigin);
bsalomon@google.coma3201942012-06-21 19:58:20 +00001448 // if the scissor fully contains the viewport then we fall through and
1449 // disable the scissor test.
bsalomonb0bd4f62014-09-03 07:19:50 -07001450 if (!scissor.contains(rtViewport)) {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001451 if (fHWScissorSettings.fRect != scissor) {
1452 scissor.pushToGLScissor(this->glInterface());
1453 fHWScissorSettings.fRect = scissor;
1454 }
1455 if (kYes_TriState != fHWScissorSettings.fEnabled) {
1456 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1457 fHWScissorSettings.fEnabled = kYes_TriState;
1458 }
1459 return;
1460 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001461 }
joshualitt77b13072014-10-27 14:51:01 -07001462
1463 // See fall through note above
1464 this->disableScissor();
1465}
1466
cdaltond0a840d2015-03-16 17:19:58 -07001467bool GrGLGpu::flushGLState(const DrawArgs& args) {
egdaniel080e6732014-12-22 07:35:52 -08001468 GrXferProcessor::BlendInfo blendInfo;
egdaniel8dd688b2015-01-22 10:16:09 -08001469 const GrPipeline& pipeline = *args.fPipeline;
1470 args.fPipeline->getXferProcessor()->getBlendInfo(&blendInfo);
egdaniel080e6732014-12-22 07:35:52 -08001471
egdaniel080e6732014-12-22 07:35:52 -08001472 this->flushColorWrite(blendInfo.fWriteColor);
egdaniel8dd688b2015-01-22 10:16:09 -08001473 this->flushDrawFace(pipeline.getDrawFace());
bsalomonbc3d0de2014-12-15 13:45:03 -08001474
bsalomon6df86402015-06-01 10:41:49 -07001475 SkAutoTUnref<GrGLProgram> program(fProgramCache->refProgram(args));
1476 if (!program) {
bsalomon682c2692015-05-22 14:01:46 -07001477 GrCapsDebugf(this->caps(), "Failed to create program!\n");
bsalomon1f78c0a2014-12-17 09:43:13 -08001478 return false;
bsalomonbc3d0de2014-12-15 13:45:03 -08001479 }
1480
bsalomon6df86402015-06-01 10:41:49 -07001481 GrGLuint programID = program->programID();
bsalomon1f78c0a2014-12-17 09:43:13 -08001482 if (fHWProgramID != programID) {
1483 GL_CALL(UseProgram(programID));
1484 fHWProgramID = programID;
1485 }
1486
egdanield803f272015-03-18 13:01:52 -07001487 if (blendInfo.fWriteColor) {
1488 this->flushBlend(blendInfo);
1489 }
bsalomon1f78c0a2014-12-17 09:43:13 -08001490
cdalton42717652015-06-18 11:54:30 -07001491 SkSTArray<8, const GrTextureAccess*> textureAccesses;
joshualitt465283c2015-09-11 08:19:35 -07001492 program->setData(*args.fPrimitiveProcessor, pipeline, &textureAccesses);
cdalton42717652015-06-18 11:54:30 -07001493
1494 int numTextureAccesses = textureAccesses.count();
1495 for (int i = 0; i < numTextureAccesses; i++) {
1496 this->bindTexture(i, textureAccesses[i]->getParams(),
1497 static_cast<GrGLTexture*>(textureAccesses[i]->getTexture()));
1498 }
bsalomon1f78c0a2014-12-17 09:43:13 -08001499
egdaniel8dd688b2015-01-22 10:16:09 -08001500 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(pipeline.getRenderTarget());
1501 this->flushStencil(pipeline.getStencil());
1502 this->flushScissor(pipeline.getScissorState(), glRT->getViewport(), glRT->origin());
cdalton7e806f32015-11-11 15:16:07 -08001503 this->flushHWAAState(glRT, pipeline.isHWAntialiasState());
bsalomonbc3d0de2014-12-15 13:45:03 -08001504
1505 // This must come after textures are flushed because a texture may need
egdanield803f272015-03-18 13:01:52 -07001506 // to be msaa-resolved (which will modify bound FBO state).
cdaltond4727922015-11-10 12:49:06 -08001507 this->flushRenderTarget(glRT, nullptr);
bsalomonbc3d0de2014-12-15 13:45:03 -08001508
1509 return true;
1510}
1511
joshualitt873ad0e2015-01-20 09:08:51 -08001512void GrGLGpu::setupGeometry(const GrPrimitiveProcessor& primProc,
bsalomone64eb572015-05-07 11:35:55 -07001513 const GrNonInstancedVertices& vertices,
bsalomonbc3d0de2014-12-15 13:45:03 -08001514 size_t* indexOffsetInBytes) {
1515 GrGLVertexBuffer* vbuf;
bsalomone64eb572015-05-07 11:35:55 -07001516 vbuf = (GrGLVertexBuffer*) vertices.vertexBuffer();
bsalomonbc3d0de2014-12-15 13:45:03 -08001517
1518 SkASSERT(vbuf);
1519 SkASSERT(!vbuf->isMapped());
1520
halcanary96fcdcc2015-08-27 07:41:13 -07001521 GrGLIndexBuffer* ibuf = nullptr;
bsalomone64eb572015-05-07 11:35:55 -07001522 if (vertices.isIndexed()) {
bsalomonbc3d0de2014-12-15 13:45:03 -08001523 SkASSERT(indexOffsetInBytes);
1524
1525 *indexOffsetInBytes = 0;
bsalomone64eb572015-05-07 11:35:55 -07001526 ibuf = (GrGLIndexBuffer*)vertices.indexBuffer();
bsalomonbc3d0de2014-12-15 13:45:03 -08001527
1528 SkASSERT(ibuf);
1529 SkASSERT(!ibuf->isMapped());
1530 *indexOffsetInBytes += ibuf->baseOffset();
1531 }
1532 GrGLAttribArrayState* attribState =
1533 fHWGeometryState.bindArrayAndBuffersToDraw(this, vbuf, ibuf);
1534
joshualitt873ad0e2015-01-20 09:08:51 -08001535 int vaCount = primProc.numAttribs();
joshualitt71c92602015-01-14 08:12:47 -08001536 if (vaCount > 0) {
bsalomonbc3d0de2014-12-15 13:45:03 -08001537
joshualitt873ad0e2015-01-20 09:08:51 -08001538 GrGLsizei stride = static_cast<GrGLsizei>(primProc.getVertexStride());
bsalomonbc3d0de2014-12-15 13:45:03 -08001539
bsalomone64eb572015-05-07 11:35:55 -07001540 size_t vertexOffsetInBytes = stride * vertices.startVertex();
bsalomonbc3d0de2014-12-15 13:45:03 -08001541
1542 vertexOffsetInBytes += vbuf->baseOffset();
1543
bsalomonbc3d0de2014-12-15 13:45:03 -08001544 uint32_t usedAttribArraysMask = 0;
1545 size_t offset = 0;
1546
1547 for (int attribIndex = 0; attribIndex < vaCount; attribIndex++) {
joshualitt873ad0e2015-01-20 09:08:51 -08001548 const GrGeometryProcessor::Attribute& attrib = primProc.getAttrib(attribIndex);
bsalomonbc3d0de2014-12-15 13:45:03 -08001549 usedAttribArraysMask |= (1 << attribIndex);
joshualitt71c92602015-01-14 08:12:47 -08001550 GrVertexAttribType attribType = attrib.fType;
bsalomonbc3d0de2014-12-15 13:45:03 -08001551 attribState->set(this,
1552 attribIndex,
bsalomon6df86402015-06-01 10:41:49 -07001553 vbuf->bufferID(),
bsalomonbc3d0de2014-12-15 13:45:03 -08001554 GrGLAttribTypeToLayout(attribType).fCount,
1555 GrGLAttribTypeToLayout(attribType).fType,
1556 GrGLAttribTypeToLayout(attribType).fNormalized,
1557 stride,
1558 reinterpret_cast<GrGLvoid*>(vertexOffsetInBytes + offset));
joshualitt71c92602015-01-14 08:12:47 -08001559 offset += attrib.fOffset;
bsalomonbc3d0de2014-12-15 13:45:03 -08001560 }
1561 attribState->disableUnusedArrays(this, usedAttribArraysMask);
1562 }
1563}
1564
joshualitt873ad0e2015-01-20 09:08:51 -08001565void GrGLGpu::buildProgramDesc(GrProgramDesc* desc,
1566 const GrPrimitiveProcessor& primProc,
joshualitt465283c2015-09-11 08:19:35 -07001567 const GrPipeline& pipeline) const {
1568 if (!GrGLProgramDescBuilder::Build(desc, primProc, pipeline, this)) {
bsalomonbc3d0de2014-12-15 13:45:03 -08001569 SkDEBUGFAIL("Failed to generate GL program descriptor");
1570 }
1571}
1572
joshualitt93316b92015-10-23 09:08:08 -07001573void GrGLGpu::bindBuffer(GrGLuint id, GrGLenum type) {
1574 this->handleDirtyContext();
1575 if (GR_GL_ARRAY_BUFFER == type) {
1576 this->bindVertexBuffer(id);
1577 } else {
1578 SkASSERT(GR_GL_ELEMENT_ARRAY_BUFFER == type);
1579 this->bindIndexBufferAndDefaultVertexArray(id);
1580 }
1581}
1582
1583void GrGLGpu::releaseBuffer(GrGLuint id, GrGLenum type) {
1584 this->handleDirtyContext();
1585 GL_CALL(DeleteBuffers(1, &id));
1586 if (GR_GL_ARRAY_BUFFER == type) {
1587 this->notifyVertexBufferDelete(id);
1588 } else {
1589 SkASSERT(GR_GL_ELEMENT_ARRAY_BUFFER == type);
1590 this->notifyIndexBufferDelete(id);
1591 }
1592}
1593
1594// GL_STREAM_DRAW triggers an optimization in Chromium's GPU process where a client's vertex buffer
1595// objects are implemented as client-side-arrays on tile-deferred architectures.
1596#define DYNAMIC_USAGE_PARAM GR_GL_STREAM_DRAW
1597
1598void* GrGLGpu::mapBuffer(GrGLuint id, GrGLenum type, bool dynamic, size_t currentSize,
1599 size_t requestedSize) {
1600 void* mapPtr = nullptr;
1601 // Handling dirty context is done in the bindBuffer call
1602 switch (this->glCaps().mapBufferType()) {
1603 case GrGLCaps::kNone_MapBufferType:
1604 break;
1605 case GrGLCaps::kMapBuffer_MapBufferType:
1606 this->bindBuffer(id, type);
1607 // Let driver know it can discard the old data
joshualitt6df232d2015-10-23 13:54:12 -07001608 if (GR_GL_USE_BUFFER_DATA_NULL_HINT || currentSize != requestedSize) {
joshualitt93316b92015-10-23 09:08:08 -07001609 GL_CALL(BufferData(type, requestedSize, nullptr,
1610 dynamic ? DYNAMIC_USAGE_PARAM : GR_GL_STATIC_DRAW));
1611 }
1612 GL_CALL_RET(mapPtr, MapBuffer(type, GR_GL_WRITE_ONLY));
1613 break;
1614 case GrGLCaps::kMapBufferRange_MapBufferType: {
1615 this->bindBuffer(id, type);
1616 // Make sure the GL buffer size agrees with fDesc before mapping.
1617 if (currentSize != requestedSize) {
1618 GL_CALL(BufferData(type, requestedSize, nullptr,
1619 dynamic ? DYNAMIC_USAGE_PARAM : GR_GL_STATIC_DRAW));
1620 }
1621 static const GrGLbitfield kAccess = GR_GL_MAP_INVALIDATE_BUFFER_BIT |
1622 GR_GL_MAP_WRITE_BIT;
1623 GL_CALL_RET(mapPtr, MapBufferRange(type, 0, requestedSize, kAccess));
1624 break;
1625 }
1626 case GrGLCaps::kChromium_MapBufferType:
1627 this->bindBuffer(id, type);
1628 // Make sure the GL buffer size agrees with fDesc before mapping.
1629 if (currentSize != requestedSize) {
1630 GL_CALL(BufferData(type, requestedSize, nullptr,
1631 dynamic ? DYNAMIC_USAGE_PARAM : GR_GL_STATIC_DRAW));
1632 }
1633 GL_CALL_RET(mapPtr, MapBufferSubData(type, 0, requestedSize, GR_GL_WRITE_ONLY));
1634 break;
1635 }
1636 return mapPtr;
1637}
1638
1639void GrGLGpu::bufferData(GrGLuint id, GrGLenum type, bool dynamic, size_t currentSize,
1640 const void* src, size_t srcSizeInBytes) {
1641 SkASSERT(srcSizeInBytes <= currentSize);
1642 // bindbuffer handles dirty context
1643 this->bindBuffer(id, type);
1644 GrGLenum usage = dynamic ? DYNAMIC_USAGE_PARAM : GR_GL_STATIC_DRAW;
1645
joshualitt6df232d2015-10-23 13:54:12 -07001646#if GR_GL_USE_BUFFER_DATA_NULL_HINT
1647 if (currentSize == srcSizeInBytes) {
1648 GL_CALL(BufferData(type, (GrGLsizeiptr) srcSizeInBytes, src, usage));
1649 } else {
1650 // Before we call glBufferSubData we give the driver a hint using
1651 // glBufferData with nullptr. This makes the old buffer contents
1652 // inaccessible to future draws. The GPU may still be processing
1653 // draws that reference the old contents. With this hint it can
1654 // assign a different allocation for the new contents to avoid
1655 // flushing the gpu past draws consuming the old contents.
1656 // TODO I think we actually want to try calling bufferData here
1657 GL_CALL(BufferData(type, currentSize, nullptr, usage));
1658 GL_CALL(BufferSubData(type, 0, (GrGLsizeiptr) srcSizeInBytes, src));
1659 }
1660#else
joshualitt93316b92015-10-23 09:08:08 -07001661 // Note that we're cheating on the size here. Currently no methods
1662 // allow a partial update that preserves contents of non-updated
1663 // portions of the buffer (map() does a glBufferData(..size, nullptr..))
1664 GL_CALL(BufferData(type, srcSizeInBytes, src, usage));
joshualitt6df232d2015-10-23 13:54:12 -07001665#endif
joshualitt93316b92015-10-23 09:08:08 -07001666}
1667
1668void GrGLGpu::unmapBuffer(GrGLuint id, GrGLenum type, void* mapPtr) {
1669 // bind buffer handles the dirty context
1670 switch (this->glCaps().mapBufferType()) {
1671 case GrGLCaps::kNone_MapBufferType:
1672 SkDEBUGFAIL("Shouldn't get here.");
1673 return;
1674 case GrGLCaps::kMapBuffer_MapBufferType: // fall through
1675 case GrGLCaps::kMapBufferRange_MapBufferType:
1676 this->bindBuffer(id, type);
1677 GL_CALL(UnmapBuffer(type));
1678 break;
1679 case GrGLCaps::kChromium_MapBufferType:
1680 this->bindBuffer(id, type);
1681 GL_CALL(UnmapBufferSubData(mapPtr));
1682 break;
1683 }
1684}
1685
bsalomon861e1032014-12-16 07:33:49 -08001686void GrGLGpu::disableScissor() {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001687 if (kNo_TriState != fHWScissorSettings.fEnabled) {
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001688 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.coma3201942012-06-21 19:58:20 +00001689 fHWScissorSettings.fEnabled = kNo_TriState;
1690 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001691 }
1692}
1693
egdaniel51c8d402015-08-06 10:54:13 -07001694void GrGLGpu::onClear(GrRenderTarget* target, const SkIRect& rect, GrColor color) {
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001695 // parent class should never let us get here with no RT
bsalomon49f085d2014-09-05 13:34:00 -07001696 SkASSERT(target);
bsalomonb0bd4f62014-09-03 07:19:50 -07001697 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001698
egdaniel51c8d402015-08-06 10:54:13 -07001699 this->flushRenderTarget(glRT, &rect);
bsalomon3e791242014-12-17 13:43:13 -08001700 GrScissorState scissorState;
egdaniel51c8d402015-08-06 10:54:13 -07001701 scissorState.set(rect);
joshualitt77b13072014-10-27 14:51:01 -07001702 this->flushScissor(scissorState, glRT->getViewport(), glRT->origin());
bsalomon@google.com74b98712011-11-11 19:46:16 +00001703
1704 GrGLfloat r, g, b, a;
1705 static const GrGLfloat scale255 = 1.f / 255.f;
1706 a = GrColorUnpackA(color) * scale255;
1707 GrGLfloat scaleRGB = scale255;
bsalomon@google.com74b98712011-11-11 19:46:16 +00001708 r = GrColorUnpackR(color) * scaleRGB;
1709 g = GrColorUnpackG(color) * scaleRGB;
1710 b = GrColorUnpackB(color) * scaleRGB;
1711
1712 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00001713 fHWWriteToColor = kYes_TriState;
bsalomon@google.com74b98712011-11-11 19:46:16 +00001714 GL_CALL(ClearColor(r, g, b, a));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001715 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001716}
1717
bsalomon861e1032014-12-16 07:33:49 -08001718void GrGLGpu::discard(GrRenderTarget* renderTarget) {
bsalomon89c62982014-11-03 12:08:42 -08001719 SkASSERT(renderTarget);
commit-bot@chromium.org52ffbf62014-04-02 16:19:33 +00001720 if (!this->caps()->discardRenderTargetSupport()) {
1721 return;
1722 }
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +00001723
1724 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(renderTarget);
egdanield803f272015-03-18 13:01:52 -07001725 if (renderTarget->getUniqueID() != fHWBoundRenderTargetUniqueID) {
1726 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
1727 fStats.incRenderTargetBinds();
1728 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, glRT->renderFBOID()));
1729 }
commit-bot@chromium.org52ffbf62014-04-02 16:19:33 +00001730 switch (this->glCaps().invalidateFBType()) {
joshualitt58162332014-08-01 06:44:53 -07001731 case GrGLCaps::kNone_InvalidateFBType:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +00001732 SkFAIL("Should never get here.");
commit-bot@chromium.org52ffbf62014-04-02 16:19:33 +00001733 break;
1734 case GrGLCaps::kInvalidate_InvalidateFBType:
egdanield803f272015-03-18 13:01:52 -07001735 if (0 == glRT->renderFBOID()) {
commit-bot@chromium.org52ffbf62014-04-02 16:19:33 +00001736 // When rendering to the default framebuffer the legal values for attachments
1737 // are GL_COLOR, GL_DEPTH, GL_STENCIL, ... rather than the various FBO attachment
1738 // types.
1739 static const GrGLenum attachments[] = { GR_GL_COLOR };
egdanield803f272015-03-18 13:01:52 -07001740 GL_CALL(InvalidateFramebuffer(GR_GL_FRAMEBUFFER, SK_ARRAY_COUNT(attachments),
commit-bot@chromium.org52ffbf62014-04-02 16:19:33 +00001741 attachments));
1742 } else {
1743 static const GrGLenum attachments[] = { GR_GL_COLOR_ATTACHMENT0 };
egdanield803f272015-03-18 13:01:52 -07001744 GL_CALL(InvalidateFramebuffer(GR_GL_FRAMEBUFFER, SK_ARRAY_COUNT(attachments),
commit-bot@chromium.org52ffbf62014-04-02 16:19:33 +00001745 attachments));
1746 }
1747 break;
1748 case GrGLCaps::kDiscard_InvalidateFBType: {
egdanield803f272015-03-18 13:01:52 -07001749 if (0 == glRT->renderFBOID()) {
commit-bot@chromium.org4453e8b2014-04-16 14:33:27 +00001750 // When rendering to the default framebuffer the legal values for attachments
1751 // are GL_COLOR, GL_DEPTH, GL_STENCIL, ... rather than the various FBO attachment
1752 // types. See glDiscardFramebuffer() spec.
1753 static const GrGLenum attachments[] = { GR_GL_COLOR };
egdanield803f272015-03-18 13:01:52 -07001754 GL_CALL(DiscardFramebuffer(GR_GL_FRAMEBUFFER, SK_ARRAY_COUNT(attachments),
commit-bot@chromium.org4453e8b2014-04-16 14:33:27 +00001755 attachments));
1756 } else {
1757 static const GrGLenum attachments[] = { GR_GL_COLOR_ATTACHMENT0 };
egdanield803f272015-03-18 13:01:52 -07001758 GL_CALL(DiscardFramebuffer(GR_GL_FRAMEBUFFER, SK_ARRAY_COUNT(attachments),
commit-bot@chromium.org4453e8b2014-04-16 14:33:27 +00001759 attachments));
1760 }
commit-bot@chromium.org52ffbf62014-04-02 16:19:33 +00001761 break;
1762 }
1763 }
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +00001764 renderTarget->flagAsResolved();
1765}
1766
bsalomon861e1032014-12-16 07:33:49 -08001767void GrGLGpu::clearStencil(GrRenderTarget* target) {
halcanary96fcdcc2015-08-27 07:41:13 -07001768 if (nullptr == target) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001769 return;
1770 }
bsalomonb0bd4f62014-09-03 07:19:50 -07001771 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
egdanield803f272015-03-18 13:01:52 -07001772 this->flushRenderTarget(glRT, &SkIRect::EmptyIRect());
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001773
joshualitt77b13072014-10-27 14:51:01 -07001774 this->disableScissor();
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001775
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001776 GL_CALL(StencilMask(0xffffffff));
1777 GL_CALL(ClearStencil(0));
1778 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001779 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001780}
1781
bsalomon861e1032014-12-16 07:33:49 -08001782void GrGLGpu::onClearStencilClip(GrRenderTarget* target, const SkIRect& rect, bool insideClip) {
bsalomon49f085d2014-09-05 13:34:00 -07001783 SkASSERT(target);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001784
egdaniel8dc7c3a2015-04-16 11:22:42 -07001785 GrStencilAttachment* sb = target->renderTargetPriv().getStencilAttachment();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001786 // this should only be called internally when we know we have a
1787 // stencil buffer.
bsalomon6bc1b5f2015-02-23 09:06:38 -08001788 SkASSERT(sb);
1789 GrGLint stencilBitCount = sb->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001790#if 0
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001791 SkASSERT(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00001792 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001793#else
1794 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001795 // ANGLE a partial stencil mask will cause clears to be
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001796 // turned into draws. Our contract on GrDrawTarget says that
1797 // changing the clip between stencil passes may or may not
1798 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00001799 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001800#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001801 GrGLint value;
1802 if (insideClip) {
1803 value = (1 << (stencilBitCount - 1));
1804 } else {
1805 value = 0;
1806 }
bsalomonb0bd4f62014-09-03 07:19:50 -07001807 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
egdanield803f272015-03-18 13:01:52 -07001808 this->flushRenderTarget(glRT, &SkIRect::EmptyIRect());
bsalomon@google.coma3201942012-06-21 19:58:20 +00001809
bsalomon3e791242014-12-17 13:43:13 -08001810 GrScissorState scissorState;
robertphillipse85a32d2015-02-10 08:16:55 -08001811 scissorState.set(rect);
joshualitt77b13072014-10-27 14:51:01 -07001812 this->flushScissor(scissorState, glRT->getViewport(), glRT->origin());
bsalomon@google.coma3201942012-06-21 19:58:20 +00001813
caryclark@google.comcf6285b2012-06-06 12:09:01 +00001814 GL_CALL(StencilMask((uint32_t) clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001815 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001816 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001817 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001818}
1819
bsalomon39826022015-07-23 08:07:21 -07001820static bool read_pixels_pays_for_y_flip(GrRenderTarget* renderTarget, const GrGLCaps& caps,
1821 int width, int height, GrPixelConfig config,
1822 size_t rowBytes) {
1823 // If this render target is already TopLeft, we don't need to flip.
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001824 if (kTopLeft_GrSurfaceOrigin == renderTarget->origin()) {
1825 return false;
1826 }
1827
bsalomon494aa592015-07-23 11:45:02 -07001828 // If the read is really small or smaller than the min texture size, don't force a draw.
bsalomon100b8f82015-10-28 08:37:44 -07001829 static const int kMinSize = 32;
1830 if (width < kMinSize || height < kMinSize) {
bsalomon494aa592015-07-23 11:45:02 -07001831 return false;
1832 }
1833
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001834 // if GL can do the flip then we'll never pay for it.
bsalomon39826022015-07-23 08:07:21 -07001835 if (caps.packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001836 return false;
1837 }
1838
1839 // If we have to do memcpy to handle non-trim rowBytes then we
bsalomon@google.com7107fa72011-11-10 14:54:14 +00001840 // get the flip for free. Otherwise it costs.
bsalomon39826022015-07-23 08:07:21 -07001841 // Note that we're assuming that 0 rowBytes has already been handled and that the width has been
1842 // clipped.
1843 return caps.packRowLengthSupport() || GrBytesPerPixel(config) * width == rowBytes;
1844}
1845
bsalomonf0674512015-07-28 13:26:15 -07001846bool GrGLGpu::onGetReadPixelsInfo(GrSurface* srcSurface, int width, int height, size_t rowBytes,
1847 GrPixelConfig readConfig, DrawPreference* drawPreference,
1848 ReadPixelTempDrawInfo* tempDrawInfo) {
bsalomon6cb3cbe2015-07-30 07:34:27 -07001849 // This subclass can only read pixels from a render target. We could use glTexSubImage2D on
1850 // GL versions that support it but we don't today.
1851 if (!srcSurface->asRenderTarget()) {
1852 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
1853 }
1854
bsalomon16921ec2015-07-30 15:34:56 -07001855 if (GrPixelConfigIsSRGB(srcSurface->config()) != GrPixelConfigIsSRGB(readConfig)) {
1856 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
1857 }
1858
bsalomon39826022015-07-23 08:07:21 -07001859 tempDrawInfo->fSwapRAndB = false;
1860
1861 // These settings we will always want if a temp draw is performed. The config is set below
1862 // depending on whether we want to do a R/B swap or not.
1863 tempDrawInfo->fTempSurfaceDesc.fFlags = kRenderTarget_GrSurfaceFlag;
1864 tempDrawInfo->fTempSurfaceDesc.fWidth = width;
1865 tempDrawInfo->fTempSurfaceDesc.fHeight = height;
1866 tempDrawInfo->fTempSurfaceDesc.fSampleCnt = 0;
1867 tempDrawInfo->fTempSurfaceDesc.fOrigin = kTopLeft_GrSurfaceOrigin; // no CPU y-flip for TL.
bsalomon100b8f82015-10-28 08:37:44 -07001868 tempDrawInfo->fUseExactScratch = this->glCaps().partialFBOReadIsSlow();
bsalomon39826022015-07-23 08:07:21 -07001869
1870 // Start off assuming that any temp draw should be to the readConfig, then check if that will
1871 // be inefficient.
1872 GrPixelConfig srcConfig = srcSurface->config();
1873 tempDrawInfo->fTempSurfaceDesc.fConfig = readConfig;
1874
bsalomon88c7b982015-07-31 11:20:16 -07001875 if (this->glCaps().rgba8888PixelsOpsAreSlow() && kRGBA_8888_GrPixelConfig == readConfig) {
bsalomon39826022015-07-23 08:07:21 -07001876 tempDrawInfo->fTempSurfaceDesc.fConfig = kBGRA_8888_GrPixelConfig;
bsalomonb411b3b2015-07-31 09:34:24 -07001877 tempDrawInfo->fSwapRAndB = true;
1878 ElevateDrawPreference(drawPreference, kGpuPrefersDraw_DrawPreference);
bsalomon39826022015-07-23 08:07:21 -07001879 } else if (kMesa_GrGLDriver == this->glContext().driver() &&
1880 GrBytesPerPixel(readConfig) == 4 &&
1881 GrPixelConfigSwapRAndB(readConfig) == srcConfig) {
1882 // Mesa 3D takes a slow path on when reading back BGRA from an RGBA surface and vice-versa.
1883 // Better to do a draw with a R/B swap and then read as the original config.
1884 tempDrawInfo->fTempSurfaceDesc.fConfig = srcConfig;
1885 tempDrawInfo->fSwapRAndB = true;
bsalomonf0674512015-07-28 13:26:15 -07001886 ElevateDrawPreference(drawPreference, kGpuPrefersDraw_DrawPreference);
bsalomon39826022015-07-23 08:07:21 -07001887 } else if (readConfig == kBGRA_8888_GrPixelConfig &&
1888 !this->glCaps().readPixelsSupported(this->glInterface(), GR_GL_BGRA,
1889 GR_GL_UNSIGNED_BYTE, srcConfig)) {
1890 tempDrawInfo->fTempSurfaceDesc.fConfig = kRGBA_8888_GrPixelConfig;
1891 tempDrawInfo->fSwapRAndB = true;
bsalomonf0674512015-07-28 13:26:15 -07001892 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
bsalomon39826022015-07-23 08:07:21 -07001893 }
1894
1895 GrRenderTarget* srcAsRT = srcSurface->asRenderTarget();
1896 if (!srcAsRT) {
bsalomonf0674512015-07-28 13:26:15 -07001897 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
bsalomon39826022015-07-23 08:07:21 -07001898 } else if (read_pixels_pays_for_y_flip(srcAsRT, this->glCaps(), width, height, readConfig,
1899 rowBytes)) {
bsalomonf0674512015-07-28 13:26:15 -07001900 ElevateDrawPreference(drawPreference, kGpuPrefersDraw_DrawPreference);
bsalomon39826022015-07-23 08:07:21 -07001901 }
1902
bsalomon39826022015-07-23 08:07:21 -07001903 return true;
bsalomon@google.comc4364992011-11-07 15:54:49 +00001904}
1905
bsalomon6cb3cbe2015-07-30 07:34:27 -07001906bool GrGLGpu::onReadPixels(GrSurface* surface,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001907 int left, int top,
1908 int width, int height,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001909 GrPixelConfig config,
1910 void* buffer,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001911 size_t rowBytes) {
bsalomon6cb3cbe2015-07-30 07:34:27 -07001912 SkASSERT(surface);
bsalomon39826022015-07-23 08:07:21 -07001913
bsalomon6cb3cbe2015-07-30 07:34:27 -07001914 GrGLRenderTarget* tgt = static_cast<GrGLRenderTarget*>(surface->asRenderTarget());
1915 if (!tgt) {
1916 return false;
1917 }
1918
bsalomon16921ec2015-07-30 15:34:56 -07001919 // OpenGL doesn't do sRGB <-> linear conversions when reading and writing pixels.
1920 if (GrPixelConfigIsSRGB(surface->config()) != GrPixelConfigIsSRGB(config)) {
1921 return false;
1922 }
1923
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +00001924 GrGLenum format = 0;
1925 GrGLenum type = 0;
bsalomon6cb3cbe2015-07-30 07:34:27 -07001926 bool flipY = kBottomLeft_GrSurfaceOrigin == surface->origin();
halcanary96fcdcc2015-08-27 07:41:13 -07001927 if (!this->configToGLFormats(config, false, nullptr, &format, &type)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001928 return false;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001929 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001930
bsalomon16921ec2015-07-30 15:34:56 -07001931 // glReadPixels does not allow GL_SRGB_ALPHA. Instead use GL_RGBA. This will not trigger a
1932 // conversion when the src is srgb.
1933 if (GR_GL_SRGB_ALPHA == format) {
1934 format = GR_GL_RGBA;
1935 }
1936
bsalomon@google.comc6980972011-11-02 19:57:21 +00001937 // resolve the render target if necessary
egdanield803f272015-03-18 13:01:52 -07001938 switch (tgt->getResolveType()) {
1939 case GrGLRenderTarget::kCantResolve_ResolveType:
1940 return false;
1941 case GrGLRenderTarget::kAutoResolves_ResolveType:
bsalomon6cb3cbe2015-07-30 07:34:27 -07001942 this->flushRenderTarget(tgt, &SkIRect::EmptyIRect());
egdanield803f272015-03-18 13:01:52 -07001943 break;
1944 case GrGLRenderTarget::kCanResolve_ResolveType:
1945 this->onResolveRenderTarget(tgt);
1946 // we don't track the state of the READ FBO ID.
1947 fStats.incRenderTargetBinds();
1948 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1949 tgt->textureFBOID()));
1950 break;
1951 default:
1952 SkFAIL("Unknown resolve type");
reed@google.comac10a2d2010-12-22 21:39:39 +00001953 }
1954
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001955 const GrGLIRect& glvp = tgt->getViewport();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001956
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001957 // the read rect is viewport-relative
1958 GrGLIRect readRect;
bsalomon6cb3cbe2015-07-30 07:34:27 -07001959 readRect.setRelativeTo(glvp, left, top, width, height, tgt->origin());
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001960
egdaniel6d901da2015-07-30 12:02:15 -07001961 size_t tightRowBytes = GrBytesPerPixel(config) * width;
1962
bsalomon@google.comc6980972011-11-02 19:57:21 +00001963 size_t readDstRowBytes = tightRowBytes;
1964 void* readDst = buffer;
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001965
bsalomon@google.comc6980972011-11-02 19:57:21 +00001966 // determine if GL can read using the passed rowBytes or if we need
1967 // a scratch buffer.
joshualitt29f86792015-05-29 08:06:48 -07001968 SkAutoSMalloc<32 * sizeof(GrColor)> scratch;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001969 if (rowBytes != tightRowBytes) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001970 if (this->glCaps().packRowLengthSupport()) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001971 SkASSERT(!(rowBytes % sizeof(GrColor)));
skia.committer@gmail.com4677acc2013-10-17 07:02:33 +00001972 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH,
robertphillips@google.come9cd27d2013-10-16 17:48:11 +00001973 static_cast<GrGLint>(rowBytes / sizeof(GrColor))));
bsalomon@google.comc6980972011-11-02 19:57:21 +00001974 readDstRowBytes = rowBytes;
1975 } else {
1976 scratch.reset(tightRowBytes * height);
1977 readDst = scratch.get();
1978 }
1979 }
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001980 if (flipY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001981 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 1));
1982 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001983 GL_CALL(ReadPixels(readRect.fLeft, readRect.fBottom,
1984 readRect.fWidth, readRect.fHeight,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001985 format, type, readDst));
1986 if (readDstRowBytes != tightRowBytes) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001987 SkASSERT(this->glCaps().packRowLengthSupport());
bsalomon@google.comc6980972011-11-02 19:57:21 +00001988 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
1989 }
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001990 if (flipY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001991 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 0));
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001992 flipY = false;
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001993 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001994
1995 // now reverse the order of the rows, since GL's are bottom-to-top, but our
bsalomon@google.comc6980972011-11-02 19:57:21 +00001996 // API presents top-to-bottom. We must preserve the padding contents. Note
1997 // that the above readPixels did not overwrite the padding.
1998 if (readDst == buffer) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001999 SkASSERT(rowBytes == readDstRowBytes);
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002000 if (flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002001 scratch.reset(tightRowBytes);
2002 void* tmpRow = scratch.get();
2003 // flip y in-place by rows
2004 const int halfY = height >> 1;
2005 char* top = reinterpret_cast<char*>(buffer);
2006 char* bottom = top + (height - 1) * rowBytes;
2007 for (int y = 0; y < halfY; y++) {
2008 memcpy(tmpRow, top, tightRowBytes);
2009 memcpy(top, bottom, tightRowBytes);
2010 memcpy(bottom, tmpRow, tightRowBytes);
2011 top += rowBytes;
2012 bottom -= rowBytes;
2013 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00002014 }
2015 } else {
egdanield803f272015-03-18 13:01:52 -07002016 SkASSERT(readDst != buffer); SkASSERT(rowBytes != tightRowBytes);
bsalomon@google.comc6980972011-11-02 19:57:21 +00002017 // copy from readDst to buffer while flipping y
caryclark@google.comcf6285b2012-06-06 12:09:01 +00002018 // const int halfY = height >> 1;
bsalomon@google.comc6980972011-11-02 19:57:21 +00002019 const char* src = reinterpret_cast<const char*>(readDst);
bsalomon@google.comc4364992011-11-07 15:54:49 +00002020 char* dst = reinterpret_cast<char*>(buffer);
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002021 if (flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002022 dst += (height-1) * rowBytes;
2023 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00002024 for (int y = 0; y < height; y++) {
2025 memcpy(dst, src, tightRowBytes);
2026 src += readDstRowBytes;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002027 if (!flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002028 dst += rowBytes;
2029 } else {
2030 dst -= rowBytes;
2031 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002032 }
2033 }
2034 return true;
2035}
2036
cdaltond4727922015-11-10 12:49:06 -08002037void GrGLGpu::flushRenderTarget(GrGLRenderTarget* target, const SkIRect* bound) {
2038
egdanield803f272015-03-18 13:01:52 -07002039 SkASSERT(target);
bsalomon6ba6fa12015-03-04 11:57:37 -08002040
egdanield803f272015-03-18 13:01:52 -07002041 uint32_t rtID = target->getUniqueID();
2042 if (fHWBoundRenderTargetUniqueID != rtID) {
bsalomon1e0bf7e2015-03-14 12:08:51 -07002043 fStats.incRenderTargetBinds();
egdanield803f272015-03-18 13:01:52 -07002044 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, target->renderFBOID()));
2045#ifdef SK_DEBUG
2046 // don't do this check in Chromium -- this is causing
2047 // lots of repeated command buffer flushes when the compositor is
2048 // rendering with Ganesh, which is really slow; even too slow for
2049 // Debug mode.
cdalton1acea862015-06-02 13:05:52 -07002050 if (kChromium_GrGLDriver != this->glContext().driver()) {
egdanield803f272015-03-18 13:01:52 -07002051 GrGLenum status;
2052 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
2053 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
2054 SkDebugf("GrGLGpu::flushRenderTarget glCheckFramebufferStatus %x\n", status);
2055 }
bsalomon160f24c2015-03-17 15:55:42 -07002056 }
egdanield803f272015-03-18 13:01:52 -07002057#endif
2058 fHWBoundRenderTargetUniqueID = rtID;
2059 const GrGLIRect& vp = target->getViewport();
2060 if (fHWViewport != vp) {
2061 vp.pushToGLViewport(this->glInterface());
2062 fHWViewport = vp;
bsalomon160f24c2015-03-17 15:55:42 -07002063 }
bsalomon16921ec2015-07-30 15:34:56 -07002064 if (this->glCaps().srgbWriteControl()) {
2065 bool enableSRGBWrite = GrPixelConfigIsSRGB(target->config());
2066 if (enableSRGBWrite && kYes_TriState != fHWSRGBFramebuffer) {
2067 GL_CALL(Enable(GR_GL_FRAMEBUFFER_SRGB));
2068 fHWSRGBFramebuffer = kYes_TriState;
2069 } else if (!enableSRGBWrite && kNo_TriState != fHWSRGBFramebuffer) {
2070 GL_CALL(Disable(GR_GL_FRAMEBUFFER_SRGB));
2071 fHWSRGBFramebuffer = kNo_TriState;
2072 }
2073 }
bsalomon5cd020f2015-03-17 12:46:56 -07002074 }
halcanary96fcdcc2015-08-27 07:41:13 -07002075 if (nullptr == bound || !bound->isEmpty()) {
egdanield803f272015-03-18 13:01:52 -07002076 target->flagAsNeedingResolve(bound);
2077 }
2078
2079 GrTexture *texture = target->asTexture();
2080 if (texture) {
2081 texture->texturePriv().dirtyMipMaps(true);
2082 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002083}
2084
twiz@google.com0f31ca72011-03-18 17:38:11 +00002085GrGLenum gPrimitiveType2GLMode[] = {
2086 GR_GL_TRIANGLES,
2087 GR_GL_TRIANGLE_STRIP,
2088 GR_GL_TRIANGLE_FAN,
2089 GR_GL_POINTS,
2090 GR_GL_LINES,
2091 GR_GL_LINE_STRIP
reed@google.comac10a2d2010-12-22 21:39:39 +00002092};
2093
bsalomon@google.comd302f142011-03-03 13:54:13 +00002094#define SWAP_PER_DRAW 0
2095
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00002096#if SWAP_PER_DRAW
commit-bot@chromium.org43823302013-09-25 20:57:51 +00002097 #if defined(SK_BUILD_FOR_MAC)
bsalomon@google.comd302f142011-03-03 13:54:13 +00002098 #include <AGL/agl.h>
commit-bot@chromium.org43823302013-09-25 20:57:51 +00002099 #elif defined(SK_BUILD_FOR_WIN32)
bsalomon@google.comce7357d2012-06-25 17:49:25 +00002100 #include <gl/GL.h>
bsalomon@google.comd302f142011-03-03 13:54:13 +00002101 void SwapBuf() {
2102 DWORD procID = GetCurrentProcessId();
2103 HWND hwnd = GetTopWindow(GetDesktopWindow());
2104 while(hwnd) {
2105 DWORD wndProcID = 0;
2106 GetWindowThreadProcessId(hwnd, &wndProcID);
2107 if(wndProcID == procID) {
2108 SwapBuffers(GetDC(hwnd));
2109 }
2110 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
2111 }
2112 }
2113 #endif
2114#endif
2115
bsalomone64eb572015-05-07 11:35:55 -07002116void GrGLGpu::onDraw(const DrawArgs& args, const GrNonInstancedVertices& vertices) {
cdaltond0a840d2015-03-16 17:19:58 -07002117 if (!this->flushGLState(args)) {
bsalomond95263c2014-12-16 13:05:12 -08002118 return;
2119 }
2120
joshualitt873ad0e2015-01-20 09:08:51 -08002121 size_t indexOffsetInBytes = 0;
bsalomoncb8979d2015-05-05 09:51:38 -07002122 this->setupGeometry(*args.fPrimitiveProcessor, vertices, &indexOffsetInBytes);
reed@google.comac10a2d2010-12-22 21:39:39 +00002123
bsalomoncb8979d2015-05-05 09:51:38 -07002124 SkASSERT((size_t)vertices.primitiveType() < SK_ARRAY_COUNT(gPrimitiveType2GLMode));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002125
bsalomoncb8979d2015-05-05 09:51:38 -07002126 if (vertices.isIndexed()) {
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00002127 GrGLvoid* indices =
bsalomoncb8979d2015-05-05 09:51:38 -07002128 reinterpret_cast<GrGLvoid*>(indexOffsetInBytes + sizeof(uint16_t) *
2129 vertices.startIndex());
bsalomon@google.com74749cd2013-01-30 16:12:41 +00002130 // info.startVertex() was accounted for by setupGeometry.
bsalomoncb8979d2015-05-05 09:51:38 -07002131 GL_CALL(DrawElements(gPrimitiveType2GLMode[vertices.primitiveType()],
2132 vertices.indexCount(),
bsalomon@google.com74749cd2013-01-30 16:12:41 +00002133 GR_GL_UNSIGNED_SHORT,
2134 indices));
2135 } else {
2136 // Pass 0 for parameter first. We have to adjust glVertexAttribPointer() to account for
2137 // startVertex in the DrawElements case. So we always rely on setupGeometry to have
2138 // accounted for startVertex.
bsalomoncb8979d2015-05-05 09:51:38 -07002139 GL_CALL(DrawArrays(gPrimitiveType2GLMode[vertices.primitiveType()], 0,
2140 vertices.vertexCount()));
bsalomon@google.com74749cd2013-01-30 16:12:41 +00002141 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00002142#if SWAP_PER_DRAW
2143 glFlush();
commit-bot@chromium.org43823302013-09-25 20:57:51 +00002144 #if defined(SK_BUILD_FOR_MAC)
bsalomon@google.comd302f142011-03-03 13:54:13 +00002145 aglSwapBuffers(aglGetCurrentContext());
2146 int set_a_break_pt_here = 9;
2147 aglSwapBuffers(aglGetCurrentContext());
commit-bot@chromium.org43823302013-09-25 20:57:51 +00002148 #elif defined(SK_BUILD_FOR_WIN32)
bsalomon@google.comd302f142011-03-03 13:54:13 +00002149 SwapBuf();
2150 int set_a_break_pt_here = 9;
2151 SwapBuf();
2152 #endif
2153#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00002154}
2155
bsalomon861e1032014-12-16 07:33:49 -08002156void GrGLGpu::onResolveRenderTarget(GrRenderTarget* target) {
bsalomon@google.com75f9f252012-01-31 13:35:56 +00002157 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00002158 if (rt->needsResolve()) {
bsalomon@google.com347c3822013-05-01 20:10:01 +00002159 // Some extensions automatically resolves the texture when it is read.
2160 if (this->glCaps().usesMSAARenderBuffers()) {
egdanield803f272015-03-18 13:01:52 -07002161 SkASSERT(rt->textureFBOID() != rt->renderFBOID());
2162 fStats.incRenderTargetBinds();
2163 fStats.incRenderTargetBinds();
2164 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER, rt->renderFBOID()));
2165 GL_CALL(BindFramebuffer(GR_GL_DRAW_FRAMEBUFFER, rt->textureFBOID()));
2166 // make sure we go through flushRenderTarget() since we've modified
2167 // the bound DRAW FBO ID.
2168 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002169 const GrGLIRect& vp = rt->getViewport();
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00002170 const SkIRect dirtyRect = rt->getResolveRect();
reed@google.comac10a2d2010-12-22 21:39:39 +00002171
bsalomon@google.com347c3822013-05-01 20:10:01 +00002172 if (GrGLCaps::kES_Apple_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002173 // Apple's extension uses the scissor as the blit bounds.
bsalomon3e791242014-12-17 13:43:13 -08002174 GrScissorState scissorState;
robertphillipse85a32d2015-02-10 08:16:55 -08002175 scissorState.set(dirtyRect);
2176 this->flushScissor(scissorState, vp, rt->origin());
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002177 GL_CALL(ResolveMultisampleFramebuffer());
2178 } else {
robertphillipse85a32d2015-02-10 08:16:55 -08002179 GrGLIRect r;
2180 r.setRelativeTo(vp, dirtyRect.fLeft, dirtyRect.fTop,
2181 dirtyRect.width(), dirtyRect.height(), target->origin());
2182
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002183 int right = r.fLeft + r.fWidth;
2184 int top = r.fBottom + r.fHeight;
derekf8c8f71a2014-09-16 06:24:57 -07002185
2186 // BlitFrameBuffer respects the scissor, so disable it.
joshualitt77b13072014-10-27 14:51:01 -07002187 this->disableScissor();
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002188 GL_CALL(BlitFramebuffer(r.fLeft, r.fBottom, right, top,
2189 r.fLeft, r.fBottom, right, top,
2190 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00002191 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002192 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00002193 rt->flagAsResolved();
reed@google.comac10a2d2010-12-22 21:39:39 +00002194 }
2195}
2196
bsalomon@google.com411dad02012-06-05 20:24:20 +00002197namespace {
bsalomon@google.comd302f142011-03-03 13:54:13 +00002198
bsalomon@google.com411dad02012-06-05 20:24:20 +00002199
2200GrGLenum gr_to_gl_stencil_op(GrStencilOp op) {
2201 static const GrGLenum gTable[] = {
2202 GR_GL_KEEP, // kKeep_StencilOp
2203 GR_GL_REPLACE, // kReplace_StencilOp
2204 GR_GL_INCR_WRAP, // kIncWrap_StencilOp
2205 GR_GL_INCR, // kIncClamp_StencilOp
2206 GR_GL_DECR_WRAP, // kDecWrap_StencilOp
2207 GR_GL_DECR, // kDecClamp_StencilOp
2208 GR_GL_ZERO, // kZero_StencilOp
2209 GR_GL_INVERT, // kInvert_StencilOp
2210 };
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +00002211 GR_STATIC_ASSERT(SK_ARRAY_COUNT(gTable) == kStencilOpCount);
bsalomon@google.com411dad02012-06-05 20:24:20 +00002212 GR_STATIC_ASSERT(0 == kKeep_StencilOp);
2213 GR_STATIC_ASSERT(1 == kReplace_StencilOp);
2214 GR_STATIC_ASSERT(2 == kIncWrap_StencilOp);
2215 GR_STATIC_ASSERT(3 == kIncClamp_StencilOp);
2216 GR_STATIC_ASSERT(4 == kDecWrap_StencilOp);
2217 GR_STATIC_ASSERT(5 == kDecClamp_StencilOp);
2218 GR_STATIC_ASSERT(6 == kZero_StencilOp);
2219 GR_STATIC_ASSERT(7 == kInvert_StencilOp);
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002220 SkASSERT((unsigned) op < kStencilOpCount);
bsalomon@google.com411dad02012-06-05 20:24:20 +00002221 return gTable[op];
2222}
2223
2224void set_gl_stencil(const GrGLInterface* gl,
bsalomon@google.coma3201942012-06-21 19:58:20 +00002225 const GrStencilSettings& settings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00002226 GrGLenum glFace,
bsalomon@google.coma3201942012-06-21 19:58:20 +00002227 GrStencilSettings::Face grFace) {
kkinnunenccdaa042014-08-20 01:36:23 -07002228 GrGLenum glFunc = GrToGLStencilFunc(settings.func(grFace));
bsalomon@google.coma3201942012-06-21 19:58:20 +00002229 GrGLenum glFailOp = gr_to_gl_stencil_op(settings.failOp(grFace));
2230 GrGLenum glPassOp = gr_to_gl_stencil_op(settings.passOp(grFace));
2231
2232 GrGLint ref = settings.funcRef(grFace);
2233 GrGLint mask = settings.funcMask(grFace);
2234 GrGLint writeMask = settings.writeMask(grFace);
bsalomon@google.com411dad02012-06-05 20:24:20 +00002235
2236 if (GR_GL_FRONT_AND_BACK == glFace) {
2237 // we call the combined func just in case separate stencil is not
2238 // supported.
2239 GR_GL_CALL(gl, StencilFunc(glFunc, ref, mask));
2240 GR_GL_CALL(gl, StencilMask(writeMask));
vbuzinovc5d58f02015-08-21 05:24:24 -07002241 GR_GL_CALL(gl, StencilOp(glFailOp, GR_GL_KEEP, glPassOp));
bsalomon@google.com411dad02012-06-05 20:24:20 +00002242 } else {
2243 GR_GL_CALL(gl, StencilFuncSeparate(glFace, glFunc, ref, mask));
2244 GR_GL_CALL(gl, StencilMaskSeparate(glFace, writeMask));
vbuzinovc5d58f02015-08-21 05:24:24 -07002245 GR_GL_CALL(gl, StencilOpSeparate(glFace, glFailOp, GR_GL_KEEP, glPassOp));
bsalomon@google.com411dad02012-06-05 20:24:20 +00002246 }
2247}
2248}
bsalomon@google.comd302f142011-03-03 13:54:13 +00002249
bsalomon3e791242014-12-17 13:43:13 -08002250void GrGLGpu::flushStencil(const GrStencilSettings& stencilSettings) {
2251 if (fHWStencilSettings != stencilSettings) {
joshualitta58fe352014-10-27 08:39:00 -07002252 if (stencilSettings.isDisabled()) {
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00002253 if (kNo_TriState != fHWStencilTestEnabled) {
2254 GL_CALL(Disable(GR_GL_STENCIL_TEST));
2255 fHWStencilTestEnabled = kNo_TriState;
2256 }
2257 } else {
2258 if (kYes_TriState != fHWStencilTestEnabled) {
2259 GL_CALL(Enable(GR_GL_STENCIL_TEST));
2260 fHWStencilTestEnabled = kYes_TriState;
2261 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00002262 }
joshualitta58fe352014-10-27 08:39:00 -07002263 if (!stencilSettings.isDisabled()) {
bsalomon@google.combcce8922013-03-25 15:38:39 +00002264 if (this->caps()->twoSidedStencilSupport()) {
bsalomon@google.com411dad02012-06-05 20:24:20 +00002265 set_gl_stencil(this->glInterface(),
joshualitta58fe352014-10-27 08:39:00 -07002266 stencilSettings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00002267 GR_GL_FRONT,
bsalomon@google.coma3201942012-06-21 19:58:20 +00002268 GrStencilSettings::kFront_Face);
bsalomon@google.com411dad02012-06-05 20:24:20 +00002269 set_gl_stencil(this->glInterface(),
joshualitta58fe352014-10-27 08:39:00 -07002270 stencilSettings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00002271 GR_GL_BACK,
bsalomon@google.coma3201942012-06-21 19:58:20 +00002272 GrStencilSettings::kBack_Face);
bsalomon@google.comd302f142011-03-03 13:54:13 +00002273 } else {
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_AND_BACK,
bsalomon@google.coma3201942012-06-21 19:58:20 +00002277 GrStencilSettings::kFront_Face);
bsalomon@google.comd302f142011-03-03 13:54:13 +00002278 }
2279 }
joshualitta58fe352014-10-27 08:39:00 -07002280 fHWStencilSettings = stencilSettings;
reed@google.comac10a2d2010-12-22 21:39:39 +00002281 }
2282}
2283
cdalton7e806f32015-11-11 15:16:07 -08002284void GrGLGpu::flushHWAAState(GrRenderTarget* rt, bool useHWAA) {
vbuzinovdded6962015-06-12 08:59:45 -07002285 SkASSERT(!useHWAA || rt->isStencilBufferMultisampled());
bsalomon@google.com202d1392013-03-19 18:58:08 +00002286
cdaltond4727922015-11-10 12:49:06 -08002287 if (this->glCaps().multisampleDisableSupport()) {
cdaltond0a840d2015-03-16 17:19:58 -07002288 if (useHWAA) {
2289 if (kYes_TriState != fMSAAEnabled) {
2290 GL_CALL(Enable(GR_GL_MULTISAMPLE));
2291 fMSAAEnabled = kYes_TriState;
2292 }
2293 } else {
2294 if (kNo_TriState != fMSAAEnabled) {
2295 GL_CALL(Disable(GR_GL_MULTISAMPLE));
2296 fMSAAEnabled = kNo_TriState;
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00002297 }
2298 }
2299 }
2300}
2301
egdaniel080e6732014-12-22 07:35:52 -08002302void GrGLGpu::flushBlend(const GrXferProcessor::BlendInfo& blendInfo) {
egdanielb414f252014-07-29 13:15:47 -07002303 // Any optimization to disable blending should have already been applied and
cdalton8917d622015-05-06 13:40:21 -07002304 // tweaked the equation to "add" or "subtract", and the coeffs to (1, 0).
bsalomonf7cc8772015-05-11 11:21:14 -07002305
cdalton8917d622015-05-06 13:40:21 -07002306 GrBlendEquation equation = blendInfo.fEquation;
egdanielc2304142014-12-11 13:15:13 -08002307 GrBlendCoeff srcCoeff = blendInfo.fSrcBlend;
2308 GrBlendCoeff dstCoeff = blendInfo.fDstBlend;
cdalton8917d622015-05-06 13:40:21 -07002309 bool blendOff = (kAdd_GrBlendEquation == equation || kSubtract_GrBlendEquation == equation) &&
2310 kOne_GrBlendCoeff == srcCoeff && kZero_GrBlendCoeff == dstCoeff;
egdanielb414f252014-07-29 13:15:47 -07002311 if (blendOff) {
2312 if (kNo_TriState != fHWBlendState.fEnabled) {
2313 GL_CALL(Disable(GR_GL_BLEND));
joel.liang9764c402015-07-09 19:46:18 -07002314
2315 // Workaround for the ARM KHR_blend_equation_advanced blacklist issue
2316 // https://code.google.com/p/skia/issues/detail?id=3943
2317 if (kARM_GrGLVendor == this->ctxInfo().vendor() &&
2318 GrBlendEquationIsAdvanced(fHWBlendState.fEquation)) {
2319 SkASSERT(this->caps()->advancedBlendEquationSupport());
2320 // Set to any basic blending equation.
2321 GrBlendEquation blend_equation = kAdd_GrBlendEquation;
2322 GL_CALL(BlendEquation(gXfermodeEquation2Blend[blend_equation]));
2323 fHWBlendState.fEquation = blend_equation;
2324 }
2325
egdanielb414f252014-07-29 13:15:47 -07002326 fHWBlendState.fEnabled = kNo_TriState;
2327 }
cdalton8917d622015-05-06 13:40:21 -07002328 return;
2329 }
2330
2331 if (kYes_TriState != fHWBlendState.fEnabled) {
2332 GL_CALL(Enable(GR_GL_BLEND));
2333 fHWBlendState.fEnabled = kYes_TriState;
2334 }
2335
2336 if (fHWBlendState.fEquation != equation) {
2337 GL_CALL(BlendEquation(gXfermodeEquation2Blend[equation]));
2338 fHWBlendState.fEquation = equation;
2339 }
2340
2341 if (GrBlendEquationIsAdvanced(equation)) {
2342 SkASSERT(this->caps()->advancedBlendEquationSupport());
2343 // Advanced equations have no other blend state.
2344 return;
2345 }
2346
2347 if (fHWBlendState.fSrcCoeff != srcCoeff ||
2348 fHWBlendState.fDstCoeff != dstCoeff) {
2349 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
2350 gXfermodeCoeff2Blend[dstCoeff]));
2351 fHWBlendState.fSrcCoeff = srcCoeff;
2352 fHWBlendState.fDstCoeff = dstCoeff;
2353 }
2354
2355 GrColor blendConst = blendInfo.fBlendConstant;
2356 if ((BlendCoeffReferencesConstant(srcCoeff) ||
2357 BlendCoeffReferencesConstant(dstCoeff)) &&
2358 (!fHWBlendState.fConstColorValid ||
2359 fHWBlendState.fConstColor != blendConst)) {
2360 GrGLfloat c[4];
2361 GrColorToRGBAFloat(blendConst, c);
2362 GL_CALL(BlendColor(c[0], c[1], c[2], c[3]));
2363 fHWBlendState.fConstColor = blendConst;
2364 fHWBlendState.fConstColorValid = true;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002365 }
2366}
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002367
commit-bot@chromium.org6364b5e2013-08-20 20:22:52 +00002368static inline GrGLenum tile_to_gl_wrap(SkShader::TileMode tm) {
bsalomon@google.comb8670992012-07-25 21:27:09 +00002369 static const GrGLenum gWrapModes[] = {
2370 GR_GL_CLAMP_TO_EDGE,
2371 GR_GL_REPEAT,
2372 GR_GL_MIRRORED_REPEAT
2373 };
commit-bot@chromium.org5d7ca952013-04-22 20:26:44 +00002374 GR_STATIC_ASSERT(SkShader::kTileModeCount == SK_ARRAY_COUNT(gWrapModes));
bsalomon@google.comb8670992012-07-25 21:27:09 +00002375 GR_STATIC_ASSERT(0 == SkShader::kClamp_TileMode);
2376 GR_STATIC_ASSERT(1 == SkShader::kRepeat_TileMode);
2377 GR_STATIC_ASSERT(2 == SkShader::kMirror_TileMode);
2378 return gWrapModes[tm];
2379}
2380
egdanielb7e7d572015-11-04 04:23:53 -08002381static GrGLenum get_component_enum_from_char(char component) {
2382 switch (component) {
2383 case 'r':
2384 return GR_GL_RED;
2385 case 'g':
2386 return GR_GL_GREEN;
2387 case 'b':
2388 return GR_GL_BLUE;
2389 case 'a':
2390 return GR_GL_ALPHA;
2391 default:
2392 SkFAIL("Unsupported component");
2393 return 0;
2394 }
2395}
2396
2397/** If texture swizzling is available using tex parameters then it is preferred over mangling
2398 the generated shader code. This potentially allows greater reuse of cached shaders. */
2399static void get_tex_param_swizzle(GrPixelConfig config,
2400 const GrGLSLCaps& caps,
2401 GrGLenum* glSwizzle) {
2402 const char* swizzle = caps.getSwizzleMap(config);
2403 for (int i = 0; i < 4; ++i) {
2404 glSwizzle[i] = get_component_enum_from_char(swizzle[i]);
egdaniel574a4c12015-11-02 06:22:44 -08002405 }
2406}
2407
bsalomon861e1032014-12-16 07:33:49 -08002408void GrGLGpu::bindTexture(int unitIdx, const GrTextureParams& params, GrGLTexture* texture) {
bsalomon49f085d2014-09-05 13:34:00 -07002409 SkASSERT(texture);
tomhudson@google.comd0c1a062012-07-12 17:23:52 +00002410
reed856e9d92015-09-30 12:21:45 -07002411#ifdef SK_DEBUG
2412 if (!this->caps()->npotTextureTileSupport()) {
2413 const bool tileX = SkShader::kClamp_TileMode != params.getTileModeX();
2414 const bool tileY = SkShader::kClamp_TileMode != params.getTileModeY();
2415 if (tileX || tileY) {
2416 const int w = texture->width();
2417 const int h = texture->height();
2418 SkASSERT(SkIsPow2(w) && SkIsPow2(h));
2419 }
2420 }
2421#endif
2422
bsalomon@google.comb8670992012-07-25 21:27:09 +00002423 // If we created a rt/tex and rendered to it without using a texture and now we're texturing
2424 // 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 +00002425 // out of the "last != next" check.
bsalomon37dd3312014-11-03 08:47:23 -08002426 GrGLRenderTarget* texRT = static_cast<GrGLRenderTarget*>(texture->asRenderTarget());
bsalomon49f085d2014-09-05 13:34:00 -07002427 if (texRT) {
bsalomon@google.com4c883782012-06-04 19:05:11 +00002428 this->onResolveRenderTarget(texRT);
2429 }
2430
bsalomon1c63bf62014-07-22 13:09:46 -07002431 uint32_t textureID = texture->getUniqueID();
bsalomon10528f12015-10-14 12:54:52 -07002432 GrGLenum target = texture->target();
bsalomon1c63bf62014-07-22 13:09:46 -07002433 if (fHWBoundTextureUniqueIDs[unitIdx] != textureID) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002434 this->setTextureUnit(unitIdx);
bsalomon10528f12015-10-14 12:54:52 -07002435 GL_CALL(BindTexture(target, texture->textureID()));
bsalomon1c63bf62014-07-22 13:09:46 -07002436 fHWBoundTextureUniqueIDs[unitIdx] = textureID;
bsalomon@google.com4c883782012-06-04 19:05:11 +00002437 }
2438
bsalomon@google.com4c883782012-06-04 19:05:11 +00002439 ResetTimestamp timestamp;
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002440 const GrGLTexture::TexParams& oldTexParams = texture->getCachedTexParams(&timestamp);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002441 bool setAll = timestamp < this->getResetTimestamp();
2442 GrGLTexture::TexParams newTexParams;
2443
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00002444 static GrGLenum glMinFilterModes[] = {
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +00002445 GR_GL_NEAREST,
2446 GR_GL_LINEAR,
2447 GR_GL_LINEAR_MIPMAP_LINEAR
2448 };
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00002449 static GrGLenum glMagFilterModes[] = {
2450 GR_GL_NEAREST,
2451 GR_GL_LINEAR,
2452 GR_GL_LINEAR
2453 };
commit-bot@chromium.org47442312013-12-19 16:18:01 +00002454 GrTextureParams::FilterMode filterMode = params.filterMode();
bsalomonefd7d452014-10-23 14:17:46 -07002455
2456 if (GrTextureParams::kMipMap_FilterMode == filterMode) {
2457 if (!this->caps()->mipMapSupport() || GrPixelConfigIsCompressed(texture->config())) {
2458 filterMode = GrTextureParams::kBilerp_FilterMode;
2459 }
commit-bot@chromium.org47442312013-12-19 16:18:01 +00002460 }
bsalomonefd7d452014-10-23 14:17:46 -07002461
commit-bot@chromium.org47442312013-12-19 16:18:01 +00002462 newTexParams.fMinFilter = glMinFilterModes[filterMode];
2463 newTexParams.fMagFilter = glMagFilterModes[filterMode];
skia.committer@gmail.comaeefb2a2013-07-27 07:01:06 +00002464
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +00002465 if (GrTextureParams::kMipMap_FilterMode == filterMode &&
bsalomonefd7d452014-10-23 14:17:46 -07002466 texture->texturePriv().mipMapsAreDirty()) {
bsalomon10528f12015-10-14 12:54:52 -07002467 GL_CALL(GenerateMipmap(target));
bsalomonafbf2d62014-09-30 12:18:44 -07002468 texture->texturePriv().dirtyMipMaps(false);
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +00002469 }
bsalomon@google.com4c883782012-06-04 19:05:11 +00002470
bsalomon@google.comb8670992012-07-25 21:27:09 +00002471 newTexParams.fWrapS = tile_to_gl_wrap(params.getTileModeX());
2472 newTexParams.fWrapT = tile_to_gl_wrap(params.getTileModeY());
egdanielb7e7d572015-11-04 04:23:53 -08002473 get_tex_param_swizzle(texture->config(), *this->glCaps().glslCaps(), newTexParams.fSwizzleRGBA);
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00002474 if (setAll || newTexParams.fMagFilter != oldTexParams.fMagFilter) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002475 this->setTextureUnit(unitIdx);
bsalomon10528f12015-10-14 12:54:52 -07002476 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MAG_FILTER, newTexParams.fMagFilter));
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00002477 }
2478 if (setAll || newTexParams.fMinFilter != oldTexParams.fMinFilter) {
2479 this->setTextureUnit(unitIdx);
bsalomon10528f12015-10-14 12:54:52 -07002480 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MIN_FILTER, newTexParams.fMinFilter));
bsalomon@google.com4c883782012-06-04 19:05:11 +00002481 }
2482 if (setAll || newTexParams.fWrapS != oldTexParams.fWrapS) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002483 this->setTextureUnit(unitIdx);
bsalomon10528f12015-10-14 12:54:52 -07002484 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_WRAP_S, newTexParams.fWrapS));
bsalomon@google.com4c883782012-06-04 19:05:11 +00002485 }
2486 if (setAll || newTexParams.fWrapT != oldTexParams.fWrapT) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002487 this->setTextureUnit(unitIdx);
bsalomon10528f12015-10-14 12:54:52 -07002488 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_WRAP_T, newTexParams.fWrapT));
bsalomon@google.com4c883782012-06-04 19:05:11 +00002489 }
egdanielb7e7d572015-11-04 04:23:53 -08002490 if (!this->glCaps().glslCaps()->mustSwizzleInShader() &&
bsalomon@google.com4c883782012-06-04 19:05:11 +00002491 (setAll || memcmp(newTexParams.fSwizzleRGBA,
2492 oldTexParams.fSwizzleRGBA,
2493 sizeof(newTexParams.fSwizzleRGBA)))) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002494 this->setTextureUnit(unitIdx);
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +00002495 if (this->glStandard() == kGLES_GrGLStandard) {
commit-bot@chromium.org6364b5e2013-08-20 20:22:52 +00002496 // ES3 added swizzle support but not GL_TEXTURE_SWIZZLE_RGBA.
2497 const GrGLenum* swizzle = newTexParams.fSwizzleRGBA;
bsalomon10528f12015-10-14 12:54:52 -07002498 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_R, swizzle[0]));
2499 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_G, swizzle[1]));
2500 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_B, swizzle[2]));
2501 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_A, swizzle[3]));
commit-bot@chromium.org6364b5e2013-08-20 20:22:52 +00002502 } else {
2503 GR_STATIC_ASSERT(sizeof(newTexParams.fSwizzleRGBA[0]) == sizeof(GrGLint));
2504 const GrGLint* swizzle = reinterpret_cast<const GrGLint*>(newTexParams.fSwizzleRGBA);
bsalomon10528f12015-10-14 12:54:52 -07002505 GL_CALL(TexParameteriv(target, GR_GL_TEXTURE_SWIZZLE_RGBA, swizzle));
commit-bot@chromium.org6364b5e2013-08-20 20:22:52 +00002506 }
bsalomon@google.com4c883782012-06-04 19:05:11 +00002507 }
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002508 texture->setCachedTexParams(newTexParams, this->getResetTimestamp());
bsalomon@google.com4c883782012-06-04 19:05:11 +00002509}
2510
egdaniel080e6732014-12-22 07:35:52 -08002511void GrGLGpu::flushColorWrite(bool writeColor) {
2512 if (!writeColor) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002513 if (kNo_TriState != fHWWriteToColor) {
2514 GL_CALL(ColorMask(GR_GL_FALSE, GR_GL_FALSE,
2515 GR_GL_FALSE, GR_GL_FALSE));
2516 fHWWriteToColor = kNo_TriState;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002517 }
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002518 } else {
2519 if (kYes_TriState != fHWWriteToColor) {
2520 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
2521 fHWWriteToColor = kYes_TriState;
2522 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00002523 }
bsalomon3e791242014-12-17 13:43:13 -08002524}
bsalomon@google.comd302f142011-03-03 13:54:13 +00002525
egdaniel8dd688b2015-01-22 10:16:09 -08002526void GrGLGpu::flushDrawFace(GrPipelineBuilder::DrawFace face) {
bsalomon3e791242014-12-17 13:43:13 -08002527 if (fHWDrawFace != face) {
2528 switch (face) {
egdaniel8dd688b2015-01-22 10:16:09 -08002529 case GrPipelineBuilder::kCCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002530 GL_CALL(Enable(GR_GL_CULL_FACE));
2531 GL_CALL(CullFace(GR_GL_BACK));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002532 break;
egdaniel8dd688b2015-01-22 10:16:09 -08002533 case GrPipelineBuilder::kCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002534 GL_CALL(Enable(GR_GL_CULL_FACE));
2535 GL_CALL(CullFace(GR_GL_FRONT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002536 break;
egdaniel8dd688b2015-01-22 10:16:09 -08002537 case GrPipelineBuilder::kBoth_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002538 GL_CALL(Disable(GR_GL_CULL_FACE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002539 break;
2540 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +00002541 SkFAIL("Unknown draw face.");
bsalomon@google.comd302f142011-03-03 13:54:13 +00002542 }
bsalomon3e791242014-12-17 13:43:13 -08002543 fHWDrawFace = face;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002544 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002545}
2546
bsalomon861e1032014-12-16 07:33:49 -08002547bool GrGLGpu::configToGLFormats(GrPixelConfig config,
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002548 bool getSizedInternalFormat,
2549 GrGLenum* internalFormat,
2550 GrGLenum* externalFormat,
jvanverth672bb7f2015-07-13 07:19:57 -07002551 GrGLenum* externalType) const {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002552 GrGLenum dontCare;
halcanary96fcdcc2015-08-27 07:41:13 -07002553 if (nullptr == internalFormat) {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002554 internalFormat = &dontCare;
2555 }
halcanary96fcdcc2015-08-27 07:41:13 -07002556 if (nullptr == externalFormat) {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002557 externalFormat = &dontCare;
2558 }
halcanary96fcdcc2015-08-27 07:41:13 -07002559 if (nullptr == externalType) {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002560 externalType = &dontCare;
2561 }
2562
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +00002563 if(!this->glCaps().isConfigTexturable(config)) {
2564 return false;
2565 }
2566
reed@google.comac10a2d2010-12-22 21:39:39 +00002567 switch (config) {
bsalomon@google.com0342a852012-08-20 19:22:38 +00002568 case kRGBA_8888_GrPixelConfig:
bsalomon@google.comc4364992011-11-07 15:54:49 +00002569 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002570 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002571 if (getSizedInternalFormat) {
2572 *internalFormat = GR_GL_RGBA8;
2573 } else {
2574 *internalFormat = GR_GL_RGBA;
2575 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002576 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.comc4364992011-11-07 15:54:49 +00002577 break;
bsalomon@google.com0342a852012-08-20 19:22:38 +00002578 case kBGRA_8888_GrPixelConfig:
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002579 if (this->glCaps().bgraIsInternalFormat()) {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002580 if (getSizedInternalFormat) {
2581 *internalFormat = GR_GL_BGRA8;
2582 } else {
2583 *internalFormat = GR_GL_BGRA;
2584 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002585 } else {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002586 if (getSizedInternalFormat) {
2587 *internalFormat = GR_GL_RGBA8;
2588 } else {
2589 *internalFormat = GR_GL_RGBA;
2590 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002591 }
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002592 *externalFormat = GR_GL_BGRA;
bsalomon@google.com6f379512011-11-16 20:36:03 +00002593 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002594 break;
jvanverthfa1e8a72014-12-22 08:31:49 -08002595 case kSRGBA_8888_GrPixelConfig:
bsalomon16921ec2015-07-30 15:34:56 -07002596 if (getSizedInternalFormat) {
jvanverthfa1e8a72014-12-22 08:31:49 -08002597 *internalFormat = GR_GL_SRGB8_ALPHA8;
2598 } else {
2599 *internalFormat = GR_GL_SRGB_ALPHA;
bsalomon16921ec2015-07-30 15:34:56 -07002600 }
2601 // OpenGL ES 2.0 + GL_EXT_sRGB allows GL_SRGB_ALPHA to be specified as the <format>
2602 // param to Tex(Sub)Image2D. ES 2.0 requires the internalFormat and format to match.
2603 // Thus, on ES 2.0 we will use GL_SRGB_ALPHA as the externalFormat. However,
2604 // onReadPixels needs code to override that because GL_SRGB_ALPHA is not allowed as a
2605 // glReadPixels format.
2606 // On OpenGL and ES 3.0 GL_SRGB_ALPHA does not work for the <format> param to
2607 // glReadPixels nor does it work with Tex(Sub)Image2D So we always set the externalFormat
2608 // return to GL_RGBA.
2609 if (this->glStandard() == kGLES_GrGLStandard &&
2610 this->glVersion() == GR_GL_VER(2,0)) {
jvanverth99babf22015-05-22 06:06:40 -07002611 *externalFormat = GR_GL_SRGB_ALPHA;
bsalomon16921ec2015-07-30 15:34:56 -07002612 } else {
2613 *externalFormat = GR_GL_RGBA;
jvanverthfa1e8a72014-12-22 08:31:49 -08002614 }
2615 *externalType = GR_GL_UNSIGNED_BYTE;
2616 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002617 case kRGB_565_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002618 *internalFormat = GR_GL_RGB;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002619 *externalFormat = GR_GL_RGB;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002620 if (getSizedInternalFormat) {
jvanverth3f801cb2014-12-16 09:49:38 -08002621 if (!this->glCaps().ES2CompatibilitySupport()) {
2622 *internalFormat = GR_GL_RGB5;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002623 } else {
2624 *internalFormat = GR_GL_RGB565;
2625 }
2626 } else {
2627 *internalFormat = GR_GL_RGB;
2628 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002629 *externalType = GR_GL_UNSIGNED_SHORT_5_6_5;
reed@google.comac10a2d2010-12-22 21:39:39 +00002630 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002631 case kRGBA_4444_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002632 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002633 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002634 if (getSizedInternalFormat) {
2635 *internalFormat = GR_GL_RGBA4;
2636 } else {
2637 *internalFormat = GR_GL_RGBA;
2638 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002639 *externalType = GR_GL_UNSIGNED_SHORT_4_4_4_4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002640 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002641 case kIndex_8_GrPixelConfig:
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +00002642 // no sized/unsized internal format distinction here
2643 *internalFormat = GR_GL_PALETTE8_RGBA8;
reed@google.comac10a2d2010-12-22 21:39:39 +00002644 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002645 case kAlpha_8_GrPixelConfig:
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002646 if (this->glCaps().textureRedSupport()) {
2647 *internalFormat = GR_GL_RED;
2648 *externalFormat = GR_GL_RED;
2649 if (getSizedInternalFormat) {
2650 *internalFormat = GR_GL_R8;
2651 } else {
2652 *internalFormat = GR_GL_RED;
2653 }
2654 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002655 } else {
2656 *internalFormat = GR_GL_ALPHA;
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002657 *externalFormat = GR_GL_ALPHA;
2658 if (getSizedInternalFormat) {
2659 *internalFormat = GR_GL_ALPHA8;
2660 } else {
2661 *internalFormat = GR_GL_ALPHA;
2662 }
2663 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002664 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002665 break;
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +00002666 case kETC1_GrPixelConfig:
egdanield588c012015-03-27 12:22:10 -07002667 *internalFormat = GR_GL_COMPRESSED_ETC1_RGB8;
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +00002668 break;
2669 case kLATC_GrPixelConfig:
2670 switch(this->glCaps().latcAlias()) {
2671 case GrGLCaps::kLATC_LATCAlias:
2672 *internalFormat = GR_GL_COMPRESSED_LUMINANCE_LATC1;
2673 break;
2674 case GrGLCaps::kRGTC_LATCAlias:
2675 *internalFormat = GR_GL_COMPRESSED_RED_RGTC1;
2676 break;
2677 case GrGLCaps::k3DC_LATCAlias:
2678 *internalFormat = GR_GL_COMPRESSED_3DC_X;
2679 break;
2680 }
2681 break;
krajcevski238b4562014-06-30 09:09:22 -07002682 case kR11_EAC_GrPixelConfig:
egdanield588c012015-03-27 12:22:10 -07002683 *internalFormat = GR_GL_COMPRESSED_R11_EAC;
krajcevski238b4562014-06-30 09:09:22 -07002684 break;
krajcevski7ef21622014-07-16 15:21:13 -07002685
2686 case kASTC_12x12_GrPixelConfig:
egdanield588c012015-03-27 12:22:10 -07002687 *internalFormat = GR_GL_COMPRESSED_RGBA_ASTC_12x12_KHR;
krajcevski7ef21622014-07-16 15:21:13 -07002688 break;
2689
joshualittee5da552014-07-16 13:32:56 -07002690 case kRGBA_float_GrPixelConfig:
2691 *internalFormat = GR_GL_RGBA32F;
2692 *externalFormat = GR_GL_RGBA;
2693 *externalType = GR_GL_FLOAT;
2694 break;
krajcevski7ef21622014-07-16 15:21:13 -07002695
jvanverth28f9c602014-12-05 13:06:35 -08002696 case kAlpha_half_GrPixelConfig:
jvanverth1334c212014-12-18 05:44:55 -08002697 if (this->glCaps().textureRedSupport()) {
2698 if (getSizedInternalFormat) {
2699 *internalFormat = GR_GL_R16F;
2700 } else {
2701 *internalFormat = GR_GL_RED;
2702 }
jvanverth28f9c602014-12-05 13:06:35 -08002703 *externalFormat = GR_GL_RED;
jvanverth1334c212014-12-18 05:44:55 -08002704 } else {
2705 if (getSizedInternalFormat) {
2706 *internalFormat = GR_GL_ALPHA16F;
2707 } else {
2708 *internalFormat = GR_GL_ALPHA;
2709 }
2710 *externalFormat = GR_GL_ALPHA;
2711 }
2712 if (kGL_GrGLStandard == this->glStandard() || this->glVersion() >= GR_GL_VER(3, 0)) {
jvanvertha60b2ea2014-12-12 05:58:06 -08002713 *externalType = GR_GL_HALF_FLOAT;
jvanverth28f9c602014-12-05 13:06:35 -08002714 } else {
jvanverth1334c212014-12-18 05:44:55 -08002715 *externalType = GR_GL_HALF_FLOAT_OES;
jvanverth28f9c602014-12-05 13:06:35 -08002716 }
2717 break;
2718
jvanverthfb5df432015-05-21 08:12:27 -07002719 case kRGBA_half_GrPixelConfig:
2720 *internalFormat = GR_GL_RGBA16F;
2721 *externalFormat = GR_GL_RGBA;
2722 if (kGL_GrGLStandard == this->glStandard() || this->glVersion() >= GR_GL_VER(3, 0)) {
2723 *externalType = GR_GL_HALF_FLOAT;
2724 } else {
2725 *externalType = GR_GL_HALF_FLOAT_OES;
2726 }
2727 break;
2728
reed@google.comac10a2d2010-12-22 21:39:39 +00002729 default:
2730 return false;
2731 }
2732 return true;
2733}
2734
bsalomon861e1032014-12-16 07:33:49 -08002735void GrGLGpu::setTextureUnit(int unit) {
bsalomon1c63bf62014-07-22 13:09:46 -07002736 SkASSERT(unit >= 0 && unit < fHWBoundTextureUniqueIDs.count());
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002737 if (unit != fHWActiveTextureUnitIdx) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002738 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com49209392012-06-05 15:13:46 +00002739 fHWActiveTextureUnitIdx = unit;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002740 }
2741}
bsalomon@google.com316f99232011-01-13 21:28:12 +00002742
bsalomon861e1032014-12-16 07:33:49 -08002743void GrGLGpu::setScratchTextureUnit() {
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002744 // Bind the last texture unit since it is the least likely to be used by GrGLProgram.
bsalomon1c63bf62014-07-22 13:09:46 -07002745 int lastUnitIdx = fHWBoundTextureUniqueIDs.count() - 1;
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002746 if (lastUnitIdx != fHWActiveTextureUnitIdx) {
2747 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + lastUnitIdx));
2748 fHWActiveTextureUnitIdx = lastUnitIdx;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002749 }
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002750 // clear out the this field so that if a program does use this unit it will rebind the correct
2751 // texture.
bsalomon1c63bf62014-07-22 13:09:46 -07002752 fHWBoundTextureUniqueIDs[lastUnitIdx] = SK_InvalidUniqueID;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002753}
2754
bsalomon0315dbc2015-11-20 20:24:31 -08002755namespace {
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002756// Determines whether glBlitFramebuffer could be used between src and dst.
bsalomon0315dbc2015-11-20 20:24:31 -08002757inline bool can_blit_framebuffer(const GrSurface* dst,
2758 const GrSurface* src,
2759 const GrGLGpu* gpu) {
commit-bot@chromium.org6b7938f2013-10-15 14:18:16 +00002760 if (gpu->glCaps().isConfigRenderable(dst->config(), dst->desc().fSampleCnt > 0) &&
2761 gpu->glCaps().isConfigRenderable(src->config(), src->desc().fSampleCnt > 0) &&
bsalomon@google.com347c3822013-05-01 20:10:01 +00002762 gpu->glCaps().usesMSAARenderBuffers()) {
commit-bot@chromium.orga8e5a062013-09-05 23:44:09 +00002763 // ES3 doesn't allow framebuffer blits when the src has MSAA and the configs don't match
2764 // or the rects are not the same (not just the same size but have the same edges).
2765 if (GrGLCaps::kES_3_0_MSFBOType == gpu->glCaps().msFBOType() &&
2766 (src->desc().fSampleCnt > 0 || src->config() != dst->config())) {
2767 return false;
2768 }
bsalomon@google.comeb851172013-04-15 13:51:00 +00002769 return true;
2770 } else {
2771 return false;
2772 }
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002773}
bsalomon@google.comeb851172013-04-15 13:51:00 +00002774
bsalomon0315dbc2015-11-20 20:24:31 -08002775inline bool can_copy_texsubimage(const GrSurface* dst,
2776 const GrSurface* src,
2777 const GrGLGpu* gpu) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002778 // Table 3.9 of the ES2 spec indicates the supported formats with CopyTexSubImage
2779 // and BGRA isn't in the spec. There doesn't appear to be any extension that adds it. Perhaps
2780 // many drivers would allow it to work, but ANGLE does not.
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +00002781 if (kGLES_GrGLStandard == gpu->glStandard() && gpu->glCaps().bgraIsInternalFormat() &&
bsalomon@google.comeb851172013-04-15 13:51:00 +00002782 (kBGRA_8888_GrPixelConfig == dst->config() || kBGRA_8888_GrPixelConfig == src->config())) {
2783 return false;
2784 }
2785 const GrGLRenderTarget* dstRT = static_cast<const GrGLRenderTarget*>(dst->asRenderTarget());
2786 // If dst is multisampled (and uses an extension where there is a separate MSAA renderbuffer)
2787 // then we don't want to copy to the texture but to the MSAA buffer.
egdanield803f272015-03-18 13:01:52 -07002788 if (dstRT && dstRT->renderFBOID() != dstRT->textureFBOID()) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002789 return false;
2790 }
bsalomon@google.coma2719852013-04-17 14:25:27 +00002791 const GrGLRenderTarget* srcRT = static_cast<const GrGLRenderTarget*>(src->asRenderTarget());
2792 // If the src is multisampled (and uses an extension where there is a separate MSAA
2793 // renderbuffer) then it is an invalid operation to call CopyTexSubImage
egdanield803f272015-03-18 13:01:52 -07002794 if (srcRT && srcRT->renderFBOID() != srcRT->textureFBOID()) {
bsalomon@google.coma2719852013-04-17 14:25:27 +00002795 return false;
2796 }
commit-bot@chromium.org6b7938f2013-10-15 14:18:16 +00002797 if (gpu->glCaps().isConfigRenderable(src->config(), src->desc().fSampleCnt > 0) &&
bsalomon0315dbc2015-11-20 20:24:31 -08002798 dst->asTexture() &&
2799 dst->origin() == src->origin() &&
2800 !GrPixelConfigIsCompressed(src->config())) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002801 return true;
2802 } else {
2803 return false;
2804 }
2805}
2806
bsalomon0315dbc2015-11-20 20:24:31 -08002807}
2808
bsalomon@google.comeb851172013-04-15 13:51:00 +00002809// If a temporary FBO was created, its non-zero ID is returned. The viewport that the copy rect is
2810// relative to is output.
bsalomon10528f12015-10-14 12:54:52 -07002811void GrGLGpu::bindSurfaceFBOForCopy(GrSurface* surface, GrGLenum fboTarget, GrGLIRect* viewport,
2812 TempFBOTarget tempFBOTarget) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002813 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(surface->asRenderTarget());
halcanary96fcdcc2015-08-27 07:41:13 -07002814 if (nullptr == rt) {
bsalomon49f085d2014-09-05 13:34:00 -07002815 SkASSERT(surface->asTexture());
bsalomon@google.comeb851172013-04-15 13:51:00 +00002816 GrGLuint texID = static_cast<GrGLTexture*>(surface->asTexture())->textureID();
bsalomon10528f12015-10-14 12:54:52 -07002817 GrGLenum target = static_cast<GrGLTexture*>(surface->asTexture())->target();
egdanield803f272015-03-18 13:01:52 -07002818 GrGLuint* tempFBOID;
2819 tempFBOID = kSrc_TempFBOTarget == tempFBOTarget ? &fTempSrcFBOID : &fTempDstFBOID;
egdaniel0f5f9672015-02-03 11:10:51 -08002820
egdanield803f272015-03-18 13:01:52 -07002821 if (0 == *tempFBOID) {
2822 GR_GL_CALL(this->glInterface(), GenFramebuffers(1, tempFBOID));
egdaniel0f5f9672015-02-03 11:10:51 -08002823 }
2824
egdanield803f272015-03-18 13:01:52 -07002825 fStats.incRenderTargetBinds();
2826 GR_GL_CALL(this->glInterface(), BindFramebuffer(fboTarget, *tempFBOID));
2827 GR_GL_CALL(this->glInterface(), FramebufferTexture2D(fboTarget,
robertphillips754f4e92014-09-18 13:52:08 -07002828 GR_GL_COLOR_ATTACHMENT0,
bsalomon10528f12015-10-14 12:54:52 -07002829 target,
robertphillips754f4e92014-09-18 13:52:08 -07002830 texID,
2831 0));
bsalomon@google.comeb851172013-04-15 13:51:00 +00002832 viewport->fLeft = 0;
2833 viewport->fBottom = 0;
2834 viewport->fWidth = surface->width();
2835 viewport->fHeight = surface->height();
2836 } else {
egdanield803f272015-03-18 13:01:52 -07002837 fStats.incRenderTargetBinds();
2838 GR_GL_CALL(this->glInterface(), BindFramebuffer(fboTarget, rt->renderFBOID()));
bsalomon@google.comeb851172013-04-15 13:51:00 +00002839 *viewport = rt->getViewport();
2840 }
egdaniel0f5f9672015-02-03 11:10:51 -08002841}
2842
bsalomon10528f12015-10-14 12:54:52 -07002843void GrGLGpu::unbindTextureFBOForCopy(GrGLenum fboTarget, GrSurface* surface) {
2844 // bindSurfaceFBOForCopy temporarily binds textures that are not render targets to
2845 if (!surface->asRenderTarget()) {
2846 SkASSERT(surface->asTexture());
2847 GrGLenum textureTarget = static_cast<GrGLTexture*>(surface->asTexture())->target();
2848 GR_GL_CALL(this->glInterface(), FramebufferTexture2D(fboTarget,
2849 GR_GL_COLOR_ATTACHMENT0,
2850 textureTarget,
2851 0,
2852 0));
2853 }
bsalomon@google.comeb851172013-04-15 13:51:00 +00002854}
2855
joshualitt1c735482015-07-13 08:08:25 -07002856bool GrGLGpu::initCopySurfaceDstDesc(const GrSurface* src, GrSurfaceDesc* desc) const {
bsalomon6df86402015-06-01 10:41:49 -07002857 // If the src is a texture, we can implement the blit as a draw assuming the config is
2858 // renderable.
2859 if (src->asTexture() && this->caps()->isConfigRenderable(src->config(), false)) {
2860 desc->fOrigin = kDefault_GrSurfaceOrigin;
2861 desc->fFlags = kRenderTarget_GrSurfaceFlag;
2862 desc->fConfig = src->config();
2863 return true;
2864 }
2865
2866 // We look for opportunities to use CopyTexSubImage, or fbo blit. If neither are
bsalomonf90a02b2014-11-26 12:28:00 -08002867 // possible and we return false to fallback to creating a render target dst for render-to-
2868 // texture. This code prefers CopyTexSubImage to fbo blit and avoids triggering temporary fbo
2869 // creation. It isn't clear that avoiding temporary fbo creation is actually optimal.
2870
bsalomon@google.comeb851172013-04-15 13:51:00 +00002871 // Check for format issues with glCopyTexSubImage2D
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +00002872 if (kGLES_GrGLStandard == this->glStandard() && this->glCaps().bgraIsInternalFormat() &&
bsalomon@google.comeb851172013-04-15 13:51:00 +00002873 kBGRA_8888_GrPixelConfig == src->config()) {
bsalomonf90a02b2014-11-26 12:28:00 -08002874 // glCopyTexSubImage2D doesn't work with this config. If the bgra can be used with fbo blit
2875 // then we set up for that, otherwise fail.
2876 if (this->caps()->isConfigRenderable(kBGRA_8888_GrPixelConfig, false)) {
2877 desc->fOrigin = kDefault_GrSurfaceOrigin;
bsalomon6bc1b5f2015-02-23 09:06:38 -08002878 desc->fFlags = kRenderTarget_GrSurfaceFlag;
bsalomonf90a02b2014-11-26 12:28:00 -08002879 desc->fConfig = kBGRA_8888_GrPixelConfig;
2880 return true;
2881 }
2882 return false;
halcanary96fcdcc2015-08-27 07:41:13 -07002883 } else if (nullptr == src->asRenderTarget()) {
bsalomonf90a02b2014-11-26 12:28:00 -08002884 // CopyTexSubImage2D or fbo blit would require creating a temp fbo for the src.
2885 return false;
bsalomon@google.coma2719852013-04-17 14:25:27 +00002886 }
2887
2888 const GrGLRenderTarget* srcRT = static_cast<const GrGLRenderTarget*>(src->asRenderTarget());
egdanield803f272015-03-18 13:01:52 -07002889 if (srcRT && srcRT->renderFBOID() != srcRT->textureFBOID()) {
bsalomonf90a02b2014-11-26 12:28:00 -08002890 // It's illegal to call CopyTexSubImage2D on a MSAA renderbuffer. Set up for FBO blit or
2891 // fail.
2892 if (this->caps()->isConfigRenderable(src->config(), false)) {
2893 desc->fOrigin = kDefault_GrSurfaceOrigin;
bsalomon6bc1b5f2015-02-23 09:06:38 -08002894 desc->fFlags = kRenderTarget_GrSurfaceFlag;
bsalomonf90a02b2014-11-26 12:28:00 -08002895 desc->fConfig = src->config();
2896 return true;
2897 }
2898 return false;
bsalomon@google.comeb851172013-04-15 13:51:00 +00002899 }
bsalomonf90a02b2014-11-26 12:28:00 -08002900
2901 // We'll do a CopyTexSubImage. Make the dst a plain old texture.
2902 desc->fConfig = src->config();
2903 desc->fOrigin = src->origin();
2904 desc->fFlags = kNone_GrSurfaceFlags;
2905 return true;
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002906}
2907
joshualitt1cbdcde2015-08-21 11:53:29 -07002908bool GrGLGpu::onCopySurface(GrSurface* dst,
2909 GrSurface* src,
2910 const SkIRect& srcRect,
2911 const SkIPoint& dstPoint) {
bsalomon6df86402015-06-01 10:41:49 -07002912 if (src->asTexture() && dst->asRenderTarget()) {
2913 this->copySurfaceAsDraw(dst, src, srcRect, dstPoint);
mtklein404b3b22015-05-18 09:29:10 -07002914 return true;
bsalomon5df6fee2015-05-18 06:26:15 -07002915 }
bsalomon6df86402015-06-01 10:41:49 -07002916
2917 if (can_copy_texsubimage(dst, src, this)) {
2918 this->copySurfaceAsCopyTexSubImage(dst, src, srcRect, dstPoint);
2919 return true;
2920 }
2921
mtklein404b3b22015-05-18 09:29:10 -07002922 if (can_blit_framebuffer(dst, src, this)) {
bsalomon6df86402015-06-01 10:41:49 -07002923 return this->copySurfaceAsBlitFramebuffer(dst, src, srcRect, dstPoint);
2924 }
2925
2926 return false;
2927}
2928
2929
bsalomon0315dbc2015-11-20 20:24:31 -08002930void GrGLGpu::createCopyProgram() {
bsalomon27a04872015-11-20 19:34:37 -08002931 const char* version = this->glCaps().glslCaps()->versionDeclString();
bsalomon0315dbc2015-11-20 20:24:31 -08002932
2933 GrGLSLShaderVar aVertex("a_vertex", kVec2f_GrSLType, GrShaderVar::kAttribute_TypeModifier);
2934 GrGLSLShaderVar uTexCoordXform("u_texCoordXform", kVec4f_GrSLType,
bsalomon27a04872015-11-20 19:34:37 -08002935 GrShaderVar::kUniform_TypeModifier);
bsalomon0315dbc2015-11-20 20:24:31 -08002936 GrGLSLShaderVar uPosXform("u_posXform", kVec4f_GrSLType, GrShaderVar::kUniform_TypeModifier);
2937 GrGLSLShaderVar uTexture("u_texture", kSampler2D_GrSLType, GrShaderVar::kUniform_TypeModifier);
2938 GrGLSLShaderVar vTexCoord("v_texCoord", kVec2f_GrSLType, GrShaderVar::kVaryingOut_TypeModifier);
2939 GrGLSLShaderVar oFragColor("o_FragColor", kVec4f_GrSLType, GrShaderVar::kOut_TypeModifier);
bsalomon6df86402015-06-01 10:41:49 -07002940
bsalomon0315dbc2015-11-20 20:24:31 -08002941 SkString vshaderTxt(version);
2942 aVertex.appendDecl(this->glCaps().glslCaps(), &vshaderTxt);
2943 vshaderTxt.append(";");
2944 uTexCoordXform.appendDecl(this->glCaps().glslCaps(), &vshaderTxt);
2945 vshaderTxt.append(";");
2946 uPosXform.appendDecl(this->glCaps().glslCaps(), &vshaderTxt);
2947 vshaderTxt.append(";");
2948 vTexCoord.appendDecl(this->glCaps().glslCaps(), &vshaderTxt);
2949 vshaderTxt.append(";");
bsalomon27a04872015-11-20 19:34:37 -08002950
bsalomon0315dbc2015-11-20 20:24:31 -08002951 vshaderTxt.append(
2952 "// Copy Program VS\n"
2953 "void main() {"
2954 " v_texCoord = a_vertex.xy * u_texCoordXform.xy + u_texCoordXform.zw;"
2955 " gl_Position.xy = a_vertex * u_posXform.xy + u_posXform.zw;"
2956 " gl_Position.zw = vec2(0, 1);"
2957 "}"
2958 );
bsalomon6df86402015-06-01 10:41:49 -07002959
bsalomon0315dbc2015-11-20 20:24:31 -08002960 SkString fshaderTxt(version);
2961 GrGLSLAppendDefaultFloatPrecisionDeclaration(kDefault_GrSLPrecision,
2962 *this->glCaps().glslCaps(),
2963 &fshaderTxt);
2964 vTexCoord.setTypeModifier(GrShaderVar::kVaryingIn_TypeModifier);
2965 vTexCoord.appendDecl(this->glCaps().glslCaps(), &fshaderTxt);
2966 fshaderTxt.append(";");
2967 uTexture.appendDecl(this->glCaps().glslCaps(), &fshaderTxt);
2968 fshaderTxt.append(";");
2969 const char* fsOutName;
2970 if (this->glCaps().glslCaps()->mustDeclareFragmentShaderOutput()) {
2971 oFragColor.appendDecl(this->glCaps().glslCaps(), &fshaderTxt);
bsalomon27a04872015-11-20 19:34:37 -08002972 fshaderTxt.append(";");
bsalomon0315dbc2015-11-20 20:24:31 -08002973 fsOutName = oFragColor.c_str();
2974 } else {
2975 fsOutName = "gl_FragColor";
bsalomon27a04872015-11-20 19:34:37 -08002976 }
bsalomon0315dbc2015-11-20 20:24:31 -08002977 fshaderTxt.appendf(
2978 "// Copy Program FS\n"
2979 "void main() {"
2980 " %s = %s(u_texture, v_texCoord);"
2981 "}",
2982 fsOutName,
2983 GrGLSLTexture2DFunctionName(kVec2f_GrSLType, this->glslGeneration())
2984 );
2985
2986 GL_CALL_RET(fCopyProgram.fProgram, CreateProgram());
2987 const char* str;
2988 GrGLint length;
bsalomon27a04872015-11-20 19:34:37 -08002989
bsalomon0315dbc2015-11-20 20:24:31 -08002990 str = vshaderTxt.c_str();
2991 length = SkToInt(vshaderTxt.size());
2992 GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fCopyProgram.fProgram,
2993 GR_GL_VERTEX_SHADER, &str, &length, 1, &fStats);
2994
2995 str = fshaderTxt.c_str();
2996 length = SkToInt(fshaderTxt.size());
2997 GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fCopyProgram.fProgram,
2998 GR_GL_FRAGMENT_SHADER, &str, &length, 1, &fStats);
2999
3000 GL_CALL(LinkProgram(fCopyProgram.fProgram));
3001
3002 GL_CALL_RET(fCopyProgram.fTextureUniform, GetUniformLocation(fCopyProgram.fProgram,
3003 "u_texture"));
3004 GL_CALL_RET(fCopyProgram.fPosXformUniform, GetUniformLocation(fCopyProgram.fProgram,
3005 "u_posXform"));
3006 GL_CALL_RET(fCopyProgram.fTexCoordXformUniform, GetUniformLocation(fCopyProgram.fProgram,
3007 "u_texCoordXform"));
3008
3009 GL_CALL(BindAttribLocation(fCopyProgram.fProgram, 0, "a_vertex"));
3010
3011 GL_CALL(DeleteShader(vshader));
3012 GL_CALL(DeleteShader(fshader));
3013
3014 GL_CALL(GenBuffers(1, &fCopyProgram.fArrayBuffer));
3015 fHWGeometryState.setVertexBufferID(this, fCopyProgram.fArrayBuffer);
bsalomon6df86402015-06-01 10:41:49 -07003016 static const GrGLfloat vdata[] = {
3017 0, 0,
3018 0, 1,
3019 1, 0,
3020 1, 1
3021 };
3022 GL_ALLOC_CALL(this->glInterface(),
3023 BufferData(GR_GL_ARRAY_BUFFER,
3024 (GrGLsizeiptr) sizeof(vdata),
3025 vdata, // data ptr
3026 GR_GL_STATIC_DRAW));
3027}
3028
3029void GrGLGpu::copySurfaceAsDraw(GrSurface* dst,
3030 GrSurface* src,
3031 const SkIRect& srcRect,
3032 const SkIPoint& dstPoint) {
3033 int w = srcRect.width();
3034 int h = srcRect.height();
3035
3036 GrGLTexture* srcTex = static_cast<GrGLTexture*>(src->asTexture());
3037 GrTextureParams params(SkShader::kClamp_TileMode, GrTextureParams::kNone_FilterMode);
3038 this->bindTexture(0, params, srcTex);
3039
3040 GrGLRenderTarget* dstRT = static_cast<GrGLRenderTarget*>(dst->asRenderTarget());
3041 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY, w, h);
3042 this->flushRenderTarget(dstRT, &dstRect);
3043
bsalomon0315dbc2015-11-20 20:24:31 -08003044 GL_CALL(UseProgram(fCopyProgram.fProgram));
3045 fHWProgramID = fCopyProgram.fProgram;
bsalomon6df86402015-06-01 10:41:49 -07003046
3047 fHWGeometryState.setVertexArrayID(this, 0);
3048
3049 GrGLAttribArrayState* attribs =
bsalomon0315dbc2015-11-20 20:24:31 -08003050 fHWGeometryState.bindArrayAndBufferToDraw(this, fCopyProgram.fArrayBuffer);
3051 attribs->set(this, 0, fCopyProgram.fArrayBuffer, 2, GR_GL_FLOAT, false,
bsalomon6df86402015-06-01 10:41:49 -07003052 2 * sizeof(GrGLfloat), 0);
bsalomond6246342015-06-04 13:57:00 -07003053 attribs->disableUnusedArrays(this, 0x1);
bsalomon6df86402015-06-01 10:41:49 -07003054
3055 // dst rect edges in NDC (-1 to 1)
3056 int dw = dst->width();
3057 int dh = dst->height();
3058 GrGLfloat dx0 = 2.f * dstPoint.fX / dw - 1.f;
3059 GrGLfloat dx1 = 2.f * (dstPoint.fX + w) / dw - 1.f;
3060 GrGLfloat dy0 = 2.f * dstPoint.fY / dh - 1.f;
3061 GrGLfloat dy1 = 2.f * (dstPoint.fY + h) / dh - 1.f;
3062 if (kBottomLeft_GrSurfaceOrigin == dst->origin()) {
3063 dy0 = -dy0;
3064 dy1 = -dy1;
3065 }
3066
3067 // src rect edges in normalized texture space (0 to 1)
3068 int sw = src->width();
3069 int sh = src->height();
3070 GrGLfloat sx0 = (GrGLfloat)srcRect.fLeft / sw;
3071 GrGLfloat sx1 = (GrGLfloat)(srcRect.fLeft + w) / sw;
3072 GrGLfloat sy0 = (GrGLfloat)srcRect.fTop / sh;
3073 GrGLfloat sy1 = (GrGLfloat)(srcRect.fTop + h) / sh;
3074 if (kBottomLeft_GrSurfaceOrigin == src->origin()) {
3075 sy0 = 1.f - sy0;
3076 sy1 = 1.f - sy1;
3077 }
3078
bsalomon0315dbc2015-11-20 20:24:31 -08003079 GL_CALL(Uniform4f(fCopyProgram.fPosXformUniform, dx1 - dx0, dy1 - dy0, dx0, dy0));
3080 GL_CALL(Uniform4f(fCopyProgram.fTexCoordXformUniform, sx1 - sx0, sy1 - sy0, sx0, sy0));
3081 GL_CALL(Uniform1i(fCopyProgram.fTextureUniform, 0));
bsalomon6df86402015-06-01 10:41:49 -07003082
3083 GrXferProcessor::BlendInfo blendInfo;
3084 blendInfo.reset();
3085 this->flushBlend(blendInfo);
3086 this->flushColorWrite(true);
bsalomon6df86402015-06-01 10:41:49 -07003087 this->flushDrawFace(GrPipelineBuilder::kBoth_DrawFace);
cdalton7e806f32015-11-11 15:16:07 -08003088 this->flushHWAAState(dstRT, false);
bsalomon6df86402015-06-01 10:41:49 -07003089 this->disableScissor();
3090 GrStencilSettings stencil;
3091 stencil.setDisabled();
3092 this->flushStencil(stencil);
3093
3094 GL_CALL(DrawArrays(GR_GL_TRIANGLE_STRIP, 0, 4));
3095}
3096
3097void GrGLGpu::copySurfaceAsCopyTexSubImage(GrSurface* dst,
3098 GrSurface* src,
3099 const SkIRect& srcRect,
3100 const SkIPoint& dstPoint) {
3101 SkASSERT(can_copy_texsubimage(dst, src, this));
bsalomon6df86402015-06-01 10:41:49 -07003102 GrGLIRect srcVP;
bsalomon10528f12015-10-14 12:54:52 -07003103 this->bindSurfaceFBOForCopy(src, GR_GL_FRAMEBUFFER, &srcVP, kSrc_TempFBOTarget);
bsalomon6df86402015-06-01 10:41:49 -07003104 GrGLTexture* dstTex = static_cast<GrGLTexture*>(dst->asTexture());
3105 SkASSERT(dstTex);
3106 // We modified the bound FBO
3107 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
3108 GrGLIRect srcGLRect;
3109 srcGLRect.setRelativeTo(srcVP,
3110 srcRect.fLeft,
3111 srcRect.fTop,
3112 srcRect.width(),
3113 srcRect.height(),
3114 src->origin());
3115
3116 this->setScratchTextureUnit();
bsalomon10528f12015-10-14 12:54:52 -07003117 GL_CALL(BindTexture(dstTex->target(), dstTex->textureID()));
bsalomon6df86402015-06-01 10:41:49 -07003118 GrGLint dstY;
3119 if (kBottomLeft_GrSurfaceOrigin == dst->origin()) {
3120 dstY = dst->height() - (dstPoint.fY + srcGLRect.fHeight);
3121 } else {
3122 dstY = dstPoint.fY;
3123 }
bsalomon10528f12015-10-14 12:54:52 -07003124 GL_CALL(CopyTexSubImage2D(dstTex->target(), 0,
bsalomon6df86402015-06-01 10:41:49 -07003125 dstPoint.fX, dstY,
3126 srcGLRect.fLeft, srcGLRect.fBottom,
3127 srcGLRect.fWidth, srcGLRect.fHeight));
bsalomon10528f12015-10-14 12:54:52 -07003128 this->unbindTextureFBOForCopy(GR_GL_FRAMEBUFFER, src);
bsalomon6df86402015-06-01 10:41:49 -07003129}
3130
3131bool GrGLGpu::copySurfaceAsBlitFramebuffer(GrSurface* dst,
3132 GrSurface* src,
3133 const SkIRect& srcRect,
3134 const SkIPoint& dstPoint) {
3135 SkASSERT(can_blit_framebuffer(dst, src, this));
3136 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
3137 srcRect.width(), srcRect.height());
3138 if (dst == src) {
3139 if (SkIRect::IntersectsNoEmptyCheck(dstRect, srcRect)) {
3140 return false;
mtklein404b3b22015-05-18 09:29:10 -07003141 }
bsalomon5df6fee2015-05-18 06:26:15 -07003142 }
bsalomon6df86402015-06-01 10:41:49 -07003143
bsalomon6df86402015-06-01 10:41:49 -07003144 GrGLIRect dstVP;
3145 GrGLIRect srcVP;
bsalomon10528f12015-10-14 12:54:52 -07003146 this->bindSurfaceFBOForCopy(dst, GR_GL_DRAW_FRAMEBUFFER, &dstVP, kDst_TempFBOTarget);
3147 this->bindSurfaceFBOForCopy(src, GR_GL_READ_FRAMEBUFFER, &srcVP, kSrc_TempFBOTarget);
bsalomon6df86402015-06-01 10:41:49 -07003148 // We modified the bound FBO
3149 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
3150 GrGLIRect srcGLRect;
3151 GrGLIRect dstGLRect;
3152 srcGLRect.setRelativeTo(srcVP,
3153 srcRect.fLeft,
3154 srcRect.fTop,
3155 srcRect.width(),
3156 srcRect.height(),
3157 src->origin());
3158 dstGLRect.setRelativeTo(dstVP,
3159 dstRect.fLeft,
3160 dstRect.fTop,
3161 dstRect.width(),
3162 dstRect.height(),
3163 dst->origin());
3164
3165 // BlitFrameBuffer respects the scissor, so disable it.
3166 this->disableScissor();
3167
3168 GrGLint srcY0;
3169 GrGLint srcY1;
3170 // Does the blit need to y-mirror or not?
3171 if (src->origin() == dst->origin()) {
3172 srcY0 = srcGLRect.fBottom;
3173 srcY1 = srcGLRect.fBottom + srcGLRect.fHeight;
3174 } else {
3175 srcY0 = srcGLRect.fBottom + srcGLRect.fHeight;
3176 srcY1 = srcGLRect.fBottom;
3177 }
3178 GL_CALL(BlitFramebuffer(srcGLRect.fLeft,
3179 srcY0,
3180 srcGLRect.fLeft + srcGLRect.fWidth,
3181 srcY1,
3182 dstGLRect.fLeft,
3183 dstGLRect.fBottom,
3184 dstGLRect.fLeft + dstGLRect.fWidth,
3185 dstGLRect.fBottom + dstGLRect.fHeight,
3186 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
bsalomon10528f12015-10-14 12:54:52 -07003187 this->unbindTextureFBOForCopy(GR_GL_DRAW_FRAMEBUFFER, dst);
3188 this->unbindTextureFBOForCopy(GR_GL_READ_FRAMEBUFFER, src);
bsalomon6df86402015-06-01 10:41:49 -07003189 return true;
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00003190}
3191
cdalton231c5fd2015-05-13 12:35:36 -07003192void GrGLGpu::xferBarrier(GrRenderTarget* rt, GrXferBarrierType type) {
bsalomoncb02b382015-08-12 11:14:50 -07003193 SkASSERT(type);
cdalton9954bc32015-04-29 14:17:00 -07003194 switch (type) {
cdalton231c5fd2015-05-13 12:35:36 -07003195 case kTexture_GrXferBarrierType: {
3196 GrGLRenderTarget* glrt = static_cast<GrGLRenderTarget*>(rt);
3197 if (glrt->textureFBOID() != glrt->renderFBOID()) {
3198 // The render target uses separate storage so no need for glTextureBarrier.
3199 // FIXME: The render target will resolve automatically when its texture is bound,
3200 // but we could resolve only the bounds that will be read if we do it here instead.
3201 return;
3202 }
cdalton9954bc32015-04-29 14:17:00 -07003203 SkASSERT(this->caps()->textureBarrierSupport());
3204 GL_CALL(TextureBarrier());
3205 return;
cdalton231c5fd2015-05-13 12:35:36 -07003206 }
cdalton8917d622015-05-06 13:40:21 -07003207 case kBlend_GrXferBarrierType:
bsalomon4b91f762015-05-19 09:29:46 -07003208 SkASSERT(GrCaps::kAdvanced_BlendEquationSupport ==
cdalton8917d622015-05-06 13:40:21 -07003209 this->caps()->blendEquationSupport());
3210 GL_CALL(BlendBarrier());
3211 return;
bsalomoncb02b382015-08-12 11:14:50 -07003212 default: break; // placate compiler warnings that kNone not handled
cdalton9954bc32015-04-29 14:17:00 -07003213 }
3214}
3215
jvanverth88957922015-07-14 11:02:52 -07003216GrBackendObject GrGLGpu::createTestingOnlyBackendTexture(void* pixels, int w, int h,
bsalomon091f60c2015-11-10 11:54:56 -08003217 GrPixelConfig config) const {
3218 GrGLTextureInfo* info = new GrGLTextureInfo;
3219 info->fTarget = GR_GL_TEXTURE_2D;
3220 GL_CALL(GenTextures(1, &info->fID));
jvanverth672bb7f2015-07-13 07:19:57 -07003221 GL_CALL(ActiveTexture(GR_GL_TEXTURE0));
3222 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, 1));
bsalomon091f60c2015-11-10 11:54:56 -08003223 GL_CALL(BindTexture(info->fTarget, info->fID));
3224 GL_CALL(TexParameteri(info->fTarget, GR_GL_TEXTURE_MAG_FILTER, GR_GL_NEAREST));
3225 GL_CALL(TexParameteri(info->fTarget, GR_GL_TEXTURE_MIN_FILTER, GR_GL_NEAREST));
3226 GL_CALL(TexParameteri(info->fTarget, GR_GL_TEXTURE_WRAP_S, GR_GL_CLAMP_TO_EDGE));
3227 GL_CALL(TexParameteri(info->fTarget, GR_GL_TEXTURE_WRAP_T, GR_GL_CLAMP_TO_EDGE));
jvanverth672bb7f2015-07-13 07:19:57 -07003228
3229 GrGLenum internalFormat = 0x0; // suppress warning
3230 GrGLenum externalFormat = 0x0; // suppress warning
3231 GrGLenum externalType = 0x0; // suppress warning
3232
3233 this->configToGLFormats(config, false, &internalFormat, &externalFormat, &externalType);
3234
bsalomon091f60c2015-11-10 11:54:56 -08003235 GL_CALL(TexImage2D(info->fTarget, 0, internalFormat, w, h, 0, externalFormat,
jvanverth672bb7f2015-07-13 07:19:57 -07003236 externalType, pixels));
3237
bsalomon091f60c2015-11-10 11:54:56 -08003238#ifdef SK_IGNORE_GL_TEXTURE_TARGET
3239 GrGLuint id = info->fID;
3240 delete info;
3241 return id;
3242#else
3243 return reinterpret_cast<GrBackendObject>(info);
3244#endif
jvanverth672bb7f2015-07-13 07:19:57 -07003245}
3246
jvanverth88957922015-07-14 11:02:52 -07003247bool GrGLGpu::isTestingOnlyBackendTexture(GrBackendObject id) const {
bsalomon091f60c2015-11-10 11:54:56 -08003248#ifdef SK_IGNORE_GL_TEXTURE_TARGET
jvanverth672bb7f2015-07-13 07:19:57 -07003249 GrGLuint texID = (GrGLuint)id;
bsalomon091f60c2015-11-10 11:54:56 -08003250#else
3251 GrGLuint texID = reinterpret_cast<const GrGLTextureInfo*>(id)->fID;
3252#endif
jvanverth672bb7f2015-07-13 07:19:57 -07003253
3254 GrGLboolean result;
3255 GL_CALL_RET(result, IsTexture(texID));
3256
3257 return (GR_GL_TRUE == result);
3258}
3259
bsalomon67d76202015-11-11 12:40:42 -08003260void GrGLGpu::deleteTestingOnlyBackendTexture(GrBackendObject id, bool abandonTexture) const {
bsalomon091f60c2015-11-10 11:54:56 -08003261#ifdef SK_IGNORE_GL_TEXTURE_TARGET
jvanverth672bb7f2015-07-13 07:19:57 -07003262 GrGLuint texID = (GrGLuint)id;
bsalomon091f60c2015-11-10 11:54:56 -08003263#else
3264 const GrGLTextureInfo* info = reinterpret_cast<const GrGLTextureInfo*>(id);
3265 GrGLuint texID = info->fID;
3266#endif
3267
bsalomon67d76202015-11-11 12:40:42 -08003268 if (!abandonTexture) {
3269 GL_CALL(DeleteTextures(1, &texID));
3270 }
bsalomon091f60c2015-11-10 11:54:56 -08003271
3272#ifndef SK_IGNORE_GL_TEXTURE_TARGET
3273 delete info;
3274#endif
jvanverth672bb7f2015-07-13 07:19:57 -07003275}
3276
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00003277///////////////////////////////////////////////////////////////////////////////
bsalomon861e1032014-12-16 07:33:49 -08003278GrGLAttribArrayState* GrGLGpu::HWGeometryState::bindArrayAndBuffersToDraw(
3279 GrGLGpu* gpu,
bsalomon@google.com6918d482013-03-07 19:09:11 +00003280 const GrGLVertexBuffer* vbuffer,
3281 const GrGLIndexBuffer* ibuffer) {
bsalomon49f085d2014-09-05 13:34:00 -07003282 SkASSERT(vbuffer);
bsalomon6df86402015-06-01 10:41:49 -07003283 GrGLuint vbufferID = vbuffer->bufferID();
halcanary96fcdcc2015-08-27 07:41:13 -07003284 GrGLuint* ibufferIDPtr = nullptr;
bsalomon6df86402015-06-01 10:41:49 -07003285 GrGLuint ibufferID;
3286 if (ibuffer) {
3287 ibufferID = ibuffer->bufferID();
3288 ibufferIDPtr = &ibufferID;
3289 }
3290 return this->internalBind(gpu, vbufferID, ibufferIDPtr);
3291}
3292
3293GrGLAttribArrayState* GrGLGpu::HWGeometryState::bindArrayAndBufferToDraw(GrGLGpu* gpu,
3294 GrGLuint vbufferID) {
halcanary96fcdcc2015-08-27 07:41:13 -07003295 return this->internalBind(gpu, vbufferID, nullptr);
bsalomon6df86402015-06-01 10:41:49 -07003296}
3297
3298GrGLAttribArrayState* GrGLGpu::HWGeometryState::bindArrayAndBuffersToDraw(GrGLGpu* gpu,
3299 GrGLuint vbufferID,
3300 GrGLuint ibufferID) {
3301 return this->internalBind(gpu, vbufferID, &ibufferID);
3302}
3303
3304GrGLAttribArrayState* GrGLGpu::HWGeometryState::internalBind(GrGLGpu* gpu,
3305 GrGLuint vbufferID,
3306 GrGLuint* ibufferID) {
robertphillips@google.com4f65a272013-03-26 19:40:46 +00003307 GrGLAttribArrayState* attribState;
3308
bsalomon6df86402015-06-01 10:41:49 -07003309 if (gpu->glCaps().isCoreProfile() && 0 != vbufferID) {
bsalomon8780bc62015-05-13 09:56:37 -07003310 if (!fVBOVertexArray) {
bsalomon@google.com6918d482013-03-07 19:09:11 +00003311 GrGLuint arrayID;
3312 GR_GL_CALL(gpu->glInterface(), GenVertexArrays(1, &arrayID));
3313 int attrCount = gpu->glCaps().maxVertexAttributes();
halcanary385fe4d2015-08-26 13:07:48 -07003314 fVBOVertexArray = new GrGLVertexArray(arrayID, attrCount);
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00003315 }
bsalomon6df86402015-06-01 10:41:49 -07003316 if (ibufferID) {
3317 attribState = fVBOVertexArray->bindWithIndexBuffer(gpu, *ibufferID);
3318 } else {
3319 attribState = fVBOVertexArray->bind(gpu);
3320 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00003321 } else {
bsalomon6df86402015-06-01 10:41:49 -07003322 if (ibufferID) {
3323 this->setIndexBufferIDOnDefaultVertexArray(gpu, *ibufferID);
bsalomon@google.com6918d482013-03-07 19:09:11 +00003324 } else {
3325 this->setVertexArrayID(gpu, 0);
3326 }
3327 int attrCount = gpu->glCaps().maxVertexAttributes();
3328 if (fDefaultVertexArrayAttribState.count() != attrCount) {
3329 fDefaultVertexArrayAttribState.resize(attrCount);
3330 }
3331 attribState = &fDefaultVertexArrayAttribState;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00003332 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00003333 return attribState;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00003334}