blob: a73f97c97f405c2f11665eb8cfd639073b11780f [file] [log] [blame]
reed@google.comac10a2d2010-12-22 21:39:39 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
reed@google.comac10a2d2010-12-22 21:39:39 +00006 */
7
jvanverth39edf762014-12-22 11:44:19 -08008#include "GrGLGpu.h"
Hal Canary95e3c052017-01-11 12:44:43 -05009
Brian Salomon028a9a52017-05-11 11:39:08 -040010#include <cmath>
Hal Canary95e3c052017-01-11 12:44:43 -050011#include "../private/GrGLSL.h"
Greg Daniela5cb7812017-06-16 09:45:32 -040012#include "GrBackendSemaphore.h"
Greg Daniel7ef28f32017-04-20 16:41:55 +000013#include "GrBackendSurface.h"
Hal Canary95e3c052017-01-11 12:44:43 -050014#include "GrFixedClip.h"
cdalton397536c2016-03-25 12:15:03 -070015#include "GrGLBuffer.h"
egdaniel066df7c2016-06-08 14:02:27 -070016#include "GrGLGpuCommandBuffer.h"
Greg Daniel6be35232017-03-01 17:01:09 -050017#include "GrGLSemaphore.h"
egdaniel8dc7c3a2015-04-16 11:22:42 -070018#include "GrGLStencilAttachment.h"
bsalomon37dd3312014-11-03 08:47:23 -080019#include "GrGLTextureRenderTarget.h"
bsalomon3582d3e2015-02-13 14:20:05 -080020#include "GrGpuResourcePriv.h"
egdaniel0e1853c2016-03-17 11:35:45 -070021#include "GrMesh.h"
Hal Canary95e3c052017-01-11 12:44:43 -050022#include "GrPipeline.h"
bsalomon6bc1b5f2015-02-23 09:06:38 -080023#include "GrRenderTargetPriv.h"
Brian Salomon94efbf52016-11-29 13:43:05 -050024#include "GrShaderCaps.h"
bsalomonafbf2d62014-09-30 12:18:44 -070025#include "GrSurfacePriv.h"
Robert Phillips3798c862017-03-27 11:08:16 -040026#include "GrSurfaceProxyPriv.h"
bsalomonafbf2d62014-09-30 12:18:44 -070027#include "GrTexturePriv.h"
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +000028#include "GrTypes.h"
Hal Canary95e3c052017-01-11 12:44:43 -050029#include "SkAutoMalloc.h"
Kevin Lubickc456b732017-01-11 17:21:57 +000030#include "SkMakeUnique.h"
31#include "SkMipMap.h"
32#include "SkPixmap.h"
Kevin Lubickc456b732017-01-11 17:21:57 +000033#include "SkSLCompiler.h"
Hal Canary95e3c052017-01-11 12:44:43 -050034#include "SkStrokeRec.h"
Kevin Lubickc456b732017-01-11 17:21:57 +000035#include "SkTemplates.h"
Ryan Macnak38a10ad2017-07-10 10:36:34 -070036#include "SkTraceEvent.h"
Kevin Lubickc456b732017-01-11 17:21:57 +000037#include "SkTypes.h"
Hal Canary95e3c052017-01-11 12:44:43 -050038#include "builders/GrGLShaderStringBuilder.h"
Hal Canary95e3c052017-01-11 12:44:43 -050039#include "instanced/GLInstancedRendering.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000040
bsalomon@google.com0b77d682011-08-19 13:28:54 +000041#define GL_CALL(X) GR_GL_CALL(this->glInterface(), X)
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +000042#define GL_CALL_RET(RET, X) GR_GL_CALL_RET(this->glInterface(), RET, X)
bsalomon@google.com0b77d682011-08-19 13:28:54 +000043
reed@google.comac10a2d2010-12-22 21:39:39 +000044#define SKIP_CACHE_CHECK true
45
bsalomon@google.com4f3c2532012-01-19 16:16:52 +000046#if GR_GL_CHECK_ALLOC_WITH_GET_ERROR
47 #define CLEAR_ERROR_BEFORE_ALLOC(iface) GrGLClearErr(iface)
48 #define GL_ALLOC_CALL(iface, call) GR_GL_CALL_NOERRCHECK(iface, call)
49 #define CHECK_ALLOC_ERROR(iface) GR_GL_GET_ERROR(iface)
rmistry@google.comfbfcd562012-08-23 18:09:54 +000050#else
bsalomon@google.com4f3c2532012-01-19 16:16:52 +000051 #define CLEAR_ERROR_BEFORE_ALLOC(iface)
52 #define GL_ALLOC_CALL(iface, call) GR_GL_CALL(iface, call)
53 #define CHECK_ALLOC_ERROR(iface) GR_GL_NO_ERROR
54#endif
55
Jim Van Verth32ac83e2016-11-28 15:23:57 -050056//#define USE_NSIGHT
57
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +000058///////////////////////////////////////////////////////////////////////////////
59
csmartdaltona7f29642016-07-07 08:49:11 -070060using gr_instanced::InstancedRendering;
61using gr_instanced::GLInstancedRendering;
cdaltonb85a0aa2014-07-21 15:32:44 -070062
Robert Phillipse3302df2017-04-24 07:31:02 -040063using gr_instanced::OpAllocator;
64using gr_instanced::GLOpAllocator;
65
cdalton8917d622015-05-06 13:40:21 -070066static const GrGLenum gXfermodeEquation2Blend[] = {
67 // Basic OpenGL blend equations.
68 GR_GL_FUNC_ADD,
69 GR_GL_FUNC_SUBTRACT,
70 GR_GL_FUNC_REVERSE_SUBTRACT,
71
72 // GL_KHR_blend_equation_advanced.
73 GR_GL_SCREEN,
74 GR_GL_OVERLAY,
75 GR_GL_DARKEN,
76 GR_GL_LIGHTEN,
77 GR_GL_COLORDODGE,
78 GR_GL_COLORBURN,
79 GR_GL_HARDLIGHT,
80 GR_GL_SOFTLIGHT,
81 GR_GL_DIFFERENCE,
82 GR_GL_EXCLUSION,
83 GR_GL_MULTIPLY,
84 GR_GL_HSL_HUE,
85 GR_GL_HSL_SATURATION,
86 GR_GL_HSL_COLOR,
87 GR_GL_HSL_LUMINOSITY
88};
89GR_STATIC_ASSERT(0 == kAdd_GrBlendEquation);
90GR_STATIC_ASSERT(1 == kSubtract_GrBlendEquation);
91GR_STATIC_ASSERT(2 == kReverseSubtract_GrBlendEquation);
92GR_STATIC_ASSERT(3 == kScreen_GrBlendEquation);
93GR_STATIC_ASSERT(4 == kOverlay_GrBlendEquation);
94GR_STATIC_ASSERT(5 == kDarken_GrBlendEquation);
95GR_STATIC_ASSERT(6 == kLighten_GrBlendEquation);
96GR_STATIC_ASSERT(7 == kColorDodge_GrBlendEquation);
97GR_STATIC_ASSERT(8 == kColorBurn_GrBlendEquation);
98GR_STATIC_ASSERT(9 == kHardLight_GrBlendEquation);
99GR_STATIC_ASSERT(10 == kSoftLight_GrBlendEquation);
100GR_STATIC_ASSERT(11 == kDifference_GrBlendEquation);
101GR_STATIC_ASSERT(12 == kExclusion_GrBlendEquation);
102GR_STATIC_ASSERT(13 == kMultiply_GrBlendEquation);
103GR_STATIC_ASSERT(14 == kHSLHue_GrBlendEquation);
104GR_STATIC_ASSERT(15 == kHSLSaturation_GrBlendEquation);
105GR_STATIC_ASSERT(16 == kHSLColor_GrBlendEquation);
106GR_STATIC_ASSERT(17 == kHSLLuminosity_GrBlendEquation);
bsalomonf7cc8772015-05-11 11:21:14 -0700107GR_STATIC_ASSERT(SK_ARRAY_COUNT(gXfermodeEquation2Blend) == kGrBlendEquationCnt);
cdalton8917d622015-05-06 13:40:21 -0700108
twiz@google.com0f31ca72011-03-18 17:38:11 +0000109static const GrGLenum gXfermodeCoeff2Blend[] = {
110 GR_GL_ZERO,
111 GR_GL_ONE,
112 GR_GL_SRC_COLOR,
113 GR_GL_ONE_MINUS_SRC_COLOR,
114 GR_GL_DST_COLOR,
115 GR_GL_ONE_MINUS_DST_COLOR,
116 GR_GL_SRC_ALPHA,
117 GR_GL_ONE_MINUS_SRC_ALPHA,
118 GR_GL_DST_ALPHA,
119 GR_GL_ONE_MINUS_DST_ALPHA,
120 GR_GL_CONSTANT_COLOR,
121 GR_GL_ONE_MINUS_CONSTANT_COLOR,
122 GR_GL_CONSTANT_ALPHA,
123 GR_GL_ONE_MINUS_CONSTANT_ALPHA,
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000124
125 // extended blend coeffs
126 GR_GL_SRC1_COLOR,
127 GR_GL_ONE_MINUS_SRC1_COLOR,
128 GR_GL_SRC1_ALPHA,
129 GR_GL_ONE_MINUS_SRC1_ALPHA,
reed@google.comac10a2d2010-12-22 21:39:39 +0000130};
131
bsalomon861e1032014-12-16 07:33:49 -0800132bool GrGLGpu::BlendCoeffReferencesConstant(GrBlendCoeff coeff) {
bsalomon@google.com080773c2011-03-15 19:09:25 +0000133 static const bool gCoeffReferencesBlendConst[] = {
134 false,
135 false,
136 false,
137 false,
138 false,
139 false,
140 false,
141 false,
142 false,
143 false,
144 true,
145 true,
146 true,
147 true,
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000148
149 // extended blend coeffs
150 false,
151 false,
152 false,
153 false,
bsalomon@google.com080773c2011-03-15 19:09:25 +0000154 };
155 return gCoeffReferencesBlendConst[coeff];
bsalomonf7cc8772015-05-11 11:21:14 -0700156 GR_STATIC_ASSERT(kGrBlendCoeffCnt == SK_ARRAY_COUNT(gCoeffReferencesBlendConst));
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000157
bsalomon@google.com47059542012-06-06 20:51:20 +0000158 GR_STATIC_ASSERT(0 == kZero_GrBlendCoeff);
159 GR_STATIC_ASSERT(1 == kOne_GrBlendCoeff);
160 GR_STATIC_ASSERT(2 == kSC_GrBlendCoeff);
161 GR_STATIC_ASSERT(3 == kISC_GrBlendCoeff);
162 GR_STATIC_ASSERT(4 == kDC_GrBlendCoeff);
163 GR_STATIC_ASSERT(5 == kIDC_GrBlendCoeff);
164 GR_STATIC_ASSERT(6 == kSA_GrBlendCoeff);
165 GR_STATIC_ASSERT(7 == kISA_GrBlendCoeff);
166 GR_STATIC_ASSERT(8 == kDA_GrBlendCoeff);
167 GR_STATIC_ASSERT(9 == kIDA_GrBlendCoeff);
168 GR_STATIC_ASSERT(10 == kConstC_GrBlendCoeff);
169 GR_STATIC_ASSERT(11 == kIConstC_GrBlendCoeff);
170 GR_STATIC_ASSERT(12 == kConstA_GrBlendCoeff);
171 GR_STATIC_ASSERT(13 == kIConstA_GrBlendCoeff);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000172
bsalomon@google.com47059542012-06-06 20:51:20 +0000173 GR_STATIC_ASSERT(14 == kS2C_GrBlendCoeff);
174 GR_STATIC_ASSERT(15 == kIS2C_GrBlendCoeff);
175 GR_STATIC_ASSERT(16 == kS2A_GrBlendCoeff);
176 GR_STATIC_ASSERT(17 == kIS2A_GrBlendCoeff);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000177
178 // assertion for gXfermodeCoeff2Blend have to be in GrGpu scope
bsalomonf7cc8772015-05-11 11:21:14 -0700179 GR_STATIC_ASSERT(kGrBlendCoeffCnt == SK_ARRAY_COUNT(gXfermodeCoeff2Blend));
bsalomon@google.com080773c2011-03-15 19:09:25 +0000180}
181
reed@google.comac10a2d2010-12-22 21:39:39 +0000182///////////////////////////////////////////////////////////////////////////////
183
egdanielff1d5472015-09-10 08:37:20 -0700184
bsalomon682c2692015-05-22 14:01:46 -0700185GrGpu* GrGLGpu::Create(GrBackendContext backendContext, const GrContextOptions& options,
186 GrContext* context) {
Hal Canary144caf52016-11-07 17:57:18 -0500187 sk_sp<const GrGLInterface> glInterface(
bsalomon424cc262015-05-22 10:37:30 -0700188 reinterpret_cast<const GrGLInterface*>(backendContext));
189 if (!glInterface) {
190 glInterface.reset(GrGLDefaultInterface());
191 } else {
192 glInterface->ref();
193 }
194 if (!glInterface) {
halcanary96fcdcc2015-08-27 07:41:13 -0700195 return nullptr;
bsalomon424cc262015-05-22 10:37:30 -0700196 }
Jim Van Verth76334772017-05-05 16:46:05 -0400197#ifdef USE_NSIGHT
198 const_cast<GrContextOptions&>(options).fSuppressPathRendering = true;
199#endif
Hal Canary144caf52016-11-07 17:57:18 -0500200 GrGLContext* glContext = GrGLContext::Create(glInterface.get(), options);
bsalomon424cc262015-05-22 10:37:30 -0700201 if (glContext) {
halcanary385fe4d2015-08-26 13:07:48 -0700202 return new GrGLGpu(glContext, context);
bsalomon424cc262015-05-22 10:37:30 -0700203 }
halcanary96fcdcc2015-08-27 07:41:13 -0700204 return nullptr;
bsalomon424cc262015-05-22 10:37:30 -0700205}
206
rileya@google.come38160c2012-07-03 18:03:04 +0000207static bool gPrintStartupSpew;
bsalomon@google.com42ab7ea2011-01-19 17:19:40 +0000208
bsalomon424cc262015-05-22 10:37:30 -0700209GrGLGpu::GrGLGpu(GrGLContext* ctx, GrContext* context)
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000210 : GrGpu(context)
cdaltone2e71c22016-04-07 18:13:29 -0700211 , fGLContext(ctx)
212 , fProgramCache(new ProgramCache(this))
213 , fHWProgramID(0)
214 , fTempSrcFBOID(0)
215 , fTempDstFBOID(0)
216 , fStencilClearFBOID(0)
cdalton74b8d322016-04-11 14:47:28 -0700217 , fHWMaxUsedBufferTextureUnit(-1)
cdaltone2e71c22016-04-07 18:13:29 -0700218 , fHWMinSampleShading(0.0) {
219 for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) {
220 fCopyPrograms[i].fProgram = 0;
221 }
brianosman33f6b3f2016-06-02 05:49:21 -0700222 for (size_t i = 0; i < SK_ARRAY_COUNT(fMipmapPrograms); ++i) {
223 fMipmapPrograms[i].fProgram = 0;
224 }
Mike Klein31550db2017-06-06 23:29:53 +0000225 fStencilClipClearProgram = 0;
cdaltone2e71c22016-04-07 18:13:29 -0700226
bsalomon424cc262015-05-22 10:37:30 -0700227 SkASSERT(ctx);
228 fCaps.reset(SkRef(ctx->caps()));
bsalomon@google.combcce8922013-03-25 15:38:39 +0000229
Brian Salomon1edc5b92016-11-29 13:43:46 -0500230 fHWBoundTextureUniqueIDs.reset(this->caps()->shaderCaps()->maxCombinedSamplers());
231 fHWBoundImageStorages.reset(this->caps()->shaderCaps()->maxCombinedImageStorages());
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +0000232
cdaltone2e71c22016-04-07 18:13:29 -0700233 fHWBufferState[kVertex_GrBufferType].fGLTarget = GR_GL_ARRAY_BUFFER;
234 fHWBufferState[kIndex_GrBufferType].fGLTarget = GR_GL_ELEMENT_ARRAY_BUFFER;
235 fHWBufferState[kTexel_GrBufferType].fGLTarget = GR_GL_TEXTURE_BUFFER;
236 fHWBufferState[kDrawIndirect_GrBufferType].fGLTarget = GR_GL_DRAW_INDIRECT_BUFFER;
237 if (GrGLCaps::kChromium_TransferBufferType == this->glCaps().transferBufferType()) {
238 fHWBufferState[kXferCpuToGpu_GrBufferType].fGLTarget =
239 GR_GL_PIXEL_UNPACK_TRANSFER_BUFFER_CHROMIUM;
240 fHWBufferState[kXferGpuToCpu_GrBufferType].fGLTarget =
241 GR_GL_PIXEL_PACK_TRANSFER_BUFFER_CHROMIUM;
242 } else {
243 fHWBufferState[kXferCpuToGpu_GrBufferType].fGLTarget = GR_GL_PIXEL_UNPACK_BUFFER;
244 fHWBufferState[kXferGpuToCpu_GrBufferType].fGLTarget = GR_GL_PIXEL_PACK_BUFFER;
245 }
246 GR_STATIC_ASSERT(6 == SK_ARRAY_COUNT(fHWBufferState));
247
cdalton74b8d322016-04-11 14:47:28 -0700248 if (this->caps()->shaderCaps()->texelBufferSupport()) {
Brian Salomon1edc5b92016-11-29 13:43:46 -0500249 fHWBufferTextures.reset(this->caps()->shaderCaps()->maxCombinedSamplers());
cdalton74b8d322016-04-11 14:47:28 -0700250 }
251
cdaltone2e71c22016-04-07 18:13:29 -0700252 if (this->glCaps().shaderCaps()->pathRenderingSupport()) {
253 fPathRendering.reset(new GrGLPathRendering(this));
254 }
255
bsalomon424cc262015-05-22 10:37:30 -0700256 GrGLClearErr(this->glInterface());
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000257 if (gPrintStartupSpew) {
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000258 const GrGLubyte* vendor;
259 const GrGLubyte* renderer;
260 const GrGLubyte* version;
Jim Van Verth7f7b9022017-06-26 16:33:54 -0400261 const GrGLubyte* glslVersion;
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000262 GL_CALL_RET(vendor, GetString(GR_GL_VENDOR));
263 GL_CALL_RET(renderer, GetString(GR_GL_RENDERER));
264 GL_CALL_RET(version, GetString(GR_GL_VERSION));
Jim Van Verth7f7b9022017-06-26 16:33:54 -0400265 GL_CALL_RET(glslVersion, GetString(GR_GL_SHADING_LANGUAGE_VERSION));
bsalomon861e1032014-12-16 07:33:49 -0800266 SkDebugf("------------------------- create GrGLGpu %p --------------\n",
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000267 this);
tfarina38406c82014-10-31 07:11:12 -0700268 SkDebugf("------ VENDOR %s\n", vendor);
269 SkDebugf("------ RENDERER %s\n", renderer);
270 SkDebugf("------ VERSION %s\n", version);
Jim Van Verth7f7b9022017-06-26 16:33:54 -0400271 SkDebugf("------ SHADING LANGUAGE VERSION %s\n", glslVersion);
tfarina38406c82014-10-31 07:11:12 -0700272 SkDebugf("------ EXTENSIONS\n");
bsalomon424cc262015-05-22 10:37:30 -0700273 this->glContext().extensions().print();
tfarina38406c82014-10-31 07:11:12 -0700274 SkDebugf("\n");
kkinnunen297aaf92015-02-19 06:32:12 -0800275 SkDebugf("%s", this->glCaps().dump().c_str());
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000276 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000277}
278
bsalomon861e1032014-12-16 07:33:49 -0800279GrGLGpu::~GrGLGpu() {
cdaltone2e71c22016-04-07 18:13:29 -0700280 // Ensure any GrGpuResource objects get deleted first, since they may require a working GrGLGpu
281 // to release the resources held by the objects themselves.
kkinnunen702501d2016-01-13 23:36:45 -0800282 fPathRendering.reset();
cdaltone2e71c22016-04-07 18:13:29 -0700283 fCopyProgramArrayBuffer.reset();
brianosman33f6b3f2016-06-02 05:49:21 -0700284 fMipmapProgramArrayBuffer.reset();
Mike Klein31550db2017-06-06 23:29:53 +0000285 fStencilClipClearArrayBuffer.reset();
kkinnunen702501d2016-01-13 23:36:45 -0800286
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000287 if (0 != fHWProgramID) {
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000288 // detach the current program so there is no confusion on OpenGL's part
289 // that we want it to be deleted
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000290 GL_CALL(UseProgram(0));
291 }
292
egdanield803f272015-03-18 13:01:52 -0700293 if (0 != fTempSrcFBOID) {
294 GL_CALL(DeleteFramebuffers(1, &fTempSrcFBOID));
egdaniel0f5f9672015-02-03 11:10:51 -0800295 }
egdanield803f272015-03-18 13:01:52 -0700296 if (0 != fTempDstFBOID) {
297 GL_CALL(DeleteFramebuffers(1, &fTempDstFBOID));
egdaniel0f5f9672015-02-03 11:10:51 -0800298 }
egdanield803f272015-03-18 13:01:52 -0700299 if (0 != fStencilClearFBOID) {
300 GL_CALL(DeleteFramebuffers(1, &fStencilClearFBOID));
bsalomondd3143b2015-02-23 09:27:45 -0800301 }
egdaniel0f5f9672015-02-03 11:10:51 -0800302
bsalomon7ea33f52015-11-22 14:51:00 -0800303 for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) {
304 if (0 != fCopyPrograms[i].fProgram) {
305 GL_CALL(DeleteProgram(fCopyPrograms[i].fProgram));
306 }
bsalomon6df86402015-06-01 10:41:49 -0700307 }
bsalomon6dea83f2015-12-03 12:58:06 -0800308
brianosman33f6b3f2016-06-02 05:49:21 -0700309 for (size_t i = 0; i < SK_ARRAY_COUNT(fMipmapPrograms); ++i) {
310 if (0 != fMipmapPrograms[i].fProgram) {
311 GL_CALL(DeleteProgram(fMipmapPrograms[i].fProgram));
312 }
313 }
314
Mike Klein31550db2017-06-06 23:29:53 +0000315 if (0 != fStencilClipClearProgram) {
316 GL_CALL(DeleteProgram(fStencilClipClearProgram));
bsalomon6dea83f2015-12-03 12:58:06 -0800317 }
318
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000319 delete fProgramCache;
bsalomonc8dc1f72014-08-21 13:02:13 -0700320}
321
bsalomon6e2aad42016-04-01 11:54:31 -0700322void GrGLGpu::disconnect(DisconnectType type) {
323 INHERITED::disconnect(type);
324 if (DisconnectType::kCleanup == type) {
325 if (fHWProgramID) {
326 GL_CALL(UseProgram(0));
327 }
328 if (fTempSrcFBOID) {
329 GL_CALL(DeleteFramebuffers(1, &fTempSrcFBOID));
330 }
331 if (fTempDstFBOID) {
332 GL_CALL(DeleteFramebuffers(1, &fTempDstFBOID));
333 }
334 if (fStencilClearFBOID) {
335 GL_CALL(DeleteFramebuffers(1, &fStencilClearFBOID));
336 }
bsalomon6e2aad42016-04-01 11:54:31 -0700337 for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) {
338 if (fCopyPrograms[i].fProgram) {
339 GL_CALL(DeleteProgram(fCopyPrograms[i].fProgram));
340 }
341 }
brianosman33f6b3f2016-06-02 05:49:21 -0700342 for (size_t i = 0; i < SK_ARRAY_COUNT(fMipmapPrograms); ++i) {
343 if (fMipmapPrograms[i].fProgram) {
344 GL_CALL(DeleteProgram(fMipmapPrograms[i].fProgram));
345 }
346 }
Mike Klein31550db2017-06-06 23:29:53 +0000347 if (fStencilClipClearProgram) {
348 GL_CALL(DeleteProgram(fStencilClipClearProgram));
bsalomon6e2aad42016-04-01 11:54:31 -0700349 }
bsalomon6e2aad42016-04-01 11:54:31 -0700350 } else {
351 if (fProgramCache) {
352 fProgramCache->abandon();
353 }
354 }
355
356 delete fProgramCache;
357 fProgramCache = nullptr;
358
bsalomonc8dc1f72014-08-21 13:02:13 -0700359 fHWProgramID = 0;
egdanield803f272015-03-18 13:01:52 -0700360 fTempSrcFBOID = 0;
361 fTempDstFBOID = 0;
362 fStencilClearFBOID = 0;
cdaltone2e71c22016-04-07 18:13:29 -0700363 fCopyProgramArrayBuffer.reset();
bsalomon7ea33f52015-11-22 14:51:00 -0800364 for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) {
365 fCopyPrograms[i].fProgram = 0;
366 }
brianosman33f6b3f2016-06-02 05:49:21 -0700367 fMipmapProgramArrayBuffer.reset();
368 for (size_t i = 0; i < SK_ARRAY_COUNT(fMipmapPrograms); ++i) {
369 fMipmapPrograms[i].fProgram = 0;
370 }
Mike Klein31550db2017-06-06 23:29:53 +0000371 fStencilClipClearProgram = 0;
372 fStencilClipClearArrayBuffer.reset();
jvanverthe9c0fc62015-04-29 11:18:05 -0700373 if (this->glCaps().shaderCaps()->pathRenderingSupport()) {
bsalomon6e2aad42016-04-01 11:54:31 -0700374 this->glPathRendering()->disconnect(type);
bsalomonc8dc1f72014-08-21 13:02:13 -0700375 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000376}
377
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000378///////////////////////////////////////////////////////////////////////////////
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000379
bsalomon861e1032014-12-16 07:33:49 -0800380void GrGLGpu::onResetContext(uint32_t resetBits) {
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000381 if (resetBits & kMisc_GrGLBackendState) {
Brian Salomonf0861672017-05-08 11:10:10 -0400382 // we don't use the zb at all
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000383 GL_CALL(Disable(GR_GL_DEPTH_TEST));
384 GL_CALL(DepthMask(GR_GL_FALSE));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000385
Brian Salomonf0861672017-05-08 11:10:10 -0400386 // We don't use face culling.
387 GL_CALL(Disable(GR_GL_CULL_FACE));
388 // We do use separate stencil. Our algorithms don't care which face is front vs. back so
389 // just set this to the default for self-consistency.
390 GL_CALL(FrontFace(GR_GL_CCW));
391
cdaltone2e71c22016-04-07 18:13:29 -0700392 fHWBufferState[kTexel_GrBufferType].invalidate();
393 fHWBufferState[kDrawIndirect_GrBufferType].invalidate();
394 fHWBufferState[kXferCpuToGpu_GrBufferType].invalidate();
395 fHWBufferState[kXferGpuToCpu_GrBufferType].invalidate();
cdaltonc1613102016-03-16 07:48:20 -0700396
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +0000397 if (kGL_GrGLStandard == this->glStandard()) {
Jim Van Verth32ac83e2016-11-28 15:23:57 -0500398#ifndef USE_NSIGHT
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000399 // Desktop-only state that we never change
400 if (!this->glCaps().isCoreProfile()) {
401 GL_CALL(Disable(GR_GL_POINT_SMOOTH));
402 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
403 GL_CALL(Disable(GR_GL_POLYGON_SMOOTH));
404 GL_CALL(Disable(GR_GL_POLYGON_STIPPLE));
405 GL_CALL(Disable(GR_GL_COLOR_LOGIC_OP));
406 GL_CALL(Disable(GR_GL_INDEX_LOGIC_OP));
407 }
408 // The windows NVIDIA driver has GL_ARB_imaging in the extension string when using a
409 // core profile. This seems like a bug since the core spec removes any mention of
410 // GL_ARB_imaging.
411 if (this->glCaps().imagingSupport() && !this->glCaps().isCoreProfile()) {
412 GL_CALL(Disable(GR_GL_COLOR_TABLE));
413 }
414 GL_CALL(Disable(GR_GL_POLYGON_OFFSET_FILL));
Jim Van Verth609e7cc2017-03-30 14:28:08 -0400415
Jim Van Verthfbdc0802017-05-02 16:15:53 -0400416 if (this->caps()->wireframeMode()) {
417 GL_CALL(PolygonMode(GR_GL_FRONT_AND_BACK, GR_GL_LINE));
418 } else {
419 GL_CALL(PolygonMode(GR_GL_FRONT_AND_BACK, GR_GL_FILL));
420 }
Jim Van Verth32ac83e2016-11-28 15:23:57 -0500421#endif
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000422 // Since ES doesn't support glPointSize at all we always use the VS to
423 // set the point size
424 GL_CALL(Enable(GR_GL_VERTEX_PROGRAM_POINT_SIZE));
425
bsalomon@google.com2b1b8c02013-02-28 22:06:02 +0000426 }
joshualitt58162332014-08-01 06:44:53 -0700427
428 if (kGLES_GrGLStandard == this->glStandard() &&
bsalomon424cc262015-05-22 10:37:30 -0700429 this->hasExtension("GL_ARM_shader_framebuffer_fetch")) {
joshualitt58162332014-08-01 06:44:53 -0700430 // The arm extension requires specifically enabling MSAA fetching per sample.
431 // On some devices this may have a perf hit. Also multiple render targets are disabled
432 GL_CALL(Enable(GR_GL_FETCH_PER_SAMPLE_ARM));
433 }
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000434 fHWWriteToColor = kUnknown_TriState;
435 // we only ever use lines in hairline mode
436 GL_CALL(LineWidth(1));
bsalomonaca31fe2015-09-22 11:38:46 -0700437 GL_CALL(Disable(GR_GL_DITHER));
bsalomon@google.comcad107b2013-06-28 14:32:08 +0000438 }
edisonn@google.comba669992013-06-28 16:03:21 +0000439
egdanielb414f252014-07-29 13:15:47 -0700440 if (resetBits & kMSAAEnable_GrGLBackendState) {
441 fMSAAEnabled = kUnknown_TriState;
vbuzinovdded6962015-06-12 08:59:45 -0700442
egdanieleed519e2016-01-15 11:36:18 -0800443 if (this->caps()->usesMixedSamples()) {
cdaltonaf8bc7d2016-02-05 09:35:20 -0800444 if (0 != this->caps()->maxRasterSamples()) {
445 fHWRasterMultisampleEnabled = kUnknown_TriState;
446 fHWNumRasterSamples = 0;
447 }
448
449 // The skia blend modes all use premultiplied alpha and therefore expect RGBA coverage
450 // modulation. This state has no effect when not rendering to a mixed sampled target.
vbuzinovdded6962015-06-12 08:59:45 -0700451 GL_CALL(CoverageModulation(GR_GL_RGBA));
452 }
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000453 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000454
commit-bot@chromium.org46fbfe02013-08-30 15:52:12 +0000455 fHWActiveTextureUnitIdx = -1; // invalid
Brian Salomonaf971de2017-06-08 16:11:33 -0400456 fLastPrimitiveType = static_cast<GrPrimitiveType>(-1);
commit-bot@chromium.org46fbfe02013-08-30 15:52:12 +0000457
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000458 if (resetBits & kTextureBinding_GrGLBackendState) {
bsalomon1c63bf62014-07-22 13:09:46 -0700459 for (int s = 0; s < fHWBoundTextureUniqueIDs.count(); ++s) {
Robert Phillips294870f2016-11-11 12:38:40 -0500460 fHWBoundTextureUniqueIDs[s].makeInvalid();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000461 }
cdalton74b8d322016-04-11 14:47:28 -0700462 for (int b = 0; b < fHWBufferTextures.count(); ++b) {
463 SkASSERT(this->caps()->shaderCaps()->texelBufferSupport());
464 fHWBufferTextures[b].fKnownBound = false;
465 }
Brian Salomonf9f45122016-11-29 11:59:17 -0500466 for (int i = 0; i < fHWBoundImageStorages.count(); ++i) {
Brian Salomon8b7a7dd2016-11-29 15:29:41 -0500467 SkASSERT(this->caps()->shaderCaps()->imageLoadStoreSupport());
Brian Salomonf9f45122016-11-29 11:59:17 -0500468 fHWBoundImageStorages[i].fTextureUniqueID.makeInvalid();
469 }
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000470 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000471
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000472 if (resetBits & kBlend_GrGLBackendState) {
473 fHWBlendState.invalidate();
474 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000475
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000476 if (resetBits & kView_GrGLBackendState) {
477 fHWScissorSettings.invalidate();
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700478 fHWWindowRectsState.invalidate();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000479 fHWViewport.invalidate();
480 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000481
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000482 if (resetBits & kStencil_GrGLBackendState) {
483 fHWStencilSettings.invalidate();
484 fHWStencilTestEnabled = kUnknown_TriState;
485 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000486
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000487 // Vertex
488 if (resetBits & kVertex_GrGLBackendState) {
cdaltone2e71c22016-04-07 18:13:29 -0700489 fHWVertexArrayState.invalidate();
490 fHWBufferState[kVertex_GrBufferType].invalidate();
491 fHWBufferState[kIndex_GrBufferType].invalidate();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000492 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000493
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000494 if (resetBits & kRenderTarget_GrGLBackendState) {
Robert Phillips294870f2016-11-11 12:38:40 -0500495 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon16921ec2015-07-30 15:34:56 -0700496 fHWSRGBFramebuffer = kUnknown_TriState;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000497 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000498
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000499 if (resetBits & kPathRendering_GrGLBackendState) {
jvanverthe9c0fc62015-04-29 11:18:05 -0700500 if (this->caps()->shaderCaps()->pathRenderingSupport()) {
kkinnunenccdaa042014-08-20 01:36:23 -0700501 this->glPathRendering()->resetContext();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000502 }
bsalomon@google.com05a718c2012-06-29 14:01:53 +0000503 }
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000504
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000505 // we assume these values
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000506 if (resetBits & kPixelStore_GrGLBackendState) {
507 if (this->glCaps().unpackRowLengthSupport()) {
508 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
509 }
510 if (this->glCaps().packRowLengthSupport()) {
511 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
512 }
513 if (this->glCaps().unpackFlipYSupport()) {
514 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
515 }
516 if (this->glCaps().packFlipYSupport()) {
517 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, GR_GL_FALSE));
518 }
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000519 }
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000520
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000521 if (resetBits & kProgram_GrGLBackendState) {
522 fHWProgramID = 0;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000523 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000524}
525
Greg Daniel7ef28f32017-04-20 16:41:55 +0000526sk_sp<GrTexture> GrGLGpu::onWrapBackendTexture(const GrBackendTexture& backendTex,
527 GrSurfaceOrigin origin,
528 GrBackendTextureFlags flags,
529 int sampleCnt,
bungeman6bd52842016-10-27 09:30:08 -0700530 GrWrapOwnership ownership) {
Greg Daniel7ef28f32017-04-20 16:41:55 +0000531 const GrGLTextureInfo* info = backendTex.getGLTextureInfo();
bsalomon091f60c2015-11-10 11:54:56 -0800532 if (!info || !info->fID) {
533 return nullptr;
534 }
robertphillips@google.comb72e5d32012-10-30 15:18:10 +0000535
Greg Daniel7ef28f32017-04-20 16:41:55 +0000536 // next line relies on GrBackendTextureFlags matching GrTexture's
537 bool renderTarget = SkToBool(flags & kRenderTarget_GrBackendTextureFlag);
bsalomon7ea33f52015-11-22 14:51:00 -0800538
bsalomonb15b4c12014-10-29 12:41:57 -0700539 GrGLTexture::IDDesc idDesc;
bsalomon091f60c2015-11-10 11:54:56 -0800540 idDesc.fInfo = *info;
bsalomone5286e02016-01-14 09:24:09 -0800541
bsalomon7ea33f52015-11-22 14:51:00 -0800542 if (GR_GL_TEXTURE_EXTERNAL == idDesc.fInfo.fTarget) {
543 if (renderTarget) {
544 // This combination is not supported.
545 return nullptr;
546 }
Brian Salomon1edc5b92016-11-29 13:43:46 -0500547 if (!this->caps()->shaderCaps()->externalTextureSupport()) {
bsalomon7ea33f52015-11-22 14:51:00 -0800548 return nullptr;
549 }
bsalomone5286e02016-01-14 09:24:09 -0800550 } else if (GR_GL_TEXTURE_RECTANGLE == idDesc.fInfo.fTarget) {
551 if (!this->glCaps().rectangleTextureSupport()) {
552 return nullptr;
553 }
554 } else if (GR_GL_TEXTURE_2D != idDesc.fInfo.fTarget) {
555 return nullptr;
bsalomon7ea33f52015-11-22 14:51:00 -0800556 }
bsalomone5286e02016-01-14 09:24:09 -0800557
558 // Sample count is interpreted to mean the number of samples that Gr code should allocate
bsalomona98419b2015-11-23 07:09:50 -0800559 // for a render buffer that resolves to the texture. We don't support MSAA textures.
Greg Daniel7ef28f32017-04-20 16:41:55 +0000560 if (sampleCnt && !renderTarget) {
bsalomona98419b2015-11-23 07:09:50 -0800561 return nullptr;
562 }
bsalomon10528f12015-10-14 12:54:52 -0700563
Brian Osman766fcbb2017-03-13 09:33:09 -0400564 if (kBorrow_GrWrapOwnership == ownership) {
Brian Osmana6953f22017-03-10 20:14:05 +0000565 idDesc.fOwnership = GrBackendObjectOwnership::kBorrowed;
Brian Osman766fcbb2017-03-13 09:33:09 -0400566 } else {
567 idDesc.fOwnership = GrBackendObjectOwnership::kOwned;
bsalomone5286e02016-01-14 09:24:09 -0800568 }
bsalomonb15b4c12014-10-29 12:41:57 -0700569
Ben Wagner18b61f92016-10-25 10:44:02 -0400570 GrSurfaceDesc surfDesc;
Greg Daniel7ef28f32017-04-20 16:41:55 +0000571 surfDesc.fFlags = (GrSurfaceFlags) flags;
572 surfDesc.fWidth = backendTex.width();
573 surfDesc.fHeight = backendTex.height();
574 surfDesc.fConfig = backendTex.config();
575 surfDesc.fSampleCnt = SkTMin(sampleCnt, this->caps()->maxSampleCount());
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000576 // FIXME: this should be calling resolve_origin(), but Chrome code is currently
577 // assuming the old behaviour, which is that backend textures are always
578 // BottomLeft, even for non-RT's. Once Chrome is fixed, change this to:
579 // glTexDesc.fOrigin = resolve_origin(desc.fOrigin, renderTarget);
Greg Daniel7ef28f32017-04-20 16:41:55 +0000580 if (kDefault_GrSurfaceOrigin == origin) {
bsalomonb15b4c12014-10-29 12:41:57 -0700581 surfDesc.fOrigin = kBottomLeft_GrSurfaceOrigin;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000582 } else {
Greg Daniel7ef28f32017-04-20 16:41:55 +0000583 surfDesc.fOrigin = origin;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000584 }
bsalomon@google.come269f212011-11-07 13:29:52 +0000585
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000586 if (renderTarget) {
bsalomonb15b4c12014-10-29 12:41:57 -0700587 GrGLRenderTarget::IDDesc rtIDDesc;
kkinnunen2e6055b2016-04-22 01:48:29 -0700588 if (!this->createRenderTargetObjects(surfDesc, idDesc.fInfo, &rtIDDesc)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700589 return nullptr;
bsalomon@google.come269f212011-11-07 13:29:52 +0000590 }
Brian Salomon9bada542017-06-12 12:09:30 -0400591 sk_sp<GrGLTextureRenderTarget> texRT(
592 GrGLTextureRenderTarget::MakeWrapped(this, surfDesc, idDesc, rtIDDesc));
593 texRT->baseLevelWasBoundToFBO();
594 return texRT;
bsalomon@google.come269f212011-11-07 13:29:52 +0000595 }
Brian Osman766fcbb2017-03-13 09:33:09 -0400596
Brian Osman85d34b22017-05-10 12:06:26 -0400597 return GrGLTexture::MakeWrapped(this, surfDesc, idDesc);
bsalomon@google.come269f212011-11-07 13:29:52 +0000598}
599
Greg Danielbcf612b2017-05-01 13:50:58 +0000600sk_sp<GrRenderTarget> GrGLGpu::onWrapBackendRenderTarget(const GrBackendRenderTarget& backendRT,
601 GrSurfaceOrigin origin) {
602 const GrGLFramebufferInfo* info = backendRT.getGLFramebufferInfo();
603 if (!info) {
604 return nullptr;
605 }
606
bsalomonb15b4c12014-10-29 12:41:57 -0700607 GrGLRenderTarget::IDDesc idDesc;
Greg Danielbcf612b2017-05-01 13:50:58 +0000608 idDesc.fRTFBOID = info->fFBOID;
bsalomonb15b4c12014-10-29 12:41:57 -0700609 idDesc.fMSColorRenderbufferID = 0;
egdanield803f272015-03-18 13:01:52 -0700610 idDesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
Brian Osman0b791f52017-03-10 08:30:22 -0500611 idDesc.fRTFBOOwnership = GrBackendObjectOwnership::kBorrowed;
csmartdaltonf9635992016-08-10 11:09:07 -0700612 idDesc.fIsMixedSampled = false;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000613
bsalomonb15b4c12014-10-29 12:41:57 -0700614 GrSurfaceDesc desc;
Greg Danielbcf612b2017-05-01 13:50:58 +0000615 desc.fConfig = backendRT.config();
Brian Salomon0ec981b2017-05-15 13:48:50 -0400616 desc.fFlags = kRenderTarget_GrSurfaceFlag;
Greg Danielbcf612b2017-05-01 13:50:58 +0000617 desc.fWidth = backendRT.width();
618 desc.fHeight = backendRT.height();
619 desc.fSampleCnt = SkTMin(backendRT.sampleCnt(), this->caps()->maxSampleCount());
620 SkASSERT(kDefault_GrSurfaceOrigin != origin);
621 desc.fOrigin = origin;
bsalomonb15b4c12014-10-29 12:41:57 -0700622
Greg Danielbcf612b2017-05-01 13:50:58 +0000623 return GrGLRenderTarget::MakeWrapped(this, desc, idDesc, backendRT.stencilBits());
bsalomon@google.come269f212011-11-07 13:29:52 +0000624}
625
Greg Daniel7ef28f32017-04-20 16:41:55 +0000626sk_sp<GrRenderTarget> GrGLGpu::onWrapBackendTextureAsRenderTarget(const GrBackendTexture& tex,
627 GrSurfaceOrigin origin,
628 int sampleCnt) {
629 const GrGLTextureInfo* info = tex.getGLTextureInfo();
ericrkf7b8b8a2016-02-24 14:49:51 -0800630 if (!info || !info->fID) {
631 return nullptr;
632 }
ericrkf7b8b8a2016-02-24 14:49:51 -0800633
jvanverthdb1adf52016-05-03 05:37:45 -0700634 GrGLTextureInfo texInfo;
jvanverthdb1adf52016-05-03 05:37:45 -0700635 texInfo = *info;
ericrkf7b8b8a2016-02-24 14:49:51 -0800636
jvanverthdb1adf52016-05-03 05:37:45 -0700637 if (GR_GL_TEXTURE_RECTANGLE != texInfo.fTarget &&
638 GR_GL_TEXTURE_2D != texInfo.fTarget) {
ericrkf7b8b8a2016-02-24 14:49:51 -0800639 // Only texture rectangle and texture 2d are supported. We do not check whether texture
640 // rectangle is supported by Skia - if the caller provided us with a texture rectangle,
641 // we assume the necessary support exists.
642 return nullptr;
643 }
644
Ben Wagner18b61f92016-10-25 10:44:02 -0400645 GrSurfaceDesc surfDesc;
Greg Daniel7ef28f32017-04-20 16:41:55 +0000646 surfDesc.fFlags = kRenderTarget_GrSurfaceFlag;
647 surfDesc.fWidth = tex.width();
648 surfDesc.fHeight = tex.height();
649 surfDesc.fConfig = tex.config();
650 surfDesc.fSampleCnt = SkTMin(sampleCnt, this->caps()->maxSampleCount());
ericrkf7b8b8a2016-02-24 14:49:51 -0800651 // FIXME: this should be calling resolve_origin(), but Chrome code is currently
652 // assuming the old behaviour, which is that backend textures are always
653 // BottomLeft, even for non-RT's. Once Chrome is fixed, change this to:
654 // glTexDesc.fOrigin = resolve_origin(desc.fOrigin, renderTarget);
Greg Daniel7ef28f32017-04-20 16:41:55 +0000655 if (kDefault_GrSurfaceOrigin == origin) {
ericrkf7b8b8a2016-02-24 14:49:51 -0800656 surfDesc.fOrigin = kBottomLeft_GrSurfaceOrigin;
657 } else {
Greg Daniel7ef28f32017-04-20 16:41:55 +0000658 surfDesc.fOrigin = origin;
ericrkf7b8b8a2016-02-24 14:49:51 -0800659 }
660
661 GrGLRenderTarget::IDDesc rtIDDesc;
jvanverthdb1adf52016-05-03 05:37:45 -0700662 if (!this->createRenderTargetObjects(surfDesc, texInfo, &rtIDDesc)) {
ericrkf7b8b8a2016-02-24 14:49:51 -0800663 return nullptr;
664 }
bungeman6bd52842016-10-27 09:30:08 -0700665 return GrGLRenderTarget::MakeWrapped(this, surfDesc, rtIDDesc, 0);
ericrkf7b8b8a2016-02-24 14:49:51 -0800666}
667
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000668////////////////////////////////////////////////////////////////////////////////
ericrkf7b8b8a2016-02-24 14:49:51 -0800669
bsalomonf0674512015-07-28 13:26:15 -0700670bool GrGLGpu::onGetWritePixelsInfo(GrSurface* dstSurface, int width, int height,
cblumeed828002016-02-16 13:00:01 -0800671 GrPixelConfig srcConfig,
bsalomonf0674512015-07-28 13:26:15 -0700672 DrawPreference* drawPreference,
673 WritePixelTempDrawInfo* tempDrawInfo) {
Brian Salomon9bada542017-06-12 12:09:30 -0400674 if (SkToBool(dstSurface->asRenderTarget())) {
675 if (this->glCaps().useDrawInsteadOfAllRenderTargetWrites()) {
676 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
677 }
678 }
679
680 GrGLTexture* texture = static_cast<GrGLTexture*>(dstSurface->asTexture());
681
682 if (texture) {
bsalomone5286e02016-01-14 09:24:09 -0800683 if (GR_GL_TEXTURE_EXTERNAL == texture->target()) {
684 // We don't currently support writing pixels to EXTERNAL textures.
bsalomon7ea33f52015-11-22 14:51:00 -0800685 return false;
686 }
Brian Salomon9bada542017-06-12 12:09:30 -0400687 if (GrPixelConfigIsUnorm(texture->config()) && texture->hasBaseLevelBeenBoundToFBO() &&
688 this->glCaps().disallowTexSubImageForUnormConfigTexturesEverBoundToFBO() &&
689 (width < dstSurface->width() || height < dstSurface->height())) {
690 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
691 }
692 } else {
693 // This subclass only allows writes to textures. If the dst is not a texture we have to draw
694 // into it. We could use glDrawPixels on GLs that have it, but we don't today.
695 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
bsalomon6cb3cbe2015-07-30 07:34:27 -0700696 }
697
Brian Osman33910292017-04-18 14:38:53 -0400698 // If the dst is MSAA, we have to draw, or we'll just be writing to the resolve target.
699 if (dstSurface->asRenderTarget() && dstSurface->asRenderTarget()->numColorSamples() > 0) {
700 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
701 }
702
bsalomon16921ec2015-07-30 15:34:56 -0700703 if (GrPixelConfigIsSRGB(dstSurface->config()) != GrPixelConfigIsSRGB(srcConfig)) {
704 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
705 }
706
bsalomon6c9cd552016-01-22 07:17:34 -0800707 // Start off assuming no swizzling
708 tempDrawInfo->fSwizzle = GrSwizzle::RGBA();
709 tempDrawInfo->fWriteConfig = srcConfig;
bsalomonf0674512015-07-28 13:26:15 -0700710
711 // These settings we will always want if a temp draw is performed. Initially set the config
712 // to srcConfig, though that may be modified if we decide to do a R/G swap.
713 tempDrawInfo->fTempSurfaceDesc.fFlags = kNone_GrSurfaceFlags;
714 tempDrawInfo->fTempSurfaceDesc.fConfig = srcConfig;
715 tempDrawInfo->fTempSurfaceDesc.fWidth = width;
716 tempDrawInfo->fTempSurfaceDesc.fHeight = height;
717 tempDrawInfo->fTempSurfaceDesc.fSampleCnt = 0;
718 tempDrawInfo->fTempSurfaceDesc.fOrigin = kTopLeft_GrSurfaceOrigin; // no CPU y-flip for TL.
719
720 bool configsAreRBSwaps = GrPixelConfigSwapRAndB(srcConfig) == dstSurface->config();
721
722 if (configsAreRBSwaps) {
723 if (!this->caps()->isConfigTexturable(srcConfig)) {
724 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
725 tempDrawInfo->fTempSurfaceDesc.fConfig = dstSurface->config();
bsalomon6c9cd552016-01-22 07:17:34 -0800726 tempDrawInfo->fSwizzle = GrSwizzle::BGRA();
727 tempDrawInfo->fWriteConfig = dstSurface->config();
bsalomon88c7b982015-07-31 11:20:16 -0700728 } else if (this->glCaps().rgba8888PixelsOpsAreSlow() &&
729 kRGBA_8888_GrPixelConfig == srcConfig) {
bsalomonf0674512015-07-28 13:26:15 -0700730 ElevateDrawPreference(drawPreference, kGpuPrefersDraw_DrawPreference);
731 tempDrawInfo->fTempSurfaceDesc.fConfig = dstSurface->config();
bsalomon6c9cd552016-01-22 07:17:34 -0800732 tempDrawInfo->fSwizzle = GrSwizzle::BGRA();
733 tempDrawInfo->fWriteConfig = dstSurface->config();
bsalomonf0674512015-07-28 13:26:15 -0700734 } else if (kGLES_GrGLStandard == this->glStandard() &&
735 this->glCaps().bgraIsInternalFormat()) {
736 // The internal format and external formats must match texture uploads so we can't
737 // swizzle while uploading when BGRA is a distinct internal format.
738 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
739 tempDrawInfo->fTempSurfaceDesc.fConfig = dstSurface->config();
bsalomon6c9cd552016-01-22 07:17:34 -0800740 tempDrawInfo->fSwizzle = GrSwizzle::BGRA();
741 tempDrawInfo->fWriteConfig = dstSurface->config();
bsalomonf0674512015-07-28 13:26:15 -0700742 }
743 }
744
745 if (!this->glCaps().unpackFlipYSupport() &&
746 kBottomLeft_GrSurfaceOrigin == dstSurface->origin()) {
747 ElevateDrawPreference(drawPreference, kGpuPrefersDraw_DrawPreference);
748 }
749
750 return true;
751}
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000752
jvanverth17aa0472016-01-05 10:41:27 -0800753static bool check_write_and_transfer_input(GrGLTexture* glTex, GrSurface* surface,
754 GrPixelConfig config) {
bsalomon6cb3cbe2015-07-30 07:34:27 -0700755 if (!glTex) {
756 return false;
757 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000758
bsalomon16921ec2015-07-30 15:34:56 -0700759 // OpenGL doesn't do sRGB <-> linear conversions when reading and writing pixels.
760 if (GrPixelConfigIsSRGB(surface->config()) != GrPixelConfigIsSRGB(config)) {
761 return false;
762 }
763
bsalomone5286e02016-01-14 09:24:09 -0800764 // Write or transfer of pixels is not implemented for TEXTURE_EXTERNAL textures
765 if (GR_GL_TEXTURE_EXTERNAL == glTex->target()) {
bsalomon7ea33f52015-11-22 14:51:00 -0800766 return false;
767 }
768
jvanverth17aa0472016-01-05 10:41:27 -0800769 return true;
770}
771
772bool GrGLGpu::onWritePixels(GrSurface* surface,
773 int left, int top, int width, int height,
cblume55f2d2d2016-02-26 13:20:48 -0800774 GrPixelConfig config,
Robert Phillips590533f2017-07-11 14:22:35 -0400775 const GrMipLevel texels[],
776 int mipLevelCount) {
jvanverth17aa0472016-01-05 10:41:27 -0800777 GrGLTexture* glTex = static_cast<GrGLTexture*>(surface->asTexture());
778
779 if (!check_write_and_transfer_input(glTex, surface, config)) {
780 return false;
781 }
782
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +0000783 this->setScratchTextureUnit();
bsalomon10528f12015-10-14 12:54:52 -0700784 GL_CALL(BindTexture(glTex->target(), glTex->textureID()));
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000785
Robert Phillips92de6312017-05-23 07:43:48 -0400786 return this->uploadTexData(glTex->config(), glTex->width(), glTex->height(),
787 glTex->origin(), glTex->target(), kWrite_UploadType,
Robert Phillips590533f2017-07-11 14:22:35 -0400788 left, top, width, height, config, texels, mipLevelCount);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000789}
790
bsalomonf46a1242015-12-15 12:37:38 -0800791// For GL_[UN]PACK_ALIGNMENT.
792static inline GrGLint config_alignment(GrPixelConfig config) {
bsalomonf46a1242015-12-15 12:37:38 -0800793 switch (config) {
794 case kAlpha_8_GrPixelConfig:
Brian Osman986563b2017-01-10 14:20:02 -0500795 case kGray_8_GrPixelConfig:
bsalomonf46a1242015-12-15 12:37:38 -0800796 return 1;
797 case kRGB_565_GrPixelConfig:
798 case kRGBA_4444_GrPixelConfig:
799 case kAlpha_half_GrPixelConfig:
800 case kRGBA_half_GrPixelConfig:
801 return 2;
802 case kRGBA_8888_GrPixelConfig:
803 case kBGRA_8888_GrPixelConfig:
804 case kSRGBA_8888_GrPixelConfig:
brianosmana6359362016-03-21 06:55:37 -0700805 case kSBGRA_8888_GrPixelConfig:
Brian Salomonbf7b6202016-11-11 16:08:03 -0500806 case kRGBA_8888_sint_GrPixelConfig:
bsalomonf46a1242015-12-15 12:37:38 -0800807 case kRGBA_float_GrPixelConfig:
csmartdalton6aa0e112017-02-08 16:14:11 -0500808 case kRG_float_GrPixelConfig:
bsalomonf46a1242015-12-15 12:37:38 -0800809 return 4;
csmartdalton6aa0e112017-02-08 16:14:11 -0500810 case kUnknown_GrPixelConfig:
bsalomonf46a1242015-12-15 12:37:38 -0800811 return 0;
812 }
csmartdalton6aa0e112017-02-08 16:14:11 -0500813 SkFAIL("Invalid pixel config");
814 return 0;
bsalomonf46a1242015-12-15 12:37:38 -0800815}
816
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400817bool GrGLGpu::onTransferPixels(GrTexture* texture,
818 int left, int top, int width, int height,
819 GrPixelConfig config, GrBuffer* transferBuffer,
820 size_t offset, size_t rowBytes) {
821 GrGLTexture* glTex = static_cast<GrGLTexture*>(texture);
822 GrPixelConfig texConfig = glTex->config();
823 SkASSERT(this->caps()->isConfigTexturable(texConfig));
824
825 if (!check_write_and_transfer_input(glTex, texture, config)) {
826 return false;
827 }
828
829 if (width <= 0 || width > SK_MaxS32 || height <= 0 || height > SK_MaxS32) {
830 return false;
831 }
832
833 this->setScratchTextureUnit();
834 GL_CALL(BindTexture(glTex->target(), glTex->textureID()));
835
836 SkASSERT(!transferBuffer->isMapped());
837 SkASSERT(!transferBuffer->isCPUBacked());
838 const GrGLBuffer* glBuffer = static_cast<const GrGLBuffer*>(transferBuffer);
839 this->bindBuffer(kXferCpuToGpu_GrBufferType, glBuffer);
840
Greg Daniel660cc992017-06-26 14:55:05 -0400841 SkDEBUGCODE(
842 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
843 SkIRect bounds = SkIRect::MakeWH(texture->width(), texture->height());
844 SkASSERT(bounds.contains(subRect));
845 )
846
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400847 size_t bpp = GrBytesPerPixel(config);
848 const size_t trimRowBytes = width * bpp;
Greg Daniel660cc992017-06-26 14:55:05 -0400849 if (!rowBytes) {
850 rowBytes = trimRowBytes;
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400851 }
Greg Daniel660cc992017-06-26 14:55:05 -0400852 const void* pixels = (void*)offset;
Bruce Dawson71479b72017-07-05 14:30:20 -0700853 if (width < 0 || height < 0) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400854 return false;
855 }
856
857 bool restoreGLRowLength = false;
858 if (trimRowBytes != rowBytes) {
859 // we should have checked for this support already
860 SkASSERT(this->glCaps().unpackRowLengthSupport());
861 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowBytes / bpp));
862 restoreGLRowLength = true;
863 }
864
865 // Internal format comes from the texture desc.
866 GrGLenum internalFormat;
867 // External format and type come from the upload data.
868 GrGLenum externalFormat;
869 GrGLenum externalType;
870 if (!this->glCaps().getTexImageFormats(texConfig, config, &internalFormat,
871 &externalFormat, &externalType)) {
872 return false;
873 }
874
875 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, config_alignment(texConfig)));
876 GL_CALL(TexSubImage2D(glTex->target(),
877 0,
878 left, top,
879 width,
880 height,
881 externalFormat, externalType,
882 pixels));
883
884 if (restoreGLRowLength) {
885 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
886 }
887
888 return true;
889}
890
cblume55f2d2d2016-02-26 13:20:48 -0800891/**
892 * Creates storage space for the texture and fills it with texels.
893 *
Brian Salomond1eaf492017-05-18 10:02:08 -0400894 * @param config Pixel config of the texture.
cblume55f2d2d2016-02-26 13:20:48 -0800895 * @param interface The GL interface in use.
cblume790d5132016-02-29 11:13:29 -0800896 * @param caps The capabilities of the GL device.
brianosman81a84852016-09-12 09:05:14 -0700897 * @param internalFormat The data format used for the internal storage of the texture. May be sized.
898 * @param internalFormatForTexStorage The data format used for the TexStorage API. Must be sized.
cblume55f2d2d2016-02-26 13:20:48 -0800899 * @param externalFormat The data format used for the external storage of the texture.
900 * @param externalType The type of the data used for the external storage of the texture.
901 * @param texels The texel data of the texture being created.
902 * @param baseWidth The width of the texture's base mipmap level
903 * @param baseHeight The height of the texture's base mipmap level
cblume55f2d2d2016-02-26 13:20:48 -0800904 */
Robert Phillips92de6312017-05-23 07:43:48 -0400905static bool allocate_and_populate_texture(GrPixelConfig config,
906 const GrGLInterface& interface,
907 const GrGLCaps& caps,
908 GrGLenum target,
909 GrGLenum internalFormat,
910 GrGLenum internalFormatForTexStorage,
911 GrGLenum externalFormat,
912 GrGLenum externalType,
Robert Phillips590533f2017-07-11 14:22:35 -0400913 const GrMipLevel texels[], int mipLevelCount,
Robert Phillips92de6312017-05-23 07:43:48 -0400914 int baseWidth, int baseHeight) {
cblume55f2d2d2016-02-26 13:20:48 -0800915 CLEAR_ERROR_BEFORE_ALLOC(&interface);
cblume790d5132016-02-29 11:13:29 -0800916
Brian Salomond1eaf492017-05-18 10:02:08 -0400917 bool useTexStorage = caps.isConfigTexSupportEnabled(config);
cblume790d5132016-02-29 11:13:29 -0800918 // We can only use TexStorage if we know we will not later change the storage requirements.
919 // This means if we may later want to add mipmaps, we cannot use TexStorage.
920 // Right now, we cannot know if we will later add mipmaps or not.
921 // The only time we can use TexStorage is when we already have the
Brian Salomonbf7b6202016-11-11 16:08:03 -0500922 // mipmaps or are using a format incompatible with MIP maps.
Robert Phillips590533f2017-07-11 14:22:35 -0400923 useTexStorage &= mipLevelCount > 1 || GrPixelConfigIsSint(config);
cblume790d5132016-02-29 11:13:29 -0800924
925 if (useTexStorage) {
926 // We never resize or change formats of textures.
cblume55f2d2d2016-02-26 13:20:48 -0800927 GL_ALLOC_CALL(&interface,
Robert Phillips590533f2017-07-11 14:22:35 -0400928 TexStorage2D(target, SkTMax(mipLevelCount, 1), internalFormatForTexStorage,
Brian Salomond1eaf492017-05-18 10:02:08 -0400929 baseWidth, baseHeight));
Brian Salomon0ec981b2017-05-15 13:48:50 -0400930 GrGLenum error = CHECK_ALLOC_ERROR(&interface);
cblume55f2d2d2016-02-26 13:20:48 -0800931 if (error != GR_GL_NO_ERROR) {
bsalomone699d0c2016-03-09 06:25:15 -0800932 return false;
cblume790d5132016-02-29 11:13:29 -0800933 } else {
Robert Phillips590533f2017-07-11 14:22:35 -0400934 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
cblume790d5132016-02-29 11:13:29 -0800935 const void* currentMipData = texels[currentMipLevel].fPixels;
936 if (currentMipData == nullptr) {
937 continue;
938 }
939 int twoToTheMipLevel = 1 << currentMipLevel;
Brian Salomond1eaf492017-05-18 10:02:08 -0400940 int currentWidth = SkTMax(1, baseWidth / twoToTheMipLevel);
941 int currentHeight = SkTMax(1, baseHeight / twoToTheMipLevel);
cblume790d5132016-02-29 11:13:29 -0800942
943 GR_GL_CALL(&interface,
944 TexSubImage2D(target,
945 currentMipLevel,
946 0, // left
947 0, // top
948 currentWidth,
949 currentHeight,
950 externalFormat, externalType,
951 currentMipData));
952 }
bsalomone699d0c2016-03-09 06:25:15 -0800953 return true;
cblume790d5132016-02-29 11:13:29 -0800954 }
955 } else {
Robert Phillips590533f2017-07-11 14:22:35 -0400956 if (!mipLevelCount) {
cblume790d5132016-02-29 11:13:29 -0800957 GL_ALLOC_CALL(&interface,
958 TexImage2D(target,
bsalomone699d0c2016-03-09 06:25:15 -0800959 0,
cblume790d5132016-02-29 11:13:29 -0800960 internalFormat,
bsalomone699d0c2016-03-09 06:25:15 -0800961 baseWidth,
962 baseHeight,
cblume790d5132016-02-29 11:13:29 -0800963 0, // border
964 externalFormat, externalType,
bsalomone699d0c2016-03-09 06:25:15 -0800965 nullptr));
Brian Salomon0ec981b2017-05-15 13:48:50 -0400966 GrGLenum error = CHECK_ALLOC_ERROR(&interface);
cblume790d5132016-02-29 11:13:29 -0800967 if (error != GR_GL_NO_ERROR) {
bsalomone699d0c2016-03-09 06:25:15 -0800968 return false;
969 }
970 } else {
Robert Phillips590533f2017-07-11 14:22:35 -0400971 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
bsalomone699d0c2016-03-09 06:25:15 -0800972 int twoToTheMipLevel = 1 << currentMipLevel;
973 int currentWidth = SkTMax(1, baseWidth / twoToTheMipLevel);
974 int currentHeight = SkTMax(1, baseHeight / twoToTheMipLevel);
975 const void* currentMipData = texels[currentMipLevel].fPixels;
976 // Even if curremtMipData is nullptr, continue to call TexImage2D.
977 // This will allocate texture memory which we can later populate.
978 GL_ALLOC_CALL(&interface,
979 TexImage2D(target,
980 currentMipLevel,
981 internalFormat,
982 currentWidth,
983 currentHeight,
984 0, // border
985 externalFormat, externalType,
986 currentMipData));
Brian Salomon0ec981b2017-05-15 13:48:50 -0400987 GrGLenum error = CHECK_ALLOC_ERROR(&interface);
bsalomone699d0c2016-03-09 06:25:15 -0800988 if (error != GR_GL_NO_ERROR) {
989 return false;
990 }
cblume790d5132016-02-29 11:13:29 -0800991 }
cblume55f2d2d2016-02-26 13:20:48 -0800992 }
993 }
bsalomone699d0c2016-03-09 06:25:15 -0800994 return true;
cblume55f2d2d2016-02-26 13:20:48 -0800995}
996
997/**
cblume55f2d2d2016-02-26 13:20:48 -0800998 * After a texture is created, any state which was altered during its creation
999 * needs to be restored.
1000 *
1001 * @param interface The GL interface to use.
1002 * @param caps The capabilities of the GL device.
1003 * @param restoreGLRowLength Should the row length unpacking be restored?
1004 * @param glFlipY Did GL flip the texture vertically?
1005 */
1006static void restore_pixelstore_state(const GrGLInterface& interface, const GrGLCaps& caps,
1007 bool restoreGLRowLength, bool glFlipY) {
1008 if (restoreGLRowLength) {
1009 SkASSERT(caps.unpackRowLengthSupport());
1010 GR_GL_CALL(&interface, PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
1011 }
1012 if (glFlipY) {
1013 GR_GL_CALL(&interface, PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
1014 }
1015}
1016
Brian Salomond1eaf492017-05-18 10:02:08 -04001017bool GrGLGpu::uploadTexData(GrPixelConfig texConfig, int texWidth, int texHeight,
1018 GrSurfaceOrigin texOrigin, GrGLenum target, UploadType uploadType,
1019 int left, int top, int width, int height, GrPixelConfig dataConfig,
Robert Phillips590533f2017-07-11 14:22:35 -04001020 const GrMipLevel texels[], int mipLevelCount) {
Brian Salomond1eaf492017-05-18 10:02:08 -04001021 SkASSERT(this->caps()->isConfigTexturable(texConfig));
Greg Daniel660cc992017-06-26 14:55:05 -04001022 SkDEBUGCODE(
1023 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
1024 SkIRect bounds = SkIRect::MakeWH(texWidth, texHeight);
1025 SkASSERT(bounds.contains(subRect));
1026 )
Robert Phillips590533f2017-07-11 14:22:35 -04001027 SkASSERT(1 == mipLevelCount ||
Greg Daniel660cc992017-06-26 14:55:05 -04001028 (0 == left && 0 == top && width == texWidth && height == texHeight));
bsalomon5b30c6f2015-12-17 14:17:34 -08001029
Jim Van Verth2e5eaf02017-06-21 15:55:46 -04001030 // unbind any previous transfer buffer
1031 auto& xferBufferState = fHWBufferState[kXferCpuToGpu_GrBufferType];
1032 if (!xferBufferState.fBoundBufferUniqueID.isInvalid()) {
1033 GL_CALL(BindBuffer(xferBufferState.fGLTarget, 0));
1034 xferBufferState.invalidate();
1035 }
1036
cblume55f2d2d2016-02-26 13:20:48 -08001037 // texels is const.
1038 // But we may need to flip the texture vertically to prepare it.
1039 // Rather than flip in place and alter the incoming data,
1040 // we allocate a new buffer to flip into.
1041 // This means we need to make a non-const shallow copy of texels.
Robert Phillips0f992772017-07-12 08:24:56 -04001042 SkAutoTMalloc<GrMipLevel> texelsShallowCopy;
1043
1044 if (mipLevelCount) {
1045 texelsShallowCopy.reset(mipLevelCount);
1046 memcpy(texelsShallowCopy.get(), texels, mipLevelCount*sizeof(GrMipLevel));
1047 }
cblume55f2d2d2016-02-26 13:20:48 -08001048
Robert Phillips590533f2017-07-11 14:22:35 -04001049 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; ++currentMipLevel) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -04001050 SkASSERT(texelsShallowCopy[currentMipLevel].fPixels);
cblume55f2d2d2016-02-26 13:20:48 -08001051 }
1052
1053 const GrGLInterface* interface = this->glInterface();
1054 const GrGLCaps& caps = this->glCaps();
1055
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001056 size_t bpp = GrBytesPerPixel(dataConfig);
cblume55f2d2d2016-02-26 13:20:48 -08001057
1058 if (width == 0 || height == 0) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +00001059 return false;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001060 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00001061
bsalomon5b30c6f2015-12-17 14:17:34 -08001062 // Internal format comes from the texture desc.
bsalomon76148af2016-01-12 11:13:47 -08001063 GrGLenum internalFormat;
bsalomon5b30c6f2015-12-17 14:17:34 -08001064 // External format and type come from the upload data.
bsalomon76148af2016-01-12 11:13:47 -08001065 GrGLenum externalFormat;
1066 GrGLenum externalType;
Brian Salomond1eaf492017-05-18 10:02:08 -04001067 if (!this->glCaps().getTexImageFormats(texConfig, dataConfig, &internalFormat, &externalFormat,
1068 &externalType)) {
bsalomon76148af2016-01-12 11:13:47 -08001069 return false;
1070 }
brianosman81a84852016-09-12 09:05:14 -07001071 // TexStorage requires a sized format, and internalFormat may or may not be
Brian Salomond1eaf492017-05-18 10:02:08 -04001072 GrGLenum internalFormatForTexStorage = this->glCaps().configSizedInternalFormat(texConfig);
brianosman81a84852016-09-12 09:05:14 -07001073
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001074 /*
bsalomon5b30c6f2015-12-17 14:17:34 -08001075 * Check whether to allocate a temporary buffer for flipping y or
bsalomon@google.com6f379512011-11-16 20:36:03 +00001076 * because our srcData has extra bytes past each row. If so, we need
1077 * to trim those off here, since GL ES may not let us specify
1078 * GL_UNPACK_ROW_LENGTH.
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001079 */
bsalomon@google.com6f379512011-11-16 20:36:03 +00001080 bool restoreGLRowLength = false;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +00001081 bool swFlipY = false;
1082 bool glFlipY = false;
cblume55f2d2d2016-02-26 13:20:48 -08001083
Robert Phillips590533f2017-07-11 14:22:35 -04001084 if (kBottomLeft_GrSurfaceOrigin == texOrigin && mipLevelCount) {
cblume55f2d2d2016-02-26 13:20:48 -08001085 if (caps.unpackFlipYSupport()) {
1086 glFlipY = true;
1087 } else {
1088 swFlipY = true;
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001089 }
cblume55f2d2d2016-02-26 13:20:48 -08001090 }
1091
1092 // in case we need a temporary, trimmed copy of the src pixels
1093 SkAutoSMalloc<128 * 128> tempStorage;
1094
1095 // find the combined size of all the mip levels and the relative offset of
1096 // each into the collective buffer
1097 size_t combined_buffer_size = 0;
Robert Phillips590533f2017-07-11 14:22:35 -04001098 SkTArray<size_t> individual_mip_offsets(mipLevelCount);
1099 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
cblume55f2d2d2016-02-26 13:20:48 -08001100 int twoToTheMipLevel = 1 << currentMipLevel;
1101 int currentWidth = SkTMax(1, width / twoToTheMipLevel);
1102 int currentHeight = SkTMax(1, height / twoToTheMipLevel);
1103 const size_t trimmedSize = currentWidth * bpp * currentHeight;
1104 individual_mip_offsets.push_back(combined_buffer_size);
1105 combined_buffer_size += trimmedSize;
1106 }
1107 char* buffer = (char*)tempStorage.reset(combined_buffer_size);
1108
Robert Phillips590533f2017-07-11 14:22:35 -04001109 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
cblume55f2d2d2016-02-26 13:20:48 -08001110 int twoToTheMipLevel = 1 << currentMipLevel;
1111 int currentWidth = SkTMax(1, width / twoToTheMipLevel);
1112 int currentHeight = SkTMax(1, height / twoToTheMipLevel);
1113 const size_t trimRowBytes = currentWidth * bpp;
1114
1115 /*
1116 * check whether to allocate a temporary buffer for flipping y or
1117 * because our srcData has extra bytes past each row. If so, we need
1118 * to trim those off here, since GL ES may not let us specify
1119 * GL_UNPACK_ROW_LENGTH.
1120 */
1121 restoreGLRowLength = false;
1122
Greg Daniel660cc992017-06-26 14:55:05 -04001123 const size_t rowBytes = texelsShallowCopy[currentMipLevel].fRowBytes ?
1124 texelsShallowCopy[currentMipLevel].fRowBytes :
1125 trimRowBytes;
1126
ericrk154349b2016-05-10 14:36:53 -07001127 // TODO: This optimization should be enabled with or without mips.
1128 // For use with mips, we must set GR_GL_UNPACK_ROW_LENGTH once per
1129 // mip level, before calling glTexImage2D.
Robert Phillips590533f2017-07-11 14:22:35 -04001130 const bool usesMips = mipLevelCount > 1;
ericrk154349b2016-05-10 14:36:53 -07001131 if (caps.unpackRowLengthSupport() && !swFlipY && !usesMips) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001132 // can't use this for flipping, only non-neg values allowed. :(
1133 if (rowBytes != trimRowBytes) {
1134 GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
cblume55f2d2d2016-02-26 13:20:48 -08001135 GR_GL_CALL(interface, PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength));
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001136 restoreGLRowLength = true;
1137 }
Jim Van Verth2e5eaf02017-06-21 15:55:46 -04001138 } else if (trimRowBytes != rowBytes || swFlipY) {
1139 // copy data into our new storage, skipping the trailing bytes
1140 const char* src = (const char*)texelsShallowCopy[currentMipLevel].fPixels;
1141 if (swFlipY && currentHeight >= 1) {
1142 src += (currentHeight - 1) * rowBytes;
Jim Van Verthf7037d02017-06-21 10:52:43 -04001143 }
Jim Van Verth2e5eaf02017-06-21 15:55:46 -04001144 char* dst = buffer + individual_mip_offsets[currentMipLevel];
1145 for (int y = 0; y < currentHeight; y++) {
1146 memcpy(dst, src, trimRowBytes);
1147 if (swFlipY) {
1148 src -= rowBytes;
1149 } else {
1150 src += rowBytes;
1151 }
1152 dst += trimRowBytes;
1153 }
1154 // now point data to our copied version
1155 texelsShallowCopy[currentMipLevel].fPixels = buffer +
1156 individual_mip_offsets[currentMipLevel];
1157 texelsShallowCopy[currentMipLevel].fRowBytes = trimRowBytes;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001158 }
bsalomone699d0c2016-03-09 06:25:15 -08001159 }
1160
Robert Phillips590533f2017-07-11 14:22:35 -04001161 if (mipLevelCount) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001162 if (glFlipY) {
cblume55f2d2d2016-02-26 13:20:48 -08001163 GR_GL_CALL(interface, PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_TRUE));
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001164 }
Brian Salomond1eaf492017-05-18 10:02:08 -04001165 GR_GL_CALL(interface, PixelStorei(GR_GL_UNPACK_ALIGNMENT, config_alignment(texConfig)));
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001166 }
cblume55f2d2d2016-02-26 13:20:48 -08001167
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001168 bool succeeded = true;
Brian Salomond1eaf492017-05-18 10:02:08 -04001169 if (kNewTexture_UploadType == uploadType) {
1170 if (0 == left && 0 == top && texWidth == width && texHeight == height) {
Robert Phillips92de6312017-05-23 07:43:48 -04001171 succeeded = allocate_and_populate_texture(
Brian Salomond1eaf492017-05-18 10:02:08 -04001172 texConfig, *interface, caps, target, internalFormat,
Robert Phillips590533f2017-07-11 14:22:35 -04001173 internalFormatForTexStorage, externalFormat, externalType,
1174 texelsShallowCopy, mipLevelCount, width, height);
Brian Salomond1eaf492017-05-18 10:02:08 -04001175 } else {
1176 succeeded = false;
1177 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001178 } else {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +00001179 if (swFlipY || glFlipY) {
Brian Salomond1eaf492017-05-18 10:02:08 -04001180 top = texHeight - (top + height);
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001181 }
Robert Phillips590533f2017-07-11 14:22:35 -04001182 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
cblume55f2d2d2016-02-26 13:20:48 -08001183 int twoToTheMipLevel = 1 << currentMipLevel;
1184 int currentWidth = SkTMax(1, width / twoToTheMipLevel);
1185 int currentHeight = SkTMax(1, height / twoToTheMipLevel);
cblume55f2d2d2016-02-26 13:20:48 -08001186
1187 GL_CALL(TexSubImage2D(target,
1188 currentMipLevel,
1189 left, top,
1190 currentWidth,
1191 currentHeight,
1192 externalFormat, externalType,
1193 texelsShallowCopy[currentMipLevel].fPixels));
1194 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00001195 }
1196
cblume55f2d2d2016-02-26 13:20:48 -08001197 restore_pixelstore_state(*interface, caps, restoreGLRowLength, glFlipY);
1198
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001199 return succeeded;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001200}
1201
bsalomon424cc262015-05-22 10:37:30 -07001202static bool renderbuffer_storage_msaa(const GrGLContext& ctx,
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001203 int sampleCount,
1204 GrGLenum format,
1205 int width, int height) {
robertphillips@google.com6177e692013-02-28 20:16:25 +00001206 CLEAR_ERROR_BEFORE_ALLOC(ctx.interface());
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +00001207 SkASSERT(GrGLCaps::kNone_MSFBOType != ctx.caps()->msFBOType());
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +00001208 switch (ctx.caps()->msFBOType()) {
Brian Salomon00731b42016-10-14 11:30:51 -04001209 case GrGLCaps::kStandard_MSFBOType:
vbuzinovdded6962015-06-12 08:59:45 -07001210 case GrGLCaps::kMixedSamples_MSFBOType:
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001211 GL_ALLOC_CALL(ctx.interface(),
1212 RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
1213 sampleCount,
1214 format,
1215 width, height));
1216 break;
1217 case GrGLCaps::kES_Apple_MSFBOType:
1218 GL_ALLOC_CALL(ctx.interface(),
1219 RenderbufferStorageMultisampleES2APPLE(GR_GL_RENDERBUFFER,
1220 sampleCount,
1221 format,
1222 width, height));
1223 break;
1224 case GrGLCaps::kES_EXT_MsToTexture_MSFBOType:
1225 case GrGLCaps::kES_IMG_MsToTexture_MSFBOType:
1226 GL_ALLOC_CALL(ctx.interface(),
1227 RenderbufferStorageMultisampleES2EXT(GR_GL_RENDERBUFFER,
1228 sampleCount,
1229 format,
1230 width, height));
1231 break;
1232 case GrGLCaps::kNone_MSFBOType:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +00001233 SkFAIL("Shouldn't be here if we don't support multisampled renderbuffers.");
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001234 break;
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001235 }
Brian Salomon9251d4e2015-03-17 16:03:19 -04001236 return (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctx.interface()));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001237}
1238
egdanielb0e1be22015-04-22 13:27:39 -07001239bool GrGLGpu::createRenderTargetObjects(const GrSurfaceDesc& desc,
bsalomon091f60c2015-11-10 11:54:56 -08001240 const GrGLTextureInfo& texInfo,
bsalomonb15b4c12014-10-29 12:41:57 -07001241 GrGLRenderTarget::IDDesc* idDesc) {
1242 idDesc->fMSColorRenderbufferID = 0;
egdanield803f272015-03-18 13:01:52 -07001243 idDesc->fRTFBOID = 0;
kkinnunen2e6055b2016-04-22 01:48:29 -07001244 idDesc->fRTFBOOwnership = GrBackendObjectOwnership::kOwned;
egdanield803f272015-03-18 13:01:52 -07001245 idDesc->fTexFBOID = 0;
robertphillips76948d42016-05-04 12:47:41 -07001246 SkASSERT((GrGLCaps::kMixedSamples_MSFBOType == this->glCaps().msFBOType()) ==
1247 this->caps()->usesMixedSamples());
csmartdaltonf9635992016-08-10 11:09:07 -07001248 idDesc->fIsMixedSampled = desc.fSampleCnt > 0 && this->caps()->usesMixedSamples();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001249
1250 GrGLenum status;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001251
bsalomona11e5fc2015-12-18 07:59:41 -08001252 GrGLenum colorRenderbufferFormat = 0; // suppress warning
bsalomon@google.comab15d612011-08-09 12:57:56 +00001253
bsalomonb15b4c12014-10-29 12:41:57 -07001254 if (desc.fSampleCnt > 0 && GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.com347c3822013-05-01 20:10:01 +00001255 goto FAILED;
1256 }
1257
egdanield803f272015-03-18 13:01:52 -07001258 GL_CALL(GenFramebuffers(1, &idDesc->fTexFBOID));
1259 if (!idDesc->fTexFBOID) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001260 goto FAILED;
1261 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001262
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001263 // If we are using multisampling we will create two FBOS. We render to one and then resolve to
1264 // the texture bound to the other. The exception is the IMG multisample extension. With this
1265 // extension the texture is multisampled when rendered to and then auto-resolves it when it is
1266 // rendered from.
bsalomonb15b4c12014-10-29 12:41:57 -07001267 if (desc.fSampleCnt > 0 && this->glCaps().usesMSAARenderBuffers()) {
egdanield803f272015-03-18 13:01:52 -07001268 GL_CALL(GenFramebuffers(1, &idDesc->fRTFBOID));
bsalomonb15b4c12014-10-29 12:41:57 -07001269 GL_CALL(GenRenderbuffers(1, &idDesc->fMSColorRenderbufferID));
egdanield803f272015-03-18 13:01:52 -07001270 if (!idDesc->fRTFBOID ||
bsalomona11e5fc2015-12-18 07:59:41 -08001271 !idDesc->fMSColorRenderbufferID) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001272 goto FAILED;
1273 }
bsalomon76148af2016-01-12 11:13:47 -08001274 if (!this->glCaps().getRenderbufferFormat(desc.fConfig, &colorRenderbufferFormat)) {
1275 return false;
1276 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001277 } else {
egdanield803f272015-03-18 13:01:52 -07001278 idDesc->fRTFBOID = idDesc->fTexFBOID;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001279 }
1280
egdanield803f272015-03-18 13:01:52 -07001281 // below here we may bind the FBO
Robert Phillips294870f2016-11-11 12:38:40 -05001282 fHWBoundRenderTargetUniqueID.makeInvalid();
egdanield803f272015-03-18 13:01:52 -07001283 if (idDesc->fRTFBOID != idDesc->fTexFBOID) {
bsalomonb15b4c12014-10-29 12:41:57 -07001284 SkASSERT(desc.fSampleCnt > 0);
1285 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, idDesc->fMSColorRenderbufferID));
bsalomon424cc262015-05-22 10:37:30 -07001286 if (!renderbuffer_storage_msaa(*fGLContext,
bsalomonb15b4c12014-10-29 12:41:57 -07001287 desc.fSampleCnt,
bsalomona11e5fc2015-12-18 07:59:41 -08001288 colorRenderbufferFormat,
bsalomonb15b4c12014-10-29 12:41:57 -07001289 desc.fWidth, desc.fHeight)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001290 goto FAILED;
1291 }
egdanield803f272015-03-18 13:01:52 -07001292 fStats.incRenderTargetBinds();
1293 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, idDesc->fRTFBOID));
1294 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon10528f12015-10-14 12:54:52 -07001295 GR_GL_COLOR_ATTACHMENT0,
1296 GR_GL_RENDERBUFFER,
1297 idDesc->fMSColorRenderbufferID));
Brian Salomon0ec981b2017-05-15 13:48:50 -04001298 if (!this->glCaps().isConfigVerifiedColorAttachment(desc.fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001299 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1300 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
1301 goto FAILED;
1302 }
bsalomon424cc262015-05-22 10:37:30 -07001303 fGLContext->caps()->markConfigAsValidColorAttachment(desc.fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001304 }
1305 }
egdanield803f272015-03-18 13:01:52 -07001306 fStats.incRenderTargetBinds();
1307 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, idDesc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001308
bsalomonb15b4c12014-10-29 12:41:57 -07001309 if (this->glCaps().usesImplicitMSAAResolve() && desc.fSampleCnt > 0) {
egdanield803f272015-03-18 13:01:52 -07001310 GL_CALL(FramebufferTexture2DMultisample(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001311 GR_GL_COLOR_ATTACHMENT0,
bsalomon091f60c2015-11-10 11:54:56 -08001312 texInfo.fTarget,
1313 texInfo.fID, 0, desc.fSampleCnt));
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001314 } else {
egdanield803f272015-03-18 13:01:52 -07001315 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001316 GR_GL_COLOR_ATTACHMENT0,
bsalomon091f60c2015-11-10 11:54:56 -08001317 texInfo.fTarget,
1318 texInfo.fID, 0));
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001319 }
Brian Salomon0ec981b2017-05-15 13:48:50 -04001320 if (!this->glCaps().isConfigVerifiedColorAttachment(desc.fConfig)) {
egdanield803f272015-03-18 13:01:52 -07001321 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001322 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
1323 goto FAILED;
1324 }
bsalomon424cc262015-05-22 10:37:30 -07001325 fGLContext->caps()->markConfigAsValidColorAttachment(desc.fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001326 }
1327
1328 return true;
1329
1330FAILED:
bsalomonb15b4c12014-10-29 12:41:57 -07001331 if (idDesc->fMSColorRenderbufferID) {
1332 GL_CALL(DeleteRenderbuffers(1, &idDesc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001333 }
egdanield803f272015-03-18 13:01:52 -07001334 if (idDesc->fRTFBOID != idDesc->fTexFBOID) {
1335 GL_CALL(DeleteFramebuffers(1, &idDesc->fRTFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001336 }
egdanield803f272015-03-18 13:01:52 -07001337 if (idDesc->fTexFBOID) {
1338 GL_CALL(DeleteFramebuffers(1, &idDesc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001339 }
1340 return false;
1341}
1342
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001343// good to set a break-point here to know when createTexture fails
Robert Phillips67d52cf2017-06-05 13:38:13 -04001344static sk_sp<GrTexture> return_null_texture() {
mtklein@google.com330313a2013-08-22 15:37:26 +00001345// SkDEBUGFAIL("null texture");
halcanary96fcdcc2015-08-27 07:41:13 -07001346 return nullptr;
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001347}
1348
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +00001349#if 0 && defined(SK_DEBUG)
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001350static size_t as_size_t(int x) {
1351 return x;
1352}
1353#endif
1354
cblume55f2d2d2016-02-26 13:20:48 -08001355static void set_initial_texture_params(const GrGLInterface* interface,
1356 const GrGLTextureInfo& info,
1357 GrGLTexture::TexParams* initialTexParams) {
1358 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
1359 // drivers have a bug where an FBO won't be complete if it includes a
1360 // texture that is not mipmap complete (considering the filter in use).
1361 // we only set a subset here so invalidate first
1362 initialTexParams->invalidate();
1363 initialTexParams->fMinFilter = GR_GL_NEAREST;
1364 initialTexParams->fMagFilter = GR_GL_NEAREST;
1365 initialTexParams->fWrapS = GR_GL_CLAMP_TO_EDGE;
1366 initialTexParams->fWrapT = GR_GL_CLAMP_TO_EDGE;
1367 GR_GL_CALL(interface, TexParameteri(info.fTarget,
1368 GR_GL_TEXTURE_MAG_FILTER,
1369 initialTexParams->fMagFilter));
1370 GR_GL_CALL(interface, TexParameteri(info.fTarget,
1371 GR_GL_TEXTURE_MIN_FILTER,
1372 initialTexParams->fMinFilter));
1373 GR_GL_CALL(interface, TexParameteri(info.fTarget,
1374 GR_GL_TEXTURE_WRAP_S,
1375 initialTexParams->fWrapS));
1376 GR_GL_CALL(interface, TexParameteri(info.fTarget,
1377 GR_GL_TEXTURE_WRAP_T,
1378 initialTexParams->fWrapT));
1379}
1380
Robert Phillips67d52cf2017-06-05 13:38:13 -04001381sk_sp<GrTexture> GrGLGpu::onCreateTexture(const GrSurfaceDesc& desc,
1382 SkBudgeted budgeted,
Robert Phillips590533f2017-07-11 14:22:35 -04001383 const GrMipLevel texels[],
1384 int mipLevelCount) {
bsalomon@google.com8a70eef2013-03-19 13:58:55 +00001385 // We fail if the MSAA was requested and is not available.
1386 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType() && desc.fSampleCnt) {
tfarina38406c82014-10-31 07:11:12 -07001387 //SkDebugf("MSAA RT requested but not supported on this platform.");
bsalomon@google.com8a70eef2013-03-19 13:58:55 +00001388 return return_null_texture();
1389 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001390
Brian Salomond17b4a62017-05-23 16:53:47 -04001391 bool performClear = (desc.fFlags & kPerformInitialClear_GrSurfaceFlag);
Brian Salomond17b4a62017-05-23 16:53:47 -04001392
Robert Phillips590533f2017-07-11 14:22:35 -04001393 GrMipLevel zeroLevel;
Brian Salomond17b4a62017-05-23 16:53:47 -04001394 std::unique_ptr<uint8_t[]> zeros;
1395 // TODO: remove the GrPixelConfigIsSint test. This is here because we have yet to add support
1396 // for glClearBuffer* which must be used instead of glClearColor/glClear for integer FBO
1397 // attachments.
1398 if (performClear && !this->glCaps().clearTextureSupport() &&
1399 (!this->glCaps().canConfigBeFBOColorAttachment(desc.fConfig) ||
1400 GrPixelConfigIsSint(desc.fConfig))) {
1401 size_t rowSize = GrBytesPerPixel(desc.fConfig) * desc.fWidth;
1402 size_t size = rowSize * desc.fHeight;
1403 zeros.reset(new uint8_t[size]);
1404 memset(zeros.get(), 0, size);
Robert Phillips590533f2017-07-11 14:22:35 -04001405 zeroLevel.fPixels = zeros.get();
1406 zeroLevel.fRowBytes = 0;
1407 texels = &zeroLevel;
1408 mipLevelCount = 1;
Brian Salomond17b4a62017-05-23 16:53:47 -04001409 performClear = false;
1410 }
1411
Brian Salomond34edf32017-05-19 15:45:48 -04001412 bool isRenderTarget = SkToBool(desc.fFlags & kRenderTarget_GrSurfaceFlag);
reed@google.comac10a2d2010-12-22 21:39:39 +00001413
bsalomonb15b4c12014-10-29 12:41:57 -07001414 GrGLTexture::IDDesc idDesc;
kkinnunen2e6055b2016-04-22 01:48:29 -07001415 idDesc.fOwnership = GrBackendObjectOwnership::kOwned;
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001416 GrGLTexture::TexParams initialTexParams;
Robert Phillips590533f2017-07-11 14:22:35 -04001417 if (!this->createTextureImpl(desc, &idDesc.fInfo, isRenderTarget, &initialTexParams,
1418 texels, mipLevelCount)) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001419 return return_null_texture();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001420 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001421
Robert Phillipsd6214d42016-11-07 08:23:48 -05001422 bool wasMipMapDataProvided = false;
Robert Phillips590533f2017-07-11 14:22:35 -04001423 if (mipLevelCount > 1) {
Robert Phillipsd6214d42016-11-07 08:23:48 -05001424 wasMipMapDataProvided = true;
1425 }
1426
Robert Phillips67d52cf2017-06-05 13:38:13 -04001427 sk_sp<GrGLTexture> tex;
Brian Salomond34edf32017-05-19 15:45:48 -04001428 if (isRenderTarget) {
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001429 // unbind the texture from the texture unit before binding it to the frame buffer
bsalomon091f60c2015-11-10 11:54:56 -08001430 GL_CALL(BindTexture(idDesc.fInfo.fTarget, 0));
bsalomon5236cf42015-01-14 10:42:08 -08001431 GrGLRenderTarget::IDDesc rtIDDesc;
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001432
kkinnunen2e6055b2016-04-22 01:48:29 -07001433 if (!this->createRenderTargetObjects(desc, idDesc.fInfo, &rtIDDesc)) {
bsalomon091f60c2015-11-10 11:54:56 -08001434 GL_CALL(DeleteTextures(1, &idDesc.fInfo.fID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001435 return return_null_texture();
1436 }
Robert Phillips67d52cf2017-06-05 13:38:13 -04001437 tex = sk_make_sp<GrGLTextureRenderTarget>(this, budgeted, desc, idDesc, rtIDDesc,
1438 wasMipMapDataProvided);
Brian Salomon9bada542017-06-12 12:09:30 -04001439 tex->baseLevelWasBoundToFBO();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001440 } else {
Robert Phillips67d52cf2017-06-05 13:38:13 -04001441 tex = sk_make_sp<GrGLTexture>(this, budgeted, desc, idDesc, wasMipMapDataProvided);
reed@google.comac10a2d2010-12-22 21:39:39 +00001442 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001443 tex->setCachedTexParams(initialTexParams, this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00001444#ifdef TRACE_TEXTURE_CREATION
tfarina38406c82014-10-31 07:11:12 -07001445 SkDebugf("--- new texture [%d] size=(%d %d) config=%d\n",
brianosmanc4459f62016-07-19 08:02:20 -07001446 idDesc.fInfo.fID, desc.fWidth, desc.fHeight, desc.fConfig);
reed@google.comac10a2d2010-12-22 21:39:39 +00001447#endif
Brian Salomond17b4a62017-05-23 16:53:47 -04001448 if (tex && performClear) {
1449 if (this->glCaps().clearTextureSupport()) {
1450 GrGLenum format = GrPixelConfigIsSint(tex->config()) ? GR_GL_RGBA_INTEGER : GR_GL_RGBA;
1451 static constexpr uint32_t kZero = 0;
1452 GL_CALL(ClearTexImage(tex->textureID(), 0, format, GR_GL_UNSIGNED_BYTE, &kZero));
1453 } else {
1454 SkASSERT(!GrPixelConfigIsSint(desc.fConfig));
1455 GrGLIRect viewport;
Robert Phillips67d52cf2017-06-05 13:38:13 -04001456 this->bindSurfaceFBOForPixelOps(tex.get(), GR_GL_FRAMEBUFFER, &viewport,
1457 kDst_TempFBOTarget);
Brian Salomond17b4a62017-05-23 16:53:47 -04001458 this->disableScissor();
1459 this->disableWindowRectangles();
1460 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
1461 fHWWriteToColor = kYes_TriState;
1462 GL_CALL(ClearColor(0, 0, 0, 0));
1463 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
Robert Phillips67d52cf2017-06-05 13:38:13 -04001464 this->unbindTextureFBOForPixelOps(GR_GL_FRAMEBUFFER, tex.get());
Brian Salomond17b4a62017-05-23 16:53:47 -04001465 fHWBoundRenderTargetUniqueID.makeInvalid();
1466 }
1467 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001468 return tex;
1469}
1470
1471namespace {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001472
egdaniel8dc7c3a2015-04-16 11:22:42 -07001473const GrGLuint kUnknownBitCount = GrGLStencilAttachment::kUnknownBitCount;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001474
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001475void inline get_stencil_rb_sizes(const GrGLInterface* gl,
egdaniel8dc7c3a2015-04-16 11:22:42 -07001476 GrGLStencilAttachment::Format* format) {
sugoi@google.com6ba0b0f2013-05-03 13:52:32 +00001477
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001478 // we shouldn't ever know one size and not the other
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001479 SkASSERT((kUnknownBitCount == format->fStencilBits) ==
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001480 (kUnknownBitCount == format->fTotalBits));
1481 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001482 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001483 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1484 (GrGLint*)&format->fStencilBits);
1485 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001486 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001487 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1488 (GrGLint*)&format->fTotalBits);
1489 format->fTotalBits += format->fStencilBits;
1490 } else {
1491 format->fTotalBits = format->fStencilBits;
1492 }
1493 }
1494}
1495}
1496
egdanielff1d5472015-09-10 08:37:20 -07001497int GrGLGpu::getCompatibleStencilIndex(GrPixelConfig config) {
bsalomon100b8f82015-10-28 08:37:44 -07001498 static const int kSize = 16;
bsalomon926cb022015-12-17 18:15:11 -08001499 SkASSERT(this->caps()->isConfigRenderable(config, false));
bsalomon30447372015-12-21 09:03:05 -08001500 if (!this->glCaps().hasStencilFormatBeenDeterminedForConfig(config)) {
1501 // Default to unsupported, set this if we find a stencil format that works.
1502 int firstWorkingStencilFormatIndex = -1;
egdanielff1d5472015-09-10 08:37:20 -07001503 // Create color texture
kkinnunen546eb5c2015-12-11 00:05:33 -08001504 GrGLuint colorID = 0;
egdanielff1d5472015-09-10 08:37:20 -07001505 GL_CALL(GenTextures(1, &colorID));
1506 this->setScratchTextureUnit();
1507 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, colorID));
1508 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1509 GR_GL_TEXTURE_MAG_FILTER,
1510 GR_GL_NEAREST));
1511 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1512 GR_GL_TEXTURE_MIN_FILTER,
1513 GR_GL_NEAREST));
1514 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1515 GR_GL_TEXTURE_WRAP_S,
1516 GR_GL_CLAMP_TO_EDGE));
1517 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1518 GR_GL_TEXTURE_WRAP_T,
1519 GR_GL_CLAMP_TO_EDGE));
1520
bsalomon76148af2016-01-12 11:13:47 -08001521 GrGLenum internalFormat;
1522 GrGLenum externalFormat;
1523 GrGLenum externalType;
1524 if (!this->glCaps().getTexImageFormats(config, config, &internalFormat, &externalFormat,
1525 &externalType)) {
1526 return false;
1527 }
egdanielff1d5472015-09-10 08:37:20 -07001528 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1529 GL_ALLOC_CALL(this->glInterface(), TexImage2D(GR_GL_TEXTURE_2D,
bsalomon926cb022015-12-17 18:15:11 -08001530 0,
bsalomon76148af2016-01-12 11:13:47 -08001531 internalFormat,
bsalomon100b8f82015-10-28 08:37:44 -07001532 kSize,
1533 kSize,
egdanielff1d5472015-09-10 08:37:20 -07001534 0,
bsalomon76148af2016-01-12 11:13:47 -08001535 externalFormat,
1536 externalType,
egdanielff1d5472015-09-10 08:37:20 -07001537 NULL));
bsalomon30447372015-12-21 09:03:05 -08001538 if (GR_GL_NO_ERROR != CHECK_ALLOC_ERROR(this->glInterface())) {
egdanielff1d5472015-09-10 08:37:20 -07001539 GL_CALL(DeleteTextures(1, &colorID));
bsalomon30447372015-12-21 09:03:05 -08001540 return -1;
egdanielff1d5472015-09-10 08:37:20 -07001541 }
1542
1543 // unbind the texture from the texture unit before binding it to the frame buffer
1544 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, 0));
1545
1546 // Create Framebuffer
kkinnunen546eb5c2015-12-11 00:05:33 -08001547 GrGLuint fb = 0;
egdanielff1d5472015-09-10 08:37:20 -07001548 GL_CALL(GenFramebuffers(1, &fb));
egdanielec00d942015-09-14 12:56:10 -07001549 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, fb));
Robert Phillips294870f2016-11-11 12:38:40 -05001550 fHWBoundRenderTargetUniqueID.makeInvalid();
egdanielff1d5472015-09-10 08:37:20 -07001551 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
1552 GR_GL_COLOR_ATTACHMENT0,
1553 GR_GL_TEXTURE_2D,
1554 colorID,
1555 0));
bsalomon30447372015-12-21 09:03:05 -08001556 GrGLuint sbRBID = 0;
1557 GL_CALL(GenRenderbuffers(1, &sbRBID));
egdanielff1d5472015-09-10 08:37:20 -07001558
1559 // look over formats till I find a compatible one
1560 int stencilFmtCnt = this->glCaps().stencilFormats().count();
bsalomon30447372015-12-21 09:03:05 -08001561 if (sbRBID) {
egdanielff1d5472015-09-10 08:37:20 -07001562 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbRBID));
bsalomon30447372015-12-21 09:03:05 -08001563 for (int i = 0; i < stencilFmtCnt && sbRBID; ++i) {
1564 const GrGLCaps::StencilFormat& sFmt = this->glCaps().stencilFormats()[i];
1565 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1566 GL_ALLOC_CALL(this->glInterface(), RenderbufferStorage(GR_GL_RENDERBUFFER,
1567 sFmt.fInternalFormat,
1568 kSize, kSize));
1569 if (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(this->glInterface())) {
egdanielff1d5472015-09-10 08:37:20 -07001570 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon30447372015-12-21 09:03:05 -08001571 GR_GL_STENCIL_ATTACHMENT,
egdanielff1d5472015-09-10 08:37:20 -07001572 GR_GL_RENDERBUFFER, sbRBID));
bsalomon30447372015-12-21 09:03:05 -08001573 if (sFmt.fPacked) {
1574 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1575 GR_GL_DEPTH_ATTACHMENT,
1576 GR_GL_RENDERBUFFER, sbRBID));
1577 } else {
1578 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1579 GR_GL_DEPTH_ATTACHMENT,
1580 GR_GL_RENDERBUFFER, 0));
1581 }
1582 GrGLenum status;
1583 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1584 if (status == GR_GL_FRAMEBUFFER_COMPLETE) {
1585 firstWorkingStencilFormatIndex = i;
1586 break;
1587 }
egdanielff1d5472015-09-10 08:37:20 -07001588 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1589 GR_GL_STENCIL_ATTACHMENT,
1590 GR_GL_RENDERBUFFER, 0));
1591 if (sFmt.fPacked) {
1592 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1593 GR_GL_DEPTH_ATTACHMENT,
1594 GR_GL_RENDERBUFFER, 0));
1595 }
egdanielff1d5472015-09-10 08:37:20 -07001596 }
1597 }
bsalomon30447372015-12-21 09:03:05 -08001598 GL_CALL(DeleteRenderbuffers(1, &sbRBID));
egdanielff1d5472015-09-10 08:37:20 -07001599 }
1600 GL_CALL(DeleteTextures(1, &colorID));
egdanielff1d5472015-09-10 08:37:20 -07001601 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, 0));
1602 GL_CALL(DeleteFramebuffers(1, &fb));
bsalomon30447372015-12-21 09:03:05 -08001603 fGLContext->caps()->setStencilFormatIndexForConfig(config, firstWorkingStencilFormatIndex);
egdanielff1d5472015-09-10 08:37:20 -07001604 }
bsalomon30447372015-12-21 09:03:05 -08001605 return this->glCaps().getStencilFormatIndexForConfig(config);
egdanielff1d5472015-09-10 08:37:20 -07001606}
1607
erikchen9a1ed5d2016-02-10 16:32:34 -08001608bool GrGLGpu::createTextureImpl(const GrSurfaceDesc& desc, GrGLTextureInfo* info,
cblume55f2d2d2016-02-26 13:20:48 -08001609 bool renderTarget, GrGLTexture::TexParams* initialTexParams,
Robert Phillips590533f2017-07-11 14:22:35 -04001610 const GrMipLevel texels[], int mipLevelCount) {
erikchen9a1ed5d2016-02-10 16:32:34 -08001611 info->fID = 0;
1612 info->fTarget = GR_GL_TEXTURE_2D;
1613 GL_CALL(GenTextures(1, &(info->fID)));
1614
1615 if (!info->fID) {
1616 return false;
1617 }
1618
1619 this->setScratchTextureUnit();
1620 GL_CALL(BindTexture(info->fTarget, info->fID));
1621
1622 if (renderTarget && this->glCaps().textureUsageSupport()) {
1623 // provides a hint about how this texture will be used
1624 GL_CALL(TexParameteri(info->fTarget,
1625 GR_GL_TEXTURE_USAGE,
1626 GR_GL_FRAMEBUFFER_ATTACHMENT));
1627 }
1628
cblume55f2d2d2016-02-26 13:20:48 -08001629 if (info) {
1630 set_initial_texture_params(this->glInterface(), *info, initialTexParams);
1631 }
Brian Salomond1eaf492017-05-18 10:02:08 -04001632 if (!this->uploadTexData(desc.fConfig, desc.fWidth, desc.fHeight, desc.fOrigin, info->fTarget,
1633 kNewTexture_UploadType, 0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
Robert Phillips590533f2017-07-11 14:22:35 -04001634 texels, mipLevelCount)) {
erikchen9a1ed5d2016-02-10 16:32:34 -08001635 GL_CALL(DeleteTextures(1, &(info->fID)));
1636 return false;
1637 }
1638 return true;
1639}
1640
egdanielec00d942015-09-14 12:56:10 -07001641GrStencilAttachment* GrGLGpu::createStencilAttachmentForRenderTarget(const GrRenderTarget* rt,
Brian Salomond1eaf492017-05-18 10:02:08 -04001642 int width, int height) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001643 SkASSERT(width >= rt->width());
1644 SkASSERT(height >= rt->height());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001645
vbuzinovdded6962015-06-12 08:59:45 -07001646 int samples = rt->numStencilSamples();
egdaniel8dc7c3a2015-04-16 11:22:42 -07001647 GrGLStencilAttachment::IDDesc sbDesc;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001648
egdanielff1d5472015-09-10 08:37:20 -07001649 int sIdx = this->getCompatibleStencilIndex(rt->config());
bsalomon62a627b2015-12-17 09:50:47 -08001650 if (sIdx < 0) {
egdanielec00d942015-09-14 12:56:10 -07001651 return nullptr;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001652 }
egdanielff1d5472015-09-10 08:37:20 -07001653
1654 if (!sbDesc.fRenderbufferID) {
1655 GL_CALL(GenRenderbuffers(1, &sbDesc.fRenderbufferID));
1656 }
1657 if (!sbDesc.fRenderbufferID) {
egdanielec00d942015-09-14 12:56:10 -07001658 return nullptr;
egdanielff1d5472015-09-10 08:37:20 -07001659 }
1660 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbDesc.fRenderbufferID));
1661 const GrGLCaps::StencilFormat& sFmt = this->glCaps().stencilFormats()[sIdx];
1662 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1663 // we do this "if" so that we don't call the multisample
1664 // version on a GL that doesn't have an MSAA extension.
1665 if (samples > 0) {
1666 SkAssertResult(renderbuffer_storage_msaa(*fGLContext,
1667 samples,
1668 sFmt.fInternalFormat,
1669 width, height));
1670 } else {
1671 GL_ALLOC_CALL(this->glInterface(), RenderbufferStorage(GR_GL_RENDERBUFFER,
1672 sFmt.fInternalFormat,
1673 width, height));
Brian Salomon0ec981b2017-05-15 13:48:50 -04001674 SkASSERT(GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(this->glInterface()));
egdanielff1d5472015-09-10 08:37:20 -07001675 }
1676 fStats.incStencilAttachmentCreates();
1677 // After sized formats we attempt an unsized format and take
1678 // whatever sizes GL gives us. In that case we query for the size.
1679 GrGLStencilAttachment::Format format = sFmt;
1680 get_stencil_rb_sizes(this->glInterface(), &format);
egdanielec00d942015-09-14 12:56:10 -07001681 GrGLStencilAttachment* stencil = new GrGLStencilAttachment(this,
1682 sbDesc,
1683 width,
1684 height,
1685 samples,
1686 format);
1687 return stencil;
reed@google.comac10a2d2010-12-22 21:39:39 +00001688}
1689
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001690////////////////////////////////////////////////////////////////////////////////
1691
jvanverth73063dc2015-12-03 09:15:47 -08001692// GL_STREAM_DRAW triggers an optimization in Chromium's GPU process where a client's vertex buffer
1693// objects are implemented as client-side-arrays on tile-deferred architectures.
1694#define DYNAMIC_USAGE_PARAM GR_GL_STREAM_DRAW
1695
cdaltone2e71c22016-04-07 18:13:29 -07001696GrBuffer* GrGLGpu::onCreateBuffer(size_t size, GrBufferType intendedType,
cdalton1bf3e712016-04-19 10:00:02 -07001697 GrAccessPattern accessPattern, const void* data) {
1698 return GrGLBuffer::Create(this, size, intendedType, accessPattern, data);
jvanverth73063dc2015-12-03 09:15:47 -08001699}
1700
Robert Phillipse3302df2017-04-24 07:31:02 -04001701
1702std::unique_ptr<OpAllocator> GrGLGpu::onCreateInstancedRenderingAllocator() {
1703 return std::unique_ptr<OpAllocator>(new GLOpAllocator(this->caps()));
1704}
1705
csmartdaltone0d36292016-07-29 08:14:20 -07001706InstancedRendering* GrGLGpu::onCreateInstancedRendering() {
1707 return new GLInstancedRendering(this);
1708}
1709
bsalomon3e791242014-12-17 13:43:13 -08001710void GrGLGpu::flushScissor(const GrScissorState& scissorState,
joshualitt77b13072014-10-27 14:51:01 -07001711 const GrGLIRect& rtViewport,
1712 GrSurfaceOrigin rtOrigin) {
robertphillipse85a32d2015-02-10 08:16:55 -08001713 if (scissorState.enabled()) {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001714 GrGLIRect scissor;
bsalomonb0bd4f62014-09-03 07:19:50 -07001715 scissor.setRelativeTo(rtViewport,
robertphillipse85a32d2015-02-10 08:16:55 -08001716 scissorState.rect().fLeft,
1717 scissorState.rect().fTop,
1718 scissorState.rect().width(),
1719 scissorState.rect().height(),
bsalomonb0bd4f62014-09-03 07:19:50 -07001720 rtOrigin);
bsalomon@google.coma3201942012-06-21 19:58:20 +00001721 // if the scissor fully contains the viewport then we fall through and
1722 // disable the scissor test.
bsalomonb0bd4f62014-09-03 07:19:50 -07001723 if (!scissor.contains(rtViewport)) {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001724 if (fHWScissorSettings.fRect != scissor) {
1725 scissor.pushToGLScissor(this->glInterface());
1726 fHWScissorSettings.fRect = scissor;
1727 }
1728 if (kYes_TriState != fHWScissorSettings.fEnabled) {
1729 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1730 fHWScissorSettings.fEnabled = kYes_TriState;
1731 }
1732 return;
1733 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001734 }
joshualitt77b13072014-10-27 14:51:01 -07001735
1736 // See fall through note above
1737 this->disableScissor();
1738}
1739
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001740void GrGLGpu::flushWindowRectangles(const GrWindowRectsState& windowState,
1741 const GrGLRenderTarget* rt) {
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001742#ifndef USE_NSIGHT
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001743 typedef GrWindowRectsState::Mode Mode;
1744 SkASSERT(!windowState.enabled() || rt->renderFBOID()); // Window rects can't be used on-screen.
1745 SkASSERT(windowState.numWindows() <= this->caps()->maxWindowRectangles());
csmartdalton28341fa2016-08-17 10:00:21 -07001746
1747 if (!this->caps()->maxWindowRectangles() ||
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001748 fHWWindowRectsState.knownEqualTo(rt->origin(), rt->getViewport(), windowState)) {
csmartdalton28341fa2016-08-17 10:00:21 -07001749 return;
csmartdalton28341fa2016-08-17 10:00:21 -07001750 }
1751
csmartdalton7535f412016-08-23 06:51:00 -07001752 // This is purely a workaround for a spurious warning generated by gcc. Otherwise the above
1753 // assert would be sufficient. https://gcc.gnu.org/bugzilla/show_bug.cgi?id=5912
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001754 int numWindows = SkTMin(windowState.numWindows(), int(GrWindowRectangles::kMaxWindows));
1755 SkASSERT(windowState.numWindows() == numWindows);
csmartdalton7535f412016-08-23 06:51:00 -07001756
csmartdalton28341fa2016-08-17 10:00:21 -07001757 GrGLIRect glwindows[GrWindowRectangles::kMaxWindows];
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001758 const SkIRect* skwindows = windowState.windows().data();
csmartdalton7535f412016-08-23 06:51:00 -07001759 for (int i = 0; i < numWindows; ++i) {
Brian Salomon9a767722017-03-13 17:57:28 -04001760 glwindows[i].setRelativeTo(rt->getViewport(), skwindows[i], rt->origin());
csmartdalton28341fa2016-08-17 10:00:21 -07001761 }
1762
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001763 GrGLenum glmode = (Mode::kExclusive == windowState.mode()) ? GR_GL_EXCLUSIVE : GR_GL_INCLUSIVE;
csmartdalton7535f412016-08-23 06:51:00 -07001764 GL_CALL(WindowRectangles(glmode, numWindows, glwindows->asInts()));
csmartdalton28341fa2016-08-17 10:00:21 -07001765
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001766 fHWWindowRectsState.set(rt->origin(), rt->getViewport(), windowState);
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001767#endif
csmartdalton28341fa2016-08-17 10:00:21 -07001768}
1769
1770void GrGLGpu::disableWindowRectangles() {
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001771#ifndef USE_NSIGHT
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001772 if (!this->caps()->maxWindowRectangles() || fHWWindowRectsState.knownDisabled()) {
csmartdalton28341fa2016-08-17 10:00:21 -07001773 return;
1774 }
1775 GL_CALL(WindowRectangles(GR_GL_EXCLUSIVE, 0, nullptr));
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001776 fHWWindowRectsState.setDisabled();
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001777#endif
csmartdalton28341fa2016-08-17 10:00:21 -07001778}
1779
ethannicholas28ef4452016-03-25 09:26:03 -07001780void GrGLGpu::flushMinSampleShading(float minSampleShading) {
1781 if (fHWMinSampleShading != minSampleShading) {
1782 if (minSampleShading > 0.0) {
1783 GL_CALL(Enable(GR_GL_SAMPLE_SHADING));
1784 GL_CALL(MinSampleShading(minSampleShading));
1785 }
1786 else {
1787 GL_CALL(Disable(GR_GL_SAMPLE_SHADING));
1788 }
1789 fHWMinSampleShading = minSampleShading;
1790 }
1791}
1792
bsalomon2eda5b32016-09-21 10:53:24 -07001793bool GrGLGpu::flushGLState(const GrPipeline& pipeline, const GrPrimitiveProcessor& primProc,
1794 bool willDrawPoints) {
Hal Canary144caf52016-11-07 17:57:18 -05001795 sk_sp<GrGLProgram> program(fProgramCache->refProgram(this, pipeline, primProc, willDrawPoints));
brianosman33f6b3f2016-06-02 05:49:21 -07001796 if (!program) {
1797 GrCapsDebugf(this->caps(), "Failed to create program!\n");
1798 return false;
1799 }
1800
1801 program->generateMipmaps(primProc, pipeline);
1802
egdaniel080e6732014-12-22 07:35:52 -08001803 GrXferProcessor::BlendInfo blendInfo;
egdaniel0e1853c2016-03-17 11:35:45 -07001804 pipeline.getXferProcessor().getBlendInfo(&blendInfo);
egdaniel080e6732014-12-22 07:35:52 -08001805
egdaniel080e6732014-12-22 07:35:52 -08001806 this->flushColorWrite(blendInfo.fWriteColor);
ethannicholas28ef4452016-03-25 09:26:03 -07001807 this->flushMinSampleShading(primProc.getSampleShading());
bsalomonbc3d0de2014-12-15 13:45:03 -08001808
bsalomon6df86402015-06-01 10:41:49 -07001809 GrGLuint programID = program->programID();
bsalomon1f78c0a2014-12-17 09:43:13 -08001810 if (fHWProgramID != programID) {
1811 GL_CALL(UseProgram(programID));
1812 fHWProgramID = programID;
1813 }
1814
egdanield803f272015-03-18 13:01:52 -07001815 if (blendInfo.fWriteColor) {
bsalomon7f9b2e42016-01-12 13:29:26 -08001816 // Swizzle the blend to match what the shader will output.
Brian Salomon1edc5b92016-11-29 13:43:46 -05001817 const GrSwizzle& swizzle = this->caps()->shaderCaps()->configOutputSwizzle(
egdaniel0e1853c2016-03-17 11:35:45 -07001818 pipeline.getRenderTarget()->config());
bsalomon7f9b2e42016-01-12 13:29:26 -08001819 this->flushBlend(blendInfo, swizzle);
egdanield803f272015-03-18 13:01:52 -07001820 }
bsalomon1f78c0a2014-12-17 09:43:13 -08001821
cdalton74b8d322016-04-11 14:47:28 -07001822 program->setData(primProc, pipeline);
bsalomon1f78c0a2014-12-17 09:43:13 -08001823
brianosman898235c2016-04-06 07:38:23 -07001824 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(pipeline.getRenderTarget());
csmartdaltonc633abb2016-11-01 08:55:55 -07001825 GrStencilSettings stencil;
1826 if (pipeline.isStencilEnabled()) {
1827 // TODO: attach stencil and create settings during render target flush.
1828 SkASSERT(glRT->renderTargetPriv().getStencilAttachment());
1829 stencil.reset(*pipeline.getUserStencil(), pipeline.hasStencilClip(),
1830 glRT->renderTargetPriv().numStencilBits());
1831 }
1832 this->flushStencil(stencil);
egdaniel8dd688b2015-01-22 10:16:09 -08001833 this->flushScissor(pipeline.getScissorState(), glRT->getViewport(), glRT->origin());
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001834 this->flushWindowRectangles(pipeline.getWindowRectsState(), glRT);
csmartdaltondabc91b2016-11-15 14:26:27 -07001835 this->flushHWAAState(glRT, pipeline.isHWAntialiasState(), !stencil.isDisabled());
bsalomonbc3d0de2014-12-15 13:45:03 -08001836
1837 // This must come after textures are flushed because a texture may need
egdanield803f272015-03-18 13:01:52 -07001838 // to be msaa-resolved (which will modify bound FBO state).
brianosman64d094d2016-03-25 06:01:59 -07001839 this->flushRenderTarget(glRT, nullptr, pipeline.getDisableOutputConversionToSRGB());
bsalomonbc3d0de2014-12-15 13:45:03 -08001840
1841 return true;
1842}
1843
joshualitt873ad0e2015-01-20 09:08:51 -08001844void GrGLGpu::setupGeometry(const GrPrimitiveProcessor& primProc,
Chris Daltonff926502017-05-03 14:36:54 -04001845 const GrBuffer* indexBuffer,
1846 const GrBuffer* vertexBuffer,
Chris Dalton1d616352017-05-31 12:51:23 -06001847 int baseVertex,
1848 const GrBuffer* instanceBuffer,
1849 int baseInstance) {
cdaltone2e71c22016-04-07 18:13:29 -07001850 GrGLAttribArrayState* attribState;
Chris Daltonff926502017-05-03 14:36:54 -04001851 if (indexBuffer) {
Chris Dalton1d616352017-05-31 12:51:23 -06001852 SkASSERT(indexBuffer && !indexBuffer->isMapped());
Chris Daltonff926502017-05-03 14:36:54 -04001853 attribState = fHWVertexArrayState.bindInternalVertexArray(this, indexBuffer);
cdaltone2e71c22016-04-07 18:13:29 -07001854 } else {
1855 attribState = fHWVertexArrayState.bindInternalVertexArray(this);
bsalomonbc3d0de2014-12-15 13:45:03 -08001856 }
bsalomonbc3d0de2014-12-15 13:45:03 -08001857
Chris Dalton1d616352017-05-31 12:51:23 -06001858 struct {
1859 const GrBuffer* fBuffer;
1860 int fStride;
1861 size_t fBufferOffset;
1862 } bindings[2];
Chris Dalton8e45b4f2017-05-05 14:00:56 -04001863
Chris Dalton1d616352017-05-31 12:51:23 -06001864 if (int vertexStride = primProc.getVertexStride()) {
1865 SkASSERT(vertexBuffer && !vertexBuffer->isMapped());
1866 bindings[0].fBuffer = vertexBuffer;
1867 bindings[0].fStride = vertexStride;
1868 bindings[0].fBufferOffset = vertexBuffer->baseOffset() + baseVertex * vertexStride;
1869 }
1870 if (int instanceStride = primProc.getInstanceStride()) {
1871 SkASSERT(instanceBuffer && !instanceBuffer->isMapped());
1872 bindings[1].fBuffer = instanceBuffer;
1873 bindings[1].fStride = instanceStride;
1874 bindings[1].fBufferOffset = instanceBuffer->baseOffset() + baseInstance * instanceStride;
1875 }
bsalomonbc3d0de2014-12-15 13:45:03 -08001876
Chris Dalton1d616352017-05-31 12:51:23 -06001877 int numAttribs = primProc.numAttribs();
1878 attribState->enableVertexArrays(this, numAttribs);
bsalomonbc3d0de2014-12-15 13:45:03 -08001879
Chris Dalton1d616352017-05-31 12:51:23 -06001880 for (int i = 0; i < numAttribs; ++i) {
1881 using InputRate = GrPrimitiveProcessor::Attribute::InputRate;
1882 const GrGeometryProcessor::Attribute& attrib = primProc.getAttrib(i);
1883 const int divisor = InputRate::kPerInstance == attrib.fInputRate ? 1 : 0;
1884 const auto& binding = bindings[divisor];
1885 attribState->set(this, i, binding.fBuffer, attrib.fType, binding.fStride,
1886 binding.fBufferOffset + attrib.fOffsetInRecord, divisor);
bsalomonbc3d0de2014-12-15 13:45:03 -08001887 }
1888}
1889
csmartdalton485a1202016-07-13 10:16:32 -07001890GrGLenum GrGLGpu::bindBuffer(GrBufferType type, const GrBuffer* buffer) {
joshualitt93316b92015-10-23 09:08:08 -07001891 this->handleDirtyContext();
cdaltondeacc972016-04-06 14:26:33 -07001892
cdaltone2e71c22016-04-07 18:13:29 -07001893 // Index buffer state is tied to the vertex array.
1894 if (kIndex_GrBufferType == type) {
1895 this->bindVertexArray(0);
cdaltondeacc972016-04-06 14:26:33 -07001896 }
cdaltone2e71c22016-04-07 18:13:29 -07001897
1898 SkASSERT(type >= 0 && type <= kLast_GrBufferType);
1899 auto& bufferState = fHWBufferState[type];
1900
robertphillips8abb3702016-08-31 14:04:06 -07001901 if (buffer->uniqueID() != bufferState.fBoundBufferUniqueID) {
csmartdalton485a1202016-07-13 10:16:32 -07001902 if (buffer->isCPUBacked()) {
1903 if (!bufferState.fBufferZeroKnownBound) {
1904 GL_CALL(BindBuffer(bufferState.fGLTarget, 0));
1905 }
1906 } else {
1907 const GrGLBuffer* glBuffer = static_cast<const GrGLBuffer*>(buffer);
1908 GL_CALL(BindBuffer(bufferState.fGLTarget, glBuffer->bufferID()));
cdaltone2e71c22016-04-07 18:13:29 -07001909 }
csmartdalton485a1202016-07-13 10:16:32 -07001910 bufferState.fBufferZeroKnownBound = buffer->isCPUBacked();
robertphillips8abb3702016-08-31 14:04:06 -07001911 bufferState.fBoundBufferUniqueID = buffer->uniqueID();
cdaltone2e71c22016-04-07 18:13:29 -07001912 }
1913
1914 return bufferState.fGLTarget;
joshualitt93316b92015-10-23 09:08:08 -07001915}
1916
cdalton74b8d322016-04-11 14:47:28 -07001917void GrGLGpu::notifyBufferReleased(const GrGLBuffer* buffer) {
1918 if (buffer->hasAttachedToTexture()) {
1919 // Detach this buffer from any textures to ensure the underlying memory is freed.
Robert Phillips294870f2016-11-11 12:38:40 -05001920 GrGpuResource::UniqueID uniqueID = buffer->uniqueID();
cdalton74b8d322016-04-11 14:47:28 -07001921 for (int i = fHWMaxUsedBufferTextureUnit; i >= 0; --i) {
1922 auto& buffTex = fHWBufferTextures[i];
1923 if (uniqueID != buffTex.fAttachedBufferUniqueID) {
1924 continue;
1925 }
1926 if (i == fHWMaxUsedBufferTextureUnit) {
1927 --fHWMaxUsedBufferTextureUnit;
1928 }
1929
1930 this->setTextureUnit(i);
1931 if (!buffTex.fKnownBound) {
1932 SkASSERT(buffTex.fTextureID);
1933 GL_CALL(BindTexture(GR_GL_TEXTURE_BUFFER, buffTex.fTextureID));
1934 buffTex.fKnownBound = true;
1935 }
1936 GL_CALL(TexBuffer(GR_GL_TEXTURE_BUFFER,
1937 this->glCaps().configSizedInternalFormat(buffTex.fTexelConfig), 0));
1938 }
1939 }
1940}
1941
bsalomon861e1032014-12-16 07:33:49 -08001942void GrGLGpu::disableScissor() {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001943 if (kNo_TriState != fHWScissorSettings.fEnabled) {
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001944 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.coma3201942012-06-21 19:58:20 +00001945 fHWScissorSettings.fEnabled = kNo_TriState;
1946 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001947 }
1948}
1949
csmartdalton29df7602016-08-31 11:55:52 -07001950void GrGLGpu::clear(const GrFixedClip& clip, GrColor color, GrRenderTarget* target) {
egdaniel9cb63402016-06-23 08:37:05 -07001951 this->handleDirtyContext();
1952
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001953 // parent class should never let us get here with no RT
bsalomon49f085d2014-09-05 13:34:00 -07001954 SkASSERT(target);
bsalomonb0bd4f62014-09-03 07:19:50 -07001955 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001956
csmartdalton29df7602016-08-31 11:55:52 -07001957 this->flushRenderTarget(glRT, clip.scissorEnabled() ? &clip.scissorRect() : nullptr);
1958 this->flushScissor(clip.scissorState(), glRT->getViewport(), glRT->origin());
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001959 this->flushWindowRectangles(clip.windowRectsState(), glRT);
bsalomon@google.com74b98712011-11-11 19:46:16 +00001960
1961 GrGLfloat r, g, b, a;
1962 static const GrGLfloat scale255 = 1.f / 255.f;
1963 a = GrColorUnpackA(color) * scale255;
1964 GrGLfloat scaleRGB = scale255;
bsalomon@google.com74b98712011-11-11 19:46:16 +00001965 r = GrColorUnpackR(color) * scaleRGB;
1966 g = GrColorUnpackG(color) * scaleRGB;
1967 b = GrColorUnpackB(color) * scaleRGB;
1968
1969 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00001970 fHWWriteToColor = kYes_TriState;
Brian Salomon028a9a52017-05-11 11:39:08 -04001971
Eric Karlaeaf22b2017-05-18 15:08:09 -07001972 if (this->glCaps().clearToBoundaryValuesIsBroken() &&
1973 (1 == r || 0 == r) && (1 == g || 0 == g) && (1 == b || 0 == b) && (1 == a || 0 == a)) {
Eric Karlaeaf22b2017-05-18 15:08:09 -07001974 static const GrGLfloat safeAlpha1 = nextafter(1.f, 2.f);
1975 static const GrGLfloat safeAlpha0 = nextafter(0.f, -1.f);
Eric Karlaeaf22b2017-05-18 15:08:09 -07001976 a = (1 == a) ? safeAlpha1 : safeAlpha0;
Brian Salomon028a9a52017-05-11 11:39:08 -04001977 }
bsalomon@google.com74b98712011-11-11 19:46:16 +00001978 GL_CALL(ClearColor(r, g, b, a));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001979 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001980}
1981
bsalomon861e1032014-12-16 07:33:49 -08001982void GrGLGpu::clearStencil(GrRenderTarget* target) {
halcanary96fcdcc2015-08-27 07:41:13 -07001983 if (nullptr == target) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001984 return;
1985 }
bsalomonb0bd4f62014-09-03 07:19:50 -07001986 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
egdanield803f272015-03-18 13:01:52 -07001987 this->flushRenderTarget(glRT, &SkIRect::EmptyIRect());
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001988
joshualitt77b13072014-10-27 14:51:01 -07001989 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07001990 this->disableWindowRectangles();
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001991
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001992 GL_CALL(StencilMask(0xffffffff));
1993 GL_CALL(ClearStencil(0));
1994 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001995 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001996}
1997
csmartdalton29df7602016-08-31 11:55:52 -07001998void GrGLGpu::clearStencilClip(const GrFixedClip& clip,
1999 bool insideStencilMask,
2000 GrRenderTarget* target) {
bsalomon49f085d2014-09-05 13:34:00 -07002001 SkASSERT(target);
egdaniel9cb63402016-06-23 08:37:05 -07002002 this->handleDirtyContext();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00002003
Mike Klein31550db2017-06-06 23:29:53 +00002004 if (this->glCaps().useDrawToClearStencilClip()) {
2005 this->clearStencilClipAsDraw(clip, insideStencilMask, target);
2006 return;
2007 }
2008
egdaniel8dc7c3a2015-04-16 11:22:42 -07002009 GrStencilAttachment* sb = target->renderTargetPriv().getStencilAttachment();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00002010 // this should only be called internally when we know we have a
2011 // stencil buffer.
bsalomon6bc1b5f2015-02-23 09:06:38 -08002012 SkASSERT(sb);
2013 GrGLint stencilBitCount = sb->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002014#if 0
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002015 SkASSERT(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00002016 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002017#else
2018 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00002019 // ANGLE a partial stencil mask will cause clears to be
Robert Phillipsf2361d22016-10-25 14:20:06 -04002020 // turned into draws. Our contract on GrOpList says that
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002021 // changing the clip between stencil passes may or may not
2022 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00002023 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002024#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002025 GrGLint value;
csmartdalton29df7602016-08-31 11:55:52 -07002026 if (insideStencilMask) {
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002027 value = (1 << (stencilBitCount - 1));
2028 } else {
2029 value = 0;
2030 }
bsalomonb0bd4f62014-09-03 07:19:50 -07002031 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
egdanield803f272015-03-18 13:01:52 -07002032 this->flushRenderTarget(glRT, &SkIRect::EmptyIRect());
bsalomon@google.coma3201942012-06-21 19:58:20 +00002033
csmartdalton29df7602016-08-31 11:55:52 -07002034 this->flushScissor(clip.scissorState(), glRT->getViewport(), glRT->origin());
csmartdaltonbf4a8f92016-09-06 10:01:06 -07002035 this->flushWindowRectangles(clip.windowRectsState(), glRT);
bsalomon@google.coma3201942012-06-21 19:58:20 +00002036
caryclark@google.comcf6285b2012-06-06 12:09:01 +00002037 GL_CALL(StencilMask((uint32_t) clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002038 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002039 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00002040 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00002041}
2042
Brian Salomon71d9d842016-11-03 13:42:00 -04002043static bool read_pixels_pays_for_y_flip(GrSurfaceOrigin origin, const GrGLCaps& caps,
bsalomon39826022015-07-23 08:07:21 -07002044 int width, int height, GrPixelConfig config,
2045 size_t rowBytes) {
Brian Salomon71d9d842016-11-03 13:42:00 -04002046 // If the surface is already TopLeft, we don't need to flip.
2047 if (kTopLeft_GrSurfaceOrigin == origin) {
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002048 return false;
2049 }
2050
bsalomon494aa592015-07-23 11:45:02 -07002051 // If the read is really small or smaller than the min texture size, don't force a draw.
bsalomon100b8f82015-10-28 08:37:44 -07002052 static const int kMinSize = 32;
2053 if (width < kMinSize || height < kMinSize) {
bsalomon494aa592015-07-23 11:45:02 -07002054 return false;
2055 }
2056
bsalomon@google.com56d11e02011-11-30 19:59:08 +00002057 // if GL can do the flip then we'll never pay for it.
bsalomon39826022015-07-23 08:07:21 -07002058 if (caps.packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00002059 return false;
2060 }
2061
2062 // If we have to do memcpy to handle non-trim rowBytes then we
bsalomon@google.com7107fa72011-11-10 14:54:14 +00002063 // get the flip for free. Otherwise it costs.
bsalomon39826022015-07-23 08:07:21 -07002064 // Note that we're assuming that 0 rowBytes has already been handled and that the width has been
2065 // clipped.
2066 return caps.packRowLengthSupport() || GrBytesPerPixel(config) * width == rowBytes;
2067}
2068
bsalomon1aa20292016-01-22 08:16:09 -08002069bool GrGLGpu::readPixelsSupported(GrRenderTarget* target, GrPixelConfig readConfig) {
Brian Salomon625cd9e2016-12-15 09:35:19 -05002070#ifdef SK_BUILD_FOR_MAC
2071 // Chromium may ask us to read back from locked IOSurfaces. Calling the command buffer's
2072 // glGetIntegerv() with GL_IMPLEMENTATION_COLOR_READ_FORMAT/_TYPE causes the command buffer
2073 // to make a call to check the framebuffer status which can hang the driver. So in Mac Chromium
2074 // we always use a temporary surface to test for read pixels support.
2075 // https://www.crbug.com/662802
2076 if (this->glContext().driver() == kChromium_GrGLDriver) {
2077 return this->readPixelsSupported(target->config(), readConfig);
2078 }
2079#endif
bsalomon1aa20292016-01-22 08:16:09 -08002080 auto bindRenderTarget = [this, target]() -> bool {
2081 this->flushRenderTarget(static_cast<GrGLRenderTarget*>(target), &SkIRect::EmptyIRect());
2082 return true;
2083 };
bsalomon2c3db322016-11-08 13:26:24 -08002084 auto unbindRenderTarget = []{};
bsalomon1aa20292016-01-22 08:16:09 -08002085 auto getIntegerv = [this](GrGLenum query, GrGLint* value) {
2086 GR_GL_GetIntegerv(this->glInterface(), query, value);
2087 };
2088 GrPixelConfig rtConfig = target->config();
bsalomon2c3db322016-11-08 13:26:24 -08002089 return this->glCaps().readPixelsSupported(rtConfig, readConfig, getIntegerv, bindRenderTarget,
2090 unbindRenderTarget);
bsalomon1aa20292016-01-22 08:16:09 -08002091}
2092
2093bool GrGLGpu::readPixelsSupported(GrPixelConfig rtConfig, GrPixelConfig readConfig) {
bsalomon2c3db322016-11-08 13:26:24 -08002094 sk_sp<GrTexture> temp;
2095 auto bindRenderTarget = [this, rtConfig, &temp]() -> bool {
bsalomon1aa20292016-01-22 08:16:09 -08002096 GrTextureDesc desc;
2097 desc.fConfig = rtConfig;
2098 desc.fWidth = desc.fHeight = 16;
bsalomon2c3db322016-11-08 13:26:24 -08002099 if (this->glCaps().isConfigRenderable(rtConfig, false)) {
2100 desc.fFlags = kRenderTarget_GrSurfaceFlag;
Robert Phillips67d52cf2017-06-05 13:38:13 -04002101 temp = this->createTexture(desc, SkBudgeted::kNo);
bsalomon2c3db322016-11-08 13:26:24 -08002102 if (!temp) {
2103 return false;
2104 }
2105 GrGLRenderTarget* glrt = static_cast<GrGLRenderTarget*>(temp->asRenderTarget());
2106 this->flushRenderTarget(glrt, &SkIRect::EmptyIRect());
2107 return true;
2108 } else if (this->glCaps().canConfigBeFBOColorAttachment(rtConfig)) {
Robert Phillips67d52cf2017-06-05 13:38:13 -04002109 temp = this->createTexture(desc, SkBudgeted::kNo);
bsalomon2c3db322016-11-08 13:26:24 -08002110 if (!temp) {
2111 return false;
2112 }
2113 GrGLIRect vp;
2114 this->bindSurfaceFBOForPixelOps(temp.get(), GR_GL_FRAMEBUFFER, &vp, kDst_TempFBOTarget);
Robert Phillips294870f2016-11-11 12:38:40 -05002115 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon2c3db322016-11-08 13:26:24 -08002116 return true;
bsalomon1aa20292016-01-22 08:16:09 -08002117 }
bsalomon2c3db322016-11-08 13:26:24 -08002118 return false;
2119 };
2120 auto unbindRenderTarget = [this, &temp]() {
2121 this->unbindTextureFBOForPixelOps(GR_GL_FRAMEBUFFER, temp.get());
bsalomon1aa20292016-01-22 08:16:09 -08002122 };
2123 auto getIntegerv = [this](GrGLenum query, GrGLint* value) {
2124 GR_GL_GetIntegerv(this->glInterface(), query, value);
2125 };
bsalomon2c3db322016-11-08 13:26:24 -08002126 return this->glCaps().readPixelsSupported(rtConfig, readConfig, getIntegerv, bindRenderTarget,
2127 unbindRenderTarget);
bsalomon1aa20292016-01-22 08:16:09 -08002128}
2129
2130bool GrGLGpu::readPixelsSupported(GrSurface* surfaceForConfig, GrPixelConfig readConfig) {
2131 if (GrRenderTarget* rt = surfaceForConfig->asRenderTarget()) {
2132 return this->readPixelsSupported(rt, readConfig);
2133 } else {
2134 GrPixelConfig config = surfaceForConfig->config();
2135 return this->readPixelsSupported(config, readConfig);
2136 }
2137}
2138
bsalomone9573312016-01-25 14:33:25 -08002139static bool requires_srgb_conversion(GrPixelConfig a, GrPixelConfig b) {
2140 if (GrPixelConfigIsSRGB(a)) {
2141 return !GrPixelConfigIsSRGB(b) && !GrPixelConfigIsAlphaOnly(b);
2142 } else if (GrPixelConfigIsSRGB(b)) {
2143 return !GrPixelConfigIsSRGB(a) && !GrPixelConfigIsAlphaOnly(a);
2144 }
2145 return false;
2146}
2147
bsalomonf0674512015-07-28 13:26:15 -07002148bool GrGLGpu::onGetReadPixelsInfo(GrSurface* srcSurface, int width, int height, size_t rowBytes,
2149 GrPixelConfig readConfig, DrawPreference* drawPreference,
2150 ReadPixelTempDrawInfo* tempDrawInfo) {
bsalomone9573312016-01-25 14:33:25 -08002151 GrPixelConfig srcConfig = srcSurface->config();
bsalomon1aa20292016-01-22 08:16:09 -08002152
bsalomone9573312016-01-25 14:33:25 -08002153 // These settings we will always want if a temp draw is performed.
bsalomon39826022015-07-23 08:07:21 -07002154 tempDrawInfo->fTempSurfaceDesc.fFlags = kRenderTarget_GrSurfaceFlag;
2155 tempDrawInfo->fTempSurfaceDesc.fWidth = width;
2156 tempDrawInfo->fTempSurfaceDesc.fHeight = height;
2157 tempDrawInfo->fTempSurfaceDesc.fSampleCnt = 0;
2158 tempDrawInfo->fTempSurfaceDesc.fOrigin = kTopLeft_GrSurfaceOrigin; // no CPU y-flip for TL.
bsalomonb117ff12016-07-19 07:24:40 -07002159 tempDrawInfo->fTempSurfaceFit = this->glCaps().partialFBOReadIsSlow() ? SkBackingFit::kExact
2160 : SkBackingFit::kApprox;
bsalomone9573312016-01-25 14:33:25 -08002161 // For now assume no swizzling, we may change that below.
2162 tempDrawInfo->fSwizzle = GrSwizzle::RGBA();
2163
2164 // Depends on why we need/want a temp draw. Start off assuming no change, the surface we read
2165 // from will be srcConfig and we will read readConfig pixels from it.
Brian Osman33910292017-04-18 14:38:53 -04002166 // Note that if we require a draw and return a non-renderable format for the temp surface the
bsalomone9573312016-01-25 14:33:25 -08002167 // base class will fail for us.
2168 tempDrawInfo->fTempSurfaceDesc.fConfig = srcConfig;
2169 tempDrawInfo->fReadConfig = readConfig;
2170
2171 if (requires_srgb_conversion(srcConfig, readConfig)) {
2172 if (!this->readPixelsSupported(readConfig, readConfig)) {
2173 return false;
2174 }
2175 // Draw to do srgb to linear conversion or vice versa.
2176 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
2177 tempDrawInfo->fTempSurfaceDesc.fConfig = readConfig;
2178 tempDrawInfo->fReadConfig = readConfig;
2179 return true;
2180 }
2181
bsalomon1aa20292016-01-22 08:16:09 -08002182 if (this->glCaps().rgba8888PixelsOpsAreSlow() && kRGBA_8888_GrPixelConfig == readConfig &&
2183 this->readPixelsSupported(kBGRA_8888_GrPixelConfig, kBGRA_8888_GrPixelConfig)) {
bsalomon39826022015-07-23 08:07:21 -07002184 tempDrawInfo->fTempSurfaceDesc.fConfig = kBGRA_8888_GrPixelConfig;
bsalomon6c9cd552016-01-22 07:17:34 -08002185 tempDrawInfo->fSwizzle = GrSwizzle::BGRA();
2186 tempDrawInfo->fReadConfig = kBGRA_8888_GrPixelConfig;
bsalomonb411b3b2015-07-31 09:34:24 -07002187 ElevateDrawPreference(drawPreference, kGpuPrefersDraw_DrawPreference);
ericrkb4ecabd2016-03-11 15:18:20 -08002188 } else if (this->glCaps().rgbaToBgraReadbackConversionsAreSlow() &&
bsalomon39826022015-07-23 08:07:21 -07002189 GrBytesPerPixel(readConfig) == 4 &&
bsalomon1aa20292016-01-22 08:16:09 -08002190 GrPixelConfigSwapRAndB(readConfig) == srcConfig &&
2191 this->readPixelsSupported(srcSurface, srcConfig)) {
bsalomon6c9cd552016-01-22 07:17:34 -08002192 // Mesa 3D takes a slow path on when reading back BGRA from an RGBA surface and vice-versa.
bsalomon39826022015-07-23 08:07:21 -07002193 // Better to do a draw with a R/B swap and then read as the original config.
2194 tempDrawInfo->fTempSurfaceDesc.fConfig = srcConfig;
bsalomon6c9cd552016-01-22 07:17:34 -08002195 tempDrawInfo->fSwizzle = GrSwizzle::BGRA();
2196 tempDrawInfo->fReadConfig = srcConfig;
bsalomonf0674512015-07-28 13:26:15 -07002197 ElevateDrawPreference(drawPreference, kGpuPrefersDraw_DrawPreference);
bsalomon1aa20292016-01-22 08:16:09 -08002198 } else if (!this->readPixelsSupported(srcSurface, readConfig)) {
2199 if (readConfig == kBGRA_8888_GrPixelConfig &&
Brian Salomon71d9d842016-11-03 13:42:00 -04002200 this->glCaps().canConfigBeFBOColorAttachment(kRGBA_8888_GrPixelConfig) &&
bsalomon1aa20292016-01-22 08:16:09 -08002201 this->readPixelsSupported(kRGBA_8888_GrPixelConfig, kRGBA_8888_GrPixelConfig)) {
bsalomone9573312016-01-25 14:33:25 -08002202 // We're trying to read BGRA but it's not supported. If RGBA is renderable and
2203 // we can read it back, then do a swizzling draw to a RGBA and read it back (which
2204 // will effectively be BGRA).
bsalomon1aa20292016-01-22 08:16:09 -08002205 tempDrawInfo->fTempSurfaceDesc.fConfig = kRGBA_8888_GrPixelConfig;
2206 tempDrawInfo->fSwizzle = GrSwizzle::BGRA();
2207 tempDrawInfo->fReadConfig = kRGBA_8888_GrPixelConfig;
2208 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
brianosmana6359362016-03-21 06:55:37 -07002209 } else if (readConfig == kSBGRA_8888_GrPixelConfig &&
Brian Salomon71d9d842016-11-03 13:42:00 -04002210 this->glCaps().canConfigBeFBOColorAttachment(kSRGBA_8888_GrPixelConfig) &&
brianosmana6359362016-03-21 06:55:37 -07002211 this->readPixelsSupported(kSRGBA_8888_GrPixelConfig, kSRGBA_8888_GrPixelConfig)) {
2212 // We're trying to read sBGRA but it's not supported. If sRGBA is renderable and
2213 // we can read it back, then do a swizzling draw to a sRGBA and read it back (which
2214 // will effectively be sBGRA).
2215 tempDrawInfo->fTempSurfaceDesc.fConfig = kSRGBA_8888_GrPixelConfig;
2216 tempDrawInfo->fSwizzle = GrSwizzle::BGRA();
2217 tempDrawInfo->fReadConfig = kSRGBA_8888_GrPixelConfig;
2218 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
bsalomone9573312016-01-25 14:33:25 -08002219 } else if (readConfig == kAlpha_8_GrPixelConfig) {
2220 // onReadPixels implements a fallback for cases where we are want to read kAlpha_8,
2221 // it's unsupported, but 32bit RGBA reads are supported.
2222 // Don't attempt to do any srgb conversions since we only care about alpha.
2223 GrPixelConfig cpuTempConfig = kRGBA_8888_GrPixelConfig;
2224 if (GrPixelConfigIsSRGB(srcSurface->config())) {
2225 cpuTempConfig = kSRGBA_8888_GrPixelConfig;
2226 }
2227 if (!this->readPixelsSupported(srcSurface, cpuTempConfig)) {
2228 // If we can't read RGBA from the src try to draw to a kRGBA_8888 (or kSRGBA_8888)
2229 // first and then onReadPixels will read that to a 32bit temporary buffer.
Brian Salomon71d9d842016-11-03 13:42:00 -04002230 if (this->glCaps().canConfigBeFBOColorAttachment(cpuTempConfig)) {
bsalomone9573312016-01-25 14:33:25 -08002231 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
2232 tempDrawInfo->fTempSurfaceDesc.fConfig = cpuTempConfig;
2233 tempDrawInfo->fReadConfig = kAlpha_8_GrPixelConfig;
2234 } else {
2235 return false;
2236 }
2237 } else {
2238 SkASSERT(tempDrawInfo->fTempSurfaceDesc.fConfig == srcConfig);
2239 SkASSERT(tempDrawInfo->fReadConfig == kAlpha_8_GrPixelConfig);
2240 }
Brian Salomon71d9d842016-11-03 13:42:00 -04002241 } else if (this->glCaps().canConfigBeFBOColorAttachment(readConfig) &&
bsalomone6d665e2016-03-10 07:22:25 -08002242 this->readPixelsSupported(readConfig, readConfig)) {
2243 // Do a draw to convert from the src config to the read config.
2244 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
2245 tempDrawInfo->fTempSurfaceDesc.fConfig = readConfig;
2246 tempDrawInfo->fReadConfig = readConfig;
bsalomon1aa20292016-01-22 08:16:09 -08002247 } else {
2248 return false;
2249 }
bsalomon39826022015-07-23 08:07:21 -07002250 }
2251
Brian Salomon71d9d842016-11-03 13:42:00 -04002252 if ((srcSurface->asRenderTarget() || this->glCaps().canConfigBeFBOColorAttachment(srcConfig)) &&
2253 read_pixels_pays_for_y_flip(srcSurface->origin(), this->glCaps(), width, height, readConfig,
2254 rowBytes)) {
bsalomonf0674512015-07-28 13:26:15 -07002255 ElevateDrawPreference(drawPreference, kGpuPrefersDraw_DrawPreference);
bsalomon39826022015-07-23 08:07:21 -07002256 }
2257
bsalomon39826022015-07-23 08:07:21 -07002258 return true;
bsalomon@google.comc4364992011-11-07 15:54:49 +00002259}
2260
bsalomon6cb3cbe2015-07-30 07:34:27 -07002261bool GrGLGpu::onReadPixels(GrSurface* surface,
bsalomon@google.comc6980972011-11-02 19:57:21 +00002262 int left, int top,
2263 int width, int height,
bsalomon@google.comc4364992011-11-07 15:54:49 +00002264 GrPixelConfig config,
2265 void* buffer,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002266 size_t rowBytes) {
bsalomon6cb3cbe2015-07-30 07:34:27 -07002267 SkASSERT(surface);
bsalomon39826022015-07-23 08:07:21 -07002268
bsalomone9573312016-01-25 14:33:25 -08002269 GrGLRenderTarget* renderTarget = static_cast<GrGLRenderTarget*>(surface->asRenderTarget());
Brian Salomon71d9d842016-11-03 13:42:00 -04002270 if (!renderTarget && !this->glCaps().canConfigBeFBOColorAttachment(surface->config())) {
bsalomon6cb3cbe2015-07-30 07:34:27 -07002271 return false;
2272 }
2273
bsalomon16921ec2015-07-30 15:34:56 -07002274 // OpenGL doesn't do sRGB <-> linear conversions when reading and writing pixels.
bsalomone9573312016-01-25 14:33:25 -08002275 if (requires_srgb_conversion(surface->config(), config)) {
2276 return false;
2277 }
2278
2279 // We have a special case fallback for reading eight bit alpha. We will read back all four 8
2280 // bit channels as RGBA and then extract A.
Brian Salomon71d9d842016-11-03 13:42:00 -04002281 if (!this->readPixelsSupported(surface, config)) {
bsalomone9573312016-01-25 14:33:25 -08002282 // Don't attempt to do any srgb conversions since we only care about alpha.
2283 GrPixelConfig tempConfig = kRGBA_8888_GrPixelConfig;
Brian Salomon71d9d842016-11-03 13:42:00 -04002284 if (GrPixelConfigIsSRGB(surface->config())) {
bsalomone9573312016-01-25 14:33:25 -08002285 tempConfig = kSRGBA_8888_GrPixelConfig;
2286 }
2287 if (kAlpha_8_GrPixelConfig == config &&
Brian Salomon71d9d842016-11-03 13:42:00 -04002288 this->readPixelsSupported(surface, tempConfig)) {
Ben Wagner7ecc5962016-11-02 17:07:33 -04002289 std::unique_ptr<uint32_t[]> temp(new uint32_t[width * height * 4]);
Brian Salomon71d9d842016-11-03 13:42:00 -04002290 if (this->onReadPixels(surface, left, top, width, height, tempConfig, temp.get(),
bsalomone9573312016-01-25 14:33:25 -08002291 width*4)) {
2292 uint8_t* dst = reinterpret_cast<uint8_t*>(buffer);
2293 for (int j = 0; j < height; ++j) {
2294 for (int i = 0; i < width; ++i) {
2295 dst[j*rowBytes + i] = (0xFF000000U & temp[j*width+i]) >> 24;
2296 }
2297 }
2298 return true;
2299 }
2300 }
bsalomon16921ec2015-07-30 15:34:56 -07002301 return false;
2302 }
2303
bsalomon76148af2016-01-12 11:13:47 -08002304 GrGLenum externalFormat;
2305 GrGLenum externalType;
Brian Salomon71d9d842016-11-03 13:42:00 -04002306 if (!this->glCaps().getReadPixelsFormat(surface->config(), config, &externalFormat,
bsalomon76148af2016-01-12 11:13:47 -08002307 &externalType)) {
2308 return false;
2309 }
bsalomon6cb3cbe2015-07-30 07:34:27 -07002310 bool flipY = kBottomLeft_GrSurfaceOrigin == surface->origin();
bsalomon@google.comc4364992011-11-07 15:54:49 +00002311
Brian Salomon71d9d842016-11-03 13:42:00 -04002312 GrGLIRect glvp;
2313 if (renderTarget) {
2314 // resolve the render target if necessary
2315 switch (renderTarget->getResolveType()) {
2316 case GrGLRenderTarget::kCantResolve_ResolveType:
2317 return false;
2318 case GrGLRenderTarget::kAutoResolves_ResolveType:
2319 this->flushRenderTarget(renderTarget, &SkIRect::EmptyIRect());
2320 break;
2321 case GrGLRenderTarget::kCanResolve_ResolveType:
2322 this->onResolveRenderTarget(renderTarget);
2323 // we don't track the state of the READ FBO ID.
2324 fStats.incRenderTargetBinds();
2325 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER, renderTarget->textureFBOID()));
2326 break;
2327 default:
2328 SkFAIL("Unknown resolve type");
2329 }
2330 glvp = renderTarget->getViewport();
2331 } else {
2332 // Use a temporary FBO.
2333 this->bindSurfaceFBOForPixelOps(surface, GR_GL_FRAMEBUFFER, &glvp, kSrc_TempFBOTarget);
Robert Phillips294870f2016-11-11 12:38:40 -05002334 fHWBoundRenderTargetUniqueID.makeInvalid();
reed@google.comac10a2d2010-12-22 21:39:39 +00002335 }
2336
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00002337 // the read rect is viewport-relative
2338 GrGLIRect readRect;
Brian Salomon71d9d842016-11-03 13:42:00 -04002339 readRect.setRelativeTo(glvp, left, top, width, height, surface->origin());
rmistry@google.comfbfcd562012-08-23 18:09:54 +00002340
bsalomon9d02b262016-02-01 12:49:30 -08002341 size_t bytesPerPixel = GrBytesPerPixel(config);
2342 size_t tightRowBytes = bytesPerPixel * width;
egdaniel6d901da2015-07-30 12:02:15 -07002343
bsalomon@google.comc6980972011-11-02 19:57:21 +00002344 size_t readDstRowBytes = tightRowBytes;
2345 void* readDst = buffer;
rmistry@google.comfbfcd562012-08-23 18:09:54 +00002346
bsalomon@google.comc6980972011-11-02 19:57:21 +00002347 // determine if GL can read using the passed rowBytes or if we need
2348 // a scratch buffer.
joshualitt29f86792015-05-29 08:06:48 -07002349 SkAutoSMalloc<32 * sizeof(GrColor)> scratch;
bsalomon@google.comc6980972011-11-02 19:57:21 +00002350 if (rowBytes != tightRowBytes) {
bsalomon9d02b262016-02-01 12:49:30 -08002351 if (this->glCaps().packRowLengthSupport() && !(rowBytes % bytesPerPixel)) {
skia.committer@gmail.com4677acc2013-10-17 07:02:33 +00002352 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH,
bsalomon9d02b262016-02-01 12:49:30 -08002353 static_cast<GrGLint>(rowBytes / bytesPerPixel)));
bsalomon@google.comc6980972011-11-02 19:57:21 +00002354 readDstRowBytes = rowBytes;
2355 } else {
2356 scratch.reset(tightRowBytes * height);
2357 readDst = scratch.get();
2358 }
2359 }
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002360 if (flipY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00002361 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 1));
2362 }
bsalomonf46a1242015-12-15 12:37:38 -08002363 GL_CALL(PixelStorei(GR_GL_PACK_ALIGNMENT, config_alignment(config)));
2364
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002365 GL_CALL(ReadPixels(readRect.fLeft, readRect.fBottom,
2366 readRect.fWidth, readRect.fHeight,
bsalomon76148af2016-01-12 11:13:47 -08002367 externalFormat, externalType, readDst));
bsalomon@google.comc6980972011-11-02 19:57:21 +00002368 if (readDstRowBytes != tightRowBytes) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002369 SkASSERT(this->glCaps().packRowLengthSupport());
bsalomon@google.comc6980972011-11-02 19:57:21 +00002370 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
2371 }
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002372 if (flipY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00002373 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 0));
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002374 flipY = false;
bsalomon@google.com56d11e02011-11-30 19:59:08 +00002375 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002376
2377 // now reverse the order of the rows, since GL's are bottom-to-top, but our
bsalomon@google.comc6980972011-11-02 19:57:21 +00002378 // API presents top-to-bottom. We must preserve the padding contents. Note
2379 // that the above readPixels did not overwrite the padding.
2380 if (readDst == buffer) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002381 SkASSERT(rowBytes == readDstRowBytes);
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002382 if (flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002383 scratch.reset(tightRowBytes);
2384 void* tmpRow = scratch.get();
2385 // flip y in-place by rows
2386 const int halfY = height >> 1;
2387 char* top = reinterpret_cast<char*>(buffer);
2388 char* bottom = top + (height - 1) * rowBytes;
2389 for (int y = 0; y < halfY; y++) {
2390 memcpy(tmpRow, top, tightRowBytes);
2391 memcpy(top, bottom, tightRowBytes);
2392 memcpy(bottom, tmpRow, tightRowBytes);
2393 top += rowBytes;
2394 bottom -= rowBytes;
2395 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00002396 }
2397 } else {
bsalomon9d02b262016-02-01 12:49:30 -08002398 SkASSERT(readDst != buffer);
2399 SkASSERT(rowBytes != tightRowBytes);
bsalomon@google.comc6980972011-11-02 19:57:21 +00002400 // copy from readDst to buffer while flipping y
caryclark@google.comcf6285b2012-06-06 12:09:01 +00002401 // const int halfY = height >> 1;
bsalomon@google.comc6980972011-11-02 19:57:21 +00002402 const char* src = reinterpret_cast<const char*>(readDst);
bsalomon@google.comc4364992011-11-07 15:54:49 +00002403 char* dst = reinterpret_cast<char*>(buffer);
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002404 if (flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002405 dst += (height-1) * rowBytes;
2406 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00002407 for (int y = 0; y < height; y++) {
2408 memcpy(dst, src, tightRowBytes);
2409 src += readDstRowBytes;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002410 if (!flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002411 dst += rowBytes;
2412 } else {
2413 dst -= rowBytes;
2414 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002415 }
2416 }
Brian Salomon71d9d842016-11-03 13:42:00 -04002417 if (!renderTarget) {
2418 this->unbindTextureFBOForPixelOps(GR_GL_FRAMEBUFFER, surface);
2419 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002420 return true;
2421}
2422
egdaniel9cb63402016-06-23 08:37:05 -07002423GrGpuCommandBuffer* GrGLGpu::createCommandBuffer(
egdaniel9cb63402016-06-23 08:37:05 -07002424 const GrGpuCommandBuffer::LoadAndStoreInfo& colorInfo,
2425 const GrGpuCommandBuffer::LoadAndStoreInfo& stencilInfo) {
Brian Salomonc293a292016-11-30 13:38:32 -05002426 return new GrGLGpuCommandBuffer(this);
egdaniel066df7c2016-06-08 14:02:27 -07002427}
2428
brianosman64d094d2016-03-25 06:01:59 -07002429void GrGLGpu::flushRenderTarget(GrGLRenderTarget* target, const SkIRect* bounds, bool disableSRGB) {
egdanield803f272015-03-18 13:01:52 -07002430 SkASSERT(target);
bsalomon6ba6fa12015-03-04 11:57:37 -08002431
Robert Phillips294870f2016-11-11 12:38:40 -05002432 GrGpuResource::UniqueID rtID = target->uniqueID();
egdanield803f272015-03-18 13:01:52 -07002433 if (fHWBoundRenderTargetUniqueID != rtID) {
bsalomon1e0bf7e2015-03-14 12:08:51 -07002434 fStats.incRenderTargetBinds();
egdanield803f272015-03-18 13:01:52 -07002435 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, target->renderFBOID()));
2436#ifdef SK_DEBUG
2437 // don't do this check in Chromium -- this is causing
2438 // lots of repeated command buffer flushes when the compositor is
2439 // rendering with Ganesh, which is really slow; even too slow for
2440 // Debug mode.
cdalton1acea862015-06-02 13:05:52 -07002441 if (kChromium_GrGLDriver != this->glContext().driver()) {
egdanield803f272015-03-18 13:01:52 -07002442 GrGLenum status;
2443 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
2444 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
2445 SkDebugf("GrGLGpu::flushRenderTarget glCheckFramebufferStatus %x\n", status);
2446 }
bsalomon160f24c2015-03-17 15:55:42 -07002447 }
egdanield803f272015-03-18 13:01:52 -07002448#endif
2449 fHWBoundRenderTargetUniqueID = rtID;
bsalomon083617b2016-02-12 12:10:14 -08002450 this->flushViewport(target->getViewport());
brianosman64d094d2016-03-25 06:01:59 -07002451 }
2452
brianosman35b784d2016-05-05 11:52:53 -07002453 if (this->glCaps().srgbWriteControl()) {
brianosman33f6b3f2016-06-02 05:49:21 -07002454 this->flushFramebufferSRGB(GrPixelConfigIsSRGB(target->config()) && !disableSRGB);
bsalomon5cd020f2015-03-17 12:46:56 -07002455 }
brianosman64d094d2016-03-25 06:01:59 -07002456
bsalomon083617b2016-02-12 12:10:14 -08002457 this->didWriteToSurface(target, bounds);
2458}
bsalomona9909122016-01-23 10:41:40 -08002459
brianosman33f6b3f2016-06-02 05:49:21 -07002460void GrGLGpu::flushFramebufferSRGB(bool enable) {
2461 if (enable && kYes_TriState != fHWSRGBFramebuffer) {
2462 GL_CALL(Enable(GR_GL_FRAMEBUFFER_SRGB));
2463 fHWSRGBFramebuffer = kYes_TriState;
2464 } else if (!enable && kNo_TriState != fHWSRGBFramebuffer) {
2465 GL_CALL(Disable(GR_GL_FRAMEBUFFER_SRGB));
2466 fHWSRGBFramebuffer = kNo_TriState;
2467 }
2468}
2469
bsalomon083617b2016-02-12 12:10:14 -08002470void GrGLGpu::flushViewport(const GrGLIRect& viewport) {
2471 if (fHWViewport != viewport) {
2472 viewport.pushToGLViewport(this->glInterface());
2473 fHWViewport = viewport;
2474 }
2475}
2476
bsalomon@google.comd302f142011-03-03 13:54:13 +00002477#define SWAP_PER_DRAW 0
2478
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00002479#if SWAP_PER_DRAW
commit-bot@chromium.org43823302013-09-25 20:57:51 +00002480 #if defined(SK_BUILD_FOR_MAC)
bsalomon@google.comd302f142011-03-03 13:54:13 +00002481 #include <AGL/agl.h>
commit-bot@chromium.org43823302013-09-25 20:57:51 +00002482 #elif defined(SK_BUILD_FOR_WIN32)
bsalomon@google.comce7357d2012-06-25 17:49:25 +00002483 #include <gl/GL.h>
bsalomon@google.comd302f142011-03-03 13:54:13 +00002484 void SwapBuf() {
2485 DWORD procID = GetCurrentProcessId();
2486 HWND hwnd = GetTopWindow(GetDesktopWindow());
2487 while(hwnd) {
2488 DWORD wndProcID = 0;
2489 GetWindowThreadProcessId(hwnd, &wndProcID);
2490 if(wndProcID == procID) {
2491 SwapBuffers(GetDC(hwnd));
2492 }
2493 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
2494 }
2495 }
2496 #endif
2497#endif
2498
egdaniel9cb63402016-06-23 08:37:05 -07002499void GrGLGpu::draw(const GrPipeline& pipeline,
2500 const GrPrimitiveProcessor& primProc,
bsalomon2eda5b32016-09-21 10:53:24 -07002501 const GrMesh meshes[],
Chris Dalton46983b72017-06-06 12:27:16 -06002502 const GrPipeline::DynamicState dynamicStates[],
egdaniel9cb63402016-06-23 08:37:05 -07002503 int meshCount) {
2504 this->handleDirtyContext();
2505
bsalomon2eda5b32016-09-21 10:53:24 -07002506 bool hasPoints = false;
2507 for (int i = 0; i < meshCount; ++i) {
Chris Dalton3809bab2017-06-13 10:55:06 -06002508 if (meshes[i].primitiveType() == GrPrimitiveType::kPoints) {
bsalomon2eda5b32016-09-21 10:53:24 -07002509 hasPoints = true;
2510 break;
2511 }
2512 }
2513 if (!this->flushGLState(pipeline, primProc, hasPoints)) {
bsalomond95263c2014-12-16 13:05:12 -08002514 return;
2515 }
ethannicholas22793252016-01-30 09:59:10 -08002516
egdaniel0e1853c2016-03-17 11:35:45 -07002517 for (int i = 0; i < meshCount; ++i) {
2518 if (GrXferBarrierType barrierType = pipeline.xferBarrierType(*this->caps())) {
2519 this->xferBarrier(pipeline.getRenderTarget(), barrierType);
2520 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002521
Chris Dalton46983b72017-06-06 12:27:16 -06002522 if (dynamicStates) {
2523 if (pipeline.getScissorState().enabled()) {
2524 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(pipeline.getRenderTarget());
2525 this->flushScissor(dynamicStates[i].fScissorRect,
2526 glRT->getViewport(), glRT->origin());
2527 }
2528 }
Brian Salomon6d9c88b2017-06-12 10:24:42 -04002529 if (this->glCaps().requiresCullFaceEnableDisableWhenDrawingLinesAfterNonLines() &&
2530 GrIsPrimTypeLines(meshes[i].primitiveType()) &&
2531 !GrIsPrimTypeLines(fLastPrimitiveType)) {
2532 GL_CALL(Enable(GR_GL_CULL_FACE));
2533 GL_CALL(Disable(GR_GL_CULL_FACE));
2534 }
Chris Dalton114a3c02017-05-26 15:17:19 -06002535 meshes[i].sendToGpu(primProc, this);
Brian Salomon6d9c88b2017-06-12 10:24:42 -04002536 fLastPrimitiveType = meshes[i].primitiveType();
bsalomon@google.com74749cd2013-01-30 16:12:41 +00002537 }
ethannicholas22793252016-01-30 09:59:10 -08002538
bsalomon@google.comd302f142011-03-03 13:54:13 +00002539#if SWAP_PER_DRAW
2540 glFlush();
commit-bot@chromium.org43823302013-09-25 20:57:51 +00002541 #if defined(SK_BUILD_FOR_MAC)
bsalomon@google.comd302f142011-03-03 13:54:13 +00002542 aglSwapBuffers(aglGetCurrentContext());
2543 int set_a_break_pt_here = 9;
2544 aglSwapBuffers(aglGetCurrentContext());
commit-bot@chromium.org43823302013-09-25 20:57:51 +00002545 #elif defined(SK_BUILD_FOR_WIN32)
bsalomon@google.comd302f142011-03-03 13:54:13 +00002546 SwapBuf();
2547 int set_a_break_pt_here = 9;
2548 SwapBuf();
2549 #endif
2550#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00002551}
2552
Chris Dalton3809bab2017-06-13 10:55:06 -06002553static GrGLenum gr_primitive_type_to_gl_mode(GrPrimitiveType primitiveType) {
2554 switch (primitiveType) {
2555 case GrPrimitiveType::kTriangles:
2556 return GR_GL_TRIANGLES;
2557 case GrPrimitiveType::kTriangleStrip:
2558 return GR_GL_TRIANGLE_STRIP;
2559 case GrPrimitiveType::kTriangleFan:
2560 return GR_GL_TRIANGLE_FAN;
2561 case GrPrimitiveType::kPoints:
2562 return GR_GL_POINTS;
2563 case GrPrimitiveType::kLines:
2564 return GR_GL_LINES;
2565 case GrPrimitiveType::kLineStrip:
2566 return GR_GL_LINE_STRIP;
2567 case GrPrimitiveType::kLinesAdjacency:
2568 return GR_GL_LINES_ADJACENCY;
2569 }
2570 SkFAIL("invalid GrPrimitiveType");
2571 return GR_GL_TRIANGLES;
2572}
2573
Chris Dalton114a3c02017-05-26 15:17:19 -06002574void GrGLGpu::sendMeshToGpu(const GrPrimitiveProcessor& primProc, GrPrimitiveType primitiveType,
2575 const GrBuffer* vertexBuffer, int vertexCount, int baseVertex) {
Chris Dalton3809bab2017-06-13 10:55:06 -06002576 const GrGLenum glPrimType = gr_primitive_type_to_gl_mode(primitiveType);
Chris Dalton114a3c02017-05-26 15:17:19 -06002577 if (this->glCaps().drawArraysBaseVertexIsBroken()) {
Chris Dalton1d616352017-05-31 12:51:23 -06002578 this->setupGeometry(primProc, nullptr, vertexBuffer, baseVertex, nullptr, 0);
Chris Dalton114a3c02017-05-26 15:17:19 -06002579 GL_CALL(DrawArrays(glPrimType, 0, vertexCount));
2580 } else {
Chris Dalton1d616352017-05-31 12:51:23 -06002581 this->setupGeometry(primProc, nullptr, vertexBuffer, 0, nullptr, 0);
Chris Dalton114a3c02017-05-26 15:17:19 -06002582 GL_CALL(DrawArrays(glPrimType, baseVertex, vertexCount));
2583 }
2584 fStats.incNumDraws();
2585}
2586
2587void GrGLGpu::sendIndexedMeshToGpu(const GrPrimitiveProcessor& primProc,
2588 GrPrimitiveType primitiveType, const GrBuffer* indexBuffer,
2589 int indexCount, int baseIndex, uint16_t minIndexValue,
2590 uint16_t maxIndexValue, const GrBuffer* vertexBuffer,
2591 int baseVertex) {
Chris Dalton3809bab2017-06-13 10:55:06 -06002592 const GrGLenum glPrimType = gr_primitive_type_to_gl_mode(primitiveType);
Chris Dalton114a3c02017-05-26 15:17:19 -06002593 GrGLvoid* const indices = reinterpret_cast<void*>(indexBuffer->baseOffset() +
2594 sizeof(uint16_t) * baseIndex);
2595
Chris Dalton1d616352017-05-31 12:51:23 -06002596 this->setupGeometry(primProc, indexBuffer, vertexBuffer, baseVertex, nullptr, 0);
Chris Dalton114a3c02017-05-26 15:17:19 -06002597
2598 if (this->glCaps().drawRangeElementsSupport()) {
2599 GL_CALL(DrawRangeElements(glPrimType, minIndexValue, maxIndexValue, indexCount,
2600 GR_GL_UNSIGNED_SHORT, indices));
2601 } else {
2602 GL_CALL(DrawElements(glPrimType, indexCount, GR_GL_UNSIGNED_SHORT, indices));
2603 }
2604 fStats.incNumDraws();
2605}
2606
Chris Dalton1d616352017-05-31 12:51:23 -06002607void GrGLGpu::sendInstancedMeshToGpu(const GrPrimitiveProcessor& primProc, GrPrimitiveType
2608 primitiveType, const GrBuffer* vertexBuffer,
2609 int vertexCount, int baseVertex,
2610 const GrBuffer* instanceBuffer, int instanceCount,
2611 int baseInstance) {
Chris Dalton3809bab2017-06-13 10:55:06 -06002612 const GrGLenum glPrimType = gr_primitive_type_to_gl_mode(primitiveType);
Chris Dalton1d616352017-05-31 12:51:23 -06002613 this->setupGeometry(primProc, nullptr, vertexBuffer, 0, instanceBuffer, baseInstance);
2614 GL_CALL(DrawArraysInstanced(glPrimType, baseVertex, vertexCount, instanceCount));
2615 fStats.incNumDraws();
2616}
2617
2618void GrGLGpu::sendIndexedInstancedMeshToGpu(const GrPrimitiveProcessor& primProc,
2619 GrPrimitiveType primitiveType,
2620 const GrBuffer* indexBuffer, int indexCount,
2621 int baseIndex, const GrBuffer* vertexBuffer,
2622 int baseVertex, const GrBuffer* instanceBuffer,
2623 int instanceCount, int baseInstance) {
Chris Dalton3809bab2017-06-13 10:55:06 -06002624 const GrGLenum glPrimType = gr_primitive_type_to_gl_mode(primitiveType);
Chris Dalton1d616352017-05-31 12:51:23 -06002625 GrGLvoid* indices = reinterpret_cast<void*>(indexBuffer->baseOffset() +
2626 sizeof(uint16_t) * baseIndex);
2627 this->setupGeometry(primProc, indexBuffer, vertexBuffer, baseVertex,
2628 instanceBuffer, baseInstance);
2629 GL_CALL(DrawElementsInstanced(glPrimType, indexCount, GR_GL_UNSIGNED_SHORT, indices,
2630 instanceCount));
2631 fStats.incNumDraws();
2632}
2633
bsalomon861e1032014-12-16 07:33:49 -08002634void GrGLGpu::onResolveRenderTarget(GrRenderTarget* target) {
bsalomon@google.com75f9f252012-01-31 13:35:56 +00002635 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00002636 if (rt->needsResolve()) {
bsalomon@google.com347c3822013-05-01 20:10:01 +00002637 // Some extensions automatically resolves the texture when it is read.
2638 if (this->glCaps().usesMSAARenderBuffers()) {
egdanield803f272015-03-18 13:01:52 -07002639 SkASSERT(rt->textureFBOID() != rt->renderFBOID());
2640 fStats.incRenderTargetBinds();
2641 fStats.incRenderTargetBinds();
2642 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER, rt->renderFBOID()));
2643 GL_CALL(BindFramebuffer(GR_GL_DRAW_FRAMEBUFFER, rt->textureFBOID()));
2644 // make sure we go through flushRenderTarget() since we've modified
2645 // the bound DRAW FBO ID.
Robert Phillips294870f2016-11-11 12:38:40 -05002646 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002647 const GrGLIRect& vp = rt->getViewport();
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00002648 const SkIRect dirtyRect = rt->getResolveRect();
reed@google.comac10a2d2010-12-22 21:39:39 +00002649
bsalomon@google.com347c3822013-05-01 20:10:01 +00002650 if (GrGLCaps::kES_Apple_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002651 // Apple's extension uses the scissor as the blit bounds.
bsalomon3e791242014-12-17 13:43:13 -08002652 GrScissorState scissorState;
robertphillipse85a32d2015-02-10 08:16:55 -08002653 scissorState.set(dirtyRect);
2654 this->flushScissor(scissorState, vp, rt->origin());
csmartdalton28341fa2016-08-17 10:00:21 -07002655 this->disableWindowRectangles();
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002656 GL_CALL(ResolveMultisampleFramebuffer());
2657 } else {
Brian Salomone5e7eb12016-10-14 16:18:33 -04002658 int l, b, r, t;
2659 if (GrGLCaps::kResolveMustBeFull_BlitFrambufferFlag &
2660 this->glCaps().blitFramebufferSupportFlags()) {
2661 l = 0;
2662 b = 0;
2663 r = target->width();
2664 t = target->height();
2665 } else {
2666 GrGLIRect rect;
2667 rect.setRelativeTo(vp, dirtyRect.fLeft, dirtyRect.fTop,
2668 dirtyRect.width(), dirtyRect.height(), target->origin());
2669 l = rect.fLeft;
2670 b = rect.fBottom;
2671 r = rect.fLeft + rect.fWidth;
2672 t = rect.fBottom + rect.fHeight;
2673 }
derekf8c8f71a2014-09-16 06:24:57 -07002674
2675 // BlitFrameBuffer respects the scissor, so disable it.
joshualitt77b13072014-10-27 14:51:01 -07002676 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07002677 this->disableWindowRectangles();
Brian Salomone5e7eb12016-10-14 16:18:33 -04002678 GL_CALL(BlitFramebuffer(l, b, r, t, l, b, r, t,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002679 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00002680 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002681 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00002682 rt->flagAsResolved();
reed@google.comac10a2d2010-12-22 21:39:39 +00002683 }
2684}
2685
bsalomon@google.com411dad02012-06-05 20:24:20 +00002686namespace {
bsalomon@google.comd302f142011-03-03 13:54:13 +00002687
bsalomon@google.com411dad02012-06-05 20:24:20 +00002688
2689GrGLenum gr_to_gl_stencil_op(GrStencilOp op) {
cdalton93a379b2016-05-11 13:58:08 -07002690 static const GrGLenum gTable[kGrStencilOpCount] = {
2691 GR_GL_KEEP, // kKeep
2692 GR_GL_ZERO, // kZero
2693 GR_GL_REPLACE, // kReplace
2694 GR_GL_INVERT, // kInvert
2695 GR_GL_INCR_WRAP, // kIncWrap
2696 GR_GL_DECR_WRAP, // kDecWrap
2697 GR_GL_INCR, // kIncClamp
2698 GR_GL_DECR, // kDecClamp
bsalomon@google.com411dad02012-06-05 20:24:20 +00002699 };
cdalton93a379b2016-05-11 13:58:08 -07002700 GR_STATIC_ASSERT(0 == (int)GrStencilOp::kKeep);
2701 GR_STATIC_ASSERT(1 == (int)GrStencilOp::kZero);
2702 GR_STATIC_ASSERT(2 == (int)GrStencilOp::kReplace);
2703 GR_STATIC_ASSERT(3 == (int)GrStencilOp::kInvert);
2704 GR_STATIC_ASSERT(4 == (int)GrStencilOp::kIncWrap);
2705 GR_STATIC_ASSERT(5 == (int)GrStencilOp::kDecWrap);
2706 GR_STATIC_ASSERT(6 == (int)GrStencilOp::kIncClamp);
2707 GR_STATIC_ASSERT(7 == (int)GrStencilOp::kDecClamp);
2708 SkASSERT(op < (GrStencilOp)kGrStencilOpCount);
2709 return gTable[(int)op];
bsalomon@google.com411dad02012-06-05 20:24:20 +00002710}
2711
2712void set_gl_stencil(const GrGLInterface* gl,
cdalton93a379b2016-05-11 13:58:08 -07002713 const GrStencilSettings::Face& face,
2714 GrGLenum glFace) {
2715 GrGLenum glFunc = GrToGLStencilFunc(face.fTest);
2716 GrGLenum glFailOp = gr_to_gl_stencil_op(face.fFailOp);
2717 GrGLenum glPassOp = gr_to_gl_stencil_op(face.fPassOp);
bsalomon@google.coma3201942012-06-21 19:58:20 +00002718
cdalton93a379b2016-05-11 13:58:08 -07002719 GrGLint ref = face.fRef;
2720 GrGLint mask = face.fTestMask;
2721 GrGLint writeMask = face.fWriteMask;
bsalomon@google.com411dad02012-06-05 20:24:20 +00002722
2723 if (GR_GL_FRONT_AND_BACK == glFace) {
2724 // we call the combined func just in case separate stencil is not
2725 // supported.
2726 GR_GL_CALL(gl, StencilFunc(glFunc, ref, mask));
2727 GR_GL_CALL(gl, StencilMask(writeMask));
vbuzinovc5d58f02015-08-21 05:24:24 -07002728 GR_GL_CALL(gl, StencilOp(glFailOp, GR_GL_KEEP, glPassOp));
bsalomon@google.com411dad02012-06-05 20:24:20 +00002729 } else {
2730 GR_GL_CALL(gl, StencilFuncSeparate(glFace, glFunc, ref, mask));
2731 GR_GL_CALL(gl, StencilMaskSeparate(glFace, writeMask));
vbuzinovc5d58f02015-08-21 05:24:24 -07002732 GR_GL_CALL(gl, StencilOpSeparate(glFace, glFailOp, GR_GL_KEEP, glPassOp));
bsalomon@google.com411dad02012-06-05 20:24:20 +00002733 }
2734}
2735}
bsalomon@google.comd302f142011-03-03 13:54:13 +00002736
bsalomon3e791242014-12-17 13:43:13 -08002737void GrGLGpu::flushStencil(const GrStencilSettings& stencilSettings) {
csmartdaltonc7d85332016-10-26 10:13:46 -07002738 if (stencilSettings.isDisabled()) {
2739 this->disableStencil();
2740 } else if (fHWStencilSettings != stencilSettings) {
2741 if (kYes_TriState != fHWStencilTestEnabled) {
2742 GL_CALL(Enable(GR_GL_STENCIL_TEST));
Brian Salomonaf971de2017-06-08 16:11:33 -04002743
csmartdaltonc7d85332016-10-26 10:13:46 -07002744 fHWStencilTestEnabled = kYes_TriState;
bsalomon@google.coma3201942012-06-21 19:58:20 +00002745 }
csmartdaltonc7d85332016-10-26 10:13:46 -07002746 if (stencilSettings.isTwoSided()) {
csmartdaltonc7d85332016-10-26 10:13:46 -07002747 set_gl_stencil(this->glInterface(),
2748 stencilSettings.front(),
2749 GR_GL_FRONT);
2750 set_gl_stencil(this->glInterface(),
2751 stencilSettings.back(),
2752 GR_GL_BACK);
2753 } else {
2754 set_gl_stencil(this->glInterface(),
2755 stencilSettings.front(),
2756 GR_GL_FRONT_AND_BACK);
bsalomon@google.comd302f142011-03-03 13:54:13 +00002757 }
joshualitta58fe352014-10-27 08:39:00 -07002758 fHWStencilSettings = stencilSettings;
reed@google.comac10a2d2010-12-22 21:39:39 +00002759 }
2760}
2761
csmartdaltonc7d85332016-10-26 10:13:46 -07002762void GrGLGpu::disableStencil() {
2763 if (kNo_TriState != fHWStencilTestEnabled) {
2764 GL_CALL(Disable(GR_GL_STENCIL_TEST));
Brian Salomonaf971de2017-06-08 16:11:33 -04002765
csmartdaltonc7d85332016-10-26 10:13:46 -07002766 fHWStencilTestEnabled = kNo_TriState;
2767 fHWStencilSettings.invalidate();
2768 }
2769}
2770
cdaltonaf8bc7d2016-02-05 09:35:20 -08002771void GrGLGpu::flushHWAAState(GrRenderTarget* rt, bool useHWAA, bool stencilEnabled) {
bsalomon083617b2016-02-12 12:10:14 -08002772 // rt is only optional if useHWAA is false.
2773 SkASSERT(rt || !useHWAA);
vbuzinovdded6962015-06-12 08:59:45 -07002774 SkASSERT(!useHWAA || rt->isStencilBufferMultisampled());
bsalomon@google.com202d1392013-03-19 18:58:08 +00002775
csmartdalton2b5f2cb2016-06-10 14:06:32 -07002776 if (this->caps()->multisampleDisableSupport()) {
cdaltond0a840d2015-03-16 17:19:58 -07002777 if (useHWAA) {
2778 if (kYes_TriState != fMSAAEnabled) {
2779 GL_CALL(Enable(GR_GL_MULTISAMPLE));
2780 fMSAAEnabled = kYes_TriState;
2781 }
2782 } else {
2783 if (kNo_TriState != fMSAAEnabled) {
2784 GL_CALL(Disable(GR_GL_MULTISAMPLE));
2785 fMSAAEnabled = kNo_TriState;
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00002786 }
2787 }
2788 }
cdaltonaf8bc7d2016-02-05 09:35:20 -08002789
2790 if (0 != this->caps()->maxRasterSamples()) {
Brian Salomon7c8460e2017-05-12 11:36:10 -04002791 if (useHWAA && GrFSAAType::kMixedSamples == rt->fsaaType() && !stencilEnabled) {
cdaltonaf8bc7d2016-02-05 09:35:20 -08002792 // Since stencil is disabled and we want more samples than are in the color buffer, we
2793 // need to tell the rasterizer explicitly how many to run.
2794 if (kYes_TriState != fHWRasterMultisampleEnabled) {
2795 GL_CALL(Enable(GR_GL_RASTER_MULTISAMPLE));
2796 fHWRasterMultisampleEnabled = kYes_TriState;
2797 }
2798 if (rt->numStencilSamples() != fHWNumRasterSamples) {
2799 SkASSERT(rt->numStencilSamples() <= this->caps()->maxRasterSamples());
2800 GL_CALL(RasterSamples(rt->numStencilSamples(), GR_GL_TRUE));
2801 fHWNumRasterSamples = rt->numStencilSamples();
2802 }
2803 } else {
2804 if (kNo_TriState != fHWRasterMultisampleEnabled) {
2805 GL_CALL(Disable(GR_GL_RASTER_MULTISAMPLE));
2806 fHWRasterMultisampleEnabled = kNo_TriState;
2807 }
2808 }
2809 } else {
Brian Salomon7c8460e2017-05-12 11:36:10 -04002810 SkASSERT(!useHWAA || GrFSAAType::kMixedSamples != rt->fsaaType() || stencilEnabled);
cdaltonaf8bc7d2016-02-05 09:35:20 -08002811 }
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00002812}
2813
bsalomon7f9b2e42016-01-12 13:29:26 -08002814void GrGLGpu::flushBlend(const GrXferProcessor::BlendInfo& blendInfo, const GrSwizzle& swizzle) {
egdanielb414f252014-07-29 13:15:47 -07002815 // Any optimization to disable blending should have already been applied and
cdalton8917d622015-05-06 13:40:21 -07002816 // tweaked the equation to "add" or "subtract", and the coeffs to (1, 0).
bsalomonf7cc8772015-05-11 11:21:14 -07002817
cdalton8917d622015-05-06 13:40:21 -07002818 GrBlendEquation equation = blendInfo.fEquation;
egdanielc2304142014-12-11 13:15:13 -08002819 GrBlendCoeff srcCoeff = blendInfo.fSrcBlend;
2820 GrBlendCoeff dstCoeff = blendInfo.fDstBlend;
cdalton8917d622015-05-06 13:40:21 -07002821 bool blendOff = (kAdd_GrBlendEquation == equation || kSubtract_GrBlendEquation == equation) &&
2822 kOne_GrBlendCoeff == srcCoeff && kZero_GrBlendCoeff == dstCoeff;
egdanielb414f252014-07-29 13:15:47 -07002823 if (blendOff) {
2824 if (kNo_TriState != fHWBlendState.fEnabled) {
2825 GL_CALL(Disable(GR_GL_BLEND));
joel.liang9764c402015-07-09 19:46:18 -07002826
2827 // Workaround for the ARM KHR_blend_equation_advanced blacklist issue
2828 // https://code.google.com/p/skia/issues/detail?id=3943
2829 if (kARM_GrGLVendor == this->ctxInfo().vendor() &&
2830 GrBlendEquationIsAdvanced(fHWBlendState.fEquation)) {
2831 SkASSERT(this->caps()->advancedBlendEquationSupport());
2832 // Set to any basic blending equation.
2833 GrBlendEquation blend_equation = kAdd_GrBlendEquation;
2834 GL_CALL(BlendEquation(gXfermodeEquation2Blend[blend_equation]));
2835 fHWBlendState.fEquation = blend_equation;
2836 }
2837
egdanielb414f252014-07-29 13:15:47 -07002838 fHWBlendState.fEnabled = kNo_TriState;
2839 }
cdalton8917d622015-05-06 13:40:21 -07002840 return;
2841 }
2842
2843 if (kYes_TriState != fHWBlendState.fEnabled) {
2844 GL_CALL(Enable(GR_GL_BLEND));
Brian Salomonaf971de2017-06-08 16:11:33 -04002845
cdalton8917d622015-05-06 13:40:21 -07002846 fHWBlendState.fEnabled = kYes_TriState;
2847 }
2848
2849 if (fHWBlendState.fEquation != equation) {
2850 GL_CALL(BlendEquation(gXfermodeEquation2Blend[equation]));
2851 fHWBlendState.fEquation = equation;
2852 }
2853
2854 if (GrBlendEquationIsAdvanced(equation)) {
2855 SkASSERT(this->caps()->advancedBlendEquationSupport());
2856 // Advanced equations have no other blend state.
2857 return;
2858 }
2859
bsalomone63ffef2016-02-05 07:17:34 -08002860 if (fHWBlendState.fSrcCoeff != srcCoeff || fHWBlendState.fDstCoeff != dstCoeff) {
cdalton8917d622015-05-06 13:40:21 -07002861 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
2862 gXfermodeCoeff2Blend[dstCoeff]));
2863 fHWBlendState.fSrcCoeff = srcCoeff;
2864 fHWBlendState.fDstCoeff = dstCoeff;
2865 }
2866
bsalomon7f9b2e42016-01-12 13:29:26 -08002867 if ((BlendCoeffReferencesConstant(srcCoeff) || BlendCoeffReferencesConstant(dstCoeff))) {
2868 GrColor blendConst = blendInfo.fBlendConstant;
2869 blendConst = swizzle.applyTo(blendConst);
2870 if (!fHWBlendState.fConstColorValid || fHWBlendState.fConstColor != blendConst) {
2871 GrGLfloat c[4];
2872 GrColorToRGBAFloat(blendConst, c);
2873 GL_CALL(BlendColor(c[0], c[1], c[2], c[3]));
2874 fHWBlendState.fConstColor = blendConst;
2875 fHWBlendState.fConstColorValid = true;
2876 }
bsalomon@google.com0650e812011-04-08 18:07:53 +00002877 }
2878}
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002879
commit-bot@chromium.org6364b5e2013-08-20 20:22:52 +00002880static inline GrGLenum tile_to_gl_wrap(SkShader::TileMode tm) {
bsalomon@google.comb8670992012-07-25 21:27:09 +00002881 static const GrGLenum gWrapModes[] = {
2882 GR_GL_CLAMP_TO_EDGE,
2883 GR_GL_REPEAT,
2884 GR_GL_MIRRORED_REPEAT
2885 };
commit-bot@chromium.org5d7ca952013-04-22 20:26:44 +00002886 GR_STATIC_ASSERT(SkShader::kTileModeCount == SK_ARRAY_COUNT(gWrapModes));
bsalomon@google.comb8670992012-07-25 21:27:09 +00002887 GR_STATIC_ASSERT(0 == SkShader::kClamp_TileMode);
2888 GR_STATIC_ASSERT(1 == SkShader::kRepeat_TileMode);
2889 GR_STATIC_ASSERT(2 == SkShader::kMirror_TileMode);
2890 return gWrapModes[tm];
2891}
2892
egdanielb7e7d572015-11-04 04:23:53 -08002893static GrGLenum get_component_enum_from_char(char component) {
2894 switch (component) {
2895 case 'r':
2896 return GR_GL_RED;
2897 case 'g':
2898 return GR_GL_GREEN;
2899 case 'b':
2900 return GR_GL_BLUE;
2901 case 'a':
2902 return GR_GL_ALPHA;
2903 default:
2904 SkFAIL("Unsupported component");
2905 return 0;
2906 }
2907}
2908
2909/** If texture swizzling is available using tex parameters then it is preferred over mangling
2910 the generated shader code. This potentially allows greater reuse of cached shaders. */
2911static void get_tex_param_swizzle(GrPixelConfig config,
bsalomoncdee0092016-01-08 13:20:12 -08002912 const GrGLCaps& caps,
egdanielb7e7d572015-11-04 04:23:53 -08002913 GrGLenum* glSwizzle) {
bsalomoncdee0092016-01-08 13:20:12 -08002914 const GrSwizzle& swizzle = caps.configSwizzle(config);
egdanielb7e7d572015-11-04 04:23:53 -08002915 for (int i = 0; i < 4; ++i) {
bsalomoncdee0092016-01-08 13:20:12 -08002916 glSwizzle[i] = get_component_enum_from_char(swizzle.c_str()[i]);
egdaniel574a4c12015-11-02 06:22:44 -08002917 }
2918}
2919
Brian Salomon514baff2016-11-17 15:17:07 -05002920void GrGLGpu::bindTexture(int unitIdx, const GrSamplerParams& params, bool allowSRGBInputs,
brianosmana6359362016-03-21 06:55:37 -07002921 GrGLTexture* texture) {
bsalomon49f085d2014-09-05 13:34:00 -07002922 SkASSERT(texture);
tomhudson@google.comd0c1a062012-07-12 17:23:52 +00002923
reed856e9d92015-09-30 12:21:45 -07002924#ifdef SK_DEBUG
2925 if (!this->caps()->npotTextureTileSupport()) {
2926 const bool tileX = SkShader::kClamp_TileMode != params.getTileModeX();
2927 const bool tileY = SkShader::kClamp_TileMode != params.getTileModeY();
2928 if (tileX || tileY) {
2929 const int w = texture->width();
2930 const int h = texture->height();
2931 SkASSERT(SkIsPow2(w) && SkIsPow2(h));
2932 }
2933 }
2934#endif
2935
bsalomon@google.comb8670992012-07-25 21:27:09 +00002936 // If we created a rt/tex and rendered to it without using a texture and now we're texturing
2937 // 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 +00002938 // out of the "last != next" check.
bsalomon37dd3312014-11-03 08:47:23 -08002939 GrGLRenderTarget* texRT = static_cast<GrGLRenderTarget*>(texture->asRenderTarget());
bsalomon49f085d2014-09-05 13:34:00 -07002940 if (texRT) {
bsalomon@google.com4c883782012-06-04 19:05:11 +00002941 this->onResolveRenderTarget(texRT);
2942 }
2943
Robert Phillips294870f2016-11-11 12:38:40 -05002944 GrGpuResource::UniqueID textureID = texture->uniqueID();
bsalomon10528f12015-10-14 12:54:52 -07002945 GrGLenum target = texture->target();
bsalomon1c63bf62014-07-22 13:09:46 -07002946 if (fHWBoundTextureUniqueIDs[unitIdx] != textureID) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002947 this->setTextureUnit(unitIdx);
bsalomon10528f12015-10-14 12:54:52 -07002948 GL_CALL(BindTexture(target, texture->textureID()));
bsalomon1c63bf62014-07-22 13:09:46 -07002949 fHWBoundTextureUniqueIDs[unitIdx] = textureID;
bsalomon@google.com4c883782012-06-04 19:05:11 +00002950 }
2951
bsalomon@google.com4c883782012-06-04 19:05:11 +00002952 ResetTimestamp timestamp;
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002953 const GrGLTexture::TexParams& oldTexParams = texture->getCachedTexParams(&timestamp);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002954 bool setAll = timestamp < this->getResetTimestamp();
2955 GrGLTexture::TexParams newTexParams;
2956
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00002957 static GrGLenum glMinFilterModes[] = {
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +00002958 GR_GL_NEAREST,
2959 GR_GL_LINEAR,
2960 GR_GL_LINEAR_MIPMAP_LINEAR
2961 };
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00002962 static GrGLenum glMagFilterModes[] = {
2963 GR_GL_NEAREST,
2964 GR_GL_LINEAR,
2965 GR_GL_LINEAR
2966 };
Brian Salomon514baff2016-11-17 15:17:07 -05002967 GrSamplerParams::FilterMode filterMode = params.filterMode();
bsalomonefd7d452014-10-23 14:17:46 -07002968
Brian Salomon514baff2016-11-17 15:17:07 -05002969 if (GrSamplerParams::kMipMap_FilterMode == filterMode) {
Robert Phillips92de6312017-05-23 07:43:48 -04002970 if (!this->caps()->mipMapSupport()) {
Brian Salomon514baff2016-11-17 15:17:07 -05002971 filterMode = GrSamplerParams::kBilerp_FilterMode;
bsalomonefd7d452014-10-23 14:17:46 -07002972 }
commit-bot@chromium.org47442312013-12-19 16:18:01 +00002973 }
bsalomonefd7d452014-10-23 14:17:46 -07002974
commit-bot@chromium.org47442312013-12-19 16:18:01 +00002975 newTexParams.fMinFilter = glMinFilterModes[filterMode];
2976 newTexParams.fMagFilter = glMagFilterModes[filterMode];
skia.committer@gmail.comaeefb2a2013-07-27 07:01:06 +00002977
brianosman20471892016-12-02 06:43:32 -08002978 if (this->glCaps().srgbDecodeDisableSupport() && GrPixelConfigIsSRGB(texture->config())) {
brianosmanfe199872016-06-13 07:59:48 -07002979 newTexParams.fSRGBDecode = allowSRGBInputs ? GR_GL_DECODE_EXT : GR_GL_SKIP_DECODE_EXT;
brianosman33f6b3f2016-06-02 05:49:21 -07002980 if (setAll || newTexParams.fSRGBDecode != oldTexParams.fSRGBDecode) {
brianosmanadf4fa62016-04-26 16:48:06 -07002981 this->setTextureUnit(unitIdx);
brianosman33f6b3f2016-06-02 05:49:21 -07002982 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SRGB_DECODE_EXT, newTexParams.fSRGBDecode));
cblume55f2d2d2016-02-26 13:20:48 -08002983 }
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +00002984 }
bsalomon@google.com4c883782012-06-04 19:05:11 +00002985
brianosman33f6b3f2016-06-02 05:49:21 -07002986#ifdef SK_DEBUG
2987 // We were supposed to ensure MipMaps were up-to-date and built correctly before getting here.
Brian Salomon514baff2016-11-17 15:17:07 -05002988 if (GrSamplerParams::kMipMap_FilterMode == filterMode) {
brianosman33f6b3f2016-06-02 05:49:21 -07002989 SkASSERT(!texture->texturePriv().mipMapsAreDirty());
2990 if (GrPixelConfigIsSRGB(texture->config())) {
Brian Osman7b8400d2016-11-08 17:08:54 -05002991 SkDestinationSurfaceColorMode colorMode = allowSRGBInputs
2992 ? SkDestinationSurfaceColorMode::kGammaAndColorSpaceAware
2993 : SkDestinationSurfaceColorMode::kLegacy;
2994 SkASSERT(texture->texturePriv().mipColorMode() == colorMode);
brianosman33f6b3f2016-06-02 05:49:21 -07002995 }
2996 }
2997#endif
2998
cblume55f2d2d2016-02-26 13:20:48 -08002999 newTexParams.fMaxMipMapLevel = texture->texturePriv().maxMipMapLevel();
3000
bsalomon@google.comb8670992012-07-25 21:27:09 +00003001 newTexParams.fWrapS = tile_to_gl_wrap(params.getTileModeX());
3002 newTexParams.fWrapT = tile_to_gl_wrap(params.getTileModeY());
bsalomoncdee0092016-01-08 13:20:12 -08003003 get_tex_param_swizzle(texture->config(), this->glCaps(), newTexParams.fSwizzleRGBA);
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00003004 if (setAll || newTexParams.fMagFilter != oldTexParams.fMagFilter) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00003005 this->setTextureUnit(unitIdx);
bsalomon10528f12015-10-14 12:54:52 -07003006 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MAG_FILTER, newTexParams.fMagFilter));
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00003007 }
3008 if (setAll || newTexParams.fMinFilter != oldTexParams.fMinFilter) {
3009 this->setTextureUnit(unitIdx);
bsalomon10528f12015-10-14 12:54:52 -07003010 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MIN_FILTER, newTexParams.fMinFilter));
bsalomon@google.com4c883782012-06-04 19:05:11 +00003011 }
cblume55f2d2d2016-02-26 13:20:48 -08003012 if (setAll || newTexParams.fMaxMipMapLevel != oldTexParams.fMaxMipMapLevel) {
cblume09bd2c02016-03-01 14:08:28 -08003013 // These are not supported in ES2 contexts
3014 if (this->glCaps().mipMapLevelAndLodControlSupport()) {
3015 if (newTexParams.fMaxMipMapLevel != 0) {
3016 this->setTextureUnit(unitIdx);
3017 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MIN_LOD, 0));
3018 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_BASE_LEVEL, 0));
3019 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MAX_LOD,
3020 newTexParams.fMaxMipMapLevel));
3021 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MAX_LEVEL,
3022 newTexParams.fMaxMipMapLevel));
3023 }
cblume55f2d2d2016-02-26 13:20:48 -08003024 }
3025 }
bsalomon@google.com4c883782012-06-04 19:05:11 +00003026 if (setAll || newTexParams.fWrapS != oldTexParams.fWrapS) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00003027 this->setTextureUnit(unitIdx);
bsalomon10528f12015-10-14 12:54:52 -07003028 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_WRAP_S, newTexParams.fWrapS));
bsalomon@google.com4c883782012-06-04 19:05:11 +00003029 }
3030 if (setAll || newTexParams.fWrapT != oldTexParams.fWrapT) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00003031 this->setTextureUnit(unitIdx);
bsalomon10528f12015-10-14 12:54:52 -07003032 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_WRAP_T, newTexParams.fWrapT));
bsalomon@google.com4c883782012-06-04 19:05:11 +00003033 }
bsalomoncdee0092016-01-08 13:20:12 -08003034 if (this->glCaps().textureSwizzleSupport() &&
bsalomon@google.com4c883782012-06-04 19:05:11 +00003035 (setAll || memcmp(newTexParams.fSwizzleRGBA,
3036 oldTexParams.fSwizzleRGBA,
3037 sizeof(newTexParams.fSwizzleRGBA)))) {
cdalton74b8d322016-04-11 14:47:28 -07003038 this->setTextureSwizzle(unitIdx, target, newTexParams.fSwizzleRGBA);
bsalomon@google.com4c883782012-06-04 19:05:11 +00003039 }
bsalomon@google.com34cccde2013-01-04 18:34:30 +00003040 texture->setCachedTexParams(newTexParams, this->getResetTimestamp());
bsalomon@google.com4c883782012-06-04 19:05:11 +00003041}
3042
csmartdalton1897cfd2016-06-03 08:50:54 -07003043void GrGLGpu::bindTexelBuffer(int unitIdx, GrPixelConfig texelConfig, GrGLBuffer* buffer) {
cdalton74b8d322016-04-11 14:47:28 -07003044 SkASSERT(this->glCaps().canUseConfigWithTexelBuffer(texelConfig));
3045 SkASSERT(unitIdx >= 0 && unitIdx < fHWBufferTextures.count());
cdalton74b8d322016-04-11 14:47:28 -07003046
3047 BufferTexture& buffTex = fHWBufferTextures[unitIdx];
3048
3049 if (!buffTex.fKnownBound) {
3050 if (!buffTex.fTextureID) {
3051 GL_CALL(GenTextures(1, &buffTex.fTextureID));
3052 if (!buffTex.fTextureID) {
3053 return;
3054 }
3055 }
3056
3057 this->setTextureUnit(unitIdx);
3058 GL_CALL(BindTexture(GR_GL_TEXTURE_BUFFER, buffTex.fTextureID));
3059
3060 buffTex.fKnownBound = true;
3061 }
3062
robertphillips8abb3702016-08-31 14:04:06 -07003063 if (buffer->uniqueID() != buffTex.fAttachedBufferUniqueID ||
csmartdalton1897cfd2016-06-03 08:50:54 -07003064 buffTex.fTexelConfig != texelConfig) {
cdalton74b8d322016-04-11 14:47:28 -07003065
3066 this->setTextureUnit(unitIdx);
csmartdalton1897cfd2016-06-03 08:50:54 -07003067 GL_CALL(TexBuffer(GR_GL_TEXTURE_BUFFER,
3068 this->glCaps().configSizedInternalFormat(texelConfig),
3069 buffer->bufferID()));
cdalton74b8d322016-04-11 14:47:28 -07003070
cdalton74b8d322016-04-11 14:47:28 -07003071 buffTex.fTexelConfig = texelConfig;
robertphillips8abb3702016-08-31 14:04:06 -07003072 buffTex.fAttachedBufferUniqueID = buffer->uniqueID();
cdalton74b8d322016-04-11 14:47:28 -07003073
3074 if (this->glCaps().textureSwizzleSupport() &&
3075 this->glCaps().configSwizzle(texelConfig) != buffTex.fSwizzle) {
3076 GrGLenum glSwizzle[4];
3077 get_tex_param_swizzle(texelConfig, this->glCaps(), glSwizzle);
3078 this->setTextureSwizzle(unitIdx, GR_GL_TEXTURE_BUFFER, glSwizzle);
3079 buffTex.fSwizzle = this->glCaps().configSwizzle(texelConfig);
3080 }
3081
3082 buffer->setHasAttachedToTexture();
3083 fHWMaxUsedBufferTextureUnit = SkTMax(unitIdx, fHWMaxUsedBufferTextureUnit);
3084 }
3085}
3086
Brian Salomonf9f45122016-11-29 11:59:17 -05003087void GrGLGpu::bindImageStorage(int unitIdx, GrIOType ioType, GrGLTexture *texture) {
3088 SkASSERT(texture);
3089 if (texture->uniqueID() != fHWBoundImageStorages[unitIdx].fTextureUniqueID ||
3090 ioType != fHWBoundImageStorages[unitIdx].fIOType) {
Joe Gregorio94e4ab12016-11-30 14:33:42 -05003091 GrGLenum access = GR_GL_READ_ONLY;
Brian Salomonf9f45122016-11-29 11:59:17 -05003092 switch (ioType) {
3093 case kRead_GrIOType:
3094 access = GR_GL_READ_ONLY;
3095 break;
3096 case kWrite_GrIOType:
3097 access = GR_GL_WRITE_ONLY;
3098 break;
3099 case kRW_GrIOType:
3100 access = GR_GL_READ_WRITE;
3101 break;
3102 }
3103 GrGLenum format = this->glCaps().getImageFormat(texture->config());
3104 GL_CALL(BindImageTexture(unitIdx, texture->textureID(), 0, GR_GL_FALSE, 0, access, format));
3105 }
3106}
3107
Brian Salomon514baff2016-11-17 15:17:07 -05003108void GrGLGpu::generateMipmaps(const GrSamplerParams& params, bool allowSRGBInputs,
brianosman33f6b3f2016-06-02 05:49:21 -07003109 GrGLTexture* texture) {
3110 SkASSERT(texture);
3111
3112 // First, figure out if we need mips for this texture at all:
Brian Salomon514baff2016-11-17 15:17:07 -05003113 GrSamplerParams::FilterMode filterMode = params.filterMode();
brianosman33f6b3f2016-06-02 05:49:21 -07003114
Brian Salomon514baff2016-11-17 15:17:07 -05003115 if (GrSamplerParams::kMipMap_FilterMode == filterMode) {
Robert Phillips92de6312017-05-23 07:43:48 -04003116 if (!this->caps()->mipMapSupport()) {
Brian Salomon514baff2016-11-17 15:17:07 -05003117 filterMode = GrSamplerParams::kBilerp_FilterMode;
brianosman33f6b3f2016-06-02 05:49:21 -07003118 }
3119 }
3120
Brian Salomon514baff2016-11-17 15:17:07 -05003121 if (GrSamplerParams::kMipMap_FilterMode != filterMode) {
brianosman33f6b3f2016-06-02 05:49:21 -07003122 return;
3123 }
3124
3125 // If this is an sRGB texture and the mips were previously built the "other" way
3126 // (gamma-correct vs. not), then we need to rebuild them. We don't need to check for
3127 // srgbSupport - we'll *never* get an sRGB pixel config if we don't support it.
Brian Osman7b8400d2016-11-08 17:08:54 -05003128 SkDestinationSurfaceColorMode colorMode = allowSRGBInputs
3129 ? SkDestinationSurfaceColorMode::kGammaAndColorSpaceAware
3130 : SkDestinationSurfaceColorMode::kLegacy;
brianosman33f6b3f2016-06-02 05:49:21 -07003131 if (GrPixelConfigIsSRGB(texture->config()) &&
Brian Osman7b8400d2016-11-08 17:08:54 -05003132 colorMode != texture->texturePriv().mipColorMode()) {
brianosman33f6b3f2016-06-02 05:49:21 -07003133 texture->texturePriv().dirtyMipMaps(true);
3134 }
3135
3136 // If the mips aren't dirty, we're done:
3137 if (!texture->texturePriv().mipMapsAreDirty()) {
3138 return;
3139 }
3140
3141 // If we created a rt/tex and rendered to it without using a texture and now we're texturing
3142 // from the rt it will still be the last bound texture, but it needs resolving.
3143 GrGLRenderTarget* texRT = static_cast<GrGLRenderTarget*>(texture->asRenderTarget());
3144 if (texRT) {
3145 this->onResolveRenderTarget(texRT);
3146 }
3147
3148 GrGLenum target = texture->target();
3149 this->setScratchTextureUnit();
3150 GL_CALL(BindTexture(target, texture->textureID()));
3151
3152 // Configure sRGB decode, if necessary. This state is the only thing needed for the driver
3153 // call (glGenerateMipmap) to work correctly. Our manual method dirties other state, too.
brianosman20471892016-12-02 06:43:32 -08003154 if (this->glCaps().srgbDecodeDisableSupport() && GrPixelConfigIsSRGB(texture->config())) {
Brian Osmanb0403202016-12-09 10:55:23 -05003155 GrGLenum srgbDecode = allowSRGBInputs ? GR_GL_DECODE_EXT : GR_GL_SKIP_DECODE_EXT;
3156 // Command buffer's sRGB decode extension doesn't influence mipmap generation correctly.
3157 // If we set this to skip_decode, it appears to suppress sRGB -> Linear for each downsample,
3158 // but not the Linear -> sRGB when writing the next level. The result is that mip-chains
3159 // get progressively brighter as you go down. Forcing this to 'decode' gives predictable
3160 // (and only slightly incorrect) results. See crbug.com/655247 (~comment 28)
3161 if (!this->glCaps().srgbDecodeDisableAffectsMipmaps()) {
3162 srgbDecode = GR_GL_DECODE_EXT;
3163 }
3164 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SRGB_DECODE_EXT, srgbDecode));
brianosman33f6b3f2016-06-02 05:49:21 -07003165 }
3166
3167 // Either do manual mipmap generation or (if that fails), just rely on the driver:
3168 if (!this->generateMipmap(texture, allowSRGBInputs)) {
3169 GL_CALL(GenerateMipmap(target));
3170 }
3171
brianosmanfe199872016-06-13 07:59:48 -07003172 texture->texturePriv().dirtyMipMaps(false);
brianosman33f6b3f2016-06-02 05:49:21 -07003173 texture->texturePriv().setMaxMipMapLevel(SkMipMap::ComputeLevelCount(
3174 texture->width(), texture->height()));
Brian Osman7b8400d2016-11-08 17:08:54 -05003175 texture->texturePriv().setMipColorMode(colorMode);
brianosman33f6b3f2016-06-02 05:49:21 -07003176
3177 // We have potentially set lots of state on the texture. Easiest to dirty it all:
3178 texture->textureParamsModified();
3179}
3180
cdalton74b8d322016-04-11 14:47:28 -07003181void GrGLGpu::setTextureSwizzle(int unitIdx, GrGLenum target, const GrGLenum swizzle[]) {
3182 this->setTextureUnit(unitIdx);
3183 if (this->glStandard() == kGLES_GrGLStandard) {
3184 // ES3 added swizzle support but not GL_TEXTURE_SWIZZLE_RGBA.
3185 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_R, swizzle[0]));
3186 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_G, swizzle[1]));
3187 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_B, swizzle[2]));
3188 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_A, swizzle[3]));
3189 } else {
3190 GR_STATIC_ASSERT(sizeof(swizzle[0]) == sizeof(GrGLint));
3191 GL_CALL(TexParameteriv(target, GR_GL_TEXTURE_SWIZZLE_RGBA,
3192 reinterpret_cast<const GrGLint*>(swizzle)));
3193 }
3194}
3195
egdaniel080e6732014-12-22 07:35:52 -08003196void GrGLGpu::flushColorWrite(bool writeColor) {
3197 if (!writeColor) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +00003198 if (kNo_TriState != fHWWriteToColor) {
3199 GL_CALL(ColorMask(GR_GL_FALSE, GR_GL_FALSE,
3200 GR_GL_FALSE, GR_GL_FALSE));
3201 fHWWriteToColor = kNo_TriState;
bsalomon@google.comd302f142011-03-03 13:54:13 +00003202 }
bsalomon@google.com978c8c62012-05-21 14:45:49 +00003203 } else {
3204 if (kYes_TriState != fHWWriteToColor) {
3205 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
3206 fHWWriteToColor = kYes_TriState;
3207 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00003208 }
bsalomon3e791242014-12-17 13:43:13 -08003209}
bsalomon@google.comd302f142011-03-03 13:54:13 +00003210
bsalomon861e1032014-12-16 07:33:49 -08003211void GrGLGpu::setTextureUnit(int unit) {
bsalomon1c63bf62014-07-22 13:09:46 -07003212 SkASSERT(unit >= 0 && unit < fHWBoundTextureUniqueIDs.count());
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00003213 if (unit != fHWActiveTextureUnitIdx) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00003214 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com49209392012-06-05 15:13:46 +00003215 fHWActiveTextureUnitIdx = unit;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00003216 }
3217}
bsalomon@google.com316f99232011-01-13 21:28:12 +00003218
bsalomon861e1032014-12-16 07:33:49 -08003219void GrGLGpu::setScratchTextureUnit() {
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00003220 // Bind the last texture unit since it is the least likely to be used by GrGLProgram.
bsalomon1c63bf62014-07-22 13:09:46 -07003221 int lastUnitIdx = fHWBoundTextureUniqueIDs.count() - 1;
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00003222 if (lastUnitIdx != fHWActiveTextureUnitIdx) {
3223 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + lastUnitIdx));
3224 fHWActiveTextureUnitIdx = lastUnitIdx;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00003225 }
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00003226 // clear out the this field so that if a program does use this unit it will rebind the correct
3227 // texture.
Robert Phillips294870f2016-11-11 12:38:40 -05003228 fHWBoundTextureUniqueIDs[lastUnitIdx].makeInvalid();
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00003229}
3230
Brian Salomone5e7eb12016-10-14 16:18:33 -04003231// Determines whether glBlitFramebuffer could be used between src and dst by onCopySurface.
3232static inline bool can_blit_framebuffer_for_copy_surface(const GrSurface* dst,
3233 const GrSurface* src,
3234 const SkIRect& srcRect,
3235 const SkIPoint& dstPoint,
3236 const GrGLGpu* gpu) {
3237 auto blitFramebufferFlags = gpu->glCaps().blitFramebufferSupportFlags();
Brian Salomon71d9d842016-11-03 13:42:00 -04003238 if (!gpu->glCaps().canConfigBeFBOColorAttachment(dst->config()) ||
3239 !gpu->glCaps().canConfigBeFBOColorAttachment(src->config())) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00003240 return false;
3241 }
Brian Salomonbf7b6202016-11-11 16:08:03 -05003242 // Blits are not allowed between int color buffers and float/fixed color buffers. GrGpu should
3243 // have filtered such cases out.
3244 SkASSERT(GrPixelConfigIsSint(dst->config()) == GrPixelConfigIsSint(src->config()));
Brian Salomone5e7eb12016-10-14 16:18:33 -04003245 const GrGLTexture* dstTex = static_cast<const GrGLTexture*>(dst->asTexture());
3246 const GrGLTexture* srcTex = static_cast<const GrGLTexture*>(dst->asTexture());
3247 const GrRenderTarget* dstRT = dst->asRenderTarget();
3248 const GrRenderTarget* srcRT = src->asRenderTarget();
3249 if (dstTex && dstTex->target() != GR_GL_TEXTURE_2D) {
3250 return false;
3251 }
3252 if (srcTex && srcTex->target() != GR_GL_TEXTURE_2D) {
3253 return false;
3254 }
3255 if (GrGLCaps::kNoSupport_BlitFramebufferFlag & blitFramebufferFlags) {
3256 return false;
3257 }
3258 if (GrGLCaps::kNoScalingOrMirroring_BlitFramebufferFlag & blitFramebufferFlags) {
3259 // We would mirror to compensate for origin changes. Note that copySurface is
3260 // specified such that the src and dst rects are the same.
3261 if (dst->origin() != src->origin()) {
3262 return false;
3263 }
3264 }
3265 if (GrGLCaps::kResolveMustBeFull_BlitFrambufferFlag & blitFramebufferFlags) {
Eric Karl74480882017-04-03 14:49:05 -07003266 if (srcRT && srcRT->numColorSamples()) {
3267 if (dstRT && !dstRT->numColorSamples()) {
3268 return false;
3269 }
3270 if (SkRect::Make(srcRect) != srcRT->getBoundsRect()) {
3271 return false;
3272 }
Brian Salomone5e7eb12016-10-14 16:18:33 -04003273 }
3274 }
3275 if (GrGLCaps::kNoMSAADst_BlitFramebufferFlag & blitFramebufferFlags) {
3276 if (dstRT && dstRT->numColorSamples() > 0) {
3277 return false;
3278 }
3279 }
3280 if (GrGLCaps::kNoFormatConversion_BlitFramebufferFlag & blitFramebufferFlags) {
3281 if (dst->config() != src->config()) {
3282 return false;
3283 }
3284 } else if (GrGLCaps::kNoFormatConversionForMSAASrc_BlitFramebufferFlag & blitFramebufferFlags) {
3285 const GrRenderTarget* srcRT = src->asRenderTarget();
3286 if (srcRT && srcRT->numColorSamples() && dst->config() != src->config()) {
3287 return false;
3288 }
3289 }
3290 if (GrGLCaps::kRectsMustMatchForMSAASrc_BlitFramebufferFlag & blitFramebufferFlags) {
Eric Karl74480882017-04-03 14:49:05 -07003291 if (srcRT && srcRT->numColorSamples()) {
3292 if (dstPoint.fX != srcRect.fLeft || dstPoint.fY != srcRect.fTop) {
3293 return false;
3294 }
3295 if (dst->origin() != src->origin()) {
3296 return false;
3297 }
Brian Salomone5e7eb12016-10-14 16:18:33 -04003298 }
3299 }
3300 return true;
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00003301}
bsalomon@google.comeb851172013-04-15 13:51:00 +00003302
bsalomon7ea33f52015-11-22 14:51:00 -08003303static inline bool can_copy_texsubimage(const GrSurface* dst,
3304 const GrSurface* src,
3305 const GrGLGpu* gpu) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00003306 // Table 3.9 of the ES2 spec indicates the supported formats with CopyTexSubImage
3307 // and BGRA isn't in the spec. There doesn't appear to be any extension that adds it. Perhaps
3308 // many drivers would allow it to work, but ANGLE does not.
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +00003309 if (kGLES_GrGLStandard == gpu->glStandard() && gpu->glCaps().bgraIsInternalFormat() &&
bsalomon@google.comeb851172013-04-15 13:51:00 +00003310 (kBGRA_8888_GrPixelConfig == dst->config() || kBGRA_8888_GrPixelConfig == src->config())) {
3311 return false;
3312 }
3313 const GrGLRenderTarget* dstRT = static_cast<const GrGLRenderTarget*>(dst->asRenderTarget());
3314 // If dst is multisampled (and uses an extension where there is a separate MSAA renderbuffer)
3315 // then we don't want to copy to the texture but to the MSAA buffer.
egdanield803f272015-03-18 13:01:52 -07003316 if (dstRT && dstRT->renderFBOID() != dstRT->textureFBOID()) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00003317 return false;
3318 }
bsalomon@google.coma2719852013-04-17 14:25:27 +00003319 const GrGLRenderTarget* srcRT = static_cast<const GrGLRenderTarget*>(src->asRenderTarget());
3320 // If the src is multisampled (and uses an extension where there is a separate MSAA
3321 // renderbuffer) then it is an invalid operation to call CopyTexSubImage
egdanield803f272015-03-18 13:01:52 -07003322 if (srcRT && srcRT->renderFBOID() != srcRT->textureFBOID()) {
bsalomon@google.coma2719852013-04-17 14:25:27 +00003323 return false;
3324 }
bsalomon7ea33f52015-11-22 14:51:00 -08003325
3326 const GrGLTexture* dstTex = static_cast<const GrGLTexture*>(dst->asTexture());
3327 // CopyTex(Sub)Image writes to a texture and we have no way of dynamically wrapping a RT in a
3328 // texture.
3329 if (!dstTex) {
3330 return false;
3331 }
3332
3333 const GrGLTexture* srcTex = static_cast<const GrGLTexture*>(src->asTexture());
cblume61214052016-01-26 09:10:48 -08003334
bsalomon7ea33f52015-11-22 14:51:00 -08003335 // Check that we could wrap the source in an FBO, that the dst is TEXTURE_2D, that no mirroring
3336 // is required.
Brian Salomon71d9d842016-11-03 13:42:00 -04003337 if (gpu->glCaps().canConfigBeFBOColorAttachment(src->config()) &&
Brian Salomon71d9d842016-11-03 13:42:00 -04003338 (!srcTex || srcTex->target() == GR_GL_TEXTURE_2D) && dstTex->target() == GR_GL_TEXTURE_2D &&
bsalomon7ea33f52015-11-22 14:51:00 -08003339 dst->origin() == src->origin()) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00003340 return true;
3341 } else {
3342 return false;
3343 }
3344}
3345
3346// If a temporary FBO was created, its non-zero ID is returned. The viewport that the copy rect is
3347// relative to is output.
Brian Salomon71d9d842016-11-03 13:42:00 -04003348void GrGLGpu::bindSurfaceFBOForPixelOps(GrSurface* surface, GrGLenum fboTarget, GrGLIRect* viewport,
3349 TempFBOTarget tempFBOTarget) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00003350 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(surface->asRenderTarget());
bsalomon083617b2016-02-12 12:10:14 -08003351 if (!rt) {
bsalomon49f085d2014-09-05 13:34:00 -07003352 SkASSERT(surface->asTexture());
Brian Salomon9bada542017-06-12 12:09:30 -04003353 GrGLTexture* texture = static_cast<GrGLTexture*>(surface->asTexture());
3354 GrGLuint texID = texture->textureID();
3355 GrGLenum target = texture->target();
egdanield803f272015-03-18 13:01:52 -07003356 GrGLuint* tempFBOID;
3357 tempFBOID = kSrc_TempFBOTarget == tempFBOTarget ? &fTempSrcFBOID : &fTempDstFBOID;
egdaniel0f5f9672015-02-03 11:10:51 -08003358
egdanield803f272015-03-18 13:01:52 -07003359 if (0 == *tempFBOID) {
3360 GR_GL_CALL(this->glInterface(), GenFramebuffers(1, tempFBOID));
egdaniel0f5f9672015-02-03 11:10:51 -08003361 }
3362
egdanield803f272015-03-18 13:01:52 -07003363 fStats.incRenderTargetBinds();
3364 GR_GL_CALL(this->glInterface(), BindFramebuffer(fboTarget, *tempFBOID));
3365 GR_GL_CALL(this->glInterface(), FramebufferTexture2D(fboTarget,
robertphillips754f4e92014-09-18 13:52:08 -07003366 GR_GL_COLOR_ATTACHMENT0,
bsalomon10528f12015-10-14 12:54:52 -07003367 target,
robertphillips754f4e92014-09-18 13:52:08 -07003368 texID,
3369 0));
Brian Salomon9bada542017-06-12 12:09:30 -04003370 texture->baseLevelWasBoundToFBO();
bsalomon@google.comeb851172013-04-15 13:51:00 +00003371 viewport->fLeft = 0;
3372 viewport->fBottom = 0;
3373 viewport->fWidth = surface->width();
3374 viewport->fHeight = surface->height();
3375 } else {
egdanield803f272015-03-18 13:01:52 -07003376 fStats.incRenderTargetBinds();
3377 GR_GL_CALL(this->glInterface(), BindFramebuffer(fboTarget, rt->renderFBOID()));
bsalomon@google.comeb851172013-04-15 13:51:00 +00003378 *viewport = rt->getViewport();
3379 }
egdaniel0f5f9672015-02-03 11:10:51 -08003380}
3381
Brian Salomon71d9d842016-11-03 13:42:00 -04003382void GrGLGpu::unbindTextureFBOForPixelOps(GrGLenum fboTarget, GrSurface* surface) {
3383 // bindSurfaceFBOForPixelOps temporarily binds textures that are not render targets to
bsalomon10528f12015-10-14 12:54:52 -07003384 if (!surface->asRenderTarget()) {
3385 SkASSERT(surface->asTexture());
3386 GrGLenum textureTarget = static_cast<GrGLTexture*>(surface->asTexture())->target();
3387 GR_GL_CALL(this->glInterface(), FramebufferTexture2D(fboTarget,
3388 GR_GL_COLOR_ATTACHMENT0,
3389 textureTarget,
3390 0,
3391 0));
3392 }
bsalomon@google.comeb851172013-04-15 13:51:00 +00003393}
3394
joshualitt1cbdcde2015-08-21 11:53:29 -07003395bool GrGLGpu::onCopySurface(GrSurface* dst,
3396 GrSurface* src,
3397 const SkIRect& srcRect,
3398 const SkIPoint& dstPoint) {
bsalomon7f9b2e42016-01-12 13:29:26 -08003399 // None of our copy methods can handle a swizzle. TODO: Make copySurfaceAsDraw handle the
3400 // swizzle.
Brian Salomon1edc5b92016-11-29 13:43:46 -05003401 if (this->caps()->shaderCaps()->configOutputSwizzle(src->config()) !=
3402 this->caps()->shaderCaps()->configOutputSwizzle(dst->config())) {
bsalomon7f9b2e42016-01-12 13:29:26 -08003403 return false;
3404 }
bsalomon083617b2016-02-12 12:10:14 -08003405 // Don't prefer copying as a draw if the dst doesn't already have a FBO object.
3406 bool preferCopy = SkToBool(dst->asRenderTarget());
3407 if (preferCopy && src->asTexture()) {
cdaltone2e71c22016-04-07 18:13:29 -07003408 if (this->copySurfaceAsDraw(dst, src, srcRect, dstPoint)) {
3409 return true;
3410 }
bsalomon5df6fee2015-05-18 06:26:15 -07003411 }
cblume61214052016-01-26 09:10:48 -08003412
bsalomon6df86402015-06-01 10:41:49 -07003413 if (can_copy_texsubimage(dst, src, this)) {
3414 this->copySurfaceAsCopyTexSubImage(dst, src, srcRect, dstPoint);
3415 return true;
3416 }
3417
Brian Salomone5e7eb12016-10-14 16:18:33 -04003418 if (can_blit_framebuffer_for_copy_surface(dst, src, srcRect, dstPoint, this)) {
bsalomon6df86402015-06-01 10:41:49 -07003419 return this->copySurfaceAsBlitFramebuffer(dst, src, srcRect, dstPoint);
3420 }
3421
bsalomon083617b2016-02-12 12:10:14 -08003422 if (!preferCopy && src->asTexture()) {
cdaltone2e71c22016-04-07 18:13:29 -07003423 if (this->copySurfaceAsDraw(dst, src, srcRect, dstPoint)) {
3424 return true;
3425 }
bsalomon083617b2016-02-12 12:10:14 -08003426 }
3427
bsalomon6df86402015-06-01 10:41:49 -07003428 return false;
3429}
3430
Brian Salomonbf7b6202016-11-11 16:08:03 -05003431bool GrGLGpu::createCopyProgram(GrTexture* srcTex) {
Ryan Macnak38a10ad2017-07-10 10:36:34 -07003432 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("skia"), "GrGLGpu::createCopyProgram()");
3433
Brian Salomonbf7b6202016-11-11 16:08:03 -05003434 int progIdx = TextureToCopyProgramIdx(srcTex);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003435 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
Brian Salomonbf7b6202016-11-11 16:08:03 -05003436 GrSLType samplerType = srcTex->texturePriv().samplerType();
cdaltone2e71c22016-04-07 18:13:29 -07003437
3438 if (!fCopyProgramArrayBuffer) {
3439 static const GrGLfloat vdata[] = {
3440 0, 0,
3441 0, 1,
3442 1, 0,
3443 1, 1
3444 };
3445 fCopyProgramArrayBuffer.reset(GrGLBuffer::Create(this, sizeof(vdata), kVertex_GrBufferType,
3446 kStatic_GrAccessPattern, vdata));
3447 }
3448 if (!fCopyProgramArrayBuffer) {
3449 return false;
3450 }
3451
3452 SkASSERT(!fCopyPrograms[progIdx].fProgram);
3453 GL_CALL_RET(fCopyPrograms[progIdx].fProgram, CreateProgram());
3454 if (!fCopyPrograms[progIdx].fProgram) {
3455 return false;
3456 }
3457
Brian Salomon1edc5b92016-11-29 13:43:46 -05003458 const char* version = shaderCaps->versionDeclString();
Brian Salomon99938a82016-11-21 13:41:08 -05003459 GrShaderVar aVertex("a_vertex", kVec2f_GrSLType, GrShaderVar::kIn_TypeModifier);
3460 GrShaderVar uTexCoordXform("u_texCoordXform", kVec4f_GrSLType,
3461 GrShaderVar::kUniform_TypeModifier);
3462 GrShaderVar uPosXform("u_posXform", kVec4f_GrSLType, GrShaderVar::kUniform_TypeModifier);
3463 GrShaderVar uTexture("u_texture", samplerType, GrShaderVar::kUniform_TypeModifier);
3464 GrShaderVar vTexCoord("v_texCoord", kVec2f_GrSLType, GrShaderVar::kOut_TypeModifier);
3465 GrShaderVar oFragColor("o_FragColor", kVec4f_GrSLType, GrShaderVar::kOut_TypeModifier);
cdaltone2e71c22016-04-07 18:13:29 -07003466
3467 SkString vshaderTxt(version);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003468 if (shaderCaps->noperspectiveInterpolationSupport()) {
3469 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
cdaltone2e71c22016-04-07 18:13:29 -07003470 vshaderTxt.appendf("#extension %s : require\n", extension);
3471 }
3472 vTexCoord.addModifier("noperspective");
3473 }
3474
Brian Salomon1edc5b92016-11-29 13:43:46 -05003475 aVertex.appendDecl(shaderCaps, &vshaderTxt);
cdaltone2e71c22016-04-07 18:13:29 -07003476 vshaderTxt.append(";");
Brian Salomon1edc5b92016-11-29 13:43:46 -05003477 uTexCoordXform.appendDecl(shaderCaps, &vshaderTxt);
cdaltone2e71c22016-04-07 18:13:29 -07003478 vshaderTxt.append(";");
Brian Salomon1edc5b92016-11-29 13:43:46 -05003479 uPosXform.appendDecl(shaderCaps, &vshaderTxt);
cdaltone2e71c22016-04-07 18:13:29 -07003480 vshaderTxt.append(";");
Brian Salomon1edc5b92016-11-29 13:43:46 -05003481 vTexCoord.appendDecl(shaderCaps, &vshaderTxt);
cdaltone2e71c22016-04-07 18:13:29 -07003482 vshaderTxt.append(";");
3483
3484 vshaderTxt.append(
3485 "// Copy Program VS\n"
3486 "void main() {"
3487 " v_texCoord = a_vertex.xy * u_texCoordXform.xy + u_texCoordXform.zw;"
3488 " gl_Position.xy = a_vertex * u_posXform.xy + u_posXform.zw;"
3489 " gl_Position.zw = vec2(0, 1);"
3490 "}"
3491 );
3492
3493 SkString fshaderTxt(version);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003494 if (shaderCaps->noperspectiveInterpolationSupport()) {
3495 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
cdaltone2e71c22016-04-07 18:13:29 -07003496 fshaderTxt.appendf("#extension %s : require\n", extension);
3497 }
3498 }
Brian Salomonbf7b6202016-11-11 16:08:03 -05003499 if (samplerType == kTextureExternalSampler_GrSLType) {
cdaltone2e71c22016-04-07 18:13:29 -07003500 fshaderTxt.appendf("#extension %s : require\n",
Brian Salomon1edc5b92016-11-29 13:43:46 -05003501 shaderCaps->externalTextureExtensionString());
cdaltone2e71c22016-04-07 18:13:29 -07003502 }
Brian Osman33aa2c72017-04-05 09:26:15 -04003503 GrGLSLAppendDefaultFloatPrecisionDeclaration(kMedium_GrSLPrecision, *shaderCaps,
cdaltone2e71c22016-04-07 18:13:29 -07003504 &fshaderTxt);
Brian Salomonf31ae492016-11-18 15:35:33 -05003505 vTexCoord.setTypeModifier(GrShaderVar::kIn_TypeModifier);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003506 vTexCoord.appendDecl(shaderCaps, &fshaderTxt);
cdaltone2e71c22016-04-07 18:13:29 -07003507 fshaderTxt.append(";");
Brian Salomon1edc5b92016-11-29 13:43:46 -05003508 uTexture.appendDecl(shaderCaps, &fshaderTxt);
cdaltone2e71c22016-04-07 18:13:29 -07003509 fshaderTxt.append(";");
cdaltone2e71c22016-04-07 18:13:29 -07003510 fshaderTxt.appendf(
3511 "// Copy Program FS\n"
3512 "void main() {"
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003513 " sk_FragColor = texture(u_texture, v_texCoord);"
3514 "}"
cdaltone2e71c22016-04-07 18:13:29 -07003515 );
3516
3517 const char* str;
3518 GrGLint length;
3519
3520 str = vshaderTxt.c_str();
3521 length = SkToInt(vshaderTxt.size());
Ethan Nicholas941e7e22016-12-12 15:33:30 -05003522 SkSL::Program::Settings settings;
3523 settings.fCaps = shaderCaps;
3524 SkSL::Program::Inputs inputs;
cdaltone2e71c22016-04-07 18:13:29 -07003525 GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fCopyPrograms[progIdx].fProgram,
3526 GR_GL_VERTEX_SHADER, &str, &length, 1,
Ethan Nicholas941e7e22016-12-12 15:33:30 -05003527 &fStats, settings, &inputs);
3528 SkASSERT(inputs.isEmpty());
cdaltone2e71c22016-04-07 18:13:29 -07003529
3530 str = fshaderTxt.c_str();
3531 length = SkToInt(fshaderTxt.size());
3532 GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fCopyPrograms[progIdx].fProgram,
3533 GR_GL_FRAGMENT_SHADER, &str, &length, 1,
Ethan Nicholas941e7e22016-12-12 15:33:30 -05003534 &fStats, settings, &inputs);
3535 SkASSERT(inputs.isEmpty());
cdaltone2e71c22016-04-07 18:13:29 -07003536
3537 GL_CALL(LinkProgram(fCopyPrograms[progIdx].fProgram));
3538
3539 GL_CALL_RET(fCopyPrograms[progIdx].fTextureUniform,
3540 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_texture"));
3541 GL_CALL_RET(fCopyPrograms[progIdx].fPosXformUniform,
3542 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_posXform"));
3543 GL_CALL_RET(fCopyPrograms[progIdx].fTexCoordXformUniform,
3544 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_texCoordXform"));
3545
3546 GL_CALL(BindAttribLocation(fCopyPrograms[progIdx].fProgram, 0, "a_vertex"));
3547
3548 GL_CALL(DeleteShader(vshader));
3549 GL_CALL(DeleteShader(fshader));
3550
3551 return true;
bsalomon6df86402015-06-01 10:41:49 -07003552}
3553
brianosman33f6b3f2016-06-02 05:49:21 -07003554bool GrGLGpu::createMipmapProgram(int progIdx) {
3555 const bool oddWidth = SkToBool(progIdx & 0x2);
3556 const bool oddHeight = SkToBool(progIdx & 0x1);
3557 const int numTaps = (oddWidth ? 2 : 1) * (oddHeight ? 2 : 1);
3558
Brian Salomon1edc5b92016-11-29 13:43:46 -05003559 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
brianosman33f6b3f2016-06-02 05:49:21 -07003560
3561 SkASSERT(!fMipmapPrograms[progIdx].fProgram);
3562 GL_CALL_RET(fMipmapPrograms[progIdx].fProgram, CreateProgram());
3563 if (!fMipmapPrograms[progIdx].fProgram) {
3564 return false;
3565 }
3566
Brian Salomon1edc5b92016-11-29 13:43:46 -05003567 const char* version = shaderCaps->versionDeclString();
Brian Salomon99938a82016-11-21 13:41:08 -05003568 GrShaderVar aVertex("a_vertex", kVec2f_GrSLType, GrShaderVar::kIn_TypeModifier);
3569 GrShaderVar uTexCoordXform("u_texCoordXform", kVec4f_GrSLType,
3570 GrShaderVar::kUniform_TypeModifier);
3571 GrShaderVar uTexture("u_texture", kTexture2DSampler_GrSLType,
3572 GrShaderVar::kUniform_TypeModifier);
brianosman33f6b3f2016-06-02 05:49:21 -07003573 // We need 1, 2, or 4 texture coordinates (depending on parity of each dimension):
Brian Salomon99938a82016-11-21 13:41:08 -05003574 GrShaderVar vTexCoords[] = {
3575 GrShaderVar("v_texCoord0", kVec2f_GrSLType, GrShaderVar::kOut_TypeModifier),
3576 GrShaderVar("v_texCoord1", kVec2f_GrSLType, GrShaderVar::kOut_TypeModifier),
3577 GrShaderVar("v_texCoord2", kVec2f_GrSLType, GrShaderVar::kOut_TypeModifier),
3578 GrShaderVar("v_texCoord3", kVec2f_GrSLType, GrShaderVar::kOut_TypeModifier),
brianosman33f6b3f2016-06-02 05:49:21 -07003579 };
Brian Salomon99938a82016-11-21 13:41:08 -05003580 GrShaderVar oFragColor("o_FragColor", kVec4f_GrSLType,GrShaderVar::kOut_TypeModifier);
brianosman33f6b3f2016-06-02 05:49:21 -07003581
3582 SkString vshaderTxt(version);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003583 if (shaderCaps->noperspectiveInterpolationSupport()) {
3584 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
brianosman33f6b3f2016-06-02 05:49:21 -07003585 vshaderTxt.appendf("#extension %s : require\n", extension);
3586 }
3587 vTexCoords[0].addModifier("noperspective");
3588 vTexCoords[1].addModifier("noperspective");
3589 vTexCoords[2].addModifier("noperspective");
3590 vTexCoords[3].addModifier("noperspective");
3591 }
3592
Brian Salomon1edc5b92016-11-29 13:43:46 -05003593 aVertex.appendDecl(shaderCaps, &vshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003594 vshaderTxt.append(";");
Brian Salomon1edc5b92016-11-29 13:43:46 -05003595 uTexCoordXform.appendDecl(shaderCaps, &vshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003596 vshaderTxt.append(";");
3597 for (int i = 0; i < numTaps; ++i) {
Brian Salomon1edc5b92016-11-29 13:43:46 -05003598 vTexCoords[i].appendDecl(shaderCaps, &vshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003599 vshaderTxt.append(";");
3600 }
3601
3602 vshaderTxt.append(
3603 "// Mipmap Program VS\n"
3604 "void main() {"
3605 " gl_Position.xy = a_vertex * vec2(2, 2) - vec2(1, 1);"
3606 " gl_Position.zw = vec2(0, 1);"
3607 );
3608
3609 // Insert texture coordinate computation:
3610 if (oddWidth && oddHeight) {
3611 vshaderTxt.append(
3612 " v_texCoord0 = a_vertex.xy * u_texCoordXform.yw;"
3613 " v_texCoord1 = a_vertex.xy * u_texCoordXform.yw + vec2(u_texCoordXform.x, 0);"
3614 " v_texCoord2 = a_vertex.xy * u_texCoordXform.yw + vec2(0, u_texCoordXform.z);"
3615 " v_texCoord3 = a_vertex.xy * u_texCoordXform.yw + u_texCoordXform.xz;"
3616 );
3617 } else if (oddWidth) {
3618 vshaderTxt.append(
3619 " v_texCoord0 = a_vertex.xy * vec2(u_texCoordXform.y, 1);"
3620 " v_texCoord1 = a_vertex.xy * vec2(u_texCoordXform.y, 1) + vec2(u_texCoordXform.x, 0);"
3621 );
3622 } else if (oddHeight) {
3623 vshaderTxt.append(
3624 " v_texCoord0 = a_vertex.xy * vec2(1, u_texCoordXform.w);"
3625 " v_texCoord1 = a_vertex.xy * vec2(1, u_texCoordXform.w) + vec2(0, u_texCoordXform.z);"
3626 );
3627 } else {
3628 vshaderTxt.append(
3629 " v_texCoord0 = a_vertex.xy;"
3630 );
3631 }
3632
3633 vshaderTxt.append("}");
3634
3635 SkString fshaderTxt(version);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003636 if (shaderCaps->noperspectiveInterpolationSupport()) {
3637 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
brianosman33f6b3f2016-06-02 05:49:21 -07003638 fshaderTxt.appendf("#extension %s : require\n", extension);
3639 }
3640 }
Brian Osman33aa2c72017-04-05 09:26:15 -04003641 GrGLSLAppendDefaultFloatPrecisionDeclaration(kMedium_GrSLPrecision, *shaderCaps,
brianosman33f6b3f2016-06-02 05:49:21 -07003642 &fshaderTxt);
3643 for (int i = 0; i < numTaps; ++i) {
Brian Salomonf31ae492016-11-18 15:35:33 -05003644 vTexCoords[i].setTypeModifier(GrShaderVar::kIn_TypeModifier);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003645 vTexCoords[i].appendDecl(shaderCaps, &fshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003646 fshaderTxt.append(";");
3647 }
Brian Salomon1edc5b92016-11-29 13:43:46 -05003648 uTexture.appendDecl(shaderCaps, &fshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003649 fshaderTxt.append(";");
brianosman33f6b3f2016-06-02 05:49:21 -07003650 fshaderTxt.append(
3651 "// Mipmap Program FS\n"
3652 "void main() {"
3653 );
3654
3655 if (oddWidth && oddHeight) {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003656 fshaderTxt.append(
3657 " sk_FragColor = (texture(u_texture, v_texCoord0) + "
3658 " texture(u_texture, v_texCoord1) + "
3659 " texture(u_texture, v_texCoord2) + "
3660 " texture(u_texture, v_texCoord3)) * 0.25;"
brianosman33f6b3f2016-06-02 05:49:21 -07003661 );
3662 } else if (oddWidth || oddHeight) {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003663 fshaderTxt.append(
3664 " sk_FragColor = (texture(u_texture, v_texCoord0) + "
3665 " texture(u_texture, v_texCoord1)) * 0.5;"
brianosman33f6b3f2016-06-02 05:49:21 -07003666 );
3667 } else {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003668 fshaderTxt.append(
3669 " sk_FragColor = texture(u_texture, v_texCoord0);"
brianosman33f6b3f2016-06-02 05:49:21 -07003670 );
3671 }
3672
3673 fshaderTxt.append("}");
3674
3675 const char* str;
3676 GrGLint length;
3677
3678 str = vshaderTxt.c_str();
3679 length = SkToInt(vshaderTxt.size());
Ethan Nicholas941e7e22016-12-12 15:33:30 -05003680 SkSL::Program::Settings settings;
3681 settings.fCaps = shaderCaps;
3682 SkSL::Program::Inputs inputs;
brianosman33f6b3f2016-06-02 05:49:21 -07003683 GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fMipmapPrograms[progIdx].fProgram,
3684 GR_GL_VERTEX_SHADER, &str, &length, 1,
Ethan Nicholas941e7e22016-12-12 15:33:30 -05003685 &fStats, settings, &inputs);
3686 SkASSERT(inputs.isEmpty());
brianosman33f6b3f2016-06-02 05:49:21 -07003687
3688 str = fshaderTxt.c_str();
3689 length = SkToInt(fshaderTxt.size());
3690 GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fMipmapPrograms[progIdx].fProgram,
3691 GR_GL_FRAGMENT_SHADER, &str, &length, 1,
Ethan Nicholas941e7e22016-12-12 15:33:30 -05003692 &fStats, settings, &inputs);
3693 SkASSERT(inputs.isEmpty());
brianosman33f6b3f2016-06-02 05:49:21 -07003694
3695 GL_CALL(LinkProgram(fMipmapPrograms[progIdx].fProgram));
3696
3697 GL_CALL_RET(fMipmapPrograms[progIdx].fTextureUniform,
3698 GetUniformLocation(fMipmapPrograms[progIdx].fProgram, "u_texture"));
3699 GL_CALL_RET(fMipmapPrograms[progIdx].fTexCoordXformUniform,
3700 GetUniformLocation(fMipmapPrograms[progIdx].fProgram, "u_texCoordXform"));
3701
3702 GL_CALL(BindAttribLocation(fMipmapPrograms[progIdx].fProgram, 0, "a_vertex"));
3703
3704 GL_CALL(DeleteShader(vshader));
3705 GL_CALL(DeleteShader(fshader));
3706
3707 return true;
3708}
3709
Mike Klein31550db2017-06-06 23:29:53 +00003710bool GrGLGpu::createStencilClipClearProgram() {
Ryan Macnak38a10ad2017-07-10 10:36:34 -07003711 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("skia"), "GrGLGpu::createStencilClipClearProgram()");
3712
Mike Klein31550db2017-06-06 23:29:53 +00003713 if (!fStencilClipClearArrayBuffer) {
3714 static const GrGLfloat vdata[] = {-1, -1, 1, -1, -1, 1, 1, 1};
3715 fStencilClipClearArrayBuffer.reset(GrGLBuffer::Create(
3716 this, sizeof(vdata), kVertex_GrBufferType, kStatic_GrAccessPattern, vdata));
3717 if (!fStencilClipClearArrayBuffer) {
cdaltone2e71c22016-04-07 18:13:29 -07003718 return false;
3719 }
3720 }
3721
Mike Klein31550db2017-06-06 23:29:53 +00003722 SkASSERT(!fStencilClipClearProgram);
3723 GL_CALL_RET(fStencilClipClearProgram, CreateProgram());
3724 if (!fStencilClipClearProgram) {
cdaltone2e71c22016-04-07 18:13:29 -07003725 return false;
3726 }
3727
Brian Salomon99938a82016-11-21 13:41:08 -05003728 GrShaderVar aVertex("a_vertex", kVec2f_GrSLType, GrShaderVar::kIn_TypeModifier);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003729 const char* version = this->caps()->shaderCaps()->versionDeclString();
bsalomon6dea83f2015-12-03 12:58:06 -08003730
bsalomon6dea83f2015-12-03 12:58:06 -08003731 SkString vshaderTxt(version);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003732 aVertex.appendDecl(this->caps()->shaderCaps(), &vshaderTxt);
bsalomon6dea83f2015-12-03 12:58:06 -08003733 vshaderTxt.append(";");
bsalomon6dea83f2015-12-03 12:58:06 -08003734 vshaderTxt.append(
Mike Klein31550db2017-06-06 23:29:53 +00003735 "// Stencil Clip Clear Program VS\n"
3736 "void main() {"
3737 " gl_Position = vec4(a_vertex.x, a_vertex.y, 0, 1);"
3738 "}");
bsalomon6dea83f2015-12-03 12:58:06 -08003739
3740 SkString fshaderTxt(version);
Brian Osman33aa2c72017-04-05 09:26:15 -04003741 GrGLSLAppendDefaultFloatPrecisionDeclaration(kMedium_GrSLPrecision,
Brian Salomon1edc5b92016-11-29 13:43:46 -05003742 *this->caps()->shaderCaps(),
bsalomon6dea83f2015-12-03 12:58:06 -08003743 &fshaderTxt);
bsalomon6dea83f2015-12-03 12:58:06 -08003744 fshaderTxt.appendf(
Mike Klein31550db2017-06-06 23:29:53 +00003745 "// Stencil Clip Clear Program FS\n"
3746 "void main() {"
3747 " sk_FragColor = vec4(0);"
3748 "}");
bsalomon6dea83f2015-12-03 12:58:06 -08003749
bsalomon6dea83f2015-12-03 12:58:06 -08003750 const char* str;
3751 GrGLint length;
3752
3753 str = vshaderTxt.c_str();
3754 length = SkToInt(vshaderTxt.size());
Ethan Nicholas941e7e22016-12-12 15:33:30 -05003755 SkSL::Program::Settings settings;
3756 settings.fCaps = this->caps()->shaderCaps();
3757 SkSL::Program::Inputs inputs;
Mike Klein31550db2017-06-06 23:29:53 +00003758 GrGLuint vshader =
3759 GrGLCompileAndAttachShader(*fGLContext, fStencilClipClearProgram, GR_GL_VERTEX_SHADER,
3760 &str, &length, 1, &fStats, settings, &inputs);
Ethan Nicholas941e7e22016-12-12 15:33:30 -05003761 SkASSERT(inputs.isEmpty());
bsalomon6dea83f2015-12-03 12:58:06 -08003762
3763 str = fshaderTxt.c_str();
3764 length = SkToInt(fshaderTxt.size());
Mike Klein31550db2017-06-06 23:29:53 +00003765 GrGLuint fshader =
3766 GrGLCompileAndAttachShader(*fGLContext, fStencilClipClearProgram, GR_GL_FRAGMENT_SHADER,
3767 &str, &length, 1, &fStats, settings, &inputs);
Ethan Nicholas941e7e22016-12-12 15:33:30 -05003768 SkASSERT(inputs.isEmpty());
bsalomon6dea83f2015-12-03 12:58:06 -08003769
Mike Klein31550db2017-06-06 23:29:53 +00003770 GL_CALL(LinkProgram(fStencilClipClearProgram));
bsalomon6dea83f2015-12-03 12:58:06 -08003771
Mike Klein31550db2017-06-06 23:29:53 +00003772 GL_CALL(BindAttribLocation(fStencilClipClearProgram, 0, "a_vertex"));
bsalomon6dea83f2015-12-03 12:58:06 -08003773
3774 GL_CALL(DeleteShader(vshader));
3775 GL_CALL(DeleteShader(fshader));
cdaltone2e71c22016-04-07 18:13:29 -07003776
3777 return true;
bsalomon6dea83f2015-12-03 12:58:06 -08003778}
3779
Mike Klein31550db2017-06-06 23:29:53 +00003780void GrGLGpu::clearStencilClipAsDraw(const GrFixedClip& clip, bool insideStencilMask,
3781 GrRenderTarget* rt) {
bsalomon7f9b2e42016-01-12 13:29:26 -08003782 // TODO: This should swizzle the output to match dst's config, though it is a debugging
3783 // visualization.
3784
bsalomon6dea83f2015-12-03 12:58:06 -08003785 this->handleDirtyContext();
Mike Klein31550db2017-06-06 23:29:53 +00003786 if (!fStencilClipClearProgram) {
3787 if (!this->createStencilClipClearProgram()) {
3788 SkDebugf("Failed to create stencil clip clear program.\n");
cdaltone2e71c22016-04-07 18:13:29 -07003789 return;
3790 }
bsalomon6dea83f2015-12-03 12:58:06 -08003791 }
3792
Mike Klein8b06ed72017-06-06 21:47:32 +00003793 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(rt->asRenderTarget());
Mike Klein31550db2017-06-06 23:29:53 +00003794 this->flushRenderTarget(glRT, nullptr);
Mike Klein8b06ed72017-06-06 21:47:32 +00003795
Mike Klein31550db2017-06-06 23:29:53 +00003796 GL_CALL(UseProgram(fStencilClipClearProgram));
3797 fHWProgramID = fStencilClipClearProgram;
bsalomon6dea83f2015-12-03 12:58:06 -08003798
cdaltone2e71c22016-04-07 18:13:29 -07003799 fHWVertexArrayState.setVertexArrayID(this, 0);
bsalomon6dea83f2015-12-03 12:58:06 -08003800
cdaltone2e71c22016-04-07 18:13:29 -07003801 GrGLAttribArrayState* attribs = fHWVertexArrayState.bindInternalVertexArray(this);
Chris Dalton8e45b4f2017-05-05 14:00:56 -04003802 attribs->enableVertexArrays(this, 1);
Mike Klein31550db2017-06-06 23:29:53 +00003803 attribs->set(this, 0, fStencilClipClearArrayBuffer.get(), kVec2f_GrVertexAttribType,
Hal Canary144caf52016-11-07 17:57:18 -05003804 2 * sizeof(GrGLfloat), 0);
bsalomon6dea83f2015-12-03 12:58:06 -08003805
bsalomon6dea83f2015-12-03 12:58:06 -08003806 GrXferProcessor::BlendInfo blendInfo;
3807 blendInfo.reset();
bsalomon7f9b2e42016-01-12 13:29:26 -08003808 this->flushBlend(blendInfo, GrSwizzle::RGBA());
Mike Klein31550db2017-06-06 23:29:53 +00003809 this->flushColorWrite(false);
cdaltonaf8bc7d2016-02-05 09:35:20 -08003810 this->flushHWAAState(glRT, false, false);
Mike Klein31550db2017-06-06 23:29:53 +00003811 this->flushScissor(clip.scissorState(), glRT->getViewport(), glRT->origin());
3812 this->flushWindowRectangles(clip.windowRectsState(), glRT);
3813 GrStencilAttachment* sb = rt->renderTargetPriv().getStencilAttachment();
3814 // This should only be called internally when we know we have a stencil buffer.
3815 SkASSERT(sb);
3816 GrStencilSettings settings = GrStencilSettings(
3817 *GrStencilSettings::SetClipBitSettings(insideStencilMask), false, sb->bits());
3818 this->flushStencil(settings);
3819 GL_CALL(DrawArrays(GR_GL_TRIANGLE_STRIP, 0, 4));
bsalomon6dea83f2015-12-03 12:58:06 -08003820}
3821
3822
cdaltone2e71c22016-04-07 18:13:29 -07003823bool GrGLGpu::copySurfaceAsDraw(GrSurface* dst,
bsalomon6df86402015-06-01 10:41:49 -07003824 GrSurface* src,
3825 const SkIRect& srcRect,
3826 const SkIPoint& dstPoint) {
cdaltone2e71c22016-04-07 18:13:29 -07003827 GrGLTexture* srcTex = static_cast<GrGLTexture*>(src->asTexture());
Brian Salomonbf7b6202016-11-11 16:08:03 -05003828 int progIdx = TextureToCopyProgramIdx(srcTex);
cdaltone2e71c22016-04-07 18:13:29 -07003829
3830 if (!fCopyPrograms[progIdx].fProgram) {
Brian Salomonbf7b6202016-11-11 16:08:03 -05003831 if (!this->createCopyProgram(srcTex)) {
cdaltone2e71c22016-04-07 18:13:29 -07003832 SkDebugf("Failed to create copy program.\n");
3833 return false;
3834 }
3835 }
3836
bsalomon6df86402015-06-01 10:41:49 -07003837 int w = srcRect.width();
3838 int h = srcRect.height();
3839
Brian Salomon514baff2016-11-17 15:17:07 -05003840 GrSamplerParams params(SkShader::kClamp_TileMode, GrSamplerParams::kNone_FilterMode);
brianosmana6359362016-03-21 06:55:37 -07003841 this->bindTexture(0, params, true, srcTex);
bsalomon6df86402015-06-01 10:41:49 -07003842
bsalomon083617b2016-02-12 12:10:14 -08003843 GrGLIRect dstVP;
Brian Salomon71d9d842016-11-03 13:42:00 -04003844 this->bindSurfaceFBOForPixelOps(dst, GR_GL_FRAMEBUFFER, &dstVP, kDst_TempFBOTarget);
bsalomon083617b2016-02-12 12:10:14 -08003845 this->flushViewport(dstVP);
Robert Phillips294870f2016-11-11 12:38:40 -05003846 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon083617b2016-02-12 12:10:14 -08003847
bsalomon6df86402015-06-01 10:41:49 -07003848 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY, w, h);
bsalomon6df86402015-06-01 10:41:49 -07003849
bsalomon7ea33f52015-11-22 14:51:00 -08003850 GL_CALL(UseProgram(fCopyPrograms[progIdx].fProgram));
3851 fHWProgramID = fCopyPrograms[progIdx].fProgram;
bsalomon6df86402015-06-01 10:41:49 -07003852
cdaltone2e71c22016-04-07 18:13:29 -07003853 fHWVertexArrayState.setVertexArrayID(this, 0);
bsalomon6df86402015-06-01 10:41:49 -07003854
cdaltone2e71c22016-04-07 18:13:29 -07003855 GrGLAttribArrayState* attribs = fHWVertexArrayState.bindInternalVertexArray(this);
Chris Dalton8e45b4f2017-05-05 14:00:56 -04003856 attribs->enableVertexArrays(this, 1);
Hal Canary144caf52016-11-07 17:57:18 -05003857 attribs->set(this, 0, fCopyProgramArrayBuffer.get(), kVec2f_GrVertexAttribType,
3858 2 * sizeof(GrGLfloat), 0);
bsalomon6df86402015-06-01 10:41:49 -07003859
3860 // dst rect edges in NDC (-1 to 1)
3861 int dw = dst->width();
3862 int dh = dst->height();
3863 GrGLfloat dx0 = 2.f * dstPoint.fX / dw - 1.f;
3864 GrGLfloat dx1 = 2.f * (dstPoint.fX + w) / dw - 1.f;
3865 GrGLfloat dy0 = 2.f * dstPoint.fY / dh - 1.f;
3866 GrGLfloat dy1 = 2.f * (dstPoint.fY + h) / dh - 1.f;
3867 if (kBottomLeft_GrSurfaceOrigin == dst->origin()) {
3868 dy0 = -dy0;
3869 dy1 = -dy1;
3870 }
3871
bsalomone5286e02016-01-14 09:24:09 -08003872 GrGLfloat sx0 = (GrGLfloat)srcRect.fLeft;
3873 GrGLfloat sx1 = (GrGLfloat)(srcRect.fLeft + w);
3874 GrGLfloat sy0 = (GrGLfloat)srcRect.fTop;
3875 GrGLfloat sy1 = (GrGLfloat)(srcRect.fTop + h);
Ethan Nicholas5338f992017-04-19 15:54:07 -04003876 int sw = src->width();
bsalomon6df86402015-06-01 10:41:49 -07003877 int sh = src->height();
bsalomon6df86402015-06-01 10:41:49 -07003878 if (kBottomLeft_GrSurfaceOrigin == src->origin()) {
bsalomone5286e02016-01-14 09:24:09 -08003879 sy0 = sh - sy0;
3880 sy1 = sh - sy1;
3881 }
Ethan Nicholas5338f992017-04-19 15:54:07 -04003882 // src rect edges in normalized texture space (0 to 1)
3883 sx0 /= sw;
3884 sx1 /= sw;
3885 sy0 /= sh;
3886 sy1 /= sh;
bsalomon6df86402015-06-01 10:41:49 -07003887
bsalomon7ea33f52015-11-22 14:51:00 -08003888 GL_CALL(Uniform4f(fCopyPrograms[progIdx].fPosXformUniform, dx1 - dx0, dy1 - dy0, dx0, dy0));
3889 GL_CALL(Uniform4f(fCopyPrograms[progIdx].fTexCoordXformUniform,
3890 sx1 - sx0, sy1 - sy0, sx0, sy0));
3891 GL_CALL(Uniform1i(fCopyPrograms[progIdx].fTextureUniform, 0));
bsalomon6df86402015-06-01 10:41:49 -07003892
3893 GrXferProcessor::BlendInfo blendInfo;
3894 blendInfo.reset();
bsalomon7f9b2e42016-01-12 13:29:26 -08003895 this->flushBlend(blendInfo, GrSwizzle::RGBA());
bsalomon6df86402015-06-01 10:41:49 -07003896 this->flushColorWrite(true);
bsalomon083617b2016-02-12 12:10:14 -08003897 this->flushHWAAState(nullptr, false, false);
bsalomon6df86402015-06-01 10:41:49 -07003898 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07003899 this->disableWindowRectangles();
csmartdaltonc7d85332016-10-26 10:13:46 -07003900 this->disableStencil();
Brian Osmanf02fa6f2017-07-11 11:17:47 -04003901 if (this->glCaps().srgbWriteControl()) {
3902 this->flushFramebufferSRGB(true);
3903 }
bsalomon6df86402015-06-01 10:41:49 -07003904
3905 GL_CALL(DrawArrays(GR_GL_TRIANGLE_STRIP, 0, 4));
Brian Salomon71d9d842016-11-03 13:42:00 -04003906 this->unbindTextureFBOForPixelOps(GR_GL_FRAMEBUFFER, dst);
bsalomon083617b2016-02-12 12:10:14 -08003907 this->didWriteToSurface(dst, &dstRect);
3908
cdaltone2e71c22016-04-07 18:13:29 -07003909 return true;
bsalomon6df86402015-06-01 10:41:49 -07003910}
3911
3912void GrGLGpu::copySurfaceAsCopyTexSubImage(GrSurface* dst,
3913 GrSurface* src,
3914 const SkIRect& srcRect,
3915 const SkIPoint& dstPoint) {
3916 SkASSERT(can_copy_texsubimage(dst, src, this));
bsalomon6df86402015-06-01 10:41:49 -07003917 GrGLIRect srcVP;
Brian Salomon71d9d842016-11-03 13:42:00 -04003918 this->bindSurfaceFBOForPixelOps(src, GR_GL_FRAMEBUFFER, &srcVP, kSrc_TempFBOTarget);
bsalomon083617b2016-02-12 12:10:14 -08003919 GrGLTexture* dstTex = static_cast<GrGLTexture *>(dst->asTexture());
bsalomon6df86402015-06-01 10:41:49 -07003920 SkASSERT(dstTex);
3921 // We modified the bound FBO
Robert Phillips294870f2016-11-11 12:38:40 -05003922 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon6df86402015-06-01 10:41:49 -07003923 GrGLIRect srcGLRect;
3924 srcGLRect.setRelativeTo(srcVP,
3925 srcRect.fLeft,
3926 srcRect.fTop,
3927 srcRect.width(),
3928 srcRect.height(),
3929 src->origin());
3930
3931 this->setScratchTextureUnit();
bsalomon10528f12015-10-14 12:54:52 -07003932 GL_CALL(BindTexture(dstTex->target(), dstTex->textureID()));
bsalomon6df86402015-06-01 10:41:49 -07003933 GrGLint dstY;
3934 if (kBottomLeft_GrSurfaceOrigin == dst->origin()) {
3935 dstY = dst->height() - (dstPoint.fY + srcGLRect.fHeight);
3936 } else {
3937 dstY = dstPoint.fY;
3938 }
bsalomon10528f12015-10-14 12:54:52 -07003939 GL_CALL(CopyTexSubImage2D(dstTex->target(), 0,
bsalomon083617b2016-02-12 12:10:14 -08003940 dstPoint.fX, dstY,
3941 srcGLRect.fLeft, srcGLRect.fBottom,
3942 srcGLRect.fWidth, srcGLRect.fHeight));
Brian Salomon71d9d842016-11-03 13:42:00 -04003943 this->unbindTextureFBOForPixelOps(GR_GL_FRAMEBUFFER, src);
bsalomon083617b2016-02-12 12:10:14 -08003944 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
3945 srcRect.width(), srcRect.height());
3946 this->didWriteToSurface(dst, &dstRect);
bsalomon6df86402015-06-01 10:41:49 -07003947}
3948
3949bool GrGLGpu::copySurfaceAsBlitFramebuffer(GrSurface* dst,
3950 GrSurface* src,
3951 const SkIRect& srcRect,
3952 const SkIPoint& dstPoint) {
Brian Salomone5e7eb12016-10-14 16:18:33 -04003953 SkASSERT(can_blit_framebuffer_for_copy_surface(dst, src, srcRect, dstPoint, this));
bsalomon6df86402015-06-01 10:41:49 -07003954 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
3955 srcRect.width(), srcRect.height());
3956 if (dst == src) {
3957 if (SkIRect::IntersectsNoEmptyCheck(dstRect, srcRect)) {
3958 return false;
mtklein404b3b22015-05-18 09:29:10 -07003959 }
bsalomon5df6fee2015-05-18 06:26:15 -07003960 }
bsalomon6df86402015-06-01 10:41:49 -07003961
bsalomon6df86402015-06-01 10:41:49 -07003962 GrGLIRect dstVP;
3963 GrGLIRect srcVP;
Brian Salomon71d9d842016-11-03 13:42:00 -04003964 this->bindSurfaceFBOForPixelOps(dst, GR_GL_DRAW_FRAMEBUFFER, &dstVP, kDst_TempFBOTarget);
3965 this->bindSurfaceFBOForPixelOps(src, GR_GL_READ_FRAMEBUFFER, &srcVP, kSrc_TempFBOTarget);
bsalomon6df86402015-06-01 10:41:49 -07003966 // We modified the bound FBO
Robert Phillips294870f2016-11-11 12:38:40 -05003967 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon6df86402015-06-01 10:41:49 -07003968 GrGLIRect srcGLRect;
3969 GrGLIRect dstGLRect;
3970 srcGLRect.setRelativeTo(srcVP,
3971 srcRect.fLeft,
3972 srcRect.fTop,
3973 srcRect.width(),
3974 srcRect.height(),
3975 src->origin());
3976 dstGLRect.setRelativeTo(dstVP,
3977 dstRect.fLeft,
3978 dstRect.fTop,
3979 dstRect.width(),
3980 dstRect.height(),
3981 dst->origin());
3982
3983 // BlitFrameBuffer respects the scissor, so disable it.
3984 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07003985 this->disableWindowRectangles();
bsalomon6df86402015-06-01 10:41:49 -07003986
3987 GrGLint srcY0;
3988 GrGLint srcY1;
3989 // Does the blit need to y-mirror or not?
3990 if (src->origin() == dst->origin()) {
3991 srcY0 = srcGLRect.fBottom;
3992 srcY1 = srcGLRect.fBottom + srcGLRect.fHeight;
3993 } else {
3994 srcY0 = srcGLRect.fBottom + srcGLRect.fHeight;
3995 srcY1 = srcGLRect.fBottom;
3996 }
3997 GL_CALL(BlitFramebuffer(srcGLRect.fLeft,
3998 srcY0,
3999 srcGLRect.fLeft + srcGLRect.fWidth,
4000 srcY1,
4001 dstGLRect.fLeft,
4002 dstGLRect.fBottom,
4003 dstGLRect.fLeft + dstGLRect.fWidth,
4004 dstGLRect.fBottom + dstGLRect.fHeight,
4005 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
Brian Salomon71d9d842016-11-03 13:42:00 -04004006 this->unbindTextureFBOForPixelOps(GR_GL_DRAW_FRAMEBUFFER, dst);
4007 this->unbindTextureFBOForPixelOps(GR_GL_READ_FRAMEBUFFER, src);
bsalomon083617b2016-02-12 12:10:14 -08004008 this->didWriteToSurface(dst, &dstRect);
bsalomon6df86402015-06-01 10:41:49 -07004009 return true;
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00004010}
4011
brianosman33f6b3f2016-06-02 05:49:21 -07004012// Manual implementation of mipmap generation, to work around driver bugs w/sRGB.
4013// Uses draw calls to do a series of downsample operations to successive mips.
4014// If this returns false, then the calling code falls back to using glGenerateMipmap.
4015bool GrGLGpu::generateMipmap(GrGLTexture* texture, bool gammaCorrect) {
Brian Salomonbf7b6202016-11-11 16:08:03 -05004016 SkASSERT(!GrPixelConfigIsSint(texture->config()));
brianosman09563ce2016-06-02 08:59:34 -07004017 // Our iterative downsample requires the ability to limit which level we're sampling:
4018 if (!this->glCaps().doManualMipmapping()) {
brianosman33f6b3f2016-06-02 05:49:21 -07004019 return false;
4020 }
4021
4022 // Mipmaps are only supported on 2D textures:
4023 if (GR_GL_TEXTURE_2D != texture->target()) {
4024 return false;
4025 }
4026
4027 // We need to be able to render to the texture for this to work:
Brian Salomon71d9d842016-11-03 13:42:00 -04004028 if (!this->glCaps().canConfigBeFBOColorAttachment(texture->config())) {
brianosman33f6b3f2016-06-02 05:49:21 -07004029 return false;
4030 }
4031
brianosman33f6b3f2016-06-02 05:49:21 -07004032 // If we're mipping an sRGB texture, we need to ensure FB sRGB is correct:
4033 if (GrPixelConfigIsSRGB(texture->config())) {
4034 // If we have write-control, just set the state that we want:
4035 if (this->glCaps().srgbWriteControl()) {
4036 this->flushFramebufferSRGB(gammaCorrect);
4037 } else if (!gammaCorrect) {
4038 // If we don't have write-control we can't do non-gamma-correct mipmapping:
4039 return false;
4040 }
4041 }
4042
4043 int width = texture->width();
4044 int height = texture->height();
4045 int levelCount = SkMipMap::ComputeLevelCount(width, height) + 1;
4046
4047 // Define all mips, if we haven't previously done so:
4048 if (0 == texture->texturePriv().maxMipMapLevel()) {
4049 GrGLenum internalFormat;
4050 GrGLenum externalFormat;
4051 GrGLenum externalType;
4052 if (!this->glCaps().getTexImageFormats(texture->config(), texture->config(),
4053 &internalFormat, &externalFormat, &externalType)) {
4054 return false;
4055 }
4056
4057 for (GrGLint level = 1; level < levelCount; ++level) {
4058 // Define the next mip:
4059 width = SkTMax(1, width / 2);
4060 height = SkTMax(1, height / 2);
4061 GL_ALLOC_CALL(this->glInterface(), TexImage2D(GR_GL_TEXTURE_2D, level, internalFormat,
4062 width, height, 0,
4063 externalFormat, externalType, nullptr));
4064 }
4065 }
4066
4067 // Create (if necessary), then bind temporary FBO:
4068 if (0 == fTempDstFBOID) {
4069 GL_CALL(GenFramebuffers(1, &fTempDstFBOID));
4070 }
4071 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, fTempDstFBOID));
Robert Phillips294870f2016-11-11 12:38:40 -05004072 fHWBoundRenderTargetUniqueID.makeInvalid();
brianosman33f6b3f2016-06-02 05:49:21 -07004073
4074 // Bind the texture, to get things configured for filtering.
4075 // We'll be changing our base level further below:
4076 this->setTextureUnit(0);
Brian Salomon514baff2016-11-17 15:17:07 -05004077 GrSamplerParams params(SkShader::kClamp_TileMode, GrSamplerParams::kBilerp_FilterMode);
brianosman33f6b3f2016-06-02 05:49:21 -07004078 this->bindTexture(0, params, gammaCorrect, texture);
4079
4080 // Vertex data:
4081 if (!fMipmapProgramArrayBuffer) {
4082 static const GrGLfloat vdata[] = {
4083 0, 0,
4084 0, 1,
4085 1, 0,
4086 1, 1
4087 };
4088 fMipmapProgramArrayBuffer.reset(GrGLBuffer::Create(this, sizeof(vdata),
4089 kVertex_GrBufferType,
4090 kStatic_GrAccessPattern, vdata));
4091 }
4092 if (!fMipmapProgramArrayBuffer) {
4093 return false;
4094 }
4095
4096 fHWVertexArrayState.setVertexArrayID(this, 0);
4097
4098 GrGLAttribArrayState* attribs = fHWVertexArrayState.bindInternalVertexArray(this);
Chris Dalton8e45b4f2017-05-05 14:00:56 -04004099 attribs->enableVertexArrays(this, 1);
Hal Canary144caf52016-11-07 17:57:18 -05004100 attribs->set(this, 0, fMipmapProgramArrayBuffer.get(), kVec2f_GrVertexAttribType,
brianosman33f6b3f2016-06-02 05:49:21 -07004101 2 * sizeof(GrGLfloat), 0);
brianosman33f6b3f2016-06-02 05:49:21 -07004102
4103 // Set "simple" state once:
4104 GrXferProcessor::BlendInfo blendInfo;
4105 blendInfo.reset();
4106 this->flushBlend(blendInfo, GrSwizzle::RGBA());
4107 this->flushColorWrite(true);
brianosman33f6b3f2016-06-02 05:49:21 -07004108 this->flushHWAAState(nullptr, false, false);
4109 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07004110 this->disableWindowRectangles();
csmartdaltonc7d85332016-10-26 10:13:46 -07004111 this->disableStencil();
brianosman33f6b3f2016-06-02 05:49:21 -07004112
4113 // Do all the blits:
4114 width = texture->width();
4115 height = texture->height();
4116 GrGLIRect viewport;
4117 viewport.fLeft = 0;
4118 viewport.fBottom = 0;
4119 for (GrGLint level = 1; level < levelCount; ++level) {
4120 // Get and bind the program for this particular downsample (filter shape can vary):
4121 int progIdx = TextureSizeToMipmapProgramIdx(width, height);
4122 if (!fMipmapPrograms[progIdx].fProgram) {
4123 if (!this->createMipmapProgram(progIdx)) {
4124 SkDebugf("Failed to create mipmap program.\n");
4125 return false;
4126 }
4127 }
4128 GL_CALL(UseProgram(fMipmapPrograms[progIdx].fProgram));
4129 fHWProgramID = fMipmapPrograms[progIdx].fProgram;
4130
4131 // Texcoord uniform is expected to contain (1/w, (w-1)/w, 1/h, (h-1)/h)
4132 const float invWidth = 1.0f / width;
4133 const float invHeight = 1.0f / height;
4134 GL_CALL(Uniform4f(fMipmapPrograms[progIdx].fTexCoordXformUniform,
4135 invWidth, (width - 1) * invWidth, invHeight, (height - 1) * invHeight));
4136 GL_CALL(Uniform1i(fMipmapPrograms[progIdx].fTextureUniform, 0));
4137
4138 // Only sample from previous mip
4139 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_BASE_LEVEL, level - 1));
4140
4141 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
4142 GR_GL_TEXTURE_2D, texture->textureID(), level));
4143
4144 width = SkTMax(1, width / 2);
4145 height = SkTMax(1, height / 2);
4146 viewport.fWidth = width;
4147 viewport.fHeight = height;
4148 this->flushViewport(viewport);
4149
4150 GL_CALL(DrawArrays(GR_GL_TRIANGLE_STRIP, 0, 4));
4151 }
4152
4153 // Unbind:
4154 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
4155 GR_GL_TEXTURE_2D, 0, 0));
4156
4157 return true;
4158}
4159
csmartdaltonc25c5d72016-11-01 07:03:59 -07004160void GrGLGpu::onQueryMultisampleSpecs(GrRenderTarget* rt, const GrStencilSettings& stencil,
4161 int* effectiveSampleCnt, SamplePattern* samplePattern) {
Brian Salomon7c8460e2017-05-12 11:36:10 -04004162 SkASSERT(GrFSAAType::kMixedSamples != rt->fsaaType() ||
4163 rt->renderTargetPriv().getStencilAttachment() || stencil.isDisabled());
cdalton28f45b92016-03-07 13:58:26 -08004164
4165 this->flushStencil(stencil);
4166 this->flushHWAAState(rt, true, !stencil.isDisabled());
4167 this->flushRenderTarget(static_cast<GrGLRenderTarget*>(rt), &SkIRect::EmptyIRect());
4168
4169 if (0 != this->caps()->maxRasterSamples()) {
4170 GR_GL_GetIntegerv(this->glInterface(), GR_GL_EFFECTIVE_RASTER_SAMPLES, effectiveSampleCnt);
4171 } else {
4172 GR_GL_GetIntegerv(this->glInterface(), GR_GL_SAMPLES, effectiveSampleCnt);
4173 }
4174
Brian Salomon154ce912017-05-17 10:40:02 -04004175 SkASSERT(*effectiveSampleCnt >= rt->numStencilSamples());
cdalton28f45b92016-03-07 13:58:26 -08004176
4177 if (this->caps()->sampleLocationsSupport()) {
csmartdalton0d28e572016-07-06 09:59:43 -07004178 samplePattern->reset(*effectiveSampleCnt);
cdalton28f45b92016-03-07 13:58:26 -08004179 for (int i = 0; i < *effectiveSampleCnt; ++i) {
4180 GrGLfloat pos[2];
4181 GL_CALL(GetMultisamplefv(GR_GL_SAMPLE_POSITION, i, pos));
4182 if (kTopLeft_GrSurfaceOrigin == rt->origin()) {
csmartdalton0d28e572016-07-06 09:59:43 -07004183 (*samplePattern)[i].set(pos[0], pos[1]);
cdalton28f45b92016-03-07 13:58:26 -08004184 } else {
csmartdalton0d28e572016-07-06 09:59:43 -07004185 (*samplePattern)[i].set(pos[0], 1 - pos[1]);
cdalton28f45b92016-03-07 13:58:26 -08004186 }
4187 }
4188 }
4189}
4190
cdalton231c5fd2015-05-13 12:35:36 -07004191void GrGLGpu::xferBarrier(GrRenderTarget* rt, GrXferBarrierType type) {
bsalomoncb02b382015-08-12 11:14:50 -07004192 SkASSERT(type);
cdalton9954bc32015-04-29 14:17:00 -07004193 switch (type) {
cdalton231c5fd2015-05-13 12:35:36 -07004194 case kTexture_GrXferBarrierType: {
4195 GrGLRenderTarget* glrt = static_cast<GrGLRenderTarget*>(rt);
4196 if (glrt->textureFBOID() != glrt->renderFBOID()) {
4197 // The render target uses separate storage so no need for glTextureBarrier.
4198 // FIXME: The render target will resolve automatically when its texture is bound,
4199 // but we could resolve only the bounds that will be read if we do it here instead.
4200 return;
4201 }
cdalton9954bc32015-04-29 14:17:00 -07004202 SkASSERT(this->caps()->textureBarrierSupport());
4203 GL_CALL(TextureBarrier());
4204 return;
cdalton231c5fd2015-05-13 12:35:36 -07004205 }
cdalton8917d622015-05-06 13:40:21 -07004206 case kBlend_GrXferBarrierType:
bsalomon4b91f762015-05-19 09:29:46 -07004207 SkASSERT(GrCaps::kAdvanced_BlendEquationSupport ==
cdalton8917d622015-05-06 13:40:21 -07004208 this->caps()->blendEquationSupport());
4209 GL_CALL(BlendBarrier());
4210 return;
bsalomoncb02b382015-08-12 11:14:50 -07004211 default: break; // placate compiler warnings that kNone not handled
cdalton9954bc32015-04-29 14:17:00 -07004212 }
4213}
4214
jvanverth88957922015-07-14 11:02:52 -07004215GrBackendObject GrGLGpu::createTestingOnlyBackendTexture(void* pixels, int w, int h,
egdaniel0a3a7f72016-06-24 09:22:31 -07004216 GrPixelConfig config, bool /*isRT*/) {
bsalomon926cb022015-12-17 18:15:11 -08004217 if (!this->caps()->isConfigTexturable(config)) {
4218 return false;
4219 }
Ben Wagner18b61f92016-10-25 10:44:02 -04004220 std::unique_ptr<GrGLTextureInfo> info = skstd::make_unique<GrGLTextureInfo>();
bsalomon091f60c2015-11-10 11:54:56 -08004221 info->fTarget = GR_GL_TEXTURE_2D;
kkinnunen546eb5c2015-12-11 00:05:33 -08004222 info->fID = 0;
bsalomon091f60c2015-11-10 11:54:56 -08004223 GL_CALL(GenTextures(1, &info->fID));
jvanverth672bb7f2015-07-13 07:19:57 -07004224 GL_CALL(ActiveTexture(GR_GL_TEXTURE0));
4225 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, 1));
bsalomon091f60c2015-11-10 11:54:56 -08004226 GL_CALL(BindTexture(info->fTarget, info->fID));
Robert Phillips294870f2016-11-11 12:38:40 -05004227 fHWBoundTextureUniqueIDs[0].makeInvalid();
bsalomon091f60c2015-11-10 11:54:56 -08004228 GL_CALL(TexParameteri(info->fTarget, GR_GL_TEXTURE_MAG_FILTER, GR_GL_NEAREST));
4229 GL_CALL(TexParameteri(info->fTarget, GR_GL_TEXTURE_MIN_FILTER, GR_GL_NEAREST));
4230 GL_CALL(TexParameteri(info->fTarget, GR_GL_TEXTURE_WRAP_S, GR_GL_CLAMP_TO_EDGE));
4231 GL_CALL(TexParameteri(info->fTarget, GR_GL_TEXTURE_WRAP_T, GR_GL_CLAMP_TO_EDGE));
jvanverth672bb7f2015-07-13 07:19:57 -07004232
bsalomon76148af2016-01-12 11:13:47 -08004233 GrGLenum internalFormat;
4234 GrGLenum externalFormat;
4235 GrGLenum externalType;
4236
4237 if (!this->glCaps().getTexImageFormats(config, config, &internalFormat, &externalFormat,
4238 &externalType)) {
bsalomon76148af2016-01-12 11:13:47 -08004239 return reinterpret_cast<GrBackendObject>(nullptr);
bsalomon76148af2016-01-12 11:13:47 -08004240 }
jvanverth672bb7f2015-07-13 07:19:57 -07004241
bsalomon091f60c2015-11-10 11:54:56 -08004242 GL_CALL(TexImage2D(info->fTarget, 0, internalFormat, w, h, 0, externalFormat,
jvanverth672bb7f2015-07-13 07:19:57 -07004243 externalType, pixels));
4244
Ben Wagner18b61f92016-10-25 10:44:02 -04004245 return reinterpret_cast<GrBackendObject>(info.release());
jvanverth672bb7f2015-07-13 07:19:57 -07004246}
4247
jvanverth88957922015-07-14 11:02:52 -07004248bool GrGLGpu::isTestingOnlyBackendTexture(GrBackendObject id) const {
bsalomon091f60c2015-11-10 11:54:56 -08004249 GrGLuint texID = reinterpret_cast<const GrGLTextureInfo*>(id)->fID;
jvanverth672bb7f2015-07-13 07:19:57 -07004250
4251 GrGLboolean result;
4252 GL_CALL_RET(result, IsTexture(texID));
4253
4254 return (GR_GL_TRUE == result);
4255}
4256
bsalomone63ffef2016-02-05 07:17:34 -08004257void GrGLGpu::deleteTestingOnlyBackendTexture(GrBackendObject id, bool abandonTexture) {
Ben Wagner18b61f92016-10-25 10:44:02 -04004258 std::unique_ptr<const GrGLTextureInfo> info(reinterpret_cast<const GrGLTextureInfo*>(id));
bsalomon091f60c2015-11-10 11:54:56 -08004259 GrGLuint texID = info->fID;
bsalomon091f60c2015-11-10 11:54:56 -08004260
bsalomon67d76202015-11-11 12:40:42 -08004261 if (!abandonTexture) {
4262 GL_CALL(DeleteTextures(1, &texID));
4263 }
jvanverth672bb7f2015-07-13 07:19:57 -07004264}
4265
joshualitt8fd844f2015-12-02 13:36:47 -08004266void GrGLGpu::resetShaderCacheForTesting() const {
4267 fProgramCache->abandon();
4268}
4269
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00004270///////////////////////////////////////////////////////////////////////////////
bsalomon6df86402015-06-01 10:41:49 -07004271
cdaltone2e71c22016-04-07 18:13:29 -07004272GrGLAttribArrayState* GrGLGpu::HWVertexArrayState::bindInternalVertexArray(GrGLGpu* gpu,
csmartdalton485a1202016-07-13 10:16:32 -07004273 const GrBuffer* ibuf) {
robertphillips@google.com4f65a272013-03-26 19:40:46 +00004274 GrGLAttribArrayState* attribState;
4275
cdaltone2e71c22016-04-07 18:13:29 -07004276 if (gpu->glCaps().isCoreProfile()) {
4277 if (!fCoreProfileVertexArray) {
bsalomon@google.com6918d482013-03-07 19:09:11 +00004278 GrGLuint arrayID;
4279 GR_GL_CALL(gpu->glInterface(), GenVertexArrays(1, &arrayID));
4280 int attrCount = gpu->glCaps().maxVertexAttributes();
cdaltone2e71c22016-04-07 18:13:29 -07004281 fCoreProfileVertexArray = new GrGLVertexArray(arrayID, attrCount);
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00004282 }
cdaltone2e71c22016-04-07 18:13:29 -07004283 if (ibuf) {
4284 attribState = fCoreProfileVertexArray->bindWithIndexBuffer(gpu, ibuf);
bsalomon6df86402015-06-01 10:41:49 -07004285 } else {
cdaltone2e71c22016-04-07 18:13:29 -07004286 attribState = fCoreProfileVertexArray->bind(gpu);
bsalomon6df86402015-06-01 10:41:49 -07004287 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00004288 } else {
cdaltone2e71c22016-04-07 18:13:29 -07004289 if (ibuf) {
4290 // bindBuffer implicitly binds VAO 0 when binding an index buffer.
4291 gpu->bindBuffer(kIndex_GrBufferType, ibuf);
bsalomon@google.com6918d482013-03-07 19:09:11 +00004292 } else {
4293 this->setVertexArrayID(gpu, 0);
4294 }
4295 int attrCount = gpu->glCaps().maxVertexAttributes();
4296 if (fDefaultVertexArrayAttribState.count() != attrCount) {
4297 fDefaultVertexArrayAttribState.resize(attrCount);
4298 }
4299 attribState = &fDefaultVertexArrayAttribState;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00004300 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00004301 return attribState;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00004302}
bsalomone179a912016-01-20 06:18:10 -08004303
Robert Phillips3798c862017-03-27 11:08:16 -04004304bool GrGLGpu::onIsACopyNeededForTextureParams(GrTextureProxy* proxy,
Robert Phillips81444fb2017-03-21 09:14:35 -04004305 const GrSamplerParams& textureParams,
4306 GrTextureProducer::CopyParams* copyParams,
4307 SkScalar scaleAdjust[2]) const {
Robert Phillips3798c862017-03-27 11:08:16 -04004308 const GrTexture* texture = proxy->priv().peekTexture();
4309 if (!texture) {
4310 // The only way to get and EXTERNAL or RECTANGLE texture in Ganesh is to wrap them.
4311 // In that case the proxy should already be instantiated.
4312 return false;
4313 }
4314
bsalomone179a912016-01-20 06:18:10 -08004315 if (textureParams.isTiled() ||
Brian Salomon514baff2016-11-17 15:17:07 -05004316 GrSamplerParams::kMipMap_FilterMode == textureParams.filterMode()) {
Robert Phillips3798c862017-03-27 11:08:16 -04004317 const GrGLTexture* glTexture = static_cast<const GrGLTexture*>(texture);
bsalomone179a912016-01-20 06:18:10 -08004318 if (GR_GL_TEXTURE_EXTERNAL == glTexture->target() ||
4319 GR_GL_TEXTURE_RECTANGLE == glTexture->target()) {
Brian Salomon514baff2016-11-17 15:17:07 -05004320 copyParams->fFilter = GrSamplerParams::kNone_FilterMode;
bsalomone179a912016-01-20 06:18:10 -08004321 copyParams->fWidth = texture->width();
4322 copyParams->fHeight = texture->height();
4323 return true;
4324 }
4325 }
4326 return false;
4327}
jvanverth84741b32016-09-30 08:39:02 -07004328
Greg Daniel6be35232017-03-01 17:01:09 -05004329GrFence SK_WARN_UNUSED_RESULT GrGLGpu::insertFence() {
Greg Danielc64ee462017-06-15 16:59:49 -04004330 SkASSERT(this->caps()->fenceSyncSupport());
Greg Daniel6be35232017-03-01 17:01:09 -05004331 GrGLsync sync;
4332 GL_CALL_RET(sync, FenceSync(GR_GL_SYNC_GPU_COMMANDS_COMPLETE, 0));
4333 GR_STATIC_ASSERT(sizeof(GrFence) >= sizeof(GrGLsync));
4334 return (GrFence)sync;
jvanverth84741b32016-09-30 08:39:02 -07004335}
4336
Greg Daniel6be35232017-03-01 17:01:09 -05004337bool GrGLGpu::waitFence(GrFence fence, uint64_t timeout) {
jvanverth84741b32016-09-30 08:39:02 -07004338 GrGLenum result;
4339 GL_CALL_RET(result, ClientWaitSync((GrGLsync)fence, GR_GL_SYNC_FLUSH_COMMANDS_BIT, timeout));
4340 return (GR_GL_CONDITION_SATISFIED == result);
4341}
4342
4343void GrGLGpu::deleteFence(GrFence fence) const {
Greg Daniel6be35232017-03-01 17:01:09 -05004344 this->deleteSync((GrGLsync)fence);
4345}
4346
Greg Daniela5cb7812017-06-16 09:45:32 -04004347sk_sp<GrSemaphore> SK_WARN_UNUSED_RESULT GrGLGpu::makeSemaphore(bool isOwned) {
Greg Danielc64ee462017-06-15 16:59:49 -04004348 SkASSERT(this->caps()->fenceSyncSupport());
Greg Daniela5cb7812017-06-16 09:45:32 -04004349 return GrGLSemaphore::Make(this, isOwned);
Greg Daniel6be35232017-03-01 17:01:09 -05004350}
4351
Greg Daniela5cb7812017-06-16 09:45:32 -04004352sk_sp<GrSemaphore> GrGLGpu::wrapBackendSemaphore(const GrBackendSemaphore& semaphore,
4353 GrWrapOwnership ownership) {
Greg Danielc64ee462017-06-15 16:59:49 -04004354 SkASSERT(this->caps()->fenceSyncSupport());
Greg Daniela5cb7812017-06-16 09:45:32 -04004355 return GrGLSemaphore::MakeWrapped(this, semaphore.glSync(), ownership);
4356}
4357
4358
Brian Osmandc87c952017-04-28 13:57:38 -04004359void GrGLGpu::insertSemaphore(sk_sp<GrSemaphore> semaphore, bool flush) {
Greg Daniel6be35232017-03-01 17:01:09 -05004360 GrGLSemaphore* glSem = static_cast<GrGLSemaphore*>(semaphore.get());
4361
4362 GrGLsync sync;
4363 GL_CALL_RET(sync, FenceSync(GR_GL_SYNC_GPU_COMMANDS_COMPLETE, 0));
4364 glSem->setSync(sync);
Brian Osmandc87c952017-04-28 13:57:38 -04004365
4366 if (flush) {
4367 GL_CALL(Flush());
4368 }
Greg Daniel6be35232017-03-01 17:01:09 -05004369}
4370
4371void GrGLGpu::waitSemaphore(sk_sp<GrSemaphore> semaphore) {
4372 GrGLSemaphore* glSem = static_cast<GrGLSemaphore*>(semaphore.get());
4373
4374 GL_CALL(WaitSync(glSem->sync(), 0, GR_GL_TIMEOUT_IGNORED));
4375}
4376
4377void GrGLGpu::deleteSync(GrGLsync sync) const {
4378 GL_CALL(DeleteSync(sync));
jvanverth84741b32016-09-30 08:39:02 -07004379}
Brian Osman13dddce2017-05-09 13:19:50 -04004380
4381sk_sp<GrSemaphore> GrGLGpu::prepareTextureForCrossContextUsage(GrTexture* texture) {
4382 // Set up a semaphore to be signaled once the data is ready, and flush GL
Greg Daniela5cb7812017-06-16 09:45:32 -04004383 sk_sp<GrSemaphore> semaphore = this->makeSemaphore(true);
Brian Osman13dddce2017-05-09 13:19:50 -04004384 this->insertSemaphore(semaphore, true);
4385
4386 return semaphore;
4387}
Robert Phillips646e4292017-06-13 12:44:56 -04004388
4389int GrGLGpu::TextureToCopyProgramIdx(GrTexture* texture) {
4390 switch (texture->texturePriv().samplerType()) {
4391 case kTexture2DSampler_GrSLType:
4392 return 0;
4393 case kITexture2DSampler_GrSLType:
4394 return 1;
4395 case kTexture2DRectSampler_GrSLType:
4396 return 2;
4397 case kTextureExternalSampler_GrSLType:
4398 return 3;
4399 default:
4400 SkFAIL("Unexpected samper type");
4401 return 0;
4402 }
4403}