blob: 7d9ee534788b34e3c0f5a8c83e9a5e7739989bbf [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 Salomon9b009bb2018-02-14 13:53:55 -0500885bool GrGLGpu::onTransferPixels(GrTexture* texture, int left, int top, int width, int height,
Brian Salomondbf70722019-02-07 11:31:24 -0500886 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
cblume55f2d2d2016-02-26 13:20:48 -0800962/**
963 * Creates storage space for the texture and fills it with texels.
964 *
Brian Salomond1eaf492017-05-18 10:02:08 -0400965 * @param config Pixel config of the texture.
cblume55f2d2d2016-02-26 13:20:48 -0800966 * @param interface The GL interface in use.
cblume790d5132016-02-29 11:13:29 -0800967 * @param caps The capabilities of the GL device.
Jim Van Verth1676cb92019-01-15 13:24:45 -0500968 * @param target Which bound texture to target (GR_GL_TEXTURE_2D, e.g.)
brianosman81a84852016-09-12 09:05:14 -0700969 * @param internalFormat The data format used for the internal storage of the texture. May be sized.
970 * @param internalFormatForTexStorage The data format used for the TexStorage API. Must be sized.
cblume55f2d2d2016-02-26 13:20:48 -0800971 * @param externalFormat The data format used for the external storage of the texture.
972 * @param externalType The type of the data used for the external storage of the texture.
973 * @param texels The texel data of the texture being created.
Jim Van Verth1676cb92019-01-15 13:24:45 -0500974 * @param mipLevelCount Number of mipmap levels
cblume55f2d2d2016-02-26 13:20:48 -0800975 * @param baseWidth The width of the texture's base mipmap level
976 * @param baseHeight The height of the texture's base mipmap level
cblume55f2d2d2016-02-26 13:20:48 -0800977 */
Robert Phillips92de6312017-05-23 07:43:48 -0400978static bool allocate_and_populate_texture(GrPixelConfig config,
979 const GrGLInterface& interface,
980 const GrGLCaps& caps,
981 GrGLenum target,
982 GrGLenum internalFormat,
983 GrGLenum internalFormatForTexStorage,
984 GrGLenum externalFormat,
985 GrGLenum externalType,
Robert Phillips590533f2017-07-11 14:22:35 -0400986 const GrMipLevel texels[], int mipLevelCount,
Robert Phillips92de6312017-05-23 07:43:48 -0400987 int baseWidth, int baseHeight) {
cblume55f2d2d2016-02-26 13:20:48 -0800988 CLEAR_ERROR_BEFORE_ALLOC(&interface);
cblume790d5132016-02-29 11:13:29 -0800989
Brian Salomon7609ac62019-03-22 12:17:27 -0400990 if (caps.isConfigTexSupportEnabled(config)) {
cblume790d5132016-02-29 11:13:29 -0800991 // We never resize or change formats of textures.
cblume55f2d2d2016-02-26 13:20:48 -0800992 GL_ALLOC_CALL(&interface,
Robert Phillips590533f2017-07-11 14:22:35 -0400993 TexStorage2D(target, SkTMax(mipLevelCount, 1), internalFormatForTexStorage,
Brian Salomond1eaf492017-05-18 10:02:08 -0400994 baseWidth, baseHeight));
Brian Salomon0ec981b2017-05-15 13:48:50 -0400995 GrGLenum error = CHECK_ALLOC_ERROR(&interface);
cblume55f2d2d2016-02-26 13:20:48 -0800996 if (error != GR_GL_NO_ERROR) {
bsalomone699d0c2016-03-09 06:25:15 -0800997 return false;
cblume790d5132016-02-29 11:13:29 -0800998 } else {
Robert Phillips590533f2017-07-11 14:22:35 -0400999 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
cblume790d5132016-02-29 11:13:29 -08001000 const void* currentMipData = texels[currentMipLevel].fPixels;
1001 if (currentMipData == nullptr) {
1002 continue;
1003 }
1004 int twoToTheMipLevel = 1 << currentMipLevel;
Brian Salomond1eaf492017-05-18 10:02:08 -04001005 int currentWidth = SkTMax(1, baseWidth / twoToTheMipLevel);
1006 int currentHeight = SkTMax(1, baseHeight / twoToTheMipLevel);
cblume790d5132016-02-29 11:13:29 -08001007
1008 GR_GL_CALL(&interface,
1009 TexSubImage2D(target,
1010 currentMipLevel,
1011 0, // left
1012 0, // top
1013 currentWidth,
1014 currentHeight,
1015 externalFormat, externalType,
1016 currentMipData));
1017 }
bsalomone699d0c2016-03-09 06:25:15 -08001018 return true;
cblume790d5132016-02-29 11:13:29 -08001019 }
1020 } else {
Robert Phillips590533f2017-07-11 14:22:35 -04001021 if (!mipLevelCount) {
cblume790d5132016-02-29 11:13:29 -08001022 GL_ALLOC_CALL(&interface,
1023 TexImage2D(target,
bsalomone699d0c2016-03-09 06:25:15 -08001024 0,
cblume790d5132016-02-29 11:13:29 -08001025 internalFormat,
bsalomone699d0c2016-03-09 06:25:15 -08001026 baseWidth,
1027 baseHeight,
cblume790d5132016-02-29 11:13:29 -08001028 0, // border
1029 externalFormat, externalType,
bsalomone699d0c2016-03-09 06:25:15 -08001030 nullptr));
Brian Salomon0ec981b2017-05-15 13:48:50 -04001031 GrGLenum error = CHECK_ALLOC_ERROR(&interface);
cblume790d5132016-02-29 11:13:29 -08001032 if (error != GR_GL_NO_ERROR) {
bsalomone699d0c2016-03-09 06:25:15 -08001033 return false;
1034 }
1035 } else {
Robert Phillips590533f2017-07-11 14:22:35 -04001036 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
bsalomone699d0c2016-03-09 06:25:15 -08001037 int twoToTheMipLevel = 1 << currentMipLevel;
1038 int currentWidth = SkTMax(1, baseWidth / twoToTheMipLevel);
1039 int currentHeight = SkTMax(1, baseHeight / twoToTheMipLevel);
Greg Daniel8b059bd2017-09-28 20:46:45 +00001040 const void* currentMipData = texels[currentMipLevel].fPixels;
bsalomone699d0c2016-03-09 06:25:15 -08001041 // Even if curremtMipData is nullptr, continue to call TexImage2D.
1042 // This will allocate texture memory which we can later populate.
1043 GL_ALLOC_CALL(&interface,
1044 TexImage2D(target,
1045 currentMipLevel,
1046 internalFormat,
1047 currentWidth,
1048 currentHeight,
1049 0, // border
1050 externalFormat, externalType,
1051 currentMipData));
Brian Salomon0ec981b2017-05-15 13:48:50 -04001052 GrGLenum error = CHECK_ALLOC_ERROR(&interface);
bsalomone699d0c2016-03-09 06:25:15 -08001053 if (error != GR_GL_NO_ERROR) {
1054 return false;
1055 }
cblume790d5132016-02-29 11:13:29 -08001056 }
cblume55f2d2d2016-02-26 13:20:48 -08001057 }
1058 }
bsalomone699d0c2016-03-09 06:25:15 -08001059 return true;
cblume55f2d2d2016-02-26 13:20:48 -08001060}
1061
1062/**
Jim Van Verth1676cb92019-01-15 13:24:45 -05001063 * Creates storage space for the texture and fills it with texels.
1064 *
1065 * @param config Compressed pixel config of the texture.
1066 * @param interface The GL interface in use.
1067 * @param caps The capabilities of the GL device.
1068 * @param target Which bound texture to target (GR_GL_TEXTURE_2D, e.g.)
1069 * @param internalFormat The data format used for the internal storage of the texture.
1070 * @param texels The texel data of the texture being created.
1071 * @param mipLevelCount Number of mipmap levels
1072 * @param baseWidth The width of the texture's base mipmap level
1073 * @param baseHeight The height of the texture's base mipmap level
1074 */
1075static bool allocate_and_populate_compressed_texture(GrPixelConfig config,
1076 const GrGLInterface& interface,
1077 const GrGLCaps& caps,
1078 GrGLenum target, GrGLenum internalFormat,
1079 const GrMipLevel texels[], int mipLevelCount,
1080 int baseWidth, int baseHeight) {
1081 CLEAR_ERROR_BEFORE_ALLOC(&interface);
1082 SkASSERT(GrPixelConfigIsCompressed(config));
1083
1084 bool useTexStorage = caps.isConfigTexSupportEnabled(config);
1085 // We can only use TexStorage if we know we will not later change the storage requirements.
1086 // This means if we may later want to add mipmaps, we cannot use TexStorage.
1087 // Right now, we cannot know if we will later add mipmaps or not.
1088 // The only time we can use TexStorage is when we already have the
1089 // mipmaps.
1090 useTexStorage &= mipLevelCount > 1;
1091
1092 if (useTexStorage) {
1093 // We never resize or change formats of textures.
1094 GL_ALLOC_CALL(&interface,
1095 TexStorage2D(target,
1096 mipLevelCount,
1097 internalFormat,
1098 baseWidth, baseHeight));
1099 GrGLenum error = CHECK_ALLOC_ERROR(&interface);
1100 if (error != GR_GL_NO_ERROR) {
1101 return false;
1102 } else {
1103 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
1104 const void* currentMipData = texels[currentMipLevel].fPixels;
1105 if (currentMipData == nullptr) {
1106 // Compressed textures require data for every level
1107 return false;
1108 }
1109
1110 int twoToTheMipLevel = 1 << currentMipLevel;
1111 int currentWidth = SkTMax(1, baseWidth / twoToTheMipLevel);
1112 int currentHeight = SkTMax(1, baseHeight / twoToTheMipLevel);
1113
1114 // Make sure that the width and height that we pass to OpenGL
1115 // is a multiple of the block size.
1116 size_t dataSize = GrCompressedFormatDataSize(config, currentWidth, currentHeight);
1117 GR_GL_CALL(&interface, CompressedTexSubImage2D(target,
1118 currentMipLevel,
1119 0, // left
1120 0, // top
1121 currentWidth,
1122 currentHeight,
1123 internalFormat,
1124 SkToInt(dataSize),
1125 currentMipData));
1126 }
1127 }
1128 } else {
1129 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
1130 const void* currentMipData = texels[currentMipLevel].fPixels;
1131 if (currentMipData == nullptr) {
1132 // Compressed textures require data for every level
1133 return false;
1134 }
1135
1136 int twoToTheMipLevel = 1 << currentMipLevel;
1137 int currentWidth = SkTMax(1, baseWidth / twoToTheMipLevel);
1138 int currentHeight = SkTMax(1, baseHeight / twoToTheMipLevel);
1139
1140 // Make sure that the width and height that we pass to OpenGL
1141 // is a multiple of the block size.
1142 size_t dataSize = GrCompressedFormatDataSize(config, baseWidth, baseHeight);
1143
1144 GL_ALLOC_CALL(&interface,
1145 CompressedTexImage2D(target,
1146 currentMipLevel,
1147 internalFormat,
1148 currentWidth,
1149 currentHeight,
1150 0, // border
1151 SkToInt(dataSize),
1152 currentMipData));
1153
1154 GrGLenum error = CHECK_ALLOC_ERROR(&interface);
1155 if (error != GR_GL_NO_ERROR) {
1156 return false;
1157 }
1158 }
1159 }
1160
1161 return true;
1162}
1163/**
cblume55f2d2d2016-02-26 13:20:48 -08001164 * After a texture is created, any state which was altered during its creation
1165 * needs to be restored.
1166 *
1167 * @param interface The GL interface to use.
1168 * @param caps The capabilities of the GL device.
1169 * @param restoreGLRowLength Should the row length unpacking be restored?
1170 * @param glFlipY Did GL flip the texture vertically?
1171 */
1172static void restore_pixelstore_state(const GrGLInterface& interface, const GrGLCaps& caps,
Brian Salomona9b04b92018-06-01 15:04:28 -04001173 bool restoreGLRowLength) {
cblume55f2d2d2016-02-26 13:20:48 -08001174 if (restoreGLRowLength) {
1175 SkASSERT(caps.unpackRowLengthSupport());
1176 GR_GL_CALL(&interface, PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
1177 }
cblume55f2d2d2016-02-26 13:20:48 -08001178}
1179
Brian Osman9b560242017-09-05 15:34:52 -04001180void GrGLGpu::unbindCpuToGpuXferBuffer() {
Brian Salomonae64c192019-02-05 09:41:37 -05001181 auto* xferBufferState = this->hwBufferState(GrGpuBufferType::kXferCpuToGpu);
1182 if (!xferBufferState->fBoundBufferUniqueID.isInvalid()) {
1183 GL_CALL(BindBuffer(xferBufferState->fGLTarget, 0));
1184 xferBufferState->invalidate();
Brian Osman9b560242017-09-05 15:34:52 -04001185 }
Brian Osman9b560242017-09-05 15:34:52 -04001186}
1187
Brian Salomonc320b152018-02-20 14:05:36 -05001188// TODO: Make this take a GrColorType instead of dataConfig. This requires updating GrGLCaps to
1189// convert from GrColorType to externalFormat/externalType GLenum values.
Brian Salomona9b04b92018-06-01 15:04:28 -04001190bool GrGLGpu::uploadTexData(GrPixelConfig texConfig, int texWidth, int texHeight, GrGLenum target,
1191 UploadType uploadType, int left, int top, int width, int height,
1192 GrPixelConfig dataConfig, const GrMipLevel texels[], int mipLevelCount,
Greg Daniel0fc4d2d2017-10-12 11:23:36 -04001193 GrMipMapsStatus* mipMapsStatus) {
Jim Van Verth1676cb92019-01-15 13:24:45 -05001194 // If we're uploading compressed data then we should be using uploadCompressedTexData
1195 SkASSERT(!GrPixelConfigIsCompressed(dataConfig));
1196
Brian Salomond1eaf492017-05-18 10:02:08 -04001197 SkASSERT(this->caps()->isConfigTexturable(texConfig));
Greg Daniel660cc992017-06-26 14:55:05 -04001198 SkDEBUGCODE(
1199 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
1200 SkIRect bounds = SkIRect::MakeWH(texWidth, texHeight);
1201 SkASSERT(bounds.contains(subRect));
1202 )
Robert Phillips590533f2017-07-11 14:22:35 -04001203 SkASSERT(1 == mipLevelCount ||
Greg Daniel660cc992017-06-26 14:55:05 -04001204 (0 == left && 0 == top && width == texWidth && height == texHeight));
bsalomon5b30c6f2015-12-17 14:17:34 -08001205
Brian Osman9b560242017-09-05 15:34:52 -04001206 this->unbindCpuToGpuXferBuffer();
Jim Van Verth2e5eaf02017-06-21 15:55:46 -04001207
cblume55f2d2d2016-02-26 13:20:48 -08001208 // texels is const.
1209 // But we may need to flip the texture vertically to prepare it.
1210 // Rather than flip in place and alter the incoming data,
1211 // we allocate a new buffer to flip into.
1212 // This means we need to make a non-const shallow copy of texels.
Robert Phillips0f992772017-07-12 08:24:56 -04001213 SkAutoTMalloc<GrMipLevel> texelsShallowCopy;
1214
1215 if (mipLevelCount) {
1216 texelsShallowCopy.reset(mipLevelCount);
1217 memcpy(texelsShallowCopy.get(), texels, mipLevelCount*sizeof(GrMipLevel));
1218 }
cblume55f2d2d2016-02-26 13:20:48 -08001219
cblume55f2d2d2016-02-26 13:20:48 -08001220 const GrGLInterface* interface = this->glInterface();
1221 const GrGLCaps& caps = this->glCaps();
1222
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001223 size_t bpp = GrBytesPerPixel(dataConfig);
cblume55f2d2d2016-02-26 13:20:48 -08001224
1225 if (width == 0 || height == 0) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +00001226 return false;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001227 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00001228
bsalomon5b30c6f2015-12-17 14:17:34 -08001229 // Internal format comes from the texture desc.
bsalomon76148af2016-01-12 11:13:47 -08001230 GrGLenum internalFormat;
bsalomon5b30c6f2015-12-17 14:17:34 -08001231 // External format and type come from the upload data.
bsalomon76148af2016-01-12 11:13:47 -08001232 GrGLenum externalFormat;
1233 GrGLenum externalType;
Brian Salomond1eaf492017-05-18 10:02:08 -04001234 if (!this->glCaps().getTexImageFormats(texConfig, dataConfig, &internalFormat, &externalFormat,
1235 &externalType)) {
bsalomon76148af2016-01-12 11:13:47 -08001236 return false;
1237 }
brianosman81a84852016-09-12 09:05:14 -07001238 // TexStorage requires a sized format, and internalFormat may or may not be
Brian Salomond1eaf492017-05-18 10:02:08 -04001239 GrGLenum internalFormatForTexStorage = this->glCaps().configSizedInternalFormat(texConfig);
brianosman81a84852016-09-12 09:05:14 -07001240
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001241 /*
bsalomon5b30c6f2015-12-17 14:17:34 -08001242 * Check whether to allocate a temporary buffer for flipping y or
bsalomon@google.com6f379512011-11-16 20:36:03 +00001243 * because our srcData has extra bytes past each row. If so, we need
1244 * to trim those off here, since GL ES may not let us specify
1245 * GL_UNPACK_ROW_LENGTH.
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001246 */
bsalomon@google.com6f379512011-11-16 20:36:03 +00001247 bool restoreGLRowLength = false;
cblume55f2d2d2016-02-26 13:20:48 -08001248
1249 // in case we need a temporary, trimmed copy of the src pixels
1250 SkAutoSMalloc<128 * 128> tempStorage;
1251
Greg Daniel0fc4d2d2017-10-12 11:23:36 -04001252 if (mipMapsStatus) {
1253 *mipMapsStatus = GrMipMapsStatus::kValid;
Greg Daniel834f1202017-10-09 15:06:20 -04001254 }
1255
Robert Phillips931f7512017-11-02 12:57:18 -04001256 const bool usesMips = mipLevelCount > 1;
1257
cblume55f2d2d2016-02-26 13:20:48 -08001258 // find the combined size of all the mip levels and the relative offset of
1259 // each into the collective buffer
Robert Phillips931f7512017-11-02 12:57:18 -04001260 bool willNeedData = false;
Greg Daniel55afd6d2017-09-29 09:32:44 -04001261 size_t combinedBufferSize = 0;
1262 SkTArray<size_t> individualMipOffsets(mipLevelCount);
Robert Phillips590533f2017-07-11 14:22:35 -04001263 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
Greg Daniel55afd6d2017-09-29 09:32:44 -04001264 if (texelsShallowCopy[currentMipLevel].fPixels) {
1265 int twoToTheMipLevel = 1 << currentMipLevel;
1266 int currentWidth = SkTMax(1, width / twoToTheMipLevel);
1267 int currentHeight = SkTMax(1, height / twoToTheMipLevel);
Robert Phillips931f7512017-11-02 12:57:18 -04001268 const size_t trimRowBytes = currentWidth * bpp;
1269 const size_t trimmedSize = trimRowBytes * currentHeight;
1270
1271 const size_t rowBytes = texelsShallowCopy[currentMipLevel].fRowBytes
1272 ? texelsShallowCopy[currentMipLevel].fRowBytes
1273 : trimRowBytes;
1274
Brian Salomona9b04b92018-06-01 15:04:28 -04001275 if (((!caps.unpackRowLengthSupport() || usesMips) && trimRowBytes != rowBytes)) {
Robert Phillips931f7512017-11-02 12:57:18 -04001276 willNeedData = true;
1277 }
1278
Greg Daniel55afd6d2017-09-29 09:32:44 -04001279 individualMipOffsets.push_back(combinedBufferSize);
1280 combinedBufferSize += trimmedSize;
1281 } else {
Greg Daniel0fc4d2d2017-10-12 11:23:36 -04001282 if (mipMapsStatus) {
1283 *mipMapsStatus = GrMipMapsStatus::kDirty;
Greg Daniel834f1202017-10-09 15:06:20 -04001284 }
Greg Daniel55afd6d2017-09-29 09:32:44 -04001285 individualMipOffsets.push_back(0);
1286 }
Greg Daniel0fc4d2d2017-10-12 11:23:36 -04001287 }
1288 if (mipMapsStatus && mipLevelCount <= 1) {
1289 *mipMapsStatus = GrMipMapsStatus::kNotAllocated;
cblume55f2d2d2016-02-26 13:20:48 -08001290 }
Robert Phillips931f7512017-11-02 12:57:18 -04001291 char* buffer = nullptr;
1292 if (willNeedData) {
1293 buffer = (char*)tempStorage.reset(combinedBufferSize);
1294 }
cblume55f2d2d2016-02-26 13:20:48 -08001295
Robert Phillips590533f2017-07-11 14:22:35 -04001296 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
Greg Daniel55afd6d2017-09-29 09:32:44 -04001297 if (!texelsShallowCopy[currentMipLevel].fPixels) {
1298 continue;
1299 }
cblume55f2d2d2016-02-26 13:20:48 -08001300 int twoToTheMipLevel = 1 << currentMipLevel;
1301 int currentWidth = SkTMax(1, width / twoToTheMipLevel);
1302 int currentHeight = SkTMax(1, height / twoToTheMipLevel);
1303 const size_t trimRowBytes = currentWidth * bpp;
1304
1305 /*
1306 * check whether to allocate a temporary buffer for flipping y or
1307 * because our srcData has extra bytes past each row. If so, we need
1308 * to trim those off here, since GL ES may not let us specify
1309 * GL_UNPACK_ROW_LENGTH.
1310 */
1311 restoreGLRowLength = false;
1312
Greg Daniel55afd6d2017-09-29 09:32:44 -04001313 const size_t rowBytes = texelsShallowCopy[currentMipLevel].fRowBytes
1314 ? texelsShallowCopy[currentMipLevel].fRowBytes
1315 : trimRowBytes;
Greg Daniel660cc992017-06-26 14:55:05 -04001316
ericrk154349b2016-05-10 14:36:53 -07001317 // TODO: This optimization should be enabled with or without mips.
1318 // For use with mips, we must set GR_GL_UNPACK_ROW_LENGTH once per
1319 // mip level, before calling glTexImage2D.
Brian Salomona9b04b92018-06-01 15:04:28 -04001320 if (caps.unpackRowLengthSupport() && !usesMips) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001321 // can't use this for flipping, only non-neg values allowed. :(
1322 if (rowBytes != trimRowBytes) {
1323 GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
cblume55f2d2d2016-02-26 13:20:48 -08001324 GR_GL_CALL(interface, PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength));
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001325 restoreGLRowLength = true;
1326 }
Brian Salomona9b04b92018-06-01 15:04:28 -04001327 } else if (trimRowBytes != rowBytes) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -04001328 // copy data into our new storage, skipping the trailing bytes
1329 const char* src = (const char*)texelsShallowCopy[currentMipLevel].fPixels;
Greg Daniel55afd6d2017-09-29 09:32:44 -04001330 char* dst = buffer + individualMipOffsets[currentMipLevel];
Brian Salomona6948702018-06-01 15:33:20 -04001331 SkRectMemcpy(dst, trimRowBytes, src, rowBytes, trimRowBytes, currentHeight);
Jim Van Verth2e5eaf02017-06-21 15:55:46 -04001332 // now point data to our copied version
1333 texelsShallowCopy[currentMipLevel].fPixels = buffer +
Greg Daniel55afd6d2017-09-29 09:32:44 -04001334 individualMipOffsets[currentMipLevel];
Jim Van Verth2e5eaf02017-06-21 15:55:46 -04001335 texelsShallowCopy[currentMipLevel].fRowBytes = trimRowBytes;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001336 }
bsalomone699d0c2016-03-09 06:25:15 -08001337 }
1338
Robert Phillips590533f2017-07-11 14:22:35 -04001339 if (mipLevelCount) {
Brian Salomond1eaf492017-05-18 10:02:08 -04001340 GR_GL_CALL(interface, PixelStorei(GR_GL_UNPACK_ALIGNMENT, config_alignment(texConfig)));
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001341 }
cblume55f2d2d2016-02-26 13:20:48 -08001342
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001343 bool succeeded = true;
Brian Salomond1eaf492017-05-18 10:02:08 -04001344 if (kNewTexture_UploadType == uploadType) {
1345 if (0 == left && 0 == top && texWidth == width && texHeight == height) {
Robert Phillips92de6312017-05-23 07:43:48 -04001346 succeeded = allocate_and_populate_texture(
Brian Salomond1eaf492017-05-18 10:02:08 -04001347 texConfig, *interface, caps, target, internalFormat,
Robert Phillips590533f2017-07-11 14:22:35 -04001348 internalFormatForTexStorage, externalFormat, externalType,
1349 texelsShallowCopy, mipLevelCount, width, height);
Brian Salomond1eaf492017-05-18 10:02:08 -04001350 } else {
1351 succeeded = false;
1352 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001353 } else {
Robert Phillips590533f2017-07-11 14:22:35 -04001354 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
Greg Daniel55afd6d2017-09-29 09:32:44 -04001355 if (!texelsShallowCopy[currentMipLevel].fPixels) {
1356 continue;
1357 }
cblume55f2d2d2016-02-26 13:20:48 -08001358 int twoToTheMipLevel = 1 << currentMipLevel;
1359 int currentWidth = SkTMax(1, width / twoToTheMipLevel);
1360 int currentHeight = SkTMax(1, height / twoToTheMipLevel);
cblume55f2d2d2016-02-26 13:20:48 -08001361
1362 GL_CALL(TexSubImage2D(target,
1363 currentMipLevel,
1364 left, top,
1365 currentWidth,
1366 currentHeight,
1367 externalFormat, externalType,
1368 texelsShallowCopy[currentMipLevel].fPixels));
1369 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00001370 }
1371
Brian Salomona9b04b92018-06-01 15:04:28 -04001372 restore_pixelstore_state(*interface, caps, restoreGLRowLength);
cblume55f2d2d2016-02-26 13:20:48 -08001373
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001374 return succeeded;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001375}
1376
Jim Van Verth1676cb92019-01-15 13:24:45 -05001377bool GrGLGpu::uploadCompressedTexData(GrPixelConfig texConfig, int texWidth, int texHeight,
1378 GrGLenum target, GrPixelConfig dataConfig,
1379 const GrMipLevel texels[], int mipLevelCount,
1380 GrMipMapsStatus* mipMapsStatus) {
1381 SkASSERT(this->caps()->isConfigTexturable(texConfig));
1382
1383 const GrGLInterface* interface = this->glInterface();
1384 const GrGLCaps& caps = this->glCaps();
1385
1386 // We only need the internal format for compressed 2D textures.
1387 GrGLenum internalFormat;
1388 if (!caps.getCompressedTexImageFormats(texConfig, &internalFormat)) {
1389 return false;
1390 }
1391
Greg Kaiser73bfb892019-02-11 09:03:41 -08001392 if (mipMapsStatus) {
1393 if (mipLevelCount <= 1) {
1394 *mipMapsStatus = GrMipMapsStatus::kNotAllocated;
1395 } else {
1396 *mipMapsStatus = GrMipMapsStatus::kValid;
1397 }
Jim Van Verth1676cb92019-01-15 13:24:45 -05001398 }
1399
1400 return allocate_and_populate_compressed_texture(texConfig, *interface, caps, target,
1401 internalFormat, texels, mipLevelCount,
1402 texWidth, texHeight);
Jim Van Verth1676cb92019-01-15 13:24:45 -05001403}
1404
bsalomon424cc262015-05-22 10:37:30 -07001405static bool renderbuffer_storage_msaa(const GrGLContext& ctx,
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001406 int sampleCount,
1407 GrGLenum format,
1408 int width, int height) {
robertphillips@google.com6177e692013-02-28 20:16:25 +00001409 CLEAR_ERROR_BEFORE_ALLOC(ctx.interface());
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +00001410 SkASSERT(GrGLCaps::kNone_MSFBOType != ctx.caps()->msFBOType());
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +00001411 switch (ctx.caps()->msFBOType()) {
Brian Salomon00731b42016-10-14 11:30:51 -04001412 case GrGLCaps::kStandard_MSFBOType:
vbuzinovdded6962015-06-12 08:59:45 -07001413 case GrGLCaps::kMixedSamples_MSFBOType:
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001414 GL_ALLOC_CALL(ctx.interface(),
1415 RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
1416 sampleCount,
1417 format,
1418 width, height));
1419 break;
1420 case GrGLCaps::kES_Apple_MSFBOType:
1421 GL_ALLOC_CALL(ctx.interface(),
1422 RenderbufferStorageMultisampleES2APPLE(GR_GL_RENDERBUFFER,
1423 sampleCount,
1424 format,
1425 width, height));
1426 break;
1427 case GrGLCaps::kES_EXT_MsToTexture_MSFBOType:
1428 case GrGLCaps::kES_IMG_MsToTexture_MSFBOType:
1429 GL_ALLOC_CALL(ctx.interface(),
1430 RenderbufferStorageMultisampleES2EXT(GR_GL_RENDERBUFFER,
1431 sampleCount,
1432 format,
1433 width, height));
1434 break;
1435 case GrGLCaps::kNone_MSFBOType:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -04001436 SK_ABORT("Shouldn't be here if we don't support multisampled renderbuffers.");
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001437 break;
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001438 }
Brian Salomon9251d4e2015-03-17 16:03:19 -04001439 return (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctx.interface()));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001440}
1441
egdanielb0e1be22015-04-22 13:27:39 -07001442bool GrGLGpu::createRenderTargetObjects(const GrSurfaceDesc& desc,
bsalomon091f60c2015-11-10 11:54:56 -08001443 const GrGLTextureInfo& texInfo,
bsalomonb15b4c12014-10-29 12:41:57 -07001444 GrGLRenderTarget::IDDesc* idDesc) {
1445 idDesc->fMSColorRenderbufferID = 0;
egdanield803f272015-03-18 13:01:52 -07001446 idDesc->fRTFBOID = 0;
kkinnunen2e6055b2016-04-22 01:48:29 -07001447 idDesc->fRTFBOOwnership = GrBackendObjectOwnership::kOwned;
egdanield803f272015-03-18 13:01:52 -07001448 idDesc->fTexFBOID = 0;
robertphillips76948d42016-05-04 12:47:41 -07001449 SkASSERT((GrGLCaps::kMixedSamples_MSFBOType == this->glCaps().msFBOType()) ==
1450 this->caps()->usesMixedSamples());
Brian Salomonbdecacf2018-02-02 20:32:49 -05001451 idDesc->fIsMixedSampled = desc.fSampleCnt > 1 && this->caps()->usesMixedSamples();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001452
1453 GrGLenum status;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001454
bsalomona11e5fc2015-12-18 07:59:41 -08001455 GrGLenum colorRenderbufferFormat = 0; // suppress warning
bsalomon@google.comab15d612011-08-09 12:57:56 +00001456
Brian Salomonbdecacf2018-02-02 20:32:49 -05001457 if (desc.fSampleCnt > 1 && GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.com347c3822013-05-01 20:10:01 +00001458 goto FAILED;
1459 }
1460
egdanield803f272015-03-18 13:01:52 -07001461 GL_CALL(GenFramebuffers(1, &idDesc->fTexFBOID));
1462 if (!idDesc->fTexFBOID) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001463 goto FAILED;
1464 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001465
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001466 // If we are using multisampling we will create two FBOS. We render to one and then resolve to
1467 // the texture bound to the other. The exception is the IMG multisample extension. With this
1468 // extension the texture is multisampled when rendered to and then auto-resolves it when it is
1469 // rendered from.
Brian Salomonbdecacf2018-02-02 20:32:49 -05001470 if (desc.fSampleCnt > 1 && this->glCaps().usesMSAARenderBuffers()) {
egdanield803f272015-03-18 13:01:52 -07001471 GL_CALL(GenFramebuffers(1, &idDesc->fRTFBOID));
bsalomonb15b4c12014-10-29 12:41:57 -07001472 GL_CALL(GenRenderbuffers(1, &idDesc->fMSColorRenderbufferID));
egdanield803f272015-03-18 13:01:52 -07001473 if (!idDesc->fRTFBOID ||
bsalomona11e5fc2015-12-18 07:59:41 -08001474 !idDesc->fMSColorRenderbufferID) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001475 goto FAILED;
1476 }
Brian Salomon93348dd2018-08-29 12:56:23 -04001477 this->glCaps().getRenderbufferFormat(desc.fConfig, &colorRenderbufferFormat);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001478 } else {
egdanield803f272015-03-18 13:01:52 -07001479 idDesc->fRTFBOID = idDesc->fTexFBOID;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001480 }
1481
egdanield803f272015-03-18 13:01:52 -07001482 // below here we may bind the FBO
Robert Phillips294870f2016-11-11 12:38:40 -05001483 fHWBoundRenderTargetUniqueID.makeInvalid();
egdanield803f272015-03-18 13:01:52 -07001484 if (idDesc->fRTFBOID != idDesc->fTexFBOID) {
Brian Salomonbdecacf2018-02-02 20:32:49 -05001485 SkASSERT(desc.fSampleCnt > 1);
bsalomonb15b4c12014-10-29 12:41:57 -07001486 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, idDesc->fMSColorRenderbufferID));
bsalomon424cc262015-05-22 10:37:30 -07001487 if (!renderbuffer_storage_msaa(*fGLContext,
bsalomonb15b4c12014-10-29 12:41:57 -07001488 desc.fSampleCnt,
bsalomona11e5fc2015-12-18 07:59:41 -08001489 colorRenderbufferFormat,
bsalomonb15b4c12014-10-29 12:41:57 -07001490 desc.fWidth, desc.fHeight)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001491 goto FAILED;
1492 }
Adrienne Walker4ee88512018-05-17 11:37:14 -07001493 this->bindFramebuffer(GR_GL_FRAMEBUFFER, idDesc->fRTFBOID);
egdanield803f272015-03-18 13:01:52 -07001494 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon10528f12015-10-14 12:54:52 -07001495 GR_GL_COLOR_ATTACHMENT0,
1496 GR_GL_RENDERBUFFER,
1497 idDesc->fMSColorRenderbufferID));
Brian Salomon0ec981b2017-05-15 13:48:50 -04001498 if (!this->glCaps().isConfigVerifiedColorAttachment(desc.fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001499 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1500 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
1501 goto FAILED;
1502 }
bsalomon424cc262015-05-22 10:37:30 -07001503 fGLContext->caps()->markConfigAsValidColorAttachment(desc.fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001504 }
1505 }
Adrienne Walker4ee88512018-05-17 11:37:14 -07001506 this->bindFramebuffer(GR_GL_FRAMEBUFFER, idDesc->fTexFBOID);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001507
Brian Salomonbdecacf2018-02-02 20:32:49 -05001508 if (this->glCaps().usesImplicitMSAAResolve() && desc.fSampleCnt > 1) {
egdanield803f272015-03-18 13:01:52 -07001509 GL_CALL(FramebufferTexture2DMultisample(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001510 GR_GL_COLOR_ATTACHMENT0,
bsalomon091f60c2015-11-10 11:54:56 -08001511 texInfo.fTarget,
1512 texInfo.fID, 0, desc.fSampleCnt));
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001513 } else {
egdanield803f272015-03-18 13:01:52 -07001514 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001515 GR_GL_COLOR_ATTACHMENT0,
bsalomon091f60c2015-11-10 11:54:56 -08001516 texInfo.fTarget,
1517 texInfo.fID, 0));
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001518 }
Brian Salomon0ec981b2017-05-15 13:48:50 -04001519 if (!this->glCaps().isConfigVerifiedColorAttachment(desc.fConfig)) {
egdanield803f272015-03-18 13:01:52 -07001520 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001521 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
1522 goto FAILED;
1523 }
bsalomon424cc262015-05-22 10:37:30 -07001524 fGLContext->caps()->markConfigAsValidColorAttachment(desc.fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001525 }
1526
1527 return true;
1528
1529FAILED:
bsalomonb15b4c12014-10-29 12:41:57 -07001530 if (idDesc->fMSColorRenderbufferID) {
1531 GL_CALL(DeleteRenderbuffers(1, &idDesc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001532 }
egdanield803f272015-03-18 13:01:52 -07001533 if (idDesc->fRTFBOID != idDesc->fTexFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -07001534 this->deleteFramebuffer(idDesc->fRTFBOID);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001535 }
egdanield803f272015-03-18 13:01:52 -07001536 if (idDesc->fTexFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -07001537 this->deleteFramebuffer(idDesc->fTexFBOID);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001538 }
1539 return false;
1540}
1541
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001542// good to set a break-point here to know when createTexture fails
Robert Phillips67d52cf2017-06-05 13:38:13 -04001543static sk_sp<GrTexture> return_null_texture() {
mtklein@google.com330313a2013-08-22 15:37:26 +00001544// SkDEBUGFAIL("null texture");
halcanary96fcdcc2015-08-27 07:41:13 -07001545 return nullptr;
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001546}
1547
Brian Salomondc829942018-10-23 16:07:24 -04001548static GrGLTexture::SamplerParams set_initial_texture_params(const GrGLInterface* interface,
1549 const GrGLTextureInfo& info) {
cblume55f2d2d2016-02-26 13:20:48 -08001550 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
1551 // drivers have a bug where an FBO won't be complete if it includes a
1552 // texture that is not mipmap complete (considering the filter in use).
Brian Salomondc829942018-10-23 16:07:24 -04001553 GrGLTexture::SamplerParams params;
1554 params.fMinFilter = GR_GL_NEAREST;
1555 params.fMagFilter = GR_GL_NEAREST;
1556 params.fWrapS = GR_GL_CLAMP_TO_EDGE;
1557 params.fWrapT = GR_GL_CLAMP_TO_EDGE;
1558 GR_GL_CALL(interface, TexParameteri(info.fTarget, GR_GL_TEXTURE_MAG_FILTER, params.fMagFilter));
1559 GR_GL_CALL(interface, TexParameteri(info.fTarget, GR_GL_TEXTURE_MIN_FILTER, params.fMinFilter));
1560 GR_GL_CALL(interface, TexParameteri(info.fTarget, GR_GL_TEXTURE_WRAP_S, params.fWrapS));
1561 GR_GL_CALL(interface, TexParameteri(info.fTarget, GR_GL_TEXTURE_WRAP_T, params.fWrapT));
1562 return params;
cblume55f2d2d2016-02-26 13:20:48 -08001563}
1564
Robert Phillips67d52cf2017-06-05 13:38:13 -04001565sk_sp<GrTexture> GrGLGpu::onCreateTexture(const GrSurfaceDesc& desc,
1566 SkBudgeted budgeted,
Robert Phillips590533f2017-07-11 14:22:35 -04001567 const GrMipLevel texels[],
1568 int mipLevelCount) {
bsalomon@google.com8a70eef2013-03-19 13:58:55 +00001569 // We fail if the MSAA was requested and is not available.
Brian Salomonbdecacf2018-02-02 20:32:49 -05001570 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType() && desc.fSampleCnt > 1) {
tfarina38406c82014-10-31 07:11:12 -07001571 //SkDebugf("MSAA RT requested but not supported on this platform.");
bsalomon@google.com8a70eef2013-03-19 13:58:55 +00001572 return return_null_texture();
1573 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001574
Jim Van Verth1676cb92019-01-15 13:24:45 -05001575 bool performClear = (desc.fFlags & kPerformInitialClear_GrSurfaceFlag) &&
1576 !GrPixelConfigIsCompressed(desc.fConfig);
Brian Salomond17b4a62017-05-23 16:53:47 -04001577
Robert Phillips590533f2017-07-11 14:22:35 -04001578 GrMipLevel zeroLevel;
Brian Salomond17b4a62017-05-23 16:53:47 -04001579 std::unique_ptr<uint8_t[]> zeros;
Brian Salomond17b4a62017-05-23 16:53:47 -04001580 if (performClear && !this->glCaps().clearTextureSupport() &&
Brian Salomon57111332018-02-05 15:55:54 -05001581 !this->glCaps().canConfigBeFBOColorAttachment(desc.fConfig)) {
Brian Salomond17b4a62017-05-23 16:53:47 -04001582 size_t rowSize = GrBytesPerPixel(desc.fConfig) * desc.fWidth;
1583 size_t size = rowSize * desc.fHeight;
1584 zeros.reset(new uint8_t[size]);
1585 memset(zeros.get(), 0, size);
Robert Phillips590533f2017-07-11 14:22:35 -04001586 zeroLevel.fPixels = zeros.get();
1587 zeroLevel.fRowBytes = 0;
1588 texels = &zeroLevel;
1589 mipLevelCount = 1;
Brian Salomond17b4a62017-05-23 16:53:47 -04001590 performClear = false;
1591 }
1592
Brian Salomond34edf32017-05-19 15:45:48 -04001593 bool isRenderTarget = SkToBool(desc.fFlags & kRenderTarget_GrSurfaceFlag);
reed@google.comac10a2d2010-12-22 21:39:39 +00001594
bsalomonb15b4c12014-10-29 12:41:57 -07001595 GrGLTexture::IDDesc idDesc;
kkinnunen2e6055b2016-04-22 01:48:29 -07001596 idDesc.fOwnership = GrBackendObjectOwnership::kOwned;
Greg Daniel0fc4d2d2017-10-12 11:23:36 -04001597 GrMipMapsStatus mipMapsStatus;
Brian Salomondc829942018-10-23 16:07:24 -04001598 GrGLTexture::SamplerParams initialTexParams;
Brian Salomon58389b92018-03-07 13:01:25 -05001599 if (!this->createTextureImpl(desc, &idDesc.fInfo, isRenderTarget, &initialTexParams, texels,
1600 mipLevelCount, &mipMapsStatus)) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001601 return return_null_texture();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001602 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001603
Robert Phillips67d52cf2017-06-05 13:38:13 -04001604 sk_sp<GrGLTexture> tex;
Brian Salomond34edf32017-05-19 15:45:48 -04001605 if (isRenderTarget) {
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001606 // unbind the texture from the texture unit before binding it to the frame buffer
bsalomon091f60c2015-11-10 11:54:56 -08001607 GL_CALL(BindTexture(idDesc.fInfo.fTarget, 0));
bsalomon5236cf42015-01-14 10:42:08 -08001608 GrGLRenderTarget::IDDesc rtIDDesc;
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001609
kkinnunen2e6055b2016-04-22 01:48:29 -07001610 if (!this->createRenderTargetObjects(desc, idDesc.fInfo, &rtIDDesc)) {
bsalomon091f60c2015-11-10 11:54:56 -08001611 GL_CALL(DeleteTextures(1, &idDesc.fInfo.fID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001612 return return_null_texture();
1613 }
Robert Phillips67d52cf2017-06-05 13:38:13 -04001614 tex = sk_make_sp<GrGLTextureRenderTarget>(this, budgeted, desc, idDesc, rtIDDesc,
Greg Daniel0fc4d2d2017-10-12 11:23:36 -04001615 mipMapsStatus);
Brian Salomon9bada542017-06-12 12:09:30 -04001616 tex->baseLevelWasBoundToFBO();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001617 } else {
Greg Daniel0fc4d2d2017-10-12 11:23:36 -04001618 tex = sk_make_sp<GrGLTexture>(this, budgeted, desc, idDesc, mipMapsStatus);
reed@google.comac10a2d2010-12-22 21:39:39 +00001619 }
Brian Salomondc829942018-10-23 16:07:24 -04001620
1621 tex->setCachedParams(&initialTexParams, tex->getCachedNonSamplerParams(),
1622 this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00001623#ifdef TRACE_TEXTURE_CREATION
tfarina38406c82014-10-31 07:11:12 -07001624 SkDebugf("--- new texture [%d] size=(%d %d) config=%d\n",
brianosmanc4459f62016-07-19 08:02:20 -07001625 idDesc.fInfo.fID, desc.fWidth, desc.fHeight, desc.fConfig);
reed@google.comac10a2d2010-12-22 21:39:39 +00001626#endif
Brian Salomond17b4a62017-05-23 16:53:47 -04001627 if (tex && performClear) {
1628 if (this->glCaps().clearTextureSupport()) {
Brian Salomond17b4a62017-05-23 16:53:47 -04001629 static constexpr uint32_t kZero = 0;
Brian Salomon57111332018-02-05 15:55:54 -05001630 GL_CALL(ClearTexImage(tex->textureID(), 0, GR_GL_RGBA, GR_GL_UNSIGNED_BYTE, &kZero));
Brian Salomond17b4a62017-05-23 16:53:47 -04001631 } else {
Brian Salomond17b4a62017-05-23 16:53:47 -04001632 GrGLIRect viewport;
Robert Phillips67d52cf2017-06-05 13:38:13 -04001633 this->bindSurfaceFBOForPixelOps(tex.get(), GR_GL_FRAMEBUFFER, &viewport,
1634 kDst_TempFBOTarget);
Brian Salomond818ebf2018-07-02 14:08:49 +00001635 this->disableScissor();
Brian Salomond17b4a62017-05-23 16:53:47 -04001636 this->disableWindowRectangles();
Brian Salomon805cc7a2019-01-28 09:52:34 -05001637 this->flushColorWrite(true);
1638 this->flushClearColor(0, 0, 0, 0);
Brian Salomond17b4a62017-05-23 16:53:47 -04001639 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
Robert Phillips67d52cf2017-06-05 13:38:13 -04001640 this->unbindTextureFBOForPixelOps(GR_GL_FRAMEBUFFER, tex.get());
Brian Salomond17b4a62017-05-23 16:53:47 -04001641 fHWBoundRenderTargetUniqueID.makeInvalid();
1642 }
1643 }
Kevin Lubickf7621cb2018-04-16 15:51:44 -04001644 return std::move(tex);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001645}
1646
1647namespace {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001648
egdaniel8dc7c3a2015-04-16 11:22:42 -07001649const GrGLuint kUnknownBitCount = GrGLStencilAttachment::kUnknownBitCount;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001650
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001651void inline get_stencil_rb_sizes(const GrGLInterface* gl,
egdaniel8dc7c3a2015-04-16 11:22:42 -07001652 GrGLStencilAttachment::Format* format) {
sugoi@google.com6ba0b0f2013-05-03 13:52:32 +00001653
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001654 // we shouldn't ever know one size and not the other
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001655 SkASSERT((kUnknownBitCount == format->fStencilBits) ==
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001656 (kUnknownBitCount == format->fTotalBits));
1657 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001658 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001659 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1660 (GrGLint*)&format->fStencilBits);
1661 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001662 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001663 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1664 (GrGLint*)&format->fTotalBits);
1665 format->fTotalBits += format->fStencilBits;
1666 } else {
1667 format->fTotalBits = format->fStencilBits;
1668 }
1669 }
1670}
1671}
1672
egdanielff1d5472015-09-10 08:37:20 -07001673int GrGLGpu::getCompatibleStencilIndex(GrPixelConfig config) {
bsalomon100b8f82015-10-28 08:37:44 -07001674 static const int kSize = 16;
Brian Salomonbdecacf2018-02-02 20:32:49 -05001675 SkASSERT(this->caps()->isConfigRenderable(config));
bsalomon30447372015-12-21 09:03:05 -08001676 if (!this->glCaps().hasStencilFormatBeenDeterminedForConfig(config)) {
1677 // Default to unsupported, set this if we find a stencil format that works.
1678 int firstWorkingStencilFormatIndex = -1;
Brian Osman91f9a2c2017-09-05 15:02:46 -04001679
egdanielff1d5472015-09-10 08:37:20 -07001680 // Create color texture
kkinnunen546eb5c2015-12-11 00:05:33 -08001681 GrGLuint colorID = 0;
egdanielff1d5472015-09-10 08:37:20 -07001682 GL_CALL(GenTextures(1, &colorID));
Brian Salomond978b902019-02-07 15:09:18 -05001683 this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, colorID);
egdanielff1d5472015-09-10 08:37:20 -07001684 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1685 GR_GL_TEXTURE_MAG_FILTER,
1686 GR_GL_NEAREST));
1687 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1688 GR_GL_TEXTURE_MIN_FILTER,
1689 GR_GL_NEAREST));
1690 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1691 GR_GL_TEXTURE_WRAP_S,
1692 GR_GL_CLAMP_TO_EDGE));
1693 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1694 GR_GL_TEXTURE_WRAP_T,
1695 GR_GL_CLAMP_TO_EDGE));
1696
bsalomon76148af2016-01-12 11:13:47 -08001697 GrGLenum internalFormat;
1698 GrGLenum externalFormat;
1699 GrGLenum externalType;
1700 if (!this->glCaps().getTexImageFormats(config, config, &internalFormat, &externalFormat,
1701 &externalType)) {
1702 return false;
1703 }
Brian Osman9b560242017-09-05 15:34:52 -04001704 this->unbindCpuToGpuXferBuffer();
egdanielff1d5472015-09-10 08:37:20 -07001705 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1706 GL_ALLOC_CALL(this->glInterface(), TexImage2D(GR_GL_TEXTURE_2D,
bsalomon926cb022015-12-17 18:15:11 -08001707 0,
bsalomon76148af2016-01-12 11:13:47 -08001708 internalFormat,
bsalomon100b8f82015-10-28 08:37:44 -07001709 kSize,
1710 kSize,
egdanielff1d5472015-09-10 08:37:20 -07001711 0,
bsalomon76148af2016-01-12 11:13:47 -08001712 externalFormat,
1713 externalType,
Ben Wagnera93a14a2017-08-28 10:34:05 -04001714 nullptr));
bsalomon30447372015-12-21 09:03:05 -08001715 if (GR_GL_NO_ERROR != CHECK_ALLOC_ERROR(this->glInterface())) {
egdanielff1d5472015-09-10 08:37:20 -07001716 GL_CALL(DeleteTextures(1, &colorID));
bsalomon30447372015-12-21 09:03:05 -08001717 return -1;
egdanielff1d5472015-09-10 08:37:20 -07001718 }
1719
1720 // unbind the texture from the texture unit before binding it to the frame buffer
1721 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, 0));
1722
1723 // Create Framebuffer
kkinnunen546eb5c2015-12-11 00:05:33 -08001724 GrGLuint fb = 0;
egdanielff1d5472015-09-10 08:37:20 -07001725 GL_CALL(GenFramebuffers(1, &fb));
Adrienne Walker4ee88512018-05-17 11:37:14 -07001726 this->bindFramebuffer(GR_GL_FRAMEBUFFER, fb);
Robert Phillips294870f2016-11-11 12:38:40 -05001727 fHWBoundRenderTargetUniqueID.makeInvalid();
egdanielff1d5472015-09-10 08:37:20 -07001728 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
1729 GR_GL_COLOR_ATTACHMENT0,
1730 GR_GL_TEXTURE_2D,
1731 colorID,
1732 0));
bsalomon30447372015-12-21 09:03:05 -08001733 GrGLuint sbRBID = 0;
1734 GL_CALL(GenRenderbuffers(1, &sbRBID));
egdanielff1d5472015-09-10 08:37:20 -07001735
1736 // look over formats till I find a compatible one
1737 int stencilFmtCnt = this->glCaps().stencilFormats().count();
bsalomon30447372015-12-21 09:03:05 -08001738 if (sbRBID) {
egdanielff1d5472015-09-10 08:37:20 -07001739 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbRBID));
bsalomon30447372015-12-21 09:03:05 -08001740 for (int i = 0; i < stencilFmtCnt && sbRBID; ++i) {
1741 const GrGLCaps::StencilFormat& sFmt = this->glCaps().stencilFormats()[i];
1742 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1743 GL_ALLOC_CALL(this->glInterface(), RenderbufferStorage(GR_GL_RENDERBUFFER,
1744 sFmt.fInternalFormat,
1745 kSize, kSize));
1746 if (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(this->glInterface())) {
egdanielff1d5472015-09-10 08:37:20 -07001747 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon30447372015-12-21 09:03:05 -08001748 GR_GL_STENCIL_ATTACHMENT,
egdanielff1d5472015-09-10 08:37:20 -07001749 GR_GL_RENDERBUFFER, sbRBID));
bsalomon30447372015-12-21 09:03:05 -08001750 if (sFmt.fPacked) {
1751 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1752 GR_GL_DEPTH_ATTACHMENT,
1753 GR_GL_RENDERBUFFER, sbRBID));
1754 } else {
1755 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1756 GR_GL_DEPTH_ATTACHMENT,
1757 GR_GL_RENDERBUFFER, 0));
1758 }
1759 GrGLenum status;
1760 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1761 if (status == GR_GL_FRAMEBUFFER_COMPLETE) {
1762 firstWorkingStencilFormatIndex = i;
1763 break;
1764 }
egdanielff1d5472015-09-10 08:37:20 -07001765 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1766 GR_GL_STENCIL_ATTACHMENT,
1767 GR_GL_RENDERBUFFER, 0));
1768 if (sFmt.fPacked) {
1769 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1770 GR_GL_DEPTH_ATTACHMENT,
1771 GR_GL_RENDERBUFFER, 0));
1772 }
egdanielff1d5472015-09-10 08:37:20 -07001773 }
1774 }
bsalomon30447372015-12-21 09:03:05 -08001775 GL_CALL(DeleteRenderbuffers(1, &sbRBID));
egdanielff1d5472015-09-10 08:37:20 -07001776 }
1777 GL_CALL(DeleteTextures(1, &colorID));
Adrienne Walker4ee88512018-05-17 11:37:14 -07001778 this->bindFramebuffer(GR_GL_FRAMEBUFFER, 0);
1779 this->deleteFramebuffer(fb);
bsalomon30447372015-12-21 09:03:05 -08001780 fGLContext->caps()->setStencilFormatIndexForConfig(config, firstWorkingStencilFormatIndex);
egdanielff1d5472015-09-10 08:37:20 -07001781 }
bsalomon30447372015-12-21 09:03:05 -08001782 return this->glCaps().getStencilFormatIndexForConfig(config);
egdanielff1d5472015-09-10 08:37:20 -07001783}
1784
Brian Salomon2a4f9832018-03-03 22:43:43 -05001785bool GrGLGpu::createTextureImpl(const GrSurfaceDesc& desc, GrGLTextureInfo* info, bool renderTarget,
Brian Salomondc829942018-10-23 16:07:24 -04001786 GrGLTexture::SamplerParams* initialTexParams,
1787 const GrMipLevel texels[], int mipLevelCount,
1788 GrMipMapsStatus* mipMapsStatus) {
erikchen9a1ed5d2016-02-10 16:32:34 -08001789 info->fID = 0;
1790 info->fTarget = GR_GL_TEXTURE_2D;
1791 GL_CALL(GenTextures(1, &(info->fID)));
1792
1793 if (!info->fID) {
1794 return false;
1795 }
1796
Brian Salomond978b902019-02-07 15:09:18 -05001797 this->bindTextureToScratchUnit(info->fTarget, info->fID);
erikchen9a1ed5d2016-02-10 16:32:34 -08001798
1799 if (renderTarget && this->glCaps().textureUsageSupport()) {
1800 // provides a hint about how this texture will be used
1801 GL_CALL(TexParameteri(info->fTarget,
1802 GR_GL_TEXTURE_USAGE,
1803 GR_GL_FRAMEBUFFER_ATTACHMENT));
1804 }
1805
cblume55f2d2d2016-02-26 13:20:48 -08001806 if (info) {
Brian Salomondc829942018-10-23 16:07:24 -04001807 *initialTexParams = set_initial_texture_params(this->glInterface(), *info);
cblume55f2d2d2016-02-26 13:20:48 -08001808 }
Brian Salomon2a4f9832018-03-03 22:43:43 -05001809
Jim Van Verth1676cb92019-01-15 13:24:45 -05001810 bool success = false;
1811 if (GrPixelConfigIsCompressed(desc.fConfig)) {
1812 SkASSERT(!renderTarget);
1813 success = this->uploadCompressedTexData(desc.fConfig, desc.fWidth, desc.fHeight,
1814 info->fTarget, desc.fConfig,
1815 texels, mipLevelCount, mipMapsStatus);
1816 } else {
1817 success = this->uploadTexData(desc.fConfig, desc.fWidth, desc.fHeight, info->fTarget,
1818 kNewTexture_UploadType, 0, 0, desc.fWidth, desc.fHeight,
1819 desc.fConfig, texels, mipLevelCount, mipMapsStatus);
1820 }
1821 if (!success) {
erikchen9a1ed5d2016-02-10 16:32:34 -08001822 GL_CALL(DeleteTextures(1, &(info->fID)));
1823 return false;
1824 }
Greg Daniele7d8da42017-12-04 11:23:19 -05001825 info->fFormat = this->glCaps().configSizedInternalFormat(desc.fConfig);
erikchen9a1ed5d2016-02-10 16:32:34 -08001826 return true;
1827}
1828
egdanielec00d942015-09-14 12:56:10 -07001829GrStencilAttachment* GrGLGpu::createStencilAttachmentForRenderTarget(const GrRenderTarget* rt,
Brian Salomond1eaf492017-05-18 10:02:08 -04001830 int width, int height) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001831 SkASSERT(width >= rt->width());
1832 SkASSERT(height >= rt->height());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001833
vbuzinovdded6962015-06-12 08:59:45 -07001834 int samples = rt->numStencilSamples();
egdaniel8dc7c3a2015-04-16 11:22:42 -07001835 GrGLStencilAttachment::IDDesc sbDesc;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001836
egdanielff1d5472015-09-10 08:37:20 -07001837 int sIdx = this->getCompatibleStencilIndex(rt->config());
bsalomon62a627b2015-12-17 09:50:47 -08001838 if (sIdx < 0) {
egdanielec00d942015-09-14 12:56:10 -07001839 return nullptr;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001840 }
egdanielff1d5472015-09-10 08:37:20 -07001841
1842 if (!sbDesc.fRenderbufferID) {
1843 GL_CALL(GenRenderbuffers(1, &sbDesc.fRenderbufferID));
1844 }
1845 if (!sbDesc.fRenderbufferID) {
egdanielec00d942015-09-14 12:56:10 -07001846 return nullptr;
egdanielff1d5472015-09-10 08:37:20 -07001847 }
1848 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbDesc.fRenderbufferID));
1849 const GrGLCaps::StencilFormat& sFmt = this->glCaps().stencilFormats()[sIdx];
1850 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1851 // we do this "if" so that we don't call the multisample
1852 // version on a GL that doesn't have an MSAA extension.
Brian Salomonbdecacf2018-02-02 20:32:49 -05001853 if (samples > 1) {
egdanielff1d5472015-09-10 08:37:20 -07001854 SkAssertResult(renderbuffer_storage_msaa(*fGLContext,
1855 samples,
1856 sFmt.fInternalFormat,
1857 width, height));
1858 } else {
1859 GL_ALLOC_CALL(this->glInterface(), RenderbufferStorage(GR_GL_RENDERBUFFER,
1860 sFmt.fInternalFormat,
1861 width, height));
Brian Salomon0ec981b2017-05-15 13:48:50 -04001862 SkASSERT(GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(this->glInterface()));
egdanielff1d5472015-09-10 08:37:20 -07001863 }
1864 fStats.incStencilAttachmentCreates();
1865 // After sized formats we attempt an unsized format and take
1866 // whatever sizes GL gives us. In that case we query for the size.
1867 GrGLStencilAttachment::Format format = sFmt;
1868 get_stencil_rb_sizes(this->glInterface(), &format);
egdanielec00d942015-09-14 12:56:10 -07001869 GrGLStencilAttachment* stencil = new GrGLStencilAttachment(this,
1870 sbDesc,
1871 width,
1872 height,
1873 samples,
1874 format);
1875 return stencil;
reed@google.comac10a2d2010-12-22 21:39:39 +00001876}
1877
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001878////////////////////////////////////////////////////////////////////////////////
1879
Brian Salomondbf70722019-02-07 11:31:24 -05001880sk_sp<GrGpuBuffer> GrGLGpu::onCreateBuffer(size_t size, GrGpuBufferType intendedType,
1881 GrAccessPattern accessPattern, const void* data) {
Brian Salomon12d22642019-01-29 14:38:50 -05001882 return GrGLBuffer::Make(this, size, intendedType, accessPattern, data);
jvanverth73063dc2015-12-03 09:15:47 -08001883}
1884
Brian Salomond818ebf2018-07-02 14:08:49 +00001885void GrGLGpu::flushScissor(const GrScissorState& scissorState,
1886 const GrGLIRect& rtViewport,
1887 GrSurfaceOrigin rtOrigin) {
1888 if (scissorState.enabled()) {
1889 GrGLIRect scissor;
1890 scissor.setRelativeTo(rtViewport, scissorState.rect(), rtOrigin);
1891 // if the scissor fully contains the viewport then we fall through and
1892 // disable the scissor test.
1893 if (!scissor.contains(rtViewport)) {
1894 if (fHWScissorSettings.fRect != scissor) {
1895 scissor.pushToGLScissor(this->glInterface());
1896 fHWScissorSettings.fRect = scissor;
1897 }
1898 if (kYes_TriState != fHWScissorSettings.fEnabled) {
1899 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1900 fHWScissorSettings.fEnabled = kYes_TriState;
bsalomon@google.coma3201942012-06-21 19:58:20 +00001901 }
1902 return;
1903 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001904 }
Brian Salomond818ebf2018-07-02 14:08:49 +00001905
1906 // See fall through note above
1907 this->disableScissor();
joshualitt77b13072014-10-27 14:51:01 -07001908}
1909
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001910void GrGLGpu::flushWindowRectangles(const GrWindowRectsState& windowState,
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001911 const GrGLRenderTarget* rt, GrSurfaceOrigin origin) {
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001912#ifndef USE_NSIGHT
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001913 typedef GrWindowRectsState::Mode Mode;
1914 SkASSERT(!windowState.enabled() || rt->renderFBOID()); // Window rects can't be used on-screen.
1915 SkASSERT(windowState.numWindows() <= this->caps()->maxWindowRectangles());
csmartdalton28341fa2016-08-17 10:00:21 -07001916
Jim Van Verth6a40abc2017-11-02 16:56:09 +00001917 if (!this->caps()->maxWindowRectangles() ||
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001918 fHWWindowRectsState.knownEqualTo(origin, rt->getViewport(), windowState)) {
csmartdalton28341fa2016-08-17 10:00:21 -07001919 return;
csmartdalton28341fa2016-08-17 10:00:21 -07001920 }
1921
csmartdalton7535f412016-08-23 06:51:00 -07001922 // This is purely a workaround for a spurious warning generated by gcc. Otherwise the above
1923 // assert would be sufficient. https://gcc.gnu.org/bugzilla/show_bug.cgi?id=5912
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001924 int numWindows = SkTMin(windowState.numWindows(), int(GrWindowRectangles::kMaxWindows));
1925 SkASSERT(windowState.numWindows() == numWindows);
csmartdalton7535f412016-08-23 06:51:00 -07001926
csmartdalton28341fa2016-08-17 10:00:21 -07001927 GrGLIRect glwindows[GrWindowRectangles::kMaxWindows];
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001928 const SkIRect* skwindows = windowState.windows().data();
csmartdalton7535f412016-08-23 06:51:00 -07001929 for (int i = 0; i < numWindows; ++i) {
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001930 glwindows[i].setRelativeTo(rt->getViewport(), skwindows[i], origin);
csmartdalton28341fa2016-08-17 10:00:21 -07001931 }
1932
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001933 GrGLenum glmode = (Mode::kExclusive == windowState.mode()) ? GR_GL_EXCLUSIVE : GR_GL_INCLUSIVE;
csmartdalton7535f412016-08-23 06:51:00 -07001934 GL_CALL(WindowRectangles(glmode, numWindows, glwindows->asInts()));
csmartdalton28341fa2016-08-17 10:00:21 -07001935
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001936 fHWWindowRectsState.set(origin, rt->getViewport(), windowState);
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001937#endif
csmartdalton28341fa2016-08-17 10:00:21 -07001938}
1939
1940void GrGLGpu::disableWindowRectangles() {
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001941#ifndef USE_NSIGHT
Jim Van Verth6a40abc2017-11-02 16:56:09 +00001942 if (!this->caps()->maxWindowRectangles() || fHWWindowRectsState.knownDisabled()) {
csmartdalton28341fa2016-08-17 10:00:21 -07001943 return;
1944 }
1945 GL_CALL(WindowRectangles(GR_GL_EXCLUSIVE, 0, nullptr));
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001946 fHWWindowRectsState.setDisabled();
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001947#endif
csmartdalton28341fa2016-08-17 10:00:21 -07001948}
1949
Brian Salomonf7232642018-09-19 08:58:08 -04001950void GrGLGpu::resolveAndGenerateMipMapsForProcessorTextures(
1951 const GrPrimitiveProcessor& primProc,
1952 const GrPipeline& pipeline,
1953 const GrTextureProxy* const primProcTextures[],
1954 int numPrimitiveProcessorTextureSets) {
Brian Salomondfec99f2018-08-02 10:40:05 -04001955 auto genLevelsIfNeeded = [this](GrTexture* tex, const GrSamplerState& sampler) {
Brian Salomon7eae3e02018-08-07 14:02:38 +00001956 SkASSERT(tex);
Brian Salomondfec99f2018-08-02 10:40:05 -04001957 if (sampler.filter() == GrSamplerState::Filter::kMipMap &&
1958 tex->texturePriv().mipMapped() == GrMipMapped::kYes &&
1959 tex->texturePriv().mipMapsAreDirty()) {
1960 SkASSERT(this->caps()->mipMapSupport());
1961 this->regenerateMipMapLevels(static_cast<GrGLTexture*>(tex));
Brian Salomonf7232642018-09-19 08:58:08 -04001962 SkASSERT(!tex->asRenderTarget() || !tex->asRenderTarget()->needsResolve());
1963 } else if (auto* rt = tex->asRenderTarget()) {
1964 if (rt->needsResolve()) {
1965 this->resolveRenderTarget(rt);
1966 }
Brian Salomondfec99f2018-08-02 10:40:05 -04001967 }
1968 };
1969
Brian Salomonf7232642018-09-19 08:58:08 -04001970 for (int set = 0, tex = 0; set < numPrimitiveProcessorTextureSets; ++set) {
1971 for (int sampler = 0; sampler < primProc.numTextureSamplers(); ++sampler, ++tex) {
1972 GrTexture* texture = primProcTextures[tex]->peekTexture();
1973 genLevelsIfNeeded(texture, primProc.textureSampler(sampler).samplerState());
1974 }
Brian Salomondfec99f2018-08-02 10:40:05 -04001975 }
1976
1977 GrFragmentProcessor::Iter iter(pipeline);
1978 while (const GrFragmentProcessor* fp = iter.next()) {
1979 for (int i = 0; i < fp->numTextureSamplers(); ++i) {
1980 const auto& textureSampler = fp->textureSampler(i);
1981 genLevelsIfNeeded(textureSampler.peekTexture(), textureSampler.samplerState());
1982 }
1983 }
1984}
1985
Robert Phillipsd0fe8752019-01-31 14:13:59 -05001986bool GrGLGpu::flushGLState(GrRenderTarget* renderTarget,
1987 GrSurfaceOrigin origin,
1988 const GrPrimitiveProcessor& primProc,
Brian Salomon49348902018-06-26 09:12:38 -04001989 const GrPipeline& pipeline,
1990 const GrPipeline::FixedDynamicState* fixedDynamicState,
Brian Salomonf7232642018-09-19 08:58:08 -04001991 const GrPipeline::DynamicStateArrays* dynamicStateArrays,
1992 int dynamicStateArraysLength,
bsalomon2eda5b32016-09-21 10:53:24 -07001993 bool willDrawPoints) {
Brian Salomonf7232642018-09-19 08:58:08 -04001994 const GrTextureProxy* const* primProcProxiesForMipRegen = nullptr;
1995 const GrTextureProxy* const* primProcProxiesToBind = nullptr;
1996 int numPrimProcTextureSets = 1; // number of texture per prim proc sampler.
1997 if (dynamicStateArrays && dynamicStateArrays->fPrimitiveProcessorTextures) {
1998 primProcProxiesForMipRegen = dynamicStateArrays->fPrimitiveProcessorTextures;
1999 numPrimProcTextureSets = dynamicStateArraysLength;
2000 } else if (fixedDynamicState && fixedDynamicState->fPrimitiveProcessorTextures) {
2001 primProcProxiesForMipRegen = fixedDynamicState->fPrimitiveProcessorTextures;
2002 primProcProxiesToBind = fixedDynamicState->fPrimitiveProcessorTextures;
Brian Salomon7eae3e02018-08-07 14:02:38 +00002003 }
Greg Daniel9a51a862018-11-30 10:18:14 -05002004
2005 SkASSERT(SkToBool(primProcProxiesForMipRegen) == SkToBool(primProc.numTextureSamplers()));
2006
Robert Phillipsd0fe8752019-01-31 14:13:59 -05002007 sk_sp<GrGLProgram> program(fProgramCache->refProgram(this, renderTarget, origin, primProc,
2008 primProcProxiesForMipRegen,
Greg Daniel9a51a862018-11-30 10:18:14 -05002009 pipeline, willDrawPoints));
2010 if (!program) {
2011 GrCapsDebugf(this->caps(), "Failed to create program!\n");
2012 return false;
2013 }
Brian Salomonf7232642018-09-19 08:58:08 -04002014 this->resolveAndGenerateMipMapsForProcessorTextures(
2015 primProc, pipeline, primProcProxiesForMipRegen, numPrimProcTextureSets);
brianosman33f6b3f2016-06-02 05:49:21 -07002016
egdaniel080e6732014-12-22 07:35:52 -08002017 GrXferProcessor::BlendInfo blendInfo;
egdaniel0e1853c2016-03-17 11:35:45 -07002018 pipeline.getXferProcessor().getBlendInfo(&blendInfo);
egdaniel080e6732014-12-22 07:35:52 -08002019
egdaniel080e6732014-12-22 07:35:52 -08002020 this->flushColorWrite(blendInfo.fWriteColor);
bsalomonbc3d0de2014-12-15 13:45:03 -08002021
Brian Salomon802cb312018-06-08 18:05:20 -04002022 this->flushProgram(std::move(program));
bsalomon1f78c0a2014-12-17 09:43:13 -08002023
Dongseong Hwang4e1f0222018-11-01 09:10:51 -07002024 // Swizzle the blend to match what the shader will output.
2025 const GrSwizzle& swizzle = this->caps()->shaderCaps()->configOutputSwizzle(
Robert Phillipsd0fe8752019-01-31 14:13:59 -05002026 renderTarget->config());
Dongseong Hwang4e1f0222018-11-01 09:10:51 -07002027 this->flushBlend(blendInfo, swizzle);
bsalomon1f78c0a2014-12-17 09:43:13 -08002028
Robert Phillipsd0fe8752019-01-31 14:13:59 -05002029 fHWProgram->updateUniformsAndTextureBindings(renderTarget, origin,
2030 primProc, pipeline, primProcProxiesToBind);
bsalomon1f78c0a2014-12-17 09:43:13 -08002031
Robert Phillipsd0fe8752019-01-31 14:13:59 -05002032 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(renderTarget);
csmartdaltonc633abb2016-11-01 08:55:55 -07002033 GrStencilSettings stencil;
2034 if (pipeline.isStencilEnabled()) {
2035 // TODO: attach stencil and create settings during render target flush.
2036 SkASSERT(glRT->renderTargetPriv().getStencilAttachment());
2037 stencil.reset(*pipeline.getUserStencil(), pipeline.hasStencilClip(),
2038 glRT->renderTargetPriv().numStencilBits());
2039 }
2040 this->flushStencil(stencil);
Brian Salomond818ebf2018-07-02 14:08:49 +00002041 if (pipeline.isScissorEnabled()) {
2042 static constexpr SkIRect kBogusScissor{0, 0, 1, 1};
2043 GrScissorState state(fixedDynamicState ? fixedDynamicState->fScissorRect : kBogusScissor);
Robert Phillipsd0fe8752019-01-31 14:13:59 -05002044 this->flushScissor(state, glRT->getViewport(), origin);
Brian Salomond818ebf2018-07-02 14:08:49 +00002045 } else {
2046 this->disableScissor();
Brian Salomon49348902018-06-26 09:12:38 -04002047 }
Robert Phillipsd0fe8752019-01-31 14:13:59 -05002048 this->flushWindowRectangles(pipeline.getWindowRectsState(), glRT, origin);
Chris Dalton4c56b032019-03-04 12:28:42 -07002049 this->flushHWAAState(glRT, pipeline.isHWAntialiasState());
bsalomonbc3d0de2014-12-15 13:45:03 -08002050
2051 // This must come after textures are flushed because a texture may need
egdanield803f272015-03-18 13:01:52 -07002052 // to be msaa-resolved (which will modify bound FBO state).
Chris Daltonc8ece3d2018-07-30 15:03:45 -06002053 this->flushRenderTarget(glRT);
bsalomonbc3d0de2014-12-15 13:45:03 -08002054
2055 return true;
2056}
2057
Brian Salomon802cb312018-06-08 18:05:20 -04002058void GrGLGpu::flushProgram(sk_sp<GrGLProgram> program) {
2059 if (!program) {
2060 fHWProgram.reset();
2061 fHWProgramID = 0;
2062 return;
2063 }
2064 SkASSERT((program == fHWProgram) == (fHWProgramID == program->programID()));
2065 if (program == fHWProgram) {
2066 return;
2067 }
2068 auto id = program->programID();
2069 SkASSERT(id);
2070 GL_CALL(UseProgram(id));
2071 fHWProgram = std::move(program);
2072 fHWProgramID = id;
2073}
2074
2075void GrGLGpu::flushProgram(GrGLuint id) {
2076 SkASSERT(id);
2077 if (fHWProgramID == id) {
2078 SkASSERT(!fHWProgram);
2079 return;
2080 }
2081 fHWProgram.reset();
2082 GL_CALL(UseProgram(id));
2083 fHWProgramID = id;
2084}
2085
2086void GrGLGpu::setupGeometry(const GrBuffer* indexBuffer,
Chris Daltonff926502017-05-03 14:36:54 -04002087 const GrBuffer* vertexBuffer,
Chris Dalton1d616352017-05-31 12:51:23 -06002088 int baseVertex,
2089 const GrBuffer* instanceBuffer,
Brian Salomon802cb312018-06-08 18:05:20 -04002090 int baseInstance,
2091 GrPrimitiveRestart enablePrimitiveRestart) {
2092 SkASSERT((enablePrimitiveRestart == GrPrimitiveRestart::kNo) || indexBuffer);
Chris Dalton27059d32018-01-23 14:06:50 -07002093
cdaltone2e71c22016-04-07 18:13:29 -07002094 GrGLAttribArrayState* attribState;
Chris Daltonff926502017-05-03 14:36:54 -04002095 if (indexBuffer) {
Brian Salomondbf70722019-02-07 11:31:24 -05002096 SkASSERT(indexBuffer->isCpuBuffer() ||
2097 !static_cast<const GrGpuBuffer*>(indexBuffer)->isMapped());
Chris Daltonff926502017-05-03 14:36:54 -04002098 attribState = fHWVertexArrayState.bindInternalVertexArray(this, indexBuffer);
cdaltone2e71c22016-04-07 18:13:29 -07002099 } else {
2100 attribState = fHWVertexArrayState.bindInternalVertexArray(this);
bsalomonbc3d0de2014-12-15 13:45:03 -08002101 }
bsalomonbc3d0de2014-12-15 13:45:03 -08002102
Brian Salomon92be2f72018-06-19 14:33:47 -04002103 int numAttribs = fHWProgram->numVertexAttributes() + fHWProgram->numInstanceAttributes();
2104 attribState->enableVertexArrays(this, numAttribs, enablePrimitiveRestart);
Chris Dalton8e45b4f2017-05-05 14:00:56 -04002105
Brian Salomon802cb312018-06-08 18:05:20 -04002106 if (int vertexStride = fHWProgram->vertexStride()) {
Brian Salomondbf70722019-02-07 11:31:24 -05002107 SkASSERT(vertexBuffer);
2108 SkASSERT(vertexBuffer->isCpuBuffer() ||
2109 !static_cast<const GrGpuBuffer*>(vertexBuffer)->isMapped());
2110 size_t bufferOffset = baseVertex * static_cast<size_t>(vertexStride);
Brian Salomon92be2f72018-06-19 14:33:47 -04002111 for (int i = 0; i < fHWProgram->numVertexAttributes(); ++i) {
2112 const auto& attrib = fHWProgram->vertexAttribute(i);
2113 static constexpr int kDivisor = 0;
Brian Osman4a3f5c82018-09-18 16:16:38 -04002114 attribState->set(this, attrib.fLocation, vertexBuffer, attrib.fCPUType, attrib.fGPUType,
2115 vertexStride, bufferOffset + attrib.fOffset, kDivisor);
Brian Salomon92be2f72018-06-19 14:33:47 -04002116 }
Chris Dalton1d616352017-05-31 12:51:23 -06002117 }
Brian Salomon802cb312018-06-08 18:05:20 -04002118 if (int instanceStride = fHWProgram->instanceStride()) {
Brian Salomondbf70722019-02-07 11:31:24 -05002119 SkASSERT(instanceBuffer);
2120 SkASSERT(instanceBuffer->isCpuBuffer() ||
2121 !static_cast<const GrGpuBuffer*>(instanceBuffer)->isMapped());
2122 size_t bufferOffset = baseInstance * static_cast<size_t>(instanceStride);
Brian Salomon92be2f72018-06-19 14:33:47 -04002123 int attribIdx = fHWProgram->numVertexAttributes();
2124 for (int i = 0; i < fHWProgram->numInstanceAttributes(); ++i, ++attribIdx) {
2125 const auto& attrib = fHWProgram->instanceAttribute(i);
2126 static constexpr int kDivisor = 1;
Brian Osman4a3f5c82018-09-18 16:16:38 -04002127 attribState->set(this, attrib.fLocation, instanceBuffer, attrib.fCPUType,
2128 attrib.fGPUType, instanceStride, bufferOffset + attrib.fOffset,
2129 kDivisor);
Brian Salomon92be2f72018-06-19 14:33:47 -04002130 }
bsalomonbc3d0de2014-12-15 13:45:03 -08002131 }
2132}
2133
Brian Salomonae64c192019-02-05 09:41:37 -05002134GrGLenum GrGLGpu::bindBuffer(GrGpuBufferType type, const GrBuffer* buffer) {
joshualitt93316b92015-10-23 09:08:08 -07002135 this->handleDirtyContext();
cdaltondeacc972016-04-06 14:26:33 -07002136
cdaltone2e71c22016-04-07 18:13:29 -07002137 // Index buffer state is tied to the vertex array.
Brian Salomonae64c192019-02-05 09:41:37 -05002138 if (GrGpuBufferType::kIndex == type) {
cdaltone2e71c22016-04-07 18:13:29 -07002139 this->bindVertexArray(0);
cdaltondeacc972016-04-06 14:26:33 -07002140 }
cdaltone2e71c22016-04-07 18:13:29 -07002141
Brian Salomonae64c192019-02-05 09:41:37 -05002142 auto* bufferState = this->hwBufferState(type);
Brian Salomondbf70722019-02-07 11:31:24 -05002143 if (buffer->isCpuBuffer()) {
Brian Salomonae64c192019-02-05 09:41:37 -05002144 if (!bufferState->fBufferZeroKnownBound) {
2145 GL_CALL(BindBuffer(bufferState->fGLTarget, 0));
2146 bufferState->fBufferZeroKnownBound = true;
2147 bufferState->fBoundBufferUniqueID.makeInvalid();
cdaltone2e71c22016-04-07 18:13:29 -07002148 }
Brian Salomondbf70722019-02-07 11:31:24 -05002149 } else if (static_cast<const GrGpuBuffer*>(buffer)->uniqueID() !=
2150 bufferState->fBoundBufferUniqueID) {
Brian Salomonae64c192019-02-05 09:41:37 -05002151 const GrGLBuffer* glBuffer = static_cast<const GrGLBuffer*>(buffer);
2152 GL_CALL(BindBuffer(bufferState->fGLTarget, glBuffer->bufferID()));
2153 bufferState->fBufferZeroKnownBound = false;
2154 bufferState->fBoundBufferUniqueID = glBuffer->uniqueID();
cdaltone2e71c22016-04-07 18:13:29 -07002155 }
2156
Brian Salomonae64c192019-02-05 09:41:37 -05002157 return bufferState->fGLTarget;
joshualitt93316b92015-10-23 09:08:08 -07002158}
Brian Salomond818ebf2018-07-02 14:08:49 +00002159void GrGLGpu::disableScissor() {
2160 if (kNo_TriState != fHWScissorSettings.fEnabled) {
2161 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
2162 fHWScissorSettings.fEnabled = kNo_TriState;
2163 return;
2164 }
2165}
2166
Brian Osman9a9baae2018-11-05 15:06:26 -05002167void GrGLGpu::clear(const GrFixedClip& clip, const SkPMColor4f& color,
Robert Phillips19e51dc2017-08-09 09:30:51 -04002168 GrRenderTarget* target, GrSurfaceOrigin origin) {
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00002169 // parent class should never let us get here with no RT
bsalomon49f085d2014-09-05 13:34:00 -07002170 SkASSERT(target);
Michael Ludwigc39d0c82019-01-15 10:03:43 -05002171 SkASSERT(!this->caps()->performColorClearsAsDraws());
2172 SkASSERT(!clip.scissorEnabled() || !this->caps()->performPartialClearsAsDraws());
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00002173
Brian Salomon43f8bf02017-10-18 08:33:29 -04002174 this->handleDirtyContext();
bsalomon@google.com74b98712011-11-11 19:46:16 +00002175
Brian Salomon43f8bf02017-10-18 08:33:29 -04002176 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
2177
Brian Salomond818ebf2018-07-02 14:08:49 +00002178 if (clip.scissorEnabled()) {
Chris Daltonc8ece3d2018-07-30 15:03:45 -06002179 this->flushRenderTarget(glRT, origin, clip.scissorRect());
Brian Salomon1fabd512018-02-09 09:54:25 -05002180 } else {
Chris Daltonc8ece3d2018-07-30 15:03:45 -06002181 this->flushRenderTarget(glRT);
Brian Salomon1fabd512018-02-09 09:54:25 -05002182 }
Brian Salomond818ebf2018-07-02 14:08:49 +00002183 this->flushScissor(clip.scissorState(), glRT->getViewport(), origin);
Brian Salomon43f8bf02017-10-18 08:33:29 -04002184 this->flushWindowRectangles(clip.windowRectsState(), glRT, origin);
Brian Salomon805cc7a2019-01-28 09:52:34 -05002185 this->flushColorWrite(true);
Brian Salomon028a9a52017-05-11 11:39:08 -04002186
Brian Osman9a9baae2018-11-05 15:06:26 -05002187 GrGLfloat r = color.fR, g = color.fG, b = color.fB, a = color.fA;
Eric Karlaeaf22b2017-05-18 15:08:09 -07002188 if (this->glCaps().clearToBoundaryValuesIsBroken() &&
2189 (1 == r || 0 == r) && (1 == g || 0 == g) && (1 == b || 0 == b) && (1 == a || 0 == a)) {
Eric Karlaeaf22b2017-05-18 15:08:09 -07002190 static const GrGLfloat safeAlpha1 = nextafter(1.f, 2.f);
2191 static const GrGLfloat safeAlpha0 = nextafter(0.f, -1.f);
Eric Karlaeaf22b2017-05-18 15:08:09 -07002192 a = (1 == a) ? safeAlpha1 : safeAlpha0;
Brian Salomon028a9a52017-05-11 11:39:08 -04002193 }
Brian Salomon805cc7a2019-01-28 09:52:34 -05002194 this->flushClearColor(r, g, b, a);
2195
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002196 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00002197}
2198
Robert Phillips95214472017-08-08 18:00:03 -04002199void GrGLGpu::clearStencil(GrRenderTarget* target, int clearValue) {
Michael Ludwigc39d0c82019-01-15 10:03:43 -05002200 SkASSERT(!this->caps()->performStencilClearsAsDraws());
2201
Robert Phillips95214472017-08-08 18:00:03 -04002202 if (!target) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00002203 return;
2204 }
Robert Phillipscb2e2352017-08-30 16:44:40 -04002205
2206 GrStencilAttachment* sb = target->renderTargetPriv().getStencilAttachment();
2207 // this should only be called internally when we know we have a
2208 // stencil buffer.
2209 SkASSERT(sb);
2210
bsalomonb0bd4f62014-09-03 07:19:50 -07002211 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
Brian Salomon1fabd512018-02-09 09:54:25 -05002212 this->flushRenderTargetNoColorWrites(glRT);
bsalomon@google.com8295dc12011-05-02 12:53:34 +00002213
Brian Salomond818ebf2018-07-02 14:08:49 +00002214 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07002215 this->disableWindowRectangles();
robertphillips@google.com730ebe52012-04-16 16:33:13 +00002216
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002217 GL_CALL(StencilMask(0xffffffff));
Robert Phillips95214472017-08-08 18:00:03 -04002218 GL_CALL(ClearStencil(clearValue));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002219 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00002220 fHWStencilSettings.invalidate();
Robert Phillipscb2e2352017-08-30 16:44:40 -04002221 if (!clearValue) {
2222 sb->cleared();
2223 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002224}
2225
csmartdalton29df7602016-08-31 11:55:52 -07002226void GrGLGpu::clearStencilClip(const GrFixedClip& clip,
2227 bool insideStencilMask,
Robert Phillips19e51dc2017-08-09 09:30:51 -04002228 GrRenderTarget* target, GrSurfaceOrigin origin) {
bsalomon49f085d2014-09-05 13:34:00 -07002229 SkASSERT(target);
Michael Ludwigc39d0c82019-01-15 10:03:43 -05002230 SkASSERT(!this->caps()->performStencilClearsAsDraws());
egdaniel9cb63402016-06-23 08:37:05 -07002231 this->handleDirtyContext();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00002232
egdaniel8dc7c3a2015-04-16 11:22:42 -07002233 GrStencilAttachment* sb = target->renderTargetPriv().getStencilAttachment();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00002234 // this should only be called internally when we know we have a
2235 // stencil buffer.
bsalomon6bc1b5f2015-02-23 09:06:38 -08002236 SkASSERT(sb);
2237 GrGLint stencilBitCount = sb->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002238#if 0
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002239 SkASSERT(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00002240 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002241#else
2242 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00002243 // ANGLE a partial stencil mask will cause clears to be
Robert Phillipsf2361d22016-10-25 14:20:06 -04002244 // turned into draws. Our contract on GrOpList says that
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002245 // changing the clip between stencil passes may or may not
2246 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00002247 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002248#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002249 GrGLint value;
csmartdalton29df7602016-08-31 11:55:52 -07002250 if (insideStencilMask) {
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002251 value = (1 << (stencilBitCount - 1));
2252 } else {
2253 value = 0;
2254 }
bsalomonb0bd4f62014-09-03 07:19:50 -07002255 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
Brian Salomon1fabd512018-02-09 09:54:25 -05002256 this->flushRenderTargetNoColorWrites(glRT);
bsalomon@google.coma3201942012-06-21 19:58:20 +00002257
Brian Salomond818ebf2018-07-02 14:08:49 +00002258 this->flushScissor(clip.scissorState(), glRT->getViewport(), origin);
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002259 this->flushWindowRectangles(clip.windowRectsState(), glRT, origin);
bsalomon@google.coma3201942012-06-21 19:58:20 +00002260
caryclark@google.comcf6285b2012-06-06 12:09:01 +00002261 GL_CALL(StencilMask((uint32_t) clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002262 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002263 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00002264 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00002265}
2266
bsalomon1aa20292016-01-22 08:16:09 -08002267bool GrGLGpu::readPixelsSupported(GrRenderTarget* target, GrPixelConfig readConfig) {
Brian Salomon625cd9e2016-12-15 09:35:19 -05002268#ifdef SK_BUILD_FOR_MAC
2269 // Chromium may ask us to read back from locked IOSurfaces. Calling the command buffer's
2270 // glGetIntegerv() with GL_IMPLEMENTATION_COLOR_READ_FORMAT/_TYPE causes the command buffer
2271 // to make a call to check the framebuffer status which can hang the driver. So in Mac Chromium
2272 // we always use a temporary surface to test for read pixels support.
2273 // https://www.crbug.com/662802
2274 if (this->glContext().driver() == kChromium_GrGLDriver) {
2275 return this->readPixelsSupported(target->config(), readConfig);
2276 }
2277#endif
bsalomon1aa20292016-01-22 08:16:09 -08002278 auto bindRenderTarget = [this, target]() -> bool {
Brian Salomon1fabd512018-02-09 09:54:25 -05002279 this->flushRenderTargetNoColorWrites(static_cast<GrGLRenderTarget*>(target));
bsalomon1aa20292016-01-22 08:16:09 -08002280 return true;
2281 };
bsalomon2c3db322016-11-08 13:26:24 -08002282 auto unbindRenderTarget = []{};
bsalomon1aa20292016-01-22 08:16:09 -08002283 auto getIntegerv = [this](GrGLenum query, GrGLint* value) {
2284 GR_GL_GetIntegerv(this->glInterface(), query, value);
2285 };
2286 GrPixelConfig rtConfig = target->config();
bsalomon2c3db322016-11-08 13:26:24 -08002287 return this->glCaps().readPixelsSupported(rtConfig, readConfig, getIntegerv, bindRenderTarget,
2288 unbindRenderTarget);
bsalomon1aa20292016-01-22 08:16:09 -08002289}
2290
2291bool GrGLGpu::readPixelsSupported(GrPixelConfig rtConfig, GrPixelConfig readConfig) {
bsalomon2c3db322016-11-08 13:26:24 -08002292 sk_sp<GrTexture> temp;
2293 auto bindRenderTarget = [this, rtConfig, &temp]() -> bool {
Robert Phillips16d8ec62017-07-27 16:16:25 -04002294 GrSurfaceDesc desc;
bsalomon1aa20292016-01-22 08:16:09 -08002295 desc.fConfig = rtConfig;
2296 desc.fWidth = desc.fHeight = 16;
Brian Salomonbdecacf2018-02-02 20:32:49 -05002297 if (this->glCaps().isConfigRenderable(rtConfig)) {
bsalomon2c3db322016-11-08 13:26:24 -08002298 desc.fFlags = kRenderTarget_GrSurfaceFlag;
Robert Phillips67d52cf2017-06-05 13:38:13 -04002299 temp = this->createTexture(desc, SkBudgeted::kNo);
bsalomon2c3db322016-11-08 13:26:24 -08002300 if (!temp) {
2301 return false;
2302 }
2303 GrGLRenderTarget* glrt = static_cast<GrGLRenderTarget*>(temp->asRenderTarget());
Brian Salomon1fabd512018-02-09 09:54:25 -05002304 this->flushRenderTargetNoColorWrites(glrt);
bsalomon2c3db322016-11-08 13:26:24 -08002305 return true;
2306 } else if (this->glCaps().canConfigBeFBOColorAttachment(rtConfig)) {
Robert Phillips67d52cf2017-06-05 13:38:13 -04002307 temp = this->createTexture(desc, SkBudgeted::kNo);
bsalomon2c3db322016-11-08 13:26:24 -08002308 if (!temp) {
2309 return false;
2310 }
2311 GrGLIRect vp;
2312 this->bindSurfaceFBOForPixelOps(temp.get(), GR_GL_FRAMEBUFFER, &vp, kDst_TempFBOTarget);
Robert Phillips294870f2016-11-11 12:38:40 -05002313 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon2c3db322016-11-08 13:26:24 -08002314 return true;
bsalomon1aa20292016-01-22 08:16:09 -08002315 }
bsalomon2c3db322016-11-08 13:26:24 -08002316 return false;
2317 };
2318 auto unbindRenderTarget = [this, &temp]() {
2319 this->unbindTextureFBOForPixelOps(GR_GL_FRAMEBUFFER, temp.get());
bsalomon1aa20292016-01-22 08:16:09 -08002320 };
2321 auto getIntegerv = [this](GrGLenum query, GrGLint* value) {
2322 GR_GL_GetIntegerv(this->glInterface(), query, value);
2323 };
bsalomon2c3db322016-11-08 13:26:24 -08002324 return this->glCaps().readPixelsSupported(rtConfig, readConfig, getIntegerv, bindRenderTarget,
2325 unbindRenderTarget);
bsalomon1aa20292016-01-22 08:16:09 -08002326}
2327
2328bool GrGLGpu::readPixelsSupported(GrSurface* surfaceForConfig, GrPixelConfig readConfig) {
2329 if (GrRenderTarget* rt = surfaceForConfig->asRenderTarget()) {
2330 return this->readPixelsSupported(rt, readConfig);
2331 } else {
2332 GrPixelConfig config = surfaceForConfig->config();
2333 return this->readPixelsSupported(config, readConfig);
2334 }
2335}
2336
Brian Salomona6948702018-06-01 15:33:20 -04002337bool GrGLGpu::onReadPixels(GrSurface* surface, int left, int top, int width, int height,
2338 GrColorType dstColorType, void* buffer, size_t rowBytes) {
bsalomon6cb3cbe2015-07-30 07:34:27 -07002339 SkASSERT(surface);
bsalomon39826022015-07-23 08:07:21 -07002340
bsalomone9573312016-01-25 14:33:25 -08002341 GrGLRenderTarget* renderTarget = static_cast<GrGLRenderTarget*>(surface->asRenderTarget());
Brian Salomon71d9d842016-11-03 13:42:00 -04002342 if (!renderTarget && !this->glCaps().canConfigBeFBOColorAttachment(surface->config())) {
bsalomon6cb3cbe2015-07-30 07:34:27 -07002343 return false;
2344 }
2345
Brian Salomonc320b152018-02-20 14:05:36 -05002346 // TODO: Avoid this conversion by making GrGLCaps work with color types.
2347 auto dstAsConfig = GrColorTypeToPixelConfig(dstColorType, GrSRGBEncoded::kNo);
2348
Brian Salomonc320b152018-02-20 14:05:36 -05002349 if (!this->readPixelsSupported(surface, dstAsConfig)) {
bsalomon16921ec2015-07-30 15:34:56 -07002350 return false;
2351 }
2352
bsalomon76148af2016-01-12 11:13:47 -08002353 GrGLenum externalFormat;
2354 GrGLenum externalType;
Brian Salomonc320b152018-02-20 14:05:36 -05002355 if (!this->glCaps().getReadPixelsFormat(surface->config(), dstAsConfig, &externalFormat,
bsalomon76148af2016-01-12 11:13:47 -08002356 &externalType)) {
2357 return false;
2358 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00002359
Brian Salomon71d9d842016-11-03 13:42:00 -04002360 GrGLIRect glvp;
2361 if (renderTarget) {
2362 // resolve the render target if necessary
2363 switch (renderTarget->getResolveType()) {
2364 case GrGLRenderTarget::kCantResolve_ResolveType:
2365 return false;
2366 case GrGLRenderTarget::kAutoResolves_ResolveType:
Brian Salomon1fabd512018-02-09 09:54:25 -05002367 this->flushRenderTargetNoColorWrites(renderTarget);
Brian Salomon71d9d842016-11-03 13:42:00 -04002368 break;
2369 case GrGLRenderTarget::kCanResolve_ResolveType:
Brian Salomon1fabd512018-02-09 09:54:25 -05002370 this->onResolveRenderTarget(renderTarget);
Brian Salomon71d9d842016-11-03 13:42:00 -04002371 // we don't track the state of the READ FBO ID.
Adrienne Walker4ee88512018-05-17 11:37:14 -07002372 this->bindFramebuffer(GR_GL_READ_FRAMEBUFFER, renderTarget->textureFBOID());
Brian Salomon71d9d842016-11-03 13:42:00 -04002373 break;
2374 default:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -04002375 SK_ABORT("Unknown resolve type");
Brian Salomon71d9d842016-11-03 13:42:00 -04002376 }
2377 glvp = renderTarget->getViewport();
2378 } else {
2379 // Use a temporary FBO.
2380 this->bindSurfaceFBOForPixelOps(surface, GR_GL_FRAMEBUFFER, &glvp, kSrc_TempFBOTarget);
Robert Phillips294870f2016-11-11 12:38:40 -05002381 fHWBoundRenderTargetUniqueID.makeInvalid();
reed@google.comac10a2d2010-12-22 21:39:39 +00002382 }
2383
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00002384 // the read rect is viewport-relative
2385 GrGLIRect readRect;
Brian Salomona6948702018-06-01 15:33:20 -04002386 readRect.setRelativeTo(glvp, left, top, width, height, kTopLeft_GrSurfaceOrigin);
rmistry@google.comfbfcd562012-08-23 18:09:54 +00002387
Brian Salomonc320b152018-02-20 14:05:36 -05002388 int bytesPerPixel = GrBytesPerPixel(dstAsConfig);
bsalomon9d02b262016-02-01 12:49:30 -08002389 size_t tightRowBytes = bytesPerPixel * width;
egdaniel6d901da2015-07-30 12:02:15 -07002390
bsalomon@google.comc6980972011-11-02 19:57:21 +00002391 size_t readDstRowBytes = tightRowBytes;
2392 void* readDst = buffer;
rmistry@google.comfbfcd562012-08-23 18:09:54 +00002393
Brian Salomona6948702018-06-01 15:33:20 -04002394 // determine if GL can read using the passed rowBytes or if we need a scratch buffer.
joshualitt29f86792015-05-29 08:06:48 -07002395 SkAutoSMalloc<32 * sizeof(GrColor)> scratch;
bsalomon@google.comc6980972011-11-02 19:57:21 +00002396 if (rowBytes != tightRowBytes) {
bsalomon9d02b262016-02-01 12:49:30 -08002397 if (this->glCaps().packRowLengthSupport() && !(rowBytes % bytesPerPixel)) {
skia.committer@gmail.com4677acc2013-10-17 07:02:33 +00002398 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH,
bsalomon9d02b262016-02-01 12:49:30 -08002399 static_cast<GrGLint>(rowBytes / bytesPerPixel)));
bsalomon@google.comc6980972011-11-02 19:57:21 +00002400 readDstRowBytes = rowBytes;
2401 } else {
2402 scratch.reset(tightRowBytes * height);
2403 readDst = scratch.get();
2404 }
2405 }
Brian Salomonc320b152018-02-20 14:05:36 -05002406 GL_CALL(PixelStorei(GR_GL_PACK_ALIGNMENT, config_alignment(dstAsConfig)));
bsalomonf46a1242015-12-15 12:37:38 -08002407
Brian Osman1348ed02018-09-12 09:08:39 -04002408 bool reattachStencil = false;
2409 if (this->glCaps().detachStencilFromMSAABuffersBeforeReadPixels() &&
2410 renderTarget &&
2411 renderTarget->renderTargetPriv().getStencilAttachment() &&
2412 renderTarget->numColorSamples() > 1) {
2413 // Fix Adreno devices that won't read from MSAA framebuffers with stencil attached
2414 reattachStencil = true;
2415 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT,
2416 GR_GL_RENDERBUFFER, 0));
2417 }
2418
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002419 GL_CALL(ReadPixels(readRect.fLeft, readRect.fBottom,
2420 readRect.fWidth, readRect.fHeight,
bsalomon76148af2016-01-12 11:13:47 -08002421 externalFormat, externalType, readDst));
Brian Osman1348ed02018-09-12 09:08:39 -04002422
2423 if (reattachStencil) {
2424 GrGLStencilAttachment* stencilAttachment = static_cast<GrGLStencilAttachment*>(
2425 renderTarget->renderTargetPriv().getStencilAttachment());
2426 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT,
2427 GR_GL_RENDERBUFFER, stencilAttachment->renderbufferID()));
2428 }
2429
bsalomon@google.comc6980972011-11-02 19:57:21 +00002430 if (readDstRowBytes != tightRowBytes) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002431 SkASSERT(this->glCaps().packRowLengthSupport());
bsalomon@google.comc6980972011-11-02 19:57:21 +00002432 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
2433 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002434
Brian Salomona6948702018-06-01 15:33:20 -04002435 if (readDst != buffer) {
bsalomon9d02b262016-02-01 12:49:30 -08002436 SkASSERT(readDst != buffer);
2437 SkASSERT(rowBytes != tightRowBytes);
bsalomon@google.comc6980972011-11-02 19:57:21 +00002438 const char* src = reinterpret_cast<const char*>(readDst);
bsalomon@google.comc4364992011-11-07 15:54:49 +00002439 char* dst = reinterpret_cast<char*>(buffer);
Brian Salomona6948702018-06-01 15:33:20 -04002440 SkRectMemcpy(dst, rowBytes, src, readDstRowBytes, tightRowBytes, height);
reed@google.comac10a2d2010-12-22 21:39:39 +00002441 }
Brian Salomon71d9d842016-11-03 13:42:00 -04002442 if (!renderTarget) {
2443 this->unbindTextureFBOForPixelOps(GR_GL_FRAMEBUFFER, surface);
2444 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002445 return true;
2446}
2447
Robert Phillips5b5d84c2018-08-09 15:12:18 -04002448GrGpuRTCommandBuffer* GrGLGpu::getCommandBuffer(
Ethan Nicholas56d19a52018-10-15 11:26:20 -04002449 GrRenderTarget* rt, GrSurfaceOrigin origin, const SkRect& bounds,
Robert Phillips6b47c7d2017-08-29 07:24:09 -04002450 const GrGpuRTCommandBuffer::LoadAndStoreInfo& colorInfo,
Greg Daniel500d58b2017-08-24 15:59:33 -04002451 const GrGpuRTCommandBuffer::StencilLoadAndStoreInfo& stencilInfo) {
Robert Phillips5b5d84c2018-08-09 15:12:18 -04002452 if (!fCachedRTCommandBuffer) {
2453 fCachedRTCommandBuffer.reset(new GrGLGpuRTCommandBuffer(this));
2454 }
2455
2456 fCachedRTCommandBuffer->set(rt, origin, colorInfo, stencilInfo);
2457 return fCachedRTCommandBuffer.get();
Greg Daniel500d58b2017-08-24 15:59:33 -04002458}
2459
Robert Phillips5b5d84c2018-08-09 15:12:18 -04002460GrGpuTextureCommandBuffer* GrGLGpu::getCommandBuffer(GrTexture* texture, GrSurfaceOrigin origin) {
2461 if (!fCachedTexCommandBuffer) {
2462 fCachedTexCommandBuffer.reset(new GrGLGpuTextureCommandBuffer(this));
2463 }
2464
2465 fCachedTexCommandBuffer->set(texture, origin);
2466 return fCachedTexCommandBuffer.get();
egdaniel066df7c2016-06-08 14:02:27 -07002467}
2468
Brian Salomon1fabd512018-02-09 09:54:25 -05002469void GrGLGpu::flushRenderTarget(GrGLRenderTarget* target, GrSurfaceOrigin origin,
Chris Daltonc8ece3d2018-07-30 15:03:45 -06002470 const SkIRect& bounds) {
Brian Osman9aa30c62018-07-02 15:21:46 -04002471 this->flushRenderTargetNoColorWrites(target);
Chris Daltonc8ece3d2018-07-30 15:03:45 -06002472 this->didWriteToSurface(target, origin, &bounds);
2473}
bsalomon6ba6fa12015-03-04 11:57:37 -08002474
Chris Daltonc8ece3d2018-07-30 15:03:45 -06002475void GrGLGpu::flushRenderTarget(GrGLRenderTarget* target) {
2476 this->flushRenderTargetNoColorWrites(target);
2477 this->didWriteToSurface(target, kTopLeft_GrSurfaceOrigin, nullptr);
Brian Salomon1fabd512018-02-09 09:54:25 -05002478}
2479
Brian Osman9aa30c62018-07-02 15:21:46 -04002480void GrGLGpu::flushRenderTargetNoColorWrites(GrGLRenderTarget* target) {
Brian Salomon1fabd512018-02-09 09:54:25 -05002481 SkASSERT(target);
Robert Phillips294870f2016-11-11 12:38:40 -05002482 GrGpuResource::UniqueID rtID = target->uniqueID();
egdanield803f272015-03-18 13:01:52 -07002483 if (fHWBoundRenderTargetUniqueID != rtID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -07002484 this->bindFramebuffer(GR_GL_FRAMEBUFFER, target->renderFBOID());
egdanield803f272015-03-18 13:01:52 -07002485#ifdef SK_DEBUG
2486 // don't do this check in Chromium -- this is causing
2487 // lots of repeated command buffer flushes when the compositor is
2488 // rendering with Ganesh, which is really slow; even too slow for
2489 // Debug mode.
cdalton1acea862015-06-02 13:05:52 -07002490 if (kChromium_GrGLDriver != this->glContext().driver()) {
egdanield803f272015-03-18 13:01:52 -07002491 GrGLenum status;
2492 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
2493 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
2494 SkDebugf("GrGLGpu::flushRenderTarget glCheckFramebufferStatus %x\n", status);
2495 }
bsalomon160f24c2015-03-17 15:55:42 -07002496 }
egdanield803f272015-03-18 13:01:52 -07002497#endif
2498 fHWBoundRenderTargetUniqueID = rtID;
bsalomon083617b2016-02-12 12:10:14 -08002499 this->flushViewport(target->getViewport());
brianosman64d094d2016-03-25 06:01:59 -07002500 }
2501
brianosman35b784d2016-05-05 11:52:53 -07002502 if (this->glCaps().srgbWriteControl()) {
Brian Osman9aa30c62018-07-02 15:21:46 -04002503 this->flushFramebufferSRGB(GrPixelConfigIsSRGB(target->config()));
bsalomon5cd020f2015-03-17 12:46:56 -07002504 }
bsalomon083617b2016-02-12 12:10:14 -08002505}
bsalomona9909122016-01-23 10:41:40 -08002506
brianosman33f6b3f2016-06-02 05:49:21 -07002507void GrGLGpu::flushFramebufferSRGB(bool enable) {
2508 if (enable && kYes_TriState != fHWSRGBFramebuffer) {
2509 GL_CALL(Enable(GR_GL_FRAMEBUFFER_SRGB));
2510 fHWSRGBFramebuffer = kYes_TriState;
2511 } else if (!enable && kNo_TriState != fHWSRGBFramebuffer) {
2512 GL_CALL(Disable(GR_GL_FRAMEBUFFER_SRGB));
2513 fHWSRGBFramebuffer = kNo_TriState;
2514 }
2515}
2516
bsalomon083617b2016-02-12 12:10:14 -08002517void GrGLGpu::flushViewport(const GrGLIRect& viewport) {
2518 if (fHWViewport != viewport) {
2519 viewport.pushToGLViewport(this->glInterface());
2520 fHWViewport = viewport;
2521 }
2522}
2523
bsalomon@google.comd302f142011-03-03 13:54:13 +00002524#define SWAP_PER_DRAW 0
2525
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00002526#if SWAP_PER_DRAW
commit-bot@chromium.org43823302013-09-25 20:57:51 +00002527 #if defined(SK_BUILD_FOR_MAC)
bsalomon@google.comd302f142011-03-03 13:54:13 +00002528 #include <AGL/agl.h>
Mike Klein8f11d4d2018-01-24 12:42:55 -05002529 #elif defined(SK_BUILD_FOR_WIN)
bsalomon@google.comce7357d2012-06-25 17:49:25 +00002530 #include <gl/GL.h>
bsalomon@google.comd302f142011-03-03 13:54:13 +00002531 void SwapBuf() {
2532 DWORD procID = GetCurrentProcessId();
2533 HWND hwnd = GetTopWindow(GetDesktopWindow());
2534 while(hwnd) {
2535 DWORD wndProcID = 0;
2536 GetWindowThreadProcessId(hwnd, &wndProcID);
2537 if(wndProcID == procID) {
2538 SwapBuffers(GetDC(hwnd));
2539 }
2540 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
2541 }
2542 }
2543 #endif
2544#endif
2545
Robert Phillipsd0fe8752019-01-31 14:13:59 -05002546void GrGLGpu::draw(GrRenderTarget* renderTarget, GrSurfaceOrigin origin,
2547 const GrPrimitiveProcessor& primProc,
Brian Salomonff168d92018-06-23 15:17:27 -04002548 const GrPipeline& pipeline,
Brian Salomon49348902018-06-26 09:12:38 -04002549 const GrPipeline::FixedDynamicState* fixedDynamicState,
2550 const GrPipeline::DynamicStateArrays* dynamicStateArrays,
bsalomon2eda5b32016-09-21 10:53:24 -07002551 const GrMesh meshes[],
egdaniel9cb63402016-06-23 08:37:05 -07002552 int meshCount) {
2553 this->handleDirtyContext();
2554
bsalomon2eda5b32016-09-21 10:53:24 -07002555 bool hasPoints = false;
2556 for (int i = 0; i < meshCount; ++i) {
Chris Dalton3809bab2017-06-13 10:55:06 -06002557 if (meshes[i].primitiveType() == GrPrimitiveType::kPoints) {
bsalomon2eda5b32016-09-21 10:53:24 -07002558 hasPoints = true;
2559 break;
2560 }
2561 }
Robert Phillipsd0fe8752019-01-31 14:13:59 -05002562 if (!this->flushGLState(renderTarget, origin, primProc, pipeline, fixedDynamicState,
2563 dynamicStateArrays, meshCount, hasPoints)) {
bsalomond95263c2014-12-16 13:05:12 -08002564 return;
2565 }
ethannicholas22793252016-01-30 09:59:10 -08002566
Brian Salomonf7232642018-09-19 08:58:08 -04002567 bool dynamicScissor = false;
2568 bool dynamicPrimProcTextures = false;
2569 if (dynamicStateArrays) {
2570 dynamicScissor = pipeline.isScissorEnabled() && dynamicStateArrays->fScissorRects;
2571 dynamicPrimProcTextures = dynamicStateArrays->fPrimitiveProcessorTextures;
2572 }
2573 for (int m = 0; m < meshCount; ++m) {
Robert Phillipsd0fe8752019-01-31 14:13:59 -05002574 if (GrXferBarrierType barrierType = pipeline.xferBarrierType(renderTarget->asTexture(),
2575 *this->caps())) {
2576 this->xferBarrier(renderTarget, barrierType);
egdaniel0e1853c2016-03-17 11:35:45 -07002577 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002578
Brian Salomon49348902018-06-26 09:12:38 -04002579 if (dynamicScissor) {
Robert Phillipsd0fe8752019-01-31 14:13:59 -05002580 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(renderTarget);
Brian Salomonf7232642018-09-19 08:58:08 -04002581 this->flushScissor(GrScissorState(dynamicStateArrays->fScissorRects[m]),
Robert Phillipsd0fe8752019-01-31 14:13:59 -05002582 glRT->getViewport(), origin);
Chris Dalton46983b72017-06-06 12:27:16 -06002583 }
Brian Salomonf7232642018-09-19 08:58:08 -04002584 if (dynamicPrimProcTextures) {
2585 auto texProxyArray = dynamicStateArrays->fPrimitiveProcessorTextures +
2586 m * primProc.numTextureSamplers();
2587 fHWProgram->updatePrimitiveProcessorTextureBindings(primProc, texProxyArray);
2588 }
Brian Salomon6d9c88b2017-06-12 10:24:42 -04002589 if (this->glCaps().requiresCullFaceEnableDisableWhenDrawingLinesAfterNonLines() &&
Brian Salomonf7232642018-09-19 08:58:08 -04002590 GrIsPrimTypeLines(meshes[m].primitiveType()) &&
Brian Salomon6d9c88b2017-06-12 10:24:42 -04002591 !GrIsPrimTypeLines(fLastPrimitiveType)) {
2592 GL_CALL(Enable(GR_GL_CULL_FACE));
2593 GL_CALL(Disable(GR_GL_CULL_FACE));
2594 }
Brian Salomonf7232642018-09-19 08:58:08 -04002595 meshes[m].sendToGpu(this);
2596 fLastPrimitiveType = meshes[m].primitiveType();
bsalomon@google.com74749cd2013-01-30 16:12:41 +00002597 }
ethannicholas22793252016-01-30 09:59:10 -08002598
bsalomon@google.comd302f142011-03-03 13:54:13 +00002599#if SWAP_PER_DRAW
2600 glFlush();
commit-bot@chromium.org43823302013-09-25 20:57:51 +00002601 #if defined(SK_BUILD_FOR_MAC)
bsalomon@google.comd302f142011-03-03 13:54:13 +00002602 aglSwapBuffers(aglGetCurrentContext());
2603 int set_a_break_pt_here = 9;
2604 aglSwapBuffers(aglGetCurrentContext());
Mike Klein8f11d4d2018-01-24 12:42:55 -05002605 #elif defined(SK_BUILD_FOR_WIN)
bsalomon@google.comd302f142011-03-03 13:54:13 +00002606 SwapBuf();
2607 int set_a_break_pt_here = 9;
2608 SwapBuf();
2609 #endif
2610#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00002611}
2612
Chris Dalton3809bab2017-06-13 10:55:06 -06002613static GrGLenum gr_primitive_type_to_gl_mode(GrPrimitiveType primitiveType) {
2614 switch (primitiveType) {
2615 case GrPrimitiveType::kTriangles:
2616 return GR_GL_TRIANGLES;
2617 case GrPrimitiveType::kTriangleStrip:
2618 return GR_GL_TRIANGLE_STRIP;
Chris Dalton3809bab2017-06-13 10:55:06 -06002619 case GrPrimitiveType::kPoints:
2620 return GR_GL_POINTS;
2621 case GrPrimitiveType::kLines:
2622 return GR_GL_LINES;
2623 case GrPrimitiveType::kLineStrip:
2624 return GR_GL_LINE_STRIP;
2625 case GrPrimitiveType::kLinesAdjacency:
2626 return GR_GL_LINES_ADJACENCY;
2627 }
Ben Wagnerb4aab9a2017-08-16 10:53:04 -04002628 SK_ABORT("invalid GrPrimitiveType");
Chris Dalton3809bab2017-06-13 10:55:06 -06002629 return GR_GL_TRIANGLES;
2630}
2631
Brian Salomon802cb312018-06-08 18:05:20 -04002632void GrGLGpu::sendMeshToGpu(GrPrimitiveType primitiveType, const GrBuffer* vertexBuffer,
2633 int vertexCount, int baseVertex) {
Chris Dalton3809bab2017-06-13 10:55:06 -06002634 const GrGLenum glPrimType = gr_primitive_type_to_gl_mode(primitiveType);
Chris Dalton114a3c02017-05-26 15:17:19 -06002635 if (this->glCaps().drawArraysBaseVertexIsBroken()) {
Brian Salomon802cb312018-06-08 18:05:20 -04002636 this->setupGeometry(nullptr, vertexBuffer, baseVertex, nullptr, 0, GrPrimitiveRestart::kNo);
Chris Dalton114a3c02017-05-26 15:17:19 -06002637 GL_CALL(DrawArrays(glPrimType, 0, vertexCount));
2638 } else {
Brian Salomon802cb312018-06-08 18:05:20 -04002639 this->setupGeometry(nullptr, vertexBuffer, 0, nullptr, 0, GrPrimitiveRestart::kNo);
Chris Dalton114a3c02017-05-26 15:17:19 -06002640 GL_CALL(DrawArrays(glPrimType, baseVertex, vertexCount));
2641 }
2642 fStats.incNumDraws();
2643}
2644
Brian Salomondbf70722019-02-07 11:31:24 -05002645static const GrGLvoid* element_ptr(const GrBuffer* indexBuffer, int baseIndex) {
2646 size_t baseOffset = baseIndex * sizeof(uint16_t);
2647 if (indexBuffer->isCpuBuffer()) {
2648 return static_cast<const GrCpuBuffer*>(indexBuffer)->data() + baseOffset;
2649 } else {
2650 return reinterpret_cast<const GrGLvoid*>(baseOffset);
2651 }
2652}
2653
Brian Salomon802cb312018-06-08 18:05:20 -04002654void GrGLGpu::sendIndexedMeshToGpu(GrPrimitiveType primitiveType, const GrBuffer* indexBuffer,
Chris Dalton114a3c02017-05-26 15:17:19 -06002655 int indexCount, int baseIndex, uint16_t minIndexValue,
2656 uint16_t maxIndexValue, const GrBuffer* vertexBuffer,
Brian Salomon802cb312018-06-08 18:05:20 -04002657 int baseVertex, GrPrimitiveRestart enablePrimitiveRestart) {
Chris Dalton3809bab2017-06-13 10:55:06 -06002658 const GrGLenum glPrimType = gr_primitive_type_to_gl_mode(primitiveType);
Brian Salomondbf70722019-02-07 11:31:24 -05002659 const GrGLvoid* elementPtr = element_ptr(indexBuffer, baseIndex);
Chris Dalton114a3c02017-05-26 15:17:19 -06002660
Brian Salomon802cb312018-06-08 18:05:20 -04002661 this->setupGeometry(indexBuffer, vertexBuffer, baseVertex, nullptr, 0, enablePrimitiveRestart);
Chris Dalton114a3c02017-05-26 15:17:19 -06002662
2663 if (this->glCaps().drawRangeElementsSupport()) {
2664 GL_CALL(DrawRangeElements(glPrimType, minIndexValue, maxIndexValue, indexCount,
Brian Salomondbf70722019-02-07 11:31:24 -05002665 GR_GL_UNSIGNED_SHORT, elementPtr));
Chris Dalton114a3c02017-05-26 15:17:19 -06002666 } else {
Brian Salomondbf70722019-02-07 11:31:24 -05002667 GL_CALL(DrawElements(glPrimType, indexCount, GR_GL_UNSIGNED_SHORT, elementPtr));
Chris Dalton114a3c02017-05-26 15:17:19 -06002668 }
2669 fStats.incNumDraws();
2670}
2671
Brian Salomon802cb312018-06-08 18:05:20 -04002672void GrGLGpu::sendInstancedMeshToGpu(GrPrimitiveType primitiveType, const GrBuffer* vertexBuffer,
Chris Dalton1d616352017-05-31 12:51:23 -06002673 int vertexCount, int baseVertex,
2674 const GrBuffer* instanceBuffer, int instanceCount,
2675 int baseInstance) {
Chris Daltoncc604e52017-10-06 16:27:32 -06002676 GrGLenum glPrimType = gr_primitive_type_to_gl_mode(primitiveType);
Chris Dalton1b4ad762018-10-04 11:58:09 -06002677 int maxInstances = this->glCaps().maxInstancesPerDrawWithoutCrashing(instanceCount);
Chris Daltoncc604e52017-10-06 16:27:32 -06002678 for (int i = 0; i < instanceCount; i += maxInstances) {
Brian Salomon802cb312018-06-08 18:05:20 -04002679 this->setupGeometry(nullptr, vertexBuffer, 0, instanceBuffer, baseInstance + i,
2680 GrPrimitiveRestart::kNo);
Chris Daltoncc604e52017-10-06 16:27:32 -06002681 GL_CALL(DrawArraysInstanced(glPrimType, baseVertex, vertexCount,
2682 SkTMin(instanceCount - i, maxInstances)));
2683 fStats.incNumDraws();
2684 }
Chris Dalton1d616352017-05-31 12:51:23 -06002685}
2686
Brian Salomon802cb312018-06-08 18:05:20 -04002687void GrGLGpu::sendIndexedInstancedMeshToGpu(GrPrimitiveType primitiveType,
Chris Dalton1d616352017-05-31 12:51:23 -06002688 const GrBuffer* indexBuffer, int indexCount,
2689 int baseIndex, const GrBuffer* vertexBuffer,
2690 int baseVertex, const GrBuffer* instanceBuffer,
Brian Salomon802cb312018-06-08 18:05:20 -04002691 int instanceCount, int baseInstance,
2692 GrPrimitiveRestart enablePrimitiveRestart) {
Chris Dalton3809bab2017-06-13 10:55:06 -06002693 const GrGLenum glPrimType = gr_primitive_type_to_gl_mode(primitiveType);
Brian Salomondbf70722019-02-07 11:31:24 -05002694 const GrGLvoid* elementPtr = element_ptr(indexBuffer, baseIndex);
Chris Dalton1b4ad762018-10-04 11:58:09 -06002695 int maxInstances = this->glCaps().maxInstancesPerDrawWithoutCrashing(instanceCount);
2696 for (int i = 0; i < instanceCount; i += maxInstances) {
2697 this->setupGeometry(indexBuffer, vertexBuffer, baseVertex, instanceBuffer, baseInstance + i,
2698 enablePrimitiveRestart);
Brian Salomondbf70722019-02-07 11:31:24 -05002699 GL_CALL(DrawElementsInstanced(glPrimType, indexCount, GR_GL_UNSIGNED_SHORT, elementPtr,
Chris Dalton1b4ad762018-10-04 11:58:09 -06002700 SkTMin(instanceCount - i, maxInstances)));
2701 fStats.incNumDraws();
2702 }
Chris Dalton1d616352017-05-31 12:51:23 -06002703}
2704
Brian Salomon1fabd512018-02-09 09:54:25 -05002705void GrGLGpu::onResolveRenderTarget(GrRenderTarget* target) {
bsalomon@google.com75f9f252012-01-31 13:35:56 +00002706 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00002707 if (rt->needsResolve()) {
bsalomon@google.com347c3822013-05-01 20:10:01 +00002708 // Some extensions automatically resolves the texture when it is read.
2709 if (this->glCaps().usesMSAARenderBuffers()) {
egdanield803f272015-03-18 13:01:52 -07002710 SkASSERT(rt->textureFBOID() != rt->renderFBOID());
Brian Osmancfe83d12018-01-19 11:13:45 -05002711 SkASSERT(rt->textureFBOID() != 0 && rt->renderFBOID() != 0);
Adrienne Walker4ee88512018-05-17 11:37:14 -07002712 this->bindFramebuffer(GR_GL_READ_FRAMEBUFFER, rt->renderFBOID());
2713 this->bindFramebuffer(GR_GL_DRAW_FRAMEBUFFER, rt->textureFBOID());
2714
egdanield803f272015-03-18 13:01:52 -07002715 // make sure we go through flushRenderTarget() since we've modified
2716 // the bound DRAW FBO ID.
Robert Phillips294870f2016-11-11 12:38:40 -05002717 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002718 const GrGLIRect& vp = rt->getViewport();
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00002719 const SkIRect dirtyRect = rt->getResolveRect();
Brian Salomon1fabd512018-02-09 09:54:25 -05002720 // The dirty rect tracked on the RT is always stored in the native coordinates of the
2721 // surface. Choose kTopLeft so no adjustments are made
2722 static constexpr auto kDirtyRectOrigin = kTopLeft_GrSurfaceOrigin;
bsalomon@google.com347c3822013-05-01 20:10:01 +00002723 if (GrGLCaps::kES_Apple_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002724 // Apple's extension uses the scissor as the blit bounds.
Brian Salomond818ebf2018-07-02 14:08:49 +00002725 GrScissorState scissorState;
2726 scissorState.set(dirtyRect);
2727 this->flushScissor(scissorState, vp, kDirtyRectOrigin);
csmartdalton28341fa2016-08-17 10:00:21 -07002728 this->disableWindowRectangles();
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002729 GL_CALL(ResolveMultisampleFramebuffer());
2730 } else {
Brian Salomone5e7eb12016-10-14 16:18:33 -04002731 int l, b, r, t;
2732 if (GrGLCaps::kResolveMustBeFull_BlitFrambufferFlag &
2733 this->glCaps().blitFramebufferSupportFlags()) {
2734 l = 0;
2735 b = 0;
2736 r = target->width();
2737 t = target->height();
2738 } else {
2739 GrGLIRect rect;
Brian Salomon1fabd512018-02-09 09:54:25 -05002740 rect.setRelativeTo(vp, dirtyRect, kDirtyRectOrigin);
Brian Salomone5e7eb12016-10-14 16:18:33 -04002741 l = rect.fLeft;
2742 b = rect.fBottom;
2743 r = rect.fLeft + rect.fWidth;
2744 t = rect.fBottom + rect.fHeight;
2745 }
derekf8c8f71a2014-09-16 06:24:57 -07002746
2747 // BlitFrameBuffer respects the scissor, so disable it.
Brian Salomond818ebf2018-07-02 14:08:49 +00002748 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07002749 this->disableWindowRectangles();
Brian Salomone5e7eb12016-10-14 16:18:33 -04002750 GL_CALL(BlitFramebuffer(l, b, r, t, l, b, r, t,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002751 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00002752 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002753 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00002754 rt->flagAsResolved();
reed@google.comac10a2d2010-12-22 21:39:39 +00002755 }
2756}
2757
bsalomon@google.com411dad02012-06-05 20:24:20 +00002758namespace {
bsalomon@google.comd302f142011-03-03 13:54:13 +00002759
bsalomon@google.com411dad02012-06-05 20:24:20 +00002760
2761GrGLenum gr_to_gl_stencil_op(GrStencilOp op) {
cdalton93a379b2016-05-11 13:58:08 -07002762 static const GrGLenum gTable[kGrStencilOpCount] = {
2763 GR_GL_KEEP, // kKeep
2764 GR_GL_ZERO, // kZero
2765 GR_GL_REPLACE, // kReplace
2766 GR_GL_INVERT, // kInvert
2767 GR_GL_INCR_WRAP, // kIncWrap
2768 GR_GL_DECR_WRAP, // kDecWrap
2769 GR_GL_INCR, // kIncClamp
2770 GR_GL_DECR, // kDecClamp
bsalomon@google.com411dad02012-06-05 20:24:20 +00002771 };
cdalton93a379b2016-05-11 13:58:08 -07002772 GR_STATIC_ASSERT(0 == (int)GrStencilOp::kKeep);
2773 GR_STATIC_ASSERT(1 == (int)GrStencilOp::kZero);
2774 GR_STATIC_ASSERT(2 == (int)GrStencilOp::kReplace);
2775 GR_STATIC_ASSERT(3 == (int)GrStencilOp::kInvert);
2776 GR_STATIC_ASSERT(4 == (int)GrStencilOp::kIncWrap);
2777 GR_STATIC_ASSERT(5 == (int)GrStencilOp::kDecWrap);
2778 GR_STATIC_ASSERT(6 == (int)GrStencilOp::kIncClamp);
2779 GR_STATIC_ASSERT(7 == (int)GrStencilOp::kDecClamp);
2780 SkASSERT(op < (GrStencilOp)kGrStencilOpCount);
2781 return gTable[(int)op];
bsalomon@google.com411dad02012-06-05 20:24:20 +00002782}
2783
2784void set_gl_stencil(const GrGLInterface* gl,
cdalton93a379b2016-05-11 13:58:08 -07002785 const GrStencilSettings::Face& face,
2786 GrGLenum glFace) {
2787 GrGLenum glFunc = GrToGLStencilFunc(face.fTest);
2788 GrGLenum glFailOp = gr_to_gl_stencil_op(face.fFailOp);
2789 GrGLenum glPassOp = gr_to_gl_stencil_op(face.fPassOp);
bsalomon@google.coma3201942012-06-21 19:58:20 +00002790
cdalton93a379b2016-05-11 13:58:08 -07002791 GrGLint ref = face.fRef;
2792 GrGLint mask = face.fTestMask;
2793 GrGLint writeMask = face.fWriteMask;
bsalomon@google.com411dad02012-06-05 20:24:20 +00002794
2795 if (GR_GL_FRONT_AND_BACK == glFace) {
2796 // we call the combined func just in case separate stencil is not
2797 // supported.
2798 GR_GL_CALL(gl, StencilFunc(glFunc, ref, mask));
2799 GR_GL_CALL(gl, StencilMask(writeMask));
vbuzinovc5d58f02015-08-21 05:24:24 -07002800 GR_GL_CALL(gl, StencilOp(glFailOp, GR_GL_KEEP, glPassOp));
bsalomon@google.com411dad02012-06-05 20:24:20 +00002801 } else {
2802 GR_GL_CALL(gl, StencilFuncSeparate(glFace, glFunc, ref, mask));
2803 GR_GL_CALL(gl, StencilMaskSeparate(glFace, writeMask));
vbuzinovc5d58f02015-08-21 05:24:24 -07002804 GR_GL_CALL(gl, StencilOpSeparate(glFace, glFailOp, GR_GL_KEEP, glPassOp));
bsalomon@google.com411dad02012-06-05 20:24:20 +00002805 }
2806}
2807}
bsalomon@google.comd302f142011-03-03 13:54:13 +00002808
bsalomon3e791242014-12-17 13:43:13 -08002809void GrGLGpu::flushStencil(const GrStencilSettings& stencilSettings) {
csmartdaltonc7d85332016-10-26 10:13:46 -07002810 if (stencilSettings.isDisabled()) {
2811 this->disableStencil();
2812 } else if (fHWStencilSettings != stencilSettings) {
2813 if (kYes_TriState != fHWStencilTestEnabled) {
2814 GL_CALL(Enable(GR_GL_STENCIL_TEST));
Brian Salomonaf971de2017-06-08 16:11:33 -04002815
csmartdaltonc7d85332016-10-26 10:13:46 -07002816 fHWStencilTestEnabled = kYes_TriState;
bsalomon@google.coma3201942012-06-21 19:58:20 +00002817 }
csmartdaltonc7d85332016-10-26 10:13:46 -07002818 if (stencilSettings.isTwoSided()) {
csmartdaltonc7d85332016-10-26 10:13:46 -07002819 set_gl_stencil(this->glInterface(),
2820 stencilSettings.front(),
2821 GR_GL_FRONT);
2822 set_gl_stencil(this->glInterface(),
2823 stencilSettings.back(),
2824 GR_GL_BACK);
2825 } else {
2826 set_gl_stencil(this->glInterface(),
2827 stencilSettings.front(),
2828 GR_GL_FRONT_AND_BACK);
bsalomon@google.comd302f142011-03-03 13:54:13 +00002829 }
joshualitta58fe352014-10-27 08:39:00 -07002830 fHWStencilSettings = stencilSettings;
reed@google.comac10a2d2010-12-22 21:39:39 +00002831 }
2832}
2833
csmartdaltonc7d85332016-10-26 10:13:46 -07002834void GrGLGpu::disableStencil() {
2835 if (kNo_TriState != fHWStencilTestEnabled) {
2836 GL_CALL(Disable(GR_GL_STENCIL_TEST));
Brian Salomonaf971de2017-06-08 16:11:33 -04002837
csmartdaltonc7d85332016-10-26 10:13:46 -07002838 fHWStencilTestEnabled = kNo_TriState;
2839 fHWStencilSettings.invalidate();
2840 }
2841}
2842
Chris Dalton4c56b032019-03-04 12:28:42 -07002843void GrGLGpu::flushHWAAState(GrRenderTarget* rt, bool useHWAA) {
bsalomon083617b2016-02-12 12:10:14 -08002844 // rt is only optional if useHWAA is false.
2845 SkASSERT(rt || !useHWAA);
vbuzinovdded6962015-06-12 08:59:45 -07002846 SkASSERT(!useHWAA || rt->isStencilBufferMultisampled());
bsalomon@google.com202d1392013-03-19 18:58:08 +00002847
csmartdalton2b5f2cb2016-06-10 14:06:32 -07002848 if (this->caps()->multisampleDisableSupport()) {
cdaltond0a840d2015-03-16 17:19:58 -07002849 if (useHWAA) {
2850 if (kYes_TriState != fMSAAEnabled) {
2851 GL_CALL(Enable(GR_GL_MULTISAMPLE));
2852 fMSAAEnabled = kYes_TriState;
2853 }
2854 } else {
2855 if (kNo_TriState != fMSAAEnabled) {
2856 GL_CALL(Disable(GR_GL_MULTISAMPLE));
2857 fMSAAEnabled = kNo_TriState;
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00002858 }
2859 }
2860 }
2861}
2862
bsalomon7f9b2e42016-01-12 13:29:26 -08002863void GrGLGpu::flushBlend(const GrXferProcessor::BlendInfo& blendInfo, const GrSwizzle& swizzle) {
egdanielb414f252014-07-29 13:15:47 -07002864 // Any optimization to disable blending should have already been applied and
cdalton8917d622015-05-06 13:40:21 -07002865 // tweaked the equation to "add" or "subtract", and the coeffs to (1, 0).
bsalomonf7cc8772015-05-11 11:21:14 -07002866
cdalton8917d622015-05-06 13:40:21 -07002867 GrBlendEquation equation = blendInfo.fEquation;
egdanielc2304142014-12-11 13:15:13 -08002868 GrBlendCoeff srcCoeff = blendInfo.fSrcBlend;
2869 GrBlendCoeff dstCoeff = blendInfo.fDstBlend;
Dongseong Hwang4e1f0222018-11-01 09:10:51 -07002870 bool blendOff =
2871 ((kAdd_GrBlendEquation == equation || kSubtract_GrBlendEquation == equation) &&
2872 kOne_GrBlendCoeff == srcCoeff && kZero_GrBlendCoeff == dstCoeff) ||
2873 !blendInfo.fWriteColor;
egdanielb414f252014-07-29 13:15:47 -07002874 if (blendOff) {
2875 if (kNo_TriState != fHWBlendState.fEnabled) {
2876 GL_CALL(Disable(GR_GL_BLEND));
joel.liang9764c402015-07-09 19:46:18 -07002877
2878 // Workaround for the ARM KHR_blend_equation_advanced blacklist issue
2879 // https://code.google.com/p/skia/issues/detail?id=3943
2880 if (kARM_GrGLVendor == this->ctxInfo().vendor() &&
2881 GrBlendEquationIsAdvanced(fHWBlendState.fEquation)) {
2882 SkASSERT(this->caps()->advancedBlendEquationSupport());
2883 // Set to any basic blending equation.
2884 GrBlendEquation blend_equation = kAdd_GrBlendEquation;
2885 GL_CALL(BlendEquation(gXfermodeEquation2Blend[blend_equation]));
2886 fHWBlendState.fEquation = blend_equation;
2887 }
2888
egdanielb414f252014-07-29 13:15:47 -07002889 fHWBlendState.fEnabled = kNo_TriState;
2890 }
cdalton8917d622015-05-06 13:40:21 -07002891 return;
2892 }
2893
2894 if (kYes_TriState != fHWBlendState.fEnabled) {
2895 GL_CALL(Enable(GR_GL_BLEND));
Brian Salomonaf971de2017-06-08 16:11:33 -04002896
cdalton8917d622015-05-06 13:40:21 -07002897 fHWBlendState.fEnabled = kYes_TriState;
2898 }
2899
Mike Kleinab5fec92018-08-16 19:43:31 +00002900 if (fHWBlendState.fEquation != equation) {
cdalton8917d622015-05-06 13:40:21 -07002901 GL_CALL(BlendEquation(gXfermodeEquation2Blend[equation]));
2902 fHWBlendState.fEquation = equation;
2903 }
2904
2905 if (GrBlendEquationIsAdvanced(equation)) {
2906 SkASSERT(this->caps()->advancedBlendEquationSupport());
2907 // Advanced equations have no other blend state.
2908 return;
2909 }
2910
Mike Kleinab5fec92018-08-16 19:43:31 +00002911 if (fHWBlendState.fSrcCoeff != srcCoeff || fHWBlendState.fDstCoeff != dstCoeff) {
cdalton8917d622015-05-06 13:40:21 -07002912 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
2913 gXfermodeCoeff2Blend[dstCoeff]));
2914 fHWBlendState.fSrcCoeff = srcCoeff;
2915 fHWBlendState.fDstCoeff = dstCoeff;
2916 }
2917
bsalomon7f9b2e42016-01-12 13:29:26 -08002918 if ((BlendCoeffReferencesConstant(srcCoeff) || BlendCoeffReferencesConstant(dstCoeff))) {
Brian Osman422f95b2018-11-05 16:49:04 -05002919 SkPMColor4f blendConst = swizzle.applyTo(blendInfo.fBlendConstant);
bsalomon7f9b2e42016-01-12 13:29:26 -08002920 if (!fHWBlendState.fConstColorValid || fHWBlendState.fConstColor != blendConst) {
Brian Osman422f95b2018-11-05 16:49:04 -05002921 GL_CALL(BlendColor(blendConst.fR, blendConst.fG, blendConst.fB, blendConst.fA));
bsalomon7f9b2e42016-01-12 13:29:26 -08002922 fHWBlendState.fConstColor = blendConst;
2923 fHWBlendState.fConstColorValid = true;
2924 }
bsalomon@google.com0650e812011-04-08 18:07:53 +00002925 }
2926}
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002927
Brian Salomondc829942018-10-23 16:07:24 -04002928static void get_gl_swizzle_values(const GrSwizzle& swizzle, GrGLenum glValues[4]) {
Brian Salomon327955b2018-10-22 15:39:22 +00002929 for (int i = 0; i < 4; ++i) {
Brian Salomondc829942018-10-23 16:07:24 -04002930 switch (swizzle[i]) {
2931 case 'r': glValues[i] = GR_GL_RED; break;
2932 case 'g': glValues[i] = GR_GL_GREEN; break;
2933 case 'b': glValues[i] = GR_GL_BLUE; break;
2934 case 'a': glValues[i] = GR_GL_ALPHA; break;
Greg Daniel216a9d72019-02-20 10:12:29 -05002935 case '1': glValues[i] = GR_GL_ONE; break;
Brian Salomondc829942018-10-23 16:07:24 -04002936 default: SK_ABORT("Unsupported component");
2937 }
Brian Salomon327955b2018-10-22 15:39:22 +00002938 }
2939}
2940
Brian Salomondc829942018-10-23 16:07:24 -04002941void GrGLGpu::bindTexture(int unitIdx, GrSamplerState samplerState, GrGLTexture* texture) {
bsalomon49f085d2014-09-05 13:34:00 -07002942 SkASSERT(texture);
tomhudson@google.comd0c1a062012-07-12 17:23:52 +00002943
reed856e9d92015-09-30 12:21:45 -07002944#ifdef SK_DEBUG
2945 if (!this->caps()->npotTextureTileSupport()) {
Brian Salomon2bbdcc42017-09-07 12:36:34 -04002946 if (samplerState.isRepeated()) {
reed856e9d92015-09-30 12:21:45 -07002947 const int w = texture->width();
2948 const int h = texture->height();
2949 SkASSERT(SkIsPow2(w) && SkIsPow2(h));
2950 }
2951 }
2952#endif
2953
bsalomon@google.comb8670992012-07-25 21:27:09 +00002954 // If we created a rt/tex and rendered to it without using a texture and now we're texturing
2955 // 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 +00002956 // out of the "last != next" check.
bsalomon37dd3312014-11-03 08:47:23 -08002957 GrGLRenderTarget* texRT = static_cast<GrGLRenderTarget*>(texture->asRenderTarget());
bsalomon49f085d2014-09-05 13:34:00 -07002958 if (texRT) {
Brian Salomon1fabd512018-02-09 09:54:25 -05002959 this->onResolveRenderTarget(texRT);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002960 }
2961
Robert Phillips294870f2016-11-11 12:38:40 -05002962 GrGpuResource::UniqueID textureID = texture->uniqueID();
bsalomon10528f12015-10-14 12:54:52 -07002963 GrGLenum target = texture->target();
Brian Salomond978b902019-02-07 15:09:18 -05002964 if (fHWTextureUnitBindings[unitIdx].boundID(target) != textureID) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002965 this->setTextureUnit(unitIdx);
bsalomon10528f12015-10-14 12:54:52 -07002966 GL_CALL(BindTexture(target, texture->textureID()));
Brian Salomond978b902019-02-07 15:09:18 -05002967 fHWTextureUnitBindings[unitIdx].setBoundID(target, textureID);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002968 }
2969
Brian Salomondc829942018-10-23 16:07:24 -04002970 if (samplerState.filter() == GrSamplerState::Filter::kMipMap) {
Greg Daniel8f5bbda2018-06-08 17:22:23 -04002971 if (!this->caps()->mipMapSupport() ||
2972 texture->texturePriv().mipMapped() == GrMipMapped::kNo) {
Brian Salomondc829942018-10-23 16:07:24 -04002973 samplerState.setFilterMode(GrSamplerState::Filter::kBilerp);
bsalomonefd7d452014-10-23 14:17:46 -07002974 }
commit-bot@chromium.org47442312013-12-19 16:18:01 +00002975 }
bsalomonefd7d452014-10-23 14:17:46 -07002976
brianosman33f6b3f2016-06-02 05:49:21 -07002977#ifdef SK_DEBUG
Brian Osman2b23c4b2018-06-01 12:25:08 -04002978 // We were supposed to ensure MipMaps were up-to-date before getting here.
Brian Salomondc829942018-10-23 16:07:24 -04002979 if (samplerState.filter() == GrSamplerState::Filter::kMipMap) {
brianosman33f6b3f2016-06-02 05:49:21 -07002980 SkASSERT(!texture->texturePriv().mipMapsAreDirty());
brianosman33f6b3f2016-06-02 05:49:21 -07002981 }
2982#endif
2983
Brian Salomondc829942018-10-23 16:07:24 -04002984 ResetTimestamp timestamp = texture->getCachedParamsTimestamp();
2985 bool setAll = timestamp < this->getResetTimestamp();
cblume55f2d2d2016-02-26 13:20:48 -08002986
Brian Salomondc829942018-10-23 16:07:24 -04002987 const GrGLTexture::SamplerParams* samplerParamsToRecord = nullptr;
2988 GrGLTexture::SamplerParams newSamplerParams;
2989 if (fSamplerObjectCache) {
2990 fSamplerObjectCache->bindSampler(unitIdx, samplerState);
2991 } else {
2992 const GrGLTexture::SamplerParams& oldSamplerParams = texture->getCachedSamplerParams();
2993 samplerParamsToRecord = &newSamplerParams;
2994
2995 newSamplerParams.fMinFilter = filter_to_gl_min_filter(samplerState.filter());
2996 newSamplerParams.fMagFilter = filter_to_gl_mag_filter(samplerState.filter());
2997
Michael Ludwigf23a1522018-12-10 11:36:13 -05002998 newSamplerParams.fWrapS = wrap_mode_to_gl_wrap(samplerState.wrapModeX(), this->glCaps());
2999 newSamplerParams.fWrapT = wrap_mode_to_gl_wrap(samplerState.wrapModeY(), this->glCaps());
Brian Salomondc829942018-10-23 16:07:24 -04003000
3001 // These are the OpenGL default values.
Brian Salomon89f2ff12018-12-07 19:30:25 -05003002 newSamplerParams.fMinLOD = -1000.f;
3003 newSamplerParams.fMaxLOD = 1000.f;
Brian Salomondc829942018-10-23 16:07:24 -04003004
3005 if (setAll || newSamplerParams.fMagFilter != oldSamplerParams.fMagFilter) {
3006 this->setTextureUnit(unitIdx);
3007 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MAG_FILTER, newSamplerParams.fMagFilter));
3008 }
3009 if (setAll || newSamplerParams.fMinFilter != oldSamplerParams.fMinFilter) {
3010 this->setTextureUnit(unitIdx);
3011 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MIN_FILTER, newSamplerParams.fMinFilter));
3012 }
Mike Klein1bccae52018-10-19 11:38:44 +00003013 if (this->glCaps().mipMapLevelAndLodControlSupport()) {
Brian Salomondc829942018-10-23 16:07:24 -04003014 if (setAll || newSamplerParams.fMinLOD != oldSamplerParams.fMinLOD) {
Mike Klein1bccae52018-10-19 11:38:44 +00003015 this->setTextureUnit(unitIdx);
Brian Salomon89f2ff12018-12-07 19:30:25 -05003016 GL_CALL(TexParameterf(target, GR_GL_TEXTURE_MIN_LOD, newSamplerParams.fMinLOD));
Brian Salomondc829942018-10-23 16:07:24 -04003017 }
3018 if (setAll || newSamplerParams.fMaxLOD != oldSamplerParams.fMaxLOD) {
3019 this->setTextureUnit(unitIdx);
Brian Salomon89f2ff12018-12-07 19:30:25 -05003020 GL_CALL(TexParameterf(target, GR_GL_TEXTURE_MAX_LOD, newSamplerParams.fMaxLOD));
Brian Salomondc829942018-10-23 16:07:24 -04003021 }
3022 }
3023 if (setAll || newSamplerParams.fWrapS != oldSamplerParams.fWrapS) {
3024 this->setTextureUnit(unitIdx);
3025 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_WRAP_S, newSamplerParams.fWrapS));
3026 }
3027 if (setAll || newSamplerParams.fWrapT != oldSamplerParams.fWrapT) {
3028 this->setTextureUnit(unitIdx);
3029 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_WRAP_T, newSamplerParams.fWrapT));
3030 }
Michael Ludwigf23a1522018-12-10 11:36:13 -05003031 if (this->glCaps().clampToBorderSupport()) {
3032 // Make sure the border color is transparent black (the default)
3033 if (setAll || oldSamplerParams.fBorderColorInvalid) {
3034 this->setTextureUnit(unitIdx);
Brian Salomon89f2ff12018-12-07 19:30:25 -05003035 static const GrGLfloat kTransparentBlack[4] = {0.f, 0.f, 0.f, 0.f};
3036 GL_CALL(TexParameterfv(target, GR_GL_TEXTURE_BORDER_COLOR, kTransparentBlack));
Michael Ludwigf23a1522018-12-10 11:36:13 -05003037 }
3038 }
Brian Salomondc829942018-10-23 16:07:24 -04003039 }
3040 GrGLTexture::NonSamplerParams newNonSamplerParams;
3041 newNonSamplerParams.fBaseMipMapLevel = 0;
3042 newNonSamplerParams.fMaxMipMapLevel = texture->texturePriv().maxMipMapLevel();
3043
3044 const GrGLTexture::NonSamplerParams& oldNonSamplerParams = texture->getCachedNonSamplerParams();
3045 if (this->glCaps().textureSwizzleSupport()) {
3046 auto swizzle = this->glCaps().configSwizzle(texture->config());
3047 newNonSamplerParams.fSwizzleKey = swizzle.asKey();
3048 if (setAll || swizzle.asKey() != oldNonSamplerParams.fSwizzleKey) {
3049 GrGLenum glValues[4];
3050 get_gl_swizzle_values(swizzle, glValues);
3051 this->setTextureUnit(unitIdx);
Kevin Lubick8aa203c2019-03-19 13:23:10 -04003052 if (GR_IS_GR_GL(this->glStandard())) {
3053 GR_STATIC_ASSERT(sizeof(glValues[0]) == sizeof(GrGLint));
3054 GL_CALL(TexParameteriv(target, GR_GL_TEXTURE_SWIZZLE_RGBA,
3055 reinterpret_cast<const GrGLint*>(glValues)));
3056 } else if (GR_IS_GR_GL_ES(this->glStandard())) {
Brian Salomondc829942018-10-23 16:07:24 -04003057 // ES3 added swizzle support but not GL_TEXTURE_SWIZZLE_RGBA.
3058 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_R, glValues[0]));
3059 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_G, glValues[1]));
3060 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_B, glValues[2]));
3061 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_A, glValues[3]));
Brian Salomonbfb3df42018-10-19 19:24:31 +00003062 }
Brian Salomonbfb3df42018-10-19 19:24:31 +00003063 }
3064 }
Brian Salomondc829942018-10-23 16:07:24 -04003065 // These are not supported in ES2 contexts
Brian Salomonf3841932018-11-29 09:13:37 -05003066 if (this->glCaps().mipMapLevelAndLodControlSupport() &&
3067 (texture->texturePriv().textureType() != GrTextureType::kExternal ||
3068 !this->glCaps().dontSetBaseOrMaxLevelForExternalTextures())) {
Brian Salomondc829942018-10-23 16:07:24 -04003069 if (newNonSamplerParams.fBaseMipMapLevel != oldNonSamplerParams.fBaseMipMapLevel) {
3070 this->setTextureUnit(unitIdx);
3071 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_BASE_LEVEL,
3072 newNonSamplerParams.fBaseMipMapLevel));
3073 }
3074 if (newNonSamplerParams.fMaxMipMapLevel != oldNonSamplerParams.fMaxMipMapLevel) {
3075 this->setTextureUnit(unitIdx);
3076 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MAX_LEVEL,
3077 newNonSamplerParams.fMaxMipMapLevel));
3078 }
Brian Salomon327955b2018-10-22 15:39:22 +00003079 }
Brian Salomondc829942018-10-23 16:07:24 -04003080 texture->setCachedParams(samplerParamsToRecord, newNonSamplerParams, this->getResetTimestamp());
cdalton74b8d322016-04-11 14:47:28 -07003081}
3082
Brian Salomon1f05d452019-02-08 12:33:08 -05003083void GrGLGpu::onResetTextureBindings() {
3084 static constexpr GrGLenum kTargets[] = {GR_GL_TEXTURE_2D, GR_GL_TEXTURE_RECTANGLE,
3085 GR_GL_TEXTURE_EXTERNAL};
3086 for (int i = 0; i < this->numTextureUnits(); ++i) {
3087 this->setTextureUnit(i);
3088 for (auto target : kTargets) {
3089 if (fHWTextureUnitBindings[i].hasBeenModified(target)) {
3090 GL_CALL(BindTexture(target, 0));
3091 }
3092 }
3093 fHWTextureUnitBindings[i].invalidateAllTargets(true);
3094 }
3095}
3096
egdaniel080e6732014-12-22 07:35:52 -08003097void GrGLGpu::flushColorWrite(bool writeColor) {
3098 if (!writeColor) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +00003099 if (kNo_TriState != fHWWriteToColor) {
3100 GL_CALL(ColorMask(GR_GL_FALSE, GR_GL_FALSE,
3101 GR_GL_FALSE, GR_GL_FALSE));
3102 fHWWriteToColor = kNo_TriState;
bsalomon@google.comd302f142011-03-03 13:54:13 +00003103 }
bsalomon@google.com978c8c62012-05-21 14:45:49 +00003104 } else {
3105 if (kYes_TriState != fHWWriteToColor) {
3106 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
3107 fHWWriteToColor = kYes_TriState;
3108 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00003109 }
bsalomon3e791242014-12-17 13:43:13 -08003110}
bsalomon@google.comd302f142011-03-03 13:54:13 +00003111
Brian Salomon805cc7a2019-01-28 09:52:34 -05003112void GrGLGpu::flushClearColor(GrGLfloat r, GrGLfloat g, GrGLfloat b, GrGLfloat a) {
3113 if (r != fHWClearColor[0] || g != fHWClearColor[1] ||
3114 b != fHWClearColor[2] || a != fHWClearColor[3]) {
3115 GL_CALL(ClearColor(r, g, b, a));
3116 fHWClearColor[0] = r;
3117 fHWClearColor[1] = g;
3118 fHWClearColor[2] = b;
3119 fHWClearColor[3] = a;
3120 }
3121}
3122
bsalomon861e1032014-12-16 07:33:49 -08003123void GrGLGpu::setTextureUnit(int unit) {
Brian Salomond978b902019-02-07 15:09:18 -05003124 SkASSERT(unit >= 0 && unit < this->numTextureUnits());
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00003125 if (unit != fHWActiveTextureUnitIdx) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00003126 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com49209392012-06-05 15:13:46 +00003127 fHWActiveTextureUnitIdx = unit;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00003128 }
3129}
bsalomon@google.com316f99232011-01-13 21:28:12 +00003130
Brian Salomond978b902019-02-07 15:09:18 -05003131void GrGLGpu::bindTextureToScratchUnit(GrGLenum target, GrGLint textureID) {
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00003132 // Bind the last texture unit since it is the least likely to be used by GrGLProgram.
Brian Salomond978b902019-02-07 15:09:18 -05003133 int lastUnitIdx = this->numTextureUnits() - 1;
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00003134 if (lastUnitIdx != fHWActiveTextureUnitIdx) {
3135 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + lastUnitIdx));
3136 fHWActiveTextureUnitIdx = lastUnitIdx;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00003137 }
Brian Salomond978b902019-02-07 15:09:18 -05003138 // Clear out the this field so that if a GrGLProgram does use this unit it will rebind the
3139 // correct texture.
Brian Salomon1f05d452019-02-08 12:33:08 -05003140 fHWTextureUnitBindings[lastUnitIdx].invalidateForScratchUse(target);
Brian Salomond978b902019-02-07 15:09:18 -05003141 GL_CALL(BindTexture(target, textureID));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00003142}
3143
Brian Salomone5e7eb12016-10-14 16:18:33 -04003144// Determines whether glBlitFramebuffer could be used between src and dst by onCopySurface.
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003145static inline bool can_blit_framebuffer_for_copy_surface(
3146 const GrSurface* dst, GrSurfaceOrigin dstOrigin,
3147 const GrSurface* src, GrSurfaceOrigin srcOrigin,
3148 const SkIRect& srcRect,
3149 const SkIPoint& dstPoint,
Greg Daniel26dbe3b2018-05-03 10:35:42 -04003150 const GrGLCaps& caps) {
3151 int dstSampleCnt = 0;
3152 int srcSampleCnt = 0;
3153 if (const GrRenderTarget* rt = dst->asRenderTarget()) {
3154 dstSampleCnt = rt->numColorSamples();
bsalomon@google.comeb851172013-04-15 13:51:00 +00003155 }
Greg Daniel26dbe3b2018-05-03 10:35:42 -04003156 if (const GrRenderTarget* rt = src->asRenderTarget()) {
3157 srcSampleCnt = rt->numColorSamples();
3158 }
3159 SkASSERT((dstSampleCnt > 0) == SkToBool(dst->asRenderTarget()));
3160 SkASSERT((srcSampleCnt > 0) == SkToBool(src->asRenderTarget()));
3161
Brian Salomone5e7eb12016-10-14 16:18:33 -04003162 const GrGLTexture* dstTex = static_cast<const GrGLTexture*>(dst->asTexture());
Mike Klein1d746202018-01-25 17:32:51 -05003163 const GrGLTexture* srcTex = static_cast<const GrGLTexture*>(src->asTexture());
Greg Daniel26dbe3b2018-05-03 10:35:42 -04003164
3165 bool dstIsGLTexture2D = dstTex ? GR_GL_TEXTURE_2D == dstTex->target() : false;
3166 bool srcIsGLTexture2D = srcTex ? GR_GL_TEXTURE_2D == srcTex->target() : false;
3167
3168 return caps.canCopyAsBlit(dst->config(), dstSampleCnt, SkToBool(dstTex), dstIsGLTexture2D,
3169 dstOrigin, src->config(), srcSampleCnt, SkToBool(srcTex),
3170 srcIsGLTexture2D, srcOrigin, src->getBoundsRect(), srcRect, dstPoint);
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00003171}
bsalomon@google.comeb851172013-04-15 13:51:00 +00003172
Brian Osmana9c8a052018-01-19 10:31:56 -05003173static bool rt_has_msaa_render_buffer(const GrGLRenderTarget* rt, const GrGLCaps& glCaps) {
3174 // A RT has a separate MSAA renderbuffer if:
3175 // 1) It's multisampled
3176 // 2) We're using an extension with separate MSAA renderbuffers
3177 // 3) It's not FBO 0, which is special and always auto-resolves
Brian Salomonbdecacf2018-02-02 20:32:49 -05003178 return rt->numColorSamples() > 1 && glCaps.usesMSAARenderBuffers() && rt->renderFBOID() != 0;
Brian Osmana9c8a052018-01-19 10:31:56 -05003179}
3180
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003181static inline bool can_copy_texsubimage(const GrSurface* dst, GrSurfaceOrigin dstOrigin,
3182 const GrSurface* src, GrSurfaceOrigin srcOrigin,
Greg Daniel26dbe3b2018-05-03 10:35:42 -04003183 const GrGLCaps& caps) {
3184
bsalomon@google.comeb851172013-04-15 13:51:00 +00003185 const GrGLRenderTarget* dstRT = static_cast<const GrGLRenderTarget*>(dst->asRenderTarget());
bsalomon@google.coma2719852013-04-17 14:25:27 +00003186 const GrGLRenderTarget* srcRT = static_cast<const GrGLRenderTarget*>(src->asRenderTarget());
bsalomon7ea33f52015-11-22 14:51:00 -08003187 const GrGLTexture* dstTex = static_cast<const GrGLTexture*>(dst->asTexture());
bsalomon7ea33f52015-11-22 14:51:00 -08003188 const GrGLTexture* srcTex = static_cast<const GrGLTexture*>(src->asTexture());
cblume61214052016-01-26 09:10:48 -08003189
Greg Daniel26dbe3b2018-05-03 10:35:42 -04003190 bool dstHasMSAARenderBuffer = dstRT ? rt_has_msaa_render_buffer(dstRT, caps) : false;
3191 bool srcHasMSAARenderBuffer = srcRT ? rt_has_msaa_render_buffer(srcRT, caps) : false;
3192
3193 bool dstIsGLTexture2D = dstTex ? GR_GL_TEXTURE_2D == dstTex->target() : false;
3194 bool srcIsGLTexture2D = srcTex ? GR_GL_TEXTURE_2D == srcTex->target() : false;
3195
3196 return caps.canCopyTexSubImage(dst->config(), dstHasMSAARenderBuffer, SkToBool(dstTex),
3197 dstIsGLTexture2D, dstOrigin, src->config(),
3198 srcHasMSAARenderBuffer, SkToBool(srcTex), srcIsGLTexture2D,
3199 srcOrigin);
bsalomon@google.comeb851172013-04-15 13:51:00 +00003200}
3201
3202// If a temporary FBO was created, its non-zero ID is returned. The viewport that the copy rect is
3203// relative to is output.
Brian Salomon71d9d842016-11-03 13:42:00 -04003204void GrGLGpu::bindSurfaceFBOForPixelOps(GrSurface* surface, GrGLenum fboTarget, GrGLIRect* viewport,
3205 TempFBOTarget tempFBOTarget) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00003206 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(surface->asRenderTarget());
bsalomon083617b2016-02-12 12:10:14 -08003207 if (!rt) {
bsalomon49f085d2014-09-05 13:34:00 -07003208 SkASSERT(surface->asTexture());
Brian Salomon9bada542017-06-12 12:09:30 -04003209 GrGLTexture* texture = static_cast<GrGLTexture*>(surface->asTexture());
3210 GrGLuint texID = texture->textureID();
3211 GrGLenum target = texture->target();
egdanield803f272015-03-18 13:01:52 -07003212 GrGLuint* tempFBOID;
3213 tempFBOID = kSrc_TempFBOTarget == tempFBOTarget ? &fTempSrcFBOID : &fTempDstFBOID;
egdaniel0f5f9672015-02-03 11:10:51 -08003214
egdanield803f272015-03-18 13:01:52 -07003215 if (0 == *tempFBOID) {
3216 GR_GL_CALL(this->glInterface(), GenFramebuffers(1, tempFBOID));
egdaniel0f5f9672015-02-03 11:10:51 -08003217 }
3218
Adrienne Walker4ee88512018-05-17 11:37:14 -07003219 this->bindFramebuffer(fboTarget, *tempFBOID);
egdanield803f272015-03-18 13:01:52 -07003220 GR_GL_CALL(this->glInterface(), FramebufferTexture2D(fboTarget,
robertphillips754f4e92014-09-18 13:52:08 -07003221 GR_GL_COLOR_ATTACHMENT0,
bsalomon10528f12015-10-14 12:54:52 -07003222 target,
robertphillips754f4e92014-09-18 13:52:08 -07003223 texID,
3224 0));
Brian Salomon9bada542017-06-12 12:09:30 -04003225 texture->baseLevelWasBoundToFBO();
bsalomon@google.comeb851172013-04-15 13:51:00 +00003226 viewport->fLeft = 0;
3227 viewport->fBottom = 0;
3228 viewport->fWidth = surface->width();
3229 viewport->fHeight = surface->height();
3230 } else {
Adrienne Walker4ee88512018-05-17 11:37:14 -07003231 this->bindFramebuffer(fboTarget, rt->renderFBOID());
bsalomon@google.comeb851172013-04-15 13:51:00 +00003232 *viewport = rt->getViewport();
3233 }
egdaniel0f5f9672015-02-03 11:10:51 -08003234}
3235
Brian Salomon71d9d842016-11-03 13:42:00 -04003236void GrGLGpu::unbindTextureFBOForPixelOps(GrGLenum fboTarget, GrSurface* surface) {
3237 // bindSurfaceFBOForPixelOps temporarily binds textures that are not render targets to
bsalomon10528f12015-10-14 12:54:52 -07003238 if (!surface->asRenderTarget()) {
3239 SkASSERT(surface->asTexture());
3240 GrGLenum textureTarget = static_cast<GrGLTexture*>(surface->asTexture())->target();
3241 GR_GL_CALL(this->glInterface(), FramebufferTexture2D(fboTarget,
3242 GR_GL_COLOR_ATTACHMENT0,
3243 textureTarget,
3244 0,
3245 0));
3246 }
bsalomon@google.comeb851172013-04-15 13:51:00 +00003247}
3248
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07003249void GrGLGpu::onFBOChanged() {
3250 if (this->caps()->workarounds().flush_on_framebuffer_change ||
3251 this->caps()->workarounds().restore_scissor_on_fbo_change) {
3252 GL_CALL(Flush());
3253 }
3254}
3255
Adrienne Walker4ee88512018-05-17 11:37:14 -07003256void GrGLGpu::bindFramebuffer(GrGLenum target, GrGLuint fboid) {
3257 fStats.incRenderTargetBinds();
3258 GL_CALL(BindFramebuffer(target, fboid));
3259 if (target == GR_GL_FRAMEBUFFER || target == GR_GL_DRAW_FRAMEBUFFER) {
3260 fBoundDrawFramebuffer = fboid;
3261 }
3262
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07003263 if (this->caps()->workarounds().restore_scissor_on_fbo_change) {
3264 // The driver forgets the correct scissor when modifying the FBO binding.
3265 if (!fHWScissorSettings.fRect.isInvalid()) {
3266 fHWScissorSettings.fRect.pushToGLScissor(this->glInterface());
3267 }
Adrienne Walker3ed33992018-05-15 11:44:34 -07003268 }
3269
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07003270 this->onFBOChanged();
Adrienne Walker3ed33992018-05-15 11:44:34 -07003271}
3272
Adrienne Walker4ee88512018-05-17 11:37:14 -07003273void GrGLGpu::deleteFramebuffer(GrGLuint fboid) {
3274 if (fboid == fBoundDrawFramebuffer &&
3275 this->caps()->workarounds().unbind_attachments_on_bound_render_fbo_delete) {
3276 // This workaround only applies to deleting currently bound framebuffers
3277 // on Adreno 420. Because this is a somewhat rare case, instead of
3278 // tracking all the attachments of every framebuffer instead just always
3279 // unbind all attachments.
3280 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
3281 GR_GL_RENDERBUFFER, 0));
3282 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT,
3283 GR_GL_RENDERBUFFER, 0));
3284 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_DEPTH_ATTACHMENT,
3285 GR_GL_RENDERBUFFER, 0));
3286 }
3287
3288 GL_CALL(DeleteFramebuffers(1, &fboid));
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07003289
3290 // Deleting the currently bound framebuffer rebinds to 0.
3291 if (fboid == fBoundDrawFramebuffer) {
3292 this->onFBOChanged();
3293 }
Adrienne Walker4ee88512018-05-17 11:37:14 -07003294}
3295
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003296bool GrGLGpu::onCopySurface(GrSurface* dst, GrSurfaceOrigin dstOrigin,
3297 GrSurface* src, GrSurfaceOrigin srcOrigin,
Greg Daniel55fa6472018-03-16 16:13:10 -04003298 const SkIRect& srcRect, const SkIPoint& dstPoint,
3299 bool canDiscardOutsideDstRect) {
bsalomon7f9b2e42016-01-12 13:29:26 -08003300 // None of our copy methods can handle a swizzle. TODO: Make copySurfaceAsDraw handle the
3301 // swizzle.
Brian Salomon1edc5b92016-11-29 13:43:46 -05003302 if (this->caps()->shaderCaps()->configOutputSwizzle(src->config()) !=
3303 this->caps()->shaderCaps()->configOutputSwizzle(dst->config())) {
bsalomon7f9b2e42016-01-12 13:29:26 -08003304 return false;
3305 }
bsalomon083617b2016-02-12 12:10:14 -08003306 // Don't prefer copying as a draw if the dst doesn't already have a FBO object.
Brian Salomon3d86a192018-02-27 16:46:11 -05003307 // This implicitly handles this->glCaps().useDrawInsteadOfAllRenderTargetWrites().
bsalomon083617b2016-02-12 12:10:14 -08003308 bool preferCopy = SkToBool(dst->asRenderTarget());
Greg Daniel26dbe3b2018-05-03 10:35:42 -04003309 if (preferCopy && this->glCaps().canCopyAsDraw(dst->config(), SkToBool(src->asTexture()))) {
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003310 if (this->copySurfaceAsDraw(dst, dstOrigin, src, srcOrigin, srcRect, dstPoint)) {
cdaltone2e71c22016-04-07 18:13:29 -07003311 return true;
3312 }
bsalomon5df6fee2015-05-18 06:26:15 -07003313 }
cblume61214052016-01-26 09:10:48 -08003314
Greg Daniel26dbe3b2018-05-03 10:35:42 -04003315 if (can_copy_texsubimage(dst, dstOrigin, src, srcOrigin, this->glCaps())) {
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003316 this->copySurfaceAsCopyTexSubImage(dst, dstOrigin, src, srcOrigin, srcRect, dstPoint);
bsalomon6df86402015-06-01 10:41:49 -07003317 return true;
3318 }
3319
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003320 if (can_blit_framebuffer_for_copy_surface(dst, dstOrigin, src, srcOrigin,
Greg Daniel26dbe3b2018-05-03 10:35:42 -04003321 srcRect, dstPoint, this->glCaps())) {
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003322 return this->copySurfaceAsBlitFramebuffer(dst, dstOrigin, src, srcOrigin,
3323 srcRect, dstPoint);
bsalomon6df86402015-06-01 10:41:49 -07003324 }
3325
Greg Daniel26dbe3b2018-05-03 10:35:42 -04003326 if (!preferCopy && this->glCaps().canCopyAsDraw(dst->config(), SkToBool(src->asTexture()))) {
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003327 if (this->copySurfaceAsDraw(dst, dstOrigin, src, srcOrigin, srcRect, dstPoint)) {
cdaltone2e71c22016-04-07 18:13:29 -07003328 return true;
3329 }
bsalomon083617b2016-02-12 12:10:14 -08003330 }
3331
bsalomon6df86402015-06-01 10:41:49 -07003332 return false;
3333}
3334
Brian Salomonbf7b6202016-11-11 16:08:03 -05003335bool GrGLGpu::createCopyProgram(GrTexture* srcTex) {
Brian Osman39c08ac2017-07-26 09:36:09 -04003336 TRACE_EVENT0("skia", TRACE_FUNC);
Ryan Macnak38a10ad2017-07-10 10:36:34 -07003337
Brian Salomonbf7b6202016-11-11 16:08:03 -05003338 int progIdx = TextureToCopyProgramIdx(srcTex);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003339 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
Brian Salomon60dd8c72018-07-30 10:24:13 -04003340 GrSLType samplerType =
3341 GrSLCombinedSamplerTypeForTextureType(srcTex->texturePriv().textureType());
cdaltone2e71c22016-04-07 18:13:29 -07003342
3343 if (!fCopyProgramArrayBuffer) {
3344 static const GrGLfloat vdata[] = {
3345 0, 0,
3346 0, 1,
3347 1, 0,
3348 1, 1
3349 };
Brian Salomonae64c192019-02-05 09:41:37 -05003350 fCopyProgramArrayBuffer = GrGLBuffer::Make(this, sizeof(vdata), GrGpuBufferType::kVertex,
Brian Salomon12d22642019-01-29 14:38:50 -05003351 kStatic_GrAccessPattern, vdata);
cdaltone2e71c22016-04-07 18:13:29 -07003352 }
3353 if (!fCopyProgramArrayBuffer) {
3354 return false;
3355 }
3356
3357 SkASSERT(!fCopyPrograms[progIdx].fProgram);
3358 GL_CALL_RET(fCopyPrograms[progIdx].fProgram, CreateProgram());
3359 if (!fCopyPrograms[progIdx].fProgram) {
3360 return false;
3361 }
3362
Brian Salomon1edc5b92016-11-29 13:43:46 -05003363 const char* version = shaderCaps->versionDeclString();
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003364 GrShaderVar aVertex("a_vertex", kHalf2_GrSLType, GrShaderVar::kIn_TypeModifier);
3365 GrShaderVar uTexCoordXform("u_texCoordXform", kHalf4_GrSLType,
Brian Salomon99938a82016-11-21 13:41:08 -05003366 GrShaderVar::kUniform_TypeModifier);
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003367 GrShaderVar uPosXform("u_posXform", kHalf4_GrSLType, GrShaderVar::kUniform_TypeModifier);
Brian Salomon99938a82016-11-21 13:41:08 -05003368 GrShaderVar uTexture("u_texture", samplerType, GrShaderVar::kUniform_TypeModifier);
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003369 GrShaderVar vTexCoord("v_texCoord", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier);
3370 GrShaderVar oFragColor("o_FragColor", kHalf4_GrSLType, GrShaderVar::kOut_TypeModifier);
cdaltone2e71c22016-04-07 18:13:29 -07003371
3372 SkString vshaderTxt(version);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003373 if (shaderCaps->noperspectiveInterpolationSupport()) {
3374 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
cdaltone2e71c22016-04-07 18:13:29 -07003375 vshaderTxt.appendf("#extension %s : require\n", extension);
3376 }
3377 vTexCoord.addModifier("noperspective");
3378 }
3379
Brian Salomon1edc5b92016-11-29 13:43:46 -05003380 aVertex.appendDecl(shaderCaps, &vshaderTxt);
cdaltone2e71c22016-04-07 18:13:29 -07003381 vshaderTxt.append(";");
Brian Salomon1edc5b92016-11-29 13:43:46 -05003382 uTexCoordXform.appendDecl(shaderCaps, &vshaderTxt);
cdaltone2e71c22016-04-07 18:13:29 -07003383 vshaderTxt.append(";");
Brian Salomon1edc5b92016-11-29 13:43:46 -05003384 uPosXform.appendDecl(shaderCaps, &vshaderTxt);
cdaltone2e71c22016-04-07 18:13:29 -07003385 vshaderTxt.append(";");
Brian Salomon1edc5b92016-11-29 13:43:46 -05003386 vTexCoord.appendDecl(shaderCaps, &vshaderTxt);
cdaltone2e71c22016-04-07 18:13:29 -07003387 vshaderTxt.append(";");
3388
3389 vshaderTxt.append(
3390 "// Copy Program VS\n"
3391 "void main() {"
Ethan Nicholase1f55022019-02-05 17:17:40 -05003392 " v_texCoord = half2(a_vertex.xy * u_texCoordXform.xy + u_texCoordXform.zw);"
Ethan Nicholasbed683a2017-09-26 14:23:59 -04003393 " sk_Position.xy = a_vertex * u_posXform.xy + u_posXform.zw;"
3394 " sk_Position.zw = half2(0, 1);"
cdaltone2e71c22016-04-07 18:13:29 -07003395 "}"
3396 );
3397
3398 SkString fshaderTxt(version);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003399 if (shaderCaps->noperspectiveInterpolationSupport()) {
3400 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
cdaltone2e71c22016-04-07 18:13:29 -07003401 fshaderTxt.appendf("#extension %s : require\n", extension);
3402 }
3403 }
Brian Salomonf31ae492016-11-18 15:35:33 -05003404 vTexCoord.setTypeModifier(GrShaderVar::kIn_TypeModifier);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003405 vTexCoord.appendDecl(shaderCaps, &fshaderTxt);
cdaltone2e71c22016-04-07 18:13:29 -07003406 fshaderTxt.append(";");
Brian Salomon1edc5b92016-11-29 13:43:46 -05003407 uTexture.appendDecl(shaderCaps, &fshaderTxt);
cdaltone2e71c22016-04-07 18:13:29 -07003408 fshaderTxt.append(";");
cdaltone2e71c22016-04-07 18:13:29 -07003409 fshaderTxt.appendf(
3410 "// Copy Program FS\n"
3411 "void main() {"
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003412 " sk_FragColor = texture(u_texture, v_texCoord);"
3413 "}"
cdaltone2e71c22016-04-07 18:13:29 -07003414 );
3415
3416 const char* str;
3417 GrGLint length;
3418
3419 str = vshaderTxt.c_str();
3420 length = SkToInt(vshaderTxt.size());
Ethan Nicholas941e7e22016-12-12 15:33:30 -05003421 SkSL::Program::Settings settings;
3422 settings.fCaps = shaderCaps;
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003423 SkSL::String glsl;
3424 std::unique_ptr<SkSL::Program> program = GrSkSLtoGLSL(*fGLContext, GR_GL_VERTEX_SHADER,
3425 &str, &length, 1, settings, &glsl);
cdaltone2e71c22016-04-07 18:13:29 -07003426 GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fCopyPrograms[progIdx].fProgram,
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003427 GR_GL_VERTEX_SHADER, glsl.c_str(), glsl.size(),
3428 &fStats, settings);
3429 SkASSERT(program->fInputs.isEmpty());
cdaltone2e71c22016-04-07 18:13:29 -07003430
3431 str = fshaderTxt.c_str();
3432 length = SkToInt(fshaderTxt.size());
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003433 program = GrSkSLtoGLSL(*fGLContext, GR_GL_FRAGMENT_SHADER, &str, &length, 1, settings, &glsl);
cdaltone2e71c22016-04-07 18:13:29 -07003434 GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fCopyPrograms[progIdx].fProgram,
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003435 GR_GL_FRAGMENT_SHADER, glsl.c_str(), glsl.size(),
3436 &fStats, settings);
3437 SkASSERT(program->fInputs.isEmpty());
cdaltone2e71c22016-04-07 18:13:29 -07003438
3439 GL_CALL(LinkProgram(fCopyPrograms[progIdx].fProgram));
3440
3441 GL_CALL_RET(fCopyPrograms[progIdx].fTextureUniform,
3442 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_texture"));
3443 GL_CALL_RET(fCopyPrograms[progIdx].fPosXformUniform,
3444 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_posXform"));
3445 GL_CALL_RET(fCopyPrograms[progIdx].fTexCoordXformUniform,
3446 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_texCoordXform"));
3447
3448 GL_CALL(BindAttribLocation(fCopyPrograms[progIdx].fProgram, 0, "a_vertex"));
3449
3450 GL_CALL(DeleteShader(vshader));
3451 GL_CALL(DeleteShader(fshader));
3452
3453 return true;
bsalomon6df86402015-06-01 10:41:49 -07003454}
3455
brianosman33f6b3f2016-06-02 05:49:21 -07003456bool GrGLGpu::createMipmapProgram(int progIdx) {
3457 const bool oddWidth = SkToBool(progIdx & 0x2);
3458 const bool oddHeight = SkToBool(progIdx & 0x1);
3459 const int numTaps = (oddWidth ? 2 : 1) * (oddHeight ? 2 : 1);
3460
Brian Salomon1edc5b92016-11-29 13:43:46 -05003461 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
brianosman33f6b3f2016-06-02 05:49:21 -07003462
3463 SkASSERT(!fMipmapPrograms[progIdx].fProgram);
3464 GL_CALL_RET(fMipmapPrograms[progIdx].fProgram, CreateProgram());
3465 if (!fMipmapPrograms[progIdx].fProgram) {
3466 return false;
3467 }
3468
Brian Salomon1edc5b92016-11-29 13:43:46 -05003469 const char* version = shaderCaps->versionDeclString();
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003470 GrShaderVar aVertex("a_vertex", kHalf2_GrSLType, GrShaderVar::kIn_TypeModifier);
3471 GrShaderVar uTexCoordXform("u_texCoordXform", kHalf4_GrSLType,
Brian Salomon99938a82016-11-21 13:41:08 -05003472 GrShaderVar::kUniform_TypeModifier);
3473 GrShaderVar uTexture("u_texture", kTexture2DSampler_GrSLType,
3474 GrShaderVar::kUniform_TypeModifier);
brianosman33f6b3f2016-06-02 05:49:21 -07003475 // We need 1, 2, or 4 texture coordinates (depending on parity of each dimension):
Brian Salomon99938a82016-11-21 13:41:08 -05003476 GrShaderVar vTexCoords[] = {
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003477 GrShaderVar("v_texCoord0", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier),
3478 GrShaderVar("v_texCoord1", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier),
3479 GrShaderVar("v_texCoord2", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier),
3480 GrShaderVar("v_texCoord3", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier),
brianosman33f6b3f2016-06-02 05:49:21 -07003481 };
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003482 GrShaderVar oFragColor("o_FragColor", kHalf4_GrSLType,GrShaderVar::kOut_TypeModifier);
brianosman33f6b3f2016-06-02 05:49:21 -07003483
3484 SkString vshaderTxt(version);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003485 if (shaderCaps->noperspectiveInterpolationSupport()) {
3486 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
brianosman33f6b3f2016-06-02 05:49:21 -07003487 vshaderTxt.appendf("#extension %s : require\n", extension);
3488 }
3489 vTexCoords[0].addModifier("noperspective");
3490 vTexCoords[1].addModifier("noperspective");
3491 vTexCoords[2].addModifier("noperspective");
3492 vTexCoords[3].addModifier("noperspective");
3493 }
3494
Brian Salomon1edc5b92016-11-29 13:43:46 -05003495 aVertex.appendDecl(shaderCaps, &vshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003496 vshaderTxt.append(";");
Brian Salomon1edc5b92016-11-29 13:43:46 -05003497 uTexCoordXform.appendDecl(shaderCaps, &vshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003498 vshaderTxt.append(";");
3499 for (int i = 0; i < numTaps; ++i) {
Brian Salomon1edc5b92016-11-29 13:43:46 -05003500 vTexCoords[i].appendDecl(shaderCaps, &vshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003501 vshaderTxt.append(";");
3502 }
3503
3504 vshaderTxt.append(
3505 "// Mipmap Program VS\n"
3506 "void main() {"
Ethan Nicholasbed683a2017-09-26 14:23:59 -04003507 " sk_Position.xy = a_vertex * half2(2, 2) - half2(1, 1);"
3508 " sk_Position.zw = half2(0, 1);"
brianosman33f6b3f2016-06-02 05:49:21 -07003509 );
3510
3511 // Insert texture coordinate computation:
3512 if (oddWidth && oddHeight) {
3513 vshaderTxt.append(
3514 " v_texCoord0 = a_vertex.xy * u_texCoordXform.yw;"
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003515 " v_texCoord1 = a_vertex.xy * u_texCoordXform.yw + half2(u_texCoordXform.x, 0);"
3516 " v_texCoord2 = a_vertex.xy * u_texCoordXform.yw + half2(0, u_texCoordXform.z);"
brianosman33f6b3f2016-06-02 05:49:21 -07003517 " v_texCoord3 = a_vertex.xy * u_texCoordXform.yw + u_texCoordXform.xz;"
3518 );
3519 } else if (oddWidth) {
3520 vshaderTxt.append(
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003521 " v_texCoord0 = a_vertex.xy * half2(u_texCoordXform.y, 1);"
3522 " v_texCoord1 = a_vertex.xy * half2(u_texCoordXform.y, 1) + half2(u_texCoordXform.x, 0);"
brianosman33f6b3f2016-06-02 05:49:21 -07003523 );
3524 } else if (oddHeight) {
3525 vshaderTxt.append(
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003526 " v_texCoord0 = a_vertex.xy * half2(1, u_texCoordXform.w);"
3527 " v_texCoord1 = a_vertex.xy * half2(1, u_texCoordXform.w) + half2(0, u_texCoordXform.z);"
brianosman33f6b3f2016-06-02 05:49:21 -07003528 );
3529 } else {
3530 vshaderTxt.append(
3531 " v_texCoord0 = a_vertex.xy;"
3532 );
3533 }
3534
3535 vshaderTxt.append("}");
3536
3537 SkString fshaderTxt(version);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003538 if (shaderCaps->noperspectiveInterpolationSupport()) {
3539 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
brianosman33f6b3f2016-06-02 05:49:21 -07003540 fshaderTxt.appendf("#extension %s : require\n", extension);
3541 }
3542 }
brianosman33f6b3f2016-06-02 05:49:21 -07003543 for (int i = 0; i < numTaps; ++i) {
Brian Salomonf31ae492016-11-18 15:35:33 -05003544 vTexCoords[i].setTypeModifier(GrShaderVar::kIn_TypeModifier);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003545 vTexCoords[i].appendDecl(shaderCaps, &fshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003546 fshaderTxt.append(";");
3547 }
Brian Salomon1edc5b92016-11-29 13:43:46 -05003548 uTexture.appendDecl(shaderCaps, &fshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003549 fshaderTxt.append(";");
brianosman33f6b3f2016-06-02 05:49:21 -07003550 fshaderTxt.append(
3551 "// Mipmap Program FS\n"
3552 "void main() {"
3553 );
3554
3555 if (oddWidth && oddHeight) {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003556 fshaderTxt.append(
3557 " sk_FragColor = (texture(u_texture, v_texCoord0) + "
3558 " texture(u_texture, v_texCoord1) + "
3559 " texture(u_texture, v_texCoord2) + "
3560 " texture(u_texture, v_texCoord3)) * 0.25;"
brianosman33f6b3f2016-06-02 05:49:21 -07003561 );
3562 } else if (oddWidth || oddHeight) {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003563 fshaderTxt.append(
3564 " sk_FragColor = (texture(u_texture, v_texCoord0) + "
3565 " texture(u_texture, v_texCoord1)) * 0.5;"
brianosman33f6b3f2016-06-02 05:49:21 -07003566 );
3567 } else {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003568 fshaderTxt.append(
3569 " sk_FragColor = texture(u_texture, v_texCoord0);"
brianosman33f6b3f2016-06-02 05:49:21 -07003570 );
3571 }
3572
3573 fshaderTxt.append("}");
3574
3575 const char* str;
3576 GrGLint length;
3577
3578 str = vshaderTxt.c_str();
3579 length = SkToInt(vshaderTxt.size());
Ethan Nicholas941e7e22016-12-12 15:33:30 -05003580 SkSL::Program::Settings settings;
3581 settings.fCaps = shaderCaps;
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003582 SkSL::String glsl;
3583 std::unique_ptr<SkSL::Program> program = GrSkSLtoGLSL(*fGLContext, GR_GL_VERTEX_SHADER,
3584 &str, &length, 1, settings, &glsl);
brianosman33f6b3f2016-06-02 05:49:21 -07003585 GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fMipmapPrograms[progIdx].fProgram,
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003586 GR_GL_VERTEX_SHADER, glsl.c_str(), glsl.size(),
3587 &fStats, settings);
3588 SkASSERT(program->fInputs.isEmpty());
brianosman33f6b3f2016-06-02 05:49:21 -07003589
3590 str = fshaderTxt.c_str();
3591 length = SkToInt(fshaderTxt.size());
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003592 program = GrSkSLtoGLSL(*fGLContext, GR_GL_FRAGMENT_SHADER, &str, &length, 1, settings, &glsl);
brianosman33f6b3f2016-06-02 05:49:21 -07003593 GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fMipmapPrograms[progIdx].fProgram,
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003594 GR_GL_FRAGMENT_SHADER, glsl.c_str(), glsl.size(),
3595 &fStats, settings);
3596 SkASSERT(program->fInputs.isEmpty());
brianosman33f6b3f2016-06-02 05:49:21 -07003597
3598 GL_CALL(LinkProgram(fMipmapPrograms[progIdx].fProgram));
3599
3600 GL_CALL_RET(fMipmapPrograms[progIdx].fTextureUniform,
3601 GetUniformLocation(fMipmapPrograms[progIdx].fProgram, "u_texture"));
3602 GL_CALL_RET(fMipmapPrograms[progIdx].fTexCoordXformUniform,
3603 GetUniformLocation(fMipmapPrograms[progIdx].fProgram, "u_texCoordXform"));
3604
3605 GL_CALL(BindAttribLocation(fMipmapPrograms[progIdx].fProgram, 0, "a_vertex"));
3606
3607 GL_CALL(DeleteShader(vshader));
3608 GL_CALL(DeleteShader(fshader));
3609
3610 return true;
3611}
3612
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003613bool GrGLGpu::copySurfaceAsDraw(GrSurface* dst, GrSurfaceOrigin dstOrigin,
3614 GrSurface* src, GrSurfaceOrigin srcOrigin,
bsalomon6df86402015-06-01 10:41:49 -07003615 const SkIRect& srcRect,
3616 const SkIPoint& dstPoint) {
cdaltone2e71c22016-04-07 18:13:29 -07003617 GrGLTexture* srcTex = static_cast<GrGLTexture*>(src->asTexture());
Brian Salomonbf7b6202016-11-11 16:08:03 -05003618 int progIdx = TextureToCopyProgramIdx(srcTex);
cdaltone2e71c22016-04-07 18:13:29 -07003619
Greg Daniel26dbe3b2018-05-03 10:35:42 -04003620 if (!this->glCaps().canConfigBeFBOColorAttachment(dst->config())) {
3621 return false;
3622 }
3623
cdaltone2e71c22016-04-07 18:13:29 -07003624 if (!fCopyPrograms[progIdx].fProgram) {
Brian Salomonbf7b6202016-11-11 16:08:03 -05003625 if (!this->createCopyProgram(srcTex)) {
cdaltone2e71c22016-04-07 18:13:29 -07003626 SkDebugf("Failed to create copy program.\n");
3627 return false;
3628 }
3629 }
3630
bsalomon6df86402015-06-01 10:41:49 -07003631 int w = srcRect.width();
3632 int h = srcRect.height();
3633
Brian Salomon930f9392018-06-20 16:25:26 -04003634 this->bindTexture(0, GrSamplerState::ClampNearest(), srcTex);
bsalomon6df86402015-06-01 10:41:49 -07003635
bsalomon083617b2016-02-12 12:10:14 -08003636 GrGLIRect dstVP;
Brian Salomon71d9d842016-11-03 13:42:00 -04003637 this->bindSurfaceFBOForPixelOps(dst, GR_GL_FRAMEBUFFER, &dstVP, kDst_TempFBOTarget);
bsalomon083617b2016-02-12 12:10:14 -08003638 this->flushViewport(dstVP);
Robert Phillips294870f2016-11-11 12:38:40 -05003639 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon083617b2016-02-12 12:10:14 -08003640
bsalomon6df86402015-06-01 10:41:49 -07003641 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY, w, h);
bsalomon6df86402015-06-01 10:41:49 -07003642
Brian Salomon802cb312018-06-08 18:05:20 -04003643 this->flushProgram(fCopyPrograms[progIdx].fProgram);
bsalomon6df86402015-06-01 10:41:49 -07003644
cdaltone2e71c22016-04-07 18:13:29 -07003645 fHWVertexArrayState.setVertexArrayID(this, 0);
bsalomon6df86402015-06-01 10:41:49 -07003646
cdaltone2e71c22016-04-07 18:13:29 -07003647 GrGLAttribArrayState* attribs = fHWVertexArrayState.bindInternalVertexArray(this);
Chris Dalton8e45b4f2017-05-05 14:00:56 -04003648 attribs->enableVertexArrays(this, 1);
Brian Osmand4c29702018-09-14 16:16:55 -04003649 attribs->set(this, 0, fCopyProgramArrayBuffer.get(), kFloat2_GrVertexAttribType,
Brian Osman4a3f5c82018-09-18 16:16:38 -04003650 kFloat2_GrSLType, 2 * sizeof(GrGLfloat), 0);
bsalomon6df86402015-06-01 10:41:49 -07003651
3652 // dst rect edges in NDC (-1 to 1)
3653 int dw = dst->width();
3654 int dh = dst->height();
3655 GrGLfloat dx0 = 2.f * dstPoint.fX / dw - 1.f;
3656 GrGLfloat dx1 = 2.f * (dstPoint.fX + w) / dw - 1.f;
3657 GrGLfloat dy0 = 2.f * dstPoint.fY / dh - 1.f;
3658 GrGLfloat dy1 = 2.f * (dstPoint.fY + h) / dh - 1.f;
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003659 if (kBottomLeft_GrSurfaceOrigin == dstOrigin) {
bsalomon6df86402015-06-01 10:41:49 -07003660 dy0 = -dy0;
3661 dy1 = -dy1;
3662 }
3663
bsalomone5286e02016-01-14 09:24:09 -08003664 GrGLfloat sx0 = (GrGLfloat)srcRect.fLeft;
3665 GrGLfloat sx1 = (GrGLfloat)(srcRect.fLeft + w);
3666 GrGLfloat sy0 = (GrGLfloat)srcRect.fTop;
3667 GrGLfloat sy1 = (GrGLfloat)(srcRect.fTop + h);
Ethan Nicholas5338f992017-04-19 15:54:07 -04003668 int sw = src->width();
bsalomon6df86402015-06-01 10:41:49 -07003669 int sh = src->height();
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003670 if (kBottomLeft_GrSurfaceOrigin == srcOrigin) {
bsalomone5286e02016-01-14 09:24:09 -08003671 sy0 = sh - sy0;
3672 sy1 = sh - sy1;
3673 }
Brian Salomon246bc3d2018-12-06 15:33:02 -05003674 if (srcTex->texturePriv().textureType() != GrTextureType::kRectangle) {
3675 // src rect edges in normalized texture space (0 to 1)
3676 sx0 /= sw;
3677 sx1 /= sw;
3678 sy0 /= sh;
3679 sy1 /= sh;
3680 }
bsalomon6df86402015-06-01 10:41:49 -07003681
bsalomon7ea33f52015-11-22 14:51:00 -08003682 GL_CALL(Uniform4f(fCopyPrograms[progIdx].fPosXformUniform, dx1 - dx0, dy1 - dy0, dx0, dy0));
3683 GL_CALL(Uniform4f(fCopyPrograms[progIdx].fTexCoordXformUniform,
3684 sx1 - sx0, sy1 - sy0, sx0, sy0));
3685 GL_CALL(Uniform1i(fCopyPrograms[progIdx].fTextureUniform, 0));
bsalomon6df86402015-06-01 10:41:49 -07003686
3687 GrXferProcessor::BlendInfo blendInfo;
3688 blendInfo.reset();
bsalomon7f9b2e42016-01-12 13:29:26 -08003689 this->flushBlend(blendInfo, GrSwizzle::RGBA());
bsalomon6df86402015-06-01 10:41:49 -07003690 this->flushColorWrite(true);
Chris Dalton4c56b032019-03-04 12:28:42 -07003691 this->flushHWAAState(nullptr, false);
Brian Salomond818ebf2018-07-02 14:08:49 +00003692 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07003693 this->disableWindowRectangles();
csmartdaltonc7d85332016-10-26 10:13:46 -07003694 this->disableStencil();
Brian Osmanf02fa6f2017-07-11 11:17:47 -04003695 if (this->glCaps().srgbWriteControl()) {
3696 this->flushFramebufferSRGB(true);
3697 }
bsalomon6df86402015-06-01 10:41:49 -07003698
3699 GL_CALL(DrawArrays(GR_GL_TRIANGLE_STRIP, 0, 4));
Brian Salomon71d9d842016-11-03 13:42:00 -04003700 this->unbindTextureFBOForPixelOps(GR_GL_FRAMEBUFFER, dst);
Brian Salomon1fabd512018-02-09 09:54:25 -05003701 this->didWriteToSurface(dst, dstOrigin, &dstRect);
bsalomon083617b2016-02-12 12:10:14 -08003702
cdaltone2e71c22016-04-07 18:13:29 -07003703 return true;
bsalomon6df86402015-06-01 10:41:49 -07003704}
3705
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003706void GrGLGpu::copySurfaceAsCopyTexSubImage(GrSurface* dst, GrSurfaceOrigin dstOrigin,
3707 GrSurface* src, GrSurfaceOrigin srcOrigin,
bsalomon6df86402015-06-01 10:41:49 -07003708 const SkIRect& srcRect,
3709 const SkIPoint& dstPoint) {
Greg Daniel26dbe3b2018-05-03 10:35:42 -04003710 SkASSERT(can_copy_texsubimage(dst, dstOrigin, src, srcOrigin, this->glCaps()));
bsalomon6df86402015-06-01 10:41:49 -07003711 GrGLIRect srcVP;
Brian Salomon71d9d842016-11-03 13:42:00 -04003712 this->bindSurfaceFBOForPixelOps(src, GR_GL_FRAMEBUFFER, &srcVP, kSrc_TempFBOTarget);
bsalomon083617b2016-02-12 12:10:14 -08003713 GrGLTexture* dstTex = static_cast<GrGLTexture *>(dst->asTexture());
bsalomon6df86402015-06-01 10:41:49 -07003714 SkASSERT(dstTex);
3715 // We modified the bound FBO
Robert Phillips294870f2016-11-11 12:38:40 -05003716 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon6df86402015-06-01 10:41:49 -07003717 GrGLIRect srcGLRect;
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003718 srcGLRect.setRelativeTo(srcVP, srcRect, srcOrigin);
bsalomon6df86402015-06-01 10:41:49 -07003719
Brian Salomond978b902019-02-07 15:09:18 -05003720 this->bindTextureToScratchUnit(dstTex->target(), dstTex->textureID());
bsalomon6df86402015-06-01 10:41:49 -07003721 GrGLint dstY;
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003722 if (kBottomLeft_GrSurfaceOrigin == dstOrigin) {
bsalomon6df86402015-06-01 10:41:49 -07003723 dstY = dst->height() - (dstPoint.fY + srcGLRect.fHeight);
3724 } else {
3725 dstY = dstPoint.fY;
3726 }
bsalomon10528f12015-10-14 12:54:52 -07003727 GL_CALL(CopyTexSubImage2D(dstTex->target(), 0,
bsalomon083617b2016-02-12 12:10:14 -08003728 dstPoint.fX, dstY,
3729 srcGLRect.fLeft, srcGLRect.fBottom,
3730 srcGLRect.fWidth, srcGLRect.fHeight));
Brian Salomon71d9d842016-11-03 13:42:00 -04003731 this->unbindTextureFBOForPixelOps(GR_GL_FRAMEBUFFER, src);
bsalomon083617b2016-02-12 12:10:14 -08003732 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
3733 srcRect.width(), srcRect.height());
Brian Salomon1fabd512018-02-09 09:54:25 -05003734 this->didWriteToSurface(dst, dstOrigin, &dstRect);
bsalomon6df86402015-06-01 10:41:49 -07003735}
3736
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003737bool GrGLGpu::copySurfaceAsBlitFramebuffer(GrSurface* dst, GrSurfaceOrigin dstOrigin,
3738 GrSurface* src, GrSurfaceOrigin srcOrigin,
bsalomon6df86402015-06-01 10:41:49 -07003739 const SkIRect& srcRect,
3740 const SkIPoint& dstPoint) {
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003741 SkASSERT(can_blit_framebuffer_for_copy_surface(dst, dstOrigin, src, srcOrigin,
Greg Daniel26dbe3b2018-05-03 10:35:42 -04003742 srcRect, dstPoint, this->glCaps()));
bsalomon6df86402015-06-01 10:41:49 -07003743 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
3744 srcRect.width(), srcRect.height());
3745 if (dst == src) {
3746 if (SkIRect::IntersectsNoEmptyCheck(dstRect, srcRect)) {
3747 return false;
mtklein404b3b22015-05-18 09:29:10 -07003748 }
bsalomon5df6fee2015-05-18 06:26:15 -07003749 }
bsalomon6df86402015-06-01 10:41:49 -07003750
bsalomon6df86402015-06-01 10:41:49 -07003751 GrGLIRect dstVP;
3752 GrGLIRect srcVP;
Brian Salomon71d9d842016-11-03 13:42:00 -04003753 this->bindSurfaceFBOForPixelOps(dst, GR_GL_DRAW_FRAMEBUFFER, &dstVP, kDst_TempFBOTarget);
3754 this->bindSurfaceFBOForPixelOps(src, GR_GL_READ_FRAMEBUFFER, &srcVP, kSrc_TempFBOTarget);
bsalomon6df86402015-06-01 10:41:49 -07003755 // We modified the bound FBO
Robert Phillips294870f2016-11-11 12:38:40 -05003756 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon6df86402015-06-01 10:41:49 -07003757 GrGLIRect srcGLRect;
3758 GrGLIRect dstGLRect;
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003759 srcGLRect.setRelativeTo(srcVP, srcRect, srcOrigin);
3760 dstGLRect.setRelativeTo(dstVP, dstRect, dstOrigin);
bsalomon6df86402015-06-01 10:41:49 -07003761
3762 // BlitFrameBuffer respects the scissor, so disable it.
Brian Salomond818ebf2018-07-02 14:08:49 +00003763 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07003764 this->disableWindowRectangles();
bsalomon6df86402015-06-01 10:41:49 -07003765
3766 GrGLint srcY0;
3767 GrGLint srcY1;
3768 // Does the blit need to y-mirror or not?
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003769 if (srcOrigin == dstOrigin) {
bsalomon6df86402015-06-01 10:41:49 -07003770 srcY0 = srcGLRect.fBottom;
3771 srcY1 = srcGLRect.fBottom + srcGLRect.fHeight;
3772 } else {
3773 srcY0 = srcGLRect.fBottom + srcGLRect.fHeight;
3774 srcY1 = srcGLRect.fBottom;
3775 }
3776 GL_CALL(BlitFramebuffer(srcGLRect.fLeft,
3777 srcY0,
3778 srcGLRect.fLeft + srcGLRect.fWidth,
3779 srcY1,
3780 dstGLRect.fLeft,
3781 dstGLRect.fBottom,
3782 dstGLRect.fLeft + dstGLRect.fWidth,
3783 dstGLRect.fBottom + dstGLRect.fHeight,
3784 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
Brian Salomon71d9d842016-11-03 13:42:00 -04003785 this->unbindTextureFBOForPixelOps(GR_GL_DRAW_FRAMEBUFFER, dst);
3786 this->unbindTextureFBOForPixelOps(GR_GL_READ_FRAMEBUFFER, src);
Brian Salomon1fabd512018-02-09 09:54:25 -05003787 this->didWriteToSurface(dst, dstOrigin, &dstRect);
bsalomon6df86402015-06-01 10:41:49 -07003788 return true;
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00003789}
3790
Brian Salomon930f9392018-06-20 16:25:26 -04003791bool GrGLGpu::onRegenerateMipMapLevels(GrTexture* texture) {
3792 auto glTex = static_cast<GrGLTexture*>(texture);
brianosman33f6b3f2016-06-02 05:49:21 -07003793 // Mipmaps are only supported on 2D textures:
Brian Salomon930f9392018-06-20 16:25:26 -04003794 if (GR_GL_TEXTURE_2D != glTex->target()) {
brianosman33f6b3f2016-06-02 05:49:21 -07003795 return false;
3796 }
3797
Brian Salomon930f9392018-06-20 16:25:26 -04003798 // Manual implementation of mipmap generation, to work around driver bugs w/sRGB.
3799 // Uses draw calls to do a series of downsample operations to successive mips.
3800
3801 // The manual approach requires the ability to limit which level we're sampling and that the
3802 // destination can be bound to a FBO:
3803 if (!this->glCaps().doManualMipmapping() ||
3804 !this->glCaps().canConfigBeFBOColorAttachment(texture->config())) {
3805 GrGLenum target = glTex->target();
Brian Salomond978b902019-02-07 15:09:18 -05003806 this->bindTextureToScratchUnit(target, glTex->textureID());
Brian Salomon930f9392018-06-20 16:25:26 -04003807 GL_CALL(GenerateMipmap(glTex->target()));
3808 return true;
brianosman33f6b3f2016-06-02 05:49:21 -07003809 }
3810
brianosman33f6b3f2016-06-02 05:49:21 -07003811 int width = texture->width();
3812 int height = texture->height();
3813 int levelCount = SkMipMap::ComputeLevelCount(width, height) + 1;
Greg Danielda86e282018-06-13 09:41:19 -04003814 SkASSERT(levelCount == texture->texturePriv().maxMipMapLevel() + 1);
brianosman33f6b3f2016-06-02 05:49:21 -07003815
3816 // Create (if necessary), then bind temporary FBO:
3817 if (0 == fTempDstFBOID) {
3818 GL_CALL(GenFramebuffers(1, &fTempDstFBOID));
3819 }
Adrienne Walker4ee88512018-05-17 11:37:14 -07003820 this->bindFramebuffer(GR_GL_FRAMEBUFFER, fTempDstFBOID);
Robert Phillips294870f2016-11-11 12:38:40 -05003821 fHWBoundRenderTargetUniqueID.makeInvalid();
brianosman33f6b3f2016-06-02 05:49:21 -07003822
3823 // Bind the texture, to get things configured for filtering.
3824 // We'll be changing our base level further below:
3825 this->setTextureUnit(0);
Brian Salomon930f9392018-06-20 16:25:26 -04003826 this->bindTexture(0, GrSamplerState::ClampBilerp(), glTex);
brianosman33f6b3f2016-06-02 05:49:21 -07003827
3828 // Vertex data:
3829 if (!fMipmapProgramArrayBuffer) {
3830 static const GrGLfloat vdata[] = {
3831 0, 0,
3832 0, 1,
3833 1, 0,
3834 1, 1
3835 };
Brian Salomonae64c192019-02-05 09:41:37 -05003836 fMipmapProgramArrayBuffer = GrGLBuffer::Make(this, sizeof(vdata), GrGpuBufferType::kVertex,
Brian Salomon12d22642019-01-29 14:38:50 -05003837 kStatic_GrAccessPattern, vdata);
brianosman33f6b3f2016-06-02 05:49:21 -07003838 }
3839 if (!fMipmapProgramArrayBuffer) {
3840 return false;
3841 }
3842
3843 fHWVertexArrayState.setVertexArrayID(this, 0);
3844
3845 GrGLAttribArrayState* attribs = fHWVertexArrayState.bindInternalVertexArray(this);
Chris Dalton8e45b4f2017-05-05 14:00:56 -04003846 attribs->enableVertexArrays(this, 1);
Brian Osmand4c29702018-09-14 16:16:55 -04003847 attribs->set(this, 0, fMipmapProgramArrayBuffer.get(), kFloat2_GrVertexAttribType,
Brian Osman4a3f5c82018-09-18 16:16:38 -04003848 kFloat2_GrSLType, 2 * sizeof(GrGLfloat), 0);
brianosman33f6b3f2016-06-02 05:49:21 -07003849
3850 // Set "simple" state once:
3851 GrXferProcessor::BlendInfo blendInfo;
3852 blendInfo.reset();
3853 this->flushBlend(blendInfo, GrSwizzle::RGBA());
3854 this->flushColorWrite(true);
Chris Dalton4c56b032019-03-04 12:28:42 -07003855 this->flushHWAAState(nullptr, false);
Brian Salomond818ebf2018-07-02 14:08:49 +00003856 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07003857 this->disableWindowRectangles();
csmartdaltonc7d85332016-10-26 10:13:46 -07003858 this->disableStencil();
brianosman33f6b3f2016-06-02 05:49:21 -07003859
3860 // Do all the blits:
3861 width = texture->width();
3862 height = texture->height();
3863 GrGLIRect viewport;
3864 viewport.fLeft = 0;
3865 viewport.fBottom = 0;
Brian Salomondc829942018-10-23 16:07:24 -04003866
brianosman33f6b3f2016-06-02 05:49:21 -07003867 for (GrGLint level = 1; level < levelCount; ++level) {
3868 // Get and bind the program for this particular downsample (filter shape can vary):
3869 int progIdx = TextureSizeToMipmapProgramIdx(width, height);
3870 if (!fMipmapPrograms[progIdx].fProgram) {
3871 if (!this->createMipmapProgram(progIdx)) {
3872 SkDebugf("Failed to create mipmap program.\n");
Brian Salomondc829942018-10-23 16:07:24 -04003873 // Invalidate all params to cover base level change in a previous iteration.
3874 glTex->textureParamsModified();
brianosman33f6b3f2016-06-02 05:49:21 -07003875 return false;
3876 }
3877 }
Brian Salomon802cb312018-06-08 18:05:20 -04003878 this->flushProgram(fMipmapPrograms[progIdx].fProgram);
brianosman33f6b3f2016-06-02 05:49:21 -07003879
3880 // Texcoord uniform is expected to contain (1/w, (w-1)/w, 1/h, (h-1)/h)
3881 const float invWidth = 1.0f / width;
3882 const float invHeight = 1.0f / height;
3883 GL_CALL(Uniform4f(fMipmapPrograms[progIdx].fTexCoordXformUniform,
3884 invWidth, (width - 1) * invWidth, invHeight, (height - 1) * invHeight));
3885 GL_CALL(Uniform1i(fMipmapPrograms[progIdx].fTextureUniform, 0));
3886
3887 // Only sample from previous mip
3888 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_BASE_LEVEL, level - 1));
3889
Brian Salomon930f9392018-06-20 16:25:26 -04003890 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0, GR_GL_TEXTURE_2D,
3891 glTex->textureID(), level));
brianosman33f6b3f2016-06-02 05:49:21 -07003892
3893 width = SkTMax(1, width / 2);
3894 height = SkTMax(1, height / 2);
3895 viewport.fWidth = width;
3896 viewport.fHeight = height;
3897 this->flushViewport(viewport);
3898
3899 GL_CALL(DrawArrays(GR_GL_TRIANGLE_STRIP, 0, 4));
3900 }
3901
3902 // Unbind:
3903 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
3904 GR_GL_TEXTURE_2D, 0, 0));
3905
Brian Salomon930f9392018-06-20 16:25:26 -04003906 // We modified the base level param.
Brian Salomondc829942018-10-23 16:07:24 -04003907 GrGLTexture::NonSamplerParams params = glTex->getCachedNonSamplerParams();
3908 params.fBaseMipMapLevel = levelCount - 2; // we drew the 2nd to last level into the last level.
3909 glTex->setCachedParams(nullptr, params, this->getResetTimestamp());
3910
brianosman33f6b3f2016-06-02 05:49:21 -07003911 return true;
3912}
3913
Chris Daltond7291ba2019-03-07 14:17:03 -07003914void GrGLGpu::querySampleLocations(
3915 GrRenderTarget* renderTarget, const GrStencilSettings& stencilSettings,
3916 SkTArray<SkPoint>* sampleLocations) {
3917 this->flushStencil(stencilSettings);
3918 this->flushHWAAState(renderTarget, true);
3919 this->flushRenderTarget(static_cast<GrGLRenderTarget*>(renderTarget));
3920
3921 int effectiveSampleCnt;
3922 GR_GL_GetIntegerv(this->glInterface(), GR_GL_SAMPLES, &effectiveSampleCnt);
3923 SkASSERT(effectiveSampleCnt >= renderTarget->numStencilSamples());
3924
3925 sampleLocations->reset(effectiveSampleCnt);
3926 for (int i = 0; i < effectiveSampleCnt; ++i) {
3927 GL_CALL(GetMultisamplefv(GR_GL_SAMPLE_POSITION, i, &(*sampleLocations)[i].fX));
3928 }
3929}
3930
cdalton231c5fd2015-05-13 12:35:36 -07003931void GrGLGpu::xferBarrier(GrRenderTarget* rt, GrXferBarrierType type) {
bsalomoncb02b382015-08-12 11:14:50 -07003932 SkASSERT(type);
cdalton9954bc32015-04-29 14:17:00 -07003933 switch (type) {
cdalton231c5fd2015-05-13 12:35:36 -07003934 case kTexture_GrXferBarrierType: {
3935 GrGLRenderTarget* glrt = static_cast<GrGLRenderTarget*>(rt);
Brian Osmancfe83d12018-01-19 11:13:45 -05003936 SkASSERT(glrt->textureFBOID() != 0 && glrt->renderFBOID() != 0);
cdalton231c5fd2015-05-13 12:35:36 -07003937 if (glrt->textureFBOID() != glrt->renderFBOID()) {
3938 // The render target uses separate storage so no need for glTextureBarrier.
3939 // FIXME: The render target will resolve automatically when its texture is bound,
3940 // but we could resolve only the bounds that will be read if we do it here instead.
3941 return;
3942 }
cdalton9954bc32015-04-29 14:17:00 -07003943 SkASSERT(this->caps()->textureBarrierSupport());
3944 GL_CALL(TextureBarrier());
3945 return;
cdalton231c5fd2015-05-13 12:35:36 -07003946 }
cdalton8917d622015-05-06 13:40:21 -07003947 case kBlend_GrXferBarrierType:
bsalomon4b91f762015-05-19 09:29:46 -07003948 SkASSERT(GrCaps::kAdvanced_BlendEquationSupport ==
cdalton8917d622015-05-06 13:40:21 -07003949 this->caps()->blendEquationSupport());
3950 GL_CALL(BlendBarrier());
3951 return;
bsalomoncb02b382015-08-12 11:14:50 -07003952 default: break; // placate compiler warnings that kNone not handled
cdalton9954bc32015-04-29 14:17:00 -07003953 }
3954}
3955
Brian Salomonf865b052018-03-09 09:01:53 -05003956#if GR_TEST_UTILS
Brian Salomon52e943a2018-03-13 09:32:39 -04003957GrBackendTexture GrGLGpu::createTestingOnlyBackendTexture(const void* pixels, int w, int h,
Robert Phillips646f6372018-09-25 09:31:10 -04003958 GrColorType colorType, bool /*isRT*/,
3959 GrMipMapped mipMapped,
3960 size_t rowBytes) {
Brian Salomon8a375832018-03-14 10:21:40 -04003961 this->handleDirtyContext();
Robert Phillips646f6372018-09-25 09:31:10 -04003962
3963 GrPixelConfig config = GrColorTypeToPixelConfig(colorType, GrSRGBEncoded::kNo);
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003964 if (!this->caps()->isConfigTexturable(config)) {
3965 return GrBackendTexture(); // invalid
3966 }
3967
Greg Daniel92cbf3f2018-04-12 16:50:17 -04003968 if (w > this->caps()->maxTextureSize() || h > this->caps()->maxTextureSize()) {
3969 return GrBackendTexture(); // invalid
3970 }
3971
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003972 // Currently we don't support uploading pixel data when mipped.
3973 if (pixels && GrMipMapped::kYes == mipMapped) {
3974 return GrBackendTexture(); // invalid
3975 }
3976
Robert Phillips646f6372018-09-25 09:31:10 -04003977 int bpp = GrColorTypeBytesPerPixel(colorType);
3978 const size_t trimRowBytes = w * bpp;
3979 if (!rowBytes) {
3980 rowBytes = trimRowBytes;
3981 }
3982
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003983 GrGLTextureInfo info;
3984 info.fTarget = GR_GL_TEXTURE_2D;
3985 info.fID = 0;
3986 GL_CALL(GenTextures(1, &info.fID));
Brian Salomon1f05d452019-02-08 12:33:08 -05003987 this->bindTextureToScratchUnit(info.fTarget, info.fID);
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003988 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, 1));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003989 GL_CALL(TexParameteri(info.fTarget, GR_GL_TEXTURE_MAG_FILTER, GR_GL_NEAREST));
3990 GL_CALL(TexParameteri(info.fTarget, GR_GL_TEXTURE_MIN_FILTER, GR_GL_NEAREST));
3991 GL_CALL(TexParameteri(info.fTarget, GR_GL_TEXTURE_WRAP_S, GR_GL_CLAMP_TO_EDGE));
3992 GL_CALL(TexParameteri(info.fTarget, GR_GL_TEXTURE_WRAP_T, GR_GL_CLAMP_TO_EDGE));
3993
Jim Van Verth1676cb92019-01-15 13:24:45 -05003994 // we have to do something special for compressed textures
3995 if (GrPixelConfigIsCompressed(config)) {
3996 GrGLenum internalFormat;
3997 const GrGLInterface* interface = this->glInterface();
3998 const GrGLCaps& caps = this->glCaps();
3999 if (!caps.getCompressedTexImageFormats(config, &internalFormat)) {
4000 return GrBackendTexture();
Robert Phillips646f6372018-09-25 09:31:10 -04004001 }
Robert Phillipsd21b2a52017-12-12 13:01:25 -05004002
Jim Van Verth1676cb92019-01-15 13:24:45 -05004003 GrMipLevel mipLevel = { pixels, rowBytes };
4004 if (!allocate_and_populate_compressed_texture(config, *interface, caps, info.fTarget,
4005 internalFormat, &mipLevel, 1,
4006 w, h)) {
4007 return GrBackendTexture();
4008 }
4009 } else {
4010 bool restoreGLRowLength = false;
4011 if (trimRowBytes != rowBytes && this->glCaps().unpackRowLengthSupport()) {
4012 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowBytes / bpp));
4013 restoreGLRowLength = true;
4014 }
4015
4016 GrGLenum internalFormat;
4017 GrGLenum externalFormat;
4018 GrGLenum externalType;
4019
4020 if (!this->glCaps().getTexImageFormats(config, config, &internalFormat, &externalFormat,
4021 &externalType)) {
4022 return GrBackendTexture(); // invalid
4023 }
4024
4025 info.fFormat = this->glCaps().configSizedInternalFormat(config);
4026
4027 this->unbindCpuToGpuXferBuffer();
4028
4029 // Figure out the number of mip levels.
4030 int mipLevels = 1;
4031 if (GrMipMapped::kYes == mipMapped) {
4032 mipLevels = SkMipMap::ComputeLevelCount(w, h) + 1;
4033 }
4034
4035 size_t baseLayerSize = bpp * w * h;
4036 SkAutoMalloc defaultStorage(baseLayerSize);
4037 if (!pixels) {
4038 // Fill in the texture with all zeros so we don't have random garbage
4039 pixels = defaultStorage.get();
4040 memset(defaultStorage.get(), 0, baseLayerSize);
4041 } else if (trimRowBytes != rowBytes && !restoreGLRowLength) {
4042 // We weren't able to use GR_GL_UNPACK_ROW_LENGTH so make a copy
4043 char* copy = (char*)defaultStorage.get();
4044 for (int y = 0; y < h; ++y) {
4045 memcpy(&copy[y*trimRowBytes], &((const char*)pixels)[y*rowBytes], trimRowBytes);
4046 }
4047 pixels = copy;
4048 }
4049
4050 int width = w;
4051 int height = h;
4052 for (int i = 0; i < mipLevels; ++i) {
4053 GL_CALL(TexImage2D(info.fTarget, i, internalFormat, width, height, 0, externalFormat,
4054 externalType, pixels));
4055 width = SkTMax(1, width / 2);
4056 height = SkTMax(1, height / 2);
4057 }
4058 if (restoreGLRowLength) {
4059 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
4060 }
Robert Phillipsd21b2a52017-12-12 13:01:25 -05004061 }
4062
Robert Phillipse8fabb22018-02-04 14:33:21 -05004063 // unbind the texture from the texture unit to avoid asserts
4064 GL_CALL(BindTexture(info.fTarget, 0));
4065
Greg Daniel108bb232018-07-03 16:18:29 -04004066 GrBackendTexture beTex = GrBackendTexture(w, h, mipMapped, info);
4067 // Lots of tests don't go through Skia's public interface which will set the config so for
4068 // testing we make sure we set a config here.
4069 beTex.setPixelConfig(config);
4070 return beTex;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05004071}
4072
4073bool GrGLGpu::isTestingOnlyBackendTexture(const GrBackendTexture& tex) const {
Greg Danielbdf12ad2018-10-12 09:31:11 -04004074 SkASSERT(GrBackendApi::kOpenGL == tex.backend());
Robert Phillipsd21b2a52017-12-12 13:01:25 -05004075
Greg Daniel52e16d92018-04-10 09:34:07 -04004076 GrGLTextureInfo info;
4077 if (!tex.getGLTextureInfo(&info)) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -05004078 return false;
4079 }
4080
4081 GrGLboolean result;
Greg Daniel52e16d92018-04-10 09:34:07 -04004082 GL_CALL_RET(result, IsTexture(info.fID));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05004083
4084 return (GR_GL_TRUE == result);
4085}
4086
Brian Salomon26102cb2018-03-09 09:33:19 -05004087void GrGLGpu::deleteTestingOnlyBackendTexture(const GrBackendTexture& tex) {
Greg Danielbdf12ad2018-10-12 09:31:11 -04004088 SkASSERT(GrBackendApi::kOpenGL == tex.backend());
Robert Phillipsd21b2a52017-12-12 13:01:25 -05004089
Greg Daniel52e16d92018-04-10 09:34:07 -04004090 GrGLTextureInfo info;
4091 if (tex.getGLTextureInfo(&info)) {
4092 GL_CALL(DeleteTextures(1, &info.fID));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05004093 }
4094}
4095
Brian Salomonf865b052018-03-09 09:01:53 -05004096GrBackendRenderTarget GrGLGpu::createTestingOnlyBackendRenderTarget(int w, int h,
Brian Osman2d010b62018-08-09 10:55:09 -04004097 GrColorType colorType) {
Greg Daniel92cbf3f2018-04-12 16:50:17 -04004098 if (w > this->caps()->maxRenderTargetSize() || h > this->caps()->maxRenderTargetSize()) {
4099 return GrBackendRenderTarget(); // invalid
4100 }
Brian Salomon8a375832018-03-14 10:21:40 -04004101 this->handleDirtyContext();
Brian Osman2d010b62018-08-09 10:55:09 -04004102 auto config = GrColorTypeToPixelConfig(colorType, GrSRGBEncoded::kNo);
Brian Salomon93348dd2018-08-29 12:56:23 -04004103 if (!this->glCaps().isConfigRenderable(config)) {
Brian Salomonf865b052018-03-09 09:01:53 -05004104 return {};
4105 }
Brian Salomon93348dd2018-08-29 12:56:23 -04004106 bool useTexture = false;
4107 GrGLenum colorBufferFormat;
4108 GrGLenum externalFormat = 0, externalType = 0;
4109 if (config == kBGRA_8888_GrPixelConfig && this->glCaps().bgraIsInternalFormat()) {
4110 // BGRA render buffers are not supported.
4111 this->glCaps().getTexImageFormats(config, config, &colorBufferFormat, &externalFormat,
4112 &externalType);
4113 useTexture = true;
4114 } else {
4115 this->glCaps().getRenderbufferFormat(config, &colorBufferFormat);
4116 }
Brian Salomonf865b052018-03-09 09:01:53 -05004117 int sFormatIdx = this->getCompatibleStencilIndex(config);
4118 if (sFormatIdx < 0) {
4119 return {};
4120 }
Brian Salomon93348dd2018-08-29 12:56:23 -04004121 GrGLuint colorID = 0;
4122 GrGLuint stencilID = 0;
4123 auto deleteIDs = [&] {
4124 if (colorID) {
4125 if (useTexture) {
4126 GL_CALL(DeleteTextures(1, &colorID));
4127 } else {
4128 GL_CALL(DeleteRenderbuffers(1, &colorID));
4129 }
Brian Salomonf865b052018-03-09 09:01:53 -05004130 }
Brian Salomon93348dd2018-08-29 12:56:23 -04004131 if (stencilID) {
4132 GL_CALL(DeleteRenderbuffers(1, &stencilID));
Brian Salomonf865b052018-03-09 09:01:53 -05004133 }
Brian Salomon93348dd2018-08-29 12:56:23 -04004134 };
4135
4136 if (useTexture) {
4137 GL_CALL(GenTextures(1, &colorID));
4138 } else {
4139 GL_CALL(GenRenderbuffers(1, &colorID));
4140 }
4141 GL_CALL(GenRenderbuffers(1, &stencilID));
4142 if (!stencilID || !colorID) {
4143 deleteIDs();
Brian Salomonf865b052018-03-09 09:01:53 -05004144 return {};
4145 }
Brian Salomon93348dd2018-08-29 12:56:23 -04004146
Brian Salomonf865b052018-03-09 09:01:53 -05004147 GrGLFramebufferInfo info;
4148 info.fFBOID = 0;
Brian Salomon93348dd2018-08-29 12:56:23 -04004149 this->glCaps().getSizedInternalFormat(config, &info.fFormat);
Brian Salomonf865b052018-03-09 09:01:53 -05004150 GL_CALL(GenFramebuffers(1, &info.fFBOID));
4151 if (!info.fFBOID) {
Brian Salomon93348dd2018-08-29 12:56:23 -04004152 deleteIDs();
4153 return {};
Brian Salomonf865b052018-03-09 09:01:53 -05004154 }
4155
4156 this->invalidateBoundRenderTarget();
4157
Adrienne Walker4ee88512018-05-17 11:37:14 -07004158 this->bindFramebuffer(GR_GL_FRAMEBUFFER, info.fFBOID);
Brian Salomon93348dd2018-08-29 12:56:23 -04004159 if (useTexture) {
Brian Salomond978b902019-02-07 15:09:18 -05004160 this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, colorID);
Brian Salomon93348dd2018-08-29 12:56:23 -04004161 GL_CALL(TexImage2D(GR_GL_TEXTURE_2D, 0, colorBufferFormat, w, h, 0, externalFormat,
4162 externalType, nullptr));
4163 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0, GR_GL_TEXTURE_2D,
4164 colorID, 0));
4165 } else {
4166 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, colorID));
4167 GL_ALLOC_CALL(this->glInterface(),
4168 RenderbufferStorage(GR_GL_RENDERBUFFER, colorBufferFormat, w, h));
4169 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
4170 GR_GL_RENDERBUFFER, colorID));
4171 }
4172 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, stencilID));
Brian Salomonf865b052018-03-09 09:01:53 -05004173 auto stencilBufferFormat = this->glCaps().stencilFormats()[sFormatIdx].fInternalFormat;
4174 GL_ALLOC_CALL(this->glInterface(),
4175 RenderbufferStorage(GR_GL_RENDERBUFFER, stencilBufferFormat, w, h));
4176 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT, GR_GL_RENDERBUFFER,
Brian Salomon93348dd2018-08-29 12:56:23 -04004177 stencilID));
Brian Salomonf865b052018-03-09 09:01:53 -05004178 if (this->glCaps().stencilFormats()[sFormatIdx].fPacked) {
4179 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_DEPTH_ATTACHMENT,
Brian Salomon93348dd2018-08-29 12:56:23 -04004180 GR_GL_RENDERBUFFER, stencilID));
Brian Salomonf865b052018-03-09 09:01:53 -05004181 }
4182
Brian Salomon93348dd2018-08-29 12:56:23 -04004183 // We don't want to have to recover the renderbuffer/texture IDs later to delete them. OpenGL
4184 // has this rule that if a renderbuffer/texture is deleted and a FBO other than the current FBO
4185 // has the RB attached then deletion is delayed. So we unbind the FBO here and delete the
4186 // renderbuffers/texture.
Adrienne Walker4ee88512018-05-17 11:37:14 -07004187 this->bindFramebuffer(GR_GL_FRAMEBUFFER, 0);
Brian Salomon93348dd2018-08-29 12:56:23 -04004188 deleteIDs();
Brian Salomonf865b052018-03-09 09:01:53 -05004189
Adrienne Walker4ee88512018-05-17 11:37:14 -07004190 this->bindFramebuffer(GR_GL_FRAMEBUFFER, info.fFBOID);
Brian Salomonf865b052018-03-09 09:01:53 -05004191 GrGLenum status;
4192 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
4193 if (GR_GL_FRAMEBUFFER_COMPLETE != status) {
Adrienne Walker4ee88512018-05-17 11:37:14 -07004194 this->deleteFramebuffer(info.fFBOID);
Brian Salomonf865b052018-03-09 09:01:53 -05004195 return {};
4196 }
4197 auto stencilBits = SkToInt(this->glCaps().stencilFormats()[sFormatIdx].fStencilBits);
Greg Daniel108bb232018-07-03 16:18:29 -04004198 GrBackendRenderTarget beRT = GrBackendRenderTarget(w, h, 1, stencilBits, info);
4199 // Lots of tests don't go through Skia's public interface which will set the config so for
4200 // testing we make sure we set a config here.
4201 beRT.setPixelConfig(config);
Brian Salomon93348dd2018-08-29 12:56:23 -04004202#ifdef SK_DEBUG
4203 SkColorType skColorType = GrColorTypeToSkColorType(colorType);
4204 if (skColorType != kUnknown_SkColorType) {
4205 SkASSERT(this->caps()->validateBackendRenderTarget(
Brian Salomonf391d0f2018-12-14 09:18:50 -05004206 beRT, GrColorTypeToSkColorType(colorType)) != kUnknown_GrPixelConfig);
Brian Salomon93348dd2018-08-29 12:56:23 -04004207 }
4208#endif
Greg Daniel108bb232018-07-03 16:18:29 -04004209 return beRT;
Brian Salomonf865b052018-03-09 09:01:53 -05004210}
4211
4212void GrGLGpu::deleteTestingOnlyBackendRenderTarget(const GrBackendRenderTarget& backendRT) {
Greg Danielbdf12ad2018-10-12 09:31:11 -04004213 SkASSERT(GrBackendApi::kOpenGL == backendRT.backend());
Greg Daniel323fbcf2018-04-10 13:46:30 -04004214 GrGLFramebufferInfo info;
4215 if (backendRT.getGLFramebufferInfo(&info)) {
4216 if (info.fFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -07004217 this->deleteFramebuffer(info.fFBOID);
Brian Salomonf865b052018-03-09 09:01:53 -05004218 }
4219 }
joshualitt8fd844f2015-12-02 13:36:47 -08004220}
4221
Greg Daniel26b50a42018-03-08 09:49:58 -05004222void GrGLGpu::testingOnly_flushGpuAndSync() {
4223 GL_CALL(Finish());
4224}
Brian Salomonf865b052018-03-09 09:01:53 -05004225#endif
Greg Daniel26b50a42018-03-08 09:49:58 -05004226
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00004227///////////////////////////////////////////////////////////////////////////////
bsalomon6df86402015-06-01 10:41:49 -07004228
cdaltone2e71c22016-04-07 18:13:29 -07004229GrGLAttribArrayState* GrGLGpu::HWVertexArrayState::bindInternalVertexArray(GrGLGpu* gpu,
csmartdalton485a1202016-07-13 10:16:32 -07004230 const GrBuffer* ibuf) {
robertphillips@google.com4f65a272013-03-26 19:40:46 +00004231 GrGLAttribArrayState* attribState;
4232
cdaltone2e71c22016-04-07 18:13:29 -07004233 if (gpu->glCaps().isCoreProfile()) {
4234 if (!fCoreProfileVertexArray) {
bsalomon@google.com6918d482013-03-07 19:09:11 +00004235 GrGLuint arrayID;
4236 GR_GL_CALL(gpu->glInterface(), GenVertexArrays(1, &arrayID));
4237 int attrCount = gpu->glCaps().maxVertexAttributes();
cdaltone2e71c22016-04-07 18:13:29 -07004238 fCoreProfileVertexArray = new GrGLVertexArray(arrayID, attrCount);
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00004239 }
cdaltone2e71c22016-04-07 18:13:29 -07004240 if (ibuf) {
4241 attribState = fCoreProfileVertexArray->bindWithIndexBuffer(gpu, ibuf);
bsalomon6df86402015-06-01 10:41:49 -07004242 } else {
cdaltone2e71c22016-04-07 18:13:29 -07004243 attribState = fCoreProfileVertexArray->bind(gpu);
bsalomon6df86402015-06-01 10:41:49 -07004244 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00004245 } else {
cdaltone2e71c22016-04-07 18:13:29 -07004246 if (ibuf) {
4247 // bindBuffer implicitly binds VAO 0 when binding an index buffer.
Brian Salomonae64c192019-02-05 09:41:37 -05004248 gpu->bindBuffer(GrGpuBufferType::kIndex, ibuf);
bsalomon@google.com6918d482013-03-07 19:09:11 +00004249 } else {
4250 this->setVertexArrayID(gpu, 0);
4251 }
4252 int attrCount = gpu->glCaps().maxVertexAttributes();
4253 if (fDefaultVertexArrayAttribState.count() != attrCount) {
4254 fDefaultVertexArrayAttribState.resize(attrCount);
4255 }
4256 attribState = &fDefaultVertexArrayAttribState;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00004257 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00004258 return attribState;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00004259}
bsalomone179a912016-01-20 06:18:10 -08004260
Greg Danielbae71212019-03-01 15:24:35 -05004261void GrGLGpu::onFinishFlush(GrSurfaceProxy*, SkSurface::BackendSurfaceAccess access,
4262 SkSurface::FlushFlags flags, bool insertedSemaphore) {
Greg Daniel51316782017-08-02 15:10:09 +00004263 // If we inserted semaphores during the flush, we need to call GLFlush.
4264 if (insertedSemaphore) {
4265 GL_CALL(Flush());
4266 }
Greg Danielbae71212019-03-01 15:24:35 -05004267 if (flags & SkSurface::kSyncCpu_FlushFlag) {
4268 GL_CALL(Finish());
4269 }
Greg Daniel51316782017-08-02 15:10:09 +00004270}
4271
Robert Phillips5b5d84c2018-08-09 15:12:18 -04004272void GrGLGpu::submit(GrGpuCommandBuffer* buffer) {
4273 if (buffer->asRTCommandBuffer()) {
4274 SkASSERT(fCachedRTCommandBuffer.get() == buffer);
4275 fCachedRTCommandBuffer->reset();
4276 } else {
4277 SkASSERT(fCachedTexCommandBuffer.get() == buffer);
4278 fCachedTexCommandBuffer->reset();
4279 }
4280}
4281
Greg Daniel6be35232017-03-01 17:01:09 -05004282GrFence SK_WARN_UNUSED_RESULT GrGLGpu::insertFence() {
Greg Danielc64ee462017-06-15 16:59:49 -04004283 SkASSERT(this->caps()->fenceSyncSupport());
Greg Daniel6be35232017-03-01 17:01:09 -05004284 GrGLsync sync;
4285 GL_CALL_RET(sync, FenceSync(GR_GL_SYNC_GPU_COMMANDS_COMPLETE, 0));
4286 GR_STATIC_ASSERT(sizeof(GrFence) >= sizeof(GrGLsync));
4287 return (GrFence)sync;
jvanverth84741b32016-09-30 08:39:02 -07004288}
4289
Greg Daniel6be35232017-03-01 17:01:09 -05004290bool GrGLGpu::waitFence(GrFence fence, uint64_t timeout) {
jvanverth84741b32016-09-30 08:39:02 -07004291 GrGLenum result;
4292 GL_CALL_RET(result, ClientWaitSync((GrGLsync)fence, GR_GL_SYNC_FLUSH_COMMANDS_BIT, timeout));
4293 return (GR_GL_CONDITION_SATISFIED == result);
4294}
4295
4296void GrGLGpu::deleteFence(GrFence fence) const {
Greg Daniel6be35232017-03-01 17:01:09 -05004297 this->deleteSync((GrGLsync)fence);
4298}
4299
Greg Daniela5cb7812017-06-16 09:45:32 -04004300sk_sp<GrSemaphore> SK_WARN_UNUSED_RESULT GrGLGpu::makeSemaphore(bool isOwned) {
Greg Danielc64ee462017-06-15 16:59:49 -04004301 SkASSERT(this->caps()->fenceSyncSupport());
Greg Daniela5cb7812017-06-16 09:45:32 -04004302 return GrGLSemaphore::Make(this, isOwned);
Greg Daniel6be35232017-03-01 17:01:09 -05004303}
4304
Greg Daniel48661b82018-01-22 16:11:35 -05004305sk_sp<GrSemaphore> GrGLGpu::wrapBackendSemaphore(const GrBackendSemaphore& semaphore,
4306 GrResourceProvider::SemaphoreWrapType wrapType,
4307 GrWrapOwnership ownership) {
Greg Danielc64ee462017-06-15 16:59:49 -04004308 SkASSERT(this->caps()->fenceSyncSupport());
Greg Daniela5cb7812017-06-16 09:45:32 -04004309 return GrGLSemaphore::MakeWrapped(this, semaphore.glSync(), ownership);
4310}
4311
Greg Daniel858e12c2018-12-06 11:11:37 -05004312void GrGLGpu::insertSemaphore(sk_sp<GrSemaphore> semaphore) {
Greg Daniel6be35232017-03-01 17:01:09 -05004313 GrGLSemaphore* glSem = static_cast<GrGLSemaphore*>(semaphore.get());
4314
Greg Daniel48661b82018-01-22 16:11:35 -05004315 GrGLsync sync;
4316 GL_CALL_RET(sync, FenceSync(GR_GL_SYNC_GPU_COMMANDS_COMPLETE, 0));
4317 glSem->setSync(sync);
Greg Daniel6be35232017-03-01 17:01:09 -05004318}
4319
Greg Daniel48661b82018-01-22 16:11:35 -05004320void GrGLGpu::waitSemaphore(sk_sp<GrSemaphore> semaphore) {
Greg Daniel6be35232017-03-01 17:01:09 -05004321 GrGLSemaphore* glSem = static_cast<GrGLSemaphore*>(semaphore.get());
4322
4323 GL_CALL(WaitSync(glSem->sync(), 0, GR_GL_TIMEOUT_IGNORED));
4324}
4325
4326void GrGLGpu::deleteSync(GrGLsync sync) const {
4327 GL_CALL(DeleteSync(sync));
jvanverth84741b32016-09-30 08:39:02 -07004328}
Brian Osman13dddce2017-05-09 13:19:50 -04004329
Robert Phillips65a88fa2017-08-08 08:36:22 -04004330void GrGLGpu::insertEventMarker(const char* msg) {
4331 GL_CALL(InsertEventMarker(strlen(msg), msg));
4332}
4333
Brian Osman13dddce2017-05-09 13:19:50 -04004334sk_sp<GrSemaphore> GrGLGpu::prepareTextureForCrossContextUsage(GrTexture* texture) {
4335 // Set up a semaphore to be signaled once the data is ready, and flush GL
Greg Daniela5cb7812017-06-16 09:45:32 -04004336 sk_sp<GrSemaphore> semaphore = this->makeSemaphore(true);
Greg Daniel858e12c2018-12-06 11:11:37 -05004337 this->insertSemaphore(semaphore);
4338 // We must call flush here to make sure the GrGLSync object gets created and sent to the gpu.
4339 GL_CALL(Flush());
Brian Osman13dddce2017-05-09 13:19:50 -04004340
4341 return semaphore;
4342}
Robert Phillips646e4292017-06-13 12:44:56 -04004343
4344int GrGLGpu::TextureToCopyProgramIdx(GrTexture* texture) {
Brian Salomon60dd8c72018-07-30 10:24:13 -04004345 switch (GrSLCombinedSamplerTypeForTextureType(texture->texturePriv().textureType())) {
Robert Phillips646e4292017-06-13 12:44:56 -04004346 case kTexture2DSampler_GrSLType:
4347 return 0;
Robert Phillips646e4292017-06-13 12:44:56 -04004348 case kTexture2DRectSampler_GrSLType:
Brian Salomon57111332018-02-05 15:55:54 -05004349 return 1;
Robert Phillips646e4292017-06-13 12:44:56 -04004350 case kTextureExternalSampler_GrSLType:
Brian Salomon57111332018-02-05 15:55:54 -05004351 return 2;
Robert Phillips646e4292017-06-13 12:44:56 -04004352 default:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -04004353 SK_ABORT("Unexpected samper type");
Robert Phillips646e4292017-06-13 12:44:56 -04004354 return 0;
4355 }
4356}
Brian Osman71a18892017-08-10 10:23:25 -04004357
Kevin Lubickf4def342018-10-04 12:52:50 -04004358#ifdef SK_ENABLE_DUMP_GPU
4359#include "SkJSONWriter.h"
Brian Osman71a18892017-08-10 10:23:25 -04004360void GrGLGpu::onDumpJSON(SkJSONWriter* writer) const {
4361 // We are called by the base class, which has already called beginObject(). We choose to nest
4362 // all of our caps information in a named sub-object.
4363 writer->beginObject("GL GPU");
4364
4365 const GrGLubyte* str;
4366 GL_CALL_RET(str, GetString(GR_GL_VERSION));
4367 writer->appendString("GL_VERSION", (const char*)(str));
4368 GL_CALL_RET(str, GetString(GR_GL_RENDERER));
4369 writer->appendString("GL_RENDERER", (const char*)(str));
4370 GL_CALL_RET(str, GetString(GR_GL_VENDOR));
4371 writer->appendString("GL_VENDOR", (const char*)(str));
4372 GL_CALL_RET(str, GetString(GR_GL_SHADING_LANGUAGE_VERSION));
4373 writer->appendString("GL_SHADING_LANGUAGE_VERSION", (const char*)(str));
4374
4375 writer->appendName("extensions");
4376 glInterface()->fExtensions.dumpJSON(writer);
4377
4378 writer->endObject();
4379}
Kevin Lubickf4def342018-10-04 12:52:50 -04004380#endif