blob: 2c8b602776a51729b1b94026c3108684e93b5014 [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"
Greg Daniela5cb7812017-06-16 09:45:32 -04009#include "GrBackendSemaphore.h"
Greg Daniel7ef28f32017-04-20 16:41:55 +000010#include "GrBackendSurface.h"
Hal Canary95e3c052017-01-11 12:44:43 -050011#include "GrFixedClip.h"
cdalton397536c2016-03-25 12:15:03 -070012#include "GrGLBuffer.h"
egdaniel066df7c2016-06-08 14:02:27 -070013#include "GrGLGpuCommandBuffer.h"
Greg Daniel6be35232017-03-01 17:01:09 -050014#include "GrGLSemaphore.h"
egdaniel8dc7c3a2015-04-16 11:22:42 -070015#include "GrGLStencilAttachment.h"
bsalomon37dd3312014-11-03 08:47:23 -080016#include "GrGLTextureRenderTarget.h"
bsalomon3582d3e2015-02-13 14:20:05 -080017#include "GrGpuResourcePriv.h"
egdaniel0e1853c2016-03-17 11:35:45 -070018#include "GrMesh.h"
Hal Canary95e3c052017-01-11 12:44:43 -050019#include "GrPipeline.h"
bsalomon6bc1b5f2015-02-23 09:06:38 -080020#include "GrRenderTargetPriv.h"
Brian Salomon94efbf52016-11-29 13:43:05 -050021#include "GrShaderCaps.h"
Robert Phillips3798c862017-03-27 11:08:16 -040022#include "GrSurfaceProxyPriv.h"
bsalomonafbf2d62014-09-30 12:18:44 -070023#include "GrTexturePriv.h"
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +000024#include "GrTypes.h"
Hal Canary95e3c052017-01-11 12:44:43 -050025#include "SkAutoMalloc.h"
Brian Salomona6948702018-06-01 15:33:20 -040026#include "SkConvertPixels.h"
Stan Iliev0078ab22017-11-08 14:16:26 -050027#include "SkHalf.h"
Kevin Lubickc456b732017-01-11 17:21:57 +000028#include "SkMakeUnique.h"
29#include "SkMipMap.h"
30#include "SkPixmap.h"
Kevin Lubickc456b732017-01-11 17:21:57 +000031#include "SkSLCompiler.h"
Hal Canary95e3c052017-01-11 12:44:43 -050032#include "SkStrokeRec.h"
Kevin Lubickc456b732017-01-11 17:21:57 +000033#include "SkTemplates.h"
Hal Canaryc640d0d2018-06-13 09:59:02 -040034#include "SkTo.h"
Ryan Macnak38a10ad2017-07-10 10:36:34 -070035#include "SkTraceEvent.h"
Kevin Lubickc456b732017-01-11 17:21:57 +000036#include "SkTypes.h"
Hal Canary95e3c052017-01-11 12:44:43 -050037#include "builders/GrGLShaderStringBuilder.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000038
Hal Canaryc640d0d2018-06-13 09:59:02 -040039#include <cmath>
40
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,
Mike Klein36743362018-11-06 08:23:30 -050081 GR_GL_HSL_LUMINOSITY,
82
83 // Illegal... needs to map to something.
84 GR_GL_FUNC_ADD,
cdalton8917d622015-05-06 13:40:21 -070085};
86GR_STATIC_ASSERT(0 == kAdd_GrBlendEquation);
87GR_STATIC_ASSERT(1 == kSubtract_GrBlendEquation);
88GR_STATIC_ASSERT(2 == kReverseSubtract_GrBlendEquation);
89GR_STATIC_ASSERT(3 == kScreen_GrBlendEquation);
90GR_STATIC_ASSERT(4 == kOverlay_GrBlendEquation);
91GR_STATIC_ASSERT(5 == kDarken_GrBlendEquation);
92GR_STATIC_ASSERT(6 == kLighten_GrBlendEquation);
93GR_STATIC_ASSERT(7 == kColorDodge_GrBlendEquation);
94GR_STATIC_ASSERT(8 == kColorBurn_GrBlendEquation);
95GR_STATIC_ASSERT(9 == kHardLight_GrBlendEquation);
96GR_STATIC_ASSERT(10 == kSoftLight_GrBlendEquation);
97GR_STATIC_ASSERT(11 == kDifference_GrBlendEquation);
98GR_STATIC_ASSERT(12 == kExclusion_GrBlendEquation);
99GR_STATIC_ASSERT(13 == kMultiply_GrBlendEquation);
100GR_STATIC_ASSERT(14 == kHSLHue_GrBlendEquation);
101GR_STATIC_ASSERT(15 == kHSLSaturation_GrBlendEquation);
102GR_STATIC_ASSERT(16 == kHSLColor_GrBlendEquation);
103GR_STATIC_ASSERT(17 == kHSLLuminosity_GrBlendEquation);
bsalomonf7cc8772015-05-11 11:21:14 -0700104GR_STATIC_ASSERT(SK_ARRAY_COUNT(gXfermodeEquation2Blend) == kGrBlendEquationCnt);
cdalton8917d622015-05-06 13:40:21 -0700105
twiz@google.com0f31ca72011-03-18 17:38:11 +0000106static const GrGLenum gXfermodeCoeff2Blend[] = {
107 GR_GL_ZERO,
108 GR_GL_ONE,
109 GR_GL_SRC_COLOR,
110 GR_GL_ONE_MINUS_SRC_COLOR,
111 GR_GL_DST_COLOR,
112 GR_GL_ONE_MINUS_DST_COLOR,
113 GR_GL_SRC_ALPHA,
114 GR_GL_ONE_MINUS_SRC_ALPHA,
115 GR_GL_DST_ALPHA,
116 GR_GL_ONE_MINUS_DST_ALPHA,
117 GR_GL_CONSTANT_COLOR,
118 GR_GL_ONE_MINUS_CONSTANT_COLOR,
119 GR_GL_CONSTANT_ALPHA,
120 GR_GL_ONE_MINUS_CONSTANT_ALPHA,
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000121
122 // extended blend coeffs
123 GR_GL_SRC1_COLOR,
124 GR_GL_ONE_MINUS_SRC1_COLOR,
125 GR_GL_SRC1_ALPHA,
126 GR_GL_ONE_MINUS_SRC1_ALPHA,
Mike Klein36743362018-11-06 08:23:30 -0500127
128 // Illegal... needs to map to something.
129 GR_GL_ZERO,
reed@google.comac10a2d2010-12-22 21:39:39 +0000130};
131
bsalomon861e1032014-12-16 07:33:49 -0800132bool GrGLGpu::BlendCoeffReferencesConstant(GrBlendCoeff coeff) {
bsalomon@google.com080773c2011-03-15 19:09:25 +0000133 static const bool gCoeffReferencesBlendConst[] = {
134 false,
135 false,
136 false,
137 false,
138 false,
139 false,
140 false,
141 false,
142 false,
143 false,
144 true,
145 true,
146 true,
147 true,
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000148
149 // extended blend coeffs
150 false,
151 false,
152 false,
153 false,
Mike Klein36743362018-11-06 08:23:30 -0500154
155 // Illegal.
156 false,
bsalomon@google.com080773c2011-03-15 19:09:25 +0000157 };
158 return gCoeffReferencesBlendConst[coeff];
bsalomonf7cc8772015-05-11 11:21:14 -0700159 GR_STATIC_ASSERT(kGrBlendCoeffCnt == SK_ARRAY_COUNT(gCoeffReferencesBlendConst));
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000160
bsalomon@google.com47059542012-06-06 20:51:20 +0000161 GR_STATIC_ASSERT(0 == kZero_GrBlendCoeff);
162 GR_STATIC_ASSERT(1 == kOne_GrBlendCoeff);
163 GR_STATIC_ASSERT(2 == kSC_GrBlendCoeff);
164 GR_STATIC_ASSERT(3 == kISC_GrBlendCoeff);
165 GR_STATIC_ASSERT(4 == kDC_GrBlendCoeff);
166 GR_STATIC_ASSERT(5 == kIDC_GrBlendCoeff);
167 GR_STATIC_ASSERT(6 == kSA_GrBlendCoeff);
168 GR_STATIC_ASSERT(7 == kISA_GrBlendCoeff);
169 GR_STATIC_ASSERT(8 == kDA_GrBlendCoeff);
170 GR_STATIC_ASSERT(9 == kIDA_GrBlendCoeff);
171 GR_STATIC_ASSERT(10 == kConstC_GrBlendCoeff);
172 GR_STATIC_ASSERT(11 == kIConstC_GrBlendCoeff);
173 GR_STATIC_ASSERT(12 == kConstA_GrBlendCoeff);
174 GR_STATIC_ASSERT(13 == kIConstA_GrBlendCoeff);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000175
bsalomon@google.com47059542012-06-06 20:51:20 +0000176 GR_STATIC_ASSERT(14 == kS2C_GrBlendCoeff);
177 GR_STATIC_ASSERT(15 == kIS2C_GrBlendCoeff);
178 GR_STATIC_ASSERT(16 == kS2A_GrBlendCoeff);
179 GR_STATIC_ASSERT(17 == kIS2A_GrBlendCoeff);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000180
181 // assertion for gXfermodeCoeff2Blend have to be in GrGpu scope
bsalomonf7cc8772015-05-11 11:21:14 -0700182 GR_STATIC_ASSERT(kGrBlendCoeffCnt == SK_ARRAY_COUNT(gXfermodeCoeff2Blend));
bsalomon@google.com080773c2011-03-15 19:09:25 +0000183}
184
Brian Salomondc829942018-10-23 16:07:24 -0400185static GrGLenum filter_to_gl_mag_filter(GrSamplerState::Filter filter) {
186 switch (filter) {
187 case GrSamplerState::Filter::kNearest: return GR_GL_NEAREST;
188 case GrSamplerState::Filter::kBilerp: return GR_GL_LINEAR;
189 case GrSamplerState::Filter::kMipMap: return GR_GL_LINEAR;
190 }
191 SK_ABORT("Unknown filter");
192 return 0;
193}
194
195static GrGLenum filter_to_gl_min_filter(GrSamplerState::Filter filter) {
196 switch (filter) {
197 case GrSamplerState::Filter::kNearest: return GR_GL_NEAREST;
198 case GrSamplerState::Filter::kBilerp: return GR_GL_LINEAR;
199 case GrSamplerState::Filter::kMipMap: return GR_GL_LINEAR_MIPMAP_LINEAR;
200 }
201 SK_ABORT("Unknown filter");
202 return 0;
203}
204
Michael Ludwigf23a1522018-12-10 11:36:13 -0500205static inline GrGLenum wrap_mode_to_gl_wrap(GrSamplerState::WrapMode wrapMode,
206 const GrCaps& caps) {
Brian Salomondc829942018-10-23 16:07:24 -0400207 switch (wrapMode) {
208 case GrSamplerState::WrapMode::kClamp: return GR_GL_CLAMP_TO_EDGE;
209 case GrSamplerState::WrapMode::kRepeat: return GR_GL_REPEAT;
210 case GrSamplerState::WrapMode::kMirrorRepeat: return GR_GL_MIRRORED_REPEAT;
Michael Ludwigf23a1522018-12-10 11:36:13 -0500211 case GrSamplerState::WrapMode::kClampToBorder:
212 // May not be supported but should have been caught earlier
213 SkASSERT(caps.clampToBorderSupport());
214 return GR_GL_CLAMP_TO_BORDER;
Brian Salomon23356442018-11-30 15:33:19 -0500215 }
Brian Salomondc829942018-10-23 16:07:24 -0400216 SK_ABORT("Unknown wrap mode");
217 return 0;
218}
219
220///////////////////////////////////////////////////////////////////////////////
221
222class GrGLGpu::SamplerObjectCache {
223public:
224 SamplerObjectCache(GrGLGpu* gpu) : fGpu(gpu) {
225 fNumTextureUnits = fGpu->glCaps().shaderCaps()->maxFragmentSamplers();
226 fHWBoundSamplers.reset(new GrGLuint[fNumTextureUnits]);
227 std::fill_n(fHWBoundSamplers.get(), fNumTextureUnits, 0);
228 std::fill_n(fSamplers, kNumSamplers, 0);
229 }
230
231 ~SamplerObjectCache() {
232 if (!fNumTextureUnits) {
233 // We've already been abandoned.
234 return;
235 }
236 GR_GL_CALL(fGpu->glInterface(), DeleteSamplers(kNumSamplers, fSamplers));
237 }
238
239 void bindSampler(int unitIdx, const GrSamplerState& state) {
240 int index = StateToIndex(state);
241 if (!fSamplers[index]) {
242 GrGLuint s;
243 GR_GL_CALL(fGpu->glInterface(), GenSamplers(1, &s));
244 if (!s) {
245 return;
246 }
247 fSamplers[index] = s;
248 auto minFilter = filter_to_gl_min_filter(state.filter());
249 auto magFilter = filter_to_gl_mag_filter(state.filter());
Michael Ludwigf23a1522018-12-10 11:36:13 -0500250 auto wrapX = wrap_mode_to_gl_wrap(state.wrapModeX(), fGpu->glCaps());
251 auto wrapY = wrap_mode_to_gl_wrap(state.wrapModeY(), fGpu->glCaps());
Brian Salomondc829942018-10-23 16:07:24 -0400252 GR_GL_CALL(fGpu->glInterface(),
253 SamplerParameteri(s, GR_GL_TEXTURE_MIN_FILTER, minFilter));
254 GR_GL_CALL(fGpu->glInterface(),
255 SamplerParameteri(s, GR_GL_TEXTURE_MAG_FILTER, magFilter));
256 GR_GL_CALL(fGpu->glInterface(), SamplerParameteri(s, GR_GL_TEXTURE_WRAP_S, wrapX));
257 GR_GL_CALL(fGpu->glInterface(), SamplerParameteri(s, GR_GL_TEXTURE_WRAP_T, wrapY));
258 }
259 if (fHWBoundSamplers[unitIdx] != fSamplers[index]) {
260 GR_GL_CALL(fGpu->glInterface(), BindSampler(unitIdx, fSamplers[index]));
261 fHWBoundSamplers[unitIdx] = fSamplers[index];
262 }
263 }
264
265 void invalidateBindings() {
266 // When we have sampler support we always use samplers. So setting these to zero will cause
267 // a rebind on next usage.
268 std::fill_n(fHWBoundSamplers.get(), fNumTextureUnits, 0);
269 }
270
271 void abandon() {
272 fHWBoundSamplers.reset();
273 fNumTextureUnits = 0;
274 }
275
276 void release() {
277 if (!fNumTextureUnits) {
278 // We've already been abandoned.
279 return;
280 }
281 GR_GL_CALL(fGpu->glInterface(), DeleteSamplers(kNumSamplers, fSamplers));
282 std::fill_n(fSamplers, kNumSamplers, 0);
283 // Deleting a bound sampler implicitly binds sampler 0.
284 std::fill_n(fHWBoundSamplers.get(), fNumTextureUnits, 0);
285 }
286
287private:
288 static int StateToIndex(const GrSamplerState& state) {
289 int filter = static_cast<int>(state.filter());
290 SkASSERT(filter >= 0 && filter < 3);
291 int wrapX = static_cast<int>(state.wrapModeX());
Michael Ludwigf23a1522018-12-10 11:36:13 -0500292 SkASSERT(wrapX >= 0 && wrapX < 4);
Brian Salomondc829942018-10-23 16:07:24 -0400293 int wrapY = static_cast<int>(state.wrapModeY());
Michael Ludwigf23a1522018-12-10 11:36:13 -0500294 SkASSERT(wrapY >= 0 && wrapY < 4);
295 int idx = 16 * filter + 4 * wrapX + wrapY;
Brian Salomondc829942018-10-23 16:07:24 -0400296 SkASSERT(idx < kNumSamplers);
297 return idx;
298 }
299
300 GrGLGpu* fGpu;
Michael Ludwigf23a1522018-12-10 11:36:13 -0500301 static constexpr int kNumSamplers = 48;
Brian Salomondc829942018-10-23 16:07:24 -0400302 std::unique_ptr<GrGLuint[]> fHWBoundSamplers;
303 GrGLuint fSamplers[kNumSamplers];
304 int fNumTextureUnits;
305};
306
reed@google.comac10a2d2010-12-22 21:39:39 +0000307///////////////////////////////////////////////////////////////////////////////
308
Brian Salomon384fab42017-12-07 12:33:05 -0500309sk_sp<GrGpu> GrGLGpu::Make(sk_sp<const GrGLInterface> interface, const GrContextOptions& options,
310 GrContext* context) {
311 if (!interface) {
Brian Salomon3d6801e2017-12-11 10:06:31 -0500312 interface = GrGLMakeNativeInterface();
313 // For clients that have written their own GrGLCreateNativeInterface and haven't yet updated
314 // to GrGLMakeNativeInterface.
315 if (!interface) {
316 interface = sk_ref_sp(GrGLCreateNativeInterface());
317 }
Brian Salomon384fab42017-12-07 12:33:05 -0500318 if (!interface) {
319 return nullptr;
320 }
bsalomon424cc262015-05-22 10:37:30 -0700321 }
Jim Van Verth76334772017-05-05 16:46:05 -0400322#ifdef USE_NSIGHT
323 const_cast<GrContextOptions&>(options).fSuppressPathRendering = true;
324#endif
Brian Salomon8ab1cc42017-12-07 12:40:00 -0500325 auto glContext = GrGLContext::Make(std::move(interface), options);
326 if (!glContext) {
327 return nullptr;
bsalomon424cc262015-05-22 10:37:30 -0700328 }
Brian Salomon8ab1cc42017-12-07 12:40:00 -0500329 return sk_sp<GrGpu>(new GrGLGpu(std::move(glContext), context));
bsalomon424cc262015-05-22 10:37:30 -0700330}
331
Brian Salomon8ab1cc42017-12-07 12:40:00 -0500332GrGLGpu::GrGLGpu(std::unique_ptr<GrGLContext> ctx, GrContext* context)
333 : GrGpu(context)
334 , fGLContext(std::move(ctx))
335 , fProgramCache(new ProgramCache(this))
336 , fHWProgramID(0)
337 , fTempSrcFBOID(0)
338 , fTempDstFBOID(0)
Brian Osmana63593a2018-12-06 15:54:53 -0500339 , fStencilClearFBOID(0) {
Brian Salomon8ab1cc42017-12-07 12:40:00 -0500340 SkASSERT(fGLContext);
Brian Salomondc829942018-10-23 16:07:24 -0400341 GrGLClearErr(this->glInterface());
Brian Salomon8ab1cc42017-12-07 12:40:00 -0500342 fCaps = sk_ref_sp(fGLContext->caps());
bsalomon@google.combcce8922013-03-25 15:38:39 +0000343
Greg Daniel0f70be82018-10-08 17:35:08 +0000344 fHWBoundTextureUniqueIDs.reset(this->caps()->shaderCaps()->maxFragmentSamplers());
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +0000345
cdaltone2e71c22016-04-07 18:13:29 -0700346 fHWBufferState[kVertex_GrBufferType].fGLTarget = GR_GL_ARRAY_BUFFER;
347 fHWBufferState[kIndex_GrBufferType].fGLTarget = GR_GL_ELEMENT_ARRAY_BUFFER;
348 fHWBufferState[kTexel_GrBufferType].fGLTarget = GR_GL_TEXTURE_BUFFER;
349 fHWBufferState[kDrawIndirect_GrBufferType].fGLTarget = GR_GL_DRAW_INDIRECT_BUFFER;
350 if (GrGLCaps::kChromium_TransferBufferType == this->glCaps().transferBufferType()) {
351 fHWBufferState[kXferCpuToGpu_GrBufferType].fGLTarget =
352 GR_GL_PIXEL_UNPACK_TRANSFER_BUFFER_CHROMIUM;
353 fHWBufferState[kXferGpuToCpu_GrBufferType].fGLTarget =
354 GR_GL_PIXEL_PACK_TRANSFER_BUFFER_CHROMIUM;
355 } else {
356 fHWBufferState[kXferCpuToGpu_GrBufferType].fGLTarget = GR_GL_PIXEL_UNPACK_BUFFER;
357 fHWBufferState[kXferGpuToCpu_GrBufferType].fGLTarget = GR_GL_PIXEL_PACK_BUFFER;
358 }
Rob Phillipsbc534f62017-09-05 19:56:19 -0400359 for (int i = 0; i < kGrBufferTypeCount; ++i) {
360 fHWBufferState[i].invalidate();
361 }
cdaltone2e71c22016-04-07 18:13:29 -0700362 GR_STATIC_ASSERT(6 == SK_ARRAY_COUNT(fHWBufferState));
363
364 if (this->glCaps().shaderCaps()->pathRenderingSupport()) {
365 fPathRendering.reset(new GrGLPathRendering(this));
366 }
367
Brian Salomondc829942018-10-23 16:07:24 -0400368 if (this->glCaps().samplerObjectSupport()) {
369 fSamplerObjectCache.reset(new SamplerObjectCache(this));
370 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000371}
372
bsalomon861e1032014-12-16 07:33:49 -0800373GrGLGpu::~GrGLGpu() {
cdaltone2e71c22016-04-07 18:13:29 -0700374 // Ensure any GrGpuResource objects get deleted first, since they may require a working GrGLGpu
375 // to release the resources held by the objects themselves.
kkinnunen702501d2016-01-13 23:36:45 -0800376 fPathRendering.reset();
cdaltone2e71c22016-04-07 18:13:29 -0700377 fCopyProgramArrayBuffer.reset();
brianosman33f6b3f2016-06-02 05:49:21 -0700378 fMipmapProgramArrayBuffer.reset();
Mike Klein31550db2017-06-06 23:29:53 +0000379 fStencilClipClearArrayBuffer.reset();
kkinnunen702501d2016-01-13 23:36:45 -0800380
Brian Salomon802cb312018-06-08 18:05:20 -0400381 fHWProgram.reset();
Brian Salomon43f8bf02017-10-18 08:33:29 -0400382 if (fHWProgramID) {
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000383 // detach the current program so there is no confusion on OpenGL's part
384 // that we want it to be deleted
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000385 GL_CALL(UseProgram(0));
386 }
387
Brian Salomon43f8bf02017-10-18 08:33:29 -0400388 if (fTempSrcFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700389 this->deleteFramebuffer(fTempSrcFBOID);
egdaniel0f5f9672015-02-03 11:10:51 -0800390 }
Brian Salomon43f8bf02017-10-18 08:33:29 -0400391 if (fTempDstFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700392 this->deleteFramebuffer(fTempDstFBOID);
egdaniel0f5f9672015-02-03 11:10:51 -0800393 }
Brian Salomon43f8bf02017-10-18 08:33:29 -0400394 if (fStencilClearFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700395 this->deleteFramebuffer(fStencilClearFBOID);
bsalomondd3143b2015-02-23 09:27:45 -0800396 }
egdaniel0f5f9672015-02-03 11:10:51 -0800397
bsalomon7ea33f52015-11-22 14:51:00 -0800398 for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) {
399 if (0 != fCopyPrograms[i].fProgram) {
400 GL_CALL(DeleteProgram(fCopyPrograms[i].fProgram));
401 }
bsalomon6df86402015-06-01 10:41:49 -0700402 }
bsalomon6dea83f2015-12-03 12:58:06 -0800403
brianosman33f6b3f2016-06-02 05:49:21 -0700404 for (size_t i = 0; i < SK_ARRAY_COUNT(fMipmapPrograms); ++i) {
405 if (0 != fMipmapPrograms[i].fProgram) {
406 GL_CALL(DeleteProgram(fMipmapPrograms[i].fProgram));
407 }
408 }
409
Brian Salomon43f8bf02017-10-18 08:33:29 -0400410 if (fStencilClipClearProgram) {
Mike Klein31550db2017-06-06 23:29:53 +0000411 GL_CALL(DeleteProgram(fStencilClipClearProgram));
bsalomon6dea83f2015-12-03 12:58:06 -0800412 }
413
Brian Salomon43f8bf02017-10-18 08:33:29 -0400414 if (fClearColorProgram.fProgram) {
415 GL_CALL(DeleteProgram(fClearColorProgram.fProgram));
416 }
417
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000418 delete fProgramCache;
Brian Salomondc829942018-10-23 16:07:24 -0400419 fSamplerObjectCache.reset();
bsalomonc8dc1f72014-08-21 13:02:13 -0700420}
421
bsalomon6e2aad42016-04-01 11:54:31 -0700422void GrGLGpu::disconnect(DisconnectType type) {
423 INHERITED::disconnect(type);
424 if (DisconnectType::kCleanup == type) {
425 if (fHWProgramID) {
426 GL_CALL(UseProgram(0));
427 }
428 if (fTempSrcFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700429 this->deleteFramebuffer(fTempSrcFBOID);
bsalomon6e2aad42016-04-01 11:54:31 -0700430 }
431 if (fTempDstFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700432 this->deleteFramebuffer(fTempDstFBOID);
bsalomon6e2aad42016-04-01 11:54:31 -0700433 }
434 if (fStencilClearFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700435 this->deleteFramebuffer(fStencilClearFBOID);
bsalomon6e2aad42016-04-01 11:54:31 -0700436 }
bsalomon6e2aad42016-04-01 11:54:31 -0700437 for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) {
438 if (fCopyPrograms[i].fProgram) {
439 GL_CALL(DeleteProgram(fCopyPrograms[i].fProgram));
440 }
441 }
brianosman33f6b3f2016-06-02 05:49:21 -0700442 for (size_t i = 0; i < SK_ARRAY_COUNT(fMipmapPrograms); ++i) {
443 if (fMipmapPrograms[i].fProgram) {
444 GL_CALL(DeleteProgram(fMipmapPrograms[i].fProgram));
445 }
446 }
Mike Klein31550db2017-06-06 23:29:53 +0000447 if (fStencilClipClearProgram) {
448 GL_CALL(DeleteProgram(fStencilClipClearProgram));
bsalomon6e2aad42016-04-01 11:54:31 -0700449 }
Brian Salomon43f8bf02017-10-18 08:33:29 -0400450
451 if (fClearColorProgram.fProgram) {
452 GL_CALL(DeleteProgram(fClearColorProgram.fProgram));
453 }
Brian Salomondc829942018-10-23 16:07:24 -0400454 if (fSamplerObjectCache) {
455 fSamplerObjectCache->release();
456 }
bsalomon6e2aad42016-04-01 11:54:31 -0700457 } else {
458 if (fProgramCache) {
459 fProgramCache->abandon();
460 }
Brian Salomondc829942018-10-23 16:07:24 -0400461 if (fSamplerObjectCache) {
462 fSamplerObjectCache->abandon();
463 }
bsalomon6e2aad42016-04-01 11:54:31 -0700464 }
465
Brian Salomon802cb312018-06-08 18:05:20 -0400466 fHWProgram.reset();
bsalomon6e2aad42016-04-01 11:54:31 -0700467 delete fProgramCache;
468 fProgramCache = nullptr;
469
bsalomonc8dc1f72014-08-21 13:02:13 -0700470 fHWProgramID = 0;
egdanield803f272015-03-18 13:01:52 -0700471 fTempSrcFBOID = 0;
472 fTempDstFBOID = 0;
473 fStencilClearFBOID = 0;
cdaltone2e71c22016-04-07 18:13:29 -0700474 fCopyProgramArrayBuffer.reset();
bsalomon7ea33f52015-11-22 14:51:00 -0800475 for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) {
476 fCopyPrograms[i].fProgram = 0;
477 }
brianosman33f6b3f2016-06-02 05:49:21 -0700478 fMipmapProgramArrayBuffer.reset();
479 for (size_t i = 0; i < SK_ARRAY_COUNT(fMipmapPrograms); ++i) {
480 fMipmapPrograms[i].fProgram = 0;
481 }
Mike Klein31550db2017-06-06 23:29:53 +0000482 fStencilClipClearProgram = 0;
483 fStencilClipClearArrayBuffer.reset();
Brian Salomon43f8bf02017-10-18 08:33:29 -0400484 fClearColorProgram.fProgram = 0;
485
jvanverthe9c0fc62015-04-29 11:18:05 -0700486 if (this->glCaps().shaderCaps()->pathRenderingSupport()) {
bsalomon6e2aad42016-04-01 11:54:31 -0700487 this->glPathRendering()->disconnect(type);
bsalomonc8dc1f72014-08-21 13:02:13 -0700488 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000489}
490
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000491///////////////////////////////////////////////////////////////////////////////
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000492
bsalomon861e1032014-12-16 07:33:49 -0800493void GrGLGpu::onResetContext(uint32_t resetBits) {
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000494 if (resetBits & kMisc_GrGLBackendState) {
Brian Salomonf0861672017-05-08 11:10:10 -0400495 // we don't use the zb at all
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000496 GL_CALL(Disable(GR_GL_DEPTH_TEST));
497 GL_CALL(DepthMask(GR_GL_FALSE));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000498
Brian Salomonf0861672017-05-08 11:10:10 -0400499 // We don't use face culling.
500 GL_CALL(Disable(GR_GL_CULL_FACE));
Chris Daltonc8ece3d2018-07-30 15:03:45 -0600501 // We do use separate stencil. Our algorithms don't care which face is front vs. back so
502 // just set this to the default for self-consistency.
503 GL_CALL(FrontFace(GR_GL_CCW));
Brian Salomonf0861672017-05-08 11:10:10 -0400504
cdaltone2e71c22016-04-07 18:13:29 -0700505 fHWBufferState[kTexel_GrBufferType].invalidate();
506 fHWBufferState[kDrawIndirect_GrBufferType].invalidate();
507 fHWBufferState[kXferCpuToGpu_GrBufferType].invalidate();
508 fHWBufferState[kXferGpuToCpu_GrBufferType].invalidate();
cdaltonc1613102016-03-16 07:48:20 -0700509
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +0000510 if (kGL_GrGLStandard == this->glStandard()) {
Jim Van Verth32ac83e2016-11-28 15:23:57 -0500511#ifndef USE_NSIGHT
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000512 // Desktop-only state that we never change
513 if (!this->glCaps().isCoreProfile()) {
514 GL_CALL(Disable(GR_GL_POINT_SMOOTH));
515 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
516 GL_CALL(Disable(GR_GL_POLYGON_SMOOTH));
517 GL_CALL(Disable(GR_GL_POLYGON_STIPPLE));
518 GL_CALL(Disable(GR_GL_COLOR_LOGIC_OP));
519 GL_CALL(Disable(GR_GL_INDEX_LOGIC_OP));
520 }
521 // The windows NVIDIA driver has GL_ARB_imaging in the extension string when using a
522 // core profile. This seems like a bug since the core spec removes any mention of
523 // GL_ARB_imaging.
524 if (this->glCaps().imagingSupport() && !this->glCaps().isCoreProfile()) {
525 GL_CALL(Disable(GR_GL_COLOR_TABLE));
526 }
527 GL_CALL(Disable(GR_GL_POLYGON_OFFSET_FILL));
Jim Van Verth609e7cc2017-03-30 14:28:08 -0400528
Jim Van Verthfbdc0802017-05-02 16:15:53 -0400529 if (this->caps()->wireframeMode()) {
530 GL_CALL(PolygonMode(GR_GL_FRONT_AND_BACK, GR_GL_LINE));
531 } else {
532 GL_CALL(PolygonMode(GR_GL_FRONT_AND_BACK, GR_GL_FILL));
533 }
Jim Van Verth32ac83e2016-11-28 15:23:57 -0500534#endif
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000535 // Since ES doesn't support glPointSize at all we always use the VS to
536 // set the point size
537 GL_CALL(Enable(GR_GL_VERTEX_PROGRAM_POINT_SIZE));
538
bsalomon@google.com2b1b8c02013-02-28 22:06:02 +0000539 }
joshualitt58162332014-08-01 06:44:53 -0700540
541 if (kGLES_GrGLStandard == this->glStandard() &&
bsalomon424cc262015-05-22 10:37:30 -0700542 this->hasExtension("GL_ARM_shader_framebuffer_fetch")) {
joshualitt58162332014-08-01 06:44:53 -0700543 // The arm extension requires specifically enabling MSAA fetching per sample.
544 // On some devices this may have a perf hit. Also multiple render targets are disabled
545 GL_CALL(Enable(GR_GL_FETCH_PER_SAMPLE_ARM));
546 }
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000547 fHWWriteToColor = kUnknown_TriState;
548 // we only ever use lines in hairline mode
549 GL_CALL(LineWidth(1));
bsalomonaca31fe2015-09-22 11:38:46 -0700550 GL_CALL(Disable(GR_GL_DITHER));
bsalomon@google.comcad107b2013-06-28 14:32:08 +0000551 }
edisonn@google.comba669992013-06-28 16:03:21 +0000552
egdanielb414f252014-07-29 13:15:47 -0700553 if (resetBits & kMSAAEnable_GrGLBackendState) {
554 fMSAAEnabled = kUnknown_TriState;
vbuzinovdded6962015-06-12 08:59:45 -0700555
egdanieleed519e2016-01-15 11:36:18 -0800556 if (this->caps()->usesMixedSamples()) {
cdaltonaf8bc7d2016-02-05 09:35:20 -0800557 if (0 != this->caps()->maxRasterSamples()) {
558 fHWRasterMultisampleEnabled = kUnknown_TriState;
559 fHWNumRasterSamples = 0;
560 }
561
562 // The skia blend modes all use premultiplied alpha and therefore expect RGBA coverage
563 // modulation. This state has no effect when not rendering to a mixed sampled target.
vbuzinovdded6962015-06-12 08:59:45 -0700564 GL_CALL(CoverageModulation(GR_GL_RGBA));
565 }
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000566 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000567
commit-bot@chromium.org46fbfe02013-08-30 15:52:12 +0000568 fHWActiveTextureUnitIdx = -1; // invalid
Brian Salomonaf971de2017-06-08 16:11:33 -0400569 fLastPrimitiveType = static_cast<GrPrimitiveType>(-1);
commit-bot@chromium.org46fbfe02013-08-30 15:52:12 +0000570
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000571 if (resetBits & kTextureBinding_GrGLBackendState) {
bsalomon1c63bf62014-07-22 13:09:46 -0700572 for (int s = 0; s < fHWBoundTextureUniqueIDs.count(); ++s) {
Robert Phillips294870f2016-11-11 12:38:40 -0500573 fHWBoundTextureUniqueIDs[s].makeInvalid();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000574 }
Brian Salomondc829942018-10-23 16:07:24 -0400575 if (fSamplerObjectCache) {
576 fSamplerObjectCache->invalidateBindings();
577 }
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000578 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000579
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000580 if (resetBits & kBlend_GrGLBackendState) {
581 fHWBlendState.invalidate();
582 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000583
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000584 if (resetBits & kView_GrGLBackendState) {
585 fHWScissorSettings.invalidate();
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700586 fHWWindowRectsState.invalidate();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000587 fHWViewport.invalidate();
588 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000589
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000590 if (resetBits & kStencil_GrGLBackendState) {
591 fHWStencilSettings.invalidate();
592 fHWStencilTestEnabled = kUnknown_TriState;
593 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000594
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000595 // Vertex
596 if (resetBits & kVertex_GrGLBackendState) {
cdaltone2e71c22016-04-07 18:13:29 -0700597 fHWVertexArrayState.invalidate();
598 fHWBufferState[kVertex_GrBufferType].invalidate();
599 fHWBufferState[kIndex_GrBufferType].invalidate();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000600 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000601
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000602 if (resetBits & kRenderTarget_GrGLBackendState) {
Robert Phillips294870f2016-11-11 12:38:40 -0500603 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon16921ec2015-07-30 15:34:56 -0700604 fHWSRGBFramebuffer = kUnknown_TriState;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000605 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000606
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000607 if (resetBits & kPathRendering_GrGLBackendState) {
jvanverthe9c0fc62015-04-29 11:18:05 -0700608 if (this->caps()->shaderCaps()->pathRenderingSupport()) {
kkinnunenccdaa042014-08-20 01:36:23 -0700609 this->glPathRendering()->resetContext();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000610 }
bsalomon@google.com05a718c2012-06-29 14:01:53 +0000611 }
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000612
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000613 // we assume these values
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000614 if (resetBits & kPixelStore_GrGLBackendState) {
615 if (this->glCaps().unpackRowLengthSupport()) {
616 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
617 }
618 if (this->glCaps().packRowLengthSupport()) {
619 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
620 }
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000621 if (this->glCaps().packFlipYSupport()) {
622 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, GR_GL_FALSE));
623 }
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000624 }
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000625
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000626 if (resetBits & kProgram_GrGLBackendState) {
627 fHWProgramID = 0;
Brian Salomon802cb312018-06-08 18:05:20 -0400628 fHWProgram.reset();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000629 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000630}
631
Brian Salomond17f6582017-07-19 18:28:58 -0400632static bool check_backend_texture(const GrBackendTexture& backendTex, const GrGLCaps& caps,
633 GrGLTexture::IDDesc* idDesc) {
Greg Daniel52e16d92018-04-10 09:34:07 -0400634 GrGLTextureInfo info;
635 if (!backendTex.getGLTextureInfo(&info) || !info.fID) {
Brian Salomond17f6582017-07-19 18:28:58 -0400636 return false;
bsalomon091f60c2015-11-10 11:54:56 -0800637 }
robertphillips@google.comb72e5d32012-10-30 15:18:10 +0000638
Greg Daniel52e16d92018-04-10 09:34:07 -0400639 idDesc->fInfo = info;
bsalomon7ea33f52015-11-22 14:51:00 -0800640
Brian Salomond17f6582017-07-19 18:28:58 -0400641 if (GR_GL_TEXTURE_EXTERNAL == idDesc->fInfo.fTarget) {
642 if (!caps.shaderCaps()->externalTextureSupport()) {
643 return false;
644 }
645 } else if (GR_GL_TEXTURE_RECTANGLE == idDesc->fInfo.fTarget) {
646 if (!caps.rectangleTextureSupport()) {
647 return false;
648 }
649 } else if (GR_GL_TEXTURE_2D != idDesc->fInfo.fTarget) {
650 return false;
651 }
652 return true;
653}
654
655sk_sp<GrTexture> GrGLGpu::onWrapBackendTexture(const GrBackendTexture& backendTex,
Brian Salomonc67c31c2018-12-06 10:00:03 -0500656 GrWrapOwnership ownership, GrIOType ioType,
657 bool purgeImmediately) {
bsalomonb15b4c12014-10-29 12:41:57 -0700658 GrGLTexture::IDDesc idDesc;
Brian Salomond17f6582017-07-19 18:28:58 -0400659 if (!check_backend_texture(backendTex, this->glCaps(), &idDesc)) {
660 return nullptr;
661 }
Greg Daniele7d8da42017-12-04 11:23:19 -0500662 if (!idDesc.fInfo.fFormat) {
663 idDesc.fInfo.fFormat = this->glCaps().configSizedInternalFormat(backendTex.config());
664 }
Brian Salomond17f6582017-07-19 18:28:58 -0400665 if (kBorrow_GrWrapOwnership == ownership) {
666 idDesc.fOwnership = GrBackendObjectOwnership::kBorrowed;
667 } else {
668 idDesc.fOwnership = GrBackendObjectOwnership::kOwned;
669 }
bsalomone5286e02016-01-14 09:24:09 -0800670
Brian Salomond17f6582017-07-19 18:28:58 -0400671 GrSurfaceDesc surfDesc;
672 surfDesc.fFlags = kNone_GrSurfaceFlags;
673 surfDesc.fWidth = backendTex.width();
674 surfDesc.fHeight = backendTex.height();
675 surfDesc.fConfig = backendTex.config();
Brian Salomonbdecacf2018-02-02 20:32:49 -0500676 surfDesc.fSampleCnt = 1;
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400677
Greg Daniel177e6952017-10-12 12:27:11 -0400678 GrMipMapsStatus mipMapsStatus = backendTex.hasMipMaps() ? GrMipMapsStatus::kValid
679 : GrMipMapsStatus::kNotAllocated;
680
Brian Salomonc67c31c2018-12-06 10:00:03 -0500681 auto texture = GrGLTexture::MakeWrapped(this, surfDesc, mipMapsStatus, idDesc, ioType,
Greg Daniel2268ad22018-11-15 09:27:38 -0500682 purgeImmediately);
Brian Salomondc829942018-10-23 16:07:24 -0400683 // We don't know what parameters are already set on wrapped textures.
684 texture->textureParamsModified();
685 return std::move(texture);
Brian Salomond17f6582017-07-19 18:28:58 -0400686}
687
688sk_sp<GrTexture> GrGLGpu::onWrapRenderableBackendTexture(const GrBackendTexture& backendTex,
Brian Salomond17f6582017-07-19 18:28:58 -0400689 int sampleCnt,
690 GrWrapOwnership ownership) {
691 GrGLTexture::IDDesc idDesc;
692 if (!check_backend_texture(backendTex, this->glCaps(), &idDesc)) {
bsalomone5286e02016-01-14 09:24:09 -0800693 return nullptr;
bsalomon7ea33f52015-11-22 14:51:00 -0800694 }
Greg Daniele7d8da42017-12-04 11:23:19 -0500695 if (!idDesc.fInfo.fFormat) {
696 idDesc.fInfo.fFormat = this->glCaps().configSizedInternalFormat(backendTex.config());
697 }
bsalomone5286e02016-01-14 09:24:09 -0800698
Brian Salomond17f6582017-07-19 18:28:58 -0400699 // We don't support rendering to a EXTERNAL texture.
700 if (GR_GL_TEXTURE_EXTERNAL == idDesc.fInfo.fTarget) {
bsalomona98419b2015-11-23 07:09:50 -0800701 return nullptr;
702 }
bsalomon10528f12015-10-14 12:54:52 -0700703
Brian Osman766fcbb2017-03-13 09:33:09 -0400704 if (kBorrow_GrWrapOwnership == ownership) {
Brian Osmana6953f22017-03-10 20:14:05 +0000705 idDesc.fOwnership = GrBackendObjectOwnership::kBorrowed;
Brian Osman766fcbb2017-03-13 09:33:09 -0400706 } else {
707 idDesc.fOwnership = GrBackendObjectOwnership::kOwned;
bsalomone5286e02016-01-14 09:24:09 -0800708 }
bsalomonb15b4c12014-10-29 12:41:57 -0700709
Ben Wagner18b61f92016-10-25 10:44:02 -0400710 GrSurfaceDesc surfDesc;
Brian Salomond17f6582017-07-19 18:28:58 -0400711 surfDesc.fFlags = kRenderTarget_GrSurfaceFlag;
Greg Daniel7ef28f32017-04-20 16:41:55 +0000712 surfDesc.fWidth = backendTex.width();
713 surfDesc.fHeight = backendTex.height();
714 surfDesc.fConfig = backendTex.config();
Brian Salomonbdecacf2018-02-02 20:32:49 -0500715 surfDesc.fSampleCnt = this->caps()->getRenderTargetSampleCount(sampleCnt, backendTex.config());
716 if (surfDesc.fSampleCnt < 1) {
717 return nullptr;
718 }
bsalomon@google.come269f212011-11-07 13:29:52 +0000719
Brian Salomond17f6582017-07-19 18:28:58 -0400720 GrGLRenderTarget::IDDesc rtIDDesc;
721 if (!this->createRenderTargetObjects(surfDesc, idDesc.fInfo, &rtIDDesc)) {
722 return nullptr;
bsalomon@google.come269f212011-11-07 13:29:52 +0000723 }
Greg Daniel177e6952017-10-12 12:27:11 -0400724
725 GrMipMapsStatus mipMapsStatus = backendTex.hasMipMaps() ? GrMipMapsStatus::kDirty
726 : GrMipMapsStatus::kNotAllocated;
727
Brian Salomond17f6582017-07-19 18:28:58 -0400728 sk_sp<GrGLTextureRenderTarget> texRT(
Greg Daniel177e6952017-10-12 12:27:11 -0400729 GrGLTextureRenderTarget::MakeWrapped(this, surfDesc, idDesc, rtIDDesc, mipMapsStatus));
Brian Salomond17f6582017-07-19 18:28:58 -0400730 texRT->baseLevelWasBoundToFBO();
Brian Salomondc829942018-10-23 16:07:24 -0400731 // We don't know what parameters are already set on wrapped textures.
732 texRT->textureParamsModified();
Brian Salomond17f6582017-07-19 18:28:58 -0400733 return std::move(texRT);
bsalomon@google.come269f212011-11-07 13:29:52 +0000734}
735
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400736sk_sp<GrRenderTarget> GrGLGpu::onWrapBackendRenderTarget(const GrBackendRenderTarget& backendRT) {
Greg Daniel323fbcf2018-04-10 13:46:30 -0400737 GrGLFramebufferInfo info;
738 if (!backendRT.getGLFramebufferInfo(&info)) {
Greg Danielbcf612b2017-05-01 13:50:58 +0000739 return nullptr;
740 }
741
bsalomonb15b4c12014-10-29 12:41:57 -0700742 GrGLRenderTarget::IDDesc idDesc;
Greg Daniel323fbcf2018-04-10 13:46:30 -0400743 idDesc.fRTFBOID = info.fFBOID;
bsalomonb15b4c12014-10-29 12:41:57 -0700744 idDesc.fMSColorRenderbufferID = 0;
egdanield803f272015-03-18 13:01:52 -0700745 idDesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
Brian Osman0b791f52017-03-10 08:30:22 -0500746 idDesc.fRTFBOOwnership = GrBackendObjectOwnership::kBorrowed;
csmartdaltonf9635992016-08-10 11:09:07 -0700747 idDesc.fIsMixedSampled = false;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000748
bsalomonb15b4c12014-10-29 12:41:57 -0700749 GrSurfaceDesc desc;
Brian Salomon0ec981b2017-05-15 13:48:50 -0400750 desc.fFlags = kRenderTarget_GrSurfaceFlag;
Greg Danielbcf612b2017-05-01 13:50:58 +0000751 desc.fWidth = backendRT.width();
752 desc.fHeight = backendRT.height();
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400753 desc.fConfig = backendRT.config();
Brian Salomonbdecacf2018-02-02 20:32:49 -0500754 desc.fSampleCnt =
755 this->caps()->getRenderTargetSampleCount(backendRT.sampleCnt(), backendRT.config());
bsalomonb15b4c12014-10-29 12:41:57 -0700756
Greg Daniel4065d452018-11-16 15:43:41 -0500757 return GrGLRenderTarget::MakeWrapped(this, desc, info.fFormat, idDesc, backendRT.stencilBits());
bsalomon@google.come269f212011-11-07 13:29:52 +0000758}
759
Greg Daniel7ef28f32017-04-20 16:41:55 +0000760sk_sp<GrRenderTarget> GrGLGpu::onWrapBackendTextureAsRenderTarget(const GrBackendTexture& tex,
Greg Daniel7ef28f32017-04-20 16:41:55 +0000761 int sampleCnt) {
Greg Daniel52e16d92018-04-10 09:34:07 -0400762 GrGLTextureInfo info;
763 if (!tex.getGLTextureInfo(&info) || !info.fID) {
ericrkf7b8b8a2016-02-24 14:49:51 -0800764 return nullptr;
765 }
ericrkf7b8b8a2016-02-24 14:49:51 -0800766
Greg Daniel52e16d92018-04-10 09:34:07 -0400767 if (GR_GL_TEXTURE_RECTANGLE != info.fTarget &&
768 GR_GL_TEXTURE_2D != info.fTarget) {
ericrkf7b8b8a2016-02-24 14:49:51 -0800769 // Only texture rectangle and texture 2d are supported. We do not check whether texture
770 // rectangle is supported by Skia - if the caller provided us with a texture rectangle,
771 // we assume the necessary support exists.
772 return nullptr;
773 }
774
Ben Wagner18b61f92016-10-25 10:44:02 -0400775 GrSurfaceDesc surfDesc;
Greg Daniel7ef28f32017-04-20 16:41:55 +0000776 surfDesc.fFlags = kRenderTarget_GrSurfaceFlag;
777 surfDesc.fWidth = tex.width();
778 surfDesc.fHeight = tex.height();
779 surfDesc.fConfig = tex.config();
Brian Salomonbdecacf2018-02-02 20:32:49 -0500780 surfDesc.fSampleCnt = this->caps()->getRenderTargetSampleCount(sampleCnt, tex.config());
ericrkf7b8b8a2016-02-24 14:49:51 -0800781
782 GrGLRenderTarget::IDDesc rtIDDesc;
Greg Daniel52e16d92018-04-10 09:34:07 -0400783 if (!this->createRenderTargetObjects(surfDesc, info, &rtIDDesc)) {
ericrkf7b8b8a2016-02-24 14:49:51 -0800784 return nullptr;
785 }
Greg Daniel4065d452018-11-16 15:43:41 -0500786 return GrGLRenderTarget::MakeWrapped(this, surfDesc, info.fFormat, rtIDDesc, 0);
ericrkf7b8b8a2016-02-24 14:49:51 -0800787}
788
Brian Salomonc320b152018-02-20 14:05:36 -0500789static bool check_write_and_transfer_input(GrGLTexture* glTex) {
bsalomon6cb3cbe2015-07-30 07:34:27 -0700790 if (!glTex) {
791 return false;
792 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000793
bsalomone5286e02016-01-14 09:24:09 -0800794 // Write or transfer of pixels is not implemented for TEXTURE_EXTERNAL textures
795 if (GR_GL_TEXTURE_EXTERNAL == glTex->target()) {
bsalomon7ea33f52015-11-22 14:51:00 -0800796 return false;
797 }
798
jvanverth17aa0472016-01-05 10:41:27 -0800799 return true;
800}
801
Brian Salomona9b04b92018-06-01 15:04:28 -0400802bool GrGLGpu::onWritePixels(GrSurface* surface, int left, int top, int width, int height,
803 GrColorType srcColorType, const GrMipLevel texels[],
804 int mipLevelCount) {
Brian Salomonc320b152018-02-20 14:05:36 -0500805 auto glTex = static_cast<GrGLTexture*>(surface->asTexture());
jvanverth17aa0472016-01-05 10:41:27 -0800806
Brian Salomonc320b152018-02-20 14:05:36 -0500807 if (!check_write_and_transfer_input(glTex)) {
jvanverth17aa0472016-01-05 10:41:27 -0800808 return false;
809 }
810
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +0000811 this->setScratchTextureUnit();
bsalomon10528f12015-10-14 12:54:52 -0700812 GL_CALL(BindTexture(glTex->target(), glTex->textureID()));
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000813
Brian Salomonc320b152018-02-20 14:05:36 -0500814 // No sRGB transformation occurs in uploadTexData. We choose to make the src config match the
815 // srgb-ness of the surface to avoid issues in ES2 where internal/external formats must match.
816 // When we're on ES2 and the dst is GL_SRGB_ALPHA by making the config be kSRGB_8888 we know
817 // that our caps will choose GL_SRGB_ALPHA as the external format, too. On ES3 or regular GL our
818 // caps knows to make the external format be GL_RGBA.
819 auto srgbEncoded = GrPixelConfigIsSRGBEncoded(surface->config());
820 auto srcAsConfig = GrColorTypeToPixelConfig(srcColorType, srgbEncoded);
Jim Van Verth1676cb92019-01-15 13:24:45 -0500821
822 SkASSERT(!GrPixelConfigIsCompressed(glTex->config()));
Brian Salomona9b04b92018-06-01 15:04:28 -0400823 return this->uploadTexData(glTex->config(), glTex->width(), glTex->height(), glTex->target(),
824 kWrite_UploadType, left, top, width, height, srcAsConfig, texels,
825 mipLevelCount);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000826}
827
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400828// For GL_[UN]PACK_ALIGNMENT. TODO: This really wants to be GrColorType.
bsalomonf46a1242015-12-15 12:37:38 -0800829static inline GrGLint config_alignment(GrPixelConfig config) {
Jim Van Verth1676cb92019-01-15 13:24:45 -0500830 SkASSERT(!GrPixelConfigIsCompressed(config));
bsalomonf46a1242015-12-15 12:37:38 -0800831 switch (config) {
832 case kAlpha_8_GrPixelConfig:
Greg Danielef59d872017-11-17 16:47:21 -0500833 case kAlpha_8_as_Alpha_GrPixelConfig:
834 case kAlpha_8_as_Red_GrPixelConfig:
Brian Osman986563b2017-01-10 14:20:02 -0500835 case kGray_8_GrPixelConfig:
Greg Daniel7af060a2017-12-05 16:27:11 -0500836 case kGray_8_as_Lum_GrPixelConfig:
837 case kGray_8_as_Red_GrPixelConfig:
bsalomonf46a1242015-12-15 12:37:38 -0800838 return 1;
839 case kRGB_565_GrPixelConfig:
840 case kRGBA_4444_GrPixelConfig:
Jim Van Verth69e57852018-12-05 13:38:59 -0500841 case kRG_88_GrPixelConfig:
bsalomonf46a1242015-12-15 12:37:38 -0800842 case kAlpha_half_GrPixelConfig:
Greg Danielef59d872017-11-17 16:47:21 -0500843 case kAlpha_half_as_Red_GrPixelConfig:
bsalomonf46a1242015-12-15 12:37:38 -0800844 case kRGBA_half_GrPixelConfig:
845 return 2;
846 case kRGBA_8888_GrPixelConfig:
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400847 case kRGB_888_GrPixelConfig: // We're really talking about GrColorType::kRGB_888x here.
bsalomonf46a1242015-12-15 12:37:38 -0800848 case kBGRA_8888_GrPixelConfig:
849 case kSRGBA_8888_GrPixelConfig:
brianosmana6359362016-03-21 06:55:37 -0700850 case kSBGRA_8888_GrPixelConfig:
Brian Osman10fc6fd2018-03-02 11:01:10 -0500851 case kRGBA_1010102_GrPixelConfig:
bsalomonf46a1242015-12-15 12:37:38 -0800852 case kRGBA_float_GrPixelConfig:
csmartdalton6aa0e112017-02-08 16:14:11 -0500853 case kRG_float_GrPixelConfig:
bsalomonf46a1242015-12-15 12:37:38 -0800854 return 4;
Jim Van Verth1676cb92019-01-15 13:24:45 -0500855 case kRGB_ETC1_GrPixelConfig:
csmartdalton6aa0e112017-02-08 16:14:11 -0500856 case kUnknown_GrPixelConfig:
bsalomonf46a1242015-12-15 12:37:38 -0800857 return 0;
858 }
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400859 SK_ABORT("Invalid pixel config");
csmartdalton6aa0e112017-02-08 16:14:11 -0500860 return 0;
bsalomonf46a1242015-12-15 12:37:38 -0800861}
862
Brian Salomon9b009bb2018-02-14 13:53:55 -0500863bool GrGLGpu::onTransferPixels(GrTexture* texture, int left, int top, int width, int height,
Brian Salomonc320b152018-02-20 14:05:36 -0500864 GrColorType bufferColorType, GrBuffer* transferBuffer, size_t offset,
Brian Salomon9b009bb2018-02-14 13:53:55 -0500865 size_t rowBytes) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400866 GrGLTexture* glTex = static_cast<GrGLTexture*>(texture);
867 GrPixelConfig texConfig = glTex->config();
868 SkASSERT(this->caps()->isConfigTexturable(texConfig));
869
Jim Van Verth1676cb92019-01-15 13:24:45 -0500870 // Can't transfer compressed data
871 SkASSERT(!GrPixelConfigIsCompressed(glTex->config()));
872
Brian Salomonc320b152018-02-20 14:05:36 -0500873 if (!check_write_and_transfer_input(glTex)) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400874 return false;
875 }
876
Hal Canaryc36f7e72018-06-18 15:50:09 -0400877 static_assert(sizeof(int) == sizeof(int32_t), "");
878 if (width <= 0 || height <= 0) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400879 return false;
880 }
881
882 this->setScratchTextureUnit();
883 GL_CALL(BindTexture(glTex->target(), glTex->textureID()));
884
885 SkASSERT(!transferBuffer->isMapped());
886 SkASSERT(!transferBuffer->isCPUBacked());
887 const GrGLBuffer* glBuffer = static_cast<const GrGLBuffer*>(transferBuffer);
888 this->bindBuffer(kXferCpuToGpu_GrBufferType, glBuffer);
889
Greg Daniel660cc992017-06-26 14:55:05 -0400890 SkDEBUGCODE(
891 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
892 SkIRect bounds = SkIRect::MakeWH(texture->width(), texture->height());
893 SkASSERT(bounds.contains(subRect));
894 )
895
Brian Salomonc320b152018-02-20 14:05:36 -0500896 int bpp = GrColorTypeBytesPerPixel(bufferColorType);
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400897 const size_t trimRowBytes = width * bpp;
Greg Daniel660cc992017-06-26 14:55:05 -0400898 if (!rowBytes) {
899 rowBytes = trimRowBytes;
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400900 }
Greg Daniel660cc992017-06-26 14:55:05 -0400901 const void* pixels = (void*)offset;
Bruce Dawson71479b72017-07-05 14:30:20 -0700902 if (width < 0 || height < 0) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400903 return false;
904 }
905
906 bool restoreGLRowLength = false;
907 if (trimRowBytes != rowBytes) {
908 // we should have checked for this support already
909 SkASSERT(this->glCaps().unpackRowLengthSupport());
910 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowBytes / bpp));
911 restoreGLRowLength = true;
912 }
913
914 // Internal format comes from the texture desc.
915 GrGLenum internalFormat;
916 // External format and type come from the upload data.
917 GrGLenum externalFormat;
918 GrGLenum externalType;
Brian Salomonc320b152018-02-20 14:05:36 -0500919 auto bufferAsConfig = GrColorTypeToPixelConfig(bufferColorType, GrSRGBEncoded::kNo);
920 if (!this->glCaps().getTexImageFormats(texConfig, bufferAsConfig, &internalFormat,
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400921 &externalFormat, &externalType)) {
922 return false;
923 }
924
925 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, config_alignment(texConfig)));
926 GL_CALL(TexSubImage2D(glTex->target(),
927 0,
928 left, top,
929 width,
930 height,
931 externalFormat, externalType,
932 pixels));
933
934 if (restoreGLRowLength) {
935 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
936 }
937
938 return true;
939}
940
cblume55f2d2d2016-02-26 13:20:48 -0800941/**
942 * Creates storage space for the texture and fills it with texels.
943 *
Brian Salomond1eaf492017-05-18 10:02:08 -0400944 * @param config Pixel config of the texture.
cblume55f2d2d2016-02-26 13:20:48 -0800945 * @param interface The GL interface in use.
cblume790d5132016-02-29 11:13:29 -0800946 * @param caps The capabilities of the GL device.
Jim Van Verth1676cb92019-01-15 13:24:45 -0500947 * @param target Which bound texture to target (GR_GL_TEXTURE_2D, e.g.)
brianosman81a84852016-09-12 09:05:14 -0700948 * @param internalFormat The data format used for the internal storage of the texture. May be sized.
949 * @param internalFormatForTexStorage The data format used for the TexStorage API. Must be sized.
cblume55f2d2d2016-02-26 13:20:48 -0800950 * @param externalFormat The data format used for the external storage of the texture.
951 * @param externalType The type of the data used for the external storage of the texture.
952 * @param texels The texel data of the texture being created.
Jim Van Verth1676cb92019-01-15 13:24:45 -0500953 * @param mipLevelCount Number of mipmap levels
cblume55f2d2d2016-02-26 13:20:48 -0800954 * @param baseWidth The width of the texture's base mipmap level
955 * @param baseHeight The height of the texture's base mipmap level
cblume55f2d2d2016-02-26 13:20:48 -0800956 */
Robert Phillips92de6312017-05-23 07:43:48 -0400957static bool allocate_and_populate_texture(GrPixelConfig config,
958 const GrGLInterface& interface,
959 const GrGLCaps& caps,
960 GrGLenum target,
961 GrGLenum internalFormat,
962 GrGLenum internalFormatForTexStorage,
963 GrGLenum externalFormat,
964 GrGLenum externalType,
Robert Phillips590533f2017-07-11 14:22:35 -0400965 const GrMipLevel texels[], int mipLevelCount,
Robert Phillips92de6312017-05-23 07:43:48 -0400966 int baseWidth, int baseHeight) {
cblume55f2d2d2016-02-26 13:20:48 -0800967 CLEAR_ERROR_BEFORE_ALLOC(&interface);
cblume790d5132016-02-29 11:13:29 -0800968
Brian Salomond1eaf492017-05-18 10:02:08 -0400969 bool useTexStorage = caps.isConfigTexSupportEnabled(config);
cblume790d5132016-02-29 11:13:29 -0800970 // We can only use TexStorage if we know we will not later change the storage requirements.
971 // This means if we may later want to add mipmaps, we cannot use TexStorage.
972 // Right now, we cannot know if we will later add mipmaps or not.
973 // The only time we can use TexStorage is when we already have the
Brian Salomon57111332018-02-05 15:55:54 -0500974 // mipmaps.
975 useTexStorage &= mipLevelCount > 1;
cblume790d5132016-02-29 11:13:29 -0800976
977 if (useTexStorage) {
978 // We never resize or change formats of textures.
cblume55f2d2d2016-02-26 13:20:48 -0800979 GL_ALLOC_CALL(&interface,
Robert Phillips590533f2017-07-11 14:22:35 -0400980 TexStorage2D(target, SkTMax(mipLevelCount, 1), internalFormatForTexStorage,
Brian Salomond1eaf492017-05-18 10:02:08 -0400981 baseWidth, baseHeight));
Brian Salomon0ec981b2017-05-15 13:48:50 -0400982 GrGLenum error = CHECK_ALLOC_ERROR(&interface);
cblume55f2d2d2016-02-26 13:20:48 -0800983 if (error != GR_GL_NO_ERROR) {
bsalomone699d0c2016-03-09 06:25:15 -0800984 return false;
cblume790d5132016-02-29 11:13:29 -0800985 } else {
Robert Phillips590533f2017-07-11 14:22:35 -0400986 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
cblume790d5132016-02-29 11:13:29 -0800987 const void* currentMipData = texels[currentMipLevel].fPixels;
988 if (currentMipData == nullptr) {
989 continue;
990 }
991 int twoToTheMipLevel = 1 << currentMipLevel;
Brian Salomond1eaf492017-05-18 10:02:08 -0400992 int currentWidth = SkTMax(1, baseWidth / twoToTheMipLevel);
993 int currentHeight = SkTMax(1, baseHeight / twoToTheMipLevel);
cblume790d5132016-02-29 11:13:29 -0800994
995 GR_GL_CALL(&interface,
996 TexSubImage2D(target,
997 currentMipLevel,
998 0, // left
999 0, // top
1000 currentWidth,
1001 currentHeight,
1002 externalFormat, externalType,
1003 currentMipData));
1004 }
bsalomone699d0c2016-03-09 06:25:15 -08001005 return true;
cblume790d5132016-02-29 11:13:29 -08001006 }
1007 } else {
Robert Phillips590533f2017-07-11 14:22:35 -04001008 if (!mipLevelCount) {
cblume790d5132016-02-29 11:13:29 -08001009 GL_ALLOC_CALL(&interface,
1010 TexImage2D(target,
bsalomone699d0c2016-03-09 06:25:15 -08001011 0,
cblume790d5132016-02-29 11:13:29 -08001012 internalFormat,
bsalomone699d0c2016-03-09 06:25:15 -08001013 baseWidth,
1014 baseHeight,
cblume790d5132016-02-29 11:13:29 -08001015 0, // border
1016 externalFormat, externalType,
bsalomone699d0c2016-03-09 06:25:15 -08001017 nullptr));
Brian Salomon0ec981b2017-05-15 13:48:50 -04001018 GrGLenum error = CHECK_ALLOC_ERROR(&interface);
cblume790d5132016-02-29 11:13:29 -08001019 if (error != GR_GL_NO_ERROR) {
bsalomone699d0c2016-03-09 06:25:15 -08001020 return false;
1021 }
1022 } else {
Robert Phillips590533f2017-07-11 14:22:35 -04001023 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
bsalomone699d0c2016-03-09 06:25:15 -08001024 int twoToTheMipLevel = 1 << currentMipLevel;
1025 int currentWidth = SkTMax(1, baseWidth / twoToTheMipLevel);
1026 int currentHeight = SkTMax(1, baseHeight / twoToTheMipLevel);
Greg Daniel8b059bd2017-09-28 20:46:45 +00001027 const void* currentMipData = texels[currentMipLevel].fPixels;
bsalomone699d0c2016-03-09 06:25:15 -08001028 // Even if curremtMipData is nullptr, continue to call TexImage2D.
1029 // This will allocate texture memory which we can later populate.
1030 GL_ALLOC_CALL(&interface,
1031 TexImage2D(target,
1032 currentMipLevel,
1033 internalFormat,
1034 currentWidth,
1035 currentHeight,
1036 0, // border
1037 externalFormat, externalType,
1038 currentMipData));
Brian Salomon0ec981b2017-05-15 13:48:50 -04001039 GrGLenum error = CHECK_ALLOC_ERROR(&interface);
bsalomone699d0c2016-03-09 06:25:15 -08001040 if (error != GR_GL_NO_ERROR) {
1041 return false;
1042 }
cblume790d5132016-02-29 11:13:29 -08001043 }
cblume55f2d2d2016-02-26 13:20:48 -08001044 }
1045 }
bsalomone699d0c2016-03-09 06:25:15 -08001046 return true;
cblume55f2d2d2016-02-26 13:20:48 -08001047}
1048
1049/**
Jim Van Verth1676cb92019-01-15 13:24:45 -05001050 * Creates storage space for the texture and fills it with texels.
1051 *
1052 * @param config Compressed pixel config of the texture.
1053 * @param interface The GL interface in use.
1054 * @param caps The capabilities of the GL device.
1055 * @param target Which bound texture to target (GR_GL_TEXTURE_2D, e.g.)
1056 * @param internalFormat The data format used for the internal storage of the texture.
1057 * @param texels The texel data of the texture being created.
1058 * @param mipLevelCount Number of mipmap levels
1059 * @param baseWidth The width of the texture's base mipmap level
1060 * @param baseHeight The height of the texture's base mipmap level
1061 */
1062static bool allocate_and_populate_compressed_texture(GrPixelConfig config,
1063 const GrGLInterface& interface,
1064 const GrGLCaps& caps,
1065 GrGLenum target, GrGLenum internalFormat,
1066 const GrMipLevel texels[], int mipLevelCount,
1067 int baseWidth, int baseHeight) {
1068 CLEAR_ERROR_BEFORE_ALLOC(&interface);
1069 SkASSERT(GrPixelConfigIsCompressed(config));
1070
1071 bool useTexStorage = caps.isConfigTexSupportEnabled(config);
1072 // We can only use TexStorage if we know we will not later change the storage requirements.
1073 // This means if we may later want to add mipmaps, we cannot use TexStorage.
1074 // Right now, we cannot know if we will later add mipmaps or not.
1075 // The only time we can use TexStorage is when we already have the
1076 // mipmaps.
1077 useTexStorage &= mipLevelCount > 1;
1078
1079 if (useTexStorage) {
1080 // We never resize or change formats of textures.
1081 GL_ALLOC_CALL(&interface,
1082 TexStorage2D(target,
1083 mipLevelCount,
1084 internalFormat,
1085 baseWidth, baseHeight));
1086 GrGLenum error = CHECK_ALLOC_ERROR(&interface);
1087 if (error != GR_GL_NO_ERROR) {
1088 return false;
1089 } else {
1090 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
1091 const void* currentMipData = texels[currentMipLevel].fPixels;
1092 if (currentMipData == nullptr) {
1093 // Compressed textures require data for every level
1094 return false;
1095 }
1096
1097 int twoToTheMipLevel = 1 << currentMipLevel;
1098 int currentWidth = SkTMax(1, baseWidth / twoToTheMipLevel);
1099 int currentHeight = SkTMax(1, baseHeight / twoToTheMipLevel);
1100
1101 // Make sure that the width and height that we pass to OpenGL
1102 // is a multiple of the block size.
1103 size_t dataSize = GrCompressedFormatDataSize(config, currentWidth, currentHeight);
1104 GR_GL_CALL(&interface, CompressedTexSubImage2D(target,
1105 currentMipLevel,
1106 0, // left
1107 0, // top
1108 currentWidth,
1109 currentHeight,
1110 internalFormat,
1111 SkToInt(dataSize),
1112 currentMipData));
1113 }
1114 }
1115 } else {
1116 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
1117 const void* currentMipData = texels[currentMipLevel].fPixels;
1118 if (currentMipData == nullptr) {
1119 // Compressed textures require data for every level
1120 return false;
1121 }
1122
1123 int twoToTheMipLevel = 1 << currentMipLevel;
1124 int currentWidth = SkTMax(1, baseWidth / twoToTheMipLevel);
1125 int currentHeight = SkTMax(1, baseHeight / twoToTheMipLevel);
1126
1127 // Make sure that the width and height that we pass to OpenGL
1128 // is a multiple of the block size.
1129 size_t dataSize = GrCompressedFormatDataSize(config, baseWidth, baseHeight);
1130
1131 GL_ALLOC_CALL(&interface,
1132 CompressedTexImage2D(target,
1133 currentMipLevel,
1134 internalFormat,
1135 currentWidth,
1136 currentHeight,
1137 0, // border
1138 SkToInt(dataSize),
1139 currentMipData));
1140
1141 GrGLenum error = CHECK_ALLOC_ERROR(&interface);
1142 if (error != GR_GL_NO_ERROR) {
1143 return false;
1144 }
1145 }
1146 }
1147
1148 return true;
1149}
1150/**
cblume55f2d2d2016-02-26 13:20:48 -08001151 * After a texture is created, any state which was altered during its creation
1152 * needs to be restored.
1153 *
1154 * @param interface The GL interface to use.
1155 * @param caps The capabilities of the GL device.
1156 * @param restoreGLRowLength Should the row length unpacking be restored?
1157 * @param glFlipY Did GL flip the texture vertically?
1158 */
1159static void restore_pixelstore_state(const GrGLInterface& interface, const GrGLCaps& caps,
Brian Salomona9b04b92018-06-01 15:04:28 -04001160 bool restoreGLRowLength) {
cblume55f2d2d2016-02-26 13:20:48 -08001161 if (restoreGLRowLength) {
1162 SkASSERT(caps.unpackRowLengthSupport());
1163 GR_GL_CALL(&interface, PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
1164 }
cblume55f2d2d2016-02-26 13:20:48 -08001165}
1166
Brian Osman9b560242017-09-05 15:34:52 -04001167void GrGLGpu::unbindCpuToGpuXferBuffer() {
1168 auto& xferBufferState = fHWBufferState[kXferCpuToGpu_GrBufferType];
1169 if (!xferBufferState.fBoundBufferUniqueID.isInvalid()) {
1170 GL_CALL(BindBuffer(xferBufferState.fGLTarget, 0));
1171 xferBufferState.invalidate();
1172 }
1173
1174}
1175
Brian Salomonc320b152018-02-20 14:05:36 -05001176// TODO: Make this take a GrColorType instead of dataConfig. This requires updating GrGLCaps to
1177// convert from GrColorType to externalFormat/externalType GLenum values.
Brian Salomona9b04b92018-06-01 15:04:28 -04001178bool GrGLGpu::uploadTexData(GrPixelConfig texConfig, int texWidth, int texHeight, GrGLenum target,
1179 UploadType uploadType, int left, int top, int width, int height,
1180 GrPixelConfig dataConfig, const GrMipLevel texels[], int mipLevelCount,
Greg Daniel0fc4d2d2017-10-12 11:23:36 -04001181 GrMipMapsStatus* mipMapsStatus) {
Jim Van Verth1676cb92019-01-15 13:24:45 -05001182 // If we're uploading compressed data then we should be using uploadCompressedTexData
1183 SkASSERT(!GrPixelConfigIsCompressed(dataConfig));
1184
Brian Salomond1eaf492017-05-18 10:02:08 -04001185 SkASSERT(this->caps()->isConfigTexturable(texConfig));
Greg Daniel660cc992017-06-26 14:55:05 -04001186 SkDEBUGCODE(
1187 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
1188 SkIRect bounds = SkIRect::MakeWH(texWidth, texHeight);
1189 SkASSERT(bounds.contains(subRect));
1190 )
Robert Phillips590533f2017-07-11 14:22:35 -04001191 SkASSERT(1 == mipLevelCount ||
Greg Daniel660cc992017-06-26 14:55:05 -04001192 (0 == left && 0 == top && width == texWidth && height == texHeight));
bsalomon5b30c6f2015-12-17 14:17:34 -08001193
Brian Osman9b560242017-09-05 15:34:52 -04001194 this->unbindCpuToGpuXferBuffer();
Jim Van Verth2e5eaf02017-06-21 15:55:46 -04001195
cblume55f2d2d2016-02-26 13:20:48 -08001196 // texels is const.
1197 // But we may need to flip the texture vertically to prepare it.
1198 // Rather than flip in place and alter the incoming data,
1199 // we allocate a new buffer to flip into.
1200 // This means we need to make a non-const shallow copy of texels.
Robert Phillips0f992772017-07-12 08:24:56 -04001201 SkAutoTMalloc<GrMipLevel> texelsShallowCopy;
1202
1203 if (mipLevelCount) {
1204 texelsShallowCopy.reset(mipLevelCount);
1205 memcpy(texelsShallowCopy.get(), texels, mipLevelCount*sizeof(GrMipLevel));
1206 }
cblume55f2d2d2016-02-26 13:20:48 -08001207
cblume55f2d2d2016-02-26 13:20:48 -08001208 const GrGLInterface* interface = this->glInterface();
1209 const GrGLCaps& caps = this->glCaps();
1210
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001211 size_t bpp = GrBytesPerPixel(dataConfig);
cblume55f2d2d2016-02-26 13:20:48 -08001212
1213 if (width == 0 || height == 0) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +00001214 return false;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001215 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00001216
bsalomon5b30c6f2015-12-17 14:17:34 -08001217 // Internal format comes from the texture desc.
bsalomon76148af2016-01-12 11:13:47 -08001218 GrGLenum internalFormat;
bsalomon5b30c6f2015-12-17 14:17:34 -08001219 // External format and type come from the upload data.
bsalomon76148af2016-01-12 11:13:47 -08001220 GrGLenum externalFormat;
1221 GrGLenum externalType;
Brian Salomond1eaf492017-05-18 10:02:08 -04001222 if (!this->glCaps().getTexImageFormats(texConfig, dataConfig, &internalFormat, &externalFormat,
1223 &externalType)) {
bsalomon76148af2016-01-12 11:13:47 -08001224 return false;
1225 }
brianosman81a84852016-09-12 09:05:14 -07001226 // TexStorage requires a sized format, and internalFormat may or may not be
Brian Salomond1eaf492017-05-18 10:02:08 -04001227 GrGLenum internalFormatForTexStorage = this->glCaps().configSizedInternalFormat(texConfig);
brianosman81a84852016-09-12 09:05:14 -07001228
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001229 /*
bsalomon5b30c6f2015-12-17 14:17:34 -08001230 * Check whether to allocate a temporary buffer for flipping y or
bsalomon@google.com6f379512011-11-16 20:36:03 +00001231 * because our srcData has extra bytes past each row. If so, we need
1232 * to trim those off here, since GL ES may not let us specify
1233 * GL_UNPACK_ROW_LENGTH.
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001234 */
bsalomon@google.com6f379512011-11-16 20:36:03 +00001235 bool restoreGLRowLength = false;
cblume55f2d2d2016-02-26 13:20:48 -08001236
1237 // in case we need a temporary, trimmed copy of the src pixels
1238 SkAutoSMalloc<128 * 128> tempStorage;
1239
Greg Daniel0fc4d2d2017-10-12 11:23:36 -04001240 if (mipMapsStatus) {
1241 *mipMapsStatus = GrMipMapsStatus::kValid;
Greg Daniel834f1202017-10-09 15:06:20 -04001242 }
1243
Robert Phillips931f7512017-11-02 12:57:18 -04001244 const bool usesMips = mipLevelCount > 1;
1245
cblume55f2d2d2016-02-26 13:20:48 -08001246 // find the combined size of all the mip levels and the relative offset of
1247 // each into the collective buffer
Robert Phillips931f7512017-11-02 12:57:18 -04001248 bool willNeedData = false;
Greg Daniel55afd6d2017-09-29 09:32:44 -04001249 size_t combinedBufferSize = 0;
1250 SkTArray<size_t> individualMipOffsets(mipLevelCount);
Robert Phillips590533f2017-07-11 14:22:35 -04001251 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
Greg Daniel55afd6d2017-09-29 09:32:44 -04001252 if (texelsShallowCopy[currentMipLevel].fPixels) {
1253 int twoToTheMipLevel = 1 << currentMipLevel;
1254 int currentWidth = SkTMax(1, width / twoToTheMipLevel);
1255 int currentHeight = SkTMax(1, height / twoToTheMipLevel);
Robert Phillips931f7512017-11-02 12:57:18 -04001256 const size_t trimRowBytes = currentWidth * bpp;
1257 const size_t trimmedSize = trimRowBytes * currentHeight;
1258
1259 const size_t rowBytes = texelsShallowCopy[currentMipLevel].fRowBytes
1260 ? texelsShallowCopy[currentMipLevel].fRowBytes
1261 : trimRowBytes;
1262
Brian Salomona9b04b92018-06-01 15:04:28 -04001263 if (((!caps.unpackRowLengthSupport() || usesMips) && trimRowBytes != rowBytes)) {
Robert Phillips931f7512017-11-02 12:57:18 -04001264 willNeedData = true;
1265 }
1266
Greg Daniel55afd6d2017-09-29 09:32:44 -04001267 individualMipOffsets.push_back(combinedBufferSize);
1268 combinedBufferSize += trimmedSize;
1269 } else {
Greg Daniel0fc4d2d2017-10-12 11:23:36 -04001270 if (mipMapsStatus) {
1271 *mipMapsStatus = GrMipMapsStatus::kDirty;
Greg Daniel834f1202017-10-09 15:06:20 -04001272 }
Greg Daniel55afd6d2017-09-29 09:32:44 -04001273 individualMipOffsets.push_back(0);
1274 }
Greg Daniel0fc4d2d2017-10-12 11:23:36 -04001275 }
1276 if (mipMapsStatus && mipLevelCount <= 1) {
1277 *mipMapsStatus = GrMipMapsStatus::kNotAllocated;
cblume55f2d2d2016-02-26 13:20:48 -08001278 }
Robert Phillips931f7512017-11-02 12:57:18 -04001279 char* buffer = nullptr;
1280 if (willNeedData) {
1281 buffer = (char*)tempStorage.reset(combinedBufferSize);
1282 }
cblume55f2d2d2016-02-26 13:20:48 -08001283
Robert Phillips590533f2017-07-11 14:22:35 -04001284 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
Greg Daniel55afd6d2017-09-29 09:32:44 -04001285 if (!texelsShallowCopy[currentMipLevel].fPixels) {
1286 continue;
1287 }
cblume55f2d2d2016-02-26 13:20:48 -08001288 int twoToTheMipLevel = 1 << currentMipLevel;
1289 int currentWidth = SkTMax(1, width / twoToTheMipLevel);
1290 int currentHeight = SkTMax(1, height / twoToTheMipLevel);
1291 const size_t trimRowBytes = currentWidth * bpp;
1292
1293 /*
1294 * check whether to allocate a temporary buffer for flipping y or
1295 * because our srcData has extra bytes past each row. If so, we need
1296 * to trim those off here, since GL ES may not let us specify
1297 * GL_UNPACK_ROW_LENGTH.
1298 */
1299 restoreGLRowLength = false;
1300
Greg Daniel55afd6d2017-09-29 09:32:44 -04001301 const size_t rowBytes = texelsShallowCopy[currentMipLevel].fRowBytes
1302 ? texelsShallowCopy[currentMipLevel].fRowBytes
1303 : trimRowBytes;
Greg Daniel660cc992017-06-26 14:55:05 -04001304
ericrk154349b2016-05-10 14:36:53 -07001305 // TODO: This optimization should be enabled with or without mips.
1306 // For use with mips, we must set GR_GL_UNPACK_ROW_LENGTH once per
1307 // mip level, before calling glTexImage2D.
Brian Salomona9b04b92018-06-01 15:04:28 -04001308 if (caps.unpackRowLengthSupport() && !usesMips) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001309 // can't use this for flipping, only non-neg values allowed. :(
1310 if (rowBytes != trimRowBytes) {
1311 GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
cblume55f2d2d2016-02-26 13:20:48 -08001312 GR_GL_CALL(interface, PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength));
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001313 restoreGLRowLength = true;
1314 }
Brian Salomona9b04b92018-06-01 15:04:28 -04001315 } else if (trimRowBytes != rowBytes) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -04001316 // copy data into our new storage, skipping the trailing bytes
1317 const char* src = (const char*)texelsShallowCopy[currentMipLevel].fPixels;
Greg Daniel55afd6d2017-09-29 09:32:44 -04001318 char* dst = buffer + individualMipOffsets[currentMipLevel];
Brian Salomona6948702018-06-01 15:33:20 -04001319 SkRectMemcpy(dst, trimRowBytes, src, rowBytes, trimRowBytes, currentHeight);
Jim Van Verth2e5eaf02017-06-21 15:55:46 -04001320 // now point data to our copied version
1321 texelsShallowCopy[currentMipLevel].fPixels = buffer +
Greg Daniel55afd6d2017-09-29 09:32:44 -04001322 individualMipOffsets[currentMipLevel];
Jim Van Verth2e5eaf02017-06-21 15:55:46 -04001323 texelsShallowCopy[currentMipLevel].fRowBytes = trimRowBytes;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001324 }
bsalomone699d0c2016-03-09 06:25:15 -08001325 }
1326
Robert Phillips590533f2017-07-11 14:22:35 -04001327 if (mipLevelCount) {
Brian Salomond1eaf492017-05-18 10:02:08 -04001328 GR_GL_CALL(interface, PixelStorei(GR_GL_UNPACK_ALIGNMENT, config_alignment(texConfig)));
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001329 }
cblume55f2d2d2016-02-26 13:20:48 -08001330
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001331 bool succeeded = true;
Brian Salomond1eaf492017-05-18 10:02:08 -04001332 if (kNewTexture_UploadType == uploadType) {
1333 if (0 == left && 0 == top && texWidth == width && texHeight == height) {
Robert Phillips92de6312017-05-23 07:43:48 -04001334 succeeded = allocate_and_populate_texture(
Brian Salomond1eaf492017-05-18 10:02:08 -04001335 texConfig, *interface, caps, target, internalFormat,
Robert Phillips590533f2017-07-11 14:22:35 -04001336 internalFormatForTexStorage, externalFormat, externalType,
1337 texelsShallowCopy, mipLevelCount, width, height);
Brian Salomond1eaf492017-05-18 10:02:08 -04001338 } else {
1339 succeeded = false;
1340 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001341 } else {
Robert Phillips590533f2017-07-11 14:22:35 -04001342 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
Greg Daniel55afd6d2017-09-29 09:32:44 -04001343 if (!texelsShallowCopy[currentMipLevel].fPixels) {
1344 continue;
1345 }
cblume55f2d2d2016-02-26 13:20:48 -08001346 int twoToTheMipLevel = 1 << currentMipLevel;
1347 int currentWidth = SkTMax(1, width / twoToTheMipLevel);
1348 int currentHeight = SkTMax(1, height / twoToTheMipLevel);
cblume55f2d2d2016-02-26 13:20:48 -08001349
1350 GL_CALL(TexSubImage2D(target,
1351 currentMipLevel,
1352 left, top,
1353 currentWidth,
1354 currentHeight,
1355 externalFormat, externalType,
1356 texelsShallowCopy[currentMipLevel].fPixels));
1357 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00001358 }
1359
Brian Salomona9b04b92018-06-01 15:04:28 -04001360 restore_pixelstore_state(*interface, caps, restoreGLRowLength);
cblume55f2d2d2016-02-26 13:20:48 -08001361
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001362 return succeeded;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001363}
1364
Jim Van Verth1676cb92019-01-15 13:24:45 -05001365bool GrGLGpu::uploadCompressedTexData(GrPixelConfig texConfig, int texWidth, int texHeight,
1366 GrGLenum target, GrPixelConfig dataConfig,
1367 const GrMipLevel texels[], int mipLevelCount,
1368 GrMipMapsStatus* mipMapsStatus) {
1369 SkASSERT(this->caps()->isConfigTexturable(texConfig));
1370
1371 const GrGLInterface* interface = this->glInterface();
1372 const GrGLCaps& caps = this->glCaps();
1373
1374 // We only need the internal format for compressed 2D textures.
1375 GrGLenum internalFormat;
1376 if (!caps.getCompressedTexImageFormats(texConfig, &internalFormat)) {
1377 return false;
1378 }
1379
1380 if (mipMapsStatus && mipLevelCount <= 1) {
1381 *mipMapsStatus = GrMipMapsStatus::kNotAllocated;
1382 } else {
1383 *mipMapsStatus = GrMipMapsStatus::kValid;
1384 }
1385
1386 return allocate_and_populate_compressed_texture(texConfig, *interface, caps, target,
1387 internalFormat, texels, mipLevelCount,
1388 texWidth, texHeight);
1389
1390 return true;
1391}
1392
bsalomon424cc262015-05-22 10:37:30 -07001393static bool renderbuffer_storage_msaa(const GrGLContext& ctx,
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001394 int sampleCount,
1395 GrGLenum format,
1396 int width, int height) {
robertphillips@google.com6177e692013-02-28 20:16:25 +00001397 CLEAR_ERROR_BEFORE_ALLOC(ctx.interface());
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +00001398 SkASSERT(GrGLCaps::kNone_MSFBOType != ctx.caps()->msFBOType());
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +00001399 switch (ctx.caps()->msFBOType()) {
Brian Salomon00731b42016-10-14 11:30:51 -04001400 case GrGLCaps::kStandard_MSFBOType:
vbuzinovdded6962015-06-12 08:59:45 -07001401 case GrGLCaps::kMixedSamples_MSFBOType:
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001402 GL_ALLOC_CALL(ctx.interface(),
1403 RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
1404 sampleCount,
1405 format,
1406 width, height));
1407 break;
1408 case GrGLCaps::kES_Apple_MSFBOType:
1409 GL_ALLOC_CALL(ctx.interface(),
1410 RenderbufferStorageMultisampleES2APPLE(GR_GL_RENDERBUFFER,
1411 sampleCount,
1412 format,
1413 width, height));
1414 break;
1415 case GrGLCaps::kES_EXT_MsToTexture_MSFBOType:
1416 case GrGLCaps::kES_IMG_MsToTexture_MSFBOType:
1417 GL_ALLOC_CALL(ctx.interface(),
1418 RenderbufferStorageMultisampleES2EXT(GR_GL_RENDERBUFFER,
1419 sampleCount,
1420 format,
1421 width, height));
1422 break;
1423 case GrGLCaps::kNone_MSFBOType:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -04001424 SK_ABORT("Shouldn't be here if we don't support multisampled renderbuffers.");
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001425 break;
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001426 }
Brian Salomon9251d4e2015-03-17 16:03:19 -04001427 return (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctx.interface()));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001428}
1429
egdanielb0e1be22015-04-22 13:27:39 -07001430bool GrGLGpu::createRenderTargetObjects(const GrSurfaceDesc& desc,
bsalomon091f60c2015-11-10 11:54:56 -08001431 const GrGLTextureInfo& texInfo,
bsalomonb15b4c12014-10-29 12:41:57 -07001432 GrGLRenderTarget::IDDesc* idDesc) {
1433 idDesc->fMSColorRenderbufferID = 0;
egdanield803f272015-03-18 13:01:52 -07001434 idDesc->fRTFBOID = 0;
kkinnunen2e6055b2016-04-22 01:48:29 -07001435 idDesc->fRTFBOOwnership = GrBackendObjectOwnership::kOwned;
egdanield803f272015-03-18 13:01:52 -07001436 idDesc->fTexFBOID = 0;
robertphillips76948d42016-05-04 12:47:41 -07001437 SkASSERT((GrGLCaps::kMixedSamples_MSFBOType == this->glCaps().msFBOType()) ==
1438 this->caps()->usesMixedSamples());
Brian Salomonbdecacf2018-02-02 20:32:49 -05001439 idDesc->fIsMixedSampled = desc.fSampleCnt > 1 && this->caps()->usesMixedSamples();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001440
1441 GrGLenum status;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001442
bsalomona11e5fc2015-12-18 07:59:41 -08001443 GrGLenum colorRenderbufferFormat = 0; // suppress warning
bsalomon@google.comab15d612011-08-09 12:57:56 +00001444
Brian Salomonbdecacf2018-02-02 20:32:49 -05001445 if (desc.fSampleCnt > 1 && GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.com347c3822013-05-01 20:10:01 +00001446 goto FAILED;
1447 }
1448
egdanield803f272015-03-18 13:01:52 -07001449 GL_CALL(GenFramebuffers(1, &idDesc->fTexFBOID));
1450 if (!idDesc->fTexFBOID) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001451 goto FAILED;
1452 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001453
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001454 // If we are using multisampling we will create two FBOS. We render to one and then resolve to
1455 // the texture bound to the other. The exception is the IMG multisample extension. With this
1456 // extension the texture is multisampled when rendered to and then auto-resolves it when it is
1457 // rendered from.
Brian Salomonbdecacf2018-02-02 20:32:49 -05001458 if (desc.fSampleCnt > 1 && this->glCaps().usesMSAARenderBuffers()) {
egdanield803f272015-03-18 13:01:52 -07001459 GL_CALL(GenFramebuffers(1, &idDesc->fRTFBOID));
bsalomonb15b4c12014-10-29 12:41:57 -07001460 GL_CALL(GenRenderbuffers(1, &idDesc->fMSColorRenderbufferID));
egdanield803f272015-03-18 13:01:52 -07001461 if (!idDesc->fRTFBOID ||
bsalomona11e5fc2015-12-18 07:59:41 -08001462 !idDesc->fMSColorRenderbufferID) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001463 goto FAILED;
1464 }
Brian Salomon93348dd2018-08-29 12:56:23 -04001465 this->glCaps().getRenderbufferFormat(desc.fConfig, &colorRenderbufferFormat);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001466 } else {
egdanield803f272015-03-18 13:01:52 -07001467 idDesc->fRTFBOID = idDesc->fTexFBOID;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001468 }
1469
egdanield803f272015-03-18 13:01:52 -07001470 // below here we may bind the FBO
Robert Phillips294870f2016-11-11 12:38:40 -05001471 fHWBoundRenderTargetUniqueID.makeInvalid();
egdanield803f272015-03-18 13:01:52 -07001472 if (idDesc->fRTFBOID != idDesc->fTexFBOID) {
Brian Salomonbdecacf2018-02-02 20:32:49 -05001473 SkASSERT(desc.fSampleCnt > 1);
bsalomonb15b4c12014-10-29 12:41:57 -07001474 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, idDesc->fMSColorRenderbufferID));
bsalomon424cc262015-05-22 10:37:30 -07001475 if (!renderbuffer_storage_msaa(*fGLContext,
bsalomonb15b4c12014-10-29 12:41:57 -07001476 desc.fSampleCnt,
bsalomona11e5fc2015-12-18 07:59:41 -08001477 colorRenderbufferFormat,
bsalomonb15b4c12014-10-29 12:41:57 -07001478 desc.fWidth, desc.fHeight)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001479 goto FAILED;
1480 }
Adrienne Walker4ee88512018-05-17 11:37:14 -07001481 this->bindFramebuffer(GR_GL_FRAMEBUFFER, idDesc->fRTFBOID);
egdanield803f272015-03-18 13:01:52 -07001482 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon10528f12015-10-14 12:54:52 -07001483 GR_GL_COLOR_ATTACHMENT0,
1484 GR_GL_RENDERBUFFER,
1485 idDesc->fMSColorRenderbufferID));
Brian Salomon0ec981b2017-05-15 13:48:50 -04001486 if (!this->glCaps().isConfigVerifiedColorAttachment(desc.fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001487 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1488 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
1489 goto FAILED;
1490 }
bsalomon424cc262015-05-22 10:37:30 -07001491 fGLContext->caps()->markConfigAsValidColorAttachment(desc.fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001492 }
1493 }
Adrienne Walker4ee88512018-05-17 11:37:14 -07001494 this->bindFramebuffer(GR_GL_FRAMEBUFFER, idDesc->fTexFBOID);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001495
Brian Salomonbdecacf2018-02-02 20:32:49 -05001496 if (this->glCaps().usesImplicitMSAAResolve() && desc.fSampleCnt > 1) {
egdanield803f272015-03-18 13:01:52 -07001497 GL_CALL(FramebufferTexture2DMultisample(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001498 GR_GL_COLOR_ATTACHMENT0,
bsalomon091f60c2015-11-10 11:54:56 -08001499 texInfo.fTarget,
1500 texInfo.fID, 0, desc.fSampleCnt));
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001501 } else {
egdanield803f272015-03-18 13:01:52 -07001502 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001503 GR_GL_COLOR_ATTACHMENT0,
bsalomon091f60c2015-11-10 11:54:56 -08001504 texInfo.fTarget,
1505 texInfo.fID, 0));
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001506 }
Brian Salomon0ec981b2017-05-15 13:48:50 -04001507 if (!this->glCaps().isConfigVerifiedColorAttachment(desc.fConfig)) {
egdanield803f272015-03-18 13:01:52 -07001508 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001509 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
1510 goto FAILED;
1511 }
bsalomon424cc262015-05-22 10:37:30 -07001512 fGLContext->caps()->markConfigAsValidColorAttachment(desc.fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001513 }
1514
1515 return true;
1516
1517FAILED:
bsalomonb15b4c12014-10-29 12:41:57 -07001518 if (idDesc->fMSColorRenderbufferID) {
1519 GL_CALL(DeleteRenderbuffers(1, &idDesc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001520 }
egdanield803f272015-03-18 13:01:52 -07001521 if (idDesc->fRTFBOID != idDesc->fTexFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -07001522 this->deleteFramebuffer(idDesc->fRTFBOID);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001523 }
egdanield803f272015-03-18 13:01:52 -07001524 if (idDesc->fTexFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -07001525 this->deleteFramebuffer(idDesc->fTexFBOID);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001526 }
1527 return false;
1528}
1529
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001530// good to set a break-point here to know when createTexture fails
Robert Phillips67d52cf2017-06-05 13:38:13 -04001531static sk_sp<GrTexture> return_null_texture() {
mtklein@google.com330313a2013-08-22 15:37:26 +00001532// SkDEBUGFAIL("null texture");
halcanary96fcdcc2015-08-27 07:41:13 -07001533 return nullptr;
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001534}
1535
Brian Salomondc829942018-10-23 16:07:24 -04001536static GrGLTexture::SamplerParams set_initial_texture_params(const GrGLInterface* interface,
1537 const GrGLTextureInfo& info) {
cblume55f2d2d2016-02-26 13:20:48 -08001538 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
1539 // drivers have a bug where an FBO won't be complete if it includes a
1540 // texture that is not mipmap complete (considering the filter in use).
Brian Salomondc829942018-10-23 16:07:24 -04001541 GrGLTexture::SamplerParams params;
1542 params.fMinFilter = GR_GL_NEAREST;
1543 params.fMagFilter = GR_GL_NEAREST;
1544 params.fWrapS = GR_GL_CLAMP_TO_EDGE;
1545 params.fWrapT = GR_GL_CLAMP_TO_EDGE;
1546 GR_GL_CALL(interface, TexParameteri(info.fTarget, GR_GL_TEXTURE_MAG_FILTER, params.fMagFilter));
1547 GR_GL_CALL(interface, TexParameteri(info.fTarget, GR_GL_TEXTURE_MIN_FILTER, params.fMinFilter));
1548 GR_GL_CALL(interface, TexParameteri(info.fTarget, GR_GL_TEXTURE_WRAP_S, params.fWrapS));
1549 GR_GL_CALL(interface, TexParameteri(info.fTarget, GR_GL_TEXTURE_WRAP_T, params.fWrapT));
1550 return params;
cblume55f2d2d2016-02-26 13:20:48 -08001551}
1552
Robert Phillips67d52cf2017-06-05 13:38:13 -04001553sk_sp<GrTexture> GrGLGpu::onCreateTexture(const GrSurfaceDesc& desc,
1554 SkBudgeted budgeted,
Robert Phillips590533f2017-07-11 14:22:35 -04001555 const GrMipLevel texels[],
1556 int mipLevelCount) {
bsalomon@google.com8a70eef2013-03-19 13:58:55 +00001557 // We fail if the MSAA was requested and is not available.
Brian Salomonbdecacf2018-02-02 20:32:49 -05001558 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType() && desc.fSampleCnt > 1) {
tfarina38406c82014-10-31 07:11:12 -07001559 //SkDebugf("MSAA RT requested but not supported on this platform.");
bsalomon@google.com8a70eef2013-03-19 13:58:55 +00001560 return return_null_texture();
1561 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001562
Jim Van Verth1676cb92019-01-15 13:24:45 -05001563 bool performClear = (desc.fFlags & kPerformInitialClear_GrSurfaceFlag) &&
1564 !GrPixelConfigIsCompressed(desc.fConfig);
Brian Salomond17b4a62017-05-23 16:53:47 -04001565
Robert Phillips590533f2017-07-11 14:22:35 -04001566 GrMipLevel zeroLevel;
Brian Salomond17b4a62017-05-23 16:53:47 -04001567 std::unique_ptr<uint8_t[]> zeros;
Brian Salomond17b4a62017-05-23 16:53:47 -04001568 if (performClear && !this->glCaps().clearTextureSupport() &&
Brian Salomon57111332018-02-05 15:55:54 -05001569 !this->glCaps().canConfigBeFBOColorAttachment(desc.fConfig)) {
Brian Salomond17b4a62017-05-23 16:53:47 -04001570 size_t rowSize = GrBytesPerPixel(desc.fConfig) * desc.fWidth;
1571 size_t size = rowSize * desc.fHeight;
1572 zeros.reset(new uint8_t[size]);
1573 memset(zeros.get(), 0, size);
Robert Phillips590533f2017-07-11 14:22:35 -04001574 zeroLevel.fPixels = zeros.get();
1575 zeroLevel.fRowBytes = 0;
1576 texels = &zeroLevel;
1577 mipLevelCount = 1;
Brian Salomond17b4a62017-05-23 16:53:47 -04001578 performClear = false;
1579 }
1580
Brian Salomond34edf32017-05-19 15:45:48 -04001581 bool isRenderTarget = SkToBool(desc.fFlags & kRenderTarget_GrSurfaceFlag);
reed@google.comac10a2d2010-12-22 21:39:39 +00001582
bsalomonb15b4c12014-10-29 12:41:57 -07001583 GrGLTexture::IDDesc idDesc;
kkinnunen2e6055b2016-04-22 01:48:29 -07001584 idDesc.fOwnership = GrBackendObjectOwnership::kOwned;
Greg Daniel0fc4d2d2017-10-12 11:23:36 -04001585 GrMipMapsStatus mipMapsStatus;
Brian Salomondc829942018-10-23 16:07:24 -04001586 GrGLTexture::SamplerParams initialTexParams;
Brian Salomon58389b92018-03-07 13:01:25 -05001587 if (!this->createTextureImpl(desc, &idDesc.fInfo, isRenderTarget, &initialTexParams, texels,
1588 mipLevelCount, &mipMapsStatus)) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001589 return return_null_texture();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001590 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001591
Robert Phillips67d52cf2017-06-05 13:38:13 -04001592 sk_sp<GrGLTexture> tex;
Brian Salomond34edf32017-05-19 15:45:48 -04001593 if (isRenderTarget) {
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001594 // unbind the texture from the texture unit before binding it to the frame buffer
bsalomon091f60c2015-11-10 11:54:56 -08001595 GL_CALL(BindTexture(idDesc.fInfo.fTarget, 0));
bsalomon5236cf42015-01-14 10:42:08 -08001596 GrGLRenderTarget::IDDesc rtIDDesc;
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001597
kkinnunen2e6055b2016-04-22 01:48:29 -07001598 if (!this->createRenderTargetObjects(desc, idDesc.fInfo, &rtIDDesc)) {
bsalomon091f60c2015-11-10 11:54:56 -08001599 GL_CALL(DeleteTextures(1, &idDesc.fInfo.fID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001600 return return_null_texture();
1601 }
Robert Phillips67d52cf2017-06-05 13:38:13 -04001602 tex = sk_make_sp<GrGLTextureRenderTarget>(this, budgeted, desc, idDesc, rtIDDesc,
Greg Daniel0fc4d2d2017-10-12 11:23:36 -04001603 mipMapsStatus);
Brian Salomon9bada542017-06-12 12:09:30 -04001604 tex->baseLevelWasBoundToFBO();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001605 } else {
Greg Daniel0fc4d2d2017-10-12 11:23:36 -04001606 tex = sk_make_sp<GrGLTexture>(this, budgeted, desc, idDesc, mipMapsStatus);
reed@google.comac10a2d2010-12-22 21:39:39 +00001607 }
Brian Salomondc829942018-10-23 16:07:24 -04001608
1609 tex->setCachedParams(&initialTexParams, tex->getCachedNonSamplerParams(),
1610 this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00001611#ifdef TRACE_TEXTURE_CREATION
tfarina38406c82014-10-31 07:11:12 -07001612 SkDebugf("--- new texture [%d] size=(%d %d) config=%d\n",
brianosmanc4459f62016-07-19 08:02:20 -07001613 idDesc.fInfo.fID, desc.fWidth, desc.fHeight, desc.fConfig);
reed@google.comac10a2d2010-12-22 21:39:39 +00001614#endif
Brian Salomond17b4a62017-05-23 16:53:47 -04001615 if (tex && performClear) {
1616 if (this->glCaps().clearTextureSupport()) {
Brian Salomond17b4a62017-05-23 16:53:47 -04001617 static constexpr uint32_t kZero = 0;
Brian Salomon57111332018-02-05 15:55:54 -05001618 GL_CALL(ClearTexImage(tex->textureID(), 0, GR_GL_RGBA, GR_GL_UNSIGNED_BYTE, &kZero));
Brian Salomond17b4a62017-05-23 16:53:47 -04001619 } else {
Brian Salomond17b4a62017-05-23 16:53:47 -04001620 GrGLIRect viewport;
Robert Phillips67d52cf2017-06-05 13:38:13 -04001621 this->bindSurfaceFBOForPixelOps(tex.get(), GR_GL_FRAMEBUFFER, &viewport,
1622 kDst_TempFBOTarget);
Brian Salomond818ebf2018-07-02 14:08:49 +00001623 this->disableScissor();
Brian Salomond17b4a62017-05-23 16:53:47 -04001624 this->disableWindowRectangles();
1625 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
1626 fHWWriteToColor = kYes_TriState;
1627 GL_CALL(ClearColor(0, 0, 0, 0));
1628 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
Robert Phillips67d52cf2017-06-05 13:38:13 -04001629 this->unbindTextureFBOForPixelOps(GR_GL_FRAMEBUFFER, tex.get());
Brian Salomond17b4a62017-05-23 16:53:47 -04001630 fHWBoundRenderTargetUniqueID.makeInvalid();
1631 }
1632 }
Kevin Lubickf7621cb2018-04-16 15:51:44 -04001633 return std::move(tex);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001634}
1635
1636namespace {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001637
egdaniel8dc7c3a2015-04-16 11:22:42 -07001638const GrGLuint kUnknownBitCount = GrGLStencilAttachment::kUnknownBitCount;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001639
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001640void inline get_stencil_rb_sizes(const GrGLInterface* gl,
egdaniel8dc7c3a2015-04-16 11:22:42 -07001641 GrGLStencilAttachment::Format* format) {
sugoi@google.com6ba0b0f2013-05-03 13:52:32 +00001642
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001643 // we shouldn't ever know one size and not the other
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001644 SkASSERT((kUnknownBitCount == format->fStencilBits) ==
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001645 (kUnknownBitCount == format->fTotalBits));
1646 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001647 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001648 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1649 (GrGLint*)&format->fStencilBits);
1650 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001651 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001652 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1653 (GrGLint*)&format->fTotalBits);
1654 format->fTotalBits += format->fStencilBits;
1655 } else {
1656 format->fTotalBits = format->fStencilBits;
1657 }
1658 }
1659}
1660}
1661
egdanielff1d5472015-09-10 08:37:20 -07001662int GrGLGpu::getCompatibleStencilIndex(GrPixelConfig config) {
bsalomon100b8f82015-10-28 08:37:44 -07001663 static const int kSize = 16;
Brian Salomonbdecacf2018-02-02 20:32:49 -05001664 SkASSERT(this->caps()->isConfigRenderable(config));
bsalomon30447372015-12-21 09:03:05 -08001665 if (!this->glCaps().hasStencilFormatBeenDeterminedForConfig(config)) {
1666 // Default to unsupported, set this if we find a stencil format that works.
1667 int firstWorkingStencilFormatIndex = -1;
Brian Osman91f9a2c2017-09-05 15:02:46 -04001668
egdanielff1d5472015-09-10 08:37:20 -07001669 // Create color texture
kkinnunen546eb5c2015-12-11 00:05:33 -08001670 GrGLuint colorID = 0;
egdanielff1d5472015-09-10 08:37:20 -07001671 GL_CALL(GenTextures(1, &colorID));
1672 this->setScratchTextureUnit();
1673 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, colorID));
1674 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1675 GR_GL_TEXTURE_MAG_FILTER,
1676 GR_GL_NEAREST));
1677 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1678 GR_GL_TEXTURE_MIN_FILTER,
1679 GR_GL_NEAREST));
1680 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1681 GR_GL_TEXTURE_WRAP_S,
1682 GR_GL_CLAMP_TO_EDGE));
1683 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1684 GR_GL_TEXTURE_WRAP_T,
1685 GR_GL_CLAMP_TO_EDGE));
1686
bsalomon76148af2016-01-12 11:13:47 -08001687 GrGLenum internalFormat;
1688 GrGLenum externalFormat;
1689 GrGLenum externalType;
1690 if (!this->glCaps().getTexImageFormats(config, config, &internalFormat, &externalFormat,
1691 &externalType)) {
1692 return false;
1693 }
Brian Osman9b560242017-09-05 15:34:52 -04001694 this->unbindCpuToGpuXferBuffer();
egdanielff1d5472015-09-10 08:37:20 -07001695 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1696 GL_ALLOC_CALL(this->glInterface(), TexImage2D(GR_GL_TEXTURE_2D,
bsalomon926cb022015-12-17 18:15:11 -08001697 0,
bsalomon76148af2016-01-12 11:13:47 -08001698 internalFormat,
bsalomon100b8f82015-10-28 08:37:44 -07001699 kSize,
1700 kSize,
egdanielff1d5472015-09-10 08:37:20 -07001701 0,
bsalomon76148af2016-01-12 11:13:47 -08001702 externalFormat,
1703 externalType,
Ben Wagnera93a14a2017-08-28 10:34:05 -04001704 nullptr));
bsalomon30447372015-12-21 09:03:05 -08001705 if (GR_GL_NO_ERROR != CHECK_ALLOC_ERROR(this->glInterface())) {
egdanielff1d5472015-09-10 08:37:20 -07001706 GL_CALL(DeleteTextures(1, &colorID));
bsalomon30447372015-12-21 09:03:05 -08001707 return -1;
egdanielff1d5472015-09-10 08:37:20 -07001708 }
1709
1710 // unbind the texture from the texture unit before binding it to the frame buffer
1711 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, 0));
1712
1713 // Create Framebuffer
kkinnunen546eb5c2015-12-11 00:05:33 -08001714 GrGLuint fb = 0;
egdanielff1d5472015-09-10 08:37:20 -07001715 GL_CALL(GenFramebuffers(1, &fb));
Adrienne Walker4ee88512018-05-17 11:37:14 -07001716 this->bindFramebuffer(GR_GL_FRAMEBUFFER, fb);
Robert Phillips294870f2016-11-11 12:38:40 -05001717 fHWBoundRenderTargetUniqueID.makeInvalid();
egdanielff1d5472015-09-10 08:37:20 -07001718 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
1719 GR_GL_COLOR_ATTACHMENT0,
1720 GR_GL_TEXTURE_2D,
1721 colorID,
1722 0));
bsalomon30447372015-12-21 09:03:05 -08001723 GrGLuint sbRBID = 0;
1724 GL_CALL(GenRenderbuffers(1, &sbRBID));
egdanielff1d5472015-09-10 08:37:20 -07001725
1726 // look over formats till I find a compatible one
1727 int stencilFmtCnt = this->glCaps().stencilFormats().count();
bsalomon30447372015-12-21 09:03:05 -08001728 if (sbRBID) {
egdanielff1d5472015-09-10 08:37:20 -07001729 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbRBID));
bsalomon30447372015-12-21 09:03:05 -08001730 for (int i = 0; i < stencilFmtCnt && sbRBID; ++i) {
1731 const GrGLCaps::StencilFormat& sFmt = this->glCaps().stencilFormats()[i];
1732 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1733 GL_ALLOC_CALL(this->glInterface(), RenderbufferStorage(GR_GL_RENDERBUFFER,
1734 sFmt.fInternalFormat,
1735 kSize, kSize));
1736 if (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(this->glInterface())) {
egdanielff1d5472015-09-10 08:37:20 -07001737 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon30447372015-12-21 09:03:05 -08001738 GR_GL_STENCIL_ATTACHMENT,
egdanielff1d5472015-09-10 08:37:20 -07001739 GR_GL_RENDERBUFFER, sbRBID));
bsalomon30447372015-12-21 09:03:05 -08001740 if (sFmt.fPacked) {
1741 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1742 GR_GL_DEPTH_ATTACHMENT,
1743 GR_GL_RENDERBUFFER, sbRBID));
1744 } else {
1745 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1746 GR_GL_DEPTH_ATTACHMENT,
1747 GR_GL_RENDERBUFFER, 0));
1748 }
1749 GrGLenum status;
1750 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1751 if (status == GR_GL_FRAMEBUFFER_COMPLETE) {
1752 firstWorkingStencilFormatIndex = i;
1753 break;
1754 }
egdanielff1d5472015-09-10 08:37:20 -07001755 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1756 GR_GL_STENCIL_ATTACHMENT,
1757 GR_GL_RENDERBUFFER, 0));
1758 if (sFmt.fPacked) {
1759 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1760 GR_GL_DEPTH_ATTACHMENT,
1761 GR_GL_RENDERBUFFER, 0));
1762 }
egdanielff1d5472015-09-10 08:37:20 -07001763 }
1764 }
bsalomon30447372015-12-21 09:03:05 -08001765 GL_CALL(DeleteRenderbuffers(1, &sbRBID));
egdanielff1d5472015-09-10 08:37:20 -07001766 }
1767 GL_CALL(DeleteTextures(1, &colorID));
Adrienne Walker4ee88512018-05-17 11:37:14 -07001768 this->bindFramebuffer(GR_GL_FRAMEBUFFER, 0);
1769 this->deleteFramebuffer(fb);
bsalomon30447372015-12-21 09:03:05 -08001770 fGLContext->caps()->setStencilFormatIndexForConfig(config, firstWorkingStencilFormatIndex);
egdanielff1d5472015-09-10 08:37:20 -07001771 }
bsalomon30447372015-12-21 09:03:05 -08001772 return this->glCaps().getStencilFormatIndexForConfig(config);
egdanielff1d5472015-09-10 08:37:20 -07001773}
1774
Brian Salomon2a4f9832018-03-03 22:43:43 -05001775bool GrGLGpu::createTextureImpl(const GrSurfaceDesc& desc, GrGLTextureInfo* info, bool renderTarget,
Brian Salomondc829942018-10-23 16:07:24 -04001776 GrGLTexture::SamplerParams* initialTexParams,
1777 const GrMipLevel texels[], int mipLevelCount,
1778 GrMipMapsStatus* mipMapsStatus) {
erikchen9a1ed5d2016-02-10 16:32:34 -08001779 info->fID = 0;
1780 info->fTarget = GR_GL_TEXTURE_2D;
1781 GL_CALL(GenTextures(1, &(info->fID)));
1782
1783 if (!info->fID) {
1784 return false;
1785 }
1786
1787 this->setScratchTextureUnit();
1788 GL_CALL(BindTexture(info->fTarget, info->fID));
1789
1790 if (renderTarget && this->glCaps().textureUsageSupport()) {
1791 // provides a hint about how this texture will be used
1792 GL_CALL(TexParameteri(info->fTarget,
1793 GR_GL_TEXTURE_USAGE,
1794 GR_GL_FRAMEBUFFER_ATTACHMENT));
1795 }
1796
cblume55f2d2d2016-02-26 13:20:48 -08001797 if (info) {
Brian Salomondc829942018-10-23 16:07:24 -04001798 *initialTexParams = set_initial_texture_params(this->glInterface(), *info);
cblume55f2d2d2016-02-26 13:20:48 -08001799 }
Brian Salomon2a4f9832018-03-03 22:43:43 -05001800
Jim Van Verth1676cb92019-01-15 13:24:45 -05001801 bool success = false;
1802 if (GrPixelConfigIsCompressed(desc.fConfig)) {
1803 SkASSERT(!renderTarget);
1804 success = this->uploadCompressedTexData(desc.fConfig, desc.fWidth, desc.fHeight,
1805 info->fTarget, desc.fConfig,
1806 texels, mipLevelCount, mipMapsStatus);
1807 } else {
1808 success = this->uploadTexData(desc.fConfig, desc.fWidth, desc.fHeight, info->fTarget,
1809 kNewTexture_UploadType, 0, 0, desc.fWidth, desc.fHeight,
1810 desc.fConfig, texels, mipLevelCount, mipMapsStatus);
1811 }
1812 if (!success) {
erikchen9a1ed5d2016-02-10 16:32:34 -08001813 GL_CALL(DeleteTextures(1, &(info->fID)));
1814 return false;
1815 }
Greg Daniele7d8da42017-12-04 11:23:19 -05001816 info->fFormat = this->glCaps().configSizedInternalFormat(desc.fConfig);
erikchen9a1ed5d2016-02-10 16:32:34 -08001817 return true;
1818}
1819
egdanielec00d942015-09-14 12:56:10 -07001820GrStencilAttachment* GrGLGpu::createStencilAttachmentForRenderTarget(const GrRenderTarget* rt,
Brian Salomond1eaf492017-05-18 10:02:08 -04001821 int width, int height) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001822 SkASSERT(width >= rt->width());
1823 SkASSERT(height >= rt->height());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001824
vbuzinovdded6962015-06-12 08:59:45 -07001825 int samples = rt->numStencilSamples();
egdaniel8dc7c3a2015-04-16 11:22:42 -07001826 GrGLStencilAttachment::IDDesc sbDesc;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001827
egdanielff1d5472015-09-10 08:37:20 -07001828 int sIdx = this->getCompatibleStencilIndex(rt->config());
bsalomon62a627b2015-12-17 09:50:47 -08001829 if (sIdx < 0) {
egdanielec00d942015-09-14 12:56:10 -07001830 return nullptr;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001831 }
egdanielff1d5472015-09-10 08:37:20 -07001832
1833 if (!sbDesc.fRenderbufferID) {
1834 GL_CALL(GenRenderbuffers(1, &sbDesc.fRenderbufferID));
1835 }
1836 if (!sbDesc.fRenderbufferID) {
egdanielec00d942015-09-14 12:56:10 -07001837 return nullptr;
egdanielff1d5472015-09-10 08:37:20 -07001838 }
1839 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbDesc.fRenderbufferID));
1840 const GrGLCaps::StencilFormat& sFmt = this->glCaps().stencilFormats()[sIdx];
1841 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1842 // we do this "if" so that we don't call the multisample
1843 // version on a GL that doesn't have an MSAA extension.
Brian Salomonbdecacf2018-02-02 20:32:49 -05001844 if (samples > 1) {
egdanielff1d5472015-09-10 08:37:20 -07001845 SkAssertResult(renderbuffer_storage_msaa(*fGLContext,
1846 samples,
1847 sFmt.fInternalFormat,
1848 width, height));
1849 } else {
1850 GL_ALLOC_CALL(this->glInterface(), RenderbufferStorage(GR_GL_RENDERBUFFER,
1851 sFmt.fInternalFormat,
1852 width, height));
Brian Salomon0ec981b2017-05-15 13:48:50 -04001853 SkASSERT(GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(this->glInterface()));
egdanielff1d5472015-09-10 08:37:20 -07001854 }
1855 fStats.incStencilAttachmentCreates();
1856 // After sized formats we attempt an unsized format and take
1857 // whatever sizes GL gives us. In that case we query for the size.
1858 GrGLStencilAttachment::Format format = sFmt;
1859 get_stencil_rb_sizes(this->glInterface(), &format);
egdanielec00d942015-09-14 12:56:10 -07001860 GrGLStencilAttachment* stencil = new GrGLStencilAttachment(this,
1861 sbDesc,
1862 width,
1863 height,
1864 samples,
1865 format);
1866 return stencil;
reed@google.comac10a2d2010-12-22 21:39:39 +00001867}
1868
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001869////////////////////////////////////////////////////////////////////////////////
1870
jvanverth73063dc2015-12-03 09:15:47 -08001871// GL_STREAM_DRAW triggers an optimization in Chromium's GPU process where a client's vertex buffer
1872// objects are implemented as client-side-arrays on tile-deferred architectures.
1873#define DYNAMIC_USAGE_PARAM GR_GL_STREAM_DRAW
1874
cdaltone2e71c22016-04-07 18:13:29 -07001875GrBuffer* GrGLGpu::onCreateBuffer(size_t size, GrBufferType intendedType,
cdalton1bf3e712016-04-19 10:00:02 -07001876 GrAccessPattern accessPattern, const void* data) {
1877 return GrGLBuffer::Create(this, size, intendedType, accessPattern, data);
jvanverth73063dc2015-12-03 09:15:47 -08001878}
1879
Brian Salomond818ebf2018-07-02 14:08:49 +00001880void GrGLGpu::flushScissor(const GrScissorState& scissorState,
1881 const GrGLIRect& rtViewport,
1882 GrSurfaceOrigin rtOrigin) {
1883 if (scissorState.enabled()) {
1884 GrGLIRect scissor;
1885 scissor.setRelativeTo(rtViewport, scissorState.rect(), rtOrigin);
1886 // if the scissor fully contains the viewport then we fall through and
1887 // disable the scissor test.
1888 if (!scissor.contains(rtViewport)) {
1889 if (fHWScissorSettings.fRect != scissor) {
1890 scissor.pushToGLScissor(this->glInterface());
1891 fHWScissorSettings.fRect = scissor;
1892 }
1893 if (kYes_TriState != fHWScissorSettings.fEnabled) {
1894 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1895 fHWScissorSettings.fEnabled = kYes_TriState;
bsalomon@google.coma3201942012-06-21 19:58:20 +00001896 }
1897 return;
1898 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001899 }
Brian Salomond818ebf2018-07-02 14:08:49 +00001900
1901 // See fall through note above
1902 this->disableScissor();
joshualitt77b13072014-10-27 14:51:01 -07001903}
1904
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001905void GrGLGpu::flushWindowRectangles(const GrWindowRectsState& windowState,
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001906 const GrGLRenderTarget* rt, GrSurfaceOrigin origin) {
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001907#ifndef USE_NSIGHT
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001908 typedef GrWindowRectsState::Mode Mode;
1909 SkASSERT(!windowState.enabled() || rt->renderFBOID()); // Window rects can't be used on-screen.
1910 SkASSERT(windowState.numWindows() <= this->caps()->maxWindowRectangles());
csmartdalton28341fa2016-08-17 10:00:21 -07001911
Jim Van Verth6a40abc2017-11-02 16:56:09 +00001912 if (!this->caps()->maxWindowRectangles() ||
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001913 fHWWindowRectsState.knownEqualTo(origin, rt->getViewport(), windowState)) {
csmartdalton28341fa2016-08-17 10:00:21 -07001914 return;
csmartdalton28341fa2016-08-17 10:00:21 -07001915 }
1916
csmartdalton7535f412016-08-23 06:51:00 -07001917 // This is purely a workaround for a spurious warning generated by gcc. Otherwise the above
1918 // assert would be sufficient. https://gcc.gnu.org/bugzilla/show_bug.cgi?id=5912
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001919 int numWindows = SkTMin(windowState.numWindows(), int(GrWindowRectangles::kMaxWindows));
1920 SkASSERT(windowState.numWindows() == numWindows);
csmartdalton7535f412016-08-23 06:51:00 -07001921
csmartdalton28341fa2016-08-17 10:00:21 -07001922 GrGLIRect glwindows[GrWindowRectangles::kMaxWindows];
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001923 const SkIRect* skwindows = windowState.windows().data();
csmartdalton7535f412016-08-23 06:51:00 -07001924 for (int i = 0; i < numWindows; ++i) {
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001925 glwindows[i].setRelativeTo(rt->getViewport(), skwindows[i], origin);
csmartdalton28341fa2016-08-17 10:00:21 -07001926 }
1927
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001928 GrGLenum glmode = (Mode::kExclusive == windowState.mode()) ? GR_GL_EXCLUSIVE : GR_GL_INCLUSIVE;
csmartdalton7535f412016-08-23 06:51:00 -07001929 GL_CALL(WindowRectangles(glmode, numWindows, glwindows->asInts()));
csmartdalton28341fa2016-08-17 10:00:21 -07001930
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001931 fHWWindowRectsState.set(origin, rt->getViewport(), windowState);
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001932#endif
csmartdalton28341fa2016-08-17 10:00:21 -07001933}
1934
1935void GrGLGpu::disableWindowRectangles() {
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001936#ifndef USE_NSIGHT
Jim Van Verth6a40abc2017-11-02 16:56:09 +00001937 if (!this->caps()->maxWindowRectangles() || fHWWindowRectsState.knownDisabled()) {
csmartdalton28341fa2016-08-17 10:00:21 -07001938 return;
1939 }
1940 GL_CALL(WindowRectangles(GR_GL_EXCLUSIVE, 0, nullptr));
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001941 fHWWindowRectsState.setDisabled();
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001942#endif
csmartdalton28341fa2016-08-17 10:00:21 -07001943}
1944
Brian Salomonf7232642018-09-19 08:58:08 -04001945void GrGLGpu::resolveAndGenerateMipMapsForProcessorTextures(
1946 const GrPrimitiveProcessor& primProc,
1947 const GrPipeline& pipeline,
1948 const GrTextureProxy* const primProcTextures[],
1949 int numPrimitiveProcessorTextureSets) {
Brian Salomondfec99f2018-08-02 10:40:05 -04001950 auto genLevelsIfNeeded = [this](GrTexture* tex, const GrSamplerState& sampler) {
Brian Salomon7eae3e02018-08-07 14:02:38 +00001951 SkASSERT(tex);
Brian Salomondfec99f2018-08-02 10:40:05 -04001952 if (sampler.filter() == GrSamplerState::Filter::kMipMap &&
1953 tex->texturePriv().mipMapped() == GrMipMapped::kYes &&
1954 tex->texturePriv().mipMapsAreDirty()) {
1955 SkASSERT(this->caps()->mipMapSupport());
1956 this->regenerateMipMapLevels(static_cast<GrGLTexture*>(tex));
Brian Salomonf7232642018-09-19 08:58:08 -04001957 SkASSERT(!tex->asRenderTarget() || !tex->asRenderTarget()->needsResolve());
1958 } else if (auto* rt = tex->asRenderTarget()) {
1959 if (rt->needsResolve()) {
1960 this->resolveRenderTarget(rt);
1961 }
Brian Salomondfec99f2018-08-02 10:40:05 -04001962 }
1963 };
1964
Brian Salomonf7232642018-09-19 08:58:08 -04001965 for (int set = 0, tex = 0; set < numPrimitiveProcessorTextureSets; ++set) {
1966 for (int sampler = 0; sampler < primProc.numTextureSamplers(); ++sampler, ++tex) {
1967 GrTexture* texture = primProcTextures[tex]->peekTexture();
1968 genLevelsIfNeeded(texture, primProc.textureSampler(sampler).samplerState());
1969 }
Brian Salomondfec99f2018-08-02 10:40:05 -04001970 }
1971
1972 GrFragmentProcessor::Iter iter(pipeline);
1973 while (const GrFragmentProcessor* fp = iter.next()) {
1974 for (int i = 0; i < fp->numTextureSamplers(); ++i) {
1975 const auto& textureSampler = fp->textureSampler(i);
1976 genLevelsIfNeeded(textureSampler.peekTexture(), textureSampler.samplerState());
1977 }
1978 }
1979}
1980
Brian Salomon49348902018-06-26 09:12:38 -04001981bool GrGLGpu::flushGLState(const GrPrimitiveProcessor& primProc,
1982 const GrPipeline& pipeline,
1983 const GrPipeline::FixedDynamicState* fixedDynamicState,
Brian Salomonf7232642018-09-19 08:58:08 -04001984 const GrPipeline::DynamicStateArrays* dynamicStateArrays,
1985 int dynamicStateArraysLength,
bsalomon2eda5b32016-09-21 10:53:24 -07001986 bool willDrawPoints) {
Brian Salomonf7232642018-09-19 08:58:08 -04001987 const GrTextureProxy* const* primProcProxiesForMipRegen = nullptr;
1988 const GrTextureProxy* const* primProcProxiesToBind = nullptr;
1989 int numPrimProcTextureSets = 1; // number of texture per prim proc sampler.
1990 if (dynamicStateArrays && dynamicStateArrays->fPrimitiveProcessorTextures) {
1991 primProcProxiesForMipRegen = dynamicStateArrays->fPrimitiveProcessorTextures;
1992 numPrimProcTextureSets = dynamicStateArraysLength;
1993 } else if (fixedDynamicState && fixedDynamicState->fPrimitiveProcessorTextures) {
1994 primProcProxiesForMipRegen = fixedDynamicState->fPrimitiveProcessorTextures;
1995 primProcProxiesToBind = fixedDynamicState->fPrimitiveProcessorTextures;
Brian Salomon7eae3e02018-08-07 14:02:38 +00001996 }
Greg Daniel9a51a862018-11-30 10:18:14 -05001997
1998 SkASSERT(SkToBool(primProcProxiesForMipRegen) == SkToBool(primProc.numTextureSamplers()));
1999
2000 sk_sp<GrGLProgram> program(fProgramCache->refProgram(this, primProc, primProcProxiesForMipRegen,
2001 pipeline, willDrawPoints));
2002 if (!program) {
2003 GrCapsDebugf(this->caps(), "Failed to create program!\n");
2004 return false;
2005 }
Brian Salomonf7232642018-09-19 08:58:08 -04002006 this->resolveAndGenerateMipMapsForProcessorTextures(
2007 primProc, pipeline, primProcProxiesForMipRegen, numPrimProcTextureSets);
brianosman33f6b3f2016-06-02 05:49:21 -07002008
egdaniel080e6732014-12-22 07:35:52 -08002009 GrXferProcessor::BlendInfo blendInfo;
egdaniel0e1853c2016-03-17 11:35:45 -07002010 pipeline.getXferProcessor().getBlendInfo(&blendInfo);
egdaniel080e6732014-12-22 07:35:52 -08002011
egdaniel080e6732014-12-22 07:35:52 -08002012 this->flushColorWrite(blendInfo.fWriteColor);
bsalomonbc3d0de2014-12-15 13:45:03 -08002013
Brian Salomon802cb312018-06-08 18:05:20 -04002014 this->flushProgram(std::move(program));
bsalomon1f78c0a2014-12-17 09:43:13 -08002015
Dongseong Hwang4e1f0222018-11-01 09:10:51 -07002016 // Swizzle the blend to match what the shader will output.
2017 const GrSwizzle& swizzle = this->caps()->shaderCaps()->configOutputSwizzle(
2018 pipeline.proxy()->config());
2019 this->flushBlend(blendInfo, swizzle);
bsalomon1f78c0a2014-12-17 09:43:13 -08002020
Brian Salomonf7232642018-09-19 08:58:08 -04002021 fHWProgram->updateUniformsAndTextureBindings(primProc, pipeline, primProcProxiesToBind);
bsalomon1f78c0a2014-12-17 09:43:13 -08002022
Robert Phillips2890fbf2017-07-26 15:48:41 -04002023 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(pipeline.renderTarget());
csmartdaltonc633abb2016-11-01 08:55:55 -07002024 GrStencilSettings stencil;
2025 if (pipeline.isStencilEnabled()) {
2026 // TODO: attach stencil and create settings during render target flush.
2027 SkASSERT(glRT->renderTargetPriv().getStencilAttachment());
2028 stencil.reset(*pipeline.getUserStencil(), pipeline.hasStencilClip(),
2029 glRT->renderTargetPriv().numStencilBits());
2030 }
2031 this->flushStencil(stencil);
Brian Salomond818ebf2018-07-02 14:08:49 +00002032 if (pipeline.isScissorEnabled()) {
2033 static constexpr SkIRect kBogusScissor{0, 0, 1, 1};
2034 GrScissorState state(fixedDynamicState ? fixedDynamicState->fScissorRect : kBogusScissor);
Chris Daltonc8ece3d2018-07-30 15:03:45 -06002035 this->flushScissor(state, glRT->getViewport(), pipeline.proxy()->origin());
Brian Salomond818ebf2018-07-02 14:08:49 +00002036 } else {
2037 this->disableScissor();
Brian Salomon49348902018-06-26 09:12:38 -04002038 }
Chris Daltonc8ece3d2018-07-30 15:03:45 -06002039 this->flushWindowRectangles(pipeline.getWindowRectsState(), glRT, pipeline.proxy()->origin());
csmartdaltondabc91b2016-11-15 14:26:27 -07002040 this->flushHWAAState(glRT, pipeline.isHWAntialiasState(), !stencil.isDisabled());
bsalomonbc3d0de2014-12-15 13:45:03 -08002041
2042 // This must come after textures are flushed because a texture may need
egdanield803f272015-03-18 13:01:52 -07002043 // to be msaa-resolved (which will modify bound FBO state).
Chris Daltonc8ece3d2018-07-30 15:03:45 -06002044 this->flushRenderTarget(glRT);
bsalomonbc3d0de2014-12-15 13:45:03 -08002045
2046 return true;
2047}
2048
Brian Salomon802cb312018-06-08 18:05:20 -04002049void GrGLGpu::flushProgram(sk_sp<GrGLProgram> program) {
2050 if (!program) {
2051 fHWProgram.reset();
2052 fHWProgramID = 0;
2053 return;
2054 }
2055 SkASSERT((program == fHWProgram) == (fHWProgramID == program->programID()));
2056 if (program == fHWProgram) {
2057 return;
2058 }
2059 auto id = program->programID();
2060 SkASSERT(id);
2061 GL_CALL(UseProgram(id));
2062 fHWProgram = std::move(program);
2063 fHWProgramID = id;
2064}
2065
2066void GrGLGpu::flushProgram(GrGLuint id) {
2067 SkASSERT(id);
2068 if (fHWProgramID == id) {
2069 SkASSERT(!fHWProgram);
2070 return;
2071 }
2072 fHWProgram.reset();
2073 GL_CALL(UseProgram(id));
2074 fHWProgramID = id;
2075}
2076
2077void GrGLGpu::setupGeometry(const GrBuffer* indexBuffer,
Chris Daltonff926502017-05-03 14:36:54 -04002078 const GrBuffer* vertexBuffer,
Chris Dalton1d616352017-05-31 12:51:23 -06002079 int baseVertex,
2080 const GrBuffer* instanceBuffer,
Brian Salomon802cb312018-06-08 18:05:20 -04002081 int baseInstance,
2082 GrPrimitiveRestart enablePrimitiveRestart) {
2083 SkASSERT((enablePrimitiveRestart == GrPrimitiveRestart::kNo) || indexBuffer);
Chris Dalton27059d32018-01-23 14:06:50 -07002084
cdaltone2e71c22016-04-07 18:13:29 -07002085 GrGLAttribArrayState* attribState;
Chris Daltonff926502017-05-03 14:36:54 -04002086 if (indexBuffer) {
Chris Dalton1d616352017-05-31 12:51:23 -06002087 SkASSERT(indexBuffer && !indexBuffer->isMapped());
Chris Daltonff926502017-05-03 14:36:54 -04002088 attribState = fHWVertexArrayState.bindInternalVertexArray(this, indexBuffer);
cdaltone2e71c22016-04-07 18:13:29 -07002089 } else {
2090 attribState = fHWVertexArrayState.bindInternalVertexArray(this);
bsalomonbc3d0de2014-12-15 13:45:03 -08002091 }
bsalomonbc3d0de2014-12-15 13:45:03 -08002092
Brian Salomon92be2f72018-06-19 14:33:47 -04002093 int numAttribs = fHWProgram->numVertexAttributes() + fHWProgram->numInstanceAttributes();
2094 attribState->enableVertexArrays(this, numAttribs, enablePrimitiveRestart);
Chris Dalton8e45b4f2017-05-05 14:00:56 -04002095
Brian Salomon802cb312018-06-08 18:05:20 -04002096 if (int vertexStride = fHWProgram->vertexStride()) {
Chris Dalton1d616352017-05-31 12:51:23 -06002097 SkASSERT(vertexBuffer && !vertexBuffer->isMapped());
Jim Van Verth275cffb2018-11-13 12:13:34 -05002098 size_t bufferOffset = vertexBuffer->baseOffset();
2099 bufferOffset += baseVertex * static_cast<size_t>(vertexStride);
Brian Salomon92be2f72018-06-19 14:33:47 -04002100 for (int i = 0; i < fHWProgram->numVertexAttributes(); ++i) {
2101 const auto& attrib = fHWProgram->vertexAttribute(i);
2102 static constexpr int kDivisor = 0;
Brian Osman4a3f5c82018-09-18 16:16:38 -04002103 attribState->set(this, attrib.fLocation, vertexBuffer, attrib.fCPUType, attrib.fGPUType,
2104 vertexStride, bufferOffset + attrib.fOffset, kDivisor);
Brian Salomon92be2f72018-06-19 14:33:47 -04002105 }
Chris Dalton1d616352017-05-31 12:51:23 -06002106 }
Brian Salomon802cb312018-06-08 18:05:20 -04002107 if (int instanceStride = fHWProgram->instanceStride()) {
Chris Dalton1d616352017-05-31 12:51:23 -06002108 SkASSERT(instanceBuffer && !instanceBuffer->isMapped());
Jim Van Verth275cffb2018-11-13 12:13:34 -05002109 size_t bufferOffset = instanceBuffer->baseOffset();
2110 bufferOffset += baseInstance * static_cast<size_t>(instanceStride);
Brian Salomon92be2f72018-06-19 14:33:47 -04002111 int attribIdx = fHWProgram->numVertexAttributes();
2112 for (int i = 0; i < fHWProgram->numInstanceAttributes(); ++i, ++attribIdx) {
2113 const auto& attrib = fHWProgram->instanceAttribute(i);
2114 static constexpr int kDivisor = 1;
Brian Osman4a3f5c82018-09-18 16:16:38 -04002115 attribState->set(this, attrib.fLocation, instanceBuffer, attrib.fCPUType,
2116 attrib.fGPUType, instanceStride, bufferOffset + attrib.fOffset,
2117 kDivisor);
Brian Salomon92be2f72018-06-19 14:33:47 -04002118 }
bsalomonbc3d0de2014-12-15 13:45:03 -08002119 }
2120}
2121
csmartdalton485a1202016-07-13 10:16:32 -07002122GrGLenum GrGLGpu::bindBuffer(GrBufferType type, const GrBuffer* buffer) {
joshualitt93316b92015-10-23 09:08:08 -07002123 this->handleDirtyContext();
cdaltondeacc972016-04-06 14:26:33 -07002124
cdaltone2e71c22016-04-07 18:13:29 -07002125 // Index buffer state is tied to the vertex array.
2126 if (kIndex_GrBufferType == type) {
2127 this->bindVertexArray(0);
cdaltondeacc972016-04-06 14:26:33 -07002128 }
cdaltone2e71c22016-04-07 18:13:29 -07002129
2130 SkASSERT(type >= 0 && type <= kLast_GrBufferType);
2131 auto& bufferState = fHWBufferState[type];
2132
robertphillips8abb3702016-08-31 14:04:06 -07002133 if (buffer->uniqueID() != bufferState.fBoundBufferUniqueID) {
csmartdalton485a1202016-07-13 10:16:32 -07002134 if (buffer->isCPUBacked()) {
2135 if (!bufferState.fBufferZeroKnownBound) {
2136 GL_CALL(BindBuffer(bufferState.fGLTarget, 0));
2137 }
2138 } else {
2139 const GrGLBuffer* glBuffer = static_cast<const GrGLBuffer*>(buffer);
2140 GL_CALL(BindBuffer(bufferState.fGLTarget, glBuffer->bufferID()));
cdaltone2e71c22016-04-07 18:13:29 -07002141 }
csmartdalton485a1202016-07-13 10:16:32 -07002142 bufferState.fBufferZeroKnownBound = buffer->isCPUBacked();
robertphillips8abb3702016-08-31 14:04:06 -07002143 bufferState.fBoundBufferUniqueID = buffer->uniqueID();
cdaltone2e71c22016-04-07 18:13:29 -07002144 }
2145
2146 return bufferState.fGLTarget;
joshualitt93316b92015-10-23 09:08:08 -07002147}
Brian Salomond818ebf2018-07-02 14:08:49 +00002148void GrGLGpu::disableScissor() {
2149 if (kNo_TriState != fHWScissorSettings.fEnabled) {
2150 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
2151 fHWScissorSettings.fEnabled = kNo_TriState;
2152 return;
2153 }
2154}
2155
Brian Osman9a9baae2018-11-05 15:06:26 -05002156void GrGLGpu::clear(const GrFixedClip& clip, const SkPMColor4f& color,
Robert Phillips19e51dc2017-08-09 09:30:51 -04002157 GrRenderTarget* target, GrSurfaceOrigin origin) {
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00002158 // parent class should never let us get here with no RT
bsalomon49f085d2014-09-05 13:34:00 -07002159 SkASSERT(target);
Michael Ludwigc39d0c82019-01-15 10:03:43 -05002160 SkASSERT(!this->caps()->performColorClearsAsDraws());
2161 SkASSERT(!clip.scissorEnabled() || !this->caps()->performPartialClearsAsDraws());
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00002162
Brian Salomon43f8bf02017-10-18 08:33:29 -04002163 this->handleDirtyContext();
bsalomon@google.com74b98712011-11-11 19:46:16 +00002164
Brian Salomon43f8bf02017-10-18 08:33:29 -04002165 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
2166
Brian Salomond818ebf2018-07-02 14:08:49 +00002167 if (clip.scissorEnabled()) {
Chris Daltonc8ece3d2018-07-30 15:03:45 -06002168 this->flushRenderTarget(glRT, origin, clip.scissorRect());
Brian Salomon1fabd512018-02-09 09:54:25 -05002169 } else {
Chris Daltonc8ece3d2018-07-30 15:03:45 -06002170 this->flushRenderTarget(glRT);
Brian Salomon1fabd512018-02-09 09:54:25 -05002171 }
Brian Salomond818ebf2018-07-02 14:08:49 +00002172 this->flushScissor(clip.scissorState(), glRT->getViewport(), origin);
Brian Salomon43f8bf02017-10-18 08:33:29 -04002173 this->flushWindowRectangles(clip.windowRectsState(), glRT, origin);
2174
bsalomon@google.com74b98712011-11-11 19:46:16 +00002175 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002176 fHWWriteToColor = kYes_TriState;
Brian Salomon028a9a52017-05-11 11:39:08 -04002177
Brian Osman9a9baae2018-11-05 15:06:26 -05002178 GrGLfloat r = color.fR, g = color.fG, b = color.fB, a = color.fA;
2179
Eric Karlaeaf22b2017-05-18 15:08:09 -07002180 if (this->glCaps().clearToBoundaryValuesIsBroken() &&
2181 (1 == r || 0 == r) && (1 == g || 0 == g) && (1 == b || 0 == b) && (1 == a || 0 == a)) {
Eric Karlaeaf22b2017-05-18 15:08:09 -07002182 static const GrGLfloat safeAlpha1 = nextafter(1.f, 2.f);
2183 static const GrGLfloat safeAlpha0 = nextafter(0.f, -1.f);
Eric Karlaeaf22b2017-05-18 15:08:09 -07002184 a = (1 == a) ? safeAlpha1 : safeAlpha0;
Brian Salomon028a9a52017-05-11 11:39:08 -04002185 }
bsalomon@google.com74b98712011-11-11 19:46:16 +00002186 GL_CALL(ClearColor(r, g, b, a));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002187 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00002188}
2189
Robert Phillips95214472017-08-08 18:00:03 -04002190void GrGLGpu::clearStencil(GrRenderTarget* target, int clearValue) {
Michael Ludwigc39d0c82019-01-15 10:03:43 -05002191 SkASSERT(!this->caps()->performStencilClearsAsDraws());
2192
Robert Phillips95214472017-08-08 18:00:03 -04002193 if (!target) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00002194 return;
2195 }
Robert Phillipscb2e2352017-08-30 16:44:40 -04002196
2197 GrStencilAttachment* sb = target->renderTargetPriv().getStencilAttachment();
2198 // this should only be called internally when we know we have a
2199 // stencil buffer.
2200 SkASSERT(sb);
2201
bsalomonb0bd4f62014-09-03 07:19:50 -07002202 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
Brian Salomon1fabd512018-02-09 09:54:25 -05002203 this->flushRenderTargetNoColorWrites(glRT);
bsalomon@google.com8295dc12011-05-02 12:53:34 +00002204
Brian Salomond818ebf2018-07-02 14:08:49 +00002205 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07002206 this->disableWindowRectangles();
robertphillips@google.com730ebe52012-04-16 16:33:13 +00002207
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002208 GL_CALL(StencilMask(0xffffffff));
Robert Phillips95214472017-08-08 18:00:03 -04002209 GL_CALL(ClearStencil(clearValue));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002210 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00002211 fHWStencilSettings.invalidate();
Robert Phillipscb2e2352017-08-30 16:44:40 -04002212 if (!clearValue) {
2213 sb->cleared();
2214 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002215}
2216
csmartdalton29df7602016-08-31 11:55:52 -07002217void GrGLGpu::clearStencilClip(const GrFixedClip& clip,
2218 bool insideStencilMask,
Robert Phillips19e51dc2017-08-09 09:30:51 -04002219 GrRenderTarget* target, GrSurfaceOrigin origin) {
bsalomon49f085d2014-09-05 13:34:00 -07002220 SkASSERT(target);
Michael Ludwigc39d0c82019-01-15 10:03:43 -05002221 SkASSERT(!this->caps()->performStencilClearsAsDraws());
egdaniel9cb63402016-06-23 08:37:05 -07002222 this->handleDirtyContext();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00002223
egdaniel8dc7c3a2015-04-16 11:22:42 -07002224 GrStencilAttachment* sb = target->renderTargetPriv().getStencilAttachment();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00002225 // this should only be called internally when we know we have a
2226 // stencil buffer.
bsalomon6bc1b5f2015-02-23 09:06:38 -08002227 SkASSERT(sb);
2228 GrGLint stencilBitCount = sb->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002229#if 0
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002230 SkASSERT(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00002231 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002232#else
2233 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00002234 // ANGLE a partial stencil mask will cause clears to be
Robert Phillipsf2361d22016-10-25 14:20:06 -04002235 // turned into draws. Our contract on GrOpList says that
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002236 // changing the clip between stencil passes may or may not
2237 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00002238 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002239#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002240 GrGLint value;
csmartdalton29df7602016-08-31 11:55:52 -07002241 if (insideStencilMask) {
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002242 value = (1 << (stencilBitCount - 1));
2243 } else {
2244 value = 0;
2245 }
bsalomonb0bd4f62014-09-03 07:19:50 -07002246 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
Brian Salomon1fabd512018-02-09 09:54:25 -05002247 this->flushRenderTargetNoColorWrites(glRT);
bsalomon@google.coma3201942012-06-21 19:58:20 +00002248
Brian Salomond818ebf2018-07-02 14:08:49 +00002249 this->flushScissor(clip.scissorState(), glRT->getViewport(), origin);
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002250 this->flushWindowRectangles(clip.windowRectsState(), glRT, origin);
bsalomon@google.coma3201942012-06-21 19:58:20 +00002251
caryclark@google.comcf6285b2012-06-06 12:09:01 +00002252 GL_CALL(StencilMask((uint32_t) clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002253 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002254 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00002255 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00002256}
2257
bsalomon1aa20292016-01-22 08:16:09 -08002258bool GrGLGpu::readPixelsSupported(GrRenderTarget* target, GrPixelConfig readConfig) {
Brian Salomon625cd9e2016-12-15 09:35:19 -05002259#ifdef SK_BUILD_FOR_MAC
2260 // Chromium may ask us to read back from locked IOSurfaces. Calling the command buffer's
2261 // glGetIntegerv() with GL_IMPLEMENTATION_COLOR_READ_FORMAT/_TYPE causes the command buffer
2262 // to make a call to check the framebuffer status which can hang the driver. So in Mac Chromium
2263 // we always use a temporary surface to test for read pixels support.
2264 // https://www.crbug.com/662802
2265 if (this->glContext().driver() == kChromium_GrGLDriver) {
2266 return this->readPixelsSupported(target->config(), readConfig);
2267 }
2268#endif
bsalomon1aa20292016-01-22 08:16:09 -08002269 auto bindRenderTarget = [this, target]() -> bool {
Brian Salomon1fabd512018-02-09 09:54:25 -05002270 this->flushRenderTargetNoColorWrites(static_cast<GrGLRenderTarget*>(target));
bsalomon1aa20292016-01-22 08:16:09 -08002271 return true;
2272 };
bsalomon2c3db322016-11-08 13:26:24 -08002273 auto unbindRenderTarget = []{};
bsalomon1aa20292016-01-22 08:16:09 -08002274 auto getIntegerv = [this](GrGLenum query, GrGLint* value) {
2275 GR_GL_GetIntegerv(this->glInterface(), query, value);
2276 };
2277 GrPixelConfig rtConfig = target->config();
bsalomon2c3db322016-11-08 13:26:24 -08002278 return this->glCaps().readPixelsSupported(rtConfig, readConfig, getIntegerv, bindRenderTarget,
2279 unbindRenderTarget);
bsalomon1aa20292016-01-22 08:16:09 -08002280}
2281
2282bool GrGLGpu::readPixelsSupported(GrPixelConfig rtConfig, GrPixelConfig readConfig) {
bsalomon2c3db322016-11-08 13:26:24 -08002283 sk_sp<GrTexture> temp;
2284 auto bindRenderTarget = [this, rtConfig, &temp]() -> bool {
Robert Phillips16d8ec62017-07-27 16:16:25 -04002285 GrSurfaceDesc desc;
bsalomon1aa20292016-01-22 08:16:09 -08002286 desc.fConfig = rtConfig;
2287 desc.fWidth = desc.fHeight = 16;
Brian Salomonbdecacf2018-02-02 20:32:49 -05002288 if (this->glCaps().isConfigRenderable(rtConfig)) {
bsalomon2c3db322016-11-08 13:26:24 -08002289 desc.fFlags = kRenderTarget_GrSurfaceFlag;
Robert Phillips67d52cf2017-06-05 13:38:13 -04002290 temp = this->createTexture(desc, SkBudgeted::kNo);
bsalomon2c3db322016-11-08 13:26:24 -08002291 if (!temp) {
2292 return false;
2293 }
2294 GrGLRenderTarget* glrt = static_cast<GrGLRenderTarget*>(temp->asRenderTarget());
Brian Salomon1fabd512018-02-09 09:54:25 -05002295 this->flushRenderTargetNoColorWrites(glrt);
bsalomon2c3db322016-11-08 13:26:24 -08002296 return true;
2297 } else if (this->glCaps().canConfigBeFBOColorAttachment(rtConfig)) {
Robert Phillips67d52cf2017-06-05 13:38:13 -04002298 temp = this->createTexture(desc, SkBudgeted::kNo);
bsalomon2c3db322016-11-08 13:26:24 -08002299 if (!temp) {
2300 return false;
2301 }
2302 GrGLIRect vp;
2303 this->bindSurfaceFBOForPixelOps(temp.get(), GR_GL_FRAMEBUFFER, &vp, kDst_TempFBOTarget);
Robert Phillips294870f2016-11-11 12:38:40 -05002304 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon2c3db322016-11-08 13:26:24 -08002305 return true;
bsalomon1aa20292016-01-22 08:16:09 -08002306 }
bsalomon2c3db322016-11-08 13:26:24 -08002307 return false;
2308 };
2309 auto unbindRenderTarget = [this, &temp]() {
2310 this->unbindTextureFBOForPixelOps(GR_GL_FRAMEBUFFER, temp.get());
bsalomon1aa20292016-01-22 08:16:09 -08002311 };
2312 auto getIntegerv = [this](GrGLenum query, GrGLint* value) {
2313 GR_GL_GetIntegerv(this->glInterface(), query, value);
2314 };
bsalomon2c3db322016-11-08 13:26:24 -08002315 return this->glCaps().readPixelsSupported(rtConfig, readConfig, getIntegerv, bindRenderTarget,
2316 unbindRenderTarget);
bsalomon1aa20292016-01-22 08:16:09 -08002317}
2318
2319bool GrGLGpu::readPixelsSupported(GrSurface* surfaceForConfig, GrPixelConfig readConfig) {
2320 if (GrRenderTarget* rt = surfaceForConfig->asRenderTarget()) {
2321 return this->readPixelsSupported(rt, readConfig);
2322 } else {
2323 GrPixelConfig config = surfaceForConfig->config();
2324 return this->readPixelsSupported(config, readConfig);
2325 }
2326}
2327
Brian Salomona6948702018-06-01 15:33:20 -04002328bool GrGLGpu::onReadPixels(GrSurface* surface, int left, int top, int width, int height,
2329 GrColorType dstColorType, void* buffer, size_t rowBytes) {
bsalomon6cb3cbe2015-07-30 07:34:27 -07002330 SkASSERT(surface);
bsalomon39826022015-07-23 08:07:21 -07002331
bsalomone9573312016-01-25 14:33:25 -08002332 GrGLRenderTarget* renderTarget = static_cast<GrGLRenderTarget*>(surface->asRenderTarget());
Brian Salomon71d9d842016-11-03 13:42:00 -04002333 if (!renderTarget && !this->glCaps().canConfigBeFBOColorAttachment(surface->config())) {
bsalomon6cb3cbe2015-07-30 07:34:27 -07002334 return false;
2335 }
2336
Brian Salomonc320b152018-02-20 14:05:36 -05002337 // TODO: Avoid this conversion by making GrGLCaps work with color types.
2338 auto dstAsConfig = GrColorTypeToPixelConfig(dstColorType, GrSRGBEncoded::kNo);
2339
Brian Salomonc320b152018-02-20 14:05:36 -05002340 if (!this->readPixelsSupported(surface, dstAsConfig)) {
bsalomon16921ec2015-07-30 15:34:56 -07002341 return false;
2342 }
2343
bsalomon76148af2016-01-12 11:13:47 -08002344 GrGLenum externalFormat;
2345 GrGLenum externalType;
Brian Salomonc320b152018-02-20 14:05:36 -05002346 if (!this->glCaps().getReadPixelsFormat(surface->config(), dstAsConfig, &externalFormat,
bsalomon76148af2016-01-12 11:13:47 -08002347 &externalType)) {
2348 return false;
2349 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00002350
Brian Salomon71d9d842016-11-03 13:42:00 -04002351 GrGLIRect glvp;
2352 if (renderTarget) {
2353 // resolve the render target if necessary
2354 switch (renderTarget->getResolveType()) {
2355 case GrGLRenderTarget::kCantResolve_ResolveType:
2356 return false;
2357 case GrGLRenderTarget::kAutoResolves_ResolveType:
Brian Salomon1fabd512018-02-09 09:54:25 -05002358 this->flushRenderTargetNoColorWrites(renderTarget);
Brian Salomon71d9d842016-11-03 13:42:00 -04002359 break;
2360 case GrGLRenderTarget::kCanResolve_ResolveType:
Brian Salomon1fabd512018-02-09 09:54:25 -05002361 this->onResolveRenderTarget(renderTarget);
Brian Salomon71d9d842016-11-03 13:42:00 -04002362 // we don't track the state of the READ FBO ID.
Adrienne Walker4ee88512018-05-17 11:37:14 -07002363 this->bindFramebuffer(GR_GL_READ_FRAMEBUFFER, renderTarget->textureFBOID());
Brian Salomon71d9d842016-11-03 13:42:00 -04002364 break;
2365 default:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -04002366 SK_ABORT("Unknown resolve type");
Brian Salomon71d9d842016-11-03 13:42:00 -04002367 }
2368 glvp = renderTarget->getViewport();
2369 } else {
2370 // Use a temporary FBO.
2371 this->bindSurfaceFBOForPixelOps(surface, GR_GL_FRAMEBUFFER, &glvp, kSrc_TempFBOTarget);
Robert Phillips294870f2016-11-11 12:38:40 -05002372 fHWBoundRenderTargetUniqueID.makeInvalid();
reed@google.comac10a2d2010-12-22 21:39:39 +00002373 }
2374
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00002375 // the read rect is viewport-relative
2376 GrGLIRect readRect;
Brian Salomona6948702018-06-01 15:33:20 -04002377 readRect.setRelativeTo(glvp, left, top, width, height, kTopLeft_GrSurfaceOrigin);
rmistry@google.comfbfcd562012-08-23 18:09:54 +00002378
Brian Salomonc320b152018-02-20 14:05:36 -05002379 int bytesPerPixel = GrBytesPerPixel(dstAsConfig);
bsalomon9d02b262016-02-01 12:49:30 -08002380 size_t tightRowBytes = bytesPerPixel * width;
egdaniel6d901da2015-07-30 12:02:15 -07002381
bsalomon@google.comc6980972011-11-02 19:57:21 +00002382 size_t readDstRowBytes = tightRowBytes;
2383 void* readDst = buffer;
rmistry@google.comfbfcd562012-08-23 18:09:54 +00002384
Brian Salomona6948702018-06-01 15:33:20 -04002385 // determine if GL can read using the passed rowBytes or if we need a scratch buffer.
joshualitt29f86792015-05-29 08:06:48 -07002386 SkAutoSMalloc<32 * sizeof(GrColor)> scratch;
bsalomon@google.comc6980972011-11-02 19:57:21 +00002387 if (rowBytes != tightRowBytes) {
bsalomon9d02b262016-02-01 12:49:30 -08002388 if (this->glCaps().packRowLengthSupport() && !(rowBytes % bytesPerPixel)) {
skia.committer@gmail.com4677acc2013-10-17 07:02:33 +00002389 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH,
bsalomon9d02b262016-02-01 12:49:30 -08002390 static_cast<GrGLint>(rowBytes / bytesPerPixel)));
bsalomon@google.comc6980972011-11-02 19:57:21 +00002391 readDstRowBytes = rowBytes;
2392 } else {
2393 scratch.reset(tightRowBytes * height);
2394 readDst = scratch.get();
2395 }
2396 }
Brian Salomonc320b152018-02-20 14:05:36 -05002397 GL_CALL(PixelStorei(GR_GL_PACK_ALIGNMENT, config_alignment(dstAsConfig)));
bsalomonf46a1242015-12-15 12:37:38 -08002398
Brian Osman1348ed02018-09-12 09:08:39 -04002399 bool reattachStencil = false;
2400 if (this->glCaps().detachStencilFromMSAABuffersBeforeReadPixels() &&
2401 renderTarget &&
2402 renderTarget->renderTargetPriv().getStencilAttachment() &&
2403 renderTarget->numColorSamples() > 1) {
2404 // Fix Adreno devices that won't read from MSAA framebuffers with stencil attached
2405 reattachStencil = true;
2406 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT,
2407 GR_GL_RENDERBUFFER, 0));
2408 }
2409
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002410 GL_CALL(ReadPixels(readRect.fLeft, readRect.fBottom,
2411 readRect.fWidth, readRect.fHeight,
bsalomon76148af2016-01-12 11:13:47 -08002412 externalFormat, externalType, readDst));
Brian Osman1348ed02018-09-12 09:08:39 -04002413
2414 if (reattachStencil) {
2415 GrGLStencilAttachment* stencilAttachment = static_cast<GrGLStencilAttachment*>(
2416 renderTarget->renderTargetPriv().getStencilAttachment());
2417 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT,
2418 GR_GL_RENDERBUFFER, stencilAttachment->renderbufferID()));
2419 }
2420
bsalomon@google.comc6980972011-11-02 19:57:21 +00002421 if (readDstRowBytes != tightRowBytes) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002422 SkASSERT(this->glCaps().packRowLengthSupport());
bsalomon@google.comc6980972011-11-02 19:57:21 +00002423 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
2424 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002425
Brian Salomona6948702018-06-01 15:33:20 -04002426 if (readDst != buffer) {
bsalomon9d02b262016-02-01 12:49:30 -08002427 SkASSERT(readDst != buffer);
2428 SkASSERT(rowBytes != tightRowBytes);
bsalomon@google.comc6980972011-11-02 19:57:21 +00002429 const char* src = reinterpret_cast<const char*>(readDst);
bsalomon@google.comc4364992011-11-07 15:54:49 +00002430 char* dst = reinterpret_cast<char*>(buffer);
Brian Salomona6948702018-06-01 15:33:20 -04002431 SkRectMemcpy(dst, rowBytes, src, readDstRowBytes, tightRowBytes, height);
reed@google.comac10a2d2010-12-22 21:39:39 +00002432 }
Brian Salomon71d9d842016-11-03 13:42:00 -04002433 if (!renderTarget) {
2434 this->unbindTextureFBOForPixelOps(GR_GL_FRAMEBUFFER, surface);
2435 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002436 return true;
2437}
2438
Robert Phillips5b5d84c2018-08-09 15:12:18 -04002439GrGpuRTCommandBuffer* GrGLGpu::getCommandBuffer(
Ethan Nicholas56d19a52018-10-15 11:26:20 -04002440 GrRenderTarget* rt, GrSurfaceOrigin origin, const SkRect& bounds,
Robert Phillips6b47c7d2017-08-29 07:24:09 -04002441 const GrGpuRTCommandBuffer::LoadAndStoreInfo& colorInfo,
Greg Daniel500d58b2017-08-24 15:59:33 -04002442 const GrGpuRTCommandBuffer::StencilLoadAndStoreInfo& stencilInfo) {
Robert Phillips5b5d84c2018-08-09 15:12:18 -04002443 if (!fCachedRTCommandBuffer) {
2444 fCachedRTCommandBuffer.reset(new GrGLGpuRTCommandBuffer(this));
2445 }
2446
2447 fCachedRTCommandBuffer->set(rt, origin, colorInfo, stencilInfo);
2448 return fCachedRTCommandBuffer.get();
Greg Daniel500d58b2017-08-24 15:59:33 -04002449}
2450
Robert Phillips5b5d84c2018-08-09 15:12:18 -04002451GrGpuTextureCommandBuffer* GrGLGpu::getCommandBuffer(GrTexture* texture, GrSurfaceOrigin origin) {
2452 if (!fCachedTexCommandBuffer) {
2453 fCachedTexCommandBuffer.reset(new GrGLGpuTextureCommandBuffer(this));
2454 }
2455
2456 fCachedTexCommandBuffer->set(texture, origin);
2457 return fCachedTexCommandBuffer.get();
egdaniel066df7c2016-06-08 14:02:27 -07002458}
2459
Brian Salomon1fabd512018-02-09 09:54:25 -05002460void GrGLGpu::flushRenderTarget(GrGLRenderTarget* target, GrSurfaceOrigin origin,
Chris Daltonc8ece3d2018-07-30 15:03:45 -06002461 const SkIRect& bounds) {
Brian Osman9aa30c62018-07-02 15:21:46 -04002462 this->flushRenderTargetNoColorWrites(target);
Chris Daltonc8ece3d2018-07-30 15:03:45 -06002463 this->didWriteToSurface(target, origin, &bounds);
2464}
bsalomon6ba6fa12015-03-04 11:57:37 -08002465
Chris Daltonc8ece3d2018-07-30 15:03:45 -06002466void GrGLGpu::flushRenderTarget(GrGLRenderTarget* target) {
2467 this->flushRenderTargetNoColorWrites(target);
2468 this->didWriteToSurface(target, kTopLeft_GrSurfaceOrigin, nullptr);
Brian Salomon1fabd512018-02-09 09:54:25 -05002469}
2470
Brian Osman9aa30c62018-07-02 15:21:46 -04002471void GrGLGpu::flushRenderTargetNoColorWrites(GrGLRenderTarget* target) {
Brian Salomon1fabd512018-02-09 09:54:25 -05002472 SkASSERT(target);
Robert Phillips294870f2016-11-11 12:38:40 -05002473 GrGpuResource::UniqueID rtID = target->uniqueID();
egdanield803f272015-03-18 13:01:52 -07002474 if (fHWBoundRenderTargetUniqueID != rtID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -07002475 this->bindFramebuffer(GR_GL_FRAMEBUFFER, target->renderFBOID());
egdanield803f272015-03-18 13:01:52 -07002476#ifdef SK_DEBUG
2477 // don't do this check in Chromium -- this is causing
2478 // lots of repeated command buffer flushes when the compositor is
2479 // rendering with Ganesh, which is really slow; even too slow for
2480 // Debug mode.
cdalton1acea862015-06-02 13:05:52 -07002481 if (kChromium_GrGLDriver != this->glContext().driver()) {
egdanield803f272015-03-18 13:01:52 -07002482 GrGLenum status;
2483 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
2484 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
2485 SkDebugf("GrGLGpu::flushRenderTarget glCheckFramebufferStatus %x\n", status);
2486 }
bsalomon160f24c2015-03-17 15:55:42 -07002487 }
egdanield803f272015-03-18 13:01:52 -07002488#endif
2489 fHWBoundRenderTargetUniqueID = rtID;
bsalomon083617b2016-02-12 12:10:14 -08002490 this->flushViewport(target->getViewport());
brianosman64d094d2016-03-25 06:01:59 -07002491 }
2492
brianosman35b784d2016-05-05 11:52:53 -07002493 if (this->glCaps().srgbWriteControl()) {
Brian Osman9aa30c62018-07-02 15:21:46 -04002494 this->flushFramebufferSRGB(GrPixelConfigIsSRGB(target->config()));
bsalomon5cd020f2015-03-17 12:46:56 -07002495 }
bsalomon083617b2016-02-12 12:10:14 -08002496}
bsalomona9909122016-01-23 10:41:40 -08002497
brianosman33f6b3f2016-06-02 05:49:21 -07002498void GrGLGpu::flushFramebufferSRGB(bool enable) {
2499 if (enable && kYes_TriState != fHWSRGBFramebuffer) {
2500 GL_CALL(Enable(GR_GL_FRAMEBUFFER_SRGB));
2501 fHWSRGBFramebuffer = kYes_TriState;
2502 } else if (!enable && kNo_TriState != fHWSRGBFramebuffer) {
2503 GL_CALL(Disable(GR_GL_FRAMEBUFFER_SRGB));
2504 fHWSRGBFramebuffer = kNo_TriState;
2505 }
2506}
2507
bsalomon083617b2016-02-12 12:10:14 -08002508void GrGLGpu::flushViewport(const GrGLIRect& viewport) {
2509 if (fHWViewport != viewport) {
2510 viewport.pushToGLViewport(this->glInterface());
2511 fHWViewport = viewport;
2512 }
2513}
2514
bsalomon@google.comd302f142011-03-03 13:54:13 +00002515#define SWAP_PER_DRAW 0
2516
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00002517#if SWAP_PER_DRAW
commit-bot@chromium.org43823302013-09-25 20:57:51 +00002518 #if defined(SK_BUILD_FOR_MAC)
bsalomon@google.comd302f142011-03-03 13:54:13 +00002519 #include <AGL/agl.h>
Mike Klein8f11d4d2018-01-24 12:42:55 -05002520 #elif defined(SK_BUILD_FOR_WIN)
bsalomon@google.comce7357d2012-06-25 17:49:25 +00002521 #include <gl/GL.h>
bsalomon@google.comd302f142011-03-03 13:54:13 +00002522 void SwapBuf() {
2523 DWORD procID = GetCurrentProcessId();
2524 HWND hwnd = GetTopWindow(GetDesktopWindow());
2525 while(hwnd) {
2526 DWORD wndProcID = 0;
2527 GetWindowThreadProcessId(hwnd, &wndProcID);
2528 if(wndProcID == procID) {
2529 SwapBuffers(GetDC(hwnd));
2530 }
2531 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
2532 }
2533 }
2534 #endif
2535#endif
2536
Brian Salomonff168d92018-06-23 15:17:27 -04002537void GrGLGpu::draw(const GrPrimitiveProcessor& primProc,
2538 const GrPipeline& pipeline,
Brian Salomon49348902018-06-26 09:12:38 -04002539 const GrPipeline::FixedDynamicState* fixedDynamicState,
2540 const GrPipeline::DynamicStateArrays* dynamicStateArrays,
bsalomon2eda5b32016-09-21 10:53:24 -07002541 const GrMesh meshes[],
egdaniel9cb63402016-06-23 08:37:05 -07002542 int meshCount) {
2543 this->handleDirtyContext();
2544
bsalomon2eda5b32016-09-21 10:53:24 -07002545 bool hasPoints = false;
2546 for (int i = 0; i < meshCount; ++i) {
Chris Dalton3809bab2017-06-13 10:55:06 -06002547 if (meshes[i].primitiveType() == GrPrimitiveType::kPoints) {
bsalomon2eda5b32016-09-21 10:53:24 -07002548 hasPoints = true;
2549 break;
2550 }
2551 }
Brian Salomonf7232642018-09-19 08:58:08 -04002552 if (!this->flushGLState(primProc, pipeline, fixedDynamicState, dynamicStateArrays, meshCount,
2553 hasPoints)) {
bsalomond95263c2014-12-16 13:05:12 -08002554 return;
2555 }
ethannicholas22793252016-01-30 09:59:10 -08002556
Brian Salomonf7232642018-09-19 08:58:08 -04002557 bool dynamicScissor = false;
2558 bool dynamicPrimProcTextures = false;
2559 if (dynamicStateArrays) {
2560 dynamicScissor = pipeline.isScissorEnabled() && dynamicStateArrays->fScissorRects;
2561 dynamicPrimProcTextures = dynamicStateArrays->fPrimitiveProcessorTextures;
2562 }
2563 for (int m = 0; m < meshCount; ++m) {
egdaniel0e1853c2016-03-17 11:35:45 -07002564 if (GrXferBarrierType barrierType = pipeline.xferBarrierType(*this->caps())) {
Robert Phillips2890fbf2017-07-26 15:48:41 -04002565 this->xferBarrier(pipeline.renderTarget(), barrierType);
egdaniel0e1853c2016-03-17 11:35:45 -07002566 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002567
Brian Salomon49348902018-06-26 09:12:38 -04002568 if (dynamicScissor) {
2569 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(pipeline.renderTarget());
Brian Salomonf7232642018-09-19 08:58:08 -04002570 this->flushScissor(GrScissorState(dynamicStateArrays->fScissorRects[m]),
Brian Salomond818ebf2018-07-02 14:08:49 +00002571 glRT->getViewport(), pipeline.proxy()->origin());
Chris Dalton46983b72017-06-06 12:27:16 -06002572 }
Brian Salomonf7232642018-09-19 08:58:08 -04002573 if (dynamicPrimProcTextures) {
2574 auto texProxyArray = dynamicStateArrays->fPrimitiveProcessorTextures +
2575 m * primProc.numTextureSamplers();
2576 fHWProgram->updatePrimitiveProcessorTextureBindings(primProc, texProxyArray);
2577 }
Brian Salomon6d9c88b2017-06-12 10:24:42 -04002578 if (this->glCaps().requiresCullFaceEnableDisableWhenDrawingLinesAfterNonLines() &&
Brian Salomonf7232642018-09-19 08:58:08 -04002579 GrIsPrimTypeLines(meshes[m].primitiveType()) &&
Brian Salomon6d9c88b2017-06-12 10:24:42 -04002580 !GrIsPrimTypeLines(fLastPrimitiveType)) {
2581 GL_CALL(Enable(GR_GL_CULL_FACE));
2582 GL_CALL(Disable(GR_GL_CULL_FACE));
2583 }
Brian Salomonf7232642018-09-19 08:58:08 -04002584 meshes[m].sendToGpu(this);
2585 fLastPrimitiveType = meshes[m].primitiveType();
bsalomon@google.com74749cd2013-01-30 16:12:41 +00002586 }
ethannicholas22793252016-01-30 09:59:10 -08002587
bsalomon@google.comd302f142011-03-03 13:54:13 +00002588#if SWAP_PER_DRAW
2589 glFlush();
commit-bot@chromium.org43823302013-09-25 20:57:51 +00002590 #if defined(SK_BUILD_FOR_MAC)
bsalomon@google.comd302f142011-03-03 13:54:13 +00002591 aglSwapBuffers(aglGetCurrentContext());
2592 int set_a_break_pt_here = 9;
2593 aglSwapBuffers(aglGetCurrentContext());
Mike Klein8f11d4d2018-01-24 12:42:55 -05002594 #elif defined(SK_BUILD_FOR_WIN)
bsalomon@google.comd302f142011-03-03 13:54:13 +00002595 SwapBuf();
2596 int set_a_break_pt_here = 9;
2597 SwapBuf();
2598 #endif
2599#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00002600}
2601
Chris Dalton3809bab2017-06-13 10:55:06 -06002602static GrGLenum gr_primitive_type_to_gl_mode(GrPrimitiveType primitiveType) {
2603 switch (primitiveType) {
2604 case GrPrimitiveType::kTriangles:
2605 return GR_GL_TRIANGLES;
2606 case GrPrimitiveType::kTriangleStrip:
2607 return GR_GL_TRIANGLE_STRIP;
Chris Dalton3809bab2017-06-13 10:55:06 -06002608 case GrPrimitiveType::kPoints:
2609 return GR_GL_POINTS;
2610 case GrPrimitiveType::kLines:
2611 return GR_GL_LINES;
2612 case GrPrimitiveType::kLineStrip:
2613 return GR_GL_LINE_STRIP;
2614 case GrPrimitiveType::kLinesAdjacency:
2615 return GR_GL_LINES_ADJACENCY;
2616 }
Ben Wagnerb4aab9a2017-08-16 10:53:04 -04002617 SK_ABORT("invalid GrPrimitiveType");
Chris Dalton3809bab2017-06-13 10:55:06 -06002618 return GR_GL_TRIANGLES;
2619}
2620
Brian Salomon802cb312018-06-08 18:05:20 -04002621void GrGLGpu::sendMeshToGpu(GrPrimitiveType primitiveType, const GrBuffer* vertexBuffer,
2622 int vertexCount, int baseVertex) {
Chris Dalton3809bab2017-06-13 10:55:06 -06002623 const GrGLenum glPrimType = gr_primitive_type_to_gl_mode(primitiveType);
Chris Dalton114a3c02017-05-26 15:17:19 -06002624 if (this->glCaps().drawArraysBaseVertexIsBroken()) {
Brian Salomon802cb312018-06-08 18:05:20 -04002625 this->setupGeometry(nullptr, vertexBuffer, baseVertex, nullptr, 0, GrPrimitiveRestart::kNo);
Chris Dalton114a3c02017-05-26 15:17:19 -06002626 GL_CALL(DrawArrays(glPrimType, 0, vertexCount));
2627 } else {
Brian Salomon802cb312018-06-08 18:05:20 -04002628 this->setupGeometry(nullptr, vertexBuffer, 0, nullptr, 0, GrPrimitiveRestart::kNo);
Chris Dalton114a3c02017-05-26 15:17:19 -06002629 GL_CALL(DrawArrays(glPrimType, baseVertex, vertexCount));
2630 }
2631 fStats.incNumDraws();
2632}
2633
Brian Salomon802cb312018-06-08 18:05:20 -04002634void GrGLGpu::sendIndexedMeshToGpu(GrPrimitiveType primitiveType, const GrBuffer* indexBuffer,
Chris Dalton114a3c02017-05-26 15:17:19 -06002635 int indexCount, int baseIndex, uint16_t minIndexValue,
2636 uint16_t maxIndexValue, const GrBuffer* vertexBuffer,
Brian Salomon802cb312018-06-08 18:05:20 -04002637 int baseVertex, GrPrimitiveRestart enablePrimitiveRestart) {
Chris Dalton3809bab2017-06-13 10:55:06 -06002638 const GrGLenum glPrimType = gr_primitive_type_to_gl_mode(primitiveType);
Chris Dalton114a3c02017-05-26 15:17:19 -06002639 GrGLvoid* const indices = reinterpret_cast<void*>(indexBuffer->baseOffset() +
2640 sizeof(uint16_t) * baseIndex);
2641
Brian Salomon802cb312018-06-08 18:05:20 -04002642 this->setupGeometry(indexBuffer, vertexBuffer, baseVertex, nullptr, 0, enablePrimitiveRestart);
Chris Dalton114a3c02017-05-26 15:17:19 -06002643
2644 if (this->glCaps().drawRangeElementsSupport()) {
2645 GL_CALL(DrawRangeElements(glPrimType, minIndexValue, maxIndexValue, indexCount,
2646 GR_GL_UNSIGNED_SHORT, indices));
2647 } else {
2648 GL_CALL(DrawElements(glPrimType, indexCount, GR_GL_UNSIGNED_SHORT, indices));
2649 }
2650 fStats.incNumDraws();
2651}
2652
Brian Salomon802cb312018-06-08 18:05:20 -04002653void GrGLGpu::sendInstancedMeshToGpu(GrPrimitiveType primitiveType, const GrBuffer* vertexBuffer,
Chris Dalton1d616352017-05-31 12:51:23 -06002654 int vertexCount, int baseVertex,
2655 const GrBuffer* instanceBuffer, int instanceCount,
2656 int baseInstance) {
Chris Daltoncc604e52017-10-06 16:27:32 -06002657 GrGLenum glPrimType = gr_primitive_type_to_gl_mode(primitiveType);
Chris Dalton1b4ad762018-10-04 11:58:09 -06002658 int maxInstances = this->glCaps().maxInstancesPerDrawWithoutCrashing(instanceCount);
Chris Daltoncc604e52017-10-06 16:27:32 -06002659 for (int i = 0; i < instanceCount; i += maxInstances) {
Brian Salomon802cb312018-06-08 18:05:20 -04002660 this->setupGeometry(nullptr, vertexBuffer, 0, instanceBuffer, baseInstance + i,
2661 GrPrimitiveRestart::kNo);
Chris Daltoncc604e52017-10-06 16:27:32 -06002662 GL_CALL(DrawArraysInstanced(glPrimType, baseVertex, vertexCount,
2663 SkTMin(instanceCount - i, maxInstances)));
2664 fStats.incNumDraws();
2665 }
Chris Dalton1d616352017-05-31 12:51:23 -06002666}
2667
Brian Salomon802cb312018-06-08 18:05:20 -04002668void GrGLGpu::sendIndexedInstancedMeshToGpu(GrPrimitiveType primitiveType,
Chris Dalton1d616352017-05-31 12:51:23 -06002669 const GrBuffer* indexBuffer, int indexCount,
2670 int baseIndex, const GrBuffer* vertexBuffer,
2671 int baseVertex, const GrBuffer* instanceBuffer,
Brian Salomon802cb312018-06-08 18:05:20 -04002672 int instanceCount, int baseInstance,
2673 GrPrimitiveRestart enablePrimitiveRestart) {
Chris Dalton3809bab2017-06-13 10:55:06 -06002674 const GrGLenum glPrimType = gr_primitive_type_to_gl_mode(primitiveType);
Chris Dalton1d616352017-05-31 12:51:23 -06002675 GrGLvoid* indices = reinterpret_cast<void*>(indexBuffer->baseOffset() +
2676 sizeof(uint16_t) * baseIndex);
Chris Dalton1b4ad762018-10-04 11:58:09 -06002677 int maxInstances = this->glCaps().maxInstancesPerDrawWithoutCrashing(instanceCount);
2678 for (int i = 0; i < instanceCount; i += maxInstances) {
2679 this->setupGeometry(indexBuffer, vertexBuffer, baseVertex, instanceBuffer, baseInstance + i,
2680 enablePrimitiveRestart);
2681 GL_CALL(DrawElementsInstanced(glPrimType, indexCount, GR_GL_UNSIGNED_SHORT, indices,
2682 SkTMin(instanceCount - i, maxInstances)));
2683 fStats.incNumDraws();
2684 }
Chris Dalton1d616352017-05-31 12:51:23 -06002685}
2686
Brian Salomon1fabd512018-02-09 09:54:25 -05002687void GrGLGpu::onResolveRenderTarget(GrRenderTarget* target) {
bsalomon@google.com75f9f252012-01-31 13:35:56 +00002688 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00002689 if (rt->needsResolve()) {
bsalomon@google.com347c3822013-05-01 20:10:01 +00002690 // Some extensions automatically resolves the texture when it is read.
2691 if (this->glCaps().usesMSAARenderBuffers()) {
egdanield803f272015-03-18 13:01:52 -07002692 SkASSERT(rt->textureFBOID() != rt->renderFBOID());
Brian Osmancfe83d12018-01-19 11:13:45 -05002693 SkASSERT(rt->textureFBOID() != 0 && rt->renderFBOID() != 0);
Adrienne Walker4ee88512018-05-17 11:37:14 -07002694 this->bindFramebuffer(GR_GL_READ_FRAMEBUFFER, rt->renderFBOID());
2695 this->bindFramebuffer(GR_GL_DRAW_FRAMEBUFFER, rt->textureFBOID());
2696
egdanield803f272015-03-18 13:01:52 -07002697 // make sure we go through flushRenderTarget() since we've modified
2698 // the bound DRAW FBO ID.
Robert Phillips294870f2016-11-11 12:38:40 -05002699 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002700 const GrGLIRect& vp = rt->getViewport();
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00002701 const SkIRect dirtyRect = rt->getResolveRect();
Brian Salomon1fabd512018-02-09 09:54:25 -05002702 // The dirty rect tracked on the RT is always stored in the native coordinates of the
2703 // surface. Choose kTopLeft so no adjustments are made
2704 static constexpr auto kDirtyRectOrigin = kTopLeft_GrSurfaceOrigin;
bsalomon@google.com347c3822013-05-01 20:10:01 +00002705 if (GrGLCaps::kES_Apple_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002706 // Apple's extension uses the scissor as the blit bounds.
Brian Salomond818ebf2018-07-02 14:08:49 +00002707 GrScissorState scissorState;
2708 scissorState.set(dirtyRect);
2709 this->flushScissor(scissorState, vp, kDirtyRectOrigin);
csmartdalton28341fa2016-08-17 10:00:21 -07002710 this->disableWindowRectangles();
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002711 GL_CALL(ResolveMultisampleFramebuffer());
2712 } else {
Brian Salomone5e7eb12016-10-14 16:18:33 -04002713 int l, b, r, t;
2714 if (GrGLCaps::kResolveMustBeFull_BlitFrambufferFlag &
2715 this->glCaps().blitFramebufferSupportFlags()) {
2716 l = 0;
2717 b = 0;
2718 r = target->width();
2719 t = target->height();
2720 } else {
2721 GrGLIRect rect;
Brian Salomon1fabd512018-02-09 09:54:25 -05002722 rect.setRelativeTo(vp, dirtyRect, kDirtyRectOrigin);
Brian Salomone5e7eb12016-10-14 16:18:33 -04002723 l = rect.fLeft;
2724 b = rect.fBottom;
2725 r = rect.fLeft + rect.fWidth;
2726 t = rect.fBottom + rect.fHeight;
2727 }
derekf8c8f71a2014-09-16 06:24:57 -07002728
2729 // BlitFrameBuffer respects the scissor, so disable it.
Brian Salomond818ebf2018-07-02 14:08:49 +00002730 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07002731 this->disableWindowRectangles();
Brian Salomone5e7eb12016-10-14 16:18:33 -04002732 GL_CALL(BlitFramebuffer(l, b, r, t, l, b, r, t,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002733 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00002734 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002735 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00002736 rt->flagAsResolved();
reed@google.comac10a2d2010-12-22 21:39:39 +00002737 }
2738}
2739
bsalomon@google.com411dad02012-06-05 20:24:20 +00002740namespace {
bsalomon@google.comd302f142011-03-03 13:54:13 +00002741
bsalomon@google.com411dad02012-06-05 20:24:20 +00002742
2743GrGLenum gr_to_gl_stencil_op(GrStencilOp op) {
cdalton93a379b2016-05-11 13:58:08 -07002744 static const GrGLenum gTable[kGrStencilOpCount] = {
2745 GR_GL_KEEP, // kKeep
2746 GR_GL_ZERO, // kZero
2747 GR_GL_REPLACE, // kReplace
2748 GR_GL_INVERT, // kInvert
2749 GR_GL_INCR_WRAP, // kIncWrap
2750 GR_GL_DECR_WRAP, // kDecWrap
2751 GR_GL_INCR, // kIncClamp
2752 GR_GL_DECR, // kDecClamp
bsalomon@google.com411dad02012-06-05 20:24:20 +00002753 };
cdalton93a379b2016-05-11 13:58:08 -07002754 GR_STATIC_ASSERT(0 == (int)GrStencilOp::kKeep);
2755 GR_STATIC_ASSERT(1 == (int)GrStencilOp::kZero);
2756 GR_STATIC_ASSERT(2 == (int)GrStencilOp::kReplace);
2757 GR_STATIC_ASSERT(3 == (int)GrStencilOp::kInvert);
2758 GR_STATIC_ASSERT(4 == (int)GrStencilOp::kIncWrap);
2759 GR_STATIC_ASSERT(5 == (int)GrStencilOp::kDecWrap);
2760 GR_STATIC_ASSERT(6 == (int)GrStencilOp::kIncClamp);
2761 GR_STATIC_ASSERT(7 == (int)GrStencilOp::kDecClamp);
2762 SkASSERT(op < (GrStencilOp)kGrStencilOpCount);
2763 return gTable[(int)op];
bsalomon@google.com411dad02012-06-05 20:24:20 +00002764}
2765
2766void set_gl_stencil(const GrGLInterface* gl,
cdalton93a379b2016-05-11 13:58:08 -07002767 const GrStencilSettings::Face& face,
2768 GrGLenum glFace) {
2769 GrGLenum glFunc = GrToGLStencilFunc(face.fTest);
2770 GrGLenum glFailOp = gr_to_gl_stencil_op(face.fFailOp);
2771 GrGLenum glPassOp = gr_to_gl_stencil_op(face.fPassOp);
bsalomon@google.coma3201942012-06-21 19:58:20 +00002772
cdalton93a379b2016-05-11 13:58:08 -07002773 GrGLint ref = face.fRef;
2774 GrGLint mask = face.fTestMask;
2775 GrGLint writeMask = face.fWriteMask;
bsalomon@google.com411dad02012-06-05 20:24:20 +00002776
2777 if (GR_GL_FRONT_AND_BACK == glFace) {
2778 // we call the combined func just in case separate stencil is not
2779 // supported.
2780 GR_GL_CALL(gl, StencilFunc(glFunc, ref, mask));
2781 GR_GL_CALL(gl, StencilMask(writeMask));
vbuzinovc5d58f02015-08-21 05:24:24 -07002782 GR_GL_CALL(gl, StencilOp(glFailOp, GR_GL_KEEP, glPassOp));
bsalomon@google.com411dad02012-06-05 20:24:20 +00002783 } else {
2784 GR_GL_CALL(gl, StencilFuncSeparate(glFace, glFunc, ref, mask));
2785 GR_GL_CALL(gl, StencilMaskSeparate(glFace, writeMask));
vbuzinovc5d58f02015-08-21 05:24:24 -07002786 GR_GL_CALL(gl, StencilOpSeparate(glFace, glFailOp, GR_GL_KEEP, glPassOp));
bsalomon@google.com411dad02012-06-05 20:24:20 +00002787 }
2788}
2789}
bsalomon@google.comd302f142011-03-03 13:54:13 +00002790
bsalomon3e791242014-12-17 13:43:13 -08002791void GrGLGpu::flushStencil(const GrStencilSettings& stencilSettings) {
csmartdaltonc7d85332016-10-26 10:13:46 -07002792 if (stencilSettings.isDisabled()) {
2793 this->disableStencil();
2794 } else if (fHWStencilSettings != stencilSettings) {
2795 if (kYes_TriState != fHWStencilTestEnabled) {
2796 GL_CALL(Enable(GR_GL_STENCIL_TEST));
Brian Salomonaf971de2017-06-08 16:11:33 -04002797
csmartdaltonc7d85332016-10-26 10:13:46 -07002798 fHWStencilTestEnabled = kYes_TriState;
bsalomon@google.coma3201942012-06-21 19:58:20 +00002799 }
csmartdaltonc7d85332016-10-26 10:13:46 -07002800 if (stencilSettings.isTwoSided()) {
csmartdaltonc7d85332016-10-26 10:13:46 -07002801 set_gl_stencil(this->glInterface(),
2802 stencilSettings.front(),
2803 GR_GL_FRONT);
2804 set_gl_stencil(this->glInterface(),
2805 stencilSettings.back(),
2806 GR_GL_BACK);
2807 } else {
2808 set_gl_stencil(this->glInterface(),
2809 stencilSettings.front(),
2810 GR_GL_FRONT_AND_BACK);
bsalomon@google.comd302f142011-03-03 13:54:13 +00002811 }
joshualitta58fe352014-10-27 08:39:00 -07002812 fHWStencilSettings = stencilSettings;
reed@google.comac10a2d2010-12-22 21:39:39 +00002813 }
2814}
2815
csmartdaltonc7d85332016-10-26 10:13:46 -07002816void GrGLGpu::disableStencil() {
2817 if (kNo_TriState != fHWStencilTestEnabled) {
2818 GL_CALL(Disable(GR_GL_STENCIL_TEST));
Brian Salomonaf971de2017-06-08 16:11:33 -04002819
csmartdaltonc7d85332016-10-26 10:13:46 -07002820 fHWStencilTestEnabled = kNo_TriState;
2821 fHWStencilSettings.invalidate();
2822 }
2823}
2824
cdaltonaf8bc7d2016-02-05 09:35:20 -08002825void GrGLGpu::flushHWAAState(GrRenderTarget* rt, bool useHWAA, bool stencilEnabled) {
bsalomon083617b2016-02-12 12:10:14 -08002826 // rt is only optional if useHWAA is false.
2827 SkASSERT(rt || !useHWAA);
vbuzinovdded6962015-06-12 08:59:45 -07002828 SkASSERT(!useHWAA || rt->isStencilBufferMultisampled());
bsalomon@google.com202d1392013-03-19 18:58:08 +00002829
csmartdalton2b5f2cb2016-06-10 14:06:32 -07002830 if (this->caps()->multisampleDisableSupport()) {
cdaltond0a840d2015-03-16 17:19:58 -07002831 if (useHWAA) {
2832 if (kYes_TriState != fMSAAEnabled) {
2833 GL_CALL(Enable(GR_GL_MULTISAMPLE));
2834 fMSAAEnabled = kYes_TriState;
2835 }
2836 } else {
2837 if (kNo_TriState != fMSAAEnabled) {
2838 GL_CALL(Disable(GR_GL_MULTISAMPLE));
2839 fMSAAEnabled = kNo_TriState;
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00002840 }
2841 }
2842 }
cdaltonaf8bc7d2016-02-05 09:35:20 -08002843
2844 if (0 != this->caps()->maxRasterSamples()) {
Brian Salomon7c8460e2017-05-12 11:36:10 -04002845 if (useHWAA && GrFSAAType::kMixedSamples == rt->fsaaType() && !stencilEnabled) {
cdaltonaf8bc7d2016-02-05 09:35:20 -08002846 // Since stencil is disabled and we want more samples than are in the color buffer, we
2847 // need to tell the rasterizer explicitly how many to run.
2848 if (kYes_TriState != fHWRasterMultisampleEnabled) {
2849 GL_CALL(Enable(GR_GL_RASTER_MULTISAMPLE));
2850 fHWRasterMultisampleEnabled = kYes_TriState;
2851 }
Brian Salomonbdecacf2018-02-02 20:32:49 -05002852 int numStencilSamples = rt->numStencilSamples();
2853 // convert to GL's understanding of sample counts where 0 means nonMSAA.
2854 numStencilSamples = 1 == numStencilSamples ? 0 : numStencilSamples;
2855 if (numStencilSamples != fHWNumRasterSamples) {
2856 SkASSERT(numStencilSamples <= this->caps()->maxRasterSamples());
2857 GL_CALL(RasterSamples(numStencilSamples, GR_GL_TRUE));
2858 fHWNumRasterSamples = numStencilSamples;
cdaltonaf8bc7d2016-02-05 09:35:20 -08002859 }
2860 } else {
2861 if (kNo_TriState != fHWRasterMultisampleEnabled) {
2862 GL_CALL(Disable(GR_GL_RASTER_MULTISAMPLE));
2863 fHWRasterMultisampleEnabled = kNo_TriState;
2864 }
2865 }
2866 } else {
Brian Salomon7c8460e2017-05-12 11:36:10 -04002867 SkASSERT(!useHWAA || GrFSAAType::kMixedSamples != rt->fsaaType() || stencilEnabled);
cdaltonaf8bc7d2016-02-05 09:35:20 -08002868 }
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00002869}
2870
bsalomon7f9b2e42016-01-12 13:29:26 -08002871void GrGLGpu::flushBlend(const GrXferProcessor::BlendInfo& blendInfo, const GrSwizzle& swizzle) {
egdanielb414f252014-07-29 13:15:47 -07002872 // Any optimization to disable blending should have already been applied and
cdalton8917d622015-05-06 13:40:21 -07002873 // tweaked the equation to "add" or "subtract", and the coeffs to (1, 0).
bsalomonf7cc8772015-05-11 11:21:14 -07002874
cdalton8917d622015-05-06 13:40:21 -07002875 GrBlendEquation equation = blendInfo.fEquation;
egdanielc2304142014-12-11 13:15:13 -08002876 GrBlendCoeff srcCoeff = blendInfo.fSrcBlend;
2877 GrBlendCoeff dstCoeff = blendInfo.fDstBlend;
Dongseong Hwang4e1f0222018-11-01 09:10:51 -07002878 bool blendOff =
2879 ((kAdd_GrBlendEquation == equation || kSubtract_GrBlendEquation == equation) &&
2880 kOne_GrBlendCoeff == srcCoeff && kZero_GrBlendCoeff == dstCoeff) ||
2881 !blendInfo.fWriteColor;
egdanielb414f252014-07-29 13:15:47 -07002882 if (blendOff) {
2883 if (kNo_TriState != fHWBlendState.fEnabled) {
2884 GL_CALL(Disable(GR_GL_BLEND));
joel.liang9764c402015-07-09 19:46:18 -07002885
2886 // Workaround for the ARM KHR_blend_equation_advanced blacklist issue
2887 // https://code.google.com/p/skia/issues/detail?id=3943
2888 if (kARM_GrGLVendor == this->ctxInfo().vendor() &&
2889 GrBlendEquationIsAdvanced(fHWBlendState.fEquation)) {
2890 SkASSERT(this->caps()->advancedBlendEquationSupport());
2891 // Set to any basic blending equation.
2892 GrBlendEquation blend_equation = kAdd_GrBlendEquation;
2893 GL_CALL(BlendEquation(gXfermodeEquation2Blend[blend_equation]));
2894 fHWBlendState.fEquation = blend_equation;
2895 }
2896
egdanielb414f252014-07-29 13:15:47 -07002897 fHWBlendState.fEnabled = kNo_TriState;
2898 }
cdalton8917d622015-05-06 13:40:21 -07002899 return;
2900 }
2901
2902 if (kYes_TriState != fHWBlendState.fEnabled) {
2903 GL_CALL(Enable(GR_GL_BLEND));
Brian Salomonaf971de2017-06-08 16:11:33 -04002904
cdalton8917d622015-05-06 13:40:21 -07002905 fHWBlendState.fEnabled = kYes_TriState;
2906 }
2907
Mike Kleinab5fec92018-08-16 19:43:31 +00002908 if (fHWBlendState.fEquation != equation) {
cdalton8917d622015-05-06 13:40:21 -07002909 GL_CALL(BlendEquation(gXfermodeEquation2Blend[equation]));
2910 fHWBlendState.fEquation = equation;
2911 }
2912
2913 if (GrBlendEquationIsAdvanced(equation)) {
2914 SkASSERT(this->caps()->advancedBlendEquationSupport());
2915 // Advanced equations have no other blend state.
2916 return;
2917 }
2918
Mike Kleinab5fec92018-08-16 19:43:31 +00002919 if (fHWBlendState.fSrcCoeff != srcCoeff || fHWBlendState.fDstCoeff != dstCoeff) {
cdalton8917d622015-05-06 13:40:21 -07002920 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
2921 gXfermodeCoeff2Blend[dstCoeff]));
2922 fHWBlendState.fSrcCoeff = srcCoeff;
2923 fHWBlendState.fDstCoeff = dstCoeff;
2924 }
2925
bsalomon7f9b2e42016-01-12 13:29:26 -08002926 if ((BlendCoeffReferencesConstant(srcCoeff) || BlendCoeffReferencesConstant(dstCoeff))) {
Brian Osman422f95b2018-11-05 16:49:04 -05002927 SkPMColor4f blendConst = swizzle.applyTo(blendInfo.fBlendConstant);
bsalomon7f9b2e42016-01-12 13:29:26 -08002928 if (!fHWBlendState.fConstColorValid || fHWBlendState.fConstColor != blendConst) {
Brian Osman422f95b2018-11-05 16:49:04 -05002929 GL_CALL(BlendColor(blendConst.fR, blendConst.fG, blendConst.fB, blendConst.fA));
bsalomon7f9b2e42016-01-12 13:29:26 -08002930 fHWBlendState.fConstColor = blendConst;
2931 fHWBlendState.fConstColorValid = true;
2932 }
bsalomon@google.com0650e812011-04-08 18:07:53 +00002933 }
2934}
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002935
Brian Salomondc829942018-10-23 16:07:24 -04002936static void get_gl_swizzle_values(const GrSwizzle& swizzle, GrGLenum glValues[4]) {
Brian Salomon327955b2018-10-22 15:39:22 +00002937 for (int i = 0; i < 4; ++i) {
Brian Salomondc829942018-10-23 16:07:24 -04002938 switch (swizzle[i]) {
2939 case 'r': glValues[i] = GR_GL_RED; break;
2940 case 'g': glValues[i] = GR_GL_GREEN; break;
2941 case 'b': glValues[i] = GR_GL_BLUE; break;
2942 case 'a': glValues[i] = GR_GL_ALPHA; break;
2943 default: SK_ABORT("Unsupported component");
2944 }
Brian Salomon327955b2018-10-22 15:39:22 +00002945 }
2946}
2947
Brian Salomondc829942018-10-23 16:07:24 -04002948void GrGLGpu::bindTexture(int unitIdx, GrSamplerState samplerState, GrGLTexture* texture) {
bsalomon49f085d2014-09-05 13:34:00 -07002949 SkASSERT(texture);
tomhudson@google.comd0c1a062012-07-12 17:23:52 +00002950
reed856e9d92015-09-30 12:21:45 -07002951#ifdef SK_DEBUG
2952 if (!this->caps()->npotTextureTileSupport()) {
Brian Salomon2bbdcc42017-09-07 12:36:34 -04002953 if (samplerState.isRepeated()) {
reed856e9d92015-09-30 12:21:45 -07002954 const int w = texture->width();
2955 const int h = texture->height();
2956 SkASSERT(SkIsPow2(w) && SkIsPow2(h));
2957 }
2958 }
2959#endif
2960
bsalomon@google.comb8670992012-07-25 21:27:09 +00002961 // If we created a rt/tex and rendered to it without using a texture and now we're texturing
2962 // 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 +00002963 // out of the "last != next" check.
bsalomon37dd3312014-11-03 08:47:23 -08002964 GrGLRenderTarget* texRT = static_cast<GrGLRenderTarget*>(texture->asRenderTarget());
bsalomon49f085d2014-09-05 13:34:00 -07002965 if (texRT) {
Brian Salomon1fabd512018-02-09 09:54:25 -05002966 this->onResolveRenderTarget(texRT);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002967 }
2968
Robert Phillips294870f2016-11-11 12:38:40 -05002969 GrGpuResource::UniqueID textureID = texture->uniqueID();
bsalomon10528f12015-10-14 12:54:52 -07002970 GrGLenum target = texture->target();
bsalomon1c63bf62014-07-22 13:09:46 -07002971 if (fHWBoundTextureUniqueIDs[unitIdx] != textureID) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002972 this->setTextureUnit(unitIdx);
bsalomon10528f12015-10-14 12:54:52 -07002973 GL_CALL(BindTexture(target, texture->textureID()));
bsalomon1c63bf62014-07-22 13:09:46 -07002974 fHWBoundTextureUniqueIDs[unitIdx] = textureID;
bsalomon@google.com4c883782012-06-04 19:05:11 +00002975 }
2976
Brian Salomondc829942018-10-23 16:07:24 -04002977 if (samplerState.filter() == GrSamplerState::Filter::kMipMap) {
Greg Daniel8f5bbda2018-06-08 17:22:23 -04002978 if (!this->caps()->mipMapSupport() ||
2979 texture->texturePriv().mipMapped() == GrMipMapped::kNo) {
Brian Salomondc829942018-10-23 16:07:24 -04002980 samplerState.setFilterMode(GrSamplerState::Filter::kBilerp);
bsalomonefd7d452014-10-23 14:17:46 -07002981 }
commit-bot@chromium.org47442312013-12-19 16:18:01 +00002982 }
bsalomonefd7d452014-10-23 14:17:46 -07002983
brianosman33f6b3f2016-06-02 05:49:21 -07002984#ifdef SK_DEBUG
Brian Osman2b23c4b2018-06-01 12:25:08 -04002985 // We were supposed to ensure MipMaps were up-to-date before getting here.
Brian Salomondc829942018-10-23 16:07:24 -04002986 if (samplerState.filter() == GrSamplerState::Filter::kMipMap) {
brianosman33f6b3f2016-06-02 05:49:21 -07002987 SkASSERT(!texture->texturePriv().mipMapsAreDirty());
brianosman33f6b3f2016-06-02 05:49:21 -07002988 }
2989#endif
2990
Brian Salomondc829942018-10-23 16:07:24 -04002991 ResetTimestamp timestamp = texture->getCachedParamsTimestamp();
2992 bool setAll = timestamp < this->getResetTimestamp();
cblume55f2d2d2016-02-26 13:20:48 -08002993
Brian Salomondc829942018-10-23 16:07:24 -04002994 const GrGLTexture::SamplerParams* samplerParamsToRecord = nullptr;
2995 GrGLTexture::SamplerParams newSamplerParams;
2996 if (fSamplerObjectCache) {
2997 fSamplerObjectCache->bindSampler(unitIdx, samplerState);
2998 } else {
2999 const GrGLTexture::SamplerParams& oldSamplerParams = texture->getCachedSamplerParams();
3000 samplerParamsToRecord = &newSamplerParams;
3001
3002 newSamplerParams.fMinFilter = filter_to_gl_min_filter(samplerState.filter());
3003 newSamplerParams.fMagFilter = filter_to_gl_mag_filter(samplerState.filter());
3004
Michael Ludwigf23a1522018-12-10 11:36:13 -05003005 newSamplerParams.fWrapS = wrap_mode_to_gl_wrap(samplerState.wrapModeX(), this->glCaps());
3006 newSamplerParams.fWrapT = wrap_mode_to_gl_wrap(samplerState.wrapModeY(), this->glCaps());
Brian Salomondc829942018-10-23 16:07:24 -04003007
3008 // These are the OpenGL default values.
Brian Salomon89f2ff12018-12-07 19:30:25 -05003009 newSamplerParams.fMinLOD = -1000.f;
3010 newSamplerParams.fMaxLOD = 1000.f;
Brian Salomondc829942018-10-23 16:07:24 -04003011
3012 if (setAll || newSamplerParams.fMagFilter != oldSamplerParams.fMagFilter) {
3013 this->setTextureUnit(unitIdx);
3014 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MAG_FILTER, newSamplerParams.fMagFilter));
3015 }
3016 if (setAll || newSamplerParams.fMinFilter != oldSamplerParams.fMinFilter) {
3017 this->setTextureUnit(unitIdx);
3018 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MIN_FILTER, newSamplerParams.fMinFilter));
3019 }
Mike Klein1bccae52018-10-19 11:38:44 +00003020 if (this->glCaps().mipMapLevelAndLodControlSupport()) {
Brian Salomondc829942018-10-23 16:07:24 -04003021 if (setAll || newSamplerParams.fMinLOD != oldSamplerParams.fMinLOD) {
Mike Klein1bccae52018-10-19 11:38:44 +00003022 this->setTextureUnit(unitIdx);
Brian Salomon89f2ff12018-12-07 19:30:25 -05003023 GL_CALL(TexParameterf(target, GR_GL_TEXTURE_MIN_LOD, newSamplerParams.fMinLOD));
Brian Salomondc829942018-10-23 16:07:24 -04003024 }
3025 if (setAll || newSamplerParams.fMaxLOD != oldSamplerParams.fMaxLOD) {
3026 this->setTextureUnit(unitIdx);
Brian Salomon89f2ff12018-12-07 19:30:25 -05003027 GL_CALL(TexParameterf(target, GR_GL_TEXTURE_MAX_LOD, newSamplerParams.fMaxLOD));
Brian Salomondc829942018-10-23 16:07:24 -04003028 }
3029 }
3030 if (setAll || newSamplerParams.fWrapS != oldSamplerParams.fWrapS) {
3031 this->setTextureUnit(unitIdx);
3032 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_WRAP_S, newSamplerParams.fWrapS));
3033 }
3034 if (setAll || newSamplerParams.fWrapT != oldSamplerParams.fWrapT) {
3035 this->setTextureUnit(unitIdx);
3036 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_WRAP_T, newSamplerParams.fWrapT));
3037 }
Michael Ludwigf23a1522018-12-10 11:36:13 -05003038 if (this->glCaps().clampToBorderSupport()) {
3039 // Make sure the border color is transparent black (the default)
3040 if (setAll || oldSamplerParams.fBorderColorInvalid) {
3041 this->setTextureUnit(unitIdx);
Brian Salomon89f2ff12018-12-07 19:30:25 -05003042 static const GrGLfloat kTransparentBlack[4] = {0.f, 0.f, 0.f, 0.f};
3043 GL_CALL(TexParameterfv(target, GR_GL_TEXTURE_BORDER_COLOR, kTransparentBlack));
Michael Ludwigf23a1522018-12-10 11:36:13 -05003044 }
3045 }
Brian Salomondc829942018-10-23 16:07:24 -04003046 }
3047 GrGLTexture::NonSamplerParams newNonSamplerParams;
3048 newNonSamplerParams.fBaseMipMapLevel = 0;
3049 newNonSamplerParams.fMaxMipMapLevel = texture->texturePriv().maxMipMapLevel();
3050
3051 const GrGLTexture::NonSamplerParams& oldNonSamplerParams = texture->getCachedNonSamplerParams();
3052 if (this->glCaps().textureSwizzleSupport()) {
3053 auto swizzle = this->glCaps().configSwizzle(texture->config());
3054 newNonSamplerParams.fSwizzleKey = swizzle.asKey();
3055 if (setAll || swizzle.asKey() != oldNonSamplerParams.fSwizzleKey) {
3056 GrGLenum glValues[4];
3057 get_gl_swizzle_values(swizzle, glValues);
3058 this->setTextureUnit(unitIdx);
3059 if (this->glStandard() == kGLES_GrGLStandard) {
3060 // ES3 added swizzle support but not GL_TEXTURE_SWIZZLE_RGBA.
3061 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_R, glValues[0]));
3062 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_G, glValues[1]));
3063 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_B, glValues[2]));
3064 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_A, glValues[3]));
3065 } else {
3066 GR_STATIC_ASSERT(sizeof(glValues[0]) == sizeof(GrGLint));
3067 GL_CALL(TexParameteriv(target, GR_GL_TEXTURE_SWIZZLE_RGBA,
3068 reinterpret_cast<const GrGLint*>(glValues)));
Brian Salomonbfb3df42018-10-19 19:24:31 +00003069 }
Brian Salomonbfb3df42018-10-19 19:24:31 +00003070 }
3071 }
Brian Salomondc829942018-10-23 16:07:24 -04003072 // These are not supported in ES2 contexts
Brian Salomonf3841932018-11-29 09:13:37 -05003073 if (this->glCaps().mipMapLevelAndLodControlSupport() &&
3074 (texture->texturePriv().textureType() != GrTextureType::kExternal ||
3075 !this->glCaps().dontSetBaseOrMaxLevelForExternalTextures())) {
Brian Salomondc829942018-10-23 16:07:24 -04003076 if (newNonSamplerParams.fBaseMipMapLevel != oldNonSamplerParams.fBaseMipMapLevel) {
3077 this->setTextureUnit(unitIdx);
3078 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_BASE_LEVEL,
3079 newNonSamplerParams.fBaseMipMapLevel));
3080 }
3081 if (newNonSamplerParams.fMaxMipMapLevel != oldNonSamplerParams.fMaxMipMapLevel) {
3082 this->setTextureUnit(unitIdx);
3083 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MAX_LEVEL,
3084 newNonSamplerParams.fMaxMipMapLevel));
3085 }
Brian Salomon327955b2018-10-22 15:39:22 +00003086 }
Brian Salomondc829942018-10-23 16:07:24 -04003087 texture->setCachedParams(samplerParamsToRecord, newNonSamplerParams, this->getResetTimestamp());
cdalton74b8d322016-04-11 14:47:28 -07003088}
3089
egdaniel080e6732014-12-22 07:35:52 -08003090void GrGLGpu::flushColorWrite(bool writeColor) {
3091 if (!writeColor) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +00003092 if (kNo_TriState != fHWWriteToColor) {
3093 GL_CALL(ColorMask(GR_GL_FALSE, GR_GL_FALSE,
3094 GR_GL_FALSE, GR_GL_FALSE));
3095 fHWWriteToColor = kNo_TriState;
bsalomon@google.comd302f142011-03-03 13:54:13 +00003096 }
bsalomon@google.com978c8c62012-05-21 14:45:49 +00003097 } else {
3098 if (kYes_TriState != fHWWriteToColor) {
3099 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
3100 fHWWriteToColor = kYes_TriState;
3101 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00003102 }
bsalomon3e791242014-12-17 13:43:13 -08003103}
bsalomon@google.comd302f142011-03-03 13:54:13 +00003104
bsalomon861e1032014-12-16 07:33:49 -08003105void GrGLGpu::setTextureUnit(int unit) {
bsalomon1c63bf62014-07-22 13:09:46 -07003106 SkASSERT(unit >= 0 && unit < fHWBoundTextureUniqueIDs.count());
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00003107 if (unit != fHWActiveTextureUnitIdx) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00003108 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com49209392012-06-05 15:13:46 +00003109 fHWActiveTextureUnitIdx = unit;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00003110 }
3111}
bsalomon@google.com316f99232011-01-13 21:28:12 +00003112
bsalomon861e1032014-12-16 07:33:49 -08003113void GrGLGpu::setScratchTextureUnit() {
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00003114 // Bind the last texture unit since it is the least likely to be used by GrGLProgram.
bsalomon1c63bf62014-07-22 13:09:46 -07003115 int lastUnitIdx = fHWBoundTextureUniqueIDs.count() - 1;
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00003116 if (lastUnitIdx != fHWActiveTextureUnitIdx) {
3117 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + lastUnitIdx));
3118 fHWActiveTextureUnitIdx = lastUnitIdx;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00003119 }
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00003120 // clear out the this field so that if a program does use this unit it will rebind the correct
3121 // texture.
Robert Phillips294870f2016-11-11 12:38:40 -05003122 fHWBoundTextureUniqueIDs[lastUnitIdx].makeInvalid();
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00003123}
3124
Brian Salomone5e7eb12016-10-14 16:18:33 -04003125// Determines whether glBlitFramebuffer could be used between src and dst by onCopySurface.
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003126static inline bool can_blit_framebuffer_for_copy_surface(
3127 const GrSurface* dst, GrSurfaceOrigin dstOrigin,
3128 const GrSurface* src, GrSurfaceOrigin srcOrigin,
3129 const SkIRect& srcRect,
3130 const SkIPoint& dstPoint,
Greg Daniel26dbe3b2018-05-03 10:35:42 -04003131 const GrGLCaps& caps) {
3132 int dstSampleCnt = 0;
3133 int srcSampleCnt = 0;
3134 if (const GrRenderTarget* rt = dst->asRenderTarget()) {
3135 dstSampleCnt = rt->numColorSamples();
bsalomon@google.comeb851172013-04-15 13:51:00 +00003136 }
Greg Daniel26dbe3b2018-05-03 10:35:42 -04003137 if (const GrRenderTarget* rt = src->asRenderTarget()) {
3138 srcSampleCnt = rt->numColorSamples();
3139 }
3140 SkASSERT((dstSampleCnt > 0) == SkToBool(dst->asRenderTarget()));
3141 SkASSERT((srcSampleCnt > 0) == SkToBool(src->asRenderTarget()));
3142
Brian Salomone5e7eb12016-10-14 16:18:33 -04003143 const GrGLTexture* dstTex = static_cast<const GrGLTexture*>(dst->asTexture());
Mike Klein1d746202018-01-25 17:32:51 -05003144 const GrGLTexture* srcTex = static_cast<const GrGLTexture*>(src->asTexture());
Greg Daniel26dbe3b2018-05-03 10:35:42 -04003145
3146 bool dstIsGLTexture2D = dstTex ? GR_GL_TEXTURE_2D == dstTex->target() : false;
3147 bool srcIsGLTexture2D = srcTex ? GR_GL_TEXTURE_2D == srcTex->target() : false;
3148
3149 return caps.canCopyAsBlit(dst->config(), dstSampleCnt, SkToBool(dstTex), dstIsGLTexture2D,
3150 dstOrigin, src->config(), srcSampleCnt, SkToBool(srcTex),
3151 srcIsGLTexture2D, srcOrigin, src->getBoundsRect(), srcRect, dstPoint);
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00003152}
bsalomon@google.comeb851172013-04-15 13:51:00 +00003153
Brian Osmana9c8a052018-01-19 10:31:56 -05003154static bool rt_has_msaa_render_buffer(const GrGLRenderTarget* rt, const GrGLCaps& glCaps) {
3155 // A RT has a separate MSAA renderbuffer if:
3156 // 1) It's multisampled
3157 // 2) We're using an extension with separate MSAA renderbuffers
3158 // 3) It's not FBO 0, which is special and always auto-resolves
Brian Salomonbdecacf2018-02-02 20:32:49 -05003159 return rt->numColorSamples() > 1 && glCaps.usesMSAARenderBuffers() && rt->renderFBOID() != 0;
Brian Osmana9c8a052018-01-19 10:31:56 -05003160}
3161
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003162static inline bool can_copy_texsubimage(const GrSurface* dst, GrSurfaceOrigin dstOrigin,
3163 const GrSurface* src, GrSurfaceOrigin srcOrigin,
Greg Daniel26dbe3b2018-05-03 10:35:42 -04003164 const GrGLCaps& caps) {
3165
bsalomon@google.comeb851172013-04-15 13:51:00 +00003166 const GrGLRenderTarget* dstRT = static_cast<const GrGLRenderTarget*>(dst->asRenderTarget());
bsalomon@google.coma2719852013-04-17 14:25:27 +00003167 const GrGLRenderTarget* srcRT = static_cast<const GrGLRenderTarget*>(src->asRenderTarget());
bsalomon7ea33f52015-11-22 14:51:00 -08003168 const GrGLTexture* dstTex = static_cast<const GrGLTexture*>(dst->asTexture());
bsalomon7ea33f52015-11-22 14:51:00 -08003169 const GrGLTexture* srcTex = static_cast<const GrGLTexture*>(src->asTexture());
cblume61214052016-01-26 09:10:48 -08003170
Greg Daniel26dbe3b2018-05-03 10:35:42 -04003171 bool dstHasMSAARenderBuffer = dstRT ? rt_has_msaa_render_buffer(dstRT, caps) : false;
3172 bool srcHasMSAARenderBuffer = srcRT ? rt_has_msaa_render_buffer(srcRT, caps) : false;
3173
3174 bool dstIsGLTexture2D = dstTex ? GR_GL_TEXTURE_2D == dstTex->target() : false;
3175 bool srcIsGLTexture2D = srcTex ? GR_GL_TEXTURE_2D == srcTex->target() : false;
3176
3177 return caps.canCopyTexSubImage(dst->config(), dstHasMSAARenderBuffer, SkToBool(dstTex),
3178 dstIsGLTexture2D, dstOrigin, src->config(),
3179 srcHasMSAARenderBuffer, SkToBool(srcTex), srcIsGLTexture2D,
3180 srcOrigin);
bsalomon@google.comeb851172013-04-15 13:51:00 +00003181}
3182
3183// If a temporary FBO was created, its non-zero ID is returned. The viewport that the copy rect is
3184// relative to is output.
Brian Salomon71d9d842016-11-03 13:42:00 -04003185void GrGLGpu::bindSurfaceFBOForPixelOps(GrSurface* surface, GrGLenum fboTarget, GrGLIRect* viewport,
3186 TempFBOTarget tempFBOTarget) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00003187 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(surface->asRenderTarget());
bsalomon083617b2016-02-12 12:10:14 -08003188 if (!rt) {
bsalomon49f085d2014-09-05 13:34:00 -07003189 SkASSERT(surface->asTexture());
Brian Salomon9bada542017-06-12 12:09:30 -04003190 GrGLTexture* texture = static_cast<GrGLTexture*>(surface->asTexture());
3191 GrGLuint texID = texture->textureID();
3192 GrGLenum target = texture->target();
egdanield803f272015-03-18 13:01:52 -07003193 GrGLuint* tempFBOID;
3194 tempFBOID = kSrc_TempFBOTarget == tempFBOTarget ? &fTempSrcFBOID : &fTempDstFBOID;
egdaniel0f5f9672015-02-03 11:10:51 -08003195
egdanield803f272015-03-18 13:01:52 -07003196 if (0 == *tempFBOID) {
3197 GR_GL_CALL(this->glInterface(), GenFramebuffers(1, tempFBOID));
egdaniel0f5f9672015-02-03 11:10:51 -08003198 }
3199
Adrienne Walker4ee88512018-05-17 11:37:14 -07003200 this->bindFramebuffer(fboTarget, *tempFBOID);
egdanield803f272015-03-18 13:01:52 -07003201 GR_GL_CALL(this->glInterface(), FramebufferTexture2D(fboTarget,
robertphillips754f4e92014-09-18 13:52:08 -07003202 GR_GL_COLOR_ATTACHMENT0,
bsalomon10528f12015-10-14 12:54:52 -07003203 target,
robertphillips754f4e92014-09-18 13:52:08 -07003204 texID,
3205 0));
Brian Salomon9bada542017-06-12 12:09:30 -04003206 texture->baseLevelWasBoundToFBO();
bsalomon@google.comeb851172013-04-15 13:51:00 +00003207 viewport->fLeft = 0;
3208 viewport->fBottom = 0;
3209 viewport->fWidth = surface->width();
3210 viewport->fHeight = surface->height();
3211 } else {
Adrienne Walker4ee88512018-05-17 11:37:14 -07003212 this->bindFramebuffer(fboTarget, rt->renderFBOID());
bsalomon@google.comeb851172013-04-15 13:51:00 +00003213 *viewport = rt->getViewport();
3214 }
egdaniel0f5f9672015-02-03 11:10:51 -08003215}
3216
Brian Salomon71d9d842016-11-03 13:42:00 -04003217void GrGLGpu::unbindTextureFBOForPixelOps(GrGLenum fboTarget, GrSurface* surface) {
3218 // bindSurfaceFBOForPixelOps temporarily binds textures that are not render targets to
bsalomon10528f12015-10-14 12:54:52 -07003219 if (!surface->asRenderTarget()) {
3220 SkASSERT(surface->asTexture());
3221 GrGLenum textureTarget = static_cast<GrGLTexture*>(surface->asTexture())->target();
3222 GR_GL_CALL(this->glInterface(), FramebufferTexture2D(fboTarget,
3223 GR_GL_COLOR_ATTACHMENT0,
3224 textureTarget,
3225 0,
3226 0));
3227 }
bsalomon@google.comeb851172013-04-15 13:51:00 +00003228}
3229
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07003230void GrGLGpu::onFBOChanged() {
3231 if (this->caps()->workarounds().flush_on_framebuffer_change ||
3232 this->caps()->workarounds().restore_scissor_on_fbo_change) {
3233 GL_CALL(Flush());
3234 }
3235}
3236
Adrienne Walker4ee88512018-05-17 11:37:14 -07003237void GrGLGpu::bindFramebuffer(GrGLenum target, GrGLuint fboid) {
3238 fStats.incRenderTargetBinds();
3239 GL_CALL(BindFramebuffer(target, fboid));
3240 if (target == GR_GL_FRAMEBUFFER || target == GR_GL_DRAW_FRAMEBUFFER) {
3241 fBoundDrawFramebuffer = fboid;
3242 }
3243
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07003244 if (this->caps()->workarounds().restore_scissor_on_fbo_change) {
3245 // The driver forgets the correct scissor when modifying the FBO binding.
3246 if (!fHWScissorSettings.fRect.isInvalid()) {
3247 fHWScissorSettings.fRect.pushToGLScissor(this->glInterface());
3248 }
Adrienne Walker3ed33992018-05-15 11:44:34 -07003249 }
3250
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07003251 this->onFBOChanged();
Adrienne Walker3ed33992018-05-15 11:44:34 -07003252}
3253
Adrienne Walker4ee88512018-05-17 11:37:14 -07003254void GrGLGpu::deleteFramebuffer(GrGLuint fboid) {
3255 if (fboid == fBoundDrawFramebuffer &&
3256 this->caps()->workarounds().unbind_attachments_on_bound_render_fbo_delete) {
3257 // This workaround only applies to deleting currently bound framebuffers
3258 // on Adreno 420. Because this is a somewhat rare case, instead of
3259 // tracking all the attachments of every framebuffer instead just always
3260 // unbind all attachments.
3261 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
3262 GR_GL_RENDERBUFFER, 0));
3263 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT,
3264 GR_GL_RENDERBUFFER, 0));
3265 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_DEPTH_ATTACHMENT,
3266 GR_GL_RENDERBUFFER, 0));
3267 }
3268
3269 GL_CALL(DeleteFramebuffers(1, &fboid));
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07003270
3271 // Deleting the currently bound framebuffer rebinds to 0.
3272 if (fboid == fBoundDrawFramebuffer) {
3273 this->onFBOChanged();
3274 }
Adrienne Walker4ee88512018-05-17 11:37:14 -07003275}
3276
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003277bool GrGLGpu::onCopySurface(GrSurface* dst, GrSurfaceOrigin dstOrigin,
3278 GrSurface* src, GrSurfaceOrigin srcOrigin,
Greg Daniel55fa6472018-03-16 16:13:10 -04003279 const SkIRect& srcRect, const SkIPoint& dstPoint,
3280 bool canDiscardOutsideDstRect) {
bsalomon7f9b2e42016-01-12 13:29:26 -08003281 // None of our copy methods can handle a swizzle. TODO: Make copySurfaceAsDraw handle the
3282 // swizzle.
Brian Salomon1edc5b92016-11-29 13:43:46 -05003283 if (this->caps()->shaderCaps()->configOutputSwizzle(src->config()) !=
3284 this->caps()->shaderCaps()->configOutputSwizzle(dst->config())) {
bsalomon7f9b2e42016-01-12 13:29:26 -08003285 return false;
3286 }
bsalomon083617b2016-02-12 12:10:14 -08003287 // Don't prefer copying as a draw if the dst doesn't already have a FBO object.
Brian Salomon3d86a192018-02-27 16:46:11 -05003288 // This implicitly handles this->glCaps().useDrawInsteadOfAllRenderTargetWrites().
bsalomon083617b2016-02-12 12:10:14 -08003289 bool preferCopy = SkToBool(dst->asRenderTarget());
Greg Daniel26dbe3b2018-05-03 10:35:42 -04003290 if (preferCopy && this->glCaps().canCopyAsDraw(dst->config(), SkToBool(src->asTexture()))) {
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003291 if (this->copySurfaceAsDraw(dst, dstOrigin, src, srcOrigin, srcRect, dstPoint)) {
cdaltone2e71c22016-04-07 18:13:29 -07003292 return true;
3293 }
bsalomon5df6fee2015-05-18 06:26:15 -07003294 }
cblume61214052016-01-26 09:10:48 -08003295
Greg Daniel26dbe3b2018-05-03 10:35:42 -04003296 if (can_copy_texsubimage(dst, dstOrigin, src, srcOrigin, this->glCaps())) {
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003297 this->copySurfaceAsCopyTexSubImage(dst, dstOrigin, src, srcOrigin, srcRect, dstPoint);
bsalomon6df86402015-06-01 10:41:49 -07003298 return true;
3299 }
3300
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003301 if (can_blit_framebuffer_for_copy_surface(dst, dstOrigin, src, srcOrigin,
Greg Daniel26dbe3b2018-05-03 10:35:42 -04003302 srcRect, dstPoint, this->glCaps())) {
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003303 return this->copySurfaceAsBlitFramebuffer(dst, dstOrigin, src, srcOrigin,
3304 srcRect, dstPoint);
bsalomon6df86402015-06-01 10:41:49 -07003305 }
3306
Greg Daniel26dbe3b2018-05-03 10:35:42 -04003307 if (!preferCopy && this->glCaps().canCopyAsDraw(dst->config(), SkToBool(src->asTexture()))) {
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003308 if (this->copySurfaceAsDraw(dst, dstOrigin, src, srcOrigin, srcRect, dstPoint)) {
cdaltone2e71c22016-04-07 18:13:29 -07003309 return true;
3310 }
bsalomon083617b2016-02-12 12:10:14 -08003311 }
3312
bsalomon6df86402015-06-01 10:41:49 -07003313 return false;
3314}
3315
Brian Salomonbf7b6202016-11-11 16:08:03 -05003316bool GrGLGpu::createCopyProgram(GrTexture* srcTex) {
Brian Osman39c08ac2017-07-26 09:36:09 -04003317 TRACE_EVENT0("skia", TRACE_FUNC);
Ryan Macnak38a10ad2017-07-10 10:36:34 -07003318
Brian Salomonbf7b6202016-11-11 16:08:03 -05003319 int progIdx = TextureToCopyProgramIdx(srcTex);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003320 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
Brian Salomon60dd8c72018-07-30 10:24:13 -04003321 GrSLType samplerType =
3322 GrSLCombinedSamplerTypeForTextureType(srcTex->texturePriv().textureType());
cdaltone2e71c22016-04-07 18:13:29 -07003323
3324 if (!fCopyProgramArrayBuffer) {
3325 static const GrGLfloat vdata[] = {
3326 0, 0,
3327 0, 1,
3328 1, 0,
3329 1, 1
3330 };
3331 fCopyProgramArrayBuffer.reset(GrGLBuffer::Create(this, sizeof(vdata), kVertex_GrBufferType,
3332 kStatic_GrAccessPattern, vdata));
3333 }
3334 if (!fCopyProgramArrayBuffer) {
3335 return false;
3336 }
3337
3338 SkASSERT(!fCopyPrograms[progIdx].fProgram);
3339 GL_CALL_RET(fCopyPrograms[progIdx].fProgram, CreateProgram());
3340 if (!fCopyPrograms[progIdx].fProgram) {
3341 return false;
3342 }
3343
Brian Salomon1edc5b92016-11-29 13:43:46 -05003344 const char* version = shaderCaps->versionDeclString();
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003345 GrShaderVar aVertex("a_vertex", kHalf2_GrSLType, GrShaderVar::kIn_TypeModifier);
3346 GrShaderVar uTexCoordXform("u_texCoordXform", kHalf4_GrSLType,
Brian Salomon99938a82016-11-21 13:41:08 -05003347 GrShaderVar::kUniform_TypeModifier);
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003348 GrShaderVar uPosXform("u_posXform", kHalf4_GrSLType, GrShaderVar::kUniform_TypeModifier);
Brian Salomon99938a82016-11-21 13:41:08 -05003349 GrShaderVar uTexture("u_texture", samplerType, GrShaderVar::kUniform_TypeModifier);
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003350 GrShaderVar vTexCoord("v_texCoord", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier);
3351 GrShaderVar oFragColor("o_FragColor", kHalf4_GrSLType, GrShaderVar::kOut_TypeModifier);
cdaltone2e71c22016-04-07 18:13:29 -07003352
3353 SkString vshaderTxt(version);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003354 if (shaderCaps->noperspectiveInterpolationSupport()) {
3355 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
cdaltone2e71c22016-04-07 18:13:29 -07003356 vshaderTxt.appendf("#extension %s : require\n", extension);
3357 }
3358 vTexCoord.addModifier("noperspective");
3359 }
3360
Brian Salomon1edc5b92016-11-29 13:43:46 -05003361 aVertex.appendDecl(shaderCaps, &vshaderTxt);
cdaltone2e71c22016-04-07 18:13:29 -07003362 vshaderTxt.append(";");
Brian Salomon1edc5b92016-11-29 13:43:46 -05003363 uTexCoordXform.appendDecl(shaderCaps, &vshaderTxt);
cdaltone2e71c22016-04-07 18:13:29 -07003364 vshaderTxt.append(";");
Brian Salomon1edc5b92016-11-29 13:43:46 -05003365 uPosXform.appendDecl(shaderCaps, &vshaderTxt);
cdaltone2e71c22016-04-07 18:13:29 -07003366 vshaderTxt.append(";");
Brian Salomon1edc5b92016-11-29 13:43:46 -05003367 vTexCoord.appendDecl(shaderCaps, &vshaderTxt);
cdaltone2e71c22016-04-07 18:13:29 -07003368 vshaderTxt.append(";");
3369
3370 vshaderTxt.append(
3371 "// Copy Program VS\n"
3372 "void main() {"
3373 " v_texCoord = a_vertex.xy * u_texCoordXform.xy + u_texCoordXform.zw;"
Ethan Nicholasbed683a2017-09-26 14:23:59 -04003374 " sk_Position.xy = a_vertex * u_posXform.xy + u_posXform.zw;"
3375 " sk_Position.zw = half2(0, 1);"
cdaltone2e71c22016-04-07 18:13:29 -07003376 "}"
3377 );
3378
3379 SkString fshaderTxt(version);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003380 if (shaderCaps->noperspectiveInterpolationSupport()) {
3381 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
cdaltone2e71c22016-04-07 18:13:29 -07003382 fshaderTxt.appendf("#extension %s : require\n", extension);
3383 }
3384 }
Brian Salomonf31ae492016-11-18 15:35:33 -05003385 vTexCoord.setTypeModifier(GrShaderVar::kIn_TypeModifier);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003386 vTexCoord.appendDecl(shaderCaps, &fshaderTxt);
cdaltone2e71c22016-04-07 18:13:29 -07003387 fshaderTxt.append(";");
Brian Salomon1edc5b92016-11-29 13:43:46 -05003388 uTexture.appendDecl(shaderCaps, &fshaderTxt);
cdaltone2e71c22016-04-07 18:13:29 -07003389 fshaderTxt.append(";");
cdaltone2e71c22016-04-07 18:13:29 -07003390 fshaderTxt.appendf(
3391 "// Copy Program FS\n"
3392 "void main() {"
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003393 " sk_FragColor = texture(u_texture, v_texCoord);"
3394 "}"
cdaltone2e71c22016-04-07 18:13:29 -07003395 );
3396
3397 const char* str;
3398 GrGLint length;
3399
3400 str = vshaderTxt.c_str();
3401 length = SkToInt(vshaderTxt.size());
Ethan Nicholas941e7e22016-12-12 15:33:30 -05003402 SkSL::Program::Settings settings;
3403 settings.fCaps = shaderCaps;
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003404 SkSL::String glsl;
3405 std::unique_ptr<SkSL::Program> program = GrSkSLtoGLSL(*fGLContext, GR_GL_VERTEX_SHADER,
3406 &str, &length, 1, settings, &glsl);
cdaltone2e71c22016-04-07 18:13:29 -07003407 GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fCopyPrograms[progIdx].fProgram,
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003408 GR_GL_VERTEX_SHADER, glsl.c_str(), glsl.size(),
3409 &fStats, settings);
3410 SkASSERT(program->fInputs.isEmpty());
cdaltone2e71c22016-04-07 18:13:29 -07003411
3412 str = fshaderTxt.c_str();
3413 length = SkToInt(fshaderTxt.size());
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003414 program = GrSkSLtoGLSL(*fGLContext, GR_GL_FRAGMENT_SHADER, &str, &length, 1, settings, &glsl);
cdaltone2e71c22016-04-07 18:13:29 -07003415 GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fCopyPrograms[progIdx].fProgram,
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003416 GR_GL_FRAGMENT_SHADER, glsl.c_str(), glsl.size(),
3417 &fStats, settings);
3418 SkASSERT(program->fInputs.isEmpty());
cdaltone2e71c22016-04-07 18:13:29 -07003419
3420 GL_CALL(LinkProgram(fCopyPrograms[progIdx].fProgram));
3421
3422 GL_CALL_RET(fCopyPrograms[progIdx].fTextureUniform,
3423 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_texture"));
3424 GL_CALL_RET(fCopyPrograms[progIdx].fPosXformUniform,
3425 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_posXform"));
3426 GL_CALL_RET(fCopyPrograms[progIdx].fTexCoordXformUniform,
3427 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_texCoordXform"));
3428
3429 GL_CALL(BindAttribLocation(fCopyPrograms[progIdx].fProgram, 0, "a_vertex"));
3430
3431 GL_CALL(DeleteShader(vshader));
3432 GL_CALL(DeleteShader(fshader));
3433
3434 return true;
bsalomon6df86402015-06-01 10:41:49 -07003435}
3436
brianosman33f6b3f2016-06-02 05:49:21 -07003437bool GrGLGpu::createMipmapProgram(int progIdx) {
3438 const bool oddWidth = SkToBool(progIdx & 0x2);
3439 const bool oddHeight = SkToBool(progIdx & 0x1);
3440 const int numTaps = (oddWidth ? 2 : 1) * (oddHeight ? 2 : 1);
3441
Brian Salomon1edc5b92016-11-29 13:43:46 -05003442 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
brianosman33f6b3f2016-06-02 05:49:21 -07003443
3444 SkASSERT(!fMipmapPrograms[progIdx].fProgram);
3445 GL_CALL_RET(fMipmapPrograms[progIdx].fProgram, CreateProgram());
3446 if (!fMipmapPrograms[progIdx].fProgram) {
3447 return false;
3448 }
3449
Brian Salomon1edc5b92016-11-29 13:43:46 -05003450 const char* version = shaderCaps->versionDeclString();
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003451 GrShaderVar aVertex("a_vertex", kHalf2_GrSLType, GrShaderVar::kIn_TypeModifier);
3452 GrShaderVar uTexCoordXform("u_texCoordXform", kHalf4_GrSLType,
Brian Salomon99938a82016-11-21 13:41:08 -05003453 GrShaderVar::kUniform_TypeModifier);
3454 GrShaderVar uTexture("u_texture", kTexture2DSampler_GrSLType,
3455 GrShaderVar::kUniform_TypeModifier);
brianosman33f6b3f2016-06-02 05:49:21 -07003456 // We need 1, 2, or 4 texture coordinates (depending on parity of each dimension):
Brian Salomon99938a82016-11-21 13:41:08 -05003457 GrShaderVar vTexCoords[] = {
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003458 GrShaderVar("v_texCoord0", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier),
3459 GrShaderVar("v_texCoord1", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier),
3460 GrShaderVar("v_texCoord2", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier),
3461 GrShaderVar("v_texCoord3", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier),
brianosman33f6b3f2016-06-02 05:49:21 -07003462 };
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003463 GrShaderVar oFragColor("o_FragColor", kHalf4_GrSLType,GrShaderVar::kOut_TypeModifier);
brianosman33f6b3f2016-06-02 05:49:21 -07003464
3465 SkString vshaderTxt(version);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003466 if (shaderCaps->noperspectiveInterpolationSupport()) {
3467 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
brianosman33f6b3f2016-06-02 05:49:21 -07003468 vshaderTxt.appendf("#extension %s : require\n", extension);
3469 }
3470 vTexCoords[0].addModifier("noperspective");
3471 vTexCoords[1].addModifier("noperspective");
3472 vTexCoords[2].addModifier("noperspective");
3473 vTexCoords[3].addModifier("noperspective");
3474 }
3475
Brian Salomon1edc5b92016-11-29 13:43:46 -05003476 aVertex.appendDecl(shaderCaps, &vshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003477 vshaderTxt.append(";");
Brian Salomon1edc5b92016-11-29 13:43:46 -05003478 uTexCoordXform.appendDecl(shaderCaps, &vshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003479 vshaderTxt.append(";");
3480 for (int i = 0; i < numTaps; ++i) {
Brian Salomon1edc5b92016-11-29 13:43:46 -05003481 vTexCoords[i].appendDecl(shaderCaps, &vshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003482 vshaderTxt.append(";");
3483 }
3484
3485 vshaderTxt.append(
3486 "// Mipmap Program VS\n"
3487 "void main() {"
Ethan Nicholasbed683a2017-09-26 14:23:59 -04003488 " sk_Position.xy = a_vertex * half2(2, 2) - half2(1, 1);"
3489 " sk_Position.zw = half2(0, 1);"
brianosman33f6b3f2016-06-02 05:49:21 -07003490 );
3491
3492 // Insert texture coordinate computation:
3493 if (oddWidth && oddHeight) {
3494 vshaderTxt.append(
3495 " v_texCoord0 = a_vertex.xy * u_texCoordXform.yw;"
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003496 " v_texCoord1 = a_vertex.xy * u_texCoordXform.yw + half2(u_texCoordXform.x, 0);"
3497 " v_texCoord2 = a_vertex.xy * u_texCoordXform.yw + half2(0, u_texCoordXform.z);"
brianosman33f6b3f2016-06-02 05:49:21 -07003498 " v_texCoord3 = a_vertex.xy * u_texCoordXform.yw + u_texCoordXform.xz;"
3499 );
3500 } else if (oddWidth) {
3501 vshaderTxt.append(
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003502 " v_texCoord0 = a_vertex.xy * half2(u_texCoordXform.y, 1);"
3503 " v_texCoord1 = a_vertex.xy * half2(u_texCoordXform.y, 1) + half2(u_texCoordXform.x, 0);"
brianosman33f6b3f2016-06-02 05:49:21 -07003504 );
3505 } else if (oddHeight) {
3506 vshaderTxt.append(
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003507 " v_texCoord0 = a_vertex.xy * half2(1, u_texCoordXform.w);"
3508 " v_texCoord1 = a_vertex.xy * half2(1, u_texCoordXform.w) + half2(0, u_texCoordXform.z);"
brianosman33f6b3f2016-06-02 05:49:21 -07003509 );
3510 } else {
3511 vshaderTxt.append(
3512 " v_texCoord0 = a_vertex.xy;"
3513 );
3514 }
3515
3516 vshaderTxt.append("}");
3517
3518 SkString fshaderTxt(version);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003519 if (shaderCaps->noperspectiveInterpolationSupport()) {
3520 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
brianosman33f6b3f2016-06-02 05:49:21 -07003521 fshaderTxt.appendf("#extension %s : require\n", extension);
3522 }
3523 }
brianosman33f6b3f2016-06-02 05:49:21 -07003524 for (int i = 0; i < numTaps; ++i) {
Brian Salomonf31ae492016-11-18 15:35:33 -05003525 vTexCoords[i].setTypeModifier(GrShaderVar::kIn_TypeModifier);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003526 vTexCoords[i].appendDecl(shaderCaps, &fshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003527 fshaderTxt.append(";");
3528 }
Brian Salomon1edc5b92016-11-29 13:43:46 -05003529 uTexture.appendDecl(shaderCaps, &fshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003530 fshaderTxt.append(";");
brianosman33f6b3f2016-06-02 05:49:21 -07003531 fshaderTxt.append(
3532 "// Mipmap Program FS\n"
3533 "void main() {"
3534 );
3535
3536 if (oddWidth && oddHeight) {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003537 fshaderTxt.append(
3538 " sk_FragColor = (texture(u_texture, v_texCoord0) + "
3539 " texture(u_texture, v_texCoord1) + "
3540 " texture(u_texture, v_texCoord2) + "
3541 " texture(u_texture, v_texCoord3)) * 0.25;"
brianosman33f6b3f2016-06-02 05:49:21 -07003542 );
3543 } else if (oddWidth || oddHeight) {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003544 fshaderTxt.append(
3545 " sk_FragColor = (texture(u_texture, v_texCoord0) + "
3546 " texture(u_texture, v_texCoord1)) * 0.5;"
brianosman33f6b3f2016-06-02 05:49:21 -07003547 );
3548 } else {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003549 fshaderTxt.append(
3550 " sk_FragColor = texture(u_texture, v_texCoord0);"
brianosman33f6b3f2016-06-02 05:49:21 -07003551 );
3552 }
3553
3554 fshaderTxt.append("}");
3555
3556 const char* str;
3557 GrGLint length;
3558
3559 str = vshaderTxt.c_str();
3560 length = SkToInt(vshaderTxt.size());
Ethan Nicholas941e7e22016-12-12 15:33:30 -05003561 SkSL::Program::Settings settings;
3562 settings.fCaps = shaderCaps;
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003563 SkSL::String glsl;
3564 std::unique_ptr<SkSL::Program> program = GrSkSLtoGLSL(*fGLContext, GR_GL_VERTEX_SHADER,
3565 &str, &length, 1, settings, &glsl);
brianosman33f6b3f2016-06-02 05:49:21 -07003566 GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fMipmapPrograms[progIdx].fProgram,
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003567 GR_GL_VERTEX_SHADER, glsl.c_str(), glsl.size(),
3568 &fStats, settings);
3569 SkASSERT(program->fInputs.isEmpty());
brianosman33f6b3f2016-06-02 05:49:21 -07003570
3571 str = fshaderTxt.c_str();
3572 length = SkToInt(fshaderTxt.size());
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003573 program = GrSkSLtoGLSL(*fGLContext, GR_GL_FRAGMENT_SHADER, &str, &length, 1, settings, &glsl);
brianosman33f6b3f2016-06-02 05:49:21 -07003574 GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fMipmapPrograms[progIdx].fProgram,
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003575 GR_GL_FRAGMENT_SHADER, glsl.c_str(), glsl.size(),
3576 &fStats, settings);
3577 SkASSERT(program->fInputs.isEmpty());
brianosman33f6b3f2016-06-02 05:49:21 -07003578
3579 GL_CALL(LinkProgram(fMipmapPrograms[progIdx].fProgram));
3580
3581 GL_CALL_RET(fMipmapPrograms[progIdx].fTextureUniform,
3582 GetUniformLocation(fMipmapPrograms[progIdx].fProgram, "u_texture"));
3583 GL_CALL_RET(fMipmapPrograms[progIdx].fTexCoordXformUniform,
3584 GetUniformLocation(fMipmapPrograms[progIdx].fProgram, "u_texCoordXform"));
3585
3586 GL_CALL(BindAttribLocation(fMipmapPrograms[progIdx].fProgram, 0, "a_vertex"));
3587
3588 GL_CALL(DeleteShader(vshader));
3589 GL_CALL(DeleteShader(fshader));
3590
3591 return true;
3592}
3593
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003594bool GrGLGpu::copySurfaceAsDraw(GrSurface* dst, GrSurfaceOrigin dstOrigin,
3595 GrSurface* src, GrSurfaceOrigin srcOrigin,
bsalomon6df86402015-06-01 10:41:49 -07003596 const SkIRect& srcRect,
3597 const SkIPoint& dstPoint) {
cdaltone2e71c22016-04-07 18:13:29 -07003598 GrGLTexture* srcTex = static_cast<GrGLTexture*>(src->asTexture());
Brian Salomonbf7b6202016-11-11 16:08:03 -05003599 int progIdx = TextureToCopyProgramIdx(srcTex);
cdaltone2e71c22016-04-07 18:13:29 -07003600
Greg Daniel26dbe3b2018-05-03 10:35:42 -04003601 if (!this->glCaps().canConfigBeFBOColorAttachment(dst->config())) {
3602 return false;
3603 }
3604
cdaltone2e71c22016-04-07 18:13:29 -07003605 if (!fCopyPrograms[progIdx].fProgram) {
Brian Salomonbf7b6202016-11-11 16:08:03 -05003606 if (!this->createCopyProgram(srcTex)) {
cdaltone2e71c22016-04-07 18:13:29 -07003607 SkDebugf("Failed to create copy program.\n");
3608 return false;
3609 }
3610 }
3611
bsalomon6df86402015-06-01 10:41:49 -07003612 int w = srcRect.width();
3613 int h = srcRect.height();
3614
Brian Salomon930f9392018-06-20 16:25:26 -04003615 this->bindTexture(0, GrSamplerState::ClampNearest(), srcTex);
bsalomon6df86402015-06-01 10:41:49 -07003616
bsalomon083617b2016-02-12 12:10:14 -08003617 GrGLIRect dstVP;
Brian Salomon71d9d842016-11-03 13:42:00 -04003618 this->bindSurfaceFBOForPixelOps(dst, GR_GL_FRAMEBUFFER, &dstVP, kDst_TempFBOTarget);
bsalomon083617b2016-02-12 12:10:14 -08003619 this->flushViewport(dstVP);
Robert Phillips294870f2016-11-11 12:38:40 -05003620 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon083617b2016-02-12 12:10:14 -08003621
bsalomon6df86402015-06-01 10:41:49 -07003622 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY, w, h);
bsalomon6df86402015-06-01 10:41:49 -07003623
Brian Salomon802cb312018-06-08 18:05:20 -04003624 this->flushProgram(fCopyPrograms[progIdx].fProgram);
bsalomon6df86402015-06-01 10:41:49 -07003625
cdaltone2e71c22016-04-07 18:13:29 -07003626 fHWVertexArrayState.setVertexArrayID(this, 0);
bsalomon6df86402015-06-01 10:41:49 -07003627
cdaltone2e71c22016-04-07 18:13:29 -07003628 GrGLAttribArrayState* attribs = fHWVertexArrayState.bindInternalVertexArray(this);
Chris Dalton8e45b4f2017-05-05 14:00:56 -04003629 attribs->enableVertexArrays(this, 1);
Brian Osmand4c29702018-09-14 16:16:55 -04003630 attribs->set(this, 0, fCopyProgramArrayBuffer.get(), kFloat2_GrVertexAttribType,
Brian Osman4a3f5c82018-09-18 16:16:38 -04003631 kFloat2_GrSLType, 2 * sizeof(GrGLfloat), 0);
bsalomon6df86402015-06-01 10:41:49 -07003632
3633 // dst rect edges in NDC (-1 to 1)
3634 int dw = dst->width();
3635 int dh = dst->height();
3636 GrGLfloat dx0 = 2.f * dstPoint.fX / dw - 1.f;
3637 GrGLfloat dx1 = 2.f * (dstPoint.fX + w) / dw - 1.f;
3638 GrGLfloat dy0 = 2.f * dstPoint.fY / dh - 1.f;
3639 GrGLfloat dy1 = 2.f * (dstPoint.fY + h) / dh - 1.f;
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003640 if (kBottomLeft_GrSurfaceOrigin == dstOrigin) {
bsalomon6df86402015-06-01 10:41:49 -07003641 dy0 = -dy0;
3642 dy1 = -dy1;
3643 }
3644
bsalomone5286e02016-01-14 09:24:09 -08003645 GrGLfloat sx0 = (GrGLfloat)srcRect.fLeft;
3646 GrGLfloat sx1 = (GrGLfloat)(srcRect.fLeft + w);
3647 GrGLfloat sy0 = (GrGLfloat)srcRect.fTop;
3648 GrGLfloat sy1 = (GrGLfloat)(srcRect.fTop + h);
Ethan Nicholas5338f992017-04-19 15:54:07 -04003649 int sw = src->width();
bsalomon6df86402015-06-01 10:41:49 -07003650 int sh = src->height();
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003651 if (kBottomLeft_GrSurfaceOrigin == srcOrigin) {
bsalomone5286e02016-01-14 09:24:09 -08003652 sy0 = sh - sy0;
3653 sy1 = sh - sy1;
3654 }
Brian Salomon246bc3d2018-12-06 15:33:02 -05003655 if (srcTex->texturePriv().textureType() != GrTextureType::kRectangle) {
3656 // src rect edges in normalized texture space (0 to 1)
3657 sx0 /= sw;
3658 sx1 /= sw;
3659 sy0 /= sh;
3660 sy1 /= sh;
3661 }
bsalomon6df86402015-06-01 10:41:49 -07003662
bsalomon7ea33f52015-11-22 14:51:00 -08003663 GL_CALL(Uniform4f(fCopyPrograms[progIdx].fPosXformUniform, dx1 - dx0, dy1 - dy0, dx0, dy0));
3664 GL_CALL(Uniform4f(fCopyPrograms[progIdx].fTexCoordXformUniform,
3665 sx1 - sx0, sy1 - sy0, sx0, sy0));
3666 GL_CALL(Uniform1i(fCopyPrograms[progIdx].fTextureUniform, 0));
bsalomon6df86402015-06-01 10:41:49 -07003667
3668 GrXferProcessor::BlendInfo blendInfo;
3669 blendInfo.reset();
bsalomon7f9b2e42016-01-12 13:29:26 -08003670 this->flushBlend(blendInfo, GrSwizzle::RGBA());
bsalomon6df86402015-06-01 10:41:49 -07003671 this->flushColorWrite(true);
bsalomon083617b2016-02-12 12:10:14 -08003672 this->flushHWAAState(nullptr, false, false);
Brian Salomond818ebf2018-07-02 14:08:49 +00003673 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07003674 this->disableWindowRectangles();
csmartdaltonc7d85332016-10-26 10:13:46 -07003675 this->disableStencil();
Brian Osmanf02fa6f2017-07-11 11:17:47 -04003676 if (this->glCaps().srgbWriteControl()) {
3677 this->flushFramebufferSRGB(true);
3678 }
bsalomon6df86402015-06-01 10:41:49 -07003679
3680 GL_CALL(DrawArrays(GR_GL_TRIANGLE_STRIP, 0, 4));
Brian Salomon71d9d842016-11-03 13:42:00 -04003681 this->unbindTextureFBOForPixelOps(GR_GL_FRAMEBUFFER, dst);
Brian Salomon1fabd512018-02-09 09:54:25 -05003682 this->didWriteToSurface(dst, dstOrigin, &dstRect);
bsalomon083617b2016-02-12 12:10:14 -08003683
cdaltone2e71c22016-04-07 18:13:29 -07003684 return true;
bsalomon6df86402015-06-01 10:41:49 -07003685}
3686
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003687void GrGLGpu::copySurfaceAsCopyTexSubImage(GrSurface* dst, GrSurfaceOrigin dstOrigin,
3688 GrSurface* src, GrSurfaceOrigin srcOrigin,
bsalomon6df86402015-06-01 10:41:49 -07003689 const SkIRect& srcRect,
3690 const SkIPoint& dstPoint) {
Greg Daniel26dbe3b2018-05-03 10:35:42 -04003691 SkASSERT(can_copy_texsubimage(dst, dstOrigin, src, srcOrigin, this->glCaps()));
bsalomon6df86402015-06-01 10:41:49 -07003692 GrGLIRect srcVP;
Brian Salomon71d9d842016-11-03 13:42:00 -04003693 this->bindSurfaceFBOForPixelOps(src, GR_GL_FRAMEBUFFER, &srcVP, kSrc_TempFBOTarget);
bsalomon083617b2016-02-12 12:10:14 -08003694 GrGLTexture* dstTex = static_cast<GrGLTexture *>(dst->asTexture());
bsalomon6df86402015-06-01 10:41:49 -07003695 SkASSERT(dstTex);
3696 // We modified the bound FBO
Robert Phillips294870f2016-11-11 12:38:40 -05003697 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon6df86402015-06-01 10:41:49 -07003698 GrGLIRect srcGLRect;
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003699 srcGLRect.setRelativeTo(srcVP, srcRect, srcOrigin);
bsalomon6df86402015-06-01 10:41:49 -07003700
3701 this->setScratchTextureUnit();
bsalomon10528f12015-10-14 12:54:52 -07003702 GL_CALL(BindTexture(dstTex->target(), dstTex->textureID()));
bsalomon6df86402015-06-01 10:41:49 -07003703 GrGLint dstY;
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003704 if (kBottomLeft_GrSurfaceOrigin == dstOrigin) {
bsalomon6df86402015-06-01 10:41:49 -07003705 dstY = dst->height() - (dstPoint.fY + srcGLRect.fHeight);
3706 } else {
3707 dstY = dstPoint.fY;
3708 }
bsalomon10528f12015-10-14 12:54:52 -07003709 GL_CALL(CopyTexSubImage2D(dstTex->target(), 0,
bsalomon083617b2016-02-12 12:10:14 -08003710 dstPoint.fX, dstY,
3711 srcGLRect.fLeft, srcGLRect.fBottom,
3712 srcGLRect.fWidth, srcGLRect.fHeight));
Brian Salomon71d9d842016-11-03 13:42:00 -04003713 this->unbindTextureFBOForPixelOps(GR_GL_FRAMEBUFFER, src);
bsalomon083617b2016-02-12 12:10:14 -08003714 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
3715 srcRect.width(), srcRect.height());
Brian Salomon1fabd512018-02-09 09:54:25 -05003716 this->didWriteToSurface(dst, dstOrigin, &dstRect);
bsalomon6df86402015-06-01 10:41:49 -07003717}
3718
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003719bool GrGLGpu::copySurfaceAsBlitFramebuffer(GrSurface* dst, GrSurfaceOrigin dstOrigin,
3720 GrSurface* src, GrSurfaceOrigin srcOrigin,
bsalomon6df86402015-06-01 10:41:49 -07003721 const SkIRect& srcRect,
3722 const SkIPoint& dstPoint) {
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003723 SkASSERT(can_blit_framebuffer_for_copy_surface(dst, dstOrigin, src, srcOrigin,
Greg Daniel26dbe3b2018-05-03 10:35:42 -04003724 srcRect, dstPoint, this->glCaps()));
bsalomon6df86402015-06-01 10:41:49 -07003725 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
3726 srcRect.width(), srcRect.height());
3727 if (dst == src) {
3728 if (SkIRect::IntersectsNoEmptyCheck(dstRect, srcRect)) {
3729 return false;
mtklein404b3b22015-05-18 09:29:10 -07003730 }
bsalomon5df6fee2015-05-18 06:26:15 -07003731 }
bsalomon6df86402015-06-01 10:41:49 -07003732
bsalomon6df86402015-06-01 10:41:49 -07003733 GrGLIRect dstVP;
3734 GrGLIRect srcVP;
Brian Salomon71d9d842016-11-03 13:42:00 -04003735 this->bindSurfaceFBOForPixelOps(dst, GR_GL_DRAW_FRAMEBUFFER, &dstVP, kDst_TempFBOTarget);
3736 this->bindSurfaceFBOForPixelOps(src, GR_GL_READ_FRAMEBUFFER, &srcVP, kSrc_TempFBOTarget);
bsalomon6df86402015-06-01 10:41:49 -07003737 // We modified the bound FBO
Robert Phillips294870f2016-11-11 12:38:40 -05003738 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon6df86402015-06-01 10:41:49 -07003739 GrGLIRect srcGLRect;
3740 GrGLIRect dstGLRect;
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003741 srcGLRect.setRelativeTo(srcVP, srcRect, srcOrigin);
3742 dstGLRect.setRelativeTo(dstVP, dstRect, dstOrigin);
bsalomon6df86402015-06-01 10:41:49 -07003743
3744 // BlitFrameBuffer respects the scissor, so disable it.
Brian Salomond818ebf2018-07-02 14:08:49 +00003745 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07003746 this->disableWindowRectangles();
bsalomon6df86402015-06-01 10:41:49 -07003747
3748 GrGLint srcY0;
3749 GrGLint srcY1;
3750 // Does the blit need to y-mirror or not?
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003751 if (srcOrigin == dstOrigin) {
bsalomon6df86402015-06-01 10:41:49 -07003752 srcY0 = srcGLRect.fBottom;
3753 srcY1 = srcGLRect.fBottom + srcGLRect.fHeight;
3754 } else {
3755 srcY0 = srcGLRect.fBottom + srcGLRect.fHeight;
3756 srcY1 = srcGLRect.fBottom;
3757 }
3758 GL_CALL(BlitFramebuffer(srcGLRect.fLeft,
3759 srcY0,
3760 srcGLRect.fLeft + srcGLRect.fWidth,
3761 srcY1,
3762 dstGLRect.fLeft,
3763 dstGLRect.fBottom,
3764 dstGLRect.fLeft + dstGLRect.fWidth,
3765 dstGLRect.fBottom + dstGLRect.fHeight,
3766 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
Brian Salomon71d9d842016-11-03 13:42:00 -04003767 this->unbindTextureFBOForPixelOps(GR_GL_DRAW_FRAMEBUFFER, dst);
3768 this->unbindTextureFBOForPixelOps(GR_GL_READ_FRAMEBUFFER, src);
Brian Salomon1fabd512018-02-09 09:54:25 -05003769 this->didWriteToSurface(dst, dstOrigin, &dstRect);
bsalomon6df86402015-06-01 10:41:49 -07003770 return true;
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00003771}
3772
Brian Salomon930f9392018-06-20 16:25:26 -04003773bool GrGLGpu::onRegenerateMipMapLevels(GrTexture* texture) {
3774 auto glTex = static_cast<GrGLTexture*>(texture);
brianosman33f6b3f2016-06-02 05:49:21 -07003775 // Mipmaps are only supported on 2D textures:
Brian Salomon930f9392018-06-20 16:25:26 -04003776 if (GR_GL_TEXTURE_2D != glTex->target()) {
brianosman33f6b3f2016-06-02 05:49:21 -07003777 return false;
3778 }
3779
Brian Salomon930f9392018-06-20 16:25:26 -04003780 // Manual implementation of mipmap generation, to work around driver bugs w/sRGB.
3781 // Uses draw calls to do a series of downsample operations to successive mips.
3782
3783 // The manual approach requires the ability to limit which level we're sampling and that the
3784 // destination can be bound to a FBO:
3785 if (!this->glCaps().doManualMipmapping() ||
3786 !this->glCaps().canConfigBeFBOColorAttachment(texture->config())) {
3787 GrGLenum target = glTex->target();
3788 this->setScratchTextureUnit();
3789 GL_CALL(BindTexture(target, glTex->textureID()));
3790 GL_CALL(GenerateMipmap(glTex->target()));
3791 return true;
brianosman33f6b3f2016-06-02 05:49:21 -07003792 }
3793
brianosman33f6b3f2016-06-02 05:49:21 -07003794 int width = texture->width();
3795 int height = texture->height();
3796 int levelCount = SkMipMap::ComputeLevelCount(width, height) + 1;
Greg Danielda86e282018-06-13 09:41:19 -04003797 SkASSERT(levelCount == texture->texturePriv().maxMipMapLevel() + 1);
brianosman33f6b3f2016-06-02 05:49:21 -07003798
3799 // Create (if necessary), then bind temporary FBO:
3800 if (0 == fTempDstFBOID) {
3801 GL_CALL(GenFramebuffers(1, &fTempDstFBOID));
3802 }
Adrienne Walker4ee88512018-05-17 11:37:14 -07003803 this->bindFramebuffer(GR_GL_FRAMEBUFFER, fTempDstFBOID);
Robert Phillips294870f2016-11-11 12:38:40 -05003804 fHWBoundRenderTargetUniqueID.makeInvalid();
brianosman33f6b3f2016-06-02 05:49:21 -07003805
3806 // Bind the texture, to get things configured for filtering.
3807 // We'll be changing our base level further below:
3808 this->setTextureUnit(0);
Brian Salomon930f9392018-06-20 16:25:26 -04003809 this->bindTexture(0, GrSamplerState::ClampBilerp(), glTex);
brianosman33f6b3f2016-06-02 05:49:21 -07003810
3811 // Vertex data:
3812 if (!fMipmapProgramArrayBuffer) {
3813 static const GrGLfloat vdata[] = {
3814 0, 0,
3815 0, 1,
3816 1, 0,
3817 1, 1
3818 };
3819 fMipmapProgramArrayBuffer.reset(GrGLBuffer::Create(this, sizeof(vdata),
3820 kVertex_GrBufferType,
3821 kStatic_GrAccessPattern, vdata));
3822 }
3823 if (!fMipmapProgramArrayBuffer) {
3824 return false;
3825 }
3826
3827 fHWVertexArrayState.setVertexArrayID(this, 0);
3828
3829 GrGLAttribArrayState* attribs = fHWVertexArrayState.bindInternalVertexArray(this);
Chris Dalton8e45b4f2017-05-05 14:00:56 -04003830 attribs->enableVertexArrays(this, 1);
Brian Osmand4c29702018-09-14 16:16:55 -04003831 attribs->set(this, 0, fMipmapProgramArrayBuffer.get(), kFloat2_GrVertexAttribType,
Brian Osman4a3f5c82018-09-18 16:16:38 -04003832 kFloat2_GrSLType, 2 * sizeof(GrGLfloat), 0);
brianosman33f6b3f2016-06-02 05:49:21 -07003833
3834 // Set "simple" state once:
3835 GrXferProcessor::BlendInfo blendInfo;
3836 blendInfo.reset();
3837 this->flushBlend(blendInfo, GrSwizzle::RGBA());
3838 this->flushColorWrite(true);
brianosman33f6b3f2016-06-02 05:49:21 -07003839 this->flushHWAAState(nullptr, false, false);
Brian Salomond818ebf2018-07-02 14:08:49 +00003840 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07003841 this->disableWindowRectangles();
csmartdaltonc7d85332016-10-26 10:13:46 -07003842 this->disableStencil();
brianosman33f6b3f2016-06-02 05:49:21 -07003843
3844 // Do all the blits:
3845 width = texture->width();
3846 height = texture->height();
3847 GrGLIRect viewport;
3848 viewport.fLeft = 0;
3849 viewport.fBottom = 0;
Brian Salomondc829942018-10-23 16:07:24 -04003850
brianosman33f6b3f2016-06-02 05:49:21 -07003851 for (GrGLint level = 1; level < levelCount; ++level) {
3852 // Get and bind the program for this particular downsample (filter shape can vary):
3853 int progIdx = TextureSizeToMipmapProgramIdx(width, height);
3854 if (!fMipmapPrograms[progIdx].fProgram) {
3855 if (!this->createMipmapProgram(progIdx)) {
3856 SkDebugf("Failed to create mipmap program.\n");
Brian Salomondc829942018-10-23 16:07:24 -04003857 // Invalidate all params to cover base level change in a previous iteration.
3858 glTex->textureParamsModified();
brianosman33f6b3f2016-06-02 05:49:21 -07003859 return false;
3860 }
3861 }
Brian Salomon802cb312018-06-08 18:05:20 -04003862 this->flushProgram(fMipmapPrograms[progIdx].fProgram);
brianosman33f6b3f2016-06-02 05:49:21 -07003863
3864 // Texcoord uniform is expected to contain (1/w, (w-1)/w, 1/h, (h-1)/h)
3865 const float invWidth = 1.0f / width;
3866 const float invHeight = 1.0f / height;
3867 GL_CALL(Uniform4f(fMipmapPrograms[progIdx].fTexCoordXformUniform,
3868 invWidth, (width - 1) * invWidth, invHeight, (height - 1) * invHeight));
3869 GL_CALL(Uniform1i(fMipmapPrograms[progIdx].fTextureUniform, 0));
3870
3871 // Only sample from previous mip
3872 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_BASE_LEVEL, level - 1));
3873
Brian Salomon930f9392018-06-20 16:25:26 -04003874 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0, GR_GL_TEXTURE_2D,
3875 glTex->textureID(), level));
brianosman33f6b3f2016-06-02 05:49:21 -07003876
3877 width = SkTMax(1, width / 2);
3878 height = SkTMax(1, height / 2);
3879 viewport.fWidth = width;
3880 viewport.fHeight = height;
3881 this->flushViewport(viewport);
3882
3883 GL_CALL(DrawArrays(GR_GL_TRIANGLE_STRIP, 0, 4));
3884 }
3885
3886 // Unbind:
3887 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
3888 GR_GL_TEXTURE_2D, 0, 0));
3889
Brian Salomon930f9392018-06-20 16:25:26 -04003890 // We modified the base level param.
Brian Salomondc829942018-10-23 16:07:24 -04003891 GrGLTexture::NonSamplerParams params = glTex->getCachedNonSamplerParams();
3892 params.fBaseMipMapLevel = levelCount - 2; // we drew the 2nd to last level into the last level.
3893 glTex->setCachedParams(nullptr, params, this->getResetTimestamp());
3894
brianosman33f6b3f2016-06-02 05:49:21 -07003895 return true;
3896}
3897
cdalton231c5fd2015-05-13 12:35:36 -07003898void GrGLGpu::xferBarrier(GrRenderTarget* rt, GrXferBarrierType type) {
bsalomoncb02b382015-08-12 11:14:50 -07003899 SkASSERT(type);
cdalton9954bc32015-04-29 14:17:00 -07003900 switch (type) {
cdalton231c5fd2015-05-13 12:35:36 -07003901 case kTexture_GrXferBarrierType: {
3902 GrGLRenderTarget* glrt = static_cast<GrGLRenderTarget*>(rt);
Brian Osmancfe83d12018-01-19 11:13:45 -05003903 SkASSERT(glrt->textureFBOID() != 0 && glrt->renderFBOID() != 0);
cdalton231c5fd2015-05-13 12:35:36 -07003904 if (glrt->textureFBOID() != glrt->renderFBOID()) {
3905 // The render target uses separate storage so no need for glTextureBarrier.
3906 // FIXME: The render target will resolve automatically when its texture is bound,
3907 // but we could resolve only the bounds that will be read if we do it here instead.
3908 return;
3909 }
cdalton9954bc32015-04-29 14:17:00 -07003910 SkASSERT(this->caps()->textureBarrierSupport());
3911 GL_CALL(TextureBarrier());
3912 return;
cdalton231c5fd2015-05-13 12:35:36 -07003913 }
cdalton8917d622015-05-06 13:40:21 -07003914 case kBlend_GrXferBarrierType:
bsalomon4b91f762015-05-19 09:29:46 -07003915 SkASSERT(GrCaps::kAdvanced_BlendEquationSupport ==
cdalton8917d622015-05-06 13:40:21 -07003916 this->caps()->blendEquationSupport());
3917 GL_CALL(BlendBarrier());
3918 return;
bsalomoncb02b382015-08-12 11:14:50 -07003919 default: break; // placate compiler warnings that kNone not handled
cdalton9954bc32015-04-29 14:17:00 -07003920 }
3921}
3922
Brian Salomonf865b052018-03-09 09:01:53 -05003923#if GR_TEST_UTILS
Brian Salomon52e943a2018-03-13 09:32:39 -04003924GrBackendTexture GrGLGpu::createTestingOnlyBackendTexture(const void* pixels, int w, int h,
Robert Phillips646f6372018-09-25 09:31:10 -04003925 GrColorType colorType, bool /*isRT*/,
3926 GrMipMapped mipMapped,
3927 size_t rowBytes) {
Brian Salomon8a375832018-03-14 10:21:40 -04003928 this->handleDirtyContext();
Robert Phillips646f6372018-09-25 09:31:10 -04003929
3930 GrPixelConfig config = GrColorTypeToPixelConfig(colorType, GrSRGBEncoded::kNo);
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003931 if (!this->caps()->isConfigTexturable(config)) {
3932 return GrBackendTexture(); // invalid
3933 }
3934
Greg Daniel92cbf3f2018-04-12 16:50:17 -04003935 if (w > this->caps()->maxTextureSize() || h > this->caps()->maxTextureSize()) {
3936 return GrBackendTexture(); // invalid
3937 }
3938
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003939 // Currently we don't support uploading pixel data when mipped.
3940 if (pixels && GrMipMapped::kYes == mipMapped) {
3941 return GrBackendTexture(); // invalid
3942 }
3943
Robert Phillips646f6372018-09-25 09:31:10 -04003944 int bpp = GrColorTypeBytesPerPixel(colorType);
3945 const size_t trimRowBytes = w * bpp;
3946 if (!rowBytes) {
3947 rowBytes = trimRowBytes;
3948 }
3949
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003950 GrGLTextureInfo info;
3951 info.fTarget = GR_GL_TEXTURE_2D;
3952 info.fID = 0;
3953 GL_CALL(GenTextures(1, &info.fID));
3954 GL_CALL(ActiveTexture(GR_GL_TEXTURE0));
3955 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, 1));
3956 GL_CALL(BindTexture(info.fTarget, info.fID));
3957 fHWBoundTextureUniqueIDs[0].makeInvalid();
3958 GL_CALL(TexParameteri(info.fTarget, GR_GL_TEXTURE_MAG_FILTER, GR_GL_NEAREST));
3959 GL_CALL(TexParameteri(info.fTarget, GR_GL_TEXTURE_MIN_FILTER, GR_GL_NEAREST));
3960 GL_CALL(TexParameteri(info.fTarget, GR_GL_TEXTURE_WRAP_S, GR_GL_CLAMP_TO_EDGE));
3961 GL_CALL(TexParameteri(info.fTarget, GR_GL_TEXTURE_WRAP_T, GR_GL_CLAMP_TO_EDGE));
3962
Jim Van Verth1676cb92019-01-15 13:24:45 -05003963 // we have to do something special for compressed textures
3964 if (GrPixelConfigIsCompressed(config)) {
3965 GrGLenum internalFormat;
3966 const GrGLInterface* interface = this->glInterface();
3967 const GrGLCaps& caps = this->glCaps();
3968 if (!caps.getCompressedTexImageFormats(config, &internalFormat)) {
3969 return GrBackendTexture();
Robert Phillips646f6372018-09-25 09:31:10 -04003970 }
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003971
Jim Van Verth1676cb92019-01-15 13:24:45 -05003972 GrMipLevel mipLevel = { pixels, rowBytes };
3973 if (!allocate_and_populate_compressed_texture(config, *interface, caps, info.fTarget,
3974 internalFormat, &mipLevel, 1,
3975 w, h)) {
3976 return GrBackendTexture();
3977 }
3978 } else {
3979 bool restoreGLRowLength = false;
3980 if (trimRowBytes != rowBytes && this->glCaps().unpackRowLengthSupport()) {
3981 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowBytes / bpp));
3982 restoreGLRowLength = true;
3983 }
3984
3985 GrGLenum internalFormat;
3986 GrGLenum externalFormat;
3987 GrGLenum externalType;
3988
3989 if (!this->glCaps().getTexImageFormats(config, config, &internalFormat, &externalFormat,
3990 &externalType)) {
3991 return GrBackendTexture(); // invalid
3992 }
3993
3994 info.fFormat = this->glCaps().configSizedInternalFormat(config);
3995
3996 this->unbindCpuToGpuXferBuffer();
3997
3998 // Figure out the number of mip levels.
3999 int mipLevels = 1;
4000 if (GrMipMapped::kYes == mipMapped) {
4001 mipLevels = SkMipMap::ComputeLevelCount(w, h) + 1;
4002 }
4003
4004 size_t baseLayerSize = bpp * w * h;
4005 SkAutoMalloc defaultStorage(baseLayerSize);
4006 if (!pixels) {
4007 // Fill in the texture with all zeros so we don't have random garbage
4008 pixels = defaultStorage.get();
4009 memset(defaultStorage.get(), 0, baseLayerSize);
4010 } else if (trimRowBytes != rowBytes && !restoreGLRowLength) {
4011 // We weren't able to use GR_GL_UNPACK_ROW_LENGTH so make a copy
4012 char* copy = (char*)defaultStorage.get();
4013 for (int y = 0; y < h; ++y) {
4014 memcpy(&copy[y*trimRowBytes], &((const char*)pixels)[y*rowBytes], trimRowBytes);
4015 }
4016 pixels = copy;
4017 }
4018
4019 int width = w;
4020 int height = h;
4021 for (int i = 0; i < mipLevels; ++i) {
4022 GL_CALL(TexImage2D(info.fTarget, i, internalFormat, width, height, 0, externalFormat,
4023 externalType, pixels));
4024 width = SkTMax(1, width / 2);
4025 height = SkTMax(1, height / 2);
4026 }
4027 if (restoreGLRowLength) {
4028 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
4029 }
Robert Phillipsd21b2a52017-12-12 13:01:25 -05004030 }
4031
Robert Phillipse8fabb22018-02-04 14:33:21 -05004032 // unbind the texture from the texture unit to avoid asserts
4033 GL_CALL(BindTexture(info.fTarget, 0));
4034
Greg Daniel108bb232018-07-03 16:18:29 -04004035 GrBackendTexture beTex = GrBackendTexture(w, h, mipMapped, info);
4036 // Lots of tests don't go through Skia's public interface which will set the config so for
4037 // testing we make sure we set a config here.
4038 beTex.setPixelConfig(config);
4039 return beTex;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05004040}
4041
4042bool GrGLGpu::isTestingOnlyBackendTexture(const GrBackendTexture& tex) const {
Greg Danielbdf12ad2018-10-12 09:31:11 -04004043 SkASSERT(GrBackendApi::kOpenGL == tex.backend());
Robert Phillipsd21b2a52017-12-12 13:01:25 -05004044
Greg Daniel52e16d92018-04-10 09:34:07 -04004045 GrGLTextureInfo info;
4046 if (!tex.getGLTextureInfo(&info)) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -05004047 return false;
4048 }
4049
4050 GrGLboolean result;
Greg Daniel52e16d92018-04-10 09:34:07 -04004051 GL_CALL_RET(result, IsTexture(info.fID));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05004052
4053 return (GR_GL_TRUE == result);
4054}
4055
Brian Salomon26102cb2018-03-09 09:33:19 -05004056void GrGLGpu::deleteTestingOnlyBackendTexture(const GrBackendTexture& tex) {
Greg Danielbdf12ad2018-10-12 09:31:11 -04004057 SkASSERT(GrBackendApi::kOpenGL == tex.backend());
Robert Phillipsd21b2a52017-12-12 13:01:25 -05004058
Greg Daniel52e16d92018-04-10 09:34:07 -04004059 GrGLTextureInfo info;
4060 if (tex.getGLTextureInfo(&info)) {
4061 GL_CALL(DeleteTextures(1, &info.fID));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05004062 }
4063}
4064
Brian Salomonf865b052018-03-09 09:01:53 -05004065GrBackendRenderTarget GrGLGpu::createTestingOnlyBackendRenderTarget(int w, int h,
Brian Osman2d010b62018-08-09 10:55:09 -04004066 GrColorType colorType) {
Greg Daniel92cbf3f2018-04-12 16:50:17 -04004067 if (w > this->caps()->maxRenderTargetSize() || h > this->caps()->maxRenderTargetSize()) {
4068 return GrBackendRenderTarget(); // invalid
4069 }
Brian Salomon8a375832018-03-14 10:21:40 -04004070 this->handleDirtyContext();
Brian Osman2d010b62018-08-09 10:55:09 -04004071 auto config = GrColorTypeToPixelConfig(colorType, GrSRGBEncoded::kNo);
Brian Salomon93348dd2018-08-29 12:56:23 -04004072 if (!this->glCaps().isConfigRenderable(config)) {
Brian Salomonf865b052018-03-09 09:01:53 -05004073 return {};
4074 }
Brian Salomon93348dd2018-08-29 12:56:23 -04004075 bool useTexture = false;
4076 GrGLenum colorBufferFormat;
4077 GrGLenum externalFormat = 0, externalType = 0;
4078 if (config == kBGRA_8888_GrPixelConfig && this->glCaps().bgraIsInternalFormat()) {
4079 // BGRA render buffers are not supported.
4080 this->glCaps().getTexImageFormats(config, config, &colorBufferFormat, &externalFormat,
4081 &externalType);
4082 useTexture = true;
4083 } else {
4084 this->glCaps().getRenderbufferFormat(config, &colorBufferFormat);
4085 }
Brian Salomonf865b052018-03-09 09:01:53 -05004086 int sFormatIdx = this->getCompatibleStencilIndex(config);
4087 if (sFormatIdx < 0) {
4088 return {};
4089 }
Brian Salomon93348dd2018-08-29 12:56:23 -04004090 GrGLuint colorID = 0;
4091 GrGLuint stencilID = 0;
4092 auto deleteIDs = [&] {
4093 if (colorID) {
4094 if (useTexture) {
4095 GL_CALL(DeleteTextures(1, &colorID));
4096 } else {
4097 GL_CALL(DeleteRenderbuffers(1, &colorID));
4098 }
Brian Salomonf865b052018-03-09 09:01:53 -05004099 }
Brian Salomon93348dd2018-08-29 12:56:23 -04004100 if (stencilID) {
4101 GL_CALL(DeleteRenderbuffers(1, &stencilID));
Brian Salomonf865b052018-03-09 09:01:53 -05004102 }
Brian Salomon93348dd2018-08-29 12:56:23 -04004103 };
4104
4105 if (useTexture) {
4106 GL_CALL(GenTextures(1, &colorID));
4107 } else {
4108 GL_CALL(GenRenderbuffers(1, &colorID));
4109 }
4110 GL_CALL(GenRenderbuffers(1, &stencilID));
4111 if (!stencilID || !colorID) {
4112 deleteIDs();
Brian Salomonf865b052018-03-09 09:01:53 -05004113 return {};
4114 }
Brian Salomon93348dd2018-08-29 12:56:23 -04004115
Brian Salomonf865b052018-03-09 09:01:53 -05004116 GrGLFramebufferInfo info;
4117 info.fFBOID = 0;
Brian Salomon93348dd2018-08-29 12:56:23 -04004118 this->glCaps().getSizedInternalFormat(config, &info.fFormat);
Brian Salomonf865b052018-03-09 09:01:53 -05004119 GL_CALL(GenFramebuffers(1, &info.fFBOID));
4120 if (!info.fFBOID) {
Brian Salomon93348dd2018-08-29 12:56:23 -04004121 deleteIDs();
4122 return {};
Brian Salomonf865b052018-03-09 09:01:53 -05004123 }
4124
4125 this->invalidateBoundRenderTarget();
4126
Adrienne Walker4ee88512018-05-17 11:37:14 -07004127 this->bindFramebuffer(GR_GL_FRAMEBUFFER, info.fFBOID);
Brian Salomon93348dd2018-08-29 12:56:23 -04004128 if (useTexture) {
4129 this->setScratchTextureUnit();
4130 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, colorID));
4131 GL_CALL(TexImage2D(GR_GL_TEXTURE_2D, 0, colorBufferFormat, w, h, 0, externalFormat,
4132 externalType, nullptr));
4133 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0, GR_GL_TEXTURE_2D,
4134 colorID, 0));
4135 } else {
4136 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, colorID));
4137 GL_ALLOC_CALL(this->glInterface(),
4138 RenderbufferStorage(GR_GL_RENDERBUFFER, colorBufferFormat, w, h));
4139 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
4140 GR_GL_RENDERBUFFER, colorID));
4141 }
4142 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, stencilID));
Brian Salomonf865b052018-03-09 09:01:53 -05004143 auto stencilBufferFormat = this->glCaps().stencilFormats()[sFormatIdx].fInternalFormat;
4144 GL_ALLOC_CALL(this->glInterface(),
4145 RenderbufferStorage(GR_GL_RENDERBUFFER, stencilBufferFormat, w, h));
4146 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT, GR_GL_RENDERBUFFER,
Brian Salomon93348dd2018-08-29 12:56:23 -04004147 stencilID));
Brian Salomonf865b052018-03-09 09:01:53 -05004148 if (this->glCaps().stencilFormats()[sFormatIdx].fPacked) {
4149 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_DEPTH_ATTACHMENT,
Brian Salomon93348dd2018-08-29 12:56:23 -04004150 GR_GL_RENDERBUFFER, stencilID));
Brian Salomonf865b052018-03-09 09:01:53 -05004151 }
4152
Brian Salomon93348dd2018-08-29 12:56:23 -04004153 // We don't want to have to recover the renderbuffer/texture IDs later to delete them. OpenGL
4154 // has this rule that if a renderbuffer/texture is deleted and a FBO other than the current FBO
4155 // has the RB attached then deletion is delayed. So we unbind the FBO here and delete the
4156 // renderbuffers/texture.
Adrienne Walker4ee88512018-05-17 11:37:14 -07004157 this->bindFramebuffer(GR_GL_FRAMEBUFFER, 0);
Brian Salomon93348dd2018-08-29 12:56:23 -04004158 deleteIDs();
Brian Salomonf865b052018-03-09 09:01:53 -05004159
Adrienne Walker4ee88512018-05-17 11:37:14 -07004160 this->bindFramebuffer(GR_GL_FRAMEBUFFER, info.fFBOID);
Brian Salomonf865b052018-03-09 09:01:53 -05004161 GrGLenum status;
4162 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
4163 if (GR_GL_FRAMEBUFFER_COMPLETE != status) {
Adrienne Walker4ee88512018-05-17 11:37:14 -07004164 this->deleteFramebuffer(info.fFBOID);
Brian Salomonf865b052018-03-09 09:01:53 -05004165 return {};
4166 }
4167 auto stencilBits = SkToInt(this->glCaps().stencilFormats()[sFormatIdx].fStencilBits);
Greg Daniel108bb232018-07-03 16:18:29 -04004168 GrBackendRenderTarget beRT = GrBackendRenderTarget(w, h, 1, stencilBits, info);
4169 // Lots of tests don't go through Skia's public interface which will set the config so for
4170 // testing we make sure we set a config here.
4171 beRT.setPixelConfig(config);
Brian Salomon93348dd2018-08-29 12:56:23 -04004172#ifdef SK_DEBUG
4173 SkColorType skColorType = GrColorTypeToSkColorType(colorType);
4174 if (skColorType != kUnknown_SkColorType) {
4175 SkASSERT(this->caps()->validateBackendRenderTarget(
Brian Salomonf391d0f2018-12-14 09:18:50 -05004176 beRT, GrColorTypeToSkColorType(colorType)) != kUnknown_GrPixelConfig);
Brian Salomon93348dd2018-08-29 12:56:23 -04004177 }
4178#endif
Greg Daniel108bb232018-07-03 16:18:29 -04004179 return beRT;
Brian Salomonf865b052018-03-09 09:01:53 -05004180}
4181
4182void GrGLGpu::deleteTestingOnlyBackendRenderTarget(const GrBackendRenderTarget& backendRT) {
Greg Danielbdf12ad2018-10-12 09:31:11 -04004183 SkASSERT(GrBackendApi::kOpenGL == backendRT.backend());
Greg Daniel323fbcf2018-04-10 13:46:30 -04004184 GrGLFramebufferInfo info;
4185 if (backendRT.getGLFramebufferInfo(&info)) {
4186 if (info.fFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -07004187 this->deleteFramebuffer(info.fFBOID);
Brian Salomonf865b052018-03-09 09:01:53 -05004188 }
4189 }
joshualitt8fd844f2015-12-02 13:36:47 -08004190}
4191
Greg Daniel26b50a42018-03-08 09:49:58 -05004192void GrGLGpu::testingOnly_flushGpuAndSync() {
4193 GL_CALL(Finish());
4194}
Brian Salomonf865b052018-03-09 09:01:53 -05004195#endif
Greg Daniel26b50a42018-03-08 09:49:58 -05004196
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00004197///////////////////////////////////////////////////////////////////////////////
bsalomon6df86402015-06-01 10:41:49 -07004198
cdaltone2e71c22016-04-07 18:13:29 -07004199GrGLAttribArrayState* GrGLGpu::HWVertexArrayState::bindInternalVertexArray(GrGLGpu* gpu,
csmartdalton485a1202016-07-13 10:16:32 -07004200 const GrBuffer* ibuf) {
robertphillips@google.com4f65a272013-03-26 19:40:46 +00004201 GrGLAttribArrayState* attribState;
4202
cdaltone2e71c22016-04-07 18:13:29 -07004203 if (gpu->glCaps().isCoreProfile()) {
4204 if (!fCoreProfileVertexArray) {
bsalomon@google.com6918d482013-03-07 19:09:11 +00004205 GrGLuint arrayID;
4206 GR_GL_CALL(gpu->glInterface(), GenVertexArrays(1, &arrayID));
4207 int attrCount = gpu->glCaps().maxVertexAttributes();
cdaltone2e71c22016-04-07 18:13:29 -07004208 fCoreProfileVertexArray = new GrGLVertexArray(arrayID, attrCount);
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00004209 }
cdaltone2e71c22016-04-07 18:13:29 -07004210 if (ibuf) {
4211 attribState = fCoreProfileVertexArray->bindWithIndexBuffer(gpu, ibuf);
bsalomon6df86402015-06-01 10:41:49 -07004212 } else {
cdaltone2e71c22016-04-07 18:13:29 -07004213 attribState = fCoreProfileVertexArray->bind(gpu);
bsalomon6df86402015-06-01 10:41:49 -07004214 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00004215 } else {
cdaltone2e71c22016-04-07 18:13:29 -07004216 if (ibuf) {
4217 // bindBuffer implicitly binds VAO 0 when binding an index buffer.
4218 gpu->bindBuffer(kIndex_GrBufferType, ibuf);
bsalomon@google.com6918d482013-03-07 19:09:11 +00004219 } else {
4220 this->setVertexArrayID(gpu, 0);
4221 }
4222 int attrCount = gpu->glCaps().maxVertexAttributes();
4223 if (fDefaultVertexArrayAttribState.count() != attrCount) {
4224 fDefaultVertexArrayAttribState.resize(attrCount);
4225 }
4226 attribState = &fDefaultVertexArrayAttribState;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00004227 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00004228 return attribState;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00004229}
bsalomone179a912016-01-20 06:18:10 -08004230
Greg Daniel51316782017-08-02 15:10:09 +00004231void GrGLGpu::onFinishFlush(bool insertedSemaphore) {
4232 // If we inserted semaphores during the flush, we need to call GLFlush.
4233 if (insertedSemaphore) {
4234 GL_CALL(Flush());
4235 }
4236}
4237
Robert Phillips5b5d84c2018-08-09 15:12:18 -04004238void GrGLGpu::submit(GrGpuCommandBuffer* buffer) {
4239 if (buffer->asRTCommandBuffer()) {
4240 SkASSERT(fCachedRTCommandBuffer.get() == buffer);
4241 fCachedRTCommandBuffer->reset();
4242 } else {
4243 SkASSERT(fCachedTexCommandBuffer.get() == buffer);
4244 fCachedTexCommandBuffer->reset();
4245 }
4246}
4247
Greg Daniel6be35232017-03-01 17:01:09 -05004248GrFence SK_WARN_UNUSED_RESULT GrGLGpu::insertFence() {
Greg Danielc64ee462017-06-15 16:59:49 -04004249 SkASSERT(this->caps()->fenceSyncSupport());
Greg Daniel6be35232017-03-01 17:01:09 -05004250 GrGLsync sync;
4251 GL_CALL_RET(sync, FenceSync(GR_GL_SYNC_GPU_COMMANDS_COMPLETE, 0));
4252 GR_STATIC_ASSERT(sizeof(GrFence) >= sizeof(GrGLsync));
4253 return (GrFence)sync;
jvanverth84741b32016-09-30 08:39:02 -07004254}
4255
Greg Daniel6be35232017-03-01 17:01:09 -05004256bool GrGLGpu::waitFence(GrFence fence, uint64_t timeout) {
jvanverth84741b32016-09-30 08:39:02 -07004257 GrGLenum result;
4258 GL_CALL_RET(result, ClientWaitSync((GrGLsync)fence, GR_GL_SYNC_FLUSH_COMMANDS_BIT, timeout));
4259 return (GR_GL_CONDITION_SATISFIED == result);
4260}
4261
4262void GrGLGpu::deleteFence(GrFence fence) const {
Greg Daniel6be35232017-03-01 17:01:09 -05004263 this->deleteSync((GrGLsync)fence);
4264}
4265
Greg Daniela5cb7812017-06-16 09:45:32 -04004266sk_sp<GrSemaphore> SK_WARN_UNUSED_RESULT GrGLGpu::makeSemaphore(bool isOwned) {
Greg Danielc64ee462017-06-15 16:59:49 -04004267 SkASSERT(this->caps()->fenceSyncSupport());
Greg Daniela5cb7812017-06-16 09:45:32 -04004268 return GrGLSemaphore::Make(this, isOwned);
Greg Daniel6be35232017-03-01 17:01:09 -05004269}
4270
Greg Daniel48661b82018-01-22 16:11:35 -05004271sk_sp<GrSemaphore> GrGLGpu::wrapBackendSemaphore(const GrBackendSemaphore& semaphore,
4272 GrResourceProvider::SemaphoreWrapType wrapType,
4273 GrWrapOwnership ownership) {
Greg Danielc64ee462017-06-15 16:59:49 -04004274 SkASSERT(this->caps()->fenceSyncSupport());
Greg Daniela5cb7812017-06-16 09:45:32 -04004275 return GrGLSemaphore::MakeWrapped(this, semaphore.glSync(), ownership);
4276}
4277
Greg Daniel858e12c2018-12-06 11:11:37 -05004278void GrGLGpu::insertSemaphore(sk_sp<GrSemaphore> semaphore) {
Greg Daniel6be35232017-03-01 17:01:09 -05004279 GrGLSemaphore* glSem = static_cast<GrGLSemaphore*>(semaphore.get());
4280
Greg Daniel48661b82018-01-22 16:11:35 -05004281 GrGLsync sync;
4282 GL_CALL_RET(sync, FenceSync(GR_GL_SYNC_GPU_COMMANDS_COMPLETE, 0));
4283 glSem->setSync(sync);
Greg Daniel6be35232017-03-01 17:01:09 -05004284}
4285
Greg Daniel48661b82018-01-22 16:11:35 -05004286void GrGLGpu::waitSemaphore(sk_sp<GrSemaphore> semaphore) {
Greg Daniel6be35232017-03-01 17:01:09 -05004287 GrGLSemaphore* glSem = static_cast<GrGLSemaphore*>(semaphore.get());
4288
4289 GL_CALL(WaitSync(glSem->sync(), 0, GR_GL_TIMEOUT_IGNORED));
4290}
4291
4292void GrGLGpu::deleteSync(GrGLsync sync) const {
4293 GL_CALL(DeleteSync(sync));
jvanverth84741b32016-09-30 08:39:02 -07004294}
Brian Osman13dddce2017-05-09 13:19:50 -04004295
Robert Phillips65a88fa2017-08-08 08:36:22 -04004296void GrGLGpu::insertEventMarker(const char* msg) {
4297 GL_CALL(InsertEventMarker(strlen(msg), msg));
4298}
4299
Brian Osman13dddce2017-05-09 13:19:50 -04004300sk_sp<GrSemaphore> GrGLGpu::prepareTextureForCrossContextUsage(GrTexture* texture) {
4301 // Set up a semaphore to be signaled once the data is ready, and flush GL
Greg Daniela5cb7812017-06-16 09:45:32 -04004302 sk_sp<GrSemaphore> semaphore = this->makeSemaphore(true);
Greg Daniel858e12c2018-12-06 11:11:37 -05004303 this->insertSemaphore(semaphore);
4304 // We must call flush here to make sure the GrGLSync object gets created and sent to the gpu.
4305 GL_CALL(Flush());
Brian Osman13dddce2017-05-09 13:19:50 -04004306
4307 return semaphore;
4308}
Robert Phillips646e4292017-06-13 12:44:56 -04004309
4310int GrGLGpu::TextureToCopyProgramIdx(GrTexture* texture) {
Brian Salomon60dd8c72018-07-30 10:24:13 -04004311 switch (GrSLCombinedSamplerTypeForTextureType(texture->texturePriv().textureType())) {
Robert Phillips646e4292017-06-13 12:44:56 -04004312 case kTexture2DSampler_GrSLType:
4313 return 0;
Robert Phillips646e4292017-06-13 12:44:56 -04004314 case kTexture2DRectSampler_GrSLType:
Brian Salomon57111332018-02-05 15:55:54 -05004315 return 1;
Robert Phillips646e4292017-06-13 12:44:56 -04004316 case kTextureExternalSampler_GrSLType:
Brian Salomon57111332018-02-05 15:55:54 -05004317 return 2;
Robert Phillips646e4292017-06-13 12:44:56 -04004318 default:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -04004319 SK_ABORT("Unexpected samper type");
Robert Phillips646e4292017-06-13 12:44:56 -04004320 return 0;
4321 }
4322}
Brian Osman71a18892017-08-10 10:23:25 -04004323
Kevin Lubickf4def342018-10-04 12:52:50 -04004324#ifdef SK_ENABLE_DUMP_GPU
4325#include "SkJSONWriter.h"
Brian Osman71a18892017-08-10 10:23:25 -04004326void GrGLGpu::onDumpJSON(SkJSONWriter* writer) const {
4327 // We are called by the base class, which has already called beginObject(). We choose to nest
4328 // all of our caps information in a named sub-object.
4329 writer->beginObject("GL GPU");
4330
4331 const GrGLubyte* str;
4332 GL_CALL_RET(str, GetString(GR_GL_VERSION));
4333 writer->appendString("GL_VERSION", (const char*)(str));
4334 GL_CALL_RET(str, GetString(GR_GL_RENDERER));
4335 writer->appendString("GL_RENDERER", (const char*)(str));
4336 GL_CALL_RET(str, GetString(GR_GL_VENDOR));
4337 writer->appendString("GL_VENDOR", (const char*)(str));
4338 GL_CALL_RET(str, GetString(GR_GL_SHADING_LANGUAGE_VERSION));
4339 writer->appendString("GL_SHADING_LANGUAGE_VERSION", (const char*)(str));
4340
4341 writer->appendName("extensions");
4342 glInterface()->fExtensions.dumpJSON(writer);
4343
4344 writer->endObject();
4345}
Kevin Lubickf4def342018-10-04 12:52:50 -04004346#endif