blob: a6fafbed5b9f550905fdaa2ad3fa867b70e6a715 [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 Daniel7ef28f32017-04-20 16:41:55 +000012#include "GrBackendSurface.h"
Hal Canary95e3c052017-01-11 12:44:43 -050013#include "GrFixedClip.h"
cdalton397536c2016-03-25 12:15:03 -070014#include "GrGLBuffer.h"
egdaniel066df7c2016-06-08 14:02:27 -070015#include "GrGLGpuCommandBuffer.h"
Greg Daniel6be35232017-03-01 17:01:09 -050016#include "GrGLSemaphore.h"
egdaniel8dc7c3a2015-04-16 11:22:42 -070017#include "GrGLStencilAttachment.h"
bsalomon37dd3312014-11-03 08:47:23 -080018#include "GrGLTextureRenderTarget.h"
bsalomon3582d3e2015-02-13 14:20:05 -080019#include "GrGpuResourcePriv.h"
egdaniel0e1853c2016-03-17 11:35:45 -070020#include "GrMesh.h"
Hal Canary95e3c052017-01-11 12:44:43 -050021#include "GrPipeline.h"
bsalomon6bc1b5f2015-02-23 09:06:38 -080022#include "GrRenderTargetPriv.h"
Brian Salomon94efbf52016-11-29 13:43:05 -050023#include "GrShaderCaps.h"
bsalomonafbf2d62014-09-30 12:18:44 -070024#include "GrSurfacePriv.h"
Robert Phillips3798c862017-03-27 11:08:16 -040025#include "GrSurfaceProxyPriv.h"
bsalomonafbf2d62014-09-30 12:18:44 -070026#include "GrTexturePriv.h"
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +000027#include "GrTypes.h"
Hal Canary95e3c052017-01-11 12:44:43 -050028#include "SkAutoMalloc.h"
Kevin Lubickc456b732017-01-11 17:21:57 +000029#include "SkMakeUnique.h"
30#include "SkMipMap.h"
31#include "SkPixmap.h"
Kevin Lubickc456b732017-01-11 17:21:57 +000032#include "SkSLCompiler.h"
Hal Canary95e3c052017-01-11 12:44:43 -050033#include "SkStrokeRec.h"
Kevin Lubickc456b732017-01-11 17:21:57 +000034#include "SkTemplates.h"
35#include "SkTypes.h"
Hal Canary95e3c052017-01-11 12:44:43 -050036#include "builders/GrGLShaderStringBuilder.h"
Hal Canary95e3c052017-01-11 12:44:43 -050037#include "instanced/GLInstancedRendering.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000038
bsalomon@google.com0b77d682011-08-19 13:28:54 +000039#define GL_CALL(X) GR_GL_CALL(this->glInterface(), X)
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +000040#define GL_CALL_RET(RET, X) GR_GL_CALL_RET(this->glInterface(), RET, X)
bsalomon@google.com0b77d682011-08-19 13:28:54 +000041
reed@google.comac10a2d2010-12-22 21:39:39 +000042#define SKIP_CACHE_CHECK true
43
bsalomon@google.com4f3c2532012-01-19 16:16:52 +000044#if GR_GL_CHECK_ALLOC_WITH_GET_ERROR
45 #define CLEAR_ERROR_BEFORE_ALLOC(iface) GrGLClearErr(iface)
46 #define GL_ALLOC_CALL(iface, call) GR_GL_CALL_NOERRCHECK(iface, call)
47 #define CHECK_ALLOC_ERROR(iface) GR_GL_GET_ERROR(iface)
rmistry@google.comfbfcd562012-08-23 18:09:54 +000048#else
bsalomon@google.com4f3c2532012-01-19 16:16:52 +000049 #define CLEAR_ERROR_BEFORE_ALLOC(iface)
50 #define GL_ALLOC_CALL(iface, call) GR_GL_CALL(iface, call)
51 #define CHECK_ALLOC_ERROR(iface) GR_GL_NO_ERROR
52#endif
53
Jim Van Verth32ac83e2016-11-28 15:23:57 -050054//#define USE_NSIGHT
55
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +000056///////////////////////////////////////////////////////////////////////////////
57
csmartdaltona7f29642016-07-07 08:49:11 -070058using gr_instanced::InstancedRendering;
59using gr_instanced::GLInstancedRendering;
cdaltonb85a0aa2014-07-21 15:32:44 -070060
Robert Phillipse3302df2017-04-24 07:31:02 -040061using gr_instanced::OpAllocator;
62using gr_instanced::GLOpAllocator;
63
cdalton8917d622015-05-06 13:40:21 -070064static const GrGLenum gXfermodeEquation2Blend[] = {
65 // Basic OpenGL blend equations.
66 GR_GL_FUNC_ADD,
67 GR_GL_FUNC_SUBTRACT,
68 GR_GL_FUNC_REVERSE_SUBTRACT,
69
70 // GL_KHR_blend_equation_advanced.
71 GR_GL_SCREEN,
72 GR_GL_OVERLAY,
73 GR_GL_DARKEN,
74 GR_GL_LIGHTEN,
75 GR_GL_COLORDODGE,
76 GR_GL_COLORBURN,
77 GR_GL_HARDLIGHT,
78 GR_GL_SOFTLIGHT,
79 GR_GL_DIFFERENCE,
80 GR_GL_EXCLUSION,
81 GR_GL_MULTIPLY,
82 GR_GL_HSL_HUE,
83 GR_GL_HSL_SATURATION,
84 GR_GL_HSL_COLOR,
85 GR_GL_HSL_LUMINOSITY
86};
87GR_STATIC_ASSERT(0 == kAdd_GrBlendEquation);
88GR_STATIC_ASSERT(1 == kSubtract_GrBlendEquation);
89GR_STATIC_ASSERT(2 == kReverseSubtract_GrBlendEquation);
90GR_STATIC_ASSERT(3 == kScreen_GrBlendEquation);
91GR_STATIC_ASSERT(4 == kOverlay_GrBlendEquation);
92GR_STATIC_ASSERT(5 == kDarken_GrBlendEquation);
93GR_STATIC_ASSERT(6 == kLighten_GrBlendEquation);
94GR_STATIC_ASSERT(7 == kColorDodge_GrBlendEquation);
95GR_STATIC_ASSERT(8 == kColorBurn_GrBlendEquation);
96GR_STATIC_ASSERT(9 == kHardLight_GrBlendEquation);
97GR_STATIC_ASSERT(10 == kSoftLight_GrBlendEquation);
98GR_STATIC_ASSERT(11 == kDifference_GrBlendEquation);
99GR_STATIC_ASSERT(12 == kExclusion_GrBlendEquation);
100GR_STATIC_ASSERT(13 == kMultiply_GrBlendEquation);
101GR_STATIC_ASSERT(14 == kHSLHue_GrBlendEquation);
102GR_STATIC_ASSERT(15 == kHSLSaturation_GrBlendEquation);
103GR_STATIC_ASSERT(16 == kHSLColor_GrBlendEquation);
104GR_STATIC_ASSERT(17 == kHSLLuminosity_GrBlendEquation);
bsalomonf7cc8772015-05-11 11:21:14 -0700105GR_STATIC_ASSERT(SK_ARRAY_COUNT(gXfermodeEquation2Blend) == kGrBlendEquationCnt);
cdalton8917d622015-05-06 13:40:21 -0700106
twiz@google.com0f31ca72011-03-18 17:38:11 +0000107static const GrGLenum gXfermodeCoeff2Blend[] = {
108 GR_GL_ZERO,
109 GR_GL_ONE,
110 GR_GL_SRC_COLOR,
111 GR_GL_ONE_MINUS_SRC_COLOR,
112 GR_GL_DST_COLOR,
113 GR_GL_ONE_MINUS_DST_COLOR,
114 GR_GL_SRC_ALPHA,
115 GR_GL_ONE_MINUS_SRC_ALPHA,
116 GR_GL_DST_ALPHA,
117 GR_GL_ONE_MINUS_DST_ALPHA,
118 GR_GL_CONSTANT_COLOR,
119 GR_GL_ONE_MINUS_CONSTANT_COLOR,
120 GR_GL_CONSTANT_ALPHA,
121 GR_GL_ONE_MINUS_CONSTANT_ALPHA,
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000122
123 // extended blend coeffs
124 GR_GL_SRC1_COLOR,
125 GR_GL_ONE_MINUS_SRC1_COLOR,
126 GR_GL_SRC1_ALPHA,
127 GR_GL_ONE_MINUS_SRC1_ALPHA,
reed@google.comac10a2d2010-12-22 21:39:39 +0000128};
129
bsalomon861e1032014-12-16 07:33:49 -0800130bool GrGLGpu::BlendCoeffReferencesConstant(GrBlendCoeff coeff) {
bsalomon@google.com080773c2011-03-15 19:09:25 +0000131 static const bool gCoeffReferencesBlendConst[] = {
132 false,
133 false,
134 false,
135 false,
136 false,
137 false,
138 false,
139 false,
140 false,
141 false,
142 true,
143 true,
144 true,
145 true,
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000146
147 // extended blend coeffs
148 false,
149 false,
150 false,
151 false,
bsalomon@google.com080773c2011-03-15 19:09:25 +0000152 };
153 return gCoeffReferencesBlendConst[coeff];
bsalomonf7cc8772015-05-11 11:21:14 -0700154 GR_STATIC_ASSERT(kGrBlendCoeffCnt == SK_ARRAY_COUNT(gCoeffReferencesBlendConst));
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000155
bsalomon@google.com47059542012-06-06 20:51:20 +0000156 GR_STATIC_ASSERT(0 == kZero_GrBlendCoeff);
157 GR_STATIC_ASSERT(1 == kOne_GrBlendCoeff);
158 GR_STATIC_ASSERT(2 == kSC_GrBlendCoeff);
159 GR_STATIC_ASSERT(3 == kISC_GrBlendCoeff);
160 GR_STATIC_ASSERT(4 == kDC_GrBlendCoeff);
161 GR_STATIC_ASSERT(5 == kIDC_GrBlendCoeff);
162 GR_STATIC_ASSERT(6 == kSA_GrBlendCoeff);
163 GR_STATIC_ASSERT(7 == kISA_GrBlendCoeff);
164 GR_STATIC_ASSERT(8 == kDA_GrBlendCoeff);
165 GR_STATIC_ASSERT(9 == kIDA_GrBlendCoeff);
166 GR_STATIC_ASSERT(10 == kConstC_GrBlendCoeff);
167 GR_STATIC_ASSERT(11 == kIConstC_GrBlendCoeff);
168 GR_STATIC_ASSERT(12 == kConstA_GrBlendCoeff);
169 GR_STATIC_ASSERT(13 == kIConstA_GrBlendCoeff);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000170
bsalomon@google.com47059542012-06-06 20:51:20 +0000171 GR_STATIC_ASSERT(14 == kS2C_GrBlendCoeff);
172 GR_STATIC_ASSERT(15 == kIS2C_GrBlendCoeff);
173 GR_STATIC_ASSERT(16 == kS2A_GrBlendCoeff);
174 GR_STATIC_ASSERT(17 == kIS2A_GrBlendCoeff);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000175
176 // assertion for gXfermodeCoeff2Blend have to be in GrGpu scope
bsalomonf7cc8772015-05-11 11:21:14 -0700177 GR_STATIC_ASSERT(kGrBlendCoeffCnt == SK_ARRAY_COUNT(gXfermodeCoeff2Blend));
bsalomon@google.com080773c2011-03-15 19:09:25 +0000178}
179
reed@google.comac10a2d2010-12-22 21:39:39 +0000180///////////////////////////////////////////////////////////////////////////////
181
egdanielff1d5472015-09-10 08:37:20 -0700182
bsalomon682c2692015-05-22 14:01:46 -0700183GrGpu* GrGLGpu::Create(GrBackendContext backendContext, const GrContextOptions& options,
184 GrContext* context) {
Hal Canary144caf52016-11-07 17:57:18 -0500185 sk_sp<const GrGLInterface> glInterface(
bsalomon424cc262015-05-22 10:37:30 -0700186 reinterpret_cast<const GrGLInterface*>(backendContext));
187 if (!glInterface) {
188 glInterface.reset(GrGLDefaultInterface());
189 } else {
190 glInterface->ref();
191 }
192 if (!glInterface) {
halcanary96fcdcc2015-08-27 07:41:13 -0700193 return nullptr;
bsalomon424cc262015-05-22 10:37:30 -0700194 }
Jim Van Verth76334772017-05-05 16:46:05 -0400195#ifdef USE_NSIGHT
196 const_cast<GrContextOptions&>(options).fSuppressPathRendering = true;
197#endif
Hal Canary144caf52016-11-07 17:57:18 -0500198 GrGLContext* glContext = GrGLContext::Create(glInterface.get(), options);
bsalomon424cc262015-05-22 10:37:30 -0700199 if (glContext) {
halcanary385fe4d2015-08-26 13:07:48 -0700200 return new GrGLGpu(glContext, context);
bsalomon424cc262015-05-22 10:37:30 -0700201 }
halcanary96fcdcc2015-08-27 07:41:13 -0700202 return nullptr;
bsalomon424cc262015-05-22 10:37:30 -0700203}
204
rileya@google.come38160c2012-07-03 18:03:04 +0000205static bool gPrintStartupSpew;
bsalomon@google.com42ab7ea2011-01-19 17:19:40 +0000206
bsalomon424cc262015-05-22 10:37:30 -0700207GrGLGpu::GrGLGpu(GrGLContext* ctx, GrContext* context)
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000208 : GrGpu(context)
cdaltone2e71c22016-04-07 18:13:29 -0700209 , fGLContext(ctx)
210 , fProgramCache(new ProgramCache(this))
211 , fHWProgramID(0)
212 , fTempSrcFBOID(0)
213 , fTempDstFBOID(0)
214 , fStencilClearFBOID(0)
cdalton74b8d322016-04-11 14:47:28 -0700215 , fHWMaxUsedBufferTextureUnit(-1)
cdaltone2e71c22016-04-07 18:13:29 -0700216 , fHWMinSampleShading(0.0) {
217 for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) {
218 fCopyPrograms[i].fProgram = 0;
219 }
brianosman33f6b3f2016-06-02 05:49:21 -0700220 for (size_t i = 0; i < SK_ARRAY_COUNT(fMipmapPrograms); ++i) {
221 fMipmapPrograms[i].fProgram = 0;
222 }
Mike Klein31550db2017-06-06 23:29:53 +0000223 fStencilClipClearProgram = 0;
cdaltone2e71c22016-04-07 18:13:29 -0700224
bsalomon424cc262015-05-22 10:37:30 -0700225 SkASSERT(ctx);
226 fCaps.reset(SkRef(ctx->caps()));
bsalomon@google.combcce8922013-03-25 15:38:39 +0000227
Brian Salomon1edc5b92016-11-29 13:43:46 -0500228 fHWBoundTextureUniqueIDs.reset(this->caps()->shaderCaps()->maxCombinedSamplers());
229 fHWBoundImageStorages.reset(this->caps()->shaderCaps()->maxCombinedImageStorages());
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +0000230
cdaltone2e71c22016-04-07 18:13:29 -0700231 fHWBufferState[kVertex_GrBufferType].fGLTarget = GR_GL_ARRAY_BUFFER;
232 fHWBufferState[kIndex_GrBufferType].fGLTarget = GR_GL_ELEMENT_ARRAY_BUFFER;
233 fHWBufferState[kTexel_GrBufferType].fGLTarget = GR_GL_TEXTURE_BUFFER;
234 fHWBufferState[kDrawIndirect_GrBufferType].fGLTarget = GR_GL_DRAW_INDIRECT_BUFFER;
235 if (GrGLCaps::kChromium_TransferBufferType == this->glCaps().transferBufferType()) {
236 fHWBufferState[kXferCpuToGpu_GrBufferType].fGLTarget =
237 GR_GL_PIXEL_UNPACK_TRANSFER_BUFFER_CHROMIUM;
238 fHWBufferState[kXferGpuToCpu_GrBufferType].fGLTarget =
239 GR_GL_PIXEL_PACK_TRANSFER_BUFFER_CHROMIUM;
240 } else {
241 fHWBufferState[kXferCpuToGpu_GrBufferType].fGLTarget = GR_GL_PIXEL_UNPACK_BUFFER;
242 fHWBufferState[kXferGpuToCpu_GrBufferType].fGLTarget = GR_GL_PIXEL_PACK_BUFFER;
243 }
244 GR_STATIC_ASSERT(6 == SK_ARRAY_COUNT(fHWBufferState));
245
cdalton74b8d322016-04-11 14:47:28 -0700246 if (this->caps()->shaderCaps()->texelBufferSupport()) {
Brian Salomon1edc5b92016-11-29 13:43:46 -0500247 fHWBufferTextures.reset(this->caps()->shaderCaps()->maxCombinedSamplers());
cdalton74b8d322016-04-11 14:47:28 -0700248 }
249
cdaltone2e71c22016-04-07 18:13:29 -0700250 if (this->glCaps().shaderCaps()->pathRenderingSupport()) {
251 fPathRendering.reset(new GrGLPathRendering(this));
252 }
253
bsalomon424cc262015-05-22 10:37:30 -0700254 GrGLClearErr(this->glInterface());
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000255 if (gPrintStartupSpew) {
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000256 const GrGLubyte* vendor;
257 const GrGLubyte* renderer;
258 const GrGLubyte* version;
259 GL_CALL_RET(vendor, GetString(GR_GL_VENDOR));
260 GL_CALL_RET(renderer, GetString(GR_GL_RENDERER));
261 GL_CALL_RET(version, GetString(GR_GL_VERSION));
bsalomon861e1032014-12-16 07:33:49 -0800262 SkDebugf("------------------------- create GrGLGpu %p --------------\n",
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000263 this);
tfarina38406c82014-10-31 07:11:12 -0700264 SkDebugf("------ VENDOR %s\n", vendor);
265 SkDebugf("------ RENDERER %s\n", renderer);
266 SkDebugf("------ VERSION %s\n", version);
267 SkDebugf("------ EXTENSIONS\n");
bsalomon424cc262015-05-22 10:37:30 -0700268 this->glContext().extensions().print();
tfarina38406c82014-10-31 07:11:12 -0700269 SkDebugf("\n");
kkinnunen297aaf92015-02-19 06:32:12 -0800270 SkDebugf("%s", this->glCaps().dump().c_str());
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000271 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000272}
273
bsalomon861e1032014-12-16 07:33:49 -0800274GrGLGpu::~GrGLGpu() {
cdaltone2e71c22016-04-07 18:13:29 -0700275 // Ensure any GrGpuResource objects get deleted first, since they may require a working GrGLGpu
276 // to release the resources held by the objects themselves.
kkinnunen702501d2016-01-13 23:36:45 -0800277 fPathRendering.reset();
cdaltone2e71c22016-04-07 18:13:29 -0700278 fCopyProgramArrayBuffer.reset();
brianosman33f6b3f2016-06-02 05:49:21 -0700279 fMipmapProgramArrayBuffer.reset();
Mike Klein31550db2017-06-06 23:29:53 +0000280 fStencilClipClearArrayBuffer.reset();
kkinnunen702501d2016-01-13 23:36:45 -0800281
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000282 if (0 != fHWProgramID) {
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000283 // detach the current program so there is no confusion on OpenGL's part
284 // that we want it to be deleted
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000285 GL_CALL(UseProgram(0));
286 }
287
egdanield803f272015-03-18 13:01:52 -0700288 if (0 != fTempSrcFBOID) {
289 GL_CALL(DeleteFramebuffers(1, &fTempSrcFBOID));
egdaniel0f5f9672015-02-03 11:10:51 -0800290 }
egdanield803f272015-03-18 13:01:52 -0700291 if (0 != fTempDstFBOID) {
292 GL_CALL(DeleteFramebuffers(1, &fTempDstFBOID));
egdaniel0f5f9672015-02-03 11:10:51 -0800293 }
egdanield803f272015-03-18 13:01:52 -0700294 if (0 != fStencilClearFBOID) {
295 GL_CALL(DeleteFramebuffers(1, &fStencilClearFBOID));
bsalomondd3143b2015-02-23 09:27:45 -0800296 }
egdaniel0f5f9672015-02-03 11:10:51 -0800297
bsalomon7ea33f52015-11-22 14:51:00 -0800298 for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) {
299 if (0 != fCopyPrograms[i].fProgram) {
300 GL_CALL(DeleteProgram(fCopyPrograms[i].fProgram));
301 }
bsalomon6df86402015-06-01 10:41:49 -0700302 }
bsalomon6dea83f2015-12-03 12:58:06 -0800303
brianosman33f6b3f2016-06-02 05:49:21 -0700304 for (size_t i = 0; i < SK_ARRAY_COUNT(fMipmapPrograms); ++i) {
305 if (0 != fMipmapPrograms[i].fProgram) {
306 GL_CALL(DeleteProgram(fMipmapPrograms[i].fProgram));
307 }
308 }
309
Mike Klein31550db2017-06-06 23:29:53 +0000310 if (0 != fStencilClipClearProgram) {
311 GL_CALL(DeleteProgram(fStencilClipClearProgram));
bsalomon6dea83f2015-12-03 12:58:06 -0800312 }
313
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000314 delete fProgramCache;
bsalomonc8dc1f72014-08-21 13:02:13 -0700315}
316
bsalomon6e2aad42016-04-01 11:54:31 -0700317void GrGLGpu::disconnect(DisconnectType type) {
318 INHERITED::disconnect(type);
319 if (DisconnectType::kCleanup == type) {
320 if (fHWProgramID) {
321 GL_CALL(UseProgram(0));
322 }
323 if (fTempSrcFBOID) {
324 GL_CALL(DeleteFramebuffers(1, &fTempSrcFBOID));
325 }
326 if (fTempDstFBOID) {
327 GL_CALL(DeleteFramebuffers(1, &fTempDstFBOID));
328 }
329 if (fStencilClearFBOID) {
330 GL_CALL(DeleteFramebuffers(1, &fStencilClearFBOID));
331 }
bsalomon6e2aad42016-04-01 11:54:31 -0700332 for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) {
333 if (fCopyPrograms[i].fProgram) {
334 GL_CALL(DeleteProgram(fCopyPrograms[i].fProgram));
335 }
336 }
brianosman33f6b3f2016-06-02 05:49:21 -0700337 for (size_t i = 0; i < SK_ARRAY_COUNT(fMipmapPrograms); ++i) {
338 if (fMipmapPrograms[i].fProgram) {
339 GL_CALL(DeleteProgram(fMipmapPrograms[i].fProgram));
340 }
341 }
Mike Klein31550db2017-06-06 23:29:53 +0000342 if (fStencilClipClearProgram) {
343 GL_CALL(DeleteProgram(fStencilClipClearProgram));
bsalomon6e2aad42016-04-01 11:54:31 -0700344 }
bsalomon6e2aad42016-04-01 11:54:31 -0700345 } else {
346 if (fProgramCache) {
347 fProgramCache->abandon();
348 }
349 }
350
351 delete fProgramCache;
352 fProgramCache = nullptr;
353
bsalomonc8dc1f72014-08-21 13:02:13 -0700354 fHWProgramID = 0;
egdanield803f272015-03-18 13:01:52 -0700355 fTempSrcFBOID = 0;
356 fTempDstFBOID = 0;
357 fStencilClearFBOID = 0;
cdaltone2e71c22016-04-07 18:13:29 -0700358 fCopyProgramArrayBuffer.reset();
bsalomon7ea33f52015-11-22 14:51:00 -0800359 for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) {
360 fCopyPrograms[i].fProgram = 0;
361 }
brianosman33f6b3f2016-06-02 05:49:21 -0700362 fMipmapProgramArrayBuffer.reset();
363 for (size_t i = 0; i < SK_ARRAY_COUNT(fMipmapPrograms); ++i) {
364 fMipmapPrograms[i].fProgram = 0;
365 }
Mike Klein31550db2017-06-06 23:29:53 +0000366 fStencilClipClearProgram = 0;
367 fStencilClipClearArrayBuffer.reset();
jvanverthe9c0fc62015-04-29 11:18:05 -0700368 if (this->glCaps().shaderCaps()->pathRenderingSupport()) {
bsalomon6e2aad42016-04-01 11:54:31 -0700369 this->glPathRendering()->disconnect(type);
bsalomonc8dc1f72014-08-21 13:02:13 -0700370 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000371}
372
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000373///////////////////////////////////////////////////////////////////////////////
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000374
bsalomon861e1032014-12-16 07:33:49 -0800375void GrGLGpu::onResetContext(uint32_t resetBits) {
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000376 if (resetBits & kMisc_GrGLBackendState) {
Brian Salomonf0861672017-05-08 11:10:10 -0400377 // we don't use the zb at all
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000378 GL_CALL(Disable(GR_GL_DEPTH_TEST));
379 GL_CALL(DepthMask(GR_GL_FALSE));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000380
Brian Salomonf0861672017-05-08 11:10:10 -0400381 // We don't use face culling.
382 GL_CALL(Disable(GR_GL_CULL_FACE));
383 // We do use separate stencil. Our algorithms don't care which face is front vs. back so
384 // just set this to the default for self-consistency.
385 GL_CALL(FrontFace(GR_GL_CCW));
386
cdaltone2e71c22016-04-07 18:13:29 -0700387 fHWBufferState[kTexel_GrBufferType].invalidate();
388 fHWBufferState[kDrawIndirect_GrBufferType].invalidate();
389 fHWBufferState[kXferCpuToGpu_GrBufferType].invalidate();
390 fHWBufferState[kXferGpuToCpu_GrBufferType].invalidate();
cdaltonc1613102016-03-16 07:48:20 -0700391
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +0000392 if (kGL_GrGLStandard == this->glStandard()) {
Jim Van Verth32ac83e2016-11-28 15:23:57 -0500393#ifndef USE_NSIGHT
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000394 // Desktop-only state that we never change
395 if (!this->glCaps().isCoreProfile()) {
396 GL_CALL(Disable(GR_GL_POINT_SMOOTH));
397 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
398 GL_CALL(Disable(GR_GL_POLYGON_SMOOTH));
399 GL_CALL(Disable(GR_GL_POLYGON_STIPPLE));
400 GL_CALL(Disable(GR_GL_COLOR_LOGIC_OP));
401 GL_CALL(Disable(GR_GL_INDEX_LOGIC_OP));
402 }
403 // The windows NVIDIA driver has GL_ARB_imaging in the extension string when using a
404 // core profile. This seems like a bug since the core spec removes any mention of
405 // GL_ARB_imaging.
406 if (this->glCaps().imagingSupport() && !this->glCaps().isCoreProfile()) {
407 GL_CALL(Disable(GR_GL_COLOR_TABLE));
408 }
409 GL_CALL(Disable(GR_GL_POLYGON_OFFSET_FILL));
Jim Van Verth609e7cc2017-03-30 14:28:08 -0400410
Jim Van Verthfbdc0802017-05-02 16:15:53 -0400411 if (this->caps()->wireframeMode()) {
412 GL_CALL(PolygonMode(GR_GL_FRONT_AND_BACK, GR_GL_LINE));
413 } else {
414 GL_CALL(PolygonMode(GR_GL_FRONT_AND_BACK, GR_GL_FILL));
415 }
Jim Van Verth32ac83e2016-11-28 15:23:57 -0500416#endif
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000417 // Since ES doesn't support glPointSize at all we always use the VS to
418 // set the point size
419 GL_CALL(Enable(GR_GL_VERTEX_PROGRAM_POINT_SIZE));
420
bsalomon@google.com2b1b8c02013-02-28 22:06:02 +0000421 }
joshualitt58162332014-08-01 06:44:53 -0700422
423 if (kGLES_GrGLStandard == this->glStandard() &&
bsalomon424cc262015-05-22 10:37:30 -0700424 this->hasExtension("GL_ARM_shader_framebuffer_fetch")) {
joshualitt58162332014-08-01 06:44:53 -0700425 // The arm extension requires specifically enabling MSAA fetching per sample.
426 // On some devices this may have a perf hit. Also multiple render targets are disabled
427 GL_CALL(Enable(GR_GL_FETCH_PER_SAMPLE_ARM));
428 }
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000429 fHWWriteToColor = kUnknown_TriState;
430 // we only ever use lines in hairline mode
431 GL_CALL(LineWidth(1));
bsalomonaca31fe2015-09-22 11:38:46 -0700432 GL_CALL(Disable(GR_GL_DITHER));
bsalomon@google.comcad107b2013-06-28 14:32:08 +0000433 }
edisonn@google.comba669992013-06-28 16:03:21 +0000434
egdanielb414f252014-07-29 13:15:47 -0700435 if (resetBits & kMSAAEnable_GrGLBackendState) {
436 fMSAAEnabled = kUnknown_TriState;
vbuzinovdded6962015-06-12 08:59:45 -0700437
egdanieleed519e2016-01-15 11:36:18 -0800438 if (this->caps()->usesMixedSamples()) {
cdaltonaf8bc7d2016-02-05 09:35:20 -0800439 if (0 != this->caps()->maxRasterSamples()) {
440 fHWRasterMultisampleEnabled = kUnknown_TriState;
441 fHWNumRasterSamples = 0;
442 }
443
444 // The skia blend modes all use premultiplied alpha and therefore expect RGBA coverage
445 // modulation. This state has no effect when not rendering to a mixed sampled target.
vbuzinovdded6962015-06-12 08:59:45 -0700446 GL_CALL(CoverageModulation(GR_GL_RGBA));
447 }
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000448 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000449
commit-bot@chromium.org46fbfe02013-08-30 15:52:12 +0000450 fHWActiveTextureUnitIdx = -1; // invalid
Brian Salomonaf971de2017-06-08 16:11:33 -0400451 fLastPrimitiveType = static_cast<GrPrimitiveType>(-1);
commit-bot@chromium.org46fbfe02013-08-30 15:52:12 +0000452
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000453 if (resetBits & kTextureBinding_GrGLBackendState) {
bsalomon1c63bf62014-07-22 13:09:46 -0700454 for (int s = 0; s < fHWBoundTextureUniqueIDs.count(); ++s) {
Robert Phillips294870f2016-11-11 12:38:40 -0500455 fHWBoundTextureUniqueIDs[s].makeInvalid();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000456 }
cdalton74b8d322016-04-11 14:47:28 -0700457 for (int b = 0; b < fHWBufferTextures.count(); ++b) {
458 SkASSERT(this->caps()->shaderCaps()->texelBufferSupport());
459 fHWBufferTextures[b].fKnownBound = false;
460 }
Brian Salomonf9f45122016-11-29 11:59:17 -0500461 for (int i = 0; i < fHWBoundImageStorages.count(); ++i) {
Brian Salomon8b7a7dd2016-11-29 15:29:41 -0500462 SkASSERT(this->caps()->shaderCaps()->imageLoadStoreSupport());
Brian Salomonf9f45122016-11-29 11:59:17 -0500463 fHWBoundImageStorages[i].fTextureUniqueID.makeInvalid();
464 }
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000465 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000466
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000467 if (resetBits & kBlend_GrGLBackendState) {
468 fHWBlendState.invalidate();
469 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000470
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000471 if (resetBits & kView_GrGLBackendState) {
472 fHWScissorSettings.invalidate();
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700473 fHWWindowRectsState.invalidate();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000474 fHWViewport.invalidate();
475 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000476
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000477 if (resetBits & kStencil_GrGLBackendState) {
478 fHWStencilSettings.invalidate();
479 fHWStencilTestEnabled = kUnknown_TriState;
480 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000481
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000482 // Vertex
483 if (resetBits & kVertex_GrGLBackendState) {
cdaltone2e71c22016-04-07 18:13:29 -0700484 fHWVertexArrayState.invalidate();
485 fHWBufferState[kVertex_GrBufferType].invalidate();
486 fHWBufferState[kIndex_GrBufferType].invalidate();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000487 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000488
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000489 if (resetBits & kRenderTarget_GrGLBackendState) {
Robert Phillips294870f2016-11-11 12:38:40 -0500490 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon16921ec2015-07-30 15:34:56 -0700491 fHWSRGBFramebuffer = kUnknown_TriState;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000492 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000493
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000494 if (resetBits & kPathRendering_GrGLBackendState) {
jvanverthe9c0fc62015-04-29 11:18:05 -0700495 if (this->caps()->shaderCaps()->pathRenderingSupport()) {
kkinnunenccdaa042014-08-20 01:36:23 -0700496 this->glPathRendering()->resetContext();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000497 }
bsalomon@google.com05a718c2012-06-29 14:01:53 +0000498 }
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000499
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000500 // we assume these values
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000501 if (resetBits & kPixelStore_GrGLBackendState) {
502 if (this->glCaps().unpackRowLengthSupport()) {
503 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
504 }
505 if (this->glCaps().packRowLengthSupport()) {
506 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
507 }
508 if (this->glCaps().unpackFlipYSupport()) {
509 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
510 }
511 if (this->glCaps().packFlipYSupport()) {
512 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, GR_GL_FALSE));
513 }
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000514 }
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000515
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000516 if (resetBits & kProgram_GrGLBackendState) {
517 fHWProgramID = 0;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000518 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000519}
520
Greg Daniel7ef28f32017-04-20 16:41:55 +0000521sk_sp<GrTexture> GrGLGpu::onWrapBackendTexture(const GrBackendTexture& backendTex,
522 GrSurfaceOrigin origin,
523 GrBackendTextureFlags flags,
524 int sampleCnt,
bungeman6bd52842016-10-27 09:30:08 -0700525 GrWrapOwnership ownership) {
Greg Daniel7ef28f32017-04-20 16:41:55 +0000526 const GrGLTextureInfo* info = backendTex.getGLTextureInfo();
bsalomon091f60c2015-11-10 11:54:56 -0800527 if (!info || !info->fID) {
528 return nullptr;
529 }
robertphillips@google.comb72e5d32012-10-30 15:18:10 +0000530
Greg Daniel7ef28f32017-04-20 16:41:55 +0000531 // next line relies on GrBackendTextureFlags matching GrTexture's
532 bool renderTarget = SkToBool(flags & kRenderTarget_GrBackendTextureFlag);
bsalomon7ea33f52015-11-22 14:51:00 -0800533
bsalomonb15b4c12014-10-29 12:41:57 -0700534 GrGLTexture::IDDesc idDesc;
bsalomon091f60c2015-11-10 11:54:56 -0800535 idDesc.fInfo = *info;
bsalomone5286e02016-01-14 09:24:09 -0800536
bsalomon7ea33f52015-11-22 14:51:00 -0800537 if (GR_GL_TEXTURE_EXTERNAL == idDesc.fInfo.fTarget) {
538 if (renderTarget) {
539 // This combination is not supported.
540 return nullptr;
541 }
Brian Salomon1edc5b92016-11-29 13:43:46 -0500542 if (!this->caps()->shaderCaps()->externalTextureSupport()) {
bsalomon7ea33f52015-11-22 14:51:00 -0800543 return nullptr;
544 }
bsalomone5286e02016-01-14 09:24:09 -0800545 } else if (GR_GL_TEXTURE_RECTANGLE == idDesc.fInfo.fTarget) {
546 if (!this->glCaps().rectangleTextureSupport()) {
547 return nullptr;
548 }
549 } else if (GR_GL_TEXTURE_2D != idDesc.fInfo.fTarget) {
550 return nullptr;
bsalomon7ea33f52015-11-22 14:51:00 -0800551 }
bsalomone5286e02016-01-14 09:24:09 -0800552
553 // Sample count is interpreted to mean the number of samples that Gr code should allocate
bsalomona98419b2015-11-23 07:09:50 -0800554 // for a render buffer that resolves to the texture. We don't support MSAA textures.
Greg Daniel7ef28f32017-04-20 16:41:55 +0000555 if (sampleCnt && !renderTarget) {
bsalomona98419b2015-11-23 07:09:50 -0800556 return nullptr;
557 }
bsalomon10528f12015-10-14 12:54:52 -0700558
Brian Osman766fcbb2017-03-13 09:33:09 -0400559 if (kBorrow_GrWrapOwnership == ownership) {
Brian Osmana6953f22017-03-10 20:14:05 +0000560 idDesc.fOwnership = GrBackendObjectOwnership::kBorrowed;
Brian Osman766fcbb2017-03-13 09:33:09 -0400561 } else {
562 idDesc.fOwnership = GrBackendObjectOwnership::kOwned;
bsalomone5286e02016-01-14 09:24:09 -0800563 }
bsalomonb15b4c12014-10-29 12:41:57 -0700564
Ben Wagner18b61f92016-10-25 10:44:02 -0400565 GrSurfaceDesc surfDesc;
Greg Daniel7ef28f32017-04-20 16:41:55 +0000566 surfDesc.fFlags = (GrSurfaceFlags) flags;
567 surfDesc.fWidth = backendTex.width();
568 surfDesc.fHeight = backendTex.height();
569 surfDesc.fConfig = backendTex.config();
570 surfDesc.fSampleCnt = SkTMin(sampleCnt, this->caps()->maxSampleCount());
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000571 // FIXME: this should be calling resolve_origin(), but Chrome code is currently
572 // assuming the old behaviour, which is that backend textures are always
573 // BottomLeft, even for non-RT's. Once Chrome is fixed, change this to:
574 // glTexDesc.fOrigin = resolve_origin(desc.fOrigin, renderTarget);
Greg Daniel7ef28f32017-04-20 16:41:55 +0000575 if (kDefault_GrSurfaceOrigin == origin) {
bsalomonb15b4c12014-10-29 12:41:57 -0700576 surfDesc.fOrigin = kBottomLeft_GrSurfaceOrigin;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000577 } else {
Greg Daniel7ef28f32017-04-20 16:41:55 +0000578 surfDesc.fOrigin = origin;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000579 }
bsalomon@google.come269f212011-11-07 13:29:52 +0000580
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000581 if (renderTarget) {
bsalomonb15b4c12014-10-29 12:41:57 -0700582 GrGLRenderTarget::IDDesc rtIDDesc;
kkinnunen2e6055b2016-04-22 01:48:29 -0700583 if (!this->createRenderTargetObjects(surfDesc, idDesc.fInfo, &rtIDDesc)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700584 return nullptr;
bsalomon@google.come269f212011-11-07 13:29:52 +0000585 }
Brian Salomon9bada542017-06-12 12:09:30 -0400586 sk_sp<GrGLTextureRenderTarget> texRT(
587 GrGLTextureRenderTarget::MakeWrapped(this, surfDesc, idDesc, rtIDDesc));
588 texRT->baseLevelWasBoundToFBO();
589 return texRT;
bsalomon@google.come269f212011-11-07 13:29:52 +0000590 }
Brian Osman766fcbb2017-03-13 09:33:09 -0400591
Brian Osman85d34b22017-05-10 12:06:26 -0400592 return GrGLTexture::MakeWrapped(this, surfDesc, idDesc);
bsalomon@google.come269f212011-11-07 13:29:52 +0000593}
594
Greg Danielbcf612b2017-05-01 13:50:58 +0000595sk_sp<GrRenderTarget> GrGLGpu::onWrapBackendRenderTarget(const GrBackendRenderTarget& backendRT,
596 GrSurfaceOrigin origin) {
597 const GrGLFramebufferInfo* info = backendRT.getGLFramebufferInfo();
598 if (!info) {
599 return nullptr;
600 }
601
bsalomonb15b4c12014-10-29 12:41:57 -0700602 GrGLRenderTarget::IDDesc idDesc;
Greg Danielbcf612b2017-05-01 13:50:58 +0000603 idDesc.fRTFBOID = info->fFBOID;
bsalomonb15b4c12014-10-29 12:41:57 -0700604 idDesc.fMSColorRenderbufferID = 0;
egdanield803f272015-03-18 13:01:52 -0700605 idDesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
Brian Osman0b791f52017-03-10 08:30:22 -0500606 idDesc.fRTFBOOwnership = GrBackendObjectOwnership::kBorrowed;
csmartdaltonf9635992016-08-10 11:09:07 -0700607 idDesc.fIsMixedSampled = false;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000608
bsalomonb15b4c12014-10-29 12:41:57 -0700609 GrSurfaceDesc desc;
Greg Danielbcf612b2017-05-01 13:50:58 +0000610 desc.fConfig = backendRT.config();
Brian Salomon0ec981b2017-05-15 13:48:50 -0400611 desc.fFlags = kRenderTarget_GrSurfaceFlag;
Greg Danielbcf612b2017-05-01 13:50:58 +0000612 desc.fWidth = backendRT.width();
613 desc.fHeight = backendRT.height();
614 desc.fSampleCnt = SkTMin(backendRT.sampleCnt(), this->caps()->maxSampleCount());
615 SkASSERT(kDefault_GrSurfaceOrigin != origin);
616 desc.fOrigin = origin;
bsalomonb15b4c12014-10-29 12:41:57 -0700617
Greg Danielbcf612b2017-05-01 13:50:58 +0000618 return GrGLRenderTarget::MakeWrapped(this, desc, idDesc, backendRT.stencilBits());
bsalomon@google.come269f212011-11-07 13:29:52 +0000619}
620
Greg Daniel7ef28f32017-04-20 16:41:55 +0000621sk_sp<GrRenderTarget> GrGLGpu::onWrapBackendTextureAsRenderTarget(const GrBackendTexture& tex,
622 GrSurfaceOrigin origin,
623 int sampleCnt) {
624 const GrGLTextureInfo* info = tex.getGLTextureInfo();
ericrkf7b8b8a2016-02-24 14:49:51 -0800625 if (!info || !info->fID) {
626 return nullptr;
627 }
ericrkf7b8b8a2016-02-24 14:49:51 -0800628
jvanverthdb1adf52016-05-03 05:37:45 -0700629 GrGLTextureInfo texInfo;
jvanverthdb1adf52016-05-03 05:37:45 -0700630 texInfo = *info;
ericrkf7b8b8a2016-02-24 14:49:51 -0800631
jvanverthdb1adf52016-05-03 05:37:45 -0700632 if (GR_GL_TEXTURE_RECTANGLE != texInfo.fTarget &&
633 GR_GL_TEXTURE_2D != texInfo.fTarget) {
ericrkf7b8b8a2016-02-24 14:49:51 -0800634 // Only texture rectangle and texture 2d are supported. We do not check whether texture
635 // rectangle is supported by Skia - if the caller provided us with a texture rectangle,
636 // we assume the necessary support exists.
637 return nullptr;
638 }
639
Ben Wagner18b61f92016-10-25 10:44:02 -0400640 GrSurfaceDesc surfDesc;
Greg Daniel7ef28f32017-04-20 16:41:55 +0000641 surfDesc.fFlags = kRenderTarget_GrSurfaceFlag;
642 surfDesc.fWidth = tex.width();
643 surfDesc.fHeight = tex.height();
644 surfDesc.fConfig = tex.config();
645 surfDesc.fSampleCnt = SkTMin(sampleCnt, this->caps()->maxSampleCount());
ericrkf7b8b8a2016-02-24 14:49:51 -0800646 // FIXME: this should be calling resolve_origin(), but Chrome code is currently
647 // assuming the old behaviour, which is that backend textures are always
648 // BottomLeft, even for non-RT's. Once Chrome is fixed, change this to:
649 // glTexDesc.fOrigin = resolve_origin(desc.fOrigin, renderTarget);
Greg Daniel7ef28f32017-04-20 16:41:55 +0000650 if (kDefault_GrSurfaceOrigin == origin) {
ericrkf7b8b8a2016-02-24 14:49:51 -0800651 surfDesc.fOrigin = kBottomLeft_GrSurfaceOrigin;
652 } else {
Greg Daniel7ef28f32017-04-20 16:41:55 +0000653 surfDesc.fOrigin = origin;
ericrkf7b8b8a2016-02-24 14:49:51 -0800654 }
655
656 GrGLRenderTarget::IDDesc rtIDDesc;
jvanverthdb1adf52016-05-03 05:37:45 -0700657 if (!this->createRenderTargetObjects(surfDesc, texInfo, &rtIDDesc)) {
ericrkf7b8b8a2016-02-24 14:49:51 -0800658 return nullptr;
659 }
bungeman6bd52842016-10-27 09:30:08 -0700660 return GrGLRenderTarget::MakeWrapped(this, surfDesc, rtIDDesc, 0);
ericrkf7b8b8a2016-02-24 14:49:51 -0800661}
662
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000663////////////////////////////////////////////////////////////////////////////////
ericrkf7b8b8a2016-02-24 14:49:51 -0800664
bsalomonf0674512015-07-28 13:26:15 -0700665bool GrGLGpu::onGetWritePixelsInfo(GrSurface* dstSurface, int width, int height,
cblumeed828002016-02-16 13:00:01 -0800666 GrPixelConfig srcConfig,
bsalomonf0674512015-07-28 13:26:15 -0700667 DrawPreference* drawPreference,
668 WritePixelTempDrawInfo* tempDrawInfo) {
Brian Salomon9bada542017-06-12 12:09:30 -0400669 if (SkToBool(dstSurface->asRenderTarget())) {
670 if (this->glCaps().useDrawInsteadOfAllRenderTargetWrites()) {
671 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
672 }
673 }
674
675 GrGLTexture* texture = static_cast<GrGLTexture*>(dstSurface->asTexture());
676
677 if (texture) {
bsalomone5286e02016-01-14 09:24:09 -0800678 if (GR_GL_TEXTURE_EXTERNAL == texture->target()) {
679 // We don't currently support writing pixels to EXTERNAL textures.
bsalomon7ea33f52015-11-22 14:51:00 -0800680 return false;
681 }
Brian Salomon9bada542017-06-12 12:09:30 -0400682 if (GrPixelConfigIsUnorm(texture->config()) && texture->hasBaseLevelBeenBoundToFBO() &&
683 this->glCaps().disallowTexSubImageForUnormConfigTexturesEverBoundToFBO() &&
684 (width < dstSurface->width() || height < dstSurface->height())) {
685 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
686 }
687 } else {
688 // This subclass only allows writes to textures. If the dst is not a texture we have to draw
689 // into it. We could use glDrawPixels on GLs that have it, but we don't today.
690 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
bsalomon6cb3cbe2015-07-30 07:34:27 -0700691 }
692
Brian Osman33910292017-04-18 14:38:53 -0400693 // If the dst is MSAA, we have to draw, or we'll just be writing to the resolve target.
694 if (dstSurface->asRenderTarget() && dstSurface->asRenderTarget()->numColorSamples() > 0) {
695 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
696 }
697
bsalomon16921ec2015-07-30 15:34:56 -0700698 if (GrPixelConfigIsSRGB(dstSurface->config()) != GrPixelConfigIsSRGB(srcConfig)) {
699 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
700 }
701
bsalomon6c9cd552016-01-22 07:17:34 -0800702 // Start off assuming no swizzling
703 tempDrawInfo->fSwizzle = GrSwizzle::RGBA();
704 tempDrawInfo->fWriteConfig = srcConfig;
bsalomonf0674512015-07-28 13:26:15 -0700705
706 // These settings we will always want if a temp draw is performed. Initially set the config
707 // to srcConfig, though that may be modified if we decide to do a R/G swap.
708 tempDrawInfo->fTempSurfaceDesc.fFlags = kNone_GrSurfaceFlags;
709 tempDrawInfo->fTempSurfaceDesc.fConfig = srcConfig;
710 tempDrawInfo->fTempSurfaceDesc.fWidth = width;
711 tempDrawInfo->fTempSurfaceDesc.fHeight = height;
712 tempDrawInfo->fTempSurfaceDesc.fSampleCnt = 0;
713 tempDrawInfo->fTempSurfaceDesc.fOrigin = kTopLeft_GrSurfaceOrigin; // no CPU y-flip for TL.
714
715 bool configsAreRBSwaps = GrPixelConfigSwapRAndB(srcConfig) == dstSurface->config();
716
717 if (configsAreRBSwaps) {
718 if (!this->caps()->isConfigTexturable(srcConfig)) {
719 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
720 tempDrawInfo->fTempSurfaceDesc.fConfig = dstSurface->config();
bsalomon6c9cd552016-01-22 07:17:34 -0800721 tempDrawInfo->fSwizzle = GrSwizzle::BGRA();
722 tempDrawInfo->fWriteConfig = dstSurface->config();
bsalomon88c7b982015-07-31 11:20:16 -0700723 } else if (this->glCaps().rgba8888PixelsOpsAreSlow() &&
724 kRGBA_8888_GrPixelConfig == srcConfig) {
bsalomonf0674512015-07-28 13:26:15 -0700725 ElevateDrawPreference(drawPreference, kGpuPrefersDraw_DrawPreference);
726 tempDrawInfo->fTempSurfaceDesc.fConfig = dstSurface->config();
bsalomon6c9cd552016-01-22 07:17:34 -0800727 tempDrawInfo->fSwizzle = GrSwizzle::BGRA();
728 tempDrawInfo->fWriteConfig = dstSurface->config();
bsalomonf0674512015-07-28 13:26:15 -0700729 } else if (kGLES_GrGLStandard == this->glStandard() &&
730 this->glCaps().bgraIsInternalFormat()) {
731 // The internal format and external formats must match texture uploads so we can't
732 // swizzle while uploading when BGRA is a distinct internal format.
733 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
734 tempDrawInfo->fTempSurfaceDesc.fConfig = dstSurface->config();
bsalomon6c9cd552016-01-22 07:17:34 -0800735 tempDrawInfo->fSwizzle = GrSwizzle::BGRA();
736 tempDrawInfo->fWriteConfig = dstSurface->config();
bsalomonf0674512015-07-28 13:26:15 -0700737 }
738 }
739
740 if (!this->glCaps().unpackFlipYSupport() &&
741 kBottomLeft_GrSurfaceOrigin == dstSurface->origin()) {
742 ElevateDrawPreference(drawPreference, kGpuPrefersDraw_DrawPreference);
743 }
744
745 return true;
746}
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000747
jvanverth17aa0472016-01-05 10:41:27 -0800748static bool check_write_and_transfer_input(GrGLTexture* glTex, GrSurface* surface,
749 GrPixelConfig config) {
bsalomon6cb3cbe2015-07-30 07:34:27 -0700750 if (!glTex) {
751 return false;
752 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000753
bsalomon16921ec2015-07-30 15:34:56 -0700754 // OpenGL doesn't do sRGB <-> linear conversions when reading and writing pixels.
755 if (GrPixelConfigIsSRGB(surface->config()) != GrPixelConfigIsSRGB(config)) {
756 return false;
757 }
758
bsalomone5286e02016-01-14 09:24:09 -0800759 // Write or transfer of pixels is not implemented for TEXTURE_EXTERNAL textures
760 if (GR_GL_TEXTURE_EXTERNAL == glTex->target()) {
bsalomon7ea33f52015-11-22 14:51:00 -0800761 return false;
762 }
763
jvanverth17aa0472016-01-05 10:41:27 -0800764 return true;
765}
766
767bool GrGLGpu::onWritePixels(GrSurface* surface,
768 int left, int top, int width, int height,
cblume55f2d2d2016-02-26 13:20:48 -0800769 GrPixelConfig config,
770 const SkTArray<GrMipLevel>& texels) {
jvanverth17aa0472016-01-05 10:41:27 -0800771 GrGLTexture* glTex = static_cast<GrGLTexture*>(surface->asTexture());
772
773 if (!check_write_and_transfer_input(glTex, surface, config)) {
774 return false;
775 }
776
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +0000777 this->setScratchTextureUnit();
bsalomon10528f12015-10-14 12:54:52 -0700778 GL_CALL(BindTexture(glTex->target(), glTex->textureID()));
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000779
Robert Phillips92de6312017-05-23 07:43:48 -0400780 return this->uploadTexData(glTex->config(), glTex->width(), glTex->height(),
781 glTex->origin(), glTex->target(), kWrite_UploadType,
782 left, top, width, height, config, texels);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000783}
784
jvanverthc3d706f2016-04-20 10:33:27 -0700785bool GrGLGpu::onTransferPixels(GrSurface* surface,
jvanverth17aa0472016-01-05 10:41:27 -0800786 int left, int top, int width, int height,
cdalton397536c2016-03-25 12:15:03 -0700787 GrPixelConfig config, GrBuffer* transferBuffer,
jvanverth17aa0472016-01-05 10:41:27 -0800788 size_t offset, size_t rowBytes) {
jvanverthc3d706f2016-04-20 10:33:27 -0700789 GrGLTexture* glTex = static_cast<GrGLTexture*>(surface->asTexture());
jvanverth17aa0472016-01-05 10:41:27 -0800790
jvanverthc3d706f2016-04-20 10:33:27 -0700791 if (!check_write_and_transfer_input(glTex, surface, config)) {
jvanverth17aa0472016-01-05 10:41:27 -0800792 return false;
793 }
794
jvanverth17aa0472016-01-05 10:41:27 -0800795 this->setScratchTextureUnit();
796 GL_CALL(BindTexture(glTex->target(), glTex->textureID()));
797
cdalton397536c2016-03-25 12:15:03 -0700798 SkASSERT(!transferBuffer->isMapped());
799 SkASSERT(!transferBuffer->isCPUBacked());
800 const GrGLBuffer* glBuffer = static_cast<const GrGLBuffer*>(transferBuffer);
cdaltone2e71c22016-04-07 18:13:29 -0700801 this->bindBuffer(kXferCpuToGpu_GrBufferType, glBuffer);
jvanverth17aa0472016-01-05 10:41:27 -0800802
803 bool success = false;
cblume55f2d2d2016-02-26 13:20:48 -0800804 GrMipLevel mipLevel;
jvanverthc3d706f2016-04-20 10:33:27 -0700805 mipLevel.fPixels = transferBuffer;
cblume55f2d2d2016-02-26 13:20:48 -0800806 mipLevel.fRowBytes = rowBytes;
807 SkSTArray<1, GrMipLevel> texels;
808 texels.push_back(mipLevel);
Brian Salomond1eaf492017-05-18 10:02:08 -0400809 success = this->uploadTexData(glTex->config(), glTex->width(), glTex->height(), glTex->origin(),
810 glTex->target(), kTransfer_UploadType, left, top, width, height,
811 config, texels);
jvanverth900bd4a2016-04-29 13:53:12 -0700812 return success;
jvanverth17aa0472016-01-05 10:41:27 -0800813}
814
bsalomonf46a1242015-12-15 12:37:38 -0800815// For GL_[UN]PACK_ALIGNMENT.
816static inline GrGLint config_alignment(GrPixelConfig config) {
bsalomonf46a1242015-12-15 12:37:38 -0800817 switch (config) {
818 case kAlpha_8_GrPixelConfig:
Brian Osman986563b2017-01-10 14:20:02 -0500819 case kGray_8_GrPixelConfig:
bsalomonf46a1242015-12-15 12:37:38 -0800820 return 1;
821 case kRGB_565_GrPixelConfig:
822 case kRGBA_4444_GrPixelConfig:
823 case kAlpha_half_GrPixelConfig:
824 case kRGBA_half_GrPixelConfig:
825 return 2;
826 case kRGBA_8888_GrPixelConfig:
827 case kBGRA_8888_GrPixelConfig:
828 case kSRGBA_8888_GrPixelConfig:
brianosmana6359362016-03-21 06:55:37 -0700829 case kSBGRA_8888_GrPixelConfig:
Brian Salomonbf7b6202016-11-11 16:08:03 -0500830 case kRGBA_8888_sint_GrPixelConfig:
bsalomonf46a1242015-12-15 12:37:38 -0800831 case kRGBA_float_GrPixelConfig:
csmartdalton6aa0e112017-02-08 16:14:11 -0500832 case kRG_float_GrPixelConfig:
bsalomonf46a1242015-12-15 12:37:38 -0800833 return 4;
csmartdalton6aa0e112017-02-08 16:14:11 -0500834 case kUnknown_GrPixelConfig:
bsalomonf46a1242015-12-15 12:37:38 -0800835 return 0;
836 }
csmartdalton6aa0e112017-02-08 16:14:11 -0500837 SkFAIL("Invalid pixel config");
838 return 0;
bsalomonf46a1242015-12-15 12:37:38 -0800839}
840
cblume55f2d2d2016-02-26 13:20:48 -0800841/**
842 * Creates storage space for the texture and fills it with texels.
843 *
Brian Salomond1eaf492017-05-18 10:02:08 -0400844 * @param config Pixel config of the texture.
cblume55f2d2d2016-02-26 13:20:48 -0800845 * @param interface The GL interface in use.
cblume790d5132016-02-29 11:13:29 -0800846 * @param caps The capabilities of the GL device.
brianosman81a84852016-09-12 09:05:14 -0700847 * @param internalFormat The data format used for the internal storage of the texture. May be sized.
848 * @param internalFormatForTexStorage The data format used for the TexStorage API. Must be sized.
cblume55f2d2d2016-02-26 13:20:48 -0800849 * @param externalFormat The data format used for the external storage of the texture.
850 * @param externalType The type of the data used for the external storage of the texture.
851 * @param texels The texel data of the texture being created.
852 * @param baseWidth The width of the texture's base mipmap level
853 * @param baseHeight The height of the texture's base mipmap level
cblume55f2d2d2016-02-26 13:20:48 -0800854 */
Robert Phillips92de6312017-05-23 07:43:48 -0400855static bool allocate_and_populate_texture(GrPixelConfig config,
856 const GrGLInterface& interface,
857 const GrGLCaps& caps,
858 GrGLenum target,
859 GrGLenum internalFormat,
860 GrGLenum internalFormatForTexStorage,
861 GrGLenum externalFormat,
862 GrGLenum externalType,
863 const SkTArray<GrMipLevel>& texels,
864 int baseWidth, int baseHeight) {
cblume55f2d2d2016-02-26 13:20:48 -0800865 CLEAR_ERROR_BEFORE_ALLOC(&interface);
cblume790d5132016-02-29 11:13:29 -0800866
Brian Salomond1eaf492017-05-18 10:02:08 -0400867 bool useTexStorage = caps.isConfigTexSupportEnabled(config);
cblume790d5132016-02-29 11:13:29 -0800868 // We can only use TexStorage if we know we will not later change the storage requirements.
869 // This means if we may later want to add mipmaps, we cannot use TexStorage.
870 // Right now, we cannot know if we will later add mipmaps or not.
871 // The only time we can use TexStorage is when we already have the
Brian Salomonbf7b6202016-11-11 16:08:03 -0500872 // mipmaps or are using a format incompatible with MIP maps.
Brian Salomond1eaf492017-05-18 10:02:08 -0400873 useTexStorage &= texels.count() > 1 || GrPixelConfigIsSint(config);
cblume790d5132016-02-29 11:13:29 -0800874
875 if (useTexStorage) {
876 // We never resize or change formats of textures.
cblume55f2d2d2016-02-26 13:20:48 -0800877 GL_ALLOC_CALL(&interface,
Brian Salomond1eaf492017-05-18 10:02:08 -0400878 TexStorage2D(target, SkTMax(texels.count(), 1), internalFormatForTexStorage,
879 baseWidth, baseHeight));
Brian Salomon0ec981b2017-05-15 13:48:50 -0400880 GrGLenum error = CHECK_ALLOC_ERROR(&interface);
cblume55f2d2d2016-02-26 13:20:48 -0800881 if (error != GR_GL_NO_ERROR) {
bsalomone699d0c2016-03-09 06:25:15 -0800882 return false;
cblume790d5132016-02-29 11:13:29 -0800883 } else {
884 for (int currentMipLevel = 0; currentMipLevel < texels.count(); currentMipLevel++) {
885 const void* currentMipData = texels[currentMipLevel].fPixels;
886 if (currentMipData == nullptr) {
887 continue;
888 }
889 int twoToTheMipLevel = 1 << currentMipLevel;
Brian Salomond1eaf492017-05-18 10:02:08 -0400890 int currentWidth = SkTMax(1, baseWidth / twoToTheMipLevel);
891 int currentHeight = SkTMax(1, baseHeight / twoToTheMipLevel);
cblume790d5132016-02-29 11:13:29 -0800892
893 GR_GL_CALL(&interface,
894 TexSubImage2D(target,
895 currentMipLevel,
896 0, // left
897 0, // top
898 currentWidth,
899 currentHeight,
900 externalFormat, externalType,
901 currentMipData));
902 }
bsalomone699d0c2016-03-09 06:25:15 -0800903 return true;
cblume790d5132016-02-29 11:13:29 -0800904 }
905 } else {
bsalomone699d0c2016-03-09 06:25:15 -0800906 if (texels.empty()) {
cblume790d5132016-02-29 11:13:29 -0800907 GL_ALLOC_CALL(&interface,
908 TexImage2D(target,
bsalomone699d0c2016-03-09 06:25:15 -0800909 0,
cblume790d5132016-02-29 11:13:29 -0800910 internalFormat,
bsalomone699d0c2016-03-09 06:25:15 -0800911 baseWidth,
912 baseHeight,
cblume790d5132016-02-29 11:13:29 -0800913 0, // border
914 externalFormat, externalType,
bsalomone699d0c2016-03-09 06:25:15 -0800915 nullptr));
Brian Salomon0ec981b2017-05-15 13:48:50 -0400916 GrGLenum error = CHECK_ALLOC_ERROR(&interface);
cblume790d5132016-02-29 11:13:29 -0800917 if (error != GR_GL_NO_ERROR) {
bsalomone699d0c2016-03-09 06:25:15 -0800918 return false;
919 }
920 } else {
921 for (int currentMipLevel = 0; currentMipLevel < texels.count(); currentMipLevel++) {
922 int twoToTheMipLevel = 1 << currentMipLevel;
923 int currentWidth = SkTMax(1, baseWidth / twoToTheMipLevel);
924 int currentHeight = SkTMax(1, baseHeight / twoToTheMipLevel);
925 const void* currentMipData = texels[currentMipLevel].fPixels;
926 // Even if curremtMipData is nullptr, continue to call TexImage2D.
927 // This will allocate texture memory which we can later populate.
928 GL_ALLOC_CALL(&interface,
929 TexImage2D(target,
930 currentMipLevel,
931 internalFormat,
932 currentWidth,
933 currentHeight,
934 0, // border
935 externalFormat, externalType,
936 currentMipData));
Brian Salomon0ec981b2017-05-15 13:48:50 -0400937 GrGLenum error = CHECK_ALLOC_ERROR(&interface);
bsalomone699d0c2016-03-09 06:25:15 -0800938 if (error != GR_GL_NO_ERROR) {
939 return false;
940 }
cblume790d5132016-02-29 11:13:29 -0800941 }
cblume55f2d2d2016-02-26 13:20:48 -0800942 }
943 }
bsalomone699d0c2016-03-09 06:25:15 -0800944 return true;
cblume55f2d2d2016-02-26 13:20:48 -0800945}
946
947/**
cblume55f2d2d2016-02-26 13:20:48 -0800948 * After a texture is created, any state which was altered during its creation
949 * needs to be restored.
950 *
951 * @param interface The GL interface to use.
952 * @param caps The capabilities of the GL device.
953 * @param restoreGLRowLength Should the row length unpacking be restored?
954 * @param glFlipY Did GL flip the texture vertically?
955 */
956static void restore_pixelstore_state(const GrGLInterface& interface, const GrGLCaps& caps,
957 bool restoreGLRowLength, bool glFlipY) {
958 if (restoreGLRowLength) {
959 SkASSERT(caps.unpackRowLengthSupport());
960 GR_GL_CALL(&interface, PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
961 }
962 if (glFlipY) {
963 GR_GL_CALL(&interface, PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
964 }
965}
966
Brian Salomond1eaf492017-05-18 10:02:08 -0400967bool GrGLGpu::uploadTexData(GrPixelConfig texConfig, int texWidth, int texHeight,
968 GrSurfaceOrigin texOrigin, GrGLenum target, UploadType uploadType,
969 int left, int top, int width, int height, GrPixelConfig dataConfig,
cblume55f2d2d2016-02-26 13:20:48 -0800970 const SkTArray<GrMipLevel>& texels) {
Brian Salomond1eaf492017-05-18 10:02:08 -0400971 SkASSERT(this->caps()->isConfigTexturable(texConfig));
bsalomon5b30c6f2015-12-17 14:17:34 -0800972
cblume55f2d2d2016-02-26 13:20:48 -0800973 // texels is const.
974 // But we may need to flip the texture vertically to prepare it.
975 // Rather than flip in place and alter the incoming data,
976 // we allocate a new buffer to flip into.
977 // This means we need to make a non-const shallow copy of texels.
978 SkTArray<GrMipLevel> texelsShallowCopy(texels);
979
980 for (int currentMipLevel = texelsShallowCopy.count() - 1; currentMipLevel >= 0;
981 currentMipLevel--) {
bsalomone699d0c2016-03-09 06:25:15 -0800982 SkASSERT(texelsShallowCopy[currentMipLevel].fPixels || kTransfer_UploadType == uploadType);
cblume55f2d2d2016-02-26 13:20:48 -0800983 }
984
985 const GrGLInterface* interface = this->glInterface();
986 const GrGLCaps& caps = this->glCaps();
987
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000988 size_t bpp = GrBytesPerPixel(dataConfig);
cblume55f2d2d2016-02-26 13:20:48 -0800989
990 if (width == 0 || height == 0) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000991 return false;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000992 }
bsalomon@google.com6f379512011-11-16 20:36:03 +0000993
cblume55f2d2d2016-02-26 13:20:48 -0800994 for (int currentMipLevel = 0; currentMipLevel < texelsShallowCopy.count(); currentMipLevel++) {
995 int twoToTheMipLevel = 1 << currentMipLevel;
996 int currentWidth = SkTMax(1, width / twoToTheMipLevel);
997 int currentHeight = SkTMax(1, height / twoToTheMipLevel);
998
cblume55f2d2d2016-02-26 13:20:48 -0800999 if (currentHeight > SK_MaxS32 ||
1000 currentWidth > SK_MaxS32) {
1001 return false;
1002 }
Brian Salomond1eaf492017-05-18 10:02:08 -04001003 if (!GrSurfacePriv::AdjustWritePixelParams(texWidth, texHeight, bpp, &left, &top,
1004 &currentWidth, &currentHeight,
1005 &texelsShallowCopy[currentMipLevel].fPixels,
1006 &texelsShallowCopy[currentMipLevel].fRowBytes)) {
cblume55f2d2d2016-02-26 13:20:48 -08001007 return false;
1008 }
1009 if (currentWidth < 0 || currentHeight < 0) {
1010 return false;
1011 }
1012 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001013
bsalomon5b30c6f2015-12-17 14:17:34 -08001014 // Internal format comes from the texture desc.
bsalomon76148af2016-01-12 11:13:47 -08001015 GrGLenum internalFormat;
bsalomon5b30c6f2015-12-17 14:17:34 -08001016 // External format and type come from the upload data.
bsalomon76148af2016-01-12 11:13:47 -08001017 GrGLenum externalFormat;
1018 GrGLenum externalType;
Brian Salomond1eaf492017-05-18 10:02:08 -04001019 if (!this->glCaps().getTexImageFormats(texConfig, dataConfig, &internalFormat, &externalFormat,
1020 &externalType)) {
bsalomon76148af2016-01-12 11:13:47 -08001021 return false;
1022 }
brianosman81a84852016-09-12 09:05:14 -07001023 // TexStorage requires a sized format, and internalFormat may or may not be
Brian Salomond1eaf492017-05-18 10:02:08 -04001024 GrGLenum internalFormatForTexStorage = this->glCaps().configSizedInternalFormat(texConfig);
brianosman81a84852016-09-12 09:05:14 -07001025
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001026 /*
bsalomon5b30c6f2015-12-17 14:17:34 -08001027 * Check whether to allocate a temporary buffer for flipping y or
bsalomon@google.com6f379512011-11-16 20:36:03 +00001028 * because our srcData has extra bytes past each row. If so, we need
1029 * to trim those off here, since GL ES may not let us specify
1030 * GL_UNPACK_ROW_LENGTH.
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001031 */
bsalomon@google.com6f379512011-11-16 20:36:03 +00001032 bool restoreGLRowLength = false;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +00001033 bool swFlipY = false;
1034 bool glFlipY = false;
cblume55f2d2d2016-02-26 13:20:48 -08001035
Brian Salomond1eaf492017-05-18 10:02:08 -04001036 if (kBottomLeft_GrSurfaceOrigin == texOrigin && !texelsShallowCopy.empty()) {
cblume55f2d2d2016-02-26 13:20:48 -08001037 if (caps.unpackFlipYSupport()) {
1038 glFlipY = true;
1039 } else {
1040 swFlipY = true;
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001041 }
cblume55f2d2d2016-02-26 13:20:48 -08001042 }
1043
1044 // in case we need a temporary, trimmed copy of the src pixels
1045 SkAutoSMalloc<128 * 128> tempStorage;
1046
1047 // find the combined size of all the mip levels and the relative offset of
1048 // each into the collective buffer
1049 size_t combined_buffer_size = 0;
1050 SkTArray<size_t> individual_mip_offsets(texelsShallowCopy.count());
1051 for (int currentMipLevel = 0; currentMipLevel < texelsShallowCopy.count(); currentMipLevel++) {
1052 int twoToTheMipLevel = 1 << currentMipLevel;
1053 int currentWidth = SkTMax(1, width / twoToTheMipLevel);
1054 int currentHeight = SkTMax(1, height / twoToTheMipLevel);
1055 const size_t trimmedSize = currentWidth * bpp * currentHeight;
1056 individual_mip_offsets.push_back(combined_buffer_size);
1057 combined_buffer_size += trimmedSize;
1058 }
1059 char* buffer = (char*)tempStorage.reset(combined_buffer_size);
1060
1061 for (int currentMipLevel = 0; currentMipLevel < texelsShallowCopy.count(); currentMipLevel++) {
cblume55f2d2d2016-02-26 13:20:48 -08001062 int twoToTheMipLevel = 1 << currentMipLevel;
1063 int currentWidth = SkTMax(1, width / twoToTheMipLevel);
1064 int currentHeight = SkTMax(1, height / twoToTheMipLevel);
1065 const size_t trimRowBytes = currentWidth * bpp;
1066
1067 /*
1068 * check whether to allocate a temporary buffer for flipping y or
1069 * because our srcData has extra bytes past each row. If so, we need
1070 * to trim those off here, since GL ES may not let us specify
1071 * GL_UNPACK_ROW_LENGTH.
1072 */
1073 restoreGLRowLength = false;
1074
1075 const size_t rowBytes = texelsShallowCopy[currentMipLevel].fRowBytes;
ericrk154349b2016-05-10 14:36:53 -07001076
1077 // TODO: This optimization should be enabled with or without mips.
1078 // For use with mips, we must set GR_GL_UNPACK_ROW_LENGTH once per
1079 // mip level, before calling glTexImage2D.
1080 const bool usesMips = texelsShallowCopy.count() > 1;
1081 if (caps.unpackRowLengthSupport() && !swFlipY && !usesMips) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001082 // can't use this for flipping, only non-neg values allowed. :(
1083 if (rowBytes != trimRowBytes) {
1084 GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
cblume55f2d2d2016-02-26 13:20:48 -08001085 GR_GL_CALL(interface, PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength));
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001086 restoreGLRowLength = true;
1087 }
jvanverth17aa0472016-01-05 10:41:27 -08001088 } else if (kTransfer_UploadType != uploadType) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001089 if (trimRowBytes != rowBytes || swFlipY) {
1090 // copy data into our new storage, skipping the trailing bytes
cblume55f2d2d2016-02-26 13:20:48 -08001091 const char* src = (const char*)texelsShallowCopy[currentMipLevel].fPixels;
1092 if (swFlipY && currentHeight >= 1) {
1093 src += (currentHeight - 1) * rowBytes;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001094 }
cblume55f2d2d2016-02-26 13:20:48 -08001095 char* dst = buffer + individual_mip_offsets[currentMipLevel];
1096 for (int y = 0; y < currentHeight; y++) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001097 memcpy(dst, src, trimRowBytes);
1098 if (swFlipY) {
1099 src -= rowBytes;
1100 } else {
1101 src += rowBytes;
1102 }
1103 dst += trimRowBytes;
1104 }
1105 // now point data to our copied version
cblume55f2d2d2016-02-26 13:20:48 -08001106 texelsShallowCopy[currentMipLevel].fPixels = buffer +
1107 individual_mip_offsets[currentMipLevel];
1108 texelsShallowCopy[currentMipLevel].fRowBytes = trimRowBytes;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001109 }
jvanverth17aa0472016-01-05 10:41:27 -08001110 } else {
1111 return false;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001112 }
bsalomone699d0c2016-03-09 06:25:15 -08001113 }
1114
1115 if (!texelsShallowCopy.empty()) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001116 if (glFlipY) {
cblume55f2d2d2016-02-26 13:20:48 -08001117 GR_GL_CALL(interface, PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_TRUE));
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001118 }
Brian Salomond1eaf492017-05-18 10:02:08 -04001119 GR_GL_CALL(interface, PixelStorei(GR_GL_UNPACK_ALIGNMENT, config_alignment(texConfig)));
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001120 }
cblume55f2d2d2016-02-26 13:20:48 -08001121
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001122 bool succeeded = true;
Brian Salomond1eaf492017-05-18 10:02:08 -04001123 if (kNewTexture_UploadType == uploadType) {
1124 if (0 == left && 0 == top && texWidth == width && texHeight == height) {
Robert Phillips92de6312017-05-23 07:43:48 -04001125 succeeded = allocate_and_populate_texture(
Brian Salomond1eaf492017-05-18 10:02:08 -04001126 texConfig, *interface, caps, target, internalFormat,
1127 internalFormatForTexStorage, externalFormat, externalType, texelsShallowCopy,
1128 width, height);
1129 } else {
1130 succeeded = false;
1131 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001132 } else {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +00001133 if (swFlipY || glFlipY) {
Brian Salomond1eaf492017-05-18 10:02:08 -04001134 top = texHeight - (top + height);
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001135 }
cblume55f2d2d2016-02-26 13:20:48 -08001136 for (int currentMipLevel = 0; currentMipLevel < texelsShallowCopy.count();
1137 currentMipLevel++) {
1138 int twoToTheMipLevel = 1 << currentMipLevel;
1139 int currentWidth = SkTMax(1, width / twoToTheMipLevel);
1140 int currentHeight = SkTMax(1, height / twoToTheMipLevel);
cblume55f2d2d2016-02-26 13:20:48 -08001141
1142 GL_CALL(TexSubImage2D(target,
1143 currentMipLevel,
1144 left, top,
1145 currentWidth,
1146 currentHeight,
1147 externalFormat, externalType,
1148 texelsShallowCopy[currentMipLevel].fPixels));
1149 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00001150 }
1151
cblume55f2d2d2016-02-26 13:20:48 -08001152 restore_pixelstore_state(*interface, caps, restoreGLRowLength, glFlipY);
1153
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001154 return succeeded;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001155}
1156
bsalomon424cc262015-05-22 10:37:30 -07001157static bool renderbuffer_storage_msaa(const GrGLContext& ctx,
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001158 int sampleCount,
1159 GrGLenum format,
1160 int width, int height) {
robertphillips@google.com6177e692013-02-28 20:16:25 +00001161 CLEAR_ERROR_BEFORE_ALLOC(ctx.interface());
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +00001162 SkASSERT(GrGLCaps::kNone_MSFBOType != ctx.caps()->msFBOType());
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +00001163 switch (ctx.caps()->msFBOType()) {
Brian Salomon00731b42016-10-14 11:30:51 -04001164 case GrGLCaps::kStandard_MSFBOType:
vbuzinovdded6962015-06-12 08:59:45 -07001165 case GrGLCaps::kMixedSamples_MSFBOType:
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001166 GL_ALLOC_CALL(ctx.interface(),
1167 RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
1168 sampleCount,
1169 format,
1170 width, height));
1171 break;
1172 case GrGLCaps::kES_Apple_MSFBOType:
1173 GL_ALLOC_CALL(ctx.interface(),
1174 RenderbufferStorageMultisampleES2APPLE(GR_GL_RENDERBUFFER,
1175 sampleCount,
1176 format,
1177 width, height));
1178 break;
1179 case GrGLCaps::kES_EXT_MsToTexture_MSFBOType:
1180 case GrGLCaps::kES_IMG_MsToTexture_MSFBOType:
1181 GL_ALLOC_CALL(ctx.interface(),
1182 RenderbufferStorageMultisampleES2EXT(GR_GL_RENDERBUFFER,
1183 sampleCount,
1184 format,
1185 width, height));
1186 break;
1187 case GrGLCaps::kNone_MSFBOType:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +00001188 SkFAIL("Shouldn't be here if we don't support multisampled renderbuffers.");
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001189 break;
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001190 }
Brian Salomon9251d4e2015-03-17 16:03:19 -04001191 return (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctx.interface()));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001192}
1193
egdanielb0e1be22015-04-22 13:27:39 -07001194bool GrGLGpu::createRenderTargetObjects(const GrSurfaceDesc& desc,
bsalomon091f60c2015-11-10 11:54:56 -08001195 const GrGLTextureInfo& texInfo,
bsalomonb15b4c12014-10-29 12:41:57 -07001196 GrGLRenderTarget::IDDesc* idDesc) {
1197 idDesc->fMSColorRenderbufferID = 0;
egdanield803f272015-03-18 13:01:52 -07001198 idDesc->fRTFBOID = 0;
kkinnunen2e6055b2016-04-22 01:48:29 -07001199 idDesc->fRTFBOOwnership = GrBackendObjectOwnership::kOwned;
egdanield803f272015-03-18 13:01:52 -07001200 idDesc->fTexFBOID = 0;
robertphillips76948d42016-05-04 12:47:41 -07001201 SkASSERT((GrGLCaps::kMixedSamples_MSFBOType == this->glCaps().msFBOType()) ==
1202 this->caps()->usesMixedSamples());
csmartdaltonf9635992016-08-10 11:09:07 -07001203 idDesc->fIsMixedSampled = desc.fSampleCnt > 0 && this->caps()->usesMixedSamples();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001204
1205 GrGLenum status;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001206
bsalomona11e5fc2015-12-18 07:59:41 -08001207 GrGLenum colorRenderbufferFormat = 0; // suppress warning
bsalomon@google.comab15d612011-08-09 12:57:56 +00001208
bsalomonb15b4c12014-10-29 12:41:57 -07001209 if (desc.fSampleCnt > 0 && GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.com347c3822013-05-01 20:10:01 +00001210 goto FAILED;
1211 }
1212
egdanield803f272015-03-18 13:01:52 -07001213 GL_CALL(GenFramebuffers(1, &idDesc->fTexFBOID));
1214 if (!idDesc->fTexFBOID) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001215 goto FAILED;
1216 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001217
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001218 // If we are using multisampling we will create two FBOS. We render to one and then resolve to
1219 // the texture bound to the other. The exception is the IMG multisample extension. With this
1220 // extension the texture is multisampled when rendered to and then auto-resolves it when it is
1221 // rendered from.
bsalomonb15b4c12014-10-29 12:41:57 -07001222 if (desc.fSampleCnt > 0 && this->glCaps().usesMSAARenderBuffers()) {
egdanield803f272015-03-18 13:01:52 -07001223 GL_CALL(GenFramebuffers(1, &idDesc->fRTFBOID));
bsalomonb15b4c12014-10-29 12:41:57 -07001224 GL_CALL(GenRenderbuffers(1, &idDesc->fMSColorRenderbufferID));
egdanield803f272015-03-18 13:01:52 -07001225 if (!idDesc->fRTFBOID ||
bsalomona11e5fc2015-12-18 07:59:41 -08001226 !idDesc->fMSColorRenderbufferID) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001227 goto FAILED;
1228 }
bsalomon76148af2016-01-12 11:13:47 -08001229 if (!this->glCaps().getRenderbufferFormat(desc.fConfig, &colorRenderbufferFormat)) {
1230 return false;
1231 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001232 } else {
egdanield803f272015-03-18 13:01:52 -07001233 idDesc->fRTFBOID = idDesc->fTexFBOID;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001234 }
1235
egdanield803f272015-03-18 13:01:52 -07001236 // below here we may bind the FBO
Robert Phillips294870f2016-11-11 12:38:40 -05001237 fHWBoundRenderTargetUniqueID.makeInvalid();
egdanield803f272015-03-18 13:01:52 -07001238 if (idDesc->fRTFBOID != idDesc->fTexFBOID) {
bsalomonb15b4c12014-10-29 12:41:57 -07001239 SkASSERT(desc.fSampleCnt > 0);
1240 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, idDesc->fMSColorRenderbufferID));
bsalomon424cc262015-05-22 10:37:30 -07001241 if (!renderbuffer_storage_msaa(*fGLContext,
bsalomonb15b4c12014-10-29 12:41:57 -07001242 desc.fSampleCnt,
bsalomona11e5fc2015-12-18 07:59:41 -08001243 colorRenderbufferFormat,
bsalomonb15b4c12014-10-29 12:41:57 -07001244 desc.fWidth, desc.fHeight)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001245 goto FAILED;
1246 }
egdanield803f272015-03-18 13:01:52 -07001247 fStats.incRenderTargetBinds();
1248 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, idDesc->fRTFBOID));
1249 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon10528f12015-10-14 12:54:52 -07001250 GR_GL_COLOR_ATTACHMENT0,
1251 GR_GL_RENDERBUFFER,
1252 idDesc->fMSColorRenderbufferID));
Brian Salomon0ec981b2017-05-15 13:48:50 -04001253 if (!this->glCaps().isConfigVerifiedColorAttachment(desc.fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001254 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1255 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
1256 goto FAILED;
1257 }
bsalomon424cc262015-05-22 10:37:30 -07001258 fGLContext->caps()->markConfigAsValidColorAttachment(desc.fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001259 }
1260 }
egdanield803f272015-03-18 13:01:52 -07001261 fStats.incRenderTargetBinds();
1262 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, idDesc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001263
bsalomonb15b4c12014-10-29 12:41:57 -07001264 if (this->glCaps().usesImplicitMSAAResolve() && desc.fSampleCnt > 0) {
egdanield803f272015-03-18 13:01:52 -07001265 GL_CALL(FramebufferTexture2DMultisample(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001266 GR_GL_COLOR_ATTACHMENT0,
bsalomon091f60c2015-11-10 11:54:56 -08001267 texInfo.fTarget,
1268 texInfo.fID, 0, desc.fSampleCnt));
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001269 } else {
egdanield803f272015-03-18 13:01:52 -07001270 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001271 GR_GL_COLOR_ATTACHMENT0,
bsalomon091f60c2015-11-10 11:54:56 -08001272 texInfo.fTarget,
1273 texInfo.fID, 0));
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001274 }
Brian Salomon0ec981b2017-05-15 13:48:50 -04001275 if (!this->glCaps().isConfigVerifiedColorAttachment(desc.fConfig)) {
egdanield803f272015-03-18 13:01:52 -07001276 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001277 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
1278 goto FAILED;
1279 }
bsalomon424cc262015-05-22 10:37:30 -07001280 fGLContext->caps()->markConfigAsValidColorAttachment(desc.fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001281 }
1282
1283 return true;
1284
1285FAILED:
bsalomonb15b4c12014-10-29 12:41:57 -07001286 if (idDesc->fMSColorRenderbufferID) {
1287 GL_CALL(DeleteRenderbuffers(1, &idDesc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001288 }
egdanield803f272015-03-18 13:01:52 -07001289 if (idDesc->fRTFBOID != idDesc->fTexFBOID) {
1290 GL_CALL(DeleteFramebuffers(1, &idDesc->fRTFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001291 }
egdanield803f272015-03-18 13:01:52 -07001292 if (idDesc->fTexFBOID) {
1293 GL_CALL(DeleteFramebuffers(1, &idDesc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001294 }
1295 return false;
1296}
1297
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001298// good to set a break-point here to know when createTexture fails
Robert Phillips67d52cf2017-06-05 13:38:13 -04001299static sk_sp<GrTexture> return_null_texture() {
mtklein@google.com330313a2013-08-22 15:37:26 +00001300// SkDEBUGFAIL("null texture");
halcanary96fcdcc2015-08-27 07:41:13 -07001301 return nullptr;
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001302}
1303
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +00001304#if 0 && defined(SK_DEBUG)
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001305static size_t as_size_t(int x) {
1306 return x;
1307}
1308#endif
1309
cblume55f2d2d2016-02-26 13:20:48 -08001310static void set_initial_texture_params(const GrGLInterface* interface,
1311 const GrGLTextureInfo& info,
1312 GrGLTexture::TexParams* initialTexParams) {
1313 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
1314 // drivers have a bug where an FBO won't be complete if it includes a
1315 // texture that is not mipmap complete (considering the filter in use).
1316 // we only set a subset here so invalidate first
1317 initialTexParams->invalidate();
1318 initialTexParams->fMinFilter = GR_GL_NEAREST;
1319 initialTexParams->fMagFilter = GR_GL_NEAREST;
1320 initialTexParams->fWrapS = GR_GL_CLAMP_TO_EDGE;
1321 initialTexParams->fWrapT = GR_GL_CLAMP_TO_EDGE;
1322 GR_GL_CALL(interface, TexParameteri(info.fTarget,
1323 GR_GL_TEXTURE_MAG_FILTER,
1324 initialTexParams->fMagFilter));
1325 GR_GL_CALL(interface, TexParameteri(info.fTarget,
1326 GR_GL_TEXTURE_MIN_FILTER,
1327 initialTexParams->fMinFilter));
1328 GR_GL_CALL(interface, TexParameteri(info.fTarget,
1329 GR_GL_TEXTURE_WRAP_S,
1330 initialTexParams->fWrapS));
1331 GR_GL_CALL(interface, TexParameteri(info.fTarget,
1332 GR_GL_TEXTURE_WRAP_T,
1333 initialTexParams->fWrapT));
1334}
1335
Robert Phillips67d52cf2017-06-05 13:38:13 -04001336sk_sp<GrTexture> GrGLGpu::onCreateTexture(const GrSurfaceDesc& desc,
1337 SkBudgeted budgeted,
1338 const SkTArray<GrMipLevel>& origTexels) {
bsalomon@google.com8a70eef2013-03-19 13:58:55 +00001339 // We fail if the MSAA was requested and is not available.
1340 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType() && desc.fSampleCnt) {
tfarina38406c82014-10-31 07:11:12 -07001341 //SkDebugf("MSAA RT requested but not supported on this platform.");
bsalomon@google.com8a70eef2013-03-19 13:58:55 +00001342 return return_null_texture();
1343 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001344
Brian Salomond17b4a62017-05-23 16:53:47 -04001345 bool performClear = (desc.fFlags & kPerformInitialClear_GrSurfaceFlag);
1346 const SkTArray<GrMipLevel>* texels = &origTexels;
1347
1348 SkTArray<GrMipLevel> zeroLevels;
1349 std::unique_ptr<uint8_t[]> zeros;
1350 // TODO: remove the GrPixelConfigIsSint test. This is here because we have yet to add support
1351 // for glClearBuffer* which must be used instead of glClearColor/glClear for integer FBO
1352 // attachments.
1353 if (performClear && !this->glCaps().clearTextureSupport() &&
1354 (!this->glCaps().canConfigBeFBOColorAttachment(desc.fConfig) ||
1355 GrPixelConfigIsSint(desc.fConfig))) {
1356 size_t rowSize = GrBytesPerPixel(desc.fConfig) * desc.fWidth;
1357 size_t size = rowSize * desc.fHeight;
1358 zeros.reset(new uint8_t[size]);
1359 memset(zeros.get(), 0, size);
1360 zeroLevels.push_back(GrMipLevel{zeros.get(), 0});
1361 texels = &zeroLevels;
1362 performClear = false;
1363 }
1364
Brian Salomond34edf32017-05-19 15:45:48 -04001365 bool isRenderTarget = SkToBool(desc.fFlags & kRenderTarget_GrSurfaceFlag);
reed@google.comac10a2d2010-12-22 21:39:39 +00001366
bsalomonb15b4c12014-10-29 12:41:57 -07001367 GrGLTexture::IDDesc idDesc;
kkinnunen2e6055b2016-04-22 01:48:29 -07001368 idDesc.fOwnership = GrBackendObjectOwnership::kOwned;
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001369 GrGLTexture::TexParams initialTexParams;
Brian Salomond17b4a62017-05-23 16:53:47 -04001370 if (!this->createTextureImpl(desc, &idDesc.fInfo, isRenderTarget, &initialTexParams, *texels)) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001371 return return_null_texture();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001372 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001373
Robert Phillipsd6214d42016-11-07 08:23:48 -05001374 bool wasMipMapDataProvided = false;
Brian Salomond17b4a62017-05-23 16:53:47 -04001375 if (texels->count() > 1) {
Robert Phillipsd6214d42016-11-07 08:23:48 -05001376 wasMipMapDataProvided = true;
1377 }
1378
Robert Phillips67d52cf2017-06-05 13:38:13 -04001379 sk_sp<GrGLTexture> tex;
Brian Salomond34edf32017-05-19 15:45:48 -04001380 if (isRenderTarget) {
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001381 // unbind the texture from the texture unit before binding it to the frame buffer
bsalomon091f60c2015-11-10 11:54:56 -08001382 GL_CALL(BindTexture(idDesc.fInfo.fTarget, 0));
bsalomon5236cf42015-01-14 10:42:08 -08001383 GrGLRenderTarget::IDDesc rtIDDesc;
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001384
kkinnunen2e6055b2016-04-22 01:48:29 -07001385 if (!this->createRenderTargetObjects(desc, idDesc.fInfo, &rtIDDesc)) {
bsalomon091f60c2015-11-10 11:54:56 -08001386 GL_CALL(DeleteTextures(1, &idDesc.fInfo.fID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001387 return return_null_texture();
1388 }
Robert Phillips67d52cf2017-06-05 13:38:13 -04001389 tex = sk_make_sp<GrGLTextureRenderTarget>(this, budgeted, desc, idDesc, rtIDDesc,
1390 wasMipMapDataProvided);
Brian Salomon9bada542017-06-12 12:09:30 -04001391 tex->baseLevelWasBoundToFBO();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001392 } else {
Robert Phillips67d52cf2017-06-05 13:38:13 -04001393 tex = sk_make_sp<GrGLTexture>(this, budgeted, desc, idDesc, wasMipMapDataProvided);
reed@google.comac10a2d2010-12-22 21:39:39 +00001394 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001395 tex->setCachedTexParams(initialTexParams, this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00001396#ifdef TRACE_TEXTURE_CREATION
tfarina38406c82014-10-31 07:11:12 -07001397 SkDebugf("--- new texture [%d] size=(%d %d) config=%d\n",
brianosmanc4459f62016-07-19 08:02:20 -07001398 idDesc.fInfo.fID, desc.fWidth, desc.fHeight, desc.fConfig);
reed@google.comac10a2d2010-12-22 21:39:39 +00001399#endif
Brian Salomond17b4a62017-05-23 16:53:47 -04001400 if (tex && performClear) {
1401 if (this->glCaps().clearTextureSupport()) {
1402 GrGLenum format = GrPixelConfigIsSint(tex->config()) ? GR_GL_RGBA_INTEGER : GR_GL_RGBA;
1403 static constexpr uint32_t kZero = 0;
1404 GL_CALL(ClearTexImage(tex->textureID(), 0, format, GR_GL_UNSIGNED_BYTE, &kZero));
1405 } else {
1406 SkASSERT(!GrPixelConfigIsSint(desc.fConfig));
1407 GrGLIRect viewport;
Robert Phillips67d52cf2017-06-05 13:38:13 -04001408 this->bindSurfaceFBOForPixelOps(tex.get(), GR_GL_FRAMEBUFFER, &viewport,
1409 kDst_TempFBOTarget);
Brian Salomond17b4a62017-05-23 16:53:47 -04001410 this->disableScissor();
1411 this->disableWindowRectangles();
1412 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
1413 fHWWriteToColor = kYes_TriState;
1414 GL_CALL(ClearColor(0, 0, 0, 0));
1415 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
Robert Phillips67d52cf2017-06-05 13:38:13 -04001416 this->unbindTextureFBOForPixelOps(GR_GL_FRAMEBUFFER, tex.get());
Brian Salomond17b4a62017-05-23 16:53:47 -04001417 fHWBoundRenderTargetUniqueID.makeInvalid();
1418 }
1419 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001420 return tex;
1421}
1422
1423namespace {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001424
egdaniel8dc7c3a2015-04-16 11:22:42 -07001425const GrGLuint kUnknownBitCount = GrGLStencilAttachment::kUnknownBitCount;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001426
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001427void inline get_stencil_rb_sizes(const GrGLInterface* gl,
egdaniel8dc7c3a2015-04-16 11:22:42 -07001428 GrGLStencilAttachment::Format* format) {
sugoi@google.com6ba0b0f2013-05-03 13:52:32 +00001429
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001430 // we shouldn't ever know one size and not the other
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001431 SkASSERT((kUnknownBitCount == format->fStencilBits) ==
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001432 (kUnknownBitCount == format->fTotalBits));
1433 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001434 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001435 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1436 (GrGLint*)&format->fStencilBits);
1437 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001438 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001439 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1440 (GrGLint*)&format->fTotalBits);
1441 format->fTotalBits += format->fStencilBits;
1442 } else {
1443 format->fTotalBits = format->fStencilBits;
1444 }
1445 }
1446}
1447}
1448
egdanielff1d5472015-09-10 08:37:20 -07001449int GrGLGpu::getCompatibleStencilIndex(GrPixelConfig config) {
bsalomon100b8f82015-10-28 08:37:44 -07001450 static const int kSize = 16;
bsalomon926cb022015-12-17 18:15:11 -08001451 SkASSERT(this->caps()->isConfigRenderable(config, false));
bsalomon30447372015-12-21 09:03:05 -08001452 if (!this->glCaps().hasStencilFormatBeenDeterminedForConfig(config)) {
1453 // Default to unsupported, set this if we find a stencil format that works.
1454 int firstWorkingStencilFormatIndex = -1;
egdanielff1d5472015-09-10 08:37:20 -07001455 // Create color texture
kkinnunen546eb5c2015-12-11 00:05:33 -08001456 GrGLuint colorID = 0;
egdanielff1d5472015-09-10 08:37:20 -07001457 GL_CALL(GenTextures(1, &colorID));
1458 this->setScratchTextureUnit();
1459 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, colorID));
1460 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1461 GR_GL_TEXTURE_MAG_FILTER,
1462 GR_GL_NEAREST));
1463 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1464 GR_GL_TEXTURE_MIN_FILTER,
1465 GR_GL_NEAREST));
1466 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1467 GR_GL_TEXTURE_WRAP_S,
1468 GR_GL_CLAMP_TO_EDGE));
1469 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1470 GR_GL_TEXTURE_WRAP_T,
1471 GR_GL_CLAMP_TO_EDGE));
1472
bsalomon76148af2016-01-12 11:13:47 -08001473 GrGLenum internalFormat;
1474 GrGLenum externalFormat;
1475 GrGLenum externalType;
1476 if (!this->glCaps().getTexImageFormats(config, config, &internalFormat, &externalFormat,
1477 &externalType)) {
1478 return false;
1479 }
egdanielff1d5472015-09-10 08:37:20 -07001480 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1481 GL_ALLOC_CALL(this->glInterface(), TexImage2D(GR_GL_TEXTURE_2D,
bsalomon926cb022015-12-17 18:15:11 -08001482 0,
bsalomon76148af2016-01-12 11:13:47 -08001483 internalFormat,
bsalomon100b8f82015-10-28 08:37:44 -07001484 kSize,
1485 kSize,
egdanielff1d5472015-09-10 08:37:20 -07001486 0,
bsalomon76148af2016-01-12 11:13:47 -08001487 externalFormat,
1488 externalType,
egdanielff1d5472015-09-10 08:37:20 -07001489 NULL));
bsalomon30447372015-12-21 09:03:05 -08001490 if (GR_GL_NO_ERROR != CHECK_ALLOC_ERROR(this->glInterface())) {
egdanielff1d5472015-09-10 08:37:20 -07001491 GL_CALL(DeleteTextures(1, &colorID));
bsalomon30447372015-12-21 09:03:05 -08001492 return -1;
egdanielff1d5472015-09-10 08:37:20 -07001493 }
1494
1495 // unbind the texture from the texture unit before binding it to the frame buffer
1496 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, 0));
1497
1498 // Create Framebuffer
kkinnunen546eb5c2015-12-11 00:05:33 -08001499 GrGLuint fb = 0;
egdanielff1d5472015-09-10 08:37:20 -07001500 GL_CALL(GenFramebuffers(1, &fb));
egdanielec00d942015-09-14 12:56:10 -07001501 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, fb));
Robert Phillips294870f2016-11-11 12:38:40 -05001502 fHWBoundRenderTargetUniqueID.makeInvalid();
egdanielff1d5472015-09-10 08:37:20 -07001503 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
1504 GR_GL_COLOR_ATTACHMENT0,
1505 GR_GL_TEXTURE_2D,
1506 colorID,
1507 0));
bsalomon30447372015-12-21 09:03:05 -08001508 GrGLuint sbRBID = 0;
1509 GL_CALL(GenRenderbuffers(1, &sbRBID));
egdanielff1d5472015-09-10 08:37:20 -07001510
1511 // look over formats till I find a compatible one
1512 int stencilFmtCnt = this->glCaps().stencilFormats().count();
bsalomon30447372015-12-21 09:03:05 -08001513 if (sbRBID) {
egdanielff1d5472015-09-10 08:37:20 -07001514 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbRBID));
bsalomon30447372015-12-21 09:03:05 -08001515 for (int i = 0; i < stencilFmtCnt && sbRBID; ++i) {
1516 const GrGLCaps::StencilFormat& sFmt = this->glCaps().stencilFormats()[i];
1517 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1518 GL_ALLOC_CALL(this->glInterface(), RenderbufferStorage(GR_GL_RENDERBUFFER,
1519 sFmt.fInternalFormat,
1520 kSize, kSize));
1521 if (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(this->glInterface())) {
egdanielff1d5472015-09-10 08:37:20 -07001522 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon30447372015-12-21 09:03:05 -08001523 GR_GL_STENCIL_ATTACHMENT,
egdanielff1d5472015-09-10 08:37:20 -07001524 GR_GL_RENDERBUFFER, sbRBID));
bsalomon30447372015-12-21 09:03:05 -08001525 if (sFmt.fPacked) {
1526 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1527 GR_GL_DEPTH_ATTACHMENT,
1528 GR_GL_RENDERBUFFER, sbRBID));
1529 } else {
1530 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1531 GR_GL_DEPTH_ATTACHMENT,
1532 GR_GL_RENDERBUFFER, 0));
1533 }
1534 GrGLenum status;
1535 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1536 if (status == GR_GL_FRAMEBUFFER_COMPLETE) {
1537 firstWorkingStencilFormatIndex = i;
1538 break;
1539 }
egdanielff1d5472015-09-10 08:37:20 -07001540 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1541 GR_GL_STENCIL_ATTACHMENT,
1542 GR_GL_RENDERBUFFER, 0));
1543 if (sFmt.fPacked) {
1544 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1545 GR_GL_DEPTH_ATTACHMENT,
1546 GR_GL_RENDERBUFFER, 0));
1547 }
egdanielff1d5472015-09-10 08:37:20 -07001548 }
1549 }
bsalomon30447372015-12-21 09:03:05 -08001550 GL_CALL(DeleteRenderbuffers(1, &sbRBID));
egdanielff1d5472015-09-10 08:37:20 -07001551 }
1552 GL_CALL(DeleteTextures(1, &colorID));
egdanielff1d5472015-09-10 08:37:20 -07001553 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, 0));
1554 GL_CALL(DeleteFramebuffers(1, &fb));
bsalomon30447372015-12-21 09:03:05 -08001555 fGLContext->caps()->setStencilFormatIndexForConfig(config, firstWorkingStencilFormatIndex);
egdanielff1d5472015-09-10 08:37:20 -07001556 }
bsalomon30447372015-12-21 09:03:05 -08001557 return this->glCaps().getStencilFormatIndexForConfig(config);
egdanielff1d5472015-09-10 08:37:20 -07001558}
1559
erikchen9a1ed5d2016-02-10 16:32:34 -08001560bool GrGLGpu::createTextureImpl(const GrSurfaceDesc& desc, GrGLTextureInfo* info,
cblume55f2d2d2016-02-26 13:20:48 -08001561 bool renderTarget, GrGLTexture::TexParams* initialTexParams,
1562 const SkTArray<GrMipLevel>& texels) {
erikchen9a1ed5d2016-02-10 16:32:34 -08001563 info->fID = 0;
1564 info->fTarget = GR_GL_TEXTURE_2D;
1565 GL_CALL(GenTextures(1, &(info->fID)));
1566
1567 if (!info->fID) {
1568 return false;
1569 }
1570
1571 this->setScratchTextureUnit();
1572 GL_CALL(BindTexture(info->fTarget, info->fID));
1573
1574 if (renderTarget && this->glCaps().textureUsageSupport()) {
1575 // provides a hint about how this texture will be used
1576 GL_CALL(TexParameteri(info->fTarget,
1577 GR_GL_TEXTURE_USAGE,
1578 GR_GL_FRAMEBUFFER_ATTACHMENT));
1579 }
1580
cblume55f2d2d2016-02-26 13:20:48 -08001581 if (info) {
1582 set_initial_texture_params(this->glInterface(), *info, initialTexParams);
1583 }
Brian Salomond1eaf492017-05-18 10:02:08 -04001584 if (!this->uploadTexData(desc.fConfig, desc.fWidth, desc.fHeight, desc.fOrigin, info->fTarget,
1585 kNewTexture_UploadType, 0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
1586 texels)) {
erikchen9a1ed5d2016-02-10 16:32:34 -08001587 GL_CALL(DeleteTextures(1, &(info->fID)));
1588 return false;
1589 }
1590 return true;
1591}
1592
egdanielec00d942015-09-14 12:56:10 -07001593GrStencilAttachment* GrGLGpu::createStencilAttachmentForRenderTarget(const GrRenderTarget* rt,
Brian Salomond1eaf492017-05-18 10:02:08 -04001594 int width, int height) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001595 SkASSERT(width >= rt->width());
1596 SkASSERT(height >= rt->height());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001597
vbuzinovdded6962015-06-12 08:59:45 -07001598 int samples = rt->numStencilSamples();
egdaniel8dc7c3a2015-04-16 11:22:42 -07001599 GrGLStencilAttachment::IDDesc sbDesc;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001600
egdanielff1d5472015-09-10 08:37:20 -07001601 int sIdx = this->getCompatibleStencilIndex(rt->config());
bsalomon62a627b2015-12-17 09:50:47 -08001602 if (sIdx < 0) {
egdanielec00d942015-09-14 12:56:10 -07001603 return nullptr;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001604 }
egdanielff1d5472015-09-10 08:37:20 -07001605
1606 if (!sbDesc.fRenderbufferID) {
1607 GL_CALL(GenRenderbuffers(1, &sbDesc.fRenderbufferID));
1608 }
1609 if (!sbDesc.fRenderbufferID) {
egdanielec00d942015-09-14 12:56:10 -07001610 return nullptr;
egdanielff1d5472015-09-10 08:37:20 -07001611 }
1612 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbDesc.fRenderbufferID));
1613 const GrGLCaps::StencilFormat& sFmt = this->glCaps().stencilFormats()[sIdx];
1614 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1615 // we do this "if" so that we don't call the multisample
1616 // version on a GL that doesn't have an MSAA extension.
1617 if (samples > 0) {
1618 SkAssertResult(renderbuffer_storage_msaa(*fGLContext,
1619 samples,
1620 sFmt.fInternalFormat,
1621 width, height));
1622 } else {
1623 GL_ALLOC_CALL(this->glInterface(), RenderbufferStorage(GR_GL_RENDERBUFFER,
1624 sFmt.fInternalFormat,
1625 width, height));
Brian Salomon0ec981b2017-05-15 13:48:50 -04001626 SkASSERT(GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(this->glInterface()));
egdanielff1d5472015-09-10 08:37:20 -07001627 }
1628 fStats.incStencilAttachmentCreates();
1629 // After sized formats we attempt an unsized format and take
1630 // whatever sizes GL gives us. In that case we query for the size.
1631 GrGLStencilAttachment::Format format = sFmt;
1632 get_stencil_rb_sizes(this->glInterface(), &format);
egdanielec00d942015-09-14 12:56:10 -07001633 GrGLStencilAttachment* stencil = new GrGLStencilAttachment(this,
1634 sbDesc,
1635 width,
1636 height,
1637 samples,
1638 format);
1639 return stencil;
reed@google.comac10a2d2010-12-22 21:39:39 +00001640}
1641
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001642////////////////////////////////////////////////////////////////////////////////
1643
jvanverth73063dc2015-12-03 09:15:47 -08001644// GL_STREAM_DRAW triggers an optimization in Chromium's GPU process where a client's vertex buffer
1645// objects are implemented as client-side-arrays on tile-deferred architectures.
1646#define DYNAMIC_USAGE_PARAM GR_GL_STREAM_DRAW
1647
cdaltone2e71c22016-04-07 18:13:29 -07001648GrBuffer* GrGLGpu::onCreateBuffer(size_t size, GrBufferType intendedType,
cdalton1bf3e712016-04-19 10:00:02 -07001649 GrAccessPattern accessPattern, const void* data) {
1650 return GrGLBuffer::Create(this, size, intendedType, accessPattern, data);
jvanverth73063dc2015-12-03 09:15:47 -08001651}
1652
Robert Phillipse3302df2017-04-24 07:31:02 -04001653
1654std::unique_ptr<OpAllocator> GrGLGpu::onCreateInstancedRenderingAllocator() {
1655 return std::unique_ptr<OpAllocator>(new GLOpAllocator(this->caps()));
1656}
1657
csmartdaltone0d36292016-07-29 08:14:20 -07001658InstancedRendering* GrGLGpu::onCreateInstancedRendering() {
1659 return new GLInstancedRendering(this);
1660}
1661
bsalomon3e791242014-12-17 13:43:13 -08001662void GrGLGpu::flushScissor(const GrScissorState& scissorState,
joshualitt77b13072014-10-27 14:51:01 -07001663 const GrGLIRect& rtViewport,
1664 GrSurfaceOrigin rtOrigin) {
robertphillipse85a32d2015-02-10 08:16:55 -08001665 if (scissorState.enabled()) {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001666 GrGLIRect scissor;
bsalomonb0bd4f62014-09-03 07:19:50 -07001667 scissor.setRelativeTo(rtViewport,
robertphillipse85a32d2015-02-10 08:16:55 -08001668 scissorState.rect().fLeft,
1669 scissorState.rect().fTop,
1670 scissorState.rect().width(),
1671 scissorState.rect().height(),
bsalomonb0bd4f62014-09-03 07:19:50 -07001672 rtOrigin);
bsalomon@google.coma3201942012-06-21 19:58:20 +00001673 // if the scissor fully contains the viewport then we fall through and
1674 // disable the scissor test.
bsalomonb0bd4f62014-09-03 07:19:50 -07001675 if (!scissor.contains(rtViewport)) {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001676 if (fHWScissorSettings.fRect != scissor) {
1677 scissor.pushToGLScissor(this->glInterface());
1678 fHWScissorSettings.fRect = scissor;
1679 }
1680 if (kYes_TriState != fHWScissorSettings.fEnabled) {
1681 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1682 fHWScissorSettings.fEnabled = kYes_TriState;
1683 }
1684 return;
1685 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001686 }
joshualitt77b13072014-10-27 14:51:01 -07001687
1688 // See fall through note above
1689 this->disableScissor();
1690}
1691
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001692void GrGLGpu::flushWindowRectangles(const GrWindowRectsState& windowState,
1693 const GrGLRenderTarget* rt) {
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001694#ifndef USE_NSIGHT
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001695 typedef GrWindowRectsState::Mode Mode;
1696 SkASSERT(!windowState.enabled() || rt->renderFBOID()); // Window rects can't be used on-screen.
1697 SkASSERT(windowState.numWindows() <= this->caps()->maxWindowRectangles());
csmartdalton28341fa2016-08-17 10:00:21 -07001698
1699 if (!this->caps()->maxWindowRectangles() ||
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001700 fHWWindowRectsState.knownEqualTo(rt->origin(), rt->getViewport(), windowState)) {
csmartdalton28341fa2016-08-17 10:00:21 -07001701 return;
csmartdalton28341fa2016-08-17 10:00:21 -07001702 }
1703
csmartdalton7535f412016-08-23 06:51:00 -07001704 // This is purely a workaround for a spurious warning generated by gcc. Otherwise the above
1705 // assert would be sufficient. https://gcc.gnu.org/bugzilla/show_bug.cgi?id=5912
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001706 int numWindows = SkTMin(windowState.numWindows(), int(GrWindowRectangles::kMaxWindows));
1707 SkASSERT(windowState.numWindows() == numWindows);
csmartdalton7535f412016-08-23 06:51:00 -07001708
csmartdalton28341fa2016-08-17 10:00:21 -07001709 GrGLIRect glwindows[GrWindowRectangles::kMaxWindows];
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001710 const SkIRect* skwindows = windowState.windows().data();
csmartdalton7535f412016-08-23 06:51:00 -07001711 for (int i = 0; i < numWindows; ++i) {
Brian Salomon9a767722017-03-13 17:57:28 -04001712 glwindows[i].setRelativeTo(rt->getViewport(), skwindows[i], rt->origin());
csmartdalton28341fa2016-08-17 10:00:21 -07001713 }
1714
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001715 GrGLenum glmode = (Mode::kExclusive == windowState.mode()) ? GR_GL_EXCLUSIVE : GR_GL_INCLUSIVE;
csmartdalton7535f412016-08-23 06:51:00 -07001716 GL_CALL(WindowRectangles(glmode, numWindows, glwindows->asInts()));
csmartdalton28341fa2016-08-17 10:00:21 -07001717
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001718 fHWWindowRectsState.set(rt->origin(), rt->getViewport(), windowState);
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001719#endif
csmartdalton28341fa2016-08-17 10:00:21 -07001720}
1721
1722void GrGLGpu::disableWindowRectangles() {
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001723#ifndef USE_NSIGHT
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001724 if (!this->caps()->maxWindowRectangles() || fHWWindowRectsState.knownDisabled()) {
csmartdalton28341fa2016-08-17 10:00:21 -07001725 return;
1726 }
1727 GL_CALL(WindowRectangles(GR_GL_EXCLUSIVE, 0, nullptr));
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001728 fHWWindowRectsState.setDisabled();
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001729#endif
csmartdalton28341fa2016-08-17 10:00:21 -07001730}
1731
ethannicholas28ef4452016-03-25 09:26:03 -07001732void GrGLGpu::flushMinSampleShading(float minSampleShading) {
1733 if (fHWMinSampleShading != minSampleShading) {
1734 if (minSampleShading > 0.0) {
1735 GL_CALL(Enable(GR_GL_SAMPLE_SHADING));
1736 GL_CALL(MinSampleShading(minSampleShading));
1737 }
1738 else {
1739 GL_CALL(Disable(GR_GL_SAMPLE_SHADING));
1740 }
1741 fHWMinSampleShading = minSampleShading;
1742 }
1743}
1744
bsalomon2eda5b32016-09-21 10:53:24 -07001745bool GrGLGpu::flushGLState(const GrPipeline& pipeline, const GrPrimitiveProcessor& primProc,
1746 bool willDrawPoints) {
Hal Canary144caf52016-11-07 17:57:18 -05001747 sk_sp<GrGLProgram> program(fProgramCache->refProgram(this, pipeline, primProc, willDrawPoints));
brianosman33f6b3f2016-06-02 05:49:21 -07001748 if (!program) {
1749 GrCapsDebugf(this->caps(), "Failed to create program!\n");
1750 return false;
1751 }
1752
1753 program->generateMipmaps(primProc, pipeline);
1754
egdaniel080e6732014-12-22 07:35:52 -08001755 GrXferProcessor::BlendInfo blendInfo;
egdaniel0e1853c2016-03-17 11:35:45 -07001756 pipeline.getXferProcessor().getBlendInfo(&blendInfo);
egdaniel080e6732014-12-22 07:35:52 -08001757
egdaniel080e6732014-12-22 07:35:52 -08001758 this->flushColorWrite(blendInfo.fWriteColor);
ethannicholas28ef4452016-03-25 09:26:03 -07001759 this->flushMinSampleShading(primProc.getSampleShading());
bsalomonbc3d0de2014-12-15 13:45:03 -08001760
bsalomon6df86402015-06-01 10:41:49 -07001761 GrGLuint programID = program->programID();
bsalomon1f78c0a2014-12-17 09:43:13 -08001762 if (fHWProgramID != programID) {
1763 GL_CALL(UseProgram(programID));
1764 fHWProgramID = programID;
1765 }
1766
egdanield803f272015-03-18 13:01:52 -07001767 if (blendInfo.fWriteColor) {
bsalomon7f9b2e42016-01-12 13:29:26 -08001768 // Swizzle the blend to match what the shader will output.
Brian Salomon1edc5b92016-11-29 13:43:46 -05001769 const GrSwizzle& swizzle = this->caps()->shaderCaps()->configOutputSwizzle(
egdaniel0e1853c2016-03-17 11:35:45 -07001770 pipeline.getRenderTarget()->config());
bsalomon7f9b2e42016-01-12 13:29:26 -08001771 this->flushBlend(blendInfo, swizzle);
egdanield803f272015-03-18 13:01:52 -07001772 }
bsalomon1f78c0a2014-12-17 09:43:13 -08001773
cdalton74b8d322016-04-11 14:47:28 -07001774 program->setData(primProc, pipeline);
bsalomon1f78c0a2014-12-17 09:43:13 -08001775
brianosman898235c2016-04-06 07:38:23 -07001776 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(pipeline.getRenderTarget());
csmartdaltonc633abb2016-11-01 08:55:55 -07001777 GrStencilSettings stencil;
1778 if (pipeline.isStencilEnabled()) {
1779 // TODO: attach stencil and create settings during render target flush.
1780 SkASSERT(glRT->renderTargetPriv().getStencilAttachment());
1781 stencil.reset(*pipeline.getUserStencil(), pipeline.hasStencilClip(),
1782 glRT->renderTargetPriv().numStencilBits());
1783 }
1784 this->flushStencil(stencil);
egdaniel8dd688b2015-01-22 10:16:09 -08001785 this->flushScissor(pipeline.getScissorState(), glRT->getViewport(), glRT->origin());
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001786 this->flushWindowRectangles(pipeline.getWindowRectsState(), glRT);
csmartdaltondabc91b2016-11-15 14:26:27 -07001787 this->flushHWAAState(glRT, pipeline.isHWAntialiasState(), !stencil.isDisabled());
bsalomonbc3d0de2014-12-15 13:45:03 -08001788
1789 // This must come after textures are flushed because a texture may need
egdanield803f272015-03-18 13:01:52 -07001790 // to be msaa-resolved (which will modify bound FBO state).
brianosman64d094d2016-03-25 06:01:59 -07001791 this->flushRenderTarget(glRT, nullptr, pipeline.getDisableOutputConversionToSRGB());
bsalomonbc3d0de2014-12-15 13:45:03 -08001792
1793 return true;
1794}
1795
joshualitt873ad0e2015-01-20 09:08:51 -08001796void GrGLGpu::setupGeometry(const GrPrimitiveProcessor& primProc,
Chris Daltonff926502017-05-03 14:36:54 -04001797 const GrBuffer* indexBuffer,
1798 const GrBuffer* vertexBuffer,
Chris Dalton1d616352017-05-31 12:51:23 -06001799 int baseVertex,
1800 const GrBuffer* instanceBuffer,
1801 int baseInstance) {
cdaltone2e71c22016-04-07 18:13:29 -07001802 GrGLAttribArrayState* attribState;
Chris Daltonff926502017-05-03 14:36:54 -04001803 if (indexBuffer) {
Chris Dalton1d616352017-05-31 12:51:23 -06001804 SkASSERT(indexBuffer && !indexBuffer->isMapped());
Chris Daltonff926502017-05-03 14:36:54 -04001805 attribState = fHWVertexArrayState.bindInternalVertexArray(this, indexBuffer);
cdaltone2e71c22016-04-07 18:13:29 -07001806 } else {
1807 attribState = fHWVertexArrayState.bindInternalVertexArray(this);
bsalomonbc3d0de2014-12-15 13:45:03 -08001808 }
bsalomonbc3d0de2014-12-15 13:45:03 -08001809
Chris Dalton1d616352017-05-31 12:51:23 -06001810 struct {
1811 const GrBuffer* fBuffer;
1812 int fStride;
1813 size_t fBufferOffset;
1814 } bindings[2];
Chris Dalton8e45b4f2017-05-05 14:00:56 -04001815
Chris Dalton1d616352017-05-31 12:51:23 -06001816 if (int vertexStride = primProc.getVertexStride()) {
1817 SkASSERT(vertexBuffer && !vertexBuffer->isMapped());
1818 bindings[0].fBuffer = vertexBuffer;
1819 bindings[0].fStride = vertexStride;
1820 bindings[0].fBufferOffset = vertexBuffer->baseOffset() + baseVertex * vertexStride;
1821 }
1822 if (int instanceStride = primProc.getInstanceStride()) {
1823 SkASSERT(instanceBuffer && !instanceBuffer->isMapped());
1824 bindings[1].fBuffer = instanceBuffer;
1825 bindings[1].fStride = instanceStride;
1826 bindings[1].fBufferOffset = instanceBuffer->baseOffset() + baseInstance * instanceStride;
1827 }
bsalomonbc3d0de2014-12-15 13:45:03 -08001828
Chris Dalton1d616352017-05-31 12:51:23 -06001829 int numAttribs = primProc.numAttribs();
1830 attribState->enableVertexArrays(this, numAttribs);
bsalomonbc3d0de2014-12-15 13:45:03 -08001831
Chris Dalton1d616352017-05-31 12:51:23 -06001832 for (int i = 0; i < numAttribs; ++i) {
1833 using InputRate = GrPrimitiveProcessor::Attribute::InputRate;
1834 const GrGeometryProcessor::Attribute& attrib = primProc.getAttrib(i);
1835 const int divisor = InputRate::kPerInstance == attrib.fInputRate ? 1 : 0;
1836 const auto& binding = bindings[divisor];
1837 attribState->set(this, i, binding.fBuffer, attrib.fType, binding.fStride,
1838 binding.fBufferOffset + attrib.fOffsetInRecord, divisor);
bsalomonbc3d0de2014-12-15 13:45:03 -08001839 }
1840}
1841
csmartdalton485a1202016-07-13 10:16:32 -07001842GrGLenum GrGLGpu::bindBuffer(GrBufferType type, const GrBuffer* buffer) {
joshualitt93316b92015-10-23 09:08:08 -07001843 this->handleDirtyContext();
cdaltondeacc972016-04-06 14:26:33 -07001844
cdaltone2e71c22016-04-07 18:13:29 -07001845 // Index buffer state is tied to the vertex array.
1846 if (kIndex_GrBufferType == type) {
1847 this->bindVertexArray(0);
cdaltondeacc972016-04-06 14:26:33 -07001848 }
cdaltone2e71c22016-04-07 18:13:29 -07001849
1850 SkASSERT(type >= 0 && type <= kLast_GrBufferType);
1851 auto& bufferState = fHWBufferState[type];
1852
robertphillips8abb3702016-08-31 14:04:06 -07001853 if (buffer->uniqueID() != bufferState.fBoundBufferUniqueID) {
csmartdalton485a1202016-07-13 10:16:32 -07001854 if (buffer->isCPUBacked()) {
1855 if (!bufferState.fBufferZeroKnownBound) {
1856 GL_CALL(BindBuffer(bufferState.fGLTarget, 0));
1857 }
1858 } else {
1859 const GrGLBuffer* glBuffer = static_cast<const GrGLBuffer*>(buffer);
1860 GL_CALL(BindBuffer(bufferState.fGLTarget, glBuffer->bufferID()));
cdaltone2e71c22016-04-07 18:13:29 -07001861 }
csmartdalton485a1202016-07-13 10:16:32 -07001862 bufferState.fBufferZeroKnownBound = buffer->isCPUBacked();
robertphillips8abb3702016-08-31 14:04:06 -07001863 bufferState.fBoundBufferUniqueID = buffer->uniqueID();
cdaltone2e71c22016-04-07 18:13:29 -07001864 }
1865
1866 return bufferState.fGLTarget;
joshualitt93316b92015-10-23 09:08:08 -07001867}
1868
cdalton74b8d322016-04-11 14:47:28 -07001869void GrGLGpu::notifyBufferReleased(const GrGLBuffer* buffer) {
1870 if (buffer->hasAttachedToTexture()) {
1871 // Detach this buffer from any textures to ensure the underlying memory is freed.
Robert Phillips294870f2016-11-11 12:38:40 -05001872 GrGpuResource::UniqueID uniqueID = buffer->uniqueID();
cdalton74b8d322016-04-11 14:47:28 -07001873 for (int i = fHWMaxUsedBufferTextureUnit; i >= 0; --i) {
1874 auto& buffTex = fHWBufferTextures[i];
1875 if (uniqueID != buffTex.fAttachedBufferUniqueID) {
1876 continue;
1877 }
1878 if (i == fHWMaxUsedBufferTextureUnit) {
1879 --fHWMaxUsedBufferTextureUnit;
1880 }
1881
1882 this->setTextureUnit(i);
1883 if (!buffTex.fKnownBound) {
1884 SkASSERT(buffTex.fTextureID);
1885 GL_CALL(BindTexture(GR_GL_TEXTURE_BUFFER, buffTex.fTextureID));
1886 buffTex.fKnownBound = true;
1887 }
1888 GL_CALL(TexBuffer(GR_GL_TEXTURE_BUFFER,
1889 this->glCaps().configSizedInternalFormat(buffTex.fTexelConfig), 0));
1890 }
1891 }
1892}
1893
bsalomon861e1032014-12-16 07:33:49 -08001894void GrGLGpu::disableScissor() {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001895 if (kNo_TriState != fHWScissorSettings.fEnabled) {
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001896 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.coma3201942012-06-21 19:58:20 +00001897 fHWScissorSettings.fEnabled = kNo_TriState;
1898 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001899 }
1900}
1901
csmartdalton29df7602016-08-31 11:55:52 -07001902void GrGLGpu::clear(const GrFixedClip& clip, GrColor color, GrRenderTarget* target) {
egdaniel9cb63402016-06-23 08:37:05 -07001903 this->handleDirtyContext();
1904
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001905 // parent class should never let us get here with no RT
bsalomon49f085d2014-09-05 13:34:00 -07001906 SkASSERT(target);
bsalomonb0bd4f62014-09-03 07:19:50 -07001907 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001908
csmartdalton29df7602016-08-31 11:55:52 -07001909 this->flushRenderTarget(glRT, clip.scissorEnabled() ? &clip.scissorRect() : nullptr);
1910 this->flushScissor(clip.scissorState(), glRT->getViewport(), glRT->origin());
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001911 this->flushWindowRectangles(clip.windowRectsState(), glRT);
bsalomon@google.com74b98712011-11-11 19:46:16 +00001912
1913 GrGLfloat r, g, b, a;
1914 static const GrGLfloat scale255 = 1.f / 255.f;
1915 a = GrColorUnpackA(color) * scale255;
1916 GrGLfloat scaleRGB = scale255;
bsalomon@google.com74b98712011-11-11 19:46:16 +00001917 r = GrColorUnpackR(color) * scaleRGB;
1918 g = GrColorUnpackG(color) * scaleRGB;
1919 b = GrColorUnpackB(color) * scaleRGB;
1920
1921 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00001922 fHWWriteToColor = kYes_TriState;
Brian Salomon028a9a52017-05-11 11:39:08 -04001923
Eric Karlaeaf22b2017-05-18 15:08:09 -07001924 if (this->glCaps().clearToBoundaryValuesIsBroken() &&
1925 (1 == r || 0 == r) && (1 == g || 0 == g) && (1 == b || 0 == b) && (1 == a || 0 == a)) {
Eric Karlaeaf22b2017-05-18 15:08:09 -07001926 static const GrGLfloat safeAlpha1 = nextafter(1.f, 2.f);
1927 static const GrGLfloat safeAlpha0 = nextafter(0.f, -1.f);
Eric Karlaeaf22b2017-05-18 15:08:09 -07001928 a = (1 == a) ? safeAlpha1 : safeAlpha0;
Brian Salomon028a9a52017-05-11 11:39:08 -04001929 }
bsalomon@google.com74b98712011-11-11 19:46:16 +00001930 GL_CALL(ClearColor(r, g, b, a));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001931 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001932}
1933
bsalomon861e1032014-12-16 07:33:49 -08001934void GrGLGpu::clearStencil(GrRenderTarget* target) {
halcanary96fcdcc2015-08-27 07:41:13 -07001935 if (nullptr == target) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001936 return;
1937 }
bsalomonb0bd4f62014-09-03 07:19:50 -07001938 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
egdanield803f272015-03-18 13:01:52 -07001939 this->flushRenderTarget(glRT, &SkIRect::EmptyIRect());
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001940
joshualitt77b13072014-10-27 14:51:01 -07001941 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07001942 this->disableWindowRectangles();
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001943
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001944 GL_CALL(StencilMask(0xffffffff));
1945 GL_CALL(ClearStencil(0));
1946 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001947 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001948}
1949
csmartdalton29df7602016-08-31 11:55:52 -07001950void GrGLGpu::clearStencilClip(const GrFixedClip& clip,
1951 bool insideStencilMask,
1952 GrRenderTarget* target) {
bsalomon49f085d2014-09-05 13:34:00 -07001953 SkASSERT(target);
egdaniel9cb63402016-06-23 08:37:05 -07001954 this->handleDirtyContext();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001955
Mike Klein31550db2017-06-06 23:29:53 +00001956 if (this->glCaps().useDrawToClearStencilClip()) {
1957 this->clearStencilClipAsDraw(clip, insideStencilMask, target);
1958 return;
1959 }
1960
egdaniel8dc7c3a2015-04-16 11:22:42 -07001961 GrStencilAttachment* sb = target->renderTargetPriv().getStencilAttachment();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001962 // this should only be called internally when we know we have a
1963 // stencil buffer.
bsalomon6bc1b5f2015-02-23 09:06:38 -08001964 SkASSERT(sb);
1965 GrGLint stencilBitCount = sb->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001966#if 0
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001967 SkASSERT(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00001968 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001969#else
1970 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001971 // ANGLE a partial stencil mask will cause clears to be
Robert Phillipsf2361d22016-10-25 14:20:06 -04001972 // turned into draws. Our contract on GrOpList says that
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001973 // changing the clip between stencil passes may or may not
1974 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00001975 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001976#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001977 GrGLint value;
csmartdalton29df7602016-08-31 11:55:52 -07001978 if (insideStencilMask) {
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001979 value = (1 << (stencilBitCount - 1));
1980 } else {
1981 value = 0;
1982 }
bsalomonb0bd4f62014-09-03 07:19:50 -07001983 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
egdanield803f272015-03-18 13:01:52 -07001984 this->flushRenderTarget(glRT, &SkIRect::EmptyIRect());
bsalomon@google.coma3201942012-06-21 19:58:20 +00001985
csmartdalton29df7602016-08-31 11:55:52 -07001986 this->flushScissor(clip.scissorState(), glRT->getViewport(), glRT->origin());
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001987 this->flushWindowRectangles(clip.windowRectsState(), glRT);
bsalomon@google.coma3201942012-06-21 19:58:20 +00001988
caryclark@google.comcf6285b2012-06-06 12:09:01 +00001989 GL_CALL(StencilMask((uint32_t) clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001990 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001991 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001992 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001993}
1994
Brian Salomon71d9d842016-11-03 13:42:00 -04001995static bool read_pixels_pays_for_y_flip(GrSurfaceOrigin origin, const GrGLCaps& caps,
bsalomon39826022015-07-23 08:07:21 -07001996 int width, int height, GrPixelConfig config,
1997 size_t rowBytes) {
Brian Salomon71d9d842016-11-03 13:42:00 -04001998 // If the surface is already TopLeft, we don't need to flip.
1999 if (kTopLeft_GrSurfaceOrigin == origin) {
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002000 return false;
2001 }
2002
bsalomon494aa592015-07-23 11:45:02 -07002003 // If the read is really small or smaller than the min texture size, don't force a draw.
bsalomon100b8f82015-10-28 08:37:44 -07002004 static const int kMinSize = 32;
2005 if (width < kMinSize || height < kMinSize) {
bsalomon494aa592015-07-23 11:45:02 -07002006 return false;
2007 }
2008
bsalomon@google.com56d11e02011-11-30 19:59:08 +00002009 // if GL can do the flip then we'll never pay for it.
bsalomon39826022015-07-23 08:07:21 -07002010 if (caps.packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00002011 return false;
2012 }
2013
2014 // If we have to do memcpy to handle non-trim rowBytes then we
bsalomon@google.com7107fa72011-11-10 14:54:14 +00002015 // get the flip for free. Otherwise it costs.
bsalomon39826022015-07-23 08:07:21 -07002016 // Note that we're assuming that 0 rowBytes has already been handled and that the width has been
2017 // clipped.
2018 return caps.packRowLengthSupport() || GrBytesPerPixel(config) * width == rowBytes;
2019}
2020
bsalomon1aa20292016-01-22 08:16:09 -08002021bool GrGLGpu::readPixelsSupported(GrRenderTarget* target, GrPixelConfig readConfig) {
Brian Salomon625cd9e2016-12-15 09:35:19 -05002022#ifdef SK_BUILD_FOR_MAC
2023 // Chromium may ask us to read back from locked IOSurfaces. Calling the command buffer's
2024 // glGetIntegerv() with GL_IMPLEMENTATION_COLOR_READ_FORMAT/_TYPE causes the command buffer
2025 // to make a call to check the framebuffer status which can hang the driver. So in Mac Chromium
2026 // we always use a temporary surface to test for read pixels support.
2027 // https://www.crbug.com/662802
2028 if (this->glContext().driver() == kChromium_GrGLDriver) {
2029 return this->readPixelsSupported(target->config(), readConfig);
2030 }
2031#endif
bsalomon1aa20292016-01-22 08:16:09 -08002032 auto bindRenderTarget = [this, target]() -> bool {
2033 this->flushRenderTarget(static_cast<GrGLRenderTarget*>(target), &SkIRect::EmptyIRect());
2034 return true;
2035 };
bsalomon2c3db322016-11-08 13:26:24 -08002036 auto unbindRenderTarget = []{};
bsalomon1aa20292016-01-22 08:16:09 -08002037 auto getIntegerv = [this](GrGLenum query, GrGLint* value) {
2038 GR_GL_GetIntegerv(this->glInterface(), query, value);
2039 };
2040 GrPixelConfig rtConfig = target->config();
bsalomon2c3db322016-11-08 13:26:24 -08002041 return this->glCaps().readPixelsSupported(rtConfig, readConfig, getIntegerv, bindRenderTarget,
2042 unbindRenderTarget);
bsalomon1aa20292016-01-22 08:16:09 -08002043}
2044
2045bool GrGLGpu::readPixelsSupported(GrPixelConfig rtConfig, GrPixelConfig readConfig) {
bsalomon2c3db322016-11-08 13:26:24 -08002046 sk_sp<GrTexture> temp;
2047 auto bindRenderTarget = [this, rtConfig, &temp]() -> bool {
bsalomon1aa20292016-01-22 08:16:09 -08002048 GrTextureDesc desc;
2049 desc.fConfig = rtConfig;
2050 desc.fWidth = desc.fHeight = 16;
bsalomon2c3db322016-11-08 13:26:24 -08002051 if (this->glCaps().isConfigRenderable(rtConfig, false)) {
2052 desc.fFlags = kRenderTarget_GrSurfaceFlag;
Robert Phillips67d52cf2017-06-05 13:38:13 -04002053 temp = this->createTexture(desc, SkBudgeted::kNo);
bsalomon2c3db322016-11-08 13:26:24 -08002054 if (!temp) {
2055 return false;
2056 }
2057 GrGLRenderTarget* glrt = static_cast<GrGLRenderTarget*>(temp->asRenderTarget());
2058 this->flushRenderTarget(glrt, &SkIRect::EmptyIRect());
2059 return true;
2060 } else if (this->glCaps().canConfigBeFBOColorAttachment(rtConfig)) {
Robert Phillips67d52cf2017-06-05 13:38:13 -04002061 temp = this->createTexture(desc, SkBudgeted::kNo);
bsalomon2c3db322016-11-08 13:26:24 -08002062 if (!temp) {
2063 return false;
2064 }
2065 GrGLIRect vp;
2066 this->bindSurfaceFBOForPixelOps(temp.get(), GR_GL_FRAMEBUFFER, &vp, kDst_TempFBOTarget);
Robert Phillips294870f2016-11-11 12:38:40 -05002067 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon2c3db322016-11-08 13:26:24 -08002068 return true;
bsalomon1aa20292016-01-22 08:16:09 -08002069 }
bsalomon2c3db322016-11-08 13:26:24 -08002070 return false;
2071 };
2072 auto unbindRenderTarget = [this, &temp]() {
2073 this->unbindTextureFBOForPixelOps(GR_GL_FRAMEBUFFER, temp.get());
bsalomon1aa20292016-01-22 08:16:09 -08002074 };
2075 auto getIntegerv = [this](GrGLenum query, GrGLint* value) {
2076 GR_GL_GetIntegerv(this->glInterface(), query, value);
2077 };
bsalomon2c3db322016-11-08 13:26:24 -08002078 return this->glCaps().readPixelsSupported(rtConfig, readConfig, getIntegerv, bindRenderTarget,
2079 unbindRenderTarget);
bsalomon1aa20292016-01-22 08:16:09 -08002080}
2081
2082bool GrGLGpu::readPixelsSupported(GrSurface* surfaceForConfig, GrPixelConfig readConfig) {
2083 if (GrRenderTarget* rt = surfaceForConfig->asRenderTarget()) {
2084 return this->readPixelsSupported(rt, readConfig);
2085 } else {
2086 GrPixelConfig config = surfaceForConfig->config();
2087 return this->readPixelsSupported(config, readConfig);
2088 }
2089}
2090
bsalomone9573312016-01-25 14:33:25 -08002091static bool requires_srgb_conversion(GrPixelConfig a, GrPixelConfig b) {
2092 if (GrPixelConfigIsSRGB(a)) {
2093 return !GrPixelConfigIsSRGB(b) && !GrPixelConfigIsAlphaOnly(b);
2094 } else if (GrPixelConfigIsSRGB(b)) {
2095 return !GrPixelConfigIsSRGB(a) && !GrPixelConfigIsAlphaOnly(a);
2096 }
2097 return false;
2098}
2099
bsalomonf0674512015-07-28 13:26:15 -07002100bool GrGLGpu::onGetReadPixelsInfo(GrSurface* srcSurface, int width, int height, size_t rowBytes,
2101 GrPixelConfig readConfig, DrawPreference* drawPreference,
2102 ReadPixelTempDrawInfo* tempDrawInfo) {
bsalomone9573312016-01-25 14:33:25 -08002103 GrPixelConfig srcConfig = srcSurface->config();
bsalomon1aa20292016-01-22 08:16:09 -08002104
bsalomone9573312016-01-25 14:33:25 -08002105 // These settings we will always want if a temp draw is performed.
bsalomon39826022015-07-23 08:07:21 -07002106 tempDrawInfo->fTempSurfaceDesc.fFlags = kRenderTarget_GrSurfaceFlag;
2107 tempDrawInfo->fTempSurfaceDesc.fWidth = width;
2108 tempDrawInfo->fTempSurfaceDesc.fHeight = height;
2109 tempDrawInfo->fTempSurfaceDesc.fSampleCnt = 0;
2110 tempDrawInfo->fTempSurfaceDesc.fOrigin = kTopLeft_GrSurfaceOrigin; // no CPU y-flip for TL.
bsalomonb117ff12016-07-19 07:24:40 -07002111 tempDrawInfo->fTempSurfaceFit = this->glCaps().partialFBOReadIsSlow() ? SkBackingFit::kExact
2112 : SkBackingFit::kApprox;
bsalomone9573312016-01-25 14:33:25 -08002113 // For now assume no swizzling, we may change that below.
2114 tempDrawInfo->fSwizzle = GrSwizzle::RGBA();
2115
2116 // Depends on why we need/want a temp draw. Start off assuming no change, the surface we read
2117 // from will be srcConfig and we will read readConfig pixels from it.
Brian Osman33910292017-04-18 14:38:53 -04002118 // Note that if we require a draw and return a non-renderable format for the temp surface the
bsalomone9573312016-01-25 14:33:25 -08002119 // base class will fail for us.
2120 tempDrawInfo->fTempSurfaceDesc.fConfig = srcConfig;
2121 tempDrawInfo->fReadConfig = readConfig;
2122
2123 if (requires_srgb_conversion(srcConfig, readConfig)) {
2124 if (!this->readPixelsSupported(readConfig, readConfig)) {
2125 return false;
2126 }
2127 // Draw to do srgb to linear conversion or vice versa.
2128 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
2129 tempDrawInfo->fTempSurfaceDesc.fConfig = readConfig;
2130 tempDrawInfo->fReadConfig = readConfig;
2131 return true;
2132 }
2133
bsalomon1aa20292016-01-22 08:16:09 -08002134 if (this->glCaps().rgba8888PixelsOpsAreSlow() && kRGBA_8888_GrPixelConfig == readConfig &&
2135 this->readPixelsSupported(kBGRA_8888_GrPixelConfig, kBGRA_8888_GrPixelConfig)) {
bsalomon39826022015-07-23 08:07:21 -07002136 tempDrawInfo->fTempSurfaceDesc.fConfig = kBGRA_8888_GrPixelConfig;
bsalomon6c9cd552016-01-22 07:17:34 -08002137 tempDrawInfo->fSwizzle = GrSwizzle::BGRA();
2138 tempDrawInfo->fReadConfig = kBGRA_8888_GrPixelConfig;
bsalomonb411b3b2015-07-31 09:34:24 -07002139 ElevateDrawPreference(drawPreference, kGpuPrefersDraw_DrawPreference);
ericrkb4ecabd2016-03-11 15:18:20 -08002140 } else if (this->glCaps().rgbaToBgraReadbackConversionsAreSlow() &&
bsalomon39826022015-07-23 08:07:21 -07002141 GrBytesPerPixel(readConfig) == 4 &&
bsalomon1aa20292016-01-22 08:16:09 -08002142 GrPixelConfigSwapRAndB(readConfig) == srcConfig &&
2143 this->readPixelsSupported(srcSurface, srcConfig)) {
bsalomon6c9cd552016-01-22 07:17:34 -08002144 // Mesa 3D takes a slow path on when reading back BGRA from an RGBA surface and vice-versa.
bsalomon39826022015-07-23 08:07:21 -07002145 // Better to do a draw with a R/B swap and then read as the original config.
2146 tempDrawInfo->fTempSurfaceDesc.fConfig = srcConfig;
bsalomon6c9cd552016-01-22 07:17:34 -08002147 tempDrawInfo->fSwizzle = GrSwizzle::BGRA();
2148 tempDrawInfo->fReadConfig = srcConfig;
bsalomonf0674512015-07-28 13:26:15 -07002149 ElevateDrawPreference(drawPreference, kGpuPrefersDraw_DrawPreference);
bsalomon1aa20292016-01-22 08:16:09 -08002150 } else if (!this->readPixelsSupported(srcSurface, readConfig)) {
2151 if (readConfig == kBGRA_8888_GrPixelConfig &&
Brian Salomon71d9d842016-11-03 13:42:00 -04002152 this->glCaps().canConfigBeFBOColorAttachment(kRGBA_8888_GrPixelConfig) &&
bsalomon1aa20292016-01-22 08:16:09 -08002153 this->readPixelsSupported(kRGBA_8888_GrPixelConfig, kRGBA_8888_GrPixelConfig)) {
bsalomone9573312016-01-25 14:33:25 -08002154 // We're trying to read BGRA but it's not supported. If RGBA is renderable and
2155 // we can read it back, then do a swizzling draw to a RGBA and read it back (which
2156 // will effectively be BGRA).
bsalomon1aa20292016-01-22 08:16:09 -08002157 tempDrawInfo->fTempSurfaceDesc.fConfig = kRGBA_8888_GrPixelConfig;
2158 tempDrawInfo->fSwizzle = GrSwizzle::BGRA();
2159 tempDrawInfo->fReadConfig = kRGBA_8888_GrPixelConfig;
2160 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
brianosmana6359362016-03-21 06:55:37 -07002161 } else if (readConfig == kSBGRA_8888_GrPixelConfig &&
Brian Salomon71d9d842016-11-03 13:42:00 -04002162 this->glCaps().canConfigBeFBOColorAttachment(kSRGBA_8888_GrPixelConfig) &&
brianosmana6359362016-03-21 06:55:37 -07002163 this->readPixelsSupported(kSRGBA_8888_GrPixelConfig, kSRGBA_8888_GrPixelConfig)) {
2164 // We're trying to read sBGRA but it's not supported. If sRGBA is renderable and
2165 // we can read it back, then do a swizzling draw to a sRGBA and read it back (which
2166 // will effectively be sBGRA).
2167 tempDrawInfo->fTempSurfaceDesc.fConfig = kSRGBA_8888_GrPixelConfig;
2168 tempDrawInfo->fSwizzle = GrSwizzle::BGRA();
2169 tempDrawInfo->fReadConfig = kSRGBA_8888_GrPixelConfig;
2170 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
bsalomone9573312016-01-25 14:33:25 -08002171 } else if (readConfig == kAlpha_8_GrPixelConfig) {
2172 // onReadPixels implements a fallback for cases where we are want to read kAlpha_8,
2173 // it's unsupported, but 32bit RGBA reads are supported.
2174 // Don't attempt to do any srgb conversions since we only care about alpha.
2175 GrPixelConfig cpuTempConfig = kRGBA_8888_GrPixelConfig;
2176 if (GrPixelConfigIsSRGB(srcSurface->config())) {
2177 cpuTempConfig = kSRGBA_8888_GrPixelConfig;
2178 }
2179 if (!this->readPixelsSupported(srcSurface, cpuTempConfig)) {
2180 // If we can't read RGBA from the src try to draw to a kRGBA_8888 (or kSRGBA_8888)
2181 // first and then onReadPixels will read that to a 32bit temporary buffer.
Brian Salomon71d9d842016-11-03 13:42:00 -04002182 if (this->glCaps().canConfigBeFBOColorAttachment(cpuTempConfig)) {
bsalomone9573312016-01-25 14:33:25 -08002183 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
2184 tempDrawInfo->fTempSurfaceDesc.fConfig = cpuTempConfig;
2185 tempDrawInfo->fReadConfig = kAlpha_8_GrPixelConfig;
2186 } else {
2187 return false;
2188 }
2189 } else {
2190 SkASSERT(tempDrawInfo->fTempSurfaceDesc.fConfig == srcConfig);
2191 SkASSERT(tempDrawInfo->fReadConfig == kAlpha_8_GrPixelConfig);
2192 }
Brian Salomon71d9d842016-11-03 13:42:00 -04002193 } else if (this->glCaps().canConfigBeFBOColorAttachment(readConfig) &&
bsalomone6d665e2016-03-10 07:22:25 -08002194 this->readPixelsSupported(readConfig, readConfig)) {
2195 // Do a draw to convert from the src config to the read config.
2196 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
2197 tempDrawInfo->fTempSurfaceDesc.fConfig = readConfig;
2198 tempDrawInfo->fReadConfig = readConfig;
bsalomon1aa20292016-01-22 08:16:09 -08002199 } else {
2200 return false;
2201 }
bsalomon39826022015-07-23 08:07:21 -07002202 }
2203
Brian Salomon71d9d842016-11-03 13:42:00 -04002204 if ((srcSurface->asRenderTarget() || this->glCaps().canConfigBeFBOColorAttachment(srcConfig)) &&
2205 read_pixels_pays_for_y_flip(srcSurface->origin(), this->glCaps(), width, height, readConfig,
2206 rowBytes)) {
bsalomonf0674512015-07-28 13:26:15 -07002207 ElevateDrawPreference(drawPreference, kGpuPrefersDraw_DrawPreference);
bsalomon39826022015-07-23 08:07:21 -07002208 }
2209
bsalomon39826022015-07-23 08:07:21 -07002210 return true;
bsalomon@google.comc4364992011-11-07 15:54:49 +00002211}
2212
bsalomon6cb3cbe2015-07-30 07:34:27 -07002213bool GrGLGpu::onReadPixels(GrSurface* surface,
bsalomon@google.comc6980972011-11-02 19:57:21 +00002214 int left, int top,
2215 int width, int height,
bsalomon@google.comc4364992011-11-07 15:54:49 +00002216 GrPixelConfig config,
2217 void* buffer,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002218 size_t rowBytes) {
bsalomon6cb3cbe2015-07-30 07:34:27 -07002219 SkASSERT(surface);
bsalomon39826022015-07-23 08:07:21 -07002220
bsalomone9573312016-01-25 14:33:25 -08002221 GrGLRenderTarget* renderTarget = static_cast<GrGLRenderTarget*>(surface->asRenderTarget());
Brian Salomon71d9d842016-11-03 13:42:00 -04002222 if (!renderTarget && !this->glCaps().canConfigBeFBOColorAttachment(surface->config())) {
bsalomon6cb3cbe2015-07-30 07:34:27 -07002223 return false;
2224 }
2225
bsalomon16921ec2015-07-30 15:34:56 -07002226 // OpenGL doesn't do sRGB <-> linear conversions when reading and writing pixels.
bsalomone9573312016-01-25 14:33:25 -08002227 if (requires_srgb_conversion(surface->config(), config)) {
2228 return false;
2229 }
2230
2231 // We have a special case fallback for reading eight bit alpha. We will read back all four 8
2232 // bit channels as RGBA and then extract A.
Brian Salomon71d9d842016-11-03 13:42:00 -04002233 if (!this->readPixelsSupported(surface, config)) {
bsalomone9573312016-01-25 14:33:25 -08002234 // Don't attempt to do any srgb conversions since we only care about alpha.
2235 GrPixelConfig tempConfig = kRGBA_8888_GrPixelConfig;
Brian Salomon71d9d842016-11-03 13:42:00 -04002236 if (GrPixelConfigIsSRGB(surface->config())) {
bsalomone9573312016-01-25 14:33:25 -08002237 tempConfig = kSRGBA_8888_GrPixelConfig;
2238 }
2239 if (kAlpha_8_GrPixelConfig == config &&
Brian Salomon71d9d842016-11-03 13:42:00 -04002240 this->readPixelsSupported(surface, tempConfig)) {
Ben Wagner7ecc5962016-11-02 17:07:33 -04002241 std::unique_ptr<uint32_t[]> temp(new uint32_t[width * height * 4]);
Brian Salomon71d9d842016-11-03 13:42:00 -04002242 if (this->onReadPixels(surface, left, top, width, height, tempConfig, temp.get(),
bsalomone9573312016-01-25 14:33:25 -08002243 width*4)) {
2244 uint8_t* dst = reinterpret_cast<uint8_t*>(buffer);
2245 for (int j = 0; j < height; ++j) {
2246 for (int i = 0; i < width; ++i) {
2247 dst[j*rowBytes + i] = (0xFF000000U & temp[j*width+i]) >> 24;
2248 }
2249 }
2250 return true;
2251 }
2252 }
bsalomon16921ec2015-07-30 15:34:56 -07002253 return false;
2254 }
2255
bsalomon76148af2016-01-12 11:13:47 -08002256 GrGLenum externalFormat;
2257 GrGLenum externalType;
Brian Salomon71d9d842016-11-03 13:42:00 -04002258 if (!this->glCaps().getReadPixelsFormat(surface->config(), config, &externalFormat,
bsalomon76148af2016-01-12 11:13:47 -08002259 &externalType)) {
2260 return false;
2261 }
bsalomon6cb3cbe2015-07-30 07:34:27 -07002262 bool flipY = kBottomLeft_GrSurfaceOrigin == surface->origin();
bsalomon@google.comc4364992011-11-07 15:54:49 +00002263
Brian Salomon71d9d842016-11-03 13:42:00 -04002264 GrGLIRect glvp;
2265 if (renderTarget) {
2266 // resolve the render target if necessary
2267 switch (renderTarget->getResolveType()) {
2268 case GrGLRenderTarget::kCantResolve_ResolveType:
2269 return false;
2270 case GrGLRenderTarget::kAutoResolves_ResolveType:
2271 this->flushRenderTarget(renderTarget, &SkIRect::EmptyIRect());
2272 break;
2273 case GrGLRenderTarget::kCanResolve_ResolveType:
2274 this->onResolveRenderTarget(renderTarget);
2275 // we don't track the state of the READ FBO ID.
2276 fStats.incRenderTargetBinds();
2277 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER, renderTarget->textureFBOID()));
2278 break;
2279 default:
2280 SkFAIL("Unknown resolve type");
2281 }
2282 glvp = renderTarget->getViewport();
2283 } else {
2284 // Use a temporary FBO.
2285 this->bindSurfaceFBOForPixelOps(surface, GR_GL_FRAMEBUFFER, &glvp, kSrc_TempFBOTarget);
Robert Phillips294870f2016-11-11 12:38:40 -05002286 fHWBoundRenderTargetUniqueID.makeInvalid();
reed@google.comac10a2d2010-12-22 21:39:39 +00002287 }
2288
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00002289 // the read rect is viewport-relative
2290 GrGLIRect readRect;
Brian Salomon71d9d842016-11-03 13:42:00 -04002291 readRect.setRelativeTo(glvp, left, top, width, height, surface->origin());
rmistry@google.comfbfcd562012-08-23 18:09:54 +00002292
bsalomon9d02b262016-02-01 12:49:30 -08002293 size_t bytesPerPixel = GrBytesPerPixel(config);
2294 size_t tightRowBytes = bytesPerPixel * width;
egdaniel6d901da2015-07-30 12:02:15 -07002295
bsalomon@google.comc6980972011-11-02 19:57:21 +00002296 size_t readDstRowBytes = tightRowBytes;
2297 void* readDst = buffer;
rmistry@google.comfbfcd562012-08-23 18:09:54 +00002298
bsalomon@google.comc6980972011-11-02 19:57:21 +00002299 // determine if GL can read using the passed rowBytes or if we need
2300 // a scratch buffer.
joshualitt29f86792015-05-29 08:06:48 -07002301 SkAutoSMalloc<32 * sizeof(GrColor)> scratch;
bsalomon@google.comc6980972011-11-02 19:57:21 +00002302 if (rowBytes != tightRowBytes) {
bsalomon9d02b262016-02-01 12:49:30 -08002303 if (this->glCaps().packRowLengthSupport() && !(rowBytes % bytesPerPixel)) {
skia.committer@gmail.com4677acc2013-10-17 07:02:33 +00002304 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH,
bsalomon9d02b262016-02-01 12:49:30 -08002305 static_cast<GrGLint>(rowBytes / bytesPerPixel)));
bsalomon@google.comc6980972011-11-02 19:57:21 +00002306 readDstRowBytes = rowBytes;
2307 } else {
2308 scratch.reset(tightRowBytes * height);
2309 readDst = scratch.get();
2310 }
2311 }
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002312 if (flipY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00002313 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 1));
2314 }
bsalomonf46a1242015-12-15 12:37:38 -08002315 GL_CALL(PixelStorei(GR_GL_PACK_ALIGNMENT, config_alignment(config)));
2316
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002317 GL_CALL(ReadPixels(readRect.fLeft, readRect.fBottom,
2318 readRect.fWidth, readRect.fHeight,
bsalomon76148af2016-01-12 11:13:47 -08002319 externalFormat, externalType, readDst));
bsalomon@google.comc6980972011-11-02 19:57:21 +00002320 if (readDstRowBytes != tightRowBytes) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002321 SkASSERT(this->glCaps().packRowLengthSupport());
bsalomon@google.comc6980972011-11-02 19:57:21 +00002322 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
2323 }
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002324 if (flipY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00002325 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 0));
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002326 flipY = false;
bsalomon@google.com56d11e02011-11-30 19:59:08 +00002327 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002328
2329 // now reverse the order of the rows, since GL's are bottom-to-top, but our
bsalomon@google.comc6980972011-11-02 19:57:21 +00002330 // API presents top-to-bottom. We must preserve the padding contents. Note
2331 // that the above readPixels did not overwrite the padding.
2332 if (readDst == buffer) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002333 SkASSERT(rowBytes == readDstRowBytes);
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002334 if (flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002335 scratch.reset(tightRowBytes);
2336 void* tmpRow = scratch.get();
2337 // flip y in-place by rows
2338 const int halfY = height >> 1;
2339 char* top = reinterpret_cast<char*>(buffer);
2340 char* bottom = top + (height - 1) * rowBytes;
2341 for (int y = 0; y < halfY; y++) {
2342 memcpy(tmpRow, top, tightRowBytes);
2343 memcpy(top, bottom, tightRowBytes);
2344 memcpy(bottom, tmpRow, tightRowBytes);
2345 top += rowBytes;
2346 bottom -= rowBytes;
2347 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00002348 }
2349 } else {
bsalomon9d02b262016-02-01 12:49:30 -08002350 SkASSERT(readDst != buffer);
2351 SkASSERT(rowBytes != tightRowBytes);
bsalomon@google.comc6980972011-11-02 19:57:21 +00002352 // copy from readDst to buffer while flipping y
caryclark@google.comcf6285b2012-06-06 12:09:01 +00002353 // const int halfY = height >> 1;
bsalomon@google.comc6980972011-11-02 19:57:21 +00002354 const char* src = reinterpret_cast<const char*>(readDst);
bsalomon@google.comc4364992011-11-07 15:54:49 +00002355 char* dst = reinterpret_cast<char*>(buffer);
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002356 if (flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002357 dst += (height-1) * rowBytes;
2358 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00002359 for (int y = 0; y < height; y++) {
2360 memcpy(dst, src, tightRowBytes);
2361 src += readDstRowBytes;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002362 if (!flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002363 dst += rowBytes;
2364 } else {
2365 dst -= rowBytes;
2366 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002367 }
2368 }
Brian Salomon71d9d842016-11-03 13:42:00 -04002369 if (!renderTarget) {
2370 this->unbindTextureFBOForPixelOps(GR_GL_FRAMEBUFFER, surface);
2371 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002372 return true;
2373}
2374
egdaniel9cb63402016-06-23 08:37:05 -07002375GrGpuCommandBuffer* GrGLGpu::createCommandBuffer(
egdaniel9cb63402016-06-23 08:37:05 -07002376 const GrGpuCommandBuffer::LoadAndStoreInfo& colorInfo,
2377 const GrGpuCommandBuffer::LoadAndStoreInfo& stencilInfo) {
Brian Salomonc293a292016-11-30 13:38:32 -05002378 return new GrGLGpuCommandBuffer(this);
egdaniel066df7c2016-06-08 14:02:27 -07002379}
2380
brianosman64d094d2016-03-25 06:01:59 -07002381void GrGLGpu::flushRenderTarget(GrGLRenderTarget* target, const SkIRect* bounds, bool disableSRGB) {
egdanield803f272015-03-18 13:01:52 -07002382 SkASSERT(target);
bsalomon6ba6fa12015-03-04 11:57:37 -08002383
Robert Phillips294870f2016-11-11 12:38:40 -05002384 GrGpuResource::UniqueID rtID = target->uniqueID();
egdanield803f272015-03-18 13:01:52 -07002385 if (fHWBoundRenderTargetUniqueID != rtID) {
bsalomon1e0bf7e2015-03-14 12:08:51 -07002386 fStats.incRenderTargetBinds();
egdanield803f272015-03-18 13:01:52 -07002387 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, target->renderFBOID()));
2388#ifdef SK_DEBUG
2389 // don't do this check in Chromium -- this is causing
2390 // lots of repeated command buffer flushes when the compositor is
2391 // rendering with Ganesh, which is really slow; even too slow for
2392 // Debug mode.
cdalton1acea862015-06-02 13:05:52 -07002393 if (kChromium_GrGLDriver != this->glContext().driver()) {
egdanield803f272015-03-18 13:01:52 -07002394 GrGLenum status;
2395 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
2396 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
2397 SkDebugf("GrGLGpu::flushRenderTarget glCheckFramebufferStatus %x\n", status);
2398 }
bsalomon160f24c2015-03-17 15:55:42 -07002399 }
egdanield803f272015-03-18 13:01:52 -07002400#endif
2401 fHWBoundRenderTargetUniqueID = rtID;
bsalomon083617b2016-02-12 12:10:14 -08002402 this->flushViewport(target->getViewport());
brianosman64d094d2016-03-25 06:01:59 -07002403 }
2404
brianosman35b784d2016-05-05 11:52:53 -07002405 if (this->glCaps().srgbWriteControl()) {
brianosman33f6b3f2016-06-02 05:49:21 -07002406 this->flushFramebufferSRGB(GrPixelConfigIsSRGB(target->config()) && !disableSRGB);
bsalomon5cd020f2015-03-17 12:46:56 -07002407 }
brianosman64d094d2016-03-25 06:01:59 -07002408
bsalomon083617b2016-02-12 12:10:14 -08002409 this->didWriteToSurface(target, bounds);
2410}
bsalomona9909122016-01-23 10:41:40 -08002411
brianosman33f6b3f2016-06-02 05:49:21 -07002412void GrGLGpu::flushFramebufferSRGB(bool enable) {
2413 if (enable && kYes_TriState != fHWSRGBFramebuffer) {
2414 GL_CALL(Enable(GR_GL_FRAMEBUFFER_SRGB));
2415 fHWSRGBFramebuffer = kYes_TriState;
2416 } else if (!enable && kNo_TriState != fHWSRGBFramebuffer) {
2417 GL_CALL(Disable(GR_GL_FRAMEBUFFER_SRGB));
2418 fHWSRGBFramebuffer = kNo_TriState;
2419 }
2420}
2421
bsalomon083617b2016-02-12 12:10:14 -08002422void GrGLGpu::flushViewport(const GrGLIRect& viewport) {
2423 if (fHWViewport != viewport) {
2424 viewport.pushToGLViewport(this->glInterface());
2425 fHWViewport = viewport;
2426 }
2427}
2428
Ravi Mistry7cd974f2017-06-13 14:45:23 +00002429GrGLenum gPrimitiveType2GLMode[] = {
2430 GR_GL_TRIANGLES,
2431 GR_GL_TRIANGLE_STRIP,
2432 GR_GL_TRIANGLE_FAN,
2433 GR_GL_POINTS,
2434 GR_GL_LINES,
2435 GR_GL_LINE_STRIP
2436};
2437
bsalomon@google.comd302f142011-03-03 13:54:13 +00002438#define SWAP_PER_DRAW 0
2439
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00002440#if SWAP_PER_DRAW
commit-bot@chromium.org43823302013-09-25 20:57:51 +00002441 #if defined(SK_BUILD_FOR_MAC)
bsalomon@google.comd302f142011-03-03 13:54:13 +00002442 #include <AGL/agl.h>
commit-bot@chromium.org43823302013-09-25 20:57:51 +00002443 #elif defined(SK_BUILD_FOR_WIN32)
bsalomon@google.comce7357d2012-06-25 17:49:25 +00002444 #include <gl/GL.h>
bsalomon@google.comd302f142011-03-03 13:54:13 +00002445 void SwapBuf() {
2446 DWORD procID = GetCurrentProcessId();
2447 HWND hwnd = GetTopWindow(GetDesktopWindow());
2448 while(hwnd) {
2449 DWORD wndProcID = 0;
2450 GetWindowThreadProcessId(hwnd, &wndProcID);
2451 if(wndProcID == procID) {
2452 SwapBuffers(GetDC(hwnd));
2453 }
2454 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
2455 }
2456 }
2457 #endif
2458#endif
2459
egdaniel9cb63402016-06-23 08:37:05 -07002460void GrGLGpu::draw(const GrPipeline& pipeline,
2461 const GrPrimitiveProcessor& primProc,
bsalomon2eda5b32016-09-21 10:53:24 -07002462 const GrMesh meshes[],
Chris Dalton46983b72017-06-06 12:27:16 -06002463 const GrPipeline::DynamicState dynamicStates[],
egdaniel9cb63402016-06-23 08:37:05 -07002464 int meshCount) {
2465 this->handleDirtyContext();
2466
bsalomon2eda5b32016-09-21 10:53:24 -07002467 bool hasPoints = false;
2468 for (int i = 0; i < meshCount; ++i) {
Ravi Mistry7cd974f2017-06-13 14:45:23 +00002469 if (meshes[i].primitiveType() == kPoints_GrPrimitiveType) {
bsalomon2eda5b32016-09-21 10:53:24 -07002470 hasPoints = true;
2471 break;
2472 }
2473 }
2474 if (!this->flushGLState(pipeline, primProc, hasPoints)) {
bsalomond95263c2014-12-16 13:05:12 -08002475 return;
2476 }
ethannicholas22793252016-01-30 09:59:10 -08002477
egdaniel0e1853c2016-03-17 11:35:45 -07002478 for (int i = 0; i < meshCount; ++i) {
2479 if (GrXferBarrierType barrierType = pipeline.xferBarrierType(*this->caps())) {
2480 this->xferBarrier(pipeline.getRenderTarget(), barrierType);
2481 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002482
Chris Dalton46983b72017-06-06 12:27:16 -06002483 if (dynamicStates) {
2484 if (pipeline.getScissorState().enabled()) {
2485 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(pipeline.getRenderTarget());
2486 this->flushScissor(dynamicStates[i].fScissorRect,
2487 glRT->getViewport(), glRT->origin());
2488 }
2489 }
Brian Salomon6d9c88b2017-06-12 10:24:42 -04002490 if (this->glCaps().requiresCullFaceEnableDisableWhenDrawingLinesAfterNonLines() &&
2491 GrIsPrimTypeLines(meshes[i].primitiveType()) &&
2492 !GrIsPrimTypeLines(fLastPrimitiveType)) {
2493 GL_CALL(Enable(GR_GL_CULL_FACE));
2494 GL_CALL(Disable(GR_GL_CULL_FACE));
2495 }
Chris Dalton114a3c02017-05-26 15:17:19 -06002496 meshes[i].sendToGpu(primProc, this);
Brian Salomon6d9c88b2017-06-12 10:24:42 -04002497 fLastPrimitiveType = meshes[i].primitiveType();
bsalomon@google.com74749cd2013-01-30 16:12:41 +00002498 }
ethannicholas22793252016-01-30 09:59:10 -08002499
bsalomon@google.comd302f142011-03-03 13:54:13 +00002500#if SWAP_PER_DRAW
2501 glFlush();
commit-bot@chromium.org43823302013-09-25 20:57:51 +00002502 #if defined(SK_BUILD_FOR_MAC)
bsalomon@google.comd302f142011-03-03 13:54:13 +00002503 aglSwapBuffers(aglGetCurrentContext());
2504 int set_a_break_pt_here = 9;
2505 aglSwapBuffers(aglGetCurrentContext());
commit-bot@chromium.org43823302013-09-25 20:57:51 +00002506 #elif defined(SK_BUILD_FOR_WIN32)
bsalomon@google.comd302f142011-03-03 13:54:13 +00002507 SwapBuf();
2508 int set_a_break_pt_here = 9;
2509 SwapBuf();
2510 #endif
2511#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00002512}
2513
Chris Dalton114a3c02017-05-26 15:17:19 -06002514void GrGLGpu::sendMeshToGpu(const GrPrimitiveProcessor& primProc, GrPrimitiveType primitiveType,
2515 const GrBuffer* vertexBuffer, int vertexCount, int baseVertex) {
Ravi Mistry7cd974f2017-06-13 14:45:23 +00002516 const GrGLenum glPrimType = gPrimitiveType2GLMode[primitiveType];
Chris Dalton114a3c02017-05-26 15:17:19 -06002517 if (this->glCaps().drawArraysBaseVertexIsBroken()) {
Chris Dalton1d616352017-05-31 12:51:23 -06002518 this->setupGeometry(primProc, nullptr, vertexBuffer, baseVertex, nullptr, 0);
Chris Dalton114a3c02017-05-26 15:17:19 -06002519 GL_CALL(DrawArrays(glPrimType, 0, vertexCount));
2520 } else {
Chris Dalton1d616352017-05-31 12:51:23 -06002521 this->setupGeometry(primProc, nullptr, vertexBuffer, 0, nullptr, 0);
Chris Dalton114a3c02017-05-26 15:17:19 -06002522 GL_CALL(DrawArrays(glPrimType, baseVertex, vertexCount));
2523 }
2524 fStats.incNumDraws();
2525}
2526
2527void GrGLGpu::sendIndexedMeshToGpu(const GrPrimitiveProcessor& primProc,
2528 GrPrimitiveType primitiveType, const GrBuffer* indexBuffer,
2529 int indexCount, int baseIndex, uint16_t minIndexValue,
2530 uint16_t maxIndexValue, const GrBuffer* vertexBuffer,
2531 int baseVertex) {
Ravi Mistry7cd974f2017-06-13 14:45:23 +00002532 const GrGLenum glPrimType = gPrimitiveType2GLMode[primitiveType];
Chris Dalton114a3c02017-05-26 15:17:19 -06002533 GrGLvoid* const indices = reinterpret_cast<void*>(indexBuffer->baseOffset() +
2534 sizeof(uint16_t) * baseIndex);
2535
Chris Dalton1d616352017-05-31 12:51:23 -06002536 this->setupGeometry(primProc, indexBuffer, vertexBuffer, baseVertex, nullptr, 0);
Chris Dalton114a3c02017-05-26 15:17:19 -06002537
2538 if (this->glCaps().drawRangeElementsSupport()) {
2539 GL_CALL(DrawRangeElements(glPrimType, minIndexValue, maxIndexValue, indexCount,
2540 GR_GL_UNSIGNED_SHORT, indices));
2541 } else {
2542 GL_CALL(DrawElements(glPrimType, indexCount, GR_GL_UNSIGNED_SHORT, indices));
2543 }
2544 fStats.incNumDraws();
2545}
2546
Chris Dalton1d616352017-05-31 12:51:23 -06002547void GrGLGpu::sendInstancedMeshToGpu(const GrPrimitiveProcessor& primProc, GrPrimitiveType
2548 primitiveType, const GrBuffer* vertexBuffer,
2549 int vertexCount, int baseVertex,
2550 const GrBuffer* instanceBuffer, int instanceCount,
2551 int baseInstance) {
Ravi Mistry7cd974f2017-06-13 14:45:23 +00002552 const GrGLenum glPrimType = gPrimitiveType2GLMode[primitiveType];
Chris Dalton1d616352017-05-31 12:51:23 -06002553 this->setupGeometry(primProc, nullptr, vertexBuffer, 0, instanceBuffer, baseInstance);
2554 GL_CALL(DrawArraysInstanced(glPrimType, baseVertex, vertexCount, instanceCount));
2555 fStats.incNumDraws();
2556}
2557
2558void GrGLGpu::sendIndexedInstancedMeshToGpu(const GrPrimitiveProcessor& primProc,
2559 GrPrimitiveType primitiveType,
2560 const GrBuffer* indexBuffer, int indexCount,
2561 int baseIndex, const GrBuffer* vertexBuffer,
2562 int baseVertex, const GrBuffer* instanceBuffer,
2563 int instanceCount, int baseInstance) {
Ravi Mistry7cd974f2017-06-13 14:45:23 +00002564 const GrGLenum glPrimType = gPrimitiveType2GLMode[primitiveType];
Chris Dalton1d616352017-05-31 12:51:23 -06002565 GrGLvoid* indices = reinterpret_cast<void*>(indexBuffer->baseOffset() +
2566 sizeof(uint16_t) * baseIndex);
2567 this->setupGeometry(primProc, indexBuffer, vertexBuffer, baseVertex,
2568 instanceBuffer, baseInstance);
2569 GL_CALL(DrawElementsInstanced(glPrimType, indexCount, GR_GL_UNSIGNED_SHORT, indices,
2570 instanceCount));
2571 fStats.incNumDraws();
2572}
2573
bsalomon861e1032014-12-16 07:33:49 -08002574void GrGLGpu::onResolveRenderTarget(GrRenderTarget* target) {
bsalomon@google.com75f9f252012-01-31 13:35:56 +00002575 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00002576 if (rt->needsResolve()) {
bsalomon@google.com347c3822013-05-01 20:10:01 +00002577 // Some extensions automatically resolves the texture when it is read.
2578 if (this->glCaps().usesMSAARenderBuffers()) {
egdanield803f272015-03-18 13:01:52 -07002579 SkASSERT(rt->textureFBOID() != rt->renderFBOID());
2580 fStats.incRenderTargetBinds();
2581 fStats.incRenderTargetBinds();
2582 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER, rt->renderFBOID()));
2583 GL_CALL(BindFramebuffer(GR_GL_DRAW_FRAMEBUFFER, rt->textureFBOID()));
2584 // make sure we go through flushRenderTarget() since we've modified
2585 // the bound DRAW FBO ID.
Robert Phillips294870f2016-11-11 12:38:40 -05002586 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002587 const GrGLIRect& vp = rt->getViewport();
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00002588 const SkIRect dirtyRect = rt->getResolveRect();
reed@google.comac10a2d2010-12-22 21:39:39 +00002589
bsalomon@google.com347c3822013-05-01 20:10:01 +00002590 if (GrGLCaps::kES_Apple_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002591 // Apple's extension uses the scissor as the blit bounds.
bsalomon3e791242014-12-17 13:43:13 -08002592 GrScissorState scissorState;
robertphillipse85a32d2015-02-10 08:16:55 -08002593 scissorState.set(dirtyRect);
2594 this->flushScissor(scissorState, vp, rt->origin());
csmartdalton28341fa2016-08-17 10:00:21 -07002595 this->disableWindowRectangles();
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002596 GL_CALL(ResolveMultisampleFramebuffer());
2597 } else {
Brian Salomone5e7eb12016-10-14 16:18:33 -04002598 int l, b, r, t;
2599 if (GrGLCaps::kResolveMustBeFull_BlitFrambufferFlag &
2600 this->glCaps().blitFramebufferSupportFlags()) {
2601 l = 0;
2602 b = 0;
2603 r = target->width();
2604 t = target->height();
2605 } else {
2606 GrGLIRect rect;
2607 rect.setRelativeTo(vp, dirtyRect.fLeft, dirtyRect.fTop,
2608 dirtyRect.width(), dirtyRect.height(), target->origin());
2609 l = rect.fLeft;
2610 b = rect.fBottom;
2611 r = rect.fLeft + rect.fWidth;
2612 t = rect.fBottom + rect.fHeight;
2613 }
derekf8c8f71a2014-09-16 06:24:57 -07002614
2615 // BlitFrameBuffer respects the scissor, so disable it.
joshualitt77b13072014-10-27 14:51:01 -07002616 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07002617 this->disableWindowRectangles();
Brian Salomone5e7eb12016-10-14 16:18:33 -04002618 GL_CALL(BlitFramebuffer(l, b, r, t, l, b, r, t,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002619 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00002620 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002621 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00002622 rt->flagAsResolved();
reed@google.comac10a2d2010-12-22 21:39:39 +00002623 }
2624}
2625
bsalomon@google.com411dad02012-06-05 20:24:20 +00002626namespace {
bsalomon@google.comd302f142011-03-03 13:54:13 +00002627
bsalomon@google.com411dad02012-06-05 20:24:20 +00002628
2629GrGLenum gr_to_gl_stencil_op(GrStencilOp op) {
cdalton93a379b2016-05-11 13:58:08 -07002630 static const GrGLenum gTable[kGrStencilOpCount] = {
2631 GR_GL_KEEP, // kKeep
2632 GR_GL_ZERO, // kZero
2633 GR_GL_REPLACE, // kReplace
2634 GR_GL_INVERT, // kInvert
2635 GR_GL_INCR_WRAP, // kIncWrap
2636 GR_GL_DECR_WRAP, // kDecWrap
2637 GR_GL_INCR, // kIncClamp
2638 GR_GL_DECR, // kDecClamp
bsalomon@google.com411dad02012-06-05 20:24:20 +00002639 };
cdalton93a379b2016-05-11 13:58:08 -07002640 GR_STATIC_ASSERT(0 == (int)GrStencilOp::kKeep);
2641 GR_STATIC_ASSERT(1 == (int)GrStencilOp::kZero);
2642 GR_STATIC_ASSERT(2 == (int)GrStencilOp::kReplace);
2643 GR_STATIC_ASSERT(3 == (int)GrStencilOp::kInvert);
2644 GR_STATIC_ASSERT(4 == (int)GrStencilOp::kIncWrap);
2645 GR_STATIC_ASSERT(5 == (int)GrStencilOp::kDecWrap);
2646 GR_STATIC_ASSERT(6 == (int)GrStencilOp::kIncClamp);
2647 GR_STATIC_ASSERT(7 == (int)GrStencilOp::kDecClamp);
2648 SkASSERT(op < (GrStencilOp)kGrStencilOpCount);
2649 return gTable[(int)op];
bsalomon@google.com411dad02012-06-05 20:24:20 +00002650}
2651
2652void set_gl_stencil(const GrGLInterface* gl,
cdalton93a379b2016-05-11 13:58:08 -07002653 const GrStencilSettings::Face& face,
2654 GrGLenum glFace) {
2655 GrGLenum glFunc = GrToGLStencilFunc(face.fTest);
2656 GrGLenum glFailOp = gr_to_gl_stencil_op(face.fFailOp);
2657 GrGLenum glPassOp = gr_to_gl_stencil_op(face.fPassOp);
bsalomon@google.coma3201942012-06-21 19:58:20 +00002658
cdalton93a379b2016-05-11 13:58:08 -07002659 GrGLint ref = face.fRef;
2660 GrGLint mask = face.fTestMask;
2661 GrGLint writeMask = face.fWriteMask;
bsalomon@google.com411dad02012-06-05 20:24:20 +00002662
2663 if (GR_GL_FRONT_AND_BACK == glFace) {
2664 // we call the combined func just in case separate stencil is not
2665 // supported.
2666 GR_GL_CALL(gl, StencilFunc(glFunc, ref, mask));
2667 GR_GL_CALL(gl, StencilMask(writeMask));
vbuzinovc5d58f02015-08-21 05:24:24 -07002668 GR_GL_CALL(gl, StencilOp(glFailOp, GR_GL_KEEP, glPassOp));
bsalomon@google.com411dad02012-06-05 20:24:20 +00002669 } else {
2670 GR_GL_CALL(gl, StencilFuncSeparate(glFace, glFunc, ref, mask));
2671 GR_GL_CALL(gl, StencilMaskSeparate(glFace, writeMask));
vbuzinovc5d58f02015-08-21 05:24:24 -07002672 GR_GL_CALL(gl, StencilOpSeparate(glFace, glFailOp, GR_GL_KEEP, glPassOp));
bsalomon@google.com411dad02012-06-05 20:24:20 +00002673 }
2674}
2675}
bsalomon@google.comd302f142011-03-03 13:54:13 +00002676
bsalomon3e791242014-12-17 13:43:13 -08002677void GrGLGpu::flushStencil(const GrStencilSettings& stencilSettings) {
csmartdaltonc7d85332016-10-26 10:13:46 -07002678 if (stencilSettings.isDisabled()) {
2679 this->disableStencil();
2680 } else if (fHWStencilSettings != stencilSettings) {
2681 if (kYes_TriState != fHWStencilTestEnabled) {
2682 GL_CALL(Enable(GR_GL_STENCIL_TEST));
Brian Salomonaf971de2017-06-08 16:11:33 -04002683
csmartdaltonc7d85332016-10-26 10:13:46 -07002684 fHWStencilTestEnabled = kYes_TriState;
bsalomon@google.coma3201942012-06-21 19:58:20 +00002685 }
csmartdaltonc7d85332016-10-26 10:13:46 -07002686 if (stencilSettings.isTwoSided()) {
csmartdaltonc7d85332016-10-26 10:13:46 -07002687 set_gl_stencil(this->glInterface(),
2688 stencilSettings.front(),
2689 GR_GL_FRONT);
2690 set_gl_stencil(this->glInterface(),
2691 stencilSettings.back(),
2692 GR_GL_BACK);
2693 } else {
2694 set_gl_stencil(this->glInterface(),
2695 stencilSettings.front(),
2696 GR_GL_FRONT_AND_BACK);
bsalomon@google.comd302f142011-03-03 13:54:13 +00002697 }
joshualitta58fe352014-10-27 08:39:00 -07002698 fHWStencilSettings = stencilSettings;
reed@google.comac10a2d2010-12-22 21:39:39 +00002699 }
2700}
2701
csmartdaltonc7d85332016-10-26 10:13:46 -07002702void GrGLGpu::disableStencil() {
2703 if (kNo_TriState != fHWStencilTestEnabled) {
2704 GL_CALL(Disable(GR_GL_STENCIL_TEST));
Brian Salomonaf971de2017-06-08 16:11:33 -04002705
csmartdaltonc7d85332016-10-26 10:13:46 -07002706 fHWStencilTestEnabled = kNo_TriState;
2707 fHWStencilSettings.invalidate();
2708 }
2709}
2710
cdaltonaf8bc7d2016-02-05 09:35:20 -08002711void GrGLGpu::flushHWAAState(GrRenderTarget* rt, bool useHWAA, bool stencilEnabled) {
bsalomon083617b2016-02-12 12:10:14 -08002712 // rt is only optional if useHWAA is false.
2713 SkASSERT(rt || !useHWAA);
vbuzinovdded6962015-06-12 08:59:45 -07002714 SkASSERT(!useHWAA || rt->isStencilBufferMultisampled());
bsalomon@google.com202d1392013-03-19 18:58:08 +00002715
csmartdalton2b5f2cb2016-06-10 14:06:32 -07002716 if (this->caps()->multisampleDisableSupport()) {
cdaltond0a840d2015-03-16 17:19:58 -07002717 if (useHWAA) {
2718 if (kYes_TriState != fMSAAEnabled) {
2719 GL_CALL(Enable(GR_GL_MULTISAMPLE));
2720 fMSAAEnabled = kYes_TriState;
2721 }
2722 } else {
2723 if (kNo_TriState != fMSAAEnabled) {
2724 GL_CALL(Disable(GR_GL_MULTISAMPLE));
2725 fMSAAEnabled = kNo_TriState;
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00002726 }
2727 }
2728 }
cdaltonaf8bc7d2016-02-05 09:35:20 -08002729
2730 if (0 != this->caps()->maxRasterSamples()) {
Brian Salomon7c8460e2017-05-12 11:36:10 -04002731 if (useHWAA && GrFSAAType::kMixedSamples == rt->fsaaType() && !stencilEnabled) {
cdaltonaf8bc7d2016-02-05 09:35:20 -08002732 // Since stencil is disabled and we want more samples than are in the color buffer, we
2733 // need to tell the rasterizer explicitly how many to run.
2734 if (kYes_TriState != fHWRasterMultisampleEnabled) {
2735 GL_CALL(Enable(GR_GL_RASTER_MULTISAMPLE));
2736 fHWRasterMultisampleEnabled = kYes_TriState;
2737 }
2738 if (rt->numStencilSamples() != fHWNumRasterSamples) {
2739 SkASSERT(rt->numStencilSamples() <= this->caps()->maxRasterSamples());
2740 GL_CALL(RasterSamples(rt->numStencilSamples(), GR_GL_TRUE));
2741 fHWNumRasterSamples = rt->numStencilSamples();
2742 }
2743 } else {
2744 if (kNo_TriState != fHWRasterMultisampleEnabled) {
2745 GL_CALL(Disable(GR_GL_RASTER_MULTISAMPLE));
2746 fHWRasterMultisampleEnabled = kNo_TriState;
2747 }
2748 }
2749 } else {
Brian Salomon7c8460e2017-05-12 11:36:10 -04002750 SkASSERT(!useHWAA || GrFSAAType::kMixedSamples != rt->fsaaType() || stencilEnabled);
cdaltonaf8bc7d2016-02-05 09:35:20 -08002751 }
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00002752}
2753
bsalomon7f9b2e42016-01-12 13:29:26 -08002754void GrGLGpu::flushBlend(const GrXferProcessor::BlendInfo& blendInfo, const GrSwizzle& swizzle) {
egdanielb414f252014-07-29 13:15:47 -07002755 // Any optimization to disable blending should have already been applied and
cdalton8917d622015-05-06 13:40:21 -07002756 // tweaked the equation to "add" or "subtract", and the coeffs to (1, 0).
bsalomonf7cc8772015-05-11 11:21:14 -07002757
cdalton8917d622015-05-06 13:40:21 -07002758 GrBlendEquation equation = blendInfo.fEquation;
egdanielc2304142014-12-11 13:15:13 -08002759 GrBlendCoeff srcCoeff = blendInfo.fSrcBlend;
2760 GrBlendCoeff dstCoeff = blendInfo.fDstBlend;
cdalton8917d622015-05-06 13:40:21 -07002761 bool blendOff = (kAdd_GrBlendEquation == equation || kSubtract_GrBlendEquation == equation) &&
2762 kOne_GrBlendCoeff == srcCoeff && kZero_GrBlendCoeff == dstCoeff;
egdanielb414f252014-07-29 13:15:47 -07002763 if (blendOff) {
2764 if (kNo_TriState != fHWBlendState.fEnabled) {
2765 GL_CALL(Disable(GR_GL_BLEND));
joel.liang9764c402015-07-09 19:46:18 -07002766
2767 // Workaround for the ARM KHR_blend_equation_advanced blacklist issue
2768 // https://code.google.com/p/skia/issues/detail?id=3943
2769 if (kARM_GrGLVendor == this->ctxInfo().vendor() &&
2770 GrBlendEquationIsAdvanced(fHWBlendState.fEquation)) {
2771 SkASSERT(this->caps()->advancedBlendEquationSupport());
2772 // Set to any basic blending equation.
2773 GrBlendEquation blend_equation = kAdd_GrBlendEquation;
2774 GL_CALL(BlendEquation(gXfermodeEquation2Blend[blend_equation]));
2775 fHWBlendState.fEquation = blend_equation;
2776 }
2777
egdanielb414f252014-07-29 13:15:47 -07002778 fHWBlendState.fEnabled = kNo_TriState;
2779 }
cdalton8917d622015-05-06 13:40:21 -07002780 return;
2781 }
2782
2783 if (kYes_TriState != fHWBlendState.fEnabled) {
2784 GL_CALL(Enable(GR_GL_BLEND));
Brian Salomonaf971de2017-06-08 16:11:33 -04002785
cdalton8917d622015-05-06 13:40:21 -07002786 fHWBlendState.fEnabled = kYes_TriState;
2787 }
2788
2789 if (fHWBlendState.fEquation != equation) {
2790 GL_CALL(BlendEquation(gXfermodeEquation2Blend[equation]));
2791 fHWBlendState.fEquation = equation;
2792 }
2793
2794 if (GrBlendEquationIsAdvanced(equation)) {
2795 SkASSERT(this->caps()->advancedBlendEquationSupport());
2796 // Advanced equations have no other blend state.
2797 return;
2798 }
2799
bsalomone63ffef2016-02-05 07:17:34 -08002800 if (fHWBlendState.fSrcCoeff != srcCoeff || fHWBlendState.fDstCoeff != dstCoeff) {
cdalton8917d622015-05-06 13:40:21 -07002801 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
2802 gXfermodeCoeff2Blend[dstCoeff]));
2803 fHWBlendState.fSrcCoeff = srcCoeff;
2804 fHWBlendState.fDstCoeff = dstCoeff;
2805 }
2806
bsalomon7f9b2e42016-01-12 13:29:26 -08002807 if ((BlendCoeffReferencesConstant(srcCoeff) || BlendCoeffReferencesConstant(dstCoeff))) {
2808 GrColor blendConst = blendInfo.fBlendConstant;
2809 blendConst = swizzle.applyTo(blendConst);
2810 if (!fHWBlendState.fConstColorValid || fHWBlendState.fConstColor != blendConst) {
2811 GrGLfloat c[4];
2812 GrColorToRGBAFloat(blendConst, c);
2813 GL_CALL(BlendColor(c[0], c[1], c[2], c[3]));
2814 fHWBlendState.fConstColor = blendConst;
2815 fHWBlendState.fConstColorValid = true;
2816 }
bsalomon@google.com0650e812011-04-08 18:07:53 +00002817 }
2818}
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002819
commit-bot@chromium.org6364b5e2013-08-20 20:22:52 +00002820static inline GrGLenum tile_to_gl_wrap(SkShader::TileMode tm) {
bsalomon@google.comb8670992012-07-25 21:27:09 +00002821 static const GrGLenum gWrapModes[] = {
2822 GR_GL_CLAMP_TO_EDGE,
2823 GR_GL_REPEAT,
2824 GR_GL_MIRRORED_REPEAT
2825 };
commit-bot@chromium.org5d7ca952013-04-22 20:26:44 +00002826 GR_STATIC_ASSERT(SkShader::kTileModeCount == SK_ARRAY_COUNT(gWrapModes));
bsalomon@google.comb8670992012-07-25 21:27:09 +00002827 GR_STATIC_ASSERT(0 == SkShader::kClamp_TileMode);
2828 GR_STATIC_ASSERT(1 == SkShader::kRepeat_TileMode);
2829 GR_STATIC_ASSERT(2 == SkShader::kMirror_TileMode);
2830 return gWrapModes[tm];
2831}
2832
egdanielb7e7d572015-11-04 04:23:53 -08002833static GrGLenum get_component_enum_from_char(char component) {
2834 switch (component) {
2835 case 'r':
2836 return GR_GL_RED;
2837 case 'g':
2838 return GR_GL_GREEN;
2839 case 'b':
2840 return GR_GL_BLUE;
2841 case 'a':
2842 return GR_GL_ALPHA;
2843 default:
2844 SkFAIL("Unsupported component");
2845 return 0;
2846 }
2847}
2848
2849/** If texture swizzling is available using tex parameters then it is preferred over mangling
2850 the generated shader code. This potentially allows greater reuse of cached shaders. */
2851static void get_tex_param_swizzle(GrPixelConfig config,
bsalomoncdee0092016-01-08 13:20:12 -08002852 const GrGLCaps& caps,
egdanielb7e7d572015-11-04 04:23:53 -08002853 GrGLenum* glSwizzle) {
bsalomoncdee0092016-01-08 13:20:12 -08002854 const GrSwizzle& swizzle = caps.configSwizzle(config);
egdanielb7e7d572015-11-04 04:23:53 -08002855 for (int i = 0; i < 4; ++i) {
bsalomoncdee0092016-01-08 13:20:12 -08002856 glSwizzle[i] = get_component_enum_from_char(swizzle.c_str()[i]);
egdaniel574a4c12015-11-02 06:22:44 -08002857 }
2858}
2859
Brian Salomon514baff2016-11-17 15:17:07 -05002860void GrGLGpu::bindTexture(int unitIdx, const GrSamplerParams& params, bool allowSRGBInputs,
brianosmana6359362016-03-21 06:55:37 -07002861 GrGLTexture* texture) {
bsalomon49f085d2014-09-05 13:34:00 -07002862 SkASSERT(texture);
tomhudson@google.comd0c1a062012-07-12 17:23:52 +00002863
reed856e9d92015-09-30 12:21:45 -07002864#ifdef SK_DEBUG
2865 if (!this->caps()->npotTextureTileSupport()) {
2866 const bool tileX = SkShader::kClamp_TileMode != params.getTileModeX();
2867 const bool tileY = SkShader::kClamp_TileMode != params.getTileModeY();
2868 if (tileX || tileY) {
2869 const int w = texture->width();
2870 const int h = texture->height();
2871 SkASSERT(SkIsPow2(w) && SkIsPow2(h));
2872 }
2873 }
2874#endif
2875
bsalomon@google.comb8670992012-07-25 21:27:09 +00002876 // If we created a rt/tex and rendered to it without using a texture and now we're texturing
2877 // 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 +00002878 // out of the "last != next" check.
bsalomon37dd3312014-11-03 08:47:23 -08002879 GrGLRenderTarget* texRT = static_cast<GrGLRenderTarget*>(texture->asRenderTarget());
bsalomon49f085d2014-09-05 13:34:00 -07002880 if (texRT) {
bsalomon@google.com4c883782012-06-04 19:05:11 +00002881 this->onResolveRenderTarget(texRT);
2882 }
2883
Robert Phillips294870f2016-11-11 12:38:40 -05002884 GrGpuResource::UniqueID textureID = texture->uniqueID();
bsalomon10528f12015-10-14 12:54:52 -07002885 GrGLenum target = texture->target();
bsalomon1c63bf62014-07-22 13:09:46 -07002886 if (fHWBoundTextureUniqueIDs[unitIdx] != textureID) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002887 this->setTextureUnit(unitIdx);
bsalomon10528f12015-10-14 12:54:52 -07002888 GL_CALL(BindTexture(target, texture->textureID()));
bsalomon1c63bf62014-07-22 13:09:46 -07002889 fHWBoundTextureUniqueIDs[unitIdx] = textureID;
bsalomon@google.com4c883782012-06-04 19:05:11 +00002890 }
2891
bsalomon@google.com4c883782012-06-04 19:05:11 +00002892 ResetTimestamp timestamp;
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002893 const GrGLTexture::TexParams& oldTexParams = texture->getCachedTexParams(&timestamp);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002894 bool setAll = timestamp < this->getResetTimestamp();
2895 GrGLTexture::TexParams newTexParams;
2896
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00002897 static GrGLenum glMinFilterModes[] = {
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +00002898 GR_GL_NEAREST,
2899 GR_GL_LINEAR,
2900 GR_GL_LINEAR_MIPMAP_LINEAR
2901 };
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00002902 static GrGLenum glMagFilterModes[] = {
2903 GR_GL_NEAREST,
2904 GR_GL_LINEAR,
2905 GR_GL_LINEAR
2906 };
Brian Salomon514baff2016-11-17 15:17:07 -05002907 GrSamplerParams::FilterMode filterMode = params.filterMode();
bsalomonefd7d452014-10-23 14:17:46 -07002908
Brian Salomon514baff2016-11-17 15:17:07 -05002909 if (GrSamplerParams::kMipMap_FilterMode == filterMode) {
Robert Phillips92de6312017-05-23 07:43:48 -04002910 if (!this->caps()->mipMapSupport()) {
Brian Salomon514baff2016-11-17 15:17:07 -05002911 filterMode = GrSamplerParams::kBilerp_FilterMode;
bsalomonefd7d452014-10-23 14:17:46 -07002912 }
commit-bot@chromium.org47442312013-12-19 16:18:01 +00002913 }
bsalomonefd7d452014-10-23 14:17:46 -07002914
commit-bot@chromium.org47442312013-12-19 16:18:01 +00002915 newTexParams.fMinFilter = glMinFilterModes[filterMode];
2916 newTexParams.fMagFilter = glMagFilterModes[filterMode];
skia.committer@gmail.comaeefb2a2013-07-27 07:01:06 +00002917
brianosman20471892016-12-02 06:43:32 -08002918 if (this->glCaps().srgbDecodeDisableSupport() && GrPixelConfigIsSRGB(texture->config())) {
brianosmanfe199872016-06-13 07:59:48 -07002919 newTexParams.fSRGBDecode = allowSRGBInputs ? GR_GL_DECODE_EXT : GR_GL_SKIP_DECODE_EXT;
brianosman33f6b3f2016-06-02 05:49:21 -07002920 if (setAll || newTexParams.fSRGBDecode != oldTexParams.fSRGBDecode) {
brianosmanadf4fa62016-04-26 16:48:06 -07002921 this->setTextureUnit(unitIdx);
brianosman33f6b3f2016-06-02 05:49:21 -07002922 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SRGB_DECODE_EXT, newTexParams.fSRGBDecode));
cblume55f2d2d2016-02-26 13:20:48 -08002923 }
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +00002924 }
bsalomon@google.com4c883782012-06-04 19:05:11 +00002925
brianosman33f6b3f2016-06-02 05:49:21 -07002926#ifdef SK_DEBUG
2927 // We were supposed to ensure MipMaps were up-to-date and built correctly before getting here.
Brian Salomon514baff2016-11-17 15:17:07 -05002928 if (GrSamplerParams::kMipMap_FilterMode == filterMode) {
brianosman33f6b3f2016-06-02 05:49:21 -07002929 SkASSERT(!texture->texturePriv().mipMapsAreDirty());
2930 if (GrPixelConfigIsSRGB(texture->config())) {
Brian Osman7b8400d2016-11-08 17:08:54 -05002931 SkDestinationSurfaceColorMode colorMode = allowSRGBInputs
2932 ? SkDestinationSurfaceColorMode::kGammaAndColorSpaceAware
2933 : SkDestinationSurfaceColorMode::kLegacy;
2934 SkASSERT(texture->texturePriv().mipColorMode() == colorMode);
brianosman33f6b3f2016-06-02 05:49:21 -07002935 }
2936 }
2937#endif
2938
cblume55f2d2d2016-02-26 13:20:48 -08002939 newTexParams.fMaxMipMapLevel = texture->texturePriv().maxMipMapLevel();
2940
bsalomon@google.comb8670992012-07-25 21:27:09 +00002941 newTexParams.fWrapS = tile_to_gl_wrap(params.getTileModeX());
2942 newTexParams.fWrapT = tile_to_gl_wrap(params.getTileModeY());
bsalomoncdee0092016-01-08 13:20:12 -08002943 get_tex_param_swizzle(texture->config(), this->glCaps(), newTexParams.fSwizzleRGBA);
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00002944 if (setAll || newTexParams.fMagFilter != oldTexParams.fMagFilter) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002945 this->setTextureUnit(unitIdx);
bsalomon10528f12015-10-14 12:54:52 -07002946 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MAG_FILTER, newTexParams.fMagFilter));
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00002947 }
2948 if (setAll || newTexParams.fMinFilter != oldTexParams.fMinFilter) {
2949 this->setTextureUnit(unitIdx);
bsalomon10528f12015-10-14 12:54:52 -07002950 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MIN_FILTER, newTexParams.fMinFilter));
bsalomon@google.com4c883782012-06-04 19:05:11 +00002951 }
cblume55f2d2d2016-02-26 13:20:48 -08002952 if (setAll || newTexParams.fMaxMipMapLevel != oldTexParams.fMaxMipMapLevel) {
cblume09bd2c02016-03-01 14:08:28 -08002953 // These are not supported in ES2 contexts
2954 if (this->glCaps().mipMapLevelAndLodControlSupport()) {
2955 if (newTexParams.fMaxMipMapLevel != 0) {
2956 this->setTextureUnit(unitIdx);
2957 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MIN_LOD, 0));
2958 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_BASE_LEVEL, 0));
2959 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MAX_LOD,
2960 newTexParams.fMaxMipMapLevel));
2961 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MAX_LEVEL,
2962 newTexParams.fMaxMipMapLevel));
2963 }
cblume55f2d2d2016-02-26 13:20:48 -08002964 }
2965 }
bsalomon@google.com4c883782012-06-04 19:05:11 +00002966 if (setAll || newTexParams.fWrapS != oldTexParams.fWrapS) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002967 this->setTextureUnit(unitIdx);
bsalomon10528f12015-10-14 12:54:52 -07002968 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_WRAP_S, newTexParams.fWrapS));
bsalomon@google.com4c883782012-06-04 19:05:11 +00002969 }
2970 if (setAll || newTexParams.fWrapT != oldTexParams.fWrapT) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002971 this->setTextureUnit(unitIdx);
bsalomon10528f12015-10-14 12:54:52 -07002972 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_WRAP_T, newTexParams.fWrapT));
bsalomon@google.com4c883782012-06-04 19:05:11 +00002973 }
bsalomoncdee0092016-01-08 13:20:12 -08002974 if (this->glCaps().textureSwizzleSupport() &&
bsalomon@google.com4c883782012-06-04 19:05:11 +00002975 (setAll || memcmp(newTexParams.fSwizzleRGBA,
2976 oldTexParams.fSwizzleRGBA,
2977 sizeof(newTexParams.fSwizzleRGBA)))) {
cdalton74b8d322016-04-11 14:47:28 -07002978 this->setTextureSwizzle(unitIdx, target, newTexParams.fSwizzleRGBA);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002979 }
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002980 texture->setCachedTexParams(newTexParams, this->getResetTimestamp());
bsalomon@google.com4c883782012-06-04 19:05:11 +00002981}
2982
csmartdalton1897cfd2016-06-03 08:50:54 -07002983void GrGLGpu::bindTexelBuffer(int unitIdx, GrPixelConfig texelConfig, GrGLBuffer* buffer) {
cdalton74b8d322016-04-11 14:47:28 -07002984 SkASSERT(this->glCaps().canUseConfigWithTexelBuffer(texelConfig));
2985 SkASSERT(unitIdx >= 0 && unitIdx < fHWBufferTextures.count());
cdalton74b8d322016-04-11 14:47:28 -07002986
2987 BufferTexture& buffTex = fHWBufferTextures[unitIdx];
2988
2989 if (!buffTex.fKnownBound) {
2990 if (!buffTex.fTextureID) {
2991 GL_CALL(GenTextures(1, &buffTex.fTextureID));
2992 if (!buffTex.fTextureID) {
2993 return;
2994 }
2995 }
2996
2997 this->setTextureUnit(unitIdx);
2998 GL_CALL(BindTexture(GR_GL_TEXTURE_BUFFER, buffTex.fTextureID));
2999
3000 buffTex.fKnownBound = true;
3001 }
3002
robertphillips8abb3702016-08-31 14:04:06 -07003003 if (buffer->uniqueID() != buffTex.fAttachedBufferUniqueID ||
csmartdalton1897cfd2016-06-03 08:50:54 -07003004 buffTex.fTexelConfig != texelConfig) {
cdalton74b8d322016-04-11 14:47:28 -07003005
3006 this->setTextureUnit(unitIdx);
csmartdalton1897cfd2016-06-03 08:50:54 -07003007 GL_CALL(TexBuffer(GR_GL_TEXTURE_BUFFER,
3008 this->glCaps().configSizedInternalFormat(texelConfig),
3009 buffer->bufferID()));
cdalton74b8d322016-04-11 14:47:28 -07003010
cdalton74b8d322016-04-11 14:47:28 -07003011 buffTex.fTexelConfig = texelConfig;
robertphillips8abb3702016-08-31 14:04:06 -07003012 buffTex.fAttachedBufferUniqueID = buffer->uniqueID();
cdalton74b8d322016-04-11 14:47:28 -07003013
3014 if (this->glCaps().textureSwizzleSupport() &&
3015 this->glCaps().configSwizzle(texelConfig) != buffTex.fSwizzle) {
3016 GrGLenum glSwizzle[4];
3017 get_tex_param_swizzle(texelConfig, this->glCaps(), glSwizzle);
3018 this->setTextureSwizzle(unitIdx, GR_GL_TEXTURE_BUFFER, glSwizzle);
3019 buffTex.fSwizzle = this->glCaps().configSwizzle(texelConfig);
3020 }
3021
3022 buffer->setHasAttachedToTexture();
3023 fHWMaxUsedBufferTextureUnit = SkTMax(unitIdx, fHWMaxUsedBufferTextureUnit);
3024 }
3025}
3026
Brian Salomonf9f45122016-11-29 11:59:17 -05003027void GrGLGpu::bindImageStorage(int unitIdx, GrIOType ioType, GrGLTexture *texture) {
3028 SkASSERT(texture);
3029 if (texture->uniqueID() != fHWBoundImageStorages[unitIdx].fTextureUniqueID ||
3030 ioType != fHWBoundImageStorages[unitIdx].fIOType) {
Joe Gregorio94e4ab12016-11-30 14:33:42 -05003031 GrGLenum access = GR_GL_READ_ONLY;
Brian Salomonf9f45122016-11-29 11:59:17 -05003032 switch (ioType) {
3033 case kRead_GrIOType:
3034 access = GR_GL_READ_ONLY;
3035 break;
3036 case kWrite_GrIOType:
3037 access = GR_GL_WRITE_ONLY;
3038 break;
3039 case kRW_GrIOType:
3040 access = GR_GL_READ_WRITE;
3041 break;
3042 }
3043 GrGLenum format = this->glCaps().getImageFormat(texture->config());
3044 GL_CALL(BindImageTexture(unitIdx, texture->textureID(), 0, GR_GL_FALSE, 0, access, format));
3045 }
3046}
3047
Brian Salomon514baff2016-11-17 15:17:07 -05003048void GrGLGpu::generateMipmaps(const GrSamplerParams& params, bool allowSRGBInputs,
brianosman33f6b3f2016-06-02 05:49:21 -07003049 GrGLTexture* texture) {
3050 SkASSERT(texture);
3051
3052 // First, figure out if we need mips for this texture at all:
Brian Salomon514baff2016-11-17 15:17:07 -05003053 GrSamplerParams::FilterMode filterMode = params.filterMode();
brianosman33f6b3f2016-06-02 05:49:21 -07003054
Brian Salomon514baff2016-11-17 15:17:07 -05003055 if (GrSamplerParams::kMipMap_FilterMode == filterMode) {
Robert Phillips92de6312017-05-23 07:43:48 -04003056 if (!this->caps()->mipMapSupport()) {
Brian Salomon514baff2016-11-17 15:17:07 -05003057 filterMode = GrSamplerParams::kBilerp_FilterMode;
brianosman33f6b3f2016-06-02 05:49:21 -07003058 }
3059 }
3060
Brian Salomon514baff2016-11-17 15:17:07 -05003061 if (GrSamplerParams::kMipMap_FilterMode != filterMode) {
brianosman33f6b3f2016-06-02 05:49:21 -07003062 return;
3063 }
3064
3065 // If this is an sRGB texture and the mips were previously built the "other" way
3066 // (gamma-correct vs. not), then we need to rebuild them. We don't need to check for
3067 // srgbSupport - we'll *never* get an sRGB pixel config if we don't support it.
Brian Osman7b8400d2016-11-08 17:08:54 -05003068 SkDestinationSurfaceColorMode colorMode = allowSRGBInputs
3069 ? SkDestinationSurfaceColorMode::kGammaAndColorSpaceAware
3070 : SkDestinationSurfaceColorMode::kLegacy;
brianosman33f6b3f2016-06-02 05:49:21 -07003071 if (GrPixelConfigIsSRGB(texture->config()) &&
Brian Osman7b8400d2016-11-08 17:08:54 -05003072 colorMode != texture->texturePriv().mipColorMode()) {
brianosman33f6b3f2016-06-02 05:49:21 -07003073 texture->texturePriv().dirtyMipMaps(true);
3074 }
3075
3076 // If the mips aren't dirty, we're done:
3077 if (!texture->texturePriv().mipMapsAreDirty()) {
3078 return;
3079 }
3080
3081 // If we created a rt/tex and rendered to it without using a texture and now we're texturing
3082 // from the rt it will still be the last bound texture, but it needs resolving.
3083 GrGLRenderTarget* texRT = static_cast<GrGLRenderTarget*>(texture->asRenderTarget());
3084 if (texRT) {
3085 this->onResolveRenderTarget(texRT);
3086 }
3087
3088 GrGLenum target = texture->target();
3089 this->setScratchTextureUnit();
3090 GL_CALL(BindTexture(target, texture->textureID()));
3091
3092 // Configure sRGB decode, if necessary. This state is the only thing needed for the driver
3093 // call (glGenerateMipmap) to work correctly. Our manual method dirties other state, too.
brianosman20471892016-12-02 06:43:32 -08003094 if (this->glCaps().srgbDecodeDisableSupport() && GrPixelConfigIsSRGB(texture->config())) {
Brian Osmanb0403202016-12-09 10:55:23 -05003095 GrGLenum srgbDecode = allowSRGBInputs ? GR_GL_DECODE_EXT : GR_GL_SKIP_DECODE_EXT;
3096 // Command buffer's sRGB decode extension doesn't influence mipmap generation correctly.
3097 // If we set this to skip_decode, it appears to suppress sRGB -> Linear for each downsample,
3098 // but not the Linear -> sRGB when writing the next level. The result is that mip-chains
3099 // get progressively brighter as you go down. Forcing this to 'decode' gives predictable
3100 // (and only slightly incorrect) results. See crbug.com/655247 (~comment 28)
3101 if (!this->glCaps().srgbDecodeDisableAffectsMipmaps()) {
3102 srgbDecode = GR_GL_DECODE_EXT;
3103 }
3104 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SRGB_DECODE_EXT, srgbDecode));
brianosman33f6b3f2016-06-02 05:49:21 -07003105 }
3106
3107 // Either do manual mipmap generation or (if that fails), just rely on the driver:
3108 if (!this->generateMipmap(texture, allowSRGBInputs)) {
3109 GL_CALL(GenerateMipmap(target));
3110 }
3111
brianosmanfe199872016-06-13 07:59:48 -07003112 texture->texturePriv().dirtyMipMaps(false);
brianosman33f6b3f2016-06-02 05:49:21 -07003113 texture->texturePriv().setMaxMipMapLevel(SkMipMap::ComputeLevelCount(
3114 texture->width(), texture->height()));
Brian Osman7b8400d2016-11-08 17:08:54 -05003115 texture->texturePriv().setMipColorMode(colorMode);
brianosman33f6b3f2016-06-02 05:49:21 -07003116
3117 // We have potentially set lots of state on the texture. Easiest to dirty it all:
3118 texture->textureParamsModified();
3119}
3120
cdalton74b8d322016-04-11 14:47:28 -07003121void GrGLGpu::setTextureSwizzle(int unitIdx, GrGLenum target, const GrGLenum swizzle[]) {
3122 this->setTextureUnit(unitIdx);
3123 if (this->glStandard() == kGLES_GrGLStandard) {
3124 // ES3 added swizzle support but not GL_TEXTURE_SWIZZLE_RGBA.
3125 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_R, swizzle[0]));
3126 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_G, swizzle[1]));
3127 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_B, swizzle[2]));
3128 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_A, swizzle[3]));
3129 } else {
3130 GR_STATIC_ASSERT(sizeof(swizzle[0]) == sizeof(GrGLint));
3131 GL_CALL(TexParameteriv(target, GR_GL_TEXTURE_SWIZZLE_RGBA,
3132 reinterpret_cast<const GrGLint*>(swizzle)));
3133 }
3134}
3135
egdaniel080e6732014-12-22 07:35:52 -08003136void GrGLGpu::flushColorWrite(bool writeColor) {
3137 if (!writeColor) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +00003138 if (kNo_TriState != fHWWriteToColor) {
3139 GL_CALL(ColorMask(GR_GL_FALSE, GR_GL_FALSE,
3140 GR_GL_FALSE, GR_GL_FALSE));
3141 fHWWriteToColor = kNo_TriState;
bsalomon@google.comd302f142011-03-03 13:54:13 +00003142 }
bsalomon@google.com978c8c62012-05-21 14:45:49 +00003143 } else {
3144 if (kYes_TriState != fHWWriteToColor) {
3145 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
3146 fHWWriteToColor = kYes_TriState;
3147 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00003148 }
bsalomon3e791242014-12-17 13:43:13 -08003149}
bsalomon@google.comd302f142011-03-03 13:54:13 +00003150
bsalomon861e1032014-12-16 07:33:49 -08003151void GrGLGpu::setTextureUnit(int unit) {
bsalomon1c63bf62014-07-22 13:09:46 -07003152 SkASSERT(unit >= 0 && unit < fHWBoundTextureUniqueIDs.count());
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00003153 if (unit != fHWActiveTextureUnitIdx) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00003154 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com49209392012-06-05 15:13:46 +00003155 fHWActiveTextureUnitIdx = unit;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00003156 }
3157}
bsalomon@google.com316f99232011-01-13 21:28:12 +00003158
bsalomon861e1032014-12-16 07:33:49 -08003159void GrGLGpu::setScratchTextureUnit() {
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00003160 // Bind the last texture unit since it is the least likely to be used by GrGLProgram.
bsalomon1c63bf62014-07-22 13:09:46 -07003161 int lastUnitIdx = fHWBoundTextureUniqueIDs.count() - 1;
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00003162 if (lastUnitIdx != fHWActiveTextureUnitIdx) {
3163 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + lastUnitIdx));
3164 fHWActiveTextureUnitIdx = lastUnitIdx;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00003165 }
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00003166 // clear out the this field so that if a program does use this unit it will rebind the correct
3167 // texture.
Robert Phillips294870f2016-11-11 12:38:40 -05003168 fHWBoundTextureUniqueIDs[lastUnitIdx].makeInvalid();
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00003169}
3170
Brian Salomone5e7eb12016-10-14 16:18:33 -04003171// Determines whether glBlitFramebuffer could be used between src and dst by onCopySurface.
3172static inline bool can_blit_framebuffer_for_copy_surface(const GrSurface* dst,
3173 const GrSurface* src,
3174 const SkIRect& srcRect,
3175 const SkIPoint& dstPoint,
3176 const GrGLGpu* gpu) {
3177 auto blitFramebufferFlags = gpu->glCaps().blitFramebufferSupportFlags();
Brian Salomon71d9d842016-11-03 13:42:00 -04003178 if (!gpu->glCaps().canConfigBeFBOColorAttachment(dst->config()) ||
3179 !gpu->glCaps().canConfigBeFBOColorAttachment(src->config())) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00003180 return false;
3181 }
Brian Salomonbf7b6202016-11-11 16:08:03 -05003182 // Blits are not allowed between int color buffers and float/fixed color buffers. GrGpu should
3183 // have filtered such cases out.
3184 SkASSERT(GrPixelConfigIsSint(dst->config()) == GrPixelConfigIsSint(src->config()));
Brian Salomone5e7eb12016-10-14 16:18:33 -04003185 const GrGLTexture* dstTex = static_cast<const GrGLTexture*>(dst->asTexture());
3186 const GrGLTexture* srcTex = static_cast<const GrGLTexture*>(dst->asTexture());
3187 const GrRenderTarget* dstRT = dst->asRenderTarget();
3188 const GrRenderTarget* srcRT = src->asRenderTarget();
3189 if (dstTex && dstTex->target() != GR_GL_TEXTURE_2D) {
3190 return false;
3191 }
3192 if (srcTex && srcTex->target() != GR_GL_TEXTURE_2D) {
3193 return false;
3194 }
3195 if (GrGLCaps::kNoSupport_BlitFramebufferFlag & blitFramebufferFlags) {
3196 return false;
3197 }
3198 if (GrGLCaps::kNoScalingOrMirroring_BlitFramebufferFlag & blitFramebufferFlags) {
3199 // We would mirror to compensate for origin changes. Note that copySurface is
3200 // specified such that the src and dst rects are the same.
3201 if (dst->origin() != src->origin()) {
3202 return false;
3203 }
3204 }
3205 if (GrGLCaps::kResolveMustBeFull_BlitFrambufferFlag & blitFramebufferFlags) {
Eric Karl74480882017-04-03 14:49:05 -07003206 if (srcRT && srcRT->numColorSamples()) {
3207 if (dstRT && !dstRT->numColorSamples()) {
3208 return false;
3209 }
3210 if (SkRect::Make(srcRect) != srcRT->getBoundsRect()) {
3211 return false;
3212 }
Brian Salomone5e7eb12016-10-14 16:18:33 -04003213 }
3214 }
3215 if (GrGLCaps::kNoMSAADst_BlitFramebufferFlag & blitFramebufferFlags) {
3216 if (dstRT && dstRT->numColorSamples() > 0) {
3217 return false;
3218 }
3219 }
3220 if (GrGLCaps::kNoFormatConversion_BlitFramebufferFlag & blitFramebufferFlags) {
3221 if (dst->config() != src->config()) {
3222 return false;
3223 }
3224 } else if (GrGLCaps::kNoFormatConversionForMSAASrc_BlitFramebufferFlag & blitFramebufferFlags) {
3225 const GrRenderTarget* srcRT = src->asRenderTarget();
3226 if (srcRT && srcRT->numColorSamples() && dst->config() != src->config()) {
3227 return false;
3228 }
3229 }
3230 if (GrGLCaps::kRectsMustMatchForMSAASrc_BlitFramebufferFlag & blitFramebufferFlags) {
Eric Karl74480882017-04-03 14:49:05 -07003231 if (srcRT && srcRT->numColorSamples()) {
3232 if (dstPoint.fX != srcRect.fLeft || dstPoint.fY != srcRect.fTop) {
3233 return false;
3234 }
3235 if (dst->origin() != src->origin()) {
3236 return false;
3237 }
Brian Salomone5e7eb12016-10-14 16:18:33 -04003238 }
3239 }
3240 return true;
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00003241}
bsalomon@google.comeb851172013-04-15 13:51:00 +00003242
bsalomon7ea33f52015-11-22 14:51:00 -08003243static inline bool can_copy_texsubimage(const GrSurface* dst,
3244 const GrSurface* src,
3245 const GrGLGpu* gpu) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00003246 // Table 3.9 of the ES2 spec indicates the supported formats with CopyTexSubImage
3247 // and BGRA isn't in the spec. There doesn't appear to be any extension that adds it. Perhaps
3248 // many drivers would allow it to work, but ANGLE does not.
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +00003249 if (kGLES_GrGLStandard == gpu->glStandard() && gpu->glCaps().bgraIsInternalFormat() &&
bsalomon@google.comeb851172013-04-15 13:51:00 +00003250 (kBGRA_8888_GrPixelConfig == dst->config() || kBGRA_8888_GrPixelConfig == src->config())) {
3251 return false;
3252 }
3253 const GrGLRenderTarget* dstRT = static_cast<const GrGLRenderTarget*>(dst->asRenderTarget());
3254 // If dst is multisampled (and uses an extension where there is a separate MSAA renderbuffer)
3255 // then we don't want to copy to the texture but to the MSAA buffer.
egdanield803f272015-03-18 13:01:52 -07003256 if (dstRT && dstRT->renderFBOID() != dstRT->textureFBOID()) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00003257 return false;
3258 }
bsalomon@google.coma2719852013-04-17 14:25:27 +00003259 const GrGLRenderTarget* srcRT = static_cast<const GrGLRenderTarget*>(src->asRenderTarget());
3260 // If the src is multisampled (and uses an extension where there is a separate MSAA
3261 // renderbuffer) then it is an invalid operation to call CopyTexSubImage
egdanield803f272015-03-18 13:01:52 -07003262 if (srcRT && srcRT->renderFBOID() != srcRT->textureFBOID()) {
bsalomon@google.coma2719852013-04-17 14:25:27 +00003263 return false;
3264 }
bsalomon7ea33f52015-11-22 14:51:00 -08003265
3266 const GrGLTexture* dstTex = static_cast<const GrGLTexture*>(dst->asTexture());
3267 // CopyTex(Sub)Image writes to a texture and we have no way of dynamically wrapping a RT in a
3268 // texture.
3269 if (!dstTex) {
3270 return false;
3271 }
3272
3273 const GrGLTexture* srcTex = static_cast<const GrGLTexture*>(src->asTexture());
cblume61214052016-01-26 09:10:48 -08003274
bsalomon7ea33f52015-11-22 14:51:00 -08003275 // Check that we could wrap the source in an FBO, that the dst is TEXTURE_2D, that no mirroring
3276 // is required.
Brian Salomon71d9d842016-11-03 13:42:00 -04003277 if (gpu->glCaps().canConfigBeFBOColorAttachment(src->config()) &&
Brian Salomon71d9d842016-11-03 13:42:00 -04003278 (!srcTex || srcTex->target() == GR_GL_TEXTURE_2D) && dstTex->target() == GR_GL_TEXTURE_2D &&
bsalomon7ea33f52015-11-22 14:51:00 -08003279 dst->origin() == src->origin()) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00003280 return true;
3281 } else {
3282 return false;
3283 }
3284}
3285
3286// If a temporary FBO was created, its non-zero ID is returned. The viewport that the copy rect is
3287// relative to is output.
Brian Salomon71d9d842016-11-03 13:42:00 -04003288void GrGLGpu::bindSurfaceFBOForPixelOps(GrSurface* surface, GrGLenum fboTarget, GrGLIRect* viewport,
3289 TempFBOTarget tempFBOTarget) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00003290 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(surface->asRenderTarget());
bsalomon083617b2016-02-12 12:10:14 -08003291 if (!rt) {
bsalomon49f085d2014-09-05 13:34:00 -07003292 SkASSERT(surface->asTexture());
Brian Salomon9bada542017-06-12 12:09:30 -04003293 GrGLTexture* texture = static_cast<GrGLTexture*>(surface->asTexture());
3294 GrGLuint texID = texture->textureID();
3295 GrGLenum target = texture->target();
egdanield803f272015-03-18 13:01:52 -07003296 GrGLuint* tempFBOID;
3297 tempFBOID = kSrc_TempFBOTarget == tempFBOTarget ? &fTempSrcFBOID : &fTempDstFBOID;
egdaniel0f5f9672015-02-03 11:10:51 -08003298
egdanield803f272015-03-18 13:01:52 -07003299 if (0 == *tempFBOID) {
3300 GR_GL_CALL(this->glInterface(), GenFramebuffers(1, tempFBOID));
egdaniel0f5f9672015-02-03 11:10:51 -08003301 }
3302
egdanield803f272015-03-18 13:01:52 -07003303 fStats.incRenderTargetBinds();
3304 GR_GL_CALL(this->glInterface(), BindFramebuffer(fboTarget, *tempFBOID));
3305 GR_GL_CALL(this->glInterface(), FramebufferTexture2D(fboTarget,
robertphillips754f4e92014-09-18 13:52:08 -07003306 GR_GL_COLOR_ATTACHMENT0,
bsalomon10528f12015-10-14 12:54:52 -07003307 target,
robertphillips754f4e92014-09-18 13:52:08 -07003308 texID,
3309 0));
Brian Salomon9bada542017-06-12 12:09:30 -04003310 texture->baseLevelWasBoundToFBO();
bsalomon@google.comeb851172013-04-15 13:51:00 +00003311 viewport->fLeft = 0;
3312 viewport->fBottom = 0;
3313 viewport->fWidth = surface->width();
3314 viewport->fHeight = surface->height();
3315 } else {
egdanield803f272015-03-18 13:01:52 -07003316 fStats.incRenderTargetBinds();
3317 GR_GL_CALL(this->glInterface(), BindFramebuffer(fboTarget, rt->renderFBOID()));
bsalomon@google.comeb851172013-04-15 13:51:00 +00003318 *viewport = rt->getViewport();
3319 }
egdaniel0f5f9672015-02-03 11:10:51 -08003320}
3321
Brian Salomon71d9d842016-11-03 13:42:00 -04003322void GrGLGpu::unbindTextureFBOForPixelOps(GrGLenum fboTarget, GrSurface* surface) {
3323 // bindSurfaceFBOForPixelOps temporarily binds textures that are not render targets to
bsalomon10528f12015-10-14 12:54:52 -07003324 if (!surface->asRenderTarget()) {
3325 SkASSERT(surface->asTexture());
3326 GrGLenum textureTarget = static_cast<GrGLTexture*>(surface->asTexture())->target();
3327 GR_GL_CALL(this->glInterface(), FramebufferTexture2D(fboTarget,
3328 GR_GL_COLOR_ATTACHMENT0,
3329 textureTarget,
3330 0,
3331 0));
3332 }
bsalomon@google.comeb851172013-04-15 13:51:00 +00003333}
3334
joshualitt1cbdcde2015-08-21 11:53:29 -07003335bool GrGLGpu::onCopySurface(GrSurface* dst,
3336 GrSurface* src,
3337 const SkIRect& srcRect,
3338 const SkIPoint& dstPoint) {
bsalomon7f9b2e42016-01-12 13:29:26 -08003339 // None of our copy methods can handle a swizzle. TODO: Make copySurfaceAsDraw handle the
3340 // swizzle.
Brian Salomon1edc5b92016-11-29 13:43:46 -05003341 if (this->caps()->shaderCaps()->configOutputSwizzle(src->config()) !=
3342 this->caps()->shaderCaps()->configOutputSwizzle(dst->config())) {
bsalomon7f9b2e42016-01-12 13:29:26 -08003343 return false;
3344 }
bsalomon083617b2016-02-12 12:10:14 -08003345 // Don't prefer copying as a draw if the dst doesn't already have a FBO object.
3346 bool preferCopy = SkToBool(dst->asRenderTarget());
3347 if (preferCopy && src->asTexture()) {
cdaltone2e71c22016-04-07 18:13:29 -07003348 if (this->copySurfaceAsDraw(dst, src, srcRect, dstPoint)) {
3349 return true;
3350 }
bsalomon5df6fee2015-05-18 06:26:15 -07003351 }
cblume61214052016-01-26 09:10:48 -08003352
bsalomon6df86402015-06-01 10:41:49 -07003353 if (can_copy_texsubimage(dst, src, this)) {
3354 this->copySurfaceAsCopyTexSubImage(dst, src, srcRect, dstPoint);
3355 return true;
3356 }
3357
Brian Salomone5e7eb12016-10-14 16:18:33 -04003358 if (can_blit_framebuffer_for_copy_surface(dst, src, srcRect, dstPoint, this)) {
bsalomon6df86402015-06-01 10:41:49 -07003359 return this->copySurfaceAsBlitFramebuffer(dst, src, srcRect, dstPoint);
3360 }
3361
bsalomon083617b2016-02-12 12:10:14 -08003362 if (!preferCopy && src->asTexture()) {
cdaltone2e71c22016-04-07 18:13:29 -07003363 if (this->copySurfaceAsDraw(dst, src, srcRect, dstPoint)) {
3364 return true;
3365 }
bsalomon083617b2016-02-12 12:10:14 -08003366 }
3367
bsalomon6df86402015-06-01 10:41:49 -07003368 return false;
3369}
3370
Brian Salomonbf7b6202016-11-11 16:08:03 -05003371bool GrGLGpu::createCopyProgram(GrTexture* srcTex) {
3372 int progIdx = TextureToCopyProgramIdx(srcTex);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003373 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
Brian Salomonbf7b6202016-11-11 16:08:03 -05003374 GrSLType samplerType = srcTex->texturePriv().samplerType();
cdaltone2e71c22016-04-07 18:13:29 -07003375
3376 if (!fCopyProgramArrayBuffer) {
3377 static const GrGLfloat vdata[] = {
3378 0, 0,
3379 0, 1,
3380 1, 0,
3381 1, 1
3382 };
3383 fCopyProgramArrayBuffer.reset(GrGLBuffer::Create(this, sizeof(vdata), kVertex_GrBufferType,
3384 kStatic_GrAccessPattern, vdata));
3385 }
3386 if (!fCopyProgramArrayBuffer) {
3387 return false;
3388 }
3389
3390 SkASSERT(!fCopyPrograms[progIdx].fProgram);
3391 GL_CALL_RET(fCopyPrograms[progIdx].fProgram, CreateProgram());
3392 if (!fCopyPrograms[progIdx].fProgram) {
3393 return false;
3394 }
3395
Brian Salomon1edc5b92016-11-29 13:43:46 -05003396 const char* version = shaderCaps->versionDeclString();
Brian Salomon99938a82016-11-21 13:41:08 -05003397 GrShaderVar aVertex("a_vertex", kVec2f_GrSLType, GrShaderVar::kIn_TypeModifier);
3398 GrShaderVar uTexCoordXform("u_texCoordXform", kVec4f_GrSLType,
3399 GrShaderVar::kUniform_TypeModifier);
3400 GrShaderVar uPosXform("u_posXform", kVec4f_GrSLType, GrShaderVar::kUniform_TypeModifier);
3401 GrShaderVar uTexture("u_texture", samplerType, GrShaderVar::kUniform_TypeModifier);
3402 GrShaderVar vTexCoord("v_texCoord", kVec2f_GrSLType, GrShaderVar::kOut_TypeModifier);
3403 GrShaderVar oFragColor("o_FragColor", kVec4f_GrSLType, GrShaderVar::kOut_TypeModifier);
cdaltone2e71c22016-04-07 18:13:29 -07003404
3405 SkString vshaderTxt(version);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003406 if (shaderCaps->noperspectiveInterpolationSupport()) {
3407 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
cdaltone2e71c22016-04-07 18:13:29 -07003408 vshaderTxt.appendf("#extension %s : require\n", extension);
3409 }
3410 vTexCoord.addModifier("noperspective");
3411 }
3412
Brian Salomon1edc5b92016-11-29 13:43:46 -05003413 aVertex.appendDecl(shaderCaps, &vshaderTxt);
cdaltone2e71c22016-04-07 18:13:29 -07003414 vshaderTxt.append(";");
Brian Salomon1edc5b92016-11-29 13:43:46 -05003415 uTexCoordXform.appendDecl(shaderCaps, &vshaderTxt);
cdaltone2e71c22016-04-07 18:13:29 -07003416 vshaderTxt.append(";");
Brian Salomon1edc5b92016-11-29 13:43:46 -05003417 uPosXform.appendDecl(shaderCaps, &vshaderTxt);
cdaltone2e71c22016-04-07 18:13:29 -07003418 vshaderTxt.append(";");
Brian Salomon1edc5b92016-11-29 13:43:46 -05003419 vTexCoord.appendDecl(shaderCaps, &vshaderTxt);
cdaltone2e71c22016-04-07 18:13:29 -07003420 vshaderTxt.append(";");
3421
3422 vshaderTxt.append(
3423 "// Copy Program VS\n"
3424 "void main() {"
3425 " v_texCoord = a_vertex.xy * u_texCoordXform.xy + u_texCoordXform.zw;"
3426 " gl_Position.xy = a_vertex * u_posXform.xy + u_posXform.zw;"
3427 " gl_Position.zw = vec2(0, 1);"
3428 "}"
3429 );
3430
3431 SkString fshaderTxt(version);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003432 if (shaderCaps->noperspectiveInterpolationSupport()) {
3433 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
cdaltone2e71c22016-04-07 18:13:29 -07003434 fshaderTxt.appendf("#extension %s : require\n", extension);
3435 }
3436 }
Brian Salomonbf7b6202016-11-11 16:08:03 -05003437 if (samplerType == kTextureExternalSampler_GrSLType) {
cdaltone2e71c22016-04-07 18:13:29 -07003438 fshaderTxt.appendf("#extension %s : require\n",
Brian Salomon1edc5b92016-11-29 13:43:46 -05003439 shaderCaps->externalTextureExtensionString());
cdaltone2e71c22016-04-07 18:13:29 -07003440 }
Brian Osman33aa2c72017-04-05 09:26:15 -04003441 GrGLSLAppendDefaultFloatPrecisionDeclaration(kMedium_GrSLPrecision, *shaderCaps,
cdaltone2e71c22016-04-07 18:13:29 -07003442 &fshaderTxt);
Brian Salomonf31ae492016-11-18 15:35:33 -05003443 vTexCoord.setTypeModifier(GrShaderVar::kIn_TypeModifier);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003444 vTexCoord.appendDecl(shaderCaps, &fshaderTxt);
cdaltone2e71c22016-04-07 18:13:29 -07003445 fshaderTxt.append(";");
Brian Salomon1edc5b92016-11-29 13:43:46 -05003446 uTexture.appendDecl(shaderCaps, &fshaderTxt);
cdaltone2e71c22016-04-07 18:13:29 -07003447 fshaderTxt.append(";");
cdaltone2e71c22016-04-07 18:13:29 -07003448 fshaderTxt.appendf(
3449 "// Copy Program FS\n"
3450 "void main() {"
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003451 " sk_FragColor = texture(u_texture, v_texCoord);"
3452 "}"
cdaltone2e71c22016-04-07 18:13:29 -07003453 );
3454
3455 const char* str;
3456 GrGLint length;
3457
3458 str = vshaderTxt.c_str();
3459 length = SkToInt(vshaderTxt.size());
Ethan Nicholas941e7e22016-12-12 15:33:30 -05003460 SkSL::Program::Settings settings;
3461 settings.fCaps = shaderCaps;
3462 SkSL::Program::Inputs inputs;
cdaltone2e71c22016-04-07 18:13:29 -07003463 GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fCopyPrograms[progIdx].fProgram,
3464 GR_GL_VERTEX_SHADER, &str, &length, 1,
Ethan Nicholas941e7e22016-12-12 15:33:30 -05003465 &fStats, settings, &inputs);
3466 SkASSERT(inputs.isEmpty());
cdaltone2e71c22016-04-07 18:13:29 -07003467
3468 str = fshaderTxt.c_str();
3469 length = SkToInt(fshaderTxt.size());
3470 GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fCopyPrograms[progIdx].fProgram,
3471 GR_GL_FRAGMENT_SHADER, &str, &length, 1,
Ethan Nicholas941e7e22016-12-12 15:33:30 -05003472 &fStats, settings, &inputs);
3473 SkASSERT(inputs.isEmpty());
cdaltone2e71c22016-04-07 18:13:29 -07003474
3475 GL_CALL(LinkProgram(fCopyPrograms[progIdx].fProgram));
3476
3477 GL_CALL_RET(fCopyPrograms[progIdx].fTextureUniform,
3478 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_texture"));
3479 GL_CALL_RET(fCopyPrograms[progIdx].fPosXformUniform,
3480 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_posXform"));
3481 GL_CALL_RET(fCopyPrograms[progIdx].fTexCoordXformUniform,
3482 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_texCoordXform"));
3483
3484 GL_CALL(BindAttribLocation(fCopyPrograms[progIdx].fProgram, 0, "a_vertex"));
3485
3486 GL_CALL(DeleteShader(vshader));
3487 GL_CALL(DeleteShader(fshader));
3488
3489 return true;
bsalomon6df86402015-06-01 10:41:49 -07003490}
3491
brianosman33f6b3f2016-06-02 05:49:21 -07003492bool GrGLGpu::createMipmapProgram(int progIdx) {
3493 const bool oddWidth = SkToBool(progIdx & 0x2);
3494 const bool oddHeight = SkToBool(progIdx & 0x1);
3495 const int numTaps = (oddWidth ? 2 : 1) * (oddHeight ? 2 : 1);
3496
Brian Salomon1edc5b92016-11-29 13:43:46 -05003497 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
brianosman33f6b3f2016-06-02 05:49:21 -07003498
3499 SkASSERT(!fMipmapPrograms[progIdx].fProgram);
3500 GL_CALL_RET(fMipmapPrograms[progIdx].fProgram, CreateProgram());
3501 if (!fMipmapPrograms[progIdx].fProgram) {
3502 return false;
3503 }
3504
Brian Salomon1edc5b92016-11-29 13:43:46 -05003505 const char* version = shaderCaps->versionDeclString();
Brian Salomon99938a82016-11-21 13:41:08 -05003506 GrShaderVar aVertex("a_vertex", kVec2f_GrSLType, GrShaderVar::kIn_TypeModifier);
3507 GrShaderVar uTexCoordXform("u_texCoordXform", kVec4f_GrSLType,
3508 GrShaderVar::kUniform_TypeModifier);
3509 GrShaderVar uTexture("u_texture", kTexture2DSampler_GrSLType,
3510 GrShaderVar::kUniform_TypeModifier);
brianosman33f6b3f2016-06-02 05:49:21 -07003511 // We need 1, 2, or 4 texture coordinates (depending on parity of each dimension):
Brian Salomon99938a82016-11-21 13:41:08 -05003512 GrShaderVar vTexCoords[] = {
3513 GrShaderVar("v_texCoord0", kVec2f_GrSLType, GrShaderVar::kOut_TypeModifier),
3514 GrShaderVar("v_texCoord1", kVec2f_GrSLType, GrShaderVar::kOut_TypeModifier),
3515 GrShaderVar("v_texCoord2", kVec2f_GrSLType, GrShaderVar::kOut_TypeModifier),
3516 GrShaderVar("v_texCoord3", kVec2f_GrSLType, GrShaderVar::kOut_TypeModifier),
brianosman33f6b3f2016-06-02 05:49:21 -07003517 };
Brian Salomon99938a82016-11-21 13:41:08 -05003518 GrShaderVar oFragColor("o_FragColor", kVec4f_GrSLType,GrShaderVar::kOut_TypeModifier);
brianosman33f6b3f2016-06-02 05:49:21 -07003519
3520 SkString vshaderTxt(version);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003521 if (shaderCaps->noperspectiveInterpolationSupport()) {
3522 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
brianosman33f6b3f2016-06-02 05:49:21 -07003523 vshaderTxt.appendf("#extension %s : require\n", extension);
3524 }
3525 vTexCoords[0].addModifier("noperspective");
3526 vTexCoords[1].addModifier("noperspective");
3527 vTexCoords[2].addModifier("noperspective");
3528 vTexCoords[3].addModifier("noperspective");
3529 }
3530
Brian Salomon1edc5b92016-11-29 13:43:46 -05003531 aVertex.appendDecl(shaderCaps, &vshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003532 vshaderTxt.append(";");
Brian Salomon1edc5b92016-11-29 13:43:46 -05003533 uTexCoordXform.appendDecl(shaderCaps, &vshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003534 vshaderTxt.append(";");
3535 for (int i = 0; i < numTaps; ++i) {
Brian Salomon1edc5b92016-11-29 13:43:46 -05003536 vTexCoords[i].appendDecl(shaderCaps, &vshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003537 vshaderTxt.append(";");
3538 }
3539
3540 vshaderTxt.append(
3541 "// Mipmap Program VS\n"
3542 "void main() {"
3543 " gl_Position.xy = a_vertex * vec2(2, 2) - vec2(1, 1);"
3544 " gl_Position.zw = vec2(0, 1);"
3545 );
3546
3547 // Insert texture coordinate computation:
3548 if (oddWidth && oddHeight) {
3549 vshaderTxt.append(
3550 " v_texCoord0 = a_vertex.xy * u_texCoordXform.yw;"
3551 " v_texCoord1 = a_vertex.xy * u_texCoordXform.yw + vec2(u_texCoordXform.x, 0);"
3552 " v_texCoord2 = a_vertex.xy * u_texCoordXform.yw + vec2(0, u_texCoordXform.z);"
3553 " v_texCoord3 = a_vertex.xy * u_texCoordXform.yw + u_texCoordXform.xz;"
3554 );
3555 } else if (oddWidth) {
3556 vshaderTxt.append(
3557 " v_texCoord0 = a_vertex.xy * vec2(u_texCoordXform.y, 1);"
3558 " v_texCoord1 = a_vertex.xy * vec2(u_texCoordXform.y, 1) + vec2(u_texCoordXform.x, 0);"
3559 );
3560 } else if (oddHeight) {
3561 vshaderTxt.append(
3562 " v_texCoord0 = a_vertex.xy * vec2(1, u_texCoordXform.w);"
3563 " v_texCoord1 = a_vertex.xy * vec2(1, u_texCoordXform.w) + vec2(0, u_texCoordXform.z);"
3564 );
3565 } else {
3566 vshaderTxt.append(
3567 " v_texCoord0 = a_vertex.xy;"
3568 );
3569 }
3570
3571 vshaderTxt.append("}");
3572
3573 SkString fshaderTxt(version);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003574 if (shaderCaps->noperspectiveInterpolationSupport()) {
3575 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
brianosman33f6b3f2016-06-02 05:49:21 -07003576 fshaderTxt.appendf("#extension %s : require\n", extension);
3577 }
3578 }
Brian Osman33aa2c72017-04-05 09:26:15 -04003579 GrGLSLAppendDefaultFloatPrecisionDeclaration(kMedium_GrSLPrecision, *shaderCaps,
brianosman33f6b3f2016-06-02 05:49:21 -07003580 &fshaderTxt);
3581 for (int i = 0; i < numTaps; ++i) {
Brian Salomonf31ae492016-11-18 15:35:33 -05003582 vTexCoords[i].setTypeModifier(GrShaderVar::kIn_TypeModifier);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003583 vTexCoords[i].appendDecl(shaderCaps, &fshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003584 fshaderTxt.append(";");
3585 }
Brian Salomon1edc5b92016-11-29 13:43:46 -05003586 uTexture.appendDecl(shaderCaps, &fshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003587 fshaderTxt.append(";");
brianosman33f6b3f2016-06-02 05:49:21 -07003588 fshaderTxt.append(
3589 "// Mipmap Program FS\n"
3590 "void main() {"
3591 );
3592
3593 if (oddWidth && oddHeight) {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003594 fshaderTxt.append(
3595 " sk_FragColor = (texture(u_texture, v_texCoord0) + "
3596 " texture(u_texture, v_texCoord1) + "
3597 " texture(u_texture, v_texCoord2) + "
3598 " texture(u_texture, v_texCoord3)) * 0.25;"
brianosman33f6b3f2016-06-02 05:49:21 -07003599 );
3600 } else if (oddWidth || oddHeight) {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003601 fshaderTxt.append(
3602 " sk_FragColor = (texture(u_texture, v_texCoord0) + "
3603 " texture(u_texture, v_texCoord1)) * 0.5;"
brianosman33f6b3f2016-06-02 05:49:21 -07003604 );
3605 } else {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003606 fshaderTxt.append(
3607 " sk_FragColor = texture(u_texture, v_texCoord0);"
brianosman33f6b3f2016-06-02 05:49:21 -07003608 );
3609 }
3610
3611 fshaderTxt.append("}");
3612
3613 const char* str;
3614 GrGLint length;
3615
3616 str = vshaderTxt.c_str();
3617 length = SkToInt(vshaderTxt.size());
Ethan Nicholas941e7e22016-12-12 15:33:30 -05003618 SkSL::Program::Settings settings;
3619 settings.fCaps = shaderCaps;
3620 SkSL::Program::Inputs inputs;
brianosman33f6b3f2016-06-02 05:49:21 -07003621 GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fMipmapPrograms[progIdx].fProgram,
3622 GR_GL_VERTEX_SHADER, &str, &length, 1,
Ethan Nicholas941e7e22016-12-12 15:33:30 -05003623 &fStats, settings, &inputs);
3624 SkASSERT(inputs.isEmpty());
brianosman33f6b3f2016-06-02 05:49:21 -07003625
3626 str = fshaderTxt.c_str();
3627 length = SkToInt(fshaderTxt.size());
3628 GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fMipmapPrograms[progIdx].fProgram,
3629 GR_GL_FRAGMENT_SHADER, &str, &length, 1,
Ethan Nicholas941e7e22016-12-12 15:33:30 -05003630 &fStats, settings, &inputs);
3631 SkASSERT(inputs.isEmpty());
brianosman33f6b3f2016-06-02 05:49:21 -07003632
3633 GL_CALL(LinkProgram(fMipmapPrograms[progIdx].fProgram));
3634
3635 GL_CALL_RET(fMipmapPrograms[progIdx].fTextureUniform,
3636 GetUniformLocation(fMipmapPrograms[progIdx].fProgram, "u_texture"));
3637 GL_CALL_RET(fMipmapPrograms[progIdx].fTexCoordXformUniform,
3638 GetUniformLocation(fMipmapPrograms[progIdx].fProgram, "u_texCoordXform"));
3639
3640 GL_CALL(BindAttribLocation(fMipmapPrograms[progIdx].fProgram, 0, "a_vertex"));
3641
3642 GL_CALL(DeleteShader(vshader));
3643 GL_CALL(DeleteShader(fshader));
3644
3645 return true;
3646}
3647
Mike Klein31550db2017-06-06 23:29:53 +00003648bool GrGLGpu::createStencilClipClearProgram() {
3649 if (!fStencilClipClearArrayBuffer) {
3650 static const GrGLfloat vdata[] = {-1, -1, 1, -1, -1, 1, 1, 1};
3651 fStencilClipClearArrayBuffer.reset(GrGLBuffer::Create(
3652 this, sizeof(vdata), kVertex_GrBufferType, kStatic_GrAccessPattern, vdata));
3653 if (!fStencilClipClearArrayBuffer) {
cdaltone2e71c22016-04-07 18:13:29 -07003654 return false;
3655 }
3656 }
3657
Mike Klein31550db2017-06-06 23:29:53 +00003658 SkASSERT(!fStencilClipClearProgram);
3659 GL_CALL_RET(fStencilClipClearProgram, CreateProgram());
3660 if (!fStencilClipClearProgram) {
cdaltone2e71c22016-04-07 18:13:29 -07003661 return false;
3662 }
3663
Brian Salomon99938a82016-11-21 13:41:08 -05003664 GrShaderVar aVertex("a_vertex", kVec2f_GrSLType, GrShaderVar::kIn_TypeModifier);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003665 const char* version = this->caps()->shaderCaps()->versionDeclString();
bsalomon6dea83f2015-12-03 12:58:06 -08003666
bsalomon6dea83f2015-12-03 12:58:06 -08003667 SkString vshaderTxt(version);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003668 aVertex.appendDecl(this->caps()->shaderCaps(), &vshaderTxt);
bsalomon6dea83f2015-12-03 12:58:06 -08003669 vshaderTxt.append(";");
bsalomon6dea83f2015-12-03 12:58:06 -08003670 vshaderTxt.append(
Mike Klein31550db2017-06-06 23:29:53 +00003671 "// Stencil Clip Clear Program VS\n"
3672 "void main() {"
3673 " gl_Position = vec4(a_vertex.x, a_vertex.y, 0, 1);"
3674 "}");
bsalomon6dea83f2015-12-03 12:58:06 -08003675
3676 SkString fshaderTxt(version);
Brian Osman33aa2c72017-04-05 09:26:15 -04003677 GrGLSLAppendDefaultFloatPrecisionDeclaration(kMedium_GrSLPrecision,
Brian Salomon1edc5b92016-11-29 13:43:46 -05003678 *this->caps()->shaderCaps(),
bsalomon6dea83f2015-12-03 12:58:06 -08003679 &fshaderTxt);
bsalomon6dea83f2015-12-03 12:58:06 -08003680 fshaderTxt.appendf(
Mike Klein31550db2017-06-06 23:29:53 +00003681 "// Stencil Clip Clear Program FS\n"
3682 "void main() {"
3683 " sk_FragColor = vec4(0);"
3684 "}");
bsalomon6dea83f2015-12-03 12:58:06 -08003685
bsalomon6dea83f2015-12-03 12:58:06 -08003686 const char* str;
3687 GrGLint length;
3688
3689 str = vshaderTxt.c_str();
3690 length = SkToInt(vshaderTxt.size());
Ethan Nicholas941e7e22016-12-12 15:33:30 -05003691 SkSL::Program::Settings settings;
3692 settings.fCaps = this->caps()->shaderCaps();
3693 SkSL::Program::Inputs inputs;
Mike Klein31550db2017-06-06 23:29:53 +00003694 GrGLuint vshader =
3695 GrGLCompileAndAttachShader(*fGLContext, fStencilClipClearProgram, GR_GL_VERTEX_SHADER,
3696 &str, &length, 1, &fStats, settings, &inputs);
Ethan Nicholas941e7e22016-12-12 15:33:30 -05003697 SkASSERT(inputs.isEmpty());
bsalomon6dea83f2015-12-03 12:58:06 -08003698
3699 str = fshaderTxt.c_str();
3700 length = SkToInt(fshaderTxt.size());
Mike Klein31550db2017-06-06 23:29:53 +00003701 GrGLuint fshader =
3702 GrGLCompileAndAttachShader(*fGLContext, fStencilClipClearProgram, GR_GL_FRAGMENT_SHADER,
3703 &str, &length, 1, &fStats, settings, &inputs);
Ethan Nicholas941e7e22016-12-12 15:33:30 -05003704 SkASSERT(inputs.isEmpty());
bsalomon6dea83f2015-12-03 12:58:06 -08003705
Mike Klein31550db2017-06-06 23:29:53 +00003706 GL_CALL(LinkProgram(fStencilClipClearProgram));
bsalomon6dea83f2015-12-03 12:58:06 -08003707
Mike Klein31550db2017-06-06 23:29:53 +00003708 GL_CALL(BindAttribLocation(fStencilClipClearProgram, 0, "a_vertex"));
bsalomon6dea83f2015-12-03 12:58:06 -08003709
3710 GL_CALL(DeleteShader(vshader));
3711 GL_CALL(DeleteShader(fshader));
cdaltone2e71c22016-04-07 18:13:29 -07003712
3713 return true;
bsalomon6dea83f2015-12-03 12:58:06 -08003714}
3715
Mike Klein31550db2017-06-06 23:29:53 +00003716void GrGLGpu::clearStencilClipAsDraw(const GrFixedClip& clip, bool insideStencilMask,
3717 GrRenderTarget* rt) {
bsalomon7f9b2e42016-01-12 13:29:26 -08003718 // TODO: This should swizzle the output to match dst's config, though it is a debugging
3719 // visualization.
3720
bsalomon6dea83f2015-12-03 12:58:06 -08003721 this->handleDirtyContext();
Mike Klein31550db2017-06-06 23:29:53 +00003722 if (!fStencilClipClearProgram) {
3723 if (!this->createStencilClipClearProgram()) {
3724 SkDebugf("Failed to create stencil clip clear program.\n");
cdaltone2e71c22016-04-07 18:13:29 -07003725 return;
3726 }
bsalomon6dea83f2015-12-03 12:58:06 -08003727 }
3728
Mike Klein8b06ed72017-06-06 21:47:32 +00003729 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(rt->asRenderTarget());
Mike Klein31550db2017-06-06 23:29:53 +00003730 this->flushRenderTarget(glRT, nullptr);
Mike Klein8b06ed72017-06-06 21:47:32 +00003731
Mike Klein31550db2017-06-06 23:29:53 +00003732 GL_CALL(UseProgram(fStencilClipClearProgram));
3733 fHWProgramID = fStencilClipClearProgram;
bsalomon6dea83f2015-12-03 12:58:06 -08003734
cdaltone2e71c22016-04-07 18:13:29 -07003735 fHWVertexArrayState.setVertexArrayID(this, 0);
bsalomon6dea83f2015-12-03 12:58:06 -08003736
cdaltone2e71c22016-04-07 18:13:29 -07003737 GrGLAttribArrayState* attribs = fHWVertexArrayState.bindInternalVertexArray(this);
Chris Dalton8e45b4f2017-05-05 14:00:56 -04003738 attribs->enableVertexArrays(this, 1);
Mike Klein31550db2017-06-06 23:29:53 +00003739 attribs->set(this, 0, fStencilClipClearArrayBuffer.get(), kVec2f_GrVertexAttribType,
Hal Canary144caf52016-11-07 17:57:18 -05003740 2 * sizeof(GrGLfloat), 0);
bsalomon6dea83f2015-12-03 12:58:06 -08003741
bsalomon6dea83f2015-12-03 12:58:06 -08003742 GrXferProcessor::BlendInfo blendInfo;
3743 blendInfo.reset();
bsalomon7f9b2e42016-01-12 13:29:26 -08003744 this->flushBlend(blendInfo, GrSwizzle::RGBA());
Mike Klein31550db2017-06-06 23:29:53 +00003745 this->flushColorWrite(false);
cdaltonaf8bc7d2016-02-05 09:35:20 -08003746 this->flushHWAAState(glRT, false, false);
Mike Klein31550db2017-06-06 23:29:53 +00003747 this->flushScissor(clip.scissorState(), glRT->getViewport(), glRT->origin());
3748 this->flushWindowRectangles(clip.windowRectsState(), glRT);
3749 GrStencilAttachment* sb = rt->renderTargetPriv().getStencilAttachment();
3750 // This should only be called internally when we know we have a stencil buffer.
3751 SkASSERT(sb);
3752 GrStencilSettings settings = GrStencilSettings(
3753 *GrStencilSettings::SetClipBitSettings(insideStencilMask), false, sb->bits());
3754 this->flushStencil(settings);
3755 GL_CALL(DrawArrays(GR_GL_TRIANGLE_STRIP, 0, 4));
bsalomon6dea83f2015-12-03 12:58:06 -08003756}
3757
3758
cdaltone2e71c22016-04-07 18:13:29 -07003759bool GrGLGpu::copySurfaceAsDraw(GrSurface* dst,
bsalomon6df86402015-06-01 10:41:49 -07003760 GrSurface* src,
3761 const SkIRect& srcRect,
3762 const SkIPoint& dstPoint) {
cdaltone2e71c22016-04-07 18:13:29 -07003763 GrGLTexture* srcTex = static_cast<GrGLTexture*>(src->asTexture());
Brian Salomonbf7b6202016-11-11 16:08:03 -05003764 int progIdx = TextureToCopyProgramIdx(srcTex);
cdaltone2e71c22016-04-07 18:13:29 -07003765
3766 if (!fCopyPrograms[progIdx].fProgram) {
Brian Salomonbf7b6202016-11-11 16:08:03 -05003767 if (!this->createCopyProgram(srcTex)) {
cdaltone2e71c22016-04-07 18:13:29 -07003768 SkDebugf("Failed to create copy program.\n");
3769 return false;
3770 }
3771 }
3772
bsalomon6df86402015-06-01 10:41:49 -07003773 int w = srcRect.width();
3774 int h = srcRect.height();
3775
Brian Salomon514baff2016-11-17 15:17:07 -05003776 GrSamplerParams params(SkShader::kClamp_TileMode, GrSamplerParams::kNone_FilterMode);
brianosmana6359362016-03-21 06:55:37 -07003777 this->bindTexture(0, params, true, srcTex);
bsalomon6df86402015-06-01 10:41:49 -07003778
bsalomon083617b2016-02-12 12:10:14 -08003779 GrGLIRect dstVP;
Brian Salomon71d9d842016-11-03 13:42:00 -04003780 this->bindSurfaceFBOForPixelOps(dst, GR_GL_FRAMEBUFFER, &dstVP, kDst_TempFBOTarget);
bsalomon083617b2016-02-12 12:10:14 -08003781 this->flushViewport(dstVP);
Robert Phillips294870f2016-11-11 12:38:40 -05003782 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon083617b2016-02-12 12:10:14 -08003783
bsalomon6df86402015-06-01 10:41:49 -07003784 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY, w, h);
bsalomon6df86402015-06-01 10:41:49 -07003785
bsalomon7ea33f52015-11-22 14:51:00 -08003786 GL_CALL(UseProgram(fCopyPrograms[progIdx].fProgram));
3787 fHWProgramID = fCopyPrograms[progIdx].fProgram;
bsalomon6df86402015-06-01 10:41:49 -07003788
cdaltone2e71c22016-04-07 18:13:29 -07003789 fHWVertexArrayState.setVertexArrayID(this, 0);
bsalomon6df86402015-06-01 10:41:49 -07003790
cdaltone2e71c22016-04-07 18:13:29 -07003791 GrGLAttribArrayState* attribs = fHWVertexArrayState.bindInternalVertexArray(this);
Chris Dalton8e45b4f2017-05-05 14:00:56 -04003792 attribs->enableVertexArrays(this, 1);
Hal Canary144caf52016-11-07 17:57:18 -05003793 attribs->set(this, 0, fCopyProgramArrayBuffer.get(), kVec2f_GrVertexAttribType,
3794 2 * sizeof(GrGLfloat), 0);
bsalomon6df86402015-06-01 10:41:49 -07003795
3796 // dst rect edges in NDC (-1 to 1)
3797 int dw = dst->width();
3798 int dh = dst->height();
3799 GrGLfloat dx0 = 2.f * dstPoint.fX / dw - 1.f;
3800 GrGLfloat dx1 = 2.f * (dstPoint.fX + w) / dw - 1.f;
3801 GrGLfloat dy0 = 2.f * dstPoint.fY / dh - 1.f;
3802 GrGLfloat dy1 = 2.f * (dstPoint.fY + h) / dh - 1.f;
3803 if (kBottomLeft_GrSurfaceOrigin == dst->origin()) {
3804 dy0 = -dy0;
3805 dy1 = -dy1;
3806 }
3807
bsalomone5286e02016-01-14 09:24:09 -08003808 GrGLfloat sx0 = (GrGLfloat)srcRect.fLeft;
3809 GrGLfloat sx1 = (GrGLfloat)(srcRect.fLeft + w);
3810 GrGLfloat sy0 = (GrGLfloat)srcRect.fTop;
3811 GrGLfloat sy1 = (GrGLfloat)(srcRect.fTop + h);
Ethan Nicholas5338f992017-04-19 15:54:07 -04003812 int sw = src->width();
bsalomon6df86402015-06-01 10:41:49 -07003813 int sh = src->height();
bsalomon6df86402015-06-01 10:41:49 -07003814 if (kBottomLeft_GrSurfaceOrigin == src->origin()) {
bsalomone5286e02016-01-14 09:24:09 -08003815 sy0 = sh - sy0;
3816 sy1 = sh - sy1;
3817 }
Ethan Nicholas5338f992017-04-19 15:54:07 -04003818 // src rect edges in normalized texture space (0 to 1)
3819 sx0 /= sw;
3820 sx1 /= sw;
3821 sy0 /= sh;
3822 sy1 /= sh;
bsalomon6df86402015-06-01 10:41:49 -07003823
bsalomon7ea33f52015-11-22 14:51:00 -08003824 GL_CALL(Uniform4f(fCopyPrograms[progIdx].fPosXformUniform, dx1 - dx0, dy1 - dy0, dx0, dy0));
3825 GL_CALL(Uniform4f(fCopyPrograms[progIdx].fTexCoordXformUniform,
3826 sx1 - sx0, sy1 - sy0, sx0, sy0));
3827 GL_CALL(Uniform1i(fCopyPrograms[progIdx].fTextureUniform, 0));
bsalomon6df86402015-06-01 10:41:49 -07003828
3829 GrXferProcessor::BlendInfo blendInfo;
3830 blendInfo.reset();
bsalomon7f9b2e42016-01-12 13:29:26 -08003831 this->flushBlend(blendInfo, GrSwizzle::RGBA());
bsalomon6df86402015-06-01 10:41:49 -07003832 this->flushColorWrite(true);
bsalomon083617b2016-02-12 12:10:14 -08003833 this->flushHWAAState(nullptr, false, false);
bsalomon6df86402015-06-01 10:41:49 -07003834 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07003835 this->disableWindowRectangles();
csmartdaltonc7d85332016-10-26 10:13:46 -07003836 this->disableStencil();
bsalomon6df86402015-06-01 10:41:49 -07003837
3838 GL_CALL(DrawArrays(GR_GL_TRIANGLE_STRIP, 0, 4));
Brian Salomon71d9d842016-11-03 13:42:00 -04003839 this->unbindTextureFBOForPixelOps(GR_GL_FRAMEBUFFER, dst);
bsalomon083617b2016-02-12 12:10:14 -08003840 this->didWriteToSurface(dst, &dstRect);
3841
cdaltone2e71c22016-04-07 18:13:29 -07003842 return true;
bsalomon6df86402015-06-01 10:41:49 -07003843}
3844
3845void GrGLGpu::copySurfaceAsCopyTexSubImage(GrSurface* dst,
3846 GrSurface* src,
3847 const SkIRect& srcRect,
3848 const SkIPoint& dstPoint) {
3849 SkASSERT(can_copy_texsubimage(dst, src, this));
bsalomon6df86402015-06-01 10:41:49 -07003850 GrGLIRect srcVP;
Brian Salomon71d9d842016-11-03 13:42:00 -04003851 this->bindSurfaceFBOForPixelOps(src, GR_GL_FRAMEBUFFER, &srcVP, kSrc_TempFBOTarget);
bsalomon083617b2016-02-12 12:10:14 -08003852 GrGLTexture* dstTex = static_cast<GrGLTexture *>(dst->asTexture());
bsalomon6df86402015-06-01 10:41:49 -07003853 SkASSERT(dstTex);
3854 // We modified the bound FBO
Robert Phillips294870f2016-11-11 12:38:40 -05003855 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon6df86402015-06-01 10:41:49 -07003856 GrGLIRect srcGLRect;
3857 srcGLRect.setRelativeTo(srcVP,
3858 srcRect.fLeft,
3859 srcRect.fTop,
3860 srcRect.width(),
3861 srcRect.height(),
3862 src->origin());
3863
3864 this->setScratchTextureUnit();
bsalomon10528f12015-10-14 12:54:52 -07003865 GL_CALL(BindTexture(dstTex->target(), dstTex->textureID()));
bsalomon6df86402015-06-01 10:41:49 -07003866 GrGLint dstY;
3867 if (kBottomLeft_GrSurfaceOrigin == dst->origin()) {
3868 dstY = dst->height() - (dstPoint.fY + srcGLRect.fHeight);
3869 } else {
3870 dstY = dstPoint.fY;
3871 }
bsalomon10528f12015-10-14 12:54:52 -07003872 GL_CALL(CopyTexSubImage2D(dstTex->target(), 0,
bsalomon083617b2016-02-12 12:10:14 -08003873 dstPoint.fX, dstY,
3874 srcGLRect.fLeft, srcGLRect.fBottom,
3875 srcGLRect.fWidth, srcGLRect.fHeight));
Brian Salomon71d9d842016-11-03 13:42:00 -04003876 this->unbindTextureFBOForPixelOps(GR_GL_FRAMEBUFFER, src);
bsalomon083617b2016-02-12 12:10:14 -08003877 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
3878 srcRect.width(), srcRect.height());
3879 this->didWriteToSurface(dst, &dstRect);
bsalomon6df86402015-06-01 10:41:49 -07003880}
3881
3882bool GrGLGpu::copySurfaceAsBlitFramebuffer(GrSurface* dst,
3883 GrSurface* src,
3884 const SkIRect& srcRect,
3885 const SkIPoint& dstPoint) {
Brian Salomone5e7eb12016-10-14 16:18:33 -04003886 SkASSERT(can_blit_framebuffer_for_copy_surface(dst, src, srcRect, dstPoint, this));
bsalomon6df86402015-06-01 10:41:49 -07003887 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
3888 srcRect.width(), srcRect.height());
3889 if (dst == src) {
3890 if (SkIRect::IntersectsNoEmptyCheck(dstRect, srcRect)) {
3891 return false;
mtklein404b3b22015-05-18 09:29:10 -07003892 }
bsalomon5df6fee2015-05-18 06:26:15 -07003893 }
bsalomon6df86402015-06-01 10:41:49 -07003894
bsalomon6df86402015-06-01 10:41:49 -07003895 GrGLIRect dstVP;
3896 GrGLIRect srcVP;
Brian Salomon71d9d842016-11-03 13:42:00 -04003897 this->bindSurfaceFBOForPixelOps(dst, GR_GL_DRAW_FRAMEBUFFER, &dstVP, kDst_TempFBOTarget);
3898 this->bindSurfaceFBOForPixelOps(src, GR_GL_READ_FRAMEBUFFER, &srcVP, kSrc_TempFBOTarget);
bsalomon6df86402015-06-01 10:41:49 -07003899 // We modified the bound FBO
Robert Phillips294870f2016-11-11 12:38:40 -05003900 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon6df86402015-06-01 10:41:49 -07003901 GrGLIRect srcGLRect;
3902 GrGLIRect dstGLRect;
3903 srcGLRect.setRelativeTo(srcVP,
3904 srcRect.fLeft,
3905 srcRect.fTop,
3906 srcRect.width(),
3907 srcRect.height(),
3908 src->origin());
3909 dstGLRect.setRelativeTo(dstVP,
3910 dstRect.fLeft,
3911 dstRect.fTop,
3912 dstRect.width(),
3913 dstRect.height(),
3914 dst->origin());
3915
3916 // BlitFrameBuffer respects the scissor, so disable it.
3917 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07003918 this->disableWindowRectangles();
bsalomon6df86402015-06-01 10:41:49 -07003919
3920 GrGLint srcY0;
3921 GrGLint srcY1;
3922 // Does the blit need to y-mirror or not?
3923 if (src->origin() == dst->origin()) {
3924 srcY0 = srcGLRect.fBottom;
3925 srcY1 = srcGLRect.fBottom + srcGLRect.fHeight;
3926 } else {
3927 srcY0 = srcGLRect.fBottom + srcGLRect.fHeight;
3928 srcY1 = srcGLRect.fBottom;
3929 }
3930 GL_CALL(BlitFramebuffer(srcGLRect.fLeft,
3931 srcY0,
3932 srcGLRect.fLeft + srcGLRect.fWidth,
3933 srcY1,
3934 dstGLRect.fLeft,
3935 dstGLRect.fBottom,
3936 dstGLRect.fLeft + dstGLRect.fWidth,
3937 dstGLRect.fBottom + dstGLRect.fHeight,
3938 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
Brian Salomon71d9d842016-11-03 13:42:00 -04003939 this->unbindTextureFBOForPixelOps(GR_GL_DRAW_FRAMEBUFFER, dst);
3940 this->unbindTextureFBOForPixelOps(GR_GL_READ_FRAMEBUFFER, src);
bsalomon083617b2016-02-12 12:10:14 -08003941 this->didWriteToSurface(dst, &dstRect);
bsalomon6df86402015-06-01 10:41:49 -07003942 return true;
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00003943}
3944
brianosman33f6b3f2016-06-02 05:49:21 -07003945// Manual implementation of mipmap generation, to work around driver bugs w/sRGB.
3946// Uses draw calls to do a series of downsample operations to successive mips.
3947// If this returns false, then the calling code falls back to using glGenerateMipmap.
3948bool GrGLGpu::generateMipmap(GrGLTexture* texture, bool gammaCorrect) {
Brian Salomonbf7b6202016-11-11 16:08:03 -05003949 SkASSERT(!GrPixelConfigIsSint(texture->config()));
brianosman09563ce2016-06-02 08:59:34 -07003950 // Our iterative downsample requires the ability to limit which level we're sampling:
3951 if (!this->glCaps().doManualMipmapping()) {
brianosman33f6b3f2016-06-02 05:49:21 -07003952 return false;
3953 }
3954
3955 // Mipmaps are only supported on 2D textures:
3956 if (GR_GL_TEXTURE_2D != texture->target()) {
3957 return false;
3958 }
3959
3960 // We need to be able to render to the texture for this to work:
Brian Salomon71d9d842016-11-03 13:42:00 -04003961 if (!this->glCaps().canConfigBeFBOColorAttachment(texture->config())) {
brianosman33f6b3f2016-06-02 05:49:21 -07003962 return false;
3963 }
3964
brianosman33f6b3f2016-06-02 05:49:21 -07003965 // If we're mipping an sRGB texture, we need to ensure FB sRGB is correct:
3966 if (GrPixelConfigIsSRGB(texture->config())) {
3967 // If we have write-control, just set the state that we want:
3968 if (this->glCaps().srgbWriteControl()) {
3969 this->flushFramebufferSRGB(gammaCorrect);
3970 } else if (!gammaCorrect) {
3971 // If we don't have write-control we can't do non-gamma-correct mipmapping:
3972 return false;
3973 }
3974 }
3975
3976 int width = texture->width();
3977 int height = texture->height();
3978 int levelCount = SkMipMap::ComputeLevelCount(width, height) + 1;
3979
3980 // Define all mips, if we haven't previously done so:
3981 if (0 == texture->texturePriv().maxMipMapLevel()) {
3982 GrGLenum internalFormat;
3983 GrGLenum externalFormat;
3984 GrGLenum externalType;
3985 if (!this->glCaps().getTexImageFormats(texture->config(), texture->config(),
3986 &internalFormat, &externalFormat, &externalType)) {
3987 return false;
3988 }
3989
3990 for (GrGLint level = 1; level < levelCount; ++level) {
3991 // Define the next mip:
3992 width = SkTMax(1, width / 2);
3993 height = SkTMax(1, height / 2);
3994 GL_ALLOC_CALL(this->glInterface(), TexImage2D(GR_GL_TEXTURE_2D, level, internalFormat,
3995 width, height, 0,
3996 externalFormat, externalType, nullptr));
3997 }
3998 }
3999
4000 // Create (if necessary), then bind temporary FBO:
4001 if (0 == fTempDstFBOID) {
4002 GL_CALL(GenFramebuffers(1, &fTempDstFBOID));
4003 }
4004 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, fTempDstFBOID));
Robert Phillips294870f2016-11-11 12:38:40 -05004005 fHWBoundRenderTargetUniqueID.makeInvalid();
brianosman33f6b3f2016-06-02 05:49:21 -07004006
4007 // Bind the texture, to get things configured for filtering.
4008 // We'll be changing our base level further below:
4009 this->setTextureUnit(0);
Brian Salomon514baff2016-11-17 15:17:07 -05004010 GrSamplerParams params(SkShader::kClamp_TileMode, GrSamplerParams::kBilerp_FilterMode);
brianosman33f6b3f2016-06-02 05:49:21 -07004011 this->bindTexture(0, params, gammaCorrect, texture);
4012
4013 // Vertex data:
4014 if (!fMipmapProgramArrayBuffer) {
4015 static const GrGLfloat vdata[] = {
4016 0, 0,
4017 0, 1,
4018 1, 0,
4019 1, 1
4020 };
4021 fMipmapProgramArrayBuffer.reset(GrGLBuffer::Create(this, sizeof(vdata),
4022 kVertex_GrBufferType,
4023 kStatic_GrAccessPattern, vdata));
4024 }
4025 if (!fMipmapProgramArrayBuffer) {
4026 return false;
4027 }
4028
4029 fHWVertexArrayState.setVertexArrayID(this, 0);
4030
4031 GrGLAttribArrayState* attribs = fHWVertexArrayState.bindInternalVertexArray(this);
Chris Dalton8e45b4f2017-05-05 14:00:56 -04004032 attribs->enableVertexArrays(this, 1);
Hal Canary144caf52016-11-07 17:57:18 -05004033 attribs->set(this, 0, fMipmapProgramArrayBuffer.get(), kVec2f_GrVertexAttribType,
brianosman33f6b3f2016-06-02 05:49:21 -07004034 2 * sizeof(GrGLfloat), 0);
brianosman33f6b3f2016-06-02 05:49:21 -07004035
4036 // Set "simple" state once:
4037 GrXferProcessor::BlendInfo blendInfo;
4038 blendInfo.reset();
4039 this->flushBlend(blendInfo, GrSwizzle::RGBA());
4040 this->flushColorWrite(true);
brianosman33f6b3f2016-06-02 05:49:21 -07004041 this->flushHWAAState(nullptr, false, false);
4042 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07004043 this->disableWindowRectangles();
csmartdaltonc7d85332016-10-26 10:13:46 -07004044 this->disableStencil();
brianosman33f6b3f2016-06-02 05:49:21 -07004045
4046 // Do all the blits:
4047 width = texture->width();
4048 height = texture->height();
4049 GrGLIRect viewport;
4050 viewport.fLeft = 0;
4051 viewport.fBottom = 0;
4052 for (GrGLint level = 1; level < levelCount; ++level) {
4053 // Get and bind the program for this particular downsample (filter shape can vary):
4054 int progIdx = TextureSizeToMipmapProgramIdx(width, height);
4055 if (!fMipmapPrograms[progIdx].fProgram) {
4056 if (!this->createMipmapProgram(progIdx)) {
4057 SkDebugf("Failed to create mipmap program.\n");
4058 return false;
4059 }
4060 }
4061 GL_CALL(UseProgram(fMipmapPrograms[progIdx].fProgram));
4062 fHWProgramID = fMipmapPrograms[progIdx].fProgram;
4063
4064 // Texcoord uniform is expected to contain (1/w, (w-1)/w, 1/h, (h-1)/h)
4065 const float invWidth = 1.0f / width;
4066 const float invHeight = 1.0f / height;
4067 GL_CALL(Uniform4f(fMipmapPrograms[progIdx].fTexCoordXformUniform,
4068 invWidth, (width - 1) * invWidth, invHeight, (height - 1) * invHeight));
4069 GL_CALL(Uniform1i(fMipmapPrograms[progIdx].fTextureUniform, 0));
4070
4071 // Only sample from previous mip
4072 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_BASE_LEVEL, level - 1));
4073
4074 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
4075 GR_GL_TEXTURE_2D, texture->textureID(), level));
4076
4077 width = SkTMax(1, width / 2);
4078 height = SkTMax(1, height / 2);
4079 viewport.fWidth = width;
4080 viewport.fHeight = height;
4081 this->flushViewport(viewport);
4082
4083 GL_CALL(DrawArrays(GR_GL_TRIANGLE_STRIP, 0, 4));
4084 }
4085
4086 // Unbind:
4087 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
4088 GR_GL_TEXTURE_2D, 0, 0));
4089
4090 return true;
4091}
4092
csmartdaltonc25c5d72016-11-01 07:03:59 -07004093void GrGLGpu::onQueryMultisampleSpecs(GrRenderTarget* rt, const GrStencilSettings& stencil,
4094 int* effectiveSampleCnt, SamplePattern* samplePattern) {
Brian Salomon7c8460e2017-05-12 11:36:10 -04004095 SkASSERT(GrFSAAType::kMixedSamples != rt->fsaaType() ||
4096 rt->renderTargetPriv().getStencilAttachment() || stencil.isDisabled());
cdalton28f45b92016-03-07 13:58:26 -08004097
4098 this->flushStencil(stencil);
4099 this->flushHWAAState(rt, true, !stencil.isDisabled());
4100 this->flushRenderTarget(static_cast<GrGLRenderTarget*>(rt), &SkIRect::EmptyIRect());
4101
4102 if (0 != this->caps()->maxRasterSamples()) {
4103 GR_GL_GetIntegerv(this->glInterface(), GR_GL_EFFECTIVE_RASTER_SAMPLES, effectiveSampleCnt);
4104 } else {
4105 GR_GL_GetIntegerv(this->glInterface(), GR_GL_SAMPLES, effectiveSampleCnt);
4106 }
4107
Brian Salomon154ce912017-05-17 10:40:02 -04004108 SkASSERT(*effectiveSampleCnt >= rt->numStencilSamples());
cdalton28f45b92016-03-07 13:58:26 -08004109
4110 if (this->caps()->sampleLocationsSupport()) {
csmartdalton0d28e572016-07-06 09:59:43 -07004111 samplePattern->reset(*effectiveSampleCnt);
cdalton28f45b92016-03-07 13:58:26 -08004112 for (int i = 0; i < *effectiveSampleCnt; ++i) {
4113 GrGLfloat pos[2];
4114 GL_CALL(GetMultisamplefv(GR_GL_SAMPLE_POSITION, i, pos));
4115 if (kTopLeft_GrSurfaceOrigin == rt->origin()) {
csmartdalton0d28e572016-07-06 09:59:43 -07004116 (*samplePattern)[i].set(pos[0], pos[1]);
cdalton28f45b92016-03-07 13:58:26 -08004117 } else {
csmartdalton0d28e572016-07-06 09:59:43 -07004118 (*samplePattern)[i].set(pos[0], 1 - pos[1]);
cdalton28f45b92016-03-07 13:58:26 -08004119 }
4120 }
4121 }
4122}
4123
cdalton231c5fd2015-05-13 12:35:36 -07004124void GrGLGpu::xferBarrier(GrRenderTarget* rt, GrXferBarrierType type) {
bsalomoncb02b382015-08-12 11:14:50 -07004125 SkASSERT(type);
cdalton9954bc32015-04-29 14:17:00 -07004126 switch (type) {
cdalton231c5fd2015-05-13 12:35:36 -07004127 case kTexture_GrXferBarrierType: {
4128 GrGLRenderTarget* glrt = static_cast<GrGLRenderTarget*>(rt);
4129 if (glrt->textureFBOID() != glrt->renderFBOID()) {
4130 // The render target uses separate storage so no need for glTextureBarrier.
4131 // FIXME: The render target will resolve automatically when its texture is bound,
4132 // but we could resolve only the bounds that will be read if we do it here instead.
4133 return;
4134 }
cdalton9954bc32015-04-29 14:17:00 -07004135 SkASSERT(this->caps()->textureBarrierSupport());
4136 GL_CALL(TextureBarrier());
4137 return;
cdalton231c5fd2015-05-13 12:35:36 -07004138 }
cdalton8917d622015-05-06 13:40:21 -07004139 case kBlend_GrXferBarrierType:
bsalomon4b91f762015-05-19 09:29:46 -07004140 SkASSERT(GrCaps::kAdvanced_BlendEquationSupport ==
cdalton8917d622015-05-06 13:40:21 -07004141 this->caps()->blendEquationSupport());
4142 GL_CALL(BlendBarrier());
4143 return;
bsalomoncb02b382015-08-12 11:14:50 -07004144 default: break; // placate compiler warnings that kNone not handled
cdalton9954bc32015-04-29 14:17:00 -07004145 }
4146}
4147
jvanverth88957922015-07-14 11:02:52 -07004148GrBackendObject GrGLGpu::createTestingOnlyBackendTexture(void* pixels, int w, int h,
egdaniel0a3a7f72016-06-24 09:22:31 -07004149 GrPixelConfig config, bool /*isRT*/) {
bsalomon926cb022015-12-17 18:15:11 -08004150 if (!this->caps()->isConfigTexturable(config)) {
4151 return false;
4152 }
Ben Wagner18b61f92016-10-25 10:44:02 -04004153 std::unique_ptr<GrGLTextureInfo> info = skstd::make_unique<GrGLTextureInfo>();
bsalomon091f60c2015-11-10 11:54:56 -08004154 info->fTarget = GR_GL_TEXTURE_2D;
kkinnunen546eb5c2015-12-11 00:05:33 -08004155 info->fID = 0;
bsalomon091f60c2015-11-10 11:54:56 -08004156 GL_CALL(GenTextures(1, &info->fID));
jvanverth672bb7f2015-07-13 07:19:57 -07004157 GL_CALL(ActiveTexture(GR_GL_TEXTURE0));
4158 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, 1));
bsalomon091f60c2015-11-10 11:54:56 -08004159 GL_CALL(BindTexture(info->fTarget, info->fID));
Robert Phillips294870f2016-11-11 12:38:40 -05004160 fHWBoundTextureUniqueIDs[0].makeInvalid();
bsalomon091f60c2015-11-10 11:54:56 -08004161 GL_CALL(TexParameteri(info->fTarget, GR_GL_TEXTURE_MAG_FILTER, GR_GL_NEAREST));
4162 GL_CALL(TexParameteri(info->fTarget, GR_GL_TEXTURE_MIN_FILTER, GR_GL_NEAREST));
4163 GL_CALL(TexParameteri(info->fTarget, GR_GL_TEXTURE_WRAP_S, GR_GL_CLAMP_TO_EDGE));
4164 GL_CALL(TexParameteri(info->fTarget, GR_GL_TEXTURE_WRAP_T, GR_GL_CLAMP_TO_EDGE));
jvanverth672bb7f2015-07-13 07:19:57 -07004165
bsalomon76148af2016-01-12 11:13:47 -08004166 GrGLenum internalFormat;
4167 GrGLenum externalFormat;
4168 GrGLenum externalType;
4169
4170 if (!this->glCaps().getTexImageFormats(config, config, &internalFormat, &externalFormat,
4171 &externalType)) {
bsalomon76148af2016-01-12 11:13:47 -08004172 return reinterpret_cast<GrBackendObject>(nullptr);
bsalomon76148af2016-01-12 11:13:47 -08004173 }
jvanverth672bb7f2015-07-13 07:19:57 -07004174
bsalomon091f60c2015-11-10 11:54:56 -08004175 GL_CALL(TexImage2D(info->fTarget, 0, internalFormat, w, h, 0, externalFormat,
jvanverth672bb7f2015-07-13 07:19:57 -07004176 externalType, pixels));
4177
Ben Wagner18b61f92016-10-25 10:44:02 -04004178 return reinterpret_cast<GrBackendObject>(info.release());
jvanverth672bb7f2015-07-13 07:19:57 -07004179}
4180
jvanverth88957922015-07-14 11:02:52 -07004181bool GrGLGpu::isTestingOnlyBackendTexture(GrBackendObject id) const {
bsalomon091f60c2015-11-10 11:54:56 -08004182 GrGLuint texID = reinterpret_cast<const GrGLTextureInfo*>(id)->fID;
jvanverth672bb7f2015-07-13 07:19:57 -07004183
4184 GrGLboolean result;
4185 GL_CALL_RET(result, IsTexture(texID));
4186
4187 return (GR_GL_TRUE == result);
4188}
4189
bsalomone63ffef2016-02-05 07:17:34 -08004190void GrGLGpu::deleteTestingOnlyBackendTexture(GrBackendObject id, bool abandonTexture) {
Ben Wagner18b61f92016-10-25 10:44:02 -04004191 std::unique_ptr<const GrGLTextureInfo> info(reinterpret_cast<const GrGLTextureInfo*>(id));
bsalomon091f60c2015-11-10 11:54:56 -08004192 GrGLuint texID = info->fID;
bsalomon091f60c2015-11-10 11:54:56 -08004193
bsalomon67d76202015-11-11 12:40:42 -08004194 if (!abandonTexture) {
4195 GL_CALL(DeleteTextures(1, &texID));
4196 }
jvanverth672bb7f2015-07-13 07:19:57 -07004197}
4198
joshualitt8fd844f2015-12-02 13:36:47 -08004199void GrGLGpu::resetShaderCacheForTesting() const {
4200 fProgramCache->abandon();
4201}
4202
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00004203///////////////////////////////////////////////////////////////////////////////
bsalomon6df86402015-06-01 10:41:49 -07004204
cdaltone2e71c22016-04-07 18:13:29 -07004205GrGLAttribArrayState* GrGLGpu::HWVertexArrayState::bindInternalVertexArray(GrGLGpu* gpu,
csmartdalton485a1202016-07-13 10:16:32 -07004206 const GrBuffer* ibuf) {
robertphillips@google.com4f65a272013-03-26 19:40:46 +00004207 GrGLAttribArrayState* attribState;
4208
cdaltone2e71c22016-04-07 18:13:29 -07004209 if (gpu->glCaps().isCoreProfile()) {
4210 if (!fCoreProfileVertexArray) {
bsalomon@google.com6918d482013-03-07 19:09:11 +00004211 GrGLuint arrayID;
4212 GR_GL_CALL(gpu->glInterface(), GenVertexArrays(1, &arrayID));
4213 int attrCount = gpu->glCaps().maxVertexAttributes();
cdaltone2e71c22016-04-07 18:13:29 -07004214 fCoreProfileVertexArray = new GrGLVertexArray(arrayID, attrCount);
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00004215 }
cdaltone2e71c22016-04-07 18:13:29 -07004216 if (ibuf) {
4217 attribState = fCoreProfileVertexArray->bindWithIndexBuffer(gpu, ibuf);
bsalomon6df86402015-06-01 10:41:49 -07004218 } else {
cdaltone2e71c22016-04-07 18:13:29 -07004219 attribState = fCoreProfileVertexArray->bind(gpu);
bsalomon6df86402015-06-01 10:41:49 -07004220 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00004221 } else {
cdaltone2e71c22016-04-07 18:13:29 -07004222 if (ibuf) {
4223 // bindBuffer implicitly binds VAO 0 when binding an index buffer.
4224 gpu->bindBuffer(kIndex_GrBufferType, ibuf);
bsalomon@google.com6918d482013-03-07 19:09:11 +00004225 } else {
4226 this->setVertexArrayID(gpu, 0);
4227 }
4228 int attrCount = gpu->glCaps().maxVertexAttributes();
4229 if (fDefaultVertexArrayAttribState.count() != attrCount) {
4230 fDefaultVertexArrayAttribState.resize(attrCount);
4231 }
4232 attribState = &fDefaultVertexArrayAttribState;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00004233 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00004234 return attribState;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00004235}
bsalomone179a912016-01-20 06:18:10 -08004236
Robert Phillips3798c862017-03-27 11:08:16 -04004237bool GrGLGpu::onIsACopyNeededForTextureParams(GrTextureProxy* proxy,
Robert Phillips81444fb2017-03-21 09:14:35 -04004238 const GrSamplerParams& textureParams,
4239 GrTextureProducer::CopyParams* copyParams,
4240 SkScalar scaleAdjust[2]) const {
Robert Phillips3798c862017-03-27 11:08:16 -04004241 const GrTexture* texture = proxy->priv().peekTexture();
4242 if (!texture) {
4243 // The only way to get and EXTERNAL or RECTANGLE texture in Ganesh is to wrap them.
4244 // In that case the proxy should already be instantiated.
4245 return false;
4246 }
4247
bsalomone179a912016-01-20 06:18:10 -08004248 if (textureParams.isTiled() ||
Brian Salomon514baff2016-11-17 15:17:07 -05004249 GrSamplerParams::kMipMap_FilterMode == textureParams.filterMode()) {
Robert Phillips3798c862017-03-27 11:08:16 -04004250 const GrGLTexture* glTexture = static_cast<const GrGLTexture*>(texture);
bsalomone179a912016-01-20 06:18:10 -08004251 if (GR_GL_TEXTURE_EXTERNAL == glTexture->target() ||
4252 GR_GL_TEXTURE_RECTANGLE == glTexture->target()) {
Brian Salomon514baff2016-11-17 15:17:07 -05004253 copyParams->fFilter = GrSamplerParams::kNone_FilterMode;
bsalomone179a912016-01-20 06:18:10 -08004254 copyParams->fWidth = texture->width();
4255 copyParams->fHeight = texture->height();
4256 return true;
4257 }
4258 }
4259 return false;
4260}
jvanverth84741b32016-09-30 08:39:02 -07004261
Greg Daniel6be35232017-03-01 17:01:09 -05004262GrFence SK_WARN_UNUSED_RESULT GrGLGpu::insertFence() {
4263 GrGLsync sync;
4264 GL_CALL_RET(sync, FenceSync(GR_GL_SYNC_GPU_COMMANDS_COMPLETE, 0));
4265 GR_STATIC_ASSERT(sizeof(GrFence) >= sizeof(GrGLsync));
4266 return (GrFence)sync;
jvanverth84741b32016-09-30 08:39:02 -07004267}
4268
Greg Daniel6be35232017-03-01 17:01:09 -05004269bool GrGLGpu::waitFence(GrFence fence, uint64_t timeout) {
jvanverth84741b32016-09-30 08:39:02 -07004270 GrGLenum result;
4271 GL_CALL_RET(result, ClientWaitSync((GrGLsync)fence, GR_GL_SYNC_FLUSH_COMMANDS_BIT, timeout));
4272 return (GR_GL_CONDITION_SATISFIED == result);
4273}
4274
4275void GrGLGpu::deleteFence(GrFence fence) const {
Greg Daniel6be35232017-03-01 17:01:09 -05004276 this->deleteSync((GrGLsync)fence);
4277}
4278
4279sk_sp<GrSemaphore> SK_WARN_UNUSED_RESULT GrGLGpu::makeSemaphore() {
4280 return GrGLSemaphore::Make(this);
4281}
4282
Brian Osmandc87c952017-04-28 13:57:38 -04004283void GrGLGpu::insertSemaphore(sk_sp<GrSemaphore> semaphore, bool flush) {
Greg Daniel6be35232017-03-01 17:01:09 -05004284 GrGLSemaphore* glSem = static_cast<GrGLSemaphore*>(semaphore.get());
4285
4286 GrGLsync sync;
4287 GL_CALL_RET(sync, FenceSync(GR_GL_SYNC_GPU_COMMANDS_COMPLETE, 0));
4288 glSem->setSync(sync);
Brian Osmandc87c952017-04-28 13:57:38 -04004289
4290 if (flush) {
4291 GL_CALL(Flush());
4292 }
Greg Daniel6be35232017-03-01 17:01:09 -05004293}
4294
4295void GrGLGpu::waitSemaphore(sk_sp<GrSemaphore> semaphore) {
4296 GrGLSemaphore* glSem = static_cast<GrGLSemaphore*>(semaphore.get());
4297
4298 GL_CALL(WaitSync(glSem->sync(), 0, GR_GL_TIMEOUT_IGNORED));
4299}
4300
4301void GrGLGpu::deleteSync(GrGLsync sync) const {
4302 GL_CALL(DeleteSync(sync));
jvanverth84741b32016-09-30 08:39:02 -07004303}
Brian Osman13dddce2017-05-09 13:19:50 -04004304
4305sk_sp<GrSemaphore> GrGLGpu::prepareTextureForCrossContextUsage(GrTexture* texture) {
4306 // Set up a semaphore to be signaled once the data is ready, and flush GL
4307 sk_sp<GrSemaphore> semaphore = this->makeSemaphore();
4308 this->insertSemaphore(semaphore, true);
4309
4310 return semaphore;
4311}
Robert Phillips646e4292017-06-13 12:44:56 -04004312
4313int GrGLGpu::TextureToCopyProgramIdx(GrTexture* texture) {
4314 switch (texture->texturePriv().samplerType()) {
4315 case kTexture2DSampler_GrSLType:
4316 return 0;
4317 case kITexture2DSampler_GrSLType:
4318 return 1;
4319 case kTexture2DRectSampler_GrSLType:
4320 return 2;
4321 case kTextureExternalSampler_GrSLType:
4322 return 3;
4323 default:
4324 SkFAIL("Unexpected samper type");
4325 return 0;
4326 }
4327}