blob: 1cf60d8ffd7c132746e27ac358b7e05b783904d7 [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) {
egdanield803f272015-03-18 13:01:52 -0700260 GL_CALL(DeleteFramebuffers(1, &fTempSrcFBOID));
egdaniel0f5f9672015-02-03 11:10:51 -0800261 }
Brian Salomon43f8bf02017-10-18 08:33:29 -0400262 if (fTempDstFBOID) {
egdanield803f272015-03-18 13:01:52 -0700263 GL_CALL(DeleteFramebuffers(1, &fTempDstFBOID));
egdaniel0f5f9672015-02-03 11:10:51 -0800264 }
Brian Salomon43f8bf02017-10-18 08:33:29 -0400265 if (fStencilClearFBOID) {
egdanield803f272015-03-18 13:01:52 -0700266 GL_CALL(DeleteFramebuffers(1, &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) {
299 GL_CALL(DeleteFramebuffers(1, &fTempSrcFBOID));
300 }
301 if (fTempDstFBOID) {
302 GL_CALL(DeleteFramebuffers(1, &fTempDstFBOID));
303 }
304 if (fStencilClearFBOID) {
305 GL_CALL(DeleteFramebuffers(1, &fStencilClearFBOID));
306 }
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();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000464 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000465
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000466 if (resetBits & kRenderTarget_GrGLBackendState) {
Robert Phillips294870f2016-11-11 12:38:40 -0500467 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon16921ec2015-07-30 15:34:56 -0700468 fHWSRGBFramebuffer = kUnknown_TriState;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000469 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000470
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000471 if (resetBits & kPathRendering_GrGLBackendState) {
jvanverthe9c0fc62015-04-29 11:18:05 -0700472 if (this->caps()->shaderCaps()->pathRenderingSupport()) {
kkinnunenccdaa042014-08-20 01:36:23 -0700473 this->glPathRendering()->resetContext();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000474 }
bsalomon@google.com05a718c2012-06-29 14:01:53 +0000475 }
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000476
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000477 // we assume these values
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000478 if (resetBits & kPixelStore_GrGLBackendState) {
479 if (this->glCaps().unpackRowLengthSupport()) {
480 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
481 }
482 if (this->glCaps().packRowLengthSupport()) {
483 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
484 }
485 if (this->glCaps().unpackFlipYSupport()) {
486 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
487 }
488 if (this->glCaps().packFlipYSupport()) {
489 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, GR_GL_FALSE));
490 }
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000491 }
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000492
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000493 if (resetBits & kProgram_GrGLBackendState) {
494 fHWProgramID = 0;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000495 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000496}
497
Brian Salomond17f6582017-07-19 18:28:58 -0400498static bool check_backend_texture(const GrBackendTexture& backendTex, const GrGLCaps& caps,
499 GrGLTexture::IDDesc* idDesc) {
Greg Daniel52e16d92018-04-10 09:34:07 -0400500 GrGLTextureInfo info;
501 if (!backendTex.getGLTextureInfo(&info) || !info.fID) {
Brian Salomond17f6582017-07-19 18:28:58 -0400502 return false;
bsalomon091f60c2015-11-10 11:54:56 -0800503 }
robertphillips@google.comb72e5d32012-10-30 15:18:10 +0000504
Greg Daniel52e16d92018-04-10 09:34:07 -0400505 idDesc->fInfo = info;
bsalomon7ea33f52015-11-22 14:51:00 -0800506
Brian Salomond17f6582017-07-19 18:28:58 -0400507 if (GR_GL_TEXTURE_EXTERNAL == idDesc->fInfo.fTarget) {
508 if (!caps.shaderCaps()->externalTextureSupport()) {
509 return false;
510 }
511 } else if (GR_GL_TEXTURE_RECTANGLE == idDesc->fInfo.fTarget) {
512 if (!caps.rectangleTextureSupport()) {
513 return false;
514 }
515 } else if (GR_GL_TEXTURE_2D != idDesc->fInfo.fTarget) {
516 return false;
517 }
518 return true;
519}
520
521sk_sp<GrTexture> GrGLGpu::onWrapBackendTexture(const GrBackendTexture& backendTex,
Brian Salomond17f6582017-07-19 18:28:58 -0400522 GrWrapOwnership ownership) {
bsalomonb15b4c12014-10-29 12:41:57 -0700523 GrGLTexture::IDDesc idDesc;
Brian Salomond17f6582017-07-19 18:28:58 -0400524 if (!check_backend_texture(backendTex, this->glCaps(), &idDesc)) {
525 return nullptr;
526 }
Greg Daniele7d8da42017-12-04 11:23:19 -0500527 if (!idDesc.fInfo.fFormat) {
528 idDesc.fInfo.fFormat = this->glCaps().configSizedInternalFormat(backendTex.config());
529 }
Brian Salomond17f6582017-07-19 18:28:58 -0400530 if (kBorrow_GrWrapOwnership == ownership) {
531 idDesc.fOwnership = GrBackendObjectOwnership::kBorrowed;
532 } else {
533 idDesc.fOwnership = GrBackendObjectOwnership::kOwned;
534 }
bsalomone5286e02016-01-14 09:24:09 -0800535
Brian Salomond17f6582017-07-19 18:28:58 -0400536 GrSurfaceDesc surfDesc;
537 surfDesc.fFlags = kNone_GrSurfaceFlags;
538 surfDesc.fWidth = backendTex.width();
539 surfDesc.fHeight = backendTex.height();
540 surfDesc.fConfig = backendTex.config();
Brian Salomonbdecacf2018-02-02 20:32:49 -0500541 surfDesc.fSampleCnt = 1;
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400542
Greg Daniel177e6952017-10-12 12:27:11 -0400543 GrMipMapsStatus mipMapsStatus = backendTex.hasMipMaps() ? GrMipMapsStatus::kValid
544 : GrMipMapsStatus::kNotAllocated;
545
546 return GrGLTexture::MakeWrapped(this, surfDesc, mipMapsStatus, idDesc);
Brian Salomond17f6582017-07-19 18:28:58 -0400547}
548
549sk_sp<GrTexture> GrGLGpu::onWrapRenderableBackendTexture(const GrBackendTexture& backendTex,
Brian Salomond17f6582017-07-19 18:28:58 -0400550 int sampleCnt,
551 GrWrapOwnership ownership) {
552 GrGLTexture::IDDesc idDesc;
553 if (!check_backend_texture(backendTex, this->glCaps(), &idDesc)) {
bsalomone5286e02016-01-14 09:24:09 -0800554 return nullptr;
bsalomon7ea33f52015-11-22 14:51:00 -0800555 }
Greg Daniele7d8da42017-12-04 11:23:19 -0500556 if (!idDesc.fInfo.fFormat) {
557 idDesc.fInfo.fFormat = this->glCaps().configSizedInternalFormat(backendTex.config());
558 }
bsalomone5286e02016-01-14 09:24:09 -0800559
Brian Salomond17f6582017-07-19 18:28:58 -0400560 // We don't support rendering to a EXTERNAL texture.
561 if (GR_GL_TEXTURE_EXTERNAL == idDesc.fInfo.fTarget) {
bsalomona98419b2015-11-23 07:09:50 -0800562 return nullptr;
563 }
bsalomon10528f12015-10-14 12:54:52 -0700564
Brian Osman766fcbb2017-03-13 09:33:09 -0400565 if (kBorrow_GrWrapOwnership == ownership) {
Brian Osmana6953f22017-03-10 20:14:05 +0000566 idDesc.fOwnership = GrBackendObjectOwnership::kBorrowed;
Brian Osman766fcbb2017-03-13 09:33:09 -0400567 } else {
568 idDesc.fOwnership = GrBackendObjectOwnership::kOwned;
bsalomone5286e02016-01-14 09:24:09 -0800569 }
bsalomonb15b4c12014-10-29 12:41:57 -0700570
Ben Wagner18b61f92016-10-25 10:44:02 -0400571 GrSurfaceDesc surfDesc;
Brian Salomond17f6582017-07-19 18:28:58 -0400572 surfDesc.fFlags = kRenderTarget_GrSurfaceFlag;
Greg Daniel7ef28f32017-04-20 16:41:55 +0000573 surfDesc.fWidth = backendTex.width();
574 surfDesc.fHeight = backendTex.height();
575 surfDesc.fConfig = backendTex.config();
Brian Salomonbdecacf2018-02-02 20:32:49 -0500576 surfDesc.fSampleCnt = this->caps()->getRenderTargetSampleCount(sampleCnt, backendTex.config());
577 if (surfDesc.fSampleCnt < 1) {
578 return nullptr;
579 }
bsalomon@google.come269f212011-11-07 13:29:52 +0000580
Brian Salomond17f6582017-07-19 18:28:58 -0400581 GrGLRenderTarget::IDDesc rtIDDesc;
582 if (!this->createRenderTargetObjects(surfDesc, idDesc.fInfo, &rtIDDesc)) {
583 return nullptr;
bsalomon@google.come269f212011-11-07 13:29:52 +0000584 }
Greg Daniel177e6952017-10-12 12:27:11 -0400585
586 GrMipMapsStatus mipMapsStatus = backendTex.hasMipMaps() ? GrMipMapsStatus::kDirty
587 : GrMipMapsStatus::kNotAllocated;
588
Brian Salomond17f6582017-07-19 18:28:58 -0400589 sk_sp<GrGLTextureRenderTarget> texRT(
Greg Daniel177e6952017-10-12 12:27:11 -0400590 GrGLTextureRenderTarget::MakeWrapped(this, surfDesc, idDesc, rtIDDesc, mipMapsStatus));
Brian Salomond17f6582017-07-19 18:28:58 -0400591 texRT->baseLevelWasBoundToFBO();
592 return std::move(texRT);
bsalomon@google.come269f212011-11-07 13:29:52 +0000593}
594
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400595sk_sp<GrRenderTarget> GrGLGpu::onWrapBackendRenderTarget(const GrBackendRenderTarget& backendRT) {
Greg Daniel323fbcf2018-04-10 13:46:30 -0400596 GrGLFramebufferInfo info;
597 if (!backendRT.getGLFramebufferInfo(&info)) {
Greg Danielbcf612b2017-05-01 13:50:58 +0000598 return nullptr;
599 }
600
bsalomonb15b4c12014-10-29 12:41:57 -0700601 GrGLRenderTarget::IDDesc idDesc;
Greg Daniel323fbcf2018-04-10 13:46:30 -0400602 idDesc.fRTFBOID = info.fFBOID;
bsalomonb15b4c12014-10-29 12:41:57 -0700603 idDesc.fMSColorRenderbufferID = 0;
egdanield803f272015-03-18 13:01:52 -0700604 idDesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
Brian Osman0b791f52017-03-10 08:30:22 -0500605 idDesc.fRTFBOOwnership = GrBackendObjectOwnership::kBorrowed;
csmartdaltonf9635992016-08-10 11:09:07 -0700606 idDesc.fIsMixedSampled = false;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000607
bsalomonb15b4c12014-10-29 12:41:57 -0700608 GrSurfaceDesc desc;
Brian Salomon0ec981b2017-05-15 13:48:50 -0400609 desc.fFlags = kRenderTarget_GrSurfaceFlag;
Greg Danielbcf612b2017-05-01 13:50:58 +0000610 desc.fWidth = backendRT.width();
611 desc.fHeight = backendRT.height();
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400612 desc.fConfig = backendRT.config();
Brian Salomonbdecacf2018-02-02 20:32:49 -0500613 desc.fSampleCnt =
614 this->caps()->getRenderTargetSampleCount(backendRT.sampleCnt(), backendRT.config());
bsalomonb15b4c12014-10-29 12:41:57 -0700615
Greg Danielbcf612b2017-05-01 13:50:58 +0000616 return GrGLRenderTarget::MakeWrapped(this, desc, idDesc, backendRT.stencilBits());
bsalomon@google.come269f212011-11-07 13:29:52 +0000617}
618
Greg Daniel7ef28f32017-04-20 16:41:55 +0000619sk_sp<GrRenderTarget> GrGLGpu::onWrapBackendTextureAsRenderTarget(const GrBackendTexture& tex,
Greg Daniel7ef28f32017-04-20 16:41:55 +0000620 int sampleCnt) {
Greg Daniel52e16d92018-04-10 09:34:07 -0400621 GrGLTextureInfo info;
622 if (!tex.getGLTextureInfo(&info) || !info.fID) {
ericrkf7b8b8a2016-02-24 14:49:51 -0800623 return nullptr;
624 }
ericrkf7b8b8a2016-02-24 14:49:51 -0800625
Greg Daniel52e16d92018-04-10 09:34:07 -0400626 if (GR_GL_TEXTURE_RECTANGLE != info.fTarget &&
627 GR_GL_TEXTURE_2D != info.fTarget) {
ericrkf7b8b8a2016-02-24 14:49:51 -0800628 // Only texture rectangle and texture 2d are supported. We do not check whether texture
629 // rectangle is supported by Skia - if the caller provided us with a texture rectangle,
630 // we assume the necessary support exists.
631 return nullptr;
632 }
633
Ben Wagner18b61f92016-10-25 10:44:02 -0400634 GrSurfaceDesc surfDesc;
Greg Daniel7ef28f32017-04-20 16:41:55 +0000635 surfDesc.fFlags = kRenderTarget_GrSurfaceFlag;
636 surfDesc.fWidth = tex.width();
637 surfDesc.fHeight = tex.height();
638 surfDesc.fConfig = tex.config();
Brian Salomonbdecacf2018-02-02 20:32:49 -0500639 surfDesc.fSampleCnt = this->caps()->getRenderTargetSampleCount(sampleCnt, tex.config());
ericrkf7b8b8a2016-02-24 14:49:51 -0800640
641 GrGLRenderTarget::IDDesc rtIDDesc;
Greg Daniel52e16d92018-04-10 09:34:07 -0400642 if (!this->createRenderTargetObjects(surfDesc, info, &rtIDDesc)) {
ericrkf7b8b8a2016-02-24 14:49:51 -0800643 return nullptr;
644 }
bungeman6bd52842016-10-27 09:30:08 -0700645 return GrGLRenderTarget::MakeWrapped(this, surfDesc, rtIDDesc, 0);
ericrkf7b8b8a2016-02-24 14:49:51 -0800646}
647
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000648////////////////////////////////////////////////////////////////////////////////
ericrkf7b8b8a2016-02-24 14:49:51 -0800649
Brian Salomonc320b152018-02-20 14:05:36 -0500650bool GrGLGpu::onGetWritePixelsInfo(GrSurface* dstSurface, GrSurfaceOrigin dstOrigin, int width,
651 int height, GrColorType srcColorType,
bsalomonf0674512015-07-28 13:26:15 -0700652 DrawPreference* drawPreference,
653 WritePixelTempDrawInfo* tempDrawInfo) {
Brian Salomonc320b152018-02-20 14:05:36 -0500654 // We don't want to introduce a sRGB conversion if we trigger a draw.
655 auto srcConfigSRGBEncoded = GrPixelConfigIsSRGBEncoded(dstSurface->config());
Brian Salomon9b009bb2018-02-14 13:53:55 -0500656 if (*drawPreference != kNoDraw_DrawPreference) {
657 // We assume the base class has only inserted a draw for sRGB reasons. So the temp surface
658 // has the config of the original src data. There is no swizzling nor src config spoofing.
Brian Salomonc320b152018-02-20 14:05:36 -0500659 SkASSERT(tempDrawInfo->fWriteColorType == srcColorType);
660 SkASSERT(GrPixelConfigToColorType(tempDrawInfo->fTempSurfaceDesc.fConfig) == srcColorType);
Brian Salomon9b009bb2018-02-14 13:53:55 -0500661 SkASSERT(tempDrawInfo->fSwizzle == GrSwizzle::RGBA());
Brian Salomonc320b152018-02-20 14:05:36 -0500662 // Don't undo a sRGB conversion introduced by our caller via an intermediate draw.
663 srcConfigSRGBEncoded = GrPixelConfigIsSRGBEncoded(tempDrawInfo->fTempSurfaceDesc.fConfig);
664 }
665 if (GrColorTypeIsAlphaOnly(srcColorType)) {
666 srcConfigSRGBEncoded = GrSRGBEncoded::kNo;
Brian Salomon9b009bb2018-02-14 13:53:55 -0500667 }
668
Brian Salomon9bada542017-06-12 12:09:30 -0400669 if (SkToBool(dstSurface->asRenderTarget())) {
670 if (this->glCaps().useDrawInsteadOfAllRenderTargetWrites()) {
671 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
672 }
673 }
674
675 GrGLTexture* texture = static_cast<GrGLTexture*>(dstSurface->asTexture());
676
677 if (texture) {
bsalomone5286e02016-01-14 09:24:09 -0800678 if (GR_GL_TEXTURE_EXTERNAL == texture->target()) {
679 // We don't currently support writing pixels to EXTERNAL textures.
bsalomon7ea33f52015-11-22 14:51:00 -0800680 return false;
681 }
Brian Salomon9bada542017-06-12 12:09:30 -0400682 if (GrPixelConfigIsUnorm(texture->config()) && texture->hasBaseLevelBeenBoundToFBO() &&
683 this->glCaps().disallowTexSubImageForUnormConfigTexturesEverBoundToFBO() &&
684 (width < dstSurface->width() || height < dstSurface->height())) {
685 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
686 }
687 } else {
688 // This subclass only allows writes to textures. If the dst is not a texture we have to draw
689 // into it. We could use glDrawPixels on GLs that have it, but we don't today.
690 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
bsalomon6cb3cbe2015-07-30 07:34:27 -0700691 }
692
Brian Osman33910292017-04-18 14:38:53 -0400693 // If the dst is MSAA, we have to draw, or we'll just be writing to the resolve target.
Brian Salomonbdecacf2018-02-02 20:32:49 -0500694 if (dstSurface->asRenderTarget() && dstSurface->asRenderTarget()->numColorSamples() > 1) {
Brian Osman33910292017-04-18 14:38:53 -0400695 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
696 }
697
Brian Salomonc320b152018-02-20 14:05:36 -0500698 auto srcAsConfig = GrColorTypeToPixelConfig(srcColorType, srcConfigSRGBEncoded);
699 SkASSERT(srcAsConfig != kUnknown_GrPixelConfig);
700 auto dstColorType = GrPixelConfigToColorType(dstSurface->config());
701 bool configsAreRBSwaps = GrPixelConfigSwapRAndB(srcAsConfig) == dstSurface->config();
bsalomonf0674512015-07-28 13:26:15 -0700702
703 if (configsAreRBSwaps) {
Brian Salomonc320b152018-02-20 14:05:36 -0500704 if (!this->caps()->isConfigTexturable(srcAsConfig)) {
bsalomonf0674512015-07-28 13:26:15 -0700705 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
706 tempDrawInfo->fTempSurfaceDesc.fConfig = dstSurface->config();
bsalomon6c9cd552016-01-22 07:17:34 -0800707 tempDrawInfo->fSwizzle = GrSwizzle::BGRA();
Brian Salomonc320b152018-02-20 14:05:36 -0500708 tempDrawInfo->fWriteColorType = dstColorType;
bsalomon88c7b982015-07-31 11:20:16 -0700709 } else if (this->glCaps().rgba8888PixelsOpsAreSlow() &&
Brian Salomonc320b152018-02-20 14:05:36 -0500710 kRGBA_8888_GrPixelConfig == srcAsConfig) {
bsalomonf0674512015-07-28 13:26:15 -0700711 ElevateDrawPreference(drawPreference, kGpuPrefersDraw_DrawPreference);
712 tempDrawInfo->fTempSurfaceDesc.fConfig = dstSurface->config();
bsalomon6c9cd552016-01-22 07:17:34 -0800713 tempDrawInfo->fSwizzle = GrSwizzle::BGRA();
Brian Salomonc320b152018-02-20 14:05:36 -0500714 tempDrawInfo->fWriteColorType = dstColorType;
bsalomonf0674512015-07-28 13:26:15 -0700715 } else if (kGLES_GrGLStandard == this->glStandard() &&
716 this->glCaps().bgraIsInternalFormat()) {
717 // The internal format and external formats must match texture uploads so we can't
718 // swizzle while uploading when BGRA is a distinct internal format.
719 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
720 tempDrawInfo->fTempSurfaceDesc.fConfig = dstSurface->config();
bsalomon6c9cd552016-01-22 07:17:34 -0800721 tempDrawInfo->fSwizzle = GrSwizzle::BGRA();
Brian Salomonc320b152018-02-20 14:05:36 -0500722 tempDrawInfo->fWriteColorType = dstColorType;
bsalomonf0674512015-07-28 13:26:15 -0700723 }
724 }
725
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400726 if (!this->glCaps().unpackFlipYSupport() && kBottomLeft_GrSurfaceOrigin == dstOrigin) {
bsalomonf0674512015-07-28 13:26:15 -0700727 ElevateDrawPreference(drawPreference, kGpuPrefersDraw_DrawPreference);
728 }
729
730 return true;
731}
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000732
Brian Salomonc320b152018-02-20 14:05:36 -0500733static bool check_write_and_transfer_input(GrGLTexture* glTex) {
bsalomon6cb3cbe2015-07-30 07:34:27 -0700734 if (!glTex) {
735 return false;
736 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000737
bsalomone5286e02016-01-14 09:24:09 -0800738 // Write or transfer of pixels is not implemented for TEXTURE_EXTERNAL textures
739 if (GR_GL_TEXTURE_EXTERNAL == glTex->target()) {
bsalomon7ea33f52015-11-22 14:51:00 -0800740 return false;
741 }
742
jvanverth17aa0472016-01-05 10:41:27 -0800743 return true;
744}
745
Brian Salomon9b009bb2018-02-14 13:53:55 -0500746bool GrGLGpu::onWritePixels(GrSurface* surface, GrSurfaceOrigin origin, int left, int top,
Brian Salomonc320b152018-02-20 14:05:36 -0500747 int width, int height, GrColorType srcColorType,
Brian Salomon9b009bb2018-02-14 13:53:55 -0500748 const GrMipLevel texels[], int mipLevelCount) {
Brian Salomonc320b152018-02-20 14:05:36 -0500749 auto glTex = static_cast<GrGLTexture*>(surface->asTexture());
jvanverth17aa0472016-01-05 10:41:27 -0800750
Brian Salomonc320b152018-02-20 14:05:36 -0500751 if (!check_write_and_transfer_input(glTex)) {
jvanverth17aa0472016-01-05 10:41:27 -0800752 return false;
753 }
754
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +0000755 this->setScratchTextureUnit();
bsalomon10528f12015-10-14 12:54:52 -0700756 GL_CALL(BindTexture(glTex->target(), glTex->textureID()));
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000757
Brian Salomonc320b152018-02-20 14:05:36 -0500758 // No sRGB transformation occurs in uploadTexData. We choose to make the src config match the
759 // srgb-ness of the surface to avoid issues in ES2 where internal/external formats must match.
760 // When we're on ES2 and the dst is GL_SRGB_ALPHA by making the config be kSRGB_8888 we know
761 // that our caps will choose GL_SRGB_ALPHA as the external format, too. On ES3 or regular GL our
762 // caps knows to make the external format be GL_RGBA.
763 auto srgbEncoded = GrPixelConfigIsSRGBEncoded(surface->config());
764 auto srcAsConfig = GrColorTypeToPixelConfig(srcColorType, srgbEncoded);
Brian Salomon9b009bb2018-02-14 13:53:55 -0500765 return this->uploadTexData(glTex->config(), glTex->width(), glTex->height(), origin,
766 glTex->target(), kWrite_UploadType, left, top, width, height,
Brian Salomonc320b152018-02-20 14:05:36 -0500767 srcAsConfig, texels, mipLevelCount);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000768}
769
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400770// For GL_[UN]PACK_ALIGNMENT. TODO: This really wants to be GrColorType.
bsalomonf46a1242015-12-15 12:37:38 -0800771static inline GrGLint config_alignment(GrPixelConfig config) {
bsalomonf46a1242015-12-15 12:37:38 -0800772 switch (config) {
773 case kAlpha_8_GrPixelConfig:
Greg Danielef59d872017-11-17 16:47:21 -0500774 case kAlpha_8_as_Alpha_GrPixelConfig:
775 case kAlpha_8_as_Red_GrPixelConfig:
Brian Osman986563b2017-01-10 14:20:02 -0500776 case kGray_8_GrPixelConfig:
Greg Daniel7af060a2017-12-05 16:27:11 -0500777 case kGray_8_as_Lum_GrPixelConfig:
778 case kGray_8_as_Red_GrPixelConfig:
bsalomonf46a1242015-12-15 12:37:38 -0800779 return 1;
780 case kRGB_565_GrPixelConfig:
781 case kRGBA_4444_GrPixelConfig:
782 case kAlpha_half_GrPixelConfig:
Greg Danielef59d872017-11-17 16:47:21 -0500783 case kAlpha_half_as_Red_GrPixelConfig:
bsalomonf46a1242015-12-15 12:37:38 -0800784 case kRGBA_half_GrPixelConfig:
785 return 2;
786 case kRGBA_8888_GrPixelConfig:
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400787 case kRGB_888_GrPixelConfig: // We're really talking about GrColorType::kRGB_888x here.
bsalomonf46a1242015-12-15 12:37:38 -0800788 case kBGRA_8888_GrPixelConfig:
789 case kSRGBA_8888_GrPixelConfig:
brianosmana6359362016-03-21 06:55:37 -0700790 case kSBGRA_8888_GrPixelConfig:
Brian Osman10fc6fd2018-03-02 11:01:10 -0500791 case kRGBA_1010102_GrPixelConfig:
bsalomonf46a1242015-12-15 12:37:38 -0800792 case kRGBA_float_GrPixelConfig:
csmartdalton6aa0e112017-02-08 16:14:11 -0500793 case kRG_float_GrPixelConfig:
bsalomonf46a1242015-12-15 12:37:38 -0800794 return 4;
csmartdalton6aa0e112017-02-08 16:14:11 -0500795 case kUnknown_GrPixelConfig:
bsalomonf46a1242015-12-15 12:37:38 -0800796 return 0;
797 }
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400798 SK_ABORT("Invalid pixel config");
csmartdalton6aa0e112017-02-08 16:14:11 -0500799 return 0;
bsalomonf46a1242015-12-15 12:37:38 -0800800}
801
Brian Salomon9b009bb2018-02-14 13:53:55 -0500802bool GrGLGpu::onTransferPixels(GrTexture* texture, int left, int top, int width, int height,
Brian Salomonc320b152018-02-20 14:05:36 -0500803 GrColorType bufferColorType, GrBuffer* transferBuffer, size_t offset,
Brian Salomon9b009bb2018-02-14 13:53:55 -0500804 size_t rowBytes) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400805 GrGLTexture* glTex = static_cast<GrGLTexture*>(texture);
806 GrPixelConfig texConfig = glTex->config();
807 SkASSERT(this->caps()->isConfigTexturable(texConfig));
808
Brian Salomonc320b152018-02-20 14:05:36 -0500809 if (!check_write_and_transfer_input(glTex)) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400810 return false;
811 }
812
813 if (width <= 0 || width > SK_MaxS32 || height <= 0 || height > SK_MaxS32) {
814 return false;
815 }
816
817 this->setScratchTextureUnit();
818 GL_CALL(BindTexture(glTex->target(), glTex->textureID()));
819
820 SkASSERT(!transferBuffer->isMapped());
821 SkASSERT(!transferBuffer->isCPUBacked());
822 const GrGLBuffer* glBuffer = static_cast<const GrGLBuffer*>(transferBuffer);
823 this->bindBuffer(kXferCpuToGpu_GrBufferType, glBuffer);
824
Greg Daniel660cc992017-06-26 14:55:05 -0400825 SkDEBUGCODE(
826 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
827 SkIRect bounds = SkIRect::MakeWH(texture->width(), texture->height());
828 SkASSERT(bounds.contains(subRect));
829 )
830
Brian Salomonc320b152018-02-20 14:05:36 -0500831 int bpp = GrColorTypeBytesPerPixel(bufferColorType);
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400832 const size_t trimRowBytes = width * bpp;
Greg Daniel660cc992017-06-26 14:55:05 -0400833 if (!rowBytes) {
834 rowBytes = trimRowBytes;
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400835 }
Greg Daniel660cc992017-06-26 14:55:05 -0400836 const void* pixels = (void*)offset;
Bruce Dawson71479b72017-07-05 14:30:20 -0700837 if (width < 0 || height < 0) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400838 return false;
839 }
840
841 bool restoreGLRowLength = false;
842 if (trimRowBytes != rowBytes) {
843 // we should have checked for this support already
844 SkASSERT(this->glCaps().unpackRowLengthSupport());
845 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowBytes / bpp));
846 restoreGLRowLength = true;
847 }
848
849 // Internal format comes from the texture desc.
850 GrGLenum internalFormat;
851 // External format and type come from the upload data.
852 GrGLenum externalFormat;
853 GrGLenum externalType;
Brian Salomonc320b152018-02-20 14:05:36 -0500854 auto bufferAsConfig = GrColorTypeToPixelConfig(bufferColorType, GrSRGBEncoded::kNo);
855 if (!this->glCaps().getTexImageFormats(texConfig, bufferAsConfig, &internalFormat,
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400856 &externalFormat, &externalType)) {
857 return false;
858 }
859
860 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, config_alignment(texConfig)));
861 GL_CALL(TexSubImage2D(glTex->target(),
862 0,
863 left, top,
864 width,
865 height,
866 externalFormat, externalType,
867 pixels));
868
869 if (restoreGLRowLength) {
870 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
871 }
872
873 return true;
874}
875
cblume55f2d2d2016-02-26 13:20:48 -0800876/**
877 * Creates storage space for the texture and fills it with texels.
878 *
Brian Salomond1eaf492017-05-18 10:02:08 -0400879 * @param config Pixel config of the texture.
cblume55f2d2d2016-02-26 13:20:48 -0800880 * @param interface The GL interface in use.
cblume790d5132016-02-29 11:13:29 -0800881 * @param caps The capabilities of the GL device.
brianosman81a84852016-09-12 09:05:14 -0700882 * @param internalFormat The data format used for the internal storage of the texture. May be sized.
883 * @param internalFormatForTexStorage The data format used for the TexStorage API. Must be sized.
cblume55f2d2d2016-02-26 13:20:48 -0800884 * @param externalFormat The data format used for the external storage of the texture.
885 * @param externalType The type of the data used for the external storage of the texture.
886 * @param texels The texel data of the texture being created.
887 * @param baseWidth The width of the texture's base mipmap level
888 * @param baseHeight The height of the texture's base mipmap level
cblume55f2d2d2016-02-26 13:20:48 -0800889 */
Robert Phillips92de6312017-05-23 07:43:48 -0400890static bool allocate_and_populate_texture(GrPixelConfig config,
891 const GrGLInterface& interface,
892 const GrGLCaps& caps,
893 GrGLenum target,
894 GrGLenum internalFormat,
895 GrGLenum internalFormatForTexStorage,
896 GrGLenum externalFormat,
897 GrGLenum externalType,
Robert Phillips590533f2017-07-11 14:22:35 -0400898 const GrMipLevel texels[], int mipLevelCount,
Robert Phillips92de6312017-05-23 07:43:48 -0400899 int baseWidth, int baseHeight) {
cblume55f2d2d2016-02-26 13:20:48 -0800900 CLEAR_ERROR_BEFORE_ALLOC(&interface);
cblume790d5132016-02-29 11:13:29 -0800901
Brian Salomond1eaf492017-05-18 10:02:08 -0400902 bool useTexStorage = caps.isConfigTexSupportEnabled(config);
cblume790d5132016-02-29 11:13:29 -0800903 // We can only use TexStorage if we know we will not later change the storage requirements.
904 // This means if we may later want to add mipmaps, we cannot use TexStorage.
905 // Right now, we cannot know if we will later add mipmaps or not.
906 // The only time we can use TexStorage is when we already have the
Brian Salomon57111332018-02-05 15:55:54 -0500907 // mipmaps.
908 useTexStorage &= mipLevelCount > 1;
cblume790d5132016-02-29 11:13:29 -0800909
910 if (useTexStorage) {
911 // We never resize or change formats of textures.
cblume55f2d2d2016-02-26 13:20:48 -0800912 GL_ALLOC_CALL(&interface,
Robert Phillips590533f2017-07-11 14:22:35 -0400913 TexStorage2D(target, SkTMax(mipLevelCount, 1), internalFormatForTexStorage,
Brian Salomond1eaf492017-05-18 10:02:08 -0400914 baseWidth, baseHeight));
Brian Salomon0ec981b2017-05-15 13:48:50 -0400915 GrGLenum error = CHECK_ALLOC_ERROR(&interface);
cblume55f2d2d2016-02-26 13:20:48 -0800916 if (error != GR_GL_NO_ERROR) {
bsalomone699d0c2016-03-09 06:25:15 -0800917 return false;
cblume790d5132016-02-29 11:13:29 -0800918 } else {
Robert Phillips590533f2017-07-11 14:22:35 -0400919 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
cblume790d5132016-02-29 11:13:29 -0800920 const void* currentMipData = texels[currentMipLevel].fPixels;
921 if (currentMipData == nullptr) {
922 continue;
923 }
924 int twoToTheMipLevel = 1 << currentMipLevel;
Brian Salomond1eaf492017-05-18 10:02:08 -0400925 int currentWidth = SkTMax(1, baseWidth / twoToTheMipLevel);
926 int currentHeight = SkTMax(1, baseHeight / twoToTheMipLevel);
cblume790d5132016-02-29 11:13:29 -0800927
928 GR_GL_CALL(&interface,
929 TexSubImage2D(target,
930 currentMipLevel,
931 0, // left
932 0, // top
933 currentWidth,
934 currentHeight,
935 externalFormat, externalType,
936 currentMipData));
937 }
bsalomone699d0c2016-03-09 06:25:15 -0800938 return true;
cblume790d5132016-02-29 11:13:29 -0800939 }
940 } else {
Robert Phillips590533f2017-07-11 14:22:35 -0400941 if (!mipLevelCount) {
cblume790d5132016-02-29 11:13:29 -0800942 GL_ALLOC_CALL(&interface,
943 TexImage2D(target,
bsalomone699d0c2016-03-09 06:25:15 -0800944 0,
cblume790d5132016-02-29 11:13:29 -0800945 internalFormat,
bsalomone699d0c2016-03-09 06:25:15 -0800946 baseWidth,
947 baseHeight,
cblume790d5132016-02-29 11:13:29 -0800948 0, // border
949 externalFormat, externalType,
bsalomone699d0c2016-03-09 06:25:15 -0800950 nullptr));
Brian Salomon0ec981b2017-05-15 13:48:50 -0400951 GrGLenum error = CHECK_ALLOC_ERROR(&interface);
cblume790d5132016-02-29 11:13:29 -0800952 if (error != GR_GL_NO_ERROR) {
bsalomone699d0c2016-03-09 06:25:15 -0800953 return false;
954 }
955 } else {
Robert Phillips590533f2017-07-11 14:22:35 -0400956 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
bsalomone699d0c2016-03-09 06:25:15 -0800957 int twoToTheMipLevel = 1 << currentMipLevel;
958 int currentWidth = SkTMax(1, baseWidth / twoToTheMipLevel);
959 int currentHeight = SkTMax(1, baseHeight / twoToTheMipLevel);
Greg Daniel8b059bd2017-09-28 20:46:45 +0000960 const void* currentMipData = texels[currentMipLevel].fPixels;
bsalomone699d0c2016-03-09 06:25:15 -0800961 // Even if curremtMipData is nullptr, continue to call TexImage2D.
962 // This will allocate texture memory which we can later populate.
963 GL_ALLOC_CALL(&interface,
964 TexImage2D(target,
965 currentMipLevel,
966 internalFormat,
967 currentWidth,
968 currentHeight,
969 0, // border
970 externalFormat, externalType,
971 currentMipData));
Brian Salomon0ec981b2017-05-15 13:48:50 -0400972 GrGLenum error = CHECK_ALLOC_ERROR(&interface);
bsalomone699d0c2016-03-09 06:25:15 -0800973 if (error != GR_GL_NO_ERROR) {
974 return false;
975 }
cblume790d5132016-02-29 11:13:29 -0800976 }
cblume55f2d2d2016-02-26 13:20:48 -0800977 }
978 }
bsalomone699d0c2016-03-09 06:25:15 -0800979 return true;
cblume55f2d2d2016-02-26 13:20:48 -0800980}
981
982/**
cblume55f2d2d2016-02-26 13:20:48 -0800983 * After a texture is created, any state which was altered during its creation
984 * needs to be restored.
985 *
986 * @param interface The GL interface to use.
987 * @param caps The capabilities of the GL device.
988 * @param restoreGLRowLength Should the row length unpacking be restored?
989 * @param glFlipY Did GL flip the texture vertically?
990 */
991static void restore_pixelstore_state(const GrGLInterface& interface, const GrGLCaps& caps,
992 bool restoreGLRowLength, bool glFlipY) {
993 if (restoreGLRowLength) {
994 SkASSERT(caps.unpackRowLengthSupport());
995 GR_GL_CALL(&interface, PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
996 }
997 if (glFlipY) {
998 GR_GL_CALL(&interface, PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
999 }
1000}
1001
Brian Osman9b560242017-09-05 15:34:52 -04001002void GrGLGpu::unbindCpuToGpuXferBuffer() {
1003 auto& xferBufferState = fHWBufferState[kXferCpuToGpu_GrBufferType];
1004 if (!xferBufferState.fBoundBufferUniqueID.isInvalid()) {
1005 GL_CALL(BindBuffer(xferBufferState.fGLTarget, 0));
1006 xferBufferState.invalidate();
1007 }
1008
1009}
1010
Brian Salomonc320b152018-02-20 14:05:36 -05001011// TODO: Make this take a GrColorType instead of dataConfig. This requires updating GrGLCaps to
1012// convert from GrColorType to externalFormat/externalType GLenum values.
Brian Salomond1eaf492017-05-18 10:02:08 -04001013bool GrGLGpu::uploadTexData(GrPixelConfig texConfig, int texWidth, int texHeight,
1014 GrSurfaceOrigin texOrigin, GrGLenum target, UploadType uploadType,
1015 int left, int top, int width, int height, GrPixelConfig dataConfig,
Greg Daniel834f1202017-10-09 15:06:20 -04001016 const GrMipLevel texels[], int mipLevelCount,
Greg Daniel0fc4d2d2017-10-12 11:23:36 -04001017 GrMipMapsStatus* mipMapsStatus) {
Brian Salomond1eaf492017-05-18 10:02:08 -04001018 SkASSERT(this->caps()->isConfigTexturable(texConfig));
Greg Daniel660cc992017-06-26 14:55:05 -04001019 SkDEBUGCODE(
1020 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
1021 SkIRect bounds = SkIRect::MakeWH(texWidth, texHeight);
1022 SkASSERT(bounds.contains(subRect));
1023 )
Robert Phillips590533f2017-07-11 14:22:35 -04001024 SkASSERT(1 == mipLevelCount ||
Greg Daniel660cc992017-06-26 14:55:05 -04001025 (0 == left && 0 == top && width == texWidth && height == texHeight));
bsalomon5b30c6f2015-12-17 14:17:34 -08001026
Brian Osman9b560242017-09-05 15:34:52 -04001027 this->unbindCpuToGpuXferBuffer();
Jim Van Verth2e5eaf02017-06-21 15:55:46 -04001028
cblume55f2d2d2016-02-26 13:20:48 -08001029 // texels is const.
1030 // But we may need to flip the texture vertically to prepare it.
1031 // Rather than flip in place and alter the incoming data,
1032 // we allocate a new buffer to flip into.
1033 // This means we need to make a non-const shallow copy of texels.
Robert Phillips0f992772017-07-12 08:24:56 -04001034 SkAutoTMalloc<GrMipLevel> texelsShallowCopy;
1035
1036 if (mipLevelCount) {
1037 texelsShallowCopy.reset(mipLevelCount);
1038 memcpy(texelsShallowCopy.get(), texels, mipLevelCount*sizeof(GrMipLevel));
1039 }
cblume55f2d2d2016-02-26 13:20:48 -08001040
cblume55f2d2d2016-02-26 13:20:48 -08001041 const GrGLInterface* interface = this->glInterface();
1042 const GrGLCaps& caps = this->glCaps();
1043
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001044 size_t bpp = GrBytesPerPixel(dataConfig);
cblume55f2d2d2016-02-26 13:20:48 -08001045
1046 if (width == 0 || height == 0) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +00001047 return false;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001048 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00001049
bsalomon5b30c6f2015-12-17 14:17:34 -08001050 // Internal format comes from the texture desc.
bsalomon76148af2016-01-12 11:13:47 -08001051 GrGLenum internalFormat;
bsalomon5b30c6f2015-12-17 14:17:34 -08001052 // External format and type come from the upload data.
bsalomon76148af2016-01-12 11:13:47 -08001053 GrGLenum externalFormat;
1054 GrGLenum externalType;
Brian Salomond1eaf492017-05-18 10:02:08 -04001055 if (!this->glCaps().getTexImageFormats(texConfig, dataConfig, &internalFormat, &externalFormat,
1056 &externalType)) {
bsalomon76148af2016-01-12 11:13:47 -08001057 return false;
1058 }
brianosman81a84852016-09-12 09:05:14 -07001059 // TexStorage requires a sized format, and internalFormat may or may not be
Brian Salomond1eaf492017-05-18 10:02:08 -04001060 GrGLenum internalFormatForTexStorage = this->glCaps().configSizedInternalFormat(texConfig);
brianosman81a84852016-09-12 09:05:14 -07001061
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001062 /*
bsalomon5b30c6f2015-12-17 14:17:34 -08001063 * Check whether to allocate a temporary buffer for flipping y or
bsalomon@google.com6f379512011-11-16 20:36:03 +00001064 * because our srcData has extra bytes past each row. If so, we need
1065 * to trim those off here, since GL ES may not let us specify
1066 * GL_UNPACK_ROW_LENGTH.
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001067 */
bsalomon@google.com6f379512011-11-16 20:36:03 +00001068 bool restoreGLRowLength = false;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +00001069 bool swFlipY = false;
1070 bool glFlipY = false;
cblume55f2d2d2016-02-26 13:20:48 -08001071
Robert Phillips590533f2017-07-11 14:22:35 -04001072 if (kBottomLeft_GrSurfaceOrigin == texOrigin && mipLevelCount) {
cblume55f2d2d2016-02-26 13:20:48 -08001073 if (caps.unpackFlipYSupport()) {
1074 glFlipY = true;
1075 } else {
1076 swFlipY = true;
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001077 }
cblume55f2d2d2016-02-26 13:20:48 -08001078 }
1079
1080 // in case we need a temporary, trimmed copy of the src pixels
1081 SkAutoSMalloc<128 * 128> tempStorage;
1082
Greg Daniel0fc4d2d2017-10-12 11:23:36 -04001083 if (mipMapsStatus) {
1084 *mipMapsStatus = GrMipMapsStatus::kValid;
Greg Daniel834f1202017-10-09 15:06:20 -04001085 }
1086
Robert Phillips931f7512017-11-02 12:57:18 -04001087 const bool usesMips = mipLevelCount > 1;
1088
cblume55f2d2d2016-02-26 13:20:48 -08001089 // find the combined size of all the mip levels and the relative offset of
1090 // each into the collective buffer
Robert Phillips931f7512017-11-02 12:57:18 -04001091 bool willNeedData = false;
Greg Daniel55afd6d2017-09-29 09:32:44 -04001092 size_t combinedBufferSize = 0;
1093 SkTArray<size_t> individualMipOffsets(mipLevelCount);
Robert Phillips590533f2017-07-11 14:22:35 -04001094 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
Greg Daniel55afd6d2017-09-29 09:32:44 -04001095 if (texelsShallowCopy[currentMipLevel].fPixels) {
1096 int twoToTheMipLevel = 1 << currentMipLevel;
1097 int currentWidth = SkTMax(1, width / twoToTheMipLevel);
1098 int currentHeight = SkTMax(1, height / twoToTheMipLevel);
Robert Phillips931f7512017-11-02 12:57:18 -04001099 const size_t trimRowBytes = currentWidth * bpp;
1100 const size_t trimmedSize = trimRowBytes * currentHeight;
1101
1102 const size_t rowBytes = texelsShallowCopy[currentMipLevel].fRowBytes
1103 ? texelsShallowCopy[currentMipLevel].fRowBytes
1104 : trimRowBytes;
1105
1106
1107 if (((!caps.unpackRowLengthSupport() || usesMips) && trimRowBytes != rowBytes) ||
1108 swFlipY) {
1109 willNeedData = true;
1110 }
1111
Greg Daniel55afd6d2017-09-29 09:32:44 -04001112 individualMipOffsets.push_back(combinedBufferSize);
1113 combinedBufferSize += trimmedSize;
1114 } else {
Greg Daniel0fc4d2d2017-10-12 11:23:36 -04001115 if (mipMapsStatus) {
1116 *mipMapsStatus = GrMipMapsStatus::kDirty;
Greg Daniel834f1202017-10-09 15:06:20 -04001117 }
Greg Daniel55afd6d2017-09-29 09:32:44 -04001118 individualMipOffsets.push_back(0);
1119 }
Greg Daniel0fc4d2d2017-10-12 11:23:36 -04001120 }
1121 if (mipMapsStatus && mipLevelCount <= 1) {
1122 *mipMapsStatus = GrMipMapsStatus::kNotAllocated;
cblume55f2d2d2016-02-26 13:20:48 -08001123 }
Robert Phillips931f7512017-11-02 12:57:18 -04001124 char* buffer = nullptr;
1125 if (willNeedData) {
1126 buffer = (char*)tempStorage.reset(combinedBufferSize);
1127 }
cblume55f2d2d2016-02-26 13:20:48 -08001128
Robert Phillips590533f2017-07-11 14:22:35 -04001129 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
Greg Daniel55afd6d2017-09-29 09:32:44 -04001130 if (!texelsShallowCopy[currentMipLevel].fPixels) {
1131 continue;
1132 }
cblume55f2d2d2016-02-26 13:20:48 -08001133 int twoToTheMipLevel = 1 << currentMipLevel;
1134 int currentWidth = SkTMax(1, width / twoToTheMipLevel);
1135 int currentHeight = SkTMax(1, height / twoToTheMipLevel);
1136 const size_t trimRowBytes = currentWidth * bpp;
1137
1138 /*
1139 * check whether to allocate a temporary buffer for flipping y or
1140 * because our srcData has extra bytes past each row. If so, we need
1141 * to trim those off here, since GL ES may not let us specify
1142 * GL_UNPACK_ROW_LENGTH.
1143 */
1144 restoreGLRowLength = false;
1145
Greg Daniel55afd6d2017-09-29 09:32:44 -04001146 const size_t rowBytes = texelsShallowCopy[currentMipLevel].fRowBytes
1147 ? texelsShallowCopy[currentMipLevel].fRowBytes
1148 : trimRowBytes;
Greg Daniel660cc992017-06-26 14:55:05 -04001149
ericrk154349b2016-05-10 14:36:53 -07001150 // TODO: This optimization should be enabled with or without mips.
1151 // For use with mips, we must set GR_GL_UNPACK_ROW_LENGTH once per
1152 // mip level, before calling glTexImage2D.
ericrk154349b2016-05-10 14:36:53 -07001153 if (caps.unpackRowLengthSupport() && !swFlipY && !usesMips) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001154 // can't use this for flipping, only non-neg values allowed. :(
1155 if (rowBytes != trimRowBytes) {
1156 GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
cblume55f2d2d2016-02-26 13:20:48 -08001157 GR_GL_CALL(interface, PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength));
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001158 restoreGLRowLength = true;
1159 }
Jim Van Verth2e5eaf02017-06-21 15:55:46 -04001160 } else if (trimRowBytes != rowBytes || swFlipY) {
1161 // copy data into our new storage, skipping the trailing bytes
1162 const char* src = (const char*)texelsShallowCopy[currentMipLevel].fPixels;
1163 if (swFlipY && currentHeight >= 1) {
1164 src += (currentHeight - 1) * rowBytes;
Jim Van Verthf7037d02017-06-21 10:52:43 -04001165 }
Greg Daniel55afd6d2017-09-29 09:32:44 -04001166 char* dst = buffer + individualMipOffsets[currentMipLevel];
Jim Van Verth2e5eaf02017-06-21 15:55:46 -04001167 for (int y = 0; y < currentHeight; y++) {
1168 memcpy(dst, src, trimRowBytes);
1169 if (swFlipY) {
1170 src -= rowBytes;
1171 } else {
1172 src += rowBytes;
1173 }
1174 dst += trimRowBytes;
1175 }
1176 // now point data to our copied version
1177 texelsShallowCopy[currentMipLevel].fPixels = buffer +
Greg Daniel55afd6d2017-09-29 09:32:44 -04001178 individualMipOffsets[currentMipLevel];
Jim Van Verth2e5eaf02017-06-21 15:55:46 -04001179 texelsShallowCopy[currentMipLevel].fRowBytes = trimRowBytes;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001180 }
bsalomone699d0c2016-03-09 06:25:15 -08001181 }
1182
Robert Phillips590533f2017-07-11 14:22:35 -04001183 if (mipLevelCount) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001184 if (glFlipY) {
cblume55f2d2d2016-02-26 13:20:48 -08001185 GR_GL_CALL(interface, PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_TRUE));
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001186 }
Brian Salomond1eaf492017-05-18 10:02:08 -04001187 GR_GL_CALL(interface, PixelStorei(GR_GL_UNPACK_ALIGNMENT, config_alignment(texConfig)));
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001188 }
cblume55f2d2d2016-02-26 13:20:48 -08001189
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001190 bool succeeded = true;
Brian Salomond1eaf492017-05-18 10:02:08 -04001191 if (kNewTexture_UploadType == uploadType) {
1192 if (0 == left && 0 == top && texWidth == width && texHeight == height) {
Robert Phillips92de6312017-05-23 07:43:48 -04001193 succeeded = allocate_and_populate_texture(
Brian Salomond1eaf492017-05-18 10:02:08 -04001194 texConfig, *interface, caps, target, internalFormat,
Robert Phillips590533f2017-07-11 14:22:35 -04001195 internalFormatForTexStorage, externalFormat, externalType,
1196 texelsShallowCopy, mipLevelCount, width, height);
Brian Salomond1eaf492017-05-18 10:02:08 -04001197 } else {
1198 succeeded = false;
1199 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001200 } else {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +00001201 if (swFlipY || glFlipY) {
Brian Salomond1eaf492017-05-18 10:02:08 -04001202 top = texHeight - (top + height);
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001203 }
Robert Phillips590533f2017-07-11 14:22:35 -04001204 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
Greg Daniel55afd6d2017-09-29 09:32:44 -04001205 if (!texelsShallowCopy[currentMipLevel].fPixels) {
1206 continue;
1207 }
cblume55f2d2d2016-02-26 13:20:48 -08001208 int twoToTheMipLevel = 1 << currentMipLevel;
1209 int currentWidth = SkTMax(1, width / twoToTheMipLevel);
1210 int currentHeight = SkTMax(1, height / twoToTheMipLevel);
cblume55f2d2d2016-02-26 13:20:48 -08001211
1212 GL_CALL(TexSubImage2D(target,
1213 currentMipLevel,
1214 left, top,
1215 currentWidth,
1216 currentHeight,
1217 externalFormat, externalType,
1218 texelsShallowCopy[currentMipLevel].fPixels));
1219 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00001220 }
1221
cblume55f2d2d2016-02-26 13:20:48 -08001222 restore_pixelstore_state(*interface, caps, restoreGLRowLength, glFlipY);
1223
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001224 return succeeded;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001225}
1226
bsalomon424cc262015-05-22 10:37:30 -07001227static bool renderbuffer_storage_msaa(const GrGLContext& ctx,
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001228 int sampleCount,
1229 GrGLenum format,
1230 int width, int height) {
robertphillips@google.com6177e692013-02-28 20:16:25 +00001231 CLEAR_ERROR_BEFORE_ALLOC(ctx.interface());
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +00001232 SkASSERT(GrGLCaps::kNone_MSFBOType != ctx.caps()->msFBOType());
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +00001233 switch (ctx.caps()->msFBOType()) {
Brian Salomon00731b42016-10-14 11:30:51 -04001234 case GrGLCaps::kStandard_MSFBOType:
vbuzinovdded6962015-06-12 08:59:45 -07001235 case GrGLCaps::kMixedSamples_MSFBOType:
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001236 GL_ALLOC_CALL(ctx.interface(),
1237 RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
1238 sampleCount,
1239 format,
1240 width, height));
1241 break;
1242 case GrGLCaps::kES_Apple_MSFBOType:
1243 GL_ALLOC_CALL(ctx.interface(),
1244 RenderbufferStorageMultisampleES2APPLE(GR_GL_RENDERBUFFER,
1245 sampleCount,
1246 format,
1247 width, height));
1248 break;
1249 case GrGLCaps::kES_EXT_MsToTexture_MSFBOType:
1250 case GrGLCaps::kES_IMG_MsToTexture_MSFBOType:
1251 GL_ALLOC_CALL(ctx.interface(),
1252 RenderbufferStorageMultisampleES2EXT(GR_GL_RENDERBUFFER,
1253 sampleCount,
1254 format,
1255 width, height));
1256 break;
1257 case GrGLCaps::kNone_MSFBOType:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -04001258 SK_ABORT("Shouldn't be here if we don't support multisampled renderbuffers.");
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001259 break;
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001260 }
Brian Salomon9251d4e2015-03-17 16:03:19 -04001261 return (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctx.interface()));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001262}
1263
egdanielb0e1be22015-04-22 13:27:39 -07001264bool GrGLGpu::createRenderTargetObjects(const GrSurfaceDesc& desc,
bsalomon091f60c2015-11-10 11:54:56 -08001265 const GrGLTextureInfo& texInfo,
bsalomonb15b4c12014-10-29 12:41:57 -07001266 GrGLRenderTarget::IDDesc* idDesc) {
1267 idDesc->fMSColorRenderbufferID = 0;
egdanield803f272015-03-18 13:01:52 -07001268 idDesc->fRTFBOID = 0;
kkinnunen2e6055b2016-04-22 01:48:29 -07001269 idDesc->fRTFBOOwnership = GrBackendObjectOwnership::kOwned;
egdanield803f272015-03-18 13:01:52 -07001270 idDesc->fTexFBOID = 0;
robertphillips76948d42016-05-04 12:47:41 -07001271 SkASSERT((GrGLCaps::kMixedSamples_MSFBOType == this->glCaps().msFBOType()) ==
1272 this->caps()->usesMixedSamples());
Brian Salomonbdecacf2018-02-02 20:32:49 -05001273 idDesc->fIsMixedSampled = desc.fSampleCnt > 1 && this->caps()->usesMixedSamples();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001274
1275 GrGLenum status;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001276
bsalomona11e5fc2015-12-18 07:59:41 -08001277 GrGLenum colorRenderbufferFormat = 0; // suppress warning
bsalomon@google.comab15d612011-08-09 12:57:56 +00001278
Brian Salomonbdecacf2018-02-02 20:32:49 -05001279 if (desc.fSampleCnt > 1 && GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.com347c3822013-05-01 20:10:01 +00001280 goto FAILED;
1281 }
1282
egdanield803f272015-03-18 13:01:52 -07001283 GL_CALL(GenFramebuffers(1, &idDesc->fTexFBOID));
1284 if (!idDesc->fTexFBOID) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001285 goto FAILED;
1286 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001287
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001288 // If we are using multisampling we will create two FBOS. We render to one and then resolve to
1289 // the texture bound to the other. The exception is the IMG multisample extension. With this
1290 // extension the texture is multisampled when rendered to and then auto-resolves it when it is
1291 // rendered from.
Brian Salomonbdecacf2018-02-02 20:32:49 -05001292 if (desc.fSampleCnt > 1 && this->glCaps().usesMSAARenderBuffers()) {
egdanield803f272015-03-18 13:01:52 -07001293 GL_CALL(GenFramebuffers(1, &idDesc->fRTFBOID));
bsalomonb15b4c12014-10-29 12:41:57 -07001294 GL_CALL(GenRenderbuffers(1, &idDesc->fMSColorRenderbufferID));
egdanield803f272015-03-18 13:01:52 -07001295 if (!idDesc->fRTFBOID ||
bsalomona11e5fc2015-12-18 07:59:41 -08001296 !idDesc->fMSColorRenderbufferID) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001297 goto FAILED;
1298 }
bsalomon76148af2016-01-12 11:13:47 -08001299 if (!this->glCaps().getRenderbufferFormat(desc.fConfig, &colorRenderbufferFormat)) {
1300 return false;
1301 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001302 } else {
egdanield803f272015-03-18 13:01:52 -07001303 idDesc->fRTFBOID = idDesc->fTexFBOID;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001304 }
1305
egdanield803f272015-03-18 13:01:52 -07001306 // below here we may bind the FBO
Robert Phillips294870f2016-11-11 12:38:40 -05001307 fHWBoundRenderTargetUniqueID.makeInvalid();
egdanield803f272015-03-18 13:01:52 -07001308 if (idDesc->fRTFBOID != idDesc->fTexFBOID) {
Brian Salomonbdecacf2018-02-02 20:32:49 -05001309 SkASSERT(desc.fSampleCnt > 1);
bsalomonb15b4c12014-10-29 12:41:57 -07001310 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, idDesc->fMSColorRenderbufferID));
bsalomon424cc262015-05-22 10:37:30 -07001311 if (!renderbuffer_storage_msaa(*fGLContext,
bsalomonb15b4c12014-10-29 12:41:57 -07001312 desc.fSampleCnt,
bsalomona11e5fc2015-12-18 07:59:41 -08001313 colorRenderbufferFormat,
bsalomonb15b4c12014-10-29 12:41:57 -07001314 desc.fWidth, desc.fHeight)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001315 goto FAILED;
1316 }
egdanield803f272015-03-18 13:01:52 -07001317 fStats.incRenderTargetBinds();
1318 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, idDesc->fRTFBOID));
1319 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon10528f12015-10-14 12:54:52 -07001320 GR_GL_COLOR_ATTACHMENT0,
1321 GR_GL_RENDERBUFFER,
1322 idDesc->fMSColorRenderbufferID));
Brian Salomon0ec981b2017-05-15 13:48:50 -04001323 if (!this->glCaps().isConfigVerifiedColorAttachment(desc.fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001324 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1325 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
1326 goto FAILED;
1327 }
bsalomon424cc262015-05-22 10:37:30 -07001328 fGLContext->caps()->markConfigAsValidColorAttachment(desc.fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001329 }
1330 }
egdanield803f272015-03-18 13:01:52 -07001331 fStats.incRenderTargetBinds();
1332 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, idDesc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001333
Brian Salomonbdecacf2018-02-02 20:32:49 -05001334 if (this->glCaps().usesImplicitMSAAResolve() && desc.fSampleCnt > 1) {
egdanield803f272015-03-18 13:01:52 -07001335 GL_CALL(FramebufferTexture2DMultisample(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001336 GR_GL_COLOR_ATTACHMENT0,
bsalomon091f60c2015-11-10 11:54:56 -08001337 texInfo.fTarget,
1338 texInfo.fID, 0, desc.fSampleCnt));
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001339 } else {
egdanield803f272015-03-18 13:01:52 -07001340 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001341 GR_GL_COLOR_ATTACHMENT0,
bsalomon091f60c2015-11-10 11:54:56 -08001342 texInfo.fTarget,
1343 texInfo.fID, 0));
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001344 }
Brian Salomon0ec981b2017-05-15 13:48:50 -04001345 if (!this->glCaps().isConfigVerifiedColorAttachment(desc.fConfig)) {
egdanield803f272015-03-18 13:01:52 -07001346 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001347 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
1348 goto FAILED;
1349 }
bsalomon424cc262015-05-22 10:37:30 -07001350 fGLContext->caps()->markConfigAsValidColorAttachment(desc.fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001351 }
1352
1353 return true;
1354
1355FAILED:
bsalomonb15b4c12014-10-29 12:41:57 -07001356 if (idDesc->fMSColorRenderbufferID) {
1357 GL_CALL(DeleteRenderbuffers(1, &idDesc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001358 }
egdanield803f272015-03-18 13:01:52 -07001359 if (idDesc->fRTFBOID != idDesc->fTexFBOID) {
1360 GL_CALL(DeleteFramebuffers(1, &idDesc->fRTFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001361 }
egdanield803f272015-03-18 13:01:52 -07001362 if (idDesc->fTexFBOID) {
1363 GL_CALL(DeleteFramebuffers(1, &idDesc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001364 }
1365 return false;
1366}
1367
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001368// good to set a break-point here to know when createTexture fails
Robert Phillips67d52cf2017-06-05 13:38:13 -04001369static sk_sp<GrTexture> return_null_texture() {
mtklein@google.com330313a2013-08-22 15:37:26 +00001370// SkDEBUGFAIL("null texture");
halcanary96fcdcc2015-08-27 07:41:13 -07001371 return nullptr;
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001372}
1373
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +00001374#if 0 && defined(SK_DEBUG)
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001375static size_t as_size_t(int x) {
1376 return x;
1377}
1378#endif
1379
cblume55f2d2d2016-02-26 13:20:48 -08001380static void set_initial_texture_params(const GrGLInterface* interface,
1381 const GrGLTextureInfo& info,
1382 GrGLTexture::TexParams* initialTexParams) {
1383 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
1384 // drivers have a bug where an FBO won't be complete if it includes a
1385 // texture that is not mipmap complete (considering the filter in use).
1386 // we only set a subset here so invalidate first
1387 initialTexParams->invalidate();
1388 initialTexParams->fMinFilter = GR_GL_NEAREST;
1389 initialTexParams->fMagFilter = GR_GL_NEAREST;
1390 initialTexParams->fWrapS = GR_GL_CLAMP_TO_EDGE;
1391 initialTexParams->fWrapT = GR_GL_CLAMP_TO_EDGE;
1392 GR_GL_CALL(interface, TexParameteri(info.fTarget,
1393 GR_GL_TEXTURE_MAG_FILTER,
1394 initialTexParams->fMagFilter));
1395 GR_GL_CALL(interface, TexParameteri(info.fTarget,
1396 GR_GL_TEXTURE_MIN_FILTER,
1397 initialTexParams->fMinFilter));
1398 GR_GL_CALL(interface, TexParameteri(info.fTarget,
1399 GR_GL_TEXTURE_WRAP_S,
1400 initialTexParams->fWrapS));
1401 GR_GL_CALL(interface, TexParameteri(info.fTarget,
1402 GR_GL_TEXTURE_WRAP_T,
1403 initialTexParams->fWrapT));
1404}
1405
Robert Phillips67d52cf2017-06-05 13:38:13 -04001406sk_sp<GrTexture> GrGLGpu::onCreateTexture(const GrSurfaceDesc& desc,
1407 SkBudgeted budgeted,
Robert Phillips590533f2017-07-11 14:22:35 -04001408 const GrMipLevel texels[],
1409 int mipLevelCount) {
bsalomon@google.com8a70eef2013-03-19 13:58:55 +00001410 // We fail if the MSAA was requested and is not available.
Brian Salomonbdecacf2018-02-02 20:32:49 -05001411 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType() && desc.fSampleCnt > 1) {
tfarina38406c82014-10-31 07:11:12 -07001412 //SkDebugf("MSAA RT requested but not supported on this platform.");
bsalomon@google.com8a70eef2013-03-19 13:58:55 +00001413 return return_null_texture();
1414 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001415
Brian Salomond17b4a62017-05-23 16:53:47 -04001416 bool performClear = (desc.fFlags & kPerformInitialClear_GrSurfaceFlag);
Brian Salomond17b4a62017-05-23 16:53:47 -04001417
Robert Phillips590533f2017-07-11 14:22:35 -04001418 GrMipLevel zeroLevel;
Brian Salomond17b4a62017-05-23 16:53:47 -04001419 std::unique_ptr<uint8_t[]> zeros;
Brian Salomond17b4a62017-05-23 16:53:47 -04001420 if (performClear && !this->glCaps().clearTextureSupport() &&
Brian Salomon57111332018-02-05 15:55:54 -05001421 !this->glCaps().canConfigBeFBOColorAttachment(desc.fConfig)) {
Brian Salomond17b4a62017-05-23 16:53:47 -04001422 size_t rowSize = GrBytesPerPixel(desc.fConfig) * desc.fWidth;
1423 size_t size = rowSize * desc.fHeight;
1424 zeros.reset(new uint8_t[size]);
1425 memset(zeros.get(), 0, size);
Robert Phillips590533f2017-07-11 14:22:35 -04001426 zeroLevel.fPixels = zeros.get();
1427 zeroLevel.fRowBytes = 0;
1428 texels = &zeroLevel;
1429 mipLevelCount = 1;
Brian Salomond17b4a62017-05-23 16:53:47 -04001430 performClear = false;
1431 }
1432
Brian Salomond34edf32017-05-19 15:45:48 -04001433 bool isRenderTarget = SkToBool(desc.fFlags & kRenderTarget_GrSurfaceFlag);
reed@google.comac10a2d2010-12-22 21:39:39 +00001434
bsalomonb15b4c12014-10-29 12:41:57 -07001435 GrGLTexture::IDDesc idDesc;
kkinnunen2e6055b2016-04-22 01:48:29 -07001436 idDesc.fOwnership = GrBackendObjectOwnership::kOwned;
Greg Daniel0fc4d2d2017-10-12 11:23:36 -04001437 GrMipMapsStatus mipMapsStatus;
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001438 GrGLTexture::TexParams initialTexParams;
Brian Salomon58389b92018-03-07 13:01:25 -05001439 if (!this->createTextureImpl(desc, &idDesc.fInfo, isRenderTarget, &initialTexParams, texels,
1440 mipLevelCount, &mipMapsStatus)) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001441 return return_null_texture();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001442 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001443
Robert Phillips67d52cf2017-06-05 13:38:13 -04001444 sk_sp<GrGLTexture> tex;
Brian Salomond34edf32017-05-19 15:45:48 -04001445 if (isRenderTarget) {
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001446 // unbind the texture from the texture unit before binding it to the frame buffer
bsalomon091f60c2015-11-10 11:54:56 -08001447 GL_CALL(BindTexture(idDesc.fInfo.fTarget, 0));
bsalomon5236cf42015-01-14 10:42:08 -08001448 GrGLRenderTarget::IDDesc rtIDDesc;
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001449
kkinnunen2e6055b2016-04-22 01:48:29 -07001450 if (!this->createRenderTargetObjects(desc, idDesc.fInfo, &rtIDDesc)) {
bsalomon091f60c2015-11-10 11:54:56 -08001451 GL_CALL(DeleteTextures(1, &idDesc.fInfo.fID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001452 return return_null_texture();
1453 }
Robert Phillips67d52cf2017-06-05 13:38:13 -04001454 tex = sk_make_sp<GrGLTextureRenderTarget>(this, budgeted, desc, idDesc, rtIDDesc,
Greg Daniel0fc4d2d2017-10-12 11:23:36 -04001455 mipMapsStatus);
Brian Salomon9bada542017-06-12 12:09:30 -04001456 tex->baseLevelWasBoundToFBO();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001457 } else {
Greg Daniel0fc4d2d2017-10-12 11:23:36 -04001458 tex = sk_make_sp<GrGLTexture>(this, budgeted, desc, idDesc, mipMapsStatus);
reed@google.comac10a2d2010-12-22 21:39:39 +00001459 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001460 tex->setCachedTexParams(initialTexParams, this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00001461#ifdef TRACE_TEXTURE_CREATION
tfarina38406c82014-10-31 07:11:12 -07001462 SkDebugf("--- new texture [%d] size=(%d %d) config=%d\n",
brianosmanc4459f62016-07-19 08:02:20 -07001463 idDesc.fInfo.fID, desc.fWidth, desc.fHeight, desc.fConfig);
reed@google.comac10a2d2010-12-22 21:39:39 +00001464#endif
Brian Salomond17b4a62017-05-23 16:53:47 -04001465 if (tex && performClear) {
1466 if (this->glCaps().clearTextureSupport()) {
Brian Salomond17b4a62017-05-23 16:53:47 -04001467 static constexpr uint32_t kZero = 0;
Brian Salomon57111332018-02-05 15:55:54 -05001468 GL_CALL(ClearTexImage(tex->textureID(), 0, GR_GL_RGBA, GR_GL_UNSIGNED_BYTE, &kZero));
Brian Salomond17b4a62017-05-23 16:53:47 -04001469 } else {
Brian Salomond17b4a62017-05-23 16:53:47 -04001470 GrGLIRect viewport;
Robert Phillips67d52cf2017-06-05 13:38:13 -04001471 this->bindSurfaceFBOForPixelOps(tex.get(), GR_GL_FRAMEBUFFER, &viewport,
1472 kDst_TempFBOTarget);
Brian Salomond17b4a62017-05-23 16:53:47 -04001473 this->disableScissor();
1474 this->disableWindowRectangles();
1475 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
1476 fHWWriteToColor = kYes_TriState;
1477 GL_CALL(ClearColor(0, 0, 0, 0));
1478 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
Robert Phillips67d52cf2017-06-05 13:38:13 -04001479 this->unbindTextureFBOForPixelOps(GR_GL_FRAMEBUFFER, tex.get());
Brian Salomond17b4a62017-05-23 16:53:47 -04001480 fHWBoundRenderTargetUniqueID.makeInvalid();
1481 }
1482 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001483 return tex;
1484}
1485
1486namespace {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001487
egdaniel8dc7c3a2015-04-16 11:22:42 -07001488const GrGLuint kUnknownBitCount = GrGLStencilAttachment::kUnknownBitCount;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001489
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001490void inline get_stencil_rb_sizes(const GrGLInterface* gl,
egdaniel8dc7c3a2015-04-16 11:22:42 -07001491 GrGLStencilAttachment::Format* format) {
sugoi@google.com6ba0b0f2013-05-03 13:52:32 +00001492
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001493 // we shouldn't ever know one size and not the other
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001494 SkASSERT((kUnknownBitCount == format->fStencilBits) ==
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001495 (kUnknownBitCount == format->fTotalBits));
1496 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001497 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001498 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1499 (GrGLint*)&format->fStencilBits);
1500 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001501 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001502 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1503 (GrGLint*)&format->fTotalBits);
1504 format->fTotalBits += format->fStencilBits;
1505 } else {
1506 format->fTotalBits = format->fStencilBits;
1507 }
1508 }
1509}
1510}
1511
egdanielff1d5472015-09-10 08:37:20 -07001512int GrGLGpu::getCompatibleStencilIndex(GrPixelConfig config) {
bsalomon100b8f82015-10-28 08:37:44 -07001513 static const int kSize = 16;
Brian Salomonbdecacf2018-02-02 20:32:49 -05001514 SkASSERT(this->caps()->isConfigRenderable(config));
bsalomon30447372015-12-21 09:03:05 -08001515 if (!this->glCaps().hasStencilFormatBeenDeterminedForConfig(config)) {
1516 // Default to unsupported, set this if we find a stencil format that works.
1517 int firstWorkingStencilFormatIndex = -1;
Brian Osman91f9a2c2017-09-05 15:02:46 -04001518
egdanielff1d5472015-09-10 08:37:20 -07001519 // Create color texture
kkinnunen546eb5c2015-12-11 00:05:33 -08001520 GrGLuint colorID = 0;
egdanielff1d5472015-09-10 08:37:20 -07001521 GL_CALL(GenTextures(1, &colorID));
1522 this->setScratchTextureUnit();
1523 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, colorID));
1524 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1525 GR_GL_TEXTURE_MAG_FILTER,
1526 GR_GL_NEAREST));
1527 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1528 GR_GL_TEXTURE_MIN_FILTER,
1529 GR_GL_NEAREST));
1530 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1531 GR_GL_TEXTURE_WRAP_S,
1532 GR_GL_CLAMP_TO_EDGE));
1533 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1534 GR_GL_TEXTURE_WRAP_T,
1535 GR_GL_CLAMP_TO_EDGE));
1536
bsalomon76148af2016-01-12 11:13:47 -08001537 GrGLenum internalFormat;
1538 GrGLenum externalFormat;
1539 GrGLenum externalType;
1540 if (!this->glCaps().getTexImageFormats(config, config, &internalFormat, &externalFormat,
1541 &externalType)) {
1542 return false;
1543 }
Brian Osman9b560242017-09-05 15:34:52 -04001544 this->unbindCpuToGpuXferBuffer();
egdanielff1d5472015-09-10 08:37:20 -07001545 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1546 GL_ALLOC_CALL(this->glInterface(), TexImage2D(GR_GL_TEXTURE_2D,
bsalomon926cb022015-12-17 18:15:11 -08001547 0,
bsalomon76148af2016-01-12 11:13:47 -08001548 internalFormat,
bsalomon100b8f82015-10-28 08:37:44 -07001549 kSize,
1550 kSize,
egdanielff1d5472015-09-10 08:37:20 -07001551 0,
bsalomon76148af2016-01-12 11:13:47 -08001552 externalFormat,
1553 externalType,
Ben Wagnera93a14a2017-08-28 10:34:05 -04001554 nullptr));
bsalomon30447372015-12-21 09:03:05 -08001555 if (GR_GL_NO_ERROR != CHECK_ALLOC_ERROR(this->glInterface())) {
egdanielff1d5472015-09-10 08:37:20 -07001556 GL_CALL(DeleteTextures(1, &colorID));
bsalomon30447372015-12-21 09:03:05 -08001557 return -1;
egdanielff1d5472015-09-10 08:37:20 -07001558 }
1559
1560 // unbind the texture from the texture unit before binding it to the frame buffer
1561 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, 0));
1562
1563 // Create Framebuffer
kkinnunen546eb5c2015-12-11 00:05:33 -08001564 GrGLuint fb = 0;
egdanielff1d5472015-09-10 08:37:20 -07001565 GL_CALL(GenFramebuffers(1, &fb));
egdanielec00d942015-09-14 12:56:10 -07001566 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, fb));
Robert Phillips294870f2016-11-11 12:38:40 -05001567 fHWBoundRenderTargetUniqueID.makeInvalid();
egdanielff1d5472015-09-10 08:37:20 -07001568 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
1569 GR_GL_COLOR_ATTACHMENT0,
1570 GR_GL_TEXTURE_2D,
1571 colorID,
1572 0));
bsalomon30447372015-12-21 09:03:05 -08001573 GrGLuint sbRBID = 0;
1574 GL_CALL(GenRenderbuffers(1, &sbRBID));
egdanielff1d5472015-09-10 08:37:20 -07001575
1576 // look over formats till I find a compatible one
1577 int stencilFmtCnt = this->glCaps().stencilFormats().count();
bsalomon30447372015-12-21 09:03:05 -08001578 if (sbRBID) {
egdanielff1d5472015-09-10 08:37:20 -07001579 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbRBID));
bsalomon30447372015-12-21 09:03:05 -08001580 for (int i = 0; i < stencilFmtCnt && sbRBID; ++i) {
1581 const GrGLCaps::StencilFormat& sFmt = this->glCaps().stencilFormats()[i];
1582 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1583 GL_ALLOC_CALL(this->glInterface(), RenderbufferStorage(GR_GL_RENDERBUFFER,
1584 sFmt.fInternalFormat,
1585 kSize, kSize));
1586 if (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(this->glInterface())) {
egdanielff1d5472015-09-10 08:37:20 -07001587 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon30447372015-12-21 09:03:05 -08001588 GR_GL_STENCIL_ATTACHMENT,
egdanielff1d5472015-09-10 08:37:20 -07001589 GR_GL_RENDERBUFFER, sbRBID));
bsalomon30447372015-12-21 09:03:05 -08001590 if (sFmt.fPacked) {
1591 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1592 GR_GL_DEPTH_ATTACHMENT,
1593 GR_GL_RENDERBUFFER, sbRBID));
1594 } else {
1595 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1596 GR_GL_DEPTH_ATTACHMENT,
1597 GR_GL_RENDERBUFFER, 0));
1598 }
1599 GrGLenum status;
1600 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1601 if (status == GR_GL_FRAMEBUFFER_COMPLETE) {
1602 firstWorkingStencilFormatIndex = i;
1603 break;
1604 }
egdanielff1d5472015-09-10 08:37:20 -07001605 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1606 GR_GL_STENCIL_ATTACHMENT,
1607 GR_GL_RENDERBUFFER, 0));
1608 if (sFmt.fPacked) {
1609 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1610 GR_GL_DEPTH_ATTACHMENT,
1611 GR_GL_RENDERBUFFER, 0));
1612 }
egdanielff1d5472015-09-10 08:37:20 -07001613 }
1614 }
bsalomon30447372015-12-21 09:03:05 -08001615 GL_CALL(DeleteRenderbuffers(1, &sbRBID));
egdanielff1d5472015-09-10 08:37:20 -07001616 }
1617 GL_CALL(DeleteTextures(1, &colorID));
egdanielff1d5472015-09-10 08:37:20 -07001618 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, 0));
1619 GL_CALL(DeleteFramebuffers(1, &fb));
bsalomon30447372015-12-21 09:03:05 -08001620 fGLContext->caps()->setStencilFormatIndexForConfig(config, firstWorkingStencilFormatIndex);
egdanielff1d5472015-09-10 08:37:20 -07001621 }
bsalomon30447372015-12-21 09:03:05 -08001622 return this->glCaps().getStencilFormatIndexForConfig(config);
egdanielff1d5472015-09-10 08:37:20 -07001623}
1624
Brian Salomon2a4f9832018-03-03 22:43:43 -05001625bool GrGLGpu::createTextureImpl(const GrSurfaceDesc& desc, GrGLTextureInfo* info, bool renderTarget,
Brian Salomon58389b92018-03-07 13:01:25 -05001626 GrGLTexture::TexParams* initialTexParams, const GrMipLevel texels[],
Brian Salomon2a4f9832018-03-03 22:43:43 -05001627 int mipLevelCount, GrMipMapsStatus* mipMapsStatus) {
erikchen9a1ed5d2016-02-10 16:32:34 -08001628 info->fID = 0;
1629 info->fTarget = GR_GL_TEXTURE_2D;
1630 GL_CALL(GenTextures(1, &(info->fID)));
1631
1632 if (!info->fID) {
1633 return false;
1634 }
1635
1636 this->setScratchTextureUnit();
1637 GL_CALL(BindTexture(info->fTarget, info->fID));
1638
1639 if (renderTarget && this->glCaps().textureUsageSupport()) {
1640 // provides a hint about how this texture will be used
1641 GL_CALL(TexParameteri(info->fTarget,
1642 GR_GL_TEXTURE_USAGE,
1643 GR_GL_FRAMEBUFFER_ATTACHMENT));
1644 }
1645
cblume55f2d2d2016-02-26 13:20:48 -08001646 if (info) {
1647 set_initial_texture_params(this->glInterface(), *info, initialTexParams);
1648 }
Brian Salomon2a4f9832018-03-03 22:43:43 -05001649
Brian Salomon58389b92018-03-07 13:01:25 -05001650 if (!this->uploadTexData(desc.fConfig, desc.fWidth, desc.fHeight, kTopLeft_GrSurfaceOrigin,
1651 info->fTarget, kNewTexture_UploadType, 0, 0, desc.fWidth, desc.fHeight,
1652 desc.fConfig, texels, mipLevelCount, mipMapsStatus)) {
erikchen9a1ed5d2016-02-10 16:32:34 -08001653 GL_CALL(DeleteTextures(1, &(info->fID)));
1654 return false;
1655 }
Greg Daniele7d8da42017-12-04 11:23:19 -05001656 info->fFormat = this->glCaps().configSizedInternalFormat(desc.fConfig);
erikchen9a1ed5d2016-02-10 16:32:34 -08001657 return true;
1658}
1659
egdanielec00d942015-09-14 12:56:10 -07001660GrStencilAttachment* GrGLGpu::createStencilAttachmentForRenderTarget(const GrRenderTarget* rt,
Brian Salomond1eaf492017-05-18 10:02:08 -04001661 int width, int height) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001662 SkASSERT(width >= rt->width());
1663 SkASSERT(height >= rt->height());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001664
vbuzinovdded6962015-06-12 08:59:45 -07001665 int samples = rt->numStencilSamples();
egdaniel8dc7c3a2015-04-16 11:22:42 -07001666 GrGLStencilAttachment::IDDesc sbDesc;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001667
egdanielff1d5472015-09-10 08:37:20 -07001668 int sIdx = this->getCompatibleStencilIndex(rt->config());
bsalomon62a627b2015-12-17 09:50:47 -08001669 if (sIdx < 0) {
egdanielec00d942015-09-14 12:56:10 -07001670 return nullptr;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001671 }
egdanielff1d5472015-09-10 08:37:20 -07001672
1673 if (!sbDesc.fRenderbufferID) {
1674 GL_CALL(GenRenderbuffers(1, &sbDesc.fRenderbufferID));
1675 }
1676 if (!sbDesc.fRenderbufferID) {
egdanielec00d942015-09-14 12:56:10 -07001677 return nullptr;
egdanielff1d5472015-09-10 08:37:20 -07001678 }
1679 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbDesc.fRenderbufferID));
1680 const GrGLCaps::StencilFormat& sFmt = this->glCaps().stencilFormats()[sIdx];
1681 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1682 // we do this "if" so that we don't call the multisample
1683 // version on a GL that doesn't have an MSAA extension.
Brian Salomonbdecacf2018-02-02 20:32:49 -05001684 if (samples > 1) {
egdanielff1d5472015-09-10 08:37:20 -07001685 SkAssertResult(renderbuffer_storage_msaa(*fGLContext,
1686 samples,
1687 sFmt.fInternalFormat,
1688 width, height));
1689 } else {
1690 GL_ALLOC_CALL(this->glInterface(), RenderbufferStorage(GR_GL_RENDERBUFFER,
1691 sFmt.fInternalFormat,
1692 width, height));
Brian Salomon0ec981b2017-05-15 13:48:50 -04001693 SkASSERT(GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(this->glInterface()));
egdanielff1d5472015-09-10 08:37:20 -07001694 }
1695 fStats.incStencilAttachmentCreates();
1696 // After sized formats we attempt an unsized format and take
1697 // whatever sizes GL gives us. In that case we query for the size.
1698 GrGLStencilAttachment::Format format = sFmt;
1699 get_stencil_rb_sizes(this->glInterface(), &format);
egdanielec00d942015-09-14 12:56:10 -07001700 GrGLStencilAttachment* stencil = new GrGLStencilAttachment(this,
1701 sbDesc,
1702 width,
1703 height,
1704 samples,
1705 format);
1706 return stencil;
reed@google.comac10a2d2010-12-22 21:39:39 +00001707}
1708
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001709////////////////////////////////////////////////////////////////////////////////
1710
jvanverth73063dc2015-12-03 09:15:47 -08001711// GL_STREAM_DRAW triggers an optimization in Chromium's GPU process where a client's vertex buffer
1712// objects are implemented as client-side-arrays on tile-deferred architectures.
1713#define DYNAMIC_USAGE_PARAM GR_GL_STREAM_DRAW
1714
cdaltone2e71c22016-04-07 18:13:29 -07001715GrBuffer* GrGLGpu::onCreateBuffer(size_t size, GrBufferType intendedType,
cdalton1bf3e712016-04-19 10:00:02 -07001716 GrAccessPattern accessPattern, const void* data) {
1717 return GrGLBuffer::Create(this, size, intendedType, accessPattern, data);
jvanverth73063dc2015-12-03 09:15:47 -08001718}
1719
bsalomon3e791242014-12-17 13:43:13 -08001720void GrGLGpu::flushScissor(const GrScissorState& scissorState,
joshualitt77b13072014-10-27 14:51:01 -07001721 const GrGLIRect& rtViewport,
1722 GrSurfaceOrigin rtOrigin) {
robertphillipse85a32d2015-02-10 08:16:55 -08001723 if (scissorState.enabled()) {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001724 GrGLIRect scissor;
Robert Phillips16d8ec62017-07-27 16:16:25 -04001725 scissor.setRelativeTo(rtViewport, scissorState.rect(), rtOrigin);
bsalomon@google.coma3201942012-06-21 19:58:20 +00001726 // if the scissor fully contains the viewport then we fall through and
1727 // disable the scissor test.
bsalomonb0bd4f62014-09-03 07:19:50 -07001728 if (!scissor.contains(rtViewport)) {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001729 if (fHWScissorSettings.fRect != scissor) {
1730 scissor.pushToGLScissor(this->glInterface());
1731 fHWScissorSettings.fRect = scissor;
1732 }
1733 if (kYes_TriState != fHWScissorSettings.fEnabled) {
1734 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1735 fHWScissorSettings.fEnabled = kYes_TriState;
1736 }
1737 return;
1738 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001739 }
joshualitt77b13072014-10-27 14:51:01 -07001740
1741 // See fall through note above
1742 this->disableScissor();
1743}
1744
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001745void GrGLGpu::flushWindowRectangles(const GrWindowRectsState& windowState,
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001746 const GrGLRenderTarget* rt, GrSurfaceOrigin origin) {
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001747#ifndef USE_NSIGHT
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001748 typedef GrWindowRectsState::Mode Mode;
1749 SkASSERT(!windowState.enabled() || rt->renderFBOID()); // Window rects can't be used on-screen.
1750 SkASSERT(windowState.numWindows() <= this->caps()->maxWindowRectangles());
csmartdalton28341fa2016-08-17 10:00:21 -07001751
Jim Van Verth6a40abc2017-11-02 16:56:09 +00001752 if (!this->caps()->maxWindowRectangles() ||
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001753 fHWWindowRectsState.knownEqualTo(origin, rt->getViewport(), windowState)) {
csmartdalton28341fa2016-08-17 10:00:21 -07001754 return;
csmartdalton28341fa2016-08-17 10:00:21 -07001755 }
1756
csmartdalton7535f412016-08-23 06:51:00 -07001757 // This is purely a workaround for a spurious warning generated by gcc. Otherwise the above
1758 // assert would be sufficient. https://gcc.gnu.org/bugzilla/show_bug.cgi?id=5912
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001759 int numWindows = SkTMin(windowState.numWindows(), int(GrWindowRectangles::kMaxWindows));
1760 SkASSERT(windowState.numWindows() == numWindows);
csmartdalton7535f412016-08-23 06:51:00 -07001761
csmartdalton28341fa2016-08-17 10:00:21 -07001762 GrGLIRect glwindows[GrWindowRectangles::kMaxWindows];
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001763 const SkIRect* skwindows = windowState.windows().data();
csmartdalton7535f412016-08-23 06:51:00 -07001764 for (int i = 0; i < numWindows; ++i) {
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001765 glwindows[i].setRelativeTo(rt->getViewport(), skwindows[i], origin);
csmartdalton28341fa2016-08-17 10:00:21 -07001766 }
1767
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001768 GrGLenum glmode = (Mode::kExclusive == windowState.mode()) ? GR_GL_EXCLUSIVE : GR_GL_INCLUSIVE;
csmartdalton7535f412016-08-23 06:51:00 -07001769 GL_CALL(WindowRectangles(glmode, numWindows, glwindows->asInts()));
csmartdalton28341fa2016-08-17 10:00:21 -07001770
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001771 fHWWindowRectsState.set(origin, rt->getViewport(), windowState);
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001772#endif
csmartdalton28341fa2016-08-17 10:00:21 -07001773}
1774
1775void GrGLGpu::disableWindowRectangles() {
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001776#ifndef USE_NSIGHT
Jim Van Verth6a40abc2017-11-02 16:56:09 +00001777 if (!this->caps()->maxWindowRectangles() || fHWWindowRectsState.knownDisabled()) {
csmartdalton28341fa2016-08-17 10:00:21 -07001778 return;
1779 }
1780 GL_CALL(WindowRectangles(GR_GL_EXCLUSIVE, 0, nullptr));
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001781 fHWWindowRectsState.setDisabled();
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001782#endif
csmartdalton28341fa2016-08-17 10:00:21 -07001783}
1784
ethannicholas28ef4452016-03-25 09:26:03 -07001785void GrGLGpu::flushMinSampleShading(float minSampleShading) {
1786 if (fHWMinSampleShading != minSampleShading) {
1787 if (minSampleShading > 0.0) {
1788 GL_CALL(Enable(GR_GL_SAMPLE_SHADING));
1789 GL_CALL(MinSampleShading(minSampleShading));
1790 }
1791 else {
1792 GL_CALL(Disable(GR_GL_SAMPLE_SHADING));
1793 }
1794 fHWMinSampleShading = minSampleShading;
1795 }
1796}
1797
bsalomon2eda5b32016-09-21 10:53:24 -07001798bool GrGLGpu::flushGLState(const GrPipeline& pipeline, const GrPrimitiveProcessor& primProc,
1799 bool willDrawPoints) {
Hal Canary144caf52016-11-07 17:57:18 -05001800 sk_sp<GrGLProgram> program(fProgramCache->refProgram(this, pipeline, primProc, willDrawPoints));
brianosman33f6b3f2016-06-02 05:49:21 -07001801 if (!program) {
1802 GrCapsDebugf(this->caps(), "Failed to create program!\n");
1803 return false;
1804 }
1805
1806 program->generateMipmaps(primProc, pipeline);
1807
egdaniel080e6732014-12-22 07:35:52 -08001808 GrXferProcessor::BlendInfo blendInfo;
egdaniel0e1853c2016-03-17 11:35:45 -07001809 pipeline.getXferProcessor().getBlendInfo(&blendInfo);
egdaniel080e6732014-12-22 07:35:52 -08001810
egdaniel080e6732014-12-22 07:35:52 -08001811 this->flushColorWrite(blendInfo.fWriteColor);
ethannicholas28ef4452016-03-25 09:26:03 -07001812 this->flushMinSampleShading(primProc.getSampleShading());
bsalomonbc3d0de2014-12-15 13:45:03 -08001813
bsalomon6df86402015-06-01 10:41:49 -07001814 GrGLuint programID = program->programID();
bsalomon1f78c0a2014-12-17 09:43:13 -08001815 if (fHWProgramID != programID) {
1816 GL_CALL(UseProgram(programID));
1817 fHWProgramID = programID;
1818 }
1819
egdanield803f272015-03-18 13:01:52 -07001820 if (blendInfo.fWriteColor) {
bsalomon7f9b2e42016-01-12 13:29:26 -08001821 // Swizzle the blend to match what the shader will output.
Brian Salomon1edc5b92016-11-29 13:43:46 -05001822 const GrSwizzle& swizzle = this->caps()->shaderCaps()->configOutputSwizzle(
Robert Phillips2890fbf2017-07-26 15:48:41 -04001823 pipeline.proxy()->config());
bsalomon7f9b2e42016-01-12 13:29:26 -08001824 this->flushBlend(blendInfo, swizzle);
egdanield803f272015-03-18 13:01:52 -07001825 }
bsalomon1f78c0a2014-12-17 09:43:13 -08001826
cdalton74b8d322016-04-11 14:47:28 -07001827 program->setData(primProc, pipeline);
bsalomon1f78c0a2014-12-17 09:43:13 -08001828
Robert Phillips2890fbf2017-07-26 15:48:41 -04001829 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(pipeline.renderTarget());
csmartdaltonc633abb2016-11-01 08:55:55 -07001830 GrStencilSettings stencil;
1831 if (pipeline.isStencilEnabled()) {
1832 // TODO: attach stencil and create settings during render target flush.
1833 SkASSERT(glRT->renderTargetPriv().getStencilAttachment());
1834 stencil.reset(*pipeline.getUserStencil(), pipeline.hasStencilClip(),
1835 glRT->renderTargetPriv().numStencilBits());
1836 }
1837 this->flushStencil(stencil);
Robert Phillips16d8ec62017-07-27 16:16:25 -04001838 this->flushScissor(pipeline.getScissorState(), glRT->getViewport(), pipeline.proxy()->origin());
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001839 this->flushWindowRectangles(pipeline.getWindowRectsState(), glRT, pipeline.proxy()->origin());
csmartdaltondabc91b2016-11-15 14:26:27 -07001840 this->flushHWAAState(glRT, pipeline.isHWAntialiasState(), !stencil.isDisabled());
bsalomonbc3d0de2014-12-15 13:45:03 -08001841
1842 // This must come after textures are flushed because a texture may need
egdanield803f272015-03-18 13:01:52 -07001843 // to be msaa-resolved (which will modify bound FBO state).
Brian Salomon1fabd512018-02-09 09:54:25 -05001844 this->flushRenderTarget(glRT, pipeline.getDisableOutputConversionToSRGB());
bsalomonbc3d0de2014-12-15 13:45:03 -08001845
1846 return true;
1847}
1848
joshualitt873ad0e2015-01-20 09:08:51 -08001849void GrGLGpu::setupGeometry(const GrPrimitiveProcessor& primProc,
Chris Daltonff926502017-05-03 14:36:54 -04001850 const GrBuffer* indexBuffer,
1851 const GrBuffer* vertexBuffer,
Chris Dalton1d616352017-05-31 12:51:23 -06001852 int baseVertex,
1853 const GrBuffer* instanceBuffer,
1854 int baseInstance) {
Chris Dalton27059d32018-01-23 14:06:50 -07001855 using EnablePrimitiveRestart = GrGLAttribArrayState::EnablePrimitiveRestart;
1856
cdaltone2e71c22016-04-07 18:13:29 -07001857 GrGLAttribArrayState* attribState;
Chris Daltonff926502017-05-03 14:36:54 -04001858 if (indexBuffer) {
Chris Dalton1d616352017-05-31 12:51:23 -06001859 SkASSERT(indexBuffer && !indexBuffer->isMapped());
Chris Daltonff926502017-05-03 14:36:54 -04001860 attribState = fHWVertexArrayState.bindInternalVertexArray(this, indexBuffer);
cdaltone2e71c22016-04-07 18:13:29 -07001861 } else {
1862 attribState = fHWVertexArrayState.bindInternalVertexArray(this);
bsalomonbc3d0de2014-12-15 13:45:03 -08001863 }
bsalomonbc3d0de2014-12-15 13:45:03 -08001864
Chris Dalton1d616352017-05-31 12:51:23 -06001865 struct {
1866 const GrBuffer* fBuffer;
1867 int fStride;
1868 size_t fBufferOffset;
1869 } bindings[2];
Chris Dalton8e45b4f2017-05-05 14:00:56 -04001870
Chris Dalton1d616352017-05-31 12:51:23 -06001871 if (int vertexStride = primProc.getVertexStride()) {
1872 SkASSERT(vertexBuffer && !vertexBuffer->isMapped());
1873 bindings[0].fBuffer = vertexBuffer;
1874 bindings[0].fStride = vertexStride;
1875 bindings[0].fBufferOffset = vertexBuffer->baseOffset() + baseVertex * vertexStride;
1876 }
1877 if (int instanceStride = primProc.getInstanceStride()) {
1878 SkASSERT(instanceBuffer && !instanceBuffer->isMapped());
1879 bindings[1].fBuffer = instanceBuffer;
1880 bindings[1].fStride = instanceStride;
1881 bindings[1].fBufferOffset = instanceBuffer->baseOffset() + baseInstance * instanceStride;
1882 }
bsalomonbc3d0de2014-12-15 13:45:03 -08001883
Chris Dalton1d616352017-05-31 12:51:23 -06001884 int numAttribs = primProc.numAttribs();
Chris Dalton27059d32018-01-23 14:06:50 -07001885 auto enableRestart = EnablePrimitiveRestart(primProc.willUsePrimitiveRestart() && indexBuffer);
1886 attribState->enableVertexArrays(this, numAttribs, enableRestart);
bsalomonbc3d0de2014-12-15 13:45:03 -08001887
Chris Dalton1d616352017-05-31 12:51:23 -06001888 for (int i = 0; i < numAttribs; ++i) {
1889 using InputRate = GrPrimitiveProcessor::Attribute::InputRate;
1890 const GrGeometryProcessor::Attribute& attrib = primProc.getAttrib(i);
1891 const int divisor = InputRate::kPerInstance == attrib.fInputRate ? 1 : 0;
1892 const auto& binding = bindings[divisor];
1893 attribState->set(this, i, binding.fBuffer, attrib.fType, binding.fStride,
1894 binding.fBufferOffset + attrib.fOffsetInRecord, divisor);
bsalomonbc3d0de2014-12-15 13:45:03 -08001895 }
1896}
1897
csmartdalton485a1202016-07-13 10:16:32 -07001898GrGLenum GrGLGpu::bindBuffer(GrBufferType type, const GrBuffer* buffer) {
joshualitt93316b92015-10-23 09:08:08 -07001899 this->handleDirtyContext();
cdaltondeacc972016-04-06 14:26:33 -07001900
cdaltone2e71c22016-04-07 18:13:29 -07001901 // Index buffer state is tied to the vertex array.
1902 if (kIndex_GrBufferType == type) {
1903 this->bindVertexArray(0);
cdaltondeacc972016-04-06 14:26:33 -07001904 }
cdaltone2e71c22016-04-07 18:13:29 -07001905
1906 SkASSERT(type >= 0 && type <= kLast_GrBufferType);
1907 auto& bufferState = fHWBufferState[type];
1908
robertphillips8abb3702016-08-31 14:04:06 -07001909 if (buffer->uniqueID() != bufferState.fBoundBufferUniqueID) {
csmartdalton485a1202016-07-13 10:16:32 -07001910 if (buffer->isCPUBacked()) {
1911 if (!bufferState.fBufferZeroKnownBound) {
1912 GL_CALL(BindBuffer(bufferState.fGLTarget, 0));
1913 }
1914 } else {
1915 const GrGLBuffer* glBuffer = static_cast<const GrGLBuffer*>(buffer);
1916 GL_CALL(BindBuffer(bufferState.fGLTarget, glBuffer->bufferID()));
cdaltone2e71c22016-04-07 18:13:29 -07001917 }
csmartdalton485a1202016-07-13 10:16:32 -07001918 bufferState.fBufferZeroKnownBound = buffer->isCPUBacked();
robertphillips8abb3702016-08-31 14:04:06 -07001919 bufferState.fBoundBufferUniqueID = buffer->uniqueID();
cdaltone2e71c22016-04-07 18:13:29 -07001920 }
1921
1922 return bufferState.fGLTarget;
joshualitt93316b92015-10-23 09:08:08 -07001923}
1924
cdalton74b8d322016-04-11 14:47:28 -07001925void GrGLGpu::notifyBufferReleased(const GrGLBuffer* buffer) {
1926 if (buffer->hasAttachedToTexture()) {
1927 // Detach this buffer from any textures to ensure the underlying memory is freed.
Robert Phillips294870f2016-11-11 12:38:40 -05001928 GrGpuResource::UniqueID uniqueID = buffer->uniqueID();
cdalton74b8d322016-04-11 14:47:28 -07001929 for (int i = fHWMaxUsedBufferTextureUnit; i >= 0; --i) {
1930 auto& buffTex = fHWBufferTextures[i];
1931 if (uniqueID != buffTex.fAttachedBufferUniqueID) {
1932 continue;
1933 }
1934 if (i == fHWMaxUsedBufferTextureUnit) {
1935 --fHWMaxUsedBufferTextureUnit;
1936 }
1937
1938 this->setTextureUnit(i);
1939 if (!buffTex.fKnownBound) {
1940 SkASSERT(buffTex.fTextureID);
1941 GL_CALL(BindTexture(GR_GL_TEXTURE_BUFFER, buffTex.fTextureID));
1942 buffTex.fKnownBound = true;
1943 }
1944 GL_CALL(TexBuffer(GR_GL_TEXTURE_BUFFER,
1945 this->glCaps().configSizedInternalFormat(buffTex.fTexelConfig), 0));
1946 }
1947 }
1948}
1949
bsalomon861e1032014-12-16 07:33:49 -08001950void GrGLGpu::disableScissor() {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001951 if (kNo_TriState != fHWScissorSettings.fEnabled) {
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001952 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.coma3201942012-06-21 19:58:20 +00001953 fHWScissorSettings.fEnabled = kNo_TriState;
1954 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001955 }
1956}
1957
Robert Phillips19e51dc2017-08-09 09:30:51 -04001958void GrGLGpu::clear(const GrFixedClip& clip, GrColor color,
1959 GrRenderTarget* target, GrSurfaceOrigin origin) {
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001960 // parent class should never let us get here with no RT
bsalomon49f085d2014-09-05 13:34:00 -07001961 SkASSERT(target);
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001962
Brian Salomon43f8bf02017-10-18 08:33:29 -04001963 this->handleDirtyContext();
bsalomon@google.com74b98712011-11-11 19:46:16 +00001964
1965 GrGLfloat r, g, b, a;
1966 static const GrGLfloat scale255 = 1.f / 255.f;
1967 a = GrColorUnpackA(color) * scale255;
1968 GrGLfloat scaleRGB = scale255;
bsalomon@google.com74b98712011-11-11 19:46:16 +00001969 r = GrColorUnpackR(color) * scaleRGB;
1970 g = GrColorUnpackG(color) * scaleRGB;
1971 b = GrColorUnpackB(color) * scaleRGB;
1972
Brian Salomon43f8bf02017-10-18 08:33:29 -04001973 if (this->glCaps().useDrawToClearColor()) {
1974 this->clearColorAsDraw(clip, r, g, b, a, target, origin);
1975 return;
1976 }
1977
1978 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
1979
Brian Salomon1fabd512018-02-09 09:54:25 -05001980 if (clip.scissorEnabled()) {
1981 this->flushRenderTarget(glRT, origin, clip.scissorRect());
1982 } else {
1983 this->flushRenderTarget(glRT);
1984 }
Brian Salomon43f8bf02017-10-18 08:33:29 -04001985 this->flushScissor(clip.scissorState(), glRT->getViewport(), origin);
1986 this->flushWindowRectangles(clip.windowRectsState(), glRT, origin);
1987
bsalomon@google.com74b98712011-11-11 19:46:16 +00001988 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00001989 fHWWriteToColor = kYes_TriState;
Brian Salomon028a9a52017-05-11 11:39:08 -04001990
Eric Karlaeaf22b2017-05-18 15:08:09 -07001991 if (this->glCaps().clearToBoundaryValuesIsBroken() &&
1992 (1 == r || 0 == r) && (1 == g || 0 == g) && (1 == b || 0 == b) && (1 == a || 0 == a)) {
Eric Karlaeaf22b2017-05-18 15:08:09 -07001993 static const GrGLfloat safeAlpha1 = nextafter(1.f, 2.f);
1994 static const GrGLfloat safeAlpha0 = nextafter(0.f, -1.f);
Eric Karlaeaf22b2017-05-18 15:08:09 -07001995 a = (1 == a) ? safeAlpha1 : safeAlpha0;
Brian Salomon028a9a52017-05-11 11:39:08 -04001996 }
bsalomon@google.com74b98712011-11-11 19:46:16 +00001997 GL_CALL(ClearColor(r, g, b, a));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001998 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001999}
2000
Robert Phillips95214472017-08-08 18:00:03 -04002001void GrGLGpu::clearStencil(GrRenderTarget* target, int clearValue) {
2002 if (!target) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00002003 return;
2004 }
Robert Phillipscb2e2352017-08-30 16:44:40 -04002005
2006 GrStencilAttachment* sb = target->renderTargetPriv().getStencilAttachment();
2007 // this should only be called internally when we know we have a
2008 // stencil buffer.
2009 SkASSERT(sb);
2010
bsalomonb0bd4f62014-09-03 07:19:50 -07002011 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
Brian Salomon1fabd512018-02-09 09:54:25 -05002012 this->flushRenderTargetNoColorWrites(glRT);
bsalomon@google.com8295dc12011-05-02 12:53:34 +00002013
joshualitt77b13072014-10-27 14:51:01 -07002014 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07002015 this->disableWindowRectangles();
robertphillips@google.com730ebe52012-04-16 16:33:13 +00002016
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002017 GL_CALL(StencilMask(0xffffffff));
Robert Phillips95214472017-08-08 18:00:03 -04002018 GL_CALL(ClearStencil(clearValue));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002019 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00002020 fHWStencilSettings.invalidate();
Robert Phillipscb2e2352017-08-30 16:44:40 -04002021 if (!clearValue) {
2022 sb->cleared();
2023 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002024}
2025
csmartdalton29df7602016-08-31 11:55:52 -07002026void GrGLGpu::clearStencilClip(const GrFixedClip& clip,
2027 bool insideStencilMask,
Robert Phillips19e51dc2017-08-09 09:30:51 -04002028 GrRenderTarget* target, GrSurfaceOrigin origin) {
bsalomon49f085d2014-09-05 13:34:00 -07002029 SkASSERT(target);
egdaniel9cb63402016-06-23 08:37:05 -07002030 this->handleDirtyContext();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00002031
Mike Klein31550db2017-06-06 23:29:53 +00002032 if (this->glCaps().useDrawToClearStencilClip()) {
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002033 this->clearStencilClipAsDraw(clip, insideStencilMask, target, origin);
Mike Klein31550db2017-06-06 23:29:53 +00002034 return;
2035 }
2036
egdaniel8dc7c3a2015-04-16 11:22:42 -07002037 GrStencilAttachment* sb = target->renderTargetPriv().getStencilAttachment();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00002038 // this should only be called internally when we know we have a
2039 // stencil buffer.
bsalomon6bc1b5f2015-02-23 09:06:38 -08002040 SkASSERT(sb);
2041 GrGLint stencilBitCount = sb->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002042#if 0
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002043 SkASSERT(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00002044 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002045#else
2046 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00002047 // ANGLE a partial stencil mask will cause clears to be
Robert Phillipsf2361d22016-10-25 14:20:06 -04002048 // turned into draws. Our contract on GrOpList says that
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002049 // changing the clip between stencil passes may or may not
2050 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00002051 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002052#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002053 GrGLint value;
csmartdalton29df7602016-08-31 11:55:52 -07002054 if (insideStencilMask) {
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002055 value = (1 << (stencilBitCount - 1));
2056 } else {
2057 value = 0;
2058 }
bsalomonb0bd4f62014-09-03 07:19:50 -07002059 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
Brian Salomon1fabd512018-02-09 09:54:25 -05002060 this->flushRenderTargetNoColorWrites(glRT);
bsalomon@google.coma3201942012-06-21 19:58:20 +00002061
Robert Phillips19e51dc2017-08-09 09:30:51 -04002062 this->flushScissor(clip.scissorState(), glRT->getViewport(), origin);
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002063 this->flushWindowRectangles(clip.windowRectsState(), glRT, origin);
bsalomon@google.coma3201942012-06-21 19:58:20 +00002064
caryclark@google.comcf6285b2012-06-06 12:09:01 +00002065 GL_CALL(StencilMask((uint32_t) clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002066 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002067 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00002068 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00002069}
2070
Brian Salomon71d9d842016-11-03 13:42:00 -04002071static bool read_pixels_pays_for_y_flip(GrSurfaceOrigin origin, const GrGLCaps& caps,
bsalomon39826022015-07-23 08:07:21 -07002072 int width, int height, GrPixelConfig config,
2073 size_t rowBytes) {
Brian Salomon71d9d842016-11-03 13:42:00 -04002074 // If the surface is already TopLeft, we don't need to flip.
2075 if (kTopLeft_GrSurfaceOrigin == origin) {
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002076 return false;
2077 }
2078
bsalomon494aa592015-07-23 11:45:02 -07002079 // If the read is really small or smaller than the min texture size, don't force a draw.
bsalomon100b8f82015-10-28 08:37:44 -07002080 static const int kMinSize = 32;
2081 if (width < kMinSize || height < kMinSize) {
bsalomon494aa592015-07-23 11:45:02 -07002082 return false;
2083 }
2084
bsalomon@google.com56d11e02011-11-30 19:59:08 +00002085 // if GL can do the flip then we'll never pay for it.
bsalomon39826022015-07-23 08:07:21 -07002086 if (caps.packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00002087 return false;
2088 }
2089
2090 // If we have to do memcpy to handle non-trim rowBytes then we
bsalomon@google.com7107fa72011-11-10 14:54:14 +00002091 // get the flip for free. Otherwise it costs.
bsalomon39826022015-07-23 08:07:21 -07002092 // Note that we're assuming that 0 rowBytes has already been handled and that the width has been
2093 // clipped.
2094 return caps.packRowLengthSupport() || GrBytesPerPixel(config) * width == rowBytes;
2095}
2096
bsalomon1aa20292016-01-22 08:16:09 -08002097bool GrGLGpu::readPixelsSupported(GrRenderTarget* target, GrPixelConfig readConfig) {
Brian Salomon625cd9e2016-12-15 09:35:19 -05002098#ifdef SK_BUILD_FOR_MAC
2099 // Chromium may ask us to read back from locked IOSurfaces. Calling the command buffer's
2100 // glGetIntegerv() with GL_IMPLEMENTATION_COLOR_READ_FORMAT/_TYPE causes the command buffer
2101 // to make a call to check the framebuffer status which can hang the driver. So in Mac Chromium
2102 // we always use a temporary surface to test for read pixels support.
2103 // https://www.crbug.com/662802
2104 if (this->glContext().driver() == kChromium_GrGLDriver) {
2105 return this->readPixelsSupported(target->config(), readConfig);
2106 }
2107#endif
bsalomon1aa20292016-01-22 08:16:09 -08002108 auto bindRenderTarget = [this, target]() -> bool {
Brian Salomon1fabd512018-02-09 09:54:25 -05002109 this->flushRenderTargetNoColorWrites(static_cast<GrGLRenderTarget*>(target));
bsalomon1aa20292016-01-22 08:16:09 -08002110 return true;
2111 };
bsalomon2c3db322016-11-08 13:26:24 -08002112 auto unbindRenderTarget = []{};
bsalomon1aa20292016-01-22 08:16:09 -08002113 auto getIntegerv = [this](GrGLenum query, GrGLint* value) {
2114 GR_GL_GetIntegerv(this->glInterface(), query, value);
2115 };
2116 GrPixelConfig rtConfig = target->config();
bsalomon2c3db322016-11-08 13:26:24 -08002117 return this->glCaps().readPixelsSupported(rtConfig, readConfig, getIntegerv, bindRenderTarget,
2118 unbindRenderTarget);
bsalomon1aa20292016-01-22 08:16:09 -08002119}
2120
2121bool GrGLGpu::readPixelsSupported(GrPixelConfig rtConfig, GrPixelConfig readConfig) {
bsalomon2c3db322016-11-08 13:26:24 -08002122 sk_sp<GrTexture> temp;
2123 auto bindRenderTarget = [this, rtConfig, &temp]() -> bool {
Robert Phillips16d8ec62017-07-27 16:16:25 -04002124 GrSurfaceDesc desc;
bsalomon1aa20292016-01-22 08:16:09 -08002125 desc.fConfig = rtConfig;
2126 desc.fWidth = desc.fHeight = 16;
Brian Salomonbdecacf2018-02-02 20:32:49 -05002127 if (this->glCaps().isConfigRenderable(rtConfig)) {
bsalomon2c3db322016-11-08 13:26:24 -08002128 desc.fFlags = kRenderTarget_GrSurfaceFlag;
Robert Phillips67d52cf2017-06-05 13:38:13 -04002129 temp = this->createTexture(desc, SkBudgeted::kNo);
bsalomon2c3db322016-11-08 13:26:24 -08002130 if (!temp) {
2131 return false;
2132 }
2133 GrGLRenderTarget* glrt = static_cast<GrGLRenderTarget*>(temp->asRenderTarget());
Brian Salomon1fabd512018-02-09 09:54:25 -05002134 this->flushRenderTargetNoColorWrites(glrt);
bsalomon2c3db322016-11-08 13:26:24 -08002135 return true;
2136 } else if (this->glCaps().canConfigBeFBOColorAttachment(rtConfig)) {
Robert Phillips67d52cf2017-06-05 13:38:13 -04002137 temp = this->createTexture(desc, SkBudgeted::kNo);
bsalomon2c3db322016-11-08 13:26:24 -08002138 if (!temp) {
2139 return false;
2140 }
2141 GrGLIRect vp;
2142 this->bindSurfaceFBOForPixelOps(temp.get(), GR_GL_FRAMEBUFFER, &vp, kDst_TempFBOTarget);
Robert Phillips294870f2016-11-11 12:38:40 -05002143 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon2c3db322016-11-08 13:26:24 -08002144 return true;
bsalomon1aa20292016-01-22 08:16:09 -08002145 }
bsalomon2c3db322016-11-08 13:26:24 -08002146 return false;
2147 };
2148 auto unbindRenderTarget = [this, &temp]() {
2149 this->unbindTextureFBOForPixelOps(GR_GL_FRAMEBUFFER, temp.get());
bsalomon1aa20292016-01-22 08:16:09 -08002150 };
2151 auto getIntegerv = [this](GrGLenum query, GrGLint* value) {
2152 GR_GL_GetIntegerv(this->glInterface(), query, value);
2153 };
bsalomon2c3db322016-11-08 13:26:24 -08002154 return this->glCaps().readPixelsSupported(rtConfig, readConfig, getIntegerv, bindRenderTarget,
2155 unbindRenderTarget);
bsalomon1aa20292016-01-22 08:16:09 -08002156}
2157
2158bool GrGLGpu::readPixelsSupported(GrSurface* surfaceForConfig, GrPixelConfig readConfig) {
2159 if (GrRenderTarget* rt = surfaceForConfig->asRenderTarget()) {
2160 return this->readPixelsSupported(rt, readConfig);
2161 } else {
2162 GrPixelConfig config = surfaceForConfig->config();
2163 return this->readPixelsSupported(config, readConfig);
2164 }
2165}
2166
Brian Salomon9b009bb2018-02-14 13:53:55 -05002167bool GrGLGpu::onGetReadPixelsInfo(GrSurface* srcSurface, GrSurfaceOrigin srcOrigin, int width,
Brian Salomonc320b152018-02-20 14:05:36 -05002168 int height, size_t rowBytes, GrColorType dstColorType,
Brian Salomon9b009bb2018-02-14 13:53:55 -05002169 DrawPreference* drawPreference,
bsalomonf0674512015-07-28 13:26:15 -07002170 ReadPixelTempDrawInfo* tempDrawInfo) {
Brian Salomonc320b152018-02-20 14:05:36 -05002171 // We don't want to introduce a sRGB conversion if we trigger a draw.
2172 auto dstConfigSRGBEncoded = GrPixelConfigIsSRGBEncoded(srcSurface->config());
Brian Salomon9b009bb2018-02-14 13:53:55 -05002173 if (*drawPreference != kNoDraw_DrawPreference) {
2174 // We assume the base class has only inserted a draw for sRGB reasons. So the
2175 // the temp surface has the config of the dst data. There is no swizzling, nor dst config
2176 // spoofing.
Brian Salomonc320b152018-02-20 14:05:36 -05002177 SkASSERT(tempDrawInfo->fReadColorType == dstColorType);
2178 SkASSERT(GrPixelConfigToColorType(tempDrawInfo->fTempSurfaceDesc.fConfig) == dstColorType);
Brian Salomon9b009bb2018-02-14 13:53:55 -05002179 SkASSERT(tempDrawInfo->fSwizzle == GrSwizzle::RGBA());
Brian Salomonc320b152018-02-20 14:05:36 -05002180 // Don't undo a sRGB conversion introduced by our caller via an intermediate draw.
2181 dstConfigSRGBEncoded = GrPixelConfigIsSRGBEncoded(tempDrawInfo->fTempSurfaceDesc.fConfig);
2182 }
2183 if (GrColorTypeIsAlphaOnly(dstColorType)) {
2184 dstConfigSRGBEncoded = GrSRGBEncoded::kNo;
Brian Salomon9b009bb2018-02-14 13:53:55 -05002185 }
bsalomone9573312016-01-25 14:33:25 -08002186 GrPixelConfig srcConfig = srcSurface->config();
bsalomon1aa20292016-01-22 08:16:09 -08002187
bsalomonb117ff12016-07-19 07:24:40 -07002188 tempDrawInfo->fTempSurfaceFit = this->glCaps().partialFBOReadIsSlow() ? SkBackingFit::kExact
2189 : SkBackingFit::kApprox;
bsalomone9573312016-01-25 14:33:25 -08002190
Brian Salomonc320b152018-02-20 14:05:36 -05002191 // TODO: Update this logic to use color type.
2192 auto dstAsConfig = GrColorTypeToPixelConfig(dstColorType, dstConfigSRGBEncoded);
2193
2194 if (this->glCaps().rgba8888PixelsOpsAreSlow() && kRGBA_8888_GrPixelConfig == dstAsConfig &&
bsalomon1aa20292016-01-22 08:16:09 -08002195 this->readPixelsSupported(kBGRA_8888_GrPixelConfig, kBGRA_8888_GrPixelConfig)) {
bsalomon39826022015-07-23 08:07:21 -07002196 tempDrawInfo->fTempSurfaceDesc.fConfig = kBGRA_8888_GrPixelConfig;
bsalomon6c9cd552016-01-22 07:17:34 -08002197 tempDrawInfo->fSwizzle = GrSwizzle::BGRA();
Brian Salomonc320b152018-02-20 14:05:36 -05002198 tempDrawInfo->fReadColorType = GrColorType::kBGRA_8888;
bsalomonb411b3b2015-07-31 09:34:24 -07002199 ElevateDrawPreference(drawPreference, kGpuPrefersDraw_DrawPreference);
ericrkb4ecabd2016-03-11 15:18:20 -08002200 } else if (this->glCaps().rgbaToBgraReadbackConversionsAreSlow() &&
Brian Salomonc320b152018-02-20 14:05:36 -05002201 GrBytesPerPixel(dstAsConfig) == 4 &&
2202 GrPixelConfigSwapRAndB(dstAsConfig) == srcConfig &&
bsalomon1aa20292016-01-22 08:16:09 -08002203 this->readPixelsSupported(srcSurface, srcConfig)) {
bsalomon6c9cd552016-01-22 07:17:34 -08002204 // Mesa 3D takes a slow path on when reading back BGRA from an RGBA surface and vice-versa.
bsalomon39826022015-07-23 08:07:21 -07002205 // Better to do a draw with a R/B swap and then read as the original config.
2206 tempDrawInfo->fTempSurfaceDesc.fConfig = srcConfig;
bsalomon6c9cd552016-01-22 07:17:34 -08002207 tempDrawInfo->fSwizzle = GrSwizzle::BGRA();
Brian Salomonc320b152018-02-20 14:05:36 -05002208 tempDrawInfo->fReadColorType = GrPixelConfigToColorType(srcConfig);
bsalomonf0674512015-07-28 13:26:15 -07002209 ElevateDrawPreference(drawPreference, kGpuPrefersDraw_DrawPreference);
Brian Salomonc320b152018-02-20 14:05:36 -05002210 } else if (!this->readPixelsSupported(srcSurface, dstAsConfig)) {
2211 if (kBGRA_8888_GrPixelConfig == dstAsConfig &&
Brian Salomon71d9d842016-11-03 13:42:00 -04002212 this->glCaps().canConfigBeFBOColorAttachment(kRGBA_8888_GrPixelConfig) &&
bsalomon1aa20292016-01-22 08:16:09 -08002213 this->readPixelsSupported(kRGBA_8888_GrPixelConfig, kRGBA_8888_GrPixelConfig)) {
bsalomone9573312016-01-25 14:33:25 -08002214 // We're trying to read BGRA but it's not supported. If RGBA is renderable and
2215 // we can read it back, then do a swizzling draw to a RGBA and read it back (which
2216 // will effectively be BGRA).
bsalomon1aa20292016-01-22 08:16:09 -08002217 tempDrawInfo->fTempSurfaceDesc.fConfig = kRGBA_8888_GrPixelConfig;
2218 tempDrawInfo->fSwizzle = GrSwizzle::BGRA();
Brian Salomonc320b152018-02-20 14:05:36 -05002219 tempDrawInfo->fReadColorType = GrColorType::kRGBA_8888;
bsalomon1aa20292016-01-22 08:16:09 -08002220 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
Brian Salomonc320b152018-02-20 14:05:36 -05002221 } else if (kSBGRA_8888_GrPixelConfig == dstAsConfig &&
Brian Salomon9b009bb2018-02-14 13:53:55 -05002222 this->glCaps().canConfigBeFBOColorAttachment(kSRGBA_8888_GrPixelConfig) &&
2223 this->readPixelsSupported(kSRGBA_8888_GrPixelConfig,
2224 kSRGBA_8888_GrPixelConfig)) {
brianosmana6359362016-03-21 06:55:37 -07002225 // We're trying to read sBGRA but it's not supported. If sRGBA is renderable and
2226 // we can read it back, then do a swizzling draw to a sRGBA and read it back (which
2227 // will effectively be sBGRA).
2228 tempDrawInfo->fTempSurfaceDesc.fConfig = kSRGBA_8888_GrPixelConfig;
2229 tempDrawInfo->fSwizzle = GrSwizzle::BGRA();
Brian Salomonc320b152018-02-20 14:05:36 -05002230 tempDrawInfo->fReadColorType = GrColorType::kRGBA_8888;
brianosmana6359362016-03-21 06:55:37 -07002231 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
Brian Salomonc320b152018-02-20 14:05:36 -05002232 } else if (kAlpha_8_GrPixelConfig == dstAsConfig) {
Brian Salomon9b009bb2018-02-14 13:53:55 -05002233 // onReadPixels implements a fallback for cases where we want to read kAlpha_8,
bsalomone9573312016-01-25 14:33:25 -08002234 // it's unsupported, but 32bit RGBA reads are supported.
Brian Salomon9b009bb2018-02-14 13:53:55 -05002235 if (!this->readPixelsSupported(srcSurface, kRGBA_8888_GrPixelConfig)) {
bsalomone9573312016-01-25 14:33:25 -08002236 // If we can't read RGBA from the src try to draw to a kRGBA_8888 (or kSRGBA_8888)
2237 // first and then onReadPixels will read that to a 32bit temporary buffer.
Brian Salomon9b009bb2018-02-14 13:53:55 -05002238 if (this->glCaps().canConfigBeFBOColorAttachment(kRGBA_8888_GrPixelConfig)) {
bsalomone9573312016-01-25 14:33:25 -08002239 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
Brian Salomon9b009bb2018-02-14 13:53:55 -05002240 tempDrawInfo->fTempSurfaceDesc.fConfig = kRGBA_8888_GrPixelConfig;
Brian Salomonc320b152018-02-20 14:05:36 -05002241 tempDrawInfo->fReadColorType = GrColorType::kAlpha_8;
bsalomone9573312016-01-25 14:33:25 -08002242 } else {
2243 return false;
2244 }
2245 } else {
Brian Salomon9b009bb2018-02-14 13:53:55 -05002246 tempDrawInfo->fTempSurfaceDesc.fConfig = srcConfig;
Brian Salomonc320b152018-02-20 14:05:36 -05002247 SkASSERT(tempDrawInfo->fReadColorType == GrColorType::kAlpha_8);
bsalomone9573312016-01-25 14:33:25 -08002248 }
Brian Salomonc320b152018-02-20 14:05:36 -05002249 } else if (kRGBA_half_GrPixelConfig == dstAsConfig &&
Stan Iliev0078ab22017-11-08 14:16:26 -05002250 this->readPixelsSupported(srcSurface, kRGBA_float_GrPixelConfig)) {
2251 // If reading in half float format is not supported, then read in float format.
2252 return true;
Brian Salomonc320b152018-02-20 14:05:36 -05002253 } else if (this->glCaps().canConfigBeFBOColorAttachment(dstAsConfig) &&
2254 this->readPixelsSupported(dstAsConfig, dstAsConfig)) {
bsalomone6d665e2016-03-10 07:22:25 -08002255 // Do a draw to convert from the src config to the read config.
2256 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
Brian Salomonc320b152018-02-20 14:05:36 -05002257 tempDrawInfo->fTempSurfaceDesc.fConfig = dstAsConfig;
2258 tempDrawInfo->fReadColorType = dstColorType;
bsalomon1aa20292016-01-22 08:16:09 -08002259 } else {
2260 return false;
2261 }
bsalomon39826022015-07-23 08:07:21 -07002262 }
2263
Brian Salomon71d9d842016-11-03 13:42:00 -04002264 if ((srcSurface->asRenderTarget() || this->glCaps().canConfigBeFBOColorAttachment(srcConfig)) &&
Brian Salomonc320b152018-02-20 14:05:36 -05002265 read_pixels_pays_for_y_flip(srcOrigin, this->glCaps(), width, height, dstAsConfig,
Brian Salomon71d9d842016-11-03 13:42:00 -04002266 rowBytes)) {
bsalomonf0674512015-07-28 13:26:15 -07002267 ElevateDrawPreference(drawPreference, kGpuPrefersDraw_DrawPreference);
bsalomon39826022015-07-23 08:07:21 -07002268 }
2269
bsalomon39826022015-07-23 08:07:21 -07002270 return true;
bsalomon@google.comc4364992011-11-07 15:54:49 +00002271}
2272
Brian Salomonc320b152018-02-20 14:05:36 -05002273bool GrGLGpu::onReadPixels(GrSurface* surface, GrSurfaceOrigin origin, int left, int top, int width,
2274 int height, GrColorType dstColorType, void* buffer, size_t rowBytes) {
bsalomon6cb3cbe2015-07-30 07:34:27 -07002275 SkASSERT(surface);
bsalomon39826022015-07-23 08:07:21 -07002276
bsalomone9573312016-01-25 14:33:25 -08002277 GrGLRenderTarget* renderTarget = static_cast<GrGLRenderTarget*>(surface->asRenderTarget());
Brian Salomon71d9d842016-11-03 13:42:00 -04002278 if (!renderTarget && !this->glCaps().canConfigBeFBOColorAttachment(surface->config())) {
bsalomon6cb3cbe2015-07-30 07:34:27 -07002279 return false;
2280 }
2281
Brian Salomonc320b152018-02-20 14:05:36 -05002282 // TODO: Avoid this conversion by making GrGLCaps work with color types.
2283 auto dstAsConfig = GrColorTypeToPixelConfig(dstColorType, GrSRGBEncoded::kNo);
2284
bsalomone9573312016-01-25 14:33:25 -08002285 // We have a special case fallback for reading eight bit alpha. We will read back all four 8
2286 // bit channels as RGBA and then extract A.
Brian Salomonc320b152018-02-20 14:05:36 -05002287 if (!this->readPixelsSupported(surface, dstAsConfig)) {
2288 if (kAlpha_8_GrPixelConfig == dstAsConfig &&
2289 this->readPixelsSupported(surface, kRGBA_8888_GrPixelConfig)) {
Ben Wagner7ecc5962016-11-02 17:07:33 -04002290 std::unique_ptr<uint32_t[]> temp(new uint32_t[width * height * 4]);
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002291 if (this->onReadPixels(surface, origin, left, top, width, height,
Brian Salomonc320b152018-02-20 14:05:36 -05002292 GrColorType::kRGBA_8888, temp.get(), width * 4)) {
bsalomone9573312016-01-25 14:33:25 -08002293 uint8_t* dst = reinterpret_cast<uint8_t*>(buffer);
2294 for (int j = 0; j < height; ++j) {
2295 for (int i = 0; i < width; ++i) {
2296 dst[j*rowBytes + i] = (0xFF000000U & temp[j*width+i]) >> 24;
2297 }
2298 }
2299 return true;
2300 }
2301 }
Stan Iliev0078ab22017-11-08 14:16:26 -05002302
2303 // If reading in half float format is not supported, then read in a temporary float buffer
2304 // and convert to half float.
Brian Salomonc320b152018-02-20 14:05:36 -05002305 if (kRGBA_half_GrPixelConfig == dstAsConfig &&
Stan Iliev0078ab22017-11-08 14:16:26 -05002306 this->readPixelsSupported(surface, kRGBA_float_GrPixelConfig)) {
2307 std::unique_ptr<float[]> temp(new float[width * height * 4]);
2308 if (this->onReadPixels(surface, origin, left, top, width, height,
Brian Salomonc320b152018-02-20 14:05:36 -05002309 GrColorType::kRGBA_F32, temp.get(), width * sizeof(float) * 4)) {
Stan Iliev0078ab22017-11-08 14:16:26 -05002310 uint8_t* dst = reinterpret_cast<uint8_t*>(buffer);
2311 float* src = temp.get();
2312 for (int j = 0; j < height; ++j) {
2313 SkHalf* dstRow = reinterpret_cast<SkHalf*>(dst);
2314 for (int i = 0; i < width; ++i) {
2315 for (int color = 0; color < 4; color++) {
2316 *dstRow++ = SkFloatToHalf(*src++);
2317 }
2318 }
2319 dst += rowBytes;
2320 }
2321 return true;
2322 }
2323 }
bsalomon16921ec2015-07-30 15:34:56 -07002324 return false;
2325 }
2326
bsalomon76148af2016-01-12 11:13:47 -08002327 GrGLenum externalFormat;
2328 GrGLenum externalType;
Brian Salomonc320b152018-02-20 14:05:36 -05002329 if (!this->glCaps().getReadPixelsFormat(surface->config(), dstAsConfig, &externalFormat,
bsalomon76148af2016-01-12 11:13:47 -08002330 &externalType)) {
2331 return false;
2332 }
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002333 bool flipY = kBottomLeft_GrSurfaceOrigin == origin;
bsalomon@google.comc4364992011-11-07 15:54:49 +00002334
Brian Salomon71d9d842016-11-03 13:42:00 -04002335 GrGLIRect glvp;
2336 if (renderTarget) {
2337 // resolve the render target if necessary
2338 switch (renderTarget->getResolveType()) {
2339 case GrGLRenderTarget::kCantResolve_ResolveType:
2340 return false;
2341 case GrGLRenderTarget::kAutoResolves_ResolveType:
Brian Salomon1fabd512018-02-09 09:54:25 -05002342 this->flushRenderTargetNoColorWrites(renderTarget);
Brian Salomon71d9d842016-11-03 13:42:00 -04002343 break;
2344 case GrGLRenderTarget::kCanResolve_ResolveType:
Brian Salomon1fabd512018-02-09 09:54:25 -05002345 this->onResolveRenderTarget(renderTarget);
Brian Salomon71d9d842016-11-03 13:42:00 -04002346 // we don't track the state of the READ FBO ID.
2347 fStats.incRenderTargetBinds();
2348 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER, renderTarget->textureFBOID()));
2349 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) {
bsalomon1e0bf7e2015-03-14 12:08:51 -07002473 fStats.incRenderTargetBinds();
egdanield803f272015-03-18 13:01:52 -07002474 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, target->renderFBOID()));
2475#ifdef SK_DEBUG
2476 // don't do this check in Chromium -- this is causing
2477 // lots of repeated command buffer flushes when the compositor is
2478 // rendering with Ganesh, which is really slow; even too slow for
2479 // Debug mode.
cdalton1acea862015-06-02 13:05:52 -07002480 if (kChromium_GrGLDriver != this->glContext().driver()) {
egdanield803f272015-03-18 13:01:52 -07002481 GrGLenum status;
2482 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
2483 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
2484 SkDebugf("GrGLGpu::flushRenderTarget glCheckFramebufferStatus %x\n", status);
2485 }
bsalomon160f24c2015-03-17 15:55:42 -07002486 }
egdanield803f272015-03-18 13:01:52 -07002487#endif
2488 fHWBoundRenderTargetUniqueID = rtID;
bsalomon083617b2016-02-12 12:10:14 -08002489 this->flushViewport(target->getViewport());
brianosman64d094d2016-03-25 06:01:59 -07002490 }
2491
brianosman35b784d2016-05-05 11:52:53 -07002492 if (this->glCaps().srgbWriteControl()) {
brianosman33f6b3f2016-06-02 05:49:21 -07002493 this->flushFramebufferSRGB(GrPixelConfigIsSRGB(target->config()) && !disableSRGB);
bsalomon5cd020f2015-03-17 12:46:56 -07002494 }
bsalomon083617b2016-02-12 12:10:14 -08002495}
bsalomona9909122016-01-23 10:41:40 -08002496
brianosman33f6b3f2016-06-02 05:49:21 -07002497void GrGLGpu::flushFramebufferSRGB(bool enable) {
2498 if (enable && kYes_TriState != fHWSRGBFramebuffer) {
2499 GL_CALL(Enable(GR_GL_FRAMEBUFFER_SRGB));
2500 fHWSRGBFramebuffer = kYes_TriState;
2501 } else if (!enable && kNo_TriState != fHWSRGBFramebuffer) {
2502 GL_CALL(Disable(GR_GL_FRAMEBUFFER_SRGB));
2503 fHWSRGBFramebuffer = kNo_TriState;
2504 }
2505}
2506
bsalomon083617b2016-02-12 12:10:14 -08002507void GrGLGpu::flushViewport(const GrGLIRect& viewport) {
2508 if (fHWViewport != viewport) {
2509 viewport.pushToGLViewport(this->glInterface());
2510 fHWViewport = viewport;
2511 }
2512}
2513
bsalomon@google.comd302f142011-03-03 13:54:13 +00002514#define SWAP_PER_DRAW 0
2515
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00002516#if SWAP_PER_DRAW
commit-bot@chromium.org43823302013-09-25 20:57:51 +00002517 #if defined(SK_BUILD_FOR_MAC)
bsalomon@google.comd302f142011-03-03 13:54:13 +00002518 #include <AGL/agl.h>
Mike Klein8f11d4d2018-01-24 12:42:55 -05002519 #elif defined(SK_BUILD_FOR_WIN)
bsalomon@google.comce7357d2012-06-25 17:49:25 +00002520 #include <gl/GL.h>
bsalomon@google.comd302f142011-03-03 13:54:13 +00002521 void SwapBuf() {
2522 DWORD procID = GetCurrentProcessId();
2523 HWND hwnd = GetTopWindow(GetDesktopWindow());
2524 while(hwnd) {
2525 DWORD wndProcID = 0;
2526 GetWindowThreadProcessId(hwnd, &wndProcID);
2527 if(wndProcID == procID) {
2528 SwapBuffers(GetDC(hwnd));
2529 }
2530 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
2531 }
2532 }
2533 #endif
2534#endif
2535
egdaniel9cb63402016-06-23 08:37:05 -07002536void GrGLGpu::draw(const GrPipeline& pipeline,
2537 const GrPrimitiveProcessor& primProc,
bsalomon2eda5b32016-09-21 10:53:24 -07002538 const GrMesh meshes[],
Chris Dalton46983b72017-06-06 12:27:16 -06002539 const GrPipeline::DynamicState dynamicStates[],
egdaniel9cb63402016-06-23 08:37:05 -07002540 int meshCount) {
2541 this->handleDirtyContext();
2542
bsalomon2eda5b32016-09-21 10:53:24 -07002543 bool hasPoints = false;
2544 for (int i = 0; i < meshCount; ++i) {
Chris Dalton3809bab2017-06-13 10:55:06 -06002545 if (meshes[i].primitiveType() == GrPrimitiveType::kPoints) {
bsalomon2eda5b32016-09-21 10:53:24 -07002546 hasPoints = true;
2547 break;
2548 }
2549 }
2550 if (!this->flushGLState(pipeline, primProc, hasPoints)) {
bsalomond95263c2014-12-16 13:05:12 -08002551 return;
2552 }
ethannicholas22793252016-01-30 09:59:10 -08002553
egdaniel0e1853c2016-03-17 11:35:45 -07002554 for (int i = 0; i < meshCount; ++i) {
2555 if (GrXferBarrierType barrierType = pipeline.xferBarrierType(*this->caps())) {
Robert Phillips2890fbf2017-07-26 15:48:41 -04002556 this->xferBarrier(pipeline.renderTarget(), barrierType);
egdaniel0e1853c2016-03-17 11:35:45 -07002557 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002558
Chris Dalton46983b72017-06-06 12:27:16 -06002559 if (dynamicStates) {
2560 if (pipeline.getScissorState().enabled()) {
Robert Phillips2890fbf2017-07-26 15:48:41 -04002561 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(pipeline.renderTarget());
Chris Dalton46983b72017-06-06 12:27:16 -06002562 this->flushScissor(dynamicStates[i].fScissorRect,
Robert Phillips16d8ec62017-07-27 16:16:25 -04002563 glRT->getViewport(), pipeline.proxy()->origin());
Chris Dalton46983b72017-06-06 12:27:16 -06002564 }
2565 }
Brian Salomon6d9c88b2017-06-12 10:24:42 -04002566 if (this->glCaps().requiresCullFaceEnableDisableWhenDrawingLinesAfterNonLines() &&
2567 GrIsPrimTypeLines(meshes[i].primitiveType()) &&
2568 !GrIsPrimTypeLines(fLastPrimitiveType)) {
2569 GL_CALL(Enable(GR_GL_CULL_FACE));
2570 GL_CALL(Disable(GR_GL_CULL_FACE));
2571 }
Chris Dalton114a3c02017-05-26 15:17:19 -06002572 meshes[i].sendToGpu(primProc, this);
Brian Salomon6d9c88b2017-06-12 10:24:42 -04002573 fLastPrimitiveType = meshes[i].primitiveType();
bsalomon@google.com74749cd2013-01-30 16:12:41 +00002574 }
ethannicholas22793252016-01-30 09:59:10 -08002575
bsalomon@google.comd302f142011-03-03 13:54:13 +00002576#if SWAP_PER_DRAW
2577 glFlush();
commit-bot@chromium.org43823302013-09-25 20:57:51 +00002578 #if defined(SK_BUILD_FOR_MAC)
bsalomon@google.comd302f142011-03-03 13:54:13 +00002579 aglSwapBuffers(aglGetCurrentContext());
2580 int set_a_break_pt_here = 9;
2581 aglSwapBuffers(aglGetCurrentContext());
Mike Klein8f11d4d2018-01-24 12:42:55 -05002582 #elif defined(SK_BUILD_FOR_WIN)
bsalomon@google.comd302f142011-03-03 13:54:13 +00002583 SwapBuf();
2584 int set_a_break_pt_here = 9;
2585 SwapBuf();
2586 #endif
2587#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00002588}
2589
Chris Dalton3809bab2017-06-13 10:55:06 -06002590static GrGLenum gr_primitive_type_to_gl_mode(GrPrimitiveType primitiveType) {
2591 switch (primitiveType) {
2592 case GrPrimitiveType::kTriangles:
2593 return GR_GL_TRIANGLES;
2594 case GrPrimitiveType::kTriangleStrip:
2595 return GR_GL_TRIANGLE_STRIP;
2596 case GrPrimitiveType::kTriangleFan:
2597 return GR_GL_TRIANGLE_FAN;
2598 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 }
2621 fStats.incNumDraws();
2622}
2623
2624void GrGLGpu::sendIndexedMeshToGpu(const GrPrimitiveProcessor& primProc,
2625 GrPrimitiveType primitiveType, const GrBuffer* indexBuffer,
2626 int indexCount, int baseIndex, uint16_t minIndexValue,
2627 uint16_t maxIndexValue, const GrBuffer* vertexBuffer,
2628 int baseVertex) {
Chris Dalton3809bab2017-06-13 10:55:06 -06002629 const GrGLenum glPrimType = gr_primitive_type_to_gl_mode(primitiveType);
Chris Dalton114a3c02017-05-26 15:17:19 -06002630 GrGLvoid* const indices = reinterpret_cast<void*>(indexBuffer->baseOffset() +
2631 sizeof(uint16_t) * baseIndex);
2632
Chris Dalton1d616352017-05-31 12:51:23 -06002633 this->setupGeometry(primProc, indexBuffer, vertexBuffer, baseVertex, nullptr, 0);
Chris Dalton114a3c02017-05-26 15:17:19 -06002634
2635 if (this->glCaps().drawRangeElementsSupport()) {
2636 GL_CALL(DrawRangeElements(glPrimType, minIndexValue, maxIndexValue, indexCount,
2637 GR_GL_UNSIGNED_SHORT, indices));
2638 } else {
2639 GL_CALL(DrawElements(glPrimType, indexCount, GR_GL_UNSIGNED_SHORT, indices));
2640 }
2641 fStats.incNumDraws();
2642}
2643
Chris Dalton1d616352017-05-31 12:51:23 -06002644void GrGLGpu::sendInstancedMeshToGpu(const GrPrimitiveProcessor& primProc, GrPrimitiveType
2645 primitiveType, const GrBuffer* vertexBuffer,
2646 int vertexCount, int baseVertex,
2647 const GrBuffer* instanceBuffer, int instanceCount,
2648 int baseInstance) {
Chris Daltoncc604e52017-10-06 16:27:32 -06002649 GrGLenum glPrimType = gr_primitive_type_to_gl_mode(primitiveType);
2650 int maxInstances = this->glCaps().maxInstancesPerDrawArraysWithoutCrashing(instanceCount);
2651 for (int i = 0; i < instanceCount; i += maxInstances) {
2652 this->setupGeometry(primProc, nullptr, vertexBuffer, 0, instanceBuffer, baseInstance + i);
2653 GL_CALL(DrawArraysInstanced(glPrimType, baseVertex, vertexCount,
2654 SkTMin(instanceCount - i, maxInstances)));
2655 fStats.incNumDraws();
2656 }
Chris Dalton1d616352017-05-31 12:51:23 -06002657}
2658
2659void GrGLGpu::sendIndexedInstancedMeshToGpu(const GrPrimitiveProcessor& primProc,
2660 GrPrimitiveType primitiveType,
2661 const GrBuffer* indexBuffer, int indexCount,
2662 int baseIndex, const GrBuffer* vertexBuffer,
2663 int baseVertex, const GrBuffer* instanceBuffer,
2664 int instanceCount, int baseInstance) {
Chris Dalton3809bab2017-06-13 10:55:06 -06002665 const GrGLenum glPrimType = gr_primitive_type_to_gl_mode(primitiveType);
Chris Dalton1d616352017-05-31 12:51:23 -06002666 GrGLvoid* indices = reinterpret_cast<void*>(indexBuffer->baseOffset() +
2667 sizeof(uint16_t) * baseIndex);
2668 this->setupGeometry(primProc, indexBuffer, vertexBuffer, baseVertex,
2669 instanceBuffer, baseInstance);
2670 GL_CALL(DrawElementsInstanced(glPrimType, indexCount, GR_GL_UNSIGNED_SHORT, indices,
2671 instanceCount));
2672 fStats.incNumDraws();
2673}
2674
Brian Salomon1fabd512018-02-09 09:54:25 -05002675void GrGLGpu::onResolveRenderTarget(GrRenderTarget* target) {
bsalomon@google.com75f9f252012-01-31 13:35:56 +00002676 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00002677 if (rt->needsResolve()) {
bsalomon@google.com347c3822013-05-01 20:10:01 +00002678 // Some extensions automatically resolves the texture when it is read.
2679 if (this->glCaps().usesMSAARenderBuffers()) {
egdanield803f272015-03-18 13:01:52 -07002680 SkASSERT(rt->textureFBOID() != rt->renderFBOID());
Brian Osmancfe83d12018-01-19 11:13:45 -05002681 SkASSERT(rt->textureFBOID() != 0 && rt->renderFBOID() != 0);
egdanield803f272015-03-18 13:01:52 -07002682 fStats.incRenderTargetBinds();
2683 fStats.incRenderTargetBinds();
2684 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER, rt->renderFBOID()));
2685 GL_CALL(BindFramebuffer(GR_GL_DRAW_FRAMEBUFFER, rt->textureFBOID()));
2686 // make sure we go through flushRenderTarget() since we've modified
2687 // the bound DRAW FBO ID.
Robert Phillips294870f2016-11-11 12:38:40 -05002688 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002689 const GrGLIRect& vp = rt->getViewport();
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00002690 const SkIRect dirtyRect = rt->getResolveRect();
Brian Salomon1fabd512018-02-09 09:54:25 -05002691 // The dirty rect tracked on the RT is always stored in the native coordinates of the
2692 // surface. Choose kTopLeft so no adjustments are made
2693 static constexpr auto kDirtyRectOrigin = kTopLeft_GrSurfaceOrigin;
bsalomon@google.com347c3822013-05-01 20:10:01 +00002694 if (GrGLCaps::kES_Apple_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002695 // Apple's extension uses the scissor as the blit bounds.
bsalomon3e791242014-12-17 13:43:13 -08002696 GrScissorState scissorState;
robertphillipse85a32d2015-02-10 08:16:55 -08002697 scissorState.set(dirtyRect);
Brian Salomon1fabd512018-02-09 09:54:25 -05002698 this->flushScissor(scissorState, vp, kDirtyRectOrigin);
csmartdalton28341fa2016-08-17 10:00:21 -07002699 this->disableWindowRectangles();
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002700 GL_CALL(ResolveMultisampleFramebuffer());
2701 } else {
Brian Salomone5e7eb12016-10-14 16:18:33 -04002702 int l, b, r, t;
2703 if (GrGLCaps::kResolveMustBeFull_BlitFrambufferFlag &
2704 this->glCaps().blitFramebufferSupportFlags()) {
2705 l = 0;
2706 b = 0;
2707 r = target->width();
2708 t = target->height();
2709 } else {
2710 GrGLIRect rect;
Brian Salomon1fabd512018-02-09 09:54:25 -05002711 rect.setRelativeTo(vp, dirtyRect, kDirtyRectOrigin);
Brian Salomone5e7eb12016-10-14 16:18:33 -04002712 l = rect.fLeft;
2713 b = rect.fBottom;
2714 r = rect.fLeft + rect.fWidth;
2715 t = rect.fBottom + rect.fHeight;
2716 }
derekf8c8f71a2014-09-16 06:24:57 -07002717
2718 // BlitFrameBuffer respects the scissor, so disable it.
joshualitt77b13072014-10-27 14:51:01 -07002719 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07002720 this->disableWindowRectangles();
Brian Salomone5e7eb12016-10-14 16:18:33 -04002721 GL_CALL(BlitFramebuffer(l, b, r, t, l, b, r, t,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002722 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00002723 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002724 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00002725 rt->flagAsResolved();
reed@google.comac10a2d2010-12-22 21:39:39 +00002726 }
2727}
2728
bsalomon@google.com411dad02012-06-05 20:24:20 +00002729namespace {
bsalomon@google.comd302f142011-03-03 13:54:13 +00002730
bsalomon@google.com411dad02012-06-05 20:24:20 +00002731
2732GrGLenum gr_to_gl_stencil_op(GrStencilOp op) {
cdalton93a379b2016-05-11 13:58:08 -07002733 static const GrGLenum gTable[kGrStencilOpCount] = {
2734 GR_GL_KEEP, // kKeep
2735 GR_GL_ZERO, // kZero
2736 GR_GL_REPLACE, // kReplace
2737 GR_GL_INVERT, // kInvert
2738 GR_GL_INCR_WRAP, // kIncWrap
2739 GR_GL_DECR_WRAP, // kDecWrap
2740 GR_GL_INCR, // kIncClamp
2741 GR_GL_DECR, // kDecClamp
bsalomon@google.com411dad02012-06-05 20:24:20 +00002742 };
cdalton93a379b2016-05-11 13:58:08 -07002743 GR_STATIC_ASSERT(0 == (int)GrStencilOp::kKeep);
2744 GR_STATIC_ASSERT(1 == (int)GrStencilOp::kZero);
2745 GR_STATIC_ASSERT(2 == (int)GrStencilOp::kReplace);
2746 GR_STATIC_ASSERT(3 == (int)GrStencilOp::kInvert);
2747 GR_STATIC_ASSERT(4 == (int)GrStencilOp::kIncWrap);
2748 GR_STATIC_ASSERT(5 == (int)GrStencilOp::kDecWrap);
2749 GR_STATIC_ASSERT(6 == (int)GrStencilOp::kIncClamp);
2750 GR_STATIC_ASSERT(7 == (int)GrStencilOp::kDecClamp);
2751 SkASSERT(op < (GrStencilOp)kGrStencilOpCount);
2752 return gTable[(int)op];
bsalomon@google.com411dad02012-06-05 20:24:20 +00002753}
2754
2755void set_gl_stencil(const GrGLInterface* gl,
cdalton93a379b2016-05-11 13:58:08 -07002756 const GrStencilSettings::Face& face,
2757 GrGLenum glFace) {
2758 GrGLenum glFunc = GrToGLStencilFunc(face.fTest);
2759 GrGLenum glFailOp = gr_to_gl_stencil_op(face.fFailOp);
2760 GrGLenum glPassOp = gr_to_gl_stencil_op(face.fPassOp);
bsalomon@google.coma3201942012-06-21 19:58:20 +00002761
cdalton93a379b2016-05-11 13:58:08 -07002762 GrGLint ref = face.fRef;
2763 GrGLint mask = face.fTestMask;
2764 GrGLint writeMask = face.fWriteMask;
bsalomon@google.com411dad02012-06-05 20:24:20 +00002765
2766 if (GR_GL_FRONT_AND_BACK == glFace) {
2767 // we call the combined func just in case separate stencil is not
2768 // supported.
2769 GR_GL_CALL(gl, StencilFunc(glFunc, ref, mask));
2770 GR_GL_CALL(gl, StencilMask(writeMask));
vbuzinovc5d58f02015-08-21 05:24:24 -07002771 GR_GL_CALL(gl, StencilOp(glFailOp, GR_GL_KEEP, glPassOp));
bsalomon@google.com411dad02012-06-05 20:24:20 +00002772 } else {
2773 GR_GL_CALL(gl, StencilFuncSeparate(glFace, glFunc, ref, mask));
2774 GR_GL_CALL(gl, StencilMaskSeparate(glFace, writeMask));
vbuzinovc5d58f02015-08-21 05:24:24 -07002775 GR_GL_CALL(gl, StencilOpSeparate(glFace, glFailOp, GR_GL_KEEP, glPassOp));
bsalomon@google.com411dad02012-06-05 20:24:20 +00002776 }
2777}
2778}
bsalomon@google.comd302f142011-03-03 13:54:13 +00002779
bsalomon3e791242014-12-17 13:43:13 -08002780void GrGLGpu::flushStencil(const GrStencilSettings& stencilSettings) {
csmartdaltonc7d85332016-10-26 10:13:46 -07002781 if (stencilSettings.isDisabled()) {
2782 this->disableStencil();
2783 } else if (fHWStencilSettings != stencilSettings) {
2784 if (kYes_TriState != fHWStencilTestEnabled) {
2785 GL_CALL(Enable(GR_GL_STENCIL_TEST));
Brian Salomonaf971de2017-06-08 16:11:33 -04002786
csmartdaltonc7d85332016-10-26 10:13:46 -07002787 fHWStencilTestEnabled = kYes_TriState;
bsalomon@google.coma3201942012-06-21 19:58:20 +00002788 }
csmartdaltonc7d85332016-10-26 10:13:46 -07002789 if (stencilSettings.isTwoSided()) {
csmartdaltonc7d85332016-10-26 10:13:46 -07002790 set_gl_stencil(this->glInterface(),
2791 stencilSettings.front(),
2792 GR_GL_FRONT);
2793 set_gl_stencil(this->glInterface(),
2794 stencilSettings.back(),
2795 GR_GL_BACK);
2796 } else {
2797 set_gl_stencil(this->glInterface(),
2798 stencilSettings.front(),
2799 GR_GL_FRONT_AND_BACK);
bsalomon@google.comd302f142011-03-03 13:54:13 +00002800 }
joshualitta58fe352014-10-27 08:39:00 -07002801 fHWStencilSettings = stencilSettings;
reed@google.comac10a2d2010-12-22 21:39:39 +00002802 }
2803}
2804
csmartdaltonc7d85332016-10-26 10:13:46 -07002805void GrGLGpu::disableStencil() {
2806 if (kNo_TriState != fHWStencilTestEnabled) {
2807 GL_CALL(Disable(GR_GL_STENCIL_TEST));
Brian Salomonaf971de2017-06-08 16:11:33 -04002808
csmartdaltonc7d85332016-10-26 10:13:46 -07002809 fHWStencilTestEnabled = kNo_TriState;
2810 fHWStencilSettings.invalidate();
2811 }
2812}
2813
cdaltonaf8bc7d2016-02-05 09:35:20 -08002814void GrGLGpu::flushHWAAState(GrRenderTarget* rt, bool useHWAA, bool stencilEnabled) {
bsalomon083617b2016-02-12 12:10:14 -08002815 // rt is only optional if useHWAA is false.
2816 SkASSERT(rt || !useHWAA);
vbuzinovdded6962015-06-12 08:59:45 -07002817 SkASSERT(!useHWAA || rt->isStencilBufferMultisampled());
bsalomon@google.com202d1392013-03-19 18:58:08 +00002818
csmartdalton2b5f2cb2016-06-10 14:06:32 -07002819 if (this->caps()->multisampleDisableSupport()) {
cdaltond0a840d2015-03-16 17:19:58 -07002820 if (useHWAA) {
2821 if (kYes_TriState != fMSAAEnabled) {
2822 GL_CALL(Enable(GR_GL_MULTISAMPLE));
2823 fMSAAEnabled = kYes_TriState;
2824 }
2825 } else {
2826 if (kNo_TriState != fMSAAEnabled) {
2827 GL_CALL(Disable(GR_GL_MULTISAMPLE));
2828 fMSAAEnabled = kNo_TriState;
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00002829 }
2830 }
2831 }
cdaltonaf8bc7d2016-02-05 09:35:20 -08002832
2833 if (0 != this->caps()->maxRasterSamples()) {
Brian Salomon7c8460e2017-05-12 11:36:10 -04002834 if (useHWAA && GrFSAAType::kMixedSamples == rt->fsaaType() && !stencilEnabled) {
cdaltonaf8bc7d2016-02-05 09:35:20 -08002835 // Since stencil is disabled and we want more samples than are in the color buffer, we
2836 // need to tell the rasterizer explicitly how many to run.
2837 if (kYes_TriState != fHWRasterMultisampleEnabled) {
2838 GL_CALL(Enable(GR_GL_RASTER_MULTISAMPLE));
2839 fHWRasterMultisampleEnabled = kYes_TriState;
2840 }
Brian Salomonbdecacf2018-02-02 20:32:49 -05002841 int numStencilSamples = rt->numStencilSamples();
2842 // convert to GL's understanding of sample counts where 0 means nonMSAA.
2843 numStencilSamples = 1 == numStencilSamples ? 0 : numStencilSamples;
2844 if (numStencilSamples != fHWNumRasterSamples) {
2845 SkASSERT(numStencilSamples <= this->caps()->maxRasterSamples());
2846 GL_CALL(RasterSamples(numStencilSamples, GR_GL_TRUE));
2847 fHWNumRasterSamples = numStencilSamples;
cdaltonaf8bc7d2016-02-05 09:35:20 -08002848 }
2849 } else {
2850 if (kNo_TriState != fHWRasterMultisampleEnabled) {
2851 GL_CALL(Disable(GR_GL_RASTER_MULTISAMPLE));
2852 fHWRasterMultisampleEnabled = kNo_TriState;
2853 }
2854 }
2855 } else {
Brian Salomon7c8460e2017-05-12 11:36:10 -04002856 SkASSERT(!useHWAA || GrFSAAType::kMixedSamples != rt->fsaaType() || stencilEnabled);
cdaltonaf8bc7d2016-02-05 09:35:20 -08002857 }
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00002858}
2859
bsalomon7f9b2e42016-01-12 13:29:26 -08002860void GrGLGpu::flushBlend(const GrXferProcessor::BlendInfo& blendInfo, const GrSwizzle& swizzle) {
egdanielb414f252014-07-29 13:15:47 -07002861 // Any optimization to disable blending should have already been applied and
cdalton8917d622015-05-06 13:40:21 -07002862 // tweaked the equation to "add" or "subtract", and the coeffs to (1, 0).
bsalomonf7cc8772015-05-11 11:21:14 -07002863
cdalton8917d622015-05-06 13:40:21 -07002864 GrBlendEquation equation = blendInfo.fEquation;
egdanielc2304142014-12-11 13:15:13 -08002865 GrBlendCoeff srcCoeff = blendInfo.fSrcBlend;
2866 GrBlendCoeff dstCoeff = blendInfo.fDstBlend;
cdalton8917d622015-05-06 13:40:21 -07002867 bool blendOff = (kAdd_GrBlendEquation == equation || kSubtract_GrBlendEquation == equation) &&
2868 kOne_GrBlendCoeff == srcCoeff && kZero_GrBlendCoeff == dstCoeff;
egdanielb414f252014-07-29 13:15:47 -07002869 if (blendOff) {
2870 if (kNo_TriState != fHWBlendState.fEnabled) {
2871 GL_CALL(Disable(GR_GL_BLEND));
joel.liang9764c402015-07-09 19:46:18 -07002872
2873 // Workaround for the ARM KHR_blend_equation_advanced blacklist issue
2874 // https://code.google.com/p/skia/issues/detail?id=3943
2875 if (kARM_GrGLVendor == this->ctxInfo().vendor() &&
2876 GrBlendEquationIsAdvanced(fHWBlendState.fEquation)) {
2877 SkASSERT(this->caps()->advancedBlendEquationSupport());
2878 // Set to any basic blending equation.
2879 GrBlendEquation blend_equation = kAdd_GrBlendEquation;
2880 GL_CALL(BlendEquation(gXfermodeEquation2Blend[blend_equation]));
2881 fHWBlendState.fEquation = blend_equation;
2882 }
2883
egdanielb414f252014-07-29 13:15:47 -07002884 fHWBlendState.fEnabled = kNo_TriState;
2885 }
cdalton8917d622015-05-06 13:40:21 -07002886 return;
2887 }
2888
2889 if (kYes_TriState != fHWBlendState.fEnabled) {
2890 GL_CALL(Enable(GR_GL_BLEND));
Brian Salomonaf971de2017-06-08 16:11:33 -04002891
cdalton8917d622015-05-06 13:40:21 -07002892 fHWBlendState.fEnabled = kYes_TriState;
2893 }
2894
2895 if (fHWBlendState.fEquation != equation) {
2896 GL_CALL(BlendEquation(gXfermodeEquation2Blend[equation]));
2897 fHWBlendState.fEquation = equation;
2898 }
2899
2900 if (GrBlendEquationIsAdvanced(equation)) {
2901 SkASSERT(this->caps()->advancedBlendEquationSupport());
2902 // Advanced equations have no other blend state.
2903 return;
2904 }
2905
bsalomone63ffef2016-02-05 07:17:34 -08002906 if (fHWBlendState.fSrcCoeff != srcCoeff || fHWBlendState.fDstCoeff != dstCoeff) {
cdalton8917d622015-05-06 13:40:21 -07002907 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
2908 gXfermodeCoeff2Blend[dstCoeff]));
2909 fHWBlendState.fSrcCoeff = srcCoeff;
2910 fHWBlendState.fDstCoeff = dstCoeff;
2911 }
2912
bsalomon7f9b2e42016-01-12 13:29:26 -08002913 if ((BlendCoeffReferencesConstant(srcCoeff) || BlendCoeffReferencesConstant(dstCoeff))) {
2914 GrColor blendConst = blendInfo.fBlendConstant;
2915 blendConst = swizzle.applyTo(blendConst);
2916 if (!fHWBlendState.fConstColorValid || fHWBlendState.fConstColor != blendConst) {
2917 GrGLfloat c[4];
2918 GrColorToRGBAFloat(blendConst, c);
2919 GL_CALL(BlendColor(c[0], c[1], c[2], c[3]));
2920 fHWBlendState.fConstColor = blendConst;
2921 fHWBlendState.fConstColorValid = true;
2922 }
bsalomon@google.com0650e812011-04-08 18:07:53 +00002923 }
2924}
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002925
Brian Salomon2bbdcc42017-09-07 12:36:34 -04002926static inline GrGLenum wrap_mode_to_gl_wrap(GrSamplerState::WrapMode wrapMode) {
2927 switch (wrapMode) {
2928 case GrSamplerState::WrapMode::kClamp:
2929 return GR_GL_CLAMP_TO_EDGE;
2930 case GrSamplerState::WrapMode::kRepeat:
2931 return GR_GL_REPEAT;
2932 case GrSamplerState::WrapMode::kMirrorRepeat:
2933 return GR_GL_MIRRORED_REPEAT;
bsalomon@google.comb8670992012-07-25 21:27:09 +00002934 };
Brian Salomon2bbdcc42017-09-07 12:36:34 -04002935 SK_ABORT("Unknown wrap mode");
2936 return 0;
bsalomon@google.comb8670992012-07-25 21:27:09 +00002937}
2938
egdanielb7e7d572015-11-04 04:23:53 -08002939static GrGLenum get_component_enum_from_char(char component) {
2940 switch (component) {
2941 case 'r':
2942 return GR_GL_RED;
2943 case 'g':
2944 return GR_GL_GREEN;
2945 case 'b':
2946 return GR_GL_BLUE;
2947 case 'a':
2948 return GR_GL_ALPHA;
2949 default:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -04002950 SK_ABORT("Unsupported component");
egdanielb7e7d572015-11-04 04:23:53 -08002951 return 0;
2952 }
2953}
2954
2955/** If texture swizzling is available using tex parameters then it is preferred over mangling
2956 the generated shader code. This potentially allows greater reuse of cached shaders. */
2957static void get_tex_param_swizzle(GrPixelConfig config,
bsalomoncdee0092016-01-08 13:20:12 -08002958 const GrGLCaps& caps,
egdanielb7e7d572015-11-04 04:23:53 -08002959 GrGLenum* glSwizzle) {
bsalomoncdee0092016-01-08 13:20:12 -08002960 const GrSwizzle& swizzle = caps.configSwizzle(config);
egdanielb7e7d572015-11-04 04:23:53 -08002961 for (int i = 0; i < 4; ++i) {
bsalomoncdee0092016-01-08 13:20:12 -08002962 glSwizzle[i] = get_component_enum_from_char(swizzle.c_str()[i]);
egdaniel574a4c12015-11-02 06:22:44 -08002963 }
2964}
2965
Brian Salomon2bbdcc42017-09-07 12:36:34 -04002966static GrGLenum filter_to_gl_mag_filter(GrSamplerState::Filter filter) {
2967 switch (filter) {
2968 case GrSamplerState::Filter::kNearest:
2969 return GR_GL_NEAREST;
2970 case GrSamplerState::Filter::kBilerp:
2971 return GR_GL_LINEAR;
2972 case GrSamplerState::Filter::kMipMap:
2973 return GR_GL_LINEAR;
2974 }
2975 SK_ABORT("Unknown filter");
2976 return 0;
2977}
2978
2979static GrGLenum filter_to_gl_min_filter(GrSamplerState::Filter filter) {
2980 switch (filter) {
2981 case GrSamplerState::Filter::kNearest:
2982 return GR_GL_NEAREST;
2983 case GrSamplerState::Filter::kBilerp:
2984 return GR_GL_LINEAR;
2985 case GrSamplerState::Filter::kMipMap:
2986 return GR_GL_LINEAR_MIPMAP_LINEAR;
2987 }
2988 SK_ABORT("Unknown filter");
2989 return 0;
2990}
2991
2992void GrGLGpu::bindTexture(int unitIdx, const GrSamplerState& samplerState, bool allowSRGBInputs,
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002993 GrGLTexture* texture, GrSurfaceOrigin textureOrigin) {
bsalomon49f085d2014-09-05 13:34:00 -07002994 SkASSERT(texture);
tomhudson@google.comd0c1a062012-07-12 17:23:52 +00002995
reed856e9d92015-09-30 12:21:45 -07002996#ifdef SK_DEBUG
2997 if (!this->caps()->npotTextureTileSupport()) {
Brian Salomon2bbdcc42017-09-07 12:36:34 -04002998 if (samplerState.isRepeated()) {
reed856e9d92015-09-30 12:21:45 -07002999 const int w = texture->width();
3000 const int h = texture->height();
3001 SkASSERT(SkIsPow2(w) && SkIsPow2(h));
3002 }
3003 }
3004#endif
3005
bsalomon@google.comb8670992012-07-25 21:27:09 +00003006 // If we created a rt/tex and rendered to it without using a texture and now we're texturing
3007 // 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 +00003008 // out of the "last != next" check.
bsalomon37dd3312014-11-03 08:47:23 -08003009 GrGLRenderTarget* texRT = static_cast<GrGLRenderTarget*>(texture->asRenderTarget());
bsalomon49f085d2014-09-05 13:34:00 -07003010 if (texRT) {
Brian Salomon1fabd512018-02-09 09:54:25 -05003011 this->onResolveRenderTarget(texRT);
bsalomon@google.com4c883782012-06-04 19:05:11 +00003012 }
3013
Robert Phillips294870f2016-11-11 12:38:40 -05003014 GrGpuResource::UniqueID textureID = texture->uniqueID();
bsalomon10528f12015-10-14 12:54:52 -07003015 GrGLenum target = texture->target();
bsalomon1c63bf62014-07-22 13:09:46 -07003016 if (fHWBoundTextureUniqueIDs[unitIdx] != textureID) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00003017 this->setTextureUnit(unitIdx);
bsalomon10528f12015-10-14 12:54:52 -07003018 GL_CALL(BindTexture(target, texture->textureID()));
bsalomon1c63bf62014-07-22 13:09:46 -07003019 fHWBoundTextureUniqueIDs[unitIdx] = textureID;
bsalomon@google.com4c883782012-06-04 19:05:11 +00003020 }
3021
bsalomon@google.com4c883782012-06-04 19:05:11 +00003022 ResetTimestamp timestamp;
bsalomon@google.com34cccde2013-01-04 18:34:30 +00003023 const GrGLTexture::TexParams& oldTexParams = texture->getCachedTexParams(&timestamp);
bsalomon@google.com4c883782012-06-04 19:05:11 +00003024 bool setAll = timestamp < this->getResetTimestamp();
3025 GrGLTexture::TexParams newTexParams;
3026
Brian Salomon2bbdcc42017-09-07 12:36:34 -04003027 GrSamplerState::Filter filterMode = samplerState.filter();
bsalomonefd7d452014-10-23 14:17:46 -07003028
Brian Salomon2bbdcc42017-09-07 12:36:34 -04003029 if (GrSamplerState::Filter::kMipMap == filterMode) {
Robert Phillips92de6312017-05-23 07:43:48 -04003030 if (!this->caps()->mipMapSupport()) {
Brian Salomon2bbdcc42017-09-07 12:36:34 -04003031 filterMode = GrSamplerState::Filter::kBilerp;
bsalomonefd7d452014-10-23 14:17:46 -07003032 }
commit-bot@chromium.org47442312013-12-19 16:18:01 +00003033 }
bsalomonefd7d452014-10-23 14:17:46 -07003034
Brian Salomon2bbdcc42017-09-07 12:36:34 -04003035 newTexParams.fMinFilter = filter_to_gl_min_filter(filterMode);
3036 newTexParams.fMagFilter = filter_to_gl_mag_filter(filterMode);
skia.committer@gmail.comaeefb2a2013-07-27 07:01:06 +00003037
brianosman20471892016-12-02 06:43:32 -08003038 if (this->glCaps().srgbDecodeDisableSupport() && GrPixelConfigIsSRGB(texture->config())) {
brianosmanfe199872016-06-13 07:59:48 -07003039 newTexParams.fSRGBDecode = allowSRGBInputs ? GR_GL_DECODE_EXT : GR_GL_SKIP_DECODE_EXT;
brianosman33f6b3f2016-06-02 05:49:21 -07003040 if (setAll || newTexParams.fSRGBDecode != oldTexParams.fSRGBDecode) {
brianosmanadf4fa62016-04-26 16:48:06 -07003041 this->setTextureUnit(unitIdx);
brianosman33f6b3f2016-06-02 05:49:21 -07003042 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SRGB_DECODE_EXT, newTexParams.fSRGBDecode));
cblume55f2d2d2016-02-26 13:20:48 -08003043 }
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +00003044 }
bsalomon@google.com4c883782012-06-04 19:05:11 +00003045
brianosman33f6b3f2016-06-02 05:49:21 -07003046#ifdef SK_DEBUG
3047 // We were supposed to ensure MipMaps were up-to-date and built correctly before getting here.
Brian Salomon2bbdcc42017-09-07 12:36:34 -04003048 if (GrSamplerState::Filter::kMipMap == filterMode) {
brianosman33f6b3f2016-06-02 05:49:21 -07003049 SkASSERT(!texture->texturePriv().mipMapsAreDirty());
3050 if (GrPixelConfigIsSRGB(texture->config())) {
Brian Osman7b8400d2016-11-08 17:08:54 -05003051 SkDestinationSurfaceColorMode colorMode = allowSRGBInputs
3052 ? SkDestinationSurfaceColorMode::kGammaAndColorSpaceAware
3053 : SkDestinationSurfaceColorMode::kLegacy;
3054 SkASSERT(texture->texturePriv().mipColorMode() == colorMode);
brianosman33f6b3f2016-06-02 05:49:21 -07003055 }
3056 }
3057#endif
3058
cblume55f2d2d2016-02-26 13:20:48 -08003059 newTexParams.fMaxMipMapLevel = texture->texturePriv().maxMipMapLevel();
3060
Brian Salomon2bbdcc42017-09-07 12:36:34 -04003061 newTexParams.fWrapS = wrap_mode_to_gl_wrap(samplerState.wrapModeX());
3062 newTexParams.fWrapT = wrap_mode_to_gl_wrap(samplerState.wrapModeY());
bsalomoncdee0092016-01-08 13:20:12 -08003063 get_tex_param_swizzle(texture->config(), this->glCaps(), newTexParams.fSwizzleRGBA);
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00003064 if (setAll || newTexParams.fMagFilter != oldTexParams.fMagFilter) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00003065 this->setTextureUnit(unitIdx);
bsalomon10528f12015-10-14 12:54:52 -07003066 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MAG_FILTER, newTexParams.fMagFilter));
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00003067 }
3068 if (setAll || newTexParams.fMinFilter != oldTexParams.fMinFilter) {
3069 this->setTextureUnit(unitIdx);
bsalomon10528f12015-10-14 12:54:52 -07003070 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MIN_FILTER, newTexParams.fMinFilter));
bsalomon@google.com4c883782012-06-04 19:05:11 +00003071 }
cblume55f2d2d2016-02-26 13:20:48 -08003072 if (setAll || newTexParams.fMaxMipMapLevel != oldTexParams.fMaxMipMapLevel) {
cblume09bd2c02016-03-01 14:08:28 -08003073 // These are not supported in ES2 contexts
3074 if (this->glCaps().mipMapLevelAndLodControlSupport()) {
3075 if (newTexParams.fMaxMipMapLevel != 0) {
3076 this->setTextureUnit(unitIdx);
3077 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MIN_LOD, 0));
3078 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_BASE_LEVEL, 0));
3079 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MAX_LOD,
3080 newTexParams.fMaxMipMapLevel));
3081 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MAX_LEVEL,
3082 newTexParams.fMaxMipMapLevel));
3083 }
cblume55f2d2d2016-02-26 13:20:48 -08003084 }
3085 }
bsalomon@google.com4c883782012-06-04 19:05:11 +00003086 if (setAll || newTexParams.fWrapS != oldTexParams.fWrapS) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00003087 this->setTextureUnit(unitIdx);
bsalomon10528f12015-10-14 12:54:52 -07003088 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_WRAP_S, newTexParams.fWrapS));
bsalomon@google.com4c883782012-06-04 19:05:11 +00003089 }
3090 if (setAll || newTexParams.fWrapT != oldTexParams.fWrapT) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00003091 this->setTextureUnit(unitIdx);
bsalomon10528f12015-10-14 12:54:52 -07003092 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_WRAP_T, newTexParams.fWrapT));
bsalomon@google.com4c883782012-06-04 19:05:11 +00003093 }
bsalomoncdee0092016-01-08 13:20:12 -08003094 if (this->glCaps().textureSwizzleSupport() &&
bsalomon@google.com4c883782012-06-04 19:05:11 +00003095 (setAll || memcmp(newTexParams.fSwizzleRGBA,
3096 oldTexParams.fSwizzleRGBA,
3097 sizeof(newTexParams.fSwizzleRGBA)))) {
cdalton74b8d322016-04-11 14:47:28 -07003098 this->setTextureSwizzle(unitIdx, target, newTexParams.fSwizzleRGBA);
bsalomon@google.com4c883782012-06-04 19:05:11 +00003099 }
bsalomon@google.com34cccde2013-01-04 18:34:30 +00003100 texture->setCachedTexParams(newTexParams, this->getResetTimestamp());
bsalomon@google.com4c883782012-06-04 19:05:11 +00003101}
3102
csmartdalton1897cfd2016-06-03 08:50:54 -07003103void GrGLGpu::bindTexelBuffer(int unitIdx, GrPixelConfig texelConfig, GrGLBuffer* buffer) {
cdalton74b8d322016-04-11 14:47:28 -07003104 SkASSERT(this->glCaps().canUseConfigWithTexelBuffer(texelConfig));
3105 SkASSERT(unitIdx >= 0 && unitIdx < fHWBufferTextures.count());
cdalton74b8d322016-04-11 14:47:28 -07003106
3107 BufferTexture& buffTex = fHWBufferTextures[unitIdx];
3108
3109 if (!buffTex.fKnownBound) {
3110 if (!buffTex.fTextureID) {
3111 GL_CALL(GenTextures(1, &buffTex.fTextureID));
3112 if (!buffTex.fTextureID) {
3113 return;
3114 }
3115 }
3116
3117 this->setTextureUnit(unitIdx);
3118 GL_CALL(BindTexture(GR_GL_TEXTURE_BUFFER, buffTex.fTextureID));
3119
3120 buffTex.fKnownBound = true;
3121 }
3122
robertphillips8abb3702016-08-31 14:04:06 -07003123 if (buffer->uniqueID() != buffTex.fAttachedBufferUniqueID ||
csmartdalton1897cfd2016-06-03 08:50:54 -07003124 buffTex.fTexelConfig != texelConfig) {
cdalton74b8d322016-04-11 14:47:28 -07003125
3126 this->setTextureUnit(unitIdx);
csmartdalton1897cfd2016-06-03 08:50:54 -07003127 GL_CALL(TexBuffer(GR_GL_TEXTURE_BUFFER,
3128 this->glCaps().configSizedInternalFormat(texelConfig),
3129 buffer->bufferID()));
cdalton74b8d322016-04-11 14:47:28 -07003130
cdalton74b8d322016-04-11 14:47:28 -07003131 buffTex.fTexelConfig = texelConfig;
robertphillips8abb3702016-08-31 14:04:06 -07003132 buffTex.fAttachedBufferUniqueID = buffer->uniqueID();
cdalton74b8d322016-04-11 14:47:28 -07003133
3134 if (this->glCaps().textureSwizzleSupport() &&
3135 this->glCaps().configSwizzle(texelConfig) != buffTex.fSwizzle) {
3136 GrGLenum glSwizzle[4];
3137 get_tex_param_swizzle(texelConfig, this->glCaps(), glSwizzle);
3138 this->setTextureSwizzle(unitIdx, GR_GL_TEXTURE_BUFFER, glSwizzle);
3139 buffTex.fSwizzle = this->glCaps().configSwizzle(texelConfig);
3140 }
3141
3142 buffer->setHasAttachedToTexture();
3143 fHWMaxUsedBufferTextureUnit = SkTMax(unitIdx, fHWMaxUsedBufferTextureUnit);
3144 }
3145}
3146
Brian Salomon2bbdcc42017-09-07 12:36:34 -04003147void GrGLGpu::generateMipmaps(const GrSamplerState& params, bool allowSRGBInputs,
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003148 GrGLTexture* texture, GrSurfaceOrigin textureOrigin) {
brianosman33f6b3f2016-06-02 05:49:21 -07003149 SkASSERT(texture);
3150
3151 // First, figure out if we need mips for this texture at all:
Brian Salomon2bbdcc42017-09-07 12:36:34 -04003152 GrSamplerState::Filter filterMode = params.filter();
brianosman33f6b3f2016-06-02 05:49:21 -07003153
Brian Salomon2bbdcc42017-09-07 12:36:34 -04003154 if (GrSamplerState::Filter::kMipMap == filterMode) {
Robert Phillips92de6312017-05-23 07:43:48 -04003155 if (!this->caps()->mipMapSupport()) {
Brian Salomon2bbdcc42017-09-07 12:36:34 -04003156 filterMode = GrSamplerState::Filter::kBilerp;
brianosman33f6b3f2016-06-02 05:49:21 -07003157 }
3158 }
3159
Brian Salomon2bbdcc42017-09-07 12:36:34 -04003160 if (GrSamplerState::Filter::kMipMap != filterMode) {
brianosman33f6b3f2016-06-02 05:49:21 -07003161 return;
3162 }
3163
3164 // If this is an sRGB texture and the mips were previously built the "other" way
3165 // (gamma-correct vs. not), then we need to rebuild them. We don't need to check for
3166 // srgbSupport - we'll *never* get an sRGB pixel config if we don't support it.
Brian Osman7b8400d2016-11-08 17:08:54 -05003167 SkDestinationSurfaceColorMode colorMode = allowSRGBInputs
3168 ? SkDestinationSurfaceColorMode::kGammaAndColorSpaceAware
3169 : SkDestinationSurfaceColorMode::kLegacy;
brianosman33f6b3f2016-06-02 05:49:21 -07003170 if (GrPixelConfigIsSRGB(texture->config()) &&
Brian Osman7b8400d2016-11-08 17:08:54 -05003171 colorMode != texture->texturePriv().mipColorMode()) {
Greg Daniel0fc4d2d2017-10-12 11:23:36 -04003172 texture->texturePriv().markMipMapsDirty();
brianosman33f6b3f2016-06-02 05:49:21 -07003173 }
3174
3175 // If the mips aren't dirty, we're done:
3176 if (!texture->texturePriv().mipMapsAreDirty()) {
3177 return;
3178 }
3179
3180 // If we created a rt/tex and rendered to it without using a texture and now we're texturing
3181 // from the rt it will still be the last bound texture, but it needs resolving.
3182 GrGLRenderTarget* texRT = static_cast<GrGLRenderTarget*>(texture->asRenderTarget());
3183 if (texRT) {
Brian Salomon1fabd512018-02-09 09:54:25 -05003184 this->onResolveRenderTarget(texRT);
brianosman33f6b3f2016-06-02 05:49:21 -07003185 }
3186
3187 GrGLenum target = texture->target();
3188 this->setScratchTextureUnit();
3189 GL_CALL(BindTexture(target, texture->textureID()));
3190
3191 // Configure sRGB decode, if necessary. This state is the only thing needed for the driver
3192 // call (glGenerateMipmap) to work correctly. Our manual method dirties other state, too.
brianosman20471892016-12-02 06:43:32 -08003193 if (this->glCaps().srgbDecodeDisableSupport() && GrPixelConfigIsSRGB(texture->config())) {
Brian Osmanb0403202016-12-09 10:55:23 -05003194 GrGLenum srgbDecode = allowSRGBInputs ? GR_GL_DECODE_EXT : GR_GL_SKIP_DECODE_EXT;
3195 // Command buffer's sRGB decode extension doesn't influence mipmap generation correctly.
3196 // If we set this to skip_decode, it appears to suppress sRGB -> Linear for each downsample,
3197 // but not the Linear -> sRGB when writing the next level. The result is that mip-chains
3198 // get progressively brighter as you go down. Forcing this to 'decode' gives predictable
3199 // (and only slightly incorrect) results. See crbug.com/655247 (~comment 28)
3200 if (!this->glCaps().srgbDecodeDisableAffectsMipmaps()) {
3201 srgbDecode = GR_GL_DECODE_EXT;
3202 }
3203 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SRGB_DECODE_EXT, srgbDecode));
brianosman33f6b3f2016-06-02 05:49:21 -07003204 }
3205
3206 // Either do manual mipmap generation or (if that fails), just rely on the driver:
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003207 if (!this->generateMipmap(texture, textureOrigin, allowSRGBInputs)) {
brianosman33f6b3f2016-06-02 05:49:21 -07003208 GL_CALL(GenerateMipmap(target));
3209 }
3210
Greg Daniel0fc4d2d2017-10-12 11:23:36 -04003211 texture->texturePriv().markMipMapsClean();
brianosman33f6b3f2016-06-02 05:49:21 -07003212 texture->texturePriv().setMaxMipMapLevel(SkMipMap::ComputeLevelCount(
3213 texture->width(), texture->height()));
Brian Osman7b8400d2016-11-08 17:08:54 -05003214 texture->texturePriv().setMipColorMode(colorMode);
brianosman33f6b3f2016-06-02 05:49:21 -07003215
3216 // We have potentially set lots of state on the texture. Easiest to dirty it all:
3217 texture->textureParamsModified();
3218}
3219
cdalton74b8d322016-04-11 14:47:28 -07003220void GrGLGpu::setTextureSwizzle(int unitIdx, GrGLenum target, const GrGLenum swizzle[]) {
3221 this->setTextureUnit(unitIdx);
3222 if (this->glStandard() == kGLES_GrGLStandard) {
3223 // ES3 added swizzle support but not GL_TEXTURE_SWIZZLE_RGBA.
3224 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_R, swizzle[0]));
3225 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_G, swizzle[1]));
3226 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_B, swizzle[2]));
3227 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_A, swizzle[3]));
3228 } else {
3229 GR_STATIC_ASSERT(sizeof(swizzle[0]) == sizeof(GrGLint));
3230 GL_CALL(TexParameteriv(target, GR_GL_TEXTURE_SWIZZLE_RGBA,
3231 reinterpret_cast<const GrGLint*>(swizzle)));
3232 }
3233}
3234
egdaniel080e6732014-12-22 07:35:52 -08003235void GrGLGpu::flushColorWrite(bool writeColor) {
3236 if (!writeColor) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +00003237 if (kNo_TriState != fHWWriteToColor) {
3238 GL_CALL(ColorMask(GR_GL_FALSE, GR_GL_FALSE,
3239 GR_GL_FALSE, GR_GL_FALSE));
3240 fHWWriteToColor = kNo_TriState;
bsalomon@google.comd302f142011-03-03 13:54:13 +00003241 }
bsalomon@google.com978c8c62012-05-21 14:45:49 +00003242 } else {
3243 if (kYes_TriState != fHWWriteToColor) {
3244 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
3245 fHWWriteToColor = kYes_TriState;
3246 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00003247 }
bsalomon3e791242014-12-17 13:43:13 -08003248}
bsalomon@google.comd302f142011-03-03 13:54:13 +00003249
bsalomon861e1032014-12-16 07:33:49 -08003250void GrGLGpu::setTextureUnit(int unit) {
bsalomon1c63bf62014-07-22 13:09:46 -07003251 SkASSERT(unit >= 0 && unit < fHWBoundTextureUniqueIDs.count());
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00003252 if (unit != fHWActiveTextureUnitIdx) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00003253 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com49209392012-06-05 15:13:46 +00003254 fHWActiveTextureUnitIdx = unit;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00003255 }
3256}
bsalomon@google.com316f99232011-01-13 21:28:12 +00003257
bsalomon861e1032014-12-16 07:33:49 -08003258void GrGLGpu::setScratchTextureUnit() {
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00003259 // Bind the last texture unit since it is the least likely to be used by GrGLProgram.
bsalomon1c63bf62014-07-22 13:09:46 -07003260 int lastUnitIdx = fHWBoundTextureUniqueIDs.count() - 1;
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00003261 if (lastUnitIdx != fHWActiveTextureUnitIdx) {
3262 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + lastUnitIdx));
3263 fHWActiveTextureUnitIdx = lastUnitIdx;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00003264 }
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00003265 // clear out the this field so that if a program does use this unit it will rebind the correct
3266 // texture.
Robert Phillips294870f2016-11-11 12:38:40 -05003267 fHWBoundTextureUniqueIDs[lastUnitIdx].makeInvalid();
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00003268}
3269
Brian Salomone5e7eb12016-10-14 16:18:33 -04003270// Determines whether glBlitFramebuffer could be used between src and dst by onCopySurface.
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003271static inline bool can_blit_framebuffer_for_copy_surface(
3272 const GrSurface* dst, GrSurfaceOrigin dstOrigin,
3273 const GrSurface* src, GrSurfaceOrigin srcOrigin,
3274 const SkIRect& srcRect,
3275 const SkIPoint& dstPoint,
3276 const GrGLGpu* gpu) {
Brian Salomone5e7eb12016-10-14 16:18:33 -04003277 auto blitFramebufferFlags = gpu->glCaps().blitFramebufferSupportFlags();
Brian Salomon71d9d842016-11-03 13:42:00 -04003278 if (!gpu->glCaps().canConfigBeFBOColorAttachment(dst->config()) ||
3279 !gpu->glCaps().canConfigBeFBOColorAttachment(src->config())) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00003280 return false;
3281 }
Brian Salomonbf7b6202016-11-11 16:08:03 -05003282 // Blits are not allowed between int color buffers and float/fixed color buffers. GrGpu should
3283 // have filtered such cases out.
Brian Salomone5e7eb12016-10-14 16:18:33 -04003284 const GrGLTexture* dstTex = static_cast<const GrGLTexture*>(dst->asTexture());
Mike Klein1d746202018-01-25 17:32:51 -05003285 const GrGLTexture* srcTex = static_cast<const GrGLTexture*>(src->asTexture());
Brian Salomone5e7eb12016-10-14 16:18:33 -04003286 const GrRenderTarget* dstRT = dst->asRenderTarget();
3287 const GrRenderTarget* srcRT = src->asRenderTarget();
3288 if (dstTex && dstTex->target() != GR_GL_TEXTURE_2D) {
3289 return false;
3290 }
3291 if (srcTex && srcTex->target() != GR_GL_TEXTURE_2D) {
3292 return false;
3293 }
3294 if (GrGLCaps::kNoSupport_BlitFramebufferFlag & blitFramebufferFlags) {
3295 return false;
3296 }
3297 if (GrGLCaps::kNoScalingOrMirroring_BlitFramebufferFlag & blitFramebufferFlags) {
3298 // We would mirror to compensate for origin changes. Note that copySurface is
3299 // specified such that the src and dst rects are the same.
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003300 if (dstOrigin != srcOrigin) {
Brian Salomone5e7eb12016-10-14 16:18:33 -04003301 return false;
3302 }
3303 }
3304 if (GrGLCaps::kResolveMustBeFull_BlitFrambufferFlag & blitFramebufferFlags) {
Brian Salomonbdecacf2018-02-02 20:32:49 -05003305 if (srcRT && srcRT->numColorSamples() > 1) {
3306 if (dstRT && 1 == dstRT->numColorSamples()) {
Eric Karl74480882017-04-03 14:49:05 -07003307 return false;
3308 }
3309 if (SkRect::Make(srcRect) != srcRT->getBoundsRect()) {
3310 return false;
3311 }
Brian Salomone5e7eb12016-10-14 16:18:33 -04003312 }
3313 }
3314 if (GrGLCaps::kNoMSAADst_BlitFramebufferFlag & blitFramebufferFlags) {
Brian Salomonbdecacf2018-02-02 20:32:49 -05003315 if (dstRT && dstRT->numColorSamples() > 1) {
Brian Salomone5e7eb12016-10-14 16:18:33 -04003316 return false;
3317 }
Ben Wagner63fd7602017-10-09 15:45:33 -04003318 }
Brian Salomone5e7eb12016-10-14 16:18:33 -04003319 if (GrGLCaps::kNoFormatConversion_BlitFramebufferFlag & blitFramebufferFlags) {
3320 if (dst->config() != src->config()) {
3321 return false;
3322 }
3323 } else if (GrGLCaps::kNoFormatConversionForMSAASrc_BlitFramebufferFlag & blitFramebufferFlags) {
3324 const GrRenderTarget* srcRT = src->asRenderTarget();
Brian Salomonbdecacf2018-02-02 20:32:49 -05003325 if (srcRT && srcRT->numColorSamples() > 1 && dst->config() != src->config()) {
Brian Salomone5e7eb12016-10-14 16:18:33 -04003326 return false;
3327 }
3328 }
3329 if (GrGLCaps::kRectsMustMatchForMSAASrc_BlitFramebufferFlag & blitFramebufferFlags) {
Brian Salomonbdecacf2018-02-02 20:32:49 -05003330 if (srcRT && srcRT->numColorSamples() > 1) {
Eric Karl74480882017-04-03 14:49:05 -07003331 if (dstPoint.fX != srcRect.fLeft || dstPoint.fY != srcRect.fTop) {
3332 return false;
3333 }
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003334 if (dstOrigin != srcOrigin) {
Eric Karl74480882017-04-03 14:49:05 -07003335 return false;
3336 }
Brian Salomone5e7eb12016-10-14 16:18:33 -04003337 }
3338 }
3339 return true;
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00003340}
bsalomon@google.comeb851172013-04-15 13:51:00 +00003341
Brian Osmana9c8a052018-01-19 10:31:56 -05003342static bool rt_has_msaa_render_buffer(const GrGLRenderTarget* rt, const GrGLCaps& glCaps) {
3343 // A RT has a separate MSAA renderbuffer if:
3344 // 1) It's multisampled
3345 // 2) We're using an extension with separate MSAA renderbuffers
3346 // 3) It's not FBO 0, which is special and always auto-resolves
Brian Salomonbdecacf2018-02-02 20:32:49 -05003347 return rt->numColorSamples() > 1 && glCaps.usesMSAARenderBuffers() && rt->renderFBOID() != 0;
Brian Osmana9c8a052018-01-19 10:31:56 -05003348}
3349
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003350static inline bool can_copy_texsubimage(const GrSurface* dst, GrSurfaceOrigin dstOrigin,
3351 const GrSurface* src, GrSurfaceOrigin srcOrigin,
bsalomon7ea33f52015-11-22 14:51:00 -08003352 const GrGLGpu* gpu) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00003353 // Table 3.9 of the ES2 spec indicates the supported formats with CopyTexSubImage
3354 // and BGRA isn't in the spec. There doesn't appear to be any extension that adds it. Perhaps
3355 // many drivers would allow it to work, but ANGLE does not.
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +00003356 if (kGLES_GrGLStandard == gpu->glStandard() && gpu->glCaps().bgraIsInternalFormat() &&
bsalomon@google.comeb851172013-04-15 13:51:00 +00003357 (kBGRA_8888_GrPixelConfig == dst->config() || kBGRA_8888_GrPixelConfig == src->config())) {
3358 return false;
3359 }
3360 const GrGLRenderTarget* dstRT = static_cast<const GrGLRenderTarget*>(dst->asRenderTarget());
3361 // If dst is multisampled (and uses an extension where there is a separate MSAA renderbuffer)
3362 // then we don't want to copy to the texture but to the MSAA buffer.
Brian Osmana9c8a052018-01-19 10:31:56 -05003363 if (dstRT && rt_has_msaa_render_buffer(dstRT, gpu->glCaps())) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00003364 return false;
3365 }
bsalomon@google.coma2719852013-04-17 14:25:27 +00003366 const GrGLRenderTarget* srcRT = static_cast<const GrGLRenderTarget*>(src->asRenderTarget());
3367 // If the src is multisampled (and uses an extension where there is a separate MSAA
3368 // renderbuffer) then it is an invalid operation to call CopyTexSubImage
Brian Osmana9c8a052018-01-19 10:31:56 -05003369 if (srcRT && rt_has_msaa_render_buffer(srcRT, gpu->glCaps())) {
bsalomon@google.coma2719852013-04-17 14:25:27 +00003370 return false;
3371 }
bsalomon7ea33f52015-11-22 14:51:00 -08003372
3373 const GrGLTexture* dstTex = static_cast<const GrGLTexture*>(dst->asTexture());
3374 // CopyTex(Sub)Image writes to a texture and we have no way of dynamically wrapping a RT in a
3375 // texture.
3376 if (!dstTex) {
3377 return false;
3378 }
3379
3380 const GrGLTexture* srcTex = static_cast<const GrGLTexture*>(src->asTexture());
cblume61214052016-01-26 09:10:48 -08003381
bsalomon7ea33f52015-11-22 14:51:00 -08003382 // Check that we could wrap the source in an FBO, that the dst is TEXTURE_2D, that no mirroring
3383 // is required.
Brian Salomon71d9d842016-11-03 13:42:00 -04003384 if (gpu->glCaps().canConfigBeFBOColorAttachment(src->config()) &&
Brian Salomon71d9d842016-11-03 13:42:00 -04003385 (!srcTex || srcTex->target() == GR_GL_TEXTURE_2D) && dstTex->target() == GR_GL_TEXTURE_2D &&
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003386 dstOrigin == srcOrigin) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00003387 return true;
3388 } else {
3389 return false;
3390 }
3391}
3392
3393// If a temporary FBO was created, its non-zero ID is returned. The viewport that the copy rect is
3394// relative to is output.
Brian Salomon71d9d842016-11-03 13:42:00 -04003395void GrGLGpu::bindSurfaceFBOForPixelOps(GrSurface* surface, GrGLenum fboTarget, GrGLIRect* viewport,
3396 TempFBOTarget tempFBOTarget) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00003397 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(surface->asRenderTarget());
bsalomon083617b2016-02-12 12:10:14 -08003398 if (!rt) {
bsalomon49f085d2014-09-05 13:34:00 -07003399 SkASSERT(surface->asTexture());
Brian Salomon9bada542017-06-12 12:09:30 -04003400 GrGLTexture* texture = static_cast<GrGLTexture*>(surface->asTexture());
3401 GrGLuint texID = texture->textureID();
3402 GrGLenum target = texture->target();
egdanield803f272015-03-18 13:01:52 -07003403 GrGLuint* tempFBOID;
3404 tempFBOID = kSrc_TempFBOTarget == tempFBOTarget ? &fTempSrcFBOID : &fTempDstFBOID;
egdaniel0f5f9672015-02-03 11:10:51 -08003405
egdanield803f272015-03-18 13:01:52 -07003406 if (0 == *tempFBOID) {
3407 GR_GL_CALL(this->glInterface(), GenFramebuffers(1, tempFBOID));
egdaniel0f5f9672015-02-03 11:10:51 -08003408 }
3409
egdanield803f272015-03-18 13:01:52 -07003410 fStats.incRenderTargetBinds();
3411 GR_GL_CALL(this->glInterface(), BindFramebuffer(fboTarget, *tempFBOID));
3412 GR_GL_CALL(this->glInterface(), FramebufferTexture2D(fboTarget,
robertphillips754f4e92014-09-18 13:52:08 -07003413 GR_GL_COLOR_ATTACHMENT0,
bsalomon10528f12015-10-14 12:54:52 -07003414 target,
robertphillips754f4e92014-09-18 13:52:08 -07003415 texID,
3416 0));
Brian Salomon9bada542017-06-12 12:09:30 -04003417 texture->baseLevelWasBoundToFBO();
bsalomon@google.comeb851172013-04-15 13:51:00 +00003418 viewport->fLeft = 0;
3419 viewport->fBottom = 0;
3420 viewport->fWidth = surface->width();
3421 viewport->fHeight = surface->height();
3422 } else {
egdanield803f272015-03-18 13:01:52 -07003423 fStats.incRenderTargetBinds();
3424 GR_GL_CALL(this->glInterface(), BindFramebuffer(fboTarget, rt->renderFBOID()));
bsalomon@google.comeb851172013-04-15 13:51:00 +00003425 *viewport = rt->getViewport();
3426 }
egdaniel0f5f9672015-02-03 11:10:51 -08003427}
3428
Brian Salomon71d9d842016-11-03 13:42:00 -04003429void GrGLGpu::unbindTextureFBOForPixelOps(GrGLenum fboTarget, GrSurface* surface) {
3430 // bindSurfaceFBOForPixelOps temporarily binds textures that are not render targets to
bsalomon10528f12015-10-14 12:54:52 -07003431 if (!surface->asRenderTarget()) {
3432 SkASSERT(surface->asTexture());
3433 GrGLenum textureTarget = static_cast<GrGLTexture*>(surface->asTexture())->target();
3434 GR_GL_CALL(this->glInterface(), FramebufferTexture2D(fboTarget,
3435 GR_GL_COLOR_ATTACHMENT0,
3436 textureTarget,
3437 0,
3438 0));
3439 }
bsalomon@google.comeb851172013-04-15 13:51:00 +00003440}
3441
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003442bool GrGLGpu::onCopySurface(GrSurface* dst, GrSurfaceOrigin dstOrigin,
3443 GrSurface* src, GrSurfaceOrigin srcOrigin,
Greg Daniel55fa6472018-03-16 16:13:10 -04003444 const SkIRect& srcRect, const SkIPoint& dstPoint,
3445 bool canDiscardOutsideDstRect) {
bsalomon7f9b2e42016-01-12 13:29:26 -08003446 // None of our copy methods can handle a swizzle. TODO: Make copySurfaceAsDraw handle the
3447 // swizzle.
Brian Salomon1edc5b92016-11-29 13:43:46 -05003448 if (this->caps()->shaderCaps()->configOutputSwizzle(src->config()) !=
3449 this->caps()->shaderCaps()->configOutputSwizzle(dst->config())) {
bsalomon7f9b2e42016-01-12 13:29:26 -08003450 return false;
3451 }
bsalomon083617b2016-02-12 12:10:14 -08003452 // Don't prefer copying as a draw if the dst doesn't already have a FBO object.
Brian Salomon3d86a192018-02-27 16:46:11 -05003453 // This implicitly handles this->glCaps().useDrawInsteadOfAllRenderTargetWrites().
bsalomon083617b2016-02-12 12:10:14 -08003454 bool preferCopy = SkToBool(dst->asRenderTarget());
3455 if (preferCopy && src->asTexture()) {
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003456 if (this->copySurfaceAsDraw(dst, dstOrigin, src, srcOrigin, srcRect, dstPoint)) {
cdaltone2e71c22016-04-07 18:13:29 -07003457 return true;
3458 }
bsalomon5df6fee2015-05-18 06:26:15 -07003459 }
cblume61214052016-01-26 09:10:48 -08003460
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003461 if (can_copy_texsubimage(dst, dstOrigin, src, srcOrigin, this)) {
3462 this->copySurfaceAsCopyTexSubImage(dst, dstOrigin, src, srcOrigin, srcRect, dstPoint);
bsalomon6df86402015-06-01 10:41:49 -07003463 return true;
3464 }
3465
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003466 if (can_blit_framebuffer_for_copy_surface(dst, dstOrigin, src, srcOrigin,
3467 srcRect, dstPoint, this)) {
3468 return this->copySurfaceAsBlitFramebuffer(dst, dstOrigin, src, srcOrigin,
3469 srcRect, dstPoint);
bsalomon6df86402015-06-01 10:41:49 -07003470 }
3471
bsalomon083617b2016-02-12 12:10:14 -08003472 if (!preferCopy && src->asTexture()) {
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003473 if (this->copySurfaceAsDraw(dst, dstOrigin, src, srcOrigin, srcRect, dstPoint)) {
cdaltone2e71c22016-04-07 18:13:29 -07003474 return true;
3475 }
bsalomon083617b2016-02-12 12:10:14 -08003476 }
3477
bsalomon6df86402015-06-01 10:41:49 -07003478 return false;
3479}
3480
Brian Salomonbf7b6202016-11-11 16:08:03 -05003481bool GrGLGpu::createCopyProgram(GrTexture* srcTex) {
Brian Osman39c08ac2017-07-26 09:36:09 -04003482 TRACE_EVENT0("skia", TRACE_FUNC);
Ryan Macnak38a10ad2017-07-10 10:36:34 -07003483
Brian Salomonbf7b6202016-11-11 16:08:03 -05003484 int progIdx = TextureToCopyProgramIdx(srcTex);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003485 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
Brian Salomonbf7b6202016-11-11 16:08:03 -05003486 GrSLType samplerType = srcTex->texturePriv().samplerType();
cdaltone2e71c22016-04-07 18:13:29 -07003487
3488 if (!fCopyProgramArrayBuffer) {
3489 static const GrGLfloat vdata[] = {
3490 0, 0,
3491 0, 1,
3492 1, 0,
3493 1, 1
3494 };
3495 fCopyProgramArrayBuffer.reset(GrGLBuffer::Create(this, sizeof(vdata), kVertex_GrBufferType,
3496 kStatic_GrAccessPattern, vdata));
3497 }
3498 if (!fCopyProgramArrayBuffer) {
3499 return false;
3500 }
3501
3502 SkASSERT(!fCopyPrograms[progIdx].fProgram);
3503 GL_CALL_RET(fCopyPrograms[progIdx].fProgram, CreateProgram());
3504 if (!fCopyPrograms[progIdx].fProgram) {
3505 return false;
3506 }
3507
Brian Salomon1edc5b92016-11-29 13:43:46 -05003508 const char* version = shaderCaps->versionDeclString();
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003509 GrShaderVar aVertex("a_vertex", kHalf2_GrSLType, GrShaderVar::kIn_TypeModifier);
3510 GrShaderVar uTexCoordXform("u_texCoordXform", kHalf4_GrSLType,
Brian Salomon99938a82016-11-21 13:41:08 -05003511 GrShaderVar::kUniform_TypeModifier);
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003512 GrShaderVar uPosXform("u_posXform", kHalf4_GrSLType, GrShaderVar::kUniform_TypeModifier);
Brian Salomon99938a82016-11-21 13:41:08 -05003513 GrShaderVar uTexture("u_texture", samplerType, GrShaderVar::kUniform_TypeModifier);
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003514 GrShaderVar vTexCoord("v_texCoord", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier);
3515 GrShaderVar oFragColor("o_FragColor", kHalf4_GrSLType, GrShaderVar::kOut_TypeModifier);
cdaltone2e71c22016-04-07 18:13:29 -07003516
3517 SkString vshaderTxt(version);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003518 if (shaderCaps->noperspectiveInterpolationSupport()) {
3519 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
cdaltone2e71c22016-04-07 18:13:29 -07003520 vshaderTxt.appendf("#extension %s : require\n", extension);
3521 }
3522 vTexCoord.addModifier("noperspective");
3523 }
3524
Brian Salomon1edc5b92016-11-29 13:43:46 -05003525 aVertex.appendDecl(shaderCaps, &vshaderTxt);
cdaltone2e71c22016-04-07 18:13:29 -07003526 vshaderTxt.append(";");
Brian Salomon1edc5b92016-11-29 13:43:46 -05003527 uTexCoordXform.appendDecl(shaderCaps, &vshaderTxt);
cdaltone2e71c22016-04-07 18:13:29 -07003528 vshaderTxt.append(";");
Brian Salomon1edc5b92016-11-29 13:43:46 -05003529 uPosXform.appendDecl(shaderCaps, &vshaderTxt);
cdaltone2e71c22016-04-07 18:13:29 -07003530 vshaderTxt.append(";");
Brian Salomon1edc5b92016-11-29 13:43:46 -05003531 vTexCoord.appendDecl(shaderCaps, &vshaderTxt);
cdaltone2e71c22016-04-07 18:13:29 -07003532 vshaderTxt.append(";");
3533
3534 vshaderTxt.append(
3535 "// Copy Program VS\n"
3536 "void main() {"
3537 " v_texCoord = a_vertex.xy * u_texCoordXform.xy + u_texCoordXform.zw;"
Ethan Nicholasbed683a2017-09-26 14:23:59 -04003538 " sk_Position.xy = a_vertex * u_posXform.xy + u_posXform.zw;"
3539 " sk_Position.zw = half2(0, 1);"
cdaltone2e71c22016-04-07 18:13:29 -07003540 "}"
3541 );
3542
3543 SkString fshaderTxt(version);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003544 if (shaderCaps->noperspectiveInterpolationSupport()) {
3545 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
cdaltone2e71c22016-04-07 18:13:29 -07003546 fshaderTxt.appendf("#extension %s : require\n", extension);
3547 }
3548 }
Brian Salomonbf7b6202016-11-11 16:08:03 -05003549 if (samplerType == kTextureExternalSampler_GrSLType) {
cdaltone2e71c22016-04-07 18:13:29 -07003550 fshaderTxt.appendf("#extension %s : require\n",
Brian Salomon1edc5b92016-11-29 13:43:46 -05003551 shaderCaps->externalTextureExtensionString());
cdaltone2e71c22016-04-07 18:13:29 -07003552 }
Brian Salomonf31ae492016-11-18 15:35:33 -05003553 vTexCoord.setTypeModifier(GrShaderVar::kIn_TypeModifier);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003554 vTexCoord.appendDecl(shaderCaps, &fshaderTxt);
cdaltone2e71c22016-04-07 18:13:29 -07003555 fshaderTxt.append(";");
Brian Salomon1edc5b92016-11-29 13:43:46 -05003556 uTexture.appendDecl(shaderCaps, &fshaderTxt);
cdaltone2e71c22016-04-07 18:13:29 -07003557 fshaderTxt.append(";");
cdaltone2e71c22016-04-07 18:13:29 -07003558 fshaderTxt.appendf(
3559 "// Copy Program FS\n"
3560 "void main() {"
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003561 " sk_FragColor = texture(u_texture, v_texCoord);"
3562 "}"
cdaltone2e71c22016-04-07 18:13:29 -07003563 );
3564
3565 const char* str;
3566 GrGLint length;
3567
3568 str = vshaderTxt.c_str();
3569 length = SkToInt(vshaderTxt.size());
Ethan Nicholas941e7e22016-12-12 15:33:30 -05003570 SkSL::Program::Settings settings;
3571 settings.fCaps = shaderCaps;
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003572 SkSL::String glsl;
3573 std::unique_ptr<SkSL::Program> program = GrSkSLtoGLSL(*fGLContext, GR_GL_VERTEX_SHADER,
3574 &str, &length, 1, settings, &glsl);
cdaltone2e71c22016-04-07 18:13:29 -07003575 GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fCopyPrograms[progIdx].fProgram,
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003576 GR_GL_VERTEX_SHADER, glsl.c_str(), glsl.size(),
3577 &fStats, settings);
3578 SkASSERT(program->fInputs.isEmpty());
cdaltone2e71c22016-04-07 18:13:29 -07003579
3580 str = fshaderTxt.c_str();
3581 length = SkToInt(fshaderTxt.size());
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003582 program = GrSkSLtoGLSL(*fGLContext, GR_GL_FRAGMENT_SHADER, &str, &length, 1, settings, &glsl);
cdaltone2e71c22016-04-07 18:13:29 -07003583 GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fCopyPrograms[progIdx].fProgram,
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003584 GR_GL_FRAGMENT_SHADER, glsl.c_str(), glsl.size(),
3585 &fStats, settings);
3586 SkASSERT(program->fInputs.isEmpty());
cdaltone2e71c22016-04-07 18:13:29 -07003587
3588 GL_CALL(LinkProgram(fCopyPrograms[progIdx].fProgram));
3589
3590 GL_CALL_RET(fCopyPrograms[progIdx].fTextureUniform,
3591 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_texture"));
3592 GL_CALL_RET(fCopyPrograms[progIdx].fPosXformUniform,
3593 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_posXform"));
3594 GL_CALL_RET(fCopyPrograms[progIdx].fTexCoordXformUniform,
3595 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_texCoordXform"));
3596
3597 GL_CALL(BindAttribLocation(fCopyPrograms[progIdx].fProgram, 0, "a_vertex"));
3598
3599 GL_CALL(DeleteShader(vshader));
3600 GL_CALL(DeleteShader(fshader));
3601
3602 return true;
bsalomon6df86402015-06-01 10:41:49 -07003603}
3604
brianosman33f6b3f2016-06-02 05:49:21 -07003605bool GrGLGpu::createMipmapProgram(int progIdx) {
3606 const bool oddWidth = SkToBool(progIdx & 0x2);
3607 const bool oddHeight = SkToBool(progIdx & 0x1);
3608 const int numTaps = (oddWidth ? 2 : 1) * (oddHeight ? 2 : 1);
3609
Brian Salomon1edc5b92016-11-29 13:43:46 -05003610 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
brianosman33f6b3f2016-06-02 05:49:21 -07003611
3612 SkASSERT(!fMipmapPrograms[progIdx].fProgram);
3613 GL_CALL_RET(fMipmapPrograms[progIdx].fProgram, CreateProgram());
3614 if (!fMipmapPrograms[progIdx].fProgram) {
3615 return false;
3616 }
3617
Brian Salomon1edc5b92016-11-29 13:43:46 -05003618 const char* version = shaderCaps->versionDeclString();
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003619 GrShaderVar aVertex("a_vertex", kHalf2_GrSLType, GrShaderVar::kIn_TypeModifier);
3620 GrShaderVar uTexCoordXform("u_texCoordXform", kHalf4_GrSLType,
Brian Salomon99938a82016-11-21 13:41:08 -05003621 GrShaderVar::kUniform_TypeModifier);
3622 GrShaderVar uTexture("u_texture", kTexture2DSampler_GrSLType,
3623 GrShaderVar::kUniform_TypeModifier);
brianosman33f6b3f2016-06-02 05:49:21 -07003624 // We need 1, 2, or 4 texture coordinates (depending on parity of each dimension):
Brian Salomon99938a82016-11-21 13:41:08 -05003625 GrShaderVar vTexCoords[] = {
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003626 GrShaderVar("v_texCoord0", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier),
3627 GrShaderVar("v_texCoord1", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier),
3628 GrShaderVar("v_texCoord2", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier),
3629 GrShaderVar("v_texCoord3", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier),
brianosman33f6b3f2016-06-02 05:49:21 -07003630 };
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003631 GrShaderVar oFragColor("o_FragColor", kHalf4_GrSLType,GrShaderVar::kOut_TypeModifier);
brianosman33f6b3f2016-06-02 05:49:21 -07003632
3633 SkString vshaderTxt(version);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003634 if (shaderCaps->noperspectiveInterpolationSupport()) {
3635 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
brianosman33f6b3f2016-06-02 05:49:21 -07003636 vshaderTxt.appendf("#extension %s : require\n", extension);
3637 }
3638 vTexCoords[0].addModifier("noperspective");
3639 vTexCoords[1].addModifier("noperspective");
3640 vTexCoords[2].addModifier("noperspective");
3641 vTexCoords[3].addModifier("noperspective");
3642 }
3643
Brian Salomon1edc5b92016-11-29 13:43:46 -05003644 aVertex.appendDecl(shaderCaps, &vshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003645 vshaderTxt.append(";");
Brian Salomon1edc5b92016-11-29 13:43:46 -05003646 uTexCoordXform.appendDecl(shaderCaps, &vshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003647 vshaderTxt.append(";");
3648 for (int i = 0; i < numTaps; ++i) {
Brian Salomon1edc5b92016-11-29 13:43:46 -05003649 vTexCoords[i].appendDecl(shaderCaps, &vshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003650 vshaderTxt.append(";");
3651 }
3652
3653 vshaderTxt.append(
3654 "// Mipmap Program VS\n"
3655 "void main() {"
Ethan Nicholasbed683a2017-09-26 14:23:59 -04003656 " sk_Position.xy = a_vertex * half2(2, 2) - half2(1, 1);"
3657 " sk_Position.zw = half2(0, 1);"
brianosman33f6b3f2016-06-02 05:49:21 -07003658 );
3659
3660 // Insert texture coordinate computation:
3661 if (oddWidth && oddHeight) {
3662 vshaderTxt.append(
3663 " v_texCoord0 = a_vertex.xy * u_texCoordXform.yw;"
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003664 " v_texCoord1 = a_vertex.xy * u_texCoordXform.yw + half2(u_texCoordXform.x, 0);"
3665 " v_texCoord2 = a_vertex.xy * u_texCoordXform.yw + half2(0, u_texCoordXform.z);"
brianosman33f6b3f2016-06-02 05:49:21 -07003666 " v_texCoord3 = a_vertex.xy * u_texCoordXform.yw + u_texCoordXform.xz;"
3667 );
3668 } else if (oddWidth) {
3669 vshaderTxt.append(
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003670 " v_texCoord0 = a_vertex.xy * half2(u_texCoordXform.y, 1);"
3671 " v_texCoord1 = a_vertex.xy * half2(u_texCoordXform.y, 1) + half2(u_texCoordXform.x, 0);"
brianosman33f6b3f2016-06-02 05:49:21 -07003672 );
3673 } else if (oddHeight) {
3674 vshaderTxt.append(
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003675 " v_texCoord0 = a_vertex.xy * half2(1, u_texCoordXform.w);"
3676 " v_texCoord1 = a_vertex.xy * half2(1, u_texCoordXform.w) + half2(0, u_texCoordXform.z);"
brianosman33f6b3f2016-06-02 05:49:21 -07003677 );
3678 } else {
3679 vshaderTxt.append(
3680 " v_texCoord0 = a_vertex.xy;"
3681 );
3682 }
3683
3684 vshaderTxt.append("}");
3685
3686 SkString fshaderTxt(version);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003687 if (shaderCaps->noperspectiveInterpolationSupport()) {
3688 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
brianosman33f6b3f2016-06-02 05:49:21 -07003689 fshaderTxt.appendf("#extension %s : require\n", extension);
3690 }
3691 }
brianosman33f6b3f2016-06-02 05:49:21 -07003692 for (int i = 0; i < numTaps; ++i) {
Brian Salomonf31ae492016-11-18 15:35:33 -05003693 vTexCoords[i].setTypeModifier(GrShaderVar::kIn_TypeModifier);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003694 vTexCoords[i].appendDecl(shaderCaps, &fshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003695 fshaderTxt.append(";");
3696 }
Brian Salomon1edc5b92016-11-29 13:43:46 -05003697 uTexture.appendDecl(shaderCaps, &fshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003698 fshaderTxt.append(";");
brianosman33f6b3f2016-06-02 05:49:21 -07003699 fshaderTxt.append(
3700 "// Mipmap Program FS\n"
3701 "void main() {"
3702 );
3703
3704 if (oddWidth && oddHeight) {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003705 fshaderTxt.append(
3706 " sk_FragColor = (texture(u_texture, v_texCoord0) + "
3707 " texture(u_texture, v_texCoord1) + "
3708 " texture(u_texture, v_texCoord2) + "
3709 " texture(u_texture, v_texCoord3)) * 0.25;"
brianosman33f6b3f2016-06-02 05:49:21 -07003710 );
3711 } else if (oddWidth || oddHeight) {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003712 fshaderTxt.append(
3713 " sk_FragColor = (texture(u_texture, v_texCoord0) + "
3714 " texture(u_texture, v_texCoord1)) * 0.5;"
brianosman33f6b3f2016-06-02 05:49:21 -07003715 );
3716 } else {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003717 fshaderTxt.append(
3718 " sk_FragColor = texture(u_texture, v_texCoord0);"
brianosman33f6b3f2016-06-02 05:49:21 -07003719 );
3720 }
3721
3722 fshaderTxt.append("}");
3723
3724 const char* str;
3725 GrGLint length;
3726
3727 str = vshaderTxt.c_str();
3728 length = SkToInt(vshaderTxt.size());
Ethan Nicholas941e7e22016-12-12 15:33:30 -05003729 SkSL::Program::Settings settings;
3730 settings.fCaps = shaderCaps;
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003731 SkSL::String glsl;
3732 std::unique_ptr<SkSL::Program> program = GrSkSLtoGLSL(*fGLContext, GR_GL_VERTEX_SHADER,
3733 &str, &length, 1, settings, &glsl);
brianosman33f6b3f2016-06-02 05:49:21 -07003734 GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fMipmapPrograms[progIdx].fProgram,
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003735 GR_GL_VERTEX_SHADER, glsl.c_str(), glsl.size(),
3736 &fStats, settings);
3737 SkASSERT(program->fInputs.isEmpty());
brianosman33f6b3f2016-06-02 05:49:21 -07003738
3739 str = fshaderTxt.c_str();
3740 length = SkToInt(fshaderTxt.size());
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003741 program = GrSkSLtoGLSL(*fGLContext, GR_GL_FRAGMENT_SHADER, &str, &length, 1, settings, &glsl);
brianosman33f6b3f2016-06-02 05:49:21 -07003742 GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fMipmapPrograms[progIdx].fProgram,
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003743 GR_GL_FRAGMENT_SHADER, glsl.c_str(), glsl.size(),
3744 &fStats, settings);
3745 SkASSERT(program->fInputs.isEmpty());
brianosman33f6b3f2016-06-02 05:49:21 -07003746
3747 GL_CALL(LinkProgram(fMipmapPrograms[progIdx].fProgram));
3748
3749 GL_CALL_RET(fMipmapPrograms[progIdx].fTextureUniform,
3750 GetUniformLocation(fMipmapPrograms[progIdx].fProgram, "u_texture"));
3751 GL_CALL_RET(fMipmapPrograms[progIdx].fTexCoordXformUniform,
3752 GetUniformLocation(fMipmapPrograms[progIdx].fProgram, "u_texCoordXform"));
3753
3754 GL_CALL(BindAttribLocation(fMipmapPrograms[progIdx].fProgram, 0, "a_vertex"));
3755
3756 GL_CALL(DeleteShader(vshader));
3757 GL_CALL(DeleteShader(fshader));
3758
3759 return true;
3760}
3761
Mike Klein31550db2017-06-06 23:29:53 +00003762bool GrGLGpu::createStencilClipClearProgram() {
Brian Osman39c08ac2017-07-26 09:36:09 -04003763 TRACE_EVENT0("skia", TRACE_FUNC);
Ryan Macnak38a10ad2017-07-10 10:36:34 -07003764
Mike Klein31550db2017-06-06 23:29:53 +00003765 if (!fStencilClipClearArrayBuffer) {
3766 static const GrGLfloat vdata[] = {-1, -1, 1, -1, -1, 1, 1, 1};
3767 fStencilClipClearArrayBuffer.reset(GrGLBuffer::Create(
3768 this, sizeof(vdata), kVertex_GrBufferType, kStatic_GrAccessPattern, vdata));
3769 if (!fStencilClipClearArrayBuffer) {
cdaltone2e71c22016-04-07 18:13:29 -07003770 return false;
3771 }
3772 }
3773
Mike Klein31550db2017-06-06 23:29:53 +00003774 SkASSERT(!fStencilClipClearProgram);
3775 GL_CALL_RET(fStencilClipClearProgram, CreateProgram());
3776 if (!fStencilClipClearProgram) {
cdaltone2e71c22016-04-07 18:13:29 -07003777 return false;
3778 }
3779
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003780 GrShaderVar aVertex("a_vertex", kHalf2_GrSLType, GrShaderVar::kIn_TypeModifier);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003781 const char* version = this->caps()->shaderCaps()->versionDeclString();
bsalomon6dea83f2015-12-03 12:58:06 -08003782
bsalomon6dea83f2015-12-03 12:58:06 -08003783 SkString vshaderTxt(version);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003784 aVertex.appendDecl(this->caps()->shaderCaps(), &vshaderTxt);
bsalomon6dea83f2015-12-03 12:58:06 -08003785 vshaderTxt.append(";");
bsalomon6dea83f2015-12-03 12:58:06 -08003786 vshaderTxt.append(
Mike Klein31550db2017-06-06 23:29:53 +00003787 "// Stencil Clip Clear Program VS\n"
3788 "void main() {"
Ethan Nicholasbed683a2017-09-26 14:23:59 -04003789 " sk_Position = float4(a_vertex.x, a_vertex.y, 0, 1);"
Mike Klein31550db2017-06-06 23:29:53 +00003790 "}");
bsalomon6dea83f2015-12-03 12:58:06 -08003791
3792 SkString fshaderTxt(version);
bsalomon6dea83f2015-12-03 12:58:06 -08003793 fshaderTxt.appendf(
Mike Klein31550db2017-06-06 23:29:53 +00003794 "// Stencil Clip Clear Program FS\n"
3795 "void main() {"
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003796 " sk_FragColor = half4(0);"
Mike Klein31550db2017-06-06 23:29:53 +00003797 "}");
bsalomon6dea83f2015-12-03 12:58:06 -08003798
bsalomon6dea83f2015-12-03 12:58:06 -08003799 const char* str;
3800 GrGLint length;
3801
3802 str = vshaderTxt.c_str();
3803 length = SkToInt(vshaderTxt.size());
Ethan Nicholas941e7e22016-12-12 15:33:30 -05003804 SkSL::Program::Settings settings;
3805 settings.fCaps = this->caps()->shaderCaps();
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003806 SkSL::String glsl;
3807 std::unique_ptr<SkSL::Program> program = GrSkSLtoGLSL(*fGLContext, GR_GL_VERTEX_SHADER,
3808 &str, &length, 1, settings, &glsl);
3809 GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fStencilClipClearProgram,
3810 GR_GL_VERTEX_SHADER, glsl.c_str(), glsl.size(),
3811 &fStats, settings);
3812 SkASSERT(program->fInputs.isEmpty());
bsalomon6dea83f2015-12-03 12:58:06 -08003813
3814 str = fshaderTxt.c_str();
3815 length = SkToInt(fshaderTxt.size());
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003816 program = GrSkSLtoGLSL(*fGLContext, GR_GL_FRAGMENT_SHADER, &str, &length, 1, settings, &glsl);
3817 GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fStencilClipClearProgram,
3818 GR_GL_FRAGMENT_SHADER, glsl.c_str(), glsl.size(),
3819 &fStats, settings);
3820 SkASSERT(program->fInputs.isEmpty());
bsalomon6dea83f2015-12-03 12:58:06 -08003821
Mike Klein31550db2017-06-06 23:29:53 +00003822 GL_CALL(LinkProgram(fStencilClipClearProgram));
bsalomon6dea83f2015-12-03 12:58:06 -08003823
Mike Klein31550db2017-06-06 23:29:53 +00003824 GL_CALL(BindAttribLocation(fStencilClipClearProgram, 0, "a_vertex"));
bsalomon6dea83f2015-12-03 12:58:06 -08003825
3826 GL_CALL(DeleteShader(vshader));
3827 GL_CALL(DeleteShader(fshader));
cdaltone2e71c22016-04-07 18:13:29 -07003828
3829 return true;
bsalomon6dea83f2015-12-03 12:58:06 -08003830}
3831
Mike Klein31550db2017-06-06 23:29:53 +00003832void GrGLGpu::clearStencilClipAsDraw(const GrFixedClip& clip, bool insideStencilMask,
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003833 GrRenderTarget* rt, GrSurfaceOrigin origin) {
bsalomon7f9b2e42016-01-12 13:29:26 -08003834 // TODO: This should swizzle the output to match dst's config, though it is a debugging
3835 // visualization.
3836
bsalomon6dea83f2015-12-03 12:58:06 -08003837 this->handleDirtyContext();
Mike Klein31550db2017-06-06 23:29:53 +00003838 if (!fStencilClipClearProgram) {
3839 if (!this->createStencilClipClearProgram()) {
3840 SkDebugf("Failed to create stencil clip clear program.\n");
cdaltone2e71c22016-04-07 18:13:29 -07003841 return;
3842 }
bsalomon6dea83f2015-12-03 12:58:06 -08003843 }
3844
Mike Klein8b06ed72017-06-06 21:47:32 +00003845 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(rt->asRenderTarget());
Brian Salomon1fabd512018-02-09 09:54:25 -05003846 this->flushRenderTarget(glRT);
Mike Klein8b06ed72017-06-06 21:47:32 +00003847
Mike Klein31550db2017-06-06 23:29:53 +00003848 GL_CALL(UseProgram(fStencilClipClearProgram));
3849 fHWProgramID = fStencilClipClearProgram;
bsalomon6dea83f2015-12-03 12:58:06 -08003850
cdaltone2e71c22016-04-07 18:13:29 -07003851 fHWVertexArrayState.setVertexArrayID(this, 0);
bsalomon6dea83f2015-12-03 12:58:06 -08003852
cdaltone2e71c22016-04-07 18:13:29 -07003853 GrGLAttribArrayState* attribs = fHWVertexArrayState.bindInternalVertexArray(this);
Chris Dalton8e45b4f2017-05-05 14:00:56 -04003854 attribs->enableVertexArrays(this, 1);
Ethan Nicholasfa7ee242017-09-25 09:52:04 -04003855 attribs->set(this, 0, fStencilClipClearArrayBuffer.get(), kHalf2_GrVertexAttribType,
Hal Canary144caf52016-11-07 17:57:18 -05003856 2 * sizeof(GrGLfloat), 0);
bsalomon6dea83f2015-12-03 12:58:06 -08003857
bsalomon6dea83f2015-12-03 12:58:06 -08003858 GrXferProcessor::BlendInfo blendInfo;
3859 blendInfo.reset();
bsalomon7f9b2e42016-01-12 13:29:26 -08003860 this->flushBlend(blendInfo, GrSwizzle::RGBA());
Mike Klein31550db2017-06-06 23:29:53 +00003861 this->flushColorWrite(false);
cdaltonaf8bc7d2016-02-05 09:35:20 -08003862 this->flushHWAAState(glRT, false, false);
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003863 this->flushScissor(clip.scissorState(), glRT->getViewport(), origin);
3864 this->flushWindowRectangles(clip.windowRectsState(), glRT, origin);
Mike Klein31550db2017-06-06 23:29:53 +00003865 GrStencilAttachment* sb = rt->renderTargetPriv().getStencilAttachment();
3866 // This should only be called internally when we know we have a stencil buffer.
3867 SkASSERT(sb);
3868 GrStencilSettings settings = GrStencilSettings(
3869 *GrStencilSettings::SetClipBitSettings(insideStencilMask), false, sb->bits());
3870 this->flushStencil(settings);
3871 GL_CALL(DrawArrays(GR_GL_TRIANGLE_STRIP, 0, 4));
bsalomon6dea83f2015-12-03 12:58:06 -08003872}
3873
Brian Salomon43f8bf02017-10-18 08:33:29 -04003874bool GrGLGpu::createClearColorProgram() {
3875 TRACE_EVENT0("skia", TRACE_FUNC);
3876
3877 if (!fClearProgramArrayBuffer) {
3878 static const GrGLfloat vdata[] = {-1, -1, 1, -1, -1, 1, 1, 1};
3879 fClearProgramArrayBuffer.reset(GrGLBuffer::Create(this, sizeof(vdata), kVertex_GrBufferType,
3880 kStatic_GrAccessPattern, vdata));
3881 if (!fClearProgramArrayBuffer) {
3882 return false;
3883 }
3884 }
3885
3886 SkASSERT(!fClearColorProgram.fProgram);
3887 GL_CALL_RET(fClearColorProgram.fProgram, CreateProgram());
3888 if (!fClearColorProgram.fProgram) {
3889 return false;
3890 }
3891
3892 GrShaderVar aVertex("a_vertex", kHalf2_GrSLType, GrShaderVar::kIn_TypeModifier);
3893 const char* version = this->caps()->shaderCaps()->versionDeclString();
3894
3895 SkString vshaderTxt(version);
3896 aVertex.appendDecl(this->caps()->shaderCaps(), &vshaderTxt);
3897 vshaderTxt.append(";");
3898 vshaderTxt.append(R"(
3899 // Clear Color Program VS
3900 void main() {
3901 sk_Position = float4(a_vertex.x, a_vertex.y, 0, 1);
3902 })");
3903
3904 GrShaderVar uColor("u_color", kHalf4_GrSLType, GrShaderVar::kUniform_TypeModifier);
3905 SkString fshaderTxt(version);
3906 uColor.appendDecl(this->caps()->shaderCaps(), &fshaderTxt);
3907 fshaderTxt.append(";");
3908 fshaderTxt.appendf(R"(
3909 // Clear Color Program FS
3910 void main() {
3911 sk_FragColor = u_color;
3912 })");
3913
3914 const char* str;
3915 GrGLint length;
3916
3917 str = vshaderTxt.c_str();
3918 length = SkToInt(vshaderTxt.size());
3919 SkSL::Program::Settings settings;
3920 settings.fCaps = this->caps()->shaderCaps();
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003921 SkSL::String glsl;
3922 GrSkSLtoGLSL(*fGLContext, GR_GL_VERTEX_SHADER, &str, &length, 1, settings, &glsl);
Brian Salomon43f8bf02017-10-18 08:33:29 -04003923 GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fClearColorProgram.fProgram,
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003924 GR_GL_VERTEX_SHADER, glsl.c_str(), glsl.size(),
3925 &fStats, settings);
Brian Salomon43f8bf02017-10-18 08:33:29 -04003926
3927 str = fshaderTxt.c_str();
3928 length = SkToInt(fshaderTxt.size());
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003929 GrSkSLtoGLSL(*fGLContext, GR_GL_FRAGMENT_SHADER, &str, &length, 1, settings, &glsl);
Brian Salomon43f8bf02017-10-18 08:33:29 -04003930 GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fClearColorProgram.fProgram,
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003931 GR_GL_FRAGMENT_SHADER, glsl.c_str(), glsl.size(),
3932 &fStats, settings);
Brian Salomon43f8bf02017-10-18 08:33:29 -04003933
3934 GL_CALL(LinkProgram(fClearColorProgram.fProgram));
3935
3936 GL_CALL(BindAttribLocation(fClearColorProgram.fProgram, 0, "a_vertex"));
3937
3938 GL_CALL_RET(fClearColorProgram.fColorUniform,
3939 GetUniformLocation(fClearColorProgram.fProgram, "u_color"));
3940
3941 GL_CALL(DeleteShader(vshader));
3942 GL_CALL(DeleteShader(fshader));
3943
3944 return true;
3945}
3946
3947void GrGLGpu::clearColorAsDraw(const GrFixedClip& clip, GrGLfloat r, GrGLfloat g, GrGLfloat b,
3948 GrGLfloat a, GrRenderTarget* dst, GrSurfaceOrigin origin) {
3949 if (!fClearColorProgram.fProgram) {
3950 if (!this->createClearColorProgram()) {
3951 SkDebugf("Failed to create clear color program.\n");
3952 return;
3953 }
3954 }
3955
3956 GrGLIRect dstVP;
3957 this->bindSurfaceFBOForPixelOps(dst, GR_GL_FRAMEBUFFER, &dstVP, kDst_TempFBOTarget);
3958 this->flushViewport(dstVP);
3959 fHWBoundRenderTargetUniqueID.makeInvalid();
3960
3961 GL_CALL(UseProgram(fClearColorProgram.fProgram));
3962 fHWProgramID = fClearColorProgram.fProgram;
3963
3964 fHWVertexArrayState.setVertexArrayID(this, 0);
3965
3966 GrGLAttribArrayState* attribs = fHWVertexArrayState.bindInternalVertexArray(this);
3967 attribs->enableVertexArrays(this, 1);
3968 attribs->set(this, 0, fClearProgramArrayBuffer.get(), kHalf2_GrVertexAttribType,
3969 2 * sizeof(GrGLfloat), 0);
3970
3971 GrGLRenderTarget* glrt = static_cast<GrGLRenderTarget*>(dst);
3972 this->flushScissor(clip.scissorState(), glrt->getViewport(), origin);
3973 this->flushWindowRectangles(clip.windowRectsState(), glrt, origin);
3974
3975 GL_CALL(Uniform4f(fClearColorProgram.fColorUniform, r, g, b, a));
3976
3977 GrXferProcessor::BlendInfo blendInfo;
3978 blendInfo.reset();
3979 this->flushBlend(blendInfo, GrSwizzle::RGBA());
3980 this->flushColorWrite(true);
3981 this->flushHWAAState(nullptr, false, false);
3982 this->disableStencil();
3983 if (this->glCaps().srgbWriteControl()) {
3984 this->flushFramebufferSRGB(true);
3985 }
3986
3987 GL_CALL(DrawArrays(GR_GL_TRIANGLE_STRIP, 0, 4));
3988 this->unbindTextureFBOForPixelOps(GR_GL_FRAMEBUFFER, dst);
Brian Salomon1fabd512018-02-09 09:54:25 -05003989 this->didWriteToSurface(dst, origin, clip.scissorEnabled() ? &clip.scissorRect() : nullptr);
Brian Salomon43f8bf02017-10-18 08:33:29 -04003990}
bsalomon6dea83f2015-12-03 12:58:06 -08003991
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003992bool GrGLGpu::copySurfaceAsDraw(GrSurface* dst, GrSurfaceOrigin dstOrigin,
3993 GrSurface* src, GrSurfaceOrigin srcOrigin,
bsalomon6df86402015-06-01 10:41:49 -07003994 const SkIRect& srcRect,
3995 const SkIPoint& dstPoint) {
cdaltone2e71c22016-04-07 18:13:29 -07003996 GrGLTexture* srcTex = static_cast<GrGLTexture*>(src->asTexture());
Brian Salomonbf7b6202016-11-11 16:08:03 -05003997 int progIdx = TextureToCopyProgramIdx(srcTex);
cdaltone2e71c22016-04-07 18:13:29 -07003998
3999 if (!fCopyPrograms[progIdx].fProgram) {
Brian Salomonbf7b6202016-11-11 16:08:03 -05004000 if (!this->createCopyProgram(srcTex)) {
cdaltone2e71c22016-04-07 18:13:29 -07004001 SkDebugf("Failed to create copy program.\n");
4002 return false;
4003 }
4004 }
4005
bsalomon6df86402015-06-01 10:41:49 -07004006 int w = srcRect.width();
4007 int h = srcRect.height();
4008
Brian Salomon2bbdcc42017-09-07 12:36:34 -04004009 this->bindTexture(0, GrSamplerState::ClampNearest(), true, srcTex, srcOrigin);
bsalomon6df86402015-06-01 10:41:49 -07004010
bsalomon083617b2016-02-12 12:10:14 -08004011 GrGLIRect dstVP;
Brian Salomon71d9d842016-11-03 13:42:00 -04004012 this->bindSurfaceFBOForPixelOps(dst, GR_GL_FRAMEBUFFER, &dstVP, kDst_TempFBOTarget);
bsalomon083617b2016-02-12 12:10:14 -08004013 this->flushViewport(dstVP);
Robert Phillips294870f2016-11-11 12:38:40 -05004014 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon083617b2016-02-12 12:10:14 -08004015
bsalomon6df86402015-06-01 10:41:49 -07004016 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY, w, h);
bsalomon6df86402015-06-01 10:41:49 -07004017
bsalomon7ea33f52015-11-22 14:51:00 -08004018 GL_CALL(UseProgram(fCopyPrograms[progIdx].fProgram));
4019 fHWProgramID = fCopyPrograms[progIdx].fProgram;
bsalomon6df86402015-06-01 10:41:49 -07004020
cdaltone2e71c22016-04-07 18:13:29 -07004021 fHWVertexArrayState.setVertexArrayID(this, 0);
bsalomon6df86402015-06-01 10:41:49 -07004022
cdaltone2e71c22016-04-07 18:13:29 -07004023 GrGLAttribArrayState* attribs = fHWVertexArrayState.bindInternalVertexArray(this);
Chris Dalton8e45b4f2017-05-05 14:00:56 -04004024 attribs->enableVertexArrays(this, 1);
Ethan Nicholasfa7ee242017-09-25 09:52:04 -04004025 attribs->set(this, 0, fCopyProgramArrayBuffer.get(), kHalf2_GrVertexAttribType,
Hal Canary144caf52016-11-07 17:57:18 -05004026 2 * sizeof(GrGLfloat), 0);
bsalomon6df86402015-06-01 10:41:49 -07004027
4028 // dst rect edges in NDC (-1 to 1)
4029 int dw = dst->width();
4030 int dh = dst->height();
4031 GrGLfloat dx0 = 2.f * dstPoint.fX / dw - 1.f;
4032 GrGLfloat dx1 = 2.f * (dstPoint.fX + w) / dw - 1.f;
4033 GrGLfloat dy0 = 2.f * dstPoint.fY / dh - 1.f;
4034 GrGLfloat dy1 = 2.f * (dstPoint.fY + h) / dh - 1.f;
Robert Phillipsb0e93a22017-08-29 08:26:54 -04004035 if (kBottomLeft_GrSurfaceOrigin == dstOrigin) {
bsalomon6df86402015-06-01 10:41:49 -07004036 dy0 = -dy0;
4037 dy1 = -dy1;
4038 }
4039
bsalomone5286e02016-01-14 09:24:09 -08004040 GrGLfloat sx0 = (GrGLfloat)srcRect.fLeft;
4041 GrGLfloat sx1 = (GrGLfloat)(srcRect.fLeft + w);
4042 GrGLfloat sy0 = (GrGLfloat)srcRect.fTop;
4043 GrGLfloat sy1 = (GrGLfloat)(srcRect.fTop + h);
Ethan Nicholas5338f992017-04-19 15:54:07 -04004044 int sw = src->width();
bsalomon6df86402015-06-01 10:41:49 -07004045 int sh = src->height();
Robert Phillipsb0e93a22017-08-29 08:26:54 -04004046 if (kBottomLeft_GrSurfaceOrigin == srcOrigin) {
bsalomone5286e02016-01-14 09:24:09 -08004047 sy0 = sh - sy0;
4048 sy1 = sh - sy1;
4049 }
Ethan Nicholas5338f992017-04-19 15:54:07 -04004050 // src rect edges in normalized texture space (0 to 1)
4051 sx0 /= sw;
4052 sx1 /= sw;
4053 sy0 /= sh;
4054 sy1 /= sh;
bsalomon6df86402015-06-01 10:41:49 -07004055
bsalomon7ea33f52015-11-22 14:51:00 -08004056 GL_CALL(Uniform4f(fCopyPrograms[progIdx].fPosXformUniform, dx1 - dx0, dy1 - dy0, dx0, dy0));
4057 GL_CALL(Uniform4f(fCopyPrograms[progIdx].fTexCoordXformUniform,
4058 sx1 - sx0, sy1 - sy0, sx0, sy0));
4059 GL_CALL(Uniform1i(fCopyPrograms[progIdx].fTextureUniform, 0));
bsalomon6df86402015-06-01 10:41:49 -07004060
4061 GrXferProcessor::BlendInfo blendInfo;
4062 blendInfo.reset();
bsalomon7f9b2e42016-01-12 13:29:26 -08004063 this->flushBlend(blendInfo, GrSwizzle::RGBA());
bsalomon6df86402015-06-01 10:41:49 -07004064 this->flushColorWrite(true);
bsalomon083617b2016-02-12 12:10:14 -08004065 this->flushHWAAState(nullptr, false, false);
bsalomon6df86402015-06-01 10:41:49 -07004066 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07004067 this->disableWindowRectangles();
csmartdaltonc7d85332016-10-26 10:13:46 -07004068 this->disableStencil();
Brian Osmanf02fa6f2017-07-11 11:17:47 -04004069 if (this->glCaps().srgbWriteControl()) {
4070 this->flushFramebufferSRGB(true);
4071 }
bsalomon6df86402015-06-01 10:41:49 -07004072
4073 GL_CALL(DrawArrays(GR_GL_TRIANGLE_STRIP, 0, 4));
Brian Salomon71d9d842016-11-03 13:42:00 -04004074 this->unbindTextureFBOForPixelOps(GR_GL_FRAMEBUFFER, dst);
Brian Salomon1fabd512018-02-09 09:54:25 -05004075 this->didWriteToSurface(dst, dstOrigin, &dstRect);
bsalomon083617b2016-02-12 12:10:14 -08004076
cdaltone2e71c22016-04-07 18:13:29 -07004077 return true;
bsalomon6df86402015-06-01 10:41:49 -07004078}
4079
Robert Phillipsb0e93a22017-08-29 08:26:54 -04004080void GrGLGpu::copySurfaceAsCopyTexSubImage(GrSurface* dst, GrSurfaceOrigin dstOrigin,
4081 GrSurface* src, GrSurfaceOrigin srcOrigin,
bsalomon6df86402015-06-01 10:41:49 -07004082 const SkIRect& srcRect,
4083 const SkIPoint& dstPoint) {
Robert Phillipsb0e93a22017-08-29 08:26:54 -04004084 SkASSERT(can_copy_texsubimage(dst, dstOrigin, src, srcOrigin, this));
bsalomon6df86402015-06-01 10:41:49 -07004085 GrGLIRect srcVP;
Brian Salomon71d9d842016-11-03 13:42:00 -04004086 this->bindSurfaceFBOForPixelOps(src, GR_GL_FRAMEBUFFER, &srcVP, kSrc_TempFBOTarget);
bsalomon083617b2016-02-12 12:10:14 -08004087 GrGLTexture* dstTex = static_cast<GrGLTexture *>(dst->asTexture());
bsalomon6df86402015-06-01 10:41:49 -07004088 SkASSERT(dstTex);
4089 // We modified the bound FBO
Robert Phillips294870f2016-11-11 12:38:40 -05004090 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon6df86402015-06-01 10:41:49 -07004091 GrGLIRect srcGLRect;
Robert Phillipsb0e93a22017-08-29 08:26:54 -04004092 srcGLRect.setRelativeTo(srcVP, srcRect, srcOrigin);
bsalomon6df86402015-06-01 10:41:49 -07004093
4094 this->setScratchTextureUnit();
bsalomon10528f12015-10-14 12:54:52 -07004095 GL_CALL(BindTexture(dstTex->target(), dstTex->textureID()));
bsalomon6df86402015-06-01 10:41:49 -07004096 GrGLint dstY;
Robert Phillipsb0e93a22017-08-29 08:26:54 -04004097 if (kBottomLeft_GrSurfaceOrigin == dstOrigin) {
bsalomon6df86402015-06-01 10:41:49 -07004098 dstY = dst->height() - (dstPoint.fY + srcGLRect.fHeight);
4099 } else {
4100 dstY = dstPoint.fY;
4101 }
bsalomon10528f12015-10-14 12:54:52 -07004102 GL_CALL(CopyTexSubImage2D(dstTex->target(), 0,
bsalomon083617b2016-02-12 12:10:14 -08004103 dstPoint.fX, dstY,
4104 srcGLRect.fLeft, srcGLRect.fBottom,
4105 srcGLRect.fWidth, srcGLRect.fHeight));
Brian Salomon71d9d842016-11-03 13:42:00 -04004106 this->unbindTextureFBOForPixelOps(GR_GL_FRAMEBUFFER, src);
bsalomon083617b2016-02-12 12:10:14 -08004107 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
4108 srcRect.width(), srcRect.height());
Brian Salomon1fabd512018-02-09 09:54:25 -05004109 this->didWriteToSurface(dst, dstOrigin, &dstRect);
bsalomon6df86402015-06-01 10:41:49 -07004110}
4111
Robert Phillipsb0e93a22017-08-29 08:26:54 -04004112bool GrGLGpu::copySurfaceAsBlitFramebuffer(GrSurface* dst, GrSurfaceOrigin dstOrigin,
4113 GrSurface* src, GrSurfaceOrigin srcOrigin,
bsalomon6df86402015-06-01 10:41:49 -07004114 const SkIRect& srcRect,
4115 const SkIPoint& dstPoint) {
Robert Phillipsb0e93a22017-08-29 08:26:54 -04004116 SkASSERT(can_blit_framebuffer_for_copy_surface(dst, dstOrigin, src, srcOrigin,
4117 srcRect, dstPoint, this));
bsalomon6df86402015-06-01 10:41:49 -07004118 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
4119 srcRect.width(), srcRect.height());
4120 if (dst == src) {
4121 if (SkIRect::IntersectsNoEmptyCheck(dstRect, srcRect)) {
4122 return false;
mtklein404b3b22015-05-18 09:29:10 -07004123 }
bsalomon5df6fee2015-05-18 06:26:15 -07004124 }
bsalomon6df86402015-06-01 10:41:49 -07004125
bsalomon6df86402015-06-01 10:41:49 -07004126 GrGLIRect dstVP;
4127 GrGLIRect srcVP;
Brian Salomon71d9d842016-11-03 13:42:00 -04004128 this->bindSurfaceFBOForPixelOps(dst, GR_GL_DRAW_FRAMEBUFFER, &dstVP, kDst_TempFBOTarget);
4129 this->bindSurfaceFBOForPixelOps(src, GR_GL_READ_FRAMEBUFFER, &srcVP, kSrc_TempFBOTarget);
bsalomon6df86402015-06-01 10:41:49 -07004130 // We modified the bound FBO
Robert Phillips294870f2016-11-11 12:38:40 -05004131 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon6df86402015-06-01 10:41:49 -07004132 GrGLIRect srcGLRect;
4133 GrGLIRect dstGLRect;
Robert Phillipsb0e93a22017-08-29 08:26:54 -04004134 srcGLRect.setRelativeTo(srcVP, srcRect, srcOrigin);
4135 dstGLRect.setRelativeTo(dstVP, dstRect, dstOrigin);
bsalomon6df86402015-06-01 10:41:49 -07004136
4137 // BlitFrameBuffer respects the scissor, so disable it.
4138 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07004139 this->disableWindowRectangles();
bsalomon6df86402015-06-01 10:41:49 -07004140
4141 GrGLint srcY0;
4142 GrGLint srcY1;
4143 // Does the blit need to y-mirror or not?
Robert Phillipsb0e93a22017-08-29 08:26:54 -04004144 if (srcOrigin == dstOrigin) {
bsalomon6df86402015-06-01 10:41:49 -07004145 srcY0 = srcGLRect.fBottom;
4146 srcY1 = srcGLRect.fBottom + srcGLRect.fHeight;
4147 } else {
4148 srcY0 = srcGLRect.fBottom + srcGLRect.fHeight;
4149 srcY1 = srcGLRect.fBottom;
4150 }
4151 GL_CALL(BlitFramebuffer(srcGLRect.fLeft,
4152 srcY0,
4153 srcGLRect.fLeft + srcGLRect.fWidth,
4154 srcY1,
4155 dstGLRect.fLeft,
4156 dstGLRect.fBottom,
4157 dstGLRect.fLeft + dstGLRect.fWidth,
4158 dstGLRect.fBottom + dstGLRect.fHeight,
4159 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
Brian Salomon71d9d842016-11-03 13:42:00 -04004160 this->unbindTextureFBOForPixelOps(GR_GL_DRAW_FRAMEBUFFER, dst);
4161 this->unbindTextureFBOForPixelOps(GR_GL_READ_FRAMEBUFFER, src);
Brian Salomon1fabd512018-02-09 09:54:25 -05004162 this->didWriteToSurface(dst, dstOrigin, &dstRect);
bsalomon6df86402015-06-01 10:41:49 -07004163 return true;
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00004164}
4165
brianosman33f6b3f2016-06-02 05:49:21 -07004166// Manual implementation of mipmap generation, to work around driver bugs w/sRGB.
4167// Uses draw calls to do a series of downsample operations to successive mips.
4168// If this returns false, then the calling code falls back to using glGenerateMipmap.
Robert Phillipsb0e93a22017-08-29 08:26:54 -04004169bool GrGLGpu::generateMipmap(GrGLTexture* texture, GrSurfaceOrigin textureOrigin,
4170 bool gammaCorrect) {
brianosman09563ce2016-06-02 08:59:34 -07004171 // Our iterative downsample requires the ability to limit which level we're sampling:
4172 if (!this->glCaps().doManualMipmapping()) {
brianosman33f6b3f2016-06-02 05:49:21 -07004173 return false;
4174 }
4175
4176 // Mipmaps are only supported on 2D textures:
4177 if (GR_GL_TEXTURE_2D != texture->target()) {
4178 return false;
4179 }
4180
4181 // We need to be able to render to the texture for this to work:
Brian Salomon71d9d842016-11-03 13:42:00 -04004182 if (!this->glCaps().canConfigBeFBOColorAttachment(texture->config())) {
brianosman33f6b3f2016-06-02 05:49:21 -07004183 return false;
4184 }
4185
brianosman33f6b3f2016-06-02 05:49:21 -07004186 // If we're mipping an sRGB texture, we need to ensure FB sRGB is correct:
4187 if (GrPixelConfigIsSRGB(texture->config())) {
4188 // If we have write-control, just set the state that we want:
4189 if (this->glCaps().srgbWriteControl()) {
4190 this->flushFramebufferSRGB(gammaCorrect);
4191 } else if (!gammaCorrect) {
4192 // If we don't have write-control we can't do non-gamma-correct mipmapping:
4193 return false;
4194 }
4195 }
4196
4197 int width = texture->width();
4198 int height = texture->height();
4199 int levelCount = SkMipMap::ComputeLevelCount(width, height) + 1;
4200
4201 // Define all mips, if we haven't previously done so:
4202 if (0 == texture->texturePriv().maxMipMapLevel()) {
4203 GrGLenum internalFormat;
4204 GrGLenum externalFormat;
4205 GrGLenum externalType;
4206 if (!this->glCaps().getTexImageFormats(texture->config(), texture->config(),
4207 &internalFormat, &externalFormat, &externalType)) {
4208 return false;
4209 }
4210
Brian Osman9b560242017-09-05 15:34:52 -04004211 this->unbindCpuToGpuXferBuffer();
4212
brianosman33f6b3f2016-06-02 05:49:21 -07004213 for (GrGLint level = 1; level < levelCount; ++level) {
4214 // Define the next mip:
4215 width = SkTMax(1, width / 2);
4216 height = SkTMax(1, height / 2);
4217 GL_ALLOC_CALL(this->glInterface(), TexImage2D(GR_GL_TEXTURE_2D, level, internalFormat,
4218 width, height, 0,
4219 externalFormat, externalType, nullptr));
4220 }
4221 }
4222
4223 // Create (if necessary), then bind temporary FBO:
4224 if (0 == fTempDstFBOID) {
4225 GL_CALL(GenFramebuffers(1, &fTempDstFBOID));
4226 }
4227 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, fTempDstFBOID));
Robert Phillips294870f2016-11-11 12:38:40 -05004228 fHWBoundRenderTargetUniqueID.makeInvalid();
brianosman33f6b3f2016-06-02 05:49:21 -07004229
4230 // Bind the texture, to get things configured for filtering.
4231 // We'll be changing our base level further below:
4232 this->setTextureUnit(0);
Brian Salomon2bbdcc42017-09-07 12:36:34 -04004233 this->bindTexture(0, GrSamplerState::ClampBilerp(), gammaCorrect, texture, textureOrigin);
brianosman33f6b3f2016-06-02 05:49:21 -07004234
4235 // Vertex data:
4236 if (!fMipmapProgramArrayBuffer) {
4237 static const GrGLfloat vdata[] = {
4238 0, 0,
4239 0, 1,
4240 1, 0,
4241 1, 1
4242 };
4243 fMipmapProgramArrayBuffer.reset(GrGLBuffer::Create(this, sizeof(vdata),
4244 kVertex_GrBufferType,
4245 kStatic_GrAccessPattern, vdata));
4246 }
4247 if (!fMipmapProgramArrayBuffer) {
4248 return false;
4249 }
4250
4251 fHWVertexArrayState.setVertexArrayID(this, 0);
4252
4253 GrGLAttribArrayState* attribs = fHWVertexArrayState.bindInternalVertexArray(this);
Chris Dalton8e45b4f2017-05-05 14:00:56 -04004254 attribs->enableVertexArrays(this, 1);
Ethan Nicholasfa7ee242017-09-25 09:52:04 -04004255 attribs->set(this, 0, fMipmapProgramArrayBuffer.get(), kHalf2_GrVertexAttribType,
brianosman33f6b3f2016-06-02 05:49:21 -07004256 2 * sizeof(GrGLfloat), 0);
brianosman33f6b3f2016-06-02 05:49:21 -07004257
4258 // Set "simple" state once:
4259 GrXferProcessor::BlendInfo blendInfo;
4260 blendInfo.reset();
4261 this->flushBlend(blendInfo, GrSwizzle::RGBA());
4262 this->flushColorWrite(true);
brianosman33f6b3f2016-06-02 05:49:21 -07004263 this->flushHWAAState(nullptr, false, false);
4264 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07004265 this->disableWindowRectangles();
csmartdaltonc7d85332016-10-26 10:13:46 -07004266 this->disableStencil();
brianosman33f6b3f2016-06-02 05:49:21 -07004267
4268 // Do all the blits:
4269 width = texture->width();
4270 height = texture->height();
4271 GrGLIRect viewport;
4272 viewport.fLeft = 0;
4273 viewport.fBottom = 0;
4274 for (GrGLint level = 1; level < levelCount; ++level) {
4275 // Get and bind the program for this particular downsample (filter shape can vary):
4276 int progIdx = TextureSizeToMipmapProgramIdx(width, height);
4277 if (!fMipmapPrograms[progIdx].fProgram) {
4278 if (!this->createMipmapProgram(progIdx)) {
4279 SkDebugf("Failed to create mipmap program.\n");
4280 return false;
4281 }
4282 }
4283 GL_CALL(UseProgram(fMipmapPrograms[progIdx].fProgram));
4284 fHWProgramID = fMipmapPrograms[progIdx].fProgram;
4285
4286 // Texcoord uniform is expected to contain (1/w, (w-1)/w, 1/h, (h-1)/h)
4287 const float invWidth = 1.0f / width;
4288 const float invHeight = 1.0f / height;
4289 GL_CALL(Uniform4f(fMipmapPrograms[progIdx].fTexCoordXformUniform,
4290 invWidth, (width - 1) * invWidth, invHeight, (height - 1) * invHeight));
4291 GL_CALL(Uniform1i(fMipmapPrograms[progIdx].fTextureUniform, 0));
4292
4293 // Only sample from previous mip
4294 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_BASE_LEVEL, level - 1));
4295
4296 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
4297 GR_GL_TEXTURE_2D, texture->textureID(), level));
4298
4299 width = SkTMax(1, width / 2);
4300 height = SkTMax(1, height / 2);
4301 viewport.fWidth = width;
4302 viewport.fHeight = height;
4303 this->flushViewport(viewport);
4304
4305 GL_CALL(DrawArrays(GR_GL_TRIANGLE_STRIP, 0, 4));
4306 }
4307
4308 // Unbind:
4309 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
4310 GR_GL_TEXTURE_2D, 0, 0));
4311
4312 return true;
4313}
4314
cdalton231c5fd2015-05-13 12:35:36 -07004315void GrGLGpu::xferBarrier(GrRenderTarget* rt, GrXferBarrierType type) {
bsalomoncb02b382015-08-12 11:14:50 -07004316 SkASSERT(type);
cdalton9954bc32015-04-29 14:17:00 -07004317 switch (type) {
cdalton231c5fd2015-05-13 12:35:36 -07004318 case kTexture_GrXferBarrierType: {
4319 GrGLRenderTarget* glrt = static_cast<GrGLRenderTarget*>(rt);
Brian Osmancfe83d12018-01-19 11:13:45 -05004320 SkASSERT(glrt->textureFBOID() != 0 && glrt->renderFBOID() != 0);
cdalton231c5fd2015-05-13 12:35:36 -07004321 if (glrt->textureFBOID() != glrt->renderFBOID()) {
4322 // The render target uses separate storage so no need for glTextureBarrier.
4323 // FIXME: The render target will resolve automatically when its texture is bound,
4324 // but we could resolve only the bounds that will be read if we do it here instead.
4325 return;
4326 }
cdalton9954bc32015-04-29 14:17:00 -07004327 SkASSERT(this->caps()->textureBarrierSupport());
4328 GL_CALL(TextureBarrier());
4329 return;
cdalton231c5fd2015-05-13 12:35:36 -07004330 }
cdalton8917d622015-05-06 13:40:21 -07004331 case kBlend_GrXferBarrierType:
bsalomon4b91f762015-05-19 09:29:46 -07004332 SkASSERT(GrCaps::kAdvanced_BlendEquationSupport ==
cdalton8917d622015-05-06 13:40:21 -07004333 this->caps()->blendEquationSupport());
4334 GL_CALL(BlendBarrier());
4335 return;
bsalomoncb02b382015-08-12 11:14:50 -07004336 default: break; // placate compiler warnings that kNone not handled
cdalton9954bc32015-04-29 14:17:00 -07004337 }
4338}
4339
Brian Salomonf865b052018-03-09 09:01:53 -05004340#if GR_TEST_UTILS
Brian Salomon52e943a2018-03-13 09:32:39 -04004341GrBackendTexture GrGLGpu::createTestingOnlyBackendTexture(const void* pixels, int w, int h,
Robert Phillipsd21b2a52017-12-12 13:01:25 -05004342 GrPixelConfig config, bool /*isRT*/,
4343 GrMipMapped mipMapped) {
Brian Salomon8a375832018-03-14 10:21:40 -04004344 this->handleDirtyContext();
Robert Phillipsd21b2a52017-12-12 13:01:25 -05004345 if (!this->caps()->isConfigTexturable(config)) {
4346 return GrBackendTexture(); // invalid
4347 }
4348
Greg Daniel92cbf3f2018-04-12 16:50:17 -04004349 if (w > this->caps()->maxTextureSize() || h > this->caps()->maxTextureSize()) {
4350 return GrBackendTexture(); // invalid
4351 }
4352
Robert Phillipsd21b2a52017-12-12 13:01:25 -05004353 // Currently we don't support uploading pixel data when mipped.
4354 if (pixels && GrMipMapped::kYes == mipMapped) {
4355 return GrBackendTexture(); // invalid
4356 }
4357
4358 GrGLTextureInfo info;
4359 info.fTarget = GR_GL_TEXTURE_2D;
4360 info.fID = 0;
4361 GL_CALL(GenTextures(1, &info.fID));
4362 GL_CALL(ActiveTexture(GR_GL_TEXTURE0));
4363 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, 1));
4364 GL_CALL(BindTexture(info.fTarget, info.fID));
4365 fHWBoundTextureUniqueIDs[0].makeInvalid();
4366 GL_CALL(TexParameteri(info.fTarget, GR_GL_TEXTURE_MAG_FILTER, GR_GL_NEAREST));
4367 GL_CALL(TexParameteri(info.fTarget, GR_GL_TEXTURE_MIN_FILTER, GR_GL_NEAREST));
4368 GL_CALL(TexParameteri(info.fTarget, GR_GL_TEXTURE_WRAP_S, GR_GL_CLAMP_TO_EDGE));
4369 GL_CALL(TexParameteri(info.fTarget, GR_GL_TEXTURE_WRAP_T, GR_GL_CLAMP_TO_EDGE));
4370
4371 GrGLenum internalFormat;
4372 GrGLenum externalFormat;
4373 GrGLenum externalType;
4374
4375 if (!this->glCaps().getTexImageFormats(config, config, &internalFormat, &externalFormat,
4376 &externalType)) {
4377 return GrBackendTexture(); // invalid
4378 }
4379
Greg Danielf5d87582017-12-18 14:48:15 -05004380 info.fFormat = this->glCaps().configSizedInternalFormat(config);
4381
Robert Phillipsd21b2a52017-12-12 13:01:25 -05004382 this->unbindCpuToGpuXferBuffer();
4383
4384 // Figure out the number of mip levels.
4385 int mipLevels = 1;
4386 if (GrMipMapped::kYes == mipMapped) {
4387 mipLevels = SkMipMap::ComputeLevelCount(w, h) + 1;
4388 }
4389
4390 size_t bpp = GrBytesPerPixel(config);
4391 size_t baseLayerSize = bpp * w * h;
4392 SkAutoMalloc defaultStorage(baseLayerSize);
4393 if (!pixels) {
4394 // Fill in the texture with all zeros so we don't have random garbage
4395 pixels = defaultStorage.get();
Brian Salomon52e943a2018-03-13 09:32:39 -04004396 memset(defaultStorage.get(), 0, baseLayerSize);
Robert Phillipsd21b2a52017-12-12 13:01:25 -05004397 }
4398
4399 int width = w;
4400 int height = h;
4401 for (int i = 0; i < mipLevels; ++i) {
4402 GL_CALL(TexImage2D(info.fTarget, i, internalFormat, width, height, 0, externalFormat,
4403 externalType, pixels));
4404 width = SkTMax(1, width / 2);
4405 height = SkTMax(1, height / 2);
4406 }
4407
Robert Phillipse8fabb22018-02-04 14:33:21 -05004408 // unbind the texture from the texture unit to avoid asserts
4409 GL_CALL(BindTexture(info.fTarget, 0));
4410
Greg Daniel8a3f55c2018-03-14 17:32:12 +00004411 return GrBackendTexture(w, h, mipMapped, info);
Robert Phillipsd21b2a52017-12-12 13:01:25 -05004412}
4413
4414bool GrGLGpu::isTestingOnlyBackendTexture(const GrBackendTexture& tex) const {
4415 SkASSERT(kOpenGL_GrBackend == tex.backend());
4416
Greg Daniel52e16d92018-04-10 09:34:07 -04004417 GrGLTextureInfo info;
4418 if (!tex.getGLTextureInfo(&info)) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -05004419 return false;
4420 }
4421
4422 GrGLboolean result;
Greg Daniel52e16d92018-04-10 09:34:07 -04004423 GL_CALL_RET(result, IsTexture(info.fID));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05004424
4425 return (GR_GL_TRUE == result);
4426}
4427
Brian Salomon26102cb2018-03-09 09:33:19 -05004428void GrGLGpu::deleteTestingOnlyBackendTexture(const GrBackendTexture& tex) {
4429 SkASSERT(kOpenGL_GrBackend == tex.backend());
Robert Phillipsd21b2a52017-12-12 13:01:25 -05004430
Greg Daniel52e16d92018-04-10 09:34:07 -04004431 GrGLTextureInfo info;
4432 if (tex.getGLTextureInfo(&info)) {
4433 GL_CALL(DeleteTextures(1, &info.fID));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05004434 }
4435}
4436
Brian Salomonf865b052018-03-09 09:01:53 -05004437GrBackendRenderTarget GrGLGpu::createTestingOnlyBackendRenderTarget(int w, int h,
4438 GrColorType colorType,
4439 GrSRGBEncoded srgbEncoded) {
Greg Daniel92cbf3f2018-04-12 16:50:17 -04004440 if (w > this->caps()->maxRenderTargetSize() || h > this->caps()->maxRenderTargetSize()) {
4441 return GrBackendRenderTarget(); // invalid
4442 }
Brian Salomon8a375832018-03-14 10:21:40 -04004443 this->handleDirtyContext();
Brian Salomonf865b052018-03-09 09:01:53 -05004444 auto config = GrColorTypeToPixelConfig(colorType, srgbEncoded);
4445 GrGLenum colorBufferFormat;
4446 if (!this->glCaps().getRenderbufferFormat(config, &colorBufferFormat)) {
4447 return {};
4448 }
4449 int sFormatIdx = this->getCompatibleStencilIndex(config);
4450 if (sFormatIdx < 0) {
4451 return {};
4452 }
4453 GrGLuint rbIDs[] = {0, 0};
4454 GL_CALL(GenRenderbuffers(2, rbIDs));
4455 if (!rbIDs[0] || !rbIDs[1]) {
4456 if (!rbIDs[0]) {
4457 GL_CALL(DeleteRenderbuffers(1, &rbIDs[0]));
4458 }
4459 if (!rbIDs[1]) {
4460 GL_CALL(DeleteRenderbuffers(1, &rbIDs[1]));
4461 }
4462 return {};
4463 }
4464 GrGLFramebufferInfo info;
4465 info.fFBOID = 0;
4466 GL_CALL(GenFramebuffers(1, &info.fFBOID));
4467 if (!info.fFBOID) {
4468 GL_CALL(DeleteRenderbuffers(2, rbIDs));
4469 return GrBackendRenderTarget();
4470 }
4471
4472 this->invalidateBoundRenderTarget();
4473
4474 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, info.fFBOID));
4475 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, rbIDs[0]));
4476 GL_ALLOC_CALL(this->glInterface(),
4477 RenderbufferStorage(GR_GL_RENDERBUFFER, colorBufferFormat, w, h));
4478 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0, GR_GL_RENDERBUFFER,
4479 rbIDs[0]));
4480 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, rbIDs[1]));
4481 auto stencilBufferFormat = this->glCaps().stencilFormats()[sFormatIdx].fInternalFormat;
4482 GL_ALLOC_CALL(this->glInterface(),
4483 RenderbufferStorage(GR_GL_RENDERBUFFER, stencilBufferFormat, w, h));
4484 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT, GR_GL_RENDERBUFFER,
4485 rbIDs[1]));
4486 if (this->glCaps().stencilFormats()[sFormatIdx].fPacked) {
4487 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_DEPTH_ATTACHMENT,
4488 GR_GL_RENDERBUFFER, rbIDs[1]));
4489 }
4490
4491 // We don't want to have to recover the renderbuffer IDs later to delete them. OpenGL has this
4492 // rule that if a renderbuffer is deleted and a FBO other than the current FBO has the RB
4493 // attached then deletion is delayed. So we unbind the FBO here and delete the renderbuffers.
4494 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, 0));
4495 GL_CALL(DeleteRenderbuffers(2, rbIDs));
4496
4497 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, info.fFBOID));
4498 GrGLenum status;
4499 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
4500 if (GR_GL_FRAMEBUFFER_COMPLETE != status) {
4501 GL_CALL(DeleteFramebuffers(1, &info.fFBOID));
4502 return {};
4503 }
4504 auto stencilBits = SkToInt(this->glCaps().stencilFormats()[sFormatIdx].fStencilBits);
Greg Daniel8a3f55c2018-03-14 17:32:12 +00004505 return {w, h, 1, stencilBits, config, info};
Brian Salomonf865b052018-03-09 09:01:53 -05004506}
4507
4508void GrGLGpu::deleteTestingOnlyBackendRenderTarget(const GrBackendRenderTarget& backendRT) {
4509 SkASSERT(kOpenGL_GrBackend == backendRT.backend());
Greg Daniel323fbcf2018-04-10 13:46:30 -04004510 GrGLFramebufferInfo info;
4511 if (backendRT.getGLFramebufferInfo(&info)) {
4512 if (info.fFBOID) {
4513 GL_CALL(DeleteFramebuffers(1, &info.fFBOID));
Brian Salomonf865b052018-03-09 09:01:53 -05004514 }
4515 }
joshualitt8fd844f2015-12-02 13:36:47 -08004516}
4517
Greg Daniel26b50a42018-03-08 09:49:58 -05004518void GrGLGpu::testingOnly_flushGpuAndSync() {
4519 GL_CALL(Finish());
4520}
Brian Salomonf865b052018-03-09 09:01:53 -05004521#endif
Greg Daniel26b50a42018-03-08 09:49:58 -05004522
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00004523///////////////////////////////////////////////////////////////////////////////
bsalomon6df86402015-06-01 10:41:49 -07004524
cdaltone2e71c22016-04-07 18:13:29 -07004525GrGLAttribArrayState* GrGLGpu::HWVertexArrayState::bindInternalVertexArray(GrGLGpu* gpu,
csmartdalton485a1202016-07-13 10:16:32 -07004526 const GrBuffer* ibuf) {
robertphillips@google.com4f65a272013-03-26 19:40:46 +00004527 GrGLAttribArrayState* attribState;
4528
cdaltone2e71c22016-04-07 18:13:29 -07004529 if (gpu->glCaps().isCoreProfile()) {
4530 if (!fCoreProfileVertexArray) {
bsalomon@google.com6918d482013-03-07 19:09:11 +00004531 GrGLuint arrayID;
4532 GR_GL_CALL(gpu->glInterface(), GenVertexArrays(1, &arrayID));
4533 int attrCount = gpu->glCaps().maxVertexAttributes();
cdaltone2e71c22016-04-07 18:13:29 -07004534 fCoreProfileVertexArray = new GrGLVertexArray(arrayID, attrCount);
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00004535 }
cdaltone2e71c22016-04-07 18:13:29 -07004536 if (ibuf) {
4537 attribState = fCoreProfileVertexArray->bindWithIndexBuffer(gpu, ibuf);
bsalomon6df86402015-06-01 10:41:49 -07004538 } else {
cdaltone2e71c22016-04-07 18:13:29 -07004539 attribState = fCoreProfileVertexArray->bind(gpu);
bsalomon6df86402015-06-01 10:41:49 -07004540 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00004541 } else {
cdaltone2e71c22016-04-07 18:13:29 -07004542 if (ibuf) {
4543 // bindBuffer implicitly binds VAO 0 when binding an index buffer.
4544 gpu->bindBuffer(kIndex_GrBufferType, ibuf);
bsalomon@google.com6918d482013-03-07 19:09:11 +00004545 } else {
4546 this->setVertexArrayID(gpu, 0);
4547 }
4548 int attrCount = gpu->glCaps().maxVertexAttributes();
4549 if (fDefaultVertexArrayAttribState.count() != attrCount) {
4550 fDefaultVertexArrayAttribState.resize(attrCount);
4551 }
4552 attribState = &fDefaultVertexArrayAttribState;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00004553 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00004554 return attribState;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00004555}
bsalomone179a912016-01-20 06:18:10 -08004556
Greg Daniel51316782017-08-02 15:10:09 +00004557void GrGLGpu::onFinishFlush(bool insertedSemaphore) {
4558 // If we inserted semaphores during the flush, we need to call GLFlush.
4559 if (insertedSemaphore) {
4560 GL_CALL(Flush());
4561 }
4562}
4563
Greg Daniel6be35232017-03-01 17:01:09 -05004564GrFence SK_WARN_UNUSED_RESULT GrGLGpu::insertFence() {
Greg Danielc64ee462017-06-15 16:59:49 -04004565 SkASSERT(this->caps()->fenceSyncSupport());
Greg Daniel6be35232017-03-01 17:01:09 -05004566 GrGLsync sync;
4567 GL_CALL_RET(sync, FenceSync(GR_GL_SYNC_GPU_COMMANDS_COMPLETE, 0));
4568 GR_STATIC_ASSERT(sizeof(GrFence) >= sizeof(GrGLsync));
4569 return (GrFence)sync;
jvanverth84741b32016-09-30 08:39:02 -07004570}
4571
Greg Daniel6be35232017-03-01 17:01:09 -05004572bool GrGLGpu::waitFence(GrFence fence, uint64_t timeout) {
jvanverth84741b32016-09-30 08:39:02 -07004573 GrGLenum result;
4574 GL_CALL_RET(result, ClientWaitSync((GrGLsync)fence, GR_GL_SYNC_FLUSH_COMMANDS_BIT, timeout));
4575 return (GR_GL_CONDITION_SATISFIED == result);
4576}
4577
4578void GrGLGpu::deleteFence(GrFence fence) const {
Greg Daniel6be35232017-03-01 17:01:09 -05004579 this->deleteSync((GrGLsync)fence);
4580}
4581
Greg Daniela5cb7812017-06-16 09:45:32 -04004582sk_sp<GrSemaphore> SK_WARN_UNUSED_RESULT GrGLGpu::makeSemaphore(bool isOwned) {
Greg Danielc64ee462017-06-15 16:59:49 -04004583 SkASSERT(this->caps()->fenceSyncSupport());
Greg Daniela5cb7812017-06-16 09:45:32 -04004584 return GrGLSemaphore::Make(this, isOwned);
Greg Daniel6be35232017-03-01 17:01:09 -05004585}
4586
Greg Daniel48661b82018-01-22 16:11:35 -05004587sk_sp<GrSemaphore> GrGLGpu::wrapBackendSemaphore(const GrBackendSemaphore& semaphore,
4588 GrResourceProvider::SemaphoreWrapType wrapType,
4589 GrWrapOwnership ownership) {
Greg Danielc64ee462017-06-15 16:59:49 -04004590 SkASSERT(this->caps()->fenceSyncSupport());
Greg Daniela5cb7812017-06-16 09:45:32 -04004591 return GrGLSemaphore::MakeWrapped(this, semaphore.glSync(), ownership);
4592}
4593
Greg Daniel48661b82018-01-22 16:11:35 -05004594void GrGLGpu::insertSemaphore(sk_sp<GrSemaphore> semaphore, bool flush) {
Greg Daniel6be35232017-03-01 17:01:09 -05004595 GrGLSemaphore* glSem = static_cast<GrGLSemaphore*>(semaphore.get());
4596
Greg Daniel48661b82018-01-22 16:11:35 -05004597 GrGLsync sync;
4598 GL_CALL_RET(sync, FenceSync(GR_GL_SYNC_GPU_COMMANDS_COMPLETE, 0));
4599 glSem->setSync(sync);
Brian Osmandc87c952017-04-28 13:57:38 -04004600
4601 if (flush) {
4602 GL_CALL(Flush());
4603 }
Greg Daniel6be35232017-03-01 17:01:09 -05004604}
4605
Greg Daniel48661b82018-01-22 16:11:35 -05004606void GrGLGpu::waitSemaphore(sk_sp<GrSemaphore> semaphore) {
Greg Daniel6be35232017-03-01 17:01:09 -05004607 GrGLSemaphore* glSem = static_cast<GrGLSemaphore*>(semaphore.get());
4608
4609 GL_CALL(WaitSync(glSem->sync(), 0, GR_GL_TIMEOUT_IGNORED));
4610}
4611
4612void GrGLGpu::deleteSync(GrGLsync sync) const {
4613 GL_CALL(DeleteSync(sync));
jvanverth84741b32016-09-30 08:39:02 -07004614}
Brian Osman13dddce2017-05-09 13:19:50 -04004615
Robert Phillips65a88fa2017-08-08 08:36:22 -04004616void GrGLGpu::insertEventMarker(const char* msg) {
4617 GL_CALL(InsertEventMarker(strlen(msg), msg));
4618}
4619
Brian Osman13dddce2017-05-09 13:19:50 -04004620sk_sp<GrSemaphore> GrGLGpu::prepareTextureForCrossContextUsage(GrTexture* texture) {
4621 // Set up a semaphore to be signaled once the data is ready, and flush GL
Greg Daniela5cb7812017-06-16 09:45:32 -04004622 sk_sp<GrSemaphore> semaphore = this->makeSemaphore(true);
Brian Osman13dddce2017-05-09 13:19:50 -04004623 this->insertSemaphore(semaphore, true);
4624
4625 return semaphore;
4626}
Robert Phillips646e4292017-06-13 12:44:56 -04004627
4628int GrGLGpu::TextureToCopyProgramIdx(GrTexture* texture) {
4629 switch (texture->texturePriv().samplerType()) {
4630 case kTexture2DSampler_GrSLType:
4631 return 0;
Robert Phillips646e4292017-06-13 12:44:56 -04004632 case kTexture2DRectSampler_GrSLType:
Brian Salomon57111332018-02-05 15:55:54 -05004633 return 1;
Robert Phillips646e4292017-06-13 12:44:56 -04004634 case kTextureExternalSampler_GrSLType:
Brian Salomon57111332018-02-05 15:55:54 -05004635 return 2;
Robert Phillips646e4292017-06-13 12:44:56 -04004636 default:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -04004637 SK_ABORT("Unexpected samper type");
Robert Phillips646e4292017-06-13 12:44:56 -04004638 return 0;
4639 }
4640}
Brian Osman71a18892017-08-10 10:23:25 -04004641
4642void GrGLGpu::onDumpJSON(SkJSONWriter* writer) const {
4643 // We are called by the base class, which has already called beginObject(). We choose to nest
4644 // all of our caps information in a named sub-object.
4645 writer->beginObject("GL GPU");
4646
4647 const GrGLubyte* str;
4648 GL_CALL_RET(str, GetString(GR_GL_VERSION));
4649 writer->appendString("GL_VERSION", (const char*)(str));
4650 GL_CALL_RET(str, GetString(GR_GL_RENDERER));
4651 writer->appendString("GL_RENDERER", (const char*)(str));
4652 GL_CALL_RET(str, GetString(GR_GL_VENDOR));
4653 writer->appendString("GL_VENDOR", (const char*)(str));
4654 GL_CALL_RET(str, GetString(GR_GL_SHADING_LANGUAGE_VERSION));
4655 writer->appendString("GL_SHADING_LANGUAGE_VERSION", (const char*)(str));
4656
4657 writer->appendName("extensions");
4658 glInterface()->fExtensions.dumpJSON(writer);
4659
4660 writer->endObject();
4661}