blob: 5bfe2e557e4ea3784157e8f9d574846e33c4e3b6 [file] [log] [blame]
reed@google.comac10a2d2010-12-22 21:39:39 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
reed@google.comac10a2d2010-12-22 21:39:39 +00006 */
7
jvanverth39edf762014-12-22 11:44:19 -08008#include "GrGLGpu.h"
Hal Canary95e3c052017-01-11 12:44:43 -05009
Brian Salomon028a9a52017-05-11 11:39:08 -040010#include <cmath>
Hal Canary95e3c052017-01-11 12:44:43 -050011#include "../private/GrGLSL.h"
Greg Daniela5cb7812017-06-16 09:45:32 -040012#include "GrBackendSemaphore.h"
Greg Daniel7ef28f32017-04-20 16:41:55 +000013#include "GrBackendSurface.h"
Hal Canary95e3c052017-01-11 12:44:43 -050014#include "GrFixedClip.h"
cdalton397536c2016-03-25 12:15:03 -070015#include "GrGLBuffer.h"
egdaniel066df7c2016-06-08 14:02:27 -070016#include "GrGLGpuCommandBuffer.h"
Greg Daniel6be35232017-03-01 17:01:09 -050017#include "GrGLSemaphore.h"
egdaniel8dc7c3a2015-04-16 11:22:42 -070018#include "GrGLStencilAttachment.h"
bsalomon37dd3312014-11-03 08:47:23 -080019#include "GrGLTextureRenderTarget.h"
bsalomon3582d3e2015-02-13 14:20:05 -080020#include "GrGpuResourcePriv.h"
egdaniel0e1853c2016-03-17 11:35:45 -070021#include "GrMesh.h"
Hal Canary95e3c052017-01-11 12:44:43 -050022#include "GrPipeline.h"
bsalomon6bc1b5f2015-02-23 09:06:38 -080023#include "GrRenderTargetPriv.h"
Brian Salomon94efbf52016-11-29 13:43:05 -050024#include "GrShaderCaps.h"
Robert Phillips3798c862017-03-27 11:08:16 -040025#include "GrSurfaceProxyPriv.h"
bsalomonafbf2d62014-09-30 12:18:44 -070026#include "GrTexturePriv.h"
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +000027#include "GrTypes.h"
Hal Canary95e3c052017-01-11 12:44:43 -050028#include "SkAutoMalloc.h"
Stan Iliev0078ab22017-11-08 14:16:26 -050029#include "SkHalf.h"
Brian Osman71a18892017-08-10 10:23:25 -040030#include "SkJSONWriter.h"
Kevin Lubickc456b732017-01-11 17:21:57 +000031#include "SkMakeUnique.h"
32#include "SkMipMap.h"
33#include "SkPixmap.h"
Kevin Lubickc456b732017-01-11 17:21:57 +000034#include "SkSLCompiler.h"
Hal Canary95e3c052017-01-11 12:44:43 -050035#include "SkStrokeRec.h"
Kevin Lubickc456b732017-01-11 17:21:57 +000036#include "SkTemplates.h"
Ryan Macnak38a10ad2017-07-10 10:36:34 -070037#include "SkTraceEvent.h"
Kevin Lubickc456b732017-01-11 17:21:57 +000038#include "SkTypes.h"
Hal Canary95e3c052017-01-11 12:44:43 -050039#include "builders/GrGLShaderStringBuilder.h"
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
cdalton8917d622015-05-06 13:40:21 -070060static const GrGLenum gXfermodeEquation2Blend[] = {
61 // Basic OpenGL blend equations.
62 GR_GL_FUNC_ADD,
63 GR_GL_FUNC_SUBTRACT,
64 GR_GL_FUNC_REVERSE_SUBTRACT,
65
66 // GL_KHR_blend_equation_advanced.
67 GR_GL_SCREEN,
68 GR_GL_OVERLAY,
69 GR_GL_DARKEN,
70 GR_GL_LIGHTEN,
71 GR_GL_COLORDODGE,
72 GR_GL_COLORBURN,
73 GR_GL_HARDLIGHT,
74 GR_GL_SOFTLIGHT,
75 GR_GL_DIFFERENCE,
76 GR_GL_EXCLUSION,
77 GR_GL_MULTIPLY,
78 GR_GL_HSL_HUE,
79 GR_GL_HSL_SATURATION,
80 GR_GL_HSL_COLOR,
81 GR_GL_HSL_LUMINOSITY
82};
83GR_STATIC_ASSERT(0 == kAdd_GrBlendEquation);
84GR_STATIC_ASSERT(1 == kSubtract_GrBlendEquation);
85GR_STATIC_ASSERT(2 == kReverseSubtract_GrBlendEquation);
86GR_STATIC_ASSERT(3 == kScreen_GrBlendEquation);
87GR_STATIC_ASSERT(4 == kOverlay_GrBlendEquation);
88GR_STATIC_ASSERT(5 == kDarken_GrBlendEquation);
89GR_STATIC_ASSERT(6 == kLighten_GrBlendEquation);
90GR_STATIC_ASSERT(7 == kColorDodge_GrBlendEquation);
91GR_STATIC_ASSERT(8 == kColorBurn_GrBlendEquation);
92GR_STATIC_ASSERT(9 == kHardLight_GrBlendEquation);
93GR_STATIC_ASSERT(10 == kSoftLight_GrBlendEquation);
94GR_STATIC_ASSERT(11 == kDifference_GrBlendEquation);
95GR_STATIC_ASSERT(12 == kExclusion_GrBlendEquation);
96GR_STATIC_ASSERT(13 == kMultiply_GrBlendEquation);
97GR_STATIC_ASSERT(14 == kHSLHue_GrBlendEquation);
98GR_STATIC_ASSERT(15 == kHSLSaturation_GrBlendEquation);
99GR_STATIC_ASSERT(16 == kHSLColor_GrBlendEquation);
100GR_STATIC_ASSERT(17 == kHSLLuminosity_GrBlendEquation);
bsalomonf7cc8772015-05-11 11:21:14 -0700101GR_STATIC_ASSERT(SK_ARRAY_COUNT(gXfermodeEquation2Blend) == kGrBlendEquationCnt);
cdalton8917d622015-05-06 13:40:21 -0700102
twiz@google.com0f31ca72011-03-18 17:38:11 +0000103static const GrGLenum gXfermodeCoeff2Blend[] = {
104 GR_GL_ZERO,
105 GR_GL_ONE,
106 GR_GL_SRC_COLOR,
107 GR_GL_ONE_MINUS_SRC_COLOR,
108 GR_GL_DST_COLOR,
109 GR_GL_ONE_MINUS_DST_COLOR,
110 GR_GL_SRC_ALPHA,
111 GR_GL_ONE_MINUS_SRC_ALPHA,
112 GR_GL_DST_ALPHA,
113 GR_GL_ONE_MINUS_DST_ALPHA,
114 GR_GL_CONSTANT_COLOR,
115 GR_GL_ONE_MINUS_CONSTANT_COLOR,
116 GR_GL_CONSTANT_ALPHA,
117 GR_GL_ONE_MINUS_CONSTANT_ALPHA,
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000118
119 // extended blend coeffs
120 GR_GL_SRC1_COLOR,
121 GR_GL_ONE_MINUS_SRC1_COLOR,
122 GR_GL_SRC1_ALPHA,
123 GR_GL_ONE_MINUS_SRC1_ALPHA,
reed@google.comac10a2d2010-12-22 21:39:39 +0000124};
125
bsalomon861e1032014-12-16 07:33:49 -0800126bool GrGLGpu::BlendCoeffReferencesConstant(GrBlendCoeff coeff) {
bsalomon@google.com080773c2011-03-15 19:09:25 +0000127 static const bool gCoeffReferencesBlendConst[] = {
128 false,
129 false,
130 false,
131 false,
132 false,
133 false,
134 false,
135 false,
136 false,
137 false,
138 true,
139 true,
140 true,
141 true,
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000142
143 // extended blend coeffs
144 false,
145 false,
146 false,
147 false,
bsalomon@google.com080773c2011-03-15 19:09:25 +0000148 };
149 return gCoeffReferencesBlendConst[coeff];
bsalomonf7cc8772015-05-11 11:21:14 -0700150 GR_STATIC_ASSERT(kGrBlendCoeffCnt == SK_ARRAY_COUNT(gCoeffReferencesBlendConst));
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000151
bsalomon@google.com47059542012-06-06 20:51:20 +0000152 GR_STATIC_ASSERT(0 == kZero_GrBlendCoeff);
153 GR_STATIC_ASSERT(1 == kOne_GrBlendCoeff);
154 GR_STATIC_ASSERT(2 == kSC_GrBlendCoeff);
155 GR_STATIC_ASSERT(3 == kISC_GrBlendCoeff);
156 GR_STATIC_ASSERT(4 == kDC_GrBlendCoeff);
157 GR_STATIC_ASSERT(5 == kIDC_GrBlendCoeff);
158 GR_STATIC_ASSERT(6 == kSA_GrBlendCoeff);
159 GR_STATIC_ASSERT(7 == kISA_GrBlendCoeff);
160 GR_STATIC_ASSERT(8 == kDA_GrBlendCoeff);
161 GR_STATIC_ASSERT(9 == kIDA_GrBlendCoeff);
162 GR_STATIC_ASSERT(10 == kConstC_GrBlendCoeff);
163 GR_STATIC_ASSERT(11 == kIConstC_GrBlendCoeff);
164 GR_STATIC_ASSERT(12 == kConstA_GrBlendCoeff);
165 GR_STATIC_ASSERT(13 == kIConstA_GrBlendCoeff);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000166
bsalomon@google.com47059542012-06-06 20:51:20 +0000167 GR_STATIC_ASSERT(14 == kS2C_GrBlendCoeff);
168 GR_STATIC_ASSERT(15 == kIS2C_GrBlendCoeff);
169 GR_STATIC_ASSERT(16 == kS2A_GrBlendCoeff);
170 GR_STATIC_ASSERT(17 == kIS2A_GrBlendCoeff);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000171
172 // assertion for gXfermodeCoeff2Blend have to be in GrGpu scope
bsalomonf7cc8772015-05-11 11:21:14 -0700173 GR_STATIC_ASSERT(kGrBlendCoeffCnt == SK_ARRAY_COUNT(gXfermodeCoeff2Blend));
bsalomon@google.com080773c2011-03-15 19:09:25 +0000174}
175
reed@google.comac10a2d2010-12-22 21:39:39 +0000176///////////////////////////////////////////////////////////////////////////////
177
Brian Salomon384fab42017-12-07 12:33:05 -0500178sk_sp<GrGpu> GrGLGpu::Make(sk_sp<const GrGLInterface> interface, const GrContextOptions& options,
179 GrContext* context) {
180 if (!interface) {
Brian Salomon3d6801e2017-12-11 10:06:31 -0500181 interface = GrGLMakeNativeInterface();
182 // For clients that have written their own GrGLCreateNativeInterface and haven't yet updated
183 // to GrGLMakeNativeInterface.
184 if (!interface) {
185 interface = sk_ref_sp(GrGLCreateNativeInterface());
186 }
Brian Salomon384fab42017-12-07 12:33:05 -0500187 if (!interface) {
188 return nullptr;
189 }
bsalomon424cc262015-05-22 10:37:30 -0700190 }
Jim Van Verth76334772017-05-05 16:46:05 -0400191#ifdef USE_NSIGHT
192 const_cast<GrContextOptions&>(options).fSuppressPathRendering = true;
193#endif
Brian Salomon8ab1cc42017-12-07 12:40:00 -0500194 auto glContext = GrGLContext::Make(std::move(interface), options);
195 if (!glContext) {
196 return nullptr;
bsalomon424cc262015-05-22 10:37:30 -0700197 }
Brian Salomon8ab1cc42017-12-07 12:40:00 -0500198 return sk_sp<GrGpu>(new GrGLGpu(std::move(glContext), context));
bsalomon424cc262015-05-22 10:37:30 -0700199}
200
Brian Salomon8ab1cc42017-12-07 12:40:00 -0500201GrGLGpu::GrGLGpu(std::unique_ptr<GrGLContext> ctx, GrContext* context)
202 : GrGpu(context)
203 , fGLContext(std::move(ctx))
204 , fProgramCache(new ProgramCache(this))
205 , fHWProgramID(0)
206 , fTempSrcFBOID(0)
207 , fTempDstFBOID(0)
208 , fStencilClearFBOID(0)
209 , fHWMaxUsedBufferTextureUnit(-1)
210 , fHWMinSampleShading(0.0) {
211 SkASSERT(fGLContext);
212 fCaps = sk_ref_sp(fGLContext->caps());
bsalomon@google.combcce8922013-03-25 15:38:39 +0000213
Brian Salomon1edc5b92016-11-29 13:43:46 -0500214 fHWBoundTextureUniqueIDs.reset(this->caps()->shaderCaps()->maxCombinedSamplers());
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +0000215
cdaltone2e71c22016-04-07 18:13:29 -0700216 fHWBufferState[kVertex_GrBufferType].fGLTarget = GR_GL_ARRAY_BUFFER;
217 fHWBufferState[kIndex_GrBufferType].fGLTarget = GR_GL_ELEMENT_ARRAY_BUFFER;
218 fHWBufferState[kTexel_GrBufferType].fGLTarget = GR_GL_TEXTURE_BUFFER;
219 fHWBufferState[kDrawIndirect_GrBufferType].fGLTarget = GR_GL_DRAW_INDIRECT_BUFFER;
220 if (GrGLCaps::kChromium_TransferBufferType == this->glCaps().transferBufferType()) {
221 fHWBufferState[kXferCpuToGpu_GrBufferType].fGLTarget =
222 GR_GL_PIXEL_UNPACK_TRANSFER_BUFFER_CHROMIUM;
223 fHWBufferState[kXferGpuToCpu_GrBufferType].fGLTarget =
224 GR_GL_PIXEL_PACK_TRANSFER_BUFFER_CHROMIUM;
225 } else {
226 fHWBufferState[kXferCpuToGpu_GrBufferType].fGLTarget = GR_GL_PIXEL_UNPACK_BUFFER;
227 fHWBufferState[kXferGpuToCpu_GrBufferType].fGLTarget = GR_GL_PIXEL_PACK_BUFFER;
228 }
Rob Phillipsbc534f62017-09-05 19:56:19 -0400229 for (int i = 0; i < kGrBufferTypeCount; ++i) {
230 fHWBufferState[i].invalidate();
231 }
cdaltone2e71c22016-04-07 18:13:29 -0700232 GR_STATIC_ASSERT(6 == SK_ARRAY_COUNT(fHWBufferState));
233
cdalton74b8d322016-04-11 14:47:28 -0700234 if (this->caps()->shaderCaps()->texelBufferSupport()) {
Brian Salomon1edc5b92016-11-29 13:43:46 -0500235 fHWBufferTextures.reset(this->caps()->shaderCaps()->maxCombinedSamplers());
cdalton74b8d322016-04-11 14:47:28 -0700236 }
237
cdaltone2e71c22016-04-07 18:13:29 -0700238 if (this->glCaps().shaderCaps()->pathRenderingSupport()) {
239 fPathRendering.reset(new GrGLPathRendering(this));
240 }
241
bsalomon424cc262015-05-22 10:37:30 -0700242 GrGLClearErr(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +0000243}
244
bsalomon861e1032014-12-16 07:33:49 -0800245GrGLGpu::~GrGLGpu() {
cdaltone2e71c22016-04-07 18:13:29 -0700246 // Ensure any GrGpuResource objects get deleted first, since they may require a working GrGLGpu
247 // to release the resources held by the objects themselves.
kkinnunen702501d2016-01-13 23:36:45 -0800248 fPathRendering.reset();
cdaltone2e71c22016-04-07 18:13:29 -0700249 fCopyProgramArrayBuffer.reset();
brianosman33f6b3f2016-06-02 05:49:21 -0700250 fMipmapProgramArrayBuffer.reset();
Mike Klein31550db2017-06-06 23:29:53 +0000251 fStencilClipClearArrayBuffer.reset();
kkinnunen702501d2016-01-13 23:36:45 -0800252
Brian Salomon43f8bf02017-10-18 08:33:29 -0400253 if (fHWProgramID) {
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000254 // detach the current program so there is no confusion on OpenGL's part
255 // that we want it to be deleted
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000256 GL_CALL(UseProgram(0));
257 }
258
Brian Salomon43f8bf02017-10-18 08:33:29 -0400259 if (fTempSrcFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700260 this->deleteFramebuffer(fTempSrcFBOID);
egdaniel0f5f9672015-02-03 11:10:51 -0800261 }
Brian Salomon43f8bf02017-10-18 08:33:29 -0400262 if (fTempDstFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700263 this->deleteFramebuffer(fTempDstFBOID);
egdaniel0f5f9672015-02-03 11:10:51 -0800264 }
Brian Salomon43f8bf02017-10-18 08:33:29 -0400265 if (fStencilClearFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700266 this->deleteFramebuffer(fStencilClearFBOID);
bsalomondd3143b2015-02-23 09:27:45 -0800267 }
egdaniel0f5f9672015-02-03 11:10:51 -0800268
bsalomon7ea33f52015-11-22 14:51:00 -0800269 for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) {
270 if (0 != fCopyPrograms[i].fProgram) {
271 GL_CALL(DeleteProgram(fCopyPrograms[i].fProgram));
272 }
bsalomon6df86402015-06-01 10:41:49 -0700273 }
bsalomon6dea83f2015-12-03 12:58:06 -0800274
brianosman33f6b3f2016-06-02 05:49:21 -0700275 for (size_t i = 0; i < SK_ARRAY_COUNT(fMipmapPrograms); ++i) {
276 if (0 != fMipmapPrograms[i].fProgram) {
277 GL_CALL(DeleteProgram(fMipmapPrograms[i].fProgram));
278 }
279 }
280
Brian Salomon43f8bf02017-10-18 08:33:29 -0400281 if (fStencilClipClearProgram) {
Mike Klein31550db2017-06-06 23:29:53 +0000282 GL_CALL(DeleteProgram(fStencilClipClearProgram));
bsalomon6dea83f2015-12-03 12:58:06 -0800283 }
284
Brian Salomon43f8bf02017-10-18 08:33:29 -0400285 if (fClearColorProgram.fProgram) {
286 GL_CALL(DeleteProgram(fClearColorProgram.fProgram));
287 }
288
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000289 delete fProgramCache;
bsalomonc8dc1f72014-08-21 13:02:13 -0700290}
291
bsalomon6e2aad42016-04-01 11:54:31 -0700292void GrGLGpu::disconnect(DisconnectType type) {
293 INHERITED::disconnect(type);
294 if (DisconnectType::kCleanup == type) {
295 if (fHWProgramID) {
296 GL_CALL(UseProgram(0));
297 }
298 if (fTempSrcFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700299 this->deleteFramebuffer(fTempSrcFBOID);
bsalomon6e2aad42016-04-01 11:54:31 -0700300 }
301 if (fTempDstFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700302 this->deleteFramebuffer(fTempDstFBOID);
bsalomon6e2aad42016-04-01 11:54:31 -0700303 }
304 if (fStencilClearFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700305 this->deleteFramebuffer(fStencilClearFBOID);
bsalomon6e2aad42016-04-01 11:54:31 -0700306 }
bsalomon6e2aad42016-04-01 11:54:31 -0700307 for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) {
308 if (fCopyPrograms[i].fProgram) {
309 GL_CALL(DeleteProgram(fCopyPrograms[i].fProgram));
310 }
311 }
brianosman33f6b3f2016-06-02 05:49:21 -0700312 for (size_t i = 0; i < SK_ARRAY_COUNT(fMipmapPrograms); ++i) {
313 if (fMipmapPrograms[i].fProgram) {
314 GL_CALL(DeleteProgram(fMipmapPrograms[i].fProgram));
315 }
316 }
Mike Klein31550db2017-06-06 23:29:53 +0000317 if (fStencilClipClearProgram) {
318 GL_CALL(DeleteProgram(fStencilClipClearProgram));
bsalomon6e2aad42016-04-01 11:54:31 -0700319 }
Brian Salomon43f8bf02017-10-18 08:33:29 -0400320
321 if (fClearColorProgram.fProgram) {
322 GL_CALL(DeleteProgram(fClearColorProgram.fProgram));
323 }
bsalomon6e2aad42016-04-01 11:54:31 -0700324 } else {
325 if (fProgramCache) {
326 fProgramCache->abandon();
327 }
328 }
329
330 delete fProgramCache;
331 fProgramCache = nullptr;
332
bsalomonc8dc1f72014-08-21 13:02:13 -0700333 fHWProgramID = 0;
egdanield803f272015-03-18 13:01:52 -0700334 fTempSrcFBOID = 0;
335 fTempDstFBOID = 0;
336 fStencilClearFBOID = 0;
cdaltone2e71c22016-04-07 18:13:29 -0700337 fCopyProgramArrayBuffer.reset();
bsalomon7ea33f52015-11-22 14:51:00 -0800338 for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) {
339 fCopyPrograms[i].fProgram = 0;
340 }
brianosman33f6b3f2016-06-02 05:49:21 -0700341 fMipmapProgramArrayBuffer.reset();
342 for (size_t i = 0; i < SK_ARRAY_COUNT(fMipmapPrograms); ++i) {
343 fMipmapPrograms[i].fProgram = 0;
344 }
Mike Klein31550db2017-06-06 23:29:53 +0000345 fStencilClipClearProgram = 0;
346 fStencilClipClearArrayBuffer.reset();
Brian Salomon43f8bf02017-10-18 08:33:29 -0400347 fClearColorProgram.fProgram = 0;
348
jvanverthe9c0fc62015-04-29 11:18:05 -0700349 if (this->glCaps().shaderCaps()->pathRenderingSupport()) {
bsalomon6e2aad42016-04-01 11:54:31 -0700350 this->glPathRendering()->disconnect(type);
bsalomonc8dc1f72014-08-21 13:02:13 -0700351 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000352}
353
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000354///////////////////////////////////////////////////////////////////////////////
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000355
bsalomon861e1032014-12-16 07:33:49 -0800356void GrGLGpu::onResetContext(uint32_t resetBits) {
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000357 if (resetBits & kMisc_GrGLBackendState) {
Brian Salomonf0861672017-05-08 11:10:10 -0400358 // we don't use the zb at all
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000359 GL_CALL(Disable(GR_GL_DEPTH_TEST));
360 GL_CALL(DepthMask(GR_GL_FALSE));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000361
Brian Salomonf0861672017-05-08 11:10:10 -0400362 // We don't use face culling.
363 GL_CALL(Disable(GR_GL_CULL_FACE));
364 // We do use separate stencil. Our algorithms don't care which face is front vs. back so
365 // just set this to the default for self-consistency.
366 GL_CALL(FrontFace(GR_GL_CCW));
367
cdaltone2e71c22016-04-07 18:13:29 -0700368 fHWBufferState[kTexel_GrBufferType].invalidate();
369 fHWBufferState[kDrawIndirect_GrBufferType].invalidate();
370 fHWBufferState[kXferCpuToGpu_GrBufferType].invalidate();
371 fHWBufferState[kXferGpuToCpu_GrBufferType].invalidate();
cdaltonc1613102016-03-16 07:48:20 -0700372
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +0000373 if (kGL_GrGLStandard == this->glStandard()) {
Jim Van Verth32ac83e2016-11-28 15:23:57 -0500374#ifndef USE_NSIGHT
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000375 // Desktop-only state that we never change
376 if (!this->glCaps().isCoreProfile()) {
377 GL_CALL(Disable(GR_GL_POINT_SMOOTH));
378 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
379 GL_CALL(Disable(GR_GL_POLYGON_SMOOTH));
380 GL_CALL(Disable(GR_GL_POLYGON_STIPPLE));
381 GL_CALL(Disable(GR_GL_COLOR_LOGIC_OP));
382 GL_CALL(Disable(GR_GL_INDEX_LOGIC_OP));
383 }
384 // The windows NVIDIA driver has GL_ARB_imaging in the extension string when using a
385 // core profile. This seems like a bug since the core spec removes any mention of
386 // GL_ARB_imaging.
387 if (this->glCaps().imagingSupport() && !this->glCaps().isCoreProfile()) {
388 GL_CALL(Disable(GR_GL_COLOR_TABLE));
389 }
390 GL_CALL(Disable(GR_GL_POLYGON_OFFSET_FILL));
Jim Van Verth609e7cc2017-03-30 14:28:08 -0400391
Jim Van Verthfbdc0802017-05-02 16:15:53 -0400392 if (this->caps()->wireframeMode()) {
393 GL_CALL(PolygonMode(GR_GL_FRONT_AND_BACK, GR_GL_LINE));
394 } else {
395 GL_CALL(PolygonMode(GR_GL_FRONT_AND_BACK, GR_GL_FILL));
396 }
Jim Van Verth32ac83e2016-11-28 15:23:57 -0500397#endif
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000398 // Since ES doesn't support glPointSize at all we always use the VS to
399 // set the point size
400 GL_CALL(Enable(GR_GL_VERTEX_PROGRAM_POINT_SIZE));
401
bsalomon@google.com2b1b8c02013-02-28 22:06:02 +0000402 }
joshualitt58162332014-08-01 06:44:53 -0700403
404 if (kGLES_GrGLStandard == this->glStandard() &&
bsalomon424cc262015-05-22 10:37:30 -0700405 this->hasExtension("GL_ARM_shader_framebuffer_fetch")) {
joshualitt58162332014-08-01 06:44:53 -0700406 // The arm extension requires specifically enabling MSAA fetching per sample.
407 // On some devices this may have a perf hit. Also multiple render targets are disabled
408 GL_CALL(Enable(GR_GL_FETCH_PER_SAMPLE_ARM));
409 }
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000410 fHWWriteToColor = kUnknown_TriState;
411 // we only ever use lines in hairline mode
412 GL_CALL(LineWidth(1));
bsalomonaca31fe2015-09-22 11:38:46 -0700413 GL_CALL(Disable(GR_GL_DITHER));
bsalomon@google.comcad107b2013-06-28 14:32:08 +0000414 }
edisonn@google.comba669992013-06-28 16:03:21 +0000415
egdanielb414f252014-07-29 13:15:47 -0700416 if (resetBits & kMSAAEnable_GrGLBackendState) {
417 fMSAAEnabled = kUnknown_TriState;
vbuzinovdded6962015-06-12 08:59:45 -0700418
egdanieleed519e2016-01-15 11:36:18 -0800419 if (this->caps()->usesMixedSamples()) {
cdaltonaf8bc7d2016-02-05 09:35:20 -0800420 if (0 != this->caps()->maxRasterSamples()) {
421 fHWRasterMultisampleEnabled = kUnknown_TriState;
422 fHWNumRasterSamples = 0;
423 }
424
425 // The skia blend modes all use premultiplied alpha and therefore expect RGBA coverage
426 // modulation. This state has no effect when not rendering to a mixed sampled target.
vbuzinovdded6962015-06-12 08:59:45 -0700427 GL_CALL(CoverageModulation(GR_GL_RGBA));
428 }
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000429 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000430
commit-bot@chromium.org46fbfe02013-08-30 15:52:12 +0000431 fHWActiveTextureUnitIdx = -1; // invalid
Brian Salomonaf971de2017-06-08 16:11:33 -0400432 fLastPrimitiveType = static_cast<GrPrimitiveType>(-1);
commit-bot@chromium.org46fbfe02013-08-30 15:52:12 +0000433
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000434 if (resetBits & kTextureBinding_GrGLBackendState) {
bsalomon1c63bf62014-07-22 13:09:46 -0700435 for (int s = 0; s < fHWBoundTextureUniqueIDs.count(); ++s) {
Robert Phillips294870f2016-11-11 12:38:40 -0500436 fHWBoundTextureUniqueIDs[s].makeInvalid();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000437 }
cdalton74b8d322016-04-11 14:47:28 -0700438 for (int b = 0; b < fHWBufferTextures.count(); ++b) {
439 SkASSERT(this->caps()->shaderCaps()->texelBufferSupport());
440 fHWBufferTextures[b].fKnownBound = false;
441 }
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000442 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000443
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000444 if (resetBits & kBlend_GrGLBackendState) {
445 fHWBlendState.invalidate();
446 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000447
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000448 if (resetBits & kView_GrGLBackendState) {
449 fHWScissorSettings.invalidate();
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700450 fHWWindowRectsState.invalidate();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000451 fHWViewport.invalidate();
452 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000453
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000454 if (resetBits & kStencil_GrGLBackendState) {
455 fHWStencilSettings.invalidate();
456 fHWStencilTestEnabled = kUnknown_TriState;
457 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000458
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000459 // Vertex
460 if (resetBits & kVertex_GrGLBackendState) {
cdaltone2e71c22016-04-07 18:13:29 -0700461 fHWVertexArrayState.invalidate();
462 fHWBufferState[kVertex_GrBufferType].invalidate();
463 fHWBufferState[kIndex_GrBufferType].invalidate();
Chris Daltonda40cd22018-04-16 13:19:58 -0600464 if (this->glCaps().requiresFlushBetweenNonAndInstancedDraws()) {
465 fRequiresFlushBeforeNextInstancedDraw = true;
466 }
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000467 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000468
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000469 if (resetBits & kRenderTarget_GrGLBackendState) {
Robert Phillips294870f2016-11-11 12:38:40 -0500470 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon16921ec2015-07-30 15:34:56 -0700471 fHWSRGBFramebuffer = kUnknown_TriState;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000472 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000473
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000474 if (resetBits & kPathRendering_GrGLBackendState) {
jvanverthe9c0fc62015-04-29 11:18:05 -0700475 if (this->caps()->shaderCaps()->pathRenderingSupport()) {
kkinnunenccdaa042014-08-20 01:36:23 -0700476 this->glPathRendering()->resetContext();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000477 }
bsalomon@google.com05a718c2012-06-29 14:01:53 +0000478 }
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000479
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000480 // we assume these values
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000481 if (resetBits & kPixelStore_GrGLBackendState) {
482 if (this->glCaps().unpackRowLengthSupport()) {
483 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
484 }
485 if (this->glCaps().packRowLengthSupport()) {
486 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
487 }
488 if (this->glCaps().unpackFlipYSupport()) {
489 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
490 }
491 if (this->glCaps().packFlipYSupport()) {
492 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, GR_GL_FALSE));
493 }
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000494 }
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000495
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000496 if (resetBits & kProgram_GrGLBackendState) {
497 fHWProgramID = 0;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000498 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000499}
500
Brian Salomond17f6582017-07-19 18:28:58 -0400501static bool check_backend_texture(const GrBackendTexture& backendTex, const GrGLCaps& caps,
502 GrGLTexture::IDDesc* idDesc) {
Greg Daniel52e16d92018-04-10 09:34:07 -0400503 GrGLTextureInfo info;
504 if (!backendTex.getGLTextureInfo(&info) || !info.fID) {
Brian Salomond17f6582017-07-19 18:28:58 -0400505 return false;
bsalomon091f60c2015-11-10 11:54:56 -0800506 }
robertphillips@google.comb72e5d32012-10-30 15:18:10 +0000507
Greg Daniel52e16d92018-04-10 09:34:07 -0400508 idDesc->fInfo = info;
bsalomon7ea33f52015-11-22 14:51:00 -0800509
Brian Salomond17f6582017-07-19 18:28:58 -0400510 if (GR_GL_TEXTURE_EXTERNAL == idDesc->fInfo.fTarget) {
511 if (!caps.shaderCaps()->externalTextureSupport()) {
512 return false;
513 }
514 } else if (GR_GL_TEXTURE_RECTANGLE == idDesc->fInfo.fTarget) {
515 if (!caps.rectangleTextureSupport()) {
516 return false;
517 }
518 } else if (GR_GL_TEXTURE_2D != idDesc->fInfo.fTarget) {
519 return false;
520 }
521 return true;
522}
523
524sk_sp<GrTexture> GrGLGpu::onWrapBackendTexture(const GrBackendTexture& backendTex,
Brian Salomond17f6582017-07-19 18:28:58 -0400525 GrWrapOwnership ownership) {
bsalomonb15b4c12014-10-29 12:41:57 -0700526 GrGLTexture::IDDesc idDesc;
Brian Salomond17f6582017-07-19 18:28:58 -0400527 if (!check_backend_texture(backendTex, this->glCaps(), &idDesc)) {
528 return nullptr;
529 }
Greg Daniele7d8da42017-12-04 11:23:19 -0500530 if (!idDesc.fInfo.fFormat) {
531 idDesc.fInfo.fFormat = this->glCaps().configSizedInternalFormat(backendTex.config());
532 }
Brian Salomond17f6582017-07-19 18:28:58 -0400533 if (kBorrow_GrWrapOwnership == ownership) {
534 idDesc.fOwnership = GrBackendObjectOwnership::kBorrowed;
535 } else {
536 idDesc.fOwnership = GrBackendObjectOwnership::kOwned;
537 }
bsalomone5286e02016-01-14 09:24:09 -0800538
Brian Salomond17f6582017-07-19 18:28:58 -0400539 GrSurfaceDesc surfDesc;
540 surfDesc.fFlags = kNone_GrSurfaceFlags;
541 surfDesc.fWidth = backendTex.width();
542 surfDesc.fHeight = backendTex.height();
543 surfDesc.fConfig = backendTex.config();
Brian Salomonbdecacf2018-02-02 20:32:49 -0500544 surfDesc.fSampleCnt = 1;
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400545
Greg Daniel177e6952017-10-12 12:27:11 -0400546 GrMipMapsStatus mipMapsStatus = backendTex.hasMipMaps() ? GrMipMapsStatus::kValid
547 : GrMipMapsStatus::kNotAllocated;
548
549 return GrGLTexture::MakeWrapped(this, surfDesc, mipMapsStatus, idDesc);
Brian Salomond17f6582017-07-19 18:28:58 -0400550}
551
552sk_sp<GrTexture> GrGLGpu::onWrapRenderableBackendTexture(const GrBackendTexture& backendTex,
Brian Salomond17f6582017-07-19 18:28:58 -0400553 int sampleCnt,
554 GrWrapOwnership ownership) {
555 GrGLTexture::IDDesc idDesc;
556 if (!check_backend_texture(backendTex, this->glCaps(), &idDesc)) {
bsalomone5286e02016-01-14 09:24:09 -0800557 return nullptr;
bsalomon7ea33f52015-11-22 14:51:00 -0800558 }
Greg Daniele7d8da42017-12-04 11:23:19 -0500559 if (!idDesc.fInfo.fFormat) {
560 idDesc.fInfo.fFormat = this->glCaps().configSizedInternalFormat(backendTex.config());
561 }
bsalomone5286e02016-01-14 09:24:09 -0800562
Brian Salomond17f6582017-07-19 18:28:58 -0400563 // We don't support rendering to a EXTERNAL texture.
564 if (GR_GL_TEXTURE_EXTERNAL == idDesc.fInfo.fTarget) {
bsalomona98419b2015-11-23 07:09:50 -0800565 return nullptr;
566 }
bsalomon10528f12015-10-14 12:54:52 -0700567
Brian Osman766fcbb2017-03-13 09:33:09 -0400568 if (kBorrow_GrWrapOwnership == ownership) {
Brian Osmana6953f22017-03-10 20:14:05 +0000569 idDesc.fOwnership = GrBackendObjectOwnership::kBorrowed;
Brian Osman766fcbb2017-03-13 09:33:09 -0400570 } else {
571 idDesc.fOwnership = GrBackendObjectOwnership::kOwned;
bsalomone5286e02016-01-14 09:24:09 -0800572 }
bsalomonb15b4c12014-10-29 12:41:57 -0700573
Ben Wagner18b61f92016-10-25 10:44:02 -0400574 GrSurfaceDesc surfDesc;
Brian Salomond17f6582017-07-19 18:28:58 -0400575 surfDesc.fFlags = kRenderTarget_GrSurfaceFlag;
Greg Daniel7ef28f32017-04-20 16:41:55 +0000576 surfDesc.fWidth = backendTex.width();
577 surfDesc.fHeight = backendTex.height();
578 surfDesc.fConfig = backendTex.config();
Brian Salomonbdecacf2018-02-02 20:32:49 -0500579 surfDesc.fSampleCnt = this->caps()->getRenderTargetSampleCount(sampleCnt, backendTex.config());
580 if (surfDesc.fSampleCnt < 1) {
581 return nullptr;
582 }
bsalomon@google.come269f212011-11-07 13:29:52 +0000583
Brian Salomond17f6582017-07-19 18:28:58 -0400584 GrGLRenderTarget::IDDesc rtIDDesc;
585 if (!this->createRenderTargetObjects(surfDesc, idDesc.fInfo, &rtIDDesc)) {
586 return nullptr;
bsalomon@google.come269f212011-11-07 13:29:52 +0000587 }
Greg Daniel177e6952017-10-12 12:27:11 -0400588
589 GrMipMapsStatus mipMapsStatus = backendTex.hasMipMaps() ? GrMipMapsStatus::kDirty
590 : GrMipMapsStatus::kNotAllocated;
591
Brian Salomond17f6582017-07-19 18:28:58 -0400592 sk_sp<GrGLTextureRenderTarget> texRT(
Greg Daniel177e6952017-10-12 12:27:11 -0400593 GrGLTextureRenderTarget::MakeWrapped(this, surfDesc, idDesc, rtIDDesc, mipMapsStatus));
Brian Salomond17f6582017-07-19 18:28:58 -0400594 texRT->baseLevelWasBoundToFBO();
595 return std::move(texRT);
bsalomon@google.come269f212011-11-07 13:29:52 +0000596}
597
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400598sk_sp<GrRenderTarget> GrGLGpu::onWrapBackendRenderTarget(const GrBackendRenderTarget& backendRT) {
Greg Daniel323fbcf2018-04-10 13:46:30 -0400599 GrGLFramebufferInfo info;
600 if (!backendRT.getGLFramebufferInfo(&info)) {
Greg Danielbcf612b2017-05-01 13:50:58 +0000601 return nullptr;
602 }
603
bsalomonb15b4c12014-10-29 12:41:57 -0700604 GrGLRenderTarget::IDDesc idDesc;
Greg Daniel323fbcf2018-04-10 13:46:30 -0400605 idDesc.fRTFBOID = info.fFBOID;
bsalomonb15b4c12014-10-29 12:41:57 -0700606 idDesc.fMSColorRenderbufferID = 0;
egdanield803f272015-03-18 13:01:52 -0700607 idDesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
Brian Osman0b791f52017-03-10 08:30:22 -0500608 idDesc.fRTFBOOwnership = GrBackendObjectOwnership::kBorrowed;
csmartdaltonf9635992016-08-10 11:09:07 -0700609 idDesc.fIsMixedSampled = false;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000610
bsalomonb15b4c12014-10-29 12:41:57 -0700611 GrSurfaceDesc desc;
Brian Salomon0ec981b2017-05-15 13:48:50 -0400612 desc.fFlags = kRenderTarget_GrSurfaceFlag;
Greg Danielbcf612b2017-05-01 13:50:58 +0000613 desc.fWidth = backendRT.width();
614 desc.fHeight = backendRT.height();
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400615 desc.fConfig = backendRT.config();
Brian Salomonbdecacf2018-02-02 20:32:49 -0500616 desc.fSampleCnt =
617 this->caps()->getRenderTargetSampleCount(backendRT.sampleCnt(), backendRT.config());
bsalomonb15b4c12014-10-29 12:41:57 -0700618
Greg Danielbcf612b2017-05-01 13:50:58 +0000619 return GrGLRenderTarget::MakeWrapped(this, desc, idDesc, backendRT.stencilBits());
bsalomon@google.come269f212011-11-07 13:29:52 +0000620}
621
Greg Daniel7ef28f32017-04-20 16:41:55 +0000622sk_sp<GrRenderTarget> GrGLGpu::onWrapBackendTextureAsRenderTarget(const GrBackendTexture& tex,
Greg Daniel7ef28f32017-04-20 16:41:55 +0000623 int sampleCnt) {
Greg Daniel52e16d92018-04-10 09:34:07 -0400624 GrGLTextureInfo info;
625 if (!tex.getGLTextureInfo(&info) || !info.fID) {
ericrkf7b8b8a2016-02-24 14:49:51 -0800626 return nullptr;
627 }
ericrkf7b8b8a2016-02-24 14:49:51 -0800628
Greg Daniel52e16d92018-04-10 09:34:07 -0400629 if (GR_GL_TEXTURE_RECTANGLE != info.fTarget &&
630 GR_GL_TEXTURE_2D != info.fTarget) {
ericrkf7b8b8a2016-02-24 14:49:51 -0800631 // Only texture rectangle and texture 2d are supported. We do not check whether texture
632 // rectangle is supported by Skia - if the caller provided us with a texture rectangle,
633 // we assume the necessary support exists.
634 return nullptr;
635 }
636
Ben Wagner18b61f92016-10-25 10:44:02 -0400637 GrSurfaceDesc surfDesc;
Greg Daniel7ef28f32017-04-20 16:41:55 +0000638 surfDesc.fFlags = kRenderTarget_GrSurfaceFlag;
639 surfDesc.fWidth = tex.width();
640 surfDesc.fHeight = tex.height();
641 surfDesc.fConfig = tex.config();
Brian Salomonbdecacf2018-02-02 20:32:49 -0500642 surfDesc.fSampleCnt = this->caps()->getRenderTargetSampleCount(sampleCnt, tex.config());
ericrkf7b8b8a2016-02-24 14:49:51 -0800643
644 GrGLRenderTarget::IDDesc rtIDDesc;
Greg Daniel52e16d92018-04-10 09:34:07 -0400645 if (!this->createRenderTargetObjects(surfDesc, info, &rtIDDesc)) {
ericrkf7b8b8a2016-02-24 14:49:51 -0800646 return nullptr;
647 }
bungeman6bd52842016-10-27 09:30:08 -0700648 return GrGLRenderTarget::MakeWrapped(this, surfDesc, rtIDDesc, 0);
ericrkf7b8b8a2016-02-24 14:49:51 -0800649}
650
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000651////////////////////////////////////////////////////////////////////////////////
ericrkf7b8b8a2016-02-24 14:49:51 -0800652
Brian Salomonc320b152018-02-20 14:05:36 -0500653bool GrGLGpu::onGetWritePixelsInfo(GrSurface* dstSurface, GrSurfaceOrigin dstOrigin, int width,
654 int height, GrColorType srcColorType,
bsalomonf0674512015-07-28 13:26:15 -0700655 DrawPreference* drawPreference,
656 WritePixelTempDrawInfo* tempDrawInfo) {
Brian Salomonc320b152018-02-20 14:05:36 -0500657 // We don't want to introduce a sRGB conversion if we trigger a draw.
658 auto srcConfigSRGBEncoded = GrPixelConfigIsSRGBEncoded(dstSurface->config());
Brian Salomon9b009bb2018-02-14 13:53:55 -0500659 if (*drawPreference != kNoDraw_DrawPreference) {
660 // We assume the base class has only inserted a draw for sRGB reasons. So the temp surface
661 // has the config of the original src data. There is no swizzling nor src config spoofing.
Brian Salomonc320b152018-02-20 14:05:36 -0500662 SkASSERT(tempDrawInfo->fWriteColorType == srcColorType);
663 SkASSERT(GrPixelConfigToColorType(tempDrawInfo->fTempSurfaceDesc.fConfig) == srcColorType);
Brian Salomon9b009bb2018-02-14 13:53:55 -0500664 SkASSERT(tempDrawInfo->fSwizzle == GrSwizzle::RGBA());
Brian Salomonc320b152018-02-20 14:05:36 -0500665 // Don't undo a sRGB conversion introduced by our caller via an intermediate draw.
666 srcConfigSRGBEncoded = GrPixelConfigIsSRGBEncoded(tempDrawInfo->fTempSurfaceDesc.fConfig);
667 }
668 if (GrColorTypeIsAlphaOnly(srcColorType)) {
669 srcConfigSRGBEncoded = GrSRGBEncoded::kNo;
Brian Salomon9b009bb2018-02-14 13:53:55 -0500670 }
671
Brian Salomon9bada542017-06-12 12:09:30 -0400672 if (SkToBool(dstSurface->asRenderTarget())) {
673 if (this->glCaps().useDrawInsteadOfAllRenderTargetWrites()) {
674 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
675 }
676 }
677
678 GrGLTexture* texture = static_cast<GrGLTexture*>(dstSurface->asTexture());
679
680 if (texture) {
bsalomone5286e02016-01-14 09:24:09 -0800681 if (GR_GL_TEXTURE_EXTERNAL == texture->target()) {
682 // We don't currently support writing pixels to EXTERNAL textures.
bsalomon7ea33f52015-11-22 14:51:00 -0800683 return false;
684 }
Brian Salomon9bada542017-06-12 12:09:30 -0400685 if (GrPixelConfigIsUnorm(texture->config()) && texture->hasBaseLevelBeenBoundToFBO() &&
686 this->glCaps().disallowTexSubImageForUnormConfigTexturesEverBoundToFBO() &&
687 (width < dstSurface->width() || height < dstSurface->height())) {
688 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
689 }
690 } else {
691 // This subclass only allows writes to textures. If the dst is not a texture we have to draw
692 // into it. We could use glDrawPixels on GLs that have it, but we don't today.
693 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
bsalomon6cb3cbe2015-07-30 07:34:27 -0700694 }
695
Brian Osman33910292017-04-18 14:38:53 -0400696 // If the dst is MSAA, we have to draw, or we'll just be writing to the resolve target.
Brian Salomonbdecacf2018-02-02 20:32:49 -0500697 if (dstSurface->asRenderTarget() && dstSurface->asRenderTarget()->numColorSamples() > 1) {
Brian Osman33910292017-04-18 14:38:53 -0400698 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
699 }
700
Brian Salomonc320b152018-02-20 14:05:36 -0500701 auto srcAsConfig = GrColorTypeToPixelConfig(srcColorType, srcConfigSRGBEncoded);
702 SkASSERT(srcAsConfig != kUnknown_GrPixelConfig);
703 auto dstColorType = GrPixelConfigToColorType(dstSurface->config());
704 bool configsAreRBSwaps = GrPixelConfigSwapRAndB(srcAsConfig) == dstSurface->config();
bsalomonf0674512015-07-28 13:26:15 -0700705
706 if (configsAreRBSwaps) {
Brian Salomonc320b152018-02-20 14:05:36 -0500707 if (!this->caps()->isConfigTexturable(srcAsConfig)) {
bsalomonf0674512015-07-28 13:26:15 -0700708 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
709 tempDrawInfo->fTempSurfaceDesc.fConfig = dstSurface->config();
bsalomon6c9cd552016-01-22 07:17:34 -0800710 tempDrawInfo->fSwizzle = GrSwizzle::BGRA();
Brian Salomonc320b152018-02-20 14:05:36 -0500711 tempDrawInfo->fWriteColorType = dstColorType;
bsalomon88c7b982015-07-31 11:20:16 -0700712 } else if (this->glCaps().rgba8888PixelsOpsAreSlow() &&
Brian Salomonc320b152018-02-20 14:05:36 -0500713 kRGBA_8888_GrPixelConfig == srcAsConfig) {
bsalomonf0674512015-07-28 13:26:15 -0700714 ElevateDrawPreference(drawPreference, kGpuPrefersDraw_DrawPreference);
715 tempDrawInfo->fTempSurfaceDesc.fConfig = dstSurface->config();
bsalomon6c9cd552016-01-22 07:17:34 -0800716 tempDrawInfo->fSwizzle = GrSwizzle::BGRA();
Brian Salomonc320b152018-02-20 14:05:36 -0500717 tempDrawInfo->fWriteColorType = dstColorType;
bsalomonf0674512015-07-28 13:26:15 -0700718 } else if (kGLES_GrGLStandard == this->glStandard() &&
719 this->glCaps().bgraIsInternalFormat()) {
720 // The internal format and external formats must match texture uploads so we can't
721 // swizzle while uploading when BGRA is a distinct internal format.
722 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
723 tempDrawInfo->fTempSurfaceDesc.fConfig = dstSurface->config();
bsalomon6c9cd552016-01-22 07:17:34 -0800724 tempDrawInfo->fSwizzle = GrSwizzle::BGRA();
Brian Salomonc320b152018-02-20 14:05:36 -0500725 tempDrawInfo->fWriteColorType = dstColorType;
bsalomonf0674512015-07-28 13:26:15 -0700726 }
727 }
728
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400729 if (!this->glCaps().unpackFlipYSupport() && kBottomLeft_GrSurfaceOrigin == dstOrigin) {
bsalomonf0674512015-07-28 13:26:15 -0700730 ElevateDrawPreference(drawPreference, kGpuPrefersDraw_DrawPreference);
731 }
732
733 return true;
734}
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000735
Brian Salomonc320b152018-02-20 14:05:36 -0500736static bool check_write_and_transfer_input(GrGLTexture* glTex) {
bsalomon6cb3cbe2015-07-30 07:34:27 -0700737 if (!glTex) {
738 return false;
739 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000740
bsalomone5286e02016-01-14 09:24:09 -0800741 // Write or transfer of pixels is not implemented for TEXTURE_EXTERNAL textures
742 if (GR_GL_TEXTURE_EXTERNAL == glTex->target()) {
bsalomon7ea33f52015-11-22 14:51:00 -0800743 return false;
744 }
745
jvanverth17aa0472016-01-05 10:41:27 -0800746 return true;
747}
748
Brian Salomon9b009bb2018-02-14 13:53:55 -0500749bool GrGLGpu::onWritePixels(GrSurface* surface, GrSurfaceOrigin origin, int left, int top,
Brian Salomonc320b152018-02-20 14:05:36 -0500750 int width, int height, GrColorType srcColorType,
Brian Salomon9b009bb2018-02-14 13:53:55 -0500751 const GrMipLevel texels[], int mipLevelCount) {
Brian Salomonc320b152018-02-20 14:05:36 -0500752 auto glTex = static_cast<GrGLTexture*>(surface->asTexture());
jvanverth17aa0472016-01-05 10:41:27 -0800753
Brian Salomonc320b152018-02-20 14:05:36 -0500754 if (!check_write_and_transfer_input(glTex)) {
jvanverth17aa0472016-01-05 10:41:27 -0800755 return false;
756 }
757
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +0000758 this->setScratchTextureUnit();
bsalomon10528f12015-10-14 12:54:52 -0700759 GL_CALL(BindTexture(glTex->target(), glTex->textureID()));
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000760
Brian Salomonc320b152018-02-20 14:05:36 -0500761 // No sRGB transformation occurs in uploadTexData. We choose to make the src config match the
762 // srgb-ness of the surface to avoid issues in ES2 where internal/external formats must match.
763 // When we're on ES2 and the dst is GL_SRGB_ALPHA by making the config be kSRGB_8888 we know
764 // that our caps will choose GL_SRGB_ALPHA as the external format, too. On ES3 or regular GL our
765 // caps knows to make the external format be GL_RGBA.
766 auto srgbEncoded = GrPixelConfigIsSRGBEncoded(surface->config());
767 auto srcAsConfig = GrColorTypeToPixelConfig(srcColorType, srgbEncoded);
Brian Salomon9b009bb2018-02-14 13:53:55 -0500768 return this->uploadTexData(glTex->config(), glTex->width(), glTex->height(), origin,
769 glTex->target(), kWrite_UploadType, left, top, width, height,
Brian Salomonc320b152018-02-20 14:05:36 -0500770 srcAsConfig, texels, mipLevelCount);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000771}
772
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400773// For GL_[UN]PACK_ALIGNMENT. TODO: This really wants to be GrColorType.
bsalomonf46a1242015-12-15 12:37:38 -0800774static inline GrGLint config_alignment(GrPixelConfig config) {
bsalomonf46a1242015-12-15 12:37:38 -0800775 switch (config) {
776 case kAlpha_8_GrPixelConfig:
Greg Danielef59d872017-11-17 16:47:21 -0500777 case kAlpha_8_as_Alpha_GrPixelConfig:
778 case kAlpha_8_as_Red_GrPixelConfig:
Brian Osman986563b2017-01-10 14:20:02 -0500779 case kGray_8_GrPixelConfig:
Greg Daniel7af060a2017-12-05 16:27:11 -0500780 case kGray_8_as_Lum_GrPixelConfig:
781 case kGray_8_as_Red_GrPixelConfig:
bsalomonf46a1242015-12-15 12:37:38 -0800782 return 1;
783 case kRGB_565_GrPixelConfig:
784 case kRGBA_4444_GrPixelConfig:
785 case kAlpha_half_GrPixelConfig:
Greg Danielef59d872017-11-17 16:47:21 -0500786 case kAlpha_half_as_Red_GrPixelConfig:
bsalomonf46a1242015-12-15 12:37:38 -0800787 case kRGBA_half_GrPixelConfig:
788 return 2;
789 case kRGBA_8888_GrPixelConfig:
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400790 case kRGB_888_GrPixelConfig: // We're really talking about GrColorType::kRGB_888x here.
bsalomonf46a1242015-12-15 12:37:38 -0800791 case kBGRA_8888_GrPixelConfig:
792 case kSRGBA_8888_GrPixelConfig:
brianosmana6359362016-03-21 06:55:37 -0700793 case kSBGRA_8888_GrPixelConfig:
Brian Osman10fc6fd2018-03-02 11:01:10 -0500794 case kRGBA_1010102_GrPixelConfig:
bsalomonf46a1242015-12-15 12:37:38 -0800795 case kRGBA_float_GrPixelConfig:
csmartdalton6aa0e112017-02-08 16:14:11 -0500796 case kRG_float_GrPixelConfig:
bsalomonf46a1242015-12-15 12:37:38 -0800797 return 4;
csmartdalton6aa0e112017-02-08 16:14:11 -0500798 case kUnknown_GrPixelConfig:
bsalomonf46a1242015-12-15 12:37:38 -0800799 return 0;
800 }
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400801 SK_ABORT("Invalid pixel config");
csmartdalton6aa0e112017-02-08 16:14:11 -0500802 return 0;
bsalomonf46a1242015-12-15 12:37:38 -0800803}
804
Brian Salomon9b009bb2018-02-14 13:53:55 -0500805bool GrGLGpu::onTransferPixels(GrTexture* texture, int left, int top, int width, int height,
Brian Salomonc320b152018-02-20 14:05:36 -0500806 GrColorType bufferColorType, GrBuffer* transferBuffer, size_t offset,
Brian Salomon9b009bb2018-02-14 13:53:55 -0500807 size_t rowBytes) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400808 GrGLTexture* glTex = static_cast<GrGLTexture*>(texture);
809 GrPixelConfig texConfig = glTex->config();
810 SkASSERT(this->caps()->isConfigTexturable(texConfig));
811
Brian Salomonc320b152018-02-20 14:05:36 -0500812 if (!check_write_and_transfer_input(glTex)) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400813 return false;
814 }
815
816 if (width <= 0 || width > SK_MaxS32 || height <= 0 || height > SK_MaxS32) {
817 return false;
818 }
819
820 this->setScratchTextureUnit();
821 GL_CALL(BindTexture(glTex->target(), glTex->textureID()));
822
823 SkASSERT(!transferBuffer->isMapped());
824 SkASSERT(!transferBuffer->isCPUBacked());
825 const GrGLBuffer* glBuffer = static_cast<const GrGLBuffer*>(transferBuffer);
826 this->bindBuffer(kXferCpuToGpu_GrBufferType, glBuffer);
827
Greg Daniel660cc992017-06-26 14:55:05 -0400828 SkDEBUGCODE(
829 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
830 SkIRect bounds = SkIRect::MakeWH(texture->width(), texture->height());
831 SkASSERT(bounds.contains(subRect));
832 )
833
Brian Salomonc320b152018-02-20 14:05:36 -0500834 int bpp = GrColorTypeBytesPerPixel(bufferColorType);
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400835 const size_t trimRowBytes = width * bpp;
Greg Daniel660cc992017-06-26 14:55:05 -0400836 if (!rowBytes) {
837 rowBytes = trimRowBytes;
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400838 }
Greg Daniel660cc992017-06-26 14:55:05 -0400839 const void* pixels = (void*)offset;
Bruce Dawson71479b72017-07-05 14:30:20 -0700840 if (width < 0 || height < 0) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400841 return false;
842 }
843
844 bool restoreGLRowLength = false;
845 if (trimRowBytes != rowBytes) {
846 // we should have checked for this support already
847 SkASSERT(this->glCaps().unpackRowLengthSupport());
848 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowBytes / bpp));
849 restoreGLRowLength = true;
850 }
851
852 // Internal format comes from the texture desc.
853 GrGLenum internalFormat;
854 // External format and type come from the upload data.
855 GrGLenum externalFormat;
856 GrGLenum externalType;
Brian Salomonc320b152018-02-20 14:05:36 -0500857 auto bufferAsConfig = GrColorTypeToPixelConfig(bufferColorType, GrSRGBEncoded::kNo);
858 if (!this->glCaps().getTexImageFormats(texConfig, bufferAsConfig, &internalFormat,
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400859 &externalFormat, &externalType)) {
860 return false;
861 }
862
863 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, config_alignment(texConfig)));
864 GL_CALL(TexSubImage2D(glTex->target(),
865 0,
866 left, top,
867 width,
868 height,
869 externalFormat, externalType,
870 pixels));
871
872 if (restoreGLRowLength) {
873 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
874 }
875
876 return true;
877}
878
cblume55f2d2d2016-02-26 13:20:48 -0800879/**
880 * Creates storage space for the texture and fills it with texels.
881 *
Brian Salomond1eaf492017-05-18 10:02:08 -0400882 * @param config Pixel config of the texture.
cblume55f2d2d2016-02-26 13:20:48 -0800883 * @param interface The GL interface in use.
cblume790d5132016-02-29 11:13:29 -0800884 * @param caps The capabilities of the GL device.
brianosman81a84852016-09-12 09:05:14 -0700885 * @param internalFormat The data format used for the internal storage of the texture. May be sized.
886 * @param internalFormatForTexStorage The data format used for the TexStorage API. Must be sized.
cblume55f2d2d2016-02-26 13:20:48 -0800887 * @param externalFormat The data format used for the external storage of the texture.
888 * @param externalType The type of the data used for the external storage of the texture.
889 * @param texels The texel data of the texture being created.
890 * @param baseWidth The width of the texture's base mipmap level
891 * @param baseHeight The height of the texture's base mipmap level
cblume55f2d2d2016-02-26 13:20:48 -0800892 */
Robert Phillips92de6312017-05-23 07:43:48 -0400893static bool allocate_and_populate_texture(GrPixelConfig config,
894 const GrGLInterface& interface,
895 const GrGLCaps& caps,
896 GrGLenum target,
897 GrGLenum internalFormat,
898 GrGLenum internalFormatForTexStorage,
899 GrGLenum externalFormat,
900 GrGLenum externalType,
Robert Phillips590533f2017-07-11 14:22:35 -0400901 const GrMipLevel texels[], int mipLevelCount,
Robert Phillips92de6312017-05-23 07:43:48 -0400902 int baseWidth, int baseHeight) {
cblume55f2d2d2016-02-26 13:20:48 -0800903 CLEAR_ERROR_BEFORE_ALLOC(&interface);
cblume790d5132016-02-29 11:13:29 -0800904
Brian Salomond1eaf492017-05-18 10:02:08 -0400905 bool useTexStorage = caps.isConfigTexSupportEnabled(config);
cblume790d5132016-02-29 11:13:29 -0800906 // We can only use TexStorage if we know we will not later change the storage requirements.
907 // This means if we may later want to add mipmaps, we cannot use TexStorage.
908 // Right now, we cannot know if we will later add mipmaps or not.
909 // The only time we can use TexStorage is when we already have the
Brian Salomon57111332018-02-05 15:55:54 -0500910 // mipmaps.
911 useTexStorage &= mipLevelCount > 1;
cblume790d5132016-02-29 11:13:29 -0800912
913 if (useTexStorage) {
914 // We never resize or change formats of textures.
cblume55f2d2d2016-02-26 13:20:48 -0800915 GL_ALLOC_CALL(&interface,
Robert Phillips590533f2017-07-11 14:22:35 -0400916 TexStorage2D(target, SkTMax(mipLevelCount, 1), internalFormatForTexStorage,
Brian Salomond1eaf492017-05-18 10:02:08 -0400917 baseWidth, baseHeight));
Brian Salomon0ec981b2017-05-15 13:48:50 -0400918 GrGLenum error = CHECK_ALLOC_ERROR(&interface);
cblume55f2d2d2016-02-26 13:20:48 -0800919 if (error != GR_GL_NO_ERROR) {
bsalomone699d0c2016-03-09 06:25:15 -0800920 return false;
cblume790d5132016-02-29 11:13:29 -0800921 } else {
Robert Phillips590533f2017-07-11 14:22:35 -0400922 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
cblume790d5132016-02-29 11:13:29 -0800923 const void* currentMipData = texels[currentMipLevel].fPixels;
924 if (currentMipData == nullptr) {
925 continue;
926 }
927 int twoToTheMipLevel = 1 << currentMipLevel;
Brian Salomond1eaf492017-05-18 10:02:08 -0400928 int currentWidth = SkTMax(1, baseWidth / twoToTheMipLevel);
929 int currentHeight = SkTMax(1, baseHeight / twoToTheMipLevel);
cblume790d5132016-02-29 11:13:29 -0800930
931 GR_GL_CALL(&interface,
932 TexSubImage2D(target,
933 currentMipLevel,
934 0, // left
935 0, // top
936 currentWidth,
937 currentHeight,
938 externalFormat, externalType,
939 currentMipData));
940 }
bsalomone699d0c2016-03-09 06:25:15 -0800941 return true;
cblume790d5132016-02-29 11:13:29 -0800942 }
943 } else {
Robert Phillips590533f2017-07-11 14:22:35 -0400944 if (!mipLevelCount) {
cblume790d5132016-02-29 11:13:29 -0800945 GL_ALLOC_CALL(&interface,
946 TexImage2D(target,
bsalomone699d0c2016-03-09 06:25:15 -0800947 0,
cblume790d5132016-02-29 11:13:29 -0800948 internalFormat,
bsalomone699d0c2016-03-09 06:25:15 -0800949 baseWidth,
950 baseHeight,
cblume790d5132016-02-29 11:13:29 -0800951 0, // border
952 externalFormat, externalType,
bsalomone699d0c2016-03-09 06:25:15 -0800953 nullptr));
Brian Salomon0ec981b2017-05-15 13:48:50 -0400954 GrGLenum error = CHECK_ALLOC_ERROR(&interface);
cblume790d5132016-02-29 11:13:29 -0800955 if (error != GR_GL_NO_ERROR) {
bsalomone699d0c2016-03-09 06:25:15 -0800956 return false;
957 }
958 } else {
Robert Phillips590533f2017-07-11 14:22:35 -0400959 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
bsalomone699d0c2016-03-09 06:25:15 -0800960 int twoToTheMipLevel = 1 << currentMipLevel;
961 int currentWidth = SkTMax(1, baseWidth / twoToTheMipLevel);
962 int currentHeight = SkTMax(1, baseHeight / twoToTheMipLevel);
Greg Daniel8b059bd2017-09-28 20:46:45 +0000963 const void* currentMipData = texels[currentMipLevel].fPixels;
bsalomone699d0c2016-03-09 06:25:15 -0800964 // Even if curremtMipData is nullptr, continue to call TexImage2D.
965 // This will allocate texture memory which we can later populate.
966 GL_ALLOC_CALL(&interface,
967 TexImage2D(target,
968 currentMipLevel,
969 internalFormat,
970 currentWidth,
971 currentHeight,
972 0, // border
973 externalFormat, externalType,
974 currentMipData));
Brian Salomon0ec981b2017-05-15 13:48:50 -0400975 GrGLenum error = CHECK_ALLOC_ERROR(&interface);
bsalomone699d0c2016-03-09 06:25:15 -0800976 if (error != GR_GL_NO_ERROR) {
977 return false;
978 }
cblume790d5132016-02-29 11:13:29 -0800979 }
cblume55f2d2d2016-02-26 13:20:48 -0800980 }
981 }
bsalomone699d0c2016-03-09 06:25:15 -0800982 return true;
cblume55f2d2d2016-02-26 13:20:48 -0800983}
984
985/**
cblume55f2d2d2016-02-26 13:20:48 -0800986 * After a texture is created, any state which was altered during its creation
987 * needs to be restored.
988 *
989 * @param interface The GL interface to use.
990 * @param caps The capabilities of the GL device.
991 * @param restoreGLRowLength Should the row length unpacking be restored?
992 * @param glFlipY Did GL flip the texture vertically?
993 */
994static void restore_pixelstore_state(const GrGLInterface& interface, const GrGLCaps& caps,
995 bool restoreGLRowLength, bool glFlipY) {
996 if (restoreGLRowLength) {
997 SkASSERT(caps.unpackRowLengthSupport());
998 GR_GL_CALL(&interface, PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
999 }
1000 if (glFlipY) {
1001 GR_GL_CALL(&interface, PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
1002 }
1003}
1004
Brian Osman9b560242017-09-05 15:34:52 -04001005void GrGLGpu::unbindCpuToGpuXferBuffer() {
1006 auto& xferBufferState = fHWBufferState[kXferCpuToGpu_GrBufferType];
1007 if (!xferBufferState.fBoundBufferUniqueID.isInvalid()) {
1008 GL_CALL(BindBuffer(xferBufferState.fGLTarget, 0));
1009 xferBufferState.invalidate();
1010 }
1011
1012}
1013
Brian Salomonc320b152018-02-20 14:05:36 -05001014// TODO: Make this take a GrColorType instead of dataConfig. This requires updating GrGLCaps to
1015// convert from GrColorType to externalFormat/externalType GLenum values.
Brian Salomond1eaf492017-05-18 10:02:08 -04001016bool GrGLGpu::uploadTexData(GrPixelConfig texConfig, int texWidth, int texHeight,
1017 GrSurfaceOrigin texOrigin, GrGLenum target, UploadType uploadType,
1018 int left, int top, int width, int height, GrPixelConfig dataConfig,
Greg Daniel834f1202017-10-09 15:06:20 -04001019 const GrMipLevel texels[], int mipLevelCount,
Greg Daniel0fc4d2d2017-10-12 11:23:36 -04001020 GrMipMapsStatus* mipMapsStatus) {
Brian Salomond1eaf492017-05-18 10:02:08 -04001021 SkASSERT(this->caps()->isConfigTexturable(texConfig));
Greg Daniel660cc992017-06-26 14:55:05 -04001022 SkDEBUGCODE(
1023 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
1024 SkIRect bounds = SkIRect::MakeWH(texWidth, texHeight);
1025 SkASSERT(bounds.contains(subRect));
1026 )
Robert Phillips590533f2017-07-11 14:22:35 -04001027 SkASSERT(1 == mipLevelCount ||
Greg Daniel660cc992017-06-26 14:55:05 -04001028 (0 == left && 0 == top && width == texWidth && height == texHeight));
bsalomon5b30c6f2015-12-17 14:17:34 -08001029
Brian Osman9b560242017-09-05 15:34:52 -04001030 this->unbindCpuToGpuXferBuffer();
Jim Van Verth2e5eaf02017-06-21 15:55:46 -04001031
cblume55f2d2d2016-02-26 13:20:48 -08001032 // texels is const.
1033 // But we may need to flip the texture vertically to prepare it.
1034 // Rather than flip in place and alter the incoming data,
1035 // we allocate a new buffer to flip into.
1036 // This means we need to make a non-const shallow copy of texels.
Robert Phillips0f992772017-07-12 08:24:56 -04001037 SkAutoTMalloc<GrMipLevel> texelsShallowCopy;
1038
1039 if (mipLevelCount) {
1040 texelsShallowCopy.reset(mipLevelCount);
1041 memcpy(texelsShallowCopy.get(), texels, mipLevelCount*sizeof(GrMipLevel));
1042 }
cblume55f2d2d2016-02-26 13:20:48 -08001043
cblume55f2d2d2016-02-26 13:20:48 -08001044 const GrGLInterface* interface = this->glInterface();
1045 const GrGLCaps& caps = this->glCaps();
1046
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001047 size_t bpp = GrBytesPerPixel(dataConfig);
cblume55f2d2d2016-02-26 13:20:48 -08001048
1049 if (width == 0 || height == 0) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +00001050 return false;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001051 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00001052
bsalomon5b30c6f2015-12-17 14:17:34 -08001053 // Internal format comes from the texture desc.
bsalomon76148af2016-01-12 11:13:47 -08001054 GrGLenum internalFormat;
bsalomon5b30c6f2015-12-17 14:17:34 -08001055 // External format and type come from the upload data.
bsalomon76148af2016-01-12 11:13:47 -08001056 GrGLenum externalFormat;
1057 GrGLenum externalType;
Brian Salomond1eaf492017-05-18 10:02:08 -04001058 if (!this->glCaps().getTexImageFormats(texConfig, dataConfig, &internalFormat, &externalFormat,
1059 &externalType)) {
bsalomon76148af2016-01-12 11:13:47 -08001060 return false;
1061 }
brianosman81a84852016-09-12 09:05:14 -07001062 // TexStorage requires a sized format, and internalFormat may or may not be
Brian Salomond1eaf492017-05-18 10:02:08 -04001063 GrGLenum internalFormatForTexStorage = this->glCaps().configSizedInternalFormat(texConfig);
brianosman81a84852016-09-12 09:05:14 -07001064
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001065 /*
bsalomon5b30c6f2015-12-17 14:17:34 -08001066 * Check whether to allocate a temporary buffer for flipping y or
bsalomon@google.com6f379512011-11-16 20:36:03 +00001067 * because our srcData has extra bytes past each row. If so, we need
1068 * to trim those off here, since GL ES may not let us specify
1069 * GL_UNPACK_ROW_LENGTH.
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001070 */
bsalomon@google.com6f379512011-11-16 20:36:03 +00001071 bool restoreGLRowLength = false;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +00001072 bool swFlipY = false;
1073 bool glFlipY = false;
cblume55f2d2d2016-02-26 13:20:48 -08001074
Robert Phillips590533f2017-07-11 14:22:35 -04001075 if (kBottomLeft_GrSurfaceOrigin == texOrigin && mipLevelCount) {
cblume55f2d2d2016-02-26 13:20:48 -08001076 if (caps.unpackFlipYSupport()) {
1077 glFlipY = true;
1078 } else {
1079 swFlipY = true;
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001080 }
cblume55f2d2d2016-02-26 13:20:48 -08001081 }
1082
1083 // in case we need a temporary, trimmed copy of the src pixels
1084 SkAutoSMalloc<128 * 128> tempStorage;
1085
Greg Daniel0fc4d2d2017-10-12 11:23:36 -04001086 if (mipMapsStatus) {
1087 *mipMapsStatus = GrMipMapsStatus::kValid;
Greg Daniel834f1202017-10-09 15:06:20 -04001088 }
1089
Robert Phillips931f7512017-11-02 12:57:18 -04001090 const bool usesMips = mipLevelCount > 1;
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
Robert Phillips931f7512017-11-02 12:57:18 -04001094 bool willNeedData = false;
Greg Daniel55afd6d2017-09-29 09:32:44 -04001095 size_t combinedBufferSize = 0;
1096 SkTArray<size_t> individualMipOffsets(mipLevelCount);
Robert Phillips590533f2017-07-11 14:22:35 -04001097 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
Greg Daniel55afd6d2017-09-29 09:32:44 -04001098 if (texelsShallowCopy[currentMipLevel].fPixels) {
1099 int twoToTheMipLevel = 1 << currentMipLevel;
1100 int currentWidth = SkTMax(1, width / twoToTheMipLevel);
1101 int currentHeight = SkTMax(1, height / twoToTheMipLevel);
Robert Phillips931f7512017-11-02 12:57:18 -04001102 const size_t trimRowBytes = currentWidth * bpp;
1103 const size_t trimmedSize = trimRowBytes * currentHeight;
1104
1105 const size_t rowBytes = texelsShallowCopy[currentMipLevel].fRowBytes
1106 ? texelsShallowCopy[currentMipLevel].fRowBytes
1107 : trimRowBytes;
1108
1109
1110 if (((!caps.unpackRowLengthSupport() || usesMips) && trimRowBytes != rowBytes) ||
1111 swFlipY) {
1112 willNeedData = true;
1113 }
1114
Greg Daniel55afd6d2017-09-29 09:32:44 -04001115 individualMipOffsets.push_back(combinedBufferSize);
1116 combinedBufferSize += trimmedSize;
1117 } else {
Greg Daniel0fc4d2d2017-10-12 11:23:36 -04001118 if (mipMapsStatus) {
1119 *mipMapsStatus = GrMipMapsStatus::kDirty;
Greg Daniel834f1202017-10-09 15:06:20 -04001120 }
Greg Daniel55afd6d2017-09-29 09:32:44 -04001121 individualMipOffsets.push_back(0);
1122 }
Greg Daniel0fc4d2d2017-10-12 11:23:36 -04001123 }
1124 if (mipMapsStatus && mipLevelCount <= 1) {
1125 *mipMapsStatus = GrMipMapsStatus::kNotAllocated;
cblume55f2d2d2016-02-26 13:20:48 -08001126 }
Robert Phillips931f7512017-11-02 12:57:18 -04001127 char* buffer = nullptr;
1128 if (willNeedData) {
1129 buffer = (char*)tempStorage.reset(combinedBufferSize);
1130 }
cblume55f2d2d2016-02-26 13:20:48 -08001131
Robert Phillips590533f2017-07-11 14:22:35 -04001132 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
Greg Daniel55afd6d2017-09-29 09:32:44 -04001133 if (!texelsShallowCopy[currentMipLevel].fPixels) {
1134 continue;
1135 }
cblume55f2d2d2016-02-26 13:20:48 -08001136 int twoToTheMipLevel = 1 << currentMipLevel;
1137 int currentWidth = SkTMax(1, width / twoToTheMipLevel);
1138 int currentHeight = SkTMax(1, height / twoToTheMipLevel);
1139 const size_t trimRowBytes = currentWidth * bpp;
1140
1141 /*
1142 * check whether to allocate a temporary buffer for flipping y or
1143 * because our srcData has extra bytes past each row. If so, we need
1144 * to trim those off here, since GL ES may not let us specify
1145 * GL_UNPACK_ROW_LENGTH.
1146 */
1147 restoreGLRowLength = false;
1148
Greg Daniel55afd6d2017-09-29 09:32:44 -04001149 const size_t rowBytes = texelsShallowCopy[currentMipLevel].fRowBytes
1150 ? texelsShallowCopy[currentMipLevel].fRowBytes
1151 : trimRowBytes;
Greg Daniel660cc992017-06-26 14:55:05 -04001152
ericrk154349b2016-05-10 14:36:53 -07001153 // TODO: This optimization should be enabled with or without mips.
1154 // For use with mips, we must set GR_GL_UNPACK_ROW_LENGTH once per
1155 // mip level, before calling glTexImage2D.
ericrk154349b2016-05-10 14:36:53 -07001156 if (caps.unpackRowLengthSupport() && !swFlipY && !usesMips) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001157 // can't use this for flipping, only non-neg values allowed. :(
1158 if (rowBytes != trimRowBytes) {
1159 GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
cblume55f2d2d2016-02-26 13:20:48 -08001160 GR_GL_CALL(interface, PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength));
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001161 restoreGLRowLength = true;
1162 }
Jim Van Verth2e5eaf02017-06-21 15:55:46 -04001163 } else if (trimRowBytes != rowBytes || swFlipY) {
1164 // copy data into our new storage, skipping the trailing bytes
1165 const char* src = (const char*)texelsShallowCopy[currentMipLevel].fPixels;
1166 if (swFlipY && currentHeight >= 1) {
1167 src += (currentHeight - 1) * rowBytes;
Jim Van Verthf7037d02017-06-21 10:52:43 -04001168 }
Greg Daniel55afd6d2017-09-29 09:32:44 -04001169 char* dst = buffer + individualMipOffsets[currentMipLevel];
Jim Van Verth2e5eaf02017-06-21 15:55:46 -04001170 for (int y = 0; y < currentHeight; y++) {
1171 memcpy(dst, src, trimRowBytes);
1172 if (swFlipY) {
1173 src -= rowBytes;
1174 } else {
1175 src += rowBytes;
1176 }
1177 dst += trimRowBytes;
1178 }
1179 // now point data to our copied version
1180 texelsShallowCopy[currentMipLevel].fPixels = buffer +
Greg Daniel55afd6d2017-09-29 09:32:44 -04001181 individualMipOffsets[currentMipLevel];
Jim Van Verth2e5eaf02017-06-21 15:55:46 -04001182 texelsShallowCopy[currentMipLevel].fRowBytes = trimRowBytes;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001183 }
bsalomone699d0c2016-03-09 06:25:15 -08001184 }
1185
Robert Phillips590533f2017-07-11 14:22:35 -04001186 if (mipLevelCount) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001187 if (glFlipY) {
cblume55f2d2d2016-02-26 13:20:48 -08001188 GR_GL_CALL(interface, PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_TRUE));
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001189 }
Brian Salomond1eaf492017-05-18 10:02:08 -04001190 GR_GL_CALL(interface, PixelStorei(GR_GL_UNPACK_ALIGNMENT, config_alignment(texConfig)));
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001191 }
cblume55f2d2d2016-02-26 13:20:48 -08001192
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001193 bool succeeded = true;
Brian Salomond1eaf492017-05-18 10:02:08 -04001194 if (kNewTexture_UploadType == uploadType) {
1195 if (0 == left && 0 == top && texWidth == width && texHeight == height) {
Robert Phillips92de6312017-05-23 07:43:48 -04001196 succeeded = allocate_and_populate_texture(
Brian Salomond1eaf492017-05-18 10:02:08 -04001197 texConfig, *interface, caps, target, internalFormat,
Robert Phillips590533f2017-07-11 14:22:35 -04001198 internalFormatForTexStorage, externalFormat, externalType,
1199 texelsShallowCopy, mipLevelCount, width, height);
Brian Salomond1eaf492017-05-18 10:02:08 -04001200 } else {
1201 succeeded = false;
1202 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001203 } else {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +00001204 if (swFlipY || glFlipY) {
Brian Salomond1eaf492017-05-18 10:02:08 -04001205 top = texHeight - (top + height);
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001206 }
Robert Phillips590533f2017-07-11 14:22:35 -04001207 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
Greg Daniel55afd6d2017-09-29 09:32:44 -04001208 if (!texelsShallowCopy[currentMipLevel].fPixels) {
1209 continue;
1210 }
cblume55f2d2d2016-02-26 13:20:48 -08001211 int twoToTheMipLevel = 1 << currentMipLevel;
1212 int currentWidth = SkTMax(1, width / twoToTheMipLevel);
1213 int currentHeight = SkTMax(1, height / twoToTheMipLevel);
cblume55f2d2d2016-02-26 13:20:48 -08001214
1215 GL_CALL(TexSubImage2D(target,
1216 currentMipLevel,
1217 left, top,
1218 currentWidth,
1219 currentHeight,
1220 externalFormat, externalType,
1221 texelsShallowCopy[currentMipLevel].fPixels));
1222 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00001223 }
1224
cblume55f2d2d2016-02-26 13:20:48 -08001225 restore_pixelstore_state(*interface, caps, restoreGLRowLength, glFlipY);
1226
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001227 return succeeded;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001228}
1229
bsalomon424cc262015-05-22 10:37:30 -07001230static bool renderbuffer_storage_msaa(const GrGLContext& ctx,
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001231 int sampleCount,
1232 GrGLenum format,
1233 int width, int height) {
robertphillips@google.com6177e692013-02-28 20:16:25 +00001234 CLEAR_ERROR_BEFORE_ALLOC(ctx.interface());
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +00001235 SkASSERT(GrGLCaps::kNone_MSFBOType != ctx.caps()->msFBOType());
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +00001236 switch (ctx.caps()->msFBOType()) {
Brian Salomon00731b42016-10-14 11:30:51 -04001237 case GrGLCaps::kStandard_MSFBOType:
vbuzinovdded6962015-06-12 08:59:45 -07001238 case GrGLCaps::kMixedSamples_MSFBOType:
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001239 GL_ALLOC_CALL(ctx.interface(),
1240 RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
1241 sampleCount,
1242 format,
1243 width, height));
1244 break;
1245 case GrGLCaps::kES_Apple_MSFBOType:
1246 GL_ALLOC_CALL(ctx.interface(),
1247 RenderbufferStorageMultisampleES2APPLE(GR_GL_RENDERBUFFER,
1248 sampleCount,
1249 format,
1250 width, height));
1251 break;
1252 case GrGLCaps::kES_EXT_MsToTexture_MSFBOType:
1253 case GrGLCaps::kES_IMG_MsToTexture_MSFBOType:
1254 GL_ALLOC_CALL(ctx.interface(),
1255 RenderbufferStorageMultisampleES2EXT(GR_GL_RENDERBUFFER,
1256 sampleCount,
1257 format,
1258 width, height));
1259 break;
1260 case GrGLCaps::kNone_MSFBOType:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -04001261 SK_ABORT("Shouldn't be here if we don't support multisampled renderbuffers.");
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001262 break;
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001263 }
Brian Salomon9251d4e2015-03-17 16:03:19 -04001264 return (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctx.interface()));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001265}
1266
egdanielb0e1be22015-04-22 13:27:39 -07001267bool GrGLGpu::createRenderTargetObjects(const GrSurfaceDesc& desc,
bsalomon091f60c2015-11-10 11:54:56 -08001268 const GrGLTextureInfo& texInfo,
bsalomonb15b4c12014-10-29 12:41:57 -07001269 GrGLRenderTarget::IDDesc* idDesc) {
1270 idDesc->fMSColorRenderbufferID = 0;
egdanield803f272015-03-18 13:01:52 -07001271 idDesc->fRTFBOID = 0;
kkinnunen2e6055b2016-04-22 01:48:29 -07001272 idDesc->fRTFBOOwnership = GrBackendObjectOwnership::kOwned;
egdanield803f272015-03-18 13:01:52 -07001273 idDesc->fTexFBOID = 0;
robertphillips76948d42016-05-04 12:47:41 -07001274 SkASSERT((GrGLCaps::kMixedSamples_MSFBOType == this->glCaps().msFBOType()) ==
1275 this->caps()->usesMixedSamples());
Brian Salomonbdecacf2018-02-02 20:32:49 -05001276 idDesc->fIsMixedSampled = desc.fSampleCnt > 1 && this->caps()->usesMixedSamples();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001277
1278 GrGLenum status;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001279
bsalomona11e5fc2015-12-18 07:59:41 -08001280 GrGLenum colorRenderbufferFormat = 0; // suppress warning
bsalomon@google.comab15d612011-08-09 12:57:56 +00001281
Brian Salomonbdecacf2018-02-02 20:32:49 -05001282 if (desc.fSampleCnt > 1 && GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.com347c3822013-05-01 20:10:01 +00001283 goto FAILED;
1284 }
1285
egdanield803f272015-03-18 13:01:52 -07001286 GL_CALL(GenFramebuffers(1, &idDesc->fTexFBOID));
1287 if (!idDesc->fTexFBOID) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001288 goto FAILED;
1289 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001290
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001291 // If we are using multisampling we will create two FBOS. We render to one and then resolve to
1292 // the texture bound to the other. The exception is the IMG multisample extension. With this
1293 // extension the texture is multisampled when rendered to and then auto-resolves it when it is
1294 // rendered from.
Brian Salomonbdecacf2018-02-02 20:32:49 -05001295 if (desc.fSampleCnt > 1 && this->glCaps().usesMSAARenderBuffers()) {
egdanield803f272015-03-18 13:01:52 -07001296 GL_CALL(GenFramebuffers(1, &idDesc->fRTFBOID));
bsalomonb15b4c12014-10-29 12:41:57 -07001297 GL_CALL(GenRenderbuffers(1, &idDesc->fMSColorRenderbufferID));
egdanield803f272015-03-18 13:01:52 -07001298 if (!idDesc->fRTFBOID ||
bsalomona11e5fc2015-12-18 07:59:41 -08001299 !idDesc->fMSColorRenderbufferID) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001300 goto FAILED;
1301 }
bsalomon76148af2016-01-12 11:13:47 -08001302 if (!this->glCaps().getRenderbufferFormat(desc.fConfig, &colorRenderbufferFormat)) {
1303 return false;
1304 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001305 } else {
egdanield803f272015-03-18 13:01:52 -07001306 idDesc->fRTFBOID = idDesc->fTexFBOID;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001307 }
1308
egdanield803f272015-03-18 13:01:52 -07001309 // below here we may bind the FBO
Robert Phillips294870f2016-11-11 12:38:40 -05001310 fHWBoundRenderTargetUniqueID.makeInvalid();
egdanield803f272015-03-18 13:01:52 -07001311 if (idDesc->fRTFBOID != idDesc->fTexFBOID) {
Brian Salomonbdecacf2018-02-02 20:32:49 -05001312 SkASSERT(desc.fSampleCnt > 1);
bsalomonb15b4c12014-10-29 12:41:57 -07001313 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, idDesc->fMSColorRenderbufferID));
bsalomon424cc262015-05-22 10:37:30 -07001314 if (!renderbuffer_storage_msaa(*fGLContext,
bsalomonb15b4c12014-10-29 12:41:57 -07001315 desc.fSampleCnt,
bsalomona11e5fc2015-12-18 07:59:41 -08001316 colorRenderbufferFormat,
bsalomonb15b4c12014-10-29 12:41:57 -07001317 desc.fWidth, desc.fHeight)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001318 goto FAILED;
1319 }
Adrienne Walker4ee88512018-05-17 11:37:14 -07001320 this->bindFramebuffer(GR_GL_FRAMEBUFFER, idDesc->fRTFBOID);
egdanield803f272015-03-18 13:01:52 -07001321 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon10528f12015-10-14 12:54:52 -07001322 GR_GL_COLOR_ATTACHMENT0,
1323 GR_GL_RENDERBUFFER,
1324 idDesc->fMSColorRenderbufferID));
Brian Salomon0ec981b2017-05-15 13:48:50 -04001325 if (!this->glCaps().isConfigVerifiedColorAttachment(desc.fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001326 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1327 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
1328 goto FAILED;
1329 }
bsalomon424cc262015-05-22 10:37:30 -07001330 fGLContext->caps()->markConfigAsValidColorAttachment(desc.fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001331 }
1332 }
Adrienne Walker4ee88512018-05-17 11:37:14 -07001333 this->bindFramebuffer(GR_GL_FRAMEBUFFER, idDesc->fTexFBOID);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001334
Brian Salomonbdecacf2018-02-02 20:32:49 -05001335 if (this->glCaps().usesImplicitMSAAResolve() && desc.fSampleCnt > 1) {
egdanield803f272015-03-18 13:01:52 -07001336 GL_CALL(FramebufferTexture2DMultisample(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001337 GR_GL_COLOR_ATTACHMENT0,
bsalomon091f60c2015-11-10 11:54:56 -08001338 texInfo.fTarget,
1339 texInfo.fID, 0, desc.fSampleCnt));
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001340 } else {
egdanield803f272015-03-18 13:01:52 -07001341 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001342 GR_GL_COLOR_ATTACHMENT0,
bsalomon091f60c2015-11-10 11:54:56 -08001343 texInfo.fTarget,
1344 texInfo.fID, 0));
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001345 }
Brian Salomon0ec981b2017-05-15 13:48:50 -04001346 if (!this->glCaps().isConfigVerifiedColorAttachment(desc.fConfig)) {
egdanield803f272015-03-18 13:01:52 -07001347 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001348 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
1349 goto FAILED;
1350 }
bsalomon424cc262015-05-22 10:37:30 -07001351 fGLContext->caps()->markConfigAsValidColorAttachment(desc.fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001352 }
1353
1354 return true;
1355
1356FAILED:
bsalomonb15b4c12014-10-29 12:41:57 -07001357 if (idDesc->fMSColorRenderbufferID) {
1358 GL_CALL(DeleteRenderbuffers(1, &idDesc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001359 }
egdanield803f272015-03-18 13:01:52 -07001360 if (idDesc->fRTFBOID != idDesc->fTexFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -07001361 this->deleteFramebuffer(idDesc->fRTFBOID);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001362 }
egdanield803f272015-03-18 13:01:52 -07001363 if (idDesc->fTexFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -07001364 this->deleteFramebuffer(idDesc->fTexFBOID);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001365 }
1366 return false;
1367}
1368
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001369// good to set a break-point here to know when createTexture fails
Robert Phillips67d52cf2017-06-05 13:38:13 -04001370static sk_sp<GrTexture> return_null_texture() {
mtklein@google.com330313a2013-08-22 15:37:26 +00001371// SkDEBUGFAIL("null texture");
halcanary96fcdcc2015-08-27 07:41:13 -07001372 return nullptr;
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001373}
1374
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +00001375#if 0 && defined(SK_DEBUG)
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001376static size_t as_size_t(int x) {
1377 return x;
1378}
1379#endif
1380
cblume55f2d2d2016-02-26 13:20:48 -08001381static void set_initial_texture_params(const GrGLInterface* interface,
1382 const GrGLTextureInfo& info,
1383 GrGLTexture::TexParams* initialTexParams) {
1384 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
1385 // drivers have a bug where an FBO won't be complete if it includes a
1386 // texture that is not mipmap complete (considering the filter in use).
1387 // we only set a subset here so invalidate first
1388 initialTexParams->invalidate();
1389 initialTexParams->fMinFilter = GR_GL_NEAREST;
1390 initialTexParams->fMagFilter = GR_GL_NEAREST;
1391 initialTexParams->fWrapS = GR_GL_CLAMP_TO_EDGE;
1392 initialTexParams->fWrapT = GR_GL_CLAMP_TO_EDGE;
1393 GR_GL_CALL(interface, TexParameteri(info.fTarget,
1394 GR_GL_TEXTURE_MAG_FILTER,
1395 initialTexParams->fMagFilter));
1396 GR_GL_CALL(interface, TexParameteri(info.fTarget,
1397 GR_GL_TEXTURE_MIN_FILTER,
1398 initialTexParams->fMinFilter));
1399 GR_GL_CALL(interface, TexParameteri(info.fTarget,
1400 GR_GL_TEXTURE_WRAP_S,
1401 initialTexParams->fWrapS));
1402 GR_GL_CALL(interface, TexParameteri(info.fTarget,
1403 GR_GL_TEXTURE_WRAP_T,
1404 initialTexParams->fWrapT));
1405}
1406
Robert Phillips67d52cf2017-06-05 13:38:13 -04001407sk_sp<GrTexture> GrGLGpu::onCreateTexture(const GrSurfaceDesc& desc,
1408 SkBudgeted budgeted,
Robert Phillips590533f2017-07-11 14:22:35 -04001409 const GrMipLevel texels[],
1410 int mipLevelCount) {
bsalomon@google.com8a70eef2013-03-19 13:58:55 +00001411 // We fail if the MSAA was requested and is not available.
Brian Salomonbdecacf2018-02-02 20:32:49 -05001412 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType() && desc.fSampleCnt > 1) {
tfarina38406c82014-10-31 07:11:12 -07001413 //SkDebugf("MSAA RT requested but not supported on this platform.");
bsalomon@google.com8a70eef2013-03-19 13:58:55 +00001414 return return_null_texture();
1415 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001416
Brian Salomond17b4a62017-05-23 16:53:47 -04001417 bool performClear = (desc.fFlags & kPerformInitialClear_GrSurfaceFlag);
Brian Salomond17b4a62017-05-23 16:53:47 -04001418
Robert Phillips590533f2017-07-11 14:22:35 -04001419 GrMipLevel zeroLevel;
Brian Salomond17b4a62017-05-23 16:53:47 -04001420 std::unique_ptr<uint8_t[]> zeros;
Brian Salomond17b4a62017-05-23 16:53:47 -04001421 if (performClear && !this->glCaps().clearTextureSupport() &&
Brian Salomon57111332018-02-05 15:55:54 -05001422 !this->glCaps().canConfigBeFBOColorAttachment(desc.fConfig)) {
Brian Salomond17b4a62017-05-23 16:53:47 -04001423 size_t rowSize = GrBytesPerPixel(desc.fConfig) * desc.fWidth;
1424 size_t size = rowSize * desc.fHeight;
1425 zeros.reset(new uint8_t[size]);
1426 memset(zeros.get(), 0, size);
Robert Phillips590533f2017-07-11 14:22:35 -04001427 zeroLevel.fPixels = zeros.get();
1428 zeroLevel.fRowBytes = 0;
1429 texels = &zeroLevel;
1430 mipLevelCount = 1;
Brian Salomond17b4a62017-05-23 16:53:47 -04001431 performClear = false;
1432 }
1433
Brian Salomond34edf32017-05-19 15:45:48 -04001434 bool isRenderTarget = SkToBool(desc.fFlags & kRenderTarget_GrSurfaceFlag);
reed@google.comac10a2d2010-12-22 21:39:39 +00001435
bsalomonb15b4c12014-10-29 12:41:57 -07001436 GrGLTexture::IDDesc idDesc;
kkinnunen2e6055b2016-04-22 01:48:29 -07001437 idDesc.fOwnership = GrBackendObjectOwnership::kOwned;
Greg Daniel0fc4d2d2017-10-12 11:23:36 -04001438 GrMipMapsStatus mipMapsStatus;
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001439 GrGLTexture::TexParams initialTexParams;
Brian Salomon58389b92018-03-07 13:01:25 -05001440 if (!this->createTextureImpl(desc, &idDesc.fInfo, isRenderTarget, &initialTexParams, texels,
1441 mipLevelCount, &mipMapsStatus)) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001442 return return_null_texture();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001443 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001444
Robert Phillips67d52cf2017-06-05 13:38:13 -04001445 sk_sp<GrGLTexture> tex;
Brian Salomond34edf32017-05-19 15:45:48 -04001446 if (isRenderTarget) {
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001447 // unbind the texture from the texture unit before binding it to the frame buffer
bsalomon091f60c2015-11-10 11:54:56 -08001448 GL_CALL(BindTexture(idDesc.fInfo.fTarget, 0));
bsalomon5236cf42015-01-14 10:42:08 -08001449 GrGLRenderTarget::IDDesc rtIDDesc;
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001450
kkinnunen2e6055b2016-04-22 01:48:29 -07001451 if (!this->createRenderTargetObjects(desc, idDesc.fInfo, &rtIDDesc)) {
bsalomon091f60c2015-11-10 11:54:56 -08001452 GL_CALL(DeleteTextures(1, &idDesc.fInfo.fID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001453 return return_null_texture();
1454 }
Robert Phillips67d52cf2017-06-05 13:38:13 -04001455 tex = sk_make_sp<GrGLTextureRenderTarget>(this, budgeted, desc, idDesc, rtIDDesc,
Greg Daniel0fc4d2d2017-10-12 11:23:36 -04001456 mipMapsStatus);
Brian Salomon9bada542017-06-12 12:09:30 -04001457 tex->baseLevelWasBoundToFBO();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001458 } else {
Greg Daniel0fc4d2d2017-10-12 11:23:36 -04001459 tex = sk_make_sp<GrGLTexture>(this, budgeted, desc, idDesc, mipMapsStatus);
reed@google.comac10a2d2010-12-22 21:39:39 +00001460 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001461 tex->setCachedTexParams(initialTexParams, this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00001462#ifdef TRACE_TEXTURE_CREATION
tfarina38406c82014-10-31 07:11:12 -07001463 SkDebugf("--- new texture [%d] size=(%d %d) config=%d\n",
brianosmanc4459f62016-07-19 08:02:20 -07001464 idDesc.fInfo.fID, desc.fWidth, desc.fHeight, desc.fConfig);
reed@google.comac10a2d2010-12-22 21:39:39 +00001465#endif
Brian Salomond17b4a62017-05-23 16:53:47 -04001466 if (tex && performClear) {
1467 if (this->glCaps().clearTextureSupport()) {
Brian Salomond17b4a62017-05-23 16:53:47 -04001468 static constexpr uint32_t kZero = 0;
Brian Salomon57111332018-02-05 15:55:54 -05001469 GL_CALL(ClearTexImage(tex->textureID(), 0, GR_GL_RGBA, GR_GL_UNSIGNED_BYTE, &kZero));
Brian Salomond17b4a62017-05-23 16:53:47 -04001470 } else {
Brian Salomond17b4a62017-05-23 16:53:47 -04001471 GrGLIRect viewport;
Robert Phillips67d52cf2017-06-05 13:38:13 -04001472 this->bindSurfaceFBOForPixelOps(tex.get(), GR_GL_FRAMEBUFFER, &viewport,
1473 kDst_TempFBOTarget);
Brian Salomond17b4a62017-05-23 16:53:47 -04001474 this->disableScissor();
1475 this->disableWindowRectangles();
1476 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
1477 fHWWriteToColor = kYes_TriState;
1478 GL_CALL(ClearColor(0, 0, 0, 0));
1479 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
Robert Phillips67d52cf2017-06-05 13:38:13 -04001480 this->unbindTextureFBOForPixelOps(GR_GL_FRAMEBUFFER, tex.get());
Brian Salomond17b4a62017-05-23 16:53:47 -04001481 fHWBoundRenderTargetUniqueID.makeInvalid();
1482 }
1483 }
Kevin Lubickf7621cb2018-04-16 15:51:44 -04001484 return std::move(tex);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001485}
1486
1487namespace {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001488
egdaniel8dc7c3a2015-04-16 11:22:42 -07001489const GrGLuint kUnknownBitCount = GrGLStencilAttachment::kUnknownBitCount;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001490
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001491void inline get_stencil_rb_sizes(const GrGLInterface* gl,
egdaniel8dc7c3a2015-04-16 11:22:42 -07001492 GrGLStencilAttachment::Format* format) {
sugoi@google.com6ba0b0f2013-05-03 13:52:32 +00001493
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001494 // we shouldn't ever know one size and not the other
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001495 SkASSERT((kUnknownBitCount == format->fStencilBits) ==
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001496 (kUnknownBitCount == format->fTotalBits));
1497 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001498 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001499 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1500 (GrGLint*)&format->fStencilBits);
1501 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001502 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001503 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1504 (GrGLint*)&format->fTotalBits);
1505 format->fTotalBits += format->fStencilBits;
1506 } else {
1507 format->fTotalBits = format->fStencilBits;
1508 }
1509 }
1510}
1511}
1512
egdanielff1d5472015-09-10 08:37:20 -07001513int GrGLGpu::getCompatibleStencilIndex(GrPixelConfig config) {
bsalomon100b8f82015-10-28 08:37:44 -07001514 static const int kSize = 16;
Brian Salomonbdecacf2018-02-02 20:32:49 -05001515 SkASSERT(this->caps()->isConfigRenderable(config));
bsalomon30447372015-12-21 09:03:05 -08001516 if (!this->glCaps().hasStencilFormatBeenDeterminedForConfig(config)) {
1517 // Default to unsupported, set this if we find a stencil format that works.
1518 int firstWorkingStencilFormatIndex = -1;
Brian Osman91f9a2c2017-09-05 15:02:46 -04001519
egdanielff1d5472015-09-10 08:37:20 -07001520 // Create color texture
kkinnunen546eb5c2015-12-11 00:05:33 -08001521 GrGLuint colorID = 0;
egdanielff1d5472015-09-10 08:37:20 -07001522 GL_CALL(GenTextures(1, &colorID));
1523 this->setScratchTextureUnit();
1524 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, colorID));
1525 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1526 GR_GL_TEXTURE_MAG_FILTER,
1527 GR_GL_NEAREST));
1528 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1529 GR_GL_TEXTURE_MIN_FILTER,
1530 GR_GL_NEAREST));
1531 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1532 GR_GL_TEXTURE_WRAP_S,
1533 GR_GL_CLAMP_TO_EDGE));
1534 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1535 GR_GL_TEXTURE_WRAP_T,
1536 GR_GL_CLAMP_TO_EDGE));
1537
bsalomon76148af2016-01-12 11:13:47 -08001538 GrGLenum internalFormat;
1539 GrGLenum externalFormat;
1540 GrGLenum externalType;
1541 if (!this->glCaps().getTexImageFormats(config, config, &internalFormat, &externalFormat,
1542 &externalType)) {
1543 return false;
1544 }
Brian Osman9b560242017-09-05 15:34:52 -04001545 this->unbindCpuToGpuXferBuffer();
egdanielff1d5472015-09-10 08:37:20 -07001546 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1547 GL_ALLOC_CALL(this->glInterface(), TexImage2D(GR_GL_TEXTURE_2D,
bsalomon926cb022015-12-17 18:15:11 -08001548 0,
bsalomon76148af2016-01-12 11:13:47 -08001549 internalFormat,
bsalomon100b8f82015-10-28 08:37:44 -07001550 kSize,
1551 kSize,
egdanielff1d5472015-09-10 08:37:20 -07001552 0,
bsalomon76148af2016-01-12 11:13:47 -08001553 externalFormat,
1554 externalType,
Ben Wagnera93a14a2017-08-28 10:34:05 -04001555 nullptr));
bsalomon30447372015-12-21 09:03:05 -08001556 if (GR_GL_NO_ERROR != CHECK_ALLOC_ERROR(this->glInterface())) {
egdanielff1d5472015-09-10 08:37:20 -07001557 GL_CALL(DeleteTextures(1, &colorID));
bsalomon30447372015-12-21 09:03:05 -08001558 return -1;
egdanielff1d5472015-09-10 08:37:20 -07001559 }
1560
1561 // unbind the texture from the texture unit before binding it to the frame buffer
1562 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, 0));
1563
1564 // Create Framebuffer
kkinnunen546eb5c2015-12-11 00:05:33 -08001565 GrGLuint fb = 0;
egdanielff1d5472015-09-10 08:37:20 -07001566 GL_CALL(GenFramebuffers(1, &fb));
Adrienne Walker4ee88512018-05-17 11:37:14 -07001567 this->bindFramebuffer(GR_GL_FRAMEBUFFER, fb);
Robert Phillips294870f2016-11-11 12:38:40 -05001568 fHWBoundRenderTargetUniqueID.makeInvalid();
egdanielff1d5472015-09-10 08:37:20 -07001569 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
1570 GR_GL_COLOR_ATTACHMENT0,
1571 GR_GL_TEXTURE_2D,
1572 colorID,
1573 0));
bsalomon30447372015-12-21 09:03:05 -08001574 GrGLuint sbRBID = 0;
1575 GL_CALL(GenRenderbuffers(1, &sbRBID));
egdanielff1d5472015-09-10 08:37:20 -07001576
1577 // look over formats till I find a compatible one
1578 int stencilFmtCnt = this->glCaps().stencilFormats().count();
bsalomon30447372015-12-21 09:03:05 -08001579 if (sbRBID) {
egdanielff1d5472015-09-10 08:37:20 -07001580 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbRBID));
bsalomon30447372015-12-21 09:03:05 -08001581 for (int i = 0; i < stencilFmtCnt && sbRBID; ++i) {
1582 const GrGLCaps::StencilFormat& sFmt = this->glCaps().stencilFormats()[i];
1583 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1584 GL_ALLOC_CALL(this->glInterface(), RenderbufferStorage(GR_GL_RENDERBUFFER,
1585 sFmt.fInternalFormat,
1586 kSize, kSize));
1587 if (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(this->glInterface())) {
egdanielff1d5472015-09-10 08:37:20 -07001588 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon30447372015-12-21 09:03:05 -08001589 GR_GL_STENCIL_ATTACHMENT,
egdanielff1d5472015-09-10 08:37:20 -07001590 GR_GL_RENDERBUFFER, sbRBID));
bsalomon30447372015-12-21 09:03:05 -08001591 if (sFmt.fPacked) {
1592 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1593 GR_GL_DEPTH_ATTACHMENT,
1594 GR_GL_RENDERBUFFER, sbRBID));
1595 } else {
1596 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1597 GR_GL_DEPTH_ATTACHMENT,
1598 GR_GL_RENDERBUFFER, 0));
1599 }
1600 GrGLenum status;
1601 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1602 if (status == GR_GL_FRAMEBUFFER_COMPLETE) {
1603 firstWorkingStencilFormatIndex = i;
1604 break;
1605 }
egdanielff1d5472015-09-10 08:37:20 -07001606 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1607 GR_GL_STENCIL_ATTACHMENT,
1608 GR_GL_RENDERBUFFER, 0));
1609 if (sFmt.fPacked) {
1610 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1611 GR_GL_DEPTH_ATTACHMENT,
1612 GR_GL_RENDERBUFFER, 0));
1613 }
egdanielff1d5472015-09-10 08:37:20 -07001614 }
1615 }
bsalomon30447372015-12-21 09:03:05 -08001616 GL_CALL(DeleteRenderbuffers(1, &sbRBID));
egdanielff1d5472015-09-10 08:37:20 -07001617 }
1618 GL_CALL(DeleteTextures(1, &colorID));
Adrienne Walker4ee88512018-05-17 11:37:14 -07001619 this->bindFramebuffer(GR_GL_FRAMEBUFFER, 0);
1620 this->deleteFramebuffer(fb);
bsalomon30447372015-12-21 09:03:05 -08001621 fGLContext->caps()->setStencilFormatIndexForConfig(config, firstWorkingStencilFormatIndex);
egdanielff1d5472015-09-10 08:37:20 -07001622 }
bsalomon30447372015-12-21 09:03:05 -08001623 return this->glCaps().getStencilFormatIndexForConfig(config);
egdanielff1d5472015-09-10 08:37:20 -07001624}
1625
Brian Salomon2a4f9832018-03-03 22:43:43 -05001626bool GrGLGpu::createTextureImpl(const GrSurfaceDesc& desc, GrGLTextureInfo* info, bool renderTarget,
Brian Salomon58389b92018-03-07 13:01:25 -05001627 GrGLTexture::TexParams* initialTexParams, const GrMipLevel texels[],
Brian Salomon2a4f9832018-03-03 22:43:43 -05001628 int mipLevelCount, GrMipMapsStatus* mipMapsStatus) {
erikchen9a1ed5d2016-02-10 16:32:34 -08001629 info->fID = 0;
1630 info->fTarget = GR_GL_TEXTURE_2D;
1631 GL_CALL(GenTextures(1, &(info->fID)));
1632
1633 if (!info->fID) {
1634 return false;
1635 }
1636
1637 this->setScratchTextureUnit();
1638 GL_CALL(BindTexture(info->fTarget, info->fID));
1639
1640 if (renderTarget && this->glCaps().textureUsageSupport()) {
1641 // provides a hint about how this texture will be used
1642 GL_CALL(TexParameteri(info->fTarget,
1643 GR_GL_TEXTURE_USAGE,
1644 GR_GL_FRAMEBUFFER_ATTACHMENT));
1645 }
1646
cblume55f2d2d2016-02-26 13:20:48 -08001647 if (info) {
1648 set_initial_texture_params(this->glInterface(), *info, initialTexParams);
1649 }
Brian Salomon2a4f9832018-03-03 22:43:43 -05001650
Brian Salomon58389b92018-03-07 13:01:25 -05001651 if (!this->uploadTexData(desc.fConfig, desc.fWidth, desc.fHeight, kTopLeft_GrSurfaceOrigin,
1652 info->fTarget, kNewTexture_UploadType, 0, 0, desc.fWidth, desc.fHeight,
1653 desc.fConfig, texels, mipLevelCount, mipMapsStatus)) {
erikchen9a1ed5d2016-02-10 16:32:34 -08001654 GL_CALL(DeleteTextures(1, &(info->fID)));
1655 return false;
1656 }
Greg Daniele7d8da42017-12-04 11:23:19 -05001657 info->fFormat = this->glCaps().configSizedInternalFormat(desc.fConfig);
erikchen9a1ed5d2016-02-10 16:32:34 -08001658 return true;
1659}
1660
egdanielec00d942015-09-14 12:56:10 -07001661GrStencilAttachment* GrGLGpu::createStencilAttachmentForRenderTarget(const GrRenderTarget* rt,
Brian Salomond1eaf492017-05-18 10:02:08 -04001662 int width, int height) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001663 SkASSERT(width >= rt->width());
1664 SkASSERT(height >= rt->height());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001665
vbuzinovdded6962015-06-12 08:59:45 -07001666 int samples = rt->numStencilSamples();
egdaniel8dc7c3a2015-04-16 11:22:42 -07001667 GrGLStencilAttachment::IDDesc sbDesc;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001668
egdanielff1d5472015-09-10 08:37:20 -07001669 int sIdx = this->getCompatibleStencilIndex(rt->config());
bsalomon62a627b2015-12-17 09:50:47 -08001670 if (sIdx < 0) {
egdanielec00d942015-09-14 12:56:10 -07001671 return nullptr;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001672 }
egdanielff1d5472015-09-10 08:37:20 -07001673
1674 if (!sbDesc.fRenderbufferID) {
1675 GL_CALL(GenRenderbuffers(1, &sbDesc.fRenderbufferID));
1676 }
1677 if (!sbDesc.fRenderbufferID) {
egdanielec00d942015-09-14 12:56:10 -07001678 return nullptr;
egdanielff1d5472015-09-10 08:37:20 -07001679 }
1680 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbDesc.fRenderbufferID));
1681 const GrGLCaps::StencilFormat& sFmt = this->glCaps().stencilFormats()[sIdx];
1682 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1683 // we do this "if" so that we don't call the multisample
1684 // version on a GL that doesn't have an MSAA extension.
Brian Salomonbdecacf2018-02-02 20:32:49 -05001685 if (samples > 1) {
egdanielff1d5472015-09-10 08:37:20 -07001686 SkAssertResult(renderbuffer_storage_msaa(*fGLContext,
1687 samples,
1688 sFmt.fInternalFormat,
1689 width, height));
1690 } else {
1691 GL_ALLOC_CALL(this->glInterface(), RenderbufferStorage(GR_GL_RENDERBUFFER,
1692 sFmt.fInternalFormat,
1693 width, height));
Brian Salomon0ec981b2017-05-15 13:48:50 -04001694 SkASSERT(GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(this->glInterface()));
egdanielff1d5472015-09-10 08:37:20 -07001695 }
1696 fStats.incStencilAttachmentCreates();
1697 // After sized formats we attempt an unsized format and take
1698 // whatever sizes GL gives us. In that case we query for the size.
1699 GrGLStencilAttachment::Format format = sFmt;
1700 get_stencil_rb_sizes(this->glInterface(), &format);
egdanielec00d942015-09-14 12:56:10 -07001701 GrGLStencilAttachment* stencil = new GrGLStencilAttachment(this,
1702 sbDesc,
1703 width,
1704 height,
1705 samples,
1706 format);
1707 return stencil;
reed@google.comac10a2d2010-12-22 21:39:39 +00001708}
1709
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001710////////////////////////////////////////////////////////////////////////////////
1711
jvanverth73063dc2015-12-03 09:15:47 -08001712// GL_STREAM_DRAW triggers an optimization in Chromium's GPU process where a client's vertex buffer
1713// objects are implemented as client-side-arrays on tile-deferred architectures.
1714#define DYNAMIC_USAGE_PARAM GR_GL_STREAM_DRAW
1715
cdaltone2e71c22016-04-07 18:13:29 -07001716GrBuffer* GrGLGpu::onCreateBuffer(size_t size, GrBufferType intendedType,
cdalton1bf3e712016-04-19 10:00:02 -07001717 GrAccessPattern accessPattern, const void* data) {
1718 return GrGLBuffer::Create(this, size, intendedType, accessPattern, data);
jvanverth73063dc2015-12-03 09:15:47 -08001719}
1720
bsalomon3e791242014-12-17 13:43:13 -08001721void GrGLGpu::flushScissor(const GrScissorState& scissorState,
joshualitt77b13072014-10-27 14:51:01 -07001722 const GrGLIRect& rtViewport,
1723 GrSurfaceOrigin rtOrigin) {
robertphillipse85a32d2015-02-10 08:16:55 -08001724 if (scissorState.enabled()) {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001725 GrGLIRect scissor;
Robert Phillips16d8ec62017-07-27 16:16:25 -04001726 scissor.setRelativeTo(rtViewport, scissorState.rect(), rtOrigin);
bsalomon@google.coma3201942012-06-21 19:58:20 +00001727 // if the scissor fully contains the viewport then we fall through and
1728 // disable the scissor test.
bsalomonb0bd4f62014-09-03 07:19:50 -07001729 if (!scissor.contains(rtViewport)) {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001730 if (fHWScissorSettings.fRect != scissor) {
1731 scissor.pushToGLScissor(this->glInterface());
1732 fHWScissorSettings.fRect = scissor;
1733 }
1734 if (kYes_TriState != fHWScissorSettings.fEnabled) {
1735 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1736 fHWScissorSettings.fEnabled = kYes_TriState;
1737 }
1738 return;
1739 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001740 }
joshualitt77b13072014-10-27 14:51:01 -07001741
1742 // See fall through note above
1743 this->disableScissor();
1744}
1745
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001746void GrGLGpu::flushWindowRectangles(const GrWindowRectsState& windowState,
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001747 const GrGLRenderTarget* rt, GrSurfaceOrigin origin) {
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001748#ifndef USE_NSIGHT
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001749 typedef GrWindowRectsState::Mode Mode;
1750 SkASSERT(!windowState.enabled() || rt->renderFBOID()); // Window rects can't be used on-screen.
1751 SkASSERT(windowState.numWindows() <= this->caps()->maxWindowRectangles());
csmartdalton28341fa2016-08-17 10:00:21 -07001752
Jim Van Verth6a40abc2017-11-02 16:56:09 +00001753 if (!this->caps()->maxWindowRectangles() ||
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001754 fHWWindowRectsState.knownEqualTo(origin, rt->getViewport(), windowState)) {
csmartdalton28341fa2016-08-17 10:00:21 -07001755 return;
csmartdalton28341fa2016-08-17 10:00:21 -07001756 }
1757
csmartdalton7535f412016-08-23 06:51:00 -07001758 // This is purely a workaround for a spurious warning generated by gcc. Otherwise the above
1759 // assert would be sufficient. https://gcc.gnu.org/bugzilla/show_bug.cgi?id=5912
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001760 int numWindows = SkTMin(windowState.numWindows(), int(GrWindowRectangles::kMaxWindows));
1761 SkASSERT(windowState.numWindows() == numWindows);
csmartdalton7535f412016-08-23 06:51:00 -07001762
csmartdalton28341fa2016-08-17 10:00:21 -07001763 GrGLIRect glwindows[GrWindowRectangles::kMaxWindows];
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001764 const SkIRect* skwindows = windowState.windows().data();
csmartdalton7535f412016-08-23 06:51:00 -07001765 for (int i = 0; i < numWindows; ++i) {
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001766 glwindows[i].setRelativeTo(rt->getViewport(), skwindows[i], origin);
csmartdalton28341fa2016-08-17 10:00:21 -07001767 }
1768
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001769 GrGLenum glmode = (Mode::kExclusive == windowState.mode()) ? GR_GL_EXCLUSIVE : GR_GL_INCLUSIVE;
csmartdalton7535f412016-08-23 06:51:00 -07001770 GL_CALL(WindowRectangles(glmode, numWindows, glwindows->asInts()));
csmartdalton28341fa2016-08-17 10:00:21 -07001771
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001772 fHWWindowRectsState.set(origin, rt->getViewport(), windowState);
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001773#endif
csmartdalton28341fa2016-08-17 10:00:21 -07001774}
1775
1776void GrGLGpu::disableWindowRectangles() {
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001777#ifndef USE_NSIGHT
Jim Van Verth6a40abc2017-11-02 16:56:09 +00001778 if (!this->caps()->maxWindowRectangles() || fHWWindowRectsState.knownDisabled()) {
csmartdalton28341fa2016-08-17 10:00:21 -07001779 return;
1780 }
1781 GL_CALL(WindowRectangles(GR_GL_EXCLUSIVE, 0, nullptr));
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001782 fHWWindowRectsState.setDisabled();
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001783#endif
csmartdalton28341fa2016-08-17 10:00:21 -07001784}
1785
ethannicholas28ef4452016-03-25 09:26:03 -07001786void GrGLGpu::flushMinSampleShading(float minSampleShading) {
1787 if (fHWMinSampleShading != minSampleShading) {
1788 if (minSampleShading > 0.0) {
1789 GL_CALL(Enable(GR_GL_SAMPLE_SHADING));
1790 GL_CALL(MinSampleShading(minSampleShading));
1791 }
1792 else {
1793 GL_CALL(Disable(GR_GL_SAMPLE_SHADING));
1794 }
1795 fHWMinSampleShading = minSampleShading;
1796 }
1797}
1798
bsalomon2eda5b32016-09-21 10:53:24 -07001799bool GrGLGpu::flushGLState(const GrPipeline& pipeline, const GrPrimitiveProcessor& primProc,
1800 bool willDrawPoints) {
Hal Canary144caf52016-11-07 17:57:18 -05001801 sk_sp<GrGLProgram> program(fProgramCache->refProgram(this, pipeline, primProc, willDrawPoints));
brianosman33f6b3f2016-06-02 05:49:21 -07001802 if (!program) {
1803 GrCapsDebugf(this->caps(), "Failed to create program!\n");
1804 return false;
1805 }
1806
1807 program->generateMipmaps(primProc, pipeline);
1808
egdaniel080e6732014-12-22 07:35:52 -08001809 GrXferProcessor::BlendInfo blendInfo;
egdaniel0e1853c2016-03-17 11:35:45 -07001810 pipeline.getXferProcessor().getBlendInfo(&blendInfo);
egdaniel080e6732014-12-22 07:35:52 -08001811
egdaniel080e6732014-12-22 07:35:52 -08001812 this->flushColorWrite(blendInfo.fWriteColor);
ethannicholas28ef4452016-03-25 09:26:03 -07001813 this->flushMinSampleShading(primProc.getSampleShading());
bsalomonbc3d0de2014-12-15 13:45:03 -08001814
bsalomon6df86402015-06-01 10:41:49 -07001815 GrGLuint programID = program->programID();
bsalomon1f78c0a2014-12-17 09:43:13 -08001816 if (fHWProgramID != programID) {
1817 GL_CALL(UseProgram(programID));
1818 fHWProgramID = programID;
1819 }
1820
egdanield803f272015-03-18 13:01:52 -07001821 if (blendInfo.fWriteColor) {
bsalomon7f9b2e42016-01-12 13:29:26 -08001822 // Swizzle the blend to match what the shader will output.
Brian Salomon1edc5b92016-11-29 13:43:46 -05001823 const GrSwizzle& swizzle = this->caps()->shaderCaps()->configOutputSwizzle(
Robert Phillips2890fbf2017-07-26 15:48:41 -04001824 pipeline.proxy()->config());
bsalomon7f9b2e42016-01-12 13:29:26 -08001825 this->flushBlend(blendInfo, swizzle);
egdanield803f272015-03-18 13:01:52 -07001826 }
bsalomon1f78c0a2014-12-17 09:43:13 -08001827
cdalton74b8d322016-04-11 14:47:28 -07001828 program->setData(primProc, pipeline);
bsalomon1f78c0a2014-12-17 09:43:13 -08001829
Robert Phillips2890fbf2017-07-26 15:48:41 -04001830 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(pipeline.renderTarget());
csmartdaltonc633abb2016-11-01 08:55:55 -07001831 GrStencilSettings stencil;
1832 if (pipeline.isStencilEnabled()) {
1833 // TODO: attach stencil and create settings during render target flush.
1834 SkASSERT(glRT->renderTargetPriv().getStencilAttachment());
1835 stencil.reset(*pipeline.getUserStencil(), pipeline.hasStencilClip(),
1836 glRT->renderTargetPriv().numStencilBits());
1837 }
1838 this->flushStencil(stencil);
Robert Phillips16d8ec62017-07-27 16:16:25 -04001839 this->flushScissor(pipeline.getScissorState(), glRT->getViewport(), pipeline.proxy()->origin());
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001840 this->flushWindowRectangles(pipeline.getWindowRectsState(), glRT, pipeline.proxy()->origin());
csmartdaltondabc91b2016-11-15 14:26:27 -07001841 this->flushHWAAState(glRT, pipeline.isHWAntialiasState(), !stencil.isDisabled());
bsalomonbc3d0de2014-12-15 13:45:03 -08001842
1843 // This must come after textures are flushed because a texture may need
egdanield803f272015-03-18 13:01:52 -07001844 // to be msaa-resolved (which will modify bound FBO state).
Brian Salomon1fabd512018-02-09 09:54:25 -05001845 this->flushRenderTarget(glRT, pipeline.getDisableOutputConversionToSRGB());
bsalomonbc3d0de2014-12-15 13:45:03 -08001846
1847 return true;
1848}
1849
joshualitt873ad0e2015-01-20 09:08:51 -08001850void GrGLGpu::setupGeometry(const GrPrimitiveProcessor& primProc,
Chris Daltonff926502017-05-03 14:36:54 -04001851 const GrBuffer* indexBuffer,
1852 const GrBuffer* vertexBuffer,
Chris Dalton1d616352017-05-31 12:51:23 -06001853 int baseVertex,
1854 const GrBuffer* instanceBuffer,
1855 int baseInstance) {
Chris Dalton27059d32018-01-23 14:06:50 -07001856 using EnablePrimitiveRestart = GrGLAttribArrayState::EnablePrimitiveRestart;
1857
cdaltone2e71c22016-04-07 18:13:29 -07001858 GrGLAttribArrayState* attribState;
Chris Daltonff926502017-05-03 14:36:54 -04001859 if (indexBuffer) {
Chris Dalton1d616352017-05-31 12:51:23 -06001860 SkASSERT(indexBuffer && !indexBuffer->isMapped());
Chris Daltonff926502017-05-03 14:36:54 -04001861 attribState = fHWVertexArrayState.bindInternalVertexArray(this, indexBuffer);
cdaltone2e71c22016-04-07 18:13:29 -07001862 } else {
1863 attribState = fHWVertexArrayState.bindInternalVertexArray(this);
bsalomonbc3d0de2014-12-15 13:45:03 -08001864 }
bsalomonbc3d0de2014-12-15 13:45:03 -08001865
Chris Dalton1d616352017-05-31 12:51:23 -06001866 struct {
1867 const GrBuffer* fBuffer;
1868 int fStride;
1869 size_t fBufferOffset;
1870 } bindings[2];
Chris Dalton8e45b4f2017-05-05 14:00:56 -04001871
Chris Dalton1d616352017-05-31 12:51:23 -06001872 if (int vertexStride = primProc.getVertexStride()) {
1873 SkASSERT(vertexBuffer && !vertexBuffer->isMapped());
1874 bindings[0].fBuffer = vertexBuffer;
1875 bindings[0].fStride = vertexStride;
1876 bindings[0].fBufferOffset = vertexBuffer->baseOffset() + baseVertex * vertexStride;
1877 }
1878 if (int instanceStride = primProc.getInstanceStride()) {
1879 SkASSERT(instanceBuffer && !instanceBuffer->isMapped());
1880 bindings[1].fBuffer = instanceBuffer;
1881 bindings[1].fStride = instanceStride;
1882 bindings[1].fBufferOffset = instanceBuffer->baseOffset() + baseInstance * instanceStride;
1883 }
bsalomonbc3d0de2014-12-15 13:45:03 -08001884
Chris Dalton1d616352017-05-31 12:51:23 -06001885 int numAttribs = primProc.numAttribs();
Chris Dalton27059d32018-01-23 14:06:50 -07001886 auto enableRestart = EnablePrimitiveRestart(primProc.willUsePrimitiveRestart() && indexBuffer);
1887 attribState->enableVertexArrays(this, numAttribs, enableRestart);
bsalomonbc3d0de2014-12-15 13:45:03 -08001888
Chris Dalton1d616352017-05-31 12:51:23 -06001889 for (int i = 0; i < numAttribs; ++i) {
1890 using InputRate = GrPrimitiveProcessor::Attribute::InputRate;
1891 const GrGeometryProcessor::Attribute& attrib = primProc.getAttrib(i);
Brian Salomon70132d02018-05-29 15:33:06 -04001892 const int divisor = InputRate::kPerInstance == attrib.inputRate() ? 1 : 0;
Chris Dalton1d616352017-05-31 12:51:23 -06001893 const auto& binding = bindings[divisor];
Brian Salomon70132d02018-05-29 15:33:06 -04001894 attribState->set(this, i, binding.fBuffer, attrib.type(), binding.fStride,
1895 binding.fBufferOffset + attrib.offsetInRecord(), divisor);
bsalomonbc3d0de2014-12-15 13:45:03 -08001896 }
1897}
1898
csmartdalton485a1202016-07-13 10:16:32 -07001899GrGLenum GrGLGpu::bindBuffer(GrBufferType type, const GrBuffer* buffer) {
joshualitt93316b92015-10-23 09:08:08 -07001900 this->handleDirtyContext();
cdaltondeacc972016-04-06 14:26:33 -07001901
cdaltone2e71c22016-04-07 18:13:29 -07001902 // Index buffer state is tied to the vertex array.
1903 if (kIndex_GrBufferType == type) {
1904 this->bindVertexArray(0);
cdaltondeacc972016-04-06 14:26:33 -07001905 }
cdaltone2e71c22016-04-07 18:13:29 -07001906
1907 SkASSERT(type >= 0 && type <= kLast_GrBufferType);
1908 auto& bufferState = fHWBufferState[type];
1909
robertphillips8abb3702016-08-31 14:04:06 -07001910 if (buffer->uniqueID() != bufferState.fBoundBufferUniqueID) {
csmartdalton485a1202016-07-13 10:16:32 -07001911 if (buffer->isCPUBacked()) {
1912 if (!bufferState.fBufferZeroKnownBound) {
1913 GL_CALL(BindBuffer(bufferState.fGLTarget, 0));
1914 }
1915 } else {
1916 const GrGLBuffer* glBuffer = static_cast<const GrGLBuffer*>(buffer);
1917 GL_CALL(BindBuffer(bufferState.fGLTarget, glBuffer->bufferID()));
cdaltone2e71c22016-04-07 18:13:29 -07001918 }
csmartdalton485a1202016-07-13 10:16:32 -07001919 bufferState.fBufferZeroKnownBound = buffer->isCPUBacked();
robertphillips8abb3702016-08-31 14:04:06 -07001920 bufferState.fBoundBufferUniqueID = buffer->uniqueID();
cdaltone2e71c22016-04-07 18:13:29 -07001921 }
1922
1923 return bufferState.fGLTarget;
joshualitt93316b92015-10-23 09:08:08 -07001924}
1925
cdalton74b8d322016-04-11 14:47:28 -07001926void GrGLGpu::notifyBufferReleased(const GrGLBuffer* buffer) {
1927 if (buffer->hasAttachedToTexture()) {
1928 // Detach this buffer from any textures to ensure the underlying memory is freed.
Robert Phillips294870f2016-11-11 12:38:40 -05001929 GrGpuResource::UniqueID uniqueID = buffer->uniqueID();
cdalton74b8d322016-04-11 14:47:28 -07001930 for (int i = fHWMaxUsedBufferTextureUnit; i >= 0; --i) {
1931 auto& buffTex = fHWBufferTextures[i];
1932 if (uniqueID != buffTex.fAttachedBufferUniqueID) {
1933 continue;
1934 }
1935 if (i == fHWMaxUsedBufferTextureUnit) {
1936 --fHWMaxUsedBufferTextureUnit;
1937 }
1938
1939 this->setTextureUnit(i);
1940 if (!buffTex.fKnownBound) {
1941 SkASSERT(buffTex.fTextureID);
1942 GL_CALL(BindTexture(GR_GL_TEXTURE_BUFFER, buffTex.fTextureID));
1943 buffTex.fKnownBound = true;
1944 }
1945 GL_CALL(TexBuffer(GR_GL_TEXTURE_BUFFER,
1946 this->glCaps().configSizedInternalFormat(buffTex.fTexelConfig), 0));
1947 }
1948 }
1949}
1950
bsalomon861e1032014-12-16 07:33:49 -08001951void GrGLGpu::disableScissor() {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001952 if (kNo_TriState != fHWScissorSettings.fEnabled) {
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001953 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.coma3201942012-06-21 19:58:20 +00001954 fHWScissorSettings.fEnabled = kNo_TriState;
1955 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001956 }
1957}
1958
Robert Phillips19e51dc2017-08-09 09:30:51 -04001959void GrGLGpu::clear(const GrFixedClip& clip, GrColor color,
1960 GrRenderTarget* target, GrSurfaceOrigin origin) {
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001961 // parent class should never let us get here with no RT
bsalomon49f085d2014-09-05 13:34:00 -07001962 SkASSERT(target);
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001963
Brian Salomon43f8bf02017-10-18 08:33:29 -04001964 this->handleDirtyContext();
bsalomon@google.com74b98712011-11-11 19:46:16 +00001965
1966 GrGLfloat r, g, b, a;
1967 static const GrGLfloat scale255 = 1.f / 255.f;
1968 a = GrColorUnpackA(color) * scale255;
1969 GrGLfloat scaleRGB = scale255;
bsalomon@google.com74b98712011-11-11 19:46:16 +00001970 r = GrColorUnpackR(color) * scaleRGB;
1971 g = GrColorUnpackG(color) * scaleRGB;
1972 b = GrColorUnpackB(color) * scaleRGB;
1973
Brian Salomon43f8bf02017-10-18 08:33:29 -04001974 if (this->glCaps().useDrawToClearColor()) {
1975 this->clearColorAsDraw(clip, r, g, b, a, target, origin);
1976 return;
1977 }
1978
1979 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
1980
Brian Salomon1fabd512018-02-09 09:54:25 -05001981 if (clip.scissorEnabled()) {
1982 this->flushRenderTarget(glRT, origin, clip.scissorRect());
1983 } else {
1984 this->flushRenderTarget(glRT);
1985 }
Brian Salomon43f8bf02017-10-18 08:33:29 -04001986 this->flushScissor(clip.scissorState(), glRT->getViewport(), origin);
1987 this->flushWindowRectangles(clip.windowRectsState(), glRT, origin);
1988
bsalomon@google.com74b98712011-11-11 19:46:16 +00001989 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00001990 fHWWriteToColor = kYes_TriState;
Brian Salomon028a9a52017-05-11 11:39:08 -04001991
Eric Karlaeaf22b2017-05-18 15:08:09 -07001992 if (this->glCaps().clearToBoundaryValuesIsBroken() &&
1993 (1 == r || 0 == r) && (1 == g || 0 == g) && (1 == b || 0 == b) && (1 == a || 0 == a)) {
Eric Karlaeaf22b2017-05-18 15:08:09 -07001994 static const GrGLfloat safeAlpha1 = nextafter(1.f, 2.f);
1995 static const GrGLfloat safeAlpha0 = nextafter(0.f, -1.f);
Eric Karlaeaf22b2017-05-18 15:08:09 -07001996 a = (1 == a) ? safeAlpha1 : safeAlpha0;
Brian Salomon028a9a52017-05-11 11:39:08 -04001997 }
bsalomon@google.com74b98712011-11-11 19:46:16 +00001998 GL_CALL(ClearColor(r, g, b, a));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001999 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00002000}
2001
Robert Phillips95214472017-08-08 18:00:03 -04002002void GrGLGpu::clearStencil(GrRenderTarget* target, int clearValue) {
2003 if (!target) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00002004 return;
2005 }
Robert Phillipscb2e2352017-08-30 16:44:40 -04002006
2007 GrStencilAttachment* sb = target->renderTargetPriv().getStencilAttachment();
2008 // this should only be called internally when we know we have a
2009 // stencil buffer.
2010 SkASSERT(sb);
2011
bsalomonb0bd4f62014-09-03 07:19:50 -07002012 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
Brian Salomon1fabd512018-02-09 09:54:25 -05002013 this->flushRenderTargetNoColorWrites(glRT);
bsalomon@google.com8295dc12011-05-02 12:53:34 +00002014
joshualitt77b13072014-10-27 14:51:01 -07002015 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07002016 this->disableWindowRectangles();
robertphillips@google.com730ebe52012-04-16 16:33:13 +00002017
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002018 GL_CALL(StencilMask(0xffffffff));
Robert Phillips95214472017-08-08 18:00:03 -04002019 GL_CALL(ClearStencil(clearValue));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002020 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00002021 fHWStencilSettings.invalidate();
Robert Phillipscb2e2352017-08-30 16:44:40 -04002022 if (!clearValue) {
2023 sb->cleared();
2024 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002025}
2026
csmartdalton29df7602016-08-31 11:55:52 -07002027void GrGLGpu::clearStencilClip(const GrFixedClip& clip,
2028 bool insideStencilMask,
Robert Phillips19e51dc2017-08-09 09:30:51 -04002029 GrRenderTarget* target, GrSurfaceOrigin origin) {
bsalomon49f085d2014-09-05 13:34:00 -07002030 SkASSERT(target);
egdaniel9cb63402016-06-23 08:37:05 -07002031 this->handleDirtyContext();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00002032
Mike Klein31550db2017-06-06 23:29:53 +00002033 if (this->glCaps().useDrawToClearStencilClip()) {
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002034 this->clearStencilClipAsDraw(clip, insideStencilMask, target, origin);
Mike Klein31550db2017-06-06 23:29:53 +00002035 return;
2036 }
2037
egdaniel8dc7c3a2015-04-16 11:22:42 -07002038 GrStencilAttachment* sb = target->renderTargetPriv().getStencilAttachment();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00002039 // this should only be called internally when we know we have a
2040 // stencil buffer.
bsalomon6bc1b5f2015-02-23 09:06:38 -08002041 SkASSERT(sb);
2042 GrGLint stencilBitCount = sb->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002043#if 0
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002044 SkASSERT(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00002045 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002046#else
2047 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00002048 // ANGLE a partial stencil mask will cause clears to be
Robert Phillipsf2361d22016-10-25 14:20:06 -04002049 // turned into draws. Our contract on GrOpList says that
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002050 // changing the clip between stencil passes may or may not
2051 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00002052 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002053#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002054 GrGLint value;
csmartdalton29df7602016-08-31 11:55:52 -07002055 if (insideStencilMask) {
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002056 value = (1 << (stencilBitCount - 1));
2057 } else {
2058 value = 0;
2059 }
bsalomonb0bd4f62014-09-03 07:19:50 -07002060 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
Brian Salomon1fabd512018-02-09 09:54:25 -05002061 this->flushRenderTargetNoColorWrites(glRT);
bsalomon@google.coma3201942012-06-21 19:58:20 +00002062
Robert Phillips19e51dc2017-08-09 09:30:51 -04002063 this->flushScissor(clip.scissorState(), glRT->getViewport(), origin);
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002064 this->flushWindowRectangles(clip.windowRectsState(), glRT, origin);
bsalomon@google.coma3201942012-06-21 19:58:20 +00002065
caryclark@google.comcf6285b2012-06-06 12:09:01 +00002066 GL_CALL(StencilMask((uint32_t) clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002067 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002068 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00002069 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00002070}
2071
Brian Salomon71d9d842016-11-03 13:42:00 -04002072static bool read_pixels_pays_for_y_flip(GrSurfaceOrigin origin, const GrGLCaps& caps,
bsalomon39826022015-07-23 08:07:21 -07002073 int width, int height, GrPixelConfig config,
2074 size_t rowBytes) {
Brian Salomon71d9d842016-11-03 13:42:00 -04002075 // If the surface is already TopLeft, we don't need to flip.
2076 if (kTopLeft_GrSurfaceOrigin == origin) {
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002077 return false;
2078 }
2079
bsalomon494aa592015-07-23 11:45:02 -07002080 // If the read is really small or smaller than the min texture size, don't force a draw.
bsalomon100b8f82015-10-28 08:37:44 -07002081 static const int kMinSize = 32;
2082 if (width < kMinSize || height < kMinSize) {
bsalomon494aa592015-07-23 11:45:02 -07002083 return false;
2084 }
2085
bsalomon@google.com56d11e02011-11-30 19:59:08 +00002086 // if GL can do the flip then we'll never pay for it.
bsalomon39826022015-07-23 08:07:21 -07002087 if (caps.packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00002088 return false;
2089 }
2090
2091 // If we have to do memcpy to handle non-trim rowBytes then we
bsalomon@google.com7107fa72011-11-10 14:54:14 +00002092 // get the flip for free. Otherwise it costs.
bsalomon39826022015-07-23 08:07:21 -07002093 // Note that we're assuming that 0 rowBytes has already been handled and that the width has been
2094 // clipped.
2095 return caps.packRowLengthSupport() || GrBytesPerPixel(config) * width == rowBytes;
2096}
2097
bsalomon1aa20292016-01-22 08:16:09 -08002098bool GrGLGpu::readPixelsSupported(GrRenderTarget* target, GrPixelConfig readConfig) {
Brian Salomon625cd9e2016-12-15 09:35:19 -05002099#ifdef SK_BUILD_FOR_MAC
2100 // Chromium may ask us to read back from locked IOSurfaces. Calling the command buffer's
2101 // glGetIntegerv() with GL_IMPLEMENTATION_COLOR_READ_FORMAT/_TYPE causes the command buffer
2102 // to make a call to check the framebuffer status which can hang the driver. So in Mac Chromium
2103 // we always use a temporary surface to test for read pixels support.
2104 // https://www.crbug.com/662802
2105 if (this->glContext().driver() == kChromium_GrGLDriver) {
2106 return this->readPixelsSupported(target->config(), readConfig);
2107 }
2108#endif
bsalomon1aa20292016-01-22 08:16:09 -08002109 auto bindRenderTarget = [this, target]() -> bool {
Brian Salomon1fabd512018-02-09 09:54:25 -05002110 this->flushRenderTargetNoColorWrites(static_cast<GrGLRenderTarget*>(target));
bsalomon1aa20292016-01-22 08:16:09 -08002111 return true;
2112 };
bsalomon2c3db322016-11-08 13:26:24 -08002113 auto unbindRenderTarget = []{};
bsalomon1aa20292016-01-22 08:16:09 -08002114 auto getIntegerv = [this](GrGLenum query, GrGLint* value) {
2115 GR_GL_GetIntegerv(this->glInterface(), query, value);
2116 };
2117 GrPixelConfig rtConfig = target->config();
bsalomon2c3db322016-11-08 13:26:24 -08002118 return this->glCaps().readPixelsSupported(rtConfig, readConfig, getIntegerv, bindRenderTarget,
2119 unbindRenderTarget);
bsalomon1aa20292016-01-22 08:16:09 -08002120}
2121
2122bool GrGLGpu::readPixelsSupported(GrPixelConfig rtConfig, GrPixelConfig readConfig) {
bsalomon2c3db322016-11-08 13:26:24 -08002123 sk_sp<GrTexture> temp;
2124 auto bindRenderTarget = [this, rtConfig, &temp]() -> bool {
Robert Phillips16d8ec62017-07-27 16:16:25 -04002125 GrSurfaceDesc desc;
bsalomon1aa20292016-01-22 08:16:09 -08002126 desc.fConfig = rtConfig;
2127 desc.fWidth = desc.fHeight = 16;
Brian Salomonbdecacf2018-02-02 20:32:49 -05002128 if (this->glCaps().isConfigRenderable(rtConfig)) {
bsalomon2c3db322016-11-08 13:26:24 -08002129 desc.fFlags = kRenderTarget_GrSurfaceFlag;
Robert Phillips67d52cf2017-06-05 13:38:13 -04002130 temp = this->createTexture(desc, SkBudgeted::kNo);
bsalomon2c3db322016-11-08 13:26:24 -08002131 if (!temp) {
2132 return false;
2133 }
2134 GrGLRenderTarget* glrt = static_cast<GrGLRenderTarget*>(temp->asRenderTarget());
Brian Salomon1fabd512018-02-09 09:54:25 -05002135 this->flushRenderTargetNoColorWrites(glrt);
bsalomon2c3db322016-11-08 13:26:24 -08002136 return true;
2137 } else if (this->glCaps().canConfigBeFBOColorAttachment(rtConfig)) {
Robert Phillips67d52cf2017-06-05 13:38:13 -04002138 temp = this->createTexture(desc, SkBudgeted::kNo);
bsalomon2c3db322016-11-08 13:26:24 -08002139 if (!temp) {
2140 return false;
2141 }
2142 GrGLIRect vp;
2143 this->bindSurfaceFBOForPixelOps(temp.get(), GR_GL_FRAMEBUFFER, &vp, kDst_TempFBOTarget);
Robert Phillips294870f2016-11-11 12:38:40 -05002144 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon2c3db322016-11-08 13:26:24 -08002145 return true;
bsalomon1aa20292016-01-22 08:16:09 -08002146 }
bsalomon2c3db322016-11-08 13:26:24 -08002147 return false;
2148 };
2149 auto unbindRenderTarget = [this, &temp]() {
2150 this->unbindTextureFBOForPixelOps(GR_GL_FRAMEBUFFER, temp.get());
bsalomon1aa20292016-01-22 08:16:09 -08002151 };
2152 auto getIntegerv = [this](GrGLenum query, GrGLint* value) {
2153 GR_GL_GetIntegerv(this->glInterface(), query, value);
2154 };
bsalomon2c3db322016-11-08 13:26:24 -08002155 return this->glCaps().readPixelsSupported(rtConfig, readConfig, getIntegerv, bindRenderTarget,
2156 unbindRenderTarget);
bsalomon1aa20292016-01-22 08:16:09 -08002157}
2158
2159bool GrGLGpu::readPixelsSupported(GrSurface* surfaceForConfig, GrPixelConfig readConfig) {
2160 if (GrRenderTarget* rt = surfaceForConfig->asRenderTarget()) {
2161 return this->readPixelsSupported(rt, readConfig);
2162 } else {
2163 GrPixelConfig config = surfaceForConfig->config();
2164 return this->readPixelsSupported(config, readConfig);
2165 }
2166}
2167
Brian Salomon9b009bb2018-02-14 13:53:55 -05002168bool GrGLGpu::onGetReadPixelsInfo(GrSurface* srcSurface, GrSurfaceOrigin srcOrigin, int width,
Brian Salomonc320b152018-02-20 14:05:36 -05002169 int height, size_t rowBytes, GrColorType dstColorType,
Brian Salomon9b009bb2018-02-14 13:53:55 -05002170 DrawPreference* drawPreference,
bsalomonf0674512015-07-28 13:26:15 -07002171 ReadPixelTempDrawInfo* tempDrawInfo) {
Brian Salomonc320b152018-02-20 14:05:36 -05002172 // We don't want to introduce a sRGB conversion if we trigger a draw.
2173 auto dstConfigSRGBEncoded = GrPixelConfigIsSRGBEncoded(srcSurface->config());
Brian Salomon9b009bb2018-02-14 13:53:55 -05002174 if (*drawPreference != kNoDraw_DrawPreference) {
2175 // We assume the base class has only inserted a draw for sRGB reasons. So the
2176 // the temp surface has the config of the dst data. There is no swizzling, nor dst config
2177 // spoofing.
Brian Salomonc320b152018-02-20 14:05:36 -05002178 SkASSERT(tempDrawInfo->fReadColorType == dstColorType);
2179 SkASSERT(GrPixelConfigToColorType(tempDrawInfo->fTempSurfaceDesc.fConfig) == dstColorType);
Brian Salomon9b009bb2018-02-14 13:53:55 -05002180 SkASSERT(tempDrawInfo->fSwizzle == GrSwizzle::RGBA());
Brian Salomonc320b152018-02-20 14:05:36 -05002181 // Don't undo a sRGB conversion introduced by our caller via an intermediate draw.
2182 dstConfigSRGBEncoded = GrPixelConfigIsSRGBEncoded(tempDrawInfo->fTempSurfaceDesc.fConfig);
2183 }
2184 if (GrColorTypeIsAlphaOnly(dstColorType)) {
2185 dstConfigSRGBEncoded = GrSRGBEncoded::kNo;
Brian Salomon9b009bb2018-02-14 13:53:55 -05002186 }
bsalomone9573312016-01-25 14:33:25 -08002187 GrPixelConfig srcConfig = srcSurface->config();
bsalomon1aa20292016-01-22 08:16:09 -08002188
bsalomonb117ff12016-07-19 07:24:40 -07002189 tempDrawInfo->fTempSurfaceFit = this->glCaps().partialFBOReadIsSlow() ? SkBackingFit::kExact
2190 : SkBackingFit::kApprox;
bsalomone9573312016-01-25 14:33:25 -08002191
Brian Salomonc320b152018-02-20 14:05:36 -05002192 // TODO: Update this logic to use color type.
2193 auto dstAsConfig = GrColorTypeToPixelConfig(dstColorType, dstConfigSRGBEncoded);
2194
2195 if (this->glCaps().rgba8888PixelsOpsAreSlow() && kRGBA_8888_GrPixelConfig == dstAsConfig &&
bsalomon1aa20292016-01-22 08:16:09 -08002196 this->readPixelsSupported(kBGRA_8888_GrPixelConfig, kBGRA_8888_GrPixelConfig)) {
bsalomon39826022015-07-23 08:07:21 -07002197 tempDrawInfo->fTempSurfaceDesc.fConfig = kBGRA_8888_GrPixelConfig;
bsalomon6c9cd552016-01-22 07:17:34 -08002198 tempDrawInfo->fSwizzle = GrSwizzle::BGRA();
Brian Salomonc320b152018-02-20 14:05:36 -05002199 tempDrawInfo->fReadColorType = GrColorType::kBGRA_8888;
bsalomonb411b3b2015-07-31 09:34:24 -07002200 ElevateDrawPreference(drawPreference, kGpuPrefersDraw_DrawPreference);
ericrkb4ecabd2016-03-11 15:18:20 -08002201 } else if (this->glCaps().rgbaToBgraReadbackConversionsAreSlow() &&
Brian Salomonc320b152018-02-20 14:05:36 -05002202 GrBytesPerPixel(dstAsConfig) == 4 &&
2203 GrPixelConfigSwapRAndB(dstAsConfig) == srcConfig &&
bsalomon1aa20292016-01-22 08:16:09 -08002204 this->readPixelsSupported(srcSurface, srcConfig)) {
bsalomon6c9cd552016-01-22 07:17:34 -08002205 // Mesa 3D takes a slow path on when reading back BGRA from an RGBA surface and vice-versa.
bsalomon39826022015-07-23 08:07:21 -07002206 // Better to do a draw with a R/B swap and then read as the original config.
2207 tempDrawInfo->fTempSurfaceDesc.fConfig = srcConfig;
bsalomon6c9cd552016-01-22 07:17:34 -08002208 tempDrawInfo->fSwizzle = GrSwizzle::BGRA();
Brian Salomonc320b152018-02-20 14:05:36 -05002209 tempDrawInfo->fReadColorType = GrPixelConfigToColorType(srcConfig);
bsalomonf0674512015-07-28 13:26:15 -07002210 ElevateDrawPreference(drawPreference, kGpuPrefersDraw_DrawPreference);
Brian Salomonc320b152018-02-20 14:05:36 -05002211 } else if (!this->readPixelsSupported(srcSurface, dstAsConfig)) {
2212 if (kBGRA_8888_GrPixelConfig == dstAsConfig &&
Brian Salomon71d9d842016-11-03 13:42:00 -04002213 this->glCaps().canConfigBeFBOColorAttachment(kRGBA_8888_GrPixelConfig) &&
bsalomon1aa20292016-01-22 08:16:09 -08002214 this->readPixelsSupported(kRGBA_8888_GrPixelConfig, kRGBA_8888_GrPixelConfig)) {
bsalomone9573312016-01-25 14:33:25 -08002215 // We're trying to read BGRA but it's not supported. If RGBA is renderable and
2216 // we can read it back, then do a swizzling draw to a RGBA and read it back (which
2217 // will effectively be BGRA).
bsalomon1aa20292016-01-22 08:16:09 -08002218 tempDrawInfo->fTempSurfaceDesc.fConfig = kRGBA_8888_GrPixelConfig;
2219 tempDrawInfo->fSwizzle = GrSwizzle::BGRA();
Brian Salomonc320b152018-02-20 14:05:36 -05002220 tempDrawInfo->fReadColorType = GrColorType::kRGBA_8888;
bsalomon1aa20292016-01-22 08:16:09 -08002221 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
Brian Salomonc320b152018-02-20 14:05:36 -05002222 } else if (kSBGRA_8888_GrPixelConfig == dstAsConfig &&
Brian Salomon9b009bb2018-02-14 13:53:55 -05002223 this->glCaps().canConfigBeFBOColorAttachment(kSRGBA_8888_GrPixelConfig) &&
2224 this->readPixelsSupported(kSRGBA_8888_GrPixelConfig,
2225 kSRGBA_8888_GrPixelConfig)) {
brianosmana6359362016-03-21 06:55:37 -07002226 // We're trying to read sBGRA but it's not supported. If sRGBA is renderable and
2227 // we can read it back, then do a swizzling draw to a sRGBA and read it back (which
2228 // will effectively be sBGRA).
2229 tempDrawInfo->fTempSurfaceDesc.fConfig = kSRGBA_8888_GrPixelConfig;
2230 tempDrawInfo->fSwizzle = GrSwizzle::BGRA();
Brian Salomonc320b152018-02-20 14:05:36 -05002231 tempDrawInfo->fReadColorType = GrColorType::kRGBA_8888;
brianosmana6359362016-03-21 06:55:37 -07002232 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
Brian Salomonc320b152018-02-20 14:05:36 -05002233 } else if (kAlpha_8_GrPixelConfig == dstAsConfig) {
Brian Salomon9b009bb2018-02-14 13:53:55 -05002234 // onReadPixels implements a fallback for cases where we want to read kAlpha_8,
bsalomone9573312016-01-25 14:33:25 -08002235 // it's unsupported, but 32bit RGBA reads are supported.
Brian Salomon9b009bb2018-02-14 13:53:55 -05002236 if (!this->readPixelsSupported(srcSurface, kRGBA_8888_GrPixelConfig)) {
bsalomone9573312016-01-25 14:33:25 -08002237 // If we can't read RGBA from the src try to draw to a kRGBA_8888 (or kSRGBA_8888)
2238 // first and then onReadPixels will read that to a 32bit temporary buffer.
Brian Salomon9b009bb2018-02-14 13:53:55 -05002239 if (this->glCaps().canConfigBeFBOColorAttachment(kRGBA_8888_GrPixelConfig)) {
bsalomone9573312016-01-25 14:33:25 -08002240 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
Brian Salomon9b009bb2018-02-14 13:53:55 -05002241 tempDrawInfo->fTempSurfaceDesc.fConfig = kRGBA_8888_GrPixelConfig;
Brian Salomonc320b152018-02-20 14:05:36 -05002242 tempDrawInfo->fReadColorType = GrColorType::kAlpha_8;
bsalomone9573312016-01-25 14:33:25 -08002243 } else {
2244 return false;
2245 }
2246 } else {
Brian Salomon9b009bb2018-02-14 13:53:55 -05002247 tempDrawInfo->fTempSurfaceDesc.fConfig = srcConfig;
Brian Salomonc320b152018-02-20 14:05:36 -05002248 SkASSERT(tempDrawInfo->fReadColorType == GrColorType::kAlpha_8);
bsalomone9573312016-01-25 14:33:25 -08002249 }
Brian Salomonc320b152018-02-20 14:05:36 -05002250 } else if (kRGBA_half_GrPixelConfig == dstAsConfig &&
Stan Iliev0078ab22017-11-08 14:16:26 -05002251 this->readPixelsSupported(srcSurface, kRGBA_float_GrPixelConfig)) {
2252 // If reading in half float format is not supported, then read in float format.
2253 return true;
Brian Salomonc320b152018-02-20 14:05:36 -05002254 } else if (this->glCaps().canConfigBeFBOColorAttachment(dstAsConfig) &&
2255 this->readPixelsSupported(dstAsConfig, dstAsConfig)) {
bsalomone6d665e2016-03-10 07:22:25 -08002256 // Do a draw to convert from the src config to the read config.
2257 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
Brian Salomonc320b152018-02-20 14:05:36 -05002258 tempDrawInfo->fTempSurfaceDesc.fConfig = dstAsConfig;
2259 tempDrawInfo->fReadColorType = dstColorType;
bsalomon1aa20292016-01-22 08:16:09 -08002260 } else {
2261 return false;
2262 }
bsalomon39826022015-07-23 08:07:21 -07002263 }
2264
Brian Salomon71d9d842016-11-03 13:42:00 -04002265 if ((srcSurface->asRenderTarget() || this->glCaps().canConfigBeFBOColorAttachment(srcConfig)) &&
Brian Salomonc320b152018-02-20 14:05:36 -05002266 read_pixels_pays_for_y_flip(srcOrigin, this->glCaps(), width, height, dstAsConfig,
Brian Salomon71d9d842016-11-03 13:42:00 -04002267 rowBytes)) {
bsalomonf0674512015-07-28 13:26:15 -07002268 ElevateDrawPreference(drawPreference, kGpuPrefersDraw_DrawPreference);
bsalomon39826022015-07-23 08:07:21 -07002269 }
2270
bsalomon39826022015-07-23 08:07:21 -07002271 return true;
bsalomon@google.comc4364992011-11-07 15:54:49 +00002272}
2273
Brian Salomonc320b152018-02-20 14:05:36 -05002274bool GrGLGpu::onReadPixels(GrSurface* surface, GrSurfaceOrigin origin, int left, int top, int width,
2275 int height, GrColorType dstColorType, void* buffer, size_t rowBytes) {
bsalomon6cb3cbe2015-07-30 07:34:27 -07002276 SkASSERT(surface);
bsalomon39826022015-07-23 08:07:21 -07002277
bsalomone9573312016-01-25 14:33:25 -08002278 GrGLRenderTarget* renderTarget = static_cast<GrGLRenderTarget*>(surface->asRenderTarget());
Brian Salomon71d9d842016-11-03 13:42:00 -04002279 if (!renderTarget && !this->glCaps().canConfigBeFBOColorAttachment(surface->config())) {
bsalomon6cb3cbe2015-07-30 07:34:27 -07002280 return false;
2281 }
2282
Brian Salomonc320b152018-02-20 14:05:36 -05002283 // TODO: Avoid this conversion by making GrGLCaps work with color types.
2284 auto dstAsConfig = GrColorTypeToPixelConfig(dstColorType, GrSRGBEncoded::kNo);
2285
bsalomone9573312016-01-25 14:33:25 -08002286 // We have a special case fallback for reading eight bit alpha. We will read back all four 8
2287 // bit channels as RGBA and then extract A.
Brian Salomonc320b152018-02-20 14:05:36 -05002288 if (!this->readPixelsSupported(surface, dstAsConfig)) {
2289 if (kAlpha_8_GrPixelConfig == dstAsConfig &&
2290 this->readPixelsSupported(surface, kRGBA_8888_GrPixelConfig)) {
Ben Wagner7ecc5962016-11-02 17:07:33 -04002291 std::unique_ptr<uint32_t[]> temp(new uint32_t[width * height * 4]);
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002292 if (this->onReadPixels(surface, origin, left, top, width, height,
Brian Salomonc320b152018-02-20 14:05:36 -05002293 GrColorType::kRGBA_8888, temp.get(), width * 4)) {
bsalomone9573312016-01-25 14:33:25 -08002294 uint8_t* dst = reinterpret_cast<uint8_t*>(buffer);
2295 for (int j = 0; j < height; ++j) {
2296 for (int i = 0; i < width; ++i) {
2297 dst[j*rowBytes + i] = (0xFF000000U & temp[j*width+i]) >> 24;
2298 }
2299 }
2300 return true;
2301 }
2302 }
Stan Iliev0078ab22017-11-08 14:16:26 -05002303
2304 // If reading in half float format is not supported, then read in a temporary float buffer
2305 // and convert to half float.
Brian Salomonc320b152018-02-20 14:05:36 -05002306 if (kRGBA_half_GrPixelConfig == dstAsConfig &&
Stan Iliev0078ab22017-11-08 14:16:26 -05002307 this->readPixelsSupported(surface, kRGBA_float_GrPixelConfig)) {
2308 std::unique_ptr<float[]> temp(new float[width * height * 4]);
2309 if (this->onReadPixels(surface, origin, left, top, width, height,
Brian Salomonc320b152018-02-20 14:05:36 -05002310 GrColorType::kRGBA_F32, temp.get(), width * sizeof(float) * 4)) {
Stan Iliev0078ab22017-11-08 14:16:26 -05002311 uint8_t* dst = reinterpret_cast<uint8_t*>(buffer);
2312 float* src = temp.get();
2313 for (int j = 0; j < height; ++j) {
2314 SkHalf* dstRow = reinterpret_cast<SkHalf*>(dst);
2315 for (int i = 0; i < width; ++i) {
2316 for (int color = 0; color < 4; color++) {
2317 *dstRow++ = SkFloatToHalf(*src++);
2318 }
2319 }
2320 dst += rowBytes;
2321 }
2322 return true;
2323 }
2324 }
bsalomon16921ec2015-07-30 15:34:56 -07002325 return false;
2326 }
2327
bsalomon76148af2016-01-12 11:13:47 -08002328 GrGLenum externalFormat;
2329 GrGLenum externalType;
Brian Salomonc320b152018-02-20 14:05:36 -05002330 if (!this->glCaps().getReadPixelsFormat(surface->config(), dstAsConfig, &externalFormat,
bsalomon76148af2016-01-12 11:13:47 -08002331 &externalType)) {
2332 return false;
2333 }
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002334 bool flipY = kBottomLeft_GrSurfaceOrigin == origin;
bsalomon@google.comc4364992011-11-07 15:54:49 +00002335
Brian Salomon71d9d842016-11-03 13:42:00 -04002336 GrGLIRect glvp;
2337 if (renderTarget) {
2338 // resolve the render target if necessary
2339 switch (renderTarget->getResolveType()) {
2340 case GrGLRenderTarget::kCantResolve_ResolveType:
2341 return false;
2342 case GrGLRenderTarget::kAutoResolves_ResolveType:
Brian Salomon1fabd512018-02-09 09:54:25 -05002343 this->flushRenderTargetNoColorWrites(renderTarget);
Brian Salomon71d9d842016-11-03 13:42:00 -04002344 break;
2345 case GrGLRenderTarget::kCanResolve_ResolveType:
Brian Salomon1fabd512018-02-09 09:54:25 -05002346 this->onResolveRenderTarget(renderTarget);
Brian Salomon71d9d842016-11-03 13:42:00 -04002347 // we don't track the state of the READ FBO ID.
Adrienne Walker4ee88512018-05-17 11:37:14 -07002348 this->bindFramebuffer(GR_GL_READ_FRAMEBUFFER, renderTarget->textureFBOID());
Brian Salomon71d9d842016-11-03 13:42:00 -04002349 break;
2350 default:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -04002351 SK_ABORT("Unknown resolve type");
Brian Salomon71d9d842016-11-03 13:42:00 -04002352 }
2353 glvp = renderTarget->getViewport();
2354 } else {
2355 // Use a temporary FBO.
2356 this->bindSurfaceFBOForPixelOps(surface, GR_GL_FRAMEBUFFER, &glvp, kSrc_TempFBOTarget);
Robert Phillips294870f2016-11-11 12:38:40 -05002357 fHWBoundRenderTargetUniqueID.makeInvalid();
reed@google.comac10a2d2010-12-22 21:39:39 +00002358 }
2359
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00002360 // the read rect is viewport-relative
2361 GrGLIRect readRect;
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002362 readRect.setRelativeTo(glvp, left, top, width, height, origin);
rmistry@google.comfbfcd562012-08-23 18:09:54 +00002363
Brian Salomonc320b152018-02-20 14:05:36 -05002364 int bytesPerPixel = GrBytesPerPixel(dstAsConfig);
bsalomon9d02b262016-02-01 12:49:30 -08002365 size_t tightRowBytes = bytesPerPixel * width;
egdaniel6d901da2015-07-30 12:02:15 -07002366
bsalomon@google.comc6980972011-11-02 19:57:21 +00002367 size_t readDstRowBytes = tightRowBytes;
2368 void* readDst = buffer;
rmistry@google.comfbfcd562012-08-23 18:09:54 +00002369
bsalomon@google.comc6980972011-11-02 19:57:21 +00002370 // determine if GL can read using the passed rowBytes or if we need
2371 // a scratch buffer.
joshualitt29f86792015-05-29 08:06:48 -07002372 SkAutoSMalloc<32 * sizeof(GrColor)> scratch;
bsalomon@google.comc6980972011-11-02 19:57:21 +00002373 if (rowBytes != tightRowBytes) {
bsalomon9d02b262016-02-01 12:49:30 -08002374 if (this->glCaps().packRowLengthSupport() && !(rowBytes % bytesPerPixel)) {
skia.committer@gmail.com4677acc2013-10-17 07:02:33 +00002375 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH,
bsalomon9d02b262016-02-01 12:49:30 -08002376 static_cast<GrGLint>(rowBytes / bytesPerPixel)));
bsalomon@google.comc6980972011-11-02 19:57:21 +00002377 readDstRowBytes = rowBytes;
2378 } else {
2379 scratch.reset(tightRowBytes * height);
2380 readDst = scratch.get();
2381 }
2382 }
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002383 if (flipY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00002384 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 1));
2385 }
Brian Salomonc320b152018-02-20 14:05:36 -05002386 GL_CALL(PixelStorei(GR_GL_PACK_ALIGNMENT, config_alignment(dstAsConfig)));
bsalomonf46a1242015-12-15 12:37:38 -08002387
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002388 GL_CALL(ReadPixels(readRect.fLeft, readRect.fBottom,
2389 readRect.fWidth, readRect.fHeight,
bsalomon76148af2016-01-12 11:13:47 -08002390 externalFormat, externalType, readDst));
bsalomon@google.comc6980972011-11-02 19:57:21 +00002391 if (readDstRowBytes != tightRowBytes) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002392 SkASSERT(this->glCaps().packRowLengthSupport());
bsalomon@google.comc6980972011-11-02 19:57:21 +00002393 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
2394 }
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002395 if (flipY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00002396 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 0));
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002397 flipY = false;
bsalomon@google.com56d11e02011-11-30 19:59:08 +00002398 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002399
2400 // now reverse the order of the rows, since GL's are bottom-to-top, but our
bsalomon@google.comc6980972011-11-02 19:57:21 +00002401 // API presents top-to-bottom. We must preserve the padding contents. Note
2402 // that the above readPixels did not overwrite the padding.
2403 if (readDst == buffer) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002404 SkASSERT(rowBytes == readDstRowBytes);
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002405 if (flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002406 scratch.reset(tightRowBytes);
2407 void* tmpRow = scratch.get();
2408 // flip y in-place by rows
2409 const int halfY = height >> 1;
2410 char* top = reinterpret_cast<char*>(buffer);
2411 char* bottom = top + (height - 1) * rowBytes;
2412 for (int y = 0; y < halfY; y++) {
2413 memcpy(tmpRow, top, tightRowBytes);
2414 memcpy(top, bottom, tightRowBytes);
2415 memcpy(bottom, tmpRow, tightRowBytes);
2416 top += rowBytes;
2417 bottom -= rowBytes;
2418 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00002419 }
2420 } else {
bsalomon9d02b262016-02-01 12:49:30 -08002421 SkASSERT(readDst != buffer);
2422 SkASSERT(rowBytes != tightRowBytes);
bsalomon@google.comc6980972011-11-02 19:57:21 +00002423 // copy from readDst to buffer while flipping y
caryclark@google.comcf6285b2012-06-06 12:09:01 +00002424 // const int halfY = height >> 1;
bsalomon@google.comc6980972011-11-02 19:57:21 +00002425 const char* src = reinterpret_cast<const char*>(readDst);
bsalomon@google.comc4364992011-11-07 15:54:49 +00002426 char* dst = reinterpret_cast<char*>(buffer);
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002427 if (flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002428 dst += (height-1) * rowBytes;
2429 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00002430 for (int y = 0; y < height; y++) {
2431 memcpy(dst, src, tightRowBytes);
2432 src += readDstRowBytes;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002433 if (!flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002434 dst += rowBytes;
2435 } else {
2436 dst -= rowBytes;
2437 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002438 }
2439 }
Brian Salomon71d9d842016-11-03 13:42:00 -04002440 if (!renderTarget) {
2441 this->unbindTextureFBOForPixelOps(GR_GL_FRAMEBUFFER, surface);
2442 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002443 return true;
2444}
2445
Greg Daniel500d58b2017-08-24 15:59:33 -04002446GrGpuRTCommandBuffer* GrGLGpu::createCommandBuffer(
Robert Phillips19e51dc2017-08-09 09:30:51 -04002447 GrRenderTarget* rt, GrSurfaceOrigin origin,
Robert Phillips6b47c7d2017-08-29 07:24:09 -04002448 const GrGpuRTCommandBuffer::LoadAndStoreInfo& colorInfo,
Greg Daniel500d58b2017-08-24 15:59:33 -04002449 const GrGpuRTCommandBuffer::StencilLoadAndStoreInfo& stencilInfo) {
Robert Phillips6b47c7d2017-08-29 07:24:09 -04002450 return new GrGLGpuRTCommandBuffer(this, rt, origin, colorInfo, stencilInfo);
Greg Daniel500d58b2017-08-24 15:59:33 -04002451}
2452
2453GrGpuTextureCommandBuffer* GrGLGpu::createCommandBuffer(GrTexture* texture,
2454 GrSurfaceOrigin origin) {
2455 return new GrGLGpuTextureCommandBuffer(this, texture, origin);
egdaniel066df7c2016-06-08 14:02:27 -07002456}
2457
Brian Salomon1fabd512018-02-09 09:54:25 -05002458void GrGLGpu::flushRenderTarget(GrGLRenderTarget* target, GrSurfaceOrigin origin,
2459 const SkIRect& bounds, bool disableSRGB) {
2460 this->flushRenderTargetNoColorWrites(target, disableSRGB);
2461 this->didWriteToSurface(target, origin, &bounds);
2462}
bsalomon6ba6fa12015-03-04 11:57:37 -08002463
Brian Salomon1fabd512018-02-09 09:54:25 -05002464void GrGLGpu::flushRenderTarget(GrGLRenderTarget* target, bool disableSRGB) {
2465 this->flushRenderTargetNoColorWrites(target, disableSRGB);
2466 this->didWriteToSurface(target, kTopLeft_GrSurfaceOrigin, nullptr);
2467}
2468
2469void GrGLGpu::flushRenderTargetNoColorWrites(GrGLRenderTarget* target, bool disableSRGB) {
2470 SkASSERT(target);
Robert Phillips294870f2016-11-11 12:38:40 -05002471 GrGpuResource::UniqueID rtID = target->uniqueID();
egdanield803f272015-03-18 13:01:52 -07002472 if (fHWBoundRenderTargetUniqueID != rtID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -07002473 this->bindFramebuffer(GR_GL_FRAMEBUFFER, target->renderFBOID());
egdanield803f272015-03-18 13:01:52 -07002474#ifdef SK_DEBUG
2475 // don't do this check in Chromium -- this is causing
2476 // lots of repeated command buffer flushes when the compositor is
2477 // rendering with Ganesh, which is really slow; even too slow for
2478 // Debug mode.
cdalton1acea862015-06-02 13:05:52 -07002479 if (kChromium_GrGLDriver != this->glContext().driver()) {
egdanield803f272015-03-18 13:01:52 -07002480 GrGLenum status;
2481 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
2482 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
2483 SkDebugf("GrGLGpu::flushRenderTarget glCheckFramebufferStatus %x\n", status);
2484 }
bsalomon160f24c2015-03-17 15:55:42 -07002485 }
egdanield803f272015-03-18 13:01:52 -07002486#endif
Chris Daltonda40cd22018-04-16 13:19:58 -06002487 if (this->glCaps().requiresFlushBetweenNonAndInstancedDraws()) {
2488 fRequiresFlushBeforeNextInstancedDraw = false;
2489 }
egdanield803f272015-03-18 13:01:52 -07002490 fHWBoundRenderTargetUniqueID = rtID;
bsalomon083617b2016-02-12 12:10:14 -08002491 this->flushViewport(target->getViewport());
brianosman64d094d2016-03-25 06:01:59 -07002492 }
2493
brianosman35b784d2016-05-05 11:52:53 -07002494 if (this->glCaps().srgbWriteControl()) {
brianosman33f6b3f2016-06-02 05:49:21 -07002495 this->flushFramebufferSRGB(GrPixelConfigIsSRGB(target->config()) && !disableSRGB);
bsalomon5cd020f2015-03-17 12:46:56 -07002496 }
bsalomon083617b2016-02-12 12:10:14 -08002497}
bsalomona9909122016-01-23 10:41:40 -08002498
brianosman33f6b3f2016-06-02 05:49:21 -07002499void GrGLGpu::flushFramebufferSRGB(bool enable) {
2500 if (enable && kYes_TriState != fHWSRGBFramebuffer) {
2501 GL_CALL(Enable(GR_GL_FRAMEBUFFER_SRGB));
2502 fHWSRGBFramebuffer = kYes_TriState;
2503 } else if (!enable && kNo_TriState != fHWSRGBFramebuffer) {
2504 GL_CALL(Disable(GR_GL_FRAMEBUFFER_SRGB));
2505 fHWSRGBFramebuffer = kNo_TriState;
2506 }
2507}
2508
bsalomon083617b2016-02-12 12:10:14 -08002509void GrGLGpu::flushViewport(const GrGLIRect& viewport) {
2510 if (fHWViewport != viewport) {
2511 viewport.pushToGLViewport(this->glInterface());
2512 fHWViewport = viewport;
2513 }
2514}
2515
bsalomon@google.comd302f142011-03-03 13:54:13 +00002516#define SWAP_PER_DRAW 0
2517
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00002518#if SWAP_PER_DRAW
commit-bot@chromium.org43823302013-09-25 20:57:51 +00002519 #if defined(SK_BUILD_FOR_MAC)
bsalomon@google.comd302f142011-03-03 13:54:13 +00002520 #include <AGL/agl.h>
Mike Klein8f11d4d2018-01-24 12:42:55 -05002521 #elif defined(SK_BUILD_FOR_WIN)
bsalomon@google.comce7357d2012-06-25 17:49:25 +00002522 #include <gl/GL.h>
bsalomon@google.comd302f142011-03-03 13:54:13 +00002523 void SwapBuf() {
2524 DWORD procID = GetCurrentProcessId();
2525 HWND hwnd = GetTopWindow(GetDesktopWindow());
2526 while(hwnd) {
2527 DWORD wndProcID = 0;
2528 GetWindowThreadProcessId(hwnd, &wndProcID);
2529 if(wndProcID == procID) {
2530 SwapBuffers(GetDC(hwnd));
2531 }
2532 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
2533 }
2534 }
2535 #endif
2536#endif
2537
egdaniel9cb63402016-06-23 08:37:05 -07002538void GrGLGpu::draw(const GrPipeline& pipeline,
2539 const GrPrimitiveProcessor& primProc,
bsalomon2eda5b32016-09-21 10:53:24 -07002540 const GrMesh meshes[],
Chris Dalton46983b72017-06-06 12:27:16 -06002541 const GrPipeline::DynamicState dynamicStates[],
egdaniel9cb63402016-06-23 08:37:05 -07002542 int meshCount) {
2543 this->handleDirtyContext();
2544
bsalomon2eda5b32016-09-21 10:53:24 -07002545 bool hasPoints = false;
2546 for (int i = 0; i < meshCount; ++i) {
Chris Dalton3809bab2017-06-13 10:55:06 -06002547 if (meshes[i].primitiveType() == GrPrimitiveType::kPoints) {
bsalomon2eda5b32016-09-21 10:53:24 -07002548 hasPoints = true;
2549 break;
2550 }
2551 }
2552 if (!this->flushGLState(pipeline, primProc, hasPoints)) {
bsalomond95263c2014-12-16 13:05:12 -08002553 return;
2554 }
ethannicholas22793252016-01-30 09:59:10 -08002555
egdaniel0e1853c2016-03-17 11:35:45 -07002556 for (int i = 0; i < meshCount; ++i) {
2557 if (GrXferBarrierType barrierType = pipeline.xferBarrierType(*this->caps())) {
Robert Phillips2890fbf2017-07-26 15:48:41 -04002558 this->xferBarrier(pipeline.renderTarget(), barrierType);
egdaniel0e1853c2016-03-17 11:35:45 -07002559 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002560
Chris Dalton46983b72017-06-06 12:27:16 -06002561 if (dynamicStates) {
2562 if (pipeline.getScissorState().enabled()) {
Robert Phillips2890fbf2017-07-26 15:48:41 -04002563 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(pipeline.renderTarget());
Chris Dalton46983b72017-06-06 12:27:16 -06002564 this->flushScissor(dynamicStates[i].fScissorRect,
Robert Phillips16d8ec62017-07-27 16:16:25 -04002565 glRT->getViewport(), pipeline.proxy()->origin());
Chris Dalton46983b72017-06-06 12:27:16 -06002566 }
2567 }
Brian Salomon6d9c88b2017-06-12 10:24:42 -04002568 if (this->glCaps().requiresCullFaceEnableDisableWhenDrawingLinesAfterNonLines() &&
2569 GrIsPrimTypeLines(meshes[i].primitiveType()) &&
2570 !GrIsPrimTypeLines(fLastPrimitiveType)) {
2571 GL_CALL(Enable(GR_GL_CULL_FACE));
2572 GL_CALL(Disable(GR_GL_CULL_FACE));
2573 }
Chris Dalton114a3c02017-05-26 15:17:19 -06002574 meshes[i].sendToGpu(primProc, this);
Brian Salomon6d9c88b2017-06-12 10:24:42 -04002575 fLastPrimitiveType = meshes[i].primitiveType();
bsalomon@google.com74749cd2013-01-30 16:12:41 +00002576 }
ethannicholas22793252016-01-30 09:59:10 -08002577
bsalomon@google.comd302f142011-03-03 13:54:13 +00002578#if SWAP_PER_DRAW
2579 glFlush();
commit-bot@chromium.org43823302013-09-25 20:57:51 +00002580 #if defined(SK_BUILD_FOR_MAC)
bsalomon@google.comd302f142011-03-03 13:54:13 +00002581 aglSwapBuffers(aglGetCurrentContext());
2582 int set_a_break_pt_here = 9;
2583 aglSwapBuffers(aglGetCurrentContext());
Mike Klein8f11d4d2018-01-24 12:42:55 -05002584 #elif defined(SK_BUILD_FOR_WIN)
bsalomon@google.comd302f142011-03-03 13:54:13 +00002585 SwapBuf();
2586 int set_a_break_pt_here = 9;
2587 SwapBuf();
2588 #endif
2589#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00002590}
2591
Chris Dalton3809bab2017-06-13 10:55:06 -06002592static GrGLenum gr_primitive_type_to_gl_mode(GrPrimitiveType primitiveType) {
2593 switch (primitiveType) {
2594 case GrPrimitiveType::kTriangles:
2595 return GR_GL_TRIANGLES;
2596 case GrPrimitiveType::kTriangleStrip:
2597 return GR_GL_TRIANGLE_STRIP;
Chris Dalton3809bab2017-06-13 10:55:06 -06002598 case GrPrimitiveType::kPoints:
2599 return GR_GL_POINTS;
2600 case GrPrimitiveType::kLines:
2601 return GR_GL_LINES;
2602 case GrPrimitiveType::kLineStrip:
2603 return GR_GL_LINE_STRIP;
2604 case GrPrimitiveType::kLinesAdjacency:
2605 return GR_GL_LINES_ADJACENCY;
2606 }
Ben Wagnerb4aab9a2017-08-16 10:53:04 -04002607 SK_ABORT("invalid GrPrimitiveType");
Chris Dalton3809bab2017-06-13 10:55:06 -06002608 return GR_GL_TRIANGLES;
2609}
2610
Chris Dalton114a3c02017-05-26 15:17:19 -06002611void GrGLGpu::sendMeshToGpu(const GrPrimitiveProcessor& primProc, GrPrimitiveType primitiveType,
2612 const GrBuffer* vertexBuffer, int vertexCount, int baseVertex) {
Chris Dalton3809bab2017-06-13 10:55:06 -06002613 const GrGLenum glPrimType = gr_primitive_type_to_gl_mode(primitiveType);
Chris Dalton114a3c02017-05-26 15:17:19 -06002614 if (this->glCaps().drawArraysBaseVertexIsBroken()) {
Chris Dalton1d616352017-05-31 12:51:23 -06002615 this->setupGeometry(primProc, nullptr, vertexBuffer, baseVertex, nullptr, 0);
Chris Dalton114a3c02017-05-26 15:17:19 -06002616 GL_CALL(DrawArrays(glPrimType, 0, vertexCount));
2617 } else {
Chris Dalton1d616352017-05-31 12:51:23 -06002618 this->setupGeometry(primProc, nullptr, vertexBuffer, 0, nullptr, 0);
Chris Dalton114a3c02017-05-26 15:17:19 -06002619 GL_CALL(DrawArrays(glPrimType, baseVertex, vertexCount));
2620 }
Chris Daltonda40cd22018-04-16 13:19:58 -06002621 if (this->glCaps().requiresFlushBetweenNonAndInstancedDraws()) {
2622 fRequiresFlushBeforeNextInstancedDraw = true;
2623 }
Chris Dalton114a3c02017-05-26 15:17:19 -06002624 fStats.incNumDraws();
2625}
2626
2627void GrGLGpu::sendIndexedMeshToGpu(const GrPrimitiveProcessor& primProc,
2628 GrPrimitiveType primitiveType, const GrBuffer* indexBuffer,
2629 int indexCount, int baseIndex, uint16_t minIndexValue,
2630 uint16_t maxIndexValue, const GrBuffer* vertexBuffer,
2631 int baseVertex) {
Chris Dalton3809bab2017-06-13 10:55:06 -06002632 const GrGLenum glPrimType = gr_primitive_type_to_gl_mode(primitiveType);
Chris Dalton114a3c02017-05-26 15:17:19 -06002633 GrGLvoid* const indices = reinterpret_cast<void*>(indexBuffer->baseOffset() +
2634 sizeof(uint16_t) * baseIndex);
2635
Chris Dalton1d616352017-05-31 12:51:23 -06002636 this->setupGeometry(primProc, indexBuffer, vertexBuffer, baseVertex, nullptr, 0);
Chris Dalton114a3c02017-05-26 15:17:19 -06002637
2638 if (this->glCaps().drawRangeElementsSupport()) {
2639 GL_CALL(DrawRangeElements(glPrimType, minIndexValue, maxIndexValue, indexCount,
2640 GR_GL_UNSIGNED_SHORT, indices));
2641 } else {
2642 GL_CALL(DrawElements(glPrimType, indexCount, GR_GL_UNSIGNED_SHORT, indices));
2643 }
Chris Daltonda40cd22018-04-16 13:19:58 -06002644 if (this->glCaps().requiresFlushBetweenNonAndInstancedDraws()) {
2645 fRequiresFlushBeforeNextInstancedDraw = true;
2646 }
Chris Dalton114a3c02017-05-26 15:17:19 -06002647 fStats.incNumDraws();
2648}
2649
Chris Dalton1d616352017-05-31 12:51:23 -06002650void GrGLGpu::sendInstancedMeshToGpu(const GrPrimitiveProcessor& primProc, GrPrimitiveType
2651 primitiveType, const GrBuffer* vertexBuffer,
2652 int vertexCount, int baseVertex,
2653 const GrBuffer* instanceBuffer, int instanceCount,
2654 int baseInstance) {
Chris Daltonda40cd22018-04-16 13:19:58 -06002655 if (fRequiresFlushBeforeNextInstancedDraw) {
2656 SkASSERT(this->glCaps().requiresFlushBetweenNonAndInstancedDraws());
2657 GL_CALL(Flush());
2658 fRequiresFlushBeforeNextInstancedDraw = false;
2659 }
Chris Daltoncc604e52017-10-06 16:27:32 -06002660 GrGLenum glPrimType = gr_primitive_type_to_gl_mode(primitiveType);
2661 int maxInstances = this->glCaps().maxInstancesPerDrawArraysWithoutCrashing(instanceCount);
2662 for (int i = 0; i < instanceCount; i += maxInstances) {
2663 this->setupGeometry(primProc, nullptr, vertexBuffer, 0, instanceBuffer, baseInstance + i);
2664 GL_CALL(DrawArraysInstanced(glPrimType, baseVertex, vertexCount,
2665 SkTMin(instanceCount - i, maxInstances)));
2666 fStats.incNumDraws();
2667 }
Chris Dalton1d616352017-05-31 12:51:23 -06002668}
2669
2670void GrGLGpu::sendIndexedInstancedMeshToGpu(const GrPrimitiveProcessor& primProc,
2671 GrPrimitiveType primitiveType,
2672 const GrBuffer* indexBuffer, int indexCount,
2673 int baseIndex, const GrBuffer* vertexBuffer,
2674 int baseVertex, const GrBuffer* instanceBuffer,
2675 int instanceCount, int baseInstance) {
Chris Daltonda40cd22018-04-16 13:19:58 -06002676 if (fRequiresFlushBeforeNextInstancedDraw) {
2677 SkASSERT(this->glCaps().requiresFlushBetweenNonAndInstancedDraws());
2678 GL_CALL(Flush());
2679 fRequiresFlushBeforeNextInstancedDraw = false;
2680 }
Chris Dalton3809bab2017-06-13 10:55:06 -06002681 const GrGLenum glPrimType = gr_primitive_type_to_gl_mode(primitiveType);
Chris Dalton1d616352017-05-31 12:51:23 -06002682 GrGLvoid* indices = reinterpret_cast<void*>(indexBuffer->baseOffset() +
2683 sizeof(uint16_t) * baseIndex);
2684 this->setupGeometry(primProc, indexBuffer, vertexBuffer, baseVertex,
2685 instanceBuffer, baseInstance);
2686 GL_CALL(DrawElementsInstanced(glPrimType, indexCount, GR_GL_UNSIGNED_SHORT, indices,
2687 instanceCount));
2688 fStats.incNumDraws();
2689}
2690
Brian Salomon1fabd512018-02-09 09:54:25 -05002691void GrGLGpu::onResolveRenderTarget(GrRenderTarget* target) {
bsalomon@google.com75f9f252012-01-31 13:35:56 +00002692 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00002693 if (rt->needsResolve()) {
bsalomon@google.com347c3822013-05-01 20:10:01 +00002694 // Some extensions automatically resolves the texture when it is read.
2695 if (this->glCaps().usesMSAARenderBuffers()) {
egdanield803f272015-03-18 13:01:52 -07002696 SkASSERT(rt->textureFBOID() != rt->renderFBOID());
Brian Osmancfe83d12018-01-19 11:13:45 -05002697 SkASSERT(rt->textureFBOID() != 0 && rt->renderFBOID() != 0);
Adrienne Walker4ee88512018-05-17 11:37:14 -07002698 this->bindFramebuffer(GR_GL_READ_FRAMEBUFFER, rt->renderFBOID());
2699 this->bindFramebuffer(GR_GL_DRAW_FRAMEBUFFER, rt->textureFBOID());
2700
egdanield803f272015-03-18 13:01:52 -07002701 // make sure we go through flushRenderTarget() since we've modified
2702 // the bound DRAW FBO ID.
Robert Phillips294870f2016-11-11 12:38:40 -05002703 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002704 const GrGLIRect& vp = rt->getViewport();
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00002705 const SkIRect dirtyRect = rt->getResolveRect();
Brian Salomon1fabd512018-02-09 09:54:25 -05002706 // The dirty rect tracked on the RT is always stored in the native coordinates of the
2707 // surface. Choose kTopLeft so no adjustments are made
2708 static constexpr auto kDirtyRectOrigin = kTopLeft_GrSurfaceOrigin;
bsalomon@google.com347c3822013-05-01 20:10:01 +00002709 if (GrGLCaps::kES_Apple_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002710 // Apple's extension uses the scissor as the blit bounds.
bsalomon3e791242014-12-17 13:43:13 -08002711 GrScissorState scissorState;
robertphillipse85a32d2015-02-10 08:16:55 -08002712 scissorState.set(dirtyRect);
Brian Salomon1fabd512018-02-09 09:54:25 -05002713 this->flushScissor(scissorState, vp, kDirtyRectOrigin);
csmartdalton28341fa2016-08-17 10:00:21 -07002714 this->disableWindowRectangles();
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002715 GL_CALL(ResolveMultisampleFramebuffer());
2716 } else {
Brian Salomone5e7eb12016-10-14 16:18:33 -04002717 int l, b, r, t;
2718 if (GrGLCaps::kResolveMustBeFull_BlitFrambufferFlag &
2719 this->glCaps().blitFramebufferSupportFlags()) {
2720 l = 0;
2721 b = 0;
2722 r = target->width();
2723 t = target->height();
2724 } else {
2725 GrGLIRect rect;
Brian Salomon1fabd512018-02-09 09:54:25 -05002726 rect.setRelativeTo(vp, dirtyRect, kDirtyRectOrigin);
Brian Salomone5e7eb12016-10-14 16:18:33 -04002727 l = rect.fLeft;
2728 b = rect.fBottom;
2729 r = rect.fLeft + rect.fWidth;
2730 t = rect.fBottom + rect.fHeight;
2731 }
derekf8c8f71a2014-09-16 06:24:57 -07002732
2733 // BlitFrameBuffer respects the scissor, so disable it.
joshualitt77b13072014-10-27 14:51:01 -07002734 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07002735 this->disableWindowRectangles();
Brian Salomone5e7eb12016-10-14 16:18:33 -04002736 GL_CALL(BlitFramebuffer(l, b, r, t, l, b, r, t,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002737 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00002738 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002739 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00002740 rt->flagAsResolved();
reed@google.comac10a2d2010-12-22 21:39:39 +00002741 }
2742}
2743
bsalomon@google.com411dad02012-06-05 20:24:20 +00002744namespace {
bsalomon@google.comd302f142011-03-03 13:54:13 +00002745
bsalomon@google.com411dad02012-06-05 20:24:20 +00002746
2747GrGLenum gr_to_gl_stencil_op(GrStencilOp op) {
cdalton93a379b2016-05-11 13:58:08 -07002748 static const GrGLenum gTable[kGrStencilOpCount] = {
2749 GR_GL_KEEP, // kKeep
2750 GR_GL_ZERO, // kZero
2751 GR_GL_REPLACE, // kReplace
2752 GR_GL_INVERT, // kInvert
2753 GR_GL_INCR_WRAP, // kIncWrap
2754 GR_GL_DECR_WRAP, // kDecWrap
2755 GR_GL_INCR, // kIncClamp
2756 GR_GL_DECR, // kDecClamp
bsalomon@google.com411dad02012-06-05 20:24:20 +00002757 };
cdalton93a379b2016-05-11 13:58:08 -07002758 GR_STATIC_ASSERT(0 == (int)GrStencilOp::kKeep);
2759 GR_STATIC_ASSERT(1 == (int)GrStencilOp::kZero);
2760 GR_STATIC_ASSERT(2 == (int)GrStencilOp::kReplace);
2761 GR_STATIC_ASSERT(3 == (int)GrStencilOp::kInvert);
2762 GR_STATIC_ASSERT(4 == (int)GrStencilOp::kIncWrap);
2763 GR_STATIC_ASSERT(5 == (int)GrStencilOp::kDecWrap);
2764 GR_STATIC_ASSERT(6 == (int)GrStencilOp::kIncClamp);
2765 GR_STATIC_ASSERT(7 == (int)GrStencilOp::kDecClamp);
2766 SkASSERT(op < (GrStencilOp)kGrStencilOpCount);
2767 return gTable[(int)op];
bsalomon@google.com411dad02012-06-05 20:24:20 +00002768}
2769
2770void set_gl_stencil(const GrGLInterface* gl,
cdalton93a379b2016-05-11 13:58:08 -07002771 const GrStencilSettings::Face& face,
2772 GrGLenum glFace) {
2773 GrGLenum glFunc = GrToGLStencilFunc(face.fTest);
2774 GrGLenum glFailOp = gr_to_gl_stencil_op(face.fFailOp);
2775 GrGLenum glPassOp = gr_to_gl_stencil_op(face.fPassOp);
bsalomon@google.coma3201942012-06-21 19:58:20 +00002776
cdalton93a379b2016-05-11 13:58:08 -07002777 GrGLint ref = face.fRef;
2778 GrGLint mask = face.fTestMask;
2779 GrGLint writeMask = face.fWriteMask;
bsalomon@google.com411dad02012-06-05 20:24:20 +00002780
2781 if (GR_GL_FRONT_AND_BACK == glFace) {
2782 // we call the combined func just in case separate stencil is not
2783 // supported.
2784 GR_GL_CALL(gl, StencilFunc(glFunc, ref, mask));
2785 GR_GL_CALL(gl, StencilMask(writeMask));
vbuzinovc5d58f02015-08-21 05:24:24 -07002786 GR_GL_CALL(gl, StencilOp(glFailOp, GR_GL_KEEP, glPassOp));
bsalomon@google.com411dad02012-06-05 20:24:20 +00002787 } else {
2788 GR_GL_CALL(gl, StencilFuncSeparate(glFace, glFunc, ref, mask));
2789 GR_GL_CALL(gl, StencilMaskSeparate(glFace, writeMask));
vbuzinovc5d58f02015-08-21 05:24:24 -07002790 GR_GL_CALL(gl, StencilOpSeparate(glFace, glFailOp, GR_GL_KEEP, glPassOp));
bsalomon@google.com411dad02012-06-05 20:24:20 +00002791 }
2792}
2793}
bsalomon@google.comd302f142011-03-03 13:54:13 +00002794
bsalomon3e791242014-12-17 13:43:13 -08002795void GrGLGpu::flushStencil(const GrStencilSettings& stencilSettings) {
csmartdaltonc7d85332016-10-26 10:13:46 -07002796 if (stencilSettings.isDisabled()) {
2797 this->disableStencil();
2798 } else if (fHWStencilSettings != stencilSettings) {
2799 if (kYes_TriState != fHWStencilTestEnabled) {
2800 GL_CALL(Enable(GR_GL_STENCIL_TEST));
Brian Salomonaf971de2017-06-08 16:11:33 -04002801
csmartdaltonc7d85332016-10-26 10:13:46 -07002802 fHWStencilTestEnabled = kYes_TriState;
bsalomon@google.coma3201942012-06-21 19:58:20 +00002803 }
csmartdaltonc7d85332016-10-26 10:13:46 -07002804 if (stencilSettings.isTwoSided()) {
csmartdaltonc7d85332016-10-26 10:13:46 -07002805 set_gl_stencil(this->glInterface(),
2806 stencilSettings.front(),
2807 GR_GL_FRONT);
2808 set_gl_stencil(this->glInterface(),
2809 stencilSettings.back(),
2810 GR_GL_BACK);
2811 } else {
2812 set_gl_stencil(this->glInterface(),
2813 stencilSettings.front(),
2814 GR_GL_FRONT_AND_BACK);
bsalomon@google.comd302f142011-03-03 13:54:13 +00002815 }
joshualitta58fe352014-10-27 08:39:00 -07002816 fHWStencilSettings = stencilSettings;
reed@google.comac10a2d2010-12-22 21:39:39 +00002817 }
2818}
2819
csmartdaltonc7d85332016-10-26 10:13:46 -07002820void GrGLGpu::disableStencil() {
2821 if (kNo_TriState != fHWStencilTestEnabled) {
2822 GL_CALL(Disable(GR_GL_STENCIL_TEST));
Brian Salomonaf971de2017-06-08 16:11:33 -04002823
csmartdaltonc7d85332016-10-26 10:13:46 -07002824 fHWStencilTestEnabled = kNo_TriState;
2825 fHWStencilSettings.invalidate();
2826 }
2827}
2828
cdaltonaf8bc7d2016-02-05 09:35:20 -08002829void GrGLGpu::flushHWAAState(GrRenderTarget* rt, bool useHWAA, bool stencilEnabled) {
bsalomon083617b2016-02-12 12:10:14 -08002830 // rt is only optional if useHWAA is false.
2831 SkASSERT(rt || !useHWAA);
vbuzinovdded6962015-06-12 08:59:45 -07002832 SkASSERT(!useHWAA || rt->isStencilBufferMultisampled());
bsalomon@google.com202d1392013-03-19 18:58:08 +00002833
csmartdalton2b5f2cb2016-06-10 14:06:32 -07002834 if (this->caps()->multisampleDisableSupport()) {
cdaltond0a840d2015-03-16 17:19:58 -07002835 if (useHWAA) {
2836 if (kYes_TriState != fMSAAEnabled) {
2837 GL_CALL(Enable(GR_GL_MULTISAMPLE));
2838 fMSAAEnabled = kYes_TriState;
2839 }
2840 } else {
2841 if (kNo_TriState != fMSAAEnabled) {
2842 GL_CALL(Disable(GR_GL_MULTISAMPLE));
2843 fMSAAEnabled = kNo_TriState;
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00002844 }
2845 }
2846 }
cdaltonaf8bc7d2016-02-05 09:35:20 -08002847
2848 if (0 != this->caps()->maxRasterSamples()) {
Brian Salomon7c8460e2017-05-12 11:36:10 -04002849 if (useHWAA && GrFSAAType::kMixedSamples == rt->fsaaType() && !stencilEnabled) {
cdaltonaf8bc7d2016-02-05 09:35:20 -08002850 // Since stencil is disabled and we want more samples than are in the color buffer, we
2851 // need to tell the rasterizer explicitly how many to run.
2852 if (kYes_TriState != fHWRasterMultisampleEnabled) {
2853 GL_CALL(Enable(GR_GL_RASTER_MULTISAMPLE));
2854 fHWRasterMultisampleEnabled = kYes_TriState;
2855 }
Brian Salomonbdecacf2018-02-02 20:32:49 -05002856 int numStencilSamples = rt->numStencilSamples();
2857 // convert to GL's understanding of sample counts where 0 means nonMSAA.
2858 numStencilSamples = 1 == numStencilSamples ? 0 : numStencilSamples;
2859 if (numStencilSamples != fHWNumRasterSamples) {
2860 SkASSERT(numStencilSamples <= this->caps()->maxRasterSamples());
2861 GL_CALL(RasterSamples(numStencilSamples, GR_GL_TRUE));
2862 fHWNumRasterSamples = numStencilSamples;
cdaltonaf8bc7d2016-02-05 09:35:20 -08002863 }
2864 } else {
2865 if (kNo_TriState != fHWRasterMultisampleEnabled) {
2866 GL_CALL(Disable(GR_GL_RASTER_MULTISAMPLE));
2867 fHWRasterMultisampleEnabled = kNo_TriState;
2868 }
2869 }
2870 } else {
Brian Salomon7c8460e2017-05-12 11:36:10 -04002871 SkASSERT(!useHWAA || GrFSAAType::kMixedSamples != rt->fsaaType() || stencilEnabled);
cdaltonaf8bc7d2016-02-05 09:35:20 -08002872 }
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00002873}
2874
bsalomon7f9b2e42016-01-12 13:29:26 -08002875void GrGLGpu::flushBlend(const GrXferProcessor::BlendInfo& blendInfo, const GrSwizzle& swizzle) {
egdanielb414f252014-07-29 13:15:47 -07002876 // Any optimization to disable blending should have already been applied and
cdalton8917d622015-05-06 13:40:21 -07002877 // tweaked the equation to "add" or "subtract", and the coeffs to (1, 0).
bsalomonf7cc8772015-05-11 11:21:14 -07002878
cdalton8917d622015-05-06 13:40:21 -07002879 GrBlendEquation equation = blendInfo.fEquation;
egdanielc2304142014-12-11 13:15:13 -08002880 GrBlendCoeff srcCoeff = blendInfo.fSrcBlend;
2881 GrBlendCoeff dstCoeff = blendInfo.fDstBlend;
cdalton8917d622015-05-06 13:40:21 -07002882 bool blendOff = (kAdd_GrBlendEquation == equation || kSubtract_GrBlendEquation == equation) &&
2883 kOne_GrBlendCoeff == srcCoeff && kZero_GrBlendCoeff == dstCoeff;
egdanielb414f252014-07-29 13:15:47 -07002884 if (blendOff) {
2885 if (kNo_TriState != fHWBlendState.fEnabled) {
2886 GL_CALL(Disable(GR_GL_BLEND));
joel.liang9764c402015-07-09 19:46:18 -07002887
2888 // Workaround for the ARM KHR_blend_equation_advanced blacklist issue
2889 // https://code.google.com/p/skia/issues/detail?id=3943
2890 if (kARM_GrGLVendor == this->ctxInfo().vendor() &&
2891 GrBlendEquationIsAdvanced(fHWBlendState.fEquation)) {
2892 SkASSERT(this->caps()->advancedBlendEquationSupport());
2893 // Set to any basic blending equation.
2894 GrBlendEquation blend_equation = kAdd_GrBlendEquation;
2895 GL_CALL(BlendEquation(gXfermodeEquation2Blend[blend_equation]));
2896 fHWBlendState.fEquation = blend_equation;
2897 }
2898
egdanielb414f252014-07-29 13:15:47 -07002899 fHWBlendState.fEnabled = kNo_TriState;
2900 }
cdalton8917d622015-05-06 13:40:21 -07002901 return;
2902 }
2903
2904 if (kYes_TriState != fHWBlendState.fEnabled) {
2905 GL_CALL(Enable(GR_GL_BLEND));
Brian Salomonaf971de2017-06-08 16:11:33 -04002906
cdalton8917d622015-05-06 13:40:21 -07002907 fHWBlendState.fEnabled = kYes_TriState;
2908 }
2909
2910 if (fHWBlendState.fEquation != equation) {
2911 GL_CALL(BlendEquation(gXfermodeEquation2Blend[equation]));
2912 fHWBlendState.fEquation = equation;
2913 }
2914
2915 if (GrBlendEquationIsAdvanced(equation)) {
2916 SkASSERT(this->caps()->advancedBlendEquationSupport());
2917 // Advanced equations have no other blend state.
2918 return;
2919 }
2920
bsalomone63ffef2016-02-05 07:17:34 -08002921 if (fHWBlendState.fSrcCoeff != srcCoeff || fHWBlendState.fDstCoeff != dstCoeff) {
cdalton8917d622015-05-06 13:40:21 -07002922 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
2923 gXfermodeCoeff2Blend[dstCoeff]));
2924 fHWBlendState.fSrcCoeff = srcCoeff;
2925 fHWBlendState.fDstCoeff = dstCoeff;
2926 }
2927
bsalomon7f9b2e42016-01-12 13:29:26 -08002928 if ((BlendCoeffReferencesConstant(srcCoeff) || BlendCoeffReferencesConstant(dstCoeff))) {
2929 GrColor blendConst = blendInfo.fBlendConstant;
2930 blendConst = swizzle.applyTo(blendConst);
2931 if (!fHWBlendState.fConstColorValid || fHWBlendState.fConstColor != blendConst) {
2932 GrGLfloat c[4];
2933 GrColorToRGBAFloat(blendConst, c);
2934 GL_CALL(BlendColor(c[0], c[1], c[2], c[3]));
2935 fHWBlendState.fConstColor = blendConst;
2936 fHWBlendState.fConstColorValid = true;
2937 }
bsalomon@google.com0650e812011-04-08 18:07:53 +00002938 }
2939}
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002940
Brian Salomon2bbdcc42017-09-07 12:36:34 -04002941static inline GrGLenum wrap_mode_to_gl_wrap(GrSamplerState::WrapMode wrapMode) {
2942 switch (wrapMode) {
2943 case GrSamplerState::WrapMode::kClamp:
2944 return GR_GL_CLAMP_TO_EDGE;
2945 case GrSamplerState::WrapMode::kRepeat:
2946 return GR_GL_REPEAT;
2947 case GrSamplerState::WrapMode::kMirrorRepeat:
2948 return GR_GL_MIRRORED_REPEAT;
bsalomon@google.comb8670992012-07-25 21:27:09 +00002949 };
Brian Salomon2bbdcc42017-09-07 12:36:34 -04002950 SK_ABORT("Unknown wrap mode");
2951 return 0;
bsalomon@google.comb8670992012-07-25 21:27:09 +00002952}
2953
egdanielb7e7d572015-11-04 04:23:53 -08002954static GrGLenum get_component_enum_from_char(char component) {
2955 switch (component) {
2956 case 'r':
2957 return GR_GL_RED;
2958 case 'g':
2959 return GR_GL_GREEN;
2960 case 'b':
2961 return GR_GL_BLUE;
2962 case 'a':
2963 return GR_GL_ALPHA;
2964 default:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -04002965 SK_ABORT("Unsupported component");
egdanielb7e7d572015-11-04 04:23:53 -08002966 return 0;
2967 }
2968}
2969
2970/** If texture swizzling is available using tex parameters then it is preferred over mangling
2971 the generated shader code. This potentially allows greater reuse of cached shaders. */
2972static void get_tex_param_swizzle(GrPixelConfig config,
bsalomoncdee0092016-01-08 13:20:12 -08002973 const GrGLCaps& caps,
egdanielb7e7d572015-11-04 04:23:53 -08002974 GrGLenum* glSwizzle) {
bsalomoncdee0092016-01-08 13:20:12 -08002975 const GrSwizzle& swizzle = caps.configSwizzle(config);
egdanielb7e7d572015-11-04 04:23:53 -08002976 for (int i = 0; i < 4; ++i) {
bsalomoncdee0092016-01-08 13:20:12 -08002977 glSwizzle[i] = get_component_enum_from_char(swizzle.c_str()[i]);
egdaniel574a4c12015-11-02 06:22:44 -08002978 }
2979}
2980
Brian Salomon2bbdcc42017-09-07 12:36:34 -04002981static GrGLenum filter_to_gl_mag_filter(GrSamplerState::Filter filter) {
2982 switch (filter) {
2983 case GrSamplerState::Filter::kNearest:
2984 return GR_GL_NEAREST;
2985 case GrSamplerState::Filter::kBilerp:
2986 return GR_GL_LINEAR;
2987 case GrSamplerState::Filter::kMipMap:
2988 return GR_GL_LINEAR;
2989 }
2990 SK_ABORT("Unknown filter");
2991 return 0;
2992}
2993
2994static GrGLenum filter_to_gl_min_filter(GrSamplerState::Filter filter) {
2995 switch (filter) {
2996 case GrSamplerState::Filter::kNearest:
2997 return GR_GL_NEAREST;
2998 case GrSamplerState::Filter::kBilerp:
2999 return GR_GL_LINEAR;
3000 case GrSamplerState::Filter::kMipMap:
3001 return GR_GL_LINEAR_MIPMAP_LINEAR;
3002 }
3003 SK_ABORT("Unknown filter");
3004 return 0;
3005}
3006
3007void GrGLGpu::bindTexture(int unitIdx, const GrSamplerState& samplerState, bool allowSRGBInputs,
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003008 GrGLTexture* texture, GrSurfaceOrigin textureOrigin) {
bsalomon49f085d2014-09-05 13:34:00 -07003009 SkASSERT(texture);
tomhudson@google.comd0c1a062012-07-12 17:23:52 +00003010
reed856e9d92015-09-30 12:21:45 -07003011#ifdef SK_DEBUG
3012 if (!this->caps()->npotTextureTileSupport()) {
Brian Salomon2bbdcc42017-09-07 12:36:34 -04003013 if (samplerState.isRepeated()) {
reed856e9d92015-09-30 12:21:45 -07003014 const int w = texture->width();
3015 const int h = texture->height();
3016 SkASSERT(SkIsPow2(w) && SkIsPow2(h));
3017 }
3018 }
3019#endif
3020
bsalomon@google.comb8670992012-07-25 21:27:09 +00003021 // If we created a rt/tex and rendered to it without using a texture and now we're texturing
3022 // 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 +00003023 // out of the "last != next" check.
bsalomon37dd3312014-11-03 08:47:23 -08003024 GrGLRenderTarget* texRT = static_cast<GrGLRenderTarget*>(texture->asRenderTarget());
bsalomon49f085d2014-09-05 13:34:00 -07003025 if (texRT) {
Brian Salomon1fabd512018-02-09 09:54:25 -05003026 this->onResolveRenderTarget(texRT);
bsalomon@google.com4c883782012-06-04 19:05:11 +00003027 }
3028
Robert Phillips294870f2016-11-11 12:38:40 -05003029 GrGpuResource::UniqueID textureID = texture->uniqueID();
bsalomon10528f12015-10-14 12:54:52 -07003030 GrGLenum target = texture->target();
bsalomon1c63bf62014-07-22 13:09:46 -07003031 if (fHWBoundTextureUniqueIDs[unitIdx] != textureID) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00003032 this->setTextureUnit(unitIdx);
bsalomon10528f12015-10-14 12:54:52 -07003033 GL_CALL(BindTexture(target, texture->textureID()));
bsalomon1c63bf62014-07-22 13:09:46 -07003034 fHWBoundTextureUniqueIDs[unitIdx] = textureID;
bsalomon@google.com4c883782012-06-04 19:05:11 +00003035 }
3036
bsalomon@google.com4c883782012-06-04 19:05:11 +00003037 ResetTimestamp timestamp;
bsalomon@google.com34cccde2013-01-04 18:34:30 +00003038 const GrGLTexture::TexParams& oldTexParams = texture->getCachedTexParams(&timestamp);
bsalomon@google.com4c883782012-06-04 19:05:11 +00003039 bool setAll = timestamp < this->getResetTimestamp();
3040 GrGLTexture::TexParams newTexParams;
3041
Brian Salomon2bbdcc42017-09-07 12:36:34 -04003042 GrSamplerState::Filter filterMode = samplerState.filter();
bsalomonefd7d452014-10-23 14:17:46 -07003043
Brian Salomon2bbdcc42017-09-07 12:36:34 -04003044 if (GrSamplerState::Filter::kMipMap == filterMode) {
Robert Phillips92de6312017-05-23 07:43:48 -04003045 if (!this->caps()->mipMapSupport()) {
Brian Salomon2bbdcc42017-09-07 12:36:34 -04003046 filterMode = GrSamplerState::Filter::kBilerp;
bsalomonefd7d452014-10-23 14:17:46 -07003047 }
commit-bot@chromium.org47442312013-12-19 16:18:01 +00003048 }
bsalomonefd7d452014-10-23 14:17:46 -07003049
Brian Salomon2bbdcc42017-09-07 12:36:34 -04003050 newTexParams.fMinFilter = filter_to_gl_min_filter(filterMode);
3051 newTexParams.fMagFilter = filter_to_gl_mag_filter(filterMode);
skia.committer@gmail.comaeefb2a2013-07-27 07:01:06 +00003052
brianosman20471892016-12-02 06:43:32 -08003053 if (this->glCaps().srgbDecodeDisableSupport() && GrPixelConfigIsSRGB(texture->config())) {
brianosmanfe199872016-06-13 07:59:48 -07003054 newTexParams.fSRGBDecode = allowSRGBInputs ? GR_GL_DECODE_EXT : GR_GL_SKIP_DECODE_EXT;
brianosman33f6b3f2016-06-02 05:49:21 -07003055 if (setAll || newTexParams.fSRGBDecode != oldTexParams.fSRGBDecode) {
brianosmanadf4fa62016-04-26 16:48:06 -07003056 this->setTextureUnit(unitIdx);
brianosman33f6b3f2016-06-02 05:49:21 -07003057 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SRGB_DECODE_EXT, newTexParams.fSRGBDecode));
cblume55f2d2d2016-02-26 13:20:48 -08003058 }
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +00003059 }
bsalomon@google.com4c883782012-06-04 19:05:11 +00003060
brianosman33f6b3f2016-06-02 05:49:21 -07003061#ifdef SK_DEBUG
3062 // We were supposed to ensure MipMaps were up-to-date and built correctly before getting here.
Brian Salomon2bbdcc42017-09-07 12:36:34 -04003063 if (GrSamplerState::Filter::kMipMap == filterMode) {
brianosman33f6b3f2016-06-02 05:49:21 -07003064 SkASSERT(!texture->texturePriv().mipMapsAreDirty());
3065 if (GrPixelConfigIsSRGB(texture->config())) {
Brian Osman7b8400d2016-11-08 17:08:54 -05003066 SkDestinationSurfaceColorMode colorMode = allowSRGBInputs
3067 ? SkDestinationSurfaceColorMode::kGammaAndColorSpaceAware
3068 : SkDestinationSurfaceColorMode::kLegacy;
3069 SkASSERT(texture->texturePriv().mipColorMode() == colorMode);
brianosman33f6b3f2016-06-02 05:49:21 -07003070 }
3071 }
3072#endif
3073
cblume55f2d2d2016-02-26 13:20:48 -08003074 newTexParams.fMaxMipMapLevel = texture->texturePriv().maxMipMapLevel();
3075
Brian Salomon2bbdcc42017-09-07 12:36:34 -04003076 newTexParams.fWrapS = wrap_mode_to_gl_wrap(samplerState.wrapModeX());
3077 newTexParams.fWrapT = wrap_mode_to_gl_wrap(samplerState.wrapModeY());
bsalomoncdee0092016-01-08 13:20:12 -08003078 get_tex_param_swizzle(texture->config(), this->glCaps(), newTexParams.fSwizzleRGBA);
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00003079 if (setAll || newTexParams.fMagFilter != oldTexParams.fMagFilter) {
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_MAG_FILTER, newTexParams.fMagFilter));
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00003082 }
3083 if (setAll || newTexParams.fMinFilter != oldTexParams.fMinFilter) {
3084 this->setTextureUnit(unitIdx);
bsalomon10528f12015-10-14 12:54:52 -07003085 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MIN_FILTER, newTexParams.fMinFilter));
bsalomon@google.com4c883782012-06-04 19:05:11 +00003086 }
cblume55f2d2d2016-02-26 13:20:48 -08003087 if (setAll || newTexParams.fMaxMipMapLevel != oldTexParams.fMaxMipMapLevel) {
cblume09bd2c02016-03-01 14:08:28 -08003088 // These are not supported in ES2 contexts
3089 if (this->glCaps().mipMapLevelAndLodControlSupport()) {
3090 if (newTexParams.fMaxMipMapLevel != 0) {
3091 this->setTextureUnit(unitIdx);
3092 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MIN_LOD, 0));
3093 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_BASE_LEVEL, 0));
3094 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MAX_LOD,
3095 newTexParams.fMaxMipMapLevel));
3096 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MAX_LEVEL,
3097 newTexParams.fMaxMipMapLevel));
3098 }
cblume55f2d2d2016-02-26 13:20:48 -08003099 }
3100 }
bsalomon@google.com4c883782012-06-04 19:05:11 +00003101 if (setAll || newTexParams.fWrapS != oldTexParams.fWrapS) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00003102 this->setTextureUnit(unitIdx);
bsalomon10528f12015-10-14 12:54:52 -07003103 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_WRAP_S, newTexParams.fWrapS));
bsalomon@google.com4c883782012-06-04 19:05:11 +00003104 }
3105 if (setAll || newTexParams.fWrapT != oldTexParams.fWrapT) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00003106 this->setTextureUnit(unitIdx);
bsalomon10528f12015-10-14 12:54:52 -07003107 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_WRAP_T, newTexParams.fWrapT));
bsalomon@google.com4c883782012-06-04 19:05:11 +00003108 }
bsalomoncdee0092016-01-08 13:20:12 -08003109 if (this->glCaps().textureSwizzleSupport() &&
bsalomon@google.com4c883782012-06-04 19:05:11 +00003110 (setAll || memcmp(newTexParams.fSwizzleRGBA,
3111 oldTexParams.fSwizzleRGBA,
3112 sizeof(newTexParams.fSwizzleRGBA)))) {
cdalton74b8d322016-04-11 14:47:28 -07003113 this->setTextureSwizzle(unitIdx, target, newTexParams.fSwizzleRGBA);
bsalomon@google.com4c883782012-06-04 19:05:11 +00003114 }
bsalomon@google.com34cccde2013-01-04 18:34:30 +00003115 texture->setCachedTexParams(newTexParams, this->getResetTimestamp());
bsalomon@google.com4c883782012-06-04 19:05:11 +00003116}
3117
csmartdalton1897cfd2016-06-03 08:50:54 -07003118void GrGLGpu::bindTexelBuffer(int unitIdx, GrPixelConfig texelConfig, GrGLBuffer* buffer) {
cdalton74b8d322016-04-11 14:47:28 -07003119 SkASSERT(this->glCaps().canUseConfigWithTexelBuffer(texelConfig));
3120 SkASSERT(unitIdx >= 0 && unitIdx < fHWBufferTextures.count());
cdalton74b8d322016-04-11 14:47:28 -07003121
3122 BufferTexture& buffTex = fHWBufferTextures[unitIdx];
3123
3124 if (!buffTex.fKnownBound) {
3125 if (!buffTex.fTextureID) {
3126 GL_CALL(GenTextures(1, &buffTex.fTextureID));
3127 if (!buffTex.fTextureID) {
3128 return;
3129 }
3130 }
3131
3132 this->setTextureUnit(unitIdx);
3133 GL_CALL(BindTexture(GR_GL_TEXTURE_BUFFER, buffTex.fTextureID));
3134
3135 buffTex.fKnownBound = true;
3136 }
3137
robertphillips8abb3702016-08-31 14:04:06 -07003138 if (buffer->uniqueID() != buffTex.fAttachedBufferUniqueID ||
csmartdalton1897cfd2016-06-03 08:50:54 -07003139 buffTex.fTexelConfig != texelConfig) {
cdalton74b8d322016-04-11 14:47:28 -07003140
3141 this->setTextureUnit(unitIdx);
csmartdalton1897cfd2016-06-03 08:50:54 -07003142 GL_CALL(TexBuffer(GR_GL_TEXTURE_BUFFER,
3143 this->glCaps().configSizedInternalFormat(texelConfig),
3144 buffer->bufferID()));
cdalton74b8d322016-04-11 14:47:28 -07003145
cdalton74b8d322016-04-11 14:47:28 -07003146 buffTex.fTexelConfig = texelConfig;
robertphillips8abb3702016-08-31 14:04:06 -07003147 buffTex.fAttachedBufferUniqueID = buffer->uniqueID();
cdalton74b8d322016-04-11 14:47:28 -07003148
3149 if (this->glCaps().textureSwizzleSupport() &&
3150 this->glCaps().configSwizzle(texelConfig) != buffTex.fSwizzle) {
3151 GrGLenum glSwizzle[4];
3152 get_tex_param_swizzle(texelConfig, this->glCaps(), glSwizzle);
3153 this->setTextureSwizzle(unitIdx, GR_GL_TEXTURE_BUFFER, glSwizzle);
3154 buffTex.fSwizzle = this->glCaps().configSwizzle(texelConfig);
3155 }
3156
3157 buffer->setHasAttachedToTexture();
3158 fHWMaxUsedBufferTextureUnit = SkTMax(unitIdx, fHWMaxUsedBufferTextureUnit);
3159 }
3160}
3161
Brian Salomon2bbdcc42017-09-07 12:36:34 -04003162void GrGLGpu::generateMipmaps(const GrSamplerState& params, bool allowSRGBInputs,
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003163 GrGLTexture* texture, GrSurfaceOrigin textureOrigin) {
brianosman33f6b3f2016-06-02 05:49:21 -07003164 SkASSERT(texture);
3165
3166 // First, figure out if we need mips for this texture at all:
Brian Salomon2bbdcc42017-09-07 12:36:34 -04003167 GrSamplerState::Filter filterMode = params.filter();
brianosman33f6b3f2016-06-02 05:49:21 -07003168
Brian Salomon2bbdcc42017-09-07 12:36:34 -04003169 if (GrSamplerState::Filter::kMipMap == filterMode) {
Robert Phillips92de6312017-05-23 07:43:48 -04003170 if (!this->caps()->mipMapSupport()) {
Brian Salomon2bbdcc42017-09-07 12:36:34 -04003171 filterMode = GrSamplerState::Filter::kBilerp;
brianosman33f6b3f2016-06-02 05:49:21 -07003172 }
3173 }
3174
Brian Salomon2bbdcc42017-09-07 12:36:34 -04003175 if (GrSamplerState::Filter::kMipMap != filterMode) {
brianosman33f6b3f2016-06-02 05:49:21 -07003176 return;
3177 }
3178
3179 // If this is an sRGB texture and the mips were previously built the "other" way
3180 // (gamma-correct vs. not), then we need to rebuild them. We don't need to check for
3181 // srgbSupport - we'll *never* get an sRGB pixel config if we don't support it.
Brian Osman7b8400d2016-11-08 17:08:54 -05003182 SkDestinationSurfaceColorMode colorMode = allowSRGBInputs
3183 ? SkDestinationSurfaceColorMode::kGammaAndColorSpaceAware
3184 : SkDestinationSurfaceColorMode::kLegacy;
brianosman33f6b3f2016-06-02 05:49:21 -07003185 if (GrPixelConfigIsSRGB(texture->config()) &&
Brian Osman7b8400d2016-11-08 17:08:54 -05003186 colorMode != texture->texturePriv().mipColorMode()) {
Greg Daniel0fc4d2d2017-10-12 11:23:36 -04003187 texture->texturePriv().markMipMapsDirty();
brianosman33f6b3f2016-06-02 05:49:21 -07003188 }
3189
3190 // If the mips aren't dirty, we're done:
3191 if (!texture->texturePriv().mipMapsAreDirty()) {
3192 return;
3193 }
3194
3195 // If we created a rt/tex and rendered to it without using a texture and now we're texturing
3196 // from the rt it will still be the last bound texture, but it needs resolving.
3197 GrGLRenderTarget* texRT = static_cast<GrGLRenderTarget*>(texture->asRenderTarget());
3198 if (texRT) {
Brian Salomon1fabd512018-02-09 09:54:25 -05003199 this->onResolveRenderTarget(texRT);
brianosman33f6b3f2016-06-02 05:49:21 -07003200 }
3201
3202 GrGLenum target = texture->target();
3203 this->setScratchTextureUnit();
3204 GL_CALL(BindTexture(target, texture->textureID()));
3205
3206 // Configure sRGB decode, if necessary. This state is the only thing needed for the driver
3207 // call (glGenerateMipmap) to work correctly. Our manual method dirties other state, too.
brianosman20471892016-12-02 06:43:32 -08003208 if (this->glCaps().srgbDecodeDisableSupport() && GrPixelConfigIsSRGB(texture->config())) {
Brian Osmanb0403202016-12-09 10:55:23 -05003209 GrGLenum srgbDecode = allowSRGBInputs ? GR_GL_DECODE_EXT : GR_GL_SKIP_DECODE_EXT;
3210 // Command buffer's sRGB decode extension doesn't influence mipmap generation correctly.
3211 // If we set this to skip_decode, it appears to suppress sRGB -> Linear for each downsample,
3212 // but not the Linear -> sRGB when writing the next level. The result is that mip-chains
3213 // get progressively brighter as you go down. Forcing this to 'decode' gives predictable
3214 // (and only slightly incorrect) results. See crbug.com/655247 (~comment 28)
3215 if (!this->glCaps().srgbDecodeDisableAffectsMipmaps()) {
3216 srgbDecode = GR_GL_DECODE_EXT;
3217 }
3218 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SRGB_DECODE_EXT, srgbDecode));
brianosman33f6b3f2016-06-02 05:49:21 -07003219 }
3220
3221 // Either do manual mipmap generation or (if that fails), just rely on the driver:
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003222 if (!this->generateMipmap(texture, textureOrigin, allowSRGBInputs)) {
brianosman33f6b3f2016-06-02 05:49:21 -07003223 GL_CALL(GenerateMipmap(target));
3224 }
3225
Greg Daniel0fc4d2d2017-10-12 11:23:36 -04003226 texture->texturePriv().markMipMapsClean();
brianosman33f6b3f2016-06-02 05:49:21 -07003227 texture->texturePriv().setMaxMipMapLevel(SkMipMap::ComputeLevelCount(
3228 texture->width(), texture->height()));
Brian Osman7b8400d2016-11-08 17:08:54 -05003229 texture->texturePriv().setMipColorMode(colorMode);
brianosman33f6b3f2016-06-02 05:49:21 -07003230
3231 // We have potentially set lots of state on the texture. Easiest to dirty it all:
3232 texture->textureParamsModified();
3233}
3234
cdalton74b8d322016-04-11 14:47:28 -07003235void GrGLGpu::setTextureSwizzle(int unitIdx, GrGLenum target, const GrGLenum swizzle[]) {
3236 this->setTextureUnit(unitIdx);
3237 if (this->glStandard() == kGLES_GrGLStandard) {
3238 // ES3 added swizzle support but not GL_TEXTURE_SWIZZLE_RGBA.
3239 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_R, swizzle[0]));
3240 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_G, swizzle[1]));
3241 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_B, swizzle[2]));
3242 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_A, swizzle[3]));
3243 } else {
3244 GR_STATIC_ASSERT(sizeof(swizzle[0]) == sizeof(GrGLint));
3245 GL_CALL(TexParameteriv(target, GR_GL_TEXTURE_SWIZZLE_RGBA,
3246 reinterpret_cast<const GrGLint*>(swizzle)));
3247 }
3248}
3249
egdaniel080e6732014-12-22 07:35:52 -08003250void GrGLGpu::flushColorWrite(bool writeColor) {
3251 if (!writeColor) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +00003252 if (kNo_TriState != fHWWriteToColor) {
3253 GL_CALL(ColorMask(GR_GL_FALSE, GR_GL_FALSE,
3254 GR_GL_FALSE, GR_GL_FALSE));
3255 fHWWriteToColor = kNo_TriState;
bsalomon@google.comd302f142011-03-03 13:54:13 +00003256 }
bsalomon@google.com978c8c62012-05-21 14:45:49 +00003257 } else {
3258 if (kYes_TriState != fHWWriteToColor) {
3259 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
3260 fHWWriteToColor = kYes_TriState;
3261 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00003262 }
bsalomon3e791242014-12-17 13:43:13 -08003263}
bsalomon@google.comd302f142011-03-03 13:54:13 +00003264
bsalomon861e1032014-12-16 07:33:49 -08003265void GrGLGpu::setTextureUnit(int unit) {
bsalomon1c63bf62014-07-22 13:09:46 -07003266 SkASSERT(unit >= 0 && unit < fHWBoundTextureUniqueIDs.count());
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00003267 if (unit != fHWActiveTextureUnitIdx) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00003268 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com49209392012-06-05 15:13:46 +00003269 fHWActiveTextureUnitIdx = unit;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00003270 }
3271}
bsalomon@google.com316f99232011-01-13 21:28:12 +00003272
bsalomon861e1032014-12-16 07:33:49 -08003273void GrGLGpu::setScratchTextureUnit() {
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00003274 // Bind the last texture unit since it is the least likely to be used by GrGLProgram.
bsalomon1c63bf62014-07-22 13:09:46 -07003275 int lastUnitIdx = fHWBoundTextureUniqueIDs.count() - 1;
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00003276 if (lastUnitIdx != fHWActiveTextureUnitIdx) {
3277 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + lastUnitIdx));
3278 fHWActiveTextureUnitIdx = lastUnitIdx;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00003279 }
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00003280 // clear out the this field so that if a program does use this unit it will rebind the correct
3281 // texture.
Robert Phillips294870f2016-11-11 12:38:40 -05003282 fHWBoundTextureUniqueIDs[lastUnitIdx].makeInvalid();
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00003283}
3284
Brian Salomone5e7eb12016-10-14 16:18:33 -04003285// Determines whether glBlitFramebuffer could be used between src and dst by onCopySurface.
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003286static inline bool can_blit_framebuffer_for_copy_surface(
3287 const GrSurface* dst, GrSurfaceOrigin dstOrigin,
3288 const GrSurface* src, GrSurfaceOrigin srcOrigin,
3289 const SkIRect& srcRect,
3290 const SkIPoint& dstPoint,
Greg Daniel26dbe3b2018-05-03 10:35:42 -04003291 const GrGLCaps& caps) {
3292 int dstSampleCnt = 0;
3293 int srcSampleCnt = 0;
3294 if (const GrRenderTarget* rt = dst->asRenderTarget()) {
3295 dstSampleCnt = rt->numColorSamples();
bsalomon@google.comeb851172013-04-15 13:51:00 +00003296 }
Greg Daniel26dbe3b2018-05-03 10:35:42 -04003297 if (const GrRenderTarget* rt = src->asRenderTarget()) {
3298 srcSampleCnt = rt->numColorSamples();
3299 }
3300 SkASSERT((dstSampleCnt > 0) == SkToBool(dst->asRenderTarget()));
3301 SkASSERT((srcSampleCnt > 0) == SkToBool(src->asRenderTarget()));
3302
Brian Salomone5e7eb12016-10-14 16:18:33 -04003303 const GrGLTexture* dstTex = static_cast<const GrGLTexture*>(dst->asTexture());
Mike Klein1d746202018-01-25 17:32:51 -05003304 const GrGLTexture* srcTex = static_cast<const GrGLTexture*>(src->asTexture());
Greg Daniel26dbe3b2018-05-03 10:35:42 -04003305
3306 bool dstIsGLTexture2D = dstTex ? GR_GL_TEXTURE_2D == dstTex->target() : false;
3307 bool srcIsGLTexture2D = srcTex ? GR_GL_TEXTURE_2D == srcTex->target() : false;
3308
3309 return caps.canCopyAsBlit(dst->config(), dstSampleCnt, SkToBool(dstTex), dstIsGLTexture2D,
3310 dstOrigin, src->config(), srcSampleCnt, SkToBool(srcTex),
3311 srcIsGLTexture2D, srcOrigin, src->getBoundsRect(), srcRect, dstPoint);
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00003312}
bsalomon@google.comeb851172013-04-15 13:51:00 +00003313
Brian Osmana9c8a052018-01-19 10:31:56 -05003314static bool rt_has_msaa_render_buffer(const GrGLRenderTarget* rt, const GrGLCaps& glCaps) {
3315 // A RT has a separate MSAA renderbuffer if:
3316 // 1) It's multisampled
3317 // 2) We're using an extension with separate MSAA renderbuffers
3318 // 3) It's not FBO 0, which is special and always auto-resolves
Brian Salomonbdecacf2018-02-02 20:32:49 -05003319 return rt->numColorSamples() > 1 && glCaps.usesMSAARenderBuffers() && rt->renderFBOID() != 0;
Brian Osmana9c8a052018-01-19 10:31:56 -05003320}
3321
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003322static inline bool can_copy_texsubimage(const GrSurface* dst, GrSurfaceOrigin dstOrigin,
3323 const GrSurface* src, GrSurfaceOrigin srcOrigin,
Greg Daniel26dbe3b2018-05-03 10:35:42 -04003324 const GrGLCaps& caps) {
3325
bsalomon@google.comeb851172013-04-15 13:51:00 +00003326 const GrGLRenderTarget* dstRT = static_cast<const GrGLRenderTarget*>(dst->asRenderTarget());
bsalomon@google.coma2719852013-04-17 14:25:27 +00003327 const GrGLRenderTarget* srcRT = static_cast<const GrGLRenderTarget*>(src->asRenderTarget());
bsalomon7ea33f52015-11-22 14:51:00 -08003328 const GrGLTexture* dstTex = static_cast<const GrGLTexture*>(dst->asTexture());
bsalomon7ea33f52015-11-22 14:51:00 -08003329 const GrGLTexture* srcTex = static_cast<const GrGLTexture*>(src->asTexture());
cblume61214052016-01-26 09:10:48 -08003330
Greg Daniel26dbe3b2018-05-03 10:35:42 -04003331 bool dstHasMSAARenderBuffer = dstRT ? rt_has_msaa_render_buffer(dstRT, caps) : false;
3332 bool srcHasMSAARenderBuffer = srcRT ? rt_has_msaa_render_buffer(srcRT, caps) : false;
3333
3334 bool dstIsGLTexture2D = dstTex ? GR_GL_TEXTURE_2D == dstTex->target() : false;
3335 bool srcIsGLTexture2D = srcTex ? GR_GL_TEXTURE_2D == srcTex->target() : false;
3336
3337 return caps.canCopyTexSubImage(dst->config(), dstHasMSAARenderBuffer, SkToBool(dstTex),
3338 dstIsGLTexture2D, dstOrigin, src->config(),
3339 srcHasMSAARenderBuffer, SkToBool(srcTex), srcIsGLTexture2D,
3340 srcOrigin);
bsalomon@google.comeb851172013-04-15 13:51:00 +00003341}
3342
3343// If a temporary FBO was created, its non-zero ID is returned. The viewport that the copy rect is
3344// relative to is output.
Brian Salomon71d9d842016-11-03 13:42:00 -04003345void GrGLGpu::bindSurfaceFBOForPixelOps(GrSurface* surface, GrGLenum fboTarget, GrGLIRect* viewport,
3346 TempFBOTarget tempFBOTarget) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00003347 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(surface->asRenderTarget());
bsalomon083617b2016-02-12 12:10:14 -08003348 if (!rt) {
bsalomon49f085d2014-09-05 13:34:00 -07003349 SkASSERT(surface->asTexture());
Brian Salomon9bada542017-06-12 12:09:30 -04003350 GrGLTexture* texture = static_cast<GrGLTexture*>(surface->asTexture());
3351 GrGLuint texID = texture->textureID();
3352 GrGLenum target = texture->target();
egdanield803f272015-03-18 13:01:52 -07003353 GrGLuint* tempFBOID;
3354 tempFBOID = kSrc_TempFBOTarget == tempFBOTarget ? &fTempSrcFBOID : &fTempDstFBOID;
egdaniel0f5f9672015-02-03 11:10:51 -08003355
egdanield803f272015-03-18 13:01:52 -07003356 if (0 == *tempFBOID) {
3357 GR_GL_CALL(this->glInterface(), GenFramebuffers(1, tempFBOID));
egdaniel0f5f9672015-02-03 11:10:51 -08003358 }
3359
Adrienne Walker4ee88512018-05-17 11:37:14 -07003360 this->bindFramebuffer(fboTarget, *tempFBOID);
egdanield803f272015-03-18 13:01:52 -07003361 GR_GL_CALL(this->glInterface(), FramebufferTexture2D(fboTarget,
robertphillips754f4e92014-09-18 13:52:08 -07003362 GR_GL_COLOR_ATTACHMENT0,
bsalomon10528f12015-10-14 12:54:52 -07003363 target,
robertphillips754f4e92014-09-18 13:52:08 -07003364 texID,
3365 0));
Brian Salomon9bada542017-06-12 12:09:30 -04003366 texture->baseLevelWasBoundToFBO();
bsalomon@google.comeb851172013-04-15 13:51:00 +00003367 viewport->fLeft = 0;
3368 viewport->fBottom = 0;
3369 viewport->fWidth = surface->width();
3370 viewport->fHeight = surface->height();
3371 } else {
Adrienne Walker4ee88512018-05-17 11:37:14 -07003372 this->bindFramebuffer(fboTarget, rt->renderFBOID());
bsalomon@google.comeb851172013-04-15 13:51:00 +00003373 *viewport = rt->getViewport();
3374 }
egdaniel0f5f9672015-02-03 11:10:51 -08003375}
3376
Brian Salomon71d9d842016-11-03 13:42:00 -04003377void GrGLGpu::unbindTextureFBOForPixelOps(GrGLenum fboTarget, GrSurface* surface) {
3378 // bindSurfaceFBOForPixelOps temporarily binds textures that are not render targets to
bsalomon10528f12015-10-14 12:54:52 -07003379 if (!surface->asRenderTarget()) {
3380 SkASSERT(surface->asTexture());
3381 GrGLenum textureTarget = static_cast<GrGLTexture*>(surface->asTexture())->target();
3382 GR_GL_CALL(this->glInterface(), FramebufferTexture2D(fboTarget,
3383 GR_GL_COLOR_ATTACHMENT0,
3384 textureTarget,
3385 0,
3386 0));
3387 }
bsalomon@google.comeb851172013-04-15 13:51:00 +00003388}
3389
Adrienne Walker4ee88512018-05-17 11:37:14 -07003390void GrGLGpu::bindFramebuffer(GrGLenum target, GrGLuint fboid) {
3391 fStats.incRenderTargetBinds();
3392 GL_CALL(BindFramebuffer(target, fboid));
3393 if (target == GR_GL_FRAMEBUFFER || target == GR_GL_DRAW_FRAMEBUFFER) {
3394 fBoundDrawFramebuffer = fboid;
3395 }
3396
Adrienne Walker3ed33992018-05-15 11:44:34 -07003397 if (!this->caps()->workarounds().restore_scissor_on_fbo_change) {
3398 return;
3399 }
3400
3401 // The driver forgets the correct scissor when modifying the FBO binding.
Adrienne Walker3a69c742018-05-21 11:05:48 -07003402 if (!fHWScissorSettings.fRect.isInvalid()) {
3403 fHWScissorSettings.fRect.pushToGLScissor(this->glInterface());
3404 }
Adrienne Walker3ed33992018-05-15 11:44:34 -07003405
3406 // crbug.com/222018 - Also on QualComm, the flush here avoids flicker,
3407 // it's unclear how this bug works.
3408 GL_CALL(Flush());
3409}
3410
Adrienne Walker4ee88512018-05-17 11:37:14 -07003411void GrGLGpu::deleteFramebuffer(GrGLuint fboid) {
3412 if (fboid == fBoundDrawFramebuffer &&
3413 this->caps()->workarounds().unbind_attachments_on_bound_render_fbo_delete) {
3414 // This workaround only applies to deleting currently bound framebuffers
3415 // on Adreno 420. Because this is a somewhat rare case, instead of
3416 // tracking all the attachments of every framebuffer instead just always
3417 // unbind all attachments.
3418 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
3419 GR_GL_RENDERBUFFER, 0));
3420 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT,
3421 GR_GL_RENDERBUFFER, 0));
3422 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_DEPTH_ATTACHMENT,
3423 GR_GL_RENDERBUFFER, 0));
3424 }
3425
3426 GL_CALL(DeleteFramebuffers(1, &fboid));
3427}
3428
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003429bool GrGLGpu::onCopySurface(GrSurface* dst, GrSurfaceOrigin dstOrigin,
3430 GrSurface* src, GrSurfaceOrigin srcOrigin,
Greg Daniel55fa6472018-03-16 16:13:10 -04003431 const SkIRect& srcRect, const SkIPoint& dstPoint,
3432 bool canDiscardOutsideDstRect) {
bsalomon7f9b2e42016-01-12 13:29:26 -08003433 // None of our copy methods can handle a swizzle. TODO: Make copySurfaceAsDraw handle the
3434 // swizzle.
Brian Salomon1edc5b92016-11-29 13:43:46 -05003435 if (this->caps()->shaderCaps()->configOutputSwizzle(src->config()) !=
3436 this->caps()->shaderCaps()->configOutputSwizzle(dst->config())) {
bsalomon7f9b2e42016-01-12 13:29:26 -08003437 return false;
3438 }
bsalomon083617b2016-02-12 12:10:14 -08003439 // Don't prefer copying as a draw if the dst doesn't already have a FBO object.
Brian Salomon3d86a192018-02-27 16:46:11 -05003440 // This implicitly handles this->glCaps().useDrawInsteadOfAllRenderTargetWrites().
bsalomon083617b2016-02-12 12:10:14 -08003441 bool preferCopy = SkToBool(dst->asRenderTarget());
Greg Daniel26dbe3b2018-05-03 10:35:42 -04003442 if (preferCopy && this->glCaps().canCopyAsDraw(dst->config(), SkToBool(src->asTexture()))) {
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003443 if (this->copySurfaceAsDraw(dst, dstOrigin, src, srcOrigin, srcRect, dstPoint)) {
cdaltone2e71c22016-04-07 18:13:29 -07003444 return true;
3445 }
bsalomon5df6fee2015-05-18 06:26:15 -07003446 }
cblume61214052016-01-26 09:10:48 -08003447
Greg Daniel26dbe3b2018-05-03 10:35:42 -04003448 if (can_copy_texsubimage(dst, dstOrigin, src, srcOrigin, this->glCaps())) {
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003449 this->copySurfaceAsCopyTexSubImage(dst, dstOrigin, src, srcOrigin, srcRect, dstPoint);
bsalomon6df86402015-06-01 10:41:49 -07003450 return true;
3451 }
3452
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003453 if (can_blit_framebuffer_for_copy_surface(dst, dstOrigin, src, srcOrigin,
Greg Daniel26dbe3b2018-05-03 10:35:42 -04003454 srcRect, dstPoint, this->glCaps())) {
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003455 return this->copySurfaceAsBlitFramebuffer(dst, dstOrigin, src, srcOrigin,
3456 srcRect, dstPoint);
bsalomon6df86402015-06-01 10:41:49 -07003457 }
3458
Greg Daniel26dbe3b2018-05-03 10:35:42 -04003459 if (!preferCopy && this->glCaps().canCopyAsDraw(dst->config(), SkToBool(src->asTexture()))) {
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003460 if (this->copySurfaceAsDraw(dst, dstOrigin, src, srcOrigin, srcRect, dstPoint)) {
cdaltone2e71c22016-04-07 18:13:29 -07003461 return true;
3462 }
bsalomon083617b2016-02-12 12:10:14 -08003463 }
3464
bsalomon6df86402015-06-01 10:41:49 -07003465 return false;
3466}
3467
Brian Salomonbf7b6202016-11-11 16:08:03 -05003468bool GrGLGpu::createCopyProgram(GrTexture* srcTex) {
Brian Osman39c08ac2017-07-26 09:36:09 -04003469 TRACE_EVENT0("skia", TRACE_FUNC);
Ryan Macnak38a10ad2017-07-10 10:36:34 -07003470
Brian Salomonbf7b6202016-11-11 16:08:03 -05003471 int progIdx = TextureToCopyProgramIdx(srcTex);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003472 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
Brian Salomonbf7b6202016-11-11 16:08:03 -05003473 GrSLType samplerType = srcTex->texturePriv().samplerType();
cdaltone2e71c22016-04-07 18:13:29 -07003474
3475 if (!fCopyProgramArrayBuffer) {
3476 static const GrGLfloat vdata[] = {
3477 0, 0,
3478 0, 1,
3479 1, 0,
3480 1, 1
3481 };
3482 fCopyProgramArrayBuffer.reset(GrGLBuffer::Create(this, sizeof(vdata), kVertex_GrBufferType,
3483 kStatic_GrAccessPattern, vdata));
3484 }
3485 if (!fCopyProgramArrayBuffer) {
3486 return false;
3487 }
3488
3489 SkASSERT(!fCopyPrograms[progIdx].fProgram);
3490 GL_CALL_RET(fCopyPrograms[progIdx].fProgram, CreateProgram());
3491 if (!fCopyPrograms[progIdx].fProgram) {
3492 return false;
3493 }
3494
Brian Salomon1edc5b92016-11-29 13:43:46 -05003495 const char* version = shaderCaps->versionDeclString();
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003496 GrShaderVar aVertex("a_vertex", kHalf2_GrSLType, GrShaderVar::kIn_TypeModifier);
3497 GrShaderVar uTexCoordXform("u_texCoordXform", kHalf4_GrSLType,
Brian Salomon99938a82016-11-21 13:41:08 -05003498 GrShaderVar::kUniform_TypeModifier);
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003499 GrShaderVar uPosXform("u_posXform", kHalf4_GrSLType, GrShaderVar::kUniform_TypeModifier);
Brian Salomon99938a82016-11-21 13:41:08 -05003500 GrShaderVar uTexture("u_texture", samplerType, GrShaderVar::kUniform_TypeModifier);
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003501 GrShaderVar vTexCoord("v_texCoord", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier);
3502 GrShaderVar oFragColor("o_FragColor", kHalf4_GrSLType, GrShaderVar::kOut_TypeModifier);
cdaltone2e71c22016-04-07 18:13:29 -07003503
3504 SkString vshaderTxt(version);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003505 if (shaderCaps->noperspectiveInterpolationSupport()) {
3506 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
cdaltone2e71c22016-04-07 18:13:29 -07003507 vshaderTxt.appendf("#extension %s : require\n", extension);
3508 }
3509 vTexCoord.addModifier("noperspective");
3510 }
3511
Brian Salomon1edc5b92016-11-29 13:43:46 -05003512 aVertex.appendDecl(shaderCaps, &vshaderTxt);
cdaltone2e71c22016-04-07 18:13:29 -07003513 vshaderTxt.append(";");
Brian Salomon1edc5b92016-11-29 13:43:46 -05003514 uTexCoordXform.appendDecl(shaderCaps, &vshaderTxt);
cdaltone2e71c22016-04-07 18:13:29 -07003515 vshaderTxt.append(";");
Brian Salomon1edc5b92016-11-29 13:43:46 -05003516 uPosXform.appendDecl(shaderCaps, &vshaderTxt);
cdaltone2e71c22016-04-07 18:13:29 -07003517 vshaderTxt.append(";");
Brian Salomon1edc5b92016-11-29 13:43:46 -05003518 vTexCoord.appendDecl(shaderCaps, &vshaderTxt);
cdaltone2e71c22016-04-07 18:13:29 -07003519 vshaderTxt.append(";");
3520
3521 vshaderTxt.append(
3522 "// Copy Program VS\n"
3523 "void main() {"
3524 " v_texCoord = a_vertex.xy * u_texCoordXform.xy + u_texCoordXform.zw;"
Ethan Nicholasbed683a2017-09-26 14:23:59 -04003525 " sk_Position.xy = a_vertex * u_posXform.xy + u_posXform.zw;"
3526 " sk_Position.zw = half2(0, 1);"
cdaltone2e71c22016-04-07 18:13:29 -07003527 "}"
3528 );
3529
3530 SkString fshaderTxt(version);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003531 if (shaderCaps->noperspectiveInterpolationSupport()) {
3532 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
cdaltone2e71c22016-04-07 18:13:29 -07003533 fshaderTxt.appendf("#extension %s : require\n", extension);
3534 }
3535 }
Brian Salomonf31ae492016-11-18 15:35:33 -05003536 vTexCoord.setTypeModifier(GrShaderVar::kIn_TypeModifier);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003537 vTexCoord.appendDecl(shaderCaps, &fshaderTxt);
cdaltone2e71c22016-04-07 18:13:29 -07003538 fshaderTxt.append(";");
Brian Salomon1edc5b92016-11-29 13:43:46 -05003539 uTexture.appendDecl(shaderCaps, &fshaderTxt);
cdaltone2e71c22016-04-07 18:13:29 -07003540 fshaderTxt.append(";");
cdaltone2e71c22016-04-07 18:13:29 -07003541 fshaderTxt.appendf(
3542 "// Copy Program FS\n"
3543 "void main() {"
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003544 " sk_FragColor = texture(u_texture, v_texCoord);"
3545 "}"
cdaltone2e71c22016-04-07 18:13:29 -07003546 );
3547
3548 const char* str;
3549 GrGLint length;
3550
3551 str = vshaderTxt.c_str();
3552 length = SkToInt(vshaderTxt.size());
Ethan Nicholas941e7e22016-12-12 15:33:30 -05003553 SkSL::Program::Settings settings;
3554 settings.fCaps = shaderCaps;
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003555 SkSL::String glsl;
3556 std::unique_ptr<SkSL::Program> program = GrSkSLtoGLSL(*fGLContext, GR_GL_VERTEX_SHADER,
3557 &str, &length, 1, settings, &glsl);
cdaltone2e71c22016-04-07 18:13:29 -07003558 GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fCopyPrograms[progIdx].fProgram,
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003559 GR_GL_VERTEX_SHADER, glsl.c_str(), glsl.size(),
3560 &fStats, settings);
3561 SkASSERT(program->fInputs.isEmpty());
cdaltone2e71c22016-04-07 18:13:29 -07003562
3563 str = fshaderTxt.c_str();
3564 length = SkToInt(fshaderTxt.size());
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003565 program = GrSkSLtoGLSL(*fGLContext, GR_GL_FRAGMENT_SHADER, &str, &length, 1, settings, &glsl);
cdaltone2e71c22016-04-07 18:13:29 -07003566 GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fCopyPrograms[progIdx].fProgram,
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003567 GR_GL_FRAGMENT_SHADER, glsl.c_str(), glsl.size(),
3568 &fStats, settings);
3569 SkASSERT(program->fInputs.isEmpty());
cdaltone2e71c22016-04-07 18:13:29 -07003570
3571 GL_CALL(LinkProgram(fCopyPrograms[progIdx].fProgram));
3572
3573 GL_CALL_RET(fCopyPrograms[progIdx].fTextureUniform,
3574 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_texture"));
3575 GL_CALL_RET(fCopyPrograms[progIdx].fPosXformUniform,
3576 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_posXform"));
3577 GL_CALL_RET(fCopyPrograms[progIdx].fTexCoordXformUniform,
3578 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_texCoordXform"));
3579
3580 GL_CALL(BindAttribLocation(fCopyPrograms[progIdx].fProgram, 0, "a_vertex"));
3581
3582 GL_CALL(DeleteShader(vshader));
3583 GL_CALL(DeleteShader(fshader));
3584
3585 return true;
bsalomon6df86402015-06-01 10:41:49 -07003586}
3587
brianosman33f6b3f2016-06-02 05:49:21 -07003588bool GrGLGpu::createMipmapProgram(int progIdx) {
3589 const bool oddWidth = SkToBool(progIdx & 0x2);
3590 const bool oddHeight = SkToBool(progIdx & 0x1);
3591 const int numTaps = (oddWidth ? 2 : 1) * (oddHeight ? 2 : 1);
3592
Brian Salomon1edc5b92016-11-29 13:43:46 -05003593 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
brianosman33f6b3f2016-06-02 05:49:21 -07003594
3595 SkASSERT(!fMipmapPrograms[progIdx].fProgram);
3596 GL_CALL_RET(fMipmapPrograms[progIdx].fProgram, CreateProgram());
3597 if (!fMipmapPrograms[progIdx].fProgram) {
3598 return false;
3599 }
3600
Brian Salomon1edc5b92016-11-29 13:43:46 -05003601 const char* version = shaderCaps->versionDeclString();
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003602 GrShaderVar aVertex("a_vertex", kHalf2_GrSLType, GrShaderVar::kIn_TypeModifier);
3603 GrShaderVar uTexCoordXform("u_texCoordXform", kHalf4_GrSLType,
Brian Salomon99938a82016-11-21 13:41:08 -05003604 GrShaderVar::kUniform_TypeModifier);
3605 GrShaderVar uTexture("u_texture", kTexture2DSampler_GrSLType,
3606 GrShaderVar::kUniform_TypeModifier);
brianosman33f6b3f2016-06-02 05:49:21 -07003607 // We need 1, 2, or 4 texture coordinates (depending on parity of each dimension):
Brian Salomon99938a82016-11-21 13:41:08 -05003608 GrShaderVar vTexCoords[] = {
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003609 GrShaderVar("v_texCoord0", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier),
3610 GrShaderVar("v_texCoord1", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier),
3611 GrShaderVar("v_texCoord2", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier),
3612 GrShaderVar("v_texCoord3", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier),
brianosman33f6b3f2016-06-02 05:49:21 -07003613 };
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003614 GrShaderVar oFragColor("o_FragColor", kHalf4_GrSLType,GrShaderVar::kOut_TypeModifier);
brianosman33f6b3f2016-06-02 05:49:21 -07003615
3616 SkString vshaderTxt(version);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003617 if (shaderCaps->noperspectiveInterpolationSupport()) {
3618 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
brianosman33f6b3f2016-06-02 05:49:21 -07003619 vshaderTxt.appendf("#extension %s : require\n", extension);
3620 }
3621 vTexCoords[0].addModifier("noperspective");
3622 vTexCoords[1].addModifier("noperspective");
3623 vTexCoords[2].addModifier("noperspective");
3624 vTexCoords[3].addModifier("noperspective");
3625 }
3626
Brian Salomon1edc5b92016-11-29 13:43:46 -05003627 aVertex.appendDecl(shaderCaps, &vshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003628 vshaderTxt.append(";");
Brian Salomon1edc5b92016-11-29 13:43:46 -05003629 uTexCoordXform.appendDecl(shaderCaps, &vshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003630 vshaderTxt.append(";");
3631 for (int i = 0; i < numTaps; ++i) {
Brian Salomon1edc5b92016-11-29 13:43:46 -05003632 vTexCoords[i].appendDecl(shaderCaps, &vshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003633 vshaderTxt.append(";");
3634 }
3635
3636 vshaderTxt.append(
3637 "// Mipmap Program VS\n"
3638 "void main() {"
Ethan Nicholasbed683a2017-09-26 14:23:59 -04003639 " sk_Position.xy = a_vertex * half2(2, 2) - half2(1, 1);"
3640 " sk_Position.zw = half2(0, 1);"
brianosman33f6b3f2016-06-02 05:49:21 -07003641 );
3642
3643 // Insert texture coordinate computation:
3644 if (oddWidth && oddHeight) {
3645 vshaderTxt.append(
3646 " v_texCoord0 = a_vertex.xy * u_texCoordXform.yw;"
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003647 " v_texCoord1 = a_vertex.xy * u_texCoordXform.yw + half2(u_texCoordXform.x, 0);"
3648 " v_texCoord2 = a_vertex.xy * u_texCoordXform.yw + half2(0, u_texCoordXform.z);"
brianosman33f6b3f2016-06-02 05:49:21 -07003649 " v_texCoord3 = a_vertex.xy * u_texCoordXform.yw + u_texCoordXform.xz;"
3650 );
3651 } else if (oddWidth) {
3652 vshaderTxt.append(
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003653 " v_texCoord0 = a_vertex.xy * half2(u_texCoordXform.y, 1);"
3654 " v_texCoord1 = a_vertex.xy * half2(u_texCoordXform.y, 1) + half2(u_texCoordXform.x, 0);"
brianosman33f6b3f2016-06-02 05:49:21 -07003655 );
3656 } else if (oddHeight) {
3657 vshaderTxt.append(
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003658 " v_texCoord0 = a_vertex.xy * half2(1, u_texCoordXform.w);"
3659 " v_texCoord1 = a_vertex.xy * half2(1, u_texCoordXform.w) + half2(0, u_texCoordXform.z);"
brianosman33f6b3f2016-06-02 05:49:21 -07003660 );
3661 } else {
3662 vshaderTxt.append(
3663 " v_texCoord0 = a_vertex.xy;"
3664 );
3665 }
3666
3667 vshaderTxt.append("}");
3668
3669 SkString fshaderTxt(version);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003670 if (shaderCaps->noperspectiveInterpolationSupport()) {
3671 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
brianosman33f6b3f2016-06-02 05:49:21 -07003672 fshaderTxt.appendf("#extension %s : require\n", extension);
3673 }
3674 }
brianosman33f6b3f2016-06-02 05:49:21 -07003675 for (int i = 0; i < numTaps; ++i) {
Brian Salomonf31ae492016-11-18 15:35:33 -05003676 vTexCoords[i].setTypeModifier(GrShaderVar::kIn_TypeModifier);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003677 vTexCoords[i].appendDecl(shaderCaps, &fshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003678 fshaderTxt.append(";");
3679 }
Brian Salomon1edc5b92016-11-29 13:43:46 -05003680 uTexture.appendDecl(shaderCaps, &fshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003681 fshaderTxt.append(";");
brianosman33f6b3f2016-06-02 05:49:21 -07003682 fshaderTxt.append(
3683 "// Mipmap Program FS\n"
3684 "void main() {"
3685 );
3686
3687 if (oddWidth && oddHeight) {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003688 fshaderTxt.append(
3689 " sk_FragColor = (texture(u_texture, v_texCoord0) + "
3690 " texture(u_texture, v_texCoord1) + "
3691 " texture(u_texture, v_texCoord2) + "
3692 " texture(u_texture, v_texCoord3)) * 0.25;"
brianosman33f6b3f2016-06-02 05:49:21 -07003693 );
3694 } else if (oddWidth || oddHeight) {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003695 fshaderTxt.append(
3696 " sk_FragColor = (texture(u_texture, v_texCoord0) + "
3697 " texture(u_texture, v_texCoord1)) * 0.5;"
brianosman33f6b3f2016-06-02 05:49:21 -07003698 );
3699 } else {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003700 fshaderTxt.append(
3701 " sk_FragColor = texture(u_texture, v_texCoord0);"
brianosman33f6b3f2016-06-02 05:49:21 -07003702 );
3703 }
3704
3705 fshaderTxt.append("}");
3706
3707 const char* str;
3708 GrGLint length;
3709
3710 str = vshaderTxt.c_str();
3711 length = SkToInt(vshaderTxt.size());
Ethan Nicholas941e7e22016-12-12 15:33:30 -05003712 SkSL::Program::Settings settings;
3713 settings.fCaps = shaderCaps;
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003714 SkSL::String glsl;
3715 std::unique_ptr<SkSL::Program> program = GrSkSLtoGLSL(*fGLContext, GR_GL_VERTEX_SHADER,
3716 &str, &length, 1, settings, &glsl);
brianosman33f6b3f2016-06-02 05:49:21 -07003717 GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fMipmapPrograms[progIdx].fProgram,
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003718 GR_GL_VERTEX_SHADER, glsl.c_str(), glsl.size(),
3719 &fStats, settings);
3720 SkASSERT(program->fInputs.isEmpty());
brianosman33f6b3f2016-06-02 05:49:21 -07003721
3722 str = fshaderTxt.c_str();
3723 length = SkToInt(fshaderTxt.size());
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003724 program = GrSkSLtoGLSL(*fGLContext, GR_GL_FRAGMENT_SHADER, &str, &length, 1, settings, &glsl);
brianosman33f6b3f2016-06-02 05:49:21 -07003725 GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fMipmapPrograms[progIdx].fProgram,
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003726 GR_GL_FRAGMENT_SHADER, glsl.c_str(), glsl.size(),
3727 &fStats, settings);
3728 SkASSERT(program->fInputs.isEmpty());
brianosman33f6b3f2016-06-02 05:49:21 -07003729
3730 GL_CALL(LinkProgram(fMipmapPrograms[progIdx].fProgram));
3731
3732 GL_CALL_RET(fMipmapPrograms[progIdx].fTextureUniform,
3733 GetUniformLocation(fMipmapPrograms[progIdx].fProgram, "u_texture"));
3734 GL_CALL_RET(fMipmapPrograms[progIdx].fTexCoordXformUniform,
3735 GetUniformLocation(fMipmapPrograms[progIdx].fProgram, "u_texCoordXform"));
3736
3737 GL_CALL(BindAttribLocation(fMipmapPrograms[progIdx].fProgram, 0, "a_vertex"));
3738
3739 GL_CALL(DeleteShader(vshader));
3740 GL_CALL(DeleteShader(fshader));
3741
3742 return true;
3743}
3744
Mike Klein31550db2017-06-06 23:29:53 +00003745bool GrGLGpu::createStencilClipClearProgram() {
Brian Osman39c08ac2017-07-26 09:36:09 -04003746 TRACE_EVENT0("skia", TRACE_FUNC);
Ryan Macnak38a10ad2017-07-10 10:36:34 -07003747
Mike Klein31550db2017-06-06 23:29:53 +00003748 if (!fStencilClipClearArrayBuffer) {
3749 static const GrGLfloat vdata[] = {-1, -1, 1, -1, -1, 1, 1, 1};
3750 fStencilClipClearArrayBuffer.reset(GrGLBuffer::Create(
3751 this, sizeof(vdata), kVertex_GrBufferType, kStatic_GrAccessPattern, vdata));
3752 if (!fStencilClipClearArrayBuffer) {
cdaltone2e71c22016-04-07 18:13:29 -07003753 return false;
3754 }
3755 }
3756
Mike Klein31550db2017-06-06 23:29:53 +00003757 SkASSERT(!fStencilClipClearProgram);
3758 GL_CALL_RET(fStencilClipClearProgram, CreateProgram());
3759 if (!fStencilClipClearProgram) {
cdaltone2e71c22016-04-07 18:13:29 -07003760 return false;
3761 }
3762
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003763 GrShaderVar aVertex("a_vertex", kHalf2_GrSLType, GrShaderVar::kIn_TypeModifier);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003764 const char* version = this->caps()->shaderCaps()->versionDeclString();
bsalomon6dea83f2015-12-03 12:58:06 -08003765
bsalomon6dea83f2015-12-03 12:58:06 -08003766 SkString vshaderTxt(version);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003767 aVertex.appendDecl(this->caps()->shaderCaps(), &vshaderTxt);
bsalomon6dea83f2015-12-03 12:58:06 -08003768 vshaderTxt.append(";");
bsalomon6dea83f2015-12-03 12:58:06 -08003769 vshaderTxt.append(
Mike Klein31550db2017-06-06 23:29:53 +00003770 "// Stencil Clip Clear Program VS\n"
3771 "void main() {"
Ethan Nicholasbed683a2017-09-26 14:23:59 -04003772 " sk_Position = float4(a_vertex.x, a_vertex.y, 0, 1);"
Mike Klein31550db2017-06-06 23:29:53 +00003773 "}");
bsalomon6dea83f2015-12-03 12:58:06 -08003774
3775 SkString fshaderTxt(version);
bsalomon6dea83f2015-12-03 12:58:06 -08003776 fshaderTxt.appendf(
Mike Klein31550db2017-06-06 23:29:53 +00003777 "// Stencil Clip Clear Program FS\n"
3778 "void main() {"
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003779 " sk_FragColor = half4(0);"
Mike Klein31550db2017-06-06 23:29:53 +00003780 "}");
bsalomon6dea83f2015-12-03 12:58:06 -08003781
bsalomon6dea83f2015-12-03 12:58:06 -08003782 const char* str;
3783 GrGLint length;
3784
3785 str = vshaderTxt.c_str();
3786 length = SkToInt(vshaderTxt.size());
Ethan Nicholas941e7e22016-12-12 15:33:30 -05003787 SkSL::Program::Settings settings;
3788 settings.fCaps = this->caps()->shaderCaps();
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003789 SkSL::String glsl;
3790 std::unique_ptr<SkSL::Program> program = GrSkSLtoGLSL(*fGLContext, GR_GL_VERTEX_SHADER,
3791 &str, &length, 1, settings, &glsl);
3792 GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fStencilClipClearProgram,
3793 GR_GL_VERTEX_SHADER, glsl.c_str(), glsl.size(),
3794 &fStats, settings);
3795 SkASSERT(program->fInputs.isEmpty());
bsalomon6dea83f2015-12-03 12:58:06 -08003796
3797 str = fshaderTxt.c_str();
3798 length = SkToInt(fshaderTxt.size());
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003799 program = GrSkSLtoGLSL(*fGLContext, GR_GL_FRAGMENT_SHADER, &str, &length, 1, settings, &glsl);
3800 GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fStencilClipClearProgram,
3801 GR_GL_FRAGMENT_SHADER, glsl.c_str(), glsl.size(),
3802 &fStats, settings);
3803 SkASSERT(program->fInputs.isEmpty());
bsalomon6dea83f2015-12-03 12:58:06 -08003804
Mike Klein31550db2017-06-06 23:29:53 +00003805 GL_CALL(LinkProgram(fStencilClipClearProgram));
bsalomon6dea83f2015-12-03 12:58:06 -08003806
Mike Klein31550db2017-06-06 23:29:53 +00003807 GL_CALL(BindAttribLocation(fStencilClipClearProgram, 0, "a_vertex"));
bsalomon6dea83f2015-12-03 12:58:06 -08003808
3809 GL_CALL(DeleteShader(vshader));
3810 GL_CALL(DeleteShader(fshader));
cdaltone2e71c22016-04-07 18:13:29 -07003811
3812 return true;
bsalomon6dea83f2015-12-03 12:58:06 -08003813}
3814
Mike Klein31550db2017-06-06 23:29:53 +00003815void GrGLGpu::clearStencilClipAsDraw(const GrFixedClip& clip, bool insideStencilMask,
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003816 GrRenderTarget* rt, GrSurfaceOrigin origin) {
bsalomon7f9b2e42016-01-12 13:29:26 -08003817 // TODO: This should swizzle the output to match dst's config, though it is a debugging
3818 // visualization.
3819
bsalomon6dea83f2015-12-03 12:58:06 -08003820 this->handleDirtyContext();
Mike Klein31550db2017-06-06 23:29:53 +00003821 if (!fStencilClipClearProgram) {
3822 if (!this->createStencilClipClearProgram()) {
3823 SkDebugf("Failed to create stencil clip clear program.\n");
cdaltone2e71c22016-04-07 18:13:29 -07003824 return;
3825 }
bsalomon6dea83f2015-12-03 12:58:06 -08003826 }
3827
Mike Klein8b06ed72017-06-06 21:47:32 +00003828 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(rt->asRenderTarget());
Brian Salomon1fabd512018-02-09 09:54:25 -05003829 this->flushRenderTarget(glRT);
Mike Klein8b06ed72017-06-06 21:47:32 +00003830
Mike Klein31550db2017-06-06 23:29:53 +00003831 GL_CALL(UseProgram(fStencilClipClearProgram));
3832 fHWProgramID = fStencilClipClearProgram;
bsalomon6dea83f2015-12-03 12:58:06 -08003833
cdaltone2e71c22016-04-07 18:13:29 -07003834 fHWVertexArrayState.setVertexArrayID(this, 0);
bsalomon6dea83f2015-12-03 12:58:06 -08003835
cdaltone2e71c22016-04-07 18:13:29 -07003836 GrGLAttribArrayState* attribs = fHWVertexArrayState.bindInternalVertexArray(this);
Chris Dalton8e45b4f2017-05-05 14:00:56 -04003837 attribs->enableVertexArrays(this, 1);
Ethan Nicholasfa7ee242017-09-25 09:52:04 -04003838 attribs->set(this, 0, fStencilClipClearArrayBuffer.get(), kHalf2_GrVertexAttribType,
Hal Canary144caf52016-11-07 17:57:18 -05003839 2 * sizeof(GrGLfloat), 0);
bsalomon6dea83f2015-12-03 12:58:06 -08003840
bsalomon6dea83f2015-12-03 12:58:06 -08003841 GrXferProcessor::BlendInfo blendInfo;
3842 blendInfo.reset();
bsalomon7f9b2e42016-01-12 13:29:26 -08003843 this->flushBlend(blendInfo, GrSwizzle::RGBA());
Mike Klein31550db2017-06-06 23:29:53 +00003844 this->flushColorWrite(false);
cdaltonaf8bc7d2016-02-05 09:35:20 -08003845 this->flushHWAAState(glRT, false, false);
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003846 this->flushScissor(clip.scissorState(), glRT->getViewport(), origin);
3847 this->flushWindowRectangles(clip.windowRectsState(), glRT, origin);
Mike Klein31550db2017-06-06 23:29:53 +00003848 GrStencilAttachment* sb = rt->renderTargetPriv().getStencilAttachment();
3849 // This should only be called internally when we know we have a stencil buffer.
3850 SkASSERT(sb);
3851 GrStencilSettings settings = GrStencilSettings(
3852 *GrStencilSettings::SetClipBitSettings(insideStencilMask), false, sb->bits());
3853 this->flushStencil(settings);
3854 GL_CALL(DrawArrays(GR_GL_TRIANGLE_STRIP, 0, 4));
bsalomon6dea83f2015-12-03 12:58:06 -08003855}
3856
Brian Salomon43f8bf02017-10-18 08:33:29 -04003857bool GrGLGpu::createClearColorProgram() {
3858 TRACE_EVENT0("skia", TRACE_FUNC);
3859
3860 if (!fClearProgramArrayBuffer) {
3861 static const GrGLfloat vdata[] = {-1, -1, 1, -1, -1, 1, 1, 1};
3862 fClearProgramArrayBuffer.reset(GrGLBuffer::Create(this, sizeof(vdata), kVertex_GrBufferType,
3863 kStatic_GrAccessPattern, vdata));
3864 if (!fClearProgramArrayBuffer) {
3865 return false;
3866 }
3867 }
3868
3869 SkASSERT(!fClearColorProgram.fProgram);
3870 GL_CALL_RET(fClearColorProgram.fProgram, CreateProgram());
3871 if (!fClearColorProgram.fProgram) {
3872 return false;
3873 }
3874
3875 GrShaderVar aVertex("a_vertex", kHalf2_GrSLType, GrShaderVar::kIn_TypeModifier);
3876 const char* version = this->caps()->shaderCaps()->versionDeclString();
3877
3878 SkString vshaderTxt(version);
3879 aVertex.appendDecl(this->caps()->shaderCaps(), &vshaderTxt);
3880 vshaderTxt.append(";");
3881 vshaderTxt.append(R"(
3882 // Clear Color Program VS
3883 void main() {
3884 sk_Position = float4(a_vertex.x, a_vertex.y, 0, 1);
3885 })");
3886
3887 GrShaderVar uColor("u_color", kHalf4_GrSLType, GrShaderVar::kUniform_TypeModifier);
3888 SkString fshaderTxt(version);
3889 uColor.appendDecl(this->caps()->shaderCaps(), &fshaderTxt);
3890 fshaderTxt.append(";");
3891 fshaderTxt.appendf(R"(
3892 // Clear Color Program FS
3893 void main() {
3894 sk_FragColor = u_color;
3895 })");
3896
3897 const char* str;
3898 GrGLint length;
3899
3900 str = vshaderTxt.c_str();
3901 length = SkToInt(vshaderTxt.size());
3902 SkSL::Program::Settings settings;
3903 settings.fCaps = this->caps()->shaderCaps();
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003904 SkSL::String glsl;
3905 GrSkSLtoGLSL(*fGLContext, GR_GL_VERTEX_SHADER, &str, &length, 1, settings, &glsl);
Brian Salomon43f8bf02017-10-18 08:33:29 -04003906 GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fClearColorProgram.fProgram,
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003907 GR_GL_VERTEX_SHADER, glsl.c_str(), glsl.size(),
3908 &fStats, settings);
Brian Salomon43f8bf02017-10-18 08:33:29 -04003909
3910 str = fshaderTxt.c_str();
3911 length = SkToInt(fshaderTxt.size());
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003912 GrSkSLtoGLSL(*fGLContext, GR_GL_FRAGMENT_SHADER, &str, &length, 1, settings, &glsl);
Brian Salomon43f8bf02017-10-18 08:33:29 -04003913 GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fClearColorProgram.fProgram,
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003914 GR_GL_FRAGMENT_SHADER, glsl.c_str(), glsl.size(),
3915 &fStats, settings);
Brian Salomon43f8bf02017-10-18 08:33:29 -04003916
3917 GL_CALL(LinkProgram(fClearColorProgram.fProgram));
3918
3919 GL_CALL(BindAttribLocation(fClearColorProgram.fProgram, 0, "a_vertex"));
3920
3921 GL_CALL_RET(fClearColorProgram.fColorUniform,
3922 GetUniformLocation(fClearColorProgram.fProgram, "u_color"));
3923
3924 GL_CALL(DeleteShader(vshader));
3925 GL_CALL(DeleteShader(fshader));
3926
3927 return true;
3928}
3929
3930void GrGLGpu::clearColorAsDraw(const GrFixedClip& clip, GrGLfloat r, GrGLfloat g, GrGLfloat b,
3931 GrGLfloat a, GrRenderTarget* dst, GrSurfaceOrigin origin) {
3932 if (!fClearColorProgram.fProgram) {
3933 if (!this->createClearColorProgram()) {
3934 SkDebugf("Failed to create clear color program.\n");
3935 return;
3936 }
3937 }
3938
3939 GrGLIRect dstVP;
3940 this->bindSurfaceFBOForPixelOps(dst, GR_GL_FRAMEBUFFER, &dstVP, kDst_TempFBOTarget);
3941 this->flushViewport(dstVP);
3942 fHWBoundRenderTargetUniqueID.makeInvalid();
3943
3944 GL_CALL(UseProgram(fClearColorProgram.fProgram));
3945 fHWProgramID = fClearColorProgram.fProgram;
3946
3947 fHWVertexArrayState.setVertexArrayID(this, 0);
3948
3949 GrGLAttribArrayState* attribs = fHWVertexArrayState.bindInternalVertexArray(this);
3950 attribs->enableVertexArrays(this, 1);
3951 attribs->set(this, 0, fClearProgramArrayBuffer.get(), kHalf2_GrVertexAttribType,
3952 2 * sizeof(GrGLfloat), 0);
3953
3954 GrGLRenderTarget* glrt = static_cast<GrGLRenderTarget*>(dst);
3955 this->flushScissor(clip.scissorState(), glrt->getViewport(), origin);
3956 this->flushWindowRectangles(clip.windowRectsState(), glrt, origin);
3957
3958 GL_CALL(Uniform4f(fClearColorProgram.fColorUniform, r, g, b, a));
3959
3960 GrXferProcessor::BlendInfo blendInfo;
3961 blendInfo.reset();
3962 this->flushBlend(blendInfo, GrSwizzle::RGBA());
3963 this->flushColorWrite(true);
3964 this->flushHWAAState(nullptr, false, false);
3965 this->disableStencil();
3966 if (this->glCaps().srgbWriteControl()) {
3967 this->flushFramebufferSRGB(true);
3968 }
3969
3970 GL_CALL(DrawArrays(GR_GL_TRIANGLE_STRIP, 0, 4));
3971 this->unbindTextureFBOForPixelOps(GR_GL_FRAMEBUFFER, dst);
Brian Salomon1fabd512018-02-09 09:54:25 -05003972 this->didWriteToSurface(dst, origin, clip.scissorEnabled() ? &clip.scissorRect() : nullptr);
Brian Salomon43f8bf02017-10-18 08:33:29 -04003973}
bsalomon6dea83f2015-12-03 12:58:06 -08003974
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003975bool GrGLGpu::copySurfaceAsDraw(GrSurface* dst, GrSurfaceOrigin dstOrigin,
3976 GrSurface* src, GrSurfaceOrigin srcOrigin,
bsalomon6df86402015-06-01 10:41:49 -07003977 const SkIRect& srcRect,
3978 const SkIPoint& dstPoint) {
cdaltone2e71c22016-04-07 18:13:29 -07003979 GrGLTexture* srcTex = static_cast<GrGLTexture*>(src->asTexture());
Brian Salomonbf7b6202016-11-11 16:08:03 -05003980 int progIdx = TextureToCopyProgramIdx(srcTex);
cdaltone2e71c22016-04-07 18:13:29 -07003981
Greg Daniel26dbe3b2018-05-03 10:35:42 -04003982 if (!this->glCaps().canConfigBeFBOColorAttachment(dst->config())) {
3983 return false;
3984 }
3985
cdaltone2e71c22016-04-07 18:13:29 -07003986 if (!fCopyPrograms[progIdx].fProgram) {
Brian Salomonbf7b6202016-11-11 16:08:03 -05003987 if (!this->createCopyProgram(srcTex)) {
cdaltone2e71c22016-04-07 18:13:29 -07003988 SkDebugf("Failed to create copy program.\n");
3989 return false;
3990 }
3991 }
3992
bsalomon6df86402015-06-01 10:41:49 -07003993 int w = srcRect.width();
3994 int h = srcRect.height();
3995
Brian Salomon2bbdcc42017-09-07 12:36:34 -04003996 this->bindTexture(0, GrSamplerState::ClampNearest(), true, srcTex, srcOrigin);
bsalomon6df86402015-06-01 10:41:49 -07003997
bsalomon083617b2016-02-12 12:10:14 -08003998 GrGLIRect dstVP;
Brian Salomon71d9d842016-11-03 13:42:00 -04003999 this->bindSurfaceFBOForPixelOps(dst, GR_GL_FRAMEBUFFER, &dstVP, kDst_TempFBOTarget);
bsalomon083617b2016-02-12 12:10:14 -08004000 this->flushViewport(dstVP);
Robert Phillips294870f2016-11-11 12:38:40 -05004001 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon083617b2016-02-12 12:10:14 -08004002
bsalomon6df86402015-06-01 10:41:49 -07004003 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY, w, h);
bsalomon6df86402015-06-01 10:41:49 -07004004
bsalomon7ea33f52015-11-22 14:51:00 -08004005 GL_CALL(UseProgram(fCopyPrograms[progIdx].fProgram));
4006 fHWProgramID = fCopyPrograms[progIdx].fProgram;
bsalomon6df86402015-06-01 10:41:49 -07004007
cdaltone2e71c22016-04-07 18:13:29 -07004008 fHWVertexArrayState.setVertexArrayID(this, 0);
bsalomon6df86402015-06-01 10:41:49 -07004009
cdaltone2e71c22016-04-07 18:13:29 -07004010 GrGLAttribArrayState* attribs = fHWVertexArrayState.bindInternalVertexArray(this);
Chris Dalton8e45b4f2017-05-05 14:00:56 -04004011 attribs->enableVertexArrays(this, 1);
Ethan Nicholasfa7ee242017-09-25 09:52:04 -04004012 attribs->set(this, 0, fCopyProgramArrayBuffer.get(), kHalf2_GrVertexAttribType,
Hal Canary144caf52016-11-07 17:57:18 -05004013 2 * sizeof(GrGLfloat), 0);
bsalomon6df86402015-06-01 10:41:49 -07004014
4015 // dst rect edges in NDC (-1 to 1)
4016 int dw = dst->width();
4017 int dh = dst->height();
4018 GrGLfloat dx0 = 2.f * dstPoint.fX / dw - 1.f;
4019 GrGLfloat dx1 = 2.f * (dstPoint.fX + w) / dw - 1.f;
4020 GrGLfloat dy0 = 2.f * dstPoint.fY / dh - 1.f;
4021 GrGLfloat dy1 = 2.f * (dstPoint.fY + h) / dh - 1.f;
Robert Phillipsb0e93a22017-08-29 08:26:54 -04004022 if (kBottomLeft_GrSurfaceOrigin == dstOrigin) {
bsalomon6df86402015-06-01 10:41:49 -07004023 dy0 = -dy0;
4024 dy1 = -dy1;
4025 }
4026
bsalomone5286e02016-01-14 09:24:09 -08004027 GrGLfloat sx0 = (GrGLfloat)srcRect.fLeft;
4028 GrGLfloat sx1 = (GrGLfloat)(srcRect.fLeft + w);
4029 GrGLfloat sy0 = (GrGLfloat)srcRect.fTop;
4030 GrGLfloat sy1 = (GrGLfloat)(srcRect.fTop + h);
Ethan Nicholas5338f992017-04-19 15:54:07 -04004031 int sw = src->width();
bsalomon6df86402015-06-01 10:41:49 -07004032 int sh = src->height();
Robert Phillipsb0e93a22017-08-29 08:26:54 -04004033 if (kBottomLeft_GrSurfaceOrigin == srcOrigin) {
bsalomone5286e02016-01-14 09:24:09 -08004034 sy0 = sh - sy0;
4035 sy1 = sh - sy1;
4036 }
Ethan Nicholas5338f992017-04-19 15:54:07 -04004037 // src rect edges in normalized texture space (0 to 1)
4038 sx0 /= sw;
4039 sx1 /= sw;
4040 sy0 /= sh;
4041 sy1 /= sh;
bsalomon6df86402015-06-01 10:41:49 -07004042
bsalomon7ea33f52015-11-22 14:51:00 -08004043 GL_CALL(Uniform4f(fCopyPrograms[progIdx].fPosXformUniform, dx1 - dx0, dy1 - dy0, dx0, dy0));
4044 GL_CALL(Uniform4f(fCopyPrograms[progIdx].fTexCoordXformUniform,
4045 sx1 - sx0, sy1 - sy0, sx0, sy0));
4046 GL_CALL(Uniform1i(fCopyPrograms[progIdx].fTextureUniform, 0));
bsalomon6df86402015-06-01 10:41:49 -07004047
4048 GrXferProcessor::BlendInfo blendInfo;
4049 blendInfo.reset();
bsalomon7f9b2e42016-01-12 13:29:26 -08004050 this->flushBlend(blendInfo, GrSwizzle::RGBA());
bsalomon6df86402015-06-01 10:41:49 -07004051 this->flushColorWrite(true);
bsalomon083617b2016-02-12 12:10:14 -08004052 this->flushHWAAState(nullptr, false, false);
bsalomon6df86402015-06-01 10:41:49 -07004053 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07004054 this->disableWindowRectangles();
csmartdaltonc7d85332016-10-26 10:13:46 -07004055 this->disableStencil();
Brian Osmanf02fa6f2017-07-11 11:17:47 -04004056 if (this->glCaps().srgbWriteControl()) {
4057 this->flushFramebufferSRGB(true);
4058 }
bsalomon6df86402015-06-01 10:41:49 -07004059
4060 GL_CALL(DrawArrays(GR_GL_TRIANGLE_STRIP, 0, 4));
Brian Salomon71d9d842016-11-03 13:42:00 -04004061 this->unbindTextureFBOForPixelOps(GR_GL_FRAMEBUFFER, dst);
Brian Salomon1fabd512018-02-09 09:54:25 -05004062 this->didWriteToSurface(dst, dstOrigin, &dstRect);
bsalomon083617b2016-02-12 12:10:14 -08004063
cdaltone2e71c22016-04-07 18:13:29 -07004064 return true;
bsalomon6df86402015-06-01 10:41:49 -07004065}
4066
Robert Phillipsb0e93a22017-08-29 08:26:54 -04004067void GrGLGpu::copySurfaceAsCopyTexSubImage(GrSurface* dst, GrSurfaceOrigin dstOrigin,
4068 GrSurface* src, GrSurfaceOrigin srcOrigin,
bsalomon6df86402015-06-01 10:41:49 -07004069 const SkIRect& srcRect,
4070 const SkIPoint& dstPoint) {
Greg Daniel26dbe3b2018-05-03 10:35:42 -04004071 SkASSERT(can_copy_texsubimage(dst, dstOrigin, src, srcOrigin, this->glCaps()));
bsalomon6df86402015-06-01 10:41:49 -07004072 GrGLIRect srcVP;
Brian Salomon71d9d842016-11-03 13:42:00 -04004073 this->bindSurfaceFBOForPixelOps(src, GR_GL_FRAMEBUFFER, &srcVP, kSrc_TempFBOTarget);
bsalomon083617b2016-02-12 12:10:14 -08004074 GrGLTexture* dstTex = static_cast<GrGLTexture *>(dst->asTexture());
bsalomon6df86402015-06-01 10:41:49 -07004075 SkASSERT(dstTex);
4076 // We modified the bound FBO
Robert Phillips294870f2016-11-11 12:38:40 -05004077 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon6df86402015-06-01 10:41:49 -07004078 GrGLIRect srcGLRect;
Robert Phillipsb0e93a22017-08-29 08:26:54 -04004079 srcGLRect.setRelativeTo(srcVP, srcRect, srcOrigin);
bsalomon6df86402015-06-01 10:41:49 -07004080
4081 this->setScratchTextureUnit();
bsalomon10528f12015-10-14 12:54:52 -07004082 GL_CALL(BindTexture(dstTex->target(), dstTex->textureID()));
bsalomon6df86402015-06-01 10:41:49 -07004083 GrGLint dstY;
Robert Phillipsb0e93a22017-08-29 08:26:54 -04004084 if (kBottomLeft_GrSurfaceOrigin == dstOrigin) {
bsalomon6df86402015-06-01 10:41:49 -07004085 dstY = dst->height() - (dstPoint.fY + srcGLRect.fHeight);
4086 } else {
4087 dstY = dstPoint.fY;
4088 }
bsalomon10528f12015-10-14 12:54:52 -07004089 GL_CALL(CopyTexSubImage2D(dstTex->target(), 0,
bsalomon083617b2016-02-12 12:10:14 -08004090 dstPoint.fX, dstY,
4091 srcGLRect.fLeft, srcGLRect.fBottom,
4092 srcGLRect.fWidth, srcGLRect.fHeight));
Brian Salomon71d9d842016-11-03 13:42:00 -04004093 this->unbindTextureFBOForPixelOps(GR_GL_FRAMEBUFFER, src);
bsalomon083617b2016-02-12 12:10:14 -08004094 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
4095 srcRect.width(), srcRect.height());
Brian Salomon1fabd512018-02-09 09:54:25 -05004096 this->didWriteToSurface(dst, dstOrigin, &dstRect);
bsalomon6df86402015-06-01 10:41:49 -07004097}
4098
Robert Phillipsb0e93a22017-08-29 08:26:54 -04004099bool GrGLGpu::copySurfaceAsBlitFramebuffer(GrSurface* dst, GrSurfaceOrigin dstOrigin,
4100 GrSurface* src, GrSurfaceOrigin srcOrigin,
bsalomon6df86402015-06-01 10:41:49 -07004101 const SkIRect& srcRect,
4102 const SkIPoint& dstPoint) {
Robert Phillipsb0e93a22017-08-29 08:26:54 -04004103 SkASSERT(can_blit_framebuffer_for_copy_surface(dst, dstOrigin, src, srcOrigin,
Greg Daniel26dbe3b2018-05-03 10:35:42 -04004104 srcRect, dstPoint, this->glCaps()));
bsalomon6df86402015-06-01 10:41:49 -07004105 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
4106 srcRect.width(), srcRect.height());
4107 if (dst == src) {
4108 if (SkIRect::IntersectsNoEmptyCheck(dstRect, srcRect)) {
4109 return false;
mtklein404b3b22015-05-18 09:29:10 -07004110 }
bsalomon5df6fee2015-05-18 06:26:15 -07004111 }
bsalomon6df86402015-06-01 10:41:49 -07004112
bsalomon6df86402015-06-01 10:41:49 -07004113 GrGLIRect dstVP;
4114 GrGLIRect srcVP;
Brian Salomon71d9d842016-11-03 13:42:00 -04004115 this->bindSurfaceFBOForPixelOps(dst, GR_GL_DRAW_FRAMEBUFFER, &dstVP, kDst_TempFBOTarget);
4116 this->bindSurfaceFBOForPixelOps(src, GR_GL_READ_FRAMEBUFFER, &srcVP, kSrc_TempFBOTarget);
bsalomon6df86402015-06-01 10:41:49 -07004117 // We modified the bound FBO
Robert Phillips294870f2016-11-11 12:38:40 -05004118 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon6df86402015-06-01 10:41:49 -07004119 GrGLIRect srcGLRect;
4120 GrGLIRect dstGLRect;
Robert Phillipsb0e93a22017-08-29 08:26:54 -04004121 srcGLRect.setRelativeTo(srcVP, srcRect, srcOrigin);
4122 dstGLRect.setRelativeTo(dstVP, dstRect, dstOrigin);
bsalomon6df86402015-06-01 10:41:49 -07004123
4124 // BlitFrameBuffer respects the scissor, so disable it.
4125 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07004126 this->disableWindowRectangles();
bsalomon6df86402015-06-01 10:41:49 -07004127
4128 GrGLint srcY0;
4129 GrGLint srcY1;
4130 // Does the blit need to y-mirror or not?
Robert Phillipsb0e93a22017-08-29 08:26:54 -04004131 if (srcOrigin == dstOrigin) {
bsalomon6df86402015-06-01 10:41:49 -07004132 srcY0 = srcGLRect.fBottom;
4133 srcY1 = srcGLRect.fBottom + srcGLRect.fHeight;
4134 } else {
4135 srcY0 = srcGLRect.fBottom + srcGLRect.fHeight;
4136 srcY1 = srcGLRect.fBottom;
4137 }
4138 GL_CALL(BlitFramebuffer(srcGLRect.fLeft,
4139 srcY0,
4140 srcGLRect.fLeft + srcGLRect.fWidth,
4141 srcY1,
4142 dstGLRect.fLeft,
4143 dstGLRect.fBottom,
4144 dstGLRect.fLeft + dstGLRect.fWidth,
4145 dstGLRect.fBottom + dstGLRect.fHeight,
4146 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
Brian Salomon71d9d842016-11-03 13:42:00 -04004147 this->unbindTextureFBOForPixelOps(GR_GL_DRAW_FRAMEBUFFER, dst);
4148 this->unbindTextureFBOForPixelOps(GR_GL_READ_FRAMEBUFFER, src);
Brian Salomon1fabd512018-02-09 09:54:25 -05004149 this->didWriteToSurface(dst, dstOrigin, &dstRect);
bsalomon6df86402015-06-01 10:41:49 -07004150 return true;
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00004151}
4152
brianosman33f6b3f2016-06-02 05:49:21 -07004153// Manual implementation of mipmap generation, to work around driver bugs w/sRGB.
4154// Uses draw calls to do a series of downsample operations to successive mips.
4155// If this returns false, then the calling code falls back to using glGenerateMipmap.
Robert Phillipsb0e93a22017-08-29 08:26:54 -04004156bool GrGLGpu::generateMipmap(GrGLTexture* texture, GrSurfaceOrigin textureOrigin,
4157 bool gammaCorrect) {
brianosman09563ce2016-06-02 08:59:34 -07004158 // Our iterative downsample requires the ability to limit which level we're sampling:
4159 if (!this->glCaps().doManualMipmapping()) {
brianosman33f6b3f2016-06-02 05:49:21 -07004160 return false;
4161 }
4162
4163 // Mipmaps are only supported on 2D textures:
4164 if (GR_GL_TEXTURE_2D != texture->target()) {
4165 return false;
4166 }
4167
4168 // We need to be able to render to the texture for this to work:
Brian Salomon71d9d842016-11-03 13:42:00 -04004169 if (!this->glCaps().canConfigBeFBOColorAttachment(texture->config())) {
brianosman33f6b3f2016-06-02 05:49:21 -07004170 return false;
4171 }
4172
brianosman33f6b3f2016-06-02 05:49:21 -07004173 // If we're mipping an sRGB texture, we need to ensure FB sRGB is correct:
4174 if (GrPixelConfigIsSRGB(texture->config())) {
4175 // If we have write-control, just set the state that we want:
4176 if (this->glCaps().srgbWriteControl()) {
4177 this->flushFramebufferSRGB(gammaCorrect);
4178 } else if (!gammaCorrect) {
4179 // If we don't have write-control we can't do non-gamma-correct mipmapping:
4180 return false;
4181 }
4182 }
4183
4184 int width = texture->width();
4185 int height = texture->height();
4186 int levelCount = SkMipMap::ComputeLevelCount(width, height) + 1;
4187
4188 // Define all mips, if we haven't previously done so:
4189 if (0 == texture->texturePriv().maxMipMapLevel()) {
4190 GrGLenum internalFormat;
4191 GrGLenum externalFormat;
4192 GrGLenum externalType;
4193 if (!this->glCaps().getTexImageFormats(texture->config(), texture->config(),
4194 &internalFormat, &externalFormat, &externalType)) {
4195 return false;
4196 }
4197
Brian Osman9b560242017-09-05 15:34:52 -04004198 this->unbindCpuToGpuXferBuffer();
4199
brianosman33f6b3f2016-06-02 05:49:21 -07004200 for (GrGLint level = 1; level < levelCount; ++level) {
4201 // Define the next mip:
4202 width = SkTMax(1, width / 2);
4203 height = SkTMax(1, height / 2);
4204 GL_ALLOC_CALL(this->glInterface(), TexImage2D(GR_GL_TEXTURE_2D, level, internalFormat,
4205 width, height, 0,
4206 externalFormat, externalType, nullptr));
4207 }
4208 }
4209
4210 // Create (if necessary), then bind temporary FBO:
4211 if (0 == fTempDstFBOID) {
4212 GL_CALL(GenFramebuffers(1, &fTempDstFBOID));
4213 }
Adrienne Walker4ee88512018-05-17 11:37:14 -07004214 this->bindFramebuffer(GR_GL_FRAMEBUFFER, fTempDstFBOID);
Robert Phillips294870f2016-11-11 12:38:40 -05004215 fHWBoundRenderTargetUniqueID.makeInvalid();
brianosman33f6b3f2016-06-02 05:49:21 -07004216
4217 // Bind the texture, to get things configured for filtering.
4218 // We'll be changing our base level further below:
4219 this->setTextureUnit(0);
Brian Salomon2bbdcc42017-09-07 12:36:34 -04004220 this->bindTexture(0, GrSamplerState::ClampBilerp(), gammaCorrect, texture, textureOrigin);
brianosman33f6b3f2016-06-02 05:49:21 -07004221
4222 // Vertex data:
4223 if (!fMipmapProgramArrayBuffer) {
4224 static const GrGLfloat vdata[] = {
4225 0, 0,
4226 0, 1,
4227 1, 0,
4228 1, 1
4229 };
4230 fMipmapProgramArrayBuffer.reset(GrGLBuffer::Create(this, sizeof(vdata),
4231 kVertex_GrBufferType,
4232 kStatic_GrAccessPattern, vdata));
4233 }
4234 if (!fMipmapProgramArrayBuffer) {
4235 return false;
4236 }
4237
4238 fHWVertexArrayState.setVertexArrayID(this, 0);
4239
4240 GrGLAttribArrayState* attribs = fHWVertexArrayState.bindInternalVertexArray(this);
Chris Dalton8e45b4f2017-05-05 14:00:56 -04004241 attribs->enableVertexArrays(this, 1);
Ethan Nicholasfa7ee242017-09-25 09:52:04 -04004242 attribs->set(this, 0, fMipmapProgramArrayBuffer.get(), kHalf2_GrVertexAttribType,
brianosman33f6b3f2016-06-02 05:49:21 -07004243 2 * sizeof(GrGLfloat), 0);
brianosman33f6b3f2016-06-02 05:49:21 -07004244
4245 // Set "simple" state once:
4246 GrXferProcessor::BlendInfo blendInfo;
4247 blendInfo.reset();
4248 this->flushBlend(blendInfo, GrSwizzle::RGBA());
4249 this->flushColorWrite(true);
brianosman33f6b3f2016-06-02 05:49:21 -07004250 this->flushHWAAState(nullptr, false, false);
4251 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07004252 this->disableWindowRectangles();
csmartdaltonc7d85332016-10-26 10:13:46 -07004253 this->disableStencil();
brianosman33f6b3f2016-06-02 05:49:21 -07004254
4255 // Do all the blits:
4256 width = texture->width();
4257 height = texture->height();
4258 GrGLIRect viewport;
4259 viewport.fLeft = 0;
4260 viewport.fBottom = 0;
4261 for (GrGLint level = 1; level < levelCount; ++level) {
4262 // Get and bind the program for this particular downsample (filter shape can vary):
4263 int progIdx = TextureSizeToMipmapProgramIdx(width, height);
4264 if (!fMipmapPrograms[progIdx].fProgram) {
4265 if (!this->createMipmapProgram(progIdx)) {
4266 SkDebugf("Failed to create mipmap program.\n");
4267 return false;
4268 }
4269 }
4270 GL_CALL(UseProgram(fMipmapPrograms[progIdx].fProgram));
4271 fHWProgramID = fMipmapPrograms[progIdx].fProgram;
4272
4273 // Texcoord uniform is expected to contain (1/w, (w-1)/w, 1/h, (h-1)/h)
4274 const float invWidth = 1.0f / width;
4275 const float invHeight = 1.0f / height;
4276 GL_CALL(Uniform4f(fMipmapPrograms[progIdx].fTexCoordXformUniform,
4277 invWidth, (width - 1) * invWidth, invHeight, (height - 1) * invHeight));
4278 GL_CALL(Uniform1i(fMipmapPrograms[progIdx].fTextureUniform, 0));
4279
4280 // Only sample from previous mip
4281 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_BASE_LEVEL, level - 1));
4282
4283 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
4284 GR_GL_TEXTURE_2D, texture->textureID(), level));
4285
4286 width = SkTMax(1, width / 2);
4287 height = SkTMax(1, height / 2);
4288 viewport.fWidth = width;
4289 viewport.fHeight = height;
4290 this->flushViewport(viewport);
4291
4292 GL_CALL(DrawArrays(GR_GL_TRIANGLE_STRIP, 0, 4));
4293 }
4294
4295 // Unbind:
4296 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
4297 GR_GL_TEXTURE_2D, 0, 0));
4298
4299 return true;
4300}
4301
cdalton231c5fd2015-05-13 12:35:36 -07004302void GrGLGpu::xferBarrier(GrRenderTarget* rt, GrXferBarrierType type) {
bsalomoncb02b382015-08-12 11:14:50 -07004303 SkASSERT(type);
cdalton9954bc32015-04-29 14:17:00 -07004304 switch (type) {
cdalton231c5fd2015-05-13 12:35:36 -07004305 case kTexture_GrXferBarrierType: {
4306 GrGLRenderTarget* glrt = static_cast<GrGLRenderTarget*>(rt);
Brian Osmancfe83d12018-01-19 11:13:45 -05004307 SkASSERT(glrt->textureFBOID() != 0 && glrt->renderFBOID() != 0);
cdalton231c5fd2015-05-13 12:35:36 -07004308 if (glrt->textureFBOID() != glrt->renderFBOID()) {
4309 // The render target uses separate storage so no need for glTextureBarrier.
4310 // FIXME: The render target will resolve automatically when its texture is bound,
4311 // but we could resolve only the bounds that will be read if we do it here instead.
4312 return;
4313 }
cdalton9954bc32015-04-29 14:17:00 -07004314 SkASSERT(this->caps()->textureBarrierSupport());
4315 GL_CALL(TextureBarrier());
4316 return;
cdalton231c5fd2015-05-13 12:35:36 -07004317 }
cdalton8917d622015-05-06 13:40:21 -07004318 case kBlend_GrXferBarrierType:
bsalomon4b91f762015-05-19 09:29:46 -07004319 SkASSERT(GrCaps::kAdvanced_BlendEquationSupport ==
cdalton8917d622015-05-06 13:40:21 -07004320 this->caps()->blendEquationSupport());
4321 GL_CALL(BlendBarrier());
4322 return;
bsalomoncb02b382015-08-12 11:14:50 -07004323 default: break; // placate compiler warnings that kNone not handled
cdalton9954bc32015-04-29 14:17:00 -07004324 }
4325}
4326
Brian Salomonf865b052018-03-09 09:01:53 -05004327#if GR_TEST_UTILS
Brian Salomon52e943a2018-03-13 09:32:39 -04004328GrBackendTexture GrGLGpu::createTestingOnlyBackendTexture(const void* pixels, int w, int h,
Robert Phillipsd21b2a52017-12-12 13:01:25 -05004329 GrPixelConfig config, bool /*isRT*/,
4330 GrMipMapped mipMapped) {
Brian Salomon8a375832018-03-14 10:21:40 -04004331 this->handleDirtyContext();
Robert Phillipsd21b2a52017-12-12 13:01:25 -05004332 if (!this->caps()->isConfigTexturable(config)) {
4333 return GrBackendTexture(); // invalid
4334 }
4335
Greg Daniel92cbf3f2018-04-12 16:50:17 -04004336 if (w > this->caps()->maxTextureSize() || h > this->caps()->maxTextureSize()) {
4337 return GrBackendTexture(); // invalid
4338 }
4339
Robert Phillipsd21b2a52017-12-12 13:01:25 -05004340 // Currently we don't support uploading pixel data when mipped.
4341 if (pixels && GrMipMapped::kYes == mipMapped) {
4342 return GrBackendTexture(); // invalid
4343 }
4344
4345 GrGLTextureInfo info;
4346 info.fTarget = GR_GL_TEXTURE_2D;
4347 info.fID = 0;
4348 GL_CALL(GenTextures(1, &info.fID));
4349 GL_CALL(ActiveTexture(GR_GL_TEXTURE0));
4350 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, 1));
4351 GL_CALL(BindTexture(info.fTarget, info.fID));
4352 fHWBoundTextureUniqueIDs[0].makeInvalid();
4353 GL_CALL(TexParameteri(info.fTarget, GR_GL_TEXTURE_MAG_FILTER, GR_GL_NEAREST));
4354 GL_CALL(TexParameteri(info.fTarget, GR_GL_TEXTURE_MIN_FILTER, GR_GL_NEAREST));
4355 GL_CALL(TexParameteri(info.fTarget, GR_GL_TEXTURE_WRAP_S, GR_GL_CLAMP_TO_EDGE));
4356 GL_CALL(TexParameteri(info.fTarget, GR_GL_TEXTURE_WRAP_T, GR_GL_CLAMP_TO_EDGE));
4357
4358 GrGLenum internalFormat;
4359 GrGLenum externalFormat;
4360 GrGLenum externalType;
4361
4362 if (!this->glCaps().getTexImageFormats(config, config, &internalFormat, &externalFormat,
4363 &externalType)) {
4364 return GrBackendTexture(); // invalid
4365 }
4366
Greg Danielf5d87582017-12-18 14:48:15 -05004367 info.fFormat = this->glCaps().configSizedInternalFormat(config);
4368
Robert Phillipsd21b2a52017-12-12 13:01:25 -05004369 this->unbindCpuToGpuXferBuffer();
4370
4371 // Figure out the number of mip levels.
4372 int mipLevels = 1;
4373 if (GrMipMapped::kYes == mipMapped) {
4374 mipLevels = SkMipMap::ComputeLevelCount(w, h) + 1;
4375 }
4376
4377 size_t bpp = GrBytesPerPixel(config);
4378 size_t baseLayerSize = bpp * w * h;
4379 SkAutoMalloc defaultStorage(baseLayerSize);
4380 if (!pixels) {
4381 // Fill in the texture with all zeros so we don't have random garbage
4382 pixels = defaultStorage.get();
Brian Salomon52e943a2018-03-13 09:32:39 -04004383 memset(defaultStorage.get(), 0, baseLayerSize);
Robert Phillipsd21b2a52017-12-12 13:01:25 -05004384 }
4385
4386 int width = w;
4387 int height = h;
4388 for (int i = 0; i < mipLevels; ++i) {
4389 GL_CALL(TexImage2D(info.fTarget, i, internalFormat, width, height, 0, externalFormat,
4390 externalType, pixels));
4391 width = SkTMax(1, width / 2);
4392 height = SkTMax(1, height / 2);
4393 }
4394
Robert Phillipse8fabb22018-02-04 14:33:21 -05004395 // unbind the texture from the texture unit to avoid asserts
4396 GL_CALL(BindTexture(info.fTarget, 0));
4397
Greg Daniel8a3f55c2018-03-14 17:32:12 +00004398 return GrBackendTexture(w, h, mipMapped, info);
Robert Phillipsd21b2a52017-12-12 13:01:25 -05004399}
4400
4401bool GrGLGpu::isTestingOnlyBackendTexture(const GrBackendTexture& tex) const {
4402 SkASSERT(kOpenGL_GrBackend == tex.backend());
4403
Greg Daniel52e16d92018-04-10 09:34:07 -04004404 GrGLTextureInfo info;
4405 if (!tex.getGLTextureInfo(&info)) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -05004406 return false;
4407 }
4408
4409 GrGLboolean result;
Greg Daniel52e16d92018-04-10 09:34:07 -04004410 GL_CALL_RET(result, IsTexture(info.fID));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05004411
4412 return (GR_GL_TRUE == result);
4413}
4414
Brian Salomon26102cb2018-03-09 09:33:19 -05004415void GrGLGpu::deleteTestingOnlyBackendTexture(const GrBackendTexture& tex) {
4416 SkASSERT(kOpenGL_GrBackend == tex.backend());
Robert Phillipsd21b2a52017-12-12 13:01:25 -05004417
Greg Daniel52e16d92018-04-10 09:34:07 -04004418 GrGLTextureInfo info;
4419 if (tex.getGLTextureInfo(&info)) {
4420 GL_CALL(DeleteTextures(1, &info.fID));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05004421 }
4422}
4423
Brian Salomonf865b052018-03-09 09:01:53 -05004424GrBackendRenderTarget GrGLGpu::createTestingOnlyBackendRenderTarget(int w, int h,
4425 GrColorType colorType,
4426 GrSRGBEncoded srgbEncoded) {
Greg Daniel92cbf3f2018-04-12 16:50:17 -04004427 if (w > this->caps()->maxRenderTargetSize() || h > this->caps()->maxRenderTargetSize()) {
4428 return GrBackendRenderTarget(); // invalid
4429 }
Brian Salomon8a375832018-03-14 10:21:40 -04004430 this->handleDirtyContext();
Brian Salomonf865b052018-03-09 09:01:53 -05004431 auto config = GrColorTypeToPixelConfig(colorType, srgbEncoded);
4432 GrGLenum colorBufferFormat;
4433 if (!this->glCaps().getRenderbufferFormat(config, &colorBufferFormat)) {
4434 return {};
4435 }
4436 int sFormatIdx = this->getCompatibleStencilIndex(config);
4437 if (sFormatIdx < 0) {
4438 return {};
4439 }
4440 GrGLuint rbIDs[] = {0, 0};
4441 GL_CALL(GenRenderbuffers(2, rbIDs));
4442 if (!rbIDs[0] || !rbIDs[1]) {
4443 if (!rbIDs[0]) {
4444 GL_CALL(DeleteRenderbuffers(1, &rbIDs[0]));
4445 }
4446 if (!rbIDs[1]) {
4447 GL_CALL(DeleteRenderbuffers(1, &rbIDs[1]));
4448 }
4449 return {};
4450 }
4451 GrGLFramebufferInfo info;
4452 info.fFBOID = 0;
4453 GL_CALL(GenFramebuffers(1, &info.fFBOID));
4454 if (!info.fFBOID) {
4455 GL_CALL(DeleteRenderbuffers(2, rbIDs));
4456 return GrBackendRenderTarget();
4457 }
4458
4459 this->invalidateBoundRenderTarget();
4460
Adrienne Walker4ee88512018-05-17 11:37:14 -07004461 this->bindFramebuffer(GR_GL_FRAMEBUFFER, info.fFBOID);
Brian Salomonf865b052018-03-09 09:01:53 -05004462 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, rbIDs[0]));
4463 GL_ALLOC_CALL(this->glInterface(),
4464 RenderbufferStorage(GR_GL_RENDERBUFFER, colorBufferFormat, w, h));
4465 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0, GR_GL_RENDERBUFFER,
4466 rbIDs[0]));
4467 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, rbIDs[1]));
4468 auto stencilBufferFormat = this->glCaps().stencilFormats()[sFormatIdx].fInternalFormat;
4469 GL_ALLOC_CALL(this->glInterface(),
4470 RenderbufferStorage(GR_GL_RENDERBUFFER, stencilBufferFormat, w, h));
4471 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT, GR_GL_RENDERBUFFER,
4472 rbIDs[1]));
4473 if (this->glCaps().stencilFormats()[sFormatIdx].fPacked) {
4474 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_DEPTH_ATTACHMENT,
4475 GR_GL_RENDERBUFFER, rbIDs[1]));
4476 }
4477
4478 // We don't want to have to recover the renderbuffer IDs later to delete them. OpenGL has this
4479 // rule that if a renderbuffer is deleted and a FBO other than the current FBO has the RB
4480 // attached then deletion is delayed. So we unbind the FBO here and delete the renderbuffers.
Adrienne Walker4ee88512018-05-17 11:37:14 -07004481 this->bindFramebuffer(GR_GL_FRAMEBUFFER, 0);
Brian Salomonf865b052018-03-09 09:01:53 -05004482 GL_CALL(DeleteRenderbuffers(2, rbIDs));
4483
Adrienne Walker4ee88512018-05-17 11:37:14 -07004484 this->bindFramebuffer(GR_GL_FRAMEBUFFER, info.fFBOID);
Brian Salomonf865b052018-03-09 09:01:53 -05004485 GrGLenum status;
4486 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
4487 if (GR_GL_FRAMEBUFFER_COMPLETE != status) {
Adrienne Walker4ee88512018-05-17 11:37:14 -07004488 this->deleteFramebuffer(info.fFBOID);
Brian Salomonf865b052018-03-09 09:01:53 -05004489 return {};
4490 }
4491 auto stencilBits = SkToInt(this->glCaps().stencilFormats()[sFormatIdx].fStencilBits);
Greg Daniel8a3f55c2018-03-14 17:32:12 +00004492 return {w, h, 1, stencilBits, config, info};
Brian Salomonf865b052018-03-09 09:01:53 -05004493}
4494
4495void GrGLGpu::deleteTestingOnlyBackendRenderTarget(const GrBackendRenderTarget& backendRT) {
4496 SkASSERT(kOpenGL_GrBackend == backendRT.backend());
Greg Daniel323fbcf2018-04-10 13:46:30 -04004497 GrGLFramebufferInfo info;
4498 if (backendRT.getGLFramebufferInfo(&info)) {
4499 if (info.fFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -07004500 this->deleteFramebuffer(info.fFBOID);
Brian Salomonf865b052018-03-09 09:01:53 -05004501 }
4502 }
joshualitt8fd844f2015-12-02 13:36:47 -08004503}
4504
Greg Daniel26b50a42018-03-08 09:49:58 -05004505void GrGLGpu::testingOnly_flushGpuAndSync() {
4506 GL_CALL(Finish());
4507}
Brian Salomonf865b052018-03-09 09:01:53 -05004508#endif
Greg Daniel26b50a42018-03-08 09:49:58 -05004509
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00004510///////////////////////////////////////////////////////////////////////////////
bsalomon6df86402015-06-01 10:41:49 -07004511
cdaltone2e71c22016-04-07 18:13:29 -07004512GrGLAttribArrayState* GrGLGpu::HWVertexArrayState::bindInternalVertexArray(GrGLGpu* gpu,
csmartdalton485a1202016-07-13 10:16:32 -07004513 const GrBuffer* ibuf) {
robertphillips@google.com4f65a272013-03-26 19:40:46 +00004514 GrGLAttribArrayState* attribState;
4515
cdaltone2e71c22016-04-07 18:13:29 -07004516 if (gpu->glCaps().isCoreProfile()) {
4517 if (!fCoreProfileVertexArray) {
bsalomon@google.com6918d482013-03-07 19:09:11 +00004518 GrGLuint arrayID;
4519 GR_GL_CALL(gpu->glInterface(), GenVertexArrays(1, &arrayID));
4520 int attrCount = gpu->glCaps().maxVertexAttributes();
cdaltone2e71c22016-04-07 18:13:29 -07004521 fCoreProfileVertexArray = new GrGLVertexArray(arrayID, attrCount);
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00004522 }
cdaltone2e71c22016-04-07 18:13:29 -07004523 if (ibuf) {
4524 attribState = fCoreProfileVertexArray->bindWithIndexBuffer(gpu, ibuf);
bsalomon6df86402015-06-01 10:41:49 -07004525 } else {
cdaltone2e71c22016-04-07 18:13:29 -07004526 attribState = fCoreProfileVertexArray->bind(gpu);
bsalomon6df86402015-06-01 10:41:49 -07004527 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00004528 } else {
cdaltone2e71c22016-04-07 18:13:29 -07004529 if (ibuf) {
4530 // bindBuffer implicitly binds VAO 0 when binding an index buffer.
4531 gpu->bindBuffer(kIndex_GrBufferType, ibuf);
bsalomon@google.com6918d482013-03-07 19:09:11 +00004532 } else {
4533 this->setVertexArrayID(gpu, 0);
4534 }
4535 int attrCount = gpu->glCaps().maxVertexAttributes();
4536 if (fDefaultVertexArrayAttribState.count() != attrCount) {
4537 fDefaultVertexArrayAttribState.resize(attrCount);
4538 }
4539 attribState = &fDefaultVertexArrayAttribState;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00004540 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00004541 return attribState;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00004542}
bsalomone179a912016-01-20 06:18:10 -08004543
Greg Daniel51316782017-08-02 15:10:09 +00004544void GrGLGpu::onFinishFlush(bool insertedSemaphore) {
4545 // If we inserted semaphores during the flush, we need to call GLFlush.
4546 if (insertedSemaphore) {
4547 GL_CALL(Flush());
4548 }
4549}
4550
Greg Daniel6be35232017-03-01 17:01:09 -05004551GrFence SK_WARN_UNUSED_RESULT GrGLGpu::insertFence() {
Greg Danielc64ee462017-06-15 16:59:49 -04004552 SkASSERT(this->caps()->fenceSyncSupport());
Greg Daniel6be35232017-03-01 17:01:09 -05004553 GrGLsync sync;
4554 GL_CALL_RET(sync, FenceSync(GR_GL_SYNC_GPU_COMMANDS_COMPLETE, 0));
4555 GR_STATIC_ASSERT(sizeof(GrFence) >= sizeof(GrGLsync));
4556 return (GrFence)sync;
jvanverth84741b32016-09-30 08:39:02 -07004557}
4558
Greg Daniel6be35232017-03-01 17:01:09 -05004559bool GrGLGpu::waitFence(GrFence fence, uint64_t timeout) {
jvanverth84741b32016-09-30 08:39:02 -07004560 GrGLenum result;
4561 GL_CALL_RET(result, ClientWaitSync((GrGLsync)fence, GR_GL_SYNC_FLUSH_COMMANDS_BIT, timeout));
4562 return (GR_GL_CONDITION_SATISFIED == result);
4563}
4564
4565void GrGLGpu::deleteFence(GrFence fence) const {
Greg Daniel6be35232017-03-01 17:01:09 -05004566 this->deleteSync((GrGLsync)fence);
4567}
4568
Greg Daniela5cb7812017-06-16 09:45:32 -04004569sk_sp<GrSemaphore> SK_WARN_UNUSED_RESULT GrGLGpu::makeSemaphore(bool isOwned) {
Greg Danielc64ee462017-06-15 16:59:49 -04004570 SkASSERT(this->caps()->fenceSyncSupport());
Greg Daniela5cb7812017-06-16 09:45:32 -04004571 return GrGLSemaphore::Make(this, isOwned);
Greg Daniel6be35232017-03-01 17:01:09 -05004572}
4573
Greg Daniel48661b82018-01-22 16:11:35 -05004574sk_sp<GrSemaphore> GrGLGpu::wrapBackendSemaphore(const GrBackendSemaphore& semaphore,
4575 GrResourceProvider::SemaphoreWrapType wrapType,
4576 GrWrapOwnership ownership) {
Greg Danielc64ee462017-06-15 16:59:49 -04004577 SkASSERT(this->caps()->fenceSyncSupport());
Greg Daniela5cb7812017-06-16 09:45:32 -04004578 return GrGLSemaphore::MakeWrapped(this, semaphore.glSync(), ownership);
4579}
4580
Greg Daniel48661b82018-01-22 16:11:35 -05004581void GrGLGpu::insertSemaphore(sk_sp<GrSemaphore> semaphore, bool flush) {
Greg Daniel6be35232017-03-01 17:01:09 -05004582 GrGLSemaphore* glSem = static_cast<GrGLSemaphore*>(semaphore.get());
4583
Greg Daniel48661b82018-01-22 16:11:35 -05004584 GrGLsync sync;
4585 GL_CALL_RET(sync, FenceSync(GR_GL_SYNC_GPU_COMMANDS_COMPLETE, 0));
4586 glSem->setSync(sync);
Brian Osmandc87c952017-04-28 13:57:38 -04004587
4588 if (flush) {
4589 GL_CALL(Flush());
4590 }
Greg Daniel6be35232017-03-01 17:01:09 -05004591}
4592
Greg Daniel48661b82018-01-22 16:11:35 -05004593void GrGLGpu::waitSemaphore(sk_sp<GrSemaphore> semaphore) {
Greg Daniel6be35232017-03-01 17:01:09 -05004594 GrGLSemaphore* glSem = static_cast<GrGLSemaphore*>(semaphore.get());
4595
4596 GL_CALL(WaitSync(glSem->sync(), 0, GR_GL_TIMEOUT_IGNORED));
4597}
4598
4599void GrGLGpu::deleteSync(GrGLsync sync) const {
4600 GL_CALL(DeleteSync(sync));
jvanverth84741b32016-09-30 08:39:02 -07004601}
Brian Osman13dddce2017-05-09 13:19:50 -04004602
Robert Phillips65a88fa2017-08-08 08:36:22 -04004603void GrGLGpu::insertEventMarker(const char* msg) {
4604 GL_CALL(InsertEventMarker(strlen(msg), msg));
4605}
4606
Brian Osman13dddce2017-05-09 13:19:50 -04004607sk_sp<GrSemaphore> GrGLGpu::prepareTextureForCrossContextUsage(GrTexture* texture) {
4608 // Set up a semaphore to be signaled once the data is ready, and flush GL
Greg Daniela5cb7812017-06-16 09:45:32 -04004609 sk_sp<GrSemaphore> semaphore = this->makeSemaphore(true);
Brian Osman13dddce2017-05-09 13:19:50 -04004610 this->insertSemaphore(semaphore, true);
4611
4612 return semaphore;
4613}
Robert Phillips646e4292017-06-13 12:44:56 -04004614
4615int GrGLGpu::TextureToCopyProgramIdx(GrTexture* texture) {
4616 switch (texture->texturePriv().samplerType()) {
4617 case kTexture2DSampler_GrSLType:
4618 return 0;
Robert Phillips646e4292017-06-13 12:44:56 -04004619 case kTexture2DRectSampler_GrSLType:
Brian Salomon57111332018-02-05 15:55:54 -05004620 return 1;
Robert Phillips646e4292017-06-13 12:44:56 -04004621 case kTextureExternalSampler_GrSLType:
Brian Salomon57111332018-02-05 15:55:54 -05004622 return 2;
Robert Phillips646e4292017-06-13 12:44:56 -04004623 default:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -04004624 SK_ABORT("Unexpected samper type");
Robert Phillips646e4292017-06-13 12:44:56 -04004625 return 0;
4626 }
4627}
Brian Osman71a18892017-08-10 10:23:25 -04004628
4629void GrGLGpu::onDumpJSON(SkJSONWriter* writer) const {
4630 // We are called by the base class, which has already called beginObject(). We choose to nest
4631 // all of our caps information in a named sub-object.
4632 writer->beginObject("GL GPU");
4633
4634 const GrGLubyte* str;
4635 GL_CALL_RET(str, GetString(GR_GL_VERSION));
4636 writer->appendString("GL_VERSION", (const char*)(str));
4637 GL_CALL_RET(str, GetString(GR_GL_RENDERER));
4638 writer->appendString("GL_RENDERER", (const char*)(str));
4639 GL_CALL_RET(str, GetString(GR_GL_VENDOR));
4640 writer->appendString("GL_VENDOR", (const char*)(str));
4641 GL_CALL_RET(str, GetString(GR_GL_SHADING_LANGUAGE_VERSION));
4642 writer->appendString("GL_SHADING_LANGUAGE_VERSION", (const char*)(str));
4643
4644 writer->appendName("extensions");
4645 glInterface()->fExtensions.dumpJSON(writer);
4646
4647 writer->endObject();
4648}