blob: 506d06aff2000f009c27e11671cfe308442189c5 [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"
Brian Salomondbf70722019-02-07 11:31:24 -050011#include "GrCpuBuffer.h"
Hal Canary95e3c052017-01-11 12:44:43 -050012#include "GrFixedClip.h"
cdalton397536c2016-03-25 12:15:03 -070013#include "GrGLBuffer.h"
egdaniel066df7c2016-06-08 14:02:27 -070014#include "GrGLGpuCommandBuffer.h"
Greg Daniel6be35232017-03-01 17:01:09 -050015#include "GrGLSemaphore.h"
egdaniel8dc7c3a2015-04-16 11:22:42 -070016#include "GrGLStencilAttachment.h"
bsalomon37dd3312014-11-03 08:47:23 -080017#include "GrGLTextureRenderTarget.h"
bsalomon3582d3e2015-02-13 14:20:05 -080018#include "GrGpuResourcePriv.h"
egdaniel0e1853c2016-03-17 11:35:45 -070019#include "GrMesh.h"
Hal Canary95e3c052017-01-11 12:44:43 -050020#include "GrPipeline.h"
bsalomon6bc1b5f2015-02-23 09:06:38 -080021#include "GrRenderTargetPriv.h"
Brian Salomon94efbf52016-11-29 13:43:05 -050022#include "GrShaderCaps.h"
Robert Phillips3798c862017-03-27 11:08:16 -040023#include "GrSurfaceProxyPriv.h"
bsalomonafbf2d62014-09-30 12:18:44 -070024#include "GrTexturePriv.h"
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +000025#include "GrTypes.h"
Hal Canary95e3c052017-01-11 12:44:43 -050026#include "SkAutoMalloc.h"
Brian Salomona6948702018-06-01 15:33:20 -040027#include "SkConvertPixels.h"
Stan Iliev0078ab22017-11-08 14:16:26 -050028#include "SkHalf.h"
Kevin Lubickc456b732017-01-11 17:21:57 +000029#include "SkMakeUnique.h"
30#include "SkMipMap.h"
31#include "SkPixmap.h"
Kevin Lubickc456b732017-01-11 17:21:57 +000032#include "SkSLCompiler.h"
Hal Canary95e3c052017-01-11 12:44:43 -050033#include "SkStrokeRec.h"
Kevin Lubickc456b732017-01-11 17:21:57 +000034#include "SkTemplates.h"
Hal Canaryc640d0d2018-06-13 09:59:02 -040035#include "SkTo.h"
Ryan Macnak38a10ad2017-07-10 10:36:34 -070036#include "SkTraceEvent.h"
Kevin Lubickc456b732017-01-11 17:21:57 +000037#include "SkTypes.h"
Hal Canary95e3c052017-01-11 12:44:43 -050038#include "builders/GrGLShaderStringBuilder.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000039
Hal Canaryc640d0d2018-06-13 09:59:02 -040040#include <cmath>
41
bsalomon@google.com0b77d682011-08-19 13:28:54 +000042#define GL_CALL(X) GR_GL_CALL(this->glInterface(), X)
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +000043#define GL_CALL_RET(RET, X) GR_GL_CALL_RET(this->glInterface(), RET, X)
bsalomon@google.com0b77d682011-08-19 13:28:54 +000044
reed@google.comac10a2d2010-12-22 21:39:39 +000045#define SKIP_CACHE_CHECK true
46
bsalomon@google.com4f3c2532012-01-19 16:16:52 +000047#if GR_GL_CHECK_ALLOC_WITH_GET_ERROR
48 #define CLEAR_ERROR_BEFORE_ALLOC(iface) GrGLClearErr(iface)
49 #define GL_ALLOC_CALL(iface, call) GR_GL_CALL_NOERRCHECK(iface, call)
50 #define CHECK_ALLOC_ERROR(iface) GR_GL_GET_ERROR(iface)
rmistry@google.comfbfcd562012-08-23 18:09:54 +000051#else
bsalomon@google.com4f3c2532012-01-19 16:16:52 +000052 #define CLEAR_ERROR_BEFORE_ALLOC(iface)
53 #define GL_ALLOC_CALL(iface, call) GR_GL_CALL(iface, call)
54 #define CHECK_ALLOC_ERROR(iface) GR_GL_NO_ERROR
55#endif
56
Jim Van Verth32ac83e2016-11-28 15:23:57 -050057//#define USE_NSIGHT
58
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +000059///////////////////////////////////////////////////////////////////////////////
60
cdalton8917d622015-05-06 13:40:21 -070061static const GrGLenum gXfermodeEquation2Blend[] = {
62 // Basic OpenGL blend equations.
63 GR_GL_FUNC_ADD,
64 GR_GL_FUNC_SUBTRACT,
65 GR_GL_FUNC_REVERSE_SUBTRACT,
66
67 // GL_KHR_blend_equation_advanced.
68 GR_GL_SCREEN,
69 GR_GL_OVERLAY,
70 GR_GL_DARKEN,
71 GR_GL_LIGHTEN,
72 GR_GL_COLORDODGE,
73 GR_GL_COLORBURN,
74 GR_GL_HARDLIGHT,
75 GR_GL_SOFTLIGHT,
76 GR_GL_DIFFERENCE,
77 GR_GL_EXCLUSION,
78 GR_GL_MULTIPLY,
79 GR_GL_HSL_HUE,
80 GR_GL_HSL_SATURATION,
81 GR_GL_HSL_COLOR,
Mike Klein36743362018-11-06 08:23:30 -050082 GR_GL_HSL_LUMINOSITY,
83
84 // Illegal... needs to map to something.
85 GR_GL_FUNC_ADD,
cdalton8917d622015-05-06 13:40:21 -070086};
87GR_STATIC_ASSERT(0 == kAdd_GrBlendEquation);
88GR_STATIC_ASSERT(1 == kSubtract_GrBlendEquation);
89GR_STATIC_ASSERT(2 == kReverseSubtract_GrBlendEquation);
90GR_STATIC_ASSERT(3 == kScreen_GrBlendEquation);
91GR_STATIC_ASSERT(4 == kOverlay_GrBlendEquation);
92GR_STATIC_ASSERT(5 == kDarken_GrBlendEquation);
93GR_STATIC_ASSERT(6 == kLighten_GrBlendEquation);
94GR_STATIC_ASSERT(7 == kColorDodge_GrBlendEquation);
95GR_STATIC_ASSERT(8 == kColorBurn_GrBlendEquation);
96GR_STATIC_ASSERT(9 == kHardLight_GrBlendEquation);
97GR_STATIC_ASSERT(10 == kSoftLight_GrBlendEquation);
98GR_STATIC_ASSERT(11 == kDifference_GrBlendEquation);
99GR_STATIC_ASSERT(12 == kExclusion_GrBlendEquation);
100GR_STATIC_ASSERT(13 == kMultiply_GrBlendEquation);
101GR_STATIC_ASSERT(14 == kHSLHue_GrBlendEquation);
102GR_STATIC_ASSERT(15 == kHSLSaturation_GrBlendEquation);
103GR_STATIC_ASSERT(16 == kHSLColor_GrBlendEquation);
104GR_STATIC_ASSERT(17 == kHSLLuminosity_GrBlendEquation);
bsalomonf7cc8772015-05-11 11:21:14 -0700105GR_STATIC_ASSERT(SK_ARRAY_COUNT(gXfermodeEquation2Blend) == kGrBlendEquationCnt);
cdalton8917d622015-05-06 13:40:21 -0700106
twiz@google.com0f31ca72011-03-18 17:38:11 +0000107static const GrGLenum gXfermodeCoeff2Blend[] = {
108 GR_GL_ZERO,
109 GR_GL_ONE,
110 GR_GL_SRC_COLOR,
111 GR_GL_ONE_MINUS_SRC_COLOR,
112 GR_GL_DST_COLOR,
113 GR_GL_ONE_MINUS_DST_COLOR,
114 GR_GL_SRC_ALPHA,
115 GR_GL_ONE_MINUS_SRC_ALPHA,
116 GR_GL_DST_ALPHA,
117 GR_GL_ONE_MINUS_DST_ALPHA,
118 GR_GL_CONSTANT_COLOR,
119 GR_GL_ONE_MINUS_CONSTANT_COLOR,
120 GR_GL_CONSTANT_ALPHA,
121 GR_GL_ONE_MINUS_CONSTANT_ALPHA,
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000122
123 // extended blend coeffs
124 GR_GL_SRC1_COLOR,
125 GR_GL_ONE_MINUS_SRC1_COLOR,
126 GR_GL_SRC1_ALPHA,
127 GR_GL_ONE_MINUS_SRC1_ALPHA,
Mike Klein36743362018-11-06 08:23:30 -0500128
129 // Illegal... needs to map to something.
130 GR_GL_ZERO,
reed@google.comac10a2d2010-12-22 21:39:39 +0000131};
132
bsalomon861e1032014-12-16 07:33:49 -0800133bool GrGLGpu::BlendCoeffReferencesConstant(GrBlendCoeff coeff) {
bsalomon@google.com080773c2011-03-15 19:09:25 +0000134 static const bool gCoeffReferencesBlendConst[] = {
135 false,
136 false,
137 false,
138 false,
139 false,
140 false,
141 false,
142 false,
143 false,
144 false,
145 true,
146 true,
147 true,
148 true,
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000149
150 // extended blend coeffs
151 false,
152 false,
153 false,
154 false,
Mike Klein36743362018-11-06 08:23:30 -0500155
156 // Illegal.
157 false,
bsalomon@google.com080773c2011-03-15 19:09:25 +0000158 };
159 return gCoeffReferencesBlendConst[coeff];
bsalomonf7cc8772015-05-11 11:21:14 -0700160 GR_STATIC_ASSERT(kGrBlendCoeffCnt == SK_ARRAY_COUNT(gCoeffReferencesBlendConst));
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000161
bsalomon@google.com47059542012-06-06 20:51:20 +0000162 GR_STATIC_ASSERT(0 == kZero_GrBlendCoeff);
163 GR_STATIC_ASSERT(1 == kOne_GrBlendCoeff);
164 GR_STATIC_ASSERT(2 == kSC_GrBlendCoeff);
165 GR_STATIC_ASSERT(3 == kISC_GrBlendCoeff);
166 GR_STATIC_ASSERT(4 == kDC_GrBlendCoeff);
167 GR_STATIC_ASSERT(5 == kIDC_GrBlendCoeff);
168 GR_STATIC_ASSERT(6 == kSA_GrBlendCoeff);
169 GR_STATIC_ASSERT(7 == kISA_GrBlendCoeff);
170 GR_STATIC_ASSERT(8 == kDA_GrBlendCoeff);
171 GR_STATIC_ASSERT(9 == kIDA_GrBlendCoeff);
172 GR_STATIC_ASSERT(10 == kConstC_GrBlendCoeff);
173 GR_STATIC_ASSERT(11 == kIConstC_GrBlendCoeff);
174 GR_STATIC_ASSERT(12 == kConstA_GrBlendCoeff);
175 GR_STATIC_ASSERT(13 == kIConstA_GrBlendCoeff);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000176
bsalomon@google.com47059542012-06-06 20:51:20 +0000177 GR_STATIC_ASSERT(14 == kS2C_GrBlendCoeff);
178 GR_STATIC_ASSERT(15 == kIS2C_GrBlendCoeff);
179 GR_STATIC_ASSERT(16 == kS2A_GrBlendCoeff);
180 GR_STATIC_ASSERT(17 == kIS2A_GrBlendCoeff);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000181
182 // assertion for gXfermodeCoeff2Blend have to be in GrGpu scope
bsalomonf7cc8772015-05-11 11:21:14 -0700183 GR_STATIC_ASSERT(kGrBlendCoeffCnt == SK_ARRAY_COUNT(gXfermodeCoeff2Blend));
bsalomon@google.com080773c2011-03-15 19:09:25 +0000184}
185
Brian Salomond978b902019-02-07 15:09:18 -0500186//////////////////////////////////////////////////////////////////////////////
187
188static int gl_target_to_binding_index(GrGLenum target) {
189 switch (target) {
190 case GR_GL_TEXTURE_2D:
191 return 0;
192 case GR_GL_TEXTURE_RECTANGLE:
193 return 1;
194 case GR_GL_TEXTURE_EXTERNAL:
195 return 2;
196 }
197 SK_ABORT("Unexpected GL texture target.");
198 return 0;
199}
200
201GrGpuResource::UniqueID GrGLGpu::TextureUnitBindings::boundID(GrGLenum target) const {
Brian Salomon1f05d452019-02-08 12:33:08 -0500202 return fTargetBindings[gl_target_to_binding_index(target)].fBoundResourceID;
203}
204
205bool GrGLGpu::TextureUnitBindings::hasBeenModified(GrGLenum target) const {
206 return fTargetBindings[gl_target_to_binding_index(target)].fHasBeenModified;
Brian Salomond978b902019-02-07 15:09:18 -0500207}
208
209void GrGLGpu::TextureUnitBindings::setBoundID(GrGLenum target, GrGpuResource::UniqueID resourceID) {
Brian Salomon1f05d452019-02-08 12:33:08 -0500210 int targetIndex = gl_target_to_binding_index(target);
211 fTargetBindings[targetIndex].fBoundResourceID = resourceID;
212 fTargetBindings[targetIndex].fHasBeenModified = true;
Brian Salomond978b902019-02-07 15:09:18 -0500213}
214
Brian Salomon1f05d452019-02-08 12:33:08 -0500215void GrGLGpu::TextureUnitBindings::invalidateForScratchUse(GrGLenum target) {
216 this->setBoundID(target, GrGpuResource::UniqueID());
Brian Salomond978b902019-02-07 15:09:18 -0500217}
218
Brian Salomon1f05d452019-02-08 12:33:08 -0500219void GrGLGpu::TextureUnitBindings::invalidateAllTargets(bool markUnmodified) {
220 for (auto& targetBinding : fTargetBindings) {
221 targetBinding.fBoundResourceID.makeInvalid();
222 if (markUnmodified) {
223 targetBinding.fHasBeenModified = false;
224 }
Brian Salomond978b902019-02-07 15:09:18 -0500225 }
226}
227
228//////////////////////////////////////////////////////////////////////////////
229
Brian Salomondc829942018-10-23 16:07:24 -0400230static GrGLenum filter_to_gl_mag_filter(GrSamplerState::Filter filter) {
231 switch (filter) {
232 case GrSamplerState::Filter::kNearest: return GR_GL_NEAREST;
233 case GrSamplerState::Filter::kBilerp: return GR_GL_LINEAR;
234 case GrSamplerState::Filter::kMipMap: return GR_GL_LINEAR;
235 }
236 SK_ABORT("Unknown filter");
237 return 0;
238}
239
240static GrGLenum filter_to_gl_min_filter(GrSamplerState::Filter filter) {
241 switch (filter) {
242 case GrSamplerState::Filter::kNearest: return GR_GL_NEAREST;
243 case GrSamplerState::Filter::kBilerp: return GR_GL_LINEAR;
244 case GrSamplerState::Filter::kMipMap: return GR_GL_LINEAR_MIPMAP_LINEAR;
245 }
246 SK_ABORT("Unknown filter");
247 return 0;
248}
249
Michael Ludwigf23a1522018-12-10 11:36:13 -0500250static inline GrGLenum wrap_mode_to_gl_wrap(GrSamplerState::WrapMode wrapMode,
251 const GrCaps& caps) {
Brian Salomondc829942018-10-23 16:07:24 -0400252 switch (wrapMode) {
253 case GrSamplerState::WrapMode::kClamp: return GR_GL_CLAMP_TO_EDGE;
254 case GrSamplerState::WrapMode::kRepeat: return GR_GL_REPEAT;
255 case GrSamplerState::WrapMode::kMirrorRepeat: return GR_GL_MIRRORED_REPEAT;
Michael Ludwigf23a1522018-12-10 11:36:13 -0500256 case GrSamplerState::WrapMode::kClampToBorder:
257 // May not be supported but should have been caught earlier
258 SkASSERT(caps.clampToBorderSupport());
259 return GR_GL_CLAMP_TO_BORDER;
Brian Salomon23356442018-11-30 15:33:19 -0500260 }
Brian Salomondc829942018-10-23 16:07:24 -0400261 SK_ABORT("Unknown wrap mode");
262 return 0;
263}
264
265///////////////////////////////////////////////////////////////////////////////
266
267class GrGLGpu::SamplerObjectCache {
268public:
269 SamplerObjectCache(GrGLGpu* gpu) : fGpu(gpu) {
270 fNumTextureUnits = fGpu->glCaps().shaderCaps()->maxFragmentSamplers();
271 fHWBoundSamplers.reset(new GrGLuint[fNumTextureUnits]);
272 std::fill_n(fHWBoundSamplers.get(), fNumTextureUnits, 0);
273 std::fill_n(fSamplers, kNumSamplers, 0);
274 }
275
276 ~SamplerObjectCache() {
277 if (!fNumTextureUnits) {
278 // We've already been abandoned.
279 return;
280 }
281 GR_GL_CALL(fGpu->glInterface(), DeleteSamplers(kNumSamplers, fSamplers));
282 }
283
284 void bindSampler(int unitIdx, const GrSamplerState& state) {
285 int index = StateToIndex(state);
286 if (!fSamplers[index]) {
287 GrGLuint s;
288 GR_GL_CALL(fGpu->glInterface(), GenSamplers(1, &s));
289 if (!s) {
290 return;
291 }
292 fSamplers[index] = s;
293 auto minFilter = filter_to_gl_min_filter(state.filter());
294 auto magFilter = filter_to_gl_mag_filter(state.filter());
Michael Ludwigf23a1522018-12-10 11:36:13 -0500295 auto wrapX = wrap_mode_to_gl_wrap(state.wrapModeX(), fGpu->glCaps());
296 auto wrapY = wrap_mode_to_gl_wrap(state.wrapModeY(), fGpu->glCaps());
Brian Salomondc829942018-10-23 16:07:24 -0400297 GR_GL_CALL(fGpu->glInterface(),
298 SamplerParameteri(s, GR_GL_TEXTURE_MIN_FILTER, minFilter));
299 GR_GL_CALL(fGpu->glInterface(),
300 SamplerParameteri(s, GR_GL_TEXTURE_MAG_FILTER, magFilter));
301 GR_GL_CALL(fGpu->glInterface(), SamplerParameteri(s, GR_GL_TEXTURE_WRAP_S, wrapX));
302 GR_GL_CALL(fGpu->glInterface(), SamplerParameteri(s, GR_GL_TEXTURE_WRAP_T, wrapY));
303 }
304 if (fHWBoundSamplers[unitIdx] != fSamplers[index]) {
305 GR_GL_CALL(fGpu->glInterface(), BindSampler(unitIdx, fSamplers[index]));
306 fHWBoundSamplers[unitIdx] = fSamplers[index];
307 }
308 }
309
310 void invalidateBindings() {
311 // When we have sampler support we always use samplers. So setting these to zero will cause
312 // a rebind on next usage.
313 std::fill_n(fHWBoundSamplers.get(), fNumTextureUnits, 0);
314 }
315
316 void abandon() {
317 fHWBoundSamplers.reset();
318 fNumTextureUnits = 0;
319 }
320
321 void release() {
322 if (!fNumTextureUnits) {
323 // We've already been abandoned.
324 return;
325 }
326 GR_GL_CALL(fGpu->glInterface(), DeleteSamplers(kNumSamplers, fSamplers));
327 std::fill_n(fSamplers, kNumSamplers, 0);
328 // Deleting a bound sampler implicitly binds sampler 0.
329 std::fill_n(fHWBoundSamplers.get(), fNumTextureUnits, 0);
330 }
331
332private:
333 static int StateToIndex(const GrSamplerState& state) {
334 int filter = static_cast<int>(state.filter());
335 SkASSERT(filter >= 0 && filter < 3);
336 int wrapX = static_cast<int>(state.wrapModeX());
Michael Ludwigf23a1522018-12-10 11:36:13 -0500337 SkASSERT(wrapX >= 0 && wrapX < 4);
Brian Salomondc829942018-10-23 16:07:24 -0400338 int wrapY = static_cast<int>(state.wrapModeY());
Michael Ludwigf23a1522018-12-10 11:36:13 -0500339 SkASSERT(wrapY >= 0 && wrapY < 4);
340 int idx = 16 * filter + 4 * wrapX + wrapY;
Brian Salomondc829942018-10-23 16:07:24 -0400341 SkASSERT(idx < kNumSamplers);
342 return idx;
343 }
344
345 GrGLGpu* fGpu;
Michael Ludwigf23a1522018-12-10 11:36:13 -0500346 static constexpr int kNumSamplers = 48;
Brian Salomondc829942018-10-23 16:07:24 -0400347 std::unique_ptr<GrGLuint[]> fHWBoundSamplers;
348 GrGLuint fSamplers[kNumSamplers];
349 int fNumTextureUnits;
350};
351
reed@google.comac10a2d2010-12-22 21:39:39 +0000352///////////////////////////////////////////////////////////////////////////////
353
Brian Salomon384fab42017-12-07 12:33:05 -0500354sk_sp<GrGpu> GrGLGpu::Make(sk_sp<const GrGLInterface> interface, const GrContextOptions& options,
355 GrContext* context) {
356 if (!interface) {
Brian Salomon3d6801e2017-12-11 10:06:31 -0500357 interface = GrGLMakeNativeInterface();
358 // For clients that have written their own GrGLCreateNativeInterface and haven't yet updated
359 // to GrGLMakeNativeInterface.
360 if (!interface) {
361 interface = sk_ref_sp(GrGLCreateNativeInterface());
362 }
Brian Salomon384fab42017-12-07 12:33:05 -0500363 if (!interface) {
364 return nullptr;
365 }
bsalomon424cc262015-05-22 10:37:30 -0700366 }
Jim Van Verth76334772017-05-05 16:46:05 -0400367#ifdef USE_NSIGHT
368 const_cast<GrContextOptions&>(options).fSuppressPathRendering = true;
369#endif
Brian Salomon8ab1cc42017-12-07 12:40:00 -0500370 auto glContext = GrGLContext::Make(std::move(interface), options);
371 if (!glContext) {
372 return nullptr;
bsalomon424cc262015-05-22 10:37:30 -0700373 }
Brian Salomon8ab1cc42017-12-07 12:40:00 -0500374 return sk_sp<GrGpu>(new GrGLGpu(std::move(glContext), context));
bsalomon424cc262015-05-22 10:37:30 -0700375}
376
Brian Salomon8ab1cc42017-12-07 12:40:00 -0500377GrGLGpu::GrGLGpu(std::unique_ptr<GrGLContext> ctx, GrContext* context)
378 : GrGpu(context)
379 , fGLContext(std::move(ctx))
380 , fProgramCache(new ProgramCache(this))
381 , fHWProgramID(0)
382 , fTempSrcFBOID(0)
383 , fTempDstFBOID(0)
Brian Osmana63593a2018-12-06 15:54:53 -0500384 , fStencilClearFBOID(0) {
Brian Salomon8ab1cc42017-12-07 12:40:00 -0500385 SkASSERT(fGLContext);
Brian Salomondc829942018-10-23 16:07:24 -0400386 GrGLClearErr(this->glInterface());
Brian Salomon8ab1cc42017-12-07 12:40:00 -0500387 fCaps = sk_ref_sp(fGLContext->caps());
bsalomon@google.combcce8922013-03-25 15:38:39 +0000388
Brian Salomond978b902019-02-07 15:09:18 -0500389 fHWTextureUnitBindings.reset(this->numTextureUnits());
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +0000390
Brian Salomonae64c192019-02-05 09:41:37 -0500391 this->hwBufferState(GrGpuBufferType::kVertex)->fGLTarget = GR_GL_ARRAY_BUFFER;
392 this->hwBufferState(GrGpuBufferType::kIndex)->fGLTarget = GR_GL_ELEMENT_ARRAY_BUFFER;
cdaltone2e71c22016-04-07 18:13:29 -0700393 if (GrGLCaps::kChromium_TransferBufferType == this->glCaps().transferBufferType()) {
Brian Salomonae64c192019-02-05 09:41:37 -0500394 this->hwBufferState(GrGpuBufferType::kXferCpuToGpu)->fGLTarget =
395 GR_GL_PIXEL_UNPACK_TRANSFER_BUFFER_CHROMIUM;
396 this->hwBufferState(GrGpuBufferType::kXferGpuToCpu)->fGLTarget =
397 GR_GL_PIXEL_PACK_TRANSFER_BUFFER_CHROMIUM;
cdaltone2e71c22016-04-07 18:13:29 -0700398 } else {
Brian Salomonae64c192019-02-05 09:41:37 -0500399 this->hwBufferState(GrGpuBufferType::kXferCpuToGpu)->fGLTarget = GR_GL_PIXEL_UNPACK_BUFFER;
400 this->hwBufferState(GrGpuBufferType::kXferGpuToCpu)->fGLTarget = GR_GL_PIXEL_PACK_BUFFER;
cdaltone2e71c22016-04-07 18:13:29 -0700401 }
Brian Salomonae64c192019-02-05 09:41:37 -0500402 for (int i = 0; i < kGrGpuBufferTypeCount; ++i) {
Rob Phillipsbc534f62017-09-05 19:56:19 -0400403 fHWBufferState[i].invalidate();
404 }
Brian Salomonae64c192019-02-05 09:41:37 -0500405 GR_STATIC_ASSERT(4 == SK_ARRAY_COUNT(fHWBufferState));
cdaltone2e71c22016-04-07 18:13:29 -0700406
407 if (this->glCaps().shaderCaps()->pathRenderingSupport()) {
408 fPathRendering.reset(new GrGLPathRendering(this));
409 }
410
Brian Salomondc829942018-10-23 16:07:24 -0400411 if (this->glCaps().samplerObjectSupport()) {
412 fSamplerObjectCache.reset(new SamplerObjectCache(this));
413 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000414}
415
bsalomon861e1032014-12-16 07:33:49 -0800416GrGLGpu::~GrGLGpu() {
cdaltone2e71c22016-04-07 18:13:29 -0700417 // Ensure any GrGpuResource objects get deleted first, since they may require a working GrGLGpu
418 // to release the resources held by the objects themselves.
kkinnunen702501d2016-01-13 23:36:45 -0800419 fPathRendering.reset();
cdaltone2e71c22016-04-07 18:13:29 -0700420 fCopyProgramArrayBuffer.reset();
brianosman33f6b3f2016-06-02 05:49:21 -0700421 fMipmapProgramArrayBuffer.reset();
kkinnunen702501d2016-01-13 23:36:45 -0800422
Brian Salomon802cb312018-06-08 18:05:20 -0400423 fHWProgram.reset();
Brian Salomon43f8bf02017-10-18 08:33:29 -0400424 if (fHWProgramID) {
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000425 // detach the current program so there is no confusion on OpenGL's part
426 // that we want it to be deleted
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000427 GL_CALL(UseProgram(0));
428 }
429
Brian Salomon43f8bf02017-10-18 08:33:29 -0400430 if (fTempSrcFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700431 this->deleteFramebuffer(fTempSrcFBOID);
egdaniel0f5f9672015-02-03 11:10:51 -0800432 }
Brian Salomon43f8bf02017-10-18 08:33:29 -0400433 if (fTempDstFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700434 this->deleteFramebuffer(fTempDstFBOID);
egdaniel0f5f9672015-02-03 11:10:51 -0800435 }
Brian Salomon43f8bf02017-10-18 08:33:29 -0400436 if (fStencilClearFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700437 this->deleteFramebuffer(fStencilClearFBOID);
bsalomondd3143b2015-02-23 09:27:45 -0800438 }
egdaniel0f5f9672015-02-03 11:10:51 -0800439
bsalomon7ea33f52015-11-22 14:51:00 -0800440 for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) {
441 if (0 != fCopyPrograms[i].fProgram) {
442 GL_CALL(DeleteProgram(fCopyPrograms[i].fProgram));
443 }
bsalomon6df86402015-06-01 10:41:49 -0700444 }
bsalomon6dea83f2015-12-03 12:58:06 -0800445
brianosman33f6b3f2016-06-02 05:49:21 -0700446 for (size_t i = 0; i < SK_ARRAY_COUNT(fMipmapPrograms); ++i) {
447 if (0 != fMipmapPrograms[i].fProgram) {
448 GL_CALL(DeleteProgram(fMipmapPrograms[i].fProgram));
449 }
450 }
451
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000452 delete fProgramCache;
Brian Salomondc829942018-10-23 16:07:24 -0400453 fSamplerObjectCache.reset();
bsalomonc8dc1f72014-08-21 13:02:13 -0700454}
455
bsalomon6e2aad42016-04-01 11:54:31 -0700456void GrGLGpu::disconnect(DisconnectType type) {
457 INHERITED::disconnect(type);
458 if (DisconnectType::kCleanup == type) {
459 if (fHWProgramID) {
460 GL_CALL(UseProgram(0));
461 }
462 if (fTempSrcFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700463 this->deleteFramebuffer(fTempSrcFBOID);
bsalomon6e2aad42016-04-01 11:54:31 -0700464 }
465 if (fTempDstFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700466 this->deleteFramebuffer(fTempDstFBOID);
bsalomon6e2aad42016-04-01 11:54:31 -0700467 }
468 if (fStencilClearFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700469 this->deleteFramebuffer(fStencilClearFBOID);
bsalomon6e2aad42016-04-01 11:54:31 -0700470 }
bsalomon6e2aad42016-04-01 11:54:31 -0700471 for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) {
472 if (fCopyPrograms[i].fProgram) {
473 GL_CALL(DeleteProgram(fCopyPrograms[i].fProgram));
474 }
475 }
brianosman33f6b3f2016-06-02 05:49:21 -0700476 for (size_t i = 0; i < SK_ARRAY_COUNT(fMipmapPrograms); ++i) {
477 if (fMipmapPrograms[i].fProgram) {
478 GL_CALL(DeleteProgram(fMipmapPrograms[i].fProgram));
479 }
480 }
Brian Salomon43f8bf02017-10-18 08:33:29 -0400481
Brian Salomondc829942018-10-23 16:07:24 -0400482 if (fSamplerObjectCache) {
483 fSamplerObjectCache->release();
484 }
bsalomon6e2aad42016-04-01 11:54:31 -0700485 } else {
486 if (fProgramCache) {
487 fProgramCache->abandon();
488 }
Brian Salomondc829942018-10-23 16:07:24 -0400489 if (fSamplerObjectCache) {
490 fSamplerObjectCache->abandon();
491 }
bsalomon6e2aad42016-04-01 11:54:31 -0700492 }
493
Brian Salomon802cb312018-06-08 18:05:20 -0400494 fHWProgram.reset();
bsalomon6e2aad42016-04-01 11:54:31 -0700495 delete fProgramCache;
496 fProgramCache = nullptr;
497
bsalomonc8dc1f72014-08-21 13:02:13 -0700498 fHWProgramID = 0;
egdanield803f272015-03-18 13:01:52 -0700499 fTempSrcFBOID = 0;
500 fTempDstFBOID = 0;
501 fStencilClearFBOID = 0;
cdaltone2e71c22016-04-07 18:13:29 -0700502 fCopyProgramArrayBuffer.reset();
bsalomon7ea33f52015-11-22 14:51:00 -0800503 for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) {
504 fCopyPrograms[i].fProgram = 0;
505 }
brianosman33f6b3f2016-06-02 05:49:21 -0700506 fMipmapProgramArrayBuffer.reset();
507 for (size_t i = 0; i < SK_ARRAY_COUNT(fMipmapPrograms); ++i) {
508 fMipmapPrograms[i].fProgram = 0;
509 }
Brian Salomon43f8bf02017-10-18 08:33:29 -0400510
jvanverthe9c0fc62015-04-29 11:18:05 -0700511 if (this->glCaps().shaderCaps()->pathRenderingSupport()) {
bsalomon6e2aad42016-04-01 11:54:31 -0700512 this->glPathRendering()->disconnect(type);
bsalomonc8dc1f72014-08-21 13:02:13 -0700513 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000514}
515
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000516///////////////////////////////////////////////////////////////////////////////
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000517
bsalomon861e1032014-12-16 07:33:49 -0800518void GrGLGpu::onResetContext(uint32_t resetBits) {
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000519 if (resetBits & kMisc_GrGLBackendState) {
Brian Salomonf0861672017-05-08 11:10:10 -0400520 // we don't use the zb at all
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000521 GL_CALL(Disable(GR_GL_DEPTH_TEST));
522 GL_CALL(DepthMask(GR_GL_FALSE));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000523
Brian Salomonf0861672017-05-08 11:10:10 -0400524 // We don't use face culling.
525 GL_CALL(Disable(GR_GL_CULL_FACE));
Chris Daltonc8ece3d2018-07-30 15:03:45 -0600526 // We do use separate stencil. Our algorithms don't care which face is front vs. back so
527 // just set this to the default for self-consistency.
528 GL_CALL(FrontFace(GR_GL_CCW));
Brian Salomonf0861672017-05-08 11:10:10 -0400529
Brian Salomonae64c192019-02-05 09:41:37 -0500530 this->hwBufferState(GrGpuBufferType::kXferCpuToGpu)->invalidate();
531 this->hwBufferState(GrGpuBufferType::kXferGpuToCpu)->invalidate();
cdaltonc1613102016-03-16 07:48:20 -0700532
Kevin Lubick8aa203c2019-03-19 13:23:10 -0400533 if (GR_IS_GR_GL(this->glStandard())) {
Jim Van Verth32ac83e2016-11-28 15:23:57 -0500534#ifndef USE_NSIGHT
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000535 // Desktop-only state that we never change
536 if (!this->glCaps().isCoreProfile()) {
537 GL_CALL(Disable(GR_GL_POINT_SMOOTH));
538 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
539 GL_CALL(Disable(GR_GL_POLYGON_SMOOTH));
540 GL_CALL(Disable(GR_GL_POLYGON_STIPPLE));
541 GL_CALL(Disable(GR_GL_COLOR_LOGIC_OP));
542 GL_CALL(Disable(GR_GL_INDEX_LOGIC_OP));
543 }
544 // The windows NVIDIA driver has GL_ARB_imaging in the extension string when using a
545 // core profile. This seems like a bug since the core spec removes any mention of
546 // GL_ARB_imaging.
547 if (this->glCaps().imagingSupport() && !this->glCaps().isCoreProfile()) {
548 GL_CALL(Disable(GR_GL_COLOR_TABLE));
549 }
550 GL_CALL(Disable(GR_GL_POLYGON_OFFSET_FILL));
Jim Van Verth609e7cc2017-03-30 14:28:08 -0400551
Jim Van Verthfbdc0802017-05-02 16:15:53 -0400552 if (this->caps()->wireframeMode()) {
553 GL_CALL(PolygonMode(GR_GL_FRONT_AND_BACK, GR_GL_LINE));
554 } else {
555 GL_CALL(PolygonMode(GR_GL_FRONT_AND_BACK, GR_GL_FILL));
556 }
Jim Van Verth32ac83e2016-11-28 15:23:57 -0500557#endif
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000558 // Since ES doesn't support glPointSize at all we always use the VS to
559 // set the point size
560 GL_CALL(Enable(GR_GL_VERTEX_PROGRAM_POINT_SIZE));
561
bsalomon@google.com2b1b8c02013-02-28 22:06:02 +0000562 }
joshualitt58162332014-08-01 06:44:53 -0700563
Kevin Lubick8aa203c2019-03-19 13:23:10 -0400564 if (GR_IS_GR_GL_ES(this->glStandard()) &&
Brian Salomon8bc352c2019-01-28 09:05:22 -0500565 this->glCaps().fbFetchRequiresEnablePerSample()) {
joshualitt58162332014-08-01 06:44:53 -0700566 // The arm extension requires specifically enabling MSAA fetching per sample.
567 // On some devices this may have a perf hit. Also multiple render targets are disabled
Brian Salomon8bc352c2019-01-28 09:05:22 -0500568 GL_CALL(Enable(GR_GL_FETCH_PER_SAMPLE));
joshualitt58162332014-08-01 06:44:53 -0700569 }
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000570 fHWWriteToColor = kUnknown_TriState;
571 // we only ever use lines in hairline mode
572 GL_CALL(LineWidth(1));
bsalomonaca31fe2015-09-22 11:38:46 -0700573 GL_CALL(Disable(GR_GL_DITHER));
Brian Salomon805cc7a2019-01-28 09:52:34 -0500574
575 fHWClearColor[0] = fHWClearColor[1] = fHWClearColor[2] = fHWClearColor[3] = SK_FloatNaN;
bsalomon@google.comcad107b2013-06-28 14:32:08 +0000576 }
edisonn@google.comba669992013-06-28 16:03:21 +0000577
egdanielb414f252014-07-29 13:15:47 -0700578 if (resetBits & kMSAAEnable_GrGLBackendState) {
579 fMSAAEnabled = kUnknown_TriState;
vbuzinovdded6962015-06-12 08:59:45 -0700580
egdanieleed519e2016-01-15 11:36:18 -0800581 if (this->caps()->usesMixedSamples()) {
cdaltonaf8bc7d2016-02-05 09:35:20 -0800582 // The skia blend modes all use premultiplied alpha and therefore expect RGBA coverage
583 // modulation. This state has no effect when not rendering to a mixed sampled target.
vbuzinovdded6962015-06-12 08:59:45 -0700584 GL_CALL(CoverageModulation(GR_GL_RGBA));
585 }
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000586 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000587
commit-bot@chromium.org46fbfe02013-08-30 15:52:12 +0000588 fHWActiveTextureUnitIdx = -1; // invalid
Brian Salomonaf971de2017-06-08 16:11:33 -0400589 fLastPrimitiveType = static_cast<GrPrimitiveType>(-1);
commit-bot@chromium.org46fbfe02013-08-30 15:52:12 +0000590
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000591 if (resetBits & kTextureBinding_GrGLBackendState) {
Brian Salomond978b902019-02-07 15:09:18 -0500592 for (int s = 0; s < this->numTextureUnits(); ++s) {
Brian Salomon1f05d452019-02-08 12:33:08 -0500593 fHWTextureUnitBindings[s].invalidateAllTargets(false);
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000594 }
Brian Salomondc829942018-10-23 16:07:24 -0400595 if (fSamplerObjectCache) {
596 fSamplerObjectCache->invalidateBindings();
597 }
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000598 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000599
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000600 if (resetBits & kBlend_GrGLBackendState) {
601 fHWBlendState.invalidate();
602 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000603
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000604 if (resetBits & kView_GrGLBackendState) {
605 fHWScissorSettings.invalidate();
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700606 fHWWindowRectsState.invalidate();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000607 fHWViewport.invalidate();
608 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000609
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000610 if (resetBits & kStencil_GrGLBackendState) {
611 fHWStencilSettings.invalidate();
612 fHWStencilTestEnabled = kUnknown_TriState;
613 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000614
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000615 // Vertex
616 if (resetBits & kVertex_GrGLBackendState) {
cdaltone2e71c22016-04-07 18:13:29 -0700617 fHWVertexArrayState.invalidate();
Brian Salomonae64c192019-02-05 09:41:37 -0500618 this->hwBufferState(GrGpuBufferType::kVertex)->invalidate();
619 this->hwBufferState(GrGpuBufferType::kIndex)->invalidate();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000620 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000621
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000622 if (resetBits & kRenderTarget_GrGLBackendState) {
Robert Phillips294870f2016-11-11 12:38:40 -0500623 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon16921ec2015-07-30 15:34:56 -0700624 fHWSRGBFramebuffer = kUnknown_TriState;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000625 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000626
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000627 if (resetBits & kPathRendering_GrGLBackendState) {
jvanverthe9c0fc62015-04-29 11:18:05 -0700628 if (this->caps()->shaderCaps()->pathRenderingSupport()) {
kkinnunenccdaa042014-08-20 01:36:23 -0700629 this->glPathRendering()->resetContext();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000630 }
bsalomon@google.com05a718c2012-06-29 14:01:53 +0000631 }
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000632
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000633 // we assume these values
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000634 if (resetBits & kPixelStore_GrGLBackendState) {
635 if (this->glCaps().unpackRowLengthSupport()) {
636 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
637 }
638 if (this->glCaps().packRowLengthSupport()) {
639 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
640 }
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000641 if (this->glCaps().packFlipYSupport()) {
642 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, GR_GL_FALSE));
643 }
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000644 }
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000645
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000646 if (resetBits & kProgram_GrGLBackendState) {
647 fHWProgramID = 0;
Brian Salomon802cb312018-06-08 18:05:20 -0400648 fHWProgram.reset();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000649 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000650}
651
Brian Salomond17f6582017-07-19 18:28:58 -0400652static bool check_backend_texture(const GrBackendTexture& backendTex, const GrGLCaps& caps,
653 GrGLTexture::IDDesc* idDesc) {
Greg Daniel52e16d92018-04-10 09:34:07 -0400654 GrGLTextureInfo info;
655 if (!backendTex.getGLTextureInfo(&info) || !info.fID) {
Brian Salomond17f6582017-07-19 18:28:58 -0400656 return false;
bsalomon091f60c2015-11-10 11:54:56 -0800657 }
robertphillips@google.comb72e5d32012-10-30 15:18:10 +0000658
Greg Daniel52e16d92018-04-10 09:34:07 -0400659 idDesc->fInfo = info;
bsalomon7ea33f52015-11-22 14:51:00 -0800660
Brian Salomond17f6582017-07-19 18:28:58 -0400661 if (GR_GL_TEXTURE_EXTERNAL == idDesc->fInfo.fTarget) {
662 if (!caps.shaderCaps()->externalTextureSupport()) {
663 return false;
664 }
665 } else if (GR_GL_TEXTURE_RECTANGLE == idDesc->fInfo.fTarget) {
666 if (!caps.rectangleTextureSupport()) {
667 return false;
668 }
669 } else if (GR_GL_TEXTURE_2D != idDesc->fInfo.fTarget) {
670 return false;
671 }
672 return true;
673}
674
675sk_sp<GrTexture> GrGLGpu::onWrapBackendTexture(const GrBackendTexture& backendTex,
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500676 GrWrapOwnership ownership, GrWrapCacheable cacheable,
677 GrIOType ioType) {
bsalomonb15b4c12014-10-29 12:41:57 -0700678 GrGLTexture::IDDesc idDesc;
Brian Salomond17f6582017-07-19 18:28:58 -0400679 if (!check_backend_texture(backendTex, this->glCaps(), &idDesc)) {
680 return nullptr;
681 }
Greg Daniele7d8da42017-12-04 11:23:19 -0500682 if (!idDesc.fInfo.fFormat) {
683 idDesc.fInfo.fFormat = this->glCaps().configSizedInternalFormat(backendTex.config());
684 }
Brian Salomond17f6582017-07-19 18:28:58 -0400685 if (kBorrow_GrWrapOwnership == ownership) {
686 idDesc.fOwnership = GrBackendObjectOwnership::kBorrowed;
687 } else {
688 idDesc.fOwnership = GrBackendObjectOwnership::kOwned;
689 }
bsalomone5286e02016-01-14 09:24:09 -0800690
Brian Salomond17f6582017-07-19 18:28:58 -0400691 GrSurfaceDesc surfDesc;
692 surfDesc.fFlags = kNone_GrSurfaceFlags;
693 surfDesc.fWidth = backendTex.width();
694 surfDesc.fHeight = backendTex.height();
695 surfDesc.fConfig = backendTex.config();
Brian Salomonbdecacf2018-02-02 20:32:49 -0500696 surfDesc.fSampleCnt = 1;
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400697
Greg Daniel177e6952017-10-12 12:27:11 -0400698 GrMipMapsStatus mipMapsStatus = backendTex.hasMipMaps() ? GrMipMapsStatus::kValid
699 : GrMipMapsStatus::kNotAllocated;
700
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500701 auto texture =
702 GrGLTexture::MakeWrapped(this, surfDesc, mipMapsStatus, idDesc, cacheable, ioType);
Brian Salomondc829942018-10-23 16:07:24 -0400703 // We don't know what parameters are already set on wrapped textures.
704 texture->textureParamsModified();
705 return std::move(texture);
Brian Salomond17f6582017-07-19 18:28:58 -0400706}
707
708sk_sp<GrTexture> GrGLGpu::onWrapRenderableBackendTexture(const GrBackendTexture& backendTex,
Brian Salomond17f6582017-07-19 18:28:58 -0400709 int sampleCnt,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500710 GrWrapOwnership ownership,
711 GrWrapCacheable cacheable) {
Brian Salomond17f6582017-07-19 18:28:58 -0400712 GrGLTexture::IDDesc idDesc;
713 if (!check_backend_texture(backendTex, this->glCaps(), &idDesc)) {
bsalomone5286e02016-01-14 09:24:09 -0800714 return nullptr;
bsalomon7ea33f52015-11-22 14:51:00 -0800715 }
Greg Daniele7d8da42017-12-04 11:23:19 -0500716 if (!idDesc.fInfo.fFormat) {
717 idDesc.fInfo.fFormat = this->glCaps().configSizedInternalFormat(backendTex.config());
718 }
bsalomone5286e02016-01-14 09:24:09 -0800719
Brian Salomond17f6582017-07-19 18:28:58 -0400720 // We don't support rendering to a EXTERNAL texture.
721 if (GR_GL_TEXTURE_EXTERNAL == idDesc.fInfo.fTarget) {
bsalomona98419b2015-11-23 07:09:50 -0800722 return nullptr;
723 }
bsalomon10528f12015-10-14 12:54:52 -0700724
Brian Osman766fcbb2017-03-13 09:33:09 -0400725 if (kBorrow_GrWrapOwnership == ownership) {
Brian Osmana6953f22017-03-10 20:14:05 +0000726 idDesc.fOwnership = GrBackendObjectOwnership::kBorrowed;
Brian Osman766fcbb2017-03-13 09:33:09 -0400727 } else {
728 idDesc.fOwnership = GrBackendObjectOwnership::kOwned;
bsalomone5286e02016-01-14 09:24:09 -0800729 }
bsalomonb15b4c12014-10-29 12:41:57 -0700730
Ben Wagner18b61f92016-10-25 10:44:02 -0400731 GrSurfaceDesc surfDesc;
Brian Salomond17f6582017-07-19 18:28:58 -0400732 surfDesc.fFlags = kRenderTarget_GrSurfaceFlag;
Greg Daniel7ef28f32017-04-20 16:41:55 +0000733 surfDesc.fWidth = backendTex.width();
734 surfDesc.fHeight = backendTex.height();
735 surfDesc.fConfig = backendTex.config();
Brian Salomonbdecacf2018-02-02 20:32:49 -0500736 surfDesc.fSampleCnt = this->caps()->getRenderTargetSampleCount(sampleCnt, backendTex.config());
737 if (surfDesc.fSampleCnt < 1) {
738 return nullptr;
739 }
bsalomon@google.come269f212011-11-07 13:29:52 +0000740
Brian Salomond17f6582017-07-19 18:28:58 -0400741 GrGLRenderTarget::IDDesc rtIDDesc;
742 if (!this->createRenderTargetObjects(surfDesc, idDesc.fInfo, &rtIDDesc)) {
743 return nullptr;
bsalomon@google.come269f212011-11-07 13:29:52 +0000744 }
Greg Daniel177e6952017-10-12 12:27:11 -0400745
746 GrMipMapsStatus mipMapsStatus = backendTex.hasMipMaps() ? GrMipMapsStatus::kDirty
747 : GrMipMapsStatus::kNotAllocated;
748
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500749 sk_sp<GrGLTextureRenderTarget> texRT(GrGLTextureRenderTarget::MakeWrapped(
750 this, surfDesc, idDesc, rtIDDesc, cacheable, mipMapsStatus));
Brian Salomond17f6582017-07-19 18:28:58 -0400751 texRT->baseLevelWasBoundToFBO();
Brian Salomondc829942018-10-23 16:07:24 -0400752 // We don't know what parameters are already set on wrapped textures.
753 texRT->textureParamsModified();
Brian Salomond17f6582017-07-19 18:28:58 -0400754 return std::move(texRT);
bsalomon@google.come269f212011-11-07 13:29:52 +0000755}
756
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400757sk_sp<GrRenderTarget> GrGLGpu::onWrapBackendRenderTarget(const GrBackendRenderTarget& backendRT) {
Greg Daniel323fbcf2018-04-10 13:46:30 -0400758 GrGLFramebufferInfo info;
759 if (!backendRT.getGLFramebufferInfo(&info)) {
Greg Danielbcf612b2017-05-01 13:50:58 +0000760 return nullptr;
761 }
762
bsalomonb15b4c12014-10-29 12:41:57 -0700763 GrGLRenderTarget::IDDesc idDesc;
Greg Daniel323fbcf2018-04-10 13:46:30 -0400764 idDesc.fRTFBOID = info.fFBOID;
bsalomonb15b4c12014-10-29 12:41:57 -0700765 idDesc.fMSColorRenderbufferID = 0;
egdanield803f272015-03-18 13:01:52 -0700766 idDesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
Brian Osman0b791f52017-03-10 08:30:22 -0500767 idDesc.fRTFBOOwnership = GrBackendObjectOwnership::kBorrowed;
csmartdaltonf9635992016-08-10 11:09:07 -0700768 idDesc.fIsMixedSampled = false;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000769
bsalomonb15b4c12014-10-29 12:41:57 -0700770 GrSurfaceDesc desc;
Brian Salomon0ec981b2017-05-15 13:48:50 -0400771 desc.fFlags = kRenderTarget_GrSurfaceFlag;
Greg Danielbcf612b2017-05-01 13:50:58 +0000772 desc.fWidth = backendRT.width();
773 desc.fHeight = backendRT.height();
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400774 desc.fConfig = backendRT.config();
Brian Salomonbdecacf2018-02-02 20:32:49 -0500775 desc.fSampleCnt =
776 this->caps()->getRenderTargetSampleCount(backendRT.sampleCnt(), backendRT.config());
bsalomonb15b4c12014-10-29 12:41:57 -0700777
Greg Daniel4065d452018-11-16 15:43:41 -0500778 return GrGLRenderTarget::MakeWrapped(this, desc, info.fFormat, idDesc, backendRT.stencilBits());
bsalomon@google.come269f212011-11-07 13:29:52 +0000779}
780
Greg Daniel7ef28f32017-04-20 16:41:55 +0000781sk_sp<GrRenderTarget> GrGLGpu::onWrapBackendTextureAsRenderTarget(const GrBackendTexture& tex,
Greg Daniel7ef28f32017-04-20 16:41:55 +0000782 int sampleCnt) {
Greg Daniel52e16d92018-04-10 09:34:07 -0400783 GrGLTextureInfo info;
784 if (!tex.getGLTextureInfo(&info) || !info.fID) {
ericrkf7b8b8a2016-02-24 14:49:51 -0800785 return nullptr;
786 }
ericrkf7b8b8a2016-02-24 14:49:51 -0800787
Greg Daniel52e16d92018-04-10 09:34:07 -0400788 if (GR_GL_TEXTURE_RECTANGLE != info.fTarget &&
789 GR_GL_TEXTURE_2D != info.fTarget) {
ericrkf7b8b8a2016-02-24 14:49:51 -0800790 // Only texture rectangle and texture 2d are supported. We do not check whether texture
791 // rectangle is supported by Skia - if the caller provided us with a texture rectangle,
792 // we assume the necessary support exists.
793 return nullptr;
794 }
795
Ben Wagner18b61f92016-10-25 10:44:02 -0400796 GrSurfaceDesc surfDesc;
Greg Daniel7ef28f32017-04-20 16:41:55 +0000797 surfDesc.fFlags = kRenderTarget_GrSurfaceFlag;
798 surfDesc.fWidth = tex.width();
799 surfDesc.fHeight = tex.height();
800 surfDesc.fConfig = tex.config();
Brian Salomonbdecacf2018-02-02 20:32:49 -0500801 surfDesc.fSampleCnt = this->caps()->getRenderTargetSampleCount(sampleCnt, tex.config());
ericrkf7b8b8a2016-02-24 14:49:51 -0800802
803 GrGLRenderTarget::IDDesc rtIDDesc;
Greg Daniel52e16d92018-04-10 09:34:07 -0400804 if (!this->createRenderTargetObjects(surfDesc, info, &rtIDDesc)) {
ericrkf7b8b8a2016-02-24 14:49:51 -0800805 return nullptr;
806 }
Greg Daniel4065d452018-11-16 15:43:41 -0500807 return GrGLRenderTarget::MakeWrapped(this, surfDesc, info.fFormat, rtIDDesc, 0);
ericrkf7b8b8a2016-02-24 14:49:51 -0800808}
809
Brian Salomonc320b152018-02-20 14:05:36 -0500810static bool check_write_and_transfer_input(GrGLTexture* glTex) {
bsalomon6cb3cbe2015-07-30 07:34:27 -0700811 if (!glTex) {
812 return false;
813 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000814
bsalomone5286e02016-01-14 09:24:09 -0800815 // Write or transfer of pixels is not implemented for TEXTURE_EXTERNAL textures
816 if (GR_GL_TEXTURE_EXTERNAL == glTex->target()) {
bsalomon7ea33f52015-11-22 14:51:00 -0800817 return false;
818 }
819
jvanverth17aa0472016-01-05 10:41:27 -0800820 return true;
821}
822
Brian Salomona9b04b92018-06-01 15:04:28 -0400823bool GrGLGpu::onWritePixels(GrSurface* surface, int left, int top, int width, int height,
824 GrColorType srcColorType, const GrMipLevel texels[],
825 int mipLevelCount) {
Brian Salomonc320b152018-02-20 14:05:36 -0500826 auto glTex = static_cast<GrGLTexture*>(surface->asTexture());
jvanverth17aa0472016-01-05 10:41:27 -0800827
Brian Salomonc320b152018-02-20 14:05:36 -0500828 if (!check_write_and_transfer_input(glTex)) {
jvanverth17aa0472016-01-05 10:41:27 -0800829 return false;
830 }
831
Brian Salomond978b902019-02-07 15:09:18 -0500832 this->bindTextureToScratchUnit(glTex->target(), glTex->textureID());
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000833
Brian Salomonc320b152018-02-20 14:05:36 -0500834 // No sRGB transformation occurs in uploadTexData. We choose to make the src config match the
835 // srgb-ness of the surface to avoid issues in ES2 where internal/external formats must match.
836 // When we're on ES2 and the dst is GL_SRGB_ALPHA by making the config be kSRGB_8888 we know
837 // that our caps will choose GL_SRGB_ALPHA as the external format, too. On ES3 or regular GL our
838 // caps knows to make the external format be GL_RGBA.
839 auto srgbEncoded = GrPixelConfigIsSRGBEncoded(surface->config());
840 auto srcAsConfig = GrColorTypeToPixelConfig(srcColorType, srgbEncoded);
Jim Van Verth1676cb92019-01-15 13:24:45 -0500841
842 SkASSERT(!GrPixelConfigIsCompressed(glTex->config()));
Brian Salomona9b04b92018-06-01 15:04:28 -0400843 return this->uploadTexData(glTex->config(), glTex->width(), glTex->height(), glTex->target(),
844 kWrite_UploadType, left, top, width, height, srcAsConfig, texels,
845 mipLevelCount);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000846}
847
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400848// For GL_[UN]PACK_ALIGNMENT. TODO: This really wants to be GrColorType.
bsalomonf46a1242015-12-15 12:37:38 -0800849static inline GrGLint config_alignment(GrPixelConfig config) {
Jim Van Verth1676cb92019-01-15 13:24:45 -0500850 SkASSERT(!GrPixelConfigIsCompressed(config));
bsalomonf46a1242015-12-15 12:37:38 -0800851 switch (config) {
852 case kAlpha_8_GrPixelConfig:
Greg Danielef59d872017-11-17 16:47:21 -0500853 case kAlpha_8_as_Alpha_GrPixelConfig:
854 case kAlpha_8_as_Red_GrPixelConfig:
Brian Osman986563b2017-01-10 14:20:02 -0500855 case kGray_8_GrPixelConfig:
Greg Daniel7af060a2017-12-05 16:27:11 -0500856 case kGray_8_as_Lum_GrPixelConfig:
857 case kGray_8_as_Red_GrPixelConfig:
bsalomonf46a1242015-12-15 12:37:38 -0800858 return 1;
859 case kRGB_565_GrPixelConfig:
860 case kRGBA_4444_GrPixelConfig:
Jim Van Verth69e57852018-12-05 13:38:59 -0500861 case kRG_88_GrPixelConfig:
bsalomonf46a1242015-12-15 12:37:38 -0800862 case kAlpha_half_GrPixelConfig:
Greg Danielef59d872017-11-17 16:47:21 -0500863 case kAlpha_half_as_Red_GrPixelConfig:
bsalomonf46a1242015-12-15 12:37:38 -0800864 case kRGBA_half_GrPixelConfig:
Brian Osmand0626aa2019-03-11 15:28:06 -0400865 case kRGBA_half_Clamped_GrPixelConfig:
bsalomonf46a1242015-12-15 12:37:38 -0800866 return 2;
867 case kRGBA_8888_GrPixelConfig:
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400868 case kRGB_888_GrPixelConfig: // We're really talking about GrColorType::kRGB_888x here.
Greg Danielf259b8b2019-02-14 09:03:43 -0500869 case kRGB_888X_GrPixelConfig:
bsalomonf46a1242015-12-15 12:37:38 -0800870 case kBGRA_8888_GrPixelConfig:
871 case kSRGBA_8888_GrPixelConfig:
brianosmana6359362016-03-21 06:55:37 -0700872 case kSBGRA_8888_GrPixelConfig:
Brian Osman10fc6fd2018-03-02 11:01:10 -0500873 case kRGBA_1010102_GrPixelConfig:
bsalomonf46a1242015-12-15 12:37:38 -0800874 case kRGBA_float_GrPixelConfig:
csmartdalton6aa0e112017-02-08 16:14:11 -0500875 case kRG_float_GrPixelConfig:
bsalomonf46a1242015-12-15 12:37:38 -0800876 return 4;
Jim Van Verth1676cb92019-01-15 13:24:45 -0500877 case kRGB_ETC1_GrPixelConfig:
csmartdalton6aa0e112017-02-08 16:14:11 -0500878 case kUnknown_GrPixelConfig:
bsalomonf46a1242015-12-15 12:37:38 -0800879 return 0;
880 }
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400881 SK_ABORT("Invalid pixel config");
csmartdalton6aa0e112017-02-08 16:14:11 -0500882 return 0;
bsalomonf46a1242015-12-15 12:37:38 -0800883}
884
Brian Salomone05ba5a2019-04-08 11:59:07 -0400885bool GrGLGpu::onTransferPixelsTo(GrTexture* texture, int left, int top, int width, int height,
886 GrColorType bufferColorType, GrGpuBuffer* transferBuffer,
887 size_t offset, size_t rowBytes) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400888 GrGLTexture* glTex = static_cast<GrGLTexture*>(texture);
889 GrPixelConfig texConfig = glTex->config();
890 SkASSERT(this->caps()->isConfigTexturable(texConfig));
891
Jim Van Verth1676cb92019-01-15 13:24:45 -0500892 // Can't transfer compressed data
893 SkASSERT(!GrPixelConfigIsCompressed(glTex->config()));
894
Brian Salomonc320b152018-02-20 14:05:36 -0500895 if (!check_write_and_transfer_input(glTex)) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400896 return false;
897 }
898
Hal Canaryc36f7e72018-06-18 15:50:09 -0400899 static_assert(sizeof(int) == sizeof(int32_t), "");
900 if (width <= 0 || height <= 0) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400901 return false;
902 }
903
Brian Salomond978b902019-02-07 15:09:18 -0500904 this->bindTextureToScratchUnit(glTex->target(), glTex->textureID());
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400905
906 SkASSERT(!transferBuffer->isMapped());
Brian Salomondbf70722019-02-07 11:31:24 -0500907 SkASSERT(!transferBuffer->isCpuBuffer());
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400908 const GrGLBuffer* glBuffer = static_cast<const GrGLBuffer*>(transferBuffer);
Brian Salomonae64c192019-02-05 09:41:37 -0500909 this->bindBuffer(GrGpuBufferType::kXferCpuToGpu, glBuffer);
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400910
Greg Daniel660cc992017-06-26 14:55:05 -0400911 SkDEBUGCODE(
912 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
913 SkIRect bounds = SkIRect::MakeWH(texture->width(), texture->height());
914 SkASSERT(bounds.contains(subRect));
915 )
916
Brian Salomonc320b152018-02-20 14:05:36 -0500917 int bpp = GrColorTypeBytesPerPixel(bufferColorType);
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400918 const size_t trimRowBytes = width * bpp;
Greg Daniel660cc992017-06-26 14:55:05 -0400919 if (!rowBytes) {
920 rowBytes = trimRowBytes;
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400921 }
Greg Daniel660cc992017-06-26 14:55:05 -0400922 const void* pixels = (void*)offset;
Bruce Dawson71479b72017-07-05 14:30:20 -0700923 if (width < 0 || height < 0) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400924 return false;
925 }
926
927 bool restoreGLRowLength = false;
928 if (trimRowBytes != rowBytes) {
929 // we should have checked for this support already
930 SkASSERT(this->glCaps().unpackRowLengthSupport());
931 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowBytes / bpp));
932 restoreGLRowLength = true;
933 }
934
935 // Internal format comes from the texture desc.
936 GrGLenum internalFormat;
937 // External format and type come from the upload data.
938 GrGLenum externalFormat;
939 GrGLenum externalType;
Brian Salomonc320b152018-02-20 14:05:36 -0500940 auto bufferAsConfig = GrColorTypeToPixelConfig(bufferColorType, GrSRGBEncoded::kNo);
941 if (!this->glCaps().getTexImageFormats(texConfig, bufferAsConfig, &internalFormat,
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400942 &externalFormat, &externalType)) {
943 return false;
944 }
945
946 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, config_alignment(texConfig)));
947 GL_CALL(TexSubImage2D(glTex->target(),
948 0,
949 left, top,
950 width,
951 height,
952 externalFormat, externalType,
953 pixels));
954
955 if (restoreGLRowLength) {
956 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
957 }
958
959 return true;
960}
961
Brian Salomone05ba5a2019-04-08 11:59:07 -0400962size_t GrGLGpu::onTransferPixelsFrom(GrSurface* surface, int left, int top, int width, int height,
963 GrColorType dstColorType, GrGpuBuffer* transferBuffer,
964 size_t offset) {
965 size_t offsetAlignment;
966 size_t rowBytes;
967 if (!this->glCaps().transferFromBufferRequirements(dstColorType, width, &rowBytes,
968 &offsetAlignment)) {
969 return false;
970 }
971 if (offset % offsetAlignment) {
972 return false;
973 }
974 if (offset + rowBytes * height > transferBuffer->size()) {
975 return false;
976 }
977
978 auto* glBuffer = static_cast<GrGLBuffer*>(transferBuffer);
979 this->bindBuffer(GrGpuBufferType::kXferGpuToCpu, glBuffer);
980
981 auto offsetAsPtr = reinterpret_cast<void*>(offset);
982 if (this->readOrTransferPixelsFrom(surface, left, top, width, height, dstColorType, offsetAsPtr,
983 rowBytes)) {
984 return rowBytes;
985 }
986 return 0;
987}
988
cblume55f2d2d2016-02-26 13:20:48 -0800989/**
990 * Creates storage space for the texture and fills it with texels.
991 *
Brian Salomond1eaf492017-05-18 10:02:08 -0400992 * @param config Pixel config of the texture.
cblume55f2d2d2016-02-26 13:20:48 -0800993 * @param interface The GL interface in use.
cblume790d5132016-02-29 11:13:29 -0800994 * @param caps The capabilities of the GL device.
Jim Van Verth1676cb92019-01-15 13:24:45 -0500995 * @param target Which bound texture to target (GR_GL_TEXTURE_2D, e.g.)
brianosman81a84852016-09-12 09:05:14 -0700996 * @param internalFormat The data format used for the internal storage of the texture. May be sized.
997 * @param internalFormatForTexStorage The data format used for the TexStorage API. Must be sized.
cblume55f2d2d2016-02-26 13:20:48 -0800998 * @param externalFormat The data format used for the external storage of the texture.
999 * @param externalType The type of the data used for the external storage of the texture.
1000 * @param texels The texel data of the texture being created.
Jim Van Verth1676cb92019-01-15 13:24:45 -05001001 * @param mipLevelCount Number of mipmap levels
cblume55f2d2d2016-02-26 13:20:48 -08001002 * @param baseWidth The width of the texture's base mipmap level
1003 * @param baseHeight The height of the texture's base mipmap level
cblume55f2d2d2016-02-26 13:20:48 -08001004 */
Robert Phillips92de6312017-05-23 07:43:48 -04001005static bool allocate_and_populate_texture(GrPixelConfig config,
1006 const GrGLInterface& interface,
1007 const GrGLCaps& caps,
1008 GrGLenum target,
1009 GrGLenum internalFormat,
1010 GrGLenum internalFormatForTexStorage,
1011 GrGLenum externalFormat,
1012 GrGLenum externalType,
Robert Phillips590533f2017-07-11 14:22:35 -04001013 const GrMipLevel texels[], int mipLevelCount,
Robert Phillips92de6312017-05-23 07:43:48 -04001014 int baseWidth, int baseHeight) {
cblume55f2d2d2016-02-26 13:20:48 -08001015 CLEAR_ERROR_BEFORE_ALLOC(&interface);
cblume790d5132016-02-29 11:13:29 -08001016
Brian Salomon7609ac62019-03-22 12:17:27 -04001017 if (caps.isConfigTexSupportEnabled(config)) {
cblume790d5132016-02-29 11:13:29 -08001018 // We never resize or change formats of textures.
cblume55f2d2d2016-02-26 13:20:48 -08001019 GL_ALLOC_CALL(&interface,
Robert Phillips590533f2017-07-11 14:22:35 -04001020 TexStorage2D(target, SkTMax(mipLevelCount, 1), internalFormatForTexStorage,
Brian Salomond1eaf492017-05-18 10:02:08 -04001021 baseWidth, baseHeight));
Brian Salomon0ec981b2017-05-15 13:48:50 -04001022 GrGLenum error = CHECK_ALLOC_ERROR(&interface);
cblume55f2d2d2016-02-26 13:20:48 -08001023 if (error != GR_GL_NO_ERROR) {
bsalomone699d0c2016-03-09 06:25:15 -08001024 return false;
cblume790d5132016-02-29 11:13:29 -08001025 } else {
Robert Phillips590533f2017-07-11 14:22:35 -04001026 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
cblume790d5132016-02-29 11:13:29 -08001027 const void* currentMipData = texels[currentMipLevel].fPixels;
1028 if (currentMipData == nullptr) {
1029 continue;
1030 }
1031 int twoToTheMipLevel = 1 << currentMipLevel;
Brian Salomond1eaf492017-05-18 10:02:08 -04001032 int currentWidth = SkTMax(1, baseWidth / twoToTheMipLevel);
1033 int currentHeight = SkTMax(1, baseHeight / twoToTheMipLevel);
cblume790d5132016-02-29 11:13:29 -08001034
1035 GR_GL_CALL(&interface,
1036 TexSubImage2D(target,
1037 currentMipLevel,
1038 0, // left
1039 0, // top
1040 currentWidth,
1041 currentHeight,
1042 externalFormat, externalType,
1043 currentMipData));
1044 }
bsalomone699d0c2016-03-09 06:25:15 -08001045 return true;
cblume790d5132016-02-29 11:13:29 -08001046 }
1047 } else {
Robert Phillips590533f2017-07-11 14:22:35 -04001048 if (!mipLevelCount) {
cblume790d5132016-02-29 11:13:29 -08001049 GL_ALLOC_CALL(&interface,
1050 TexImage2D(target,
bsalomone699d0c2016-03-09 06:25:15 -08001051 0,
cblume790d5132016-02-29 11:13:29 -08001052 internalFormat,
bsalomone699d0c2016-03-09 06:25:15 -08001053 baseWidth,
1054 baseHeight,
cblume790d5132016-02-29 11:13:29 -08001055 0, // border
1056 externalFormat, externalType,
bsalomone699d0c2016-03-09 06:25:15 -08001057 nullptr));
Brian Salomon0ec981b2017-05-15 13:48:50 -04001058 GrGLenum error = CHECK_ALLOC_ERROR(&interface);
cblume790d5132016-02-29 11:13:29 -08001059 if (error != GR_GL_NO_ERROR) {
bsalomone699d0c2016-03-09 06:25:15 -08001060 return false;
1061 }
1062 } else {
Robert Phillips590533f2017-07-11 14:22:35 -04001063 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
bsalomone699d0c2016-03-09 06:25:15 -08001064 int twoToTheMipLevel = 1 << currentMipLevel;
1065 int currentWidth = SkTMax(1, baseWidth / twoToTheMipLevel);
1066 int currentHeight = SkTMax(1, baseHeight / twoToTheMipLevel);
Greg Daniel8b059bd2017-09-28 20:46:45 +00001067 const void* currentMipData = texels[currentMipLevel].fPixels;
bsalomone699d0c2016-03-09 06:25:15 -08001068 // Even if curremtMipData is nullptr, continue to call TexImage2D.
1069 // This will allocate texture memory which we can later populate.
1070 GL_ALLOC_CALL(&interface,
1071 TexImage2D(target,
1072 currentMipLevel,
1073 internalFormat,
1074 currentWidth,
1075 currentHeight,
1076 0, // border
1077 externalFormat, externalType,
1078 currentMipData));
Brian Salomon0ec981b2017-05-15 13:48:50 -04001079 GrGLenum error = CHECK_ALLOC_ERROR(&interface);
bsalomone699d0c2016-03-09 06:25:15 -08001080 if (error != GR_GL_NO_ERROR) {
1081 return false;
1082 }
cblume790d5132016-02-29 11:13:29 -08001083 }
cblume55f2d2d2016-02-26 13:20:48 -08001084 }
1085 }
bsalomone699d0c2016-03-09 06:25:15 -08001086 return true;
cblume55f2d2d2016-02-26 13:20:48 -08001087}
1088
1089/**
Jim Van Verth1676cb92019-01-15 13:24:45 -05001090 * Creates storage space for the texture and fills it with texels.
1091 *
1092 * @param config Compressed pixel config of the texture.
1093 * @param interface The GL interface in use.
1094 * @param caps The capabilities of the GL device.
1095 * @param target Which bound texture to target (GR_GL_TEXTURE_2D, e.g.)
1096 * @param internalFormat The data format used for the internal storage of the texture.
1097 * @param texels The texel data of the texture being created.
1098 * @param mipLevelCount Number of mipmap levels
1099 * @param baseWidth The width of the texture's base mipmap level
1100 * @param baseHeight The height of the texture's base mipmap level
1101 */
1102static bool allocate_and_populate_compressed_texture(GrPixelConfig config,
1103 const GrGLInterface& interface,
1104 const GrGLCaps& caps,
1105 GrGLenum target, GrGLenum internalFormat,
1106 const GrMipLevel texels[], int mipLevelCount,
1107 int baseWidth, int baseHeight) {
1108 CLEAR_ERROR_BEFORE_ALLOC(&interface);
1109 SkASSERT(GrPixelConfigIsCompressed(config));
1110
1111 bool useTexStorage = caps.isConfigTexSupportEnabled(config);
1112 // We can only use TexStorage if we know we will not later change the storage requirements.
1113 // This means if we may later want to add mipmaps, we cannot use TexStorage.
1114 // Right now, we cannot know if we will later add mipmaps or not.
1115 // The only time we can use TexStorage is when we already have the
1116 // mipmaps.
1117 useTexStorage &= mipLevelCount > 1;
1118
1119 if (useTexStorage) {
1120 // We never resize or change formats of textures.
1121 GL_ALLOC_CALL(&interface,
1122 TexStorage2D(target,
1123 mipLevelCount,
1124 internalFormat,
1125 baseWidth, baseHeight));
1126 GrGLenum error = CHECK_ALLOC_ERROR(&interface);
1127 if (error != GR_GL_NO_ERROR) {
1128 return false;
1129 } else {
1130 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
1131 const void* currentMipData = texels[currentMipLevel].fPixels;
1132 if (currentMipData == nullptr) {
1133 // Compressed textures require data for every level
1134 return false;
1135 }
1136
1137 int twoToTheMipLevel = 1 << currentMipLevel;
1138 int currentWidth = SkTMax(1, baseWidth / twoToTheMipLevel);
1139 int currentHeight = SkTMax(1, baseHeight / twoToTheMipLevel);
1140
1141 // Make sure that the width and height that we pass to OpenGL
1142 // is a multiple of the block size.
1143 size_t dataSize = GrCompressedFormatDataSize(config, currentWidth, currentHeight);
1144 GR_GL_CALL(&interface, CompressedTexSubImage2D(target,
1145 currentMipLevel,
1146 0, // left
1147 0, // top
1148 currentWidth,
1149 currentHeight,
1150 internalFormat,
1151 SkToInt(dataSize),
1152 currentMipData));
1153 }
1154 }
1155 } else {
1156 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
1157 const void* currentMipData = texels[currentMipLevel].fPixels;
1158 if (currentMipData == nullptr) {
1159 // Compressed textures require data for every level
1160 return false;
1161 }
1162
1163 int twoToTheMipLevel = 1 << currentMipLevel;
1164 int currentWidth = SkTMax(1, baseWidth / twoToTheMipLevel);
1165 int currentHeight = SkTMax(1, baseHeight / twoToTheMipLevel);
1166
1167 // Make sure that the width and height that we pass to OpenGL
1168 // is a multiple of the block size.
1169 size_t dataSize = GrCompressedFormatDataSize(config, baseWidth, baseHeight);
1170
1171 GL_ALLOC_CALL(&interface,
1172 CompressedTexImage2D(target,
1173 currentMipLevel,
1174 internalFormat,
1175 currentWidth,
1176 currentHeight,
1177 0, // border
1178 SkToInt(dataSize),
1179 currentMipData));
1180
1181 GrGLenum error = CHECK_ALLOC_ERROR(&interface);
1182 if (error != GR_GL_NO_ERROR) {
1183 return false;
1184 }
1185 }
1186 }
1187
1188 return true;
1189}
1190/**
cblume55f2d2d2016-02-26 13:20:48 -08001191 * After a texture is created, any state which was altered during its creation
1192 * needs to be restored.
1193 *
1194 * @param interface The GL interface to use.
1195 * @param caps The capabilities of the GL device.
1196 * @param restoreGLRowLength Should the row length unpacking be restored?
1197 * @param glFlipY Did GL flip the texture vertically?
1198 */
1199static void restore_pixelstore_state(const GrGLInterface& interface, const GrGLCaps& caps,
Brian Salomona9b04b92018-06-01 15:04:28 -04001200 bool restoreGLRowLength) {
cblume55f2d2d2016-02-26 13:20:48 -08001201 if (restoreGLRowLength) {
1202 SkASSERT(caps.unpackRowLengthSupport());
1203 GR_GL_CALL(&interface, PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
1204 }
cblume55f2d2d2016-02-26 13:20:48 -08001205}
1206
Brian Osman9b560242017-09-05 15:34:52 -04001207void GrGLGpu::unbindCpuToGpuXferBuffer() {
Brian Salomonae64c192019-02-05 09:41:37 -05001208 auto* xferBufferState = this->hwBufferState(GrGpuBufferType::kXferCpuToGpu);
1209 if (!xferBufferState->fBoundBufferUniqueID.isInvalid()) {
1210 GL_CALL(BindBuffer(xferBufferState->fGLTarget, 0));
1211 xferBufferState->invalidate();
Brian Osman9b560242017-09-05 15:34:52 -04001212 }
Brian Osman9b560242017-09-05 15:34:52 -04001213}
1214
Brian Salomonc320b152018-02-20 14:05:36 -05001215// TODO: Make this take a GrColorType instead of dataConfig. This requires updating GrGLCaps to
1216// convert from GrColorType to externalFormat/externalType GLenum values.
Brian Salomona9b04b92018-06-01 15:04:28 -04001217bool GrGLGpu::uploadTexData(GrPixelConfig texConfig, int texWidth, int texHeight, GrGLenum target,
1218 UploadType uploadType, int left, int top, int width, int height,
1219 GrPixelConfig dataConfig, const GrMipLevel texels[], int mipLevelCount,
Greg Daniel0fc4d2d2017-10-12 11:23:36 -04001220 GrMipMapsStatus* mipMapsStatus) {
Jim Van Verth1676cb92019-01-15 13:24:45 -05001221 // If we're uploading compressed data then we should be using uploadCompressedTexData
1222 SkASSERT(!GrPixelConfigIsCompressed(dataConfig));
1223
Brian Salomond1eaf492017-05-18 10:02:08 -04001224 SkASSERT(this->caps()->isConfigTexturable(texConfig));
Greg Daniel660cc992017-06-26 14:55:05 -04001225 SkDEBUGCODE(
1226 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
1227 SkIRect bounds = SkIRect::MakeWH(texWidth, texHeight);
1228 SkASSERT(bounds.contains(subRect));
1229 )
Robert Phillips590533f2017-07-11 14:22:35 -04001230 SkASSERT(1 == mipLevelCount ||
Greg Daniel660cc992017-06-26 14:55:05 -04001231 (0 == left && 0 == top && width == texWidth && height == texHeight));
bsalomon5b30c6f2015-12-17 14:17:34 -08001232
Brian Osman9b560242017-09-05 15:34:52 -04001233 this->unbindCpuToGpuXferBuffer();
Jim Van Verth2e5eaf02017-06-21 15:55:46 -04001234
cblume55f2d2d2016-02-26 13:20:48 -08001235 // texels is const.
1236 // But we may need to flip the texture vertically to prepare it.
1237 // Rather than flip in place and alter the incoming data,
1238 // we allocate a new buffer to flip into.
1239 // This means we need to make a non-const shallow copy of texels.
Robert Phillips0f992772017-07-12 08:24:56 -04001240 SkAutoTMalloc<GrMipLevel> texelsShallowCopy;
1241
1242 if (mipLevelCount) {
1243 texelsShallowCopy.reset(mipLevelCount);
1244 memcpy(texelsShallowCopy.get(), texels, mipLevelCount*sizeof(GrMipLevel));
1245 }
cblume55f2d2d2016-02-26 13:20:48 -08001246
cblume55f2d2d2016-02-26 13:20:48 -08001247 const GrGLInterface* interface = this->glInterface();
1248 const GrGLCaps& caps = this->glCaps();
1249
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001250 size_t bpp = GrBytesPerPixel(dataConfig);
cblume55f2d2d2016-02-26 13:20:48 -08001251
1252 if (width == 0 || height == 0) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +00001253 return false;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001254 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00001255
bsalomon5b30c6f2015-12-17 14:17:34 -08001256 // Internal format comes from the texture desc.
bsalomon76148af2016-01-12 11:13:47 -08001257 GrGLenum internalFormat;
bsalomon5b30c6f2015-12-17 14:17:34 -08001258 // External format and type come from the upload data.
bsalomon76148af2016-01-12 11:13:47 -08001259 GrGLenum externalFormat;
1260 GrGLenum externalType;
Brian Salomond1eaf492017-05-18 10:02:08 -04001261 if (!this->glCaps().getTexImageFormats(texConfig, dataConfig, &internalFormat, &externalFormat,
1262 &externalType)) {
bsalomon76148af2016-01-12 11:13:47 -08001263 return false;
1264 }
brianosman81a84852016-09-12 09:05:14 -07001265 // TexStorage requires a sized format, and internalFormat may or may not be
Brian Salomond1eaf492017-05-18 10:02:08 -04001266 GrGLenum internalFormatForTexStorage = this->glCaps().configSizedInternalFormat(texConfig);
brianosman81a84852016-09-12 09:05:14 -07001267
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001268 /*
bsalomon5b30c6f2015-12-17 14:17:34 -08001269 * Check whether to allocate a temporary buffer for flipping y or
bsalomon@google.com6f379512011-11-16 20:36:03 +00001270 * because our srcData has extra bytes past each row. If so, we need
1271 * to trim those off here, since GL ES may not let us specify
1272 * GL_UNPACK_ROW_LENGTH.
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001273 */
bsalomon@google.com6f379512011-11-16 20:36:03 +00001274 bool restoreGLRowLength = false;
cblume55f2d2d2016-02-26 13:20:48 -08001275
1276 // in case we need a temporary, trimmed copy of the src pixels
1277 SkAutoSMalloc<128 * 128> tempStorage;
1278
Greg Daniel0fc4d2d2017-10-12 11:23:36 -04001279 if (mipMapsStatus) {
1280 *mipMapsStatus = GrMipMapsStatus::kValid;
Greg Daniel834f1202017-10-09 15:06:20 -04001281 }
1282
Robert Phillips931f7512017-11-02 12:57:18 -04001283 const bool usesMips = mipLevelCount > 1;
1284
cblume55f2d2d2016-02-26 13:20:48 -08001285 // find the combined size of all the mip levels and the relative offset of
1286 // each into the collective buffer
Robert Phillips931f7512017-11-02 12:57:18 -04001287 bool willNeedData = false;
Greg Daniel55afd6d2017-09-29 09:32:44 -04001288 size_t combinedBufferSize = 0;
1289 SkTArray<size_t> individualMipOffsets(mipLevelCount);
Robert Phillips590533f2017-07-11 14:22:35 -04001290 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
Greg Daniel55afd6d2017-09-29 09:32:44 -04001291 if (texelsShallowCopy[currentMipLevel].fPixels) {
1292 int twoToTheMipLevel = 1 << currentMipLevel;
1293 int currentWidth = SkTMax(1, width / twoToTheMipLevel);
1294 int currentHeight = SkTMax(1, height / twoToTheMipLevel);
Robert Phillips931f7512017-11-02 12:57:18 -04001295 const size_t trimRowBytes = currentWidth * bpp;
1296 const size_t trimmedSize = trimRowBytes * currentHeight;
1297
1298 const size_t rowBytes = texelsShallowCopy[currentMipLevel].fRowBytes
1299 ? texelsShallowCopy[currentMipLevel].fRowBytes
1300 : trimRowBytes;
1301
Brian Salomona9b04b92018-06-01 15:04:28 -04001302 if (((!caps.unpackRowLengthSupport() || usesMips) && trimRowBytes != rowBytes)) {
Robert Phillips931f7512017-11-02 12:57:18 -04001303 willNeedData = true;
1304 }
1305
Greg Daniel55afd6d2017-09-29 09:32:44 -04001306 individualMipOffsets.push_back(combinedBufferSize);
1307 combinedBufferSize += trimmedSize;
1308 } else {
Greg Daniel0fc4d2d2017-10-12 11:23:36 -04001309 if (mipMapsStatus) {
1310 *mipMapsStatus = GrMipMapsStatus::kDirty;
Greg Daniel834f1202017-10-09 15:06:20 -04001311 }
Greg Daniel55afd6d2017-09-29 09:32:44 -04001312 individualMipOffsets.push_back(0);
1313 }
Greg Daniel0fc4d2d2017-10-12 11:23:36 -04001314 }
1315 if (mipMapsStatus && mipLevelCount <= 1) {
1316 *mipMapsStatus = GrMipMapsStatus::kNotAllocated;
cblume55f2d2d2016-02-26 13:20:48 -08001317 }
Robert Phillips931f7512017-11-02 12:57:18 -04001318 char* buffer = nullptr;
1319 if (willNeedData) {
1320 buffer = (char*)tempStorage.reset(combinedBufferSize);
1321 }
cblume55f2d2d2016-02-26 13:20:48 -08001322
Robert Phillips590533f2017-07-11 14:22:35 -04001323 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
Greg Daniel55afd6d2017-09-29 09:32:44 -04001324 if (!texelsShallowCopy[currentMipLevel].fPixels) {
1325 continue;
1326 }
cblume55f2d2d2016-02-26 13:20:48 -08001327 int twoToTheMipLevel = 1 << currentMipLevel;
1328 int currentWidth = SkTMax(1, width / twoToTheMipLevel);
1329 int currentHeight = SkTMax(1, height / twoToTheMipLevel);
1330 const size_t trimRowBytes = currentWidth * bpp;
1331
1332 /*
1333 * check whether to allocate a temporary buffer for flipping y or
1334 * because our srcData has extra bytes past each row. If so, we need
1335 * to trim those off here, since GL ES may not let us specify
1336 * GL_UNPACK_ROW_LENGTH.
1337 */
1338 restoreGLRowLength = false;
1339
Greg Daniel55afd6d2017-09-29 09:32:44 -04001340 const size_t rowBytes = texelsShallowCopy[currentMipLevel].fRowBytes
1341 ? texelsShallowCopy[currentMipLevel].fRowBytes
1342 : trimRowBytes;
Greg Daniel660cc992017-06-26 14:55:05 -04001343
ericrk154349b2016-05-10 14:36:53 -07001344 // TODO: This optimization should be enabled with or without mips.
1345 // For use with mips, we must set GR_GL_UNPACK_ROW_LENGTH once per
1346 // mip level, before calling glTexImage2D.
Brian Salomona9b04b92018-06-01 15:04:28 -04001347 if (caps.unpackRowLengthSupport() && !usesMips) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001348 // can't use this for flipping, only non-neg values allowed. :(
1349 if (rowBytes != trimRowBytes) {
1350 GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
cblume55f2d2d2016-02-26 13:20:48 -08001351 GR_GL_CALL(interface, PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength));
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001352 restoreGLRowLength = true;
1353 }
Brian Salomona9b04b92018-06-01 15:04:28 -04001354 } else if (trimRowBytes != rowBytes) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -04001355 // copy data into our new storage, skipping the trailing bytes
1356 const char* src = (const char*)texelsShallowCopy[currentMipLevel].fPixels;
Greg Daniel55afd6d2017-09-29 09:32:44 -04001357 char* dst = buffer + individualMipOffsets[currentMipLevel];
Brian Salomona6948702018-06-01 15:33:20 -04001358 SkRectMemcpy(dst, trimRowBytes, src, rowBytes, trimRowBytes, currentHeight);
Jim Van Verth2e5eaf02017-06-21 15:55:46 -04001359 // now point data to our copied version
1360 texelsShallowCopy[currentMipLevel].fPixels = buffer +
Greg Daniel55afd6d2017-09-29 09:32:44 -04001361 individualMipOffsets[currentMipLevel];
Jim Van Verth2e5eaf02017-06-21 15:55:46 -04001362 texelsShallowCopy[currentMipLevel].fRowBytes = trimRowBytes;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001363 }
bsalomone699d0c2016-03-09 06:25:15 -08001364 }
1365
Robert Phillips590533f2017-07-11 14:22:35 -04001366 if (mipLevelCount) {
Brian Salomond1eaf492017-05-18 10:02:08 -04001367 GR_GL_CALL(interface, PixelStorei(GR_GL_UNPACK_ALIGNMENT, config_alignment(texConfig)));
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001368 }
cblume55f2d2d2016-02-26 13:20:48 -08001369
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001370 bool succeeded = true;
Brian Salomond1eaf492017-05-18 10:02:08 -04001371 if (kNewTexture_UploadType == uploadType) {
1372 if (0 == left && 0 == top && texWidth == width && texHeight == height) {
Robert Phillips92de6312017-05-23 07:43:48 -04001373 succeeded = allocate_and_populate_texture(
Brian Salomond1eaf492017-05-18 10:02:08 -04001374 texConfig, *interface, caps, target, internalFormat,
Robert Phillips590533f2017-07-11 14:22:35 -04001375 internalFormatForTexStorage, externalFormat, externalType,
1376 texelsShallowCopy, mipLevelCount, width, height);
Brian Salomond1eaf492017-05-18 10:02:08 -04001377 } else {
1378 succeeded = false;
1379 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001380 } else {
Robert Phillips590533f2017-07-11 14:22:35 -04001381 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
Greg Daniel55afd6d2017-09-29 09:32:44 -04001382 if (!texelsShallowCopy[currentMipLevel].fPixels) {
1383 continue;
1384 }
cblume55f2d2d2016-02-26 13:20:48 -08001385 int twoToTheMipLevel = 1 << currentMipLevel;
1386 int currentWidth = SkTMax(1, width / twoToTheMipLevel);
1387 int currentHeight = SkTMax(1, height / twoToTheMipLevel);
cblume55f2d2d2016-02-26 13:20:48 -08001388
1389 GL_CALL(TexSubImage2D(target,
1390 currentMipLevel,
1391 left, top,
1392 currentWidth,
1393 currentHeight,
1394 externalFormat, externalType,
1395 texelsShallowCopy[currentMipLevel].fPixels));
1396 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00001397 }
1398
Brian Salomona9b04b92018-06-01 15:04:28 -04001399 restore_pixelstore_state(*interface, caps, restoreGLRowLength);
cblume55f2d2d2016-02-26 13:20:48 -08001400
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001401 return succeeded;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001402}
1403
Jim Van Verth1676cb92019-01-15 13:24:45 -05001404bool GrGLGpu::uploadCompressedTexData(GrPixelConfig texConfig, int texWidth, int texHeight,
1405 GrGLenum target, GrPixelConfig dataConfig,
1406 const GrMipLevel texels[], int mipLevelCount,
1407 GrMipMapsStatus* mipMapsStatus) {
1408 SkASSERT(this->caps()->isConfigTexturable(texConfig));
1409
1410 const GrGLInterface* interface = this->glInterface();
1411 const GrGLCaps& caps = this->glCaps();
1412
1413 // We only need the internal format for compressed 2D textures.
1414 GrGLenum internalFormat;
1415 if (!caps.getCompressedTexImageFormats(texConfig, &internalFormat)) {
1416 return false;
1417 }
1418
Greg Kaiser73bfb892019-02-11 09:03:41 -08001419 if (mipMapsStatus) {
1420 if (mipLevelCount <= 1) {
1421 *mipMapsStatus = GrMipMapsStatus::kNotAllocated;
1422 } else {
1423 *mipMapsStatus = GrMipMapsStatus::kValid;
1424 }
Jim Van Verth1676cb92019-01-15 13:24:45 -05001425 }
1426
1427 return allocate_and_populate_compressed_texture(texConfig, *interface, caps, target,
1428 internalFormat, texels, mipLevelCount,
1429 texWidth, texHeight);
Jim Van Verth1676cb92019-01-15 13:24:45 -05001430}
1431
bsalomon424cc262015-05-22 10:37:30 -07001432static bool renderbuffer_storage_msaa(const GrGLContext& ctx,
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001433 int sampleCount,
1434 GrGLenum format,
1435 int width, int height) {
robertphillips@google.com6177e692013-02-28 20:16:25 +00001436 CLEAR_ERROR_BEFORE_ALLOC(ctx.interface());
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +00001437 SkASSERT(GrGLCaps::kNone_MSFBOType != ctx.caps()->msFBOType());
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +00001438 switch (ctx.caps()->msFBOType()) {
Brian Salomon00731b42016-10-14 11:30:51 -04001439 case GrGLCaps::kStandard_MSFBOType:
vbuzinovdded6962015-06-12 08:59:45 -07001440 case GrGLCaps::kMixedSamples_MSFBOType:
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001441 GL_ALLOC_CALL(ctx.interface(),
1442 RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
1443 sampleCount,
1444 format,
1445 width, height));
1446 break;
1447 case GrGLCaps::kES_Apple_MSFBOType:
1448 GL_ALLOC_CALL(ctx.interface(),
1449 RenderbufferStorageMultisampleES2APPLE(GR_GL_RENDERBUFFER,
1450 sampleCount,
1451 format,
1452 width, height));
1453 break;
1454 case GrGLCaps::kES_EXT_MsToTexture_MSFBOType:
1455 case GrGLCaps::kES_IMG_MsToTexture_MSFBOType:
1456 GL_ALLOC_CALL(ctx.interface(),
1457 RenderbufferStorageMultisampleES2EXT(GR_GL_RENDERBUFFER,
1458 sampleCount,
1459 format,
1460 width, height));
1461 break;
1462 case GrGLCaps::kNone_MSFBOType:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -04001463 SK_ABORT("Shouldn't be here if we don't support multisampled renderbuffers.");
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001464 break;
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001465 }
Brian Salomon9251d4e2015-03-17 16:03:19 -04001466 return (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctx.interface()));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001467}
1468
egdanielb0e1be22015-04-22 13:27:39 -07001469bool GrGLGpu::createRenderTargetObjects(const GrSurfaceDesc& desc,
bsalomon091f60c2015-11-10 11:54:56 -08001470 const GrGLTextureInfo& texInfo,
bsalomonb15b4c12014-10-29 12:41:57 -07001471 GrGLRenderTarget::IDDesc* idDesc) {
1472 idDesc->fMSColorRenderbufferID = 0;
egdanield803f272015-03-18 13:01:52 -07001473 idDesc->fRTFBOID = 0;
kkinnunen2e6055b2016-04-22 01:48:29 -07001474 idDesc->fRTFBOOwnership = GrBackendObjectOwnership::kOwned;
egdanield803f272015-03-18 13:01:52 -07001475 idDesc->fTexFBOID = 0;
robertphillips76948d42016-05-04 12:47:41 -07001476 SkASSERT((GrGLCaps::kMixedSamples_MSFBOType == this->glCaps().msFBOType()) ==
1477 this->caps()->usesMixedSamples());
Brian Salomonbdecacf2018-02-02 20:32:49 -05001478 idDesc->fIsMixedSampled = desc.fSampleCnt > 1 && this->caps()->usesMixedSamples();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001479
1480 GrGLenum status;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001481
bsalomona11e5fc2015-12-18 07:59:41 -08001482 GrGLenum colorRenderbufferFormat = 0; // suppress warning
bsalomon@google.comab15d612011-08-09 12:57:56 +00001483
Brian Salomonbdecacf2018-02-02 20:32:49 -05001484 if (desc.fSampleCnt > 1 && GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.com347c3822013-05-01 20:10:01 +00001485 goto FAILED;
1486 }
1487
egdanield803f272015-03-18 13:01:52 -07001488 GL_CALL(GenFramebuffers(1, &idDesc->fTexFBOID));
1489 if (!idDesc->fTexFBOID) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001490 goto FAILED;
1491 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001492
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001493 // If we are using multisampling we will create two FBOS. We render to one and then resolve to
1494 // the texture bound to the other. The exception is the IMG multisample extension. With this
1495 // extension the texture is multisampled when rendered to and then auto-resolves it when it is
1496 // rendered from.
Brian Salomonbdecacf2018-02-02 20:32:49 -05001497 if (desc.fSampleCnt > 1 && this->glCaps().usesMSAARenderBuffers()) {
egdanield803f272015-03-18 13:01:52 -07001498 GL_CALL(GenFramebuffers(1, &idDesc->fRTFBOID));
bsalomonb15b4c12014-10-29 12:41:57 -07001499 GL_CALL(GenRenderbuffers(1, &idDesc->fMSColorRenderbufferID));
egdanield803f272015-03-18 13:01:52 -07001500 if (!idDesc->fRTFBOID ||
bsalomona11e5fc2015-12-18 07:59:41 -08001501 !idDesc->fMSColorRenderbufferID) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001502 goto FAILED;
1503 }
Brian Salomon93348dd2018-08-29 12:56:23 -04001504 this->glCaps().getRenderbufferFormat(desc.fConfig, &colorRenderbufferFormat);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001505 } else {
egdanield803f272015-03-18 13:01:52 -07001506 idDesc->fRTFBOID = idDesc->fTexFBOID;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001507 }
1508
egdanield803f272015-03-18 13:01:52 -07001509 // below here we may bind the FBO
Robert Phillips294870f2016-11-11 12:38:40 -05001510 fHWBoundRenderTargetUniqueID.makeInvalid();
egdanield803f272015-03-18 13:01:52 -07001511 if (idDesc->fRTFBOID != idDesc->fTexFBOID) {
Brian Salomonbdecacf2018-02-02 20:32:49 -05001512 SkASSERT(desc.fSampleCnt > 1);
bsalomonb15b4c12014-10-29 12:41:57 -07001513 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, idDesc->fMSColorRenderbufferID));
bsalomon424cc262015-05-22 10:37:30 -07001514 if (!renderbuffer_storage_msaa(*fGLContext,
bsalomonb15b4c12014-10-29 12:41:57 -07001515 desc.fSampleCnt,
bsalomona11e5fc2015-12-18 07:59:41 -08001516 colorRenderbufferFormat,
bsalomonb15b4c12014-10-29 12:41:57 -07001517 desc.fWidth, desc.fHeight)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001518 goto FAILED;
1519 }
Adrienne Walker4ee88512018-05-17 11:37:14 -07001520 this->bindFramebuffer(GR_GL_FRAMEBUFFER, idDesc->fRTFBOID);
egdanield803f272015-03-18 13:01:52 -07001521 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon10528f12015-10-14 12:54:52 -07001522 GR_GL_COLOR_ATTACHMENT0,
1523 GR_GL_RENDERBUFFER,
1524 idDesc->fMSColorRenderbufferID));
Brian Salomon0ec981b2017-05-15 13:48:50 -04001525 if (!this->glCaps().isConfigVerifiedColorAttachment(desc.fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001526 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1527 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
1528 goto FAILED;
1529 }
bsalomon424cc262015-05-22 10:37:30 -07001530 fGLContext->caps()->markConfigAsValidColorAttachment(desc.fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001531 }
1532 }
Adrienne Walker4ee88512018-05-17 11:37:14 -07001533 this->bindFramebuffer(GR_GL_FRAMEBUFFER, idDesc->fTexFBOID);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001534
Brian Salomonbdecacf2018-02-02 20:32:49 -05001535 if (this->glCaps().usesImplicitMSAAResolve() && desc.fSampleCnt > 1) {
egdanield803f272015-03-18 13:01:52 -07001536 GL_CALL(FramebufferTexture2DMultisample(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001537 GR_GL_COLOR_ATTACHMENT0,
bsalomon091f60c2015-11-10 11:54:56 -08001538 texInfo.fTarget,
1539 texInfo.fID, 0, desc.fSampleCnt));
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001540 } else {
egdanield803f272015-03-18 13:01:52 -07001541 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001542 GR_GL_COLOR_ATTACHMENT0,
bsalomon091f60c2015-11-10 11:54:56 -08001543 texInfo.fTarget,
1544 texInfo.fID, 0));
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001545 }
Brian Salomon0ec981b2017-05-15 13:48:50 -04001546 if (!this->glCaps().isConfigVerifiedColorAttachment(desc.fConfig)) {
egdanield803f272015-03-18 13:01:52 -07001547 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001548 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
1549 goto FAILED;
1550 }
bsalomon424cc262015-05-22 10:37:30 -07001551 fGLContext->caps()->markConfigAsValidColorAttachment(desc.fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001552 }
1553
1554 return true;
1555
1556FAILED:
bsalomonb15b4c12014-10-29 12:41:57 -07001557 if (idDesc->fMSColorRenderbufferID) {
1558 GL_CALL(DeleteRenderbuffers(1, &idDesc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001559 }
egdanield803f272015-03-18 13:01:52 -07001560 if (idDesc->fRTFBOID != idDesc->fTexFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -07001561 this->deleteFramebuffer(idDesc->fRTFBOID);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001562 }
egdanield803f272015-03-18 13:01:52 -07001563 if (idDesc->fTexFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -07001564 this->deleteFramebuffer(idDesc->fTexFBOID);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001565 }
1566 return false;
1567}
1568
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001569// good to set a break-point here to know when createTexture fails
Robert Phillips67d52cf2017-06-05 13:38:13 -04001570static sk_sp<GrTexture> return_null_texture() {
mtklein@google.com330313a2013-08-22 15:37:26 +00001571// SkDEBUGFAIL("null texture");
halcanary96fcdcc2015-08-27 07:41:13 -07001572 return nullptr;
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001573}
1574
Brian Salomondc829942018-10-23 16:07:24 -04001575static GrGLTexture::SamplerParams set_initial_texture_params(const GrGLInterface* interface,
1576 const GrGLTextureInfo& info) {
cblume55f2d2d2016-02-26 13:20:48 -08001577 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
1578 // drivers have a bug where an FBO won't be complete if it includes a
1579 // texture that is not mipmap complete (considering the filter in use).
Brian Salomondc829942018-10-23 16:07:24 -04001580 GrGLTexture::SamplerParams params;
1581 params.fMinFilter = GR_GL_NEAREST;
1582 params.fMagFilter = GR_GL_NEAREST;
1583 params.fWrapS = GR_GL_CLAMP_TO_EDGE;
1584 params.fWrapT = GR_GL_CLAMP_TO_EDGE;
1585 GR_GL_CALL(interface, TexParameteri(info.fTarget, GR_GL_TEXTURE_MAG_FILTER, params.fMagFilter));
1586 GR_GL_CALL(interface, TexParameteri(info.fTarget, GR_GL_TEXTURE_MIN_FILTER, params.fMinFilter));
1587 GR_GL_CALL(interface, TexParameteri(info.fTarget, GR_GL_TEXTURE_WRAP_S, params.fWrapS));
1588 GR_GL_CALL(interface, TexParameteri(info.fTarget, GR_GL_TEXTURE_WRAP_T, params.fWrapT));
1589 return params;
cblume55f2d2d2016-02-26 13:20:48 -08001590}
1591
Robert Phillips67d52cf2017-06-05 13:38:13 -04001592sk_sp<GrTexture> GrGLGpu::onCreateTexture(const GrSurfaceDesc& desc,
1593 SkBudgeted budgeted,
Robert Phillips590533f2017-07-11 14:22:35 -04001594 const GrMipLevel texels[],
1595 int mipLevelCount) {
bsalomon@google.com8a70eef2013-03-19 13:58:55 +00001596 // We fail if the MSAA was requested and is not available.
Brian Salomonbdecacf2018-02-02 20:32:49 -05001597 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType() && desc.fSampleCnt > 1) {
tfarina38406c82014-10-31 07:11:12 -07001598 //SkDebugf("MSAA RT requested but not supported on this platform.");
bsalomon@google.com8a70eef2013-03-19 13:58:55 +00001599 return return_null_texture();
1600 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001601
Jim Van Verth1676cb92019-01-15 13:24:45 -05001602 bool performClear = (desc.fFlags & kPerformInitialClear_GrSurfaceFlag) &&
1603 !GrPixelConfigIsCompressed(desc.fConfig);
Brian Salomond17b4a62017-05-23 16:53:47 -04001604
Robert Phillips590533f2017-07-11 14:22:35 -04001605 GrMipLevel zeroLevel;
Brian Salomond17b4a62017-05-23 16:53:47 -04001606 std::unique_ptr<uint8_t[]> zeros;
Brian Salomond17b4a62017-05-23 16:53:47 -04001607 if (performClear && !this->glCaps().clearTextureSupport() &&
Brian Salomon57111332018-02-05 15:55:54 -05001608 !this->glCaps().canConfigBeFBOColorAttachment(desc.fConfig)) {
Brian Salomond17b4a62017-05-23 16:53:47 -04001609 size_t rowSize = GrBytesPerPixel(desc.fConfig) * desc.fWidth;
1610 size_t size = rowSize * desc.fHeight;
1611 zeros.reset(new uint8_t[size]);
1612 memset(zeros.get(), 0, size);
Robert Phillips590533f2017-07-11 14:22:35 -04001613 zeroLevel.fPixels = zeros.get();
1614 zeroLevel.fRowBytes = 0;
1615 texels = &zeroLevel;
1616 mipLevelCount = 1;
Brian Salomond17b4a62017-05-23 16:53:47 -04001617 performClear = false;
1618 }
1619
Brian Salomond34edf32017-05-19 15:45:48 -04001620 bool isRenderTarget = SkToBool(desc.fFlags & kRenderTarget_GrSurfaceFlag);
reed@google.comac10a2d2010-12-22 21:39:39 +00001621
bsalomonb15b4c12014-10-29 12:41:57 -07001622 GrGLTexture::IDDesc idDesc;
kkinnunen2e6055b2016-04-22 01:48:29 -07001623 idDesc.fOwnership = GrBackendObjectOwnership::kOwned;
Greg Daniel0fc4d2d2017-10-12 11:23:36 -04001624 GrMipMapsStatus mipMapsStatus;
Brian Salomondc829942018-10-23 16:07:24 -04001625 GrGLTexture::SamplerParams initialTexParams;
Brian Salomon58389b92018-03-07 13:01:25 -05001626 if (!this->createTextureImpl(desc, &idDesc.fInfo, isRenderTarget, &initialTexParams, texels,
1627 mipLevelCount, &mipMapsStatus)) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001628 return return_null_texture();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001629 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001630
Robert Phillips67d52cf2017-06-05 13:38:13 -04001631 sk_sp<GrGLTexture> tex;
Brian Salomond34edf32017-05-19 15:45:48 -04001632 if (isRenderTarget) {
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001633 // unbind the texture from the texture unit before binding it to the frame buffer
bsalomon091f60c2015-11-10 11:54:56 -08001634 GL_CALL(BindTexture(idDesc.fInfo.fTarget, 0));
bsalomon5236cf42015-01-14 10:42:08 -08001635 GrGLRenderTarget::IDDesc rtIDDesc;
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001636
kkinnunen2e6055b2016-04-22 01:48:29 -07001637 if (!this->createRenderTargetObjects(desc, idDesc.fInfo, &rtIDDesc)) {
bsalomon091f60c2015-11-10 11:54:56 -08001638 GL_CALL(DeleteTextures(1, &idDesc.fInfo.fID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001639 return return_null_texture();
1640 }
Robert Phillips67d52cf2017-06-05 13:38:13 -04001641 tex = sk_make_sp<GrGLTextureRenderTarget>(this, budgeted, desc, idDesc, rtIDDesc,
Greg Daniel0fc4d2d2017-10-12 11:23:36 -04001642 mipMapsStatus);
Brian Salomon9bada542017-06-12 12:09:30 -04001643 tex->baseLevelWasBoundToFBO();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001644 } else {
Greg Daniel0fc4d2d2017-10-12 11:23:36 -04001645 tex = sk_make_sp<GrGLTexture>(this, budgeted, desc, idDesc, mipMapsStatus);
reed@google.comac10a2d2010-12-22 21:39:39 +00001646 }
Brian Salomondc829942018-10-23 16:07:24 -04001647
1648 tex->setCachedParams(&initialTexParams, tex->getCachedNonSamplerParams(),
1649 this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00001650#ifdef TRACE_TEXTURE_CREATION
tfarina38406c82014-10-31 07:11:12 -07001651 SkDebugf("--- new texture [%d] size=(%d %d) config=%d\n",
brianosmanc4459f62016-07-19 08:02:20 -07001652 idDesc.fInfo.fID, desc.fWidth, desc.fHeight, desc.fConfig);
reed@google.comac10a2d2010-12-22 21:39:39 +00001653#endif
Brian Salomond17b4a62017-05-23 16:53:47 -04001654 if (tex && performClear) {
1655 if (this->glCaps().clearTextureSupport()) {
Brian Salomond17b4a62017-05-23 16:53:47 -04001656 static constexpr uint32_t kZero = 0;
Brian Salomon57111332018-02-05 15:55:54 -05001657 GL_CALL(ClearTexImage(tex->textureID(), 0, GR_GL_RGBA, GR_GL_UNSIGNED_BYTE, &kZero));
Brian Salomond17b4a62017-05-23 16:53:47 -04001658 } else {
Brian Salomond17b4a62017-05-23 16:53:47 -04001659 GrGLIRect viewport;
Robert Phillips67d52cf2017-06-05 13:38:13 -04001660 this->bindSurfaceFBOForPixelOps(tex.get(), GR_GL_FRAMEBUFFER, &viewport,
1661 kDst_TempFBOTarget);
Brian Salomond818ebf2018-07-02 14:08:49 +00001662 this->disableScissor();
Brian Salomond17b4a62017-05-23 16:53:47 -04001663 this->disableWindowRectangles();
Brian Salomon805cc7a2019-01-28 09:52:34 -05001664 this->flushColorWrite(true);
1665 this->flushClearColor(0, 0, 0, 0);
Brian Salomond17b4a62017-05-23 16:53:47 -04001666 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
Robert Phillips67d52cf2017-06-05 13:38:13 -04001667 this->unbindTextureFBOForPixelOps(GR_GL_FRAMEBUFFER, tex.get());
Brian Salomond17b4a62017-05-23 16:53:47 -04001668 fHWBoundRenderTargetUniqueID.makeInvalid();
1669 }
1670 }
Kevin Lubickf7621cb2018-04-16 15:51:44 -04001671 return std::move(tex);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001672}
1673
1674namespace {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001675
egdaniel8dc7c3a2015-04-16 11:22:42 -07001676const GrGLuint kUnknownBitCount = GrGLStencilAttachment::kUnknownBitCount;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001677
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001678void inline get_stencil_rb_sizes(const GrGLInterface* gl,
egdaniel8dc7c3a2015-04-16 11:22:42 -07001679 GrGLStencilAttachment::Format* format) {
sugoi@google.com6ba0b0f2013-05-03 13:52:32 +00001680
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001681 // we shouldn't ever know one size and not the other
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001682 SkASSERT((kUnknownBitCount == format->fStencilBits) ==
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001683 (kUnknownBitCount == format->fTotalBits));
1684 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001685 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001686 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1687 (GrGLint*)&format->fStencilBits);
1688 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001689 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001690 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1691 (GrGLint*)&format->fTotalBits);
1692 format->fTotalBits += format->fStencilBits;
1693 } else {
1694 format->fTotalBits = format->fStencilBits;
1695 }
1696 }
1697}
1698}
1699
egdanielff1d5472015-09-10 08:37:20 -07001700int GrGLGpu::getCompatibleStencilIndex(GrPixelConfig config) {
bsalomon100b8f82015-10-28 08:37:44 -07001701 static const int kSize = 16;
Brian Salomonbdecacf2018-02-02 20:32:49 -05001702 SkASSERT(this->caps()->isConfigRenderable(config));
bsalomon30447372015-12-21 09:03:05 -08001703 if (!this->glCaps().hasStencilFormatBeenDeterminedForConfig(config)) {
1704 // Default to unsupported, set this if we find a stencil format that works.
1705 int firstWorkingStencilFormatIndex = -1;
Brian Osman91f9a2c2017-09-05 15:02:46 -04001706
egdanielff1d5472015-09-10 08:37:20 -07001707 // Create color texture
kkinnunen546eb5c2015-12-11 00:05:33 -08001708 GrGLuint colorID = 0;
egdanielff1d5472015-09-10 08:37:20 -07001709 GL_CALL(GenTextures(1, &colorID));
Brian Salomond978b902019-02-07 15:09:18 -05001710 this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, colorID);
egdanielff1d5472015-09-10 08:37:20 -07001711 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1712 GR_GL_TEXTURE_MAG_FILTER,
1713 GR_GL_NEAREST));
1714 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1715 GR_GL_TEXTURE_MIN_FILTER,
1716 GR_GL_NEAREST));
1717 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1718 GR_GL_TEXTURE_WRAP_S,
1719 GR_GL_CLAMP_TO_EDGE));
1720 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1721 GR_GL_TEXTURE_WRAP_T,
1722 GR_GL_CLAMP_TO_EDGE));
1723
bsalomon76148af2016-01-12 11:13:47 -08001724 GrGLenum internalFormat;
1725 GrGLenum externalFormat;
1726 GrGLenum externalType;
1727 if (!this->glCaps().getTexImageFormats(config, config, &internalFormat, &externalFormat,
1728 &externalType)) {
1729 return false;
1730 }
Brian Osman9b560242017-09-05 15:34:52 -04001731 this->unbindCpuToGpuXferBuffer();
egdanielff1d5472015-09-10 08:37:20 -07001732 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1733 GL_ALLOC_CALL(this->glInterface(), TexImage2D(GR_GL_TEXTURE_2D,
bsalomon926cb022015-12-17 18:15:11 -08001734 0,
bsalomon76148af2016-01-12 11:13:47 -08001735 internalFormat,
bsalomon100b8f82015-10-28 08:37:44 -07001736 kSize,
1737 kSize,
egdanielff1d5472015-09-10 08:37:20 -07001738 0,
bsalomon76148af2016-01-12 11:13:47 -08001739 externalFormat,
1740 externalType,
Ben Wagnera93a14a2017-08-28 10:34:05 -04001741 nullptr));
bsalomon30447372015-12-21 09:03:05 -08001742 if (GR_GL_NO_ERROR != CHECK_ALLOC_ERROR(this->glInterface())) {
egdanielff1d5472015-09-10 08:37:20 -07001743 GL_CALL(DeleteTextures(1, &colorID));
bsalomon30447372015-12-21 09:03:05 -08001744 return -1;
egdanielff1d5472015-09-10 08:37:20 -07001745 }
1746
1747 // unbind the texture from the texture unit before binding it to the frame buffer
1748 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, 0));
1749
1750 // Create Framebuffer
kkinnunen546eb5c2015-12-11 00:05:33 -08001751 GrGLuint fb = 0;
egdanielff1d5472015-09-10 08:37:20 -07001752 GL_CALL(GenFramebuffers(1, &fb));
Adrienne Walker4ee88512018-05-17 11:37:14 -07001753 this->bindFramebuffer(GR_GL_FRAMEBUFFER, fb);
Robert Phillips294870f2016-11-11 12:38:40 -05001754 fHWBoundRenderTargetUniqueID.makeInvalid();
egdanielff1d5472015-09-10 08:37:20 -07001755 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
1756 GR_GL_COLOR_ATTACHMENT0,
1757 GR_GL_TEXTURE_2D,
1758 colorID,
1759 0));
bsalomon30447372015-12-21 09:03:05 -08001760 GrGLuint sbRBID = 0;
1761 GL_CALL(GenRenderbuffers(1, &sbRBID));
egdanielff1d5472015-09-10 08:37:20 -07001762
1763 // look over formats till I find a compatible one
1764 int stencilFmtCnt = this->glCaps().stencilFormats().count();
bsalomon30447372015-12-21 09:03:05 -08001765 if (sbRBID) {
egdanielff1d5472015-09-10 08:37:20 -07001766 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbRBID));
bsalomon30447372015-12-21 09:03:05 -08001767 for (int i = 0; i < stencilFmtCnt && sbRBID; ++i) {
1768 const GrGLCaps::StencilFormat& sFmt = this->glCaps().stencilFormats()[i];
1769 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1770 GL_ALLOC_CALL(this->glInterface(), RenderbufferStorage(GR_GL_RENDERBUFFER,
1771 sFmt.fInternalFormat,
1772 kSize, kSize));
1773 if (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(this->glInterface())) {
egdanielff1d5472015-09-10 08:37:20 -07001774 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon30447372015-12-21 09:03:05 -08001775 GR_GL_STENCIL_ATTACHMENT,
egdanielff1d5472015-09-10 08:37:20 -07001776 GR_GL_RENDERBUFFER, sbRBID));
bsalomon30447372015-12-21 09:03:05 -08001777 if (sFmt.fPacked) {
1778 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1779 GR_GL_DEPTH_ATTACHMENT,
1780 GR_GL_RENDERBUFFER, sbRBID));
1781 } else {
1782 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1783 GR_GL_DEPTH_ATTACHMENT,
1784 GR_GL_RENDERBUFFER, 0));
1785 }
1786 GrGLenum status;
1787 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1788 if (status == GR_GL_FRAMEBUFFER_COMPLETE) {
1789 firstWorkingStencilFormatIndex = i;
1790 break;
1791 }
egdanielff1d5472015-09-10 08:37:20 -07001792 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1793 GR_GL_STENCIL_ATTACHMENT,
1794 GR_GL_RENDERBUFFER, 0));
1795 if (sFmt.fPacked) {
1796 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1797 GR_GL_DEPTH_ATTACHMENT,
1798 GR_GL_RENDERBUFFER, 0));
1799 }
egdanielff1d5472015-09-10 08:37:20 -07001800 }
1801 }
bsalomon30447372015-12-21 09:03:05 -08001802 GL_CALL(DeleteRenderbuffers(1, &sbRBID));
egdanielff1d5472015-09-10 08:37:20 -07001803 }
1804 GL_CALL(DeleteTextures(1, &colorID));
Adrienne Walker4ee88512018-05-17 11:37:14 -07001805 this->bindFramebuffer(GR_GL_FRAMEBUFFER, 0);
1806 this->deleteFramebuffer(fb);
bsalomon30447372015-12-21 09:03:05 -08001807 fGLContext->caps()->setStencilFormatIndexForConfig(config, firstWorkingStencilFormatIndex);
egdanielff1d5472015-09-10 08:37:20 -07001808 }
bsalomon30447372015-12-21 09:03:05 -08001809 return this->glCaps().getStencilFormatIndexForConfig(config);
egdanielff1d5472015-09-10 08:37:20 -07001810}
1811
Brian Salomon2a4f9832018-03-03 22:43:43 -05001812bool GrGLGpu::createTextureImpl(const GrSurfaceDesc& desc, GrGLTextureInfo* info, bool renderTarget,
Brian Salomondc829942018-10-23 16:07:24 -04001813 GrGLTexture::SamplerParams* initialTexParams,
1814 const GrMipLevel texels[], int mipLevelCount,
1815 GrMipMapsStatus* mipMapsStatus) {
erikchen9a1ed5d2016-02-10 16:32:34 -08001816 info->fID = 0;
1817 info->fTarget = GR_GL_TEXTURE_2D;
1818 GL_CALL(GenTextures(1, &(info->fID)));
1819
1820 if (!info->fID) {
1821 return false;
1822 }
1823
Brian Salomond978b902019-02-07 15:09:18 -05001824 this->bindTextureToScratchUnit(info->fTarget, info->fID);
erikchen9a1ed5d2016-02-10 16:32:34 -08001825
1826 if (renderTarget && this->glCaps().textureUsageSupport()) {
1827 // provides a hint about how this texture will be used
1828 GL_CALL(TexParameteri(info->fTarget,
1829 GR_GL_TEXTURE_USAGE,
1830 GR_GL_FRAMEBUFFER_ATTACHMENT));
1831 }
1832
cblume55f2d2d2016-02-26 13:20:48 -08001833 if (info) {
Brian Salomondc829942018-10-23 16:07:24 -04001834 *initialTexParams = set_initial_texture_params(this->glInterface(), *info);
cblume55f2d2d2016-02-26 13:20:48 -08001835 }
Brian Salomon2a4f9832018-03-03 22:43:43 -05001836
Jim Van Verth1676cb92019-01-15 13:24:45 -05001837 bool success = false;
1838 if (GrPixelConfigIsCompressed(desc.fConfig)) {
1839 SkASSERT(!renderTarget);
1840 success = this->uploadCompressedTexData(desc.fConfig, desc.fWidth, desc.fHeight,
1841 info->fTarget, desc.fConfig,
1842 texels, mipLevelCount, mipMapsStatus);
1843 } else {
1844 success = this->uploadTexData(desc.fConfig, desc.fWidth, desc.fHeight, info->fTarget,
1845 kNewTexture_UploadType, 0, 0, desc.fWidth, desc.fHeight,
1846 desc.fConfig, texels, mipLevelCount, mipMapsStatus);
1847 }
1848 if (!success) {
erikchen9a1ed5d2016-02-10 16:32:34 -08001849 GL_CALL(DeleteTextures(1, &(info->fID)));
1850 return false;
1851 }
Greg Daniele7d8da42017-12-04 11:23:19 -05001852 info->fFormat = this->glCaps().configSizedInternalFormat(desc.fConfig);
erikchen9a1ed5d2016-02-10 16:32:34 -08001853 return true;
1854}
1855
egdanielec00d942015-09-14 12:56:10 -07001856GrStencilAttachment* GrGLGpu::createStencilAttachmentForRenderTarget(const GrRenderTarget* rt,
Brian Salomond1eaf492017-05-18 10:02:08 -04001857 int width, int height) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001858 SkASSERT(width >= rt->width());
1859 SkASSERT(height >= rt->height());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001860
vbuzinovdded6962015-06-12 08:59:45 -07001861 int samples = rt->numStencilSamples();
egdaniel8dc7c3a2015-04-16 11:22:42 -07001862 GrGLStencilAttachment::IDDesc sbDesc;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001863
egdanielff1d5472015-09-10 08:37:20 -07001864 int sIdx = this->getCompatibleStencilIndex(rt->config());
bsalomon62a627b2015-12-17 09:50:47 -08001865 if (sIdx < 0) {
egdanielec00d942015-09-14 12:56:10 -07001866 return nullptr;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001867 }
egdanielff1d5472015-09-10 08:37:20 -07001868
1869 if (!sbDesc.fRenderbufferID) {
1870 GL_CALL(GenRenderbuffers(1, &sbDesc.fRenderbufferID));
1871 }
1872 if (!sbDesc.fRenderbufferID) {
egdanielec00d942015-09-14 12:56:10 -07001873 return nullptr;
egdanielff1d5472015-09-10 08:37:20 -07001874 }
1875 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbDesc.fRenderbufferID));
1876 const GrGLCaps::StencilFormat& sFmt = this->glCaps().stencilFormats()[sIdx];
1877 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1878 // we do this "if" so that we don't call the multisample
1879 // version on a GL that doesn't have an MSAA extension.
Brian Salomonbdecacf2018-02-02 20:32:49 -05001880 if (samples > 1) {
egdanielff1d5472015-09-10 08:37:20 -07001881 SkAssertResult(renderbuffer_storage_msaa(*fGLContext,
1882 samples,
1883 sFmt.fInternalFormat,
1884 width, height));
1885 } else {
1886 GL_ALLOC_CALL(this->glInterface(), RenderbufferStorage(GR_GL_RENDERBUFFER,
1887 sFmt.fInternalFormat,
1888 width, height));
Brian Salomon0ec981b2017-05-15 13:48:50 -04001889 SkASSERT(GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(this->glInterface()));
egdanielff1d5472015-09-10 08:37:20 -07001890 }
1891 fStats.incStencilAttachmentCreates();
1892 // After sized formats we attempt an unsized format and take
1893 // whatever sizes GL gives us. In that case we query for the size.
1894 GrGLStencilAttachment::Format format = sFmt;
1895 get_stencil_rb_sizes(this->glInterface(), &format);
egdanielec00d942015-09-14 12:56:10 -07001896 GrGLStencilAttachment* stencil = new GrGLStencilAttachment(this,
1897 sbDesc,
1898 width,
1899 height,
1900 samples,
1901 format);
1902 return stencil;
reed@google.comac10a2d2010-12-22 21:39:39 +00001903}
1904
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001905////////////////////////////////////////////////////////////////////////////////
1906
Brian Salomondbf70722019-02-07 11:31:24 -05001907sk_sp<GrGpuBuffer> GrGLGpu::onCreateBuffer(size_t size, GrGpuBufferType intendedType,
1908 GrAccessPattern accessPattern, const void* data) {
Brian Salomon12d22642019-01-29 14:38:50 -05001909 return GrGLBuffer::Make(this, size, intendedType, accessPattern, data);
jvanverth73063dc2015-12-03 09:15:47 -08001910}
1911
Brian Salomond818ebf2018-07-02 14:08:49 +00001912void GrGLGpu::flushScissor(const GrScissorState& scissorState,
1913 const GrGLIRect& rtViewport,
1914 GrSurfaceOrigin rtOrigin) {
1915 if (scissorState.enabled()) {
1916 GrGLIRect scissor;
1917 scissor.setRelativeTo(rtViewport, scissorState.rect(), rtOrigin);
1918 // if the scissor fully contains the viewport then we fall through and
1919 // disable the scissor test.
1920 if (!scissor.contains(rtViewport)) {
1921 if (fHWScissorSettings.fRect != scissor) {
1922 scissor.pushToGLScissor(this->glInterface());
1923 fHWScissorSettings.fRect = scissor;
1924 }
1925 if (kYes_TriState != fHWScissorSettings.fEnabled) {
1926 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1927 fHWScissorSettings.fEnabled = kYes_TriState;
bsalomon@google.coma3201942012-06-21 19:58:20 +00001928 }
1929 return;
1930 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001931 }
Brian Salomond818ebf2018-07-02 14:08:49 +00001932
1933 // See fall through note above
1934 this->disableScissor();
joshualitt77b13072014-10-27 14:51:01 -07001935}
1936
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001937void GrGLGpu::flushWindowRectangles(const GrWindowRectsState& windowState,
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001938 const GrGLRenderTarget* rt, GrSurfaceOrigin origin) {
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001939#ifndef USE_NSIGHT
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001940 typedef GrWindowRectsState::Mode Mode;
1941 SkASSERT(!windowState.enabled() || rt->renderFBOID()); // Window rects can't be used on-screen.
1942 SkASSERT(windowState.numWindows() <= this->caps()->maxWindowRectangles());
csmartdalton28341fa2016-08-17 10:00:21 -07001943
Jim Van Verth6a40abc2017-11-02 16:56:09 +00001944 if (!this->caps()->maxWindowRectangles() ||
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001945 fHWWindowRectsState.knownEqualTo(origin, rt->getViewport(), windowState)) {
csmartdalton28341fa2016-08-17 10:00:21 -07001946 return;
csmartdalton28341fa2016-08-17 10:00:21 -07001947 }
1948
csmartdalton7535f412016-08-23 06:51:00 -07001949 // This is purely a workaround for a spurious warning generated by gcc. Otherwise the above
1950 // assert would be sufficient. https://gcc.gnu.org/bugzilla/show_bug.cgi?id=5912
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001951 int numWindows = SkTMin(windowState.numWindows(), int(GrWindowRectangles::kMaxWindows));
1952 SkASSERT(windowState.numWindows() == numWindows);
csmartdalton7535f412016-08-23 06:51:00 -07001953
csmartdalton28341fa2016-08-17 10:00:21 -07001954 GrGLIRect glwindows[GrWindowRectangles::kMaxWindows];
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001955 const SkIRect* skwindows = windowState.windows().data();
csmartdalton7535f412016-08-23 06:51:00 -07001956 for (int i = 0; i < numWindows; ++i) {
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001957 glwindows[i].setRelativeTo(rt->getViewport(), skwindows[i], origin);
csmartdalton28341fa2016-08-17 10:00:21 -07001958 }
1959
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001960 GrGLenum glmode = (Mode::kExclusive == windowState.mode()) ? GR_GL_EXCLUSIVE : GR_GL_INCLUSIVE;
csmartdalton7535f412016-08-23 06:51:00 -07001961 GL_CALL(WindowRectangles(glmode, numWindows, glwindows->asInts()));
csmartdalton28341fa2016-08-17 10:00:21 -07001962
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001963 fHWWindowRectsState.set(origin, rt->getViewport(), windowState);
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001964#endif
csmartdalton28341fa2016-08-17 10:00:21 -07001965}
1966
1967void GrGLGpu::disableWindowRectangles() {
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001968#ifndef USE_NSIGHT
Jim Van Verth6a40abc2017-11-02 16:56:09 +00001969 if (!this->caps()->maxWindowRectangles() || fHWWindowRectsState.knownDisabled()) {
csmartdalton28341fa2016-08-17 10:00:21 -07001970 return;
1971 }
1972 GL_CALL(WindowRectangles(GR_GL_EXCLUSIVE, 0, nullptr));
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001973 fHWWindowRectsState.setDisabled();
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001974#endif
csmartdalton28341fa2016-08-17 10:00:21 -07001975}
1976
Brian Salomonf7232642018-09-19 08:58:08 -04001977void GrGLGpu::resolveAndGenerateMipMapsForProcessorTextures(
1978 const GrPrimitiveProcessor& primProc,
1979 const GrPipeline& pipeline,
1980 const GrTextureProxy* const primProcTextures[],
1981 int numPrimitiveProcessorTextureSets) {
Brian Salomondfec99f2018-08-02 10:40:05 -04001982 auto genLevelsIfNeeded = [this](GrTexture* tex, const GrSamplerState& sampler) {
Brian Salomon7eae3e02018-08-07 14:02:38 +00001983 SkASSERT(tex);
Brian Salomondfec99f2018-08-02 10:40:05 -04001984 if (sampler.filter() == GrSamplerState::Filter::kMipMap &&
1985 tex->texturePriv().mipMapped() == GrMipMapped::kYes &&
1986 tex->texturePriv().mipMapsAreDirty()) {
1987 SkASSERT(this->caps()->mipMapSupport());
1988 this->regenerateMipMapLevels(static_cast<GrGLTexture*>(tex));
Brian Salomonf7232642018-09-19 08:58:08 -04001989 SkASSERT(!tex->asRenderTarget() || !tex->asRenderTarget()->needsResolve());
1990 } else if (auto* rt = tex->asRenderTarget()) {
1991 if (rt->needsResolve()) {
1992 this->resolveRenderTarget(rt);
1993 }
Brian Salomondfec99f2018-08-02 10:40:05 -04001994 }
1995 };
1996
Brian Salomonf7232642018-09-19 08:58:08 -04001997 for (int set = 0, tex = 0; set < numPrimitiveProcessorTextureSets; ++set) {
1998 for (int sampler = 0; sampler < primProc.numTextureSamplers(); ++sampler, ++tex) {
1999 GrTexture* texture = primProcTextures[tex]->peekTexture();
2000 genLevelsIfNeeded(texture, primProc.textureSampler(sampler).samplerState());
2001 }
Brian Salomondfec99f2018-08-02 10:40:05 -04002002 }
2003
2004 GrFragmentProcessor::Iter iter(pipeline);
2005 while (const GrFragmentProcessor* fp = iter.next()) {
2006 for (int i = 0; i < fp->numTextureSamplers(); ++i) {
2007 const auto& textureSampler = fp->textureSampler(i);
2008 genLevelsIfNeeded(textureSampler.peekTexture(), textureSampler.samplerState());
2009 }
2010 }
2011}
2012
Robert Phillipsd0fe8752019-01-31 14:13:59 -05002013bool GrGLGpu::flushGLState(GrRenderTarget* renderTarget,
2014 GrSurfaceOrigin origin,
2015 const GrPrimitiveProcessor& primProc,
Brian Salomon49348902018-06-26 09:12:38 -04002016 const GrPipeline& pipeline,
2017 const GrPipeline::FixedDynamicState* fixedDynamicState,
Brian Salomonf7232642018-09-19 08:58:08 -04002018 const GrPipeline::DynamicStateArrays* dynamicStateArrays,
2019 int dynamicStateArraysLength,
bsalomon2eda5b32016-09-21 10:53:24 -07002020 bool willDrawPoints) {
Brian Salomonf7232642018-09-19 08:58:08 -04002021 const GrTextureProxy* const* primProcProxiesForMipRegen = nullptr;
2022 const GrTextureProxy* const* primProcProxiesToBind = nullptr;
2023 int numPrimProcTextureSets = 1; // number of texture per prim proc sampler.
2024 if (dynamicStateArrays && dynamicStateArrays->fPrimitiveProcessorTextures) {
2025 primProcProxiesForMipRegen = dynamicStateArrays->fPrimitiveProcessorTextures;
2026 numPrimProcTextureSets = dynamicStateArraysLength;
2027 } else if (fixedDynamicState && fixedDynamicState->fPrimitiveProcessorTextures) {
2028 primProcProxiesForMipRegen = fixedDynamicState->fPrimitiveProcessorTextures;
2029 primProcProxiesToBind = fixedDynamicState->fPrimitiveProcessorTextures;
Brian Salomon7eae3e02018-08-07 14:02:38 +00002030 }
Greg Daniel9a51a862018-11-30 10:18:14 -05002031
2032 SkASSERT(SkToBool(primProcProxiesForMipRegen) == SkToBool(primProc.numTextureSamplers()));
2033
Robert Phillipsd0fe8752019-01-31 14:13:59 -05002034 sk_sp<GrGLProgram> program(fProgramCache->refProgram(this, renderTarget, origin, primProc,
2035 primProcProxiesForMipRegen,
Greg Daniel9a51a862018-11-30 10:18:14 -05002036 pipeline, willDrawPoints));
2037 if (!program) {
2038 GrCapsDebugf(this->caps(), "Failed to create program!\n");
2039 return false;
2040 }
Brian Salomonf7232642018-09-19 08:58:08 -04002041 this->resolveAndGenerateMipMapsForProcessorTextures(
2042 primProc, pipeline, primProcProxiesForMipRegen, numPrimProcTextureSets);
brianosman33f6b3f2016-06-02 05:49:21 -07002043
egdaniel080e6732014-12-22 07:35:52 -08002044 GrXferProcessor::BlendInfo blendInfo;
egdaniel0e1853c2016-03-17 11:35:45 -07002045 pipeline.getXferProcessor().getBlendInfo(&blendInfo);
egdaniel080e6732014-12-22 07:35:52 -08002046
egdaniel080e6732014-12-22 07:35:52 -08002047 this->flushColorWrite(blendInfo.fWriteColor);
bsalomonbc3d0de2014-12-15 13:45:03 -08002048
Brian Salomon802cb312018-06-08 18:05:20 -04002049 this->flushProgram(std::move(program));
bsalomon1f78c0a2014-12-17 09:43:13 -08002050
Dongseong Hwang4e1f0222018-11-01 09:10:51 -07002051 // Swizzle the blend to match what the shader will output.
2052 const GrSwizzle& swizzle = this->caps()->shaderCaps()->configOutputSwizzle(
Robert Phillipsd0fe8752019-01-31 14:13:59 -05002053 renderTarget->config());
Dongseong Hwang4e1f0222018-11-01 09:10:51 -07002054 this->flushBlend(blendInfo, swizzle);
bsalomon1f78c0a2014-12-17 09:43:13 -08002055
Robert Phillipsd0fe8752019-01-31 14:13:59 -05002056 fHWProgram->updateUniformsAndTextureBindings(renderTarget, origin,
2057 primProc, pipeline, primProcProxiesToBind);
bsalomon1f78c0a2014-12-17 09:43:13 -08002058
Robert Phillipsd0fe8752019-01-31 14:13:59 -05002059 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(renderTarget);
csmartdaltonc633abb2016-11-01 08:55:55 -07002060 GrStencilSettings stencil;
2061 if (pipeline.isStencilEnabled()) {
2062 // TODO: attach stencil and create settings during render target flush.
2063 SkASSERT(glRT->renderTargetPriv().getStencilAttachment());
2064 stencil.reset(*pipeline.getUserStencil(), pipeline.hasStencilClip(),
2065 glRT->renderTargetPriv().numStencilBits());
2066 }
2067 this->flushStencil(stencil);
Brian Salomond818ebf2018-07-02 14:08:49 +00002068 if (pipeline.isScissorEnabled()) {
2069 static constexpr SkIRect kBogusScissor{0, 0, 1, 1};
2070 GrScissorState state(fixedDynamicState ? fixedDynamicState->fScissorRect : kBogusScissor);
Robert Phillipsd0fe8752019-01-31 14:13:59 -05002071 this->flushScissor(state, glRT->getViewport(), origin);
Brian Salomond818ebf2018-07-02 14:08:49 +00002072 } else {
2073 this->disableScissor();
Brian Salomon49348902018-06-26 09:12:38 -04002074 }
Robert Phillipsd0fe8752019-01-31 14:13:59 -05002075 this->flushWindowRectangles(pipeline.getWindowRectsState(), glRT, origin);
Chris Dalton4c56b032019-03-04 12:28:42 -07002076 this->flushHWAAState(glRT, pipeline.isHWAntialiasState());
bsalomonbc3d0de2014-12-15 13:45:03 -08002077
2078 // This must come after textures are flushed because a texture may need
egdanield803f272015-03-18 13:01:52 -07002079 // to be msaa-resolved (which will modify bound FBO state).
Chris Daltonc8ece3d2018-07-30 15:03:45 -06002080 this->flushRenderTarget(glRT);
bsalomonbc3d0de2014-12-15 13:45:03 -08002081
2082 return true;
2083}
2084
Brian Salomon802cb312018-06-08 18:05:20 -04002085void GrGLGpu::flushProgram(sk_sp<GrGLProgram> program) {
2086 if (!program) {
2087 fHWProgram.reset();
2088 fHWProgramID = 0;
2089 return;
2090 }
2091 SkASSERT((program == fHWProgram) == (fHWProgramID == program->programID()));
2092 if (program == fHWProgram) {
2093 return;
2094 }
2095 auto id = program->programID();
2096 SkASSERT(id);
2097 GL_CALL(UseProgram(id));
2098 fHWProgram = std::move(program);
2099 fHWProgramID = id;
2100}
2101
2102void GrGLGpu::flushProgram(GrGLuint id) {
2103 SkASSERT(id);
2104 if (fHWProgramID == id) {
2105 SkASSERT(!fHWProgram);
2106 return;
2107 }
2108 fHWProgram.reset();
2109 GL_CALL(UseProgram(id));
2110 fHWProgramID = id;
2111}
2112
2113void GrGLGpu::setupGeometry(const GrBuffer* indexBuffer,
Chris Daltonff926502017-05-03 14:36:54 -04002114 const GrBuffer* vertexBuffer,
Chris Dalton1d616352017-05-31 12:51:23 -06002115 int baseVertex,
2116 const GrBuffer* instanceBuffer,
Brian Salomon802cb312018-06-08 18:05:20 -04002117 int baseInstance,
2118 GrPrimitiveRestart enablePrimitiveRestart) {
2119 SkASSERT((enablePrimitiveRestart == GrPrimitiveRestart::kNo) || indexBuffer);
Chris Dalton27059d32018-01-23 14:06:50 -07002120
cdaltone2e71c22016-04-07 18:13:29 -07002121 GrGLAttribArrayState* attribState;
Chris Daltonff926502017-05-03 14:36:54 -04002122 if (indexBuffer) {
Brian Salomondbf70722019-02-07 11:31:24 -05002123 SkASSERT(indexBuffer->isCpuBuffer() ||
2124 !static_cast<const GrGpuBuffer*>(indexBuffer)->isMapped());
Chris Daltonff926502017-05-03 14:36:54 -04002125 attribState = fHWVertexArrayState.bindInternalVertexArray(this, indexBuffer);
cdaltone2e71c22016-04-07 18:13:29 -07002126 } else {
2127 attribState = fHWVertexArrayState.bindInternalVertexArray(this);
bsalomonbc3d0de2014-12-15 13:45:03 -08002128 }
bsalomonbc3d0de2014-12-15 13:45:03 -08002129
Brian Salomon92be2f72018-06-19 14:33:47 -04002130 int numAttribs = fHWProgram->numVertexAttributes() + fHWProgram->numInstanceAttributes();
2131 attribState->enableVertexArrays(this, numAttribs, enablePrimitiveRestart);
Chris Dalton8e45b4f2017-05-05 14:00:56 -04002132
Brian Salomon802cb312018-06-08 18:05:20 -04002133 if (int vertexStride = fHWProgram->vertexStride()) {
Brian Salomondbf70722019-02-07 11:31:24 -05002134 SkASSERT(vertexBuffer);
2135 SkASSERT(vertexBuffer->isCpuBuffer() ||
2136 !static_cast<const GrGpuBuffer*>(vertexBuffer)->isMapped());
2137 size_t bufferOffset = baseVertex * static_cast<size_t>(vertexStride);
Brian Salomon92be2f72018-06-19 14:33:47 -04002138 for (int i = 0; i < fHWProgram->numVertexAttributes(); ++i) {
2139 const auto& attrib = fHWProgram->vertexAttribute(i);
2140 static constexpr int kDivisor = 0;
Brian Osman4a3f5c82018-09-18 16:16:38 -04002141 attribState->set(this, attrib.fLocation, vertexBuffer, attrib.fCPUType, attrib.fGPUType,
2142 vertexStride, bufferOffset + attrib.fOffset, kDivisor);
Brian Salomon92be2f72018-06-19 14:33:47 -04002143 }
Chris Dalton1d616352017-05-31 12:51:23 -06002144 }
Brian Salomon802cb312018-06-08 18:05:20 -04002145 if (int instanceStride = fHWProgram->instanceStride()) {
Brian Salomondbf70722019-02-07 11:31:24 -05002146 SkASSERT(instanceBuffer);
2147 SkASSERT(instanceBuffer->isCpuBuffer() ||
2148 !static_cast<const GrGpuBuffer*>(instanceBuffer)->isMapped());
2149 size_t bufferOffset = baseInstance * static_cast<size_t>(instanceStride);
Brian Salomon92be2f72018-06-19 14:33:47 -04002150 int attribIdx = fHWProgram->numVertexAttributes();
2151 for (int i = 0; i < fHWProgram->numInstanceAttributes(); ++i, ++attribIdx) {
2152 const auto& attrib = fHWProgram->instanceAttribute(i);
2153 static constexpr int kDivisor = 1;
Brian Osman4a3f5c82018-09-18 16:16:38 -04002154 attribState->set(this, attrib.fLocation, instanceBuffer, attrib.fCPUType,
2155 attrib.fGPUType, instanceStride, bufferOffset + attrib.fOffset,
2156 kDivisor);
Brian Salomon92be2f72018-06-19 14:33:47 -04002157 }
bsalomonbc3d0de2014-12-15 13:45:03 -08002158 }
2159}
2160
Brian Salomonae64c192019-02-05 09:41:37 -05002161GrGLenum GrGLGpu::bindBuffer(GrGpuBufferType type, const GrBuffer* buffer) {
joshualitt93316b92015-10-23 09:08:08 -07002162 this->handleDirtyContext();
cdaltondeacc972016-04-06 14:26:33 -07002163
cdaltone2e71c22016-04-07 18:13:29 -07002164 // Index buffer state is tied to the vertex array.
Brian Salomonae64c192019-02-05 09:41:37 -05002165 if (GrGpuBufferType::kIndex == type) {
cdaltone2e71c22016-04-07 18:13:29 -07002166 this->bindVertexArray(0);
cdaltondeacc972016-04-06 14:26:33 -07002167 }
cdaltone2e71c22016-04-07 18:13:29 -07002168
Brian Salomonae64c192019-02-05 09:41:37 -05002169 auto* bufferState = this->hwBufferState(type);
Brian Salomondbf70722019-02-07 11:31:24 -05002170 if (buffer->isCpuBuffer()) {
Brian Salomonae64c192019-02-05 09:41:37 -05002171 if (!bufferState->fBufferZeroKnownBound) {
2172 GL_CALL(BindBuffer(bufferState->fGLTarget, 0));
2173 bufferState->fBufferZeroKnownBound = true;
2174 bufferState->fBoundBufferUniqueID.makeInvalid();
cdaltone2e71c22016-04-07 18:13:29 -07002175 }
Brian Salomondbf70722019-02-07 11:31:24 -05002176 } else if (static_cast<const GrGpuBuffer*>(buffer)->uniqueID() !=
2177 bufferState->fBoundBufferUniqueID) {
Brian Salomonae64c192019-02-05 09:41:37 -05002178 const GrGLBuffer* glBuffer = static_cast<const GrGLBuffer*>(buffer);
2179 GL_CALL(BindBuffer(bufferState->fGLTarget, glBuffer->bufferID()));
2180 bufferState->fBufferZeroKnownBound = false;
2181 bufferState->fBoundBufferUniqueID = glBuffer->uniqueID();
cdaltone2e71c22016-04-07 18:13:29 -07002182 }
2183
Brian Salomonae64c192019-02-05 09:41:37 -05002184 return bufferState->fGLTarget;
joshualitt93316b92015-10-23 09:08:08 -07002185}
Brian Salomond818ebf2018-07-02 14:08:49 +00002186void GrGLGpu::disableScissor() {
2187 if (kNo_TriState != fHWScissorSettings.fEnabled) {
2188 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
2189 fHWScissorSettings.fEnabled = kNo_TriState;
2190 return;
2191 }
2192}
2193
Brian Osman9a9baae2018-11-05 15:06:26 -05002194void GrGLGpu::clear(const GrFixedClip& clip, const SkPMColor4f& color,
Robert Phillips19e51dc2017-08-09 09:30:51 -04002195 GrRenderTarget* target, GrSurfaceOrigin origin) {
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00002196 // parent class should never let us get here with no RT
bsalomon49f085d2014-09-05 13:34:00 -07002197 SkASSERT(target);
Michael Ludwigc39d0c82019-01-15 10:03:43 -05002198 SkASSERT(!this->caps()->performColorClearsAsDraws());
2199 SkASSERT(!clip.scissorEnabled() || !this->caps()->performPartialClearsAsDraws());
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00002200
Brian Salomon43f8bf02017-10-18 08:33:29 -04002201 this->handleDirtyContext();
bsalomon@google.com74b98712011-11-11 19:46:16 +00002202
Brian Salomon43f8bf02017-10-18 08:33:29 -04002203 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
2204
Brian Salomond818ebf2018-07-02 14:08:49 +00002205 if (clip.scissorEnabled()) {
Chris Daltonc8ece3d2018-07-30 15:03:45 -06002206 this->flushRenderTarget(glRT, origin, clip.scissorRect());
Brian Salomon1fabd512018-02-09 09:54:25 -05002207 } else {
Chris Daltonc8ece3d2018-07-30 15:03:45 -06002208 this->flushRenderTarget(glRT);
Brian Salomon1fabd512018-02-09 09:54:25 -05002209 }
Brian Salomond818ebf2018-07-02 14:08:49 +00002210 this->flushScissor(clip.scissorState(), glRT->getViewport(), origin);
Brian Salomon43f8bf02017-10-18 08:33:29 -04002211 this->flushWindowRectangles(clip.windowRectsState(), glRT, origin);
Brian Salomon805cc7a2019-01-28 09:52:34 -05002212 this->flushColorWrite(true);
Brian Salomon028a9a52017-05-11 11:39:08 -04002213
Brian Osman9a9baae2018-11-05 15:06:26 -05002214 GrGLfloat r = color.fR, g = color.fG, b = color.fB, a = color.fA;
Eric Karlaeaf22b2017-05-18 15:08:09 -07002215 if (this->glCaps().clearToBoundaryValuesIsBroken() &&
2216 (1 == r || 0 == r) && (1 == g || 0 == g) && (1 == b || 0 == b) && (1 == a || 0 == a)) {
Eric Karlaeaf22b2017-05-18 15:08:09 -07002217 static const GrGLfloat safeAlpha1 = nextafter(1.f, 2.f);
2218 static const GrGLfloat safeAlpha0 = nextafter(0.f, -1.f);
Eric Karlaeaf22b2017-05-18 15:08:09 -07002219 a = (1 == a) ? safeAlpha1 : safeAlpha0;
Brian Salomon028a9a52017-05-11 11:39:08 -04002220 }
Brian Salomon805cc7a2019-01-28 09:52:34 -05002221 this->flushClearColor(r, g, b, a);
2222
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002223 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00002224}
2225
Robert Phillips95214472017-08-08 18:00:03 -04002226void GrGLGpu::clearStencil(GrRenderTarget* target, int clearValue) {
Michael Ludwigc39d0c82019-01-15 10:03:43 -05002227 SkASSERT(!this->caps()->performStencilClearsAsDraws());
2228
Robert Phillips95214472017-08-08 18:00:03 -04002229 if (!target) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00002230 return;
2231 }
Robert Phillipscb2e2352017-08-30 16:44:40 -04002232
2233 GrStencilAttachment* sb = target->renderTargetPriv().getStencilAttachment();
2234 // this should only be called internally when we know we have a
2235 // stencil buffer.
2236 SkASSERT(sb);
2237
bsalomonb0bd4f62014-09-03 07:19:50 -07002238 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
Brian Salomon1fabd512018-02-09 09:54:25 -05002239 this->flushRenderTargetNoColorWrites(glRT);
bsalomon@google.com8295dc12011-05-02 12:53:34 +00002240
Brian Salomond818ebf2018-07-02 14:08:49 +00002241 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07002242 this->disableWindowRectangles();
robertphillips@google.com730ebe52012-04-16 16:33:13 +00002243
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002244 GL_CALL(StencilMask(0xffffffff));
Robert Phillips95214472017-08-08 18:00:03 -04002245 GL_CALL(ClearStencil(clearValue));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002246 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00002247 fHWStencilSettings.invalidate();
Robert Phillipscb2e2352017-08-30 16:44:40 -04002248 if (!clearValue) {
2249 sb->cleared();
2250 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002251}
2252
csmartdalton29df7602016-08-31 11:55:52 -07002253void GrGLGpu::clearStencilClip(const GrFixedClip& clip,
2254 bool insideStencilMask,
Robert Phillips19e51dc2017-08-09 09:30:51 -04002255 GrRenderTarget* target, GrSurfaceOrigin origin) {
bsalomon49f085d2014-09-05 13:34:00 -07002256 SkASSERT(target);
Michael Ludwigc39d0c82019-01-15 10:03:43 -05002257 SkASSERT(!this->caps()->performStencilClearsAsDraws());
egdaniel9cb63402016-06-23 08:37:05 -07002258 this->handleDirtyContext();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00002259
egdaniel8dc7c3a2015-04-16 11:22:42 -07002260 GrStencilAttachment* sb = target->renderTargetPriv().getStencilAttachment();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00002261 // this should only be called internally when we know we have a
2262 // stencil buffer.
bsalomon6bc1b5f2015-02-23 09:06:38 -08002263 SkASSERT(sb);
2264 GrGLint stencilBitCount = sb->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002265#if 0
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002266 SkASSERT(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00002267 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002268#else
2269 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00002270 // ANGLE a partial stencil mask will cause clears to be
Robert Phillipsf2361d22016-10-25 14:20:06 -04002271 // turned into draws. Our contract on GrOpList says that
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002272 // changing the clip between stencil passes may or may not
2273 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00002274 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002275#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002276 GrGLint value;
csmartdalton29df7602016-08-31 11:55:52 -07002277 if (insideStencilMask) {
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002278 value = (1 << (stencilBitCount - 1));
2279 } else {
2280 value = 0;
2281 }
bsalomonb0bd4f62014-09-03 07:19:50 -07002282 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
Brian Salomon1fabd512018-02-09 09:54:25 -05002283 this->flushRenderTargetNoColorWrites(glRT);
bsalomon@google.coma3201942012-06-21 19:58:20 +00002284
Brian Salomond818ebf2018-07-02 14:08:49 +00002285 this->flushScissor(clip.scissorState(), glRT->getViewport(), origin);
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002286 this->flushWindowRectangles(clip.windowRectsState(), glRT, origin);
bsalomon@google.coma3201942012-06-21 19:58:20 +00002287
caryclark@google.comcf6285b2012-06-06 12:09:01 +00002288 GL_CALL(StencilMask((uint32_t) clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002289 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002290 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00002291 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00002292}
2293
bsalomon1aa20292016-01-22 08:16:09 -08002294bool GrGLGpu::readPixelsSupported(GrRenderTarget* target, GrPixelConfig readConfig) {
Brian Salomon625cd9e2016-12-15 09:35:19 -05002295#ifdef SK_BUILD_FOR_MAC
2296 // Chromium may ask us to read back from locked IOSurfaces. Calling the command buffer's
2297 // glGetIntegerv() with GL_IMPLEMENTATION_COLOR_READ_FORMAT/_TYPE causes the command buffer
2298 // to make a call to check the framebuffer status which can hang the driver. So in Mac Chromium
2299 // we always use a temporary surface to test for read pixels support.
2300 // https://www.crbug.com/662802
2301 if (this->glContext().driver() == kChromium_GrGLDriver) {
2302 return this->readPixelsSupported(target->config(), readConfig);
2303 }
2304#endif
bsalomon1aa20292016-01-22 08:16:09 -08002305 auto bindRenderTarget = [this, target]() -> bool {
Brian Salomon1fabd512018-02-09 09:54:25 -05002306 this->flushRenderTargetNoColorWrites(static_cast<GrGLRenderTarget*>(target));
bsalomon1aa20292016-01-22 08:16:09 -08002307 return true;
2308 };
bsalomon2c3db322016-11-08 13:26:24 -08002309 auto unbindRenderTarget = []{};
bsalomon1aa20292016-01-22 08:16:09 -08002310 auto getIntegerv = [this](GrGLenum query, GrGLint* value) {
2311 GR_GL_GetIntegerv(this->glInterface(), query, value);
2312 };
2313 GrPixelConfig rtConfig = target->config();
bsalomon2c3db322016-11-08 13:26:24 -08002314 return this->glCaps().readPixelsSupported(rtConfig, readConfig, getIntegerv, bindRenderTarget,
2315 unbindRenderTarget);
bsalomon1aa20292016-01-22 08:16:09 -08002316}
2317
2318bool GrGLGpu::readPixelsSupported(GrPixelConfig rtConfig, GrPixelConfig readConfig) {
bsalomon2c3db322016-11-08 13:26:24 -08002319 sk_sp<GrTexture> temp;
2320 auto bindRenderTarget = [this, rtConfig, &temp]() -> bool {
Robert Phillips16d8ec62017-07-27 16:16:25 -04002321 GrSurfaceDesc desc;
bsalomon1aa20292016-01-22 08:16:09 -08002322 desc.fConfig = rtConfig;
2323 desc.fWidth = desc.fHeight = 16;
Brian Salomonbdecacf2018-02-02 20:32:49 -05002324 if (this->glCaps().isConfigRenderable(rtConfig)) {
bsalomon2c3db322016-11-08 13:26:24 -08002325 desc.fFlags = kRenderTarget_GrSurfaceFlag;
Robert Phillips67d52cf2017-06-05 13:38:13 -04002326 temp = this->createTexture(desc, SkBudgeted::kNo);
bsalomon2c3db322016-11-08 13:26:24 -08002327 if (!temp) {
2328 return false;
2329 }
2330 GrGLRenderTarget* glrt = static_cast<GrGLRenderTarget*>(temp->asRenderTarget());
Brian Salomon1fabd512018-02-09 09:54:25 -05002331 this->flushRenderTargetNoColorWrites(glrt);
bsalomon2c3db322016-11-08 13:26:24 -08002332 return true;
2333 } else if (this->glCaps().canConfigBeFBOColorAttachment(rtConfig)) {
Robert Phillips67d52cf2017-06-05 13:38:13 -04002334 temp = this->createTexture(desc, SkBudgeted::kNo);
bsalomon2c3db322016-11-08 13:26:24 -08002335 if (!temp) {
2336 return false;
2337 }
2338 GrGLIRect vp;
2339 this->bindSurfaceFBOForPixelOps(temp.get(), GR_GL_FRAMEBUFFER, &vp, kDst_TempFBOTarget);
Robert Phillips294870f2016-11-11 12:38:40 -05002340 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon2c3db322016-11-08 13:26:24 -08002341 return true;
bsalomon1aa20292016-01-22 08:16:09 -08002342 }
bsalomon2c3db322016-11-08 13:26:24 -08002343 return false;
2344 };
2345 auto unbindRenderTarget = [this, &temp]() {
2346 this->unbindTextureFBOForPixelOps(GR_GL_FRAMEBUFFER, temp.get());
bsalomon1aa20292016-01-22 08:16:09 -08002347 };
2348 auto getIntegerv = [this](GrGLenum query, GrGLint* value) {
2349 GR_GL_GetIntegerv(this->glInterface(), query, value);
2350 };
bsalomon2c3db322016-11-08 13:26:24 -08002351 return this->glCaps().readPixelsSupported(rtConfig, readConfig, getIntegerv, bindRenderTarget,
2352 unbindRenderTarget);
bsalomon1aa20292016-01-22 08:16:09 -08002353}
2354
2355bool GrGLGpu::readPixelsSupported(GrSurface* surfaceForConfig, GrPixelConfig readConfig) {
2356 if (GrRenderTarget* rt = surfaceForConfig->asRenderTarget()) {
2357 return this->readPixelsSupported(rt, readConfig);
2358 } else {
2359 GrPixelConfig config = surfaceForConfig->config();
2360 return this->readPixelsSupported(config, readConfig);
2361 }
2362}
2363
Brian Salomone05ba5a2019-04-08 11:59:07 -04002364bool GrGLGpu::readOrTransferPixelsFrom(GrSurface* surface, int left, int top, int width, int height,
2365 GrColorType dstColorType, void* offsetOrPtr,
2366 size_t rowBytes) {
bsalomon6cb3cbe2015-07-30 07:34:27 -07002367 SkASSERT(surface);
bsalomon39826022015-07-23 08:07:21 -07002368
bsalomone9573312016-01-25 14:33:25 -08002369 GrGLRenderTarget* renderTarget = static_cast<GrGLRenderTarget*>(surface->asRenderTarget());
Brian Salomon71d9d842016-11-03 13:42:00 -04002370 if (!renderTarget && !this->glCaps().canConfigBeFBOColorAttachment(surface->config())) {
bsalomon6cb3cbe2015-07-30 07:34:27 -07002371 return false;
2372 }
2373
Brian Salomonc320b152018-02-20 14:05:36 -05002374 // TODO: Avoid this conversion by making GrGLCaps work with color types.
2375 auto dstAsConfig = GrColorTypeToPixelConfig(dstColorType, GrSRGBEncoded::kNo);
2376
Brian Salomonc320b152018-02-20 14:05:36 -05002377 if (!this->readPixelsSupported(surface, dstAsConfig)) {
bsalomon16921ec2015-07-30 15:34:56 -07002378 return false;
2379 }
2380
bsalomon76148af2016-01-12 11:13:47 -08002381 GrGLenum externalFormat;
2382 GrGLenum externalType;
Brian Salomonc320b152018-02-20 14:05:36 -05002383 if (!this->glCaps().getReadPixelsFormat(surface->config(), dstAsConfig, &externalFormat,
bsalomon76148af2016-01-12 11:13:47 -08002384 &externalType)) {
2385 return false;
2386 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00002387
Brian Salomon71d9d842016-11-03 13:42:00 -04002388 GrGLIRect glvp;
2389 if (renderTarget) {
2390 // resolve the render target if necessary
2391 switch (renderTarget->getResolveType()) {
2392 case GrGLRenderTarget::kCantResolve_ResolveType:
2393 return false;
2394 case GrGLRenderTarget::kAutoResolves_ResolveType:
Brian Salomon1fabd512018-02-09 09:54:25 -05002395 this->flushRenderTargetNoColorWrites(renderTarget);
Brian Salomon71d9d842016-11-03 13:42:00 -04002396 break;
2397 case GrGLRenderTarget::kCanResolve_ResolveType:
Brian Salomon1fabd512018-02-09 09:54:25 -05002398 this->onResolveRenderTarget(renderTarget);
Brian Salomon71d9d842016-11-03 13:42:00 -04002399 // we don't track the state of the READ FBO ID.
Adrienne Walker4ee88512018-05-17 11:37:14 -07002400 this->bindFramebuffer(GR_GL_READ_FRAMEBUFFER, renderTarget->textureFBOID());
Brian Salomon71d9d842016-11-03 13:42:00 -04002401 break;
2402 default:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -04002403 SK_ABORT("Unknown resolve type");
Brian Salomon71d9d842016-11-03 13:42:00 -04002404 }
2405 glvp = renderTarget->getViewport();
2406 } else {
2407 // Use a temporary FBO.
2408 this->bindSurfaceFBOForPixelOps(surface, GR_GL_FRAMEBUFFER, &glvp, kSrc_TempFBOTarget);
Robert Phillips294870f2016-11-11 12:38:40 -05002409 fHWBoundRenderTargetUniqueID.makeInvalid();
reed@google.comac10a2d2010-12-22 21:39:39 +00002410 }
2411
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00002412 // the read rect is viewport-relative
2413 GrGLIRect readRect;
Brian Salomona6948702018-06-01 15:33:20 -04002414 readRect.setRelativeTo(glvp, left, top, width, height, kTopLeft_GrSurfaceOrigin);
rmistry@google.comfbfcd562012-08-23 18:09:54 +00002415
Brian Salomonc320b152018-02-20 14:05:36 -05002416 int bytesPerPixel = GrBytesPerPixel(dstAsConfig);
bsalomon9d02b262016-02-01 12:49:30 -08002417 size_t tightRowBytes = bytesPerPixel * width;
Brian Salomona6948702018-06-01 15:33:20 -04002418 // determine if GL can read using the passed rowBytes or if we need a scratch buffer.
bsalomon@google.comc6980972011-11-02 19:57:21 +00002419 if (rowBytes != tightRowBytes) {
Brian Salomone05ba5a2019-04-08 11:59:07 -04002420 SkASSERT(this->glCaps().packRowLengthSupport());
2421 SkASSERT(!(rowBytes % bytesPerPixel));
2422 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, static_cast<GrGLint>(rowBytes / bytesPerPixel)));
bsalomon@google.comc6980972011-11-02 19:57:21 +00002423 }
Brian Salomonc320b152018-02-20 14:05:36 -05002424 GL_CALL(PixelStorei(GR_GL_PACK_ALIGNMENT, config_alignment(dstAsConfig)));
bsalomonf46a1242015-12-15 12:37:38 -08002425
Brian Osman1348ed02018-09-12 09:08:39 -04002426 bool reattachStencil = false;
2427 if (this->glCaps().detachStencilFromMSAABuffersBeforeReadPixels() &&
2428 renderTarget &&
2429 renderTarget->renderTargetPriv().getStencilAttachment() &&
2430 renderTarget->numColorSamples() > 1) {
2431 // Fix Adreno devices that won't read from MSAA framebuffers with stencil attached
2432 reattachStencil = true;
2433 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT,
2434 GR_GL_RENDERBUFFER, 0));
2435 }
2436
Brian Salomone05ba5a2019-04-08 11:59:07 -04002437 GL_CALL(ReadPixels(readRect.fLeft, readRect.fBottom, readRect.fWidth, readRect.fHeight,
2438 externalFormat, externalType, offsetOrPtr));
Brian Osman1348ed02018-09-12 09:08:39 -04002439
2440 if (reattachStencil) {
2441 GrGLStencilAttachment* stencilAttachment = static_cast<GrGLStencilAttachment*>(
2442 renderTarget->renderTargetPriv().getStencilAttachment());
2443 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT,
2444 GR_GL_RENDERBUFFER, stencilAttachment->renderbufferID()));
2445 }
2446
Brian Salomone05ba5a2019-04-08 11:59:07 -04002447 if (rowBytes != tightRowBytes) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002448 SkASSERT(this->glCaps().packRowLengthSupport());
bsalomon@google.comc6980972011-11-02 19:57:21 +00002449 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
2450 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002451
Brian Salomone05ba5a2019-04-08 11:59:07 -04002452 if (!renderTarget) {
2453 this->unbindTextureFBOForPixelOps(GR_GL_FRAMEBUFFER, surface);
2454 }
2455 return true;
2456}
2457
2458bool GrGLGpu::onReadPixels(GrSurface* surface, int left, int top, int width, int height,
2459 GrColorType dstColorType, void* buffer, size_t rowBytes) {
2460 SkASSERT(surface);
2461
2462 // TODO: Avoid this conversion by making GrGLCaps work with color types.
2463 auto dstAsConfig = GrColorTypeToPixelConfig(dstColorType, GrSRGBEncoded::kNo);
2464
2465 int bytesPerPixel = GrBytesPerPixel(dstAsConfig);
2466 size_t tightRowBytes = bytesPerPixel * width;
2467
2468 size_t readDstRowBytes = tightRowBytes;
2469 void* readDst = buffer;
2470
2471 // determine if GL can read using the passed rowBytes or if we need a scratch buffer.
2472 SkAutoSMalloc<32 * sizeof(GrColor)> scratch;
2473 if (rowBytes != tightRowBytes) {
2474 if (this->glCaps().packRowLengthSupport() && !(rowBytes % bytesPerPixel)) {
2475 readDstRowBytes = rowBytes;
2476 } else {
2477 scratch.reset(tightRowBytes * height);
2478 readDst = scratch.get();
2479 }
2480 }
2481 if (!this->readOrTransferPixelsFrom(surface, left, top, width, height, dstColorType, readDst,
2482 readDstRowBytes)) {
2483 return false;
2484 }
2485
Brian Salomona6948702018-06-01 15:33:20 -04002486 if (readDst != buffer) {
bsalomon9d02b262016-02-01 12:49:30 -08002487 SkASSERT(readDst != buffer);
2488 SkASSERT(rowBytes != tightRowBytes);
bsalomon@google.comc6980972011-11-02 19:57:21 +00002489 const char* src = reinterpret_cast<const char*>(readDst);
bsalomon@google.comc4364992011-11-07 15:54:49 +00002490 char* dst = reinterpret_cast<char*>(buffer);
Brian Salomona6948702018-06-01 15:33:20 -04002491 SkRectMemcpy(dst, rowBytes, src, readDstRowBytes, tightRowBytes, height);
reed@google.comac10a2d2010-12-22 21:39:39 +00002492 }
2493 return true;
2494}
2495
Robert Phillips5b5d84c2018-08-09 15:12:18 -04002496GrGpuRTCommandBuffer* GrGLGpu::getCommandBuffer(
Ethan Nicholas56d19a52018-10-15 11:26:20 -04002497 GrRenderTarget* rt, GrSurfaceOrigin origin, const SkRect& bounds,
Robert Phillips6b47c7d2017-08-29 07:24:09 -04002498 const GrGpuRTCommandBuffer::LoadAndStoreInfo& colorInfo,
Greg Daniel500d58b2017-08-24 15:59:33 -04002499 const GrGpuRTCommandBuffer::StencilLoadAndStoreInfo& stencilInfo) {
Robert Phillips5b5d84c2018-08-09 15:12:18 -04002500 if (!fCachedRTCommandBuffer) {
2501 fCachedRTCommandBuffer.reset(new GrGLGpuRTCommandBuffer(this));
2502 }
2503
2504 fCachedRTCommandBuffer->set(rt, origin, colorInfo, stencilInfo);
2505 return fCachedRTCommandBuffer.get();
Greg Daniel500d58b2017-08-24 15:59:33 -04002506}
2507
Robert Phillips5b5d84c2018-08-09 15:12:18 -04002508GrGpuTextureCommandBuffer* GrGLGpu::getCommandBuffer(GrTexture* texture, GrSurfaceOrigin origin) {
2509 if (!fCachedTexCommandBuffer) {
2510 fCachedTexCommandBuffer.reset(new GrGLGpuTextureCommandBuffer(this));
2511 }
2512
2513 fCachedTexCommandBuffer->set(texture, origin);
2514 return fCachedTexCommandBuffer.get();
egdaniel066df7c2016-06-08 14:02:27 -07002515}
2516
Brian Salomon1fabd512018-02-09 09:54:25 -05002517void GrGLGpu::flushRenderTarget(GrGLRenderTarget* target, GrSurfaceOrigin origin,
Chris Daltonc8ece3d2018-07-30 15:03:45 -06002518 const SkIRect& bounds) {
Brian Osman9aa30c62018-07-02 15:21:46 -04002519 this->flushRenderTargetNoColorWrites(target);
Chris Daltonc8ece3d2018-07-30 15:03:45 -06002520 this->didWriteToSurface(target, origin, &bounds);
2521}
bsalomon6ba6fa12015-03-04 11:57:37 -08002522
Chris Daltonc8ece3d2018-07-30 15:03:45 -06002523void GrGLGpu::flushRenderTarget(GrGLRenderTarget* target) {
2524 this->flushRenderTargetNoColorWrites(target);
2525 this->didWriteToSurface(target, kTopLeft_GrSurfaceOrigin, nullptr);
Brian Salomon1fabd512018-02-09 09:54:25 -05002526}
2527
Brian Osman9aa30c62018-07-02 15:21:46 -04002528void GrGLGpu::flushRenderTargetNoColorWrites(GrGLRenderTarget* target) {
Brian Salomon1fabd512018-02-09 09:54:25 -05002529 SkASSERT(target);
Robert Phillips294870f2016-11-11 12:38:40 -05002530 GrGpuResource::UniqueID rtID = target->uniqueID();
egdanield803f272015-03-18 13:01:52 -07002531 if (fHWBoundRenderTargetUniqueID != rtID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -07002532 this->bindFramebuffer(GR_GL_FRAMEBUFFER, target->renderFBOID());
egdanield803f272015-03-18 13:01:52 -07002533#ifdef SK_DEBUG
2534 // don't do this check in Chromium -- this is causing
2535 // lots of repeated command buffer flushes when the compositor is
2536 // rendering with Ganesh, which is really slow; even too slow for
2537 // Debug mode.
cdalton1acea862015-06-02 13:05:52 -07002538 if (kChromium_GrGLDriver != this->glContext().driver()) {
egdanield803f272015-03-18 13:01:52 -07002539 GrGLenum status;
2540 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
2541 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
2542 SkDebugf("GrGLGpu::flushRenderTarget glCheckFramebufferStatus %x\n", status);
2543 }
bsalomon160f24c2015-03-17 15:55:42 -07002544 }
egdanield803f272015-03-18 13:01:52 -07002545#endif
2546 fHWBoundRenderTargetUniqueID = rtID;
bsalomon083617b2016-02-12 12:10:14 -08002547 this->flushViewport(target->getViewport());
brianosman64d094d2016-03-25 06:01:59 -07002548 }
2549
brianosman35b784d2016-05-05 11:52:53 -07002550 if (this->glCaps().srgbWriteControl()) {
Brian Osman9aa30c62018-07-02 15:21:46 -04002551 this->flushFramebufferSRGB(GrPixelConfigIsSRGB(target->config()));
bsalomon5cd020f2015-03-17 12:46:56 -07002552 }
bsalomon083617b2016-02-12 12:10:14 -08002553}
bsalomona9909122016-01-23 10:41:40 -08002554
brianosman33f6b3f2016-06-02 05:49:21 -07002555void GrGLGpu::flushFramebufferSRGB(bool enable) {
2556 if (enable && kYes_TriState != fHWSRGBFramebuffer) {
2557 GL_CALL(Enable(GR_GL_FRAMEBUFFER_SRGB));
2558 fHWSRGBFramebuffer = kYes_TriState;
2559 } else if (!enable && kNo_TriState != fHWSRGBFramebuffer) {
2560 GL_CALL(Disable(GR_GL_FRAMEBUFFER_SRGB));
2561 fHWSRGBFramebuffer = kNo_TriState;
2562 }
2563}
2564
bsalomon083617b2016-02-12 12:10:14 -08002565void GrGLGpu::flushViewport(const GrGLIRect& viewport) {
2566 if (fHWViewport != viewport) {
2567 viewport.pushToGLViewport(this->glInterface());
2568 fHWViewport = viewport;
2569 }
2570}
2571
bsalomon@google.comd302f142011-03-03 13:54:13 +00002572#define SWAP_PER_DRAW 0
2573
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00002574#if SWAP_PER_DRAW
commit-bot@chromium.org43823302013-09-25 20:57:51 +00002575 #if defined(SK_BUILD_FOR_MAC)
bsalomon@google.comd302f142011-03-03 13:54:13 +00002576 #include <AGL/agl.h>
Mike Klein8f11d4d2018-01-24 12:42:55 -05002577 #elif defined(SK_BUILD_FOR_WIN)
bsalomon@google.comce7357d2012-06-25 17:49:25 +00002578 #include <gl/GL.h>
bsalomon@google.comd302f142011-03-03 13:54:13 +00002579 void SwapBuf() {
2580 DWORD procID = GetCurrentProcessId();
2581 HWND hwnd = GetTopWindow(GetDesktopWindow());
2582 while(hwnd) {
2583 DWORD wndProcID = 0;
2584 GetWindowThreadProcessId(hwnd, &wndProcID);
2585 if(wndProcID == procID) {
2586 SwapBuffers(GetDC(hwnd));
2587 }
2588 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
2589 }
2590 }
2591 #endif
2592#endif
2593
Robert Phillipsd0fe8752019-01-31 14:13:59 -05002594void GrGLGpu::draw(GrRenderTarget* renderTarget, GrSurfaceOrigin origin,
2595 const GrPrimitiveProcessor& primProc,
Brian Salomonff168d92018-06-23 15:17:27 -04002596 const GrPipeline& pipeline,
Brian Salomon49348902018-06-26 09:12:38 -04002597 const GrPipeline::FixedDynamicState* fixedDynamicState,
2598 const GrPipeline::DynamicStateArrays* dynamicStateArrays,
bsalomon2eda5b32016-09-21 10:53:24 -07002599 const GrMesh meshes[],
egdaniel9cb63402016-06-23 08:37:05 -07002600 int meshCount) {
2601 this->handleDirtyContext();
2602
bsalomon2eda5b32016-09-21 10:53:24 -07002603 bool hasPoints = false;
2604 for (int i = 0; i < meshCount; ++i) {
Chris Dalton3809bab2017-06-13 10:55:06 -06002605 if (meshes[i].primitiveType() == GrPrimitiveType::kPoints) {
bsalomon2eda5b32016-09-21 10:53:24 -07002606 hasPoints = true;
2607 break;
2608 }
2609 }
Robert Phillipsd0fe8752019-01-31 14:13:59 -05002610 if (!this->flushGLState(renderTarget, origin, primProc, pipeline, fixedDynamicState,
2611 dynamicStateArrays, meshCount, hasPoints)) {
bsalomond95263c2014-12-16 13:05:12 -08002612 return;
2613 }
ethannicholas22793252016-01-30 09:59:10 -08002614
Brian Salomonf7232642018-09-19 08:58:08 -04002615 bool dynamicScissor = false;
2616 bool dynamicPrimProcTextures = false;
2617 if (dynamicStateArrays) {
2618 dynamicScissor = pipeline.isScissorEnabled() && dynamicStateArrays->fScissorRects;
2619 dynamicPrimProcTextures = dynamicStateArrays->fPrimitiveProcessorTextures;
2620 }
2621 for (int m = 0; m < meshCount; ++m) {
Robert Phillipsd0fe8752019-01-31 14:13:59 -05002622 if (GrXferBarrierType barrierType = pipeline.xferBarrierType(renderTarget->asTexture(),
2623 *this->caps())) {
2624 this->xferBarrier(renderTarget, barrierType);
egdaniel0e1853c2016-03-17 11:35:45 -07002625 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002626
Brian Salomon49348902018-06-26 09:12:38 -04002627 if (dynamicScissor) {
Robert Phillipsd0fe8752019-01-31 14:13:59 -05002628 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(renderTarget);
Brian Salomonf7232642018-09-19 08:58:08 -04002629 this->flushScissor(GrScissorState(dynamicStateArrays->fScissorRects[m]),
Robert Phillipsd0fe8752019-01-31 14:13:59 -05002630 glRT->getViewport(), origin);
Chris Dalton46983b72017-06-06 12:27:16 -06002631 }
Brian Salomonf7232642018-09-19 08:58:08 -04002632 if (dynamicPrimProcTextures) {
2633 auto texProxyArray = dynamicStateArrays->fPrimitiveProcessorTextures +
2634 m * primProc.numTextureSamplers();
2635 fHWProgram->updatePrimitiveProcessorTextureBindings(primProc, texProxyArray);
2636 }
Brian Salomon6d9c88b2017-06-12 10:24:42 -04002637 if (this->glCaps().requiresCullFaceEnableDisableWhenDrawingLinesAfterNonLines() &&
Brian Salomonf7232642018-09-19 08:58:08 -04002638 GrIsPrimTypeLines(meshes[m].primitiveType()) &&
Brian Salomon6d9c88b2017-06-12 10:24:42 -04002639 !GrIsPrimTypeLines(fLastPrimitiveType)) {
2640 GL_CALL(Enable(GR_GL_CULL_FACE));
2641 GL_CALL(Disable(GR_GL_CULL_FACE));
2642 }
Brian Salomonf7232642018-09-19 08:58:08 -04002643 meshes[m].sendToGpu(this);
2644 fLastPrimitiveType = meshes[m].primitiveType();
bsalomon@google.com74749cd2013-01-30 16:12:41 +00002645 }
ethannicholas22793252016-01-30 09:59:10 -08002646
bsalomon@google.comd302f142011-03-03 13:54:13 +00002647#if SWAP_PER_DRAW
2648 glFlush();
commit-bot@chromium.org43823302013-09-25 20:57:51 +00002649 #if defined(SK_BUILD_FOR_MAC)
bsalomon@google.comd302f142011-03-03 13:54:13 +00002650 aglSwapBuffers(aglGetCurrentContext());
2651 int set_a_break_pt_here = 9;
2652 aglSwapBuffers(aglGetCurrentContext());
Mike Klein8f11d4d2018-01-24 12:42:55 -05002653 #elif defined(SK_BUILD_FOR_WIN)
bsalomon@google.comd302f142011-03-03 13:54:13 +00002654 SwapBuf();
2655 int set_a_break_pt_here = 9;
2656 SwapBuf();
2657 #endif
2658#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00002659}
2660
Chris Dalton3809bab2017-06-13 10:55:06 -06002661static GrGLenum gr_primitive_type_to_gl_mode(GrPrimitiveType primitiveType) {
2662 switch (primitiveType) {
2663 case GrPrimitiveType::kTriangles:
2664 return GR_GL_TRIANGLES;
2665 case GrPrimitiveType::kTriangleStrip:
2666 return GR_GL_TRIANGLE_STRIP;
Chris Dalton3809bab2017-06-13 10:55:06 -06002667 case GrPrimitiveType::kPoints:
2668 return GR_GL_POINTS;
2669 case GrPrimitiveType::kLines:
2670 return GR_GL_LINES;
2671 case GrPrimitiveType::kLineStrip:
2672 return GR_GL_LINE_STRIP;
2673 case GrPrimitiveType::kLinesAdjacency:
2674 return GR_GL_LINES_ADJACENCY;
2675 }
Ben Wagnerb4aab9a2017-08-16 10:53:04 -04002676 SK_ABORT("invalid GrPrimitiveType");
Chris Dalton3809bab2017-06-13 10:55:06 -06002677 return GR_GL_TRIANGLES;
2678}
2679
Brian Salomon802cb312018-06-08 18:05:20 -04002680void GrGLGpu::sendMeshToGpu(GrPrimitiveType primitiveType, const GrBuffer* vertexBuffer,
2681 int vertexCount, int baseVertex) {
Chris Dalton3809bab2017-06-13 10:55:06 -06002682 const GrGLenum glPrimType = gr_primitive_type_to_gl_mode(primitiveType);
Chris Dalton114a3c02017-05-26 15:17:19 -06002683 if (this->glCaps().drawArraysBaseVertexIsBroken()) {
Brian Salomon802cb312018-06-08 18:05:20 -04002684 this->setupGeometry(nullptr, vertexBuffer, baseVertex, nullptr, 0, GrPrimitiveRestart::kNo);
Chris Dalton114a3c02017-05-26 15:17:19 -06002685 GL_CALL(DrawArrays(glPrimType, 0, vertexCount));
2686 } else {
Brian Salomon802cb312018-06-08 18:05:20 -04002687 this->setupGeometry(nullptr, vertexBuffer, 0, nullptr, 0, GrPrimitiveRestart::kNo);
Chris Dalton114a3c02017-05-26 15:17:19 -06002688 GL_CALL(DrawArrays(glPrimType, baseVertex, vertexCount));
2689 }
2690 fStats.incNumDraws();
2691}
2692
Brian Salomondbf70722019-02-07 11:31:24 -05002693static const GrGLvoid* element_ptr(const GrBuffer* indexBuffer, int baseIndex) {
2694 size_t baseOffset = baseIndex * sizeof(uint16_t);
2695 if (indexBuffer->isCpuBuffer()) {
2696 return static_cast<const GrCpuBuffer*>(indexBuffer)->data() + baseOffset;
2697 } else {
2698 return reinterpret_cast<const GrGLvoid*>(baseOffset);
2699 }
2700}
2701
Brian Salomon802cb312018-06-08 18:05:20 -04002702void GrGLGpu::sendIndexedMeshToGpu(GrPrimitiveType primitiveType, const GrBuffer* indexBuffer,
Chris Dalton114a3c02017-05-26 15:17:19 -06002703 int indexCount, int baseIndex, uint16_t minIndexValue,
2704 uint16_t maxIndexValue, const GrBuffer* vertexBuffer,
Brian Salomon802cb312018-06-08 18:05:20 -04002705 int baseVertex, GrPrimitiveRestart enablePrimitiveRestart) {
Chris Dalton3809bab2017-06-13 10:55:06 -06002706 const GrGLenum glPrimType = gr_primitive_type_to_gl_mode(primitiveType);
Brian Salomondbf70722019-02-07 11:31:24 -05002707 const GrGLvoid* elementPtr = element_ptr(indexBuffer, baseIndex);
Chris Dalton114a3c02017-05-26 15:17:19 -06002708
Brian Salomon802cb312018-06-08 18:05:20 -04002709 this->setupGeometry(indexBuffer, vertexBuffer, baseVertex, nullptr, 0, enablePrimitiveRestart);
Chris Dalton114a3c02017-05-26 15:17:19 -06002710
2711 if (this->glCaps().drawRangeElementsSupport()) {
2712 GL_CALL(DrawRangeElements(glPrimType, minIndexValue, maxIndexValue, indexCount,
Brian Salomondbf70722019-02-07 11:31:24 -05002713 GR_GL_UNSIGNED_SHORT, elementPtr));
Chris Dalton114a3c02017-05-26 15:17:19 -06002714 } else {
Brian Salomondbf70722019-02-07 11:31:24 -05002715 GL_CALL(DrawElements(glPrimType, indexCount, GR_GL_UNSIGNED_SHORT, elementPtr));
Chris Dalton114a3c02017-05-26 15:17:19 -06002716 }
2717 fStats.incNumDraws();
2718}
2719
Brian Salomon802cb312018-06-08 18:05:20 -04002720void GrGLGpu::sendInstancedMeshToGpu(GrPrimitiveType primitiveType, const GrBuffer* vertexBuffer,
Chris Dalton1d616352017-05-31 12:51:23 -06002721 int vertexCount, int baseVertex,
2722 const GrBuffer* instanceBuffer, int instanceCount,
2723 int baseInstance) {
Chris Daltoncc604e52017-10-06 16:27:32 -06002724 GrGLenum glPrimType = gr_primitive_type_to_gl_mode(primitiveType);
Chris Dalton1b4ad762018-10-04 11:58:09 -06002725 int maxInstances = this->glCaps().maxInstancesPerDrawWithoutCrashing(instanceCount);
Chris Daltoncc604e52017-10-06 16:27:32 -06002726 for (int i = 0; i < instanceCount; i += maxInstances) {
Brian Salomon802cb312018-06-08 18:05:20 -04002727 this->setupGeometry(nullptr, vertexBuffer, 0, instanceBuffer, baseInstance + i,
2728 GrPrimitiveRestart::kNo);
Chris Daltoncc604e52017-10-06 16:27:32 -06002729 GL_CALL(DrawArraysInstanced(glPrimType, baseVertex, vertexCount,
2730 SkTMin(instanceCount - i, maxInstances)));
2731 fStats.incNumDraws();
2732 }
Chris Dalton1d616352017-05-31 12:51:23 -06002733}
2734
Brian Salomon802cb312018-06-08 18:05:20 -04002735void GrGLGpu::sendIndexedInstancedMeshToGpu(GrPrimitiveType primitiveType,
Chris Dalton1d616352017-05-31 12:51:23 -06002736 const GrBuffer* indexBuffer, int indexCount,
2737 int baseIndex, const GrBuffer* vertexBuffer,
2738 int baseVertex, const GrBuffer* instanceBuffer,
Brian Salomon802cb312018-06-08 18:05:20 -04002739 int instanceCount, int baseInstance,
2740 GrPrimitiveRestart enablePrimitiveRestart) {
Chris Dalton3809bab2017-06-13 10:55:06 -06002741 const GrGLenum glPrimType = gr_primitive_type_to_gl_mode(primitiveType);
Brian Salomondbf70722019-02-07 11:31:24 -05002742 const GrGLvoid* elementPtr = element_ptr(indexBuffer, baseIndex);
Chris Dalton1b4ad762018-10-04 11:58:09 -06002743 int maxInstances = this->glCaps().maxInstancesPerDrawWithoutCrashing(instanceCount);
2744 for (int i = 0; i < instanceCount; i += maxInstances) {
2745 this->setupGeometry(indexBuffer, vertexBuffer, baseVertex, instanceBuffer, baseInstance + i,
2746 enablePrimitiveRestart);
Brian Salomondbf70722019-02-07 11:31:24 -05002747 GL_CALL(DrawElementsInstanced(glPrimType, indexCount, GR_GL_UNSIGNED_SHORT, elementPtr,
Chris Dalton1b4ad762018-10-04 11:58:09 -06002748 SkTMin(instanceCount - i, maxInstances)));
2749 fStats.incNumDraws();
2750 }
Chris Dalton1d616352017-05-31 12:51:23 -06002751}
2752
Brian Salomon1fabd512018-02-09 09:54:25 -05002753void GrGLGpu::onResolveRenderTarget(GrRenderTarget* target) {
bsalomon@google.com75f9f252012-01-31 13:35:56 +00002754 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00002755 if (rt->needsResolve()) {
bsalomon@google.com347c3822013-05-01 20:10:01 +00002756 // Some extensions automatically resolves the texture when it is read.
2757 if (this->glCaps().usesMSAARenderBuffers()) {
egdanield803f272015-03-18 13:01:52 -07002758 SkASSERT(rt->textureFBOID() != rt->renderFBOID());
Brian Osmancfe83d12018-01-19 11:13:45 -05002759 SkASSERT(rt->textureFBOID() != 0 && rt->renderFBOID() != 0);
Adrienne Walker4ee88512018-05-17 11:37:14 -07002760 this->bindFramebuffer(GR_GL_READ_FRAMEBUFFER, rt->renderFBOID());
2761 this->bindFramebuffer(GR_GL_DRAW_FRAMEBUFFER, rt->textureFBOID());
2762
egdanield803f272015-03-18 13:01:52 -07002763 // make sure we go through flushRenderTarget() since we've modified
2764 // the bound DRAW FBO ID.
Robert Phillips294870f2016-11-11 12:38:40 -05002765 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002766 const GrGLIRect& vp = rt->getViewport();
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00002767 const SkIRect dirtyRect = rt->getResolveRect();
Brian Salomon1fabd512018-02-09 09:54:25 -05002768 // The dirty rect tracked on the RT is always stored in the native coordinates of the
2769 // surface. Choose kTopLeft so no adjustments are made
2770 static constexpr auto kDirtyRectOrigin = kTopLeft_GrSurfaceOrigin;
bsalomon@google.com347c3822013-05-01 20:10:01 +00002771 if (GrGLCaps::kES_Apple_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002772 // Apple's extension uses the scissor as the blit bounds.
Brian Salomond818ebf2018-07-02 14:08:49 +00002773 GrScissorState scissorState;
2774 scissorState.set(dirtyRect);
2775 this->flushScissor(scissorState, vp, kDirtyRectOrigin);
csmartdalton28341fa2016-08-17 10:00:21 -07002776 this->disableWindowRectangles();
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002777 GL_CALL(ResolveMultisampleFramebuffer());
2778 } else {
Brian Salomone5e7eb12016-10-14 16:18:33 -04002779 int l, b, r, t;
2780 if (GrGLCaps::kResolveMustBeFull_BlitFrambufferFlag &
2781 this->glCaps().blitFramebufferSupportFlags()) {
2782 l = 0;
2783 b = 0;
2784 r = target->width();
2785 t = target->height();
2786 } else {
2787 GrGLIRect rect;
Brian Salomon1fabd512018-02-09 09:54:25 -05002788 rect.setRelativeTo(vp, dirtyRect, kDirtyRectOrigin);
Brian Salomone5e7eb12016-10-14 16:18:33 -04002789 l = rect.fLeft;
2790 b = rect.fBottom;
2791 r = rect.fLeft + rect.fWidth;
2792 t = rect.fBottom + rect.fHeight;
2793 }
derekf8c8f71a2014-09-16 06:24:57 -07002794
2795 // BlitFrameBuffer respects the scissor, so disable it.
Brian Salomond818ebf2018-07-02 14:08:49 +00002796 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07002797 this->disableWindowRectangles();
Brian Salomone5e7eb12016-10-14 16:18:33 -04002798 GL_CALL(BlitFramebuffer(l, b, r, t, l, b, r, t,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002799 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00002800 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002801 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00002802 rt->flagAsResolved();
reed@google.comac10a2d2010-12-22 21:39:39 +00002803 }
2804}
2805
bsalomon@google.com411dad02012-06-05 20:24:20 +00002806namespace {
bsalomon@google.comd302f142011-03-03 13:54:13 +00002807
bsalomon@google.com411dad02012-06-05 20:24:20 +00002808
2809GrGLenum gr_to_gl_stencil_op(GrStencilOp op) {
cdalton93a379b2016-05-11 13:58:08 -07002810 static const GrGLenum gTable[kGrStencilOpCount] = {
2811 GR_GL_KEEP, // kKeep
2812 GR_GL_ZERO, // kZero
2813 GR_GL_REPLACE, // kReplace
2814 GR_GL_INVERT, // kInvert
2815 GR_GL_INCR_WRAP, // kIncWrap
2816 GR_GL_DECR_WRAP, // kDecWrap
2817 GR_GL_INCR, // kIncClamp
2818 GR_GL_DECR, // kDecClamp
bsalomon@google.com411dad02012-06-05 20:24:20 +00002819 };
cdalton93a379b2016-05-11 13:58:08 -07002820 GR_STATIC_ASSERT(0 == (int)GrStencilOp::kKeep);
2821 GR_STATIC_ASSERT(1 == (int)GrStencilOp::kZero);
2822 GR_STATIC_ASSERT(2 == (int)GrStencilOp::kReplace);
2823 GR_STATIC_ASSERT(3 == (int)GrStencilOp::kInvert);
2824 GR_STATIC_ASSERT(4 == (int)GrStencilOp::kIncWrap);
2825 GR_STATIC_ASSERT(5 == (int)GrStencilOp::kDecWrap);
2826 GR_STATIC_ASSERT(6 == (int)GrStencilOp::kIncClamp);
2827 GR_STATIC_ASSERT(7 == (int)GrStencilOp::kDecClamp);
2828 SkASSERT(op < (GrStencilOp)kGrStencilOpCount);
2829 return gTable[(int)op];
bsalomon@google.com411dad02012-06-05 20:24:20 +00002830}
2831
2832void set_gl_stencil(const GrGLInterface* gl,
cdalton93a379b2016-05-11 13:58:08 -07002833 const GrStencilSettings::Face& face,
2834 GrGLenum glFace) {
2835 GrGLenum glFunc = GrToGLStencilFunc(face.fTest);
2836 GrGLenum glFailOp = gr_to_gl_stencil_op(face.fFailOp);
2837 GrGLenum glPassOp = gr_to_gl_stencil_op(face.fPassOp);
bsalomon@google.coma3201942012-06-21 19:58:20 +00002838
cdalton93a379b2016-05-11 13:58:08 -07002839 GrGLint ref = face.fRef;
2840 GrGLint mask = face.fTestMask;
2841 GrGLint writeMask = face.fWriteMask;
bsalomon@google.com411dad02012-06-05 20:24:20 +00002842
2843 if (GR_GL_FRONT_AND_BACK == glFace) {
2844 // we call the combined func just in case separate stencil is not
2845 // supported.
2846 GR_GL_CALL(gl, StencilFunc(glFunc, ref, mask));
2847 GR_GL_CALL(gl, StencilMask(writeMask));
vbuzinovc5d58f02015-08-21 05:24:24 -07002848 GR_GL_CALL(gl, StencilOp(glFailOp, GR_GL_KEEP, glPassOp));
bsalomon@google.com411dad02012-06-05 20:24:20 +00002849 } else {
2850 GR_GL_CALL(gl, StencilFuncSeparate(glFace, glFunc, ref, mask));
2851 GR_GL_CALL(gl, StencilMaskSeparate(glFace, writeMask));
vbuzinovc5d58f02015-08-21 05:24:24 -07002852 GR_GL_CALL(gl, StencilOpSeparate(glFace, glFailOp, GR_GL_KEEP, glPassOp));
bsalomon@google.com411dad02012-06-05 20:24:20 +00002853 }
2854}
2855}
bsalomon@google.comd302f142011-03-03 13:54:13 +00002856
bsalomon3e791242014-12-17 13:43:13 -08002857void GrGLGpu::flushStencil(const GrStencilSettings& stencilSettings) {
csmartdaltonc7d85332016-10-26 10:13:46 -07002858 if (stencilSettings.isDisabled()) {
2859 this->disableStencil();
2860 } else if (fHWStencilSettings != stencilSettings) {
2861 if (kYes_TriState != fHWStencilTestEnabled) {
2862 GL_CALL(Enable(GR_GL_STENCIL_TEST));
Brian Salomonaf971de2017-06-08 16:11:33 -04002863
csmartdaltonc7d85332016-10-26 10:13:46 -07002864 fHWStencilTestEnabled = kYes_TriState;
bsalomon@google.coma3201942012-06-21 19:58:20 +00002865 }
csmartdaltonc7d85332016-10-26 10:13:46 -07002866 if (stencilSettings.isTwoSided()) {
csmartdaltonc7d85332016-10-26 10:13:46 -07002867 set_gl_stencil(this->glInterface(),
2868 stencilSettings.front(),
2869 GR_GL_FRONT);
2870 set_gl_stencil(this->glInterface(),
2871 stencilSettings.back(),
2872 GR_GL_BACK);
2873 } else {
2874 set_gl_stencil(this->glInterface(),
2875 stencilSettings.front(),
2876 GR_GL_FRONT_AND_BACK);
bsalomon@google.comd302f142011-03-03 13:54:13 +00002877 }
joshualitta58fe352014-10-27 08:39:00 -07002878 fHWStencilSettings = stencilSettings;
reed@google.comac10a2d2010-12-22 21:39:39 +00002879 }
2880}
2881
csmartdaltonc7d85332016-10-26 10:13:46 -07002882void GrGLGpu::disableStencil() {
2883 if (kNo_TriState != fHWStencilTestEnabled) {
2884 GL_CALL(Disable(GR_GL_STENCIL_TEST));
Brian Salomonaf971de2017-06-08 16:11:33 -04002885
csmartdaltonc7d85332016-10-26 10:13:46 -07002886 fHWStencilTestEnabled = kNo_TriState;
2887 fHWStencilSettings.invalidate();
2888 }
2889}
2890
Chris Dalton4c56b032019-03-04 12:28:42 -07002891void GrGLGpu::flushHWAAState(GrRenderTarget* rt, bool useHWAA) {
bsalomon083617b2016-02-12 12:10:14 -08002892 // rt is only optional if useHWAA is false.
2893 SkASSERT(rt || !useHWAA);
vbuzinovdded6962015-06-12 08:59:45 -07002894 SkASSERT(!useHWAA || rt->isStencilBufferMultisampled());
bsalomon@google.com202d1392013-03-19 18:58:08 +00002895
csmartdalton2b5f2cb2016-06-10 14:06:32 -07002896 if (this->caps()->multisampleDisableSupport()) {
cdaltond0a840d2015-03-16 17:19:58 -07002897 if (useHWAA) {
2898 if (kYes_TriState != fMSAAEnabled) {
2899 GL_CALL(Enable(GR_GL_MULTISAMPLE));
2900 fMSAAEnabled = kYes_TriState;
2901 }
2902 } else {
2903 if (kNo_TriState != fMSAAEnabled) {
2904 GL_CALL(Disable(GR_GL_MULTISAMPLE));
2905 fMSAAEnabled = kNo_TriState;
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00002906 }
2907 }
2908 }
2909}
2910
bsalomon7f9b2e42016-01-12 13:29:26 -08002911void GrGLGpu::flushBlend(const GrXferProcessor::BlendInfo& blendInfo, const GrSwizzle& swizzle) {
egdanielb414f252014-07-29 13:15:47 -07002912 // Any optimization to disable blending should have already been applied and
cdalton8917d622015-05-06 13:40:21 -07002913 // tweaked the equation to "add" or "subtract", and the coeffs to (1, 0).
bsalomonf7cc8772015-05-11 11:21:14 -07002914
cdalton8917d622015-05-06 13:40:21 -07002915 GrBlendEquation equation = blendInfo.fEquation;
egdanielc2304142014-12-11 13:15:13 -08002916 GrBlendCoeff srcCoeff = blendInfo.fSrcBlend;
2917 GrBlendCoeff dstCoeff = blendInfo.fDstBlend;
Dongseong Hwang4e1f0222018-11-01 09:10:51 -07002918 bool blendOff =
2919 ((kAdd_GrBlendEquation == equation || kSubtract_GrBlendEquation == equation) &&
2920 kOne_GrBlendCoeff == srcCoeff && kZero_GrBlendCoeff == dstCoeff) ||
2921 !blendInfo.fWriteColor;
egdanielb414f252014-07-29 13:15:47 -07002922 if (blendOff) {
2923 if (kNo_TriState != fHWBlendState.fEnabled) {
2924 GL_CALL(Disable(GR_GL_BLEND));
joel.liang9764c402015-07-09 19:46:18 -07002925
2926 // Workaround for the ARM KHR_blend_equation_advanced blacklist issue
2927 // https://code.google.com/p/skia/issues/detail?id=3943
2928 if (kARM_GrGLVendor == this->ctxInfo().vendor() &&
2929 GrBlendEquationIsAdvanced(fHWBlendState.fEquation)) {
2930 SkASSERT(this->caps()->advancedBlendEquationSupport());
2931 // Set to any basic blending equation.
2932 GrBlendEquation blend_equation = kAdd_GrBlendEquation;
2933 GL_CALL(BlendEquation(gXfermodeEquation2Blend[blend_equation]));
2934 fHWBlendState.fEquation = blend_equation;
2935 }
2936
egdanielb414f252014-07-29 13:15:47 -07002937 fHWBlendState.fEnabled = kNo_TriState;
2938 }
cdalton8917d622015-05-06 13:40:21 -07002939 return;
2940 }
2941
2942 if (kYes_TriState != fHWBlendState.fEnabled) {
2943 GL_CALL(Enable(GR_GL_BLEND));
Brian Salomonaf971de2017-06-08 16:11:33 -04002944
cdalton8917d622015-05-06 13:40:21 -07002945 fHWBlendState.fEnabled = kYes_TriState;
2946 }
2947
Mike Kleinab5fec92018-08-16 19:43:31 +00002948 if (fHWBlendState.fEquation != equation) {
cdalton8917d622015-05-06 13:40:21 -07002949 GL_CALL(BlendEquation(gXfermodeEquation2Blend[equation]));
2950 fHWBlendState.fEquation = equation;
2951 }
2952
2953 if (GrBlendEquationIsAdvanced(equation)) {
2954 SkASSERT(this->caps()->advancedBlendEquationSupport());
2955 // Advanced equations have no other blend state.
2956 return;
2957 }
2958
Mike Kleinab5fec92018-08-16 19:43:31 +00002959 if (fHWBlendState.fSrcCoeff != srcCoeff || fHWBlendState.fDstCoeff != dstCoeff) {
cdalton8917d622015-05-06 13:40:21 -07002960 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
2961 gXfermodeCoeff2Blend[dstCoeff]));
2962 fHWBlendState.fSrcCoeff = srcCoeff;
2963 fHWBlendState.fDstCoeff = dstCoeff;
2964 }
2965
bsalomon7f9b2e42016-01-12 13:29:26 -08002966 if ((BlendCoeffReferencesConstant(srcCoeff) || BlendCoeffReferencesConstant(dstCoeff))) {
Brian Osman422f95b2018-11-05 16:49:04 -05002967 SkPMColor4f blendConst = swizzle.applyTo(blendInfo.fBlendConstant);
bsalomon7f9b2e42016-01-12 13:29:26 -08002968 if (!fHWBlendState.fConstColorValid || fHWBlendState.fConstColor != blendConst) {
Brian Osman422f95b2018-11-05 16:49:04 -05002969 GL_CALL(BlendColor(blendConst.fR, blendConst.fG, blendConst.fB, blendConst.fA));
bsalomon7f9b2e42016-01-12 13:29:26 -08002970 fHWBlendState.fConstColor = blendConst;
2971 fHWBlendState.fConstColorValid = true;
2972 }
bsalomon@google.com0650e812011-04-08 18:07:53 +00002973 }
2974}
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002975
Brian Salomondc829942018-10-23 16:07:24 -04002976static void get_gl_swizzle_values(const GrSwizzle& swizzle, GrGLenum glValues[4]) {
Brian Salomon327955b2018-10-22 15:39:22 +00002977 for (int i = 0; i < 4; ++i) {
Brian Salomondc829942018-10-23 16:07:24 -04002978 switch (swizzle[i]) {
2979 case 'r': glValues[i] = GR_GL_RED; break;
2980 case 'g': glValues[i] = GR_GL_GREEN; break;
2981 case 'b': glValues[i] = GR_GL_BLUE; break;
2982 case 'a': glValues[i] = GR_GL_ALPHA; break;
Greg Daniel216a9d72019-02-20 10:12:29 -05002983 case '1': glValues[i] = GR_GL_ONE; break;
Brian Salomondc829942018-10-23 16:07:24 -04002984 default: SK_ABORT("Unsupported component");
2985 }
Brian Salomon327955b2018-10-22 15:39:22 +00002986 }
2987}
2988
Brian Salomondc829942018-10-23 16:07:24 -04002989void GrGLGpu::bindTexture(int unitIdx, GrSamplerState samplerState, GrGLTexture* texture) {
bsalomon49f085d2014-09-05 13:34:00 -07002990 SkASSERT(texture);
tomhudson@google.comd0c1a062012-07-12 17:23:52 +00002991
reed856e9d92015-09-30 12:21:45 -07002992#ifdef SK_DEBUG
2993 if (!this->caps()->npotTextureTileSupport()) {
Brian Salomon2bbdcc42017-09-07 12:36:34 -04002994 if (samplerState.isRepeated()) {
reed856e9d92015-09-30 12:21:45 -07002995 const int w = texture->width();
2996 const int h = texture->height();
2997 SkASSERT(SkIsPow2(w) && SkIsPow2(h));
2998 }
2999 }
3000#endif
3001
bsalomon@google.comb8670992012-07-25 21:27:09 +00003002 // If we created a rt/tex and rendered to it without using a texture and now we're texturing
3003 // 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 +00003004 // out of the "last != next" check.
bsalomon37dd3312014-11-03 08:47:23 -08003005 GrGLRenderTarget* texRT = static_cast<GrGLRenderTarget*>(texture->asRenderTarget());
bsalomon49f085d2014-09-05 13:34:00 -07003006 if (texRT) {
Brian Salomon1fabd512018-02-09 09:54:25 -05003007 this->onResolveRenderTarget(texRT);
bsalomon@google.com4c883782012-06-04 19:05:11 +00003008 }
3009
Robert Phillips294870f2016-11-11 12:38:40 -05003010 GrGpuResource::UniqueID textureID = texture->uniqueID();
bsalomon10528f12015-10-14 12:54:52 -07003011 GrGLenum target = texture->target();
Brian Salomond978b902019-02-07 15:09:18 -05003012 if (fHWTextureUnitBindings[unitIdx].boundID(target) != textureID) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00003013 this->setTextureUnit(unitIdx);
bsalomon10528f12015-10-14 12:54:52 -07003014 GL_CALL(BindTexture(target, texture->textureID()));
Brian Salomond978b902019-02-07 15:09:18 -05003015 fHWTextureUnitBindings[unitIdx].setBoundID(target, textureID);
bsalomon@google.com4c883782012-06-04 19:05:11 +00003016 }
3017
Brian Salomondc829942018-10-23 16:07:24 -04003018 if (samplerState.filter() == GrSamplerState::Filter::kMipMap) {
Greg Daniel8f5bbda2018-06-08 17:22:23 -04003019 if (!this->caps()->mipMapSupport() ||
3020 texture->texturePriv().mipMapped() == GrMipMapped::kNo) {
Brian Salomondc829942018-10-23 16:07:24 -04003021 samplerState.setFilterMode(GrSamplerState::Filter::kBilerp);
bsalomonefd7d452014-10-23 14:17:46 -07003022 }
commit-bot@chromium.org47442312013-12-19 16:18:01 +00003023 }
bsalomonefd7d452014-10-23 14:17:46 -07003024
brianosman33f6b3f2016-06-02 05:49:21 -07003025#ifdef SK_DEBUG
Brian Osman2b23c4b2018-06-01 12:25:08 -04003026 // We were supposed to ensure MipMaps were up-to-date before getting here.
Brian Salomondc829942018-10-23 16:07:24 -04003027 if (samplerState.filter() == GrSamplerState::Filter::kMipMap) {
brianosman33f6b3f2016-06-02 05:49:21 -07003028 SkASSERT(!texture->texturePriv().mipMapsAreDirty());
brianosman33f6b3f2016-06-02 05:49:21 -07003029 }
3030#endif
3031
Brian Salomondc829942018-10-23 16:07:24 -04003032 ResetTimestamp timestamp = texture->getCachedParamsTimestamp();
3033 bool setAll = timestamp < this->getResetTimestamp();
cblume55f2d2d2016-02-26 13:20:48 -08003034
Brian Salomondc829942018-10-23 16:07:24 -04003035 const GrGLTexture::SamplerParams* samplerParamsToRecord = nullptr;
3036 GrGLTexture::SamplerParams newSamplerParams;
3037 if (fSamplerObjectCache) {
3038 fSamplerObjectCache->bindSampler(unitIdx, samplerState);
3039 } else {
3040 const GrGLTexture::SamplerParams& oldSamplerParams = texture->getCachedSamplerParams();
3041 samplerParamsToRecord = &newSamplerParams;
3042
3043 newSamplerParams.fMinFilter = filter_to_gl_min_filter(samplerState.filter());
3044 newSamplerParams.fMagFilter = filter_to_gl_mag_filter(samplerState.filter());
3045
Michael Ludwigf23a1522018-12-10 11:36:13 -05003046 newSamplerParams.fWrapS = wrap_mode_to_gl_wrap(samplerState.wrapModeX(), this->glCaps());
3047 newSamplerParams.fWrapT = wrap_mode_to_gl_wrap(samplerState.wrapModeY(), this->glCaps());
Brian Salomondc829942018-10-23 16:07:24 -04003048
3049 // These are the OpenGL default values.
Brian Salomon89f2ff12018-12-07 19:30:25 -05003050 newSamplerParams.fMinLOD = -1000.f;
3051 newSamplerParams.fMaxLOD = 1000.f;
Brian Salomondc829942018-10-23 16:07:24 -04003052
3053 if (setAll || newSamplerParams.fMagFilter != oldSamplerParams.fMagFilter) {
3054 this->setTextureUnit(unitIdx);
3055 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MAG_FILTER, newSamplerParams.fMagFilter));
3056 }
3057 if (setAll || newSamplerParams.fMinFilter != oldSamplerParams.fMinFilter) {
3058 this->setTextureUnit(unitIdx);
3059 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MIN_FILTER, newSamplerParams.fMinFilter));
3060 }
Mike Klein1bccae52018-10-19 11:38:44 +00003061 if (this->glCaps().mipMapLevelAndLodControlSupport()) {
Brian Salomondc829942018-10-23 16:07:24 -04003062 if (setAll || newSamplerParams.fMinLOD != oldSamplerParams.fMinLOD) {
Mike Klein1bccae52018-10-19 11:38:44 +00003063 this->setTextureUnit(unitIdx);
Brian Salomon89f2ff12018-12-07 19:30:25 -05003064 GL_CALL(TexParameterf(target, GR_GL_TEXTURE_MIN_LOD, newSamplerParams.fMinLOD));
Brian Salomondc829942018-10-23 16:07:24 -04003065 }
3066 if (setAll || newSamplerParams.fMaxLOD != oldSamplerParams.fMaxLOD) {
3067 this->setTextureUnit(unitIdx);
Brian Salomon89f2ff12018-12-07 19:30:25 -05003068 GL_CALL(TexParameterf(target, GR_GL_TEXTURE_MAX_LOD, newSamplerParams.fMaxLOD));
Brian Salomondc829942018-10-23 16:07:24 -04003069 }
3070 }
3071 if (setAll || newSamplerParams.fWrapS != oldSamplerParams.fWrapS) {
3072 this->setTextureUnit(unitIdx);
3073 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_WRAP_S, newSamplerParams.fWrapS));
3074 }
3075 if (setAll || newSamplerParams.fWrapT != oldSamplerParams.fWrapT) {
3076 this->setTextureUnit(unitIdx);
3077 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_WRAP_T, newSamplerParams.fWrapT));
3078 }
Michael Ludwigf23a1522018-12-10 11:36:13 -05003079 if (this->glCaps().clampToBorderSupport()) {
3080 // Make sure the border color is transparent black (the default)
3081 if (setAll || oldSamplerParams.fBorderColorInvalid) {
3082 this->setTextureUnit(unitIdx);
Brian Salomon89f2ff12018-12-07 19:30:25 -05003083 static const GrGLfloat kTransparentBlack[4] = {0.f, 0.f, 0.f, 0.f};
3084 GL_CALL(TexParameterfv(target, GR_GL_TEXTURE_BORDER_COLOR, kTransparentBlack));
Michael Ludwigf23a1522018-12-10 11:36:13 -05003085 }
3086 }
Brian Salomondc829942018-10-23 16:07:24 -04003087 }
3088 GrGLTexture::NonSamplerParams newNonSamplerParams;
3089 newNonSamplerParams.fBaseMipMapLevel = 0;
3090 newNonSamplerParams.fMaxMipMapLevel = texture->texturePriv().maxMipMapLevel();
3091
3092 const GrGLTexture::NonSamplerParams& oldNonSamplerParams = texture->getCachedNonSamplerParams();
3093 if (this->glCaps().textureSwizzleSupport()) {
3094 auto swizzle = this->glCaps().configSwizzle(texture->config());
3095 newNonSamplerParams.fSwizzleKey = swizzle.asKey();
3096 if (setAll || swizzle.asKey() != oldNonSamplerParams.fSwizzleKey) {
3097 GrGLenum glValues[4];
3098 get_gl_swizzle_values(swizzle, glValues);
3099 this->setTextureUnit(unitIdx);
Kevin Lubick8aa203c2019-03-19 13:23:10 -04003100 if (GR_IS_GR_GL(this->glStandard())) {
3101 GR_STATIC_ASSERT(sizeof(glValues[0]) == sizeof(GrGLint));
3102 GL_CALL(TexParameteriv(target, GR_GL_TEXTURE_SWIZZLE_RGBA,
3103 reinterpret_cast<const GrGLint*>(glValues)));
3104 } else if (GR_IS_GR_GL_ES(this->glStandard())) {
Brian Salomondc829942018-10-23 16:07:24 -04003105 // ES3 added swizzle support but not GL_TEXTURE_SWIZZLE_RGBA.
3106 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_R, glValues[0]));
3107 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_G, glValues[1]));
3108 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_B, glValues[2]));
3109 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_A, glValues[3]));
Brian Salomonbfb3df42018-10-19 19:24:31 +00003110 }
Brian Salomonbfb3df42018-10-19 19:24:31 +00003111 }
3112 }
Brian Salomondc829942018-10-23 16:07:24 -04003113 // These are not supported in ES2 contexts
Brian Salomonf3841932018-11-29 09:13:37 -05003114 if (this->glCaps().mipMapLevelAndLodControlSupport() &&
3115 (texture->texturePriv().textureType() != GrTextureType::kExternal ||
3116 !this->glCaps().dontSetBaseOrMaxLevelForExternalTextures())) {
Brian Salomondc829942018-10-23 16:07:24 -04003117 if (newNonSamplerParams.fBaseMipMapLevel != oldNonSamplerParams.fBaseMipMapLevel) {
3118 this->setTextureUnit(unitIdx);
3119 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_BASE_LEVEL,
3120 newNonSamplerParams.fBaseMipMapLevel));
3121 }
3122 if (newNonSamplerParams.fMaxMipMapLevel != oldNonSamplerParams.fMaxMipMapLevel) {
3123 this->setTextureUnit(unitIdx);
3124 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MAX_LEVEL,
3125 newNonSamplerParams.fMaxMipMapLevel));
3126 }
Brian Salomon327955b2018-10-22 15:39:22 +00003127 }
Brian Salomondc829942018-10-23 16:07:24 -04003128 texture->setCachedParams(samplerParamsToRecord, newNonSamplerParams, this->getResetTimestamp());
cdalton74b8d322016-04-11 14:47:28 -07003129}
3130
Brian Salomon1f05d452019-02-08 12:33:08 -05003131void GrGLGpu::onResetTextureBindings() {
3132 static constexpr GrGLenum kTargets[] = {GR_GL_TEXTURE_2D, GR_GL_TEXTURE_RECTANGLE,
3133 GR_GL_TEXTURE_EXTERNAL};
3134 for (int i = 0; i < this->numTextureUnits(); ++i) {
3135 this->setTextureUnit(i);
3136 for (auto target : kTargets) {
3137 if (fHWTextureUnitBindings[i].hasBeenModified(target)) {
3138 GL_CALL(BindTexture(target, 0));
3139 }
3140 }
3141 fHWTextureUnitBindings[i].invalidateAllTargets(true);
3142 }
3143}
3144
egdaniel080e6732014-12-22 07:35:52 -08003145void GrGLGpu::flushColorWrite(bool writeColor) {
3146 if (!writeColor) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +00003147 if (kNo_TriState != fHWWriteToColor) {
3148 GL_CALL(ColorMask(GR_GL_FALSE, GR_GL_FALSE,
3149 GR_GL_FALSE, GR_GL_FALSE));
3150 fHWWriteToColor = kNo_TriState;
bsalomon@google.comd302f142011-03-03 13:54:13 +00003151 }
bsalomon@google.com978c8c62012-05-21 14:45:49 +00003152 } else {
3153 if (kYes_TriState != fHWWriteToColor) {
3154 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
3155 fHWWriteToColor = kYes_TriState;
3156 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00003157 }
bsalomon3e791242014-12-17 13:43:13 -08003158}
bsalomon@google.comd302f142011-03-03 13:54:13 +00003159
Brian Salomon805cc7a2019-01-28 09:52:34 -05003160void GrGLGpu::flushClearColor(GrGLfloat r, GrGLfloat g, GrGLfloat b, GrGLfloat a) {
3161 if (r != fHWClearColor[0] || g != fHWClearColor[1] ||
3162 b != fHWClearColor[2] || a != fHWClearColor[3]) {
3163 GL_CALL(ClearColor(r, g, b, a));
3164 fHWClearColor[0] = r;
3165 fHWClearColor[1] = g;
3166 fHWClearColor[2] = b;
3167 fHWClearColor[3] = a;
3168 }
3169}
3170
bsalomon861e1032014-12-16 07:33:49 -08003171void GrGLGpu::setTextureUnit(int unit) {
Brian Salomond978b902019-02-07 15:09:18 -05003172 SkASSERT(unit >= 0 && unit < this->numTextureUnits());
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00003173 if (unit != fHWActiveTextureUnitIdx) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00003174 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com49209392012-06-05 15:13:46 +00003175 fHWActiveTextureUnitIdx = unit;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00003176 }
3177}
bsalomon@google.com316f99232011-01-13 21:28:12 +00003178
Brian Salomond978b902019-02-07 15:09:18 -05003179void GrGLGpu::bindTextureToScratchUnit(GrGLenum target, GrGLint textureID) {
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00003180 // Bind the last texture unit since it is the least likely to be used by GrGLProgram.
Brian Salomond978b902019-02-07 15:09:18 -05003181 int lastUnitIdx = this->numTextureUnits() - 1;
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00003182 if (lastUnitIdx != fHWActiveTextureUnitIdx) {
3183 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + lastUnitIdx));
3184 fHWActiveTextureUnitIdx = lastUnitIdx;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00003185 }
Brian Salomond978b902019-02-07 15:09:18 -05003186 // Clear out the this field so that if a GrGLProgram does use this unit it will rebind the
3187 // correct texture.
Brian Salomon1f05d452019-02-08 12:33:08 -05003188 fHWTextureUnitBindings[lastUnitIdx].invalidateForScratchUse(target);
Brian Salomond978b902019-02-07 15:09:18 -05003189 GL_CALL(BindTexture(target, textureID));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00003190}
3191
Brian Salomone5e7eb12016-10-14 16:18:33 -04003192// Determines whether glBlitFramebuffer could be used between src and dst by onCopySurface.
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003193static inline bool can_blit_framebuffer_for_copy_surface(
3194 const GrSurface* dst, GrSurfaceOrigin dstOrigin,
3195 const GrSurface* src, GrSurfaceOrigin srcOrigin,
3196 const SkIRect& srcRect,
3197 const SkIPoint& dstPoint,
Greg Daniel26dbe3b2018-05-03 10:35:42 -04003198 const GrGLCaps& caps) {
3199 int dstSampleCnt = 0;
3200 int srcSampleCnt = 0;
3201 if (const GrRenderTarget* rt = dst->asRenderTarget()) {
3202 dstSampleCnt = rt->numColorSamples();
bsalomon@google.comeb851172013-04-15 13:51:00 +00003203 }
Greg Daniel26dbe3b2018-05-03 10:35:42 -04003204 if (const GrRenderTarget* rt = src->asRenderTarget()) {
3205 srcSampleCnt = rt->numColorSamples();
3206 }
3207 SkASSERT((dstSampleCnt > 0) == SkToBool(dst->asRenderTarget()));
3208 SkASSERT((srcSampleCnt > 0) == SkToBool(src->asRenderTarget()));
3209
Brian Salomone5e7eb12016-10-14 16:18:33 -04003210 const GrGLTexture* dstTex = static_cast<const GrGLTexture*>(dst->asTexture());
Mike Klein1d746202018-01-25 17:32:51 -05003211 const GrGLTexture* srcTex = static_cast<const GrGLTexture*>(src->asTexture());
Greg Daniel26dbe3b2018-05-03 10:35:42 -04003212
3213 bool dstIsGLTexture2D = dstTex ? GR_GL_TEXTURE_2D == dstTex->target() : false;
3214 bool srcIsGLTexture2D = srcTex ? GR_GL_TEXTURE_2D == srcTex->target() : false;
3215
3216 return caps.canCopyAsBlit(dst->config(), dstSampleCnt, SkToBool(dstTex), dstIsGLTexture2D,
3217 dstOrigin, src->config(), srcSampleCnt, SkToBool(srcTex),
3218 srcIsGLTexture2D, srcOrigin, src->getBoundsRect(), srcRect, dstPoint);
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00003219}
bsalomon@google.comeb851172013-04-15 13:51:00 +00003220
Brian Osmana9c8a052018-01-19 10:31:56 -05003221static bool rt_has_msaa_render_buffer(const GrGLRenderTarget* rt, const GrGLCaps& glCaps) {
3222 // A RT has a separate MSAA renderbuffer if:
3223 // 1) It's multisampled
3224 // 2) We're using an extension with separate MSAA renderbuffers
3225 // 3) It's not FBO 0, which is special and always auto-resolves
Brian Salomonbdecacf2018-02-02 20:32:49 -05003226 return rt->numColorSamples() > 1 && glCaps.usesMSAARenderBuffers() && rt->renderFBOID() != 0;
Brian Osmana9c8a052018-01-19 10:31:56 -05003227}
3228
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003229static inline bool can_copy_texsubimage(const GrSurface* dst, GrSurfaceOrigin dstOrigin,
3230 const GrSurface* src, GrSurfaceOrigin srcOrigin,
Greg Daniel26dbe3b2018-05-03 10:35:42 -04003231 const GrGLCaps& caps) {
3232
bsalomon@google.comeb851172013-04-15 13:51:00 +00003233 const GrGLRenderTarget* dstRT = static_cast<const GrGLRenderTarget*>(dst->asRenderTarget());
bsalomon@google.coma2719852013-04-17 14:25:27 +00003234 const GrGLRenderTarget* srcRT = static_cast<const GrGLRenderTarget*>(src->asRenderTarget());
bsalomon7ea33f52015-11-22 14:51:00 -08003235 const GrGLTexture* dstTex = static_cast<const GrGLTexture*>(dst->asTexture());
bsalomon7ea33f52015-11-22 14:51:00 -08003236 const GrGLTexture* srcTex = static_cast<const GrGLTexture*>(src->asTexture());
cblume61214052016-01-26 09:10:48 -08003237
Greg Daniel26dbe3b2018-05-03 10:35:42 -04003238 bool dstHasMSAARenderBuffer = dstRT ? rt_has_msaa_render_buffer(dstRT, caps) : false;
3239 bool srcHasMSAARenderBuffer = srcRT ? rt_has_msaa_render_buffer(srcRT, caps) : false;
3240
3241 bool dstIsGLTexture2D = dstTex ? GR_GL_TEXTURE_2D == dstTex->target() : false;
3242 bool srcIsGLTexture2D = srcTex ? GR_GL_TEXTURE_2D == srcTex->target() : false;
3243
3244 return caps.canCopyTexSubImage(dst->config(), dstHasMSAARenderBuffer, SkToBool(dstTex),
3245 dstIsGLTexture2D, dstOrigin, src->config(),
3246 srcHasMSAARenderBuffer, SkToBool(srcTex), srcIsGLTexture2D,
3247 srcOrigin);
bsalomon@google.comeb851172013-04-15 13:51:00 +00003248}
3249
3250// If a temporary FBO was created, its non-zero ID is returned. The viewport that the copy rect is
3251// relative to is output.
Brian Salomon71d9d842016-11-03 13:42:00 -04003252void GrGLGpu::bindSurfaceFBOForPixelOps(GrSurface* surface, GrGLenum fboTarget, GrGLIRect* viewport,
3253 TempFBOTarget tempFBOTarget) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00003254 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(surface->asRenderTarget());
bsalomon083617b2016-02-12 12:10:14 -08003255 if (!rt) {
bsalomon49f085d2014-09-05 13:34:00 -07003256 SkASSERT(surface->asTexture());
Brian Salomon9bada542017-06-12 12:09:30 -04003257 GrGLTexture* texture = static_cast<GrGLTexture*>(surface->asTexture());
3258 GrGLuint texID = texture->textureID();
3259 GrGLenum target = texture->target();
egdanield803f272015-03-18 13:01:52 -07003260 GrGLuint* tempFBOID;
3261 tempFBOID = kSrc_TempFBOTarget == tempFBOTarget ? &fTempSrcFBOID : &fTempDstFBOID;
egdaniel0f5f9672015-02-03 11:10:51 -08003262
egdanield803f272015-03-18 13:01:52 -07003263 if (0 == *tempFBOID) {
3264 GR_GL_CALL(this->glInterface(), GenFramebuffers(1, tempFBOID));
egdaniel0f5f9672015-02-03 11:10:51 -08003265 }
3266
Adrienne Walker4ee88512018-05-17 11:37:14 -07003267 this->bindFramebuffer(fboTarget, *tempFBOID);
egdanield803f272015-03-18 13:01:52 -07003268 GR_GL_CALL(this->glInterface(), FramebufferTexture2D(fboTarget,
robertphillips754f4e92014-09-18 13:52:08 -07003269 GR_GL_COLOR_ATTACHMENT0,
bsalomon10528f12015-10-14 12:54:52 -07003270 target,
robertphillips754f4e92014-09-18 13:52:08 -07003271 texID,
3272 0));
Brian Salomon9bada542017-06-12 12:09:30 -04003273 texture->baseLevelWasBoundToFBO();
bsalomon@google.comeb851172013-04-15 13:51:00 +00003274 viewport->fLeft = 0;
3275 viewport->fBottom = 0;
3276 viewport->fWidth = surface->width();
3277 viewport->fHeight = surface->height();
3278 } else {
Adrienne Walker4ee88512018-05-17 11:37:14 -07003279 this->bindFramebuffer(fboTarget, rt->renderFBOID());
bsalomon@google.comeb851172013-04-15 13:51:00 +00003280 *viewport = rt->getViewport();
3281 }
egdaniel0f5f9672015-02-03 11:10:51 -08003282}
3283
Brian Salomon71d9d842016-11-03 13:42:00 -04003284void GrGLGpu::unbindTextureFBOForPixelOps(GrGLenum fboTarget, GrSurface* surface) {
3285 // bindSurfaceFBOForPixelOps temporarily binds textures that are not render targets to
bsalomon10528f12015-10-14 12:54:52 -07003286 if (!surface->asRenderTarget()) {
3287 SkASSERT(surface->asTexture());
3288 GrGLenum textureTarget = static_cast<GrGLTexture*>(surface->asTexture())->target();
3289 GR_GL_CALL(this->glInterface(), FramebufferTexture2D(fboTarget,
3290 GR_GL_COLOR_ATTACHMENT0,
3291 textureTarget,
3292 0,
3293 0));
3294 }
bsalomon@google.comeb851172013-04-15 13:51:00 +00003295}
3296
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07003297void GrGLGpu::onFBOChanged() {
3298 if (this->caps()->workarounds().flush_on_framebuffer_change ||
3299 this->caps()->workarounds().restore_scissor_on_fbo_change) {
3300 GL_CALL(Flush());
3301 }
3302}
3303
Adrienne Walker4ee88512018-05-17 11:37:14 -07003304void GrGLGpu::bindFramebuffer(GrGLenum target, GrGLuint fboid) {
3305 fStats.incRenderTargetBinds();
3306 GL_CALL(BindFramebuffer(target, fboid));
3307 if (target == GR_GL_FRAMEBUFFER || target == GR_GL_DRAW_FRAMEBUFFER) {
3308 fBoundDrawFramebuffer = fboid;
3309 }
3310
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07003311 if (this->caps()->workarounds().restore_scissor_on_fbo_change) {
3312 // The driver forgets the correct scissor when modifying the FBO binding.
3313 if (!fHWScissorSettings.fRect.isInvalid()) {
3314 fHWScissorSettings.fRect.pushToGLScissor(this->glInterface());
3315 }
Adrienne Walker3ed33992018-05-15 11:44:34 -07003316 }
3317
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07003318 this->onFBOChanged();
Adrienne Walker3ed33992018-05-15 11:44:34 -07003319}
3320
Adrienne Walker4ee88512018-05-17 11:37:14 -07003321void GrGLGpu::deleteFramebuffer(GrGLuint fboid) {
3322 if (fboid == fBoundDrawFramebuffer &&
3323 this->caps()->workarounds().unbind_attachments_on_bound_render_fbo_delete) {
3324 // This workaround only applies to deleting currently bound framebuffers
3325 // on Adreno 420. Because this is a somewhat rare case, instead of
3326 // tracking all the attachments of every framebuffer instead just always
3327 // unbind all attachments.
3328 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
3329 GR_GL_RENDERBUFFER, 0));
3330 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT,
3331 GR_GL_RENDERBUFFER, 0));
3332 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_DEPTH_ATTACHMENT,
3333 GR_GL_RENDERBUFFER, 0));
3334 }
3335
3336 GL_CALL(DeleteFramebuffers(1, &fboid));
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07003337
3338 // Deleting the currently bound framebuffer rebinds to 0.
3339 if (fboid == fBoundDrawFramebuffer) {
3340 this->onFBOChanged();
3341 }
Adrienne Walker4ee88512018-05-17 11:37:14 -07003342}
3343
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003344bool GrGLGpu::onCopySurface(GrSurface* dst, GrSurfaceOrigin dstOrigin,
3345 GrSurface* src, GrSurfaceOrigin srcOrigin,
Greg Daniel55fa6472018-03-16 16:13:10 -04003346 const SkIRect& srcRect, const SkIPoint& dstPoint,
3347 bool canDiscardOutsideDstRect) {
bsalomon7f9b2e42016-01-12 13:29:26 -08003348 // None of our copy methods can handle a swizzle. TODO: Make copySurfaceAsDraw handle the
3349 // swizzle.
Brian Salomon1edc5b92016-11-29 13:43:46 -05003350 if (this->caps()->shaderCaps()->configOutputSwizzle(src->config()) !=
3351 this->caps()->shaderCaps()->configOutputSwizzle(dst->config())) {
bsalomon7f9b2e42016-01-12 13:29:26 -08003352 return false;
3353 }
bsalomon083617b2016-02-12 12:10:14 -08003354 // Don't prefer copying as a draw if the dst doesn't already have a FBO object.
Brian Salomon3d86a192018-02-27 16:46:11 -05003355 // This implicitly handles this->glCaps().useDrawInsteadOfAllRenderTargetWrites().
bsalomon083617b2016-02-12 12:10:14 -08003356 bool preferCopy = SkToBool(dst->asRenderTarget());
Greg Daniel26dbe3b2018-05-03 10:35:42 -04003357 if (preferCopy && this->glCaps().canCopyAsDraw(dst->config(), SkToBool(src->asTexture()))) {
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003358 if (this->copySurfaceAsDraw(dst, dstOrigin, src, srcOrigin, srcRect, dstPoint)) {
cdaltone2e71c22016-04-07 18:13:29 -07003359 return true;
3360 }
bsalomon5df6fee2015-05-18 06:26:15 -07003361 }
cblume61214052016-01-26 09:10:48 -08003362
Greg Daniel26dbe3b2018-05-03 10:35:42 -04003363 if (can_copy_texsubimage(dst, dstOrigin, src, srcOrigin, this->glCaps())) {
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003364 this->copySurfaceAsCopyTexSubImage(dst, dstOrigin, src, srcOrigin, srcRect, dstPoint);
bsalomon6df86402015-06-01 10:41:49 -07003365 return true;
3366 }
3367
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003368 if (can_blit_framebuffer_for_copy_surface(dst, dstOrigin, src, srcOrigin,
Greg Daniel26dbe3b2018-05-03 10:35:42 -04003369 srcRect, dstPoint, this->glCaps())) {
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003370 return this->copySurfaceAsBlitFramebuffer(dst, dstOrigin, src, srcOrigin,
3371 srcRect, dstPoint);
bsalomon6df86402015-06-01 10:41:49 -07003372 }
3373
Greg Daniel26dbe3b2018-05-03 10:35:42 -04003374 if (!preferCopy && this->glCaps().canCopyAsDraw(dst->config(), SkToBool(src->asTexture()))) {
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003375 if (this->copySurfaceAsDraw(dst, dstOrigin, src, srcOrigin, srcRect, dstPoint)) {
cdaltone2e71c22016-04-07 18:13:29 -07003376 return true;
3377 }
bsalomon083617b2016-02-12 12:10:14 -08003378 }
3379
bsalomon6df86402015-06-01 10:41:49 -07003380 return false;
3381}
3382
Brian Salomonbf7b6202016-11-11 16:08:03 -05003383bool GrGLGpu::createCopyProgram(GrTexture* srcTex) {
Brian Osman39c08ac2017-07-26 09:36:09 -04003384 TRACE_EVENT0("skia", TRACE_FUNC);
Ryan Macnak38a10ad2017-07-10 10:36:34 -07003385
Brian Salomonbf7b6202016-11-11 16:08:03 -05003386 int progIdx = TextureToCopyProgramIdx(srcTex);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003387 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
Brian Salomon60dd8c72018-07-30 10:24:13 -04003388 GrSLType samplerType =
3389 GrSLCombinedSamplerTypeForTextureType(srcTex->texturePriv().textureType());
cdaltone2e71c22016-04-07 18:13:29 -07003390
3391 if (!fCopyProgramArrayBuffer) {
3392 static const GrGLfloat vdata[] = {
3393 0, 0,
3394 0, 1,
3395 1, 0,
3396 1, 1
3397 };
Brian Salomonae64c192019-02-05 09:41:37 -05003398 fCopyProgramArrayBuffer = GrGLBuffer::Make(this, sizeof(vdata), GrGpuBufferType::kVertex,
Brian Salomon12d22642019-01-29 14:38:50 -05003399 kStatic_GrAccessPattern, vdata);
cdaltone2e71c22016-04-07 18:13:29 -07003400 }
3401 if (!fCopyProgramArrayBuffer) {
3402 return false;
3403 }
3404
3405 SkASSERT(!fCopyPrograms[progIdx].fProgram);
3406 GL_CALL_RET(fCopyPrograms[progIdx].fProgram, CreateProgram());
3407 if (!fCopyPrograms[progIdx].fProgram) {
3408 return false;
3409 }
3410
Brian Salomon1edc5b92016-11-29 13:43:46 -05003411 const char* version = shaderCaps->versionDeclString();
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003412 GrShaderVar aVertex("a_vertex", kHalf2_GrSLType, GrShaderVar::kIn_TypeModifier);
3413 GrShaderVar uTexCoordXform("u_texCoordXform", kHalf4_GrSLType,
Brian Salomon99938a82016-11-21 13:41:08 -05003414 GrShaderVar::kUniform_TypeModifier);
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003415 GrShaderVar uPosXform("u_posXform", kHalf4_GrSLType, GrShaderVar::kUniform_TypeModifier);
Brian Salomon99938a82016-11-21 13:41:08 -05003416 GrShaderVar uTexture("u_texture", samplerType, GrShaderVar::kUniform_TypeModifier);
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003417 GrShaderVar vTexCoord("v_texCoord", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier);
3418 GrShaderVar oFragColor("o_FragColor", kHalf4_GrSLType, GrShaderVar::kOut_TypeModifier);
cdaltone2e71c22016-04-07 18:13:29 -07003419
3420 SkString vshaderTxt(version);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003421 if (shaderCaps->noperspectiveInterpolationSupport()) {
3422 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
cdaltone2e71c22016-04-07 18:13:29 -07003423 vshaderTxt.appendf("#extension %s : require\n", extension);
3424 }
3425 vTexCoord.addModifier("noperspective");
3426 }
3427
Brian Salomon1edc5b92016-11-29 13:43:46 -05003428 aVertex.appendDecl(shaderCaps, &vshaderTxt);
cdaltone2e71c22016-04-07 18:13:29 -07003429 vshaderTxt.append(";");
Brian Salomon1edc5b92016-11-29 13:43:46 -05003430 uTexCoordXform.appendDecl(shaderCaps, &vshaderTxt);
cdaltone2e71c22016-04-07 18:13:29 -07003431 vshaderTxt.append(";");
Brian Salomon1edc5b92016-11-29 13:43:46 -05003432 uPosXform.appendDecl(shaderCaps, &vshaderTxt);
cdaltone2e71c22016-04-07 18:13:29 -07003433 vshaderTxt.append(";");
Brian Salomon1edc5b92016-11-29 13:43:46 -05003434 vTexCoord.appendDecl(shaderCaps, &vshaderTxt);
cdaltone2e71c22016-04-07 18:13:29 -07003435 vshaderTxt.append(";");
3436
3437 vshaderTxt.append(
3438 "// Copy Program VS\n"
3439 "void main() {"
Ethan Nicholase1f55022019-02-05 17:17:40 -05003440 " v_texCoord = half2(a_vertex.xy * u_texCoordXform.xy + u_texCoordXform.zw);"
Ethan Nicholasbed683a2017-09-26 14:23:59 -04003441 " sk_Position.xy = a_vertex * u_posXform.xy + u_posXform.zw;"
3442 " sk_Position.zw = half2(0, 1);"
cdaltone2e71c22016-04-07 18:13:29 -07003443 "}"
3444 );
3445
3446 SkString fshaderTxt(version);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003447 if (shaderCaps->noperspectiveInterpolationSupport()) {
3448 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
cdaltone2e71c22016-04-07 18:13:29 -07003449 fshaderTxt.appendf("#extension %s : require\n", extension);
3450 }
3451 }
Brian Salomonf31ae492016-11-18 15:35:33 -05003452 vTexCoord.setTypeModifier(GrShaderVar::kIn_TypeModifier);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003453 vTexCoord.appendDecl(shaderCaps, &fshaderTxt);
cdaltone2e71c22016-04-07 18:13:29 -07003454 fshaderTxt.append(";");
Brian Salomon1edc5b92016-11-29 13:43:46 -05003455 uTexture.appendDecl(shaderCaps, &fshaderTxt);
cdaltone2e71c22016-04-07 18:13:29 -07003456 fshaderTxt.append(";");
cdaltone2e71c22016-04-07 18:13:29 -07003457 fshaderTxt.appendf(
3458 "// Copy Program FS\n"
3459 "void main() {"
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003460 " sk_FragColor = texture(u_texture, v_texCoord);"
3461 "}"
cdaltone2e71c22016-04-07 18:13:29 -07003462 );
3463
3464 const char* str;
3465 GrGLint length;
3466
3467 str = vshaderTxt.c_str();
3468 length = SkToInt(vshaderTxt.size());
Ethan Nicholas941e7e22016-12-12 15:33:30 -05003469 SkSL::Program::Settings settings;
3470 settings.fCaps = shaderCaps;
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003471 SkSL::String glsl;
3472 std::unique_ptr<SkSL::Program> program = GrSkSLtoGLSL(*fGLContext, GR_GL_VERTEX_SHADER,
3473 &str, &length, 1, settings, &glsl);
cdaltone2e71c22016-04-07 18:13:29 -07003474 GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fCopyPrograms[progIdx].fProgram,
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003475 GR_GL_VERTEX_SHADER, glsl.c_str(), glsl.size(),
3476 &fStats, settings);
3477 SkASSERT(program->fInputs.isEmpty());
cdaltone2e71c22016-04-07 18:13:29 -07003478
3479 str = fshaderTxt.c_str();
3480 length = SkToInt(fshaderTxt.size());
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003481 program = GrSkSLtoGLSL(*fGLContext, GR_GL_FRAGMENT_SHADER, &str, &length, 1, settings, &glsl);
cdaltone2e71c22016-04-07 18:13:29 -07003482 GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fCopyPrograms[progIdx].fProgram,
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003483 GR_GL_FRAGMENT_SHADER, glsl.c_str(), glsl.size(),
3484 &fStats, settings);
3485 SkASSERT(program->fInputs.isEmpty());
cdaltone2e71c22016-04-07 18:13:29 -07003486
3487 GL_CALL(LinkProgram(fCopyPrograms[progIdx].fProgram));
3488
3489 GL_CALL_RET(fCopyPrograms[progIdx].fTextureUniform,
3490 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_texture"));
3491 GL_CALL_RET(fCopyPrograms[progIdx].fPosXformUniform,
3492 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_posXform"));
3493 GL_CALL_RET(fCopyPrograms[progIdx].fTexCoordXformUniform,
3494 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_texCoordXform"));
3495
3496 GL_CALL(BindAttribLocation(fCopyPrograms[progIdx].fProgram, 0, "a_vertex"));
3497
3498 GL_CALL(DeleteShader(vshader));
3499 GL_CALL(DeleteShader(fshader));
3500
3501 return true;
bsalomon6df86402015-06-01 10:41:49 -07003502}
3503
brianosman33f6b3f2016-06-02 05:49:21 -07003504bool GrGLGpu::createMipmapProgram(int progIdx) {
3505 const bool oddWidth = SkToBool(progIdx & 0x2);
3506 const bool oddHeight = SkToBool(progIdx & 0x1);
3507 const int numTaps = (oddWidth ? 2 : 1) * (oddHeight ? 2 : 1);
3508
Brian Salomon1edc5b92016-11-29 13:43:46 -05003509 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
brianosman33f6b3f2016-06-02 05:49:21 -07003510
3511 SkASSERT(!fMipmapPrograms[progIdx].fProgram);
3512 GL_CALL_RET(fMipmapPrograms[progIdx].fProgram, CreateProgram());
3513 if (!fMipmapPrograms[progIdx].fProgram) {
3514 return false;
3515 }
3516
Brian Salomon1edc5b92016-11-29 13:43:46 -05003517 const char* version = shaderCaps->versionDeclString();
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003518 GrShaderVar aVertex("a_vertex", kHalf2_GrSLType, GrShaderVar::kIn_TypeModifier);
3519 GrShaderVar uTexCoordXform("u_texCoordXform", kHalf4_GrSLType,
Brian Salomon99938a82016-11-21 13:41:08 -05003520 GrShaderVar::kUniform_TypeModifier);
3521 GrShaderVar uTexture("u_texture", kTexture2DSampler_GrSLType,
3522 GrShaderVar::kUniform_TypeModifier);
brianosman33f6b3f2016-06-02 05:49:21 -07003523 // We need 1, 2, or 4 texture coordinates (depending on parity of each dimension):
Brian Salomon99938a82016-11-21 13:41:08 -05003524 GrShaderVar vTexCoords[] = {
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003525 GrShaderVar("v_texCoord0", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier),
3526 GrShaderVar("v_texCoord1", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier),
3527 GrShaderVar("v_texCoord2", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier),
3528 GrShaderVar("v_texCoord3", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier),
brianosman33f6b3f2016-06-02 05:49:21 -07003529 };
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003530 GrShaderVar oFragColor("o_FragColor", kHalf4_GrSLType,GrShaderVar::kOut_TypeModifier);
brianosman33f6b3f2016-06-02 05:49:21 -07003531
3532 SkString vshaderTxt(version);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003533 if (shaderCaps->noperspectiveInterpolationSupport()) {
3534 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
brianosman33f6b3f2016-06-02 05:49:21 -07003535 vshaderTxt.appendf("#extension %s : require\n", extension);
3536 }
3537 vTexCoords[0].addModifier("noperspective");
3538 vTexCoords[1].addModifier("noperspective");
3539 vTexCoords[2].addModifier("noperspective");
3540 vTexCoords[3].addModifier("noperspective");
3541 }
3542
Brian Salomon1edc5b92016-11-29 13:43:46 -05003543 aVertex.appendDecl(shaderCaps, &vshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003544 vshaderTxt.append(";");
Brian Salomon1edc5b92016-11-29 13:43:46 -05003545 uTexCoordXform.appendDecl(shaderCaps, &vshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003546 vshaderTxt.append(";");
3547 for (int i = 0; i < numTaps; ++i) {
Brian Salomon1edc5b92016-11-29 13:43:46 -05003548 vTexCoords[i].appendDecl(shaderCaps, &vshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003549 vshaderTxt.append(";");
3550 }
3551
3552 vshaderTxt.append(
3553 "// Mipmap Program VS\n"
3554 "void main() {"
Ethan Nicholasbed683a2017-09-26 14:23:59 -04003555 " sk_Position.xy = a_vertex * half2(2, 2) - half2(1, 1);"
3556 " sk_Position.zw = half2(0, 1);"
brianosman33f6b3f2016-06-02 05:49:21 -07003557 );
3558
3559 // Insert texture coordinate computation:
3560 if (oddWidth && oddHeight) {
3561 vshaderTxt.append(
3562 " v_texCoord0 = a_vertex.xy * u_texCoordXform.yw;"
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003563 " v_texCoord1 = a_vertex.xy * u_texCoordXform.yw + half2(u_texCoordXform.x, 0);"
3564 " v_texCoord2 = a_vertex.xy * u_texCoordXform.yw + half2(0, u_texCoordXform.z);"
brianosman33f6b3f2016-06-02 05:49:21 -07003565 " v_texCoord3 = a_vertex.xy * u_texCoordXform.yw + u_texCoordXform.xz;"
3566 );
3567 } else if (oddWidth) {
3568 vshaderTxt.append(
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003569 " v_texCoord0 = a_vertex.xy * half2(u_texCoordXform.y, 1);"
3570 " v_texCoord1 = a_vertex.xy * half2(u_texCoordXform.y, 1) + half2(u_texCoordXform.x, 0);"
brianosman33f6b3f2016-06-02 05:49:21 -07003571 );
3572 } else if (oddHeight) {
3573 vshaderTxt.append(
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003574 " v_texCoord0 = a_vertex.xy * half2(1, u_texCoordXform.w);"
3575 " v_texCoord1 = a_vertex.xy * half2(1, u_texCoordXform.w) + half2(0, u_texCoordXform.z);"
brianosman33f6b3f2016-06-02 05:49:21 -07003576 );
3577 } else {
3578 vshaderTxt.append(
3579 " v_texCoord0 = a_vertex.xy;"
3580 );
3581 }
3582
3583 vshaderTxt.append("}");
3584
3585 SkString fshaderTxt(version);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003586 if (shaderCaps->noperspectiveInterpolationSupport()) {
3587 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
brianosman33f6b3f2016-06-02 05:49:21 -07003588 fshaderTxt.appendf("#extension %s : require\n", extension);
3589 }
3590 }
brianosman33f6b3f2016-06-02 05:49:21 -07003591 for (int i = 0; i < numTaps; ++i) {
Brian Salomonf31ae492016-11-18 15:35:33 -05003592 vTexCoords[i].setTypeModifier(GrShaderVar::kIn_TypeModifier);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003593 vTexCoords[i].appendDecl(shaderCaps, &fshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003594 fshaderTxt.append(";");
3595 }
Brian Salomon1edc5b92016-11-29 13:43:46 -05003596 uTexture.appendDecl(shaderCaps, &fshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003597 fshaderTxt.append(";");
brianosman33f6b3f2016-06-02 05:49:21 -07003598 fshaderTxt.append(
3599 "// Mipmap Program FS\n"
3600 "void main() {"
3601 );
3602
3603 if (oddWidth && oddHeight) {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003604 fshaderTxt.append(
3605 " sk_FragColor = (texture(u_texture, v_texCoord0) + "
3606 " texture(u_texture, v_texCoord1) + "
3607 " texture(u_texture, v_texCoord2) + "
3608 " texture(u_texture, v_texCoord3)) * 0.25;"
brianosman33f6b3f2016-06-02 05:49:21 -07003609 );
3610 } else if (oddWidth || oddHeight) {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003611 fshaderTxt.append(
3612 " sk_FragColor = (texture(u_texture, v_texCoord0) + "
3613 " texture(u_texture, v_texCoord1)) * 0.5;"
brianosman33f6b3f2016-06-02 05:49:21 -07003614 );
3615 } else {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003616 fshaderTxt.append(
3617 " sk_FragColor = texture(u_texture, v_texCoord0);"
brianosman33f6b3f2016-06-02 05:49:21 -07003618 );
3619 }
3620
3621 fshaderTxt.append("}");
3622
3623 const char* str;
3624 GrGLint length;
3625
3626 str = vshaderTxt.c_str();
3627 length = SkToInt(vshaderTxt.size());
Ethan Nicholas941e7e22016-12-12 15:33:30 -05003628 SkSL::Program::Settings settings;
3629 settings.fCaps = shaderCaps;
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003630 SkSL::String glsl;
3631 std::unique_ptr<SkSL::Program> program = GrSkSLtoGLSL(*fGLContext, GR_GL_VERTEX_SHADER,
3632 &str, &length, 1, settings, &glsl);
brianosman33f6b3f2016-06-02 05:49:21 -07003633 GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fMipmapPrograms[progIdx].fProgram,
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003634 GR_GL_VERTEX_SHADER, glsl.c_str(), glsl.size(),
3635 &fStats, settings);
3636 SkASSERT(program->fInputs.isEmpty());
brianosman33f6b3f2016-06-02 05:49:21 -07003637
3638 str = fshaderTxt.c_str();
3639 length = SkToInt(fshaderTxt.size());
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003640 program = GrSkSLtoGLSL(*fGLContext, GR_GL_FRAGMENT_SHADER, &str, &length, 1, settings, &glsl);
brianosman33f6b3f2016-06-02 05:49:21 -07003641 GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fMipmapPrograms[progIdx].fProgram,
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003642 GR_GL_FRAGMENT_SHADER, glsl.c_str(), glsl.size(),
3643 &fStats, settings);
3644 SkASSERT(program->fInputs.isEmpty());
brianosman33f6b3f2016-06-02 05:49:21 -07003645
3646 GL_CALL(LinkProgram(fMipmapPrograms[progIdx].fProgram));
3647
3648 GL_CALL_RET(fMipmapPrograms[progIdx].fTextureUniform,
3649 GetUniformLocation(fMipmapPrograms[progIdx].fProgram, "u_texture"));
3650 GL_CALL_RET(fMipmapPrograms[progIdx].fTexCoordXformUniform,
3651 GetUniformLocation(fMipmapPrograms[progIdx].fProgram, "u_texCoordXform"));
3652
3653 GL_CALL(BindAttribLocation(fMipmapPrograms[progIdx].fProgram, 0, "a_vertex"));
3654
3655 GL_CALL(DeleteShader(vshader));
3656 GL_CALL(DeleteShader(fshader));
3657
3658 return true;
3659}
3660
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003661bool GrGLGpu::copySurfaceAsDraw(GrSurface* dst, GrSurfaceOrigin dstOrigin,
3662 GrSurface* src, GrSurfaceOrigin srcOrigin,
bsalomon6df86402015-06-01 10:41:49 -07003663 const SkIRect& srcRect,
3664 const SkIPoint& dstPoint) {
cdaltone2e71c22016-04-07 18:13:29 -07003665 GrGLTexture* srcTex = static_cast<GrGLTexture*>(src->asTexture());
Brian Salomonbf7b6202016-11-11 16:08:03 -05003666 int progIdx = TextureToCopyProgramIdx(srcTex);
cdaltone2e71c22016-04-07 18:13:29 -07003667
Greg Daniel26dbe3b2018-05-03 10:35:42 -04003668 if (!this->glCaps().canConfigBeFBOColorAttachment(dst->config())) {
3669 return false;
3670 }
3671
cdaltone2e71c22016-04-07 18:13:29 -07003672 if (!fCopyPrograms[progIdx].fProgram) {
Brian Salomonbf7b6202016-11-11 16:08:03 -05003673 if (!this->createCopyProgram(srcTex)) {
cdaltone2e71c22016-04-07 18:13:29 -07003674 SkDebugf("Failed to create copy program.\n");
3675 return false;
3676 }
3677 }
3678
bsalomon6df86402015-06-01 10:41:49 -07003679 int w = srcRect.width();
3680 int h = srcRect.height();
3681
Brian Salomon930f9392018-06-20 16:25:26 -04003682 this->bindTexture(0, GrSamplerState::ClampNearest(), srcTex);
bsalomon6df86402015-06-01 10:41:49 -07003683
bsalomon083617b2016-02-12 12:10:14 -08003684 GrGLIRect dstVP;
Brian Salomon71d9d842016-11-03 13:42:00 -04003685 this->bindSurfaceFBOForPixelOps(dst, GR_GL_FRAMEBUFFER, &dstVP, kDst_TempFBOTarget);
bsalomon083617b2016-02-12 12:10:14 -08003686 this->flushViewport(dstVP);
Robert Phillips294870f2016-11-11 12:38:40 -05003687 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon083617b2016-02-12 12:10:14 -08003688
bsalomon6df86402015-06-01 10:41:49 -07003689 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY, w, h);
bsalomon6df86402015-06-01 10:41:49 -07003690
Brian Salomon802cb312018-06-08 18:05:20 -04003691 this->flushProgram(fCopyPrograms[progIdx].fProgram);
bsalomon6df86402015-06-01 10:41:49 -07003692
cdaltone2e71c22016-04-07 18:13:29 -07003693 fHWVertexArrayState.setVertexArrayID(this, 0);
bsalomon6df86402015-06-01 10:41:49 -07003694
cdaltone2e71c22016-04-07 18:13:29 -07003695 GrGLAttribArrayState* attribs = fHWVertexArrayState.bindInternalVertexArray(this);
Chris Dalton8e45b4f2017-05-05 14:00:56 -04003696 attribs->enableVertexArrays(this, 1);
Brian Osmand4c29702018-09-14 16:16:55 -04003697 attribs->set(this, 0, fCopyProgramArrayBuffer.get(), kFloat2_GrVertexAttribType,
Brian Osman4a3f5c82018-09-18 16:16:38 -04003698 kFloat2_GrSLType, 2 * sizeof(GrGLfloat), 0);
bsalomon6df86402015-06-01 10:41:49 -07003699
3700 // dst rect edges in NDC (-1 to 1)
3701 int dw = dst->width();
3702 int dh = dst->height();
3703 GrGLfloat dx0 = 2.f * dstPoint.fX / dw - 1.f;
3704 GrGLfloat dx1 = 2.f * (dstPoint.fX + w) / dw - 1.f;
3705 GrGLfloat dy0 = 2.f * dstPoint.fY / dh - 1.f;
3706 GrGLfloat dy1 = 2.f * (dstPoint.fY + h) / dh - 1.f;
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003707 if (kBottomLeft_GrSurfaceOrigin == dstOrigin) {
bsalomon6df86402015-06-01 10:41:49 -07003708 dy0 = -dy0;
3709 dy1 = -dy1;
3710 }
3711
bsalomone5286e02016-01-14 09:24:09 -08003712 GrGLfloat sx0 = (GrGLfloat)srcRect.fLeft;
3713 GrGLfloat sx1 = (GrGLfloat)(srcRect.fLeft + w);
3714 GrGLfloat sy0 = (GrGLfloat)srcRect.fTop;
3715 GrGLfloat sy1 = (GrGLfloat)(srcRect.fTop + h);
Ethan Nicholas5338f992017-04-19 15:54:07 -04003716 int sw = src->width();
bsalomon6df86402015-06-01 10:41:49 -07003717 int sh = src->height();
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003718 if (kBottomLeft_GrSurfaceOrigin == srcOrigin) {
bsalomone5286e02016-01-14 09:24:09 -08003719 sy0 = sh - sy0;
3720 sy1 = sh - sy1;
3721 }
Brian Salomon246bc3d2018-12-06 15:33:02 -05003722 if (srcTex->texturePriv().textureType() != GrTextureType::kRectangle) {
3723 // src rect edges in normalized texture space (0 to 1)
3724 sx0 /= sw;
3725 sx1 /= sw;
3726 sy0 /= sh;
3727 sy1 /= sh;
3728 }
bsalomon6df86402015-06-01 10:41:49 -07003729
bsalomon7ea33f52015-11-22 14:51:00 -08003730 GL_CALL(Uniform4f(fCopyPrograms[progIdx].fPosXformUniform, dx1 - dx0, dy1 - dy0, dx0, dy0));
3731 GL_CALL(Uniform4f(fCopyPrograms[progIdx].fTexCoordXformUniform,
3732 sx1 - sx0, sy1 - sy0, sx0, sy0));
3733 GL_CALL(Uniform1i(fCopyPrograms[progIdx].fTextureUniform, 0));
bsalomon6df86402015-06-01 10:41:49 -07003734
3735 GrXferProcessor::BlendInfo blendInfo;
3736 blendInfo.reset();
bsalomon7f9b2e42016-01-12 13:29:26 -08003737 this->flushBlend(blendInfo, GrSwizzle::RGBA());
bsalomon6df86402015-06-01 10:41:49 -07003738 this->flushColorWrite(true);
Chris Dalton4c56b032019-03-04 12:28:42 -07003739 this->flushHWAAState(nullptr, false);
Brian Salomond818ebf2018-07-02 14:08:49 +00003740 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07003741 this->disableWindowRectangles();
csmartdaltonc7d85332016-10-26 10:13:46 -07003742 this->disableStencil();
Brian Osmanf02fa6f2017-07-11 11:17:47 -04003743 if (this->glCaps().srgbWriteControl()) {
3744 this->flushFramebufferSRGB(true);
3745 }
bsalomon6df86402015-06-01 10:41:49 -07003746
3747 GL_CALL(DrawArrays(GR_GL_TRIANGLE_STRIP, 0, 4));
Brian Salomon71d9d842016-11-03 13:42:00 -04003748 this->unbindTextureFBOForPixelOps(GR_GL_FRAMEBUFFER, dst);
Brian Salomon1fabd512018-02-09 09:54:25 -05003749 this->didWriteToSurface(dst, dstOrigin, &dstRect);
bsalomon083617b2016-02-12 12:10:14 -08003750
cdaltone2e71c22016-04-07 18:13:29 -07003751 return true;
bsalomon6df86402015-06-01 10:41:49 -07003752}
3753
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003754void GrGLGpu::copySurfaceAsCopyTexSubImage(GrSurface* dst, GrSurfaceOrigin dstOrigin,
3755 GrSurface* src, GrSurfaceOrigin srcOrigin,
bsalomon6df86402015-06-01 10:41:49 -07003756 const SkIRect& srcRect,
3757 const SkIPoint& dstPoint) {
Greg Daniel26dbe3b2018-05-03 10:35:42 -04003758 SkASSERT(can_copy_texsubimage(dst, dstOrigin, src, srcOrigin, this->glCaps()));
bsalomon6df86402015-06-01 10:41:49 -07003759 GrGLIRect srcVP;
Brian Salomon71d9d842016-11-03 13:42:00 -04003760 this->bindSurfaceFBOForPixelOps(src, GR_GL_FRAMEBUFFER, &srcVP, kSrc_TempFBOTarget);
bsalomon083617b2016-02-12 12:10:14 -08003761 GrGLTexture* dstTex = static_cast<GrGLTexture *>(dst->asTexture());
bsalomon6df86402015-06-01 10:41:49 -07003762 SkASSERT(dstTex);
3763 // We modified the bound FBO
Robert Phillips294870f2016-11-11 12:38:40 -05003764 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon6df86402015-06-01 10:41:49 -07003765 GrGLIRect srcGLRect;
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003766 srcGLRect.setRelativeTo(srcVP, srcRect, srcOrigin);
bsalomon6df86402015-06-01 10:41:49 -07003767
Brian Salomond978b902019-02-07 15:09:18 -05003768 this->bindTextureToScratchUnit(dstTex->target(), dstTex->textureID());
bsalomon6df86402015-06-01 10:41:49 -07003769 GrGLint dstY;
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003770 if (kBottomLeft_GrSurfaceOrigin == dstOrigin) {
bsalomon6df86402015-06-01 10:41:49 -07003771 dstY = dst->height() - (dstPoint.fY + srcGLRect.fHeight);
3772 } else {
3773 dstY = dstPoint.fY;
3774 }
bsalomon10528f12015-10-14 12:54:52 -07003775 GL_CALL(CopyTexSubImage2D(dstTex->target(), 0,
bsalomon083617b2016-02-12 12:10:14 -08003776 dstPoint.fX, dstY,
3777 srcGLRect.fLeft, srcGLRect.fBottom,
3778 srcGLRect.fWidth, srcGLRect.fHeight));
Brian Salomon71d9d842016-11-03 13:42:00 -04003779 this->unbindTextureFBOForPixelOps(GR_GL_FRAMEBUFFER, src);
bsalomon083617b2016-02-12 12:10:14 -08003780 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
3781 srcRect.width(), srcRect.height());
Brian Salomon1fabd512018-02-09 09:54:25 -05003782 this->didWriteToSurface(dst, dstOrigin, &dstRect);
bsalomon6df86402015-06-01 10:41:49 -07003783}
3784
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003785bool GrGLGpu::copySurfaceAsBlitFramebuffer(GrSurface* dst, GrSurfaceOrigin dstOrigin,
3786 GrSurface* src, GrSurfaceOrigin srcOrigin,
bsalomon6df86402015-06-01 10:41:49 -07003787 const SkIRect& srcRect,
3788 const SkIPoint& dstPoint) {
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003789 SkASSERT(can_blit_framebuffer_for_copy_surface(dst, dstOrigin, src, srcOrigin,
Greg Daniel26dbe3b2018-05-03 10:35:42 -04003790 srcRect, dstPoint, this->glCaps()));
bsalomon6df86402015-06-01 10:41:49 -07003791 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
3792 srcRect.width(), srcRect.height());
3793 if (dst == src) {
3794 if (SkIRect::IntersectsNoEmptyCheck(dstRect, srcRect)) {
3795 return false;
mtklein404b3b22015-05-18 09:29:10 -07003796 }
bsalomon5df6fee2015-05-18 06:26:15 -07003797 }
bsalomon6df86402015-06-01 10:41:49 -07003798
bsalomon6df86402015-06-01 10:41:49 -07003799 GrGLIRect dstVP;
3800 GrGLIRect srcVP;
Brian Salomon71d9d842016-11-03 13:42:00 -04003801 this->bindSurfaceFBOForPixelOps(dst, GR_GL_DRAW_FRAMEBUFFER, &dstVP, kDst_TempFBOTarget);
3802 this->bindSurfaceFBOForPixelOps(src, GR_GL_READ_FRAMEBUFFER, &srcVP, kSrc_TempFBOTarget);
bsalomon6df86402015-06-01 10:41:49 -07003803 // We modified the bound FBO
Robert Phillips294870f2016-11-11 12:38:40 -05003804 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon6df86402015-06-01 10:41:49 -07003805 GrGLIRect srcGLRect;
3806 GrGLIRect dstGLRect;
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003807 srcGLRect.setRelativeTo(srcVP, srcRect, srcOrigin);
3808 dstGLRect.setRelativeTo(dstVP, dstRect, dstOrigin);
bsalomon6df86402015-06-01 10:41:49 -07003809
3810 // BlitFrameBuffer respects the scissor, so disable it.
Brian Salomond818ebf2018-07-02 14:08:49 +00003811 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07003812 this->disableWindowRectangles();
bsalomon6df86402015-06-01 10:41:49 -07003813
3814 GrGLint srcY0;
3815 GrGLint srcY1;
3816 // Does the blit need to y-mirror or not?
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003817 if (srcOrigin == dstOrigin) {
bsalomon6df86402015-06-01 10:41:49 -07003818 srcY0 = srcGLRect.fBottom;
3819 srcY1 = srcGLRect.fBottom + srcGLRect.fHeight;
3820 } else {
3821 srcY0 = srcGLRect.fBottom + srcGLRect.fHeight;
3822 srcY1 = srcGLRect.fBottom;
3823 }
3824 GL_CALL(BlitFramebuffer(srcGLRect.fLeft,
3825 srcY0,
3826 srcGLRect.fLeft + srcGLRect.fWidth,
3827 srcY1,
3828 dstGLRect.fLeft,
3829 dstGLRect.fBottom,
3830 dstGLRect.fLeft + dstGLRect.fWidth,
3831 dstGLRect.fBottom + dstGLRect.fHeight,
3832 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
Brian Salomon71d9d842016-11-03 13:42:00 -04003833 this->unbindTextureFBOForPixelOps(GR_GL_DRAW_FRAMEBUFFER, dst);
3834 this->unbindTextureFBOForPixelOps(GR_GL_READ_FRAMEBUFFER, src);
Brian Salomon1fabd512018-02-09 09:54:25 -05003835 this->didWriteToSurface(dst, dstOrigin, &dstRect);
bsalomon6df86402015-06-01 10:41:49 -07003836 return true;
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00003837}
3838
Brian Salomon930f9392018-06-20 16:25:26 -04003839bool GrGLGpu::onRegenerateMipMapLevels(GrTexture* texture) {
3840 auto glTex = static_cast<GrGLTexture*>(texture);
brianosman33f6b3f2016-06-02 05:49:21 -07003841 // Mipmaps are only supported on 2D textures:
Brian Salomon930f9392018-06-20 16:25:26 -04003842 if (GR_GL_TEXTURE_2D != glTex->target()) {
brianosman33f6b3f2016-06-02 05:49:21 -07003843 return false;
3844 }
3845
Brian Salomon930f9392018-06-20 16:25:26 -04003846 // Manual implementation of mipmap generation, to work around driver bugs w/sRGB.
3847 // Uses draw calls to do a series of downsample operations to successive mips.
3848
3849 // The manual approach requires the ability to limit which level we're sampling and that the
3850 // destination can be bound to a FBO:
3851 if (!this->glCaps().doManualMipmapping() ||
3852 !this->glCaps().canConfigBeFBOColorAttachment(texture->config())) {
3853 GrGLenum target = glTex->target();
Brian Salomond978b902019-02-07 15:09:18 -05003854 this->bindTextureToScratchUnit(target, glTex->textureID());
Brian Salomon930f9392018-06-20 16:25:26 -04003855 GL_CALL(GenerateMipmap(glTex->target()));
3856 return true;
brianosman33f6b3f2016-06-02 05:49:21 -07003857 }
3858
brianosman33f6b3f2016-06-02 05:49:21 -07003859 int width = texture->width();
3860 int height = texture->height();
3861 int levelCount = SkMipMap::ComputeLevelCount(width, height) + 1;
Greg Danielda86e282018-06-13 09:41:19 -04003862 SkASSERT(levelCount == texture->texturePriv().maxMipMapLevel() + 1);
brianosman33f6b3f2016-06-02 05:49:21 -07003863
3864 // Create (if necessary), then bind temporary FBO:
3865 if (0 == fTempDstFBOID) {
3866 GL_CALL(GenFramebuffers(1, &fTempDstFBOID));
3867 }
Adrienne Walker4ee88512018-05-17 11:37:14 -07003868 this->bindFramebuffer(GR_GL_FRAMEBUFFER, fTempDstFBOID);
Robert Phillips294870f2016-11-11 12:38:40 -05003869 fHWBoundRenderTargetUniqueID.makeInvalid();
brianosman33f6b3f2016-06-02 05:49:21 -07003870
3871 // Bind the texture, to get things configured for filtering.
3872 // We'll be changing our base level further below:
3873 this->setTextureUnit(0);
Brian Salomon930f9392018-06-20 16:25:26 -04003874 this->bindTexture(0, GrSamplerState::ClampBilerp(), glTex);
brianosman33f6b3f2016-06-02 05:49:21 -07003875
3876 // Vertex data:
3877 if (!fMipmapProgramArrayBuffer) {
3878 static const GrGLfloat vdata[] = {
3879 0, 0,
3880 0, 1,
3881 1, 0,
3882 1, 1
3883 };
Brian Salomonae64c192019-02-05 09:41:37 -05003884 fMipmapProgramArrayBuffer = GrGLBuffer::Make(this, sizeof(vdata), GrGpuBufferType::kVertex,
Brian Salomon12d22642019-01-29 14:38:50 -05003885 kStatic_GrAccessPattern, vdata);
brianosman33f6b3f2016-06-02 05:49:21 -07003886 }
3887 if (!fMipmapProgramArrayBuffer) {
3888 return false;
3889 }
3890
3891 fHWVertexArrayState.setVertexArrayID(this, 0);
3892
3893 GrGLAttribArrayState* attribs = fHWVertexArrayState.bindInternalVertexArray(this);
Chris Dalton8e45b4f2017-05-05 14:00:56 -04003894 attribs->enableVertexArrays(this, 1);
Brian Osmand4c29702018-09-14 16:16:55 -04003895 attribs->set(this, 0, fMipmapProgramArrayBuffer.get(), kFloat2_GrVertexAttribType,
Brian Osman4a3f5c82018-09-18 16:16:38 -04003896 kFloat2_GrSLType, 2 * sizeof(GrGLfloat), 0);
brianosman33f6b3f2016-06-02 05:49:21 -07003897
3898 // Set "simple" state once:
3899 GrXferProcessor::BlendInfo blendInfo;
3900 blendInfo.reset();
3901 this->flushBlend(blendInfo, GrSwizzle::RGBA());
3902 this->flushColorWrite(true);
Chris Dalton4c56b032019-03-04 12:28:42 -07003903 this->flushHWAAState(nullptr, false);
Brian Salomond818ebf2018-07-02 14:08:49 +00003904 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07003905 this->disableWindowRectangles();
csmartdaltonc7d85332016-10-26 10:13:46 -07003906 this->disableStencil();
brianosman33f6b3f2016-06-02 05:49:21 -07003907
3908 // Do all the blits:
3909 width = texture->width();
3910 height = texture->height();
3911 GrGLIRect viewport;
3912 viewport.fLeft = 0;
3913 viewport.fBottom = 0;
Brian Salomondc829942018-10-23 16:07:24 -04003914
brianosman33f6b3f2016-06-02 05:49:21 -07003915 for (GrGLint level = 1; level < levelCount; ++level) {
3916 // Get and bind the program for this particular downsample (filter shape can vary):
3917 int progIdx = TextureSizeToMipmapProgramIdx(width, height);
3918 if (!fMipmapPrograms[progIdx].fProgram) {
3919 if (!this->createMipmapProgram(progIdx)) {
3920 SkDebugf("Failed to create mipmap program.\n");
Brian Salomondc829942018-10-23 16:07:24 -04003921 // Invalidate all params to cover base level change in a previous iteration.
3922 glTex->textureParamsModified();
brianosman33f6b3f2016-06-02 05:49:21 -07003923 return false;
3924 }
3925 }
Brian Salomon802cb312018-06-08 18:05:20 -04003926 this->flushProgram(fMipmapPrograms[progIdx].fProgram);
brianosman33f6b3f2016-06-02 05:49:21 -07003927
3928 // Texcoord uniform is expected to contain (1/w, (w-1)/w, 1/h, (h-1)/h)
3929 const float invWidth = 1.0f / width;
3930 const float invHeight = 1.0f / height;
3931 GL_CALL(Uniform4f(fMipmapPrograms[progIdx].fTexCoordXformUniform,
3932 invWidth, (width - 1) * invWidth, invHeight, (height - 1) * invHeight));
3933 GL_CALL(Uniform1i(fMipmapPrograms[progIdx].fTextureUniform, 0));
3934
3935 // Only sample from previous mip
3936 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_BASE_LEVEL, level - 1));
3937
Brian Salomon930f9392018-06-20 16:25:26 -04003938 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0, GR_GL_TEXTURE_2D,
3939 glTex->textureID(), level));
brianosman33f6b3f2016-06-02 05:49:21 -07003940
3941 width = SkTMax(1, width / 2);
3942 height = SkTMax(1, height / 2);
3943 viewport.fWidth = width;
3944 viewport.fHeight = height;
3945 this->flushViewport(viewport);
3946
3947 GL_CALL(DrawArrays(GR_GL_TRIANGLE_STRIP, 0, 4));
3948 }
3949
3950 // Unbind:
3951 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
3952 GR_GL_TEXTURE_2D, 0, 0));
3953
Brian Salomon930f9392018-06-20 16:25:26 -04003954 // We modified the base level param.
Brian Salomondc829942018-10-23 16:07:24 -04003955 GrGLTexture::NonSamplerParams params = glTex->getCachedNonSamplerParams();
3956 params.fBaseMipMapLevel = levelCount - 2; // we drew the 2nd to last level into the last level.
3957 glTex->setCachedParams(nullptr, params, this->getResetTimestamp());
3958
brianosman33f6b3f2016-06-02 05:49:21 -07003959 return true;
3960}
3961
Chris Daltond7291ba2019-03-07 14:17:03 -07003962void GrGLGpu::querySampleLocations(
3963 GrRenderTarget* renderTarget, const GrStencilSettings& stencilSettings,
3964 SkTArray<SkPoint>* sampleLocations) {
3965 this->flushStencil(stencilSettings);
3966 this->flushHWAAState(renderTarget, true);
3967 this->flushRenderTarget(static_cast<GrGLRenderTarget*>(renderTarget));
3968
3969 int effectiveSampleCnt;
3970 GR_GL_GetIntegerv(this->glInterface(), GR_GL_SAMPLES, &effectiveSampleCnt);
3971 SkASSERT(effectiveSampleCnt >= renderTarget->numStencilSamples());
3972
3973 sampleLocations->reset(effectiveSampleCnt);
3974 for (int i = 0; i < effectiveSampleCnt; ++i) {
3975 GL_CALL(GetMultisamplefv(GR_GL_SAMPLE_POSITION, i, &(*sampleLocations)[i].fX));
3976 }
3977}
3978
cdalton231c5fd2015-05-13 12:35:36 -07003979void GrGLGpu::xferBarrier(GrRenderTarget* rt, GrXferBarrierType type) {
bsalomoncb02b382015-08-12 11:14:50 -07003980 SkASSERT(type);
cdalton9954bc32015-04-29 14:17:00 -07003981 switch (type) {
cdalton231c5fd2015-05-13 12:35:36 -07003982 case kTexture_GrXferBarrierType: {
3983 GrGLRenderTarget* glrt = static_cast<GrGLRenderTarget*>(rt);
Brian Osmancfe83d12018-01-19 11:13:45 -05003984 SkASSERT(glrt->textureFBOID() != 0 && glrt->renderFBOID() != 0);
cdalton231c5fd2015-05-13 12:35:36 -07003985 if (glrt->textureFBOID() != glrt->renderFBOID()) {
3986 // The render target uses separate storage so no need for glTextureBarrier.
3987 // FIXME: The render target will resolve automatically when its texture is bound,
3988 // but we could resolve only the bounds that will be read if we do it here instead.
3989 return;
3990 }
cdalton9954bc32015-04-29 14:17:00 -07003991 SkASSERT(this->caps()->textureBarrierSupport());
3992 GL_CALL(TextureBarrier());
3993 return;
cdalton231c5fd2015-05-13 12:35:36 -07003994 }
cdalton8917d622015-05-06 13:40:21 -07003995 case kBlend_GrXferBarrierType:
bsalomon4b91f762015-05-19 09:29:46 -07003996 SkASSERT(GrCaps::kAdvanced_BlendEquationSupport ==
cdalton8917d622015-05-06 13:40:21 -07003997 this->caps()->blendEquationSupport());
3998 GL_CALL(BlendBarrier());
3999 return;
bsalomoncb02b382015-08-12 11:14:50 -07004000 default: break; // placate compiler warnings that kNone not handled
cdalton9954bc32015-04-29 14:17:00 -07004001 }
4002}
4003
Brian Salomonf865b052018-03-09 09:01:53 -05004004#if GR_TEST_UTILS
Brian Salomon52e943a2018-03-13 09:32:39 -04004005GrBackendTexture GrGLGpu::createTestingOnlyBackendTexture(const void* pixels, int w, int h,
Robert Phillips646f6372018-09-25 09:31:10 -04004006 GrColorType colorType, bool /*isRT*/,
4007 GrMipMapped mipMapped,
4008 size_t rowBytes) {
Brian Salomon8a375832018-03-14 10:21:40 -04004009 this->handleDirtyContext();
Robert Phillips646f6372018-09-25 09:31:10 -04004010
4011 GrPixelConfig config = GrColorTypeToPixelConfig(colorType, GrSRGBEncoded::kNo);
Robert Phillipsd21b2a52017-12-12 13:01:25 -05004012 if (!this->caps()->isConfigTexturable(config)) {
4013 return GrBackendTexture(); // invalid
4014 }
4015
Greg Daniel92cbf3f2018-04-12 16:50:17 -04004016 if (w > this->caps()->maxTextureSize() || h > this->caps()->maxTextureSize()) {
4017 return GrBackendTexture(); // invalid
4018 }
4019
Robert Phillipsd21b2a52017-12-12 13:01:25 -05004020 // Currently we don't support uploading pixel data when mipped.
4021 if (pixels && GrMipMapped::kYes == mipMapped) {
4022 return GrBackendTexture(); // invalid
4023 }
4024
Robert Phillips646f6372018-09-25 09:31:10 -04004025 int bpp = GrColorTypeBytesPerPixel(colorType);
4026 const size_t trimRowBytes = w * bpp;
4027 if (!rowBytes) {
4028 rowBytes = trimRowBytes;
4029 }
4030
Robert Phillipsd21b2a52017-12-12 13:01:25 -05004031 GrGLTextureInfo info;
4032 info.fTarget = GR_GL_TEXTURE_2D;
4033 info.fID = 0;
4034 GL_CALL(GenTextures(1, &info.fID));
Brian Salomon1f05d452019-02-08 12:33:08 -05004035 this->bindTextureToScratchUnit(info.fTarget, info.fID);
Robert Phillipsd21b2a52017-12-12 13:01:25 -05004036 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, 1));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05004037 GL_CALL(TexParameteri(info.fTarget, GR_GL_TEXTURE_MAG_FILTER, GR_GL_NEAREST));
4038 GL_CALL(TexParameteri(info.fTarget, GR_GL_TEXTURE_MIN_FILTER, GR_GL_NEAREST));
4039 GL_CALL(TexParameteri(info.fTarget, GR_GL_TEXTURE_WRAP_S, GR_GL_CLAMP_TO_EDGE));
4040 GL_CALL(TexParameteri(info.fTarget, GR_GL_TEXTURE_WRAP_T, GR_GL_CLAMP_TO_EDGE));
4041
Jim Van Verth1676cb92019-01-15 13:24:45 -05004042 // we have to do something special for compressed textures
4043 if (GrPixelConfigIsCompressed(config)) {
4044 GrGLenum internalFormat;
4045 const GrGLInterface* interface = this->glInterface();
4046 const GrGLCaps& caps = this->glCaps();
4047 if (!caps.getCompressedTexImageFormats(config, &internalFormat)) {
4048 return GrBackendTexture();
Robert Phillips646f6372018-09-25 09:31:10 -04004049 }
Robert Phillipsd21b2a52017-12-12 13:01:25 -05004050
Jim Van Verth1676cb92019-01-15 13:24:45 -05004051 GrMipLevel mipLevel = { pixels, rowBytes };
4052 if (!allocate_and_populate_compressed_texture(config, *interface, caps, info.fTarget,
4053 internalFormat, &mipLevel, 1,
4054 w, h)) {
4055 return GrBackendTexture();
4056 }
4057 } else {
4058 bool restoreGLRowLength = false;
4059 if (trimRowBytes != rowBytes && this->glCaps().unpackRowLengthSupport()) {
4060 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowBytes / bpp));
4061 restoreGLRowLength = true;
4062 }
4063
4064 GrGLenum internalFormat;
4065 GrGLenum externalFormat;
4066 GrGLenum externalType;
4067
4068 if (!this->glCaps().getTexImageFormats(config, config, &internalFormat, &externalFormat,
4069 &externalType)) {
4070 return GrBackendTexture(); // invalid
4071 }
4072
4073 info.fFormat = this->glCaps().configSizedInternalFormat(config);
4074
4075 this->unbindCpuToGpuXferBuffer();
4076
4077 // Figure out the number of mip levels.
4078 int mipLevels = 1;
4079 if (GrMipMapped::kYes == mipMapped) {
4080 mipLevels = SkMipMap::ComputeLevelCount(w, h) + 1;
4081 }
4082
4083 size_t baseLayerSize = bpp * w * h;
4084 SkAutoMalloc defaultStorage(baseLayerSize);
4085 if (!pixels) {
4086 // Fill in the texture with all zeros so we don't have random garbage
4087 pixels = defaultStorage.get();
4088 memset(defaultStorage.get(), 0, baseLayerSize);
4089 } else if (trimRowBytes != rowBytes && !restoreGLRowLength) {
4090 // We weren't able to use GR_GL_UNPACK_ROW_LENGTH so make a copy
4091 char* copy = (char*)defaultStorage.get();
4092 for (int y = 0; y < h; ++y) {
4093 memcpy(&copy[y*trimRowBytes], &((const char*)pixels)[y*rowBytes], trimRowBytes);
4094 }
4095 pixels = copy;
4096 }
4097
4098 int width = w;
4099 int height = h;
4100 for (int i = 0; i < mipLevels; ++i) {
4101 GL_CALL(TexImage2D(info.fTarget, i, internalFormat, width, height, 0, externalFormat,
4102 externalType, pixels));
4103 width = SkTMax(1, width / 2);
4104 height = SkTMax(1, height / 2);
4105 }
4106 if (restoreGLRowLength) {
4107 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
4108 }
Robert Phillipsd21b2a52017-12-12 13:01:25 -05004109 }
4110
Robert Phillipse8fabb22018-02-04 14:33:21 -05004111 // unbind the texture from the texture unit to avoid asserts
4112 GL_CALL(BindTexture(info.fTarget, 0));
4113
Greg Daniel108bb232018-07-03 16:18:29 -04004114 GrBackendTexture beTex = GrBackendTexture(w, h, mipMapped, info);
4115 // Lots of tests don't go through Skia's public interface which will set the config so for
4116 // testing we make sure we set a config here.
4117 beTex.setPixelConfig(config);
4118 return beTex;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05004119}
4120
4121bool GrGLGpu::isTestingOnlyBackendTexture(const GrBackendTexture& tex) const {
Greg Danielbdf12ad2018-10-12 09:31:11 -04004122 SkASSERT(GrBackendApi::kOpenGL == tex.backend());
Robert Phillipsd21b2a52017-12-12 13:01:25 -05004123
Greg Daniel52e16d92018-04-10 09:34:07 -04004124 GrGLTextureInfo info;
4125 if (!tex.getGLTextureInfo(&info)) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -05004126 return false;
4127 }
4128
4129 GrGLboolean result;
Greg Daniel52e16d92018-04-10 09:34:07 -04004130 GL_CALL_RET(result, IsTexture(info.fID));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05004131
4132 return (GR_GL_TRUE == result);
4133}
4134
Brian Salomon26102cb2018-03-09 09:33:19 -05004135void GrGLGpu::deleteTestingOnlyBackendTexture(const GrBackendTexture& tex) {
Greg Danielbdf12ad2018-10-12 09:31:11 -04004136 SkASSERT(GrBackendApi::kOpenGL == tex.backend());
Robert Phillipsd21b2a52017-12-12 13:01:25 -05004137
Greg Daniel52e16d92018-04-10 09:34:07 -04004138 GrGLTextureInfo info;
4139 if (tex.getGLTextureInfo(&info)) {
4140 GL_CALL(DeleteTextures(1, &info.fID));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05004141 }
4142}
4143
Brian Salomonf865b052018-03-09 09:01:53 -05004144GrBackendRenderTarget GrGLGpu::createTestingOnlyBackendRenderTarget(int w, int h,
Brian Osman2d010b62018-08-09 10:55:09 -04004145 GrColorType colorType) {
Greg Daniel92cbf3f2018-04-12 16:50:17 -04004146 if (w > this->caps()->maxRenderTargetSize() || h > this->caps()->maxRenderTargetSize()) {
4147 return GrBackendRenderTarget(); // invalid
4148 }
Brian Salomon8a375832018-03-14 10:21:40 -04004149 this->handleDirtyContext();
Brian Osman2d010b62018-08-09 10:55:09 -04004150 auto config = GrColorTypeToPixelConfig(colorType, GrSRGBEncoded::kNo);
Brian Salomon93348dd2018-08-29 12:56:23 -04004151 if (!this->glCaps().isConfigRenderable(config)) {
Brian Salomonf865b052018-03-09 09:01:53 -05004152 return {};
4153 }
Brian Salomon93348dd2018-08-29 12:56:23 -04004154 bool useTexture = false;
4155 GrGLenum colorBufferFormat;
4156 GrGLenum externalFormat = 0, externalType = 0;
4157 if (config == kBGRA_8888_GrPixelConfig && this->glCaps().bgraIsInternalFormat()) {
4158 // BGRA render buffers are not supported.
4159 this->glCaps().getTexImageFormats(config, config, &colorBufferFormat, &externalFormat,
4160 &externalType);
4161 useTexture = true;
4162 } else {
4163 this->glCaps().getRenderbufferFormat(config, &colorBufferFormat);
4164 }
Brian Salomonf865b052018-03-09 09:01:53 -05004165 int sFormatIdx = this->getCompatibleStencilIndex(config);
4166 if (sFormatIdx < 0) {
4167 return {};
4168 }
Brian Salomon93348dd2018-08-29 12:56:23 -04004169 GrGLuint colorID = 0;
4170 GrGLuint stencilID = 0;
4171 auto deleteIDs = [&] {
4172 if (colorID) {
4173 if (useTexture) {
4174 GL_CALL(DeleteTextures(1, &colorID));
4175 } else {
4176 GL_CALL(DeleteRenderbuffers(1, &colorID));
4177 }
Brian Salomonf865b052018-03-09 09:01:53 -05004178 }
Brian Salomon93348dd2018-08-29 12:56:23 -04004179 if (stencilID) {
4180 GL_CALL(DeleteRenderbuffers(1, &stencilID));
Brian Salomonf865b052018-03-09 09:01:53 -05004181 }
Brian Salomon93348dd2018-08-29 12:56:23 -04004182 };
4183
4184 if (useTexture) {
4185 GL_CALL(GenTextures(1, &colorID));
4186 } else {
4187 GL_CALL(GenRenderbuffers(1, &colorID));
4188 }
4189 GL_CALL(GenRenderbuffers(1, &stencilID));
4190 if (!stencilID || !colorID) {
4191 deleteIDs();
Brian Salomonf865b052018-03-09 09:01:53 -05004192 return {};
4193 }
Brian Salomon93348dd2018-08-29 12:56:23 -04004194
Brian Salomonf865b052018-03-09 09:01:53 -05004195 GrGLFramebufferInfo info;
4196 info.fFBOID = 0;
Brian Salomon93348dd2018-08-29 12:56:23 -04004197 this->glCaps().getSizedInternalFormat(config, &info.fFormat);
Brian Salomonf865b052018-03-09 09:01:53 -05004198 GL_CALL(GenFramebuffers(1, &info.fFBOID));
4199 if (!info.fFBOID) {
Brian Salomon93348dd2018-08-29 12:56:23 -04004200 deleteIDs();
4201 return {};
Brian Salomonf865b052018-03-09 09:01:53 -05004202 }
4203
4204 this->invalidateBoundRenderTarget();
4205
Adrienne Walker4ee88512018-05-17 11:37:14 -07004206 this->bindFramebuffer(GR_GL_FRAMEBUFFER, info.fFBOID);
Brian Salomon93348dd2018-08-29 12:56:23 -04004207 if (useTexture) {
Brian Salomond978b902019-02-07 15:09:18 -05004208 this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, colorID);
Brian Salomon93348dd2018-08-29 12:56:23 -04004209 GL_CALL(TexImage2D(GR_GL_TEXTURE_2D, 0, colorBufferFormat, w, h, 0, externalFormat,
4210 externalType, nullptr));
4211 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0, GR_GL_TEXTURE_2D,
4212 colorID, 0));
4213 } else {
4214 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, colorID));
4215 GL_ALLOC_CALL(this->glInterface(),
4216 RenderbufferStorage(GR_GL_RENDERBUFFER, colorBufferFormat, w, h));
4217 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
4218 GR_GL_RENDERBUFFER, colorID));
4219 }
4220 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, stencilID));
Brian Salomonf865b052018-03-09 09:01:53 -05004221 auto stencilBufferFormat = this->glCaps().stencilFormats()[sFormatIdx].fInternalFormat;
4222 GL_ALLOC_CALL(this->glInterface(),
4223 RenderbufferStorage(GR_GL_RENDERBUFFER, stencilBufferFormat, w, h));
4224 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT, GR_GL_RENDERBUFFER,
Brian Salomon93348dd2018-08-29 12:56:23 -04004225 stencilID));
Brian Salomonf865b052018-03-09 09:01:53 -05004226 if (this->glCaps().stencilFormats()[sFormatIdx].fPacked) {
4227 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_DEPTH_ATTACHMENT,
Brian Salomon93348dd2018-08-29 12:56:23 -04004228 GR_GL_RENDERBUFFER, stencilID));
Brian Salomonf865b052018-03-09 09:01:53 -05004229 }
4230
Brian Salomon93348dd2018-08-29 12:56:23 -04004231 // We don't want to have to recover the renderbuffer/texture IDs later to delete them. OpenGL
4232 // has this rule that if a renderbuffer/texture is deleted and a FBO other than the current FBO
4233 // has the RB attached then deletion is delayed. So we unbind the FBO here and delete the
4234 // renderbuffers/texture.
Adrienne Walker4ee88512018-05-17 11:37:14 -07004235 this->bindFramebuffer(GR_GL_FRAMEBUFFER, 0);
Brian Salomon93348dd2018-08-29 12:56:23 -04004236 deleteIDs();
Brian Salomonf865b052018-03-09 09:01:53 -05004237
Adrienne Walker4ee88512018-05-17 11:37:14 -07004238 this->bindFramebuffer(GR_GL_FRAMEBUFFER, info.fFBOID);
Brian Salomonf865b052018-03-09 09:01:53 -05004239 GrGLenum status;
4240 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
4241 if (GR_GL_FRAMEBUFFER_COMPLETE != status) {
Adrienne Walker4ee88512018-05-17 11:37:14 -07004242 this->deleteFramebuffer(info.fFBOID);
Brian Salomonf865b052018-03-09 09:01:53 -05004243 return {};
4244 }
4245 auto stencilBits = SkToInt(this->glCaps().stencilFormats()[sFormatIdx].fStencilBits);
Greg Daniel108bb232018-07-03 16:18:29 -04004246 GrBackendRenderTarget beRT = GrBackendRenderTarget(w, h, 1, stencilBits, info);
4247 // Lots of tests don't go through Skia's public interface which will set the config so for
4248 // testing we make sure we set a config here.
4249 beRT.setPixelConfig(config);
Brian Salomon93348dd2018-08-29 12:56:23 -04004250#ifdef SK_DEBUG
4251 SkColorType skColorType = GrColorTypeToSkColorType(colorType);
4252 if (skColorType != kUnknown_SkColorType) {
4253 SkASSERT(this->caps()->validateBackendRenderTarget(
Brian Salomonf391d0f2018-12-14 09:18:50 -05004254 beRT, GrColorTypeToSkColorType(colorType)) != kUnknown_GrPixelConfig);
Brian Salomon93348dd2018-08-29 12:56:23 -04004255 }
4256#endif
Greg Daniel108bb232018-07-03 16:18:29 -04004257 return beRT;
Brian Salomonf865b052018-03-09 09:01:53 -05004258}
4259
4260void GrGLGpu::deleteTestingOnlyBackendRenderTarget(const GrBackendRenderTarget& backendRT) {
Greg Danielbdf12ad2018-10-12 09:31:11 -04004261 SkASSERT(GrBackendApi::kOpenGL == backendRT.backend());
Greg Daniel323fbcf2018-04-10 13:46:30 -04004262 GrGLFramebufferInfo info;
4263 if (backendRT.getGLFramebufferInfo(&info)) {
4264 if (info.fFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -07004265 this->deleteFramebuffer(info.fFBOID);
Brian Salomonf865b052018-03-09 09:01:53 -05004266 }
4267 }
joshualitt8fd844f2015-12-02 13:36:47 -08004268}
4269
Greg Daniel26b50a42018-03-08 09:49:58 -05004270void GrGLGpu::testingOnly_flushGpuAndSync() {
4271 GL_CALL(Finish());
4272}
Brian Salomonf865b052018-03-09 09:01:53 -05004273#endif
Greg Daniel26b50a42018-03-08 09:49:58 -05004274
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00004275///////////////////////////////////////////////////////////////////////////////
bsalomon6df86402015-06-01 10:41:49 -07004276
cdaltone2e71c22016-04-07 18:13:29 -07004277GrGLAttribArrayState* GrGLGpu::HWVertexArrayState::bindInternalVertexArray(GrGLGpu* gpu,
csmartdalton485a1202016-07-13 10:16:32 -07004278 const GrBuffer* ibuf) {
robertphillips@google.com4f65a272013-03-26 19:40:46 +00004279 GrGLAttribArrayState* attribState;
4280
cdaltone2e71c22016-04-07 18:13:29 -07004281 if (gpu->glCaps().isCoreProfile()) {
4282 if (!fCoreProfileVertexArray) {
bsalomon@google.com6918d482013-03-07 19:09:11 +00004283 GrGLuint arrayID;
4284 GR_GL_CALL(gpu->glInterface(), GenVertexArrays(1, &arrayID));
4285 int attrCount = gpu->glCaps().maxVertexAttributes();
cdaltone2e71c22016-04-07 18:13:29 -07004286 fCoreProfileVertexArray = new GrGLVertexArray(arrayID, attrCount);
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00004287 }
cdaltone2e71c22016-04-07 18:13:29 -07004288 if (ibuf) {
4289 attribState = fCoreProfileVertexArray->bindWithIndexBuffer(gpu, ibuf);
bsalomon6df86402015-06-01 10:41:49 -07004290 } else {
cdaltone2e71c22016-04-07 18:13:29 -07004291 attribState = fCoreProfileVertexArray->bind(gpu);
bsalomon6df86402015-06-01 10:41:49 -07004292 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00004293 } else {
cdaltone2e71c22016-04-07 18:13:29 -07004294 if (ibuf) {
4295 // bindBuffer implicitly binds VAO 0 when binding an index buffer.
Brian Salomonae64c192019-02-05 09:41:37 -05004296 gpu->bindBuffer(GrGpuBufferType::kIndex, ibuf);
bsalomon@google.com6918d482013-03-07 19:09:11 +00004297 } else {
4298 this->setVertexArrayID(gpu, 0);
4299 }
4300 int attrCount = gpu->glCaps().maxVertexAttributes();
4301 if (fDefaultVertexArrayAttribState.count() != attrCount) {
4302 fDefaultVertexArrayAttribState.resize(attrCount);
4303 }
4304 attribState = &fDefaultVertexArrayAttribState;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00004305 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00004306 return attribState;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00004307}
bsalomone179a912016-01-20 06:18:10 -08004308
Greg Danielbae71212019-03-01 15:24:35 -05004309void GrGLGpu::onFinishFlush(GrSurfaceProxy*, SkSurface::BackendSurfaceAccess access,
Greg Danielb9990e42019-04-10 16:28:52 -04004310 GrFlushFlags flags, bool insertedSemaphore) {
Greg Daniel51316782017-08-02 15:10:09 +00004311 // If we inserted semaphores during the flush, we need to call GLFlush.
4312 if (insertedSemaphore) {
4313 GL_CALL(Flush());
4314 }
Greg Danielb9990e42019-04-10 16:28:52 -04004315 if (flags & kSyncCpu_GrFlushFlag) {
Greg Danielbae71212019-03-01 15:24:35 -05004316 GL_CALL(Finish());
4317 }
Greg Daniel51316782017-08-02 15:10:09 +00004318}
4319
Robert Phillips5b5d84c2018-08-09 15:12:18 -04004320void GrGLGpu::submit(GrGpuCommandBuffer* buffer) {
4321 if (buffer->asRTCommandBuffer()) {
4322 SkASSERT(fCachedRTCommandBuffer.get() == buffer);
4323 fCachedRTCommandBuffer->reset();
4324 } else {
4325 SkASSERT(fCachedTexCommandBuffer.get() == buffer);
4326 fCachedTexCommandBuffer->reset();
4327 }
4328}
4329
Greg Daniel6be35232017-03-01 17:01:09 -05004330GrFence SK_WARN_UNUSED_RESULT GrGLGpu::insertFence() {
Greg Danielc64ee462017-06-15 16:59:49 -04004331 SkASSERT(this->caps()->fenceSyncSupport());
Greg Daniel6be35232017-03-01 17:01:09 -05004332 GrGLsync sync;
4333 GL_CALL_RET(sync, FenceSync(GR_GL_SYNC_GPU_COMMANDS_COMPLETE, 0));
4334 GR_STATIC_ASSERT(sizeof(GrFence) >= sizeof(GrGLsync));
4335 return (GrFence)sync;
jvanverth84741b32016-09-30 08:39:02 -07004336}
4337
Greg Daniel6be35232017-03-01 17:01:09 -05004338bool GrGLGpu::waitFence(GrFence fence, uint64_t timeout) {
jvanverth84741b32016-09-30 08:39:02 -07004339 GrGLenum result;
4340 GL_CALL_RET(result, ClientWaitSync((GrGLsync)fence, GR_GL_SYNC_FLUSH_COMMANDS_BIT, timeout));
4341 return (GR_GL_CONDITION_SATISFIED == result);
4342}
4343
4344void GrGLGpu::deleteFence(GrFence fence) const {
Greg Daniel6be35232017-03-01 17:01:09 -05004345 this->deleteSync((GrGLsync)fence);
4346}
4347
Greg Daniela5cb7812017-06-16 09:45:32 -04004348sk_sp<GrSemaphore> SK_WARN_UNUSED_RESULT GrGLGpu::makeSemaphore(bool isOwned) {
Greg Danielc64ee462017-06-15 16:59:49 -04004349 SkASSERT(this->caps()->fenceSyncSupport());
Greg Daniela5cb7812017-06-16 09:45:32 -04004350 return GrGLSemaphore::Make(this, isOwned);
Greg Daniel6be35232017-03-01 17:01:09 -05004351}
4352
Greg Daniel48661b82018-01-22 16:11:35 -05004353sk_sp<GrSemaphore> GrGLGpu::wrapBackendSemaphore(const GrBackendSemaphore& semaphore,
4354 GrResourceProvider::SemaphoreWrapType wrapType,
4355 GrWrapOwnership ownership) {
Greg Danielc64ee462017-06-15 16:59:49 -04004356 SkASSERT(this->caps()->fenceSyncSupport());
Greg Daniela5cb7812017-06-16 09:45:32 -04004357 return GrGLSemaphore::MakeWrapped(this, semaphore.glSync(), ownership);
4358}
4359
Greg Daniel858e12c2018-12-06 11:11:37 -05004360void GrGLGpu::insertSemaphore(sk_sp<GrSemaphore> semaphore) {
Greg Daniel6be35232017-03-01 17:01:09 -05004361 GrGLSemaphore* glSem = static_cast<GrGLSemaphore*>(semaphore.get());
4362
Greg Daniel48661b82018-01-22 16:11:35 -05004363 GrGLsync sync;
4364 GL_CALL_RET(sync, FenceSync(GR_GL_SYNC_GPU_COMMANDS_COMPLETE, 0));
4365 glSem->setSync(sync);
Greg Daniel6be35232017-03-01 17:01:09 -05004366}
4367
Greg Daniel48661b82018-01-22 16:11:35 -05004368void GrGLGpu::waitSemaphore(sk_sp<GrSemaphore> semaphore) {
Greg Daniel6be35232017-03-01 17:01:09 -05004369 GrGLSemaphore* glSem = static_cast<GrGLSemaphore*>(semaphore.get());
4370
4371 GL_CALL(WaitSync(glSem->sync(), 0, GR_GL_TIMEOUT_IGNORED));
4372}
4373
4374void GrGLGpu::deleteSync(GrGLsync sync) const {
4375 GL_CALL(DeleteSync(sync));
jvanverth84741b32016-09-30 08:39:02 -07004376}
Brian Osman13dddce2017-05-09 13:19:50 -04004377
Robert Phillips65a88fa2017-08-08 08:36:22 -04004378void GrGLGpu::insertEventMarker(const char* msg) {
4379 GL_CALL(InsertEventMarker(strlen(msg), msg));
4380}
4381
Brian Osman13dddce2017-05-09 13:19:50 -04004382sk_sp<GrSemaphore> GrGLGpu::prepareTextureForCrossContextUsage(GrTexture* texture) {
4383 // Set up a semaphore to be signaled once the data is ready, and flush GL
Greg Daniela5cb7812017-06-16 09:45:32 -04004384 sk_sp<GrSemaphore> semaphore = this->makeSemaphore(true);
Greg Daniel858e12c2018-12-06 11:11:37 -05004385 this->insertSemaphore(semaphore);
4386 // We must call flush here to make sure the GrGLSync object gets created and sent to the gpu.
4387 GL_CALL(Flush());
Brian Osman13dddce2017-05-09 13:19:50 -04004388
4389 return semaphore;
4390}
Robert Phillips646e4292017-06-13 12:44:56 -04004391
4392int GrGLGpu::TextureToCopyProgramIdx(GrTexture* texture) {
Brian Salomon60dd8c72018-07-30 10:24:13 -04004393 switch (GrSLCombinedSamplerTypeForTextureType(texture->texturePriv().textureType())) {
Robert Phillips646e4292017-06-13 12:44:56 -04004394 case kTexture2DSampler_GrSLType:
4395 return 0;
Robert Phillips646e4292017-06-13 12:44:56 -04004396 case kTexture2DRectSampler_GrSLType:
Brian Salomon57111332018-02-05 15:55:54 -05004397 return 1;
Robert Phillips646e4292017-06-13 12:44:56 -04004398 case kTextureExternalSampler_GrSLType:
Brian Salomon57111332018-02-05 15:55:54 -05004399 return 2;
Robert Phillips646e4292017-06-13 12:44:56 -04004400 default:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -04004401 SK_ABORT("Unexpected samper type");
Robert Phillips646e4292017-06-13 12:44:56 -04004402 return 0;
4403 }
4404}
Brian Osman71a18892017-08-10 10:23:25 -04004405
Kevin Lubickf4def342018-10-04 12:52:50 -04004406#ifdef SK_ENABLE_DUMP_GPU
4407#include "SkJSONWriter.h"
Brian Osman71a18892017-08-10 10:23:25 -04004408void GrGLGpu::onDumpJSON(SkJSONWriter* writer) const {
4409 // We are called by the base class, which has already called beginObject(). We choose to nest
4410 // all of our caps information in a named sub-object.
4411 writer->beginObject("GL GPU");
4412
4413 const GrGLubyte* str;
4414 GL_CALL_RET(str, GetString(GR_GL_VERSION));
4415 writer->appendString("GL_VERSION", (const char*)(str));
4416 GL_CALL_RET(str, GetString(GR_GL_RENDERER));
4417 writer->appendString("GL_RENDERER", (const char*)(str));
4418 GL_CALL_RET(str, GetString(GR_GL_VENDOR));
4419 writer->appendString("GL_VENDOR", (const char*)(str));
4420 GL_CALL_RET(str, GetString(GR_GL_SHADING_LANGUAGE_VERSION));
4421 writer->appendString("GL_SHADING_LANGUAGE_VERSION", (const char*)(str));
4422
4423 writer->appendName("extensions");
4424 glInterface()->fExtensions.dumpJSON(writer);
4425
4426 writer->endObject();
4427}
Kevin Lubickf4def342018-10-04 12:52:50 -04004428#endif