blob: 8bf6389fb616ac2318966d6666247ea1423ee735 [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
jvanverth39edf762014-12-22 11:44:19 -08008#include "GrGLGpu.h"
Hal Canary95e3c052017-01-11 12:44:43 -05009
Brian Salomon028a9a52017-05-11 11:39:08 -040010#include <cmath>
Hal Canary95e3c052017-01-11 12:44:43 -050011#include "../private/GrGLSL.h"
Greg Daniela5cb7812017-06-16 09:45:32 -040012#include "GrBackendSemaphore.h"
Greg Daniel7ef28f32017-04-20 16:41:55 +000013#include "GrBackendSurface.h"
Hal Canary95e3c052017-01-11 12:44:43 -050014#include "GrFixedClip.h"
cdalton397536c2016-03-25 12:15:03 -070015#include "GrGLBuffer.h"
egdaniel066df7c2016-06-08 14:02:27 -070016#include "GrGLGpuCommandBuffer.h"
Greg Daniel6be35232017-03-01 17:01:09 -050017#include "GrGLSemaphore.h"
egdaniel8dc7c3a2015-04-16 11:22:42 -070018#include "GrGLStencilAttachment.h"
bsalomon37dd3312014-11-03 08:47:23 -080019#include "GrGLTextureRenderTarget.h"
bsalomon3582d3e2015-02-13 14:20:05 -080020#include "GrGpuResourcePriv.h"
egdaniel0e1853c2016-03-17 11:35:45 -070021#include "GrMesh.h"
Hal Canary95e3c052017-01-11 12:44:43 -050022#include "GrPipeline.h"
bsalomon6bc1b5f2015-02-23 09:06:38 -080023#include "GrRenderTargetPriv.h"
Brian Salomon94efbf52016-11-29 13:43:05 -050024#include "GrShaderCaps.h"
Robert Phillips3798c862017-03-27 11:08:16 -040025#include "GrSurfaceProxyPriv.h"
bsalomonafbf2d62014-09-30 12:18:44 -070026#include "GrTexturePriv.h"
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +000027#include "GrTypes.h"
Hal Canary95e3c052017-01-11 12:44:43 -050028#include "SkAutoMalloc.h"
Stan Iliev0078ab22017-11-08 14:16:26 -050029#include "SkHalf.h"
Brian Osman71a18892017-08-10 10:23:25 -040030#include "SkJSONWriter.h"
Kevin Lubickc456b732017-01-11 17:21:57 +000031#include "SkMakeUnique.h"
32#include "SkMipMap.h"
33#include "SkPixmap.h"
Kevin Lubickc456b732017-01-11 17:21:57 +000034#include "SkSLCompiler.h"
Hal Canary95e3c052017-01-11 12:44:43 -050035#include "SkStrokeRec.h"
Kevin Lubickc456b732017-01-11 17:21:57 +000036#include "SkTemplates.h"
Ryan Macnak38a10ad2017-07-10 10:36:34 -070037#include "SkTraceEvent.h"
Kevin Lubickc456b732017-01-11 17:21:57 +000038#include "SkTypes.h"
Hal Canary95e3c052017-01-11 12:44:43 -050039#include "builders/GrGLShaderStringBuilder.h"
Hal Canary95e3c052017-01-11 12:44:43 -050040#include "instanced/GLInstancedRendering.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000041
bsalomon@google.com0b77d682011-08-19 13:28:54 +000042#define GL_CALL(X) GR_GL_CALL(this->glInterface(), X)
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +000043#define GL_CALL_RET(RET, X) GR_GL_CALL_RET(this->glInterface(), RET, X)
bsalomon@google.com0b77d682011-08-19 13:28:54 +000044
reed@google.comac10a2d2010-12-22 21:39:39 +000045#define SKIP_CACHE_CHECK true
46
bsalomon@google.com4f3c2532012-01-19 16:16:52 +000047#if GR_GL_CHECK_ALLOC_WITH_GET_ERROR
48 #define CLEAR_ERROR_BEFORE_ALLOC(iface) GrGLClearErr(iface)
49 #define GL_ALLOC_CALL(iface, call) GR_GL_CALL_NOERRCHECK(iface, call)
50 #define CHECK_ALLOC_ERROR(iface) GR_GL_GET_ERROR(iface)
rmistry@google.comfbfcd562012-08-23 18:09:54 +000051#else
bsalomon@google.com4f3c2532012-01-19 16:16:52 +000052 #define CLEAR_ERROR_BEFORE_ALLOC(iface)
53 #define GL_ALLOC_CALL(iface, call) GR_GL_CALL(iface, call)
54 #define CHECK_ALLOC_ERROR(iface) GR_GL_NO_ERROR
55#endif
56
Jim Van Verth32ac83e2016-11-28 15:23:57 -050057//#define USE_NSIGHT
58
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +000059///////////////////////////////////////////////////////////////////////////////
60
csmartdaltona7f29642016-07-07 08:49:11 -070061using gr_instanced::InstancedRendering;
62using gr_instanced::GLInstancedRendering;
cdaltonb85a0aa2014-07-21 15:32:44 -070063
Robert Phillipse3302df2017-04-24 07:31:02 -040064using gr_instanced::OpAllocator;
65using gr_instanced::GLOpAllocator;
66
cdalton8917d622015-05-06 13:40:21 -070067static const GrGLenum gXfermodeEquation2Blend[] = {
68 // Basic OpenGL blend equations.
69 GR_GL_FUNC_ADD,
70 GR_GL_FUNC_SUBTRACT,
71 GR_GL_FUNC_REVERSE_SUBTRACT,
72
73 // GL_KHR_blend_equation_advanced.
74 GR_GL_SCREEN,
75 GR_GL_OVERLAY,
76 GR_GL_DARKEN,
77 GR_GL_LIGHTEN,
78 GR_GL_COLORDODGE,
79 GR_GL_COLORBURN,
80 GR_GL_HARDLIGHT,
81 GR_GL_SOFTLIGHT,
82 GR_GL_DIFFERENCE,
83 GR_GL_EXCLUSION,
84 GR_GL_MULTIPLY,
85 GR_GL_HSL_HUE,
86 GR_GL_HSL_SATURATION,
87 GR_GL_HSL_COLOR,
88 GR_GL_HSL_LUMINOSITY
89};
90GR_STATIC_ASSERT(0 == kAdd_GrBlendEquation);
91GR_STATIC_ASSERT(1 == kSubtract_GrBlendEquation);
92GR_STATIC_ASSERT(2 == kReverseSubtract_GrBlendEquation);
93GR_STATIC_ASSERT(3 == kScreen_GrBlendEquation);
94GR_STATIC_ASSERT(4 == kOverlay_GrBlendEquation);
95GR_STATIC_ASSERT(5 == kDarken_GrBlendEquation);
96GR_STATIC_ASSERT(6 == kLighten_GrBlendEquation);
97GR_STATIC_ASSERT(7 == kColorDodge_GrBlendEquation);
98GR_STATIC_ASSERT(8 == kColorBurn_GrBlendEquation);
99GR_STATIC_ASSERT(9 == kHardLight_GrBlendEquation);
100GR_STATIC_ASSERT(10 == kSoftLight_GrBlendEquation);
101GR_STATIC_ASSERT(11 == kDifference_GrBlendEquation);
102GR_STATIC_ASSERT(12 == kExclusion_GrBlendEquation);
103GR_STATIC_ASSERT(13 == kMultiply_GrBlendEquation);
104GR_STATIC_ASSERT(14 == kHSLHue_GrBlendEquation);
105GR_STATIC_ASSERT(15 == kHSLSaturation_GrBlendEquation);
106GR_STATIC_ASSERT(16 == kHSLColor_GrBlendEquation);
107GR_STATIC_ASSERT(17 == kHSLLuminosity_GrBlendEquation);
bsalomonf7cc8772015-05-11 11:21:14 -0700108GR_STATIC_ASSERT(SK_ARRAY_COUNT(gXfermodeEquation2Blend) == kGrBlendEquationCnt);
cdalton8917d622015-05-06 13:40:21 -0700109
twiz@google.com0f31ca72011-03-18 17:38:11 +0000110static const GrGLenum gXfermodeCoeff2Blend[] = {
111 GR_GL_ZERO,
112 GR_GL_ONE,
113 GR_GL_SRC_COLOR,
114 GR_GL_ONE_MINUS_SRC_COLOR,
115 GR_GL_DST_COLOR,
116 GR_GL_ONE_MINUS_DST_COLOR,
117 GR_GL_SRC_ALPHA,
118 GR_GL_ONE_MINUS_SRC_ALPHA,
119 GR_GL_DST_ALPHA,
120 GR_GL_ONE_MINUS_DST_ALPHA,
121 GR_GL_CONSTANT_COLOR,
122 GR_GL_ONE_MINUS_CONSTANT_COLOR,
123 GR_GL_CONSTANT_ALPHA,
124 GR_GL_ONE_MINUS_CONSTANT_ALPHA,
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000125
126 // extended blend coeffs
127 GR_GL_SRC1_COLOR,
128 GR_GL_ONE_MINUS_SRC1_COLOR,
129 GR_GL_SRC1_ALPHA,
130 GR_GL_ONE_MINUS_SRC1_ALPHA,
reed@google.comac10a2d2010-12-22 21:39:39 +0000131};
132
bsalomon861e1032014-12-16 07:33:49 -0800133bool GrGLGpu::BlendCoeffReferencesConstant(GrBlendCoeff coeff) {
bsalomon@google.com080773c2011-03-15 19:09:25 +0000134 static const bool gCoeffReferencesBlendConst[] = {
135 false,
136 false,
137 false,
138 false,
139 false,
140 false,
141 false,
142 false,
143 false,
144 false,
145 true,
146 true,
147 true,
148 true,
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000149
150 // extended blend coeffs
151 false,
152 false,
153 false,
154 false,
bsalomon@google.com080773c2011-03-15 19:09:25 +0000155 };
156 return gCoeffReferencesBlendConst[coeff];
bsalomonf7cc8772015-05-11 11:21:14 -0700157 GR_STATIC_ASSERT(kGrBlendCoeffCnt == SK_ARRAY_COUNT(gCoeffReferencesBlendConst));
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000158
bsalomon@google.com47059542012-06-06 20:51:20 +0000159 GR_STATIC_ASSERT(0 == kZero_GrBlendCoeff);
160 GR_STATIC_ASSERT(1 == kOne_GrBlendCoeff);
161 GR_STATIC_ASSERT(2 == kSC_GrBlendCoeff);
162 GR_STATIC_ASSERT(3 == kISC_GrBlendCoeff);
163 GR_STATIC_ASSERT(4 == kDC_GrBlendCoeff);
164 GR_STATIC_ASSERT(5 == kIDC_GrBlendCoeff);
165 GR_STATIC_ASSERT(6 == kSA_GrBlendCoeff);
166 GR_STATIC_ASSERT(7 == kISA_GrBlendCoeff);
167 GR_STATIC_ASSERT(8 == kDA_GrBlendCoeff);
168 GR_STATIC_ASSERT(9 == kIDA_GrBlendCoeff);
169 GR_STATIC_ASSERT(10 == kConstC_GrBlendCoeff);
170 GR_STATIC_ASSERT(11 == kIConstC_GrBlendCoeff);
171 GR_STATIC_ASSERT(12 == kConstA_GrBlendCoeff);
172 GR_STATIC_ASSERT(13 == kIConstA_GrBlendCoeff);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000173
bsalomon@google.com47059542012-06-06 20:51:20 +0000174 GR_STATIC_ASSERT(14 == kS2C_GrBlendCoeff);
175 GR_STATIC_ASSERT(15 == kIS2C_GrBlendCoeff);
176 GR_STATIC_ASSERT(16 == kS2A_GrBlendCoeff);
177 GR_STATIC_ASSERT(17 == kIS2A_GrBlendCoeff);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000178
179 // assertion for gXfermodeCoeff2Blend have to be in GrGpu scope
bsalomonf7cc8772015-05-11 11:21:14 -0700180 GR_STATIC_ASSERT(kGrBlendCoeffCnt == SK_ARRAY_COUNT(gXfermodeCoeff2Blend));
bsalomon@google.com080773c2011-03-15 19:09:25 +0000181}
182
reed@google.comac10a2d2010-12-22 21:39:39 +0000183///////////////////////////////////////////////////////////////////////////////
184
egdanielff1d5472015-09-10 08:37:20 -0700185
bsalomon682c2692015-05-22 14:01:46 -0700186GrGpu* GrGLGpu::Create(GrBackendContext backendContext, const GrContextOptions& options,
187 GrContext* context) {
Greg Daniel02611d92017-07-25 10:05:01 -0400188 return Create(reinterpret_cast<const GrGLInterface*>(backendContext), options, context);
189}
190
191GrGpu* GrGLGpu::Create(const GrGLInterface* interface, const GrContextOptions& options,
192 GrContext* context) {
193 sk_sp<const GrGLInterface> glInterface(interface);
bsalomon424cc262015-05-22 10:37:30 -0700194 if (!glInterface) {
195 glInterface.reset(GrGLDefaultInterface());
196 } else {
197 glInterface->ref();
198 }
199 if (!glInterface) {
halcanary96fcdcc2015-08-27 07:41:13 -0700200 return nullptr;
bsalomon424cc262015-05-22 10:37:30 -0700201 }
Jim Van Verth76334772017-05-05 16:46:05 -0400202#ifdef USE_NSIGHT
203 const_cast<GrContextOptions&>(options).fSuppressPathRendering = true;
204#endif
Hal Canary144caf52016-11-07 17:57:18 -0500205 GrGLContext* glContext = GrGLContext::Create(glInterface.get(), options);
bsalomon424cc262015-05-22 10:37:30 -0700206 if (glContext) {
halcanary385fe4d2015-08-26 13:07:48 -0700207 return new GrGLGpu(glContext, context);
bsalomon424cc262015-05-22 10:37:30 -0700208 }
halcanary96fcdcc2015-08-27 07:41:13 -0700209 return nullptr;
bsalomon424cc262015-05-22 10:37:30 -0700210}
211
bsalomon424cc262015-05-22 10:37:30 -0700212GrGLGpu::GrGLGpu(GrGLContext* ctx, GrContext* context)
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000213 : GrGpu(context)
cdaltone2e71c22016-04-07 18:13:29 -0700214 , fGLContext(ctx)
215 , fProgramCache(new ProgramCache(this))
216 , fHWProgramID(0)
217 , fTempSrcFBOID(0)
218 , fTempDstFBOID(0)
219 , fStencilClearFBOID(0)
cdalton74b8d322016-04-11 14:47:28 -0700220 , fHWMaxUsedBufferTextureUnit(-1)
cdaltone2e71c22016-04-07 18:13:29 -0700221 , fHWMinSampleShading(0.0) {
bsalomon424cc262015-05-22 10:37:30 -0700222 SkASSERT(ctx);
223 fCaps.reset(SkRef(ctx->caps()));
bsalomon@google.combcce8922013-03-25 15:38:39 +0000224
Brian Salomon1edc5b92016-11-29 13:43:46 -0500225 fHWBoundTextureUniqueIDs.reset(this->caps()->shaderCaps()->maxCombinedSamplers());
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +0000226
cdaltone2e71c22016-04-07 18:13:29 -0700227 fHWBufferState[kVertex_GrBufferType].fGLTarget = GR_GL_ARRAY_BUFFER;
228 fHWBufferState[kIndex_GrBufferType].fGLTarget = GR_GL_ELEMENT_ARRAY_BUFFER;
229 fHWBufferState[kTexel_GrBufferType].fGLTarget = GR_GL_TEXTURE_BUFFER;
230 fHWBufferState[kDrawIndirect_GrBufferType].fGLTarget = GR_GL_DRAW_INDIRECT_BUFFER;
231 if (GrGLCaps::kChromium_TransferBufferType == this->glCaps().transferBufferType()) {
232 fHWBufferState[kXferCpuToGpu_GrBufferType].fGLTarget =
233 GR_GL_PIXEL_UNPACK_TRANSFER_BUFFER_CHROMIUM;
234 fHWBufferState[kXferGpuToCpu_GrBufferType].fGLTarget =
235 GR_GL_PIXEL_PACK_TRANSFER_BUFFER_CHROMIUM;
236 } else {
237 fHWBufferState[kXferCpuToGpu_GrBufferType].fGLTarget = GR_GL_PIXEL_UNPACK_BUFFER;
238 fHWBufferState[kXferGpuToCpu_GrBufferType].fGLTarget = GR_GL_PIXEL_PACK_BUFFER;
239 }
Rob Phillipsbc534f62017-09-05 19:56:19 -0400240 for (int i = 0; i < kGrBufferTypeCount; ++i) {
241 fHWBufferState[i].invalidate();
242 }
cdaltone2e71c22016-04-07 18:13:29 -0700243 GR_STATIC_ASSERT(6 == SK_ARRAY_COUNT(fHWBufferState));
244
cdalton74b8d322016-04-11 14:47:28 -0700245 if (this->caps()->shaderCaps()->texelBufferSupport()) {
Brian Salomon1edc5b92016-11-29 13:43:46 -0500246 fHWBufferTextures.reset(this->caps()->shaderCaps()->maxCombinedSamplers());
cdalton74b8d322016-04-11 14:47:28 -0700247 }
248
cdaltone2e71c22016-04-07 18:13:29 -0700249 if (this->glCaps().shaderCaps()->pathRenderingSupport()) {
250 fPathRendering.reset(new GrGLPathRendering(this));
251 }
252
bsalomon424cc262015-05-22 10:37:30 -0700253 GrGLClearErr(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +0000254}
255
bsalomon861e1032014-12-16 07:33:49 -0800256GrGLGpu::~GrGLGpu() {
cdaltone2e71c22016-04-07 18:13:29 -0700257 // Ensure any GrGpuResource objects get deleted first, since they may require a working GrGLGpu
258 // to release the resources held by the objects themselves.
kkinnunen702501d2016-01-13 23:36:45 -0800259 fPathRendering.reset();
cdaltone2e71c22016-04-07 18:13:29 -0700260 fCopyProgramArrayBuffer.reset();
brianosman33f6b3f2016-06-02 05:49:21 -0700261 fMipmapProgramArrayBuffer.reset();
Mike Klein31550db2017-06-06 23:29:53 +0000262 fStencilClipClearArrayBuffer.reset();
kkinnunen702501d2016-01-13 23:36:45 -0800263
Brian Salomon43f8bf02017-10-18 08:33:29 -0400264 if (fHWProgramID) {
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000265 // detach the current program so there is no confusion on OpenGL's part
266 // that we want it to be deleted
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000267 GL_CALL(UseProgram(0));
268 }
269
Brian Salomon43f8bf02017-10-18 08:33:29 -0400270 if (fTempSrcFBOID) {
egdanield803f272015-03-18 13:01:52 -0700271 GL_CALL(DeleteFramebuffers(1, &fTempSrcFBOID));
egdaniel0f5f9672015-02-03 11:10:51 -0800272 }
Brian Salomon43f8bf02017-10-18 08:33:29 -0400273 if (fTempDstFBOID) {
egdanield803f272015-03-18 13:01:52 -0700274 GL_CALL(DeleteFramebuffers(1, &fTempDstFBOID));
egdaniel0f5f9672015-02-03 11:10:51 -0800275 }
Brian Salomon43f8bf02017-10-18 08:33:29 -0400276 if (fStencilClearFBOID) {
egdanield803f272015-03-18 13:01:52 -0700277 GL_CALL(DeleteFramebuffers(1, &fStencilClearFBOID));
bsalomondd3143b2015-02-23 09:27:45 -0800278 }
egdaniel0f5f9672015-02-03 11:10:51 -0800279
bsalomon7ea33f52015-11-22 14:51:00 -0800280 for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) {
281 if (0 != fCopyPrograms[i].fProgram) {
282 GL_CALL(DeleteProgram(fCopyPrograms[i].fProgram));
283 }
bsalomon6df86402015-06-01 10:41:49 -0700284 }
bsalomon6dea83f2015-12-03 12:58:06 -0800285
brianosman33f6b3f2016-06-02 05:49:21 -0700286 for (size_t i = 0; i < SK_ARRAY_COUNT(fMipmapPrograms); ++i) {
287 if (0 != fMipmapPrograms[i].fProgram) {
288 GL_CALL(DeleteProgram(fMipmapPrograms[i].fProgram));
289 }
290 }
291
Brian Salomon43f8bf02017-10-18 08:33:29 -0400292 if (fStencilClipClearProgram) {
Mike Klein31550db2017-06-06 23:29:53 +0000293 GL_CALL(DeleteProgram(fStencilClipClearProgram));
bsalomon6dea83f2015-12-03 12:58:06 -0800294 }
295
Brian Salomon43f8bf02017-10-18 08:33:29 -0400296 if (fClearColorProgram.fProgram) {
297 GL_CALL(DeleteProgram(fClearColorProgram.fProgram));
298 }
299
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000300 delete fProgramCache;
bsalomonc8dc1f72014-08-21 13:02:13 -0700301}
302
bsalomon6e2aad42016-04-01 11:54:31 -0700303void GrGLGpu::disconnect(DisconnectType type) {
304 INHERITED::disconnect(type);
305 if (DisconnectType::kCleanup == type) {
306 if (fHWProgramID) {
307 GL_CALL(UseProgram(0));
308 }
309 if (fTempSrcFBOID) {
310 GL_CALL(DeleteFramebuffers(1, &fTempSrcFBOID));
311 }
312 if (fTempDstFBOID) {
313 GL_CALL(DeleteFramebuffers(1, &fTempDstFBOID));
314 }
315 if (fStencilClearFBOID) {
316 GL_CALL(DeleteFramebuffers(1, &fStencilClearFBOID));
317 }
bsalomon6e2aad42016-04-01 11:54:31 -0700318 for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) {
319 if (fCopyPrograms[i].fProgram) {
320 GL_CALL(DeleteProgram(fCopyPrograms[i].fProgram));
321 }
322 }
brianosman33f6b3f2016-06-02 05:49:21 -0700323 for (size_t i = 0; i < SK_ARRAY_COUNT(fMipmapPrograms); ++i) {
324 if (fMipmapPrograms[i].fProgram) {
325 GL_CALL(DeleteProgram(fMipmapPrograms[i].fProgram));
326 }
327 }
Mike Klein31550db2017-06-06 23:29:53 +0000328 if (fStencilClipClearProgram) {
329 GL_CALL(DeleteProgram(fStencilClipClearProgram));
bsalomon6e2aad42016-04-01 11:54:31 -0700330 }
Brian Salomon43f8bf02017-10-18 08:33:29 -0400331
332 if (fClearColorProgram.fProgram) {
333 GL_CALL(DeleteProgram(fClearColorProgram.fProgram));
334 }
bsalomon6e2aad42016-04-01 11:54:31 -0700335 } else {
336 if (fProgramCache) {
337 fProgramCache->abandon();
338 }
339 }
340
341 delete fProgramCache;
342 fProgramCache = nullptr;
343
bsalomonc8dc1f72014-08-21 13:02:13 -0700344 fHWProgramID = 0;
egdanield803f272015-03-18 13:01:52 -0700345 fTempSrcFBOID = 0;
346 fTempDstFBOID = 0;
347 fStencilClearFBOID = 0;
cdaltone2e71c22016-04-07 18:13:29 -0700348 fCopyProgramArrayBuffer.reset();
bsalomon7ea33f52015-11-22 14:51:00 -0800349 for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) {
350 fCopyPrograms[i].fProgram = 0;
351 }
brianosman33f6b3f2016-06-02 05:49:21 -0700352 fMipmapProgramArrayBuffer.reset();
353 for (size_t i = 0; i < SK_ARRAY_COUNT(fMipmapPrograms); ++i) {
354 fMipmapPrograms[i].fProgram = 0;
355 }
Mike Klein31550db2017-06-06 23:29:53 +0000356 fStencilClipClearProgram = 0;
357 fStencilClipClearArrayBuffer.reset();
Brian Salomon43f8bf02017-10-18 08:33:29 -0400358 fClearColorProgram.fProgram = 0;
359
jvanverthe9c0fc62015-04-29 11:18:05 -0700360 if (this->glCaps().shaderCaps()->pathRenderingSupport()) {
bsalomon6e2aad42016-04-01 11:54:31 -0700361 this->glPathRendering()->disconnect(type);
bsalomonc8dc1f72014-08-21 13:02:13 -0700362 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000363}
364
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000365///////////////////////////////////////////////////////////////////////////////
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000366
bsalomon861e1032014-12-16 07:33:49 -0800367void GrGLGpu::onResetContext(uint32_t resetBits) {
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000368 if (resetBits & kMisc_GrGLBackendState) {
Brian Salomonf0861672017-05-08 11:10:10 -0400369 // we don't use the zb at all
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000370 GL_CALL(Disable(GR_GL_DEPTH_TEST));
371 GL_CALL(DepthMask(GR_GL_FALSE));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000372
Brian Salomonf0861672017-05-08 11:10:10 -0400373 // We don't use face culling.
374 GL_CALL(Disable(GR_GL_CULL_FACE));
375 // We do use separate stencil. Our algorithms don't care which face is front vs. back so
376 // just set this to the default for self-consistency.
377 GL_CALL(FrontFace(GR_GL_CCW));
378
cdaltone2e71c22016-04-07 18:13:29 -0700379 fHWBufferState[kTexel_GrBufferType].invalidate();
380 fHWBufferState[kDrawIndirect_GrBufferType].invalidate();
381 fHWBufferState[kXferCpuToGpu_GrBufferType].invalidate();
382 fHWBufferState[kXferGpuToCpu_GrBufferType].invalidate();
cdaltonc1613102016-03-16 07:48:20 -0700383
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +0000384 if (kGL_GrGLStandard == this->glStandard()) {
Jim Van Verth32ac83e2016-11-28 15:23:57 -0500385#ifndef USE_NSIGHT
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000386 // Desktop-only state that we never change
387 if (!this->glCaps().isCoreProfile()) {
388 GL_CALL(Disable(GR_GL_POINT_SMOOTH));
389 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
390 GL_CALL(Disable(GR_GL_POLYGON_SMOOTH));
391 GL_CALL(Disable(GR_GL_POLYGON_STIPPLE));
392 GL_CALL(Disable(GR_GL_COLOR_LOGIC_OP));
393 GL_CALL(Disable(GR_GL_INDEX_LOGIC_OP));
394 }
395 // The windows NVIDIA driver has GL_ARB_imaging in the extension string when using a
396 // core profile. This seems like a bug since the core spec removes any mention of
397 // GL_ARB_imaging.
398 if (this->glCaps().imagingSupport() && !this->glCaps().isCoreProfile()) {
399 GL_CALL(Disable(GR_GL_COLOR_TABLE));
400 }
401 GL_CALL(Disable(GR_GL_POLYGON_OFFSET_FILL));
Jim Van Verth609e7cc2017-03-30 14:28:08 -0400402
Jim Van Verthfbdc0802017-05-02 16:15:53 -0400403 if (this->caps()->wireframeMode()) {
404 GL_CALL(PolygonMode(GR_GL_FRONT_AND_BACK, GR_GL_LINE));
405 } else {
406 GL_CALL(PolygonMode(GR_GL_FRONT_AND_BACK, GR_GL_FILL));
407 }
Jim Van Verth32ac83e2016-11-28 15:23:57 -0500408#endif
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000409 // Since ES doesn't support glPointSize at all we always use the VS to
410 // set the point size
411 GL_CALL(Enable(GR_GL_VERTEX_PROGRAM_POINT_SIZE));
412
bsalomon@google.com2b1b8c02013-02-28 22:06:02 +0000413 }
joshualitt58162332014-08-01 06:44:53 -0700414
415 if (kGLES_GrGLStandard == this->glStandard() &&
bsalomon424cc262015-05-22 10:37:30 -0700416 this->hasExtension("GL_ARM_shader_framebuffer_fetch")) {
joshualitt58162332014-08-01 06:44:53 -0700417 // The arm extension requires specifically enabling MSAA fetching per sample.
418 // On some devices this may have a perf hit. Also multiple render targets are disabled
419 GL_CALL(Enable(GR_GL_FETCH_PER_SAMPLE_ARM));
420 }
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000421 fHWWriteToColor = kUnknown_TriState;
422 // we only ever use lines in hairline mode
423 GL_CALL(LineWidth(1));
bsalomonaca31fe2015-09-22 11:38:46 -0700424 GL_CALL(Disable(GR_GL_DITHER));
bsalomon@google.comcad107b2013-06-28 14:32:08 +0000425 }
edisonn@google.comba669992013-06-28 16:03:21 +0000426
egdanielb414f252014-07-29 13:15:47 -0700427 if (resetBits & kMSAAEnable_GrGLBackendState) {
428 fMSAAEnabled = kUnknown_TriState;
vbuzinovdded6962015-06-12 08:59:45 -0700429
egdanieleed519e2016-01-15 11:36:18 -0800430 if (this->caps()->usesMixedSamples()) {
cdaltonaf8bc7d2016-02-05 09:35:20 -0800431 if (0 != this->caps()->maxRasterSamples()) {
432 fHWRasterMultisampleEnabled = kUnknown_TriState;
433 fHWNumRasterSamples = 0;
434 }
435
436 // The skia blend modes all use premultiplied alpha and therefore expect RGBA coverage
437 // modulation. This state has no effect when not rendering to a mixed sampled target.
vbuzinovdded6962015-06-12 08:59:45 -0700438 GL_CALL(CoverageModulation(GR_GL_RGBA));
439 }
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000440 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000441
commit-bot@chromium.org46fbfe02013-08-30 15:52:12 +0000442 fHWActiveTextureUnitIdx = -1; // invalid
Brian Salomonaf971de2017-06-08 16:11:33 -0400443 fLastPrimitiveType = static_cast<GrPrimitiveType>(-1);
commit-bot@chromium.org46fbfe02013-08-30 15:52:12 +0000444
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000445 if (resetBits & kTextureBinding_GrGLBackendState) {
bsalomon1c63bf62014-07-22 13:09:46 -0700446 for (int s = 0; s < fHWBoundTextureUniqueIDs.count(); ++s) {
Robert Phillips294870f2016-11-11 12:38:40 -0500447 fHWBoundTextureUniqueIDs[s].makeInvalid();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000448 }
cdalton74b8d322016-04-11 14:47:28 -0700449 for (int b = 0; b < fHWBufferTextures.count(); ++b) {
450 SkASSERT(this->caps()->shaderCaps()->texelBufferSupport());
451 fHWBufferTextures[b].fKnownBound = false;
452 }
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000453 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000454
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000455 if (resetBits & kBlend_GrGLBackendState) {
456 fHWBlendState.invalidate();
457 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000458
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000459 if (resetBits & kView_GrGLBackendState) {
460 fHWScissorSettings.invalidate();
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700461 fHWWindowRectsState.invalidate();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000462 fHWViewport.invalidate();
463 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000464
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000465 if (resetBits & kStencil_GrGLBackendState) {
466 fHWStencilSettings.invalidate();
467 fHWStencilTestEnabled = kUnknown_TriState;
468 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000469
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000470 // Vertex
471 if (resetBits & kVertex_GrGLBackendState) {
cdaltone2e71c22016-04-07 18:13:29 -0700472 fHWVertexArrayState.invalidate();
473 fHWBufferState[kVertex_GrBufferType].invalidate();
474 fHWBufferState[kIndex_GrBufferType].invalidate();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000475 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000476
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000477 if (resetBits & kRenderTarget_GrGLBackendState) {
Robert Phillips294870f2016-11-11 12:38:40 -0500478 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon16921ec2015-07-30 15:34:56 -0700479 fHWSRGBFramebuffer = kUnknown_TriState;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000480 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000481
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000482 if (resetBits & kPathRendering_GrGLBackendState) {
jvanverthe9c0fc62015-04-29 11:18:05 -0700483 if (this->caps()->shaderCaps()->pathRenderingSupport()) {
kkinnunenccdaa042014-08-20 01:36:23 -0700484 this->glPathRendering()->resetContext();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000485 }
bsalomon@google.com05a718c2012-06-29 14:01:53 +0000486 }
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000487
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000488 // we assume these values
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000489 if (resetBits & kPixelStore_GrGLBackendState) {
490 if (this->glCaps().unpackRowLengthSupport()) {
491 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
492 }
493 if (this->glCaps().packRowLengthSupport()) {
494 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
495 }
496 if (this->glCaps().unpackFlipYSupport()) {
497 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
498 }
499 if (this->glCaps().packFlipYSupport()) {
500 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, GR_GL_FALSE));
501 }
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000502 }
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000503
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000504 if (resetBits & kProgram_GrGLBackendState) {
505 fHWProgramID = 0;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000506 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000507}
508
Brian Salomond17f6582017-07-19 18:28:58 -0400509static bool check_backend_texture(const GrBackendTexture& backendTex, const GrGLCaps& caps,
510 GrGLTexture::IDDesc* idDesc) {
Greg Daniel7ef28f32017-04-20 16:41:55 +0000511 const GrGLTextureInfo* info = backendTex.getGLTextureInfo();
bsalomon091f60c2015-11-10 11:54:56 -0800512 if (!info || !info->fID) {
Brian Salomond17f6582017-07-19 18:28:58 -0400513 return false;
bsalomon091f60c2015-11-10 11:54:56 -0800514 }
robertphillips@google.comb72e5d32012-10-30 15:18:10 +0000515
Brian Salomond17f6582017-07-19 18:28:58 -0400516 idDesc->fInfo = *info;
bsalomon7ea33f52015-11-22 14:51:00 -0800517
Brian Salomond17f6582017-07-19 18:28:58 -0400518 if (GR_GL_TEXTURE_EXTERNAL == idDesc->fInfo.fTarget) {
519 if (!caps.shaderCaps()->externalTextureSupport()) {
520 return false;
521 }
522 } else if (GR_GL_TEXTURE_RECTANGLE == idDesc->fInfo.fTarget) {
523 if (!caps.rectangleTextureSupport()) {
524 return false;
525 }
526 } else if (GR_GL_TEXTURE_2D != idDesc->fInfo.fTarget) {
527 return false;
528 }
529 return true;
530}
531
532sk_sp<GrTexture> GrGLGpu::onWrapBackendTexture(const GrBackendTexture& backendTex,
Brian Salomond17f6582017-07-19 18:28:58 -0400533 GrWrapOwnership ownership) {
bsalomonb15b4c12014-10-29 12:41:57 -0700534 GrGLTexture::IDDesc idDesc;
Brian Salomond17f6582017-07-19 18:28:58 -0400535 if (!check_backend_texture(backendTex, this->glCaps(), &idDesc)) {
536 return nullptr;
537 }
Greg Daniele7d8da42017-12-04 11:23:19 -0500538 if (!idDesc.fInfo.fFormat) {
539 idDesc.fInfo.fFormat = this->glCaps().configSizedInternalFormat(backendTex.config());
540 }
Brian Salomond17f6582017-07-19 18:28:58 -0400541 if (kBorrow_GrWrapOwnership == ownership) {
542 idDesc.fOwnership = GrBackendObjectOwnership::kBorrowed;
543 } else {
544 idDesc.fOwnership = GrBackendObjectOwnership::kOwned;
545 }
bsalomone5286e02016-01-14 09:24:09 -0800546
Brian Salomond17f6582017-07-19 18:28:58 -0400547 GrSurfaceDesc surfDesc;
548 surfDesc.fFlags = kNone_GrSurfaceFlags;
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400549 surfDesc.fOrigin = kTopLeft_GrSurfaceOrigin; // This isn't used in the following
Brian Salomond17f6582017-07-19 18:28:58 -0400550 surfDesc.fWidth = backendTex.width();
551 surfDesc.fHeight = backendTex.height();
552 surfDesc.fConfig = backendTex.config();
553 surfDesc.fSampleCnt = 0;
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400554
Greg Daniel177e6952017-10-12 12:27:11 -0400555 GrMipMapsStatus mipMapsStatus = backendTex.hasMipMaps() ? GrMipMapsStatus::kValid
556 : GrMipMapsStatus::kNotAllocated;
557
558 return GrGLTexture::MakeWrapped(this, surfDesc, mipMapsStatus, idDesc);
Brian Salomond17f6582017-07-19 18:28:58 -0400559}
560
561sk_sp<GrTexture> GrGLGpu::onWrapRenderableBackendTexture(const GrBackendTexture& backendTex,
Brian Salomond17f6582017-07-19 18:28:58 -0400562 int sampleCnt,
563 GrWrapOwnership ownership) {
564 GrGLTexture::IDDesc idDesc;
565 if (!check_backend_texture(backendTex, this->glCaps(), &idDesc)) {
bsalomone5286e02016-01-14 09:24:09 -0800566 return nullptr;
bsalomon7ea33f52015-11-22 14:51:00 -0800567 }
Greg Daniele7d8da42017-12-04 11:23:19 -0500568 if (!idDesc.fInfo.fFormat) {
569 idDesc.fInfo.fFormat = this->glCaps().configSizedInternalFormat(backendTex.config());
570 }
bsalomone5286e02016-01-14 09:24:09 -0800571
Brian Salomond17f6582017-07-19 18:28:58 -0400572 // We don't support rendering to a EXTERNAL texture.
573 if (GR_GL_TEXTURE_EXTERNAL == idDesc.fInfo.fTarget) {
bsalomona98419b2015-11-23 07:09:50 -0800574 return nullptr;
575 }
bsalomon10528f12015-10-14 12:54:52 -0700576
Brian Osman766fcbb2017-03-13 09:33:09 -0400577 if (kBorrow_GrWrapOwnership == ownership) {
Brian Osmana6953f22017-03-10 20:14:05 +0000578 idDesc.fOwnership = GrBackendObjectOwnership::kBorrowed;
Brian Osman766fcbb2017-03-13 09:33:09 -0400579 } else {
580 idDesc.fOwnership = GrBackendObjectOwnership::kOwned;
bsalomone5286e02016-01-14 09:24:09 -0800581 }
bsalomonb15b4c12014-10-29 12:41:57 -0700582
Ben Wagner18b61f92016-10-25 10:44:02 -0400583 GrSurfaceDesc surfDesc;
Brian Salomond17f6582017-07-19 18:28:58 -0400584 surfDesc.fFlags = kRenderTarget_GrSurfaceFlag;
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400585 surfDesc.fOrigin = kBottomLeft_GrSurfaceOrigin; // This isn't actually used in the following
Greg Daniel7ef28f32017-04-20 16:41:55 +0000586 surfDesc.fWidth = backendTex.width();
587 surfDesc.fHeight = backendTex.height();
588 surfDesc.fConfig = backendTex.config();
Greg Daniel81e7bf82017-07-19 14:47:42 -0400589 surfDesc.fSampleCnt = this->caps()->getSampleCount(sampleCnt, backendTex.config());
bsalomon@google.come269f212011-11-07 13:29:52 +0000590
Brian Salomond17f6582017-07-19 18:28:58 -0400591 GrGLRenderTarget::IDDesc rtIDDesc;
592 if (!this->createRenderTargetObjects(surfDesc, idDesc.fInfo, &rtIDDesc)) {
593 return nullptr;
bsalomon@google.come269f212011-11-07 13:29:52 +0000594 }
Greg Daniel177e6952017-10-12 12:27:11 -0400595
596 GrMipMapsStatus mipMapsStatus = backendTex.hasMipMaps() ? GrMipMapsStatus::kDirty
597 : GrMipMapsStatus::kNotAllocated;
598
Brian Salomond17f6582017-07-19 18:28:58 -0400599 sk_sp<GrGLTextureRenderTarget> texRT(
Greg Daniel177e6952017-10-12 12:27:11 -0400600 GrGLTextureRenderTarget::MakeWrapped(this, surfDesc, idDesc, rtIDDesc, mipMapsStatus));
Brian Salomond17f6582017-07-19 18:28:58 -0400601 texRT->baseLevelWasBoundToFBO();
602 return std::move(texRT);
bsalomon@google.come269f212011-11-07 13:29:52 +0000603}
604
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400605sk_sp<GrRenderTarget> GrGLGpu::onWrapBackendRenderTarget(const GrBackendRenderTarget& backendRT) {
Greg Danielbcf612b2017-05-01 13:50:58 +0000606 const GrGLFramebufferInfo* info = backendRT.getGLFramebufferInfo();
607 if (!info) {
608 return nullptr;
609 }
610
bsalomonb15b4c12014-10-29 12:41:57 -0700611 GrGLRenderTarget::IDDesc idDesc;
Greg Danielbcf612b2017-05-01 13:50:58 +0000612 idDesc.fRTFBOID = info->fFBOID;
bsalomonb15b4c12014-10-29 12:41:57 -0700613 idDesc.fMSColorRenderbufferID = 0;
egdanield803f272015-03-18 13:01:52 -0700614 idDesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
Brian Osman0b791f52017-03-10 08:30:22 -0500615 idDesc.fRTFBOOwnership = GrBackendObjectOwnership::kBorrowed;
csmartdaltonf9635992016-08-10 11:09:07 -0700616 idDesc.fIsMixedSampled = false;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000617
bsalomonb15b4c12014-10-29 12:41:57 -0700618 GrSurfaceDesc desc;
Brian Salomon0ec981b2017-05-15 13:48:50 -0400619 desc.fFlags = kRenderTarget_GrSurfaceFlag;
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400620 desc.fOrigin = kBottomLeft_GrSurfaceOrigin; // This isn't actually used in the following
Greg Danielbcf612b2017-05-01 13:50:58 +0000621 desc.fWidth = backendRT.width();
622 desc.fHeight = backendRT.height();
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400623 desc.fConfig = backendRT.config();
Greg Daniel81e7bf82017-07-19 14:47:42 -0400624 desc.fSampleCnt = this->caps()->getSampleCount(backendRT.sampleCnt(), backendRT.config());
bsalomonb15b4c12014-10-29 12:41:57 -0700625
Greg Danielbcf612b2017-05-01 13:50:58 +0000626 return GrGLRenderTarget::MakeWrapped(this, desc, idDesc, backendRT.stencilBits());
bsalomon@google.come269f212011-11-07 13:29:52 +0000627}
628
Greg Daniel7ef28f32017-04-20 16:41:55 +0000629sk_sp<GrRenderTarget> GrGLGpu::onWrapBackendTextureAsRenderTarget(const GrBackendTexture& tex,
Greg Daniel7ef28f32017-04-20 16:41:55 +0000630 int sampleCnt) {
631 const GrGLTextureInfo* info = tex.getGLTextureInfo();
ericrkf7b8b8a2016-02-24 14:49:51 -0800632 if (!info || !info->fID) {
633 return nullptr;
634 }
ericrkf7b8b8a2016-02-24 14:49:51 -0800635
jvanverthdb1adf52016-05-03 05:37:45 -0700636 GrGLTextureInfo texInfo;
jvanverthdb1adf52016-05-03 05:37:45 -0700637 texInfo = *info;
ericrkf7b8b8a2016-02-24 14:49:51 -0800638
jvanverthdb1adf52016-05-03 05:37:45 -0700639 if (GR_GL_TEXTURE_RECTANGLE != texInfo.fTarget &&
640 GR_GL_TEXTURE_2D != texInfo.fTarget) {
ericrkf7b8b8a2016-02-24 14:49:51 -0800641 // Only texture rectangle and texture 2d are supported. We do not check whether texture
642 // rectangle is supported by Skia - if the caller provided us with a texture rectangle,
643 // we assume the necessary support exists.
644 return nullptr;
645 }
646
Ben Wagner18b61f92016-10-25 10:44:02 -0400647 GrSurfaceDesc surfDesc;
Greg Daniel7ef28f32017-04-20 16:41:55 +0000648 surfDesc.fFlags = kRenderTarget_GrSurfaceFlag;
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400649 surfDesc.fOrigin = kBottomLeft_GrSurfaceOrigin; // This isn't actually used in the following
Greg Daniel7ef28f32017-04-20 16:41:55 +0000650 surfDesc.fWidth = tex.width();
651 surfDesc.fHeight = tex.height();
652 surfDesc.fConfig = tex.config();
Greg Daniel81e7bf82017-07-19 14:47:42 -0400653 surfDesc.fSampleCnt = this->caps()->getSampleCount(sampleCnt, tex.config());
ericrkf7b8b8a2016-02-24 14:49:51 -0800654
655 GrGLRenderTarget::IDDesc rtIDDesc;
jvanverthdb1adf52016-05-03 05:37:45 -0700656 if (!this->createRenderTargetObjects(surfDesc, texInfo, &rtIDDesc)) {
ericrkf7b8b8a2016-02-24 14:49:51 -0800657 return nullptr;
658 }
bungeman6bd52842016-10-27 09:30:08 -0700659 return GrGLRenderTarget::MakeWrapped(this, surfDesc, rtIDDesc, 0);
ericrkf7b8b8a2016-02-24 14:49:51 -0800660}
661
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000662////////////////////////////////////////////////////////////////////////////////
ericrkf7b8b8a2016-02-24 14:49:51 -0800663
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400664bool GrGLGpu::onGetWritePixelsInfo(GrSurface* dstSurface, GrSurfaceOrigin dstOrigin,
665 int width, int height,
cblumeed828002016-02-16 13:00:01 -0800666 GrPixelConfig srcConfig,
bsalomonf0674512015-07-28 13:26:15 -0700667 DrawPreference* drawPreference,
668 WritePixelTempDrawInfo* tempDrawInfo) {
Brian Salomon9bada542017-06-12 12:09:30 -0400669 if (SkToBool(dstSurface->asRenderTarget())) {
670 if (this->glCaps().useDrawInsteadOfAllRenderTargetWrites()) {
671 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
672 }
673 }
674
675 GrGLTexture* texture = static_cast<GrGLTexture*>(dstSurface->asTexture());
676
677 if (texture) {
bsalomone5286e02016-01-14 09:24:09 -0800678 if (GR_GL_TEXTURE_EXTERNAL == texture->target()) {
679 // We don't currently support writing pixels to EXTERNAL textures.
bsalomon7ea33f52015-11-22 14:51:00 -0800680 return false;
681 }
Brian Salomon9bada542017-06-12 12:09:30 -0400682 if (GrPixelConfigIsUnorm(texture->config()) && texture->hasBaseLevelBeenBoundToFBO() &&
683 this->glCaps().disallowTexSubImageForUnormConfigTexturesEverBoundToFBO() &&
684 (width < dstSurface->width() || height < dstSurface->height())) {
685 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
686 }
687 } else {
688 // This subclass only allows writes to textures. If the dst is not a texture we have to draw
689 // into it. We could use glDrawPixels on GLs that have it, but we don't today.
690 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
bsalomon6cb3cbe2015-07-30 07:34:27 -0700691 }
692
Brian Osman33910292017-04-18 14:38:53 -0400693 // If the dst is MSAA, we have to draw, or we'll just be writing to the resolve target.
694 if (dstSurface->asRenderTarget() && dstSurface->asRenderTarget()->numColorSamples() > 0) {
695 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
696 }
697
bsalomon16921ec2015-07-30 15:34:56 -0700698 if (GrPixelConfigIsSRGB(dstSurface->config()) != GrPixelConfigIsSRGB(srcConfig)) {
699 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
700 }
701
bsalomon6c9cd552016-01-22 07:17:34 -0800702 // Start off assuming no swizzling
703 tempDrawInfo->fSwizzle = GrSwizzle::RGBA();
704 tempDrawInfo->fWriteConfig = srcConfig;
bsalomonf0674512015-07-28 13:26:15 -0700705
706 // These settings we will always want if a temp draw is performed. Initially set the config
707 // to srcConfig, though that may be modified if we decide to do a R/G swap.
708 tempDrawInfo->fTempSurfaceDesc.fFlags = kNone_GrSurfaceFlags;
709 tempDrawInfo->fTempSurfaceDesc.fConfig = srcConfig;
710 tempDrawInfo->fTempSurfaceDesc.fWidth = width;
711 tempDrawInfo->fTempSurfaceDesc.fHeight = height;
712 tempDrawInfo->fTempSurfaceDesc.fSampleCnt = 0;
713 tempDrawInfo->fTempSurfaceDesc.fOrigin = kTopLeft_GrSurfaceOrigin; // no CPU y-flip for TL.
714
715 bool configsAreRBSwaps = GrPixelConfigSwapRAndB(srcConfig) == dstSurface->config();
716
717 if (configsAreRBSwaps) {
718 if (!this->caps()->isConfigTexturable(srcConfig)) {
719 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
720 tempDrawInfo->fTempSurfaceDesc.fConfig = dstSurface->config();
bsalomon6c9cd552016-01-22 07:17:34 -0800721 tempDrawInfo->fSwizzle = GrSwizzle::BGRA();
722 tempDrawInfo->fWriteConfig = dstSurface->config();
bsalomon88c7b982015-07-31 11:20:16 -0700723 } else if (this->glCaps().rgba8888PixelsOpsAreSlow() &&
724 kRGBA_8888_GrPixelConfig == srcConfig) {
bsalomonf0674512015-07-28 13:26:15 -0700725 ElevateDrawPreference(drawPreference, kGpuPrefersDraw_DrawPreference);
726 tempDrawInfo->fTempSurfaceDesc.fConfig = dstSurface->config();
bsalomon6c9cd552016-01-22 07:17:34 -0800727 tempDrawInfo->fSwizzle = GrSwizzle::BGRA();
728 tempDrawInfo->fWriteConfig = dstSurface->config();
bsalomonf0674512015-07-28 13:26:15 -0700729 } else if (kGLES_GrGLStandard == this->glStandard() &&
730 this->glCaps().bgraIsInternalFormat()) {
731 // The internal format and external formats must match texture uploads so we can't
732 // swizzle while uploading when BGRA is a distinct internal format.
733 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
734 tempDrawInfo->fTempSurfaceDesc.fConfig = dstSurface->config();
bsalomon6c9cd552016-01-22 07:17:34 -0800735 tempDrawInfo->fSwizzle = GrSwizzle::BGRA();
736 tempDrawInfo->fWriteConfig = dstSurface->config();
bsalomonf0674512015-07-28 13:26:15 -0700737 }
738 }
739
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400740 if (!this->glCaps().unpackFlipYSupport() && kBottomLeft_GrSurfaceOrigin == dstOrigin) {
bsalomonf0674512015-07-28 13:26:15 -0700741 ElevateDrawPreference(drawPreference, kGpuPrefersDraw_DrawPreference);
742 }
743
744 return true;
745}
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000746
jvanverth17aa0472016-01-05 10:41:27 -0800747static bool check_write_and_transfer_input(GrGLTexture* glTex, GrSurface* surface,
748 GrPixelConfig config) {
bsalomon6cb3cbe2015-07-30 07:34:27 -0700749 if (!glTex) {
750 return false;
751 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000752
bsalomon16921ec2015-07-30 15:34:56 -0700753 // OpenGL doesn't do sRGB <-> linear conversions when reading and writing pixels.
754 if (GrPixelConfigIsSRGB(surface->config()) != GrPixelConfigIsSRGB(config)) {
755 return false;
756 }
757
bsalomone5286e02016-01-14 09:24:09 -0800758 // Write or transfer of pixels is not implemented for TEXTURE_EXTERNAL textures
759 if (GR_GL_TEXTURE_EXTERNAL == glTex->target()) {
bsalomon7ea33f52015-11-22 14:51:00 -0800760 return false;
761 }
762
jvanverth17aa0472016-01-05 10:41:27 -0800763 return true;
764}
765
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400766bool GrGLGpu::onWritePixels(GrSurface* surface, GrSurfaceOrigin origin,
jvanverth17aa0472016-01-05 10:41:27 -0800767 int left, int top, int width, int height,
cblume55f2d2d2016-02-26 13:20:48 -0800768 GrPixelConfig config,
Robert Phillips590533f2017-07-11 14:22:35 -0400769 const GrMipLevel texels[],
770 int mipLevelCount) {
jvanverth17aa0472016-01-05 10:41:27 -0800771 GrGLTexture* glTex = static_cast<GrGLTexture*>(surface->asTexture());
772
773 if (!check_write_and_transfer_input(glTex, surface, config)) {
774 return false;
775 }
776
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +0000777 this->setScratchTextureUnit();
bsalomon10528f12015-10-14 12:54:52 -0700778 GL_CALL(BindTexture(glTex->target(), glTex->textureID()));
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000779
Robert Phillips92de6312017-05-23 07:43:48 -0400780 return this->uploadTexData(glTex->config(), glTex->width(), glTex->height(),
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400781 origin, glTex->target(), kWrite_UploadType,
Robert Phillips590533f2017-07-11 14:22:35 -0400782 left, top, width, height, config, texels, mipLevelCount);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000783}
784
bsalomonf46a1242015-12-15 12:37:38 -0800785// For GL_[UN]PACK_ALIGNMENT.
786static inline GrGLint config_alignment(GrPixelConfig config) {
bsalomonf46a1242015-12-15 12:37:38 -0800787 switch (config) {
788 case kAlpha_8_GrPixelConfig:
Greg Danielef59d872017-11-17 16:47:21 -0500789 case kAlpha_8_as_Alpha_GrPixelConfig:
790 case kAlpha_8_as_Red_GrPixelConfig:
Brian Osman986563b2017-01-10 14:20:02 -0500791 case kGray_8_GrPixelConfig:
bsalomonf46a1242015-12-15 12:37:38 -0800792 return 1;
793 case kRGB_565_GrPixelConfig:
794 case kRGBA_4444_GrPixelConfig:
795 case kAlpha_half_GrPixelConfig:
Greg Danielef59d872017-11-17 16:47:21 -0500796 case kAlpha_half_as_Red_GrPixelConfig:
bsalomonf46a1242015-12-15 12:37:38 -0800797 case kRGBA_half_GrPixelConfig:
798 return 2;
799 case kRGBA_8888_GrPixelConfig:
800 case kBGRA_8888_GrPixelConfig:
801 case kSRGBA_8888_GrPixelConfig:
brianosmana6359362016-03-21 06:55:37 -0700802 case kSBGRA_8888_GrPixelConfig:
Brian Salomonbf7b6202016-11-11 16:08:03 -0500803 case kRGBA_8888_sint_GrPixelConfig:
bsalomonf46a1242015-12-15 12:37:38 -0800804 case kRGBA_float_GrPixelConfig:
csmartdalton6aa0e112017-02-08 16:14:11 -0500805 case kRG_float_GrPixelConfig:
bsalomonf46a1242015-12-15 12:37:38 -0800806 return 4;
csmartdalton6aa0e112017-02-08 16:14:11 -0500807 case kUnknown_GrPixelConfig:
bsalomonf46a1242015-12-15 12:37:38 -0800808 return 0;
809 }
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400810 SK_ABORT("Invalid pixel config");
csmartdalton6aa0e112017-02-08 16:14:11 -0500811 return 0;
bsalomonf46a1242015-12-15 12:37:38 -0800812}
813
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400814bool GrGLGpu::onTransferPixels(GrTexture* texture,
815 int left, int top, int width, int height,
816 GrPixelConfig config, GrBuffer* transferBuffer,
817 size_t offset, size_t rowBytes) {
818 GrGLTexture* glTex = static_cast<GrGLTexture*>(texture);
819 GrPixelConfig texConfig = glTex->config();
820 SkASSERT(this->caps()->isConfigTexturable(texConfig));
821
822 if (!check_write_and_transfer_input(glTex, texture, config)) {
823 return false;
824 }
825
826 if (width <= 0 || width > SK_MaxS32 || height <= 0 || height > SK_MaxS32) {
827 return false;
828 }
829
830 this->setScratchTextureUnit();
831 GL_CALL(BindTexture(glTex->target(), glTex->textureID()));
832
833 SkASSERT(!transferBuffer->isMapped());
834 SkASSERT(!transferBuffer->isCPUBacked());
835 const GrGLBuffer* glBuffer = static_cast<const GrGLBuffer*>(transferBuffer);
836 this->bindBuffer(kXferCpuToGpu_GrBufferType, glBuffer);
837
Greg Daniel660cc992017-06-26 14:55:05 -0400838 SkDEBUGCODE(
839 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
840 SkIRect bounds = SkIRect::MakeWH(texture->width(), texture->height());
841 SkASSERT(bounds.contains(subRect));
842 )
843
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400844 size_t bpp = GrBytesPerPixel(config);
845 const size_t trimRowBytes = width * bpp;
Greg Daniel660cc992017-06-26 14:55:05 -0400846 if (!rowBytes) {
847 rowBytes = trimRowBytes;
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400848 }
Greg Daniel660cc992017-06-26 14:55:05 -0400849 const void* pixels = (void*)offset;
Bruce Dawson71479b72017-07-05 14:30:20 -0700850 if (width < 0 || height < 0) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400851 return false;
852 }
853
854 bool restoreGLRowLength = false;
855 if (trimRowBytes != rowBytes) {
856 // we should have checked for this support already
857 SkASSERT(this->glCaps().unpackRowLengthSupport());
858 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowBytes / bpp));
859 restoreGLRowLength = true;
860 }
861
862 // Internal format comes from the texture desc.
863 GrGLenum internalFormat;
864 // External format and type come from the upload data.
865 GrGLenum externalFormat;
866 GrGLenum externalType;
867 if (!this->glCaps().getTexImageFormats(texConfig, config, &internalFormat,
868 &externalFormat, &externalType)) {
869 return false;
870 }
871
872 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, config_alignment(texConfig)));
873 GL_CALL(TexSubImage2D(glTex->target(),
874 0,
875 left, top,
876 width,
877 height,
878 externalFormat, externalType,
879 pixels));
880
881 if (restoreGLRowLength) {
882 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
883 }
884
885 return true;
886}
887
cblume55f2d2d2016-02-26 13:20:48 -0800888/**
889 * Creates storage space for the texture and fills it with texels.
890 *
Brian Salomond1eaf492017-05-18 10:02:08 -0400891 * @param config Pixel config of the texture.
cblume55f2d2d2016-02-26 13:20:48 -0800892 * @param interface The GL interface in use.
cblume790d5132016-02-29 11:13:29 -0800893 * @param caps The capabilities of the GL device.
brianosman81a84852016-09-12 09:05:14 -0700894 * @param internalFormat The data format used for the internal storage of the texture. May be sized.
895 * @param internalFormatForTexStorage The data format used for the TexStorage API. Must be sized.
cblume55f2d2d2016-02-26 13:20:48 -0800896 * @param externalFormat The data format used for the external storage of the texture.
897 * @param externalType The type of the data used for the external storage of the texture.
898 * @param texels The texel data of the texture being created.
899 * @param baseWidth The width of the texture's base mipmap level
900 * @param baseHeight The height of the texture's base mipmap level
cblume55f2d2d2016-02-26 13:20:48 -0800901 */
Robert Phillips92de6312017-05-23 07:43:48 -0400902static bool allocate_and_populate_texture(GrPixelConfig config,
903 const GrGLInterface& interface,
904 const GrGLCaps& caps,
905 GrGLenum target,
906 GrGLenum internalFormat,
907 GrGLenum internalFormatForTexStorage,
908 GrGLenum externalFormat,
909 GrGLenum externalType,
Robert Phillips590533f2017-07-11 14:22:35 -0400910 const GrMipLevel texels[], int mipLevelCount,
Robert Phillips92de6312017-05-23 07:43:48 -0400911 int baseWidth, int baseHeight) {
cblume55f2d2d2016-02-26 13:20:48 -0800912 CLEAR_ERROR_BEFORE_ALLOC(&interface);
cblume790d5132016-02-29 11:13:29 -0800913
Brian Salomond1eaf492017-05-18 10:02:08 -0400914 bool useTexStorage = caps.isConfigTexSupportEnabled(config);
cblume790d5132016-02-29 11:13:29 -0800915 // We can only use TexStorage if we know we will not later change the storage requirements.
916 // This means if we may later want to add mipmaps, we cannot use TexStorage.
917 // Right now, we cannot know if we will later add mipmaps or not.
918 // The only time we can use TexStorage is when we already have the
Brian Salomonbf7b6202016-11-11 16:08:03 -0500919 // mipmaps or are using a format incompatible with MIP maps.
Robert Phillips590533f2017-07-11 14:22:35 -0400920 useTexStorage &= mipLevelCount > 1 || GrPixelConfigIsSint(config);
cblume790d5132016-02-29 11:13:29 -0800921
922 if (useTexStorage) {
923 // We never resize or change formats of textures.
cblume55f2d2d2016-02-26 13:20:48 -0800924 GL_ALLOC_CALL(&interface,
Robert Phillips590533f2017-07-11 14:22:35 -0400925 TexStorage2D(target, SkTMax(mipLevelCount, 1), internalFormatForTexStorage,
Brian Salomond1eaf492017-05-18 10:02:08 -0400926 baseWidth, baseHeight));
Brian Salomon0ec981b2017-05-15 13:48:50 -0400927 GrGLenum error = CHECK_ALLOC_ERROR(&interface);
cblume55f2d2d2016-02-26 13:20:48 -0800928 if (error != GR_GL_NO_ERROR) {
bsalomone699d0c2016-03-09 06:25:15 -0800929 return false;
cblume790d5132016-02-29 11:13:29 -0800930 } else {
Robert Phillips590533f2017-07-11 14:22:35 -0400931 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
cblume790d5132016-02-29 11:13:29 -0800932 const void* currentMipData = texels[currentMipLevel].fPixels;
933 if (currentMipData == nullptr) {
934 continue;
935 }
936 int twoToTheMipLevel = 1 << currentMipLevel;
Brian Salomond1eaf492017-05-18 10:02:08 -0400937 int currentWidth = SkTMax(1, baseWidth / twoToTheMipLevel);
938 int currentHeight = SkTMax(1, baseHeight / twoToTheMipLevel);
cblume790d5132016-02-29 11:13:29 -0800939
940 GR_GL_CALL(&interface,
941 TexSubImage2D(target,
942 currentMipLevel,
943 0, // left
944 0, // top
945 currentWidth,
946 currentHeight,
947 externalFormat, externalType,
948 currentMipData));
949 }
bsalomone699d0c2016-03-09 06:25:15 -0800950 return true;
cblume790d5132016-02-29 11:13:29 -0800951 }
952 } else {
Robert Phillips590533f2017-07-11 14:22:35 -0400953 if (!mipLevelCount) {
cblume790d5132016-02-29 11:13:29 -0800954 GL_ALLOC_CALL(&interface,
955 TexImage2D(target,
bsalomone699d0c2016-03-09 06:25:15 -0800956 0,
cblume790d5132016-02-29 11:13:29 -0800957 internalFormat,
bsalomone699d0c2016-03-09 06:25:15 -0800958 baseWidth,
959 baseHeight,
cblume790d5132016-02-29 11:13:29 -0800960 0, // border
961 externalFormat, externalType,
bsalomone699d0c2016-03-09 06:25:15 -0800962 nullptr));
Brian Salomon0ec981b2017-05-15 13:48:50 -0400963 GrGLenum error = CHECK_ALLOC_ERROR(&interface);
cblume790d5132016-02-29 11:13:29 -0800964 if (error != GR_GL_NO_ERROR) {
bsalomone699d0c2016-03-09 06:25:15 -0800965 return false;
966 }
967 } else {
Robert Phillips590533f2017-07-11 14:22:35 -0400968 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
bsalomone699d0c2016-03-09 06:25:15 -0800969 int twoToTheMipLevel = 1 << currentMipLevel;
970 int currentWidth = SkTMax(1, baseWidth / twoToTheMipLevel);
971 int currentHeight = SkTMax(1, baseHeight / twoToTheMipLevel);
Greg Daniel8b059bd2017-09-28 20:46:45 +0000972 const void* currentMipData = texels[currentMipLevel].fPixels;
bsalomone699d0c2016-03-09 06:25:15 -0800973 // Even if curremtMipData is nullptr, continue to call TexImage2D.
974 // This will allocate texture memory which we can later populate.
975 GL_ALLOC_CALL(&interface,
976 TexImage2D(target,
977 currentMipLevel,
978 internalFormat,
979 currentWidth,
980 currentHeight,
981 0, // border
982 externalFormat, externalType,
983 currentMipData));
Brian Salomon0ec981b2017-05-15 13:48:50 -0400984 GrGLenum error = CHECK_ALLOC_ERROR(&interface);
bsalomone699d0c2016-03-09 06:25:15 -0800985 if (error != GR_GL_NO_ERROR) {
986 return false;
987 }
cblume790d5132016-02-29 11:13:29 -0800988 }
cblume55f2d2d2016-02-26 13:20:48 -0800989 }
990 }
bsalomone699d0c2016-03-09 06:25:15 -0800991 return true;
cblume55f2d2d2016-02-26 13:20:48 -0800992}
993
994/**
cblume55f2d2d2016-02-26 13:20:48 -0800995 * After a texture is created, any state which was altered during its creation
996 * needs to be restored.
997 *
998 * @param interface The GL interface to use.
999 * @param caps The capabilities of the GL device.
1000 * @param restoreGLRowLength Should the row length unpacking be restored?
1001 * @param glFlipY Did GL flip the texture vertically?
1002 */
1003static void restore_pixelstore_state(const GrGLInterface& interface, const GrGLCaps& caps,
1004 bool restoreGLRowLength, bool glFlipY) {
1005 if (restoreGLRowLength) {
1006 SkASSERT(caps.unpackRowLengthSupport());
1007 GR_GL_CALL(&interface, PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
1008 }
1009 if (glFlipY) {
1010 GR_GL_CALL(&interface, PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
1011 }
1012}
1013
Brian Osman9b560242017-09-05 15:34:52 -04001014void GrGLGpu::unbindCpuToGpuXferBuffer() {
1015 auto& xferBufferState = fHWBufferState[kXferCpuToGpu_GrBufferType];
1016 if (!xferBufferState.fBoundBufferUniqueID.isInvalid()) {
1017 GL_CALL(BindBuffer(xferBufferState.fGLTarget, 0));
1018 xferBufferState.invalidate();
1019 }
1020
1021}
1022
Brian Salomond1eaf492017-05-18 10:02:08 -04001023bool GrGLGpu::uploadTexData(GrPixelConfig texConfig, int texWidth, int texHeight,
1024 GrSurfaceOrigin texOrigin, GrGLenum target, UploadType uploadType,
1025 int left, int top, int width, int height, GrPixelConfig dataConfig,
Greg Daniel834f1202017-10-09 15:06:20 -04001026 const GrMipLevel texels[], int mipLevelCount,
Greg Daniel0fc4d2d2017-10-12 11:23:36 -04001027 GrMipMapsStatus* mipMapsStatus) {
Brian Salomond1eaf492017-05-18 10:02:08 -04001028 SkASSERT(this->caps()->isConfigTexturable(texConfig));
Greg Daniel660cc992017-06-26 14:55:05 -04001029 SkDEBUGCODE(
1030 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
1031 SkIRect bounds = SkIRect::MakeWH(texWidth, texHeight);
1032 SkASSERT(bounds.contains(subRect));
1033 )
Robert Phillips590533f2017-07-11 14:22:35 -04001034 SkASSERT(1 == mipLevelCount ||
Greg Daniel660cc992017-06-26 14:55:05 -04001035 (0 == left && 0 == top && width == texWidth && height == texHeight));
bsalomon5b30c6f2015-12-17 14:17:34 -08001036
Brian Osman9b560242017-09-05 15:34:52 -04001037 this->unbindCpuToGpuXferBuffer();
Jim Van Verth2e5eaf02017-06-21 15:55:46 -04001038
cblume55f2d2d2016-02-26 13:20:48 -08001039 // texels is const.
1040 // But we may need to flip the texture vertically to prepare it.
1041 // Rather than flip in place and alter the incoming data,
1042 // we allocate a new buffer to flip into.
1043 // This means we need to make a non-const shallow copy of texels.
Robert Phillips0f992772017-07-12 08:24:56 -04001044 SkAutoTMalloc<GrMipLevel> texelsShallowCopy;
1045
1046 if (mipLevelCount) {
1047 texelsShallowCopy.reset(mipLevelCount);
1048 memcpy(texelsShallowCopy.get(), texels, mipLevelCount*sizeof(GrMipLevel));
1049 }
cblume55f2d2d2016-02-26 13:20:48 -08001050
cblume55f2d2d2016-02-26 13:20:48 -08001051 const GrGLInterface* interface = this->glInterface();
1052 const GrGLCaps& caps = this->glCaps();
1053
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001054 size_t bpp = GrBytesPerPixel(dataConfig);
cblume55f2d2d2016-02-26 13:20:48 -08001055
1056 if (width == 0 || height == 0) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +00001057 return false;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001058 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00001059
bsalomon5b30c6f2015-12-17 14:17:34 -08001060 // Internal format comes from the texture desc.
bsalomon76148af2016-01-12 11:13:47 -08001061 GrGLenum internalFormat;
bsalomon5b30c6f2015-12-17 14:17:34 -08001062 // External format and type come from the upload data.
bsalomon76148af2016-01-12 11:13:47 -08001063 GrGLenum externalFormat;
1064 GrGLenum externalType;
Brian Salomond1eaf492017-05-18 10:02:08 -04001065 if (!this->glCaps().getTexImageFormats(texConfig, dataConfig, &internalFormat, &externalFormat,
1066 &externalType)) {
bsalomon76148af2016-01-12 11:13:47 -08001067 return false;
1068 }
brianosman81a84852016-09-12 09:05:14 -07001069 // TexStorage requires a sized format, and internalFormat may or may not be
Brian Salomond1eaf492017-05-18 10:02:08 -04001070 GrGLenum internalFormatForTexStorage = this->glCaps().configSizedInternalFormat(texConfig);
brianosman81a84852016-09-12 09:05:14 -07001071
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001072 /*
bsalomon5b30c6f2015-12-17 14:17:34 -08001073 * Check whether to allocate a temporary buffer for flipping y or
bsalomon@google.com6f379512011-11-16 20:36:03 +00001074 * because our srcData has extra bytes past each row. If so, we need
1075 * to trim those off here, since GL ES may not let us specify
1076 * GL_UNPACK_ROW_LENGTH.
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001077 */
bsalomon@google.com6f379512011-11-16 20:36:03 +00001078 bool restoreGLRowLength = false;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +00001079 bool swFlipY = false;
1080 bool glFlipY = false;
cblume55f2d2d2016-02-26 13:20:48 -08001081
Robert Phillips590533f2017-07-11 14:22:35 -04001082 if (kBottomLeft_GrSurfaceOrigin == texOrigin && mipLevelCount) {
cblume55f2d2d2016-02-26 13:20:48 -08001083 if (caps.unpackFlipYSupport()) {
1084 glFlipY = true;
1085 } else {
1086 swFlipY = true;
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001087 }
cblume55f2d2d2016-02-26 13:20:48 -08001088 }
1089
1090 // in case we need a temporary, trimmed copy of the src pixels
1091 SkAutoSMalloc<128 * 128> tempStorage;
1092
Greg Daniel0fc4d2d2017-10-12 11:23:36 -04001093 if (mipMapsStatus) {
1094 *mipMapsStatus = GrMipMapsStatus::kValid;
Greg Daniel834f1202017-10-09 15:06:20 -04001095 }
1096
Robert Phillips931f7512017-11-02 12:57:18 -04001097 const bool usesMips = mipLevelCount > 1;
1098
cblume55f2d2d2016-02-26 13:20:48 -08001099 // find the combined size of all the mip levels and the relative offset of
1100 // each into the collective buffer
Robert Phillips931f7512017-11-02 12:57:18 -04001101 bool willNeedData = false;
Greg Daniel55afd6d2017-09-29 09:32:44 -04001102 size_t combinedBufferSize = 0;
1103 SkTArray<size_t> individualMipOffsets(mipLevelCount);
Robert Phillips590533f2017-07-11 14:22:35 -04001104 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
Greg Daniel55afd6d2017-09-29 09:32:44 -04001105 if (texelsShallowCopy[currentMipLevel].fPixels) {
1106 int twoToTheMipLevel = 1 << currentMipLevel;
1107 int currentWidth = SkTMax(1, width / twoToTheMipLevel);
1108 int currentHeight = SkTMax(1, height / twoToTheMipLevel);
Robert Phillips931f7512017-11-02 12:57:18 -04001109 const size_t trimRowBytes = currentWidth * bpp;
1110 const size_t trimmedSize = trimRowBytes * currentHeight;
1111
1112 const size_t rowBytes = texelsShallowCopy[currentMipLevel].fRowBytes
1113 ? texelsShallowCopy[currentMipLevel].fRowBytes
1114 : trimRowBytes;
1115
1116
1117 if (((!caps.unpackRowLengthSupport() || usesMips) && trimRowBytes != rowBytes) ||
1118 swFlipY) {
1119 willNeedData = true;
1120 }
1121
Greg Daniel55afd6d2017-09-29 09:32:44 -04001122 individualMipOffsets.push_back(combinedBufferSize);
1123 combinedBufferSize += trimmedSize;
1124 } else {
Greg Daniel0fc4d2d2017-10-12 11:23:36 -04001125 if (mipMapsStatus) {
1126 *mipMapsStatus = GrMipMapsStatus::kDirty;
Greg Daniel834f1202017-10-09 15:06:20 -04001127 }
Greg Daniel55afd6d2017-09-29 09:32:44 -04001128 individualMipOffsets.push_back(0);
1129 }
Greg Daniel0fc4d2d2017-10-12 11:23:36 -04001130 }
1131 if (mipMapsStatus && mipLevelCount <= 1) {
1132 *mipMapsStatus = GrMipMapsStatus::kNotAllocated;
cblume55f2d2d2016-02-26 13:20:48 -08001133 }
Robert Phillips931f7512017-11-02 12:57:18 -04001134 char* buffer = nullptr;
1135 if (willNeedData) {
1136 buffer = (char*)tempStorage.reset(combinedBufferSize);
1137 }
cblume55f2d2d2016-02-26 13:20:48 -08001138
Robert Phillips590533f2017-07-11 14:22:35 -04001139 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
Greg Daniel55afd6d2017-09-29 09:32:44 -04001140 if (!texelsShallowCopy[currentMipLevel].fPixels) {
1141 continue;
1142 }
cblume55f2d2d2016-02-26 13:20:48 -08001143 int twoToTheMipLevel = 1 << currentMipLevel;
1144 int currentWidth = SkTMax(1, width / twoToTheMipLevel);
1145 int currentHeight = SkTMax(1, height / twoToTheMipLevel);
1146 const size_t trimRowBytes = currentWidth * bpp;
1147
1148 /*
1149 * check whether to allocate a temporary buffer for flipping y or
1150 * because our srcData has extra bytes past each row. If so, we need
1151 * to trim those off here, since GL ES may not let us specify
1152 * GL_UNPACK_ROW_LENGTH.
1153 */
1154 restoreGLRowLength = false;
1155
Greg Daniel55afd6d2017-09-29 09:32:44 -04001156 const size_t rowBytes = texelsShallowCopy[currentMipLevel].fRowBytes
1157 ? texelsShallowCopy[currentMipLevel].fRowBytes
1158 : trimRowBytes;
Greg Daniel660cc992017-06-26 14:55:05 -04001159
ericrk154349b2016-05-10 14:36:53 -07001160 // TODO: This optimization should be enabled with or without mips.
1161 // For use with mips, we must set GR_GL_UNPACK_ROW_LENGTH once per
1162 // mip level, before calling glTexImage2D.
ericrk154349b2016-05-10 14:36:53 -07001163 if (caps.unpackRowLengthSupport() && !swFlipY && !usesMips) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001164 // can't use this for flipping, only non-neg values allowed. :(
1165 if (rowBytes != trimRowBytes) {
1166 GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
cblume55f2d2d2016-02-26 13:20:48 -08001167 GR_GL_CALL(interface, PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength));
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001168 restoreGLRowLength = true;
1169 }
Jim Van Verth2e5eaf02017-06-21 15:55:46 -04001170 } else if (trimRowBytes != rowBytes || swFlipY) {
1171 // copy data into our new storage, skipping the trailing bytes
1172 const char* src = (const char*)texelsShallowCopy[currentMipLevel].fPixels;
1173 if (swFlipY && currentHeight >= 1) {
1174 src += (currentHeight - 1) * rowBytes;
Jim Van Verthf7037d02017-06-21 10:52:43 -04001175 }
Greg Daniel55afd6d2017-09-29 09:32:44 -04001176 char* dst = buffer + individualMipOffsets[currentMipLevel];
Jim Van Verth2e5eaf02017-06-21 15:55:46 -04001177 for (int y = 0; y < currentHeight; y++) {
1178 memcpy(dst, src, trimRowBytes);
1179 if (swFlipY) {
1180 src -= rowBytes;
1181 } else {
1182 src += rowBytes;
1183 }
1184 dst += trimRowBytes;
1185 }
1186 // now point data to our copied version
1187 texelsShallowCopy[currentMipLevel].fPixels = buffer +
Greg Daniel55afd6d2017-09-29 09:32:44 -04001188 individualMipOffsets[currentMipLevel];
Jim Van Verth2e5eaf02017-06-21 15:55:46 -04001189 texelsShallowCopy[currentMipLevel].fRowBytes = trimRowBytes;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001190 }
bsalomone699d0c2016-03-09 06:25:15 -08001191 }
1192
Robert Phillips590533f2017-07-11 14:22:35 -04001193 if (mipLevelCount) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001194 if (glFlipY) {
cblume55f2d2d2016-02-26 13:20:48 -08001195 GR_GL_CALL(interface, PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_TRUE));
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001196 }
Brian Salomond1eaf492017-05-18 10:02:08 -04001197 GR_GL_CALL(interface, PixelStorei(GR_GL_UNPACK_ALIGNMENT, config_alignment(texConfig)));
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001198 }
cblume55f2d2d2016-02-26 13:20:48 -08001199
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001200 bool succeeded = true;
Brian Salomond1eaf492017-05-18 10:02:08 -04001201 if (kNewTexture_UploadType == uploadType) {
1202 if (0 == left && 0 == top && texWidth == width && texHeight == height) {
Robert Phillips92de6312017-05-23 07:43:48 -04001203 succeeded = allocate_and_populate_texture(
Brian Salomond1eaf492017-05-18 10:02:08 -04001204 texConfig, *interface, caps, target, internalFormat,
Robert Phillips590533f2017-07-11 14:22:35 -04001205 internalFormatForTexStorage, externalFormat, externalType,
1206 texelsShallowCopy, mipLevelCount, width, height);
Brian Salomond1eaf492017-05-18 10:02:08 -04001207 } else {
1208 succeeded = false;
1209 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001210 } else {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +00001211 if (swFlipY || glFlipY) {
Brian Salomond1eaf492017-05-18 10:02:08 -04001212 top = texHeight - (top + height);
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001213 }
Robert Phillips590533f2017-07-11 14:22:35 -04001214 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
Greg Daniel55afd6d2017-09-29 09:32:44 -04001215 if (!texelsShallowCopy[currentMipLevel].fPixels) {
1216 continue;
1217 }
cblume55f2d2d2016-02-26 13:20:48 -08001218 int twoToTheMipLevel = 1 << currentMipLevel;
1219 int currentWidth = SkTMax(1, width / twoToTheMipLevel);
1220 int currentHeight = SkTMax(1, height / twoToTheMipLevel);
cblume55f2d2d2016-02-26 13:20:48 -08001221
1222 GL_CALL(TexSubImage2D(target,
1223 currentMipLevel,
1224 left, top,
1225 currentWidth,
1226 currentHeight,
1227 externalFormat, externalType,
1228 texelsShallowCopy[currentMipLevel].fPixels));
1229 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00001230 }
1231
cblume55f2d2d2016-02-26 13:20:48 -08001232 restore_pixelstore_state(*interface, caps, restoreGLRowLength, glFlipY);
1233
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001234 return succeeded;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001235}
1236
bsalomon424cc262015-05-22 10:37:30 -07001237static bool renderbuffer_storage_msaa(const GrGLContext& ctx,
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001238 int sampleCount,
1239 GrGLenum format,
1240 int width, int height) {
robertphillips@google.com6177e692013-02-28 20:16:25 +00001241 CLEAR_ERROR_BEFORE_ALLOC(ctx.interface());
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +00001242 SkASSERT(GrGLCaps::kNone_MSFBOType != ctx.caps()->msFBOType());
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +00001243 switch (ctx.caps()->msFBOType()) {
Brian Salomon00731b42016-10-14 11:30:51 -04001244 case GrGLCaps::kStandard_MSFBOType:
vbuzinovdded6962015-06-12 08:59:45 -07001245 case GrGLCaps::kMixedSamples_MSFBOType:
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001246 GL_ALLOC_CALL(ctx.interface(),
1247 RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
1248 sampleCount,
1249 format,
1250 width, height));
1251 break;
1252 case GrGLCaps::kES_Apple_MSFBOType:
1253 GL_ALLOC_CALL(ctx.interface(),
1254 RenderbufferStorageMultisampleES2APPLE(GR_GL_RENDERBUFFER,
1255 sampleCount,
1256 format,
1257 width, height));
1258 break;
1259 case GrGLCaps::kES_EXT_MsToTexture_MSFBOType:
1260 case GrGLCaps::kES_IMG_MsToTexture_MSFBOType:
1261 GL_ALLOC_CALL(ctx.interface(),
1262 RenderbufferStorageMultisampleES2EXT(GR_GL_RENDERBUFFER,
1263 sampleCount,
1264 format,
1265 width, height));
1266 break;
1267 case GrGLCaps::kNone_MSFBOType:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -04001268 SK_ABORT("Shouldn't be here if we don't support multisampled renderbuffers.");
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001269 break;
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001270 }
Brian Salomon9251d4e2015-03-17 16:03:19 -04001271 return (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctx.interface()));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001272}
1273
egdanielb0e1be22015-04-22 13:27:39 -07001274bool GrGLGpu::createRenderTargetObjects(const GrSurfaceDesc& desc,
bsalomon091f60c2015-11-10 11:54:56 -08001275 const GrGLTextureInfo& texInfo,
bsalomonb15b4c12014-10-29 12:41:57 -07001276 GrGLRenderTarget::IDDesc* idDesc) {
1277 idDesc->fMSColorRenderbufferID = 0;
egdanield803f272015-03-18 13:01:52 -07001278 idDesc->fRTFBOID = 0;
kkinnunen2e6055b2016-04-22 01:48:29 -07001279 idDesc->fRTFBOOwnership = GrBackendObjectOwnership::kOwned;
egdanield803f272015-03-18 13:01:52 -07001280 idDesc->fTexFBOID = 0;
robertphillips76948d42016-05-04 12:47:41 -07001281 SkASSERT((GrGLCaps::kMixedSamples_MSFBOType == this->glCaps().msFBOType()) ==
1282 this->caps()->usesMixedSamples());
csmartdaltonf9635992016-08-10 11:09:07 -07001283 idDesc->fIsMixedSampled = desc.fSampleCnt > 0 && this->caps()->usesMixedSamples();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001284
1285 GrGLenum status;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001286
bsalomona11e5fc2015-12-18 07:59:41 -08001287 GrGLenum colorRenderbufferFormat = 0; // suppress warning
bsalomon@google.comab15d612011-08-09 12:57:56 +00001288
bsalomonb15b4c12014-10-29 12:41:57 -07001289 if (desc.fSampleCnt > 0 && GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.com347c3822013-05-01 20:10:01 +00001290 goto FAILED;
1291 }
1292
egdanield803f272015-03-18 13:01:52 -07001293 GL_CALL(GenFramebuffers(1, &idDesc->fTexFBOID));
1294 if (!idDesc->fTexFBOID) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001295 goto FAILED;
1296 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001297
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001298 // If we are using multisampling we will create two FBOS. We render to one and then resolve to
1299 // the texture bound to the other. The exception is the IMG multisample extension. With this
1300 // extension the texture is multisampled when rendered to and then auto-resolves it when it is
1301 // rendered from.
bsalomonb15b4c12014-10-29 12:41:57 -07001302 if (desc.fSampleCnt > 0 && this->glCaps().usesMSAARenderBuffers()) {
egdanield803f272015-03-18 13:01:52 -07001303 GL_CALL(GenFramebuffers(1, &idDesc->fRTFBOID));
bsalomonb15b4c12014-10-29 12:41:57 -07001304 GL_CALL(GenRenderbuffers(1, &idDesc->fMSColorRenderbufferID));
egdanield803f272015-03-18 13:01:52 -07001305 if (!idDesc->fRTFBOID ||
bsalomona11e5fc2015-12-18 07:59:41 -08001306 !idDesc->fMSColorRenderbufferID) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001307 goto FAILED;
1308 }
bsalomon76148af2016-01-12 11:13:47 -08001309 if (!this->glCaps().getRenderbufferFormat(desc.fConfig, &colorRenderbufferFormat)) {
1310 return false;
1311 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001312 } else {
egdanield803f272015-03-18 13:01:52 -07001313 idDesc->fRTFBOID = idDesc->fTexFBOID;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001314 }
1315
egdanield803f272015-03-18 13:01:52 -07001316 // below here we may bind the FBO
Robert Phillips294870f2016-11-11 12:38:40 -05001317 fHWBoundRenderTargetUniqueID.makeInvalid();
egdanield803f272015-03-18 13:01:52 -07001318 if (idDesc->fRTFBOID != idDesc->fTexFBOID) {
bsalomonb15b4c12014-10-29 12:41:57 -07001319 SkASSERT(desc.fSampleCnt > 0);
1320 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, idDesc->fMSColorRenderbufferID));
bsalomon424cc262015-05-22 10:37:30 -07001321 if (!renderbuffer_storage_msaa(*fGLContext,
bsalomonb15b4c12014-10-29 12:41:57 -07001322 desc.fSampleCnt,
bsalomona11e5fc2015-12-18 07:59:41 -08001323 colorRenderbufferFormat,
bsalomonb15b4c12014-10-29 12:41:57 -07001324 desc.fWidth, desc.fHeight)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001325 goto FAILED;
1326 }
egdanield803f272015-03-18 13:01:52 -07001327 fStats.incRenderTargetBinds();
1328 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, idDesc->fRTFBOID));
1329 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon10528f12015-10-14 12:54:52 -07001330 GR_GL_COLOR_ATTACHMENT0,
1331 GR_GL_RENDERBUFFER,
1332 idDesc->fMSColorRenderbufferID));
Brian Salomon0ec981b2017-05-15 13:48:50 -04001333 if (!this->glCaps().isConfigVerifiedColorAttachment(desc.fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001334 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1335 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
1336 goto FAILED;
1337 }
bsalomon424cc262015-05-22 10:37:30 -07001338 fGLContext->caps()->markConfigAsValidColorAttachment(desc.fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001339 }
1340 }
egdanield803f272015-03-18 13:01:52 -07001341 fStats.incRenderTargetBinds();
1342 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, idDesc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001343
bsalomonb15b4c12014-10-29 12:41:57 -07001344 if (this->glCaps().usesImplicitMSAAResolve() && desc.fSampleCnt > 0) {
egdanield803f272015-03-18 13:01:52 -07001345 GL_CALL(FramebufferTexture2DMultisample(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001346 GR_GL_COLOR_ATTACHMENT0,
bsalomon091f60c2015-11-10 11:54:56 -08001347 texInfo.fTarget,
1348 texInfo.fID, 0, desc.fSampleCnt));
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001349 } else {
egdanield803f272015-03-18 13:01:52 -07001350 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001351 GR_GL_COLOR_ATTACHMENT0,
bsalomon091f60c2015-11-10 11:54:56 -08001352 texInfo.fTarget,
1353 texInfo.fID, 0));
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001354 }
Brian Salomon0ec981b2017-05-15 13:48:50 -04001355 if (!this->glCaps().isConfigVerifiedColorAttachment(desc.fConfig)) {
egdanield803f272015-03-18 13:01:52 -07001356 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001357 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
1358 goto FAILED;
1359 }
bsalomon424cc262015-05-22 10:37:30 -07001360 fGLContext->caps()->markConfigAsValidColorAttachment(desc.fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001361 }
1362
1363 return true;
1364
1365FAILED:
bsalomonb15b4c12014-10-29 12:41:57 -07001366 if (idDesc->fMSColorRenderbufferID) {
1367 GL_CALL(DeleteRenderbuffers(1, &idDesc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001368 }
egdanield803f272015-03-18 13:01:52 -07001369 if (idDesc->fRTFBOID != idDesc->fTexFBOID) {
1370 GL_CALL(DeleteFramebuffers(1, &idDesc->fRTFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001371 }
egdanield803f272015-03-18 13:01:52 -07001372 if (idDesc->fTexFBOID) {
1373 GL_CALL(DeleteFramebuffers(1, &idDesc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001374 }
1375 return false;
1376}
1377
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001378// good to set a break-point here to know when createTexture fails
Robert Phillips67d52cf2017-06-05 13:38:13 -04001379static sk_sp<GrTexture> return_null_texture() {
mtklein@google.com330313a2013-08-22 15:37:26 +00001380// SkDEBUGFAIL("null texture");
halcanary96fcdcc2015-08-27 07:41:13 -07001381 return nullptr;
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001382}
1383
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +00001384#if 0 && defined(SK_DEBUG)
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001385static size_t as_size_t(int x) {
1386 return x;
1387}
1388#endif
1389
cblume55f2d2d2016-02-26 13:20:48 -08001390static void set_initial_texture_params(const GrGLInterface* interface,
1391 const GrGLTextureInfo& info,
1392 GrGLTexture::TexParams* initialTexParams) {
1393 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
1394 // drivers have a bug where an FBO won't be complete if it includes a
1395 // texture that is not mipmap complete (considering the filter in use).
1396 // we only set a subset here so invalidate first
1397 initialTexParams->invalidate();
1398 initialTexParams->fMinFilter = GR_GL_NEAREST;
1399 initialTexParams->fMagFilter = GR_GL_NEAREST;
1400 initialTexParams->fWrapS = GR_GL_CLAMP_TO_EDGE;
1401 initialTexParams->fWrapT = GR_GL_CLAMP_TO_EDGE;
1402 GR_GL_CALL(interface, TexParameteri(info.fTarget,
1403 GR_GL_TEXTURE_MAG_FILTER,
1404 initialTexParams->fMagFilter));
1405 GR_GL_CALL(interface, TexParameteri(info.fTarget,
1406 GR_GL_TEXTURE_MIN_FILTER,
1407 initialTexParams->fMinFilter));
1408 GR_GL_CALL(interface, TexParameteri(info.fTarget,
1409 GR_GL_TEXTURE_WRAP_S,
1410 initialTexParams->fWrapS));
1411 GR_GL_CALL(interface, TexParameteri(info.fTarget,
1412 GR_GL_TEXTURE_WRAP_T,
1413 initialTexParams->fWrapT));
1414}
1415
Robert Phillips67d52cf2017-06-05 13:38:13 -04001416sk_sp<GrTexture> GrGLGpu::onCreateTexture(const GrSurfaceDesc& desc,
1417 SkBudgeted budgeted,
Robert Phillips590533f2017-07-11 14:22:35 -04001418 const GrMipLevel texels[],
1419 int mipLevelCount) {
bsalomon@google.com8a70eef2013-03-19 13:58:55 +00001420 // We fail if the MSAA was requested and is not available.
1421 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType() && desc.fSampleCnt) {
tfarina38406c82014-10-31 07:11:12 -07001422 //SkDebugf("MSAA RT requested but not supported on this platform.");
bsalomon@google.com8a70eef2013-03-19 13:58:55 +00001423 return return_null_texture();
1424 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001425
Brian Salomond17b4a62017-05-23 16:53:47 -04001426 bool performClear = (desc.fFlags & kPerformInitialClear_GrSurfaceFlag);
Brian Salomond17b4a62017-05-23 16:53:47 -04001427
Robert Phillips590533f2017-07-11 14:22:35 -04001428 GrMipLevel zeroLevel;
Brian Salomond17b4a62017-05-23 16:53:47 -04001429 std::unique_ptr<uint8_t[]> zeros;
1430 // TODO: remove the GrPixelConfigIsSint test. This is here because we have yet to add support
1431 // for glClearBuffer* which must be used instead of glClearColor/glClear for integer FBO
1432 // attachments.
1433 if (performClear && !this->glCaps().clearTextureSupport() &&
1434 (!this->glCaps().canConfigBeFBOColorAttachment(desc.fConfig) ||
1435 GrPixelConfigIsSint(desc.fConfig))) {
1436 size_t rowSize = GrBytesPerPixel(desc.fConfig) * desc.fWidth;
1437 size_t size = rowSize * desc.fHeight;
1438 zeros.reset(new uint8_t[size]);
1439 memset(zeros.get(), 0, size);
Robert Phillips590533f2017-07-11 14:22:35 -04001440 zeroLevel.fPixels = zeros.get();
1441 zeroLevel.fRowBytes = 0;
1442 texels = &zeroLevel;
1443 mipLevelCount = 1;
Brian Salomond17b4a62017-05-23 16:53:47 -04001444 performClear = false;
1445 }
1446
Brian Salomond34edf32017-05-19 15:45:48 -04001447 bool isRenderTarget = SkToBool(desc.fFlags & kRenderTarget_GrSurfaceFlag);
reed@google.comac10a2d2010-12-22 21:39:39 +00001448
bsalomonb15b4c12014-10-29 12:41:57 -07001449 GrGLTexture::IDDesc idDesc;
kkinnunen2e6055b2016-04-22 01:48:29 -07001450 idDesc.fOwnership = GrBackendObjectOwnership::kOwned;
Greg Daniel0fc4d2d2017-10-12 11:23:36 -04001451 GrMipMapsStatus mipMapsStatus;
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001452 GrGLTexture::TexParams initialTexParams;
Robert Phillips590533f2017-07-11 14:22:35 -04001453 if (!this->createTextureImpl(desc, &idDesc.fInfo, isRenderTarget, &initialTexParams,
Greg Daniel0fc4d2d2017-10-12 11:23:36 -04001454 texels, mipLevelCount, &mipMapsStatus)) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001455 return return_null_texture();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001456 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001457
Robert Phillips67d52cf2017-06-05 13:38:13 -04001458 sk_sp<GrGLTexture> tex;
Brian Salomond34edf32017-05-19 15:45:48 -04001459 if (isRenderTarget) {
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001460 // unbind the texture from the texture unit before binding it to the frame buffer
bsalomon091f60c2015-11-10 11:54:56 -08001461 GL_CALL(BindTexture(idDesc.fInfo.fTarget, 0));
bsalomon5236cf42015-01-14 10:42:08 -08001462 GrGLRenderTarget::IDDesc rtIDDesc;
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001463
kkinnunen2e6055b2016-04-22 01:48:29 -07001464 if (!this->createRenderTargetObjects(desc, idDesc.fInfo, &rtIDDesc)) {
bsalomon091f60c2015-11-10 11:54:56 -08001465 GL_CALL(DeleteTextures(1, &idDesc.fInfo.fID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001466 return return_null_texture();
1467 }
Robert Phillips67d52cf2017-06-05 13:38:13 -04001468 tex = sk_make_sp<GrGLTextureRenderTarget>(this, budgeted, desc, idDesc, rtIDDesc,
Greg Daniel0fc4d2d2017-10-12 11:23:36 -04001469 mipMapsStatus);
Brian Salomon9bada542017-06-12 12:09:30 -04001470 tex->baseLevelWasBoundToFBO();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001471 } else {
Greg Daniel0fc4d2d2017-10-12 11:23:36 -04001472 tex = sk_make_sp<GrGLTexture>(this, budgeted, desc, idDesc, mipMapsStatus);
reed@google.comac10a2d2010-12-22 21:39:39 +00001473 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001474 tex->setCachedTexParams(initialTexParams, this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00001475#ifdef TRACE_TEXTURE_CREATION
tfarina38406c82014-10-31 07:11:12 -07001476 SkDebugf("--- new texture [%d] size=(%d %d) config=%d\n",
brianosmanc4459f62016-07-19 08:02:20 -07001477 idDesc.fInfo.fID, desc.fWidth, desc.fHeight, desc.fConfig);
reed@google.comac10a2d2010-12-22 21:39:39 +00001478#endif
Brian Salomond17b4a62017-05-23 16:53:47 -04001479 if (tex && performClear) {
1480 if (this->glCaps().clearTextureSupport()) {
1481 GrGLenum format = GrPixelConfigIsSint(tex->config()) ? GR_GL_RGBA_INTEGER : GR_GL_RGBA;
1482 static constexpr uint32_t kZero = 0;
1483 GL_CALL(ClearTexImage(tex->textureID(), 0, format, GR_GL_UNSIGNED_BYTE, &kZero));
1484 } else {
1485 SkASSERT(!GrPixelConfigIsSint(desc.fConfig));
1486 GrGLIRect viewport;
Robert Phillips67d52cf2017-06-05 13:38:13 -04001487 this->bindSurfaceFBOForPixelOps(tex.get(), GR_GL_FRAMEBUFFER, &viewport,
1488 kDst_TempFBOTarget);
Brian Salomond17b4a62017-05-23 16:53:47 -04001489 this->disableScissor();
1490 this->disableWindowRectangles();
1491 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
1492 fHWWriteToColor = kYes_TriState;
1493 GL_CALL(ClearColor(0, 0, 0, 0));
1494 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
Robert Phillips67d52cf2017-06-05 13:38:13 -04001495 this->unbindTextureFBOForPixelOps(GR_GL_FRAMEBUFFER, tex.get());
Brian Salomond17b4a62017-05-23 16:53:47 -04001496 fHWBoundRenderTargetUniqueID.makeInvalid();
1497 }
1498 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001499 return tex;
1500}
1501
1502namespace {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001503
egdaniel8dc7c3a2015-04-16 11:22:42 -07001504const GrGLuint kUnknownBitCount = GrGLStencilAttachment::kUnknownBitCount;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001505
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001506void inline get_stencil_rb_sizes(const GrGLInterface* gl,
egdaniel8dc7c3a2015-04-16 11:22:42 -07001507 GrGLStencilAttachment::Format* format) {
sugoi@google.com6ba0b0f2013-05-03 13:52:32 +00001508
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001509 // we shouldn't ever know one size and not the other
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001510 SkASSERT((kUnknownBitCount == format->fStencilBits) ==
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001511 (kUnknownBitCount == format->fTotalBits));
1512 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001513 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001514 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1515 (GrGLint*)&format->fStencilBits);
1516 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001517 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001518 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1519 (GrGLint*)&format->fTotalBits);
1520 format->fTotalBits += format->fStencilBits;
1521 } else {
1522 format->fTotalBits = format->fStencilBits;
1523 }
1524 }
1525}
1526}
1527
egdanielff1d5472015-09-10 08:37:20 -07001528int GrGLGpu::getCompatibleStencilIndex(GrPixelConfig config) {
bsalomon100b8f82015-10-28 08:37:44 -07001529 static const int kSize = 16;
bsalomon926cb022015-12-17 18:15:11 -08001530 SkASSERT(this->caps()->isConfigRenderable(config, false));
bsalomon30447372015-12-21 09:03:05 -08001531 if (!this->glCaps().hasStencilFormatBeenDeterminedForConfig(config)) {
1532 // Default to unsupported, set this if we find a stencil format that works.
1533 int firstWorkingStencilFormatIndex = -1;
Brian Osman91f9a2c2017-09-05 15:02:46 -04001534
egdanielff1d5472015-09-10 08:37:20 -07001535 // Create color texture
kkinnunen546eb5c2015-12-11 00:05:33 -08001536 GrGLuint colorID = 0;
egdanielff1d5472015-09-10 08:37:20 -07001537 GL_CALL(GenTextures(1, &colorID));
1538 this->setScratchTextureUnit();
1539 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, colorID));
1540 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1541 GR_GL_TEXTURE_MAG_FILTER,
1542 GR_GL_NEAREST));
1543 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1544 GR_GL_TEXTURE_MIN_FILTER,
1545 GR_GL_NEAREST));
1546 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1547 GR_GL_TEXTURE_WRAP_S,
1548 GR_GL_CLAMP_TO_EDGE));
1549 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1550 GR_GL_TEXTURE_WRAP_T,
1551 GR_GL_CLAMP_TO_EDGE));
1552
bsalomon76148af2016-01-12 11:13:47 -08001553 GrGLenum internalFormat;
1554 GrGLenum externalFormat;
1555 GrGLenum externalType;
1556 if (!this->glCaps().getTexImageFormats(config, config, &internalFormat, &externalFormat,
1557 &externalType)) {
1558 return false;
1559 }
Brian Osman9b560242017-09-05 15:34:52 -04001560 this->unbindCpuToGpuXferBuffer();
egdanielff1d5472015-09-10 08:37:20 -07001561 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1562 GL_ALLOC_CALL(this->glInterface(), TexImage2D(GR_GL_TEXTURE_2D,
bsalomon926cb022015-12-17 18:15:11 -08001563 0,
bsalomon76148af2016-01-12 11:13:47 -08001564 internalFormat,
bsalomon100b8f82015-10-28 08:37:44 -07001565 kSize,
1566 kSize,
egdanielff1d5472015-09-10 08:37:20 -07001567 0,
bsalomon76148af2016-01-12 11:13:47 -08001568 externalFormat,
1569 externalType,
Ben Wagnera93a14a2017-08-28 10:34:05 -04001570 nullptr));
bsalomon30447372015-12-21 09:03:05 -08001571 if (GR_GL_NO_ERROR != CHECK_ALLOC_ERROR(this->glInterface())) {
egdanielff1d5472015-09-10 08:37:20 -07001572 GL_CALL(DeleteTextures(1, &colorID));
bsalomon30447372015-12-21 09:03:05 -08001573 return -1;
egdanielff1d5472015-09-10 08:37:20 -07001574 }
1575
1576 // unbind the texture from the texture unit before binding it to the frame buffer
1577 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, 0));
1578
1579 // Create Framebuffer
kkinnunen546eb5c2015-12-11 00:05:33 -08001580 GrGLuint fb = 0;
egdanielff1d5472015-09-10 08:37:20 -07001581 GL_CALL(GenFramebuffers(1, &fb));
egdanielec00d942015-09-14 12:56:10 -07001582 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, fb));
Robert Phillips294870f2016-11-11 12:38:40 -05001583 fHWBoundRenderTargetUniqueID.makeInvalid();
egdanielff1d5472015-09-10 08:37:20 -07001584 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
1585 GR_GL_COLOR_ATTACHMENT0,
1586 GR_GL_TEXTURE_2D,
1587 colorID,
1588 0));
bsalomon30447372015-12-21 09:03:05 -08001589 GrGLuint sbRBID = 0;
1590 GL_CALL(GenRenderbuffers(1, &sbRBID));
egdanielff1d5472015-09-10 08:37:20 -07001591
1592 // look over formats till I find a compatible one
1593 int stencilFmtCnt = this->glCaps().stencilFormats().count();
bsalomon30447372015-12-21 09:03:05 -08001594 if (sbRBID) {
egdanielff1d5472015-09-10 08:37:20 -07001595 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbRBID));
bsalomon30447372015-12-21 09:03:05 -08001596 for (int i = 0; i < stencilFmtCnt && sbRBID; ++i) {
1597 const GrGLCaps::StencilFormat& sFmt = this->glCaps().stencilFormats()[i];
1598 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1599 GL_ALLOC_CALL(this->glInterface(), RenderbufferStorage(GR_GL_RENDERBUFFER,
1600 sFmt.fInternalFormat,
1601 kSize, kSize));
1602 if (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(this->glInterface())) {
egdanielff1d5472015-09-10 08:37:20 -07001603 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon30447372015-12-21 09:03:05 -08001604 GR_GL_STENCIL_ATTACHMENT,
egdanielff1d5472015-09-10 08:37:20 -07001605 GR_GL_RENDERBUFFER, sbRBID));
bsalomon30447372015-12-21 09:03:05 -08001606 if (sFmt.fPacked) {
1607 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1608 GR_GL_DEPTH_ATTACHMENT,
1609 GR_GL_RENDERBUFFER, sbRBID));
1610 } else {
1611 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1612 GR_GL_DEPTH_ATTACHMENT,
1613 GR_GL_RENDERBUFFER, 0));
1614 }
1615 GrGLenum status;
1616 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1617 if (status == GR_GL_FRAMEBUFFER_COMPLETE) {
1618 firstWorkingStencilFormatIndex = i;
1619 break;
1620 }
egdanielff1d5472015-09-10 08:37:20 -07001621 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1622 GR_GL_STENCIL_ATTACHMENT,
1623 GR_GL_RENDERBUFFER, 0));
1624 if (sFmt.fPacked) {
1625 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1626 GR_GL_DEPTH_ATTACHMENT,
1627 GR_GL_RENDERBUFFER, 0));
1628 }
egdanielff1d5472015-09-10 08:37:20 -07001629 }
1630 }
bsalomon30447372015-12-21 09:03:05 -08001631 GL_CALL(DeleteRenderbuffers(1, &sbRBID));
egdanielff1d5472015-09-10 08:37:20 -07001632 }
1633 GL_CALL(DeleteTextures(1, &colorID));
egdanielff1d5472015-09-10 08:37:20 -07001634 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, 0));
1635 GL_CALL(DeleteFramebuffers(1, &fb));
bsalomon30447372015-12-21 09:03:05 -08001636 fGLContext->caps()->setStencilFormatIndexForConfig(config, firstWorkingStencilFormatIndex);
egdanielff1d5472015-09-10 08:37:20 -07001637 }
bsalomon30447372015-12-21 09:03:05 -08001638 return this->glCaps().getStencilFormatIndexForConfig(config);
egdanielff1d5472015-09-10 08:37:20 -07001639}
1640
erikchen9a1ed5d2016-02-10 16:32:34 -08001641bool GrGLGpu::createTextureImpl(const GrSurfaceDesc& desc, GrGLTextureInfo* info,
cblume55f2d2d2016-02-26 13:20:48 -08001642 bool renderTarget, GrGLTexture::TexParams* initialTexParams,
Greg Daniel834f1202017-10-09 15:06:20 -04001643 const GrMipLevel texels[], int mipLevelCount,
Greg Daniel0fc4d2d2017-10-12 11:23:36 -04001644 GrMipMapsStatus* mipMapsStatus) {
erikchen9a1ed5d2016-02-10 16:32:34 -08001645 info->fID = 0;
1646 info->fTarget = GR_GL_TEXTURE_2D;
1647 GL_CALL(GenTextures(1, &(info->fID)));
1648
1649 if (!info->fID) {
1650 return false;
1651 }
1652
1653 this->setScratchTextureUnit();
1654 GL_CALL(BindTexture(info->fTarget, info->fID));
1655
1656 if (renderTarget && this->glCaps().textureUsageSupport()) {
1657 // provides a hint about how this texture will be used
1658 GL_CALL(TexParameteri(info->fTarget,
1659 GR_GL_TEXTURE_USAGE,
1660 GR_GL_FRAMEBUFFER_ATTACHMENT));
1661 }
1662
cblume55f2d2d2016-02-26 13:20:48 -08001663 if (info) {
1664 set_initial_texture_params(this->glInterface(), *info, initialTexParams);
1665 }
Brian Salomond1eaf492017-05-18 10:02:08 -04001666 if (!this->uploadTexData(desc.fConfig, desc.fWidth, desc.fHeight, desc.fOrigin, info->fTarget,
1667 kNewTexture_UploadType, 0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
Greg Daniel0fc4d2d2017-10-12 11:23:36 -04001668 texels, mipLevelCount, mipMapsStatus)) {
erikchen9a1ed5d2016-02-10 16:32:34 -08001669 GL_CALL(DeleteTextures(1, &(info->fID)));
1670 return false;
1671 }
Greg Daniele7d8da42017-12-04 11:23:19 -05001672 info->fFormat = this->glCaps().configSizedInternalFormat(desc.fConfig);
erikchen9a1ed5d2016-02-10 16:32:34 -08001673 return true;
1674}
1675
egdanielec00d942015-09-14 12:56:10 -07001676GrStencilAttachment* GrGLGpu::createStencilAttachmentForRenderTarget(const GrRenderTarget* rt,
Brian Salomond1eaf492017-05-18 10:02:08 -04001677 int width, int height) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001678 SkASSERT(width >= rt->width());
1679 SkASSERT(height >= rt->height());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001680
vbuzinovdded6962015-06-12 08:59:45 -07001681 int samples = rt->numStencilSamples();
egdaniel8dc7c3a2015-04-16 11:22:42 -07001682 GrGLStencilAttachment::IDDesc sbDesc;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001683
egdanielff1d5472015-09-10 08:37:20 -07001684 int sIdx = this->getCompatibleStencilIndex(rt->config());
bsalomon62a627b2015-12-17 09:50:47 -08001685 if (sIdx < 0) {
egdanielec00d942015-09-14 12:56:10 -07001686 return nullptr;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001687 }
egdanielff1d5472015-09-10 08:37:20 -07001688
1689 if (!sbDesc.fRenderbufferID) {
1690 GL_CALL(GenRenderbuffers(1, &sbDesc.fRenderbufferID));
1691 }
1692 if (!sbDesc.fRenderbufferID) {
egdanielec00d942015-09-14 12:56:10 -07001693 return nullptr;
egdanielff1d5472015-09-10 08:37:20 -07001694 }
1695 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbDesc.fRenderbufferID));
1696 const GrGLCaps::StencilFormat& sFmt = this->glCaps().stencilFormats()[sIdx];
1697 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1698 // we do this "if" so that we don't call the multisample
1699 // version on a GL that doesn't have an MSAA extension.
1700 if (samples > 0) {
1701 SkAssertResult(renderbuffer_storage_msaa(*fGLContext,
1702 samples,
1703 sFmt.fInternalFormat,
1704 width, height));
1705 } else {
1706 GL_ALLOC_CALL(this->glInterface(), RenderbufferStorage(GR_GL_RENDERBUFFER,
1707 sFmt.fInternalFormat,
1708 width, height));
Brian Salomon0ec981b2017-05-15 13:48:50 -04001709 SkASSERT(GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(this->glInterface()));
egdanielff1d5472015-09-10 08:37:20 -07001710 }
1711 fStats.incStencilAttachmentCreates();
1712 // After sized formats we attempt an unsized format and take
1713 // whatever sizes GL gives us. In that case we query for the size.
1714 GrGLStencilAttachment::Format format = sFmt;
1715 get_stencil_rb_sizes(this->glInterface(), &format);
egdanielec00d942015-09-14 12:56:10 -07001716 GrGLStencilAttachment* stencil = new GrGLStencilAttachment(this,
1717 sbDesc,
1718 width,
1719 height,
1720 samples,
1721 format);
1722 return stencil;
reed@google.comac10a2d2010-12-22 21:39:39 +00001723}
1724
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001725////////////////////////////////////////////////////////////////////////////////
1726
jvanverth73063dc2015-12-03 09:15:47 -08001727// GL_STREAM_DRAW triggers an optimization in Chromium's GPU process where a client's vertex buffer
1728// objects are implemented as client-side-arrays on tile-deferred architectures.
1729#define DYNAMIC_USAGE_PARAM GR_GL_STREAM_DRAW
1730
cdaltone2e71c22016-04-07 18:13:29 -07001731GrBuffer* GrGLGpu::onCreateBuffer(size_t size, GrBufferType intendedType,
cdalton1bf3e712016-04-19 10:00:02 -07001732 GrAccessPattern accessPattern, const void* data) {
1733 return GrGLBuffer::Create(this, size, intendedType, accessPattern, data);
jvanverth73063dc2015-12-03 09:15:47 -08001734}
1735
Robert Phillipse3302df2017-04-24 07:31:02 -04001736
1737std::unique_ptr<OpAllocator> GrGLGpu::onCreateInstancedRenderingAllocator() {
1738 return std::unique_ptr<OpAllocator>(new GLOpAllocator(this->caps()));
1739}
1740
csmartdaltone0d36292016-07-29 08:14:20 -07001741InstancedRendering* GrGLGpu::onCreateInstancedRendering() {
1742 return new GLInstancedRendering(this);
1743}
1744
bsalomon3e791242014-12-17 13:43:13 -08001745void GrGLGpu::flushScissor(const GrScissorState& scissorState,
joshualitt77b13072014-10-27 14:51:01 -07001746 const GrGLIRect& rtViewport,
1747 GrSurfaceOrigin rtOrigin) {
robertphillipse85a32d2015-02-10 08:16:55 -08001748 if (scissorState.enabled()) {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001749 GrGLIRect scissor;
Robert Phillips16d8ec62017-07-27 16:16:25 -04001750 scissor.setRelativeTo(rtViewport, scissorState.rect(), rtOrigin);
bsalomon@google.coma3201942012-06-21 19:58:20 +00001751 // if the scissor fully contains the viewport then we fall through and
1752 // disable the scissor test.
bsalomonb0bd4f62014-09-03 07:19:50 -07001753 if (!scissor.contains(rtViewport)) {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001754 if (fHWScissorSettings.fRect != scissor) {
1755 scissor.pushToGLScissor(this->glInterface());
1756 fHWScissorSettings.fRect = scissor;
1757 }
1758 if (kYes_TriState != fHWScissorSettings.fEnabled) {
1759 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1760 fHWScissorSettings.fEnabled = kYes_TriState;
1761 }
1762 return;
1763 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001764 }
joshualitt77b13072014-10-27 14:51:01 -07001765
1766 // See fall through note above
1767 this->disableScissor();
1768}
1769
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001770void GrGLGpu::flushWindowRectangles(const GrWindowRectsState& windowState,
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001771 const GrGLRenderTarget* rt, GrSurfaceOrigin origin) {
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001772#ifndef USE_NSIGHT
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001773 typedef GrWindowRectsState::Mode Mode;
1774 SkASSERT(!windowState.enabled() || rt->renderFBOID()); // Window rects can't be used on-screen.
1775 SkASSERT(windowState.numWindows() <= this->caps()->maxWindowRectangles());
csmartdalton28341fa2016-08-17 10:00:21 -07001776
Jim Van Verth6a40abc2017-11-02 16:56:09 +00001777 if (!this->caps()->maxWindowRectangles() ||
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001778 fHWWindowRectsState.knownEqualTo(origin, rt->getViewport(), windowState)) {
csmartdalton28341fa2016-08-17 10:00:21 -07001779 return;
csmartdalton28341fa2016-08-17 10:00:21 -07001780 }
1781
csmartdalton7535f412016-08-23 06:51:00 -07001782 // This is purely a workaround for a spurious warning generated by gcc. Otherwise the above
1783 // assert would be sufficient. https://gcc.gnu.org/bugzilla/show_bug.cgi?id=5912
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001784 int numWindows = SkTMin(windowState.numWindows(), int(GrWindowRectangles::kMaxWindows));
1785 SkASSERT(windowState.numWindows() == numWindows);
csmartdalton7535f412016-08-23 06:51:00 -07001786
csmartdalton28341fa2016-08-17 10:00:21 -07001787 GrGLIRect glwindows[GrWindowRectangles::kMaxWindows];
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001788 const SkIRect* skwindows = windowState.windows().data();
csmartdalton7535f412016-08-23 06:51:00 -07001789 for (int i = 0; i < numWindows; ++i) {
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001790 glwindows[i].setRelativeTo(rt->getViewport(), skwindows[i], origin);
csmartdalton28341fa2016-08-17 10:00:21 -07001791 }
1792
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001793 GrGLenum glmode = (Mode::kExclusive == windowState.mode()) ? GR_GL_EXCLUSIVE : GR_GL_INCLUSIVE;
csmartdalton7535f412016-08-23 06:51:00 -07001794 GL_CALL(WindowRectangles(glmode, numWindows, glwindows->asInts()));
csmartdalton28341fa2016-08-17 10:00:21 -07001795
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001796 fHWWindowRectsState.set(origin, rt->getViewport(), windowState);
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001797#endif
csmartdalton28341fa2016-08-17 10:00:21 -07001798}
1799
1800void GrGLGpu::disableWindowRectangles() {
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001801#ifndef USE_NSIGHT
Jim Van Verth6a40abc2017-11-02 16:56:09 +00001802 if (!this->caps()->maxWindowRectangles() || fHWWindowRectsState.knownDisabled()) {
csmartdalton28341fa2016-08-17 10:00:21 -07001803 return;
1804 }
1805 GL_CALL(WindowRectangles(GR_GL_EXCLUSIVE, 0, nullptr));
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001806 fHWWindowRectsState.setDisabled();
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001807#endif
csmartdalton28341fa2016-08-17 10:00:21 -07001808}
1809
ethannicholas28ef4452016-03-25 09:26:03 -07001810void GrGLGpu::flushMinSampleShading(float minSampleShading) {
1811 if (fHWMinSampleShading != minSampleShading) {
1812 if (minSampleShading > 0.0) {
1813 GL_CALL(Enable(GR_GL_SAMPLE_SHADING));
1814 GL_CALL(MinSampleShading(minSampleShading));
1815 }
1816 else {
1817 GL_CALL(Disable(GR_GL_SAMPLE_SHADING));
1818 }
1819 fHWMinSampleShading = minSampleShading;
1820 }
1821}
1822
bsalomon2eda5b32016-09-21 10:53:24 -07001823bool GrGLGpu::flushGLState(const GrPipeline& pipeline, const GrPrimitiveProcessor& primProc,
1824 bool willDrawPoints) {
Hal Canary144caf52016-11-07 17:57:18 -05001825 sk_sp<GrGLProgram> program(fProgramCache->refProgram(this, pipeline, primProc, willDrawPoints));
brianosman33f6b3f2016-06-02 05:49:21 -07001826 if (!program) {
1827 GrCapsDebugf(this->caps(), "Failed to create program!\n");
1828 return false;
1829 }
1830
1831 program->generateMipmaps(primProc, pipeline);
1832
egdaniel080e6732014-12-22 07:35:52 -08001833 GrXferProcessor::BlendInfo blendInfo;
egdaniel0e1853c2016-03-17 11:35:45 -07001834 pipeline.getXferProcessor().getBlendInfo(&blendInfo);
egdaniel080e6732014-12-22 07:35:52 -08001835
egdaniel080e6732014-12-22 07:35:52 -08001836 this->flushColorWrite(blendInfo.fWriteColor);
ethannicholas28ef4452016-03-25 09:26:03 -07001837 this->flushMinSampleShading(primProc.getSampleShading());
bsalomonbc3d0de2014-12-15 13:45:03 -08001838
bsalomon6df86402015-06-01 10:41:49 -07001839 GrGLuint programID = program->programID();
bsalomon1f78c0a2014-12-17 09:43:13 -08001840 if (fHWProgramID != programID) {
1841 GL_CALL(UseProgram(programID));
1842 fHWProgramID = programID;
1843 }
1844
egdanield803f272015-03-18 13:01:52 -07001845 if (blendInfo.fWriteColor) {
bsalomon7f9b2e42016-01-12 13:29:26 -08001846 // Swizzle the blend to match what the shader will output.
Brian Salomon1edc5b92016-11-29 13:43:46 -05001847 const GrSwizzle& swizzle = this->caps()->shaderCaps()->configOutputSwizzle(
Robert Phillips2890fbf2017-07-26 15:48:41 -04001848 pipeline.proxy()->config());
bsalomon7f9b2e42016-01-12 13:29:26 -08001849 this->flushBlend(blendInfo, swizzle);
egdanield803f272015-03-18 13:01:52 -07001850 }
bsalomon1f78c0a2014-12-17 09:43:13 -08001851
cdalton74b8d322016-04-11 14:47:28 -07001852 program->setData(primProc, pipeline);
bsalomon1f78c0a2014-12-17 09:43:13 -08001853
Robert Phillips2890fbf2017-07-26 15:48:41 -04001854 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(pipeline.renderTarget());
csmartdaltonc633abb2016-11-01 08:55:55 -07001855 GrStencilSettings stencil;
1856 if (pipeline.isStencilEnabled()) {
1857 // TODO: attach stencil and create settings during render target flush.
1858 SkASSERT(glRT->renderTargetPriv().getStencilAttachment());
1859 stencil.reset(*pipeline.getUserStencil(), pipeline.hasStencilClip(),
1860 glRT->renderTargetPriv().numStencilBits());
1861 }
1862 this->flushStencil(stencil);
Robert Phillips16d8ec62017-07-27 16:16:25 -04001863 this->flushScissor(pipeline.getScissorState(), glRT->getViewport(), pipeline.proxy()->origin());
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001864 this->flushWindowRectangles(pipeline.getWindowRectsState(), glRT, pipeline.proxy()->origin());
csmartdaltondabc91b2016-11-15 14:26:27 -07001865 this->flushHWAAState(glRT, pipeline.isHWAntialiasState(), !stencil.isDisabled());
bsalomonbc3d0de2014-12-15 13:45:03 -08001866
1867 // This must come after textures are flushed because a texture may need
egdanield803f272015-03-18 13:01:52 -07001868 // to be msaa-resolved (which will modify bound FBO state).
brianosman64d094d2016-03-25 06:01:59 -07001869 this->flushRenderTarget(glRT, nullptr, pipeline.getDisableOutputConversionToSRGB());
bsalomonbc3d0de2014-12-15 13:45:03 -08001870
1871 return true;
1872}
1873
joshualitt873ad0e2015-01-20 09:08:51 -08001874void GrGLGpu::setupGeometry(const GrPrimitiveProcessor& primProc,
Chris Daltonff926502017-05-03 14:36:54 -04001875 const GrBuffer* indexBuffer,
1876 const GrBuffer* vertexBuffer,
Chris Dalton1d616352017-05-31 12:51:23 -06001877 int baseVertex,
1878 const GrBuffer* instanceBuffer,
1879 int baseInstance) {
cdaltone2e71c22016-04-07 18:13:29 -07001880 GrGLAttribArrayState* attribState;
Chris Daltonff926502017-05-03 14:36:54 -04001881 if (indexBuffer) {
Chris Dalton1d616352017-05-31 12:51:23 -06001882 SkASSERT(indexBuffer && !indexBuffer->isMapped());
Chris Daltonff926502017-05-03 14:36:54 -04001883 attribState = fHWVertexArrayState.bindInternalVertexArray(this, indexBuffer);
cdaltone2e71c22016-04-07 18:13:29 -07001884 } else {
1885 attribState = fHWVertexArrayState.bindInternalVertexArray(this);
bsalomonbc3d0de2014-12-15 13:45:03 -08001886 }
bsalomonbc3d0de2014-12-15 13:45:03 -08001887
Chris Dalton1d616352017-05-31 12:51:23 -06001888 struct {
1889 const GrBuffer* fBuffer;
1890 int fStride;
1891 size_t fBufferOffset;
1892 } bindings[2];
Chris Dalton8e45b4f2017-05-05 14:00:56 -04001893
Chris Dalton1d616352017-05-31 12:51:23 -06001894 if (int vertexStride = primProc.getVertexStride()) {
1895 SkASSERT(vertexBuffer && !vertexBuffer->isMapped());
1896 bindings[0].fBuffer = vertexBuffer;
1897 bindings[0].fStride = vertexStride;
1898 bindings[0].fBufferOffset = vertexBuffer->baseOffset() + baseVertex * vertexStride;
1899 }
1900 if (int instanceStride = primProc.getInstanceStride()) {
1901 SkASSERT(instanceBuffer && !instanceBuffer->isMapped());
1902 bindings[1].fBuffer = instanceBuffer;
1903 bindings[1].fStride = instanceStride;
1904 bindings[1].fBufferOffset = instanceBuffer->baseOffset() + baseInstance * instanceStride;
1905 }
bsalomonbc3d0de2014-12-15 13:45:03 -08001906
Chris Dalton1d616352017-05-31 12:51:23 -06001907 int numAttribs = primProc.numAttribs();
1908 attribState->enableVertexArrays(this, numAttribs);
bsalomonbc3d0de2014-12-15 13:45:03 -08001909
Chris Dalton1d616352017-05-31 12:51:23 -06001910 for (int i = 0; i < numAttribs; ++i) {
1911 using InputRate = GrPrimitiveProcessor::Attribute::InputRate;
1912 const GrGeometryProcessor::Attribute& attrib = primProc.getAttrib(i);
1913 const int divisor = InputRate::kPerInstance == attrib.fInputRate ? 1 : 0;
1914 const auto& binding = bindings[divisor];
1915 attribState->set(this, i, binding.fBuffer, attrib.fType, binding.fStride,
1916 binding.fBufferOffset + attrib.fOffsetInRecord, divisor);
bsalomonbc3d0de2014-12-15 13:45:03 -08001917 }
1918}
1919
csmartdalton485a1202016-07-13 10:16:32 -07001920GrGLenum GrGLGpu::bindBuffer(GrBufferType type, const GrBuffer* buffer) {
joshualitt93316b92015-10-23 09:08:08 -07001921 this->handleDirtyContext();
cdaltondeacc972016-04-06 14:26:33 -07001922
cdaltone2e71c22016-04-07 18:13:29 -07001923 // Index buffer state is tied to the vertex array.
1924 if (kIndex_GrBufferType == type) {
1925 this->bindVertexArray(0);
cdaltondeacc972016-04-06 14:26:33 -07001926 }
cdaltone2e71c22016-04-07 18:13:29 -07001927
1928 SkASSERT(type >= 0 && type <= kLast_GrBufferType);
1929 auto& bufferState = fHWBufferState[type];
1930
robertphillips8abb3702016-08-31 14:04:06 -07001931 if (buffer->uniqueID() != bufferState.fBoundBufferUniqueID) {
csmartdalton485a1202016-07-13 10:16:32 -07001932 if (buffer->isCPUBacked()) {
1933 if (!bufferState.fBufferZeroKnownBound) {
1934 GL_CALL(BindBuffer(bufferState.fGLTarget, 0));
1935 }
1936 } else {
1937 const GrGLBuffer* glBuffer = static_cast<const GrGLBuffer*>(buffer);
1938 GL_CALL(BindBuffer(bufferState.fGLTarget, glBuffer->bufferID()));
cdaltone2e71c22016-04-07 18:13:29 -07001939 }
csmartdalton485a1202016-07-13 10:16:32 -07001940 bufferState.fBufferZeroKnownBound = buffer->isCPUBacked();
robertphillips8abb3702016-08-31 14:04:06 -07001941 bufferState.fBoundBufferUniqueID = buffer->uniqueID();
cdaltone2e71c22016-04-07 18:13:29 -07001942 }
1943
1944 return bufferState.fGLTarget;
joshualitt93316b92015-10-23 09:08:08 -07001945}
1946
cdalton74b8d322016-04-11 14:47:28 -07001947void GrGLGpu::notifyBufferReleased(const GrGLBuffer* buffer) {
1948 if (buffer->hasAttachedToTexture()) {
1949 // Detach this buffer from any textures to ensure the underlying memory is freed.
Robert Phillips294870f2016-11-11 12:38:40 -05001950 GrGpuResource::UniqueID uniqueID = buffer->uniqueID();
cdalton74b8d322016-04-11 14:47:28 -07001951 for (int i = fHWMaxUsedBufferTextureUnit; i >= 0; --i) {
1952 auto& buffTex = fHWBufferTextures[i];
1953 if (uniqueID != buffTex.fAttachedBufferUniqueID) {
1954 continue;
1955 }
1956 if (i == fHWMaxUsedBufferTextureUnit) {
1957 --fHWMaxUsedBufferTextureUnit;
1958 }
1959
1960 this->setTextureUnit(i);
1961 if (!buffTex.fKnownBound) {
1962 SkASSERT(buffTex.fTextureID);
1963 GL_CALL(BindTexture(GR_GL_TEXTURE_BUFFER, buffTex.fTextureID));
1964 buffTex.fKnownBound = true;
1965 }
1966 GL_CALL(TexBuffer(GR_GL_TEXTURE_BUFFER,
1967 this->glCaps().configSizedInternalFormat(buffTex.fTexelConfig), 0));
1968 }
1969 }
1970}
1971
bsalomon861e1032014-12-16 07:33:49 -08001972void GrGLGpu::disableScissor() {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001973 if (kNo_TriState != fHWScissorSettings.fEnabled) {
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001974 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.coma3201942012-06-21 19:58:20 +00001975 fHWScissorSettings.fEnabled = kNo_TriState;
1976 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001977 }
1978}
1979
Robert Phillips19e51dc2017-08-09 09:30:51 -04001980void GrGLGpu::clear(const GrFixedClip& clip, GrColor color,
1981 GrRenderTarget* target, GrSurfaceOrigin origin) {
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001982 // parent class should never let us get here with no RT
bsalomon49f085d2014-09-05 13:34:00 -07001983 SkASSERT(target);
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001984
Brian Salomon43f8bf02017-10-18 08:33:29 -04001985 this->handleDirtyContext();
bsalomon@google.com74b98712011-11-11 19:46:16 +00001986
1987 GrGLfloat r, g, b, a;
1988 static const GrGLfloat scale255 = 1.f / 255.f;
1989 a = GrColorUnpackA(color) * scale255;
1990 GrGLfloat scaleRGB = scale255;
bsalomon@google.com74b98712011-11-11 19:46:16 +00001991 r = GrColorUnpackR(color) * scaleRGB;
1992 g = GrColorUnpackG(color) * scaleRGB;
1993 b = GrColorUnpackB(color) * scaleRGB;
1994
Brian Salomon43f8bf02017-10-18 08:33:29 -04001995 if (this->glCaps().useDrawToClearColor()) {
1996 this->clearColorAsDraw(clip, r, g, b, a, target, origin);
1997 return;
1998 }
1999
2000 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
2001
2002 this->flushRenderTarget(glRT, clip.scissorEnabled() ? &clip.scissorRect() : nullptr);
2003 this->flushScissor(clip.scissorState(), glRT->getViewport(), origin);
2004 this->flushWindowRectangles(clip.windowRectsState(), glRT, origin);
2005
bsalomon@google.com74b98712011-11-11 19:46:16 +00002006 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002007 fHWWriteToColor = kYes_TriState;
Brian Salomon028a9a52017-05-11 11:39:08 -04002008
Eric Karlaeaf22b2017-05-18 15:08:09 -07002009 if (this->glCaps().clearToBoundaryValuesIsBroken() &&
2010 (1 == r || 0 == r) && (1 == g || 0 == g) && (1 == b || 0 == b) && (1 == a || 0 == a)) {
Eric Karlaeaf22b2017-05-18 15:08:09 -07002011 static const GrGLfloat safeAlpha1 = nextafter(1.f, 2.f);
2012 static const GrGLfloat safeAlpha0 = nextafter(0.f, -1.f);
Eric Karlaeaf22b2017-05-18 15:08:09 -07002013 a = (1 == a) ? safeAlpha1 : safeAlpha0;
Brian Salomon028a9a52017-05-11 11:39:08 -04002014 }
bsalomon@google.com74b98712011-11-11 19:46:16 +00002015 GL_CALL(ClearColor(r, g, b, a));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002016 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00002017}
2018
Robert Phillips95214472017-08-08 18:00:03 -04002019void GrGLGpu::clearStencil(GrRenderTarget* target, int clearValue) {
2020 if (!target) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00002021 return;
2022 }
Robert Phillipscb2e2352017-08-30 16:44:40 -04002023
2024 GrStencilAttachment* sb = target->renderTargetPriv().getStencilAttachment();
2025 // this should only be called internally when we know we have a
2026 // stencil buffer.
2027 SkASSERT(sb);
2028
bsalomonb0bd4f62014-09-03 07:19:50 -07002029 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
egdanield803f272015-03-18 13:01:52 -07002030 this->flushRenderTarget(glRT, &SkIRect::EmptyIRect());
bsalomon@google.com8295dc12011-05-02 12:53:34 +00002031
joshualitt77b13072014-10-27 14:51:01 -07002032 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07002033 this->disableWindowRectangles();
robertphillips@google.com730ebe52012-04-16 16:33:13 +00002034
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002035 GL_CALL(StencilMask(0xffffffff));
Robert Phillips95214472017-08-08 18:00:03 -04002036 GL_CALL(ClearStencil(clearValue));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002037 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00002038 fHWStencilSettings.invalidate();
Robert Phillipscb2e2352017-08-30 16:44:40 -04002039 if (!clearValue) {
2040 sb->cleared();
2041 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002042}
2043
csmartdalton29df7602016-08-31 11:55:52 -07002044void GrGLGpu::clearStencilClip(const GrFixedClip& clip,
2045 bool insideStencilMask,
Robert Phillips19e51dc2017-08-09 09:30:51 -04002046 GrRenderTarget* target, GrSurfaceOrigin origin) {
bsalomon49f085d2014-09-05 13:34:00 -07002047 SkASSERT(target);
egdaniel9cb63402016-06-23 08:37:05 -07002048 this->handleDirtyContext();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00002049
Mike Klein31550db2017-06-06 23:29:53 +00002050 if (this->glCaps().useDrawToClearStencilClip()) {
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002051 this->clearStencilClipAsDraw(clip, insideStencilMask, target, origin);
Mike Klein31550db2017-06-06 23:29:53 +00002052 return;
2053 }
2054
egdaniel8dc7c3a2015-04-16 11:22:42 -07002055 GrStencilAttachment* sb = target->renderTargetPriv().getStencilAttachment();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00002056 // this should only be called internally when we know we have a
2057 // stencil buffer.
bsalomon6bc1b5f2015-02-23 09:06:38 -08002058 SkASSERT(sb);
2059 GrGLint stencilBitCount = sb->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002060#if 0
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002061 SkASSERT(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00002062 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002063#else
2064 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00002065 // ANGLE a partial stencil mask will cause clears to be
Robert Phillipsf2361d22016-10-25 14:20:06 -04002066 // turned into draws. Our contract on GrOpList says that
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002067 // changing the clip between stencil passes may or may not
2068 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00002069 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002070#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002071 GrGLint value;
csmartdalton29df7602016-08-31 11:55:52 -07002072 if (insideStencilMask) {
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002073 value = (1 << (stencilBitCount - 1));
2074 } else {
2075 value = 0;
2076 }
bsalomonb0bd4f62014-09-03 07:19:50 -07002077 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
egdanield803f272015-03-18 13:01:52 -07002078 this->flushRenderTarget(glRT, &SkIRect::EmptyIRect());
bsalomon@google.coma3201942012-06-21 19:58:20 +00002079
Robert Phillips19e51dc2017-08-09 09:30:51 -04002080 this->flushScissor(clip.scissorState(), glRT->getViewport(), origin);
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002081 this->flushWindowRectangles(clip.windowRectsState(), glRT, origin);
bsalomon@google.coma3201942012-06-21 19:58:20 +00002082
caryclark@google.comcf6285b2012-06-06 12:09:01 +00002083 GL_CALL(StencilMask((uint32_t) clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002084 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002085 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00002086 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00002087}
2088
Brian Salomon71d9d842016-11-03 13:42:00 -04002089static bool read_pixels_pays_for_y_flip(GrSurfaceOrigin origin, const GrGLCaps& caps,
bsalomon39826022015-07-23 08:07:21 -07002090 int width, int height, GrPixelConfig config,
2091 size_t rowBytes) {
Brian Salomon71d9d842016-11-03 13:42:00 -04002092 // If the surface is already TopLeft, we don't need to flip.
2093 if (kTopLeft_GrSurfaceOrigin == origin) {
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002094 return false;
2095 }
2096
bsalomon494aa592015-07-23 11:45:02 -07002097 // If the read is really small or smaller than the min texture size, don't force a draw.
bsalomon100b8f82015-10-28 08:37:44 -07002098 static const int kMinSize = 32;
2099 if (width < kMinSize || height < kMinSize) {
bsalomon494aa592015-07-23 11:45:02 -07002100 return false;
2101 }
2102
bsalomon@google.com56d11e02011-11-30 19:59:08 +00002103 // if GL can do the flip then we'll never pay for it.
bsalomon39826022015-07-23 08:07:21 -07002104 if (caps.packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00002105 return false;
2106 }
2107
2108 // If we have to do memcpy to handle non-trim rowBytes then we
bsalomon@google.com7107fa72011-11-10 14:54:14 +00002109 // get the flip for free. Otherwise it costs.
bsalomon39826022015-07-23 08:07:21 -07002110 // Note that we're assuming that 0 rowBytes has already been handled and that the width has been
2111 // clipped.
2112 return caps.packRowLengthSupport() || GrBytesPerPixel(config) * width == rowBytes;
2113}
2114
bsalomon1aa20292016-01-22 08:16:09 -08002115bool GrGLGpu::readPixelsSupported(GrRenderTarget* target, GrPixelConfig readConfig) {
Brian Salomon625cd9e2016-12-15 09:35:19 -05002116#ifdef SK_BUILD_FOR_MAC
2117 // Chromium may ask us to read back from locked IOSurfaces. Calling the command buffer's
2118 // glGetIntegerv() with GL_IMPLEMENTATION_COLOR_READ_FORMAT/_TYPE causes the command buffer
2119 // to make a call to check the framebuffer status which can hang the driver. So in Mac Chromium
2120 // we always use a temporary surface to test for read pixels support.
2121 // https://www.crbug.com/662802
2122 if (this->glContext().driver() == kChromium_GrGLDriver) {
2123 return this->readPixelsSupported(target->config(), readConfig);
2124 }
2125#endif
bsalomon1aa20292016-01-22 08:16:09 -08002126 auto bindRenderTarget = [this, target]() -> bool {
2127 this->flushRenderTarget(static_cast<GrGLRenderTarget*>(target), &SkIRect::EmptyIRect());
2128 return true;
2129 };
bsalomon2c3db322016-11-08 13:26:24 -08002130 auto unbindRenderTarget = []{};
bsalomon1aa20292016-01-22 08:16:09 -08002131 auto getIntegerv = [this](GrGLenum query, GrGLint* value) {
2132 GR_GL_GetIntegerv(this->glInterface(), query, value);
2133 };
2134 GrPixelConfig rtConfig = target->config();
bsalomon2c3db322016-11-08 13:26:24 -08002135 return this->glCaps().readPixelsSupported(rtConfig, readConfig, getIntegerv, bindRenderTarget,
2136 unbindRenderTarget);
bsalomon1aa20292016-01-22 08:16:09 -08002137}
2138
2139bool GrGLGpu::readPixelsSupported(GrPixelConfig rtConfig, GrPixelConfig readConfig) {
bsalomon2c3db322016-11-08 13:26:24 -08002140 sk_sp<GrTexture> temp;
2141 auto bindRenderTarget = [this, rtConfig, &temp]() -> bool {
Robert Phillips16d8ec62017-07-27 16:16:25 -04002142 GrSurfaceDesc desc;
bsalomon1aa20292016-01-22 08:16:09 -08002143 desc.fConfig = rtConfig;
2144 desc.fWidth = desc.fHeight = 16;
bsalomon2c3db322016-11-08 13:26:24 -08002145 if (this->glCaps().isConfigRenderable(rtConfig, false)) {
2146 desc.fFlags = kRenderTarget_GrSurfaceFlag;
Robert Phillips16d8ec62017-07-27 16:16:25 -04002147 desc.fOrigin = kBottomLeft_GrSurfaceOrigin;
Robert Phillips67d52cf2017-06-05 13:38:13 -04002148 temp = this->createTexture(desc, SkBudgeted::kNo);
bsalomon2c3db322016-11-08 13:26:24 -08002149 if (!temp) {
2150 return false;
2151 }
2152 GrGLRenderTarget* glrt = static_cast<GrGLRenderTarget*>(temp->asRenderTarget());
2153 this->flushRenderTarget(glrt, &SkIRect::EmptyIRect());
2154 return true;
2155 } else if (this->glCaps().canConfigBeFBOColorAttachment(rtConfig)) {
Robert Phillips16d8ec62017-07-27 16:16:25 -04002156 desc.fOrigin = kTopLeft_GrSurfaceOrigin;
Robert Phillips67d52cf2017-06-05 13:38:13 -04002157 temp = this->createTexture(desc, SkBudgeted::kNo);
bsalomon2c3db322016-11-08 13:26:24 -08002158 if (!temp) {
2159 return false;
2160 }
2161 GrGLIRect vp;
2162 this->bindSurfaceFBOForPixelOps(temp.get(), GR_GL_FRAMEBUFFER, &vp, kDst_TempFBOTarget);
Robert Phillips294870f2016-11-11 12:38:40 -05002163 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon2c3db322016-11-08 13:26:24 -08002164 return true;
bsalomon1aa20292016-01-22 08:16:09 -08002165 }
bsalomon2c3db322016-11-08 13:26:24 -08002166 return false;
2167 };
2168 auto unbindRenderTarget = [this, &temp]() {
2169 this->unbindTextureFBOForPixelOps(GR_GL_FRAMEBUFFER, temp.get());
bsalomon1aa20292016-01-22 08:16:09 -08002170 };
2171 auto getIntegerv = [this](GrGLenum query, GrGLint* value) {
2172 GR_GL_GetIntegerv(this->glInterface(), query, value);
2173 };
bsalomon2c3db322016-11-08 13:26:24 -08002174 return this->glCaps().readPixelsSupported(rtConfig, readConfig, getIntegerv, bindRenderTarget,
2175 unbindRenderTarget);
bsalomon1aa20292016-01-22 08:16:09 -08002176}
2177
2178bool GrGLGpu::readPixelsSupported(GrSurface* surfaceForConfig, GrPixelConfig readConfig) {
2179 if (GrRenderTarget* rt = surfaceForConfig->asRenderTarget()) {
2180 return this->readPixelsSupported(rt, readConfig);
2181 } else {
2182 GrPixelConfig config = surfaceForConfig->config();
2183 return this->readPixelsSupported(config, readConfig);
2184 }
2185}
2186
bsalomone9573312016-01-25 14:33:25 -08002187static bool requires_srgb_conversion(GrPixelConfig a, GrPixelConfig b) {
2188 if (GrPixelConfigIsSRGB(a)) {
2189 return !GrPixelConfigIsSRGB(b) && !GrPixelConfigIsAlphaOnly(b);
2190 } else if (GrPixelConfigIsSRGB(b)) {
2191 return !GrPixelConfigIsSRGB(a) && !GrPixelConfigIsAlphaOnly(a);
2192 }
2193 return false;
2194}
2195
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002196bool GrGLGpu::onGetReadPixelsInfo(GrSurface* srcSurface, GrSurfaceOrigin srcOrigin,
2197 int width, int height, size_t rowBytes,
bsalomonf0674512015-07-28 13:26:15 -07002198 GrPixelConfig readConfig, DrawPreference* drawPreference,
2199 ReadPixelTempDrawInfo* tempDrawInfo) {
bsalomone9573312016-01-25 14:33:25 -08002200 GrPixelConfig srcConfig = srcSurface->config();
bsalomon1aa20292016-01-22 08:16:09 -08002201
bsalomone9573312016-01-25 14:33:25 -08002202 // These settings we will always want if a temp draw is performed.
bsalomon39826022015-07-23 08:07:21 -07002203 tempDrawInfo->fTempSurfaceDesc.fFlags = kRenderTarget_GrSurfaceFlag;
2204 tempDrawInfo->fTempSurfaceDesc.fWidth = width;
2205 tempDrawInfo->fTempSurfaceDesc.fHeight = height;
2206 tempDrawInfo->fTempSurfaceDesc.fSampleCnt = 0;
2207 tempDrawInfo->fTempSurfaceDesc.fOrigin = kTopLeft_GrSurfaceOrigin; // no CPU y-flip for TL.
bsalomonb117ff12016-07-19 07:24:40 -07002208 tempDrawInfo->fTempSurfaceFit = this->glCaps().partialFBOReadIsSlow() ? SkBackingFit::kExact
2209 : SkBackingFit::kApprox;
bsalomone9573312016-01-25 14:33:25 -08002210 // For now assume no swizzling, we may change that below.
2211 tempDrawInfo->fSwizzle = GrSwizzle::RGBA();
2212
2213 // Depends on why we need/want a temp draw. Start off assuming no change, the surface we read
2214 // from will be srcConfig and we will read readConfig pixels from it.
Brian Osman33910292017-04-18 14:38:53 -04002215 // Note that if we require a draw and return a non-renderable format for the temp surface the
bsalomone9573312016-01-25 14:33:25 -08002216 // base class will fail for us.
2217 tempDrawInfo->fTempSurfaceDesc.fConfig = srcConfig;
2218 tempDrawInfo->fReadConfig = readConfig;
2219
2220 if (requires_srgb_conversion(srcConfig, readConfig)) {
2221 if (!this->readPixelsSupported(readConfig, readConfig)) {
2222 return false;
2223 }
2224 // Draw to do srgb to linear conversion or vice versa.
2225 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
2226 tempDrawInfo->fTempSurfaceDesc.fConfig = readConfig;
2227 tempDrawInfo->fReadConfig = readConfig;
2228 return true;
2229 }
2230
bsalomon1aa20292016-01-22 08:16:09 -08002231 if (this->glCaps().rgba8888PixelsOpsAreSlow() && kRGBA_8888_GrPixelConfig == readConfig &&
2232 this->readPixelsSupported(kBGRA_8888_GrPixelConfig, kBGRA_8888_GrPixelConfig)) {
bsalomon39826022015-07-23 08:07:21 -07002233 tempDrawInfo->fTempSurfaceDesc.fConfig = kBGRA_8888_GrPixelConfig;
bsalomon6c9cd552016-01-22 07:17:34 -08002234 tempDrawInfo->fSwizzle = GrSwizzle::BGRA();
2235 tempDrawInfo->fReadConfig = kBGRA_8888_GrPixelConfig;
bsalomonb411b3b2015-07-31 09:34:24 -07002236 ElevateDrawPreference(drawPreference, kGpuPrefersDraw_DrawPreference);
ericrkb4ecabd2016-03-11 15:18:20 -08002237 } else if (this->glCaps().rgbaToBgraReadbackConversionsAreSlow() &&
bsalomon39826022015-07-23 08:07:21 -07002238 GrBytesPerPixel(readConfig) == 4 &&
bsalomon1aa20292016-01-22 08:16:09 -08002239 GrPixelConfigSwapRAndB(readConfig) == srcConfig &&
2240 this->readPixelsSupported(srcSurface, srcConfig)) {
bsalomon6c9cd552016-01-22 07:17:34 -08002241 // Mesa 3D takes a slow path on when reading back BGRA from an RGBA surface and vice-versa.
bsalomon39826022015-07-23 08:07:21 -07002242 // Better to do a draw with a R/B swap and then read as the original config.
2243 tempDrawInfo->fTempSurfaceDesc.fConfig = srcConfig;
bsalomon6c9cd552016-01-22 07:17:34 -08002244 tempDrawInfo->fSwizzle = GrSwizzle::BGRA();
2245 tempDrawInfo->fReadConfig = srcConfig;
bsalomonf0674512015-07-28 13:26:15 -07002246 ElevateDrawPreference(drawPreference, kGpuPrefersDraw_DrawPreference);
bsalomon1aa20292016-01-22 08:16:09 -08002247 } else if (!this->readPixelsSupported(srcSurface, readConfig)) {
2248 if (readConfig == kBGRA_8888_GrPixelConfig &&
Brian Salomon71d9d842016-11-03 13:42:00 -04002249 this->glCaps().canConfigBeFBOColorAttachment(kRGBA_8888_GrPixelConfig) &&
bsalomon1aa20292016-01-22 08:16:09 -08002250 this->readPixelsSupported(kRGBA_8888_GrPixelConfig, kRGBA_8888_GrPixelConfig)) {
bsalomone9573312016-01-25 14:33:25 -08002251 // We're trying to read BGRA but it's not supported. If RGBA is renderable and
2252 // we can read it back, then do a swizzling draw to a RGBA and read it back (which
2253 // will effectively be BGRA).
bsalomon1aa20292016-01-22 08:16:09 -08002254 tempDrawInfo->fTempSurfaceDesc.fConfig = kRGBA_8888_GrPixelConfig;
2255 tempDrawInfo->fSwizzle = GrSwizzle::BGRA();
2256 tempDrawInfo->fReadConfig = kRGBA_8888_GrPixelConfig;
2257 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
brianosmana6359362016-03-21 06:55:37 -07002258 } else if (readConfig == kSBGRA_8888_GrPixelConfig &&
Brian Salomon71d9d842016-11-03 13:42:00 -04002259 this->glCaps().canConfigBeFBOColorAttachment(kSRGBA_8888_GrPixelConfig) &&
brianosmana6359362016-03-21 06:55:37 -07002260 this->readPixelsSupported(kSRGBA_8888_GrPixelConfig, kSRGBA_8888_GrPixelConfig)) {
2261 // We're trying to read sBGRA but it's not supported. If sRGBA is renderable and
2262 // we can read it back, then do a swizzling draw to a sRGBA and read it back (which
2263 // will effectively be sBGRA).
2264 tempDrawInfo->fTempSurfaceDesc.fConfig = kSRGBA_8888_GrPixelConfig;
2265 tempDrawInfo->fSwizzle = GrSwizzle::BGRA();
2266 tempDrawInfo->fReadConfig = kSRGBA_8888_GrPixelConfig;
2267 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
bsalomone9573312016-01-25 14:33:25 -08002268 } else if (readConfig == kAlpha_8_GrPixelConfig) {
2269 // onReadPixels implements a fallback for cases where we are want to read kAlpha_8,
2270 // it's unsupported, but 32bit RGBA reads are supported.
2271 // Don't attempt to do any srgb conversions since we only care about alpha.
2272 GrPixelConfig cpuTempConfig = kRGBA_8888_GrPixelConfig;
2273 if (GrPixelConfigIsSRGB(srcSurface->config())) {
2274 cpuTempConfig = kSRGBA_8888_GrPixelConfig;
2275 }
2276 if (!this->readPixelsSupported(srcSurface, cpuTempConfig)) {
2277 // If we can't read RGBA from the src try to draw to a kRGBA_8888 (or kSRGBA_8888)
2278 // first and then onReadPixels will read that to a 32bit temporary buffer.
Brian Salomon71d9d842016-11-03 13:42:00 -04002279 if (this->glCaps().canConfigBeFBOColorAttachment(cpuTempConfig)) {
bsalomone9573312016-01-25 14:33:25 -08002280 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
2281 tempDrawInfo->fTempSurfaceDesc.fConfig = cpuTempConfig;
2282 tempDrawInfo->fReadConfig = kAlpha_8_GrPixelConfig;
2283 } else {
2284 return false;
2285 }
2286 } else {
2287 SkASSERT(tempDrawInfo->fTempSurfaceDesc.fConfig == srcConfig);
2288 SkASSERT(tempDrawInfo->fReadConfig == kAlpha_8_GrPixelConfig);
2289 }
Stan Iliev0078ab22017-11-08 14:16:26 -05002290 } else if (readConfig == kRGBA_half_GrPixelConfig &&
2291 this->readPixelsSupported(srcSurface, kRGBA_float_GrPixelConfig)) {
2292 // If reading in half float format is not supported, then read in float format.
2293 return true;
Brian Salomon71d9d842016-11-03 13:42:00 -04002294 } else if (this->glCaps().canConfigBeFBOColorAttachment(readConfig) &&
bsalomone6d665e2016-03-10 07:22:25 -08002295 this->readPixelsSupported(readConfig, readConfig)) {
2296 // Do a draw to convert from the src config to the read config.
2297 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
2298 tempDrawInfo->fTempSurfaceDesc.fConfig = readConfig;
2299 tempDrawInfo->fReadConfig = readConfig;
bsalomon1aa20292016-01-22 08:16:09 -08002300 } else {
2301 return false;
2302 }
bsalomon39826022015-07-23 08:07:21 -07002303 }
2304
Brian Salomon71d9d842016-11-03 13:42:00 -04002305 if ((srcSurface->asRenderTarget() || this->glCaps().canConfigBeFBOColorAttachment(srcConfig)) &&
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002306 read_pixels_pays_for_y_flip(srcOrigin, this->glCaps(), width, height, readConfig,
Brian Salomon71d9d842016-11-03 13:42:00 -04002307 rowBytes)) {
bsalomonf0674512015-07-28 13:26:15 -07002308 ElevateDrawPreference(drawPreference, kGpuPrefersDraw_DrawPreference);
bsalomon39826022015-07-23 08:07:21 -07002309 }
2310
bsalomon39826022015-07-23 08:07:21 -07002311 return true;
bsalomon@google.comc4364992011-11-07 15:54:49 +00002312}
2313
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002314bool GrGLGpu::onReadPixels(GrSurface* surface, GrSurfaceOrigin origin,
bsalomon@google.comc6980972011-11-02 19:57:21 +00002315 int left, int top,
2316 int width, int height,
bsalomon@google.comc4364992011-11-07 15:54:49 +00002317 GrPixelConfig config,
2318 void* buffer,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002319 size_t rowBytes) {
bsalomon6cb3cbe2015-07-30 07:34:27 -07002320 SkASSERT(surface);
bsalomon39826022015-07-23 08:07:21 -07002321
bsalomone9573312016-01-25 14:33:25 -08002322 GrGLRenderTarget* renderTarget = static_cast<GrGLRenderTarget*>(surface->asRenderTarget());
Brian Salomon71d9d842016-11-03 13:42:00 -04002323 if (!renderTarget && !this->glCaps().canConfigBeFBOColorAttachment(surface->config())) {
bsalomon6cb3cbe2015-07-30 07:34:27 -07002324 return false;
2325 }
2326
bsalomon16921ec2015-07-30 15:34:56 -07002327 // OpenGL doesn't do sRGB <-> linear conversions when reading and writing pixels.
bsalomone9573312016-01-25 14:33:25 -08002328 if (requires_srgb_conversion(surface->config(), config)) {
2329 return false;
2330 }
2331
2332 // We have a special case fallback for reading eight bit alpha. We will read back all four 8
2333 // bit channels as RGBA and then extract A.
Brian Salomon71d9d842016-11-03 13:42:00 -04002334 if (!this->readPixelsSupported(surface, config)) {
bsalomone9573312016-01-25 14:33:25 -08002335 // Don't attempt to do any srgb conversions since we only care about alpha.
2336 GrPixelConfig tempConfig = kRGBA_8888_GrPixelConfig;
Brian Salomon71d9d842016-11-03 13:42:00 -04002337 if (GrPixelConfigIsSRGB(surface->config())) {
bsalomone9573312016-01-25 14:33:25 -08002338 tempConfig = kSRGBA_8888_GrPixelConfig;
2339 }
2340 if (kAlpha_8_GrPixelConfig == config &&
Brian Salomon71d9d842016-11-03 13:42:00 -04002341 this->readPixelsSupported(surface, tempConfig)) {
Ben Wagner7ecc5962016-11-02 17:07:33 -04002342 std::unique_ptr<uint32_t[]> temp(new uint32_t[width * height * 4]);
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002343 if (this->onReadPixels(surface, origin, left, top, width, height,
2344 tempConfig, temp.get(), width*4)) {
bsalomone9573312016-01-25 14:33:25 -08002345 uint8_t* dst = reinterpret_cast<uint8_t*>(buffer);
2346 for (int j = 0; j < height; ++j) {
2347 for (int i = 0; i < width; ++i) {
2348 dst[j*rowBytes + i] = (0xFF000000U & temp[j*width+i]) >> 24;
2349 }
2350 }
2351 return true;
2352 }
2353 }
Stan Iliev0078ab22017-11-08 14:16:26 -05002354
2355 // If reading in half float format is not supported, then read in a temporary float buffer
2356 // and convert to half float.
2357 if (kRGBA_half_GrPixelConfig == config &&
2358 this->readPixelsSupported(surface, kRGBA_float_GrPixelConfig)) {
2359 std::unique_ptr<float[]> temp(new float[width * height * 4]);
2360 if (this->onReadPixels(surface, origin, left, top, width, height,
2361 kRGBA_float_GrPixelConfig, temp.get(),
2362 width*sizeof(float)*4)) {
2363 uint8_t* dst = reinterpret_cast<uint8_t*>(buffer);
2364 float* src = temp.get();
2365 for (int j = 0; j < height; ++j) {
2366 SkHalf* dstRow = reinterpret_cast<SkHalf*>(dst);
2367 for (int i = 0; i < width; ++i) {
2368 for (int color = 0; color < 4; color++) {
2369 *dstRow++ = SkFloatToHalf(*src++);
2370 }
2371 }
2372 dst += rowBytes;
2373 }
2374 return true;
2375 }
2376 }
bsalomon16921ec2015-07-30 15:34:56 -07002377 return false;
2378 }
2379
bsalomon76148af2016-01-12 11:13:47 -08002380 GrGLenum externalFormat;
2381 GrGLenum externalType;
Brian Salomon71d9d842016-11-03 13:42:00 -04002382 if (!this->glCaps().getReadPixelsFormat(surface->config(), config, &externalFormat,
bsalomon76148af2016-01-12 11:13:47 -08002383 &externalType)) {
2384 return false;
2385 }
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002386 bool flipY = kBottomLeft_GrSurfaceOrigin == origin;
bsalomon@google.comc4364992011-11-07 15:54:49 +00002387
Brian Salomon71d9d842016-11-03 13:42:00 -04002388 GrGLIRect glvp;
2389 if (renderTarget) {
2390 // resolve the render target if necessary
2391 switch (renderTarget->getResolveType()) {
2392 case GrGLRenderTarget::kCantResolve_ResolveType:
2393 return false;
2394 case GrGLRenderTarget::kAutoResolves_ResolveType:
2395 this->flushRenderTarget(renderTarget, &SkIRect::EmptyIRect());
2396 break;
2397 case GrGLRenderTarget::kCanResolve_ResolveType:
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002398 this->onResolveRenderTarget(renderTarget, origin);
Brian Salomon71d9d842016-11-03 13:42:00 -04002399 // we don't track the state of the READ FBO ID.
2400 fStats.incRenderTargetBinds();
2401 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER, renderTarget->textureFBOID()));
2402 break;
2403 default:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -04002404 SK_ABORT("Unknown resolve type");
Brian Salomon71d9d842016-11-03 13:42:00 -04002405 }
2406 glvp = renderTarget->getViewport();
2407 } else {
2408 // Use a temporary FBO.
2409 this->bindSurfaceFBOForPixelOps(surface, GR_GL_FRAMEBUFFER, &glvp, kSrc_TempFBOTarget);
Robert Phillips294870f2016-11-11 12:38:40 -05002410 fHWBoundRenderTargetUniqueID.makeInvalid();
reed@google.comac10a2d2010-12-22 21:39:39 +00002411 }
2412
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00002413 // the read rect is viewport-relative
2414 GrGLIRect readRect;
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002415 readRect.setRelativeTo(glvp, left, top, width, height, origin);
rmistry@google.comfbfcd562012-08-23 18:09:54 +00002416
bsalomon9d02b262016-02-01 12:49:30 -08002417 size_t bytesPerPixel = GrBytesPerPixel(config);
2418 size_t tightRowBytes = bytesPerPixel * width;
egdaniel6d901da2015-07-30 12:02:15 -07002419
bsalomon@google.comc6980972011-11-02 19:57:21 +00002420 size_t readDstRowBytes = tightRowBytes;
2421 void* readDst = buffer;
rmistry@google.comfbfcd562012-08-23 18:09:54 +00002422
bsalomon@google.comc6980972011-11-02 19:57:21 +00002423 // determine if GL can read using the passed rowBytes or if we need
2424 // a scratch buffer.
joshualitt29f86792015-05-29 08:06:48 -07002425 SkAutoSMalloc<32 * sizeof(GrColor)> scratch;
bsalomon@google.comc6980972011-11-02 19:57:21 +00002426 if (rowBytes != tightRowBytes) {
bsalomon9d02b262016-02-01 12:49:30 -08002427 if (this->glCaps().packRowLengthSupport() && !(rowBytes % bytesPerPixel)) {
skia.committer@gmail.com4677acc2013-10-17 07:02:33 +00002428 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH,
bsalomon9d02b262016-02-01 12:49:30 -08002429 static_cast<GrGLint>(rowBytes / bytesPerPixel)));
bsalomon@google.comc6980972011-11-02 19:57:21 +00002430 readDstRowBytes = rowBytes;
2431 } else {
2432 scratch.reset(tightRowBytes * height);
2433 readDst = scratch.get();
2434 }
2435 }
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002436 if (flipY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00002437 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 1));
2438 }
bsalomonf46a1242015-12-15 12:37:38 -08002439 GL_CALL(PixelStorei(GR_GL_PACK_ALIGNMENT, config_alignment(config)));
2440
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002441 GL_CALL(ReadPixels(readRect.fLeft, readRect.fBottom,
2442 readRect.fWidth, readRect.fHeight,
bsalomon76148af2016-01-12 11:13:47 -08002443 externalFormat, externalType, readDst));
bsalomon@google.comc6980972011-11-02 19:57:21 +00002444 if (readDstRowBytes != tightRowBytes) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002445 SkASSERT(this->glCaps().packRowLengthSupport());
bsalomon@google.comc6980972011-11-02 19:57:21 +00002446 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
2447 }
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002448 if (flipY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00002449 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 0));
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002450 flipY = false;
bsalomon@google.com56d11e02011-11-30 19:59:08 +00002451 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002452
2453 // now reverse the order of the rows, since GL's are bottom-to-top, but our
bsalomon@google.comc6980972011-11-02 19:57:21 +00002454 // API presents top-to-bottom. We must preserve the padding contents. Note
2455 // that the above readPixels did not overwrite the padding.
2456 if (readDst == buffer) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002457 SkASSERT(rowBytes == readDstRowBytes);
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002458 if (flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002459 scratch.reset(tightRowBytes);
2460 void* tmpRow = scratch.get();
2461 // flip y in-place by rows
2462 const int halfY = height >> 1;
2463 char* top = reinterpret_cast<char*>(buffer);
2464 char* bottom = top + (height - 1) * rowBytes;
2465 for (int y = 0; y < halfY; y++) {
2466 memcpy(tmpRow, top, tightRowBytes);
2467 memcpy(top, bottom, tightRowBytes);
2468 memcpy(bottom, tmpRow, tightRowBytes);
2469 top += rowBytes;
2470 bottom -= rowBytes;
2471 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00002472 }
2473 } else {
bsalomon9d02b262016-02-01 12:49:30 -08002474 SkASSERT(readDst != buffer);
2475 SkASSERT(rowBytes != tightRowBytes);
bsalomon@google.comc6980972011-11-02 19:57:21 +00002476 // copy from readDst to buffer while flipping y
caryclark@google.comcf6285b2012-06-06 12:09:01 +00002477 // const int halfY = height >> 1;
bsalomon@google.comc6980972011-11-02 19:57:21 +00002478 const char* src = reinterpret_cast<const char*>(readDst);
bsalomon@google.comc4364992011-11-07 15:54:49 +00002479 char* dst = reinterpret_cast<char*>(buffer);
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002480 if (flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002481 dst += (height-1) * rowBytes;
2482 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00002483 for (int y = 0; y < height; y++) {
2484 memcpy(dst, src, tightRowBytes);
2485 src += readDstRowBytes;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002486 if (!flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002487 dst += rowBytes;
2488 } else {
2489 dst -= rowBytes;
2490 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002491 }
2492 }
Brian Salomon71d9d842016-11-03 13:42:00 -04002493 if (!renderTarget) {
2494 this->unbindTextureFBOForPixelOps(GR_GL_FRAMEBUFFER, surface);
2495 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002496 return true;
2497}
2498
Greg Daniel500d58b2017-08-24 15:59:33 -04002499GrGpuRTCommandBuffer* GrGLGpu::createCommandBuffer(
Robert Phillips19e51dc2017-08-09 09:30:51 -04002500 GrRenderTarget* rt, GrSurfaceOrigin origin,
Robert Phillips6b47c7d2017-08-29 07:24:09 -04002501 const GrGpuRTCommandBuffer::LoadAndStoreInfo& colorInfo,
Greg Daniel500d58b2017-08-24 15:59:33 -04002502 const GrGpuRTCommandBuffer::StencilLoadAndStoreInfo& stencilInfo) {
Robert Phillips6b47c7d2017-08-29 07:24:09 -04002503 return new GrGLGpuRTCommandBuffer(this, rt, origin, colorInfo, stencilInfo);
Greg Daniel500d58b2017-08-24 15:59:33 -04002504}
2505
2506GrGpuTextureCommandBuffer* GrGLGpu::createCommandBuffer(GrTexture* texture,
2507 GrSurfaceOrigin origin) {
2508 return new GrGLGpuTextureCommandBuffer(this, texture, origin);
egdaniel066df7c2016-06-08 14:02:27 -07002509}
2510
brianosman64d094d2016-03-25 06:01:59 -07002511void GrGLGpu::flushRenderTarget(GrGLRenderTarget* target, const SkIRect* bounds, bool disableSRGB) {
egdanield803f272015-03-18 13:01:52 -07002512 SkASSERT(target);
bsalomon6ba6fa12015-03-04 11:57:37 -08002513
Robert Phillips294870f2016-11-11 12:38:40 -05002514 GrGpuResource::UniqueID rtID = target->uniqueID();
egdanield803f272015-03-18 13:01:52 -07002515 if (fHWBoundRenderTargetUniqueID != rtID) {
bsalomon1e0bf7e2015-03-14 12:08:51 -07002516 fStats.incRenderTargetBinds();
egdanield803f272015-03-18 13:01:52 -07002517 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, target->renderFBOID()));
2518#ifdef SK_DEBUG
2519 // don't do this check in Chromium -- this is causing
2520 // lots of repeated command buffer flushes when the compositor is
2521 // rendering with Ganesh, which is really slow; even too slow for
2522 // Debug mode.
cdalton1acea862015-06-02 13:05:52 -07002523 if (kChromium_GrGLDriver != this->glContext().driver()) {
egdanield803f272015-03-18 13:01:52 -07002524 GrGLenum status;
2525 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
2526 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
2527 SkDebugf("GrGLGpu::flushRenderTarget glCheckFramebufferStatus %x\n", status);
2528 }
bsalomon160f24c2015-03-17 15:55:42 -07002529 }
egdanield803f272015-03-18 13:01:52 -07002530#endif
2531 fHWBoundRenderTargetUniqueID = rtID;
bsalomon083617b2016-02-12 12:10:14 -08002532 this->flushViewport(target->getViewport());
brianosman64d094d2016-03-25 06:01:59 -07002533 }
2534
brianosman35b784d2016-05-05 11:52:53 -07002535 if (this->glCaps().srgbWriteControl()) {
brianosman33f6b3f2016-06-02 05:49:21 -07002536 this->flushFramebufferSRGB(GrPixelConfigIsSRGB(target->config()) && !disableSRGB);
bsalomon5cd020f2015-03-17 12:46:56 -07002537 }
brianosman64d094d2016-03-25 06:01:59 -07002538
bsalomon083617b2016-02-12 12:10:14 -08002539 this->didWriteToSurface(target, bounds);
2540}
bsalomona9909122016-01-23 10:41:40 -08002541
brianosman33f6b3f2016-06-02 05:49:21 -07002542void GrGLGpu::flushFramebufferSRGB(bool enable) {
2543 if (enable && kYes_TriState != fHWSRGBFramebuffer) {
2544 GL_CALL(Enable(GR_GL_FRAMEBUFFER_SRGB));
2545 fHWSRGBFramebuffer = kYes_TriState;
2546 } else if (!enable && kNo_TriState != fHWSRGBFramebuffer) {
2547 GL_CALL(Disable(GR_GL_FRAMEBUFFER_SRGB));
2548 fHWSRGBFramebuffer = kNo_TriState;
2549 }
2550}
2551
bsalomon083617b2016-02-12 12:10:14 -08002552void GrGLGpu::flushViewport(const GrGLIRect& viewport) {
2553 if (fHWViewport != viewport) {
2554 viewport.pushToGLViewport(this->glInterface());
2555 fHWViewport = viewport;
2556 }
2557}
2558
bsalomon@google.comd302f142011-03-03 13:54:13 +00002559#define SWAP_PER_DRAW 0
2560
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00002561#if SWAP_PER_DRAW
commit-bot@chromium.org43823302013-09-25 20:57:51 +00002562 #if defined(SK_BUILD_FOR_MAC)
bsalomon@google.comd302f142011-03-03 13:54:13 +00002563 #include <AGL/agl.h>
commit-bot@chromium.org43823302013-09-25 20:57:51 +00002564 #elif defined(SK_BUILD_FOR_WIN32)
bsalomon@google.comce7357d2012-06-25 17:49:25 +00002565 #include <gl/GL.h>
bsalomon@google.comd302f142011-03-03 13:54:13 +00002566 void SwapBuf() {
2567 DWORD procID = GetCurrentProcessId();
2568 HWND hwnd = GetTopWindow(GetDesktopWindow());
2569 while(hwnd) {
2570 DWORD wndProcID = 0;
2571 GetWindowThreadProcessId(hwnd, &wndProcID);
2572 if(wndProcID == procID) {
2573 SwapBuffers(GetDC(hwnd));
2574 }
2575 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
2576 }
2577 }
2578 #endif
2579#endif
2580
egdaniel9cb63402016-06-23 08:37:05 -07002581void GrGLGpu::draw(const GrPipeline& pipeline,
2582 const GrPrimitiveProcessor& primProc,
bsalomon2eda5b32016-09-21 10:53:24 -07002583 const GrMesh meshes[],
Chris Dalton46983b72017-06-06 12:27:16 -06002584 const GrPipeline::DynamicState dynamicStates[],
egdaniel9cb63402016-06-23 08:37:05 -07002585 int meshCount) {
2586 this->handleDirtyContext();
2587
bsalomon2eda5b32016-09-21 10:53:24 -07002588 bool hasPoints = false;
2589 for (int i = 0; i < meshCount; ++i) {
Chris Dalton3809bab2017-06-13 10:55:06 -06002590 if (meshes[i].primitiveType() == GrPrimitiveType::kPoints) {
bsalomon2eda5b32016-09-21 10:53:24 -07002591 hasPoints = true;
2592 break;
2593 }
2594 }
2595 if (!this->flushGLState(pipeline, primProc, hasPoints)) {
bsalomond95263c2014-12-16 13:05:12 -08002596 return;
2597 }
ethannicholas22793252016-01-30 09:59:10 -08002598
egdaniel0e1853c2016-03-17 11:35:45 -07002599 for (int i = 0; i < meshCount; ++i) {
2600 if (GrXferBarrierType barrierType = pipeline.xferBarrierType(*this->caps())) {
Robert Phillips2890fbf2017-07-26 15:48:41 -04002601 this->xferBarrier(pipeline.renderTarget(), barrierType);
egdaniel0e1853c2016-03-17 11:35:45 -07002602 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002603
Chris Dalton46983b72017-06-06 12:27:16 -06002604 if (dynamicStates) {
2605 if (pipeline.getScissorState().enabled()) {
Robert Phillips2890fbf2017-07-26 15:48:41 -04002606 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(pipeline.renderTarget());
Chris Dalton46983b72017-06-06 12:27:16 -06002607 this->flushScissor(dynamicStates[i].fScissorRect,
Robert Phillips16d8ec62017-07-27 16:16:25 -04002608 glRT->getViewport(), pipeline.proxy()->origin());
Chris Dalton46983b72017-06-06 12:27:16 -06002609 }
2610 }
Brian Salomon6d9c88b2017-06-12 10:24:42 -04002611 if (this->glCaps().requiresCullFaceEnableDisableWhenDrawingLinesAfterNonLines() &&
2612 GrIsPrimTypeLines(meshes[i].primitiveType()) &&
2613 !GrIsPrimTypeLines(fLastPrimitiveType)) {
2614 GL_CALL(Enable(GR_GL_CULL_FACE));
2615 GL_CALL(Disable(GR_GL_CULL_FACE));
2616 }
Chris Dalton114a3c02017-05-26 15:17:19 -06002617 meshes[i].sendToGpu(primProc, this);
Brian Salomon6d9c88b2017-06-12 10:24:42 -04002618 fLastPrimitiveType = meshes[i].primitiveType();
bsalomon@google.com74749cd2013-01-30 16:12:41 +00002619 }
ethannicholas22793252016-01-30 09:59:10 -08002620
bsalomon@google.comd302f142011-03-03 13:54:13 +00002621#if SWAP_PER_DRAW
2622 glFlush();
commit-bot@chromium.org43823302013-09-25 20:57:51 +00002623 #if defined(SK_BUILD_FOR_MAC)
bsalomon@google.comd302f142011-03-03 13:54:13 +00002624 aglSwapBuffers(aglGetCurrentContext());
2625 int set_a_break_pt_here = 9;
2626 aglSwapBuffers(aglGetCurrentContext());
commit-bot@chromium.org43823302013-09-25 20:57:51 +00002627 #elif defined(SK_BUILD_FOR_WIN32)
bsalomon@google.comd302f142011-03-03 13:54:13 +00002628 SwapBuf();
2629 int set_a_break_pt_here = 9;
2630 SwapBuf();
2631 #endif
2632#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00002633}
2634
Chris Dalton3809bab2017-06-13 10:55:06 -06002635static GrGLenum gr_primitive_type_to_gl_mode(GrPrimitiveType primitiveType) {
2636 switch (primitiveType) {
2637 case GrPrimitiveType::kTriangles:
2638 return GR_GL_TRIANGLES;
2639 case GrPrimitiveType::kTriangleStrip:
2640 return GR_GL_TRIANGLE_STRIP;
2641 case GrPrimitiveType::kTriangleFan:
2642 return GR_GL_TRIANGLE_FAN;
2643 case GrPrimitiveType::kPoints:
2644 return GR_GL_POINTS;
2645 case GrPrimitiveType::kLines:
2646 return GR_GL_LINES;
2647 case GrPrimitiveType::kLineStrip:
2648 return GR_GL_LINE_STRIP;
2649 case GrPrimitiveType::kLinesAdjacency:
2650 return GR_GL_LINES_ADJACENCY;
2651 }
Ben Wagnerb4aab9a2017-08-16 10:53:04 -04002652 SK_ABORT("invalid GrPrimitiveType");
Chris Dalton3809bab2017-06-13 10:55:06 -06002653 return GR_GL_TRIANGLES;
2654}
2655
Chris Dalton114a3c02017-05-26 15:17:19 -06002656void GrGLGpu::sendMeshToGpu(const GrPrimitiveProcessor& primProc, GrPrimitiveType primitiveType,
2657 const GrBuffer* vertexBuffer, int vertexCount, int baseVertex) {
Chris Dalton3809bab2017-06-13 10:55:06 -06002658 const GrGLenum glPrimType = gr_primitive_type_to_gl_mode(primitiveType);
Chris Dalton114a3c02017-05-26 15:17:19 -06002659 if (this->glCaps().drawArraysBaseVertexIsBroken()) {
Chris Dalton1d616352017-05-31 12:51:23 -06002660 this->setupGeometry(primProc, nullptr, vertexBuffer, baseVertex, nullptr, 0);
Chris Dalton114a3c02017-05-26 15:17:19 -06002661 GL_CALL(DrawArrays(glPrimType, 0, vertexCount));
2662 } else {
Chris Dalton1d616352017-05-31 12:51:23 -06002663 this->setupGeometry(primProc, nullptr, vertexBuffer, 0, nullptr, 0);
Chris Dalton114a3c02017-05-26 15:17:19 -06002664 GL_CALL(DrawArrays(glPrimType, baseVertex, vertexCount));
2665 }
2666 fStats.incNumDraws();
2667}
2668
2669void GrGLGpu::sendIndexedMeshToGpu(const GrPrimitiveProcessor& primProc,
2670 GrPrimitiveType primitiveType, const GrBuffer* indexBuffer,
2671 int indexCount, int baseIndex, uint16_t minIndexValue,
2672 uint16_t maxIndexValue, const GrBuffer* vertexBuffer,
2673 int baseVertex) {
Chris Dalton3809bab2017-06-13 10:55:06 -06002674 const GrGLenum glPrimType = gr_primitive_type_to_gl_mode(primitiveType);
Chris Dalton114a3c02017-05-26 15:17:19 -06002675 GrGLvoid* const indices = reinterpret_cast<void*>(indexBuffer->baseOffset() +
2676 sizeof(uint16_t) * baseIndex);
2677
Chris Dalton1d616352017-05-31 12:51:23 -06002678 this->setupGeometry(primProc, indexBuffer, vertexBuffer, baseVertex, nullptr, 0);
Chris Dalton114a3c02017-05-26 15:17:19 -06002679
2680 if (this->glCaps().drawRangeElementsSupport()) {
2681 GL_CALL(DrawRangeElements(glPrimType, minIndexValue, maxIndexValue, indexCount,
2682 GR_GL_UNSIGNED_SHORT, indices));
2683 } else {
2684 GL_CALL(DrawElements(glPrimType, indexCount, GR_GL_UNSIGNED_SHORT, indices));
2685 }
2686 fStats.incNumDraws();
2687}
2688
Chris Dalton1d616352017-05-31 12:51:23 -06002689void GrGLGpu::sendInstancedMeshToGpu(const GrPrimitiveProcessor& primProc, GrPrimitiveType
2690 primitiveType, const GrBuffer* vertexBuffer,
2691 int vertexCount, int baseVertex,
2692 const GrBuffer* instanceBuffer, int instanceCount,
2693 int baseInstance) {
Chris Daltoncc604e52017-10-06 16:27:32 -06002694 GrGLenum glPrimType = gr_primitive_type_to_gl_mode(primitiveType);
2695 int maxInstances = this->glCaps().maxInstancesPerDrawArraysWithoutCrashing(instanceCount);
2696 for (int i = 0; i < instanceCount; i += maxInstances) {
2697 this->setupGeometry(primProc, nullptr, vertexBuffer, 0, instanceBuffer, baseInstance + i);
2698 GL_CALL(DrawArraysInstanced(glPrimType, baseVertex, vertexCount,
2699 SkTMin(instanceCount - i, maxInstances)));
2700 fStats.incNumDraws();
2701 }
Chris Dalton1d616352017-05-31 12:51:23 -06002702}
2703
2704void GrGLGpu::sendIndexedInstancedMeshToGpu(const GrPrimitiveProcessor& primProc,
2705 GrPrimitiveType primitiveType,
2706 const GrBuffer* indexBuffer, int indexCount,
2707 int baseIndex, const GrBuffer* vertexBuffer,
2708 int baseVertex, const GrBuffer* instanceBuffer,
2709 int instanceCount, int baseInstance) {
Chris Dalton3809bab2017-06-13 10:55:06 -06002710 const GrGLenum glPrimType = gr_primitive_type_to_gl_mode(primitiveType);
Chris Dalton1d616352017-05-31 12:51:23 -06002711 GrGLvoid* indices = reinterpret_cast<void*>(indexBuffer->baseOffset() +
2712 sizeof(uint16_t) * baseIndex);
2713 this->setupGeometry(primProc, indexBuffer, vertexBuffer, baseVertex,
2714 instanceBuffer, baseInstance);
2715 GL_CALL(DrawElementsInstanced(glPrimType, indexCount, GR_GL_UNSIGNED_SHORT, indices,
2716 instanceCount));
2717 fStats.incNumDraws();
2718}
2719
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002720void GrGLGpu::onResolveRenderTarget(GrRenderTarget* target, GrSurfaceOrigin origin) {
bsalomon@google.com75f9f252012-01-31 13:35:56 +00002721 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00002722 if (rt->needsResolve()) {
bsalomon@google.com347c3822013-05-01 20:10:01 +00002723 // Some extensions automatically resolves the texture when it is read.
2724 if (this->glCaps().usesMSAARenderBuffers()) {
egdanield803f272015-03-18 13:01:52 -07002725 SkASSERT(rt->textureFBOID() != rt->renderFBOID());
2726 fStats.incRenderTargetBinds();
2727 fStats.incRenderTargetBinds();
2728 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER, rt->renderFBOID()));
2729 GL_CALL(BindFramebuffer(GR_GL_DRAW_FRAMEBUFFER, rt->textureFBOID()));
2730 // make sure we go through flushRenderTarget() since we've modified
2731 // the bound DRAW FBO ID.
Robert Phillips294870f2016-11-11 12:38:40 -05002732 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002733 const GrGLIRect& vp = rt->getViewport();
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00002734 const SkIRect dirtyRect = rt->getResolveRect();
reed@google.comac10a2d2010-12-22 21:39:39 +00002735
bsalomon@google.com347c3822013-05-01 20:10:01 +00002736 if (GrGLCaps::kES_Apple_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002737 // Apple's extension uses the scissor as the blit bounds.
bsalomon3e791242014-12-17 13:43:13 -08002738 GrScissorState scissorState;
robertphillipse85a32d2015-02-10 08:16:55 -08002739 scissorState.set(dirtyRect);
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002740 this->flushScissor(scissorState, vp, origin);
csmartdalton28341fa2016-08-17 10:00:21 -07002741 this->disableWindowRectangles();
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002742 GL_CALL(ResolveMultisampleFramebuffer());
2743 } else {
Brian Salomone5e7eb12016-10-14 16:18:33 -04002744 int l, b, r, t;
2745 if (GrGLCaps::kResolveMustBeFull_BlitFrambufferFlag &
2746 this->glCaps().blitFramebufferSupportFlags()) {
2747 l = 0;
2748 b = 0;
2749 r = target->width();
2750 t = target->height();
2751 } else {
2752 GrGLIRect rect;
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002753 rect.setRelativeTo(vp, dirtyRect, origin);
Brian Salomone5e7eb12016-10-14 16:18:33 -04002754 l = rect.fLeft;
2755 b = rect.fBottom;
2756 r = rect.fLeft + rect.fWidth;
2757 t = rect.fBottom + rect.fHeight;
2758 }
derekf8c8f71a2014-09-16 06:24:57 -07002759
2760 // BlitFrameBuffer respects the scissor, so disable it.
joshualitt77b13072014-10-27 14:51:01 -07002761 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07002762 this->disableWindowRectangles();
Brian Salomone5e7eb12016-10-14 16:18:33 -04002763 GL_CALL(BlitFramebuffer(l, b, r, t, l, b, r, t,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002764 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00002765 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002766 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00002767 rt->flagAsResolved();
reed@google.comac10a2d2010-12-22 21:39:39 +00002768 }
2769}
2770
bsalomon@google.com411dad02012-06-05 20:24:20 +00002771namespace {
bsalomon@google.comd302f142011-03-03 13:54:13 +00002772
bsalomon@google.com411dad02012-06-05 20:24:20 +00002773
2774GrGLenum gr_to_gl_stencil_op(GrStencilOp op) {
cdalton93a379b2016-05-11 13:58:08 -07002775 static const GrGLenum gTable[kGrStencilOpCount] = {
2776 GR_GL_KEEP, // kKeep
2777 GR_GL_ZERO, // kZero
2778 GR_GL_REPLACE, // kReplace
2779 GR_GL_INVERT, // kInvert
2780 GR_GL_INCR_WRAP, // kIncWrap
2781 GR_GL_DECR_WRAP, // kDecWrap
2782 GR_GL_INCR, // kIncClamp
2783 GR_GL_DECR, // kDecClamp
bsalomon@google.com411dad02012-06-05 20:24:20 +00002784 };
cdalton93a379b2016-05-11 13:58:08 -07002785 GR_STATIC_ASSERT(0 == (int)GrStencilOp::kKeep);
2786 GR_STATIC_ASSERT(1 == (int)GrStencilOp::kZero);
2787 GR_STATIC_ASSERT(2 == (int)GrStencilOp::kReplace);
2788 GR_STATIC_ASSERT(3 == (int)GrStencilOp::kInvert);
2789 GR_STATIC_ASSERT(4 == (int)GrStencilOp::kIncWrap);
2790 GR_STATIC_ASSERT(5 == (int)GrStencilOp::kDecWrap);
2791 GR_STATIC_ASSERT(6 == (int)GrStencilOp::kIncClamp);
2792 GR_STATIC_ASSERT(7 == (int)GrStencilOp::kDecClamp);
2793 SkASSERT(op < (GrStencilOp)kGrStencilOpCount);
2794 return gTable[(int)op];
bsalomon@google.com411dad02012-06-05 20:24:20 +00002795}
2796
2797void set_gl_stencil(const GrGLInterface* gl,
cdalton93a379b2016-05-11 13:58:08 -07002798 const GrStencilSettings::Face& face,
2799 GrGLenum glFace) {
2800 GrGLenum glFunc = GrToGLStencilFunc(face.fTest);
2801 GrGLenum glFailOp = gr_to_gl_stencil_op(face.fFailOp);
2802 GrGLenum glPassOp = gr_to_gl_stencil_op(face.fPassOp);
bsalomon@google.coma3201942012-06-21 19:58:20 +00002803
cdalton93a379b2016-05-11 13:58:08 -07002804 GrGLint ref = face.fRef;
2805 GrGLint mask = face.fTestMask;
2806 GrGLint writeMask = face.fWriteMask;
bsalomon@google.com411dad02012-06-05 20:24:20 +00002807
2808 if (GR_GL_FRONT_AND_BACK == glFace) {
2809 // we call the combined func just in case separate stencil is not
2810 // supported.
2811 GR_GL_CALL(gl, StencilFunc(glFunc, ref, mask));
2812 GR_GL_CALL(gl, StencilMask(writeMask));
vbuzinovc5d58f02015-08-21 05:24:24 -07002813 GR_GL_CALL(gl, StencilOp(glFailOp, GR_GL_KEEP, glPassOp));
bsalomon@google.com411dad02012-06-05 20:24:20 +00002814 } else {
2815 GR_GL_CALL(gl, StencilFuncSeparate(glFace, glFunc, ref, mask));
2816 GR_GL_CALL(gl, StencilMaskSeparate(glFace, writeMask));
vbuzinovc5d58f02015-08-21 05:24:24 -07002817 GR_GL_CALL(gl, StencilOpSeparate(glFace, glFailOp, GR_GL_KEEP, glPassOp));
bsalomon@google.com411dad02012-06-05 20:24:20 +00002818 }
2819}
2820}
bsalomon@google.comd302f142011-03-03 13:54:13 +00002821
bsalomon3e791242014-12-17 13:43:13 -08002822void GrGLGpu::flushStencil(const GrStencilSettings& stencilSettings) {
csmartdaltonc7d85332016-10-26 10:13:46 -07002823 if (stencilSettings.isDisabled()) {
2824 this->disableStencil();
2825 } else if (fHWStencilSettings != stencilSettings) {
2826 if (kYes_TriState != fHWStencilTestEnabled) {
2827 GL_CALL(Enable(GR_GL_STENCIL_TEST));
Brian Salomonaf971de2017-06-08 16:11:33 -04002828
csmartdaltonc7d85332016-10-26 10:13:46 -07002829 fHWStencilTestEnabled = kYes_TriState;
bsalomon@google.coma3201942012-06-21 19:58:20 +00002830 }
csmartdaltonc7d85332016-10-26 10:13:46 -07002831 if (stencilSettings.isTwoSided()) {
csmartdaltonc7d85332016-10-26 10:13:46 -07002832 set_gl_stencil(this->glInterface(),
2833 stencilSettings.front(),
2834 GR_GL_FRONT);
2835 set_gl_stencil(this->glInterface(),
2836 stencilSettings.back(),
2837 GR_GL_BACK);
2838 } else {
2839 set_gl_stencil(this->glInterface(),
2840 stencilSettings.front(),
2841 GR_GL_FRONT_AND_BACK);
bsalomon@google.comd302f142011-03-03 13:54:13 +00002842 }
joshualitta58fe352014-10-27 08:39:00 -07002843 fHWStencilSettings = stencilSettings;
reed@google.comac10a2d2010-12-22 21:39:39 +00002844 }
2845}
2846
csmartdaltonc7d85332016-10-26 10:13:46 -07002847void GrGLGpu::disableStencil() {
2848 if (kNo_TriState != fHWStencilTestEnabled) {
2849 GL_CALL(Disable(GR_GL_STENCIL_TEST));
Brian Salomonaf971de2017-06-08 16:11:33 -04002850
csmartdaltonc7d85332016-10-26 10:13:46 -07002851 fHWStencilTestEnabled = kNo_TriState;
2852 fHWStencilSettings.invalidate();
2853 }
2854}
2855
cdaltonaf8bc7d2016-02-05 09:35:20 -08002856void GrGLGpu::flushHWAAState(GrRenderTarget* rt, bool useHWAA, bool stencilEnabled) {
bsalomon083617b2016-02-12 12:10:14 -08002857 // rt is only optional if useHWAA is false.
2858 SkASSERT(rt || !useHWAA);
vbuzinovdded6962015-06-12 08:59:45 -07002859 SkASSERT(!useHWAA || rt->isStencilBufferMultisampled());
bsalomon@google.com202d1392013-03-19 18:58:08 +00002860
csmartdalton2b5f2cb2016-06-10 14:06:32 -07002861 if (this->caps()->multisampleDisableSupport()) {
cdaltond0a840d2015-03-16 17:19:58 -07002862 if (useHWAA) {
2863 if (kYes_TriState != fMSAAEnabled) {
2864 GL_CALL(Enable(GR_GL_MULTISAMPLE));
2865 fMSAAEnabled = kYes_TriState;
2866 }
2867 } else {
2868 if (kNo_TriState != fMSAAEnabled) {
2869 GL_CALL(Disable(GR_GL_MULTISAMPLE));
2870 fMSAAEnabled = kNo_TriState;
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00002871 }
2872 }
2873 }
cdaltonaf8bc7d2016-02-05 09:35:20 -08002874
2875 if (0 != this->caps()->maxRasterSamples()) {
Brian Salomon7c8460e2017-05-12 11:36:10 -04002876 if (useHWAA && GrFSAAType::kMixedSamples == rt->fsaaType() && !stencilEnabled) {
cdaltonaf8bc7d2016-02-05 09:35:20 -08002877 // Since stencil is disabled and we want more samples than are in the color buffer, we
2878 // need to tell the rasterizer explicitly how many to run.
2879 if (kYes_TriState != fHWRasterMultisampleEnabled) {
2880 GL_CALL(Enable(GR_GL_RASTER_MULTISAMPLE));
2881 fHWRasterMultisampleEnabled = kYes_TriState;
2882 }
2883 if (rt->numStencilSamples() != fHWNumRasterSamples) {
2884 SkASSERT(rt->numStencilSamples() <= this->caps()->maxRasterSamples());
2885 GL_CALL(RasterSamples(rt->numStencilSamples(), GR_GL_TRUE));
2886 fHWNumRasterSamples = rt->numStencilSamples();
2887 }
2888 } else {
2889 if (kNo_TriState != fHWRasterMultisampleEnabled) {
2890 GL_CALL(Disable(GR_GL_RASTER_MULTISAMPLE));
2891 fHWRasterMultisampleEnabled = kNo_TriState;
2892 }
2893 }
2894 } else {
Brian Salomon7c8460e2017-05-12 11:36:10 -04002895 SkASSERT(!useHWAA || GrFSAAType::kMixedSamples != rt->fsaaType() || stencilEnabled);
cdaltonaf8bc7d2016-02-05 09:35:20 -08002896 }
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00002897}
2898
bsalomon7f9b2e42016-01-12 13:29:26 -08002899void GrGLGpu::flushBlend(const GrXferProcessor::BlendInfo& blendInfo, const GrSwizzle& swizzle) {
egdanielb414f252014-07-29 13:15:47 -07002900 // Any optimization to disable blending should have already been applied and
cdalton8917d622015-05-06 13:40:21 -07002901 // tweaked the equation to "add" or "subtract", and the coeffs to (1, 0).
bsalomonf7cc8772015-05-11 11:21:14 -07002902
cdalton8917d622015-05-06 13:40:21 -07002903 GrBlendEquation equation = blendInfo.fEquation;
egdanielc2304142014-12-11 13:15:13 -08002904 GrBlendCoeff srcCoeff = blendInfo.fSrcBlend;
2905 GrBlendCoeff dstCoeff = blendInfo.fDstBlend;
cdalton8917d622015-05-06 13:40:21 -07002906 bool blendOff = (kAdd_GrBlendEquation == equation || kSubtract_GrBlendEquation == equation) &&
2907 kOne_GrBlendCoeff == srcCoeff && kZero_GrBlendCoeff == dstCoeff;
egdanielb414f252014-07-29 13:15:47 -07002908 if (blendOff) {
2909 if (kNo_TriState != fHWBlendState.fEnabled) {
2910 GL_CALL(Disable(GR_GL_BLEND));
joel.liang9764c402015-07-09 19:46:18 -07002911
2912 // Workaround for the ARM KHR_blend_equation_advanced blacklist issue
2913 // https://code.google.com/p/skia/issues/detail?id=3943
2914 if (kARM_GrGLVendor == this->ctxInfo().vendor() &&
2915 GrBlendEquationIsAdvanced(fHWBlendState.fEquation)) {
2916 SkASSERT(this->caps()->advancedBlendEquationSupport());
2917 // Set to any basic blending equation.
2918 GrBlendEquation blend_equation = kAdd_GrBlendEquation;
2919 GL_CALL(BlendEquation(gXfermodeEquation2Blend[blend_equation]));
2920 fHWBlendState.fEquation = blend_equation;
2921 }
2922
egdanielb414f252014-07-29 13:15:47 -07002923 fHWBlendState.fEnabled = kNo_TriState;
2924 }
cdalton8917d622015-05-06 13:40:21 -07002925 return;
2926 }
2927
2928 if (kYes_TriState != fHWBlendState.fEnabled) {
2929 GL_CALL(Enable(GR_GL_BLEND));
Brian Salomonaf971de2017-06-08 16:11:33 -04002930
cdalton8917d622015-05-06 13:40:21 -07002931 fHWBlendState.fEnabled = kYes_TriState;
2932 }
2933
2934 if (fHWBlendState.fEquation != equation) {
2935 GL_CALL(BlendEquation(gXfermodeEquation2Blend[equation]));
2936 fHWBlendState.fEquation = equation;
2937 }
2938
2939 if (GrBlendEquationIsAdvanced(equation)) {
2940 SkASSERT(this->caps()->advancedBlendEquationSupport());
2941 // Advanced equations have no other blend state.
2942 return;
2943 }
2944
bsalomone63ffef2016-02-05 07:17:34 -08002945 if (fHWBlendState.fSrcCoeff != srcCoeff || fHWBlendState.fDstCoeff != dstCoeff) {
cdalton8917d622015-05-06 13:40:21 -07002946 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
2947 gXfermodeCoeff2Blend[dstCoeff]));
2948 fHWBlendState.fSrcCoeff = srcCoeff;
2949 fHWBlendState.fDstCoeff = dstCoeff;
2950 }
2951
bsalomon7f9b2e42016-01-12 13:29:26 -08002952 if ((BlendCoeffReferencesConstant(srcCoeff) || BlendCoeffReferencesConstant(dstCoeff))) {
2953 GrColor blendConst = blendInfo.fBlendConstant;
2954 blendConst = swizzle.applyTo(blendConst);
2955 if (!fHWBlendState.fConstColorValid || fHWBlendState.fConstColor != blendConst) {
2956 GrGLfloat c[4];
2957 GrColorToRGBAFloat(blendConst, c);
2958 GL_CALL(BlendColor(c[0], c[1], c[2], c[3]));
2959 fHWBlendState.fConstColor = blendConst;
2960 fHWBlendState.fConstColorValid = true;
2961 }
bsalomon@google.com0650e812011-04-08 18:07:53 +00002962 }
2963}
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002964
Brian Salomon2bbdcc42017-09-07 12:36:34 -04002965static inline GrGLenum wrap_mode_to_gl_wrap(GrSamplerState::WrapMode wrapMode) {
2966 switch (wrapMode) {
2967 case GrSamplerState::WrapMode::kClamp:
2968 return GR_GL_CLAMP_TO_EDGE;
2969 case GrSamplerState::WrapMode::kRepeat:
2970 return GR_GL_REPEAT;
2971 case GrSamplerState::WrapMode::kMirrorRepeat:
2972 return GR_GL_MIRRORED_REPEAT;
bsalomon@google.comb8670992012-07-25 21:27:09 +00002973 };
Brian Salomon2bbdcc42017-09-07 12:36:34 -04002974 SK_ABORT("Unknown wrap mode");
2975 return 0;
bsalomon@google.comb8670992012-07-25 21:27:09 +00002976}
2977
egdanielb7e7d572015-11-04 04:23:53 -08002978static GrGLenum get_component_enum_from_char(char component) {
2979 switch (component) {
2980 case 'r':
2981 return GR_GL_RED;
2982 case 'g':
2983 return GR_GL_GREEN;
2984 case 'b':
2985 return GR_GL_BLUE;
2986 case 'a':
2987 return GR_GL_ALPHA;
2988 default:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -04002989 SK_ABORT("Unsupported component");
egdanielb7e7d572015-11-04 04:23:53 -08002990 return 0;
2991 }
2992}
2993
2994/** If texture swizzling is available using tex parameters then it is preferred over mangling
2995 the generated shader code. This potentially allows greater reuse of cached shaders. */
2996static void get_tex_param_swizzle(GrPixelConfig config,
bsalomoncdee0092016-01-08 13:20:12 -08002997 const GrGLCaps& caps,
egdanielb7e7d572015-11-04 04:23:53 -08002998 GrGLenum* glSwizzle) {
bsalomoncdee0092016-01-08 13:20:12 -08002999 const GrSwizzle& swizzle = caps.configSwizzle(config);
egdanielb7e7d572015-11-04 04:23:53 -08003000 for (int i = 0; i < 4; ++i) {
bsalomoncdee0092016-01-08 13:20:12 -08003001 glSwizzle[i] = get_component_enum_from_char(swizzle.c_str()[i]);
egdaniel574a4c12015-11-02 06:22:44 -08003002 }
3003}
3004
Brian Salomon2bbdcc42017-09-07 12:36:34 -04003005static GrGLenum filter_to_gl_mag_filter(GrSamplerState::Filter filter) {
3006 switch (filter) {
3007 case GrSamplerState::Filter::kNearest:
3008 return GR_GL_NEAREST;
3009 case GrSamplerState::Filter::kBilerp:
3010 return GR_GL_LINEAR;
3011 case GrSamplerState::Filter::kMipMap:
3012 return GR_GL_LINEAR;
3013 }
3014 SK_ABORT("Unknown filter");
3015 return 0;
3016}
3017
3018static GrGLenum filter_to_gl_min_filter(GrSamplerState::Filter filter) {
3019 switch (filter) {
3020 case GrSamplerState::Filter::kNearest:
3021 return GR_GL_NEAREST;
3022 case GrSamplerState::Filter::kBilerp:
3023 return GR_GL_LINEAR;
3024 case GrSamplerState::Filter::kMipMap:
3025 return GR_GL_LINEAR_MIPMAP_LINEAR;
3026 }
3027 SK_ABORT("Unknown filter");
3028 return 0;
3029}
3030
3031void GrGLGpu::bindTexture(int unitIdx, const GrSamplerState& samplerState, bool allowSRGBInputs,
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003032 GrGLTexture* texture, GrSurfaceOrigin textureOrigin) {
bsalomon49f085d2014-09-05 13:34:00 -07003033 SkASSERT(texture);
tomhudson@google.comd0c1a062012-07-12 17:23:52 +00003034
reed856e9d92015-09-30 12:21:45 -07003035#ifdef SK_DEBUG
3036 if (!this->caps()->npotTextureTileSupport()) {
Brian Salomon2bbdcc42017-09-07 12:36:34 -04003037 if (samplerState.isRepeated()) {
reed856e9d92015-09-30 12:21:45 -07003038 const int w = texture->width();
3039 const int h = texture->height();
3040 SkASSERT(SkIsPow2(w) && SkIsPow2(h));
3041 }
3042 }
3043#endif
3044
bsalomon@google.comb8670992012-07-25 21:27:09 +00003045 // If we created a rt/tex and rendered to it without using a texture and now we're texturing
3046 // 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 +00003047 // out of the "last != next" check.
bsalomon37dd3312014-11-03 08:47:23 -08003048 GrGLRenderTarget* texRT = static_cast<GrGLRenderTarget*>(texture->asRenderTarget());
bsalomon49f085d2014-09-05 13:34:00 -07003049 if (texRT) {
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003050 this->onResolveRenderTarget(texRT, textureOrigin);
bsalomon@google.com4c883782012-06-04 19:05:11 +00003051 }
3052
Robert Phillips294870f2016-11-11 12:38:40 -05003053 GrGpuResource::UniqueID textureID = texture->uniqueID();
bsalomon10528f12015-10-14 12:54:52 -07003054 GrGLenum target = texture->target();
bsalomon1c63bf62014-07-22 13:09:46 -07003055 if (fHWBoundTextureUniqueIDs[unitIdx] != textureID) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00003056 this->setTextureUnit(unitIdx);
bsalomon10528f12015-10-14 12:54:52 -07003057 GL_CALL(BindTexture(target, texture->textureID()));
bsalomon1c63bf62014-07-22 13:09:46 -07003058 fHWBoundTextureUniqueIDs[unitIdx] = textureID;
bsalomon@google.com4c883782012-06-04 19:05:11 +00003059 }
3060
bsalomon@google.com4c883782012-06-04 19:05:11 +00003061 ResetTimestamp timestamp;
bsalomon@google.com34cccde2013-01-04 18:34:30 +00003062 const GrGLTexture::TexParams& oldTexParams = texture->getCachedTexParams(&timestamp);
bsalomon@google.com4c883782012-06-04 19:05:11 +00003063 bool setAll = timestamp < this->getResetTimestamp();
3064 GrGLTexture::TexParams newTexParams;
3065
Brian Salomon2bbdcc42017-09-07 12:36:34 -04003066 GrSamplerState::Filter filterMode = samplerState.filter();
bsalomonefd7d452014-10-23 14:17:46 -07003067
Brian Salomon2bbdcc42017-09-07 12:36:34 -04003068 if (GrSamplerState::Filter::kMipMap == filterMode) {
Robert Phillips92de6312017-05-23 07:43:48 -04003069 if (!this->caps()->mipMapSupport()) {
Brian Salomon2bbdcc42017-09-07 12:36:34 -04003070 filterMode = GrSamplerState::Filter::kBilerp;
bsalomonefd7d452014-10-23 14:17:46 -07003071 }
commit-bot@chromium.org47442312013-12-19 16:18:01 +00003072 }
bsalomonefd7d452014-10-23 14:17:46 -07003073
Brian Salomon2bbdcc42017-09-07 12:36:34 -04003074 newTexParams.fMinFilter = filter_to_gl_min_filter(filterMode);
3075 newTexParams.fMagFilter = filter_to_gl_mag_filter(filterMode);
skia.committer@gmail.comaeefb2a2013-07-27 07:01:06 +00003076
brianosman20471892016-12-02 06:43:32 -08003077 if (this->glCaps().srgbDecodeDisableSupport() && GrPixelConfigIsSRGB(texture->config())) {
brianosmanfe199872016-06-13 07:59:48 -07003078 newTexParams.fSRGBDecode = allowSRGBInputs ? GR_GL_DECODE_EXT : GR_GL_SKIP_DECODE_EXT;
brianosman33f6b3f2016-06-02 05:49:21 -07003079 if (setAll || newTexParams.fSRGBDecode != oldTexParams.fSRGBDecode) {
brianosmanadf4fa62016-04-26 16:48:06 -07003080 this->setTextureUnit(unitIdx);
brianosman33f6b3f2016-06-02 05:49:21 -07003081 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SRGB_DECODE_EXT, newTexParams.fSRGBDecode));
cblume55f2d2d2016-02-26 13:20:48 -08003082 }
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +00003083 }
bsalomon@google.com4c883782012-06-04 19:05:11 +00003084
brianosman33f6b3f2016-06-02 05:49:21 -07003085#ifdef SK_DEBUG
3086 // We were supposed to ensure MipMaps were up-to-date and built correctly before getting here.
Brian Salomon2bbdcc42017-09-07 12:36:34 -04003087 if (GrSamplerState::Filter::kMipMap == filterMode) {
brianosman33f6b3f2016-06-02 05:49:21 -07003088 SkASSERT(!texture->texturePriv().mipMapsAreDirty());
3089 if (GrPixelConfigIsSRGB(texture->config())) {
Brian Osman7b8400d2016-11-08 17:08:54 -05003090 SkDestinationSurfaceColorMode colorMode = allowSRGBInputs
3091 ? SkDestinationSurfaceColorMode::kGammaAndColorSpaceAware
3092 : SkDestinationSurfaceColorMode::kLegacy;
3093 SkASSERT(texture->texturePriv().mipColorMode() == colorMode);
brianosman33f6b3f2016-06-02 05:49:21 -07003094 }
3095 }
3096#endif
3097
cblume55f2d2d2016-02-26 13:20:48 -08003098 newTexParams.fMaxMipMapLevel = texture->texturePriv().maxMipMapLevel();
3099
Brian Salomon2bbdcc42017-09-07 12:36:34 -04003100 newTexParams.fWrapS = wrap_mode_to_gl_wrap(samplerState.wrapModeX());
3101 newTexParams.fWrapT = wrap_mode_to_gl_wrap(samplerState.wrapModeY());
bsalomoncdee0092016-01-08 13:20:12 -08003102 get_tex_param_swizzle(texture->config(), this->glCaps(), newTexParams.fSwizzleRGBA);
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00003103 if (setAll || newTexParams.fMagFilter != oldTexParams.fMagFilter) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00003104 this->setTextureUnit(unitIdx);
bsalomon10528f12015-10-14 12:54:52 -07003105 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MAG_FILTER, newTexParams.fMagFilter));
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00003106 }
3107 if (setAll || newTexParams.fMinFilter != oldTexParams.fMinFilter) {
3108 this->setTextureUnit(unitIdx);
bsalomon10528f12015-10-14 12:54:52 -07003109 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MIN_FILTER, newTexParams.fMinFilter));
bsalomon@google.com4c883782012-06-04 19:05:11 +00003110 }
cblume55f2d2d2016-02-26 13:20:48 -08003111 if (setAll || newTexParams.fMaxMipMapLevel != oldTexParams.fMaxMipMapLevel) {
cblume09bd2c02016-03-01 14:08:28 -08003112 // These are not supported in ES2 contexts
3113 if (this->glCaps().mipMapLevelAndLodControlSupport()) {
3114 if (newTexParams.fMaxMipMapLevel != 0) {
3115 this->setTextureUnit(unitIdx);
3116 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MIN_LOD, 0));
3117 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_BASE_LEVEL, 0));
3118 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MAX_LOD,
3119 newTexParams.fMaxMipMapLevel));
3120 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MAX_LEVEL,
3121 newTexParams.fMaxMipMapLevel));
3122 }
cblume55f2d2d2016-02-26 13:20:48 -08003123 }
3124 }
bsalomon@google.com4c883782012-06-04 19:05:11 +00003125 if (setAll || newTexParams.fWrapS != oldTexParams.fWrapS) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00003126 this->setTextureUnit(unitIdx);
bsalomon10528f12015-10-14 12:54:52 -07003127 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_WRAP_S, newTexParams.fWrapS));
bsalomon@google.com4c883782012-06-04 19:05:11 +00003128 }
3129 if (setAll || newTexParams.fWrapT != oldTexParams.fWrapT) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00003130 this->setTextureUnit(unitIdx);
bsalomon10528f12015-10-14 12:54:52 -07003131 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_WRAP_T, newTexParams.fWrapT));
bsalomon@google.com4c883782012-06-04 19:05:11 +00003132 }
bsalomoncdee0092016-01-08 13:20:12 -08003133 if (this->glCaps().textureSwizzleSupport() &&
bsalomon@google.com4c883782012-06-04 19:05:11 +00003134 (setAll || memcmp(newTexParams.fSwizzleRGBA,
3135 oldTexParams.fSwizzleRGBA,
3136 sizeof(newTexParams.fSwizzleRGBA)))) {
cdalton74b8d322016-04-11 14:47:28 -07003137 this->setTextureSwizzle(unitIdx, target, newTexParams.fSwizzleRGBA);
bsalomon@google.com4c883782012-06-04 19:05:11 +00003138 }
bsalomon@google.com34cccde2013-01-04 18:34:30 +00003139 texture->setCachedTexParams(newTexParams, this->getResetTimestamp());
bsalomon@google.com4c883782012-06-04 19:05:11 +00003140}
3141
csmartdalton1897cfd2016-06-03 08:50:54 -07003142void GrGLGpu::bindTexelBuffer(int unitIdx, GrPixelConfig texelConfig, GrGLBuffer* buffer) {
cdalton74b8d322016-04-11 14:47:28 -07003143 SkASSERT(this->glCaps().canUseConfigWithTexelBuffer(texelConfig));
3144 SkASSERT(unitIdx >= 0 && unitIdx < fHWBufferTextures.count());
cdalton74b8d322016-04-11 14:47:28 -07003145
3146 BufferTexture& buffTex = fHWBufferTextures[unitIdx];
3147
3148 if (!buffTex.fKnownBound) {
3149 if (!buffTex.fTextureID) {
3150 GL_CALL(GenTextures(1, &buffTex.fTextureID));
3151 if (!buffTex.fTextureID) {
3152 return;
3153 }
3154 }
3155
3156 this->setTextureUnit(unitIdx);
3157 GL_CALL(BindTexture(GR_GL_TEXTURE_BUFFER, buffTex.fTextureID));
3158
3159 buffTex.fKnownBound = true;
3160 }
3161
robertphillips8abb3702016-08-31 14:04:06 -07003162 if (buffer->uniqueID() != buffTex.fAttachedBufferUniqueID ||
csmartdalton1897cfd2016-06-03 08:50:54 -07003163 buffTex.fTexelConfig != texelConfig) {
cdalton74b8d322016-04-11 14:47:28 -07003164
3165 this->setTextureUnit(unitIdx);
csmartdalton1897cfd2016-06-03 08:50:54 -07003166 GL_CALL(TexBuffer(GR_GL_TEXTURE_BUFFER,
3167 this->glCaps().configSizedInternalFormat(texelConfig),
3168 buffer->bufferID()));
cdalton74b8d322016-04-11 14:47:28 -07003169
cdalton74b8d322016-04-11 14:47:28 -07003170 buffTex.fTexelConfig = texelConfig;
robertphillips8abb3702016-08-31 14:04:06 -07003171 buffTex.fAttachedBufferUniqueID = buffer->uniqueID();
cdalton74b8d322016-04-11 14:47:28 -07003172
3173 if (this->glCaps().textureSwizzleSupport() &&
3174 this->glCaps().configSwizzle(texelConfig) != buffTex.fSwizzle) {
3175 GrGLenum glSwizzle[4];
3176 get_tex_param_swizzle(texelConfig, this->glCaps(), glSwizzle);
3177 this->setTextureSwizzle(unitIdx, GR_GL_TEXTURE_BUFFER, glSwizzle);
3178 buffTex.fSwizzle = this->glCaps().configSwizzle(texelConfig);
3179 }
3180
3181 buffer->setHasAttachedToTexture();
3182 fHWMaxUsedBufferTextureUnit = SkTMax(unitIdx, fHWMaxUsedBufferTextureUnit);
3183 }
3184}
3185
Brian Salomon2bbdcc42017-09-07 12:36:34 -04003186void GrGLGpu::generateMipmaps(const GrSamplerState& params, bool allowSRGBInputs,
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003187 GrGLTexture* texture, GrSurfaceOrigin textureOrigin) {
brianosman33f6b3f2016-06-02 05:49:21 -07003188 SkASSERT(texture);
3189
3190 // First, figure out if we need mips for this texture at all:
Brian Salomon2bbdcc42017-09-07 12:36:34 -04003191 GrSamplerState::Filter filterMode = params.filter();
brianosman33f6b3f2016-06-02 05:49:21 -07003192
Brian Salomon2bbdcc42017-09-07 12:36:34 -04003193 if (GrSamplerState::Filter::kMipMap == filterMode) {
Robert Phillips92de6312017-05-23 07:43:48 -04003194 if (!this->caps()->mipMapSupport()) {
Brian Salomon2bbdcc42017-09-07 12:36:34 -04003195 filterMode = GrSamplerState::Filter::kBilerp;
brianosman33f6b3f2016-06-02 05:49:21 -07003196 }
3197 }
3198
Brian Salomon2bbdcc42017-09-07 12:36:34 -04003199 if (GrSamplerState::Filter::kMipMap != filterMode) {
brianosman33f6b3f2016-06-02 05:49:21 -07003200 return;
3201 }
3202
3203 // If this is an sRGB texture and the mips were previously built the "other" way
3204 // (gamma-correct vs. not), then we need to rebuild them. We don't need to check for
3205 // srgbSupport - we'll *never* get an sRGB pixel config if we don't support it.
Brian Osman7b8400d2016-11-08 17:08:54 -05003206 SkDestinationSurfaceColorMode colorMode = allowSRGBInputs
3207 ? SkDestinationSurfaceColorMode::kGammaAndColorSpaceAware
3208 : SkDestinationSurfaceColorMode::kLegacy;
brianosman33f6b3f2016-06-02 05:49:21 -07003209 if (GrPixelConfigIsSRGB(texture->config()) &&
Brian Osman7b8400d2016-11-08 17:08:54 -05003210 colorMode != texture->texturePriv().mipColorMode()) {
Greg Daniel0fc4d2d2017-10-12 11:23:36 -04003211 texture->texturePriv().markMipMapsDirty();
brianosman33f6b3f2016-06-02 05:49:21 -07003212 }
3213
3214 // If the mips aren't dirty, we're done:
3215 if (!texture->texturePriv().mipMapsAreDirty()) {
3216 return;
3217 }
3218
3219 // If we created a rt/tex and rendered to it without using a texture and now we're texturing
3220 // from the rt it will still be the last bound texture, but it needs resolving.
3221 GrGLRenderTarget* texRT = static_cast<GrGLRenderTarget*>(texture->asRenderTarget());
3222 if (texRT) {
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003223 this->onResolveRenderTarget(texRT, textureOrigin);
brianosman33f6b3f2016-06-02 05:49:21 -07003224 }
3225
3226 GrGLenum target = texture->target();
3227 this->setScratchTextureUnit();
3228 GL_CALL(BindTexture(target, texture->textureID()));
3229
3230 // Configure sRGB decode, if necessary. This state is the only thing needed for the driver
3231 // call (glGenerateMipmap) to work correctly. Our manual method dirties other state, too.
brianosman20471892016-12-02 06:43:32 -08003232 if (this->glCaps().srgbDecodeDisableSupport() && GrPixelConfigIsSRGB(texture->config())) {
Brian Osmanb0403202016-12-09 10:55:23 -05003233 GrGLenum srgbDecode = allowSRGBInputs ? GR_GL_DECODE_EXT : GR_GL_SKIP_DECODE_EXT;
3234 // Command buffer's sRGB decode extension doesn't influence mipmap generation correctly.
3235 // If we set this to skip_decode, it appears to suppress sRGB -> Linear for each downsample,
3236 // but not the Linear -> sRGB when writing the next level. The result is that mip-chains
3237 // get progressively brighter as you go down. Forcing this to 'decode' gives predictable
3238 // (and only slightly incorrect) results. See crbug.com/655247 (~comment 28)
3239 if (!this->glCaps().srgbDecodeDisableAffectsMipmaps()) {
3240 srgbDecode = GR_GL_DECODE_EXT;
3241 }
3242 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SRGB_DECODE_EXT, srgbDecode));
brianosman33f6b3f2016-06-02 05:49:21 -07003243 }
3244
3245 // Either do manual mipmap generation or (if that fails), just rely on the driver:
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003246 if (!this->generateMipmap(texture, textureOrigin, allowSRGBInputs)) {
brianosman33f6b3f2016-06-02 05:49:21 -07003247 GL_CALL(GenerateMipmap(target));
3248 }
3249
Greg Daniel0fc4d2d2017-10-12 11:23:36 -04003250 texture->texturePriv().markMipMapsClean();
brianosman33f6b3f2016-06-02 05:49:21 -07003251 texture->texturePriv().setMaxMipMapLevel(SkMipMap::ComputeLevelCount(
3252 texture->width(), texture->height()));
Brian Osman7b8400d2016-11-08 17:08:54 -05003253 texture->texturePriv().setMipColorMode(colorMode);
brianosman33f6b3f2016-06-02 05:49:21 -07003254
3255 // We have potentially set lots of state on the texture. Easiest to dirty it all:
3256 texture->textureParamsModified();
3257}
3258
cdalton74b8d322016-04-11 14:47:28 -07003259void GrGLGpu::setTextureSwizzle(int unitIdx, GrGLenum target, const GrGLenum swizzle[]) {
3260 this->setTextureUnit(unitIdx);
3261 if (this->glStandard() == kGLES_GrGLStandard) {
3262 // ES3 added swizzle support but not GL_TEXTURE_SWIZZLE_RGBA.
3263 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_R, swizzle[0]));
3264 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_G, swizzle[1]));
3265 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_B, swizzle[2]));
3266 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_A, swizzle[3]));
3267 } else {
3268 GR_STATIC_ASSERT(sizeof(swizzle[0]) == sizeof(GrGLint));
3269 GL_CALL(TexParameteriv(target, GR_GL_TEXTURE_SWIZZLE_RGBA,
3270 reinterpret_cast<const GrGLint*>(swizzle)));
3271 }
3272}
3273
egdaniel080e6732014-12-22 07:35:52 -08003274void GrGLGpu::flushColorWrite(bool writeColor) {
3275 if (!writeColor) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +00003276 if (kNo_TriState != fHWWriteToColor) {
3277 GL_CALL(ColorMask(GR_GL_FALSE, GR_GL_FALSE,
3278 GR_GL_FALSE, GR_GL_FALSE));
3279 fHWWriteToColor = kNo_TriState;
bsalomon@google.comd302f142011-03-03 13:54:13 +00003280 }
bsalomon@google.com978c8c62012-05-21 14:45:49 +00003281 } else {
3282 if (kYes_TriState != fHWWriteToColor) {
3283 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
3284 fHWWriteToColor = kYes_TriState;
3285 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00003286 }
bsalomon3e791242014-12-17 13:43:13 -08003287}
bsalomon@google.comd302f142011-03-03 13:54:13 +00003288
bsalomon861e1032014-12-16 07:33:49 -08003289void GrGLGpu::setTextureUnit(int unit) {
bsalomon1c63bf62014-07-22 13:09:46 -07003290 SkASSERT(unit >= 0 && unit < fHWBoundTextureUniqueIDs.count());
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00003291 if (unit != fHWActiveTextureUnitIdx) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00003292 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com49209392012-06-05 15:13:46 +00003293 fHWActiveTextureUnitIdx = unit;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00003294 }
3295}
bsalomon@google.com316f99232011-01-13 21:28:12 +00003296
bsalomon861e1032014-12-16 07:33:49 -08003297void GrGLGpu::setScratchTextureUnit() {
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00003298 // Bind the last texture unit since it is the least likely to be used by GrGLProgram.
bsalomon1c63bf62014-07-22 13:09:46 -07003299 int lastUnitIdx = fHWBoundTextureUniqueIDs.count() - 1;
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00003300 if (lastUnitIdx != fHWActiveTextureUnitIdx) {
3301 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + lastUnitIdx));
3302 fHWActiveTextureUnitIdx = lastUnitIdx;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00003303 }
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00003304 // clear out the this field so that if a program does use this unit it will rebind the correct
3305 // texture.
Robert Phillips294870f2016-11-11 12:38:40 -05003306 fHWBoundTextureUniqueIDs[lastUnitIdx].makeInvalid();
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00003307}
3308
Brian Salomone5e7eb12016-10-14 16:18:33 -04003309// Determines whether glBlitFramebuffer could be used between src and dst by onCopySurface.
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003310static inline bool can_blit_framebuffer_for_copy_surface(
3311 const GrSurface* dst, GrSurfaceOrigin dstOrigin,
3312 const GrSurface* src, GrSurfaceOrigin srcOrigin,
3313 const SkIRect& srcRect,
3314 const SkIPoint& dstPoint,
3315 const GrGLGpu* gpu) {
Brian Salomone5e7eb12016-10-14 16:18:33 -04003316 auto blitFramebufferFlags = gpu->glCaps().blitFramebufferSupportFlags();
Brian Salomon71d9d842016-11-03 13:42:00 -04003317 if (!gpu->glCaps().canConfigBeFBOColorAttachment(dst->config()) ||
3318 !gpu->glCaps().canConfigBeFBOColorAttachment(src->config())) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00003319 return false;
3320 }
Brian Salomonbf7b6202016-11-11 16:08:03 -05003321 // Blits are not allowed between int color buffers and float/fixed color buffers. GrGpu should
3322 // have filtered such cases out.
3323 SkASSERT(GrPixelConfigIsSint(dst->config()) == GrPixelConfigIsSint(src->config()));
Brian Salomone5e7eb12016-10-14 16:18:33 -04003324 const GrGLTexture* dstTex = static_cast<const GrGLTexture*>(dst->asTexture());
3325 const GrGLTexture* srcTex = static_cast<const GrGLTexture*>(dst->asTexture());
3326 const GrRenderTarget* dstRT = dst->asRenderTarget();
3327 const GrRenderTarget* srcRT = src->asRenderTarget();
3328 if (dstTex && dstTex->target() != GR_GL_TEXTURE_2D) {
3329 return false;
3330 }
3331 if (srcTex && srcTex->target() != GR_GL_TEXTURE_2D) {
3332 return false;
3333 }
3334 if (GrGLCaps::kNoSupport_BlitFramebufferFlag & blitFramebufferFlags) {
3335 return false;
3336 }
3337 if (GrGLCaps::kNoScalingOrMirroring_BlitFramebufferFlag & blitFramebufferFlags) {
3338 // We would mirror to compensate for origin changes. Note that copySurface is
3339 // specified such that the src and dst rects are the same.
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003340 if (dstOrigin != srcOrigin) {
Brian Salomone5e7eb12016-10-14 16:18:33 -04003341 return false;
3342 }
3343 }
3344 if (GrGLCaps::kResolveMustBeFull_BlitFrambufferFlag & blitFramebufferFlags) {
Eric Karl74480882017-04-03 14:49:05 -07003345 if (srcRT && srcRT->numColorSamples()) {
3346 if (dstRT && !dstRT->numColorSamples()) {
3347 return false;
3348 }
3349 if (SkRect::Make(srcRect) != srcRT->getBoundsRect()) {
3350 return false;
3351 }
Brian Salomone5e7eb12016-10-14 16:18:33 -04003352 }
3353 }
3354 if (GrGLCaps::kNoMSAADst_BlitFramebufferFlag & blitFramebufferFlags) {
3355 if (dstRT && dstRT->numColorSamples() > 0) {
3356 return false;
3357 }
Ben Wagner63fd7602017-10-09 15:45:33 -04003358 }
Brian Salomone5e7eb12016-10-14 16:18:33 -04003359 if (GrGLCaps::kNoFormatConversion_BlitFramebufferFlag & blitFramebufferFlags) {
3360 if (dst->config() != src->config()) {
3361 return false;
3362 }
3363 } else if (GrGLCaps::kNoFormatConversionForMSAASrc_BlitFramebufferFlag & blitFramebufferFlags) {
3364 const GrRenderTarget* srcRT = src->asRenderTarget();
3365 if (srcRT && srcRT->numColorSamples() && dst->config() != src->config()) {
3366 return false;
3367 }
3368 }
3369 if (GrGLCaps::kRectsMustMatchForMSAASrc_BlitFramebufferFlag & blitFramebufferFlags) {
Eric Karl74480882017-04-03 14:49:05 -07003370 if (srcRT && srcRT->numColorSamples()) {
3371 if (dstPoint.fX != srcRect.fLeft || dstPoint.fY != srcRect.fTop) {
3372 return false;
3373 }
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003374 if (dstOrigin != srcOrigin) {
Eric Karl74480882017-04-03 14:49:05 -07003375 return false;
3376 }
Brian Salomone5e7eb12016-10-14 16:18:33 -04003377 }
3378 }
3379 return true;
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00003380}
bsalomon@google.comeb851172013-04-15 13:51:00 +00003381
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003382static inline bool can_copy_texsubimage(const GrSurface* dst, GrSurfaceOrigin dstOrigin,
3383 const GrSurface* src, GrSurfaceOrigin srcOrigin,
bsalomon7ea33f52015-11-22 14:51:00 -08003384 const GrGLGpu* gpu) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00003385 // Table 3.9 of the ES2 spec indicates the supported formats with CopyTexSubImage
3386 // and BGRA isn't in the spec. There doesn't appear to be any extension that adds it. Perhaps
3387 // many drivers would allow it to work, but ANGLE does not.
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +00003388 if (kGLES_GrGLStandard == gpu->glStandard() && gpu->glCaps().bgraIsInternalFormat() &&
bsalomon@google.comeb851172013-04-15 13:51:00 +00003389 (kBGRA_8888_GrPixelConfig == dst->config() || kBGRA_8888_GrPixelConfig == src->config())) {
3390 return false;
3391 }
3392 const GrGLRenderTarget* dstRT = static_cast<const GrGLRenderTarget*>(dst->asRenderTarget());
3393 // If dst is multisampled (and uses an extension where there is a separate MSAA renderbuffer)
3394 // then we don't want to copy to the texture but to the MSAA buffer.
egdanield803f272015-03-18 13:01:52 -07003395 if (dstRT && dstRT->renderFBOID() != dstRT->textureFBOID()) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00003396 return false;
3397 }
bsalomon@google.coma2719852013-04-17 14:25:27 +00003398 const GrGLRenderTarget* srcRT = static_cast<const GrGLRenderTarget*>(src->asRenderTarget());
3399 // If the src is multisampled (and uses an extension where there is a separate MSAA
3400 // renderbuffer) then it is an invalid operation to call CopyTexSubImage
egdanield803f272015-03-18 13:01:52 -07003401 if (srcRT && srcRT->renderFBOID() != srcRT->textureFBOID()) {
bsalomon@google.coma2719852013-04-17 14:25:27 +00003402 return false;
3403 }
bsalomon7ea33f52015-11-22 14:51:00 -08003404
3405 const GrGLTexture* dstTex = static_cast<const GrGLTexture*>(dst->asTexture());
3406 // CopyTex(Sub)Image writes to a texture and we have no way of dynamically wrapping a RT in a
3407 // texture.
3408 if (!dstTex) {
3409 return false;
3410 }
3411
3412 const GrGLTexture* srcTex = static_cast<const GrGLTexture*>(src->asTexture());
cblume61214052016-01-26 09:10:48 -08003413
bsalomon7ea33f52015-11-22 14:51:00 -08003414 // Check that we could wrap the source in an FBO, that the dst is TEXTURE_2D, that no mirroring
3415 // is required.
Brian Salomon71d9d842016-11-03 13:42:00 -04003416 if (gpu->glCaps().canConfigBeFBOColorAttachment(src->config()) &&
Brian Salomon71d9d842016-11-03 13:42:00 -04003417 (!srcTex || srcTex->target() == GR_GL_TEXTURE_2D) && dstTex->target() == GR_GL_TEXTURE_2D &&
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003418 dstOrigin == srcOrigin) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00003419 return true;
3420 } else {
3421 return false;
3422 }
3423}
3424
3425// If a temporary FBO was created, its non-zero ID is returned. The viewport that the copy rect is
3426// relative to is output.
Brian Salomon71d9d842016-11-03 13:42:00 -04003427void GrGLGpu::bindSurfaceFBOForPixelOps(GrSurface* surface, GrGLenum fboTarget, GrGLIRect* viewport,
3428 TempFBOTarget tempFBOTarget) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00003429 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(surface->asRenderTarget());
bsalomon083617b2016-02-12 12:10:14 -08003430 if (!rt) {
bsalomon49f085d2014-09-05 13:34:00 -07003431 SkASSERT(surface->asTexture());
Brian Salomon9bada542017-06-12 12:09:30 -04003432 GrGLTexture* texture = static_cast<GrGLTexture*>(surface->asTexture());
3433 GrGLuint texID = texture->textureID();
3434 GrGLenum target = texture->target();
egdanield803f272015-03-18 13:01:52 -07003435 GrGLuint* tempFBOID;
3436 tempFBOID = kSrc_TempFBOTarget == tempFBOTarget ? &fTempSrcFBOID : &fTempDstFBOID;
egdaniel0f5f9672015-02-03 11:10:51 -08003437
egdanield803f272015-03-18 13:01:52 -07003438 if (0 == *tempFBOID) {
3439 GR_GL_CALL(this->glInterface(), GenFramebuffers(1, tempFBOID));
egdaniel0f5f9672015-02-03 11:10:51 -08003440 }
3441
egdanield803f272015-03-18 13:01:52 -07003442 fStats.incRenderTargetBinds();
3443 GR_GL_CALL(this->glInterface(), BindFramebuffer(fboTarget, *tempFBOID));
3444 GR_GL_CALL(this->glInterface(), FramebufferTexture2D(fboTarget,
robertphillips754f4e92014-09-18 13:52:08 -07003445 GR_GL_COLOR_ATTACHMENT0,
bsalomon10528f12015-10-14 12:54:52 -07003446 target,
robertphillips754f4e92014-09-18 13:52:08 -07003447 texID,
3448 0));
Brian Salomon9bada542017-06-12 12:09:30 -04003449 texture->baseLevelWasBoundToFBO();
bsalomon@google.comeb851172013-04-15 13:51:00 +00003450 viewport->fLeft = 0;
3451 viewport->fBottom = 0;
3452 viewport->fWidth = surface->width();
3453 viewport->fHeight = surface->height();
3454 } else {
egdanield803f272015-03-18 13:01:52 -07003455 fStats.incRenderTargetBinds();
3456 GR_GL_CALL(this->glInterface(), BindFramebuffer(fboTarget, rt->renderFBOID()));
bsalomon@google.comeb851172013-04-15 13:51:00 +00003457 *viewport = rt->getViewport();
3458 }
egdaniel0f5f9672015-02-03 11:10:51 -08003459}
3460
Brian Salomon71d9d842016-11-03 13:42:00 -04003461void GrGLGpu::unbindTextureFBOForPixelOps(GrGLenum fboTarget, GrSurface* surface) {
3462 // bindSurfaceFBOForPixelOps temporarily binds textures that are not render targets to
bsalomon10528f12015-10-14 12:54:52 -07003463 if (!surface->asRenderTarget()) {
3464 SkASSERT(surface->asTexture());
3465 GrGLenum textureTarget = static_cast<GrGLTexture*>(surface->asTexture())->target();
3466 GR_GL_CALL(this->glInterface(), FramebufferTexture2D(fboTarget,
3467 GR_GL_COLOR_ATTACHMENT0,
3468 textureTarget,
3469 0,
3470 0));
3471 }
bsalomon@google.comeb851172013-04-15 13:51:00 +00003472}
3473
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003474bool GrGLGpu::onCopySurface(GrSurface* dst, GrSurfaceOrigin dstOrigin,
3475 GrSurface* src, GrSurfaceOrigin srcOrigin,
3476 const SkIRect& srcRect, const SkIPoint& dstPoint) {
bsalomon7f9b2e42016-01-12 13:29:26 -08003477 // None of our copy methods can handle a swizzle. TODO: Make copySurfaceAsDraw handle the
3478 // swizzle.
Brian Salomon1edc5b92016-11-29 13:43:46 -05003479 if (this->caps()->shaderCaps()->configOutputSwizzle(src->config()) !=
3480 this->caps()->shaderCaps()->configOutputSwizzle(dst->config())) {
bsalomon7f9b2e42016-01-12 13:29:26 -08003481 return false;
3482 }
bsalomon083617b2016-02-12 12:10:14 -08003483 // Don't prefer copying as a draw if the dst doesn't already have a FBO object.
3484 bool preferCopy = SkToBool(dst->asRenderTarget());
3485 if (preferCopy && src->asTexture()) {
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003486 if (this->copySurfaceAsDraw(dst, dstOrigin, src, srcOrigin, srcRect, dstPoint)) {
cdaltone2e71c22016-04-07 18:13:29 -07003487 return true;
3488 }
bsalomon5df6fee2015-05-18 06:26:15 -07003489 }
cblume61214052016-01-26 09:10:48 -08003490
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003491 if (can_copy_texsubimage(dst, dstOrigin, src, srcOrigin, this)) {
3492 this->copySurfaceAsCopyTexSubImage(dst, dstOrigin, src, srcOrigin, srcRect, dstPoint);
bsalomon6df86402015-06-01 10:41:49 -07003493 return true;
3494 }
3495
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003496 if (can_blit_framebuffer_for_copy_surface(dst, dstOrigin, src, srcOrigin,
3497 srcRect, dstPoint, this)) {
3498 return this->copySurfaceAsBlitFramebuffer(dst, dstOrigin, src, srcOrigin,
3499 srcRect, dstPoint);
bsalomon6df86402015-06-01 10:41:49 -07003500 }
3501
bsalomon083617b2016-02-12 12:10:14 -08003502 if (!preferCopy && src->asTexture()) {
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003503 if (this->copySurfaceAsDraw(dst, dstOrigin, src, srcOrigin, srcRect, dstPoint)) {
cdaltone2e71c22016-04-07 18:13:29 -07003504 return true;
3505 }
bsalomon083617b2016-02-12 12:10:14 -08003506 }
3507
bsalomon6df86402015-06-01 10:41:49 -07003508 return false;
3509}
3510
Brian Salomonbf7b6202016-11-11 16:08:03 -05003511bool GrGLGpu::createCopyProgram(GrTexture* srcTex) {
Brian Osman39c08ac2017-07-26 09:36:09 -04003512 TRACE_EVENT0("skia", TRACE_FUNC);
Ryan Macnak38a10ad2017-07-10 10:36:34 -07003513
Brian Salomonbf7b6202016-11-11 16:08:03 -05003514 int progIdx = TextureToCopyProgramIdx(srcTex);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003515 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
Brian Salomonbf7b6202016-11-11 16:08:03 -05003516 GrSLType samplerType = srcTex->texturePriv().samplerType();
cdaltone2e71c22016-04-07 18:13:29 -07003517
3518 if (!fCopyProgramArrayBuffer) {
3519 static const GrGLfloat vdata[] = {
3520 0, 0,
3521 0, 1,
3522 1, 0,
3523 1, 1
3524 };
3525 fCopyProgramArrayBuffer.reset(GrGLBuffer::Create(this, sizeof(vdata), kVertex_GrBufferType,
3526 kStatic_GrAccessPattern, vdata));
3527 }
3528 if (!fCopyProgramArrayBuffer) {
3529 return false;
3530 }
3531
3532 SkASSERT(!fCopyPrograms[progIdx].fProgram);
3533 GL_CALL_RET(fCopyPrograms[progIdx].fProgram, CreateProgram());
3534 if (!fCopyPrograms[progIdx].fProgram) {
3535 return false;
3536 }
3537
Brian Salomon1edc5b92016-11-29 13:43:46 -05003538 const char* version = shaderCaps->versionDeclString();
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003539 GrShaderVar aVertex("a_vertex", kHalf2_GrSLType, GrShaderVar::kIn_TypeModifier);
3540 GrShaderVar uTexCoordXform("u_texCoordXform", kHalf4_GrSLType,
Brian Salomon99938a82016-11-21 13:41:08 -05003541 GrShaderVar::kUniform_TypeModifier);
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003542 GrShaderVar uPosXform("u_posXform", kHalf4_GrSLType, GrShaderVar::kUniform_TypeModifier);
Brian Salomon99938a82016-11-21 13:41:08 -05003543 GrShaderVar uTexture("u_texture", samplerType, GrShaderVar::kUniform_TypeModifier);
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003544 GrShaderVar vTexCoord("v_texCoord", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier);
3545 GrShaderVar oFragColor("o_FragColor", kHalf4_GrSLType, GrShaderVar::kOut_TypeModifier);
cdaltone2e71c22016-04-07 18:13:29 -07003546
3547 SkString vshaderTxt(version);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003548 if (shaderCaps->noperspectiveInterpolationSupport()) {
3549 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
cdaltone2e71c22016-04-07 18:13:29 -07003550 vshaderTxt.appendf("#extension %s : require\n", extension);
3551 }
3552 vTexCoord.addModifier("noperspective");
3553 }
3554
Brian Salomon1edc5b92016-11-29 13:43:46 -05003555 aVertex.appendDecl(shaderCaps, &vshaderTxt);
cdaltone2e71c22016-04-07 18:13:29 -07003556 vshaderTxt.append(";");
Brian Salomon1edc5b92016-11-29 13:43:46 -05003557 uTexCoordXform.appendDecl(shaderCaps, &vshaderTxt);
cdaltone2e71c22016-04-07 18:13:29 -07003558 vshaderTxt.append(";");
Brian Salomon1edc5b92016-11-29 13:43:46 -05003559 uPosXform.appendDecl(shaderCaps, &vshaderTxt);
cdaltone2e71c22016-04-07 18:13:29 -07003560 vshaderTxt.append(";");
Brian Salomon1edc5b92016-11-29 13:43:46 -05003561 vTexCoord.appendDecl(shaderCaps, &vshaderTxt);
cdaltone2e71c22016-04-07 18:13:29 -07003562 vshaderTxt.append(";");
3563
3564 vshaderTxt.append(
3565 "// Copy Program VS\n"
3566 "void main() {"
3567 " v_texCoord = a_vertex.xy * u_texCoordXform.xy + u_texCoordXform.zw;"
Ethan Nicholasbed683a2017-09-26 14:23:59 -04003568 " sk_Position.xy = a_vertex * u_posXform.xy + u_posXform.zw;"
3569 " sk_Position.zw = half2(0, 1);"
cdaltone2e71c22016-04-07 18:13:29 -07003570 "}"
3571 );
3572
3573 SkString fshaderTxt(version);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003574 if (shaderCaps->noperspectiveInterpolationSupport()) {
3575 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
cdaltone2e71c22016-04-07 18:13:29 -07003576 fshaderTxt.appendf("#extension %s : require\n", extension);
3577 }
3578 }
Brian Salomonbf7b6202016-11-11 16:08:03 -05003579 if (samplerType == kTextureExternalSampler_GrSLType) {
cdaltone2e71c22016-04-07 18:13:29 -07003580 fshaderTxt.appendf("#extension %s : require\n",
Brian Salomon1edc5b92016-11-29 13:43:46 -05003581 shaderCaps->externalTextureExtensionString());
cdaltone2e71c22016-04-07 18:13:29 -07003582 }
Brian Salomonf31ae492016-11-18 15:35:33 -05003583 vTexCoord.setTypeModifier(GrShaderVar::kIn_TypeModifier);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003584 vTexCoord.appendDecl(shaderCaps, &fshaderTxt);
cdaltone2e71c22016-04-07 18:13:29 -07003585 fshaderTxt.append(";");
Brian Salomon1edc5b92016-11-29 13:43:46 -05003586 uTexture.appendDecl(shaderCaps, &fshaderTxt);
cdaltone2e71c22016-04-07 18:13:29 -07003587 fshaderTxt.append(";");
cdaltone2e71c22016-04-07 18:13:29 -07003588 fshaderTxt.appendf(
3589 "// Copy Program FS\n"
3590 "void main() {"
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003591 " sk_FragColor = texture(u_texture, v_texCoord);"
3592 "}"
cdaltone2e71c22016-04-07 18:13:29 -07003593 );
3594
3595 const char* str;
3596 GrGLint length;
3597
3598 str = vshaderTxt.c_str();
3599 length = SkToInt(vshaderTxt.size());
Ethan Nicholas941e7e22016-12-12 15:33:30 -05003600 SkSL::Program::Settings settings;
3601 settings.fCaps = shaderCaps;
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003602 SkSL::String glsl;
3603 std::unique_ptr<SkSL::Program> program = GrSkSLtoGLSL(*fGLContext, GR_GL_VERTEX_SHADER,
3604 &str, &length, 1, settings, &glsl);
cdaltone2e71c22016-04-07 18:13:29 -07003605 GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fCopyPrograms[progIdx].fProgram,
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003606 GR_GL_VERTEX_SHADER, glsl.c_str(), glsl.size(),
3607 &fStats, settings);
3608 SkASSERT(program->fInputs.isEmpty());
cdaltone2e71c22016-04-07 18:13:29 -07003609
3610 str = fshaderTxt.c_str();
3611 length = SkToInt(fshaderTxt.size());
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003612 program = GrSkSLtoGLSL(*fGLContext, GR_GL_FRAGMENT_SHADER, &str, &length, 1, settings, &glsl);
cdaltone2e71c22016-04-07 18:13:29 -07003613 GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fCopyPrograms[progIdx].fProgram,
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003614 GR_GL_FRAGMENT_SHADER, glsl.c_str(), glsl.size(),
3615 &fStats, settings);
3616 SkASSERT(program->fInputs.isEmpty());
cdaltone2e71c22016-04-07 18:13:29 -07003617
3618 GL_CALL(LinkProgram(fCopyPrograms[progIdx].fProgram));
3619
3620 GL_CALL_RET(fCopyPrograms[progIdx].fTextureUniform,
3621 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_texture"));
3622 GL_CALL_RET(fCopyPrograms[progIdx].fPosXformUniform,
3623 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_posXform"));
3624 GL_CALL_RET(fCopyPrograms[progIdx].fTexCoordXformUniform,
3625 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_texCoordXform"));
3626
3627 GL_CALL(BindAttribLocation(fCopyPrograms[progIdx].fProgram, 0, "a_vertex"));
3628
3629 GL_CALL(DeleteShader(vshader));
3630 GL_CALL(DeleteShader(fshader));
3631
3632 return true;
bsalomon6df86402015-06-01 10:41:49 -07003633}
3634
brianosman33f6b3f2016-06-02 05:49:21 -07003635bool GrGLGpu::createMipmapProgram(int progIdx) {
3636 const bool oddWidth = SkToBool(progIdx & 0x2);
3637 const bool oddHeight = SkToBool(progIdx & 0x1);
3638 const int numTaps = (oddWidth ? 2 : 1) * (oddHeight ? 2 : 1);
3639
Brian Salomon1edc5b92016-11-29 13:43:46 -05003640 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
brianosman33f6b3f2016-06-02 05:49:21 -07003641
3642 SkASSERT(!fMipmapPrograms[progIdx].fProgram);
3643 GL_CALL_RET(fMipmapPrograms[progIdx].fProgram, CreateProgram());
3644 if (!fMipmapPrograms[progIdx].fProgram) {
3645 return false;
3646 }
3647
Brian Salomon1edc5b92016-11-29 13:43:46 -05003648 const char* version = shaderCaps->versionDeclString();
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003649 GrShaderVar aVertex("a_vertex", kHalf2_GrSLType, GrShaderVar::kIn_TypeModifier);
3650 GrShaderVar uTexCoordXform("u_texCoordXform", kHalf4_GrSLType,
Brian Salomon99938a82016-11-21 13:41:08 -05003651 GrShaderVar::kUniform_TypeModifier);
3652 GrShaderVar uTexture("u_texture", kTexture2DSampler_GrSLType,
3653 GrShaderVar::kUniform_TypeModifier);
brianosman33f6b3f2016-06-02 05:49:21 -07003654 // We need 1, 2, or 4 texture coordinates (depending on parity of each dimension):
Brian Salomon99938a82016-11-21 13:41:08 -05003655 GrShaderVar vTexCoords[] = {
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003656 GrShaderVar("v_texCoord0", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier),
3657 GrShaderVar("v_texCoord1", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier),
3658 GrShaderVar("v_texCoord2", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier),
3659 GrShaderVar("v_texCoord3", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier),
brianosman33f6b3f2016-06-02 05:49:21 -07003660 };
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003661 GrShaderVar oFragColor("o_FragColor", kHalf4_GrSLType,GrShaderVar::kOut_TypeModifier);
brianosman33f6b3f2016-06-02 05:49:21 -07003662
3663 SkString vshaderTxt(version);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003664 if (shaderCaps->noperspectiveInterpolationSupport()) {
3665 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
brianosman33f6b3f2016-06-02 05:49:21 -07003666 vshaderTxt.appendf("#extension %s : require\n", extension);
3667 }
3668 vTexCoords[0].addModifier("noperspective");
3669 vTexCoords[1].addModifier("noperspective");
3670 vTexCoords[2].addModifier("noperspective");
3671 vTexCoords[3].addModifier("noperspective");
3672 }
3673
Brian Salomon1edc5b92016-11-29 13:43:46 -05003674 aVertex.appendDecl(shaderCaps, &vshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003675 vshaderTxt.append(";");
Brian Salomon1edc5b92016-11-29 13:43:46 -05003676 uTexCoordXform.appendDecl(shaderCaps, &vshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003677 vshaderTxt.append(";");
3678 for (int i = 0; i < numTaps; ++i) {
Brian Salomon1edc5b92016-11-29 13:43:46 -05003679 vTexCoords[i].appendDecl(shaderCaps, &vshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003680 vshaderTxt.append(";");
3681 }
3682
3683 vshaderTxt.append(
3684 "// Mipmap Program VS\n"
3685 "void main() {"
Ethan Nicholasbed683a2017-09-26 14:23:59 -04003686 " sk_Position.xy = a_vertex * half2(2, 2) - half2(1, 1);"
3687 " sk_Position.zw = half2(0, 1);"
brianosman33f6b3f2016-06-02 05:49:21 -07003688 );
3689
3690 // Insert texture coordinate computation:
3691 if (oddWidth && oddHeight) {
3692 vshaderTxt.append(
3693 " v_texCoord0 = a_vertex.xy * u_texCoordXform.yw;"
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003694 " v_texCoord1 = a_vertex.xy * u_texCoordXform.yw + half2(u_texCoordXform.x, 0);"
3695 " v_texCoord2 = a_vertex.xy * u_texCoordXform.yw + half2(0, u_texCoordXform.z);"
brianosman33f6b3f2016-06-02 05:49:21 -07003696 " v_texCoord3 = a_vertex.xy * u_texCoordXform.yw + u_texCoordXform.xz;"
3697 );
3698 } else if (oddWidth) {
3699 vshaderTxt.append(
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003700 " v_texCoord0 = a_vertex.xy * half2(u_texCoordXform.y, 1);"
3701 " v_texCoord1 = a_vertex.xy * half2(u_texCoordXform.y, 1) + half2(u_texCoordXform.x, 0);"
brianosman33f6b3f2016-06-02 05:49:21 -07003702 );
3703 } else if (oddHeight) {
3704 vshaderTxt.append(
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003705 " v_texCoord0 = a_vertex.xy * half2(1, u_texCoordXform.w);"
3706 " v_texCoord1 = a_vertex.xy * half2(1, u_texCoordXform.w) + half2(0, u_texCoordXform.z);"
brianosman33f6b3f2016-06-02 05:49:21 -07003707 );
3708 } else {
3709 vshaderTxt.append(
3710 " v_texCoord0 = a_vertex.xy;"
3711 );
3712 }
3713
3714 vshaderTxt.append("}");
3715
3716 SkString fshaderTxt(version);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003717 if (shaderCaps->noperspectiveInterpolationSupport()) {
3718 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
brianosman33f6b3f2016-06-02 05:49:21 -07003719 fshaderTxt.appendf("#extension %s : require\n", extension);
3720 }
3721 }
brianosman33f6b3f2016-06-02 05:49:21 -07003722 for (int i = 0; i < numTaps; ++i) {
Brian Salomonf31ae492016-11-18 15:35:33 -05003723 vTexCoords[i].setTypeModifier(GrShaderVar::kIn_TypeModifier);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003724 vTexCoords[i].appendDecl(shaderCaps, &fshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003725 fshaderTxt.append(";");
3726 }
Brian Salomon1edc5b92016-11-29 13:43:46 -05003727 uTexture.appendDecl(shaderCaps, &fshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003728 fshaderTxt.append(";");
brianosman33f6b3f2016-06-02 05:49:21 -07003729 fshaderTxt.append(
3730 "// Mipmap Program FS\n"
3731 "void main() {"
3732 );
3733
3734 if (oddWidth && oddHeight) {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003735 fshaderTxt.append(
3736 " sk_FragColor = (texture(u_texture, v_texCoord0) + "
3737 " texture(u_texture, v_texCoord1) + "
3738 " texture(u_texture, v_texCoord2) + "
3739 " texture(u_texture, v_texCoord3)) * 0.25;"
brianosman33f6b3f2016-06-02 05:49:21 -07003740 );
3741 } else if (oddWidth || oddHeight) {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003742 fshaderTxt.append(
3743 " sk_FragColor = (texture(u_texture, v_texCoord0) + "
3744 " texture(u_texture, v_texCoord1)) * 0.5;"
brianosman33f6b3f2016-06-02 05:49:21 -07003745 );
3746 } else {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003747 fshaderTxt.append(
3748 " sk_FragColor = texture(u_texture, v_texCoord0);"
brianosman33f6b3f2016-06-02 05:49:21 -07003749 );
3750 }
3751
3752 fshaderTxt.append("}");
3753
3754 const char* str;
3755 GrGLint length;
3756
3757 str = vshaderTxt.c_str();
3758 length = SkToInt(vshaderTxt.size());
Ethan Nicholas941e7e22016-12-12 15:33:30 -05003759 SkSL::Program::Settings settings;
3760 settings.fCaps = shaderCaps;
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003761 SkSL::String glsl;
3762 std::unique_ptr<SkSL::Program> program = GrSkSLtoGLSL(*fGLContext, GR_GL_VERTEX_SHADER,
3763 &str, &length, 1, settings, &glsl);
brianosman33f6b3f2016-06-02 05:49:21 -07003764 GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fMipmapPrograms[progIdx].fProgram,
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003765 GR_GL_VERTEX_SHADER, glsl.c_str(), glsl.size(),
3766 &fStats, settings);
3767 SkASSERT(program->fInputs.isEmpty());
brianosman33f6b3f2016-06-02 05:49:21 -07003768
3769 str = fshaderTxt.c_str();
3770 length = SkToInt(fshaderTxt.size());
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003771 program = GrSkSLtoGLSL(*fGLContext, GR_GL_FRAGMENT_SHADER, &str, &length, 1, settings, &glsl);
brianosman33f6b3f2016-06-02 05:49:21 -07003772 GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fMipmapPrograms[progIdx].fProgram,
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003773 GR_GL_FRAGMENT_SHADER, glsl.c_str(), glsl.size(),
3774 &fStats, settings);
3775 SkASSERT(program->fInputs.isEmpty());
brianosman33f6b3f2016-06-02 05:49:21 -07003776
3777 GL_CALL(LinkProgram(fMipmapPrograms[progIdx].fProgram));
3778
3779 GL_CALL_RET(fMipmapPrograms[progIdx].fTextureUniform,
3780 GetUniformLocation(fMipmapPrograms[progIdx].fProgram, "u_texture"));
3781 GL_CALL_RET(fMipmapPrograms[progIdx].fTexCoordXformUniform,
3782 GetUniformLocation(fMipmapPrograms[progIdx].fProgram, "u_texCoordXform"));
3783
3784 GL_CALL(BindAttribLocation(fMipmapPrograms[progIdx].fProgram, 0, "a_vertex"));
3785
3786 GL_CALL(DeleteShader(vshader));
3787 GL_CALL(DeleteShader(fshader));
3788
3789 return true;
3790}
3791
Mike Klein31550db2017-06-06 23:29:53 +00003792bool GrGLGpu::createStencilClipClearProgram() {
Brian Osman39c08ac2017-07-26 09:36:09 -04003793 TRACE_EVENT0("skia", TRACE_FUNC);
Ryan Macnak38a10ad2017-07-10 10:36:34 -07003794
Mike Klein31550db2017-06-06 23:29:53 +00003795 if (!fStencilClipClearArrayBuffer) {
3796 static const GrGLfloat vdata[] = {-1, -1, 1, -1, -1, 1, 1, 1};
3797 fStencilClipClearArrayBuffer.reset(GrGLBuffer::Create(
3798 this, sizeof(vdata), kVertex_GrBufferType, kStatic_GrAccessPattern, vdata));
3799 if (!fStencilClipClearArrayBuffer) {
cdaltone2e71c22016-04-07 18:13:29 -07003800 return false;
3801 }
3802 }
3803
Mike Klein31550db2017-06-06 23:29:53 +00003804 SkASSERT(!fStencilClipClearProgram);
3805 GL_CALL_RET(fStencilClipClearProgram, CreateProgram());
3806 if (!fStencilClipClearProgram) {
cdaltone2e71c22016-04-07 18:13:29 -07003807 return false;
3808 }
3809
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003810 GrShaderVar aVertex("a_vertex", kHalf2_GrSLType, GrShaderVar::kIn_TypeModifier);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003811 const char* version = this->caps()->shaderCaps()->versionDeclString();
bsalomon6dea83f2015-12-03 12:58:06 -08003812
bsalomon6dea83f2015-12-03 12:58:06 -08003813 SkString vshaderTxt(version);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003814 aVertex.appendDecl(this->caps()->shaderCaps(), &vshaderTxt);
bsalomon6dea83f2015-12-03 12:58:06 -08003815 vshaderTxt.append(";");
bsalomon6dea83f2015-12-03 12:58:06 -08003816 vshaderTxt.append(
Mike Klein31550db2017-06-06 23:29:53 +00003817 "// Stencil Clip Clear Program VS\n"
3818 "void main() {"
Ethan Nicholasbed683a2017-09-26 14:23:59 -04003819 " sk_Position = float4(a_vertex.x, a_vertex.y, 0, 1);"
Mike Klein31550db2017-06-06 23:29:53 +00003820 "}");
bsalomon6dea83f2015-12-03 12:58:06 -08003821
3822 SkString fshaderTxt(version);
bsalomon6dea83f2015-12-03 12:58:06 -08003823 fshaderTxt.appendf(
Mike Klein31550db2017-06-06 23:29:53 +00003824 "// Stencil Clip Clear Program FS\n"
3825 "void main() {"
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003826 " sk_FragColor = half4(0);"
Mike Klein31550db2017-06-06 23:29:53 +00003827 "}");
bsalomon6dea83f2015-12-03 12:58:06 -08003828
bsalomon6dea83f2015-12-03 12:58:06 -08003829 const char* str;
3830 GrGLint length;
3831
3832 str = vshaderTxt.c_str();
3833 length = SkToInt(vshaderTxt.size());
Ethan Nicholas941e7e22016-12-12 15:33:30 -05003834 SkSL::Program::Settings settings;
3835 settings.fCaps = this->caps()->shaderCaps();
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003836 SkSL::String glsl;
3837 std::unique_ptr<SkSL::Program> program = GrSkSLtoGLSL(*fGLContext, GR_GL_VERTEX_SHADER,
3838 &str, &length, 1, settings, &glsl);
3839 GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fStencilClipClearProgram,
3840 GR_GL_VERTEX_SHADER, glsl.c_str(), glsl.size(),
3841 &fStats, settings);
3842 SkASSERT(program->fInputs.isEmpty());
bsalomon6dea83f2015-12-03 12:58:06 -08003843
3844 str = fshaderTxt.c_str();
3845 length = SkToInt(fshaderTxt.size());
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003846 program = GrSkSLtoGLSL(*fGLContext, GR_GL_FRAGMENT_SHADER, &str, &length, 1, settings, &glsl);
3847 GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fStencilClipClearProgram,
3848 GR_GL_FRAGMENT_SHADER, glsl.c_str(), glsl.size(),
3849 &fStats, settings);
3850 SkASSERT(program->fInputs.isEmpty());
bsalomon6dea83f2015-12-03 12:58:06 -08003851
Mike Klein31550db2017-06-06 23:29:53 +00003852 GL_CALL(LinkProgram(fStencilClipClearProgram));
bsalomon6dea83f2015-12-03 12:58:06 -08003853
Mike Klein31550db2017-06-06 23:29:53 +00003854 GL_CALL(BindAttribLocation(fStencilClipClearProgram, 0, "a_vertex"));
bsalomon6dea83f2015-12-03 12:58:06 -08003855
3856 GL_CALL(DeleteShader(vshader));
3857 GL_CALL(DeleteShader(fshader));
cdaltone2e71c22016-04-07 18:13:29 -07003858
3859 return true;
bsalomon6dea83f2015-12-03 12:58:06 -08003860}
3861
Mike Klein31550db2017-06-06 23:29:53 +00003862void GrGLGpu::clearStencilClipAsDraw(const GrFixedClip& clip, bool insideStencilMask,
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003863 GrRenderTarget* rt, GrSurfaceOrigin origin) {
bsalomon7f9b2e42016-01-12 13:29:26 -08003864 // TODO: This should swizzle the output to match dst's config, though it is a debugging
3865 // visualization.
3866
bsalomon6dea83f2015-12-03 12:58:06 -08003867 this->handleDirtyContext();
Mike Klein31550db2017-06-06 23:29:53 +00003868 if (!fStencilClipClearProgram) {
3869 if (!this->createStencilClipClearProgram()) {
3870 SkDebugf("Failed to create stencil clip clear program.\n");
cdaltone2e71c22016-04-07 18:13:29 -07003871 return;
3872 }
bsalomon6dea83f2015-12-03 12:58:06 -08003873 }
3874
Mike Klein8b06ed72017-06-06 21:47:32 +00003875 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(rt->asRenderTarget());
Mike Klein31550db2017-06-06 23:29:53 +00003876 this->flushRenderTarget(glRT, nullptr);
Mike Klein8b06ed72017-06-06 21:47:32 +00003877
Mike Klein31550db2017-06-06 23:29:53 +00003878 GL_CALL(UseProgram(fStencilClipClearProgram));
3879 fHWProgramID = fStencilClipClearProgram;
bsalomon6dea83f2015-12-03 12:58:06 -08003880
cdaltone2e71c22016-04-07 18:13:29 -07003881 fHWVertexArrayState.setVertexArrayID(this, 0);
bsalomon6dea83f2015-12-03 12:58:06 -08003882
cdaltone2e71c22016-04-07 18:13:29 -07003883 GrGLAttribArrayState* attribs = fHWVertexArrayState.bindInternalVertexArray(this);
Chris Dalton8e45b4f2017-05-05 14:00:56 -04003884 attribs->enableVertexArrays(this, 1);
Ethan Nicholasfa7ee242017-09-25 09:52:04 -04003885 attribs->set(this, 0, fStencilClipClearArrayBuffer.get(), kHalf2_GrVertexAttribType,
Hal Canary144caf52016-11-07 17:57:18 -05003886 2 * sizeof(GrGLfloat), 0);
bsalomon6dea83f2015-12-03 12:58:06 -08003887
bsalomon6dea83f2015-12-03 12:58:06 -08003888 GrXferProcessor::BlendInfo blendInfo;
3889 blendInfo.reset();
bsalomon7f9b2e42016-01-12 13:29:26 -08003890 this->flushBlend(blendInfo, GrSwizzle::RGBA());
Mike Klein31550db2017-06-06 23:29:53 +00003891 this->flushColorWrite(false);
cdaltonaf8bc7d2016-02-05 09:35:20 -08003892 this->flushHWAAState(glRT, false, false);
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003893 this->flushScissor(clip.scissorState(), glRT->getViewport(), origin);
3894 this->flushWindowRectangles(clip.windowRectsState(), glRT, origin);
Mike Klein31550db2017-06-06 23:29:53 +00003895 GrStencilAttachment* sb = rt->renderTargetPriv().getStencilAttachment();
3896 // This should only be called internally when we know we have a stencil buffer.
3897 SkASSERT(sb);
3898 GrStencilSettings settings = GrStencilSettings(
3899 *GrStencilSettings::SetClipBitSettings(insideStencilMask), false, sb->bits());
3900 this->flushStencil(settings);
3901 GL_CALL(DrawArrays(GR_GL_TRIANGLE_STRIP, 0, 4));
bsalomon6dea83f2015-12-03 12:58:06 -08003902}
3903
Brian Salomon43f8bf02017-10-18 08:33:29 -04003904bool GrGLGpu::createClearColorProgram() {
3905 TRACE_EVENT0("skia", TRACE_FUNC);
3906
3907 if (!fClearProgramArrayBuffer) {
3908 static const GrGLfloat vdata[] = {-1, -1, 1, -1, -1, 1, 1, 1};
3909 fClearProgramArrayBuffer.reset(GrGLBuffer::Create(this, sizeof(vdata), kVertex_GrBufferType,
3910 kStatic_GrAccessPattern, vdata));
3911 if (!fClearProgramArrayBuffer) {
3912 return false;
3913 }
3914 }
3915
3916 SkASSERT(!fClearColorProgram.fProgram);
3917 GL_CALL_RET(fClearColorProgram.fProgram, CreateProgram());
3918 if (!fClearColorProgram.fProgram) {
3919 return false;
3920 }
3921
3922 GrShaderVar aVertex("a_vertex", kHalf2_GrSLType, GrShaderVar::kIn_TypeModifier);
3923 const char* version = this->caps()->shaderCaps()->versionDeclString();
3924
3925 SkString vshaderTxt(version);
3926 aVertex.appendDecl(this->caps()->shaderCaps(), &vshaderTxt);
3927 vshaderTxt.append(";");
3928 vshaderTxt.append(R"(
3929 // Clear Color Program VS
3930 void main() {
3931 sk_Position = float4(a_vertex.x, a_vertex.y, 0, 1);
3932 })");
3933
3934 GrShaderVar uColor("u_color", kHalf4_GrSLType, GrShaderVar::kUniform_TypeModifier);
3935 SkString fshaderTxt(version);
3936 uColor.appendDecl(this->caps()->shaderCaps(), &fshaderTxt);
3937 fshaderTxt.append(";");
3938 fshaderTxt.appendf(R"(
3939 // Clear Color Program FS
3940 void main() {
3941 sk_FragColor = u_color;
3942 })");
3943
3944 const char* str;
3945 GrGLint length;
3946
3947 str = vshaderTxt.c_str();
3948 length = SkToInt(vshaderTxt.size());
3949 SkSL::Program::Settings settings;
3950 settings.fCaps = this->caps()->shaderCaps();
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003951 SkSL::String glsl;
3952 GrSkSLtoGLSL(*fGLContext, GR_GL_VERTEX_SHADER, &str, &length, 1, settings, &glsl);
Brian Salomon43f8bf02017-10-18 08:33:29 -04003953 GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fClearColorProgram.fProgram,
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003954 GR_GL_VERTEX_SHADER, glsl.c_str(), glsl.size(),
3955 &fStats, settings);
Brian Salomon43f8bf02017-10-18 08:33:29 -04003956
3957 str = fshaderTxt.c_str();
3958 length = SkToInt(fshaderTxt.size());
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003959 GrSkSLtoGLSL(*fGLContext, GR_GL_FRAGMENT_SHADER, &str, &length, 1, settings, &glsl);
Brian Salomon43f8bf02017-10-18 08:33:29 -04003960 GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fClearColorProgram.fProgram,
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003961 GR_GL_FRAGMENT_SHADER, glsl.c_str(), glsl.size(),
3962 &fStats, settings);
Brian Salomon43f8bf02017-10-18 08:33:29 -04003963
3964 GL_CALL(LinkProgram(fClearColorProgram.fProgram));
3965
3966 GL_CALL(BindAttribLocation(fClearColorProgram.fProgram, 0, "a_vertex"));
3967
3968 GL_CALL_RET(fClearColorProgram.fColorUniform,
3969 GetUniformLocation(fClearColorProgram.fProgram, "u_color"));
3970
3971 GL_CALL(DeleteShader(vshader));
3972 GL_CALL(DeleteShader(fshader));
3973
3974 return true;
3975}
3976
3977void GrGLGpu::clearColorAsDraw(const GrFixedClip& clip, GrGLfloat r, GrGLfloat g, GrGLfloat b,
3978 GrGLfloat a, GrRenderTarget* dst, GrSurfaceOrigin origin) {
3979 if (!fClearColorProgram.fProgram) {
3980 if (!this->createClearColorProgram()) {
3981 SkDebugf("Failed to create clear color program.\n");
3982 return;
3983 }
3984 }
3985
3986 GrGLIRect dstVP;
3987 this->bindSurfaceFBOForPixelOps(dst, GR_GL_FRAMEBUFFER, &dstVP, kDst_TempFBOTarget);
3988 this->flushViewport(dstVP);
3989 fHWBoundRenderTargetUniqueID.makeInvalid();
3990
3991 GL_CALL(UseProgram(fClearColorProgram.fProgram));
3992 fHWProgramID = fClearColorProgram.fProgram;
3993
3994 fHWVertexArrayState.setVertexArrayID(this, 0);
3995
3996 GrGLAttribArrayState* attribs = fHWVertexArrayState.bindInternalVertexArray(this);
3997 attribs->enableVertexArrays(this, 1);
3998 attribs->set(this, 0, fClearProgramArrayBuffer.get(), kHalf2_GrVertexAttribType,
3999 2 * sizeof(GrGLfloat), 0);
4000
4001 GrGLRenderTarget* glrt = static_cast<GrGLRenderTarget*>(dst);
4002 this->flushScissor(clip.scissorState(), glrt->getViewport(), origin);
4003 this->flushWindowRectangles(clip.windowRectsState(), glrt, origin);
4004
4005 GL_CALL(Uniform4f(fClearColorProgram.fColorUniform, r, g, b, a));
4006
4007 GrXferProcessor::BlendInfo blendInfo;
4008 blendInfo.reset();
4009 this->flushBlend(blendInfo, GrSwizzle::RGBA());
4010 this->flushColorWrite(true);
4011 this->flushHWAAState(nullptr, false, false);
4012 this->disableStencil();
4013 if (this->glCaps().srgbWriteControl()) {
4014 this->flushFramebufferSRGB(true);
4015 }
4016
4017 GL_CALL(DrawArrays(GR_GL_TRIANGLE_STRIP, 0, 4));
4018 this->unbindTextureFBOForPixelOps(GR_GL_FRAMEBUFFER, dst);
Brian Salomon62cbb672017-10-18 15:10:07 -04004019 this->didWriteToSurface(dst, clip.scissorEnabled() ? &clip.scissorRect() : nullptr);
Brian Salomon43f8bf02017-10-18 08:33:29 -04004020}
bsalomon6dea83f2015-12-03 12:58:06 -08004021
Robert Phillipsb0e93a22017-08-29 08:26:54 -04004022bool GrGLGpu::copySurfaceAsDraw(GrSurface* dst, GrSurfaceOrigin dstOrigin,
4023 GrSurface* src, GrSurfaceOrigin srcOrigin,
bsalomon6df86402015-06-01 10:41:49 -07004024 const SkIRect& srcRect,
4025 const SkIPoint& dstPoint) {
cdaltone2e71c22016-04-07 18:13:29 -07004026 GrGLTexture* srcTex = static_cast<GrGLTexture*>(src->asTexture());
Brian Salomonbf7b6202016-11-11 16:08:03 -05004027 int progIdx = TextureToCopyProgramIdx(srcTex);
cdaltone2e71c22016-04-07 18:13:29 -07004028
4029 if (!fCopyPrograms[progIdx].fProgram) {
Brian Salomonbf7b6202016-11-11 16:08:03 -05004030 if (!this->createCopyProgram(srcTex)) {
cdaltone2e71c22016-04-07 18:13:29 -07004031 SkDebugf("Failed to create copy program.\n");
4032 return false;
4033 }
4034 }
4035
bsalomon6df86402015-06-01 10:41:49 -07004036 int w = srcRect.width();
4037 int h = srcRect.height();
4038
Brian Salomon2bbdcc42017-09-07 12:36:34 -04004039 this->bindTexture(0, GrSamplerState::ClampNearest(), true, srcTex, srcOrigin);
bsalomon6df86402015-06-01 10:41:49 -07004040
bsalomon083617b2016-02-12 12:10:14 -08004041 GrGLIRect dstVP;
Brian Salomon71d9d842016-11-03 13:42:00 -04004042 this->bindSurfaceFBOForPixelOps(dst, GR_GL_FRAMEBUFFER, &dstVP, kDst_TempFBOTarget);
bsalomon083617b2016-02-12 12:10:14 -08004043 this->flushViewport(dstVP);
Robert Phillips294870f2016-11-11 12:38:40 -05004044 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon083617b2016-02-12 12:10:14 -08004045
bsalomon6df86402015-06-01 10:41:49 -07004046 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY, w, h);
bsalomon6df86402015-06-01 10:41:49 -07004047
bsalomon7ea33f52015-11-22 14:51:00 -08004048 GL_CALL(UseProgram(fCopyPrograms[progIdx].fProgram));
4049 fHWProgramID = fCopyPrograms[progIdx].fProgram;
bsalomon6df86402015-06-01 10:41:49 -07004050
cdaltone2e71c22016-04-07 18:13:29 -07004051 fHWVertexArrayState.setVertexArrayID(this, 0);
bsalomon6df86402015-06-01 10:41:49 -07004052
cdaltone2e71c22016-04-07 18:13:29 -07004053 GrGLAttribArrayState* attribs = fHWVertexArrayState.bindInternalVertexArray(this);
Chris Dalton8e45b4f2017-05-05 14:00:56 -04004054 attribs->enableVertexArrays(this, 1);
Ethan Nicholasfa7ee242017-09-25 09:52:04 -04004055 attribs->set(this, 0, fCopyProgramArrayBuffer.get(), kHalf2_GrVertexAttribType,
Hal Canary144caf52016-11-07 17:57:18 -05004056 2 * sizeof(GrGLfloat), 0);
bsalomon6df86402015-06-01 10:41:49 -07004057
4058 // dst rect edges in NDC (-1 to 1)
4059 int dw = dst->width();
4060 int dh = dst->height();
4061 GrGLfloat dx0 = 2.f * dstPoint.fX / dw - 1.f;
4062 GrGLfloat dx1 = 2.f * (dstPoint.fX + w) / dw - 1.f;
4063 GrGLfloat dy0 = 2.f * dstPoint.fY / dh - 1.f;
4064 GrGLfloat dy1 = 2.f * (dstPoint.fY + h) / dh - 1.f;
Robert Phillipsb0e93a22017-08-29 08:26:54 -04004065 if (kBottomLeft_GrSurfaceOrigin == dstOrigin) {
bsalomon6df86402015-06-01 10:41:49 -07004066 dy0 = -dy0;
4067 dy1 = -dy1;
4068 }
4069
bsalomone5286e02016-01-14 09:24:09 -08004070 GrGLfloat sx0 = (GrGLfloat)srcRect.fLeft;
4071 GrGLfloat sx1 = (GrGLfloat)(srcRect.fLeft + w);
4072 GrGLfloat sy0 = (GrGLfloat)srcRect.fTop;
4073 GrGLfloat sy1 = (GrGLfloat)(srcRect.fTop + h);
Ethan Nicholas5338f992017-04-19 15:54:07 -04004074 int sw = src->width();
bsalomon6df86402015-06-01 10:41:49 -07004075 int sh = src->height();
Robert Phillipsb0e93a22017-08-29 08:26:54 -04004076 if (kBottomLeft_GrSurfaceOrigin == srcOrigin) {
bsalomone5286e02016-01-14 09:24:09 -08004077 sy0 = sh - sy0;
4078 sy1 = sh - sy1;
4079 }
Ethan Nicholas5338f992017-04-19 15:54:07 -04004080 // src rect edges in normalized texture space (0 to 1)
4081 sx0 /= sw;
4082 sx1 /= sw;
4083 sy0 /= sh;
4084 sy1 /= sh;
bsalomon6df86402015-06-01 10:41:49 -07004085
bsalomon7ea33f52015-11-22 14:51:00 -08004086 GL_CALL(Uniform4f(fCopyPrograms[progIdx].fPosXformUniform, dx1 - dx0, dy1 - dy0, dx0, dy0));
4087 GL_CALL(Uniform4f(fCopyPrograms[progIdx].fTexCoordXformUniform,
4088 sx1 - sx0, sy1 - sy0, sx0, sy0));
4089 GL_CALL(Uniform1i(fCopyPrograms[progIdx].fTextureUniform, 0));
bsalomon6df86402015-06-01 10:41:49 -07004090
4091 GrXferProcessor::BlendInfo blendInfo;
4092 blendInfo.reset();
bsalomon7f9b2e42016-01-12 13:29:26 -08004093 this->flushBlend(blendInfo, GrSwizzle::RGBA());
bsalomon6df86402015-06-01 10:41:49 -07004094 this->flushColorWrite(true);
bsalomon083617b2016-02-12 12:10:14 -08004095 this->flushHWAAState(nullptr, false, false);
bsalomon6df86402015-06-01 10:41:49 -07004096 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07004097 this->disableWindowRectangles();
csmartdaltonc7d85332016-10-26 10:13:46 -07004098 this->disableStencil();
Brian Osmanf02fa6f2017-07-11 11:17:47 -04004099 if (this->glCaps().srgbWriteControl()) {
4100 this->flushFramebufferSRGB(true);
4101 }
bsalomon6df86402015-06-01 10:41:49 -07004102
4103 GL_CALL(DrawArrays(GR_GL_TRIANGLE_STRIP, 0, 4));
Brian Salomon71d9d842016-11-03 13:42:00 -04004104 this->unbindTextureFBOForPixelOps(GR_GL_FRAMEBUFFER, dst);
bsalomon083617b2016-02-12 12:10:14 -08004105 this->didWriteToSurface(dst, &dstRect);
4106
cdaltone2e71c22016-04-07 18:13:29 -07004107 return true;
bsalomon6df86402015-06-01 10:41:49 -07004108}
4109
Robert Phillipsb0e93a22017-08-29 08:26:54 -04004110void GrGLGpu::copySurfaceAsCopyTexSubImage(GrSurface* dst, GrSurfaceOrigin dstOrigin,
4111 GrSurface* src, GrSurfaceOrigin srcOrigin,
bsalomon6df86402015-06-01 10:41:49 -07004112 const SkIRect& srcRect,
4113 const SkIPoint& dstPoint) {
Robert Phillipsb0e93a22017-08-29 08:26:54 -04004114 SkASSERT(can_copy_texsubimage(dst, dstOrigin, src, srcOrigin, this));
bsalomon6df86402015-06-01 10:41:49 -07004115 GrGLIRect srcVP;
Brian Salomon71d9d842016-11-03 13:42:00 -04004116 this->bindSurfaceFBOForPixelOps(src, GR_GL_FRAMEBUFFER, &srcVP, kSrc_TempFBOTarget);
bsalomon083617b2016-02-12 12:10:14 -08004117 GrGLTexture* dstTex = static_cast<GrGLTexture *>(dst->asTexture());
bsalomon6df86402015-06-01 10:41:49 -07004118 SkASSERT(dstTex);
4119 // We modified the bound FBO
Robert Phillips294870f2016-11-11 12:38:40 -05004120 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon6df86402015-06-01 10:41:49 -07004121 GrGLIRect srcGLRect;
Robert Phillipsb0e93a22017-08-29 08:26:54 -04004122 srcGLRect.setRelativeTo(srcVP, srcRect, srcOrigin);
bsalomon6df86402015-06-01 10:41:49 -07004123
4124 this->setScratchTextureUnit();
bsalomon10528f12015-10-14 12:54:52 -07004125 GL_CALL(BindTexture(dstTex->target(), dstTex->textureID()));
bsalomon6df86402015-06-01 10:41:49 -07004126 GrGLint dstY;
Robert Phillipsb0e93a22017-08-29 08:26:54 -04004127 if (kBottomLeft_GrSurfaceOrigin == dstOrigin) {
bsalomon6df86402015-06-01 10:41:49 -07004128 dstY = dst->height() - (dstPoint.fY + srcGLRect.fHeight);
4129 } else {
4130 dstY = dstPoint.fY;
4131 }
bsalomon10528f12015-10-14 12:54:52 -07004132 GL_CALL(CopyTexSubImage2D(dstTex->target(), 0,
bsalomon083617b2016-02-12 12:10:14 -08004133 dstPoint.fX, dstY,
4134 srcGLRect.fLeft, srcGLRect.fBottom,
4135 srcGLRect.fWidth, srcGLRect.fHeight));
Brian Salomon71d9d842016-11-03 13:42:00 -04004136 this->unbindTextureFBOForPixelOps(GR_GL_FRAMEBUFFER, src);
bsalomon083617b2016-02-12 12:10:14 -08004137 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
4138 srcRect.width(), srcRect.height());
4139 this->didWriteToSurface(dst, &dstRect);
bsalomon6df86402015-06-01 10:41:49 -07004140}
4141
Robert Phillipsb0e93a22017-08-29 08:26:54 -04004142bool GrGLGpu::copySurfaceAsBlitFramebuffer(GrSurface* dst, GrSurfaceOrigin dstOrigin,
4143 GrSurface* src, GrSurfaceOrigin srcOrigin,
bsalomon6df86402015-06-01 10:41:49 -07004144 const SkIRect& srcRect,
4145 const SkIPoint& dstPoint) {
Robert Phillipsb0e93a22017-08-29 08:26:54 -04004146 SkASSERT(can_blit_framebuffer_for_copy_surface(dst, dstOrigin, src, srcOrigin,
4147 srcRect, dstPoint, this));
bsalomon6df86402015-06-01 10:41:49 -07004148 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
4149 srcRect.width(), srcRect.height());
4150 if (dst == src) {
4151 if (SkIRect::IntersectsNoEmptyCheck(dstRect, srcRect)) {
4152 return false;
mtklein404b3b22015-05-18 09:29:10 -07004153 }
bsalomon5df6fee2015-05-18 06:26:15 -07004154 }
bsalomon6df86402015-06-01 10:41:49 -07004155
bsalomon6df86402015-06-01 10:41:49 -07004156 GrGLIRect dstVP;
4157 GrGLIRect srcVP;
Brian Salomon71d9d842016-11-03 13:42:00 -04004158 this->bindSurfaceFBOForPixelOps(dst, GR_GL_DRAW_FRAMEBUFFER, &dstVP, kDst_TempFBOTarget);
4159 this->bindSurfaceFBOForPixelOps(src, GR_GL_READ_FRAMEBUFFER, &srcVP, kSrc_TempFBOTarget);
bsalomon6df86402015-06-01 10:41:49 -07004160 // We modified the bound FBO
Robert Phillips294870f2016-11-11 12:38:40 -05004161 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon6df86402015-06-01 10:41:49 -07004162 GrGLIRect srcGLRect;
4163 GrGLIRect dstGLRect;
Robert Phillipsb0e93a22017-08-29 08:26:54 -04004164 srcGLRect.setRelativeTo(srcVP, srcRect, srcOrigin);
4165 dstGLRect.setRelativeTo(dstVP, dstRect, dstOrigin);
bsalomon6df86402015-06-01 10:41:49 -07004166
4167 // BlitFrameBuffer respects the scissor, so disable it.
4168 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07004169 this->disableWindowRectangles();
bsalomon6df86402015-06-01 10:41:49 -07004170
4171 GrGLint srcY0;
4172 GrGLint srcY1;
4173 // Does the blit need to y-mirror or not?
Robert Phillipsb0e93a22017-08-29 08:26:54 -04004174 if (srcOrigin == dstOrigin) {
bsalomon6df86402015-06-01 10:41:49 -07004175 srcY0 = srcGLRect.fBottom;
4176 srcY1 = srcGLRect.fBottom + srcGLRect.fHeight;
4177 } else {
4178 srcY0 = srcGLRect.fBottom + srcGLRect.fHeight;
4179 srcY1 = srcGLRect.fBottom;
4180 }
4181 GL_CALL(BlitFramebuffer(srcGLRect.fLeft,
4182 srcY0,
4183 srcGLRect.fLeft + srcGLRect.fWidth,
4184 srcY1,
4185 dstGLRect.fLeft,
4186 dstGLRect.fBottom,
4187 dstGLRect.fLeft + dstGLRect.fWidth,
4188 dstGLRect.fBottom + dstGLRect.fHeight,
4189 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
Brian Salomon71d9d842016-11-03 13:42:00 -04004190 this->unbindTextureFBOForPixelOps(GR_GL_DRAW_FRAMEBUFFER, dst);
4191 this->unbindTextureFBOForPixelOps(GR_GL_READ_FRAMEBUFFER, src);
bsalomon083617b2016-02-12 12:10:14 -08004192 this->didWriteToSurface(dst, &dstRect);
bsalomon6df86402015-06-01 10:41:49 -07004193 return true;
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00004194}
4195
brianosman33f6b3f2016-06-02 05:49:21 -07004196// Manual implementation of mipmap generation, to work around driver bugs w/sRGB.
4197// Uses draw calls to do a series of downsample operations to successive mips.
4198// If this returns false, then the calling code falls back to using glGenerateMipmap.
Robert Phillipsb0e93a22017-08-29 08:26:54 -04004199bool GrGLGpu::generateMipmap(GrGLTexture* texture, GrSurfaceOrigin textureOrigin,
4200 bool gammaCorrect) {
Brian Salomonbf7b6202016-11-11 16:08:03 -05004201 SkASSERT(!GrPixelConfigIsSint(texture->config()));
brianosman09563ce2016-06-02 08:59:34 -07004202 // Our iterative downsample requires the ability to limit which level we're sampling:
4203 if (!this->glCaps().doManualMipmapping()) {
brianosman33f6b3f2016-06-02 05:49:21 -07004204 return false;
4205 }
4206
4207 // Mipmaps are only supported on 2D textures:
4208 if (GR_GL_TEXTURE_2D != texture->target()) {
4209 return false;
4210 }
4211
4212 // We need to be able to render to the texture for this to work:
Brian Salomon71d9d842016-11-03 13:42:00 -04004213 if (!this->glCaps().canConfigBeFBOColorAttachment(texture->config())) {
brianosman33f6b3f2016-06-02 05:49:21 -07004214 return false;
4215 }
4216
brianosman33f6b3f2016-06-02 05:49:21 -07004217 // If we're mipping an sRGB texture, we need to ensure FB sRGB is correct:
4218 if (GrPixelConfigIsSRGB(texture->config())) {
4219 // If we have write-control, just set the state that we want:
4220 if (this->glCaps().srgbWriteControl()) {
4221 this->flushFramebufferSRGB(gammaCorrect);
4222 } else if (!gammaCorrect) {
4223 // If we don't have write-control we can't do non-gamma-correct mipmapping:
4224 return false;
4225 }
4226 }
4227
4228 int width = texture->width();
4229 int height = texture->height();
4230 int levelCount = SkMipMap::ComputeLevelCount(width, height) + 1;
4231
4232 // Define all mips, if we haven't previously done so:
4233 if (0 == texture->texturePriv().maxMipMapLevel()) {
4234 GrGLenum internalFormat;
4235 GrGLenum externalFormat;
4236 GrGLenum externalType;
4237 if (!this->glCaps().getTexImageFormats(texture->config(), texture->config(),
4238 &internalFormat, &externalFormat, &externalType)) {
4239 return false;
4240 }
4241
Brian Osman9b560242017-09-05 15:34:52 -04004242 this->unbindCpuToGpuXferBuffer();
4243
brianosman33f6b3f2016-06-02 05:49:21 -07004244 for (GrGLint level = 1; level < levelCount; ++level) {
4245 // Define the next mip:
4246 width = SkTMax(1, width / 2);
4247 height = SkTMax(1, height / 2);
4248 GL_ALLOC_CALL(this->glInterface(), TexImage2D(GR_GL_TEXTURE_2D, level, internalFormat,
4249 width, height, 0,
4250 externalFormat, externalType, nullptr));
4251 }
4252 }
4253
4254 // Create (if necessary), then bind temporary FBO:
4255 if (0 == fTempDstFBOID) {
4256 GL_CALL(GenFramebuffers(1, &fTempDstFBOID));
4257 }
4258 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, fTempDstFBOID));
Robert Phillips294870f2016-11-11 12:38:40 -05004259 fHWBoundRenderTargetUniqueID.makeInvalid();
brianosman33f6b3f2016-06-02 05:49:21 -07004260
4261 // Bind the texture, to get things configured for filtering.
4262 // We'll be changing our base level further below:
4263 this->setTextureUnit(0);
Brian Salomon2bbdcc42017-09-07 12:36:34 -04004264 this->bindTexture(0, GrSamplerState::ClampBilerp(), gammaCorrect, texture, textureOrigin);
brianosman33f6b3f2016-06-02 05:49:21 -07004265
4266 // Vertex data:
4267 if (!fMipmapProgramArrayBuffer) {
4268 static const GrGLfloat vdata[] = {
4269 0, 0,
4270 0, 1,
4271 1, 0,
4272 1, 1
4273 };
4274 fMipmapProgramArrayBuffer.reset(GrGLBuffer::Create(this, sizeof(vdata),
4275 kVertex_GrBufferType,
4276 kStatic_GrAccessPattern, vdata));
4277 }
4278 if (!fMipmapProgramArrayBuffer) {
4279 return false;
4280 }
4281
4282 fHWVertexArrayState.setVertexArrayID(this, 0);
4283
4284 GrGLAttribArrayState* attribs = fHWVertexArrayState.bindInternalVertexArray(this);
Chris Dalton8e45b4f2017-05-05 14:00:56 -04004285 attribs->enableVertexArrays(this, 1);
Ethan Nicholasfa7ee242017-09-25 09:52:04 -04004286 attribs->set(this, 0, fMipmapProgramArrayBuffer.get(), kHalf2_GrVertexAttribType,
brianosman33f6b3f2016-06-02 05:49:21 -07004287 2 * sizeof(GrGLfloat), 0);
brianosman33f6b3f2016-06-02 05:49:21 -07004288
4289 // Set "simple" state once:
4290 GrXferProcessor::BlendInfo blendInfo;
4291 blendInfo.reset();
4292 this->flushBlend(blendInfo, GrSwizzle::RGBA());
4293 this->flushColorWrite(true);
brianosman33f6b3f2016-06-02 05:49:21 -07004294 this->flushHWAAState(nullptr, false, false);
4295 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07004296 this->disableWindowRectangles();
csmartdaltonc7d85332016-10-26 10:13:46 -07004297 this->disableStencil();
brianosman33f6b3f2016-06-02 05:49:21 -07004298
4299 // Do all the blits:
4300 width = texture->width();
4301 height = texture->height();
4302 GrGLIRect viewport;
4303 viewport.fLeft = 0;
4304 viewport.fBottom = 0;
4305 for (GrGLint level = 1; level < levelCount; ++level) {
4306 // Get and bind the program for this particular downsample (filter shape can vary):
4307 int progIdx = TextureSizeToMipmapProgramIdx(width, height);
4308 if (!fMipmapPrograms[progIdx].fProgram) {
4309 if (!this->createMipmapProgram(progIdx)) {
4310 SkDebugf("Failed to create mipmap program.\n");
4311 return false;
4312 }
4313 }
4314 GL_CALL(UseProgram(fMipmapPrograms[progIdx].fProgram));
4315 fHWProgramID = fMipmapPrograms[progIdx].fProgram;
4316
4317 // Texcoord uniform is expected to contain (1/w, (w-1)/w, 1/h, (h-1)/h)
4318 const float invWidth = 1.0f / width;
4319 const float invHeight = 1.0f / height;
4320 GL_CALL(Uniform4f(fMipmapPrograms[progIdx].fTexCoordXformUniform,
4321 invWidth, (width - 1) * invWidth, invHeight, (height - 1) * invHeight));
4322 GL_CALL(Uniform1i(fMipmapPrograms[progIdx].fTextureUniform, 0));
4323
4324 // Only sample from previous mip
4325 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_BASE_LEVEL, level - 1));
4326
4327 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
4328 GR_GL_TEXTURE_2D, texture->textureID(), level));
4329
4330 width = SkTMax(1, width / 2);
4331 height = SkTMax(1, height / 2);
4332 viewport.fWidth = width;
4333 viewport.fHeight = height;
4334 this->flushViewport(viewport);
4335
4336 GL_CALL(DrawArrays(GR_GL_TRIANGLE_STRIP, 0, 4));
4337 }
4338
4339 // Unbind:
4340 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
4341 GR_GL_TEXTURE_2D, 0, 0));
4342
4343 return true;
4344}
4345
Robert Phillipsb0e93a22017-08-29 08:26:54 -04004346void GrGLGpu::onQueryMultisampleSpecs(GrRenderTarget* rt, GrSurfaceOrigin rtOrigin,
4347 const GrStencilSettings& stencil,
csmartdaltonc25c5d72016-11-01 07:03:59 -07004348 int* effectiveSampleCnt, SamplePattern* samplePattern) {
Brian Salomon7c8460e2017-05-12 11:36:10 -04004349 SkASSERT(GrFSAAType::kMixedSamples != rt->fsaaType() ||
4350 rt->renderTargetPriv().getStencilAttachment() || stencil.isDisabled());
cdalton28f45b92016-03-07 13:58:26 -08004351
4352 this->flushStencil(stencil);
4353 this->flushHWAAState(rt, true, !stencil.isDisabled());
4354 this->flushRenderTarget(static_cast<GrGLRenderTarget*>(rt), &SkIRect::EmptyIRect());
4355
4356 if (0 != this->caps()->maxRasterSamples()) {
4357 GR_GL_GetIntegerv(this->glInterface(), GR_GL_EFFECTIVE_RASTER_SAMPLES, effectiveSampleCnt);
4358 } else {
4359 GR_GL_GetIntegerv(this->glInterface(), GR_GL_SAMPLES, effectiveSampleCnt);
4360 }
4361
Brian Salomon154ce912017-05-17 10:40:02 -04004362 SkASSERT(*effectiveSampleCnt >= rt->numStencilSamples());
cdalton28f45b92016-03-07 13:58:26 -08004363
4364 if (this->caps()->sampleLocationsSupport()) {
csmartdalton0d28e572016-07-06 09:59:43 -07004365 samplePattern->reset(*effectiveSampleCnt);
cdalton28f45b92016-03-07 13:58:26 -08004366 for (int i = 0; i < *effectiveSampleCnt; ++i) {
4367 GrGLfloat pos[2];
4368 GL_CALL(GetMultisamplefv(GR_GL_SAMPLE_POSITION, i, pos));
Robert Phillipsb0e93a22017-08-29 08:26:54 -04004369 if (kTopLeft_GrSurfaceOrigin == rtOrigin) {
csmartdalton0d28e572016-07-06 09:59:43 -07004370 (*samplePattern)[i].set(pos[0], pos[1]);
cdalton28f45b92016-03-07 13:58:26 -08004371 } else {
csmartdalton0d28e572016-07-06 09:59:43 -07004372 (*samplePattern)[i].set(pos[0], 1 - pos[1]);
cdalton28f45b92016-03-07 13:58:26 -08004373 }
4374 }
4375 }
4376}
4377
cdalton231c5fd2015-05-13 12:35:36 -07004378void GrGLGpu::xferBarrier(GrRenderTarget* rt, GrXferBarrierType type) {
bsalomoncb02b382015-08-12 11:14:50 -07004379 SkASSERT(type);
cdalton9954bc32015-04-29 14:17:00 -07004380 switch (type) {
cdalton231c5fd2015-05-13 12:35:36 -07004381 case kTexture_GrXferBarrierType: {
4382 GrGLRenderTarget* glrt = static_cast<GrGLRenderTarget*>(rt);
4383 if (glrt->textureFBOID() != glrt->renderFBOID()) {
4384 // The render target uses separate storage so no need for glTextureBarrier.
4385 // FIXME: The render target will resolve automatically when its texture is bound,
4386 // but we could resolve only the bounds that will be read if we do it here instead.
4387 return;
4388 }
cdalton9954bc32015-04-29 14:17:00 -07004389 SkASSERT(this->caps()->textureBarrierSupport());
4390 GL_CALL(TextureBarrier());
4391 return;
cdalton231c5fd2015-05-13 12:35:36 -07004392 }
cdalton8917d622015-05-06 13:40:21 -07004393 case kBlend_GrXferBarrierType:
bsalomon4b91f762015-05-19 09:29:46 -07004394 SkASSERT(GrCaps::kAdvanced_BlendEquationSupport ==
cdalton8917d622015-05-06 13:40:21 -07004395 this->caps()->blendEquationSupport());
4396 GL_CALL(BlendBarrier());
4397 return;
bsalomoncb02b382015-08-12 11:14:50 -07004398 default: break; // placate compiler warnings that kNone not handled
cdalton9954bc32015-04-29 14:17:00 -07004399 }
4400}
4401
jvanverth88957922015-07-14 11:02:52 -07004402GrBackendObject GrGLGpu::createTestingOnlyBackendTexture(void* pixels, int w, int h,
Greg Daniel177e6952017-10-12 12:27:11 -04004403 GrPixelConfig config, bool /*isRT*/,
4404 GrMipMapped mipMapped) {
bsalomon926cb022015-12-17 18:15:11 -08004405 if (!this->caps()->isConfigTexturable(config)) {
Greg Daniel177e6952017-10-12 12:27:11 -04004406 return reinterpret_cast<GrBackendObject>(nullptr);
bsalomon926cb022015-12-17 18:15:11 -08004407 }
Greg Daniel177e6952017-10-12 12:27:11 -04004408
4409 // Currently we don't support uploading pixel data when mipped.
4410 if (pixels && GrMipMapped::kYes == mipMapped) {
4411 return reinterpret_cast<GrBackendObject>(nullptr);
4412 }
4413
Ben Wagner18b61f92016-10-25 10:44:02 -04004414 std::unique_ptr<GrGLTextureInfo> info = skstd::make_unique<GrGLTextureInfo>();
bsalomon091f60c2015-11-10 11:54:56 -08004415 info->fTarget = GR_GL_TEXTURE_2D;
kkinnunen546eb5c2015-12-11 00:05:33 -08004416 info->fID = 0;
bsalomon091f60c2015-11-10 11:54:56 -08004417 GL_CALL(GenTextures(1, &info->fID));
jvanverth672bb7f2015-07-13 07:19:57 -07004418 GL_CALL(ActiveTexture(GR_GL_TEXTURE0));
4419 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, 1));
bsalomon091f60c2015-11-10 11:54:56 -08004420 GL_CALL(BindTexture(info->fTarget, info->fID));
Robert Phillips294870f2016-11-11 12:38:40 -05004421 fHWBoundTextureUniqueIDs[0].makeInvalid();
bsalomon091f60c2015-11-10 11:54:56 -08004422 GL_CALL(TexParameteri(info->fTarget, GR_GL_TEXTURE_MAG_FILTER, GR_GL_NEAREST));
4423 GL_CALL(TexParameteri(info->fTarget, GR_GL_TEXTURE_MIN_FILTER, GR_GL_NEAREST));
4424 GL_CALL(TexParameteri(info->fTarget, GR_GL_TEXTURE_WRAP_S, GR_GL_CLAMP_TO_EDGE));
4425 GL_CALL(TexParameteri(info->fTarget, GR_GL_TEXTURE_WRAP_T, GR_GL_CLAMP_TO_EDGE));
jvanverth672bb7f2015-07-13 07:19:57 -07004426
bsalomon76148af2016-01-12 11:13:47 -08004427 GrGLenum internalFormat;
4428 GrGLenum externalFormat;
4429 GrGLenum externalType;
4430
4431 if (!this->glCaps().getTexImageFormats(config, config, &internalFormat, &externalFormat,
4432 &externalType)) {
bsalomon76148af2016-01-12 11:13:47 -08004433 return reinterpret_cast<GrBackendObject>(nullptr);
bsalomon76148af2016-01-12 11:13:47 -08004434 }
jvanverth672bb7f2015-07-13 07:19:57 -07004435
Brian Osman9b560242017-09-05 15:34:52 -04004436 this->unbindCpuToGpuXferBuffer();
Greg Daniel177e6952017-10-12 12:27:11 -04004437
4438 // Figure out the number of mip levels.
4439 int mipLevels = 1;
4440 if (GrMipMapped::kYes == mipMapped) {
4441 mipLevels = SkMipMap::ComputeLevelCount(w, h) + 1;
4442 }
4443
Greg Daniel261b8aa2017-10-23 09:37:36 -04004444 size_t bpp = GrBytesPerPixel(config);
4445 size_t baseLayerSize = bpp * w * h;
4446 SkAutoMalloc defaultStorage(baseLayerSize);
4447 if (!pixels) {
4448 // Fill in the texture with all zeros so we don't have random garbage
4449 pixels = defaultStorage.get();
4450 memset(pixels, 0, baseLayerSize);
4451 }
4452
Greg Daniel177e6952017-10-12 12:27:11 -04004453 int width = w;
4454 int height = h;
4455 for (int i = 0; i < mipLevels; ++i) {
Greg Daniel45d63032017-10-30 13:41:26 -04004456 GL_CALL(TexImage2D(info->fTarget, i, internalFormat, width, height, 0, externalFormat,
Greg Daniel177e6952017-10-12 12:27:11 -04004457 externalType, pixels));
4458 width = SkTMax(1, width / 2);
4459 height = SkTMax(1, height / 2);
4460 }
jvanverth672bb7f2015-07-13 07:19:57 -07004461
Ben Wagner18b61f92016-10-25 10:44:02 -04004462 return reinterpret_cast<GrBackendObject>(info.release());
jvanverth672bb7f2015-07-13 07:19:57 -07004463}
4464
jvanverth88957922015-07-14 11:02:52 -07004465bool GrGLGpu::isTestingOnlyBackendTexture(GrBackendObject id) const {
bsalomon091f60c2015-11-10 11:54:56 -08004466 GrGLuint texID = reinterpret_cast<const GrGLTextureInfo*>(id)->fID;
jvanverth672bb7f2015-07-13 07:19:57 -07004467
4468 GrGLboolean result;
4469 GL_CALL_RET(result, IsTexture(texID));
4470
4471 return (GR_GL_TRUE == result);
4472}
4473
bsalomone63ffef2016-02-05 07:17:34 -08004474void GrGLGpu::deleteTestingOnlyBackendTexture(GrBackendObject id, bool abandonTexture) {
Ben Wagner18b61f92016-10-25 10:44:02 -04004475 std::unique_ptr<const GrGLTextureInfo> info(reinterpret_cast<const GrGLTextureInfo*>(id));
bsalomon091f60c2015-11-10 11:54:56 -08004476 GrGLuint texID = info->fID;
bsalomon091f60c2015-11-10 11:54:56 -08004477
bsalomon67d76202015-11-11 12:40:42 -08004478 if (!abandonTexture) {
4479 GL_CALL(DeleteTextures(1, &texID));
4480 }
jvanverth672bb7f2015-07-13 07:19:57 -07004481}
4482
joshualitt8fd844f2015-12-02 13:36:47 -08004483void GrGLGpu::resetShaderCacheForTesting() const {
4484 fProgramCache->abandon();
4485}
4486
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00004487///////////////////////////////////////////////////////////////////////////////
bsalomon6df86402015-06-01 10:41:49 -07004488
cdaltone2e71c22016-04-07 18:13:29 -07004489GrGLAttribArrayState* GrGLGpu::HWVertexArrayState::bindInternalVertexArray(GrGLGpu* gpu,
csmartdalton485a1202016-07-13 10:16:32 -07004490 const GrBuffer* ibuf) {
robertphillips@google.com4f65a272013-03-26 19:40:46 +00004491 GrGLAttribArrayState* attribState;
4492
cdaltone2e71c22016-04-07 18:13:29 -07004493 if (gpu->glCaps().isCoreProfile()) {
4494 if (!fCoreProfileVertexArray) {
bsalomon@google.com6918d482013-03-07 19:09:11 +00004495 GrGLuint arrayID;
4496 GR_GL_CALL(gpu->glInterface(), GenVertexArrays(1, &arrayID));
4497 int attrCount = gpu->glCaps().maxVertexAttributes();
cdaltone2e71c22016-04-07 18:13:29 -07004498 fCoreProfileVertexArray = new GrGLVertexArray(arrayID, attrCount);
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00004499 }
cdaltone2e71c22016-04-07 18:13:29 -07004500 if (ibuf) {
4501 attribState = fCoreProfileVertexArray->bindWithIndexBuffer(gpu, ibuf);
bsalomon6df86402015-06-01 10:41:49 -07004502 } else {
cdaltone2e71c22016-04-07 18:13:29 -07004503 attribState = fCoreProfileVertexArray->bind(gpu);
bsalomon6df86402015-06-01 10:41:49 -07004504 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00004505 } else {
cdaltone2e71c22016-04-07 18:13:29 -07004506 if (ibuf) {
4507 // bindBuffer implicitly binds VAO 0 when binding an index buffer.
4508 gpu->bindBuffer(kIndex_GrBufferType, ibuf);
bsalomon@google.com6918d482013-03-07 19:09:11 +00004509 } else {
4510 this->setVertexArrayID(gpu, 0);
4511 }
4512 int attrCount = gpu->glCaps().maxVertexAttributes();
4513 if (fDefaultVertexArrayAttribState.count() != attrCount) {
4514 fDefaultVertexArrayAttribState.resize(attrCount);
4515 }
4516 attribState = &fDefaultVertexArrayAttribState;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00004517 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00004518 return attribState;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00004519}
bsalomone179a912016-01-20 06:18:10 -08004520
Robert Phillips3798c862017-03-27 11:08:16 -04004521bool GrGLGpu::onIsACopyNeededForTextureParams(GrTextureProxy* proxy,
Brian Salomon2bbdcc42017-09-07 12:36:34 -04004522 const GrSamplerState& textureParams,
Robert Phillips81444fb2017-03-21 09:14:35 -04004523 GrTextureProducer::CopyParams* copyParams,
4524 SkScalar scaleAdjust[2]) const {
Robert Phillips3798c862017-03-27 11:08:16 -04004525 const GrTexture* texture = proxy->priv().peekTexture();
4526 if (!texture) {
4527 // The only way to get and EXTERNAL or RECTANGLE texture in Ganesh is to wrap them.
4528 // In that case the proxy should already be instantiated.
4529 return false;
4530 }
4531
Brian Salomon2bbdcc42017-09-07 12:36:34 -04004532 if (textureParams.isRepeated() || GrSamplerState::Filter::kMipMap == textureParams.filter()) {
Robert Phillips3798c862017-03-27 11:08:16 -04004533 const GrGLTexture* glTexture = static_cast<const GrGLTexture*>(texture);
bsalomone179a912016-01-20 06:18:10 -08004534 if (GR_GL_TEXTURE_EXTERNAL == glTexture->target() ||
4535 GR_GL_TEXTURE_RECTANGLE == glTexture->target()) {
Brian Salomon2bbdcc42017-09-07 12:36:34 -04004536 copyParams->fFilter = GrSamplerState::Filter::kNearest;
bsalomone179a912016-01-20 06:18:10 -08004537 copyParams->fWidth = texture->width();
4538 copyParams->fHeight = texture->height();
4539 return true;
4540 }
4541 }
4542 return false;
4543}
jvanverth84741b32016-09-30 08:39:02 -07004544
Greg Daniel51316782017-08-02 15:10:09 +00004545void GrGLGpu::onFinishFlush(bool insertedSemaphore) {
4546 // If we inserted semaphores during the flush, we need to call GLFlush.
4547 if (insertedSemaphore) {
4548 GL_CALL(Flush());
4549 }
4550}
4551
Greg Daniel6be35232017-03-01 17:01:09 -05004552GrFence SK_WARN_UNUSED_RESULT GrGLGpu::insertFence() {
Greg Danielc64ee462017-06-15 16:59:49 -04004553 SkASSERT(this->caps()->fenceSyncSupport());
Greg Daniel6be35232017-03-01 17:01:09 -05004554 GrGLsync sync;
4555 GL_CALL_RET(sync, FenceSync(GR_GL_SYNC_GPU_COMMANDS_COMPLETE, 0));
4556 GR_STATIC_ASSERT(sizeof(GrFence) >= sizeof(GrGLsync));
4557 return (GrFence)sync;
jvanverth84741b32016-09-30 08:39:02 -07004558}
4559
Greg Daniel6be35232017-03-01 17:01:09 -05004560bool GrGLGpu::waitFence(GrFence fence, uint64_t timeout) {
jvanverth84741b32016-09-30 08:39:02 -07004561 GrGLenum result;
4562 GL_CALL_RET(result, ClientWaitSync((GrGLsync)fence, GR_GL_SYNC_FLUSH_COMMANDS_BIT, timeout));
4563 return (GR_GL_CONDITION_SATISFIED == result);
4564}
4565
4566void GrGLGpu::deleteFence(GrFence fence) const {
Greg Daniel6be35232017-03-01 17:01:09 -05004567 this->deleteSync((GrGLsync)fence);
4568}
4569
Greg Daniela5cb7812017-06-16 09:45:32 -04004570sk_sp<GrSemaphore> SK_WARN_UNUSED_RESULT GrGLGpu::makeSemaphore(bool isOwned) {
Greg Danielc64ee462017-06-15 16:59:49 -04004571 SkASSERT(this->caps()->fenceSyncSupport());
Greg Daniela5cb7812017-06-16 09:45:32 -04004572 return GrGLSemaphore::Make(this, isOwned);
Greg Daniel6be35232017-03-01 17:01:09 -05004573}
4574
Greg Daniela5cb7812017-06-16 09:45:32 -04004575sk_sp<GrSemaphore> GrGLGpu::wrapBackendSemaphore(const GrBackendSemaphore& semaphore,
4576 GrWrapOwnership ownership) {
Greg Danielc64ee462017-06-15 16:59:49 -04004577 SkASSERT(this->caps()->fenceSyncSupport());
Greg Daniela5cb7812017-06-16 09:45:32 -04004578 return GrGLSemaphore::MakeWrapped(this, semaphore.glSync(), ownership);
4579}
4580
Brian Osmandc87c952017-04-28 13:57:38 -04004581void GrGLGpu::insertSemaphore(sk_sp<GrSemaphore> semaphore, bool flush) {
Greg Daniel6be35232017-03-01 17:01:09 -05004582 GrGLSemaphore* glSem = static_cast<GrGLSemaphore*>(semaphore.get());
4583
4584 GrGLsync sync;
4585 GL_CALL_RET(sync, FenceSync(GR_GL_SYNC_GPU_COMMANDS_COMPLETE, 0));
4586 glSem->setSync(sync);
Brian Osmandc87c952017-04-28 13:57:38 -04004587
4588 if (flush) {
4589 GL_CALL(Flush());
4590 }
Greg Daniel6be35232017-03-01 17:01:09 -05004591}
4592
4593void GrGLGpu::waitSemaphore(sk_sp<GrSemaphore> semaphore) {
4594 GrGLSemaphore* glSem = static_cast<GrGLSemaphore*>(semaphore.get());
4595
4596 GL_CALL(WaitSync(glSem->sync(), 0, GR_GL_TIMEOUT_IGNORED));
4597}
4598
4599void GrGLGpu::deleteSync(GrGLsync sync) const {
4600 GL_CALL(DeleteSync(sync));
jvanverth84741b32016-09-30 08:39:02 -07004601}
Brian Osman13dddce2017-05-09 13:19:50 -04004602
Robert Phillips65a88fa2017-08-08 08:36:22 -04004603void GrGLGpu::insertEventMarker(const char* msg) {
4604 GL_CALL(InsertEventMarker(strlen(msg), msg));
4605}
4606
Brian Osman13dddce2017-05-09 13:19:50 -04004607sk_sp<GrSemaphore> GrGLGpu::prepareTextureForCrossContextUsage(GrTexture* texture) {
4608 // Set up a semaphore to be signaled once the data is ready, and flush GL
Greg Daniela5cb7812017-06-16 09:45:32 -04004609 sk_sp<GrSemaphore> semaphore = this->makeSemaphore(true);
Brian Osman13dddce2017-05-09 13:19:50 -04004610 this->insertSemaphore(semaphore, true);
4611
4612 return semaphore;
4613}
Robert Phillips646e4292017-06-13 12:44:56 -04004614
4615int GrGLGpu::TextureToCopyProgramIdx(GrTexture* texture) {
4616 switch (texture->texturePriv().samplerType()) {
4617 case kTexture2DSampler_GrSLType:
4618 return 0;
4619 case kITexture2DSampler_GrSLType:
4620 return 1;
4621 case kTexture2DRectSampler_GrSLType:
4622 return 2;
4623 case kTextureExternalSampler_GrSLType:
4624 return 3;
4625 default:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -04004626 SK_ABORT("Unexpected samper type");
Robert Phillips646e4292017-06-13 12:44:56 -04004627 return 0;
4628 }
4629}
Brian Osman71a18892017-08-10 10:23:25 -04004630
4631void GrGLGpu::onDumpJSON(SkJSONWriter* writer) const {
4632 // We are called by the base class, which has already called beginObject(). We choose to nest
4633 // all of our caps information in a named sub-object.
4634 writer->beginObject("GL GPU");
4635
4636 const GrGLubyte* str;
4637 GL_CALL_RET(str, GetString(GR_GL_VERSION));
4638 writer->appendString("GL_VERSION", (const char*)(str));
4639 GL_CALL_RET(str, GetString(GR_GL_RENDERER));
4640 writer->appendString("GL_RENDERER", (const char*)(str));
4641 GL_CALL_RET(str, GetString(GR_GL_VENDOR));
4642 writer->appendString("GL_VENDOR", (const char*)(str));
4643 GL_CALL_RET(str, GetString(GR_GL_SHADING_LANGUAGE_VERSION));
4644 writer->appendString("GL_SHADING_LANGUAGE_VERSION", (const char*)(str));
4645
4646 writer->appendName("extensions");
4647 glInterface()->fExtensions.dumpJSON(writer);
4648
4649 writer->endObject();
4650}