blob: 649373bf79f77c6ae362c714da8666483c965d47 [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"
Brian Osman71a18892017-08-10 10:23:25 -040029#include "SkJSONWriter.h"
Kevin Lubickc456b732017-01-11 17:21:57 +000030#include "SkMakeUnique.h"
31#include "SkMipMap.h"
32#include "SkPixmap.h"
Kevin Lubickc456b732017-01-11 17:21:57 +000033#include "SkSLCompiler.h"
Hal Canary95e3c052017-01-11 12:44:43 -050034#include "SkStrokeRec.h"
Kevin Lubickc456b732017-01-11 17:21:57 +000035#include "SkTemplates.h"
Ryan Macnak38a10ad2017-07-10 10:36:34 -070036#include "SkTraceEvent.h"
Kevin Lubickc456b732017-01-11 17:21:57 +000037#include "SkTypes.h"
Hal Canary95e3c052017-01-11 12:44:43 -050038#include "builders/GrGLShaderStringBuilder.h"
Hal Canary95e3c052017-01-11 12:44:43 -050039#include "instanced/GLInstancedRendering.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000040
bsalomon@google.com0b77d682011-08-19 13:28:54 +000041#define GL_CALL(X) GR_GL_CALL(this->glInterface(), X)
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +000042#define GL_CALL_RET(RET, X) GR_GL_CALL_RET(this->glInterface(), RET, X)
bsalomon@google.com0b77d682011-08-19 13:28:54 +000043
reed@google.comac10a2d2010-12-22 21:39:39 +000044#define SKIP_CACHE_CHECK true
45
bsalomon@google.com4f3c2532012-01-19 16:16:52 +000046#if GR_GL_CHECK_ALLOC_WITH_GET_ERROR
47 #define CLEAR_ERROR_BEFORE_ALLOC(iface) GrGLClearErr(iface)
48 #define GL_ALLOC_CALL(iface, call) GR_GL_CALL_NOERRCHECK(iface, call)
49 #define CHECK_ALLOC_ERROR(iface) GR_GL_GET_ERROR(iface)
rmistry@google.comfbfcd562012-08-23 18:09:54 +000050#else
bsalomon@google.com4f3c2532012-01-19 16:16:52 +000051 #define CLEAR_ERROR_BEFORE_ALLOC(iface)
52 #define GL_ALLOC_CALL(iface, call) GR_GL_CALL(iface, call)
53 #define CHECK_ALLOC_ERROR(iface) GR_GL_NO_ERROR
54#endif
55
Jim Van Verth32ac83e2016-11-28 15:23:57 -050056//#define USE_NSIGHT
57
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +000058///////////////////////////////////////////////////////////////////////////////
59
csmartdaltona7f29642016-07-07 08:49:11 -070060using gr_instanced::InstancedRendering;
61using gr_instanced::GLInstancedRendering;
cdaltonb85a0aa2014-07-21 15:32:44 -070062
Robert Phillipse3302df2017-04-24 07:31:02 -040063using gr_instanced::OpAllocator;
64using gr_instanced::GLOpAllocator;
65
cdalton8917d622015-05-06 13:40:21 -070066static const GrGLenum gXfermodeEquation2Blend[] = {
67 // Basic OpenGL blend equations.
68 GR_GL_FUNC_ADD,
69 GR_GL_FUNC_SUBTRACT,
70 GR_GL_FUNC_REVERSE_SUBTRACT,
71
72 // GL_KHR_blend_equation_advanced.
73 GR_GL_SCREEN,
74 GR_GL_OVERLAY,
75 GR_GL_DARKEN,
76 GR_GL_LIGHTEN,
77 GR_GL_COLORDODGE,
78 GR_GL_COLORBURN,
79 GR_GL_HARDLIGHT,
80 GR_GL_SOFTLIGHT,
81 GR_GL_DIFFERENCE,
82 GR_GL_EXCLUSION,
83 GR_GL_MULTIPLY,
84 GR_GL_HSL_HUE,
85 GR_GL_HSL_SATURATION,
86 GR_GL_HSL_COLOR,
87 GR_GL_HSL_LUMINOSITY
88};
89GR_STATIC_ASSERT(0 == kAdd_GrBlendEquation);
90GR_STATIC_ASSERT(1 == kSubtract_GrBlendEquation);
91GR_STATIC_ASSERT(2 == kReverseSubtract_GrBlendEquation);
92GR_STATIC_ASSERT(3 == kScreen_GrBlendEquation);
93GR_STATIC_ASSERT(4 == kOverlay_GrBlendEquation);
94GR_STATIC_ASSERT(5 == kDarken_GrBlendEquation);
95GR_STATIC_ASSERT(6 == kLighten_GrBlendEquation);
96GR_STATIC_ASSERT(7 == kColorDodge_GrBlendEquation);
97GR_STATIC_ASSERT(8 == kColorBurn_GrBlendEquation);
98GR_STATIC_ASSERT(9 == kHardLight_GrBlendEquation);
99GR_STATIC_ASSERT(10 == kSoftLight_GrBlendEquation);
100GR_STATIC_ASSERT(11 == kDifference_GrBlendEquation);
101GR_STATIC_ASSERT(12 == kExclusion_GrBlendEquation);
102GR_STATIC_ASSERT(13 == kMultiply_GrBlendEquation);
103GR_STATIC_ASSERT(14 == kHSLHue_GrBlendEquation);
104GR_STATIC_ASSERT(15 == kHSLSaturation_GrBlendEquation);
105GR_STATIC_ASSERT(16 == kHSLColor_GrBlendEquation);
106GR_STATIC_ASSERT(17 == kHSLLuminosity_GrBlendEquation);
bsalomonf7cc8772015-05-11 11:21:14 -0700107GR_STATIC_ASSERT(SK_ARRAY_COUNT(gXfermodeEquation2Blend) == kGrBlendEquationCnt);
cdalton8917d622015-05-06 13:40:21 -0700108
twiz@google.com0f31ca72011-03-18 17:38:11 +0000109static const GrGLenum gXfermodeCoeff2Blend[] = {
110 GR_GL_ZERO,
111 GR_GL_ONE,
112 GR_GL_SRC_COLOR,
113 GR_GL_ONE_MINUS_SRC_COLOR,
114 GR_GL_DST_COLOR,
115 GR_GL_ONE_MINUS_DST_COLOR,
116 GR_GL_SRC_ALPHA,
117 GR_GL_ONE_MINUS_SRC_ALPHA,
118 GR_GL_DST_ALPHA,
119 GR_GL_ONE_MINUS_DST_ALPHA,
120 GR_GL_CONSTANT_COLOR,
121 GR_GL_ONE_MINUS_CONSTANT_COLOR,
122 GR_GL_CONSTANT_ALPHA,
123 GR_GL_ONE_MINUS_CONSTANT_ALPHA,
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000124
125 // extended blend coeffs
126 GR_GL_SRC1_COLOR,
127 GR_GL_ONE_MINUS_SRC1_COLOR,
128 GR_GL_SRC1_ALPHA,
129 GR_GL_ONE_MINUS_SRC1_ALPHA,
reed@google.comac10a2d2010-12-22 21:39:39 +0000130};
131
bsalomon861e1032014-12-16 07:33:49 -0800132bool GrGLGpu::BlendCoeffReferencesConstant(GrBlendCoeff coeff) {
bsalomon@google.com080773c2011-03-15 19:09:25 +0000133 static const bool gCoeffReferencesBlendConst[] = {
134 false,
135 false,
136 false,
137 false,
138 false,
139 false,
140 false,
141 false,
142 false,
143 false,
144 true,
145 true,
146 true,
147 true,
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000148
149 // extended blend coeffs
150 false,
151 false,
152 false,
153 false,
bsalomon@google.com080773c2011-03-15 19:09:25 +0000154 };
155 return gCoeffReferencesBlendConst[coeff];
bsalomonf7cc8772015-05-11 11:21:14 -0700156 GR_STATIC_ASSERT(kGrBlendCoeffCnt == SK_ARRAY_COUNT(gCoeffReferencesBlendConst));
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000157
bsalomon@google.com47059542012-06-06 20:51:20 +0000158 GR_STATIC_ASSERT(0 == kZero_GrBlendCoeff);
159 GR_STATIC_ASSERT(1 == kOne_GrBlendCoeff);
160 GR_STATIC_ASSERT(2 == kSC_GrBlendCoeff);
161 GR_STATIC_ASSERT(3 == kISC_GrBlendCoeff);
162 GR_STATIC_ASSERT(4 == kDC_GrBlendCoeff);
163 GR_STATIC_ASSERT(5 == kIDC_GrBlendCoeff);
164 GR_STATIC_ASSERT(6 == kSA_GrBlendCoeff);
165 GR_STATIC_ASSERT(7 == kISA_GrBlendCoeff);
166 GR_STATIC_ASSERT(8 == kDA_GrBlendCoeff);
167 GR_STATIC_ASSERT(9 == kIDA_GrBlendCoeff);
168 GR_STATIC_ASSERT(10 == kConstC_GrBlendCoeff);
169 GR_STATIC_ASSERT(11 == kIConstC_GrBlendCoeff);
170 GR_STATIC_ASSERT(12 == kConstA_GrBlendCoeff);
171 GR_STATIC_ASSERT(13 == kIConstA_GrBlendCoeff);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000172
bsalomon@google.com47059542012-06-06 20:51:20 +0000173 GR_STATIC_ASSERT(14 == kS2C_GrBlendCoeff);
174 GR_STATIC_ASSERT(15 == kIS2C_GrBlendCoeff);
175 GR_STATIC_ASSERT(16 == kS2A_GrBlendCoeff);
176 GR_STATIC_ASSERT(17 == kIS2A_GrBlendCoeff);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000177
178 // assertion for gXfermodeCoeff2Blend have to be in GrGpu scope
bsalomonf7cc8772015-05-11 11:21:14 -0700179 GR_STATIC_ASSERT(kGrBlendCoeffCnt == SK_ARRAY_COUNT(gXfermodeCoeff2Blend));
bsalomon@google.com080773c2011-03-15 19:09:25 +0000180}
181
reed@google.comac10a2d2010-12-22 21:39:39 +0000182///////////////////////////////////////////////////////////////////////////////
183
egdanielff1d5472015-09-10 08:37:20 -0700184
bsalomon682c2692015-05-22 14:01:46 -0700185GrGpu* GrGLGpu::Create(GrBackendContext backendContext, const GrContextOptions& options,
186 GrContext* context) {
Greg Daniel02611d92017-07-25 10:05:01 -0400187 return Create(reinterpret_cast<const GrGLInterface*>(backendContext), options, context);
188}
189
190GrGpu* GrGLGpu::Create(const GrGLInterface* interface, const GrContextOptions& options,
191 GrContext* context) {
192 sk_sp<const GrGLInterface> glInterface(interface);
bsalomon424cc262015-05-22 10:37:30 -0700193 if (!glInterface) {
194 glInterface.reset(GrGLDefaultInterface());
195 } else {
196 glInterface->ref();
197 }
198 if (!glInterface) {
halcanary96fcdcc2015-08-27 07:41:13 -0700199 return nullptr;
bsalomon424cc262015-05-22 10:37:30 -0700200 }
Jim Van Verth76334772017-05-05 16:46:05 -0400201#ifdef USE_NSIGHT
202 const_cast<GrContextOptions&>(options).fSuppressPathRendering = true;
203#endif
Hal Canary144caf52016-11-07 17:57:18 -0500204 GrGLContext* glContext = GrGLContext::Create(glInterface.get(), options);
bsalomon424cc262015-05-22 10:37:30 -0700205 if (glContext) {
halcanary385fe4d2015-08-26 13:07:48 -0700206 return new GrGLGpu(glContext, context);
bsalomon424cc262015-05-22 10:37:30 -0700207 }
halcanary96fcdcc2015-08-27 07:41:13 -0700208 return nullptr;
bsalomon424cc262015-05-22 10:37:30 -0700209}
210
bsalomon424cc262015-05-22 10:37:30 -0700211GrGLGpu::GrGLGpu(GrGLContext* ctx, GrContext* context)
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000212 : GrGpu(context)
cdaltone2e71c22016-04-07 18:13:29 -0700213 , fGLContext(ctx)
214 , fProgramCache(new ProgramCache(this))
215 , fHWProgramID(0)
216 , fTempSrcFBOID(0)
217 , fTempDstFBOID(0)
218 , fStencilClearFBOID(0)
cdalton74b8d322016-04-11 14:47:28 -0700219 , fHWMaxUsedBufferTextureUnit(-1)
cdaltone2e71c22016-04-07 18:13:29 -0700220 , fHWMinSampleShading(0.0) {
bsalomon424cc262015-05-22 10:37:30 -0700221 SkASSERT(ctx);
222 fCaps.reset(SkRef(ctx->caps()));
bsalomon@google.combcce8922013-03-25 15:38:39 +0000223
Brian Salomon1edc5b92016-11-29 13:43:46 -0500224 fHWBoundTextureUniqueIDs.reset(this->caps()->shaderCaps()->maxCombinedSamplers());
225 fHWBoundImageStorages.reset(this->caps()->shaderCaps()->maxCombinedImageStorages());
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 }
Brian Salomonf9f45122016-11-29 11:59:17 -0500453 for (int i = 0; i < fHWBoundImageStorages.count(); ++i) {
Brian Salomon8b7a7dd2016-11-29 15:29:41 -0500454 SkASSERT(this->caps()->shaderCaps()->imageLoadStoreSupport());
Brian Salomonf9f45122016-11-29 11:59:17 -0500455 fHWBoundImageStorages[i].fTextureUniqueID.makeInvalid();
456 }
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000457 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000458
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000459 if (resetBits & kBlend_GrGLBackendState) {
460 fHWBlendState.invalidate();
461 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000462
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000463 if (resetBits & kView_GrGLBackendState) {
464 fHWScissorSettings.invalidate();
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700465 fHWWindowRectsState.invalidate();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000466 fHWViewport.invalidate();
467 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000468
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000469 if (resetBits & kStencil_GrGLBackendState) {
470 fHWStencilSettings.invalidate();
471 fHWStencilTestEnabled = kUnknown_TriState;
472 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000473
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000474 // Vertex
475 if (resetBits & kVertex_GrGLBackendState) {
cdaltone2e71c22016-04-07 18:13:29 -0700476 fHWVertexArrayState.invalidate();
477 fHWBufferState[kVertex_GrBufferType].invalidate();
478 fHWBufferState[kIndex_GrBufferType].invalidate();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000479 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000480
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000481 if (resetBits & kRenderTarget_GrGLBackendState) {
Robert Phillips294870f2016-11-11 12:38:40 -0500482 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon16921ec2015-07-30 15:34:56 -0700483 fHWSRGBFramebuffer = kUnknown_TriState;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000484 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000485
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000486 if (resetBits & kPathRendering_GrGLBackendState) {
jvanverthe9c0fc62015-04-29 11:18:05 -0700487 if (this->caps()->shaderCaps()->pathRenderingSupport()) {
kkinnunenccdaa042014-08-20 01:36:23 -0700488 this->glPathRendering()->resetContext();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000489 }
bsalomon@google.com05a718c2012-06-29 14:01:53 +0000490 }
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000491
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000492 // we assume these values
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000493 if (resetBits & kPixelStore_GrGLBackendState) {
494 if (this->glCaps().unpackRowLengthSupport()) {
495 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
496 }
497 if (this->glCaps().packRowLengthSupport()) {
498 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
499 }
500 if (this->glCaps().unpackFlipYSupport()) {
501 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
502 }
503 if (this->glCaps().packFlipYSupport()) {
504 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, GR_GL_FALSE));
505 }
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000506 }
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000507
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000508 if (resetBits & kProgram_GrGLBackendState) {
509 fHWProgramID = 0;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000510 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000511}
512
Brian Salomond17f6582017-07-19 18:28:58 -0400513static bool check_backend_texture(const GrBackendTexture& backendTex, const GrGLCaps& caps,
514 GrGLTexture::IDDesc* idDesc) {
Greg Daniel7ef28f32017-04-20 16:41:55 +0000515 const GrGLTextureInfo* info = backendTex.getGLTextureInfo();
bsalomon091f60c2015-11-10 11:54:56 -0800516 if (!info || !info->fID) {
Brian Salomond17f6582017-07-19 18:28:58 -0400517 return false;
bsalomon091f60c2015-11-10 11:54:56 -0800518 }
robertphillips@google.comb72e5d32012-10-30 15:18:10 +0000519
Brian Salomond17f6582017-07-19 18:28:58 -0400520 idDesc->fInfo = *info;
bsalomon7ea33f52015-11-22 14:51:00 -0800521
Brian Salomond17f6582017-07-19 18:28:58 -0400522 if (GR_GL_TEXTURE_EXTERNAL == idDesc->fInfo.fTarget) {
523 if (!caps.shaderCaps()->externalTextureSupport()) {
524 return false;
525 }
526 } else if (GR_GL_TEXTURE_RECTANGLE == idDesc->fInfo.fTarget) {
527 if (!caps.rectangleTextureSupport()) {
528 return false;
529 }
530 } else if (GR_GL_TEXTURE_2D != idDesc->fInfo.fTarget) {
531 return false;
532 }
533 return true;
534}
535
536sk_sp<GrTexture> GrGLGpu::onWrapBackendTexture(const GrBackendTexture& backendTex,
Brian Salomond17f6582017-07-19 18:28:58 -0400537 GrWrapOwnership ownership) {
bsalomonb15b4c12014-10-29 12:41:57 -0700538 GrGLTexture::IDDesc idDesc;
Brian Salomond17f6582017-07-19 18:28:58 -0400539 if (!check_backend_texture(backendTex, this->glCaps(), &idDesc)) {
540 return nullptr;
541 }
542 if (kBorrow_GrWrapOwnership == ownership) {
543 idDesc.fOwnership = GrBackendObjectOwnership::kBorrowed;
544 } else {
545 idDesc.fOwnership = GrBackendObjectOwnership::kOwned;
546 }
bsalomone5286e02016-01-14 09:24:09 -0800547
Brian Salomond17f6582017-07-19 18:28:58 -0400548 GrSurfaceDesc surfDesc;
549 surfDesc.fFlags = kNone_GrSurfaceFlags;
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400550 surfDesc.fOrigin = kTopLeft_GrSurfaceOrigin; // This isn't used in the following
Brian Salomond17f6582017-07-19 18:28:58 -0400551 surfDesc.fWidth = backendTex.width();
552 surfDesc.fHeight = backendTex.height();
553 surfDesc.fConfig = backendTex.config();
554 surfDesc.fSampleCnt = 0;
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400555
Greg Daniel177e6952017-10-12 12:27:11 -0400556 GrMipMapsStatus mipMapsStatus = backendTex.hasMipMaps() ? GrMipMapsStatus::kValid
557 : GrMipMapsStatus::kNotAllocated;
558
559 return GrGLTexture::MakeWrapped(this, surfDesc, mipMapsStatus, idDesc);
Brian Salomond17f6582017-07-19 18:28:58 -0400560}
561
562sk_sp<GrTexture> GrGLGpu::onWrapRenderableBackendTexture(const GrBackendTexture& backendTex,
Brian Salomond17f6582017-07-19 18:28:58 -0400563 int sampleCnt,
564 GrWrapOwnership ownership) {
565 GrGLTexture::IDDesc idDesc;
566 if (!check_backend_texture(backendTex, this->glCaps(), &idDesc)) {
bsalomone5286e02016-01-14 09:24:09 -0800567 return nullptr;
bsalomon7ea33f52015-11-22 14:51:00 -0800568 }
bsalomone5286e02016-01-14 09:24:09 -0800569
Brian Salomond17f6582017-07-19 18:28:58 -0400570 // We don't support rendering to a EXTERNAL texture.
571 if (GR_GL_TEXTURE_EXTERNAL == idDesc.fInfo.fTarget) {
bsalomona98419b2015-11-23 07:09:50 -0800572 return nullptr;
573 }
bsalomon10528f12015-10-14 12:54:52 -0700574
Brian Osman766fcbb2017-03-13 09:33:09 -0400575 if (kBorrow_GrWrapOwnership == ownership) {
Brian Osmana6953f22017-03-10 20:14:05 +0000576 idDesc.fOwnership = GrBackendObjectOwnership::kBorrowed;
Brian Osman766fcbb2017-03-13 09:33:09 -0400577 } else {
578 idDesc.fOwnership = GrBackendObjectOwnership::kOwned;
bsalomone5286e02016-01-14 09:24:09 -0800579 }
bsalomonb15b4c12014-10-29 12:41:57 -0700580
Ben Wagner18b61f92016-10-25 10:44:02 -0400581 GrSurfaceDesc surfDesc;
Brian Salomond17f6582017-07-19 18:28:58 -0400582 surfDesc.fFlags = kRenderTarget_GrSurfaceFlag;
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400583 surfDesc.fOrigin = kBottomLeft_GrSurfaceOrigin; // This isn't actually used in the following
Greg Daniel7ef28f32017-04-20 16:41:55 +0000584 surfDesc.fWidth = backendTex.width();
585 surfDesc.fHeight = backendTex.height();
586 surfDesc.fConfig = backendTex.config();
Greg Daniel81e7bf82017-07-19 14:47:42 -0400587 surfDesc.fSampleCnt = this->caps()->getSampleCount(sampleCnt, backendTex.config());
bsalomon@google.come269f212011-11-07 13:29:52 +0000588
Brian Salomond17f6582017-07-19 18:28:58 -0400589 GrGLRenderTarget::IDDesc rtIDDesc;
590 if (!this->createRenderTargetObjects(surfDesc, idDesc.fInfo, &rtIDDesc)) {
591 return nullptr;
bsalomon@google.come269f212011-11-07 13:29:52 +0000592 }
Greg Daniel177e6952017-10-12 12:27:11 -0400593
594 GrMipMapsStatus mipMapsStatus = backendTex.hasMipMaps() ? GrMipMapsStatus::kDirty
595 : GrMipMapsStatus::kNotAllocated;
596
Brian Salomond17f6582017-07-19 18:28:58 -0400597 sk_sp<GrGLTextureRenderTarget> texRT(
Greg Daniel177e6952017-10-12 12:27:11 -0400598 GrGLTextureRenderTarget::MakeWrapped(this, surfDesc, idDesc, rtIDDesc, mipMapsStatus));
Brian Salomond17f6582017-07-19 18:28:58 -0400599 texRT->baseLevelWasBoundToFBO();
600 return std::move(texRT);
bsalomon@google.come269f212011-11-07 13:29:52 +0000601}
602
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400603sk_sp<GrRenderTarget> GrGLGpu::onWrapBackendRenderTarget(const GrBackendRenderTarget& backendRT) {
Greg Danielbcf612b2017-05-01 13:50:58 +0000604 const GrGLFramebufferInfo* info = backendRT.getGLFramebufferInfo();
605 if (!info) {
606 return nullptr;
607 }
608
bsalomonb15b4c12014-10-29 12:41:57 -0700609 GrGLRenderTarget::IDDesc idDesc;
Greg Danielbcf612b2017-05-01 13:50:58 +0000610 idDesc.fRTFBOID = info->fFBOID;
bsalomonb15b4c12014-10-29 12:41:57 -0700611 idDesc.fMSColorRenderbufferID = 0;
egdanield803f272015-03-18 13:01:52 -0700612 idDesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
Brian Osman0b791f52017-03-10 08:30:22 -0500613 idDesc.fRTFBOOwnership = GrBackendObjectOwnership::kBorrowed;
csmartdaltonf9635992016-08-10 11:09:07 -0700614 idDesc.fIsMixedSampled = false;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000615
bsalomonb15b4c12014-10-29 12:41:57 -0700616 GrSurfaceDesc desc;
Brian Salomon0ec981b2017-05-15 13:48:50 -0400617 desc.fFlags = kRenderTarget_GrSurfaceFlag;
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400618 desc.fOrigin = kBottomLeft_GrSurfaceOrigin; // This isn't actually used in the following
Greg Danielbcf612b2017-05-01 13:50:58 +0000619 desc.fWidth = backendRT.width();
620 desc.fHeight = backendRT.height();
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400621 desc.fConfig = backendRT.config();
Greg Daniel81e7bf82017-07-19 14:47:42 -0400622 desc.fSampleCnt = this->caps()->getSampleCount(backendRT.sampleCnt(), backendRT.config());
bsalomonb15b4c12014-10-29 12:41:57 -0700623
Greg Danielbcf612b2017-05-01 13:50:58 +0000624 return GrGLRenderTarget::MakeWrapped(this, desc, idDesc, backendRT.stencilBits());
bsalomon@google.come269f212011-11-07 13:29:52 +0000625}
626
Greg Daniel7ef28f32017-04-20 16:41:55 +0000627sk_sp<GrRenderTarget> GrGLGpu::onWrapBackendTextureAsRenderTarget(const GrBackendTexture& tex,
Greg Daniel7ef28f32017-04-20 16:41:55 +0000628 int sampleCnt) {
629 const GrGLTextureInfo* info = tex.getGLTextureInfo();
ericrkf7b8b8a2016-02-24 14:49:51 -0800630 if (!info || !info->fID) {
631 return nullptr;
632 }
ericrkf7b8b8a2016-02-24 14:49:51 -0800633
jvanverthdb1adf52016-05-03 05:37:45 -0700634 GrGLTextureInfo texInfo;
jvanverthdb1adf52016-05-03 05:37:45 -0700635 texInfo = *info;
ericrkf7b8b8a2016-02-24 14:49:51 -0800636
jvanverthdb1adf52016-05-03 05:37:45 -0700637 if (GR_GL_TEXTURE_RECTANGLE != texInfo.fTarget &&
638 GR_GL_TEXTURE_2D != texInfo.fTarget) {
ericrkf7b8b8a2016-02-24 14:49:51 -0800639 // Only texture rectangle and texture 2d are supported. We do not check whether texture
640 // rectangle is supported by Skia - if the caller provided us with a texture rectangle,
641 // we assume the necessary support exists.
642 return nullptr;
643 }
644
Ben Wagner18b61f92016-10-25 10:44:02 -0400645 GrSurfaceDesc surfDesc;
Greg Daniel7ef28f32017-04-20 16:41:55 +0000646 surfDesc.fFlags = kRenderTarget_GrSurfaceFlag;
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400647 surfDesc.fOrigin = kBottomLeft_GrSurfaceOrigin; // This isn't actually used in the following
Greg Daniel7ef28f32017-04-20 16:41:55 +0000648 surfDesc.fWidth = tex.width();
649 surfDesc.fHeight = tex.height();
650 surfDesc.fConfig = tex.config();
Greg Daniel81e7bf82017-07-19 14:47:42 -0400651 surfDesc.fSampleCnt = this->caps()->getSampleCount(sampleCnt, tex.config());
ericrkf7b8b8a2016-02-24 14:49:51 -0800652
653 GrGLRenderTarget::IDDesc rtIDDesc;
jvanverthdb1adf52016-05-03 05:37:45 -0700654 if (!this->createRenderTargetObjects(surfDesc, texInfo, &rtIDDesc)) {
ericrkf7b8b8a2016-02-24 14:49:51 -0800655 return nullptr;
656 }
bungeman6bd52842016-10-27 09:30:08 -0700657 return GrGLRenderTarget::MakeWrapped(this, surfDesc, rtIDDesc, 0);
ericrkf7b8b8a2016-02-24 14:49:51 -0800658}
659
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000660////////////////////////////////////////////////////////////////////////////////
ericrkf7b8b8a2016-02-24 14:49:51 -0800661
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400662bool GrGLGpu::onGetWritePixelsInfo(GrSurface* dstSurface, GrSurfaceOrigin dstOrigin,
663 int width, int height,
cblumeed828002016-02-16 13:00:01 -0800664 GrPixelConfig srcConfig,
bsalomonf0674512015-07-28 13:26:15 -0700665 DrawPreference* drawPreference,
666 WritePixelTempDrawInfo* tempDrawInfo) {
Brian Salomon9bada542017-06-12 12:09:30 -0400667 if (SkToBool(dstSurface->asRenderTarget())) {
668 if (this->glCaps().useDrawInsteadOfAllRenderTargetWrites()) {
669 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
670 }
671 }
672
673 GrGLTexture* texture = static_cast<GrGLTexture*>(dstSurface->asTexture());
674
675 if (texture) {
bsalomone5286e02016-01-14 09:24:09 -0800676 if (GR_GL_TEXTURE_EXTERNAL == texture->target()) {
677 // We don't currently support writing pixels to EXTERNAL textures.
bsalomon7ea33f52015-11-22 14:51:00 -0800678 return false;
679 }
Brian Salomon9bada542017-06-12 12:09:30 -0400680 if (GrPixelConfigIsUnorm(texture->config()) && texture->hasBaseLevelBeenBoundToFBO() &&
681 this->glCaps().disallowTexSubImageForUnormConfigTexturesEverBoundToFBO() &&
682 (width < dstSurface->width() || height < dstSurface->height())) {
683 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
684 }
685 } else {
686 // This subclass only allows writes to textures. If the dst is not a texture we have to draw
687 // into it. We could use glDrawPixels on GLs that have it, but we don't today.
688 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
bsalomon6cb3cbe2015-07-30 07:34:27 -0700689 }
690
Brian Osman33910292017-04-18 14:38:53 -0400691 // If the dst is MSAA, we have to draw, or we'll just be writing to the resolve target.
692 if (dstSurface->asRenderTarget() && dstSurface->asRenderTarget()->numColorSamples() > 0) {
693 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
694 }
695
bsalomon16921ec2015-07-30 15:34:56 -0700696 if (GrPixelConfigIsSRGB(dstSurface->config()) != GrPixelConfigIsSRGB(srcConfig)) {
697 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
698 }
699
bsalomon6c9cd552016-01-22 07:17:34 -0800700 // Start off assuming no swizzling
701 tempDrawInfo->fSwizzle = GrSwizzle::RGBA();
702 tempDrawInfo->fWriteConfig = srcConfig;
bsalomonf0674512015-07-28 13:26:15 -0700703
704 // These settings we will always want if a temp draw is performed. Initially set the config
705 // to srcConfig, though that may be modified if we decide to do a R/G swap.
706 tempDrawInfo->fTempSurfaceDesc.fFlags = kNone_GrSurfaceFlags;
707 tempDrawInfo->fTempSurfaceDesc.fConfig = srcConfig;
708 tempDrawInfo->fTempSurfaceDesc.fWidth = width;
709 tempDrawInfo->fTempSurfaceDesc.fHeight = height;
710 tempDrawInfo->fTempSurfaceDesc.fSampleCnt = 0;
711 tempDrawInfo->fTempSurfaceDesc.fOrigin = kTopLeft_GrSurfaceOrigin; // no CPU y-flip for TL.
712
713 bool configsAreRBSwaps = GrPixelConfigSwapRAndB(srcConfig) == dstSurface->config();
714
715 if (configsAreRBSwaps) {
716 if (!this->caps()->isConfigTexturable(srcConfig)) {
717 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
718 tempDrawInfo->fTempSurfaceDesc.fConfig = dstSurface->config();
bsalomon6c9cd552016-01-22 07:17:34 -0800719 tempDrawInfo->fSwizzle = GrSwizzle::BGRA();
720 tempDrawInfo->fWriteConfig = dstSurface->config();
bsalomon88c7b982015-07-31 11:20:16 -0700721 } else if (this->glCaps().rgba8888PixelsOpsAreSlow() &&
722 kRGBA_8888_GrPixelConfig == srcConfig) {
bsalomonf0674512015-07-28 13:26:15 -0700723 ElevateDrawPreference(drawPreference, kGpuPrefersDraw_DrawPreference);
724 tempDrawInfo->fTempSurfaceDesc.fConfig = dstSurface->config();
bsalomon6c9cd552016-01-22 07:17:34 -0800725 tempDrawInfo->fSwizzle = GrSwizzle::BGRA();
726 tempDrawInfo->fWriteConfig = dstSurface->config();
bsalomonf0674512015-07-28 13:26:15 -0700727 } else if (kGLES_GrGLStandard == this->glStandard() &&
728 this->glCaps().bgraIsInternalFormat()) {
729 // The internal format and external formats must match texture uploads so we can't
730 // swizzle while uploading when BGRA is a distinct internal format.
731 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
732 tempDrawInfo->fTempSurfaceDesc.fConfig = dstSurface->config();
bsalomon6c9cd552016-01-22 07:17:34 -0800733 tempDrawInfo->fSwizzle = GrSwizzle::BGRA();
734 tempDrawInfo->fWriteConfig = dstSurface->config();
bsalomonf0674512015-07-28 13:26:15 -0700735 }
736 }
737
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400738 if (!this->glCaps().unpackFlipYSupport() && kBottomLeft_GrSurfaceOrigin == dstOrigin) {
bsalomonf0674512015-07-28 13:26:15 -0700739 ElevateDrawPreference(drawPreference, kGpuPrefersDraw_DrawPreference);
740 }
741
742 return true;
743}
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000744
jvanverth17aa0472016-01-05 10:41:27 -0800745static bool check_write_and_transfer_input(GrGLTexture* glTex, GrSurface* surface,
746 GrPixelConfig config) {
bsalomon6cb3cbe2015-07-30 07:34:27 -0700747 if (!glTex) {
748 return false;
749 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000750
bsalomon16921ec2015-07-30 15:34:56 -0700751 // OpenGL doesn't do sRGB <-> linear conversions when reading and writing pixels.
752 if (GrPixelConfigIsSRGB(surface->config()) != GrPixelConfigIsSRGB(config)) {
753 return false;
754 }
755
bsalomone5286e02016-01-14 09:24:09 -0800756 // Write or transfer of pixels is not implemented for TEXTURE_EXTERNAL textures
757 if (GR_GL_TEXTURE_EXTERNAL == glTex->target()) {
bsalomon7ea33f52015-11-22 14:51:00 -0800758 return false;
759 }
760
jvanverth17aa0472016-01-05 10:41:27 -0800761 return true;
762}
763
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400764bool GrGLGpu::onWritePixels(GrSurface* surface, GrSurfaceOrigin origin,
jvanverth17aa0472016-01-05 10:41:27 -0800765 int left, int top, int width, int height,
cblume55f2d2d2016-02-26 13:20:48 -0800766 GrPixelConfig config,
Robert Phillips590533f2017-07-11 14:22:35 -0400767 const GrMipLevel texels[],
768 int mipLevelCount) {
jvanverth17aa0472016-01-05 10:41:27 -0800769 GrGLTexture* glTex = static_cast<GrGLTexture*>(surface->asTexture());
770
771 if (!check_write_and_transfer_input(glTex, surface, config)) {
772 return false;
773 }
774
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +0000775 this->setScratchTextureUnit();
bsalomon10528f12015-10-14 12:54:52 -0700776 GL_CALL(BindTexture(glTex->target(), glTex->textureID()));
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000777
Robert Phillips92de6312017-05-23 07:43:48 -0400778 return this->uploadTexData(glTex->config(), glTex->width(), glTex->height(),
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400779 origin, glTex->target(), kWrite_UploadType,
Robert Phillips590533f2017-07-11 14:22:35 -0400780 left, top, width, height, config, texels, mipLevelCount);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000781}
782
bsalomonf46a1242015-12-15 12:37:38 -0800783// For GL_[UN]PACK_ALIGNMENT.
784static inline GrGLint config_alignment(GrPixelConfig config) {
bsalomonf46a1242015-12-15 12:37:38 -0800785 switch (config) {
786 case kAlpha_8_GrPixelConfig:
Brian Osman986563b2017-01-10 14:20:02 -0500787 case kGray_8_GrPixelConfig:
bsalomonf46a1242015-12-15 12:37:38 -0800788 return 1;
789 case kRGB_565_GrPixelConfig:
790 case kRGBA_4444_GrPixelConfig:
791 case kAlpha_half_GrPixelConfig:
792 case kRGBA_half_GrPixelConfig:
793 return 2;
794 case kRGBA_8888_GrPixelConfig:
795 case kBGRA_8888_GrPixelConfig:
796 case kSRGBA_8888_GrPixelConfig:
brianosmana6359362016-03-21 06:55:37 -0700797 case kSBGRA_8888_GrPixelConfig:
Brian Salomonbf7b6202016-11-11 16:08:03 -0500798 case kRGBA_8888_sint_GrPixelConfig:
bsalomonf46a1242015-12-15 12:37:38 -0800799 case kRGBA_float_GrPixelConfig:
csmartdalton6aa0e112017-02-08 16:14:11 -0500800 case kRG_float_GrPixelConfig:
bsalomonf46a1242015-12-15 12:37:38 -0800801 return 4;
csmartdalton6aa0e112017-02-08 16:14:11 -0500802 case kUnknown_GrPixelConfig:
bsalomonf46a1242015-12-15 12:37:38 -0800803 return 0;
804 }
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400805 SK_ABORT("Invalid pixel config");
csmartdalton6aa0e112017-02-08 16:14:11 -0500806 return 0;
bsalomonf46a1242015-12-15 12:37:38 -0800807}
808
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400809bool GrGLGpu::onTransferPixels(GrTexture* texture,
810 int left, int top, int width, int height,
811 GrPixelConfig config, GrBuffer* transferBuffer,
812 size_t offset, size_t rowBytes) {
813 GrGLTexture* glTex = static_cast<GrGLTexture*>(texture);
814 GrPixelConfig texConfig = glTex->config();
815 SkASSERT(this->caps()->isConfigTexturable(texConfig));
816
817 if (!check_write_and_transfer_input(glTex, texture, config)) {
818 return false;
819 }
820
821 if (width <= 0 || width > SK_MaxS32 || height <= 0 || height > SK_MaxS32) {
822 return false;
823 }
824
825 this->setScratchTextureUnit();
826 GL_CALL(BindTexture(glTex->target(), glTex->textureID()));
827
828 SkASSERT(!transferBuffer->isMapped());
829 SkASSERT(!transferBuffer->isCPUBacked());
830 const GrGLBuffer* glBuffer = static_cast<const GrGLBuffer*>(transferBuffer);
831 this->bindBuffer(kXferCpuToGpu_GrBufferType, glBuffer);
832
Greg Daniel660cc992017-06-26 14:55:05 -0400833 SkDEBUGCODE(
834 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
835 SkIRect bounds = SkIRect::MakeWH(texture->width(), texture->height());
836 SkASSERT(bounds.contains(subRect));
837 )
838
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400839 size_t bpp = GrBytesPerPixel(config);
840 const size_t trimRowBytes = width * bpp;
Greg Daniel660cc992017-06-26 14:55:05 -0400841 if (!rowBytes) {
842 rowBytes = trimRowBytes;
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400843 }
Greg Daniel660cc992017-06-26 14:55:05 -0400844 const void* pixels = (void*)offset;
Bruce Dawson71479b72017-07-05 14:30:20 -0700845 if (width < 0 || height < 0) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400846 return false;
847 }
848
849 bool restoreGLRowLength = false;
850 if (trimRowBytes != rowBytes) {
851 // we should have checked for this support already
852 SkASSERT(this->glCaps().unpackRowLengthSupport());
853 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowBytes / bpp));
854 restoreGLRowLength = true;
855 }
856
857 // Internal format comes from the texture desc.
858 GrGLenum internalFormat;
859 // External format and type come from the upload data.
860 GrGLenum externalFormat;
861 GrGLenum externalType;
862 if (!this->glCaps().getTexImageFormats(texConfig, config, &internalFormat,
863 &externalFormat, &externalType)) {
864 return false;
865 }
866
867 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, config_alignment(texConfig)));
868 GL_CALL(TexSubImage2D(glTex->target(),
869 0,
870 left, top,
871 width,
872 height,
873 externalFormat, externalType,
874 pixels));
875
876 if (restoreGLRowLength) {
877 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
878 }
879
880 return true;
881}
882
cblume55f2d2d2016-02-26 13:20:48 -0800883/**
884 * Creates storage space for the texture and fills it with texels.
885 *
Brian Salomond1eaf492017-05-18 10:02:08 -0400886 * @param config Pixel config of the texture.
cblume55f2d2d2016-02-26 13:20:48 -0800887 * @param interface The GL interface in use.
cblume790d5132016-02-29 11:13:29 -0800888 * @param caps The capabilities of the GL device.
brianosman81a84852016-09-12 09:05:14 -0700889 * @param internalFormat The data format used for the internal storage of the texture. May be sized.
890 * @param internalFormatForTexStorage The data format used for the TexStorage API. Must be sized.
cblume55f2d2d2016-02-26 13:20:48 -0800891 * @param externalFormat The data format used for the external storage of the texture.
892 * @param externalType The type of the data used for the external storage of the texture.
893 * @param texels The texel data of the texture being created.
894 * @param baseWidth The width of the texture's base mipmap level
895 * @param baseHeight The height of the texture's base mipmap level
cblume55f2d2d2016-02-26 13:20:48 -0800896 */
Robert Phillips92de6312017-05-23 07:43:48 -0400897static bool allocate_and_populate_texture(GrPixelConfig config,
898 const GrGLInterface& interface,
899 const GrGLCaps& caps,
900 GrGLenum target,
901 GrGLenum internalFormat,
902 GrGLenum internalFormatForTexStorage,
903 GrGLenum externalFormat,
904 GrGLenum externalType,
Robert Phillips590533f2017-07-11 14:22:35 -0400905 const GrMipLevel texels[], int mipLevelCount,
Robert Phillips92de6312017-05-23 07:43:48 -0400906 int baseWidth, int baseHeight) {
cblume55f2d2d2016-02-26 13:20:48 -0800907 CLEAR_ERROR_BEFORE_ALLOC(&interface);
cblume790d5132016-02-29 11:13:29 -0800908
Brian Salomond1eaf492017-05-18 10:02:08 -0400909 bool useTexStorage = caps.isConfigTexSupportEnabled(config);
cblume790d5132016-02-29 11:13:29 -0800910 // We can only use TexStorage if we know we will not later change the storage requirements.
911 // This means if we may later want to add mipmaps, we cannot use TexStorage.
912 // Right now, we cannot know if we will later add mipmaps or not.
913 // The only time we can use TexStorage is when we already have the
Brian Salomonbf7b6202016-11-11 16:08:03 -0500914 // mipmaps or are using a format incompatible with MIP maps.
Robert Phillips590533f2017-07-11 14:22:35 -0400915 useTexStorage &= mipLevelCount > 1 || GrPixelConfigIsSint(config);
cblume790d5132016-02-29 11:13:29 -0800916
917 if (useTexStorage) {
918 // We never resize or change formats of textures.
cblume55f2d2d2016-02-26 13:20:48 -0800919 GL_ALLOC_CALL(&interface,
Robert Phillips590533f2017-07-11 14:22:35 -0400920 TexStorage2D(target, SkTMax(mipLevelCount, 1), internalFormatForTexStorage,
Brian Salomond1eaf492017-05-18 10:02:08 -0400921 baseWidth, baseHeight));
Brian Salomon0ec981b2017-05-15 13:48:50 -0400922 GrGLenum error = CHECK_ALLOC_ERROR(&interface);
cblume55f2d2d2016-02-26 13:20:48 -0800923 if (error != GR_GL_NO_ERROR) {
bsalomone699d0c2016-03-09 06:25:15 -0800924 return false;
cblume790d5132016-02-29 11:13:29 -0800925 } else {
Robert Phillips590533f2017-07-11 14:22:35 -0400926 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
cblume790d5132016-02-29 11:13:29 -0800927 const void* currentMipData = texels[currentMipLevel].fPixels;
928 if (currentMipData == nullptr) {
929 continue;
930 }
931 int twoToTheMipLevel = 1 << currentMipLevel;
Brian Salomond1eaf492017-05-18 10:02:08 -0400932 int currentWidth = SkTMax(1, baseWidth / twoToTheMipLevel);
933 int currentHeight = SkTMax(1, baseHeight / twoToTheMipLevel);
cblume790d5132016-02-29 11:13:29 -0800934
935 GR_GL_CALL(&interface,
936 TexSubImage2D(target,
937 currentMipLevel,
938 0, // left
939 0, // top
940 currentWidth,
941 currentHeight,
942 externalFormat, externalType,
943 currentMipData));
944 }
bsalomone699d0c2016-03-09 06:25:15 -0800945 return true;
cblume790d5132016-02-29 11:13:29 -0800946 }
947 } else {
Robert Phillips590533f2017-07-11 14:22:35 -0400948 if (!mipLevelCount) {
cblume790d5132016-02-29 11:13:29 -0800949 GL_ALLOC_CALL(&interface,
950 TexImage2D(target,
bsalomone699d0c2016-03-09 06:25:15 -0800951 0,
cblume790d5132016-02-29 11:13:29 -0800952 internalFormat,
bsalomone699d0c2016-03-09 06:25:15 -0800953 baseWidth,
954 baseHeight,
cblume790d5132016-02-29 11:13:29 -0800955 0, // border
956 externalFormat, externalType,
bsalomone699d0c2016-03-09 06:25:15 -0800957 nullptr));
Brian Salomon0ec981b2017-05-15 13:48:50 -0400958 GrGLenum error = CHECK_ALLOC_ERROR(&interface);
cblume790d5132016-02-29 11:13:29 -0800959 if (error != GR_GL_NO_ERROR) {
bsalomone699d0c2016-03-09 06:25:15 -0800960 return false;
961 }
962 } else {
Robert Phillips590533f2017-07-11 14:22:35 -0400963 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
bsalomone699d0c2016-03-09 06:25:15 -0800964 int twoToTheMipLevel = 1 << currentMipLevel;
965 int currentWidth = SkTMax(1, baseWidth / twoToTheMipLevel);
966 int currentHeight = SkTMax(1, baseHeight / twoToTheMipLevel);
Greg Daniel8b059bd2017-09-28 20:46:45 +0000967 const void* currentMipData = texels[currentMipLevel].fPixels;
bsalomone699d0c2016-03-09 06:25:15 -0800968 // Even if curremtMipData is nullptr, continue to call TexImage2D.
969 // This will allocate texture memory which we can later populate.
970 GL_ALLOC_CALL(&interface,
971 TexImage2D(target,
972 currentMipLevel,
973 internalFormat,
974 currentWidth,
975 currentHeight,
976 0, // border
977 externalFormat, externalType,
978 currentMipData));
Brian Salomon0ec981b2017-05-15 13:48:50 -0400979 GrGLenum error = CHECK_ALLOC_ERROR(&interface);
bsalomone699d0c2016-03-09 06:25:15 -0800980 if (error != GR_GL_NO_ERROR) {
981 return false;
982 }
cblume790d5132016-02-29 11:13:29 -0800983 }
cblume55f2d2d2016-02-26 13:20:48 -0800984 }
985 }
bsalomone699d0c2016-03-09 06:25:15 -0800986 return true;
cblume55f2d2d2016-02-26 13:20:48 -0800987}
988
989/**
cblume55f2d2d2016-02-26 13:20:48 -0800990 * After a texture is created, any state which was altered during its creation
991 * needs to be restored.
992 *
993 * @param interface The GL interface to use.
994 * @param caps The capabilities of the GL device.
995 * @param restoreGLRowLength Should the row length unpacking be restored?
996 * @param glFlipY Did GL flip the texture vertically?
997 */
998static void restore_pixelstore_state(const GrGLInterface& interface, const GrGLCaps& caps,
999 bool restoreGLRowLength, bool glFlipY) {
1000 if (restoreGLRowLength) {
1001 SkASSERT(caps.unpackRowLengthSupport());
1002 GR_GL_CALL(&interface, PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
1003 }
1004 if (glFlipY) {
1005 GR_GL_CALL(&interface, PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
1006 }
1007}
1008
Brian Osman9b560242017-09-05 15:34:52 -04001009void GrGLGpu::unbindCpuToGpuXferBuffer() {
1010 auto& xferBufferState = fHWBufferState[kXferCpuToGpu_GrBufferType];
1011 if (!xferBufferState.fBoundBufferUniqueID.isInvalid()) {
1012 GL_CALL(BindBuffer(xferBufferState.fGLTarget, 0));
1013 xferBufferState.invalidate();
1014 }
1015
1016}
1017
Brian Salomond1eaf492017-05-18 10:02:08 -04001018bool GrGLGpu::uploadTexData(GrPixelConfig texConfig, int texWidth, int texHeight,
1019 GrSurfaceOrigin texOrigin, GrGLenum target, UploadType uploadType,
1020 int left, int top, int width, int height, GrPixelConfig dataConfig,
Greg Daniel834f1202017-10-09 15:06:20 -04001021 const GrMipLevel texels[], int mipLevelCount,
Greg Daniel0fc4d2d2017-10-12 11:23:36 -04001022 GrMipMapsStatus* mipMapsStatus) {
Brian Salomond1eaf492017-05-18 10:02:08 -04001023 SkASSERT(this->caps()->isConfigTexturable(texConfig));
Greg Daniel660cc992017-06-26 14:55:05 -04001024 SkDEBUGCODE(
1025 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
1026 SkIRect bounds = SkIRect::MakeWH(texWidth, texHeight);
1027 SkASSERT(bounds.contains(subRect));
1028 )
Robert Phillips590533f2017-07-11 14:22:35 -04001029 SkASSERT(1 == mipLevelCount ||
Greg Daniel660cc992017-06-26 14:55:05 -04001030 (0 == left && 0 == top && width == texWidth && height == texHeight));
bsalomon5b30c6f2015-12-17 14:17:34 -08001031
Brian Osman9b560242017-09-05 15:34:52 -04001032 this->unbindCpuToGpuXferBuffer();
Jim Van Verth2e5eaf02017-06-21 15:55:46 -04001033
cblume55f2d2d2016-02-26 13:20:48 -08001034 // texels is const.
1035 // But we may need to flip the texture vertically to prepare it.
1036 // Rather than flip in place and alter the incoming data,
1037 // we allocate a new buffer to flip into.
1038 // This means we need to make a non-const shallow copy of texels.
Robert Phillips0f992772017-07-12 08:24:56 -04001039 SkAutoTMalloc<GrMipLevel> texelsShallowCopy;
1040
1041 if (mipLevelCount) {
1042 texelsShallowCopy.reset(mipLevelCount);
1043 memcpy(texelsShallowCopy.get(), texels, mipLevelCount*sizeof(GrMipLevel));
1044 }
cblume55f2d2d2016-02-26 13:20:48 -08001045
cblume55f2d2d2016-02-26 13:20:48 -08001046 const GrGLInterface* interface = this->glInterface();
1047 const GrGLCaps& caps = this->glCaps();
1048
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001049 size_t bpp = GrBytesPerPixel(dataConfig);
cblume55f2d2d2016-02-26 13:20:48 -08001050
1051 if (width == 0 || height == 0) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +00001052 return false;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001053 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00001054
bsalomon5b30c6f2015-12-17 14:17:34 -08001055 // Internal format comes from the texture desc.
bsalomon76148af2016-01-12 11:13:47 -08001056 GrGLenum internalFormat;
bsalomon5b30c6f2015-12-17 14:17:34 -08001057 // External format and type come from the upload data.
bsalomon76148af2016-01-12 11:13:47 -08001058 GrGLenum externalFormat;
1059 GrGLenum externalType;
Brian Salomond1eaf492017-05-18 10:02:08 -04001060 if (!this->glCaps().getTexImageFormats(texConfig, dataConfig, &internalFormat, &externalFormat,
1061 &externalType)) {
bsalomon76148af2016-01-12 11:13:47 -08001062 return false;
1063 }
brianosman81a84852016-09-12 09:05:14 -07001064 // TexStorage requires a sized format, and internalFormat may or may not be
Brian Salomond1eaf492017-05-18 10:02:08 -04001065 GrGLenum internalFormatForTexStorage = this->glCaps().configSizedInternalFormat(texConfig);
brianosman81a84852016-09-12 09:05:14 -07001066
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001067 /*
bsalomon5b30c6f2015-12-17 14:17:34 -08001068 * Check whether to allocate a temporary buffer for flipping y or
bsalomon@google.com6f379512011-11-16 20:36:03 +00001069 * because our srcData has extra bytes past each row. If so, we need
1070 * to trim those off here, since GL ES may not let us specify
1071 * GL_UNPACK_ROW_LENGTH.
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001072 */
bsalomon@google.com6f379512011-11-16 20:36:03 +00001073 bool restoreGLRowLength = false;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +00001074 bool swFlipY = false;
1075 bool glFlipY = false;
cblume55f2d2d2016-02-26 13:20:48 -08001076
Robert Phillips590533f2017-07-11 14:22:35 -04001077 if (kBottomLeft_GrSurfaceOrigin == texOrigin && mipLevelCount) {
cblume55f2d2d2016-02-26 13:20:48 -08001078 if (caps.unpackFlipYSupport()) {
1079 glFlipY = true;
1080 } else {
1081 swFlipY = true;
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001082 }
cblume55f2d2d2016-02-26 13:20:48 -08001083 }
1084
1085 // in case we need a temporary, trimmed copy of the src pixels
1086 SkAutoSMalloc<128 * 128> tempStorage;
1087
Greg Daniel0fc4d2d2017-10-12 11:23:36 -04001088 if (mipMapsStatus) {
1089 *mipMapsStatus = GrMipMapsStatus::kValid;
Greg Daniel834f1202017-10-09 15:06:20 -04001090 }
1091
cblume55f2d2d2016-02-26 13:20:48 -08001092 // find the combined size of all the mip levels and the relative offset of
1093 // each into the collective buffer
Greg Daniel55afd6d2017-09-29 09:32:44 -04001094 size_t combinedBufferSize = 0;
1095 SkTArray<size_t> individualMipOffsets(mipLevelCount);
Robert Phillips590533f2017-07-11 14:22:35 -04001096 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
Greg Daniel55afd6d2017-09-29 09:32:44 -04001097 if (texelsShallowCopy[currentMipLevel].fPixels) {
1098 int twoToTheMipLevel = 1 << currentMipLevel;
1099 int currentWidth = SkTMax(1, width / twoToTheMipLevel);
1100 int currentHeight = SkTMax(1, height / twoToTheMipLevel);
1101 const size_t trimmedSize = currentWidth * bpp * currentHeight;
1102 individualMipOffsets.push_back(combinedBufferSize);
1103 combinedBufferSize += trimmedSize;
1104 } else {
Greg Daniel0fc4d2d2017-10-12 11:23:36 -04001105 if (mipMapsStatus) {
1106 *mipMapsStatus = GrMipMapsStatus::kDirty;
Greg Daniel834f1202017-10-09 15:06:20 -04001107 }
Greg Daniel55afd6d2017-09-29 09:32:44 -04001108 individualMipOffsets.push_back(0);
1109 }
Greg Daniel0fc4d2d2017-10-12 11:23:36 -04001110 }
1111 if (mipMapsStatus && mipLevelCount <= 1) {
1112 *mipMapsStatus = GrMipMapsStatus::kNotAllocated;
cblume55f2d2d2016-02-26 13:20:48 -08001113 }
Greg Daniel55afd6d2017-09-29 09:32:44 -04001114 char* buffer = (char*)tempStorage.reset(combinedBufferSize);
cblume55f2d2d2016-02-26 13:20:48 -08001115
Robert Phillips590533f2017-07-11 14:22:35 -04001116 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
Greg Daniel55afd6d2017-09-29 09:32:44 -04001117 if (!texelsShallowCopy[currentMipLevel].fPixels) {
1118 continue;
1119 }
cblume55f2d2d2016-02-26 13:20:48 -08001120 int twoToTheMipLevel = 1 << currentMipLevel;
1121 int currentWidth = SkTMax(1, width / twoToTheMipLevel);
1122 int currentHeight = SkTMax(1, height / twoToTheMipLevel);
1123 const size_t trimRowBytes = currentWidth * bpp;
1124
1125 /*
1126 * check whether to allocate a temporary buffer for flipping y or
1127 * because our srcData has extra bytes past each row. If so, we need
1128 * to trim those off here, since GL ES may not let us specify
1129 * GL_UNPACK_ROW_LENGTH.
1130 */
1131 restoreGLRowLength = false;
1132
Greg Daniel55afd6d2017-09-29 09:32:44 -04001133 const size_t rowBytes = texelsShallowCopy[currentMipLevel].fRowBytes
1134 ? texelsShallowCopy[currentMipLevel].fRowBytes
1135 : trimRowBytes;
Greg Daniel660cc992017-06-26 14:55:05 -04001136
ericrk154349b2016-05-10 14:36:53 -07001137 // TODO: This optimization should be enabled with or without mips.
1138 // For use with mips, we must set GR_GL_UNPACK_ROW_LENGTH once per
1139 // mip level, before calling glTexImage2D.
Robert Phillips590533f2017-07-11 14:22:35 -04001140 const bool usesMips = mipLevelCount > 1;
ericrk154349b2016-05-10 14:36:53 -07001141 if (caps.unpackRowLengthSupport() && !swFlipY && !usesMips) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001142 // can't use this for flipping, only non-neg values allowed. :(
1143 if (rowBytes != trimRowBytes) {
1144 GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
cblume55f2d2d2016-02-26 13:20:48 -08001145 GR_GL_CALL(interface, PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength));
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001146 restoreGLRowLength = true;
1147 }
Jim Van Verth2e5eaf02017-06-21 15:55:46 -04001148 } else if (trimRowBytes != rowBytes || swFlipY) {
1149 // copy data into our new storage, skipping the trailing bytes
1150 const char* src = (const char*)texelsShallowCopy[currentMipLevel].fPixels;
1151 if (swFlipY && currentHeight >= 1) {
1152 src += (currentHeight - 1) * rowBytes;
Jim Van Verthf7037d02017-06-21 10:52:43 -04001153 }
Greg Daniel55afd6d2017-09-29 09:32:44 -04001154 char* dst = buffer + individualMipOffsets[currentMipLevel];
Jim Van Verth2e5eaf02017-06-21 15:55:46 -04001155 for (int y = 0; y < currentHeight; y++) {
1156 memcpy(dst, src, trimRowBytes);
1157 if (swFlipY) {
1158 src -= rowBytes;
1159 } else {
1160 src += rowBytes;
1161 }
1162 dst += trimRowBytes;
1163 }
1164 // now point data to our copied version
1165 texelsShallowCopy[currentMipLevel].fPixels = buffer +
Greg Daniel55afd6d2017-09-29 09:32:44 -04001166 individualMipOffsets[currentMipLevel];
Jim Van Verth2e5eaf02017-06-21 15:55:46 -04001167 texelsShallowCopy[currentMipLevel].fRowBytes = trimRowBytes;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001168 }
bsalomone699d0c2016-03-09 06:25:15 -08001169 }
1170
Robert Phillips590533f2017-07-11 14:22:35 -04001171 if (mipLevelCount) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001172 if (glFlipY) {
cblume55f2d2d2016-02-26 13:20:48 -08001173 GR_GL_CALL(interface, PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_TRUE));
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001174 }
Brian Salomond1eaf492017-05-18 10:02:08 -04001175 GR_GL_CALL(interface, PixelStorei(GR_GL_UNPACK_ALIGNMENT, config_alignment(texConfig)));
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001176 }
cblume55f2d2d2016-02-26 13:20:48 -08001177
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001178 bool succeeded = true;
Brian Salomond1eaf492017-05-18 10:02:08 -04001179 if (kNewTexture_UploadType == uploadType) {
1180 if (0 == left && 0 == top && texWidth == width && texHeight == height) {
Robert Phillips92de6312017-05-23 07:43:48 -04001181 succeeded = allocate_and_populate_texture(
Brian Salomond1eaf492017-05-18 10:02:08 -04001182 texConfig, *interface, caps, target, internalFormat,
Robert Phillips590533f2017-07-11 14:22:35 -04001183 internalFormatForTexStorage, externalFormat, externalType,
1184 texelsShallowCopy, mipLevelCount, width, height);
Brian Salomond1eaf492017-05-18 10:02:08 -04001185 } else {
1186 succeeded = false;
1187 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001188 } else {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +00001189 if (swFlipY || glFlipY) {
Brian Salomond1eaf492017-05-18 10:02:08 -04001190 top = texHeight - (top + height);
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001191 }
Robert Phillips590533f2017-07-11 14:22:35 -04001192 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
Greg Daniel55afd6d2017-09-29 09:32:44 -04001193 if (!texelsShallowCopy[currentMipLevel].fPixels) {
1194 continue;
1195 }
cblume55f2d2d2016-02-26 13:20:48 -08001196 int twoToTheMipLevel = 1 << currentMipLevel;
1197 int currentWidth = SkTMax(1, width / twoToTheMipLevel);
1198 int currentHeight = SkTMax(1, height / twoToTheMipLevel);
cblume55f2d2d2016-02-26 13:20:48 -08001199
1200 GL_CALL(TexSubImage2D(target,
1201 currentMipLevel,
1202 left, top,
1203 currentWidth,
1204 currentHeight,
1205 externalFormat, externalType,
1206 texelsShallowCopy[currentMipLevel].fPixels));
1207 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00001208 }
1209
cblume55f2d2d2016-02-26 13:20:48 -08001210 restore_pixelstore_state(*interface, caps, restoreGLRowLength, glFlipY);
1211
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001212 return succeeded;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001213}
1214
bsalomon424cc262015-05-22 10:37:30 -07001215static bool renderbuffer_storage_msaa(const GrGLContext& ctx,
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001216 int sampleCount,
1217 GrGLenum format,
1218 int width, int height) {
robertphillips@google.com6177e692013-02-28 20:16:25 +00001219 CLEAR_ERROR_BEFORE_ALLOC(ctx.interface());
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +00001220 SkASSERT(GrGLCaps::kNone_MSFBOType != ctx.caps()->msFBOType());
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +00001221 switch (ctx.caps()->msFBOType()) {
Brian Salomon00731b42016-10-14 11:30:51 -04001222 case GrGLCaps::kStandard_MSFBOType:
vbuzinovdded6962015-06-12 08:59:45 -07001223 case GrGLCaps::kMixedSamples_MSFBOType:
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001224 GL_ALLOC_CALL(ctx.interface(),
1225 RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
1226 sampleCount,
1227 format,
1228 width, height));
1229 break;
1230 case GrGLCaps::kES_Apple_MSFBOType:
1231 GL_ALLOC_CALL(ctx.interface(),
1232 RenderbufferStorageMultisampleES2APPLE(GR_GL_RENDERBUFFER,
1233 sampleCount,
1234 format,
1235 width, height));
1236 break;
1237 case GrGLCaps::kES_EXT_MsToTexture_MSFBOType:
1238 case GrGLCaps::kES_IMG_MsToTexture_MSFBOType:
1239 GL_ALLOC_CALL(ctx.interface(),
1240 RenderbufferStorageMultisampleES2EXT(GR_GL_RENDERBUFFER,
1241 sampleCount,
1242 format,
1243 width, height));
1244 break;
1245 case GrGLCaps::kNone_MSFBOType:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -04001246 SK_ABORT("Shouldn't be here if we don't support multisampled renderbuffers.");
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001247 break;
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001248 }
Brian Salomon9251d4e2015-03-17 16:03:19 -04001249 return (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctx.interface()));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001250}
1251
egdanielb0e1be22015-04-22 13:27:39 -07001252bool GrGLGpu::createRenderTargetObjects(const GrSurfaceDesc& desc,
bsalomon091f60c2015-11-10 11:54:56 -08001253 const GrGLTextureInfo& texInfo,
bsalomonb15b4c12014-10-29 12:41:57 -07001254 GrGLRenderTarget::IDDesc* idDesc) {
1255 idDesc->fMSColorRenderbufferID = 0;
egdanield803f272015-03-18 13:01:52 -07001256 idDesc->fRTFBOID = 0;
kkinnunen2e6055b2016-04-22 01:48:29 -07001257 idDesc->fRTFBOOwnership = GrBackendObjectOwnership::kOwned;
egdanield803f272015-03-18 13:01:52 -07001258 idDesc->fTexFBOID = 0;
robertphillips76948d42016-05-04 12:47:41 -07001259 SkASSERT((GrGLCaps::kMixedSamples_MSFBOType == this->glCaps().msFBOType()) ==
1260 this->caps()->usesMixedSamples());
csmartdaltonf9635992016-08-10 11:09:07 -07001261 idDesc->fIsMixedSampled = desc.fSampleCnt > 0 && this->caps()->usesMixedSamples();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001262
1263 GrGLenum status;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001264
bsalomona11e5fc2015-12-18 07:59:41 -08001265 GrGLenum colorRenderbufferFormat = 0; // suppress warning
bsalomon@google.comab15d612011-08-09 12:57:56 +00001266
bsalomonb15b4c12014-10-29 12:41:57 -07001267 if (desc.fSampleCnt > 0 && GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.com347c3822013-05-01 20:10:01 +00001268 goto FAILED;
1269 }
1270
egdanield803f272015-03-18 13:01:52 -07001271 GL_CALL(GenFramebuffers(1, &idDesc->fTexFBOID));
1272 if (!idDesc->fTexFBOID) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001273 goto FAILED;
1274 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001275
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001276 // If we are using multisampling we will create two FBOS. We render to one and then resolve to
1277 // the texture bound to the other. The exception is the IMG multisample extension. With this
1278 // extension the texture is multisampled when rendered to and then auto-resolves it when it is
1279 // rendered from.
bsalomonb15b4c12014-10-29 12:41:57 -07001280 if (desc.fSampleCnt > 0 && this->glCaps().usesMSAARenderBuffers()) {
egdanield803f272015-03-18 13:01:52 -07001281 GL_CALL(GenFramebuffers(1, &idDesc->fRTFBOID));
bsalomonb15b4c12014-10-29 12:41:57 -07001282 GL_CALL(GenRenderbuffers(1, &idDesc->fMSColorRenderbufferID));
egdanield803f272015-03-18 13:01:52 -07001283 if (!idDesc->fRTFBOID ||
bsalomona11e5fc2015-12-18 07:59:41 -08001284 !idDesc->fMSColorRenderbufferID) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001285 goto FAILED;
1286 }
bsalomon76148af2016-01-12 11:13:47 -08001287 if (!this->glCaps().getRenderbufferFormat(desc.fConfig, &colorRenderbufferFormat)) {
1288 return false;
1289 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001290 } else {
egdanield803f272015-03-18 13:01:52 -07001291 idDesc->fRTFBOID = idDesc->fTexFBOID;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001292 }
1293
egdanield803f272015-03-18 13:01:52 -07001294 // below here we may bind the FBO
Robert Phillips294870f2016-11-11 12:38:40 -05001295 fHWBoundRenderTargetUniqueID.makeInvalid();
egdanield803f272015-03-18 13:01:52 -07001296 if (idDesc->fRTFBOID != idDesc->fTexFBOID) {
bsalomonb15b4c12014-10-29 12:41:57 -07001297 SkASSERT(desc.fSampleCnt > 0);
1298 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, idDesc->fMSColorRenderbufferID));
bsalomon424cc262015-05-22 10:37:30 -07001299 if (!renderbuffer_storage_msaa(*fGLContext,
bsalomonb15b4c12014-10-29 12:41:57 -07001300 desc.fSampleCnt,
bsalomona11e5fc2015-12-18 07:59:41 -08001301 colorRenderbufferFormat,
bsalomonb15b4c12014-10-29 12:41:57 -07001302 desc.fWidth, desc.fHeight)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001303 goto FAILED;
1304 }
egdanield803f272015-03-18 13:01:52 -07001305 fStats.incRenderTargetBinds();
1306 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, idDesc->fRTFBOID));
1307 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon10528f12015-10-14 12:54:52 -07001308 GR_GL_COLOR_ATTACHMENT0,
1309 GR_GL_RENDERBUFFER,
1310 idDesc->fMSColorRenderbufferID));
Brian Salomon0ec981b2017-05-15 13:48:50 -04001311 if (!this->glCaps().isConfigVerifiedColorAttachment(desc.fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001312 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1313 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
1314 goto FAILED;
1315 }
bsalomon424cc262015-05-22 10:37:30 -07001316 fGLContext->caps()->markConfigAsValidColorAttachment(desc.fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001317 }
1318 }
egdanield803f272015-03-18 13:01:52 -07001319 fStats.incRenderTargetBinds();
1320 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, idDesc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001321
bsalomonb15b4c12014-10-29 12:41:57 -07001322 if (this->glCaps().usesImplicitMSAAResolve() && desc.fSampleCnt > 0) {
egdanield803f272015-03-18 13:01:52 -07001323 GL_CALL(FramebufferTexture2DMultisample(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001324 GR_GL_COLOR_ATTACHMENT0,
bsalomon091f60c2015-11-10 11:54:56 -08001325 texInfo.fTarget,
1326 texInfo.fID, 0, desc.fSampleCnt));
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001327 } else {
egdanield803f272015-03-18 13:01:52 -07001328 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001329 GR_GL_COLOR_ATTACHMENT0,
bsalomon091f60c2015-11-10 11:54:56 -08001330 texInfo.fTarget,
1331 texInfo.fID, 0));
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001332 }
Brian Salomon0ec981b2017-05-15 13:48:50 -04001333 if (!this->glCaps().isConfigVerifiedColorAttachment(desc.fConfig)) {
egdanield803f272015-03-18 13:01:52 -07001334 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001335 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
1341 return true;
1342
1343FAILED:
bsalomonb15b4c12014-10-29 12:41:57 -07001344 if (idDesc->fMSColorRenderbufferID) {
1345 GL_CALL(DeleteRenderbuffers(1, &idDesc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001346 }
egdanield803f272015-03-18 13:01:52 -07001347 if (idDesc->fRTFBOID != idDesc->fTexFBOID) {
1348 GL_CALL(DeleteFramebuffers(1, &idDesc->fRTFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001349 }
egdanield803f272015-03-18 13:01:52 -07001350 if (idDesc->fTexFBOID) {
1351 GL_CALL(DeleteFramebuffers(1, &idDesc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001352 }
1353 return false;
1354}
1355
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001356// good to set a break-point here to know when createTexture fails
Robert Phillips67d52cf2017-06-05 13:38:13 -04001357static sk_sp<GrTexture> return_null_texture() {
mtklein@google.com330313a2013-08-22 15:37:26 +00001358// SkDEBUGFAIL("null texture");
halcanary96fcdcc2015-08-27 07:41:13 -07001359 return nullptr;
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001360}
1361
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +00001362#if 0 && defined(SK_DEBUG)
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001363static size_t as_size_t(int x) {
1364 return x;
1365}
1366#endif
1367
cblume55f2d2d2016-02-26 13:20:48 -08001368static void set_initial_texture_params(const GrGLInterface* interface,
1369 const GrGLTextureInfo& info,
1370 GrGLTexture::TexParams* initialTexParams) {
1371 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
1372 // drivers have a bug where an FBO won't be complete if it includes a
1373 // texture that is not mipmap complete (considering the filter in use).
1374 // we only set a subset here so invalidate first
1375 initialTexParams->invalidate();
1376 initialTexParams->fMinFilter = GR_GL_NEAREST;
1377 initialTexParams->fMagFilter = GR_GL_NEAREST;
1378 initialTexParams->fWrapS = GR_GL_CLAMP_TO_EDGE;
1379 initialTexParams->fWrapT = GR_GL_CLAMP_TO_EDGE;
1380 GR_GL_CALL(interface, TexParameteri(info.fTarget,
1381 GR_GL_TEXTURE_MAG_FILTER,
1382 initialTexParams->fMagFilter));
1383 GR_GL_CALL(interface, TexParameteri(info.fTarget,
1384 GR_GL_TEXTURE_MIN_FILTER,
1385 initialTexParams->fMinFilter));
1386 GR_GL_CALL(interface, TexParameteri(info.fTarget,
1387 GR_GL_TEXTURE_WRAP_S,
1388 initialTexParams->fWrapS));
1389 GR_GL_CALL(interface, TexParameteri(info.fTarget,
1390 GR_GL_TEXTURE_WRAP_T,
1391 initialTexParams->fWrapT));
1392}
1393
Robert Phillips67d52cf2017-06-05 13:38:13 -04001394sk_sp<GrTexture> GrGLGpu::onCreateTexture(const GrSurfaceDesc& desc,
1395 SkBudgeted budgeted,
Robert Phillips590533f2017-07-11 14:22:35 -04001396 const GrMipLevel texels[],
1397 int mipLevelCount) {
bsalomon@google.com8a70eef2013-03-19 13:58:55 +00001398 // We fail if the MSAA was requested and is not available.
1399 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType() && desc.fSampleCnt) {
tfarina38406c82014-10-31 07:11:12 -07001400 //SkDebugf("MSAA RT requested but not supported on this platform.");
bsalomon@google.com8a70eef2013-03-19 13:58:55 +00001401 return return_null_texture();
1402 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001403
Brian Salomond17b4a62017-05-23 16:53:47 -04001404 bool performClear = (desc.fFlags & kPerformInitialClear_GrSurfaceFlag);
Brian Salomond17b4a62017-05-23 16:53:47 -04001405
Robert Phillips590533f2017-07-11 14:22:35 -04001406 GrMipLevel zeroLevel;
Brian Salomond17b4a62017-05-23 16:53:47 -04001407 std::unique_ptr<uint8_t[]> zeros;
1408 // TODO: remove the GrPixelConfigIsSint test. This is here because we have yet to add support
1409 // for glClearBuffer* which must be used instead of glClearColor/glClear for integer FBO
1410 // attachments.
1411 if (performClear && !this->glCaps().clearTextureSupport() &&
1412 (!this->glCaps().canConfigBeFBOColorAttachment(desc.fConfig) ||
1413 GrPixelConfigIsSint(desc.fConfig))) {
1414 size_t rowSize = GrBytesPerPixel(desc.fConfig) * desc.fWidth;
1415 size_t size = rowSize * desc.fHeight;
1416 zeros.reset(new uint8_t[size]);
1417 memset(zeros.get(), 0, size);
Robert Phillips590533f2017-07-11 14:22:35 -04001418 zeroLevel.fPixels = zeros.get();
1419 zeroLevel.fRowBytes = 0;
1420 texels = &zeroLevel;
1421 mipLevelCount = 1;
Brian Salomond17b4a62017-05-23 16:53:47 -04001422 performClear = false;
1423 }
1424
Brian Salomond34edf32017-05-19 15:45:48 -04001425 bool isRenderTarget = SkToBool(desc.fFlags & kRenderTarget_GrSurfaceFlag);
reed@google.comac10a2d2010-12-22 21:39:39 +00001426
bsalomonb15b4c12014-10-29 12:41:57 -07001427 GrGLTexture::IDDesc idDesc;
kkinnunen2e6055b2016-04-22 01:48:29 -07001428 idDesc.fOwnership = GrBackendObjectOwnership::kOwned;
Greg Daniel0fc4d2d2017-10-12 11:23:36 -04001429 GrMipMapsStatus mipMapsStatus;
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001430 GrGLTexture::TexParams initialTexParams;
Robert Phillips590533f2017-07-11 14:22:35 -04001431 if (!this->createTextureImpl(desc, &idDesc.fInfo, isRenderTarget, &initialTexParams,
Greg Daniel0fc4d2d2017-10-12 11:23:36 -04001432 texels, mipLevelCount, &mipMapsStatus)) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001433 return return_null_texture();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001434 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001435
Robert Phillips67d52cf2017-06-05 13:38:13 -04001436 sk_sp<GrGLTexture> tex;
Brian Salomond34edf32017-05-19 15:45:48 -04001437 if (isRenderTarget) {
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001438 // unbind the texture from the texture unit before binding it to the frame buffer
bsalomon091f60c2015-11-10 11:54:56 -08001439 GL_CALL(BindTexture(idDesc.fInfo.fTarget, 0));
bsalomon5236cf42015-01-14 10:42:08 -08001440 GrGLRenderTarget::IDDesc rtIDDesc;
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001441
kkinnunen2e6055b2016-04-22 01:48:29 -07001442 if (!this->createRenderTargetObjects(desc, idDesc.fInfo, &rtIDDesc)) {
bsalomon091f60c2015-11-10 11:54:56 -08001443 GL_CALL(DeleteTextures(1, &idDesc.fInfo.fID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001444 return return_null_texture();
1445 }
Robert Phillips67d52cf2017-06-05 13:38:13 -04001446 tex = sk_make_sp<GrGLTextureRenderTarget>(this, budgeted, desc, idDesc, rtIDDesc,
Greg Daniel0fc4d2d2017-10-12 11:23:36 -04001447 mipMapsStatus);
Brian Salomon9bada542017-06-12 12:09:30 -04001448 tex->baseLevelWasBoundToFBO();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001449 } else {
Greg Daniel0fc4d2d2017-10-12 11:23:36 -04001450 tex = sk_make_sp<GrGLTexture>(this, budgeted, desc, idDesc, mipMapsStatus);
reed@google.comac10a2d2010-12-22 21:39:39 +00001451 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001452 tex->setCachedTexParams(initialTexParams, this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00001453#ifdef TRACE_TEXTURE_CREATION
tfarina38406c82014-10-31 07:11:12 -07001454 SkDebugf("--- new texture [%d] size=(%d %d) config=%d\n",
brianosmanc4459f62016-07-19 08:02:20 -07001455 idDesc.fInfo.fID, desc.fWidth, desc.fHeight, desc.fConfig);
reed@google.comac10a2d2010-12-22 21:39:39 +00001456#endif
Brian Salomond17b4a62017-05-23 16:53:47 -04001457 if (tex && performClear) {
1458 if (this->glCaps().clearTextureSupport()) {
1459 GrGLenum format = GrPixelConfigIsSint(tex->config()) ? GR_GL_RGBA_INTEGER : GR_GL_RGBA;
1460 static constexpr uint32_t kZero = 0;
1461 GL_CALL(ClearTexImage(tex->textureID(), 0, format, GR_GL_UNSIGNED_BYTE, &kZero));
1462 } else {
1463 SkASSERT(!GrPixelConfigIsSint(desc.fConfig));
1464 GrGLIRect viewport;
Robert Phillips67d52cf2017-06-05 13:38:13 -04001465 this->bindSurfaceFBOForPixelOps(tex.get(), GR_GL_FRAMEBUFFER, &viewport,
1466 kDst_TempFBOTarget);
Brian Salomond17b4a62017-05-23 16:53:47 -04001467 this->disableScissor();
1468 this->disableWindowRectangles();
1469 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
1470 fHWWriteToColor = kYes_TriState;
1471 GL_CALL(ClearColor(0, 0, 0, 0));
1472 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
Robert Phillips67d52cf2017-06-05 13:38:13 -04001473 this->unbindTextureFBOForPixelOps(GR_GL_FRAMEBUFFER, tex.get());
Brian Salomond17b4a62017-05-23 16:53:47 -04001474 fHWBoundRenderTargetUniqueID.makeInvalid();
1475 }
1476 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001477 return tex;
1478}
1479
1480namespace {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001481
egdaniel8dc7c3a2015-04-16 11:22:42 -07001482const GrGLuint kUnknownBitCount = GrGLStencilAttachment::kUnknownBitCount;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001483
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001484void inline get_stencil_rb_sizes(const GrGLInterface* gl,
egdaniel8dc7c3a2015-04-16 11:22:42 -07001485 GrGLStencilAttachment::Format* format) {
sugoi@google.com6ba0b0f2013-05-03 13:52:32 +00001486
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001487 // we shouldn't ever know one size and not the other
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001488 SkASSERT((kUnknownBitCount == format->fStencilBits) ==
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001489 (kUnknownBitCount == format->fTotalBits));
1490 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001491 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001492 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1493 (GrGLint*)&format->fStencilBits);
1494 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001495 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001496 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1497 (GrGLint*)&format->fTotalBits);
1498 format->fTotalBits += format->fStencilBits;
1499 } else {
1500 format->fTotalBits = format->fStencilBits;
1501 }
1502 }
1503}
1504}
1505
egdanielff1d5472015-09-10 08:37:20 -07001506int GrGLGpu::getCompatibleStencilIndex(GrPixelConfig config) {
bsalomon100b8f82015-10-28 08:37:44 -07001507 static const int kSize = 16;
bsalomon926cb022015-12-17 18:15:11 -08001508 SkASSERT(this->caps()->isConfigRenderable(config, false));
bsalomon30447372015-12-21 09:03:05 -08001509 if (!this->glCaps().hasStencilFormatBeenDeterminedForConfig(config)) {
1510 // Default to unsupported, set this if we find a stencil format that works.
1511 int firstWorkingStencilFormatIndex = -1;
Brian Osman91f9a2c2017-09-05 15:02:46 -04001512
egdanielff1d5472015-09-10 08:37:20 -07001513 // Create color texture
kkinnunen546eb5c2015-12-11 00:05:33 -08001514 GrGLuint colorID = 0;
egdanielff1d5472015-09-10 08:37:20 -07001515 GL_CALL(GenTextures(1, &colorID));
1516 this->setScratchTextureUnit();
1517 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, colorID));
1518 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1519 GR_GL_TEXTURE_MAG_FILTER,
1520 GR_GL_NEAREST));
1521 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1522 GR_GL_TEXTURE_MIN_FILTER,
1523 GR_GL_NEAREST));
1524 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1525 GR_GL_TEXTURE_WRAP_S,
1526 GR_GL_CLAMP_TO_EDGE));
1527 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1528 GR_GL_TEXTURE_WRAP_T,
1529 GR_GL_CLAMP_TO_EDGE));
1530
bsalomon76148af2016-01-12 11:13:47 -08001531 GrGLenum internalFormat;
1532 GrGLenum externalFormat;
1533 GrGLenum externalType;
1534 if (!this->glCaps().getTexImageFormats(config, config, &internalFormat, &externalFormat,
1535 &externalType)) {
1536 return false;
1537 }
Brian Osman9b560242017-09-05 15:34:52 -04001538 this->unbindCpuToGpuXferBuffer();
egdanielff1d5472015-09-10 08:37:20 -07001539 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1540 GL_ALLOC_CALL(this->glInterface(), TexImage2D(GR_GL_TEXTURE_2D,
bsalomon926cb022015-12-17 18:15:11 -08001541 0,
bsalomon76148af2016-01-12 11:13:47 -08001542 internalFormat,
bsalomon100b8f82015-10-28 08:37:44 -07001543 kSize,
1544 kSize,
egdanielff1d5472015-09-10 08:37:20 -07001545 0,
bsalomon76148af2016-01-12 11:13:47 -08001546 externalFormat,
1547 externalType,
Ben Wagnera93a14a2017-08-28 10:34:05 -04001548 nullptr));
bsalomon30447372015-12-21 09:03:05 -08001549 if (GR_GL_NO_ERROR != CHECK_ALLOC_ERROR(this->glInterface())) {
egdanielff1d5472015-09-10 08:37:20 -07001550 GL_CALL(DeleteTextures(1, &colorID));
bsalomon30447372015-12-21 09:03:05 -08001551 return -1;
egdanielff1d5472015-09-10 08:37:20 -07001552 }
1553
1554 // unbind the texture from the texture unit before binding it to the frame buffer
1555 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, 0));
1556
1557 // Create Framebuffer
kkinnunen546eb5c2015-12-11 00:05:33 -08001558 GrGLuint fb = 0;
egdanielff1d5472015-09-10 08:37:20 -07001559 GL_CALL(GenFramebuffers(1, &fb));
egdanielec00d942015-09-14 12:56:10 -07001560 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, fb));
Robert Phillips294870f2016-11-11 12:38:40 -05001561 fHWBoundRenderTargetUniqueID.makeInvalid();
egdanielff1d5472015-09-10 08:37:20 -07001562 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
1563 GR_GL_COLOR_ATTACHMENT0,
1564 GR_GL_TEXTURE_2D,
1565 colorID,
1566 0));
bsalomon30447372015-12-21 09:03:05 -08001567 GrGLuint sbRBID = 0;
1568 GL_CALL(GenRenderbuffers(1, &sbRBID));
egdanielff1d5472015-09-10 08:37:20 -07001569
1570 // look over formats till I find a compatible one
1571 int stencilFmtCnt = this->glCaps().stencilFormats().count();
bsalomon30447372015-12-21 09:03:05 -08001572 if (sbRBID) {
egdanielff1d5472015-09-10 08:37:20 -07001573 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbRBID));
bsalomon30447372015-12-21 09:03:05 -08001574 for (int i = 0; i < stencilFmtCnt && sbRBID; ++i) {
1575 const GrGLCaps::StencilFormat& sFmt = this->glCaps().stencilFormats()[i];
1576 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1577 GL_ALLOC_CALL(this->glInterface(), RenderbufferStorage(GR_GL_RENDERBUFFER,
1578 sFmt.fInternalFormat,
1579 kSize, kSize));
1580 if (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(this->glInterface())) {
egdanielff1d5472015-09-10 08:37:20 -07001581 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon30447372015-12-21 09:03:05 -08001582 GR_GL_STENCIL_ATTACHMENT,
egdanielff1d5472015-09-10 08:37:20 -07001583 GR_GL_RENDERBUFFER, sbRBID));
bsalomon30447372015-12-21 09:03:05 -08001584 if (sFmt.fPacked) {
1585 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1586 GR_GL_DEPTH_ATTACHMENT,
1587 GR_GL_RENDERBUFFER, sbRBID));
1588 } else {
1589 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1590 GR_GL_DEPTH_ATTACHMENT,
1591 GR_GL_RENDERBUFFER, 0));
1592 }
1593 GrGLenum status;
1594 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1595 if (status == GR_GL_FRAMEBUFFER_COMPLETE) {
1596 firstWorkingStencilFormatIndex = i;
1597 break;
1598 }
egdanielff1d5472015-09-10 08:37:20 -07001599 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1600 GR_GL_STENCIL_ATTACHMENT,
1601 GR_GL_RENDERBUFFER, 0));
1602 if (sFmt.fPacked) {
1603 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1604 GR_GL_DEPTH_ATTACHMENT,
1605 GR_GL_RENDERBUFFER, 0));
1606 }
egdanielff1d5472015-09-10 08:37:20 -07001607 }
1608 }
bsalomon30447372015-12-21 09:03:05 -08001609 GL_CALL(DeleteRenderbuffers(1, &sbRBID));
egdanielff1d5472015-09-10 08:37:20 -07001610 }
1611 GL_CALL(DeleteTextures(1, &colorID));
egdanielff1d5472015-09-10 08:37:20 -07001612 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, 0));
1613 GL_CALL(DeleteFramebuffers(1, &fb));
bsalomon30447372015-12-21 09:03:05 -08001614 fGLContext->caps()->setStencilFormatIndexForConfig(config, firstWorkingStencilFormatIndex);
egdanielff1d5472015-09-10 08:37:20 -07001615 }
bsalomon30447372015-12-21 09:03:05 -08001616 return this->glCaps().getStencilFormatIndexForConfig(config);
egdanielff1d5472015-09-10 08:37:20 -07001617}
1618
erikchen9a1ed5d2016-02-10 16:32:34 -08001619bool GrGLGpu::createTextureImpl(const GrSurfaceDesc& desc, GrGLTextureInfo* info,
cblume55f2d2d2016-02-26 13:20:48 -08001620 bool renderTarget, GrGLTexture::TexParams* initialTexParams,
Greg Daniel834f1202017-10-09 15:06:20 -04001621 const GrMipLevel texels[], int mipLevelCount,
Greg Daniel0fc4d2d2017-10-12 11:23:36 -04001622 GrMipMapsStatus* mipMapsStatus) {
erikchen9a1ed5d2016-02-10 16:32:34 -08001623 info->fID = 0;
1624 info->fTarget = GR_GL_TEXTURE_2D;
1625 GL_CALL(GenTextures(1, &(info->fID)));
1626
1627 if (!info->fID) {
1628 return false;
1629 }
1630
1631 this->setScratchTextureUnit();
1632 GL_CALL(BindTexture(info->fTarget, info->fID));
1633
1634 if (renderTarget && this->glCaps().textureUsageSupport()) {
1635 // provides a hint about how this texture will be used
1636 GL_CALL(TexParameteri(info->fTarget,
1637 GR_GL_TEXTURE_USAGE,
1638 GR_GL_FRAMEBUFFER_ATTACHMENT));
1639 }
1640
cblume55f2d2d2016-02-26 13:20:48 -08001641 if (info) {
1642 set_initial_texture_params(this->glInterface(), *info, initialTexParams);
1643 }
Brian Salomond1eaf492017-05-18 10:02:08 -04001644 if (!this->uploadTexData(desc.fConfig, desc.fWidth, desc.fHeight, desc.fOrigin, info->fTarget,
1645 kNewTexture_UploadType, 0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
Greg Daniel0fc4d2d2017-10-12 11:23:36 -04001646 texels, mipLevelCount, mipMapsStatus)) {
erikchen9a1ed5d2016-02-10 16:32:34 -08001647 GL_CALL(DeleteTextures(1, &(info->fID)));
1648 return false;
1649 }
1650 return true;
1651}
1652
egdanielec00d942015-09-14 12:56:10 -07001653GrStencilAttachment* GrGLGpu::createStencilAttachmentForRenderTarget(const GrRenderTarget* rt,
Brian Salomond1eaf492017-05-18 10:02:08 -04001654 int width, int height) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001655 SkASSERT(width >= rt->width());
1656 SkASSERT(height >= rt->height());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001657
vbuzinovdded6962015-06-12 08:59:45 -07001658 int samples = rt->numStencilSamples();
egdaniel8dc7c3a2015-04-16 11:22:42 -07001659 GrGLStencilAttachment::IDDesc sbDesc;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001660
egdanielff1d5472015-09-10 08:37:20 -07001661 int sIdx = this->getCompatibleStencilIndex(rt->config());
bsalomon62a627b2015-12-17 09:50:47 -08001662 if (sIdx < 0) {
egdanielec00d942015-09-14 12:56:10 -07001663 return nullptr;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001664 }
egdanielff1d5472015-09-10 08:37:20 -07001665
1666 if (!sbDesc.fRenderbufferID) {
1667 GL_CALL(GenRenderbuffers(1, &sbDesc.fRenderbufferID));
1668 }
1669 if (!sbDesc.fRenderbufferID) {
egdanielec00d942015-09-14 12:56:10 -07001670 return nullptr;
egdanielff1d5472015-09-10 08:37:20 -07001671 }
1672 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbDesc.fRenderbufferID));
1673 const GrGLCaps::StencilFormat& sFmt = this->glCaps().stencilFormats()[sIdx];
1674 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1675 // we do this "if" so that we don't call the multisample
1676 // version on a GL that doesn't have an MSAA extension.
1677 if (samples > 0) {
1678 SkAssertResult(renderbuffer_storage_msaa(*fGLContext,
1679 samples,
1680 sFmt.fInternalFormat,
1681 width, height));
1682 } else {
1683 GL_ALLOC_CALL(this->glInterface(), RenderbufferStorage(GR_GL_RENDERBUFFER,
1684 sFmt.fInternalFormat,
1685 width, height));
Brian Salomon0ec981b2017-05-15 13:48:50 -04001686 SkASSERT(GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(this->glInterface()));
egdanielff1d5472015-09-10 08:37:20 -07001687 }
1688 fStats.incStencilAttachmentCreates();
1689 // After sized formats we attempt an unsized format and take
1690 // whatever sizes GL gives us. In that case we query for the size.
1691 GrGLStencilAttachment::Format format = sFmt;
1692 get_stencil_rb_sizes(this->glInterface(), &format);
egdanielec00d942015-09-14 12:56:10 -07001693 GrGLStencilAttachment* stencil = new GrGLStencilAttachment(this,
1694 sbDesc,
1695 width,
1696 height,
1697 samples,
1698 format);
1699 return stencil;
reed@google.comac10a2d2010-12-22 21:39:39 +00001700}
1701
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001702////////////////////////////////////////////////////////////////////////////////
1703
jvanverth73063dc2015-12-03 09:15:47 -08001704// GL_STREAM_DRAW triggers an optimization in Chromium's GPU process where a client's vertex buffer
1705// objects are implemented as client-side-arrays on tile-deferred architectures.
1706#define DYNAMIC_USAGE_PARAM GR_GL_STREAM_DRAW
1707
cdaltone2e71c22016-04-07 18:13:29 -07001708GrBuffer* GrGLGpu::onCreateBuffer(size_t size, GrBufferType intendedType,
cdalton1bf3e712016-04-19 10:00:02 -07001709 GrAccessPattern accessPattern, const void* data) {
1710 return GrGLBuffer::Create(this, size, intendedType, accessPattern, data);
jvanverth73063dc2015-12-03 09:15:47 -08001711}
1712
Robert Phillipse3302df2017-04-24 07:31:02 -04001713
1714std::unique_ptr<OpAllocator> GrGLGpu::onCreateInstancedRenderingAllocator() {
1715 return std::unique_ptr<OpAllocator>(new GLOpAllocator(this->caps()));
1716}
1717
csmartdaltone0d36292016-07-29 08:14:20 -07001718InstancedRendering* GrGLGpu::onCreateInstancedRendering() {
1719 return new GLInstancedRendering(this);
1720}
1721
bsalomon3e791242014-12-17 13:43:13 -08001722void GrGLGpu::flushScissor(const GrScissorState& scissorState,
joshualitt77b13072014-10-27 14:51:01 -07001723 const GrGLIRect& rtViewport,
1724 GrSurfaceOrigin rtOrigin) {
robertphillipse85a32d2015-02-10 08:16:55 -08001725 if (scissorState.enabled()) {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001726 GrGLIRect scissor;
Robert Phillips16d8ec62017-07-27 16:16:25 -04001727 scissor.setRelativeTo(rtViewport, scissorState.rect(), rtOrigin);
bsalomon@google.coma3201942012-06-21 19:58:20 +00001728 // if the scissor fully contains the viewport then we fall through and
1729 // disable the scissor test.
bsalomonb0bd4f62014-09-03 07:19:50 -07001730 if (!scissor.contains(rtViewport)) {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001731 if (fHWScissorSettings.fRect != scissor) {
1732 scissor.pushToGLScissor(this->glInterface());
1733 fHWScissorSettings.fRect = scissor;
1734 }
1735 if (kYes_TriState != fHWScissorSettings.fEnabled) {
1736 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1737 fHWScissorSettings.fEnabled = kYes_TriState;
1738 }
1739 return;
1740 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001741 }
joshualitt77b13072014-10-27 14:51:01 -07001742
1743 // See fall through note above
1744 this->disableScissor();
1745}
1746
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001747void GrGLGpu::flushWindowRectangles(const GrWindowRectsState& windowState,
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001748 const GrGLRenderTarget* rt, GrSurfaceOrigin origin) {
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001749#ifndef USE_NSIGHT
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001750 typedef GrWindowRectsState::Mode Mode;
1751 SkASSERT(!windowState.enabled() || rt->renderFBOID()); // Window rects can't be used on-screen.
1752 SkASSERT(windowState.numWindows() <= this->caps()->maxWindowRectangles());
csmartdalton28341fa2016-08-17 10:00:21 -07001753
1754 if (!this->caps()->maxWindowRectangles() ||
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001755 fHWWindowRectsState.knownEqualTo(origin, rt->getViewport(), windowState)) {
csmartdalton28341fa2016-08-17 10:00:21 -07001756 return;
csmartdalton28341fa2016-08-17 10:00:21 -07001757 }
1758
csmartdalton7535f412016-08-23 06:51:00 -07001759 // This is purely a workaround for a spurious warning generated by gcc. Otherwise the above
1760 // assert would be sufficient. https://gcc.gnu.org/bugzilla/show_bug.cgi?id=5912
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001761 int numWindows = SkTMin(windowState.numWindows(), int(GrWindowRectangles::kMaxWindows));
1762 SkASSERT(windowState.numWindows() == numWindows);
csmartdalton7535f412016-08-23 06:51:00 -07001763
csmartdalton28341fa2016-08-17 10:00:21 -07001764 GrGLIRect glwindows[GrWindowRectangles::kMaxWindows];
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001765 const SkIRect* skwindows = windowState.windows().data();
csmartdalton7535f412016-08-23 06:51:00 -07001766 for (int i = 0; i < numWindows; ++i) {
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001767 glwindows[i].setRelativeTo(rt->getViewport(), skwindows[i], origin);
csmartdalton28341fa2016-08-17 10:00:21 -07001768 }
1769
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001770 GrGLenum glmode = (Mode::kExclusive == windowState.mode()) ? GR_GL_EXCLUSIVE : GR_GL_INCLUSIVE;
csmartdalton7535f412016-08-23 06:51:00 -07001771 GL_CALL(WindowRectangles(glmode, numWindows, glwindows->asInts()));
csmartdalton28341fa2016-08-17 10:00:21 -07001772
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001773 fHWWindowRectsState.set(origin, rt->getViewport(), windowState);
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001774#endif
csmartdalton28341fa2016-08-17 10:00:21 -07001775}
1776
1777void GrGLGpu::disableWindowRectangles() {
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001778#ifndef USE_NSIGHT
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001779 if (!this->caps()->maxWindowRectangles() || fHWWindowRectsState.knownDisabled()) {
csmartdalton28341fa2016-08-17 10:00:21 -07001780 return;
1781 }
1782 GL_CALL(WindowRectangles(GR_GL_EXCLUSIVE, 0, nullptr));
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001783 fHWWindowRectsState.setDisabled();
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001784#endif
csmartdalton28341fa2016-08-17 10:00:21 -07001785}
1786
ethannicholas28ef4452016-03-25 09:26:03 -07001787void GrGLGpu::flushMinSampleShading(float minSampleShading) {
1788 if (fHWMinSampleShading != minSampleShading) {
1789 if (minSampleShading > 0.0) {
1790 GL_CALL(Enable(GR_GL_SAMPLE_SHADING));
1791 GL_CALL(MinSampleShading(minSampleShading));
1792 }
1793 else {
1794 GL_CALL(Disable(GR_GL_SAMPLE_SHADING));
1795 }
1796 fHWMinSampleShading = minSampleShading;
1797 }
1798}
1799
bsalomon2eda5b32016-09-21 10:53:24 -07001800bool GrGLGpu::flushGLState(const GrPipeline& pipeline, const GrPrimitiveProcessor& primProc,
1801 bool willDrawPoints) {
Hal Canary144caf52016-11-07 17:57:18 -05001802 sk_sp<GrGLProgram> program(fProgramCache->refProgram(this, pipeline, primProc, willDrawPoints));
brianosman33f6b3f2016-06-02 05:49:21 -07001803 if (!program) {
1804 GrCapsDebugf(this->caps(), "Failed to create program!\n");
1805 return false;
1806 }
1807
1808 program->generateMipmaps(primProc, pipeline);
1809
egdaniel080e6732014-12-22 07:35:52 -08001810 GrXferProcessor::BlendInfo blendInfo;
egdaniel0e1853c2016-03-17 11:35:45 -07001811 pipeline.getXferProcessor().getBlendInfo(&blendInfo);
egdaniel080e6732014-12-22 07:35:52 -08001812
egdaniel080e6732014-12-22 07:35:52 -08001813 this->flushColorWrite(blendInfo.fWriteColor);
ethannicholas28ef4452016-03-25 09:26:03 -07001814 this->flushMinSampleShading(primProc.getSampleShading());
bsalomonbc3d0de2014-12-15 13:45:03 -08001815
bsalomon6df86402015-06-01 10:41:49 -07001816 GrGLuint programID = program->programID();
bsalomon1f78c0a2014-12-17 09:43:13 -08001817 if (fHWProgramID != programID) {
1818 GL_CALL(UseProgram(programID));
1819 fHWProgramID = programID;
1820 }
1821
egdanield803f272015-03-18 13:01:52 -07001822 if (blendInfo.fWriteColor) {
bsalomon7f9b2e42016-01-12 13:29:26 -08001823 // Swizzle the blend to match what the shader will output.
Brian Salomon1edc5b92016-11-29 13:43:46 -05001824 const GrSwizzle& swizzle = this->caps()->shaderCaps()->configOutputSwizzle(
Robert Phillips2890fbf2017-07-26 15:48:41 -04001825 pipeline.proxy()->config());
bsalomon7f9b2e42016-01-12 13:29:26 -08001826 this->flushBlend(blendInfo, swizzle);
egdanield803f272015-03-18 13:01:52 -07001827 }
bsalomon1f78c0a2014-12-17 09:43:13 -08001828
cdalton74b8d322016-04-11 14:47:28 -07001829 program->setData(primProc, pipeline);
bsalomon1f78c0a2014-12-17 09:43:13 -08001830
Robert Phillips2890fbf2017-07-26 15:48:41 -04001831 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(pipeline.renderTarget());
csmartdaltonc633abb2016-11-01 08:55:55 -07001832 GrStencilSettings stencil;
1833 if (pipeline.isStencilEnabled()) {
1834 // TODO: attach stencil and create settings during render target flush.
1835 SkASSERT(glRT->renderTargetPriv().getStencilAttachment());
1836 stencil.reset(*pipeline.getUserStencil(), pipeline.hasStencilClip(),
1837 glRT->renderTargetPriv().numStencilBits());
1838 }
1839 this->flushStencil(stencil);
Robert Phillips16d8ec62017-07-27 16:16:25 -04001840 this->flushScissor(pipeline.getScissorState(), glRT->getViewport(), pipeline.proxy()->origin());
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001841 this->flushWindowRectangles(pipeline.getWindowRectsState(), glRT, pipeline.proxy()->origin());
csmartdaltondabc91b2016-11-15 14:26:27 -07001842 this->flushHWAAState(glRT, pipeline.isHWAntialiasState(), !stencil.isDisabled());
bsalomonbc3d0de2014-12-15 13:45:03 -08001843
1844 // This must come after textures are flushed because a texture may need
egdanield803f272015-03-18 13:01:52 -07001845 // to be msaa-resolved (which will modify bound FBO state).
brianosman64d094d2016-03-25 06:01:59 -07001846 this->flushRenderTarget(glRT, nullptr, pipeline.getDisableOutputConversionToSRGB());
bsalomonbc3d0de2014-12-15 13:45:03 -08001847
1848 return true;
1849}
1850
joshualitt873ad0e2015-01-20 09:08:51 -08001851void GrGLGpu::setupGeometry(const GrPrimitiveProcessor& primProc,
Chris Daltonff926502017-05-03 14:36:54 -04001852 const GrBuffer* indexBuffer,
1853 const GrBuffer* vertexBuffer,
Chris Dalton1d616352017-05-31 12:51:23 -06001854 int baseVertex,
1855 const GrBuffer* instanceBuffer,
1856 int baseInstance) {
cdaltone2e71c22016-04-07 18:13:29 -07001857 GrGLAttribArrayState* attribState;
Chris Daltonff926502017-05-03 14:36:54 -04001858 if (indexBuffer) {
Chris Dalton1d616352017-05-31 12:51:23 -06001859 SkASSERT(indexBuffer && !indexBuffer->isMapped());
Chris Daltonff926502017-05-03 14:36:54 -04001860 attribState = fHWVertexArrayState.bindInternalVertexArray(this, indexBuffer);
cdaltone2e71c22016-04-07 18:13:29 -07001861 } else {
1862 attribState = fHWVertexArrayState.bindInternalVertexArray(this);
bsalomonbc3d0de2014-12-15 13:45:03 -08001863 }
bsalomonbc3d0de2014-12-15 13:45:03 -08001864
Chris Dalton1d616352017-05-31 12:51:23 -06001865 struct {
1866 const GrBuffer* fBuffer;
1867 int fStride;
1868 size_t fBufferOffset;
1869 } bindings[2];
Chris Dalton8e45b4f2017-05-05 14:00:56 -04001870
Chris Dalton1d616352017-05-31 12:51:23 -06001871 if (int vertexStride = primProc.getVertexStride()) {
1872 SkASSERT(vertexBuffer && !vertexBuffer->isMapped());
1873 bindings[0].fBuffer = vertexBuffer;
1874 bindings[0].fStride = vertexStride;
1875 bindings[0].fBufferOffset = vertexBuffer->baseOffset() + baseVertex * vertexStride;
1876 }
1877 if (int instanceStride = primProc.getInstanceStride()) {
1878 SkASSERT(instanceBuffer && !instanceBuffer->isMapped());
1879 bindings[1].fBuffer = instanceBuffer;
1880 bindings[1].fStride = instanceStride;
1881 bindings[1].fBufferOffset = instanceBuffer->baseOffset() + baseInstance * instanceStride;
1882 }
bsalomonbc3d0de2014-12-15 13:45:03 -08001883
Chris Dalton1d616352017-05-31 12:51:23 -06001884 int numAttribs = primProc.numAttribs();
1885 attribState->enableVertexArrays(this, numAttribs);
bsalomonbc3d0de2014-12-15 13:45:03 -08001886
Chris Dalton1d616352017-05-31 12:51:23 -06001887 for (int i = 0; i < numAttribs; ++i) {
1888 using InputRate = GrPrimitiveProcessor::Attribute::InputRate;
1889 const GrGeometryProcessor::Attribute& attrib = primProc.getAttrib(i);
1890 const int divisor = InputRate::kPerInstance == attrib.fInputRate ? 1 : 0;
1891 const auto& binding = bindings[divisor];
1892 attribState->set(this, i, binding.fBuffer, attrib.fType, binding.fStride,
1893 binding.fBufferOffset + attrib.fOffsetInRecord, divisor);
bsalomonbc3d0de2014-12-15 13:45:03 -08001894 }
1895}
1896
csmartdalton485a1202016-07-13 10:16:32 -07001897GrGLenum GrGLGpu::bindBuffer(GrBufferType type, const GrBuffer* buffer) {
joshualitt93316b92015-10-23 09:08:08 -07001898 this->handleDirtyContext();
cdaltondeacc972016-04-06 14:26:33 -07001899
cdaltone2e71c22016-04-07 18:13:29 -07001900 // Index buffer state is tied to the vertex array.
1901 if (kIndex_GrBufferType == type) {
1902 this->bindVertexArray(0);
cdaltondeacc972016-04-06 14:26:33 -07001903 }
cdaltone2e71c22016-04-07 18:13:29 -07001904
1905 SkASSERT(type >= 0 && type <= kLast_GrBufferType);
1906 auto& bufferState = fHWBufferState[type];
1907
robertphillips8abb3702016-08-31 14:04:06 -07001908 if (buffer->uniqueID() != bufferState.fBoundBufferUniqueID) {
csmartdalton485a1202016-07-13 10:16:32 -07001909 if (buffer->isCPUBacked()) {
1910 if (!bufferState.fBufferZeroKnownBound) {
1911 GL_CALL(BindBuffer(bufferState.fGLTarget, 0));
1912 }
1913 } else {
1914 const GrGLBuffer* glBuffer = static_cast<const GrGLBuffer*>(buffer);
1915 GL_CALL(BindBuffer(bufferState.fGLTarget, glBuffer->bufferID()));
cdaltone2e71c22016-04-07 18:13:29 -07001916 }
csmartdalton485a1202016-07-13 10:16:32 -07001917 bufferState.fBufferZeroKnownBound = buffer->isCPUBacked();
robertphillips8abb3702016-08-31 14:04:06 -07001918 bufferState.fBoundBufferUniqueID = buffer->uniqueID();
cdaltone2e71c22016-04-07 18:13:29 -07001919 }
1920
1921 return bufferState.fGLTarget;
joshualitt93316b92015-10-23 09:08:08 -07001922}
1923
cdalton74b8d322016-04-11 14:47:28 -07001924void GrGLGpu::notifyBufferReleased(const GrGLBuffer* buffer) {
1925 if (buffer->hasAttachedToTexture()) {
1926 // Detach this buffer from any textures to ensure the underlying memory is freed.
Robert Phillips294870f2016-11-11 12:38:40 -05001927 GrGpuResource::UniqueID uniqueID = buffer->uniqueID();
cdalton74b8d322016-04-11 14:47:28 -07001928 for (int i = fHWMaxUsedBufferTextureUnit; i >= 0; --i) {
1929 auto& buffTex = fHWBufferTextures[i];
1930 if (uniqueID != buffTex.fAttachedBufferUniqueID) {
1931 continue;
1932 }
1933 if (i == fHWMaxUsedBufferTextureUnit) {
1934 --fHWMaxUsedBufferTextureUnit;
1935 }
1936
1937 this->setTextureUnit(i);
1938 if (!buffTex.fKnownBound) {
1939 SkASSERT(buffTex.fTextureID);
1940 GL_CALL(BindTexture(GR_GL_TEXTURE_BUFFER, buffTex.fTextureID));
1941 buffTex.fKnownBound = true;
1942 }
1943 GL_CALL(TexBuffer(GR_GL_TEXTURE_BUFFER,
1944 this->glCaps().configSizedInternalFormat(buffTex.fTexelConfig), 0));
1945 }
1946 }
1947}
1948
bsalomon861e1032014-12-16 07:33:49 -08001949void GrGLGpu::disableScissor() {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001950 if (kNo_TriState != fHWScissorSettings.fEnabled) {
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001951 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.coma3201942012-06-21 19:58:20 +00001952 fHWScissorSettings.fEnabled = kNo_TriState;
1953 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001954 }
1955}
1956
Robert Phillips19e51dc2017-08-09 09:30:51 -04001957void GrGLGpu::clear(const GrFixedClip& clip, GrColor color,
1958 GrRenderTarget* target, GrSurfaceOrigin origin) {
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001959 // parent class should never let us get here with no RT
bsalomon49f085d2014-09-05 13:34:00 -07001960 SkASSERT(target);
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001961
Brian Salomon43f8bf02017-10-18 08:33:29 -04001962 this->handleDirtyContext();
bsalomon@google.com74b98712011-11-11 19:46:16 +00001963
1964 GrGLfloat r, g, b, a;
1965 static const GrGLfloat scale255 = 1.f / 255.f;
1966 a = GrColorUnpackA(color) * scale255;
1967 GrGLfloat scaleRGB = scale255;
bsalomon@google.com74b98712011-11-11 19:46:16 +00001968 r = GrColorUnpackR(color) * scaleRGB;
1969 g = GrColorUnpackG(color) * scaleRGB;
1970 b = GrColorUnpackB(color) * scaleRGB;
1971
Brian Salomon43f8bf02017-10-18 08:33:29 -04001972 if (this->glCaps().useDrawToClearColor()) {
1973 this->clearColorAsDraw(clip, r, g, b, a, target, origin);
1974 return;
1975 }
1976
1977 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
1978
1979 this->flushRenderTarget(glRT, clip.scissorEnabled() ? &clip.scissorRect() : nullptr);
1980 this->flushScissor(clip.scissorState(), glRT->getViewport(), origin);
1981 this->flushWindowRectangles(clip.windowRectsState(), glRT, origin);
1982
bsalomon@google.com74b98712011-11-11 19:46:16 +00001983 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00001984 fHWWriteToColor = kYes_TriState;
Brian Salomon028a9a52017-05-11 11:39:08 -04001985
Eric Karlaeaf22b2017-05-18 15:08:09 -07001986 if (this->glCaps().clearToBoundaryValuesIsBroken() &&
1987 (1 == r || 0 == r) && (1 == g || 0 == g) && (1 == b || 0 == b) && (1 == a || 0 == a)) {
Eric Karlaeaf22b2017-05-18 15:08:09 -07001988 static const GrGLfloat safeAlpha1 = nextafter(1.f, 2.f);
1989 static const GrGLfloat safeAlpha0 = nextafter(0.f, -1.f);
Eric Karlaeaf22b2017-05-18 15:08:09 -07001990 a = (1 == a) ? safeAlpha1 : safeAlpha0;
Brian Salomon028a9a52017-05-11 11:39:08 -04001991 }
bsalomon@google.com74b98712011-11-11 19:46:16 +00001992 GL_CALL(ClearColor(r, g, b, a));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001993 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001994}
1995
Robert Phillips95214472017-08-08 18:00:03 -04001996void GrGLGpu::clearStencil(GrRenderTarget* target, int clearValue) {
1997 if (!target) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001998 return;
1999 }
Robert Phillipscb2e2352017-08-30 16:44:40 -04002000
2001 GrStencilAttachment* sb = target->renderTargetPriv().getStencilAttachment();
2002 // this should only be called internally when we know we have a
2003 // stencil buffer.
2004 SkASSERT(sb);
2005
bsalomonb0bd4f62014-09-03 07:19:50 -07002006 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
egdanield803f272015-03-18 13:01:52 -07002007 this->flushRenderTarget(glRT, &SkIRect::EmptyIRect());
bsalomon@google.com8295dc12011-05-02 12:53:34 +00002008
joshualitt77b13072014-10-27 14:51:01 -07002009 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07002010 this->disableWindowRectangles();
robertphillips@google.com730ebe52012-04-16 16:33:13 +00002011
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002012 GL_CALL(StencilMask(0xffffffff));
Robert Phillips95214472017-08-08 18:00:03 -04002013 GL_CALL(ClearStencil(clearValue));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002014 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00002015 fHWStencilSettings.invalidate();
Robert Phillipscb2e2352017-08-30 16:44:40 -04002016 if (!clearValue) {
2017 sb->cleared();
2018 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002019}
2020
csmartdalton29df7602016-08-31 11:55:52 -07002021void GrGLGpu::clearStencilClip(const GrFixedClip& clip,
2022 bool insideStencilMask,
Robert Phillips19e51dc2017-08-09 09:30:51 -04002023 GrRenderTarget* target, GrSurfaceOrigin origin) {
bsalomon49f085d2014-09-05 13:34:00 -07002024 SkASSERT(target);
egdaniel9cb63402016-06-23 08:37:05 -07002025 this->handleDirtyContext();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00002026
Mike Klein31550db2017-06-06 23:29:53 +00002027 if (this->glCaps().useDrawToClearStencilClip()) {
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002028 this->clearStencilClipAsDraw(clip, insideStencilMask, target, origin);
Mike Klein31550db2017-06-06 23:29:53 +00002029 return;
2030 }
2031
egdaniel8dc7c3a2015-04-16 11:22:42 -07002032 GrStencilAttachment* sb = target->renderTargetPriv().getStencilAttachment();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00002033 // this should only be called internally when we know we have a
2034 // stencil buffer.
bsalomon6bc1b5f2015-02-23 09:06:38 -08002035 SkASSERT(sb);
2036 GrGLint stencilBitCount = sb->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002037#if 0
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002038 SkASSERT(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00002039 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002040#else
2041 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00002042 // ANGLE a partial stencil mask will cause clears to be
Robert Phillipsf2361d22016-10-25 14:20:06 -04002043 // turned into draws. Our contract on GrOpList says that
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002044 // changing the clip between stencil passes may or may not
2045 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00002046 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002047#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002048 GrGLint value;
csmartdalton29df7602016-08-31 11:55:52 -07002049 if (insideStencilMask) {
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002050 value = (1 << (stencilBitCount - 1));
2051 } else {
2052 value = 0;
2053 }
bsalomonb0bd4f62014-09-03 07:19:50 -07002054 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
egdanield803f272015-03-18 13:01:52 -07002055 this->flushRenderTarget(glRT, &SkIRect::EmptyIRect());
bsalomon@google.coma3201942012-06-21 19:58:20 +00002056
Robert Phillips19e51dc2017-08-09 09:30:51 -04002057 this->flushScissor(clip.scissorState(), glRT->getViewport(), origin);
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002058 this->flushWindowRectangles(clip.windowRectsState(), glRT, origin);
bsalomon@google.coma3201942012-06-21 19:58:20 +00002059
caryclark@google.comcf6285b2012-06-06 12:09:01 +00002060 GL_CALL(StencilMask((uint32_t) clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002061 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002062 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00002063 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00002064}
2065
Brian Salomon71d9d842016-11-03 13:42:00 -04002066static bool read_pixels_pays_for_y_flip(GrSurfaceOrigin origin, const GrGLCaps& caps,
bsalomon39826022015-07-23 08:07:21 -07002067 int width, int height, GrPixelConfig config,
2068 size_t rowBytes) {
Brian Salomon71d9d842016-11-03 13:42:00 -04002069 // If the surface is already TopLeft, we don't need to flip.
2070 if (kTopLeft_GrSurfaceOrigin == origin) {
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002071 return false;
2072 }
2073
bsalomon494aa592015-07-23 11:45:02 -07002074 // If the read is really small or smaller than the min texture size, don't force a draw.
bsalomon100b8f82015-10-28 08:37:44 -07002075 static const int kMinSize = 32;
2076 if (width < kMinSize || height < kMinSize) {
bsalomon494aa592015-07-23 11:45:02 -07002077 return false;
2078 }
2079
bsalomon@google.com56d11e02011-11-30 19:59:08 +00002080 // if GL can do the flip then we'll never pay for it.
bsalomon39826022015-07-23 08:07:21 -07002081 if (caps.packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00002082 return false;
2083 }
2084
2085 // If we have to do memcpy to handle non-trim rowBytes then we
bsalomon@google.com7107fa72011-11-10 14:54:14 +00002086 // get the flip for free. Otherwise it costs.
bsalomon39826022015-07-23 08:07:21 -07002087 // Note that we're assuming that 0 rowBytes has already been handled and that the width has been
2088 // clipped.
2089 return caps.packRowLengthSupport() || GrBytesPerPixel(config) * width == rowBytes;
2090}
2091
bsalomon1aa20292016-01-22 08:16:09 -08002092bool GrGLGpu::readPixelsSupported(GrRenderTarget* target, GrPixelConfig readConfig) {
Brian Salomon625cd9e2016-12-15 09:35:19 -05002093#ifdef SK_BUILD_FOR_MAC
2094 // Chromium may ask us to read back from locked IOSurfaces. Calling the command buffer's
2095 // glGetIntegerv() with GL_IMPLEMENTATION_COLOR_READ_FORMAT/_TYPE causes the command buffer
2096 // to make a call to check the framebuffer status which can hang the driver. So in Mac Chromium
2097 // we always use a temporary surface to test for read pixels support.
2098 // https://www.crbug.com/662802
2099 if (this->glContext().driver() == kChromium_GrGLDriver) {
2100 return this->readPixelsSupported(target->config(), readConfig);
2101 }
2102#endif
bsalomon1aa20292016-01-22 08:16:09 -08002103 auto bindRenderTarget = [this, target]() -> bool {
2104 this->flushRenderTarget(static_cast<GrGLRenderTarget*>(target), &SkIRect::EmptyIRect());
2105 return true;
2106 };
bsalomon2c3db322016-11-08 13:26:24 -08002107 auto unbindRenderTarget = []{};
bsalomon1aa20292016-01-22 08:16:09 -08002108 auto getIntegerv = [this](GrGLenum query, GrGLint* value) {
2109 GR_GL_GetIntegerv(this->glInterface(), query, value);
2110 };
2111 GrPixelConfig rtConfig = target->config();
bsalomon2c3db322016-11-08 13:26:24 -08002112 return this->glCaps().readPixelsSupported(rtConfig, readConfig, getIntegerv, bindRenderTarget,
2113 unbindRenderTarget);
bsalomon1aa20292016-01-22 08:16:09 -08002114}
2115
2116bool GrGLGpu::readPixelsSupported(GrPixelConfig rtConfig, GrPixelConfig readConfig) {
bsalomon2c3db322016-11-08 13:26:24 -08002117 sk_sp<GrTexture> temp;
2118 auto bindRenderTarget = [this, rtConfig, &temp]() -> bool {
Robert Phillips16d8ec62017-07-27 16:16:25 -04002119 GrSurfaceDesc desc;
bsalomon1aa20292016-01-22 08:16:09 -08002120 desc.fConfig = rtConfig;
2121 desc.fWidth = desc.fHeight = 16;
bsalomon2c3db322016-11-08 13:26:24 -08002122 if (this->glCaps().isConfigRenderable(rtConfig, false)) {
2123 desc.fFlags = kRenderTarget_GrSurfaceFlag;
Robert Phillips16d8ec62017-07-27 16:16:25 -04002124 desc.fOrigin = kBottomLeft_GrSurfaceOrigin;
Robert Phillips67d52cf2017-06-05 13:38:13 -04002125 temp = this->createTexture(desc, SkBudgeted::kNo);
bsalomon2c3db322016-11-08 13:26:24 -08002126 if (!temp) {
2127 return false;
2128 }
2129 GrGLRenderTarget* glrt = static_cast<GrGLRenderTarget*>(temp->asRenderTarget());
2130 this->flushRenderTarget(glrt, &SkIRect::EmptyIRect());
2131 return true;
2132 } else if (this->glCaps().canConfigBeFBOColorAttachment(rtConfig)) {
Robert Phillips16d8ec62017-07-27 16:16:25 -04002133 desc.fOrigin = kTopLeft_GrSurfaceOrigin;
Robert Phillips67d52cf2017-06-05 13:38:13 -04002134 temp = this->createTexture(desc, SkBudgeted::kNo);
bsalomon2c3db322016-11-08 13:26:24 -08002135 if (!temp) {
2136 return false;
2137 }
2138 GrGLIRect vp;
2139 this->bindSurfaceFBOForPixelOps(temp.get(), GR_GL_FRAMEBUFFER, &vp, kDst_TempFBOTarget);
Robert Phillips294870f2016-11-11 12:38:40 -05002140 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon2c3db322016-11-08 13:26:24 -08002141 return true;
bsalomon1aa20292016-01-22 08:16:09 -08002142 }
bsalomon2c3db322016-11-08 13:26:24 -08002143 return false;
2144 };
2145 auto unbindRenderTarget = [this, &temp]() {
2146 this->unbindTextureFBOForPixelOps(GR_GL_FRAMEBUFFER, temp.get());
bsalomon1aa20292016-01-22 08:16:09 -08002147 };
2148 auto getIntegerv = [this](GrGLenum query, GrGLint* value) {
2149 GR_GL_GetIntegerv(this->glInterface(), query, value);
2150 };
bsalomon2c3db322016-11-08 13:26:24 -08002151 return this->glCaps().readPixelsSupported(rtConfig, readConfig, getIntegerv, bindRenderTarget,
2152 unbindRenderTarget);
bsalomon1aa20292016-01-22 08:16:09 -08002153}
2154
2155bool GrGLGpu::readPixelsSupported(GrSurface* surfaceForConfig, GrPixelConfig readConfig) {
2156 if (GrRenderTarget* rt = surfaceForConfig->asRenderTarget()) {
2157 return this->readPixelsSupported(rt, readConfig);
2158 } else {
2159 GrPixelConfig config = surfaceForConfig->config();
2160 return this->readPixelsSupported(config, readConfig);
2161 }
2162}
2163
bsalomone9573312016-01-25 14:33:25 -08002164static bool requires_srgb_conversion(GrPixelConfig a, GrPixelConfig b) {
2165 if (GrPixelConfigIsSRGB(a)) {
2166 return !GrPixelConfigIsSRGB(b) && !GrPixelConfigIsAlphaOnly(b);
2167 } else if (GrPixelConfigIsSRGB(b)) {
2168 return !GrPixelConfigIsSRGB(a) && !GrPixelConfigIsAlphaOnly(a);
2169 }
2170 return false;
2171}
2172
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002173bool GrGLGpu::onGetReadPixelsInfo(GrSurface* srcSurface, GrSurfaceOrigin srcOrigin,
2174 int width, int height, size_t rowBytes,
bsalomonf0674512015-07-28 13:26:15 -07002175 GrPixelConfig readConfig, DrawPreference* drawPreference,
2176 ReadPixelTempDrawInfo* tempDrawInfo) {
bsalomone9573312016-01-25 14:33:25 -08002177 GrPixelConfig srcConfig = srcSurface->config();
bsalomon1aa20292016-01-22 08:16:09 -08002178
bsalomone9573312016-01-25 14:33:25 -08002179 // These settings we will always want if a temp draw is performed.
bsalomon39826022015-07-23 08:07:21 -07002180 tempDrawInfo->fTempSurfaceDesc.fFlags = kRenderTarget_GrSurfaceFlag;
2181 tempDrawInfo->fTempSurfaceDesc.fWidth = width;
2182 tempDrawInfo->fTempSurfaceDesc.fHeight = height;
2183 tempDrawInfo->fTempSurfaceDesc.fSampleCnt = 0;
2184 tempDrawInfo->fTempSurfaceDesc.fOrigin = kTopLeft_GrSurfaceOrigin; // no CPU y-flip for TL.
bsalomonb117ff12016-07-19 07:24:40 -07002185 tempDrawInfo->fTempSurfaceFit = this->glCaps().partialFBOReadIsSlow() ? SkBackingFit::kExact
2186 : SkBackingFit::kApprox;
bsalomone9573312016-01-25 14:33:25 -08002187 // For now assume no swizzling, we may change that below.
2188 tempDrawInfo->fSwizzle = GrSwizzle::RGBA();
2189
2190 // Depends on why we need/want a temp draw. Start off assuming no change, the surface we read
2191 // from will be srcConfig and we will read readConfig pixels from it.
Brian Osman33910292017-04-18 14:38:53 -04002192 // Note that if we require a draw and return a non-renderable format for the temp surface the
bsalomone9573312016-01-25 14:33:25 -08002193 // base class will fail for us.
2194 tempDrawInfo->fTempSurfaceDesc.fConfig = srcConfig;
2195 tempDrawInfo->fReadConfig = readConfig;
2196
2197 if (requires_srgb_conversion(srcConfig, readConfig)) {
2198 if (!this->readPixelsSupported(readConfig, readConfig)) {
2199 return false;
2200 }
2201 // Draw to do srgb to linear conversion or vice versa.
2202 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
2203 tempDrawInfo->fTempSurfaceDesc.fConfig = readConfig;
2204 tempDrawInfo->fReadConfig = readConfig;
2205 return true;
2206 }
2207
bsalomon1aa20292016-01-22 08:16:09 -08002208 if (this->glCaps().rgba8888PixelsOpsAreSlow() && kRGBA_8888_GrPixelConfig == readConfig &&
2209 this->readPixelsSupported(kBGRA_8888_GrPixelConfig, kBGRA_8888_GrPixelConfig)) {
bsalomon39826022015-07-23 08:07:21 -07002210 tempDrawInfo->fTempSurfaceDesc.fConfig = kBGRA_8888_GrPixelConfig;
bsalomon6c9cd552016-01-22 07:17:34 -08002211 tempDrawInfo->fSwizzle = GrSwizzle::BGRA();
2212 tempDrawInfo->fReadConfig = kBGRA_8888_GrPixelConfig;
bsalomonb411b3b2015-07-31 09:34:24 -07002213 ElevateDrawPreference(drawPreference, kGpuPrefersDraw_DrawPreference);
ericrkb4ecabd2016-03-11 15:18:20 -08002214 } else if (this->glCaps().rgbaToBgraReadbackConversionsAreSlow() &&
bsalomon39826022015-07-23 08:07:21 -07002215 GrBytesPerPixel(readConfig) == 4 &&
bsalomon1aa20292016-01-22 08:16:09 -08002216 GrPixelConfigSwapRAndB(readConfig) == srcConfig &&
2217 this->readPixelsSupported(srcSurface, srcConfig)) {
bsalomon6c9cd552016-01-22 07:17:34 -08002218 // Mesa 3D takes a slow path on when reading back BGRA from an RGBA surface and vice-versa.
bsalomon39826022015-07-23 08:07:21 -07002219 // Better to do a draw with a R/B swap and then read as the original config.
2220 tempDrawInfo->fTempSurfaceDesc.fConfig = srcConfig;
bsalomon6c9cd552016-01-22 07:17:34 -08002221 tempDrawInfo->fSwizzle = GrSwizzle::BGRA();
2222 tempDrawInfo->fReadConfig = srcConfig;
bsalomonf0674512015-07-28 13:26:15 -07002223 ElevateDrawPreference(drawPreference, kGpuPrefersDraw_DrawPreference);
bsalomon1aa20292016-01-22 08:16:09 -08002224 } else if (!this->readPixelsSupported(srcSurface, readConfig)) {
2225 if (readConfig == kBGRA_8888_GrPixelConfig &&
Brian Salomon71d9d842016-11-03 13:42:00 -04002226 this->glCaps().canConfigBeFBOColorAttachment(kRGBA_8888_GrPixelConfig) &&
bsalomon1aa20292016-01-22 08:16:09 -08002227 this->readPixelsSupported(kRGBA_8888_GrPixelConfig, kRGBA_8888_GrPixelConfig)) {
bsalomone9573312016-01-25 14:33:25 -08002228 // We're trying to read BGRA but it's not supported. If RGBA is renderable and
2229 // we can read it back, then do a swizzling draw to a RGBA and read it back (which
2230 // will effectively be BGRA).
bsalomon1aa20292016-01-22 08:16:09 -08002231 tempDrawInfo->fTempSurfaceDesc.fConfig = kRGBA_8888_GrPixelConfig;
2232 tempDrawInfo->fSwizzle = GrSwizzle::BGRA();
2233 tempDrawInfo->fReadConfig = kRGBA_8888_GrPixelConfig;
2234 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
brianosmana6359362016-03-21 06:55:37 -07002235 } else if (readConfig == kSBGRA_8888_GrPixelConfig &&
Brian Salomon71d9d842016-11-03 13:42:00 -04002236 this->glCaps().canConfigBeFBOColorAttachment(kSRGBA_8888_GrPixelConfig) &&
brianosmana6359362016-03-21 06:55:37 -07002237 this->readPixelsSupported(kSRGBA_8888_GrPixelConfig, kSRGBA_8888_GrPixelConfig)) {
2238 // We're trying to read sBGRA but it's not supported. If sRGBA is renderable and
2239 // we can read it back, then do a swizzling draw to a sRGBA and read it back (which
2240 // will effectively be sBGRA).
2241 tempDrawInfo->fTempSurfaceDesc.fConfig = kSRGBA_8888_GrPixelConfig;
2242 tempDrawInfo->fSwizzle = GrSwizzle::BGRA();
2243 tempDrawInfo->fReadConfig = kSRGBA_8888_GrPixelConfig;
2244 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
bsalomone9573312016-01-25 14:33:25 -08002245 } else if (readConfig == kAlpha_8_GrPixelConfig) {
2246 // onReadPixels implements a fallback for cases where we are want to read kAlpha_8,
2247 // it's unsupported, but 32bit RGBA reads are supported.
2248 // Don't attempt to do any srgb conversions since we only care about alpha.
2249 GrPixelConfig cpuTempConfig = kRGBA_8888_GrPixelConfig;
2250 if (GrPixelConfigIsSRGB(srcSurface->config())) {
2251 cpuTempConfig = kSRGBA_8888_GrPixelConfig;
2252 }
2253 if (!this->readPixelsSupported(srcSurface, cpuTempConfig)) {
2254 // If we can't read RGBA from the src try to draw to a kRGBA_8888 (or kSRGBA_8888)
2255 // first and then onReadPixels will read that to a 32bit temporary buffer.
Brian Salomon71d9d842016-11-03 13:42:00 -04002256 if (this->glCaps().canConfigBeFBOColorAttachment(cpuTempConfig)) {
bsalomone9573312016-01-25 14:33:25 -08002257 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
2258 tempDrawInfo->fTempSurfaceDesc.fConfig = cpuTempConfig;
2259 tempDrawInfo->fReadConfig = kAlpha_8_GrPixelConfig;
2260 } else {
2261 return false;
2262 }
2263 } else {
2264 SkASSERT(tempDrawInfo->fTempSurfaceDesc.fConfig == srcConfig);
2265 SkASSERT(tempDrawInfo->fReadConfig == kAlpha_8_GrPixelConfig);
2266 }
Brian Salomon71d9d842016-11-03 13:42:00 -04002267 } else if (this->glCaps().canConfigBeFBOColorAttachment(readConfig) &&
bsalomone6d665e2016-03-10 07:22:25 -08002268 this->readPixelsSupported(readConfig, readConfig)) {
2269 // Do a draw to convert from the src config to the read config.
2270 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
2271 tempDrawInfo->fTempSurfaceDesc.fConfig = readConfig;
2272 tempDrawInfo->fReadConfig = readConfig;
bsalomon1aa20292016-01-22 08:16:09 -08002273 } else {
2274 return false;
2275 }
bsalomon39826022015-07-23 08:07:21 -07002276 }
2277
Brian Salomon71d9d842016-11-03 13:42:00 -04002278 if ((srcSurface->asRenderTarget() || this->glCaps().canConfigBeFBOColorAttachment(srcConfig)) &&
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002279 read_pixels_pays_for_y_flip(srcOrigin, this->glCaps(), width, height, readConfig,
Brian Salomon71d9d842016-11-03 13:42:00 -04002280 rowBytes)) {
bsalomonf0674512015-07-28 13:26:15 -07002281 ElevateDrawPreference(drawPreference, kGpuPrefersDraw_DrawPreference);
bsalomon39826022015-07-23 08:07:21 -07002282 }
2283
bsalomon39826022015-07-23 08:07:21 -07002284 return true;
bsalomon@google.comc4364992011-11-07 15:54:49 +00002285}
2286
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002287bool GrGLGpu::onReadPixels(GrSurface* surface, GrSurfaceOrigin origin,
bsalomon@google.comc6980972011-11-02 19:57:21 +00002288 int left, int top,
2289 int width, int height,
bsalomon@google.comc4364992011-11-07 15:54:49 +00002290 GrPixelConfig config,
2291 void* buffer,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002292 size_t rowBytes) {
bsalomon6cb3cbe2015-07-30 07:34:27 -07002293 SkASSERT(surface);
bsalomon39826022015-07-23 08:07:21 -07002294
bsalomone9573312016-01-25 14:33:25 -08002295 GrGLRenderTarget* renderTarget = static_cast<GrGLRenderTarget*>(surface->asRenderTarget());
Brian Salomon71d9d842016-11-03 13:42:00 -04002296 if (!renderTarget && !this->glCaps().canConfigBeFBOColorAttachment(surface->config())) {
bsalomon6cb3cbe2015-07-30 07:34:27 -07002297 return false;
2298 }
2299
bsalomon16921ec2015-07-30 15:34:56 -07002300 // OpenGL doesn't do sRGB <-> linear conversions when reading and writing pixels.
bsalomone9573312016-01-25 14:33:25 -08002301 if (requires_srgb_conversion(surface->config(), config)) {
2302 return false;
2303 }
2304
2305 // We have a special case fallback for reading eight bit alpha. We will read back all four 8
2306 // bit channels as RGBA and then extract A.
Brian Salomon71d9d842016-11-03 13:42:00 -04002307 if (!this->readPixelsSupported(surface, config)) {
bsalomone9573312016-01-25 14:33:25 -08002308 // Don't attempt to do any srgb conversions since we only care about alpha.
2309 GrPixelConfig tempConfig = kRGBA_8888_GrPixelConfig;
Brian Salomon71d9d842016-11-03 13:42:00 -04002310 if (GrPixelConfigIsSRGB(surface->config())) {
bsalomone9573312016-01-25 14:33:25 -08002311 tempConfig = kSRGBA_8888_GrPixelConfig;
2312 }
2313 if (kAlpha_8_GrPixelConfig == config &&
Brian Salomon71d9d842016-11-03 13:42:00 -04002314 this->readPixelsSupported(surface, tempConfig)) {
Ben Wagner7ecc5962016-11-02 17:07:33 -04002315 std::unique_ptr<uint32_t[]> temp(new uint32_t[width * height * 4]);
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002316 if (this->onReadPixels(surface, origin, left, top, width, height,
2317 tempConfig, temp.get(), width*4)) {
bsalomone9573312016-01-25 14:33:25 -08002318 uint8_t* dst = reinterpret_cast<uint8_t*>(buffer);
2319 for (int j = 0; j < height; ++j) {
2320 for (int i = 0; i < width; ++i) {
2321 dst[j*rowBytes + i] = (0xFF000000U & temp[j*width+i]) >> 24;
2322 }
2323 }
2324 return true;
2325 }
2326 }
bsalomon16921ec2015-07-30 15:34:56 -07002327 return false;
2328 }
2329
bsalomon76148af2016-01-12 11:13:47 -08002330 GrGLenum externalFormat;
2331 GrGLenum externalType;
Brian Salomon71d9d842016-11-03 13:42:00 -04002332 if (!this->glCaps().getReadPixelsFormat(surface->config(), config, &externalFormat,
bsalomon76148af2016-01-12 11:13:47 -08002333 &externalType)) {
2334 return false;
2335 }
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002336 bool flipY = kBottomLeft_GrSurfaceOrigin == origin;
bsalomon@google.comc4364992011-11-07 15:54:49 +00002337
Brian Salomon71d9d842016-11-03 13:42:00 -04002338 GrGLIRect glvp;
2339 if (renderTarget) {
2340 // resolve the render target if necessary
2341 switch (renderTarget->getResolveType()) {
2342 case GrGLRenderTarget::kCantResolve_ResolveType:
2343 return false;
2344 case GrGLRenderTarget::kAutoResolves_ResolveType:
2345 this->flushRenderTarget(renderTarget, &SkIRect::EmptyIRect());
2346 break;
2347 case GrGLRenderTarget::kCanResolve_ResolveType:
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002348 this->onResolveRenderTarget(renderTarget, origin);
Brian Salomon71d9d842016-11-03 13:42:00 -04002349 // we don't track the state of the READ FBO ID.
2350 fStats.incRenderTargetBinds();
2351 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER, renderTarget->textureFBOID()));
2352 break;
2353 default:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -04002354 SK_ABORT("Unknown resolve type");
Brian Salomon71d9d842016-11-03 13:42:00 -04002355 }
2356 glvp = renderTarget->getViewport();
2357 } else {
2358 // Use a temporary FBO.
2359 this->bindSurfaceFBOForPixelOps(surface, GR_GL_FRAMEBUFFER, &glvp, kSrc_TempFBOTarget);
Robert Phillips294870f2016-11-11 12:38:40 -05002360 fHWBoundRenderTargetUniqueID.makeInvalid();
reed@google.comac10a2d2010-12-22 21:39:39 +00002361 }
2362
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00002363 // the read rect is viewport-relative
2364 GrGLIRect readRect;
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002365 readRect.setRelativeTo(glvp, left, top, width, height, origin);
rmistry@google.comfbfcd562012-08-23 18:09:54 +00002366
bsalomon9d02b262016-02-01 12:49:30 -08002367 size_t bytesPerPixel = GrBytesPerPixel(config);
2368 size_t tightRowBytes = bytesPerPixel * width;
egdaniel6d901da2015-07-30 12:02:15 -07002369
bsalomon@google.comc6980972011-11-02 19:57:21 +00002370 size_t readDstRowBytes = tightRowBytes;
2371 void* readDst = buffer;
rmistry@google.comfbfcd562012-08-23 18:09:54 +00002372
bsalomon@google.comc6980972011-11-02 19:57:21 +00002373 // determine if GL can read using the passed rowBytes or if we need
2374 // a scratch buffer.
joshualitt29f86792015-05-29 08:06:48 -07002375 SkAutoSMalloc<32 * sizeof(GrColor)> scratch;
bsalomon@google.comc6980972011-11-02 19:57:21 +00002376 if (rowBytes != tightRowBytes) {
bsalomon9d02b262016-02-01 12:49:30 -08002377 if (this->glCaps().packRowLengthSupport() && !(rowBytes % bytesPerPixel)) {
skia.committer@gmail.com4677acc2013-10-17 07:02:33 +00002378 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH,
bsalomon9d02b262016-02-01 12:49:30 -08002379 static_cast<GrGLint>(rowBytes / bytesPerPixel)));
bsalomon@google.comc6980972011-11-02 19:57:21 +00002380 readDstRowBytes = rowBytes;
2381 } else {
2382 scratch.reset(tightRowBytes * height);
2383 readDst = scratch.get();
2384 }
2385 }
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002386 if (flipY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00002387 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 1));
2388 }
bsalomonf46a1242015-12-15 12:37:38 -08002389 GL_CALL(PixelStorei(GR_GL_PACK_ALIGNMENT, config_alignment(config)));
2390
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002391 GL_CALL(ReadPixels(readRect.fLeft, readRect.fBottom,
2392 readRect.fWidth, readRect.fHeight,
bsalomon76148af2016-01-12 11:13:47 -08002393 externalFormat, externalType, readDst));
bsalomon@google.comc6980972011-11-02 19:57:21 +00002394 if (readDstRowBytes != tightRowBytes) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002395 SkASSERT(this->glCaps().packRowLengthSupport());
bsalomon@google.comc6980972011-11-02 19:57:21 +00002396 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
2397 }
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002398 if (flipY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00002399 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 0));
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002400 flipY = false;
bsalomon@google.com56d11e02011-11-30 19:59:08 +00002401 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002402
2403 // now reverse the order of the rows, since GL's are bottom-to-top, but our
bsalomon@google.comc6980972011-11-02 19:57:21 +00002404 // API presents top-to-bottom. We must preserve the padding contents. Note
2405 // that the above readPixels did not overwrite the padding.
2406 if (readDst == buffer) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002407 SkASSERT(rowBytes == readDstRowBytes);
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002408 if (flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002409 scratch.reset(tightRowBytes);
2410 void* tmpRow = scratch.get();
2411 // flip y in-place by rows
2412 const int halfY = height >> 1;
2413 char* top = reinterpret_cast<char*>(buffer);
2414 char* bottom = top + (height - 1) * rowBytes;
2415 for (int y = 0; y < halfY; y++) {
2416 memcpy(tmpRow, top, tightRowBytes);
2417 memcpy(top, bottom, tightRowBytes);
2418 memcpy(bottom, tmpRow, tightRowBytes);
2419 top += rowBytes;
2420 bottom -= rowBytes;
2421 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00002422 }
2423 } else {
bsalomon9d02b262016-02-01 12:49:30 -08002424 SkASSERT(readDst != buffer);
2425 SkASSERT(rowBytes != tightRowBytes);
bsalomon@google.comc6980972011-11-02 19:57:21 +00002426 // copy from readDst to buffer while flipping y
caryclark@google.comcf6285b2012-06-06 12:09:01 +00002427 // const int halfY = height >> 1;
bsalomon@google.comc6980972011-11-02 19:57:21 +00002428 const char* src = reinterpret_cast<const char*>(readDst);
bsalomon@google.comc4364992011-11-07 15:54:49 +00002429 char* dst = reinterpret_cast<char*>(buffer);
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002430 if (flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002431 dst += (height-1) * rowBytes;
2432 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00002433 for (int y = 0; y < height; y++) {
2434 memcpy(dst, src, tightRowBytes);
2435 src += readDstRowBytes;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002436 if (!flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002437 dst += rowBytes;
2438 } else {
2439 dst -= rowBytes;
2440 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002441 }
2442 }
Brian Salomon71d9d842016-11-03 13:42:00 -04002443 if (!renderTarget) {
2444 this->unbindTextureFBOForPixelOps(GR_GL_FRAMEBUFFER, surface);
2445 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002446 return true;
2447}
2448
Greg Daniel500d58b2017-08-24 15:59:33 -04002449GrGpuRTCommandBuffer* GrGLGpu::createCommandBuffer(
Robert Phillips19e51dc2017-08-09 09:30:51 -04002450 GrRenderTarget* rt, GrSurfaceOrigin origin,
Robert Phillips6b47c7d2017-08-29 07:24:09 -04002451 const GrGpuRTCommandBuffer::LoadAndStoreInfo& colorInfo,
Greg Daniel500d58b2017-08-24 15:59:33 -04002452 const GrGpuRTCommandBuffer::StencilLoadAndStoreInfo& stencilInfo) {
Robert Phillips6b47c7d2017-08-29 07:24:09 -04002453 return new GrGLGpuRTCommandBuffer(this, rt, origin, colorInfo, stencilInfo);
Greg Daniel500d58b2017-08-24 15:59:33 -04002454}
2455
2456GrGpuTextureCommandBuffer* GrGLGpu::createCommandBuffer(GrTexture* texture,
2457 GrSurfaceOrigin origin) {
2458 return new GrGLGpuTextureCommandBuffer(this, texture, origin);
egdaniel066df7c2016-06-08 14:02:27 -07002459}
2460
brianosman64d094d2016-03-25 06:01:59 -07002461void GrGLGpu::flushRenderTarget(GrGLRenderTarget* target, const SkIRect* bounds, bool disableSRGB) {
egdanield803f272015-03-18 13:01:52 -07002462 SkASSERT(target);
bsalomon6ba6fa12015-03-04 11:57:37 -08002463
Robert Phillips294870f2016-11-11 12:38:40 -05002464 GrGpuResource::UniqueID rtID = target->uniqueID();
egdanield803f272015-03-18 13:01:52 -07002465 if (fHWBoundRenderTargetUniqueID != rtID) {
bsalomon1e0bf7e2015-03-14 12:08:51 -07002466 fStats.incRenderTargetBinds();
egdanield803f272015-03-18 13:01:52 -07002467 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, target->renderFBOID()));
2468#ifdef SK_DEBUG
2469 // don't do this check in Chromium -- this is causing
2470 // lots of repeated command buffer flushes when the compositor is
2471 // rendering with Ganesh, which is really slow; even too slow for
2472 // Debug mode.
cdalton1acea862015-06-02 13:05:52 -07002473 if (kChromium_GrGLDriver != this->glContext().driver()) {
egdanield803f272015-03-18 13:01:52 -07002474 GrGLenum status;
2475 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
2476 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
2477 SkDebugf("GrGLGpu::flushRenderTarget glCheckFramebufferStatus %x\n", status);
2478 }
bsalomon160f24c2015-03-17 15:55:42 -07002479 }
egdanield803f272015-03-18 13:01:52 -07002480#endif
2481 fHWBoundRenderTargetUniqueID = rtID;
bsalomon083617b2016-02-12 12:10:14 -08002482 this->flushViewport(target->getViewport());
brianosman64d094d2016-03-25 06:01:59 -07002483 }
2484
brianosman35b784d2016-05-05 11:52:53 -07002485 if (this->glCaps().srgbWriteControl()) {
brianosman33f6b3f2016-06-02 05:49:21 -07002486 this->flushFramebufferSRGB(GrPixelConfigIsSRGB(target->config()) && !disableSRGB);
bsalomon5cd020f2015-03-17 12:46:56 -07002487 }
brianosman64d094d2016-03-25 06:01:59 -07002488
bsalomon083617b2016-02-12 12:10:14 -08002489 this->didWriteToSurface(target, bounds);
2490}
bsalomona9909122016-01-23 10:41:40 -08002491
brianosman33f6b3f2016-06-02 05:49:21 -07002492void GrGLGpu::flushFramebufferSRGB(bool enable) {
2493 if (enable && kYes_TriState != fHWSRGBFramebuffer) {
2494 GL_CALL(Enable(GR_GL_FRAMEBUFFER_SRGB));
2495 fHWSRGBFramebuffer = kYes_TriState;
2496 } else if (!enable && kNo_TriState != fHWSRGBFramebuffer) {
2497 GL_CALL(Disable(GR_GL_FRAMEBUFFER_SRGB));
2498 fHWSRGBFramebuffer = kNo_TriState;
2499 }
2500}
2501
bsalomon083617b2016-02-12 12:10:14 -08002502void GrGLGpu::flushViewport(const GrGLIRect& viewport) {
2503 if (fHWViewport != viewport) {
2504 viewport.pushToGLViewport(this->glInterface());
2505 fHWViewport = viewport;
2506 }
2507}
2508
bsalomon@google.comd302f142011-03-03 13:54:13 +00002509#define SWAP_PER_DRAW 0
2510
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00002511#if SWAP_PER_DRAW
commit-bot@chromium.org43823302013-09-25 20:57:51 +00002512 #if defined(SK_BUILD_FOR_MAC)
bsalomon@google.comd302f142011-03-03 13:54:13 +00002513 #include <AGL/agl.h>
commit-bot@chromium.org43823302013-09-25 20:57:51 +00002514 #elif defined(SK_BUILD_FOR_WIN32)
bsalomon@google.comce7357d2012-06-25 17:49:25 +00002515 #include <gl/GL.h>
bsalomon@google.comd302f142011-03-03 13:54:13 +00002516 void SwapBuf() {
2517 DWORD procID = GetCurrentProcessId();
2518 HWND hwnd = GetTopWindow(GetDesktopWindow());
2519 while(hwnd) {
2520 DWORD wndProcID = 0;
2521 GetWindowThreadProcessId(hwnd, &wndProcID);
2522 if(wndProcID == procID) {
2523 SwapBuffers(GetDC(hwnd));
2524 }
2525 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
2526 }
2527 }
2528 #endif
2529#endif
2530
egdaniel9cb63402016-06-23 08:37:05 -07002531void GrGLGpu::draw(const GrPipeline& pipeline,
2532 const GrPrimitiveProcessor& primProc,
bsalomon2eda5b32016-09-21 10:53:24 -07002533 const GrMesh meshes[],
Chris Dalton46983b72017-06-06 12:27:16 -06002534 const GrPipeline::DynamicState dynamicStates[],
egdaniel9cb63402016-06-23 08:37:05 -07002535 int meshCount) {
2536 this->handleDirtyContext();
2537
bsalomon2eda5b32016-09-21 10:53:24 -07002538 bool hasPoints = false;
2539 for (int i = 0; i < meshCount; ++i) {
Chris Dalton3809bab2017-06-13 10:55:06 -06002540 if (meshes[i].primitiveType() == GrPrimitiveType::kPoints) {
bsalomon2eda5b32016-09-21 10:53:24 -07002541 hasPoints = true;
2542 break;
2543 }
2544 }
2545 if (!this->flushGLState(pipeline, primProc, hasPoints)) {
bsalomond95263c2014-12-16 13:05:12 -08002546 return;
2547 }
ethannicholas22793252016-01-30 09:59:10 -08002548
egdaniel0e1853c2016-03-17 11:35:45 -07002549 for (int i = 0; i < meshCount; ++i) {
2550 if (GrXferBarrierType barrierType = pipeline.xferBarrierType(*this->caps())) {
Robert Phillips2890fbf2017-07-26 15:48:41 -04002551 this->xferBarrier(pipeline.renderTarget(), barrierType);
egdaniel0e1853c2016-03-17 11:35:45 -07002552 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002553
Chris Dalton46983b72017-06-06 12:27:16 -06002554 if (dynamicStates) {
2555 if (pipeline.getScissorState().enabled()) {
Robert Phillips2890fbf2017-07-26 15:48:41 -04002556 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(pipeline.renderTarget());
Chris Dalton46983b72017-06-06 12:27:16 -06002557 this->flushScissor(dynamicStates[i].fScissorRect,
Robert Phillips16d8ec62017-07-27 16:16:25 -04002558 glRT->getViewport(), pipeline.proxy()->origin());
Chris Dalton46983b72017-06-06 12:27:16 -06002559 }
2560 }
Brian Salomon6d9c88b2017-06-12 10:24:42 -04002561 if (this->glCaps().requiresCullFaceEnableDisableWhenDrawingLinesAfterNonLines() &&
2562 GrIsPrimTypeLines(meshes[i].primitiveType()) &&
2563 !GrIsPrimTypeLines(fLastPrimitiveType)) {
2564 GL_CALL(Enable(GR_GL_CULL_FACE));
2565 GL_CALL(Disable(GR_GL_CULL_FACE));
2566 }
Chris Dalton114a3c02017-05-26 15:17:19 -06002567 meshes[i].sendToGpu(primProc, this);
Brian Salomon6d9c88b2017-06-12 10:24:42 -04002568 fLastPrimitiveType = meshes[i].primitiveType();
bsalomon@google.com74749cd2013-01-30 16:12:41 +00002569 }
ethannicholas22793252016-01-30 09:59:10 -08002570
bsalomon@google.comd302f142011-03-03 13:54:13 +00002571#if SWAP_PER_DRAW
2572 glFlush();
commit-bot@chromium.org43823302013-09-25 20:57:51 +00002573 #if defined(SK_BUILD_FOR_MAC)
bsalomon@google.comd302f142011-03-03 13:54:13 +00002574 aglSwapBuffers(aglGetCurrentContext());
2575 int set_a_break_pt_here = 9;
2576 aglSwapBuffers(aglGetCurrentContext());
commit-bot@chromium.org43823302013-09-25 20:57:51 +00002577 #elif defined(SK_BUILD_FOR_WIN32)
bsalomon@google.comd302f142011-03-03 13:54:13 +00002578 SwapBuf();
2579 int set_a_break_pt_here = 9;
2580 SwapBuf();
2581 #endif
2582#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00002583}
2584
Chris Dalton3809bab2017-06-13 10:55:06 -06002585static GrGLenum gr_primitive_type_to_gl_mode(GrPrimitiveType primitiveType) {
2586 switch (primitiveType) {
2587 case GrPrimitiveType::kTriangles:
2588 return GR_GL_TRIANGLES;
2589 case GrPrimitiveType::kTriangleStrip:
2590 return GR_GL_TRIANGLE_STRIP;
2591 case GrPrimitiveType::kTriangleFan:
2592 return GR_GL_TRIANGLE_FAN;
2593 case GrPrimitiveType::kPoints:
2594 return GR_GL_POINTS;
2595 case GrPrimitiveType::kLines:
2596 return GR_GL_LINES;
2597 case GrPrimitiveType::kLineStrip:
2598 return GR_GL_LINE_STRIP;
2599 case GrPrimitiveType::kLinesAdjacency:
2600 return GR_GL_LINES_ADJACENCY;
2601 }
Ben Wagnerb4aab9a2017-08-16 10:53:04 -04002602 SK_ABORT("invalid GrPrimitiveType");
Chris Dalton3809bab2017-06-13 10:55:06 -06002603 return GR_GL_TRIANGLES;
2604}
2605
Chris Dalton114a3c02017-05-26 15:17:19 -06002606void GrGLGpu::sendMeshToGpu(const GrPrimitiveProcessor& primProc, GrPrimitiveType primitiveType,
2607 const GrBuffer* vertexBuffer, int vertexCount, int baseVertex) {
Chris Dalton3809bab2017-06-13 10:55:06 -06002608 const GrGLenum glPrimType = gr_primitive_type_to_gl_mode(primitiveType);
Chris Dalton114a3c02017-05-26 15:17:19 -06002609 if (this->glCaps().drawArraysBaseVertexIsBroken()) {
Chris Dalton1d616352017-05-31 12:51:23 -06002610 this->setupGeometry(primProc, nullptr, vertexBuffer, baseVertex, nullptr, 0);
Chris Dalton114a3c02017-05-26 15:17:19 -06002611 GL_CALL(DrawArrays(glPrimType, 0, vertexCount));
2612 } else {
Chris Dalton1d616352017-05-31 12:51:23 -06002613 this->setupGeometry(primProc, nullptr, vertexBuffer, 0, nullptr, 0);
Chris Dalton114a3c02017-05-26 15:17:19 -06002614 GL_CALL(DrawArrays(glPrimType, baseVertex, vertexCount));
2615 }
2616 fStats.incNumDraws();
2617}
2618
2619void GrGLGpu::sendIndexedMeshToGpu(const GrPrimitiveProcessor& primProc,
2620 GrPrimitiveType primitiveType, const GrBuffer* indexBuffer,
2621 int indexCount, int baseIndex, uint16_t minIndexValue,
2622 uint16_t maxIndexValue, const GrBuffer* vertexBuffer,
2623 int baseVertex) {
Chris Dalton3809bab2017-06-13 10:55:06 -06002624 const GrGLenum glPrimType = gr_primitive_type_to_gl_mode(primitiveType);
Chris Dalton114a3c02017-05-26 15:17:19 -06002625 GrGLvoid* const indices = reinterpret_cast<void*>(indexBuffer->baseOffset() +
2626 sizeof(uint16_t) * baseIndex);
2627
Chris Dalton1d616352017-05-31 12:51:23 -06002628 this->setupGeometry(primProc, indexBuffer, vertexBuffer, baseVertex, nullptr, 0);
Chris Dalton114a3c02017-05-26 15:17:19 -06002629
2630 if (this->glCaps().drawRangeElementsSupport()) {
2631 GL_CALL(DrawRangeElements(glPrimType, minIndexValue, maxIndexValue, indexCount,
2632 GR_GL_UNSIGNED_SHORT, indices));
2633 } else {
2634 GL_CALL(DrawElements(glPrimType, indexCount, GR_GL_UNSIGNED_SHORT, indices));
2635 }
2636 fStats.incNumDraws();
2637}
2638
Chris Dalton1d616352017-05-31 12:51:23 -06002639void GrGLGpu::sendInstancedMeshToGpu(const GrPrimitiveProcessor& primProc, GrPrimitiveType
2640 primitiveType, const GrBuffer* vertexBuffer,
2641 int vertexCount, int baseVertex,
2642 const GrBuffer* instanceBuffer, int instanceCount,
2643 int baseInstance) {
Chris Daltoncc604e52017-10-06 16:27:32 -06002644 GrGLenum glPrimType = gr_primitive_type_to_gl_mode(primitiveType);
2645 int maxInstances = this->glCaps().maxInstancesPerDrawArraysWithoutCrashing(instanceCount);
2646 for (int i = 0; i < instanceCount; i += maxInstances) {
2647 this->setupGeometry(primProc, nullptr, vertexBuffer, 0, instanceBuffer, baseInstance + i);
2648 GL_CALL(DrawArraysInstanced(glPrimType, baseVertex, vertexCount,
2649 SkTMin(instanceCount - i, maxInstances)));
2650 fStats.incNumDraws();
2651 }
Chris Dalton1d616352017-05-31 12:51:23 -06002652}
2653
2654void GrGLGpu::sendIndexedInstancedMeshToGpu(const GrPrimitiveProcessor& primProc,
2655 GrPrimitiveType primitiveType,
2656 const GrBuffer* indexBuffer, int indexCount,
2657 int baseIndex, const GrBuffer* vertexBuffer,
2658 int baseVertex, const GrBuffer* instanceBuffer,
2659 int instanceCount, int baseInstance) {
Chris Dalton3809bab2017-06-13 10:55:06 -06002660 const GrGLenum glPrimType = gr_primitive_type_to_gl_mode(primitiveType);
Chris Dalton1d616352017-05-31 12:51:23 -06002661 GrGLvoid* indices = reinterpret_cast<void*>(indexBuffer->baseOffset() +
2662 sizeof(uint16_t) * baseIndex);
2663 this->setupGeometry(primProc, indexBuffer, vertexBuffer, baseVertex,
2664 instanceBuffer, baseInstance);
2665 GL_CALL(DrawElementsInstanced(glPrimType, indexCount, GR_GL_UNSIGNED_SHORT, indices,
2666 instanceCount));
2667 fStats.incNumDraws();
2668}
2669
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002670void GrGLGpu::onResolveRenderTarget(GrRenderTarget* target, GrSurfaceOrigin origin) {
bsalomon@google.com75f9f252012-01-31 13:35:56 +00002671 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00002672 if (rt->needsResolve()) {
bsalomon@google.com347c3822013-05-01 20:10:01 +00002673 // Some extensions automatically resolves the texture when it is read.
2674 if (this->glCaps().usesMSAARenderBuffers()) {
egdanield803f272015-03-18 13:01:52 -07002675 SkASSERT(rt->textureFBOID() != rt->renderFBOID());
2676 fStats.incRenderTargetBinds();
2677 fStats.incRenderTargetBinds();
2678 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER, rt->renderFBOID()));
2679 GL_CALL(BindFramebuffer(GR_GL_DRAW_FRAMEBUFFER, rt->textureFBOID()));
2680 // make sure we go through flushRenderTarget() since we've modified
2681 // the bound DRAW FBO ID.
Robert Phillips294870f2016-11-11 12:38:40 -05002682 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002683 const GrGLIRect& vp = rt->getViewport();
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00002684 const SkIRect dirtyRect = rt->getResolveRect();
reed@google.comac10a2d2010-12-22 21:39:39 +00002685
bsalomon@google.com347c3822013-05-01 20:10:01 +00002686 if (GrGLCaps::kES_Apple_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002687 // Apple's extension uses the scissor as the blit bounds.
bsalomon3e791242014-12-17 13:43:13 -08002688 GrScissorState scissorState;
robertphillipse85a32d2015-02-10 08:16:55 -08002689 scissorState.set(dirtyRect);
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002690 this->flushScissor(scissorState, vp, origin);
csmartdalton28341fa2016-08-17 10:00:21 -07002691 this->disableWindowRectangles();
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002692 GL_CALL(ResolveMultisampleFramebuffer());
2693 } else {
Brian Salomone5e7eb12016-10-14 16:18:33 -04002694 int l, b, r, t;
2695 if (GrGLCaps::kResolveMustBeFull_BlitFrambufferFlag &
2696 this->glCaps().blitFramebufferSupportFlags()) {
2697 l = 0;
2698 b = 0;
2699 r = target->width();
2700 t = target->height();
2701 } else {
2702 GrGLIRect rect;
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002703 rect.setRelativeTo(vp, dirtyRect, origin);
Brian Salomone5e7eb12016-10-14 16:18:33 -04002704 l = rect.fLeft;
2705 b = rect.fBottom;
2706 r = rect.fLeft + rect.fWidth;
2707 t = rect.fBottom + rect.fHeight;
2708 }
derekf8c8f71a2014-09-16 06:24:57 -07002709
2710 // BlitFrameBuffer respects the scissor, so disable it.
joshualitt77b13072014-10-27 14:51:01 -07002711 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07002712 this->disableWindowRectangles();
Brian Salomone5e7eb12016-10-14 16:18:33 -04002713 GL_CALL(BlitFramebuffer(l, b, r, t, l, b, r, t,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002714 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00002715 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002716 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00002717 rt->flagAsResolved();
reed@google.comac10a2d2010-12-22 21:39:39 +00002718 }
2719}
2720
bsalomon@google.com411dad02012-06-05 20:24:20 +00002721namespace {
bsalomon@google.comd302f142011-03-03 13:54:13 +00002722
bsalomon@google.com411dad02012-06-05 20:24:20 +00002723
2724GrGLenum gr_to_gl_stencil_op(GrStencilOp op) {
cdalton93a379b2016-05-11 13:58:08 -07002725 static const GrGLenum gTable[kGrStencilOpCount] = {
2726 GR_GL_KEEP, // kKeep
2727 GR_GL_ZERO, // kZero
2728 GR_GL_REPLACE, // kReplace
2729 GR_GL_INVERT, // kInvert
2730 GR_GL_INCR_WRAP, // kIncWrap
2731 GR_GL_DECR_WRAP, // kDecWrap
2732 GR_GL_INCR, // kIncClamp
2733 GR_GL_DECR, // kDecClamp
bsalomon@google.com411dad02012-06-05 20:24:20 +00002734 };
cdalton93a379b2016-05-11 13:58:08 -07002735 GR_STATIC_ASSERT(0 == (int)GrStencilOp::kKeep);
2736 GR_STATIC_ASSERT(1 == (int)GrStencilOp::kZero);
2737 GR_STATIC_ASSERT(2 == (int)GrStencilOp::kReplace);
2738 GR_STATIC_ASSERT(3 == (int)GrStencilOp::kInvert);
2739 GR_STATIC_ASSERT(4 == (int)GrStencilOp::kIncWrap);
2740 GR_STATIC_ASSERT(5 == (int)GrStencilOp::kDecWrap);
2741 GR_STATIC_ASSERT(6 == (int)GrStencilOp::kIncClamp);
2742 GR_STATIC_ASSERT(7 == (int)GrStencilOp::kDecClamp);
2743 SkASSERT(op < (GrStencilOp)kGrStencilOpCount);
2744 return gTable[(int)op];
bsalomon@google.com411dad02012-06-05 20:24:20 +00002745}
2746
2747void set_gl_stencil(const GrGLInterface* gl,
cdalton93a379b2016-05-11 13:58:08 -07002748 const GrStencilSettings::Face& face,
2749 GrGLenum glFace) {
2750 GrGLenum glFunc = GrToGLStencilFunc(face.fTest);
2751 GrGLenum glFailOp = gr_to_gl_stencil_op(face.fFailOp);
2752 GrGLenum glPassOp = gr_to_gl_stencil_op(face.fPassOp);
bsalomon@google.coma3201942012-06-21 19:58:20 +00002753
cdalton93a379b2016-05-11 13:58:08 -07002754 GrGLint ref = face.fRef;
2755 GrGLint mask = face.fTestMask;
2756 GrGLint writeMask = face.fWriteMask;
bsalomon@google.com411dad02012-06-05 20:24:20 +00002757
2758 if (GR_GL_FRONT_AND_BACK == glFace) {
2759 // we call the combined func just in case separate stencil is not
2760 // supported.
2761 GR_GL_CALL(gl, StencilFunc(glFunc, ref, mask));
2762 GR_GL_CALL(gl, StencilMask(writeMask));
vbuzinovc5d58f02015-08-21 05:24:24 -07002763 GR_GL_CALL(gl, StencilOp(glFailOp, GR_GL_KEEP, glPassOp));
bsalomon@google.com411dad02012-06-05 20:24:20 +00002764 } else {
2765 GR_GL_CALL(gl, StencilFuncSeparate(glFace, glFunc, ref, mask));
2766 GR_GL_CALL(gl, StencilMaskSeparate(glFace, writeMask));
vbuzinovc5d58f02015-08-21 05:24:24 -07002767 GR_GL_CALL(gl, StencilOpSeparate(glFace, glFailOp, GR_GL_KEEP, glPassOp));
bsalomon@google.com411dad02012-06-05 20:24:20 +00002768 }
2769}
2770}
bsalomon@google.comd302f142011-03-03 13:54:13 +00002771
bsalomon3e791242014-12-17 13:43:13 -08002772void GrGLGpu::flushStencil(const GrStencilSettings& stencilSettings) {
csmartdaltonc7d85332016-10-26 10:13:46 -07002773 if (stencilSettings.isDisabled()) {
2774 this->disableStencil();
2775 } else if (fHWStencilSettings != stencilSettings) {
2776 if (kYes_TriState != fHWStencilTestEnabled) {
2777 GL_CALL(Enable(GR_GL_STENCIL_TEST));
Brian Salomonaf971de2017-06-08 16:11:33 -04002778
csmartdaltonc7d85332016-10-26 10:13:46 -07002779 fHWStencilTestEnabled = kYes_TriState;
bsalomon@google.coma3201942012-06-21 19:58:20 +00002780 }
csmartdaltonc7d85332016-10-26 10:13:46 -07002781 if (stencilSettings.isTwoSided()) {
csmartdaltonc7d85332016-10-26 10:13:46 -07002782 set_gl_stencil(this->glInterface(),
2783 stencilSettings.front(),
2784 GR_GL_FRONT);
2785 set_gl_stencil(this->glInterface(),
2786 stencilSettings.back(),
2787 GR_GL_BACK);
2788 } else {
2789 set_gl_stencil(this->glInterface(),
2790 stencilSettings.front(),
2791 GR_GL_FRONT_AND_BACK);
bsalomon@google.comd302f142011-03-03 13:54:13 +00002792 }
joshualitta58fe352014-10-27 08:39:00 -07002793 fHWStencilSettings = stencilSettings;
reed@google.comac10a2d2010-12-22 21:39:39 +00002794 }
2795}
2796
csmartdaltonc7d85332016-10-26 10:13:46 -07002797void GrGLGpu::disableStencil() {
2798 if (kNo_TriState != fHWStencilTestEnabled) {
2799 GL_CALL(Disable(GR_GL_STENCIL_TEST));
Brian Salomonaf971de2017-06-08 16:11:33 -04002800
csmartdaltonc7d85332016-10-26 10:13:46 -07002801 fHWStencilTestEnabled = kNo_TriState;
2802 fHWStencilSettings.invalidate();
2803 }
2804}
2805
cdaltonaf8bc7d2016-02-05 09:35:20 -08002806void GrGLGpu::flushHWAAState(GrRenderTarget* rt, bool useHWAA, bool stencilEnabled) {
bsalomon083617b2016-02-12 12:10:14 -08002807 // rt is only optional if useHWAA is false.
2808 SkASSERT(rt || !useHWAA);
vbuzinovdded6962015-06-12 08:59:45 -07002809 SkASSERT(!useHWAA || rt->isStencilBufferMultisampled());
bsalomon@google.com202d1392013-03-19 18:58:08 +00002810
csmartdalton2b5f2cb2016-06-10 14:06:32 -07002811 if (this->caps()->multisampleDisableSupport()) {
cdaltond0a840d2015-03-16 17:19:58 -07002812 if (useHWAA) {
2813 if (kYes_TriState != fMSAAEnabled) {
2814 GL_CALL(Enable(GR_GL_MULTISAMPLE));
2815 fMSAAEnabled = kYes_TriState;
2816 }
2817 } else {
2818 if (kNo_TriState != fMSAAEnabled) {
2819 GL_CALL(Disable(GR_GL_MULTISAMPLE));
2820 fMSAAEnabled = kNo_TriState;
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00002821 }
2822 }
2823 }
cdaltonaf8bc7d2016-02-05 09:35:20 -08002824
2825 if (0 != this->caps()->maxRasterSamples()) {
Brian Salomon7c8460e2017-05-12 11:36:10 -04002826 if (useHWAA && GrFSAAType::kMixedSamples == rt->fsaaType() && !stencilEnabled) {
cdaltonaf8bc7d2016-02-05 09:35:20 -08002827 // Since stencil is disabled and we want more samples than are in the color buffer, we
2828 // need to tell the rasterizer explicitly how many to run.
2829 if (kYes_TriState != fHWRasterMultisampleEnabled) {
2830 GL_CALL(Enable(GR_GL_RASTER_MULTISAMPLE));
2831 fHWRasterMultisampleEnabled = kYes_TriState;
2832 }
2833 if (rt->numStencilSamples() != fHWNumRasterSamples) {
2834 SkASSERT(rt->numStencilSamples() <= this->caps()->maxRasterSamples());
2835 GL_CALL(RasterSamples(rt->numStencilSamples(), GR_GL_TRUE));
2836 fHWNumRasterSamples = rt->numStencilSamples();
2837 }
2838 } else {
2839 if (kNo_TriState != fHWRasterMultisampleEnabled) {
2840 GL_CALL(Disable(GR_GL_RASTER_MULTISAMPLE));
2841 fHWRasterMultisampleEnabled = kNo_TriState;
2842 }
2843 }
2844 } else {
Brian Salomon7c8460e2017-05-12 11:36:10 -04002845 SkASSERT(!useHWAA || GrFSAAType::kMixedSamples != rt->fsaaType() || stencilEnabled);
cdaltonaf8bc7d2016-02-05 09:35:20 -08002846 }
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00002847}
2848
bsalomon7f9b2e42016-01-12 13:29:26 -08002849void GrGLGpu::flushBlend(const GrXferProcessor::BlendInfo& blendInfo, const GrSwizzle& swizzle) {
egdanielb414f252014-07-29 13:15:47 -07002850 // Any optimization to disable blending should have already been applied and
cdalton8917d622015-05-06 13:40:21 -07002851 // tweaked the equation to "add" or "subtract", and the coeffs to (1, 0).
bsalomonf7cc8772015-05-11 11:21:14 -07002852
cdalton8917d622015-05-06 13:40:21 -07002853 GrBlendEquation equation = blendInfo.fEquation;
egdanielc2304142014-12-11 13:15:13 -08002854 GrBlendCoeff srcCoeff = blendInfo.fSrcBlend;
2855 GrBlendCoeff dstCoeff = blendInfo.fDstBlend;
cdalton8917d622015-05-06 13:40:21 -07002856 bool blendOff = (kAdd_GrBlendEquation == equation || kSubtract_GrBlendEquation == equation) &&
2857 kOne_GrBlendCoeff == srcCoeff && kZero_GrBlendCoeff == dstCoeff;
egdanielb414f252014-07-29 13:15:47 -07002858 if (blendOff) {
2859 if (kNo_TriState != fHWBlendState.fEnabled) {
2860 GL_CALL(Disable(GR_GL_BLEND));
joel.liang9764c402015-07-09 19:46:18 -07002861
2862 // Workaround for the ARM KHR_blend_equation_advanced blacklist issue
2863 // https://code.google.com/p/skia/issues/detail?id=3943
2864 if (kARM_GrGLVendor == this->ctxInfo().vendor() &&
2865 GrBlendEquationIsAdvanced(fHWBlendState.fEquation)) {
2866 SkASSERT(this->caps()->advancedBlendEquationSupport());
2867 // Set to any basic blending equation.
2868 GrBlendEquation blend_equation = kAdd_GrBlendEquation;
2869 GL_CALL(BlendEquation(gXfermodeEquation2Blend[blend_equation]));
2870 fHWBlendState.fEquation = blend_equation;
2871 }
2872
egdanielb414f252014-07-29 13:15:47 -07002873 fHWBlendState.fEnabled = kNo_TriState;
2874 }
cdalton8917d622015-05-06 13:40:21 -07002875 return;
2876 }
2877
2878 if (kYes_TriState != fHWBlendState.fEnabled) {
2879 GL_CALL(Enable(GR_GL_BLEND));
Brian Salomonaf971de2017-06-08 16:11:33 -04002880
cdalton8917d622015-05-06 13:40:21 -07002881 fHWBlendState.fEnabled = kYes_TriState;
2882 }
2883
2884 if (fHWBlendState.fEquation != equation) {
2885 GL_CALL(BlendEquation(gXfermodeEquation2Blend[equation]));
2886 fHWBlendState.fEquation = equation;
2887 }
2888
2889 if (GrBlendEquationIsAdvanced(equation)) {
2890 SkASSERT(this->caps()->advancedBlendEquationSupport());
2891 // Advanced equations have no other blend state.
2892 return;
2893 }
2894
bsalomone63ffef2016-02-05 07:17:34 -08002895 if (fHWBlendState.fSrcCoeff != srcCoeff || fHWBlendState.fDstCoeff != dstCoeff) {
cdalton8917d622015-05-06 13:40:21 -07002896 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
2897 gXfermodeCoeff2Blend[dstCoeff]));
2898 fHWBlendState.fSrcCoeff = srcCoeff;
2899 fHWBlendState.fDstCoeff = dstCoeff;
2900 }
2901
bsalomon7f9b2e42016-01-12 13:29:26 -08002902 if ((BlendCoeffReferencesConstant(srcCoeff) || BlendCoeffReferencesConstant(dstCoeff))) {
2903 GrColor blendConst = blendInfo.fBlendConstant;
2904 blendConst = swizzle.applyTo(blendConst);
2905 if (!fHWBlendState.fConstColorValid || fHWBlendState.fConstColor != blendConst) {
2906 GrGLfloat c[4];
2907 GrColorToRGBAFloat(blendConst, c);
2908 GL_CALL(BlendColor(c[0], c[1], c[2], c[3]));
2909 fHWBlendState.fConstColor = blendConst;
2910 fHWBlendState.fConstColorValid = true;
2911 }
bsalomon@google.com0650e812011-04-08 18:07:53 +00002912 }
2913}
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002914
Brian Salomon2bbdcc42017-09-07 12:36:34 -04002915static inline GrGLenum wrap_mode_to_gl_wrap(GrSamplerState::WrapMode wrapMode) {
2916 switch (wrapMode) {
2917 case GrSamplerState::WrapMode::kClamp:
2918 return GR_GL_CLAMP_TO_EDGE;
2919 case GrSamplerState::WrapMode::kRepeat:
2920 return GR_GL_REPEAT;
2921 case GrSamplerState::WrapMode::kMirrorRepeat:
2922 return GR_GL_MIRRORED_REPEAT;
bsalomon@google.comb8670992012-07-25 21:27:09 +00002923 };
Brian Salomon2bbdcc42017-09-07 12:36:34 -04002924 SK_ABORT("Unknown wrap mode");
2925 return 0;
bsalomon@google.comb8670992012-07-25 21:27:09 +00002926}
2927
egdanielb7e7d572015-11-04 04:23:53 -08002928static GrGLenum get_component_enum_from_char(char component) {
2929 switch (component) {
2930 case 'r':
2931 return GR_GL_RED;
2932 case 'g':
2933 return GR_GL_GREEN;
2934 case 'b':
2935 return GR_GL_BLUE;
2936 case 'a':
2937 return GR_GL_ALPHA;
2938 default:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -04002939 SK_ABORT("Unsupported component");
egdanielb7e7d572015-11-04 04:23:53 -08002940 return 0;
2941 }
2942}
2943
2944/** If texture swizzling is available using tex parameters then it is preferred over mangling
2945 the generated shader code. This potentially allows greater reuse of cached shaders. */
2946static void get_tex_param_swizzle(GrPixelConfig config,
bsalomoncdee0092016-01-08 13:20:12 -08002947 const GrGLCaps& caps,
egdanielb7e7d572015-11-04 04:23:53 -08002948 GrGLenum* glSwizzle) {
bsalomoncdee0092016-01-08 13:20:12 -08002949 const GrSwizzle& swizzle = caps.configSwizzle(config);
egdanielb7e7d572015-11-04 04:23:53 -08002950 for (int i = 0; i < 4; ++i) {
bsalomoncdee0092016-01-08 13:20:12 -08002951 glSwizzle[i] = get_component_enum_from_char(swizzle.c_str()[i]);
egdaniel574a4c12015-11-02 06:22:44 -08002952 }
2953}
2954
Brian Salomon2bbdcc42017-09-07 12:36:34 -04002955static GrGLenum filter_to_gl_mag_filter(GrSamplerState::Filter filter) {
2956 switch (filter) {
2957 case GrSamplerState::Filter::kNearest:
2958 return GR_GL_NEAREST;
2959 case GrSamplerState::Filter::kBilerp:
2960 return GR_GL_LINEAR;
2961 case GrSamplerState::Filter::kMipMap:
2962 return GR_GL_LINEAR;
2963 }
2964 SK_ABORT("Unknown filter");
2965 return 0;
2966}
2967
2968static GrGLenum filter_to_gl_min_filter(GrSamplerState::Filter filter) {
2969 switch (filter) {
2970 case GrSamplerState::Filter::kNearest:
2971 return GR_GL_NEAREST;
2972 case GrSamplerState::Filter::kBilerp:
2973 return GR_GL_LINEAR;
2974 case GrSamplerState::Filter::kMipMap:
2975 return GR_GL_LINEAR_MIPMAP_LINEAR;
2976 }
2977 SK_ABORT("Unknown filter");
2978 return 0;
2979}
2980
2981void GrGLGpu::bindTexture(int unitIdx, const GrSamplerState& samplerState, bool allowSRGBInputs,
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002982 GrGLTexture* texture, GrSurfaceOrigin textureOrigin) {
bsalomon49f085d2014-09-05 13:34:00 -07002983 SkASSERT(texture);
tomhudson@google.comd0c1a062012-07-12 17:23:52 +00002984
reed856e9d92015-09-30 12:21:45 -07002985#ifdef SK_DEBUG
2986 if (!this->caps()->npotTextureTileSupport()) {
Brian Salomon2bbdcc42017-09-07 12:36:34 -04002987 if (samplerState.isRepeated()) {
reed856e9d92015-09-30 12:21:45 -07002988 const int w = texture->width();
2989 const int h = texture->height();
2990 SkASSERT(SkIsPow2(w) && SkIsPow2(h));
2991 }
2992 }
2993#endif
2994
bsalomon@google.comb8670992012-07-25 21:27:09 +00002995 // If we created a rt/tex and rendered to it without using a texture and now we're texturing
2996 // 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 +00002997 // out of the "last != next" check.
bsalomon37dd3312014-11-03 08:47:23 -08002998 GrGLRenderTarget* texRT = static_cast<GrGLRenderTarget*>(texture->asRenderTarget());
bsalomon49f085d2014-09-05 13:34:00 -07002999 if (texRT) {
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003000 this->onResolveRenderTarget(texRT, textureOrigin);
bsalomon@google.com4c883782012-06-04 19:05:11 +00003001 }
3002
Robert Phillips294870f2016-11-11 12:38:40 -05003003 GrGpuResource::UniqueID textureID = texture->uniqueID();
bsalomon10528f12015-10-14 12:54:52 -07003004 GrGLenum target = texture->target();
bsalomon1c63bf62014-07-22 13:09:46 -07003005 if (fHWBoundTextureUniqueIDs[unitIdx] != textureID) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00003006 this->setTextureUnit(unitIdx);
bsalomon10528f12015-10-14 12:54:52 -07003007 GL_CALL(BindTexture(target, texture->textureID()));
bsalomon1c63bf62014-07-22 13:09:46 -07003008 fHWBoundTextureUniqueIDs[unitIdx] = textureID;
bsalomon@google.com4c883782012-06-04 19:05:11 +00003009 }
3010
bsalomon@google.com4c883782012-06-04 19:05:11 +00003011 ResetTimestamp timestamp;
bsalomon@google.com34cccde2013-01-04 18:34:30 +00003012 const GrGLTexture::TexParams& oldTexParams = texture->getCachedTexParams(&timestamp);
bsalomon@google.com4c883782012-06-04 19:05:11 +00003013 bool setAll = timestamp < this->getResetTimestamp();
3014 GrGLTexture::TexParams newTexParams;
3015
Brian Salomon2bbdcc42017-09-07 12:36:34 -04003016 GrSamplerState::Filter filterMode = samplerState.filter();
bsalomonefd7d452014-10-23 14:17:46 -07003017
Brian Salomon2bbdcc42017-09-07 12:36:34 -04003018 if (GrSamplerState::Filter::kMipMap == filterMode) {
Robert Phillips92de6312017-05-23 07:43:48 -04003019 if (!this->caps()->mipMapSupport()) {
Brian Salomon2bbdcc42017-09-07 12:36:34 -04003020 filterMode = GrSamplerState::Filter::kBilerp;
bsalomonefd7d452014-10-23 14:17:46 -07003021 }
commit-bot@chromium.org47442312013-12-19 16:18:01 +00003022 }
bsalomonefd7d452014-10-23 14:17:46 -07003023
Brian Salomon2bbdcc42017-09-07 12:36:34 -04003024 newTexParams.fMinFilter = filter_to_gl_min_filter(filterMode);
3025 newTexParams.fMagFilter = filter_to_gl_mag_filter(filterMode);
skia.committer@gmail.comaeefb2a2013-07-27 07:01:06 +00003026
brianosman20471892016-12-02 06:43:32 -08003027 if (this->glCaps().srgbDecodeDisableSupport() && GrPixelConfigIsSRGB(texture->config())) {
brianosmanfe199872016-06-13 07:59:48 -07003028 newTexParams.fSRGBDecode = allowSRGBInputs ? GR_GL_DECODE_EXT : GR_GL_SKIP_DECODE_EXT;
brianosman33f6b3f2016-06-02 05:49:21 -07003029 if (setAll || newTexParams.fSRGBDecode != oldTexParams.fSRGBDecode) {
brianosmanadf4fa62016-04-26 16:48:06 -07003030 this->setTextureUnit(unitIdx);
brianosman33f6b3f2016-06-02 05:49:21 -07003031 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SRGB_DECODE_EXT, newTexParams.fSRGBDecode));
cblume55f2d2d2016-02-26 13:20:48 -08003032 }
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +00003033 }
bsalomon@google.com4c883782012-06-04 19:05:11 +00003034
brianosman33f6b3f2016-06-02 05:49:21 -07003035#ifdef SK_DEBUG
3036 // We were supposed to ensure MipMaps were up-to-date and built correctly before getting here.
Brian Salomon2bbdcc42017-09-07 12:36:34 -04003037 if (GrSamplerState::Filter::kMipMap == filterMode) {
brianosman33f6b3f2016-06-02 05:49:21 -07003038 SkASSERT(!texture->texturePriv().mipMapsAreDirty());
3039 if (GrPixelConfigIsSRGB(texture->config())) {
Brian Osman7b8400d2016-11-08 17:08:54 -05003040 SkDestinationSurfaceColorMode colorMode = allowSRGBInputs
3041 ? SkDestinationSurfaceColorMode::kGammaAndColorSpaceAware
3042 : SkDestinationSurfaceColorMode::kLegacy;
3043 SkASSERT(texture->texturePriv().mipColorMode() == colorMode);
brianosman33f6b3f2016-06-02 05:49:21 -07003044 }
3045 }
3046#endif
3047
cblume55f2d2d2016-02-26 13:20:48 -08003048 newTexParams.fMaxMipMapLevel = texture->texturePriv().maxMipMapLevel();
3049
Brian Salomon2bbdcc42017-09-07 12:36:34 -04003050 newTexParams.fWrapS = wrap_mode_to_gl_wrap(samplerState.wrapModeX());
3051 newTexParams.fWrapT = wrap_mode_to_gl_wrap(samplerState.wrapModeY());
bsalomoncdee0092016-01-08 13:20:12 -08003052 get_tex_param_swizzle(texture->config(), this->glCaps(), newTexParams.fSwizzleRGBA);
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00003053 if (setAll || newTexParams.fMagFilter != oldTexParams.fMagFilter) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00003054 this->setTextureUnit(unitIdx);
bsalomon10528f12015-10-14 12:54:52 -07003055 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MAG_FILTER, newTexParams.fMagFilter));
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00003056 }
3057 if (setAll || newTexParams.fMinFilter != oldTexParams.fMinFilter) {
3058 this->setTextureUnit(unitIdx);
bsalomon10528f12015-10-14 12:54:52 -07003059 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MIN_FILTER, newTexParams.fMinFilter));
bsalomon@google.com4c883782012-06-04 19:05:11 +00003060 }
cblume55f2d2d2016-02-26 13:20:48 -08003061 if (setAll || newTexParams.fMaxMipMapLevel != oldTexParams.fMaxMipMapLevel) {
cblume09bd2c02016-03-01 14:08:28 -08003062 // These are not supported in ES2 contexts
3063 if (this->glCaps().mipMapLevelAndLodControlSupport()) {
3064 if (newTexParams.fMaxMipMapLevel != 0) {
3065 this->setTextureUnit(unitIdx);
3066 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MIN_LOD, 0));
3067 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_BASE_LEVEL, 0));
3068 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MAX_LOD,
3069 newTexParams.fMaxMipMapLevel));
3070 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MAX_LEVEL,
3071 newTexParams.fMaxMipMapLevel));
3072 }
cblume55f2d2d2016-02-26 13:20:48 -08003073 }
3074 }
bsalomon@google.com4c883782012-06-04 19:05:11 +00003075 if (setAll || newTexParams.fWrapS != oldTexParams.fWrapS) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00003076 this->setTextureUnit(unitIdx);
bsalomon10528f12015-10-14 12:54:52 -07003077 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_WRAP_S, newTexParams.fWrapS));
bsalomon@google.com4c883782012-06-04 19:05:11 +00003078 }
3079 if (setAll || newTexParams.fWrapT != oldTexParams.fWrapT) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00003080 this->setTextureUnit(unitIdx);
bsalomon10528f12015-10-14 12:54:52 -07003081 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_WRAP_T, newTexParams.fWrapT));
bsalomon@google.com4c883782012-06-04 19:05:11 +00003082 }
bsalomoncdee0092016-01-08 13:20:12 -08003083 if (this->glCaps().textureSwizzleSupport() &&
bsalomon@google.com4c883782012-06-04 19:05:11 +00003084 (setAll || memcmp(newTexParams.fSwizzleRGBA,
3085 oldTexParams.fSwizzleRGBA,
3086 sizeof(newTexParams.fSwizzleRGBA)))) {
cdalton74b8d322016-04-11 14:47:28 -07003087 this->setTextureSwizzle(unitIdx, target, newTexParams.fSwizzleRGBA);
bsalomon@google.com4c883782012-06-04 19:05:11 +00003088 }
bsalomon@google.com34cccde2013-01-04 18:34:30 +00003089 texture->setCachedTexParams(newTexParams, this->getResetTimestamp());
bsalomon@google.com4c883782012-06-04 19:05:11 +00003090}
3091
csmartdalton1897cfd2016-06-03 08:50:54 -07003092void GrGLGpu::bindTexelBuffer(int unitIdx, GrPixelConfig texelConfig, GrGLBuffer* buffer) {
cdalton74b8d322016-04-11 14:47:28 -07003093 SkASSERT(this->glCaps().canUseConfigWithTexelBuffer(texelConfig));
3094 SkASSERT(unitIdx >= 0 && unitIdx < fHWBufferTextures.count());
cdalton74b8d322016-04-11 14:47:28 -07003095
3096 BufferTexture& buffTex = fHWBufferTextures[unitIdx];
3097
3098 if (!buffTex.fKnownBound) {
3099 if (!buffTex.fTextureID) {
3100 GL_CALL(GenTextures(1, &buffTex.fTextureID));
3101 if (!buffTex.fTextureID) {
3102 return;
3103 }
3104 }
3105
3106 this->setTextureUnit(unitIdx);
3107 GL_CALL(BindTexture(GR_GL_TEXTURE_BUFFER, buffTex.fTextureID));
3108
3109 buffTex.fKnownBound = true;
3110 }
3111
robertphillips8abb3702016-08-31 14:04:06 -07003112 if (buffer->uniqueID() != buffTex.fAttachedBufferUniqueID ||
csmartdalton1897cfd2016-06-03 08:50:54 -07003113 buffTex.fTexelConfig != texelConfig) {
cdalton74b8d322016-04-11 14:47:28 -07003114
3115 this->setTextureUnit(unitIdx);
csmartdalton1897cfd2016-06-03 08:50:54 -07003116 GL_CALL(TexBuffer(GR_GL_TEXTURE_BUFFER,
3117 this->glCaps().configSizedInternalFormat(texelConfig),
3118 buffer->bufferID()));
cdalton74b8d322016-04-11 14:47:28 -07003119
cdalton74b8d322016-04-11 14:47:28 -07003120 buffTex.fTexelConfig = texelConfig;
robertphillips8abb3702016-08-31 14:04:06 -07003121 buffTex.fAttachedBufferUniqueID = buffer->uniqueID();
cdalton74b8d322016-04-11 14:47:28 -07003122
3123 if (this->glCaps().textureSwizzleSupport() &&
3124 this->glCaps().configSwizzle(texelConfig) != buffTex.fSwizzle) {
3125 GrGLenum glSwizzle[4];
3126 get_tex_param_swizzle(texelConfig, this->glCaps(), glSwizzle);
3127 this->setTextureSwizzle(unitIdx, GR_GL_TEXTURE_BUFFER, glSwizzle);
3128 buffTex.fSwizzle = this->glCaps().configSwizzle(texelConfig);
3129 }
3130
3131 buffer->setHasAttachedToTexture();
3132 fHWMaxUsedBufferTextureUnit = SkTMax(unitIdx, fHWMaxUsedBufferTextureUnit);
3133 }
3134}
3135
Brian Salomonf9f45122016-11-29 11:59:17 -05003136void GrGLGpu::bindImageStorage(int unitIdx, GrIOType ioType, GrGLTexture *texture) {
3137 SkASSERT(texture);
3138 if (texture->uniqueID() != fHWBoundImageStorages[unitIdx].fTextureUniqueID ||
3139 ioType != fHWBoundImageStorages[unitIdx].fIOType) {
Joe Gregorio94e4ab12016-11-30 14:33:42 -05003140 GrGLenum access = GR_GL_READ_ONLY;
Brian Salomonf9f45122016-11-29 11:59:17 -05003141 switch (ioType) {
3142 case kRead_GrIOType:
3143 access = GR_GL_READ_ONLY;
3144 break;
3145 case kWrite_GrIOType:
3146 access = GR_GL_WRITE_ONLY;
3147 break;
3148 case kRW_GrIOType:
3149 access = GR_GL_READ_WRITE;
3150 break;
3151 }
3152 GrGLenum format = this->glCaps().getImageFormat(texture->config());
3153 GL_CALL(BindImageTexture(unitIdx, texture->textureID(), 0, GR_GL_FALSE, 0, access, format));
3154 }
3155}
3156
Brian Salomon2bbdcc42017-09-07 12:36:34 -04003157void GrGLGpu::generateMipmaps(const GrSamplerState& params, bool allowSRGBInputs,
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003158 GrGLTexture* texture, GrSurfaceOrigin textureOrigin) {
brianosman33f6b3f2016-06-02 05:49:21 -07003159 SkASSERT(texture);
3160
3161 // First, figure out if we need mips for this texture at all:
Brian Salomon2bbdcc42017-09-07 12:36:34 -04003162 GrSamplerState::Filter filterMode = params.filter();
brianosman33f6b3f2016-06-02 05:49:21 -07003163
Brian Salomon2bbdcc42017-09-07 12:36:34 -04003164 if (GrSamplerState::Filter::kMipMap == filterMode) {
Robert Phillips92de6312017-05-23 07:43:48 -04003165 if (!this->caps()->mipMapSupport()) {
Brian Salomon2bbdcc42017-09-07 12:36:34 -04003166 filterMode = GrSamplerState::Filter::kBilerp;
brianosman33f6b3f2016-06-02 05:49:21 -07003167 }
3168 }
3169
Brian Salomon2bbdcc42017-09-07 12:36:34 -04003170 if (GrSamplerState::Filter::kMipMap != filterMode) {
brianosman33f6b3f2016-06-02 05:49:21 -07003171 return;
3172 }
3173
3174 // If this is an sRGB texture and the mips were previously built the "other" way
3175 // (gamma-correct vs. not), then we need to rebuild them. We don't need to check for
3176 // srgbSupport - we'll *never* get an sRGB pixel config if we don't support it.
Brian Osman7b8400d2016-11-08 17:08:54 -05003177 SkDestinationSurfaceColorMode colorMode = allowSRGBInputs
3178 ? SkDestinationSurfaceColorMode::kGammaAndColorSpaceAware
3179 : SkDestinationSurfaceColorMode::kLegacy;
brianosman33f6b3f2016-06-02 05:49:21 -07003180 if (GrPixelConfigIsSRGB(texture->config()) &&
Brian Osman7b8400d2016-11-08 17:08:54 -05003181 colorMode != texture->texturePriv().mipColorMode()) {
Greg Daniel0fc4d2d2017-10-12 11:23:36 -04003182 texture->texturePriv().markMipMapsDirty();
brianosman33f6b3f2016-06-02 05:49:21 -07003183 }
3184
3185 // If the mips aren't dirty, we're done:
3186 if (!texture->texturePriv().mipMapsAreDirty()) {
3187 return;
3188 }
3189
3190 // If we created a rt/tex and rendered to it without using a texture and now we're texturing
3191 // from the rt it will still be the last bound texture, but it needs resolving.
3192 GrGLRenderTarget* texRT = static_cast<GrGLRenderTarget*>(texture->asRenderTarget());
3193 if (texRT) {
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003194 this->onResolveRenderTarget(texRT, textureOrigin);
brianosman33f6b3f2016-06-02 05:49:21 -07003195 }
3196
3197 GrGLenum target = texture->target();
3198 this->setScratchTextureUnit();
3199 GL_CALL(BindTexture(target, texture->textureID()));
3200
3201 // Configure sRGB decode, if necessary. This state is the only thing needed for the driver
3202 // call (glGenerateMipmap) to work correctly. Our manual method dirties other state, too.
brianosman20471892016-12-02 06:43:32 -08003203 if (this->glCaps().srgbDecodeDisableSupport() && GrPixelConfigIsSRGB(texture->config())) {
Brian Osmanb0403202016-12-09 10:55:23 -05003204 GrGLenum srgbDecode = allowSRGBInputs ? GR_GL_DECODE_EXT : GR_GL_SKIP_DECODE_EXT;
3205 // Command buffer's sRGB decode extension doesn't influence mipmap generation correctly.
3206 // If we set this to skip_decode, it appears to suppress sRGB -> Linear for each downsample,
3207 // but not the Linear -> sRGB when writing the next level. The result is that mip-chains
3208 // get progressively brighter as you go down. Forcing this to 'decode' gives predictable
3209 // (and only slightly incorrect) results. See crbug.com/655247 (~comment 28)
3210 if (!this->glCaps().srgbDecodeDisableAffectsMipmaps()) {
3211 srgbDecode = GR_GL_DECODE_EXT;
3212 }
3213 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SRGB_DECODE_EXT, srgbDecode));
brianosman33f6b3f2016-06-02 05:49:21 -07003214 }
3215
3216 // Either do manual mipmap generation or (if that fails), just rely on the driver:
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003217 if (!this->generateMipmap(texture, textureOrigin, allowSRGBInputs)) {
brianosman33f6b3f2016-06-02 05:49:21 -07003218 GL_CALL(GenerateMipmap(target));
3219 }
3220
Greg Daniel0fc4d2d2017-10-12 11:23:36 -04003221 texture->texturePriv().markMipMapsClean();
brianosman33f6b3f2016-06-02 05:49:21 -07003222 texture->texturePriv().setMaxMipMapLevel(SkMipMap::ComputeLevelCount(
3223 texture->width(), texture->height()));
Brian Osman7b8400d2016-11-08 17:08:54 -05003224 texture->texturePriv().setMipColorMode(colorMode);
brianosman33f6b3f2016-06-02 05:49:21 -07003225
3226 // We have potentially set lots of state on the texture. Easiest to dirty it all:
3227 texture->textureParamsModified();
3228}
3229
cdalton74b8d322016-04-11 14:47:28 -07003230void GrGLGpu::setTextureSwizzle(int unitIdx, GrGLenum target, const GrGLenum swizzle[]) {
3231 this->setTextureUnit(unitIdx);
3232 if (this->glStandard() == kGLES_GrGLStandard) {
3233 // ES3 added swizzle support but not GL_TEXTURE_SWIZZLE_RGBA.
3234 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_R, swizzle[0]));
3235 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_G, swizzle[1]));
3236 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_B, swizzle[2]));
3237 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_A, swizzle[3]));
3238 } else {
3239 GR_STATIC_ASSERT(sizeof(swizzle[0]) == sizeof(GrGLint));
3240 GL_CALL(TexParameteriv(target, GR_GL_TEXTURE_SWIZZLE_RGBA,
3241 reinterpret_cast<const GrGLint*>(swizzle)));
3242 }
3243}
3244
egdaniel080e6732014-12-22 07:35:52 -08003245void GrGLGpu::flushColorWrite(bool writeColor) {
3246 if (!writeColor) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +00003247 if (kNo_TriState != fHWWriteToColor) {
3248 GL_CALL(ColorMask(GR_GL_FALSE, GR_GL_FALSE,
3249 GR_GL_FALSE, GR_GL_FALSE));
3250 fHWWriteToColor = kNo_TriState;
bsalomon@google.comd302f142011-03-03 13:54:13 +00003251 }
bsalomon@google.com978c8c62012-05-21 14:45:49 +00003252 } else {
3253 if (kYes_TriState != fHWWriteToColor) {
3254 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
3255 fHWWriteToColor = kYes_TriState;
3256 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00003257 }
bsalomon3e791242014-12-17 13:43:13 -08003258}
bsalomon@google.comd302f142011-03-03 13:54:13 +00003259
bsalomon861e1032014-12-16 07:33:49 -08003260void GrGLGpu::setTextureUnit(int unit) {
bsalomon1c63bf62014-07-22 13:09:46 -07003261 SkASSERT(unit >= 0 && unit < fHWBoundTextureUniqueIDs.count());
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00003262 if (unit != fHWActiveTextureUnitIdx) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00003263 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com49209392012-06-05 15:13:46 +00003264 fHWActiveTextureUnitIdx = unit;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00003265 }
3266}
bsalomon@google.com316f99232011-01-13 21:28:12 +00003267
bsalomon861e1032014-12-16 07:33:49 -08003268void GrGLGpu::setScratchTextureUnit() {
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00003269 // Bind the last texture unit since it is the least likely to be used by GrGLProgram.
bsalomon1c63bf62014-07-22 13:09:46 -07003270 int lastUnitIdx = fHWBoundTextureUniqueIDs.count() - 1;
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00003271 if (lastUnitIdx != fHWActiveTextureUnitIdx) {
3272 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + lastUnitIdx));
3273 fHWActiveTextureUnitIdx = lastUnitIdx;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00003274 }
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00003275 // clear out the this field so that if a program does use this unit it will rebind the correct
3276 // texture.
Robert Phillips294870f2016-11-11 12:38:40 -05003277 fHWBoundTextureUniqueIDs[lastUnitIdx].makeInvalid();
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00003278}
3279
Brian Salomone5e7eb12016-10-14 16:18:33 -04003280// Determines whether glBlitFramebuffer could be used between src and dst by onCopySurface.
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003281static inline bool can_blit_framebuffer_for_copy_surface(
3282 const GrSurface* dst, GrSurfaceOrigin dstOrigin,
3283 const GrSurface* src, GrSurfaceOrigin srcOrigin,
3284 const SkIRect& srcRect,
3285 const SkIPoint& dstPoint,
3286 const GrGLGpu* gpu) {
Brian Salomone5e7eb12016-10-14 16:18:33 -04003287 auto blitFramebufferFlags = gpu->glCaps().blitFramebufferSupportFlags();
Brian Salomon71d9d842016-11-03 13:42:00 -04003288 if (!gpu->glCaps().canConfigBeFBOColorAttachment(dst->config()) ||
3289 !gpu->glCaps().canConfigBeFBOColorAttachment(src->config())) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00003290 return false;
3291 }
Brian Salomonbf7b6202016-11-11 16:08:03 -05003292 // Blits are not allowed between int color buffers and float/fixed color buffers. GrGpu should
3293 // have filtered such cases out.
3294 SkASSERT(GrPixelConfigIsSint(dst->config()) == GrPixelConfigIsSint(src->config()));
Brian Salomone5e7eb12016-10-14 16:18:33 -04003295 const GrGLTexture* dstTex = static_cast<const GrGLTexture*>(dst->asTexture());
3296 const GrGLTexture* srcTex = static_cast<const GrGLTexture*>(dst->asTexture());
3297 const GrRenderTarget* dstRT = dst->asRenderTarget();
3298 const GrRenderTarget* srcRT = src->asRenderTarget();
3299 if (dstTex && dstTex->target() != GR_GL_TEXTURE_2D) {
3300 return false;
3301 }
3302 if (srcTex && srcTex->target() != GR_GL_TEXTURE_2D) {
3303 return false;
3304 }
3305 if (GrGLCaps::kNoSupport_BlitFramebufferFlag & blitFramebufferFlags) {
3306 return false;
3307 }
3308 if (GrGLCaps::kNoScalingOrMirroring_BlitFramebufferFlag & blitFramebufferFlags) {
3309 // We would mirror to compensate for origin changes. Note that copySurface is
3310 // specified such that the src and dst rects are the same.
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003311 if (dstOrigin != srcOrigin) {
Brian Salomone5e7eb12016-10-14 16:18:33 -04003312 return false;
3313 }
3314 }
3315 if (GrGLCaps::kResolveMustBeFull_BlitFrambufferFlag & blitFramebufferFlags) {
Eric Karl74480882017-04-03 14:49:05 -07003316 if (srcRT && srcRT->numColorSamples()) {
3317 if (dstRT && !dstRT->numColorSamples()) {
3318 return false;
3319 }
3320 if (SkRect::Make(srcRect) != srcRT->getBoundsRect()) {
3321 return false;
3322 }
Brian Salomone5e7eb12016-10-14 16:18:33 -04003323 }
3324 }
3325 if (GrGLCaps::kNoMSAADst_BlitFramebufferFlag & blitFramebufferFlags) {
3326 if (dstRT && dstRT->numColorSamples() > 0) {
3327 return false;
3328 }
Ben Wagner63fd7602017-10-09 15:45:33 -04003329 }
Brian Salomone5e7eb12016-10-14 16:18:33 -04003330 if (GrGLCaps::kNoFormatConversion_BlitFramebufferFlag & blitFramebufferFlags) {
3331 if (dst->config() != src->config()) {
3332 return false;
3333 }
3334 } else if (GrGLCaps::kNoFormatConversionForMSAASrc_BlitFramebufferFlag & blitFramebufferFlags) {
3335 const GrRenderTarget* srcRT = src->asRenderTarget();
3336 if (srcRT && srcRT->numColorSamples() && dst->config() != src->config()) {
3337 return false;
3338 }
3339 }
3340 if (GrGLCaps::kRectsMustMatchForMSAASrc_BlitFramebufferFlag & blitFramebufferFlags) {
Eric Karl74480882017-04-03 14:49:05 -07003341 if (srcRT && srcRT->numColorSamples()) {
3342 if (dstPoint.fX != srcRect.fLeft || dstPoint.fY != srcRect.fTop) {
3343 return false;
3344 }
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003345 if (dstOrigin != srcOrigin) {
Eric Karl74480882017-04-03 14:49:05 -07003346 return false;
3347 }
Brian Salomone5e7eb12016-10-14 16:18:33 -04003348 }
3349 }
3350 return true;
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00003351}
bsalomon@google.comeb851172013-04-15 13:51:00 +00003352
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003353static inline bool can_copy_texsubimage(const GrSurface* dst, GrSurfaceOrigin dstOrigin,
3354 const GrSurface* src, GrSurfaceOrigin srcOrigin,
bsalomon7ea33f52015-11-22 14:51:00 -08003355 const GrGLGpu* gpu) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00003356 // Table 3.9 of the ES2 spec indicates the supported formats with CopyTexSubImage
3357 // and BGRA isn't in the spec. There doesn't appear to be any extension that adds it. Perhaps
3358 // many drivers would allow it to work, but ANGLE does not.
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +00003359 if (kGLES_GrGLStandard == gpu->glStandard() && gpu->glCaps().bgraIsInternalFormat() &&
bsalomon@google.comeb851172013-04-15 13:51:00 +00003360 (kBGRA_8888_GrPixelConfig == dst->config() || kBGRA_8888_GrPixelConfig == src->config())) {
3361 return false;
3362 }
3363 const GrGLRenderTarget* dstRT = static_cast<const GrGLRenderTarget*>(dst->asRenderTarget());
3364 // If dst is multisampled (and uses an extension where there is a separate MSAA renderbuffer)
3365 // then we don't want to copy to the texture but to the MSAA buffer.
egdanield803f272015-03-18 13:01:52 -07003366 if (dstRT && dstRT->renderFBOID() != dstRT->textureFBOID()) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00003367 return false;
3368 }
bsalomon@google.coma2719852013-04-17 14:25:27 +00003369 const GrGLRenderTarget* srcRT = static_cast<const GrGLRenderTarget*>(src->asRenderTarget());
3370 // If the src is multisampled (and uses an extension where there is a separate MSAA
3371 // renderbuffer) then it is an invalid operation to call CopyTexSubImage
egdanield803f272015-03-18 13:01:52 -07003372 if (srcRT && srcRT->renderFBOID() != srcRT->textureFBOID()) {
bsalomon@google.coma2719852013-04-17 14:25:27 +00003373 return false;
3374 }
bsalomon7ea33f52015-11-22 14:51:00 -08003375
3376 const GrGLTexture* dstTex = static_cast<const GrGLTexture*>(dst->asTexture());
3377 // CopyTex(Sub)Image writes to a texture and we have no way of dynamically wrapping a RT in a
3378 // texture.
3379 if (!dstTex) {
3380 return false;
3381 }
3382
3383 const GrGLTexture* srcTex = static_cast<const GrGLTexture*>(src->asTexture());
cblume61214052016-01-26 09:10:48 -08003384
bsalomon7ea33f52015-11-22 14:51:00 -08003385 // Check that we could wrap the source in an FBO, that the dst is TEXTURE_2D, that no mirroring
3386 // is required.
Brian Salomon71d9d842016-11-03 13:42:00 -04003387 if (gpu->glCaps().canConfigBeFBOColorAttachment(src->config()) &&
Brian Salomon71d9d842016-11-03 13:42:00 -04003388 (!srcTex || srcTex->target() == GR_GL_TEXTURE_2D) && dstTex->target() == GR_GL_TEXTURE_2D &&
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003389 dstOrigin == srcOrigin) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00003390 return true;
3391 } else {
3392 return false;
3393 }
3394}
3395
3396// If a temporary FBO was created, its non-zero ID is returned. The viewport that the copy rect is
3397// relative to is output.
Brian Salomon71d9d842016-11-03 13:42:00 -04003398void GrGLGpu::bindSurfaceFBOForPixelOps(GrSurface* surface, GrGLenum fboTarget, GrGLIRect* viewport,
3399 TempFBOTarget tempFBOTarget) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00003400 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(surface->asRenderTarget());
bsalomon083617b2016-02-12 12:10:14 -08003401 if (!rt) {
bsalomon49f085d2014-09-05 13:34:00 -07003402 SkASSERT(surface->asTexture());
Brian Salomon9bada542017-06-12 12:09:30 -04003403 GrGLTexture* texture = static_cast<GrGLTexture*>(surface->asTexture());
3404 GrGLuint texID = texture->textureID();
3405 GrGLenum target = texture->target();
egdanield803f272015-03-18 13:01:52 -07003406 GrGLuint* tempFBOID;
3407 tempFBOID = kSrc_TempFBOTarget == tempFBOTarget ? &fTempSrcFBOID : &fTempDstFBOID;
egdaniel0f5f9672015-02-03 11:10:51 -08003408
egdanield803f272015-03-18 13:01:52 -07003409 if (0 == *tempFBOID) {
3410 GR_GL_CALL(this->glInterface(), GenFramebuffers(1, tempFBOID));
egdaniel0f5f9672015-02-03 11:10:51 -08003411 }
3412
egdanield803f272015-03-18 13:01:52 -07003413 fStats.incRenderTargetBinds();
3414 GR_GL_CALL(this->glInterface(), BindFramebuffer(fboTarget, *tempFBOID));
3415 GR_GL_CALL(this->glInterface(), FramebufferTexture2D(fboTarget,
robertphillips754f4e92014-09-18 13:52:08 -07003416 GR_GL_COLOR_ATTACHMENT0,
bsalomon10528f12015-10-14 12:54:52 -07003417 target,
robertphillips754f4e92014-09-18 13:52:08 -07003418 texID,
3419 0));
Brian Salomon9bada542017-06-12 12:09:30 -04003420 texture->baseLevelWasBoundToFBO();
bsalomon@google.comeb851172013-04-15 13:51:00 +00003421 viewport->fLeft = 0;
3422 viewport->fBottom = 0;
3423 viewport->fWidth = surface->width();
3424 viewport->fHeight = surface->height();
3425 } else {
egdanield803f272015-03-18 13:01:52 -07003426 fStats.incRenderTargetBinds();
3427 GR_GL_CALL(this->glInterface(), BindFramebuffer(fboTarget, rt->renderFBOID()));
bsalomon@google.comeb851172013-04-15 13:51:00 +00003428 *viewport = rt->getViewport();
3429 }
egdaniel0f5f9672015-02-03 11:10:51 -08003430}
3431
Brian Salomon71d9d842016-11-03 13:42:00 -04003432void GrGLGpu::unbindTextureFBOForPixelOps(GrGLenum fboTarget, GrSurface* surface) {
3433 // bindSurfaceFBOForPixelOps temporarily binds textures that are not render targets to
bsalomon10528f12015-10-14 12:54:52 -07003434 if (!surface->asRenderTarget()) {
3435 SkASSERT(surface->asTexture());
3436 GrGLenum textureTarget = static_cast<GrGLTexture*>(surface->asTexture())->target();
3437 GR_GL_CALL(this->glInterface(), FramebufferTexture2D(fboTarget,
3438 GR_GL_COLOR_ATTACHMENT0,
3439 textureTarget,
3440 0,
3441 0));
3442 }
bsalomon@google.comeb851172013-04-15 13:51:00 +00003443}
3444
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003445bool GrGLGpu::onCopySurface(GrSurface* dst, GrSurfaceOrigin dstOrigin,
3446 GrSurface* src, GrSurfaceOrigin srcOrigin,
3447 const SkIRect& srcRect, const SkIPoint& dstPoint) {
bsalomon7f9b2e42016-01-12 13:29:26 -08003448 // None of our copy methods can handle a swizzle. TODO: Make copySurfaceAsDraw handle the
3449 // swizzle.
Brian Salomon1edc5b92016-11-29 13:43:46 -05003450 if (this->caps()->shaderCaps()->configOutputSwizzle(src->config()) !=
3451 this->caps()->shaderCaps()->configOutputSwizzle(dst->config())) {
bsalomon7f9b2e42016-01-12 13:29:26 -08003452 return false;
3453 }
bsalomon083617b2016-02-12 12:10:14 -08003454 // Don't prefer copying as a draw if the dst doesn't already have a FBO object.
3455 bool preferCopy = SkToBool(dst->asRenderTarget());
3456 if (preferCopy && src->asTexture()) {
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003457 if (this->copySurfaceAsDraw(dst, dstOrigin, src, srcOrigin, srcRect, dstPoint)) {
cdaltone2e71c22016-04-07 18:13:29 -07003458 return true;
3459 }
bsalomon5df6fee2015-05-18 06:26:15 -07003460 }
cblume61214052016-01-26 09:10:48 -08003461
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003462 if (can_copy_texsubimage(dst, dstOrigin, src, srcOrigin, this)) {
3463 this->copySurfaceAsCopyTexSubImage(dst, dstOrigin, src, srcOrigin, srcRect, dstPoint);
bsalomon6df86402015-06-01 10:41:49 -07003464 return true;
3465 }
3466
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003467 if (can_blit_framebuffer_for_copy_surface(dst, dstOrigin, src, srcOrigin,
3468 srcRect, dstPoint, this)) {
3469 return this->copySurfaceAsBlitFramebuffer(dst, dstOrigin, src, srcOrigin,
3470 srcRect, dstPoint);
bsalomon6df86402015-06-01 10:41:49 -07003471 }
3472
bsalomon083617b2016-02-12 12:10:14 -08003473 if (!preferCopy && src->asTexture()) {
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003474 if (this->copySurfaceAsDraw(dst, dstOrigin, src, srcOrigin, srcRect, dstPoint)) {
cdaltone2e71c22016-04-07 18:13:29 -07003475 return true;
3476 }
bsalomon083617b2016-02-12 12:10:14 -08003477 }
3478
bsalomon6df86402015-06-01 10:41:49 -07003479 return false;
3480}
3481
Brian Salomonbf7b6202016-11-11 16:08:03 -05003482bool GrGLGpu::createCopyProgram(GrTexture* srcTex) {
Brian Osman39c08ac2017-07-26 09:36:09 -04003483 TRACE_EVENT0("skia", TRACE_FUNC);
Ryan Macnak38a10ad2017-07-10 10:36:34 -07003484
Brian Salomonbf7b6202016-11-11 16:08:03 -05003485 int progIdx = TextureToCopyProgramIdx(srcTex);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003486 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
Brian Salomonbf7b6202016-11-11 16:08:03 -05003487 GrSLType samplerType = srcTex->texturePriv().samplerType();
cdaltone2e71c22016-04-07 18:13:29 -07003488
3489 if (!fCopyProgramArrayBuffer) {
3490 static const GrGLfloat vdata[] = {
3491 0, 0,
3492 0, 1,
3493 1, 0,
3494 1, 1
3495 };
3496 fCopyProgramArrayBuffer.reset(GrGLBuffer::Create(this, sizeof(vdata), kVertex_GrBufferType,
3497 kStatic_GrAccessPattern, vdata));
3498 }
3499 if (!fCopyProgramArrayBuffer) {
3500 return false;
3501 }
3502
3503 SkASSERT(!fCopyPrograms[progIdx].fProgram);
3504 GL_CALL_RET(fCopyPrograms[progIdx].fProgram, CreateProgram());
3505 if (!fCopyPrograms[progIdx].fProgram) {
3506 return false;
3507 }
3508
Brian Salomon1edc5b92016-11-29 13:43:46 -05003509 const char* version = shaderCaps->versionDeclString();
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003510 GrShaderVar aVertex("a_vertex", kHalf2_GrSLType, GrShaderVar::kIn_TypeModifier);
3511 GrShaderVar uTexCoordXform("u_texCoordXform", kHalf4_GrSLType,
Brian Salomon99938a82016-11-21 13:41:08 -05003512 GrShaderVar::kUniform_TypeModifier);
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003513 GrShaderVar uPosXform("u_posXform", kHalf4_GrSLType, GrShaderVar::kUniform_TypeModifier);
Brian Salomon99938a82016-11-21 13:41:08 -05003514 GrShaderVar uTexture("u_texture", samplerType, GrShaderVar::kUniform_TypeModifier);
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003515 GrShaderVar vTexCoord("v_texCoord", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier);
3516 GrShaderVar oFragColor("o_FragColor", kHalf4_GrSLType, GrShaderVar::kOut_TypeModifier);
cdaltone2e71c22016-04-07 18:13:29 -07003517
3518 SkString vshaderTxt(version);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003519 if (shaderCaps->noperspectiveInterpolationSupport()) {
3520 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
cdaltone2e71c22016-04-07 18:13:29 -07003521 vshaderTxt.appendf("#extension %s : require\n", extension);
3522 }
3523 vTexCoord.addModifier("noperspective");
3524 }
3525
Brian Salomon1edc5b92016-11-29 13:43:46 -05003526 aVertex.appendDecl(shaderCaps, &vshaderTxt);
cdaltone2e71c22016-04-07 18:13:29 -07003527 vshaderTxt.append(";");
Brian Salomon1edc5b92016-11-29 13:43:46 -05003528 uTexCoordXform.appendDecl(shaderCaps, &vshaderTxt);
cdaltone2e71c22016-04-07 18:13:29 -07003529 vshaderTxt.append(";");
Brian Salomon1edc5b92016-11-29 13:43:46 -05003530 uPosXform.appendDecl(shaderCaps, &vshaderTxt);
cdaltone2e71c22016-04-07 18:13:29 -07003531 vshaderTxt.append(";");
Brian Salomon1edc5b92016-11-29 13:43:46 -05003532 vTexCoord.appendDecl(shaderCaps, &vshaderTxt);
cdaltone2e71c22016-04-07 18:13:29 -07003533 vshaderTxt.append(";");
3534
3535 vshaderTxt.append(
3536 "// Copy Program VS\n"
3537 "void main() {"
3538 " v_texCoord = a_vertex.xy * u_texCoordXform.xy + u_texCoordXform.zw;"
Ethan Nicholasbed683a2017-09-26 14:23:59 -04003539 " sk_Position.xy = a_vertex * u_posXform.xy + u_posXform.zw;"
3540 " sk_Position.zw = half2(0, 1);"
cdaltone2e71c22016-04-07 18:13:29 -07003541 "}"
3542 );
3543
3544 SkString fshaderTxt(version);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003545 if (shaderCaps->noperspectiveInterpolationSupport()) {
3546 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
cdaltone2e71c22016-04-07 18:13:29 -07003547 fshaderTxt.appendf("#extension %s : require\n", extension);
3548 }
3549 }
Brian Salomonbf7b6202016-11-11 16:08:03 -05003550 if (samplerType == kTextureExternalSampler_GrSLType) {
cdaltone2e71c22016-04-07 18:13:29 -07003551 fshaderTxt.appendf("#extension %s : require\n",
Brian Salomon1edc5b92016-11-29 13:43:46 -05003552 shaderCaps->externalTextureExtensionString());
cdaltone2e71c22016-04-07 18:13:29 -07003553 }
Brian Salomonf31ae492016-11-18 15:35:33 -05003554 vTexCoord.setTypeModifier(GrShaderVar::kIn_TypeModifier);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003555 vTexCoord.appendDecl(shaderCaps, &fshaderTxt);
cdaltone2e71c22016-04-07 18:13:29 -07003556 fshaderTxt.append(";");
Brian Salomon1edc5b92016-11-29 13:43:46 -05003557 uTexture.appendDecl(shaderCaps, &fshaderTxt);
cdaltone2e71c22016-04-07 18:13:29 -07003558 fshaderTxt.append(";");
cdaltone2e71c22016-04-07 18:13:29 -07003559 fshaderTxt.appendf(
3560 "// Copy Program FS\n"
3561 "void main() {"
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003562 " sk_FragColor = texture(u_texture, v_texCoord);"
3563 "}"
cdaltone2e71c22016-04-07 18:13:29 -07003564 );
3565
3566 const char* str;
3567 GrGLint length;
3568
3569 str = vshaderTxt.c_str();
3570 length = SkToInt(vshaderTxt.size());
Ethan Nicholas941e7e22016-12-12 15:33:30 -05003571 SkSL::Program::Settings settings;
3572 settings.fCaps = shaderCaps;
3573 SkSL::Program::Inputs inputs;
cdaltone2e71c22016-04-07 18:13:29 -07003574 GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fCopyPrograms[progIdx].fProgram,
3575 GR_GL_VERTEX_SHADER, &str, &length, 1,
Ethan Nicholas941e7e22016-12-12 15:33:30 -05003576 &fStats, settings, &inputs);
3577 SkASSERT(inputs.isEmpty());
cdaltone2e71c22016-04-07 18:13:29 -07003578
3579 str = fshaderTxt.c_str();
3580 length = SkToInt(fshaderTxt.size());
3581 GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fCopyPrograms[progIdx].fProgram,
3582 GR_GL_FRAGMENT_SHADER, &str, &length, 1,
Ethan Nicholas941e7e22016-12-12 15:33:30 -05003583 &fStats, settings, &inputs);
3584 SkASSERT(inputs.isEmpty());
cdaltone2e71c22016-04-07 18:13:29 -07003585
3586 GL_CALL(LinkProgram(fCopyPrograms[progIdx].fProgram));
3587
3588 GL_CALL_RET(fCopyPrograms[progIdx].fTextureUniform,
3589 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_texture"));
3590 GL_CALL_RET(fCopyPrograms[progIdx].fPosXformUniform,
3591 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_posXform"));
3592 GL_CALL_RET(fCopyPrograms[progIdx].fTexCoordXformUniform,
3593 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_texCoordXform"));
3594
3595 GL_CALL(BindAttribLocation(fCopyPrograms[progIdx].fProgram, 0, "a_vertex"));
3596
3597 GL_CALL(DeleteShader(vshader));
3598 GL_CALL(DeleteShader(fshader));
3599
3600 return true;
bsalomon6df86402015-06-01 10:41:49 -07003601}
3602
brianosman33f6b3f2016-06-02 05:49:21 -07003603bool GrGLGpu::createMipmapProgram(int progIdx) {
3604 const bool oddWidth = SkToBool(progIdx & 0x2);
3605 const bool oddHeight = SkToBool(progIdx & 0x1);
3606 const int numTaps = (oddWidth ? 2 : 1) * (oddHeight ? 2 : 1);
3607
Brian Salomon1edc5b92016-11-29 13:43:46 -05003608 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
brianosman33f6b3f2016-06-02 05:49:21 -07003609
3610 SkASSERT(!fMipmapPrograms[progIdx].fProgram);
3611 GL_CALL_RET(fMipmapPrograms[progIdx].fProgram, CreateProgram());
3612 if (!fMipmapPrograms[progIdx].fProgram) {
3613 return false;
3614 }
3615
Brian Salomon1edc5b92016-11-29 13:43:46 -05003616 const char* version = shaderCaps->versionDeclString();
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003617 GrShaderVar aVertex("a_vertex", kHalf2_GrSLType, GrShaderVar::kIn_TypeModifier);
3618 GrShaderVar uTexCoordXform("u_texCoordXform", kHalf4_GrSLType,
Brian Salomon99938a82016-11-21 13:41:08 -05003619 GrShaderVar::kUniform_TypeModifier);
3620 GrShaderVar uTexture("u_texture", kTexture2DSampler_GrSLType,
3621 GrShaderVar::kUniform_TypeModifier);
brianosman33f6b3f2016-06-02 05:49:21 -07003622 // We need 1, 2, or 4 texture coordinates (depending on parity of each dimension):
Brian Salomon99938a82016-11-21 13:41:08 -05003623 GrShaderVar vTexCoords[] = {
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003624 GrShaderVar("v_texCoord0", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier),
3625 GrShaderVar("v_texCoord1", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier),
3626 GrShaderVar("v_texCoord2", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier),
3627 GrShaderVar("v_texCoord3", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier),
brianosman33f6b3f2016-06-02 05:49:21 -07003628 };
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003629 GrShaderVar oFragColor("o_FragColor", kHalf4_GrSLType,GrShaderVar::kOut_TypeModifier);
brianosman33f6b3f2016-06-02 05:49:21 -07003630
3631 SkString vshaderTxt(version);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003632 if (shaderCaps->noperspectiveInterpolationSupport()) {
3633 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
brianosman33f6b3f2016-06-02 05:49:21 -07003634 vshaderTxt.appendf("#extension %s : require\n", extension);
3635 }
3636 vTexCoords[0].addModifier("noperspective");
3637 vTexCoords[1].addModifier("noperspective");
3638 vTexCoords[2].addModifier("noperspective");
3639 vTexCoords[3].addModifier("noperspective");
3640 }
3641
Brian Salomon1edc5b92016-11-29 13:43:46 -05003642 aVertex.appendDecl(shaderCaps, &vshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003643 vshaderTxt.append(";");
Brian Salomon1edc5b92016-11-29 13:43:46 -05003644 uTexCoordXform.appendDecl(shaderCaps, &vshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003645 vshaderTxt.append(";");
3646 for (int i = 0; i < numTaps; ++i) {
Brian Salomon1edc5b92016-11-29 13:43:46 -05003647 vTexCoords[i].appendDecl(shaderCaps, &vshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003648 vshaderTxt.append(";");
3649 }
3650
3651 vshaderTxt.append(
3652 "// Mipmap Program VS\n"
3653 "void main() {"
Ethan Nicholasbed683a2017-09-26 14:23:59 -04003654 " sk_Position.xy = a_vertex * half2(2, 2) - half2(1, 1);"
3655 " sk_Position.zw = half2(0, 1);"
brianosman33f6b3f2016-06-02 05:49:21 -07003656 );
3657
3658 // Insert texture coordinate computation:
3659 if (oddWidth && oddHeight) {
3660 vshaderTxt.append(
3661 " v_texCoord0 = a_vertex.xy * u_texCoordXform.yw;"
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003662 " v_texCoord1 = a_vertex.xy * u_texCoordXform.yw + half2(u_texCoordXform.x, 0);"
3663 " v_texCoord2 = a_vertex.xy * u_texCoordXform.yw + half2(0, u_texCoordXform.z);"
brianosman33f6b3f2016-06-02 05:49:21 -07003664 " v_texCoord3 = a_vertex.xy * u_texCoordXform.yw + u_texCoordXform.xz;"
3665 );
3666 } else if (oddWidth) {
3667 vshaderTxt.append(
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003668 " v_texCoord0 = a_vertex.xy * half2(u_texCoordXform.y, 1);"
3669 " v_texCoord1 = a_vertex.xy * half2(u_texCoordXform.y, 1) + half2(u_texCoordXform.x, 0);"
brianosman33f6b3f2016-06-02 05:49:21 -07003670 );
3671 } else if (oddHeight) {
3672 vshaderTxt.append(
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003673 " v_texCoord0 = a_vertex.xy * half2(1, u_texCoordXform.w);"
3674 " v_texCoord1 = a_vertex.xy * half2(1, u_texCoordXform.w) + half2(0, u_texCoordXform.z);"
brianosman33f6b3f2016-06-02 05:49:21 -07003675 );
3676 } else {
3677 vshaderTxt.append(
3678 " v_texCoord0 = a_vertex.xy;"
3679 );
3680 }
3681
3682 vshaderTxt.append("}");
3683
3684 SkString fshaderTxt(version);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003685 if (shaderCaps->noperspectiveInterpolationSupport()) {
3686 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
brianosman33f6b3f2016-06-02 05:49:21 -07003687 fshaderTxt.appendf("#extension %s : require\n", extension);
3688 }
3689 }
brianosman33f6b3f2016-06-02 05:49:21 -07003690 for (int i = 0; i < numTaps; ++i) {
Brian Salomonf31ae492016-11-18 15:35:33 -05003691 vTexCoords[i].setTypeModifier(GrShaderVar::kIn_TypeModifier);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003692 vTexCoords[i].appendDecl(shaderCaps, &fshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003693 fshaderTxt.append(";");
3694 }
Brian Salomon1edc5b92016-11-29 13:43:46 -05003695 uTexture.appendDecl(shaderCaps, &fshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003696 fshaderTxt.append(";");
brianosman33f6b3f2016-06-02 05:49:21 -07003697 fshaderTxt.append(
3698 "// Mipmap Program FS\n"
3699 "void main() {"
3700 );
3701
3702 if (oddWidth && oddHeight) {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003703 fshaderTxt.append(
3704 " sk_FragColor = (texture(u_texture, v_texCoord0) + "
3705 " texture(u_texture, v_texCoord1) + "
3706 " texture(u_texture, v_texCoord2) + "
3707 " texture(u_texture, v_texCoord3)) * 0.25;"
brianosman33f6b3f2016-06-02 05:49:21 -07003708 );
3709 } else if (oddWidth || oddHeight) {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003710 fshaderTxt.append(
3711 " sk_FragColor = (texture(u_texture, v_texCoord0) + "
3712 " texture(u_texture, v_texCoord1)) * 0.5;"
brianosman33f6b3f2016-06-02 05:49:21 -07003713 );
3714 } else {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003715 fshaderTxt.append(
3716 " sk_FragColor = texture(u_texture, v_texCoord0);"
brianosman33f6b3f2016-06-02 05:49:21 -07003717 );
3718 }
3719
3720 fshaderTxt.append("}");
3721
3722 const char* str;
3723 GrGLint length;
3724
3725 str = vshaderTxt.c_str();
3726 length = SkToInt(vshaderTxt.size());
Ethan Nicholas941e7e22016-12-12 15:33:30 -05003727 SkSL::Program::Settings settings;
3728 settings.fCaps = shaderCaps;
3729 SkSL::Program::Inputs inputs;
brianosman33f6b3f2016-06-02 05:49:21 -07003730 GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fMipmapPrograms[progIdx].fProgram,
3731 GR_GL_VERTEX_SHADER, &str, &length, 1,
Ethan Nicholas941e7e22016-12-12 15:33:30 -05003732 &fStats, settings, &inputs);
3733 SkASSERT(inputs.isEmpty());
brianosman33f6b3f2016-06-02 05:49:21 -07003734
3735 str = fshaderTxt.c_str();
3736 length = SkToInt(fshaderTxt.size());
3737 GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fMipmapPrograms[progIdx].fProgram,
3738 GR_GL_FRAGMENT_SHADER, &str, &length, 1,
Ethan Nicholas941e7e22016-12-12 15:33:30 -05003739 &fStats, settings, &inputs);
3740 SkASSERT(inputs.isEmpty());
brianosman33f6b3f2016-06-02 05:49:21 -07003741
3742 GL_CALL(LinkProgram(fMipmapPrograms[progIdx].fProgram));
3743
3744 GL_CALL_RET(fMipmapPrograms[progIdx].fTextureUniform,
3745 GetUniformLocation(fMipmapPrograms[progIdx].fProgram, "u_texture"));
3746 GL_CALL_RET(fMipmapPrograms[progIdx].fTexCoordXformUniform,
3747 GetUniformLocation(fMipmapPrograms[progIdx].fProgram, "u_texCoordXform"));
3748
3749 GL_CALL(BindAttribLocation(fMipmapPrograms[progIdx].fProgram, 0, "a_vertex"));
3750
3751 GL_CALL(DeleteShader(vshader));
3752 GL_CALL(DeleteShader(fshader));
3753
3754 return true;
3755}
3756
Mike Klein31550db2017-06-06 23:29:53 +00003757bool GrGLGpu::createStencilClipClearProgram() {
Brian Osman39c08ac2017-07-26 09:36:09 -04003758 TRACE_EVENT0("skia", TRACE_FUNC);
Ryan Macnak38a10ad2017-07-10 10:36:34 -07003759
Mike Klein31550db2017-06-06 23:29:53 +00003760 if (!fStencilClipClearArrayBuffer) {
3761 static const GrGLfloat vdata[] = {-1, -1, 1, -1, -1, 1, 1, 1};
3762 fStencilClipClearArrayBuffer.reset(GrGLBuffer::Create(
3763 this, sizeof(vdata), kVertex_GrBufferType, kStatic_GrAccessPattern, vdata));
3764 if (!fStencilClipClearArrayBuffer) {
cdaltone2e71c22016-04-07 18:13:29 -07003765 return false;
3766 }
3767 }
3768
Mike Klein31550db2017-06-06 23:29:53 +00003769 SkASSERT(!fStencilClipClearProgram);
3770 GL_CALL_RET(fStencilClipClearProgram, CreateProgram());
3771 if (!fStencilClipClearProgram) {
cdaltone2e71c22016-04-07 18:13:29 -07003772 return false;
3773 }
3774
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003775 GrShaderVar aVertex("a_vertex", kHalf2_GrSLType, GrShaderVar::kIn_TypeModifier);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003776 const char* version = this->caps()->shaderCaps()->versionDeclString();
bsalomon6dea83f2015-12-03 12:58:06 -08003777
bsalomon6dea83f2015-12-03 12:58:06 -08003778 SkString vshaderTxt(version);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003779 aVertex.appendDecl(this->caps()->shaderCaps(), &vshaderTxt);
bsalomon6dea83f2015-12-03 12:58:06 -08003780 vshaderTxt.append(";");
bsalomon6dea83f2015-12-03 12:58:06 -08003781 vshaderTxt.append(
Mike Klein31550db2017-06-06 23:29:53 +00003782 "// Stencil Clip Clear Program VS\n"
3783 "void main() {"
Ethan Nicholasbed683a2017-09-26 14:23:59 -04003784 " sk_Position = float4(a_vertex.x, a_vertex.y, 0, 1);"
Mike Klein31550db2017-06-06 23:29:53 +00003785 "}");
bsalomon6dea83f2015-12-03 12:58:06 -08003786
3787 SkString fshaderTxt(version);
bsalomon6dea83f2015-12-03 12:58:06 -08003788 fshaderTxt.appendf(
Mike Klein31550db2017-06-06 23:29:53 +00003789 "// Stencil Clip Clear Program FS\n"
3790 "void main() {"
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003791 " sk_FragColor = half4(0);"
Mike Klein31550db2017-06-06 23:29:53 +00003792 "}");
bsalomon6dea83f2015-12-03 12:58:06 -08003793
bsalomon6dea83f2015-12-03 12:58:06 -08003794 const char* str;
3795 GrGLint length;
3796
3797 str = vshaderTxt.c_str();
3798 length = SkToInt(vshaderTxt.size());
Ethan Nicholas941e7e22016-12-12 15:33:30 -05003799 SkSL::Program::Settings settings;
3800 settings.fCaps = this->caps()->shaderCaps();
3801 SkSL::Program::Inputs inputs;
Mike Klein31550db2017-06-06 23:29:53 +00003802 GrGLuint vshader =
3803 GrGLCompileAndAttachShader(*fGLContext, fStencilClipClearProgram, GR_GL_VERTEX_SHADER,
3804 &str, &length, 1, &fStats, settings, &inputs);
Ethan Nicholas941e7e22016-12-12 15:33:30 -05003805 SkASSERT(inputs.isEmpty());
bsalomon6dea83f2015-12-03 12:58:06 -08003806
3807 str = fshaderTxt.c_str();
3808 length = SkToInt(fshaderTxt.size());
Mike Klein31550db2017-06-06 23:29:53 +00003809 GrGLuint fshader =
3810 GrGLCompileAndAttachShader(*fGLContext, fStencilClipClearProgram, GR_GL_FRAGMENT_SHADER,
3811 &str, &length, 1, &fStats, settings, &inputs);
Ethan Nicholas941e7e22016-12-12 15:33:30 -05003812 SkASSERT(inputs.isEmpty());
bsalomon6dea83f2015-12-03 12:58:06 -08003813
Mike Klein31550db2017-06-06 23:29:53 +00003814 GL_CALL(LinkProgram(fStencilClipClearProgram));
bsalomon6dea83f2015-12-03 12:58:06 -08003815
Mike Klein31550db2017-06-06 23:29:53 +00003816 GL_CALL(BindAttribLocation(fStencilClipClearProgram, 0, "a_vertex"));
bsalomon6dea83f2015-12-03 12:58:06 -08003817
3818 GL_CALL(DeleteShader(vshader));
3819 GL_CALL(DeleteShader(fshader));
cdaltone2e71c22016-04-07 18:13:29 -07003820
3821 return true;
bsalomon6dea83f2015-12-03 12:58:06 -08003822}
3823
Mike Klein31550db2017-06-06 23:29:53 +00003824void GrGLGpu::clearStencilClipAsDraw(const GrFixedClip& clip, bool insideStencilMask,
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003825 GrRenderTarget* rt, GrSurfaceOrigin origin) {
bsalomon7f9b2e42016-01-12 13:29:26 -08003826 // TODO: This should swizzle the output to match dst's config, though it is a debugging
3827 // visualization.
3828
bsalomon6dea83f2015-12-03 12:58:06 -08003829 this->handleDirtyContext();
Mike Klein31550db2017-06-06 23:29:53 +00003830 if (!fStencilClipClearProgram) {
3831 if (!this->createStencilClipClearProgram()) {
3832 SkDebugf("Failed to create stencil clip clear program.\n");
cdaltone2e71c22016-04-07 18:13:29 -07003833 return;
3834 }
bsalomon6dea83f2015-12-03 12:58:06 -08003835 }
3836
Mike Klein8b06ed72017-06-06 21:47:32 +00003837 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(rt->asRenderTarget());
Mike Klein31550db2017-06-06 23:29:53 +00003838 this->flushRenderTarget(glRT, nullptr);
Mike Klein8b06ed72017-06-06 21:47:32 +00003839
Mike Klein31550db2017-06-06 23:29:53 +00003840 GL_CALL(UseProgram(fStencilClipClearProgram));
3841 fHWProgramID = fStencilClipClearProgram;
bsalomon6dea83f2015-12-03 12:58:06 -08003842
cdaltone2e71c22016-04-07 18:13:29 -07003843 fHWVertexArrayState.setVertexArrayID(this, 0);
bsalomon6dea83f2015-12-03 12:58:06 -08003844
cdaltone2e71c22016-04-07 18:13:29 -07003845 GrGLAttribArrayState* attribs = fHWVertexArrayState.bindInternalVertexArray(this);
Chris Dalton8e45b4f2017-05-05 14:00:56 -04003846 attribs->enableVertexArrays(this, 1);
Ethan Nicholasfa7ee242017-09-25 09:52:04 -04003847 attribs->set(this, 0, fStencilClipClearArrayBuffer.get(), kHalf2_GrVertexAttribType,
Hal Canary144caf52016-11-07 17:57:18 -05003848 2 * sizeof(GrGLfloat), 0);
bsalomon6dea83f2015-12-03 12:58:06 -08003849
bsalomon6dea83f2015-12-03 12:58:06 -08003850 GrXferProcessor::BlendInfo blendInfo;
3851 blendInfo.reset();
bsalomon7f9b2e42016-01-12 13:29:26 -08003852 this->flushBlend(blendInfo, GrSwizzle::RGBA());
Mike Klein31550db2017-06-06 23:29:53 +00003853 this->flushColorWrite(false);
cdaltonaf8bc7d2016-02-05 09:35:20 -08003854 this->flushHWAAState(glRT, false, false);
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003855 this->flushScissor(clip.scissorState(), glRT->getViewport(), origin);
3856 this->flushWindowRectangles(clip.windowRectsState(), glRT, origin);
Mike Klein31550db2017-06-06 23:29:53 +00003857 GrStencilAttachment* sb = rt->renderTargetPriv().getStencilAttachment();
3858 // This should only be called internally when we know we have a stencil buffer.
3859 SkASSERT(sb);
3860 GrStencilSettings settings = GrStencilSettings(
3861 *GrStencilSettings::SetClipBitSettings(insideStencilMask), false, sb->bits());
3862 this->flushStencil(settings);
3863 GL_CALL(DrawArrays(GR_GL_TRIANGLE_STRIP, 0, 4));
bsalomon6dea83f2015-12-03 12:58:06 -08003864}
3865
Brian Salomon43f8bf02017-10-18 08:33:29 -04003866bool GrGLGpu::createClearColorProgram() {
3867 TRACE_EVENT0("skia", TRACE_FUNC);
3868
3869 if (!fClearProgramArrayBuffer) {
3870 static const GrGLfloat vdata[] = {-1, -1, 1, -1, -1, 1, 1, 1};
3871 fClearProgramArrayBuffer.reset(GrGLBuffer::Create(this, sizeof(vdata), kVertex_GrBufferType,
3872 kStatic_GrAccessPattern, vdata));
3873 if (!fClearProgramArrayBuffer) {
3874 return false;
3875 }
3876 }
3877
3878 SkASSERT(!fClearColorProgram.fProgram);
3879 GL_CALL_RET(fClearColorProgram.fProgram, CreateProgram());
3880 if (!fClearColorProgram.fProgram) {
3881 return false;
3882 }
3883
3884 GrShaderVar aVertex("a_vertex", kHalf2_GrSLType, GrShaderVar::kIn_TypeModifier);
3885 const char* version = this->caps()->shaderCaps()->versionDeclString();
3886
3887 SkString vshaderTxt(version);
3888 aVertex.appendDecl(this->caps()->shaderCaps(), &vshaderTxt);
3889 vshaderTxt.append(";");
3890 vshaderTxt.append(R"(
3891 // Clear Color Program VS
3892 void main() {
3893 sk_Position = float4(a_vertex.x, a_vertex.y, 0, 1);
3894 })");
3895
3896 GrShaderVar uColor("u_color", kHalf4_GrSLType, GrShaderVar::kUniform_TypeModifier);
3897 SkString fshaderTxt(version);
3898 uColor.appendDecl(this->caps()->shaderCaps(), &fshaderTxt);
3899 fshaderTxt.append(";");
3900 fshaderTxt.appendf(R"(
3901 // Clear Color Program FS
3902 void main() {
3903 sk_FragColor = u_color;
3904 })");
3905
3906 const char* str;
3907 GrGLint length;
3908
3909 str = vshaderTxt.c_str();
3910 length = SkToInt(vshaderTxt.size());
3911 SkSL::Program::Settings settings;
3912 settings.fCaps = this->caps()->shaderCaps();
3913 SkSL::Program::Inputs inputs;
3914 GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fClearColorProgram.fProgram,
3915 GR_GL_VERTEX_SHADER, &str, &length, 1, &fStats,
3916 settings, &inputs);
3917 SkASSERT(inputs.isEmpty());
3918
3919 str = fshaderTxt.c_str();
3920 length = SkToInt(fshaderTxt.size());
3921 GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fClearColorProgram.fProgram,
3922 GR_GL_FRAGMENT_SHADER, &str, &length, 1, &fStats,
3923 settings, &inputs);
3924 SkASSERT(inputs.isEmpty());
3925
3926 GL_CALL(LinkProgram(fClearColorProgram.fProgram));
3927
3928 GL_CALL(BindAttribLocation(fClearColorProgram.fProgram, 0, "a_vertex"));
3929
3930 GL_CALL_RET(fClearColorProgram.fColorUniform,
3931 GetUniformLocation(fClearColorProgram.fProgram, "u_color"));
3932
3933 GL_CALL(DeleteShader(vshader));
3934 GL_CALL(DeleteShader(fshader));
3935
3936 return true;
3937}
3938
3939void GrGLGpu::clearColorAsDraw(const GrFixedClip& clip, GrGLfloat r, GrGLfloat g, GrGLfloat b,
3940 GrGLfloat a, GrRenderTarget* dst, GrSurfaceOrigin origin) {
3941 if (!fClearColorProgram.fProgram) {
3942 if (!this->createClearColorProgram()) {
3943 SkDebugf("Failed to create clear color program.\n");
3944 return;
3945 }
3946 }
3947
3948 GrGLIRect dstVP;
3949 this->bindSurfaceFBOForPixelOps(dst, GR_GL_FRAMEBUFFER, &dstVP, kDst_TempFBOTarget);
3950 this->flushViewport(dstVP);
3951 fHWBoundRenderTargetUniqueID.makeInvalid();
3952
3953 GL_CALL(UseProgram(fClearColorProgram.fProgram));
3954 fHWProgramID = fClearColorProgram.fProgram;
3955
3956 fHWVertexArrayState.setVertexArrayID(this, 0);
3957
3958 GrGLAttribArrayState* attribs = fHWVertexArrayState.bindInternalVertexArray(this);
3959 attribs->enableVertexArrays(this, 1);
3960 attribs->set(this, 0, fClearProgramArrayBuffer.get(), kHalf2_GrVertexAttribType,
3961 2 * sizeof(GrGLfloat), 0);
3962
3963 GrGLRenderTarget* glrt = static_cast<GrGLRenderTarget*>(dst);
3964 this->flushScissor(clip.scissorState(), glrt->getViewport(), origin);
3965 this->flushWindowRectangles(clip.windowRectsState(), glrt, origin);
3966
3967 GL_CALL(Uniform4f(fClearColorProgram.fColorUniform, r, g, b, a));
3968
3969 GrXferProcessor::BlendInfo blendInfo;
3970 blendInfo.reset();
3971 this->flushBlend(blendInfo, GrSwizzle::RGBA());
3972 this->flushColorWrite(true);
3973 this->flushHWAAState(nullptr, false, false);
3974 this->disableStencil();
3975 if (this->glCaps().srgbWriteControl()) {
3976 this->flushFramebufferSRGB(true);
3977 }
3978
3979 GL_CALL(DrawArrays(GR_GL_TRIANGLE_STRIP, 0, 4));
3980 this->unbindTextureFBOForPixelOps(GR_GL_FRAMEBUFFER, dst);
Brian Salomon62cbb672017-10-18 15:10:07 -04003981 this->didWriteToSurface(dst, clip.scissorEnabled() ? &clip.scissorRect() : nullptr);
Brian Salomon43f8bf02017-10-18 08:33:29 -04003982}
bsalomon6dea83f2015-12-03 12:58:06 -08003983
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003984bool GrGLGpu::copySurfaceAsDraw(GrSurface* dst, GrSurfaceOrigin dstOrigin,
3985 GrSurface* src, GrSurfaceOrigin srcOrigin,
bsalomon6df86402015-06-01 10:41:49 -07003986 const SkIRect& srcRect,
3987 const SkIPoint& dstPoint) {
cdaltone2e71c22016-04-07 18:13:29 -07003988 GrGLTexture* srcTex = static_cast<GrGLTexture*>(src->asTexture());
Brian Salomonbf7b6202016-11-11 16:08:03 -05003989 int progIdx = TextureToCopyProgramIdx(srcTex);
cdaltone2e71c22016-04-07 18:13:29 -07003990
3991 if (!fCopyPrograms[progIdx].fProgram) {
Brian Salomonbf7b6202016-11-11 16:08:03 -05003992 if (!this->createCopyProgram(srcTex)) {
cdaltone2e71c22016-04-07 18:13:29 -07003993 SkDebugf("Failed to create copy program.\n");
3994 return false;
3995 }
3996 }
3997
bsalomon6df86402015-06-01 10:41:49 -07003998 int w = srcRect.width();
3999 int h = srcRect.height();
4000
Brian Salomon2bbdcc42017-09-07 12:36:34 -04004001 this->bindTexture(0, GrSamplerState::ClampNearest(), true, srcTex, srcOrigin);
bsalomon6df86402015-06-01 10:41:49 -07004002
bsalomon083617b2016-02-12 12:10:14 -08004003 GrGLIRect dstVP;
Brian Salomon71d9d842016-11-03 13:42:00 -04004004 this->bindSurfaceFBOForPixelOps(dst, GR_GL_FRAMEBUFFER, &dstVP, kDst_TempFBOTarget);
bsalomon083617b2016-02-12 12:10:14 -08004005 this->flushViewport(dstVP);
Robert Phillips294870f2016-11-11 12:38:40 -05004006 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon083617b2016-02-12 12:10:14 -08004007
bsalomon6df86402015-06-01 10:41:49 -07004008 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY, w, h);
bsalomon6df86402015-06-01 10:41:49 -07004009
bsalomon7ea33f52015-11-22 14:51:00 -08004010 GL_CALL(UseProgram(fCopyPrograms[progIdx].fProgram));
4011 fHWProgramID = fCopyPrograms[progIdx].fProgram;
bsalomon6df86402015-06-01 10:41:49 -07004012
cdaltone2e71c22016-04-07 18:13:29 -07004013 fHWVertexArrayState.setVertexArrayID(this, 0);
bsalomon6df86402015-06-01 10:41:49 -07004014
cdaltone2e71c22016-04-07 18:13:29 -07004015 GrGLAttribArrayState* attribs = fHWVertexArrayState.bindInternalVertexArray(this);
Chris Dalton8e45b4f2017-05-05 14:00:56 -04004016 attribs->enableVertexArrays(this, 1);
Ethan Nicholasfa7ee242017-09-25 09:52:04 -04004017 attribs->set(this, 0, fCopyProgramArrayBuffer.get(), kHalf2_GrVertexAttribType,
Hal Canary144caf52016-11-07 17:57:18 -05004018 2 * sizeof(GrGLfloat), 0);
bsalomon6df86402015-06-01 10:41:49 -07004019
4020 // dst rect edges in NDC (-1 to 1)
4021 int dw = dst->width();
4022 int dh = dst->height();
4023 GrGLfloat dx0 = 2.f * dstPoint.fX / dw - 1.f;
4024 GrGLfloat dx1 = 2.f * (dstPoint.fX + w) / dw - 1.f;
4025 GrGLfloat dy0 = 2.f * dstPoint.fY / dh - 1.f;
4026 GrGLfloat dy1 = 2.f * (dstPoint.fY + h) / dh - 1.f;
Robert Phillipsb0e93a22017-08-29 08:26:54 -04004027 if (kBottomLeft_GrSurfaceOrigin == dstOrigin) {
bsalomon6df86402015-06-01 10:41:49 -07004028 dy0 = -dy0;
4029 dy1 = -dy1;
4030 }
4031
bsalomone5286e02016-01-14 09:24:09 -08004032 GrGLfloat sx0 = (GrGLfloat)srcRect.fLeft;
4033 GrGLfloat sx1 = (GrGLfloat)(srcRect.fLeft + w);
4034 GrGLfloat sy0 = (GrGLfloat)srcRect.fTop;
4035 GrGLfloat sy1 = (GrGLfloat)(srcRect.fTop + h);
Ethan Nicholas5338f992017-04-19 15:54:07 -04004036 int sw = src->width();
bsalomon6df86402015-06-01 10:41:49 -07004037 int sh = src->height();
Robert Phillipsb0e93a22017-08-29 08:26:54 -04004038 if (kBottomLeft_GrSurfaceOrigin == srcOrigin) {
bsalomone5286e02016-01-14 09:24:09 -08004039 sy0 = sh - sy0;
4040 sy1 = sh - sy1;
4041 }
Ethan Nicholas5338f992017-04-19 15:54:07 -04004042 // src rect edges in normalized texture space (0 to 1)
4043 sx0 /= sw;
4044 sx1 /= sw;
4045 sy0 /= sh;
4046 sy1 /= sh;
bsalomon6df86402015-06-01 10:41:49 -07004047
bsalomon7ea33f52015-11-22 14:51:00 -08004048 GL_CALL(Uniform4f(fCopyPrograms[progIdx].fPosXformUniform, dx1 - dx0, dy1 - dy0, dx0, dy0));
4049 GL_CALL(Uniform4f(fCopyPrograms[progIdx].fTexCoordXformUniform,
4050 sx1 - sx0, sy1 - sy0, sx0, sy0));
4051 GL_CALL(Uniform1i(fCopyPrograms[progIdx].fTextureUniform, 0));
bsalomon6df86402015-06-01 10:41:49 -07004052
4053 GrXferProcessor::BlendInfo blendInfo;
4054 blendInfo.reset();
bsalomon7f9b2e42016-01-12 13:29:26 -08004055 this->flushBlend(blendInfo, GrSwizzle::RGBA());
bsalomon6df86402015-06-01 10:41:49 -07004056 this->flushColorWrite(true);
bsalomon083617b2016-02-12 12:10:14 -08004057 this->flushHWAAState(nullptr, false, false);
bsalomon6df86402015-06-01 10:41:49 -07004058 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07004059 this->disableWindowRectangles();
csmartdaltonc7d85332016-10-26 10:13:46 -07004060 this->disableStencil();
Brian Osmanf02fa6f2017-07-11 11:17:47 -04004061 if (this->glCaps().srgbWriteControl()) {
4062 this->flushFramebufferSRGB(true);
4063 }
bsalomon6df86402015-06-01 10:41:49 -07004064
4065 GL_CALL(DrawArrays(GR_GL_TRIANGLE_STRIP, 0, 4));
Brian Salomon71d9d842016-11-03 13:42:00 -04004066 this->unbindTextureFBOForPixelOps(GR_GL_FRAMEBUFFER, dst);
bsalomon083617b2016-02-12 12:10:14 -08004067 this->didWriteToSurface(dst, &dstRect);
4068
cdaltone2e71c22016-04-07 18:13:29 -07004069 return true;
bsalomon6df86402015-06-01 10:41:49 -07004070}
4071
Robert Phillipsb0e93a22017-08-29 08:26:54 -04004072void GrGLGpu::copySurfaceAsCopyTexSubImage(GrSurface* dst, GrSurfaceOrigin dstOrigin,
4073 GrSurface* src, GrSurfaceOrigin srcOrigin,
bsalomon6df86402015-06-01 10:41:49 -07004074 const SkIRect& srcRect,
4075 const SkIPoint& dstPoint) {
Robert Phillipsb0e93a22017-08-29 08:26:54 -04004076 SkASSERT(can_copy_texsubimage(dst, dstOrigin, src, srcOrigin, this));
bsalomon6df86402015-06-01 10:41:49 -07004077 GrGLIRect srcVP;
Brian Salomon71d9d842016-11-03 13:42:00 -04004078 this->bindSurfaceFBOForPixelOps(src, GR_GL_FRAMEBUFFER, &srcVP, kSrc_TempFBOTarget);
bsalomon083617b2016-02-12 12:10:14 -08004079 GrGLTexture* dstTex = static_cast<GrGLTexture *>(dst->asTexture());
bsalomon6df86402015-06-01 10:41:49 -07004080 SkASSERT(dstTex);
4081 // We modified the bound FBO
Robert Phillips294870f2016-11-11 12:38:40 -05004082 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon6df86402015-06-01 10:41:49 -07004083 GrGLIRect srcGLRect;
Robert Phillipsb0e93a22017-08-29 08:26:54 -04004084 srcGLRect.setRelativeTo(srcVP, srcRect, srcOrigin);
bsalomon6df86402015-06-01 10:41:49 -07004085
4086 this->setScratchTextureUnit();
bsalomon10528f12015-10-14 12:54:52 -07004087 GL_CALL(BindTexture(dstTex->target(), dstTex->textureID()));
bsalomon6df86402015-06-01 10:41:49 -07004088 GrGLint dstY;
Robert Phillipsb0e93a22017-08-29 08:26:54 -04004089 if (kBottomLeft_GrSurfaceOrigin == dstOrigin) {
bsalomon6df86402015-06-01 10:41:49 -07004090 dstY = dst->height() - (dstPoint.fY + srcGLRect.fHeight);
4091 } else {
4092 dstY = dstPoint.fY;
4093 }
bsalomon10528f12015-10-14 12:54:52 -07004094 GL_CALL(CopyTexSubImage2D(dstTex->target(), 0,
bsalomon083617b2016-02-12 12:10:14 -08004095 dstPoint.fX, dstY,
4096 srcGLRect.fLeft, srcGLRect.fBottom,
4097 srcGLRect.fWidth, srcGLRect.fHeight));
Brian Salomon71d9d842016-11-03 13:42:00 -04004098 this->unbindTextureFBOForPixelOps(GR_GL_FRAMEBUFFER, src);
bsalomon083617b2016-02-12 12:10:14 -08004099 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
4100 srcRect.width(), srcRect.height());
4101 this->didWriteToSurface(dst, &dstRect);
bsalomon6df86402015-06-01 10:41:49 -07004102}
4103
Robert Phillipsb0e93a22017-08-29 08:26:54 -04004104bool GrGLGpu::copySurfaceAsBlitFramebuffer(GrSurface* dst, GrSurfaceOrigin dstOrigin,
4105 GrSurface* src, GrSurfaceOrigin srcOrigin,
bsalomon6df86402015-06-01 10:41:49 -07004106 const SkIRect& srcRect,
4107 const SkIPoint& dstPoint) {
Robert Phillipsb0e93a22017-08-29 08:26:54 -04004108 SkASSERT(can_blit_framebuffer_for_copy_surface(dst, dstOrigin, src, srcOrigin,
4109 srcRect, dstPoint, this));
bsalomon6df86402015-06-01 10:41:49 -07004110 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
4111 srcRect.width(), srcRect.height());
4112 if (dst == src) {
4113 if (SkIRect::IntersectsNoEmptyCheck(dstRect, srcRect)) {
4114 return false;
mtklein404b3b22015-05-18 09:29:10 -07004115 }
bsalomon5df6fee2015-05-18 06:26:15 -07004116 }
bsalomon6df86402015-06-01 10:41:49 -07004117
bsalomon6df86402015-06-01 10:41:49 -07004118 GrGLIRect dstVP;
4119 GrGLIRect srcVP;
Brian Salomon71d9d842016-11-03 13:42:00 -04004120 this->bindSurfaceFBOForPixelOps(dst, GR_GL_DRAW_FRAMEBUFFER, &dstVP, kDst_TempFBOTarget);
4121 this->bindSurfaceFBOForPixelOps(src, GR_GL_READ_FRAMEBUFFER, &srcVP, kSrc_TempFBOTarget);
bsalomon6df86402015-06-01 10:41:49 -07004122 // We modified the bound FBO
Robert Phillips294870f2016-11-11 12:38:40 -05004123 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon6df86402015-06-01 10:41:49 -07004124 GrGLIRect srcGLRect;
4125 GrGLIRect dstGLRect;
Robert Phillipsb0e93a22017-08-29 08:26:54 -04004126 srcGLRect.setRelativeTo(srcVP, srcRect, srcOrigin);
4127 dstGLRect.setRelativeTo(dstVP, dstRect, dstOrigin);
bsalomon6df86402015-06-01 10:41:49 -07004128
4129 // BlitFrameBuffer respects the scissor, so disable it.
4130 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07004131 this->disableWindowRectangles();
bsalomon6df86402015-06-01 10:41:49 -07004132
4133 GrGLint srcY0;
4134 GrGLint srcY1;
4135 // Does the blit need to y-mirror or not?
Robert Phillipsb0e93a22017-08-29 08:26:54 -04004136 if (srcOrigin == dstOrigin) {
bsalomon6df86402015-06-01 10:41:49 -07004137 srcY0 = srcGLRect.fBottom;
4138 srcY1 = srcGLRect.fBottom + srcGLRect.fHeight;
4139 } else {
4140 srcY0 = srcGLRect.fBottom + srcGLRect.fHeight;
4141 srcY1 = srcGLRect.fBottom;
4142 }
4143 GL_CALL(BlitFramebuffer(srcGLRect.fLeft,
4144 srcY0,
4145 srcGLRect.fLeft + srcGLRect.fWidth,
4146 srcY1,
4147 dstGLRect.fLeft,
4148 dstGLRect.fBottom,
4149 dstGLRect.fLeft + dstGLRect.fWidth,
4150 dstGLRect.fBottom + dstGLRect.fHeight,
4151 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
Brian Salomon71d9d842016-11-03 13:42:00 -04004152 this->unbindTextureFBOForPixelOps(GR_GL_DRAW_FRAMEBUFFER, dst);
4153 this->unbindTextureFBOForPixelOps(GR_GL_READ_FRAMEBUFFER, src);
bsalomon083617b2016-02-12 12:10:14 -08004154 this->didWriteToSurface(dst, &dstRect);
bsalomon6df86402015-06-01 10:41:49 -07004155 return true;
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00004156}
4157
brianosman33f6b3f2016-06-02 05:49:21 -07004158// Manual implementation of mipmap generation, to work around driver bugs w/sRGB.
4159// Uses draw calls to do a series of downsample operations to successive mips.
4160// If this returns false, then the calling code falls back to using glGenerateMipmap.
Robert Phillipsb0e93a22017-08-29 08:26:54 -04004161bool GrGLGpu::generateMipmap(GrGLTexture* texture, GrSurfaceOrigin textureOrigin,
4162 bool gammaCorrect) {
Brian Salomonbf7b6202016-11-11 16:08:03 -05004163 SkASSERT(!GrPixelConfigIsSint(texture->config()));
brianosman09563ce2016-06-02 08:59:34 -07004164 // Our iterative downsample requires the ability to limit which level we're sampling:
4165 if (!this->glCaps().doManualMipmapping()) {
brianosman33f6b3f2016-06-02 05:49:21 -07004166 return false;
4167 }
4168
4169 // Mipmaps are only supported on 2D textures:
4170 if (GR_GL_TEXTURE_2D != texture->target()) {
4171 return false;
4172 }
4173
4174 // We need to be able to render to the texture for this to work:
Brian Salomon71d9d842016-11-03 13:42:00 -04004175 if (!this->glCaps().canConfigBeFBOColorAttachment(texture->config())) {
brianosman33f6b3f2016-06-02 05:49:21 -07004176 return false;
4177 }
4178
brianosman33f6b3f2016-06-02 05:49:21 -07004179 // If we're mipping an sRGB texture, we need to ensure FB sRGB is correct:
4180 if (GrPixelConfigIsSRGB(texture->config())) {
4181 // If we have write-control, just set the state that we want:
4182 if (this->glCaps().srgbWriteControl()) {
4183 this->flushFramebufferSRGB(gammaCorrect);
4184 } else if (!gammaCorrect) {
4185 // If we don't have write-control we can't do non-gamma-correct mipmapping:
4186 return false;
4187 }
4188 }
4189
4190 int width = texture->width();
4191 int height = texture->height();
4192 int levelCount = SkMipMap::ComputeLevelCount(width, height) + 1;
4193
4194 // Define all mips, if we haven't previously done so:
4195 if (0 == texture->texturePriv().maxMipMapLevel()) {
4196 GrGLenum internalFormat;
4197 GrGLenum externalFormat;
4198 GrGLenum externalType;
4199 if (!this->glCaps().getTexImageFormats(texture->config(), texture->config(),
4200 &internalFormat, &externalFormat, &externalType)) {
4201 return false;
4202 }
4203
Brian Osman9b560242017-09-05 15:34:52 -04004204 this->unbindCpuToGpuXferBuffer();
4205
brianosman33f6b3f2016-06-02 05:49:21 -07004206 for (GrGLint level = 1; level < levelCount; ++level) {
4207 // Define the next mip:
4208 width = SkTMax(1, width / 2);
4209 height = SkTMax(1, height / 2);
4210 GL_ALLOC_CALL(this->glInterface(), TexImage2D(GR_GL_TEXTURE_2D, level, internalFormat,
4211 width, height, 0,
4212 externalFormat, externalType, nullptr));
4213 }
4214 }
4215
4216 // Create (if necessary), then bind temporary FBO:
4217 if (0 == fTempDstFBOID) {
4218 GL_CALL(GenFramebuffers(1, &fTempDstFBOID));
4219 }
4220 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, fTempDstFBOID));
Robert Phillips294870f2016-11-11 12:38:40 -05004221 fHWBoundRenderTargetUniqueID.makeInvalid();
brianosman33f6b3f2016-06-02 05:49:21 -07004222
4223 // Bind the texture, to get things configured for filtering.
4224 // We'll be changing our base level further below:
4225 this->setTextureUnit(0);
Brian Salomon2bbdcc42017-09-07 12:36:34 -04004226 this->bindTexture(0, GrSamplerState::ClampBilerp(), gammaCorrect, texture, textureOrigin);
brianosman33f6b3f2016-06-02 05:49:21 -07004227
4228 // Vertex data:
4229 if (!fMipmapProgramArrayBuffer) {
4230 static const GrGLfloat vdata[] = {
4231 0, 0,
4232 0, 1,
4233 1, 0,
4234 1, 1
4235 };
4236 fMipmapProgramArrayBuffer.reset(GrGLBuffer::Create(this, sizeof(vdata),
4237 kVertex_GrBufferType,
4238 kStatic_GrAccessPattern, vdata));
4239 }
4240 if (!fMipmapProgramArrayBuffer) {
4241 return false;
4242 }
4243
4244 fHWVertexArrayState.setVertexArrayID(this, 0);
4245
4246 GrGLAttribArrayState* attribs = fHWVertexArrayState.bindInternalVertexArray(this);
Chris Dalton8e45b4f2017-05-05 14:00:56 -04004247 attribs->enableVertexArrays(this, 1);
Ethan Nicholasfa7ee242017-09-25 09:52:04 -04004248 attribs->set(this, 0, fMipmapProgramArrayBuffer.get(), kHalf2_GrVertexAttribType,
brianosman33f6b3f2016-06-02 05:49:21 -07004249 2 * sizeof(GrGLfloat), 0);
brianosman33f6b3f2016-06-02 05:49:21 -07004250
4251 // Set "simple" state once:
4252 GrXferProcessor::BlendInfo blendInfo;
4253 blendInfo.reset();
4254 this->flushBlend(blendInfo, GrSwizzle::RGBA());
4255 this->flushColorWrite(true);
brianosman33f6b3f2016-06-02 05:49:21 -07004256 this->flushHWAAState(nullptr, false, false);
4257 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07004258 this->disableWindowRectangles();
csmartdaltonc7d85332016-10-26 10:13:46 -07004259 this->disableStencil();
brianosman33f6b3f2016-06-02 05:49:21 -07004260
4261 // Do all the blits:
4262 width = texture->width();
4263 height = texture->height();
4264 GrGLIRect viewport;
4265 viewport.fLeft = 0;
4266 viewport.fBottom = 0;
4267 for (GrGLint level = 1; level < levelCount; ++level) {
4268 // Get and bind the program for this particular downsample (filter shape can vary):
4269 int progIdx = TextureSizeToMipmapProgramIdx(width, height);
4270 if (!fMipmapPrograms[progIdx].fProgram) {
4271 if (!this->createMipmapProgram(progIdx)) {
4272 SkDebugf("Failed to create mipmap program.\n");
4273 return false;
4274 }
4275 }
4276 GL_CALL(UseProgram(fMipmapPrograms[progIdx].fProgram));
4277 fHWProgramID = fMipmapPrograms[progIdx].fProgram;
4278
4279 // Texcoord uniform is expected to contain (1/w, (w-1)/w, 1/h, (h-1)/h)
4280 const float invWidth = 1.0f / width;
4281 const float invHeight = 1.0f / height;
4282 GL_CALL(Uniform4f(fMipmapPrograms[progIdx].fTexCoordXformUniform,
4283 invWidth, (width - 1) * invWidth, invHeight, (height - 1) * invHeight));
4284 GL_CALL(Uniform1i(fMipmapPrograms[progIdx].fTextureUniform, 0));
4285
4286 // Only sample from previous mip
4287 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_BASE_LEVEL, level - 1));
4288
4289 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
4290 GR_GL_TEXTURE_2D, texture->textureID(), level));
4291
4292 width = SkTMax(1, width / 2);
4293 height = SkTMax(1, height / 2);
4294 viewport.fWidth = width;
4295 viewport.fHeight = height;
4296 this->flushViewport(viewport);
4297
4298 GL_CALL(DrawArrays(GR_GL_TRIANGLE_STRIP, 0, 4));
4299 }
4300
4301 // Unbind:
4302 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
4303 GR_GL_TEXTURE_2D, 0, 0));
4304
4305 return true;
4306}
4307
Robert Phillipsb0e93a22017-08-29 08:26:54 -04004308void GrGLGpu::onQueryMultisampleSpecs(GrRenderTarget* rt, GrSurfaceOrigin rtOrigin,
4309 const GrStencilSettings& stencil,
csmartdaltonc25c5d72016-11-01 07:03:59 -07004310 int* effectiveSampleCnt, SamplePattern* samplePattern) {
Brian Salomon7c8460e2017-05-12 11:36:10 -04004311 SkASSERT(GrFSAAType::kMixedSamples != rt->fsaaType() ||
4312 rt->renderTargetPriv().getStencilAttachment() || stencil.isDisabled());
cdalton28f45b92016-03-07 13:58:26 -08004313
4314 this->flushStencil(stencil);
4315 this->flushHWAAState(rt, true, !stencil.isDisabled());
4316 this->flushRenderTarget(static_cast<GrGLRenderTarget*>(rt), &SkIRect::EmptyIRect());
4317
4318 if (0 != this->caps()->maxRasterSamples()) {
4319 GR_GL_GetIntegerv(this->glInterface(), GR_GL_EFFECTIVE_RASTER_SAMPLES, effectiveSampleCnt);
4320 } else {
4321 GR_GL_GetIntegerv(this->glInterface(), GR_GL_SAMPLES, effectiveSampleCnt);
4322 }
4323
Brian Salomon154ce912017-05-17 10:40:02 -04004324 SkASSERT(*effectiveSampleCnt >= rt->numStencilSamples());
cdalton28f45b92016-03-07 13:58:26 -08004325
4326 if (this->caps()->sampleLocationsSupport()) {
csmartdalton0d28e572016-07-06 09:59:43 -07004327 samplePattern->reset(*effectiveSampleCnt);
cdalton28f45b92016-03-07 13:58:26 -08004328 for (int i = 0; i < *effectiveSampleCnt; ++i) {
4329 GrGLfloat pos[2];
4330 GL_CALL(GetMultisamplefv(GR_GL_SAMPLE_POSITION, i, pos));
Robert Phillipsb0e93a22017-08-29 08:26:54 -04004331 if (kTopLeft_GrSurfaceOrigin == rtOrigin) {
csmartdalton0d28e572016-07-06 09:59:43 -07004332 (*samplePattern)[i].set(pos[0], pos[1]);
cdalton28f45b92016-03-07 13:58:26 -08004333 } else {
csmartdalton0d28e572016-07-06 09:59:43 -07004334 (*samplePattern)[i].set(pos[0], 1 - pos[1]);
cdalton28f45b92016-03-07 13:58:26 -08004335 }
4336 }
4337 }
4338}
4339
cdalton231c5fd2015-05-13 12:35:36 -07004340void GrGLGpu::xferBarrier(GrRenderTarget* rt, GrXferBarrierType type) {
bsalomoncb02b382015-08-12 11:14:50 -07004341 SkASSERT(type);
cdalton9954bc32015-04-29 14:17:00 -07004342 switch (type) {
cdalton231c5fd2015-05-13 12:35:36 -07004343 case kTexture_GrXferBarrierType: {
4344 GrGLRenderTarget* glrt = static_cast<GrGLRenderTarget*>(rt);
4345 if (glrt->textureFBOID() != glrt->renderFBOID()) {
4346 // The render target uses separate storage so no need for glTextureBarrier.
4347 // FIXME: The render target will resolve automatically when its texture is bound,
4348 // but we could resolve only the bounds that will be read if we do it here instead.
4349 return;
4350 }
cdalton9954bc32015-04-29 14:17:00 -07004351 SkASSERT(this->caps()->textureBarrierSupport());
4352 GL_CALL(TextureBarrier());
4353 return;
cdalton231c5fd2015-05-13 12:35:36 -07004354 }
cdalton8917d622015-05-06 13:40:21 -07004355 case kBlend_GrXferBarrierType:
bsalomon4b91f762015-05-19 09:29:46 -07004356 SkASSERT(GrCaps::kAdvanced_BlendEquationSupport ==
cdalton8917d622015-05-06 13:40:21 -07004357 this->caps()->blendEquationSupport());
4358 GL_CALL(BlendBarrier());
4359 return;
bsalomoncb02b382015-08-12 11:14:50 -07004360 default: break; // placate compiler warnings that kNone not handled
cdalton9954bc32015-04-29 14:17:00 -07004361 }
4362}
4363
jvanverth88957922015-07-14 11:02:52 -07004364GrBackendObject GrGLGpu::createTestingOnlyBackendTexture(void* pixels, int w, int h,
Greg Daniel177e6952017-10-12 12:27:11 -04004365 GrPixelConfig config, bool /*isRT*/,
4366 GrMipMapped mipMapped) {
bsalomon926cb022015-12-17 18:15:11 -08004367 if (!this->caps()->isConfigTexturable(config)) {
Greg Daniel177e6952017-10-12 12:27:11 -04004368 return reinterpret_cast<GrBackendObject>(nullptr);
bsalomon926cb022015-12-17 18:15:11 -08004369 }
Greg Daniel177e6952017-10-12 12:27:11 -04004370
4371 // Currently we don't support uploading pixel data when mipped.
4372 if (pixels && GrMipMapped::kYes == mipMapped) {
4373 return reinterpret_cast<GrBackendObject>(nullptr);
4374 }
4375
Ben Wagner18b61f92016-10-25 10:44:02 -04004376 std::unique_ptr<GrGLTextureInfo> info = skstd::make_unique<GrGLTextureInfo>();
bsalomon091f60c2015-11-10 11:54:56 -08004377 info->fTarget = GR_GL_TEXTURE_2D;
kkinnunen546eb5c2015-12-11 00:05:33 -08004378 info->fID = 0;
bsalomon091f60c2015-11-10 11:54:56 -08004379 GL_CALL(GenTextures(1, &info->fID));
jvanverth672bb7f2015-07-13 07:19:57 -07004380 GL_CALL(ActiveTexture(GR_GL_TEXTURE0));
4381 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, 1));
bsalomon091f60c2015-11-10 11:54:56 -08004382 GL_CALL(BindTexture(info->fTarget, info->fID));
Robert Phillips294870f2016-11-11 12:38:40 -05004383 fHWBoundTextureUniqueIDs[0].makeInvalid();
bsalomon091f60c2015-11-10 11:54:56 -08004384 GL_CALL(TexParameteri(info->fTarget, GR_GL_TEXTURE_MAG_FILTER, GR_GL_NEAREST));
4385 GL_CALL(TexParameteri(info->fTarget, GR_GL_TEXTURE_MIN_FILTER, GR_GL_NEAREST));
4386 GL_CALL(TexParameteri(info->fTarget, GR_GL_TEXTURE_WRAP_S, GR_GL_CLAMP_TO_EDGE));
4387 GL_CALL(TexParameteri(info->fTarget, GR_GL_TEXTURE_WRAP_T, GR_GL_CLAMP_TO_EDGE));
jvanverth672bb7f2015-07-13 07:19:57 -07004388
bsalomon76148af2016-01-12 11:13:47 -08004389 GrGLenum internalFormat;
4390 GrGLenum externalFormat;
4391 GrGLenum externalType;
4392
4393 if (!this->glCaps().getTexImageFormats(config, config, &internalFormat, &externalFormat,
4394 &externalType)) {
bsalomon76148af2016-01-12 11:13:47 -08004395 return reinterpret_cast<GrBackendObject>(nullptr);
bsalomon76148af2016-01-12 11:13:47 -08004396 }
jvanverth672bb7f2015-07-13 07:19:57 -07004397
Brian Osman9b560242017-09-05 15:34:52 -04004398 this->unbindCpuToGpuXferBuffer();
Greg Daniel177e6952017-10-12 12:27:11 -04004399
4400 // Figure out the number of mip levels.
4401 int mipLevels = 1;
4402 if (GrMipMapped::kYes == mipMapped) {
4403 mipLevels = SkMipMap::ComputeLevelCount(w, h) + 1;
4404 }
4405
4406 int width = w;
4407 int height = h;
4408 for (int i = 0; i < mipLevels; ++i) {
4409 GL_CALL(TexImage2D(info->fTarget, 0, internalFormat, width, height, 0, externalFormat,
4410 externalType, pixels));
4411 width = SkTMax(1, width / 2);
4412 height = SkTMax(1, height / 2);
4413 }
jvanverth672bb7f2015-07-13 07:19:57 -07004414
Ben Wagner18b61f92016-10-25 10:44:02 -04004415 return reinterpret_cast<GrBackendObject>(info.release());
jvanverth672bb7f2015-07-13 07:19:57 -07004416}
4417
jvanverth88957922015-07-14 11:02:52 -07004418bool GrGLGpu::isTestingOnlyBackendTexture(GrBackendObject id) const {
bsalomon091f60c2015-11-10 11:54:56 -08004419 GrGLuint texID = reinterpret_cast<const GrGLTextureInfo*>(id)->fID;
jvanverth672bb7f2015-07-13 07:19:57 -07004420
4421 GrGLboolean result;
4422 GL_CALL_RET(result, IsTexture(texID));
4423
4424 return (GR_GL_TRUE == result);
4425}
4426
bsalomone63ffef2016-02-05 07:17:34 -08004427void GrGLGpu::deleteTestingOnlyBackendTexture(GrBackendObject id, bool abandonTexture) {
Ben Wagner18b61f92016-10-25 10:44:02 -04004428 std::unique_ptr<const GrGLTextureInfo> info(reinterpret_cast<const GrGLTextureInfo*>(id));
bsalomon091f60c2015-11-10 11:54:56 -08004429 GrGLuint texID = info->fID;
bsalomon091f60c2015-11-10 11:54:56 -08004430
bsalomon67d76202015-11-11 12:40:42 -08004431 if (!abandonTexture) {
4432 GL_CALL(DeleteTextures(1, &texID));
4433 }
jvanverth672bb7f2015-07-13 07:19:57 -07004434}
4435
joshualitt8fd844f2015-12-02 13:36:47 -08004436void GrGLGpu::resetShaderCacheForTesting() const {
4437 fProgramCache->abandon();
4438}
4439
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00004440///////////////////////////////////////////////////////////////////////////////
bsalomon6df86402015-06-01 10:41:49 -07004441
cdaltone2e71c22016-04-07 18:13:29 -07004442GrGLAttribArrayState* GrGLGpu::HWVertexArrayState::bindInternalVertexArray(GrGLGpu* gpu,
csmartdalton485a1202016-07-13 10:16:32 -07004443 const GrBuffer* ibuf) {
robertphillips@google.com4f65a272013-03-26 19:40:46 +00004444 GrGLAttribArrayState* attribState;
4445
cdaltone2e71c22016-04-07 18:13:29 -07004446 if (gpu->glCaps().isCoreProfile()) {
4447 if (!fCoreProfileVertexArray) {
bsalomon@google.com6918d482013-03-07 19:09:11 +00004448 GrGLuint arrayID;
4449 GR_GL_CALL(gpu->glInterface(), GenVertexArrays(1, &arrayID));
4450 int attrCount = gpu->glCaps().maxVertexAttributes();
cdaltone2e71c22016-04-07 18:13:29 -07004451 fCoreProfileVertexArray = new GrGLVertexArray(arrayID, attrCount);
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00004452 }
cdaltone2e71c22016-04-07 18:13:29 -07004453 if (ibuf) {
4454 attribState = fCoreProfileVertexArray->bindWithIndexBuffer(gpu, ibuf);
bsalomon6df86402015-06-01 10:41:49 -07004455 } else {
cdaltone2e71c22016-04-07 18:13:29 -07004456 attribState = fCoreProfileVertexArray->bind(gpu);
bsalomon6df86402015-06-01 10:41:49 -07004457 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00004458 } else {
cdaltone2e71c22016-04-07 18:13:29 -07004459 if (ibuf) {
4460 // bindBuffer implicitly binds VAO 0 when binding an index buffer.
4461 gpu->bindBuffer(kIndex_GrBufferType, ibuf);
bsalomon@google.com6918d482013-03-07 19:09:11 +00004462 } else {
4463 this->setVertexArrayID(gpu, 0);
4464 }
4465 int attrCount = gpu->glCaps().maxVertexAttributes();
4466 if (fDefaultVertexArrayAttribState.count() != attrCount) {
4467 fDefaultVertexArrayAttribState.resize(attrCount);
4468 }
4469 attribState = &fDefaultVertexArrayAttribState;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00004470 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00004471 return attribState;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00004472}
bsalomone179a912016-01-20 06:18:10 -08004473
Robert Phillips3798c862017-03-27 11:08:16 -04004474bool GrGLGpu::onIsACopyNeededForTextureParams(GrTextureProxy* proxy,
Brian Salomon2bbdcc42017-09-07 12:36:34 -04004475 const GrSamplerState& textureParams,
Robert Phillips81444fb2017-03-21 09:14:35 -04004476 GrTextureProducer::CopyParams* copyParams,
4477 SkScalar scaleAdjust[2]) const {
Robert Phillips3798c862017-03-27 11:08:16 -04004478 const GrTexture* texture = proxy->priv().peekTexture();
4479 if (!texture) {
4480 // The only way to get and EXTERNAL or RECTANGLE texture in Ganesh is to wrap them.
4481 // In that case the proxy should already be instantiated.
4482 return false;
4483 }
4484
Brian Salomon2bbdcc42017-09-07 12:36:34 -04004485 if (textureParams.isRepeated() || GrSamplerState::Filter::kMipMap == textureParams.filter()) {
Robert Phillips3798c862017-03-27 11:08:16 -04004486 const GrGLTexture* glTexture = static_cast<const GrGLTexture*>(texture);
bsalomone179a912016-01-20 06:18:10 -08004487 if (GR_GL_TEXTURE_EXTERNAL == glTexture->target() ||
4488 GR_GL_TEXTURE_RECTANGLE == glTexture->target()) {
Brian Salomon2bbdcc42017-09-07 12:36:34 -04004489 copyParams->fFilter = GrSamplerState::Filter::kNearest;
bsalomone179a912016-01-20 06:18:10 -08004490 copyParams->fWidth = texture->width();
4491 copyParams->fHeight = texture->height();
4492 return true;
4493 }
4494 }
4495 return false;
4496}
jvanverth84741b32016-09-30 08:39:02 -07004497
Greg Daniel51316782017-08-02 15:10:09 +00004498void GrGLGpu::onFinishFlush(bool insertedSemaphore) {
4499 // If we inserted semaphores during the flush, we need to call GLFlush.
4500 if (insertedSemaphore) {
4501 GL_CALL(Flush());
4502 }
4503}
4504
Greg Daniel6be35232017-03-01 17:01:09 -05004505GrFence SK_WARN_UNUSED_RESULT GrGLGpu::insertFence() {
Greg Danielc64ee462017-06-15 16:59:49 -04004506 SkASSERT(this->caps()->fenceSyncSupport());
Greg Daniel6be35232017-03-01 17:01:09 -05004507 GrGLsync sync;
4508 GL_CALL_RET(sync, FenceSync(GR_GL_SYNC_GPU_COMMANDS_COMPLETE, 0));
4509 GR_STATIC_ASSERT(sizeof(GrFence) >= sizeof(GrGLsync));
4510 return (GrFence)sync;
jvanverth84741b32016-09-30 08:39:02 -07004511}
4512
Greg Daniel6be35232017-03-01 17:01:09 -05004513bool GrGLGpu::waitFence(GrFence fence, uint64_t timeout) {
jvanverth84741b32016-09-30 08:39:02 -07004514 GrGLenum result;
4515 GL_CALL_RET(result, ClientWaitSync((GrGLsync)fence, GR_GL_SYNC_FLUSH_COMMANDS_BIT, timeout));
4516 return (GR_GL_CONDITION_SATISFIED == result);
4517}
4518
4519void GrGLGpu::deleteFence(GrFence fence) const {
Greg Daniel6be35232017-03-01 17:01:09 -05004520 this->deleteSync((GrGLsync)fence);
4521}
4522
Greg Daniela5cb7812017-06-16 09:45:32 -04004523sk_sp<GrSemaphore> SK_WARN_UNUSED_RESULT GrGLGpu::makeSemaphore(bool isOwned) {
Greg Danielc64ee462017-06-15 16:59:49 -04004524 SkASSERT(this->caps()->fenceSyncSupport());
Greg Daniela5cb7812017-06-16 09:45:32 -04004525 return GrGLSemaphore::Make(this, isOwned);
Greg Daniel6be35232017-03-01 17:01:09 -05004526}
4527
Greg Daniela5cb7812017-06-16 09:45:32 -04004528sk_sp<GrSemaphore> GrGLGpu::wrapBackendSemaphore(const GrBackendSemaphore& semaphore,
4529 GrWrapOwnership ownership) {
Greg Danielc64ee462017-06-15 16:59:49 -04004530 SkASSERT(this->caps()->fenceSyncSupport());
Greg Daniela5cb7812017-06-16 09:45:32 -04004531 return GrGLSemaphore::MakeWrapped(this, semaphore.glSync(), ownership);
4532}
4533
Brian Osmandc87c952017-04-28 13:57:38 -04004534void GrGLGpu::insertSemaphore(sk_sp<GrSemaphore> semaphore, bool flush) {
Greg Daniel6be35232017-03-01 17:01:09 -05004535 GrGLSemaphore* glSem = static_cast<GrGLSemaphore*>(semaphore.get());
4536
4537 GrGLsync sync;
4538 GL_CALL_RET(sync, FenceSync(GR_GL_SYNC_GPU_COMMANDS_COMPLETE, 0));
4539 glSem->setSync(sync);
Brian Osmandc87c952017-04-28 13:57:38 -04004540
4541 if (flush) {
4542 GL_CALL(Flush());
4543 }
Greg Daniel6be35232017-03-01 17:01:09 -05004544}
4545
4546void GrGLGpu::waitSemaphore(sk_sp<GrSemaphore> semaphore) {
4547 GrGLSemaphore* glSem = static_cast<GrGLSemaphore*>(semaphore.get());
4548
4549 GL_CALL(WaitSync(glSem->sync(), 0, GR_GL_TIMEOUT_IGNORED));
4550}
4551
4552void GrGLGpu::deleteSync(GrGLsync sync) const {
4553 GL_CALL(DeleteSync(sync));
jvanverth84741b32016-09-30 08:39:02 -07004554}
Brian Osman13dddce2017-05-09 13:19:50 -04004555
Robert Phillips65a88fa2017-08-08 08:36:22 -04004556void GrGLGpu::insertEventMarker(const char* msg) {
4557 GL_CALL(InsertEventMarker(strlen(msg), msg));
4558}
4559
Brian Osman13dddce2017-05-09 13:19:50 -04004560sk_sp<GrSemaphore> GrGLGpu::prepareTextureForCrossContextUsage(GrTexture* texture) {
4561 // Set up a semaphore to be signaled once the data is ready, and flush GL
Greg Daniela5cb7812017-06-16 09:45:32 -04004562 sk_sp<GrSemaphore> semaphore = this->makeSemaphore(true);
Brian Osman13dddce2017-05-09 13:19:50 -04004563 this->insertSemaphore(semaphore, true);
4564
4565 return semaphore;
4566}
Robert Phillips646e4292017-06-13 12:44:56 -04004567
4568int GrGLGpu::TextureToCopyProgramIdx(GrTexture* texture) {
4569 switch (texture->texturePriv().samplerType()) {
4570 case kTexture2DSampler_GrSLType:
4571 return 0;
4572 case kITexture2DSampler_GrSLType:
4573 return 1;
4574 case kTexture2DRectSampler_GrSLType:
4575 return 2;
4576 case kTextureExternalSampler_GrSLType:
4577 return 3;
4578 default:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -04004579 SK_ABORT("Unexpected samper type");
Robert Phillips646e4292017-06-13 12:44:56 -04004580 return 0;
4581 }
4582}
Brian Osman71a18892017-08-10 10:23:25 -04004583
4584void GrGLGpu::onDumpJSON(SkJSONWriter* writer) const {
4585 // We are called by the base class, which has already called beginObject(). We choose to nest
4586 // all of our caps information in a named sub-object.
4587 writer->beginObject("GL GPU");
4588
4589 const GrGLubyte* str;
4590 GL_CALL_RET(str, GetString(GR_GL_VERSION));
4591 writer->appendString("GL_VERSION", (const char*)(str));
4592 GL_CALL_RET(str, GetString(GR_GL_RENDERER));
4593 writer->appendString("GL_RENDERER", (const char*)(str));
4594 GL_CALL_RET(str, GetString(GR_GL_VENDOR));
4595 writer->appendString("GL_VENDOR", (const char*)(str));
4596 GL_CALL_RET(str, GetString(GR_GL_SHADING_LANGUAGE_VERSION));
4597 writer->appendString("GL_SHADING_LANGUAGE_VERSION", (const char*)(str));
4598
4599 writer->appendName("extensions");
4600 glInterface()->fExtensions.dumpJSON(writer);
4601
4602 writer->endObject();
4603}