blob: d90a91eb89095538e7f46371de1e77d0beb18152 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkPixmap.h"
9#include "include/core/SkStrokeRec.h"
10#include "include/core/SkTypes.h"
11#include "include/gpu/GrBackendSemaphore.h"
12#include "include/gpu/GrBackendSurface.h"
13#include "include/gpu/GrTypes.h"
14#include "include/private/SkHalf.h"
15#include "include/private/SkTemplates.h"
16#include "include/private/SkTo.h"
17#include "src/core/SkAutoMalloc.h"
18#include "src/core/SkConvertPixels.h"
19#include "src/core/SkMakeUnique.h"
20#include "src/core/SkMipMap.h"
21#include "src/core/SkTraceEvent.h"
22#include "src/gpu/GrCpuBuffer.h"
23#include "src/gpu/GrFixedClip.h"
24#include "src/gpu/GrGpuResourcePriv.h"
25#include "src/gpu/GrMesh.h"
26#include "src/gpu/GrPipeline.h"
27#include "src/gpu/GrRenderTargetPriv.h"
28#include "src/gpu/GrShaderCaps.h"
29#include "src/gpu/GrSurfaceProxyPriv.h"
30#include "src/gpu/GrTexturePriv.h"
31#include "src/gpu/gl/GrGLBuffer.h"
32#include "src/gpu/gl/GrGLGpu.h"
33#include "src/gpu/gl/GrGLGpuCommandBuffer.h"
34#include "src/gpu/gl/GrGLSemaphore.h"
35#include "src/gpu/gl/GrGLStencilAttachment.h"
36#include "src/gpu/gl/GrGLTextureRenderTarget.h"
37#include "src/gpu/gl/builders/GrGLShaderStringBuilder.h"
38#include "src/sksl/SkSLCompiler.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000039
Hal Canaryc640d0d2018-06-13 09:59:02 -040040#include <cmath>
41
bsalomon@google.com0b77d682011-08-19 13:28:54 +000042#define GL_CALL(X) GR_GL_CALL(this->glInterface(), X)
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +000043#define GL_CALL_RET(RET, X) GR_GL_CALL_RET(this->glInterface(), RET, X)
bsalomon@google.com0b77d682011-08-19 13:28:54 +000044
reed@google.comac10a2d2010-12-22 21:39:39 +000045#define SKIP_CACHE_CHECK true
46
bsalomon@google.com4f3c2532012-01-19 16:16:52 +000047#if GR_GL_CHECK_ALLOC_WITH_GET_ERROR
48 #define CLEAR_ERROR_BEFORE_ALLOC(iface) GrGLClearErr(iface)
49 #define GL_ALLOC_CALL(iface, call) GR_GL_CALL_NOERRCHECK(iface, call)
50 #define CHECK_ALLOC_ERROR(iface) GR_GL_GET_ERROR(iface)
rmistry@google.comfbfcd562012-08-23 18:09:54 +000051#else
bsalomon@google.com4f3c2532012-01-19 16:16:52 +000052 #define CLEAR_ERROR_BEFORE_ALLOC(iface)
53 #define GL_ALLOC_CALL(iface, call) GR_GL_CALL(iface, call)
54 #define CHECK_ALLOC_ERROR(iface) GR_GL_NO_ERROR
55#endif
56
Jim Van Verth32ac83e2016-11-28 15:23:57 -050057//#define USE_NSIGHT
58
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +000059///////////////////////////////////////////////////////////////////////////////
60
cdalton8917d622015-05-06 13:40:21 -070061static const GrGLenum gXfermodeEquation2Blend[] = {
62 // Basic OpenGL blend equations.
63 GR_GL_FUNC_ADD,
64 GR_GL_FUNC_SUBTRACT,
65 GR_GL_FUNC_REVERSE_SUBTRACT,
66
67 // GL_KHR_blend_equation_advanced.
68 GR_GL_SCREEN,
69 GR_GL_OVERLAY,
70 GR_GL_DARKEN,
71 GR_GL_LIGHTEN,
72 GR_GL_COLORDODGE,
73 GR_GL_COLORBURN,
74 GR_GL_HARDLIGHT,
75 GR_GL_SOFTLIGHT,
76 GR_GL_DIFFERENCE,
77 GR_GL_EXCLUSION,
78 GR_GL_MULTIPLY,
79 GR_GL_HSL_HUE,
80 GR_GL_HSL_SATURATION,
81 GR_GL_HSL_COLOR,
Mike Klein36743362018-11-06 08:23:30 -050082 GR_GL_HSL_LUMINOSITY,
83
84 // Illegal... needs to map to something.
85 GR_GL_FUNC_ADD,
cdalton8917d622015-05-06 13:40:21 -070086};
87GR_STATIC_ASSERT(0 == kAdd_GrBlendEquation);
88GR_STATIC_ASSERT(1 == kSubtract_GrBlendEquation);
89GR_STATIC_ASSERT(2 == kReverseSubtract_GrBlendEquation);
90GR_STATIC_ASSERT(3 == kScreen_GrBlendEquation);
91GR_STATIC_ASSERT(4 == kOverlay_GrBlendEquation);
92GR_STATIC_ASSERT(5 == kDarken_GrBlendEquation);
93GR_STATIC_ASSERT(6 == kLighten_GrBlendEquation);
94GR_STATIC_ASSERT(7 == kColorDodge_GrBlendEquation);
95GR_STATIC_ASSERT(8 == kColorBurn_GrBlendEquation);
96GR_STATIC_ASSERT(9 == kHardLight_GrBlendEquation);
97GR_STATIC_ASSERT(10 == kSoftLight_GrBlendEquation);
98GR_STATIC_ASSERT(11 == kDifference_GrBlendEquation);
99GR_STATIC_ASSERT(12 == kExclusion_GrBlendEquation);
100GR_STATIC_ASSERT(13 == kMultiply_GrBlendEquation);
101GR_STATIC_ASSERT(14 == kHSLHue_GrBlendEquation);
102GR_STATIC_ASSERT(15 == kHSLSaturation_GrBlendEquation);
103GR_STATIC_ASSERT(16 == kHSLColor_GrBlendEquation);
104GR_STATIC_ASSERT(17 == kHSLLuminosity_GrBlendEquation);
bsalomonf7cc8772015-05-11 11:21:14 -0700105GR_STATIC_ASSERT(SK_ARRAY_COUNT(gXfermodeEquation2Blend) == kGrBlendEquationCnt);
cdalton8917d622015-05-06 13:40:21 -0700106
twiz@google.com0f31ca72011-03-18 17:38:11 +0000107static const GrGLenum gXfermodeCoeff2Blend[] = {
108 GR_GL_ZERO,
109 GR_GL_ONE,
110 GR_GL_SRC_COLOR,
111 GR_GL_ONE_MINUS_SRC_COLOR,
112 GR_GL_DST_COLOR,
113 GR_GL_ONE_MINUS_DST_COLOR,
114 GR_GL_SRC_ALPHA,
115 GR_GL_ONE_MINUS_SRC_ALPHA,
116 GR_GL_DST_ALPHA,
117 GR_GL_ONE_MINUS_DST_ALPHA,
118 GR_GL_CONSTANT_COLOR,
119 GR_GL_ONE_MINUS_CONSTANT_COLOR,
120 GR_GL_CONSTANT_ALPHA,
121 GR_GL_ONE_MINUS_CONSTANT_ALPHA,
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000122
123 // extended blend coeffs
124 GR_GL_SRC1_COLOR,
125 GR_GL_ONE_MINUS_SRC1_COLOR,
126 GR_GL_SRC1_ALPHA,
127 GR_GL_ONE_MINUS_SRC1_ALPHA,
Mike Klein36743362018-11-06 08:23:30 -0500128
129 // Illegal... needs to map to something.
130 GR_GL_ZERO,
reed@google.comac10a2d2010-12-22 21:39:39 +0000131};
132
bsalomon861e1032014-12-16 07:33:49 -0800133bool GrGLGpu::BlendCoeffReferencesConstant(GrBlendCoeff coeff) {
bsalomon@google.com080773c2011-03-15 19:09:25 +0000134 static const bool gCoeffReferencesBlendConst[] = {
135 false,
136 false,
137 false,
138 false,
139 false,
140 false,
141 false,
142 false,
143 false,
144 false,
145 true,
146 true,
147 true,
148 true,
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000149
150 // extended blend coeffs
151 false,
152 false,
153 false,
154 false,
Mike Klein36743362018-11-06 08:23:30 -0500155
156 // Illegal.
157 false,
bsalomon@google.com080773c2011-03-15 19:09:25 +0000158 };
159 return gCoeffReferencesBlendConst[coeff];
bsalomonf7cc8772015-05-11 11:21:14 -0700160 GR_STATIC_ASSERT(kGrBlendCoeffCnt == SK_ARRAY_COUNT(gCoeffReferencesBlendConst));
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000161
bsalomon@google.com47059542012-06-06 20:51:20 +0000162 GR_STATIC_ASSERT(0 == kZero_GrBlendCoeff);
163 GR_STATIC_ASSERT(1 == kOne_GrBlendCoeff);
164 GR_STATIC_ASSERT(2 == kSC_GrBlendCoeff);
165 GR_STATIC_ASSERT(3 == kISC_GrBlendCoeff);
166 GR_STATIC_ASSERT(4 == kDC_GrBlendCoeff);
167 GR_STATIC_ASSERT(5 == kIDC_GrBlendCoeff);
168 GR_STATIC_ASSERT(6 == kSA_GrBlendCoeff);
169 GR_STATIC_ASSERT(7 == kISA_GrBlendCoeff);
170 GR_STATIC_ASSERT(8 == kDA_GrBlendCoeff);
171 GR_STATIC_ASSERT(9 == kIDA_GrBlendCoeff);
172 GR_STATIC_ASSERT(10 == kConstC_GrBlendCoeff);
173 GR_STATIC_ASSERT(11 == kIConstC_GrBlendCoeff);
174 GR_STATIC_ASSERT(12 == kConstA_GrBlendCoeff);
175 GR_STATIC_ASSERT(13 == kIConstA_GrBlendCoeff);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000176
bsalomon@google.com47059542012-06-06 20:51:20 +0000177 GR_STATIC_ASSERT(14 == kS2C_GrBlendCoeff);
178 GR_STATIC_ASSERT(15 == kIS2C_GrBlendCoeff);
179 GR_STATIC_ASSERT(16 == kS2A_GrBlendCoeff);
180 GR_STATIC_ASSERT(17 == kIS2A_GrBlendCoeff);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000181
182 // assertion for gXfermodeCoeff2Blend have to be in GrGpu scope
bsalomonf7cc8772015-05-11 11:21:14 -0700183 GR_STATIC_ASSERT(kGrBlendCoeffCnt == SK_ARRAY_COUNT(gXfermodeCoeff2Blend));
bsalomon@google.com080773c2011-03-15 19:09:25 +0000184}
185
Brian Salomond978b902019-02-07 15:09:18 -0500186//////////////////////////////////////////////////////////////////////////////
187
188static int gl_target_to_binding_index(GrGLenum target) {
189 switch (target) {
190 case GR_GL_TEXTURE_2D:
191 return 0;
192 case GR_GL_TEXTURE_RECTANGLE:
193 return 1;
194 case GR_GL_TEXTURE_EXTERNAL:
195 return 2;
196 }
197 SK_ABORT("Unexpected GL texture target.");
198 return 0;
199}
200
201GrGpuResource::UniqueID GrGLGpu::TextureUnitBindings::boundID(GrGLenum target) const {
Brian Salomon1f05d452019-02-08 12:33:08 -0500202 return fTargetBindings[gl_target_to_binding_index(target)].fBoundResourceID;
203}
204
205bool GrGLGpu::TextureUnitBindings::hasBeenModified(GrGLenum target) const {
206 return fTargetBindings[gl_target_to_binding_index(target)].fHasBeenModified;
Brian Salomond978b902019-02-07 15:09:18 -0500207}
208
209void GrGLGpu::TextureUnitBindings::setBoundID(GrGLenum target, GrGpuResource::UniqueID resourceID) {
Brian Salomon1f05d452019-02-08 12:33:08 -0500210 int targetIndex = gl_target_to_binding_index(target);
211 fTargetBindings[targetIndex].fBoundResourceID = resourceID;
212 fTargetBindings[targetIndex].fHasBeenModified = true;
Brian Salomond978b902019-02-07 15:09:18 -0500213}
214
Brian Salomon1f05d452019-02-08 12:33:08 -0500215void GrGLGpu::TextureUnitBindings::invalidateForScratchUse(GrGLenum target) {
216 this->setBoundID(target, GrGpuResource::UniqueID());
Brian Salomond978b902019-02-07 15:09:18 -0500217}
218
Brian Salomon1f05d452019-02-08 12:33:08 -0500219void GrGLGpu::TextureUnitBindings::invalidateAllTargets(bool markUnmodified) {
220 for (auto& targetBinding : fTargetBindings) {
221 targetBinding.fBoundResourceID.makeInvalid();
222 if (markUnmodified) {
223 targetBinding.fHasBeenModified = false;
224 }
Brian Salomond978b902019-02-07 15:09:18 -0500225 }
226}
227
228//////////////////////////////////////////////////////////////////////////////
229
Brian Salomondc829942018-10-23 16:07:24 -0400230static GrGLenum filter_to_gl_mag_filter(GrSamplerState::Filter filter) {
231 switch (filter) {
232 case GrSamplerState::Filter::kNearest: return GR_GL_NEAREST;
233 case GrSamplerState::Filter::kBilerp: return GR_GL_LINEAR;
234 case GrSamplerState::Filter::kMipMap: return GR_GL_LINEAR;
235 }
236 SK_ABORT("Unknown filter");
237 return 0;
238}
239
240static GrGLenum filter_to_gl_min_filter(GrSamplerState::Filter filter) {
241 switch (filter) {
242 case GrSamplerState::Filter::kNearest: return GR_GL_NEAREST;
243 case GrSamplerState::Filter::kBilerp: return GR_GL_LINEAR;
244 case GrSamplerState::Filter::kMipMap: return GR_GL_LINEAR_MIPMAP_LINEAR;
245 }
246 SK_ABORT("Unknown filter");
247 return 0;
248}
249
Michael Ludwigf23a1522018-12-10 11:36:13 -0500250static inline GrGLenum wrap_mode_to_gl_wrap(GrSamplerState::WrapMode wrapMode,
251 const GrCaps& caps) {
Brian Salomondc829942018-10-23 16:07:24 -0400252 switch (wrapMode) {
253 case GrSamplerState::WrapMode::kClamp: return GR_GL_CLAMP_TO_EDGE;
254 case GrSamplerState::WrapMode::kRepeat: return GR_GL_REPEAT;
255 case GrSamplerState::WrapMode::kMirrorRepeat: return GR_GL_MIRRORED_REPEAT;
Michael Ludwigf23a1522018-12-10 11:36:13 -0500256 case GrSamplerState::WrapMode::kClampToBorder:
257 // May not be supported but should have been caught earlier
258 SkASSERT(caps.clampToBorderSupport());
259 return GR_GL_CLAMP_TO_BORDER;
Brian Salomon23356442018-11-30 15:33:19 -0500260 }
Brian Salomondc829942018-10-23 16:07:24 -0400261 SK_ABORT("Unknown wrap mode");
262 return 0;
263}
264
265///////////////////////////////////////////////////////////////////////////////
266
267class GrGLGpu::SamplerObjectCache {
268public:
269 SamplerObjectCache(GrGLGpu* gpu) : fGpu(gpu) {
270 fNumTextureUnits = fGpu->glCaps().shaderCaps()->maxFragmentSamplers();
271 fHWBoundSamplers.reset(new GrGLuint[fNumTextureUnits]);
272 std::fill_n(fHWBoundSamplers.get(), fNumTextureUnits, 0);
273 std::fill_n(fSamplers, kNumSamplers, 0);
274 }
275
276 ~SamplerObjectCache() {
277 if (!fNumTextureUnits) {
278 // We've already been abandoned.
279 return;
280 }
281 GR_GL_CALL(fGpu->glInterface(), DeleteSamplers(kNumSamplers, fSamplers));
282 }
283
284 void bindSampler(int unitIdx, const GrSamplerState& state) {
285 int index = StateToIndex(state);
286 if (!fSamplers[index]) {
287 GrGLuint s;
288 GR_GL_CALL(fGpu->glInterface(), GenSamplers(1, &s));
289 if (!s) {
290 return;
291 }
292 fSamplers[index] = s;
293 auto minFilter = filter_to_gl_min_filter(state.filter());
294 auto magFilter = filter_to_gl_mag_filter(state.filter());
Michael Ludwigf23a1522018-12-10 11:36:13 -0500295 auto wrapX = wrap_mode_to_gl_wrap(state.wrapModeX(), fGpu->glCaps());
296 auto wrapY = wrap_mode_to_gl_wrap(state.wrapModeY(), fGpu->glCaps());
Brian Salomondc829942018-10-23 16:07:24 -0400297 GR_GL_CALL(fGpu->glInterface(),
298 SamplerParameteri(s, GR_GL_TEXTURE_MIN_FILTER, minFilter));
299 GR_GL_CALL(fGpu->glInterface(),
300 SamplerParameteri(s, GR_GL_TEXTURE_MAG_FILTER, magFilter));
301 GR_GL_CALL(fGpu->glInterface(), SamplerParameteri(s, GR_GL_TEXTURE_WRAP_S, wrapX));
302 GR_GL_CALL(fGpu->glInterface(), SamplerParameteri(s, GR_GL_TEXTURE_WRAP_T, wrapY));
303 }
304 if (fHWBoundSamplers[unitIdx] != fSamplers[index]) {
305 GR_GL_CALL(fGpu->glInterface(), BindSampler(unitIdx, fSamplers[index]));
306 fHWBoundSamplers[unitIdx] = fSamplers[index];
307 }
308 }
309
310 void invalidateBindings() {
311 // When we have sampler support we always use samplers. So setting these to zero will cause
312 // a rebind on next usage.
313 std::fill_n(fHWBoundSamplers.get(), fNumTextureUnits, 0);
314 }
315
316 void abandon() {
317 fHWBoundSamplers.reset();
318 fNumTextureUnits = 0;
319 }
320
321 void release() {
322 if (!fNumTextureUnits) {
323 // We've already been abandoned.
324 return;
325 }
326 GR_GL_CALL(fGpu->glInterface(), DeleteSamplers(kNumSamplers, fSamplers));
327 std::fill_n(fSamplers, kNumSamplers, 0);
328 // Deleting a bound sampler implicitly binds sampler 0.
329 std::fill_n(fHWBoundSamplers.get(), fNumTextureUnits, 0);
330 }
331
332private:
333 static int StateToIndex(const GrSamplerState& state) {
334 int filter = static_cast<int>(state.filter());
335 SkASSERT(filter >= 0 && filter < 3);
336 int wrapX = static_cast<int>(state.wrapModeX());
Michael Ludwigf23a1522018-12-10 11:36:13 -0500337 SkASSERT(wrapX >= 0 && wrapX < 4);
Brian Salomondc829942018-10-23 16:07:24 -0400338 int wrapY = static_cast<int>(state.wrapModeY());
Michael Ludwigf23a1522018-12-10 11:36:13 -0500339 SkASSERT(wrapY >= 0 && wrapY < 4);
340 int idx = 16 * filter + 4 * wrapX + wrapY;
Brian Salomondc829942018-10-23 16:07:24 -0400341 SkASSERT(idx < kNumSamplers);
342 return idx;
343 }
344
345 GrGLGpu* fGpu;
Michael Ludwigf23a1522018-12-10 11:36:13 -0500346 static constexpr int kNumSamplers = 48;
Brian Salomondc829942018-10-23 16:07:24 -0400347 std::unique_ptr<GrGLuint[]> fHWBoundSamplers;
348 GrGLuint fSamplers[kNumSamplers];
349 int fNumTextureUnits;
350};
351
reed@google.comac10a2d2010-12-22 21:39:39 +0000352///////////////////////////////////////////////////////////////////////////////
353
Brian Salomon384fab42017-12-07 12:33:05 -0500354sk_sp<GrGpu> GrGLGpu::Make(sk_sp<const GrGLInterface> interface, const GrContextOptions& options,
355 GrContext* context) {
356 if (!interface) {
Brian Salomon3d6801e2017-12-11 10:06:31 -0500357 interface = GrGLMakeNativeInterface();
358 // For clients that have written their own GrGLCreateNativeInterface and haven't yet updated
359 // to GrGLMakeNativeInterface.
360 if (!interface) {
361 interface = sk_ref_sp(GrGLCreateNativeInterface());
362 }
Brian Salomon384fab42017-12-07 12:33:05 -0500363 if (!interface) {
364 return nullptr;
365 }
bsalomon424cc262015-05-22 10:37:30 -0700366 }
Jim Van Verth76334772017-05-05 16:46:05 -0400367#ifdef USE_NSIGHT
368 const_cast<GrContextOptions&>(options).fSuppressPathRendering = true;
369#endif
Brian Salomon8ab1cc42017-12-07 12:40:00 -0500370 auto glContext = GrGLContext::Make(std::move(interface), options);
371 if (!glContext) {
372 return nullptr;
bsalomon424cc262015-05-22 10:37:30 -0700373 }
Brian Salomon8ab1cc42017-12-07 12:40:00 -0500374 return sk_sp<GrGpu>(new GrGLGpu(std::move(glContext), context));
bsalomon424cc262015-05-22 10:37:30 -0700375}
376
Brian Salomon8ab1cc42017-12-07 12:40:00 -0500377GrGLGpu::GrGLGpu(std::unique_ptr<GrGLContext> ctx, GrContext* context)
378 : GrGpu(context)
379 , fGLContext(std::move(ctx))
380 , fProgramCache(new ProgramCache(this))
381 , fHWProgramID(0)
382 , fTempSrcFBOID(0)
383 , fTempDstFBOID(0)
Brian Osmana63593a2018-12-06 15:54:53 -0500384 , fStencilClearFBOID(0) {
Brian Salomon8ab1cc42017-12-07 12:40:00 -0500385 SkASSERT(fGLContext);
Brian Salomondc829942018-10-23 16:07:24 -0400386 GrGLClearErr(this->glInterface());
Brian Salomon8ab1cc42017-12-07 12:40:00 -0500387 fCaps = sk_ref_sp(fGLContext->caps());
bsalomon@google.combcce8922013-03-25 15:38:39 +0000388
Brian Salomond978b902019-02-07 15:09:18 -0500389 fHWTextureUnitBindings.reset(this->numTextureUnits());
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +0000390
Brian Salomonae64c192019-02-05 09:41:37 -0500391 this->hwBufferState(GrGpuBufferType::kVertex)->fGLTarget = GR_GL_ARRAY_BUFFER;
392 this->hwBufferState(GrGpuBufferType::kIndex)->fGLTarget = GR_GL_ELEMENT_ARRAY_BUFFER;
cdaltone2e71c22016-04-07 18:13:29 -0700393 if (GrGLCaps::kChromium_TransferBufferType == this->glCaps().transferBufferType()) {
Brian Salomonae64c192019-02-05 09:41:37 -0500394 this->hwBufferState(GrGpuBufferType::kXferCpuToGpu)->fGLTarget =
395 GR_GL_PIXEL_UNPACK_TRANSFER_BUFFER_CHROMIUM;
396 this->hwBufferState(GrGpuBufferType::kXferGpuToCpu)->fGLTarget =
397 GR_GL_PIXEL_PACK_TRANSFER_BUFFER_CHROMIUM;
cdaltone2e71c22016-04-07 18:13:29 -0700398 } else {
Brian Salomonae64c192019-02-05 09:41:37 -0500399 this->hwBufferState(GrGpuBufferType::kXferCpuToGpu)->fGLTarget = GR_GL_PIXEL_UNPACK_BUFFER;
400 this->hwBufferState(GrGpuBufferType::kXferGpuToCpu)->fGLTarget = GR_GL_PIXEL_PACK_BUFFER;
cdaltone2e71c22016-04-07 18:13:29 -0700401 }
Brian Salomonae64c192019-02-05 09:41:37 -0500402 for (int i = 0; i < kGrGpuBufferTypeCount; ++i) {
Rob Phillipsbc534f62017-09-05 19:56:19 -0400403 fHWBufferState[i].invalidate();
404 }
Brian Salomonae64c192019-02-05 09:41:37 -0500405 GR_STATIC_ASSERT(4 == SK_ARRAY_COUNT(fHWBufferState));
cdaltone2e71c22016-04-07 18:13:29 -0700406
407 if (this->glCaps().shaderCaps()->pathRenderingSupport()) {
408 fPathRendering.reset(new GrGLPathRendering(this));
409 }
410
Brian Salomondc829942018-10-23 16:07:24 -0400411 if (this->glCaps().samplerObjectSupport()) {
412 fSamplerObjectCache.reset(new SamplerObjectCache(this));
413 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000414}
415
bsalomon861e1032014-12-16 07:33:49 -0800416GrGLGpu::~GrGLGpu() {
cdaltone2e71c22016-04-07 18:13:29 -0700417 // Ensure any GrGpuResource objects get deleted first, since they may require a working GrGLGpu
418 // to release the resources held by the objects themselves.
kkinnunen702501d2016-01-13 23:36:45 -0800419 fPathRendering.reset();
cdaltone2e71c22016-04-07 18:13:29 -0700420 fCopyProgramArrayBuffer.reset();
brianosman33f6b3f2016-06-02 05:49:21 -0700421 fMipmapProgramArrayBuffer.reset();
kkinnunen702501d2016-01-13 23:36:45 -0800422
Brian Salomon802cb312018-06-08 18:05:20 -0400423 fHWProgram.reset();
Brian Salomon43f8bf02017-10-18 08:33:29 -0400424 if (fHWProgramID) {
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000425 // detach the current program so there is no confusion on OpenGL's part
426 // that we want it to be deleted
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000427 GL_CALL(UseProgram(0));
428 }
429
Brian Salomon43f8bf02017-10-18 08:33:29 -0400430 if (fTempSrcFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700431 this->deleteFramebuffer(fTempSrcFBOID);
egdaniel0f5f9672015-02-03 11:10:51 -0800432 }
Brian Salomon43f8bf02017-10-18 08:33:29 -0400433 if (fTempDstFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700434 this->deleteFramebuffer(fTempDstFBOID);
egdaniel0f5f9672015-02-03 11:10:51 -0800435 }
Brian Salomon43f8bf02017-10-18 08:33:29 -0400436 if (fStencilClearFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700437 this->deleteFramebuffer(fStencilClearFBOID);
bsalomondd3143b2015-02-23 09:27:45 -0800438 }
egdaniel0f5f9672015-02-03 11:10:51 -0800439
bsalomon7ea33f52015-11-22 14:51:00 -0800440 for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) {
441 if (0 != fCopyPrograms[i].fProgram) {
442 GL_CALL(DeleteProgram(fCopyPrograms[i].fProgram));
443 }
bsalomon6df86402015-06-01 10:41:49 -0700444 }
bsalomon6dea83f2015-12-03 12:58:06 -0800445
brianosman33f6b3f2016-06-02 05:49:21 -0700446 for (size_t i = 0; i < SK_ARRAY_COUNT(fMipmapPrograms); ++i) {
447 if (0 != fMipmapPrograms[i].fProgram) {
448 GL_CALL(DeleteProgram(fMipmapPrograms[i].fProgram));
449 }
450 }
451
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000452 delete fProgramCache;
Brian Salomondc829942018-10-23 16:07:24 -0400453 fSamplerObjectCache.reset();
bsalomonc8dc1f72014-08-21 13:02:13 -0700454}
455
bsalomon6e2aad42016-04-01 11:54:31 -0700456void GrGLGpu::disconnect(DisconnectType type) {
457 INHERITED::disconnect(type);
458 if (DisconnectType::kCleanup == type) {
459 if (fHWProgramID) {
460 GL_CALL(UseProgram(0));
461 }
462 if (fTempSrcFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700463 this->deleteFramebuffer(fTempSrcFBOID);
bsalomon6e2aad42016-04-01 11:54:31 -0700464 }
465 if (fTempDstFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700466 this->deleteFramebuffer(fTempDstFBOID);
bsalomon6e2aad42016-04-01 11:54:31 -0700467 }
468 if (fStencilClearFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700469 this->deleteFramebuffer(fStencilClearFBOID);
bsalomon6e2aad42016-04-01 11:54:31 -0700470 }
bsalomon6e2aad42016-04-01 11:54:31 -0700471 for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) {
472 if (fCopyPrograms[i].fProgram) {
473 GL_CALL(DeleteProgram(fCopyPrograms[i].fProgram));
474 }
475 }
brianosman33f6b3f2016-06-02 05:49:21 -0700476 for (size_t i = 0; i < SK_ARRAY_COUNT(fMipmapPrograms); ++i) {
477 if (fMipmapPrograms[i].fProgram) {
478 GL_CALL(DeleteProgram(fMipmapPrograms[i].fProgram));
479 }
480 }
Brian Salomon43f8bf02017-10-18 08:33:29 -0400481
Brian Salomondc829942018-10-23 16:07:24 -0400482 if (fSamplerObjectCache) {
483 fSamplerObjectCache->release();
484 }
bsalomon6e2aad42016-04-01 11:54:31 -0700485 } else {
486 if (fProgramCache) {
487 fProgramCache->abandon();
488 }
Brian Salomondc829942018-10-23 16:07:24 -0400489 if (fSamplerObjectCache) {
490 fSamplerObjectCache->abandon();
491 }
bsalomon6e2aad42016-04-01 11:54:31 -0700492 }
493
Brian Salomon802cb312018-06-08 18:05:20 -0400494 fHWProgram.reset();
bsalomon6e2aad42016-04-01 11:54:31 -0700495 delete fProgramCache;
496 fProgramCache = nullptr;
497
bsalomonc8dc1f72014-08-21 13:02:13 -0700498 fHWProgramID = 0;
egdanield803f272015-03-18 13:01:52 -0700499 fTempSrcFBOID = 0;
500 fTempDstFBOID = 0;
501 fStencilClearFBOID = 0;
cdaltone2e71c22016-04-07 18:13:29 -0700502 fCopyProgramArrayBuffer.reset();
bsalomon7ea33f52015-11-22 14:51:00 -0800503 for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) {
504 fCopyPrograms[i].fProgram = 0;
505 }
brianosman33f6b3f2016-06-02 05:49:21 -0700506 fMipmapProgramArrayBuffer.reset();
507 for (size_t i = 0; i < SK_ARRAY_COUNT(fMipmapPrograms); ++i) {
508 fMipmapPrograms[i].fProgram = 0;
509 }
Brian Salomon43f8bf02017-10-18 08:33:29 -0400510
jvanverthe9c0fc62015-04-29 11:18:05 -0700511 if (this->glCaps().shaderCaps()->pathRenderingSupport()) {
bsalomon6e2aad42016-04-01 11:54:31 -0700512 this->glPathRendering()->disconnect(type);
bsalomonc8dc1f72014-08-21 13:02:13 -0700513 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000514}
515
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000516///////////////////////////////////////////////////////////////////////////////
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000517
bsalomon861e1032014-12-16 07:33:49 -0800518void GrGLGpu::onResetContext(uint32_t resetBits) {
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000519 if (resetBits & kMisc_GrGLBackendState) {
Brian Salomonf0861672017-05-08 11:10:10 -0400520 // we don't use the zb at all
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000521 GL_CALL(Disable(GR_GL_DEPTH_TEST));
522 GL_CALL(DepthMask(GR_GL_FALSE));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000523
Brian Salomonf0861672017-05-08 11:10:10 -0400524 // We don't use face culling.
525 GL_CALL(Disable(GR_GL_CULL_FACE));
Chris Daltonc8ece3d2018-07-30 15:03:45 -0600526 // We do use separate stencil. Our algorithms don't care which face is front vs. back so
527 // just set this to the default for self-consistency.
528 GL_CALL(FrontFace(GR_GL_CCW));
Brian Salomonf0861672017-05-08 11:10:10 -0400529
Brian Salomonae64c192019-02-05 09:41:37 -0500530 this->hwBufferState(GrGpuBufferType::kXferCpuToGpu)->invalidate();
531 this->hwBufferState(GrGpuBufferType::kXferGpuToCpu)->invalidate();
cdaltonc1613102016-03-16 07:48:20 -0700532
Kevin Lubick8aa203c2019-03-19 13:23:10 -0400533 if (GR_IS_GR_GL(this->glStandard())) {
Jim Van Verth32ac83e2016-11-28 15:23:57 -0500534#ifndef USE_NSIGHT
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000535 // Desktop-only state that we never change
536 if (!this->glCaps().isCoreProfile()) {
537 GL_CALL(Disable(GR_GL_POINT_SMOOTH));
538 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
539 GL_CALL(Disable(GR_GL_POLYGON_SMOOTH));
540 GL_CALL(Disable(GR_GL_POLYGON_STIPPLE));
541 GL_CALL(Disable(GR_GL_COLOR_LOGIC_OP));
542 GL_CALL(Disable(GR_GL_INDEX_LOGIC_OP));
543 }
544 // The windows NVIDIA driver has GL_ARB_imaging in the extension string when using a
545 // core profile. This seems like a bug since the core spec removes any mention of
546 // GL_ARB_imaging.
547 if (this->glCaps().imagingSupport() && !this->glCaps().isCoreProfile()) {
548 GL_CALL(Disable(GR_GL_COLOR_TABLE));
549 }
550 GL_CALL(Disable(GR_GL_POLYGON_OFFSET_FILL));
Jim Van Verth609e7cc2017-03-30 14:28:08 -0400551
Jim Van Verthfbdc0802017-05-02 16:15:53 -0400552 if (this->caps()->wireframeMode()) {
553 GL_CALL(PolygonMode(GR_GL_FRONT_AND_BACK, GR_GL_LINE));
554 } else {
555 GL_CALL(PolygonMode(GR_GL_FRONT_AND_BACK, GR_GL_FILL));
556 }
Jim Van Verth32ac83e2016-11-28 15:23:57 -0500557#endif
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000558 // Since ES doesn't support glPointSize at all we always use the VS to
559 // set the point size
560 GL_CALL(Enable(GR_GL_VERTEX_PROGRAM_POINT_SIZE));
561
bsalomon@google.com2b1b8c02013-02-28 22:06:02 +0000562 }
joshualitt58162332014-08-01 06:44:53 -0700563
Kevin Lubick8aa203c2019-03-19 13:23:10 -0400564 if (GR_IS_GR_GL_ES(this->glStandard()) &&
Brian Salomon8bc352c2019-01-28 09:05:22 -0500565 this->glCaps().fbFetchRequiresEnablePerSample()) {
joshualitt58162332014-08-01 06:44:53 -0700566 // The arm extension requires specifically enabling MSAA fetching per sample.
567 // On some devices this may have a perf hit. Also multiple render targets are disabled
Brian Salomon8bc352c2019-01-28 09:05:22 -0500568 GL_CALL(Enable(GR_GL_FETCH_PER_SAMPLE));
joshualitt58162332014-08-01 06:44:53 -0700569 }
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000570 fHWWriteToColor = kUnknown_TriState;
571 // we only ever use lines in hairline mode
572 GL_CALL(LineWidth(1));
bsalomonaca31fe2015-09-22 11:38:46 -0700573 GL_CALL(Disable(GR_GL_DITHER));
Brian Salomon805cc7a2019-01-28 09:52:34 -0500574
575 fHWClearColor[0] = fHWClearColor[1] = fHWClearColor[2] = fHWClearColor[3] = SK_FloatNaN;
bsalomon@google.comcad107b2013-06-28 14:32:08 +0000576 }
edisonn@google.comba669992013-06-28 16:03:21 +0000577
egdanielb414f252014-07-29 13:15:47 -0700578 if (resetBits & kMSAAEnable_GrGLBackendState) {
579 fMSAAEnabled = kUnknown_TriState;
vbuzinovdded6962015-06-12 08:59:45 -0700580
egdanieleed519e2016-01-15 11:36:18 -0800581 if (this->caps()->usesMixedSamples()) {
cdaltonaf8bc7d2016-02-05 09:35:20 -0800582 // The skia blend modes all use premultiplied alpha and therefore expect RGBA coverage
583 // modulation. This state has no effect when not rendering to a mixed sampled target.
vbuzinovdded6962015-06-12 08:59:45 -0700584 GL_CALL(CoverageModulation(GR_GL_RGBA));
585 }
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000586 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000587
commit-bot@chromium.org46fbfe02013-08-30 15:52:12 +0000588 fHWActiveTextureUnitIdx = -1; // invalid
Brian Salomonaf971de2017-06-08 16:11:33 -0400589 fLastPrimitiveType = static_cast<GrPrimitiveType>(-1);
commit-bot@chromium.org46fbfe02013-08-30 15:52:12 +0000590
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000591 if (resetBits & kTextureBinding_GrGLBackendState) {
Brian Salomond978b902019-02-07 15:09:18 -0500592 for (int s = 0; s < this->numTextureUnits(); ++s) {
Brian Salomon1f05d452019-02-08 12:33:08 -0500593 fHWTextureUnitBindings[s].invalidateAllTargets(false);
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000594 }
Brian Salomondc829942018-10-23 16:07:24 -0400595 if (fSamplerObjectCache) {
596 fSamplerObjectCache->invalidateBindings();
597 }
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000598 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000599
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000600 if (resetBits & kBlend_GrGLBackendState) {
601 fHWBlendState.invalidate();
602 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000603
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000604 if (resetBits & kView_GrGLBackendState) {
605 fHWScissorSettings.invalidate();
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700606 fHWWindowRectsState.invalidate();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000607 fHWViewport.invalidate();
608 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000609
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000610 if (resetBits & kStencil_GrGLBackendState) {
611 fHWStencilSettings.invalidate();
612 fHWStencilTestEnabled = kUnknown_TriState;
613 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000614
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000615 // Vertex
616 if (resetBits & kVertex_GrGLBackendState) {
cdaltone2e71c22016-04-07 18:13:29 -0700617 fHWVertexArrayState.invalidate();
Brian Salomonae64c192019-02-05 09:41:37 -0500618 this->hwBufferState(GrGpuBufferType::kVertex)->invalidate();
619 this->hwBufferState(GrGpuBufferType::kIndex)->invalidate();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000620 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000621
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000622 if (resetBits & kRenderTarget_GrGLBackendState) {
Robert Phillips294870f2016-11-11 12:38:40 -0500623 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon16921ec2015-07-30 15:34:56 -0700624 fHWSRGBFramebuffer = kUnknown_TriState;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000625 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000626
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000627 if (resetBits & kPathRendering_GrGLBackendState) {
jvanverthe9c0fc62015-04-29 11:18:05 -0700628 if (this->caps()->shaderCaps()->pathRenderingSupport()) {
kkinnunenccdaa042014-08-20 01:36:23 -0700629 this->glPathRendering()->resetContext();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000630 }
bsalomon@google.com05a718c2012-06-29 14:01:53 +0000631 }
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000632
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000633 // we assume these values
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000634 if (resetBits & kPixelStore_GrGLBackendState) {
635 if (this->glCaps().unpackRowLengthSupport()) {
636 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
637 }
638 if (this->glCaps().packRowLengthSupport()) {
639 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
640 }
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000641 if (this->glCaps().packFlipYSupport()) {
642 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, GR_GL_FALSE));
643 }
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000644 }
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000645
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000646 if (resetBits & kProgram_GrGLBackendState) {
647 fHWProgramID = 0;
Brian Salomon802cb312018-06-08 18:05:20 -0400648 fHWProgram.reset();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000649 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000650}
651
Brian Salomond17f6582017-07-19 18:28:58 -0400652static bool check_backend_texture(const GrBackendTexture& backendTex, const GrGLCaps& caps,
653 GrGLTexture::IDDesc* idDesc) {
Greg Daniel52e16d92018-04-10 09:34:07 -0400654 GrGLTextureInfo info;
655 if (!backendTex.getGLTextureInfo(&info) || !info.fID) {
Brian Salomond17f6582017-07-19 18:28:58 -0400656 return false;
bsalomon091f60c2015-11-10 11:54:56 -0800657 }
robertphillips@google.comb72e5d32012-10-30 15:18:10 +0000658
Greg Daniel52e16d92018-04-10 09:34:07 -0400659 idDesc->fInfo = info;
bsalomon7ea33f52015-11-22 14:51:00 -0800660
Brian Salomond17f6582017-07-19 18:28:58 -0400661 if (GR_GL_TEXTURE_EXTERNAL == idDesc->fInfo.fTarget) {
662 if (!caps.shaderCaps()->externalTextureSupport()) {
663 return false;
664 }
665 } else if (GR_GL_TEXTURE_RECTANGLE == idDesc->fInfo.fTarget) {
666 if (!caps.rectangleTextureSupport()) {
667 return false;
668 }
669 } else if (GR_GL_TEXTURE_2D != idDesc->fInfo.fTarget) {
670 return false;
671 }
672 return true;
673}
674
675sk_sp<GrTexture> GrGLGpu::onWrapBackendTexture(const GrBackendTexture& backendTex,
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500676 GrWrapOwnership ownership, GrWrapCacheable cacheable,
677 GrIOType ioType) {
bsalomonb15b4c12014-10-29 12:41:57 -0700678 GrGLTexture::IDDesc idDesc;
Brian Salomond17f6582017-07-19 18:28:58 -0400679 if (!check_backend_texture(backendTex, this->glCaps(), &idDesc)) {
680 return nullptr;
681 }
Greg Daniele7d8da42017-12-04 11:23:19 -0500682 if (!idDesc.fInfo.fFormat) {
683 idDesc.fInfo.fFormat = this->glCaps().configSizedInternalFormat(backendTex.config());
684 }
Brian Salomond17f6582017-07-19 18:28:58 -0400685 if (kBorrow_GrWrapOwnership == ownership) {
686 idDesc.fOwnership = GrBackendObjectOwnership::kBorrowed;
687 } else {
688 idDesc.fOwnership = GrBackendObjectOwnership::kOwned;
689 }
bsalomone5286e02016-01-14 09:24:09 -0800690
Brian Salomond17f6582017-07-19 18:28:58 -0400691 GrSurfaceDesc surfDesc;
692 surfDesc.fFlags = kNone_GrSurfaceFlags;
693 surfDesc.fWidth = backendTex.width();
694 surfDesc.fHeight = backendTex.height();
695 surfDesc.fConfig = backendTex.config();
Brian Salomonbdecacf2018-02-02 20:32:49 -0500696 surfDesc.fSampleCnt = 1;
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400697
Greg Daniel177e6952017-10-12 12:27:11 -0400698 GrMipMapsStatus mipMapsStatus = backendTex.hasMipMaps() ? GrMipMapsStatus::kValid
699 : GrMipMapsStatus::kNotAllocated;
700
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500701 auto texture =
702 GrGLTexture::MakeWrapped(this, surfDesc, mipMapsStatus, idDesc, cacheable, ioType);
Brian Salomondc829942018-10-23 16:07:24 -0400703 // We don't know what parameters are already set on wrapped textures.
704 texture->textureParamsModified();
705 return std::move(texture);
Brian Salomond17f6582017-07-19 18:28:58 -0400706}
707
708sk_sp<GrTexture> GrGLGpu::onWrapRenderableBackendTexture(const GrBackendTexture& backendTex,
Brian Salomond17f6582017-07-19 18:28:58 -0400709 int sampleCnt,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500710 GrWrapOwnership ownership,
711 GrWrapCacheable cacheable) {
Brian Salomond17f6582017-07-19 18:28:58 -0400712 GrGLTexture::IDDesc idDesc;
713 if (!check_backend_texture(backendTex, this->glCaps(), &idDesc)) {
bsalomone5286e02016-01-14 09:24:09 -0800714 return nullptr;
bsalomon7ea33f52015-11-22 14:51:00 -0800715 }
Greg Daniele7d8da42017-12-04 11:23:19 -0500716 if (!idDesc.fInfo.fFormat) {
717 idDesc.fInfo.fFormat = this->glCaps().configSizedInternalFormat(backendTex.config());
718 }
bsalomone5286e02016-01-14 09:24:09 -0800719
Brian Salomond17f6582017-07-19 18:28:58 -0400720 // We don't support rendering to a EXTERNAL texture.
721 if (GR_GL_TEXTURE_EXTERNAL == idDesc.fInfo.fTarget) {
bsalomona98419b2015-11-23 07:09:50 -0800722 return nullptr;
723 }
bsalomon10528f12015-10-14 12:54:52 -0700724
Brian Osman766fcbb2017-03-13 09:33:09 -0400725 if (kBorrow_GrWrapOwnership == ownership) {
Brian Osmana6953f22017-03-10 20:14:05 +0000726 idDesc.fOwnership = GrBackendObjectOwnership::kBorrowed;
Brian Osman766fcbb2017-03-13 09:33:09 -0400727 } else {
728 idDesc.fOwnership = GrBackendObjectOwnership::kOwned;
bsalomone5286e02016-01-14 09:24:09 -0800729 }
bsalomonb15b4c12014-10-29 12:41:57 -0700730
Ben Wagner18b61f92016-10-25 10:44:02 -0400731 GrSurfaceDesc surfDesc;
Brian Salomond17f6582017-07-19 18:28:58 -0400732 surfDesc.fFlags = kRenderTarget_GrSurfaceFlag;
Greg Daniel7ef28f32017-04-20 16:41:55 +0000733 surfDesc.fWidth = backendTex.width();
734 surfDesc.fHeight = backendTex.height();
735 surfDesc.fConfig = backendTex.config();
Brian Salomonbdecacf2018-02-02 20:32:49 -0500736 surfDesc.fSampleCnt = this->caps()->getRenderTargetSampleCount(sampleCnt, backendTex.config());
737 if (surfDesc.fSampleCnt < 1) {
738 return nullptr;
739 }
bsalomon@google.come269f212011-11-07 13:29:52 +0000740
Brian Salomond17f6582017-07-19 18:28:58 -0400741 GrGLRenderTarget::IDDesc rtIDDesc;
742 if (!this->createRenderTargetObjects(surfDesc, idDesc.fInfo, &rtIDDesc)) {
743 return nullptr;
bsalomon@google.come269f212011-11-07 13:29:52 +0000744 }
Greg Daniel177e6952017-10-12 12:27:11 -0400745
746 GrMipMapsStatus mipMapsStatus = backendTex.hasMipMaps() ? GrMipMapsStatus::kDirty
747 : GrMipMapsStatus::kNotAllocated;
748
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500749 sk_sp<GrGLTextureRenderTarget> texRT(GrGLTextureRenderTarget::MakeWrapped(
750 this, surfDesc, idDesc, rtIDDesc, cacheable, mipMapsStatus));
Brian Salomond17f6582017-07-19 18:28:58 -0400751 texRT->baseLevelWasBoundToFBO();
Brian Salomondc829942018-10-23 16:07:24 -0400752 // We don't know what parameters are already set on wrapped textures.
753 texRT->textureParamsModified();
Brian Salomond17f6582017-07-19 18:28:58 -0400754 return std::move(texRT);
bsalomon@google.come269f212011-11-07 13:29:52 +0000755}
756
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400757sk_sp<GrRenderTarget> GrGLGpu::onWrapBackendRenderTarget(const GrBackendRenderTarget& backendRT) {
Greg Daniel323fbcf2018-04-10 13:46:30 -0400758 GrGLFramebufferInfo info;
759 if (!backendRT.getGLFramebufferInfo(&info)) {
Greg Danielbcf612b2017-05-01 13:50:58 +0000760 return nullptr;
761 }
762
bsalomonb15b4c12014-10-29 12:41:57 -0700763 GrGLRenderTarget::IDDesc idDesc;
Greg Daniel323fbcf2018-04-10 13:46:30 -0400764 idDesc.fRTFBOID = info.fFBOID;
bsalomonb15b4c12014-10-29 12:41:57 -0700765 idDesc.fMSColorRenderbufferID = 0;
egdanield803f272015-03-18 13:01:52 -0700766 idDesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
Brian Osman0b791f52017-03-10 08:30:22 -0500767 idDesc.fRTFBOOwnership = GrBackendObjectOwnership::kBorrowed;
csmartdaltonf9635992016-08-10 11:09:07 -0700768 idDesc.fIsMixedSampled = false;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000769
bsalomonb15b4c12014-10-29 12:41:57 -0700770 GrSurfaceDesc desc;
Brian Salomon0ec981b2017-05-15 13:48:50 -0400771 desc.fFlags = kRenderTarget_GrSurfaceFlag;
Greg Danielbcf612b2017-05-01 13:50:58 +0000772 desc.fWidth = backendRT.width();
773 desc.fHeight = backendRT.height();
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400774 desc.fConfig = backendRT.config();
Brian Salomonbdecacf2018-02-02 20:32:49 -0500775 desc.fSampleCnt =
776 this->caps()->getRenderTargetSampleCount(backendRT.sampleCnt(), backendRT.config());
bsalomonb15b4c12014-10-29 12:41:57 -0700777
Greg Daniel4065d452018-11-16 15:43:41 -0500778 return GrGLRenderTarget::MakeWrapped(this, desc, info.fFormat, idDesc, backendRT.stencilBits());
bsalomon@google.come269f212011-11-07 13:29:52 +0000779}
780
Greg Daniel7ef28f32017-04-20 16:41:55 +0000781sk_sp<GrRenderTarget> GrGLGpu::onWrapBackendTextureAsRenderTarget(const GrBackendTexture& tex,
Greg Daniel7ef28f32017-04-20 16:41:55 +0000782 int sampleCnt) {
Greg Daniel52e16d92018-04-10 09:34:07 -0400783 GrGLTextureInfo info;
784 if (!tex.getGLTextureInfo(&info) || !info.fID) {
ericrkf7b8b8a2016-02-24 14:49:51 -0800785 return nullptr;
786 }
ericrkf7b8b8a2016-02-24 14:49:51 -0800787
Greg Daniel52e16d92018-04-10 09:34:07 -0400788 if (GR_GL_TEXTURE_RECTANGLE != info.fTarget &&
789 GR_GL_TEXTURE_2D != info.fTarget) {
ericrkf7b8b8a2016-02-24 14:49:51 -0800790 // Only texture rectangle and texture 2d are supported. We do not check whether texture
791 // rectangle is supported by Skia - if the caller provided us with a texture rectangle,
792 // we assume the necessary support exists.
793 return nullptr;
794 }
795
Ben Wagner18b61f92016-10-25 10:44:02 -0400796 GrSurfaceDesc surfDesc;
Greg Daniel7ef28f32017-04-20 16:41:55 +0000797 surfDesc.fFlags = kRenderTarget_GrSurfaceFlag;
798 surfDesc.fWidth = tex.width();
799 surfDesc.fHeight = tex.height();
800 surfDesc.fConfig = tex.config();
Brian Salomonbdecacf2018-02-02 20:32:49 -0500801 surfDesc.fSampleCnt = this->caps()->getRenderTargetSampleCount(sampleCnt, tex.config());
ericrkf7b8b8a2016-02-24 14:49:51 -0800802
803 GrGLRenderTarget::IDDesc rtIDDesc;
Greg Daniel52e16d92018-04-10 09:34:07 -0400804 if (!this->createRenderTargetObjects(surfDesc, info, &rtIDDesc)) {
ericrkf7b8b8a2016-02-24 14:49:51 -0800805 return nullptr;
806 }
Greg Daniel4065d452018-11-16 15:43:41 -0500807 return GrGLRenderTarget::MakeWrapped(this, surfDesc, info.fFormat, rtIDDesc, 0);
ericrkf7b8b8a2016-02-24 14:49:51 -0800808}
809
Brian Salomonc320b152018-02-20 14:05:36 -0500810static bool check_write_and_transfer_input(GrGLTexture* glTex) {
bsalomon6cb3cbe2015-07-30 07:34:27 -0700811 if (!glTex) {
812 return false;
813 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000814
bsalomone5286e02016-01-14 09:24:09 -0800815 // Write or transfer of pixels is not implemented for TEXTURE_EXTERNAL textures
816 if (GR_GL_TEXTURE_EXTERNAL == glTex->target()) {
bsalomon7ea33f52015-11-22 14:51:00 -0800817 return false;
818 }
819
jvanverth17aa0472016-01-05 10:41:27 -0800820 return true;
821}
822
Brian Salomona9b04b92018-06-01 15:04:28 -0400823bool GrGLGpu::onWritePixels(GrSurface* surface, int left, int top, int width, int height,
824 GrColorType srcColorType, const GrMipLevel texels[],
825 int mipLevelCount) {
Brian Salomonc320b152018-02-20 14:05:36 -0500826 auto glTex = static_cast<GrGLTexture*>(surface->asTexture());
jvanverth17aa0472016-01-05 10:41:27 -0800827
Brian Salomonc320b152018-02-20 14:05:36 -0500828 if (!check_write_and_transfer_input(glTex)) {
jvanverth17aa0472016-01-05 10:41:27 -0800829 return false;
830 }
831
Brian Salomond978b902019-02-07 15:09:18 -0500832 this->bindTextureToScratchUnit(glTex->target(), glTex->textureID());
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000833
Brian Salomonc320b152018-02-20 14:05:36 -0500834 // No sRGB transformation occurs in uploadTexData. We choose to make the src config match the
835 // srgb-ness of the surface to avoid issues in ES2 where internal/external formats must match.
836 // When we're on ES2 and the dst is GL_SRGB_ALPHA by making the config be kSRGB_8888 we know
837 // that our caps will choose GL_SRGB_ALPHA as the external format, too. On ES3 or regular GL our
838 // caps knows to make the external format be GL_RGBA.
839 auto srgbEncoded = GrPixelConfigIsSRGBEncoded(surface->config());
840 auto srcAsConfig = GrColorTypeToPixelConfig(srcColorType, srgbEncoded);
Jim Van Verth1676cb92019-01-15 13:24:45 -0500841
842 SkASSERT(!GrPixelConfigIsCompressed(glTex->config()));
Brian Salomona9b04b92018-06-01 15:04:28 -0400843 return this->uploadTexData(glTex->config(), glTex->width(), glTex->height(), glTex->target(),
844 kWrite_UploadType, left, top, width, height, srcAsConfig, texels,
845 mipLevelCount);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000846}
847
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400848// For GL_[UN]PACK_ALIGNMENT. TODO: This really wants to be GrColorType.
bsalomonf46a1242015-12-15 12:37:38 -0800849static inline GrGLint config_alignment(GrPixelConfig config) {
Jim Van Verth1676cb92019-01-15 13:24:45 -0500850 SkASSERT(!GrPixelConfigIsCompressed(config));
bsalomonf46a1242015-12-15 12:37:38 -0800851 switch (config) {
852 case kAlpha_8_GrPixelConfig:
Greg Danielef59d872017-11-17 16:47:21 -0500853 case kAlpha_8_as_Alpha_GrPixelConfig:
854 case kAlpha_8_as_Red_GrPixelConfig:
Brian Osman986563b2017-01-10 14:20:02 -0500855 case kGray_8_GrPixelConfig:
Greg Daniel7af060a2017-12-05 16:27:11 -0500856 case kGray_8_as_Lum_GrPixelConfig:
857 case kGray_8_as_Red_GrPixelConfig:
bsalomonf46a1242015-12-15 12:37:38 -0800858 return 1;
859 case kRGB_565_GrPixelConfig:
860 case kRGBA_4444_GrPixelConfig:
Jim Van Verth69e57852018-12-05 13:38:59 -0500861 case kRG_88_GrPixelConfig:
bsalomonf46a1242015-12-15 12:37:38 -0800862 case kAlpha_half_GrPixelConfig:
Greg Danielef59d872017-11-17 16:47:21 -0500863 case kAlpha_half_as_Red_GrPixelConfig:
bsalomonf46a1242015-12-15 12:37:38 -0800864 case kRGBA_half_GrPixelConfig:
Brian Osmand0626aa2019-03-11 15:28:06 -0400865 case kRGBA_half_Clamped_GrPixelConfig:
bsalomonf46a1242015-12-15 12:37:38 -0800866 return 2;
867 case kRGBA_8888_GrPixelConfig:
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400868 case kRGB_888_GrPixelConfig: // We're really talking about GrColorType::kRGB_888x here.
Greg Danielf259b8b2019-02-14 09:03:43 -0500869 case kRGB_888X_GrPixelConfig:
bsalomonf46a1242015-12-15 12:37:38 -0800870 case kBGRA_8888_GrPixelConfig:
871 case kSRGBA_8888_GrPixelConfig:
brianosmana6359362016-03-21 06:55:37 -0700872 case kSBGRA_8888_GrPixelConfig:
Brian Osman10fc6fd2018-03-02 11:01:10 -0500873 case kRGBA_1010102_GrPixelConfig:
bsalomonf46a1242015-12-15 12:37:38 -0800874 case kRGBA_float_GrPixelConfig:
csmartdalton6aa0e112017-02-08 16:14:11 -0500875 case kRG_float_GrPixelConfig:
bsalomonf46a1242015-12-15 12:37:38 -0800876 return 4;
Jim Van Verth1676cb92019-01-15 13:24:45 -0500877 case kRGB_ETC1_GrPixelConfig:
csmartdalton6aa0e112017-02-08 16:14:11 -0500878 case kUnknown_GrPixelConfig:
bsalomonf46a1242015-12-15 12:37:38 -0800879 return 0;
880 }
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400881 SK_ABORT("Invalid pixel config");
csmartdalton6aa0e112017-02-08 16:14:11 -0500882 return 0;
bsalomonf46a1242015-12-15 12:37:38 -0800883}
884
Brian Salomone05ba5a2019-04-08 11:59:07 -0400885bool GrGLGpu::onTransferPixelsTo(GrTexture* texture, int left, int top, int width, int height,
886 GrColorType bufferColorType, GrGpuBuffer* transferBuffer,
887 size_t offset, size_t rowBytes) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400888 GrGLTexture* glTex = static_cast<GrGLTexture*>(texture);
889 GrPixelConfig texConfig = glTex->config();
890 SkASSERT(this->caps()->isConfigTexturable(texConfig));
891
Jim Van Verth1676cb92019-01-15 13:24:45 -0500892 // Can't transfer compressed data
893 SkASSERT(!GrPixelConfigIsCompressed(glTex->config()));
894
Brian Salomonc320b152018-02-20 14:05:36 -0500895 if (!check_write_and_transfer_input(glTex)) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400896 return false;
897 }
898
Hal Canaryc36f7e72018-06-18 15:50:09 -0400899 static_assert(sizeof(int) == sizeof(int32_t), "");
900 if (width <= 0 || height <= 0) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400901 return false;
902 }
903
Brian Salomond978b902019-02-07 15:09:18 -0500904 this->bindTextureToScratchUnit(glTex->target(), glTex->textureID());
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400905
906 SkASSERT(!transferBuffer->isMapped());
Brian Salomondbf70722019-02-07 11:31:24 -0500907 SkASSERT(!transferBuffer->isCpuBuffer());
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400908 const GrGLBuffer* glBuffer = static_cast<const GrGLBuffer*>(transferBuffer);
Brian Salomonae64c192019-02-05 09:41:37 -0500909 this->bindBuffer(GrGpuBufferType::kXferCpuToGpu, glBuffer);
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400910
Greg Daniel660cc992017-06-26 14:55:05 -0400911 SkDEBUGCODE(
912 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
913 SkIRect bounds = SkIRect::MakeWH(texture->width(), texture->height());
914 SkASSERT(bounds.contains(subRect));
915 )
916
Brian Salomonc320b152018-02-20 14:05:36 -0500917 int bpp = GrColorTypeBytesPerPixel(bufferColorType);
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400918 const size_t trimRowBytes = width * bpp;
Greg Daniel660cc992017-06-26 14:55:05 -0400919 if (!rowBytes) {
920 rowBytes = trimRowBytes;
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400921 }
Greg Daniel660cc992017-06-26 14:55:05 -0400922 const void* pixels = (void*)offset;
Bruce Dawson71479b72017-07-05 14:30:20 -0700923 if (width < 0 || height < 0) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400924 return false;
925 }
926
927 bool restoreGLRowLength = false;
928 if (trimRowBytes != rowBytes) {
929 // we should have checked for this support already
930 SkASSERT(this->glCaps().unpackRowLengthSupport());
931 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowBytes / bpp));
932 restoreGLRowLength = true;
933 }
934
935 // Internal format comes from the texture desc.
936 GrGLenum internalFormat;
937 // External format and type come from the upload data.
938 GrGLenum externalFormat;
939 GrGLenum externalType;
Brian Salomonc320b152018-02-20 14:05:36 -0500940 auto bufferAsConfig = GrColorTypeToPixelConfig(bufferColorType, GrSRGBEncoded::kNo);
941 if (!this->glCaps().getTexImageFormats(texConfig, bufferAsConfig, &internalFormat,
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400942 &externalFormat, &externalType)) {
943 return false;
944 }
945
946 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, config_alignment(texConfig)));
947 GL_CALL(TexSubImage2D(glTex->target(),
948 0,
949 left, top,
950 width,
951 height,
952 externalFormat, externalType,
953 pixels));
954
955 if (restoreGLRowLength) {
956 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
957 }
958
959 return true;
960}
961
Brian Salomon26de56e2019-04-10 12:14:26 -0400962bool GrGLGpu::onTransferPixelsFrom(GrSurface* surface, int left, int top, int width, int height,
963 GrColorType dstColorType, GrGpuBuffer* transferBuffer,
964 size_t offset) {
Brian Salomone05ba5a2019-04-08 11:59:07 -0400965 auto* glBuffer = static_cast<GrGLBuffer*>(transferBuffer);
966 this->bindBuffer(GrGpuBufferType::kXferGpuToCpu, glBuffer);
Brian Salomone05ba5a2019-04-08 11:59:07 -0400967 auto offsetAsPtr = reinterpret_cast<void*>(offset);
Brian Salomon26de56e2019-04-10 12:14:26 -0400968 return this->readOrTransferPixelsFrom(surface, left, top, width, height, dstColorType,
969 offsetAsPtr, width);
Brian Salomone05ba5a2019-04-08 11:59:07 -0400970}
971
cblume55f2d2d2016-02-26 13:20:48 -0800972/**
973 * Creates storage space for the texture and fills it with texels.
974 *
Brian Salomond1eaf492017-05-18 10:02:08 -0400975 * @param config Pixel config of the texture.
cblume55f2d2d2016-02-26 13:20:48 -0800976 * @param interface The GL interface in use.
cblume790d5132016-02-29 11:13:29 -0800977 * @param caps The capabilities of the GL device.
Jim Van Verth1676cb92019-01-15 13:24:45 -0500978 * @param target Which bound texture to target (GR_GL_TEXTURE_2D, e.g.)
brianosman81a84852016-09-12 09:05:14 -0700979 * @param internalFormat The data format used for the internal storage of the texture. May be sized.
980 * @param internalFormatForTexStorage The data format used for the TexStorage API. Must be sized.
cblume55f2d2d2016-02-26 13:20:48 -0800981 * @param externalFormat The data format used for the external storage of the texture.
982 * @param externalType The type of the data used for the external storage of the texture.
983 * @param texels The texel data of the texture being created.
Jim Van Verth1676cb92019-01-15 13:24:45 -0500984 * @param mipLevelCount Number of mipmap levels
cblume55f2d2d2016-02-26 13:20:48 -0800985 * @param baseWidth The width of the texture's base mipmap level
986 * @param baseHeight The height of the texture's base mipmap level
cblume55f2d2d2016-02-26 13:20:48 -0800987 */
Robert Phillips92de6312017-05-23 07:43:48 -0400988static bool allocate_and_populate_texture(GrPixelConfig config,
989 const GrGLInterface& interface,
990 const GrGLCaps& caps,
991 GrGLenum target,
992 GrGLenum internalFormat,
993 GrGLenum internalFormatForTexStorage,
994 GrGLenum externalFormat,
995 GrGLenum externalType,
Robert Phillips590533f2017-07-11 14:22:35 -0400996 const GrMipLevel texels[], int mipLevelCount,
Robert Phillips92de6312017-05-23 07:43:48 -0400997 int baseWidth, int baseHeight) {
cblume55f2d2d2016-02-26 13:20:48 -0800998 CLEAR_ERROR_BEFORE_ALLOC(&interface);
cblume790d5132016-02-29 11:13:29 -0800999
Brian Salomon7609ac62019-03-22 12:17:27 -04001000 if (caps.isConfigTexSupportEnabled(config)) {
cblume790d5132016-02-29 11:13:29 -08001001 // We never resize or change formats of textures.
cblume55f2d2d2016-02-26 13:20:48 -08001002 GL_ALLOC_CALL(&interface,
Robert Phillips590533f2017-07-11 14:22:35 -04001003 TexStorage2D(target, SkTMax(mipLevelCount, 1), internalFormatForTexStorage,
Brian Salomond1eaf492017-05-18 10:02:08 -04001004 baseWidth, baseHeight));
Brian Salomon0ec981b2017-05-15 13:48:50 -04001005 GrGLenum error = CHECK_ALLOC_ERROR(&interface);
cblume55f2d2d2016-02-26 13:20:48 -08001006 if (error != GR_GL_NO_ERROR) {
bsalomone699d0c2016-03-09 06:25:15 -08001007 return false;
cblume790d5132016-02-29 11:13:29 -08001008 } else {
Robert Phillips590533f2017-07-11 14:22:35 -04001009 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
cblume790d5132016-02-29 11:13:29 -08001010 const void* currentMipData = texels[currentMipLevel].fPixels;
1011 if (currentMipData == nullptr) {
1012 continue;
1013 }
1014 int twoToTheMipLevel = 1 << currentMipLevel;
Brian Salomond1eaf492017-05-18 10:02:08 -04001015 int currentWidth = SkTMax(1, baseWidth / twoToTheMipLevel);
1016 int currentHeight = SkTMax(1, baseHeight / twoToTheMipLevel);
cblume790d5132016-02-29 11:13:29 -08001017
1018 GR_GL_CALL(&interface,
1019 TexSubImage2D(target,
1020 currentMipLevel,
1021 0, // left
1022 0, // top
1023 currentWidth,
1024 currentHeight,
1025 externalFormat, externalType,
1026 currentMipData));
1027 }
bsalomone699d0c2016-03-09 06:25:15 -08001028 return true;
cblume790d5132016-02-29 11:13:29 -08001029 }
1030 } else {
Robert Phillips590533f2017-07-11 14:22:35 -04001031 if (!mipLevelCount) {
cblume790d5132016-02-29 11:13:29 -08001032 GL_ALLOC_CALL(&interface,
1033 TexImage2D(target,
bsalomone699d0c2016-03-09 06:25:15 -08001034 0,
cblume790d5132016-02-29 11:13:29 -08001035 internalFormat,
bsalomone699d0c2016-03-09 06:25:15 -08001036 baseWidth,
1037 baseHeight,
cblume790d5132016-02-29 11:13:29 -08001038 0, // border
1039 externalFormat, externalType,
bsalomone699d0c2016-03-09 06:25:15 -08001040 nullptr));
Brian Salomon0ec981b2017-05-15 13:48:50 -04001041 GrGLenum error = CHECK_ALLOC_ERROR(&interface);
cblume790d5132016-02-29 11:13:29 -08001042 if (error != GR_GL_NO_ERROR) {
bsalomone699d0c2016-03-09 06:25:15 -08001043 return false;
1044 }
1045 } else {
Robert Phillips590533f2017-07-11 14:22:35 -04001046 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
bsalomone699d0c2016-03-09 06:25:15 -08001047 int twoToTheMipLevel = 1 << currentMipLevel;
1048 int currentWidth = SkTMax(1, baseWidth / twoToTheMipLevel);
1049 int currentHeight = SkTMax(1, baseHeight / twoToTheMipLevel);
Greg Daniel8b059bd2017-09-28 20:46:45 +00001050 const void* currentMipData = texels[currentMipLevel].fPixels;
bsalomone699d0c2016-03-09 06:25:15 -08001051 // Even if curremtMipData is nullptr, continue to call TexImage2D.
1052 // This will allocate texture memory which we can later populate.
1053 GL_ALLOC_CALL(&interface,
1054 TexImage2D(target,
1055 currentMipLevel,
1056 internalFormat,
1057 currentWidth,
1058 currentHeight,
1059 0, // border
1060 externalFormat, externalType,
1061 currentMipData));
Brian Salomon0ec981b2017-05-15 13:48:50 -04001062 GrGLenum error = CHECK_ALLOC_ERROR(&interface);
bsalomone699d0c2016-03-09 06:25:15 -08001063 if (error != GR_GL_NO_ERROR) {
1064 return false;
1065 }
cblume790d5132016-02-29 11:13:29 -08001066 }
cblume55f2d2d2016-02-26 13:20:48 -08001067 }
1068 }
bsalomone699d0c2016-03-09 06:25:15 -08001069 return true;
cblume55f2d2d2016-02-26 13:20:48 -08001070}
1071
1072/**
Jim Van Verth1676cb92019-01-15 13:24:45 -05001073 * Creates storage space for the texture and fills it with texels.
1074 *
1075 * @param config Compressed pixel config of the texture.
1076 * @param interface The GL interface in use.
1077 * @param caps The capabilities of the GL device.
1078 * @param target Which bound texture to target (GR_GL_TEXTURE_2D, e.g.)
1079 * @param internalFormat The data format used for the internal storage of the texture.
1080 * @param texels The texel data of the texture being created.
1081 * @param mipLevelCount Number of mipmap levels
1082 * @param baseWidth The width of the texture's base mipmap level
1083 * @param baseHeight The height of the texture's base mipmap level
1084 */
1085static bool allocate_and_populate_compressed_texture(GrPixelConfig config,
1086 const GrGLInterface& interface,
1087 const GrGLCaps& caps,
1088 GrGLenum target, GrGLenum internalFormat,
1089 const GrMipLevel texels[], int mipLevelCount,
1090 int baseWidth, int baseHeight) {
1091 CLEAR_ERROR_BEFORE_ALLOC(&interface);
1092 SkASSERT(GrPixelConfigIsCompressed(config));
1093
1094 bool useTexStorage = caps.isConfigTexSupportEnabled(config);
1095 // We can only use TexStorage if we know we will not later change the storage requirements.
1096 // This means if we may later want to add mipmaps, we cannot use TexStorage.
1097 // Right now, we cannot know if we will later add mipmaps or not.
1098 // The only time we can use TexStorage is when we already have the
1099 // mipmaps.
1100 useTexStorage &= mipLevelCount > 1;
1101
1102 if (useTexStorage) {
1103 // We never resize or change formats of textures.
1104 GL_ALLOC_CALL(&interface,
1105 TexStorage2D(target,
1106 mipLevelCount,
1107 internalFormat,
1108 baseWidth, baseHeight));
1109 GrGLenum error = CHECK_ALLOC_ERROR(&interface);
1110 if (error != GR_GL_NO_ERROR) {
1111 return false;
1112 } else {
1113 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
1114 const void* currentMipData = texels[currentMipLevel].fPixels;
1115 if (currentMipData == nullptr) {
1116 // Compressed textures require data for every level
1117 return false;
1118 }
1119
1120 int twoToTheMipLevel = 1 << currentMipLevel;
1121 int currentWidth = SkTMax(1, baseWidth / twoToTheMipLevel);
1122 int currentHeight = SkTMax(1, baseHeight / twoToTheMipLevel);
1123
1124 // Make sure that the width and height that we pass to OpenGL
1125 // is a multiple of the block size.
1126 size_t dataSize = GrCompressedFormatDataSize(config, currentWidth, currentHeight);
1127 GR_GL_CALL(&interface, CompressedTexSubImage2D(target,
1128 currentMipLevel,
1129 0, // left
1130 0, // top
1131 currentWidth,
1132 currentHeight,
1133 internalFormat,
1134 SkToInt(dataSize),
1135 currentMipData));
1136 }
1137 }
1138 } else {
1139 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
1140 const void* currentMipData = texels[currentMipLevel].fPixels;
1141 if (currentMipData == nullptr) {
1142 // Compressed textures require data for every level
1143 return false;
1144 }
1145
1146 int twoToTheMipLevel = 1 << currentMipLevel;
1147 int currentWidth = SkTMax(1, baseWidth / twoToTheMipLevel);
1148 int currentHeight = SkTMax(1, baseHeight / twoToTheMipLevel);
1149
1150 // Make sure that the width and height that we pass to OpenGL
1151 // is a multiple of the block size.
1152 size_t dataSize = GrCompressedFormatDataSize(config, baseWidth, baseHeight);
1153
1154 GL_ALLOC_CALL(&interface,
1155 CompressedTexImage2D(target,
1156 currentMipLevel,
1157 internalFormat,
1158 currentWidth,
1159 currentHeight,
1160 0, // border
1161 SkToInt(dataSize),
1162 currentMipData));
1163
1164 GrGLenum error = CHECK_ALLOC_ERROR(&interface);
1165 if (error != GR_GL_NO_ERROR) {
1166 return false;
1167 }
1168 }
1169 }
1170
1171 return true;
1172}
1173/**
cblume55f2d2d2016-02-26 13:20:48 -08001174 * After a texture is created, any state which was altered during its creation
1175 * needs to be restored.
1176 *
1177 * @param interface The GL interface to use.
1178 * @param caps The capabilities of the GL device.
1179 * @param restoreGLRowLength Should the row length unpacking be restored?
1180 * @param glFlipY Did GL flip the texture vertically?
1181 */
1182static void restore_pixelstore_state(const GrGLInterface& interface, const GrGLCaps& caps,
Brian Salomona9b04b92018-06-01 15:04:28 -04001183 bool restoreGLRowLength) {
cblume55f2d2d2016-02-26 13:20:48 -08001184 if (restoreGLRowLength) {
1185 SkASSERT(caps.unpackRowLengthSupport());
1186 GR_GL_CALL(&interface, PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
1187 }
cblume55f2d2d2016-02-26 13:20:48 -08001188}
1189
Brian Osman9b560242017-09-05 15:34:52 -04001190void GrGLGpu::unbindCpuToGpuXferBuffer() {
Brian Salomonae64c192019-02-05 09:41:37 -05001191 auto* xferBufferState = this->hwBufferState(GrGpuBufferType::kXferCpuToGpu);
1192 if (!xferBufferState->fBoundBufferUniqueID.isInvalid()) {
1193 GL_CALL(BindBuffer(xferBufferState->fGLTarget, 0));
1194 xferBufferState->invalidate();
Brian Osman9b560242017-09-05 15:34:52 -04001195 }
Brian Osman9b560242017-09-05 15:34:52 -04001196}
1197
Brian Salomonc320b152018-02-20 14:05:36 -05001198// TODO: Make this take a GrColorType instead of dataConfig. This requires updating GrGLCaps to
1199// convert from GrColorType to externalFormat/externalType GLenum values.
Brian Salomona9b04b92018-06-01 15:04:28 -04001200bool GrGLGpu::uploadTexData(GrPixelConfig texConfig, int texWidth, int texHeight, GrGLenum target,
1201 UploadType uploadType, int left, int top, int width, int height,
1202 GrPixelConfig dataConfig, const GrMipLevel texels[], int mipLevelCount,
Greg Daniel0fc4d2d2017-10-12 11:23:36 -04001203 GrMipMapsStatus* mipMapsStatus) {
Jim Van Verth1676cb92019-01-15 13:24:45 -05001204 // If we're uploading compressed data then we should be using uploadCompressedTexData
1205 SkASSERT(!GrPixelConfigIsCompressed(dataConfig));
1206
Brian Salomond1eaf492017-05-18 10:02:08 -04001207 SkASSERT(this->caps()->isConfigTexturable(texConfig));
Greg Daniel660cc992017-06-26 14:55:05 -04001208 SkDEBUGCODE(
1209 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
1210 SkIRect bounds = SkIRect::MakeWH(texWidth, texHeight);
1211 SkASSERT(bounds.contains(subRect));
1212 )
Robert Phillips590533f2017-07-11 14:22:35 -04001213 SkASSERT(1 == mipLevelCount ||
Greg Daniel660cc992017-06-26 14:55:05 -04001214 (0 == left && 0 == top && width == texWidth && height == texHeight));
bsalomon5b30c6f2015-12-17 14:17:34 -08001215
Brian Osman9b560242017-09-05 15:34:52 -04001216 this->unbindCpuToGpuXferBuffer();
Jim Van Verth2e5eaf02017-06-21 15:55:46 -04001217
cblume55f2d2d2016-02-26 13:20:48 -08001218 // texels is const.
1219 // But we may need to flip the texture vertically to prepare it.
1220 // Rather than flip in place and alter the incoming data,
1221 // we allocate a new buffer to flip into.
1222 // This means we need to make a non-const shallow copy of texels.
Robert Phillips0f992772017-07-12 08:24:56 -04001223 SkAutoTMalloc<GrMipLevel> texelsShallowCopy;
1224
1225 if (mipLevelCount) {
1226 texelsShallowCopy.reset(mipLevelCount);
1227 memcpy(texelsShallowCopy.get(), texels, mipLevelCount*sizeof(GrMipLevel));
1228 }
cblume55f2d2d2016-02-26 13:20:48 -08001229
cblume55f2d2d2016-02-26 13:20:48 -08001230 const GrGLInterface* interface = this->glInterface();
1231 const GrGLCaps& caps = this->glCaps();
1232
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001233 size_t bpp = GrBytesPerPixel(dataConfig);
cblume55f2d2d2016-02-26 13:20:48 -08001234
1235 if (width == 0 || height == 0) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +00001236 return false;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001237 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00001238
bsalomon5b30c6f2015-12-17 14:17:34 -08001239 // Internal format comes from the texture desc.
bsalomon76148af2016-01-12 11:13:47 -08001240 GrGLenum internalFormat;
bsalomon5b30c6f2015-12-17 14:17:34 -08001241 // External format and type come from the upload data.
bsalomon76148af2016-01-12 11:13:47 -08001242 GrGLenum externalFormat;
1243 GrGLenum externalType;
Brian Salomond1eaf492017-05-18 10:02:08 -04001244 if (!this->glCaps().getTexImageFormats(texConfig, dataConfig, &internalFormat, &externalFormat,
1245 &externalType)) {
bsalomon76148af2016-01-12 11:13:47 -08001246 return false;
1247 }
brianosman81a84852016-09-12 09:05:14 -07001248 // TexStorage requires a sized format, and internalFormat may or may not be
Brian Salomond1eaf492017-05-18 10:02:08 -04001249 GrGLenum internalFormatForTexStorage = this->glCaps().configSizedInternalFormat(texConfig);
brianosman81a84852016-09-12 09:05:14 -07001250
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001251 /*
bsalomon5b30c6f2015-12-17 14:17:34 -08001252 * Check whether to allocate a temporary buffer for flipping y or
bsalomon@google.com6f379512011-11-16 20:36:03 +00001253 * because our srcData has extra bytes past each row. If so, we need
1254 * to trim those off here, since GL ES may not let us specify
1255 * GL_UNPACK_ROW_LENGTH.
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001256 */
bsalomon@google.com6f379512011-11-16 20:36:03 +00001257 bool restoreGLRowLength = false;
cblume55f2d2d2016-02-26 13:20:48 -08001258
1259 // in case we need a temporary, trimmed copy of the src pixels
1260 SkAutoSMalloc<128 * 128> tempStorage;
1261
Greg Daniel0fc4d2d2017-10-12 11:23:36 -04001262 if (mipMapsStatus) {
1263 *mipMapsStatus = GrMipMapsStatus::kValid;
Greg Daniel834f1202017-10-09 15:06:20 -04001264 }
1265
Robert Phillips931f7512017-11-02 12:57:18 -04001266 const bool usesMips = mipLevelCount > 1;
1267
cblume55f2d2d2016-02-26 13:20:48 -08001268 // find the combined size of all the mip levels and the relative offset of
1269 // each into the collective buffer
Robert Phillips931f7512017-11-02 12:57:18 -04001270 bool willNeedData = false;
Greg Daniel55afd6d2017-09-29 09:32:44 -04001271 size_t combinedBufferSize = 0;
1272 SkTArray<size_t> individualMipOffsets(mipLevelCount);
Robert Phillips590533f2017-07-11 14:22:35 -04001273 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
Greg Daniel55afd6d2017-09-29 09:32:44 -04001274 if (texelsShallowCopy[currentMipLevel].fPixels) {
1275 int twoToTheMipLevel = 1 << currentMipLevel;
1276 int currentWidth = SkTMax(1, width / twoToTheMipLevel);
1277 int currentHeight = SkTMax(1, height / twoToTheMipLevel);
Robert Phillips931f7512017-11-02 12:57:18 -04001278 const size_t trimRowBytes = currentWidth * bpp;
1279 const size_t trimmedSize = trimRowBytes * currentHeight;
1280
1281 const size_t rowBytes = texelsShallowCopy[currentMipLevel].fRowBytes
1282 ? texelsShallowCopy[currentMipLevel].fRowBytes
1283 : trimRowBytes;
1284
Brian Salomona9b04b92018-06-01 15:04:28 -04001285 if (((!caps.unpackRowLengthSupport() || usesMips) && trimRowBytes != rowBytes)) {
Robert Phillips931f7512017-11-02 12:57:18 -04001286 willNeedData = true;
1287 }
1288
Greg Daniel55afd6d2017-09-29 09:32:44 -04001289 individualMipOffsets.push_back(combinedBufferSize);
1290 combinedBufferSize += trimmedSize;
1291 } else {
Greg Daniel0fc4d2d2017-10-12 11:23:36 -04001292 if (mipMapsStatus) {
1293 *mipMapsStatus = GrMipMapsStatus::kDirty;
Greg Daniel834f1202017-10-09 15:06:20 -04001294 }
Greg Daniel55afd6d2017-09-29 09:32:44 -04001295 individualMipOffsets.push_back(0);
1296 }
Greg Daniel0fc4d2d2017-10-12 11:23:36 -04001297 }
1298 if (mipMapsStatus && mipLevelCount <= 1) {
1299 *mipMapsStatus = GrMipMapsStatus::kNotAllocated;
cblume55f2d2d2016-02-26 13:20:48 -08001300 }
Robert Phillips931f7512017-11-02 12:57:18 -04001301 char* buffer = nullptr;
1302 if (willNeedData) {
1303 buffer = (char*)tempStorage.reset(combinedBufferSize);
1304 }
cblume55f2d2d2016-02-26 13:20:48 -08001305
Robert Phillips590533f2017-07-11 14:22:35 -04001306 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
Greg Daniel55afd6d2017-09-29 09:32:44 -04001307 if (!texelsShallowCopy[currentMipLevel].fPixels) {
1308 continue;
1309 }
cblume55f2d2d2016-02-26 13:20:48 -08001310 int twoToTheMipLevel = 1 << currentMipLevel;
1311 int currentWidth = SkTMax(1, width / twoToTheMipLevel);
1312 int currentHeight = SkTMax(1, height / twoToTheMipLevel);
1313 const size_t trimRowBytes = currentWidth * bpp;
1314
1315 /*
1316 * check whether to allocate a temporary buffer for flipping y or
1317 * because our srcData has extra bytes past each row. If so, we need
1318 * to trim those off here, since GL ES may not let us specify
1319 * GL_UNPACK_ROW_LENGTH.
1320 */
1321 restoreGLRowLength = false;
1322
Greg Daniel55afd6d2017-09-29 09:32:44 -04001323 const size_t rowBytes = texelsShallowCopy[currentMipLevel].fRowBytes
1324 ? texelsShallowCopy[currentMipLevel].fRowBytes
1325 : trimRowBytes;
Greg Daniel660cc992017-06-26 14:55:05 -04001326
ericrk154349b2016-05-10 14:36:53 -07001327 // TODO: This optimization should be enabled with or without mips.
1328 // For use with mips, we must set GR_GL_UNPACK_ROW_LENGTH once per
1329 // mip level, before calling glTexImage2D.
Brian Salomona9b04b92018-06-01 15:04:28 -04001330 if (caps.unpackRowLengthSupport() && !usesMips) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001331 // can't use this for flipping, only non-neg values allowed. :(
1332 if (rowBytes != trimRowBytes) {
1333 GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
cblume55f2d2d2016-02-26 13:20:48 -08001334 GR_GL_CALL(interface, PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength));
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001335 restoreGLRowLength = true;
1336 }
Brian Salomona9b04b92018-06-01 15:04:28 -04001337 } else if (trimRowBytes != rowBytes) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -04001338 // copy data into our new storage, skipping the trailing bytes
1339 const char* src = (const char*)texelsShallowCopy[currentMipLevel].fPixels;
Greg Daniel55afd6d2017-09-29 09:32:44 -04001340 char* dst = buffer + individualMipOffsets[currentMipLevel];
Brian Salomona6948702018-06-01 15:33:20 -04001341 SkRectMemcpy(dst, trimRowBytes, src, rowBytes, trimRowBytes, currentHeight);
Jim Van Verth2e5eaf02017-06-21 15:55:46 -04001342 // now point data to our copied version
1343 texelsShallowCopy[currentMipLevel].fPixels = buffer +
Greg Daniel55afd6d2017-09-29 09:32:44 -04001344 individualMipOffsets[currentMipLevel];
Jim Van Verth2e5eaf02017-06-21 15:55:46 -04001345 texelsShallowCopy[currentMipLevel].fRowBytes = trimRowBytes;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001346 }
bsalomone699d0c2016-03-09 06:25:15 -08001347 }
1348
Robert Phillips590533f2017-07-11 14:22:35 -04001349 if (mipLevelCount) {
Brian Salomond1eaf492017-05-18 10:02:08 -04001350 GR_GL_CALL(interface, PixelStorei(GR_GL_UNPACK_ALIGNMENT, config_alignment(texConfig)));
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001351 }
cblume55f2d2d2016-02-26 13:20:48 -08001352
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001353 bool succeeded = true;
Brian Salomond1eaf492017-05-18 10:02:08 -04001354 if (kNewTexture_UploadType == uploadType) {
1355 if (0 == left && 0 == top && texWidth == width && texHeight == height) {
Robert Phillips92de6312017-05-23 07:43:48 -04001356 succeeded = allocate_and_populate_texture(
Brian Salomond1eaf492017-05-18 10:02:08 -04001357 texConfig, *interface, caps, target, internalFormat,
Robert Phillips590533f2017-07-11 14:22:35 -04001358 internalFormatForTexStorage, externalFormat, externalType,
1359 texelsShallowCopy, mipLevelCount, width, height);
Brian Salomond1eaf492017-05-18 10:02:08 -04001360 } else {
1361 succeeded = false;
1362 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001363 } else {
Robert Phillips590533f2017-07-11 14:22:35 -04001364 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
Greg Daniel55afd6d2017-09-29 09:32:44 -04001365 if (!texelsShallowCopy[currentMipLevel].fPixels) {
1366 continue;
1367 }
cblume55f2d2d2016-02-26 13:20:48 -08001368 int twoToTheMipLevel = 1 << currentMipLevel;
1369 int currentWidth = SkTMax(1, width / twoToTheMipLevel);
1370 int currentHeight = SkTMax(1, height / twoToTheMipLevel);
cblume55f2d2d2016-02-26 13:20:48 -08001371
1372 GL_CALL(TexSubImage2D(target,
1373 currentMipLevel,
1374 left, top,
1375 currentWidth,
1376 currentHeight,
1377 externalFormat, externalType,
1378 texelsShallowCopy[currentMipLevel].fPixels));
1379 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00001380 }
1381
Brian Salomona9b04b92018-06-01 15:04:28 -04001382 restore_pixelstore_state(*interface, caps, restoreGLRowLength);
cblume55f2d2d2016-02-26 13:20:48 -08001383
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001384 return succeeded;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001385}
1386
Jim Van Verth1676cb92019-01-15 13:24:45 -05001387bool GrGLGpu::uploadCompressedTexData(GrPixelConfig texConfig, int texWidth, int texHeight,
1388 GrGLenum target, GrPixelConfig dataConfig,
1389 const GrMipLevel texels[], int mipLevelCount,
1390 GrMipMapsStatus* mipMapsStatus) {
1391 SkASSERT(this->caps()->isConfigTexturable(texConfig));
1392
1393 const GrGLInterface* interface = this->glInterface();
1394 const GrGLCaps& caps = this->glCaps();
1395
1396 // We only need the internal format for compressed 2D textures.
1397 GrGLenum internalFormat;
1398 if (!caps.getCompressedTexImageFormats(texConfig, &internalFormat)) {
1399 return false;
1400 }
1401
Greg Kaiser73bfb892019-02-11 09:03:41 -08001402 if (mipMapsStatus) {
1403 if (mipLevelCount <= 1) {
1404 *mipMapsStatus = GrMipMapsStatus::kNotAllocated;
1405 } else {
1406 *mipMapsStatus = GrMipMapsStatus::kValid;
1407 }
Jim Van Verth1676cb92019-01-15 13:24:45 -05001408 }
1409
1410 return allocate_and_populate_compressed_texture(texConfig, *interface, caps, target,
1411 internalFormat, texels, mipLevelCount,
1412 texWidth, texHeight);
Jim Van Verth1676cb92019-01-15 13:24:45 -05001413}
1414
bsalomon424cc262015-05-22 10:37:30 -07001415static bool renderbuffer_storage_msaa(const GrGLContext& ctx,
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001416 int sampleCount,
1417 GrGLenum format,
1418 int width, int height) {
robertphillips@google.com6177e692013-02-28 20:16:25 +00001419 CLEAR_ERROR_BEFORE_ALLOC(ctx.interface());
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +00001420 SkASSERT(GrGLCaps::kNone_MSFBOType != ctx.caps()->msFBOType());
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +00001421 switch (ctx.caps()->msFBOType()) {
Brian Salomon00731b42016-10-14 11:30:51 -04001422 case GrGLCaps::kStandard_MSFBOType:
vbuzinovdded6962015-06-12 08:59:45 -07001423 case GrGLCaps::kMixedSamples_MSFBOType:
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001424 GL_ALLOC_CALL(ctx.interface(),
1425 RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
1426 sampleCount,
1427 format,
1428 width, height));
1429 break;
1430 case GrGLCaps::kES_Apple_MSFBOType:
1431 GL_ALLOC_CALL(ctx.interface(),
1432 RenderbufferStorageMultisampleES2APPLE(GR_GL_RENDERBUFFER,
1433 sampleCount,
1434 format,
1435 width, height));
1436 break;
1437 case GrGLCaps::kES_EXT_MsToTexture_MSFBOType:
1438 case GrGLCaps::kES_IMG_MsToTexture_MSFBOType:
1439 GL_ALLOC_CALL(ctx.interface(),
1440 RenderbufferStorageMultisampleES2EXT(GR_GL_RENDERBUFFER,
1441 sampleCount,
1442 format,
1443 width, height));
1444 break;
1445 case GrGLCaps::kNone_MSFBOType:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -04001446 SK_ABORT("Shouldn't be here if we don't support multisampled renderbuffers.");
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001447 break;
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001448 }
Brian Salomon9251d4e2015-03-17 16:03:19 -04001449 return (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctx.interface()));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001450}
1451
egdanielb0e1be22015-04-22 13:27:39 -07001452bool GrGLGpu::createRenderTargetObjects(const GrSurfaceDesc& desc,
bsalomon091f60c2015-11-10 11:54:56 -08001453 const GrGLTextureInfo& texInfo,
bsalomonb15b4c12014-10-29 12:41:57 -07001454 GrGLRenderTarget::IDDesc* idDesc) {
1455 idDesc->fMSColorRenderbufferID = 0;
egdanield803f272015-03-18 13:01:52 -07001456 idDesc->fRTFBOID = 0;
kkinnunen2e6055b2016-04-22 01:48:29 -07001457 idDesc->fRTFBOOwnership = GrBackendObjectOwnership::kOwned;
egdanield803f272015-03-18 13:01:52 -07001458 idDesc->fTexFBOID = 0;
robertphillips76948d42016-05-04 12:47:41 -07001459 SkASSERT((GrGLCaps::kMixedSamples_MSFBOType == this->glCaps().msFBOType()) ==
1460 this->caps()->usesMixedSamples());
Brian Salomonbdecacf2018-02-02 20:32:49 -05001461 idDesc->fIsMixedSampled = desc.fSampleCnt > 1 && this->caps()->usesMixedSamples();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001462
1463 GrGLenum status;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001464
bsalomona11e5fc2015-12-18 07:59:41 -08001465 GrGLenum colorRenderbufferFormat = 0; // suppress warning
bsalomon@google.comab15d612011-08-09 12:57:56 +00001466
Brian Salomonbdecacf2018-02-02 20:32:49 -05001467 if (desc.fSampleCnt > 1 && GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.com347c3822013-05-01 20:10:01 +00001468 goto FAILED;
1469 }
1470
egdanield803f272015-03-18 13:01:52 -07001471 GL_CALL(GenFramebuffers(1, &idDesc->fTexFBOID));
1472 if (!idDesc->fTexFBOID) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001473 goto FAILED;
1474 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001475
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001476 // If we are using multisampling we will create two FBOS. We render to one and then resolve to
1477 // the texture bound to the other. The exception is the IMG multisample extension. With this
1478 // extension the texture is multisampled when rendered to and then auto-resolves it when it is
1479 // rendered from.
Brian Salomonbdecacf2018-02-02 20:32:49 -05001480 if (desc.fSampleCnt > 1 && this->glCaps().usesMSAARenderBuffers()) {
egdanield803f272015-03-18 13:01:52 -07001481 GL_CALL(GenFramebuffers(1, &idDesc->fRTFBOID));
bsalomonb15b4c12014-10-29 12:41:57 -07001482 GL_CALL(GenRenderbuffers(1, &idDesc->fMSColorRenderbufferID));
egdanield803f272015-03-18 13:01:52 -07001483 if (!idDesc->fRTFBOID ||
bsalomona11e5fc2015-12-18 07:59:41 -08001484 !idDesc->fMSColorRenderbufferID) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001485 goto FAILED;
1486 }
Brian Salomon93348dd2018-08-29 12:56:23 -04001487 this->glCaps().getRenderbufferFormat(desc.fConfig, &colorRenderbufferFormat);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001488 } else {
egdanield803f272015-03-18 13:01:52 -07001489 idDesc->fRTFBOID = idDesc->fTexFBOID;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001490 }
1491
egdanield803f272015-03-18 13:01:52 -07001492 // below here we may bind the FBO
Robert Phillips294870f2016-11-11 12:38:40 -05001493 fHWBoundRenderTargetUniqueID.makeInvalid();
egdanield803f272015-03-18 13:01:52 -07001494 if (idDesc->fRTFBOID != idDesc->fTexFBOID) {
Brian Salomonbdecacf2018-02-02 20:32:49 -05001495 SkASSERT(desc.fSampleCnt > 1);
bsalomonb15b4c12014-10-29 12:41:57 -07001496 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, idDesc->fMSColorRenderbufferID));
bsalomon424cc262015-05-22 10:37:30 -07001497 if (!renderbuffer_storage_msaa(*fGLContext,
bsalomonb15b4c12014-10-29 12:41:57 -07001498 desc.fSampleCnt,
bsalomona11e5fc2015-12-18 07:59:41 -08001499 colorRenderbufferFormat,
bsalomonb15b4c12014-10-29 12:41:57 -07001500 desc.fWidth, desc.fHeight)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001501 goto FAILED;
1502 }
Adrienne Walker4ee88512018-05-17 11:37:14 -07001503 this->bindFramebuffer(GR_GL_FRAMEBUFFER, idDesc->fRTFBOID);
egdanield803f272015-03-18 13:01:52 -07001504 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon10528f12015-10-14 12:54:52 -07001505 GR_GL_COLOR_ATTACHMENT0,
1506 GR_GL_RENDERBUFFER,
1507 idDesc->fMSColorRenderbufferID));
Brian Salomon0ec981b2017-05-15 13:48:50 -04001508 if (!this->glCaps().isConfigVerifiedColorAttachment(desc.fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001509 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1510 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
1511 goto FAILED;
1512 }
bsalomon424cc262015-05-22 10:37:30 -07001513 fGLContext->caps()->markConfigAsValidColorAttachment(desc.fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001514 }
1515 }
Adrienne Walker4ee88512018-05-17 11:37:14 -07001516 this->bindFramebuffer(GR_GL_FRAMEBUFFER, idDesc->fTexFBOID);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001517
Brian Salomonbdecacf2018-02-02 20:32:49 -05001518 if (this->glCaps().usesImplicitMSAAResolve() && desc.fSampleCnt > 1) {
egdanield803f272015-03-18 13:01:52 -07001519 GL_CALL(FramebufferTexture2DMultisample(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001520 GR_GL_COLOR_ATTACHMENT0,
bsalomon091f60c2015-11-10 11:54:56 -08001521 texInfo.fTarget,
1522 texInfo.fID, 0, desc.fSampleCnt));
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001523 } else {
egdanield803f272015-03-18 13:01:52 -07001524 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001525 GR_GL_COLOR_ATTACHMENT0,
bsalomon091f60c2015-11-10 11:54:56 -08001526 texInfo.fTarget,
1527 texInfo.fID, 0));
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001528 }
Brian Salomon0ec981b2017-05-15 13:48:50 -04001529 if (!this->glCaps().isConfigVerifiedColorAttachment(desc.fConfig)) {
egdanield803f272015-03-18 13:01:52 -07001530 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001531 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
1532 goto FAILED;
1533 }
bsalomon424cc262015-05-22 10:37:30 -07001534 fGLContext->caps()->markConfigAsValidColorAttachment(desc.fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001535 }
1536
1537 return true;
1538
1539FAILED:
bsalomonb15b4c12014-10-29 12:41:57 -07001540 if (idDesc->fMSColorRenderbufferID) {
1541 GL_CALL(DeleteRenderbuffers(1, &idDesc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001542 }
egdanield803f272015-03-18 13:01:52 -07001543 if (idDesc->fRTFBOID != idDesc->fTexFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -07001544 this->deleteFramebuffer(idDesc->fRTFBOID);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001545 }
egdanield803f272015-03-18 13:01:52 -07001546 if (idDesc->fTexFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -07001547 this->deleteFramebuffer(idDesc->fTexFBOID);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001548 }
1549 return false;
1550}
1551
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001552// good to set a break-point here to know when createTexture fails
Robert Phillips67d52cf2017-06-05 13:38:13 -04001553static sk_sp<GrTexture> return_null_texture() {
mtklein@google.com330313a2013-08-22 15:37:26 +00001554// SkDEBUGFAIL("null texture");
halcanary96fcdcc2015-08-27 07:41:13 -07001555 return nullptr;
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001556}
1557
Brian Salomondc829942018-10-23 16:07:24 -04001558static GrGLTexture::SamplerParams set_initial_texture_params(const GrGLInterface* interface,
1559 const GrGLTextureInfo& info) {
cblume55f2d2d2016-02-26 13:20:48 -08001560 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
1561 // drivers have a bug where an FBO won't be complete if it includes a
1562 // texture that is not mipmap complete (considering the filter in use).
Brian Salomondc829942018-10-23 16:07:24 -04001563 GrGLTexture::SamplerParams params;
1564 params.fMinFilter = GR_GL_NEAREST;
1565 params.fMagFilter = GR_GL_NEAREST;
1566 params.fWrapS = GR_GL_CLAMP_TO_EDGE;
1567 params.fWrapT = GR_GL_CLAMP_TO_EDGE;
1568 GR_GL_CALL(interface, TexParameteri(info.fTarget, GR_GL_TEXTURE_MAG_FILTER, params.fMagFilter));
1569 GR_GL_CALL(interface, TexParameteri(info.fTarget, GR_GL_TEXTURE_MIN_FILTER, params.fMinFilter));
1570 GR_GL_CALL(interface, TexParameteri(info.fTarget, GR_GL_TEXTURE_WRAP_S, params.fWrapS));
1571 GR_GL_CALL(interface, TexParameteri(info.fTarget, GR_GL_TEXTURE_WRAP_T, params.fWrapT));
1572 return params;
cblume55f2d2d2016-02-26 13:20:48 -08001573}
1574
Robert Phillips67d52cf2017-06-05 13:38:13 -04001575sk_sp<GrTexture> GrGLGpu::onCreateTexture(const GrSurfaceDesc& desc,
1576 SkBudgeted budgeted,
Robert Phillips590533f2017-07-11 14:22:35 -04001577 const GrMipLevel texels[],
1578 int mipLevelCount) {
bsalomon@google.com8a70eef2013-03-19 13:58:55 +00001579 // We fail if the MSAA was requested and is not available.
Brian Salomonbdecacf2018-02-02 20:32:49 -05001580 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType() && desc.fSampleCnt > 1) {
tfarina38406c82014-10-31 07:11:12 -07001581 //SkDebugf("MSAA RT requested but not supported on this platform.");
bsalomon@google.com8a70eef2013-03-19 13:58:55 +00001582 return return_null_texture();
1583 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001584
Jim Van Verth1676cb92019-01-15 13:24:45 -05001585 bool performClear = (desc.fFlags & kPerformInitialClear_GrSurfaceFlag) &&
1586 !GrPixelConfigIsCompressed(desc.fConfig);
Brian Salomond17b4a62017-05-23 16:53:47 -04001587
Robert Phillips590533f2017-07-11 14:22:35 -04001588 GrMipLevel zeroLevel;
Brian Salomond17b4a62017-05-23 16:53:47 -04001589 std::unique_ptr<uint8_t[]> zeros;
Brian Salomond17b4a62017-05-23 16:53:47 -04001590 if (performClear && !this->glCaps().clearTextureSupport() &&
Brian Salomon57111332018-02-05 15:55:54 -05001591 !this->glCaps().canConfigBeFBOColorAttachment(desc.fConfig)) {
Brian Salomond17b4a62017-05-23 16:53:47 -04001592 size_t rowSize = GrBytesPerPixel(desc.fConfig) * desc.fWidth;
1593 size_t size = rowSize * desc.fHeight;
1594 zeros.reset(new uint8_t[size]);
1595 memset(zeros.get(), 0, size);
Robert Phillips590533f2017-07-11 14:22:35 -04001596 zeroLevel.fPixels = zeros.get();
1597 zeroLevel.fRowBytes = 0;
1598 texels = &zeroLevel;
1599 mipLevelCount = 1;
Brian Salomond17b4a62017-05-23 16:53:47 -04001600 performClear = false;
1601 }
1602
Brian Salomond34edf32017-05-19 15:45:48 -04001603 bool isRenderTarget = SkToBool(desc.fFlags & kRenderTarget_GrSurfaceFlag);
reed@google.comac10a2d2010-12-22 21:39:39 +00001604
bsalomonb15b4c12014-10-29 12:41:57 -07001605 GrGLTexture::IDDesc idDesc;
kkinnunen2e6055b2016-04-22 01:48:29 -07001606 idDesc.fOwnership = GrBackendObjectOwnership::kOwned;
Greg Daniel0fc4d2d2017-10-12 11:23:36 -04001607 GrMipMapsStatus mipMapsStatus;
Brian Salomondc829942018-10-23 16:07:24 -04001608 GrGLTexture::SamplerParams initialTexParams;
Brian Salomon58389b92018-03-07 13:01:25 -05001609 if (!this->createTextureImpl(desc, &idDesc.fInfo, isRenderTarget, &initialTexParams, texels,
1610 mipLevelCount, &mipMapsStatus)) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001611 return return_null_texture();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001612 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001613
Robert Phillips67d52cf2017-06-05 13:38:13 -04001614 sk_sp<GrGLTexture> tex;
Brian Salomond34edf32017-05-19 15:45:48 -04001615 if (isRenderTarget) {
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001616 // unbind the texture from the texture unit before binding it to the frame buffer
bsalomon091f60c2015-11-10 11:54:56 -08001617 GL_CALL(BindTexture(idDesc.fInfo.fTarget, 0));
bsalomon5236cf42015-01-14 10:42:08 -08001618 GrGLRenderTarget::IDDesc rtIDDesc;
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001619
kkinnunen2e6055b2016-04-22 01:48:29 -07001620 if (!this->createRenderTargetObjects(desc, idDesc.fInfo, &rtIDDesc)) {
bsalomon091f60c2015-11-10 11:54:56 -08001621 GL_CALL(DeleteTextures(1, &idDesc.fInfo.fID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001622 return return_null_texture();
1623 }
Robert Phillips67d52cf2017-06-05 13:38:13 -04001624 tex = sk_make_sp<GrGLTextureRenderTarget>(this, budgeted, desc, idDesc, rtIDDesc,
Greg Daniel0fc4d2d2017-10-12 11:23:36 -04001625 mipMapsStatus);
Brian Salomon9bada542017-06-12 12:09:30 -04001626 tex->baseLevelWasBoundToFBO();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001627 } else {
Greg Daniel0fc4d2d2017-10-12 11:23:36 -04001628 tex = sk_make_sp<GrGLTexture>(this, budgeted, desc, idDesc, mipMapsStatus);
reed@google.comac10a2d2010-12-22 21:39:39 +00001629 }
Brian Salomondc829942018-10-23 16:07:24 -04001630
1631 tex->setCachedParams(&initialTexParams, tex->getCachedNonSamplerParams(),
1632 this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00001633#ifdef TRACE_TEXTURE_CREATION
tfarina38406c82014-10-31 07:11:12 -07001634 SkDebugf("--- new texture [%d] size=(%d %d) config=%d\n",
brianosmanc4459f62016-07-19 08:02:20 -07001635 idDesc.fInfo.fID, desc.fWidth, desc.fHeight, desc.fConfig);
reed@google.comac10a2d2010-12-22 21:39:39 +00001636#endif
Brian Salomond17b4a62017-05-23 16:53:47 -04001637 if (tex && performClear) {
1638 if (this->glCaps().clearTextureSupport()) {
Brian Salomond17b4a62017-05-23 16:53:47 -04001639 static constexpr uint32_t kZero = 0;
Brian Salomon57111332018-02-05 15:55:54 -05001640 GL_CALL(ClearTexImage(tex->textureID(), 0, GR_GL_RGBA, GR_GL_UNSIGNED_BYTE, &kZero));
Brian Salomond17b4a62017-05-23 16:53:47 -04001641 } else {
Brian Salomond17b4a62017-05-23 16:53:47 -04001642 GrGLIRect viewport;
Robert Phillips67d52cf2017-06-05 13:38:13 -04001643 this->bindSurfaceFBOForPixelOps(tex.get(), GR_GL_FRAMEBUFFER, &viewport,
1644 kDst_TempFBOTarget);
Brian Salomond818ebf2018-07-02 14:08:49 +00001645 this->disableScissor();
Brian Salomond17b4a62017-05-23 16:53:47 -04001646 this->disableWindowRectangles();
Brian Salomon805cc7a2019-01-28 09:52:34 -05001647 this->flushColorWrite(true);
1648 this->flushClearColor(0, 0, 0, 0);
Brian Salomond17b4a62017-05-23 16:53:47 -04001649 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
Robert Phillips67d52cf2017-06-05 13:38:13 -04001650 this->unbindTextureFBOForPixelOps(GR_GL_FRAMEBUFFER, tex.get());
Brian Salomond17b4a62017-05-23 16:53:47 -04001651 fHWBoundRenderTargetUniqueID.makeInvalid();
1652 }
1653 }
Kevin Lubickf7621cb2018-04-16 15:51:44 -04001654 return std::move(tex);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001655}
1656
1657namespace {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001658
egdaniel8dc7c3a2015-04-16 11:22:42 -07001659const GrGLuint kUnknownBitCount = GrGLStencilAttachment::kUnknownBitCount;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001660
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001661void inline get_stencil_rb_sizes(const GrGLInterface* gl,
egdaniel8dc7c3a2015-04-16 11:22:42 -07001662 GrGLStencilAttachment::Format* format) {
sugoi@google.com6ba0b0f2013-05-03 13:52:32 +00001663
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001664 // we shouldn't ever know one size and not the other
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001665 SkASSERT((kUnknownBitCount == format->fStencilBits) ==
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001666 (kUnknownBitCount == format->fTotalBits));
1667 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001668 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001669 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1670 (GrGLint*)&format->fStencilBits);
1671 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001672 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001673 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1674 (GrGLint*)&format->fTotalBits);
1675 format->fTotalBits += format->fStencilBits;
1676 } else {
1677 format->fTotalBits = format->fStencilBits;
1678 }
1679 }
1680}
1681}
1682
egdanielff1d5472015-09-10 08:37:20 -07001683int GrGLGpu::getCompatibleStencilIndex(GrPixelConfig config) {
bsalomon100b8f82015-10-28 08:37:44 -07001684 static const int kSize = 16;
Brian Salomonbdecacf2018-02-02 20:32:49 -05001685 SkASSERT(this->caps()->isConfigRenderable(config));
bsalomon30447372015-12-21 09:03:05 -08001686 if (!this->glCaps().hasStencilFormatBeenDeterminedForConfig(config)) {
1687 // Default to unsupported, set this if we find a stencil format that works.
1688 int firstWorkingStencilFormatIndex = -1;
Brian Osman91f9a2c2017-09-05 15:02:46 -04001689
egdanielff1d5472015-09-10 08:37:20 -07001690 // Create color texture
kkinnunen546eb5c2015-12-11 00:05:33 -08001691 GrGLuint colorID = 0;
egdanielff1d5472015-09-10 08:37:20 -07001692 GL_CALL(GenTextures(1, &colorID));
Brian Salomond978b902019-02-07 15:09:18 -05001693 this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, colorID);
egdanielff1d5472015-09-10 08:37:20 -07001694 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1695 GR_GL_TEXTURE_MAG_FILTER,
1696 GR_GL_NEAREST));
1697 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1698 GR_GL_TEXTURE_MIN_FILTER,
1699 GR_GL_NEAREST));
1700 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1701 GR_GL_TEXTURE_WRAP_S,
1702 GR_GL_CLAMP_TO_EDGE));
1703 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1704 GR_GL_TEXTURE_WRAP_T,
1705 GR_GL_CLAMP_TO_EDGE));
1706
bsalomon76148af2016-01-12 11:13:47 -08001707 GrGLenum internalFormat;
1708 GrGLenum externalFormat;
1709 GrGLenum externalType;
1710 if (!this->glCaps().getTexImageFormats(config, config, &internalFormat, &externalFormat,
1711 &externalType)) {
1712 return false;
1713 }
Brian Osman9b560242017-09-05 15:34:52 -04001714 this->unbindCpuToGpuXferBuffer();
egdanielff1d5472015-09-10 08:37:20 -07001715 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1716 GL_ALLOC_CALL(this->glInterface(), TexImage2D(GR_GL_TEXTURE_2D,
bsalomon926cb022015-12-17 18:15:11 -08001717 0,
bsalomon76148af2016-01-12 11:13:47 -08001718 internalFormat,
bsalomon100b8f82015-10-28 08:37:44 -07001719 kSize,
1720 kSize,
egdanielff1d5472015-09-10 08:37:20 -07001721 0,
bsalomon76148af2016-01-12 11:13:47 -08001722 externalFormat,
1723 externalType,
Ben Wagnera93a14a2017-08-28 10:34:05 -04001724 nullptr));
bsalomon30447372015-12-21 09:03:05 -08001725 if (GR_GL_NO_ERROR != CHECK_ALLOC_ERROR(this->glInterface())) {
egdanielff1d5472015-09-10 08:37:20 -07001726 GL_CALL(DeleteTextures(1, &colorID));
bsalomon30447372015-12-21 09:03:05 -08001727 return -1;
egdanielff1d5472015-09-10 08:37:20 -07001728 }
1729
1730 // unbind the texture from the texture unit before binding it to the frame buffer
1731 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, 0));
1732
1733 // Create Framebuffer
kkinnunen546eb5c2015-12-11 00:05:33 -08001734 GrGLuint fb = 0;
egdanielff1d5472015-09-10 08:37:20 -07001735 GL_CALL(GenFramebuffers(1, &fb));
Adrienne Walker4ee88512018-05-17 11:37:14 -07001736 this->bindFramebuffer(GR_GL_FRAMEBUFFER, fb);
Robert Phillips294870f2016-11-11 12:38:40 -05001737 fHWBoundRenderTargetUniqueID.makeInvalid();
egdanielff1d5472015-09-10 08:37:20 -07001738 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
1739 GR_GL_COLOR_ATTACHMENT0,
1740 GR_GL_TEXTURE_2D,
1741 colorID,
1742 0));
bsalomon30447372015-12-21 09:03:05 -08001743 GrGLuint sbRBID = 0;
1744 GL_CALL(GenRenderbuffers(1, &sbRBID));
egdanielff1d5472015-09-10 08:37:20 -07001745
1746 // look over formats till I find a compatible one
1747 int stencilFmtCnt = this->glCaps().stencilFormats().count();
bsalomon30447372015-12-21 09:03:05 -08001748 if (sbRBID) {
egdanielff1d5472015-09-10 08:37:20 -07001749 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbRBID));
bsalomon30447372015-12-21 09:03:05 -08001750 for (int i = 0; i < stencilFmtCnt && sbRBID; ++i) {
1751 const GrGLCaps::StencilFormat& sFmt = this->glCaps().stencilFormats()[i];
1752 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1753 GL_ALLOC_CALL(this->glInterface(), RenderbufferStorage(GR_GL_RENDERBUFFER,
1754 sFmt.fInternalFormat,
1755 kSize, kSize));
1756 if (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(this->glInterface())) {
egdanielff1d5472015-09-10 08:37:20 -07001757 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon30447372015-12-21 09:03:05 -08001758 GR_GL_STENCIL_ATTACHMENT,
egdanielff1d5472015-09-10 08:37:20 -07001759 GR_GL_RENDERBUFFER, sbRBID));
bsalomon30447372015-12-21 09:03:05 -08001760 if (sFmt.fPacked) {
1761 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1762 GR_GL_DEPTH_ATTACHMENT,
1763 GR_GL_RENDERBUFFER, sbRBID));
1764 } else {
1765 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1766 GR_GL_DEPTH_ATTACHMENT,
1767 GR_GL_RENDERBUFFER, 0));
1768 }
1769 GrGLenum status;
1770 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1771 if (status == GR_GL_FRAMEBUFFER_COMPLETE) {
1772 firstWorkingStencilFormatIndex = i;
1773 break;
1774 }
egdanielff1d5472015-09-10 08:37:20 -07001775 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1776 GR_GL_STENCIL_ATTACHMENT,
1777 GR_GL_RENDERBUFFER, 0));
1778 if (sFmt.fPacked) {
1779 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1780 GR_GL_DEPTH_ATTACHMENT,
1781 GR_GL_RENDERBUFFER, 0));
1782 }
egdanielff1d5472015-09-10 08:37:20 -07001783 }
1784 }
bsalomon30447372015-12-21 09:03:05 -08001785 GL_CALL(DeleteRenderbuffers(1, &sbRBID));
egdanielff1d5472015-09-10 08:37:20 -07001786 }
1787 GL_CALL(DeleteTextures(1, &colorID));
Adrienne Walker4ee88512018-05-17 11:37:14 -07001788 this->bindFramebuffer(GR_GL_FRAMEBUFFER, 0);
1789 this->deleteFramebuffer(fb);
bsalomon30447372015-12-21 09:03:05 -08001790 fGLContext->caps()->setStencilFormatIndexForConfig(config, firstWorkingStencilFormatIndex);
egdanielff1d5472015-09-10 08:37:20 -07001791 }
bsalomon30447372015-12-21 09:03:05 -08001792 return this->glCaps().getStencilFormatIndexForConfig(config);
egdanielff1d5472015-09-10 08:37:20 -07001793}
1794
Brian Salomon2a4f9832018-03-03 22:43:43 -05001795bool GrGLGpu::createTextureImpl(const GrSurfaceDesc& desc, GrGLTextureInfo* info, bool renderTarget,
Brian Salomondc829942018-10-23 16:07:24 -04001796 GrGLTexture::SamplerParams* initialTexParams,
1797 const GrMipLevel texels[], int mipLevelCount,
1798 GrMipMapsStatus* mipMapsStatus) {
erikchen9a1ed5d2016-02-10 16:32:34 -08001799 info->fID = 0;
1800 info->fTarget = GR_GL_TEXTURE_2D;
1801 GL_CALL(GenTextures(1, &(info->fID)));
1802
1803 if (!info->fID) {
1804 return false;
1805 }
1806
Brian Salomond978b902019-02-07 15:09:18 -05001807 this->bindTextureToScratchUnit(info->fTarget, info->fID);
erikchen9a1ed5d2016-02-10 16:32:34 -08001808
1809 if (renderTarget && this->glCaps().textureUsageSupport()) {
1810 // provides a hint about how this texture will be used
1811 GL_CALL(TexParameteri(info->fTarget,
1812 GR_GL_TEXTURE_USAGE,
1813 GR_GL_FRAMEBUFFER_ATTACHMENT));
1814 }
1815
cblume55f2d2d2016-02-26 13:20:48 -08001816 if (info) {
Brian Salomondc829942018-10-23 16:07:24 -04001817 *initialTexParams = set_initial_texture_params(this->glInterface(), *info);
cblume55f2d2d2016-02-26 13:20:48 -08001818 }
Brian Salomon2a4f9832018-03-03 22:43:43 -05001819
Jim Van Verth1676cb92019-01-15 13:24:45 -05001820 bool success = false;
1821 if (GrPixelConfigIsCompressed(desc.fConfig)) {
1822 SkASSERT(!renderTarget);
1823 success = this->uploadCompressedTexData(desc.fConfig, desc.fWidth, desc.fHeight,
1824 info->fTarget, desc.fConfig,
1825 texels, mipLevelCount, mipMapsStatus);
1826 } else {
1827 success = this->uploadTexData(desc.fConfig, desc.fWidth, desc.fHeight, info->fTarget,
1828 kNewTexture_UploadType, 0, 0, desc.fWidth, desc.fHeight,
1829 desc.fConfig, texels, mipLevelCount, mipMapsStatus);
1830 }
1831 if (!success) {
erikchen9a1ed5d2016-02-10 16:32:34 -08001832 GL_CALL(DeleteTextures(1, &(info->fID)));
1833 return false;
1834 }
Greg Daniele7d8da42017-12-04 11:23:19 -05001835 info->fFormat = this->glCaps().configSizedInternalFormat(desc.fConfig);
erikchen9a1ed5d2016-02-10 16:32:34 -08001836 return true;
1837}
1838
egdanielec00d942015-09-14 12:56:10 -07001839GrStencilAttachment* GrGLGpu::createStencilAttachmentForRenderTarget(const GrRenderTarget* rt,
Brian Salomond1eaf492017-05-18 10:02:08 -04001840 int width, int height) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001841 SkASSERT(width >= rt->width());
1842 SkASSERT(height >= rt->height());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001843
vbuzinovdded6962015-06-12 08:59:45 -07001844 int samples = rt->numStencilSamples();
egdaniel8dc7c3a2015-04-16 11:22:42 -07001845 GrGLStencilAttachment::IDDesc sbDesc;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001846
egdanielff1d5472015-09-10 08:37:20 -07001847 int sIdx = this->getCompatibleStencilIndex(rt->config());
bsalomon62a627b2015-12-17 09:50:47 -08001848 if (sIdx < 0) {
egdanielec00d942015-09-14 12:56:10 -07001849 return nullptr;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001850 }
egdanielff1d5472015-09-10 08:37:20 -07001851
1852 if (!sbDesc.fRenderbufferID) {
1853 GL_CALL(GenRenderbuffers(1, &sbDesc.fRenderbufferID));
1854 }
1855 if (!sbDesc.fRenderbufferID) {
egdanielec00d942015-09-14 12:56:10 -07001856 return nullptr;
egdanielff1d5472015-09-10 08:37:20 -07001857 }
1858 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbDesc.fRenderbufferID));
1859 const GrGLCaps::StencilFormat& sFmt = this->glCaps().stencilFormats()[sIdx];
1860 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1861 // we do this "if" so that we don't call the multisample
1862 // version on a GL that doesn't have an MSAA extension.
Brian Salomonbdecacf2018-02-02 20:32:49 -05001863 if (samples > 1) {
egdanielff1d5472015-09-10 08:37:20 -07001864 SkAssertResult(renderbuffer_storage_msaa(*fGLContext,
1865 samples,
1866 sFmt.fInternalFormat,
1867 width, height));
1868 } else {
1869 GL_ALLOC_CALL(this->glInterface(), RenderbufferStorage(GR_GL_RENDERBUFFER,
1870 sFmt.fInternalFormat,
1871 width, height));
Brian Salomon0ec981b2017-05-15 13:48:50 -04001872 SkASSERT(GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(this->glInterface()));
egdanielff1d5472015-09-10 08:37:20 -07001873 }
1874 fStats.incStencilAttachmentCreates();
1875 // After sized formats we attempt an unsized format and take
1876 // whatever sizes GL gives us. In that case we query for the size.
1877 GrGLStencilAttachment::Format format = sFmt;
1878 get_stencil_rb_sizes(this->glInterface(), &format);
egdanielec00d942015-09-14 12:56:10 -07001879 GrGLStencilAttachment* stencil = new GrGLStencilAttachment(this,
1880 sbDesc,
1881 width,
1882 height,
1883 samples,
1884 format);
1885 return stencil;
reed@google.comac10a2d2010-12-22 21:39:39 +00001886}
1887
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001888////////////////////////////////////////////////////////////////////////////////
1889
Brian Salomondbf70722019-02-07 11:31:24 -05001890sk_sp<GrGpuBuffer> GrGLGpu::onCreateBuffer(size_t size, GrGpuBufferType intendedType,
1891 GrAccessPattern accessPattern, const void* data) {
Brian Salomon12d22642019-01-29 14:38:50 -05001892 return GrGLBuffer::Make(this, size, intendedType, accessPattern, data);
jvanverth73063dc2015-12-03 09:15:47 -08001893}
1894
Brian Salomond818ebf2018-07-02 14:08:49 +00001895void GrGLGpu::flushScissor(const GrScissorState& scissorState,
1896 const GrGLIRect& rtViewport,
1897 GrSurfaceOrigin rtOrigin) {
1898 if (scissorState.enabled()) {
1899 GrGLIRect scissor;
1900 scissor.setRelativeTo(rtViewport, scissorState.rect(), rtOrigin);
1901 // if the scissor fully contains the viewport then we fall through and
1902 // disable the scissor test.
1903 if (!scissor.contains(rtViewport)) {
1904 if (fHWScissorSettings.fRect != scissor) {
1905 scissor.pushToGLScissor(this->glInterface());
1906 fHWScissorSettings.fRect = scissor;
1907 }
1908 if (kYes_TriState != fHWScissorSettings.fEnabled) {
1909 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1910 fHWScissorSettings.fEnabled = kYes_TriState;
bsalomon@google.coma3201942012-06-21 19:58:20 +00001911 }
1912 return;
1913 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001914 }
Brian Salomond818ebf2018-07-02 14:08:49 +00001915
1916 // See fall through note above
1917 this->disableScissor();
joshualitt77b13072014-10-27 14:51:01 -07001918}
1919
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001920void GrGLGpu::flushWindowRectangles(const GrWindowRectsState& windowState,
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001921 const GrGLRenderTarget* rt, GrSurfaceOrigin origin) {
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001922#ifndef USE_NSIGHT
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001923 typedef GrWindowRectsState::Mode Mode;
1924 SkASSERT(!windowState.enabled() || rt->renderFBOID()); // Window rects can't be used on-screen.
1925 SkASSERT(windowState.numWindows() <= this->caps()->maxWindowRectangles());
csmartdalton28341fa2016-08-17 10:00:21 -07001926
Jim Van Verth6a40abc2017-11-02 16:56:09 +00001927 if (!this->caps()->maxWindowRectangles() ||
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001928 fHWWindowRectsState.knownEqualTo(origin, rt->getViewport(), windowState)) {
csmartdalton28341fa2016-08-17 10:00:21 -07001929 return;
csmartdalton28341fa2016-08-17 10:00:21 -07001930 }
1931
csmartdalton7535f412016-08-23 06:51:00 -07001932 // This is purely a workaround for a spurious warning generated by gcc. Otherwise the above
1933 // assert would be sufficient. https://gcc.gnu.org/bugzilla/show_bug.cgi?id=5912
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001934 int numWindows = SkTMin(windowState.numWindows(), int(GrWindowRectangles::kMaxWindows));
1935 SkASSERT(windowState.numWindows() == numWindows);
csmartdalton7535f412016-08-23 06:51:00 -07001936
csmartdalton28341fa2016-08-17 10:00:21 -07001937 GrGLIRect glwindows[GrWindowRectangles::kMaxWindows];
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001938 const SkIRect* skwindows = windowState.windows().data();
csmartdalton7535f412016-08-23 06:51:00 -07001939 for (int i = 0; i < numWindows; ++i) {
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001940 glwindows[i].setRelativeTo(rt->getViewport(), skwindows[i], origin);
csmartdalton28341fa2016-08-17 10:00:21 -07001941 }
1942
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001943 GrGLenum glmode = (Mode::kExclusive == windowState.mode()) ? GR_GL_EXCLUSIVE : GR_GL_INCLUSIVE;
csmartdalton7535f412016-08-23 06:51:00 -07001944 GL_CALL(WindowRectangles(glmode, numWindows, glwindows->asInts()));
csmartdalton28341fa2016-08-17 10:00:21 -07001945
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001946 fHWWindowRectsState.set(origin, rt->getViewport(), windowState);
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001947#endif
csmartdalton28341fa2016-08-17 10:00:21 -07001948}
1949
1950void GrGLGpu::disableWindowRectangles() {
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001951#ifndef USE_NSIGHT
Jim Van Verth6a40abc2017-11-02 16:56:09 +00001952 if (!this->caps()->maxWindowRectangles() || fHWWindowRectsState.knownDisabled()) {
csmartdalton28341fa2016-08-17 10:00:21 -07001953 return;
1954 }
1955 GL_CALL(WindowRectangles(GR_GL_EXCLUSIVE, 0, nullptr));
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001956 fHWWindowRectsState.setDisabled();
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001957#endif
csmartdalton28341fa2016-08-17 10:00:21 -07001958}
1959
Brian Salomonf7232642018-09-19 08:58:08 -04001960void GrGLGpu::resolveAndGenerateMipMapsForProcessorTextures(
1961 const GrPrimitiveProcessor& primProc,
1962 const GrPipeline& pipeline,
1963 const GrTextureProxy* const primProcTextures[],
1964 int numPrimitiveProcessorTextureSets) {
Brian Salomondfec99f2018-08-02 10:40:05 -04001965 auto genLevelsIfNeeded = [this](GrTexture* tex, const GrSamplerState& sampler) {
Brian Salomon7eae3e02018-08-07 14:02:38 +00001966 SkASSERT(tex);
Brian Salomondfec99f2018-08-02 10:40:05 -04001967 if (sampler.filter() == GrSamplerState::Filter::kMipMap &&
1968 tex->texturePriv().mipMapped() == GrMipMapped::kYes &&
1969 tex->texturePriv().mipMapsAreDirty()) {
1970 SkASSERT(this->caps()->mipMapSupport());
1971 this->regenerateMipMapLevels(static_cast<GrGLTexture*>(tex));
Brian Salomonf7232642018-09-19 08:58:08 -04001972 SkASSERT(!tex->asRenderTarget() || !tex->asRenderTarget()->needsResolve());
1973 } else if (auto* rt = tex->asRenderTarget()) {
1974 if (rt->needsResolve()) {
1975 this->resolveRenderTarget(rt);
1976 }
Brian Salomondfec99f2018-08-02 10:40:05 -04001977 }
1978 };
1979
Brian Salomonf7232642018-09-19 08:58:08 -04001980 for (int set = 0, tex = 0; set < numPrimitiveProcessorTextureSets; ++set) {
1981 for (int sampler = 0; sampler < primProc.numTextureSamplers(); ++sampler, ++tex) {
1982 GrTexture* texture = primProcTextures[tex]->peekTexture();
1983 genLevelsIfNeeded(texture, primProc.textureSampler(sampler).samplerState());
1984 }
Brian Salomondfec99f2018-08-02 10:40:05 -04001985 }
1986
1987 GrFragmentProcessor::Iter iter(pipeline);
1988 while (const GrFragmentProcessor* fp = iter.next()) {
1989 for (int i = 0; i < fp->numTextureSamplers(); ++i) {
1990 const auto& textureSampler = fp->textureSampler(i);
1991 genLevelsIfNeeded(textureSampler.peekTexture(), textureSampler.samplerState());
1992 }
1993 }
1994}
1995
Robert Phillipsd0fe8752019-01-31 14:13:59 -05001996bool GrGLGpu::flushGLState(GrRenderTarget* renderTarget,
1997 GrSurfaceOrigin origin,
1998 const GrPrimitiveProcessor& primProc,
Brian Salomon49348902018-06-26 09:12:38 -04001999 const GrPipeline& pipeline,
2000 const GrPipeline::FixedDynamicState* fixedDynamicState,
Brian Salomonf7232642018-09-19 08:58:08 -04002001 const GrPipeline::DynamicStateArrays* dynamicStateArrays,
2002 int dynamicStateArraysLength,
bsalomon2eda5b32016-09-21 10:53:24 -07002003 bool willDrawPoints) {
Brian Salomonf7232642018-09-19 08:58:08 -04002004 const GrTextureProxy* const* primProcProxiesForMipRegen = nullptr;
2005 const GrTextureProxy* const* primProcProxiesToBind = nullptr;
2006 int numPrimProcTextureSets = 1; // number of texture per prim proc sampler.
2007 if (dynamicStateArrays && dynamicStateArrays->fPrimitiveProcessorTextures) {
2008 primProcProxiesForMipRegen = dynamicStateArrays->fPrimitiveProcessorTextures;
2009 numPrimProcTextureSets = dynamicStateArraysLength;
2010 } else if (fixedDynamicState && fixedDynamicState->fPrimitiveProcessorTextures) {
2011 primProcProxiesForMipRegen = fixedDynamicState->fPrimitiveProcessorTextures;
2012 primProcProxiesToBind = fixedDynamicState->fPrimitiveProcessorTextures;
Brian Salomon7eae3e02018-08-07 14:02:38 +00002013 }
Greg Daniel9a51a862018-11-30 10:18:14 -05002014
2015 SkASSERT(SkToBool(primProcProxiesForMipRegen) == SkToBool(primProc.numTextureSamplers()));
2016
Robert Phillipsd0fe8752019-01-31 14:13:59 -05002017 sk_sp<GrGLProgram> program(fProgramCache->refProgram(this, renderTarget, origin, primProc,
2018 primProcProxiesForMipRegen,
Greg Daniel9a51a862018-11-30 10:18:14 -05002019 pipeline, willDrawPoints));
2020 if (!program) {
2021 GrCapsDebugf(this->caps(), "Failed to create program!\n");
2022 return false;
2023 }
Brian Salomonf7232642018-09-19 08:58:08 -04002024 this->resolveAndGenerateMipMapsForProcessorTextures(
2025 primProc, pipeline, primProcProxiesForMipRegen, numPrimProcTextureSets);
brianosman33f6b3f2016-06-02 05:49:21 -07002026
egdaniel080e6732014-12-22 07:35:52 -08002027 GrXferProcessor::BlendInfo blendInfo;
egdaniel0e1853c2016-03-17 11:35:45 -07002028 pipeline.getXferProcessor().getBlendInfo(&blendInfo);
egdaniel080e6732014-12-22 07:35:52 -08002029
egdaniel080e6732014-12-22 07:35:52 -08002030 this->flushColorWrite(blendInfo.fWriteColor);
bsalomonbc3d0de2014-12-15 13:45:03 -08002031
Brian Salomon802cb312018-06-08 18:05:20 -04002032 this->flushProgram(std::move(program));
bsalomon1f78c0a2014-12-17 09:43:13 -08002033
Dongseong Hwang4e1f0222018-11-01 09:10:51 -07002034 // Swizzle the blend to match what the shader will output.
2035 const GrSwizzle& swizzle = this->caps()->shaderCaps()->configOutputSwizzle(
Robert Phillipsd0fe8752019-01-31 14:13:59 -05002036 renderTarget->config());
Dongseong Hwang4e1f0222018-11-01 09:10:51 -07002037 this->flushBlend(blendInfo, swizzle);
bsalomon1f78c0a2014-12-17 09:43:13 -08002038
Robert Phillipsd0fe8752019-01-31 14:13:59 -05002039 fHWProgram->updateUniformsAndTextureBindings(renderTarget, origin,
2040 primProc, pipeline, primProcProxiesToBind);
bsalomon1f78c0a2014-12-17 09:43:13 -08002041
Robert Phillipsd0fe8752019-01-31 14:13:59 -05002042 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(renderTarget);
csmartdaltonc633abb2016-11-01 08:55:55 -07002043 GrStencilSettings stencil;
2044 if (pipeline.isStencilEnabled()) {
2045 // TODO: attach stencil and create settings during render target flush.
2046 SkASSERT(glRT->renderTargetPriv().getStencilAttachment());
2047 stencil.reset(*pipeline.getUserStencil(), pipeline.hasStencilClip(),
2048 glRT->renderTargetPriv().numStencilBits());
2049 }
Chris Dalton71713f62019-04-18 12:41:03 -06002050 this->flushStencil(stencil, origin);
Brian Salomond818ebf2018-07-02 14:08:49 +00002051 if (pipeline.isScissorEnabled()) {
2052 static constexpr SkIRect kBogusScissor{0, 0, 1, 1};
2053 GrScissorState state(fixedDynamicState ? fixedDynamicState->fScissorRect : kBogusScissor);
Robert Phillipsd0fe8752019-01-31 14:13:59 -05002054 this->flushScissor(state, glRT->getViewport(), origin);
Brian Salomond818ebf2018-07-02 14:08:49 +00002055 } else {
2056 this->disableScissor();
Brian Salomon49348902018-06-26 09:12:38 -04002057 }
Robert Phillipsd0fe8752019-01-31 14:13:59 -05002058 this->flushWindowRectangles(pipeline.getWindowRectsState(), glRT, origin);
Chris Dalton4c56b032019-03-04 12:28:42 -07002059 this->flushHWAAState(glRT, pipeline.isHWAntialiasState());
bsalomonbc3d0de2014-12-15 13:45:03 -08002060
2061 // This must come after textures are flushed because a texture may need
egdanield803f272015-03-18 13:01:52 -07002062 // to be msaa-resolved (which will modify bound FBO state).
Chris Daltonc8ece3d2018-07-30 15:03:45 -06002063 this->flushRenderTarget(glRT);
bsalomonbc3d0de2014-12-15 13:45:03 -08002064
2065 return true;
2066}
2067
Brian Salomon802cb312018-06-08 18:05:20 -04002068void GrGLGpu::flushProgram(sk_sp<GrGLProgram> program) {
2069 if (!program) {
2070 fHWProgram.reset();
2071 fHWProgramID = 0;
2072 return;
2073 }
2074 SkASSERT((program == fHWProgram) == (fHWProgramID == program->programID()));
2075 if (program == fHWProgram) {
2076 return;
2077 }
2078 auto id = program->programID();
2079 SkASSERT(id);
2080 GL_CALL(UseProgram(id));
2081 fHWProgram = std::move(program);
2082 fHWProgramID = id;
2083}
2084
2085void GrGLGpu::flushProgram(GrGLuint id) {
2086 SkASSERT(id);
2087 if (fHWProgramID == id) {
2088 SkASSERT(!fHWProgram);
2089 return;
2090 }
2091 fHWProgram.reset();
2092 GL_CALL(UseProgram(id));
2093 fHWProgramID = id;
2094}
2095
2096void GrGLGpu::setupGeometry(const GrBuffer* indexBuffer,
Chris Daltonff926502017-05-03 14:36:54 -04002097 const GrBuffer* vertexBuffer,
Chris Dalton1d616352017-05-31 12:51:23 -06002098 int baseVertex,
2099 const GrBuffer* instanceBuffer,
Brian Salomon802cb312018-06-08 18:05:20 -04002100 int baseInstance,
2101 GrPrimitiveRestart enablePrimitiveRestart) {
2102 SkASSERT((enablePrimitiveRestart == GrPrimitiveRestart::kNo) || indexBuffer);
Chris Dalton27059d32018-01-23 14:06:50 -07002103
cdaltone2e71c22016-04-07 18:13:29 -07002104 GrGLAttribArrayState* attribState;
Chris Daltonff926502017-05-03 14:36:54 -04002105 if (indexBuffer) {
Brian Salomondbf70722019-02-07 11:31:24 -05002106 SkASSERT(indexBuffer->isCpuBuffer() ||
2107 !static_cast<const GrGpuBuffer*>(indexBuffer)->isMapped());
Chris Daltonff926502017-05-03 14:36:54 -04002108 attribState = fHWVertexArrayState.bindInternalVertexArray(this, indexBuffer);
cdaltone2e71c22016-04-07 18:13:29 -07002109 } else {
2110 attribState = fHWVertexArrayState.bindInternalVertexArray(this);
bsalomonbc3d0de2014-12-15 13:45:03 -08002111 }
bsalomonbc3d0de2014-12-15 13:45:03 -08002112
Brian Salomon92be2f72018-06-19 14:33:47 -04002113 int numAttribs = fHWProgram->numVertexAttributes() + fHWProgram->numInstanceAttributes();
2114 attribState->enableVertexArrays(this, numAttribs, enablePrimitiveRestart);
Chris Dalton8e45b4f2017-05-05 14:00:56 -04002115
Brian Salomon802cb312018-06-08 18:05:20 -04002116 if (int vertexStride = fHWProgram->vertexStride()) {
Brian Salomondbf70722019-02-07 11:31:24 -05002117 SkASSERT(vertexBuffer);
2118 SkASSERT(vertexBuffer->isCpuBuffer() ||
2119 !static_cast<const GrGpuBuffer*>(vertexBuffer)->isMapped());
2120 size_t bufferOffset = baseVertex * static_cast<size_t>(vertexStride);
Brian Salomon92be2f72018-06-19 14:33:47 -04002121 for (int i = 0; i < fHWProgram->numVertexAttributes(); ++i) {
2122 const auto& attrib = fHWProgram->vertexAttribute(i);
2123 static constexpr int kDivisor = 0;
Brian Osman4a3f5c82018-09-18 16:16:38 -04002124 attribState->set(this, attrib.fLocation, vertexBuffer, attrib.fCPUType, attrib.fGPUType,
2125 vertexStride, bufferOffset + attrib.fOffset, kDivisor);
Brian Salomon92be2f72018-06-19 14:33:47 -04002126 }
Chris Dalton1d616352017-05-31 12:51:23 -06002127 }
Brian Salomon802cb312018-06-08 18:05:20 -04002128 if (int instanceStride = fHWProgram->instanceStride()) {
Brian Salomondbf70722019-02-07 11:31:24 -05002129 SkASSERT(instanceBuffer);
2130 SkASSERT(instanceBuffer->isCpuBuffer() ||
2131 !static_cast<const GrGpuBuffer*>(instanceBuffer)->isMapped());
2132 size_t bufferOffset = baseInstance * static_cast<size_t>(instanceStride);
Brian Salomon92be2f72018-06-19 14:33:47 -04002133 int attribIdx = fHWProgram->numVertexAttributes();
2134 for (int i = 0; i < fHWProgram->numInstanceAttributes(); ++i, ++attribIdx) {
2135 const auto& attrib = fHWProgram->instanceAttribute(i);
2136 static constexpr int kDivisor = 1;
Brian Osman4a3f5c82018-09-18 16:16:38 -04002137 attribState->set(this, attrib.fLocation, instanceBuffer, attrib.fCPUType,
2138 attrib.fGPUType, instanceStride, bufferOffset + attrib.fOffset,
2139 kDivisor);
Brian Salomon92be2f72018-06-19 14:33:47 -04002140 }
bsalomonbc3d0de2014-12-15 13:45:03 -08002141 }
2142}
2143
Brian Salomonae64c192019-02-05 09:41:37 -05002144GrGLenum GrGLGpu::bindBuffer(GrGpuBufferType type, const GrBuffer* buffer) {
joshualitt93316b92015-10-23 09:08:08 -07002145 this->handleDirtyContext();
cdaltondeacc972016-04-06 14:26:33 -07002146
cdaltone2e71c22016-04-07 18:13:29 -07002147 // Index buffer state is tied to the vertex array.
Brian Salomonae64c192019-02-05 09:41:37 -05002148 if (GrGpuBufferType::kIndex == type) {
cdaltone2e71c22016-04-07 18:13:29 -07002149 this->bindVertexArray(0);
cdaltondeacc972016-04-06 14:26:33 -07002150 }
cdaltone2e71c22016-04-07 18:13:29 -07002151
Brian Salomonae64c192019-02-05 09:41:37 -05002152 auto* bufferState = this->hwBufferState(type);
Brian Salomondbf70722019-02-07 11:31:24 -05002153 if (buffer->isCpuBuffer()) {
Brian Salomonae64c192019-02-05 09:41:37 -05002154 if (!bufferState->fBufferZeroKnownBound) {
2155 GL_CALL(BindBuffer(bufferState->fGLTarget, 0));
2156 bufferState->fBufferZeroKnownBound = true;
2157 bufferState->fBoundBufferUniqueID.makeInvalid();
cdaltone2e71c22016-04-07 18:13:29 -07002158 }
Brian Salomondbf70722019-02-07 11:31:24 -05002159 } else if (static_cast<const GrGpuBuffer*>(buffer)->uniqueID() !=
2160 bufferState->fBoundBufferUniqueID) {
Brian Salomonae64c192019-02-05 09:41:37 -05002161 const GrGLBuffer* glBuffer = static_cast<const GrGLBuffer*>(buffer);
2162 GL_CALL(BindBuffer(bufferState->fGLTarget, glBuffer->bufferID()));
2163 bufferState->fBufferZeroKnownBound = false;
2164 bufferState->fBoundBufferUniqueID = glBuffer->uniqueID();
cdaltone2e71c22016-04-07 18:13:29 -07002165 }
2166
Brian Salomonae64c192019-02-05 09:41:37 -05002167 return bufferState->fGLTarget;
joshualitt93316b92015-10-23 09:08:08 -07002168}
Brian Salomond818ebf2018-07-02 14:08:49 +00002169void GrGLGpu::disableScissor() {
2170 if (kNo_TriState != fHWScissorSettings.fEnabled) {
2171 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
2172 fHWScissorSettings.fEnabled = kNo_TriState;
2173 return;
2174 }
2175}
2176
Brian Osman9a9baae2018-11-05 15:06:26 -05002177void GrGLGpu::clear(const GrFixedClip& clip, const SkPMColor4f& color,
Robert Phillips19e51dc2017-08-09 09:30:51 -04002178 GrRenderTarget* target, GrSurfaceOrigin origin) {
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00002179 // parent class should never let us get here with no RT
bsalomon49f085d2014-09-05 13:34:00 -07002180 SkASSERT(target);
Michael Ludwigc39d0c82019-01-15 10:03:43 -05002181 SkASSERT(!this->caps()->performColorClearsAsDraws());
2182 SkASSERT(!clip.scissorEnabled() || !this->caps()->performPartialClearsAsDraws());
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00002183
Brian Salomon43f8bf02017-10-18 08:33:29 -04002184 this->handleDirtyContext();
bsalomon@google.com74b98712011-11-11 19:46:16 +00002185
Brian Salomon43f8bf02017-10-18 08:33:29 -04002186 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
2187
Brian Salomond818ebf2018-07-02 14:08:49 +00002188 if (clip.scissorEnabled()) {
Chris Daltonc8ece3d2018-07-30 15:03:45 -06002189 this->flushRenderTarget(glRT, origin, clip.scissorRect());
Brian Salomon1fabd512018-02-09 09:54:25 -05002190 } else {
Chris Daltonc8ece3d2018-07-30 15:03:45 -06002191 this->flushRenderTarget(glRT);
Brian Salomon1fabd512018-02-09 09:54:25 -05002192 }
Brian Salomond818ebf2018-07-02 14:08:49 +00002193 this->flushScissor(clip.scissorState(), glRT->getViewport(), origin);
Brian Salomon43f8bf02017-10-18 08:33:29 -04002194 this->flushWindowRectangles(clip.windowRectsState(), glRT, origin);
Brian Salomon805cc7a2019-01-28 09:52:34 -05002195 this->flushColorWrite(true);
Brian Salomon028a9a52017-05-11 11:39:08 -04002196
Brian Osman9a9baae2018-11-05 15:06:26 -05002197 GrGLfloat r = color.fR, g = color.fG, b = color.fB, a = color.fA;
Eric Karlaeaf22b2017-05-18 15:08:09 -07002198 if (this->glCaps().clearToBoundaryValuesIsBroken() &&
2199 (1 == r || 0 == r) && (1 == g || 0 == g) && (1 == b || 0 == b) && (1 == a || 0 == a)) {
Eric Karlaeaf22b2017-05-18 15:08:09 -07002200 static const GrGLfloat safeAlpha1 = nextafter(1.f, 2.f);
2201 static const GrGLfloat safeAlpha0 = nextafter(0.f, -1.f);
Eric Karlaeaf22b2017-05-18 15:08:09 -07002202 a = (1 == a) ? safeAlpha1 : safeAlpha0;
Brian Salomon028a9a52017-05-11 11:39:08 -04002203 }
Brian Salomon805cc7a2019-01-28 09:52:34 -05002204 this->flushClearColor(r, g, b, a);
2205
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002206 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00002207}
2208
Robert Phillips95214472017-08-08 18:00:03 -04002209void GrGLGpu::clearStencil(GrRenderTarget* target, int clearValue) {
Michael Ludwigc39d0c82019-01-15 10:03:43 -05002210 SkASSERT(!this->caps()->performStencilClearsAsDraws());
2211
Robert Phillips95214472017-08-08 18:00:03 -04002212 if (!target) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00002213 return;
2214 }
Robert Phillipscb2e2352017-08-30 16:44:40 -04002215
2216 GrStencilAttachment* sb = target->renderTargetPriv().getStencilAttachment();
2217 // this should only be called internally when we know we have a
2218 // stencil buffer.
2219 SkASSERT(sb);
2220
bsalomonb0bd4f62014-09-03 07:19:50 -07002221 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
Brian Salomon1fabd512018-02-09 09:54:25 -05002222 this->flushRenderTargetNoColorWrites(glRT);
bsalomon@google.com8295dc12011-05-02 12:53:34 +00002223
Brian Salomond818ebf2018-07-02 14:08:49 +00002224 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07002225 this->disableWindowRectangles();
robertphillips@google.com730ebe52012-04-16 16:33:13 +00002226
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002227 GL_CALL(StencilMask(0xffffffff));
Robert Phillips95214472017-08-08 18:00:03 -04002228 GL_CALL(ClearStencil(clearValue));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002229 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00002230 fHWStencilSettings.invalidate();
Robert Phillipscb2e2352017-08-30 16:44:40 -04002231 if (!clearValue) {
2232 sb->cleared();
2233 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002234}
2235
csmartdalton29df7602016-08-31 11:55:52 -07002236void GrGLGpu::clearStencilClip(const GrFixedClip& clip,
2237 bool insideStencilMask,
Robert Phillips19e51dc2017-08-09 09:30:51 -04002238 GrRenderTarget* target, GrSurfaceOrigin origin) {
bsalomon49f085d2014-09-05 13:34:00 -07002239 SkASSERT(target);
Michael Ludwigc39d0c82019-01-15 10:03:43 -05002240 SkASSERT(!this->caps()->performStencilClearsAsDraws());
egdaniel9cb63402016-06-23 08:37:05 -07002241 this->handleDirtyContext();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00002242
egdaniel8dc7c3a2015-04-16 11:22:42 -07002243 GrStencilAttachment* sb = target->renderTargetPriv().getStencilAttachment();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00002244 // this should only be called internally when we know we have a
2245 // stencil buffer.
bsalomon6bc1b5f2015-02-23 09:06:38 -08002246 SkASSERT(sb);
2247 GrGLint stencilBitCount = sb->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002248#if 0
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002249 SkASSERT(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00002250 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002251#else
2252 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00002253 // ANGLE a partial stencil mask will cause clears to be
Robert Phillipsf2361d22016-10-25 14:20:06 -04002254 // turned into draws. Our contract on GrOpList says that
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002255 // changing the clip between stencil passes may or may not
2256 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00002257 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002258#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002259 GrGLint value;
csmartdalton29df7602016-08-31 11:55:52 -07002260 if (insideStencilMask) {
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002261 value = (1 << (stencilBitCount - 1));
2262 } else {
2263 value = 0;
2264 }
bsalomonb0bd4f62014-09-03 07:19:50 -07002265 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
Brian Salomon1fabd512018-02-09 09:54:25 -05002266 this->flushRenderTargetNoColorWrites(glRT);
bsalomon@google.coma3201942012-06-21 19:58:20 +00002267
Brian Salomond818ebf2018-07-02 14:08:49 +00002268 this->flushScissor(clip.scissorState(), glRT->getViewport(), origin);
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002269 this->flushWindowRectangles(clip.windowRectsState(), glRT, origin);
bsalomon@google.coma3201942012-06-21 19:58:20 +00002270
caryclark@google.comcf6285b2012-06-06 12:09:01 +00002271 GL_CALL(StencilMask((uint32_t) clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002272 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002273 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00002274 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00002275}
2276
bsalomon1aa20292016-01-22 08:16:09 -08002277bool GrGLGpu::readPixelsSupported(GrRenderTarget* target, GrPixelConfig readConfig) {
Brian Salomon625cd9e2016-12-15 09:35:19 -05002278#ifdef SK_BUILD_FOR_MAC
2279 // Chromium may ask us to read back from locked IOSurfaces. Calling the command buffer's
2280 // glGetIntegerv() with GL_IMPLEMENTATION_COLOR_READ_FORMAT/_TYPE causes the command buffer
2281 // to make a call to check the framebuffer status which can hang the driver. So in Mac Chromium
2282 // we always use a temporary surface to test for read pixels support.
2283 // https://www.crbug.com/662802
2284 if (this->glContext().driver() == kChromium_GrGLDriver) {
2285 return this->readPixelsSupported(target->config(), readConfig);
2286 }
2287#endif
bsalomon1aa20292016-01-22 08:16:09 -08002288 auto bindRenderTarget = [this, target]() -> bool {
Brian Salomon1fabd512018-02-09 09:54:25 -05002289 this->flushRenderTargetNoColorWrites(static_cast<GrGLRenderTarget*>(target));
bsalomon1aa20292016-01-22 08:16:09 -08002290 return true;
2291 };
bsalomon2c3db322016-11-08 13:26:24 -08002292 auto unbindRenderTarget = []{};
bsalomon1aa20292016-01-22 08:16:09 -08002293 auto getIntegerv = [this](GrGLenum query, GrGLint* value) {
2294 GR_GL_GetIntegerv(this->glInterface(), query, value);
2295 };
2296 GrPixelConfig rtConfig = target->config();
bsalomon2c3db322016-11-08 13:26:24 -08002297 return this->glCaps().readPixelsSupported(rtConfig, readConfig, getIntegerv, bindRenderTarget,
2298 unbindRenderTarget);
bsalomon1aa20292016-01-22 08:16:09 -08002299}
2300
2301bool GrGLGpu::readPixelsSupported(GrPixelConfig rtConfig, GrPixelConfig readConfig) {
bsalomon2c3db322016-11-08 13:26:24 -08002302 sk_sp<GrTexture> temp;
2303 auto bindRenderTarget = [this, rtConfig, &temp]() -> bool {
Robert Phillips16d8ec62017-07-27 16:16:25 -04002304 GrSurfaceDesc desc;
bsalomon1aa20292016-01-22 08:16:09 -08002305 desc.fConfig = rtConfig;
2306 desc.fWidth = desc.fHeight = 16;
Brian Salomonbdecacf2018-02-02 20:32:49 -05002307 if (this->glCaps().isConfigRenderable(rtConfig)) {
bsalomon2c3db322016-11-08 13:26:24 -08002308 desc.fFlags = kRenderTarget_GrSurfaceFlag;
Robert Phillips67d52cf2017-06-05 13:38:13 -04002309 temp = this->createTexture(desc, SkBudgeted::kNo);
bsalomon2c3db322016-11-08 13:26:24 -08002310 if (!temp) {
2311 return false;
2312 }
2313 GrGLRenderTarget* glrt = static_cast<GrGLRenderTarget*>(temp->asRenderTarget());
Brian Salomon1fabd512018-02-09 09:54:25 -05002314 this->flushRenderTargetNoColorWrites(glrt);
bsalomon2c3db322016-11-08 13:26:24 -08002315 return true;
2316 } else if (this->glCaps().canConfigBeFBOColorAttachment(rtConfig)) {
Robert Phillips67d52cf2017-06-05 13:38:13 -04002317 temp = this->createTexture(desc, SkBudgeted::kNo);
bsalomon2c3db322016-11-08 13:26:24 -08002318 if (!temp) {
2319 return false;
2320 }
2321 GrGLIRect vp;
2322 this->bindSurfaceFBOForPixelOps(temp.get(), GR_GL_FRAMEBUFFER, &vp, kDst_TempFBOTarget);
Robert Phillips294870f2016-11-11 12:38:40 -05002323 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon2c3db322016-11-08 13:26:24 -08002324 return true;
bsalomon1aa20292016-01-22 08:16:09 -08002325 }
bsalomon2c3db322016-11-08 13:26:24 -08002326 return false;
2327 };
2328 auto unbindRenderTarget = [this, &temp]() {
2329 this->unbindTextureFBOForPixelOps(GR_GL_FRAMEBUFFER, temp.get());
bsalomon1aa20292016-01-22 08:16:09 -08002330 };
2331 auto getIntegerv = [this](GrGLenum query, GrGLint* value) {
2332 GR_GL_GetIntegerv(this->glInterface(), query, value);
2333 };
bsalomon2c3db322016-11-08 13:26:24 -08002334 return this->glCaps().readPixelsSupported(rtConfig, readConfig, getIntegerv, bindRenderTarget,
2335 unbindRenderTarget);
bsalomon1aa20292016-01-22 08:16:09 -08002336}
2337
2338bool GrGLGpu::readPixelsSupported(GrSurface* surfaceForConfig, GrPixelConfig readConfig) {
2339 if (GrRenderTarget* rt = surfaceForConfig->asRenderTarget()) {
2340 return this->readPixelsSupported(rt, readConfig);
2341 } else {
2342 GrPixelConfig config = surfaceForConfig->config();
2343 return this->readPixelsSupported(config, readConfig);
2344 }
2345}
2346
Brian Salomone05ba5a2019-04-08 11:59:07 -04002347bool GrGLGpu::readOrTransferPixelsFrom(GrSurface* surface, int left, int top, int width, int height,
2348 GrColorType dstColorType, void* offsetOrPtr,
Brian Salomon26de56e2019-04-10 12:14:26 -04002349 int rowWidthInPixels) {
bsalomon6cb3cbe2015-07-30 07:34:27 -07002350 SkASSERT(surface);
bsalomon39826022015-07-23 08:07:21 -07002351
bsalomone9573312016-01-25 14:33:25 -08002352 GrGLRenderTarget* renderTarget = static_cast<GrGLRenderTarget*>(surface->asRenderTarget());
Brian Salomon71d9d842016-11-03 13:42:00 -04002353 if (!renderTarget && !this->glCaps().canConfigBeFBOColorAttachment(surface->config())) {
bsalomon6cb3cbe2015-07-30 07:34:27 -07002354 return false;
2355 }
2356
Brian Salomonc320b152018-02-20 14:05:36 -05002357 // TODO: Avoid this conversion by making GrGLCaps work with color types.
2358 auto dstAsConfig = GrColorTypeToPixelConfig(dstColorType, GrSRGBEncoded::kNo);
2359
Brian Salomonc320b152018-02-20 14:05:36 -05002360 if (!this->readPixelsSupported(surface, dstAsConfig)) {
bsalomon16921ec2015-07-30 15:34:56 -07002361 return false;
2362 }
2363
bsalomon76148af2016-01-12 11:13:47 -08002364 GrGLenum externalFormat;
2365 GrGLenum externalType;
Brian Salomonc320b152018-02-20 14:05:36 -05002366 if (!this->glCaps().getReadPixelsFormat(surface->config(), dstAsConfig, &externalFormat,
bsalomon76148af2016-01-12 11:13:47 -08002367 &externalType)) {
2368 return false;
2369 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00002370
Brian Salomon71d9d842016-11-03 13:42:00 -04002371 GrGLIRect glvp;
2372 if (renderTarget) {
2373 // resolve the render target if necessary
2374 switch (renderTarget->getResolveType()) {
2375 case GrGLRenderTarget::kCantResolve_ResolveType:
2376 return false;
2377 case GrGLRenderTarget::kAutoResolves_ResolveType:
Brian Salomon1fabd512018-02-09 09:54:25 -05002378 this->flushRenderTargetNoColorWrites(renderTarget);
Brian Salomon71d9d842016-11-03 13:42:00 -04002379 break;
2380 case GrGLRenderTarget::kCanResolve_ResolveType:
Brian Salomon1fabd512018-02-09 09:54:25 -05002381 this->onResolveRenderTarget(renderTarget);
Brian Salomon71d9d842016-11-03 13:42:00 -04002382 // we don't track the state of the READ FBO ID.
Adrienne Walker4ee88512018-05-17 11:37:14 -07002383 this->bindFramebuffer(GR_GL_READ_FRAMEBUFFER, renderTarget->textureFBOID());
Brian Salomon71d9d842016-11-03 13:42:00 -04002384 break;
2385 default:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -04002386 SK_ABORT("Unknown resolve type");
Brian Salomon71d9d842016-11-03 13:42:00 -04002387 }
2388 glvp = renderTarget->getViewport();
2389 } else {
2390 // Use a temporary FBO.
2391 this->bindSurfaceFBOForPixelOps(surface, GR_GL_FRAMEBUFFER, &glvp, kSrc_TempFBOTarget);
Robert Phillips294870f2016-11-11 12:38:40 -05002392 fHWBoundRenderTargetUniqueID.makeInvalid();
reed@google.comac10a2d2010-12-22 21:39:39 +00002393 }
2394
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00002395 // the read rect is viewport-relative
2396 GrGLIRect readRect;
Brian Salomona6948702018-06-01 15:33:20 -04002397 readRect.setRelativeTo(glvp, left, top, width, height, kTopLeft_GrSurfaceOrigin);
rmistry@google.comfbfcd562012-08-23 18:09:54 +00002398
Brian Salomona6948702018-06-01 15:33:20 -04002399 // determine if GL can read using the passed rowBytes or if we need a scratch buffer.
Brian Salomon26de56e2019-04-10 12:14:26 -04002400 if (rowWidthInPixels != width) {
Brian Salomone05ba5a2019-04-08 11:59:07 -04002401 SkASSERT(this->glCaps().packRowLengthSupport());
Brian Salomon26de56e2019-04-10 12:14:26 -04002402 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, rowWidthInPixels));
bsalomon@google.comc6980972011-11-02 19:57:21 +00002403 }
Brian Salomonc320b152018-02-20 14:05:36 -05002404 GL_CALL(PixelStorei(GR_GL_PACK_ALIGNMENT, config_alignment(dstAsConfig)));
bsalomonf46a1242015-12-15 12:37:38 -08002405
Brian Osman1348ed02018-09-12 09:08:39 -04002406 bool reattachStencil = false;
2407 if (this->glCaps().detachStencilFromMSAABuffersBeforeReadPixels() &&
2408 renderTarget &&
2409 renderTarget->renderTargetPriv().getStencilAttachment() &&
2410 renderTarget->numColorSamples() > 1) {
2411 // Fix Adreno devices that won't read from MSAA framebuffers with stencil attached
2412 reattachStencil = true;
2413 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT,
2414 GR_GL_RENDERBUFFER, 0));
2415 }
2416
Brian Salomone05ba5a2019-04-08 11:59:07 -04002417 GL_CALL(ReadPixels(readRect.fLeft, readRect.fBottom, readRect.fWidth, readRect.fHeight,
2418 externalFormat, externalType, offsetOrPtr));
Brian Osman1348ed02018-09-12 09:08:39 -04002419
2420 if (reattachStencil) {
2421 GrGLStencilAttachment* stencilAttachment = static_cast<GrGLStencilAttachment*>(
2422 renderTarget->renderTargetPriv().getStencilAttachment());
2423 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT,
2424 GR_GL_RENDERBUFFER, stencilAttachment->renderbufferID()));
2425 }
2426
Brian Salomon26de56e2019-04-10 12:14:26 -04002427 if (rowWidthInPixels != width) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002428 SkASSERT(this->glCaps().packRowLengthSupport());
bsalomon@google.comc6980972011-11-02 19:57:21 +00002429 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
2430 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002431
Brian Salomone05ba5a2019-04-08 11:59:07 -04002432 if (!renderTarget) {
2433 this->unbindTextureFBOForPixelOps(GR_GL_FRAMEBUFFER, surface);
2434 }
2435 return true;
2436}
2437
2438bool GrGLGpu::onReadPixels(GrSurface* surface, int left, int top, int width, int height,
2439 GrColorType dstColorType, void* buffer, size_t rowBytes) {
2440 SkASSERT(surface);
2441
Brian Salomon26de56e2019-04-10 12:14:26 -04002442 int bytesPerPixel = GrColorTypeBytesPerPixel(dstColorType);
Brian Salomone05ba5a2019-04-08 11:59:07 -04002443
Brian Salomon26de56e2019-04-10 12:14:26 -04002444 // GL_PACK_ROW_LENGTH is in terms of pixels not bytes.
2445 int rowPixelWidth;
Brian Salomone05ba5a2019-04-08 11:59:07 -04002446 void* readDst = buffer;
2447
2448 // determine if GL can read using the passed rowBytes or if we need a scratch buffer.
2449 SkAutoSMalloc<32 * sizeof(GrColor)> scratch;
Brian Salomon26de56e2019-04-10 12:14:26 -04002450 if (!rowBytes || rowBytes == (size_t)(width * bytesPerPixel)) {
2451 rowPixelWidth = width;
2452 } else {
Brian Salomone05ba5a2019-04-08 11:59:07 -04002453 if (this->glCaps().packRowLengthSupport() && !(rowBytes % bytesPerPixel)) {
Brian Salomon26de56e2019-04-10 12:14:26 -04002454 rowPixelWidth = rowBytes / bytesPerPixel;
Brian Salomone05ba5a2019-04-08 11:59:07 -04002455 } else {
Brian Salomon26de56e2019-04-10 12:14:26 -04002456 scratch.reset(width * bytesPerPixel * height);
Brian Salomone05ba5a2019-04-08 11:59:07 -04002457 readDst = scratch.get();
Brian Salomon26de56e2019-04-10 12:14:26 -04002458 rowPixelWidth = width;
Brian Salomone05ba5a2019-04-08 11:59:07 -04002459 }
2460 }
2461 if (!this->readOrTransferPixelsFrom(surface, left, top, width, height, dstColorType, readDst,
Brian Salomon26de56e2019-04-10 12:14:26 -04002462 rowPixelWidth)) {
Brian Salomone05ba5a2019-04-08 11:59:07 -04002463 return false;
2464 }
2465
Brian Salomona6948702018-06-01 15:33:20 -04002466 if (readDst != buffer) {
bsalomon9d02b262016-02-01 12:49:30 -08002467 SkASSERT(readDst != buffer);
Brian Salomon26de56e2019-04-10 12:14:26 -04002468 SkASSERT(rowBytes != (size_t)(rowPixelWidth * bytesPerPixel));
bsalomon@google.comc6980972011-11-02 19:57:21 +00002469 const char* src = reinterpret_cast<const char*>(readDst);
bsalomon@google.comc4364992011-11-07 15:54:49 +00002470 char* dst = reinterpret_cast<char*>(buffer);
Brian Salomon26de56e2019-04-10 12:14:26 -04002471 SkRectMemcpy(dst, rowBytes, src, rowPixelWidth * bytesPerPixel, width * bytesPerPixel,
2472 height);
reed@google.comac10a2d2010-12-22 21:39:39 +00002473 }
2474 return true;
2475}
2476
Robert Phillips5b5d84c2018-08-09 15:12:18 -04002477GrGpuRTCommandBuffer* GrGLGpu::getCommandBuffer(
Ethan Nicholas56d19a52018-10-15 11:26:20 -04002478 GrRenderTarget* rt, GrSurfaceOrigin origin, const SkRect& bounds,
Robert Phillips6b47c7d2017-08-29 07:24:09 -04002479 const GrGpuRTCommandBuffer::LoadAndStoreInfo& colorInfo,
Greg Daniel500d58b2017-08-24 15:59:33 -04002480 const GrGpuRTCommandBuffer::StencilLoadAndStoreInfo& stencilInfo) {
Robert Phillips5b5d84c2018-08-09 15:12:18 -04002481 if (!fCachedRTCommandBuffer) {
2482 fCachedRTCommandBuffer.reset(new GrGLGpuRTCommandBuffer(this));
2483 }
2484
2485 fCachedRTCommandBuffer->set(rt, origin, colorInfo, stencilInfo);
2486 return fCachedRTCommandBuffer.get();
Greg Daniel500d58b2017-08-24 15:59:33 -04002487}
2488
Robert Phillips5b5d84c2018-08-09 15:12:18 -04002489GrGpuTextureCommandBuffer* GrGLGpu::getCommandBuffer(GrTexture* texture, GrSurfaceOrigin origin) {
2490 if (!fCachedTexCommandBuffer) {
2491 fCachedTexCommandBuffer.reset(new GrGLGpuTextureCommandBuffer(this));
2492 }
2493
2494 fCachedTexCommandBuffer->set(texture, origin);
2495 return fCachedTexCommandBuffer.get();
egdaniel066df7c2016-06-08 14:02:27 -07002496}
2497
Brian Salomon1fabd512018-02-09 09:54:25 -05002498void GrGLGpu::flushRenderTarget(GrGLRenderTarget* target, GrSurfaceOrigin origin,
Chris Daltonc8ece3d2018-07-30 15:03:45 -06002499 const SkIRect& bounds) {
Brian Osman9aa30c62018-07-02 15:21:46 -04002500 this->flushRenderTargetNoColorWrites(target);
Chris Daltonc8ece3d2018-07-30 15:03:45 -06002501 this->didWriteToSurface(target, origin, &bounds);
2502}
bsalomon6ba6fa12015-03-04 11:57:37 -08002503
Chris Daltonc8ece3d2018-07-30 15:03:45 -06002504void GrGLGpu::flushRenderTarget(GrGLRenderTarget* target) {
2505 this->flushRenderTargetNoColorWrites(target);
2506 this->didWriteToSurface(target, kTopLeft_GrSurfaceOrigin, nullptr);
Brian Salomon1fabd512018-02-09 09:54:25 -05002507}
2508
Brian Osman9aa30c62018-07-02 15:21:46 -04002509void GrGLGpu::flushRenderTargetNoColorWrites(GrGLRenderTarget* target) {
Brian Salomon1fabd512018-02-09 09:54:25 -05002510 SkASSERT(target);
Robert Phillips294870f2016-11-11 12:38:40 -05002511 GrGpuResource::UniqueID rtID = target->uniqueID();
egdanield803f272015-03-18 13:01:52 -07002512 if (fHWBoundRenderTargetUniqueID != rtID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -07002513 this->bindFramebuffer(GR_GL_FRAMEBUFFER, target->renderFBOID());
egdanield803f272015-03-18 13:01:52 -07002514#ifdef SK_DEBUG
2515 // don't do this check in Chromium -- this is causing
2516 // lots of repeated command buffer flushes when the compositor is
2517 // rendering with Ganesh, which is really slow; even too slow for
2518 // Debug mode.
cdalton1acea862015-06-02 13:05:52 -07002519 if (kChromium_GrGLDriver != this->glContext().driver()) {
egdanield803f272015-03-18 13:01:52 -07002520 GrGLenum status;
2521 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
2522 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
2523 SkDebugf("GrGLGpu::flushRenderTarget glCheckFramebufferStatus %x\n", status);
2524 }
bsalomon160f24c2015-03-17 15:55:42 -07002525 }
egdanield803f272015-03-18 13:01:52 -07002526#endif
2527 fHWBoundRenderTargetUniqueID = rtID;
bsalomon083617b2016-02-12 12:10:14 -08002528 this->flushViewport(target->getViewport());
brianosman64d094d2016-03-25 06:01:59 -07002529 }
2530
brianosman35b784d2016-05-05 11:52:53 -07002531 if (this->glCaps().srgbWriteControl()) {
Brian Osman9aa30c62018-07-02 15:21:46 -04002532 this->flushFramebufferSRGB(GrPixelConfigIsSRGB(target->config()));
bsalomon5cd020f2015-03-17 12:46:56 -07002533 }
bsalomon083617b2016-02-12 12:10:14 -08002534}
bsalomona9909122016-01-23 10:41:40 -08002535
brianosman33f6b3f2016-06-02 05:49:21 -07002536void GrGLGpu::flushFramebufferSRGB(bool enable) {
2537 if (enable && kYes_TriState != fHWSRGBFramebuffer) {
2538 GL_CALL(Enable(GR_GL_FRAMEBUFFER_SRGB));
2539 fHWSRGBFramebuffer = kYes_TriState;
2540 } else if (!enable && kNo_TriState != fHWSRGBFramebuffer) {
2541 GL_CALL(Disable(GR_GL_FRAMEBUFFER_SRGB));
2542 fHWSRGBFramebuffer = kNo_TriState;
2543 }
2544}
2545
bsalomon083617b2016-02-12 12:10:14 -08002546void GrGLGpu::flushViewport(const GrGLIRect& viewport) {
2547 if (fHWViewport != viewport) {
2548 viewport.pushToGLViewport(this->glInterface());
2549 fHWViewport = viewport;
2550 }
2551}
2552
bsalomon@google.comd302f142011-03-03 13:54:13 +00002553#define SWAP_PER_DRAW 0
2554
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00002555#if SWAP_PER_DRAW
commit-bot@chromium.org43823302013-09-25 20:57:51 +00002556 #if defined(SK_BUILD_FOR_MAC)
bsalomon@google.comd302f142011-03-03 13:54:13 +00002557 #include <AGL/agl.h>
Mike Klein8f11d4d2018-01-24 12:42:55 -05002558 #elif defined(SK_BUILD_FOR_WIN)
bsalomon@google.comce7357d2012-06-25 17:49:25 +00002559 #include <gl/GL.h>
bsalomon@google.comd302f142011-03-03 13:54:13 +00002560 void SwapBuf() {
2561 DWORD procID = GetCurrentProcessId();
2562 HWND hwnd = GetTopWindow(GetDesktopWindow());
2563 while(hwnd) {
2564 DWORD wndProcID = 0;
2565 GetWindowThreadProcessId(hwnd, &wndProcID);
2566 if(wndProcID == procID) {
2567 SwapBuffers(GetDC(hwnd));
2568 }
2569 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
2570 }
2571 }
2572 #endif
2573#endif
2574
Robert Phillipsd0fe8752019-01-31 14:13:59 -05002575void GrGLGpu::draw(GrRenderTarget* renderTarget, GrSurfaceOrigin origin,
2576 const GrPrimitiveProcessor& primProc,
Brian Salomonff168d92018-06-23 15:17:27 -04002577 const GrPipeline& pipeline,
Brian Salomon49348902018-06-26 09:12:38 -04002578 const GrPipeline::FixedDynamicState* fixedDynamicState,
2579 const GrPipeline::DynamicStateArrays* dynamicStateArrays,
bsalomon2eda5b32016-09-21 10:53:24 -07002580 const GrMesh meshes[],
egdaniel9cb63402016-06-23 08:37:05 -07002581 int meshCount) {
2582 this->handleDirtyContext();
2583
bsalomon2eda5b32016-09-21 10:53:24 -07002584 bool hasPoints = false;
2585 for (int i = 0; i < meshCount; ++i) {
Chris Dalton3809bab2017-06-13 10:55:06 -06002586 if (meshes[i].primitiveType() == GrPrimitiveType::kPoints) {
bsalomon2eda5b32016-09-21 10:53:24 -07002587 hasPoints = true;
2588 break;
2589 }
2590 }
Robert Phillipsd0fe8752019-01-31 14:13:59 -05002591 if (!this->flushGLState(renderTarget, origin, primProc, pipeline, fixedDynamicState,
2592 dynamicStateArrays, meshCount, hasPoints)) {
bsalomond95263c2014-12-16 13:05:12 -08002593 return;
2594 }
ethannicholas22793252016-01-30 09:59:10 -08002595
Brian Salomonf7232642018-09-19 08:58:08 -04002596 bool dynamicScissor = false;
2597 bool dynamicPrimProcTextures = false;
2598 if (dynamicStateArrays) {
2599 dynamicScissor = pipeline.isScissorEnabled() && dynamicStateArrays->fScissorRects;
2600 dynamicPrimProcTextures = dynamicStateArrays->fPrimitiveProcessorTextures;
2601 }
2602 for (int m = 0; m < meshCount; ++m) {
Robert Phillipsd0fe8752019-01-31 14:13:59 -05002603 if (GrXferBarrierType barrierType = pipeline.xferBarrierType(renderTarget->asTexture(),
2604 *this->caps())) {
2605 this->xferBarrier(renderTarget, barrierType);
egdaniel0e1853c2016-03-17 11:35:45 -07002606 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002607
Brian Salomon49348902018-06-26 09:12:38 -04002608 if (dynamicScissor) {
Robert Phillipsd0fe8752019-01-31 14:13:59 -05002609 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(renderTarget);
Brian Salomonf7232642018-09-19 08:58:08 -04002610 this->flushScissor(GrScissorState(dynamicStateArrays->fScissorRects[m]),
Robert Phillipsd0fe8752019-01-31 14:13:59 -05002611 glRT->getViewport(), origin);
Chris Dalton46983b72017-06-06 12:27:16 -06002612 }
Brian Salomonf7232642018-09-19 08:58:08 -04002613 if (dynamicPrimProcTextures) {
2614 auto texProxyArray = dynamicStateArrays->fPrimitiveProcessorTextures +
2615 m * primProc.numTextureSamplers();
2616 fHWProgram->updatePrimitiveProcessorTextureBindings(primProc, texProxyArray);
2617 }
Brian Salomon6d9c88b2017-06-12 10:24:42 -04002618 if (this->glCaps().requiresCullFaceEnableDisableWhenDrawingLinesAfterNonLines() &&
Brian Salomonf7232642018-09-19 08:58:08 -04002619 GrIsPrimTypeLines(meshes[m].primitiveType()) &&
Brian Salomon6d9c88b2017-06-12 10:24:42 -04002620 !GrIsPrimTypeLines(fLastPrimitiveType)) {
2621 GL_CALL(Enable(GR_GL_CULL_FACE));
2622 GL_CALL(Disable(GR_GL_CULL_FACE));
2623 }
Brian Salomonf7232642018-09-19 08:58:08 -04002624 meshes[m].sendToGpu(this);
2625 fLastPrimitiveType = meshes[m].primitiveType();
bsalomon@google.com74749cd2013-01-30 16:12:41 +00002626 }
ethannicholas22793252016-01-30 09:59:10 -08002627
bsalomon@google.comd302f142011-03-03 13:54:13 +00002628#if SWAP_PER_DRAW
2629 glFlush();
commit-bot@chromium.org43823302013-09-25 20:57:51 +00002630 #if defined(SK_BUILD_FOR_MAC)
bsalomon@google.comd302f142011-03-03 13:54:13 +00002631 aglSwapBuffers(aglGetCurrentContext());
2632 int set_a_break_pt_here = 9;
2633 aglSwapBuffers(aglGetCurrentContext());
Mike Klein8f11d4d2018-01-24 12:42:55 -05002634 #elif defined(SK_BUILD_FOR_WIN)
bsalomon@google.comd302f142011-03-03 13:54:13 +00002635 SwapBuf();
2636 int set_a_break_pt_here = 9;
2637 SwapBuf();
2638 #endif
2639#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00002640}
2641
Chris Dalton3809bab2017-06-13 10:55:06 -06002642static GrGLenum gr_primitive_type_to_gl_mode(GrPrimitiveType primitiveType) {
2643 switch (primitiveType) {
2644 case GrPrimitiveType::kTriangles:
2645 return GR_GL_TRIANGLES;
2646 case GrPrimitiveType::kTriangleStrip:
2647 return GR_GL_TRIANGLE_STRIP;
Chris Dalton3809bab2017-06-13 10:55:06 -06002648 case GrPrimitiveType::kPoints:
2649 return GR_GL_POINTS;
2650 case GrPrimitiveType::kLines:
2651 return GR_GL_LINES;
2652 case GrPrimitiveType::kLineStrip:
2653 return GR_GL_LINE_STRIP;
2654 case GrPrimitiveType::kLinesAdjacency:
2655 return GR_GL_LINES_ADJACENCY;
2656 }
Ben Wagnerb4aab9a2017-08-16 10:53:04 -04002657 SK_ABORT("invalid GrPrimitiveType");
Chris Dalton3809bab2017-06-13 10:55:06 -06002658 return GR_GL_TRIANGLES;
2659}
2660
Brian Salomon802cb312018-06-08 18:05:20 -04002661void GrGLGpu::sendMeshToGpu(GrPrimitiveType primitiveType, const GrBuffer* vertexBuffer,
2662 int vertexCount, int baseVertex) {
Chris Dalton3809bab2017-06-13 10:55:06 -06002663 const GrGLenum glPrimType = gr_primitive_type_to_gl_mode(primitiveType);
Chris Dalton114a3c02017-05-26 15:17:19 -06002664 if (this->glCaps().drawArraysBaseVertexIsBroken()) {
Brian Salomon802cb312018-06-08 18:05:20 -04002665 this->setupGeometry(nullptr, vertexBuffer, baseVertex, nullptr, 0, GrPrimitiveRestart::kNo);
Chris Dalton114a3c02017-05-26 15:17:19 -06002666 GL_CALL(DrawArrays(glPrimType, 0, vertexCount));
2667 } else {
Brian Salomon802cb312018-06-08 18:05:20 -04002668 this->setupGeometry(nullptr, vertexBuffer, 0, nullptr, 0, GrPrimitiveRestart::kNo);
Chris Dalton114a3c02017-05-26 15:17:19 -06002669 GL_CALL(DrawArrays(glPrimType, baseVertex, vertexCount));
2670 }
2671 fStats.incNumDraws();
2672}
2673
Brian Salomondbf70722019-02-07 11:31:24 -05002674static const GrGLvoid* element_ptr(const GrBuffer* indexBuffer, int baseIndex) {
2675 size_t baseOffset = baseIndex * sizeof(uint16_t);
2676 if (indexBuffer->isCpuBuffer()) {
2677 return static_cast<const GrCpuBuffer*>(indexBuffer)->data() + baseOffset;
2678 } else {
2679 return reinterpret_cast<const GrGLvoid*>(baseOffset);
2680 }
2681}
2682
Brian Salomon802cb312018-06-08 18:05:20 -04002683void GrGLGpu::sendIndexedMeshToGpu(GrPrimitiveType primitiveType, const GrBuffer* indexBuffer,
Chris Dalton114a3c02017-05-26 15:17:19 -06002684 int indexCount, int baseIndex, uint16_t minIndexValue,
2685 uint16_t maxIndexValue, const GrBuffer* vertexBuffer,
Brian Salomon802cb312018-06-08 18:05:20 -04002686 int baseVertex, GrPrimitiveRestart enablePrimitiveRestart) {
Chris Dalton3809bab2017-06-13 10:55:06 -06002687 const GrGLenum glPrimType = gr_primitive_type_to_gl_mode(primitiveType);
Brian Salomondbf70722019-02-07 11:31:24 -05002688 const GrGLvoid* elementPtr = element_ptr(indexBuffer, baseIndex);
Chris Dalton114a3c02017-05-26 15:17:19 -06002689
Brian Salomon802cb312018-06-08 18:05:20 -04002690 this->setupGeometry(indexBuffer, vertexBuffer, baseVertex, nullptr, 0, enablePrimitiveRestart);
Chris Dalton114a3c02017-05-26 15:17:19 -06002691
2692 if (this->glCaps().drawRangeElementsSupport()) {
2693 GL_CALL(DrawRangeElements(glPrimType, minIndexValue, maxIndexValue, indexCount,
Brian Salomondbf70722019-02-07 11:31:24 -05002694 GR_GL_UNSIGNED_SHORT, elementPtr));
Chris Dalton114a3c02017-05-26 15:17:19 -06002695 } else {
Brian Salomondbf70722019-02-07 11:31:24 -05002696 GL_CALL(DrawElements(glPrimType, indexCount, GR_GL_UNSIGNED_SHORT, elementPtr));
Chris Dalton114a3c02017-05-26 15:17:19 -06002697 }
2698 fStats.incNumDraws();
2699}
2700
Brian Salomon802cb312018-06-08 18:05:20 -04002701void GrGLGpu::sendInstancedMeshToGpu(GrPrimitiveType primitiveType, const GrBuffer* vertexBuffer,
Chris Dalton1d616352017-05-31 12:51:23 -06002702 int vertexCount, int baseVertex,
2703 const GrBuffer* instanceBuffer, int instanceCount,
2704 int baseInstance) {
Chris Daltoncc604e52017-10-06 16:27:32 -06002705 GrGLenum glPrimType = gr_primitive_type_to_gl_mode(primitiveType);
Chris Dalton1b4ad762018-10-04 11:58:09 -06002706 int maxInstances = this->glCaps().maxInstancesPerDrawWithoutCrashing(instanceCount);
Chris Daltoncc604e52017-10-06 16:27:32 -06002707 for (int i = 0; i < instanceCount; i += maxInstances) {
Brian Salomon802cb312018-06-08 18:05:20 -04002708 this->setupGeometry(nullptr, vertexBuffer, 0, instanceBuffer, baseInstance + i,
2709 GrPrimitiveRestart::kNo);
Chris Daltoncc604e52017-10-06 16:27:32 -06002710 GL_CALL(DrawArraysInstanced(glPrimType, baseVertex, vertexCount,
2711 SkTMin(instanceCount - i, maxInstances)));
2712 fStats.incNumDraws();
2713 }
Chris Dalton1d616352017-05-31 12:51:23 -06002714}
2715
Brian Salomon802cb312018-06-08 18:05:20 -04002716void GrGLGpu::sendIndexedInstancedMeshToGpu(GrPrimitiveType primitiveType,
Chris Dalton1d616352017-05-31 12:51:23 -06002717 const GrBuffer* indexBuffer, int indexCount,
2718 int baseIndex, const GrBuffer* vertexBuffer,
2719 int baseVertex, const GrBuffer* instanceBuffer,
Brian Salomon802cb312018-06-08 18:05:20 -04002720 int instanceCount, int baseInstance,
2721 GrPrimitiveRestart enablePrimitiveRestart) {
Chris Dalton3809bab2017-06-13 10:55:06 -06002722 const GrGLenum glPrimType = gr_primitive_type_to_gl_mode(primitiveType);
Brian Salomondbf70722019-02-07 11:31:24 -05002723 const GrGLvoid* elementPtr = element_ptr(indexBuffer, baseIndex);
Chris Dalton1b4ad762018-10-04 11:58:09 -06002724 int maxInstances = this->glCaps().maxInstancesPerDrawWithoutCrashing(instanceCount);
2725 for (int i = 0; i < instanceCount; i += maxInstances) {
2726 this->setupGeometry(indexBuffer, vertexBuffer, baseVertex, instanceBuffer, baseInstance + i,
2727 enablePrimitiveRestart);
Brian Salomondbf70722019-02-07 11:31:24 -05002728 GL_CALL(DrawElementsInstanced(glPrimType, indexCount, GR_GL_UNSIGNED_SHORT, elementPtr,
Chris Dalton1b4ad762018-10-04 11:58:09 -06002729 SkTMin(instanceCount - i, maxInstances)));
2730 fStats.incNumDraws();
2731 }
Chris Dalton1d616352017-05-31 12:51:23 -06002732}
2733
Brian Salomon1fabd512018-02-09 09:54:25 -05002734void GrGLGpu::onResolveRenderTarget(GrRenderTarget* target) {
bsalomon@google.com75f9f252012-01-31 13:35:56 +00002735 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00002736 if (rt->needsResolve()) {
bsalomon@google.com347c3822013-05-01 20:10:01 +00002737 // Some extensions automatically resolves the texture when it is read.
2738 if (this->glCaps().usesMSAARenderBuffers()) {
egdanield803f272015-03-18 13:01:52 -07002739 SkASSERT(rt->textureFBOID() != rt->renderFBOID());
Brian Osmancfe83d12018-01-19 11:13:45 -05002740 SkASSERT(rt->textureFBOID() != 0 && rt->renderFBOID() != 0);
Adrienne Walker4ee88512018-05-17 11:37:14 -07002741 this->bindFramebuffer(GR_GL_READ_FRAMEBUFFER, rt->renderFBOID());
2742 this->bindFramebuffer(GR_GL_DRAW_FRAMEBUFFER, rt->textureFBOID());
2743
egdanield803f272015-03-18 13:01:52 -07002744 // make sure we go through flushRenderTarget() since we've modified
2745 // the bound DRAW FBO ID.
Robert Phillips294870f2016-11-11 12:38:40 -05002746 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002747 const GrGLIRect& vp = rt->getViewport();
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00002748 const SkIRect dirtyRect = rt->getResolveRect();
Brian Salomon1fabd512018-02-09 09:54:25 -05002749 // The dirty rect tracked on the RT is always stored in the native coordinates of the
2750 // surface. Choose kTopLeft so no adjustments are made
2751 static constexpr auto kDirtyRectOrigin = kTopLeft_GrSurfaceOrigin;
bsalomon@google.com347c3822013-05-01 20:10:01 +00002752 if (GrGLCaps::kES_Apple_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002753 // Apple's extension uses the scissor as the blit bounds.
Brian Salomond818ebf2018-07-02 14:08:49 +00002754 GrScissorState scissorState;
2755 scissorState.set(dirtyRect);
2756 this->flushScissor(scissorState, vp, kDirtyRectOrigin);
csmartdalton28341fa2016-08-17 10:00:21 -07002757 this->disableWindowRectangles();
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002758 GL_CALL(ResolveMultisampleFramebuffer());
2759 } else {
Brian Salomone5e7eb12016-10-14 16:18:33 -04002760 int l, b, r, t;
2761 if (GrGLCaps::kResolveMustBeFull_BlitFrambufferFlag &
2762 this->glCaps().blitFramebufferSupportFlags()) {
2763 l = 0;
2764 b = 0;
2765 r = target->width();
2766 t = target->height();
2767 } else {
2768 GrGLIRect rect;
Brian Salomon1fabd512018-02-09 09:54:25 -05002769 rect.setRelativeTo(vp, dirtyRect, kDirtyRectOrigin);
Brian Salomone5e7eb12016-10-14 16:18:33 -04002770 l = rect.fLeft;
2771 b = rect.fBottom;
2772 r = rect.fLeft + rect.fWidth;
2773 t = rect.fBottom + rect.fHeight;
2774 }
derekf8c8f71a2014-09-16 06:24:57 -07002775
2776 // BlitFrameBuffer respects the scissor, so disable it.
Brian Salomond818ebf2018-07-02 14:08:49 +00002777 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07002778 this->disableWindowRectangles();
Brian Salomone5e7eb12016-10-14 16:18:33 -04002779 GL_CALL(BlitFramebuffer(l, b, r, t, l, b, r, t,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002780 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00002781 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002782 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00002783 rt->flagAsResolved();
reed@google.comac10a2d2010-12-22 21:39:39 +00002784 }
2785}
2786
bsalomon@google.com411dad02012-06-05 20:24:20 +00002787namespace {
bsalomon@google.comd302f142011-03-03 13:54:13 +00002788
bsalomon@google.com411dad02012-06-05 20:24:20 +00002789
2790GrGLenum gr_to_gl_stencil_op(GrStencilOp op) {
cdalton93a379b2016-05-11 13:58:08 -07002791 static const GrGLenum gTable[kGrStencilOpCount] = {
2792 GR_GL_KEEP, // kKeep
2793 GR_GL_ZERO, // kZero
2794 GR_GL_REPLACE, // kReplace
2795 GR_GL_INVERT, // kInvert
2796 GR_GL_INCR_WRAP, // kIncWrap
2797 GR_GL_DECR_WRAP, // kDecWrap
2798 GR_GL_INCR, // kIncClamp
2799 GR_GL_DECR, // kDecClamp
bsalomon@google.com411dad02012-06-05 20:24:20 +00002800 };
cdalton93a379b2016-05-11 13:58:08 -07002801 GR_STATIC_ASSERT(0 == (int)GrStencilOp::kKeep);
2802 GR_STATIC_ASSERT(1 == (int)GrStencilOp::kZero);
2803 GR_STATIC_ASSERT(2 == (int)GrStencilOp::kReplace);
2804 GR_STATIC_ASSERT(3 == (int)GrStencilOp::kInvert);
2805 GR_STATIC_ASSERT(4 == (int)GrStencilOp::kIncWrap);
2806 GR_STATIC_ASSERT(5 == (int)GrStencilOp::kDecWrap);
2807 GR_STATIC_ASSERT(6 == (int)GrStencilOp::kIncClamp);
2808 GR_STATIC_ASSERT(7 == (int)GrStencilOp::kDecClamp);
2809 SkASSERT(op < (GrStencilOp)kGrStencilOpCount);
2810 return gTable[(int)op];
bsalomon@google.com411dad02012-06-05 20:24:20 +00002811}
2812
2813void set_gl_stencil(const GrGLInterface* gl,
cdalton93a379b2016-05-11 13:58:08 -07002814 const GrStencilSettings::Face& face,
2815 GrGLenum glFace) {
2816 GrGLenum glFunc = GrToGLStencilFunc(face.fTest);
2817 GrGLenum glFailOp = gr_to_gl_stencil_op(face.fFailOp);
2818 GrGLenum glPassOp = gr_to_gl_stencil_op(face.fPassOp);
bsalomon@google.coma3201942012-06-21 19:58:20 +00002819
cdalton93a379b2016-05-11 13:58:08 -07002820 GrGLint ref = face.fRef;
2821 GrGLint mask = face.fTestMask;
2822 GrGLint writeMask = face.fWriteMask;
bsalomon@google.com411dad02012-06-05 20:24:20 +00002823
2824 if (GR_GL_FRONT_AND_BACK == glFace) {
2825 // we call the combined func just in case separate stencil is not
2826 // supported.
2827 GR_GL_CALL(gl, StencilFunc(glFunc, ref, mask));
2828 GR_GL_CALL(gl, StencilMask(writeMask));
vbuzinovc5d58f02015-08-21 05:24:24 -07002829 GR_GL_CALL(gl, StencilOp(glFailOp, GR_GL_KEEP, glPassOp));
bsalomon@google.com411dad02012-06-05 20:24:20 +00002830 } else {
2831 GR_GL_CALL(gl, StencilFuncSeparate(glFace, glFunc, ref, mask));
2832 GR_GL_CALL(gl, StencilMaskSeparate(glFace, writeMask));
vbuzinovc5d58f02015-08-21 05:24:24 -07002833 GR_GL_CALL(gl, StencilOpSeparate(glFace, glFailOp, GR_GL_KEEP, glPassOp));
bsalomon@google.com411dad02012-06-05 20:24:20 +00002834 }
2835}
2836}
bsalomon@google.comd302f142011-03-03 13:54:13 +00002837
Chris Dalton71713f62019-04-18 12:41:03 -06002838void GrGLGpu::flushStencil(const GrStencilSettings& stencilSettings, GrSurfaceOrigin origin) {
csmartdaltonc7d85332016-10-26 10:13:46 -07002839 if (stencilSettings.isDisabled()) {
2840 this->disableStencil();
Chris Dalton71713f62019-04-18 12:41:03 -06002841 } else if (fHWStencilSettings != stencilSettings ||
2842 (stencilSettings.isTwoSided() && fHWStencilOrigin != origin)) {
csmartdaltonc7d85332016-10-26 10:13:46 -07002843 if (kYes_TriState != fHWStencilTestEnabled) {
2844 GL_CALL(Enable(GR_GL_STENCIL_TEST));
Brian Salomonaf971de2017-06-08 16:11:33 -04002845
csmartdaltonc7d85332016-10-26 10:13:46 -07002846 fHWStencilTestEnabled = kYes_TriState;
bsalomon@google.coma3201942012-06-21 19:58:20 +00002847 }
csmartdaltonc7d85332016-10-26 10:13:46 -07002848 if (stencilSettings.isTwoSided()) {
Chris Dalton71713f62019-04-18 12:41:03 -06002849 set_gl_stencil(this->glInterface(), stencilSettings.front(origin), GR_GL_FRONT);
2850 set_gl_stencil(this->glInterface(), stencilSettings.back(origin), GR_GL_BACK);
csmartdaltonc7d85332016-10-26 10:13:46 -07002851 } else {
Chris Dalton71713f62019-04-18 12:41:03 -06002852 set_gl_stencil(
2853 this->glInterface(), stencilSettings.frontAndBack(), GR_GL_FRONT_AND_BACK);
bsalomon@google.comd302f142011-03-03 13:54:13 +00002854 }
joshualitta58fe352014-10-27 08:39:00 -07002855 fHWStencilSettings = stencilSettings;
Chris Dalton71713f62019-04-18 12:41:03 -06002856 fHWStencilOrigin = origin;
reed@google.comac10a2d2010-12-22 21:39:39 +00002857 }
2858}
2859
csmartdaltonc7d85332016-10-26 10:13:46 -07002860void GrGLGpu::disableStencil() {
2861 if (kNo_TriState != fHWStencilTestEnabled) {
2862 GL_CALL(Disable(GR_GL_STENCIL_TEST));
Brian Salomonaf971de2017-06-08 16:11:33 -04002863
csmartdaltonc7d85332016-10-26 10:13:46 -07002864 fHWStencilTestEnabled = kNo_TriState;
2865 fHWStencilSettings.invalidate();
2866 }
2867}
2868
Chris Dalton4c56b032019-03-04 12:28:42 -07002869void GrGLGpu::flushHWAAState(GrRenderTarget* rt, bool useHWAA) {
bsalomon083617b2016-02-12 12:10:14 -08002870 // rt is only optional if useHWAA is false.
2871 SkASSERT(rt || !useHWAA);
vbuzinovdded6962015-06-12 08:59:45 -07002872 SkASSERT(!useHWAA || rt->isStencilBufferMultisampled());
bsalomon@google.com202d1392013-03-19 18:58:08 +00002873
csmartdalton2b5f2cb2016-06-10 14:06:32 -07002874 if (this->caps()->multisampleDisableSupport()) {
cdaltond0a840d2015-03-16 17:19:58 -07002875 if (useHWAA) {
2876 if (kYes_TriState != fMSAAEnabled) {
2877 GL_CALL(Enable(GR_GL_MULTISAMPLE));
2878 fMSAAEnabled = kYes_TriState;
2879 }
2880 } else {
2881 if (kNo_TriState != fMSAAEnabled) {
2882 GL_CALL(Disable(GR_GL_MULTISAMPLE));
2883 fMSAAEnabled = kNo_TriState;
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00002884 }
2885 }
2886 }
2887}
2888
bsalomon7f9b2e42016-01-12 13:29:26 -08002889void GrGLGpu::flushBlend(const GrXferProcessor::BlendInfo& blendInfo, const GrSwizzle& swizzle) {
egdanielb414f252014-07-29 13:15:47 -07002890 // Any optimization to disable blending should have already been applied and
cdalton8917d622015-05-06 13:40:21 -07002891 // tweaked the equation to "add" or "subtract", and the coeffs to (1, 0).
bsalomonf7cc8772015-05-11 11:21:14 -07002892
cdalton8917d622015-05-06 13:40:21 -07002893 GrBlendEquation equation = blendInfo.fEquation;
egdanielc2304142014-12-11 13:15:13 -08002894 GrBlendCoeff srcCoeff = blendInfo.fSrcBlend;
2895 GrBlendCoeff dstCoeff = blendInfo.fDstBlend;
Dongseong Hwang4e1f0222018-11-01 09:10:51 -07002896 bool blendOff =
2897 ((kAdd_GrBlendEquation == equation || kSubtract_GrBlendEquation == equation) &&
2898 kOne_GrBlendCoeff == srcCoeff && kZero_GrBlendCoeff == dstCoeff) ||
2899 !blendInfo.fWriteColor;
egdanielb414f252014-07-29 13:15:47 -07002900 if (blendOff) {
2901 if (kNo_TriState != fHWBlendState.fEnabled) {
2902 GL_CALL(Disable(GR_GL_BLEND));
joel.liang9764c402015-07-09 19:46:18 -07002903
2904 // Workaround for the ARM KHR_blend_equation_advanced blacklist issue
2905 // https://code.google.com/p/skia/issues/detail?id=3943
2906 if (kARM_GrGLVendor == this->ctxInfo().vendor() &&
2907 GrBlendEquationIsAdvanced(fHWBlendState.fEquation)) {
2908 SkASSERT(this->caps()->advancedBlendEquationSupport());
2909 // Set to any basic blending equation.
2910 GrBlendEquation blend_equation = kAdd_GrBlendEquation;
2911 GL_CALL(BlendEquation(gXfermodeEquation2Blend[blend_equation]));
2912 fHWBlendState.fEquation = blend_equation;
2913 }
2914
egdanielb414f252014-07-29 13:15:47 -07002915 fHWBlendState.fEnabled = kNo_TriState;
2916 }
cdalton8917d622015-05-06 13:40:21 -07002917 return;
2918 }
2919
2920 if (kYes_TriState != fHWBlendState.fEnabled) {
2921 GL_CALL(Enable(GR_GL_BLEND));
Brian Salomonaf971de2017-06-08 16:11:33 -04002922
cdalton8917d622015-05-06 13:40:21 -07002923 fHWBlendState.fEnabled = kYes_TriState;
2924 }
2925
Mike Kleinab5fec92018-08-16 19:43:31 +00002926 if (fHWBlendState.fEquation != equation) {
cdalton8917d622015-05-06 13:40:21 -07002927 GL_CALL(BlendEquation(gXfermodeEquation2Blend[equation]));
2928 fHWBlendState.fEquation = equation;
2929 }
2930
2931 if (GrBlendEquationIsAdvanced(equation)) {
2932 SkASSERT(this->caps()->advancedBlendEquationSupport());
2933 // Advanced equations have no other blend state.
2934 return;
2935 }
2936
Mike Kleinab5fec92018-08-16 19:43:31 +00002937 if (fHWBlendState.fSrcCoeff != srcCoeff || fHWBlendState.fDstCoeff != dstCoeff) {
cdalton8917d622015-05-06 13:40:21 -07002938 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
2939 gXfermodeCoeff2Blend[dstCoeff]));
2940 fHWBlendState.fSrcCoeff = srcCoeff;
2941 fHWBlendState.fDstCoeff = dstCoeff;
2942 }
2943
bsalomon7f9b2e42016-01-12 13:29:26 -08002944 if ((BlendCoeffReferencesConstant(srcCoeff) || BlendCoeffReferencesConstant(dstCoeff))) {
Brian Osman422f95b2018-11-05 16:49:04 -05002945 SkPMColor4f blendConst = swizzle.applyTo(blendInfo.fBlendConstant);
bsalomon7f9b2e42016-01-12 13:29:26 -08002946 if (!fHWBlendState.fConstColorValid || fHWBlendState.fConstColor != blendConst) {
Brian Osman422f95b2018-11-05 16:49:04 -05002947 GL_CALL(BlendColor(blendConst.fR, blendConst.fG, blendConst.fB, blendConst.fA));
bsalomon7f9b2e42016-01-12 13:29:26 -08002948 fHWBlendState.fConstColor = blendConst;
2949 fHWBlendState.fConstColorValid = true;
2950 }
bsalomon@google.com0650e812011-04-08 18:07:53 +00002951 }
2952}
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002953
Brian Salomondc829942018-10-23 16:07:24 -04002954static void get_gl_swizzle_values(const GrSwizzle& swizzle, GrGLenum glValues[4]) {
Brian Salomon327955b2018-10-22 15:39:22 +00002955 for (int i = 0; i < 4; ++i) {
Brian Salomondc829942018-10-23 16:07:24 -04002956 switch (swizzle[i]) {
2957 case 'r': glValues[i] = GR_GL_RED; break;
2958 case 'g': glValues[i] = GR_GL_GREEN; break;
2959 case 'b': glValues[i] = GR_GL_BLUE; break;
2960 case 'a': glValues[i] = GR_GL_ALPHA; break;
Greg Daniel216a9d72019-02-20 10:12:29 -05002961 case '1': glValues[i] = GR_GL_ONE; break;
Brian Salomondc829942018-10-23 16:07:24 -04002962 default: SK_ABORT("Unsupported component");
2963 }
Brian Salomon327955b2018-10-22 15:39:22 +00002964 }
2965}
2966
Brian Salomondc829942018-10-23 16:07:24 -04002967void GrGLGpu::bindTexture(int unitIdx, GrSamplerState samplerState, GrGLTexture* texture) {
bsalomon49f085d2014-09-05 13:34:00 -07002968 SkASSERT(texture);
tomhudson@google.comd0c1a062012-07-12 17:23:52 +00002969
reed856e9d92015-09-30 12:21:45 -07002970#ifdef SK_DEBUG
2971 if (!this->caps()->npotTextureTileSupport()) {
Brian Salomon2bbdcc42017-09-07 12:36:34 -04002972 if (samplerState.isRepeated()) {
reed856e9d92015-09-30 12:21:45 -07002973 const int w = texture->width();
2974 const int h = texture->height();
2975 SkASSERT(SkIsPow2(w) && SkIsPow2(h));
2976 }
2977 }
2978#endif
2979
bsalomon@google.comb8670992012-07-25 21:27:09 +00002980 // If we created a rt/tex and rendered to it without using a texture and now we're texturing
2981 // 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 +00002982 // out of the "last != next" check.
bsalomon37dd3312014-11-03 08:47:23 -08002983 GrGLRenderTarget* texRT = static_cast<GrGLRenderTarget*>(texture->asRenderTarget());
bsalomon49f085d2014-09-05 13:34:00 -07002984 if (texRT) {
Brian Salomon1fabd512018-02-09 09:54:25 -05002985 this->onResolveRenderTarget(texRT);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002986 }
2987
Robert Phillips294870f2016-11-11 12:38:40 -05002988 GrGpuResource::UniqueID textureID = texture->uniqueID();
bsalomon10528f12015-10-14 12:54:52 -07002989 GrGLenum target = texture->target();
Brian Salomond978b902019-02-07 15:09:18 -05002990 if (fHWTextureUnitBindings[unitIdx].boundID(target) != textureID) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002991 this->setTextureUnit(unitIdx);
bsalomon10528f12015-10-14 12:54:52 -07002992 GL_CALL(BindTexture(target, texture->textureID()));
Brian Salomond978b902019-02-07 15:09:18 -05002993 fHWTextureUnitBindings[unitIdx].setBoundID(target, textureID);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002994 }
2995
Brian Salomondc829942018-10-23 16:07:24 -04002996 if (samplerState.filter() == GrSamplerState::Filter::kMipMap) {
Greg Daniel8f5bbda2018-06-08 17:22:23 -04002997 if (!this->caps()->mipMapSupport() ||
2998 texture->texturePriv().mipMapped() == GrMipMapped::kNo) {
Brian Salomondc829942018-10-23 16:07:24 -04002999 samplerState.setFilterMode(GrSamplerState::Filter::kBilerp);
bsalomonefd7d452014-10-23 14:17:46 -07003000 }
commit-bot@chromium.org47442312013-12-19 16:18:01 +00003001 }
bsalomonefd7d452014-10-23 14:17:46 -07003002
brianosman33f6b3f2016-06-02 05:49:21 -07003003#ifdef SK_DEBUG
Brian Osman2b23c4b2018-06-01 12:25:08 -04003004 // We were supposed to ensure MipMaps were up-to-date before getting here.
Brian Salomondc829942018-10-23 16:07:24 -04003005 if (samplerState.filter() == GrSamplerState::Filter::kMipMap) {
brianosman33f6b3f2016-06-02 05:49:21 -07003006 SkASSERT(!texture->texturePriv().mipMapsAreDirty());
brianosman33f6b3f2016-06-02 05:49:21 -07003007 }
3008#endif
3009
Brian Salomondc829942018-10-23 16:07:24 -04003010 ResetTimestamp timestamp = texture->getCachedParamsTimestamp();
3011 bool setAll = timestamp < this->getResetTimestamp();
cblume55f2d2d2016-02-26 13:20:48 -08003012
Brian Salomondc829942018-10-23 16:07:24 -04003013 const GrGLTexture::SamplerParams* samplerParamsToRecord = nullptr;
3014 GrGLTexture::SamplerParams newSamplerParams;
3015 if (fSamplerObjectCache) {
3016 fSamplerObjectCache->bindSampler(unitIdx, samplerState);
3017 } else {
3018 const GrGLTexture::SamplerParams& oldSamplerParams = texture->getCachedSamplerParams();
3019 samplerParamsToRecord = &newSamplerParams;
3020
3021 newSamplerParams.fMinFilter = filter_to_gl_min_filter(samplerState.filter());
3022 newSamplerParams.fMagFilter = filter_to_gl_mag_filter(samplerState.filter());
3023
Michael Ludwigf23a1522018-12-10 11:36:13 -05003024 newSamplerParams.fWrapS = wrap_mode_to_gl_wrap(samplerState.wrapModeX(), this->glCaps());
3025 newSamplerParams.fWrapT = wrap_mode_to_gl_wrap(samplerState.wrapModeY(), this->glCaps());
Brian Salomondc829942018-10-23 16:07:24 -04003026
3027 // These are the OpenGL default values.
Brian Salomon89f2ff12018-12-07 19:30:25 -05003028 newSamplerParams.fMinLOD = -1000.f;
3029 newSamplerParams.fMaxLOD = 1000.f;
Brian Salomondc829942018-10-23 16:07:24 -04003030
3031 if (setAll || newSamplerParams.fMagFilter != oldSamplerParams.fMagFilter) {
3032 this->setTextureUnit(unitIdx);
3033 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MAG_FILTER, newSamplerParams.fMagFilter));
3034 }
3035 if (setAll || newSamplerParams.fMinFilter != oldSamplerParams.fMinFilter) {
3036 this->setTextureUnit(unitIdx);
3037 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MIN_FILTER, newSamplerParams.fMinFilter));
3038 }
Mike Klein1bccae52018-10-19 11:38:44 +00003039 if (this->glCaps().mipMapLevelAndLodControlSupport()) {
Brian Salomondc829942018-10-23 16:07:24 -04003040 if (setAll || newSamplerParams.fMinLOD != oldSamplerParams.fMinLOD) {
Mike Klein1bccae52018-10-19 11:38:44 +00003041 this->setTextureUnit(unitIdx);
Brian Salomon89f2ff12018-12-07 19:30:25 -05003042 GL_CALL(TexParameterf(target, GR_GL_TEXTURE_MIN_LOD, newSamplerParams.fMinLOD));
Brian Salomondc829942018-10-23 16:07:24 -04003043 }
3044 if (setAll || newSamplerParams.fMaxLOD != oldSamplerParams.fMaxLOD) {
3045 this->setTextureUnit(unitIdx);
Brian Salomon89f2ff12018-12-07 19:30:25 -05003046 GL_CALL(TexParameterf(target, GR_GL_TEXTURE_MAX_LOD, newSamplerParams.fMaxLOD));
Brian Salomondc829942018-10-23 16:07:24 -04003047 }
3048 }
3049 if (setAll || newSamplerParams.fWrapS != oldSamplerParams.fWrapS) {
3050 this->setTextureUnit(unitIdx);
3051 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_WRAP_S, newSamplerParams.fWrapS));
3052 }
3053 if (setAll || newSamplerParams.fWrapT != oldSamplerParams.fWrapT) {
3054 this->setTextureUnit(unitIdx);
3055 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_WRAP_T, newSamplerParams.fWrapT));
3056 }
Michael Ludwigf23a1522018-12-10 11:36:13 -05003057 if (this->glCaps().clampToBorderSupport()) {
3058 // Make sure the border color is transparent black (the default)
3059 if (setAll || oldSamplerParams.fBorderColorInvalid) {
3060 this->setTextureUnit(unitIdx);
Brian Salomon89f2ff12018-12-07 19:30:25 -05003061 static const GrGLfloat kTransparentBlack[4] = {0.f, 0.f, 0.f, 0.f};
3062 GL_CALL(TexParameterfv(target, GR_GL_TEXTURE_BORDER_COLOR, kTransparentBlack));
Michael Ludwigf23a1522018-12-10 11:36:13 -05003063 }
3064 }
Brian Salomondc829942018-10-23 16:07:24 -04003065 }
3066 GrGLTexture::NonSamplerParams newNonSamplerParams;
3067 newNonSamplerParams.fBaseMipMapLevel = 0;
3068 newNonSamplerParams.fMaxMipMapLevel = texture->texturePriv().maxMipMapLevel();
3069
3070 const GrGLTexture::NonSamplerParams& oldNonSamplerParams = texture->getCachedNonSamplerParams();
3071 if (this->glCaps().textureSwizzleSupport()) {
3072 auto swizzle = this->glCaps().configSwizzle(texture->config());
3073 newNonSamplerParams.fSwizzleKey = swizzle.asKey();
3074 if (setAll || swizzle.asKey() != oldNonSamplerParams.fSwizzleKey) {
3075 GrGLenum glValues[4];
3076 get_gl_swizzle_values(swizzle, glValues);
3077 this->setTextureUnit(unitIdx);
Kevin Lubick8aa203c2019-03-19 13:23:10 -04003078 if (GR_IS_GR_GL(this->glStandard())) {
3079 GR_STATIC_ASSERT(sizeof(glValues[0]) == sizeof(GrGLint));
3080 GL_CALL(TexParameteriv(target, GR_GL_TEXTURE_SWIZZLE_RGBA,
3081 reinterpret_cast<const GrGLint*>(glValues)));
3082 } else if (GR_IS_GR_GL_ES(this->glStandard())) {
Brian Salomondc829942018-10-23 16:07:24 -04003083 // ES3 added swizzle support but not GL_TEXTURE_SWIZZLE_RGBA.
3084 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_R, glValues[0]));
3085 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_G, glValues[1]));
3086 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_B, glValues[2]));
3087 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_A, glValues[3]));
Brian Salomonbfb3df42018-10-19 19:24:31 +00003088 }
Brian Salomonbfb3df42018-10-19 19:24:31 +00003089 }
3090 }
Brian Salomondc829942018-10-23 16:07:24 -04003091 // These are not supported in ES2 contexts
Brian Salomonf3841932018-11-29 09:13:37 -05003092 if (this->glCaps().mipMapLevelAndLodControlSupport() &&
3093 (texture->texturePriv().textureType() != GrTextureType::kExternal ||
3094 !this->glCaps().dontSetBaseOrMaxLevelForExternalTextures())) {
Brian Salomondc829942018-10-23 16:07:24 -04003095 if (newNonSamplerParams.fBaseMipMapLevel != oldNonSamplerParams.fBaseMipMapLevel) {
3096 this->setTextureUnit(unitIdx);
3097 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_BASE_LEVEL,
3098 newNonSamplerParams.fBaseMipMapLevel));
3099 }
3100 if (newNonSamplerParams.fMaxMipMapLevel != oldNonSamplerParams.fMaxMipMapLevel) {
3101 this->setTextureUnit(unitIdx);
3102 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MAX_LEVEL,
3103 newNonSamplerParams.fMaxMipMapLevel));
3104 }
Brian Salomon327955b2018-10-22 15:39:22 +00003105 }
Brian Salomondc829942018-10-23 16:07:24 -04003106 texture->setCachedParams(samplerParamsToRecord, newNonSamplerParams, this->getResetTimestamp());
cdalton74b8d322016-04-11 14:47:28 -07003107}
3108
Brian Salomon1f05d452019-02-08 12:33:08 -05003109void GrGLGpu::onResetTextureBindings() {
3110 static constexpr GrGLenum kTargets[] = {GR_GL_TEXTURE_2D, GR_GL_TEXTURE_RECTANGLE,
3111 GR_GL_TEXTURE_EXTERNAL};
3112 for (int i = 0; i < this->numTextureUnits(); ++i) {
3113 this->setTextureUnit(i);
3114 for (auto target : kTargets) {
3115 if (fHWTextureUnitBindings[i].hasBeenModified(target)) {
3116 GL_CALL(BindTexture(target, 0));
3117 }
3118 }
3119 fHWTextureUnitBindings[i].invalidateAllTargets(true);
3120 }
3121}
3122
egdaniel080e6732014-12-22 07:35:52 -08003123void GrGLGpu::flushColorWrite(bool writeColor) {
3124 if (!writeColor) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +00003125 if (kNo_TriState != fHWWriteToColor) {
3126 GL_CALL(ColorMask(GR_GL_FALSE, GR_GL_FALSE,
3127 GR_GL_FALSE, GR_GL_FALSE));
3128 fHWWriteToColor = kNo_TriState;
bsalomon@google.comd302f142011-03-03 13:54:13 +00003129 }
bsalomon@google.com978c8c62012-05-21 14:45:49 +00003130 } else {
3131 if (kYes_TriState != fHWWriteToColor) {
3132 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
3133 fHWWriteToColor = kYes_TriState;
3134 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00003135 }
bsalomon3e791242014-12-17 13:43:13 -08003136}
bsalomon@google.comd302f142011-03-03 13:54:13 +00003137
Brian Salomon805cc7a2019-01-28 09:52:34 -05003138void GrGLGpu::flushClearColor(GrGLfloat r, GrGLfloat g, GrGLfloat b, GrGLfloat a) {
3139 if (r != fHWClearColor[0] || g != fHWClearColor[1] ||
3140 b != fHWClearColor[2] || a != fHWClearColor[3]) {
3141 GL_CALL(ClearColor(r, g, b, a));
3142 fHWClearColor[0] = r;
3143 fHWClearColor[1] = g;
3144 fHWClearColor[2] = b;
3145 fHWClearColor[3] = a;
3146 }
3147}
3148
bsalomon861e1032014-12-16 07:33:49 -08003149void GrGLGpu::setTextureUnit(int unit) {
Brian Salomond978b902019-02-07 15:09:18 -05003150 SkASSERT(unit >= 0 && unit < this->numTextureUnits());
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00003151 if (unit != fHWActiveTextureUnitIdx) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00003152 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com49209392012-06-05 15:13:46 +00003153 fHWActiveTextureUnitIdx = unit;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00003154 }
3155}
bsalomon@google.com316f99232011-01-13 21:28:12 +00003156
Brian Salomond978b902019-02-07 15:09:18 -05003157void GrGLGpu::bindTextureToScratchUnit(GrGLenum target, GrGLint textureID) {
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00003158 // Bind the last texture unit since it is the least likely to be used by GrGLProgram.
Brian Salomond978b902019-02-07 15:09:18 -05003159 int lastUnitIdx = this->numTextureUnits() - 1;
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00003160 if (lastUnitIdx != fHWActiveTextureUnitIdx) {
3161 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + lastUnitIdx));
3162 fHWActiveTextureUnitIdx = lastUnitIdx;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00003163 }
Brian Salomond978b902019-02-07 15:09:18 -05003164 // Clear out the this field so that if a GrGLProgram does use this unit it will rebind the
3165 // correct texture.
Brian Salomon1f05d452019-02-08 12:33:08 -05003166 fHWTextureUnitBindings[lastUnitIdx].invalidateForScratchUse(target);
Brian Salomond978b902019-02-07 15:09:18 -05003167 GL_CALL(BindTexture(target, textureID));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00003168}
3169
Brian Salomone5e7eb12016-10-14 16:18:33 -04003170// Determines whether glBlitFramebuffer could be used between src and dst by onCopySurface.
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003171static inline bool can_blit_framebuffer_for_copy_surface(
3172 const GrSurface* dst, GrSurfaceOrigin dstOrigin,
3173 const GrSurface* src, GrSurfaceOrigin srcOrigin,
3174 const SkIRect& srcRect,
3175 const SkIPoint& dstPoint,
Greg Daniel26dbe3b2018-05-03 10:35:42 -04003176 const GrGLCaps& caps) {
3177 int dstSampleCnt = 0;
3178 int srcSampleCnt = 0;
3179 if (const GrRenderTarget* rt = dst->asRenderTarget()) {
3180 dstSampleCnt = rt->numColorSamples();
bsalomon@google.comeb851172013-04-15 13:51:00 +00003181 }
Greg Daniel26dbe3b2018-05-03 10:35:42 -04003182 if (const GrRenderTarget* rt = src->asRenderTarget()) {
3183 srcSampleCnt = rt->numColorSamples();
3184 }
3185 SkASSERT((dstSampleCnt > 0) == SkToBool(dst->asRenderTarget()));
3186 SkASSERT((srcSampleCnt > 0) == SkToBool(src->asRenderTarget()));
3187
Brian Salomone5e7eb12016-10-14 16:18:33 -04003188 const GrGLTexture* dstTex = static_cast<const GrGLTexture*>(dst->asTexture());
Mike Klein1d746202018-01-25 17:32:51 -05003189 const GrGLTexture* srcTex = static_cast<const GrGLTexture*>(src->asTexture());
Greg Daniel26dbe3b2018-05-03 10:35:42 -04003190
3191 bool dstIsGLTexture2D = dstTex ? GR_GL_TEXTURE_2D == dstTex->target() : false;
3192 bool srcIsGLTexture2D = srcTex ? GR_GL_TEXTURE_2D == srcTex->target() : false;
3193
3194 return caps.canCopyAsBlit(dst->config(), dstSampleCnt, SkToBool(dstTex), dstIsGLTexture2D,
3195 dstOrigin, src->config(), srcSampleCnt, SkToBool(srcTex),
3196 srcIsGLTexture2D, srcOrigin, src->getBoundsRect(), srcRect, dstPoint);
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00003197}
bsalomon@google.comeb851172013-04-15 13:51:00 +00003198
Brian Osmana9c8a052018-01-19 10:31:56 -05003199static bool rt_has_msaa_render_buffer(const GrGLRenderTarget* rt, const GrGLCaps& glCaps) {
3200 // A RT has a separate MSAA renderbuffer if:
3201 // 1) It's multisampled
3202 // 2) We're using an extension with separate MSAA renderbuffers
3203 // 3) It's not FBO 0, which is special and always auto-resolves
Brian Salomonbdecacf2018-02-02 20:32:49 -05003204 return rt->numColorSamples() > 1 && glCaps.usesMSAARenderBuffers() && rt->renderFBOID() != 0;
Brian Osmana9c8a052018-01-19 10:31:56 -05003205}
3206
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003207static inline bool can_copy_texsubimage(const GrSurface* dst, GrSurfaceOrigin dstOrigin,
3208 const GrSurface* src, GrSurfaceOrigin srcOrigin,
Greg Daniel26dbe3b2018-05-03 10:35:42 -04003209 const GrGLCaps& caps) {
3210
bsalomon@google.comeb851172013-04-15 13:51:00 +00003211 const GrGLRenderTarget* dstRT = static_cast<const GrGLRenderTarget*>(dst->asRenderTarget());
bsalomon@google.coma2719852013-04-17 14:25:27 +00003212 const GrGLRenderTarget* srcRT = static_cast<const GrGLRenderTarget*>(src->asRenderTarget());
bsalomon7ea33f52015-11-22 14:51:00 -08003213 const GrGLTexture* dstTex = static_cast<const GrGLTexture*>(dst->asTexture());
bsalomon7ea33f52015-11-22 14:51:00 -08003214 const GrGLTexture* srcTex = static_cast<const GrGLTexture*>(src->asTexture());
cblume61214052016-01-26 09:10:48 -08003215
Greg Daniel26dbe3b2018-05-03 10:35:42 -04003216 bool dstHasMSAARenderBuffer = dstRT ? rt_has_msaa_render_buffer(dstRT, caps) : false;
3217 bool srcHasMSAARenderBuffer = srcRT ? rt_has_msaa_render_buffer(srcRT, caps) : false;
3218
3219 bool dstIsGLTexture2D = dstTex ? GR_GL_TEXTURE_2D == dstTex->target() : false;
3220 bool srcIsGLTexture2D = srcTex ? GR_GL_TEXTURE_2D == srcTex->target() : false;
3221
3222 return caps.canCopyTexSubImage(dst->config(), dstHasMSAARenderBuffer, SkToBool(dstTex),
3223 dstIsGLTexture2D, dstOrigin, src->config(),
3224 srcHasMSAARenderBuffer, SkToBool(srcTex), srcIsGLTexture2D,
3225 srcOrigin);
bsalomon@google.comeb851172013-04-15 13:51:00 +00003226}
3227
3228// If a temporary FBO was created, its non-zero ID is returned. The viewport that the copy rect is
3229// relative to is output.
Brian Salomon71d9d842016-11-03 13:42:00 -04003230void GrGLGpu::bindSurfaceFBOForPixelOps(GrSurface* surface, GrGLenum fboTarget, GrGLIRect* viewport,
3231 TempFBOTarget tempFBOTarget) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00003232 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(surface->asRenderTarget());
bsalomon083617b2016-02-12 12:10:14 -08003233 if (!rt) {
bsalomon49f085d2014-09-05 13:34:00 -07003234 SkASSERT(surface->asTexture());
Brian Salomon9bada542017-06-12 12:09:30 -04003235 GrGLTexture* texture = static_cast<GrGLTexture*>(surface->asTexture());
3236 GrGLuint texID = texture->textureID();
3237 GrGLenum target = texture->target();
egdanield803f272015-03-18 13:01:52 -07003238 GrGLuint* tempFBOID;
3239 tempFBOID = kSrc_TempFBOTarget == tempFBOTarget ? &fTempSrcFBOID : &fTempDstFBOID;
egdaniel0f5f9672015-02-03 11:10:51 -08003240
egdanield803f272015-03-18 13:01:52 -07003241 if (0 == *tempFBOID) {
3242 GR_GL_CALL(this->glInterface(), GenFramebuffers(1, tempFBOID));
egdaniel0f5f9672015-02-03 11:10:51 -08003243 }
3244
Adrienne Walker4ee88512018-05-17 11:37:14 -07003245 this->bindFramebuffer(fboTarget, *tempFBOID);
egdanield803f272015-03-18 13:01:52 -07003246 GR_GL_CALL(this->glInterface(), FramebufferTexture2D(fboTarget,
robertphillips754f4e92014-09-18 13:52:08 -07003247 GR_GL_COLOR_ATTACHMENT0,
bsalomon10528f12015-10-14 12:54:52 -07003248 target,
robertphillips754f4e92014-09-18 13:52:08 -07003249 texID,
3250 0));
Brian Salomon9bada542017-06-12 12:09:30 -04003251 texture->baseLevelWasBoundToFBO();
bsalomon@google.comeb851172013-04-15 13:51:00 +00003252 viewport->fLeft = 0;
3253 viewport->fBottom = 0;
3254 viewport->fWidth = surface->width();
3255 viewport->fHeight = surface->height();
3256 } else {
Adrienne Walker4ee88512018-05-17 11:37:14 -07003257 this->bindFramebuffer(fboTarget, rt->renderFBOID());
bsalomon@google.comeb851172013-04-15 13:51:00 +00003258 *viewport = rt->getViewport();
3259 }
egdaniel0f5f9672015-02-03 11:10:51 -08003260}
3261
Brian Salomon71d9d842016-11-03 13:42:00 -04003262void GrGLGpu::unbindTextureFBOForPixelOps(GrGLenum fboTarget, GrSurface* surface) {
3263 // bindSurfaceFBOForPixelOps temporarily binds textures that are not render targets to
bsalomon10528f12015-10-14 12:54:52 -07003264 if (!surface->asRenderTarget()) {
3265 SkASSERT(surface->asTexture());
3266 GrGLenum textureTarget = static_cast<GrGLTexture*>(surface->asTexture())->target();
3267 GR_GL_CALL(this->glInterface(), FramebufferTexture2D(fboTarget,
3268 GR_GL_COLOR_ATTACHMENT0,
3269 textureTarget,
3270 0,
3271 0));
3272 }
bsalomon@google.comeb851172013-04-15 13:51:00 +00003273}
3274
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07003275void GrGLGpu::onFBOChanged() {
3276 if (this->caps()->workarounds().flush_on_framebuffer_change ||
3277 this->caps()->workarounds().restore_scissor_on_fbo_change) {
3278 GL_CALL(Flush());
3279 }
3280}
3281
Adrienne Walker4ee88512018-05-17 11:37:14 -07003282void GrGLGpu::bindFramebuffer(GrGLenum target, GrGLuint fboid) {
3283 fStats.incRenderTargetBinds();
3284 GL_CALL(BindFramebuffer(target, fboid));
3285 if (target == GR_GL_FRAMEBUFFER || target == GR_GL_DRAW_FRAMEBUFFER) {
3286 fBoundDrawFramebuffer = fboid;
3287 }
3288
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07003289 if (this->caps()->workarounds().restore_scissor_on_fbo_change) {
3290 // The driver forgets the correct scissor when modifying the FBO binding.
3291 if (!fHWScissorSettings.fRect.isInvalid()) {
3292 fHWScissorSettings.fRect.pushToGLScissor(this->glInterface());
3293 }
Adrienne Walker3ed33992018-05-15 11:44:34 -07003294 }
3295
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07003296 this->onFBOChanged();
Adrienne Walker3ed33992018-05-15 11:44:34 -07003297}
3298
Adrienne Walker4ee88512018-05-17 11:37:14 -07003299void GrGLGpu::deleteFramebuffer(GrGLuint fboid) {
3300 if (fboid == fBoundDrawFramebuffer &&
3301 this->caps()->workarounds().unbind_attachments_on_bound_render_fbo_delete) {
3302 // This workaround only applies to deleting currently bound framebuffers
3303 // on Adreno 420. Because this is a somewhat rare case, instead of
3304 // tracking all the attachments of every framebuffer instead just always
3305 // unbind all attachments.
3306 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
3307 GR_GL_RENDERBUFFER, 0));
3308 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT,
3309 GR_GL_RENDERBUFFER, 0));
3310 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_DEPTH_ATTACHMENT,
3311 GR_GL_RENDERBUFFER, 0));
3312 }
3313
3314 GL_CALL(DeleteFramebuffers(1, &fboid));
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07003315
3316 // Deleting the currently bound framebuffer rebinds to 0.
3317 if (fboid == fBoundDrawFramebuffer) {
3318 this->onFBOChanged();
3319 }
Adrienne Walker4ee88512018-05-17 11:37:14 -07003320}
3321
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003322bool GrGLGpu::onCopySurface(GrSurface* dst, GrSurfaceOrigin dstOrigin,
3323 GrSurface* src, GrSurfaceOrigin srcOrigin,
Greg Daniel55fa6472018-03-16 16:13:10 -04003324 const SkIRect& srcRect, const SkIPoint& dstPoint,
3325 bool canDiscardOutsideDstRect) {
bsalomon7f9b2e42016-01-12 13:29:26 -08003326 // None of our copy methods can handle a swizzle. TODO: Make copySurfaceAsDraw handle the
3327 // swizzle.
Brian Salomon1edc5b92016-11-29 13:43:46 -05003328 if (this->caps()->shaderCaps()->configOutputSwizzle(src->config()) !=
3329 this->caps()->shaderCaps()->configOutputSwizzle(dst->config())) {
bsalomon7f9b2e42016-01-12 13:29:26 -08003330 return false;
3331 }
bsalomon083617b2016-02-12 12:10:14 -08003332 // Don't prefer copying as a draw if the dst doesn't already have a FBO object.
Brian Salomon3d86a192018-02-27 16:46:11 -05003333 // This implicitly handles this->glCaps().useDrawInsteadOfAllRenderTargetWrites().
bsalomon083617b2016-02-12 12:10:14 -08003334 bool preferCopy = SkToBool(dst->asRenderTarget());
Greg Daniel26dbe3b2018-05-03 10:35:42 -04003335 if (preferCopy && this->glCaps().canCopyAsDraw(dst->config(), SkToBool(src->asTexture()))) {
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003336 if (this->copySurfaceAsDraw(dst, dstOrigin, src, srcOrigin, srcRect, dstPoint)) {
cdaltone2e71c22016-04-07 18:13:29 -07003337 return true;
3338 }
bsalomon5df6fee2015-05-18 06:26:15 -07003339 }
cblume61214052016-01-26 09:10:48 -08003340
Greg Daniel26dbe3b2018-05-03 10:35:42 -04003341 if (can_copy_texsubimage(dst, dstOrigin, src, srcOrigin, this->glCaps())) {
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003342 this->copySurfaceAsCopyTexSubImage(dst, dstOrigin, src, srcOrigin, srcRect, dstPoint);
bsalomon6df86402015-06-01 10:41:49 -07003343 return true;
3344 }
3345
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003346 if (can_blit_framebuffer_for_copy_surface(dst, dstOrigin, src, srcOrigin,
Greg Daniel26dbe3b2018-05-03 10:35:42 -04003347 srcRect, dstPoint, this->glCaps())) {
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003348 return this->copySurfaceAsBlitFramebuffer(dst, dstOrigin, src, srcOrigin,
3349 srcRect, dstPoint);
bsalomon6df86402015-06-01 10:41:49 -07003350 }
3351
Greg Daniel26dbe3b2018-05-03 10:35:42 -04003352 if (!preferCopy && this->glCaps().canCopyAsDraw(dst->config(), SkToBool(src->asTexture()))) {
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003353 if (this->copySurfaceAsDraw(dst, dstOrigin, src, srcOrigin, srcRect, dstPoint)) {
cdaltone2e71c22016-04-07 18:13:29 -07003354 return true;
3355 }
bsalomon083617b2016-02-12 12:10:14 -08003356 }
3357
bsalomon6df86402015-06-01 10:41:49 -07003358 return false;
3359}
3360
Brian Salomonbf7b6202016-11-11 16:08:03 -05003361bool GrGLGpu::createCopyProgram(GrTexture* srcTex) {
Brian Osman39c08ac2017-07-26 09:36:09 -04003362 TRACE_EVENT0("skia", TRACE_FUNC);
Ryan Macnak38a10ad2017-07-10 10:36:34 -07003363
Brian Salomonbf7b6202016-11-11 16:08:03 -05003364 int progIdx = TextureToCopyProgramIdx(srcTex);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003365 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
Brian Salomon60dd8c72018-07-30 10:24:13 -04003366 GrSLType samplerType =
3367 GrSLCombinedSamplerTypeForTextureType(srcTex->texturePriv().textureType());
cdaltone2e71c22016-04-07 18:13:29 -07003368
3369 if (!fCopyProgramArrayBuffer) {
3370 static const GrGLfloat vdata[] = {
3371 0, 0,
3372 0, 1,
3373 1, 0,
3374 1, 1
3375 };
Brian Salomonae64c192019-02-05 09:41:37 -05003376 fCopyProgramArrayBuffer = GrGLBuffer::Make(this, sizeof(vdata), GrGpuBufferType::kVertex,
Brian Salomon12d22642019-01-29 14:38:50 -05003377 kStatic_GrAccessPattern, vdata);
cdaltone2e71c22016-04-07 18:13:29 -07003378 }
3379 if (!fCopyProgramArrayBuffer) {
3380 return false;
3381 }
3382
3383 SkASSERT(!fCopyPrograms[progIdx].fProgram);
3384 GL_CALL_RET(fCopyPrograms[progIdx].fProgram, CreateProgram());
3385 if (!fCopyPrograms[progIdx].fProgram) {
3386 return false;
3387 }
3388
Brian Salomon1edc5b92016-11-29 13:43:46 -05003389 const char* version = shaderCaps->versionDeclString();
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003390 GrShaderVar aVertex("a_vertex", kHalf2_GrSLType, GrShaderVar::kIn_TypeModifier);
3391 GrShaderVar uTexCoordXform("u_texCoordXform", kHalf4_GrSLType,
Brian Salomon99938a82016-11-21 13:41:08 -05003392 GrShaderVar::kUniform_TypeModifier);
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003393 GrShaderVar uPosXform("u_posXform", kHalf4_GrSLType, GrShaderVar::kUniform_TypeModifier);
Brian Salomon99938a82016-11-21 13:41:08 -05003394 GrShaderVar uTexture("u_texture", samplerType, GrShaderVar::kUniform_TypeModifier);
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003395 GrShaderVar vTexCoord("v_texCoord", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier);
3396 GrShaderVar oFragColor("o_FragColor", kHalf4_GrSLType, GrShaderVar::kOut_TypeModifier);
cdaltone2e71c22016-04-07 18:13:29 -07003397
3398 SkString vshaderTxt(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 vshaderTxt.appendf("#extension %s : require\n", extension);
3402 }
3403 vTexCoord.addModifier("noperspective");
3404 }
3405
Brian Salomon1edc5b92016-11-29 13:43:46 -05003406 aVertex.appendDecl(shaderCaps, &vshaderTxt);
cdaltone2e71c22016-04-07 18:13:29 -07003407 vshaderTxt.append(";");
Brian Salomon1edc5b92016-11-29 13:43:46 -05003408 uTexCoordXform.appendDecl(shaderCaps, &vshaderTxt);
cdaltone2e71c22016-04-07 18:13:29 -07003409 vshaderTxt.append(";");
Brian Salomon1edc5b92016-11-29 13:43:46 -05003410 uPosXform.appendDecl(shaderCaps, &vshaderTxt);
cdaltone2e71c22016-04-07 18:13:29 -07003411 vshaderTxt.append(";");
Brian Salomon1edc5b92016-11-29 13:43:46 -05003412 vTexCoord.appendDecl(shaderCaps, &vshaderTxt);
cdaltone2e71c22016-04-07 18:13:29 -07003413 vshaderTxt.append(";");
3414
3415 vshaderTxt.append(
3416 "// Copy Program VS\n"
3417 "void main() {"
Ethan Nicholase1f55022019-02-05 17:17:40 -05003418 " v_texCoord = half2(a_vertex.xy * u_texCoordXform.xy + u_texCoordXform.zw);"
Ethan Nicholasbed683a2017-09-26 14:23:59 -04003419 " sk_Position.xy = a_vertex * u_posXform.xy + u_posXform.zw;"
3420 " sk_Position.zw = half2(0, 1);"
cdaltone2e71c22016-04-07 18:13:29 -07003421 "}"
3422 );
3423
3424 SkString fshaderTxt(version);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003425 if (shaderCaps->noperspectiveInterpolationSupport()) {
3426 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
cdaltone2e71c22016-04-07 18:13:29 -07003427 fshaderTxt.appendf("#extension %s : require\n", extension);
3428 }
3429 }
Brian Salomonf31ae492016-11-18 15:35:33 -05003430 vTexCoord.setTypeModifier(GrShaderVar::kIn_TypeModifier);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003431 vTexCoord.appendDecl(shaderCaps, &fshaderTxt);
cdaltone2e71c22016-04-07 18:13:29 -07003432 fshaderTxt.append(";");
Brian Salomon1edc5b92016-11-29 13:43:46 -05003433 uTexture.appendDecl(shaderCaps, &fshaderTxt);
cdaltone2e71c22016-04-07 18:13:29 -07003434 fshaderTxt.append(";");
cdaltone2e71c22016-04-07 18:13:29 -07003435 fshaderTxt.appendf(
3436 "// Copy Program FS\n"
3437 "void main() {"
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003438 " sk_FragColor = texture(u_texture, v_texCoord);"
3439 "}"
cdaltone2e71c22016-04-07 18:13:29 -07003440 );
3441
Brian Osman6c431d52019-04-15 16:31:54 -04003442 SkSL::String sksl(vshaderTxt.c_str(), vshaderTxt.size());
Ethan Nicholas941e7e22016-12-12 15:33:30 -05003443 SkSL::Program::Settings settings;
3444 settings.fCaps = shaderCaps;
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003445 SkSL::String glsl;
Brian Osmanac9be9d2019-05-01 10:29:34 -04003446 std::unique_ptr<SkSL::Program> program = GrSkSLtoGLSL(*fGLContext, SkSL::Program::kVertex_Kind,
Brian Osman6c431d52019-04-15 16:31:54 -04003447 sksl, settings, &glsl);
cdaltone2e71c22016-04-07 18:13:29 -07003448 GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fCopyPrograms[progIdx].fProgram,
Brian Osmanac9be9d2019-05-01 10:29:34 -04003449 GR_GL_VERTEX_SHADER, glsl, &fStats);
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003450 SkASSERT(program->fInputs.isEmpty());
cdaltone2e71c22016-04-07 18:13:29 -07003451
Brian Osman6c431d52019-04-15 16:31:54 -04003452 sksl.assign(fshaderTxt.c_str(), fshaderTxt.size());
Brian Osmanac9be9d2019-05-01 10:29:34 -04003453 program = GrSkSLtoGLSL(*fGLContext, SkSL::Program::kFragment_Kind, sksl, settings, &glsl);
cdaltone2e71c22016-04-07 18:13:29 -07003454 GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fCopyPrograms[progIdx].fProgram,
Brian Osmanac9be9d2019-05-01 10:29:34 -04003455 GR_GL_FRAGMENT_SHADER, glsl, &fStats);
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003456 SkASSERT(program->fInputs.isEmpty());
cdaltone2e71c22016-04-07 18:13:29 -07003457
3458 GL_CALL(LinkProgram(fCopyPrograms[progIdx].fProgram));
3459
3460 GL_CALL_RET(fCopyPrograms[progIdx].fTextureUniform,
3461 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_texture"));
3462 GL_CALL_RET(fCopyPrograms[progIdx].fPosXformUniform,
3463 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_posXform"));
3464 GL_CALL_RET(fCopyPrograms[progIdx].fTexCoordXformUniform,
3465 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_texCoordXform"));
3466
3467 GL_CALL(BindAttribLocation(fCopyPrograms[progIdx].fProgram, 0, "a_vertex"));
3468
3469 GL_CALL(DeleteShader(vshader));
3470 GL_CALL(DeleteShader(fshader));
3471
3472 return true;
bsalomon6df86402015-06-01 10:41:49 -07003473}
3474
brianosman33f6b3f2016-06-02 05:49:21 -07003475bool GrGLGpu::createMipmapProgram(int progIdx) {
3476 const bool oddWidth = SkToBool(progIdx & 0x2);
3477 const bool oddHeight = SkToBool(progIdx & 0x1);
3478 const int numTaps = (oddWidth ? 2 : 1) * (oddHeight ? 2 : 1);
3479
Brian Salomon1edc5b92016-11-29 13:43:46 -05003480 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
brianosman33f6b3f2016-06-02 05:49:21 -07003481
3482 SkASSERT(!fMipmapPrograms[progIdx].fProgram);
3483 GL_CALL_RET(fMipmapPrograms[progIdx].fProgram, CreateProgram());
3484 if (!fMipmapPrograms[progIdx].fProgram) {
3485 return false;
3486 }
3487
Brian Salomon1edc5b92016-11-29 13:43:46 -05003488 const char* version = shaderCaps->versionDeclString();
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003489 GrShaderVar aVertex("a_vertex", kHalf2_GrSLType, GrShaderVar::kIn_TypeModifier);
3490 GrShaderVar uTexCoordXform("u_texCoordXform", kHalf4_GrSLType,
Brian Salomon99938a82016-11-21 13:41:08 -05003491 GrShaderVar::kUniform_TypeModifier);
3492 GrShaderVar uTexture("u_texture", kTexture2DSampler_GrSLType,
3493 GrShaderVar::kUniform_TypeModifier);
brianosman33f6b3f2016-06-02 05:49:21 -07003494 // We need 1, 2, or 4 texture coordinates (depending on parity of each dimension):
Brian Salomon99938a82016-11-21 13:41:08 -05003495 GrShaderVar vTexCoords[] = {
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003496 GrShaderVar("v_texCoord0", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier),
3497 GrShaderVar("v_texCoord1", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier),
3498 GrShaderVar("v_texCoord2", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier),
3499 GrShaderVar("v_texCoord3", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier),
brianosman33f6b3f2016-06-02 05:49:21 -07003500 };
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003501 GrShaderVar oFragColor("o_FragColor", kHalf4_GrSLType,GrShaderVar::kOut_TypeModifier);
brianosman33f6b3f2016-06-02 05:49:21 -07003502
3503 SkString vshaderTxt(version);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003504 if (shaderCaps->noperspectiveInterpolationSupport()) {
3505 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
brianosman33f6b3f2016-06-02 05:49:21 -07003506 vshaderTxt.appendf("#extension %s : require\n", extension);
3507 }
3508 vTexCoords[0].addModifier("noperspective");
3509 vTexCoords[1].addModifier("noperspective");
3510 vTexCoords[2].addModifier("noperspective");
3511 vTexCoords[3].addModifier("noperspective");
3512 }
3513
Brian Salomon1edc5b92016-11-29 13:43:46 -05003514 aVertex.appendDecl(shaderCaps, &vshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003515 vshaderTxt.append(";");
Brian Salomon1edc5b92016-11-29 13:43:46 -05003516 uTexCoordXform.appendDecl(shaderCaps, &vshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003517 vshaderTxt.append(";");
3518 for (int i = 0; i < numTaps; ++i) {
Brian Salomon1edc5b92016-11-29 13:43:46 -05003519 vTexCoords[i].appendDecl(shaderCaps, &vshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003520 vshaderTxt.append(";");
3521 }
3522
3523 vshaderTxt.append(
3524 "// Mipmap Program VS\n"
3525 "void main() {"
Ethan Nicholasbed683a2017-09-26 14:23:59 -04003526 " sk_Position.xy = a_vertex * half2(2, 2) - half2(1, 1);"
3527 " sk_Position.zw = half2(0, 1);"
brianosman33f6b3f2016-06-02 05:49:21 -07003528 );
3529
3530 // Insert texture coordinate computation:
3531 if (oddWidth && oddHeight) {
3532 vshaderTxt.append(
3533 " v_texCoord0 = a_vertex.xy * u_texCoordXform.yw;"
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003534 " v_texCoord1 = a_vertex.xy * u_texCoordXform.yw + half2(u_texCoordXform.x, 0);"
3535 " v_texCoord2 = a_vertex.xy * u_texCoordXform.yw + half2(0, u_texCoordXform.z);"
brianosman33f6b3f2016-06-02 05:49:21 -07003536 " v_texCoord3 = a_vertex.xy * u_texCoordXform.yw + u_texCoordXform.xz;"
3537 );
3538 } else if (oddWidth) {
3539 vshaderTxt.append(
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003540 " v_texCoord0 = a_vertex.xy * half2(u_texCoordXform.y, 1);"
3541 " v_texCoord1 = a_vertex.xy * half2(u_texCoordXform.y, 1) + half2(u_texCoordXform.x, 0);"
brianosman33f6b3f2016-06-02 05:49:21 -07003542 );
3543 } else if (oddHeight) {
3544 vshaderTxt.append(
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003545 " v_texCoord0 = a_vertex.xy * half2(1, u_texCoordXform.w);"
3546 " v_texCoord1 = a_vertex.xy * half2(1, u_texCoordXform.w) + half2(0, u_texCoordXform.z);"
brianosman33f6b3f2016-06-02 05:49:21 -07003547 );
3548 } else {
3549 vshaderTxt.append(
3550 " v_texCoord0 = a_vertex.xy;"
3551 );
3552 }
3553
3554 vshaderTxt.append("}");
3555
3556 SkString fshaderTxt(version);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003557 if (shaderCaps->noperspectiveInterpolationSupport()) {
3558 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
brianosman33f6b3f2016-06-02 05:49:21 -07003559 fshaderTxt.appendf("#extension %s : require\n", extension);
3560 }
3561 }
brianosman33f6b3f2016-06-02 05:49:21 -07003562 for (int i = 0; i < numTaps; ++i) {
Brian Salomonf31ae492016-11-18 15:35:33 -05003563 vTexCoords[i].setTypeModifier(GrShaderVar::kIn_TypeModifier);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003564 vTexCoords[i].appendDecl(shaderCaps, &fshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003565 fshaderTxt.append(";");
3566 }
Brian Salomon1edc5b92016-11-29 13:43:46 -05003567 uTexture.appendDecl(shaderCaps, &fshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003568 fshaderTxt.append(";");
brianosman33f6b3f2016-06-02 05:49:21 -07003569 fshaderTxt.append(
3570 "// Mipmap Program FS\n"
3571 "void main() {"
3572 );
3573
3574 if (oddWidth && oddHeight) {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003575 fshaderTxt.append(
3576 " sk_FragColor = (texture(u_texture, v_texCoord0) + "
3577 " texture(u_texture, v_texCoord1) + "
3578 " texture(u_texture, v_texCoord2) + "
3579 " texture(u_texture, v_texCoord3)) * 0.25;"
brianosman33f6b3f2016-06-02 05:49:21 -07003580 );
3581 } else if (oddWidth || oddHeight) {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003582 fshaderTxt.append(
3583 " sk_FragColor = (texture(u_texture, v_texCoord0) + "
3584 " texture(u_texture, v_texCoord1)) * 0.5;"
brianosman33f6b3f2016-06-02 05:49:21 -07003585 );
3586 } else {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003587 fshaderTxt.append(
3588 " sk_FragColor = texture(u_texture, v_texCoord0);"
brianosman33f6b3f2016-06-02 05:49:21 -07003589 );
3590 }
3591
3592 fshaderTxt.append("}");
3593
Brian Osman6c431d52019-04-15 16:31:54 -04003594 SkSL::String sksl(vshaderTxt.c_str(), vshaderTxt.size());
Ethan Nicholas941e7e22016-12-12 15:33:30 -05003595 SkSL::Program::Settings settings;
3596 settings.fCaps = shaderCaps;
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003597 SkSL::String glsl;
Brian Osmanac9be9d2019-05-01 10:29:34 -04003598 std::unique_ptr<SkSL::Program> program = GrSkSLtoGLSL(*fGLContext, SkSL::Program::kVertex_Kind,
Brian Osman6c431d52019-04-15 16:31:54 -04003599 sksl, settings, &glsl);
brianosman33f6b3f2016-06-02 05:49:21 -07003600 GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fMipmapPrograms[progIdx].fProgram,
Brian Osmanac9be9d2019-05-01 10:29:34 -04003601 GR_GL_VERTEX_SHADER, glsl, &fStats);
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003602 SkASSERT(program->fInputs.isEmpty());
brianosman33f6b3f2016-06-02 05:49:21 -07003603
Brian Osman6c431d52019-04-15 16:31:54 -04003604 sksl.assign(fshaderTxt.c_str(), fshaderTxt.size());
Brian Osmanac9be9d2019-05-01 10:29:34 -04003605 program = GrSkSLtoGLSL(*fGLContext, SkSL::Program::kFragment_Kind, sksl, settings, &glsl);
brianosman33f6b3f2016-06-02 05:49:21 -07003606 GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fMipmapPrograms[progIdx].fProgram,
Brian Osmanac9be9d2019-05-01 10:29:34 -04003607 GR_GL_FRAGMENT_SHADER, glsl, &fStats);
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003608 SkASSERT(program->fInputs.isEmpty());
brianosman33f6b3f2016-06-02 05:49:21 -07003609
3610 GL_CALL(LinkProgram(fMipmapPrograms[progIdx].fProgram));
3611
3612 GL_CALL_RET(fMipmapPrograms[progIdx].fTextureUniform,
3613 GetUniformLocation(fMipmapPrograms[progIdx].fProgram, "u_texture"));
3614 GL_CALL_RET(fMipmapPrograms[progIdx].fTexCoordXformUniform,
3615 GetUniformLocation(fMipmapPrograms[progIdx].fProgram, "u_texCoordXform"));
3616
3617 GL_CALL(BindAttribLocation(fMipmapPrograms[progIdx].fProgram, 0, "a_vertex"));
3618
3619 GL_CALL(DeleteShader(vshader));
3620 GL_CALL(DeleteShader(fshader));
3621
3622 return true;
3623}
3624
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003625bool GrGLGpu::copySurfaceAsDraw(GrSurface* dst, GrSurfaceOrigin dstOrigin,
3626 GrSurface* src, GrSurfaceOrigin srcOrigin,
bsalomon6df86402015-06-01 10:41:49 -07003627 const SkIRect& srcRect,
3628 const SkIPoint& dstPoint) {
cdaltone2e71c22016-04-07 18:13:29 -07003629 GrGLTexture* srcTex = static_cast<GrGLTexture*>(src->asTexture());
Brian Salomonbf7b6202016-11-11 16:08:03 -05003630 int progIdx = TextureToCopyProgramIdx(srcTex);
cdaltone2e71c22016-04-07 18:13:29 -07003631
Greg Daniel26dbe3b2018-05-03 10:35:42 -04003632 if (!this->glCaps().canConfigBeFBOColorAttachment(dst->config())) {
3633 return false;
3634 }
3635
cdaltone2e71c22016-04-07 18:13:29 -07003636 if (!fCopyPrograms[progIdx].fProgram) {
Brian Salomonbf7b6202016-11-11 16:08:03 -05003637 if (!this->createCopyProgram(srcTex)) {
cdaltone2e71c22016-04-07 18:13:29 -07003638 SkDebugf("Failed to create copy program.\n");
3639 return false;
3640 }
3641 }
3642
bsalomon6df86402015-06-01 10:41:49 -07003643 int w = srcRect.width();
3644 int h = srcRect.height();
3645
Brian Salomon930f9392018-06-20 16:25:26 -04003646 this->bindTexture(0, GrSamplerState::ClampNearest(), srcTex);
bsalomon6df86402015-06-01 10:41:49 -07003647
bsalomon083617b2016-02-12 12:10:14 -08003648 GrGLIRect dstVP;
Brian Salomon71d9d842016-11-03 13:42:00 -04003649 this->bindSurfaceFBOForPixelOps(dst, GR_GL_FRAMEBUFFER, &dstVP, kDst_TempFBOTarget);
bsalomon083617b2016-02-12 12:10:14 -08003650 this->flushViewport(dstVP);
Robert Phillips294870f2016-11-11 12:38:40 -05003651 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon083617b2016-02-12 12:10:14 -08003652
bsalomon6df86402015-06-01 10:41:49 -07003653 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY, w, h);
bsalomon6df86402015-06-01 10:41:49 -07003654
Brian Salomon802cb312018-06-08 18:05:20 -04003655 this->flushProgram(fCopyPrograms[progIdx].fProgram);
bsalomon6df86402015-06-01 10:41:49 -07003656
cdaltone2e71c22016-04-07 18:13:29 -07003657 fHWVertexArrayState.setVertexArrayID(this, 0);
bsalomon6df86402015-06-01 10:41:49 -07003658
cdaltone2e71c22016-04-07 18:13:29 -07003659 GrGLAttribArrayState* attribs = fHWVertexArrayState.bindInternalVertexArray(this);
Chris Dalton8e45b4f2017-05-05 14:00:56 -04003660 attribs->enableVertexArrays(this, 1);
Brian Osmand4c29702018-09-14 16:16:55 -04003661 attribs->set(this, 0, fCopyProgramArrayBuffer.get(), kFloat2_GrVertexAttribType,
Brian Osman4a3f5c82018-09-18 16:16:38 -04003662 kFloat2_GrSLType, 2 * sizeof(GrGLfloat), 0);
bsalomon6df86402015-06-01 10:41:49 -07003663
3664 // dst rect edges in NDC (-1 to 1)
3665 int dw = dst->width();
3666 int dh = dst->height();
3667 GrGLfloat dx0 = 2.f * dstPoint.fX / dw - 1.f;
3668 GrGLfloat dx1 = 2.f * (dstPoint.fX + w) / dw - 1.f;
3669 GrGLfloat dy0 = 2.f * dstPoint.fY / dh - 1.f;
3670 GrGLfloat dy1 = 2.f * (dstPoint.fY + h) / dh - 1.f;
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003671 if (kBottomLeft_GrSurfaceOrigin == dstOrigin) {
bsalomon6df86402015-06-01 10:41:49 -07003672 dy0 = -dy0;
3673 dy1 = -dy1;
3674 }
3675
bsalomone5286e02016-01-14 09:24:09 -08003676 GrGLfloat sx0 = (GrGLfloat)srcRect.fLeft;
3677 GrGLfloat sx1 = (GrGLfloat)(srcRect.fLeft + w);
3678 GrGLfloat sy0 = (GrGLfloat)srcRect.fTop;
3679 GrGLfloat sy1 = (GrGLfloat)(srcRect.fTop + h);
Ethan Nicholas5338f992017-04-19 15:54:07 -04003680 int sw = src->width();
bsalomon6df86402015-06-01 10:41:49 -07003681 int sh = src->height();
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003682 if (kBottomLeft_GrSurfaceOrigin == srcOrigin) {
bsalomone5286e02016-01-14 09:24:09 -08003683 sy0 = sh - sy0;
3684 sy1 = sh - sy1;
3685 }
Brian Salomon246bc3d2018-12-06 15:33:02 -05003686 if (srcTex->texturePriv().textureType() != GrTextureType::kRectangle) {
3687 // src rect edges in normalized texture space (0 to 1)
3688 sx0 /= sw;
3689 sx1 /= sw;
3690 sy0 /= sh;
3691 sy1 /= sh;
3692 }
bsalomon6df86402015-06-01 10:41:49 -07003693
bsalomon7ea33f52015-11-22 14:51:00 -08003694 GL_CALL(Uniform4f(fCopyPrograms[progIdx].fPosXformUniform, dx1 - dx0, dy1 - dy0, dx0, dy0));
3695 GL_CALL(Uniform4f(fCopyPrograms[progIdx].fTexCoordXformUniform,
3696 sx1 - sx0, sy1 - sy0, sx0, sy0));
3697 GL_CALL(Uniform1i(fCopyPrograms[progIdx].fTextureUniform, 0));
bsalomon6df86402015-06-01 10:41:49 -07003698
3699 GrXferProcessor::BlendInfo blendInfo;
3700 blendInfo.reset();
bsalomon7f9b2e42016-01-12 13:29:26 -08003701 this->flushBlend(blendInfo, GrSwizzle::RGBA());
bsalomon6df86402015-06-01 10:41:49 -07003702 this->flushColorWrite(true);
Chris Dalton4c56b032019-03-04 12:28:42 -07003703 this->flushHWAAState(nullptr, false);
Brian Salomond818ebf2018-07-02 14:08:49 +00003704 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07003705 this->disableWindowRectangles();
csmartdaltonc7d85332016-10-26 10:13:46 -07003706 this->disableStencil();
Brian Osmanf02fa6f2017-07-11 11:17:47 -04003707 if (this->glCaps().srgbWriteControl()) {
3708 this->flushFramebufferSRGB(true);
3709 }
bsalomon6df86402015-06-01 10:41:49 -07003710
3711 GL_CALL(DrawArrays(GR_GL_TRIANGLE_STRIP, 0, 4));
Brian Salomon71d9d842016-11-03 13:42:00 -04003712 this->unbindTextureFBOForPixelOps(GR_GL_FRAMEBUFFER, dst);
Brian Salomon1fabd512018-02-09 09:54:25 -05003713 this->didWriteToSurface(dst, dstOrigin, &dstRect);
bsalomon083617b2016-02-12 12:10:14 -08003714
cdaltone2e71c22016-04-07 18:13:29 -07003715 return true;
bsalomon6df86402015-06-01 10:41:49 -07003716}
3717
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003718void GrGLGpu::copySurfaceAsCopyTexSubImage(GrSurface* dst, GrSurfaceOrigin dstOrigin,
3719 GrSurface* src, GrSurfaceOrigin srcOrigin,
bsalomon6df86402015-06-01 10:41:49 -07003720 const SkIRect& srcRect,
3721 const SkIPoint& dstPoint) {
Greg Daniel26dbe3b2018-05-03 10:35:42 -04003722 SkASSERT(can_copy_texsubimage(dst, dstOrigin, src, srcOrigin, this->glCaps()));
bsalomon6df86402015-06-01 10:41:49 -07003723 GrGLIRect srcVP;
Brian Salomon71d9d842016-11-03 13:42:00 -04003724 this->bindSurfaceFBOForPixelOps(src, GR_GL_FRAMEBUFFER, &srcVP, kSrc_TempFBOTarget);
bsalomon083617b2016-02-12 12:10:14 -08003725 GrGLTexture* dstTex = static_cast<GrGLTexture *>(dst->asTexture());
bsalomon6df86402015-06-01 10:41:49 -07003726 SkASSERT(dstTex);
3727 // We modified the bound FBO
Robert Phillips294870f2016-11-11 12:38:40 -05003728 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon6df86402015-06-01 10:41:49 -07003729 GrGLIRect srcGLRect;
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003730 srcGLRect.setRelativeTo(srcVP, srcRect, srcOrigin);
bsalomon6df86402015-06-01 10:41:49 -07003731
Brian Salomond978b902019-02-07 15:09:18 -05003732 this->bindTextureToScratchUnit(dstTex->target(), dstTex->textureID());
bsalomon6df86402015-06-01 10:41:49 -07003733 GrGLint dstY;
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003734 if (kBottomLeft_GrSurfaceOrigin == dstOrigin) {
bsalomon6df86402015-06-01 10:41:49 -07003735 dstY = dst->height() - (dstPoint.fY + srcGLRect.fHeight);
3736 } else {
3737 dstY = dstPoint.fY;
3738 }
bsalomon10528f12015-10-14 12:54:52 -07003739 GL_CALL(CopyTexSubImage2D(dstTex->target(), 0,
bsalomon083617b2016-02-12 12:10:14 -08003740 dstPoint.fX, dstY,
3741 srcGLRect.fLeft, srcGLRect.fBottom,
3742 srcGLRect.fWidth, srcGLRect.fHeight));
Brian Salomon71d9d842016-11-03 13:42:00 -04003743 this->unbindTextureFBOForPixelOps(GR_GL_FRAMEBUFFER, src);
bsalomon083617b2016-02-12 12:10:14 -08003744 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
3745 srcRect.width(), srcRect.height());
Brian Salomon1fabd512018-02-09 09:54:25 -05003746 this->didWriteToSurface(dst, dstOrigin, &dstRect);
bsalomon6df86402015-06-01 10:41:49 -07003747}
3748
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003749bool GrGLGpu::copySurfaceAsBlitFramebuffer(GrSurface* dst, GrSurfaceOrigin dstOrigin,
3750 GrSurface* src, GrSurfaceOrigin srcOrigin,
bsalomon6df86402015-06-01 10:41:49 -07003751 const SkIRect& srcRect,
3752 const SkIPoint& dstPoint) {
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003753 SkASSERT(can_blit_framebuffer_for_copy_surface(dst, dstOrigin, src, srcOrigin,
Greg Daniel26dbe3b2018-05-03 10:35:42 -04003754 srcRect, dstPoint, this->glCaps()));
bsalomon6df86402015-06-01 10:41:49 -07003755 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
3756 srcRect.width(), srcRect.height());
3757 if (dst == src) {
3758 if (SkIRect::IntersectsNoEmptyCheck(dstRect, srcRect)) {
3759 return false;
mtklein404b3b22015-05-18 09:29:10 -07003760 }
bsalomon5df6fee2015-05-18 06:26:15 -07003761 }
bsalomon6df86402015-06-01 10:41:49 -07003762
bsalomon6df86402015-06-01 10:41:49 -07003763 GrGLIRect dstVP;
3764 GrGLIRect srcVP;
Brian Salomon71d9d842016-11-03 13:42:00 -04003765 this->bindSurfaceFBOForPixelOps(dst, GR_GL_DRAW_FRAMEBUFFER, &dstVP, kDst_TempFBOTarget);
3766 this->bindSurfaceFBOForPixelOps(src, GR_GL_READ_FRAMEBUFFER, &srcVP, kSrc_TempFBOTarget);
bsalomon6df86402015-06-01 10:41:49 -07003767 // We modified the bound FBO
Robert Phillips294870f2016-11-11 12:38:40 -05003768 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon6df86402015-06-01 10:41:49 -07003769 GrGLIRect srcGLRect;
3770 GrGLIRect dstGLRect;
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003771 srcGLRect.setRelativeTo(srcVP, srcRect, srcOrigin);
3772 dstGLRect.setRelativeTo(dstVP, dstRect, dstOrigin);
bsalomon6df86402015-06-01 10:41:49 -07003773
3774 // BlitFrameBuffer respects the scissor, so disable it.
Brian Salomond818ebf2018-07-02 14:08:49 +00003775 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07003776 this->disableWindowRectangles();
bsalomon6df86402015-06-01 10:41:49 -07003777
3778 GrGLint srcY0;
3779 GrGLint srcY1;
3780 // Does the blit need to y-mirror or not?
Robert Phillipsb0e93a22017-08-29 08:26:54 -04003781 if (srcOrigin == dstOrigin) {
bsalomon6df86402015-06-01 10:41:49 -07003782 srcY0 = srcGLRect.fBottom;
3783 srcY1 = srcGLRect.fBottom + srcGLRect.fHeight;
3784 } else {
3785 srcY0 = srcGLRect.fBottom + srcGLRect.fHeight;
3786 srcY1 = srcGLRect.fBottom;
3787 }
3788 GL_CALL(BlitFramebuffer(srcGLRect.fLeft,
3789 srcY0,
3790 srcGLRect.fLeft + srcGLRect.fWidth,
3791 srcY1,
3792 dstGLRect.fLeft,
3793 dstGLRect.fBottom,
3794 dstGLRect.fLeft + dstGLRect.fWidth,
3795 dstGLRect.fBottom + dstGLRect.fHeight,
3796 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
Brian Salomon71d9d842016-11-03 13:42:00 -04003797 this->unbindTextureFBOForPixelOps(GR_GL_DRAW_FRAMEBUFFER, dst);
3798 this->unbindTextureFBOForPixelOps(GR_GL_READ_FRAMEBUFFER, src);
Brian Salomon1fabd512018-02-09 09:54:25 -05003799 this->didWriteToSurface(dst, dstOrigin, &dstRect);
bsalomon6df86402015-06-01 10:41:49 -07003800 return true;
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00003801}
3802
Brian Salomon930f9392018-06-20 16:25:26 -04003803bool GrGLGpu::onRegenerateMipMapLevels(GrTexture* texture) {
3804 auto glTex = static_cast<GrGLTexture*>(texture);
brianosman33f6b3f2016-06-02 05:49:21 -07003805 // Mipmaps are only supported on 2D textures:
Brian Salomon930f9392018-06-20 16:25:26 -04003806 if (GR_GL_TEXTURE_2D != glTex->target()) {
brianosman33f6b3f2016-06-02 05:49:21 -07003807 return false;
3808 }
3809
Brian Salomon930f9392018-06-20 16:25:26 -04003810 // Manual implementation of mipmap generation, to work around driver bugs w/sRGB.
3811 // Uses draw calls to do a series of downsample operations to successive mips.
3812
3813 // The manual approach requires the ability to limit which level we're sampling and that the
3814 // destination can be bound to a FBO:
3815 if (!this->glCaps().doManualMipmapping() ||
3816 !this->glCaps().canConfigBeFBOColorAttachment(texture->config())) {
3817 GrGLenum target = glTex->target();
Brian Salomond978b902019-02-07 15:09:18 -05003818 this->bindTextureToScratchUnit(target, glTex->textureID());
Brian Salomon930f9392018-06-20 16:25:26 -04003819 GL_CALL(GenerateMipmap(glTex->target()));
3820 return true;
brianosman33f6b3f2016-06-02 05:49:21 -07003821 }
3822
brianosman33f6b3f2016-06-02 05:49:21 -07003823 int width = texture->width();
3824 int height = texture->height();
3825 int levelCount = SkMipMap::ComputeLevelCount(width, height) + 1;
Greg Danielda86e282018-06-13 09:41:19 -04003826 SkASSERT(levelCount == texture->texturePriv().maxMipMapLevel() + 1);
brianosman33f6b3f2016-06-02 05:49:21 -07003827
3828 // Create (if necessary), then bind temporary FBO:
3829 if (0 == fTempDstFBOID) {
3830 GL_CALL(GenFramebuffers(1, &fTempDstFBOID));
3831 }
Adrienne Walker4ee88512018-05-17 11:37:14 -07003832 this->bindFramebuffer(GR_GL_FRAMEBUFFER, fTempDstFBOID);
Robert Phillips294870f2016-11-11 12:38:40 -05003833 fHWBoundRenderTargetUniqueID.makeInvalid();
brianosman33f6b3f2016-06-02 05:49:21 -07003834
3835 // Bind the texture, to get things configured for filtering.
3836 // We'll be changing our base level further below:
3837 this->setTextureUnit(0);
Brian Salomon930f9392018-06-20 16:25:26 -04003838 this->bindTexture(0, GrSamplerState::ClampBilerp(), glTex);
brianosman33f6b3f2016-06-02 05:49:21 -07003839
3840 // Vertex data:
3841 if (!fMipmapProgramArrayBuffer) {
3842 static const GrGLfloat vdata[] = {
3843 0, 0,
3844 0, 1,
3845 1, 0,
3846 1, 1
3847 };
Brian Salomonae64c192019-02-05 09:41:37 -05003848 fMipmapProgramArrayBuffer = GrGLBuffer::Make(this, sizeof(vdata), GrGpuBufferType::kVertex,
Brian Salomon12d22642019-01-29 14:38:50 -05003849 kStatic_GrAccessPattern, vdata);
brianosman33f6b3f2016-06-02 05:49:21 -07003850 }
3851 if (!fMipmapProgramArrayBuffer) {
3852 return false;
3853 }
3854
3855 fHWVertexArrayState.setVertexArrayID(this, 0);
3856
3857 GrGLAttribArrayState* attribs = fHWVertexArrayState.bindInternalVertexArray(this);
Chris Dalton8e45b4f2017-05-05 14:00:56 -04003858 attribs->enableVertexArrays(this, 1);
Brian Osmand4c29702018-09-14 16:16:55 -04003859 attribs->set(this, 0, fMipmapProgramArrayBuffer.get(), kFloat2_GrVertexAttribType,
Brian Osman4a3f5c82018-09-18 16:16:38 -04003860 kFloat2_GrSLType, 2 * sizeof(GrGLfloat), 0);
brianosman33f6b3f2016-06-02 05:49:21 -07003861
3862 // Set "simple" state once:
3863 GrXferProcessor::BlendInfo blendInfo;
3864 blendInfo.reset();
3865 this->flushBlend(blendInfo, GrSwizzle::RGBA());
3866 this->flushColorWrite(true);
Chris Dalton4c56b032019-03-04 12:28:42 -07003867 this->flushHWAAState(nullptr, false);
Brian Salomond818ebf2018-07-02 14:08:49 +00003868 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07003869 this->disableWindowRectangles();
csmartdaltonc7d85332016-10-26 10:13:46 -07003870 this->disableStencil();
brianosman33f6b3f2016-06-02 05:49:21 -07003871
3872 // Do all the blits:
3873 width = texture->width();
3874 height = texture->height();
3875 GrGLIRect viewport;
3876 viewport.fLeft = 0;
3877 viewport.fBottom = 0;
Brian Salomondc829942018-10-23 16:07:24 -04003878
brianosman33f6b3f2016-06-02 05:49:21 -07003879 for (GrGLint level = 1; level < levelCount; ++level) {
3880 // Get and bind the program for this particular downsample (filter shape can vary):
3881 int progIdx = TextureSizeToMipmapProgramIdx(width, height);
3882 if (!fMipmapPrograms[progIdx].fProgram) {
3883 if (!this->createMipmapProgram(progIdx)) {
3884 SkDebugf("Failed to create mipmap program.\n");
Brian Salomondc829942018-10-23 16:07:24 -04003885 // Invalidate all params to cover base level change in a previous iteration.
3886 glTex->textureParamsModified();
brianosman33f6b3f2016-06-02 05:49:21 -07003887 return false;
3888 }
3889 }
Brian Salomon802cb312018-06-08 18:05:20 -04003890 this->flushProgram(fMipmapPrograms[progIdx].fProgram);
brianosman33f6b3f2016-06-02 05:49:21 -07003891
3892 // Texcoord uniform is expected to contain (1/w, (w-1)/w, 1/h, (h-1)/h)
3893 const float invWidth = 1.0f / width;
3894 const float invHeight = 1.0f / height;
3895 GL_CALL(Uniform4f(fMipmapPrograms[progIdx].fTexCoordXformUniform,
3896 invWidth, (width - 1) * invWidth, invHeight, (height - 1) * invHeight));
3897 GL_CALL(Uniform1i(fMipmapPrograms[progIdx].fTextureUniform, 0));
3898
3899 // Only sample from previous mip
3900 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_BASE_LEVEL, level - 1));
3901
Brian Salomon930f9392018-06-20 16:25:26 -04003902 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0, GR_GL_TEXTURE_2D,
3903 glTex->textureID(), level));
brianosman33f6b3f2016-06-02 05:49:21 -07003904
3905 width = SkTMax(1, width / 2);
3906 height = SkTMax(1, height / 2);
3907 viewport.fWidth = width;
3908 viewport.fHeight = height;
3909 this->flushViewport(viewport);
3910
3911 GL_CALL(DrawArrays(GR_GL_TRIANGLE_STRIP, 0, 4));
3912 }
3913
3914 // Unbind:
3915 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
3916 GR_GL_TEXTURE_2D, 0, 0));
3917
Brian Salomon930f9392018-06-20 16:25:26 -04003918 // We modified the base level param.
Brian Salomondc829942018-10-23 16:07:24 -04003919 GrGLTexture::NonSamplerParams params = glTex->getCachedNonSamplerParams();
3920 params.fBaseMipMapLevel = levelCount - 2; // we drew the 2nd to last level into the last level.
3921 glTex->setCachedParams(nullptr, params, this->getResetTimestamp());
3922
brianosman33f6b3f2016-06-02 05:49:21 -07003923 return true;
3924}
3925
Chris Daltond7291ba2019-03-07 14:17:03 -07003926void GrGLGpu::querySampleLocations(
Chris Dalton8c4cafd2019-04-15 19:14:36 -06003927 GrRenderTarget* renderTarget, SkTArray<SkPoint>* sampleLocations) {
3928 this->flushRenderTargetNoColorWrites(static_cast<GrGLRenderTarget*>(renderTarget));
Chris Daltond7291ba2019-03-07 14:17:03 -07003929
3930 int effectiveSampleCnt;
3931 GR_GL_GetIntegerv(this->glInterface(), GR_GL_SAMPLES, &effectiveSampleCnt);
3932 SkASSERT(effectiveSampleCnt >= renderTarget->numStencilSamples());
3933
3934 sampleLocations->reset(effectiveSampleCnt);
3935 for (int i = 0; i < effectiveSampleCnt; ++i) {
3936 GL_CALL(GetMultisamplefv(GR_GL_SAMPLE_POSITION, i, &(*sampleLocations)[i].fX));
3937 }
3938}
3939
cdalton231c5fd2015-05-13 12:35:36 -07003940void GrGLGpu::xferBarrier(GrRenderTarget* rt, GrXferBarrierType type) {
bsalomoncb02b382015-08-12 11:14:50 -07003941 SkASSERT(type);
cdalton9954bc32015-04-29 14:17:00 -07003942 switch (type) {
cdalton231c5fd2015-05-13 12:35:36 -07003943 case kTexture_GrXferBarrierType: {
3944 GrGLRenderTarget* glrt = static_cast<GrGLRenderTarget*>(rt);
Brian Osmancfe83d12018-01-19 11:13:45 -05003945 SkASSERT(glrt->textureFBOID() != 0 && glrt->renderFBOID() != 0);
cdalton231c5fd2015-05-13 12:35:36 -07003946 if (glrt->textureFBOID() != glrt->renderFBOID()) {
3947 // The render target uses separate storage so no need for glTextureBarrier.
3948 // FIXME: The render target will resolve automatically when its texture is bound,
3949 // but we could resolve only the bounds that will be read if we do it here instead.
3950 return;
3951 }
cdalton9954bc32015-04-29 14:17:00 -07003952 SkASSERT(this->caps()->textureBarrierSupport());
3953 GL_CALL(TextureBarrier());
3954 return;
cdalton231c5fd2015-05-13 12:35:36 -07003955 }
cdalton8917d622015-05-06 13:40:21 -07003956 case kBlend_GrXferBarrierType:
bsalomon4b91f762015-05-19 09:29:46 -07003957 SkASSERT(GrCaps::kAdvanced_BlendEquationSupport ==
cdalton8917d622015-05-06 13:40:21 -07003958 this->caps()->blendEquationSupport());
3959 GL_CALL(BlendBarrier());
3960 return;
bsalomoncb02b382015-08-12 11:14:50 -07003961 default: break; // placate compiler warnings that kNone not handled
cdalton9954bc32015-04-29 14:17:00 -07003962 }
3963}
3964
Brian Salomonf865b052018-03-09 09:01:53 -05003965#if GR_TEST_UTILS
Brian Salomon52e943a2018-03-13 09:32:39 -04003966GrBackendTexture GrGLGpu::createTestingOnlyBackendTexture(const void* pixels, int w, int h,
Robert Phillips646f6372018-09-25 09:31:10 -04003967 GrColorType colorType, bool /*isRT*/,
3968 GrMipMapped mipMapped,
3969 size_t rowBytes) {
Brian Salomon8a375832018-03-14 10:21:40 -04003970 this->handleDirtyContext();
Robert Phillips646f6372018-09-25 09:31:10 -04003971
3972 GrPixelConfig config = GrColorTypeToPixelConfig(colorType, GrSRGBEncoded::kNo);
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003973 if (!this->caps()->isConfigTexturable(config)) {
3974 return GrBackendTexture(); // invalid
3975 }
3976
Greg Daniel92cbf3f2018-04-12 16:50:17 -04003977 if (w > this->caps()->maxTextureSize() || h > this->caps()->maxTextureSize()) {
3978 return GrBackendTexture(); // invalid
3979 }
3980
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003981 // Currently we don't support uploading pixel data when mipped.
3982 if (pixels && GrMipMapped::kYes == mipMapped) {
3983 return GrBackendTexture(); // invalid
3984 }
3985
Robert Phillips646f6372018-09-25 09:31:10 -04003986 int bpp = GrColorTypeBytesPerPixel(colorType);
3987 const size_t trimRowBytes = w * bpp;
3988 if (!rowBytes) {
3989 rowBytes = trimRowBytes;
3990 }
3991
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003992 GrGLTextureInfo info;
3993 info.fTarget = GR_GL_TEXTURE_2D;
3994 info.fID = 0;
3995 GL_CALL(GenTextures(1, &info.fID));
Brian Salomon1f05d452019-02-08 12:33:08 -05003996 this->bindTextureToScratchUnit(info.fTarget, info.fID);
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003997 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, 1));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003998 GL_CALL(TexParameteri(info.fTarget, GR_GL_TEXTURE_MAG_FILTER, GR_GL_NEAREST));
3999 GL_CALL(TexParameteri(info.fTarget, GR_GL_TEXTURE_MIN_FILTER, GR_GL_NEAREST));
4000 GL_CALL(TexParameteri(info.fTarget, GR_GL_TEXTURE_WRAP_S, GR_GL_CLAMP_TO_EDGE));
4001 GL_CALL(TexParameteri(info.fTarget, GR_GL_TEXTURE_WRAP_T, GR_GL_CLAMP_TO_EDGE));
4002
Jim Van Verth1676cb92019-01-15 13:24:45 -05004003 // we have to do something special for compressed textures
4004 if (GrPixelConfigIsCompressed(config)) {
4005 GrGLenum internalFormat;
4006 const GrGLInterface* interface = this->glInterface();
4007 const GrGLCaps& caps = this->glCaps();
4008 if (!caps.getCompressedTexImageFormats(config, &internalFormat)) {
4009 return GrBackendTexture();
Robert Phillips646f6372018-09-25 09:31:10 -04004010 }
Robert Phillipsd21b2a52017-12-12 13:01:25 -05004011
Jim Van Verth1676cb92019-01-15 13:24:45 -05004012 GrMipLevel mipLevel = { pixels, rowBytes };
4013 if (!allocate_and_populate_compressed_texture(config, *interface, caps, info.fTarget,
4014 internalFormat, &mipLevel, 1,
4015 w, h)) {
4016 return GrBackendTexture();
4017 }
4018 } else {
4019 bool restoreGLRowLength = false;
4020 if (trimRowBytes != rowBytes && this->glCaps().unpackRowLengthSupport()) {
4021 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowBytes / bpp));
4022 restoreGLRowLength = true;
4023 }
4024
4025 GrGLenum internalFormat;
4026 GrGLenum externalFormat;
4027 GrGLenum externalType;
4028
4029 if (!this->glCaps().getTexImageFormats(config, config, &internalFormat, &externalFormat,
4030 &externalType)) {
4031 return GrBackendTexture(); // invalid
4032 }
4033
4034 info.fFormat = this->glCaps().configSizedInternalFormat(config);
4035
4036 this->unbindCpuToGpuXferBuffer();
4037
4038 // Figure out the number of mip levels.
4039 int mipLevels = 1;
4040 if (GrMipMapped::kYes == mipMapped) {
4041 mipLevels = SkMipMap::ComputeLevelCount(w, h) + 1;
4042 }
4043
4044 size_t baseLayerSize = bpp * w * h;
4045 SkAutoMalloc defaultStorage(baseLayerSize);
4046 if (!pixels) {
4047 // Fill in the texture with all zeros so we don't have random garbage
4048 pixels = defaultStorage.get();
4049 memset(defaultStorage.get(), 0, baseLayerSize);
4050 } else if (trimRowBytes != rowBytes && !restoreGLRowLength) {
4051 // We weren't able to use GR_GL_UNPACK_ROW_LENGTH so make a copy
4052 char* copy = (char*)defaultStorage.get();
4053 for (int y = 0; y < h; ++y) {
4054 memcpy(&copy[y*trimRowBytes], &((const char*)pixels)[y*rowBytes], trimRowBytes);
4055 }
4056 pixels = copy;
4057 }
4058
4059 int width = w;
4060 int height = h;
4061 for (int i = 0; i < mipLevels; ++i) {
4062 GL_CALL(TexImage2D(info.fTarget, i, internalFormat, width, height, 0, externalFormat,
4063 externalType, pixels));
4064 width = SkTMax(1, width / 2);
4065 height = SkTMax(1, height / 2);
4066 }
4067 if (restoreGLRowLength) {
4068 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
4069 }
Robert Phillipsd21b2a52017-12-12 13:01:25 -05004070 }
4071
Robert Phillipse8fabb22018-02-04 14:33:21 -05004072 // unbind the texture from the texture unit to avoid asserts
4073 GL_CALL(BindTexture(info.fTarget, 0));
4074
Greg Daniel108bb232018-07-03 16:18:29 -04004075 GrBackendTexture beTex = GrBackendTexture(w, h, mipMapped, info);
4076 // Lots of tests don't go through Skia's public interface which will set the config so for
4077 // testing we make sure we set a config here.
4078 beTex.setPixelConfig(config);
4079 return beTex;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05004080}
4081
4082bool GrGLGpu::isTestingOnlyBackendTexture(const GrBackendTexture& tex) const {
Greg Danielbdf12ad2018-10-12 09:31:11 -04004083 SkASSERT(GrBackendApi::kOpenGL == tex.backend());
Robert Phillipsd21b2a52017-12-12 13:01:25 -05004084
Greg Daniel52e16d92018-04-10 09:34:07 -04004085 GrGLTextureInfo info;
4086 if (!tex.getGLTextureInfo(&info)) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -05004087 return false;
4088 }
4089
4090 GrGLboolean result;
Greg Daniel52e16d92018-04-10 09:34:07 -04004091 GL_CALL_RET(result, IsTexture(info.fID));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05004092
4093 return (GR_GL_TRUE == result);
4094}
4095
Brian Salomon26102cb2018-03-09 09:33:19 -05004096void GrGLGpu::deleteTestingOnlyBackendTexture(const GrBackendTexture& tex) {
Greg Danielbdf12ad2018-10-12 09:31:11 -04004097 SkASSERT(GrBackendApi::kOpenGL == tex.backend());
Robert Phillipsd21b2a52017-12-12 13:01:25 -05004098
Greg Daniel52e16d92018-04-10 09:34:07 -04004099 GrGLTextureInfo info;
4100 if (tex.getGLTextureInfo(&info)) {
4101 GL_CALL(DeleteTextures(1, &info.fID));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05004102 }
4103}
4104
Brian Salomonf865b052018-03-09 09:01:53 -05004105GrBackendRenderTarget GrGLGpu::createTestingOnlyBackendRenderTarget(int w, int h,
Brian Osman2d010b62018-08-09 10:55:09 -04004106 GrColorType colorType) {
Greg Daniel92cbf3f2018-04-12 16:50:17 -04004107 if (w > this->caps()->maxRenderTargetSize() || h > this->caps()->maxRenderTargetSize()) {
4108 return GrBackendRenderTarget(); // invalid
4109 }
Brian Salomon8a375832018-03-14 10:21:40 -04004110 this->handleDirtyContext();
Brian Osman2d010b62018-08-09 10:55:09 -04004111 auto config = GrColorTypeToPixelConfig(colorType, GrSRGBEncoded::kNo);
Brian Salomon93348dd2018-08-29 12:56:23 -04004112 if (!this->glCaps().isConfigRenderable(config)) {
Brian Salomonf865b052018-03-09 09:01:53 -05004113 return {};
4114 }
Brian Salomon93348dd2018-08-29 12:56:23 -04004115 bool useTexture = false;
4116 GrGLenum colorBufferFormat;
4117 GrGLenum externalFormat = 0, externalType = 0;
4118 if (config == kBGRA_8888_GrPixelConfig && this->glCaps().bgraIsInternalFormat()) {
4119 // BGRA render buffers are not supported.
4120 this->glCaps().getTexImageFormats(config, config, &colorBufferFormat, &externalFormat,
4121 &externalType);
4122 useTexture = true;
4123 } else {
4124 this->glCaps().getRenderbufferFormat(config, &colorBufferFormat);
4125 }
Brian Salomonf865b052018-03-09 09:01:53 -05004126 int sFormatIdx = this->getCompatibleStencilIndex(config);
4127 if (sFormatIdx < 0) {
4128 return {};
4129 }
Brian Salomon93348dd2018-08-29 12:56:23 -04004130 GrGLuint colorID = 0;
4131 GrGLuint stencilID = 0;
4132 auto deleteIDs = [&] {
4133 if (colorID) {
4134 if (useTexture) {
4135 GL_CALL(DeleteTextures(1, &colorID));
4136 } else {
4137 GL_CALL(DeleteRenderbuffers(1, &colorID));
4138 }
Brian Salomonf865b052018-03-09 09:01:53 -05004139 }
Brian Salomon93348dd2018-08-29 12:56:23 -04004140 if (stencilID) {
4141 GL_CALL(DeleteRenderbuffers(1, &stencilID));
Brian Salomonf865b052018-03-09 09:01:53 -05004142 }
Brian Salomon93348dd2018-08-29 12:56:23 -04004143 };
4144
4145 if (useTexture) {
4146 GL_CALL(GenTextures(1, &colorID));
4147 } else {
4148 GL_CALL(GenRenderbuffers(1, &colorID));
4149 }
4150 GL_CALL(GenRenderbuffers(1, &stencilID));
4151 if (!stencilID || !colorID) {
4152 deleteIDs();
Brian Salomonf865b052018-03-09 09:01:53 -05004153 return {};
4154 }
Brian Salomon93348dd2018-08-29 12:56:23 -04004155
Brian Salomonf865b052018-03-09 09:01:53 -05004156 GrGLFramebufferInfo info;
4157 info.fFBOID = 0;
Brian Salomon93348dd2018-08-29 12:56:23 -04004158 this->glCaps().getSizedInternalFormat(config, &info.fFormat);
Brian Salomonf865b052018-03-09 09:01:53 -05004159 GL_CALL(GenFramebuffers(1, &info.fFBOID));
4160 if (!info.fFBOID) {
Brian Salomon93348dd2018-08-29 12:56:23 -04004161 deleteIDs();
4162 return {};
Brian Salomonf865b052018-03-09 09:01:53 -05004163 }
4164
4165 this->invalidateBoundRenderTarget();
4166
Adrienne Walker4ee88512018-05-17 11:37:14 -07004167 this->bindFramebuffer(GR_GL_FRAMEBUFFER, info.fFBOID);
Brian Salomon93348dd2018-08-29 12:56:23 -04004168 if (useTexture) {
Brian Salomond978b902019-02-07 15:09:18 -05004169 this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, colorID);
Brian Salomon93348dd2018-08-29 12:56:23 -04004170 GL_CALL(TexImage2D(GR_GL_TEXTURE_2D, 0, colorBufferFormat, w, h, 0, externalFormat,
4171 externalType, nullptr));
4172 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0, GR_GL_TEXTURE_2D,
4173 colorID, 0));
4174 } else {
4175 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, colorID));
4176 GL_ALLOC_CALL(this->glInterface(),
4177 RenderbufferStorage(GR_GL_RENDERBUFFER, colorBufferFormat, w, h));
4178 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
4179 GR_GL_RENDERBUFFER, colorID));
4180 }
4181 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, stencilID));
Brian Salomonf865b052018-03-09 09:01:53 -05004182 auto stencilBufferFormat = this->glCaps().stencilFormats()[sFormatIdx].fInternalFormat;
4183 GL_ALLOC_CALL(this->glInterface(),
4184 RenderbufferStorage(GR_GL_RENDERBUFFER, stencilBufferFormat, w, h));
4185 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT, GR_GL_RENDERBUFFER,
Brian Salomon93348dd2018-08-29 12:56:23 -04004186 stencilID));
Brian Salomonf865b052018-03-09 09:01:53 -05004187 if (this->glCaps().stencilFormats()[sFormatIdx].fPacked) {
4188 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_DEPTH_ATTACHMENT,
Brian Salomon93348dd2018-08-29 12:56:23 -04004189 GR_GL_RENDERBUFFER, stencilID));
Brian Salomonf865b052018-03-09 09:01:53 -05004190 }
4191
Brian Salomon93348dd2018-08-29 12:56:23 -04004192 // We don't want to have to recover the renderbuffer/texture IDs later to delete them. OpenGL
4193 // has this rule that if a renderbuffer/texture is deleted and a FBO other than the current FBO
4194 // has the RB attached then deletion is delayed. So we unbind the FBO here and delete the
4195 // renderbuffers/texture.
Adrienne Walker4ee88512018-05-17 11:37:14 -07004196 this->bindFramebuffer(GR_GL_FRAMEBUFFER, 0);
Brian Salomon93348dd2018-08-29 12:56:23 -04004197 deleteIDs();
Brian Salomonf865b052018-03-09 09:01:53 -05004198
Adrienne Walker4ee88512018-05-17 11:37:14 -07004199 this->bindFramebuffer(GR_GL_FRAMEBUFFER, info.fFBOID);
Brian Salomonf865b052018-03-09 09:01:53 -05004200 GrGLenum status;
4201 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
4202 if (GR_GL_FRAMEBUFFER_COMPLETE != status) {
Adrienne Walker4ee88512018-05-17 11:37:14 -07004203 this->deleteFramebuffer(info.fFBOID);
Brian Salomonf865b052018-03-09 09:01:53 -05004204 return {};
4205 }
4206 auto stencilBits = SkToInt(this->glCaps().stencilFormats()[sFormatIdx].fStencilBits);
Greg Daniel108bb232018-07-03 16:18:29 -04004207 GrBackendRenderTarget beRT = GrBackendRenderTarget(w, h, 1, stencilBits, info);
4208 // Lots of tests don't go through Skia's public interface which will set the config so for
4209 // testing we make sure we set a config here.
4210 beRT.setPixelConfig(config);
Brian Salomon93348dd2018-08-29 12:56:23 -04004211#ifdef SK_DEBUG
4212 SkColorType skColorType = GrColorTypeToSkColorType(colorType);
4213 if (skColorType != kUnknown_SkColorType) {
4214 SkASSERT(this->caps()->validateBackendRenderTarget(
Brian Salomonf391d0f2018-12-14 09:18:50 -05004215 beRT, GrColorTypeToSkColorType(colorType)) != kUnknown_GrPixelConfig);
Brian Salomon93348dd2018-08-29 12:56:23 -04004216 }
4217#endif
Greg Daniel108bb232018-07-03 16:18:29 -04004218 return beRT;
Brian Salomonf865b052018-03-09 09:01:53 -05004219}
4220
4221void GrGLGpu::deleteTestingOnlyBackendRenderTarget(const GrBackendRenderTarget& backendRT) {
Greg Danielbdf12ad2018-10-12 09:31:11 -04004222 SkASSERT(GrBackendApi::kOpenGL == backendRT.backend());
Greg Daniel323fbcf2018-04-10 13:46:30 -04004223 GrGLFramebufferInfo info;
4224 if (backendRT.getGLFramebufferInfo(&info)) {
4225 if (info.fFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -07004226 this->deleteFramebuffer(info.fFBOID);
Brian Salomonf865b052018-03-09 09:01:53 -05004227 }
4228 }
joshualitt8fd844f2015-12-02 13:36:47 -08004229}
4230
Greg Daniel26b50a42018-03-08 09:49:58 -05004231void GrGLGpu::testingOnly_flushGpuAndSync() {
4232 GL_CALL(Finish());
4233}
Brian Salomonf865b052018-03-09 09:01:53 -05004234#endif
Greg Daniel26b50a42018-03-08 09:49:58 -05004235
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00004236///////////////////////////////////////////////////////////////////////////////
bsalomon6df86402015-06-01 10:41:49 -07004237
cdaltone2e71c22016-04-07 18:13:29 -07004238GrGLAttribArrayState* GrGLGpu::HWVertexArrayState::bindInternalVertexArray(GrGLGpu* gpu,
csmartdalton485a1202016-07-13 10:16:32 -07004239 const GrBuffer* ibuf) {
robertphillips@google.com4f65a272013-03-26 19:40:46 +00004240 GrGLAttribArrayState* attribState;
4241
cdaltone2e71c22016-04-07 18:13:29 -07004242 if (gpu->glCaps().isCoreProfile()) {
4243 if (!fCoreProfileVertexArray) {
bsalomon@google.com6918d482013-03-07 19:09:11 +00004244 GrGLuint arrayID;
4245 GR_GL_CALL(gpu->glInterface(), GenVertexArrays(1, &arrayID));
4246 int attrCount = gpu->glCaps().maxVertexAttributes();
cdaltone2e71c22016-04-07 18:13:29 -07004247 fCoreProfileVertexArray = new GrGLVertexArray(arrayID, attrCount);
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00004248 }
cdaltone2e71c22016-04-07 18:13:29 -07004249 if (ibuf) {
4250 attribState = fCoreProfileVertexArray->bindWithIndexBuffer(gpu, ibuf);
bsalomon6df86402015-06-01 10:41:49 -07004251 } else {
cdaltone2e71c22016-04-07 18:13:29 -07004252 attribState = fCoreProfileVertexArray->bind(gpu);
bsalomon6df86402015-06-01 10:41:49 -07004253 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00004254 } else {
cdaltone2e71c22016-04-07 18:13:29 -07004255 if (ibuf) {
4256 // bindBuffer implicitly binds VAO 0 when binding an index buffer.
Brian Salomonae64c192019-02-05 09:41:37 -05004257 gpu->bindBuffer(GrGpuBufferType::kIndex, ibuf);
bsalomon@google.com6918d482013-03-07 19:09:11 +00004258 } else {
4259 this->setVertexArrayID(gpu, 0);
4260 }
4261 int attrCount = gpu->glCaps().maxVertexAttributes();
4262 if (fDefaultVertexArrayAttribState.count() != attrCount) {
4263 fDefaultVertexArrayAttribState.resize(attrCount);
4264 }
4265 attribState = &fDefaultVertexArrayAttribState;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00004266 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00004267 return attribState;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00004268}
bsalomone179a912016-01-20 06:18:10 -08004269
Greg Danielbae71212019-03-01 15:24:35 -05004270void GrGLGpu::onFinishFlush(GrSurfaceProxy*, SkSurface::BackendSurfaceAccess access,
Greg Daniele6bfb7d2019-04-17 15:26:11 -04004271 const GrFlushInfo& info) {
Greg Daniel51316782017-08-02 15:10:09 +00004272 // If we inserted semaphores during the flush, we need to call GLFlush.
Greg Daniele6bfb7d2019-04-17 15:26:11 -04004273 bool insertedSemaphore = info.fNumSemaphores > 0 && this->caps()->fenceSyncSupport();
Greg Daniel51316782017-08-02 15:10:09 +00004274 if (insertedSemaphore) {
4275 GL_CALL(Flush());
4276 }
Greg Daniele6bfb7d2019-04-17 15:26:11 -04004277 if (info.fFlags & kSyncCpu_GrFlushFlag) {
Greg Danielbae71212019-03-01 15:24:35 -05004278 GL_CALL(Finish());
4279 }
Greg Daniela3aa75a2019-04-12 14:24:55 -04004280 // TODO: We should have GL actually wait until the GPU has finished work on the GPU.
Greg Daniele6bfb7d2019-04-17 15:26:11 -04004281 if (info.fFinishedProc) {
4282 info.fFinishedProc(info.fFinishedContext);
Greg Daniela3aa75a2019-04-12 14:24:55 -04004283 }
Greg Daniel51316782017-08-02 15:10:09 +00004284}
4285
Robert Phillips5b5d84c2018-08-09 15:12:18 -04004286void GrGLGpu::submit(GrGpuCommandBuffer* buffer) {
4287 if (buffer->asRTCommandBuffer()) {
4288 SkASSERT(fCachedRTCommandBuffer.get() == buffer);
4289 fCachedRTCommandBuffer->reset();
4290 } else {
4291 SkASSERT(fCachedTexCommandBuffer.get() == buffer);
4292 fCachedTexCommandBuffer->reset();
4293 }
4294}
4295
Greg Daniel6be35232017-03-01 17:01:09 -05004296GrFence SK_WARN_UNUSED_RESULT GrGLGpu::insertFence() {
Greg Danielc64ee462017-06-15 16:59:49 -04004297 SkASSERT(this->caps()->fenceSyncSupport());
Greg Daniel6be35232017-03-01 17:01:09 -05004298 GrGLsync sync;
4299 GL_CALL_RET(sync, FenceSync(GR_GL_SYNC_GPU_COMMANDS_COMPLETE, 0));
4300 GR_STATIC_ASSERT(sizeof(GrFence) >= sizeof(GrGLsync));
4301 return (GrFence)sync;
jvanverth84741b32016-09-30 08:39:02 -07004302}
4303
Greg Daniel6be35232017-03-01 17:01:09 -05004304bool GrGLGpu::waitFence(GrFence fence, uint64_t timeout) {
jvanverth84741b32016-09-30 08:39:02 -07004305 GrGLenum result;
4306 GL_CALL_RET(result, ClientWaitSync((GrGLsync)fence, GR_GL_SYNC_FLUSH_COMMANDS_BIT, timeout));
4307 return (GR_GL_CONDITION_SATISFIED == result);
4308}
4309
4310void GrGLGpu::deleteFence(GrFence fence) const {
Greg Daniel6be35232017-03-01 17:01:09 -05004311 this->deleteSync((GrGLsync)fence);
4312}
4313
Greg Daniela5cb7812017-06-16 09:45:32 -04004314sk_sp<GrSemaphore> SK_WARN_UNUSED_RESULT GrGLGpu::makeSemaphore(bool isOwned) {
Greg Danielc64ee462017-06-15 16:59:49 -04004315 SkASSERT(this->caps()->fenceSyncSupport());
Greg Daniela5cb7812017-06-16 09:45:32 -04004316 return GrGLSemaphore::Make(this, isOwned);
Greg Daniel6be35232017-03-01 17:01:09 -05004317}
4318
Greg Daniel48661b82018-01-22 16:11:35 -05004319sk_sp<GrSemaphore> GrGLGpu::wrapBackendSemaphore(const GrBackendSemaphore& semaphore,
4320 GrResourceProvider::SemaphoreWrapType wrapType,
4321 GrWrapOwnership ownership) {
Greg Danielc64ee462017-06-15 16:59:49 -04004322 SkASSERT(this->caps()->fenceSyncSupport());
Greg Daniela5cb7812017-06-16 09:45:32 -04004323 return GrGLSemaphore::MakeWrapped(this, semaphore.glSync(), ownership);
4324}
4325
Greg Daniel858e12c2018-12-06 11:11:37 -05004326void GrGLGpu::insertSemaphore(sk_sp<GrSemaphore> semaphore) {
Greg Daniel6be35232017-03-01 17:01:09 -05004327 GrGLSemaphore* glSem = static_cast<GrGLSemaphore*>(semaphore.get());
4328
Greg Daniel48661b82018-01-22 16:11:35 -05004329 GrGLsync sync;
4330 GL_CALL_RET(sync, FenceSync(GR_GL_SYNC_GPU_COMMANDS_COMPLETE, 0));
4331 glSem->setSync(sync);
Greg Daniel6be35232017-03-01 17:01:09 -05004332}
4333
Greg Daniel48661b82018-01-22 16:11:35 -05004334void GrGLGpu::waitSemaphore(sk_sp<GrSemaphore> semaphore) {
Greg Daniel6be35232017-03-01 17:01:09 -05004335 GrGLSemaphore* glSem = static_cast<GrGLSemaphore*>(semaphore.get());
4336
4337 GL_CALL(WaitSync(glSem->sync(), 0, GR_GL_TIMEOUT_IGNORED));
4338}
4339
4340void GrGLGpu::deleteSync(GrGLsync sync) const {
4341 GL_CALL(DeleteSync(sync));
jvanverth84741b32016-09-30 08:39:02 -07004342}
Brian Osman13dddce2017-05-09 13:19:50 -04004343
Robert Phillips65a88fa2017-08-08 08:36:22 -04004344void GrGLGpu::insertEventMarker(const char* msg) {
4345 GL_CALL(InsertEventMarker(strlen(msg), msg));
4346}
4347
Brian Osman13dddce2017-05-09 13:19:50 -04004348sk_sp<GrSemaphore> GrGLGpu::prepareTextureForCrossContextUsage(GrTexture* texture) {
4349 // Set up a semaphore to be signaled once the data is ready, and flush GL
Greg Daniela5cb7812017-06-16 09:45:32 -04004350 sk_sp<GrSemaphore> semaphore = this->makeSemaphore(true);
Greg Daniel858e12c2018-12-06 11:11:37 -05004351 this->insertSemaphore(semaphore);
4352 // We must call flush here to make sure the GrGLSync object gets created and sent to the gpu.
4353 GL_CALL(Flush());
Brian Osman13dddce2017-05-09 13:19:50 -04004354
4355 return semaphore;
4356}
Robert Phillips646e4292017-06-13 12:44:56 -04004357
4358int GrGLGpu::TextureToCopyProgramIdx(GrTexture* texture) {
Brian Salomon60dd8c72018-07-30 10:24:13 -04004359 switch (GrSLCombinedSamplerTypeForTextureType(texture->texturePriv().textureType())) {
Robert Phillips646e4292017-06-13 12:44:56 -04004360 case kTexture2DSampler_GrSLType:
4361 return 0;
Robert Phillips646e4292017-06-13 12:44:56 -04004362 case kTexture2DRectSampler_GrSLType:
Brian Salomon57111332018-02-05 15:55:54 -05004363 return 1;
Robert Phillips646e4292017-06-13 12:44:56 -04004364 case kTextureExternalSampler_GrSLType:
Brian Salomon57111332018-02-05 15:55:54 -05004365 return 2;
Robert Phillips646e4292017-06-13 12:44:56 -04004366 default:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -04004367 SK_ABORT("Unexpected samper type");
Robert Phillips646e4292017-06-13 12:44:56 -04004368 return 0;
4369 }
4370}
Brian Osman71a18892017-08-10 10:23:25 -04004371
Kevin Lubickf4def342018-10-04 12:52:50 -04004372#ifdef SK_ENABLE_DUMP_GPU
Mike Kleinc0bd9f92019-04-23 12:05:21 -05004373#include "src/utils/SkJSONWriter.h"
Brian Osman71a18892017-08-10 10:23:25 -04004374void GrGLGpu::onDumpJSON(SkJSONWriter* writer) const {
4375 // We are called by the base class, which has already called beginObject(). We choose to nest
4376 // all of our caps information in a named sub-object.
4377 writer->beginObject("GL GPU");
4378
4379 const GrGLubyte* str;
4380 GL_CALL_RET(str, GetString(GR_GL_VERSION));
4381 writer->appendString("GL_VERSION", (const char*)(str));
4382 GL_CALL_RET(str, GetString(GR_GL_RENDERER));
4383 writer->appendString("GL_RENDERER", (const char*)(str));
4384 GL_CALL_RET(str, GetString(GR_GL_VENDOR));
4385 writer->appendString("GL_VENDOR", (const char*)(str));
4386 GL_CALL_RET(str, GetString(GR_GL_SHADING_LANGUAGE_VERSION));
4387 writer->appendString("GL_SHADING_LANGUAGE_VERSION", (const char*)(str));
4388
4389 writer->appendName("extensions");
4390 glInterface()->fExtensions.dumpJSON(writer);
4391
4392 writer->endObject();
4393}
Kevin Lubickf4def342018-10-04 12:52:50 -04004394#endif